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)");
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->dev->dev, iu->buf, size, direction);
143 if (ib_dma_mapping_error(host->dev->dev, iu->dma))
147 iu->direction = direction;
159 static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
164 ib_dma_unmap_single(host->dev->dev, iu->dma, iu->size, iu->direction);
169 static void srp_qp_event(struct ib_event *event, void *context)
171 printk(KERN_ERR PFX "QP event %d\n", event->event);
174 static int srp_init_qp(struct srp_target_port *target,
177 struct ib_qp_attr *attr;
180 attr = kmalloc(sizeof *attr, GFP_KERNEL);
184 ret = ib_find_cached_pkey(target->srp_host->dev->dev,
185 target->srp_host->port,
186 be16_to_cpu(target->path.pkey),
191 attr->qp_state = IB_QPS_INIT;
192 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
193 IB_ACCESS_REMOTE_WRITE);
194 attr->port_num = target->srp_host->port;
196 ret = ib_modify_qp(qp, attr,
207 static int srp_create_target_ib(struct srp_target_port *target)
209 struct ib_qp_init_attr *init_attr;
212 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
216 target->cq = ib_create_cq(target->srp_host->dev->dev, srp_completion,
217 NULL, target, SRP_CQ_SIZE, 0);
218 if (IS_ERR(target->cq)) {
219 ret = PTR_ERR(target->cq);
223 ib_req_notify_cq(target->cq, IB_CQ_NEXT_COMP);
225 init_attr->event_handler = srp_qp_event;
226 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
227 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
228 init_attr->cap.max_recv_sge = 1;
229 init_attr->cap.max_send_sge = 1;
230 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
231 init_attr->qp_type = IB_QPT_RC;
232 init_attr->send_cq = target->cq;
233 init_attr->recv_cq = target->cq;
235 target->qp = ib_create_qp(target->srp_host->dev->pd, init_attr);
236 if (IS_ERR(target->qp)) {
237 ret = PTR_ERR(target->qp);
238 ib_destroy_cq(target->cq);
242 ret = srp_init_qp(target, target->qp);
244 ib_destroy_qp(target->qp);
245 ib_destroy_cq(target->cq);
254 static void srp_free_target_ib(struct srp_target_port *target)
258 ib_destroy_qp(target->qp);
259 ib_destroy_cq(target->cq);
261 for (i = 0; i < SRP_RQ_SIZE; ++i)
262 srp_free_iu(target->srp_host, target->rx_ring[i]);
263 for (i = 0; i < SRP_SQ_SIZE + 1; ++i)
264 srp_free_iu(target->srp_host, target->tx_ring[i]);
267 static void srp_path_rec_completion(int status,
268 struct ib_sa_path_rec *pathrec,
271 struct srp_target_port *target = target_ptr;
273 target->status = status;
275 printk(KERN_ERR PFX "Got failed path rec status %d\n", status);
277 target->path = *pathrec;
278 complete(&target->done);
281 static int srp_lookup_path(struct srp_target_port *target)
283 target->path.numb_path = 1;
285 init_completion(&target->done);
287 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
288 target->srp_host->dev->dev,
289 target->srp_host->port,
291 IB_SA_PATH_REC_SERVICE_ID |
292 IB_SA_PATH_REC_DGID |
293 IB_SA_PATH_REC_SGID |
294 IB_SA_PATH_REC_NUMB_PATH |
296 SRP_PATH_REC_TIMEOUT_MS,
298 srp_path_rec_completion,
299 target, &target->path_query);
300 if (target->path_query_id < 0)
301 return target->path_query_id;
303 wait_for_completion(&target->done);
305 if (target->status < 0)
306 printk(KERN_WARNING PFX "Path record query failed\n");
308 return target->status;
311 static int srp_send_req(struct srp_target_port *target)
314 struct ib_cm_req_param param;
315 struct srp_login_req priv;
319 req = kzalloc(sizeof *req, GFP_KERNEL);
323 req->param.primary_path = &target->path;
324 req->param.alternate_path = NULL;
325 req->param.service_id = target->service_id;
326 req->param.qp_num = target->qp->qp_num;
327 req->param.qp_type = target->qp->qp_type;
328 req->param.private_data = &req->priv;
329 req->param.private_data_len = sizeof req->priv;
330 req->param.flow_control = 1;
332 get_random_bytes(&req->param.starting_psn, 4);
333 req->param.starting_psn &= 0xffffff;
336 * Pick some arbitrary defaults here; we could make these
337 * module parameters if anyone cared about setting them.
339 req->param.responder_resources = 4;
340 req->param.remote_cm_response_timeout = 20;
341 req->param.local_cm_response_timeout = 20;
342 req->param.retry_count = 7;
343 req->param.rnr_retry_count = 7;
344 req->param.max_cm_retries = 15;
346 req->priv.opcode = SRP_LOGIN_REQ;
348 req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
349 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
350 SRP_BUF_FORMAT_INDIRECT);
352 * In the published SRP specification (draft rev. 16a), the
353 * port identifier format is 8 bytes of ID extension followed
354 * by 8 bytes of GUID. Older drafts put the two halves in the
355 * opposite order, so that the GUID comes first.
357 * Targets conforming to these obsolete drafts can be
358 * recognized by the I/O Class they report.
360 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
361 memcpy(req->priv.initiator_port_id,
362 &target->path.sgid.global.interface_id, 8);
363 memcpy(req->priv.initiator_port_id + 8,
364 &target->initiator_ext, 8);
365 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
366 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
368 memcpy(req->priv.initiator_port_id,
369 &target->initiator_ext, 8);
370 memcpy(req->priv.initiator_port_id + 8,
371 &target->path.sgid.global.interface_id, 8);
372 memcpy(req->priv.target_port_id, &target->id_ext, 8);
373 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
377 * Topspin/Cisco SRP targets will reject our login unless we
378 * zero out the first 8 bytes of our initiator port ID and set
379 * the second 8 bytes to the local node GUID.
381 if (srp_target_is_topspin(target)) {
382 printk(KERN_DEBUG PFX "Topspin/Cisco initiator port ID workaround "
383 "activated for target GUID %016llx\n",
384 (unsigned long long) be64_to_cpu(target->ioc_guid));
385 memset(req->priv.initiator_port_id, 0, 8);
386 memcpy(req->priv.initiator_port_id + 8,
387 &target->srp_host->dev->dev->node_guid, 8);
390 status = ib_send_cm_req(target->cm_id, &req->param);
397 static void srp_disconnect_target(struct srp_target_port *target)
399 /* XXX should send SRP_I_LOGOUT request */
401 init_completion(&target->done);
402 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
403 printk(KERN_DEBUG PFX "Sending CM DREQ failed\n");
406 wait_for_completion(&target->done);
409 static void srp_remove_work(struct work_struct *work)
411 struct srp_target_port *target =
412 container_of(work, struct srp_target_port, work);
414 spin_lock_irq(target->scsi_host->host_lock);
415 if (target->state != SRP_TARGET_DEAD) {
416 spin_unlock_irq(target->scsi_host->host_lock);
419 target->state = SRP_TARGET_REMOVED;
420 spin_unlock_irq(target->scsi_host->host_lock);
422 spin_lock(&target->srp_host->target_lock);
423 list_del(&target->list);
424 spin_unlock(&target->srp_host->target_lock);
426 srp_remove_host(target->scsi_host);
427 scsi_remove_host(target->scsi_host);
428 ib_destroy_cm_id(target->cm_id);
429 srp_free_target_ib(target);
430 scsi_host_put(target->scsi_host);
433 static int srp_connect_target(struct srp_target_port *target)
437 ret = srp_lookup_path(target);
442 init_completion(&target->done);
443 ret = srp_send_req(target);
446 wait_for_completion(&target->done);
449 * The CM event handling code will set status to
450 * SRP_PORT_REDIRECT if we get a port redirect REJ
451 * back, or SRP_DLID_REDIRECT if we get a lid/qp
454 switch (target->status) {
458 case SRP_PORT_REDIRECT:
459 ret = srp_lookup_path(target);
464 case SRP_DLID_REDIRECT:
468 return target->status;
473 static void srp_unmap_data(struct scsi_cmnd *scmnd,
474 struct srp_target_port *target,
475 struct srp_request *req)
477 if (!scsi_sglist(scmnd) ||
478 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
479 scmnd->sc_data_direction != DMA_FROM_DEVICE))
483 ib_fmr_pool_unmap(req->fmr);
487 ib_dma_unmap_sg(target->srp_host->dev->dev, scsi_sglist(scmnd),
488 scsi_sg_count(scmnd), scmnd->sc_data_direction);
491 static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
493 srp_unmap_data(req->scmnd, target, req);
494 list_move_tail(&req->list, &target->free_reqs);
497 static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
499 req->scmnd->result = DID_RESET << 16;
500 req->scmnd->scsi_done(req->scmnd);
501 srp_remove_req(target, req);
504 static int srp_reconnect_target(struct srp_target_port *target)
506 struct ib_cm_id *new_cm_id;
507 struct ib_qp_attr qp_attr;
508 struct srp_request *req, *tmp;
512 spin_lock_irq(target->scsi_host->host_lock);
513 if (target->state != SRP_TARGET_LIVE) {
514 spin_unlock_irq(target->scsi_host->host_lock);
517 target->state = SRP_TARGET_CONNECTING;
518 spin_unlock_irq(target->scsi_host->host_lock);
520 srp_disconnect_target(target);
522 * Now get a new local CM ID so that we avoid confusing the
523 * target in case things are really fouled up.
525 new_cm_id = ib_create_cm_id(target->srp_host->dev->dev,
526 srp_cm_handler, target);
527 if (IS_ERR(new_cm_id)) {
528 ret = PTR_ERR(new_cm_id);
531 ib_destroy_cm_id(target->cm_id);
532 target->cm_id = new_cm_id;
534 qp_attr.qp_state = IB_QPS_RESET;
535 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
539 ret = srp_init_qp(target, target->qp);
543 while (ib_poll_cq(target->cq, 1, &wc) > 0)
546 spin_lock_irq(target->scsi_host->host_lock);
547 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
548 srp_reset_req(target, req);
549 spin_unlock_irq(target->scsi_host->host_lock);
555 target->qp_in_error = 0;
556 ret = srp_connect_target(target);
560 spin_lock_irq(target->scsi_host->host_lock);
561 if (target->state == SRP_TARGET_CONNECTING) {
563 target->state = SRP_TARGET_LIVE;
566 spin_unlock_irq(target->scsi_host->host_lock);
571 printk(KERN_ERR PFX "reconnect failed (%d), removing target port.\n", ret);
574 * We couldn't reconnect, so kill our target port off.
575 * However, we have to defer the real removal because we might
576 * be in the context of the SCSI error handler now, which
577 * would deadlock if we call scsi_remove_host().
579 spin_lock_irq(target->scsi_host->host_lock);
580 if (target->state == SRP_TARGET_CONNECTING) {
581 target->state = SRP_TARGET_DEAD;
582 INIT_WORK(&target->work, srp_remove_work);
583 schedule_work(&target->work);
585 spin_unlock_irq(target->scsi_host->host_lock);
590 static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat,
591 int sg_cnt, struct srp_request *req,
592 struct srp_direct_buf *buf)
600 struct srp_device *dev = target->srp_host->dev;
601 struct ib_device *ibdev = dev->dev;
602 struct scatterlist *sg;
607 if (srp_target_is_mellanox(target) &&
608 (ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask))
612 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
613 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
615 if (ib_sg_dma_address(ibdev, sg) & ~dev->fmr_page_mask) {
621 if ((ib_sg_dma_address(ibdev, sg) + dma_len) &
622 ~dev->fmr_page_mask) {
632 page_cnt += len >> dev->fmr_page_shift;
633 if (page_cnt > SRP_FMR_SIZE)
636 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
641 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
642 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
644 for (j = 0; j < dma_len; j += dev->fmr_page_size)
645 dma_pages[page_cnt++] =
646 (ib_sg_dma_address(ibdev, sg) &
647 dev->fmr_page_mask) + j;
650 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
651 dma_pages, page_cnt, io_addr);
652 if (IS_ERR(req->fmr)) {
653 ret = PTR_ERR(req->fmr);
658 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) &
659 ~dev->fmr_page_mask);
660 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
661 buf->len = cpu_to_be32(len);
671 static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
672 struct srp_request *req)
674 struct scatterlist *scat;
675 struct srp_cmd *cmd = req->cmd->buf;
676 int len, nents, count;
677 u8 fmt = SRP_DATA_DESC_DIRECT;
678 struct srp_device *dev;
679 struct ib_device *ibdev;
681 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
682 return sizeof (struct srp_cmd);
684 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
685 scmnd->sc_data_direction != DMA_TO_DEVICE) {
686 printk(KERN_WARNING PFX "Unhandled data direction %d\n",
687 scmnd->sc_data_direction);
691 nents = scsi_sg_count(scmnd);
692 scat = scsi_sglist(scmnd);
694 dev = target->srp_host->dev;
697 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
699 fmt = SRP_DATA_DESC_DIRECT;
700 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
704 * The midlayer only generated a single gather/scatter
705 * entry, or DMA mapping coalesced everything to a
706 * single entry. So a direct descriptor along with
707 * the DMA MR suffices.
709 struct srp_direct_buf *buf = (void *) cmd->add_data;
711 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
712 buf->key = cpu_to_be32(dev->mr->rkey);
713 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
714 } else if (srp_map_fmr(target, scat, count, req,
715 (void *) cmd->add_data)) {
717 * FMR mapping failed, and the scatterlist has more
718 * than one entry. Generate an indirect memory
721 struct srp_indirect_buf *buf = (void *) cmd->add_data;
722 struct scatterlist *sg;
726 fmt = SRP_DATA_DESC_INDIRECT;
727 len = sizeof (struct srp_cmd) +
728 sizeof (struct srp_indirect_buf) +
729 count * sizeof (struct srp_direct_buf);
731 scsi_for_each_sg(scmnd, sg, count, i) {
732 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
734 buf->desc_list[i].va =
735 cpu_to_be64(ib_sg_dma_address(ibdev, sg));
736 buf->desc_list[i].key =
737 cpu_to_be32(dev->mr->rkey);
738 buf->desc_list[i].len = cpu_to_be32(dma_len);
742 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
743 cmd->data_out_desc_cnt = count;
745 cmd->data_in_desc_cnt = count;
748 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
749 buf->table_desc.key =
750 cpu_to_be32(target->srp_host->dev->mr->rkey);
751 buf->table_desc.len =
752 cpu_to_be32(count * sizeof (struct srp_direct_buf));
754 buf->len = cpu_to_be32(datalen);
757 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
758 cmd->buf_fmt = fmt << 4;
765 static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
767 struct srp_request *req;
768 struct scsi_cmnd *scmnd;
772 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
774 spin_lock_irqsave(target->scsi_host->host_lock, flags);
776 target->req_lim += delta;
778 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
780 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
781 if (be32_to_cpu(rsp->resp_data_len) < 4)
782 req->tsk_status = -1;
784 req->tsk_status = rsp->data[3];
785 complete(&req->done);
789 printk(KERN_ERR "Null scmnd for RSP w/tag %016llx\n",
790 (unsigned long long) rsp->tag);
791 scmnd->result = rsp->status;
793 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
794 memcpy(scmnd->sense_buffer, rsp->data +
795 be32_to_cpu(rsp->resp_data_len),
796 min_t(int, be32_to_cpu(rsp->sense_data_len),
797 SCSI_SENSE_BUFFERSIZE));
800 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
801 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
802 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
803 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
805 if (!req->tsk_mgmt) {
806 scmnd->host_scribble = (void *) -1L;
807 scmnd->scsi_done(scmnd);
809 srp_remove_req(target, req);
814 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
817 static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
819 struct ib_device *dev;
823 iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
825 dev = target->srp_host->dev->dev;
826 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
829 opcode = *(u8 *) iu->buf;
834 printk(KERN_ERR PFX "recv completion, opcode 0x%02x\n", opcode);
836 for (i = 0; i < wc->byte_len; ++i) {
838 printk(KERN_ERR " [%02x] ", i);
839 printk(" %02x", ((u8 *) iu->buf)[i]);
840 if ((i + 1) % 8 == 0)
844 if (wc->byte_len % 8)
850 srp_process_rsp(target, iu->buf);
854 /* XXX Handle target logout */
855 printk(KERN_WARNING PFX "Got target logout request\n");
859 printk(KERN_WARNING PFX "Unhandled SRP opcode 0x%02x\n", opcode);
863 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
867 static void srp_completion(struct ib_cq *cq, void *target_ptr)
869 struct srp_target_port *target = target_ptr;
872 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
873 while (ib_poll_cq(cq, 1, &wc) > 0) {
875 printk(KERN_ERR PFX "failed %s status %d\n",
876 wc.wr_id & SRP_OP_RECV ? "receive" : "send",
878 target->qp_in_error = 1;
882 if (wc.wr_id & SRP_OP_RECV)
883 srp_handle_recv(target, &wc);
889 static int __srp_post_recv(struct srp_target_port *target)
893 struct ib_recv_wr wr, *bad_wr;
897 next = target->rx_head & (SRP_RQ_SIZE - 1);
898 wr.wr_id = next | SRP_OP_RECV;
899 iu = target->rx_ring[next];
902 list.length = iu->size;
903 list.lkey = target->srp_host->dev->mr->lkey;
909 ret = ib_post_recv(target->qp, &wr, &bad_wr);
916 static int srp_post_recv(struct srp_target_port *target)
921 spin_lock_irqsave(target->scsi_host->host_lock, flags);
922 ret = __srp_post_recv(target);
923 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
929 * Must be called with target->scsi_host->host_lock held to protect
930 * req_lim and tx_head. Lock cannot be dropped between call here and
931 * call to __srp_post_send().
933 static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target)
935 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
938 if (unlikely(target->req_lim < 1))
939 ++target->zero_req_lim;
941 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
945 * Must be called with target->scsi_host->host_lock held to protect
946 * req_lim and tx_head.
948 static int __srp_post_send(struct srp_target_port *target,
949 struct srp_iu *iu, int len)
952 struct ib_send_wr wr, *bad_wr;
957 list.lkey = target->srp_host->dev->mr->lkey;
960 wr.wr_id = target->tx_head & SRP_SQ_SIZE;
963 wr.opcode = IB_WR_SEND;
964 wr.send_flags = IB_SEND_SIGNALED;
966 ret = ib_post_send(target->qp, &wr, &bad_wr);
976 static int srp_queuecommand(struct scsi_cmnd *scmnd,
977 void (*done)(struct scsi_cmnd *))
979 struct srp_target_port *target = host_to_target(scmnd->device->host);
980 struct srp_request *req;
983 struct ib_device *dev;
986 if (target->state == SRP_TARGET_CONNECTING)
989 if (target->state == SRP_TARGET_DEAD ||
990 target->state == SRP_TARGET_REMOVED) {
991 scmnd->result = DID_BAD_TARGET << 16;
996 iu = __srp_get_tx_iu(target);
1000 dev = target->srp_host->dev->dev;
1001 ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len,
1004 req = list_entry(target->free_reqs.next, struct srp_request, list);
1006 scmnd->scsi_done = done;
1008 scmnd->host_scribble = (void *) (long) req->index;
1011 memset(cmd, 0, sizeof *cmd);
1013 cmd->opcode = SRP_CMD;
1014 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
1015 cmd->tag = req->index;
1016 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1021 req->tsk_mgmt = NULL;
1023 len = srp_map_data(scmnd, target, req);
1025 printk(KERN_ERR PFX "Failed to map data\n");
1029 if (__srp_post_recv(target)) {
1030 printk(KERN_ERR PFX "Recv failed\n");
1034 ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len,
1037 if (__srp_post_send(target, iu, len)) {
1038 printk(KERN_ERR PFX "Send failed\n");
1042 list_move_tail(&req->list, &target->req_queue);
1047 srp_unmap_data(scmnd, target, req);
1050 return SCSI_MLQUEUE_HOST_BUSY;
1053 static int srp_alloc_iu_bufs(struct srp_target_port *target)
1057 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1058 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1059 target->max_ti_iu_len,
1060 GFP_KERNEL, DMA_FROM_DEVICE);
1061 if (!target->rx_ring[i])
1065 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1066 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
1068 GFP_KERNEL, DMA_TO_DEVICE);
1069 if (!target->tx_ring[i])
1076 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1077 srp_free_iu(target->srp_host, target->rx_ring[i]);
1078 target->rx_ring[i] = NULL;
1081 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1082 srp_free_iu(target->srp_host, target->tx_ring[i]);
1083 target->tx_ring[i] = NULL;
1089 static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1090 struct ib_cm_event *event,
1091 struct srp_target_port *target)
1093 struct ib_class_port_info *cpi;
1096 switch (event->param.rej_rcvd.reason) {
1097 case IB_CM_REJ_PORT_CM_REDIRECT:
1098 cpi = event->param.rej_rcvd.ari;
1099 target->path.dlid = cpi->redirect_lid;
1100 target->path.pkey = cpi->redirect_pkey;
1101 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1102 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1104 target->status = target->path.dlid ?
1105 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1108 case IB_CM_REJ_PORT_REDIRECT:
1109 if (srp_target_is_topspin(target)) {
1111 * Topspin/Cisco SRP gateways incorrectly send
1112 * reject reason code 25 when they mean 24
1115 memcpy(target->path.dgid.raw,
1116 event->param.rej_rcvd.ari, 16);
1118 printk(KERN_DEBUG PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1119 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1120 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1122 target->status = SRP_PORT_REDIRECT;
1124 printk(KERN_WARNING " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1125 target->status = -ECONNRESET;
1129 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
1130 printk(KERN_WARNING " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1131 target->status = -ECONNRESET;
1134 case IB_CM_REJ_CONSUMER_DEFINED:
1135 opcode = *(u8 *) event->private_data;
1136 if (opcode == SRP_LOGIN_REJ) {
1137 struct srp_login_rej *rej = event->private_data;
1138 u32 reason = be32_to_cpu(rej->reason);
1140 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
1141 printk(KERN_WARNING PFX
1142 "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1144 printk(KERN_WARNING PFX
1145 "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
1147 printk(KERN_WARNING " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1148 " opcode 0x%02x\n", opcode);
1149 target->status = -ECONNRESET;
1153 printk(KERN_WARNING " REJ reason 0x%x\n",
1154 event->param.rej_rcvd.reason);
1155 target->status = -ECONNRESET;
1159 static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1161 struct srp_target_port *target = cm_id->context;
1162 struct ib_qp_attr *qp_attr = NULL;
1167 switch (event->event) {
1168 case IB_CM_REQ_ERROR:
1169 printk(KERN_DEBUG PFX "Sending CM REQ failed\n");
1171 target->status = -ECONNRESET;
1174 case IB_CM_REP_RECEIVED:
1176 opcode = *(u8 *) event->private_data;
1178 if (opcode == SRP_LOGIN_RSP) {
1179 struct srp_login_rsp *rsp = event->private_data;
1181 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1182 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1184 target->scsi_host->can_queue = min(target->req_lim,
1185 target->scsi_host->can_queue);
1187 printk(KERN_WARNING PFX "Unhandled RSP opcode %#x\n", opcode);
1188 target->status = -ECONNRESET;
1192 if (!target->rx_ring[0]) {
1193 target->status = srp_alloc_iu_bufs(target);
1198 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1200 target->status = -ENOMEM;
1204 qp_attr->qp_state = IB_QPS_RTR;
1205 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1209 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1213 target->status = srp_post_recv(target);
1217 qp_attr->qp_state = IB_QPS_RTS;
1218 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1222 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1226 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1232 case IB_CM_REJ_RECEIVED:
1233 printk(KERN_DEBUG PFX "REJ received\n");
1236 srp_cm_rej_handler(cm_id, event, target);
1239 case IB_CM_DREQ_RECEIVED:
1240 printk(KERN_WARNING PFX "DREQ received - connection closed\n");
1241 if (ib_send_cm_drep(cm_id, NULL, 0))
1242 printk(KERN_ERR PFX "Sending CM DREP failed\n");
1245 case IB_CM_TIMEWAIT_EXIT:
1246 printk(KERN_ERR PFX "connection closed\n");
1252 case IB_CM_MRA_RECEIVED:
1253 case IB_CM_DREQ_ERROR:
1254 case IB_CM_DREP_RECEIVED:
1258 printk(KERN_WARNING PFX "Unhandled CM event %d\n", event->event);
1263 complete(&target->done);
1270 static int srp_send_tsk_mgmt(struct srp_target_port *target,
1271 struct srp_request *req, u8 func)
1274 struct srp_tsk_mgmt *tsk_mgmt;
1276 spin_lock_irq(target->scsi_host->host_lock);
1278 if (target->state == SRP_TARGET_DEAD ||
1279 target->state == SRP_TARGET_REMOVED) {
1280 req->scmnd->result = DID_BAD_TARGET << 16;
1284 init_completion(&req->done);
1286 iu = __srp_get_tx_iu(target);
1291 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1293 tsk_mgmt->opcode = SRP_TSK_MGMT;
1294 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1295 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
1296 tsk_mgmt->tsk_mgmt_func = func;
1297 tsk_mgmt->task_tag = req->index;
1299 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1304 spin_unlock_irq(target->scsi_host->host_lock);
1306 if (!wait_for_completion_timeout(&req->done,
1307 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
1313 spin_unlock_irq(target->scsi_host->host_lock);
1317 static int srp_find_req(struct srp_target_port *target,
1318 struct scsi_cmnd *scmnd,
1319 struct srp_request **req)
1321 if (scmnd->host_scribble == (void *) -1L)
1324 *req = &target->req_ring[(long) scmnd->host_scribble];
1329 static int srp_abort(struct scsi_cmnd *scmnd)
1331 struct srp_target_port *target = host_to_target(scmnd->device->host);
1332 struct srp_request *req;
1335 printk(KERN_ERR "SRP abort called\n");
1337 if (target->qp_in_error)
1339 if (srp_find_req(target, scmnd, &req))
1341 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1344 spin_lock_irq(target->scsi_host->host_lock);
1346 if (req->cmd_done) {
1347 srp_remove_req(target, req);
1348 scmnd->scsi_done(scmnd);
1349 } else if (!req->tsk_status) {
1350 srp_remove_req(target, req);
1351 scmnd->result = DID_ABORT << 16;
1355 spin_unlock_irq(target->scsi_host->host_lock);
1360 static int srp_reset_device(struct scsi_cmnd *scmnd)
1362 struct srp_target_port *target = host_to_target(scmnd->device->host);
1363 struct srp_request *req, *tmp;
1365 printk(KERN_ERR "SRP reset_device called\n");
1367 if (target->qp_in_error)
1369 if (srp_find_req(target, scmnd, &req))
1371 if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1373 if (req->tsk_status)
1376 spin_lock_irq(target->scsi_host->host_lock);
1378 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
1379 if (req->scmnd->device == scmnd->device)
1380 srp_reset_req(target, req);
1382 spin_unlock_irq(target->scsi_host->host_lock);
1387 static int srp_reset_host(struct scsi_cmnd *scmnd)
1389 struct srp_target_port *target = host_to_target(scmnd->device->host);
1392 printk(KERN_ERR PFX "SRP reset_host called\n");
1394 if (!srp_reconnect_target(target))
1400 static ssize_t show_id_ext(struct class_device *cdev, char *buf)
1402 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1404 if (target->state == SRP_TARGET_DEAD ||
1405 target->state == SRP_TARGET_REMOVED)
1408 return sprintf(buf, "0x%016llx\n",
1409 (unsigned long long) be64_to_cpu(target->id_ext));
1412 static ssize_t show_ioc_guid(struct class_device *cdev, char *buf)
1414 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1416 if (target->state == SRP_TARGET_DEAD ||
1417 target->state == SRP_TARGET_REMOVED)
1420 return sprintf(buf, "0x%016llx\n",
1421 (unsigned long long) be64_to_cpu(target->ioc_guid));
1424 static ssize_t show_service_id(struct class_device *cdev, char *buf)
1426 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1428 if (target->state == SRP_TARGET_DEAD ||
1429 target->state == SRP_TARGET_REMOVED)
1432 return sprintf(buf, "0x%016llx\n",
1433 (unsigned long long) be64_to_cpu(target->service_id));
1436 static ssize_t show_pkey(struct class_device *cdev, char *buf)
1438 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1440 if (target->state == SRP_TARGET_DEAD ||
1441 target->state == SRP_TARGET_REMOVED)
1444 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1447 static ssize_t show_dgid(struct class_device *cdev, char *buf)
1449 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1451 if (target->state == SRP_TARGET_DEAD ||
1452 target->state == SRP_TARGET_REMOVED)
1455 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1456 be16_to_cpu(((__be16 *) target->path.dgid.raw)[0]),
1457 be16_to_cpu(((__be16 *) target->path.dgid.raw)[1]),
1458 be16_to_cpu(((__be16 *) target->path.dgid.raw)[2]),
1459 be16_to_cpu(((__be16 *) target->path.dgid.raw)[3]),
1460 be16_to_cpu(((__be16 *) target->path.dgid.raw)[4]),
1461 be16_to_cpu(((__be16 *) target->path.dgid.raw)[5]),
1462 be16_to_cpu(((__be16 *) target->path.dgid.raw)[6]),
1463 be16_to_cpu(((__be16 *) target->path.dgid.raw)[7]));
1466 static ssize_t show_orig_dgid(struct class_device *cdev, char *buf)
1468 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1470 if (target->state == SRP_TARGET_DEAD ||
1471 target->state == SRP_TARGET_REMOVED)
1474 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1475 be16_to_cpu(target->orig_dgid[0]),
1476 be16_to_cpu(target->orig_dgid[1]),
1477 be16_to_cpu(target->orig_dgid[2]),
1478 be16_to_cpu(target->orig_dgid[3]),
1479 be16_to_cpu(target->orig_dgid[4]),
1480 be16_to_cpu(target->orig_dgid[5]),
1481 be16_to_cpu(target->orig_dgid[6]),
1482 be16_to_cpu(target->orig_dgid[7]));
1485 static ssize_t show_zero_req_lim(struct class_device *cdev, char *buf)
1487 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1489 if (target->state == SRP_TARGET_DEAD ||
1490 target->state == SRP_TARGET_REMOVED)
1493 return sprintf(buf, "%d\n", target->zero_req_lim);
1496 static ssize_t show_local_ib_port(struct class_device *cdev, char *buf)
1498 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1500 return sprintf(buf, "%d\n", target->srp_host->port);
1503 static ssize_t show_local_ib_device(struct class_device *cdev, char *buf)
1505 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1507 return sprintf(buf, "%s\n", target->srp_host->dev->dev->name);
1510 static CLASS_DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1511 static CLASS_DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1512 static CLASS_DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1513 static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1514 static CLASS_DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1515 static CLASS_DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
1516 static CLASS_DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1517 static CLASS_DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1518 static CLASS_DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
1520 static struct class_device_attribute *srp_host_attrs[] = {
1521 &class_device_attr_id_ext,
1522 &class_device_attr_ioc_guid,
1523 &class_device_attr_service_id,
1524 &class_device_attr_pkey,
1525 &class_device_attr_dgid,
1526 &class_device_attr_orig_dgid,
1527 &class_device_attr_zero_req_lim,
1528 &class_device_attr_local_ib_port,
1529 &class_device_attr_local_ib_device,
1533 static struct scsi_host_template srp_template = {
1534 .module = THIS_MODULE,
1535 .name = "InfiniBand SRP initiator",
1536 .proc_name = DRV_NAME,
1537 .info = srp_target_info,
1538 .queuecommand = srp_queuecommand,
1539 .eh_abort_handler = srp_abort,
1540 .eh_device_reset_handler = srp_reset_device,
1541 .eh_host_reset_handler = srp_reset_host,
1542 .can_queue = SRP_SQ_SIZE,
1544 .cmd_per_lun = SRP_SQ_SIZE,
1545 .use_clustering = ENABLE_CLUSTERING,
1546 .shost_attrs = srp_host_attrs
1549 static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1551 struct srp_rport_identifiers ids;
1552 struct srp_rport *rport;
1554 sprintf(target->target_name, "SRP.T10:%016llX",
1555 (unsigned long long) be64_to_cpu(target->id_ext));
1557 if (scsi_add_host(target->scsi_host, host->dev->dev->dma_device))
1560 memcpy(ids.port_id, &target->id_ext, 8);
1561 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
1562 ids.roles = SRP_RPORT_ROLE_TARGET;
1563 rport = srp_rport_add(target->scsi_host, &ids);
1564 if (IS_ERR(rport)) {
1565 scsi_remove_host(target->scsi_host);
1566 return PTR_ERR(rport);
1569 spin_lock(&host->target_lock);
1570 list_add_tail(&target->list, &host->target_list);
1571 spin_unlock(&host->target_lock);
1573 target->state = SRP_TARGET_LIVE;
1575 scsi_scan_target(&target->scsi_host->shost_gendev,
1576 0, target->scsi_id, SCAN_WILD_CARD, 0);
1581 static void srp_release_class_dev(struct class_device *class_dev)
1583 struct srp_host *host =
1584 container_of(class_dev, struct srp_host, class_dev);
1586 complete(&host->released);
1589 static struct class srp_class = {
1590 .name = "infiniband_srp",
1591 .release = srp_release_class_dev
1595 * Target ports are added by writing
1597 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1598 * pkey=<P_Key>,service_id=<service ID>
1600 * to the add_target sysfs attribute.
1604 SRP_OPT_ID_EXT = 1 << 0,
1605 SRP_OPT_IOC_GUID = 1 << 1,
1606 SRP_OPT_DGID = 1 << 2,
1607 SRP_OPT_PKEY = 1 << 3,
1608 SRP_OPT_SERVICE_ID = 1 << 4,
1609 SRP_OPT_MAX_SECT = 1 << 5,
1610 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
1611 SRP_OPT_IO_CLASS = 1 << 7,
1612 SRP_OPT_INITIATOR_EXT = 1 << 8,
1613 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1617 SRP_OPT_SERVICE_ID),
1620 static match_table_t srp_opt_tokens = {
1621 { SRP_OPT_ID_EXT, "id_ext=%s" },
1622 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1623 { SRP_OPT_DGID, "dgid=%s" },
1624 { SRP_OPT_PKEY, "pkey=%x" },
1625 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1626 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1627 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
1628 { SRP_OPT_IO_CLASS, "io_class=%x" },
1629 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
1630 { SRP_OPT_ERR, NULL }
1633 static int srp_parse_options(const char *buf, struct srp_target_port *target)
1635 char *options, *sep_opt;
1638 substring_t args[MAX_OPT_ARGS];
1644 options = kstrdup(buf, GFP_KERNEL);
1649 while ((p = strsep(&sep_opt, ",")) != NULL) {
1653 token = match_token(p, srp_opt_tokens, args);
1657 case SRP_OPT_ID_EXT:
1658 p = match_strdup(args);
1663 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1667 case SRP_OPT_IOC_GUID:
1668 p = match_strdup(args);
1673 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1678 p = match_strdup(args);
1683 if (strlen(p) != 32) {
1684 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
1689 for (i = 0; i < 16; ++i) {
1690 strlcpy(dgid, p + i * 2, 3);
1691 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1694 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
1698 if (match_hex(args, &token)) {
1699 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1702 target->path.pkey = cpu_to_be16(token);
1705 case SRP_OPT_SERVICE_ID:
1706 p = match_strdup(args);
1711 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
1712 target->path.service_id = target->service_id;
1716 case SRP_OPT_MAX_SECT:
1717 if (match_int(args, &token)) {
1718 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1721 target->scsi_host->max_sectors = token;
1724 case SRP_OPT_MAX_CMD_PER_LUN:
1725 if (match_int(args, &token)) {
1726 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1729 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1732 case SRP_OPT_IO_CLASS:
1733 if (match_hex(args, &token)) {
1734 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1737 if (token != SRP_REV10_IB_IO_CLASS &&
1738 token != SRP_REV16A_IB_IO_CLASS) {
1739 printk(KERN_WARNING PFX "unknown IO class parameter value"
1740 " %x specified (use %x or %x).\n",
1741 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1744 target->io_class = token;
1747 case SRP_OPT_INITIATOR_EXT:
1748 p = match_strdup(args);
1753 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1758 printk(KERN_WARNING PFX "unknown parameter or missing value "
1759 "'%s' in target creation request\n", p);
1764 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1767 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1768 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1769 !(srp_opt_tokens[i].token & opt_mask))
1770 printk(KERN_WARNING PFX "target creation request is "
1771 "missing parameter '%s'\n",
1772 srp_opt_tokens[i].pattern);
1779 static ssize_t srp_create_target(struct class_device *class_dev,
1780 const char *buf, size_t count)
1782 struct srp_host *host =
1783 container_of(class_dev, struct srp_host, class_dev);
1784 struct Scsi_Host *target_host;
1785 struct srp_target_port *target;
1789 target_host = scsi_host_alloc(&srp_template,
1790 sizeof (struct srp_target_port));
1794 target_host->transportt = ib_srp_transport_template;
1795 target_host->max_lun = SRP_MAX_LUN;
1796 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
1798 target = host_to_target(target_host);
1800 target->io_class = SRP_REV16A_IB_IO_CLASS;
1801 target->scsi_host = target_host;
1802 target->srp_host = host;
1804 INIT_LIST_HEAD(&target->free_reqs);
1805 INIT_LIST_HEAD(&target->req_queue);
1806 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1807 target->req_ring[i].index = i;
1808 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1811 ret = srp_parse_options(buf, target);
1815 ib_get_cached_gid(host->dev->dev, host->port, 0, &target->path.sgid);
1817 printk(KERN_DEBUG PFX "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1818 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1819 (unsigned long long) be64_to_cpu(target->id_ext),
1820 (unsigned long long) be64_to_cpu(target->ioc_guid),
1821 be16_to_cpu(target->path.pkey),
1822 (unsigned long long) be64_to_cpu(target->service_id),
1823 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]),
1824 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]),
1825 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]),
1826 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]),
1827 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]),
1828 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]),
1829 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]),
1830 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14]));
1832 ret = srp_create_target_ib(target);
1836 target->cm_id = ib_create_cm_id(host->dev->dev, srp_cm_handler, target);
1837 if (IS_ERR(target->cm_id)) {
1838 ret = PTR_ERR(target->cm_id);
1842 target->qp_in_error = 0;
1843 ret = srp_connect_target(target);
1845 printk(KERN_ERR PFX "Connection failed\n");
1849 ret = srp_add_target(host, target);
1851 goto err_disconnect;
1856 srp_disconnect_target(target);
1859 ib_destroy_cm_id(target->cm_id);
1862 srp_free_target_ib(target);
1865 scsi_host_put(target_host);
1870 static CLASS_DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
1872 static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
1874 struct srp_host *host =
1875 container_of(class_dev, struct srp_host, class_dev);
1877 return sprintf(buf, "%s\n", host->dev->dev->name);
1880 static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1882 static ssize_t show_port(struct class_device *class_dev, char *buf)
1884 struct srp_host *host =
1885 container_of(class_dev, struct srp_host, class_dev);
1887 return sprintf(buf, "%d\n", host->port);
1890 static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
1892 static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
1894 struct srp_host *host;
1896 host = kzalloc(sizeof *host, GFP_KERNEL);
1900 INIT_LIST_HEAD(&host->target_list);
1901 spin_lock_init(&host->target_lock);
1902 init_completion(&host->released);
1906 host->class_dev.class = &srp_class;
1907 host->class_dev.dev = device->dev->dma_device;
1908 snprintf(host->class_dev.class_id, BUS_ID_SIZE, "srp-%s-%d",
1909 device->dev->name, port);
1911 if (class_device_register(&host->class_dev))
1913 if (class_device_create_file(&host->class_dev, &class_device_attr_add_target))
1915 if (class_device_create_file(&host->class_dev, &class_device_attr_ibdev))
1917 if (class_device_create_file(&host->class_dev, &class_device_attr_port))
1923 class_device_unregister(&host->class_dev);
1931 static void srp_add_one(struct ib_device *device)
1933 struct srp_device *srp_dev;
1934 struct ib_device_attr *dev_attr;
1935 struct ib_fmr_pool_param fmr_param;
1936 struct srp_host *host;
1939 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
1943 if (ib_query_device(device, dev_attr)) {
1944 printk(KERN_WARNING PFX "Query device failed for %s\n",
1949 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
1954 * Use the smallest page size supported by the HCA, down to a
1955 * minimum of 512 bytes (which is the smallest sector that a
1956 * SCSI command will ever carry).
1958 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
1959 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
1960 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
1962 INIT_LIST_HEAD(&srp_dev->dev_list);
1964 srp_dev->dev = device;
1965 srp_dev->pd = ib_alloc_pd(device);
1966 if (IS_ERR(srp_dev->pd))
1969 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
1970 IB_ACCESS_LOCAL_WRITE |
1971 IB_ACCESS_REMOTE_READ |
1972 IB_ACCESS_REMOTE_WRITE);
1973 if (IS_ERR(srp_dev->mr))
1976 memset(&fmr_param, 0, sizeof fmr_param);
1977 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
1978 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
1979 fmr_param.cache = 1;
1980 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
1981 fmr_param.page_shift = srp_dev->fmr_page_shift;
1982 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
1983 IB_ACCESS_REMOTE_WRITE |
1984 IB_ACCESS_REMOTE_READ);
1986 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
1987 if (IS_ERR(srp_dev->fmr_pool))
1988 srp_dev->fmr_pool = NULL;
1990 if (device->node_type == RDMA_NODE_IB_SWITCH) {
1995 e = device->phys_port_cnt;
1998 for (p = s; p <= e; ++p) {
1999 host = srp_add_port(srp_dev, p);
2001 list_add_tail(&host->list, &srp_dev->dev_list);
2004 ib_set_client_data(device, &srp_client, srp_dev);
2009 ib_dealloc_pd(srp_dev->pd);
2018 static void srp_remove_one(struct ib_device *device)
2020 struct srp_device *srp_dev;
2021 struct srp_host *host, *tmp_host;
2022 LIST_HEAD(target_list);
2023 struct srp_target_port *target, *tmp_target;
2025 srp_dev = ib_get_client_data(device, &srp_client);
2027 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
2028 class_device_unregister(&host->class_dev);
2030 * Wait for the sysfs entry to go away, so that no new
2031 * target ports can be created.
2033 wait_for_completion(&host->released);
2036 * Mark all target ports as removed, so we stop queueing
2037 * commands and don't try to reconnect.
2039 spin_lock(&host->target_lock);
2040 list_for_each_entry(target, &host->target_list, list) {
2041 spin_lock_irq(target->scsi_host->host_lock);
2042 target->state = SRP_TARGET_REMOVED;
2043 spin_unlock_irq(target->scsi_host->host_lock);
2045 spin_unlock(&host->target_lock);
2048 * Wait for any reconnection tasks that may have
2049 * started before we marked our target ports as
2050 * removed, and any target port removal tasks.
2052 flush_scheduled_work();
2054 list_for_each_entry_safe(target, tmp_target,
2055 &host->target_list, list) {
2056 scsi_remove_host(target->scsi_host);
2057 srp_disconnect_target(target);
2058 ib_destroy_cm_id(target->cm_id);
2059 srp_free_target_ib(target);
2060 scsi_host_put(target->scsi_host);
2066 if (srp_dev->fmr_pool)
2067 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2068 ib_dereg_mr(srp_dev->mr);
2069 ib_dealloc_pd(srp_dev->pd);
2074 static struct srp_function_template ib_srp_transport_functions = {
2077 static int __init srp_init_module(void)
2081 ib_srp_transport_template =
2082 srp_attach_transport(&ib_srp_transport_functions);
2083 if (!ib_srp_transport_template)
2086 srp_template.sg_tablesize = srp_sg_tablesize;
2087 srp_max_iu_len = (sizeof (struct srp_cmd) +
2088 sizeof (struct srp_indirect_buf) +
2089 srp_sg_tablesize * 16);
2091 ret = class_register(&srp_class);
2093 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
2094 srp_release_transport(ib_srp_transport_template);
2098 ib_sa_register_client(&srp_sa_client);
2100 ret = ib_register_client(&srp_client);
2102 printk(KERN_ERR PFX "couldn't register IB client\n");
2103 srp_release_transport(ib_srp_transport_template);
2104 ib_sa_unregister_client(&srp_sa_client);
2105 class_unregister(&srp_class);
2112 static void __exit srp_cleanup_module(void)
2114 ib_unregister_client(&srp_client);
2115 ib_sa_unregister_client(&srp_sa_client);
2116 class_unregister(&srp_class);
2117 srp_release_transport(ib_srp_transport_template);
2120 module_init(srp_init_module);
2121 module_exit(srp_cleanup_module);