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>
43 #include <asm/atomic.h>
45 #include <scsi/scsi.h>
46 #include <scsi/scsi_device.h>
47 #include <scsi/scsi_dbg.h>
50 #include <rdma/ib_cache.h>
54 #define DRV_NAME "ib_srp"
55 #define PFX DRV_NAME ": "
56 #define DRV_VERSION "0.2"
57 #define DRV_RELDATE "November 1, 2005"
59 MODULE_AUTHOR("Roland Dreier");
60 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
61 "v" DRV_VERSION
" (" DRV_RELDATE
")");
62 MODULE_LICENSE("Dual BSD/GPL");
64 static int topspin_workarounds
= 1;
66 module_param(topspin_workarounds
, int, 0444);
67 MODULE_PARM_DESC(topspin_workarounds
,
68 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
70 static const u8 topspin_oui
[3] = { 0x00, 0x05, 0xad };
72 static void srp_add_one(struct ib_device
*device
);
73 static void srp_remove_one(struct ib_device
*device
);
74 static void srp_completion(struct ib_cq
*cq
, void *target_ptr
);
75 static int srp_cm_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*event
);
77 static struct ib_client srp_client
= {
80 .remove
= srp_remove_one
83 static inline struct srp_target_port
*host_to_target(struct Scsi_Host
*host
)
85 return (struct srp_target_port
*) host
->hostdata
;
88 static const char *srp_target_info(struct Scsi_Host
*host
)
90 return host_to_target(host
)->target_name
;
93 static struct srp_iu
*srp_alloc_iu(struct srp_host
*host
, size_t size
,
95 enum dma_data_direction direction
)
99 iu
= kmalloc(sizeof *iu
, gfp_mask
);
103 iu
->buf
= kzalloc(size
, gfp_mask
);
107 iu
->dma
= dma_map_single(host
->dev
->dma_device
, iu
->buf
, size
, direction
);
108 if (dma_mapping_error(iu
->dma
))
112 iu
->direction
= direction
;
124 static void srp_free_iu(struct srp_host
*host
, struct srp_iu
*iu
)
129 dma_unmap_single(host
->dev
->dma_device
, iu
->dma
, iu
->size
, iu
->direction
);
134 static void srp_qp_event(struct ib_event
*event
, void *context
)
136 printk(KERN_ERR PFX
"QP event %d\n", event
->event
);
139 static int srp_init_qp(struct srp_target_port
*target
,
142 struct ib_qp_attr
*attr
;
145 attr
= kmalloc(sizeof *attr
, GFP_KERNEL
);
149 ret
= ib_find_cached_pkey(target
->srp_host
->dev
,
150 target
->srp_host
->port
,
151 be16_to_cpu(target
->path
.pkey
),
156 attr
->qp_state
= IB_QPS_INIT
;
157 attr
->qp_access_flags
= (IB_ACCESS_REMOTE_READ
|
158 IB_ACCESS_REMOTE_WRITE
);
159 attr
->port_num
= target
->srp_host
->port
;
161 ret
= ib_modify_qp(qp
, attr
,
172 static int srp_create_target_ib(struct srp_target_port
*target
)
174 struct ib_qp_init_attr
*init_attr
;
177 init_attr
= kzalloc(sizeof *init_attr
, GFP_KERNEL
);
181 target
->cq
= ib_create_cq(target
->srp_host
->dev
, srp_completion
,
182 NULL
, target
, SRP_CQ_SIZE
);
183 if (IS_ERR(target
->cq
)) {
184 ret
= PTR_ERR(target
->cq
);
188 ib_req_notify_cq(target
->cq
, IB_CQ_NEXT_COMP
);
190 init_attr
->event_handler
= srp_qp_event
;
191 init_attr
->cap
.max_send_wr
= SRP_SQ_SIZE
;
192 init_attr
->cap
.max_recv_wr
= SRP_RQ_SIZE
;
193 init_attr
->cap
.max_recv_sge
= 1;
194 init_attr
->cap
.max_send_sge
= 1;
195 init_attr
->sq_sig_type
= IB_SIGNAL_ALL_WR
;
196 init_attr
->qp_type
= IB_QPT_RC
;
197 init_attr
->send_cq
= target
->cq
;
198 init_attr
->recv_cq
= target
->cq
;
200 target
->qp
= ib_create_qp(target
->srp_host
->pd
, init_attr
);
201 if (IS_ERR(target
->qp
)) {
202 ret
= PTR_ERR(target
->qp
);
203 ib_destroy_cq(target
->cq
);
207 ret
= srp_init_qp(target
, target
->qp
);
209 ib_destroy_qp(target
->qp
);
210 ib_destroy_cq(target
->cq
);
219 static void srp_free_target_ib(struct srp_target_port
*target
)
223 ib_destroy_qp(target
->qp
);
224 ib_destroy_cq(target
->cq
);
226 for (i
= 0; i
< SRP_RQ_SIZE
; ++i
)
227 srp_free_iu(target
->srp_host
, target
->rx_ring
[i
]);
228 for (i
= 0; i
< SRP_SQ_SIZE
+ 1; ++i
)
229 srp_free_iu(target
->srp_host
, target
->tx_ring
[i
]);
232 static void srp_path_rec_completion(int status
,
233 struct ib_sa_path_rec
*pathrec
,
236 struct srp_target_port
*target
= target_ptr
;
238 target
->status
= status
;
240 printk(KERN_ERR PFX
"Got failed path rec status %d\n", status
);
242 target
->path
= *pathrec
;
243 complete(&target
->done
);
246 static int srp_lookup_path(struct srp_target_port
*target
)
248 target
->path
.numb_path
= 1;
250 init_completion(&target
->done
);
252 target
->path_query_id
= ib_sa_path_rec_get(target
->srp_host
->dev
,
253 target
->srp_host
->port
,
255 IB_SA_PATH_REC_DGID
|
256 IB_SA_PATH_REC_SGID
|
257 IB_SA_PATH_REC_NUMB_PATH
|
259 SRP_PATH_REC_TIMEOUT_MS
,
261 srp_path_rec_completion
,
262 target
, &target
->path_query
);
263 if (target
->path_query_id
< 0)
264 return target
->path_query_id
;
266 wait_for_completion(&target
->done
);
268 if (target
->status
< 0)
269 printk(KERN_WARNING PFX
"Path record query failed\n");
271 return target
->status
;
274 static int srp_send_req(struct srp_target_port
*target
)
277 struct ib_cm_req_param param
;
278 struct srp_login_req priv
;
282 req
= kzalloc(sizeof *req
, GFP_KERNEL
);
286 req
->param
.primary_path
= &target
->path
;
287 req
->param
.alternate_path
= NULL
;
288 req
->param
.service_id
= target
->service_id
;
289 req
->param
.qp_num
= target
->qp
->qp_num
;
290 req
->param
.qp_type
= target
->qp
->qp_type
;
291 req
->param
.private_data
= &req
->priv
;
292 req
->param
.private_data_len
= sizeof req
->priv
;
293 req
->param
.flow_control
= 1;
295 get_random_bytes(&req
->param
.starting_psn
, 4);
296 req
->param
.starting_psn
&= 0xffffff;
299 * Pick some arbitrary defaults here; we could make these
300 * module parameters if anyone cared about setting them.
302 req
->param
.responder_resources
= 4;
303 req
->param
.remote_cm_response_timeout
= 20;
304 req
->param
.local_cm_response_timeout
= 20;
305 req
->param
.retry_count
= 7;
306 req
->param
.rnr_retry_count
= 7;
307 req
->param
.max_cm_retries
= 15;
309 req
->priv
.opcode
= SRP_LOGIN_REQ
;
311 req
->priv
.req_it_iu_len
= cpu_to_be32(SRP_MAX_IU_LEN
);
312 req
->priv
.req_buf_fmt
= cpu_to_be16(SRP_BUF_FORMAT_DIRECT
|
313 SRP_BUF_FORMAT_INDIRECT
);
314 memcpy(req
->priv
.initiator_port_id
, target
->srp_host
->initiator_port_id
, 16);
316 * Topspin/Cisco SRP targets will reject our login unless we
317 * zero out the first 8 bytes of our initiator port ID. The
318 * second 8 bytes must be our local node GUID, but we always
321 if (topspin_workarounds
&& !memcmp(&target
->ioc_guid
, topspin_oui
, 3)) {
322 printk(KERN_DEBUG PFX
"Topspin/Cisco initiator port ID workaround "
323 "activated for target GUID %016llx\n",
324 (unsigned long long) be64_to_cpu(target
->ioc_guid
));
325 memset(req
->priv
.initiator_port_id
, 0, 8);
327 memcpy(req
->priv
.target_port_id
, &target
->id_ext
, 8);
328 memcpy(req
->priv
.target_port_id
+ 8, &target
->ioc_guid
, 8);
330 status
= ib_send_cm_req(target
->cm_id
, &req
->param
);
337 static void srp_disconnect_target(struct srp_target_port
*target
)
339 /* XXX should send SRP_I_LOGOUT request */
341 init_completion(&target
->done
);
342 ib_send_cm_dreq(target
->cm_id
, NULL
, 0);
343 wait_for_completion(&target
->done
);
346 static void srp_remove_work(void *target_ptr
)
348 struct srp_target_port
*target
= target_ptr
;
350 spin_lock_irq(target
->scsi_host
->host_lock
);
351 if (target
->state
!= SRP_TARGET_DEAD
) {
352 spin_unlock_irq(target
->scsi_host
->host_lock
);
353 scsi_host_put(target
->scsi_host
);
356 target
->state
= SRP_TARGET_REMOVED
;
357 spin_unlock_irq(target
->scsi_host
->host_lock
);
359 down(&target
->srp_host
->target_mutex
);
360 list_del(&target
->list
);
361 up(&target
->srp_host
->target_mutex
);
363 scsi_remove_host(target
->scsi_host
);
364 ib_destroy_cm_id(target
->cm_id
);
365 srp_free_target_ib(target
);
366 scsi_host_put(target
->scsi_host
);
367 /* And another put to really free the target port... */
368 scsi_host_put(target
->scsi_host
);
371 static int srp_connect_target(struct srp_target_port
*target
)
375 ret
= srp_lookup_path(target
);
380 init_completion(&target
->done
);
381 ret
= srp_send_req(target
);
384 wait_for_completion(&target
->done
);
387 * The CM event handling code will set status to
388 * SRP_PORT_REDIRECT if we get a port redirect REJ
389 * back, or SRP_DLID_REDIRECT if we get a lid/qp
392 switch (target
->status
) {
396 case SRP_PORT_REDIRECT
:
397 ret
= srp_lookup_path(target
);
402 case SRP_DLID_REDIRECT
:
406 return target
->status
;
411 static int srp_reconnect_target(struct srp_target_port
*target
)
413 struct ib_cm_id
*new_cm_id
;
414 struct ib_qp_attr qp_attr
;
415 struct srp_request
*req
;
420 spin_lock_irq(target
->scsi_host
->host_lock
);
421 if (target
->state
!= SRP_TARGET_LIVE
) {
422 spin_unlock_irq(target
->scsi_host
->host_lock
);
425 target
->state
= SRP_TARGET_CONNECTING
;
426 spin_unlock_irq(target
->scsi_host
->host_lock
);
428 srp_disconnect_target(target
);
430 * Now get a new local CM ID so that we avoid confusing the
431 * target in case things are really fouled up.
433 new_cm_id
= ib_create_cm_id(target
->srp_host
->dev
,
434 srp_cm_handler
, target
);
435 if (IS_ERR(new_cm_id
)) {
436 ret
= PTR_ERR(new_cm_id
);
439 ib_destroy_cm_id(target
->cm_id
);
440 target
->cm_id
= new_cm_id
;
442 qp_attr
.qp_state
= IB_QPS_RESET
;
443 ret
= ib_modify_qp(target
->qp
, &qp_attr
, IB_QP_STATE
);
447 ret
= srp_init_qp(target
, target
->qp
);
451 while (ib_poll_cq(target
->cq
, 1, &wc
) > 0)
454 list_for_each_entry(req
, &target
->req_queue
, list
) {
455 req
->scmnd
->result
= DID_RESET
<< 16;
456 req
->scmnd
->scsi_done(req
->scmnd
);
462 target
->req_head
= 0;
463 for (i
= 0; i
< SRP_SQ_SIZE
- 1; ++i
)
464 target
->req_ring
[i
].next
= i
+ 1;
465 target
->req_ring
[SRP_SQ_SIZE
- 1].next
= -1;
466 INIT_LIST_HEAD(&target
->req_queue
);
468 ret
= srp_connect_target(target
);
472 spin_lock_irq(target
->scsi_host
->host_lock
);
473 if (target
->state
== SRP_TARGET_CONNECTING
) {
475 target
->state
= SRP_TARGET_LIVE
;
478 spin_unlock_irq(target
->scsi_host
->host_lock
);
483 printk(KERN_ERR PFX
"reconnect failed (%d), removing target port.\n", ret
);
486 * We couldn't reconnect, so kill our target port off.
487 * However, we have to defer the real removal because we might
488 * be in the context of the SCSI error handler now, which
489 * would deadlock if we call scsi_remove_host().
491 spin_lock_irq(target
->scsi_host
->host_lock
);
492 if (target
->state
== SRP_TARGET_CONNECTING
) {
493 target
->state
= SRP_TARGET_DEAD
;
494 INIT_WORK(&target
->work
, srp_remove_work
, target
);
495 schedule_work(&target
->work
);
497 spin_unlock_irq(target
->scsi_host
->host_lock
);
502 static int srp_map_data(struct scsi_cmnd
*scmnd
, struct srp_target_port
*target
,
503 struct srp_request
*req
)
505 struct srp_cmd
*cmd
= req
->cmd
->buf
;
509 if (!scmnd
->request_buffer
|| scmnd
->sc_data_direction
== DMA_NONE
)
510 return sizeof (struct srp_cmd
);
512 if (scmnd
->sc_data_direction
!= DMA_FROM_DEVICE
&&
513 scmnd
->sc_data_direction
!= DMA_TO_DEVICE
) {
514 printk(KERN_WARNING PFX
"Unhandled data direction %d\n",
515 scmnd
->sc_data_direction
);
520 struct scatterlist
*scat
= scmnd
->request_buffer
;
524 n
= dma_map_sg(target
->srp_host
->dev
->dma_device
,
525 scat
, scmnd
->use_sg
, scmnd
->sc_data_direction
);
528 struct srp_direct_buf
*buf
= (void *) cmd
->add_data
;
530 fmt
= SRP_DATA_DESC_DIRECT
;
532 buf
->va
= cpu_to_be64(sg_dma_address(scat
));
533 buf
->key
= cpu_to_be32(target
->srp_host
->mr
->rkey
);
534 buf
->len
= cpu_to_be32(sg_dma_len(scat
));
536 len
= sizeof (struct srp_cmd
) +
537 sizeof (struct srp_direct_buf
);
539 struct srp_indirect_buf
*buf
= (void *) cmd
->add_data
;
542 fmt
= SRP_DATA_DESC_INDIRECT
;
544 if (scmnd
->sc_data_direction
== DMA_TO_DEVICE
)
545 cmd
->data_out_desc_cnt
= n
;
547 cmd
->data_in_desc_cnt
= n
;
549 buf
->table_desc
.va
= cpu_to_be64(req
->cmd
->dma
+
552 buf
->table_desc
.key
=
553 cpu_to_be32(target
->srp_host
->mr
->rkey
);
554 buf
->table_desc
.len
=
555 cpu_to_be32(n
* sizeof (struct srp_direct_buf
));
557 for (i
= 0; i
< n
; ++i
) {
558 buf
->desc_list
[i
].va
= cpu_to_be64(sg_dma_address(&scat
[i
]));
559 buf
->desc_list
[i
].key
=
560 cpu_to_be32(target
->srp_host
->mr
->rkey
);
561 buf
->desc_list
[i
].len
= cpu_to_be32(sg_dma_len(&scat
[i
]));
563 datalen
+= sg_dma_len(&scat
[i
]);
566 buf
->len
= cpu_to_be32(datalen
);
568 len
= sizeof (struct srp_cmd
) +
569 sizeof (struct srp_indirect_buf
) +
570 n
* sizeof (struct srp_direct_buf
);
573 struct srp_direct_buf
*buf
= (void *) cmd
->add_data
;
576 dma
= dma_map_single(target
->srp_host
->dev
->dma_device
,
577 scmnd
->request_buffer
, scmnd
->request_bufflen
,
578 scmnd
->sc_data_direction
);
579 if (dma_mapping_error(dma
)) {
580 printk(KERN_WARNING PFX
"unable to map %p/%d (dir %d)\n",
581 scmnd
->request_buffer
, (int) scmnd
->request_bufflen
,
582 scmnd
->sc_data_direction
);
586 pci_unmap_addr_set(req
, direct_mapping
, dma
);
588 buf
->va
= cpu_to_be64(dma
);
589 buf
->key
= cpu_to_be32(target
->srp_host
->mr
->rkey
);
590 buf
->len
= cpu_to_be32(scmnd
->request_bufflen
);
592 fmt
= SRP_DATA_DESC_DIRECT
;
594 len
= sizeof (struct srp_cmd
) + sizeof (struct srp_direct_buf
);
597 if (scmnd
->sc_data_direction
== DMA_TO_DEVICE
)
598 cmd
->buf_fmt
= fmt
<< 4;
606 static void srp_unmap_data(struct scsi_cmnd
*scmnd
,
607 struct srp_target_port
*target
,
608 struct srp_request
*req
)
610 if (!scmnd
->request_buffer
||
611 (scmnd
->sc_data_direction
!= DMA_TO_DEVICE
&&
612 scmnd
->sc_data_direction
!= DMA_FROM_DEVICE
))
616 dma_unmap_sg(target
->srp_host
->dev
->dma_device
,
617 (struct scatterlist
*) scmnd
->request_buffer
,
618 scmnd
->use_sg
, scmnd
->sc_data_direction
);
620 dma_unmap_single(target
->srp_host
->dev
->dma_device
,
621 pci_unmap_addr(req
, direct_mapping
),
622 scmnd
->request_bufflen
,
623 scmnd
->sc_data_direction
);
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 srp_unmap_data(scmnd
, target
, req
);
668 if (!req
->tsk_mgmt
) {
670 scmnd
->host_scribble
= (void *) -1L;
671 scmnd
->scsi_done(scmnd
);
673 list_del(&req
->list
);
674 req
->next
= target
->req_head
;
675 target
->req_head
= rsp
->tag
& ~SRP_TAG_TSK_MGMT
;
680 spin_unlock_irqrestore(target
->scsi_host
->host_lock
, flags
);
683 static void srp_reconnect_work(void *target_ptr
)
685 struct srp_target_port
*target
= target_ptr
;
687 srp_reconnect_target(target
);
690 static void srp_handle_recv(struct srp_target_port
*target
, struct ib_wc
*wc
)
695 iu
= target
->rx_ring
[wc
->wr_id
& ~SRP_OP_RECV
];
697 dma_sync_single_for_cpu(target
->srp_host
->dev
->dma_device
, iu
->dma
,
698 target
->max_ti_iu_len
, DMA_FROM_DEVICE
);
700 opcode
= *(u8
*) iu
->buf
;
705 printk(KERN_ERR PFX
"recv completion, opcode 0x%02x\n", opcode
);
707 for (i
= 0; i
< wc
->byte_len
; ++i
) {
709 printk(KERN_ERR
" [%02x] ", i
);
710 printk(" %02x", ((u8
*) iu
->buf
)[i
]);
711 if ((i
+ 1) % 8 == 0)
715 if (wc
->byte_len
% 8)
721 srp_process_rsp(target
, iu
->buf
);
725 /* XXX Handle target logout */
726 printk(KERN_WARNING PFX
"Got target logout request\n");
730 printk(KERN_WARNING PFX
"Unhandled SRP opcode 0x%02x\n", opcode
);
734 dma_sync_single_for_device(target
->srp_host
->dev
->dma_device
, iu
->dma
,
735 target
->max_ti_iu_len
, DMA_FROM_DEVICE
);
738 static void srp_completion(struct ib_cq
*cq
, void *target_ptr
)
740 struct srp_target_port
*target
= target_ptr
;
744 ib_req_notify_cq(cq
, IB_CQ_NEXT_COMP
);
745 while (ib_poll_cq(cq
, 1, &wc
) > 0) {
747 printk(KERN_ERR PFX
"failed %s status %d\n",
748 wc
.wr_id
& SRP_OP_RECV
? "receive" : "send",
750 spin_lock_irqsave(target
->scsi_host
->host_lock
, flags
);
751 if (target
->state
== SRP_TARGET_LIVE
)
752 schedule_work(&target
->work
);
753 spin_unlock_irqrestore(target
->scsi_host
->host_lock
, flags
);
757 if (wc
.wr_id
& SRP_OP_RECV
)
758 srp_handle_recv(target
, &wc
);
764 static int __srp_post_recv(struct srp_target_port
*target
)
768 struct ib_recv_wr wr
, *bad_wr
;
772 next
= target
->rx_head
& (SRP_RQ_SIZE
- 1);
773 wr
.wr_id
= next
| SRP_OP_RECV
;
774 iu
= target
->rx_ring
[next
];
777 list
.length
= iu
->size
;
778 list
.lkey
= target
->srp_host
->mr
->lkey
;
784 ret
= ib_post_recv(target
->qp
, &wr
, &bad_wr
);
791 static int srp_post_recv(struct srp_target_port
*target
)
796 spin_lock_irqsave(target
->scsi_host
->host_lock
, flags
);
797 ret
= __srp_post_recv(target
);
798 spin_unlock_irqrestore(target
->scsi_host
->host_lock
, flags
);
804 * Must be called with target->scsi_host->host_lock held to protect
805 * req_lim and tx_head. Lock cannot be dropped between call here and
806 * call to __srp_post_send().
808 static struct srp_iu
*__srp_get_tx_iu(struct srp_target_port
*target
)
810 if (target
->tx_head
- target
->tx_tail
>= SRP_SQ_SIZE
)
813 if (unlikely(target
->req_lim
< 1)) {
814 if (printk_ratelimit())
815 printk(KERN_DEBUG PFX
"Target has req_lim %d\n",
820 return target
->tx_ring
[target
->tx_head
& SRP_SQ_SIZE
];
824 * Must be called with target->scsi_host->host_lock held to protect
825 * req_lim and tx_head.
827 static int __srp_post_send(struct srp_target_port
*target
,
828 struct srp_iu
*iu
, int len
)
831 struct ib_send_wr wr
, *bad_wr
;
836 list
.lkey
= target
->srp_host
->mr
->lkey
;
839 wr
.wr_id
= target
->tx_head
& SRP_SQ_SIZE
;
842 wr
.opcode
= IB_WR_SEND
;
843 wr
.send_flags
= IB_SEND_SIGNALED
;
845 ret
= ib_post_send(target
->qp
, &wr
, &bad_wr
);
855 static int srp_queuecommand(struct scsi_cmnd
*scmnd
,
856 void (*done
)(struct scsi_cmnd
*))
858 struct srp_target_port
*target
= host_to_target(scmnd
->device
->host
);
859 struct srp_request
*req
;
865 if (target
->state
== SRP_TARGET_CONNECTING
)
868 if (target
->state
== SRP_TARGET_DEAD
||
869 target
->state
== SRP_TARGET_REMOVED
) {
870 scmnd
->result
= DID_BAD_TARGET
<< 16;
875 iu
= __srp_get_tx_iu(target
);
879 dma_sync_single_for_cpu(target
->srp_host
->dev
->dma_device
, iu
->dma
,
880 SRP_MAX_IU_LEN
, DMA_TO_DEVICE
);
882 req_index
= target
->req_head
;
884 scmnd
->scsi_done
= done
;
886 scmnd
->host_scribble
= (void *) req_index
;
889 memset(cmd
, 0, sizeof *cmd
);
891 cmd
->opcode
= SRP_CMD
;
892 cmd
->lun
= cpu_to_be64((u64
) scmnd
->device
->lun
<< 48);
893 cmd
->tag
= req_index
;
894 memcpy(cmd
->cdb
, scmnd
->cmnd
, scmnd
->cmd_len
);
896 req
= &target
->req_ring
[req_index
];
901 req
->tsk_mgmt
= NULL
;
903 len
= srp_map_data(scmnd
, target
, req
);
905 printk(KERN_ERR PFX
"Failed to map data\n");
909 if (__srp_post_recv(target
)) {
910 printk(KERN_ERR PFX
"Recv failed\n");
914 dma_sync_single_for_device(target
->srp_host
->dev
->dma_device
, iu
->dma
,
915 SRP_MAX_IU_LEN
, DMA_TO_DEVICE
);
917 if (__srp_post_send(target
, iu
, len
)) {
918 printk(KERN_ERR PFX
"Send failed\n");
922 target
->req_head
= req
->next
;
923 list_add_tail(&req
->list
, &target
->req_queue
);
928 srp_unmap_data(scmnd
, target
, req
);
931 return SCSI_MLQUEUE_HOST_BUSY
;
934 static int srp_alloc_iu_bufs(struct srp_target_port
*target
)
938 for (i
= 0; i
< SRP_RQ_SIZE
; ++i
) {
939 target
->rx_ring
[i
] = srp_alloc_iu(target
->srp_host
,
940 target
->max_ti_iu_len
,
941 GFP_KERNEL
, DMA_FROM_DEVICE
);
942 if (!target
->rx_ring
[i
])
946 for (i
= 0; i
< SRP_SQ_SIZE
+ 1; ++i
) {
947 target
->tx_ring
[i
] = srp_alloc_iu(target
->srp_host
,
949 GFP_KERNEL
, DMA_TO_DEVICE
);
950 if (!target
->tx_ring
[i
])
957 for (i
= 0; i
< SRP_RQ_SIZE
; ++i
) {
958 srp_free_iu(target
->srp_host
, target
->rx_ring
[i
]);
959 target
->rx_ring
[i
] = NULL
;
962 for (i
= 0; i
< SRP_SQ_SIZE
+ 1; ++i
) {
963 srp_free_iu(target
->srp_host
, target
->tx_ring
[i
]);
964 target
->tx_ring
[i
] = NULL
;
970 static void srp_cm_rej_handler(struct ib_cm_id
*cm_id
,
971 struct ib_cm_event
*event
,
972 struct srp_target_port
*target
)
974 struct ib_class_port_info
*cpi
;
977 switch (event
->param
.rej_rcvd
.reason
) {
978 case IB_CM_REJ_PORT_CM_REDIRECT
:
979 cpi
= event
->param
.rej_rcvd
.ari
;
980 target
->path
.dlid
= cpi
->redirect_lid
;
981 target
->path
.pkey
= cpi
->redirect_pkey
;
982 cm_id
->remote_cm_qpn
= be32_to_cpu(cpi
->redirect_qp
) & 0x00ffffff;
983 memcpy(target
->path
.dgid
.raw
, cpi
->redirect_gid
, 16);
985 target
->status
= target
->path
.dlid
?
986 SRP_DLID_REDIRECT
: SRP_PORT_REDIRECT
;
989 case IB_CM_REJ_PORT_REDIRECT
:
990 if (topspin_workarounds
&&
991 !memcmp(&target
->ioc_guid
, topspin_oui
, 3)) {
993 * Topspin/Cisco SRP gateways incorrectly send
994 * reject reason code 25 when they mean 24
997 memcpy(target
->path
.dgid
.raw
,
998 event
->param
.rej_rcvd
.ari
, 16);
1000 printk(KERN_DEBUG PFX
"Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1001 (unsigned long long) be64_to_cpu(target
->path
.dgid
.global
.subnet_prefix
),
1002 (unsigned long long) be64_to_cpu(target
->path
.dgid
.global
.interface_id
));
1004 target
->status
= SRP_PORT_REDIRECT
;
1006 printk(KERN_WARNING
" REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1007 target
->status
= -ECONNRESET
;
1011 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID
:
1012 printk(KERN_WARNING
" REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1013 target
->status
= -ECONNRESET
;
1016 case IB_CM_REJ_CONSUMER_DEFINED
:
1017 opcode
= *(u8
*) event
->private_data
;
1018 if (opcode
== SRP_LOGIN_REJ
) {
1019 struct srp_login_rej
*rej
= event
->private_data
;
1020 u32 reason
= be32_to_cpu(rej
->reason
);
1022 if (reason
== SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE
)
1023 printk(KERN_WARNING PFX
1024 "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1026 printk(KERN_WARNING PFX
1027 "SRP LOGIN REJECTED, reason 0x%08x\n", reason
);
1029 printk(KERN_WARNING
" REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1030 " opcode 0x%02x\n", opcode
);
1031 target
->status
= -ECONNRESET
;
1035 printk(KERN_WARNING
" REJ reason 0x%x\n",
1036 event
->param
.rej_rcvd
.reason
);
1037 target
->status
= -ECONNRESET
;
1041 static int srp_cm_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*event
)
1043 struct srp_target_port
*target
= cm_id
->context
;
1044 struct ib_qp_attr
*qp_attr
= NULL
;
1049 switch (event
->event
) {
1050 case IB_CM_REQ_ERROR
:
1051 printk(KERN_DEBUG PFX
"Sending CM REQ failed\n");
1053 target
->status
= -ECONNRESET
;
1056 case IB_CM_REP_RECEIVED
:
1058 opcode
= *(u8
*) event
->private_data
;
1060 if (opcode
== SRP_LOGIN_RSP
) {
1061 struct srp_login_rsp
*rsp
= event
->private_data
;
1063 target
->max_ti_iu_len
= be32_to_cpu(rsp
->max_ti_iu_len
);
1064 target
->req_lim
= be32_to_cpu(rsp
->req_lim_delta
);
1066 target
->scsi_host
->can_queue
= min(target
->req_lim
,
1067 target
->scsi_host
->can_queue
);
1069 printk(KERN_WARNING PFX
"Unhandled RSP opcode %#x\n", opcode
);
1070 target
->status
= -ECONNRESET
;
1074 target
->status
= srp_alloc_iu_bufs(target
);
1078 qp_attr
= kmalloc(sizeof *qp_attr
, GFP_KERNEL
);
1080 target
->status
= -ENOMEM
;
1084 qp_attr
->qp_state
= IB_QPS_RTR
;
1085 target
->status
= ib_cm_init_qp_attr(cm_id
, qp_attr
, &attr_mask
);
1089 target
->status
= ib_modify_qp(target
->qp
, qp_attr
, attr_mask
);
1093 target
->status
= srp_post_recv(target
);
1097 qp_attr
->qp_state
= IB_QPS_RTS
;
1098 target
->status
= ib_cm_init_qp_attr(cm_id
, qp_attr
, &attr_mask
);
1102 target
->status
= ib_modify_qp(target
->qp
, qp_attr
, attr_mask
);
1106 target
->status
= ib_send_cm_rtu(cm_id
, NULL
, 0);
1112 case IB_CM_REJ_RECEIVED
:
1113 printk(KERN_DEBUG PFX
"REJ received\n");
1116 srp_cm_rej_handler(cm_id
, event
, target
);
1119 case IB_CM_MRA_RECEIVED
:
1120 printk(KERN_ERR PFX
"MRA received\n");
1123 case IB_CM_DREP_RECEIVED
:
1126 case IB_CM_TIMEWAIT_EXIT
:
1127 printk(KERN_ERR PFX
"connection closed\n");
1134 printk(KERN_WARNING PFX
"Unhandled CM event %d\n", event
->event
);
1139 complete(&target
->done
);
1146 static int srp_send_tsk_mgmt(struct scsi_cmnd
*scmnd
, u8 func
)
1148 struct srp_target_port
*target
= host_to_target(scmnd
->device
->host
);
1149 struct srp_request
*req
;
1151 struct srp_tsk_mgmt
*tsk_mgmt
;
1155 spin_lock_irq(target
->scsi_host
->host_lock
);
1157 if (scmnd
->host_scribble
== (void *) -1L)
1160 req_index
= (long) scmnd
->host_scribble
;
1161 printk(KERN_ERR
"Abort for req_index %d\n", req_index
);
1163 req
= &target
->req_ring
[req_index
];
1164 init_completion(&req
->done
);
1166 iu
= __srp_get_tx_iu(target
);
1171 memset(tsk_mgmt
, 0, sizeof *tsk_mgmt
);
1173 tsk_mgmt
->opcode
= SRP_TSK_MGMT
;
1174 tsk_mgmt
->lun
= cpu_to_be64((u64
) scmnd
->device
->lun
<< 48);
1175 tsk_mgmt
->tag
= req_index
| SRP_TAG_TSK_MGMT
;
1176 tsk_mgmt
->tsk_mgmt_func
= func
;
1177 tsk_mgmt
->task_tag
= req_index
;
1179 if (__srp_post_send(target
, iu
, sizeof *tsk_mgmt
))
1184 spin_unlock_irq(target
->scsi_host
->host_lock
);
1185 if (!wait_for_completion_timeout(&req
->done
,
1186 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS
)))
1188 spin_lock_irq(target
->scsi_host
->host_lock
);
1190 if (req
->cmd_done
) {
1191 list_del(&req
->list
);
1192 req
->next
= target
->req_head
;
1193 target
->req_head
= req_index
;
1195 scmnd
->scsi_done(scmnd
);
1196 } else if (!req
->tsk_status
) {
1197 scmnd
->result
= DID_ABORT
<< 16;
1202 spin_unlock_irq(target
->scsi_host
->host_lock
);
1206 static int srp_abort(struct scsi_cmnd
*scmnd
)
1208 printk(KERN_ERR
"SRP abort called\n");
1210 return srp_send_tsk_mgmt(scmnd
, SRP_TSK_ABORT_TASK
);
1213 static int srp_reset_device(struct scsi_cmnd
*scmnd
)
1215 printk(KERN_ERR
"SRP reset_device called\n");
1217 return srp_send_tsk_mgmt(scmnd
, SRP_TSK_LUN_RESET
);
1220 static int srp_reset_host(struct scsi_cmnd
*scmnd
)
1222 struct srp_target_port
*target
= host_to_target(scmnd
->device
->host
);
1225 printk(KERN_ERR PFX
"SRP reset_host called\n");
1227 if (!srp_reconnect_target(target
))
1233 static struct scsi_host_template srp_template
= {
1234 .module
= THIS_MODULE
,
1236 .info
= srp_target_info
,
1237 .queuecommand
= srp_queuecommand
,
1238 .eh_abort_handler
= srp_abort
,
1239 .eh_device_reset_handler
= srp_reset_device
,
1240 .eh_host_reset_handler
= srp_reset_host
,
1241 .can_queue
= SRP_SQ_SIZE
,
1243 .sg_tablesize
= SRP_MAX_INDIRECT
,
1244 .cmd_per_lun
= SRP_SQ_SIZE
,
1245 .use_clustering
= ENABLE_CLUSTERING
1248 static int srp_add_target(struct srp_host
*host
, struct srp_target_port
*target
)
1250 sprintf(target
->target_name
, "SRP.T10:%016llX",
1251 (unsigned long long) be64_to_cpu(target
->id_ext
));
1253 if (scsi_add_host(target
->scsi_host
, host
->dev
->dma_device
))
1256 down(&host
->target_mutex
);
1257 list_add_tail(&target
->list
, &host
->target_list
);
1258 up(&host
->target_mutex
);
1260 target
->state
= SRP_TARGET_LIVE
;
1262 /* XXX: are we supposed to have a definition of SCAN_WILD_CARD ?? */
1263 scsi_scan_target(&target
->scsi_host
->shost_gendev
,
1264 0, target
->scsi_id
, ~0, 0);
1269 static void srp_release_class_dev(struct class_device
*class_dev
)
1271 struct srp_host
*host
=
1272 container_of(class_dev
, struct srp_host
, class_dev
);
1274 complete(&host
->released
);
1277 static struct class srp_class
= {
1278 .name
= "infiniband_srp",
1279 .release
= srp_release_class_dev
1283 * Target ports are added by writing
1285 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1286 * pkey=<P_Key>,service_id=<service ID>
1288 * to the add_target sysfs attribute.
1292 SRP_OPT_ID_EXT
= 1 << 0,
1293 SRP_OPT_IOC_GUID
= 1 << 1,
1294 SRP_OPT_DGID
= 1 << 2,
1295 SRP_OPT_PKEY
= 1 << 3,
1296 SRP_OPT_SERVICE_ID
= 1 << 4,
1297 SRP_OPT_MAX_SECT
= 1 << 5,
1298 SRP_OPT_ALL
= (SRP_OPT_ID_EXT
|
1302 SRP_OPT_SERVICE_ID
),
1305 static match_table_t srp_opt_tokens
= {
1306 { SRP_OPT_ID_EXT
, "id_ext=%s" },
1307 { SRP_OPT_IOC_GUID
, "ioc_guid=%s" },
1308 { SRP_OPT_DGID
, "dgid=%s" },
1309 { SRP_OPT_PKEY
, "pkey=%x" },
1310 { SRP_OPT_SERVICE_ID
, "service_id=%s" },
1311 { SRP_OPT_MAX_SECT
, "max_sect=%d" },
1312 { SRP_OPT_ERR
, NULL
}
1315 static int srp_parse_options(const char *buf
, struct srp_target_port
*target
)
1317 char *options
, *sep_opt
;
1320 substring_t args
[MAX_OPT_ARGS
];
1326 options
= kstrdup(buf
, GFP_KERNEL
);
1331 while ((p
= strsep(&sep_opt
, ",")) != NULL
) {
1335 token
= match_token(p
, srp_opt_tokens
, args
);
1339 case SRP_OPT_ID_EXT
:
1340 p
= match_strdup(args
);
1341 target
->id_ext
= cpu_to_be64(simple_strtoull(p
, NULL
, 16));
1345 case SRP_OPT_IOC_GUID
:
1346 p
= match_strdup(args
);
1347 target
->ioc_guid
= cpu_to_be64(simple_strtoull(p
, NULL
, 16));
1352 p
= match_strdup(args
);
1353 if (strlen(p
) != 32) {
1354 printk(KERN_WARNING PFX
"bad dest GID parameter '%s'\n", p
);
1358 for (i
= 0; i
< 16; ++i
) {
1359 strlcpy(dgid
, p
+ i
* 2, 3);
1360 target
->path
.dgid
.raw
[i
] = simple_strtoul(dgid
, NULL
, 16);
1365 if (match_hex(args
, &token
)) {
1366 printk(KERN_WARNING PFX
"bad P_Key parameter '%s'\n", p
);
1369 target
->path
.pkey
= cpu_to_be16(token
);
1372 case SRP_OPT_SERVICE_ID
:
1373 p
= match_strdup(args
);
1374 target
->service_id
= cpu_to_be64(simple_strtoull(p
, NULL
, 16));
1378 case SRP_OPT_MAX_SECT
:
1379 if (match_int(args
, &token
)) {
1380 printk(KERN_WARNING PFX
"bad max sect parameter '%s'\n", p
);
1383 target
->scsi_host
->max_sectors
= token
;
1387 printk(KERN_WARNING PFX
"unknown parameter or missing value "
1388 "'%s' in target creation request\n", p
);
1393 if ((opt_mask
& SRP_OPT_ALL
) == SRP_OPT_ALL
)
1396 for (i
= 0; i
< ARRAY_SIZE(srp_opt_tokens
); ++i
)
1397 if ((srp_opt_tokens
[i
].token
& SRP_OPT_ALL
) &&
1398 !(srp_opt_tokens
[i
].token
& opt_mask
))
1399 printk(KERN_WARNING PFX
"target creation request is "
1400 "missing parameter '%s'\n",
1401 srp_opt_tokens
[i
].pattern
);
1408 static ssize_t
srp_create_target(struct class_device
*class_dev
,
1409 const char *buf
, size_t count
)
1411 struct srp_host
*host
=
1412 container_of(class_dev
, struct srp_host
, class_dev
);
1413 struct Scsi_Host
*target_host
;
1414 struct srp_target_port
*target
;
1418 target_host
= scsi_host_alloc(&srp_template
,
1419 sizeof (struct srp_target_port
));
1423 target_host
->max_lun
= SRP_MAX_LUN
;
1425 target
= host_to_target(target_host
);
1426 memset(target
, 0, sizeof *target
);
1428 target
->scsi_host
= target_host
;
1429 target
->srp_host
= host
;
1431 INIT_WORK(&target
->work
, srp_reconnect_work
, target
);
1433 for (i
= 0; i
< SRP_SQ_SIZE
- 1; ++i
)
1434 target
->req_ring
[i
].next
= i
+ 1;
1435 target
->req_ring
[SRP_SQ_SIZE
- 1].next
= -1;
1436 INIT_LIST_HEAD(&target
->req_queue
);
1438 ret
= srp_parse_options(buf
, target
);
1442 ib_get_cached_gid(host
->dev
, host
->port
, 0, &target
->path
.sgid
);
1444 printk(KERN_DEBUG PFX
"new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1445 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1446 (unsigned long long) be64_to_cpu(target
->id_ext
),
1447 (unsigned long long) be64_to_cpu(target
->ioc_guid
),
1448 be16_to_cpu(target
->path
.pkey
),
1449 (unsigned long long) be64_to_cpu(target
->service_id
),
1450 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[0]),
1451 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[2]),
1452 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[4]),
1453 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[6]),
1454 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[8]),
1455 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[10]),
1456 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[12]),
1457 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[14]));
1459 ret
= srp_create_target_ib(target
);
1463 target
->cm_id
= ib_create_cm_id(host
->dev
, srp_cm_handler
, target
);
1464 if (IS_ERR(target
->cm_id
)) {
1465 ret
= PTR_ERR(target
->cm_id
);
1469 ret
= srp_connect_target(target
);
1471 printk(KERN_ERR PFX
"Connection failed\n");
1475 ret
= srp_add_target(host
, target
);
1477 goto err_disconnect
;
1482 srp_disconnect_target(target
);
1485 ib_destroy_cm_id(target
->cm_id
);
1488 srp_free_target_ib(target
);
1491 scsi_host_put(target_host
);
1496 static CLASS_DEVICE_ATTR(add_target
, S_IWUSR
, NULL
, srp_create_target
);
1498 static ssize_t
show_ibdev(struct class_device
*class_dev
, char *buf
)
1500 struct srp_host
*host
=
1501 container_of(class_dev
, struct srp_host
, class_dev
);
1503 return sprintf(buf
, "%s\n", host
->dev
->name
);
1506 static CLASS_DEVICE_ATTR(ibdev
, S_IRUGO
, show_ibdev
, NULL
);
1508 static ssize_t
show_port(struct class_device
*class_dev
, char *buf
)
1510 struct srp_host
*host
=
1511 container_of(class_dev
, struct srp_host
, class_dev
);
1513 return sprintf(buf
, "%d\n", host
->port
);
1516 static CLASS_DEVICE_ATTR(port
, S_IRUGO
, show_port
, NULL
);
1518 static struct srp_host
*srp_add_port(struct ib_device
*device
,
1519 __be64 node_guid
, u8 port
)
1521 struct srp_host
*host
;
1523 host
= kzalloc(sizeof *host
, GFP_KERNEL
);
1527 INIT_LIST_HEAD(&host
->target_list
);
1528 init_MUTEX(&host
->target_mutex
);
1529 init_completion(&host
->released
);
1533 host
->initiator_port_id
[7] = port
;
1534 memcpy(host
->initiator_port_id
+ 8, &node_guid
, 8);
1536 host
->pd
= ib_alloc_pd(device
);
1537 if (IS_ERR(host
->pd
))
1540 host
->mr
= ib_get_dma_mr(host
->pd
,
1541 IB_ACCESS_LOCAL_WRITE
|
1542 IB_ACCESS_REMOTE_READ
|
1543 IB_ACCESS_REMOTE_WRITE
);
1544 if (IS_ERR(host
->mr
))
1547 host
->class_dev
.class = &srp_class
;
1548 host
->class_dev
.dev
= device
->dma_device
;
1549 snprintf(host
->class_dev
.class_id
, BUS_ID_SIZE
, "srp-%s-%d",
1550 device
->name
, port
);
1552 if (class_device_register(&host
->class_dev
))
1554 if (class_device_create_file(&host
->class_dev
, &class_device_attr_add_target
))
1556 if (class_device_create_file(&host
->class_dev
, &class_device_attr_ibdev
))
1558 if (class_device_create_file(&host
->class_dev
, &class_device_attr_port
))
1564 class_device_unregister(&host
->class_dev
);
1567 ib_dereg_mr(host
->mr
);
1570 ib_dealloc_pd(host
->pd
);
1578 static void srp_add_one(struct ib_device
*device
)
1580 struct list_head
*dev_list
;
1581 struct srp_host
*host
;
1582 struct ib_device_attr
*dev_attr
;
1585 dev_attr
= kmalloc(sizeof *dev_attr
, GFP_KERNEL
);
1589 if (ib_query_device(device
, dev_attr
)) {
1590 printk(KERN_WARNING PFX
"Couldn't query node GUID for %s.\n",
1595 dev_list
= kmalloc(sizeof *dev_list
, GFP_KERNEL
);
1599 INIT_LIST_HEAD(dev_list
);
1601 if (device
->node_type
== IB_NODE_SWITCH
) {
1606 e
= device
->phys_port_cnt
;
1609 for (p
= s
; p
<= e
; ++p
) {
1610 host
= srp_add_port(device
, dev_attr
->node_guid
, p
);
1612 list_add_tail(&host
->list
, dev_list
);
1615 ib_set_client_data(device
, &srp_client
, dev_list
);
1621 static void srp_remove_one(struct ib_device
*device
)
1623 struct list_head
*dev_list
;
1624 struct srp_host
*host
, *tmp_host
;
1625 LIST_HEAD(target_list
);
1626 struct srp_target_port
*target
, *tmp_target
;
1627 unsigned long flags
;
1629 dev_list
= ib_get_client_data(device
, &srp_client
);
1631 list_for_each_entry_safe(host
, tmp_host
, dev_list
, list
) {
1632 class_device_unregister(&host
->class_dev
);
1634 * Wait for the sysfs entry to go away, so that no new
1635 * target ports can be created.
1637 wait_for_completion(&host
->released
);
1640 * Mark all target ports as removed, so we stop queueing
1641 * commands and don't try to reconnect.
1643 down(&host
->target_mutex
);
1644 list_for_each_entry_safe(target
, tmp_target
,
1645 &host
->target_list
, list
) {
1646 spin_lock_irqsave(target
->scsi_host
->host_lock
, flags
);
1647 if (target
->state
!= SRP_TARGET_REMOVED
)
1648 target
->state
= SRP_TARGET_REMOVED
;
1649 spin_unlock_irqrestore(target
->scsi_host
->host_lock
, flags
);
1651 up(&host
->target_mutex
);
1654 * Wait for any reconnection tasks that may have
1655 * started before we marked our target ports as
1656 * removed, and any target port removal tasks.
1658 flush_scheduled_work();
1660 list_for_each_entry_safe(target
, tmp_target
,
1661 &host
->target_list
, list
) {
1662 scsi_remove_host(target
->scsi_host
);
1663 srp_disconnect_target(target
);
1664 ib_destroy_cm_id(target
->cm_id
);
1665 srp_free_target_ib(target
);
1666 scsi_host_put(target
->scsi_host
);
1669 ib_dereg_mr(host
->mr
);
1670 ib_dealloc_pd(host
->pd
);
1677 static int __init
srp_init_module(void)
1681 ret
= class_register(&srp_class
);
1683 printk(KERN_ERR PFX
"couldn't register class infiniband_srp\n");
1687 ret
= ib_register_client(&srp_client
);
1689 printk(KERN_ERR PFX
"couldn't register IB client\n");
1690 class_unregister(&srp_class
);
1697 static void __exit
srp_cleanup_module(void)
1699 ib_unregister_client(&srp_client
);
1700 class_unregister(&srp_class
);
1703 module_init(srp_init_module
);
1704 module_exit(srp_cleanup_module
);