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
33 #define pr_fmt(fmt) PFX fmt
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 <linux/atomic.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_device.h>
48 #include <scsi/scsi_dbg.h>
50 #include <scsi/scsi_transport_srp.h>
54 #define DRV_NAME "ib_srp"
55 #define PFX DRV_NAME ": "
56 #define DRV_VERSION "0.2"
57 #define DRV_RELDATE "November 1, 2005"
59 MODULE_AUTHOR("Roland Dreier");
60 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
61 "v" DRV_VERSION
" (" DRV_RELDATE
")");
62 MODULE_LICENSE("Dual BSD/GPL");
64 static unsigned int srp_sg_tablesize
;
65 static unsigned int cmd_sg_entries
;
66 static unsigned int indirect_sg_entries
;
67 static bool allow_ext_sg
;
68 static int topspin_workarounds
= 1;
70 module_param(srp_sg_tablesize
, uint
, 0444);
71 MODULE_PARM_DESC(srp_sg_tablesize
, "Deprecated name for cmd_sg_entries");
73 module_param(cmd_sg_entries
, uint
, 0444);
74 MODULE_PARM_DESC(cmd_sg_entries
,
75 "Default number of gather/scatter entries in the SRP command (default is 12, max 255)");
77 module_param(indirect_sg_entries
, uint
, 0444);
78 MODULE_PARM_DESC(indirect_sg_entries
,
79 "Default max number of gather/scatter entries (default is 12, max is " __stringify(SCSI_MAX_SG_CHAIN_SEGMENTS
) ")");
81 module_param(allow_ext_sg
, bool, 0444);
82 MODULE_PARM_DESC(allow_ext_sg
,
83 "Default behavior when there are more than cmd_sg_entries S/G entries after mapping; fails the request when false (default false)");
85 module_param(topspin_workarounds
, int, 0444);
86 MODULE_PARM_DESC(topspin_workarounds
,
87 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
89 static void srp_add_one(struct ib_device
*device
);
90 static void srp_remove_one(struct ib_device
*device
);
91 static void srp_recv_completion(struct ib_cq
*cq
, void *target_ptr
);
92 static void srp_send_completion(struct ib_cq
*cq
, void *target_ptr
);
93 static int srp_cm_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*event
);
95 static struct scsi_transport_template
*ib_srp_transport_template
;
97 static struct ib_client srp_client
= {
100 .remove
= srp_remove_one
103 static struct ib_sa_client srp_sa_client
;
105 static inline struct srp_target_port
*host_to_target(struct Scsi_Host
*host
)
107 return (struct srp_target_port
*) host
->hostdata
;
110 static const char *srp_target_info(struct Scsi_Host
*host
)
112 return host_to_target(host
)->target_name
;
115 static int srp_target_is_topspin(struct srp_target_port
*target
)
117 static const u8 topspin_oui
[3] = { 0x00, 0x05, 0xad };
118 static const u8 cisco_oui
[3] = { 0x00, 0x1b, 0x0d };
120 return topspin_workarounds
&&
121 (!memcmp(&target
->ioc_guid
, topspin_oui
, sizeof topspin_oui
) ||
122 !memcmp(&target
->ioc_guid
, cisco_oui
, sizeof cisco_oui
));
125 static struct srp_iu
*srp_alloc_iu(struct srp_host
*host
, size_t size
,
127 enum dma_data_direction direction
)
131 iu
= kmalloc(sizeof *iu
, gfp_mask
);
135 iu
->buf
= kzalloc(size
, gfp_mask
);
139 iu
->dma
= ib_dma_map_single(host
->srp_dev
->dev
, iu
->buf
, size
,
141 if (ib_dma_mapping_error(host
->srp_dev
->dev
, iu
->dma
))
145 iu
->direction
= direction
;
157 static void srp_free_iu(struct srp_host
*host
, struct srp_iu
*iu
)
162 ib_dma_unmap_single(host
->srp_dev
->dev
, iu
->dma
, iu
->size
,
168 static void srp_qp_event(struct ib_event
*event
, void *context
)
170 pr_debug("QP event %d\n", event
->event
);
173 static int srp_init_qp(struct srp_target_port
*target
,
176 struct ib_qp_attr
*attr
;
179 attr
= kmalloc(sizeof *attr
, GFP_KERNEL
);
183 ret
= ib_find_pkey(target
->srp_host
->srp_dev
->dev
,
184 target
->srp_host
->port
,
185 be16_to_cpu(target
->path
.pkey
),
190 attr
->qp_state
= IB_QPS_INIT
;
191 attr
->qp_access_flags
= (IB_ACCESS_REMOTE_READ
|
192 IB_ACCESS_REMOTE_WRITE
);
193 attr
->port_num
= target
->srp_host
->port
;
195 ret
= ib_modify_qp(qp
, attr
,
206 static int srp_new_cm_id(struct srp_target_port
*target
)
208 struct ib_cm_id
*new_cm_id
;
210 new_cm_id
= ib_create_cm_id(target
->srp_host
->srp_dev
->dev
,
211 srp_cm_handler
, target
);
212 if (IS_ERR(new_cm_id
))
213 return PTR_ERR(new_cm_id
);
216 ib_destroy_cm_id(target
->cm_id
);
217 target
->cm_id
= new_cm_id
;
222 static int srp_create_target_ib(struct srp_target_port
*target
)
224 struct ib_qp_init_attr
*init_attr
;
225 struct ib_cq
*recv_cq
, *send_cq
;
229 init_attr
= kzalloc(sizeof *init_attr
, GFP_KERNEL
);
233 recv_cq
= ib_create_cq(target
->srp_host
->srp_dev
->dev
,
234 srp_recv_completion
, NULL
, target
, SRP_RQ_SIZE
, 0);
235 if (IS_ERR(recv_cq
)) {
236 ret
= PTR_ERR(recv_cq
);
240 send_cq
= ib_create_cq(target
->srp_host
->srp_dev
->dev
,
241 srp_send_completion
, NULL
, target
, SRP_SQ_SIZE
, 0);
242 if (IS_ERR(send_cq
)) {
243 ret
= PTR_ERR(send_cq
);
247 ib_req_notify_cq(recv_cq
, IB_CQ_NEXT_COMP
);
249 init_attr
->event_handler
= srp_qp_event
;
250 init_attr
->cap
.max_send_wr
= SRP_SQ_SIZE
;
251 init_attr
->cap
.max_recv_wr
= SRP_RQ_SIZE
;
252 init_attr
->cap
.max_recv_sge
= 1;
253 init_attr
->cap
.max_send_sge
= 1;
254 init_attr
->sq_sig_type
= IB_SIGNAL_ALL_WR
;
255 init_attr
->qp_type
= IB_QPT_RC
;
256 init_attr
->send_cq
= send_cq
;
257 init_attr
->recv_cq
= recv_cq
;
259 qp
= ib_create_qp(target
->srp_host
->srp_dev
->pd
, init_attr
);
265 ret
= srp_init_qp(target
, qp
);
270 ib_destroy_qp(target
->qp
);
272 ib_destroy_cq(target
->recv_cq
);
274 ib_destroy_cq(target
->send_cq
);
277 target
->recv_cq
= recv_cq
;
278 target
->send_cq
= send_cq
;
287 ib_destroy_cq(send_cq
);
290 ib_destroy_cq(recv_cq
);
297 static void srp_free_target_ib(struct srp_target_port
*target
)
301 ib_destroy_qp(target
->qp
);
302 ib_destroy_cq(target
->send_cq
);
303 ib_destroy_cq(target
->recv_cq
);
306 target
->send_cq
= target
->recv_cq
= NULL
;
308 for (i
= 0; i
< SRP_RQ_SIZE
; ++i
)
309 srp_free_iu(target
->srp_host
, target
->rx_ring
[i
]);
310 for (i
= 0; i
< SRP_SQ_SIZE
; ++i
)
311 srp_free_iu(target
->srp_host
, target
->tx_ring
[i
]);
314 static void srp_path_rec_completion(int status
,
315 struct ib_sa_path_rec
*pathrec
,
318 struct srp_target_port
*target
= target_ptr
;
320 target
->status
= status
;
322 shost_printk(KERN_ERR
, target
->scsi_host
,
323 PFX
"Got failed path rec status %d\n", status
);
325 target
->path
= *pathrec
;
326 complete(&target
->done
);
329 static int srp_lookup_path(struct srp_target_port
*target
)
331 target
->path
.numb_path
= 1;
333 init_completion(&target
->done
);
335 target
->path_query_id
= ib_sa_path_rec_get(&srp_sa_client
,
336 target
->srp_host
->srp_dev
->dev
,
337 target
->srp_host
->port
,
339 IB_SA_PATH_REC_SERVICE_ID
|
340 IB_SA_PATH_REC_DGID
|
341 IB_SA_PATH_REC_SGID
|
342 IB_SA_PATH_REC_NUMB_PATH
|
344 SRP_PATH_REC_TIMEOUT_MS
,
346 srp_path_rec_completion
,
347 target
, &target
->path_query
);
348 if (target
->path_query_id
< 0)
349 return target
->path_query_id
;
351 wait_for_completion(&target
->done
);
353 if (target
->status
< 0)
354 shost_printk(KERN_WARNING
, target
->scsi_host
,
355 PFX
"Path record query failed\n");
357 return target
->status
;
360 static int srp_send_req(struct srp_target_port
*target
)
363 struct ib_cm_req_param param
;
364 struct srp_login_req priv
;
368 req
= kzalloc(sizeof *req
, GFP_KERNEL
);
372 req
->param
.primary_path
= &target
->path
;
373 req
->param
.alternate_path
= NULL
;
374 req
->param
.service_id
= target
->service_id
;
375 req
->param
.qp_num
= target
->qp
->qp_num
;
376 req
->param
.qp_type
= target
->qp
->qp_type
;
377 req
->param
.private_data
= &req
->priv
;
378 req
->param
.private_data_len
= sizeof req
->priv
;
379 req
->param
.flow_control
= 1;
381 get_random_bytes(&req
->param
.starting_psn
, 4);
382 req
->param
.starting_psn
&= 0xffffff;
385 * Pick some arbitrary defaults here; we could make these
386 * module parameters if anyone cared about setting them.
388 req
->param
.responder_resources
= 4;
389 req
->param
.remote_cm_response_timeout
= 20;
390 req
->param
.local_cm_response_timeout
= 20;
391 req
->param
.retry_count
= 7;
392 req
->param
.rnr_retry_count
= 7;
393 req
->param
.max_cm_retries
= 15;
395 req
->priv
.opcode
= SRP_LOGIN_REQ
;
397 req
->priv
.req_it_iu_len
= cpu_to_be32(target
->max_iu_len
);
398 req
->priv
.req_buf_fmt
= cpu_to_be16(SRP_BUF_FORMAT_DIRECT
|
399 SRP_BUF_FORMAT_INDIRECT
);
401 * In the published SRP specification (draft rev. 16a), the
402 * port identifier format is 8 bytes of ID extension followed
403 * by 8 bytes of GUID. Older drafts put the two halves in the
404 * opposite order, so that the GUID comes first.
406 * Targets conforming to these obsolete drafts can be
407 * recognized by the I/O Class they report.
409 if (target
->io_class
== SRP_REV10_IB_IO_CLASS
) {
410 memcpy(req
->priv
.initiator_port_id
,
411 &target
->path
.sgid
.global
.interface_id
, 8);
412 memcpy(req
->priv
.initiator_port_id
+ 8,
413 &target
->initiator_ext
, 8);
414 memcpy(req
->priv
.target_port_id
, &target
->ioc_guid
, 8);
415 memcpy(req
->priv
.target_port_id
+ 8, &target
->id_ext
, 8);
417 memcpy(req
->priv
.initiator_port_id
,
418 &target
->initiator_ext
, 8);
419 memcpy(req
->priv
.initiator_port_id
+ 8,
420 &target
->path
.sgid
.global
.interface_id
, 8);
421 memcpy(req
->priv
.target_port_id
, &target
->id_ext
, 8);
422 memcpy(req
->priv
.target_port_id
+ 8, &target
->ioc_guid
, 8);
426 * Topspin/Cisco SRP targets will reject our login unless we
427 * zero out the first 8 bytes of our initiator port ID and set
428 * the second 8 bytes to the local node GUID.
430 if (srp_target_is_topspin(target
)) {
431 shost_printk(KERN_DEBUG
, target
->scsi_host
,
432 PFX
"Topspin/Cisco initiator port ID workaround "
433 "activated for target GUID %016llx\n",
434 (unsigned long long) be64_to_cpu(target
->ioc_guid
));
435 memset(req
->priv
.initiator_port_id
, 0, 8);
436 memcpy(req
->priv
.initiator_port_id
+ 8,
437 &target
->srp_host
->srp_dev
->dev
->node_guid
, 8);
440 status
= ib_send_cm_req(target
->cm_id
, &req
->param
);
447 static bool srp_queue_remove_work(struct srp_target_port
*target
)
449 bool changed
= false;
451 spin_lock_irq(&target
->lock
);
452 if (target
->state
!= SRP_TARGET_REMOVED
) {
453 target
->state
= SRP_TARGET_REMOVED
;
456 spin_unlock_irq(&target
->lock
);
459 queue_work(system_long_wq
, &target
->remove_work
);
464 static bool srp_change_conn_state(struct srp_target_port
*target
,
467 bool changed
= false;
469 spin_lock_irq(&target
->lock
);
470 if (target
->connected
!= connected
) {
471 target
->connected
= connected
;
474 spin_unlock_irq(&target
->lock
);
479 static void srp_disconnect_target(struct srp_target_port
*target
)
481 if (srp_change_conn_state(target
, false)) {
482 /* XXX should send SRP_I_LOGOUT request */
484 if (ib_send_cm_dreq(target
->cm_id
, NULL
, 0)) {
485 shost_printk(KERN_DEBUG
, target
->scsi_host
,
486 PFX
"Sending CM DREQ failed\n");
491 static void srp_free_req_data(struct srp_target_port
*target
)
493 struct ib_device
*ibdev
= target
->srp_host
->srp_dev
->dev
;
494 struct srp_request
*req
;
497 for (i
= 0, req
= target
->req_ring
; i
< SRP_CMD_SQ_SIZE
; ++i
, ++req
) {
498 kfree(req
->fmr_list
);
499 kfree(req
->map_page
);
500 if (req
->indirect_dma_addr
) {
501 ib_dma_unmap_single(ibdev
, req
->indirect_dma_addr
,
502 target
->indirect_size
,
505 kfree(req
->indirect_desc
);
510 * srp_del_scsi_host_attr() - Remove attributes defined in the host template.
511 * @shost: SCSI host whose attributes to remove from sysfs.
513 * Note: Any attributes defined in the host template and that did not exist
514 * before invocation of this function will be ignored.
516 static void srp_del_scsi_host_attr(struct Scsi_Host
*shost
)
518 struct device_attribute
**attr
;
520 for (attr
= shost
->hostt
->shost_attrs
; attr
&& *attr
; ++attr
)
521 device_remove_file(&shost
->shost_dev
, *attr
);
524 static void srp_remove_target(struct srp_target_port
*target
)
526 WARN_ON_ONCE(target
->state
!= SRP_TARGET_REMOVED
);
528 srp_del_scsi_host_attr(target
->scsi_host
);
529 srp_remove_host(target
->scsi_host
);
530 scsi_remove_host(target
->scsi_host
);
531 srp_disconnect_target(target
);
532 ib_destroy_cm_id(target
->cm_id
);
533 srp_free_target_ib(target
);
534 srp_free_req_data(target
);
535 scsi_host_put(target
->scsi_host
);
538 static void srp_remove_work(struct work_struct
*work
)
540 struct srp_target_port
*target
=
541 container_of(work
, struct srp_target_port
, remove_work
);
543 WARN_ON_ONCE(target
->state
!= SRP_TARGET_REMOVED
);
545 srp_remove_target(target
);
547 spin_lock(&target
->srp_host
->target_lock
);
548 list_del(&target
->list
);
549 spin_unlock(&target
->srp_host
->target_lock
);
552 static void srp_rport_delete(struct srp_rport
*rport
)
554 struct srp_target_port
*target
= rport
->lld_data
;
556 srp_queue_remove_work(target
);
559 static int srp_connect_target(struct srp_target_port
*target
)
564 WARN_ON_ONCE(target
->connected
);
566 target
->qp_in_error
= false;
568 ret
= srp_lookup_path(target
);
573 init_completion(&target
->done
);
574 ret
= srp_send_req(target
);
577 wait_for_completion(&target
->done
);
580 * The CM event handling code will set status to
581 * SRP_PORT_REDIRECT if we get a port redirect REJ
582 * back, or SRP_DLID_REDIRECT if we get a lid/qp
585 switch (target
->status
) {
587 srp_change_conn_state(target
, true);
590 case SRP_PORT_REDIRECT
:
591 ret
= srp_lookup_path(target
);
596 case SRP_DLID_REDIRECT
:
600 /* Our current CM id was stale, and is now in timewait.
601 * Try to reconnect with a new one.
603 if (!retries
-- || srp_new_cm_id(target
)) {
604 shost_printk(KERN_ERR
, target
->scsi_host
, PFX
605 "giving up on stale connection\n");
606 target
->status
= -ECONNRESET
;
607 return target
->status
;
610 shost_printk(KERN_ERR
, target
->scsi_host
, PFX
611 "retrying stale connection\n");
615 return target
->status
;
620 static void srp_unmap_data(struct scsi_cmnd
*scmnd
,
621 struct srp_target_port
*target
,
622 struct srp_request
*req
)
624 struct ib_device
*ibdev
= target
->srp_host
->srp_dev
->dev
;
625 struct ib_pool_fmr
**pfmr
;
627 if (!scsi_sglist(scmnd
) ||
628 (scmnd
->sc_data_direction
!= DMA_TO_DEVICE
&&
629 scmnd
->sc_data_direction
!= DMA_FROM_DEVICE
))
632 pfmr
= req
->fmr_list
;
634 ib_fmr_pool_unmap(*pfmr
++);
636 ib_dma_unmap_sg(ibdev
, scsi_sglist(scmnd
), scsi_sg_count(scmnd
),
637 scmnd
->sc_data_direction
);
641 * srp_claim_req - Take ownership of the scmnd associated with a request.
642 * @target: SRP target port.
644 * @scmnd: If NULL, take ownership of @req->scmnd. If not NULL, only take
645 * ownership of @req->scmnd if it equals @scmnd.
648 * Either NULL or a pointer to the SCSI command the caller became owner of.
650 static struct scsi_cmnd
*srp_claim_req(struct srp_target_port
*target
,
651 struct srp_request
*req
,
652 struct scsi_cmnd
*scmnd
)
656 spin_lock_irqsave(&target
->lock
, flags
);
660 } else if (req
->scmnd
== scmnd
) {
665 spin_unlock_irqrestore(&target
->lock
, flags
);
671 * srp_free_req() - Unmap data and add request to the free request list.
673 static void srp_free_req(struct srp_target_port
*target
,
674 struct srp_request
*req
, struct scsi_cmnd
*scmnd
,
679 srp_unmap_data(scmnd
, target
, req
);
681 spin_lock_irqsave(&target
->lock
, flags
);
682 target
->req_lim
+= req_lim_delta
;
683 list_add_tail(&req
->list
, &target
->free_reqs
);
684 spin_unlock_irqrestore(&target
->lock
, flags
);
687 static void srp_reset_req(struct srp_target_port
*target
, struct srp_request
*req
)
689 struct scsi_cmnd
*scmnd
= srp_claim_req(target
, req
, NULL
);
692 srp_free_req(target
, req
, scmnd
, 0);
693 scmnd
->result
= DID_RESET
<< 16;
694 scmnd
->scsi_done(scmnd
);
698 static int srp_reconnect_target(struct srp_target_port
*target
)
700 struct Scsi_Host
*shost
= target
->scsi_host
;
703 scsi_target_block(&shost
->shost_gendev
);
705 srp_disconnect_target(target
);
707 * Now get a new local CM ID so that we avoid confusing the target in
708 * case things are really fouled up. Doing so also ensures that all CM
709 * callbacks will have finished before a new QP is allocated.
711 ret
= srp_new_cm_id(target
);
713 * Whether or not creating a new CM ID succeeded, create a new
714 * QP. This guarantees that all completion callback function
715 * invocations have finished before request resetting starts.
718 ret
= srp_create_target_ib(target
);
720 srp_create_target_ib(target
);
722 for (i
= 0; i
< SRP_CMD_SQ_SIZE
; ++i
) {
723 struct srp_request
*req
= &target
->req_ring
[i
];
725 srp_reset_req(target
, req
);
728 INIT_LIST_HEAD(&target
->free_tx
);
729 for (i
= 0; i
< SRP_SQ_SIZE
; ++i
)
730 list_add(&target
->tx_ring
[i
]->list
, &target
->free_tx
);
733 ret
= srp_connect_target(target
);
735 scsi_target_unblock(&shost
->shost_gendev
, ret
== 0 ? SDEV_RUNNING
:
736 SDEV_TRANSPORT_OFFLINE
);
737 target
->transport_offline
= !!ret
;
742 shost_printk(KERN_INFO
, target
->scsi_host
, PFX
"reconnect succeeded\n");
747 shost_printk(KERN_ERR
, target
->scsi_host
,
748 PFX
"reconnect failed (%d), removing target port.\n", ret
);
751 * We couldn't reconnect, so kill our target port off.
752 * However, we have to defer the real removal because we
753 * are in the context of the SCSI error handler now, which
754 * will deadlock if we call scsi_remove_host().
756 srp_queue_remove_work(target
);
761 static void srp_map_desc(struct srp_map_state
*state
, dma_addr_t dma_addr
,
762 unsigned int dma_len
, u32 rkey
)
764 struct srp_direct_buf
*desc
= state
->desc
;
766 desc
->va
= cpu_to_be64(dma_addr
);
767 desc
->key
= cpu_to_be32(rkey
);
768 desc
->len
= cpu_to_be32(dma_len
);
770 state
->total_len
+= dma_len
;
775 static int srp_map_finish_fmr(struct srp_map_state
*state
,
776 struct srp_target_port
*target
)
778 struct srp_device
*dev
= target
->srp_host
->srp_dev
;
779 struct ib_pool_fmr
*fmr
;
785 if (state
->npages
== 1) {
786 srp_map_desc(state
, state
->base_dma_addr
, state
->fmr_len
,
788 state
->npages
= state
->fmr_len
= 0;
792 fmr
= ib_fmr_pool_map_phys(dev
->fmr_pool
, state
->pages
,
793 state
->npages
, io_addr
);
797 *state
->next_fmr
++ = fmr
;
800 srp_map_desc(state
, 0, state
->fmr_len
, fmr
->fmr
->rkey
);
801 state
->npages
= state
->fmr_len
= 0;
805 static void srp_map_update_start(struct srp_map_state
*state
,
806 struct scatterlist
*sg
, int sg_index
,
809 state
->unmapped_sg
= sg
;
810 state
->unmapped_index
= sg_index
;
811 state
->unmapped_addr
= dma_addr
;
814 static int srp_map_sg_entry(struct srp_map_state
*state
,
815 struct srp_target_port
*target
,
816 struct scatterlist
*sg
, int sg_index
,
819 struct srp_device
*dev
= target
->srp_host
->srp_dev
;
820 struct ib_device
*ibdev
= dev
->dev
;
821 dma_addr_t dma_addr
= ib_sg_dma_address(ibdev
, sg
);
822 unsigned int dma_len
= ib_sg_dma_len(ibdev
, sg
);
829 if (use_fmr
== SRP_MAP_NO_FMR
) {
830 /* Once we're in direct map mode for a request, we don't
831 * go back to FMR mode, so no need to update anything
832 * other than the descriptor.
834 srp_map_desc(state
, dma_addr
, dma_len
, target
->rkey
);
838 /* If we start at an offset into the FMR page, don't merge into
839 * the current FMR. Finish it out, and use the kernel's MR for this
840 * sg entry. This is to avoid potential bugs on some SRP targets
841 * that were never quite defined, but went away when the initiator
842 * avoided using FMR on such page fragments.
844 if (dma_addr
& ~dev
->fmr_page_mask
|| dma_len
> dev
->fmr_max_size
) {
845 ret
= srp_map_finish_fmr(state
, target
);
849 srp_map_desc(state
, dma_addr
, dma_len
, target
->rkey
);
850 srp_map_update_start(state
, NULL
, 0, 0);
854 /* If this is the first sg to go into the FMR, save our position.
855 * We need to know the first unmapped entry, its index, and the
856 * first unmapped address within that entry to be able to restart
857 * mapping after an error.
859 if (!state
->unmapped_sg
)
860 srp_map_update_start(state
, sg
, sg_index
, dma_addr
);
863 if (state
->npages
== SRP_FMR_SIZE
) {
864 ret
= srp_map_finish_fmr(state
, target
);
868 srp_map_update_start(state
, sg
, sg_index
, dma_addr
);
871 len
= min_t(unsigned int, dma_len
, dev
->fmr_page_size
);
874 state
->base_dma_addr
= dma_addr
;
875 state
->pages
[state
->npages
++] = dma_addr
;
876 state
->fmr_len
+= len
;
881 /* If the last entry of the FMR wasn't a full page, then we need to
882 * close it out and start a new one -- we can only merge at page
886 if (len
!= dev
->fmr_page_size
) {
887 ret
= srp_map_finish_fmr(state
, target
);
889 srp_map_update_start(state
, NULL
, 0, 0);
894 static int srp_map_data(struct scsi_cmnd
*scmnd
, struct srp_target_port
*target
,
895 struct srp_request
*req
)
897 struct scatterlist
*scat
, *sg
;
898 struct srp_cmd
*cmd
= req
->cmd
->buf
;
899 int i
, len
, nents
, count
, use_fmr
;
900 struct srp_device
*dev
;
901 struct ib_device
*ibdev
;
902 struct srp_map_state state
;
903 struct srp_indirect_buf
*indirect_hdr
;
907 if (!scsi_sglist(scmnd
) || scmnd
->sc_data_direction
== DMA_NONE
)
908 return sizeof (struct srp_cmd
);
910 if (scmnd
->sc_data_direction
!= DMA_FROM_DEVICE
&&
911 scmnd
->sc_data_direction
!= DMA_TO_DEVICE
) {
912 shost_printk(KERN_WARNING
, target
->scsi_host
,
913 PFX
"Unhandled data direction %d\n",
914 scmnd
->sc_data_direction
);
918 nents
= scsi_sg_count(scmnd
);
919 scat
= scsi_sglist(scmnd
);
921 dev
= target
->srp_host
->srp_dev
;
924 count
= ib_dma_map_sg(ibdev
, scat
, nents
, scmnd
->sc_data_direction
);
925 if (unlikely(count
== 0))
928 fmt
= SRP_DATA_DESC_DIRECT
;
929 len
= sizeof (struct srp_cmd
) + sizeof (struct srp_direct_buf
);
933 * The midlayer only generated a single gather/scatter
934 * entry, or DMA mapping coalesced everything to a
935 * single entry. So a direct descriptor along with
936 * the DMA MR suffices.
938 struct srp_direct_buf
*buf
= (void *) cmd
->add_data
;
940 buf
->va
= cpu_to_be64(ib_sg_dma_address(ibdev
, scat
));
941 buf
->key
= cpu_to_be32(target
->rkey
);
942 buf
->len
= cpu_to_be32(ib_sg_dma_len(ibdev
, scat
));
948 /* We have more than one scatter/gather entry, so build our indirect
949 * descriptor table, trying to merge as many entries with FMR as we
952 indirect_hdr
= (void *) cmd
->add_data
;
954 ib_dma_sync_single_for_cpu(ibdev
, req
->indirect_dma_addr
,
955 target
->indirect_size
, DMA_TO_DEVICE
);
957 memset(&state
, 0, sizeof(state
));
958 state
.desc
= req
->indirect_desc
;
959 state
.pages
= req
->map_page
;
960 state
.next_fmr
= req
->fmr_list
;
962 use_fmr
= dev
->fmr_pool
? SRP_MAP_ALLOW_FMR
: SRP_MAP_NO_FMR
;
964 for_each_sg(scat
, sg
, count
, i
) {
965 if (srp_map_sg_entry(&state
, target
, sg
, i
, use_fmr
)) {
966 /* FMR mapping failed, so backtrack to the first
967 * unmapped entry and continue on without using FMR.
970 unsigned int dma_len
;
973 sg
= state
.unmapped_sg
;
974 i
= state
.unmapped_index
;
976 dma_addr
= ib_sg_dma_address(ibdev
, sg
);
977 dma_len
= ib_sg_dma_len(ibdev
, sg
);
978 dma_len
-= (state
.unmapped_addr
- dma_addr
);
979 dma_addr
= state
.unmapped_addr
;
980 use_fmr
= SRP_MAP_NO_FMR
;
981 srp_map_desc(&state
, dma_addr
, dma_len
, target
->rkey
);
985 if (use_fmr
== SRP_MAP_ALLOW_FMR
&& srp_map_finish_fmr(&state
, target
))
988 /* We've mapped the request, now pull as much of the indirect
989 * descriptor table as we can into the command buffer. If this
990 * target is not using an external indirect table, we are
991 * guaranteed to fit into the command, as the SCSI layer won't
992 * give us more S/G entries than we allow.
994 req
->nfmr
= state
.nfmr
;
995 if (state
.ndesc
== 1) {
996 /* FMR mapping was able to collapse this to one entry,
997 * so use a direct descriptor.
999 struct srp_direct_buf
*buf
= (void *) cmd
->add_data
;
1001 *buf
= req
->indirect_desc
[0];
1005 if (unlikely(target
->cmd_sg_cnt
< state
.ndesc
&&
1006 !target
->allow_ext_sg
)) {
1007 shost_printk(KERN_ERR
, target
->scsi_host
,
1008 "Could not fit S/G list into SRP_CMD\n");
1012 count
= min(state
.ndesc
, target
->cmd_sg_cnt
);
1013 table_len
= state
.ndesc
* sizeof (struct srp_direct_buf
);
1015 fmt
= SRP_DATA_DESC_INDIRECT
;
1016 len
= sizeof(struct srp_cmd
) + sizeof (struct srp_indirect_buf
);
1017 len
+= count
* sizeof (struct srp_direct_buf
);
1019 memcpy(indirect_hdr
->desc_list
, req
->indirect_desc
,
1020 count
* sizeof (struct srp_direct_buf
));
1022 indirect_hdr
->table_desc
.va
= cpu_to_be64(req
->indirect_dma_addr
);
1023 indirect_hdr
->table_desc
.key
= cpu_to_be32(target
->rkey
);
1024 indirect_hdr
->table_desc
.len
= cpu_to_be32(table_len
);
1025 indirect_hdr
->len
= cpu_to_be32(state
.total_len
);
1027 if (scmnd
->sc_data_direction
== DMA_TO_DEVICE
)
1028 cmd
->data_out_desc_cnt
= count
;
1030 cmd
->data_in_desc_cnt
= count
;
1032 ib_dma_sync_single_for_device(ibdev
, req
->indirect_dma_addr
, table_len
,
1036 if (scmnd
->sc_data_direction
== DMA_TO_DEVICE
)
1037 cmd
->buf_fmt
= fmt
<< 4;
1045 * Return an IU and possible credit to the free pool
1047 static void srp_put_tx_iu(struct srp_target_port
*target
, struct srp_iu
*iu
,
1048 enum srp_iu_type iu_type
)
1050 unsigned long flags
;
1052 spin_lock_irqsave(&target
->lock
, flags
);
1053 list_add(&iu
->list
, &target
->free_tx
);
1054 if (iu_type
!= SRP_IU_RSP
)
1056 spin_unlock_irqrestore(&target
->lock
, flags
);
1060 * Must be called with target->lock held to protect req_lim and free_tx.
1061 * If IU is not sent, it must be returned using srp_put_tx_iu().
1064 * An upper limit for the number of allocated information units for each
1066 * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues
1067 * more than Scsi_Host.can_queue requests.
1068 * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE.
1069 * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than
1070 * one unanswered SRP request to an initiator.
1072 static struct srp_iu
*__srp_get_tx_iu(struct srp_target_port
*target
,
1073 enum srp_iu_type iu_type
)
1075 s32 rsv
= (iu_type
== SRP_IU_TSK_MGMT
) ? 0 : SRP_TSK_MGMT_SQ_SIZE
;
1078 srp_send_completion(target
->send_cq
, target
);
1080 if (list_empty(&target
->free_tx
))
1083 /* Initiator responses to target requests do not consume credits */
1084 if (iu_type
!= SRP_IU_RSP
) {
1085 if (target
->req_lim
<= rsv
) {
1086 ++target
->zero_req_lim
;
1093 iu
= list_first_entry(&target
->free_tx
, struct srp_iu
, list
);
1094 list_del(&iu
->list
);
1098 static int srp_post_send(struct srp_target_port
*target
,
1099 struct srp_iu
*iu
, int len
)
1102 struct ib_send_wr wr
, *bad_wr
;
1104 list
.addr
= iu
->dma
;
1106 list
.lkey
= target
->lkey
;
1109 wr
.wr_id
= (uintptr_t) iu
;
1112 wr
.opcode
= IB_WR_SEND
;
1113 wr
.send_flags
= IB_SEND_SIGNALED
;
1115 return ib_post_send(target
->qp
, &wr
, &bad_wr
);
1118 static int srp_post_recv(struct srp_target_port
*target
, struct srp_iu
*iu
)
1120 struct ib_recv_wr wr
, *bad_wr
;
1123 list
.addr
= iu
->dma
;
1124 list
.length
= iu
->size
;
1125 list
.lkey
= target
->lkey
;
1128 wr
.wr_id
= (uintptr_t) iu
;
1132 return ib_post_recv(target
->qp
, &wr
, &bad_wr
);
1135 static void srp_process_rsp(struct srp_target_port
*target
, struct srp_rsp
*rsp
)
1137 struct srp_request
*req
;
1138 struct scsi_cmnd
*scmnd
;
1139 unsigned long flags
;
1141 if (unlikely(rsp
->tag
& SRP_TAG_TSK_MGMT
)) {
1142 spin_lock_irqsave(&target
->lock
, flags
);
1143 target
->req_lim
+= be32_to_cpu(rsp
->req_lim_delta
);
1144 spin_unlock_irqrestore(&target
->lock
, flags
);
1146 target
->tsk_mgmt_status
= -1;
1147 if (be32_to_cpu(rsp
->resp_data_len
) >= 4)
1148 target
->tsk_mgmt_status
= rsp
->data
[3];
1149 complete(&target
->tsk_mgmt_done
);
1151 req
= &target
->req_ring
[rsp
->tag
];
1152 scmnd
= srp_claim_req(target
, req
, NULL
);
1154 shost_printk(KERN_ERR
, target
->scsi_host
,
1155 "Null scmnd for RSP w/tag %016llx\n",
1156 (unsigned long long) rsp
->tag
);
1158 spin_lock_irqsave(&target
->lock
, flags
);
1159 target
->req_lim
+= be32_to_cpu(rsp
->req_lim_delta
);
1160 spin_unlock_irqrestore(&target
->lock
, flags
);
1164 scmnd
->result
= rsp
->status
;
1166 if (rsp
->flags
& SRP_RSP_FLAG_SNSVALID
) {
1167 memcpy(scmnd
->sense_buffer
, rsp
->data
+
1168 be32_to_cpu(rsp
->resp_data_len
),
1169 min_t(int, be32_to_cpu(rsp
->sense_data_len
),
1170 SCSI_SENSE_BUFFERSIZE
));
1173 if (rsp
->flags
& (SRP_RSP_FLAG_DOOVER
| SRP_RSP_FLAG_DOUNDER
))
1174 scsi_set_resid(scmnd
, be32_to_cpu(rsp
->data_out_res_cnt
));
1175 else if (rsp
->flags
& (SRP_RSP_FLAG_DIOVER
| SRP_RSP_FLAG_DIUNDER
))
1176 scsi_set_resid(scmnd
, be32_to_cpu(rsp
->data_in_res_cnt
));
1178 srp_free_req(target
, req
, scmnd
,
1179 be32_to_cpu(rsp
->req_lim_delta
));
1181 scmnd
->host_scribble
= NULL
;
1182 scmnd
->scsi_done(scmnd
);
1186 static int srp_response_common(struct srp_target_port
*target
, s32 req_delta
,
1189 struct ib_device
*dev
= target
->srp_host
->srp_dev
->dev
;
1190 unsigned long flags
;
1194 spin_lock_irqsave(&target
->lock
, flags
);
1195 target
->req_lim
+= req_delta
;
1196 iu
= __srp_get_tx_iu(target
, SRP_IU_RSP
);
1197 spin_unlock_irqrestore(&target
->lock
, flags
);
1200 shost_printk(KERN_ERR
, target
->scsi_host
, PFX
1201 "no IU available to send response\n");
1205 ib_dma_sync_single_for_cpu(dev
, iu
->dma
, len
, DMA_TO_DEVICE
);
1206 memcpy(iu
->buf
, rsp
, len
);
1207 ib_dma_sync_single_for_device(dev
, iu
->dma
, len
, DMA_TO_DEVICE
);
1209 err
= srp_post_send(target
, iu
, len
);
1211 shost_printk(KERN_ERR
, target
->scsi_host
, PFX
1212 "unable to post response: %d\n", err
);
1213 srp_put_tx_iu(target
, iu
, SRP_IU_RSP
);
1219 static void srp_process_cred_req(struct srp_target_port
*target
,
1220 struct srp_cred_req
*req
)
1222 struct srp_cred_rsp rsp
= {
1223 .opcode
= SRP_CRED_RSP
,
1226 s32 delta
= be32_to_cpu(req
->req_lim_delta
);
1228 if (srp_response_common(target
, delta
, &rsp
, sizeof rsp
))
1229 shost_printk(KERN_ERR
, target
->scsi_host
, PFX
1230 "problems processing SRP_CRED_REQ\n");
1233 static void srp_process_aer_req(struct srp_target_port
*target
,
1234 struct srp_aer_req
*req
)
1236 struct srp_aer_rsp rsp
= {
1237 .opcode
= SRP_AER_RSP
,
1240 s32 delta
= be32_to_cpu(req
->req_lim_delta
);
1242 shost_printk(KERN_ERR
, target
->scsi_host
, PFX
1243 "ignoring AER for LUN %llu\n", be64_to_cpu(req
->lun
));
1245 if (srp_response_common(target
, delta
, &rsp
, sizeof rsp
))
1246 shost_printk(KERN_ERR
, target
->scsi_host
, PFX
1247 "problems processing SRP_AER_REQ\n");
1250 static void srp_handle_recv(struct srp_target_port
*target
, struct ib_wc
*wc
)
1252 struct ib_device
*dev
= target
->srp_host
->srp_dev
->dev
;
1253 struct srp_iu
*iu
= (struct srp_iu
*) (uintptr_t) wc
->wr_id
;
1257 ib_dma_sync_single_for_cpu(dev
, iu
->dma
, target
->max_ti_iu_len
,
1260 opcode
= *(u8
*) iu
->buf
;
1263 shost_printk(KERN_ERR
, target
->scsi_host
,
1264 PFX
"recv completion, opcode 0x%02x\n", opcode
);
1265 print_hex_dump(KERN_ERR
, "", DUMP_PREFIX_OFFSET
, 8, 1,
1266 iu
->buf
, wc
->byte_len
, true);
1271 srp_process_rsp(target
, iu
->buf
);
1275 srp_process_cred_req(target
, iu
->buf
);
1279 srp_process_aer_req(target
, iu
->buf
);
1283 /* XXX Handle target logout */
1284 shost_printk(KERN_WARNING
, target
->scsi_host
,
1285 PFX
"Got target logout request\n");
1289 shost_printk(KERN_WARNING
, target
->scsi_host
,
1290 PFX
"Unhandled SRP opcode 0x%02x\n", opcode
);
1294 ib_dma_sync_single_for_device(dev
, iu
->dma
, target
->max_ti_iu_len
,
1297 res
= srp_post_recv(target
, iu
);
1299 shost_printk(KERN_ERR
, target
->scsi_host
,
1300 PFX
"Recv failed with error code %d\n", res
);
1303 static void srp_handle_qp_err(enum ib_wc_status wc_status
,
1304 enum ib_wc_opcode wc_opcode
,
1305 struct srp_target_port
*target
)
1307 if (target
->connected
&& !target
->qp_in_error
) {
1308 shost_printk(KERN_ERR
, target
->scsi_host
,
1309 PFX
"failed %s status %d\n",
1310 wc_opcode
& IB_WC_RECV
? "receive" : "send",
1313 target
->qp_in_error
= true;
1316 static void srp_recv_completion(struct ib_cq
*cq
, void *target_ptr
)
1318 struct srp_target_port
*target
= target_ptr
;
1321 ib_req_notify_cq(cq
, IB_CQ_NEXT_COMP
);
1322 while (ib_poll_cq(cq
, 1, &wc
) > 0) {
1323 if (likely(wc
.status
== IB_WC_SUCCESS
)) {
1324 srp_handle_recv(target
, &wc
);
1326 srp_handle_qp_err(wc
.status
, wc
.opcode
, target
);
1331 static void srp_send_completion(struct ib_cq
*cq
, void *target_ptr
)
1333 struct srp_target_port
*target
= target_ptr
;
1337 while (ib_poll_cq(cq
, 1, &wc
) > 0) {
1338 if (likely(wc
.status
== IB_WC_SUCCESS
)) {
1339 iu
= (struct srp_iu
*) (uintptr_t) wc
.wr_id
;
1340 list_add(&iu
->list
, &target
->free_tx
);
1342 srp_handle_qp_err(wc
.status
, wc
.opcode
, target
);
1347 static int srp_queuecommand(struct Scsi_Host
*shost
, struct scsi_cmnd
*scmnd
)
1349 struct srp_target_port
*target
= host_to_target(shost
);
1350 struct srp_request
*req
;
1352 struct srp_cmd
*cmd
;
1353 struct ib_device
*dev
;
1354 unsigned long flags
;
1357 if (unlikely(target
->transport_offline
)) {
1358 scmnd
->result
= DID_NO_CONNECT
<< 16;
1359 scmnd
->scsi_done(scmnd
);
1363 spin_lock_irqsave(&target
->lock
, flags
);
1364 iu
= __srp_get_tx_iu(target
, SRP_IU_CMD
);
1368 req
= list_first_entry(&target
->free_reqs
, struct srp_request
, list
);
1369 list_del(&req
->list
);
1370 spin_unlock_irqrestore(&target
->lock
, flags
);
1372 dev
= target
->srp_host
->srp_dev
->dev
;
1373 ib_dma_sync_single_for_cpu(dev
, iu
->dma
, target
->max_iu_len
,
1377 scmnd
->host_scribble
= (void *) req
;
1380 memset(cmd
, 0, sizeof *cmd
);
1382 cmd
->opcode
= SRP_CMD
;
1383 cmd
->lun
= cpu_to_be64((u64
) scmnd
->device
->lun
<< 48);
1384 cmd
->tag
= req
->index
;
1385 memcpy(cmd
->cdb
, scmnd
->cmnd
, scmnd
->cmd_len
);
1390 len
= srp_map_data(scmnd
, target
, req
);
1392 shost_printk(KERN_ERR
, target
->scsi_host
,
1393 PFX
"Failed to map data\n");
1397 ib_dma_sync_single_for_device(dev
, iu
->dma
, target
->max_iu_len
,
1400 if (srp_post_send(target
, iu
, len
)) {
1401 shost_printk(KERN_ERR
, target
->scsi_host
, PFX
"Send failed\n");
1408 srp_unmap_data(scmnd
, target
, req
);
1411 srp_put_tx_iu(target
, iu
, SRP_IU_CMD
);
1413 spin_lock_irqsave(&target
->lock
, flags
);
1414 list_add(&req
->list
, &target
->free_reqs
);
1417 spin_unlock_irqrestore(&target
->lock
, flags
);
1419 return SCSI_MLQUEUE_HOST_BUSY
;
1422 static int srp_alloc_iu_bufs(struct srp_target_port
*target
)
1426 for (i
= 0; i
< SRP_RQ_SIZE
; ++i
) {
1427 target
->rx_ring
[i
] = srp_alloc_iu(target
->srp_host
,
1428 target
->max_ti_iu_len
,
1429 GFP_KERNEL
, DMA_FROM_DEVICE
);
1430 if (!target
->rx_ring
[i
])
1434 for (i
= 0; i
< SRP_SQ_SIZE
; ++i
) {
1435 target
->tx_ring
[i
] = srp_alloc_iu(target
->srp_host
,
1437 GFP_KERNEL
, DMA_TO_DEVICE
);
1438 if (!target
->tx_ring
[i
])
1441 list_add(&target
->tx_ring
[i
]->list
, &target
->free_tx
);
1447 for (i
= 0; i
< SRP_RQ_SIZE
; ++i
) {
1448 srp_free_iu(target
->srp_host
, target
->rx_ring
[i
]);
1449 target
->rx_ring
[i
] = NULL
;
1452 for (i
= 0; i
< SRP_SQ_SIZE
; ++i
) {
1453 srp_free_iu(target
->srp_host
, target
->tx_ring
[i
]);
1454 target
->tx_ring
[i
] = NULL
;
1460 static uint32_t srp_compute_rq_tmo(struct ib_qp_attr
*qp_attr
, int attr_mask
)
1462 uint64_t T_tr_ns
, max_compl_time_ms
;
1463 uint32_t rq_tmo_jiffies
;
1466 * According to section 11.2.4.2 in the IBTA spec (Modify Queue Pair,
1467 * table 91), both the QP timeout and the retry count have to be set
1468 * for RC QP's during the RTR to RTS transition.
1470 WARN_ON_ONCE((attr_mask
& (IB_QP_TIMEOUT
| IB_QP_RETRY_CNT
)) !=
1471 (IB_QP_TIMEOUT
| IB_QP_RETRY_CNT
));
1474 * Set target->rq_tmo_jiffies to one second more than the largest time
1475 * it can take before an error completion is generated. See also
1476 * C9-140..142 in the IBTA spec for more information about how to
1477 * convert the QP Local ACK Timeout value to nanoseconds.
1479 T_tr_ns
= 4096 * (1ULL << qp_attr
->timeout
);
1480 max_compl_time_ms
= qp_attr
->retry_cnt
* 4 * T_tr_ns
;
1481 do_div(max_compl_time_ms
, NSEC_PER_MSEC
);
1482 rq_tmo_jiffies
= msecs_to_jiffies(max_compl_time_ms
+ 1000);
1484 return rq_tmo_jiffies
;
1487 static void srp_cm_rep_handler(struct ib_cm_id
*cm_id
,
1488 struct srp_login_rsp
*lrsp
,
1489 struct srp_target_port
*target
)
1491 struct ib_qp_attr
*qp_attr
= NULL
;
1496 if (lrsp
->opcode
== SRP_LOGIN_RSP
) {
1497 target
->max_ti_iu_len
= be32_to_cpu(lrsp
->max_ti_iu_len
);
1498 target
->req_lim
= be32_to_cpu(lrsp
->req_lim_delta
);
1501 * Reserve credits for task management so we don't
1502 * bounce requests back to the SCSI mid-layer.
1504 target
->scsi_host
->can_queue
1505 = min(target
->req_lim
- SRP_TSK_MGMT_SQ_SIZE
,
1506 target
->scsi_host
->can_queue
);
1508 shost_printk(KERN_WARNING
, target
->scsi_host
,
1509 PFX
"Unhandled RSP opcode %#x\n", lrsp
->opcode
);
1514 if (!target
->rx_ring
[0]) {
1515 ret
= srp_alloc_iu_bufs(target
);
1521 qp_attr
= kmalloc(sizeof *qp_attr
, GFP_KERNEL
);
1525 qp_attr
->qp_state
= IB_QPS_RTR
;
1526 ret
= ib_cm_init_qp_attr(cm_id
, qp_attr
, &attr_mask
);
1530 ret
= ib_modify_qp(target
->qp
, qp_attr
, attr_mask
);
1534 for (i
= 0; i
< SRP_RQ_SIZE
; i
++) {
1535 struct srp_iu
*iu
= target
->rx_ring
[i
];
1536 ret
= srp_post_recv(target
, iu
);
1541 qp_attr
->qp_state
= IB_QPS_RTS
;
1542 ret
= ib_cm_init_qp_attr(cm_id
, qp_attr
, &attr_mask
);
1546 target
->rq_tmo_jiffies
= srp_compute_rq_tmo(qp_attr
, attr_mask
);
1548 ret
= ib_modify_qp(target
->qp
, qp_attr
, attr_mask
);
1552 ret
= ib_send_cm_rtu(cm_id
, NULL
, 0);
1558 target
->status
= ret
;
1561 static void srp_cm_rej_handler(struct ib_cm_id
*cm_id
,
1562 struct ib_cm_event
*event
,
1563 struct srp_target_port
*target
)
1565 struct Scsi_Host
*shost
= target
->scsi_host
;
1566 struct ib_class_port_info
*cpi
;
1569 switch (event
->param
.rej_rcvd
.reason
) {
1570 case IB_CM_REJ_PORT_CM_REDIRECT
:
1571 cpi
= event
->param
.rej_rcvd
.ari
;
1572 target
->path
.dlid
= cpi
->redirect_lid
;
1573 target
->path
.pkey
= cpi
->redirect_pkey
;
1574 cm_id
->remote_cm_qpn
= be32_to_cpu(cpi
->redirect_qp
) & 0x00ffffff;
1575 memcpy(target
->path
.dgid
.raw
, cpi
->redirect_gid
, 16);
1577 target
->status
= target
->path
.dlid
?
1578 SRP_DLID_REDIRECT
: SRP_PORT_REDIRECT
;
1581 case IB_CM_REJ_PORT_REDIRECT
:
1582 if (srp_target_is_topspin(target
)) {
1584 * Topspin/Cisco SRP gateways incorrectly send
1585 * reject reason code 25 when they mean 24
1588 memcpy(target
->path
.dgid
.raw
,
1589 event
->param
.rej_rcvd
.ari
, 16);
1591 shost_printk(KERN_DEBUG
, shost
,
1592 PFX
"Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1593 (unsigned long long) be64_to_cpu(target
->path
.dgid
.global
.subnet_prefix
),
1594 (unsigned long long) be64_to_cpu(target
->path
.dgid
.global
.interface_id
));
1596 target
->status
= SRP_PORT_REDIRECT
;
1598 shost_printk(KERN_WARNING
, shost
,
1599 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1600 target
->status
= -ECONNRESET
;
1604 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID
:
1605 shost_printk(KERN_WARNING
, shost
,
1606 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1607 target
->status
= -ECONNRESET
;
1610 case IB_CM_REJ_CONSUMER_DEFINED
:
1611 opcode
= *(u8
*) event
->private_data
;
1612 if (opcode
== SRP_LOGIN_REJ
) {
1613 struct srp_login_rej
*rej
= event
->private_data
;
1614 u32 reason
= be32_to_cpu(rej
->reason
);
1616 if (reason
== SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE
)
1617 shost_printk(KERN_WARNING
, shost
,
1618 PFX
"SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1620 shost_printk(KERN_WARNING
, shost
,
1621 PFX
"SRP LOGIN REJECTED, reason 0x%08x\n", reason
);
1623 shost_printk(KERN_WARNING
, shost
,
1624 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1625 " opcode 0x%02x\n", opcode
);
1626 target
->status
= -ECONNRESET
;
1629 case IB_CM_REJ_STALE_CONN
:
1630 shost_printk(KERN_WARNING
, shost
, " REJ reason: stale connection\n");
1631 target
->status
= SRP_STALE_CONN
;
1635 shost_printk(KERN_WARNING
, shost
, " REJ reason 0x%x\n",
1636 event
->param
.rej_rcvd
.reason
);
1637 target
->status
= -ECONNRESET
;
1641 static int srp_cm_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*event
)
1643 struct srp_target_port
*target
= cm_id
->context
;
1646 switch (event
->event
) {
1647 case IB_CM_REQ_ERROR
:
1648 shost_printk(KERN_DEBUG
, target
->scsi_host
,
1649 PFX
"Sending CM REQ failed\n");
1651 target
->status
= -ECONNRESET
;
1654 case IB_CM_REP_RECEIVED
:
1656 srp_cm_rep_handler(cm_id
, event
->private_data
, target
);
1659 case IB_CM_REJ_RECEIVED
:
1660 shost_printk(KERN_DEBUG
, target
->scsi_host
, PFX
"REJ received\n");
1663 srp_cm_rej_handler(cm_id
, event
, target
);
1666 case IB_CM_DREQ_RECEIVED
:
1667 shost_printk(KERN_WARNING
, target
->scsi_host
,
1668 PFX
"DREQ received - connection closed\n");
1669 srp_change_conn_state(target
, false);
1670 if (ib_send_cm_drep(cm_id
, NULL
, 0))
1671 shost_printk(KERN_ERR
, target
->scsi_host
,
1672 PFX
"Sending CM DREP failed\n");
1675 case IB_CM_TIMEWAIT_EXIT
:
1676 shost_printk(KERN_ERR
, target
->scsi_host
,
1677 PFX
"connection closed\n");
1682 case IB_CM_MRA_RECEIVED
:
1683 case IB_CM_DREQ_ERROR
:
1684 case IB_CM_DREP_RECEIVED
:
1688 shost_printk(KERN_WARNING
, target
->scsi_host
,
1689 PFX
"Unhandled CM event %d\n", event
->event
);
1694 complete(&target
->done
);
1699 static int srp_send_tsk_mgmt(struct srp_target_port
*target
,
1700 u64 req_tag
, unsigned int lun
, u8 func
)
1702 struct ib_device
*dev
= target
->srp_host
->srp_dev
->dev
;
1704 struct srp_tsk_mgmt
*tsk_mgmt
;
1706 if (!target
->connected
|| target
->qp_in_error
)
1709 init_completion(&target
->tsk_mgmt_done
);
1711 spin_lock_irq(&target
->lock
);
1712 iu
= __srp_get_tx_iu(target
, SRP_IU_TSK_MGMT
);
1713 spin_unlock_irq(&target
->lock
);
1718 ib_dma_sync_single_for_cpu(dev
, iu
->dma
, sizeof *tsk_mgmt
,
1721 memset(tsk_mgmt
, 0, sizeof *tsk_mgmt
);
1723 tsk_mgmt
->opcode
= SRP_TSK_MGMT
;
1724 tsk_mgmt
->lun
= cpu_to_be64((u64
) lun
<< 48);
1725 tsk_mgmt
->tag
= req_tag
| SRP_TAG_TSK_MGMT
;
1726 tsk_mgmt
->tsk_mgmt_func
= func
;
1727 tsk_mgmt
->task_tag
= req_tag
;
1729 ib_dma_sync_single_for_device(dev
, iu
->dma
, sizeof *tsk_mgmt
,
1731 if (srp_post_send(target
, iu
, sizeof *tsk_mgmt
)) {
1732 srp_put_tx_iu(target
, iu
, SRP_IU_TSK_MGMT
);
1736 if (!wait_for_completion_timeout(&target
->tsk_mgmt_done
,
1737 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS
)))
1743 static int srp_abort(struct scsi_cmnd
*scmnd
)
1745 struct srp_target_port
*target
= host_to_target(scmnd
->device
->host
);
1746 struct srp_request
*req
= (struct srp_request
*) scmnd
->host_scribble
;
1749 shost_printk(KERN_ERR
, target
->scsi_host
, "SRP abort called\n");
1751 if (!req
|| !srp_claim_req(target
, req
, scmnd
))
1753 if (srp_send_tsk_mgmt(target
, req
->index
, scmnd
->device
->lun
,
1754 SRP_TSK_ABORT_TASK
) == 0 ||
1755 target
->transport_offline
)
1757 else if (target
->transport_offline
)
1761 srp_free_req(target
, req
, scmnd
, 0);
1762 scmnd
->result
= DID_ABORT
<< 16;
1763 scmnd
->scsi_done(scmnd
);
1768 static int srp_reset_device(struct scsi_cmnd
*scmnd
)
1770 struct srp_target_port
*target
= host_to_target(scmnd
->device
->host
);
1773 shost_printk(KERN_ERR
, target
->scsi_host
, "SRP reset_device called\n");
1775 if (srp_send_tsk_mgmt(target
, SRP_TAG_NO_REQ
, scmnd
->device
->lun
,
1778 if (target
->tsk_mgmt_status
)
1781 for (i
= 0; i
< SRP_CMD_SQ_SIZE
; ++i
) {
1782 struct srp_request
*req
= &target
->req_ring
[i
];
1783 if (req
->scmnd
&& req
->scmnd
->device
== scmnd
->device
)
1784 srp_reset_req(target
, req
);
1790 static int srp_reset_host(struct scsi_cmnd
*scmnd
)
1792 struct srp_target_port
*target
= host_to_target(scmnd
->device
->host
);
1795 shost_printk(KERN_ERR
, target
->scsi_host
, PFX
"SRP reset_host called\n");
1797 if (!srp_reconnect_target(target
))
1803 static int srp_slave_configure(struct scsi_device
*sdev
)
1805 struct Scsi_Host
*shost
= sdev
->host
;
1806 struct srp_target_port
*target
= host_to_target(shost
);
1807 struct request_queue
*q
= sdev
->request_queue
;
1808 unsigned long timeout
;
1810 if (sdev
->type
== TYPE_DISK
) {
1811 timeout
= max_t(unsigned, 30 * HZ
, target
->rq_tmo_jiffies
);
1812 blk_queue_rq_timeout(q
, timeout
);
1818 static ssize_t
show_id_ext(struct device
*dev
, struct device_attribute
*attr
,
1821 struct srp_target_port
*target
= host_to_target(class_to_shost(dev
));
1823 return sprintf(buf
, "0x%016llx\n",
1824 (unsigned long long) be64_to_cpu(target
->id_ext
));
1827 static ssize_t
show_ioc_guid(struct device
*dev
, struct device_attribute
*attr
,
1830 struct srp_target_port
*target
= host_to_target(class_to_shost(dev
));
1832 return sprintf(buf
, "0x%016llx\n",
1833 (unsigned long long) be64_to_cpu(target
->ioc_guid
));
1836 static ssize_t
show_service_id(struct device
*dev
,
1837 struct device_attribute
*attr
, char *buf
)
1839 struct srp_target_port
*target
= host_to_target(class_to_shost(dev
));
1841 return sprintf(buf
, "0x%016llx\n",
1842 (unsigned long long) be64_to_cpu(target
->service_id
));
1845 static ssize_t
show_pkey(struct device
*dev
, struct device_attribute
*attr
,
1848 struct srp_target_port
*target
= host_to_target(class_to_shost(dev
));
1850 return sprintf(buf
, "0x%04x\n", be16_to_cpu(target
->path
.pkey
));
1853 static ssize_t
show_dgid(struct device
*dev
, struct device_attribute
*attr
,
1856 struct srp_target_port
*target
= host_to_target(class_to_shost(dev
));
1858 return sprintf(buf
, "%pI6\n", target
->path
.dgid
.raw
);
1861 static ssize_t
show_orig_dgid(struct device
*dev
,
1862 struct device_attribute
*attr
, char *buf
)
1864 struct srp_target_port
*target
= host_to_target(class_to_shost(dev
));
1866 return sprintf(buf
, "%pI6\n", target
->orig_dgid
);
1869 static ssize_t
show_req_lim(struct device
*dev
,
1870 struct device_attribute
*attr
, char *buf
)
1872 struct srp_target_port
*target
= host_to_target(class_to_shost(dev
));
1874 return sprintf(buf
, "%d\n", target
->req_lim
);
1877 static ssize_t
show_zero_req_lim(struct device
*dev
,
1878 struct device_attribute
*attr
, char *buf
)
1880 struct srp_target_port
*target
= host_to_target(class_to_shost(dev
));
1882 return sprintf(buf
, "%d\n", target
->zero_req_lim
);
1885 static ssize_t
show_local_ib_port(struct device
*dev
,
1886 struct device_attribute
*attr
, char *buf
)
1888 struct srp_target_port
*target
= host_to_target(class_to_shost(dev
));
1890 return sprintf(buf
, "%d\n", target
->srp_host
->port
);
1893 static ssize_t
show_local_ib_device(struct device
*dev
,
1894 struct device_attribute
*attr
, char *buf
)
1896 struct srp_target_port
*target
= host_to_target(class_to_shost(dev
));
1898 return sprintf(buf
, "%s\n", target
->srp_host
->srp_dev
->dev
->name
);
1901 static ssize_t
show_cmd_sg_entries(struct device
*dev
,
1902 struct device_attribute
*attr
, char *buf
)
1904 struct srp_target_port
*target
= host_to_target(class_to_shost(dev
));
1906 return sprintf(buf
, "%u\n", target
->cmd_sg_cnt
);
1909 static ssize_t
show_allow_ext_sg(struct device
*dev
,
1910 struct device_attribute
*attr
, char *buf
)
1912 struct srp_target_port
*target
= host_to_target(class_to_shost(dev
));
1914 return sprintf(buf
, "%s\n", target
->allow_ext_sg
? "true" : "false");
1917 static DEVICE_ATTR(id_ext
, S_IRUGO
, show_id_ext
, NULL
);
1918 static DEVICE_ATTR(ioc_guid
, S_IRUGO
, show_ioc_guid
, NULL
);
1919 static DEVICE_ATTR(service_id
, S_IRUGO
, show_service_id
, NULL
);
1920 static DEVICE_ATTR(pkey
, S_IRUGO
, show_pkey
, NULL
);
1921 static DEVICE_ATTR(dgid
, S_IRUGO
, show_dgid
, NULL
);
1922 static DEVICE_ATTR(orig_dgid
, S_IRUGO
, show_orig_dgid
, NULL
);
1923 static DEVICE_ATTR(req_lim
, S_IRUGO
, show_req_lim
, NULL
);
1924 static DEVICE_ATTR(zero_req_lim
, S_IRUGO
, show_zero_req_lim
, NULL
);
1925 static DEVICE_ATTR(local_ib_port
, S_IRUGO
, show_local_ib_port
, NULL
);
1926 static DEVICE_ATTR(local_ib_device
, S_IRUGO
, show_local_ib_device
, NULL
);
1927 static DEVICE_ATTR(cmd_sg_entries
, S_IRUGO
, show_cmd_sg_entries
, NULL
);
1928 static DEVICE_ATTR(allow_ext_sg
, S_IRUGO
, show_allow_ext_sg
, NULL
);
1930 static struct device_attribute
*srp_host_attrs
[] = {
1933 &dev_attr_service_id
,
1936 &dev_attr_orig_dgid
,
1938 &dev_attr_zero_req_lim
,
1939 &dev_attr_local_ib_port
,
1940 &dev_attr_local_ib_device
,
1941 &dev_attr_cmd_sg_entries
,
1942 &dev_attr_allow_ext_sg
,
1946 static struct scsi_host_template srp_template
= {
1947 .module
= THIS_MODULE
,
1948 .name
= "InfiniBand SRP initiator",
1949 .proc_name
= DRV_NAME
,
1950 .slave_configure
= srp_slave_configure
,
1951 .info
= srp_target_info
,
1952 .queuecommand
= srp_queuecommand
,
1953 .eh_abort_handler
= srp_abort
,
1954 .eh_device_reset_handler
= srp_reset_device
,
1955 .eh_host_reset_handler
= srp_reset_host
,
1956 .skip_settle_delay
= true,
1957 .sg_tablesize
= SRP_DEF_SG_TABLESIZE
,
1958 .can_queue
= SRP_CMD_SQ_SIZE
,
1960 .cmd_per_lun
= SRP_CMD_SQ_SIZE
,
1961 .use_clustering
= ENABLE_CLUSTERING
,
1962 .shost_attrs
= srp_host_attrs
1965 static int srp_add_target(struct srp_host
*host
, struct srp_target_port
*target
)
1967 struct srp_rport_identifiers ids
;
1968 struct srp_rport
*rport
;
1970 sprintf(target
->target_name
, "SRP.T10:%016llX",
1971 (unsigned long long) be64_to_cpu(target
->id_ext
));
1973 if (scsi_add_host(target
->scsi_host
, host
->srp_dev
->dev
->dma_device
))
1976 memcpy(ids
.port_id
, &target
->id_ext
, 8);
1977 memcpy(ids
.port_id
+ 8, &target
->ioc_guid
, 8);
1978 ids
.roles
= SRP_RPORT_ROLE_TARGET
;
1979 rport
= srp_rport_add(target
->scsi_host
, &ids
);
1980 if (IS_ERR(rport
)) {
1981 scsi_remove_host(target
->scsi_host
);
1982 return PTR_ERR(rport
);
1985 rport
->lld_data
= target
;
1987 spin_lock(&host
->target_lock
);
1988 list_add_tail(&target
->list
, &host
->target_list
);
1989 spin_unlock(&host
->target_lock
);
1991 target
->state
= SRP_TARGET_LIVE
;
1993 scsi_scan_target(&target
->scsi_host
->shost_gendev
,
1994 0, target
->scsi_id
, SCAN_WILD_CARD
, 0);
1999 static void srp_release_dev(struct device
*dev
)
2001 struct srp_host
*host
=
2002 container_of(dev
, struct srp_host
, dev
);
2004 complete(&host
->released
);
2007 static struct class srp_class
= {
2008 .name
= "infiniband_srp",
2009 .dev_release
= srp_release_dev
2013 * srp_conn_unique() - check whether the connection to a target is unique
2015 static bool srp_conn_unique(struct srp_host
*host
,
2016 struct srp_target_port
*target
)
2018 struct srp_target_port
*t
;
2021 if (target
->state
== SRP_TARGET_REMOVED
)
2026 spin_lock(&host
->target_lock
);
2027 list_for_each_entry(t
, &host
->target_list
, list
) {
2029 target
->id_ext
== t
->id_ext
&&
2030 target
->ioc_guid
== t
->ioc_guid
&&
2031 target
->initiator_ext
== t
->initiator_ext
) {
2036 spin_unlock(&host
->target_lock
);
2043 * Target ports are added by writing
2045 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
2046 * pkey=<P_Key>,service_id=<service ID>
2048 * to the add_target sysfs attribute.
2052 SRP_OPT_ID_EXT
= 1 << 0,
2053 SRP_OPT_IOC_GUID
= 1 << 1,
2054 SRP_OPT_DGID
= 1 << 2,
2055 SRP_OPT_PKEY
= 1 << 3,
2056 SRP_OPT_SERVICE_ID
= 1 << 4,
2057 SRP_OPT_MAX_SECT
= 1 << 5,
2058 SRP_OPT_MAX_CMD_PER_LUN
= 1 << 6,
2059 SRP_OPT_IO_CLASS
= 1 << 7,
2060 SRP_OPT_INITIATOR_EXT
= 1 << 8,
2061 SRP_OPT_CMD_SG_ENTRIES
= 1 << 9,
2062 SRP_OPT_ALLOW_EXT_SG
= 1 << 10,
2063 SRP_OPT_SG_TABLESIZE
= 1 << 11,
2064 SRP_OPT_ALL
= (SRP_OPT_ID_EXT
|
2068 SRP_OPT_SERVICE_ID
),
2071 static const match_table_t srp_opt_tokens
= {
2072 { SRP_OPT_ID_EXT
, "id_ext=%s" },
2073 { SRP_OPT_IOC_GUID
, "ioc_guid=%s" },
2074 { SRP_OPT_DGID
, "dgid=%s" },
2075 { SRP_OPT_PKEY
, "pkey=%x" },
2076 { SRP_OPT_SERVICE_ID
, "service_id=%s" },
2077 { SRP_OPT_MAX_SECT
, "max_sect=%d" },
2078 { SRP_OPT_MAX_CMD_PER_LUN
, "max_cmd_per_lun=%d" },
2079 { SRP_OPT_IO_CLASS
, "io_class=%x" },
2080 { SRP_OPT_INITIATOR_EXT
, "initiator_ext=%s" },
2081 { SRP_OPT_CMD_SG_ENTRIES
, "cmd_sg_entries=%u" },
2082 { SRP_OPT_ALLOW_EXT_SG
, "allow_ext_sg=%u" },
2083 { SRP_OPT_SG_TABLESIZE
, "sg_tablesize=%u" },
2084 { SRP_OPT_ERR
, NULL
}
2087 static int srp_parse_options(const char *buf
, struct srp_target_port
*target
)
2089 char *options
, *sep_opt
;
2092 substring_t args
[MAX_OPT_ARGS
];
2098 options
= kstrdup(buf
, GFP_KERNEL
);
2103 while ((p
= strsep(&sep_opt
, ",")) != NULL
) {
2107 token
= match_token(p
, srp_opt_tokens
, args
);
2111 case SRP_OPT_ID_EXT
:
2112 p
= match_strdup(args
);
2117 target
->id_ext
= cpu_to_be64(simple_strtoull(p
, NULL
, 16));
2121 case SRP_OPT_IOC_GUID
:
2122 p
= match_strdup(args
);
2127 target
->ioc_guid
= cpu_to_be64(simple_strtoull(p
, NULL
, 16));
2132 p
= match_strdup(args
);
2137 if (strlen(p
) != 32) {
2138 pr_warn("bad dest GID parameter '%s'\n", p
);
2143 for (i
= 0; i
< 16; ++i
) {
2144 strlcpy(dgid
, p
+ i
* 2, 3);
2145 target
->path
.dgid
.raw
[i
] = simple_strtoul(dgid
, NULL
, 16);
2148 memcpy(target
->orig_dgid
, target
->path
.dgid
.raw
, 16);
2152 if (match_hex(args
, &token
)) {
2153 pr_warn("bad P_Key parameter '%s'\n", p
);
2156 target
->path
.pkey
= cpu_to_be16(token
);
2159 case SRP_OPT_SERVICE_ID
:
2160 p
= match_strdup(args
);
2165 target
->service_id
= cpu_to_be64(simple_strtoull(p
, NULL
, 16));
2166 target
->path
.service_id
= target
->service_id
;
2170 case SRP_OPT_MAX_SECT
:
2171 if (match_int(args
, &token
)) {
2172 pr_warn("bad max sect parameter '%s'\n", p
);
2175 target
->scsi_host
->max_sectors
= token
;
2178 case SRP_OPT_MAX_CMD_PER_LUN
:
2179 if (match_int(args
, &token
)) {
2180 pr_warn("bad max cmd_per_lun parameter '%s'\n",
2184 target
->scsi_host
->cmd_per_lun
= min(token
, SRP_CMD_SQ_SIZE
);
2187 case SRP_OPT_IO_CLASS
:
2188 if (match_hex(args
, &token
)) {
2189 pr_warn("bad IO class parameter '%s'\n", p
);
2192 if (token
!= SRP_REV10_IB_IO_CLASS
&&
2193 token
!= SRP_REV16A_IB_IO_CLASS
) {
2194 pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n",
2195 token
, SRP_REV10_IB_IO_CLASS
,
2196 SRP_REV16A_IB_IO_CLASS
);
2199 target
->io_class
= token
;
2202 case SRP_OPT_INITIATOR_EXT
:
2203 p
= match_strdup(args
);
2208 target
->initiator_ext
= cpu_to_be64(simple_strtoull(p
, NULL
, 16));
2212 case SRP_OPT_CMD_SG_ENTRIES
:
2213 if (match_int(args
, &token
) || token
< 1 || token
> 255) {
2214 pr_warn("bad max cmd_sg_entries parameter '%s'\n",
2218 target
->cmd_sg_cnt
= token
;
2221 case SRP_OPT_ALLOW_EXT_SG
:
2222 if (match_int(args
, &token
)) {
2223 pr_warn("bad allow_ext_sg parameter '%s'\n", p
);
2226 target
->allow_ext_sg
= !!token
;
2229 case SRP_OPT_SG_TABLESIZE
:
2230 if (match_int(args
, &token
) || token
< 1 ||
2231 token
> SCSI_MAX_SG_CHAIN_SEGMENTS
) {
2232 pr_warn("bad max sg_tablesize parameter '%s'\n",
2236 target
->sg_tablesize
= token
;
2240 pr_warn("unknown parameter or missing value '%s' in target creation request\n",
2246 if ((opt_mask
& SRP_OPT_ALL
) == SRP_OPT_ALL
)
2249 for (i
= 0; i
< ARRAY_SIZE(srp_opt_tokens
); ++i
)
2250 if ((srp_opt_tokens
[i
].token
& SRP_OPT_ALL
) &&
2251 !(srp_opt_tokens
[i
].token
& opt_mask
))
2252 pr_warn("target creation request is missing parameter '%s'\n",
2253 srp_opt_tokens
[i
].pattern
);
2260 static ssize_t
srp_create_target(struct device
*dev
,
2261 struct device_attribute
*attr
,
2262 const char *buf
, size_t count
)
2264 struct srp_host
*host
=
2265 container_of(dev
, struct srp_host
, dev
);
2266 struct Scsi_Host
*target_host
;
2267 struct srp_target_port
*target
;
2268 struct ib_device
*ibdev
= host
->srp_dev
->dev
;
2269 dma_addr_t dma_addr
;
2272 target_host
= scsi_host_alloc(&srp_template
,
2273 sizeof (struct srp_target_port
));
2277 target_host
->transportt
= ib_srp_transport_template
;
2278 target_host
->max_channel
= 0;
2279 target_host
->max_id
= 1;
2280 target_host
->max_lun
= SRP_MAX_LUN
;
2281 target_host
->max_cmd_len
= sizeof ((struct srp_cmd
*) (void *) 0L)->cdb
;
2283 target
= host_to_target(target_host
);
2285 target
->io_class
= SRP_REV16A_IB_IO_CLASS
;
2286 target
->scsi_host
= target_host
;
2287 target
->srp_host
= host
;
2288 target
->lkey
= host
->srp_dev
->mr
->lkey
;
2289 target
->rkey
= host
->srp_dev
->mr
->rkey
;
2290 target
->cmd_sg_cnt
= cmd_sg_entries
;
2291 target
->sg_tablesize
= indirect_sg_entries
? : cmd_sg_entries
;
2292 target
->allow_ext_sg
= allow_ext_sg
;
2294 ret
= srp_parse_options(buf
, target
);
2298 if (!srp_conn_unique(target
->srp_host
, target
)) {
2299 shost_printk(KERN_INFO
, target
->scsi_host
,
2300 PFX
"Already connected to target port with id_ext=%016llx;ioc_guid=%016llx;initiator_ext=%016llx\n",
2301 be64_to_cpu(target
->id_ext
),
2302 be64_to_cpu(target
->ioc_guid
),
2303 be64_to_cpu(target
->initiator_ext
));
2308 if (!host
->srp_dev
->fmr_pool
&& !target
->allow_ext_sg
&&
2309 target
->cmd_sg_cnt
< target
->sg_tablesize
) {
2310 pr_warn("No FMR pool and no external indirect descriptors, limiting sg_tablesize to cmd_sg_cnt\n");
2311 target
->sg_tablesize
= target
->cmd_sg_cnt
;
2314 target_host
->sg_tablesize
= target
->sg_tablesize
;
2315 target
->indirect_size
= target
->sg_tablesize
*
2316 sizeof (struct srp_direct_buf
);
2317 target
->max_iu_len
= sizeof (struct srp_cmd
) +
2318 sizeof (struct srp_indirect_buf
) +
2319 target
->cmd_sg_cnt
* sizeof (struct srp_direct_buf
);
2321 INIT_WORK(&target
->remove_work
, srp_remove_work
);
2322 spin_lock_init(&target
->lock
);
2323 INIT_LIST_HEAD(&target
->free_tx
);
2324 INIT_LIST_HEAD(&target
->free_reqs
);
2325 for (i
= 0; i
< SRP_CMD_SQ_SIZE
; ++i
) {
2326 struct srp_request
*req
= &target
->req_ring
[i
];
2328 req
->fmr_list
= kmalloc(target
->cmd_sg_cnt
* sizeof (void *),
2330 req
->map_page
= kmalloc(SRP_FMR_SIZE
* sizeof (void *),
2332 req
->indirect_desc
= kmalloc(target
->indirect_size
, GFP_KERNEL
);
2333 if (!req
->fmr_list
|| !req
->map_page
|| !req
->indirect_desc
)
2336 dma_addr
= ib_dma_map_single(ibdev
, req
->indirect_desc
,
2337 target
->indirect_size
,
2339 if (ib_dma_mapping_error(ibdev
, dma_addr
))
2342 req
->indirect_dma_addr
= dma_addr
;
2344 list_add_tail(&req
->list
, &target
->free_reqs
);
2347 ib_query_gid(ibdev
, host
->port
, 0, &target
->path
.sgid
);
2349 shost_printk(KERN_DEBUG
, target
->scsi_host
, PFX
2350 "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
2351 "service_id %016llx dgid %pI6\n",
2352 (unsigned long long) be64_to_cpu(target
->id_ext
),
2353 (unsigned long long) be64_to_cpu(target
->ioc_guid
),
2354 be16_to_cpu(target
->path
.pkey
),
2355 (unsigned long long) be64_to_cpu(target
->service_id
),
2356 target
->path
.dgid
.raw
);
2358 ret
= srp_create_target_ib(target
);
2362 ret
= srp_new_cm_id(target
);
2366 ret
= srp_connect_target(target
);
2368 shost_printk(KERN_ERR
, target
->scsi_host
,
2369 PFX
"Connection failed\n");
2373 ret
= srp_add_target(host
, target
);
2375 goto err_disconnect
;
2380 srp_disconnect_target(target
);
2383 ib_destroy_cm_id(target
->cm_id
);
2386 srp_free_target_ib(target
);
2389 srp_free_req_data(target
);
2392 scsi_host_put(target_host
);
2397 static DEVICE_ATTR(add_target
, S_IWUSR
, NULL
, srp_create_target
);
2399 static ssize_t
show_ibdev(struct device
*dev
, struct device_attribute
*attr
,
2402 struct srp_host
*host
= container_of(dev
, struct srp_host
, dev
);
2404 return sprintf(buf
, "%s\n", host
->srp_dev
->dev
->name
);
2407 static DEVICE_ATTR(ibdev
, S_IRUGO
, show_ibdev
, NULL
);
2409 static ssize_t
show_port(struct device
*dev
, struct device_attribute
*attr
,
2412 struct srp_host
*host
= container_of(dev
, struct srp_host
, dev
);
2414 return sprintf(buf
, "%d\n", host
->port
);
2417 static DEVICE_ATTR(port
, S_IRUGO
, show_port
, NULL
);
2419 static struct srp_host
*srp_add_port(struct srp_device
*device
, u8 port
)
2421 struct srp_host
*host
;
2423 host
= kzalloc(sizeof *host
, GFP_KERNEL
);
2427 INIT_LIST_HEAD(&host
->target_list
);
2428 spin_lock_init(&host
->target_lock
);
2429 init_completion(&host
->released
);
2430 host
->srp_dev
= device
;
2433 host
->dev
.class = &srp_class
;
2434 host
->dev
.parent
= device
->dev
->dma_device
;
2435 dev_set_name(&host
->dev
, "srp-%s-%d", device
->dev
->name
, port
);
2437 if (device_register(&host
->dev
))
2439 if (device_create_file(&host
->dev
, &dev_attr_add_target
))
2441 if (device_create_file(&host
->dev
, &dev_attr_ibdev
))
2443 if (device_create_file(&host
->dev
, &dev_attr_port
))
2449 device_unregister(&host
->dev
);
2457 static void srp_add_one(struct ib_device
*device
)
2459 struct srp_device
*srp_dev
;
2460 struct ib_device_attr
*dev_attr
;
2461 struct ib_fmr_pool_param fmr_param
;
2462 struct srp_host
*host
;
2463 int max_pages_per_fmr
, fmr_page_shift
, s
, e
, p
;
2465 dev_attr
= kmalloc(sizeof *dev_attr
, GFP_KERNEL
);
2469 if (ib_query_device(device
, dev_attr
)) {
2470 pr_warn("Query device failed for %s\n", device
->name
);
2474 srp_dev
= kmalloc(sizeof *srp_dev
, GFP_KERNEL
);
2479 * Use the smallest page size supported by the HCA, down to a
2480 * minimum of 4096 bytes. We're unlikely to build large sglists
2481 * out of smaller entries.
2483 fmr_page_shift
= max(12, ffs(dev_attr
->page_size_cap
) - 1);
2484 srp_dev
->fmr_page_size
= 1 << fmr_page_shift
;
2485 srp_dev
->fmr_page_mask
= ~((u64
) srp_dev
->fmr_page_size
- 1);
2486 srp_dev
->fmr_max_size
= srp_dev
->fmr_page_size
* SRP_FMR_SIZE
;
2488 INIT_LIST_HEAD(&srp_dev
->dev_list
);
2490 srp_dev
->dev
= device
;
2491 srp_dev
->pd
= ib_alloc_pd(device
);
2492 if (IS_ERR(srp_dev
->pd
))
2495 srp_dev
->mr
= ib_get_dma_mr(srp_dev
->pd
,
2496 IB_ACCESS_LOCAL_WRITE
|
2497 IB_ACCESS_REMOTE_READ
|
2498 IB_ACCESS_REMOTE_WRITE
);
2499 if (IS_ERR(srp_dev
->mr
))
2502 for (max_pages_per_fmr
= SRP_FMR_SIZE
;
2503 max_pages_per_fmr
>= SRP_FMR_MIN_SIZE
;
2504 max_pages_per_fmr
/= 2, srp_dev
->fmr_max_size
/= 2) {
2505 memset(&fmr_param
, 0, sizeof fmr_param
);
2506 fmr_param
.pool_size
= SRP_FMR_POOL_SIZE
;
2507 fmr_param
.dirty_watermark
= SRP_FMR_DIRTY_SIZE
;
2508 fmr_param
.cache
= 1;
2509 fmr_param
.max_pages_per_fmr
= max_pages_per_fmr
;
2510 fmr_param
.page_shift
= fmr_page_shift
;
2511 fmr_param
.access
= (IB_ACCESS_LOCAL_WRITE
|
2512 IB_ACCESS_REMOTE_WRITE
|
2513 IB_ACCESS_REMOTE_READ
);
2515 srp_dev
->fmr_pool
= ib_create_fmr_pool(srp_dev
->pd
, &fmr_param
);
2516 if (!IS_ERR(srp_dev
->fmr_pool
))
2520 if (IS_ERR(srp_dev
->fmr_pool
))
2521 srp_dev
->fmr_pool
= NULL
;
2523 if (device
->node_type
== RDMA_NODE_IB_SWITCH
) {
2528 e
= device
->phys_port_cnt
;
2531 for (p
= s
; p
<= e
; ++p
) {
2532 host
= srp_add_port(srp_dev
, p
);
2534 list_add_tail(&host
->list
, &srp_dev
->dev_list
);
2537 ib_set_client_data(device
, &srp_client
, srp_dev
);
2542 ib_dealloc_pd(srp_dev
->pd
);
2551 static void srp_remove_one(struct ib_device
*device
)
2553 struct srp_device
*srp_dev
;
2554 struct srp_host
*host
, *tmp_host
;
2555 struct srp_target_port
*target
;
2557 srp_dev
= ib_get_client_data(device
, &srp_client
);
2561 list_for_each_entry_safe(host
, tmp_host
, &srp_dev
->dev_list
, list
) {
2562 device_unregister(&host
->dev
);
2564 * Wait for the sysfs entry to go away, so that no new
2565 * target ports can be created.
2567 wait_for_completion(&host
->released
);
2570 * Remove all target ports.
2572 spin_lock(&host
->target_lock
);
2573 list_for_each_entry(target
, &host
->target_list
, list
)
2574 srp_queue_remove_work(target
);
2575 spin_unlock(&host
->target_lock
);
2578 * Wait for target port removal tasks.
2580 flush_workqueue(system_long_wq
);
2585 if (srp_dev
->fmr_pool
)
2586 ib_destroy_fmr_pool(srp_dev
->fmr_pool
);
2587 ib_dereg_mr(srp_dev
->mr
);
2588 ib_dealloc_pd(srp_dev
->pd
);
2593 static struct srp_function_template ib_srp_transport_functions
= {
2594 .rport_delete
= srp_rport_delete
,
2597 static int __init
srp_init_module(void)
2601 BUILD_BUG_ON(FIELD_SIZEOF(struct ib_wc
, wr_id
) < sizeof(void *));
2603 if (srp_sg_tablesize
) {
2604 pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n");
2605 if (!cmd_sg_entries
)
2606 cmd_sg_entries
= srp_sg_tablesize
;
2609 if (!cmd_sg_entries
)
2610 cmd_sg_entries
= SRP_DEF_SG_TABLESIZE
;
2612 if (cmd_sg_entries
> 255) {
2613 pr_warn("Clamping cmd_sg_entries to 255\n");
2614 cmd_sg_entries
= 255;
2617 if (!indirect_sg_entries
)
2618 indirect_sg_entries
= cmd_sg_entries
;
2619 else if (indirect_sg_entries
< cmd_sg_entries
) {
2620 pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n",
2622 indirect_sg_entries
= cmd_sg_entries
;
2625 ib_srp_transport_template
=
2626 srp_attach_transport(&ib_srp_transport_functions
);
2627 if (!ib_srp_transport_template
)
2630 ret
= class_register(&srp_class
);
2632 pr_err("couldn't register class infiniband_srp\n");
2633 srp_release_transport(ib_srp_transport_template
);
2637 ib_sa_register_client(&srp_sa_client
);
2639 ret
= ib_register_client(&srp_client
);
2641 pr_err("couldn't register IB client\n");
2642 srp_release_transport(ib_srp_transport_template
);
2643 ib_sa_unregister_client(&srp_sa_client
);
2644 class_unregister(&srp_class
);
2651 static void __exit
srp_cleanup_module(void)
2653 ib_unregister_client(&srp_client
);
2654 ib_sa_unregister_client(&srp_sa_client
);
2655 class_unregister(&srp_class
);
2656 srp_release_transport(ib_srp_transport_template
);
2659 module_init(srp_init_module
);
2660 module_exit(srp_cleanup_module
);