Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / drivers / infiniband / ulp / srp / ib_srp.c
blob529b6bcdca7a5267570a81c4b031a069b8692235
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/scsi_tcq.h>
50 #include <scsi/srp.h>
51 #include <scsi/scsi_transport_srp.h>
53 #include "ib_srp.h"
55 #define DRV_NAME "ib_srp"
56 #define PFX DRV_NAME ": "
57 #define DRV_VERSION "1.0"
58 #define DRV_RELDATE "July 1, 2013"
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 unsigned int srp_sg_tablesize;
66 static unsigned int cmd_sg_entries;
67 static unsigned int indirect_sg_entries;
68 static bool allow_ext_sg;
69 static int topspin_workarounds = 1;
71 module_param(srp_sg_tablesize, uint, 0444);
72 MODULE_PARM_DESC(srp_sg_tablesize, "Deprecated name for cmd_sg_entries");
74 module_param(cmd_sg_entries, uint, 0444);
75 MODULE_PARM_DESC(cmd_sg_entries,
76 "Default number of gather/scatter entries in the SRP command (default is 12, max 255)");
78 module_param(indirect_sg_entries, uint, 0444);
79 MODULE_PARM_DESC(indirect_sg_entries,
80 "Default max number of gather/scatter entries (default is 12, max is " __stringify(SCSI_MAX_SG_CHAIN_SEGMENTS) ")");
82 module_param(allow_ext_sg, bool, 0444);
83 MODULE_PARM_DESC(allow_ext_sg,
84 "Default behavior when there are more than cmd_sg_entries S/G entries after mapping; fails the request when false (default false)");
86 module_param(topspin_workarounds, int, 0444);
87 MODULE_PARM_DESC(topspin_workarounds,
88 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
90 static struct kernel_param_ops srp_tmo_ops;
92 static int srp_reconnect_delay = 10;
93 module_param_cb(reconnect_delay, &srp_tmo_ops, &srp_reconnect_delay,
94 S_IRUGO | S_IWUSR);
95 MODULE_PARM_DESC(reconnect_delay, "Time between successive reconnect attempts");
97 static int srp_fast_io_fail_tmo = 15;
98 module_param_cb(fast_io_fail_tmo, &srp_tmo_ops, &srp_fast_io_fail_tmo,
99 S_IRUGO | S_IWUSR);
100 MODULE_PARM_DESC(fast_io_fail_tmo,
101 "Number of seconds between the observation of a transport"
102 " layer error and failing all I/O. \"off\" means that this"
103 " functionality is disabled.");
105 static int srp_dev_loss_tmo = 600;
106 module_param_cb(dev_loss_tmo, &srp_tmo_ops, &srp_dev_loss_tmo,
107 S_IRUGO | S_IWUSR);
108 MODULE_PARM_DESC(dev_loss_tmo,
109 "Maximum number of seconds that the SRP transport should"
110 " insulate transport layer errors. After this time has been"
111 " exceeded the SCSI host is removed. Should be"
112 " between 1 and " __stringify(SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
113 " if fast_io_fail_tmo has not been set. \"off\" means that"
114 " this functionality is disabled.");
116 static void srp_add_one(struct ib_device *device);
117 static void srp_remove_one(struct ib_device *device);
118 static void srp_recv_completion(struct ib_cq *cq, void *target_ptr);
119 static void srp_send_completion(struct ib_cq *cq, void *target_ptr);
120 static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
122 static struct scsi_transport_template *ib_srp_transport_template;
124 static struct ib_client srp_client = {
125 .name = "srp",
126 .add = srp_add_one,
127 .remove = srp_remove_one
130 static struct ib_sa_client srp_sa_client;
132 static int srp_tmo_get(char *buffer, const struct kernel_param *kp)
134 int tmo = *(int *)kp->arg;
136 if (tmo >= 0)
137 return sprintf(buffer, "%d", tmo);
138 else
139 return sprintf(buffer, "off");
142 static int srp_tmo_set(const char *val, const struct kernel_param *kp)
144 int tmo, res;
146 if (strncmp(val, "off", 3) != 0) {
147 res = kstrtoint(val, 0, &tmo);
148 if (res)
149 goto out;
150 } else {
151 tmo = -1;
153 if (kp->arg == &srp_reconnect_delay)
154 res = srp_tmo_valid(tmo, srp_fast_io_fail_tmo,
155 srp_dev_loss_tmo);
156 else if (kp->arg == &srp_fast_io_fail_tmo)
157 res = srp_tmo_valid(srp_reconnect_delay, tmo, srp_dev_loss_tmo);
158 else
159 res = srp_tmo_valid(srp_reconnect_delay, srp_fast_io_fail_tmo,
160 tmo);
161 if (res)
162 goto out;
163 *(int *)kp->arg = tmo;
165 out:
166 return res;
169 static struct kernel_param_ops srp_tmo_ops = {
170 .get = srp_tmo_get,
171 .set = srp_tmo_set,
174 static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
176 return (struct srp_target_port *) host->hostdata;
179 static const char *srp_target_info(struct Scsi_Host *host)
181 return host_to_target(host)->target_name;
184 static int srp_target_is_topspin(struct srp_target_port *target)
186 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
187 static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
189 return topspin_workarounds &&
190 (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
191 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
194 static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
195 gfp_t gfp_mask,
196 enum dma_data_direction direction)
198 struct srp_iu *iu;
200 iu = kmalloc(sizeof *iu, gfp_mask);
201 if (!iu)
202 goto out;
204 iu->buf = kzalloc(size, gfp_mask);
205 if (!iu->buf)
206 goto out_free_iu;
208 iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
209 direction);
210 if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
211 goto out_free_buf;
213 iu->size = size;
214 iu->direction = direction;
216 return iu;
218 out_free_buf:
219 kfree(iu->buf);
220 out_free_iu:
221 kfree(iu);
222 out:
223 return NULL;
226 static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
228 if (!iu)
229 return;
231 ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
232 iu->direction);
233 kfree(iu->buf);
234 kfree(iu);
237 static void srp_qp_event(struct ib_event *event, void *context)
239 pr_debug("QP event %d\n", event->event);
242 static int srp_init_qp(struct srp_target_port *target,
243 struct ib_qp *qp)
245 struct ib_qp_attr *attr;
246 int ret;
248 attr = kmalloc(sizeof *attr, GFP_KERNEL);
249 if (!attr)
250 return -ENOMEM;
252 ret = ib_find_pkey(target->srp_host->srp_dev->dev,
253 target->srp_host->port,
254 be16_to_cpu(target->path.pkey),
255 &attr->pkey_index);
256 if (ret)
257 goto out;
259 attr->qp_state = IB_QPS_INIT;
260 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
261 IB_ACCESS_REMOTE_WRITE);
262 attr->port_num = target->srp_host->port;
264 ret = ib_modify_qp(qp, attr,
265 IB_QP_STATE |
266 IB_QP_PKEY_INDEX |
267 IB_QP_ACCESS_FLAGS |
268 IB_QP_PORT);
270 out:
271 kfree(attr);
272 return ret;
275 static int srp_new_cm_id(struct srp_target_port *target)
277 struct ib_cm_id *new_cm_id;
279 new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
280 srp_cm_handler, target);
281 if (IS_ERR(new_cm_id))
282 return PTR_ERR(new_cm_id);
284 if (target->cm_id)
285 ib_destroy_cm_id(target->cm_id);
286 target->cm_id = new_cm_id;
288 return 0;
291 static int srp_create_target_ib(struct srp_target_port *target)
293 struct ib_qp_init_attr *init_attr;
294 struct ib_cq *recv_cq, *send_cq;
295 struct ib_qp *qp;
296 int ret;
298 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
299 if (!init_attr)
300 return -ENOMEM;
302 recv_cq = ib_create_cq(target->srp_host->srp_dev->dev,
303 srp_recv_completion, NULL, target,
304 target->queue_size, target->comp_vector);
305 if (IS_ERR(recv_cq)) {
306 ret = PTR_ERR(recv_cq);
307 goto err;
310 send_cq = ib_create_cq(target->srp_host->srp_dev->dev,
311 srp_send_completion, NULL, target,
312 target->queue_size, target->comp_vector);
313 if (IS_ERR(send_cq)) {
314 ret = PTR_ERR(send_cq);
315 goto err_recv_cq;
318 ib_req_notify_cq(recv_cq, IB_CQ_NEXT_COMP);
320 init_attr->event_handler = srp_qp_event;
321 init_attr->cap.max_send_wr = target->queue_size;
322 init_attr->cap.max_recv_wr = target->queue_size;
323 init_attr->cap.max_recv_sge = 1;
324 init_attr->cap.max_send_sge = 1;
325 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
326 init_attr->qp_type = IB_QPT_RC;
327 init_attr->send_cq = send_cq;
328 init_attr->recv_cq = recv_cq;
330 qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr);
331 if (IS_ERR(qp)) {
332 ret = PTR_ERR(qp);
333 goto err_send_cq;
336 ret = srp_init_qp(target, qp);
337 if (ret)
338 goto err_qp;
340 if (target->qp)
341 ib_destroy_qp(target->qp);
342 if (target->recv_cq)
343 ib_destroy_cq(target->recv_cq);
344 if (target->send_cq)
345 ib_destroy_cq(target->send_cq);
347 target->qp = qp;
348 target->recv_cq = recv_cq;
349 target->send_cq = send_cq;
351 kfree(init_attr);
352 return 0;
354 err_qp:
355 ib_destroy_qp(qp);
357 err_send_cq:
358 ib_destroy_cq(send_cq);
360 err_recv_cq:
361 ib_destroy_cq(recv_cq);
363 err:
364 kfree(init_attr);
365 return ret;
369 * Note: this function may be called without srp_alloc_iu_bufs() having been
370 * invoked. Hence the target->[rt]x_ring checks.
372 static void srp_free_target_ib(struct srp_target_port *target)
374 int i;
376 ib_destroy_qp(target->qp);
377 ib_destroy_cq(target->send_cq);
378 ib_destroy_cq(target->recv_cq);
380 target->qp = NULL;
381 target->send_cq = target->recv_cq = NULL;
383 if (target->rx_ring) {
384 for (i = 0; i < target->queue_size; ++i)
385 srp_free_iu(target->srp_host, target->rx_ring[i]);
386 kfree(target->rx_ring);
387 target->rx_ring = NULL;
389 if (target->tx_ring) {
390 for (i = 0; i < target->queue_size; ++i)
391 srp_free_iu(target->srp_host, target->tx_ring[i]);
392 kfree(target->tx_ring);
393 target->tx_ring = NULL;
397 static void srp_path_rec_completion(int status,
398 struct ib_sa_path_rec *pathrec,
399 void *target_ptr)
401 struct srp_target_port *target = target_ptr;
403 target->status = status;
404 if (status)
405 shost_printk(KERN_ERR, target->scsi_host,
406 PFX "Got failed path rec status %d\n", status);
407 else
408 target->path = *pathrec;
409 complete(&target->done);
412 static int srp_lookup_path(struct srp_target_port *target)
414 target->path.numb_path = 1;
416 init_completion(&target->done);
418 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
419 target->srp_host->srp_dev->dev,
420 target->srp_host->port,
421 &target->path,
422 IB_SA_PATH_REC_SERVICE_ID |
423 IB_SA_PATH_REC_DGID |
424 IB_SA_PATH_REC_SGID |
425 IB_SA_PATH_REC_NUMB_PATH |
426 IB_SA_PATH_REC_PKEY,
427 SRP_PATH_REC_TIMEOUT_MS,
428 GFP_KERNEL,
429 srp_path_rec_completion,
430 target, &target->path_query);
431 if (target->path_query_id < 0)
432 return target->path_query_id;
434 wait_for_completion(&target->done);
436 if (target->status < 0)
437 shost_printk(KERN_WARNING, target->scsi_host,
438 PFX "Path record query failed\n");
440 return target->status;
443 static int srp_send_req(struct srp_target_port *target)
445 struct {
446 struct ib_cm_req_param param;
447 struct srp_login_req priv;
448 } *req = NULL;
449 int status;
451 req = kzalloc(sizeof *req, GFP_KERNEL);
452 if (!req)
453 return -ENOMEM;
455 req->param.primary_path = &target->path;
456 req->param.alternate_path = NULL;
457 req->param.service_id = target->service_id;
458 req->param.qp_num = target->qp->qp_num;
459 req->param.qp_type = target->qp->qp_type;
460 req->param.private_data = &req->priv;
461 req->param.private_data_len = sizeof req->priv;
462 req->param.flow_control = 1;
464 get_random_bytes(&req->param.starting_psn, 4);
465 req->param.starting_psn &= 0xffffff;
468 * Pick some arbitrary defaults here; we could make these
469 * module parameters if anyone cared about setting them.
471 req->param.responder_resources = 4;
472 req->param.remote_cm_response_timeout = 20;
473 req->param.local_cm_response_timeout = 20;
474 req->param.retry_count = target->tl_retry_count;
475 req->param.rnr_retry_count = 7;
476 req->param.max_cm_retries = 15;
478 req->priv.opcode = SRP_LOGIN_REQ;
479 req->priv.tag = 0;
480 req->priv.req_it_iu_len = cpu_to_be32(target->max_iu_len);
481 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
482 SRP_BUF_FORMAT_INDIRECT);
484 * In the published SRP specification (draft rev. 16a), the
485 * port identifier format is 8 bytes of ID extension followed
486 * by 8 bytes of GUID. Older drafts put the two halves in the
487 * opposite order, so that the GUID comes first.
489 * Targets conforming to these obsolete drafts can be
490 * recognized by the I/O Class they report.
492 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
493 memcpy(req->priv.initiator_port_id,
494 &target->path.sgid.global.interface_id, 8);
495 memcpy(req->priv.initiator_port_id + 8,
496 &target->initiator_ext, 8);
497 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
498 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
499 } else {
500 memcpy(req->priv.initiator_port_id,
501 &target->initiator_ext, 8);
502 memcpy(req->priv.initiator_port_id + 8,
503 &target->path.sgid.global.interface_id, 8);
504 memcpy(req->priv.target_port_id, &target->id_ext, 8);
505 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
509 * Topspin/Cisco SRP targets will reject our login unless we
510 * zero out the first 8 bytes of our initiator port ID and set
511 * the second 8 bytes to the local node GUID.
513 if (srp_target_is_topspin(target)) {
514 shost_printk(KERN_DEBUG, target->scsi_host,
515 PFX "Topspin/Cisco initiator port ID workaround "
516 "activated for target GUID %016llx\n",
517 (unsigned long long) be64_to_cpu(target->ioc_guid));
518 memset(req->priv.initiator_port_id, 0, 8);
519 memcpy(req->priv.initiator_port_id + 8,
520 &target->srp_host->srp_dev->dev->node_guid, 8);
523 status = ib_send_cm_req(target->cm_id, &req->param);
525 kfree(req);
527 return status;
530 static bool srp_queue_remove_work(struct srp_target_port *target)
532 bool changed = false;
534 spin_lock_irq(&target->lock);
535 if (target->state != SRP_TARGET_REMOVED) {
536 target->state = SRP_TARGET_REMOVED;
537 changed = true;
539 spin_unlock_irq(&target->lock);
541 if (changed)
542 queue_work(system_long_wq, &target->remove_work);
544 return changed;
547 static bool srp_change_conn_state(struct srp_target_port *target,
548 bool connected)
550 bool changed = false;
552 spin_lock_irq(&target->lock);
553 if (target->connected != connected) {
554 target->connected = connected;
555 changed = true;
557 spin_unlock_irq(&target->lock);
559 return changed;
562 static void srp_disconnect_target(struct srp_target_port *target)
564 if (srp_change_conn_state(target, false)) {
565 /* XXX should send SRP_I_LOGOUT request */
567 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
568 shost_printk(KERN_DEBUG, target->scsi_host,
569 PFX "Sending CM DREQ failed\n");
574 static void srp_free_req_data(struct srp_target_port *target)
576 struct ib_device *ibdev = target->srp_host->srp_dev->dev;
577 struct srp_request *req;
578 int i;
580 if (!target->req_ring)
581 return;
583 for (i = 0; i < target->req_ring_size; ++i) {
584 req = &target->req_ring[i];
585 kfree(req->fmr_list);
586 kfree(req->map_page);
587 if (req->indirect_dma_addr) {
588 ib_dma_unmap_single(ibdev, req->indirect_dma_addr,
589 target->indirect_size,
590 DMA_TO_DEVICE);
592 kfree(req->indirect_desc);
595 kfree(target->req_ring);
596 target->req_ring = NULL;
599 static int srp_alloc_req_data(struct srp_target_port *target)
601 struct srp_device *srp_dev = target->srp_host->srp_dev;
602 struct ib_device *ibdev = srp_dev->dev;
603 struct srp_request *req;
604 dma_addr_t dma_addr;
605 int i, ret = -ENOMEM;
607 INIT_LIST_HEAD(&target->free_reqs);
609 target->req_ring = kzalloc(target->req_ring_size *
610 sizeof(*target->req_ring), GFP_KERNEL);
611 if (!target->req_ring)
612 goto out;
614 for (i = 0; i < target->req_ring_size; ++i) {
615 req = &target->req_ring[i];
616 req->fmr_list = kmalloc(target->cmd_sg_cnt * sizeof(void *),
617 GFP_KERNEL);
618 req->map_page = kmalloc(SRP_FMR_SIZE * sizeof(void *),
619 GFP_KERNEL);
620 req->indirect_desc = kmalloc(target->indirect_size, GFP_KERNEL);
621 if (!req->fmr_list || !req->map_page || !req->indirect_desc)
622 goto out;
624 dma_addr = ib_dma_map_single(ibdev, req->indirect_desc,
625 target->indirect_size,
626 DMA_TO_DEVICE);
627 if (ib_dma_mapping_error(ibdev, dma_addr))
628 goto out;
630 req->indirect_dma_addr = dma_addr;
631 req->index = i;
632 list_add_tail(&req->list, &target->free_reqs);
634 ret = 0;
636 out:
637 return ret;
641 * srp_del_scsi_host_attr() - Remove attributes defined in the host template.
642 * @shost: SCSI host whose attributes to remove from sysfs.
644 * Note: Any attributes defined in the host template and that did not exist
645 * before invocation of this function will be ignored.
647 static void srp_del_scsi_host_attr(struct Scsi_Host *shost)
649 struct device_attribute **attr;
651 for (attr = shost->hostt->shost_attrs; attr && *attr; ++attr)
652 device_remove_file(&shost->shost_dev, *attr);
655 static void srp_remove_target(struct srp_target_port *target)
657 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
659 srp_del_scsi_host_attr(target->scsi_host);
660 srp_rport_get(target->rport);
661 srp_remove_host(target->scsi_host);
662 scsi_remove_host(target->scsi_host);
663 srp_stop_rport_timers(target->rport);
664 srp_disconnect_target(target);
665 ib_destroy_cm_id(target->cm_id);
666 srp_free_target_ib(target);
667 cancel_work_sync(&target->tl_err_work);
668 srp_rport_put(target->rport);
669 srp_free_req_data(target);
671 spin_lock(&target->srp_host->target_lock);
672 list_del(&target->list);
673 spin_unlock(&target->srp_host->target_lock);
675 scsi_host_put(target->scsi_host);
678 static void srp_remove_work(struct work_struct *work)
680 struct srp_target_port *target =
681 container_of(work, struct srp_target_port, remove_work);
683 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
685 srp_remove_target(target);
688 static void srp_rport_delete(struct srp_rport *rport)
690 struct srp_target_port *target = rport->lld_data;
692 srp_queue_remove_work(target);
695 static int srp_connect_target(struct srp_target_port *target)
697 int retries = 3;
698 int ret;
700 WARN_ON_ONCE(target->connected);
702 target->qp_in_error = false;
704 ret = srp_lookup_path(target);
705 if (ret)
706 return ret;
708 while (1) {
709 init_completion(&target->done);
710 ret = srp_send_req(target);
711 if (ret)
712 return ret;
713 wait_for_completion(&target->done);
716 * The CM event handling code will set status to
717 * SRP_PORT_REDIRECT if we get a port redirect REJ
718 * back, or SRP_DLID_REDIRECT if we get a lid/qp
719 * redirect REJ back.
721 switch (target->status) {
722 case 0:
723 srp_change_conn_state(target, true);
724 return 0;
726 case SRP_PORT_REDIRECT:
727 ret = srp_lookup_path(target);
728 if (ret)
729 return ret;
730 break;
732 case SRP_DLID_REDIRECT:
733 break;
735 case SRP_STALE_CONN:
736 /* Our current CM id was stale, and is now in timewait.
737 * Try to reconnect with a new one.
739 if (!retries-- || srp_new_cm_id(target)) {
740 shost_printk(KERN_ERR, target->scsi_host, PFX
741 "giving up on stale connection\n");
742 target->status = -ECONNRESET;
743 return target->status;
746 shost_printk(KERN_ERR, target->scsi_host, PFX
747 "retrying stale connection\n");
748 break;
750 default:
751 return target->status;
756 static void srp_unmap_data(struct scsi_cmnd *scmnd,
757 struct srp_target_port *target,
758 struct srp_request *req)
760 struct ib_device *ibdev = target->srp_host->srp_dev->dev;
761 struct ib_pool_fmr **pfmr;
763 if (!scsi_sglist(scmnd) ||
764 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
765 scmnd->sc_data_direction != DMA_FROM_DEVICE))
766 return;
768 pfmr = req->fmr_list;
769 while (req->nfmr--)
770 ib_fmr_pool_unmap(*pfmr++);
772 ib_dma_unmap_sg(ibdev, scsi_sglist(scmnd), scsi_sg_count(scmnd),
773 scmnd->sc_data_direction);
777 * srp_claim_req - Take ownership of the scmnd associated with a request.
778 * @target: SRP target port.
779 * @req: SRP request.
780 * @scmnd: If NULL, take ownership of @req->scmnd. If not NULL, only take
781 * ownership of @req->scmnd if it equals @scmnd.
783 * Return value:
784 * Either NULL or a pointer to the SCSI command the caller became owner of.
786 static struct scsi_cmnd *srp_claim_req(struct srp_target_port *target,
787 struct srp_request *req,
788 struct scsi_cmnd *scmnd)
790 unsigned long flags;
792 spin_lock_irqsave(&target->lock, flags);
793 if (!scmnd) {
794 scmnd = req->scmnd;
795 req->scmnd = NULL;
796 } else if (req->scmnd == scmnd) {
797 req->scmnd = NULL;
798 } else {
799 scmnd = NULL;
801 spin_unlock_irqrestore(&target->lock, flags);
803 return scmnd;
807 * srp_free_req() - Unmap data and add request to the free request list.
809 static void srp_free_req(struct srp_target_port *target,
810 struct srp_request *req, struct scsi_cmnd *scmnd,
811 s32 req_lim_delta)
813 unsigned long flags;
815 srp_unmap_data(scmnd, target, req);
817 spin_lock_irqsave(&target->lock, flags);
818 target->req_lim += req_lim_delta;
819 list_add_tail(&req->list, &target->free_reqs);
820 spin_unlock_irqrestore(&target->lock, flags);
823 static void srp_finish_req(struct srp_target_port *target,
824 struct srp_request *req, int result)
826 struct scsi_cmnd *scmnd = srp_claim_req(target, req, NULL);
828 if (scmnd) {
829 srp_free_req(target, req, scmnd, 0);
830 scmnd->result = result;
831 scmnd->scsi_done(scmnd);
835 static void srp_terminate_io(struct srp_rport *rport)
837 struct srp_target_port *target = rport->lld_data;
838 int i;
840 for (i = 0; i < target->req_ring_size; ++i) {
841 struct srp_request *req = &target->req_ring[i];
842 srp_finish_req(target, req, DID_TRANSPORT_FAILFAST << 16);
847 * It is up to the caller to ensure that srp_rport_reconnect() calls are
848 * serialized and that no concurrent srp_queuecommand(), srp_abort(),
849 * srp_reset_device() or srp_reset_host() calls will occur while this function
850 * is in progress. One way to realize that is not to call this function
851 * directly but to call srp_reconnect_rport() instead since that last function
852 * serializes calls of this function via rport->mutex and also blocks
853 * srp_queuecommand() calls before invoking this function.
855 static int srp_rport_reconnect(struct srp_rport *rport)
857 struct srp_target_port *target = rport->lld_data;
858 int i, ret;
860 srp_disconnect_target(target);
862 * Now get a new local CM ID so that we avoid confusing the target in
863 * case things are really fouled up. Doing so also ensures that all CM
864 * callbacks will have finished before a new QP is allocated.
866 ret = srp_new_cm_id(target);
868 * Whether or not creating a new CM ID succeeded, create a new
869 * QP. This guarantees that all completion callback function
870 * invocations have finished before request resetting starts.
872 if (ret == 0)
873 ret = srp_create_target_ib(target);
874 else
875 srp_create_target_ib(target);
877 for (i = 0; i < target->req_ring_size; ++i) {
878 struct srp_request *req = &target->req_ring[i];
879 srp_finish_req(target, req, DID_RESET << 16);
882 INIT_LIST_HEAD(&target->free_tx);
883 for (i = 0; i < target->queue_size; ++i)
884 list_add(&target->tx_ring[i]->list, &target->free_tx);
886 if (ret == 0)
887 ret = srp_connect_target(target);
889 if (ret == 0)
890 shost_printk(KERN_INFO, target->scsi_host,
891 PFX "reconnect succeeded\n");
893 return ret;
896 static void srp_map_desc(struct srp_map_state *state, dma_addr_t dma_addr,
897 unsigned int dma_len, u32 rkey)
899 struct srp_direct_buf *desc = state->desc;
901 desc->va = cpu_to_be64(dma_addr);
902 desc->key = cpu_to_be32(rkey);
903 desc->len = cpu_to_be32(dma_len);
905 state->total_len += dma_len;
906 state->desc++;
907 state->ndesc++;
910 static int srp_map_finish_fmr(struct srp_map_state *state,
911 struct srp_target_port *target)
913 struct srp_device *dev = target->srp_host->srp_dev;
914 struct ib_pool_fmr *fmr;
915 u64 io_addr = 0;
917 if (!state->npages)
918 return 0;
920 if (state->npages == 1) {
921 srp_map_desc(state, state->base_dma_addr, state->fmr_len,
922 target->rkey);
923 state->npages = state->fmr_len = 0;
924 return 0;
927 fmr = ib_fmr_pool_map_phys(dev->fmr_pool, state->pages,
928 state->npages, io_addr);
929 if (IS_ERR(fmr))
930 return PTR_ERR(fmr);
932 *state->next_fmr++ = fmr;
933 state->nfmr++;
935 srp_map_desc(state, 0, state->fmr_len, fmr->fmr->rkey);
936 state->npages = state->fmr_len = 0;
937 return 0;
940 static void srp_map_update_start(struct srp_map_state *state,
941 struct scatterlist *sg, int sg_index,
942 dma_addr_t dma_addr)
944 state->unmapped_sg = sg;
945 state->unmapped_index = sg_index;
946 state->unmapped_addr = dma_addr;
949 static int srp_map_sg_entry(struct srp_map_state *state,
950 struct srp_target_port *target,
951 struct scatterlist *sg, int sg_index,
952 int use_fmr)
954 struct srp_device *dev = target->srp_host->srp_dev;
955 struct ib_device *ibdev = dev->dev;
956 dma_addr_t dma_addr = ib_sg_dma_address(ibdev, sg);
957 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
958 unsigned int len;
959 int ret;
961 if (!dma_len)
962 return 0;
964 if (use_fmr == SRP_MAP_NO_FMR) {
965 /* Once we're in direct map mode for a request, we don't
966 * go back to FMR mode, so no need to update anything
967 * other than the descriptor.
969 srp_map_desc(state, dma_addr, dma_len, target->rkey);
970 return 0;
973 /* If we start at an offset into the FMR page, don't merge into
974 * the current FMR. Finish it out, and use the kernel's MR for this
975 * sg entry. This is to avoid potential bugs on some SRP targets
976 * that were never quite defined, but went away when the initiator
977 * avoided using FMR on such page fragments.
979 if (dma_addr & ~dev->fmr_page_mask || dma_len > dev->fmr_max_size) {
980 ret = srp_map_finish_fmr(state, target);
981 if (ret)
982 return ret;
984 srp_map_desc(state, dma_addr, dma_len, target->rkey);
985 srp_map_update_start(state, NULL, 0, 0);
986 return 0;
989 /* If this is the first sg to go into the FMR, save our position.
990 * We need to know the first unmapped entry, its index, and the
991 * first unmapped address within that entry to be able to restart
992 * mapping after an error.
994 if (!state->unmapped_sg)
995 srp_map_update_start(state, sg, sg_index, dma_addr);
997 while (dma_len) {
998 if (state->npages == SRP_FMR_SIZE) {
999 ret = srp_map_finish_fmr(state, target);
1000 if (ret)
1001 return ret;
1003 srp_map_update_start(state, sg, sg_index, dma_addr);
1006 len = min_t(unsigned int, dma_len, dev->fmr_page_size);
1008 if (!state->npages)
1009 state->base_dma_addr = dma_addr;
1010 state->pages[state->npages++] = dma_addr;
1011 state->fmr_len += len;
1012 dma_addr += len;
1013 dma_len -= len;
1016 /* If the last entry of the FMR wasn't a full page, then we need to
1017 * close it out and start a new one -- we can only merge at page
1018 * boundries.
1020 ret = 0;
1021 if (len != dev->fmr_page_size) {
1022 ret = srp_map_finish_fmr(state, target);
1023 if (!ret)
1024 srp_map_update_start(state, NULL, 0, 0);
1026 return ret;
1029 static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
1030 struct srp_request *req)
1032 struct scatterlist *scat, *sg;
1033 struct srp_cmd *cmd = req->cmd->buf;
1034 int i, len, nents, count, use_fmr;
1035 struct srp_device *dev;
1036 struct ib_device *ibdev;
1037 struct srp_map_state state;
1038 struct srp_indirect_buf *indirect_hdr;
1039 u32 table_len;
1040 u8 fmt;
1042 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
1043 return sizeof (struct srp_cmd);
1045 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
1046 scmnd->sc_data_direction != DMA_TO_DEVICE) {
1047 shost_printk(KERN_WARNING, target->scsi_host,
1048 PFX "Unhandled data direction %d\n",
1049 scmnd->sc_data_direction);
1050 return -EINVAL;
1053 nents = scsi_sg_count(scmnd);
1054 scat = scsi_sglist(scmnd);
1056 dev = target->srp_host->srp_dev;
1057 ibdev = dev->dev;
1059 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
1060 if (unlikely(count == 0))
1061 return -EIO;
1063 fmt = SRP_DATA_DESC_DIRECT;
1064 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
1066 if (count == 1) {
1068 * The midlayer only generated a single gather/scatter
1069 * entry, or DMA mapping coalesced everything to a
1070 * single entry. So a direct descriptor along with
1071 * the DMA MR suffices.
1073 struct srp_direct_buf *buf = (void *) cmd->add_data;
1075 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
1076 buf->key = cpu_to_be32(target->rkey);
1077 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
1079 req->nfmr = 0;
1080 goto map_complete;
1083 /* We have more than one scatter/gather entry, so build our indirect
1084 * descriptor table, trying to merge as many entries with FMR as we
1085 * can.
1087 indirect_hdr = (void *) cmd->add_data;
1089 ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr,
1090 target->indirect_size, DMA_TO_DEVICE);
1092 memset(&state, 0, sizeof(state));
1093 state.desc = req->indirect_desc;
1094 state.pages = req->map_page;
1095 state.next_fmr = req->fmr_list;
1097 use_fmr = dev->fmr_pool ? SRP_MAP_ALLOW_FMR : SRP_MAP_NO_FMR;
1099 for_each_sg(scat, sg, count, i) {
1100 if (srp_map_sg_entry(&state, target, sg, i, use_fmr)) {
1101 /* FMR mapping failed, so backtrack to the first
1102 * unmapped entry and continue on without using FMR.
1104 dma_addr_t dma_addr;
1105 unsigned int dma_len;
1107 backtrack:
1108 sg = state.unmapped_sg;
1109 i = state.unmapped_index;
1111 dma_addr = ib_sg_dma_address(ibdev, sg);
1112 dma_len = ib_sg_dma_len(ibdev, sg);
1113 dma_len -= (state.unmapped_addr - dma_addr);
1114 dma_addr = state.unmapped_addr;
1115 use_fmr = SRP_MAP_NO_FMR;
1116 srp_map_desc(&state, dma_addr, dma_len, target->rkey);
1120 if (use_fmr == SRP_MAP_ALLOW_FMR && srp_map_finish_fmr(&state, target))
1121 goto backtrack;
1123 /* We've mapped the request, now pull as much of the indirect
1124 * descriptor table as we can into the command buffer. If this
1125 * target is not using an external indirect table, we are
1126 * guaranteed to fit into the command, as the SCSI layer won't
1127 * give us more S/G entries than we allow.
1129 req->nfmr = state.nfmr;
1130 if (state.ndesc == 1) {
1131 /* FMR mapping was able to collapse this to one entry,
1132 * so use a direct descriptor.
1134 struct srp_direct_buf *buf = (void *) cmd->add_data;
1136 *buf = req->indirect_desc[0];
1137 goto map_complete;
1140 if (unlikely(target->cmd_sg_cnt < state.ndesc &&
1141 !target->allow_ext_sg)) {
1142 shost_printk(KERN_ERR, target->scsi_host,
1143 "Could not fit S/G list into SRP_CMD\n");
1144 return -EIO;
1147 count = min(state.ndesc, target->cmd_sg_cnt);
1148 table_len = state.ndesc * sizeof (struct srp_direct_buf);
1150 fmt = SRP_DATA_DESC_INDIRECT;
1151 len = sizeof(struct srp_cmd) + sizeof (struct srp_indirect_buf);
1152 len += count * sizeof (struct srp_direct_buf);
1154 memcpy(indirect_hdr->desc_list, req->indirect_desc,
1155 count * sizeof (struct srp_direct_buf));
1157 indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr);
1158 indirect_hdr->table_desc.key = cpu_to_be32(target->rkey);
1159 indirect_hdr->table_desc.len = cpu_to_be32(table_len);
1160 indirect_hdr->len = cpu_to_be32(state.total_len);
1162 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
1163 cmd->data_out_desc_cnt = count;
1164 else
1165 cmd->data_in_desc_cnt = count;
1167 ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len,
1168 DMA_TO_DEVICE);
1170 map_complete:
1171 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
1172 cmd->buf_fmt = fmt << 4;
1173 else
1174 cmd->buf_fmt = fmt;
1176 return len;
1180 * Return an IU and possible credit to the free pool
1182 static void srp_put_tx_iu(struct srp_target_port *target, struct srp_iu *iu,
1183 enum srp_iu_type iu_type)
1185 unsigned long flags;
1187 spin_lock_irqsave(&target->lock, flags);
1188 list_add(&iu->list, &target->free_tx);
1189 if (iu_type != SRP_IU_RSP)
1190 ++target->req_lim;
1191 spin_unlock_irqrestore(&target->lock, flags);
1195 * Must be called with target->lock held to protect req_lim and free_tx.
1196 * If IU is not sent, it must be returned using srp_put_tx_iu().
1198 * Note:
1199 * An upper limit for the number of allocated information units for each
1200 * request type is:
1201 * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues
1202 * more than Scsi_Host.can_queue requests.
1203 * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE.
1204 * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than
1205 * one unanswered SRP request to an initiator.
1207 static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
1208 enum srp_iu_type iu_type)
1210 s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE;
1211 struct srp_iu *iu;
1213 srp_send_completion(target->send_cq, target);
1215 if (list_empty(&target->free_tx))
1216 return NULL;
1218 /* Initiator responses to target requests do not consume credits */
1219 if (iu_type != SRP_IU_RSP) {
1220 if (target->req_lim <= rsv) {
1221 ++target->zero_req_lim;
1222 return NULL;
1225 --target->req_lim;
1228 iu = list_first_entry(&target->free_tx, struct srp_iu, list);
1229 list_del(&iu->list);
1230 return iu;
1233 static int srp_post_send(struct srp_target_port *target,
1234 struct srp_iu *iu, int len)
1236 struct ib_sge list;
1237 struct ib_send_wr wr, *bad_wr;
1239 list.addr = iu->dma;
1240 list.length = len;
1241 list.lkey = target->lkey;
1243 wr.next = NULL;
1244 wr.wr_id = (uintptr_t) iu;
1245 wr.sg_list = &list;
1246 wr.num_sge = 1;
1247 wr.opcode = IB_WR_SEND;
1248 wr.send_flags = IB_SEND_SIGNALED;
1250 return ib_post_send(target->qp, &wr, &bad_wr);
1253 static int srp_post_recv(struct srp_target_port *target, struct srp_iu *iu)
1255 struct ib_recv_wr wr, *bad_wr;
1256 struct ib_sge list;
1258 list.addr = iu->dma;
1259 list.length = iu->size;
1260 list.lkey = target->lkey;
1262 wr.next = NULL;
1263 wr.wr_id = (uintptr_t) iu;
1264 wr.sg_list = &list;
1265 wr.num_sge = 1;
1267 return ib_post_recv(target->qp, &wr, &bad_wr);
1270 static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
1272 struct srp_request *req;
1273 struct scsi_cmnd *scmnd;
1274 unsigned long flags;
1276 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
1277 spin_lock_irqsave(&target->lock, flags);
1278 target->req_lim += be32_to_cpu(rsp->req_lim_delta);
1279 spin_unlock_irqrestore(&target->lock, flags);
1281 target->tsk_mgmt_status = -1;
1282 if (be32_to_cpu(rsp->resp_data_len) >= 4)
1283 target->tsk_mgmt_status = rsp->data[3];
1284 complete(&target->tsk_mgmt_done);
1285 } else {
1286 req = &target->req_ring[rsp->tag];
1287 scmnd = srp_claim_req(target, req, NULL);
1288 if (!scmnd) {
1289 shost_printk(KERN_ERR, target->scsi_host,
1290 "Null scmnd for RSP w/tag %016llx\n",
1291 (unsigned long long) rsp->tag);
1293 spin_lock_irqsave(&target->lock, flags);
1294 target->req_lim += be32_to_cpu(rsp->req_lim_delta);
1295 spin_unlock_irqrestore(&target->lock, flags);
1297 return;
1299 scmnd->result = rsp->status;
1301 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
1302 memcpy(scmnd->sense_buffer, rsp->data +
1303 be32_to_cpu(rsp->resp_data_len),
1304 min_t(int, be32_to_cpu(rsp->sense_data_len),
1305 SCSI_SENSE_BUFFERSIZE));
1308 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
1309 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
1310 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
1311 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
1313 srp_free_req(target, req, scmnd,
1314 be32_to_cpu(rsp->req_lim_delta));
1316 scmnd->host_scribble = NULL;
1317 scmnd->scsi_done(scmnd);
1321 static int srp_response_common(struct srp_target_port *target, s32 req_delta,
1322 void *rsp, int len)
1324 struct ib_device *dev = target->srp_host->srp_dev->dev;
1325 unsigned long flags;
1326 struct srp_iu *iu;
1327 int err;
1329 spin_lock_irqsave(&target->lock, flags);
1330 target->req_lim += req_delta;
1331 iu = __srp_get_tx_iu(target, SRP_IU_RSP);
1332 spin_unlock_irqrestore(&target->lock, flags);
1334 if (!iu) {
1335 shost_printk(KERN_ERR, target->scsi_host, PFX
1336 "no IU available to send response\n");
1337 return 1;
1340 ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE);
1341 memcpy(iu->buf, rsp, len);
1342 ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE);
1344 err = srp_post_send(target, iu, len);
1345 if (err) {
1346 shost_printk(KERN_ERR, target->scsi_host, PFX
1347 "unable to post response: %d\n", err);
1348 srp_put_tx_iu(target, iu, SRP_IU_RSP);
1351 return err;
1354 static void srp_process_cred_req(struct srp_target_port *target,
1355 struct srp_cred_req *req)
1357 struct srp_cred_rsp rsp = {
1358 .opcode = SRP_CRED_RSP,
1359 .tag = req->tag,
1361 s32 delta = be32_to_cpu(req->req_lim_delta);
1363 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1364 shost_printk(KERN_ERR, target->scsi_host, PFX
1365 "problems processing SRP_CRED_REQ\n");
1368 static void srp_process_aer_req(struct srp_target_port *target,
1369 struct srp_aer_req *req)
1371 struct srp_aer_rsp rsp = {
1372 .opcode = SRP_AER_RSP,
1373 .tag = req->tag,
1375 s32 delta = be32_to_cpu(req->req_lim_delta);
1377 shost_printk(KERN_ERR, target->scsi_host, PFX
1378 "ignoring AER for LUN %llu\n", be64_to_cpu(req->lun));
1380 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1381 shost_printk(KERN_ERR, target->scsi_host, PFX
1382 "problems processing SRP_AER_REQ\n");
1385 static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
1387 struct ib_device *dev = target->srp_host->srp_dev->dev;
1388 struct srp_iu *iu = (struct srp_iu *) (uintptr_t) wc->wr_id;
1389 int res;
1390 u8 opcode;
1392 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
1393 DMA_FROM_DEVICE);
1395 opcode = *(u8 *) iu->buf;
1397 if (0) {
1398 shost_printk(KERN_ERR, target->scsi_host,
1399 PFX "recv completion, opcode 0x%02x\n", opcode);
1400 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1,
1401 iu->buf, wc->byte_len, true);
1404 switch (opcode) {
1405 case SRP_RSP:
1406 srp_process_rsp(target, iu->buf);
1407 break;
1409 case SRP_CRED_REQ:
1410 srp_process_cred_req(target, iu->buf);
1411 break;
1413 case SRP_AER_REQ:
1414 srp_process_aer_req(target, iu->buf);
1415 break;
1417 case SRP_T_LOGOUT:
1418 /* XXX Handle target logout */
1419 shost_printk(KERN_WARNING, target->scsi_host,
1420 PFX "Got target logout request\n");
1421 break;
1423 default:
1424 shost_printk(KERN_WARNING, target->scsi_host,
1425 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
1426 break;
1429 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
1430 DMA_FROM_DEVICE);
1432 res = srp_post_recv(target, iu);
1433 if (res != 0)
1434 shost_printk(KERN_ERR, target->scsi_host,
1435 PFX "Recv failed with error code %d\n", res);
1439 * srp_tl_err_work() - handle a transport layer error
1441 * Note: This function may get invoked before the rport has been created,
1442 * hence the target->rport test.
1444 static void srp_tl_err_work(struct work_struct *work)
1446 struct srp_target_port *target;
1448 target = container_of(work, struct srp_target_port, tl_err_work);
1449 if (target->rport)
1450 srp_start_tl_fail_timers(target->rport);
1453 static void srp_handle_qp_err(enum ib_wc_status wc_status, bool send_err,
1454 struct srp_target_port *target)
1456 if (target->connected && !target->qp_in_error) {
1457 shost_printk(KERN_ERR, target->scsi_host,
1458 PFX "failed %s status %d\n",
1459 send_err ? "send" : "receive",
1460 wc_status);
1461 queue_work(system_long_wq, &target->tl_err_work);
1463 target->qp_in_error = true;
1466 static void srp_recv_completion(struct ib_cq *cq, void *target_ptr)
1468 struct srp_target_port *target = target_ptr;
1469 struct ib_wc wc;
1471 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1472 while (ib_poll_cq(cq, 1, &wc) > 0) {
1473 if (likely(wc.status == IB_WC_SUCCESS)) {
1474 srp_handle_recv(target, &wc);
1475 } else {
1476 srp_handle_qp_err(wc.status, false, target);
1481 static void srp_send_completion(struct ib_cq *cq, void *target_ptr)
1483 struct srp_target_port *target = target_ptr;
1484 struct ib_wc wc;
1485 struct srp_iu *iu;
1487 while (ib_poll_cq(cq, 1, &wc) > 0) {
1488 if (likely(wc.status == IB_WC_SUCCESS)) {
1489 iu = (struct srp_iu *) (uintptr_t) wc.wr_id;
1490 list_add(&iu->list, &target->free_tx);
1491 } else {
1492 srp_handle_qp_err(wc.status, true, target);
1497 static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd)
1499 struct srp_target_port *target = host_to_target(shost);
1500 struct srp_rport *rport = target->rport;
1501 struct srp_request *req;
1502 struct srp_iu *iu;
1503 struct srp_cmd *cmd;
1504 struct ib_device *dev;
1505 unsigned long flags;
1506 int len, result;
1507 const bool in_scsi_eh = !in_interrupt() && current == shost->ehandler;
1510 * The SCSI EH thread is the only context from which srp_queuecommand()
1511 * can get invoked for blocked devices (SDEV_BLOCK /
1512 * SDEV_CREATED_BLOCK). Avoid racing with srp_reconnect_rport() by
1513 * locking the rport mutex if invoked from inside the SCSI EH.
1515 if (in_scsi_eh)
1516 mutex_lock(&rport->mutex);
1518 result = srp_chkready(target->rport);
1519 if (unlikely(result)) {
1520 scmnd->result = result;
1521 scmnd->scsi_done(scmnd);
1522 goto unlock_rport;
1525 spin_lock_irqsave(&target->lock, flags);
1526 iu = __srp_get_tx_iu(target, SRP_IU_CMD);
1527 if (!iu)
1528 goto err_unlock;
1530 req = list_first_entry(&target->free_reqs, struct srp_request, list);
1531 list_del(&req->list);
1532 spin_unlock_irqrestore(&target->lock, flags);
1534 dev = target->srp_host->srp_dev->dev;
1535 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_iu_len,
1536 DMA_TO_DEVICE);
1538 scmnd->result = 0;
1539 scmnd->host_scribble = (void *) req;
1541 cmd = iu->buf;
1542 memset(cmd, 0, sizeof *cmd);
1544 cmd->opcode = SRP_CMD;
1545 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
1546 cmd->tag = req->index;
1547 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1549 req->scmnd = scmnd;
1550 req->cmd = iu;
1552 len = srp_map_data(scmnd, target, req);
1553 if (len < 0) {
1554 shost_printk(KERN_ERR, target->scsi_host,
1555 PFX "Failed to map data\n");
1556 goto err_iu;
1559 ib_dma_sync_single_for_device(dev, iu->dma, target->max_iu_len,
1560 DMA_TO_DEVICE);
1562 if (srp_post_send(target, iu, len)) {
1563 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
1564 goto err_unmap;
1567 unlock_rport:
1568 if (in_scsi_eh)
1569 mutex_unlock(&rport->mutex);
1571 return 0;
1573 err_unmap:
1574 srp_unmap_data(scmnd, target, req);
1576 err_iu:
1577 srp_put_tx_iu(target, iu, SRP_IU_CMD);
1579 spin_lock_irqsave(&target->lock, flags);
1580 list_add(&req->list, &target->free_reqs);
1582 err_unlock:
1583 spin_unlock_irqrestore(&target->lock, flags);
1585 if (in_scsi_eh)
1586 mutex_unlock(&rport->mutex);
1588 return SCSI_MLQUEUE_HOST_BUSY;
1592 * Note: the resources allocated in this function are freed in
1593 * srp_free_target_ib().
1595 static int srp_alloc_iu_bufs(struct srp_target_port *target)
1597 int i;
1599 target->rx_ring = kzalloc(target->queue_size * sizeof(*target->rx_ring),
1600 GFP_KERNEL);
1601 if (!target->rx_ring)
1602 goto err_no_ring;
1603 target->tx_ring = kzalloc(target->queue_size * sizeof(*target->tx_ring),
1604 GFP_KERNEL);
1605 if (!target->tx_ring)
1606 goto err_no_ring;
1608 for (i = 0; i < target->queue_size; ++i) {
1609 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1610 target->max_ti_iu_len,
1611 GFP_KERNEL, DMA_FROM_DEVICE);
1612 if (!target->rx_ring[i])
1613 goto err;
1616 for (i = 0; i < target->queue_size; ++i) {
1617 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
1618 target->max_iu_len,
1619 GFP_KERNEL, DMA_TO_DEVICE);
1620 if (!target->tx_ring[i])
1621 goto err;
1623 list_add(&target->tx_ring[i]->list, &target->free_tx);
1626 return 0;
1628 err:
1629 for (i = 0; i < target->queue_size; ++i) {
1630 srp_free_iu(target->srp_host, target->rx_ring[i]);
1631 srp_free_iu(target->srp_host, target->tx_ring[i]);
1635 err_no_ring:
1636 kfree(target->tx_ring);
1637 target->tx_ring = NULL;
1638 kfree(target->rx_ring);
1639 target->rx_ring = NULL;
1641 return -ENOMEM;
1644 static uint32_t srp_compute_rq_tmo(struct ib_qp_attr *qp_attr, int attr_mask)
1646 uint64_t T_tr_ns, max_compl_time_ms;
1647 uint32_t rq_tmo_jiffies;
1650 * According to section 11.2.4.2 in the IBTA spec (Modify Queue Pair,
1651 * table 91), both the QP timeout and the retry count have to be set
1652 * for RC QP's during the RTR to RTS transition.
1654 WARN_ON_ONCE((attr_mask & (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)) !=
1655 (IB_QP_TIMEOUT | IB_QP_RETRY_CNT));
1658 * Set target->rq_tmo_jiffies to one second more than the largest time
1659 * it can take before an error completion is generated. See also
1660 * C9-140..142 in the IBTA spec for more information about how to
1661 * convert the QP Local ACK Timeout value to nanoseconds.
1663 T_tr_ns = 4096 * (1ULL << qp_attr->timeout);
1664 max_compl_time_ms = qp_attr->retry_cnt * 4 * T_tr_ns;
1665 do_div(max_compl_time_ms, NSEC_PER_MSEC);
1666 rq_tmo_jiffies = msecs_to_jiffies(max_compl_time_ms + 1000);
1668 return rq_tmo_jiffies;
1671 static void srp_cm_rep_handler(struct ib_cm_id *cm_id,
1672 struct srp_login_rsp *lrsp,
1673 struct srp_target_port *target)
1675 struct ib_qp_attr *qp_attr = NULL;
1676 int attr_mask = 0;
1677 int ret;
1678 int i;
1680 if (lrsp->opcode == SRP_LOGIN_RSP) {
1681 target->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len);
1682 target->req_lim = be32_to_cpu(lrsp->req_lim_delta);
1685 * Reserve credits for task management so we don't
1686 * bounce requests back to the SCSI mid-layer.
1688 target->scsi_host->can_queue
1689 = min(target->req_lim - SRP_TSK_MGMT_SQ_SIZE,
1690 target->scsi_host->can_queue);
1691 target->scsi_host->cmd_per_lun
1692 = min_t(int, target->scsi_host->can_queue,
1693 target->scsi_host->cmd_per_lun);
1694 } else {
1695 shost_printk(KERN_WARNING, target->scsi_host,
1696 PFX "Unhandled RSP opcode %#x\n", lrsp->opcode);
1697 ret = -ECONNRESET;
1698 goto error;
1701 if (!target->rx_ring) {
1702 ret = srp_alloc_iu_bufs(target);
1703 if (ret)
1704 goto error;
1707 ret = -ENOMEM;
1708 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1709 if (!qp_attr)
1710 goto error;
1712 qp_attr->qp_state = IB_QPS_RTR;
1713 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1714 if (ret)
1715 goto error_free;
1717 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
1718 if (ret)
1719 goto error_free;
1721 for (i = 0; i < target->queue_size; i++) {
1722 struct srp_iu *iu = target->rx_ring[i];
1723 ret = srp_post_recv(target, iu);
1724 if (ret)
1725 goto error_free;
1728 qp_attr->qp_state = IB_QPS_RTS;
1729 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1730 if (ret)
1731 goto error_free;
1733 target->rq_tmo_jiffies = srp_compute_rq_tmo(qp_attr, attr_mask);
1735 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
1736 if (ret)
1737 goto error_free;
1739 ret = ib_send_cm_rtu(cm_id, NULL, 0);
1741 error_free:
1742 kfree(qp_attr);
1744 error:
1745 target->status = ret;
1748 static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1749 struct ib_cm_event *event,
1750 struct srp_target_port *target)
1752 struct Scsi_Host *shost = target->scsi_host;
1753 struct ib_class_port_info *cpi;
1754 int opcode;
1756 switch (event->param.rej_rcvd.reason) {
1757 case IB_CM_REJ_PORT_CM_REDIRECT:
1758 cpi = event->param.rej_rcvd.ari;
1759 target->path.dlid = cpi->redirect_lid;
1760 target->path.pkey = cpi->redirect_pkey;
1761 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1762 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1764 target->status = target->path.dlid ?
1765 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1766 break;
1768 case IB_CM_REJ_PORT_REDIRECT:
1769 if (srp_target_is_topspin(target)) {
1771 * Topspin/Cisco SRP gateways incorrectly send
1772 * reject reason code 25 when they mean 24
1773 * (port redirect).
1775 memcpy(target->path.dgid.raw,
1776 event->param.rej_rcvd.ari, 16);
1778 shost_printk(KERN_DEBUG, shost,
1779 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1780 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1781 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1783 target->status = SRP_PORT_REDIRECT;
1784 } else {
1785 shost_printk(KERN_WARNING, shost,
1786 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1787 target->status = -ECONNRESET;
1789 break;
1791 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
1792 shost_printk(KERN_WARNING, shost,
1793 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1794 target->status = -ECONNRESET;
1795 break;
1797 case IB_CM_REJ_CONSUMER_DEFINED:
1798 opcode = *(u8 *) event->private_data;
1799 if (opcode == SRP_LOGIN_REJ) {
1800 struct srp_login_rej *rej = event->private_data;
1801 u32 reason = be32_to_cpu(rej->reason);
1803 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
1804 shost_printk(KERN_WARNING, shost,
1805 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1806 else
1807 shost_printk(KERN_WARNING, shost,
1808 PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
1809 } else
1810 shost_printk(KERN_WARNING, shost,
1811 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1812 " opcode 0x%02x\n", opcode);
1813 target->status = -ECONNRESET;
1814 break;
1816 case IB_CM_REJ_STALE_CONN:
1817 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
1818 target->status = SRP_STALE_CONN;
1819 break;
1821 default:
1822 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
1823 event->param.rej_rcvd.reason);
1824 target->status = -ECONNRESET;
1828 static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1830 struct srp_target_port *target = cm_id->context;
1831 int comp = 0;
1833 switch (event->event) {
1834 case IB_CM_REQ_ERROR:
1835 shost_printk(KERN_DEBUG, target->scsi_host,
1836 PFX "Sending CM REQ failed\n");
1837 comp = 1;
1838 target->status = -ECONNRESET;
1839 break;
1841 case IB_CM_REP_RECEIVED:
1842 comp = 1;
1843 srp_cm_rep_handler(cm_id, event->private_data, target);
1844 break;
1846 case IB_CM_REJ_RECEIVED:
1847 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
1848 comp = 1;
1850 srp_cm_rej_handler(cm_id, event, target);
1851 break;
1853 case IB_CM_DREQ_RECEIVED:
1854 shost_printk(KERN_WARNING, target->scsi_host,
1855 PFX "DREQ received - connection closed\n");
1856 srp_change_conn_state(target, false);
1857 if (ib_send_cm_drep(cm_id, NULL, 0))
1858 shost_printk(KERN_ERR, target->scsi_host,
1859 PFX "Sending CM DREP failed\n");
1860 queue_work(system_long_wq, &target->tl_err_work);
1861 break;
1863 case IB_CM_TIMEWAIT_EXIT:
1864 shost_printk(KERN_ERR, target->scsi_host,
1865 PFX "connection closed\n");
1867 target->status = 0;
1868 break;
1870 case IB_CM_MRA_RECEIVED:
1871 case IB_CM_DREQ_ERROR:
1872 case IB_CM_DREP_RECEIVED:
1873 break;
1875 default:
1876 shost_printk(KERN_WARNING, target->scsi_host,
1877 PFX "Unhandled CM event %d\n", event->event);
1878 break;
1881 if (comp)
1882 complete(&target->done);
1884 return 0;
1888 * srp_change_queue_type - changing device queue tag type
1889 * @sdev: scsi device struct
1890 * @tag_type: requested tag type
1892 * Returns queue tag type.
1894 static int
1895 srp_change_queue_type(struct scsi_device *sdev, int tag_type)
1897 if (sdev->tagged_supported) {
1898 scsi_set_tag_type(sdev, tag_type);
1899 if (tag_type)
1900 scsi_activate_tcq(sdev, sdev->queue_depth);
1901 else
1902 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1903 } else
1904 tag_type = 0;
1906 return tag_type;
1910 * srp_change_queue_depth - setting device queue depth
1911 * @sdev: scsi device struct
1912 * @qdepth: requested queue depth
1913 * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
1914 * (see include/scsi/scsi_host.h for definition)
1916 * Returns queue depth.
1918 static int
1919 srp_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1921 struct Scsi_Host *shost = sdev->host;
1922 int max_depth;
1923 if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP) {
1924 max_depth = shost->can_queue;
1925 if (!sdev->tagged_supported)
1926 max_depth = 1;
1927 if (qdepth > max_depth)
1928 qdepth = max_depth;
1929 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1930 } else if (reason == SCSI_QDEPTH_QFULL)
1931 scsi_track_queue_full(sdev, qdepth);
1932 else
1933 return -EOPNOTSUPP;
1935 return sdev->queue_depth;
1938 static int srp_send_tsk_mgmt(struct srp_target_port *target,
1939 u64 req_tag, unsigned int lun, u8 func)
1941 struct srp_rport *rport = target->rport;
1942 struct ib_device *dev = target->srp_host->srp_dev->dev;
1943 struct srp_iu *iu;
1944 struct srp_tsk_mgmt *tsk_mgmt;
1946 if (!target->connected || target->qp_in_error)
1947 return -1;
1949 init_completion(&target->tsk_mgmt_done);
1952 * Lock the rport mutex to avoid that srp_create_target_ib() is
1953 * invoked while a task management function is being sent.
1955 mutex_lock(&rport->mutex);
1956 spin_lock_irq(&target->lock);
1957 iu = __srp_get_tx_iu(target, SRP_IU_TSK_MGMT);
1958 spin_unlock_irq(&target->lock);
1960 if (!iu) {
1961 mutex_unlock(&rport->mutex);
1963 return -1;
1966 ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt,
1967 DMA_TO_DEVICE);
1968 tsk_mgmt = iu->buf;
1969 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1971 tsk_mgmt->opcode = SRP_TSK_MGMT;
1972 tsk_mgmt->lun = cpu_to_be64((u64) lun << 48);
1973 tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT;
1974 tsk_mgmt->tsk_mgmt_func = func;
1975 tsk_mgmt->task_tag = req_tag;
1977 ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt,
1978 DMA_TO_DEVICE);
1979 if (srp_post_send(target, iu, sizeof *tsk_mgmt)) {
1980 srp_put_tx_iu(target, iu, SRP_IU_TSK_MGMT);
1981 mutex_unlock(&rport->mutex);
1983 return -1;
1985 mutex_unlock(&rport->mutex);
1987 if (!wait_for_completion_timeout(&target->tsk_mgmt_done,
1988 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
1989 return -1;
1991 return 0;
1994 static int srp_abort(struct scsi_cmnd *scmnd)
1996 struct srp_target_port *target = host_to_target(scmnd->device->host);
1997 struct srp_request *req = (struct srp_request *) scmnd->host_scribble;
1998 int ret;
2000 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
2002 if (!req || !srp_claim_req(target, req, scmnd))
2003 return SUCCESS;
2004 if (srp_send_tsk_mgmt(target, req->index, scmnd->device->lun,
2005 SRP_TSK_ABORT_TASK) == 0)
2006 ret = SUCCESS;
2007 else if (target->rport->state == SRP_RPORT_LOST)
2008 ret = FAST_IO_FAIL;
2009 else
2010 ret = FAILED;
2011 srp_free_req(target, req, scmnd, 0);
2012 scmnd->result = DID_ABORT << 16;
2013 scmnd->scsi_done(scmnd);
2015 return ret;
2018 static int srp_reset_device(struct scsi_cmnd *scmnd)
2020 struct srp_target_port *target = host_to_target(scmnd->device->host);
2021 int i;
2023 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
2025 if (srp_send_tsk_mgmt(target, SRP_TAG_NO_REQ, scmnd->device->lun,
2026 SRP_TSK_LUN_RESET))
2027 return FAILED;
2028 if (target->tsk_mgmt_status)
2029 return FAILED;
2031 for (i = 0; i < target->req_ring_size; ++i) {
2032 struct srp_request *req = &target->req_ring[i];
2033 if (req->scmnd && req->scmnd->device == scmnd->device)
2034 srp_finish_req(target, req, DID_RESET << 16);
2037 return SUCCESS;
2040 static int srp_reset_host(struct scsi_cmnd *scmnd)
2042 struct srp_target_port *target = host_to_target(scmnd->device->host);
2044 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
2046 return srp_reconnect_rport(target->rport) == 0 ? SUCCESS : FAILED;
2049 static int srp_slave_configure(struct scsi_device *sdev)
2051 struct Scsi_Host *shost = sdev->host;
2052 struct srp_target_port *target = host_to_target(shost);
2053 struct request_queue *q = sdev->request_queue;
2054 unsigned long timeout;
2056 if (sdev->type == TYPE_DISK) {
2057 timeout = max_t(unsigned, 30 * HZ, target->rq_tmo_jiffies);
2058 blk_queue_rq_timeout(q, timeout);
2061 return 0;
2064 static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
2065 char *buf)
2067 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2069 return sprintf(buf, "0x%016llx\n",
2070 (unsigned long long) be64_to_cpu(target->id_ext));
2073 static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
2074 char *buf)
2076 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2078 return sprintf(buf, "0x%016llx\n",
2079 (unsigned long long) be64_to_cpu(target->ioc_guid));
2082 static ssize_t show_service_id(struct device *dev,
2083 struct device_attribute *attr, char *buf)
2085 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2087 return sprintf(buf, "0x%016llx\n",
2088 (unsigned long long) be64_to_cpu(target->service_id));
2091 static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
2092 char *buf)
2094 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2096 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
2099 static ssize_t show_sgid(struct device *dev, struct device_attribute *attr,
2100 char *buf)
2102 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2104 return sprintf(buf, "%pI6\n", target->path.sgid.raw);
2107 static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
2108 char *buf)
2110 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2112 return sprintf(buf, "%pI6\n", target->path.dgid.raw);
2115 static ssize_t show_orig_dgid(struct device *dev,
2116 struct device_attribute *attr, char *buf)
2118 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2120 return sprintf(buf, "%pI6\n", target->orig_dgid);
2123 static ssize_t show_req_lim(struct device *dev,
2124 struct device_attribute *attr, char *buf)
2126 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2128 return sprintf(buf, "%d\n", target->req_lim);
2131 static ssize_t show_zero_req_lim(struct device *dev,
2132 struct device_attribute *attr, char *buf)
2134 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2136 return sprintf(buf, "%d\n", target->zero_req_lim);
2139 static ssize_t show_local_ib_port(struct device *dev,
2140 struct device_attribute *attr, char *buf)
2142 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2144 return sprintf(buf, "%d\n", target->srp_host->port);
2147 static ssize_t show_local_ib_device(struct device *dev,
2148 struct device_attribute *attr, char *buf)
2150 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2152 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
2155 static ssize_t show_comp_vector(struct device *dev,
2156 struct device_attribute *attr, char *buf)
2158 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2160 return sprintf(buf, "%d\n", target->comp_vector);
2163 static ssize_t show_tl_retry_count(struct device *dev,
2164 struct device_attribute *attr, char *buf)
2166 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2168 return sprintf(buf, "%d\n", target->tl_retry_count);
2171 static ssize_t show_cmd_sg_entries(struct device *dev,
2172 struct device_attribute *attr, char *buf)
2174 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2176 return sprintf(buf, "%u\n", target->cmd_sg_cnt);
2179 static ssize_t show_allow_ext_sg(struct device *dev,
2180 struct device_attribute *attr, char *buf)
2182 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2184 return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false");
2187 static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
2188 static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
2189 static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
2190 static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
2191 static DEVICE_ATTR(sgid, S_IRUGO, show_sgid, NULL);
2192 static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
2193 static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
2194 static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL);
2195 static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
2196 static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
2197 static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
2198 static DEVICE_ATTR(comp_vector, S_IRUGO, show_comp_vector, NULL);
2199 static DEVICE_ATTR(tl_retry_count, S_IRUGO, show_tl_retry_count, NULL);
2200 static DEVICE_ATTR(cmd_sg_entries, S_IRUGO, show_cmd_sg_entries, NULL);
2201 static DEVICE_ATTR(allow_ext_sg, S_IRUGO, show_allow_ext_sg, NULL);
2203 static struct device_attribute *srp_host_attrs[] = {
2204 &dev_attr_id_ext,
2205 &dev_attr_ioc_guid,
2206 &dev_attr_service_id,
2207 &dev_attr_pkey,
2208 &dev_attr_sgid,
2209 &dev_attr_dgid,
2210 &dev_attr_orig_dgid,
2211 &dev_attr_req_lim,
2212 &dev_attr_zero_req_lim,
2213 &dev_attr_local_ib_port,
2214 &dev_attr_local_ib_device,
2215 &dev_attr_comp_vector,
2216 &dev_attr_tl_retry_count,
2217 &dev_attr_cmd_sg_entries,
2218 &dev_attr_allow_ext_sg,
2219 NULL
2222 static struct scsi_host_template srp_template = {
2223 .module = THIS_MODULE,
2224 .name = "InfiniBand SRP initiator",
2225 .proc_name = DRV_NAME,
2226 .slave_configure = srp_slave_configure,
2227 .info = srp_target_info,
2228 .queuecommand = srp_queuecommand,
2229 .change_queue_depth = srp_change_queue_depth,
2230 .change_queue_type = srp_change_queue_type,
2231 .eh_abort_handler = srp_abort,
2232 .eh_device_reset_handler = srp_reset_device,
2233 .eh_host_reset_handler = srp_reset_host,
2234 .skip_settle_delay = true,
2235 .sg_tablesize = SRP_DEF_SG_TABLESIZE,
2236 .can_queue = SRP_DEFAULT_CMD_SQ_SIZE,
2237 .this_id = -1,
2238 .cmd_per_lun = SRP_DEFAULT_CMD_SQ_SIZE,
2239 .use_clustering = ENABLE_CLUSTERING,
2240 .shost_attrs = srp_host_attrs
2243 static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
2245 struct srp_rport_identifiers ids;
2246 struct srp_rport *rport;
2248 sprintf(target->target_name, "SRP.T10:%016llX",
2249 (unsigned long long) be64_to_cpu(target->id_ext));
2251 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
2252 return -ENODEV;
2254 memcpy(ids.port_id, &target->id_ext, 8);
2255 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
2256 ids.roles = SRP_RPORT_ROLE_TARGET;
2257 rport = srp_rport_add(target->scsi_host, &ids);
2258 if (IS_ERR(rport)) {
2259 scsi_remove_host(target->scsi_host);
2260 return PTR_ERR(rport);
2263 rport->lld_data = target;
2264 target->rport = rport;
2266 spin_lock(&host->target_lock);
2267 list_add_tail(&target->list, &host->target_list);
2268 spin_unlock(&host->target_lock);
2270 target->state = SRP_TARGET_LIVE;
2272 scsi_scan_target(&target->scsi_host->shost_gendev,
2273 0, target->scsi_id, SCAN_WILD_CARD, 0);
2275 return 0;
2278 static void srp_release_dev(struct device *dev)
2280 struct srp_host *host =
2281 container_of(dev, struct srp_host, dev);
2283 complete(&host->released);
2286 static struct class srp_class = {
2287 .name = "infiniband_srp",
2288 .dev_release = srp_release_dev
2292 * srp_conn_unique() - check whether the connection to a target is unique
2294 static bool srp_conn_unique(struct srp_host *host,
2295 struct srp_target_port *target)
2297 struct srp_target_port *t;
2298 bool ret = false;
2300 if (target->state == SRP_TARGET_REMOVED)
2301 goto out;
2303 ret = true;
2305 spin_lock(&host->target_lock);
2306 list_for_each_entry(t, &host->target_list, list) {
2307 if (t != target &&
2308 target->id_ext == t->id_ext &&
2309 target->ioc_guid == t->ioc_guid &&
2310 target->initiator_ext == t->initiator_ext) {
2311 ret = false;
2312 break;
2315 spin_unlock(&host->target_lock);
2317 out:
2318 return ret;
2322 * Target ports are added by writing
2324 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
2325 * pkey=<P_Key>,service_id=<service ID>
2327 * to the add_target sysfs attribute.
2329 enum {
2330 SRP_OPT_ERR = 0,
2331 SRP_OPT_ID_EXT = 1 << 0,
2332 SRP_OPT_IOC_GUID = 1 << 1,
2333 SRP_OPT_DGID = 1 << 2,
2334 SRP_OPT_PKEY = 1 << 3,
2335 SRP_OPT_SERVICE_ID = 1 << 4,
2336 SRP_OPT_MAX_SECT = 1 << 5,
2337 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
2338 SRP_OPT_IO_CLASS = 1 << 7,
2339 SRP_OPT_INITIATOR_EXT = 1 << 8,
2340 SRP_OPT_CMD_SG_ENTRIES = 1 << 9,
2341 SRP_OPT_ALLOW_EXT_SG = 1 << 10,
2342 SRP_OPT_SG_TABLESIZE = 1 << 11,
2343 SRP_OPT_COMP_VECTOR = 1 << 12,
2344 SRP_OPT_TL_RETRY_COUNT = 1 << 13,
2345 SRP_OPT_QUEUE_SIZE = 1 << 14,
2346 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
2347 SRP_OPT_IOC_GUID |
2348 SRP_OPT_DGID |
2349 SRP_OPT_PKEY |
2350 SRP_OPT_SERVICE_ID),
2353 static const match_table_t srp_opt_tokens = {
2354 { SRP_OPT_ID_EXT, "id_ext=%s" },
2355 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
2356 { SRP_OPT_DGID, "dgid=%s" },
2357 { SRP_OPT_PKEY, "pkey=%x" },
2358 { SRP_OPT_SERVICE_ID, "service_id=%s" },
2359 { SRP_OPT_MAX_SECT, "max_sect=%d" },
2360 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
2361 { SRP_OPT_IO_CLASS, "io_class=%x" },
2362 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
2363 { SRP_OPT_CMD_SG_ENTRIES, "cmd_sg_entries=%u" },
2364 { SRP_OPT_ALLOW_EXT_SG, "allow_ext_sg=%u" },
2365 { SRP_OPT_SG_TABLESIZE, "sg_tablesize=%u" },
2366 { SRP_OPT_COMP_VECTOR, "comp_vector=%u" },
2367 { SRP_OPT_TL_RETRY_COUNT, "tl_retry_count=%u" },
2368 { SRP_OPT_QUEUE_SIZE, "queue_size=%d" },
2369 { SRP_OPT_ERR, NULL }
2372 static int srp_parse_options(const char *buf, struct srp_target_port *target)
2374 char *options, *sep_opt;
2375 char *p;
2376 char dgid[3];
2377 substring_t args[MAX_OPT_ARGS];
2378 int opt_mask = 0;
2379 int token;
2380 int ret = -EINVAL;
2381 int i;
2383 options = kstrdup(buf, GFP_KERNEL);
2384 if (!options)
2385 return -ENOMEM;
2387 sep_opt = options;
2388 while ((p = strsep(&sep_opt, ",")) != NULL) {
2389 if (!*p)
2390 continue;
2392 token = match_token(p, srp_opt_tokens, args);
2393 opt_mask |= token;
2395 switch (token) {
2396 case SRP_OPT_ID_EXT:
2397 p = match_strdup(args);
2398 if (!p) {
2399 ret = -ENOMEM;
2400 goto out;
2402 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2403 kfree(p);
2404 break;
2406 case SRP_OPT_IOC_GUID:
2407 p = match_strdup(args);
2408 if (!p) {
2409 ret = -ENOMEM;
2410 goto out;
2412 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
2413 kfree(p);
2414 break;
2416 case SRP_OPT_DGID:
2417 p = match_strdup(args);
2418 if (!p) {
2419 ret = -ENOMEM;
2420 goto out;
2422 if (strlen(p) != 32) {
2423 pr_warn("bad dest GID parameter '%s'\n", p);
2424 kfree(p);
2425 goto out;
2428 for (i = 0; i < 16; ++i) {
2429 strlcpy(dgid, p + i * 2, 3);
2430 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
2432 kfree(p);
2433 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
2434 break;
2436 case SRP_OPT_PKEY:
2437 if (match_hex(args, &token)) {
2438 pr_warn("bad P_Key parameter '%s'\n", p);
2439 goto out;
2441 target->path.pkey = cpu_to_be16(token);
2442 break;
2444 case SRP_OPT_SERVICE_ID:
2445 p = match_strdup(args);
2446 if (!p) {
2447 ret = -ENOMEM;
2448 goto out;
2450 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
2451 target->path.service_id = target->service_id;
2452 kfree(p);
2453 break;
2455 case SRP_OPT_MAX_SECT:
2456 if (match_int(args, &token)) {
2457 pr_warn("bad max sect parameter '%s'\n", p);
2458 goto out;
2460 target->scsi_host->max_sectors = token;
2461 break;
2463 case SRP_OPT_QUEUE_SIZE:
2464 if (match_int(args, &token) || token < 1) {
2465 pr_warn("bad queue_size parameter '%s'\n", p);
2466 goto out;
2468 target->scsi_host->can_queue = token;
2469 target->queue_size = token + SRP_RSP_SQ_SIZE +
2470 SRP_TSK_MGMT_SQ_SIZE;
2471 if (!(opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
2472 target->scsi_host->cmd_per_lun = token;
2473 break;
2475 case SRP_OPT_MAX_CMD_PER_LUN:
2476 if (match_int(args, &token) || token < 1) {
2477 pr_warn("bad max cmd_per_lun parameter '%s'\n",
2479 goto out;
2481 target->scsi_host->cmd_per_lun = token;
2482 break;
2484 case SRP_OPT_IO_CLASS:
2485 if (match_hex(args, &token)) {
2486 pr_warn("bad IO class parameter '%s'\n", p);
2487 goto out;
2489 if (token != SRP_REV10_IB_IO_CLASS &&
2490 token != SRP_REV16A_IB_IO_CLASS) {
2491 pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n",
2492 token, SRP_REV10_IB_IO_CLASS,
2493 SRP_REV16A_IB_IO_CLASS);
2494 goto out;
2496 target->io_class = token;
2497 break;
2499 case SRP_OPT_INITIATOR_EXT:
2500 p = match_strdup(args);
2501 if (!p) {
2502 ret = -ENOMEM;
2503 goto out;
2505 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2506 kfree(p);
2507 break;
2509 case SRP_OPT_CMD_SG_ENTRIES:
2510 if (match_int(args, &token) || token < 1 || token > 255) {
2511 pr_warn("bad max cmd_sg_entries parameter '%s'\n",
2513 goto out;
2515 target->cmd_sg_cnt = token;
2516 break;
2518 case SRP_OPT_ALLOW_EXT_SG:
2519 if (match_int(args, &token)) {
2520 pr_warn("bad allow_ext_sg parameter '%s'\n", p);
2521 goto out;
2523 target->allow_ext_sg = !!token;
2524 break;
2526 case SRP_OPT_SG_TABLESIZE:
2527 if (match_int(args, &token) || token < 1 ||
2528 token > SCSI_MAX_SG_CHAIN_SEGMENTS) {
2529 pr_warn("bad max sg_tablesize parameter '%s'\n",
2531 goto out;
2533 target->sg_tablesize = token;
2534 break;
2536 case SRP_OPT_COMP_VECTOR:
2537 if (match_int(args, &token) || token < 0) {
2538 pr_warn("bad comp_vector parameter '%s'\n", p);
2539 goto out;
2541 target->comp_vector = token;
2542 break;
2544 case SRP_OPT_TL_RETRY_COUNT:
2545 if (match_int(args, &token) || token < 2 || token > 7) {
2546 pr_warn("bad tl_retry_count parameter '%s' (must be a number between 2 and 7)\n",
2548 goto out;
2550 target->tl_retry_count = token;
2551 break;
2553 default:
2554 pr_warn("unknown parameter or missing value '%s' in target creation request\n",
2556 goto out;
2560 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
2561 ret = 0;
2562 else
2563 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
2564 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
2565 !(srp_opt_tokens[i].token & opt_mask))
2566 pr_warn("target creation request is missing parameter '%s'\n",
2567 srp_opt_tokens[i].pattern);
2569 if (target->scsi_host->cmd_per_lun > target->scsi_host->can_queue
2570 && (opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
2571 pr_warn("cmd_per_lun = %d > queue_size = %d\n",
2572 target->scsi_host->cmd_per_lun,
2573 target->scsi_host->can_queue);
2575 out:
2576 kfree(options);
2577 return ret;
2580 static ssize_t srp_create_target(struct device *dev,
2581 struct device_attribute *attr,
2582 const char *buf, size_t count)
2584 struct srp_host *host =
2585 container_of(dev, struct srp_host, dev);
2586 struct Scsi_Host *target_host;
2587 struct srp_target_port *target;
2588 struct ib_device *ibdev = host->srp_dev->dev;
2589 int ret;
2591 target_host = scsi_host_alloc(&srp_template,
2592 sizeof (struct srp_target_port));
2593 if (!target_host)
2594 return -ENOMEM;
2596 target_host->transportt = ib_srp_transport_template;
2597 target_host->max_channel = 0;
2598 target_host->max_id = 1;
2599 target_host->max_lun = SRP_MAX_LUN;
2600 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
2602 target = host_to_target(target_host);
2604 target->io_class = SRP_REV16A_IB_IO_CLASS;
2605 target->scsi_host = target_host;
2606 target->srp_host = host;
2607 target->lkey = host->srp_dev->mr->lkey;
2608 target->rkey = host->srp_dev->mr->rkey;
2609 target->cmd_sg_cnt = cmd_sg_entries;
2610 target->sg_tablesize = indirect_sg_entries ? : cmd_sg_entries;
2611 target->allow_ext_sg = allow_ext_sg;
2612 target->tl_retry_count = 7;
2613 target->queue_size = SRP_DEFAULT_QUEUE_SIZE;
2615 ret = srp_parse_options(buf, target);
2616 if (ret)
2617 goto err;
2619 target->req_ring_size = target->queue_size - SRP_TSK_MGMT_SQ_SIZE;
2621 if (!srp_conn_unique(target->srp_host, target)) {
2622 shost_printk(KERN_INFO, target->scsi_host,
2623 PFX "Already connected to target port with id_ext=%016llx;ioc_guid=%016llx;initiator_ext=%016llx\n",
2624 be64_to_cpu(target->id_ext),
2625 be64_to_cpu(target->ioc_guid),
2626 be64_to_cpu(target->initiator_ext));
2627 ret = -EEXIST;
2628 goto err;
2631 if (!host->srp_dev->fmr_pool && !target->allow_ext_sg &&
2632 target->cmd_sg_cnt < target->sg_tablesize) {
2633 pr_warn("No FMR pool and no external indirect descriptors, limiting sg_tablesize to cmd_sg_cnt\n");
2634 target->sg_tablesize = target->cmd_sg_cnt;
2637 target_host->sg_tablesize = target->sg_tablesize;
2638 target->indirect_size = target->sg_tablesize *
2639 sizeof (struct srp_direct_buf);
2640 target->max_iu_len = sizeof (struct srp_cmd) +
2641 sizeof (struct srp_indirect_buf) +
2642 target->cmd_sg_cnt * sizeof (struct srp_direct_buf);
2644 INIT_WORK(&target->tl_err_work, srp_tl_err_work);
2645 INIT_WORK(&target->remove_work, srp_remove_work);
2646 spin_lock_init(&target->lock);
2647 INIT_LIST_HEAD(&target->free_tx);
2648 ret = srp_alloc_req_data(target);
2649 if (ret)
2650 goto err_free_mem;
2652 ib_query_gid(ibdev, host->port, 0, &target->path.sgid);
2654 shost_printk(KERN_DEBUG, target->scsi_host, PFX
2655 "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
2656 "service_id %016llx dgid %pI6\n",
2657 (unsigned long long) be64_to_cpu(target->id_ext),
2658 (unsigned long long) be64_to_cpu(target->ioc_guid),
2659 be16_to_cpu(target->path.pkey),
2660 (unsigned long long) be64_to_cpu(target->service_id),
2661 target->path.dgid.raw);
2663 ret = srp_create_target_ib(target);
2664 if (ret)
2665 goto err_free_mem;
2667 ret = srp_new_cm_id(target);
2668 if (ret)
2669 goto err_free_ib;
2671 ret = srp_connect_target(target);
2672 if (ret) {
2673 shost_printk(KERN_ERR, target->scsi_host,
2674 PFX "Connection failed\n");
2675 goto err_cm_id;
2678 ret = srp_add_target(host, target);
2679 if (ret)
2680 goto err_disconnect;
2682 return count;
2684 err_disconnect:
2685 srp_disconnect_target(target);
2687 err_cm_id:
2688 ib_destroy_cm_id(target->cm_id);
2690 err_free_ib:
2691 srp_free_target_ib(target);
2693 err_free_mem:
2694 srp_free_req_data(target);
2696 err:
2697 scsi_host_put(target_host);
2699 return ret;
2702 static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
2704 static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
2705 char *buf)
2707 struct srp_host *host = container_of(dev, struct srp_host, dev);
2709 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
2712 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
2714 static ssize_t show_port(struct device *dev, struct device_attribute *attr,
2715 char *buf)
2717 struct srp_host *host = container_of(dev, struct srp_host, dev);
2719 return sprintf(buf, "%d\n", host->port);
2722 static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
2724 static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
2726 struct srp_host *host;
2728 host = kzalloc(sizeof *host, GFP_KERNEL);
2729 if (!host)
2730 return NULL;
2732 INIT_LIST_HEAD(&host->target_list);
2733 spin_lock_init(&host->target_lock);
2734 init_completion(&host->released);
2735 host->srp_dev = device;
2736 host->port = port;
2738 host->dev.class = &srp_class;
2739 host->dev.parent = device->dev->dma_device;
2740 dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port);
2742 if (device_register(&host->dev))
2743 goto free_host;
2744 if (device_create_file(&host->dev, &dev_attr_add_target))
2745 goto err_class;
2746 if (device_create_file(&host->dev, &dev_attr_ibdev))
2747 goto err_class;
2748 if (device_create_file(&host->dev, &dev_attr_port))
2749 goto err_class;
2751 return host;
2753 err_class:
2754 device_unregister(&host->dev);
2756 free_host:
2757 kfree(host);
2759 return NULL;
2762 static void srp_add_one(struct ib_device *device)
2764 struct srp_device *srp_dev;
2765 struct ib_device_attr *dev_attr;
2766 struct ib_fmr_pool_param fmr_param;
2767 struct srp_host *host;
2768 int max_pages_per_fmr, fmr_page_shift, s, e, p;
2770 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
2771 if (!dev_attr)
2772 return;
2774 if (ib_query_device(device, dev_attr)) {
2775 pr_warn("Query device failed for %s\n", device->name);
2776 goto free_attr;
2779 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
2780 if (!srp_dev)
2781 goto free_attr;
2784 * Use the smallest page size supported by the HCA, down to a
2785 * minimum of 4096 bytes. We're unlikely to build large sglists
2786 * out of smaller entries.
2788 fmr_page_shift = max(12, ffs(dev_attr->page_size_cap) - 1);
2789 srp_dev->fmr_page_size = 1 << fmr_page_shift;
2790 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
2791 srp_dev->fmr_max_size = srp_dev->fmr_page_size * SRP_FMR_SIZE;
2793 INIT_LIST_HEAD(&srp_dev->dev_list);
2795 srp_dev->dev = device;
2796 srp_dev->pd = ib_alloc_pd(device);
2797 if (IS_ERR(srp_dev->pd))
2798 goto free_dev;
2800 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
2801 IB_ACCESS_LOCAL_WRITE |
2802 IB_ACCESS_REMOTE_READ |
2803 IB_ACCESS_REMOTE_WRITE);
2804 if (IS_ERR(srp_dev->mr))
2805 goto err_pd;
2807 for (max_pages_per_fmr = SRP_FMR_SIZE;
2808 max_pages_per_fmr >= SRP_FMR_MIN_SIZE;
2809 max_pages_per_fmr /= 2, srp_dev->fmr_max_size /= 2) {
2810 memset(&fmr_param, 0, sizeof fmr_param);
2811 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
2812 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
2813 fmr_param.cache = 1;
2814 fmr_param.max_pages_per_fmr = max_pages_per_fmr;
2815 fmr_param.page_shift = fmr_page_shift;
2816 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
2817 IB_ACCESS_REMOTE_WRITE |
2818 IB_ACCESS_REMOTE_READ);
2820 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
2821 if (!IS_ERR(srp_dev->fmr_pool))
2822 break;
2825 if (IS_ERR(srp_dev->fmr_pool))
2826 srp_dev->fmr_pool = NULL;
2828 if (device->node_type == RDMA_NODE_IB_SWITCH) {
2829 s = 0;
2830 e = 0;
2831 } else {
2832 s = 1;
2833 e = device->phys_port_cnt;
2836 for (p = s; p <= e; ++p) {
2837 host = srp_add_port(srp_dev, p);
2838 if (host)
2839 list_add_tail(&host->list, &srp_dev->dev_list);
2842 ib_set_client_data(device, &srp_client, srp_dev);
2844 goto free_attr;
2846 err_pd:
2847 ib_dealloc_pd(srp_dev->pd);
2849 free_dev:
2850 kfree(srp_dev);
2852 free_attr:
2853 kfree(dev_attr);
2856 static void srp_remove_one(struct ib_device *device)
2858 struct srp_device *srp_dev;
2859 struct srp_host *host, *tmp_host;
2860 struct srp_target_port *target;
2862 srp_dev = ib_get_client_data(device, &srp_client);
2863 if (!srp_dev)
2864 return;
2866 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
2867 device_unregister(&host->dev);
2869 * Wait for the sysfs entry to go away, so that no new
2870 * target ports can be created.
2872 wait_for_completion(&host->released);
2875 * Remove all target ports.
2877 spin_lock(&host->target_lock);
2878 list_for_each_entry(target, &host->target_list, list)
2879 srp_queue_remove_work(target);
2880 spin_unlock(&host->target_lock);
2883 * Wait for target port removal tasks.
2885 flush_workqueue(system_long_wq);
2887 kfree(host);
2890 if (srp_dev->fmr_pool)
2891 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2892 ib_dereg_mr(srp_dev->mr);
2893 ib_dealloc_pd(srp_dev->pd);
2895 kfree(srp_dev);
2898 static struct srp_function_template ib_srp_transport_functions = {
2899 .has_rport_state = true,
2900 .reset_timer_if_blocked = true,
2901 .reconnect_delay = &srp_reconnect_delay,
2902 .fast_io_fail_tmo = &srp_fast_io_fail_tmo,
2903 .dev_loss_tmo = &srp_dev_loss_tmo,
2904 .reconnect = srp_rport_reconnect,
2905 .rport_delete = srp_rport_delete,
2906 .terminate_rport_io = srp_terminate_io,
2909 static int __init srp_init_module(void)
2911 int ret;
2913 BUILD_BUG_ON(FIELD_SIZEOF(struct ib_wc, wr_id) < sizeof(void *));
2915 if (srp_sg_tablesize) {
2916 pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n");
2917 if (!cmd_sg_entries)
2918 cmd_sg_entries = srp_sg_tablesize;
2921 if (!cmd_sg_entries)
2922 cmd_sg_entries = SRP_DEF_SG_TABLESIZE;
2924 if (cmd_sg_entries > 255) {
2925 pr_warn("Clamping cmd_sg_entries to 255\n");
2926 cmd_sg_entries = 255;
2929 if (!indirect_sg_entries)
2930 indirect_sg_entries = cmd_sg_entries;
2931 else if (indirect_sg_entries < cmd_sg_entries) {
2932 pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n",
2933 cmd_sg_entries);
2934 indirect_sg_entries = cmd_sg_entries;
2937 ib_srp_transport_template =
2938 srp_attach_transport(&ib_srp_transport_functions);
2939 if (!ib_srp_transport_template)
2940 return -ENOMEM;
2942 ret = class_register(&srp_class);
2943 if (ret) {
2944 pr_err("couldn't register class infiniband_srp\n");
2945 srp_release_transport(ib_srp_transport_template);
2946 return ret;
2949 ib_sa_register_client(&srp_sa_client);
2951 ret = ib_register_client(&srp_client);
2952 if (ret) {
2953 pr_err("couldn't register IB client\n");
2954 srp_release_transport(ib_srp_transport_template);
2955 ib_sa_unregister_client(&srp_sa_client);
2956 class_unregister(&srp_class);
2957 return ret;
2960 return 0;
2963 static void __exit srp_cleanup_module(void)
2965 ib_unregister_client(&srp_client);
2966 ib_sa_unregister_client(&srp_sa_client);
2967 class_unregister(&srp_class);
2968 srp_release_transport(ib_srp_transport_template);
2971 module_init(srp_init_module);
2972 module_exit(srp_cleanup_module);