IB/srp: Let srp_abort() return FAST_IO_FAIL if TL offline
[linux/fpc-iii.git] / drivers / infiniband / ulp / srp / ib_srp.c
blobf93baf8254c4d492065155b4b701529629260b30
1 /*
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
12 * conditions are met:
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
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
30 * SOFTWARE.
33 #define pr_fmt(fmt) PFX fmt
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/err.h>
39 #include <linux/string.h>
40 #include <linux/parser.h>
41 #include <linux/random.h>
42 #include <linux/jiffies.h>
44 #include <linux/atomic.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_device.h>
48 #include <scsi/scsi_dbg.h>
49 #include <scsi/srp.h>
50 #include <scsi/scsi_transport_srp.h>
52 #include "ib_srp.h"
54 #define DRV_NAME "ib_srp"
55 #define PFX DRV_NAME ": "
56 #define DRV_VERSION "1.0"
57 #define DRV_RELDATE "July 1, 2013"
59 MODULE_AUTHOR("Roland Dreier");
60 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
61 "v" DRV_VERSION " (" DRV_RELDATE ")");
62 MODULE_LICENSE("Dual BSD/GPL");
64 static unsigned int srp_sg_tablesize;
65 static unsigned int cmd_sg_entries;
66 static unsigned int indirect_sg_entries;
67 static bool allow_ext_sg;
68 static int topspin_workarounds = 1;
70 module_param(srp_sg_tablesize, uint, 0444);
71 MODULE_PARM_DESC(srp_sg_tablesize, "Deprecated name for cmd_sg_entries");
73 module_param(cmd_sg_entries, uint, 0444);
74 MODULE_PARM_DESC(cmd_sg_entries,
75 "Default number of gather/scatter entries in the SRP command (default is 12, max 255)");
77 module_param(indirect_sg_entries, uint, 0444);
78 MODULE_PARM_DESC(indirect_sg_entries,
79 "Default max number of gather/scatter entries (default is 12, max is " __stringify(SCSI_MAX_SG_CHAIN_SEGMENTS) ")");
81 module_param(allow_ext_sg, bool, 0444);
82 MODULE_PARM_DESC(allow_ext_sg,
83 "Default behavior when there are more than cmd_sg_entries S/G entries after mapping; fails the request when false (default false)");
85 module_param(topspin_workarounds, int, 0444);
86 MODULE_PARM_DESC(topspin_workarounds,
87 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
89 static void srp_add_one(struct ib_device *device);
90 static void srp_remove_one(struct ib_device *device);
91 static void srp_recv_completion(struct ib_cq *cq, void *target_ptr);
92 static void srp_send_completion(struct ib_cq *cq, void *target_ptr);
93 static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
95 static struct scsi_transport_template *ib_srp_transport_template;
97 static struct ib_client srp_client = {
98 .name = "srp",
99 .add = srp_add_one,
100 .remove = srp_remove_one
103 static struct ib_sa_client srp_sa_client;
105 static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
107 return (struct srp_target_port *) host->hostdata;
110 static const char *srp_target_info(struct Scsi_Host *host)
112 return host_to_target(host)->target_name;
115 static int srp_target_is_topspin(struct srp_target_port *target)
117 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
118 static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
120 return topspin_workarounds &&
121 (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
122 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
125 static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
126 gfp_t gfp_mask,
127 enum dma_data_direction direction)
129 struct srp_iu *iu;
131 iu = kmalloc(sizeof *iu, gfp_mask);
132 if (!iu)
133 goto out;
135 iu->buf = kzalloc(size, gfp_mask);
136 if (!iu->buf)
137 goto out_free_iu;
139 iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
140 direction);
141 if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
142 goto out_free_buf;
144 iu->size = size;
145 iu->direction = direction;
147 return iu;
149 out_free_buf:
150 kfree(iu->buf);
151 out_free_iu:
152 kfree(iu);
153 out:
154 return NULL;
157 static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
159 if (!iu)
160 return;
162 ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
163 iu->direction);
164 kfree(iu->buf);
165 kfree(iu);
168 static void srp_qp_event(struct ib_event *event, void *context)
170 pr_debug("QP event %d\n", event->event);
173 static int srp_init_qp(struct srp_target_port *target,
174 struct ib_qp *qp)
176 struct ib_qp_attr *attr;
177 int ret;
179 attr = kmalloc(sizeof *attr, GFP_KERNEL);
180 if (!attr)
181 return -ENOMEM;
183 ret = ib_find_pkey(target->srp_host->srp_dev->dev,
184 target->srp_host->port,
185 be16_to_cpu(target->path.pkey),
186 &attr->pkey_index);
187 if (ret)
188 goto out;
190 attr->qp_state = IB_QPS_INIT;
191 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
192 IB_ACCESS_REMOTE_WRITE);
193 attr->port_num = target->srp_host->port;
195 ret = ib_modify_qp(qp, attr,
196 IB_QP_STATE |
197 IB_QP_PKEY_INDEX |
198 IB_QP_ACCESS_FLAGS |
199 IB_QP_PORT);
201 out:
202 kfree(attr);
203 return ret;
206 static int srp_new_cm_id(struct srp_target_port *target)
208 struct ib_cm_id *new_cm_id;
210 new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
211 srp_cm_handler, target);
212 if (IS_ERR(new_cm_id))
213 return PTR_ERR(new_cm_id);
215 if (target->cm_id)
216 ib_destroy_cm_id(target->cm_id);
217 target->cm_id = new_cm_id;
219 return 0;
222 static int srp_create_target_ib(struct srp_target_port *target)
224 struct ib_qp_init_attr *init_attr;
225 struct ib_cq *recv_cq, *send_cq;
226 struct ib_qp *qp;
227 int ret;
229 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
230 if (!init_attr)
231 return -ENOMEM;
233 recv_cq = ib_create_cq(target->srp_host->srp_dev->dev,
234 srp_recv_completion, NULL, target, SRP_RQ_SIZE,
235 target->comp_vector);
236 if (IS_ERR(recv_cq)) {
237 ret = PTR_ERR(recv_cq);
238 goto err;
241 send_cq = ib_create_cq(target->srp_host->srp_dev->dev,
242 srp_send_completion, NULL, target, SRP_SQ_SIZE,
243 target->comp_vector);
244 if (IS_ERR(send_cq)) {
245 ret = PTR_ERR(send_cq);
246 goto err_recv_cq;
249 ib_req_notify_cq(recv_cq, IB_CQ_NEXT_COMP);
251 init_attr->event_handler = srp_qp_event;
252 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
253 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
254 init_attr->cap.max_recv_sge = 1;
255 init_attr->cap.max_send_sge = 1;
256 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
257 init_attr->qp_type = IB_QPT_RC;
258 init_attr->send_cq = send_cq;
259 init_attr->recv_cq = recv_cq;
261 qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr);
262 if (IS_ERR(qp)) {
263 ret = PTR_ERR(qp);
264 goto err_send_cq;
267 ret = srp_init_qp(target, qp);
268 if (ret)
269 goto err_qp;
271 if (target->qp)
272 ib_destroy_qp(target->qp);
273 if (target->recv_cq)
274 ib_destroy_cq(target->recv_cq);
275 if (target->send_cq)
276 ib_destroy_cq(target->send_cq);
278 target->qp = qp;
279 target->recv_cq = recv_cq;
280 target->send_cq = send_cq;
282 kfree(init_attr);
283 return 0;
285 err_qp:
286 ib_destroy_qp(qp);
288 err_send_cq:
289 ib_destroy_cq(send_cq);
291 err_recv_cq:
292 ib_destroy_cq(recv_cq);
294 err:
295 kfree(init_attr);
296 return ret;
299 static void srp_free_target_ib(struct srp_target_port *target)
301 int i;
303 ib_destroy_qp(target->qp);
304 ib_destroy_cq(target->send_cq);
305 ib_destroy_cq(target->recv_cq);
307 target->qp = NULL;
308 target->send_cq = target->recv_cq = NULL;
310 for (i = 0; i < SRP_RQ_SIZE; ++i)
311 srp_free_iu(target->srp_host, target->rx_ring[i]);
312 for (i = 0; i < SRP_SQ_SIZE; ++i)
313 srp_free_iu(target->srp_host, target->tx_ring[i]);
316 static void srp_path_rec_completion(int status,
317 struct ib_sa_path_rec *pathrec,
318 void *target_ptr)
320 struct srp_target_port *target = target_ptr;
322 target->status = status;
323 if (status)
324 shost_printk(KERN_ERR, target->scsi_host,
325 PFX "Got failed path rec status %d\n", status);
326 else
327 target->path = *pathrec;
328 complete(&target->done);
331 static int srp_lookup_path(struct srp_target_port *target)
333 target->path.numb_path = 1;
335 init_completion(&target->done);
337 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
338 target->srp_host->srp_dev->dev,
339 target->srp_host->port,
340 &target->path,
341 IB_SA_PATH_REC_SERVICE_ID |
342 IB_SA_PATH_REC_DGID |
343 IB_SA_PATH_REC_SGID |
344 IB_SA_PATH_REC_NUMB_PATH |
345 IB_SA_PATH_REC_PKEY,
346 SRP_PATH_REC_TIMEOUT_MS,
347 GFP_KERNEL,
348 srp_path_rec_completion,
349 target, &target->path_query);
350 if (target->path_query_id < 0)
351 return target->path_query_id;
353 wait_for_completion(&target->done);
355 if (target->status < 0)
356 shost_printk(KERN_WARNING, target->scsi_host,
357 PFX "Path record query failed\n");
359 return target->status;
362 static int srp_send_req(struct srp_target_port *target)
364 struct {
365 struct ib_cm_req_param param;
366 struct srp_login_req priv;
367 } *req = NULL;
368 int status;
370 req = kzalloc(sizeof *req, GFP_KERNEL);
371 if (!req)
372 return -ENOMEM;
374 req->param.primary_path = &target->path;
375 req->param.alternate_path = NULL;
376 req->param.service_id = target->service_id;
377 req->param.qp_num = target->qp->qp_num;
378 req->param.qp_type = target->qp->qp_type;
379 req->param.private_data = &req->priv;
380 req->param.private_data_len = sizeof req->priv;
381 req->param.flow_control = 1;
383 get_random_bytes(&req->param.starting_psn, 4);
384 req->param.starting_psn &= 0xffffff;
387 * Pick some arbitrary defaults here; we could make these
388 * module parameters if anyone cared about setting them.
390 req->param.responder_resources = 4;
391 req->param.remote_cm_response_timeout = 20;
392 req->param.local_cm_response_timeout = 20;
393 req->param.retry_count = 7;
394 req->param.rnr_retry_count = 7;
395 req->param.max_cm_retries = 15;
397 req->priv.opcode = SRP_LOGIN_REQ;
398 req->priv.tag = 0;
399 req->priv.req_it_iu_len = cpu_to_be32(target->max_iu_len);
400 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
401 SRP_BUF_FORMAT_INDIRECT);
403 * In the published SRP specification (draft rev. 16a), the
404 * port identifier format is 8 bytes of ID extension followed
405 * by 8 bytes of GUID. Older drafts put the two halves in the
406 * opposite order, so that the GUID comes first.
408 * Targets conforming to these obsolete drafts can be
409 * recognized by the I/O Class they report.
411 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
412 memcpy(req->priv.initiator_port_id,
413 &target->path.sgid.global.interface_id, 8);
414 memcpy(req->priv.initiator_port_id + 8,
415 &target->initiator_ext, 8);
416 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
417 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
418 } else {
419 memcpy(req->priv.initiator_port_id,
420 &target->initiator_ext, 8);
421 memcpy(req->priv.initiator_port_id + 8,
422 &target->path.sgid.global.interface_id, 8);
423 memcpy(req->priv.target_port_id, &target->id_ext, 8);
424 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
428 * Topspin/Cisco SRP targets will reject our login unless we
429 * zero out the first 8 bytes of our initiator port ID and set
430 * the second 8 bytes to the local node GUID.
432 if (srp_target_is_topspin(target)) {
433 shost_printk(KERN_DEBUG, target->scsi_host,
434 PFX "Topspin/Cisco initiator port ID workaround "
435 "activated for target GUID %016llx\n",
436 (unsigned long long) be64_to_cpu(target->ioc_guid));
437 memset(req->priv.initiator_port_id, 0, 8);
438 memcpy(req->priv.initiator_port_id + 8,
439 &target->srp_host->srp_dev->dev->node_guid, 8);
442 status = ib_send_cm_req(target->cm_id, &req->param);
444 kfree(req);
446 return status;
449 static bool srp_queue_remove_work(struct srp_target_port *target)
451 bool changed = false;
453 spin_lock_irq(&target->lock);
454 if (target->state != SRP_TARGET_REMOVED) {
455 target->state = SRP_TARGET_REMOVED;
456 changed = true;
458 spin_unlock_irq(&target->lock);
460 if (changed)
461 queue_work(system_long_wq, &target->remove_work);
463 return changed;
466 static bool srp_change_conn_state(struct srp_target_port *target,
467 bool connected)
469 bool changed = false;
471 spin_lock_irq(&target->lock);
472 if (target->connected != connected) {
473 target->connected = connected;
474 changed = true;
476 spin_unlock_irq(&target->lock);
478 return changed;
481 static void srp_disconnect_target(struct srp_target_port *target)
483 if (srp_change_conn_state(target, false)) {
484 /* XXX should send SRP_I_LOGOUT request */
486 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
487 shost_printk(KERN_DEBUG, target->scsi_host,
488 PFX "Sending CM DREQ failed\n");
493 static void srp_free_req_data(struct srp_target_port *target)
495 struct ib_device *ibdev = target->srp_host->srp_dev->dev;
496 struct srp_request *req;
497 int i;
499 for (i = 0, req = target->req_ring; i < SRP_CMD_SQ_SIZE; ++i, ++req) {
500 kfree(req->fmr_list);
501 kfree(req->map_page);
502 if (req->indirect_dma_addr) {
503 ib_dma_unmap_single(ibdev, req->indirect_dma_addr,
504 target->indirect_size,
505 DMA_TO_DEVICE);
507 kfree(req->indirect_desc);
512 * srp_del_scsi_host_attr() - Remove attributes defined in the host template.
513 * @shost: SCSI host whose attributes to remove from sysfs.
515 * Note: Any attributes defined in the host template and that did not exist
516 * before invocation of this function will be ignored.
518 static void srp_del_scsi_host_attr(struct Scsi_Host *shost)
520 struct device_attribute **attr;
522 for (attr = shost->hostt->shost_attrs; attr && *attr; ++attr)
523 device_remove_file(&shost->shost_dev, *attr);
526 static void srp_remove_target(struct srp_target_port *target)
528 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
530 srp_del_scsi_host_attr(target->scsi_host);
531 srp_remove_host(target->scsi_host);
532 scsi_remove_host(target->scsi_host);
533 srp_disconnect_target(target);
534 ib_destroy_cm_id(target->cm_id);
535 srp_free_target_ib(target);
536 srp_free_req_data(target);
537 scsi_host_put(target->scsi_host);
540 static void srp_remove_work(struct work_struct *work)
542 struct srp_target_port *target =
543 container_of(work, struct srp_target_port, remove_work);
545 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
547 srp_remove_target(target);
549 spin_lock(&target->srp_host->target_lock);
550 list_del(&target->list);
551 spin_unlock(&target->srp_host->target_lock);
554 static void srp_rport_delete(struct srp_rport *rport)
556 struct srp_target_port *target = rport->lld_data;
558 srp_queue_remove_work(target);
561 static int srp_connect_target(struct srp_target_port *target)
563 int retries = 3;
564 int ret;
566 WARN_ON_ONCE(target->connected);
568 target->qp_in_error = false;
570 ret = srp_lookup_path(target);
571 if (ret)
572 return ret;
574 while (1) {
575 init_completion(&target->done);
576 ret = srp_send_req(target);
577 if (ret)
578 return ret;
579 wait_for_completion(&target->done);
582 * The CM event handling code will set status to
583 * SRP_PORT_REDIRECT if we get a port redirect REJ
584 * back, or SRP_DLID_REDIRECT if we get a lid/qp
585 * redirect REJ back.
587 switch (target->status) {
588 case 0:
589 srp_change_conn_state(target, true);
590 return 0;
592 case SRP_PORT_REDIRECT:
593 ret = srp_lookup_path(target);
594 if (ret)
595 return ret;
596 break;
598 case SRP_DLID_REDIRECT:
599 break;
601 case SRP_STALE_CONN:
602 /* Our current CM id was stale, and is now in timewait.
603 * Try to reconnect with a new one.
605 if (!retries-- || srp_new_cm_id(target)) {
606 shost_printk(KERN_ERR, target->scsi_host, PFX
607 "giving up on stale connection\n");
608 target->status = -ECONNRESET;
609 return target->status;
612 shost_printk(KERN_ERR, target->scsi_host, PFX
613 "retrying stale connection\n");
614 break;
616 default:
617 return target->status;
622 static void srp_unmap_data(struct scsi_cmnd *scmnd,
623 struct srp_target_port *target,
624 struct srp_request *req)
626 struct ib_device *ibdev = target->srp_host->srp_dev->dev;
627 struct ib_pool_fmr **pfmr;
629 if (!scsi_sglist(scmnd) ||
630 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
631 scmnd->sc_data_direction != DMA_FROM_DEVICE))
632 return;
634 pfmr = req->fmr_list;
635 while (req->nfmr--)
636 ib_fmr_pool_unmap(*pfmr++);
638 ib_dma_unmap_sg(ibdev, scsi_sglist(scmnd), scsi_sg_count(scmnd),
639 scmnd->sc_data_direction);
643 * srp_claim_req - Take ownership of the scmnd associated with a request.
644 * @target: SRP target port.
645 * @req: SRP request.
646 * @scmnd: If NULL, take ownership of @req->scmnd. If not NULL, only take
647 * ownership of @req->scmnd if it equals @scmnd.
649 * Return value:
650 * Either NULL or a pointer to the SCSI command the caller became owner of.
652 static struct scsi_cmnd *srp_claim_req(struct srp_target_port *target,
653 struct srp_request *req,
654 struct scsi_cmnd *scmnd)
656 unsigned long flags;
658 spin_lock_irqsave(&target->lock, flags);
659 if (!scmnd) {
660 scmnd = req->scmnd;
661 req->scmnd = NULL;
662 } else if (req->scmnd == scmnd) {
663 req->scmnd = NULL;
664 } else {
665 scmnd = NULL;
667 spin_unlock_irqrestore(&target->lock, flags);
669 return scmnd;
673 * srp_free_req() - Unmap data and add request to the free request list.
675 static void srp_free_req(struct srp_target_port *target,
676 struct srp_request *req, struct scsi_cmnd *scmnd,
677 s32 req_lim_delta)
679 unsigned long flags;
681 srp_unmap_data(scmnd, target, req);
683 spin_lock_irqsave(&target->lock, flags);
684 target->req_lim += req_lim_delta;
685 list_add_tail(&req->list, &target->free_reqs);
686 spin_unlock_irqrestore(&target->lock, flags);
689 static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
691 struct scsi_cmnd *scmnd = srp_claim_req(target, req, NULL);
693 if (scmnd) {
694 srp_free_req(target, req, scmnd, 0);
695 scmnd->result = DID_RESET << 16;
696 scmnd->scsi_done(scmnd);
700 static int srp_reconnect_target(struct srp_target_port *target)
702 struct Scsi_Host *shost = target->scsi_host;
703 int i, ret;
705 scsi_target_block(&shost->shost_gendev);
707 srp_disconnect_target(target);
709 * Now get a new local CM ID so that we avoid confusing the target in
710 * case things are really fouled up. Doing so also ensures that all CM
711 * callbacks will have finished before a new QP is allocated.
713 ret = srp_new_cm_id(target);
715 * Whether or not creating a new CM ID succeeded, create a new
716 * QP. This guarantees that all completion callback function
717 * invocations have finished before request resetting starts.
719 if (ret == 0)
720 ret = srp_create_target_ib(target);
721 else
722 srp_create_target_ib(target);
724 for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
725 struct srp_request *req = &target->req_ring[i];
726 if (req->scmnd)
727 srp_reset_req(target, req);
730 INIT_LIST_HEAD(&target->free_tx);
731 for (i = 0; i < SRP_SQ_SIZE; ++i)
732 list_add(&target->tx_ring[i]->list, &target->free_tx);
734 if (ret == 0)
735 ret = srp_connect_target(target);
737 scsi_target_unblock(&shost->shost_gendev, ret == 0 ? SDEV_RUNNING :
738 SDEV_TRANSPORT_OFFLINE);
739 target->transport_offline = !!ret;
741 if (ret)
742 goto err;
744 shost_printk(KERN_INFO, target->scsi_host, PFX "reconnect succeeded\n");
746 return ret;
748 err:
749 shost_printk(KERN_ERR, target->scsi_host,
750 PFX "reconnect failed (%d), removing target port.\n", ret);
753 * We couldn't reconnect, so kill our target port off.
754 * However, we have to defer the real removal because we
755 * are in the context of the SCSI error handler now, which
756 * will deadlock if we call scsi_remove_host().
758 srp_queue_remove_work(target);
760 return ret;
763 static void srp_map_desc(struct srp_map_state *state, dma_addr_t dma_addr,
764 unsigned int dma_len, u32 rkey)
766 struct srp_direct_buf *desc = state->desc;
768 desc->va = cpu_to_be64(dma_addr);
769 desc->key = cpu_to_be32(rkey);
770 desc->len = cpu_to_be32(dma_len);
772 state->total_len += dma_len;
773 state->desc++;
774 state->ndesc++;
777 static int srp_map_finish_fmr(struct srp_map_state *state,
778 struct srp_target_port *target)
780 struct srp_device *dev = target->srp_host->srp_dev;
781 struct ib_pool_fmr *fmr;
782 u64 io_addr = 0;
784 if (!state->npages)
785 return 0;
787 if (state->npages == 1) {
788 srp_map_desc(state, state->base_dma_addr, state->fmr_len,
789 target->rkey);
790 state->npages = state->fmr_len = 0;
791 return 0;
794 fmr = ib_fmr_pool_map_phys(dev->fmr_pool, state->pages,
795 state->npages, io_addr);
796 if (IS_ERR(fmr))
797 return PTR_ERR(fmr);
799 *state->next_fmr++ = fmr;
800 state->nfmr++;
802 srp_map_desc(state, 0, state->fmr_len, fmr->fmr->rkey);
803 state->npages = state->fmr_len = 0;
804 return 0;
807 static void srp_map_update_start(struct srp_map_state *state,
808 struct scatterlist *sg, int sg_index,
809 dma_addr_t dma_addr)
811 state->unmapped_sg = sg;
812 state->unmapped_index = sg_index;
813 state->unmapped_addr = dma_addr;
816 static int srp_map_sg_entry(struct srp_map_state *state,
817 struct srp_target_port *target,
818 struct scatterlist *sg, int sg_index,
819 int use_fmr)
821 struct srp_device *dev = target->srp_host->srp_dev;
822 struct ib_device *ibdev = dev->dev;
823 dma_addr_t dma_addr = ib_sg_dma_address(ibdev, sg);
824 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
825 unsigned int len;
826 int ret;
828 if (!dma_len)
829 return 0;
831 if (use_fmr == SRP_MAP_NO_FMR) {
832 /* Once we're in direct map mode for a request, we don't
833 * go back to FMR mode, so no need to update anything
834 * other than the descriptor.
836 srp_map_desc(state, dma_addr, dma_len, target->rkey);
837 return 0;
840 /* If we start at an offset into the FMR page, don't merge into
841 * the current FMR. Finish it out, and use the kernel's MR for this
842 * sg entry. This is to avoid potential bugs on some SRP targets
843 * that were never quite defined, but went away when the initiator
844 * avoided using FMR on such page fragments.
846 if (dma_addr & ~dev->fmr_page_mask || dma_len > dev->fmr_max_size) {
847 ret = srp_map_finish_fmr(state, target);
848 if (ret)
849 return ret;
851 srp_map_desc(state, dma_addr, dma_len, target->rkey);
852 srp_map_update_start(state, NULL, 0, 0);
853 return 0;
856 /* If this is the first sg to go into the FMR, save our position.
857 * We need to know the first unmapped entry, its index, and the
858 * first unmapped address within that entry to be able to restart
859 * mapping after an error.
861 if (!state->unmapped_sg)
862 srp_map_update_start(state, sg, sg_index, dma_addr);
864 while (dma_len) {
865 if (state->npages == SRP_FMR_SIZE) {
866 ret = srp_map_finish_fmr(state, target);
867 if (ret)
868 return ret;
870 srp_map_update_start(state, sg, sg_index, dma_addr);
873 len = min_t(unsigned int, dma_len, dev->fmr_page_size);
875 if (!state->npages)
876 state->base_dma_addr = dma_addr;
877 state->pages[state->npages++] = dma_addr;
878 state->fmr_len += len;
879 dma_addr += len;
880 dma_len -= len;
883 /* If the last entry of the FMR wasn't a full page, then we need to
884 * close it out and start a new one -- we can only merge at page
885 * boundries.
887 ret = 0;
888 if (len != dev->fmr_page_size) {
889 ret = srp_map_finish_fmr(state, target);
890 if (!ret)
891 srp_map_update_start(state, NULL, 0, 0);
893 return ret;
896 static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
897 struct srp_request *req)
899 struct scatterlist *scat, *sg;
900 struct srp_cmd *cmd = req->cmd->buf;
901 int i, len, nents, count, use_fmr;
902 struct srp_device *dev;
903 struct ib_device *ibdev;
904 struct srp_map_state state;
905 struct srp_indirect_buf *indirect_hdr;
906 u32 table_len;
907 u8 fmt;
909 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
910 return sizeof (struct srp_cmd);
912 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
913 scmnd->sc_data_direction != DMA_TO_DEVICE) {
914 shost_printk(KERN_WARNING, target->scsi_host,
915 PFX "Unhandled data direction %d\n",
916 scmnd->sc_data_direction);
917 return -EINVAL;
920 nents = scsi_sg_count(scmnd);
921 scat = scsi_sglist(scmnd);
923 dev = target->srp_host->srp_dev;
924 ibdev = dev->dev;
926 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
927 if (unlikely(count == 0))
928 return -EIO;
930 fmt = SRP_DATA_DESC_DIRECT;
931 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
933 if (count == 1) {
935 * The midlayer only generated a single gather/scatter
936 * entry, or DMA mapping coalesced everything to a
937 * single entry. So a direct descriptor along with
938 * the DMA MR suffices.
940 struct srp_direct_buf *buf = (void *) cmd->add_data;
942 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
943 buf->key = cpu_to_be32(target->rkey);
944 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
946 req->nfmr = 0;
947 goto map_complete;
950 /* We have more than one scatter/gather entry, so build our indirect
951 * descriptor table, trying to merge as many entries with FMR as we
952 * can.
954 indirect_hdr = (void *) cmd->add_data;
956 ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr,
957 target->indirect_size, DMA_TO_DEVICE);
959 memset(&state, 0, sizeof(state));
960 state.desc = req->indirect_desc;
961 state.pages = req->map_page;
962 state.next_fmr = req->fmr_list;
964 use_fmr = dev->fmr_pool ? SRP_MAP_ALLOW_FMR : SRP_MAP_NO_FMR;
966 for_each_sg(scat, sg, count, i) {
967 if (srp_map_sg_entry(&state, target, sg, i, use_fmr)) {
968 /* FMR mapping failed, so backtrack to the first
969 * unmapped entry and continue on without using FMR.
971 dma_addr_t dma_addr;
972 unsigned int dma_len;
974 backtrack:
975 sg = state.unmapped_sg;
976 i = state.unmapped_index;
978 dma_addr = ib_sg_dma_address(ibdev, sg);
979 dma_len = ib_sg_dma_len(ibdev, sg);
980 dma_len -= (state.unmapped_addr - dma_addr);
981 dma_addr = state.unmapped_addr;
982 use_fmr = SRP_MAP_NO_FMR;
983 srp_map_desc(&state, dma_addr, dma_len, target->rkey);
987 if (use_fmr == SRP_MAP_ALLOW_FMR && srp_map_finish_fmr(&state, target))
988 goto backtrack;
990 /* We've mapped the request, now pull as much of the indirect
991 * descriptor table as we can into the command buffer. If this
992 * target is not using an external indirect table, we are
993 * guaranteed to fit into the command, as the SCSI layer won't
994 * give us more S/G entries than we allow.
996 req->nfmr = state.nfmr;
997 if (state.ndesc == 1) {
998 /* FMR mapping was able to collapse this to one entry,
999 * so use a direct descriptor.
1001 struct srp_direct_buf *buf = (void *) cmd->add_data;
1003 *buf = req->indirect_desc[0];
1004 goto map_complete;
1007 if (unlikely(target->cmd_sg_cnt < state.ndesc &&
1008 !target->allow_ext_sg)) {
1009 shost_printk(KERN_ERR, target->scsi_host,
1010 "Could not fit S/G list into SRP_CMD\n");
1011 return -EIO;
1014 count = min(state.ndesc, target->cmd_sg_cnt);
1015 table_len = state.ndesc * sizeof (struct srp_direct_buf);
1017 fmt = SRP_DATA_DESC_INDIRECT;
1018 len = sizeof(struct srp_cmd) + sizeof (struct srp_indirect_buf);
1019 len += count * sizeof (struct srp_direct_buf);
1021 memcpy(indirect_hdr->desc_list, req->indirect_desc,
1022 count * sizeof (struct srp_direct_buf));
1024 indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr);
1025 indirect_hdr->table_desc.key = cpu_to_be32(target->rkey);
1026 indirect_hdr->table_desc.len = cpu_to_be32(table_len);
1027 indirect_hdr->len = cpu_to_be32(state.total_len);
1029 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
1030 cmd->data_out_desc_cnt = count;
1031 else
1032 cmd->data_in_desc_cnt = count;
1034 ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len,
1035 DMA_TO_DEVICE);
1037 map_complete:
1038 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
1039 cmd->buf_fmt = fmt << 4;
1040 else
1041 cmd->buf_fmt = fmt;
1043 return len;
1047 * Return an IU and possible credit to the free pool
1049 static void srp_put_tx_iu(struct srp_target_port *target, struct srp_iu *iu,
1050 enum srp_iu_type iu_type)
1052 unsigned long flags;
1054 spin_lock_irqsave(&target->lock, flags);
1055 list_add(&iu->list, &target->free_tx);
1056 if (iu_type != SRP_IU_RSP)
1057 ++target->req_lim;
1058 spin_unlock_irqrestore(&target->lock, flags);
1062 * Must be called with target->lock held to protect req_lim and free_tx.
1063 * If IU is not sent, it must be returned using srp_put_tx_iu().
1065 * Note:
1066 * An upper limit for the number of allocated information units for each
1067 * request type is:
1068 * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues
1069 * more than Scsi_Host.can_queue requests.
1070 * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE.
1071 * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than
1072 * one unanswered SRP request to an initiator.
1074 static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
1075 enum srp_iu_type iu_type)
1077 s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE;
1078 struct srp_iu *iu;
1080 srp_send_completion(target->send_cq, target);
1082 if (list_empty(&target->free_tx))
1083 return NULL;
1085 /* Initiator responses to target requests do not consume credits */
1086 if (iu_type != SRP_IU_RSP) {
1087 if (target->req_lim <= rsv) {
1088 ++target->zero_req_lim;
1089 return NULL;
1092 --target->req_lim;
1095 iu = list_first_entry(&target->free_tx, struct srp_iu, list);
1096 list_del(&iu->list);
1097 return iu;
1100 static int srp_post_send(struct srp_target_port *target,
1101 struct srp_iu *iu, int len)
1103 struct ib_sge list;
1104 struct ib_send_wr wr, *bad_wr;
1106 list.addr = iu->dma;
1107 list.length = len;
1108 list.lkey = target->lkey;
1110 wr.next = NULL;
1111 wr.wr_id = (uintptr_t) iu;
1112 wr.sg_list = &list;
1113 wr.num_sge = 1;
1114 wr.opcode = IB_WR_SEND;
1115 wr.send_flags = IB_SEND_SIGNALED;
1117 return ib_post_send(target->qp, &wr, &bad_wr);
1120 static int srp_post_recv(struct srp_target_port *target, struct srp_iu *iu)
1122 struct ib_recv_wr wr, *bad_wr;
1123 struct ib_sge list;
1125 list.addr = iu->dma;
1126 list.length = iu->size;
1127 list.lkey = target->lkey;
1129 wr.next = NULL;
1130 wr.wr_id = (uintptr_t) iu;
1131 wr.sg_list = &list;
1132 wr.num_sge = 1;
1134 return ib_post_recv(target->qp, &wr, &bad_wr);
1137 static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
1139 struct srp_request *req;
1140 struct scsi_cmnd *scmnd;
1141 unsigned long flags;
1143 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
1144 spin_lock_irqsave(&target->lock, flags);
1145 target->req_lim += be32_to_cpu(rsp->req_lim_delta);
1146 spin_unlock_irqrestore(&target->lock, flags);
1148 target->tsk_mgmt_status = -1;
1149 if (be32_to_cpu(rsp->resp_data_len) >= 4)
1150 target->tsk_mgmt_status = rsp->data[3];
1151 complete(&target->tsk_mgmt_done);
1152 } else {
1153 req = &target->req_ring[rsp->tag];
1154 scmnd = srp_claim_req(target, req, NULL);
1155 if (!scmnd) {
1156 shost_printk(KERN_ERR, target->scsi_host,
1157 "Null scmnd for RSP w/tag %016llx\n",
1158 (unsigned long long) rsp->tag);
1160 spin_lock_irqsave(&target->lock, flags);
1161 target->req_lim += be32_to_cpu(rsp->req_lim_delta);
1162 spin_unlock_irqrestore(&target->lock, flags);
1164 return;
1166 scmnd->result = rsp->status;
1168 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
1169 memcpy(scmnd->sense_buffer, rsp->data +
1170 be32_to_cpu(rsp->resp_data_len),
1171 min_t(int, be32_to_cpu(rsp->sense_data_len),
1172 SCSI_SENSE_BUFFERSIZE));
1175 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
1176 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
1177 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
1178 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
1180 srp_free_req(target, req, scmnd,
1181 be32_to_cpu(rsp->req_lim_delta));
1183 scmnd->host_scribble = NULL;
1184 scmnd->scsi_done(scmnd);
1188 static int srp_response_common(struct srp_target_port *target, s32 req_delta,
1189 void *rsp, int len)
1191 struct ib_device *dev = target->srp_host->srp_dev->dev;
1192 unsigned long flags;
1193 struct srp_iu *iu;
1194 int err;
1196 spin_lock_irqsave(&target->lock, flags);
1197 target->req_lim += req_delta;
1198 iu = __srp_get_tx_iu(target, SRP_IU_RSP);
1199 spin_unlock_irqrestore(&target->lock, flags);
1201 if (!iu) {
1202 shost_printk(KERN_ERR, target->scsi_host, PFX
1203 "no IU available to send response\n");
1204 return 1;
1207 ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE);
1208 memcpy(iu->buf, rsp, len);
1209 ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE);
1211 err = srp_post_send(target, iu, len);
1212 if (err) {
1213 shost_printk(KERN_ERR, target->scsi_host, PFX
1214 "unable to post response: %d\n", err);
1215 srp_put_tx_iu(target, iu, SRP_IU_RSP);
1218 return err;
1221 static void srp_process_cred_req(struct srp_target_port *target,
1222 struct srp_cred_req *req)
1224 struct srp_cred_rsp rsp = {
1225 .opcode = SRP_CRED_RSP,
1226 .tag = req->tag,
1228 s32 delta = be32_to_cpu(req->req_lim_delta);
1230 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1231 shost_printk(KERN_ERR, target->scsi_host, PFX
1232 "problems processing SRP_CRED_REQ\n");
1235 static void srp_process_aer_req(struct srp_target_port *target,
1236 struct srp_aer_req *req)
1238 struct srp_aer_rsp rsp = {
1239 .opcode = SRP_AER_RSP,
1240 .tag = req->tag,
1242 s32 delta = be32_to_cpu(req->req_lim_delta);
1244 shost_printk(KERN_ERR, target->scsi_host, PFX
1245 "ignoring AER for LUN %llu\n", be64_to_cpu(req->lun));
1247 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1248 shost_printk(KERN_ERR, target->scsi_host, PFX
1249 "problems processing SRP_AER_REQ\n");
1252 static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
1254 struct ib_device *dev = target->srp_host->srp_dev->dev;
1255 struct srp_iu *iu = (struct srp_iu *) (uintptr_t) wc->wr_id;
1256 int res;
1257 u8 opcode;
1259 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
1260 DMA_FROM_DEVICE);
1262 opcode = *(u8 *) iu->buf;
1264 if (0) {
1265 shost_printk(KERN_ERR, target->scsi_host,
1266 PFX "recv completion, opcode 0x%02x\n", opcode);
1267 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1,
1268 iu->buf, wc->byte_len, true);
1271 switch (opcode) {
1272 case SRP_RSP:
1273 srp_process_rsp(target, iu->buf);
1274 break;
1276 case SRP_CRED_REQ:
1277 srp_process_cred_req(target, iu->buf);
1278 break;
1280 case SRP_AER_REQ:
1281 srp_process_aer_req(target, iu->buf);
1282 break;
1284 case SRP_T_LOGOUT:
1285 /* XXX Handle target logout */
1286 shost_printk(KERN_WARNING, target->scsi_host,
1287 PFX "Got target logout request\n");
1288 break;
1290 default:
1291 shost_printk(KERN_WARNING, target->scsi_host,
1292 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
1293 break;
1296 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
1297 DMA_FROM_DEVICE);
1299 res = srp_post_recv(target, iu);
1300 if (res != 0)
1301 shost_printk(KERN_ERR, target->scsi_host,
1302 PFX "Recv failed with error code %d\n", res);
1305 static void srp_handle_qp_err(enum ib_wc_status wc_status,
1306 enum ib_wc_opcode wc_opcode,
1307 struct srp_target_port *target)
1309 if (target->connected && !target->qp_in_error) {
1310 shost_printk(KERN_ERR, target->scsi_host,
1311 PFX "failed %s status %d\n",
1312 wc_opcode & IB_WC_RECV ? "receive" : "send",
1313 wc_status);
1315 target->qp_in_error = true;
1318 static void srp_recv_completion(struct ib_cq *cq, void *target_ptr)
1320 struct srp_target_port *target = target_ptr;
1321 struct ib_wc wc;
1323 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1324 while (ib_poll_cq(cq, 1, &wc) > 0) {
1325 if (likely(wc.status == IB_WC_SUCCESS)) {
1326 srp_handle_recv(target, &wc);
1327 } else {
1328 srp_handle_qp_err(wc.status, wc.opcode, target);
1333 static void srp_send_completion(struct ib_cq *cq, void *target_ptr)
1335 struct srp_target_port *target = target_ptr;
1336 struct ib_wc wc;
1337 struct srp_iu *iu;
1339 while (ib_poll_cq(cq, 1, &wc) > 0) {
1340 if (likely(wc.status == IB_WC_SUCCESS)) {
1341 iu = (struct srp_iu *) (uintptr_t) wc.wr_id;
1342 list_add(&iu->list, &target->free_tx);
1343 } else {
1344 srp_handle_qp_err(wc.status, wc.opcode, target);
1349 static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd)
1351 struct srp_target_port *target = host_to_target(shost);
1352 struct srp_request *req;
1353 struct srp_iu *iu;
1354 struct srp_cmd *cmd;
1355 struct ib_device *dev;
1356 unsigned long flags;
1357 int len;
1359 if (unlikely(target->transport_offline)) {
1360 scmnd->result = DID_NO_CONNECT << 16;
1361 scmnd->scsi_done(scmnd);
1362 return 0;
1365 spin_lock_irqsave(&target->lock, flags);
1366 iu = __srp_get_tx_iu(target, SRP_IU_CMD);
1367 if (!iu)
1368 goto err_unlock;
1370 req = list_first_entry(&target->free_reqs, struct srp_request, list);
1371 list_del(&req->list);
1372 spin_unlock_irqrestore(&target->lock, flags);
1374 dev = target->srp_host->srp_dev->dev;
1375 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_iu_len,
1376 DMA_TO_DEVICE);
1378 scmnd->result = 0;
1379 scmnd->host_scribble = (void *) req;
1381 cmd = iu->buf;
1382 memset(cmd, 0, sizeof *cmd);
1384 cmd->opcode = SRP_CMD;
1385 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
1386 cmd->tag = req->index;
1387 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1389 req->scmnd = scmnd;
1390 req->cmd = iu;
1392 len = srp_map_data(scmnd, target, req);
1393 if (len < 0) {
1394 shost_printk(KERN_ERR, target->scsi_host,
1395 PFX "Failed to map data\n");
1396 goto err_iu;
1399 ib_dma_sync_single_for_device(dev, iu->dma, target->max_iu_len,
1400 DMA_TO_DEVICE);
1402 if (srp_post_send(target, iu, len)) {
1403 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
1404 goto err_unmap;
1407 return 0;
1409 err_unmap:
1410 srp_unmap_data(scmnd, target, req);
1412 err_iu:
1413 srp_put_tx_iu(target, iu, SRP_IU_CMD);
1415 spin_lock_irqsave(&target->lock, flags);
1416 list_add(&req->list, &target->free_reqs);
1418 err_unlock:
1419 spin_unlock_irqrestore(&target->lock, flags);
1421 return SCSI_MLQUEUE_HOST_BUSY;
1424 static int srp_alloc_iu_bufs(struct srp_target_port *target)
1426 int i;
1428 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1429 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1430 target->max_ti_iu_len,
1431 GFP_KERNEL, DMA_FROM_DEVICE);
1432 if (!target->rx_ring[i])
1433 goto err;
1436 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1437 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
1438 target->max_iu_len,
1439 GFP_KERNEL, DMA_TO_DEVICE);
1440 if (!target->tx_ring[i])
1441 goto err;
1443 list_add(&target->tx_ring[i]->list, &target->free_tx);
1446 return 0;
1448 err:
1449 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1450 srp_free_iu(target->srp_host, target->rx_ring[i]);
1451 target->rx_ring[i] = NULL;
1454 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1455 srp_free_iu(target->srp_host, target->tx_ring[i]);
1456 target->tx_ring[i] = NULL;
1459 return -ENOMEM;
1462 static uint32_t srp_compute_rq_tmo(struct ib_qp_attr *qp_attr, int attr_mask)
1464 uint64_t T_tr_ns, max_compl_time_ms;
1465 uint32_t rq_tmo_jiffies;
1468 * According to section 11.2.4.2 in the IBTA spec (Modify Queue Pair,
1469 * table 91), both the QP timeout and the retry count have to be set
1470 * for RC QP's during the RTR to RTS transition.
1472 WARN_ON_ONCE((attr_mask & (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)) !=
1473 (IB_QP_TIMEOUT | IB_QP_RETRY_CNT));
1476 * Set target->rq_tmo_jiffies to one second more than the largest time
1477 * it can take before an error completion is generated. See also
1478 * C9-140..142 in the IBTA spec for more information about how to
1479 * convert the QP Local ACK Timeout value to nanoseconds.
1481 T_tr_ns = 4096 * (1ULL << qp_attr->timeout);
1482 max_compl_time_ms = qp_attr->retry_cnt * 4 * T_tr_ns;
1483 do_div(max_compl_time_ms, NSEC_PER_MSEC);
1484 rq_tmo_jiffies = msecs_to_jiffies(max_compl_time_ms + 1000);
1486 return rq_tmo_jiffies;
1489 static void srp_cm_rep_handler(struct ib_cm_id *cm_id,
1490 struct srp_login_rsp *lrsp,
1491 struct srp_target_port *target)
1493 struct ib_qp_attr *qp_attr = NULL;
1494 int attr_mask = 0;
1495 int ret;
1496 int i;
1498 if (lrsp->opcode == SRP_LOGIN_RSP) {
1499 target->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len);
1500 target->req_lim = be32_to_cpu(lrsp->req_lim_delta);
1503 * Reserve credits for task management so we don't
1504 * bounce requests back to the SCSI mid-layer.
1506 target->scsi_host->can_queue
1507 = min(target->req_lim - SRP_TSK_MGMT_SQ_SIZE,
1508 target->scsi_host->can_queue);
1509 } else {
1510 shost_printk(KERN_WARNING, target->scsi_host,
1511 PFX "Unhandled RSP opcode %#x\n", lrsp->opcode);
1512 ret = -ECONNRESET;
1513 goto error;
1516 if (!target->rx_ring[0]) {
1517 ret = srp_alloc_iu_bufs(target);
1518 if (ret)
1519 goto error;
1522 ret = -ENOMEM;
1523 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1524 if (!qp_attr)
1525 goto error;
1527 qp_attr->qp_state = IB_QPS_RTR;
1528 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1529 if (ret)
1530 goto error_free;
1532 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
1533 if (ret)
1534 goto error_free;
1536 for (i = 0; i < SRP_RQ_SIZE; i++) {
1537 struct srp_iu *iu = target->rx_ring[i];
1538 ret = srp_post_recv(target, iu);
1539 if (ret)
1540 goto error_free;
1543 qp_attr->qp_state = IB_QPS_RTS;
1544 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1545 if (ret)
1546 goto error_free;
1548 target->rq_tmo_jiffies = srp_compute_rq_tmo(qp_attr, attr_mask);
1550 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
1551 if (ret)
1552 goto error_free;
1554 ret = ib_send_cm_rtu(cm_id, NULL, 0);
1556 error_free:
1557 kfree(qp_attr);
1559 error:
1560 target->status = ret;
1563 static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1564 struct ib_cm_event *event,
1565 struct srp_target_port *target)
1567 struct Scsi_Host *shost = target->scsi_host;
1568 struct ib_class_port_info *cpi;
1569 int opcode;
1571 switch (event->param.rej_rcvd.reason) {
1572 case IB_CM_REJ_PORT_CM_REDIRECT:
1573 cpi = event->param.rej_rcvd.ari;
1574 target->path.dlid = cpi->redirect_lid;
1575 target->path.pkey = cpi->redirect_pkey;
1576 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1577 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1579 target->status = target->path.dlid ?
1580 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1581 break;
1583 case IB_CM_REJ_PORT_REDIRECT:
1584 if (srp_target_is_topspin(target)) {
1586 * Topspin/Cisco SRP gateways incorrectly send
1587 * reject reason code 25 when they mean 24
1588 * (port redirect).
1590 memcpy(target->path.dgid.raw,
1591 event->param.rej_rcvd.ari, 16);
1593 shost_printk(KERN_DEBUG, shost,
1594 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1595 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1596 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1598 target->status = SRP_PORT_REDIRECT;
1599 } else {
1600 shost_printk(KERN_WARNING, shost,
1601 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1602 target->status = -ECONNRESET;
1604 break;
1606 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
1607 shost_printk(KERN_WARNING, shost,
1608 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1609 target->status = -ECONNRESET;
1610 break;
1612 case IB_CM_REJ_CONSUMER_DEFINED:
1613 opcode = *(u8 *) event->private_data;
1614 if (opcode == SRP_LOGIN_REJ) {
1615 struct srp_login_rej *rej = event->private_data;
1616 u32 reason = be32_to_cpu(rej->reason);
1618 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
1619 shost_printk(KERN_WARNING, shost,
1620 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1621 else
1622 shost_printk(KERN_WARNING, shost,
1623 PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
1624 } else
1625 shost_printk(KERN_WARNING, shost,
1626 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1627 " opcode 0x%02x\n", opcode);
1628 target->status = -ECONNRESET;
1629 break;
1631 case IB_CM_REJ_STALE_CONN:
1632 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
1633 target->status = SRP_STALE_CONN;
1634 break;
1636 default:
1637 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
1638 event->param.rej_rcvd.reason);
1639 target->status = -ECONNRESET;
1643 static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1645 struct srp_target_port *target = cm_id->context;
1646 int comp = 0;
1648 switch (event->event) {
1649 case IB_CM_REQ_ERROR:
1650 shost_printk(KERN_DEBUG, target->scsi_host,
1651 PFX "Sending CM REQ failed\n");
1652 comp = 1;
1653 target->status = -ECONNRESET;
1654 break;
1656 case IB_CM_REP_RECEIVED:
1657 comp = 1;
1658 srp_cm_rep_handler(cm_id, event->private_data, target);
1659 break;
1661 case IB_CM_REJ_RECEIVED:
1662 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
1663 comp = 1;
1665 srp_cm_rej_handler(cm_id, event, target);
1666 break;
1668 case IB_CM_DREQ_RECEIVED:
1669 shost_printk(KERN_WARNING, target->scsi_host,
1670 PFX "DREQ received - connection closed\n");
1671 srp_change_conn_state(target, false);
1672 if (ib_send_cm_drep(cm_id, NULL, 0))
1673 shost_printk(KERN_ERR, target->scsi_host,
1674 PFX "Sending CM DREP failed\n");
1675 break;
1677 case IB_CM_TIMEWAIT_EXIT:
1678 shost_printk(KERN_ERR, target->scsi_host,
1679 PFX "connection closed\n");
1681 target->status = 0;
1682 break;
1684 case IB_CM_MRA_RECEIVED:
1685 case IB_CM_DREQ_ERROR:
1686 case IB_CM_DREP_RECEIVED:
1687 break;
1689 default:
1690 shost_printk(KERN_WARNING, target->scsi_host,
1691 PFX "Unhandled CM event %d\n", event->event);
1692 break;
1695 if (comp)
1696 complete(&target->done);
1698 return 0;
1701 static int srp_send_tsk_mgmt(struct srp_target_port *target,
1702 u64 req_tag, unsigned int lun, u8 func)
1704 struct ib_device *dev = target->srp_host->srp_dev->dev;
1705 struct srp_iu *iu;
1706 struct srp_tsk_mgmt *tsk_mgmt;
1708 if (!target->connected || target->qp_in_error)
1709 return -1;
1711 init_completion(&target->tsk_mgmt_done);
1713 spin_lock_irq(&target->lock);
1714 iu = __srp_get_tx_iu(target, SRP_IU_TSK_MGMT);
1715 spin_unlock_irq(&target->lock);
1717 if (!iu)
1718 return -1;
1720 ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt,
1721 DMA_TO_DEVICE);
1722 tsk_mgmt = iu->buf;
1723 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1725 tsk_mgmt->opcode = SRP_TSK_MGMT;
1726 tsk_mgmt->lun = cpu_to_be64((u64) lun << 48);
1727 tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT;
1728 tsk_mgmt->tsk_mgmt_func = func;
1729 tsk_mgmt->task_tag = req_tag;
1731 ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt,
1732 DMA_TO_DEVICE);
1733 if (srp_post_send(target, iu, sizeof *tsk_mgmt)) {
1734 srp_put_tx_iu(target, iu, SRP_IU_TSK_MGMT);
1735 return -1;
1738 if (!wait_for_completion_timeout(&target->tsk_mgmt_done,
1739 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
1740 return -1;
1742 return 0;
1745 static int srp_abort(struct scsi_cmnd *scmnd)
1747 struct srp_target_port *target = host_to_target(scmnd->device->host);
1748 struct srp_request *req = (struct srp_request *) scmnd->host_scribble;
1749 int ret;
1751 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
1753 if (!req || !srp_claim_req(target, req, scmnd))
1754 return FAILED;
1755 if (srp_send_tsk_mgmt(target, req->index, scmnd->device->lun,
1756 SRP_TSK_ABORT_TASK) == 0)
1757 ret = SUCCESS;
1758 else if (target->transport_offline)
1759 ret = FAST_IO_FAIL;
1760 else
1761 ret = FAILED;
1762 srp_free_req(target, req, scmnd, 0);
1763 scmnd->result = DID_ABORT << 16;
1764 scmnd->scsi_done(scmnd);
1766 return ret;
1769 static int srp_reset_device(struct scsi_cmnd *scmnd)
1771 struct srp_target_port *target = host_to_target(scmnd->device->host);
1772 int i;
1774 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
1776 if (srp_send_tsk_mgmt(target, SRP_TAG_NO_REQ, scmnd->device->lun,
1777 SRP_TSK_LUN_RESET))
1778 return FAILED;
1779 if (target->tsk_mgmt_status)
1780 return FAILED;
1782 for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
1783 struct srp_request *req = &target->req_ring[i];
1784 if (req->scmnd && req->scmnd->device == scmnd->device)
1785 srp_reset_req(target, req);
1788 return SUCCESS;
1791 static int srp_reset_host(struct scsi_cmnd *scmnd)
1793 struct srp_target_port *target = host_to_target(scmnd->device->host);
1794 int ret = FAILED;
1796 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
1798 if (!srp_reconnect_target(target))
1799 ret = SUCCESS;
1801 return ret;
1804 static int srp_slave_configure(struct scsi_device *sdev)
1806 struct Scsi_Host *shost = sdev->host;
1807 struct srp_target_port *target = host_to_target(shost);
1808 struct request_queue *q = sdev->request_queue;
1809 unsigned long timeout;
1811 if (sdev->type == TYPE_DISK) {
1812 timeout = max_t(unsigned, 30 * HZ, target->rq_tmo_jiffies);
1813 blk_queue_rq_timeout(q, timeout);
1816 return 0;
1819 static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
1820 char *buf)
1822 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1824 return sprintf(buf, "0x%016llx\n",
1825 (unsigned long long) be64_to_cpu(target->id_ext));
1828 static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
1829 char *buf)
1831 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1833 return sprintf(buf, "0x%016llx\n",
1834 (unsigned long long) be64_to_cpu(target->ioc_guid));
1837 static ssize_t show_service_id(struct device *dev,
1838 struct device_attribute *attr, char *buf)
1840 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1842 return sprintf(buf, "0x%016llx\n",
1843 (unsigned long long) be64_to_cpu(target->service_id));
1846 static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
1847 char *buf)
1849 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1851 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1854 static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
1855 char *buf)
1857 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1859 return sprintf(buf, "%pI6\n", target->path.dgid.raw);
1862 static ssize_t show_orig_dgid(struct device *dev,
1863 struct device_attribute *attr, char *buf)
1865 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1867 return sprintf(buf, "%pI6\n", target->orig_dgid);
1870 static ssize_t show_req_lim(struct device *dev,
1871 struct device_attribute *attr, char *buf)
1873 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1875 return sprintf(buf, "%d\n", target->req_lim);
1878 static ssize_t show_zero_req_lim(struct device *dev,
1879 struct device_attribute *attr, char *buf)
1881 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1883 return sprintf(buf, "%d\n", target->zero_req_lim);
1886 static ssize_t show_local_ib_port(struct device *dev,
1887 struct device_attribute *attr, char *buf)
1889 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1891 return sprintf(buf, "%d\n", target->srp_host->port);
1894 static ssize_t show_local_ib_device(struct device *dev,
1895 struct device_attribute *attr, char *buf)
1897 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1899 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
1902 static ssize_t show_comp_vector(struct device *dev,
1903 struct device_attribute *attr, char *buf)
1905 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1907 return sprintf(buf, "%d\n", target->comp_vector);
1910 static ssize_t show_cmd_sg_entries(struct device *dev,
1911 struct device_attribute *attr, char *buf)
1913 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1915 return sprintf(buf, "%u\n", target->cmd_sg_cnt);
1918 static ssize_t show_allow_ext_sg(struct device *dev,
1919 struct device_attribute *attr, char *buf)
1921 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1923 return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false");
1926 static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1927 static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1928 static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1929 static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1930 static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1931 static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
1932 static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL);
1933 static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1934 static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1935 static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
1936 static DEVICE_ATTR(comp_vector, S_IRUGO, show_comp_vector, NULL);
1937 static DEVICE_ATTR(cmd_sg_entries, S_IRUGO, show_cmd_sg_entries, NULL);
1938 static DEVICE_ATTR(allow_ext_sg, S_IRUGO, show_allow_ext_sg, NULL);
1940 static struct device_attribute *srp_host_attrs[] = {
1941 &dev_attr_id_ext,
1942 &dev_attr_ioc_guid,
1943 &dev_attr_service_id,
1944 &dev_attr_pkey,
1945 &dev_attr_dgid,
1946 &dev_attr_orig_dgid,
1947 &dev_attr_req_lim,
1948 &dev_attr_zero_req_lim,
1949 &dev_attr_local_ib_port,
1950 &dev_attr_local_ib_device,
1951 &dev_attr_comp_vector,
1952 &dev_attr_cmd_sg_entries,
1953 &dev_attr_allow_ext_sg,
1954 NULL
1957 static struct scsi_host_template srp_template = {
1958 .module = THIS_MODULE,
1959 .name = "InfiniBand SRP initiator",
1960 .proc_name = DRV_NAME,
1961 .slave_configure = srp_slave_configure,
1962 .info = srp_target_info,
1963 .queuecommand = srp_queuecommand,
1964 .eh_abort_handler = srp_abort,
1965 .eh_device_reset_handler = srp_reset_device,
1966 .eh_host_reset_handler = srp_reset_host,
1967 .skip_settle_delay = true,
1968 .sg_tablesize = SRP_DEF_SG_TABLESIZE,
1969 .can_queue = SRP_CMD_SQ_SIZE,
1970 .this_id = -1,
1971 .cmd_per_lun = SRP_CMD_SQ_SIZE,
1972 .use_clustering = ENABLE_CLUSTERING,
1973 .shost_attrs = srp_host_attrs
1976 static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1978 struct srp_rport_identifiers ids;
1979 struct srp_rport *rport;
1981 sprintf(target->target_name, "SRP.T10:%016llX",
1982 (unsigned long long) be64_to_cpu(target->id_ext));
1984 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
1985 return -ENODEV;
1987 memcpy(ids.port_id, &target->id_ext, 8);
1988 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
1989 ids.roles = SRP_RPORT_ROLE_TARGET;
1990 rport = srp_rport_add(target->scsi_host, &ids);
1991 if (IS_ERR(rport)) {
1992 scsi_remove_host(target->scsi_host);
1993 return PTR_ERR(rport);
1996 rport->lld_data = target;
1998 spin_lock(&host->target_lock);
1999 list_add_tail(&target->list, &host->target_list);
2000 spin_unlock(&host->target_lock);
2002 target->state = SRP_TARGET_LIVE;
2004 scsi_scan_target(&target->scsi_host->shost_gendev,
2005 0, target->scsi_id, SCAN_WILD_CARD, 0);
2007 return 0;
2010 static void srp_release_dev(struct device *dev)
2012 struct srp_host *host =
2013 container_of(dev, struct srp_host, dev);
2015 complete(&host->released);
2018 static struct class srp_class = {
2019 .name = "infiniband_srp",
2020 .dev_release = srp_release_dev
2024 * srp_conn_unique() - check whether the connection to a target is unique
2026 static bool srp_conn_unique(struct srp_host *host,
2027 struct srp_target_port *target)
2029 struct srp_target_port *t;
2030 bool ret = false;
2032 if (target->state == SRP_TARGET_REMOVED)
2033 goto out;
2035 ret = true;
2037 spin_lock(&host->target_lock);
2038 list_for_each_entry(t, &host->target_list, list) {
2039 if (t != target &&
2040 target->id_ext == t->id_ext &&
2041 target->ioc_guid == t->ioc_guid &&
2042 target->initiator_ext == t->initiator_ext) {
2043 ret = false;
2044 break;
2047 spin_unlock(&host->target_lock);
2049 out:
2050 return ret;
2054 * Target ports are added by writing
2056 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
2057 * pkey=<P_Key>,service_id=<service ID>
2059 * to the add_target sysfs attribute.
2061 enum {
2062 SRP_OPT_ERR = 0,
2063 SRP_OPT_ID_EXT = 1 << 0,
2064 SRP_OPT_IOC_GUID = 1 << 1,
2065 SRP_OPT_DGID = 1 << 2,
2066 SRP_OPT_PKEY = 1 << 3,
2067 SRP_OPT_SERVICE_ID = 1 << 4,
2068 SRP_OPT_MAX_SECT = 1 << 5,
2069 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
2070 SRP_OPT_IO_CLASS = 1 << 7,
2071 SRP_OPT_INITIATOR_EXT = 1 << 8,
2072 SRP_OPT_CMD_SG_ENTRIES = 1 << 9,
2073 SRP_OPT_ALLOW_EXT_SG = 1 << 10,
2074 SRP_OPT_SG_TABLESIZE = 1 << 11,
2075 SRP_OPT_COMP_VECTOR = 1 << 12,
2076 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
2077 SRP_OPT_IOC_GUID |
2078 SRP_OPT_DGID |
2079 SRP_OPT_PKEY |
2080 SRP_OPT_SERVICE_ID),
2083 static const match_table_t srp_opt_tokens = {
2084 { SRP_OPT_ID_EXT, "id_ext=%s" },
2085 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
2086 { SRP_OPT_DGID, "dgid=%s" },
2087 { SRP_OPT_PKEY, "pkey=%x" },
2088 { SRP_OPT_SERVICE_ID, "service_id=%s" },
2089 { SRP_OPT_MAX_SECT, "max_sect=%d" },
2090 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
2091 { SRP_OPT_IO_CLASS, "io_class=%x" },
2092 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
2093 { SRP_OPT_CMD_SG_ENTRIES, "cmd_sg_entries=%u" },
2094 { SRP_OPT_ALLOW_EXT_SG, "allow_ext_sg=%u" },
2095 { SRP_OPT_SG_TABLESIZE, "sg_tablesize=%u" },
2096 { SRP_OPT_COMP_VECTOR, "comp_vector=%u" },
2097 { SRP_OPT_ERR, NULL }
2100 static int srp_parse_options(const char *buf, struct srp_target_port *target)
2102 char *options, *sep_opt;
2103 char *p;
2104 char dgid[3];
2105 substring_t args[MAX_OPT_ARGS];
2106 int opt_mask = 0;
2107 int token;
2108 int ret = -EINVAL;
2109 int i;
2111 options = kstrdup(buf, GFP_KERNEL);
2112 if (!options)
2113 return -ENOMEM;
2115 sep_opt = options;
2116 while ((p = strsep(&sep_opt, ",")) != NULL) {
2117 if (!*p)
2118 continue;
2120 token = match_token(p, srp_opt_tokens, args);
2121 opt_mask |= token;
2123 switch (token) {
2124 case SRP_OPT_ID_EXT:
2125 p = match_strdup(args);
2126 if (!p) {
2127 ret = -ENOMEM;
2128 goto out;
2130 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2131 kfree(p);
2132 break;
2134 case SRP_OPT_IOC_GUID:
2135 p = match_strdup(args);
2136 if (!p) {
2137 ret = -ENOMEM;
2138 goto out;
2140 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
2141 kfree(p);
2142 break;
2144 case SRP_OPT_DGID:
2145 p = match_strdup(args);
2146 if (!p) {
2147 ret = -ENOMEM;
2148 goto out;
2150 if (strlen(p) != 32) {
2151 pr_warn("bad dest GID parameter '%s'\n", p);
2152 kfree(p);
2153 goto out;
2156 for (i = 0; i < 16; ++i) {
2157 strlcpy(dgid, p + i * 2, 3);
2158 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
2160 kfree(p);
2161 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
2162 break;
2164 case SRP_OPT_PKEY:
2165 if (match_hex(args, &token)) {
2166 pr_warn("bad P_Key parameter '%s'\n", p);
2167 goto out;
2169 target->path.pkey = cpu_to_be16(token);
2170 break;
2172 case SRP_OPT_SERVICE_ID:
2173 p = match_strdup(args);
2174 if (!p) {
2175 ret = -ENOMEM;
2176 goto out;
2178 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
2179 target->path.service_id = target->service_id;
2180 kfree(p);
2181 break;
2183 case SRP_OPT_MAX_SECT:
2184 if (match_int(args, &token)) {
2185 pr_warn("bad max sect parameter '%s'\n", p);
2186 goto out;
2188 target->scsi_host->max_sectors = token;
2189 break;
2191 case SRP_OPT_MAX_CMD_PER_LUN:
2192 if (match_int(args, &token)) {
2193 pr_warn("bad max cmd_per_lun parameter '%s'\n",
2195 goto out;
2197 target->scsi_host->cmd_per_lun = min(token, SRP_CMD_SQ_SIZE);
2198 break;
2200 case SRP_OPT_IO_CLASS:
2201 if (match_hex(args, &token)) {
2202 pr_warn("bad IO class parameter '%s'\n", p);
2203 goto out;
2205 if (token != SRP_REV10_IB_IO_CLASS &&
2206 token != SRP_REV16A_IB_IO_CLASS) {
2207 pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n",
2208 token, SRP_REV10_IB_IO_CLASS,
2209 SRP_REV16A_IB_IO_CLASS);
2210 goto out;
2212 target->io_class = token;
2213 break;
2215 case SRP_OPT_INITIATOR_EXT:
2216 p = match_strdup(args);
2217 if (!p) {
2218 ret = -ENOMEM;
2219 goto out;
2221 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2222 kfree(p);
2223 break;
2225 case SRP_OPT_CMD_SG_ENTRIES:
2226 if (match_int(args, &token) || token < 1 || token > 255) {
2227 pr_warn("bad max cmd_sg_entries parameter '%s'\n",
2229 goto out;
2231 target->cmd_sg_cnt = token;
2232 break;
2234 case SRP_OPT_ALLOW_EXT_SG:
2235 if (match_int(args, &token)) {
2236 pr_warn("bad allow_ext_sg parameter '%s'\n", p);
2237 goto out;
2239 target->allow_ext_sg = !!token;
2240 break;
2242 case SRP_OPT_SG_TABLESIZE:
2243 if (match_int(args, &token) || token < 1 ||
2244 token > SCSI_MAX_SG_CHAIN_SEGMENTS) {
2245 pr_warn("bad max sg_tablesize parameter '%s'\n",
2247 goto out;
2249 target->sg_tablesize = token;
2250 break;
2252 case SRP_OPT_COMP_VECTOR:
2253 if (match_int(args, &token) || token < 0) {
2254 pr_warn("bad comp_vector parameter '%s'\n", p);
2255 goto out;
2257 target->comp_vector = token;
2258 break;
2260 default:
2261 pr_warn("unknown parameter or missing value '%s' in target creation request\n",
2263 goto out;
2267 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
2268 ret = 0;
2269 else
2270 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
2271 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
2272 !(srp_opt_tokens[i].token & opt_mask))
2273 pr_warn("target creation request is missing parameter '%s'\n",
2274 srp_opt_tokens[i].pattern);
2276 out:
2277 kfree(options);
2278 return ret;
2281 static ssize_t srp_create_target(struct device *dev,
2282 struct device_attribute *attr,
2283 const char *buf, size_t count)
2285 struct srp_host *host =
2286 container_of(dev, struct srp_host, dev);
2287 struct Scsi_Host *target_host;
2288 struct srp_target_port *target;
2289 struct ib_device *ibdev = host->srp_dev->dev;
2290 dma_addr_t dma_addr;
2291 int i, ret;
2293 target_host = scsi_host_alloc(&srp_template,
2294 sizeof (struct srp_target_port));
2295 if (!target_host)
2296 return -ENOMEM;
2298 target_host->transportt = ib_srp_transport_template;
2299 target_host->max_channel = 0;
2300 target_host->max_id = 1;
2301 target_host->max_lun = SRP_MAX_LUN;
2302 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
2304 target = host_to_target(target_host);
2306 target->io_class = SRP_REV16A_IB_IO_CLASS;
2307 target->scsi_host = target_host;
2308 target->srp_host = host;
2309 target->lkey = host->srp_dev->mr->lkey;
2310 target->rkey = host->srp_dev->mr->rkey;
2311 target->cmd_sg_cnt = cmd_sg_entries;
2312 target->sg_tablesize = indirect_sg_entries ? : cmd_sg_entries;
2313 target->allow_ext_sg = allow_ext_sg;
2315 ret = srp_parse_options(buf, target);
2316 if (ret)
2317 goto err;
2319 if (!srp_conn_unique(target->srp_host, target)) {
2320 shost_printk(KERN_INFO, target->scsi_host,
2321 PFX "Already connected to target port with id_ext=%016llx;ioc_guid=%016llx;initiator_ext=%016llx\n",
2322 be64_to_cpu(target->id_ext),
2323 be64_to_cpu(target->ioc_guid),
2324 be64_to_cpu(target->initiator_ext));
2325 ret = -EEXIST;
2326 goto err;
2329 if (!host->srp_dev->fmr_pool && !target->allow_ext_sg &&
2330 target->cmd_sg_cnt < target->sg_tablesize) {
2331 pr_warn("No FMR pool and no external indirect descriptors, limiting sg_tablesize to cmd_sg_cnt\n");
2332 target->sg_tablesize = target->cmd_sg_cnt;
2335 target_host->sg_tablesize = target->sg_tablesize;
2336 target->indirect_size = target->sg_tablesize *
2337 sizeof (struct srp_direct_buf);
2338 target->max_iu_len = sizeof (struct srp_cmd) +
2339 sizeof (struct srp_indirect_buf) +
2340 target->cmd_sg_cnt * sizeof (struct srp_direct_buf);
2342 INIT_WORK(&target->remove_work, srp_remove_work);
2343 spin_lock_init(&target->lock);
2344 INIT_LIST_HEAD(&target->free_tx);
2345 INIT_LIST_HEAD(&target->free_reqs);
2346 for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
2347 struct srp_request *req = &target->req_ring[i];
2349 req->fmr_list = kmalloc(target->cmd_sg_cnt * sizeof (void *),
2350 GFP_KERNEL);
2351 req->map_page = kmalloc(SRP_FMR_SIZE * sizeof (void *),
2352 GFP_KERNEL);
2353 req->indirect_desc = kmalloc(target->indirect_size, GFP_KERNEL);
2354 if (!req->fmr_list || !req->map_page || !req->indirect_desc)
2355 goto err_free_mem;
2357 dma_addr = ib_dma_map_single(ibdev, req->indirect_desc,
2358 target->indirect_size,
2359 DMA_TO_DEVICE);
2360 if (ib_dma_mapping_error(ibdev, dma_addr))
2361 goto err_free_mem;
2363 req->indirect_dma_addr = dma_addr;
2364 req->index = i;
2365 list_add_tail(&req->list, &target->free_reqs);
2368 ib_query_gid(ibdev, host->port, 0, &target->path.sgid);
2370 shost_printk(KERN_DEBUG, target->scsi_host, PFX
2371 "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
2372 "service_id %016llx dgid %pI6\n",
2373 (unsigned long long) be64_to_cpu(target->id_ext),
2374 (unsigned long long) be64_to_cpu(target->ioc_guid),
2375 be16_to_cpu(target->path.pkey),
2376 (unsigned long long) be64_to_cpu(target->service_id),
2377 target->path.dgid.raw);
2379 ret = srp_create_target_ib(target);
2380 if (ret)
2381 goto err_free_mem;
2383 ret = srp_new_cm_id(target);
2384 if (ret)
2385 goto err_free_ib;
2387 ret = srp_connect_target(target);
2388 if (ret) {
2389 shost_printk(KERN_ERR, target->scsi_host,
2390 PFX "Connection failed\n");
2391 goto err_cm_id;
2394 ret = srp_add_target(host, target);
2395 if (ret)
2396 goto err_disconnect;
2398 return count;
2400 err_disconnect:
2401 srp_disconnect_target(target);
2403 err_cm_id:
2404 ib_destroy_cm_id(target->cm_id);
2406 err_free_ib:
2407 srp_free_target_ib(target);
2409 err_free_mem:
2410 srp_free_req_data(target);
2412 err:
2413 scsi_host_put(target_host);
2415 return ret;
2418 static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
2420 static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
2421 char *buf)
2423 struct srp_host *host = container_of(dev, struct srp_host, dev);
2425 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
2428 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
2430 static ssize_t show_port(struct device *dev, struct device_attribute *attr,
2431 char *buf)
2433 struct srp_host *host = container_of(dev, struct srp_host, dev);
2435 return sprintf(buf, "%d\n", host->port);
2438 static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
2440 static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
2442 struct srp_host *host;
2444 host = kzalloc(sizeof *host, GFP_KERNEL);
2445 if (!host)
2446 return NULL;
2448 INIT_LIST_HEAD(&host->target_list);
2449 spin_lock_init(&host->target_lock);
2450 init_completion(&host->released);
2451 host->srp_dev = device;
2452 host->port = port;
2454 host->dev.class = &srp_class;
2455 host->dev.parent = device->dev->dma_device;
2456 dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port);
2458 if (device_register(&host->dev))
2459 goto free_host;
2460 if (device_create_file(&host->dev, &dev_attr_add_target))
2461 goto err_class;
2462 if (device_create_file(&host->dev, &dev_attr_ibdev))
2463 goto err_class;
2464 if (device_create_file(&host->dev, &dev_attr_port))
2465 goto err_class;
2467 return host;
2469 err_class:
2470 device_unregister(&host->dev);
2472 free_host:
2473 kfree(host);
2475 return NULL;
2478 static void srp_add_one(struct ib_device *device)
2480 struct srp_device *srp_dev;
2481 struct ib_device_attr *dev_attr;
2482 struct ib_fmr_pool_param fmr_param;
2483 struct srp_host *host;
2484 int max_pages_per_fmr, fmr_page_shift, s, e, p;
2486 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
2487 if (!dev_attr)
2488 return;
2490 if (ib_query_device(device, dev_attr)) {
2491 pr_warn("Query device failed for %s\n", device->name);
2492 goto free_attr;
2495 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
2496 if (!srp_dev)
2497 goto free_attr;
2500 * Use the smallest page size supported by the HCA, down to a
2501 * minimum of 4096 bytes. We're unlikely to build large sglists
2502 * out of smaller entries.
2504 fmr_page_shift = max(12, ffs(dev_attr->page_size_cap) - 1);
2505 srp_dev->fmr_page_size = 1 << fmr_page_shift;
2506 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
2507 srp_dev->fmr_max_size = srp_dev->fmr_page_size * SRP_FMR_SIZE;
2509 INIT_LIST_HEAD(&srp_dev->dev_list);
2511 srp_dev->dev = device;
2512 srp_dev->pd = ib_alloc_pd(device);
2513 if (IS_ERR(srp_dev->pd))
2514 goto free_dev;
2516 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
2517 IB_ACCESS_LOCAL_WRITE |
2518 IB_ACCESS_REMOTE_READ |
2519 IB_ACCESS_REMOTE_WRITE);
2520 if (IS_ERR(srp_dev->mr))
2521 goto err_pd;
2523 for (max_pages_per_fmr = SRP_FMR_SIZE;
2524 max_pages_per_fmr >= SRP_FMR_MIN_SIZE;
2525 max_pages_per_fmr /= 2, srp_dev->fmr_max_size /= 2) {
2526 memset(&fmr_param, 0, sizeof fmr_param);
2527 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
2528 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
2529 fmr_param.cache = 1;
2530 fmr_param.max_pages_per_fmr = max_pages_per_fmr;
2531 fmr_param.page_shift = fmr_page_shift;
2532 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
2533 IB_ACCESS_REMOTE_WRITE |
2534 IB_ACCESS_REMOTE_READ);
2536 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
2537 if (!IS_ERR(srp_dev->fmr_pool))
2538 break;
2541 if (IS_ERR(srp_dev->fmr_pool))
2542 srp_dev->fmr_pool = NULL;
2544 if (device->node_type == RDMA_NODE_IB_SWITCH) {
2545 s = 0;
2546 e = 0;
2547 } else {
2548 s = 1;
2549 e = device->phys_port_cnt;
2552 for (p = s; p <= e; ++p) {
2553 host = srp_add_port(srp_dev, p);
2554 if (host)
2555 list_add_tail(&host->list, &srp_dev->dev_list);
2558 ib_set_client_data(device, &srp_client, srp_dev);
2560 goto free_attr;
2562 err_pd:
2563 ib_dealloc_pd(srp_dev->pd);
2565 free_dev:
2566 kfree(srp_dev);
2568 free_attr:
2569 kfree(dev_attr);
2572 static void srp_remove_one(struct ib_device *device)
2574 struct srp_device *srp_dev;
2575 struct srp_host *host, *tmp_host;
2576 struct srp_target_port *target;
2578 srp_dev = ib_get_client_data(device, &srp_client);
2579 if (!srp_dev)
2580 return;
2582 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
2583 device_unregister(&host->dev);
2585 * Wait for the sysfs entry to go away, so that no new
2586 * target ports can be created.
2588 wait_for_completion(&host->released);
2591 * Remove all target ports.
2593 spin_lock(&host->target_lock);
2594 list_for_each_entry(target, &host->target_list, list)
2595 srp_queue_remove_work(target);
2596 spin_unlock(&host->target_lock);
2599 * Wait for target port removal tasks.
2601 flush_workqueue(system_long_wq);
2603 kfree(host);
2606 if (srp_dev->fmr_pool)
2607 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2608 ib_dereg_mr(srp_dev->mr);
2609 ib_dealloc_pd(srp_dev->pd);
2611 kfree(srp_dev);
2614 static struct srp_function_template ib_srp_transport_functions = {
2615 .rport_delete = srp_rport_delete,
2618 static int __init srp_init_module(void)
2620 int ret;
2622 BUILD_BUG_ON(FIELD_SIZEOF(struct ib_wc, wr_id) < sizeof(void *));
2624 if (srp_sg_tablesize) {
2625 pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n");
2626 if (!cmd_sg_entries)
2627 cmd_sg_entries = srp_sg_tablesize;
2630 if (!cmd_sg_entries)
2631 cmd_sg_entries = SRP_DEF_SG_TABLESIZE;
2633 if (cmd_sg_entries > 255) {
2634 pr_warn("Clamping cmd_sg_entries to 255\n");
2635 cmd_sg_entries = 255;
2638 if (!indirect_sg_entries)
2639 indirect_sg_entries = cmd_sg_entries;
2640 else if (indirect_sg_entries < cmd_sg_entries) {
2641 pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n",
2642 cmd_sg_entries);
2643 indirect_sg_entries = cmd_sg_entries;
2646 ib_srp_transport_template =
2647 srp_attach_transport(&ib_srp_transport_functions);
2648 if (!ib_srp_transport_template)
2649 return -ENOMEM;
2651 ret = class_register(&srp_class);
2652 if (ret) {
2653 pr_err("couldn't register class infiniband_srp\n");
2654 srp_release_transport(ib_srp_transport_template);
2655 return ret;
2658 ib_sa_register_client(&srp_sa_client);
2660 ret = ib_register_client(&srp_client);
2661 if (ret) {
2662 pr_err("couldn't register IB client\n");
2663 srp_release_transport(ib_srp_transport_template);
2664 ib_sa_unregister_client(&srp_sa_client);
2665 class_unregister(&srp_class);
2666 return ret;
2669 return 0;
2672 static void __exit srp_cleanup_module(void)
2674 ib_unregister_client(&srp_client);
2675 ib_sa_unregister_client(&srp_sa_client);
2676 class_unregister(&srp_class);
2677 srp_release_transport(ib_srp_transport_template);
2680 module_init(srp_init_module);
2681 module_exit(srp_cleanup_module);