2 * Copyright (c) 2005 Cisco Systems. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * $Id: ib_srp.c 3932 2005-11-01 17:19:29Z roland $
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/err.h>
39 #include <linux/string.h>
40 #include <linux/parser.h>
41 #include <linux/random.h>
42 #include <linux/jiffies.h>
44 #include <asm/atomic.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_device.h>
48 #include <scsi/scsi_dbg.h>
51 #include <rdma/ib_cache.h>
55 #define DRV_NAME "ib_srp"
56 #define PFX DRV_NAME ": "
57 #define DRV_VERSION "0.2"
58 #define DRV_RELDATE "November 1, 2005"
60 MODULE_AUTHOR("Roland Dreier");
61 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
62 "v" DRV_VERSION " (" DRV_RELDATE ")");
63 MODULE_LICENSE("Dual BSD/GPL");
65 static int srp_sg_tablesize = SRP_DEF_SG_TABLESIZE;
66 static int srp_max_iu_len;
68 module_param(srp_sg_tablesize, int, 0444);
69 MODULE_PARM_DESC(srp_sg_tablesize,
70 "Max number of gather/scatter entries per I/O (default is 12)");
72 static int topspin_workarounds = 1;
74 module_param(topspin_workarounds, int, 0444);
75 MODULE_PARM_DESC(topspin_workarounds,
76 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
78 static int mellanox_workarounds = 1;
80 module_param(mellanox_workarounds, int, 0444);
81 MODULE_PARM_DESC(mellanox_workarounds,
82 "Enable workarounds for Mellanox SRP target bugs if != 0");
84 static void srp_add_one(struct ib_device *device);
85 static void srp_remove_one(struct ib_device *device);
86 static void srp_completion(struct ib_cq *cq, void *target_ptr);
87 static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
89 static struct ib_client srp_client = {
92 .remove = srp_remove_one
95 static struct ib_sa_client srp_sa_client;
97 static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
99 return (struct srp_target_port *) host->hostdata;
102 static const char *srp_target_info(struct Scsi_Host *host)
104 return host_to_target(host)->target_name;
107 static int srp_target_is_topspin(struct srp_target_port *target)
109 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
110 static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
112 return topspin_workarounds &&
113 (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
114 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
117 static int srp_target_is_mellanox(struct srp_target_port *target)
119 static const u8 mellanox_oui[3] = { 0x00, 0x02, 0xc9 };
121 return mellanox_workarounds &&
122 !memcmp(&target->ioc_guid, mellanox_oui, sizeof mellanox_oui);
125 static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
127 enum dma_data_direction direction)
131 iu = kmalloc(sizeof *iu, gfp_mask);
135 iu->buf = kzalloc(size, gfp_mask);
139 iu->dma = ib_dma_map_single(host->dev->dev, iu->buf, size, direction);
140 if (ib_dma_mapping_error(host->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->dev->dev, iu->dma, iu->size, iu->direction);
166 static void srp_qp_event(struct ib_event *event, void *context)
168 printk(KERN_ERR PFX "QP event %d\n", event->event);
171 static int srp_init_qp(struct srp_target_port *target,
174 struct ib_qp_attr *attr;
177 attr = kmalloc(sizeof *attr, GFP_KERNEL);
181 ret = ib_find_cached_pkey(target->srp_host->dev->dev,
182 target->srp_host->port,
183 be16_to_cpu(target->path.pkey),
188 attr->qp_state = IB_QPS_INIT;
189 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
190 IB_ACCESS_REMOTE_WRITE);
191 attr->port_num = target->srp_host->port;
193 ret = ib_modify_qp(qp, attr,
204 static int srp_create_target_ib(struct srp_target_port *target)
206 struct ib_qp_init_attr *init_attr;
209 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
213 target->cq = ib_create_cq(target->srp_host->dev->dev, srp_completion,
214 NULL, target, SRP_CQ_SIZE, 0);
215 if (IS_ERR(target->cq)) {
216 ret = PTR_ERR(target->cq);
220 ib_req_notify_cq(target->cq, IB_CQ_NEXT_COMP);
222 init_attr->event_handler = srp_qp_event;
223 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
224 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
225 init_attr->cap.max_recv_sge = 1;
226 init_attr->cap.max_send_sge = 1;
227 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
228 init_attr->qp_type = IB_QPT_RC;
229 init_attr->send_cq = target->cq;
230 init_attr->recv_cq = target->cq;
232 target->qp = ib_create_qp(target->srp_host->dev->pd, init_attr);
233 if (IS_ERR(target->qp)) {
234 ret = PTR_ERR(target->qp);
235 ib_destroy_cq(target->cq);
239 ret = srp_init_qp(target, target->qp);
241 ib_destroy_qp(target->qp);
242 ib_destroy_cq(target->cq);
251 static void srp_free_target_ib(struct srp_target_port *target)
255 ib_destroy_qp(target->qp);
256 ib_destroy_cq(target->cq);
258 for (i = 0; i < SRP_RQ_SIZE; ++i)
259 srp_free_iu(target->srp_host, target->rx_ring[i]);
260 for (i = 0; i < SRP_SQ_SIZE + 1; ++i)
261 srp_free_iu(target->srp_host, target->tx_ring[i]);
264 static void srp_path_rec_completion(int status,
265 struct ib_sa_path_rec *pathrec,
268 struct srp_target_port *target = target_ptr;
270 target->status = status;
272 printk(KERN_ERR PFX "Got failed path rec status %d\n", status);
274 target->path = *pathrec;
275 complete(&target->done);
278 static int srp_lookup_path(struct srp_target_port *target)
280 target->path.numb_path = 1;
282 init_completion(&target->done);
284 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
285 target->srp_host->dev->dev,
286 target->srp_host->port,
288 IB_SA_PATH_REC_SERVICE_ID |
289 IB_SA_PATH_REC_DGID |
290 IB_SA_PATH_REC_SGID |
291 IB_SA_PATH_REC_NUMB_PATH |
293 SRP_PATH_REC_TIMEOUT_MS,
295 srp_path_rec_completion,
296 target, &target->path_query);
297 if (target->path_query_id < 0)
298 return target->path_query_id;
300 wait_for_completion(&target->done);
302 if (target->status < 0)
303 printk(KERN_WARNING PFX "Path record query failed\n");
305 return target->status;
308 static int srp_send_req(struct srp_target_port *target)
311 struct ib_cm_req_param param;
312 struct srp_login_req priv;
316 req = kzalloc(sizeof *req, GFP_KERNEL);
320 req->param.primary_path = &target->path;
321 req->param.alternate_path = NULL;
322 req->param.service_id = target->service_id;
323 req->param.qp_num = target->qp->qp_num;
324 req->param.qp_type = target->qp->qp_type;
325 req->param.private_data = &req->priv;
326 req->param.private_data_len = sizeof req->priv;
327 req->param.flow_control = 1;
329 get_random_bytes(&req->param.starting_psn, 4);
330 req->param.starting_psn &= 0xffffff;
333 * Pick some arbitrary defaults here; we could make these
334 * module parameters if anyone cared about setting them.
336 req->param.responder_resources = 4;
337 req->param.remote_cm_response_timeout = 20;
338 req->param.local_cm_response_timeout = 20;
339 req->param.retry_count = 7;
340 req->param.rnr_retry_count = 7;
341 req->param.max_cm_retries = 15;
343 req->priv.opcode = SRP_LOGIN_REQ;
345 req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
346 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
347 SRP_BUF_FORMAT_INDIRECT);
349 * In the published SRP specification (draft rev. 16a), the
350 * port identifier format is 8 bytes of ID extension followed
351 * by 8 bytes of GUID. Older drafts put the two halves in the
352 * opposite order, so that the GUID comes first.
354 * Targets conforming to these obsolete drafts can be
355 * recognized by the I/O Class they report.
357 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
358 memcpy(req->priv.initiator_port_id,
359 &target->path.sgid.global.interface_id, 8);
360 memcpy(req->priv.initiator_port_id + 8,
361 &target->initiator_ext, 8);
362 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
363 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
365 memcpy(req->priv.initiator_port_id,
366 &target->initiator_ext, 8);
367 memcpy(req->priv.initiator_port_id + 8,
368 &target->path.sgid.global.interface_id, 8);
369 memcpy(req->priv.target_port_id, &target->id_ext, 8);
370 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
374 * Topspin/Cisco SRP targets will reject our login unless we
375 * zero out the first 8 bytes of our initiator port ID and set
376 * the second 8 bytes to the local node GUID.
378 if (srp_target_is_topspin(target)) {
379 printk(KERN_DEBUG PFX "Topspin/Cisco initiator port ID workaround "
380 "activated for target GUID %016llx\n",
381 (unsigned long long) be64_to_cpu(target->ioc_guid));
382 memset(req->priv.initiator_port_id, 0, 8);
383 memcpy(req->priv.initiator_port_id + 8,
384 &target->srp_host->dev->dev->node_guid, 8);
387 status = ib_send_cm_req(target->cm_id, &req->param);
394 static void srp_disconnect_target(struct srp_target_port *target)
396 /* XXX should send SRP_I_LOGOUT request */
398 init_completion(&target->done);
399 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
400 printk(KERN_DEBUG PFX "Sending CM DREQ failed\n");
403 wait_for_completion(&target->done);
406 static void srp_remove_work(struct work_struct *work)
408 struct srp_target_port *target =
409 container_of(work, struct srp_target_port, work);
411 spin_lock_irq(target->scsi_host->host_lock);
412 if (target->state != SRP_TARGET_DEAD) {
413 spin_unlock_irq(target->scsi_host->host_lock);
416 target->state = SRP_TARGET_REMOVED;
417 spin_unlock_irq(target->scsi_host->host_lock);
419 spin_lock(&target->srp_host->target_lock);
420 list_del(&target->list);
421 spin_unlock(&target->srp_host->target_lock);
423 scsi_remove_host(target->scsi_host);
424 ib_destroy_cm_id(target->cm_id);
425 srp_free_target_ib(target);
426 scsi_host_put(target->scsi_host);
429 static int srp_connect_target(struct srp_target_port *target)
433 ret = srp_lookup_path(target);
438 init_completion(&target->done);
439 ret = srp_send_req(target);
442 wait_for_completion(&target->done);
445 * The CM event handling code will set status to
446 * SRP_PORT_REDIRECT if we get a port redirect REJ
447 * back, or SRP_DLID_REDIRECT if we get a lid/qp
450 switch (target->status) {
454 case SRP_PORT_REDIRECT:
455 ret = srp_lookup_path(target);
460 case SRP_DLID_REDIRECT:
464 return target->status;
469 static void srp_unmap_data(struct scsi_cmnd *scmnd,
470 struct srp_target_port *target,
471 struct srp_request *req)
473 if (!scsi_sglist(scmnd) ||
474 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
475 scmnd->sc_data_direction != DMA_FROM_DEVICE))
479 ib_fmr_pool_unmap(req->fmr);
483 ib_dma_unmap_sg(target->srp_host->dev->dev, scsi_sglist(scmnd),
484 scsi_sg_count(scmnd), scmnd->sc_data_direction);
487 static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
489 srp_unmap_data(req->scmnd, target, req);
490 list_move_tail(&req->list, &target->free_reqs);
493 static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
495 req->scmnd->result = DID_RESET << 16;
496 req->scmnd->scsi_done(req->scmnd);
497 srp_remove_req(target, req);
500 static int srp_reconnect_target(struct srp_target_port *target)
502 struct ib_cm_id *new_cm_id;
503 struct ib_qp_attr qp_attr;
504 struct srp_request *req, *tmp;
508 spin_lock_irq(target->scsi_host->host_lock);
509 if (target->state != SRP_TARGET_LIVE) {
510 spin_unlock_irq(target->scsi_host->host_lock);
513 target->state = SRP_TARGET_CONNECTING;
514 spin_unlock_irq(target->scsi_host->host_lock);
516 srp_disconnect_target(target);
518 * Now get a new local CM ID so that we avoid confusing the
519 * target in case things are really fouled up.
521 new_cm_id = ib_create_cm_id(target->srp_host->dev->dev,
522 srp_cm_handler, target);
523 if (IS_ERR(new_cm_id)) {
524 ret = PTR_ERR(new_cm_id);
527 ib_destroy_cm_id(target->cm_id);
528 target->cm_id = new_cm_id;
530 qp_attr.qp_state = IB_QPS_RESET;
531 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
535 ret = srp_init_qp(target, target->qp);
539 while (ib_poll_cq(target->cq, 1, &wc) > 0)
542 spin_lock_irq(target->scsi_host->host_lock);
543 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
544 srp_reset_req(target, req);
545 spin_unlock_irq(target->scsi_host->host_lock);
551 target->qp_in_error = 0;
552 ret = srp_connect_target(target);
556 spin_lock_irq(target->scsi_host->host_lock);
557 if (target->state == SRP_TARGET_CONNECTING) {
559 target->state = SRP_TARGET_LIVE;
562 spin_unlock_irq(target->scsi_host->host_lock);
567 printk(KERN_ERR PFX "reconnect failed (%d), removing target port.\n", ret);
570 * We couldn't reconnect, so kill our target port off.
571 * However, we have to defer the real removal because we might
572 * be in the context of the SCSI error handler now, which
573 * would deadlock if we call scsi_remove_host().
575 spin_lock_irq(target->scsi_host->host_lock);
576 if (target->state == SRP_TARGET_CONNECTING) {
577 target->state = SRP_TARGET_DEAD;
578 INIT_WORK(&target->work, srp_remove_work);
579 schedule_work(&target->work);
581 spin_unlock_irq(target->scsi_host->host_lock);
586 static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat,
587 int sg_cnt, struct srp_request *req,
588 struct srp_direct_buf *buf)
596 struct srp_device *dev = target->srp_host->dev;
597 struct ib_device *ibdev = dev->dev;
598 struct scatterlist *sg;
603 if (srp_target_is_mellanox(target) &&
604 (ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask))
608 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
609 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
611 if (ib_sg_dma_address(ibdev, sg) & ~dev->fmr_page_mask) {
617 if ((ib_sg_dma_address(ibdev, sg) + dma_len) &
618 ~dev->fmr_page_mask) {
628 page_cnt += len >> dev->fmr_page_shift;
629 if (page_cnt > SRP_FMR_SIZE)
632 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
637 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
638 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
640 for (j = 0; j < dma_len; j += dev->fmr_page_size)
641 dma_pages[page_cnt++] =
642 (ib_sg_dma_address(ibdev, sg) &
643 dev->fmr_page_mask) + j;
646 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
647 dma_pages, page_cnt, io_addr);
648 if (IS_ERR(req->fmr)) {
649 ret = PTR_ERR(req->fmr);
654 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) &
655 ~dev->fmr_page_mask);
656 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
657 buf->len = cpu_to_be32(len);
667 static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
668 struct srp_request *req)
670 struct scatterlist *scat;
671 struct srp_cmd *cmd = req->cmd->buf;
672 int len, nents, count;
673 u8 fmt = SRP_DATA_DESC_DIRECT;
674 struct srp_device *dev;
675 struct ib_device *ibdev;
677 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
678 return sizeof (struct srp_cmd);
680 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
681 scmnd->sc_data_direction != DMA_TO_DEVICE) {
682 printk(KERN_WARNING PFX "Unhandled data direction %d\n",
683 scmnd->sc_data_direction);
687 nents = scsi_sg_count(scmnd);
688 scat = scsi_sglist(scmnd);
690 dev = target->srp_host->dev;
693 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
695 fmt = SRP_DATA_DESC_DIRECT;
696 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
700 * The midlayer only generated a single gather/scatter
701 * entry, or DMA mapping coalesced everything to a
702 * single entry. So a direct descriptor along with
703 * the DMA MR suffices.
705 struct srp_direct_buf *buf = (void *) cmd->add_data;
707 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
708 buf->key = cpu_to_be32(dev->mr->rkey);
709 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
710 } else if (srp_map_fmr(target, scat, count, req,
711 (void *) cmd->add_data)) {
713 * FMR mapping failed, and the scatterlist has more
714 * than one entry. Generate an indirect memory
717 struct srp_indirect_buf *buf = (void *) cmd->add_data;
718 struct scatterlist *sg;
722 fmt = SRP_DATA_DESC_INDIRECT;
723 len = sizeof (struct srp_cmd) +
724 sizeof (struct srp_indirect_buf) +
725 count * sizeof (struct srp_direct_buf);
727 scsi_for_each_sg(scmnd, sg, count, i) {
728 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
730 buf->desc_list[i].va =
731 cpu_to_be64(ib_sg_dma_address(ibdev, sg));
732 buf->desc_list[i].key =
733 cpu_to_be32(dev->mr->rkey);
734 buf->desc_list[i].len = cpu_to_be32(dma_len);
738 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
739 cmd->data_out_desc_cnt = count;
741 cmd->data_in_desc_cnt = count;
744 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
745 buf->table_desc.key =
746 cpu_to_be32(target->srp_host->dev->mr->rkey);
747 buf->table_desc.len =
748 cpu_to_be32(count * sizeof (struct srp_direct_buf));
750 buf->len = cpu_to_be32(datalen);
753 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
754 cmd->buf_fmt = fmt << 4;
761 static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
763 struct srp_request *req;
764 struct scsi_cmnd *scmnd;
768 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
770 spin_lock_irqsave(target->scsi_host->host_lock, flags);
772 target->req_lim += delta;
774 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
776 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
777 if (be32_to_cpu(rsp->resp_data_len) < 4)
778 req->tsk_status = -1;
780 req->tsk_status = rsp->data[3];
781 complete(&req->done);
785 printk(KERN_ERR "Null scmnd for RSP w/tag %016llx\n",
786 (unsigned long long) rsp->tag);
787 scmnd->result = rsp->status;
789 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
790 memcpy(scmnd->sense_buffer, rsp->data +
791 be32_to_cpu(rsp->resp_data_len),
792 min_t(int, be32_to_cpu(rsp->sense_data_len),
793 SCSI_SENSE_BUFFERSIZE));
796 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
797 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
798 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
799 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
801 if (!req->tsk_mgmt) {
802 scmnd->host_scribble = (void *) -1L;
803 scmnd->scsi_done(scmnd);
805 srp_remove_req(target, req);
810 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
813 static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
815 struct ib_device *dev;
819 iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
821 dev = target->srp_host->dev->dev;
822 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
825 opcode = *(u8 *) iu->buf;
830 printk(KERN_ERR PFX "recv completion, opcode 0x%02x\n", opcode);
832 for (i = 0; i < wc->byte_len; ++i) {
834 printk(KERN_ERR " [%02x] ", i);
835 printk(" %02x", ((u8 *) iu->buf)[i]);
836 if ((i + 1) % 8 == 0)
840 if (wc->byte_len % 8)
846 srp_process_rsp(target, iu->buf);
850 /* XXX Handle target logout */
851 printk(KERN_WARNING PFX "Got target logout request\n");
855 printk(KERN_WARNING PFX "Unhandled SRP opcode 0x%02x\n", opcode);
859 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
863 static void srp_completion(struct ib_cq *cq, void *target_ptr)
865 struct srp_target_port *target = target_ptr;
868 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
869 while (ib_poll_cq(cq, 1, &wc) > 0) {
871 printk(KERN_ERR PFX "failed %s status %d\n",
872 wc.wr_id & SRP_OP_RECV ? "receive" : "send",
874 target->qp_in_error = 1;
878 if (wc.wr_id & SRP_OP_RECV)
879 srp_handle_recv(target, &wc);
885 static int __srp_post_recv(struct srp_target_port *target)
889 struct ib_recv_wr wr, *bad_wr;
893 next = target->rx_head & (SRP_RQ_SIZE - 1);
894 wr.wr_id = next | SRP_OP_RECV;
895 iu = target->rx_ring[next];
898 list.length = iu->size;
899 list.lkey = target->srp_host->dev->mr->lkey;
905 ret = ib_post_recv(target->qp, &wr, &bad_wr);
912 static int srp_post_recv(struct srp_target_port *target)
917 spin_lock_irqsave(target->scsi_host->host_lock, flags);
918 ret = __srp_post_recv(target);
919 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
925 * Must be called with target->scsi_host->host_lock held to protect
926 * req_lim and tx_head. Lock cannot be dropped between call here and
927 * call to __srp_post_send().
929 static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target)
931 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
934 if (unlikely(target->req_lim < 1))
935 ++target->zero_req_lim;
937 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
941 * Must be called with target->scsi_host->host_lock held to protect
942 * req_lim and tx_head.
944 static int __srp_post_send(struct srp_target_port *target,
945 struct srp_iu *iu, int len)
948 struct ib_send_wr wr, *bad_wr;
953 list.lkey = target->srp_host->dev->mr->lkey;
956 wr.wr_id = target->tx_head & SRP_SQ_SIZE;
959 wr.opcode = IB_WR_SEND;
960 wr.send_flags = IB_SEND_SIGNALED;
962 ret = ib_post_send(target->qp, &wr, &bad_wr);
972 static int srp_queuecommand(struct scsi_cmnd *scmnd,
973 void (*done)(struct scsi_cmnd *))
975 struct srp_target_port *target = host_to_target(scmnd->device->host);
976 struct srp_request *req;
979 struct ib_device *dev;
982 if (target->state == SRP_TARGET_CONNECTING)
985 if (target->state == SRP_TARGET_DEAD ||
986 target->state == SRP_TARGET_REMOVED) {
987 scmnd->result = DID_BAD_TARGET << 16;
992 iu = __srp_get_tx_iu(target);
996 dev = target->srp_host->dev->dev;
997 ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len,
1000 req = list_entry(target->free_reqs.next, struct srp_request, list);
1002 scmnd->scsi_done = done;
1004 scmnd->host_scribble = (void *) (long) req->index;
1007 memset(cmd, 0, sizeof *cmd);
1009 cmd->opcode = SRP_CMD;
1010 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
1011 cmd->tag = req->index;
1012 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1017 req->tsk_mgmt = NULL;
1019 len = srp_map_data(scmnd, target, req);
1021 printk(KERN_ERR PFX "Failed to map data\n");
1025 if (__srp_post_recv(target)) {
1026 printk(KERN_ERR PFX "Recv failed\n");
1030 ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len,
1033 if (__srp_post_send(target, iu, len)) {
1034 printk(KERN_ERR PFX "Send failed\n");
1038 list_move_tail(&req->list, &target->req_queue);
1043 srp_unmap_data(scmnd, target, req);
1046 return SCSI_MLQUEUE_HOST_BUSY;
1049 static int srp_alloc_iu_bufs(struct srp_target_port *target)
1053 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1054 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1055 target->max_ti_iu_len,
1056 GFP_KERNEL, DMA_FROM_DEVICE);
1057 if (!target->rx_ring[i])
1061 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1062 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
1064 GFP_KERNEL, DMA_TO_DEVICE);
1065 if (!target->tx_ring[i])
1072 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1073 srp_free_iu(target->srp_host, target->rx_ring[i]);
1074 target->rx_ring[i] = NULL;
1077 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1078 srp_free_iu(target->srp_host, target->tx_ring[i]);
1079 target->tx_ring[i] = NULL;
1085 static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1086 struct ib_cm_event *event,
1087 struct srp_target_port *target)
1089 struct ib_class_port_info *cpi;
1092 switch (event->param.rej_rcvd.reason) {
1093 case IB_CM_REJ_PORT_CM_REDIRECT:
1094 cpi = event->param.rej_rcvd.ari;
1095 target->path.dlid = cpi->redirect_lid;
1096 target->path.pkey = cpi->redirect_pkey;
1097 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1098 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1100 target->status = target->path.dlid ?
1101 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1104 case IB_CM_REJ_PORT_REDIRECT:
1105 if (srp_target_is_topspin(target)) {
1107 * Topspin/Cisco SRP gateways incorrectly send
1108 * reject reason code 25 when they mean 24
1111 memcpy(target->path.dgid.raw,
1112 event->param.rej_rcvd.ari, 16);
1114 printk(KERN_DEBUG PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1115 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1116 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1118 target->status = SRP_PORT_REDIRECT;
1120 printk(KERN_WARNING " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1121 target->status = -ECONNRESET;
1125 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
1126 printk(KERN_WARNING " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1127 target->status = -ECONNRESET;
1130 case IB_CM_REJ_CONSUMER_DEFINED:
1131 opcode = *(u8 *) event->private_data;
1132 if (opcode == SRP_LOGIN_REJ) {
1133 struct srp_login_rej *rej = event->private_data;
1134 u32 reason = be32_to_cpu(rej->reason);
1136 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
1137 printk(KERN_WARNING PFX
1138 "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1140 printk(KERN_WARNING PFX
1141 "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
1143 printk(KERN_WARNING " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1144 " opcode 0x%02x\n", opcode);
1145 target->status = -ECONNRESET;
1149 printk(KERN_WARNING " REJ reason 0x%x\n",
1150 event->param.rej_rcvd.reason);
1151 target->status = -ECONNRESET;
1155 static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1157 struct srp_target_port *target = cm_id->context;
1158 struct ib_qp_attr *qp_attr = NULL;
1163 switch (event->event) {
1164 case IB_CM_REQ_ERROR:
1165 printk(KERN_DEBUG PFX "Sending CM REQ failed\n");
1167 target->status = -ECONNRESET;
1170 case IB_CM_REP_RECEIVED:
1172 opcode = *(u8 *) event->private_data;
1174 if (opcode == SRP_LOGIN_RSP) {
1175 struct srp_login_rsp *rsp = event->private_data;
1177 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1178 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1180 target->scsi_host->can_queue = min(target->req_lim,
1181 target->scsi_host->can_queue);
1183 printk(KERN_WARNING PFX "Unhandled RSP opcode %#x\n", opcode);
1184 target->status = -ECONNRESET;
1188 if (!target->rx_ring[0]) {
1189 target->status = srp_alloc_iu_bufs(target);
1194 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1196 target->status = -ENOMEM;
1200 qp_attr->qp_state = IB_QPS_RTR;
1201 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1205 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1209 target->status = srp_post_recv(target);
1213 qp_attr->qp_state = IB_QPS_RTS;
1214 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1218 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1222 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1228 case IB_CM_REJ_RECEIVED:
1229 printk(KERN_DEBUG PFX "REJ received\n");
1232 srp_cm_rej_handler(cm_id, event, target);
1235 case IB_CM_DREQ_RECEIVED:
1236 printk(KERN_WARNING PFX "DREQ received - connection closed\n");
1237 if (ib_send_cm_drep(cm_id, NULL, 0))
1238 printk(KERN_ERR PFX "Sending CM DREP failed\n");
1241 case IB_CM_TIMEWAIT_EXIT:
1242 printk(KERN_ERR PFX "connection closed\n");
1248 case IB_CM_MRA_RECEIVED:
1249 case IB_CM_DREQ_ERROR:
1250 case IB_CM_DREP_RECEIVED:
1254 printk(KERN_WARNING PFX "Unhandled CM event %d\n", event->event);
1259 complete(&target->done);
1266 static int srp_send_tsk_mgmt(struct srp_target_port *target,
1267 struct srp_request *req, u8 func)
1270 struct srp_tsk_mgmt *tsk_mgmt;
1272 spin_lock_irq(target->scsi_host->host_lock);
1274 if (target->state == SRP_TARGET_DEAD ||
1275 target->state == SRP_TARGET_REMOVED) {
1276 req->scmnd->result = DID_BAD_TARGET << 16;
1280 init_completion(&req->done);
1282 iu = __srp_get_tx_iu(target);
1287 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1289 tsk_mgmt->opcode = SRP_TSK_MGMT;
1290 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1291 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
1292 tsk_mgmt->tsk_mgmt_func = func;
1293 tsk_mgmt->task_tag = req->index;
1295 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1300 spin_unlock_irq(target->scsi_host->host_lock);
1302 if (!wait_for_completion_timeout(&req->done,
1303 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
1309 spin_unlock_irq(target->scsi_host->host_lock);
1313 static int srp_find_req(struct srp_target_port *target,
1314 struct scsi_cmnd *scmnd,
1315 struct srp_request **req)
1317 if (scmnd->host_scribble == (void *) -1L)
1320 *req = &target->req_ring[(long) scmnd->host_scribble];
1325 static int srp_abort(struct scsi_cmnd *scmnd)
1327 struct srp_target_port *target = host_to_target(scmnd->device->host);
1328 struct srp_request *req;
1331 printk(KERN_ERR "SRP abort called\n");
1333 if (target->qp_in_error)
1335 if (srp_find_req(target, scmnd, &req))
1337 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1340 spin_lock_irq(target->scsi_host->host_lock);
1342 if (req->cmd_done) {
1343 srp_remove_req(target, req);
1344 scmnd->scsi_done(scmnd);
1345 } else if (!req->tsk_status) {
1346 srp_remove_req(target, req);
1347 scmnd->result = DID_ABORT << 16;
1351 spin_unlock_irq(target->scsi_host->host_lock);
1356 static int srp_reset_device(struct scsi_cmnd *scmnd)
1358 struct srp_target_port *target = host_to_target(scmnd->device->host);
1359 struct srp_request *req, *tmp;
1361 printk(KERN_ERR "SRP reset_device called\n");
1363 if (target->qp_in_error)
1365 if (srp_find_req(target, scmnd, &req))
1367 if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1369 if (req->tsk_status)
1372 spin_lock_irq(target->scsi_host->host_lock);
1374 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
1375 if (req->scmnd->device == scmnd->device)
1376 srp_reset_req(target, req);
1378 spin_unlock_irq(target->scsi_host->host_lock);
1383 static int srp_reset_host(struct scsi_cmnd *scmnd)
1385 struct srp_target_port *target = host_to_target(scmnd->device->host);
1388 printk(KERN_ERR PFX "SRP reset_host called\n");
1390 if (!srp_reconnect_target(target))
1396 static ssize_t show_id_ext(struct class_device *cdev, char *buf)
1398 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1400 if (target->state == SRP_TARGET_DEAD ||
1401 target->state == SRP_TARGET_REMOVED)
1404 return sprintf(buf, "0x%016llx\n",
1405 (unsigned long long) be64_to_cpu(target->id_ext));
1408 static ssize_t show_ioc_guid(struct class_device *cdev, char *buf)
1410 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1412 if (target->state == SRP_TARGET_DEAD ||
1413 target->state == SRP_TARGET_REMOVED)
1416 return sprintf(buf, "0x%016llx\n",
1417 (unsigned long long) be64_to_cpu(target->ioc_guid));
1420 static ssize_t show_service_id(struct class_device *cdev, char *buf)
1422 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1424 if (target->state == SRP_TARGET_DEAD ||
1425 target->state == SRP_TARGET_REMOVED)
1428 return sprintf(buf, "0x%016llx\n",
1429 (unsigned long long) be64_to_cpu(target->service_id));
1432 static ssize_t show_pkey(struct class_device *cdev, char *buf)
1434 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1436 if (target->state == SRP_TARGET_DEAD ||
1437 target->state == SRP_TARGET_REMOVED)
1440 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1443 static ssize_t show_dgid(struct class_device *cdev, char *buf)
1445 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1447 if (target->state == SRP_TARGET_DEAD ||
1448 target->state == SRP_TARGET_REMOVED)
1451 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1452 be16_to_cpu(((__be16 *) target->path.dgid.raw)[0]),
1453 be16_to_cpu(((__be16 *) target->path.dgid.raw)[1]),
1454 be16_to_cpu(((__be16 *) target->path.dgid.raw)[2]),
1455 be16_to_cpu(((__be16 *) target->path.dgid.raw)[3]),
1456 be16_to_cpu(((__be16 *) target->path.dgid.raw)[4]),
1457 be16_to_cpu(((__be16 *) target->path.dgid.raw)[5]),
1458 be16_to_cpu(((__be16 *) target->path.dgid.raw)[6]),
1459 be16_to_cpu(((__be16 *) target->path.dgid.raw)[7]));
1462 static ssize_t show_orig_dgid(struct class_device *cdev, char *buf)
1464 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1466 if (target->state == SRP_TARGET_DEAD ||
1467 target->state == SRP_TARGET_REMOVED)
1470 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1471 be16_to_cpu(target->orig_dgid[0]),
1472 be16_to_cpu(target->orig_dgid[1]),
1473 be16_to_cpu(target->orig_dgid[2]),
1474 be16_to_cpu(target->orig_dgid[3]),
1475 be16_to_cpu(target->orig_dgid[4]),
1476 be16_to_cpu(target->orig_dgid[5]),
1477 be16_to_cpu(target->orig_dgid[6]),
1478 be16_to_cpu(target->orig_dgid[7]));
1481 static ssize_t show_zero_req_lim(struct class_device *cdev, char *buf)
1483 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1485 if (target->state == SRP_TARGET_DEAD ||
1486 target->state == SRP_TARGET_REMOVED)
1489 return sprintf(buf, "%d\n", target->zero_req_lim);
1492 static ssize_t show_local_ib_port(struct class_device *cdev, char *buf)
1494 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1496 return sprintf(buf, "%d\n", target->srp_host->port);
1499 static ssize_t show_local_ib_device(struct class_device *cdev, char *buf)
1501 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1503 return sprintf(buf, "%s\n", target->srp_host->dev->dev->name);
1506 static CLASS_DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1507 static CLASS_DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1508 static CLASS_DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1509 static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1510 static CLASS_DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1511 static CLASS_DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
1512 static CLASS_DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1513 static CLASS_DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1514 static CLASS_DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
1516 static struct class_device_attribute *srp_host_attrs[] = {
1517 &class_device_attr_id_ext,
1518 &class_device_attr_ioc_guid,
1519 &class_device_attr_service_id,
1520 &class_device_attr_pkey,
1521 &class_device_attr_dgid,
1522 &class_device_attr_orig_dgid,
1523 &class_device_attr_zero_req_lim,
1524 &class_device_attr_local_ib_port,
1525 &class_device_attr_local_ib_device,
1529 static struct scsi_host_template srp_template = {
1530 .module = THIS_MODULE,
1531 .name = "InfiniBand SRP initiator",
1532 .proc_name = DRV_NAME,
1533 .info = srp_target_info,
1534 .queuecommand = srp_queuecommand,
1535 .eh_abort_handler = srp_abort,
1536 .eh_device_reset_handler = srp_reset_device,
1537 .eh_host_reset_handler = srp_reset_host,
1538 .can_queue = SRP_SQ_SIZE,
1540 .cmd_per_lun = SRP_SQ_SIZE,
1541 .use_clustering = ENABLE_CLUSTERING,
1542 .shost_attrs = srp_host_attrs
1545 static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1547 sprintf(target->target_name, "SRP.T10:%016llX",
1548 (unsigned long long) be64_to_cpu(target->id_ext));
1550 if (scsi_add_host(target->scsi_host, host->dev->dev->dma_device))
1553 spin_lock(&host->target_lock);
1554 list_add_tail(&target->list, &host->target_list);
1555 spin_unlock(&host->target_lock);
1557 target->state = SRP_TARGET_LIVE;
1559 scsi_scan_target(&target->scsi_host->shost_gendev,
1560 0, target->scsi_id, SCAN_WILD_CARD, 0);
1565 static void srp_release_class_dev(struct class_device *class_dev)
1567 struct srp_host *host =
1568 container_of(class_dev, struct srp_host, class_dev);
1570 complete(&host->released);
1573 static struct class srp_class = {
1574 .name = "infiniband_srp",
1575 .release = srp_release_class_dev
1579 * Target ports are added by writing
1581 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1582 * pkey=<P_Key>,service_id=<service ID>
1584 * to the add_target sysfs attribute.
1588 SRP_OPT_ID_EXT = 1 << 0,
1589 SRP_OPT_IOC_GUID = 1 << 1,
1590 SRP_OPT_DGID = 1 << 2,
1591 SRP_OPT_PKEY = 1 << 3,
1592 SRP_OPT_SERVICE_ID = 1 << 4,
1593 SRP_OPT_MAX_SECT = 1 << 5,
1594 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
1595 SRP_OPT_IO_CLASS = 1 << 7,
1596 SRP_OPT_INITIATOR_EXT = 1 << 8,
1597 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1601 SRP_OPT_SERVICE_ID),
1604 static match_table_t srp_opt_tokens = {
1605 { SRP_OPT_ID_EXT, "id_ext=%s" },
1606 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1607 { SRP_OPT_DGID, "dgid=%s" },
1608 { SRP_OPT_PKEY, "pkey=%x" },
1609 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1610 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1611 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
1612 { SRP_OPT_IO_CLASS, "io_class=%x" },
1613 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
1614 { SRP_OPT_ERR, NULL }
1617 static int srp_parse_options(const char *buf, struct srp_target_port *target)
1619 char *options, *sep_opt;
1622 substring_t args[MAX_OPT_ARGS];
1628 options = kstrdup(buf, GFP_KERNEL);
1633 while ((p = strsep(&sep_opt, ",")) != NULL) {
1637 token = match_token(p, srp_opt_tokens, args);
1641 case SRP_OPT_ID_EXT:
1642 p = match_strdup(args);
1647 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1651 case SRP_OPT_IOC_GUID:
1652 p = match_strdup(args);
1657 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1662 p = match_strdup(args);
1667 if (strlen(p) != 32) {
1668 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
1673 for (i = 0; i < 16; ++i) {
1674 strlcpy(dgid, p + i * 2, 3);
1675 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1678 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
1682 if (match_hex(args, &token)) {
1683 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1686 target->path.pkey = cpu_to_be16(token);
1689 case SRP_OPT_SERVICE_ID:
1690 p = match_strdup(args);
1695 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
1696 target->path.service_id = target->service_id;
1700 case SRP_OPT_MAX_SECT:
1701 if (match_int(args, &token)) {
1702 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1705 target->scsi_host->max_sectors = token;
1708 case SRP_OPT_MAX_CMD_PER_LUN:
1709 if (match_int(args, &token)) {
1710 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1713 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1716 case SRP_OPT_IO_CLASS:
1717 if (match_hex(args, &token)) {
1718 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1721 if (token != SRP_REV10_IB_IO_CLASS &&
1722 token != SRP_REV16A_IB_IO_CLASS) {
1723 printk(KERN_WARNING PFX "unknown IO class parameter value"
1724 " %x specified (use %x or %x).\n",
1725 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1728 target->io_class = token;
1731 case SRP_OPT_INITIATOR_EXT:
1732 p = match_strdup(args);
1737 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1742 printk(KERN_WARNING PFX "unknown parameter or missing value "
1743 "'%s' in target creation request\n", p);
1748 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1751 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1752 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1753 !(srp_opt_tokens[i].token & opt_mask))
1754 printk(KERN_WARNING PFX "target creation request is "
1755 "missing parameter '%s'\n",
1756 srp_opt_tokens[i].pattern);
1763 static ssize_t srp_create_target(struct class_device *class_dev,
1764 const char *buf, size_t count)
1766 struct srp_host *host =
1767 container_of(class_dev, struct srp_host, class_dev);
1768 struct Scsi_Host *target_host;
1769 struct srp_target_port *target;
1773 target_host = scsi_host_alloc(&srp_template,
1774 sizeof (struct srp_target_port));
1778 target_host->max_lun = SRP_MAX_LUN;
1779 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
1781 target = host_to_target(target_host);
1783 target->io_class = SRP_REV16A_IB_IO_CLASS;
1784 target->scsi_host = target_host;
1785 target->srp_host = host;
1787 INIT_LIST_HEAD(&target->free_reqs);
1788 INIT_LIST_HEAD(&target->req_queue);
1789 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1790 target->req_ring[i].index = i;
1791 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1794 ret = srp_parse_options(buf, target);
1798 ib_get_cached_gid(host->dev->dev, host->port, 0, &target->path.sgid);
1800 printk(KERN_DEBUG PFX "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1801 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1802 (unsigned long long) be64_to_cpu(target->id_ext),
1803 (unsigned long long) be64_to_cpu(target->ioc_guid),
1804 be16_to_cpu(target->path.pkey),
1805 (unsigned long long) be64_to_cpu(target->service_id),
1806 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]),
1807 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]),
1808 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]),
1809 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]),
1810 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]),
1811 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]),
1812 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]),
1813 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14]));
1815 ret = srp_create_target_ib(target);
1819 target->cm_id = ib_create_cm_id(host->dev->dev, srp_cm_handler, target);
1820 if (IS_ERR(target->cm_id)) {
1821 ret = PTR_ERR(target->cm_id);
1825 target->qp_in_error = 0;
1826 ret = srp_connect_target(target);
1828 printk(KERN_ERR PFX "Connection failed\n");
1832 ret = srp_add_target(host, target);
1834 goto err_disconnect;
1839 srp_disconnect_target(target);
1842 ib_destroy_cm_id(target->cm_id);
1845 srp_free_target_ib(target);
1848 scsi_host_put(target_host);
1853 static CLASS_DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
1855 static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
1857 struct srp_host *host =
1858 container_of(class_dev, struct srp_host, class_dev);
1860 return sprintf(buf, "%s\n", host->dev->dev->name);
1863 static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1865 static ssize_t show_port(struct class_device *class_dev, char *buf)
1867 struct srp_host *host =
1868 container_of(class_dev, struct srp_host, class_dev);
1870 return sprintf(buf, "%d\n", host->port);
1873 static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
1875 static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
1877 struct srp_host *host;
1879 host = kzalloc(sizeof *host, GFP_KERNEL);
1883 INIT_LIST_HEAD(&host->target_list);
1884 spin_lock_init(&host->target_lock);
1885 init_completion(&host->released);
1889 host->class_dev.class = &srp_class;
1890 host->class_dev.dev = device->dev->dma_device;
1891 snprintf(host->class_dev.class_id, BUS_ID_SIZE, "srp-%s-%d",
1892 device->dev->name, port);
1894 if (class_device_register(&host->class_dev))
1896 if (class_device_create_file(&host->class_dev, &class_device_attr_add_target))
1898 if (class_device_create_file(&host->class_dev, &class_device_attr_ibdev))
1900 if (class_device_create_file(&host->class_dev, &class_device_attr_port))
1906 class_device_unregister(&host->class_dev);
1914 static void srp_add_one(struct ib_device *device)
1916 struct srp_device *srp_dev;
1917 struct ib_device_attr *dev_attr;
1918 struct ib_fmr_pool_param fmr_param;
1919 struct srp_host *host;
1922 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
1926 if (ib_query_device(device, dev_attr)) {
1927 printk(KERN_WARNING PFX "Query device failed for %s\n",
1932 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
1937 * Use the smallest page size supported by the HCA, down to a
1938 * minimum of 512 bytes (which is the smallest sector that a
1939 * SCSI command will ever carry).
1941 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
1942 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
1943 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
1945 INIT_LIST_HEAD(&srp_dev->dev_list);
1947 srp_dev->dev = device;
1948 srp_dev->pd = ib_alloc_pd(device);
1949 if (IS_ERR(srp_dev->pd))
1952 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
1953 IB_ACCESS_LOCAL_WRITE |
1954 IB_ACCESS_REMOTE_READ |
1955 IB_ACCESS_REMOTE_WRITE);
1956 if (IS_ERR(srp_dev->mr))
1959 memset(&fmr_param, 0, sizeof fmr_param);
1960 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
1961 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
1962 fmr_param.cache = 1;
1963 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
1964 fmr_param.page_shift = srp_dev->fmr_page_shift;
1965 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
1966 IB_ACCESS_REMOTE_WRITE |
1967 IB_ACCESS_REMOTE_READ);
1969 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
1970 if (IS_ERR(srp_dev->fmr_pool))
1971 srp_dev->fmr_pool = NULL;
1973 if (device->node_type == RDMA_NODE_IB_SWITCH) {
1978 e = device->phys_port_cnt;
1981 for (p = s; p <= e; ++p) {
1982 host = srp_add_port(srp_dev, p);
1984 list_add_tail(&host->list, &srp_dev->dev_list);
1987 ib_set_client_data(device, &srp_client, srp_dev);
1992 ib_dealloc_pd(srp_dev->pd);
2001 static void srp_remove_one(struct ib_device *device)
2003 struct srp_device *srp_dev;
2004 struct srp_host *host, *tmp_host;
2005 LIST_HEAD(target_list);
2006 struct srp_target_port *target, *tmp_target;
2008 srp_dev = ib_get_client_data(device, &srp_client);
2010 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
2011 class_device_unregister(&host->class_dev);
2013 * Wait for the sysfs entry to go away, so that no new
2014 * target ports can be created.
2016 wait_for_completion(&host->released);
2019 * Mark all target ports as removed, so we stop queueing
2020 * commands and don't try to reconnect.
2022 spin_lock(&host->target_lock);
2023 list_for_each_entry(target, &host->target_list, list) {
2024 spin_lock_irq(target->scsi_host->host_lock);
2025 target->state = SRP_TARGET_REMOVED;
2026 spin_unlock_irq(target->scsi_host->host_lock);
2028 spin_unlock(&host->target_lock);
2031 * Wait for any reconnection tasks that may have
2032 * started before we marked our target ports as
2033 * removed, and any target port removal tasks.
2035 flush_scheduled_work();
2037 list_for_each_entry_safe(target, tmp_target,
2038 &host->target_list, list) {
2039 scsi_remove_host(target->scsi_host);
2040 srp_disconnect_target(target);
2041 ib_destroy_cm_id(target->cm_id);
2042 srp_free_target_ib(target);
2043 scsi_host_put(target->scsi_host);
2049 if (srp_dev->fmr_pool)
2050 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2051 ib_dereg_mr(srp_dev->mr);
2052 ib_dealloc_pd(srp_dev->pd);
2057 static int __init srp_init_module(void)
2061 srp_template.sg_tablesize = srp_sg_tablesize;
2062 srp_max_iu_len = (sizeof (struct srp_cmd) +
2063 sizeof (struct srp_indirect_buf) +
2064 srp_sg_tablesize * 16);
2066 ret = class_register(&srp_class);
2068 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
2072 ib_sa_register_client(&srp_sa_client);
2074 ret = ib_register_client(&srp_client);
2076 printk(KERN_ERR PFX "couldn't register IB client\n");
2077 ib_sa_unregister_client(&srp_sa_client);
2078 class_unregister(&srp_class);
2085 static void __exit srp_cleanup_module(void)
2087 ib_unregister_client(&srp_client);
2088 ib_sa_unregister_client(&srp_sa_client);
2089 class_unregister(&srp_class);
2092 module_init(srp_init_module);
2093 module_exit(srp_cleanup_module);