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 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/slab.h>
36 #include <linux/err.h>
37 #include <linux/string.h>
38 #include <linux/parser.h>
39 #include <linux/random.h>
40 #include <linux/jiffies.h>
42 #include <asm/atomic.h>
44 #include <scsi/scsi.h>
45 #include <scsi/scsi_device.h>
46 #include <scsi/scsi_dbg.h>
48 #include <scsi/scsi_transport_srp.h>
52 #define DRV_NAME "ib_srp"
53 #define PFX DRV_NAME ": "
54 #define DRV_VERSION "0.2"
55 #define DRV_RELDATE "November 1, 2005"
57 MODULE_AUTHOR("Roland Dreier");
58 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
59 "v" DRV_VERSION " (" DRV_RELDATE ")");
60 MODULE_LICENSE("Dual BSD/GPL");
62 static int srp_sg_tablesize = SRP_DEF_SG_TABLESIZE;
63 static int srp_max_iu_len;
65 module_param(srp_sg_tablesize, int, 0444);
66 MODULE_PARM_DESC(srp_sg_tablesize,
67 "Max number of gather/scatter entries per I/O (default is 12, max 255)");
69 static int topspin_workarounds = 1;
71 module_param(topspin_workarounds, int, 0444);
72 MODULE_PARM_DESC(topspin_workarounds,
73 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
75 static int mellanox_workarounds = 1;
77 module_param(mellanox_workarounds, int, 0444);
78 MODULE_PARM_DESC(mellanox_workarounds,
79 "Enable workarounds for Mellanox SRP target bugs if != 0");
81 static void srp_add_one(struct ib_device *device);
82 static void srp_remove_one(struct ib_device *device);
83 static void srp_completion(struct ib_cq *cq, void *target_ptr);
84 static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
86 static struct scsi_transport_template *ib_srp_transport_template;
88 static struct ib_client srp_client = {
91 .remove = srp_remove_one
94 static struct ib_sa_client srp_sa_client;
96 static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
98 return (struct srp_target_port *) host->hostdata;
101 static const char *srp_target_info(struct Scsi_Host *host)
103 return host_to_target(host)->target_name;
106 static int srp_target_is_topspin(struct srp_target_port *target)
108 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
109 static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
111 return topspin_workarounds &&
112 (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
113 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
116 static int srp_target_is_mellanox(struct srp_target_port *target)
118 static const u8 mellanox_oui[3] = { 0x00, 0x02, 0xc9 };
120 return mellanox_workarounds &&
121 !memcmp(&target->ioc_guid, mellanox_oui, sizeof mellanox_oui);
124 static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
126 enum dma_data_direction direction)
130 iu = kmalloc(sizeof *iu, gfp_mask);
134 iu->buf = kzalloc(size, gfp_mask);
138 iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
140 if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
144 iu->direction = direction;
156 static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
161 ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
167 static void srp_qp_event(struct ib_event *event, void *context)
169 printk(KERN_ERR PFX "QP event %d\n", event->event);
172 static int srp_init_qp(struct srp_target_port *target,
175 struct ib_qp_attr *attr;
178 attr = kmalloc(sizeof *attr, GFP_KERNEL);
182 ret = ib_find_pkey(target->srp_host->srp_dev->dev,
183 target->srp_host->port,
184 be16_to_cpu(target->path.pkey),
189 attr->qp_state = IB_QPS_INIT;
190 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
191 IB_ACCESS_REMOTE_WRITE);
192 attr->port_num = target->srp_host->port;
194 ret = ib_modify_qp(qp, attr,
205 static int srp_new_cm_id(struct srp_target_port *target)
207 struct ib_cm_id *new_cm_id;
209 new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
210 srp_cm_handler, target);
211 if (IS_ERR(new_cm_id))
212 return PTR_ERR(new_cm_id);
215 ib_destroy_cm_id(target->cm_id);
216 target->cm_id = new_cm_id;
221 static int srp_create_target_ib(struct srp_target_port *target)
223 struct ib_qp_init_attr *init_attr;
226 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
230 target->cq = ib_create_cq(target->srp_host->srp_dev->dev,
231 srp_completion, NULL, target, SRP_CQ_SIZE, 0);
232 if (IS_ERR(target->cq)) {
233 ret = PTR_ERR(target->cq);
237 ib_req_notify_cq(target->cq, IB_CQ_NEXT_COMP);
239 init_attr->event_handler = srp_qp_event;
240 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
241 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
242 init_attr->cap.max_recv_sge = 1;
243 init_attr->cap.max_send_sge = 1;
244 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
245 init_attr->qp_type = IB_QPT_RC;
246 init_attr->send_cq = target->cq;
247 init_attr->recv_cq = target->cq;
249 target->qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr);
250 if (IS_ERR(target->qp)) {
251 ret = PTR_ERR(target->qp);
252 ib_destroy_cq(target->cq);
256 ret = srp_init_qp(target, target->qp);
258 ib_destroy_qp(target->qp);
259 ib_destroy_cq(target->cq);
268 static void srp_free_target_ib(struct srp_target_port *target)
272 ib_destroy_qp(target->qp);
273 ib_destroy_cq(target->cq);
275 for (i = 0; i < SRP_RQ_SIZE; ++i)
276 srp_free_iu(target->srp_host, target->rx_ring[i]);
277 for (i = 0; i < SRP_SQ_SIZE + 1; ++i)
278 srp_free_iu(target->srp_host, target->tx_ring[i]);
281 static void srp_path_rec_completion(int status,
282 struct ib_sa_path_rec *pathrec,
285 struct srp_target_port *target = target_ptr;
287 target->status = status;
289 shost_printk(KERN_ERR, target->scsi_host,
290 PFX "Got failed path rec status %d\n", status);
292 target->path = *pathrec;
293 complete(&target->done);
296 static int srp_lookup_path(struct srp_target_port *target)
298 target->path.numb_path = 1;
300 init_completion(&target->done);
302 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
303 target->srp_host->srp_dev->dev,
304 target->srp_host->port,
306 IB_SA_PATH_REC_SERVICE_ID |
307 IB_SA_PATH_REC_DGID |
308 IB_SA_PATH_REC_SGID |
309 IB_SA_PATH_REC_NUMB_PATH |
311 SRP_PATH_REC_TIMEOUT_MS,
313 srp_path_rec_completion,
314 target, &target->path_query);
315 if (target->path_query_id < 0)
316 return target->path_query_id;
318 wait_for_completion(&target->done);
320 if (target->status < 0)
321 shost_printk(KERN_WARNING, target->scsi_host,
322 PFX "Path record query failed\n");
324 return target->status;
327 static int srp_send_req(struct srp_target_port *target)
330 struct ib_cm_req_param param;
331 struct srp_login_req priv;
335 req = kzalloc(sizeof *req, GFP_KERNEL);
339 req->param.primary_path = &target->path;
340 req->param.alternate_path = NULL;
341 req->param.service_id = target->service_id;
342 req->param.qp_num = target->qp->qp_num;
343 req->param.qp_type = target->qp->qp_type;
344 req->param.private_data = &req->priv;
345 req->param.private_data_len = sizeof req->priv;
346 req->param.flow_control = 1;
348 get_random_bytes(&req->param.starting_psn, 4);
349 req->param.starting_psn &= 0xffffff;
352 * Pick some arbitrary defaults here; we could make these
353 * module parameters if anyone cared about setting them.
355 req->param.responder_resources = 4;
356 req->param.remote_cm_response_timeout = 20;
357 req->param.local_cm_response_timeout = 20;
358 req->param.retry_count = 7;
359 req->param.rnr_retry_count = 7;
360 req->param.max_cm_retries = 15;
362 req->priv.opcode = SRP_LOGIN_REQ;
364 req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
365 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
366 SRP_BUF_FORMAT_INDIRECT);
368 * In the published SRP specification (draft rev. 16a), the
369 * port identifier format is 8 bytes of ID extension followed
370 * by 8 bytes of GUID. Older drafts put the two halves in the
371 * opposite order, so that the GUID comes first.
373 * Targets conforming to these obsolete drafts can be
374 * recognized by the I/O Class they report.
376 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
377 memcpy(req->priv.initiator_port_id,
378 &target->path.sgid.global.interface_id, 8);
379 memcpy(req->priv.initiator_port_id + 8,
380 &target->initiator_ext, 8);
381 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
382 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
384 memcpy(req->priv.initiator_port_id,
385 &target->initiator_ext, 8);
386 memcpy(req->priv.initiator_port_id + 8,
387 &target->path.sgid.global.interface_id, 8);
388 memcpy(req->priv.target_port_id, &target->id_ext, 8);
389 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
393 * Topspin/Cisco SRP targets will reject our login unless we
394 * zero out the first 8 bytes of our initiator port ID and set
395 * the second 8 bytes to the local node GUID.
397 if (srp_target_is_topspin(target)) {
398 shost_printk(KERN_DEBUG, target->scsi_host,
399 PFX "Topspin/Cisco initiator port ID workaround "
400 "activated for target GUID %016llx\n",
401 (unsigned long long) be64_to_cpu(target->ioc_guid));
402 memset(req->priv.initiator_port_id, 0, 8);
403 memcpy(req->priv.initiator_port_id + 8,
404 &target->srp_host->srp_dev->dev->node_guid, 8);
407 status = ib_send_cm_req(target->cm_id, &req->param);
414 static void srp_disconnect_target(struct srp_target_port *target)
416 /* XXX should send SRP_I_LOGOUT request */
418 init_completion(&target->done);
419 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
420 shost_printk(KERN_DEBUG, target->scsi_host,
421 PFX "Sending CM DREQ failed\n");
424 wait_for_completion(&target->done);
427 static void srp_remove_work(struct work_struct *work)
429 struct srp_target_port *target =
430 container_of(work, struct srp_target_port, work);
432 spin_lock_irq(target->scsi_host->host_lock);
433 if (target->state != SRP_TARGET_DEAD) {
434 spin_unlock_irq(target->scsi_host->host_lock);
437 target->state = SRP_TARGET_REMOVED;
438 spin_unlock_irq(target->scsi_host->host_lock);
440 spin_lock(&target->srp_host->target_lock);
441 list_del(&target->list);
442 spin_unlock(&target->srp_host->target_lock);
444 srp_remove_host(target->scsi_host);
445 scsi_remove_host(target->scsi_host);
446 ib_destroy_cm_id(target->cm_id);
447 srp_free_target_ib(target);
448 scsi_host_put(target->scsi_host);
451 static int srp_connect_target(struct srp_target_port *target)
456 ret = srp_lookup_path(target);
461 init_completion(&target->done);
462 ret = srp_send_req(target);
465 wait_for_completion(&target->done);
468 * The CM event handling code will set status to
469 * SRP_PORT_REDIRECT if we get a port redirect REJ
470 * back, or SRP_DLID_REDIRECT if we get a lid/qp
473 switch (target->status) {
477 case SRP_PORT_REDIRECT:
478 ret = srp_lookup_path(target);
483 case SRP_DLID_REDIRECT:
487 /* Our current CM id was stale, and is now in timewait.
488 * Try to reconnect with a new one.
490 if (!retries-- || srp_new_cm_id(target)) {
491 shost_printk(KERN_ERR, target->scsi_host, PFX
492 "giving up on stale connection\n");
493 target->status = -ECONNRESET;
494 return target->status;
497 shost_printk(KERN_ERR, target->scsi_host, PFX
498 "retrying stale connection\n");
502 return target->status;
507 static void srp_unmap_data(struct scsi_cmnd *scmnd,
508 struct srp_target_port *target,
509 struct srp_request *req)
511 if (!scsi_sglist(scmnd) ||
512 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
513 scmnd->sc_data_direction != DMA_FROM_DEVICE))
517 ib_fmr_pool_unmap(req->fmr);
521 ib_dma_unmap_sg(target->srp_host->srp_dev->dev, scsi_sglist(scmnd),
522 scsi_sg_count(scmnd), scmnd->sc_data_direction);
525 static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
527 srp_unmap_data(req->scmnd, target, req);
528 list_move_tail(&req->list, &target->free_reqs);
531 static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
533 req->scmnd->result = DID_RESET << 16;
534 req->scmnd->scsi_done(req->scmnd);
535 srp_remove_req(target, req);
538 static int srp_reconnect_target(struct srp_target_port *target)
540 struct ib_qp_attr qp_attr;
541 struct srp_request *req, *tmp;
545 spin_lock_irq(target->scsi_host->host_lock);
546 if (target->state != SRP_TARGET_LIVE) {
547 spin_unlock_irq(target->scsi_host->host_lock);
550 target->state = SRP_TARGET_CONNECTING;
551 spin_unlock_irq(target->scsi_host->host_lock);
553 srp_disconnect_target(target);
555 * Now get a new local CM ID so that we avoid confusing the
556 * target in case things are really fouled up.
558 ret = srp_new_cm_id(target);
562 qp_attr.qp_state = IB_QPS_RESET;
563 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
567 ret = srp_init_qp(target, target->qp);
571 while (ib_poll_cq(target->cq, 1, &wc) > 0)
574 spin_lock_irq(target->scsi_host->host_lock);
575 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
576 srp_reset_req(target, req);
577 spin_unlock_irq(target->scsi_host->host_lock);
583 target->qp_in_error = 0;
584 ret = srp_connect_target(target);
588 spin_lock_irq(target->scsi_host->host_lock);
589 if (target->state == SRP_TARGET_CONNECTING) {
591 target->state = SRP_TARGET_LIVE;
594 spin_unlock_irq(target->scsi_host->host_lock);
599 shost_printk(KERN_ERR, target->scsi_host,
600 PFX "reconnect failed (%d), removing target port.\n", ret);
603 * We couldn't reconnect, so kill our target port off.
604 * However, we have to defer the real removal because we might
605 * be in the context of the SCSI error handler now, which
606 * would deadlock if we call scsi_remove_host().
608 spin_lock_irq(target->scsi_host->host_lock);
609 if (target->state == SRP_TARGET_CONNECTING) {
610 target->state = SRP_TARGET_DEAD;
611 INIT_WORK(&target->work, srp_remove_work);
612 schedule_work(&target->work);
614 spin_unlock_irq(target->scsi_host->host_lock);
619 static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat,
620 int sg_cnt, struct srp_request *req,
621 struct srp_direct_buf *buf)
629 struct srp_device *dev = target->srp_host->srp_dev;
630 struct ib_device *ibdev = dev->dev;
631 struct scatterlist *sg;
636 if (srp_target_is_mellanox(target) &&
637 (ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask))
641 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
642 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
644 if (ib_sg_dma_address(ibdev, sg) & ~dev->fmr_page_mask) {
650 if ((ib_sg_dma_address(ibdev, sg) + dma_len) &
651 ~dev->fmr_page_mask) {
661 page_cnt += len >> dev->fmr_page_shift;
662 if (page_cnt > SRP_FMR_SIZE)
665 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
670 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
671 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
673 for (j = 0; j < dma_len; j += dev->fmr_page_size)
674 dma_pages[page_cnt++] =
675 (ib_sg_dma_address(ibdev, sg) &
676 dev->fmr_page_mask) + j;
679 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
680 dma_pages, page_cnt, io_addr);
681 if (IS_ERR(req->fmr)) {
682 ret = PTR_ERR(req->fmr);
687 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) &
688 ~dev->fmr_page_mask);
689 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
690 buf->len = cpu_to_be32(len);
700 static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
701 struct srp_request *req)
703 struct scatterlist *scat;
704 struct srp_cmd *cmd = req->cmd->buf;
705 int len, nents, count;
706 u8 fmt = SRP_DATA_DESC_DIRECT;
707 struct srp_device *dev;
708 struct ib_device *ibdev;
710 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
711 return sizeof (struct srp_cmd);
713 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
714 scmnd->sc_data_direction != DMA_TO_DEVICE) {
715 shost_printk(KERN_WARNING, target->scsi_host,
716 PFX "Unhandled data direction %d\n",
717 scmnd->sc_data_direction);
721 nents = scsi_sg_count(scmnd);
722 scat = scsi_sglist(scmnd);
724 dev = target->srp_host->srp_dev;
727 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
729 fmt = SRP_DATA_DESC_DIRECT;
730 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
734 * The midlayer only generated a single gather/scatter
735 * entry, or DMA mapping coalesced everything to a
736 * single entry. So a direct descriptor along with
737 * the DMA MR suffices.
739 struct srp_direct_buf *buf = (void *) cmd->add_data;
741 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
742 buf->key = cpu_to_be32(dev->mr->rkey);
743 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
744 } else if (srp_map_fmr(target, scat, count, req,
745 (void *) cmd->add_data)) {
747 * FMR mapping failed, and the scatterlist has more
748 * than one entry. Generate an indirect memory
751 struct srp_indirect_buf *buf = (void *) cmd->add_data;
752 struct scatterlist *sg;
756 fmt = SRP_DATA_DESC_INDIRECT;
757 len = sizeof (struct srp_cmd) +
758 sizeof (struct srp_indirect_buf) +
759 count * sizeof (struct srp_direct_buf);
761 scsi_for_each_sg(scmnd, sg, count, i) {
762 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
764 buf->desc_list[i].va =
765 cpu_to_be64(ib_sg_dma_address(ibdev, sg));
766 buf->desc_list[i].key =
767 cpu_to_be32(dev->mr->rkey);
768 buf->desc_list[i].len = cpu_to_be32(dma_len);
772 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
773 cmd->data_out_desc_cnt = count;
775 cmd->data_in_desc_cnt = count;
778 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
779 buf->table_desc.key =
780 cpu_to_be32(target->srp_host->srp_dev->mr->rkey);
781 buf->table_desc.len =
782 cpu_to_be32(count * sizeof (struct srp_direct_buf));
784 buf->len = cpu_to_be32(datalen);
787 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
788 cmd->buf_fmt = fmt << 4;
795 static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
797 struct srp_request *req;
798 struct scsi_cmnd *scmnd;
802 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
804 spin_lock_irqsave(target->scsi_host->host_lock, flags);
806 target->req_lim += delta;
808 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
810 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
811 if (be32_to_cpu(rsp->resp_data_len) < 4)
812 req->tsk_status = -1;
814 req->tsk_status = rsp->data[3];
815 complete(&req->done);
819 shost_printk(KERN_ERR, target->scsi_host,
820 "Null scmnd for RSP w/tag %016llx\n",
821 (unsigned long long) rsp->tag);
822 scmnd->result = rsp->status;
824 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
825 memcpy(scmnd->sense_buffer, rsp->data +
826 be32_to_cpu(rsp->resp_data_len),
827 min_t(int, be32_to_cpu(rsp->sense_data_len),
828 SCSI_SENSE_BUFFERSIZE));
831 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
832 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
833 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
834 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
836 if (!req->tsk_mgmt) {
837 scmnd->host_scribble = (void *) -1L;
838 scmnd->scsi_done(scmnd);
840 srp_remove_req(target, req);
845 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
848 static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
850 struct ib_device *dev;
854 iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
856 dev = target->srp_host->srp_dev->dev;
857 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
860 opcode = *(u8 *) iu->buf;
865 shost_printk(KERN_ERR, target->scsi_host,
866 PFX "recv completion, opcode 0x%02x\n", opcode);
868 for (i = 0; i < wc->byte_len; ++i) {
870 printk(KERN_ERR " [%02x] ", i);
871 printk(" %02x", ((u8 *) iu->buf)[i]);
872 if ((i + 1) % 8 == 0)
876 if (wc->byte_len % 8)
882 srp_process_rsp(target, iu->buf);
886 /* XXX Handle target logout */
887 shost_printk(KERN_WARNING, target->scsi_host,
888 PFX "Got target logout request\n");
892 shost_printk(KERN_WARNING, target->scsi_host,
893 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
897 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
901 static void srp_completion(struct ib_cq *cq, void *target_ptr)
903 struct srp_target_port *target = target_ptr;
906 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
907 while (ib_poll_cq(cq, 1, &wc) > 0) {
909 shost_printk(KERN_ERR, target->scsi_host,
910 PFX "failed %s status %d\n",
911 wc.wr_id & SRP_OP_RECV ? "receive" : "send",
913 target->qp_in_error = 1;
917 if (wc.wr_id & SRP_OP_RECV)
918 srp_handle_recv(target, &wc);
924 static int __srp_post_recv(struct srp_target_port *target)
928 struct ib_recv_wr wr, *bad_wr;
932 next = target->rx_head & (SRP_RQ_SIZE - 1);
933 wr.wr_id = next | SRP_OP_RECV;
934 iu = target->rx_ring[next];
937 list.length = iu->size;
938 list.lkey = target->srp_host->srp_dev->mr->lkey;
944 ret = ib_post_recv(target->qp, &wr, &bad_wr);
951 static int srp_post_recv(struct srp_target_port *target)
956 spin_lock_irqsave(target->scsi_host->host_lock, flags);
957 ret = __srp_post_recv(target);
958 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
964 * Must be called with target->scsi_host->host_lock held to protect
965 * req_lim and tx_head. Lock cannot be dropped between call here and
966 * call to __srp_post_send().
968 static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
969 enum srp_request_type req_type)
971 s32 min = (req_type == SRP_REQ_TASK_MGMT) ? 1 : 2;
973 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
976 if (target->req_lim < min) {
977 ++target->zero_req_lim;
981 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
985 * Must be called with target->scsi_host->host_lock held to protect
986 * req_lim and tx_head.
988 static int __srp_post_send(struct srp_target_port *target,
989 struct srp_iu *iu, int len)
992 struct ib_send_wr wr, *bad_wr;
997 list.lkey = target->srp_host->srp_dev->mr->lkey;
1000 wr.wr_id = target->tx_head & SRP_SQ_SIZE;
1003 wr.opcode = IB_WR_SEND;
1004 wr.send_flags = IB_SEND_SIGNALED;
1006 ret = ib_post_send(target->qp, &wr, &bad_wr);
1016 static int srp_queuecommand(struct scsi_cmnd *scmnd,
1017 void (*done)(struct scsi_cmnd *))
1019 struct srp_target_port *target = host_to_target(scmnd->device->host);
1020 struct srp_request *req;
1022 struct srp_cmd *cmd;
1023 struct ib_device *dev;
1026 if (target->state == SRP_TARGET_CONNECTING)
1029 if (target->state == SRP_TARGET_DEAD ||
1030 target->state == SRP_TARGET_REMOVED) {
1031 scmnd->result = DID_BAD_TARGET << 16;
1036 iu = __srp_get_tx_iu(target, SRP_REQ_NORMAL);
1040 dev = target->srp_host->srp_dev->dev;
1041 ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len,
1044 req = list_entry(target->free_reqs.next, struct srp_request, list);
1046 scmnd->scsi_done = done;
1048 scmnd->host_scribble = (void *) (long) req->index;
1051 memset(cmd, 0, sizeof *cmd);
1053 cmd->opcode = SRP_CMD;
1054 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
1055 cmd->tag = req->index;
1056 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1061 req->tsk_mgmt = NULL;
1063 len = srp_map_data(scmnd, target, req);
1065 shost_printk(KERN_ERR, target->scsi_host,
1066 PFX "Failed to map data\n");
1070 if (__srp_post_recv(target)) {
1071 shost_printk(KERN_ERR, target->scsi_host, PFX "Recv failed\n");
1075 ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len,
1078 if (__srp_post_send(target, iu, len)) {
1079 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
1083 list_move_tail(&req->list, &target->req_queue);
1088 srp_unmap_data(scmnd, target, req);
1091 return SCSI_MLQUEUE_HOST_BUSY;
1094 static int srp_alloc_iu_bufs(struct srp_target_port *target)
1098 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1099 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1100 target->max_ti_iu_len,
1101 GFP_KERNEL, DMA_FROM_DEVICE);
1102 if (!target->rx_ring[i])
1106 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1107 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
1109 GFP_KERNEL, DMA_TO_DEVICE);
1110 if (!target->tx_ring[i])
1117 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1118 srp_free_iu(target->srp_host, target->rx_ring[i]);
1119 target->rx_ring[i] = NULL;
1122 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1123 srp_free_iu(target->srp_host, target->tx_ring[i]);
1124 target->tx_ring[i] = NULL;
1130 static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1131 struct ib_cm_event *event,
1132 struct srp_target_port *target)
1134 struct Scsi_Host *shost = target->scsi_host;
1135 struct ib_class_port_info *cpi;
1138 switch (event->param.rej_rcvd.reason) {
1139 case IB_CM_REJ_PORT_CM_REDIRECT:
1140 cpi = event->param.rej_rcvd.ari;
1141 target->path.dlid = cpi->redirect_lid;
1142 target->path.pkey = cpi->redirect_pkey;
1143 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1144 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1146 target->status = target->path.dlid ?
1147 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1150 case IB_CM_REJ_PORT_REDIRECT:
1151 if (srp_target_is_topspin(target)) {
1153 * Topspin/Cisco SRP gateways incorrectly send
1154 * reject reason code 25 when they mean 24
1157 memcpy(target->path.dgid.raw,
1158 event->param.rej_rcvd.ari, 16);
1160 shost_printk(KERN_DEBUG, shost,
1161 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1162 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1163 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1165 target->status = SRP_PORT_REDIRECT;
1167 shost_printk(KERN_WARNING, shost,
1168 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1169 target->status = -ECONNRESET;
1173 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
1174 shost_printk(KERN_WARNING, shost,
1175 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1176 target->status = -ECONNRESET;
1179 case IB_CM_REJ_CONSUMER_DEFINED:
1180 opcode = *(u8 *) event->private_data;
1181 if (opcode == SRP_LOGIN_REJ) {
1182 struct srp_login_rej *rej = event->private_data;
1183 u32 reason = be32_to_cpu(rej->reason);
1185 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
1186 shost_printk(KERN_WARNING, shost,
1187 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1189 shost_printk(KERN_WARNING, shost,
1190 PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
1192 shost_printk(KERN_WARNING, shost,
1193 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1194 " opcode 0x%02x\n", opcode);
1195 target->status = -ECONNRESET;
1198 case IB_CM_REJ_STALE_CONN:
1199 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
1200 target->status = SRP_STALE_CONN;
1204 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
1205 event->param.rej_rcvd.reason);
1206 target->status = -ECONNRESET;
1210 static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1212 struct srp_target_port *target = cm_id->context;
1213 struct ib_qp_attr *qp_attr = NULL;
1218 switch (event->event) {
1219 case IB_CM_REQ_ERROR:
1220 shost_printk(KERN_DEBUG, target->scsi_host,
1221 PFX "Sending CM REQ failed\n");
1223 target->status = -ECONNRESET;
1226 case IB_CM_REP_RECEIVED:
1228 opcode = *(u8 *) event->private_data;
1230 if (opcode == SRP_LOGIN_RSP) {
1231 struct srp_login_rsp *rsp = event->private_data;
1233 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1234 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1236 target->scsi_host->can_queue = min(target->req_lim,
1237 target->scsi_host->can_queue);
1239 shost_printk(KERN_WARNING, target->scsi_host,
1240 PFX "Unhandled RSP opcode %#x\n", opcode);
1241 target->status = -ECONNRESET;
1245 if (!target->rx_ring[0]) {
1246 target->status = srp_alloc_iu_bufs(target);
1251 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1253 target->status = -ENOMEM;
1257 qp_attr->qp_state = IB_QPS_RTR;
1258 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1262 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1266 target->status = srp_post_recv(target);
1270 qp_attr->qp_state = IB_QPS_RTS;
1271 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1275 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1279 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1285 case IB_CM_REJ_RECEIVED:
1286 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
1289 srp_cm_rej_handler(cm_id, event, target);
1292 case IB_CM_DREQ_RECEIVED:
1293 shost_printk(KERN_WARNING, target->scsi_host,
1294 PFX "DREQ received - connection closed\n");
1295 if (ib_send_cm_drep(cm_id, NULL, 0))
1296 shost_printk(KERN_ERR, target->scsi_host,
1297 PFX "Sending CM DREP failed\n");
1300 case IB_CM_TIMEWAIT_EXIT:
1301 shost_printk(KERN_ERR, target->scsi_host,
1302 PFX "connection closed\n");
1308 case IB_CM_MRA_RECEIVED:
1309 case IB_CM_DREQ_ERROR:
1310 case IB_CM_DREP_RECEIVED:
1314 shost_printk(KERN_WARNING, target->scsi_host,
1315 PFX "Unhandled CM event %d\n", event->event);
1320 complete(&target->done);
1327 static int srp_send_tsk_mgmt(struct srp_target_port *target,
1328 struct srp_request *req, u8 func)
1331 struct srp_tsk_mgmt *tsk_mgmt;
1333 spin_lock_irq(target->scsi_host->host_lock);
1335 if (target->state == SRP_TARGET_DEAD ||
1336 target->state == SRP_TARGET_REMOVED) {
1337 req->scmnd->result = DID_BAD_TARGET << 16;
1341 init_completion(&req->done);
1343 iu = __srp_get_tx_iu(target, SRP_REQ_TASK_MGMT);
1348 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1350 tsk_mgmt->opcode = SRP_TSK_MGMT;
1351 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1352 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
1353 tsk_mgmt->tsk_mgmt_func = func;
1354 tsk_mgmt->task_tag = req->index;
1356 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1361 spin_unlock_irq(target->scsi_host->host_lock);
1363 if (!wait_for_completion_timeout(&req->done,
1364 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
1370 spin_unlock_irq(target->scsi_host->host_lock);
1374 static int srp_find_req(struct srp_target_port *target,
1375 struct scsi_cmnd *scmnd,
1376 struct srp_request **req)
1378 if (scmnd->host_scribble == (void *) -1L)
1381 *req = &target->req_ring[(long) scmnd->host_scribble];
1386 static int srp_abort(struct scsi_cmnd *scmnd)
1388 struct srp_target_port *target = host_to_target(scmnd->device->host);
1389 struct srp_request *req;
1392 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
1394 if (target->qp_in_error)
1396 if (srp_find_req(target, scmnd, &req))
1398 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1401 spin_lock_irq(target->scsi_host->host_lock);
1403 if (req->cmd_done) {
1404 srp_remove_req(target, req);
1405 scmnd->scsi_done(scmnd);
1406 } else if (!req->tsk_status) {
1407 srp_remove_req(target, req);
1408 scmnd->result = DID_ABORT << 16;
1412 spin_unlock_irq(target->scsi_host->host_lock);
1417 static int srp_reset_device(struct scsi_cmnd *scmnd)
1419 struct srp_target_port *target = host_to_target(scmnd->device->host);
1420 struct srp_request *req, *tmp;
1422 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
1424 if (target->qp_in_error)
1426 if (srp_find_req(target, scmnd, &req))
1428 if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1430 if (req->tsk_status)
1433 spin_lock_irq(target->scsi_host->host_lock);
1435 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
1436 if (req->scmnd->device == scmnd->device)
1437 srp_reset_req(target, req);
1439 spin_unlock_irq(target->scsi_host->host_lock);
1444 static int srp_reset_host(struct scsi_cmnd *scmnd)
1446 struct srp_target_port *target = host_to_target(scmnd->device->host);
1449 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
1451 if (!srp_reconnect_target(target))
1457 static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
1460 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1462 if (target->state == SRP_TARGET_DEAD ||
1463 target->state == SRP_TARGET_REMOVED)
1466 return sprintf(buf, "0x%016llx\n",
1467 (unsigned long long) be64_to_cpu(target->id_ext));
1470 static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
1473 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1475 if (target->state == SRP_TARGET_DEAD ||
1476 target->state == SRP_TARGET_REMOVED)
1479 return sprintf(buf, "0x%016llx\n",
1480 (unsigned long long) be64_to_cpu(target->ioc_guid));
1483 static ssize_t show_service_id(struct device *dev,
1484 struct device_attribute *attr, char *buf)
1486 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1488 if (target->state == SRP_TARGET_DEAD ||
1489 target->state == SRP_TARGET_REMOVED)
1492 return sprintf(buf, "0x%016llx\n",
1493 (unsigned long long) be64_to_cpu(target->service_id));
1496 static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
1499 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1501 if (target->state == SRP_TARGET_DEAD ||
1502 target->state == SRP_TARGET_REMOVED)
1505 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1508 static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
1511 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1513 if (target->state == SRP_TARGET_DEAD ||
1514 target->state == SRP_TARGET_REMOVED)
1517 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1518 be16_to_cpu(((__be16 *) target->path.dgid.raw)[0]),
1519 be16_to_cpu(((__be16 *) target->path.dgid.raw)[1]),
1520 be16_to_cpu(((__be16 *) target->path.dgid.raw)[2]),
1521 be16_to_cpu(((__be16 *) target->path.dgid.raw)[3]),
1522 be16_to_cpu(((__be16 *) target->path.dgid.raw)[4]),
1523 be16_to_cpu(((__be16 *) target->path.dgid.raw)[5]),
1524 be16_to_cpu(((__be16 *) target->path.dgid.raw)[6]),
1525 be16_to_cpu(((__be16 *) target->path.dgid.raw)[7]));
1528 static ssize_t show_orig_dgid(struct device *dev,
1529 struct device_attribute *attr, char *buf)
1531 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1533 if (target->state == SRP_TARGET_DEAD ||
1534 target->state == SRP_TARGET_REMOVED)
1537 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1538 be16_to_cpu(target->orig_dgid[0]),
1539 be16_to_cpu(target->orig_dgid[1]),
1540 be16_to_cpu(target->orig_dgid[2]),
1541 be16_to_cpu(target->orig_dgid[3]),
1542 be16_to_cpu(target->orig_dgid[4]),
1543 be16_to_cpu(target->orig_dgid[5]),
1544 be16_to_cpu(target->orig_dgid[6]),
1545 be16_to_cpu(target->orig_dgid[7]));
1548 static ssize_t show_zero_req_lim(struct device *dev,
1549 struct device_attribute *attr, char *buf)
1551 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1553 if (target->state == SRP_TARGET_DEAD ||
1554 target->state == SRP_TARGET_REMOVED)
1557 return sprintf(buf, "%d\n", target->zero_req_lim);
1560 static ssize_t show_local_ib_port(struct device *dev,
1561 struct device_attribute *attr, char *buf)
1563 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1565 return sprintf(buf, "%d\n", target->srp_host->port);
1568 static ssize_t show_local_ib_device(struct device *dev,
1569 struct device_attribute *attr, char *buf)
1571 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1573 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
1576 static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1577 static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1578 static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1579 static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1580 static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1581 static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
1582 static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1583 static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1584 static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
1586 static struct device_attribute *srp_host_attrs[] = {
1589 &dev_attr_service_id,
1592 &dev_attr_orig_dgid,
1593 &dev_attr_zero_req_lim,
1594 &dev_attr_local_ib_port,
1595 &dev_attr_local_ib_device,
1599 static struct scsi_host_template srp_template = {
1600 .module = THIS_MODULE,
1601 .name = "InfiniBand SRP initiator",
1602 .proc_name = DRV_NAME,
1603 .info = srp_target_info,
1604 .queuecommand = srp_queuecommand,
1605 .eh_abort_handler = srp_abort,
1606 .eh_device_reset_handler = srp_reset_device,
1607 .eh_host_reset_handler = srp_reset_host,
1608 .can_queue = SRP_SQ_SIZE,
1610 .cmd_per_lun = SRP_SQ_SIZE,
1611 .use_clustering = ENABLE_CLUSTERING,
1612 .shost_attrs = srp_host_attrs
1615 static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1617 struct srp_rport_identifiers ids;
1618 struct srp_rport *rport;
1620 sprintf(target->target_name, "SRP.T10:%016llX",
1621 (unsigned long long) be64_to_cpu(target->id_ext));
1623 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
1626 memcpy(ids.port_id, &target->id_ext, 8);
1627 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
1628 ids.roles = SRP_RPORT_ROLE_TARGET;
1629 rport = srp_rport_add(target->scsi_host, &ids);
1630 if (IS_ERR(rport)) {
1631 scsi_remove_host(target->scsi_host);
1632 return PTR_ERR(rport);
1635 spin_lock(&host->target_lock);
1636 list_add_tail(&target->list, &host->target_list);
1637 spin_unlock(&host->target_lock);
1639 target->state = SRP_TARGET_LIVE;
1641 scsi_scan_target(&target->scsi_host->shost_gendev,
1642 0, target->scsi_id, SCAN_WILD_CARD, 0);
1647 static void srp_release_dev(struct device *dev)
1649 struct srp_host *host =
1650 container_of(dev, struct srp_host, dev);
1652 complete(&host->released);
1655 static struct class srp_class = {
1656 .name = "infiniband_srp",
1657 .dev_release = srp_release_dev
1661 * Target ports are added by writing
1663 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1664 * pkey=<P_Key>,service_id=<service ID>
1666 * to the add_target sysfs attribute.
1670 SRP_OPT_ID_EXT = 1 << 0,
1671 SRP_OPT_IOC_GUID = 1 << 1,
1672 SRP_OPT_DGID = 1 << 2,
1673 SRP_OPT_PKEY = 1 << 3,
1674 SRP_OPT_SERVICE_ID = 1 << 4,
1675 SRP_OPT_MAX_SECT = 1 << 5,
1676 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
1677 SRP_OPT_IO_CLASS = 1 << 7,
1678 SRP_OPT_INITIATOR_EXT = 1 << 8,
1679 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1683 SRP_OPT_SERVICE_ID),
1686 static match_table_t srp_opt_tokens = {
1687 { SRP_OPT_ID_EXT, "id_ext=%s" },
1688 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1689 { SRP_OPT_DGID, "dgid=%s" },
1690 { SRP_OPT_PKEY, "pkey=%x" },
1691 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1692 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1693 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
1694 { SRP_OPT_IO_CLASS, "io_class=%x" },
1695 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
1696 { SRP_OPT_ERR, NULL }
1699 static int srp_parse_options(const char *buf, struct srp_target_port *target)
1701 char *options, *sep_opt;
1704 substring_t args[MAX_OPT_ARGS];
1710 options = kstrdup(buf, GFP_KERNEL);
1715 while ((p = strsep(&sep_opt, ",")) != NULL) {
1719 token = match_token(p, srp_opt_tokens, args);
1723 case SRP_OPT_ID_EXT:
1724 p = match_strdup(args);
1729 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1733 case SRP_OPT_IOC_GUID:
1734 p = match_strdup(args);
1739 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1744 p = match_strdup(args);
1749 if (strlen(p) != 32) {
1750 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
1755 for (i = 0; i < 16; ++i) {
1756 strlcpy(dgid, p + i * 2, 3);
1757 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1760 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
1764 if (match_hex(args, &token)) {
1765 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1768 target->path.pkey = cpu_to_be16(token);
1771 case SRP_OPT_SERVICE_ID:
1772 p = match_strdup(args);
1777 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
1778 target->path.service_id = target->service_id;
1782 case SRP_OPT_MAX_SECT:
1783 if (match_int(args, &token)) {
1784 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1787 target->scsi_host->max_sectors = token;
1790 case SRP_OPT_MAX_CMD_PER_LUN:
1791 if (match_int(args, &token)) {
1792 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1795 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1798 case SRP_OPT_IO_CLASS:
1799 if (match_hex(args, &token)) {
1800 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1803 if (token != SRP_REV10_IB_IO_CLASS &&
1804 token != SRP_REV16A_IB_IO_CLASS) {
1805 printk(KERN_WARNING PFX "unknown IO class parameter value"
1806 " %x specified (use %x or %x).\n",
1807 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1810 target->io_class = token;
1813 case SRP_OPT_INITIATOR_EXT:
1814 p = match_strdup(args);
1819 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1824 printk(KERN_WARNING PFX "unknown parameter or missing value "
1825 "'%s' in target creation request\n", p);
1830 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1833 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1834 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1835 !(srp_opt_tokens[i].token & opt_mask))
1836 printk(KERN_WARNING PFX "target creation request is "
1837 "missing parameter '%s'\n",
1838 srp_opt_tokens[i].pattern);
1845 static ssize_t srp_create_target(struct device *dev,
1846 struct device_attribute *attr,
1847 const char *buf, size_t count)
1849 struct srp_host *host =
1850 container_of(dev, struct srp_host, dev);
1851 struct Scsi_Host *target_host;
1852 struct srp_target_port *target;
1856 target_host = scsi_host_alloc(&srp_template,
1857 sizeof (struct srp_target_port));
1861 target_host->transportt = ib_srp_transport_template;
1862 target_host->max_lun = SRP_MAX_LUN;
1863 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
1865 target = host_to_target(target_host);
1867 target->io_class = SRP_REV16A_IB_IO_CLASS;
1868 target->scsi_host = target_host;
1869 target->srp_host = host;
1871 INIT_LIST_HEAD(&target->free_reqs);
1872 INIT_LIST_HEAD(&target->req_queue);
1873 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1874 target->req_ring[i].index = i;
1875 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1878 ret = srp_parse_options(buf, target);
1882 ib_query_gid(host->srp_dev->dev, host->port, 0, &target->path.sgid);
1884 shost_printk(KERN_DEBUG, target->scsi_host, PFX
1885 "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1886 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1887 (unsigned long long) be64_to_cpu(target->id_ext),
1888 (unsigned long long) be64_to_cpu(target->ioc_guid),
1889 be16_to_cpu(target->path.pkey),
1890 (unsigned long long) be64_to_cpu(target->service_id),
1891 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]),
1892 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]),
1893 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]),
1894 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]),
1895 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]),
1896 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]),
1897 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]),
1898 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14]));
1900 ret = srp_create_target_ib(target);
1904 ret = srp_new_cm_id(target);
1908 target->qp_in_error = 0;
1909 ret = srp_connect_target(target);
1911 shost_printk(KERN_ERR, target->scsi_host,
1912 PFX "Connection failed\n");
1916 ret = srp_add_target(host, target);
1918 goto err_disconnect;
1923 srp_disconnect_target(target);
1926 ib_destroy_cm_id(target->cm_id);
1929 srp_free_target_ib(target);
1932 scsi_host_put(target_host);
1937 static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
1939 static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
1942 struct srp_host *host = container_of(dev, struct srp_host, dev);
1944 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
1947 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1949 static ssize_t show_port(struct device *dev, struct device_attribute *attr,
1952 struct srp_host *host = container_of(dev, struct srp_host, dev);
1954 return sprintf(buf, "%d\n", host->port);
1957 static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
1959 static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
1961 struct srp_host *host;
1963 host = kzalloc(sizeof *host, GFP_KERNEL);
1967 INIT_LIST_HEAD(&host->target_list);
1968 spin_lock_init(&host->target_lock);
1969 init_completion(&host->released);
1970 host->srp_dev = device;
1973 host->dev.class = &srp_class;
1974 host->dev.parent = device->dev->dma_device;
1975 snprintf(host->dev.bus_id, BUS_ID_SIZE, "srp-%s-%d",
1976 device->dev->name, port);
1978 if (device_register(&host->dev))
1980 if (device_create_file(&host->dev, &dev_attr_add_target))
1982 if (device_create_file(&host->dev, &dev_attr_ibdev))
1984 if (device_create_file(&host->dev, &dev_attr_port))
1990 device_unregister(&host->dev);
1998 static void srp_add_one(struct ib_device *device)
2000 struct srp_device *srp_dev;
2001 struct ib_device_attr *dev_attr;
2002 struct ib_fmr_pool_param fmr_param;
2003 struct srp_host *host;
2006 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
2010 if (ib_query_device(device, dev_attr)) {
2011 printk(KERN_WARNING PFX "Query device failed for %s\n",
2016 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
2021 * Use the smallest page size supported by the HCA, down to a
2022 * minimum of 512 bytes (which is the smallest sector that a
2023 * SCSI command will ever carry).
2025 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
2026 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
2027 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
2029 INIT_LIST_HEAD(&srp_dev->dev_list);
2031 srp_dev->dev = device;
2032 srp_dev->pd = ib_alloc_pd(device);
2033 if (IS_ERR(srp_dev->pd))
2036 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
2037 IB_ACCESS_LOCAL_WRITE |
2038 IB_ACCESS_REMOTE_READ |
2039 IB_ACCESS_REMOTE_WRITE);
2040 if (IS_ERR(srp_dev->mr))
2043 memset(&fmr_param, 0, sizeof fmr_param);
2044 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
2045 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
2046 fmr_param.cache = 1;
2047 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
2048 fmr_param.page_shift = srp_dev->fmr_page_shift;
2049 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
2050 IB_ACCESS_REMOTE_WRITE |
2051 IB_ACCESS_REMOTE_READ);
2053 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
2054 if (IS_ERR(srp_dev->fmr_pool))
2055 srp_dev->fmr_pool = NULL;
2057 if (device->node_type == RDMA_NODE_IB_SWITCH) {
2062 e = device->phys_port_cnt;
2065 for (p = s; p <= e; ++p) {
2066 host = srp_add_port(srp_dev, p);
2068 list_add_tail(&host->list, &srp_dev->dev_list);
2071 ib_set_client_data(device, &srp_client, srp_dev);
2076 ib_dealloc_pd(srp_dev->pd);
2085 static void srp_remove_one(struct ib_device *device)
2087 struct srp_device *srp_dev;
2088 struct srp_host *host, *tmp_host;
2089 LIST_HEAD(target_list);
2090 struct srp_target_port *target, *tmp_target;
2092 srp_dev = ib_get_client_data(device, &srp_client);
2094 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
2095 device_unregister(&host->dev);
2097 * Wait for the sysfs entry to go away, so that no new
2098 * target ports can be created.
2100 wait_for_completion(&host->released);
2103 * Mark all target ports as removed, so we stop queueing
2104 * commands and don't try to reconnect.
2106 spin_lock(&host->target_lock);
2107 list_for_each_entry(target, &host->target_list, list) {
2108 spin_lock_irq(target->scsi_host->host_lock);
2109 target->state = SRP_TARGET_REMOVED;
2110 spin_unlock_irq(target->scsi_host->host_lock);
2112 spin_unlock(&host->target_lock);
2115 * Wait for any reconnection tasks that may have
2116 * started before we marked our target ports as
2117 * removed, and any target port removal tasks.
2119 flush_scheduled_work();
2121 list_for_each_entry_safe(target, tmp_target,
2122 &host->target_list, list) {
2123 srp_remove_host(target->scsi_host);
2124 scsi_remove_host(target->scsi_host);
2125 srp_disconnect_target(target);
2126 ib_destroy_cm_id(target->cm_id);
2127 srp_free_target_ib(target);
2128 scsi_host_put(target->scsi_host);
2134 if (srp_dev->fmr_pool)
2135 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2136 ib_dereg_mr(srp_dev->mr);
2137 ib_dealloc_pd(srp_dev->pd);
2142 static struct srp_function_template ib_srp_transport_functions = {
2145 static int __init srp_init_module(void)
2149 if (srp_sg_tablesize > 255) {
2150 printk(KERN_WARNING PFX "Clamping srp_sg_tablesize to 255\n");
2151 srp_sg_tablesize = 255;
2154 ib_srp_transport_template =
2155 srp_attach_transport(&ib_srp_transport_functions);
2156 if (!ib_srp_transport_template)
2159 srp_template.sg_tablesize = srp_sg_tablesize;
2160 srp_max_iu_len = (sizeof (struct srp_cmd) +
2161 sizeof (struct srp_indirect_buf) +
2162 srp_sg_tablesize * 16);
2164 ret = class_register(&srp_class);
2166 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
2167 srp_release_transport(ib_srp_transport_template);
2171 ib_sa_register_client(&srp_sa_client);
2173 ret = ib_register_client(&srp_client);
2175 printk(KERN_ERR PFX "couldn't register IB client\n");
2176 srp_release_transport(ib_srp_transport_template);
2177 ib_sa_unregister_client(&srp_sa_client);
2178 class_unregister(&srp_class);
2185 static void __exit srp_cleanup_module(void)
2187 ib_unregister_client(&srp_client);
2188 ib_sa_unregister_client(&srp_sa_client);
2189 class_unregister(&srp_class);
2190 srp_release_transport(ib_srp_transport_template);
2193 module_init(srp_init_module);
2194 module_exit(srp_cleanup_module);