2 * Copyright (c) 2005 Cisco Systems. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * $Id: ib_srp.c 3932 2005-11-01 17:19:29Z roland $
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/err.h>
39 #include <linux/string.h>
40 #include <linux/parser.h>
41 #include <linux/random.h>
42 #include <linux/jiffies.h>
44 #include <asm/atomic.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_device.h>
48 #include <scsi/scsi_dbg.h>
51 #include <rdma/ib_cache.h>
55 #define DRV_NAME "ib_srp"
56 #define PFX DRV_NAME ": "
57 #define DRV_VERSION "0.2"
58 #define DRV_RELDATE "November 1, 2005"
60 MODULE_AUTHOR("Roland Dreier");
61 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
62 "v" DRV_VERSION
" (" DRV_RELDATE
")");
63 MODULE_LICENSE("Dual BSD/GPL");
65 static int topspin_workarounds
= 1;
67 module_param(topspin_workarounds
, int, 0444);
68 MODULE_PARM_DESC(topspin_workarounds
,
69 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
71 static const u8 topspin_oui
[3] = { 0x00, 0x05, 0xad };
73 static void srp_add_one(struct ib_device
*device
);
74 static void srp_remove_one(struct ib_device
*device
);
75 static void srp_completion(struct ib_cq
*cq
, void *target_ptr
);
76 static int srp_cm_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*event
);
78 static struct ib_client srp_client
= {
81 .remove
= srp_remove_one
84 static inline struct srp_target_port
*host_to_target(struct Scsi_Host
*host
)
86 return (struct srp_target_port
*) host
->hostdata
;
89 static const char *srp_target_info(struct Scsi_Host
*host
)
91 return host_to_target(host
)->target_name
;
94 static struct srp_iu
*srp_alloc_iu(struct srp_host
*host
, size_t size
,
96 enum dma_data_direction direction
)
100 iu
= kmalloc(sizeof *iu
, gfp_mask
);
104 iu
->buf
= kzalloc(size
, gfp_mask
);
108 iu
->dma
= dma_map_single(host
->dev
->dma_device
, iu
->buf
, size
, direction
);
109 if (dma_mapping_error(iu
->dma
))
113 iu
->direction
= direction
;
125 static void srp_free_iu(struct srp_host
*host
, struct srp_iu
*iu
)
130 dma_unmap_single(host
->dev
->dma_device
, iu
->dma
, iu
->size
, iu
->direction
);
135 static void srp_qp_event(struct ib_event
*event
, void *context
)
137 printk(KERN_ERR PFX
"QP event %d\n", event
->event
);
140 static int srp_init_qp(struct srp_target_port
*target
,
143 struct ib_qp_attr
*attr
;
146 attr
= kmalloc(sizeof *attr
, GFP_KERNEL
);
150 ret
= ib_find_cached_pkey(target
->srp_host
->dev
,
151 target
->srp_host
->port
,
152 be16_to_cpu(target
->path
.pkey
),
157 attr
->qp_state
= IB_QPS_INIT
;
158 attr
->qp_access_flags
= (IB_ACCESS_REMOTE_READ
|
159 IB_ACCESS_REMOTE_WRITE
);
160 attr
->port_num
= target
->srp_host
->port
;
162 ret
= ib_modify_qp(qp
, attr
,
173 static int srp_create_target_ib(struct srp_target_port
*target
)
175 struct ib_qp_init_attr
*init_attr
;
178 init_attr
= kzalloc(sizeof *init_attr
, GFP_KERNEL
);
182 target
->cq
= ib_create_cq(target
->srp_host
->dev
, srp_completion
,
183 NULL
, target
, SRP_CQ_SIZE
);
184 if (IS_ERR(target
->cq
)) {
185 ret
= PTR_ERR(target
->cq
);
189 ib_req_notify_cq(target
->cq
, IB_CQ_NEXT_COMP
);
191 init_attr
->event_handler
= srp_qp_event
;
192 init_attr
->cap
.max_send_wr
= SRP_SQ_SIZE
;
193 init_attr
->cap
.max_recv_wr
= SRP_RQ_SIZE
;
194 init_attr
->cap
.max_recv_sge
= 1;
195 init_attr
->cap
.max_send_sge
= 1;
196 init_attr
->sq_sig_type
= IB_SIGNAL_ALL_WR
;
197 init_attr
->qp_type
= IB_QPT_RC
;
198 init_attr
->send_cq
= target
->cq
;
199 init_attr
->recv_cq
= target
->cq
;
201 target
->qp
= ib_create_qp(target
->srp_host
->pd
, init_attr
);
202 if (IS_ERR(target
->qp
)) {
203 ret
= PTR_ERR(target
->qp
);
204 ib_destroy_cq(target
->cq
);
208 ret
= srp_init_qp(target
, target
->qp
);
210 ib_destroy_qp(target
->qp
);
211 ib_destroy_cq(target
->cq
);
220 static void srp_free_target_ib(struct srp_target_port
*target
)
224 ib_destroy_qp(target
->qp
);
225 ib_destroy_cq(target
->cq
);
227 for (i
= 0; i
< SRP_RQ_SIZE
; ++i
)
228 srp_free_iu(target
->srp_host
, target
->rx_ring
[i
]);
229 for (i
= 0; i
< SRP_SQ_SIZE
+ 1; ++i
)
230 srp_free_iu(target
->srp_host
, target
->tx_ring
[i
]);
233 static void srp_path_rec_completion(int status
,
234 struct ib_sa_path_rec
*pathrec
,
237 struct srp_target_port
*target
= target_ptr
;
239 target
->status
= status
;
241 printk(KERN_ERR PFX
"Got failed path rec status %d\n", status
);
243 target
->path
= *pathrec
;
244 complete(&target
->done
);
247 static int srp_lookup_path(struct srp_target_port
*target
)
249 target
->path
.numb_path
= 1;
251 init_completion(&target
->done
);
253 target
->path_query_id
= ib_sa_path_rec_get(target
->srp_host
->dev
,
254 target
->srp_host
->port
,
256 IB_SA_PATH_REC_DGID
|
257 IB_SA_PATH_REC_SGID
|
258 IB_SA_PATH_REC_NUMB_PATH
|
260 SRP_PATH_REC_TIMEOUT_MS
,
262 srp_path_rec_completion
,
263 target
, &target
->path_query
);
264 if (target
->path_query_id
< 0)
265 return target
->path_query_id
;
267 wait_for_completion(&target
->done
);
269 if (target
->status
< 0)
270 printk(KERN_WARNING PFX
"Path record query failed\n");
272 return target
->status
;
275 static int srp_send_req(struct srp_target_port
*target
)
278 struct ib_cm_req_param param
;
279 struct srp_login_req priv
;
283 req
= kzalloc(sizeof *req
, GFP_KERNEL
);
287 req
->param
.primary_path
= &target
->path
;
288 req
->param
.alternate_path
= NULL
;
289 req
->param
.service_id
= target
->service_id
;
290 req
->param
.qp_num
= target
->qp
->qp_num
;
291 req
->param
.qp_type
= target
->qp
->qp_type
;
292 req
->param
.private_data
= &req
->priv
;
293 req
->param
.private_data_len
= sizeof req
->priv
;
294 req
->param
.flow_control
= 1;
296 get_random_bytes(&req
->param
.starting_psn
, 4);
297 req
->param
.starting_psn
&= 0xffffff;
300 * Pick some arbitrary defaults here; we could make these
301 * module parameters if anyone cared about setting them.
303 req
->param
.responder_resources
= 4;
304 req
->param
.remote_cm_response_timeout
= 20;
305 req
->param
.local_cm_response_timeout
= 20;
306 req
->param
.retry_count
= 7;
307 req
->param
.rnr_retry_count
= 7;
308 req
->param
.max_cm_retries
= 15;
310 req
->priv
.opcode
= SRP_LOGIN_REQ
;
312 req
->priv
.req_it_iu_len
= cpu_to_be32(SRP_MAX_IU_LEN
);
313 req
->priv
.req_buf_fmt
= cpu_to_be16(SRP_BUF_FORMAT_DIRECT
|
314 SRP_BUF_FORMAT_INDIRECT
);
315 memcpy(req
->priv
.initiator_port_id
, target
->srp_host
->initiator_port_id
, 16);
317 * Topspin/Cisco SRP targets will reject our login unless we
318 * zero out the first 8 bytes of our initiator port ID. The
319 * second 8 bytes must be our local node GUID, but we always
322 if (topspin_workarounds
&& !memcmp(&target
->ioc_guid
, topspin_oui
, 3)) {
323 printk(KERN_DEBUG PFX
"Topspin/Cisco initiator port ID workaround "
324 "activated for target GUID %016llx\n",
325 (unsigned long long) be64_to_cpu(target
->ioc_guid
));
326 memset(req
->priv
.initiator_port_id
, 0, 8);
328 memcpy(req
->priv
.target_port_id
, &target
->id_ext
, 8);
329 memcpy(req
->priv
.target_port_id
+ 8, &target
->ioc_guid
, 8);
331 status
= ib_send_cm_req(target
->cm_id
, &req
->param
);
338 static void srp_disconnect_target(struct srp_target_port
*target
)
340 /* XXX should send SRP_I_LOGOUT request */
342 init_completion(&target
->done
);
343 if (ib_send_cm_dreq(target
->cm_id
, NULL
, 0)) {
344 printk(KERN_DEBUG PFX
"Sending CM DREQ failed\n");
347 wait_for_completion(&target
->done
);
350 static void srp_remove_work(void *target_ptr
)
352 struct srp_target_port
*target
= target_ptr
;
354 spin_lock_irq(target
->scsi_host
->host_lock
);
355 if (target
->state
!= SRP_TARGET_DEAD
) {
356 spin_unlock_irq(target
->scsi_host
->host_lock
);
359 target
->state
= SRP_TARGET_REMOVED
;
360 spin_unlock_irq(target
->scsi_host
->host_lock
);
362 mutex_lock(&target
->srp_host
->target_mutex
);
363 list_del(&target
->list
);
364 mutex_unlock(&target
->srp_host
->target_mutex
);
366 scsi_remove_host(target
->scsi_host
);
367 ib_destroy_cm_id(target
->cm_id
);
368 srp_free_target_ib(target
);
369 scsi_host_put(target
->scsi_host
);
372 static int srp_connect_target(struct srp_target_port
*target
)
376 ret
= srp_lookup_path(target
);
381 init_completion(&target
->done
);
382 ret
= srp_send_req(target
);
385 wait_for_completion(&target
->done
);
388 * The CM event handling code will set status to
389 * SRP_PORT_REDIRECT if we get a port redirect REJ
390 * back, or SRP_DLID_REDIRECT if we get a lid/qp
393 switch (target
->status
) {
397 case SRP_PORT_REDIRECT
:
398 ret
= srp_lookup_path(target
);
403 case SRP_DLID_REDIRECT
:
407 return target
->status
;
412 static void srp_unmap_data(struct scsi_cmnd
*scmnd
,
413 struct srp_target_port
*target
,
414 struct srp_request
*req
)
416 struct scatterlist
*scat
;
419 if (!scmnd
->request_buffer
||
420 (scmnd
->sc_data_direction
!= DMA_TO_DEVICE
&&
421 scmnd
->sc_data_direction
!= DMA_FROM_DEVICE
))
425 * This handling of non-SG commands can be killed when the
426 * SCSI midlayer no longer generates non-SG commands.
428 if (likely(scmnd
->use_sg
)) {
429 nents
= scmnd
->use_sg
;
430 scat
= scmnd
->request_buffer
;
433 scat
= &req
->fake_sg
;
436 dma_unmap_sg(target
->srp_host
->dev
->dma_device
, scat
, nents
,
437 scmnd
->sc_data_direction
);
440 static int srp_reconnect_target(struct srp_target_port
*target
)
442 struct ib_cm_id
*new_cm_id
;
443 struct ib_qp_attr qp_attr
;
444 struct srp_request
*req
;
449 spin_lock_irq(target
->scsi_host
->host_lock
);
450 if (target
->state
!= SRP_TARGET_LIVE
) {
451 spin_unlock_irq(target
->scsi_host
->host_lock
);
454 target
->state
= SRP_TARGET_CONNECTING
;
455 spin_unlock_irq(target
->scsi_host
->host_lock
);
457 srp_disconnect_target(target
);
459 * Now get a new local CM ID so that we avoid confusing the
460 * target in case things are really fouled up.
462 new_cm_id
= ib_create_cm_id(target
->srp_host
->dev
,
463 srp_cm_handler
, target
);
464 if (IS_ERR(new_cm_id
)) {
465 ret
= PTR_ERR(new_cm_id
);
468 ib_destroy_cm_id(target
->cm_id
);
469 target
->cm_id
= new_cm_id
;
471 qp_attr
.qp_state
= IB_QPS_RESET
;
472 ret
= ib_modify_qp(target
->qp
, &qp_attr
, IB_QP_STATE
);
476 ret
= srp_init_qp(target
, target
->qp
);
480 while (ib_poll_cq(target
->cq
, 1, &wc
) > 0)
483 list_for_each_entry(req
, &target
->req_queue
, list
) {
484 req
->scmnd
->result
= DID_RESET
<< 16;
485 req
->scmnd
->scsi_done(req
->scmnd
);
486 srp_unmap_data(req
->scmnd
, target
, req
);
492 INIT_LIST_HEAD(&target
->free_reqs
);
493 INIT_LIST_HEAD(&target
->req_queue
);
494 for (i
= 0; i
< SRP_SQ_SIZE
; ++i
)
495 list_add_tail(&target
->req_ring
[i
].list
, &target
->free_reqs
);
497 ret
= srp_connect_target(target
);
501 spin_lock_irq(target
->scsi_host
->host_lock
);
502 if (target
->state
== SRP_TARGET_CONNECTING
) {
504 target
->state
= SRP_TARGET_LIVE
;
507 spin_unlock_irq(target
->scsi_host
->host_lock
);
512 printk(KERN_ERR PFX
"reconnect failed (%d), removing target port.\n", ret
);
515 * We couldn't reconnect, so kill our target port off.
516 * However, we have to defer the real removal because we might
517 * be in the context of the SCSI error handler now, which
518 * would deadlock if we call scsi_remove_host().
520 spin_lock_irq(target
->scsi_host
->host_lock
);
521 if (target
->state
== SRP_TARGET_CONNECTING
) {
522 target
->state
= SRP_TARGET_DEAD
;
523 INIT_WORK(&target
->work
, srp_remove_work
, target
);
524 schedule_work(&target
->work
);
526 spin_unlock_irq(target
->scsi_host
->host_lock
);
531 static int srp_map_data(struct scsi_cmnd
*scmnd
, struct srp_target_port
*target
,
532 struct srp_request
*req
)
534 struct scatterlist
*scat
;
535 struct srp_cmd
*cmd
= req
->cmd
->buf
;
536 int len
, nents
, count
;
540 if (!scmnd
->request_buffer
|| scmnd
->sc_data_direction
== DMA_NONE
)
541 return sizeof (struct srp_cmd
);
543 if (scmnd
->sc_data_direction
!= DMA_FROM_DEVICE
&&
544 scmnd
->sc_data_direction
!= DMA_TO_DEVICE
) {
545 printk(KERN_WARNING PFX
"Unhandled data direction %d\n",
546 scmnd
->sc_data_direction
);
551 * This handling of non-SG commands can be killed when the
552 * SCSI midlayer no longer generates non-SG commands.
554 if (likely(scmnd
->use_sg
)) {
555 nents
= scmnd
->use_sg
;
556 scat
= scmnd
->request_buffer
;
559 scat
= &req
->fake_sg
;
560 sg_init_one(scat
, scmnd
->request_buffer
, scmnd
->request_bufflen
);
563 count
= dma_map_sg(target
->srp_host
->dev
->dma_device
, scat
, nents
,
564 scmnd
->sc_data_direction
);
567 struct srp_direct_buf
*buf
= (void *) cmd
->add_data
;
569 fmt
= SRP_DATA_DESC_DIRECT
;
571 buf
->va
= cpu_to_be64(sg_dma_address(scat
));
572 buf
->key
= cpu_to_be32(target
->srp_host
->mr
->rkey
);
573 buf
->len
= cpu_to_be32(sg_dma_len(scat
));
575 len
= sizeof (struct srp_cmd
) +
576 sizeof (struct srp_direct_buf
);
578 struct srp_indirect_buf
*buf
= (void *) cmd
->add_data
;
581 fmt
= SRP_DATA_DESC_INDIRECT
;
583 if (scmnd
->sc_data_direction
== DMA_TO_DEVICE
)
584 cmd
->data_out_desc_cnt
= count
;
586 cmd
->data_in_desc_cnt
= count
;
588 buf
->table_desc
.va
= cpu_to_be64(req
->cmd
->dma
+
591 buf
->table_desc
.key
=
592 cpu_to_be32(target
->srp_host
->mr
->rkey
);
593 buf
->table_desc
.len
=
594 cpu_to_be32(count
* sizeof (struct srp_direct_buf
));
596 for (i
= 0; i
< count
; ++i
) {
597 buf
->desc_list
[i
].va
= cpu_to_be64(sg_dma_address(&scat
[i
]));
598 buf
->desc_list
[i
].key
=
599 cpu_to_be32(target
->srp_host
->mr
->rkey
);
600 buf
->desc_list
[i
].len
= cpu_to_be32(sg_dma_len(&scat
[i
]));
602 datalen
+= sg_dma_len(&scat
[i
]);
605 buf
->len
= cpu_to_be32(datalen
);
607 len
= sizeof (struct srp_cmd
) +
608 sizeof (struct srp_indirect_buf
) +
609 count
* sizeof (struct srp_direct_buf
);
612 if (scmnd
->sc_data_direction
== DMA_TO_DEVICE
)
613 cmd
->buf_fmt
= fmt
<< 4;
620 static void srp_remove_req(struct srp_target_port
*target
, struct srp_request
*req
)
622 srp_unmap_data(req
->scmnd
, target
, req
);
623 list_move_tail(&req
->list
, &target
->free_reqs
);
626 static void srp_process_rsp(struct srp_target_port
*target
, struct srp_rsp
*rsp
)
628 struct srp_request
*req
;
629 struct scsi_cmnd
*scmnd
;
633 delta
= (s32
) be32_to_cpu(rsp
->req_lim_delta
);
635 spin_lock_irqsave(target
->scsi_host
->host_lock
, flags
);
637 target
->req_lim
+= delta
;
639 req
= &target
->req_ring
[rsp
->tag
& ~SRP_TAG_TSK_MGMT
];
641 if (unlikely(rsp
->tag
& SRP_TAG_TSK_MGMT
)) {
642 if (be32_to_cpu(rsp
->resp_data_len
) < 4)
643 req
->tsk_status
= -1;
645 req
->tsk_status
= rsp
->data
[3];
646 complete(&req
->done
);
650 printk(KERN_ERR
"Null scmnd for RSP w/tag %016llx\n",
651 (unsigned long long) rsp
->tag
);
652 scmnd
->result
= rsp
->status
;
654 if (rsp
->flags
& SRP_RSP_FLAG_SNSVALID
) {
655 memcpy(scmnd
->sense_buffer
, rsp
->data
+
656 be32_to_cpu(rsp
->resp_data_len
),
657 min_t(int, be32_to_cpu(rsp
->sense_data_len
),
658 SCSI_SENSE_BUFFERSIZE
));
661 if (rsp
->flags
& (SRP_RSP_FLAG_DOOVER
| SRP_RSP_FLAG_DOUNDER
))
662 scmnd
->resid
= be32_to_cpu(rsp
->data_out_res_cnt
);
663 else if (rsp
->flags
& (SRP_RSP_FLAG_DIOVER
| SRP_RSP_FLAG_DIUNDER
))
664 scmnd
->resid
= be32_to_cpu(rsp
->data_in_res_cnt
);
666 if (!req
->tsk_mgmt
) {
667 scmnd
->host_scribble
= (void *) -1L;
668 scmnd
->scsi_done(scmnd
);
670 srp_remove_req(target
, req
);
675 spin_unlock_irqrestore(target
->scsi_host
->host_lock
, flags
);
678 static void srp_reconnect_work(void *target_ptr
)
680 struct srp_target_port
*target
= target_ptr
;
682 srp_reconnect_target(target
);
685 static void srp_handle_recv(struct srp_target_port
*target
, struct ib_wc
*wc
)
690 iu
= target
->rx_ring
[wc
->wr_id
& ~SRP_OP_RECV
];
692 dma_sync_single_for_cpu(target
->srp_host
->dev
->dma_device
, iu
->dma
,
693 target
->max_ti_iu_len
, DMA_FROM_DEVICE
);
695 opcode
= *(u8
*) iu
->buf
;
700 printk(KERN_ERR PFX
"recv completion, opcode 0x%02x\n", opcode
);
702 for (i
= 0; i
< wc
->byte_len
; ++i
) {
704 printk(KERN_ERR
" [%02x] ", i
);
705 printk(" %02x", ((u8
*) iu
->buf
)[i
]);
706 if ((i
+ 1) % 8 == 0)
710 if (wc
->byte_len
% 8)
716 srp_process_rsp(target
, iu
->buf
);
720 /* XXX Handle target logout */
721 printk(KERN_WARNING PFX
"Got target logout request\n");
725 printk(KERN_WARNING PFX
"Unhandled SRP opcode 0x%02x\n", opcode
);
729 dma_sync_single_for_device(target
->srp_host
->dev
->dma_device
, iu
->dma
,
730 target
->max_ti_iu_len
, DMA_FROM_DEVICE
);
733 static void srp_completion(struct ib_cq
*cq
, void *target_ptr
)
735 struct srp_target_port
*target
= target_ptr
;
739 ib_req_notify_cq(cq
, IB_CQ_NEXT_COMP
);
740 while (ib_poll_cq(cq
, 1, &wc
) > 0) {
742 printk(KERN_ERR PFX
"failed %s status %d\n",
743 wc
.wr_id
& SRP_OP_RECV
? "receive" : "send",
745 spin_lock_irqsave(target
->scsi_host
->host_lock
, flags
);
746 if (target
->state
== SRP_TARGET_LIVE
)
747 schedule_work(&target
->work
);
748 spin_unlock_irqrestore(target
->scsi_host
->host_lock
, flags
);
752 if (wc
.wr_id
& SRP_OP_RECV
)
753 srp_handle_recv(target
, &wc
);
759 static int __srp_post_recv(struct srp_target_port
*target
)
763 struct ib_recv_wr wr
, *bad_wr
;
767 next
= target
->rx_head
& (SRP_RQ_SIZE
- 1);
768 wr
.wr_id
= next
| SRP_OP_RECV
;
769 iu
= target
->rx_ring
[next
];
772 list
.length
= iu
->size
;
773 list
.lkey
= target
->srp_host
->mr
->lkey
;
779 ret
= ib_post_recv(target
->qp
, &wr
, &bad_wr
);
786 static int srp_post_recv(struct srp_target_port
*target
)
791 spin_lock_irqsave(target
->scsi_host
->host_lock
, flags
);
792 ret
= __srp_post_recv(target
);
793 spin_unlock_irqrestore(target
->scsi_host
->host_lock
, flags
);
799 * Must be called with target->scsi_host->host_lock held to protect
800 * req_lim and tx_head. Lock cannot be dropped between call here and
801 * call to __srp_post_send().
803 static struct srp_iu
*__srp_get_tx_iu(struct srp_target_port
*target
)
805 if (target
->tx_head
- target
->tx_tail
>= SRP_SQ_SIZE
)
808 if (unlikely(target
->req_lim
< 1)) {
809 if (printk_ratelimit())
810 printk(KERN_DEBUG PFX
"Target has req_lim %d\n",
815 return target
->tx_ring
[target
->tx_head
& SRP_SQ_SIZE
];
819 * Must be called with target->scsi_host->host_lock held to protect
820 * req_lim and tx_head.
822 static int __srp_post_send(struct srp_target_port
*target
,
823 struct srp_iu
*iu
, int len
)
826 struct ib_send_wr wr
, *bad_wr
;
831 list
.lkey
= target
->srp_host
->mr
->lkey
;
834 wr
.wr_id
= target
->tx_head
& SRP_SQ_SIZE
;
837 wr
.opcode
= IB_WR_SEND
;
838 wr
.send_flags
= IB_SEND_SIGNALED
;
840 ret
= ib_post_send(target
->qp
, &wr
, &bad_wr
);
850 static int srp_queuecommand(struct scsi_cmnd
*scmnd
,
851 void (*done
)(struct scsi_cmnd
*))
853 struct srp_target_port
*target
= host_to_target(scmnd
->device
->host
);
854 struct srp_request
*req
;
859 if (target
->state
== SRP_TARGET_CONNECTING
)
862 if (target
->state
== SRP_TARGET_DEAD
||
863 target
->state
== SRP_TARGET_REMOVED
) {
864 scmnd
->result
= DID_BAD_TARGET
<< 16;
869 iu
= __srp_get_tx_iu(target
);
873 dma_sync_single_for_cpu(target
->srp_host
->dev
->dma_device
, iu
->dma
,
874 SRP_MAX_IU_LEN
, DMA_TO_DEVICE
);
876 req
= list_entry(target
->free_reqs
.next
, struct srp_request
, list
);
878 scmnd
->scsi_done
= done
;
880 scmnd
->host_scribble
= (void *) (long) req
->index
;
883 memset(cmd
, 0, sizeof *cmd
);
885 cmd
->opcode
= SRP_CMD
;
886 cmd
->lun
= cpu_to_be64((u64
) scmnd
->device
->lun
<< 48);
887 cmd
->tag
= req
->index
;
888 memcpy(cmd
->cdb
, scmnd
->cmnd
, scmnd
->cmd_len
);
893 req
->tsk_mgmt
= NULL
;
895 len
= srp_map_data(scmnd
, target
, req
);
897 printk(KERN_ERR PFX
"Failed to map data\n");
901 if (__srp_post_recv(target
)) {
902 printk(KERN_ERR PFX
"Recv failed\n");
906 dma_sync_single_for_device(target
->srp_host
->dev
->dma_device
, iu
->dma
,
907 SRP_MAX_IU_LEN
, DMA_TO_DEVICE
);
909 if (__srp_post_send(target
, iu
, len
)) {
910 printk(KERN_ERR PFX
"Send failed\n");
914 list_move_tail(&req
->list
, &target
->req_queue
);
919 srp_unmap_data(scmnd
, target
, req
);
922 return SCSI_MLQUEUE_HOST_BUSY
;
925 static int srp_alloc_iu_bufs(struct srp_target_port
*target
)
929 for (i
= 0; i
< SRP_RQ_SIZE
; ++i
) {
930 target
->rx_ring
[i
] = srp_alloc_iu(target
->srp_host
,
931 target
->max_ti_iu_len
,
932 GFP_KERNEL
, DMA_FROM_DEVICE
);
933 if (!target
->rx_ring
[i
])
937 for (i
= 0; i
< SRP_SQ_SIZE
+ 1; ++i
) {
938 target
->tx_ring
[i
] = srp_alloc_iu(target
->srp_host
,
940 GFP_KERNEL
, DMA_TO_DEVICE
);
941 if (!target
->tx_ring
[i
])
948 for (i
= 0; i
< SRP_RQ_SIZE
; ++i
) {
949 srp_free_iu(target
->srp_host
, target
->rx_ring
[i
]);
950 target
->rx_ring
[i
] = NULL
;
953 for (i
= 0; i
< SRP_SQ_SIZE
+ 1; ++i
) {
954 srp_free_iu(target
->srp_host
, target
->tx_ring
[i
]);
955 target
->tx_ring
[i
] = NULL
;
961 static void srp_cm_rej_handler(struct ib_cm_id
*cm_id
,
962 struct ib_cm_event
*event
,
963 struct srp_target_port
*target
)
965 struct ib_class_port_info
*cpi
;
968 switch (event
->param
.rej_rcvd
.reason
) {
969 case IB_CM_REJ_PORT_CM_REDIRECT
:
970 cpi
= event
->param
.rej_rcvd
.ari
;
971 target
->path
.dlid
= cpi
->redirect_lid
;
972 target
->path
.pkey
= cpi
->redirect_pkey
;
973 cm_id
->remote_cm_qpn
= be32_to_cpu(cpi
->redirect_qp
) & 0x00ffffff;
974 memcpy(target
->path
.dgid
.raw
, cpi
->redirect_gid
, 16);
976 target
->status
= target
->path
.dlid
?
977 SRP_DLID_REDIRECT
: SRP_PORT_REDIRECT
;
980 case IB_CM_REJ_PORT_REDIRECT
:
981 if (topspin_workarounds
&&
982 !memcmp(&target
->ioc_guid
, topspin_oui
, 3)) {
984 * Topspin/Cisco SRP gateways incorrectly send
985 * reject reason code 25 when they mean 24
988 memcpy(target
->path
.dgid
.raw
,
989 event
->param
.rej_rcvd
.ari
, 16);
991 printk(KERN_DEBUG PFX
"Topspin/Cisco redirect to target port GID %016llx%016llx\n",
992 (unsigned long long) be64_to_cpu(target
->path
.dgid
.global
.subnet_prefix
),
993 (unsigned long long) be64_to_cpu(target
->path
.dgid
.global
.interface_id
));
995 target
->status
= SRP_PORT_REDIRECT
;
997 printk(KERN_WARNING
" REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
998 target
->status
= -ECONNRESET
;
1002 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID
:
1003 printk(KERN_WARNING
" REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1004 target
->status
= -ECONNRESET
;
1007 case IB_CM_REJ_CONSUMER_DEFINED
:
1008 opcode
= *(u8
*) event
->private_data
;
1009 if (opcode
== SRP_LOGIN_REJ
) {
1010 struct srp_login_rej
*rej
= event
->private_data
;
1011 u32 reason
= be32_to_cpu(rej
->reason
);
1013 if (reason
== SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE
)
1014 printk(KERN_WARNING PFX
1015 "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1017 printk(KERN_WARNING PFX
1018 "SRP LOGIN REJECTED, reason 0x%08x\n", reason
);
1020 printk(KERN_WARNING
" REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1021 " opcode 0x%02x\n", opcode
);
1022 target
->status
= -ECONNRESET
;
1026 printk(KERN_WARNING
" REJ reason 0x%x\n",
1027 event
->param
.rej_rcvd
.reason
);
1028 target
->status
= -ECONNRESET
;
1032 static int srp_cm_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*event
)
1034 struct srp_target_port
*target
= cm_id
->context
;
1035 struct ib_qp_attr
*qp_attr
= NULL
;
1040 switch (event
->event
) {
1041 case IB_CM_REQ_ERROR
:
1042 printk(KERN_DEBUG PFX
"Sending CM REQ failed\n");
1044 target
->status
= -ECONNRESET
;
1047 case IB_CM_REP_RECEIVED
:
1049 opcode
= *(u8
*) event
->private_data
;
1051 if (opcode
== SRP_LOGIN_RSP
) {
1052 struct srp_login_rsp
*rsp
= event
->private_data
;
1054 target
->max_ti_iu_len
= be32_to_cpu(rsp
->max_ti_iu_len
);
1055 target
->req_lim
= be32_to_cpu(rsp
->req_lim_delta
);
1057 target
->scsi_host
->can_queue
= min(target
->req_lim
,
1058 target
->scsi_host
->can_queue
);
1060 printk(KERN_WARNING PFX
"Unhandled RSP opcode %#x\n", opcode
);
1061 target
->status
= -ECONNRESET
;
1065 target
->status
= srp_alloc_iu_bufs(target
);
1069 qp_attr
= kmalloc(sizeof *qp_attr
, GFP_KERNEL
);
1071 target
->status
= -ENOMEM
;
1075 qp_attr
->qp_state
= IB_QPS_RTR
;
1076 target
->status
= ib_cm_init_qp_attr(cm_id
, qp_attr
, &attr_mask
);
1080 target
->status
= ib_modify_qp(target
->qp
, qp_attr
, attr_mask
);
1084 target
->status
= srp_post_recv(target
);
1088 qp_attr
->qp_state
= IB_QPS_RTS
;
1089 target
->status
= ib_cm_init_qp_attr(cm_id
, qp_attr
, &attr_mask
);
1093 target
->status
= ib_modify_qp(target
->qp
, qp_attr
, attr_mask
);
1097 target
->status
= ib_send_cm_rtu(cm_id
, NULL
, 0);
1103 case IB_CM_REJ_RECEIVED
:
1104 printk(KERN_DEBUG PFX
"REJ received\n");
1107 srp_cm_rej_handler(cm_id
, event
, target
);
1110 case IB_CM_MRA_RECEIVED
:
1111 printk(KERN_ERR PFX
"MRA received\n");
1114 case IB_CM_DREP_RECEIVED
:
1117 case IB_CM_TIMEWAIT_EXIT
:
1118 printk(KERN_ERR PFX
"connection closed\n");
1125 printk(KERN_WARNING PFX
"Unhandled CM event %d\n", event
->event
);
1130 complete(&target
->done
);
1137 static int srp_send_tsk_mgmt(struct srp_target_port
*target
,
1138 struct srp_request
*req
, u8 func
)
1141 struct srp_tsk_mgmt
*tsk_mgmt
;
1143 spin_lock_irq(target
->scsi_host
->host_lock
);
1145 if (target
->state
== SRP_TARGET_DEAD
||
1146 target
->state
== SRP_TARGET_REMOVED
) {
1147 req
->scmnd
->result
= DID_BAD_TARGET
<< 16;
1151 init_completion(&req
->done
);
1153 iu
= __srp_get_tx_iu(target
);
1158 memset(tsk_mgmt
, 0, sizeof *tsk_mgmt
);
1160 tsk_mgmt
->opcode
= SRP_TSK_MGMT
;
1161 tsk_mgmt
->lun
= cpu_to_be64((u64
) req
->scmnd
->device
->lun
<< 48);
1162 tsk_mgmt
->tag
= req
->index
| SRP_TAG_TSK_MGMT
;
1163 tsk_mgmt
->tsk_mgmt_func
= func
;
1164 tsk_mgmt
->task_tag
= req
->index
;
1166 if (__srp_post_send(target
, iu
, sizeof *tsk_mgmt
))
1171 spin_unlock_irq(target
->scsi_host
->host_lock
);
1173 if (!wait_for_completion_timeout(&req
->done
,
1174 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS
)))
1180 spin_unlock_irq(target
->scsi_host
->host_lock
);
1184 static int srp_find_req(struct srp_target_port
*target
,
1185 struct scsi_cmnd
*scmnd
,
1186 struct srp_request
**req
)
1188 if (scmnd
->host_scribble
== (void *) -1L)
1191 *req
= &target
->req_ring
[(long) scmnd
->host_scribble
];
1196 static int srp_abort(struct scsi_cmnd
*scmnd
)
1198 struct srp_target_port
*target
= host_to_target(scmnd
->device
->host
);
1199 struct srp_request
*req
;
1202 printk(KERN_ERR
"SRP abort called\n");
1204 if (srp_find_req(target
, scmnd
, &req
))
1206 if (srp_send_tsk_mgmt(target
, req
, SRP_TSK_ABORT_TASK
))
1209 spin_lock_irq(target
->scsi_host
->host_lock
);
1211 if (req
->cmd_done
) {
1212 srp_remove_req(target
, req
);
1213 scmnd
->scsi_done(scmnd
);
1214 } else if (!req
->tsk_status
) {
1215 srp_remove_req(target
, req
);
1216 scmnd
->result
= DID_ABORT
<< 16;
1220 spin_unlock_irq(target
->scsi_host
->host_lock
);
1225 static int srp_reset_device(struct scsi_cmnd
*scmnd
)
1227 struct srp_target_port
*target
= host_to_target(scmnd
->device
->host
);
1228 struct srp_request
*req
, *tmp
;
1230 printk(KERN_ERR
"SRP reset_device called\n");
1232 if (srp_find_req(target
, scmnd
, &req
))
1234 if (srp_send_tsk_mgmt(target
, req
, SRP_TSK_LUN_RESET
))
1236 if (req
->tsk_status
)
1239 spin_lock_irq(target
->scsi_host
->host_lock
);
1241 list_for_each_entry_safe(req
, tmp
, &target
->req_queue
, list
)
1242 if (req
->scmnd
->device
== scmnd
->device
) {
1243 req
->scmnd
->result
= DID_RESET
<< 16;
1244 req
->scmnd
->scsi_done(req
->scmnd
);
1245 srp_remove_req(target
, req
);
1248 spin_unlock_irq(target
->scsi_host
->host_lock
);
1253 static int srp_reset_host(struct scsi_cmnd
*scmnd
)
1255 struct srp_target_port
*target
= host_to_target(scmnd
->device
->host
);
1258 printk(KERN_ERR PFX
"SRP reset_host called\n");
1260 if (!srp_reconnect_target(target
))
1266 static ssize_t
show_id_ext(struct class_device
*cdev
, char *buf
)
1268 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1270 if (target
->state
== SRP_TARGET_DEAD
||
1271 target
->state
== SRP_TARGET_REMOVED
)
1274 return sprintf(buf
, "0x%016llx\n",
1275 (unsigned long long) be64_to_cpu(target
->id_ext
));
1278 static ssize_t
show_ioc_guid(struct class_device
*cdev
, char *buf
)
1280 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1282 if (target
->state
== SRP_TARGET_DEAD
||
1283 target
->state
== SRP_TARGET_REMOVED
)
1286 return sprintf(buf
, "0x%016llx\n",
1287 (unsigned long long) be64_to_cpu(target
->ioc_guid
));
1290 static ssize_t
show_service_id(struct class_device
*cdev
, char *buf
)
1292 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1294 if (target
->state
== SRP_TARGET_DEAD
||
1295 target
->state
== SRP_TARGET_REMOVED
)
1298 return sprintf(buf
, "0x%016llx\n",
1299 (unsigned long long) be64_to_cpu(target
->service_id
));
1302 static ssize_t
show_pkey(struct class_device
*cdev
, char *buf
)
1304 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1306 if (target
->state
== SRP_TARGET_DEAD
||
1307 target
->state
== SRP_TARGET_REMOVED
)
1310 return sprintf(buf
, "0x%04x\n", be16_to_cpu(target
->path
.pkey
));
1313 static ssize_t
show_dgid(struct class_device
*cdev
, char *buf
)
1315 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1317 if (target
->state
== SRP_TARGET_DEAD
||
1318 target
->state
== SRP_TARGET_REMOVED
)
1321 return sprintf(buf
, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1322 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[0]),
1323 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[1]),
1324 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[2]),
1325 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[3]),
1326 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[4]),
1327 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[5]),
1328 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[6]),
1329 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[7]));
1332 static CLASS_DEVICE_ATTR(id_ext
, S_IRUGO
, show_id_ext
, NULL
);
1333 static CLASS_DEVICE_ATTR(ioc_guid
, S_IRUGO
, show_ioc_guid
, NULL
);
1334 static CLASS_DEVICE_ATTR(service_id
, S_IRUGO
, show_service_id
, NULL
);
1335 static CLASS_DEVICE_ATTR(pkey
, S_IRUGO
, show_pkey
, NULL
);
1336 static CLASS_DEVICE_ATTR(dgid
, S_IRUGO
, show_dgid
, NULL
);
1338 static struct class_device_attribute
*srp_host_attrs
[] = {
1339 &class_device_attr_id_ext
,
1340 &class_device_attr_ioc_guid
,
1341 &class_device_attr_service_id
,
1342 &class_device_attr_pkey
,
1343 &class_device_attr_dgid
,
1347 static struct scsi_host_template srp_template
= {
1348 .module
= THIS_MODULE
,
1350 .info
= srp_target_info
,
1351 .queuecommand
= srp_queuecommand
,
1352 .eh_abort_handler
= srp_abort
,
1353 .eh_device_reset_handler
= srp_reset_device
,
1354 .eh_host_reset_handler
= srp_reset_host
,
1355 .can_queue
= SRP_SQ_SIZE
,
1357 .sg_tablesize
= SRP_MAX_INDIRECT
,
1358 .cmd_per_lun
= SRP_SQ_SIZE
,
1359 .use_clustering
= ENABLE_CLUSTERING
,
1360 .shost_attrs
= srp_host_attrs
1363 static int srp_add_target(struct srp_host
*host
, struct srp_target_port
*target
)
1365 sprintf(target
->target_name
, "SRP.T10:%016llX",
1366 (unsigned long long) be64_to_cpu(target
->id_ext
));
1368 if (scsi_add_host(target
->scsi_host
, host
->dev
->dma_device
))
1371 mutex_lock(&host
->target_mutex
);
1372 list_add_tail(&target
->list
, &host
->target_list
);
1373 mutex_unlock(&host
->target_mutex
);
1375 target
->state
= SRP_TARGET_LIVE
;
1377 /* XXX: are we supposed to have a definition of SCAN_WILD_CARD ?? */
1378 scsi_scan_target(&target
->scsi_host
->shost_gendev
,
1379 0, target
->scsi_id
, ~0, 0);
1384 static void srp_release_class_dev(struct class_device
*class_dev
)
1386 struct srp_host
*host
=
1387 container_of(class_dev
, struct srp_host
, class_dev
);
1389 complete(&host
->released
);
1392 static struct class srp_class
= {
1393 .name
= "infiniband_srp",
1394 .release
= srp_release_class_dev
1398 * Target ports are added by writing
1400 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1401 * pkey=<P_Key>,service_id=<service ID>
1403 * to the add_target sysfs attribute.
1407 SRP_OPT_ID_EXT
= 1 << 0,
1408 SRP_OPT_IOC_GUID
= 1 << 1,
1409 SRP_OPT_DGID
= 1 << 2,
1410 SRP_OPT_PKEY
= 1 << 3,
1411 SRP_OPT_SERVICE_ID
= 1 << 4,
1412 SRP_OPT_MAX_SECT
= 1 << 5,
1413 SRP_OPT_ALL
= (SRP_OPT_ID_EXT
|
1417 SRP_OPT_SERVICE_ID
),
1420 static match_table_t srp_opt_tokens
= {
1421 { SRP_OPT_ID_EXT
, "id_ext=%s" },
1422 { SRP_OPT_IOC_GUID
, "ioc_guid=%s" },
1423 { SRP_OPT_DGID
, "dgid=%s" },
1424 { SRP_OPT_PKEY
, "pkey=%x" },
1425 { SRP_OPT_SERVICE_ID
, "service_id=%s" },
1426 { SRP_OPT_MAX_SECT
, "max_sect=%d" },
1427 { SRP_OPT_ERR
, NULL
}
1430 static int srp_parse_options(const char *buf
, struct srp_target_port
*target
)
1432 char *options
, *sep_opt
;
1435 substring_t args
[MAX_OPT_ARGS
];
1441 options
= kstrdup(buf
, GFP_KERNEL
);
1446 while ((p
= strsep(&sep_opt
, ",")) != NULL
) {
1450 token
= match_token(p
, srp_opt_tokens
, args
);
1454 case SRP_OPT_ID_EXT
:
1455 p
= match_strdup(args
);
1456 target
->id_ext
= cpu_to_be64(simple_strtoull(p
, NULL
, 16));
1460 case SRP_OPT_IOC_GUID
:
1461 p
= match_strdup(args
);
1462 target
->ioc_guid
= cpu_to_be64(simple_strtoull(p
, NULL
, 16));
1467 p
= match_strdup(args
);
1468 if (strlen(p
) != 32) {
1469 printk(KERN_WARNING PFX
"bad dest GID parameter '%s'\n", p
);
1474 for (i
= 0; i
< 16; ++i
) {
1475 strlcpy(dgid
, p
+ i
* 2, 3);
1476 target
->path
.dgid
.raw
[i
] = simple_strtoul(dgid
, NULL
, 16);
1482 if (match_hex(args
, &token
)) {
1483 printk(KERN_WARNING PFX
"bad P_Key parameter '%s'\n", p
);
1486 target
->path
.pkey
= cpu_to_be16(token
);
1489 case SRP_OPT_SERVICE_ID
:
1490 p
= match_strdup(args
);
1491 target
->service_id
= cpu_to_be64(simple_strtoull(p
, NULL
, 16));
1495 case SRP_OPT_MAX_SECT
:
1496 if (match_int(args
, &token
)) {
1497 printk(KERN_WARNING PFX
"bad max sect parameter '%s'\n", p
);
1500 target
->scsi_host
->max_sectors
= token
;
1504 printk(KERN_WARNING PFX
"unknown parameter or missing value "
1505 "'%s' in target creation request\n", p
);
1510 if ((opt_mask
& SRP_OPT_ALL
) == SRP_OPT_ALL
)
1513 for (i
= 0; i
< ARRAY_SIZE(srp_opt_tokens
); ++i
)
1514 if ((srp_opt_tokens
[i
].token
& SRP_OPT_ALL
) &&
1515 !(srp_opt_tokens
[i
].token
& opt_mask
))
1516 printk(KERN_WARNING PFX
"target creation request is "
1517 "missing parameter '%s'\n",
1518 srp_opt_tokens
[i
].pattern
);
1525 static ssize_t
srp_create_target(struct class_device
*class_dev
,
1526 const char *buf
, size_t count
)
1528 struct srp_host
*host
=
1529 container_of(class_dev
, struct srp_host
, class_dev
);
1530 struct Scsi_Host
*target_host
;
1531 struct srp_target_port
*target
;
1535 target_host
= scsi_host_alloc(&srp_template
,
1536 sizeof (struct srp_target_port
));
1540 target_host
->max_lun
= SRP_MAX_LUN
;
1542 target
= host_to_target(target_host
);
1543 memset(target
, 0, sizeof *target
);
1545 target
->scsi_host
= target_host
;
1546 target
->srp_host
= host
;
1548 INIT_WORK(&target
->work
, srp_reconnect_work
, target
);
1550 INIT_LIST_HEAD(&target
->free_reqs
);
1551 INIT_LIST_HEAD(&target
->req_queue
);
1552 for (i
= 0; i
< SRP_SQ_SIZE
; ++i
) {
1553 target
->req_ring
[i
].index
= i
;
1554 list_add_tail(&target
->req_ring
[i
].list
, &target
->free_reqs
);
1557 ret
= srp_parse_options(buf
, target
);
1561 ib_get_cached_gid(host
->dev
, host
->port
, 0, &target
->path
.sgid
);
1563 printk(KERN_DEBUG PFX
"new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1564 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1565 (unsigned long long) be64_to_cpu(target
->id_ext
),
1566 (unsigned long long) be64_to_cpu(target
->ioc_guid
),
1567 be16_to_cpu(target
->path
.pkey
),
1568 (unsigned long long) be64_to_cpu(target
->service_id
),
1569 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[0]),
1570 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[2]),
1571 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[4]),
1572 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[6]),
1573 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[8]),
1574 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[10]),
1575 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[12]),
1576 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[14]));
1578 ret
= srp_create_target_ib(target
);
1582 target
->cm_id
= ib_create_cm_id(host
->dev
, srp_cm_handler
, target
);
1583 if (IS_ERR(target
->cm_id
)) {
1584 ret
= PTR_ERR(target
->cm_id
);
1588 ret
= srp_connect_target(target
);
1590 printk(KERN_ERR PFX
"Connection failed\n");
1594 ret
= srp_add_target(host
, target
);
1596 goto err_disconnect
;
1601 srp_disconnect_target(target
);
1604 ib_destroy_cm_id(target
->cm_id
);
1607 srp_free_target_ib(target
);
1610 scsi_host_put(target_host
);
1615 static CLASS_DEVICE_ATTR(add_target
, S_IWUSR
, NULL
, srp_create_target
);
1617 static ssize_t
show_ibdev(struct class_device
*class_dev
, char *buf
)
1619 struct srp_host
*host
=
1620 container_of(class_dev
, struct srp_host
, class_dev
);
1622 return sprintf(buf
, "%s\n", host
->dev
->name
);
1625 static CLASS_DEVICE_ATTR(ibdev
, S_IRUGO
, show_ibdev
, NULL
);
1627 static ssize_t
show_port(struct class_device
*class_dev
, char *buf
)
1629 struct srp_host
*host
=
1630 container_of(class_dev
, struct srp_host
, class_dev
);
1632 return sprintf(buf
, "%d\n", host
->port
);
1635 static CLASS_DEVICE_ATTR(port
, S_IRUGO
, show_port
, NULL
);
1637 static struct srp_host
*srp_add_port(struct ib_device
*device
, u8 port
)
1639 struct srp_host
*host
;
1641 host
= kzalloc(sizeof *host
, GFP_KERNEL
);
1645 INIT_LIST_HEAD(&host
->target_list
);
1646 mutex_init(&host
->target_mutex
);
1647 init_completion(&host
->released
);
1651 host
->initiator_port_id
[7] = port
;
1652 memcpy(host
->initiator_port_id
+ 8, &device
->node_guid
, 8);
1654 host
->pd
= ib_alloc_pd(device
);
1655 if (IS_ERR(host
->pd
))
1658 host
->mr
= ib_get_dma_mr(host
->pd
,
1659 IB_ACCESS_LOCAL_WRITE
|
1660 IB_ACCESS_REMOTE_READ
|
1661 IB_ACCESS_REMOTE_WRITE
);
1662 if (IS_ERR(host
->mr
))
1665 host
->class_dev
.class = &srp_class
;
1666 host
->class_dev
.dev
= device
->dma_device
;
1667 snprintf(host
->class_dev
.class_id
, BUS_ID_SIZE
, "srp-%s-%d",
1668 device
->name
, port
);
1670 if (class_device_register(&host
->class_dev
))
1672 if (class_device_create_file(&host
->class_dev
, &class_device_attr_add_target
))
1674 if (class_device_create_file(&host
->class_dev
, &class_device_attr_ibdev
))
1676 if (class_device_create_file(&host
->class_dev
, &class_device_attr_port
))
1682 class_device_unregister(&host
->class_dev
);
1685 ib_dereg_mr(host
->mr
);
1688 ib_dealloc_pd(host
->pd
);
1696 static void srp_add_one(struct ib_device
*device
)
1698 struct list_head
*dev_list
;
1699 struct srp_host
*host
;
1702 dev_list
= kmalloc(sizeof *dev_list
, GFP_KERNEL
);
1706 INIT_LIST_HEAD(dev_list
);
1708 if (device
->node_type
== IB_NODE_SWITCH
) {
1713 e
= device
->phys_port_cnt
;
1716 for (p
= s
; p
<= e
; ++p
) {
1717 host
= srp_add_port(device
, p
);
1719 list_add_tail(&host
->list
, dev_list
);
1722 ib_set_client_data(device
, &srp_client
, dev_list
);
1725 static void srp_remove_one(struct ib_device
*device
)
1727 struct list_head
*dev_list
;
1728 struct srp_host
*host
, *tmp_host
;
1729 LIST_HEAD(target_list
);
1730 struct srp_target_port
*target
, *tmp_target
;
1731 unsigned long flags
;
1733 dev_list
= ib_get_client_data(device
, &srp_client
);
1735 list_for_each_entry_safe(host
, tmp_host
, dev_list
, list
) {
1736 class_device_unregister(&host
->class_dev
);
1738 * Wait for the sysfs entry to go away, so that no new
1739 * target ports can be created.
1741 wait_for_completion(&host
->released
);
1744 * Mark all target ports as removed, so we stop queueing
1745 * commands and don't try to reconnect.
1747 mutex_lock(&host
->target_mutex
);
1748 list_for_each_entry_safe(target
, tmp_target
,
1749 &host
->target_list
, list
) {
1750 spin_lock_irqsave(target
->scsi_host
->host_lock
, flags
);
1751 if (target
->state
!= SRP_TARGET_REMOVED
)
1752 target
->state
= SRP_TARGET_REMOVED
;
1753 spin_unlock_irqrestore(target
->scsi_host
->host_lock
, flags
);
1755 mutex_unlock(&host
->target_mutex
);
1758 * Wait for any reconnection tasks that may have
1759 * started before we marked our target ports as
1760 * removed, and any target port removal tasks.
1762 flush_scheduled_work();
1764 list_for_each_entry_safe(target
, tmp_target
,
1765 &host
->target_list
, list
) {
1766 scsi_remove_host(target
->scsi_host
);
1767 srp_disconnect_target(target
);
1768 ib_destroy_cm_id(target
->cm_id
);
1769 srp_free_target_ib(target
);
1770 scsi_host_put(target
->scsi_host
);
1773 ib_dereg_mr(host
->mr
);
1774 ib_dealloc_pd(host
->pd
);
1781 static int __init
srp_init_module(void)
1785 ret
= class_register(&srp_class
);
1787 printk(KERN_ERR PFX
"couldn't register class infiniband_srp\n");
1791 ret
= ib_register_client(&srp_client
);
1793 printk(KERN_ERR PFX
"couldn't register IB client\n");
1794 class_unregister(&srp_class
);
1801 static void __exit
srp_cleanup_module(void)
1803 ib_unregister_client(&srp_client
);
1804 class_unregister(&srp_class
);
1807 module_init(srp_init_module
);
1808 module_exit(srp_cleanup_module
);