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>
50 #include <scsi/scsi_transport_srp.h>
52 #include <rdma/ib_cache.h>
56 #define DRV_NAME "ib_srp"
57 #define PFX DRV_NAME ": "
58 #define DRV_VERSION "0.2"
59 #define DRV_RELDATE "November 1, 2005"
61 MODULE_AUTHOR("Roland Dreier");
62 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
63 "v" DRV_VERSION
" (" DRV_RELDATE
")");
64 MODULE_LICENSE("Dual BSD/GPL");
66 static int srp_sg_tablesize
= SRP_DEF_SG_TABLESIZE
;
67 static int srp_max_iu_len
;
69 module_param(srp_sg_tablesize
, int, 0444);
70 MODULE_PARM_DESC(srp_sg_tablesize
,
71 "Max number of gather/scatter entries per I/O (default is 12)");
73 static int topspin_workarounds
= 1;
75 module_param(topspin_workarounds
, int, 0444);
76 MODULE_PARM_DESC(topspin_workarounds
,
77 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
79 static const u8 topspin_oui
[3] = { 0x00, 0x05, 0xad };
81 static int mellanox_workarounds
= 1;
83 module_param(mellanox_workarounds
, int, 0444);
84 MODULE_PARM_DESC(mellanox_workarounds
,
85 "Enable workarounds for Mellanox SRP target bugs if != 0");
87 static const u8 mellanox_oui
[3] = { 0x00, 0x02, 0xc9 };
89 static void srp_add_one(struct ib_device
*device
);
90 static void srp_remove_one(struct ib_device
*device
);
91 static void srp_completion(struct ib_cq
*cq
, void *target_ptr
);
92 static int srp_cm_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*event
);
94 static struct scsi_transport_template
*ib_srp_transport_template
;
96 static struct ib_client srp_client
= {
99 .remove
= srp_remove_one
102 static struct ib_sa_client srp_sa_client
;
104 static inline struct srp_target_port
*host_to_target(struct Scsi_Host
*host
)
106 return (struct srp_target_port
*) host
->hostdata
;
109 static const char *srp_target_info(struct Scsi_Host
*host
)
111 return host_to_target(host
)->target_name
;
114 static struct srp_iu
*srp_alloc_iu(struct srp_host
*host
, size_t size
,
116 enum dma_data_direction direction
)
120 iu
= kmalloc(sizeof *iu
, gfp_mask
);
124 iu
->buf
= kzalloc(size
, gfp_mask
);
128 iu
->dma
= ib_dma_map_single(host
->dev
->dev
, iu
->buf
, size
, direction
);
129 if (ib_dma_mapping_error(host
->dev
->dev
, iu
->dma
))
133 iu
->direction
= direction
;
145 static void srp_free_iu(struct srp_host
*host
, struct srp_iu
*iu
)
150 ib_dma_unmap_single(host
->dev
->dev
, iu
->dma
, iu
->size
, iu
->direction
);
155 static void srp_qp_event(struct ib_event
*event
, void *context
)
157 printk(KERN_ERR PFX
"QP event %d\n", event
->event
);
160 static int srp_init_qp(struct srp_target_port
*target
,
163 struct ib_qp_attr
*attr
;
166 attr
= kmalloc(sizeof *attr
, GFP_KERNEL
);
170 ret
= ib_find_cached_pkey(target
->srp_host
->dev
->dev
,
171 target
->srp_host
->port
,
172 be16_to_cpu(target
->path
.pkey
),
177 attr
->qp_state
= IB_QPS_INIT
;
178 attr
->qp_access_flags
= (IB_ACCESS_REMOTE_READ
|
179 IB_ACCESS_REMOTE_WRITE
);
180 attr
->port_num
= target
->srp_host
->port
;
182 ret
= ib_modify_qp(qp
, attr
,
193 static int srp_create_target_ib(struct srp_target_port
*target
)
195 struct ib_qp_init_attr
*init_attr
;
198 init_attr
= kzalloc(sizeof *init_attr
, GFP_KERNEL
);
202 target
->cq
= ib_create_cq(target
->srp_host
->dev
->dev
, srp_completion
,
203 NULL
, target
, SRP_CQ_SIZE
, 0);
204 if (IS_ERR(target
->cq
)) {
205 ret
= PTR_ERR(target
->cq
);
209 ib_req_notify_cq(target
->cq
, IB_CQ_NEXT_COMP
);
211 init_attr
->event_handler
= srp_qp_event
;
212 init_attr
->cap
.max_send_wr
= SRP_SQ_SIZE
;
213 init_attr
->cap
.max_recv_wr
= SRP_RQ_SIZE
;
214 init_attr
->cap
.max_recv_sge
= 1;
215 init_attr
->cap
.max_send_sge
= 1;
216 init_attr
->sq_sig_type
= IB_SIGNAL_ALL_WR
;
217 init_attr
->qp_type
= IB_QPT_RC
;
218 init_attr
->send_cq
= target
->cq
;
219 init_attr
->recv_cq
= target
->cq
;
221 target
->qp
= ib_create_qp(target
->srp_host
->dev
->pd
, init_attr
);
222 if (IS_ERR(target
->qp
)) {
223 ret
= PTR_ERR(target
->qp
);
224 ib_destroy_cq(target
->cq
);
228 ret
= srp_init_qp(target
, target
->qp
);
230 ib_destroy_qp(target
->qp
);
231 ib_destroy_cq(target
->cq
);
240 static void srp_free_target_ib(struct srp_target_port
*target
)
244 ib_destroy_qp(target
->qp
);
245 ib_destroy_cq(target
->cq
);
247 for (i
= 0; i
< SRP_RQ_SIZE
; ++i
)
248 srp_free_iu(target
->srp_host
, target
->rx_ring
[i
]);
249 for (i
= 0; i
< SRP_SQ_SIZE
+ 1; ++i
)
250 srp_free_iu(target
->srp_host
, target
->tx_ring
[i
]);
253 static void srp_path_rec_completion(int status
,
254 struct ib_sa_path_rec
*pathrec
,
257 struct srp_target_port
*target
= target_ptr
;
259 target
->status
= status
;
261 printk(KERN_ERR PFX
"Got failed path rec status %d\n", status
);
263 target
->path
= *pathrec
;
264 complete(&target
->done
);
267 static int srp_lookup_path(struct srp_target_port
*target
)
269 target
->path
.numb_path
= 1;
271 init_completion(&target
->done
);
273 target
->path_query_id
= ib_sa_path_rec_get(&srp_sa_client
,
274 target
->srp_host
->dev
->dev
,
275 target
->srp_host
->port
,
277 IB_SA_PATH_REC_DGID
|
278 IB_SA_PATH_REC_SGID
|
279 IB_SA_PATH_REC_NUMB_PATH
|
281 SRP_PATH_REC_TIMEOUT_MS
,
283 srp_path_rec_completion
,
284 target
, &target
->path_query
);
285 if (target
->path_query_id
< 0)
286 return target
->path_query_id
;
288 wait_for_completion(&target
->done
);
290 if (target
->status
< 0)
291 printk(KERN_WARNING PFX
"Path record query failed\n");
293 return target
->status
;
296 static int srp_send_req(struct srp_target_port
*target
)
299 struct ib_cm_req_param param
;
300 struct srp_login_req priv
;
304 req
= kzalloc(sizeof *req
, GFP_KERNEL
);
308 req
->param
.primary_path
= &target
->path
;
309 req
->param
.alternate_path
= NULL
;
310 req
->param
.service_id
= target
->service_id
;
311 req
->param
.qp_num
= target
->qp
->qp_num
;
312 req
->param
.qp_type
= target
->qp
->qp_type
;
313 req
->param
.private_data
= &req
->priv
;
314 req
->param
.private_data_len
= sizeof req
->priv
;
315 req
->param
.flow_control
= 1;
317 get_random_bytes(&req
->param
.starting_psn
, 4);
318 req
->param
.starting_psn
&= 0xffffff;
321 * Pick some arbitrary defaults here; we could make these
322 * module parameters if anyone cared about setting them.
324 req
->param
.responder_resources
= 4;
325 req
->param
.remote_cm_response_timeout
= 20;
326 req
->param
.local_cm_response_timeout
= 20;
327 req
->param
.retry_count
= 7;
328 req
->param
.rnr_retry_count
= 7;
329 req
->param
.max_cm_retries
= 15;
331 req
->priv
.opcode
= SRP_LOGIN_REQ
;
333 req
->priv
.req_it_iu_len
= cpu_to_be32(srp_max_iu_len
);
334 req
->priv
.req_buf_fmt
= cpu_to_be16(SRP_BUF_FORMAT_DIRECT
|
335 SRP_BUF_FORMAT_INDIRECT
);
337 * In the published SRP specification (draft rev. 16a), the
338 * port identifier format is 8 bytes of ID extension followed
339 * by 8 bytes of GUID. Older drafts put the two halves in the
340 * opposite order, so that the GUID comes first.
342 * Targets conforming to these obsolete drafts can be
343 * recognized by the I/O Class they report.
345 if (target
->io_class
== SRP_REV10_IB_IO_CLASS
) {
346 memcpy(req
->priv
.initiator_port_id
,
347 &target
->path
.sgid
.global
.interface_id
, 8);
348 memcpy(req
->priv
.initiator_port_id
+ 8,
349 &target
->initiator_ext
, 8);
350 memcpy(req
->priv
.target_port_id
, &target
->ioc_guid
, 8);
351 memcpy(req
->priv
.target_port_id
+ 8, &target
->id_ext
, 8);
353 memcpy(req
->priv
.initiator_port_id
,
354 &target
->initiator_ext
, 8);
355 memcpy(req
->priv
.initiator_port_id
+ 8,
356 &target
->path
.sgid
.global
.interface_id
, 8);
357 memcpy(req
->priv
.target_port_id
, &target
->id_ext
, 8);
358 memcpy(req
->priv
.target_port_id
+ 8, &target
->ioc_guid
, 8);
362 * Topspin/Cisco SRP targets will reject our login unless we
363 * zero out the first 8 bytes of our initiator port ID and set
364 * the second 8 bytes to the local node GUID.
366 if (topspin_workarounds
&& !memcmp(&target
->ioc_guid
, topspin_oui
, 3)) {
367 printk(KERN_DEBUG PFX
"Topspin/Cisco initiator port ID workaround "
368 "activated for target GUID %016llx\n",
369 (unsigned long long) be64_to_cpu(target
->ioc_guid
));
370 memset(req
->priv
.initiator_port_id
, 0, 8);
371 memcpy(req
->priv
.initiator_port_id
+ 8,
372 &target
->srp_host
->dev
->dev
->node_guid
, 8);
375 status
= ib_send_cm_req(target
->cm_id
, &req
->param
);
382 static void srp_disconnect_target(struct srp_target_port
*target
)
384 /* XXX should send SRP_I_LOGOUT request */
386 init_completion(&target
->done
);
387 if (ib_send_cm_dreq(target
->cm_id
, NULL
, 0)) {
388 printk(KERN_DEBUG PFX
"Sending CM DREQ failed\n");
391 wait_for_completion(&target
->done
);
394 static void srp_remove_work(struct work_struct
*work
)
396 struct srp_target_port
*target
=
397 container_of(work
, struct srp_target_port
, work
);
399 spin_lock_irq(target
->scsi_host
->host_lock
);
400 if (target
->state
!= SRP_TARGET_DEAD
) {
401 spin_unlock_irq(target
->scsi_host
->host_lock
);
404 target
->state
= SRP_TARGET_REMOVED
;
405 spin_unlock_irq(target
->scsi_host
->host_lock
);
407 spin_lock(&target
->srp_host
->target_lock
);
408 list_del(&target
->list
);
409 spin_unlock(&target
->srp_host
->target_lock
);
411 srp_remove_host(target
->scsi_host
);
412 scsi_remove_host(target
->scsi_host
);
413 ib_destroy_cm_id(target
->cm_id
);
414 srp_free_target_ib(target
);
415 scsi_host_put(target
->scsi_host
);
418 static int srp_connect_target(struct srp_target_port
*target
)
422 ret
= srp_lookup_path(target
);
427 init_completion(&target
->done
);
428 ret
= srp_send_req(target
);
431 wait_for_completion(&target
->done
);
434 * The CM event handling code will set status to
435 * SRP_PORT_REDIRECT if we get a port redirect REJ
436 * back, or SRP_DLID_REDIRECT if we get a lid/qp
439 switch (target
->status
) {
443 case SRP_PORT_REDIRECT
:
444 ret
= srp_lookup_path(target
);
449 case SRP_DLID_REDIRECT
:
453 return target
->status
;
458 static void srp_unmap_data(struct scsi_cmnd
*scmnd
,
459 struct srp_target_port
*target
,
460 struct srp_request
*req
)
462 if (!scsi_sglist(scmnd
) ||
463 (scmnd
->sc_data_direction
!= DMA_TO_DEVICE
&&
464 scmnd
->sc_data_direction
!= DMA_FROM_DEVICE
))
468 ib_fmr_pool_unmap(req
->fmr
);
472 ib_dma_unmap_sg(target
->srp_host
->dev
->dev
, scsi_sglist(scmnd
),
473 scsi_sg_count(scmnd
), scmnd
->sc_data_direction
);
476 static void srp_remove_req(struct srp_target_port
*target
, struct srp_request
*req
)
478 srp_unmap_data(req
->scmnd
, target
, req
);
479 list_move_tail(&req
->list
, &target
->free_reqs
);
482 static void srp_reset_req(struct srp_target_port
*target
, struct srp_request
*req
)
484 req
->scmnd
->result
= DID_RESET
<< 16;
485 req
->scmnd
->scsi_done(req
->scmnd
);
486 srp_remove_req(target
, req
);
489 static int srp_reconnect_target(struct srp_target_port
*target
)
491 struct ib_cm_id
*new_cm_id
;
492 struct ib_qp_attr qp_attr
;
493 struct srp_request
*req
, *tmp
;
497 spin_lock_irq(target
->scsi_host
->host_lock
);
498 if (target
->state
!= SRP_TARGET_LIVE
) {
499 spin_unlock_irq(target
->scsi_host
->host_lock
);
502 target
->state
= SRP_TARGET_CONNECTING
;
503 spin_unlock_irq(target
->scsi_host
->host_lock
);
505 srp_disconnect_target(target
);
507 * Now get a new local CM ID so that we avoid confusing the
508 * target in case things are really fouled up.
510 new_cm_id
= ib_create_cm_id(target
->srp_host
->dev
->dev
,
511 srp_cm_handler
, target
);
512 if (IS_ERR(new_cm_id
)) {
513 ret
= PTR_ERR(new_cm_id
);
516 ib_destroy_cm_id(target
->cm_id
);
517 target
->cm_id
= new_cm_id
;
519 qp_attr
.qp_state
= IB_QPS_RESET
;
520 ret
= ib_modify_qp(target
->qp
, &qp_attr
, IB_QP_STATE
);
524 ret
= srp_init_qp(target
, target
->qp
);
528 while (ib_poll_cq(target
->cq
, 1, &wc
) > 0)
531 spin_lock_irq(target
->scsi_host
->host_lock
);
532 list_for_each_entry_safe(req
, tmp
, &target
->req_queue
, list
)
533 srp_reset_req(target
, req
);
534 spin_unlock_irq(target
->scsi_host
->host_lock
);
540 target
->qp_in_error
= 0;
541 ret
= srp_connect_target(target
);
545 spin_lock_irq(target
->scsi_host
->host_lock
);
546 if (target
->state
== SRP_TARGET_CONNECTING
) {
548 target
->state
= SRP_TARGET_LIVE
;
551 spin_unlock_irq(target
->scsi_host
->host_lock
);
556 printk(KERN_ERR PFX
"reconnect failed (%d), removing target port.\n", ret
);
559 * We couldn't reconnect, so kill our target port off.
560 * However, we have to defer the real removal because we might
561 * be in the context of the SCSI error handler now, which
562 * would deadlock if we call scsi_remove_host().
564 spin_lock_irq(target
->scsi_host
->host_lock
);
565 if (target
->state
== SRP_TARGET_CONNECTING
) {
566 target
->state
= SRP_TARGET_DEAD
;
567 INIT_WORK(&target
->work
, srp_remove_work
);
568 schedule_work(&target
->work
);
570 spin_unlock_irq(target
->scsi_host
->host_lock
);
575 static int srp_map_fmr(struct srp_target_port
*target
, struct scatterlist
*scat
,
576 int sg_cnt
, struct srp_request
*req
,
577 struct srp_direct_buf
*buf
)
585 struct srp_device
*dev
= target
->srp_host
->dev
;
586 struct ib_device
*ibdev
= dev
->dev
;
587 struct scatterlist
*sg
;
592 if ((ib_sg_dma_address(ibdev
, &scat
[0]) & ~dev
->fmr_page_mask
) &&
593 mellanox_workarounds
&& !memcmp(&target
->ioc_guid
, mellanox_oui
, 3))
597 scsi_for_each_sg(req
->scmnd
, sg
, sg_cnt
, i
) {
598 unsigned int dma_len
= ib_sg_dma_len(ibdev
, sg
);
600 if (ib_sg_dma_address(ibdev
, sg
) & ~dev
->fmr_page_mask
) {
606 if ((ib_sg_dma_address(ibdev
, sg
) + dma_len
) &
607 ~dev
->fmr_page_mask
) {
617 page_cnt
+= len
>> dev
->fmr_page_shift
;
618 if (page_cnt
> SRP_FMR_SIZE
)
621 dma_pages
= kmalloc(sizeof (u64
) * page_cnt
, GFP_ATOMIC
);
626 scsi_for_each_sg(req
->scmnd
, sg
, sg_cnt
, i
) {
627 unsigned int dma_len
= ib_sg_dma_len(ibdev
, sg
);
629 for (j
= 0; j
< dma_len
; j
+= dev
->fmr_page_size
)
630 dma_pages
[page_cnt
++] =
631 (ib_sg_dma_address(ibdev
, sg
) &
632 dev
->fmr_page_mask
) + j
;
635 req
->fmr
= ib_fmr_pool_map_phys(dev
->fmr_pool
,
636 dma_pages
, page_cnt
, io_addr
);
637 if (IS_ERR(req
->fmr
)) {
638 ret
= PTR_ERR(req
->fmr
);
643 buf
->va
= cpu_to_be64(ib_sg_dma_address(ibdev
, &scat
[0]) &
644 ~dev
->fmr_page_mask
);
645 buf
->key
= cpu_to_be32(req
->fmr
->fmr
->rkey
);
646 buf
->len
= cpu_to_be32(len
);
656 static int srp_map_data(struct scsi_cmnd
*scmnd
, struct srp_target_port
*target
,
657 struct srp_request
*req
)
659 struct scatterlist
*scat
;
660 struct srp_cmd
*cmd
= req
->cmd
->buf
;
661 int len
, nents
, count
;
662 u8 fmt
= SRP_DATA_DESC_DIRECT
;
663 struct srp_device
*dev
;
664 struct ib_device
*ibdev
;
666 if (!scsi_sglist(scmnd
) || scmnd
->sc_data_direction
== DMA_NONE
)
667 return sizeof (struct srp_cmd
);
669 if (scmnd
->sc_data_direction
!= DMA_FROM_DEVICE
&&
670 scmnd
->sc_data_direction
!= DMA_TO_DEVICE
) {
671 printk(KERN_WARNING PFX
"Unhandled data direction %d\n",
672 scmnd
->sc_data_direction
);
676 nents
= scsi_sg_count(scmnd
);
677 scat
= scsi_sglist(scmnd
);
679 dev
= target
->srp_host
->dev
;
682 count
= ib_dma_map_sg(ibdev
, scat
, nents
, scmnd
->sc_data_direction
);
684 fmt
= SRP_DATA_DESC_DIRECT
;
685 len
= sizeof (struct srp_cmd
) + sizeof (struct srp_direct_buf
);
689 * The midlayer only generated a single gather/scatter
690 * entry, or DMA mapping coalesced everything to a
691 * single entry. So a direct descriptor along with
692 * the DMA MR suffices.
694 struct srp_direct_buf
*buf
= (void *) cmd
->add_data
;
696 buf
->va
= cpu_to_be64(ib_sg_dma_address(ibdev
, scat
));
697 buf
->key
= cpu_to_be32(dev
->mr
->rkey
);
698 buf
->len
= cpu_to_be32(ib_sg_dma_len(ibdev
, scat
));
699 } else if (srp_map_fmr(target
, scat
, count
, req
,
700 (void *) cmd
->add_data
)) {
702 * FMR mapping failed, and the scatterlist has more
703 * than one entry. Generate an indirect memory
706 struct srp_indirect_buf
*buf
= (void *) cmd
->add_data
;
707 struct scatterlist
*sg
;
711 fmt
= SRP_DATA_DESC_INDIRECT
;
712 len
= sizeof (struct srp_cmd
) +
713 sizeof (struct srp_indirect_buf
) +
714 count
* sizeof (struct srp_direct_buf
);
716 scsi_for_each_sg(scmnd
, sg
, count
, i
) {
717 unsigned int dma_len
= ib_sg_dma_len(ibdev
, sg
);
719 buf
->desc_list
[i
].va
=
720 cpu_to_be64(ib_sg_dma_address(ibdev
, sg
));
721 buf
->desc_list
[i
].key
=
722 cpu_to_be32(dev
->mr
->rkey
);
723 buf
->desc_list
[i
].len
= cpu_to_be32(dma_len
);
727 if (scmnd
->sc_data_direction
== DMA_TO_DEVICE
)
728 cmd
->data_out_desc_cnt
= count
;
730 cmd
->data_in_desc_cnt
= count
;
733 cpu_to_be64(req
->cmd
->dma
+ sizeof *cmd
+ sizeof *buf
);
734 buf
->table_desc
.key
=
735 cpu_to_be32(target
->srp_host
->dev
->mr
->rkey
);
736 buf
->table_desc
.len
=
737 cpu_to_be32(count
* sizeof (struct srp_direct_buf
));
739 buf
->len
= cpu_to_be32(datalen
);
742 if (scmnd
->sc_data_direction
== DMA_TO_DEVICE
)
743 cmd
->buf_fmt
= fmt
<< 4;
750 static void srp_process_rsp(struct srp_target_port
*target
, struct srp_rsp
*rsp
)
752 struct srp_request
*req
;
753 struct scsi_cmnd
*scmnd
;
757 delta
= (s32
) be32_to_cpu(rsp
->req_lim_delta
);
759 spin_lock_irqsave(target
->scsi_host
->host_lock
, flags
);
761 target
->req_lim
+= delta
;
763 req
= &target
->req_ring
[rsp
->tag
& ~SRP_TAG_TSK_MGMT
];
765 if (unlikely(rsp
->tag
& SRP_TAG_TSK_MGMT
)) {
766 if (be32_to_cpu(rsp
->resp_data_len
) < 4)
767 req
->tsk_status
= -1;
769 req
->tsk_status
= rsp
->data
[3];
770 complete(&req
->done
);
774 printk(KERN_ERR
"Null scmnd for RSP w/tag %016llx\n",
775 (unsigned long long) rsp
->tag
);
776 scmnd
->result
= rsp
->status
;
778 if (rsp
->flags
& SRP_RSP_FLAG_SNSVALID
) {
779 memcpy(scmnd
->sense_buffer
, rsp
->data
+
780 be32_to_cpu(rsp
->resp_data_len
),
781 min_t(int, be32_to_cpu(rsp
->sense_data_len
),
782 SCSI_SENSE_BUFFERSIZE
));
785 if (rsp
->flags
& (SRP_RSP_FLAG_DOOVER
| SRP_RSP_FLAG_DOUNDER
))
786 scsi_set_resid(scmnd
, be32_to_cpu(rsp
->data_out_res_cnt
));
787 else if (rsp
->flags
& (SRP_RSP_FLAG_DIOVER
| SRP_RSP_FLAG_DIUNDER
))
788 scsi_set_resid(scmnd
, be32_to_cpu(rsp
->data_in_res_cnt
));
790 if (!req
->tsk_mgmt
) {
791 scmnd
->host_scribble
= (void *) -1L;
792 scmnd
->scsi_done(scmnd
);
794 srp_remove_req(target
, req
);
799 spin_unlock_irqrestore(target
->scsi_host
->host_lock
, flags
);
802 static void srp_handle_recv(struct srp_target_port
*target
, struct ib_wc
*wc
)
804 struct ib_device
*dev
;
808 iu
= target
->rx_ring
[wc
->wr_id
& ~SRP_OP_RECV
];
810 dev
= target
->srp_host
->dev
->dev
;
811 ib_dma_sync_single_for_cpu(dev
, iu
->dma
, target
->max_ti_iu_len
,
814 opcode
= *(u8
*) iu
->buf
;
819 printk(KERN_ERR PFX
"recv completion, opcode 0x%02x\n", opcode
);
821 for (i
= 0; i
< wc
->byte_len
; ++i
) {
823 printk(KERN_ERR
" [%02x] ", i
);
824 printk(" %02x", ((u8
*) iu
->buf
)[i
]);
825 if ((i
+ 1) % 8 == 0)
829 if (wc
->byte_len
% 8)
835 srp_process_rsp(target
, iu
->buf
);
839 /* XXX Handle target logout */
840 printk(KERN_WARNING PFX
"Got target logout request\n");
844 printk(KERN_WARNING PFX
"Unhandled SRP opcode 0x%02x\n", opcode
);
848 ib_dma_sync_single_for_device(dev
, iu
->dma
, target
->max_ti_iu_len
,
852 static void srp_completion(struct ib_cq
*cq
, void *target_ptr
)
854 struct srp_target_port
*target
= target_ptr
;
857 ib_req_notify_cq(cq
, IB_CQ_NEXT_COMP
);
858 while (ib_poll_cq(cq
, 1, &wc
) > 0) {
860 printk(KERN_ERR PFX
"failed %s status %d\n",
861 wc
.wr_id
& SRP_OP_RECV
? "receive" : "send",
863 target
->qp_in_error
= 1;
867 if (wc
.wr_id
& SRP_OP_RECV
)
868 srp_handle_recv(target
, &wc
);
874 static int __srp_post_recv(struct srp_target_port
*target
)
878 struct ib_recv_wr wr
, *bad_wr
;
882 next
= target
->rx_head
& (SRP_RQ_SIZE
- 1);
883 wr
.wr_id
= next
| SRP_OP_RECV
;
884 iu
= target
->rx_ring
[next
];
887 list
.length
= iu
->size
;
888 list
.lkey
= target
->srp_host
->dev
->mr
->lkey
;
894 ret
= ib_post_recv(target
->qp
, &wr
, &bad_wr
);
901 static int srp_post_recv(struct srp_target_port
*target
)
906 spin_lock_irqsave(target
->scsi_host
->host_lock
, flags
);
907 ret
= __srp_post_recv(target
);
908 spin_unlock_irqrestore(target
->scsi_host
->host_lock
, flags
);
914 * Must be called with target->scsi_host->host_lock held to protect
915 * req_lim and tx_head. Lock cannot be dropped between call here and
916 * call to __srp_post_send().
918 static struct srp_iu
*__srp_get_tx_iu(struct srp_target_port
*target
)
920 if (target
->tx_head
- target
->tx_tail
>= SRP_SQ_SIZE
)
923 if (unlikely(target
->req_lim
< 1))
924 ++target
->zero_req_lim
;
926 return target
->tx_ring
[target
->tx_head
& SRP_SQ_SIZE
];
930 * Must be called with target->scsi_host->host_lock held to protect
931 * req_lim and tx_head.
933 static int __srp_post_send(struct srp_target_port
*target
,
934 struct srp_iu
*iu
, int len
)
937 struct ib_send_wr wr
, *bad_wr
;
942 list
.lkey
= target
->srp_host
->dev
->mr
->lkey
;
945 wr
.wr_id
= target
->tx_head
& SRP_SQ_SIZE
;
948 wr
.opcode
= IB_WR_SEND
;
949 wr
.send_flags
= IB_SEND_SIGNALED
;
951 ret
= ib_post_send(target
->qp
, &wr
, &bad_wr
);
961 static int srp_queuecommand(struct scsi_cmnd
*scmnd
,
962 void (*done
)(struct scsi_cmnd
*))
964 struct srp_target_port
*target
= host_to_target(scmnd
->device
->host
);
965 struct srp_request
*req
;
968 struct ib_device
*dev
;
971 if (target
->state
== SRP_TARGET_CONNECTING
)
974 if (target
->state
== SRP_TARGET_DEAD
||
975 target
->state
== SRP_TARGET_REMOVED
) {
976 scmnd
->result
= DID_BAD_TARGET
<< 16;
981 iu
= __srp_get_tx_iu(target
);
985 dev
= target
->srp_host
->dev
->dev
;
986 ib_dma_sync_single_for_cpu(dev
, iu
->dma
, srp_max_iu_len
,
989 req
= list_entry(target
->free_reqs
.next
, struct srp_request
, list
);
991 scmnd
->scsi_done
= done
;
993 scmnd
->host_scribble
= (void *) (long) req
->index
;
996 memset(cmd
, 0, sizeof *cmd
);
998 cmd
->opcode
= SRP_CMD
;
999 cmd
->lun
= cpu_to_be64((u64
) scmnd
->device
->lun
<< 48);
1000 cmd
->tag
= req
->index
;
1001 memcpy(cmd
->cdb
, scmnd
->cmnd
, scmnd
->cmd_len
);
1006 req
->tsk_mgmt
= NULL
;
1008 len
= srp_map_data(scmnd
, target
, req
);
1010 printk(KERN_ERR PFX
"Failed to map data\n");
1014 if (__srp_post_recv(target
)) {
1015 printk(KERN_ERR PFX
"Recv failed\n");
1019 ib_dma_sync_single_for_device(dev
, iu
->dma
, srp_max_iu_len
,
1022 if (__srp_post_send(target
, iu
, len
)) {
1023 printk(KERN_ERR PFX
"Send failed\n");
1027 list_move_tail(&req
->list
, &target
->req_queue
);
1032 srp_unmap_data(scmnd
, target
, req
);
1035 return SCSI_MLQUEUE_HOST_BUSY
;
1038 static int srp_alloc_iu_bufs(struct srp_target_port
*target
)
1042 for (i
= 0; i
< SRP_RQ_SIZE
; ++i
) {
1043 target
->rx_ring
[i
] = srp_alloc_iu(target
->srp_host
,
1044 target
->max_ti_iu_len
,
1045 GFP_KERNEL
, DMA_FROM_DEVICE
);
1046 if (!target
->rx_ring
[i
])
1050 for (i
= 0; i
< SRP_SQ_SIZE
+ 1; ++i
) {
1051 target
->tx_ring
[i
] = srp_alloc_iu(target
->srp_host
,
1053 GFP_KERNEL
, DMA_TO_DEVICE
);
1054 if (!target
->tx_ring
[i
])
1061 for (i
= 0; i
< SRP_RQ_SIZE
; ++i
) {
1062 srp_free_iu(target
->srp_host
, target
->rx_ring
[i
]);
1063 target
->rx_ring
[i
] = NULL
;
1066 for (i
= 0; i
< SRP_SQ_SIZE
+ 1; ++i
) {
1067 srp_free_iu(target
->srp_host
, target
->tx_ring
[i
]);
1068 target
->tx_ring
[i
] = NULL
;
1074 static void srp_cm_rej_handler(struct ib_cm_id
*cm_id
,
1075 struct ib_cm_event
*event
,
1076 struct srp_target_port
*target
)
1078 struct ib_class_port_info
*cpi
;
1081 switch (event
->param
.rej_rcvd
.reason
) {
1082 case IB_CM_REJ_PORT_CM_REDIRECT
:
1083 cpi
= event
->param
.rej_rcvd
.ari
;
1084 target
->path
.dlid
= cpi
->redirect_lid
;
1085 target
->path
.pkey
= cpi
->redirect_pkey
;
1086 cm_id
->remote_cm_qpn
= be32_to_cpu(cpi
->redirect_qp
) & 0x00ffffff;
1087 memcpy(target
->path
.dgid
.raw
, cpi
->redirect_gid
, 16);
1089 target
->status
= target
->path
.dlid
?
1090 SRP_DLID_REDIRECT
: SRP_PORT_REDIRECT
;
1093 case IB_CM_REJ_PORT_REDIRECT
:
1094 if (topspin_workarounds
&&
1095 !memcmp(&target
->ioc_guid
, topspin_oui
, 3)) {
1097 * Topspin/Cisco SRP gateways incorrectly send
1098 * reject reason code 25 when they mean 24
1101 memcpy(target
->path
.dgid
.raw
,
1102 event
->param
.rej_rcvd
.ari
, 16);
1104 printk(KERN_DEBUG PFX
"Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1105 (unsigned long long) be64_to_cpu(target
->path
.dgid
.global
.subnet_prefix
),
1106 (unsigned long long) be64_to_cpu(target
->path
.dgid
.global
.interface_id
));
1108 target
->status
= SRP_PORT_REDIRECT
;
1110 printk(KERN_WARNING
" REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1111 target
->status
= -ECONNRESET
;
1115 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID
:
1116 printk(KERN_WARNING
" REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1117 target
->status
= -ECONNRESET
;
1120 case IB_CM_REJ_CONSUMER_DEFINED
:
1121 opcode
= *(u8
*) event
->private_data
;
1122 if (opcode
== SRP_LOGIN_REJ
) {
1123 struct srp_login_rej
*rej
= event
->private_data
;
1124 u32 reason
= be32_to_cpu(rej
->reason
);
1126 if (reason
== SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE
)
1127 printk(KERN_WARNING PFX
1128 "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1130 printk(KERN_WARNING PFX
1131 "SRP LOGIN REJECTED, reason 0x%08x\n", reason
);
1133 printk(KERN_WARNING
" REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1134 " opcode 0x%02x\n", opcode
);
1135 target
->status
= -ECONNRESET
;
1139 printk(KERN_WARNING
" REJ reason 0x%x\n",
1140 event
->param
.rej_rcvd
.reason
);
1141 target
->status
= -ECONNRESET
;
1145 static int srp_cm_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*event
)
1147 struct srp_target_port
*target
= cm_id
->context
;
1148 struct ib_qp_attr
*qp_attr
= NULL
;
1153 switch (event
->event
) {
1154 case IB_CM_REQ_ERROR
:
1155 printk(KERN_DEBUG PFX
"Sending CM REQ failed\n");
1157 target
->status
= -ECONNRESET
;
1160 case IB_CM_REP_RECEIVED
:
1162 opcode
= *(u8
*) event
->private_data
;
1164 if (opcode
== SRP_LOGIN_RSP
) {
1165 struct srp_login_rsp
*rsp
= event
->private_data
;
1167 target
->max_ti_iu_len
= be32_to_cpu(rsp
->max_ti_iu_len
);
1168 target
->req_lim
= be32_to_cpu(rsp
->req_lim_delta
);
1170 target
->scsi_host
->can_queue
= min(target
->req_lim
,
1171 target
->scsi_host
->can_queue
);
1173 printk(KERN_WARNING PFX
"Unhandled RSP opcode %#x\n", opcode
);
1174 target
->status
= -ECONNRESET
;
1178 if (!target
->rx_ring
[0]) {
1179 target
->status
= srp_alloc_iu_bufs(target
);
1184 qp_attr
= kmalloc(sizeof *qp_attr
, GFP_KERNEL
);
1186 target
->status
= -ENOMEM
;
1190 qp_attr
->qp_state
= IB_QPS_RTR
;
1191 target
->status
= ib_cm_init_qp_attr(cm_id
, qp_attr
, &attr_mask
);
1195 target
->status
= ib_modify_qp(target
->qp
, qp_attr
, attr_mask
);
1199 target
->status
= srp_post_recv(target
);
1203 qp_attr
->qp_state
= IB_QPS_RTS
;
1204 target
->status
= ib_cm_init_qp_attr(cm_id
, qp_attr
, &attr_mask
);
1208 target
->status
= ib_modify_qp(target
->qp
, qp_attr
, attr_mask
);
1212 target
->status
= ib_send_cm_rtu(cm_id
, NULL
, 0);
1218 case IB_CM_REJ_RECEIVED
:
1219 printk(KERN_DEBUG PFX
"REJ received\n");
1222 srp_cm_rej_handler(cm_id
, event
, target
);
1225 case IB_CM_DREQ_RECEIVED
:
1226 printk(KERN_WARNING PFX
"DREQ received - connection closed\n");
1227 if (ib_send_cm_drep(cm_id
, NULL
, 0))
1228 printk(KERN_ERR PFX
"Sending CM DREP failed\n");
1231 case IB_CM_TIMEWAIT_EXIT
:
1232 printk(KERN_ERR PFX
"connection closed\n");
1238 case IB_CM_MRA_RECEIVED
:
1239 case IB_CM_DREQ_ERROR
:
1240 case IB_CM_DREP_RECEIVED
:
1244 printk(KERN_WARNING PFX
"Unhandled CM event %d\n", event
->event
);
1249 complete(&target
->done
);
1256 static int srp_send_tsk_mgmt(struct srp_target_port
*target
,
1257 struct srp_request
*req
, u8 func
)
1260 struct srp_tsk_mgmt
*tsk_mgmt
;
1262 spin_lock_irq(target
->scsi_host
->host_lock
);
1264 if (target
->state
== SRP_TARGET_DEAD
||
1265 target
->state
== SRP_TARGET_REMOVED
) {
1266 req
->scmnd
->result
= DID_BAD_TARGET
<< 16;
1270 init_completion(&req
->done
);
1272 iu
= __srp_get_tx_iu(target
);
1277 memset(tsk_mgmt
, 0, sizeof *tsk_mgmt
);
1279 tsk_mgmt
->opcode
= SRP_TSK_MGMT
;
1280 tsk_mgmt
->lun
= cpu_to_be64((u64
) req
->scmnd
->device
->lun
<< 48);
1281 tsk_mgmt
->tag
= req
->index
| SRP_TAG_TSK_MGMT
;
1282 tsk_mgmt
->tsk_mgmt_func
= func
;
1283 tsk_mgmt
->task_tag
= req
->index
;
1285 if (__srp_post_send(target
, iu
, sizeof *tsk_mgmt
))
1290 spin_unlock_irq(target
->scsi_host
->host_lock
);
1292 if (!wait_for_completion_timeout(&req
->done
,
1293 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS
)))
1299 spin_unlock_irq(target
->scsi_host
->host_lock
);
1303 static int srp_find_req(struct srp_target_port
*target
,
1304 struct scsi_cmnd
*scmnd
,
1305 struct srp_request
**req
)
1307 if (scmnd
->host_scribble
== (void *) -1L)
1310 *req
= &target
->req_ring
[(long) scmnd
->host_scribble
];
1315 static int srp_abort(struct scsi_cmnd
*scmnd
)
1317 struct srp_target_port
*target
= host_to_target(scmnd
->device
->host
);
1318 struct srp_request
*req
;
1321 printk(KERN_ERR
"SRP abort called\n");
1323 if (target
->qp_in_error
)
1325 if (srp_find_req(target
, scmnd
, &req
))
1327 if (srp_send_tsk_mgmt(target
, req
, SRP_TSK_ABORT_TASK
))
1330 spin_lock_irq(target
->scsi_host
->host_lock
);
1332 if (req
->cmd_done
) {
1333 srp_remove_req(target
, req
);
1334 scmnd
->scsi_done(scmnd
);
1335 } else if (!req
->tsk_status
) {
1336 srp_remove_req(target
, req
);
1337 scmnd
->result
= DID_ABORT
<< 16;
1341 spin_unlock_irq(target
->scsi_host
->host_lock
);
1346 static int srp_reset_device(struct scsi_cmnd
*scmnd
)
1348 struct srp_target_port
*target
= host_to_target(scmnd
->device
->host
);
1349 struct srp_request
*req
, *tmp
;
1351 printk(KERN_ERR
"SRP reset_device called\n");
1353 if (target
->qp_in_error
)
1355 if (srp_find_req(target
, scmnd
, &req
))
1357 if (srp_send_tsk_mgmt(target
, req
, SRP_TSK_LUN_RESET
))
1359 if (req
->tsk_status
)
1362 spin_lock_irq(target
->scsi_host
->host_lock
);
1364 list_for_each_entry_safe(req
, tmp
, &target
->req_queue
, list
)
1365 if (req
->scmnd
->device
== scmnd
->device
)
1366 srp_reset_req(target
, req
);
1368 spin_unlock_irq(target
->scsi_host
->host_lock
);
1373 static int srp_reset_host(struct scsi_cmnd
*scmnd
)
1375 struct srp_target_port
*target
= host_to_target(scmnd
->device
->host
);
1378 printk(KERN_ERR PFX
"SRP reset_host called\n");
1380 if (!srp_reconnect_target(target
))
1386 static ssize_t
show_id_ext(struct class_device
*cdev
, char *buf
)
1388 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1390 if (target
->state
== SRP_TARGET_DEAD
||
1391 target
->state
== SRP_TARGET_REMOVED
)
1394 return sprintf(buf
, "0x%016llx\n",
1395 (unsigned long long) be64_to_cpu(target
->id_ext
));
1398 static ssize_t
show_ioc_guid(struct class_device
*cdev
, char *buf
)
1400 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1402 if (target
->state
== SRP_TARGET_DEAD
||
1403 target
->state
== SRP_TARGET_REMOVED
)
1406 return sprintf(buf
, "0x%016llx\n",
1407 (unsigned long long) be64_to_cpu(target
->ioc_guid
));
1410 static ssize_t
show_service_id(struct class_device
*cdev
, char *buf
)
1412 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1414 if (target
->state
== SRP_TARGET_DEAD
||
1415 target
->state
== SRP_TARGET_REMOVED
)
1418 return sprintf(buf
, "0x%016llx\n",
1419 (unsigned long long) be64_to_cpu(target
->service_id
));
1422 static ssize_t
show_pkey(struct class_device
*cdev
, char *buf
)
1424 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1426 if (target
->state
== SRP_TARGET_DEAD
||
1427 target
->state
== SRP_TARGET_REMOVED
)
1430 return sprintf(buf
, "0x%04x\n", be16_to_cpu(target
->path
.pkey
));
1433 static ssize_t
show_dgid(struct class_device
*cdev
, char *buf
)
1435 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1437 if (target
->state
== SRP_TARGET_DEAD
||
1438 target
->state
== SRP_TARGET_REMOVED
)
1441 return sprintf(buf
, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1442 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[0]),
1443 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[1]),
1444 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[2]),
1445 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[3]),
1446 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[4]),
1447 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[5]),
1448 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[6]),
1449 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[7]));
1452 static ssize_t
show_orig_dgid(struct class_device
*cdev
, char *buf
)
1454 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1456 if (target
->state
== SRP_TARGET_DEAD
||
1457 target
->state
== SRP_TARGET_REMOVED
)
1460 return sprintf(buf
, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1461 be16_to_cpu(target
->orig_dgid
[0]),
1462 be16_to_cpu(target
->orig_dgid
[1]),
1463 be16_to_cpu(target
->orig_dgid
[2]),
1464 be16_to_cpu(target
->orig_dgid
[3]),
1465 be16_to_cpu(target
->orig_dgid
[4]),
1466 be16_to_cpu(target
->orig_dgid
[5]),
1467 be16_to_cpu(target
->orig_dgid
[6]),
1468 be16_to_cpu(target
->orig_dgid
[7]));
1471 static ssize_t
show_zero_req_lim(struct class_device
*cdev
, char *buf
)
1473 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1475 if (target
->state
== SRP_TARGET_DEAD
||
1476 target
->state
== SRP_TARGET_REMOVED
)
1479 return sprintf(buf
, "%d\n", target
->zero_req_lim
);
1482 static ssize_t
show_local_ib_port(struct class_device
*cdev
, char *buf
)
1484 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1486 return sprintf(buf
, "%d\n", target
->srp_host
->port
);
1489 static ssize_t
show_local_ib_device(struct class_device
*cdev
, char *buf
)
1491 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1493 return sprintf(buf
, "%s\n", target
->srp_host
->dev
->dev
->name
);
1496 static CLASS_DEVICE_ATTR(id_ext
, S_IRUGO
, show_id_ext
, NULL
);
1497 static CLASS_DEVICE_ATTR(ioc_guid
, S_IRUGO
, show_ioc_guid
, NULL
);
1498 static CLASS_DEVICE_ATTR(service_id
, S_IRUGO
, show_service_id
, NULL
);
1499 static CLASS_DEVICE_ATTR(pkey
, S_IRUGO
, show_pkey
, NULL
);
1500 static CLASS_DEVICE_ATTR(dgid
, S_IRUGO
, show_dgid
, NULL
);
1501 static CLASS_DEVICE_ATTR(orig_dgid
, S_IRUGO
, show_orig_dgid
, NULL
);
1502 static CLASS_DEVICE_ATTR(zero_req_lim
, S_IRUGO
, show_zero_req_lim
, NULL
);
1503 static CLASS_DEVICE_ATTR(local_ib_port
, S_IRUGO
, show_local_ib_port
, NULL
);
1504 static CLASS_DEVICE_ATTR(local_ib_device
, S_IRUGO
, show_local_ib_device
, NULL
);
1506 static struct class_device_attribute
*srp_host_attrs
[] = {
1507 &class_device_attr_id_ext
,
1508 &class_device_attr_ioc_guid
,
1509 &class_device_attr_service_id
,
1510 &class_device_attr_pkey
,
1511 &class_device_attr_dgid
,
1512 &class_device_attr_orig_dgid
,
1513 &class_device_attr_zero_req_lim
,
1514 &class_device_attr_local_ib_port
,
1515 &class_device_attr_local_ib_device
,
1519 static struct scsi_host_template srp_template
= {
1520 .module
= THIS_MODULE
,
1521 .name
= "InfiniBand SRP initiator",
1522 .proc_name
= DRV_NAME
,
1523 .info
= srp_target_info
,
1524 .queuecommand
= srp_queuecommand
,
1525 .eh_abort_handler
= srp_abort
,
1526 .eh_device_reset_handler
= srp_reset_device
,
1527 .eh_host_reset_handler
= srp_reset_host
,
1528 .can_queue
= SRP_SQ_SIZE
,
1530 .cmd_per_lun
= SRP_SQ_SIZE
,
1531 .use_clustering
= ENABLE_CLUSTERING
,
1532 .shost_attrs
= srp_host_attrs
1535 static int srp_add_target(struct srp_host
*host
, struct srp_target_port
*target
)
1537 struct srp_rport_identifiers ids
;
1538 struct srp_rport
*rport
;
1540 sprintf(target
->target_name
, "SRP.T10:%016llX",
1541 (unsigned long long) be64_to_cpu(target
->id_ext
));
1543 if (scsi_add_host(target
->scsi_host
, host
->dev
->dev
->dma_device
))
1546 memcpy(ids
.port_id
, &target
->id_ext
, 8);
1547 memcpy(ids
.port_id
+ 8, &target
->ioc_guid
, 8);
1548 ids
.roles
= SRP_RPORT_ROLE_TARGET
;
1549 rport
= srp_rport_add(target
->scsi_host
, &ids
);
1550 if (IS_ERR(rport
)) {
1551 scsi_remove_host(target
->scsi_host
);
1552 return PTR_ERR(rport
);
1555 spin_lock(&host
->target_lock
);
1556 list_add_tail(&target
->list
, &host
->target_list
);
1557 spin_unlock(&host
->target_lock
);
1559 target
->state
= SRP_TARGET_LIVE
;
1561 scsi_scan_target(&target
->scsi_host
->shost_gendev
,
1562 0, target
->scsi_id
, SCAN_WILD_CARD
, 0);
1567 static void srp_release_class_dev(struct class_device
*class_dev
)
1569 struct srp_host
*host
=
1570 container_of(class_dev
, struct srp_host
, class_dev
);
1572 complete(&host
->released
);
1575 static struct class srp_class
= {
1576 .name
= "infiniband_srp",
1577 .release
= srp_release_class_dev
1581 * Target ports are added by writing
1583 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1584 * pkey=<P_Key>,service_id=<service ID>
1586 * to the add_target sysfs attribute.
1590 SRP_OPT_ID_EXT
= 1 << 0,
1591 SRP_OPT_IOC_GUID
= 1 << 1,
1592 SRP_OPT_DGID
= 1 << 2,
1593 SRP_OPT_PKEY
= 1 << 3,
1594 SRP_OPT_SERVICE_ID
= 1 << 4,
1595 SRP_OPT_MAX_SECT
= 1 << 5,
1596 SRP_OPT_MAX_CMD_PER_LUN
= 1 << 6,
1597 SRP_OPT_IO_CLASS
= 1 << 7,
1598 SRP_OPT_INITIATOR_EXT
= 1 << 8,
1599 SRP_OPT_ALL
= (SRP_OPT_ID_EXT
|
1603 SRP_OPT_SERVICE_ID
),
1606 static match_table_t srp_opt_tokens
= {
1607 { SRP_OPT_ID_EXT
, "id_ext=%s" },
1608 { SRP_OPT_IOC_GUID
, "ioc_guid=%s" },
1609 { SRP_OPT_DGID
, "dgid=%s" },
1610 { SRP_OPT_PKEY
, "pkey=%x" },
1611 { SRP_OPT_SERVICE_ID
, "service_id=%s" },
1612 { SRP_OPT_MAX_SECT
, "max_sect=%d" },
1613 { SRP_OPT_MAX_CMD_PER_LUN
, "max_cmd_per_lun=%d" },
1614 { SRP_OPT_IO_CLASS
, "io_class=%x" },
1615 { SRP_OPT_INITIATOR_EXT
, "initiator_ext=%s" },
1616 { SRP_OPT_ERR
, NULL
}
1619 static int srp_parse_options(const char *buf
, struct srp_target_port
*target
)
1621 char *options
, *sep_opt
;
1624 substring_t args
[MAX_OPT_ARGS
];
1630 options
= kstrdup(buf
, GFP_KERNEL
);
1635 while ((p
= strsep(&sep_opt
, ",")) != NULL
) {
1639 token
= match_token(p
, srp_opt_tokens
, args
);
1643 case SRP_OPT_ID_EXT
:
1644 p
= match_strdup(args
);
1649 target
->id_ext
= cpu_to_be64(simple_strtoull(p
, NULL
, 16));
1653 case SRP_OPT_IOC_GUID
:
1654 p
= match_strdup(args
);
1659 target
->ioc_guid
= cpu_to_be64(simple_strtoull(p
, NULL
, 16));
1664 p
= match_strdup(args
);
1669 if (strlen(p
) != 32) {
1670 printk(KERN_WARNING PFX
"bad dest GID parameter '%s'\n", p
);
1675 for (i
= 0; i
< 16; ++i
) {
1676 strlcpy(dgid
, p
+ i
* 2, 3);
1677 target
->path
.dgid
.raw
[i
] = simple_strtoul(dgid
, NULL
, 16);
1680 memcpy(target
->orig_dgid
, target
->path
.dgid
.raw
, 16);
1684 if (match_hex(args
, &token
)) {
1685 printk(KERN_WARNING PFX
"bad P_Key parameter '%s'\n", p
);
1688 target
->path
.pkey
= cpu_to_be16(token
);
1691 case SRP_OPT_SERVICE_ID
:
1692 p
= match_strdup(args
);
1697 target
->service_id
= cpu_to_be64(simple_strtoull(p
, NULL
, 16));
1701 case SRP_OPT_MAX_SECT
:
1702 if (match_int(args
, &token
)) {
1703 printk(KERN_WARNING PFX
"bad max sect parameter '%s'\n", p
);
1706 target
->scsi_host
->max_sectors
= token
;
1709 case SRP_OPT_MAX_CMD_PER_LUN
:
1710 if (match_int(args
, &token
)) {
1711 printk(KERN_WARNING PFX
"bad max cmd_per_lun parameter '%s'\n", p
);
1714 target
->scsi_host
->cmd_per_lun
= min(token
, SRP_SQ_SIZE
);
1717 case SRP_OPT_IO_CLASS
:
1718 if (match_hex(args
, &token
)) {
1719 printk(KERN_WARNING PFX
"bad IO class parameter '%s' \n", p
);
1722 if (token
!= SRP_REV10_IB_IO_CLASS
&&
1723 token
!= SRP_REV16A_IB_IO_CLASS
) {
1724 printk(KERN_WARNING PFX
"unknown IO class parameter value"
1725 " %x specified (use %x or %x).\n",
1726 token
, SRP_REV10_IB_IO_CLASS
, SRP_REV16A_IB_IO_CLASS
);
1729 target
->io_class
= token
;
1732 case SRP_OPT_INITIATOR_EXT
:
1733 p
= match_strdup(args
);
1738 target
->initiator_ext
= cpu_to_be64(simple_strtoull(p
, NULL
, 16));
1743 printk(KERN_WARNING PFX
"unknown parameter or missing value "
1744 "'%s' in target creation request\n", p
);
1749 if ((opt_mask
& SRP_OPT_ALL
) == SRP_OPT_ALL
)
1752 for (i
= 0; i
< ARRAY_SIZE(srp_opt_tokens
); ++i
)
1753 if ((srp_opt_tokens
[i
].token
& SRP_OPT_ALL
) &&
1754 !(srp_opt_tokens
[i
].token
& opt_mask
))
1755 printk(KERN_WARNING PFX
"target creation request is "
1756 "missing parameter '%s'\n",
1757 srp_opt_tokens
[i
].pattern
);
1764 static ssize_t
srp_create_target(struct class_device
*class_dev
,
1765 const char *buf
, size_t count
)
1767 struct srp_host
*host
=
1768 container_of(class_dev
, struct srp_host
, class_dev
);
1769 struct Scsi_Host
*target_host
;
1770 struct srp_target_port
*target
;
1774 target_host
= scsi_host_alloc(&srp_template
,
1775 sizeof (struct srp_target_port
));
1779 target_host
->transportt
= ib_srp_transport_template
;
1780 target_host
->max_lun
= SRP_MAX_LUN
;
1781 target_host
->max_cmd_len
= sizeof ((struct srp_cmd
*) (void *) 0L)->cdb
;
1783 target
= host_to_target(target_host
);
1785 target
->io_class
= SRP_REV16A_IB_IO_CLASS
;
1786 target
->scsi_host
= target_host
;
1787 target
->srp_host
= host
;
1789 INIT_LIST_HEAD(&target
->free_reqs
);
1790 INIT_LIST_HEAD(&target
->req_queue
);
1791 for (i
= 0; i
< SRP_SQ_SIZE
; ++i
) {
1792 target
->req_ring
[i
].index
= i
;
1793 list_add_tail(&target
->req_ring
[i
].list
, &target
->free_reqs
);
1796 ret
= srp_parse_options(buf
, target
);
1800 ib_get_cached_gid(host
->dev
->dev
, host
->port
, 0, &target
->path
.sgid
);
1802 printk(KERN_DEBUG PFX
"new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1803 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1804 (unsigned long long) be64_to_cpu(target
->id_ext
),
1805 (unsigned long long) be64_to_cpu(target
->ioc_guid
),
1806 be16_to_cpu(target
->path
.pkey
),
1807 (unsigned long long) be64_to_cpu(target
->service_id
),
1808 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[0]),
1809 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[2]),
1810 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[4]),
1811 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[6]),
1812 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[8]),
1813 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[10]),
1814 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[12]),
1815 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[14]));
1817 ret
= srp_create_target_ib(target
);
1821 target
->cm_id
= ib_create_cm_id(host
->dev
->dev
, srp_cm_handler
, target
);
1822 if (IS_ERR(target
->cm_id
)) {
1823 ret
= PTR_ERR(target
->cm_id
);
1827 target
->qp_in_error
= 0;
1828 ret
= srp_connect_target(target
);
1830 printk(KERN_ERR PFX
"Connection failed\n");
1834 ret
= srp_add_target(host
, target
);
1836 goto err_disconnect
;
1841 srp_disconnect_target(target
);
1844 ib_destroy_cm_id(target
->cm_id
);
1847 srp_free_target_ib(target
);
1850 scsi_host_put(target_host
);
1855 static CLASS_DEVICE_ATTR(add_target
, S_IWUSR
, NULL
, srp_create_target
);
1857 static ssize_t
show_ibdev(struct class_device
*class_dev
, char *buf
)
1859 struct srp_host
*host
=
1860 container_of(class_dev
, struct srp_host
, class_dev
);
1862 return sprintf(buf
, "%s\n", host
->dev
->dev
->name
);
1865 static CLASS_DEVICE_ATTR(ibdev
, S_IRUGO
, show_ibdev
, NULL
);
1867 static ssize_t
show_port(struct class_device
*class_dev
, char *buf
)
1869 struct srp_host
*host
=
1870 container_of(class_dev
, struct srp_host
, class_dev
);
1872 return sprintf(buf
, "%d\n", host
->port
);
1875 static CLASS_DEVICE_ATTR(port
, S_IRUGO
, show_port
, NULL
);
1877 static struct srp_host
*srp_add_port(struct srp_device
*device
, u8 port
)
1879 struct srp_host
*host
;
1881 host
= kzalloc(sizeof *host
, GFP_KERNEL
);
1885 INIT_LIST_HEAD(&host
->target_list
);
1886 spin_lock_init(&host
->target_lock
);
1887 init_completion(&host
->released
);
1891 host
->class_dev
.class = &srp_class
;
1892 host
->class_dev
.dev
= device
->dev
->dma_device
;
1893 snprintf(host
->class_dev
.class_id
, BUS_ID_SIZE
, "srp-%s-%d",
1894 device
->dev
->name
, port
);
1896 if (class_device_register(&host
->class_dev
))
1898 if (class_device_create_file(&host
->class_dev
, &class_device_attr_add_target
))
1900 if (class_device_create_file(&host
->class_dev
, &class_device_attr_ibdev
))
1902 if (class_device_create_file(&host
->class_dev
, &class_device_attr_port
))
1908 class_device_unregister(&host
->class_dev
);
1916 static void srp_add_one(struct ib_device
*device
)
1918 struct srp_device
*srp_dev
;
1919 struct ib_device_attr
*dev_attr
;
1920 struct ib_fmr_pool_param fmr_param
;
1921 struct srp_host
*host
;
1924 dev_attr
= kmalloc(sizeof *dev_attr
, GFP_KERNEL
);
1928 if (ib_query_device(device
, dev_attr
)) {
1929 printk(KERN_WARNING PFX
"Query device failed for %s\n",
1934 srp_dev
= kmalloc(sizeof *srp_dev
, GFP_KERNEL
);
1939 * Use the smallest page size supported by the HCA, down to a
1940 * minimum of 512 bytes (which is the smallest sector that a
1941 * SCSI command will ever carry).
1943 srp_dev
->fmr_page_shift
= max(9, ffs(dev_attr
->page_size_cap
) - 1);
1944 srp_dev
->fmr_page_size
= 1 << srp_dev
->fmr_page_shift
;
1945 srp_dev
->fmr_page_mask
= ~((u64
) srp_dev
->fmr_page_size
- 1);
1947 INIT_LIST_HEAD(&srp_dev
->dev_list
);
1949 srp_dev
->dev
= device
;
1950 srp_dev
->pd
= ib_alloc_pd(device
);
1951 if (IS_ERR(srp_dev
->pd
))
1954 srp_dev
->mr
= ib_get_dma_mr(srp_dev
->pd
,
1955 IB_ACCESS_LOCAL_WRITE
|
1956 IB_ACCESS_REMOTE_READ
|
1957 IB_ACCESS_REMOTE_WRITE
);
1958 if (IS_ERR(srp_dev
->mr
))
1961 memset(&fmr_param
, 0, sizeof fmr_param
);
1962 fmr_param
.pool_size
= SRP_FMR_POOL_SIZE
;
1963 fmr_param
.dirty_watermark
= SRP_FMR_DIRTY_SIZE
;
1964 fmr_param
.cache
= 1;
1965 fmr_param
.max_pages_per_fmr
= SRP_FMR_SIZE
;
1966 fmr_param
.page_shift
= srp_dev
->fmr_page_shift
;
1967 fmr_param
.access
= (IB_ACCESS_LOCAL_WRITE
|
1968 IB_ACCESS_REMOTE_WRITE
|
1969 IB_ACCESS_REMOTE_READ
);
1971 srp_dev
->fmr_pool
= ib_create_fmr_pool(srp_dev
->pd
, &fmr_param
);
1972 if (IS_ERR(srp_dev
->fmr_pool
))
1973 srp_dev
->fmr_pool
= NULL
;
1975 if (device
->node_type
== RDMA_NODE_IB_SWITCH
) {
1980 e
= device
->phys_port_cnt
;
1983 for (p
= s
; p
<= e
; ++p
) {
1984 host
= srp_add_port(srp_dev
, p
);
1986 list_add_tail(&host
->list
, &srp_dev
->dev_list
);
1989 ib_set_client_data(device
, &srp_client
, srp_dev
);
1994 ib_dealloc_pd(srp_dev
->pd
);
2003 static void srp_remove_one(struct ib_device
*device
)
2005 struct srp_device
*srp_dev
;
2006 struct srp_host
*host
, *tmp_host
;
2007 LIST_HEAD(target_list
);
2008 struct srp_target_port
*target
, *tmp_target
;
2010 srp_dev
= ib_get_client_data(device
, &srp_client
);
2012 list_for_each_entry_safe(host
, tmp_host
, &srp_dev
->dev_list
, list
) {
2013 class_device_unregister(&host
->class_dev
);
2015 * Wait for the sysfs entry to go away, so that no new
2016 * target ports can be created.
2018 wait_for_completion(&host
->released
);
2021 * Mark all target ports as removed, so we stop queueing
2022 * commands and don't try to reconnect.
2024 spin_lock(&host
->target_lock
);
2025 list_for_each_entry(target
, &host
->target_list
, list
) {
2026 spin_lock_irq(target
->scsi_host
->host_lock
);
2027 target
->state
= SRP_TARGET_REMOVED
;
2028 spin_unlock_irq(target
->scsi_host
->host_lock
);
2030 spin_unlock(&host
->target_lock
);
2033 * Wait for any reconnection tasks that may have
2034 * started before we marked our target ports as
2035 * removed, and any target port removal tasks.
2037 flush_scheduled_work();
2039 list_for_each_entry_safe(target
, tmp_target
,
2040 &host
->target_list
, list
) {
2041 scsi_remove_host(target
->scsi_host
);
2042 srp_disconnect_target(target
);
2043 ib_destroy_cm_id(target
->cm_id
);
2044 srp_free_target_ib(target
);
2045 scsi_host_put(target
->scsi_host
);
2051 if (srp_dev
->fmr_pool
)
2052 ib_destroy_fmr_pool(srp_dev
->fmr_pool
);
2053 ib_dereg_mr(srp_dev
->mr
);
2054 ib_dealloc_pd(srp_dev
->pd
);
2059 static struct srp_function_template ib_srp_transport_functions
= {
2062 static int __init
srp_init_module(void)
2066 ib_srp_transport_template
=
2067 srp_attach_transport(&ib_srp_transport_functions
);
2068 if (!ib_srp_transport_template
)
2071 srp_template
.sg_tablesize
= srp_sg_tablesize
;
2072 srp_max_iu_len
= (sizeof (struct srp_cmd
) +
2073 sizeof (struct srp_indirect_buf
) +
2074 srp_sg_tablesize
* 16);
2076 ret
= class_register(&srp_class
);
2078 printk(KERN_ERR PFX
"couldn't register class infiniband_srp\n");
2079 srp_release_transport(ib_srp_transport_template
);
2083 ib_sa_register_client(&srp_sa_client
);
2085 ret
= ib_register_client(&srp_client
);
2087 printk(KERN_ERR PFX
"couldn't register IB client\n");
2088 srp_release_transport(ib_srp_transport_template
);
2089 ib_sa_unregister_client(&srp_sa_client
);
2090 class_unregister(&srp_class
);
2097 static void __exit
srp_cleanup_module(void)
2099 ib_unregister_client(&srp_client
);
2100 ib_sa_unregister_client(&srp_sa_client
);
2101 class_unregister(&srp_class
);
2102 srp_release_transport(ib_srp_transport_template
);
2105 module_init(srp_init_module
);
2106 module_exit(srp_cleanup_module
);