2 * Copyright (c) 2005 Cisco Systems. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * $Id: ib_srp.c 3932 2005-11-01 17:19:29Z roland $
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/err.h>
39 #include <linux/string.h>
40 #include <linux/parser.h>
41 #include <linux/random.h>
42 #include <linux/jiffies.h>
44 #include <asm/atomic.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_device.h>
48 #include <scsi/scsi_dbg.h>
50 #include <scsi/scsi_transport_srp.h>
52 #include <rdma/ib_cache.h>
56 #define DRV_NAME "ib_srp"
57 #define PFX DRV_NAME ": "
58 #define DRV_VERSION "0.2"
59 #define DRV_RELDATE "November 1, 2005"
61 MODULE_AUTHOR("Roland Dreier");
62 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
63 "v" DRV_VERSION " (" DRV_RELDATE ")");
64 MODULE_LICENSE("Dual BSD/GPL");
66 static int srp_sg_tablesize = SRP_DEF_SG_TABLESIZE;
67 static int srp_max_iu_len;
69 module_param(srp_sg_tablesize, int, 0444);
70 MODULE_PARM_DESC(srp_sg_tablesize,
71 "Max number of gather/scatter entries per I/O (default is 12, max 255)");
73 static int topspin_workarounds = 1;
75 module_param(topspin_workarounds, int, 0444);
76 MODULE_PARM_DESC(topspin_workarounds,
77 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
79 static int mellanox_workarounds = 1;
81 module_param(mellanox_workarounds, int, 0444);
82 MODULE_PARM_DESC(mellanox_workarounds,
83 "Enable workarounds for Mellanox SRP target bugs if != 0");
85 static void srp_add_one(struct ib_device *device);
86 static void srp_remove_one(struct ib_device *device);
87 static void srp_completion(struct ib_cq *cq, void *target_ptr);
88 static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
90 static struct scsi_transport_template *ib_srp_transport_template;
92 static struct ib_client srp_client = {
95 .remove = srp_remove_one
98 static struct ib_sa_client srp_sa_client;
100 static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
102 return (struct srp_target_port *) host->hostdata;
105 static const char *srp_target_info(struct Scsi_Host *host)
107 return host_to_target(host)->target_name;
110 static int srp_target_is_topspin(struct srp_target_port *target)
112 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
113 static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
115 return topspin_workarounds &&
116 (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
117 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
120 static int srp_target_is_mellanox(struct srp_target_port *target)
122 static const u8 mellanox_oui[3] = { 0x00, 0x02, 0xc9 };
124 return mellanox_workarounds &&
125 !memcmp(&target->ioc_guid, mellanox_oui, sizeof mellanox_oui);
128 static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
130 enum dma_data_direction direction)
134 iu = kmalloc(sizeof *iu, gfp_mask);
138 iu->buf = kzalloc(size, gfp_mask);
142 iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
144 if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
148 iu->direction = direction;
160 static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
165 ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
171 static void srp_qp_event(struct ib_event *event, void *context)
173 printk(KERN_ERR PFX "QP event %d\n", event->event);
176 static int srp_init_qp(struct srp_target_port *target,
179 struct ib_qp_attr *attr;
182 attr = kmalloc(sizeof *attr, GFP_KERNEL);
186 ret = ib_find_cached_pkey(target->srp_host->srp_dev->dev,
187 target->srp_host->port,
188 be16_to_cpu(target->path.pkey),
193 attr->qp_state = IB_QPS_INIT;
194 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
195 IB_ACCESS_REMOTE_WRITE);
196 attr->port_num = target->srp_host->port;
198 ret = ib_modify_qp(qp, attr,
209 static int srp_new_cm_id(struct srp_target_port *target)
211 struct ib_cm_id *new_cm_id;
213 new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
214 srp_cm_handler, target);
215 if (IS_ERR(new_cm_id))
216 return PTR_ERR(new_cm_id);
219 ib_destroy_cm_id(target->cm_id);
220 target->cm_id = new_cm_id;
225 static int srp_create_target_ib(struct srp_target_port *target)
227 struct ib_qp_init_attr *init_attr;
230 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
234 target->cq = ib_create_cq(target->srp_host->srp_dev->dev,
235 srp_completion, NULL, target, SRP_CQ_SIZE, 0);
236 if (IS_ERR(target->cq)) {
237 ret = PTR_ERR(target->cq);
241 ib_req_notify_cq(target->cq, IB_CQ_NEXT_COMP);
243 init_attr->event_handler = srp_qp_event;
244 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
245 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
246 init_attr->cap.max_recv_sge = 1;
247 init_attr->cap.max_send_sge = 1;
248 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
249 init_attr->qp_type = IB_QPT_RC;
250 init_attr->send_cq = target->cq;
251 init_attr->recv_cq = target->cq;
253 target->qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr);
254 if (IS_ERR(target->qp)) {
255 ret = PTR_ERR(target->qp);
256 ib_destroy_cq(target->cq);
260 ret = srp_init_qp(target, target->qp);
262 ib_destroy_qp(target->qp);
263 ib_destroy_cq(target->cq);
272 static void srp_free_target_ib(struct srp_target_port *target)
276 ib_destroy_qp(target->qp);
277 ib_destroy_cq(target->cq);
279 for (i = 0; i < SRP_RQ_SIZE; ++i)
280 srp_free_iu(target->srp_host, target->rx_ring[i]);
281 for (i = 0; i < SRP_SQ_SIZE + 1; ++i)
282 srp_free_iu(target->srp_host, target->tx_ring[i]);
285 static void srp_path_rec_completion(int status,
286 struct ib_sa_path_rec *pathrec,
289 struct srp_target_port *target = target_ptr;
291 target->status = status;
293 shost_printk(KERN_ERR, target->scsi_host,
294 PFX "Got failed path rec status %d\n", status);
296 target->path = *pathrec;
297 complete(&target->done);
300 static int srp_lookup_path(struct srp_target_port *target)
302 target->path.numb_path = 1;
304 init_completion(&target->done);
306 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
307 target->srp_host->srp_dev->dev,
308 target->srp_host->port,
310 IB_SA_PATH_REC_SERVICE_ID |
311 IB_SA_PATH_REC_DGID |
312 IB_SA_PATH_REC_SGID |
313 IB_SA_PATH_REC_NUMB_PATH |
315 SRP_PATH_REC_TIMEOUT_MS,
317 srp_path_rec_completion,
318 target, &target->path_query);
319 if (target->path_query_id < 0)
320 return target->path_query_id;
322 wait_for_completion(&target->done);
324 if (target->status < 0)
325 shost_printk(KERN_WARNING, target->scsi_host,
326 PFX "Path record query failed\n");
328 return target->status;
331 static int srp_send_req(struct srp_target_port *target)
334 struct ib_cm_req_param param;
335 struct srp_login_req priv;
339 req = kzalloc(sizeof *req, GFP_KERNEL);
343 req->param.primary_path = &target->path;
344 req->param.alternate_path = NULL;
345 req->param.service_id = target->service_id;
346 req->param.qp_num = target->qp->qp_num;
347 req->param.qp_type = target->qp->qp_type;
348 req->param.private_data = &req->priv;
349 req->param.private_data_len = sizeof req->priv;
350 req->param.flow_control = 1;
352 get_random_bytes(&req->param.starting_psn, 4);
353 req->param.starting_psn &= 0xffffff;
356 * Pick some arbitrary defaults here; we could make these
357 * module parameters if anyone cared about setting them.
359 req->param.responder_resources = 4;
360 req->param.remote_cm_response_timeout = 20;
361 req->param.local_cm_response_timeout = 20;
362 req->param.retry_count = 7;
363 req->param.rnr_retry_count = 7;
364 req->param.max_cm_retries = 15;
366 req->priv.opcode = SRP_LOGIN_REQ;
368 req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
369 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
370 SRP_BUF_FORMAT_INDIRECT);
372 * In the published SRP specification (draft rev. 16a), the
373 * port identifier format is 8 bytes of ID extension followed
374 * by 8 bytes of GUID. Older drafts put the two halves in the
375 * opposite order, so that the GUID comes first.
377 * Targets conforming to these obsolete drafts can be
378 * recognized by the I/O Class they report.
380 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
381 memcpy(req->priv.initiator_port_id,
382 &target->path.sgid.global.interface_id, 8);
383 memcpy(req->priv.initiator_port_id + 8,
384 &target->initiator_ext, 8);
385 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
386 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
388 memcpy(req->priv.initiator_port_id,
389 &target->initiator_ext, 8);
390 memcpy(req->priv.initiator_port_id + 8,
391 &target->path.sgid.global.interface_id, 8);
392 memcpy(req->priv.target_port_id, &target->id_ext, 8);
393 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
397 * Topspin/Cisco SRP targets will reject our login unless we
398 * zero out the first 8 bytes of our initiator port ID and set
399 * the second 8 bytes to the local node GUID.
401 if (srp_target_is_topspin(target)) {
402 shost_printk(KERN_DEBUG, target->scsi_host,
403 PFX "Topspin/Cisco initiator port ID workaround "
404 "activated for target GUID %016llx\n",
405 (unsigned long long) be64_to_cpu(target->ioc_guid));
406 memset(req->priv.initiator_port_id, 0, 8);
407 memcpy(req->priv.initiator_port_id + 8,
408 &target->srp_host->srp_dev->dev->node_guid, 8);
411 status = ib_send_cm_req(target->cm_id, &req->param);
418 static void srp_disconnect_target(struct srp_target_port *target)
420 /* XXX should send SRP_I_LOGOUT request */
422 init_completion(&target->done);
423 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
424 shost_printk(KERN_DEBUG, target->scsi_host,
425 PFX "Sending CM DREQ failed\n");
428 wait_for_completion(&target->done);
431 static void srp_remove_work(struct work_struct *work)
433 struct srp_target_port *target =
434 container_of(work, struct srp_target_port, work);
436 spin_lock_irq(target->scsi_host->host_lock);
437 if (target->state != SRP_TARGET_DEAD) {
438 spin_unlock_irq(target->scsi_host->host_lock);
441 target->state = SRP_TARGET_REMOVED;
442 spin_unlock_irq(target->scsi_host->host_lock);
444 spin_lock(&target->srp_host->target_lock);
445 list_del(&target->list);
446 spin_unlock(&target->srp_host->target_lock);
448 srp_remove_host(target->scsi_host);
449 scsi_remove_host(target->scsi_host);
450 ib_destroy_cm_id(target->cm_id);
451 srp_free_target_ib(target);
452 scsi_host_put(target->scsi_host);
455 static int srp_connect_target(struct srp_target_port *target)
460 ret = srp_lookup_path(target);
465 init_completion(&target->done);
466 ret = srp_send_req(target);
469 wait_for_completion(&target->done);
472 * The CM event handling code will set status to
473 * SRP_PORT_REDIRECT if we get a port redirect REJ
474 * back, or SRP_DLID_REDIRECT if we get a lid/qp
477 switch (target->status) {
481 case SRP_PORT_REDIRECT:
482 ret = srp_lookup_path(target);
487 case SRP_DLID_REDIRECT:
491 /* Our current CM id was stale, and is now in timewait.
492 * Try to reconnect with a new one.
494 if (!retries-- || srp_new_cm_id(target)) {
495 shost_printk(KERN_ERR, target->scsi_host, PFX
496 "giving up on stale connection\n");
497 target->status = -ECONNRESET;
498 return target->status;
501 shost_printk(KERN_ERR, target->scsi_host, PFX
502 "retrying stale connection\n");
506 return target->status;
511 static void srp_unmap_data(struct scsi_cmnd *scmnd,
512 struct srp_target_port *target,
513 struct srp_request *req)
515 if (!scsi_sglist(scmnd) ||
516 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
517 scmnd->sc_data_direction != DMA_FROM_DEVICE))
521 ib_fmr_pool_unmap(req->fmr);
525 ib_dma_unmap_sg(target->srp_host->srp_dev->dev, scsi_sglist(scmnd),
526 scsi_sg_count(scmnd), scmnd->sc_data_direction);
529 static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
531 srp_unmap_data(req->scmnd, target, req);
532 list_move_tail(&req->list, &target->free_reqs);
535 static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
537 req->scmnd->result = DID_RESET << 16;
538 req->scmnd->scsi_done(req->scmnd);
539 srp_remove_req(target, req);
542 static int srp_reconnect_target(struct srp_target_port *target)
544 struct ib_qp_attr qp_attr;
545 struct srp_request *req, *tmp;
549 spin_lock_irq(target->scsi_host->host_lock);
550 if (target->state != SRP_TARGET_LIVE) {
551 spin_unlock_irq(target->scsi_host->host_lock);
554 target->state = SRP_TARGET_CONNECTING;
555 spin_unlock_irq(target->scsi_host->host_lock);
557 srp_disconnect_target(target);
559 * Now get a new local CM ID so that we avoid confusing the
560 * target in case things are really fouled up.
562 ret = srp_new_cm_id(target);
566 qp_attr.qp_state = IB_QPS_RESET;
567 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
571 ret = srp_init_qp(target, target->qp);
575 while (ib_poll_cq(target->cq, 1, &wc) > 0)
578 spin_lock_irq(target->scsi_host->host_lock);
579 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
580 srp_reset_req(target, req);
581 spin_unlock_irq(target->scsi_host->host_lock);
587 target->qp_in_error = 0;
588 ret = srp_connect_target(target);
592 spin_lock_irq(target->scsi_host->host_lock);
593 if (target->state == SRP_TARGET_CONNECTING) {
595 target->state = SRP_TARGET_LIVE;
598 spin_unlock_irq(target->scsi_host->host_lock);
603 shost_printk(KERN_ERR, target->scsi_host,
604 PFX "reconnect failed (%d), removing target port.\n", ret);
607 * We couldn't reconnect, so kill our target port off.
608 * However, we have to defer the real removal because we might
609 * be in the context of the SCSI error handler now, which
610 * would deadlock if we call scsi_remove_host().
612 spin_lock_irq(target->scsi_host->host_lock);
613 if (target->state == SRP_TARGET_CONNECTING) {
614 target->state = SRP_TARGET_DEAD;
615 INIT_WORK(&target->work, srp_remove_work);
616 schedule_work(&target->work);
618 spin_unlock_irq(target->scsi_host->host_lock);
623 static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat,
624 int sg_cnt, struct srp_request *req,
625 struct srp_direct_buf *buf)
633 struct srp_device *dev = target->srp_host->srp_dev;
634 struct ib_device *ibdev = dev->dev;
635 struct scatterlist *sg;
640 if (srp_target_is_mellanox(target) &&
641 (ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask))
645 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
646 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
648 if (ib_sg_dma_address(ibdev, sg) & ~dev->fmr_page_mask) {
654 if ((ib_sg_dma_address(ibdev, sg) + dma_len) &
655 ~dev->fmr_page_mask) {
665 page_cnt += len >> dev->fmr_page_shift;
666 if (page_cnt > SRP_FMR_SIZE)
669 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
674 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
675 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
677 for (j = 0; j < dma_len; j += dev->fmr_page_size)
678 dma_pages[page_cnt++] =
679 (ib_sg_dma_address(ibdev, sg) &
680 dev->fmr_page_mask) + j;
683 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
684 dma_pages, page_cnt, io_addr);
685 if (IS_ERR(req->fmr)) {
686 ret = PTR_ERR(req->fmr);
691 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) &
692 ~dev->fmr_page_mask);
693 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
694 buf->len = cpu_to_be32(len);
704 static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
705 struct srp_request *req)
707 struct scatterlist *scat;
708 struct srp_cmd *cmd = req->cmd->buf;
709 int len, nents, count;
710 u8 fmt = SRP_DATA_DESC_DIRECT;
711 struct srp_device *dev;
712 struct ib_device *ibdev;
714 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
715 return sizeof (struct srp_cmd);
717 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
718 scmnd->sc_data_direction != DMA_TO_DEVICE) {
719 shost_printk(KERN_WARNING, target->scsi_host,
720 PFX "Unhandled data direction %d\n",
721 scmnd->sc_data_direction);
725 nents = scsi_sg_count(scmnd);
726 scat = scsi_sglist(scmnd);
728 dev = target->srp_host->srp_dev;
731 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
733 fmt = SRP_DATA_DESC_DIRECT;
734 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
738 * The midlayer only generated a single gather/scatter
739 * entry, or DMA mapping coalesced everything to a
740 * single entry. So a direct descriptor along with
741 * the DMA MR suffices.
743 struct srp_direct_buf *buf = (void *) cmd->add_data;
745 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
746 buf->key = cpu_to_be32(dev->mr->rkey);
747 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
748 } else if (srp_map_fmr(target, scat, count, req,
749 (void *) cmd->add_data)) {
751 * FMR mapping failed, and the scatterlist has more
752 * than one entry. Generate an indirect memory
755 struct srp_indirect_buf *buf = (void *) cmd->add_data;
756 struct scatterlist *sg;
760 fmt = SRP_DATA_DESC_INDIRECT;
761 len = sizeof (struct srp_cmd) +
762 sizeof (struct srp_indirect_buf) +
763 count * sizeof (struct srp_direct_buf);
765 scsi_for_each_sg(scmnd, sg, count, i) {
766 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
768 buf->desc_list[i].va =
769 cpu_to_be64(ib_sg_dma_address(ibdev, sg));
770 buf->desc_list[i].key =
771 cpu_to_be32(dev->mr->rkey);
772 buf->desc_list[i].len = cpu_to_be32(dma_len);
776 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
777 cmd->data_out_desc_cnt = count;
779 cmd->data_in_desc_cnt = count;
782 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
783 buf->table_desc.key =
784 cpu_to_be32(target->srp_host->srp_dev->mr->rkey);
785 buf->table_desc.len =
786 cpu_to_be32(count * sizeof (struct srp_direct_buf));
788 buf->len = cpu_to_be32(datalen);
791 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
792 cmd->buf_fmt = fmt << 4;
799 static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
801 struct srp_request *req;
802 struct scsi_cmnd *scmnd;
806 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
808 spin_lock_irqsave(target->scsi_host->host_lock, flags);
810 target->req_lim += delta;
812 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
814 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
815 if (be32_to_cpu(rsp->resp_data_len) < 4)
816 req->tsk_status = -1;
818 req->tsk_status = rsp->data[3];
819 complete(&req->done);
823 shost_printk(KERN_ERR, target->scsi_host,
824 "Null scmnd for RSP w/tag %016llx\n",
825 (unsigned long long) rsp->tag);
826 scmnd->result = rsp->status;
828 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
829 memcpy(scmnd->sense_buffer, rsp->data +
830 be32_to_cpu(rsp->resp_data_len),
831 min_t(int, be32_to_cpu(rsp->sense_data_len),
832 SCSI_SENSE_BUFFERSIZE));
835 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
836 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
837 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
838 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
840 if (!req->tsk_mgmt) {
841 scmnd->host_scribble = (void *) -1L;
842 scmnd->scsi_done(scmnd);
844 srp_remove_req(target, req);
849 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
852 static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
854 struct ib_device *dev;
858 iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
860 dev = target->srp_host->srp_dev->dev;
861 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
864 opcode = *(u8 *) iu->buf;
869 shost_printk(KERN_ERR, target->scsi_host,
870 PFX "recv completion, opcode 0x%02x\n", opcode);
872 for (i = 0; i < wc->byte_len; ++i) {
874 printk(KERN_ERR " [%02x] ", i);
875 printk(" %02x", ((u8 *) iu->buf)[i]);
876 if ((i + 1) % 8 == 0)
880 if (wc->byte_len % 8)
886 srp_process_rsp(target, iu->buf);
890 /* XXX Handle target logout */
891 shost_printk(KERN_WARNING, target->scsi_host,
892 PFX "Got target logout request\n");
896 shost_printk(KERN_WARNING, target->scsi_host,
897 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
901 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
905 static void srp_completion(struct ib_cq *cq, void *target_ptr)
907 struct srp_target_port *target = target_ptr;
910 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
911 while (ib_poll_cq(cq, 1, &wc) > 0) {
913 shost_printk(KERN_ERR, target->scsi_host,
914 PFX "failed %s status %d\n",
915 wc.wr_id & SRP_OP_RECV ? "receive" : "send",
917 target->qp_in_error = 1;
921 if (wc.wr_id & SRP_OP_RECV)
922 srp_handle_recv(target, &wc);
928 static int __srp_post_recv(struct srp_target_port *target)
932 struct ib_recv_wr wr, *bad_wr;
936 next = target->rx_head & (SRP_RQ_SIZE - 1);
937 wr.wr_id = next | SRP_OP_RECV;
938 iu = target->rx_ring[next];
941 list.length = iu->size;
942 list.lkey = target->srp_host->srp_dev->mr->lkey;
948 ret = ib_post_recv(target->qp, &wr, &bad_wr);
955 static int srp_post_recv(struct srp_target_port *target)
960 spin_lock_irqsave(target->scsi_host->host_lock, flags);
961 ret = __srp_post_recv(target);
962 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
968 * Must be called with target->scsi_host->host_lock held to protect
969 * req_lim and tx_head. Lock cannot be dropped between call here and
970 * call to __srp_post_send().
972 static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
973 enum srp_request_type req_type)
975 s32 min = (req_type == SRP_REQ_TASK_MGMT) ? 1 : 2;
977 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
980 if (target->req_lim < min) {
981 ++target->zero_req_lim;
985 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
989 * Must be called with target->scsi_host->host_lock held to protect
990 * req_lim and tx_head.
992 static int __srp_post_send(struct srp_target_port *target,
993 struct srp_iu *iu, int len)
996 struct ib_send_wr wr, *bad_wr;
1001 list.lkey = target->srp_host->srp_dev->mr->lkey;
1004 wr.wr_id = target->tx_head & SRP_SQ_SIZE;
1007 wr.opcode = IB_WR_SEND;
1008 wr.send_flags = IB_SEND_SIGNALED;
1010 ret = ib_post_send(target->qp, &wr, &bad_wr);
1020 static int srp_queuecommand(struct scsi_cmnd *scmnd,
1021 void (*done)(struct scsi_cmnd *))
1023 struct srp_target_port *target = host_to_target(scmnd->device->host);
1024 struct srp_request *req;
1026 struct srp_cmd *cmd;
1027 struct ib_device *dev;
1030 if (target->state == SRP_TARGET_CONNECTING)
1033 if (target->state == SRP_TARGET_DEAD ||
1034 target->state == SRP_TARGET_REMOVED) {
1035 scmnd->result = DID_BAD_TARGET << 16;
1040 iu = __srp_get_tx_iu(target, SRP_REQ_NORMAL);
1044 dev = target->srp_host->srp_dev->dev;
1045 ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len,
1048 req = list_entry(target->free_reqs.next, struct srp_request, list);
1050 scmnd->scsi_done = done;
1052 scmnd->host_scribble = (void *) (long) req->index;
1055 memset(cmd, 0, sizeof *cmd);
1057 cmd->opcode = SRP_CMD;
1058 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
1059 cmd->tag = req->index;
1060 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1065 req->tsk_mgmt = NULL;
1067 len = srp_map_data(scmnd, target, req);
1069 shost_printk(KERN_ERR, target->scsi_host,
1070 PFX "Failed to map data\n");
1074 if (__srp_post_recv(target)) {
1075 shost_printk(KERN_ERR, target->scsi_host, PFX "Recv failed\n");
1079 ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len,
1082 if (__srp_post_send(target, iu, len)) {
1083 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
1087 list_move_tail(&req->list, &target->req_queue);
1092 srp_unmap_data(scmnd, target, req);
1095 return SCSI_MLQUEUE_HOST_BUSY;
1098 static int srp_alloc_iu_bufs(struct srp_target_port *target)
1102 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1103 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1104 target->max_ti_iu_len,
1105 GFP_KERNEL, DMA_FROM_DEVICE);
1106 if (!target->rx_ring[i])
1110 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1111 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
1113 GFP_KERNEL, DMA_TO_DEVICE);
1114 if (!target->tx_ring[i])
1121 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1122 srp_free_iu(target->srp_host, target->rx_ring[i]);
1123 target->rx_ring[i] = NULL;
1126 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1127 srp_free_iu(target->srp_host, target->tx_ring[i]);
1128 target->tx_ring[i] = NULL;
1134 static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1135 struct ib_cm_event *event,
1136 struct srp_target_port *target)
1138 struct Scsi_Host *shost = target->scsi_host;
1139 struct ib_class_port_info *cpi;
1142 switch (event->param.rej_rcvd.reason) {
1143 case IB_CM_REJ_PORT_CM_REDIRECT:
1144 cpi = event->param.rej_rcvd.ari;
1145 target->path.dlid = cpi->redirect_lid;
1146 target->path.pkey = cpi->redirect_pkey;
1147 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1148 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1150 target->status = target->path.dlid ?
1151 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1154 case IB_CM_REJ_PORT_REDIRECT:
1155 if (srp_target_is_topspin(target)) {
1157 * Topspin/Cisco SRP gateways incorrectly send
1158 * reject reason code 25 when they mean 24
1161 memcpy(target->path.dgid.raw,
1162 event->param.rej_rcvd.ari, 16);
1164 shost_printk(KERN_DEBUG, shost,
1165 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1166 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1167 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1169 target->status = SRP_PORT_REDIRECT;
1171 shost_printk(KERN_WARNING, shost,
1172 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1173 target->status = -ECONNRESET;
1177 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
1178 shost_printk(KERN_WARNING, shost,
1179 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1180 target->status = -ECONNRESET;
1183 case IB_CM_REJ_CONSUMER_DEFINED:
1184 opcode = *(u8 *) event->private_data;
1185 if (opcode == SRP_LOGIN_REJ) {
1186 struct srp_login_rej *rej = event->private_data;
1187 u32 reason = be32_to_cpu(rej->reason);
1189 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
1190 shost_printk(KERN_WARNING, shost,
1191 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1193 shost_printk(KERN_WARNING, shost,
1194 PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
1196 shost_printk(KERN_WARNING, shost,
1197 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1198 " opcode 0x%02x\n", opcode);
1199 target->status = -ECONNRESET;
1202 case IB_CM_REJ_STALE_CONN:
1203 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
1204 target->status = SRP_STALE_CONN;
1208 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
1209 event->param.rej_rcvd.reason);
1210 target->status = -ECONNRESET;
1214 static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1216 struct srp_target_port *target = cm_id->context;
1217 struct ib_qp_attr *qp_attr = NULL;
1222 switch (event->event) {
1223 case IB_CM_REQ_ERROR:
1224 shost_printk(KERN_DEBUG, target->scsi_host,
1225 PFX "Sending CM REQ failed\n");
1227 target->status = -ECONNRESET;
1230 case IB_CM_REP_RECEIVED:
1232 opcode = *(u8 *) event->private_data;
1234 if (opcode == SRP_LOGIN_RSP) {
1235 struct srp_login_rsp *rsp = event->private_data;
1237 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1238 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1240 target->scsi_host->can_queue = min(target->req_lim,
1241 target->scsi_host->can_queue);
1243 shost_printk(KERN_WARNING, target->scsi_host,
1244 PFX "Unhandled RSP opcode %#x\n", opcode);
1245 target->status = -ECONNRESET;
1249 if (!target->rx_ring[0]) {
1250 target->status = srp_alloc_iu_bufs(target);
1255 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1257 target->status = -ENOMEM;
1261 qp_attr->qp_state = IB_QPS_RTR;
1262 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1266 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1270 target->status = srp_post_recv(target);
1274 qp_attr->qp_state = IB_QPS_RTS;
1275 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1279 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1283 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1289 case IB_CM_REJ_RECEIVED:
1290 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
1293 srp_cm_rej_handler(cm_id, event, target);
1296 case IB_CM_DREQ_RECEIVED:
1297 shost_printk(KERN_WARNING, target->scsi_host,
1298 PFX "DREQ received - connection closed\n");
1299 if (ib_send_cm_drep(cm_id, NULL, 0))
1300 shost_printk(KERN_ERR, target->scsi_host,
1301 PFX "Sending CM DREP failed\n");
1304 case IB_CM_TIMEWAIT_EXIT:
1305 shost_printk(KERN_ERR, target->scsi_host,
1306 PFX "connection closed\n");
1312 case IB_CM_MRA_RECEIVED:
1313 case IB_CM_DREQ_ERROR:
1314 case IB_CM_DREP_RECEIVED:
1318 shost_printk(KERN_WARNING, target->scsi_host,
1319 PFX "Unhandled CM event %d\n", event->event);
1324 complete(&target->done);
1331 static int srp_send_tsk_mgmt(struct srp_target_port *target,
1332 struct srp_request *req, u8 func)
1335 struct srp_tsk_mgmt *tsk_mgmt;
1337 spin_lock_irq(target->scsi_host->host_lock);
1339 if (target->state == SRP_TARGET_DEAD ||
1340 target->state == SRP_TARGET_REMOVED) {
1341 req->scmnd->result = DID_BAD_TARGET << 16;
1345 init_completion(&req->done);
1347 iu = __srp_get_tx_iu(target, SRP_REQ_TASK_MGMT);
1352 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1354 tsk_mgmt->opcode = SRP_TSK_MGMT;
1355 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1356 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
1357 tsk_mgmt->tsk_mgmt_func = func;
1358 tsk_mgmt->task_tag = req->index;
1360 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1365 spin_unlock_irq(target->scsi_host->host_lock);
1367 if (!wait_for_completion_timeout(&req->done,
1368 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
1374 spin_unlock_irq(target->scsi_host->host_lock);
1378 static int srp_find_req(struct srp_target_port *target,
1379 struct scsi_cmnd *scmnd,
1380 struct srp_request **req)
1382 if (scmnd->host_scribble == (void *) -1L)
1385 *req = &target->req_ring[(long) scmnd->host_scribble];
1390 static int srp_abort(struct scsi_cmnd *scmnd)
1392 struct srp_target_port *target = host_to_target(scmnd->device->host);
1393 struct srp_request *req;
1396 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
1398 if (target->qp_in_error)
1400 if (srp_find_req(target, scmnd, &req))
1402 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1405 spin_lock_irq(target->scsi_host->host_lock);
1407 if (req->cmd_done) {
1408 srp_remove_req(target, req);
1409 scmnd->scsi_done(scmnd);
1410 } else if (!req->tsk_status) {
1411 srp_remove_req(target, req);
1412 scmnd->result = DID_ABORT << 16;
1416 spin_unlock_irq(target->scsi_host->host_lock);
1421 static int srp_reset_device(struct scsi_cmnd *scmnd)
1423 struct srp_target_port *target = host_to_target(scmnd->device->host);
1424 struct srp_request *req, *tmp;
1426 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
1428 if (target->qp_in_error)
1430 if (srp_find_req(target, scmnd, &req))
1432 if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1434 if (req->tsk_status)
1437 spin_lock_irq(target->scsi_host->host_lock);
1439 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
1440 if (req->scmnd->device == scmnd->device)
1441 srp_reset_req(target, req);
1443 spin_unlock_irq(target->scsi_host->host_lock);
1448 static int srp_reset_host(struct scsi_cmnd *scmnd)
1450 struct srp_target_port *target = host_to_target(scmnd->device->host);
1453 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
1455 if (!srp_reconnect_target(target))
1461 static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
1464 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1466 if (target->state == SRP_TARGET_DEAD ||
1467 target->state == SRP_TARGET_REMOVED)
1470 return sprintf(buf, "0x%016llx\n",
1471 (unsigned long long) be64_to_cpu(target->id_ext));
1474 static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
1477 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1479 if (target->state == SRP_TARGET_DEAD ||
1480 target->state == SRP_TARGET_REMOVED)
1483 return sprintf(buf, "0x%016llx\n",
1484 (unsigned long long) be64_to_cpu(target->ioc_guid));
1487 static ssize_t show_service_id(struct device *dev,
1488 struct device_attribute *attr, char *buf)
1490 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1492 if (target->state == SRP_TARGET_DEAD ||
1493 target->state == SRP_TARGET_REMOVED)
1496 return sprintf(buf, "0x%016llx\n",
1497 (unsigned long long) be64_to_cpu(target->service_id));
1500 static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
1503 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1505 if (target->state == SRP_TARGET_DEAD ||
1506 target->state == SRP_TARGET_REMOVED)
1509 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1512 static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
1515 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1517 if (target->state == SRP_TARGET_DEAD ||
1518 target->state == SRP_TARGET_REMOVED)
1521 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1522 be16_to_cpu(((__be16 *) target->path.dgid.raw)[0]),
1523 be16_to_cpu(((__be16 *) target->path.dgid.raw)[1]),
1524 be16_to_cpu(((__be16 *) target->path.dgid.raw)[2]),
1525 be16_to_cpu(((__be16 *) target->path.dgid.raw)[3]),
1526 be16_to_cpu(((__be16 *) target->path.dgid.raw)[4]),
1527 be16_to_cpu(((__be16 *) target->path.dgid.raw)[5]),
1528 be16_to_cpu(((__be16 *) target->path.dgid.raw)[6]),
1529 be16_to_cpu(((__be16 *) target->path.dgid.raw)[7]));
1532 static ssize_t show_orig_dgid(struct device *dev,
1533 struct device_attribute *attr, char *buf)
1535 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1537 if (target->state == SRP_TARGET_DEAD ||
1538 target->state == SRP_TARGET_REMOVED)
1541 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1542 be16_to_cpu(target->orig_dgid[0]),
1543 be16_to_cpu(target->orig_dgid[1]),
1544 be16_to_cpu(target->orig_dgid[2]),
1545 be16_to_cpu(target->orig_dgid[3]),
1546 be16_to_cpu(target->orig_dgid[4]),
1547 be16_to_cpu(target->orig_dgid[5]),
1548 be16_to_cpu(target->orig_dgid[6]),
1549 be16_to_cpu(target->orig_dgid[7]));
1552 static ssize_t show_zero_req_lim(struct device *dev,
1553 struct device_attribute *attr, char *buf)
1555 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1557 if (target->state == SRP_TARGET_DEAD ||
1558 target->state == SRP_TARGET_REMOVED)
1561 return sprintf(buf, "%d\n", target->zero_req_lim);
1564 static ssize_t show_local_ib_port(struct device *dev,
1565 struct device_attribute *attr, char *buf)
1567 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1569 return sprintf(buf, "%d\n", target->srp_host->port);
1572 static ssize_t show_local_ib_device(struct device *dev,
1573 struct device_attribute *attr, char *buf)
1575 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1577 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
1580 static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1581 static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1582 static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1583 static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1584 static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1585 static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
1586 static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1587 static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1588 static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
1590 static struct device_attribute *srp_host_attrs[] = {
1593 &dev_attr_service_id,
1596 &dev_attr_orig_dgid,
1597 &dev_attr_zero_req_lim,
1598 &dev_attr_local_ib_port,
1599 &dev_attr_local_ib_device,
1603 static struct scsi_host_template srp_template = {
1604 .module = THIS_MODULE,
1605 .name = "InfiniBand SRP initiator",
1606 .proc_name = DRV_NAME,
1607 .info = srp_target_info,
1608 .queuecommand = srp_queuecommand,
1609 .eh_abort_handler = srp_abort,
1610 .eh_device_reset_handler = srp_reset_device,
1611 .eh_host_reset_handler = srp_reset_host,
1612 .can_queue = SRP_SQ_SIZE,
1614 .cmd_per_lun = SRP_SQ_SIZE,
1615 .use_clustering = ENABLE_CLUSTERING,
1616 .shost_attrs = srp_host_attrs
1619 static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1621 struct srp_rport_identifiers ids;
1622 struct srp_rport *rport;
1624 sprintf(target->target_name, "SRP.T10:%016llX",
1625 (unsigned long long) be64_to_cpu(target->id_ext));
1627 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
1630 memcpy(ids.port_id, &target->id_ext, 8);
1631 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
1632 ids.roles = SRP_RPORT_ROLE_TARGET;
1633 rport = srp_rport_add(target->scsi_host, &ids);
1634 if (IS_ERR(rport)) {
1635 scsi_remove_host(target->scsi_host);
1636 return PTR_ERR(rport);
1639 spin_lock(&host->target_lock);
1640 list_add_tail(&target->list, &host->target_list);
1641 spin_unlock(&host->target_lock);
1643 target->state = SRP_TARGET_LIVE;
1645 scsi_scan_target(&target->scsi_host->shost_gendev,
1646 0, target->scsi_id, SCAN_WILD_CARD, 0);
1651 static void srp_release_dev(struct device *dev)
1653 struct srp_host *host =
1654 container_of(dev, struct srp_host, dev);
1656 complete(&host->released);
1659 static struct class srp_class = {
1660 .name = "infiniband_srp",
1661 .dev_release = srp_release_dev
1665 * Target ports are added by writing
1667 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1668 * pkey=<P_Key>,service_id=<service ID>
1670 * to the add_target sysfs attribute.
1674 SRP_OPT_ID_EXT = 1 << 0,
1675 SRP_OPT_IOC_GUID = 1 << 1,
1676 SRP_OPT_DGID = 1 << 2,
1677 SRP_OPT_PKEY = 1 << 3,
1678 SRP_OPT_SERVICE_ID = 1 << 4,
1679 SRP_OPT_MAX_SECT = 1 << 5,
1680 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
1681 SRP_OPT_IO_CLASS = 1 << 7,
1682 SRP_OPT_INITIATOR_EXT = 1 << 8,
1683 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1687 SRP_OPT_SERVICE_ID),
1690 static match_table_t srp_opt_tokens = {
1691 { SRP_OPT_ID_EXT, "id_ext=%s" },
1692 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1693 { SRP_OPT_DGID, "dgid=%s" },
1694 { SRP_OPT_PKEY, "pkey=%x" },
1695 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1696 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1697 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
1698 { SRP_OPT_IO_CLASS, "io_class=%x" },
1699 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
1700 { SRP_OPT_ERR, NULL }
1703 static int srp_parse_options(const char *buf, struct srp_target_port *target)
1705 char *options, *sep_opt;
1708 substring_t args[MAX_OPT_ARGS];
1714 options = kstrdup(buf, GFP_KERNEL);
1719 while ((p = strsep(&sep_opt, ",")) != NULL) {
1723 token = match_token(p, srp_opt_tokens, args);
1727 case SRP_OPT_ID_EXT:
1728 p = match_strdup(args);
1733 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1737 case SRP_OPT_IOC_GUID:
1738 p = match_strdup(args);
1743 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1748 p = match_strdup(args);
1753 if (strlen(p) != 32) {
1754 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
1759 for (i = 0; i < 16; ++i) {
1760 strlcpy(dgid, p + i * 2, 3);
1761 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1764 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
1768 if (match_hex(args, &token)) {
1769 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1772 target->path.pkey = cpu_to_be16(token);
1775 case SRP_OPT_SERVICE_ID:
1776 p = match_strdup(args);
1781 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
1782 target->path.service_id = target->service_id;
1786 case SRP_OPT_MAX_SECT:
1787 if (match_int(args, &token)) {
1788 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1791 target->scsi_host->max_sectors = token;
1794 case SRP_OPT_MAX_CMD_PER_LUN:
1795 if (match_int(args, &token)) {
1796 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1799 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1802 case SRP_OPT_IO_CLASS:
1803 if (match_hex(args, &token)) {
1804 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1807 if (token != SRP_REV10_IB_IO_CLASS &&
1808 token != SRP_REV16A_IB_IO_CLASS) {
1809 printk(KERN_WARNING PFX "unknown IO class parameter value"
1810 " %x specified (use %x or %x).\n",
1811 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1814 target->io_class = token;
1817 case SRP_OPT_INITIATOR_EXT:
1818 p = match_strdup(args);
1823 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1828 printk(KERN_WARNING PFX "unknown parameter or missing value "
1829 "'%s' in target creation request\n", p);
1834 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1837 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1838 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1839 !(srp_opt_tokens[i].token & opt_mask))
1840 printk(KERN_WARNING PFX "target creation request is "
1841 "missing parameter '%s'\n",
1842 srp_opt_tokens[i].pattern);
1849 static ssize_t srp_create_target(struct device *dev,
1850 struct device_attribute *attr,
1851 const char *buf, size_t count)
1853 struct srp_host *host =
1854 container_of(dev, struct srp_host, dev);
1855 struct Scsi_Host *target_host;
1856 struct srp_target_port *target;
1860 target_host = scsi_host_alloc(&srp_template,
1861 sizeof (struct srp_target_port));
1865 target_host->transportt = ib_srp_transport_template;
1866 target_host->max_lun = SRP_MAX_LUN;
1867 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
1869 target = host_to_target(target_host);
1871 target->io_class = SRP_REV16A_IB_IO_CLASS;
1872 target->scsi_host = target_host;
1873 target->srp_host = host;
1875 INIT_LIST_HEAD(&target->free_reqs);
1876 INIT_LIST_HEAD(&target->req_queue);
1877 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1878 target->req_ring[i].index = i;
1879 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1882 ret = srp_parse_options(buf, target);
1886 ib_get_cached_gid(host->srp_dev->dev, host->port, 0,
1887 &target->path.sgid);
1889 shost_printk(KERN_DEBUG, target->scsi_host, PFX
1890 "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1891 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1892 (unsigned long long) be64_to_cpu(target->id_ext),
1893 (unsigned long long) be64_to_cpu(target->ioc_guid),
1894 be16_to_cpu(target->path.pkey),
1895 (unsigned long long) be64_to_cpu(target->service_id),
1896 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]),
1897 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]),
1898 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]),
1899 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]),
1900 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]),
1901 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]),
1902 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]),
1903 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14]));
1905 ret = srp_create_target_ib(target);
1909 ret = srp_new_cm_id(target);
1913 target->qp_in_error = 0;
1914 ret = srp_connect_target(target);
1916 shost_printk(KERN_ERR, target->scsi_host,
1917 PFX "Connection failed\n");
1921 ret = srp_add_target(host, target);
1923 goto err_disconnect;
1928 srp_disconnect_target(target);
1931 ib_destroy_cm_id(target->cm_id);
1934 srp_free_target_ib(target);
1937 scsi_host_put(target_host);
1942 static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
1944 static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
1947 struct srp_host *host = container_of(dev, struct srp_host, dev);
1949 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
1952 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1954 static ssize_t show_port(struct device *dev, struct device_attribute *attr,
1957 struct srp_host *host = container_of(dev, struct srp_host, dev);
1959 return sprintf(buf, "%d\n", host->port);
1962 static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
1964 static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
1966 struct srp_host *host;
1968 host = kzalloc(sizeof *host, GFP_KERNEL);
1972 INIT_LIST_HEAD(&host->target_list);
1973 spin_lock_init(&host->target_lock);
1974 init_completion(&host->released);
1975 host->srp_dev = device;
1978 host->dev.class = &srp_class;
1979 host->dev.parent = device->dev->dma_device;
1980 snprintf(host->dev.bus_id, BUS_ID_SIZE, "srp-%s-%d",
1981 device->dev->name, port);
1983 if (device_register(&host->dev))
1985 if (device_create_file(&host->dev, &dev_attr_add_target))
1987 if (device_create_file(&host->dev, &dev_attr_ibdev))
1989 if (device_create_file(&host->dev, &dev_attr_port))
1995 device_unregister(&host->dev);
2003 static void srp_add_one(struct ib_device *device)
2005 struct srp_device *srp_dev;
2006 struct ib_device_attr *dev_attr;
2007 struct ib_fmr_pool_param fmr_param;
2008 struct srp_host *host;
2011 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
2015 if (ib_query_device(device, dev_attr)) {
2016 printk(KERN_WARNING PFX "Query device failed for %s\n",
2021 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
2026 * Use the smallest page size supported by the HCA, down to a
2027 * minimum of 512 bytes (which is the smallest sector that a
2028 * SCSI command will ever carry).
2030 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
2031 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
2032 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
2034 INIT_LIST_HEAD(&srp_dev->dev_list);
2036 srp_dev->dev = device;
2037 srp_dev->pd = ib_alloc_pd(device);
2038 if (IS_ERR(srp_dev->pd))
2041 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
2042 IB_ACCESS_LOCAL_WRITE |
2043 IB_ACCESS_REMOTE_READ |
2044 IB_ACCESS_REMOTE_WRITE);
2045 if (IS_ERR(srp_dev->mr))
2048 memset(&fmr_param, 0, sizeof fmr_param);
2049 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
2050 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
2051 fmr_param.cache = 1;
2052 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
2053 fmr_param.page_shift = srp_dev->fmr_page_shift;
2054 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
2055 IB_ACCESS_REMOTE_WRITE |
2056 IB_ACCESS_REMOTE_READ);
2058 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
2059 if (IS_ERR(srp_dev->fmr_pool))
2060 srp_dev->fmr_pool = NULL;
2062 if (device->node_type == RDMA_NODE_IB_SWITCH) {
2067 e = device->phys_port_cnt;
2070 for (p = s; p <= e; ++p) {
2071 host = srp_add_port(srp_dev, p);
2073 list_add_tail(&host->list, &srp_dev->dev_list);
2076 ib_set_client_data(device, &srp_client, srp_dev);
2081 ib_dealloc_pd(srp_dev->pd);
2090 static void srp_remove_one(struct ib_device *device)
2092 struct srp_device *srp_dev;
2093 struct srp_host *host, *tmp_host;
2094 LIST_HEAD(target_list);
2095 struct srp_target_port *target, *tmp_target;
2097 srp_dev = ib_get_client_data(device, &srp_client);
2099 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
2100 device_unregister(&host->dev);
2102 * Wait for the sysfs entry to go away, so that no new
2103 * target ports can be created.
2105 wait_for_completion(&host->released);
2108 * Mark all target ports as removed, so we stop queueing
2109 * commands and don't try to reconnect.
2111 spin_lock(&host->target_lock);
2112 list_for_each_entry(target, &host->target_list, list) {
2113 spin_lock_irq(target->scsi_host->host_lock);
2114 target->state = SRP_TARGET_REMOVED;
2115 spin_unlock_irq(target->scsi_host->host_lock);
2117 spin_unlock(&host->target_lock);
2120 * Wait for any reconnection tasks that may have
2121 * started before we marked our target ports as
2122 * removed, and any target port removal tasks.
2124 flush_scheduled_work();
2126 list_for_each_entry_safe(target, tmp_target,
2127 &host->target_list, list) {
2128 srp_remove_host(target->scsi_host);
2129 scsi_remove_host(target->scsi_host);
2130 srp_disconnect_target(target);
2131 ib_destroy_cm_id(target->cm_id);
2132 srp_free_target_ib(target);
2133 scsi_host_put(target->scsi_host);
2139 if (srp_dev->fmr_pool)
2140 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2141 ib_dereg_mr(srp_dev->mr);
2142 ib_dealloc_pd(srp_dev->pd);
2147 static struct srp_function_template ib_srp_transport_functions = {
2150 static int __init srp_init_module(void)
2154 if (srp_sg_tablesize > 255) {
2155 printk(KERN_WARNING PFX "Clamping srp_sg_tablesize to 255\n");
2156 srp_sg_tablesize = 255;
2159 ib_srp_transport_template =
2160 srp_attach_transport(&ib_srp_transport_functions);
2161 if (!ib_srp_transport_template)
2164 srp_template.sg_tablesize = srp_sg_tablesize;
2165 srp_max_iu_len = (sizeof (struct srp_cmd) +
2166 sizeof (struct srp_indirect_buf) +
2167 srp_sg_tablesize * 16);
2169 ret = class_register(&srp_class);
2171 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
2172 srp_release_transport(ib_srp_transport_template);
2176 ib_sa_register_client(&srp_sa_client);
2178 ret = ib_register_client(&srp_client);
2180 printk(KERN_ERR PFX "couldn't register IB client\n");
2181 srp_release_transport(ib_srp_transport_template);
2182 ib_sa_unregister_client(&srp_sa_client);
2183 class_unregister(&srp_class);
2190 static void __exit srp_cleanup_module(void)
2192 ib_unregister_client(&srp_client);
2193 ib_sa_unregister_client(&srp_sa_client);
2194 class_unregister(&srp_class);
2195 srp_release_transport(ib_srp_transport_template);
2198 module_init(srp_init_module);
2199 module_exit(srp_cleanup_module);