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 const u8 topspin_oui
[3] = { 0x00, 0x05, 0xad };
80 static int mellanox_workarounds
= 1;
82 module_param(mellanox_workarounds
, int, 0444);
83 MODULE_PARM_DESC(mellanox_workarounds
,
84 "Enable workarounds for Mellanox SRP target bugs if != 0");
86 static const u8 mellanox_oui
[3] = { 0x00, 0x02, 0xc9 };
88 static void srp_add_one(struct ib_device
*device
);
89 static void srp_remove_one(struct ib_device
*device
);
90 static void srp_completion(struct ib_cq
*cq
, void *target_ptr
);
91 static int srp_cm_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*event
);
93 static struct ib_client srp_client
= {
96 .remove
= srp_remove_one
99 static inline struct srp_target_port
*host_to_target(struct Scsi_Host
*host
)
101 return (struct srp_target_port
*) host
->hostdata
;
104 static const char *srp_target_info(struct Scsi_Host
*host
)
106 return host_to_target(host
)->target_name
;
109 static struct srp_iu
*srp_alloc_iu(struct srp_host
*host
, size_t size
,
111 enum dma_data_direction direction
)
115 iu
= kmalloc(sizeof *iu
, gfp_mask
);
119 iu
->buf
= kzalloc(size
, gfp_mask
);
123 iu
->dma
= dma_map_single(host
->dev
->dev
->dma_device
,
124 iu
->buf
, size
, direction
);
125 if (dma_mapping_error(iu
->dma
))
129 iu
->direction
= direction
;
141 static void srp_free_iu(struct srp_host
*host
, struct srp_iu
*iu
)
146 dma_unmap_single(host
->dev
->dev
->dma_device
,
147 iu
->dma
, iu
->size
, iu
->direction
);
152 static void srp_qp_event(struct ib_event
*event
, void *context
)
154 printk(KERN_ERR PFX
"QP event %d\n", event
->event
);
157 static int srp_init_qp(struct srp_target_port
*target
,
160 struct ib_qp_attr
*attr
;
163 attr
= kmalloc(sizeof *attr
, GFP_KERNEL
);
167 ret
= ib_find_cached_pkey(target
->srp_host
->dev
->dev
,
168 target
->srp_host
->port
,
169 be16_to_cpu(target
->path
.pkey
),
174 attr
->qp_state
= IB_QPS_INIT
;
175 attr
->qp_access_flags
= (IB_ACCESS_REMOTE_READ
|
176 IB_ACCESS_REMOTE_WRITE
);
177 attr
->port_num
= target
->srp_host
->port
;
179 ret
= ib_modify_qp(qp
, attr
,
190 static int srp_create_target_ib(struct srp_target_port
*target
)
192 struct ib_qp_init_attr
*init_attr
;
195 init_attr
= kzalloc(sizeof *init_attr
, GFP_KERNEL
);
199 target
->cq
= ib_create_cq(target
->srp_host
->dev
->dev
, srp_completion
,
200 NULL
, target
, SRP_CQ_SIZE
);
201 if (IS_ERR(target
->cq
)) {
202 ret
= PTR_ERR(target
->cq
);
206 ib_req_notify_cq(target
->cq
, IB_CQ_NEXT_COMP
);
208 init_attr
->event_handler
= srp_qp_event
;
209 init_attr
->cap
.max_send_wr
= SRP_SQ_SIZE
;
210 init_attr
->cap
.max_recv_wr
= SRP_RQ_SIZE
;
211 init_attr
->cap
.max_recv_sge
= 1;
212 init_attr
->cap
.max_send_sge
= 1;
213 init_attr
->sq_sig_type
= IB_SIGNAL_ALL_WR
;
214 init_attr
->qp_type
= IB_QPT_RC
;
215 init_attr
->send_cq
= target
->cq
;
216 init_attr
->recv_cq
= target
->cq
;
218 target
->qp
= ib_create_qp(target
->srp_host
->dev
->pd
, init_attr
);
219 if (IS_ERR(target
->qp
)) {
220 ret
= PTR_ERR(target
->qp
);
221 ib_destroy_cq(target
->cq
);
225 ret
= srp_init_qp(target
, target
->qp
);
227 ib_destroy_qp(target
->qp
);
228 ib_destroy_cq(target
->cq
);
237 static void srp_free_target_ib(struct srp_target_port
*target
)
241 ib_destroy_qp(target
->qp
);
242 ib_destroy_cq(target
->cq
);
244 for (i
= 0; i
< SRP_RQ_SIZE
; ++i
)
245 srp_free_iu(target
->srp_host
, target
->rx_ring
[i
]);
246 for (i
= 0; i
< SRP_SQ_SIZE
+ 1; ++i
)
247 srp_free_iu(target
->srp_host
, target
->tx_ring
[i
]);
250 static void srp_path_rec_completion(int status
,
251 struct ib_sa_path_rec
*pathrec
,
254 struct srp_target_port
*target
= target_ptr
;
256 target
->status
= status
;
258 printk(KERN_ERR PFX
"Got failed path rec status %d\n", status
);
260 target
->path
= *pathrec
;
261 complete(&target
->done
);
264 static int srp_lookup_path(struct srp_target_port
*target
)
266 target
->path
.numb_path
= 1;
268 init_completion(&target
->done
);
270 target
->path_query_id
= ib_sa_path_rec_get(target
->srp_host
->dev
->dev
,
271 target
->srp_host
->port
,
273 IB_SA_PATH_REC_DGID
|
274 IB_SA_PATH_REC_SGID
|
275 IB_SA_PATH_REC_NUMB_PATH
|
277 SRP_PATH_REC_TIMEOUT_MS
,
279 srp_path_rec_completion
,
280 target
, &target
->path_query
);
281 if (target
->path_query_id
< 0)
282 return target
->path_query_id
;
284 wait_for_completion(&target
->done
);
286 if (target
->status
< 0)
287 printk(KERN_WARNING PFX
"Path record query failed\n");
289 return target
->status
;
292 static int srp_send_req(struct srp_target_port
*target
)
295 struct ib_cm_req_param param
;
296 struct srp_login_req priv
;
300 req
= kzalloc(sizeof *req
, GFP_KERNEL
);
304 req
->param
.primary_path
= &target
->path
;
305 req
->param
.alternate_path
= NULL
;
306 req
->param
.service_id
= target
->service_id
;
307 req
->param
.qp_num
= target
->qp
->qp_num
;
308 req
->param
.qp_type
= target
->qp
->qp_type
;
309 req
->param
.private_data
= &req
->priv
;
310 req
->param
.private_data_len
= sizeof req
->priv
;
311 req
->param
.flow_control
= 1;
313 get_random_bytes(&req
->param
.starting_psn
, 4);
314 req
->param
.starting_psn
&= 0xffffff;
317 * Pick some arbitrary defaults here; we could make these
318 * module parameters if anyone cared about setting them.
320 req
->param
.responder_resources
= 4;
321 req
->param
.remote_cm_response_timeout
= 20;
322 req
->param
.local_cm_response_timeout
= 20;
323 req
->param
.retry_count
= 7;
324 req
->param
.rnr_retry_count
= 7;
325 req
->param
.max_cm_retries
= 15;
327 req
->priv
.opcode
= SRP_LOGIN_REQ
;
329 req
->priv
.req_it_iu_len
= cpu_to_be32(srp_max_iu_len
);
330 req
->priv
.req_buf_fmt
= cpu_to_be16(SRP_BUF_FORMAT_DIRECT
|
331 SRP_BUF_FORMAT_INDIRECT
);
333 * In the published SRP specification (draft rev. 16a), the
334 * port identifier format is 8 bytes of ID extension followed
335 * by 8 bytes of GUID. Older drafts put the two halves in the
336 * opposite order, so that the GUID comes first.
338 * Targets conforming to these obsolete drafts can be
339 * recognized by the I/O Class they report.
341 if (target
->io_class
== SRP_REV10_IB_IO_CLASS
) {
342 memcpy(req
->priv
.initiator_port_id
,
343 target
->srp_host
->initiator_port_id
+ 8, 8);
344 memcpy(req
->priv
.initiator_port_id
+ 8,
345 target
->srp_host
->initiator_port_id
, 8);
346 memcpy(req
->priv
.target_port_id
, &target
->ioc_guid
, 8);
347 memcpy(req
->priv
.target_port_id
+ 8, &target
->id_ext
, 8);
349 memcpy(req
->priv
.initiator_port_id
,
350 target
->srp_host
->initiator_port_id
, 16);
351 memcpy(req
->priv
.target_port_id
, &target
->id_ext
, 8);
352 memcpy(req
->priv
.target_port_id
+ 8, &target
->ioc_guid
, 8);
356 * Topspin/Cisco SRP targets will reject our login unless we
357 * zero out the first 8 bytes of our initiator port ID. The
358 * second 8 bytes must be our local node GUID, but we always
361 if (topspin_workarounds
&& !memcmp(&target
->ioc_guid
, topspin_oui
, 3)) {
362 printk(KERN_DEBUG PFX
"Topspin/Cisco initiator port ID workaround "
363 "activated for target GUID %016llx\n",
364 (unsigned long long) be64_to_cpu(target
->ioc_guid
));
365 memset(req
->priv
.initiator_port_id
, 0, 8);
368 status
= ib_send_cm_req(target
->cm_id
, &req
->param
);
375 static void srp_disconnect_target(struct srp_target_port
*target
)
377 /* XXX should send SRP_I_LOGOUT request */
379 init_completion(&target
->done
);
380 if (ib_send_cm_dreq(target
->cm_id
, NULL
, 0)) {
381 printk(KERN_DEBUG PFX
"Sending CM DREQ failed\n");
384 wait_for_completion(&target
->done
);
387 static void srp_remove_work(void *target_ptr
)
389 struct srp_target_port
*target
= target_ptr
;
391 spin_lock_irq(target
->scsi_host
->host_lock
);
392 if (target
->state
!= SRP_TARGET_DEAD
) {
393 spin_unlock_irq(target
->scsi_host
->host_lock
);
396 target
->state
= SRP_TARGET_REMOVED
;
397 spin_unlock_irq(target
->scsi_host
->host_lock
);
399 spin_lock(&target
->srp_host
->target_lock
);
400 list_del(&target
->list
);
401 spin_unlock(&target
->srp_host
->target_lock
);
403 scsi_remove_host(target
->scsi_host
);
404 ib_destroy_cm_id(target
->cm_id
);
405 srp_free_target_ib(target
);
406 scsi_host_put(target
->scsi_host
);
409 static int srp_connect_target(struct srp_target_port
*target
)
413 ret
= srp_lookup_path(target
);
418 init_completion(&target
->done
);
419 ret
= srp_send_req(target
);
422 wait_for_completion(&target
->done
);
425 * The CM event handling code will set status to
426 * SRP_PORT_REDIRECT if we get a port redirect REJ
427 * back, or SRP_DLID_REDIRECT if we get a lid/qp
430 switch (target
->status
) {
434 case SRP_PORT_REDIRECT
:
435 ret
= srp_lookup_path(target
);
440 case SRP_DLID_REDIRECT
:
444 return target
->status
;
449 static void srp_unmap_data(struct scsi_cmnd
*scmnd
,
450 struct srp_target_port
*target
,
451 struct srp_request
*req
)
453 struct scatterlist
*scat
;
456 if (!scmnd
->request_buffer
||
457 (scmnd
->sc_data_direction
!= DMA_TO_DEVICE
&&
458 scmnd
->sc_data_direction
!= DMA_FROM_DEVICE
))
462 ib_fmr_pool_unmap(req
->fmr
);
467 * This handling of non-SG commands can be killed when the
468 * SCSI midlayer no longer generates non-SG commands.
470 if (likely(scmnd
->use_sg
)) {
471 nents
= scmnd
->use_sg
;
472 scat
= scmnd
->request_buffer
;
475 scat
= &req
->fake_sg
;
478 dma_unmap_sg(target
->srp_host
->dev
->dev
->dma_device
, scat
, nents
,
479 scmnd
->sc_data_direction
);
482 static void srp_remove_req(struct srp_target_port
*target
, struct srp_request
*req
)
484 srp_unmap_data(req
->scmnd
, target
, req
);
485 list_move_tail(&req
->list
, &target
->free_reqs
);
488 static void srp_reset_req(struct srp_target_port
*target
, struct srp_request
*req
)
490 req
->scmnd
->result
= DID_RESET
<< 16;
491 req
->scmnd
->scsi_done(req
->scmnd
);
492 srp_remove_req(target
, req
);
495 static int srp_reconnect_target(struct srp_target_port
*target
)
497 struct ib_cm_id
*new_cm_id
;
498 struct ib_qp_attr qp_attr
;
499 struct srp_request
*req
, *tmp
;
503 spin_lock_irq(target
->scsi_host
->host_lock
);
504 if (target
->state
!= SRP_TARGET_LIVE
) {
505 spin_unlock_irq(target
->scsi_host
->host_lock
);
508 target
->state
= SRP_TARGET_CONNECTING
;
509 spin_unlock_irq(target
->scsi_host
->host_lock
);
511 srp_disconnect_target(target
);
513 * Now get a new local CM ID so that we avoid confusing the
514 * target in case things are really fouled up.
516 new_cm_id
= ib_create_cm_id(target
->srp_host
->dev
->dev
,
517 srp_cm_handler
, target
);
518 if (IS_ERR(new_cm_id
)) {
519 ret
= PTR_ERR(new_cm_id
);
522 ib_destroy_cm_id(target
->cm_id
);
523 target
->cm_id
= new_cm_id
;
525 qp_attr
.qp_state
= IB_QPS_RESET
;
526 ret
= ib_modify_qp(target
->qp
, &qp_attr
, IB_QP_STATE
);
530 ret
= srp_init_qp(target
, target
->qp
);
534 while (ib_poll_cq(target
->cq
, 1, &wc
) > 0)
537 spin_lock_irq(target
->scsi_host
->host_lock
);
538 list_for_each_entry_safe(req
, tmp
, &target
->req_queue
, list
)
539 srp_reset_req(target
, req
);
540 spin_unlock_irq(target
->scsi_host
->host_lock
);
546 ret
= srp_connect_target(target
);
550 spin_lock_irq(target
->scsi_host
->host_lock
);
551 if (target
->state
== SRP_TARGET_CONNECTING
) {
553 target
->state
= SRP_TARGET_LIVE
;
556 spin_unlock_irq(target
->scsi_host
->host_lock
);
561 printk(KERN_ERR PFX
"reconnect failed (%d), removing target port.\n", ret
);
564 * We couldn't reconnect, so kill our target port off.
565 * However, we have to defer the real removal because we might
566 * be in the context of the SCSI error handler now, which
567 * would deadlock if we call scsi_remove_host().
569 spin_lock_irq(target
->scsi_host
->host_lock
);
570 if (target
->state
== SRP_TARGET_CONNECTING
) {
571 target
->state
= SRP_TARGET_DEAD
;
572 INIT_WORK(&target
->work
, srp_remove_work
, target
);
573 schedule_work(&target
->work
);
575 spin_unlock_irq(target
->scsi_host
->host_lock
);
580 static int srp_map_fmr(struct srp_target_port
*target
, struct scatterlist
*scat
,
581 int sg_cnt
, struct srp_request
*req
,
582 struct srp_direct_buf
*buf
)
590 struct srp_device
*dev
= target
->srp_host
->dev
;
595 if ((sg_dma_address(&scat
[0]) & ~dev
->fmr_page_mask
) &&
596 mellanox_workarounds
&& !memcmp(&target
->ioc_guid
, mellanox_oui
, 3))
600 for (i
= 0; i
< sg_cnt
; ++i
) {
601 if (sg_dma_address(&scat
[i
]) & ~dev
->fmr_page_mask
) {
607 if ((sg_dma_address(&scat
[i
]) + sg_dma_len(&scat
[i
])) &
608 ~dev
->fmr_page_mask
) {
615 len
+= sg_dma_len(&scat
[i
]);
618 page_cnt
+= len
>> dev
->fmr_page_shift
;
619 if (page_cnt
> SRP_FMR_SIZE
)
622 dma_pages
= kmalloc(sizeof (u64
) * page_cnt
, GFP_ATOMIC
);
627 for (i
= 0; i
< sg_cnt
; ++i
)
628 for (j
= 0; j
< sg_dma_len(&scat
[i
]); j
+= dev
->fmr_page_size
)
629 dma_pages
[page_cnt
++] =
630 (sg_dma_address(&scat
[i
]) & dev
->fmr_page_mask
) + j
;
632 req
->fmr
= ib_fmr_pool_map_phys(dev
->fmr_pool
,
633 dma_pages
, page_cnt
, io_addr
);
634 if (IS_ERR(req
->fmr
)) {
635 ret
= PTR_ERR(req
->fmr
);
640 buf
->va
= cpu_to_be64(sg_dma_address(&scat
[0]) & ~dev
->fmr_page_mask
);
641 buf
->key
= cpu_to_be32(req
->fmr
->fmr
->rkey
);
642 buf
->len
= cpu_to_be32(len
);
652 static int srp_map_data(struct scsi_cmnd
*scmnd
, struct srp_target_port
*target
,
653 struct srp_request
*req
)
655 struct scatterlist
*scat
;
656 struct srp_cmd
*cmd
= req
->cmd
->buf
;
657 int len
, nents
, count
;
658 u8 fmt
= SRP_DATA_DESC_DIRECT
;
660 if (!scmnd
->request_buffer
|| scmnd
->sc_data_direction
== DMA_NONE
)
661 return sizeof (struct srp_cmd
);
663 if (scmnd
->sc_data_direction
!= DMA_FROM_DEVICE
&&
664 scmnd
->sc_data_direction
!= DMA_TO_DEVICE
) {
665 printk(KERN_WARNING PFX
"Unhandled data direction %d\n",
666 scmnd
->sc_data_direction
);
671 * This handling of non-SG commands can be killed when the
672 * SCSI midlayer no longer generates non-SG commands.
674 if (likely(scmnd
->use_sg
)) {
675 nents
= scmnd
->use_sg
;
676 scat
= scmnd
->request_buffer
;
679 scat
= &req
->fake_sg
;
680 sg_init_one(scat
, scmnd
->request_buffer
, scmnd
->request_bufflen
);
683 count
= dma_map_sg(target
->srp_host
->dev
->dev
->dma_device
,
684 scat
, nents
, scmnd
->sc_data_direction
);
686 fmt
= SRP_DATA_DESC_DIRECT
;
687 len
= sizeof (struct srp_cmd
) + sizeof (struct srp_direct_buf
);
691 * The midlayer only generated a single gather/scatter
692 * entry, or DMA mapping coalesced everything to a
693 * single entry. So a direct descriptor along with
694 * the DMA MR suffices.
696 struct srp_direct_buf
*buf
= (void *) cmd
->add_data
;
698 buf
->va
= cpu_to_be64(sg_dma_address(scat
));
699 buf
->key
= cpu_to_be32(target
->srp_host
->dev
->mr
->rkey
);
700 buf
->len
= cpu_to_be32(sg_dma_len(scat
));
701 } else if (srp_map_fmr(target
, scat
, count
, req
,
702 (void *) cmd
->add_data
)) {
704 * FMR mapping failed, and the scatterlist has more
705 * than one entry. Generate an indirect memory
708 struct srp_indirect_buf
*buf
= (void *) cmd
->add_data
;
712 fmt
= SRP_DATA_DESC_INDIRECT
;
713 len
= sizeof (struct srp_cmd
) +
714 sizeof (struct srp_indirect_buf
) +
715 count
* sizeof (struct srp_direct_buf
);
717 for (i
= 0; i
< count
; ++i
) {
718 buf
->desc_list
[i
].va
=
719 cpu_to_be64(sg_dma_address(&scat
[i
]));
720 buf
->desc_list
[i
].key
=
721 cpu_to_be32(target
->srp_host
->dev
->mr
->rkey
);
722 buf
->desc_list
[i
].len
=
723 cpu_to_be32(sg_dma_len(&scat
[i
]));
724 datalen
+= sg_dma_len(&scat
[i
]);
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 scmnd
->resid
= be32_to_cpu(rsp
->data_out_res_cnt
);
787 else if (rsp
->flags
& (SRP_RSP_FLAG_DIOVER
| SRP_RSP_FLAG_DIUNDER
))
788 scmnd
->resid
= 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_reconnect_work(void *target_ptr
)
804 struct srp_target_port
*target
= target_ptr
;
806 srp_reconnect_target(target
);
809 static void srp_handle_recv(struct srp_target_port
*target
, struct ib_wc
*wc
)
814 iu
= target
->rx_ring
[wc
->wr_id
& ~SRP_OP_RECV
];
816 dma_sync_single_for_cpu(target
->srp_host
->dev
->dev
->dma_device
, iu
->dma
,
817 target
->max_ti_iu_len
, DMA_FROM_DEVICE
);
819 opcode
= *(u8
*) iu
->buf
;
824 printk(KERN_ERR PFX
"recv completion, opcode 0x%02x\n", opcode
);
826 for (i
= 0; i
< wc
->byte_len
; ++i
) {
828 printk(KERN_ERR
" [%02x] ", i
);
829 printk(" %02x", ((u8
*) iu
->buf
)[i
]);
830 if ((i
+ 1) % 8 == 0)
834 if (wc
->byte_len
% 8)
840 srp_process_rsp(target
, iu
->buf
);
844 /* XXX Handle target logout */
845 printk(KERN_WARNING PFX
"Got target logout request\n");
849 printk(KERN_WARNING PFX
"Unhandled SRP opcode 0x%02x\n", opcode
);
853 dma_sync_single_for_device(target
->srp_host
->dev
->dev
->dma_device
, iu
->dma
,
854 target
->max_ti_iu_len
, DMA_FROM_DEVICE
);
857 static void srp_completion(struct ib_cq
*cq
, void *target_ptr
)
859 struct srp_target_port
*target
= target_ptr
;
863 ib_req_notify_cq(cq
, IB_CQ_NEXT_COMP
);
864 while (ib_poll_cq(cq
, 1, &wc
) > 0) {
866 printk(KERN_ERR PFX
"failed %s status %d\n",
867 wc
.wr_id
& SRP_OP_RECV
? "receive" : "send",
869 spin_lock_irqsave(target
->scsi_host
->host_lock
, flags
);
870 if (target
->state
== SRP_TARGET_LIVE
)
871 schedule_work(&target
->work
);
872 spin_unlock_irqrestore(target
->scsi_host
->host_lock
, flags
);
876 if (wc
.wr_id
& SRP_OP_RECV
)
877 srp_handle_recv(target
, &wc
);
883 static int __srp_post_recv(struct srp_target_port
*target
)
887 struct ib_recv_wr wr
, *bad_wr
;
891 next
= target
->rx_head
& (SRP_RQ_SIZE
- 1);
892 wr
.wr_id
= next
| SRP_OP_RECV
;
893 iu
= target
->rx_ring
[next
];
896 list
.length
= iu
->size
;
897 list
.lkey
= target
->srp_host
->dev
->mr
->lkey
;
903 ret
= ib_post_recv(target
->qp
, &wr
, &bad_wr
);
910 static int srp_post_recv(struct srp_target_port
*target
)
915 spin_lock_irqsave(target
->scsi_host
->host_lock
, flags
);
916 ret
= __srp_post_recv(target
);
917 spin_unlock_irqrestore(target
->scsi_host
->host_lock
, flags
);
923 * Must be called with target->scsi_host->host_lock held to protect
924 * req_lim and tx_head. Lock cannot be dropped between call here and
925 * call to __srp_post_send().
927 static struct srp_iu
*__srp_get_tx_iu(struct srp_target_port
*target
)
929 if (target
->tx_head
- target
->tx_tail
>= SRP_SQ_SIZE
)
932 if (unlikely(target
->req_lim
< 1))
933 ++target
->zero_req_lim
;
935 return target
->tx_ring
[target
->tx_head
& SRP_SQ_SIZE
];
939 * Must be called with target->scsi_host->host_lock held to protect
940 * req_lim and tx_head.
942 static int __srp_post_send(struct srp_target_port
*target
,
943 struct srp_iu
*iu
, int len
)
946 struct ib_send_wr wr
, *bad_wr
;
951 list
.lkey
= target
->srp_host
->dev
->mr
->lkey
;
954 wr
.wr_id
= target
->tx_head
& SRP_SQ_SIZE
;
957 wr
.opcode
= IB_WR_SEND
;
958 wr
.send_flags
= IB_SEND_SIGNALED
;
960 ret
= ib_post_send(target
->qp
, &wr
, &bad_wr
);
970 static int srp_queuecommand(struct scsi_cmnd
*scmnd
,
971 void (*done
)(struct scsi_cmnd
*))
973 struct srp_target_port
*target
= host_to_target(scmnd
->device
->host
);
974 struct srp_request
*req
;
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 dma_sync_single_for_cpu(target
->srp_host
->dev
->dev
->dma_device
, iu
->dma
,
994 srp_max_iu_len
, DMA_TO_DEVICE
);
996 req
= list_entry(target
->free_reqs
.next
, struct srp_request
, list
);
998 scmnd
->scsi_done
= done
;
1000 scmnd
->host_scribble
= (void *) (long) req
->index
;
1003 memset(cmd
, 0, sizeof *cmd
);
1005 cmd
->opcode
= SRP_CMD
;
1006 cmd
->lun
= cpu_to_be64((u64
) scmnd
->device
->lun
<< 48);
1007 cmd
->tag
= req
->index
;
1008 memcpy(cmd
->cdb
, scmnd
->cmnd
, scmnd
->cmd_len
);
1013 req
->tsk_mgmt
= NULL
;
1015 len
= srp_map_data(scmnd
, target
, req
);
1017 printk(KERN_ERR PFX
"Failed to map data\n");
1021 if (__srp_post_recv(target
)) {
1022 printk(KERN_ERR PFX
"Recv failed\n");
1026 dma_sync_single_for_device(target
->srp_host
->dev
->dev
->dma_device
, iu
->dma
,
1027 srp_max_iu_len
, DMA_TO_DEVICE
);
1029 if (__srp_post_send(target
, iu
, len
)) {
1030 printk(KERN_ERR PFX
"Send failed\n");
1034 list_move_tail(&req
->list
, &target
->req_queue
);
1039 srp_unmap_data(scmnd
, target
, req
);
1042 return SCSI_MLQUEUE_HOST_BUSY
;
1045 static int srp_alloc_iu_bufs(struct srp_target_port
*target
)
1049 for (i
= 0; i
< SRP_RQ_SIZE
; ++i
) {
1050 target
->rx_ring
[i
] = srp_alloc_iu(target
->srp_host
,
1051 target
->max_ti_iu_len
,
1052 GFP_KERNEL
, DMA_FROM_DEVICE
);
1053 if (!target
->rx_ring
[i
])
1057 for (i
= 0; i
< SRP_SQ_SIZE
+ 1; ++i
) {
1058 target
->tx_ring
[i
] = srp_alloc_iu(target
->srp_host
,
1060 GFP_KERNEL
, DMA_TO_DEVICE
);
1061 if (!target
->tx_ring
[i
])
1068 for (i
= 0; i
< SRP_RQ_SIZE
; ++i
) {
1069 srp_free_iu(target
->srp_host
, target
->rx_ring
[i
]);
1070 target
->rx_ring
[i
] = NULL
;
1073 for (i
= 0; i
< SRP_SQ_SIZE
+ 1; ++i
) {
1074 srp_free_iu(target
->srp_host
, target
->tx_ring
[i
]);
1075 target
->tx_ring
[i
] = NULL
;
1081 static void srp_cm_rej_handler(struct ib_cm_id
*cm_id
,
1082 struct ib_cm_event
*event
,
1083 struct srp_target_port
*target
)
1085 struct ib_class_port_info
*cpi
;
1088 switch (event
->param
.rej_rcvd
.reason
) {
1089 case IB_CM_REJ_PORT_CM_REDIRECT
:
1090 cpi
= event
->param
.rej_rcvd
.ari
;
1091 target
->path
.dlid
= cpi
->redirect_lid
;
1092 target
->path
.pkey
= cpi
->redirect_pkey
;
1093 cm_id
->remote_cm_qpn
= be32_to_cpu(cpi
->redirect_qp
) & 0x00ffffff;
1094 memcpy(target
->path
.dgid
.raw
, cpi
->redirect_gid
, 16);
1096 target
->status
= target
->path
.dlid
?
1097 SRP_DLID_REDIRECT
: SRP_PORT_REDIRECT
;
1100 case IB_CM_REJ_PORT_REDIRECT
:
1101 if (topspin_workarounds
&&
1102 !memcmp(&target
->ioc_guid
, topspin_oui
, 3)) {
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 target
->status
= srp_alloc_iu_bufs(target
);
1189 qp_attr
= kmalloc(sizeof *qp_attr
, GFP_KERNEL
);
1191 target
->status
= -ENOMEM
;
1195 qp_attr
->qp_state
= IB_QPS_RTR
;
1196 target
->status
= ib_cm_init_qp_attr(cm_id
, qp_attr
, &attr_mask
);
1200 target
->status
= ib_modify_qp(target
->qp
, qp_attr
, attr_mask
);
1204 target
->status
= srp_post_recv(target
);
1208 qp_attr
->qp_state
= IB_QPS_RTS
;
1209 target
->status
= ib_cm_init_qp_attr(cm_id
, qp_attr
, &attr_mask
);
1213 target
->status
= ib_modify_qp(target
->qp
, qp_attr
, attr_mask
);
1217 target
->status
= ib_send_cm_rtu(cm_id
, NULL
, 0);
1223 case IB_CM_REJ_RECEIVED
:
1224 printk(KERN_DEBUG PFX
"REJ received\n");
1227 srp_cm_rej_handler(cm_id
, event
, target
);
1230 case IB_CM_DREQ_RECEIVED
:
1231 printk(KERN_WARNING PFX
"DREQ received - connection closed\n");
1232 if (ib_send_cm_drep(cm_id
, NULL
, 0))
1233 printk(KERN_ERR PFX
"Sending CM DREP failed\n");
1236 case IB_CM_TIMEWAIT_EXIT
:
1237 printk(KERN_ERR PFX
"connection closed\n");
1243 case IB_CM_MRA_RECEIVED
:
1244 case IB_CM_DREQ_ERROR
:
1245 case IB_CM_DREP_RECEIVED
:
1249 printk(KERN_WARNING PFX
"Unhandled CM event %d\n", event
->event
);
1254 complete(&target
->done
);
1261 static int srp_send_tsk_mgmt(struct srp_target_port
*target
,
1262 struct srp_request
*req
, u8 func
)
1265 struct srp_tsk_mgmt
*tsk_mgmt
;
1267 spin_lock_irq(target
->scsi_host
->host_lock
);
1269 if (target
->state
== SRP_TARGET_DEAD
||
1270 target
->state
== SRP_TARGET_REMOVED
) {
1271 req
->scmnd
->result
= DID_BAD_TARGET
<< 16;
1275 init_completion(&req
->done
);
1277 iu
= __srp_get_tx_iu(target
);
1282 memset(tsk_mgmt
, 0, sizeof *tsk_mgmt
);
1284 tsk_mgmt
->opcode
= SRP_TSK_MGMT
;
1285 tsk_mgmt
->lun
= cpu_to_be64((u64
) req
->scmnd
->device
->lun
<< 48);
1286 tsk_mgmt
->tag
= req
->index
| SRP_TAG_TSK_MGMT
;
1287 tsk_mgmt
->tsk_mgmt_func
= func
;
1288 tsk_mgmt
->task_tag
= req
->index
;
1290 if (__srp_post_send(target
, iu
, sizeof *tsk_mgmt
))
1295 spin_unlock_irq(target
->scsi_host
->host_lock
);
1297 if (!wait_for_completion_timeout(&req
->done
,
1298 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS
)))
1304 spin_unlock_irq(target
->scsi_host
->host_lock
);
1308 static int srp_find_req(struct srp_target_port
*target
,
1309 struct scsi_cmnd
*scmnd
,
1310 struct srp_request
**req
)
1312 if (scmnd
->host_scribble
== (void *) -1L)
1315 *req
= &target
->req_ring
[(long) scmnd
->host_scribble
];
1320 static int srp_abort(struct scsi_cmnd
*scmnd
)
1322 struct srp_target_port
*target
= host_to_target(scmnd
->device
->host
);
1323 struct srp_request
*req
;
1326 printk(KERN_ERR
"SRP abort called\n");
1328 if (srp_find_req(target
, scmnd
, &req
))
1330 if (srp_send_tsk_mgmt(target
, req
, SRP_TSK_ABORT_TASK
))
1333 spin_lock_irq(target
->scsi_host
->host_lock
);
1335 if (req
->cmd_done
) {
1336 srp_remove_req(target
, req
);
1337 scmnd
->scsi_done(scmnd
);
1338 } else if (!req
->tsk_status
) {
1339 srp_remove_req(target
, req
);
1340 scmnd
->result
= DID_ABORT
<< 16;
1344 spin_unlock_irq(target
->scsi_host
->host_lock
);
1349 static int srp_reset_device(struct scsi_cmnd
*scmnd
)
1351 struct srp_target_port
*target
= host_to_target(scmnd
->device
->host
);
1352 struct srp_request
*req
, *tmp
;
1354 printk(KERN_ERR
"SRP reset_device called\n");
1356 if (srp_find_req(target
, scmnd
, &req
))
1358 if (srp_send_tsk_mgmt(target
, req
, SRP_TSK_LUN_RESET
))
1360 if (req
->tsk_status
)
1363 spin_lock_irq(target
->scsi_host
->host_lock
);
1365 list_for_each_entry_safe(req
, tmp
, &target
->req_queue
, list
)
1366 if (req
->scmnd
->device
== scmnd
->device
)
1367 srp_reset_req(target
, req
);
1369 spin_unlock_irq(target
->scsi_host
->host_lock
);
1374 static int srp_reset_host(struct scsi_cmnd
*scmnd
)
1376 struct srp_target_port
*target
= host_to_target(scmnd
->device
->host
);
1379 printk(KERN_ERR PFX
"SRP reset_host called\n");
1381 if (!srp_reconnect_target(target
))
1387 static ssize_t
show_id_ext(struct class_device
*cdev
, char *buf
)
1389 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1391 if (target
->state
== SRP_TARGET_DEAD
||
1392 target
->state
== SRP_TARGET_REMOVED
)
1395 return sprintf(buf
, "0x%016llx\n",
1396 (unsigned long long) be64_to_cpu(target
->id_ext
));
1399 static ssize_t
show_ioc_guid(struct class_device
*cdev
, char *buf
)
1401 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1403 if (target
->state
== SRP_TARGET_DEAD
||
1404 target
->state
== SRP_TARGET_REMOVED
)
1407 return sprintf(buf
, "0x%016llx\n",
1408 (unsigned long long) be64_to_cpu(target
->ioc_guid
));
1411 static ssize_t
show_service_id(struct class_device
*cdev
, char *buf
)
1413 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1415 if (target
->state
== SRP_TARGET_DEAD
||
1416 target
->state
== SRP_TARGET_REMOVED
)
1419 return sprintf(buf
, "0x%016llx\n",
1420 (unsigned long long) be64_to_cpu(target
->service_id
));
1423 static ssize_t
show_pkey(struct class_device
*cdev
, char *buf
)
1425 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1427 if (target
->state
== SRP_TARGET_DEAD
||
1428 target
->state
== SRP_TARGET_REMOVED
)
1431 return sprintf(buf
, "0x%04x\n", be16_to_cpu(target
->path
.pkey
));
1434 static ssize_t
show_dgid(struct class_device
*cdev
, char *buf
)
1436 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1438 if (target
->state
== SRP_TARGET_DEAD
||
1439 target
->state
== SRP_TARGET_REMOVED
)
1442 return sprintf(buf
, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1443 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[0]),
1444 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[1]),
1445 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[2]),
1446 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[3]),
1447 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[4]),
1448 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[5]),
1449 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[6]),
1450 be16_to_cpu(((__be16
*) target
->path
.dgid
.raw
)[7]));
1453 static ssize_t
show_zero_req_lim(struct class_device
*cdev
, char *buf
)
1455 struct srp_target_port
*target
= host_to_target(class_to_shost(cdev
));
1457 if (target
->state
== SRP_TARGET_DEAD
||
1458 target
->state
== SRP_TARGET_REMOVED
)
1461 return sprintf(buf
, "%d\n", target
->zero_req_lim
);
1464 static CLASS_DEVICE_ATTR(id_ext
, S_IRUGO
, show_id_ext
, NULL
);
1465 static CLASS_DEVICE_ATTR(ioc_guid
, S_IRUGO
, show_ioc_guid
, NULL
);
1466 static CLASS_DEVICE_ATTR(service_id
, S_IRUGO
, show_service_id
, NULL
);
1467 static CLASS_DEVICE_ATTR(pkey
, S_IRUGO
, show_pkey
, NULL
);
1468 static CLASS_DEVICE_ATTR(dgid
, S_IRUGO
, show_dgid
, NULL
);
1469 static CLASS_DEVICE_ATTR(zero_req_lim
, S_IRUGO
, show_zero_req_lim
, NULL
);
1471 static struct class_device_attribute
*srp_host_attrs
[] = {
1472 &class_device_attr_id_ext
,
1473 &class_device_attr_ioc_guid
,
1474 &class_device_attr_service_id
,
1475 &class_device_attr_pkey
,
1476 &class_device_attr_dgid
,
1477 &class_device_attr_zero_req_lim
,
1481 static struct scsi_host_template srp_template
= {
1482 .module
= THIS_MODULE
,
1484 .info
= srp_target_info
,
1485 .queuecommand
= srp_queuecommand
,
1486 .eh_abort_handler
= srp_abort
,
1487 .eh_device_reset_handler
= srp_reset_device
,
1488 .eh_host_reset_handler
= srp_reset_host
,
1489 .can_queue
= SRP_SQ_SIZE
,
1491 .cmd_per_lun
= SRP_SQ_SIZE
,
1492 .use_clustering
= ENABLE_CLUSTERING
,
1493 .shost_attrs
= srp_host_attrs
1496 static int srp_add_target(struct srp_host
*host
, struct srp_target_port
*target
)
1498 sprintf(target
->target_name
, "SRP.T10:%016llX",
1499 (unsigned long long) be64_to_cpu(target
->id_ext
));
1501 if (scsi_add_host(target
->scsi_host
, host
->dev
->dev
->dma_device
))
1504 spin_lock(&host
->target_lock
);
1505 list_add_tail(&target
->list
, &host
->target_list
);
1506 spin_unlock(&host
->target_lock
);
1508 target
->state
= SRP_TARGET_LIVE
;
1510 scsi_scan_target(&target
->scsi_host
->shost_gendev
,
1511 0, target
->scsi_id
, SCAN_WILD_CARD
, 0);
1516 static void srp_release_class_dev(struct class_device
*class_dev
)
1518 struct srp_host
*host
=
1519 container_of(class_dev
, struct srp_host
, class_dev
);
1521 complete(&host
->released
);
1524 static struct class srp_class
= {
1525 .name
= "infiniband_srp",
1526 .release
= srp_release_class_dev
1530 * Target ports are added by writing
1532 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1533 * pkey=<P_Key>,service_id=<service ID>
1535 * to the add_target sysfs attribute.
1539 SRP_OPT_ID_EXT
= 1 << 0,
1540 SRP_OPT_IOC_GUID
= 1 << 1,
1541 SRP_OPT_DGID
= 1 << 2,
1542 SRP_OPT_PKEY
= 1 << 3,
1543 SRP_OPT_SERVICE_ID
= 1 << 4,
1544 SRP_OPT_MAX_SECT
= 1 << 5,
1545 SRP_OPT_MAX_CMD_PER_LUN
= 1 << 6,
1546 SRP_OPT_IO_CLASS
= 1 << 7,
1547 SRP_OPT_ALL
= (SRP_OPT_ID_EXT
|
1551 SRP_OPT_SERVICE_ID
),
1554 static match_table_t srp_opt_tokens
= {
1555 { SRP_OPT_ID_EXT
, "id_ext=%s" },
1556 { SRP_OPT_IOC_GUID
, "ioc_guid=%s" },
1557 { SRP_OPT_DGID
, "dgid=%s" },
1558 { SRP_OPT_PKEY
, "pkey=%x" },
1559 { SRP_OPT_SERVICE_ID
, "service_id=%s" },
1560 { SRP_OPT_MAX_SECT
, "max_sect=%d" },
1561 { SRP_OPT_MAX_CMD_PER_LUN
, "max_cmd_per_lun=%d" },
1562 { SRP_OPT_IO_CLASS
, "io_class=%x" },
1563 { SRP_OPT_ERR
, NULL
}
1566 static int srp_parse_options(const char *buf
, struct srp_target_port
*target
)
1568 char *options
, *sep_opt
;
1571 substring_t args
[MAX_OPT_ARGS
];
1577 options
= kstrdup(buf
, GFP_KERNEL
);
1582 while ((p
= strsep(&sep_opt
, ",")) != NULL
) {
1586 token
= match_token(p
, srp_opt_tokens
, args
);
1590 case SRP_OPT_ID_EXT
:
1591 p
= match_strdup(args
);
1592 target
->id_ext
= cpu_to_be64(simple_strtoull(p
, NULL
, 16));
1596 case SRP_OPT_IOC_GUID
:
1597 p
= match_strdup(args
);
1598 target
->ioc_guid
= cpu_to_be64(simple_strtoull(p
, NULL
, 16));
1603 p
= match_strdup(args
);
1604 if (strlen(p
) != 32) {
1605 printk(KERN_WARNING PFX
"bad dest GID parameter '%s'\n", p
);
1610 for (i
= 0; i
< 16; ++i
) {
1611 strlcpy(dgid
, p
+ i
* 2, 3);
1612 target
->path
.dgid
.raw
[i
] = simple_strtoul(dgid
, NULL
, 16);
1618 if (match_hex(args
, &token
)) {
1619 printk(KERN_WARNING PFX
"bad P_Key parameter '%s'\n", p
);
1622 target
->path
.pkey
= cpu_to_be16(token
);
1625 case SRP_OPT_SERVICE_ID
:
1626 p
= match_strdup(args
);
1627 target
->service_id
= cpu_to_be64(simple_strtoull(p
, NULL
, 16));
1631 case SRP_OPT_MAX_SECT
:
1632 if (match_int(args
, &token
)) {
1633 printk(KERN_WARNING PFX
"bad max sect parameter '%s'\n", p
);
1636 target
->scsi_host
->max_sectors
= token
;
1639 case SRP_OPT_MAX_CMD_PER_LUN
:
1640 if (match_int(args
, &token
)) {
1641 printk(KERN_WARNING PFX
"bad max cmd_per_lun parameter '%s'\n", p
);
1644 target
->scsi_host
->cmd_per_lun
= min(token
, SRP_SQ_SIZE
);
1647 case SRP_OPT_IO_CLASS
:
1648 if (match_hex(args
, &token
)) {
1649 printk(KERN_WARNING PFX
"bad IO class parameter '%s' \n", p
);
1652 if (token
!= SRP_REV10_IB_IO_CLASS
&&
1653 token
!= SRP_REV16A_IB_IO_CLASS
) {
1654 printk(KERN_WARNING PFX
"unknown IO class parameter value"
1655 " %x specified (use %x or %x).\n",
1656 token
, SRP_REV10_IB_IO_CLASS
, SRP_REV16A_IB_IO_CLASS
);
1659 target
->io_class
= token
;
1663 printk(KERN_WARNING PFX
"unknown parameter or missing value "
1664 "'%s' in target creation request\n", p
);
1669 if ((opt_mask
& SRP_OPT_ALL
) == SRP_OPT_ALL
)
1672 for (i
= 0; i
< ARRAY_SIZE(srp_opt_tokens
); ++i
)
1673 if ((srp_opt_tokens
[i
].token
& SRP_OPT_ALL
) &&
1674 !(srp_opt_tokens
[i
].token
& opt_mask
))
1675 printk(KERN_WARNING PFX
"target creation request is "
1676 "missing parameter '%s'\n",
1677 srp_opt_tokens
[i
].pattern
);
1684 static ssize_t
srp_create_target(struct class_device
*class_dev
,
1685 const char *buf
, size_t count
)
1687 struct srp_host
*host
=
1688 container_of(class_dev
, struct srp_host
, class_dev
);
1689 struct Scsi_Host
*target_host
;
1690 struct srp_target_port
*target
;
1694 target_host
= scsi_host_alloc(&srp_template
,
1695 sizeof (struct srp_target_port
));
1699 target_host
->max_lun
= SRP_MAX_LUN
;
1701 target
= host_to_target(target_host
);
1702 memset(target
, 0, sizeof *target
);
1704 target
->io_class
= SRP_REV16A_IB_IO_CLASS
;
1705 target
->scsi_host
= target_host
;
1706 target
->srp_host
= host
;
1708 INIT_WORK(&target
->work
, srp_reconnect_work
, target
);
1710 INIT_LIST_HEAD(&target
->free_reqs
);
1711 INIT_LIST_HEAD(&target
->req_queue
);
1712 for (i
= 0; i
< SRP_SQ_SIZE
; ++i
) {
1713 target
->req_ring
[i
].index
= i
;
1714 list_add_tail(&target
->req_ring
[i
].list
, &target
->free_reqs
);
1717 ret
= srp_parse_options(buf
, target
);
1721 ib_get_cached_gid(host
->dev
->dev
, host
->port
, 0, &target
->path
.sgid
);
1723 printk(KERN_DEBUG PFX
"new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1724 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1725 (unsigned long long) be64_to_cpu(target
->id_ext
),
1726 (unsigned long long) be64_to_cpu(target
->ioc_guid
),
1727 be16_to_cpu(target
->path
.pkey
),
1728 (unsigned long long) be64_to_cpu(target
->service_id
),
1729 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[0]),
1730 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[2]),
1731 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[4]),
1732 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[6]),
1733 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[8]),
1734 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[10]),
1735 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[12]),
1736 (int) be16_to_cpu(*(__be16
*) &target
->path
.dgid
.raw
[14]));
1738 ret
= srp_create_target_ib(target
);
1742 target
->cm_id
= ib_create_cm_id(host
->dev
->dev
, srp_cm_handler
, target
);
1743 if (IS_ERR(target
->cm_id
)) {
1744 ret
= PTR_ERR(target
->cm_id
);
1748 ret
= srp_connect_target(target
);
1750 printk(KERN_ERR PFX
"Connection failed\n");
1754 ret
= srp_add_target(host
, target
);
1756 goto err_disconnect
;
1761 srp_disconnect_target(target
);
1764 ib_destroy_cm_id(target
->cm_id
);
1767 srp_free_target_ib(target
);
1770 scsi_host_put(target_host
);
1775 static CLASS_DEVICE_ATTR(add_target
, S_IWUSR
, NULL
, srp_create_target
);
1777 static ssize_t
show_ibdev(struct class_device
*class_dev
, char *buf
)
1779 struct srp_host
*host
=
1780 container_of(class_dev
, struct srp_host
, class_dev
);
1782 return sprintf(buf
, "%s\n", host
->dev
->dev
->name
);
1785 static CLASS_DEVICE_ATTR(ibdev
, S_IRUGO
, show_ibdev
, NULL
);
1787 static ssize_t
show_port(struct class_device
*class_dev
, char *buf
)
1789 struct srp_host
*host
=
1790 container_of(class_dev
, struct srp_host
, class_dev
);
1792 return sprintf(buf
, "%d\n", host
->port
);
1795 static CLASS_DEVICE_ATTR(port
, S_IRUGO
, show_port
, NULL
);
1797 static struct srp_host
*srp_add_port(struct srp_device
*device
, u8 port
)
1799 struct srp_host
*host
;
1801 host
= kzalloc(sizeof *host
, GFP_KERNEL
);
1805 INIT_LIST_HEAD(&host
->target_list
);
1806 spin_lock_init(&host
->target_lock
);
1807 init_completion(&host
->released
);
1811 host
->initiator_port_id
[7] = port
;
1812 memcpy(host
->initiator_port_id
+ 8, &device
->dev
->node_guid
, 8);
1814 host
->class_dev
.class = &srp_class
;
1815 host
->class_dev
.dev
= device
->dev
->dma_device
;
1816 snprintf(host
->class_dev
.class_id
, BUS_ID_SIZE
, "srp-%s-%d",
1817 device
->dev
->name
, port
);
1819 if (class_device_register(&host
->class_dev
))
1821 if (class_device_create_file(&host
->class_dev
, &class_device_attr_add_target
))
1823 if (class_device_create_file(&host
->class_dev
, &class_device_attr_ibdev
))
1825 if (class_device_create_file(&host
->class_dev
, &class_device_attr_port
))
1831 class_device_unregister(&host
->class_dev
);
1839 static void srp_add_one(struct ib_device
*device
)
1841 struct srp_device
*srp_dev
;
1842 struct ib_device_attr
*dev_attr
;
1843 struct ib_fmr_pool_param fmr_param
;
1844 struct srp_host
*host
;
1847 dev_attr
= kmalloc(sizeof *dev_attr
, GFP_KERNEL
);
1851 if (ib_query_device(device
, dev_attr
)) {
1852 printk(KERN_WARNING PFX
"Query device failed for %s\n",
1857 srp_dev
= kmalloc(sizeof *srp_dev
, GFP_KERNEL
);
1862 * Use the smallest page size supported by the HCA, down to a
1863 * minimum of 512 bytes (which is the smallest sector that a
1864 * SCSI command will ever carry).
1866 srp_dev
->fmr_page_shift
= max(9, ffs(dev_attr
->page_size_cap
) - 1);
1867 srp_dev
->fmr_page_size
= 1 << srp_dev
->fmr_page_shift
;
1868 srp_dev
->fmr_page_mask
= ~((unsigned long) srp_dev
->fmr_page_size
- 1);
1870 INIT_LIST_HEAD(&srp_dev
->dev_list
);
1872 srp_dev
->dev
= device
;
1873 srp_dev
->pd
= ib_alloc_pd(device
);
1874 if (IS_ERR(srp_dev
->pd
))
1877 srp_dev
->mr
= ib_get_dma_mr(srp_dev
->pd
,
1878 IB_ACCESS_LOCAL_WRITE
|
1879 IB_ACCESS_REMOTE_READ
|
1880 IB_ACCESS_REMOTE_WRITE
);
1881 if (IS_ERR(srp_dev
->mr
))
1884 memset(&fmr_param
, 0, sizeof fmr_param
);
1885 fmr_param
.pool_size
= SRP_FMR_POOL_SIZE
;
1886 fmr_param
.dirty_watermark
= SRP_FMR_DIRTY_SIZE
;
1887 fmr_param
.cache
= 1;
1888 fmr_param
.max_pages_per_fmr
= SRP_FMR_SIZE
;
1889 fmr_param
.page_shift
= srp_dev
->fmr_page_shift
;
1890 fmr_param
.access
= (IB_ACCESS_LOCAL_WRITE
|
1891 IB_ACCESS_REMOTE_WRITE
|
1892 IB_ACCESS_REMOTE_READ
);
1894 srp_dev
->fmr_pool
= ib_create_fmr_pool(srp_dev
->pd
, &fmr_param
);
1895 if (IS_ERR(srp_dev
->fmr_pool
))
1896 srp_dev
->fmr_pool
= NULL
;
1898 if (device
->node_type
== IB_NODE_SWITCH
) {
1903 e
= device
->phys_port_cnt
;
1906 for (p
= s
; p
<= e
; ++p
) {
1907 host
= srp_add_port(srp_dev
, p
);
1909 list_add_tail(&host
->list
, &srp_dev
->dev_list
);
1912 ib_set_client_data(device
, &srp_client
, srp_dev
);
1917 ib_dealloc_pd(srp_dev
->pd
);
1926 static void srp_remove_one(struct ib_device
*device
)
1928 struct srp_device
*srp_dev
;
1929 struct srp_host
*host
, *tmp_host
;
1930 LIST_HEAD(target_list
);
1931 struct srp_target_port
*target
, *tmp_target
;
1933 srp_dev
= ib_get_client_data(device
, &srp_client
);
1935 list_for_each_entry_safe(host
, tmp_host
, &srp_dev
->dev_list
, list
) {
1936 class_device_unregister(&host
->class_dev
);
1938 * Wait for the sysfs entry to go away, so that no new
1939 * target ports can be created.
1941 wait_for_completion(&host
->released
);
1944 * Mark all target ports as removed, so we stop queueing
1945 * commands and don't try to reconnect.
1947 spin_lock(&host
->target_lock
);
1948 list_for_each_entry(target
, &host
->target_list
, list
) {
1949 spin_lock_irq(target
->scsi_host
->host_lock
);
1950 target
->state
= SRP_TARGET_REMOVED
;
1951 spin_unlock_irq(target
->scsi_host
->host_lock
);
1953 spin_unlock(&host
->target_lock
);
1956 * Wait for any reconnection tasks that may have
1957 * started before we marked our target ports as
1958 * removed, and any target port removal tasks.
1960 flush_scheduled_work();
1962 list_for_each_entry_safe(target
, tmp_target
,
1963 &host
->target_list
, list
) {
1964 scsi_remove_host(target
->scsi_host
);
1965 srp_disconnect_target(target
);
1966 ib_destroy_cm_id(target
->cm_id
);
1967 srp_free_target_ib(target
);
1968 scsi_host_put(target
->scsi_host
);
1974 if (srp_dev
->fmr_pool
)
1975 ib_destroy_fmr_pool(srp_dev
->fmr_pool
);
1976 ib_dereg_mr(srp_dev
->mr
);
1977 ib_dealloc_pd(srp_dev
->pd
);
1982 static int __init
srp_init_module(void)
1986 srp_template
.sg_tablesize
= srp_sg_tablesize
;
1987 srp_max_iu_len
= (sizeof (struct srp_cmd
) +
1988 sizeof (struct srp_indirect_buf
) +
1989 srp_sg_tablesize
* 16);
1991 ret
= class_register(&srp_class
);
1993 printk(KERN_ERR PFX
"couldn't register class infiniband_srp\n");
1997 ret
= ib_register_client(&srp_client
);
1999 printk(KERN_ERR PFX
"couldn't register IB client\n");
2000 class_unregister(&srp_class
);
2007 static void __exit
srp_cleanup_module(void)
2009 ib_unregister_client(&srp_client
);
2010 class_unregister(&srp_class
);
2013 module_init(srp_init_module
);
2014 module_exit(srp_cleanup_module
);