IB/srp: Move ib_destroy_cm_id() call into srp_free_ch_ib()
[deliverable/linux.git] / drivers / infiniband / ulp / srp / ib_srp.c
1 /*
2 * Copyright (c) 2005 Cisco Systems. All rights reserved.
3 *
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:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
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.
22 *
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.
31 */
32
33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
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>
43
44 #include <linux/atomic.h>
45
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>
52
53 #include "ib_srp.h"
54
55 #define DRV_NAME "ib_srp"
56 #define PFX DRV_NAME ": "
57 #define DRV_VERSION "1.0"
58 #define DRV_RELDATE "July 1, 2013"
59
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");
64
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 bool prefer_fr;
70 static bool register_always;
71 static int topspin_workarounds = 1;
72
73 module_param(srp_sg_tablesize, uint, 0444);
74 MODULE_PARM_DESC(srp_sg_tablesize, "Deprecated name for cmd_sg_entries");
75
76 module_param(cmd_sg_entries, uint, 0444);
77 MODULE_PARM_DESC(cmd_sg_entries,
78 "Default number of gather/scatter entries in the SRP command (default is 12, max 255)");
79
80 module_param(indirect_sg_entries, uint, 0444);
81 MODULE_PARM_DESC(indirect_sg_entries,
82 "Default max number of gather/scatter entries (default is 12, max is " __stringify(SCSI_MAX_SG_CHAIN_SEGMENTS) ")");
83
84 module_param(allow_ext_sg, bool, 0444);
85 MODULE_PARM_DESC(allow_ext_sg,
86 "Default behavior when there are more than cmd_sg_entries S/G entries after mapping; fails the request when false (default false)");
87
88 module_param(topspin_workarounds, int, 0444);
89 MODULE_PARM_DESC(topspin_workarounds,
90 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
91
92 module_param(prefer_fr, bool, 0444);
93 MODULE_PARM_DESC(prefer_fr,
94 "Whether to use fast registration if both FMR and fast registration are supported");
95
96 module_param(register_always, bool, 0444);
97 MODULE_PARM_DESC(register_always,
98 "Use memory registration even for contiguous memory regions");
99
100 static struct kernel_param_ops srp_tmo_ops;
101
102 static int srp_reconnect_delay = 10;
103 module_param_cb(reconnect_delay, &srp_tmo_ops, &srp_reconnect_delay,
104 S_IRUGO | S_IWUSR);
105 MODULE_PARM_DESC(reconnect_delay, "Time between successive reconnect attempts");
106
107 static int srp_fast_io_fail_tmo = 15;
108 module_param_cb(fast_io_fail_tmo, &srp_tmo_ops, &srp_fast_io_fail_tmo,
109 S_IRUGO | S_IWUSR);
110 MODULE_PARM_DESC(fast_io_fail_tmo,
111 "Number of seconds between the observation of a transport"
112 " layer error and failing all I/O. \"off\" means that this"
113 " functionality is disabled.");
114
115 static int srp_dev_loss_tmo = 600;
116 module_param_cb(dev_loss_tmo, &srp_tmo_ops, &srp_dev_loss_tmo,
117 S_IRUGO | S_IWUSR);
118 MODULE_PARM_DESC(dev_loss_tmo,
119 "Maximum number of seconds that the SRP transport should"
120 " insulate transport layer errors. After this time has been"
121 " exceeded the SCSI host is removed. Should be"
122 " between 1 and " __stringify(SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
123 " if fast_io_fail_tmo has not been set. \"off\" means that"
124 " this functionality is disabled.");
125
126 static void srp_add_one(struct ib_device *device);
127 static void srp_remove_one(struct ib_device *device);
128 static void srp_recv_completion(struct ib_cq *cq, void *target_ptr);
129 static void srp_send_completion(struct ib_cq *cq, void *target_ptr);
130 static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
131
132 static struct scsi_transport_template *ib_srp_transport_template;
133 static struct workqueue_struct *srp_remove_wq;
134
135 static struct ib_client srp_client = {
136 .name = "srp",
137 .add = srp_add_one,
138 .remove = srp_remove_one
139 };
140
141 static struct ib_sa_client srp_sa_client;
142
143 static int srp_tmo_get(char *buffer, const struct kernel_param *kp)
144 {
145 int tmo = *(int *)kp->arg;
146
147 if (tmo >= 0)
148 return sprintf(buffer, "%d", tmo);
149 else
150 return sprintf(buffer, "off");
151 }
152
153 static int srp_tmo_set(const char *val, const struct kernel_param *kp)
154 {
155 int tmo, res;
156
157 if (strncmp(val, "off", 3) != 0) {
158 res = kstrtoint(val, 0, &tmo);
159 if (res)
160 goto out;
161 } else {
162 tmo = -1;
163 }
164 if (kp->arg == &srp_reconnect_delay)
165 res = srp_tmo_valid(tmo, srp_fast_io_fail_tmo,
166 srp_dev_loss_tmo);
167 else if (kp->arg == &srp_fast_io_fail_tmo)
168 res = srp_tmo_valid(srp_reconnect_delay, tmo, srp_dev_loss_tmo);
169 else
170 res = srp_tmo_valid(srp_reconnect_delay, srp_fast_io_fail_tmo,
171 tmo);
172 if (res)
173 goto out;
174 *(int *)kp->arg = tmo;
175
176 out:
177 return res;
178 }
179
180 static struct kernel_param_ops srp_tmo_ops = {
181 .get = srp_tmo_get,
182 .set = srp_tmo_set,
183 };
184
185 static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
186 {
187 return (struct srp_target_port *) host->hostdata;
188 }
189
190 static const char *srp_target_info(struct Scsi_Host *host)
191 {
192 return host_to_target(host)->target_name;
193 }
194
195 static int srp_target_is_topspin(struct srp_target_port *target)
196 {
197 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
198 static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
199
200 return topspin_workarounds &&
201 (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
202 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
203 }
204
205 static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
206 gfp_t gfp_mask,
207 enum dma_data_direction direction)
208 {
209 struct srp_iu *iu;
210
211 iu = kmalloc(sizeof *iu, gfp_mask);
212 if (!iu)
213 goto out;
214
215 iu->buf = kzalloc(size, gfp_mask);
216 if (!iu->buf)
217 goto out_free_iu;
218
219 iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
220 direction);
221 if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
222 goto out_free_buf;
223
224 iu->size = size;
225 iu->direction = direction;
226
227 return iu;
228
229 out_free_buf:
230 kfree(iu->buf);
231 out_free_iu:
232 kfree(iu);
233 out:
234 return NULL;
235 }
236
237 static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
238 {
239 if (!iu)
240 return;
241
242 ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
243 iu->direction);
244 kfree(iu->buf);
245 kfree(iu);
246 }
247
248 static void srp_qp_event(struct ib_event *event, void *context)
249 {
250 pr_debug("QP event %d\n", event->event);
251 }
252
253 static int srp_init_qp(struct srp_target_port *target,
254 struct ib_qp *qp)
255 {
256 struct ib_qp_attr *attr;
257 int ret;
258
259 attr = kmalloc(sizeof *attr, GFP_KERNEL);
260 if (!attr)
261 return -ENOMEM;
262
263 ret = ib_find_pkey(target->srp_host->srp_dev->dev,
264 target->srp_host->port,
265 be16_to_cpu(target->path.pkey),
266 &attr->pkey_index);
267 if (ret)
268 goto out;
269
270 attr->qp_state = IB_QPS_INIT;
271 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
272 IB_ACCESS_REMOTE_WRITE);
273 attr->port_num = target->srp_host->port;
274
275 ret = ib_modify_qp(qp, attr,
276 IB_QP_STATE |
277 IB_QP_PKEY_INDEX |
278 IB_QP_ACCESS_FLAGS |
279 IB_QP_PORT);
280
281 out:
282 kfree(attr);
283 return ret;
284 }
285
286 static int srp_new_cm_id(struct srp_target_port *target)
287 {
288 struct ib_cm_id *new_cm_id;
289
290 new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
291 srp_cm_handler, target);
292 if (IS_ERR(new_cm_id))
293 return PTR_ERR(new_cm_id);
294
295 if (target->cm_id)
296 ib_destroy_cm_id(target->cm_id);
297 target->cm_id = new_cm_id;
298
299 return 0;
300 }
301
302 static struct ib_fmr_pool *srp_alloc_fmr_pool(struct srp_target_port *target)
303 {
304 struct srp_device *dev = target->srp_host->srp_dev;
305 struct ib_fmr_pool_param fmr_param;
306
307 memset(&fmr_param, 0, sizeof(fmr_param));
308 fmr_param.pool_size = target->scsi_host->can_queue;
309 fmr_param.dirty_watermark = fmr_param.pool_size / 4;
310 fmr_param.cache = 1;
311 fmr_param.max_pages_per_fmr = dev->max_pages_per_mr;
312 fmr_param.page_shift = ilog2(dev->mr_page_size);
313 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
314 IB_ACCESS_REMOTE_WRITE |
315 IB_ACCESS_REMOTE_READ);
316
317 return ib_create_fmr_pool(dev->pd, &fmr_param);
318 }
319
320 /**
321 * srp_destroy_fr_pool() - free the resources owned by a pool
322 * @pool: Fast registration pool to be destroyed.
323 */
324 static void srp_destroy_fr_pool(struct srp_fr_pool *pool)
325 {
326 int i;
327 struct srp_fr_desc *d;
328
329 if (!pool)
330 return;
331
332 for (i = 0, d = &pool->desc[0]; i < pool->size; i++, d++) {
333 if (d->frpl)
334 ib_free_fast_reg_page_list(d->frpl);
335 if (d->mr)
336 ib_dereg_mr(d->mr);
337 }
338 kfree(pool);
339 }
340
341 /**
342 * srp_create_fr_pool() - allocate and initialize a pool for fast registration
343 * @device: IB device to allocate fast registration descriptors for.
344 * @pd: Protection domain associated with the FR descriptors.
345 * @pool_size: Number of descriptors to allocate.
346 * @max_page_list_len: Maximum fast registration work request page list length.
347 */
348 static struct srp_fr_pool *srp_create_fr_pool(struct ib_device *device,
349 struct ib_pd *pd, int pool_size,
350 int max_page_list_len)
351 {
352 struct srp_fr_pool *pool;
353 struct srp_fr_desc *d;
354 struct ib_mr *mr;
355 struct ib_fast_reg_page_list *frpl;
356 int i, ret = -EINVAL;
357
358 if (pool_size <= 0)
359 goto err;
360 ret = -ENOMEM;
361 pool = kzalloc(sizeof(struct srp_fr_pool) +
362 pool_size * sizeof(struct srp_fr_desc), GFP_KERNEL);
363 if (!pool)
364 goto err;
365 pool->size = pool_size;
366 pool->max_page_list_len = max_page_list_len;
367 spin_lock_init(&pool->lock);
368 INIT_LIST_HEAD(&pool->free_list);
369
370 for (i = 0, d = &pool->desc[0]; i < pool->size; i++, d++) {
371 mr = ib_alloc_fast_reg_mr(pd, max_page_list_len);
372 if (IS_ERR(mr)) {
373 ret = PTR_ERR(mr);
374 goto destroy_pool;
375 }
376 d->mr = mr;
377 frpl = ib_alloc_fast_reg_page_list(device, max_page_list_len);
378 if (IS_ERR(frpl)) {
379 ret = PTR_ERR(frpl);
380 goto destroy_pool;
381 }
382 d->frpl = frpl;
383 list_add_tail(&d->entry, &pool->free_list);
384 }
385
386 out:
387 return pool;
388
389 destroy_pool:
390 srp_destroy_fr_pool(pool);
391
392 err:
393 pool = ERR_PTR(ret);
394 goto out;
395 }
396
397 /**
398 * srp_fr_pool_get() - obtain a descriptor suitable for fast registration
399 * @pool: Pool to obtain descriptor from.
400 */
401 static struct srp_fr_desc *srp_fr_pool_get(struct srp_fr_pool *pool)
402 {
403 struct srp_fr_desc *d = NULL;
404 unsigned long flags;
405
406 spin_lock_irqsave(&pool->lock, flags);
407 if (!list_empty(&pool->free_list)) {
408 d = list_first_entry(&pool->free_list, typeof(*d), entry);
409 list_del(&d->entry);
410 }
411 spin_unlock_irqrestore(&pool->lock, flags);
412
413 return d;
414 }
415
416 /**
417 * srp_fr_pool_put() - put an FR descriptor back in the free list
418 * @pool: Pool the descriptor was allocated from.
419 * @desc: Pointer to an array of fast registration descriptor pointers.
420 * @n: Number of descriptors to put back.
421 *
422 * Note: The caller must already have queued an invalidation request for
423 * desc->mr->rkey before calling this function.
424 */
425 static void srp_fr_pool_put(struct srp_fr_pool *pool, struct srp_fr_desc **desc,
426 int n)
427 {
428 unsigned long flags;
429 int i;
430
431 spin_lock_irqsave(&pool->lock, flags);
432 for (i = 0; i < n; i++)
433 list_add(&desc[i]->entry, &pool->free_list);
434 spin_unlock_irqrestore(&pool->lock, flags);
435 }
436
437 static struct srp_fr_pool *srp_alloc_fr_pool(struct srp_target_port *target)
438 {
439 struct srp_device *dev = target->srp_host->srp_dev;
440
441 return srp_create_fr_pool(dev->dev, dev->pd,
442 target->scsi_host->can_queue,
443 dev->max_pages_per_mr);
444 }
445
446 static int srp_create_target_ib(struct srp_target_port *target)
447 {
448 struct srp_device *dev = target->srp_host->srp_dev;
449 struct ib_qp_init_attr *init_attr;
450 struct ib_cq *recv_cq, *send_cq;
451 struct ib_qp *qp;
452 struct ib_fmr_pool *fmr_pool = NULL;
453 struct srp_fr_pool *fr_pool = NULL;
454 const int m = 1 + dev->use_fast_reg;
455 int ret;
456
457 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
458 if (!init_attr)
459 return -ENOMEM;
460
461 recv_cq = ib_create_cq(dev->dev, srp_recv_completion, NULL, target,
462 target->queue_size, target->comp_vector);
463 if (IS_ERR(recv_cq)) {
464 ret = PTR_ERR(recv_cq);
465 goto err;
466 }
467
468 send_cq = ib_create_cq(dev->dev, srp_send_completion, NULL, target,
469 m * target->queue_size, target->comp_vector);
470 if (IS_ERR(send_cq)) {
471 ret = PTR_ERR(send_cq);
472 goto err_recv_cq;
473 }
474
475 ib_req_notify_cq(recv_cq, IB_CQ_NEXT_COMP);
476
477 init_attr->event_handler = srp_qp_event;
478 init_attr->cap.max_send_wr = m * target->queue_size;
479 init_attr->cap.max_recv_wr = target->queue_size;
480 init_attr->cap.max_recv_sge = 1;
481 init_attr->cap.max_send_sge = 1;
482 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
483 init_attr->qp_type = IB_QPT_RC;
484 init_attr->send_cq = send_cq;
485 init_attr->recv_cq = recv_cq;
486
487 qp = ib_create_qp(dev->pd, init_attr);
488 if (IS_ERR(qp)) {
489 ret = PTR_ERR(qp);
490 goto err_send_cq;
491 }
492
493 ret = srp_init_qp(target, qp);
494 if (ret)
495 goto err_qp;
496
497 if (dev->use_fast_reg && dev->has_fr) {
498 fr_pool = srp_alloc_fr_pool(target);
499 if (IS_ERR(fr_pool)) {
500 ret = PTR_ERR(fr_pool);
501 shost_printk(KERN_WARNING, target->scsi_host, PFX
502 "FR pool allocation failed (%d)\n", ret);
503 goto err_qp;
504 }
505 if (target->fr_pool)
506 srp_destroy_fr_pool(target->fr_pool);
507 target->fr_pool = fr_pool;
508 } else if (!dev->use_fast_reg && dev->has_fmr) {
509 fmr_pool = srp_alloc_fmr_pool(target);
510 if (IS_ERR(fmr_pool)) {
511 ret = PTR_ERR(fmr_pool);
512 shost_printk(KERN_WARNING, target->scsi_host, PFX
513 "FMR pool allocation failed (%d)\n", ret);
514 goto err_qp;
515 }
516 if (target->fmr_pool)
517 ib_destroy_fmr_pool(target->fmr_pool);
518 target->fmr_pool = fmr_pool;
519 }
520
521 if (target->qp)
522 ib_destroy_qp(target->qp);
523 if (target->recv_cq)
524 ib_destroy_cq(target->recv_cq);
525 if (target->send_cq)
526 ib_destroy_cq(target->send_cq);
527
528 target->qp = qp;
529 target->recv_cq = recv_cq;
530 target->send_cq = send_cq;
531
532 kfree(init_attr);
533 return 0;
534
535 err_qp:
536 ib_destroy_qp(qp);
537
538 err_send_cq:
539 ib_destroy_cq(send_cq);
540
541 err_recv_cq:
542 ib_destroy_cq(recv_cq);
543
544 err:
545 kfree(init_attr);
546 return ret;
547 }
548
549 /*
550 * Note: this function may be called without srp_alloc_iu_bufs() having been
551 * invoked. Hence the target->[rt]x_ring checks.
552 */
553 static void srp_free_target_ib(struct srp_target_port *target)
554 {
555 struct srp_device *dev = target->srp_host->srp_dev;
556 int i;
557
558 if (target->cm_id) {
559 ib_destroy_cm_id(target->cm_id);
560 target->cm_id = NULL;
561 }
562
563 if (dev->use_fast_reg) {
564 if (target->fr_pool)
565 srp_destroy_fr_pool(target->fr_pool);
566 } else {
567 if (target->fmr_pool)
568 ib_destroy_fmr_pool(target->fmr_pool);
569 }
570 ib_destroy_qp(target->qp);
571 ib_destroy_cq(target->send_cq);
572 ib_destroy_cq(target->recv_cq);
573
574 target->qp = NULL;
575 target->send_cq = target->recv_cq = NULL;
576
577 if (target->rx_ring) {
578 for (i = 0; i < target->queue_size; ++i)
579 srp_free_iu(target->srp_host, target->rx_ring[i]);
580 kfree(target->rx_ring);
581 target->rx_ring = NULL;
582 }
583 if (target->tx_ring) {
584 for (i = 0; i < target->queue_size; ++i)
585 srp_free_iu(target->srp_host, target->tx_ring[i]);
586 kfree(target->tx_ring);
587 target->tx_ring = NULL;
588 }
589 }
590
591 static void srp_path_rec_completion(int status,
592 struct ib_sa_path_rec *pathrec,
593 void *target_ptr)
594 {
595 struct srp_target_port *target = target_ptr;
596
597 target->status = status;
598 if (status)
599 shost_printk(KERN_ERR, target->scsi_host,
600 PFX "Got failed path rec status %d\n", status);
601 else
602 target->path = *pathrec;
603 complete(&target->done);
604 }
605
606 static int srp_lookup_path(struct srp_target_port *target)
607 {
608 int ret;
609
610 target->path.numb_path = 1;
611
612 init_completion(&target->done);
613
614 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
615 target->srp_host->srp_dev->dev,
616 target->srp_host->port,
617 &target->path,
618 IB_SA_PATH_REC_SERVICE_ID |
619 IB_SA_PATH_REC_DGID |
620 IB_SA_PATH_REC_SGID |
621 IB_SA_PATH_REC_NUMB_PATH |
622 IB_SA_PATH_REC_PKEY,
623 SRP_PATH_REC_TIMEOUT_MS,
624 GFP_KERNEL,
625 srp_path_rec_completion,
626 target, &target->path_query);
627 if (target->path_query_id < 0)
628 return target->path_query_id;
629
630 ret = wait_for_completion_interruptible(&target->done);
631 if (ret < 0)
632 return ret;
633
634 if (target->status < 0)
635 shost_printk(KERN_WARNING, target->scsi_host,
636 PFX "Path record query failed\n");
637
638 return target->status;
639 }
640
641 static int srp_send_req(struct srp_target_port *target)
642 {
643 struct {
644 struct ib_cm_req_param param;
645 struct srp_login_req priv;
646 } *req = NULL;
647 int status;
648
649 req = kzalloc(sizeof *req, GFP_KERNEL);
650 if (!req)
651 return -ENOMEM;
652
653 req->param.primary_path = &target->path;
654 req->param.alternate_path = NULL;
655 req->param.service_id = target->service_id;
656 req->param.qp_num = target->qp->qp_num;
657 req->param.qp_type = target->qp->qp_type;
658 req->param.private_data = &req->priv;
659 req->param.private_data_len = sizeof req->priv;
660 req->param.flow_control = 1;
661
662 get_random_bytes(&req->param.starting_psn, 4);
663 req->param.starting_psn &= 0xffffff;
664
665 /*
666 * Pick some arbitrary defaults here; we could make these
667 * module parameters if anyone cared about setting them.
668 */
669 req->param.responder_resources = 4;
670 req->param.remote_cm_response_timeout = 20;
671 req->param.local_cm_response_timeout = 20;
672 req->param.retry_count = target->tl_retry_count;
673 req->param.rnr_retry_count = 7;
674 req->param.max_cm_retries = 15;
675
676 req->priv.opcode = SRP_LOGIN_REQ;
677 req->priv.tag = 0;
678 req->priv.req_it_iu_len = cpu_to_be32(target->max_iu_len);
679 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
680 SRP_BUF_FORMAT_INDIRECT);
681 /*
682 * In the published SRP specification (draft rev. 16a), the
683 * port identifier format is 8 bytes of ID extension followed
684 * by 8 bytes of GUID. Older drafts put the two halves in the
685 * opposite order, so that the GUID comes first.
686 *
687 * Targets conforming to these obsolete drafts can be
688 * recognized by the I/O Class they report.
689 */
690 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
691 memcpy(req->priv.initiator_port_id,
692 &target->path.sgid.global.interface_id, 8);
693 memcpy(req->priv.initiator_port_id + 8,
694 &target->initiator_ext, 8);
695 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
696 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
697 } else {
698 memcpy(req->priv.initiator_port_id,
699 &target->initiator_ext, 8);
700 memcpy(req->priv.initiator_port_id + 8,
701 &target->path.sgid.global.interface_id, 8);
702 memcpy(req->priv.target_port_id, &target->id_ext, 8);
703 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
704 }
705
706 /*
707 * Topspin/Cisco SRP targets will reject our login unless we
708 * zero out the first 8 bytes of our initiator port ID and set
709 * the second 8 bytes to the local node GUID.
710 */
711 if (srp_target_is_topspin(target)) {
712 shost_printk(KERN_DEBUG, target->scsi_host,
713 PFX "Topspin/Cisco initiator port ID workaround "
714 "activated for target GUID %016llx\n",
715 (unsigned long long) be64_to_cpu(target->ioc_guid));
716 memset(req->priv.initiator_port_id, 0, 8);
717 memcpy(req->priv.initiator_port_id + 8,
718 &target->srp_host->srp_dev->dev->node_guid, 8);
719 }
720
721 status = ib_send_cm_req(target->cm_id, &req->param);
722
723 kfree(req);
724
725 return status;
726 }
727
728 static bool srp_queue_remove_work(struct srp_target_port *target)
729 {
730 bool changed = false;
731
732 spin_lock_irq(&target->lock);
733 if (target->state != SRP_TARGET_REMOVED) {
734 target->state = SRP_TARGET_REMOVED;
735 changed = true;
736 }
737 spin_unlock_irq(&target->lock);
738
739 if (changed)
740 queue_work(srp_remove_wq, &target->remove_work);
741
742 return changed;
743 }
744
745 static bool srp_change_conn_state(struct srp_target_port *target,
746 bool connected)
747 {
748 bool changed = false;
749
750 spin_lock_irq(&target->lock);
751 if (target->connected != connected) {
752 target->connected = connected;
753 changed = true;
754 }
755 spin_unlock_irq(&target->lock);
756
757 return changed;
758 }
759
760 static void srp_disconnect_target(struct srp_target_port *target)
761 {
762 if (srp_change_conn_state(target, false)) {
763 /* XXX should send SRP_I_LOGOUT request */
764
765 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
766 shost_printk(KERN_DEBUG, target->scsi_host,
767 PFX "Sending CM DREQ failed\n");
768 }
769 }
770 }
771
772 static void srp_free_req_data(struct srp_target_port *target)
773 {
774 struct srp_device *dev = target->srp_host->srp_dev;
775 struct ib_device *ibdev = dev->dev;
776 struct srp_request *req;
777 int i;
778
779 if (!target->req_ring)
780 return;
781
782 for (i = 0; i < target->req_ring_size; ++i) {
783 req = &target->req_ring[i];
784 if (dev->use_fast_reg)
785 kfree(req->fr_list);
786 else
787 kfree(req->fmr_list);
788 kfree(req->map_page);
789 if (req->indirect_dma_addr) {
790 ib_dma_unmap_single(ibdev, req->indirect_dma_addr,
791 target->indirect_size,
792 DMA_TO_DEVICE);
793 }
794 kfree(req->indirect_desc);
795 }
796
797 kfree(target->req_ring);
798 target->req_ring = NULL;
799 }
800
801 static int srp_alloc_req_data(struct srp_target_port *target)
802 {
803 struct srp_device *srp_dev = target->srp_host->srp_dev;
804 struct ib_device *ibdev = srp_dev->dev;
805 struct srp_request *req;
806 void *mr_list;
807 dma_addr_t dma_addr;
808 int i, ret = -ENOMEM;
809
810 INIT_LIST_HEAD(&target->free_reqs);
811
812 target->req_ring = kzalloc(target->req_ring_size *
813 sizeof(*target->req_ring), GFP_KERNEL);
814 if (!target->req_ring)
815 goto out;
816
817 for (i = 0; i < target->req_ring_size; ++i) {
818 req = &target->req_ring[i];
819 mr_list = kmalloc(target->cmd_sg_cnt * sizeof(void *),
820 GFP_KERNEL);
821 if (!mr_list)
822 goto out;
823 if (srp_dev->use_fast_reg)
824 req->fr_list = mr_list;
825 else
826 req->fmr_list = mr_list;
827 req->map_page = kmalloc(srp_dev->max_pages_per_mr *
828 sizeof(void *), GFP_KERNEL);
829 if (!req->map_page)
830 goto out;
831 req->indirect_desc = kmalloc(target->indirect_size, GFP_KERNEL);
832 if (!req->indirect_desc)
833 goto out;
834
835 dma_addr = ib_dma_map_single(ibdev, req->indirect_desc,
836 target->indirect_size,
837 DMA_TO_DEVICE);
838 if (ib_dma_mapping_error(ibdev, dma_addr))
839 goto out;
840
841 req->indirect_dma_addr = dma_addr;
842 req->index = i;
843 list_add_tail(&req->list, &target->free_reqs);
844 }
845 ret = 0;
846
847 out:
848 return ret;
849 }
850
851 /**
852 * srp_del_scsi_host_attr() - Remove attributes defined in the host template.
853 * @shost: SCSI host whose attributes to remove from sysfs.
854 *
855 * Note: Any attributes defined in the host template and that did not exist
856 * before invocation of this function will be ignored.
857 */
858 static void srp_del_scsi_host_attr(struct Scsi_Host *shost)
859 {
860 struct device_attribute **attr;
861
862 for (attr = shost->hostt->shost_attrs; attr && *attr; ++attr)
863 device_remove_file(&shost->shost_dev, *attr);
864 }
865
866 static void srp_remove_target(struct srp_target_port *target)
867 {
868 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
869
870 srp_del_scsi_host_attr(target->scsi_host);
871 srp_rport_get(target->rport);
872 srp_remove_host(target->scsi_host);
873 scsi_remove_host(target->scsi_host);
874 srp_stop_rport_timers(target->rport);
875 srp_disconnect_target(target);
876 srp_free_target_ib(target);
877 cancel_work_sync(&target->tl_err_work);
878 srp_rport_put(target->rport);
879 srp_free_req_data(target);
880
881 spin_lock(&target->srp_host->target_lock);
882 list_del(&target->list);
883 spin_unlock(&target->srp_host->target_lock);
884
885 scsi_host_put(target->scsi_host);
886 }
887
888 static void srp_remove_work(struct work_struct *work)
889 {
890 struct srp_target_port *target =
891 container_of(work, struct srp_target_port, remove_work);
892
893 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
894
895 srp_remove_target(target);
896 }
897
898 static void srp_rport_delete(struct srp_rport *rport)
899 {
900 struct srp_target_port *target = rport->lld_data;
901
902 srp_queue_remove_work(target);
903 }
904
905 static int srp_connect_target(struct srp_target_port *target)
906 {
907 int retries = 3;
908 int ret;
909
910 WARN_ON_ONCE(target->connected);
911
912 target->qp_in_error = false;
913
914 ret = srp_lookup_path(target);
915 if (ret)
916 return ret;
917
918 while (1) {
919 init_completion(&target->done);
920 ret = srp_send_req(target);
921 if (ret)
922 return ret;
923 ret = wait_for_completion_interruptible(&target->done);
924 if (ret < 0)
925 return ret;
926
927 /*
928 * The CM event handling code will set status to
929 * SRP_PORT_REDIRECT if we get a port redirect REJ
930 * back, or SRP_DLID_REDIRECT if we get a lid/qp
931 * redirect REJ back.
932 */
933 switch (target->status) {
934 case 0:
935 srp_change_conn_state(target, true);
936 return 0;
937
938 case SRP_PORT_REDIRECT:
939 ret = srp_lookup_path(target);
940 if (ret)
941 return ret;
942 break;
943
944 case SRP_DLID_REDIRECT:
945 break;
946
947 case SRP_STALE_CONN:
948 /* Our current CM id was stale, and is now in timewait.
949 * Try to reconnect with a new one.
950 */
951 if (!retries-- || srp_new_cm_id(target)) {
952 shost_printk(KERN_ERR, target->scsi_host, PFX
953 "giving up on stale connection\n");
954 target->status = -ECONNRESET;
955 return target->status;
956 }
957
958 shost_printk(KERN_ERR, target->scsi_host, PFX
959 "retrying stale connection\n");
960 break;
961
962 default:
963 return target->status;
964 }
965 }
966 }
967
968 static int srp_inv_rkey(struct srp_target_port *target, u32 rkey)
969 {
970 struct ib_send_wr *bad_wr;
971 struct ib_send_wr wr = {
972 .opcode = IB_WR_LOCAL_INV,
973 .wr_id = LOCAL_INV_WR_ID_MASK,
974 .next = NULL,
975 .num_sge = 0,
976 .send_flags = 0,
977 .ex.invalidate_rkey = rkey,
978 };
979
980 return ib_post_send(target->qp, &wr, &bad_wr);
981 }
982
983 static void srp_unmap_data(struct scsi_cmnd *scmnd,
984 struct srp_target_port *target,
985 struct srp_request *req)
986 {
987 struct srp_device *dev = target->srp_host->srp_dev;
988 struct ib_device *ibdev = dev->dev;
989 int i, res;
990
991 if (!scsi_sglist(scmnd) ||
992 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
993 scmnd->sc_data_direction != DMA_FROM_DEVICE))
994 return;
995
996 if (dev->use_fast_reg) {
997 struct srp_fr_desc **pfr;
998
999 for (i = req->nmdesc, pfr = req->fr_list; i > 0; i--, pfr++) {
1000 res = srp_inv_rkey(target, (*pfr)->mr->rkey);
1001 if (res < 0) {
1002 shost_printk(KERN_ERR, target->scsi_host, PFX
1003 "Queueing INV WR for rkey %#x failed (%d)\n",
1004 (*pfr)->mr->rkey, res);
1005 queue_work(system_long_wq,
1006 &target->tl_err_work);
1007 }
1008 }
1009 if (req->nmdesc)
1010 srp_fr_pool_put(target->fr_pool, req->fr_list,
1011 req->nmdesc);
1012 } else {
1013 struct ib_pool_fmr **pfmr;
1014
1015 for (i = req->nmdesc, pfmr = req->fmr_list; i > 0; i--, pfmr++)
1016 ib_fmr_pool_unmap(*pfmr);
1017 }
1018
1019 ib_dma_unmap_sg(ibdev, scsi_sglist(scmnd), scsi_sg_count(scmnd),
1020 scmnd->sc_data_direction);
1021 }
1022
1023 /**
1024 * srp_claim_req - Take ownership of the scmnd associated with a request.
1025 * @target: SRP target port.
1026 * @req: SRP request.
1027 * @sdev: If not NULL, only take ownership for this SCSI device.
1028 * @scmnd: If NULL, take ownership of @req->scmnd. If not NULL, only take
1029 * ownership of @req->scmnd if it equals @scmnd.
1030 *
1031 * Return value:
1032 * Either NULL or a pointer to the SCSI command the caller became owner of.
1033 */
1034 static struct scsi_cmnd *srp_claim_req(struct srp_target_port *target,
1035 struct srp_request *req,
1036 struct scsi_device *sdev,
1037 struct scsi_cmnd *scmnd)
1038 {
1039 unsigned long flags;
1040
1041 spin_lock_irqsave(&target->lock, flags);
1042 if (req->scmnd &&
1043 (!sdev || req->scmnd->device == sdev) &&
1044 (!scmnd || req->scmnd == scmnd)) {
1045 scmnd = req->scmnd;
1046 req->scmnd = NULL;
1047 } else {
1048 scmnd = NULL;
1049 }
1050 spin_unlock_irqrestore(&target->lock, flags);
1051
1052 return scmnd;
1053 }
1054
1055 /**
1056 * srp_free_req() - Unmap data and add request to the free request list.
1057 * @target: SRP target port.
1058 * @req: Request to be freed.
1059 * @scmnd: SCSI command associated with @req.
1060 * @req_lim_delta: Amount to be added to @target->req_lim.
1061 */
1062 static void srp_free_req(struct srp_target_port *target,
1063 struct srp_request *req, struct scsi_cmnd *scmnd,
1064 s32 req_lim_delta)
1065 {
1066 unsigned long flags;
1067
1068 srp_unmap_data(scmnd, target, req);
1069
1070 spin_lock_irqsave(&target->lock, flags);
1071 target->req_lim += req_lim_delta;
1072 list_add_tail(&req->list, &target->free_reqs);
1073 spin_unlock_irqrestore(&target->lock, flags);
1074 }
1075
1076 static void srp_finish_req(struct srp_target_port *target,
1077 struct srp_request *req, struct scsi_device *sdev,
1078 int result)
1079 {
1080 struct scsi_cmnd *scmnd = srp_claim_req(target, req, sdev, NULL);
1081
1082 if (scmnd) {
1083 srp_free_req(target, req, scmnd, 0);
1084 scmnd->result = result;
1085 scmnd->scsi_done(scmnd);
1086 }
1087 }
1088
1089 static void srp_terminate_io(struct srp_rport *rport)
1090 {
1091 struct srp_target_port *target = rport->lld_data;
1092 struct Scsi_Host *shost = target->scsi_host;
1093 struct scsi_device *sdev;
1094 int i;
1095
1096 /*
1097 * Invoking srp_terminate_io() while srp_queuecommand() is running
1098 * is not safe. Hence the warning statement below.
1099 */
1100 shost_for_each_device(sdev, shost)
1101 WARN_ON_ONCE(sdev->request_queue->request_fn_active);
1102
1103 for (i = 0; i < target->req_ring_size; ++i) {
1104 struct srp_request *req = &target->req_ring[i];
1105 srp_finish_req(target, req, NULL, DID_TRANSPORT_FAILFAST << 16);
1106 }
1107 }
1108
1109 /*
1110 * It is up to the caller to ensure that srp_rport_reconnect() calls are
1111 * serialized and that no concurrent srp_queuecommand(), srp_abort(),
1112 * srp_reset_device() or srp_reset_host() calls will occur while this function
1113 * is in progress. One way to realize that is not to call this function
1114 * directly but to call srp_reconnect_rport() instead since that last function
1115 * serializes calls of this function via rport->mutex and also blocks
1116 * srp_queuecommand() calls before invoking this function.
1117 */
1118 static int srp_rport_reconnect(struct srp_rport *rport)
1119 {
1120 struct srp_target_port *target = rport->lld_data;
1121 int i, ret;
1122
1123 srp_disconnect_target(target);
1124 /*
1125 * Now get a new local CM ID so that we avoid confusing the target in
1126 * case things are really fouled up. Doing so also ensures that all CM
1127 * callbacks will have finished before a new QP is allocated.
1128 */
1129 ret = srp_new_cm_id(target);
1130
1131 for (i = 0; i < target->req_ring_size; ++i) {
1132 struct srp_request *req = &target->req_ring[i];
1133 srp_finish_req(target, req, NULL, DID_RESET << 16);
1134 }
1135
1136 /*
1137 * Whether or not creating a new CM ID succeeded, create a new
1138 * QP. This guarantees that all callback functions for the old QP have
1139 * finished before any send requests are posted on the new QP.
1140 */
1141 ret += srp_create_target_ib(target);
1142
1143 INIT_LIST_HEAD(&target->free_tx);
1144 for (i = 0; i < target->queue_size; ++i)
1145 list_add(&target->tx_ring[i]->list, &target->free_tx);
1146
1147 if (ret == 0)
1148 ret = srp_connect_target(target);
1149
1150 if (ret == 0)
1151 shost_printk(KERN_INFO, target->scsi_host,
1152 PFX "reconnect succeeded\n");
1153
1154 return ret;
1155 }
1156
1157 static void srp_map_desc(struct srp_map_state *state, dma_addr_t dma_addr,
1158 unsigned int dma_len, u32 rkey)
1159 {
1160 struct srp_direct_buf *desc = state->desc;
1161
1162 desc->va = cpu_to_be64(dma_addr);
1163 desc->key = cpu_to_be32(rkey);
1164 desc->len = cpu_to_be32(dma_len);
1165
1166 state->total_len += dma_len;
1167 state->desc++;
1168 state->ndesc++;
1169 }
1170
1171 static int srp_map_finish_fmr(struct srp_map_state *state,
1172 struct srp_target_port *target)
1173 {
1174 struct ib_pool_fmr *fmr;
1175 u64 io_addr = 0;
1176
1177 fmr = ib_fmr_pool_map_phys(target->fmr_pool, state->pages,
1178 state->npages, io_addr);
1179 if (IS_ERR(fmr))
1180 return PTR_ERR(fmr);
1181
1182 *state->next_fmr++ = fmr;
1183 state->nmdesc++;
1184
1185 srp_map_desc(state, 0, state->dma_len, fmr->fmr->rkey);
1186
1187 return 0;
1188 }
1189
1190 static int srp_map_finish_fr(struct srp_map_state *state,
1191 struct srp_target_port *target)
1192 {
1193 struct srp_device *dev = target->srp_host->srp_dev;
1194 struct ib_send_wr *bad_wr;
1195 struct ib_send_wr wr;
1196 struct srp_fr_desc *desc;
1197 u32 rkey;
1198
1199 desc = srp_fr_pool_get(target->fr_pool);
1200 if (!desc)
1201 return -ENOMEM;
1202
1203 rkey = ib_inc_rkey(desc->mr->rkey);
1204 ib_update_fast_reg_key(desc->mr, rkey);
1205
1206 memcpy(desc->frpl->page_list, state->pages,
1207 sizeof(state->pages[0]) * state->npages);
1208
1209 memset(&wr, 0, sizeof(wr));
1210 wr.opcode = IB_WR_FAST_REG_MR;
1211 wr.wr_id = FAST_REG_WR_ID_MASK;
1212 wr.wr.fast_reg.iova_start = state->base_dma_addr;
1213 wr.wr.fast_reg.page_list = desc->frpl;
1214 wr.wr.fast_reg.page_list_len = state->npages;
1215 wr.wr.fast_reg.page_shift = ilog2(dev->mr_page_size);
1216 wr.wr.fast_reg.length = state->dma_len;
1217 wr.wr.fast_reg.access_flags = (IB_ACCESS_LOCAL_WRITE |
1218 IB_ACCESS_REMOTE_READ |
1219 IB_ACCESS_REMOTE_WRITE);
1220 wr.wr.fast_reg.rkey = desc->mr->lkey;
1221
1222 *state->next_fr++ = desc;
1223 state->nmdesc++;
1224
1225 srp_map_desc(state, state->base_dma_addr, state->dma_len,
1226 desc->mr->rkey);
1227
1228 return ib_post_send(target->qp, &wr, &bad_wr);
1229 }
1230
1231 static int srp_finish_mapping(struct srp_map_state *state,
1232 struct srp_target_port *target)
1233 {
1234 int ret = 0;
1235
1236 if (state->npages == 0)
1237 return 0;
1238
1239 if (state->npages == 1 && !register_always)
1240 srp_map_desc(state, state->base_dma_addr, state->dma_len,
1241 target->rkey);
1242 else
1243 ret = target->srp_host->srp_dev->use_fast_reg ?
1244 srp_map_finish_fr(state, target) :
1245 srp_map_finish_fmr(state, target);
1246
1247 if (ret == 0) {
1248 state->npages = 0;
1249 state->dma_len = 0;
1250 }
1251
1252 return ret;
1253 }
1254
1255 static void srp_map_update_start(struct srp_map_state *state,
1256 struct scatterlist *sg, int sg_index,
1257 dma_addr_t dma_addr)
1258 {
1259 state->unmapped_sg = sg;
1260 state->unmapped_index = sg_index;
1261 state->unmapped_addr = dma_addr;
1262 }
1263
1264 static int srp_map_sg_entry(struct srp_map_state *state,
1265 struct srp_target_port *target,
1266 struct scatterlist *sg, int sg_index,
1267 bool use_mr)
1268 {
1269 struct srp_device *dev = target->srp_host->srp_dev;
1270 struct ib_device *ibdev = dev->dev;
1271 dma_addr_t dma_addr = ib_sg_dma_address(ibdev, sg);
1272 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
1273 unsigned int len;
1274 int ret;
1275
1276 if (!dma_len)
1277 return 0;
1278
1279 if (!use_mr) {
1280 /*
1281 * Once we're in direct map mode for a request, we don't
1282 * go back to FMR or FR mode, so no need to update anything
1283 * other than the descriptor.
1284 */
1285 srp_map_desc(state, dma_addr, dma_len, target->rkey);
1286 return 0;
1287 }
1288
1289 /*
1290 * Since not all RDMA HW drivers support non-zero page offsets for
1291 * FMR, if we start at an offset into a page, don't merge into the
1292 * current FMR mapping. Finish it out, and use the kernel's MR for
1293 * this sg entry.
1294 */
1295 if ((!dev->use_fast_reg && dma_addr & ~dev->mr_page_mask) ||
1296 dma_len > dev->mr_max_size) {
1297 ret = srp_finish_mapping(state, target);
1298 if (ret)
1299 return ret;
1300
1301 srp_map_desc(state, dma_addr, dma_len, target->rkey);
1302 srp_map_update_start(state, NULL, 0, 0);
1303 return 0;
1304 }
1305
1306 /*
1307 * If this is the first sg that will be mapped via FMR or via FR, save
1308 * our position. We need to know the first unmapped entry, its index,
1309 * and the first unmapped address within that entry to be able to
1310 * restart mapping after an error.
1311 */
1312 if (!state->unmapped_sg)
1313 srp_map_update_start(state, sg, sg_index, dma_addr);
1314
1315 while (dma_len) {
1316 unsigned offset = dma_addr & ~dev->mr_page_mask;
1317 if (state->npages == dev->max_pages_per_mr || offset != 0) {
1318 ret = srp_finish_mapping(state, target);
1319 if (ret)
1320 return ret;
1321
1322 srp_map_update_start(state, sg, sg_index, dma_addr);
1323 }
1324
1325 len = min_t(unsigned int, dma_len, dev->mr_page_size - offset);
1326
1327 if (!state->npages)
1328 state->base_dma_addr = dma_addr;
1329 state->pages[state->npages++] = dma_addr & dev->mr_page_mask;
1330 state->dma_len += len;
1331 dma_addr += len;
1332 dma_len -= len;
1333 }
1334
1335 /*
1336 * If the last entry of the MR wasn't a full page, then we need to
1337 * close it out and start a new one -- we can only merge at page
1338 * boundries.
1339 */
1340 ret = 0;
1341 if (len != dev->mr_page_size) {
1342 ret = srp_finish_mapping(state, target);
1343 if (!ret)
1344 srp_map_update_start(state, NULL, 0, 0);
1345 }
1346 return ret;
1347 }
1348
1349 static int srp_map_sg(struct srp_map_state *state,
1350 struct srp_target_port *target, struct srp_request *req,
1351 struct scatterlist *scat, int count)
1352 {
1353 struct srp_device *dev = target->srp_host->srp_dev;
1354 struct ib_device *ibdev = dev->dev;
1355 struct scatterlist *sg;
1356 int i;
1357 bool use_mr;
1358
1359 state->desc = req->indirect_desc;
1360 state->pages = req->map_page;
1361 if (dev->use_fast_reg) {
1362 state->next_fr = req->fr_list;
1363 use_mr = !!target->fr_pool;
1364 } else {
1365 state->next_fmr = req->fmr_list;
1366 use_mr = !!target->fmr_pool;
1367 }
1368
1369 for_each_sg(scat, sg, count, i) {
1370 if (srp_map_sg_entry(state, target, sg, i, use_mr)) {
1371 /*
1372 * Memory registration failed, so backtrack to the
1373 * first unmapped entry and continue on without using
1374 * memory registration.
1375 */
1376 dma_addr_t dma_addr;
1377 unsigned int dma_len;
1378
1379 backtrack:
1380 sg = state->unmapped_sg;
1381 i = state->unmapped_index;
1382
1383 dma_addr = ib_sg_dma_address(ibdev, sg);
1384 dma_len = ib_sg_dma_len(ibdev, sg);
1385 dma_len -= (state->unmapped_addr - dma_addr);
1386 dma_addr = state->unmapped_addr;
1387 use_mr = false;
1388 srp_map_desc(state, dma_addr, dma_len, target->rkey);
1389 }
1390 }
1391
1392 if (use_mr && srp_finish_mapping(state, target))
1393 goto backtrack;
1394
1395 req->nmdesc = state->nmdesc;
1396
1397 return 0;
1398 }
1399
1400 static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
1401 struct srp_request *req)
1402 {
1403 struct scatterlist *scat;
1404 struct srp_cmd *cmd = req->cmd->buf;
1405 int len, nents, count;
1406 struct srp_device *dev;
1407 struct ib_device *ibdev;
1408 struct srp_map_state state;
1409 struct srp_indirect_buf *indirect_hdr;
1410 u32 table_len;
1411 u8 fmt;
1412
1413 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
1414 return sizeof (struct srp_cmd);
1415
1416 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
1417 scmnd->sc_data_direction != DMA_TO_DEVICE) {
1418 shost_printk(KERN_WARNING, target->scsi_host,
1419 PFX "Unhandled data direction %d\n",
1420 scmnd->sc_data_direction);
1421 return -EINVAL;
1422 }
1423
1424 nents = scsi_sg_count(scmnd);
1425 scat = scsi_sglist(scmnd);
1426
1427 dev = target->srp_host->srp_dev;
1428 ibdev = dev->dev;
1429
1430 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
1431 if (unlikely(count == 0))
1432 return -EIO;
1433
1434 fmt = SRP_DATA_DESC_DIRECT;
1435 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
1436
1437 if (count == 1 && !register_always) {
1438 /*
1439 * The midlayer only generated a single gather/scatter
1440 * entry, or DMA mapping coalesced everything to a
1441 * single entry. So a direct descriptor along with
1442 * the DMA MR suffices.
1443 */
1444 struct srp_direct_buf *buf = (void *) cmd->add_data;
1445
1446 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
1447 buf->key = cpu_to_be32(target->rkey);
1448 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
1449
1450 req->nmdesc = 0;
1451 goto map_complete;
1452 }
1453
1454 /*
1455 * We have more than one scatter/gather entry, so build our indirect
1456 * descriptor table, trying to merge as many entries as we can.
1457 */
1458 indirect_hdr = (void *) cmd->add_data;
1459
1460 ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr,
1461 target->indirect_size, DMA_TO_DEVICE);
1462
1463 memset(&state, 0, sizeof(state));
1464 srp_map_sg(&state, target, req, scat, count);
1465
1466 /* We've mapped the request, now pull as much of the indirect
1467 * descriptor table as we can into the command buffer. If this
1468 * target is not using an external indirect table, we are
1469 * guaranteed to fit into the command, as the SCSI layer won't
1470 * give us more S/G entries than we allow.
1471 */
1472 if (state.ndesc == 1) {
1473 /*
1474 * Memory registration collapsed the sg-list into one entry,
1475 * so use a direct descriptor.
1476 */
1477 struct srp_direct_buf *buf = (void *) cmd->add_data;
1478
1479 *buf = req->indirect_desc[0];
1480 goto map_complete;
1481 }
1482
1483 if (unlikely(target->cmd_sg_cnt < state.ndesc &&
1484 !target->allow_ext_sg)) {
1485 shost_printk(KERN_ERR, target->scsi_host,
1486 "Could not fit S/G list into SRP_CMD\n");
1487 return -EIO;
1488 }
1489
1490 count = min(state.ndesc, target->cmd_sg_cnt);
1491 table_len = state.ndesc * sizeof (struct srp_direct_buf);
1492
1493 fmt = SRP_DATA_DESC_INDIRECT;
1494 len = sizeof(struct srp_cmd) + sizeof (struct srp_indirect_buf);
1495 len += count * sizeof (struct srp_direct_buf);
1496
1497 memcpy(indirect_hdr->desc_list, req->indirect_desc,
1498 count * sizeof (struct srp_direct_buf));
1499
1500 indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr);
1501 indirect_hdr->table_desc.key = cpu_to_be32(target->rkey);
1502 indirect_hdr->table_desc.len = cpu_to_be32(table_len);
1503 indirect_hdr->len = cpu_to_be32(state.total_len);
1504
1505 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
1506 cmd->data_out_desc_cnt = count;
1507 else
1508 cmd->data_in_desc_cnt = count;
1509
1510 ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len,
1511 DMA_TO_DEVICE);
1512
1513 map_complete:
1514 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
1515 cmd->buf_fmt = fmt << 4;
1516 else
1517 cmd->buf_fmt = fmt;
1518
1519 return len;
1520 }
1521
1522 /*
1523 * Return an IU and possible credit to the free pool
1524 */
1525 static void srp_put_tx_iu(struct srp_target_port *target, struct srp_iu *iu,
1526 enum srp_iu_type iu_type)
1527 {
1528 unsigned long flags;
1529
1530 spin_lock_irqsave(&target->lock, flags);
1531 list_add(&iu->list, &target->free_tx);
1532 if (iu_type != SRP_IU_RSP)
1533 ++target->req_lim;
1534 spin_unlock_irqrestore(&target->lock, flags);
1535 }
1536
1537 /*
1538 * Must be called with target->lock held to protect req_lim and free_tx.
1539 * If IU is not sent, it must be returned using srp_put_tx_iu().
1540 *
1541 * Note:
1542 * An upper limit for the number of allocated information units for each
1543 * request type is:
1544 * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues
1545 * more than Scsi_Host.can_queue requests.
1546 * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE.
1547 * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than
1548 * one unanswered SRP request to an initiator.
1549 */
1550 static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
1551 enum srp_iu_type iu_type)
1552 {
1553 s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE;
1554 struct srp_iu *iu;
1555
1556 srp_send_completion(target->send_cq, target);
1557
1558 if (list_empty(&target->free_tx))
1559 return NULL;
1560
1561 /* Initiator responses to target requests do not consume credits */
1562 if (iu_type != SRP_IU_RSP) {
1563 if (target->req_lim <= rsv) {
1564 ++target->zero_req_lim;
1565 return NULL;
1566 }
1567
1568 --target->req_lim;
1569 }
1570
1571 iu = list_first_entry(&target->free_tx, struct srp_iu, list);
1572 list_del(&iu->list);
1573 return iu;
1574 }
1575
1576 static int srp_post_send(struct srp_target_port *target,
1577 struct srp_iu *iu, int len)
1578 {
1579 struct ib_sge list;
1580 struct ib_send_wr wr, *bad_wr;
1581
1582 list.addr = iu->dma;
1583 list.length = len;
1584 list.lkey = target->lkey;
1585
1586 wr.next = NULL;
1587 wr.wr_id = (uintptr_t) iu;
1588 wr.sg_list = &list;
1589 wr.num_sge = 1;
1590 wr.opcode = IB_WR_SEND;
1591 wr.send_flags = IB_SEND_SIGNALED;
1592
1593 return ib_post_send(target->qp, &wr, &bad_wr);
1594 }
1595
1596 static int srp_post_recv(struct srp_target_port *target, struct srp_iu *iu)
1597 {
1598 struct ib_recv_wr wr, *bad_wr;
1599 struct ib_sge list;
1600
1601 list.addr = iu->dma;
1602 list.length = iu->size;
1603 list.lkey = target->lkey;
1604
1605 wr.next = NULL;
1606 wr.wr_id = (uintptr_t) iu;
1607 wr.sg_list = &list;
1608 wr.num_sge = 1;
1609
1610 return ib_post_recv(target->qp, &wr, &bad_wr);
1611 }
1612
1613 static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
1614 {
1615 struct srp_request *req;
1616 struct scsi_cmnd *scmnd;
1617 unsigned long flags;
1618
1619 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
1620 spin_lock_irqsave(&target->lock, flags);
1621 target->req_lim += be32_to_cpu(rsp->req_lim_delta);
1622 spin_unlock_irqrestore(&target->lock, flags);
1623
1624 target->tsk_mgmt_status = -1;
1625 if (be32_to_cpu(rsp->resp_data_len) >= 4)
1626 target->tsk_mgmt_status = rsp->data[3];
1627 complete(&target->tsk_mgmt_done);
1628 } else {
1629 req = &target->req_ring[rsp->tag];
1630 scmnd = srp_claim_req(target, req, NULL, NULL);
1631 if (!scmnd) {
1632 shost_printk(KERN_ERR, target->scsi_host,
1633 "Null scmnd for RSP w/tag %016llx\n",
1634 (unsigned long long) rsp->tag);
1635
1636 spin_lock_irqsave(&target->lock, flags);
1637 target->req_lim += be32_to_cpu(rsp->req_lim_delta);
1638 spin_unlock_irqrestore(&target->lock, flags);
1639
1640 return;
1641 }
1642 scmnd->result = rsp->status;
1643
1644 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
1645 memcpy(scmnd->sense_buffer, rsp->data +
1646 be32_to_cpu(rsp->resp_data_len),
1647 min_t(int, be32_to_cpu(rsp->sense_data_len),
1648 SCSI_SENSE_BUFFERSIZE));
1649 }
1650
1651 if (unlikely(rsp->flags & SRP_RSP_FLAG_DIUNDER))
1652 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
1653 else if (unlikely(rsp->flags & SRP_RSP_FLAG_DIOVER))
1654 scsi_set_resid(scmnd, -be32_to_cpu(rsp->data_in_res_cnt));
1655 else if (unlikely(rsp->flags & SRP_RSP_FLAG_DOUNDER))
1656 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
1657 else if (unlikely(rsp->flags & SRP_RSP_FLAG_DOOVER))
1658 scsi_set_resid(scmnd, -be32_to_cpu(rsp->data_out_res_cnt));
1659
1660 srp_free_req(target, req, scmnd,
1661 be32_to_cpu(rsp->req_lim_delta));
1662
1663 scmnd->host_scribble = NULL;
1664 scmnd->scsi_done(scmnd);
1665 }
1666 }
1667
1668 static int srp_response_common(struct srp_target_port *target, s32 req_delta,
1669 void *rsp, int len)
1670 {
1671 struct ib_device *dev = target->srp_host->srp_dev->dev;
1672 unsigned long flags;
1673 struct srp_iu *iu;
1674 int err;
1675
1676 spin_lock_irqsave(&target->lock, flags);
1677 target->req_lim += req_delta;
1678 iu = __srp_get_tx_iu(target, SRP_IU_RSP);
1679 spin_unlock_irqrestore(&target->lock, flags);
1680
1681 if (!iu) {
1682 shost_printk(KERN_ERR, target->scsi_host, PFX
1683 "no IU available to send response\n");
1684 return 1;
1685 }
1686
1687 ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE);
1688 memcpy(iu->buf, rsp, len);
1689 ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE);
1690
1691 err = srp_post_send(target, iu, len);
1692 if (err) {
1693 shost_printk(KERN_ERR, target->scsi_host, PFX
1694 "unable to post response: %d\n", err);
1695 srp_put_tx_iu(target, iu, SRP_IU_RSP);
1696 }
1697
1698 return err;
1699 }
1700
1701 static void srp_process_cred_req(struct srp_target_port *target,
1702 struct srp_cred_req *req)
1703 {
1704 struct srp_cred_rsp rsp = {
1705 .opcode = SRP_CRED_RSP,
1706 .tag = req->tag,
1707 };
1708 s32 delta = be32_to_cpu(req->req_lim_delta);
1709
1710 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1711 shost_printk(KERN_ERR, target->scsi_host, PFX
1712 "problems processing SRP_CRED_REQ\n");
1713 }
1714
1715 static void srp_process_aer_req(struct srp_target_port *target,
1716 struct srp_aer_req *req)
1717 {
1718 struct srp_aer_rsp rsp = {
1719 .opcode = SRP_AER_RSP,
1720 .tag = req->tag,
1721 };
1722 s32 delta = be32_to_cpu(req->req_lim_delta);
1723
1724 shost_printk(KERN_ERR, target->scsi_host, PFX
1725 "ignoring AER for LUN %llu\n", be64_to_cpu(req->lun));
1726
1727 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1728 shost_printk(KERN_ERR, target->scsi_host, PFX
1729 "problems processing SRP_AER_REQ\n");
1730 }
1731
1732 static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
1733 {
1734 struct ib_device *dev = target->srp_host->srp_dev->dev;
1735 struct srp_iu *iu = (struct srp_iu *) (uintptr_t) wc->wr_id;
1736 int res;
1737 u8 opcode;
1738
1739 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
1740 DMA_FROM_DEVICE);
1741
1742 opcode = *(u8 *) iu->buf;
1743
1744 if (0) {
1745 shost_printk(KERN_ERR, target->scsi_host,
1746 PFX "recv completion, opcode 0x%02x\n", opcode);
1747 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1,
1748 iu->buf, wc->byte_len, true);
1749 }
1750
1751 switch (opcode) {
1752 case SRP_RSP:
1753 srp_process_rsp(target, iu->buf);
1754 break;
1755
1756 case SRP_CRED_REQ:
1757 srp_process_cred_req(target, iu->buf);
1758 break;
1759
1760 case SRP_AER_REQ:
1761 srp_process_aer_req(target, iu->buf);
1762 break;
1763
1764 case SRP_T_LOGOUT:
1765 /* XXX Handle target logout */
1766 shost_printk(KERN_WARNING, target->scsi_host,
1767 PFX "Got target logout request\n");
1768 break;
1769
1770 default:
1771 shost_printk(KERN_WARNING, target->scsi_host,
1772 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
1773 break;
1774 }
1775
1776 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
1777 DMA_FROM_DEVICE);
1778
1779 res = srp_post_recv(target, iu);
1780 if (res != 0)
1781 shost_printk(KERN_ERR, target->scsi_host,
1782 PFX "Recv failed with error code %d\n", res);
1783 }
1784
1785 /**
1786 * srp_tl_err_work() - handle a transport layer error
1787 * @work: Work structure embedded in an SRP target port.
1788 *
1789 * Note: This function may get invoked before the rport has been created,
1790 * hence the target->rport test.
1791 */
1792 static void srp_tl_err_work(struct work_struct *work)
1793 {
1794 struct srp_target_port *target;
1795
1796 target = container_of(work, struct srp_target_port, tl_err_work);
1797 if (target->rport)
1798 srp_start_tl_fail_timers(target->rport);
1799 }
1800
1801 static void srp_handle_qp_err(u64 wr_id, enum ib_wc_status wc_status,
1802 bool send_err, struct srp_target_port *target)
1803 {
1804 if (target->connected && !target->qp_in_error) {
1805 if (wr_id & LOCAL_INV_WR_ID_MASK) {
1806 shost_printk(KERN_ERR, target->scsi_host, PFX
1807 "LOCAL_INV failed with status %d\n",
1808 wc_status);
1809 } else if (wr_id & FAST_REG_WR_ID_MASK) {
1810 shost_printk(KERN_ERR, target->scsi_host, PFX
1811 "FAST_REG_MR failed status %d\n",
1812 wc_status);
1813 } else {
1814 shost_printk(KERN_ERR, target->scsi_host,
1815 PFX "failed %s status %d for iu %p\n",
1816 send_err ? "send" : "receive",
1817 wc_status, (void *)(uintptr_t)wr_id);
1818 }
1819 queue_work(system_long_wq, &target->tl_err_work);
1820 }
1821 target->qp_in_error = true;
1822 }
1823
1824 static void srp_recv_completion(struct ib_cq *cq, void *target_ptr)
1825 {
1826 struct srp_target_port *target = target_ptr;
1827 struct ib_wc wc;
1828
1829 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1830 while (ib_poll_cq(cq, 1, &wc) > 0) {
1831 if (likely(wc.status == IB_WC_SUCCESS)) {
1832 srp_handle_recv(target, &wc);
1833 } else {
1834 srp_handle_qp_err(wc.wr_id, wc.status, false, target);
1835 }
1836 }
1837 }
1838
1839 static void srp_send_completion(struct ib_cq *cq, void *target_ptr)
1840 {
1841 struct srp_target_port *target = target_ptr;
1842 struct ib_wc wc;
1843 struct srp_iu *iu;
1844
1845 while (ib_poll_cq(cq, 1, &wc) > 0) {
1846 if (likely(wc.status == IB_WC_SUCCESS)) {
1847 iu = (struct srp_iu *) (uintptr_t) wc.wr_id;
1848 list_add(&iu->list, &target->free_tx);
1849 } else {
1850 srp_handle_qp_err(wc.wr_id, wc.status, true, target);
1851 }
1852 }
1853 }
1854
1855 static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd)
1856 {
1857 struct srp_target_port *target = host_to_target(shost);
1858 struct srp_rport *rport = target->rport;
1859 struct srp_request *req;
1860 struct srp_iu *iu;
1861 struct srp_cmd *cmd;
1862 struct ib_device *dev;
1863 unsigned long flags;
1864 int len, ret;
1865 const bool in_scsi_eh = !in_interrupt() && current == shost->ehandler;
1866
1867 /*
1868 * The SCSI EH thread is the only context from which srp_queuecommand()
1869 * can get invoked for blocked devices (SDEV_BLOCK /
1870 * SDEV_CREATED_BLOCK). Avoid racing with srp_reconnect_rport() by
1871 * locking the rport mutex if invoked from inside the SCSI EH.
1872 */
1873 if (in_scsi_eh)
1874 mutex_lock(&rport->mutex);
1875
1876 scmnd->result = srp_chkready(target->rport);
1877 if (unlikely(scmnd->result))
1878 goto err;
1879
1880 spin_lock_irqsave(&target->lock, flags);
1881 iu = __srp_get_tx_iu(target, SRP_IU_CMD);
1882 if (!iu)
1883 goto err_unlock;
1884
1885 req = list_first_entry(&target->free_reqs, struct srp_request, list);
1886 list_del(&req->list);
1887 spin_unlock_irqrestore(&target->lock, flags);
1888
1889 dev = target->srp_host->srp_dev->dev;
1890 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_iu_len,
1891 DMA_TO_DEVICE);
1892
1893 scmnd->host_scribble = (void *) req;
1894
1895 cmd = iu->buf;
1896 memset(cmd, 0, sizeof *cmd);
1897
1898 cmd->opcode = SRP_CMD;
1899 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
1900 cmd->tag = req->index;
1901 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1902
1903 req->scmnd = scmnd;
1904 req->cmd = iu;
1905
1906 len = srp_map_data(scmnd, target, req);
1907 if (len < 0) {
1908 shost_printk(KERN_ERR, target->scsi_host,
1909 PFX "Failed to map data (%d)\n", len);
1910 /*
1911 * If we ran out of memory descriptors (-ENOMEM) because an
1912 * application is queuing many requests with more than
1913 * max_pages_per_mr sg-list elements, tell the SCSI mid-layer
1914 * to reduce queue depth temporarily.
1915 */
1916 scmnd->result = len == -ENOMEM ?
1917 DID_OK << 16 | QUEUE_FULL << 1 : DID_ERROR << 16;
1918 goto err_iu;
1919 }
1920
1921 ib_dma_sync_single_for_device(dev, iu->dma, target->max_iu_len,
1922 DMA_TO_DEVICE);
1923
1924 if (srp_post_send(target, iu, len)) {
1925 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
1926 goto err_unmap;
1927 }
1928
1929 ret = 0;
1930
1931 unlock_rport:
1932 if (in_scsi_eh)
1933 mutex_unlock(&rport->mutex);
1934
1935 return ret;
1936
1937 err_unmap:
1938 srp_unmap_data(scmnd, target, req);
1939
1940 err_iu:
1941 srp_put_tx_iu(target, iu, SRP_IU_CMD);
1942
1943 /*
1944 * Avoid that the loops that iterate over the request ring can
1945 * encounter a dangling SCSI command pointer.
1946 */
1947 req->scmnd = NULL;
1948
1949 spin_lock_irqsave(&target->lock, flags);
1950 list_add(&req->list, &target->free_reqs);
1951
1952 err_unlock:
1953 spin_unlock_irqrestore(&target->lock, flags);
1954
1955 err:
1956 if (scmnd->result) {
1957 scmnd->scsi_done(scmnd);
1958 ret = 0;
1959 } else {
1960 ret = SCSI_MLQUEUE_HOST_BUSY;
1961 }
1962
1963 goto unlock_rport;
1964 }
1965
1966 /*
1967 * Note: the resources allocated in this function are freed in
1968 * srp_free_target_ib().
1969 */
1970 static int srp_alloc_iu_bufs(struct srp_target_port *target)
1971 {
1972 int i;
1973
1974 target->rx_ring = kzalloc(target->queue_size * sizeof(*target->rx_ring),
1975 GFP_KERNEL);
1976 if (!target->rx_ring)
1977 goto err_no_ring;
1978 target->tx_ring = kzalloc(target->queue_size * sizeof(*target->tx_ring),
1979 GFP_KERNEL);
1980 if (!target->tx_ring)
1981 goto err_no_ring;
1982
1983 for (i = 0; i < target->queue_size; ++i) {
1984 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1985 target->max_ti_iu_len,
1986 GFP_KERNEL, DMA_FROM_DEVICE);
1987 if (!target->rx_ring[i])
1988 goto err;
1989 }
1990
1991 for (i = 0; i < target->queue_size; ++i) {
1992 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
1993 target->max_iu_len,
1994 GFP_KERNEL, DMA_TO_DEVICE);
1995 if (!target->tx_ring[i])
1996 goto err;
1997
1998 list_add(&target->tx_ring[i]->list, &target->free_tx);
1999 }
2000
2001 return 0;
2002
2003 err:
2004 for (i = 0; i < target->queue_size; ++i) {
2005 srp_free_iu(target->srp_host, target->rx_ring[i]);
2006 srp_free_iu(target->srp_host, target->tx_ring[i]);
2007 }
2008
2009
2010 err_no_ring:
2011 kfree(target->tx_ring);
2012 target->tx_ring = NULL;
2013 kfree(target->rx_ring);
2014 target->rx_ring = NULL;
2015
2016 return -ENOMEM;
2017 }
2018
2019 static uint32_t srp_compute_rq_tmo(struct ib_qp_attr *qp_attr, int attr_mask)
2020 {
2021 uint64_t T_tr_ns, max_compl_time_ms;
2022 uint32_t rq_tmo_jiffies;
2023
2024 /*
2025 * According to section 11.2.4.2 in the IBTA spec (Modify Queue Pair,
2026 * table 91), both the QP timeout and the retry count have to be set
2027 * for RC QP's during the RTR to RTS transition.
2028 */
2029 WARN_ON_ONCE((attr_mask & (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)) !=
2030 (IB_QP_TIMEOUT | IB_QP_RETRY_CNT));
2031
2032 /*
2033 * Set target->rq_tmo_jiffies to one second more than the largest time
2034 * it can take before an error completion is generated. See also
2035 * C9-140..142 in the IBTA spec for more information about how to
2036 * convert the QP Local ACK Timeout value to nanoseconds.
2037 */
2038 T_tr_ns = 4096 * (1ULL << qp_attr->timeout);
2039 max_compl_time_ms = qp_attr->retry_cnt * 4 * T_tr_ns;
2040 do_div(max_compl_time_ms, NSEC_PER_MSEC);
2041 rq_tmo_jiffies = msecs_to_jiffies(max_compl_time_ms + 1000);
2042
2043 return rq_tmo_jiffies;
2044 }
2045
2046 static void srp_cm_rep_handler(struct ib_cm_id *cm_id,
2047 struct srp_login_rsp *lrsp,
2048 struct srp_target_port *target)
2049 {
2050 struct ib_qp_attr *qp_attr = NULL;
2051 int attr_mask = 0;
2052 int ret;
2053 int i;
2054
2055 if (lrsp->opcode == SRP_LOGIN_RSP) {
2056 target->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len);
2057 target->req_lim = be32_to_cpu(lrsp->req_lim_delta);
2058
2059 /*
2060 * Reserve credits for task management so we don't
2061 * bounce requests back to the SCSI mid-layer.
2062 */
2063 target->scsi_host->can_queue
2064 = min(target->req_lim - SRP_TSK_MGMT_SQ_SIZE,
2065 target->scsi_host->can_queue);
2066 target->scsi_host->cmd_per_lun
2067 = min_t(int, target->scsi_host->can_queue,
2068 target->scsi_host->cmd_per_lun);
2069 } else {
2070 shost_printk(KERN_WARNING, target->scsi_host,
2071 PFX "Unhandled RSP opcode %#x\n", lrsp->opcode);
2072 ret = -ECONNRESET;
2073 goto error;
2074 }
2075
2076 if (!target->rx_ring) {
2077 ret = srp_alloc_iu_bufs(target);
2078 if (ret)
2079 goto error;
2080 }
2081
2082 ret = -ENOMEM;
2083 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
2084 if (!qp_attr)
2085 goto error;
2086
2087 qp_attr->qp_state = IB_QPS_RTR;
2088 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
2089 if (ret)
2090 goto error_free;
2091
2092 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
2093 if (ret)
2094 goto error_free;
2095
2096 for (i = 0; i < target->queue_size; i++) {
2097 struct srp_iu *iu = target->rx_ring[i];
2098 ret = srp_post_recv(target, iu);
2099 if (ret)
2100 goto error_free;
2101 }
2102
2103 qp_attr->qp_state = IB_QPS_RTS;
2104 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
2105 if (ret)
2106 goto error_free;
2107
2108 target->rq_tmo_jiffies = srp_compute_rq_tmo(qp_attr, attr_mask);
2109
2110 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
2111 if (ret)
2112 goto error_free;
2113
2114 ret = ib_send_cm_rtu(cm_id, NULL, 0);
2115
2116 error_free:
2117 kfree(qp_attr);
2118
2119 error:
2120 target->status = ret;
2121 }
2122
2123 static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
2124 struct ib_cm_event *event,
2125 struct srp_target_port *target)
2126 {
2127 struct Scsi_Host *shost = target->scsi_host;
2128 struct ib_class_port_info *cpi;
2129 int opcode;
2130
2131 switch (event->param.rej_rcvd.reason) {
2132 case IB_CM_REJ_PORT_CM_REDIRECT:
2133 cpi = event->param.rej_rcvd.ari;
2134 target->path.dlid = cpi->redirect_lid;
2135 target->path.pkey = cpi->redirect_pkey;
2136 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
2137 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
2138
2139 target->status = target->path.dlid ?
2140 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
2141 break;
2142
2143 case IB_CM_REJ_PORT_REDIRECT:
2144 if (srp_target_is_topspin(target)) {
2145 /*
2146 * Topspin/Cisco SRP gateways incorrectly send
2147 * reject reason code 25 when they mean 24
2148 * (port redirect).
2149 */
2150 memcpy(target->path.dgid.raw,
2151 event->param.rej_rcvd.ari, 16);
2152
2153 shost_printk(KERN_DEBUG, shost,
2154 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
2155 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
2156 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
2157
2158 target->status = SRP_PORT_REDIRECT;
2159 } else {
2160 shost_printk(KERN_WARNING, shost,
2161 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
2162 target->status = -ECONNRESET;
2163 }
2164 break;
2165
2166 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
2167 shost_printk(KERN_WARNING, shost,
2168 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
2169 target->status = -ECONNRESET;
2170 break;
2171
2172 case IB_CM_REJ_CONSUMER_DEFINED:
2173 opcode = *(u8 *) event->private_data;
2174 if (opcode == SRP_LOGIN_REJ) {
2175 struct srp_login_rej *rej = event->private_data;
2176 u32 reason = be32_to_cpu(rej->reason);
2177
2178 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
2179 shost_printk(KERN_WARNING, shost,
2180 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
2181 else
2182 shost_printk(KERN_WARNING, shost, PFX
2183 "SRP LOGIN from %pI6 to %pI6 REJECTED, reason 0x%08x\n",
2184 target->path.sgid.raw,
2185 target->orig_dgid, reason);
2186 } else
2187 shost_printk(KERN_WARNING, shost,
2188 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
2189 " opcode 0x%02x\n", opcode);
2190 target->status = -ECONNRESET;
2191 break;
2192
2193 case IB_CM_REJ_STALE_CONN:
2194 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
2195 target->status = SRP_STALE_CONN;
2196 break;
2197
2198 default:
2199 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
2200 event->param.rej_rcvd.reason);
2201 target->status = -ECONNRESET;
2202 }
2203 }
2204
2205 static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2206 {
2207 struct srp_target_port *target = cm_id->context;
2208 int comp = 0;
2209
2210 switch (event->event) {
2211 case IB_CM_REQ_ERROR:
2212 shost_printk(KERN_DEBUG, target->scsi_host,
2213 PFX "Sending CM REQ failed\n");
2214 comp = 1;
2215 target->status = -ECONNRESET;
2216 break;
2217
2218 case IB_CM_REP_RECEIVED:
2219 comp = 1;
2220 srp_cm_rep_handler(cm_id, event->private_data, target);
2221 break;
2222
2223 case IB_CM_REJ_RECEIVED:
2224 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
2225 comp = 1;
2226
2227 srp_cm_rej_handler(cm_id, event, target);
2228 break;
2229
2230 case IB_CM_DREQ_RECEIVED:
2231 shost_printk(KERN_WARNING, target->scsi_host,
2232 PFX "DREQ received - connection closed\n");
2233 srp_change_conn_state(target, false);
2234 if (ib_send_cm_drep(cm_id, NULL, 0))
2235 shost_printk(KERN_ERR, target->scsi_host,
2236 PFX "Sending CM DREP failed\n");
2237 queue_work(system_long_wq, &target->tl_err_work);
2238 break;
2239
2240 case IB_CM_TIMEWAIT_EXIT:
2241 shost_printk(KERN_ERR, target->scsi_host,
2242 PFX "connection closed\n");
2243 comp = 1;
2244
2245 target->status = 0;
2246 break;
2247
2248 case IB_CM_MRA_RECEIVED:
2249 case IB_CM_DREQ_ERROR:
2250 case IB_CM_DREP_RECEIVED:
2251 break;
2252
2253 default:
2254 shost_printk(KERN_WARNING, target->scsi_host,
2255 PFX "Unhandled CM event %d\n", event->event);
2256 break;
2257 }
2258
2259 if (comp)
2260 complete(&target->done);
2261
2262 return 0;
2263 }
2264
2265 /**
2266 * srp_change_queue_depth - setting device queue depth
2267 * @sdev: scsi device struct
2268 * @qdepth: requested queue depth
2269 * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
2270 * (see include/scsi/scsi_host.h for definition)
2271 *
2272 * Returns queue depth.
2273 */
2274 static int
2275 srp_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
2276 {
2277 struct Scsi_Host *shost = sdev->host;
2278 int max_depth;
2279 if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP) {
2280 max_depth = shost->can_queue;
2281 if (!sdev->tagged_supported)
2282 max_depth = 1;
2283 if (qdepth > max_depth)
2284 qdepth = max_depth;
2285 scsi_adjust_queue_depth(sdev, qdepth);
2286 } else if (reason == SCSI_QDEPTH_QFULL)
2287 scsi_track_queue_full(sdev, qdepth);
2288 else
2289 return -EOPNOTSUPP;
2290
2291 return sdev->queue_depth;
2292 }
2293
2294 static int srp_send_tsk_mgmt(struct srp_target_port *target,
2295 u64 req_tag, unsigned int lun, u8 func)
2296 {
2297 struct srp_rport *rport = target->rport;
2298 struct ib_device *dev = target->srp_host->srp_dev->dev;
2299 struct srp_iu *iu;
2300 struct srp_tsk_mgmt *tsk_mgmt;
2301
2302 if (!target->connected || target->qp_in_error)
2303 return -1;
2304
2305 init_completion(&target->tsk_mgmt_done);
2306
2307 /*
2308 * Lock the rport mutex to avoid that srp_create_target_ib() is
2309 * invoked while a task management function is being sent.
2310 */
2311 mutex_lock(&rport->mutex);
2312 spin_lock_irq(&target->lock);
2313 iu = __srp_get_tx_iu(target, SRP_IU_TSK_MGMT);
2314 spin_unlock_irq(&target->lock);
2315
2316 if (!iu) {
2317 mutex_unlock(&rport->mutex);
2318
2319 return -1;
2320 }
2321
2322 ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt,
2323 DMA_TO_DEVICE);
2324 tsk_mgmt = iu->buf;
2325 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
2326
2327 tsk_mgmt->opcode = SRP_TSK_MGMT;
2328 tsk_mgmt->lun = cpu_to_be64((u64) lun << 48);
2329 tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT;
2330 tsk_mgmt->tsk_mgmt_func = func;
2331 tsk_mgmt->task_tag = req_tag;
2332
2333 ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt,
2334 DMA_TO_DEVICE);
2335 if (srp_post_send(target, iu, sizeof *tsk_mgmt)) {
2336 srp_put_tx_iu(target, iu, SRP_IU_TSK_MGMT);
2337 mutex_unlock(&rport->mutex);
2338
2339 return -1;
2340 }
2341 mutex_unlock(&rport->mutex);
2342
2343 if (!wait_for_completion_timeout(&target->tsk_mgmt_done,
2344 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
2345 return -1;
2346
2347 return 0;
2348 }
2349
2350 static int srp_abort(struct scsi_cmnd *scmnd)
2351 {
2352 struct srp_target_port *target = host_to_target(scmnd->device->host);
2353 struct srp_request *req = (struct srp_request *) scmnd->host_scribble;
2354 int ret;
2355
2356 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
2357
2358 if (!req || !srp_claim_req(target, req, NULL, scmnd))
2359 return SUCCESS;
2360 if (srp_send_tsk_mgmt(target, req->index, scmnd->device->lun,
2361 SRP_TSK_ABORT_TASK) == 0)
2362 ret = SUCCESS;
2363 else if (target->rport->state == SRP_RPORT_LOST)
2364 ret = FAST_IO_FAIL;
2365 else
2366 ret = FAILED;
2367 srp_free_req(target, req, scmnd, 0);
2368 scmnd->result = DID_ABORT << 16;
2369 scmnd->scsi_done(scmnd);
2370
2371 return ret;
2372 }
2373
2374 static int srp_reset_device(struct scsi_cmnd *scmnd)
2375 {
2376 struct srp_target_port *target = host_to_target(scmnd->device->host);
2377 int i;
2378
2379 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
2380
2381 if (srp_send_tsk_mgmt(target, SRP_TAG_NO_REQ, scmnd->device->lun,
2382 SRP_TSK_LUN_RESET))
2383 return FAILED;
2384 if (target->tsk_mgmt_status)
2385 return FAILED;
2386
2387 for (i = 0; i < target->req_ring_size; ++i) {
2388 struct srp_request *req = &target->req_ring[i];
2389 srp_finish_req(target, req, scmnd->device, DID_RESET << 16);
2390 }
2391
2392 return SUCCESS;
2393 }
2394
2395 static int srp_reset_host(struct scsi_cmnd *scmnd)
2396 {
2397 struct srp_target_port *target = host_to_target(scmnd->device->host);
2398
2399 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
2400
2401 return srp_reconnect_rport(target->rport) == 0 ? SUCCESS : FAILED;
2402 }
2403
2404 static int srp_slave_configure(struct scsi_device *sdev)
2405 {
2406 struct Scsi_Host *shost = sdev->host;
2407 struct srp_target_port *target = host_to_target(shost);
2408 struct request_queue *q = sdev->request_queue;
2409 unsigned long timeout;
2410
2411 if (sdev->type == TYPE_DISK) {
2412 timeout = max_t(unsigned, 30 * HZ, target->rq_tmo_jiffies);
2413 blk_queue_rq_timeout(q, timeout);
2414 }
2415
2416 return 0;
2417 }
2418
2419 static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
2420 char *buf)
2421 {
2422 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2423
2424 return sprintf(buf, "0x%016llx\n",
2425 (unsigned long long) be64_to_cpu(target->id_ext));
2426 }
2427
2428 static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
2429 char *buf)
2430 {
2431 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2432
2433 return sprintf(buf, "0x%016llx\n",
2434 (unsigned long long) be64_to_cpu(target->ioc_guid));
2435 }
2436
2437 static ssize_t show_service_id(struct device *dev,
2438 struct device_attribute *attr, char *buf)
2439 {
2440 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2441
2442 return sprintf(buf, "0x%016llx\n",
2443 (unsigned long long) be64_to_cpu(target->service_id));
2444 }
2445
2446 static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
2447 char *buf)
2448 {
2449 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2450
2451 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
2452 }
2453
2454 static ssize_t show_sgid(struct device *dev, struct device_attribute *attr,
2455 char *buf)
2456 {
2457 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2458
2459 return sprintf(buf, "%pI6\n", target->path.sgid.raw);
2460 }
2461
2462 static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
2463 char *buf)
2464 {
2465 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2466
2467 return sprintf(buf, "%pI6\n", target->path.dgid.raw);
2468 }
2469
2470 static ssize_t show_orig_dgid(struct device *dev,
2471 struct device_attribute *attr, char *buf)
2472 {
2473 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2474
2475 return sprintf(buf, "%pI6\n", target->orig_dgid);
2476 }
2477
2478 static ssize_t show_req_lim(struct device *dev,
2479 struct device_attribute *attr, char *buf)
2480 {
2481 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2482
2483 return sprintf(buf, "%d\n", target->req_lim);
2484 }
2485
2486 static ssize_t show_zero_req_lim(struct device *dev,
2487 struct device_attribute *attr, char *buf)
2488 {
2489 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2490
2491 return sprintf(buf, "%d\n", target->zero_req_lim);
2492 }
2493
2494 static ssize_t show_local_ib_port(struct device *dev,
2495 struct device_attribute *attr, char *buf)
2496 {
2497 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2498
2499 return sprintf(buf, "%d\n", target->srp_host->port);
2500 }
2501
2502 static ssize_t show_local_ib_device(struct device *dev,
2503 struct device_attribute *attr, char *buf)
2504 {
2505 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2506
2507 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
2508 }
2509
2510 static ssize_t show_comp_vector(struct device *dev,
2511 struct device_attribute *attr, char *buf)
2512 {
2513 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2514
2515 return sprintf(buf, "%d\n", target->comp_vector);
2516 }
2517
2518 static ssize_t show_tl_retry_count(struct device *dev,
2519 struct device_attribute *attr, char *buf)
2520 {
2521 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2522
2523 return sprintf(buf, "%d\n", target->tl_retry_count);
2524 }
2525
2526 static ssize_t show_cmd_sg_entries(struct device *dev,
2527 struct device_attribute *attr, char *buf)
2528 {
2529 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2530
2531 return sprintf(buf, "%u\n", target->cmd_sg_cnt);
2532 }
2533
2534 static ssize_t show_allow_ext_sg(struct device *dev,
2535 struct device_attribute *attr, char *buf)
2536 {
2537 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2538
2539 return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false");
2540 }
2541
2542 static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
2543 static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
2544 static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
2545 static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
2546 static DEVICE_ATTR(sgid, S_IRUGO, show_sgid, NULL);
2547 static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
2548 static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
2549 static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL);
2550 static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
2551 static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
2552 static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
2553 static DEVICE_ATTR(comp_vector, S_IRUGO, show_comp_vector, NULL);
2554 static DEVICE_ATTR(tl_retry_count, S_IRUGO, show_tl_retry_count, NULL);
2555 static DEVICE_ATTR(cmd_sg_entries, S_IRUGO, show_cmd_sg_entries, NULL);
2556 static DEVICE_ATTR(allow_ext_sg, S_IRUGO, show_allow_ext_sg, NULL);
2557
2558 static struct device_attribute *srp_host_attrs[] = {
2559 &dev_attr_id_ext,
2560 &dev_attr_ioc_guid,
2561 &dev_attr_service_id,
2562 &dev_attr_pkey,
2563 &dev_attr_sgid,
2564 &dev_attr_dgid,
2565 &dev_attr_orig_dgid,
2566 &dev_attr_req_lim,
2567 &dev_attr_zero_req_lim,
2568 &dev_attr_local_ib_port,
2569 &dev_attr_local_ib_device,
2570 &dev_attr_comp_vector,
2571 &dev_attr_tl_retry_count,
2572 &dev_attr_cmd_sg_entries,
2573 &dev_attr_allow_ext_sg,
2574 NULL
2575 };
2576
2577 static struct scsi_host_template srp_template = {
2578 .module = THIS_MODULE,
2579 .name = "InfiniBand SRP initiator",
2580 .proc_name = DRV_NAME,
2581 .slave_configure = srp_slave_configure,
2582 .info = srp_target_info,
2583 .queuecommand = srp_queuecommand,
2584 .change_queue_depth = srp_change_queue_depth,
2585 .change_queue_type = scsi_change_queue_type,
2586 .eh_abort_handler = srp_abort,
2587 .eh_device_reset_handler = srp_reset_device,
2588 .eh_host_reset_handler = srp_reset_host,
2589 .skip_settle_delay = true,
2590 .sg_tablesize = SRP_DEF_SG_TABLESIZE,
2591 .can_queue = SRP_DEFAULT_CMD_SQ_SIZE,
2592 .this_id = -1,
2593 .cmd_per_lun = SRP_DEFAULT_CMD_SQ_SIZE,
2594 .use_clustering = ENABLE_CLUSTERING,
2595 .shost_attrs = srp_host_attrs
2596 };
2597
2598 static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
2599 {
2600 struct srp_rport_identifiers ids;
2601 struct srp_rport *rport;
2602
2603 sprintf(target->target_name, "SRP.T10:%016llX",
2604 (unsigned long long) be64_to_cpu(target->id_ext));
2605
2606 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
2607 return -ENODEV;
2608
2609 memcpy(ids.port_id, &target->id_ext, 8);
2610 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
2611 ids.roles = SRP_RPORT_ROLE_TARGET;
2612 rport = srp_rport_add(target->scsi_host, &ids);
2613 if (IS_ERR(rport)) {
2614 scsi_remove_host(target->scsi_host);
2615 return PTR_ERR(rport);
2616 }
2617
2618 rport->lld_data = target;
2619 target->rport = rport;
2620
2621 spin_lock(&host->target_lock);
2622 list_add_tail(&target->list, &host->target_list);
2623 spin_unlock(&host->target_lock);
2624
2625 target->state = SRP_TARGET_LIVE;
2626
2627 scsi_scan_target(&target->scsi_host->shost_gendev,
2628 0, target->scsi_id, SCAN_WILD_CARD, 0);
2629
2630 return 0;
2631 }
2632
2633 static void srp_release_dev(struct device *dev)
2634 {
2635 struct srp_host *host =
2636 container_of(dev, struct srp_host, dev);
2637
2638 complete(&host->released);
2639 }
2640
2641 static struct class srp_class = {
2642 .name = "infiniband_srp",
2643 .dev_release = srp_release_dev
2644 };
2645
2646 /**
2647 * srp_conn_unique() - check whether the connection to a target is unique
2648 * @host: SRP host.
2649 * @target: SRP target port.
2650 */
2651 static bool srp_conn_unique(struct srp_host *host,
2652 struct srp_target_port *target)
2653 {
2654 struct srp_target_port *t;
2655 bool ret = false;
2656
2657 if (target->state == SRP_TARGET_REMOVED)
2658 goto out;
2659
2660 ret = true;
2661
2662 spin_lock(&host->target_lock);
2663 list_for_each_entry(t, &host->target_list, list) {
2664 if (t != target &&
2665 target->id_ext == t->id_ext &&
2666 target->ioc_guid == t->ioc_guid &&
2667 target->initiator_ext == t->initiator_ext) {
2668 ret = false;
2669 break;
2670 }
2671 }
2672 spin_unlock(&host->target_lock);
2673
2674 out:
2675 return ret;
2676 }
2677
2678 /*
2679 * Target ports are added by writing
2680 *
2681 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
2682 * pkey=<P_Key>,service_id=<service ID>
2683 *
2684 * to the add_target sysfs attribute.
2685 */
2686 enum {
2687 SRP_OPT_ERR = 0,
2688 SRP_OPT_ID_EXT = 1 << 0,
2689 SRP_OPT_IOC_GUID = 1 << 1,
2690 SRP_OPT_DGID = 1 << 2,
2691 SRP_OPT_PKEY = 1 << 3,
2692 SRP_OPT_SERVICE_ID = 1 << 4,
2693 SRP_OPT_MAX_SECT = 1 << 5,
2694 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
2695 SRP_OPT_IO_CLASS = 1 << 7,
2696 SRP_OPT_INITIATOR_EXT = 1 << 8,
2697 SRP_OPT_CMD_SG_ENTRIES = 1 << 9,
2698 SRP_OPT_ALLOW_EXT_SG = 1 << 10,
2699 SRP_OPT_SG_TABLESIZE = 1 << 11,
2700 SRP_OPT_COMP_VECTOR = 1 << 12,
2701 SRP_OPT_TL_RETRY_COUNT = 1 << 13,
2702 SRP_OPT_QUEUE_SIZE = 1 << 14,
2703 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
2704 SRP_OPT_IOC_GUID |
2705 SRP_OPT_DGID |
2706 SRP_OPT_PKEY |
2707 SRP_OPT_SERVICE_ID),
2708 };
2709
2710 static const match_table_t srp_opt_tokens = {
2711 { SRP_OPT_ID_EXT, "id_ext=%s" },
2712 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
2713 { SRP_OPT_DGID, "dgid=%s" },
2714 { SRP_OPT_PKEY, "pkey=%x" },
2715 { SRP_OPT_SERVICE_ID, "service_id=%s" },
2716 { SRP_OPT_MAX_SECT, "max_sect=%d" },
2717 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
2718 { SRP_OPT_IO_CLASS, "io_class=%x" },
2719 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
2720 { SRP_OPT_CMD_SG_ENTRIES, "cmd_sg_entries=%u" },
2721 { SRP_OPT_ALLOW_EXT_SG, "allow_ext_sg=%u" },
2722 { SRP_OPT_SG_TABLESIZE, "sg_tablesize=%u" },
2723 { SRP_OPT_COMP_VECTOR, "comp_vector=%u" },
2724 { SRP_OPT_TL_RETRY_COUNT, "tl_retry_count=%u" },
2725 { SRP_OPT_QUEUE_SIZE, "queue_size=%d" },
2726 { SRP_OPT_ERR, NULL }
2727 };
2728
2729 static int srp_parse_options(const char *buf, struct srp_target_port *target)
2730 {
2731 char *options, *sep_opt;
2732 char *p;
2733 char dgid[3];
2734 substring_t args[MAX_OPT_ARGS];
2735 int opt_mask = 0;
2736 int token;
2737 int ret = -EINVAL;
2738 int i;
2739
2740 options = kstrdup(buf, GFP_KERNEL);
2741 if (!options)
2742 return -ENOMEM;
2743
2744 sep_opt = options;
2745 while ((p = strsep(&sep_opt, ",")) != NULL) {
2746 if (!*p)
2747 continue;
2748
2749 token = match_token(p, srp_opt_tokens, args);
2750 opt_mask |= token;
2751
2752 switch (token) {
2753 case SRP_OPT_ID_EXT:
2754 p = match_strdup(args);
2755 if (!p) {
2756 ret = -ENOMEM;
2757 goto out;
2758 }
2759 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2760 kfree(p);
2761 break;
2762
2763 case SRP_OPT_IOC_GUID:
2764 p = match_strdup(args);
2765 if (!p) {
2766 ret = -ENOMEM;
2767 goto out;
2768 }
2769 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
2770 kfree(p);
2771 break;
2772
2773 case SRP_OPT_DGID:
2774 p = match_strdup(args);
2775 if (!p) {
2776 ret = -ENOMEM;
2777 goto out;
2778 }
2779 if (strlen(p) != 32) {
2780 pr_warn("bad dest GID parameter '%s'\n", p);
2781 kfree(p);
2782 goto out;
2783 }
2784
2785 for (i = 0; i < 16; ++i) {
2786 strlcpy(dgid, p + i * 2, 3);
2787 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
2788 }
2789 kfree(p);
2790 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
2791 break;
2792
2793 case SRP_OPT_PKEY:
2794 if (match_hex(args, &token)) {
2795 pr_warn("bad P_Key parameter '%s'\n", p);
2796 goto out;
2797 }
2798 target->path.pkey = cpu_to_be16(token);
2799 break;
2800
2801 case SRP_OPT_SERVICE_ID:
2802 p = match_strdup(args);
2803 if (!p) {
2804 ret = -ENOMEM;
2805 goto out;
2806 }
2807 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
2808 target->path.service_id = target->service_id;
2809 kfree(p);
2810 break;
2811
2812 case SRP_OPT_MAX_SECT:
2813 if (match_int(args, &token)) {
2814 pr_warn("bad max sect parameter '%s'\n", p);
2815 goto out;
2816 }
2817 target->scsi_host->max_sectors = token;
2818 break;
2819
2820 case SRP_OPT_QUEUE_SIZE:
2821 if (match_int(args, &token) || token < 1) {
2822 pr_warn("bad queue_size parameter '%s'\n", p);
2823 goto out;
2824 }
2825 target->scsi_host->can_queue = token;
2826 target->queue_size = token + SRP_RSP_SQ_SIZE +
2827 SRP_TSK_MGMT_SQ_SIZE;
2828 if (!(opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
2829 target->scsi_host->cmd_per_lun = token;
2830 break;
2831
2832 case SRP_OPT_MAX_CMD_PER_LUN:
2833 if (match_int(args, &token) || token < 1) {
2834 pr_warn("bad max cmd_per_lun parameter '%s'\n",
2835 p);
2836 goto out;
2837 }
2838 target->scsi_host->cmd_per_lun = token;
2839 break;
2840
2841 case SRP_OPT_IO_CLASS:
2842 if (match_hex(args, &token)) {
2843 pr_warn("bad IO class parameter '%s'\n", p);
2844 goto out;
2845 }
2846 if (token != SRP_REV10_IB_IO_CLASS &&
2847 token != SRP_REV16A_IB_IO_CLASS) {
2848 pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n",
2849 token, SRP_REV10_IB_IO_CLASS,
2850 SRP_REV16A_IB_IO_CLASS);
2851 goto out;
2852 }
2853 target->io_class = token;
2854 break;
2855
2856 case SRP_OPT_INITIATOR_EXT:
2857 p = match_strdup(args);
2858 if (!p) {
2859 ret = -ENOMEM;
2860 goto out;
2861 }
2862 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2863 kfree(p);
2864 break;
2865
2866 case SRP_OPT_CMD_SG_ENTRIES:
2867 if (match_int(args, &token) || token < 1 || token > 255) {
2868 pr_warn("bad max cmd_sg_entries parameter '%s'\n",
2869 p);
2870 goto out;
2871 }
2872 target->cmd_sg_cnt = token;
2873 break;
2874
2875 case SRP_OPT_ALLOW_EXT_SG:
2876 if (match_int(args, &token)) {
2877 pr_warn("bad allow_ext_sg parameter '%s'\n", p);
2878 goto out;
2879 }
2880 target->allow_ext_sg = !!token;
2881 break;
2882
2883 case SRP_OPT_SG_TABLESIZE:
2884 if (match_int(args, &token) || token < 1 ||
2885 token > SCSI_MAX_SG_CHAIN_SEGMENTS) {
2886 pr_warn("bad max sg_tablesize parameter '%s'\n",
2887 p);
2888 goto out;
2889 }
2890 target->sg_tablesize = token;
2891 break;
2892
2893 case SRP_OPT_COMP_VECTOR:
2894 if (match_int(args, &token) || token < 0) {
2895 pr_warn("bad comp_vector parameter '%s'\n", p);
2896 goto out;
2897 }
2898 target->comp_vector = token;
2899 break;
2900
2901 case SRP_OPT_TL_RETRY_COUNT:
2902 if (match_int(args, &token) || token < 2 || token > 7) {
2903 pr_warn("bad tl_retry_count parameter '%s' (must be a number between 2 and 7)\n",
2904 p);
2905 goto out;
2906 }
2907 target->tl_retry_count = token;
2908 break;
2909
2910 default:
2911 pr_warn("unknown parameter or missing value '%s' in target creation request\n",
2912 p);
2913 goto out;
2914 }
2915 }
2916
2917 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
2918 ret = 0;
2919 else
2920 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
2921 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
2922 !(srp_opt_tokens[i].token & opt_mask))
2923 pr_warn("target creation request is missing parameter '%s'\n",
2924 srp_opt_tokens[i].pattern);
2925
2926 if (target->scsi_host->cmd_per_lun > target->scsi_host->can_queue
2927 && (opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
2928 pr_warn("cmd_per_lun = %d > queue_size = %d\n",
2929 target->scsi_host->cmd_per_lun,
2930 target->scsi_host->can_queue);
2931
2932 out:
2933 kfree(options);
2934 return ret;
2935 }
2936
2937 static ssize_t srp_create_target(struct device *dev,
2938 struct device_attribute *attr,
2939 const char *buf, size_t count)
2940 {
2941 struct srp_host *host =
2942 container_of(dev, struct srp_host, dev);
2943 struct Scsi_Host *target_host;
2944 struct srp_target_port *target;
2945 struct srp_device *srp_dev = host->srp_dev;
2946 struct ib_device *ibdev = srp_dev->dev;
2947 int ret;
2948
2949 target_host = scsi_host_alloc(&srp_template,
2950 sizeof (struct srp_target_port));
2951 if (!target_host)
2952 return -ENOMEM;
2953
2954 target_host->transportt = ib_srp_transport_template;
2955 target_host->max_channel = 0;
2956 target_host->max_id = 1;
2957 target_host->max_lun = SRP_MAX_LUN;
2958 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
2959
2960 target = host_to_target(target_host);
2961
2962 target->io_class = SRP_REV16A_IB_IO_CLASS;
2963 target->scsi_host = target_host;
2964 target->srp_host = host;
2965 target->lkey = host->srp_dev->mr->lkey;
2966 target->rkey = host->srp_dev->mr->rkey;
2967 target->cmd_sg_cnt = cmd_sg_entries;
2968 target->sg_tablesize = indirect_sg_entries ? : cmd_sg_entries;
2969 target->allow_ext_sg = allow_ext_sg;
2970 target->tl_retry_count = 7;
2971 target->queue_size = SRP_DEFAULT_QUEUE_SIZE;
2972
2973 mutex_lock(&host->add_target_mutex);
2974
2975 ret = srp_parse_options(buf, target);
2976 if (ret)
2977 goto err;
2978
2979 target->req_ring_size = target->queue_size - SRP_TSK_MGMT_SQ_SIZE;
2980
2981 if (!srp_conn_unique(target->srp_host, target)) {
2982 shost_printk(KERN_INFO, target->scsi_host,
2983 PFX "Already connected to target port with id_ext=%016llx;ioc_guid=%016llx;initiator_ext=%016llx\n",
2984 be64_to_cpu(target->id_ext),
2985 be64_to_cpu(target->ioc_guid),
2986 be64_to_cpu(target->initiator_ext));
2987 ret = -EEXIST;
2988 goto err;
2989 }
2990
2991 if (!srp_dev->has_fmr && !srp_dev->has_fr && !target->allow_ext_sg &&
2992 target->cmd_sg_cnt < target->sg_tablesize) {
2993 pr_warn("No MR pool and no external indirect descriptors, limiting sg_tablesize to cmd_sg_cnt\n");
2994 target->sg_tablesize = target->cmd_sg_cnt;
2995 }
2996
2997 target_host->sg_tablesize = target->sg_tablesize;
2998 target->indirect_size = target->sg_tablesize *
2999 sizeof (struct srp_direct_buf);
3000 target->max_iu_len = sizeof (struct srp_cmd) +
3001 sizeof (struct srp_indirect_buf) +
3002 target->cmd_sg_cnt * sizeof (struct srp_direct_buf);
3003
3004 INIT_WORK(&target->tl_err_work, srp_tl_err_work);
3005 INIT_WORK(&target->remove_work, srp_remove_work);
3006 spin_lock_init(&target->lock);
3007 INIT_LIST_HEAD(&target->free_tx);
3008 ret = srp_alloc_req_data(target);
3009 if (ret)
3010 goto err_free_mem;
3011
3012 ret = ib_query_gid(ibdev, host->port, 0, &target->path.sgid);
3013 if (ret)
3014 goto err_free_mem;
3015
3016 ret = srp_create_target_ib(target);
3017 if (ret)
3018 goto err_free_mem;
3019
3020 ret = srp_new_cm_id(target);
3021 if (ret)
3022 goto err_free_ib;
3023
3024 ret = srp_connect_target(target);
3025 if (ret) {
3026 shost_printk(KERN_ERR, target->scsi_host,
3027 PFX "Connection failed\n");
3028 goto err_free_ib;
3029 }
3030
3031 ret = srp_add_target(host, target);
3032 if (ret)
3033 goto err_disconnect;
3034
3035 shost_printk(KERN_DEBUG, target->scsi_host, PFX
3036 "new target: id_ext %016llx ioc_guid %016llx pkey %04x service_id %016llx sgid %pI6 dgid %pI6\n",
3037 be64_to_cpu(target->id_ext),
3038 be64_to_cpu(target->ioc_guid),
3039 be16_to_cpu(target->path.pkey),
3040 be64_to_cpu(target->service_id),
3041 target->path.sgid.raw, target->path.dgid.raw);
3042
3043 ret = count;
3044
3045 out:
3046 mutex_unlock(&host->add_target_mutex);
3047 return ret;
3048
3049 err_disconnect:
3050 srp_disconnect_target(target);
3051
3052 err_free_ib:
3053 srp_free_target_ib(target);
3054
3055 err_free_mem:
3056 srp_free_req_data(target);
3057
3058 err:
3059 scsi_host_put(target_host);
3060 goto out;
3061 }
3062
3063 static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
3064
3065 static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
3066 char *buf)
3067 {
3068 struct srp_host *host = container_of(dev, struct srp_host, dev);
3069
3070 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
3071 }
3072
3073 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
3074
3075 static ssize_t show_port(struct device *dev, struct device_attribute *attr,
3076 char *buf)
3077 {
3078 struct srp_host *host = container_of(dev, struct srp_host, dev);
3079
3080 return sprintf(buf, "%d\n", host->port);
3081 }
3082
3083 static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
3084
3085 static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
3086 {
3087 struct srp_host *host;
3088
3089 host = kzalloc(sizeof *host, GFP_KERNEL);
3090 if (!host)
3091 return NULL;
3092
3093 INIT_LIST_HEAD(&host->target_list);
3094 spin_lock_init(&host->target_lock);
3095 init_completion(&host->released);
3096 mutex_init(&host->add_target_mutex);
3097 host->srp_dev = device;
3098 host->port = port;
3099
3100 host->dev.class = &srp_class;
3101 host->dev.parent = device->dev->dma_device;
3102 dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port);
3103
3104 if (device_register(&host->dev))
3105 goto free_host;
3106 if (device_create_file(&host->dev, &dev_attr_add_target))
3107 goto err_class;
3108 if (device_create_file(&host->dev, &dev_attr_ibdev))
3109 goto err_class;
3110 if (device_create_file(&host->dev, &dev_attr_port))
3111 goto err_class;
3112
3113 return host;
3114
3115 err_class:
3116 device_unregister(&host->dev);
3117
3118 free_host:
3119 kfree(host);
3120
3121 return NULL;
3122 }
3123
3124 static void srp_add_one(struct ib_device *device)
3125 {
3126 struct srp_device *srp_dev;
3127 struct ib_device_attr *dev_attr;
3128 struct srp_host *host;
3129 int mr_page_shift, s, e, p;
3130 u64 max_pages_per_mr;
3131
3132 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
3133 if (!dev_attr)
3134 return;
3135
3136 if (ib_query_device(device, dev_attr)) {
3137 pr_warn("Query device failed for %s\n", device->name);
3138 goto free_attr;
3139 }
3140
3141 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
3142 if (!srp_dev)
3143 goto free_attr;
3144
3145 srp_dev->has_fmr = (device->alloc_fmr && device->dealloc_fmr &&
3146 device->map_phys_fmr && device->unmap_fmr);
3147 srp_dev->has_fr = (dev_attr->device_cap_flags &
3148 IB_DEVICE_MEM_MGT_EXTENSIONS);
3149 if (!srp_dev->has_fmr && !srp_dev->has_fr)
3150 dev_warn(&device->dev, "neither FMR nor FR is supported\n");
3151
3152 srp_dev->use_fast_reg = (srp_dev->has_fr &&
3153 (!srp_dev->has_fmr || prefer_fr));
3154
3155 /*
3156 * Use the smallest page size supported by the HCA, down to a
3157 * minimum of 4096 bytes. We're unlikely to build large sglists
3158 * out of smaller entries.
3159 */
3160 mr_page_shift = max(12, ffs(dev_attr->page_size_cap) - 1);
3161 srp_dev->mr_page_size = 1 << mr_page_shift;
3162 srp_dev->mr_page_mask = ~((u64) srp_dev->mr_page_size - 1);
3163 max_pages_per_mr = dev_attr->max_mr_size;
3164 do_div(max_pages_per_mr, srp_dev->mr_page_size);
3165 srp_dev->max_pages_per_mr = min_t(u64, SRP_MAX_PAGES_PER_MR,
3166 max_pages_per_mr);
3167 if (srp_dev->use_fast_reg) {
3168 srp_dev->max_pages_per_mr =
3169 min_t(u32, srp_dev->max_pages_per_mr,
3170 dev_attr->max_fast_reg_page_list_len);
3171 }
3172 srp_dev->mr_max_size = srp_dev->mr_page_size *
3173 srp_dev->max_pages_per_mr;
3174 pr_debug("%s: mr_page_shift = %d, dev_attr->max_mr_size = %#llx, dev_attr->max_fast_reg_page_list_len = %u, max_pages_per_mr = %d, mr_max_size = %#x\n",
3175 device->name, mr_page_shift, dev_attr->max_mr_size,
3176 dev_attr->max_fast_reg_page_list_len,
3177 srp_dev->max_pages_per_mr, srp_dev->mr_max_size);
3178
3179 INIT_LIST_HEAD(&srp_dev->dev_list);
3180
3181 srp_dev->dev = device;
3182 srp_dev->pd = ib_alloc_pd(device);
3183 if (IS_ERR(srp_dev->pd))
3184 goto free_dev;
3185
3186 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
3187 IB_ACCESS_LOCAL_WRITE |
3188 IB_ACCESS_REMOTE_READ |
3189 IB_ACCESS_REMOTE_WRITE);
3190 if (IS_ERR(srp_dev->mr))
3191 goto err_pd;
3192
3193 if (device->node_type == RDMA_NODE_IB_SWITCH) {
3194 s = 0;
3195 e = 0;
3196 } else {
3197 s = 1;
3198 e = device->phys_port_cnt;
3199 }
3200
3201 for (p = s; p <= e; ++p) {
3202 host = srp_add_port(srp_dev, p);
3203 if (host)
3204 list_add_tail(&host->list, &srp_dev->dev_list);
3205 }
3206
3207 ib_set_client_data(device, &srp_client, srp_dev);
3208
3209 goto free_attr;
3210
3211 err_pd:
3212 ib_dealloc_pd(srp_dev->pd);
3213
3214 free_dev:
3215 kfree(srp_dev);
3216
3217 free_attr:
3218 kfree(dev_attr);
3219 }
3220
3221 static void srp_remove_one(struct ib_device *device)
3222 {
3223 struct srp_device *srp_dev;
3224 struct srp_host *host, *tmp_host;
3225 struct srp_target_port *target;
3226
3227 srp_dev = ib_get_client_data(device, &srp_client);
3228 if (!srp_dev)
3229 return;
3230
3231 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
3232 device_unregister(&host->dev);
3233 /*
3234 * Wait for the sysfs entry to go away, so that no new
3235 * target ports can be created.
3236 */
3237 wait_for_completion(&host->released);
3238
3239 /*
3240 * Remove all target ports.
3241 */
3242 spin_lock(&host->target_lock);
3243 list_for_each_entry(target, &host->target_list, list)
3244 srp_queue_remove_work(target);
3245 spin_unlock(&host->target_lock);
3246
3247 /*
3248 * Wait for tl_err and target port removal tasks.
3249 */
3250 flush_workqueue(system_long_wq);
3251 flush_workqueue(srp_remove_wq);
3252
3253 kfree(host);
3254 }
3255
3256 ib_dereg_mr(srp_dev->mr);
3257 ib_dealloc_pd(srp_dev->pd);
3258
3259 kfree(srp_dev);
3260 }
3261
3262 static struct srp_function_template ib_srp_transport_functions = {
3263 .has_rport_state = true,
3264 .reset_timer_if_blocked = true,
3265 .reconnect_delay = &srp_reconnect_delay,
3266 .fast_io_fail_tmo = &srp_fast_io_fail_tmo,
3267 .dev_loss_tmo = &srp_dev_loss_tmo,
3268 .reconnect = srp_rport_reconnect,
3269 .rport_delete = srp_rport_delete,
3270 .terminate_rport_io = srp_terminate_io,
3271 };
3272
3273 static int __init srp_init_module(void)
3274 {
3275 int ret;
3276
3277 BUILD_BUG_ON(FIELD_SIZEOF(struct ib_wc, wr_id) < sizeof(void *));
3278
3279 if (srp_sg_tablesize) {
3280 pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n");
3281 if (!cmd_sg_entries)
3282 cmd_sg_entries = srp_sg_tablesize;
3283 }
3284
3285 if (!cmd_sg_entries)
3286 cmd_sg_entries = SRP_DEF_SG_TABLESIZE;
3287
3288 if (cmd_sg_entries > 255) {
3289 pr_warn("Clamping cmd_sg_entries to 255\n");
3290 cmd_sg_entries = 255;
3291 }
3292
3293 if (!indirect_sg_entries)
3294 indirect_sg_entries = cmd_sg_entries;
3295 else if (indirect_sg_entries < cmd_sg_entries) {
3296 pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n",
3297 cmd_sg_entries);
3298 indirect_sg_entries = cmd_sg_entries;
3299 }
3300
3301 srp_remove_wq = create_workqueue("srp_remove");
3302 if (!srp_remove_wq) {
3303 ret = -ENOMEM;
3304 goto out;
3305 }
3306
3307 ret = -ENOMEM;
3308 ib_srp_transport_template =
3309 srp_attach_transport(&ib_srp_transport_functions);
3310 if (!ib_srp_transport_template)
3311 goto destroy_wq;
3312
3313 ret = class_register(&srp_class);
3314 if (ret) {
3315 pr_err("couldn't register class infiniband_srp\n");
3316 goto release_tr;
3317 }
3318
3319 ib_sa_register_client(&srp_sa_client);
3320
3321 ret = ib_register_client(&srp_client);
3322 if (ret) {
3323 pr_err("couldn't register IB client\n");
3324 goto unreg_sa;
3325 }
3326
3327 out:
3328 return ret;
3329
3330 unreg_sa:
3331 ib_sa_unregister_client(&srp_sa_client);
3332 class_unregister(&srp_class);
3333
3334 release_tr:
3335 srp_release_transport(ib_srp_transport_template);
3336
3337 destroy_wq:
3338 destroy_workqueue(srp_remove_wq);
3339 goto out;
3340 }
3341
3342 static void __exit srp_cleanup_module(void)
3343 {
3344 ib_unregister_client(&srp_client);
3345 ib_sa_unregister_client(&srp_sa_client);
3346 class_unregister(&srp_class);
3347 srp_release_transport(ib_srp_transport_template);
3348 destroy_workqueue(srp_remove_wq);
3349 }
3350
3351 module_init(srp_init_module);
3352 module_exit(srp_cleanup_module);
This page took 0.296437 seconds and 5 git commands to generate.