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 srp_sg_tablesize
= SRP_DEF_SG_TABLESIZE
;
66 static int srp_max_iu_len
;
68 module_param(srp_sg_tablesize
, int, 0444);
69 MODULE_PARM_DESC(srp_sg_tablesize
,
70 "Max number of gather/scatter entries per I/O (default is 12)");
72 static int topspin_workarounds
= 1;
74 module_param(topspin_workarounds
, int, 0444);
75 MODULE_PARM_DESC(topspin_workarounds
,
76 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
78 static int mellanox_workarounds
= 1;
80 module_param(mellanox_workarounds
, int, 0444);
81 MODULE_PARM_DESC(mellanox_workarounds
,
82 "Enable workarounds for Mellanox SRP target bugs if != 0");
84 static void srp_add_one(struct ib_device
*device
);
85 static void srp_remove_one(struct ib_device
*device
);
86 static void srp_completion(struct ib_cq
*cq
, void *target_ptr
);
87 static int srp_cm_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*event
);
89 static struct ib_client srp_client
= {
92 .remove
= srp_remove_one
95 static struct ib_sa_client srp_sa_client
;
97 static inline struct srp_target_port
*host_to_target(struct Scsi_Host
*host
)
99 return (struct srp_target_port
*) host
->hostdata
;
102 static const char *srp_target_info(struct Scsi_Host
*host
)
104 return host_to_target(host
)->target_name
;
107 static int srp_target_is_topspin(struct srp_target_port
*target
)
109 static const u8 topspin_oui
[3] = { 0x00, 0x05, 0xad };
111 return topspin_workarounds
&&
112 !memcmp(&target
->ioc_guid
, topspin_oui
, sizeof topspin_oui
);
115 static int srp_target_is_mellanox(struct srp_target_port
*target
)
117 static const u8 mellanox_oui
[3] = { 0x00, 0x02, 0xc9 };
119 return mellanox_workarounds
&&
120 !memcmp(&target
->ioc_guid
, mellanox_oui
, sizeof mellanox_oui
);
123 static struct srp_iu
*srp_alloc_iu(struct srp_host
*host
, size_t size
,
125 enum dma_data_direction direction
)
129 iu
= kmalloc(sizeof *iu
, gfp_mask
);
133 iu
->buf
= kzalloc(size
, gfp_mask
);
137 iu
->dma
= ib_dma_map_single(host
->dev
->dev
, iu
->buf
, size
, direction
);
138 if (ib_dma_mapping_error(host
->dev
->dev
, iu
->dma
))
142 iu
->direction
= direction
;
154 static void srp_free_iu(struct srp_host
*host
, struct srp_iu
*iu
)
159 ib_dma_unmap_single(host
->dev
->dev
, iu
->dma
, iu
->size
, iu
->direction
);
164 static void srp_qp_event(struct ib_event
*event
, void *context
)
166 printk(KERN_ERR PFX
"QP event %d\n", event
->event
);
169 static int srp_init_qp(struct srp_target_port
*target
,
172 struct ib_qp_attr
*attr
;
175 attr
= kmalloc(sizeof *attr
, GFP_KERNEL
);
179 ret
= ib_find_cached_pkey(target
->srp_host
->dev
->dev
,
180 target
->srp_host
->port
,
181 be16_to_cpu(target
->path
.pkey
),
186 attr
->qp_state
= IB_QPS_INIT
;
187 attr
->qp_access_flags
= (IB_ACCESS_REMOTE_READ
|
188 IB_ACCESS_REMOTE_WRITE
);
189 attr
->port_num
= target
->srp_host
->port
;
191 ret
= ib_modify_qp(qp
, attr
,
202 static int srp_create_target_ib(struct srp_target_port
*target
)
204 struct ib_qp_init_attr
*init_attr
;
207 init_attr
= kzalloc(sizeof *init_attr
, GFP_KERNEL
);
211 target
->cq
= ib_create_cq(target
->srp_host
->dev
->dev
, srp_completion
,
212 NULL
, target
, SRP_CQ_SIZE
, 0);
213 if (IS_ERR(target
->cq
)) {
214 ret
= PTR_ERR(target
->cq
);
218 ib_req_notify_cq(target
->cq
, IB_CQ_NEXT_COMP
);
220 init_attr
->event_handler
= srp_qp_event
;
221 init_attr
->cap
.max_send_wr
= SRP_SQ_SIZE
;
222 init_attr
->cap
.max_recv_wr
= SRP_RQ_SIZE
;
223 init_attr
->cap
.max_recv_sge
= 1;
224 init_attr
->cap
.max_send_sge
= 1;
225 init_attr
->sq_sig_type
= IB_SIGNAL_ALL_WR
;
226 init_attr
->qp_type
= IB_QPT_RC
;
227 init_attr
->send_cq
= target
->cq
;
228 init_attr
->recv_cq
= target
->cq
;
230 target
->qp
= ib_create_qp(target
->srp_host
->dev
->pd
, init_attr
);
231 if (IS_ERR(target
->qp
)) {
232 ret
= PTR_ERR(target
->qp
);
233 ib_destroy_cq(target
->cq
);
237 ret
= srp_init_qp(target
, target
->qp
);
239 ib_destroy_qp(target
->qp
);
240 ib_destroy_cq(target
->cq
);
249 static void srp_free_target_ib(struct srp_target_port
*target
)
253 ib_destroy_qp(target
->qp
);
254 ib_destroy_cq(target
->cq
);
256 for (i
= 0; i
< SRP_RQ_SIZE
; ++i
)
257 srp_free_iu(target
->srp_host
, target
->rx_ring
[i
]);
258 for (i
= 0; i
< SRP_SQ_SIZE
+ 1; ++i
)
259 srp_free_iu(target
->srp_host
, target
->tx_ring
[i
]);
262 static void srp_path_rec_completion(int status
,
263 struct ib_sa_path_rec
*pathrec
,
266 struct srp_target_port
*target
= target_ptr
;
268 target
->status
= status
;
270 printk(KERN_ERR PFX
"Got failed path rec status %d\n", status
);
272 target
->path
= *pathrec
;
273 complete(&target
->done
);
276 static int srp_lookup_path(struct srp_target_port
*target
)
278 target
->path
.numb_path
= 1;
280 init_completion(&target
->done
);
282 target
->path_query_id
= ib_sa_path_rec_get(&srp_sa_client
,
283 target
->srp_host
->dev
->dev
,
284 target
->srp_host
->port
,
286 IB_SA_PATH_REC_DGID
|
287 IB_SA_PATH_REC_SGID
|
288 IB_SA_PATH_REC_NUMB_PATH
|
290 SRP_PATH_REC_TIMEOUT_MS
,
292 srp_path_rec_completion
,
293 target
, &target
->path_query
);
294 if (target
->path_query_id
< 0)
295 return target
->path_query_id
;
297 wait_for_completion(&target
->done
);
299 if (target
->status
< 0)
300 printk(KERN_WARNING PFX
"Path record query failed\n");
302 return target
->status
;
305 static int srp_send_req(struct srp_target_port
*target
)
308 struct ib_cm_req_param param
;
309 struct srp_login_req priv
;
313 req
= kzalloc(sizeof *req
, GFP_KERNEL
);
317 req
->param
.primary_path
= &target
->path
;
318 req
->param
.alternate_path
= NULL
;
319 req
->param
.service_id
= target
->service_id
;
320 req
->param
.qp_num
= target
->qp
->qp_num
;
321 req
->param
.qp_type
= target
->qp
->qp_type
;
322 req
->param
.private_data
= &req
->priv
;
323 req
->param
.private_data_len
= sizeof req
->priv
;
324 req
->param
.flow_control
= 1;
326 get_random_bytes(&req
->param
.starting_psn
, 4);
327 req
->param
.starting_psn
&= 0xffffff;
330 * Pick some arbitrary defaults here; we could make these
331 * module parameters if anyone cared about setting them.
333 req
->param
.responder_resources
= 4;
334 req
->param
.remote_cm_response_timeout
= 20;
335 req
->param
.local_cm_response_timeout
= 20;
336 req
->param
.retry_count
= 7;
337 req
->param
.rnr_retry_count
= 7;
338 req
->param
.max_cm_retries
= 15;
340 req
->priv
.opcode
= SRP_LOGIN_REQ
;
342 req
->priv
.req_it_iu_len
= cpu_to_be32(srp_max_iu_len
);
343 req
->priv
.req_buf_fmt
= cpu_to_be16(SRP_BUF_FORMAT_DIRECT
|
344 SRP_BUF_FORMAT_INDIRECT
);
346 * In the published SRP specification (draft rev. 16a), the
347 * port identifier format is 8 bytes of ID extension followed
348 * by 8 bytes of GUID. Older drafts put the two halves in the
349 * opposite order, so that the GUID comes first.
351 * Targets conforming to these obsolete drafts can be
352 * recognized by the I/O Class they report.
354 if (target
->io_class
== SRP_REV10_IB_IO_CLASS
) {
355 memcpy(req
->priv
.initiator_port_id
,
356 &target
->path
.sgid
.global
.interface_id
, 8);
357 memcpy(req
->priv
.initiator_port_id
+ 8,
358 &target
->initiator_ext
, 8);
359 memcpy(req
->priv
.target_port_id
, &target
->ioc_guid
, 8);
360 memcpy(req
->priv
.target_port_id
+ 8, &target
->id_ext
, 8);
362 memcpy(req
->priv
.initiator_port_id
,
363 &target
->initiator_ext
, 8);
364 memcpy(req
->priv
.initiator_port_id
+ 8,
365 &target
->path
.sgid
.global
.interface_id
, 8);
366 memcpy(req
->priv
.target_port_id
, &target
->id_ext
, 8);
367 memcpy(req
->priv
.target_port_id
+ 8, &target
->ioc_guid
, 8);
371 * Topspin/Cisco SRP targets will reject our login unless we
372 * zero out the first 8 bytes of our initiator port ID and set
373 * the second 8 bytes to the local node GUID.
375 if (srp_target_is_topspin(target
)) {
376 printk(KERN_DEBUG PFX
"Topspin/Cisco initiator port ID workaround "
377 "activated for target GUID %016llx\n",
378 (unsigned long long) be64_to_cpu(target
->ioc_guid
));
379 memset(req
->priv
.initiator_port_id
, 0, 8);
380 memcpy(req
->priv
.initiator_port_id
+ 8,
381 &target
->srp_host
->dev
->dev
->node_guid
, 8);
384 status
= ib_send_cm_req(target
->cm_id
, &req
->param
);
391 static void srp_disconnect_target(struct srp_target_port
*target
)
393 /* XXX should send SRP_I_LOGOUT request */
395 init_completion(&target
->done
);
396 if (ib_send_cm_dreq(target
->cm_id
, NULL
, 0)) {
397 printk(KERN_DEBUG PFX
"Sending CM DREQ failed\n");
400 wait_for_completion(&target
->done
);
403 static void srp_remove_work(struct work_struct
*work
)
405 struct srp_target_port
*target
=
406 container_of(work
, struct srp_target_port
, work
);
408 spin_lock_irq(target
->scsi_host
->host_lock
);
409 if (target
->state
!= SRP_TARGET_DEAD
) {
410 spin_unlock_irq(target
->scsi_host
->host_lock
);
413 target
->state
= SRP_TARGET_REMOVED
;
414 spin_unlock_irq(target
->scsi_host
->host_lock
);
416 spin_lock(&target
->srp_host
->target_lock
);
417 list_del(&target
->list
);
418 spin_unlock(&target
->srp_host
->target_lock
);
420 scsi_remove_host(target
->scsi_host
);
421 ib_destroy_cm_id(target
->cm_id
);
422 srp_free_target_ib(target
);
423 scsi_host_put(target
->scsi_host
);
426 static int srp_connect_target(struct srp_target_port
*target
)
430 ret
= srp_lookup_path(target
);
435 init_completion(&target
->done
);
436 ret
= srp_send_req(target
);
439 wait_for_completion(&target
->done
);
442 * The CM event handling code will set status to
443 * SRP_PORT_REDIRECT if we get a port redirect REJ
444 * back, or SRP_DLID_REDIRECT if we get a lid/qp
447 switch (target
->status
) {
451 case SRP_PORT_REDIRECT
:
452 ret
= srp_lookup_path(target
);
457 case SRP_DLID_REDIRECT
:
461 return target
->status
;
466 static void srp_unmap_data(struct scsi_cmnd
*scmnd
,
467 struct srp_target_port
*target
,
468 struct srp_request
*req
)
470 if (!scsi_sglist(scmnd
) ||
471 (scmnd
->sc_data_direction
!= DMA_TO_DEVICE
&&
472 scmnd
->sc_data_direction
!= DMA_FROM_DEVICE
))
476 ib_fmr_pool_unmap(req
->fmr
);
480 ib_dma_unmap_sg(target
->srp_host
->dev
->dev
, scsi_sglist(scmnd
),
481 scsi_sg_count(scmnd
), scmnd
->sc_data_direction
);
484 static void srp_remove_req(struct srp_target_port
*target
, struct srp_request
*req
)
486 srp_unmap_data(req
->scmnd
, target
, req
);
487 list_move_tail(&req
->list
, &target
->free_reqs
);
490 static void srp_reset_req(struct srp_target_port
*target
, struct srp_request
*req
)
492 req
->scmnd
->result
= DID_RESET
<< 16;
493 req
->scmnd
->scsi_done(req
->scmnd
);
494 srp_remove_req(target
, req
);
497 static int srp_reconnect_target(struct srp_target_port
*target
)
499 struct ib_cm_id
*new_cm_id
;
500 struct ib_qp_attr qp_attr
;
501 struct srp_request
*req
, *tmp
;
505 spin_lock_irq(target
->scsi_host
->host_lock
);
506 if (target
->state
!= SRP_TARGET_LIVE
) {
507 spin_unlock_irq(target
->scsi_host
->host_lock
);
510 target
->state
= SRP_TARGET_CONNECTING
;
511 spin_unlock_irq(target
->scsi_host
->host_lock
);
513 srp_disconnect_target(target
);
515 * Now get a new local CM ID so that we avoid confusing the
516 * target in case things are really fouled up.
518 new_cm_id
= ib_create_cm_id(target
->srp_host
->dev
->dev
,
519 srp_cm_handler
, target
);
520 if (IS_ERR(new_cm_id
)) {
521 ret
= PTR_ERR(new_cm_id
);
524 ib_destroy_cm_id(target
->cm_id
);
525 target
->cm_id
= new_cm_id
;
527 qp_attr
.qp_state
= IB_QPS_RESET
;
528 ret
= ib_modify_qp(target
->qp
, &qp_attr
, IB_QP_STATE
);
532 ret
= srp_init_qp(target
, target
->qp
);
536 while (ib_poll_cq(target
->cq
, 1, &wc
) > 0)
539 spin_lock_irq(target
->scsi_host
->host_lock
);
540 list_for_each_entry_safe(req
, tmp
, &target
->req_queue
, list
)
541 srp_reset_req(target
, req
);
542 spin_unlock_irq(target
->scsi_host
->host_lock
);
548 target
->qp_in_error
= 0;
549 ret
= srp_connect_target(target
);
553 spin_lock_irq(target
->scsi_host
->host_lock
);
554 if (target
->state
== SRP_TARGET_CONNECTING
) {
556 target
->state
= SRP_TARGET_LIVE
;
559 spin_unlock_irq(target
->scsi_host
->host_lock
);
564 printk(KERN_ERR PFX
"reconnect failed (%d), removing target port.\n", ret
);
567 * We couldn't reconnect, so kill our target port off.
568 * However, we have to defer the real removal because we might
569 * be in the context of the SCSI error handler now, which
570 * would deadlock if we call scsi_remove_host().
572 spin_lock_irq(target
->scsi_host
->host_lock
);
573 if (target
->state
== SRP_TARGET_CONNECTING
) {
574 target
->state
= SRP_TARGET_DEAD
;
575 INIT_WORK(&target
->work
, srp_remove_work
);
576 schedule_work(&target
->work
);
578 spin_unlock_irq(target
->scsi_host
->host_lock
);
583 static int srp_map_fmr(struct srp_target_port
*target
, struct scatterlist
*scat
,
584 int sg_cnt
, struct srp_request
*req
,
585 struct srp_direct_buf
*buf
)
593 struct srp_device
*dev
= target
->srp_host
->dev
;
594 struct ib_device
*ibdev
= dev
->dev
;
595 struct scatterlist
*sg
;
600 if (srp_target_is_mellanox(target
) &&
601 (ib_sg_dma_address(ibdev
, &scat
[0]) & ~dev
->fmr_page_mask
))
605 scsi_for_each_sg(req
->scmnd
, sg
, sg_cnt
, i
) {
606 unsigned int dma_len
= ib_sg_dma_len(ibdev
, sg
);
608 if (ib_sg_dma_address(ibdev
, sg
) & ~dev
->fmr_page_mask
) {
614 if ((ib_sg_dma_address(ibdev
, sg
) + dma_len
) &
615 ~dev
->fmr_page_mask
) {
625 page_cnt
+= len
>> dev
->fmr_page_shift
;
626 if (page_cnt
> SRP_FMR_SIZE
)
629 dma_pages
= kmalloc(sizeof (u64
) * page_cnt
, GFP_ATOMIC
);
634 scsi_for_each_sg(req
->scmnd
, sg
, sg_cnt
, i
) {
635 unsigned int dma_len
= ib_sg_dma_len(ibdev
, sg
);
637 for (j
= 0; j
< dma_len
; j
+= dev
->fmr_page_size
)
638 dma_pages
[page_cnt
++] =
639 (ib_sg_dma_address(ibdev
, sg
) &
640 dev
->fmr_page_mask
) + j
;
643 req
->fmr
= ib_fmr_pool_map_phys(dev
->fmr_pool
,
644 dma_pages
, page_cnt
, io_addr
);
645 if (IS_ERR(req
->fmr
)) {
646 ret
= PTR_ERR(req
->fmr
);
651 buf
->va
= cpu_to_be64(ib_sg_dma_address(ibdev
, &scat
[0]) &
652 ~dev
->fmr_page_mask
);
653 buf
->key
= cpu_to_be32(req
->fmr
->fmr
->rkey
);
654 buf
->len
= cpu_to_be32(len
);
664 static int srp_map_data(struct scsi_cmnd
*scmnd
, struct srp_target_port
*target
,
665 struct srp_request
*req
)
667 struct scatterlist
*scat
;
668 struct srp_cmd
*cmd
= req
->cmd
->buf
;
669 int len
, nents
, count
;
670 u8 fmt
= SRP_DATA_DESC_DIRECT
;
671 struct srp_device
*dev
;
672 struct ib_device
*ibdev
;
674 if (!scsi_sglist(scmnd
) || scmnd
->sc_data_direction
== DMA_NONE
)
675 return sizeof (struct srp_cmd
);
677 if (scmnd
->sc_data_direction
!= DMA_FROM_DEVICE
&&
678 scmnd
->sc_data_direction
!= DMA_TO_DEVICE
) {
679 printk(KERN_WARNING PFX
"Unhandled data direction %d\n",
680 scmnd
->sc_data_direction
);
684 nents
= scsi_sg_count(scmnd
);
685 scat
= scsi_sglist(scmnd
);
687 dev
= target
->srp_host
->dev
;
690 count
= ib_dma_map_sg(ibdev
, scat
, nents
, scmnd
->sc_data_direction
);
692 fmt
= SRP_DATA_DESC_DIRECT
;
693 len
= sizeof (struct srp_cmd
) + sizeof (struct srp_direct_buf
);
697 * The midlayer only generated a single gather/scatter
698 * entry, or DMA mapping coalesced everything to a
699 * single entry. So a direct descriptor along with
700 * the DMA MR suffices.
702 struct srp_direct_buf
*buf
= (void *) cmd
->add_data
;
704 buf
->va
= cpu_to_be64(ib_sg_dma_address(ibdev
, scat
));
705 buf
->key
= cpu_to_be32(dev
->mr
->rkey
);
706 buf
->len
= cpu_to_be32(ib_sg_dma_len(ibdev
, scat
));
707 } else if (srp_map_fmr(target
, scat
, count
, req
,
708 (void *) cmd
->add_data
)) {
710 * FMR mapping failed, and the scatterlist has more
711 * than one entry. Generate an indirect memory
714 struct srp_indirect_buf
*buf
= (void *) cmd
->add_data
;
715 struct scatterlist
*sg
;
719 fmt
= SRP_DATA_DESC_INDIRECT
;
720 len
= sizeof (struct srp_cmd
) +
721 sizeof (struct srp_indirect_buf
) +
722 count
* sizeof (struct srp_direct_buf
);
724 scsi_for_each_sg(scmnd
, sg
, count
, i
) {
725 unsigned int dma_len
= ib_sg_dma_len(ibdev
, sg
);
727 buf
->desc_list
[i
].va
=
728 cpu_to_be64(ib_sg_dma_address(ibdev
, sg
));
729 buf
->desc_list
[i
].key
=
730 cpu_to_be32(dev
->mr
->rkey
);
731 buf
->desc_list
[i
].len
= cpu_to_be32(dma_len
);
735 if (scmnd
->sc_data_direction
== DMA_TO_DEVICE
)
736 cmd
->data_out_desc_cnt
= count
;
738 cmd
->data_in_desc_cnt
= count
;
741 cpu_to_be64(req
->cmd
->dma
+ sizeof *cmd
+ sizeof *buf
);
742 buf
->table_desc
.key
=
743 cpu_to_be32(target
->srp_host
->dev
->mr
->rkey
);
744 buf
->table_desc
.len
=
745 cpu_to_be32(count
* sizeof (struct srp_direct_buf
));
747 buf
->len
= cpu_to_be32(datalen
);
750 if (scmnd
->sc_data_direction
== DMA_TO_DEVICE
)
751 cmd
->buf_fmt
= fmt
<< 4;
758 static void srp_process_rsp(struct srp_target_port
*target
, struct srp_rsp
*rsp
)
760 struct srp_request
*req
;
761 struct scsi_cmnd
*scmnd
;
765 delta
= (s32
) be32_to_cpu(rsp
->req_lim_delta
);
767 spin_lock_irqsave(target
->scsi_host
->host_lock
, flags
);
769 target
->req_lim
+= delta
;
771 req
= &target
->req_ring
[rsp
->tag
& ~SRP_TAG_TSK_MGMT
];
773 if (unlikely(rsp
->tag
& SRP_TAG_TSK_MGMT
)) {
774 if (be32_to_cpu(rsp
->resp_data_len
) < 4)
775 req
->tsk_status
= -1;
777 req
->tsk_status
= rsp
->data
[3];
778 complete(&req
->done
);
782 printk(KERN_ERR
"Null scmnd for RSP w/tag %016llx\n",
783 (unsigned long long) rsp
->tag
);
784 scmnd
->result
= rsp
->status
;
786 if (rsp
->flags
& SRP_RSP_FLAG_SNSVALID
) {
787 memcpy(scmnd
->sense_buffer
, rsp
->data
+
788 be32_to_cpu(rsp
->resp_data_len
),
789 min_t(int, be32_to_cpu(rsp
->sense_data_len
),
790 SCSI_SENSE_BUFFERSIZE
));
793 if (rsp
->flags
& (SRP_RSP_FLAG_DOOVER
| SRP_RSP_FLAG_DOUNDER
))
794 scsi_set_resid(scmnd
, be32_to_cpu(rsp
->data_out_res_cnt
));
795 else if (rsp
->flags
& (SRP_RSP_FLAG_DIOVER
| SRP_RSP_FLAG_DIUNDER
))
796 scsi_set_resid(scmnd
, be32_to_cpu(rsp
->data_in_res_cnt
));
798 if (!req
->tsk_mgmt
) {
799 scmnd
->host_scribble
= (void *) -1L;
800 scmnd
->scsi_done(scmnd
);
802 srp_remove_req(target
, req
);
807 spin_unlock_irqrestore(target
->scsi_host
->host_lock
, flags
);
810 static void srp_handle_recv(struct srp_target_port
*target
, struct ib_wc
*wc
)
812 struct ib_device
*dev
;
816 iu
= target
->rx_ring
[wc
->wr_id
& ~SRP_OP_RECV
];
818 dev
= target
->srp_host
->dev
->dev
;
819 ib_dma_sync_single_for_cpu(dev
, iu
->dma
, target
->max_ti_iu_len
,
822 opcode
= *(u8
*) iu
->buf
;
827 printk(KERN_ERR PFX
"recv completion, opcode 0x%02x\n", opcode
);
829 for (i
= 0; i
< wc
->byte_len
; ++i
) {
831 printk(KERN_ERR
" [%02x] ", i
);
832 printk(" %02x", ((u8
*) iu
->buf
)[i
]);
833 if ((i
+ 1) % 8 == 0)
837 if (wc
->byte_len
% 8)
843 srp_process_rsp(target
, iu
->buf
);
847 /* XXX Handle target logout */
848 printk(KERN_WARNING PFX
"Got target logout request\n");
852 printk(KERN_WARNING PFX
"Unhandled SRP opcode 0x%02x\n", opcode
);
856 ib_dma_sync_single_for_device(dev
, iu
->dma
, target
->max_ti_iu_len
,
860 static void srp_completion(struct ib_cq
*cq
, void *target_ptr
)
862 struct srp_target_port
*target
= target_ptr
;
865 ib_req_notify_cq(cq
, IB_CQ_NEXT_COMP
);
866 while (ib_poll_cq(cq
, 1, &wc
) > 0) {
868 printk(KERN_ERR PFX
"failed %s status %d\n",
869 wc
.wr_id
& SRP_OP_RECV
? "receive" : "send",
871 target
->qp_in_error
= 1;
875 if (wc
.wr_id
& SRP_OP_RECV
)
876 srp_handle_recv(target
, &wc
);
882 static int __srp_post_recv(struct srp_target_port
*target
)
886 struct ib_recv_wr wr
, *bad_wr
;
890 next
= target
->rx_head
& (SRP_RQ_SIZE
- 1);
891 wr
.wr_id
= next
| SRP_OP_RECV
;
892 iu
= target
->rx_ring
[next
];
895 list
.length
= iu
->size
;
896 list
.lkey
= target
->srp_host
->dev
->mr
->lkey
;
902 ret
= ib_post_recv(target
->qp
, &wr
, &bad_wr
);
909 static int srp_post_recv(struct srp_target_port
*target
)
914 spin_lock_irqsave(target
->scsi_host
->host_lock
, flags
);
915 ret
= __srp_post_recv(target
);
916 spin_unlock_irqrestore(target
->scsi_host
->host_lock
, flags
);
922 * Must be called with target->scsi_host->host_lock held to protect
923 * req_lim and tx_head. Lock cannot be dropped between call here and
924 * call to __srp_post_send().
926 static struct srp_iu
*__srp_get_tx_iu(struct srp_target_port
*target
)
928 if (target
->tx_head
- target
->tx_tail
>= SRP_SQ_SIZE
)
931 if (unlikely(target
->req_lim
< 1))
932 ++target
->zero_req_lim
;
934 return target
->tx_ring
[target
->tx_head
& SRP_SQ_SIZE
];
938 * Must be called with target->scsi_host->host_lock held to protect
939 * req_lim and tx_head.
941 static int __srp_post_send(struct srp_target_port
*target
,
942 struct srp_iu
*iu
, int len
)
945 struct ib_send_wr wr
, *bad_wr
;
950 list
.lkey
= target
->srp_host
->dev
->mr
->lkey
;
953 wr
.wr_id
= target
->tx_head
& SRP_SQ_SIZE
;
956 wr
.opcode
= IB_WR_SEND
;
957 wr
.send_flags
= IB_SEND_SIGNALED
;
959 ret
= ib_post_send(target
->qp
, &wr
, &bad_wr
);
969 static int srp_queuecommand(struct scsi_cmnd
*scmnd
,
970 void (*done
)(struct scsi_cmnd
*))
972 struct srp_target_port
*target
= host_to_target(scmnd
->device
->host
);
973 struct srp_request
*req
;
976 struct ib_device
*dev
;
979 if (target
->state
== SRP_TARGET_CONNECTING
)
982 if (target
->state
== SRP_TARGET_DEAD
||
983 target
->state
== SRP_TARGET_REMOVED
) {
984 scmnd
->result
= DID_BAD_TARGET
<< 16;
989 iu
= __srp_get_tx_iu(target
);
993 dev
= target
->srp_host
->dev
->dev
;
994 ib_dma_sync_single_for_cpu(dev
, iu
->dma
, srp_max_iu_len
,
997 req
= list_entry(target
->free_reqs
.next
, struct srp_request
, list
);
999 scmnd
->scsi_done
= done
;
1001 scmnd
->host_scribble
= (void *) (long) req
->index
;
1004 memset(cmd
, 0, sizeof *cmd
);
1006 cmd
->opcode
= SRP_CMD
;
1007 cmd
->lun
= cpu_to_be64((u64
) scmnd
->device
->lun
<< 48);
1008 cmd
->tag
= req
->index
;
1009 memcpy(cmd
->cdb
, scmnd
->cmnd
, scmnd
->cmd_len
);
1014 req
->tsk_mgmt
= NULL
;
1016 len
= srp_map_data(scmnd
, target
, req
);
1018 printk(KERN_ERR PFX
"Failed to map data\n");
1022 if (__srp_post_recv(target
)) {
1023 printk(KERN_ERR PFX
"Recv failed\n");
1027 ib_dma_sync_single_for_device(dev
, iu
->dma
, srp_max_iu_len
,
1030 if (__srp_post_send(target
, iu
, len
)) {
1031 printk(KERN_ERR PFX
"Send failed\n");
1035 list_move_tail(&req
->list
, &target
->req_queue
);
1040 srp_unmap_data(scmnd
, target
, req
);
1043 return SCSI_MLQUEUE_HOST_BUSY
;
1046 static int srp_alloc_iu_bufs(struct srp_target_port
*target
)
1050 for (i
= 0; i
< SRP_RQ_SIZE
; ++i
) {
1051 target
->rx_ring
[i
] = srp_alloc_iu(target
->srp_host
,
1052 target
->max_ti_iu_len
,
1053 GFP_KERNEL
, DMA_FROM_DEVICE
);
1054 if (!target
->rx_ring
[i
])
1058 for (i
= 0; i
< SRP_SQ_SIZE
+ 1; ++i
) {
1059 target
->tx_ring
[i
] = srp_alloc_iu(target
->srp_host
,
1061 GFP_KERNEL
, DMA_TO_DEVICE
);
1062 if (!target
->tx_ring
[i
])
1069 for (i
= 0; i
< SRP_RQ_SIZE
; ++i
) {
1070 srp_free_iu(target
->srp_host
, target
->rx_ring
[i
]);
1071 target
->rx_ring
[i
] = NULL
;
1074 for (i
= 0; i
< SRP_SQ_SIZE
+ 1; ++i
) {
1075 srp_free_iu(target
->srp_host
, target
->tx_ring
[i
]);
1076 target
->tx_ring
[i
] = NULL
;
1082 static void srp_cm_rej_handler(struct ib_cm_id
*cm_id
,
1083 struct ib_cm_event
*event
,
1084 struct srp_target_port
*target
)
1086 struct ib_class_port_info
*cpi
;
1089 switch (event
->param
.rej_rcvd
.reason
) {
1090 case IB_CM_REJ_PORT_CM_REDIRECT
:
1091 cpi
= event
->param
.rej_rcvd
.ari
;
1092 target
->path
.dlid
= cpi
->redirect_lid
;
1093 target
->path
.pkey
= cpi
->redirect_pkey
;
1094 cm_id
->remote_cm_qpn
= be32_to_cpu(cpi
->redirect_qp
) & 0x00ffffff;
1095 memcpy(target
->path
.dgid
.raw
, cpi
->redirect_gid
, 16);
1097 target
->status
= target
->path
.dlid
?
1098 SRP_DLID_REDIRECT
: SRP_PORT_REDIRECT
;
1101 case IB_CM_REJ_PORT_REDIRECT
:
1102 if (srp_target_is_topspin(target
)) {
1104 * Topspin/Cisco SRP gateways incorrectly send
1105 * reject reason code 25 when they mean 24
1108 memcpy(target
->path
.dgid
.raw
,
1109 event
->param
.rej_rcvd
.ari
, 16);
1111 printk(KERN_DEBUG PFX
"Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1112 (unsigned long long) be64_to_cpu(target
->path
.dgid
.global
.subnet_prefix
),
1113 (unsigned long long) be64_to_cpu(target
->path
.dgid
.global
.interface_id
));
1115 target
->status
= SRP_PORT_REDIRECT
;
1117 printk(KERN_WARNING
" REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1118 target
->status
= -ECONNRESET
;
1122 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID
:
1123 printk(KERN_WARNING
" REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1124 target
->status
= -ECONNRESET
;
1127 case IB_CM_REJ_CONSUMER_DEFINED
:
1128 opcode
= *(u8
*) event
->private_data
;
1129 if (opcode
== SRP_LOGIN_REJ
) {
1130 struct srp_login_rej
*rej
= event
->private_data
;
1131 u32 reason
= be32_to_cpu(rej
->reason
);
1133 if (reason
== SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE
)
1134 printk(KERN_WARNING PFX
1135 "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1137 printk(KERN_WARNING PFX
1138 "SRP LOGIN REJECTED, reason 0x%08x\n", reason
);
1140 printk(KERN_WARNING
" REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1141 " opcode 0x%02x\n", opcode
);
1142 target
->status
= -ECONNRESET
;
1146 printk(KERN_WARNING
" REJ reason 0x%x\n",
1147 event
->param
.rej_rcvd
.reason
);
1148 target
->status
= -ECONNRESET
;
1152 static int srp_cm_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*event
)
1154 struct srp_target_port
*target
= cm_id
->context
;
1155 struct ib_qp_attr
*qp_attr
= NULL
;
1160 switch (event
->event
) {
1161 case IB_CM_REQ_ERROR
:
1162 printk(KERN_DEBUG PFX
"Sending CM REQ failed\n");
1164 target
->status
= -ECONNRESET
;
1167 case IB_CM_REP_RECEIVED
:
1169 opcode
= *(u8
*) event
->private_data
;
1171 if (opcode
== SRP_LOGIN_RSP
) {
1172 struct srp_login_rsp
*rsp
= event
->private_data
;
1174 target
->max_ti_iu_len
= be32_to_cpu(rsp
->max_ti_iu_len
);
1175 target
->req_lim
= be32_to_cpu(rsp
->req_lim_delta
);
1177 target
->scsi_host
->can_queue
= min(target
->req_lim
,
1178 target
->scsi_host
->can_queue
);
1180 printk(KERN_WARNING PFX
"Unhandled RSP opcode %#x\n", opcode
);
1181 target
->status
= -ECONNRESET
;
1185 if (!target
->rx_ring
[0]) {
1186 target
->status
= srp_alloc_iu_bufs(target
);
1191 qp_attr
= kmalloc(sizeof *qp_attr
, GFP_KERNEL
);
1193 target
->status
= -ENOMEM
;
1197 qp_attr
->qp_state
= IB_QPS_RTR
;
1198 target
->status
= ib_cm_init_qp_attr(cm_id
, qp_attr
, &attr_mask
);
1202 target
->status
= ib_modify_qp(target
->qp
, qp_attr
, attr_mask
);
1206 target
->status
= srp_post_recv(target
);
1210 qp_attr
->qp_state
= IB_QPS_RTS
;
1211 target
->status
= ib_cm_init_qp_attr(cm_id
, qp_attr
, &attr_mask
);
1215 target
->status
= ib_modify_qp(target
->qp
, qp_attr
, attr_mask
);
1219 target
->status
= ib_send_cm_rtu(cm_id
, NULL
, 0);
1225 case IB_CM_REJ_RECEIVED
:
1226 printk(KERN_DEBUG PFX
"REJ received\n");
1229 srp_cm_rej_handler(cm_id
, event
, target
);
1232 case IB_CM_DREQ_RECEIVED
:
1233 printk(KERN_WARNING PFX
"DREQ received - connection closed\n");
1234 if (ib_send_cm_drep(cm_id
, NULL
, 0))
1235 printk(KERN_ERR PFX
"Sending CM DREP failed\n");
1238 case IB_CM_TIMEWAIT_EXIT
:
1239 printk(KERN_ERR PFX
"connection closed\n");
1245 case IB_CM_MRA_RECEIVED
:
1246 case IB_CM_DREQ_ERROR
:
1247 case IB_CM_DREP_RECEIVED
:
1251 printk(KERN_WARNING PFX
"Unhandled CM event %d\n", event
->event
);
1256 complete(&target
->done
);
1263 static int srp_send_tsk_mgmt(struct srp_target_port
*target
,
1264 struct srp_request
*req
, u8 func
)
1267 struct srp_tsk_mgmt
*tsk_mgmt
;
1269 spin_lock_irq(target
->scsi_host
->host_lock
);
1271 if (target
->state
== SRP_TARGET_DEAD
||
1272 target
->state
== SRP_TARGET_REMOVED
) {
1273 req
->scmnd
->result
= DID_BAD_TARGET
<< 16;
1277 init_completion(&req
->done
);
1279 iu
= __srp_get_tx_iu(target
);
1284 memset(tsk_mgmt
, 0, sizeof *tsk_mgmt
);
1286 tsk_mgmt
->opcode
= SRP_TSK_MGMT
;
1287 tsk_mgmt
->lun
= cpu_to_be64((u64
) req
->scmnd
->device
->lun
<< 48);
1288 tsk_mgmt
->tag
= req
->index
| SRP_TAG_TSK_MGMT
;
1289 tsk_mgmt
->tsk_mgmt_func
= func
;
1290 tsk_mgmt
->task_tag
= req
->index
;
1292 if (__srp_post_send(target
, iu
, sizeof *tsk_mgmt
))
1297 spin_unlock_irq(target
->scsi_host
->host_lock
);
1299 if (!wait_for_completion_timeout(&req
->done
,
1300 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS
)))
1306 spin_unlock_irq(target
->scsi_host
->host_lock
);
1310 static int srp_find_req(struct srp_target_port
*target
,
1311 struct scsi_cmnd
*scmnd
,
1312 struct srp_request
**req
)
1314 if (scmnd
->host_scribble
== (void *) -1L)
1317 *req
= &target
->req_ring
[(long) scmnd
->host_scribble
];
1322 static int srp_abort(struct scsi_cmnd
*scmnd
)
1324 struct srp_target_port
*target
= host_to_target(scmnd
->device
->host
);
1325 struct srp_request
*req
;
1328 printk(KERN_ERR
"SRP abort called\n");
1330 if (target
->qp_in_error
)
1332 if (srp_find_req(target
, scmnd
, &req
))
1334 if (srp_send_tsk_mgmt(target
, req
, SRP_TSK_ABORT_TASK
))
1337 spin_lock_irq(target
->scsi_host
->host_lock
);
1339 if (req
->cmd_done
) {
1340 srp_remove_req(target
, req
);
1341 scmnd
->scsi_done(scmnd
);
1342 } else if (!req
->tsk_status
) {
1343 srp_remove_req(target
, req
);
1344 scmnd
->result
= DID_ABORT
<< 16;
1348 spin_unlock_irq(target
->scsi_host
->host_lock
);
1353 static int srp_reset_device(struct scsi_cmnd
*scmnd
)
1355 struct srp_target_port
*target
= host_to_target(scmnd
->device
->host
);
1356 struct srp_request
*req
, *tmp
;
1358 printk(KERN_ERR
"SRP reset_device called\n");
1360 if (target
->qp_in_error
)
1362 if (srp_find_req(target
, scmnd
, &req
))
1364 if (srp_send_tsk_mgmt(target
, req
, SRP_TSK_LUN_RESET
))
1366 if (req
->tsk_status
)
1369 spin_lock_irq(target
->scsi_host
->host_lock
);
1371 list_for_each_entry_safe(req
, tmp
, &target
->req_queue
, list
)
1372 if (req
->scmnd
->device
== scmnd
->device
)
1373 srp_reset_req(target
, req
);
1375 spin_unlock_irq(target
->scsi_host
->host_lock
);
1380 static int srp_reset_host(struct scsi_cmnd
*scmnd
)
1382 struct srp_target_port
*target
= host_to_target(scmnd
->device
->host
);
1385 printk(KERN_ERR PFX
"SRP reset_host called\n");
1387 if (!srp_reconnect_target(target
))
1393 static ssize_t
show_id_ext(struct class_device
*cdev
, char *buf
)
1395 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1397 if (target
->state
== SRP_TARGET_DEAD
||
1398 target
->state
== SRP_TARGET_REMOVED
)
1401 return sprintf(buf
, "0x%016llx\n",
1402 (unsigned long long) be64_to_cpu(target
->id_ext
));
1405 static ssize_t
show_ioc_guid(struct class_device
*cdev
, char *buf
)
1407 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1409 if (target
->state
== SRP_TARGET_DEAD
||
1410 target
->state
== SRP_TARGET_REMOVED
)
1413 return sprintf(buf
, "0x%016llx\n",
1414 (unsigned long long) be64_to_cpu(target
->ioc_guid
));
1417 static ssize_t
show_service_id(struct class_device
*cdev
, char *buf
)
1419 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1421 if (target
->state
== SRP_TARGET_DEAD
||
1422 target
->state
== SRP_TARGET_REMOVED
)
1425 return sprintf(buf
, "0x%016llx\n",
1426 (unsigned long long) be64_to_cpu(target
->service_id
));
1429 static ssize_t
show_pkey(struct class_device
*cdev
, char *buf
)
1431 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1433 if (target
->state
== SRP_TARGET_DEAD
||
1434 target
->state
== SRP_TARGET_REMOVED
)
1437 return sprintf(buf
, "0x%04x\n", be16_to_cpu(target
->path
.pkey
));
1440 static ssize_t
show_dgid(struct class_device
*cdev
, char *buf
)
1442 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1444 if (target
->state
== SRP_TARGET_DEAD
||
1445 target
->state
== SRP_TARGET_REMOVED
)
1448 return sprintf(buf
, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1449 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[0]),
1450 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[1]),
1451 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[2]),
1452 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[3]),
1453 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[4]),
1454 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[5]),
1455 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[6]),
1456 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[7]));
1459 static ssize_t
show_orig_dgid(struct class_device
*cdev
, char *buf
)
1461 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1463 if (target
->state
== SRP_TARGET_DEAD
||
1464 target
->state
== SRP_TARGET_REMOVED
)
1467 return sprintf(buf
, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1468 be16_to_cpu(target
->orig_dgid
[0]),
1469 be16_to_cpu(target
->orig_dgid
[1]),
1470 be16_to_cpu(target
->orig_dgid
[2]),
1471 be16_to_cpu(target
->orig_dgid
[3]),
1472 be16_to_cpu(target
->orig_dgid
[4]),
1473 be16_to_cpu(target
->orig_dgid
[5]),
1474 be16_to_cpu(target
->orig_dgid
[6]),
1475 be16_to_cpu(target
->orig_dgid
[7]));
1478 static ssize_t
show_zero_req_lim(struct class_device
*cdev
, char *buf
)
1480 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1482 if (target
->state
== SRP_TARGET_DEAD
||
1483 target
->state
== SRP_TARGET_REMOVED
)
1486 return sprintf(buf
, "%d\n", target
->zero_req_lim
);
1489 static ssize_t
show_local_ib_port(struct class_device
*cdev
, char *buf
)
1491 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1493 return sprintf(buf
, "%d\n", target
->srp_host
->port
);
1496 static ssize_t
show_local_ib_device(struct class_device
*cdev
, char *buf
)
1498 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1500 return sprintf(buf
, "%s\n", target
->srp_host
->dev
->dev
->name
);
1503 static CLASS_DEVICE_ATTR(id_ext
, S_IRUGO
, show_id_ext
, NULL
);
1504 static CLASS_DEVICE_ATTR(ioc_guid
, S_IRUGO
, show_ioc_guid
, NULL
);
1505 static CLASS_DEVICE_ATTR(service_id
, S_IRUGO
, show_service_id
, NULL
);
1506 static CLASS_DEVICE_ATTR(pkey
, S_IRUGO
, show_pkey
, NULL
);
1507 static CLASS_DEVICE_ATTR(dgid
, S_IRUGO
, show_dgid
, NULL
);
1508 static CLASS_DEVICE_ATTR(orig_dgid
, S_IRUGO
, show_orig_dgid
, NULL
);
1509 static CLASS_DEVICE_ATTR(zero_req_lim
, S_IRUGO
, show_zero_req_lim
, NULL
);
1510 static CLASS_DEVICE_ATTR(local_ib_port
, S_IRUGO
, show_local_ib_port
, NULL
);
1511 static CLASS_DEVICE_ATTR(local_ib_device
, S_IRUGO
, show_local_ib_device
, NULL
);
1513 static struct class_device_attribute
*srp_host_attrs
[] = {
1514 &class_device_attr_id_ext
,
1515 &class_device_attr_ioc_guid
,
1516 &class_device_attr_service_id
,
1517 &class_device_attr_pkey
,
1518 &class_device_attr_dgid
,
1519 &class_device_attr_orig_dgid
,
1520 &class_device_attr_zero_req_lim
,
1521 &class_device_attr_local_ib_port
,
1522 &class_device_attr_local_ib_device
,
1526 static struct scsi_host_template srp_template
= {
1527 .module
= THIS_MODULE
,
1528 .name
= "InfiniBand SRP initiator",
1529 .proc_name
= DRV_NAME
,
1530 .info
= srp_target_info
,
1531 .queuecommand
= srp_queuecommand
,
1532 .eh_abort_handler
= srp_abort
,
1533 .eh_device_reset_handler
= srp_reset_device
,
1534 .eh_host_reset_handler
= srp_reset_host
,
1535 .can_queue
= SRP_SQ_SIZE
,
1537 .cmd_per_lun
= SRP_SQ_SIZE
,
1538 .use_clustering
= ENABLE_CLUSTERING
,
1539 .shost_attrs
= srp_host_attrs
1542 static int srp_add_target(struct srp_host
*host
, struct srp_target_port
*target
)
1544 sprintf(target
->target_name
, "SRP.T10:%016llX",
1545 (unsigned long long) be64_to_cpu(target
->id_ext
));
1547 if (scsi_add_host(target
->scsi_host
, host
->dev
->dev
->dma_device
))
1550 spin_lock(&host
->target_lock
);
1551 list_add_tail(&target
->list
, &host
->target_list
);
1552 spin_unlock(&host
->target_lock
);
1554 target
->state
= SRP_TARGET_LIVE
;
1556 scsi_scan_target(&target
->scsi_host
->shost_gendev
,
1557 0, target
->scsi_id
, SCAN_WILD_CARD
, 0);
1562 static void srp_release_class_dev(struct class_device
*class_dev
)
1564 struct srp_host
*host
=
1565 container_of(class_dev
, struct srp_host
, class_dev
);
1567 complete(&host
->released
);
1570 static struct class srp_class
= {
1571 .name
= "infiniband_srp",
1572 .release
= srp_release_class_dev
1576 * Target ports are added by writing
1578 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1579 * pkey=<P_Key>,service_id=<service ID>
1581 * to the add_target sysfs attribute.
1585 SRP_OPT_ID_EXT
= 1 << 0,
1586 SRP_OPT_IOC_GUID
= 1 << 1,
1587 SRP_OPT_DGID
= 1 << 2,
1588 SRP_OPT_PKEY
= 1 << 3,
1589 SRP_OPT_SERVICE_ID
= 1 << 4,
1590 SRP_OPT_MAX_SECT
= 1 << 5,
1591 SRP_OPT_MAX_CMD_PER_LUN
= 1 << 6,
1592 SRP_OPT_IO_CLASS
= 1 << 7,
1593 SRP_OPT_INITIATOR_EXT
= 1 << 8,
1594 SRP_OPT_ALL
= (SRP_OPT_ID_EXT
|
1598 SRP_OPT_SERVICE_ID
),
1601 static match_table_t srp_opt_tokens
= {
1602 { SRP_OPT_ID_EXT
, "id_ext=%s" },
1603 { SRP_OPT_IOC_GUID
, "ioc_guid=%s" },
1604 { SRP_OPT_DGID
, "dgid=%s" },
1605 { SRP_OPT_PKEY
, "pkey=%x" },
1606 { SRP_OPT_SERVICE_ID
, "service_id=%s" },
1607 { SRP_OPT_MAX_SECT
, "max_sect=%d" },
1608 { SRP_OPT_MAX_CMD_PER_LUN
, "max_cmd_per_lun=%d" },
1609 { SRP_OPT_IO_CLASS
, "io_class=%x" },
1610 { SRP_OPT_INITIATOR_EXT
, "initiator_ext=%s" },
1611 { SRP_OPT_ERR
, NULL
}
1614 static int srp_parse_options(const char *buf
, struct srp_target_port
*target
)
1616 char *options
, *sep_opt
;
1619 substring_t args
[MAX_OPT_ARGS
];
1625 options
= kstrdup(buf
, GFP_KERNEL
);
1630 while ((p
= strsep(&sep_opt
, ",")) != NULL
) {
1634 token
= match_token(p
, srp_opt_tokens
, args
);
1638 case SRP_OPT_ID_EXT
:
1639 p
= match_strdup(args
);
1644 target
->id_ext
= cpu_to_be64(simple_strtoull(p
, NULL
, 16));
1648 case SRP_OPT_IOC_GUID
:
1649 p
= match_strdup(args
);
1654 target
->ioc_guid
= cpu_to_be64(simple_strtoull(p
, NULL
, 16));
1659 p
= match_strdup(args
);
1664 if (strlen(p
) != 32) {
1665 printk(KERN_WARNING PFX
"bad dest GID parameter '%s'\n", p
);
1670 for (i
= 0; i
< 16; ++i
) {
1671 strlcpy(dgid
, p
+ i
* 2, 3);
1672 target
->path
.dgid
.raw
[i
] = simple_strtoul(dgid
, NULL
, 16);
1675 memcpy(target
->orig_dgid
, target
->path
.dgid
.raw
, 16);
1679 if (match_hex(args
, &token
)) {
1680 printk(KERN_WARNING PFX
"bad P_Key parameter '%s'\n", p
);
1683 target
->path
.pkey
= cpu_to_be16(token
);
1686 case SRP_OPT_SERVICE_ID
:
1687 p
= match_strdup(args
);
1692 target
->service_id
= cpu_to_be64(simple_strtoull(p
, NULL
, 16));
1696 case SRP_OPT_MAX_SECT
:
1697 if (match_int(args
, &token
)) {
1698 printk(KERN_WARNING PFX
"bad max sect parameter '%s'\n", p
);
1701 target
->scsi_host
->max_sectors
= token
;
1704 case SRP_OPT_MAX_CMD_PER_LUN
:
1705 if (match_int(args
, &token
)) {
1706 printk(KERN_WARNING PFX
"bad max cmd_per_lun parameter '%s'\n", p
);
1709 target
->scsi_host
->cmd_per_lun
= min(token
, SRP_SQ_SIZE
);
1712 case SRP_OPT_IO_CLASS
:
1713 if (match_hex(args
, &token
)) {
1714 printk(KERN_WARNING PFX
"bad IO class parameter '%s' \n", p
);
1717 if (token
!= SRP_REV10_IB_IO_CLASS
&&
1718 token
!= SRP_REV16A_IB_IO_CLASS
) {
1719 printk(KERN_WARNING PFX
"unknown IO class parameter value"
1720 " %x specified (use %x or %x).\n",
1721 token
, SRP_REV10_IB_IO_CLASS
, SRP_REV16A_IB_IO_CLASS
);
1724 target
->io_class
= token
;
1727 case SRP_OPT_INITIATOR_EXT
:
1728 p
= match_strdup(args
);
1733 target
->initiator_ext
= cpu_to_be64(simple_strtoull(p
, NULL
, 16));
1738 printk(KERN_WARNING PFX
"unknown parameter or missing value "
1739 "'%s' in target creation request\n", p
);
1744 if ((opt_mask
& SRP_OPT_ALL
) == SRP_OPT_ALL
)
1747 for (i
= 0; i
< ARRAY_SIZE(srp_opt_tokens
); ++i
)
1748 if ((srp_opt_tokens
[i
].token
& SRP_OPT_ALL
) &&
1749 !(srp_opt_tokens
[i
].token
& opt_mask
))
1750 printk(KERN_WARNING PFX
"target creation request is "
1751 "missing parameter '%s'\n",
1752 srp_opt_tokens
[i
].pattern
);
1759 static ssize_t
srp_create_target(struct class_device
*class_dev
,
1760 const char *buf
, size_t count
)
1762 struct srp_host
*host
=
1763 container_of(class_dev
, struct srp_host
, class_dev
);
1764 struct Scsi_Host
*target_host
;
1765 struct srp_target_port
*target
;
1769 target_host
= scsi_host_alloc(&srp_template
,
1770 sizeof (struct srp_target_port
));
1774 target_host
->max_lun
= SRP_MAX_LUN
;
1775 target_host
->max_cmd_len
= sizeof ((struct srp_cmd
*) (void *) 0L)->cdb
;
1777 target
= host_to_target(target_host
);
1779 target
->io_class
= SRP_REV16A_IB_IO_CLASS
;
1780 target
->scsi_host
= target_host
;
1781 target
->srp_host
= host
;
1783 INIT_LIST_HEAD(&target
->free_reqs
);
1784 INIT_LIST_HEAD(&target
->req_queue
);
1785 for (i
= 0; i
< SRP_SQ_SIZE
; ++i
) {
1786 target
->req_ring
[i
].index
= i
;
1787 list_add_tail(&target
->req_ring
[i
].list
, &target
->free_reqs
);
1790 ret
= srp_parse_options(buf
, target
);
1794 ib_get_cached_gid(host
->dev
->dev
, host
->port
, 0, &target
->path
.sgid
);
1796 printk(KERN_DEBUG PFX
"new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1797 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1798 (unsigned long long) be64_to_cpu(target
->id_ext
),
1799 (unsigned long long) be64_to_cpu(target
->ioc_guid
),
1800 be16_to_cpu(target
->path
.pkey
),
1801 (unsigned long long) be64_to_cpu(target
->service_id
),
1802 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[0]),
1803 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[2]),
1804 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[4]),
1805 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[6]),
1806 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[8]),
1807 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[10]),
1808 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[12]),
1809 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[14]));
1811 ret
= srp_create_target_ib(target
);
1815 target
->cm_id
= ib_create_cm_id(host
->dev
->dev
, srp_cm_handler
, target
);
1816 if (IS_ERR(target
->cm_id
)) {
1817 ret
= PTR_ERR(target
->cm_id
);
1821 target
->qp_in_error
= 0;
1822 ret
= srp_connect_target(target
);
1824 printk(KERN_ERR PFX
"Connection failed\n");
1828 ret
= srp_add_target(host
, target
);
1830 goto err_disconnect
;
1835 srp_disconnect_target(target
);
1838 ib_destroy_cm_id(target
->cm_id
);
1841 srp_free_target_ib(target
);
1844 scsi_host_put(target_host
);
1849 static CLASS_DEVICE_ATTR(add_target
, S_IWUSR
, NULL
, srp_create_target
);
1851 static ssize_t
show_ibdev(struct class_device
*class_dev
, char *buf
)
1853 struct srp_host
*host
=
1854 container_of(class_dev
, struct srp_host
, class_dev
);
1856 return sprintf(buf
, "%s\n", host
->dev
->dev
->name
);
1859 static CLASS_DEVICE_ATTR(ibdev
, S_IRUGO
, show_ibdev
, NULL
);
1861 static ssize_t
show_port(struct class_device
*class_dev
, char *buf
)
1863 struct srp_host
*host
=
1864 container_of(class_dev
, struct srp_host
, class_dev
);
1866 return sprintf(buf
, "%d\n", host
->port
);
1869 static CLASS_DEVICE_ATTR(port
, S_IRUGO
, show_port
, NULL
);
1871 static struct srp_host
*srp_add_port(struct srp_device
*device
, u8 port
)
1873 struct srp_host
*host
;
1875 host
= kzalloc(sizeof *host
, GFP_KERNEL
);
1879 INIT_LIST_HEAD(&host
->target_list
);
1880 spin_lock_init(&host
->target_lock
);
1881 init_completion(&host
->released
);
1885 host
->class_dev
.class = &srp_class
;
1886 host
->class_dev
.dev
= device
->dev
->dma_device
;
1887 snprintf(host
->class_dev
.class_id
, BUS_ID_SIZE
, "srp-%s-%d",
1888 device
->dev
->name
, port
);
1890 if (class_device_register(&host
->class_dev
))
1892 if (class_device_create_file(&host
->class_dev
, &class_device_attr_add_target
))
1894 if (class_device_create_file(&host
->class_dev
, &class_device_attr_ibdev
))
1896 if (class_device_create_file(&host
->class_dev
, &class_device_attr_port
))
1902 class_device_unregister(&host
->class_dev
);
1910 static void srp_add_one(struct ib_device
*device
)
1912 struct srp_device
*srp_dev
;
1913 struct ib_device_attr
*dev_attr
;
1914 struct ib_fmr_pool_param fmr_param
;
1915 struct srp_host
*host
;
1918 dev_attr
= kmalloc(sizeof *dev_attr
, GFP_KERNEL
);
1922 if (ib_query_device(device
, dev_attr
)) {
1923 printk(KERN_WARNING PFX
"Query device failed for %s\n",
1928 srp_dev
= kmalloc(sizeof *srp_dev
, GFP_KERNEL
);
1933 * Use the smallest page size supported by the HCA, down to a
1934 * minimum of 512 bytes (which is the smallest sector that a
1935 * SCSI command will ever carry).
1937 srp_dev
->fmr_page_shift
= max(9, ffs(dev_attr
->page_size_cap
) - 1);
1938 srp_dev
->fmr_page_size
= 1 << srp_dev
->fmr_page_shift
;
1939 srp_dev
->fmr_page_mask
= ~((u64
) srp_dev
->fmr_page_size
- 1);
1941 INIT_LIST_HEAD(&srp_dev
->dev_list
);
1943 srp_dev
->dev
= device
;
1944 srp_dev
->pd
= ib_alloc_pd(device
);
1945 if (IS_ERR(srp_dev
->pd
))
1948 srp_dev
->mr
= ib_get_dma_mr(srp_dev
->pd
,
1949 IB_ACCESS_LOCAL_WRITE
|
1950 IB_ACCESS_REMOTE_READ
|
1951 IB_ACCESS_REMOTE_WRITE
);
1952 if (IS_ERR(srp_dev
->mr
))
1955 memset(&fmr_param
, 0, sizeof fmr_param
);
1956 fmr_param
.pool_size
= SRP_FMR_POOL_SIZE
;
1957 fmr_param
.dirty_watermark
= SRP_FMR_DIRTY_SIZE
;
1958 fmr_param
.cache
= 1;
1959 fmr_param
.max_pages_per_fmr
= SRP_FMR_SIZE
;
1960 fmr_param
.page_shift
= srp_dev
->fmr_page_shift
;
1961 fmr_param
.access
= (IB_ACCESS_LOCAL_WRITE
|
1962 IB_ACCESS_REMOTE_WRITE
|
1963 IB_ACCESS_REMOTE_READ
);
1965 srp_dev
->fmr_pool
= ib_create_fmr_pool(srp_dev
->pd
, &fmr_param
);
1966 if (IS_ERR(srp_dev
->fmr_pool
))
1967 srp_dev
->fmr_pool
= NULL
;
1969 if (device
->node_type
== RDMA_NODE_IB_SWITCH
) {
1974 e
= device
->phys_port_cnt
;
1977 for (p
= s
; p
<= e
; ++p
) {
1978 host
= srp_add_port(srp_dev
, p
);
1980 list_add_tail(&host
->list
, &srp_dev
->dev_list
);
1983 ib_set_client_data(device
, &srp_client
, srp_dev
);
1988 ib_dealloc_pd(srp_dev
->pd
);
1997 static void srp_remove_one(struct ib_device
*device
)
1999 struct srp_device
*srp_dev
;
2000 struct srp_host
*host
, *tmp_host
;
2001 LIST_HEAD(target_list
);
2002 struct srp_target_port
*target
, *tmp_target
;
2004 srp_dev
= ib_get_client_data(device
, &srp_client
);
2006 list_for_each_entry_safe(host
, tmp_host
, &srp_dev
->dev_list
, list
) {
2007 class_device_unregister(&host
->class_dev
);
2009 * Wait for the sysfs entry to go away, so that no new
2010 * target ports can be created.
2012 wait_for_completion(&host
->released
);
2015 * Mark all target ports as removed, so we stop queueing
2016 * commands and don't try to reconnect.
2018 spin_lock(&host
->target_lock
);
2019 list_for_each_entry(target
, &host
->target_list
, list
) {
2020 spin_lock_irq(target
->scsi_host
->host_lock
);
2021 target
->state
= SRP_TARGET_REMOVED
;
2022 spin_unlock_irq(target
->scsi_host
->host_lock
);
2024 spin_unlock(&host
->target_lock
);
2027 * Wait for any reconnection tasks that may have
2028 * started before we marked our target ports as
2029 * removed, and any target port removal tasks.
2031 flush_scheduled_work();
2033 list_for_each_entry_safe(target
, tmp_target
,
2034 &host
->target_list
, list
) {
2035 scsi_remove_host(target
->scsi_host
);
2036 srp_disconnect_target(target
);
2037 ib_destroy_cm_id(target
->cm_id
);
2038 srp_free_target_ib(target
);
2039 scsi_host_put(target
->scsi_host
);
2045 if (srp_dev
->fmr_pool
)
2046 ib_destroy_fmr_pool(srp_dev
->fmr_pool
);
2047 ib_dereg_mr(srp_dev
->mr
);
2048 ib_dealloc_pd(srp_dev
->pd
);
2053 static int __init
srp_init_module(void)
2057 srp_template
.sg_tablesize
= srp_sg_tablesize
;
2058 srp_max_iu_len
= (sizeof (struct srp_cmd
) +
2059 sizeof (struct srp_indirect_buf
) +
2060 srp_sg_tablesize
* 16);
2062 ret
= class_register(&srp_class
);
2064 printk(KERN_ERR PFX
"couldn't register class infiniband_srp\n");
2068 ib_sa_register_client(&srp_sa_client
);
2070 ret
= ib_register_client(&srp_client
);
2072 printk(KERN_ERR PFX
"couldn't register IB client\n");
2073 ib_sa_unregister_client(&srp_sa_client
);
2074 class_unregister(&srp_class
);
2081 static void __exit
srp_cleanup_module(void)
2083 ib_unregister_client(&srp_client
);
2084 ib_sa_unregister_client(&srp_sa_client
);
2085 class_unregister(&srp_class
);
2088 module_init(srp_init_module
);
2089 module_exit(srp_cleanup_module
);