• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <linux/lockdep.h>
44 #include <linux/inet.h>
45 #include <rdma/ib_cache.h>
46 
47 #include <linux/atomic.h>
48 
49 #include <scsi/scsi.h>
50 #include <scsi/scsi_device.h>
51 #include <scsi/scsi_dbg.h>
52 #include <scsi/scsi_tcq.h>
53 #include <scsi/srp.h>
54 #include <scsi/scsi_transport_srp.h>
55 
56 #include "ib_srp.h"
57 
58 #define DRV_NAME	"ib_srp"
59 #define PFX		DRV_NAME ": "
60 
61 MODULE_AUTHOR("Roland Dreier");
62 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator");
63 MODULE_LICENSE("Dual BSD/GPL");
64 
65 #if !defined(CONFIG_DYNAMIC_DEBUG)
66 #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt)
67 #define DYNAMIC_DEBUG_BRANCH(descriptor) false
68 #endif
69 
70 static unsigned int srp_sg_tablesize;
71 static unsigned int cmd_sg_entries;
72 static unsigned int indirect_sg_entries;
73 static bool allow_ext_sg;
74 static bool register_always = true;
75 static bool never_register;
76 static int topspin_workarounds = 1;
77 
78 module_param(srp_sg_tablesize, uint, 0444);
79 MODULE_PARM_DESC(srp_sg_tablesize, "Deprecated name for cmd_sg_entries");
80 
81 module_param(cmd_sg_entries, uint, 0444);
82 MODULE_PARM_DESC(cmd_sg_entries,
83 		 "Default number of gather/scatter entries in the SRP command (default is 12, max 255)");
84 
85 module_param(indirect_sg_entries, uint, 0444);
86 MODULE_PARM_DESC(indirect_sg_entries,
87 		 "Default max number of gather/scatter entries (default is 12, max is " __stringify(SG_MAX_SEGMENTS) ")");
88 
89 module_param(allow_ext_sg, bool, 0444);
90 MODULE_PARM_DESC(allow_ext_sg,
91 		  "Default behavior when there are more than cmd_sg_entries S/G entries after mapping; fails the request when false (default false)");
92 
93 module_param(topspin_workarounds, int, 0444);
94 MODULE_PARM_DESC(topspin_workarounds,
95 		 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
96 
97 module_param(register_always, bool, 0444);
98 MODULE_PARM_DESC(register_always,
99 		 "Use memory registration even for contiguous memory regions");
100 
101 module_param(never_register, bool, 0444);
102 MODULE_PARM_DESC(never_register, "Never register memory");
103 
104 static const struct kernel_param_ops srp_tmo_ops;
105 
106 static int srp_reconnect_delay = 10;
107 module_param_cb(reconnect_delay, &srp_tmo_ops, &srp_reconnect_delay,
108 		S_IRUGO | S_IWUSR);
109 MODULE_PARM_DESC(reconnect_delay, "Time between successive reconnect attempts");
110 
111 static int srp_fast_io_fail_tmo = 15;
112 module_param_cb(fast_io_fail_tmo, &srp_tmo_ops, &srp_fast_io_fail_tmo,
113 		S_IRUGO | S_IWUSR);
114 MODULE_PARM_DESC(fast_io_fail_tmo,
115 		 "Number of seconds between the observation of a transport"
116 		 " layer error and failing all I/O. \"off\" means that this"
117 		 " functionality is disabled.");
118 
119 static int srp_dev_loss_tmo = 600;
120 module_param_cb(dev_loss_tmo, &srp_tmo_ops, &srp_dev_loss_tmo,
121 		S_IRUGO | S_IWUSR);
122 MODULE_PARM_DESC(dev_loss_tmo,
123 		 "Maximum number of seconds that the SRP transport should"
124 		 " insulate transport layer errors. After this time has been"
125 		 " exceeded the SCSI host is removed. Should be"
126 		 " between 1 and " __stringify(SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
127 		 " if fast_io_fail_tmo has not been set. \"off\" means that"
128 		 " this functionality is disabled.");
129 
130 static bool srp_use_imm_data = true;
131 module_param_named(use_imm_data, srp_use_imm_data, bool, 0644);
132 MODULE_PARM_DESC(use_imm_data,
133 		 "Whether or not to request permission to use immediate data during SRP login.");
134 
135 static unsigned int srp_max_imm_data = 8 * 1024;
136 module_param_named(max_imm_data, srp_max_imm_data, uint, 0644);
137 MODULE_PARM_DESC(max_imm_data, "Maximum immediate data size.");
138 
139 static unsigned ch_count;
140 module_param(ch_count, uint, 0444);
141 MODULE_PARM_DESC(ch_count,
142 		 "Number of RDMA channels to use for communication with an SRP target. Using more than one channel improves performance if the HCA supports multiple completion vectors. The default value is the minimum of four times the number of online CPU sockets and the number of completion vectors supported by the HCA.");
143 
144 static int srp_add_one(struct ib_device *device);
145 static void srp_remove_one(struct ib_device *device, void *client_data);
146 static void srp_rename_dev(struct ib_device *device, void *client_data);
147 static void srp_recv_done(struct ib_cq *cq, struct ib_wc *wc);
148 static void srp_handle_qp_err(struct ib_cq *cq, struct ib_wc *wc,
149 		const char *opname);
150 static int srp_ib_cm_handler(struct ib_cm_id *cm_id,
151 			     const struct ib_cm_event *event);
152 static int srp_rdma_cm_handler(struct rdma_cm_id *cm_id,
153 			       struct rdma_cm_event *event);
154 
155 static struct scsi_transport_template *ib_srp_transport_template;
156 static struct workqueue_struct *srp_remove_wq;
157 
158 static struct ib_client srp_client = {
159 	.name   = "srp",
160 	.add    = srp_add_one,
161 	.remove = srp_remove_one,
162 	.rename = srp_rename_dev
163 };
164 
165 static struct ib_sa_client srp_sa_client;
166 
srp_tmo_get(char * buffer,const struct kernel_param * kp)167 static int srp_tmo_get(char *buffer, const struct kernel_param *kp)
168 {
169 	int tmo = *(int *)kp->arg;
170 
171 	if (tmo >= 0)
172 		return sysfs_emit(buffer, "%d\n", tmo);
173 	else
174 		return sysfs_emit(buffer, "off\n");
175 }
176 
srp_tmo_set(const char * val,const struct kernel_param * kp)177 static int srp_tmo_set(const char *val, const struct kernel_param *kp)
178 {
179 	int tmo, res;
180 
181 	res = srp_parse_tmo(&tmo, val);
182 	if (res)
183 		goto out;
184 
185 	if (kp->arg == &srp_reconnect_delay)
186 		res = srp_tmo_valid(tmo, srp_fast_io_fail_tmo,
187 				    srp_dev_loss_tmo);
188 	else if (kp->arg == &srp_fast_io_fail_tmo)
189 		res = srp_tmo_valid(srp_reconnect_delay, tmo, srp_dev_loss_tmo);
190 	else
191 		res = srp_tmo_valid(srp_reconnect_delay, srp_fast_io_fail_tmo,
192 				    tmo);
193 	if (res)
194 		goto out;
195 	*(int *)kp->arg = tmo;
196 
197 out:
198 	return res;
199 }
200 
201 static const struct kernel_param_ops srp_tmo_ops = {
202 	.get = srp_tmo_get,
203 	.set = srp_tmo_set,
204 };
205 
host_to_target(struct Scsi_Host * host)206 static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
207 {
208 	return (struct srp_target_port *) host->hostdata;
209 }
210 
srp_target_info(struct Scsi_Host * host)211 static const char *srp_target_info(struct Scsi_Host *host)
212 {
213 	return host_to_target(host)->target_name;
214 }
215 
srp_target_is_topspin(struct srp_target_port * target)216 static int srp_target_is_topspin(struct srp_target_port *target)
217 {
218 	static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
219 	static const u8 cisco_oui[3]   = { 0x00, 0x1b, 0x0d };
220 
221 	return topspin_workarounds &&
222 		(!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
223 		 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
224 }
225 
srp_alloc_iu(struct srp_host * host,size_t size,gfp_t gfp_mask,enum dma_data_direction direction)226 static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
227 				   gfp_t gfp_mask,
228 				   enum dma_data_direction direction)
229 {
230 	struct srp_iu *iu;
231 
232 	iu = kmalloc(sizeof *iu, gfp_mask);
233 	if (!iu)
234 		goto out;
235 
236 	iu->buf = kzalloc(size, gfp_mask);
237 	if (!iu->buf)
238 		goto out_free_iu;
239 
240 	iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
241 				    direction);
242 	if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
243 		goto out_free_buf;
244 
245 	iu->size      = size;
246 	iu->direction = direction;
247 
248 	return iu;
249 
250 out_free_buf:
251 	kfree(iu->buf);
252 out_free_iu:
253 	kfree(iu);
254 out:
255 	return NULL;
256 }
257 
srp_free_iu(struct srp_host * host,struct srp_iu * iu)258 static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
259 {
260 	if (!iu)
261 		return;
262 
263 	ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
264 			    iu->direction);
265 	kfree(iu->buf);
266 	kfree(iu);
267 }
268 
srp_qp_event(struct ib_event * event,void * context)269 static void srp_qp_event(struct ib_event *event, void *context)
270 {
271 	pr_debug("QP event %s (%d)\n",
272 		 ib_event_msg(event->event), event->event);
273 }
274 
srp_init_ib_qp(struct srp_target_port * target,struct ib_qp * qp)275 static int srp_init_ib_qp(struct srp_target_port *target,
276 			  struct ib_qp *qp)
277 {
278 	struct ib_qp_attr *attr;
279 	int ret;
280 
281 	attr = kmalloc(sizeof *attr, GFP_KERNEL);
282 	if (!attr)
283 		return -ENOMEM;
284 
285 	ret = ib_find_cached_pkey(target->srp_host->srp_dev->dev,
286 				  target->srp_host->port,
287 				  be16_to_cpu(target->ib_cm.pkey),
288 				  &attr->pkey_index);
289 	if (ret)
290 		goto out;
291 
292 	attr->qp_state        = IB_QPS_INIT;
293 	attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
294 				    IB_ACCESS_REMOTE_WRITE);
295 	attr->port_num        = target->srp_host->port;
296 
297 	ret = ib_modify_qp(qp, attr,
298 			   IB_QP_STATE		|
299 			   IB_QP_PKEY_INDEX	|
300 			   IB_QP_ACCESS_FLAGS	|
301 			   IB_QP_PORT);
302 
303 out:
304 	kfree(attr);
305 	return ret;
306 }
307 
srp_new_ib_cm_id(struct srp_rdma_ch * ch)308 static int srp_new_ib_cm_id(struct srp_rdma_ch *ch)
309 {
310 	struct srp_target_port *target = ch->target;
311 	struct ib_cm_id *new_cm_id;
312 
313 	new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
314 				    srp_ib_cm_handler, ch);
315 	if (IS_ERR(new_cm_id))
316 		return PTR_ERR(new_cm_id);
317 
318 	if (ch->ib_cm.cm_id)
319 		ib_destroy_cm_id(ch->ib_cm.cm_id);
320 	ch->ib_cm.cm_id = new_cm_id;
321 	if (rdma_cap_opa_ah(target->srp_host->srp_dev->dev,
322 			    target->srp_host->port))
323 		ch->ib_cm.path.rec_type = SA_PATH_REC_TYPE_OPA;
324 	else
325 		ch->ib_cm.path.rec_type = SA_PATH_REC_TYPE_IB;
326 	ch->ib_cm.path.sgid = target->sgid;
327 	ch->ib_cm.path.dgid = target->ib_cm.orig_dgid;
328 	ch->ib_cm.path.pkey = target->ib_cm.pkey;
329 	ch->ib_cm.path.service_id = target->ib_cm.service_id;
330 
331 	return 0;
332 }
333 
srp_new_rdma_cm_id(struct srp_rdma_ch * ch)334 static int srp_new_rdma_cm_id(struct srp_rdma_ch *ch)
335 {
336 	struct srp_target_port *target = ch->target;
337 	struct rdma_cm_id *new_cm_id;
338 	int ret;
339 
340 	new_cm_id = rdma_create_id(target->net, srp_rdma_cm_handler, ch,
341 				   RDMA_PS_TCP, IB_QPT_RC);
342 	if (IS_ERR(new_cm_id)) {
343 		ret = PTR_ERR(new_cm_id);
344 		new_cm_id = NULL;
345 		goto out;
346 	}
347 
348 	init_completion(&ch->done);
349 	ret = rdma_resolve_addr(new_cm_id, target->rdma_cm.src_specified ?
350 				&target->rdma_cm.src.sa : NULL,
351 				&target->rdma_cm.dst.sa,
352 				SRP_PATH_REC_TIMEOUT_MS);
353 	if (ret) {
354 		pr_err("No route available from %pISpsc to %pISpsc (%d)\n",
355 		       &target->rdma_cm.src, &target->rdma_cm.dst, ret);
356 		goto out;
357 	}
358 	ret = wait_for_completion_interruptible(&ch->done);
359 	if (ret < 0)
360 		goto out;
361 
362 	ret = ch->status;
363 	if (ret) {
364 		pr_err("Resolving address %pISpsc failed (%d)\n",
365 		       &target->rdma_cm.dst, ret);
366 		goto out;
367 	}
368 
369 	swap(ch->rdma_cm.cm_id, new_cm_id);
370 
371 out:
372 	if (new_cm_id)
373 		rdma_destroy_id(new_cm_id);
374 
375 	return ret;
376 }
377 
srp_new_cm_id(struct srp_rdma_ch * ch)378 static int srp_new_cm_id(struct srp_rdma_ch *ch)
379 {
380 	struct srp_target_port *target = ch->target;
381 
382 	return target->using_rdma_cm ? srp_new_rdma_cm_id(ch) :
383 		srp_new_ib_cm_id(ch);
384 }
385 
386 /**
387  * srp_destroy_fr_pool() - free the resources owned by a pool
388  * @pool: Fast registration pool to be destroyed.
389  */
srp_destroy_fr_pool(struct srp_fr_pool * pool)390 static void srp_destroy_fr_pool(struct srp_fr_pool *pool)
391 {
392 	int i;
393 	struct srp_fr_desc *d;
394 
395 	if (!pool)
396 		return;
397 
398 	for (i = 0, d = &pool->desc[0]; i < pool->size; i++, d++) {
399 		if (d->mr)
400 			ib_dereg_mr(d->mr);
401 	}
402 	kfree(pool);
403 }
404 
405 /**
406  * srp_create_fr_pool() - allocate and initialize a pool for fast registration
407  * @device:            IB device to allocate fast registration descriptors for.
408  * @pd:                Protection domain associated with the FR descriptors.
409  * @pool_size:         Number of descriptors to allocate.
410  * @max_page_list_len: Maximum fast registration work request page list length.
411  */
srp_create_fr_pool(struct ib_device * device,struct ib_pd * pd,int pool_size,int max_page_list_len)412 static struct srp_fr_pool *srp_create_fr_pool(struct ib_device *device,
413 					      struct ib_pd *pd, int pool_size,
414 					      int max_page_list_len)
415 {
416 	struct srp_fr_pool *pool;
417 	struct srp_fr_desc *d;
418 	struct ib_mr *mr;
419 	int i, ret = -EINVAL;
420 	enum ib_mr_type mr_type;
421 
422 	if (pool_size <= 0)
423 		goto err;
424 	ret = -ENOMEM;
425 	pool = kzalloc(struct_size(pool, desc, pool_size), GFP_KERNEL);
426 	if (!pool)
427 		goto err;
428 	pool->size = pool_size;
429 	pool->max_page_list_len = max_page_list_len;
430 	spin_lock_init(&pool->lock);
431 	INIT_LIST_HEAD(&pool->free_list);
432 
433 	if (device->attrs.device_cap_flags & IB_DEVICE_SG_GAPS_REG)
434 		mr_type = IB_MR_TYPE_SG_GAPS;
435 	else
436 		mr_type = IB_MR_TYPE_MEM_REG;
437 
438 	for (i = 0, d = &pool->desc[0]; i < pool->size; i++, d++) {
439 		mr = ib_alloc_mr(pd, mr_type, max_page_list_len);
440 		if (IS_ERR(mr)) {
441 			ret = PTR_ERR(mr);
442 			if (ret == -ENOMEM)
443 				pr_info("%s: ib_alloc_mr() failed. Try to reduce max_cmd_per_lun, max_sect or ch_count\n",
444 					dev_name(&device->dev));
445 			goto destroy_pool;
446 		}
447 		d->mr = mr;
448 		list_add_tail(&d->entry, &pool->free_list);
449 	}
450 
451 out:
452 	return pool;
453 
454 destroy_pool:
455 	srp_destroy_fr_pool(pool);
456 
457 err:
458 	pool = ERR_PTR(ret);
459 	goto out;
460 }
461 
462 /**
463  * srp_fr_pool_get() - obtain a descriptor suitable for fast registration
464  * @pool: Pool to obtain descriptor from.
465  */
srp_fr_pool_get(struct srp_fr_pool * pool)466 static struct srp_fr_desc *srp_fr_pool_get(struct srp_fr_pool *pool)
467 {
468 	struct srp_fr_desc *d = NULL;
469 	unsigned long flags;
470 
471 	spin_lock_irqsave(&pool->lock, flags);
472 	if (!list_empty(&pool->free_list)) {
473 		d = list_first_entry(&pool->free_list, typeof(*d), entry);
474 		list_del(&d->entry);
475 	}
476 	spin_unlock_irqrestore(&pool->lock, flags);
477 
478 	return d;
479 }
480 
481 /**
482  * srp_fr_pool_put() - put an FR descriptor back in the free list
483  * @pool: Pool the descriptor was allocated from.
484  * @desc: Pointer to an array of fast registration descriptor pointers.
485  * @n:    Number of descriptors to put back.
486  *
487  * Note: The caller must already have queued an invalidation request for
488  * desc->mr->rkey before calling this function.
489  */
srp_fr_pool_put(struct srp_fr_pool * pool,struct srp_fr_desc ** desc,int n)490 static void srp_fr_pool_put(struct srp_fr_pool *pool, struct srp_fr_desc **desc,
491 			    int n)
492 {
493 	unsigned long flags;
494 	int i;
495 
496 	spin_lock_irqsave(&pool->lock, flags);
497 	for (i = 0; i < n; i++)
498 		list_add(&desc[i]->entry, &pool->free_list);
499 	spin_unlock_irqrestore(&pool->lock, flags);
500 }
501 
srp_alloc_fr_pool(struct srp_target_port * target)502 static struct srp_fr_pool *srp_alloc_fr_pool(struct srp_target_port *target)
503 {
504 	struct srp_device *dev = target->srp_host->srp_dev;
505 
506 	return srp_create_fr_pool(dev->dev, dev->pd, target->mr_pool_size,
507 				  dev->max_pages_per_mr);
508 }
509 
510 /**
511  * srp_destroy_qp() - destroy an RDMA queue pair
512  * @ch: SRP RDMA channel.
513  *
514  * Drain the qp before destroying it.  This avoids that the receive
515  * completion handler can access the queue pair while it is
516  * being destroyed.
517  */
srp_destroy_qp(struct srp_rdma_ch * ch)518 static void srp_destroy_qp(struct srp_rdma_ch *ch)
519 {
520 	spin_lock_irq(&ch->lock);
521 	ib_process_cq_direct(ch->send_cq, -1);
522 	spin_unlock_irq(&ch->lock);
523 
524 	ib_drain_qp(ch->qp);
525 	ib_destroy_qp(ch->qp);
526 }
527 
srp_create_ch_ib(struct srp_rdma_ch * ch)528 static int srp_create_ch_ib(struct srp_rdma_ch *ch)
529 {
530 	struct srp_target_port *target = ch->target;
531 	struct srp_device *dev = target->srp_host->srp_dev;
532 	const struct ib_device_attr *attr = &dev->dev->attrs;
533 	struct ib_qp_init_attr *init_attr;
534 	struct ib_cq *recv_cq, *send_cq;
535 	struct ib_qp *qp;
536 	struct srp_fr_pool *fr_pool = NULL;
537 	const int m = 1 + dev->use_fast_reg * target->mr_per_cmd * 2;
538 	int ret;
539 
540 	init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
541 	if (!init_attr)
542 		return -ENOMEM;
543 
544 	/* queue_size + 1 for ib_drain_rq() */
545 	recv_cq = ib_alloc_cq(dev->dev, ch, target->queue_size + 1,
546 				ch->comp_vector, IB_POLL_SOFTIRQ);
547 	if (IS_ERR(recv_cq)) {
548 		ret = PTR_ERR(recv_cq);
549 		goto err;
550 	}
551 
552 	send_cq = ib_alloc_cq(dev->dev, ch, m * target->queue_size,
553 				ch->comp_vector, IB_POLL_DIRECT);
554 	if (IS_ERR(send_cq)) {
555 		ret = PTR_ERR(send_cq);
556 		goto err_recv_cq;
557 	}
558 
559 	init_attr->event_handler       = srp_qp_event;
560 	init_attr->cap.max_send_wr     = m * target->queue_size;
561 	init_attr->cap.max_recv_wr     = target->queue_size + 1;
562 	init_attr->cap.max_recv_sge    = 1;
563 	init_attr->cap.max_send_sge    = min(SRP_MAX_SGE, attr->max_send_sge);
564 	init_attr->sq_sig_type         = IB_SIGNAL_REQ_WR;
565 	init_attr->qp_type             = IB_QPT_RC;
566 	init_attr->send_cq             = send_cq;
567 	init_attr->recv_cq             = recv_cq;
568 
569 	ch->max_imm_sge = min(init_attr->cap.max_send_sge - 1U, 255U);
570 
571 	if (target->using_rdma_cm) {
572 		ret = rdma_create_qp(ch->rdma_cm.cm_id, dev->pd, init_attr);
573 		qp = ch->rdma_cm.cm_id->qp;
574 	} else {
575 		qp = ib_create_qp(dev->pd, init_attr);
576 		if (!IS_ERR(qp)) {
577 			ret = srp_init_ib_qp(target, qp);
578 			if (ret)
579 				ib_destroy_qp(qp);
580 		} else {
581 			ret = PTR_ERR(qp);
582 		}
583 	}
584 	if (ret) {
585 		pr_err("QP creation failed for dev %s: %d\n",
586 		       dev_name(&dev->dev->dev), ret);
587 		goto err_send_cq;
588 	}
589 
590 	if (dev->use_fast_reg) {
591 		fr_pool = srp_alloc_fr_pool(target);
592 		if (IS_ERR(fr_pool)) {
593 			ret = PTR_ERR(fr_pool);
594 			shost_printk(KERN_WARNING, target->scsi_host, PFX
595 				     "FR pool allocation failed (%d)\n", ret);
596 			goto err_qp;
597 		}
598 	}
599 
600 	if (ch->qp)
601 		srp_destroy_qp(ch);
602 	if (ch->recv_cq)
603 		ib_free_cq(ch->recv_cq);
604 	if (ch->send_cq)
605 		ib_free_cq(ch->send_cq);
606 
607 	ch->qp = qp;
608 	ch->recv_cq = recv_cq;
609 	ch->send_cq = send_cq;
610 
611 	if (dev->use_fast_reg) {
612 		if (ch->fr_pool)
613 			srp_destroy_fr_pool(ch->fr_pool);
614 		ch->fr_pool = fr_pool;
615 	}
616 
617 	kfree(init_attr);
618 	return 0;
619 
620 err_qp:
621 	if (target->using_rdma_cm)
622 		rdma_destroy_qp(ch->rdma_cm.cm_id);
623 	else
624 		ib_destroy_qp(qp);
625 
626 err_send_cq:
627 	ib_free_cq(send_cq);
628 
629 err_recv_cq:
630 	ib_free_cq(recv_cq);
631 
632 err:
633 	kfree(init_attr);
634 	return ret;
635 }
636 
637 /*
638  * Note: this function may be called without srp_alloc_iu_bufs() having been
639  * invoked. Hence the ch->[rt]x_ring checks.
640  */
srp_free_ch_ib(struct srp_target_port * target,struct srp_rdma_ch * ch)641 static void srp_free_ch_ib(struct srp_target_port *target,
642 			   struct srp_rdma_ch *ch)
643 {
644 	struct srp_device *dev = target->srp_host->srp_dev;
645 	int i;
646 
647 	if (!ch->target)
648 		return;
649 
650 	if (target->using_rdma_cm) {
651 		if (ch->rdma_cm.cm_id) {
652 			rdma_destroy_id(ch->rdma_cm.cm_id);
653 			ch->rdma_cm.cm_id = NULL;
654 		}
655 	} else {
656 		if (ch->ib_cm.cm_id) {
657 			ib_destroy_cm_id(ch->ib_cm.cm_id);
658 			ch->ib_cm.cm_id = NULL;
659 		}
660 	}
661 
662 	/* If srp_new_cm_id() succeeded but srp_create_ch_ib() not, return. */
663 	if (!ch->qp)
664 		return;
665 
666 	if (dev->use_fast_reg) {
667 		if (ch->fr_pool)
668 			srp_destroy_fr_pool(ch->fr_pool);
669 	}
670 
671 	srp_destroy_qp(ch);
672 	ib_free_cq(ch->send_cq);
673 	ib_free_cq(ch->recv_cq);
674 
675 	/*
676 	 * Avoid that the SCSI error handler tries to use this channel after
677 	 * it has been freed. The SCSI error handler can namely continue
678 	 * trying to perform recovery actions after scsi_remove_host()
679 	 * returned.
680 	 */
681 	ch->target = NULL;
682 
683 	ch->qp = NULL;
684 	ch->send_cq = ch->recv_cq = NULL;
685 
686 	if (ch->rx_ring) {
687 		for (i = 0; i < target->queue_size; ++i)
688 			srp_free_iu(target->srp_host, ch->rx_ring[i]);
689 		kfree(ch->rx_ring);
690 		ch->rx_ring = NULL;
691 	}
692 	if (ch->tx_ring) {
693 		for (i = 0; i < target->queue_size; ++i)
694 			srp_free_iu(target->srp_host, ch->tx_ring[i]);
695 		kfree(ch->tx_ring);
696 		ch->tx_ring = NULL;
697 	}
698 }
699 
srp_path_rec_completion(int status,struct sa_path_rec * pathrec,void * ch_ptr)700 static void srp_path_rec_completion(int status,
701 				    struct sa_path_rec *pathrec,
702 				    void *ch_ptr)
703 {
704 	struct srp_rdma_ch *ch = ch_ptr;
705 	struct srp_target_port *target = ch->target;
706 
707 	ch->status = status;
708 	if (status)
709 		shost_printk(KERN_ERR, target->scsi_host,
710 			     PFX "Got failed path rec status %d\n", status);
711 	else
712 		ch->ib_cm.path = *pathrec;
713 	complete(&ch->done);
714 }
715 
srp_ib_lookup_path(struct srp_rdma_ch * ch)716 static int srp_ib_lookup_path(struct srp_rdma_ch *ch)
717 {
718 	struct srp_target_port *target = ch->target;
719 	int ret;
720 
721 	ch->ib_cm.path.numb_path = 1;
722 
723 	init_completion(&ch->done);
724 
725 	ch->ib_cm.path_query_id = ib_sa_path_rec_get(&srp_sa_client,
726 					       target->srp_host->srp_dev->dev,
727 					       target->srp_host->port,
728 					       &ch->ib_cm.path,
729 					       IB_SA_PATH_REC_SERVICE_ID |
730 					       IB_SA_PATH_REC_DGID	 |
731 					       IB_SA_PATH_REC_SGID	 |
732 					       IB_SA_PATH_REC_NUMB_PATH	 |
733 					       IB_SA_PATH_REC_PKEY,
734 					       SRP_PATH_REC_TIMEOUT_MS,
735 					       GFP_KERNEL,
736 					       srp_path_rec_completion,
737 					       ch, &ch->ib_cm.path_query);
738 	if (ch->ib_cm.path_query_id < 0)
739 		return ch->ib_cm.path_query_id;
740 
741 	ret = wait_for_completion_interruptible(&ch->done);
742 	if (ret < 0)
743 		return ret;
744 
745 	if (ch->status < 0)
746 		shost_printk(KERN_WARNING, target->scsi_host,
747 			     PFX "Path record query failed: sgid %pI6, dgid %pI6, pkey %#04x, service_id %#16llx\n",
748 			     ch->ib_cm.path.sgid.raw, ch->ib_cm.path.dgid.raw,
749 			     be16_to_cpu(target->ib_cm.pkey),
750 			     be64_to_cpu(target->ib_cm.service_id));
751 
752 	return ch->status;
753 }
754 
srp_rdma_lookup_path(struct srp_rdma_ch * ch)755 static int srp_rdma_lookup_path(struct srp_rdma_ch *ch)
756 {
757 	struct srp_target_port *target = ch->target;
758 	int ret;
759 
760 	init_completion(&ch->done);
761 
762 	ret = rdma_resolve_route(ch->rdma_cm.cm_id, SRP_PATH_REC_TIMEOUT_MS);
763 	if (ret)
764 		return ret;
765 
766 	wait_for_completion_interruptible(&ch->done);
767 
768 	if (ch->status != 0)
769 		shost_printk(KERN_WARNING, target->scsi_host,
770 			     PFX "Path resolution failed\n");
771 
772 	return ch->status;
773 }
774 
srp_lookup_path(struct srp_rdma_ch * ch)775 static int srp_lookup_path(struct srp_rdma_ch *ch)
776 {
777 	struct srp_target_port *target = ch->target;
778 
779 	return target->using_rdma_cm ? srp_rdma_lookup_path(ch) :
780 		srp_ib_lookup_path(ch);
781 }
782 
srp_get_subnet_timeout(struct srp_host * host)783 static u8 srp_get_subnet_timeout(struct srp_host *host)
784 {
785 	struct ib_port_attr attr;
786 	int ret;
787 	u8 subnet_timeout = 18;
788 
789 	ret = ib_query_port(host->srp_dev->dev, host->port, &attr);
790 	if (ret == 0)
791 		subnet_timeout = attr.subnet_timeout;
792 
793 	if (unlikely(subnet_timeout < 15))
794 		pr_warn("%s: subnet timeout %d may cause SRP login to fail.\n",
795 			dev_name(&host->srp_dev->dev->dev), subnet_timeout);
796 
797 	return subnet_timeout;
798 }
799 
srp_send_req(struct srp_rdma_ch * ch,uint32_t max_iu_len,bool multich)800 static int srp_send_req(struct srp_rdma_ch *ch, uint32_t max_iu_len,
801 			bool multich)
802 {
803 	struct srp_target_port *target = ch->target;
804 	struct {
805 		struct rdma_conn_param	  rdma_param;
806 		struct srp_login_req_rdma rdma_req;
807 		struct ib_cm_req_param	  ib_param;
808 		struct srp_login_req	  ib_req;
809 	} *req = NULL;
810 	char *ipi, *tpi;
811 	int status;
812 
813 	req = kzalloc(sizeof *req, GFP_KERNEL);
814 	if (!req)
815 		return -ENOMEM;
816 
817 	req->ib_param.flow_control = 1;
818 	req->ib_param.retry_count = target->tl_retry_count;
819 
820 	/*
821 	 * Pick some arbitrary defaults here; we could make these
822 	 * module parameters if anyone cared about setting them.
823 	 */
824 	req->ib_param.responder_resources = 4;
825 	req->ib_param.rnr_retry_count = 7;
826 	req->ib_param.max_cm_retries = 15;
827 
828 	req->ib_req.opcode = SRP_LOGIN_REQ;
829 	req->ib_req.tag = 0;
830 	req->ib_req.req_it_iu_len = cpu_to_be32(max_iu_len);
831 	req->ib_req.req_buf_fmt	= cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
832 					      SRP_BUF_FORMAT_INDIRECT);
833 	req->ib_req.req_flags = (multich ? SRP_MULTICHAN_MULTI :
834 				 SRP_MULTICHAN_SINGLE);
835 	if (srp_use_imm_data) {
836 		req->ib_req.req_flags |= SRP_IMMED_REQUESTED;
837 		req->ib_req.imm_data_offset = cpu_to_be16(SRP_IMM_DATA_OFFSET);
838 	}
839 
840 	if (target->using_rdma_cm) {
841 		req->rdma_param.flow_control = req->ib_param.flow_control;
842 		req->rdma_param.responder_resources =
843 			req->ib_param.responder_resources;
844 		req->rdma_param.initiator_depth = req->ib_param.initiator_depth;
845 		req->rdma_param.retry_count = req->ib_param.retry_count;
846 		req->rdma_param.rnr_retry_count = req->ib_param.rnr_retry_count;
847 		req->rdma_param.private_data = &req->rdma_req;
848 		req->rdma_param.private_data_len = sizeof(req->rdma_req);
849 
850 		req->rdma_req.opcode = req->ib_req.opcode;
851 		req->rdma_req.tag = req->ib_req.tag;
852 		req->rdma_req.req_it_iu_len = req->ib_req.req_it_iu_len;
853 		req->rdma_req.req_buf_fmt = req->ib_req.req_buf_fmt;
854 		req->rdma_req.req_flags	= req->ib_req.req_flags;
855 		req->rdma_req.imm_data_offset = req->ib_req.imm_data_offset;
856 
857 		ipi = req->rdma_req.initiator_port_id;
858 		tpi = req->rdma_req.target_port_id;
859 	} else {
860 		u8 subnet_timeout;
861 
862 		subnet_timeout = srp_get_subnet_timeout(target->srp_host);
863 
864 		req->ib_param.primary_path = &ch->ib_cm.path;
865 		req->ib_param.alternate_path = NULL;
866 		req->ib_param.service_id = target->ib_cm.service_id;
867 		get_random_bytes(&req->ib_param.starting_psn, 4);
868 		req->ib_param.starting_psn &= 0xffffff;
869 		req->ib_param.qp_num = ch->qp->qp_num;
870 		req->ib_param.qp_type = ch->qp->qp_type;
871 		req->ib_param.local_cm_response_timeout = subnet_timeout + 2;
872 		req->ib_param.remote_cm_response_timeout = subnet_timeout + 2;
873 		req->ib_param.private_data = &req->ib_req;
874 		req->ib_param.private_data_len = sizeof(req->ib_req);
875 
876 		ipi = req->ib_req.initiator_port_id;
877 		tpi = req->ib_req.target_port_id;
878 	}
879 
880 	/*
881 	 * In the published SRP specification (draft rev. 16a), the
882 	 * port identifier format is 8 bytes of ID extension followed
883 	 * by 8 bytes of GUID.  Older drafts put the two halves in the
884 	 * opposite order, so that the GUID comes first.
885 	 *
886 	 * Targets conforming to these obsolete drafts can be
887 	 * recognized by the I/O Class they report.
888 	 */
889 	if (target->io_class == SRP_REV10_IB_IO_CLASS) {
890 		memcpy(ipi,     &target->sgid.global.interface_id, 8);
891 		memcpy(ipi + 8, &target->initiator_ext, 8);
892 		memcpy(tpi,     &target->ioc_guid, 8);
893 		memcpy(tpi + 8, &target->id_ext, 8);
894 	} else {
895 		memcpy(ipi,     &target->initiator_ext, 8);
896 		memcpy(ipi + 8, &target->sgid.global.interface_id, 8);
897 		memcpy(tpi,     &target->id_ext, 8);
898 		memcpy(tpi + 8, &target->ioc_guid, 8);
899 	}
900 
901 	/*
902 	 * Topspin/Cisco SRP targets will reject our login unless we
903 	 * zero out the first 8 bytes of our initiator port ID and set
904 	 * the second 8 bytes to the local node GUID.
905 	 */
906 	if (srp_target_is_topspin(target)) {
907 		shost_printk(KERN_DEBUG, target->scsi_host,
908 			     PFX "Topspin/Cisco initiator port ID workaround "
909 			     "activated for target GUID %016llx\n",
910 			     be64_to_cpu(target->ioc_guid));
911 		memset(ipi, 0, 8);
912 		memcpy(ipi + 8, &target->srp_host->srp_dev->dev->node_guid, 8);
913 	}
914 
915 	if (target->using_rdma_cm)
916 		status = rdma_connect(ch->rdma_cm.cm_id, &req->rdma_param);
917 	else
918 		status = ib_send_cm_req(ch->ib_cm.cm_id, &req->ib_param);
919 
920 	kfree(req);
921 
922 	return status;
923 }
924 
srp_queue_remove_work(struct srp_target_port * target)925 static bool srp_queue_remove_work(struct srp_target_port *target)
926 {
927 	bool changed = false;
928 
929 	spin_lock_irq(&target->lock);
930 	if (target->state != SRP_TARGET_REMOVED) {
931 		target->state = SRP_TARGET_REMOVED;
932 		changed = true;
933 	}
934 	spin_unlock_irq(&target->lock);
935 
936 	if (changed)
937 		queue_work(srp_remove_wq, &target->remove_work);
938 
939 	return changed;
940 }
941 
srp_disconnect_target(struct srp_target_port * target)942 static void srp_disconnect_target(struct srp_target_port *target)
943 {
944 	struct srp_rdma_ch *ch;
945 	int i, ret;
946 
947 	/* XXX should send SRP_I_LOGOUT request */
948 
949 	for (i = 0; i < target->ch_count; i++) {
950 		ch = &target->ch[i];
951 		ch->connected = false;
952 		ret = 0;
953 		if (target->using_rdma_cm) {
954 			if (ch->rdma_cm.cm_id)
955 				rdma_disconnect(ch->rdma_cm.cm_id);
956 		} else {
957 			if (ch->ib_cm.cm_id)
958 				ret = ib_send_cm_dreq(ch->ib_cm.cm_id,
959 						      NULL, 0);
960 		}
961 		if (ret < 0) {
962 			shost_printk(KERN_DEBUG, target->scsi_host,
963 				     PFX "Sending CM DREQ failed\n");
964 		}
965 	}
966 }
967 
srp_exit_cmd_priv(struct Scsi_Host * shost,struct scsi_cmnd * cmd)968 static int srp_exit_cmd_priv(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
969 {
970 	struct srp_target_port *target = host_to_target(shost);
971 	struct srp_device *dev = target->srp_host->srp_dev;
972 	struct ib_device *ibdev = dev->dev;
973 	struct srp_request *req = scsi_cmd_priv(cmd);
974 
975 	kfree(req->fr_list);
976 	if (req->indirect_dma_addr) {
977 		ib_dma_unmap_single(ibdev, req->indirect_dma_addr,
978 				    target->indirect_size,
979 				    DMA_TO_DEVICE);
980 	}
981 	kfree(req->indirect_desc);
982 
983 	return 0;
984 }
985 
srp_init_cmd_priv(struct Scsi_Host * shost,struct scsi_cmnd * cmd)986 static int srp_init_cmd_priv(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
987 {
988 	struct srp_target_port *target = host_to_target(shost);
989 	struct srp_device *srp_dev = target->srp_host->srp_dev;
990 	struct ib_device *ibdev = srp_dev->dev;
991 	struct srp_request *req = scsi_cmd_priv(cmd);
992 	dma_addr_t dma_addr;
993 	int ret = -ENOMEM;
994 
995 	if (srp_dev->use_fast_reg) {
996 		req->fr_list = kmalloc_array(target->mr_per_cmd, sizeof(void *),
997 					GFP_KERNEL);
998 		if (!req->fr_list)
999 			goto out;
1000 	}
1001 	req->indirect_desc = kmalloc(target->indirect_size, GFP_KERNEL);
1002 	if (!req->indirect_desc)
1003 		goto out;
1004 
1005 	dma_addr = ib_dma_map_single(ibdev, req->indirect_desc,
1006 				     target->indirect_size,
1007 				     DMA_TO_DEVICE);
1008 	if (ib_dma_mapping_error(ibdev, dma_addr)) {
1009 		srp_exit_cmd_priv(shost, cmd);
1010 		goto out;
1011 	}
1012 
1013 	req->indirect_dma_addr = dma_addr;
1014 	ret = 0;
1015 
1016 out:
1017 	return ret;
1018 }
1019 
1020 /**
1021  * srp_del_scsi_host_attr() - Remove attributes defined in the host template.
1022  * @shost: SCSI host whose attributes to remove from sysfs.
1023  *
1024  * Note: Any attributes defined in the host template and that did not exist
1025  * before invocation of this function will be ignored.
1026  */
srp_del_scsi_host_attr(struct Scsi_Host * shost)1027 static void srp_del_scsi_host_attr(struct Scsi_Host *shost)
1028 {
1029 	struct device_attribute **attr;
1030 
1031 	for (attr = shost->hostt->shost_attrs; attr && *attr; ++attr)
1032 		device_remove_file(&shost->shost_dev, *attr);
1033 }
1034 
srp_remove_target(struct srp_target_port * target)1035 static void srp_remove_target(struct srp_target_port *target)
1036 {
1037 	struct srp_rdma_ch *ch;
1038 	int i;
1039 
1040 	WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
1041 
1042 	srp_del_scsi_host_attr(target->scsi_host);
1043 	srp_rport_get(target->rport);
1044 	srp_remove_host(target->scsi_host);
1045 	scsi_remove_host(target->scsi_host);
1046 	srp_stop_rport_timers(target->rport);
1047 	srp_disconnect_target(target);
1048 	kobj_ns_drop(KOBJ_NS_TYPE_NET, target->net);
1049 	for (i = 0; i < target->ch_count; i++) {
1050 		ch = &target->ch[i];
1051 		srp_free_ch_ib(target, ch);
1052 	}
1053 	cancel_work_sync(&target->tl_err_work);
1054 	srp_rport_put(target->rport);
1055 	kfree(target->ch);
1056 	target->ch = NULL;
1057 
1058 	spin_lock(&target->srp_host->target_lock);
1059 	list_del(&target->list);
1060 	spin_unlock(&target->srp_host->target_lock);
1061 
1062 	scsi_host_put(target->scsi_host);
1063 }
1064 
srp_remove_work(struct work_struct * work)1065 static void srp_remove_work(struct work_struct *work)
1066 {
1067 	struct srp_target_port *target =
1068 		container_of(work, struct srp_target_port, remove_work);
1069 
1070 	WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
1071 
1072 	srp_remove_target(target);
1073 }
1074 
srp_rport_delete(struct srp_rport * rport)1075 static void srp_rport_delete(struct srp_rport *rport)
1076 {
1077 	struct srp_target_port *target = rport->lld_data;
1078 
1079 	srp_queue_remove_work(target);
1080 }
1081 
1082 /**
1083  * srp_connected_ch() - number of connected channels
1084  * @target: SRP target port.
1085  */
srp_connected_ch(struct srp_target_port * target)1086 static int srp_connected_ch(struct srp_target_port *target)
1087 {
1088 	int i, c = 0;
1089 
1090 	for (i = 0; i < target->ch_count; i++)
1091 		c += target->ch[i].connected;
1092 
1093 	return c;
1094 }
1095 
srp_connect_ch(struct srp_rdma_ch * ch,uint32_t max_iu_len,bool multich)1096 static int srp_connect_ch(struct srp_rdma_ch *ch, uint32_t max_iu_len,
1097 			  bool multich)
1098 {
1099 	struct srp_target_port *target = ch->target;
1100 	int ret;
1101 
1102 	WARN_ON_ONCE(!multich && srp_connected_ch(target) > 0);
1103 
1104 	ret = srp_lookup_path(ch);
1105 	if (ret)
1106 		goto out;
1107 
1108 	while (1) {
1109 		init_completion(&ch->done);
1110 		ret = srp_send_req(ch, max_iu_len, multich);
1111 		if (ret)
1112 			goto out;
1113 		ret = wait_for_completion_interruptible(&ch->done);
1114 		if (ret < 0)
1115 			goto out;
1116 
1117 		/*
1118 		 * The CM event handling code will set status to
1119 		 * SRP_PORT_REDIRECT if we get a port redirect REJ
1120 		 * back, or SRP_DLID_REDIRECT if we get a lid/qp
1121 		 * redirect REJ back.
1122 		 */
1123 		ret = ch->status;
1124 		switch (ret) {
1125 		case 0:
1126 			ch->connected = true;
1127 			goto out;
1128 
1129 		case SRP_PORT_REDIRECT:
1130 			ret = srp_lookup_path(ch);
1131 			if (ret)
1132 				goto out;
1133 			break;
1134 
1135 		case SRP_DLID_REDIRECT:
1136 			break;
1137 
1138 		case SRP_STALE_CONN:
1139 			shost_printk(KERN_ERR, target->scsi_host, PFX
1140 				     "giving up on stale connection\n");
1141 			ret = -ECONNRESET;
1142 			goto out;
1143 
1144 		default:
1145 			goto out;
1146 		}
1147 	}
1148 
1149 out:
1150 	return ret <= 0 ? ret : -ENODEV;
1151 }
1152 
srp_inv_rkey_err_done(struct ib_cq * cq,struct ib_wc * wc)1153 static void srp_inv_rkey_err_done(struct ib_cq *cq, struct ib_wc *wc)
1154 {
1155 	srp_handle_qp_err(cq, wc, "INV RKEY");
1156 }
1157 
srp_inv_rkey(struct srp_request * req,struct srp_rdma_ch * ch,u32 rkey)1158 static int srp_inv_rkey(struct srp_request *req, struct srp_rdma_ch *ch,
1159 		u32 rkey)
1160 {
1161 	struct ib_send_wr wr = {
1162 		.opcode		    = IB_WR_LOCAL_INV,
1163 		.next		    = NULL,
1164 		.num_sge	    = 0,
1165 		.send_flags	    = 0,
1166 		.ex.invalidate_rkey = rkey,
1167 	};
1168 
1169 	wr.wr_cqe = &req->reg_cqe;
1170 	req->reg_cqe.done = srp_inv_rkey_err_done;
1171 	return ib_post_send(ch->qp, &wr, NULL);
1172 }
1173 
srp_unmap_data(struct scsi_cmnd * scmnd,struct srp_rdma_ch * ch,struct srp_request * req)1174 static void srp_unmap_data(struct scsi_cmnd *scmnd,
1175 			   struct srp_rdma_ch *ch,
1176 			   struct srp_request *req)
1177 {
1178 	struct srp_target_port *target = ch->target;
1179 	struct srp_device *dev = target->srp_host->srp_dev;
1180 	struct ib_device *ibdev = dev->dev;
1181 	int i, res;
1182 
1183 	if (!scsi_sglist(scmnd) ||
1184 	    (scmnd->sc_data_direction != DMA_TO_DEVICE &&
1185 	     scmnd->sc_data_direction != DMA_FROM_DEVICE))
1186 		return;
1187 
1188 	if (dev->use_fast_reg) {
1189 		struct srp_fr_desc **pfr;
1190 
1191 		for (i = req->nmdesc, pfr = req->fr_list; i > 0; i--, pfr++) {
1192 			res = srp_inv_rkey(req, ch, (*pfr)->mr->rkey);
1193 			if (res < 0) {
1194 				shost_printk(KERN_ERR, target->scsi_host, PFX
1195 				  "Queueing INV WR for rkey %#x failed (%d)\n",
1196 				  (*pfr)->mr->rkey, res);
1197 				queue_work(system_long_wq,
1198 					   &target->tl_err_work);
1199 			}
1200 		}
1201 		if (req->nmdesc)
1202 			srp_fr_pool_put(ch->fr_pool, req->fr_list,
1203 					req->nmdesc);
1204 	}
1205 
1206 	ib_dma_unmap_sg(ibdev, scsi_sglist(scmnd), scsi_sg_count(scmnd),
1207 			scmnd->sc_data_direction);
1208 }
1209 
1210 /**
1211  * srp_claim_req - Take ownership of the scmnd associated with a request.
1212  * @ch: SRP RDMA channel.
1213  * @req: SRP request.
1214  * @sdev: If not NULL, only take ownership for this SCSI device.
1215  * @scmnd: If NULL, take ownership of @req->scmnd. If not NULL, only take
1216  *         ownership of @req->scmnd if it equals @scmnd.
1217  *
1218  * Return value:
1219  * Either NULL or a pointer to the SCSI command the caller became owner of.
1220  */
srp_claim_req(struct srp_rdma_ch * ch,struct srp_request * req,struct scsi_device * sdev,struct scsi_cmnd * scmnd)1221 static struct scsi_cmnd *srp_claim_req(struct srp_rdma_ch *ch,
1222 				       struct srp_request *req,
1223 				       struct scsi_device *sdev,
1224 				       struct scsi_cmnd *scmnd)
1225 {
1226 	unsigned long flags;
1227 
1228 	spin_lock_irqsave(&ch->lock, flags);
1229 	if (req->scmnd &&
1230 	    (!sdev || req->scmnd->device == sdev) &&
1231 	    (!scmnd || req->scmnd == scmnd)) {
1232 		scmnd = req->scmnd;
1233 		req->scmnd = NULL;
1234 	} else {
1235 		scmnd = NULL;
1236 	}
1237 	spin_unlock_irqrestore(&ch->lock, flags);
1238 
1239 	return scmnd;
1240 }
1241 
1242 /**
1243  * srp_free_req() - Unmap data and adjust ch->req_lim.
1244  * @ch:     SRP RDMA channel.
1245  * @req:    Request to be freed.
1246  * @scmnd:  SCSI command associated with @req.
1247  * @req_lim_delta: Amount to be added to @target->req_lim.
1248  */
srp_free_req(struct srp_rdma_ch * ch,struct srp_request * req,struct scsi_cmnd * scmnd,s32 req_lim_delta)1249 static void srp_free_req(struct srp_rdma_ch *ch, struct srp_request *req,
1250 			 struct scsi_cmnd *scmnd, s32 req_lim_delta)
1251 {
1252 	unsigned long flags;
1253 
1254 	srp_unmap_data(scmnd, ch, req);
1255 
1256 	spin_lock_irqsave(&ch->lock, flags);
1257 	ch->req_lim += req_lim_delta;
1258 	spin_unlock_irqrestore(&ch->lock, flags);
1259 }
1260 
srp_finish_req(struct srp_rdma_ch * ch,struct srp_request * req,struct scsi_device * sdev,int result)1261 static void srp_finish_req(struct srp_rdma_ch *ch, struct srp_request *req,
1262 			   struct scsi_device *sdev, int result)
1263 {
1264 	struct scsi_cmnd *scmnd = srp_claim_req(ch, req, sdev, NULL);
1265 
1266 	if (scmnd) {
1267 		srp_free_req(ch, req, scmnd, 0);
1268 		scmnd->result = result;
1269 		scmnd->scsi_done(scmnd);
1270 	}
1271 }
1272 
1273 struct srp_terminate_context {
1274 	struct srp_target_port *srp_target;
1275 	int scsi_result;
1276 };
1277 
srp_terminate_cmd(struct scsi_cmnd * scmnd,void * context_ptr,bool reserved)1278 static bool srp_terminate_cmd(struct scsi_cmnd *scmnd, void *context_ptr,
1279 			      bool reserved)
1280 {
1281 	struct srp_terminate_context *context = context_ptr;
1282 	struct srp_target_port *target = context->srp_target;
1283 	u32 tag = blk_mq_unique_tag(scsi_cmd_to_rq(scmnd));
1284 	struct srp_rdma_ch *ch = &target->ch[blk_mq_unique_tag_to_hwq(tag)];
1285 	struct srp_request *req = scsi_cmd_priv(scmnd);
1286 
1287 	srp_finish_req(ch, req, NULL, context->scsi_result);
1288 
1289 	return true;
1290 }
1291 
srp_terminate_io(struct srp_rport * rport)1292 static void srp_terminate_io(struct srp_rport *rport)
1293 {
1294 	struct srp_target_port *target = rport->lld_data;
1295 	struct srp_terminate_context context = { .srp_target = target,
1296 		.scsi_result = DID_TRANSPORT_FAILFAST << 16 };
1297 
1298 	scsi_host_busy_iter(target->scsi_host, srp_terminate_cmd, &context);
1299 }
1300 
1301 /* Calculate maximum initiator to target information unit length. */
srp_max_it_iu_len(int cmd_sg_cnt,bool use_imm_data,uint32_t max_it_iu_size)1302 static uint32_t srp_max_it_iu_len(int cmd_sg_cnt, bool use_imm_data,
1303 				  uint32_t max_it_iu_size)
1304 {
1305 	uint32_t max_iu_len = sizeof(struct srp_cmd) + SRP_MAX_ADD_CDB_LEN +
1306 		sizeof(struct srp_indirect_buf) +
1307 		cmd_sg_cnt * sizeof(struct srp_direct_buf);
1308 
1309 	if (use_imm_data)
1310 		max_iu_len = max(max_iu_len, SRP_IMM_DATA_OFFSET +
1311 				 srp_max_imm_data);
1312 
1313 	if (max_it_iu_size)
1314 		max_iu_len = min(max_iu_len, max_it_iu_size);
1315 
1316 	pr_debug("max_iu_len = %d\n", max_iu_len);
1317 
1318 	return max_iu_len;
1319 }
1320 
1321 /*
1322  * It is up to the caller to ensure that srp_rport_reconnect() calls are
1323  * serialized and that no concurrent srp_queuecommand(), srp_abort(),
1324  * srp_reset_device() or srp_reset_host() calls will occur while this function
1325  * is in progress. One way to realize that is not to call this function
1326  * directly but to call srp_reconnect_rport() instead since that last function
1327  * serializes calls of this function via rport->mutex and also blocks
1328  * srp_queuecommand() calls before invoking this function.
1329  */
srp_rport_reconnect(struct srp_rport * rport)1330 static int srp_rport_reconnect(struct srp_rport *rport)
1331 {
1332 	struct srp_target_port *target = rport->lld_data;
1333 	struct srp_rdma_ch *ch;
1334 	uint32_t max_iu_len = srp_max_it_iu_len(target->cmd_sg_cnt,
1335 						srp_use_imm_data,
1336 						target->max_it_iu_size);
1337 	int i, j, ret = 0;
1338 	bool multich = false;
1339 
1340 	srp_disconnect_target(target);
1341 
1342 	if (target->state == SRP_TARGET_SCANNING)
1343 		return -ENODEV;
1344 
1345 	/*
1346 	 * Now get a new local CM ID so that we avoid confusing the target in
1347 	 * case things are really fouled up. Doing so also ensures that all CM
1348 	 * callbacks will have finished before a new QP is allocated.
1349 	 */
1350 	for (i = 0; i < target->ch_count; i++) {
1351 		ch = &target->ch[i];
1352 		ret += srp_new_cm_id(ch);
1353 	}
1354 	{
1355 		struct srp_terminate_context context = {
1356 			.srp_target = target, .scsi_result = DID_RESET << 16};
1357 
1358 		scsi_host_busy_iter(target->scsi_host, srp_terminate_cmd,
1359 				    &context);
1360 	}
1361 	for (i = 0; i < target->ch_count; i++) {
1362 		ch = &target->ch[i];
1363 		/*
1364 		 * Whether or not creating a new CM ID succeeded, create a new
1365 		 * QP. This guarantees that all completion callback function
1366 		 * invocations have finished before request resetting starts.
1367 		 */
1368 		ret += srp_create_ch_ib(ch);
1369 
1370 		INIT_LIST_HEAD(&ch->free_tx);
1371 		for (j = 0; j < target->queue_size; ++j)
1372 			list_add(&ch->tx_ring[j]->list, &ch->free_tx);
1373 	}
1374 
1375 	target->qp_in_error = false;
1376 
1377 	for (i = 0; i < target->ch_count; i++) {
1378 		ch = &target->ch[i];
1379 		if (ret)
1380 			break;
1381 		ret = srp_connect_ch(ch, max_iu_len, multich);
1382 		multich = true;
1383 	}
1384 
1385 	if (ret == 0)
1386 		shost_printk(KERN_INFO, target->scsi_host,
1387 			     PFX "reconnect succeeded\n");
1388 
1389 	return ret;
1390 }
1391 
srp_map_desc(struct srp_map_state * state,dma_addr_t dma_addr,unsigned int dma_len,u32 rkey)1392 static void srp_map_desc(struct srp_map_state *state, dma_addr_t dma_addr,
1393 			 unsigned int dma_len, u32 rkey)
1394 {
1395 	struct srp_direct_buf *desc = state->desc;
1396 
1397 	WARN_ON_ONCE(!dma_len);
1398 
1399 	desc->va = cpu_to_be64(dma_addr);
1400 	desc->key = cpu_to_be32(rkey);
1401 	desc->len = cpu_to_be32(dma_len);
1402 
1403 	state->total_len += dma_len;
1404 	state->desc++;
1405 	state->ndesc++;
1406 }
1407 
srp_reg_mr_err_done(struct ib_cq * cq,struct ib_wc * wc)1408 static void srp_reg_mr_err_done(struct ib_cq *cq, struct ib_wc *wc)
1409 {
1410 	srp_handle_qp_err(cq, wc, "FAST REG");
1411 }
1412 
1413 /*
1414  * Map up to sg_nents elements of state->sg where *sg_offset_p is the offset
1415  * where to start in the first element. If sg_offset_p != NULL then
1416  * *sg_offset_p is updated to the offset in state->sg[retval] of the first
1417  * byte that has not yet been mapped.
1418  */
srp_map_finish_fr(struct srp_map_state * state,struct srp_request * req,struct srp_rdma_ch * ch,int sg_nents,unsigned int * sg_offset_p)1419 static int srp_map_finish_fr(struct srp_map_state *state,
1420 			     struct srp_request *req,
1421 			     struct srp_rdma_ch *ch, int sg_nents,
1422 			     unsigned int *sg_offset_p)
1423 {
1424 	struct srp_target_port *target = ch->target;
1425 	struct srp_device *dev = target->srp_host->srp_dev;
1426 	struct ib_reg_wr wr;
1427 	struct srp_fr_desc *desc;
1428 	u32 rkey;
1429 	int n, err;
1430 
1431 	if (state->fr.next >= state->fr.end) {
1432 		shost_printk(KERN_ERR, ch->target->scsi_host,
1433 			     PFX "Out of MRs (mr_per_cmd = %d)\n",
1434 			     ch->target->mr_per_cmd);
1435 		return -ENOMEM;
1436 	}
1437 
1438 	WARN_ON_ONCE(!dev->use_fast_reg);
1439 
1440 	if (sg_nents == 1 && target->global_rkey) {
1441 		unsigned int sg_offset = sg_offset_p ? *sg_offset_p : 0;
1442 
1443 		srp_map_desc(state, sg_dma_address(state->sg) + sg_offset,
1444 			     sg_dma_len(state->sg) - sg_offset,
1445 			     target->global_rkey);
1446 		if (sg_offset_p)
1447 			*sg_offset_p = 0;
1448 		return 1;
1449 	}
1450 
1451 	desc = srp_fr_pool_get(ch->fr_pool);
1452 	if (!desc)
1453 		return -ENOMEM;
1454 
1455 	rkey = ib_inc_rkey(desc->mr->rkey);
1456 	ib_update_fast_reg_key(desc->mr, rkey);
1457 
1458 	n = ib_map_mr_sg(desc->mr, state->sg, sg_nents, sg_offset_p,
1459 			 dev->mr_page_size);
1460 	if (unlikely(n < 0)) {
1461 		srp_fr_pool_put(ch->fr_pool, &desc, 1);
1462 		pr_debug("%s: ib_map_mr_sg(%d, %d) returned %d.\n",
1463 			 dev_name(&req->scmnd->device->sdev_gendev), sg_nents,
1464 			 sg_offset_p ? *sg_offset_p : -1, n);
1465 		return n;
1466 	}
1467 
1468 	WARN_ON_ONCE(desc->mr->length == 0);
1469 
1470 	req->reg_cqe.done = srp_reg_mr_err_done;
1471 
1472 	wr.wr.next = NULL;
1473 	wr.wr.opcode = IB_WR_REG_MR;
1474 	wr.wr.wr_cqe = &req->reg_cqe;
1475 	wr.wr.num_sge = 0;
1476 	wr.wr.send_flags = 0;
1477 	wr.mr = desc->mr;
1478 	wr.key = desc->mr->rkey;
1479 	wr.access = (IB_ACCESS_LOCAL_WRITE |
1480 		     IB_ACCESS_REMOTE_READ |
1481 		     IB_ACCESS_REMOTE_WRITE);
1482 
1483 	*state->fr.next++ = desc;
1484 	state->nmdesc++;
1485 
1486 	srp_map_desc(state, desc->mr->iova,
1487 		     desc->mr->length, desc->mr->rkey);
1488 
1489 	err = ib_post_send(ch->qp, &wr.wr, NULL);
1490 	if (unlikely(err)) {
1491 		WARN_ON_ONCE(err == -ENOMEM);
1492 		return err;
1493 	}
1494 
1495 	return n;
1496 }
1497 
srp_map_sg_fr(struct srp_map_state * state,struct srp_rdma_ch * ch,struct srp_request * req,struct scatterlist * scat,int count)1498 static int srp_map_sg_fr(struct srp_map_state *state, struct srp_rdma_ch *ch,
1499 			 struct srp_request *req, struct scatterlist *scat,
1500 			 int count)
1501 {
1502 	unsigned int sg_offset = 0;
1503 
1504 	state->fr.next = req->fr_list;
1505 	state->fr.end = req->fr_list + ch->target->mr_per_cmd;
1506 	state->sg = scat;
1507 
1508 	if (count == 0)
1509 		return 0;
1510 
1511 	while (count) {
1512 		int i, n;
1513 
1514 		n = srp_map_finish_fr(state, req, ch, count, &sg_offset);
1515 		if (unlikely(n < 0))
1516 			return n;
1517 
1518 		count -= n;
1519 		for (i = 0; i < n; i++)
1520 			state->sg = sg_next(state->sg);
1521 	}
1522 
1523 	return 0;
1524 }
1525 
srp_map_sg_dma(struct srp_map_state * state,struct srp_rdma_ch * ch,struct srp_request * req,struct scatterlist * scat,int count)1526 static int srp_map_sg_dma(struct srp_map_state *state, struct srp_rdma_ch *ch,
1527 			  struct srp_request *req, struct scatterlist *scat,
1528 			  int count)
1529 {
1530 	struct srp_target_port *target = ch->target;
1531 	struct scatterlist *sg;
1532 	int i;
1533 
1534 	for_each_sg(scat, sg, count, i) {
1535 		srp_map_desc(state, sg_dma_address(sg), sg_dma_len(sg),
1536 			     target->global_rkey);
1537 	}
1538 
1539 	return 0;
1540 }
1541 
1542 /*
1543  * Register the indirect data buffer descriptor with the HCA.
1544  *
1545  * Note: since the indirect data buffer descriptor has been allocated with
1546  * kmalloc() it is guaranteed that this buffer is a physically contiguous
1547  * memory buffer.
1548  */
srp_map_idb(struct srp_rdma_ch * ch,struct srp_request * req,void ** next_mr,void ** end_mr,u32 idb_len,__be32 * idb_rkey)1549 static int srp_map_idb(struct srp_rdma_ch *ch, struct srp_request *req,
1550 		       void **next_mr, void **end_mr, u32 idb_len,
1551 		       __be32 *idb_rkey)
1552 {
1553 	struct srp_target_port *target = ch->target;
1554 	struct srp_device *dev = target->srp_host->srp_dev;
1555 	struct srp_map_state state;
1556 	struct srp_direct_buf idb_desc;
1557 	struct scatterlist idb_sg[1];
1558 	int ret;
1559 
1560 	memset(&state, 0, sizeof(state));
1561 	memset(&idb_desc, 0, sizeof(idb_desc));
1562 	state.gen.next = next_mr;
1563 	state.gen.end = end_mr;
1564 	state.desc = &idb_desc;
1565 	state.base_dma_addr = req->indirect_dma_addr;
1566 	state.dma_len = idb_len;
1567 
1568 	if (dev->use_fast_reg) {
1569 		state.sg = idb_sg;
1570 		sg_init_one(idb_sg, req->indirect_desc, idb_len);
1571 		idb_sg->dma_address = req->indirect_dma_addr; /* hack! */
1572 #ifdef CONFIG_NEED_SG_DMA_LENGTH
1573 		idb_sg->dma_length = idb_sg->length;	      /* hack^2 */
1574 #endif
1575 		ret = srp_map_finish_fr(&state, req, ch, 1, NULL);
1576 		if (ret < 0)
1577 			return ret;
1578 		WARN_ON_ONCE(ret < 1);
1579 	} else {
1580 		return -EINVAL;
1581 	}
1582 
1583 	*idb_rkey = idb_desc.key;
1584 
1585 	return 0;
1586 }
1587 
srp_check_mapping(struct srp_map_state * state,struct srp_rdma_ch * ch,struct srp_request * req,struct scatterlist * scat,int count)1588 static void srp_check_mapping(struct srp_map_state *state,
1589 			      struct srp_rdma_ch *ch, struct srp_request *req,
1590 			      struct scatterlist *scat, int count)
1591 {
1592 	struct srp_device *dev = ch->target->srp_host->srp_dev;
1593 	struct srp_fr_desc **pfr;
1594 	u64 desc_len = 0, mr_len = 0;
1595 	int i;
1596 
1597 	for (i = 0; i < state->ndesc; i++)
1598 		desc_len += be32_to_cpu(req->indirect_desc[i].len);
1599 	if (dev->use_fast_reg)
1600 		for (i = 0, pfr = req->fr_list; i < state->nmdesc; i++, pfr++)
1601 			mr_len += (*pfr)->mr->length;
1602 	if (desc_len != scsi_bufflen(req->scmnd) ||
1603 	    mr_len > scsi_bufflen(req->scmnd))
1604 		pr_err("Inconsistent: scsi len %d <> desc len %lld <> mr len %lld; ndesc %d; nmdesc = %d\n",
1605 		       scsi_bufflen(req->scmnd), desc_len, mr_len,
1606 		       state->ndesc, state->nmdesc);
1607 }
1608 
1609 /**
1610  * srp_map_data() - map SCSI data buffer onto an SRP request
1611  * @scmnd: SCSI command to map
1612  * @ch: SRP RDMA channel
1613  * @req: SRP request
1614  *
1615  * Returns the length in bytes of the SRP_CMD IU or a negative value if
1616  * mapping failed. The size of any immediate data is not included in the
1617  * return value.
1618  */
srp_map_data(struct scsi_cmnd * scmnd,struct srp_rdma_ch * ch,struct srp_request * req)1619 static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_rdma_ch *ch,
1620 			struct srp_request *req)
1621 {
1622 	struct srp_target_port *target = ch->target;
1623 	struct scatterlist *scat, *sg;
1624 	struct srp_cmd *cmd = req->cmd->buf;
1625 	int i, len, nents, count, ret;
1626 	struct srp_device *dev;
1627 	struct ib_device *ibdev;
1628 	struct srp_map_state state;
1629 	struct srp_indirect_buf *indirect_hdr;
1630 	u64 data_len;
1631 	u32 idb_len, table_len;
1632 	__be32 idb_rkey;
1633 	u8 fmt;
1634 
1635 	req->cmd->num_sge = 1;
1636 
1637 	if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
1638 		return sizeof(struct srp_cmd) + cmd->add_cdb_len;
1639 
1640 	if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
1641 	    scmnd->sc_data_direction != DMA_TO_DEVICE) {
1642 		shost_printk(KERN_WARNING, target->scsi_host,
1643 			     PFX "Unhandled data direction %d\n",
1644 			     scmnd->sc_data_direction);
1645 		return -EINVAL;
1646 	}
1647 
1648 	nents = scsi_sg_count(scmnd);
1649 	scat  = scsi_sglist(scmnd);
1650 	data_len = scsi_bufflen(scmnd);
1651 
1652 	dev = target->srp_host->srp_dev;
1653 	ibdev = dev->dev;
1654 
1655 	count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
1656 	if (unlikely(count == 0))
1657 		return -EIO;
1658 
1659 	if (ch->use_imm_data &&
1660 	    count <= ch->max_imm_sge &&
1661 	    SRP_IMM_DATA_OFFSET + data_len <= ch->max_it_iu_len &&
1662 	    scmnd->sc_data_direction == DMA_TO_DEVICE) {
1663 		struct srp_imm_buf *buf;
1664 		struct ib_sge *sge = &req->cmd->sge[1];
1665 
1666 		fmt = SRP_DATA_DESC_IMM;
1667 		len = SRP_IMM_DATA_OFFSET;
1668 		req->nmdesc = 0;
1669 		buf = (void *)cmd->add_data + cmd->add_cdb_len;
1670 		buf->len = cpu_to_be32(data_len);
1671 		WARN_ON_ONCE((void *)(buf + 1) > (void *)cmd + len);
1672 		for_each_sg(scat, sg, count, i) {
1673 			sge[i].addr   = sg_dma_address(sg);
1674 			sge[i].length = sg_dma_len(sg);
1675 			sge[i].lkey   = target->lkey;
1676 		}
1677 		req->cmd->num_sge += count;
1678 		goto map_complete;
1679 	}
1680 
1681 	fmt = SRP_DATA_DESC_DIRECT;
1682 	len = sizeof(struct srp_cmd) + cmd->add_cdb_len +
1683 		sizeof(struct srp_direct_buf);
1684 
1685 	if (count == 1 && target->global_rkey) {
1686 		/*
1687 		 * The midlayer only generated a single gather/scatter
1688 		 * entry, or DMA mapping coalesced everything to a
1689 		 * single entry.  So a direct descriptor along with
1690 		 * the DMA MR suffices.
1691 		 */
1692 		struct srp_direct_buf *buf;
1693 
1694 		buf = (void *)cmd->add_data + cmd->add_cdb_len;
1695 		buf->va  = cpu_to_be64(sg_dma_address(scat));
1696 		buf->key = cpu_to_be32(target->global_rkey);
1697 		buf->len = cpu_to_be32(sg_dma_len(scat));
1698 
1699 		req->nmdesc = 0;
1700 		goto map_complete;
1701 	}
1702 
1703 	/*
1704 	 * We have more than one scatter/gather entry, so build our indirect
1705 	 * descriptor table, trying to merge as many entries as we can.
1706 	 */
1707 	indirect_hdr = (void *)cmd->add_data + cmd->add_cdb_len;
1708 
1709 	ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr,
1710 				   target->indirect_size, DMA_TO_DEVICE);
1711 
1712 	memset(&state, 0, sizeof(state));
1713 	state.desc = req->indirect_desc;
1714 	if (dev->use_fast_reg)
1715 		ret = srp_map_sg_fr(&state, ch, req, scat, count);
1716 	else
1717 		ret = srp_map_sg_dma(&state, ch, req, scat, count);
1718 	req->nmdesc = state.nmdesc;
1719 	if (ret < 0)
1720 		goto unmap;
1721 
1722 	{
1723 		DEFINE_DYNAMIC_DEBUG_METADATA(ddm,
1724 			"Memory mapping consistency check");
1725 		if (DYNAMIC_DEBUG_BRANCH(ddm))
1726 			srp_check_mapping(&state, ch, req, scat, count);
1727 	}
1728 
1729 	/* We've mapped the request, now pull as much of the indirect
1730 	 * descriptor table as we can into the command buffer. If this
1731 	 * target is not using an external indirect table, we are
1732 	 * guaranteed to fit into the command, as the SCSI layer won't
1733 	 * give us more S/G entries than we allow.
1734 	 */
1735 	if (state.ndesc == 1) {
1736 		/*
1737 		 * Memory registration collapsed the sg-list into one entry,
1738 		 * so use a direct descriptor.
1739 		 */
1740 		struct srp_direct_buf *buf;
1741 
1742 		buf = (void *)cmd->add_data + cmd->add_cdb_len;
1743 		*buf = req->indirect_desc[0];
1744 		goto map_complete;
1745 	}
1746 
1747 	if (unlikely(target->cmd_sg_cnt < state.ndesc &&
1748 						!target->allow_ext_sg)) {
1749 		shost_printk(KERN_ERR, target->scsi_host,
1750 			     "Could not fit S/G list into SRP_CMD\n");
1751 		ret = -EIO;
1752 		goto unmap;
1753 	}
1754 
1755 	count = min(state.ndesc, target->cmd_sg_cnt);
1756 	table_len = state.ndesc * sizeof (struct srp_direct_buf);
1757 	idb_len = sizeof(struct srp_indirect_buf) + table_len;
1758 
1759 	fmt = SRP_DATA_DESC_INDIRECT;
1760 	len = sizeof(struct srp_cmd) + cmd->add_cdb_len +
1761 		sizeof(struct srp_indirect_buf);
1762 	len += count * sizeof (struct srp_direct_buf);
1763 
1764 	memcpy(indirect_hdr->desc_list, req->indirect_desc,
1765 	       count * sizeof (struct srp_direct_buf));
1766 
1767 	if (!target->global_rkey) {
1768 		ret = srp_map_idb(ch, req, state.gen.next, state.gen.end,
1769 				  idb_len, &idb_rkey);
1770 		if (ret < 0)
1771 			goto unmap;
1772 		req->nmdesc++;
1773 	} else {
1774 		idb_rkey = cpu_to_be32(target->global_rkey);
1775 	}
1776 
1777 	indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr);
1778 	indirect_hdr->table_desc.key = idb_rkey;
1779 	indirect_hdr->table_desc.len = cpu_to_be32(table_len);
1780 	indirect_hdr->len = cpu_to_be32(state.total_len);
1781 
1782 	if (scmnd->sc_data_direction == DMA_TO_DEVICE)
1783 		cmd->data_out_desc_cnt = count;
1784 	else
1785 		cmd->data_in_desc_cnt = count;
1786 
1787 	ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len,
1788 				      DMA_TO_DEVICE);
1789 
1790 map_complete:
1791 	if (scmnd->sc_data_direction == DMA_TO_DEVICE)
1792 		cmd->buf_fmt = fmt << 4;
1793 	else
1794 		cmd->buf_fmt = fmt;
1795 
1796 	return len;
1797 
1798 unmap:
1799 	srp_unmap_data(scmnd, ch, req);
1800 	if (ret == -ENOMEM && req->nmdesc >= target->mr_pool_size)
1801 		ret = -E2BIG;
1802 	return ret;
1803 }
1804 
1805 /*
1806  * Return an IU and possible credit to the free pool
1807  */
srp_put_tx_iu(struct srp_rdma_ch * ch,struct srp_iu * iu,enum srp_iu_type iu_type)1808 static void srp_put_tx_iu(struct srp_rdma_ch *ch, struct srp_iu *iu,
1809 			  enum srp_iu_type iu_type)
1810 {
1811 	unsigned long flags;
1812 
1813 	spin_lock_irqsave(&ch->lock, flags);
1814 	list_add(&iu->list, &ch->free_tx);
1815 	if (iu_type != SRP_IU_RSP)
1816 		++ch->req_lim;
1817 	spin_unlock_irqrestore(&ch->lock, flags);
1818 }
1819 
1820 /*
1821  * Must be called with ch->lock held to protect req_lim and free_tx.
1822  * If IU is not sent, it must be returned using srp_put_tx_iu().
1823  *
1824  * Note:
1825  * An upper limit for the number of allocated information units for each
1826  * request type is:
1827  * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues
1828  *   more than Scsi_Host.can_queue requests.
1829  * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE.
1830  * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than
1831  *   one unanswered SRP request to an initiator.
1832  */
__srp_get_tx_iu(struct srp_rdma_ch * ch,enum srp_iu_type iu_type)1833 static struct srp_iu *__srp_get_tx_iu(struct srp_rdma_ch *ch,
1834 				      enum srp_iu_type iu_type)
1835 {
1836 	struct srp_target_port *target = ch->target;
1837 	s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE;
1838 	struct srp_iu *iu;
1839 
1840 	lockdep_assert_held(&ch->lock);
1841 
1842 	ib_process_cq_direct(ch->send_cq, -1);
1843 
1844 	if (list_empty(&ch->free_tx))
1845 		return NULL;
1846 
1847 	/* Initiator responses to target requests do not consume credits */
1848 	if (iu_type != SRP_IU_RSP) {
1849 		if (ch->req_lim <= rsv) {
1850 			++target->zero_req_lim;
1851 			return NULL;
1852 		}
1853 
1854 		--ch->req_lim;
1855 	}
1856 
1857 	iu = list_first_entry(&ch->free_tx, struct srp_iu, list);
1858 	list_del(&iu->list);
1859 	return iu;
1860 }
1861 
1862 /*
1863  * Note: if this function is called from inside ib_drain_sq() then it will
1864  * be called without ch->lock being held. If ib_drain_sq() dequeues a WQE
1865  * with status IB_WC_SUCCESS then that's a bug.
1866  */
srp_send_done(struct ib_cq * cq,struct ib_wc * wc)1867 static void srp_send_done(struct ib_cq *cq, struct ib_wc *wc)
1868 {
1869 	struct srp_iu *iu = container_of(wc->wr_cqe, struct srp_iu, cqe);
1870 	struct srp_rdma_ch *ch = cq->cq_context;
1871 
1872 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
1873 		srp_handle_qp_err(cq, wc, "SEND");
1874 		return;
1875 	}
1876 
1877 	lockdep_assert_held(&ch->lock);
1878 
1879 	list_add(&iu->list, &ch->free_tx);
1880 }
1881 
1882 /**
1883  * srp_post_send() - send an SRP information unit
1884  * @ch: RDMA channel over which to send the information unit.
1885  * @iu: Information unit to send.
1886  * @len: Length of the information unit excluding immediate data.
1887  */
srp_post_send(struct srp_rdma_ch * ch,struct srp_iu * iu,int len)1888 static int srp_post_send(struct srp_rdma_ch *ch, struct srp_iu *iu, int len)
1889 {
1890 	struct srp_target_port *target = ch->target;
1891 	struct ib_send_wr wr;
1892 
1893 	if (WARN_ON_ONCE(iu->num_sge > SRP_MAX_SGE))
1894 		return -EINVAL;
1895 
1896 	iu->sge[0].addr   = iu->dma;
1897 	iu->sge[0].length = len;
1898 	iu->sge[0].lkey   = target->lkey;
1899 
1900 	iu->cqe.done = srp_send_done;
1901 
1902 	wr.next       = NULL;
1903 	wr.wr_cqe     = &iu->cqe;
1904 	wr.sg_list    = &iu->sge[0];
1905 	wr.num_sge    = iu->num_sge;
1906 	wr.opcode     = IB_WR_SEND;
1907 	wr.send_flags = IB_SEND_SIGNALED;
1908 
1909 	return ib_post_send(ch->qp, &wr, NULL);
1910 }
1911 
srp_post_recv(struct srp_rdma_ch * ch,struct srp_iu * iu)1912 static int srp_post_recv(struct srp_rdma_ch *ch, struct srp_iu *iu)
1913 {
1914 	struct srp_target_port *target = ch->target;
1915 	struct ib_recv_wr wr;
1916 	struct ib_sge list;
1917 
1918 	list.addr   = iu->dma;
1919 	list.length = iu->size;
1920 	list.lkey   = target->lkey;
1921 
1922 	iu->cqe.done = srp_recv_done;
1923 
1924 	wr.next     = NULL;
1925 	wr.wr_cqe   = &iu->cqe;
1926 	wr.sg_list  = &list;
1927 	wr.num_sge  = 1;
1928 
1929 	return ib_post_recv(ch->qp, &wr, NULL);
1930 }
1931 
srp_process_rsp(struct srp_rdma_ch * ch,struct srp_rsp * rsp)1932 static void srp_process_rsp(struct srp_rdma_ch *ch, struct srp_rsp *rsp)
1933 {
1934 	struct srp_target_port *target = ch->target;
1935 	struct srp_request *req;
1936 	struct scsi_cmnd *scmnd;
1937 	unsigned long flags;
1938 
1939 	if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
1940 		spin_lock_irqsave(&ch->lock, flags);
1941 		ch->req_lim += be32_to_cpu(rsp->req_lim_delta);
1942 		if (rsp->tag == ch->tsk_mgmt_tag) {
1943 			ch->tsk_mgmt_status = -1;
1944 			if (be32_to_cpu(rsp->resp_data_len) >= 4)
1945 				ch->tsk_mgmt_status = rsp->data[3];
1946 			complete(&ch->tsk_mgmt_done);
1947 		} else {
1948 			shost_printk(KERN_ERR, target->scsi_host,
1949 				     "Received tsk mgmt response too late for tag %#llx\n",
1950 				     rsp->tag);
1951 		}
1952 		spin_unlock_irqrestore(&ch->lock, flags);
1953 	} else {
1954 		scmnd = scsi_host_find_tag(target->scsi_host, rsp->tag);
1955 		if (scmnd) {
1956 			req = scsi_cmd_priv(scmnd);
1957 			scmnd = srp_claim_req(ch, req, NULL, scmnd);
1958 		}
1959 		if (!scmnd) {
1960 			shost_printk(KERN_ERR, target->scsi_host,
1961 				     "Null scmnd for RSP w/tag %#016llx received on ch %td / QP %#x\n",
1962 				     rsp->tag, ch - target->ch, ch->qp->qp_num);
1963 
1964 			spin_lock_irqsave(&ch->lock, flags);
1965 			ch->req_lim += be32_to_cpu(rsp->req_lim_delta);
1966 			spin_unlock_irqrestore(&ch->lock, flags);
1967 
1968 			return;
1969 		}
1970 		scmnd->result = rsp->status;
1971 
1972 		if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
1973 			memcpy(scmnd->sense_buffer, rsp->data +
1974 			       be32_to_cpu(rsp->resp_data_len),
1975 			       min_t(int, be32_to_cpu(rsp->sense_data_len),
1976 				     SCSI_SENSE_BUFFERSIZE));
1977 		}
1978 
1979 		if (unlikely(rsp->flags & SRP_RSP_FLAG_DIUNDER))
1980 			scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
1981 		else if (unlikely(rsp->flags & SRP_RSP_FLAG_DOUNDER))
1982 			scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
1983 
1984 		srp_free_req(ch, req, scmnd,
1985 			     be32_to_cpu(rsp->req_lim_delta));
1986 
1987 		scmnd->scsi_done(scmnd);
1988 	}
1989 }
1990 
srp_response_common(struct srp_rdma_ch * ch,s32 req_delta,void * rsp,int len)1991 static int srp_response_common(struct srp_rdma_ch *ch, s32 req_delta,
1992 			       void *rsp, int len)
1993 {
1994 	struct srp_target_port *target = ch->target;
1995 	struct ib_device *dev = target->srp_host->srp_dev->dev;
1996 	unsigned long flags;
1997 	struct srp_iu *iu;
1998 	int err;
1999 
2000 	spin_lock_irqsave(&ch->lock, flags);
2001 	ch->req_lim += req_delta;
2002 	iu = __srp_get_tx_iu(ch, SRP_IU_RSP);
2003 	spin_unlock_irqrestore(&ch->lock, flags);
2004 
2005 	if (!iu) {
2006 		shost_printk(KERN_ERR, target->scsi_host, PFX
2007 			     "no IU available to send response\n");
2008 		return 1;
2009 	}
2010 
2011 	iu->num_sge = 1;
2012 	ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE);
2013 	memcpy(iu->buf, rsp, len);
2014 	ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE);
2015 
2016 	err = srp_post_send(ch, iu, len);
2017 	if (err) {
2018 		shost_printk(KERN_ERR, target->scsi_host, PFX
2019 			     "unable to post response: %d\n", err);
2020 		srp_put_tx_iu(ch, iu, SRP_IU_RSP);
2021 	}
2022 
2023 	return err;
2024 }
2025 
srp_process_cred_req(struct srp_rdma_ch * ch,struct srp_cred_req * req)2026 static void srp_process_cred_req(struct srp_rdma_ch *ch,
2027 				 struct srp_cred_req *req)
2028 {
2029 	struct srp_cred_rsp rsp = {
2030 		.opcode = SRP_CRED_RSP,
2031 		.tag = req->tag,
2032 	};
2033 	s32 delta = be32_to_cpu(req->req_lim_delta);
2034 
2035 	if (srp_response_common(ch, delta, &rsp, sizeof(rsp)))
2036 		shost_printk(KERN_ERR, ch->target->scsi_host, PFX
2037 			     "problems processing SRP_CRED_REQ\n");
2038 }
2039 
srp_process_aer_req(struct srp_rdma_ch * ch,struct srp_aer_req * req)2040 static void srp_process_aer_req(struct srp_rdma_ch *ch,
2041 				struct srp_aer_req *req)
2042 {
2043 	struct srp_target_port *target = ch->target;
2044 	struct srp_aer_rsp rsp = {
2045 		.opcode = SRP_AER_RSP,
2046 		.tag = req->tag,
2047 	};
2048 	s32 delta = be32_to_cpu(req->req_lim_delta);
2049 
2050 	shost_printk(KERN_ERR, target->scsi_host, PFX
2051 		     "ignoring AER for LUN %llu\n", scsilun_to_int(&req->lun));
2052 
2053 	if (srp_response_common(ch, delta, &rsp, sizeof(rsp)))
2054 		shost_printk(KERN_ERR, target->scsi_host, PFX
2055 			     "problems processing SRP_AER_REQ\n");
2056 }
2057 
srp_recv_done(struct ib_cq * cq,struct ib_wc * wc)2058 static void srp_recv_done(struct ib_cq *cq, struct ib_wc *wc)
2059 {
2060 	struct srp_iu *iu = container_of(wc->wr_cqe, struct srp_iu, cqe);
2061 	struct srp_rdma_ch *ch = cq->cq_context;
2062 	struct srp_target_port *target = ch->target;
2063 	struct ib_device *dev = target->srp_host->srp_dev->dev;
2064 	int res;
2065 	u8 opcode;
2066 
2067 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
2068 		srp_handle_qp_err(cq, wc, "RECV");
2069 		return;
2070 	}
2071 
2072 	ib_dma_sync_single_for_cpu(dev, iu->dma, ch->max_ti_iu_len,
2073 				   DMA_FROM_DEVICE);
2074 
2075 	opcode = *(u8 *) iu->buf;
2076 
2077 	if (0) {
2078 		shost_printk(KERN_ERR, target->scsi_host,
2079 			     PFX "recv completion, opcode 0x%02x\n", opcode);
2080 		print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1,
2081 			       iu->buf, wc->byte_len, true);
2082 	}
2083 
2084 	switch (opcode) {
2085 	case SRP_RSP:
2086 		srp_process_rsp(ch, iu->buf);
2087 		break;
2088 
2089 	case SRP_CRED_REQ:
2090 		srp_process_cred_req(ch, iu->buf);
2091 		break;
2092 
2093 	case SRP_AER_REQ:
2094 		srp_process_aer_req(ch, iu->buf);
2095 		break;
2096 
2097 	case SRP_T_LOGOUT:
2098 		/* XXX Handle target logout */
2099 		shost_printk(KERN_WARNING, target->scsi_host,
2100 			     PFX "Got target logout request\n");
2101 		break;
2102 
2103 	default:
2104 		shost_printk(KERN_WARNING, target->scsi_host,
2105 			     PFX "Unhandled SRP opcode 0x%02x\n", opcode);
2106 		break;
2107 	}
2108 
2109 	ib_dma_sync_single_for_device(dev, iu->dma, ch->max_ti_iu_len,
2110 				      DMA_FROM_DEVICE);
2111 
2112 	res = srp_post_recv(ch, iu);
2113 	if (res != 0)
2114 		shost_printk(KERN_ERR, target->scsi_host,
2115 			     PFX "Recv failed with error code %d\n", res);
2116 }
2117 
2118 /**
2119  * srp_tl_err_work() - handle a transport layer error
2120  * @work: Work structure embedded in an SRP target port.
2121  *
2122  * Note: This function may get invoked before the rport has been created,
2123  * hence the target->rport test.
2124  */
srp_tl_err_work(struct work_struct * work)2125 static void srp_tl_err_work(struct work_struct *work)
2126 {
2127 	struct srp_target_port *target;
2128 
2129 	target = container_of(work, struct srp_target_port, tl_err_work);
2130 	if (target->rport)
2131 		srp_start_tl_fail_timers(target->rport);
2132 }
2133 
srp_handle_qp_err(struct ib_cq * cq,struct ib_wc * wc,const char * opname)2134 static void srp_handle_qp_err(struct ib_cq *cq, struct ib_wc *wc,
2135 		const char *opname)
2136 {
2137 	struct srp_rdma_ch *ch = cq->cq_context;
2138 	struct srp_target_port *target = ch->target;
2139 
2140 	if (ch->connected && !target->qp_in_error) {
2141 		shost_printk(KERN_ERR, target->scsi_host,
2142 			     PFX "failed %s status %s (%d) for CQE %p\n",
2143 			     opname, ib_wc_status_msg(wc->status), wc->status,
2144 			     wc->wr_cqe);
2145 		queue_work(system_long_wq, &target->tl_err_work);
2146 	}
2147 	target->qp_in_error = true;
2148 }
2149 
srp_queuecommand(struct Scsi_Host * shost,struct scsi_cmnd * scmnd)2150 static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd)
2151 {
2152 	struct request *rq = scsi_cmd_to_rq(scmnd);
2153 	struct srp_target_port *target = host_to_target(shost);
2154 	struct srp_rdma_ch *ch;
2155 	struct srp_request *req = scsi_cmd_priv(scmnd);
2156 	struct srp_iu *iu;
2157 	struct srp_cmd *cmd;
2158 	struct ib_device *dev;
2159 	unsigned long flags;
2160 	u32 tag;
2161 	int len, ret;
2162 
2163 	scmnd->result = srp_chkready(target->rport);
2164 	if (unlikely(scmnd->result))
2165 		goto err;
2166 
2167 	WARN_ON_ONCE(rq->tag < 0);
2168 	tag = blk_mq_unique_tag(rq);
2169 	ch = &target->ch[blk_mq_unique_tag_to_hwq(tag)];
2170 
2171 	spin_lock_irqsave(&ch->lock, flags);
2172 	iu = __srp_get_tx_iu(ch, SRP_IU_CMD);
2173 	spin_unlock_irqrestore(&ch->lock, flags);
2174 
2175 	if (!iu)
2176 		goto err;
2177 
2178 	dev = target->srp_host->srp_dev->dev;
2179 	ib_dma_sync_single_for_cpu(dev, iu->dma, ch->max_it_iu_len,
2180 				   DMA_TO_DEVICE);
2181 
2182 	cmd = iu->buf;
2183 	memset(cmd, 0, sizeof *cmd);
2184 
2185 	cmd->opcode = SRP_CMD;
2186 	int_to_scsilun(scmnd->device->lun, &cmd->lun);
2187 	cmd->tag    = tag;
2188 	memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
2189 	if (unlikely(scmnd->cmd_len > sizeof(cmd->cdb))) {
2190 		cmd->add_cdb_len = round_up(scmnd->cmd_len - sizeof(cmd->cdb),
2191 					    4);
2192 		if (WARN_ON_ONCE(cmd->add_cdb_len > SRP_MAX_ADD_CDB_LEN))
2193 			goto err_iu;
2194 	}
2195 
2196 	req->scmnd    = scmnd;
2197 	req->cmd      = iu;
2198 
2199 	len = srp_map_data(scmnd, ch, req);
2200 	if (len < 0) {
2201 		shost_printk(KERN_ERR, target->scsi_host,
2202 			     PFX "Failed to map data (%d)\n", len);
2203 		/*
2204 		 * If we ran out of memory descriptors (-ENOMEM) because an
2205 		 * application is queuing many requests with more than
2206 		 * max_pages_per_mr sg-list elements, tell the SCSI mid-layer
2207 		 * to reduce queue depth temporarily.
2208 		 */
2209 		scmnd->result = len == -ENOMEM ?
2210 			DID_OK << 16 | SAM_STAT_TASK_SET_FULL : DID_ERROR << 16;
2211 		goto err_iu;
2212 	}
2213 
2214 	ib_dma_sync_single_for_device(dev, iu->dma, ch->max_it_iu_len,
2215 				      DMA_TO_DEVICE);
2216 
2217 	if (srp_post_send(ch, iu, len)) {
2218 		shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
2219 		scmnd->result = DID_ERROR << 16;
2220 		goto err_unmap;
2221 	}
2222 
2223 	return 0;
2224 
2225 err_unmap:
2226 	srp_unmap_data(scmnd, ch, req);
2227 
2228 err_iu:
2229 	srp_put_tx_iu(ch, iu, SRP_IU_CMD);
2230 
2231 	/*
2232 	 * Avoid that the loops that iterate over the request ring can
2233 	 * encounter a dangling SCSI command pointer.
2234 	 */
2235 	req->scmnd = NULL;
2236 
2237 err:
2238 	if (scmnd->result) {
2239 		scmnd->scsi_done(scmnd);
2240 		ret = 0;
2241 	} else {
2242 		ret = SCSI_MLQUEUE_HOST_BUSY;
2243 	}
2244 
2245 	return ret;
2246 }
2247 
2248 /*
2249  * Note: the resources allocated in this function are freed in
2250  * srp_free_ch_ib().
2251  */
srp_alloc_iu_bufs(struct srp_rdma_ch * ch)2252 static int srp_alloc_iu_bufs(struct srp_rdma_ch *ch)
2253 {
2254 	struct srp_target_port *target = ch->target;
2255 	int i;
2256 
2257 	ch->rx_ring = kcalloc(target->queue_size, sizeof(*ch->rx_ring),
2258 			      GFP_KERNEL);
2259 	if (!ch->rx_ring)
2260 		goto err_no_ring;
2261 	ch->tx_ring = kcalloc(target->queue_size, sizeof(*ch->tx_ring),
2262 			      GFP_KERNEL);
2263 	if (!ch->tx_ring)
2264 		goto err_no_ring;
2265 
2266 	for (i = 0; i < target->queue_size; ++i) {
2267 		ch->rx_ring[i] = srp_alloc_iu(target->srp_host,
2268 					      ch->max_ti_iu_len,
2269 					      GFP_KERNEL, DMA_FROM_DEVICE);
2270 		if (!ch->rx_ring[i])
2271 			goto err;
2272 	}
2273 
2274 	for (i = 0; i < target->queue_size; ++i) {
2275 		ch->tx_ring[i] = srp_alloc_iu(target->srp_host,
2276 					      ch->max_it_iu_len,
2277 					      GFP_KERNEL, DMA_TO_DEVICE);
2278 		if (!ch->tx_ring[i])
2279 			goto err;
2280 
2281 		list_add(&ch->tx_ring[i]->list, &ch->free_tx);
2282 	}
2283 
2284 	return 0;
2285 
2286 err:
2287 	for (i = 0; i < target->queue_size; ++i) {
2288 		srp_free_iu(target->srp_host, ch->rx_ring[i]);
2289 		srp_free_iu(target->srp_host, ch->tx_ring[i]);
2290 	}
2291 
2292 
2293 err_no_ring:
2294 	kfree(ch->tx_ring);
2295 	ch->tx_ring = NULL;
2296 	kfree(ch->rx_ring);
2297 	ch->rx_ring = NULL;
2298 
2299 	return -ENOMEM;
2300 }
2301 
srp_compute_rq_tmo(struct ib_qp_attr * qp_attr,int attr_mask)2302 static uint32_t srp_compute_rq_tmo(struct ib_qp_attr *qp_attr, int attr_mask)
2303 {
2304 	uint64_t T_tr_ns, max_compl_time_ms;
2305 	uint32_t rq_tmo_jiffies;
2306 
2307 	/*
2308 	 * According to section 11.2.4.2 in the IBTA spec (Modify Queue Pair,
2309 	 * table 91), both the QP timeout and the retry count have to be set
2310 	 * for RC QP's during the RTR to RTS transition.
2311 	 */
2312 	WARN_ON_ONCE((attr_mask & (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)) !=
2313 		     (IB_QP_TIMEOUT | IB_QP_RETRY_CNT));
2314 
2315 	/*
2316 	 * Set target->rq_tmo_jiffies to one second more than the largest time
2317 	 * it can take before an error completion is generated. See also
2318 	 * C9-140..142 in the IBTA spec for more information about how to
2319 	 * convert the QP Local ACK Timeout value to nanoseconds.
2320 	 */
2321 	T_tr_ns = 4096 * (1ULL << qp_attr->timeout);
2322 	max_compl_time_ms = qp_attr->retry_cnt * 4 * T_tr_ns;
2323 	do_div(max_compl_time_ms, NSEC_PER_MSEC);
2324 	rq_tmo_jiffies = msecs_to_jiffies(max_compl_time_ms + 1000);
2325 
2326 	return rq_tmo_jiffies;
2327 }
2328 
srp_cm_rep_handler(struct ib_cm_id * cm_id,const struct srp_login_rsp * lrsp,struct srp_rdma_ch * ch)2329 static void srp_cm_rep_handler(struct ib_cm_id *cm_id,
2330 			       const struct srp_login_rsp *lrsp,
2331 			       struct srp_rdma_ch *ch)
2332 {
2333 	struct srp_target_port *target = ch->target;
2334 	struct ib_qp_attr *qp_attr = NULL;
2335 	int attr_mask = 0;
2336 	int ret = 0;
2337 	int i;
2338 
2339 	if (lrsp->opcode == SRP_LOGIN_RSP) {
2340 		ch->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len);
2341 		ch->req_lim       = be32_to_cpu(lrsp->req_lim_delta);
2342 		ch->use_imm_data  = srp_use_imm_data &&
2343 			(lrsp->rsp_flags & SRP_LOGIN_RSP_IMMED_SUPP);
2344 		ch->max_it_iu_len = srp_max_it_iu_len(target->cmd_sg_cnt,
2345 						      ch->use_imm_data,
2346 						      target->max_it_iu_size);
2347 		WARN_ON_ONCE(ch->max_it_iu_len >
2348 			     be32_to_cpu(lrsp->max_it_iu_len));
2349 
2350 		if (ch->use_imm_data)
2351 			shost_printk(KERN_DEBUG, target->scsi_host,
2352 				     PFX "using immediate data\n");
2353 
2354 		/*
2355 		 * Reserve credits for task management so we don't
2356 		 * bounce requests back to the SCSI mid-layer.
2357 		 */
2358 		target->scsi_host->can_queue
2359 			= min(ch->req_lim - SRP_TSK_MGMT_SQ_SIZE,
2360 			      target->scsi_host->can_queue);
2361 		target->scsi_host->cmd_per_lun
2362 			= min_t(int, target->scsi_host->can_queue,
2363 				target->scsi_host->cmd_per_lun);
2364 	} else {
2365 		shost_printk(KERN_WARNING, target->scsi_host,
2366 			     PFX "Unhandled RSP opcode %#x\n", lrsp->opcode);
2367 		ret = -ECONNRESET;
2368 		goto error;
2369 	}
2370 
2371 	if (!ch->rx_ring) {
2372 		ret = srp_alloc_iu_bufs(ch);
2373 		if (ret)
2374 			goto error;
2375 	}
2376 
2377 	for (i = 0; i < target->queue_size; i++) {
2378 		struct srp_iu *iu = ch->rx_ring[i];
2379 
2380 		ret = srp_post_recv(ch, iu);
2381 		if (ret)
2382 			goto error;
2383 	}
2384 
2385 	if (!target->using_rdma_cm) {
2386 		ret = -ENOMEM;
2387 		qp_attr = kmalloc(sizeof(*qp_attr), GFP_KERNEL);
2388 		if (!qp_attr)
2389 			goto error;
2390 
2391 		qp_attr->qp_state = IB_QPS_RTR;
2392 		ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
2393 		if (ret)
2394 			goto error_free;
2395 
2396 		ret = ib_modify_qp(ch->qp, qp_attr, attr_mask);
2397 		if (ret)
2398 			goto error_free;
2399 
2400 		qp_attr->qp_state = IB_QPS_RTS;
2401 		ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
2402 		if (ret)
2403 			goto error_free;
2404 
2405 		target->rq_tmo_jiffies = srp_compute_rq_tmo(qp_attr, attr_mask);
2406 
2407 		ret = ib_modify_qp(ch->qp, qp_attr, attr_mask);
2408 		if (ret)
2409 			goto error_free;
2410 
2411 		ret = ib_send_cm_rtu(cm_id, NULL, 0);
2412 	}
2413 
2414 error_free:
2415 	kfree(qp_attr);
2416 
2417 error:
2418 	ch->status = ret;
2419 }
2420 
srp_ib_cm_rej_handler(struct ib_cm_id * cm_id,const struct ib_cm_event * event,struct srp_rdma_ch * ch)2421 static void srp_ib_cm_rej_handler(struct ib_cm_id *cm_id,
2422 				  const struct ib_cm_event *event,
2423 				  struct srp_rdma_ch *ch)
2424 {
2425 	struct srp_target_port *target = ch->target;
2426 	struct Scsi_Host *shost = target->scsi_host;
2427 	struct ib_class_port_info *cpi;
2428 	int opcode;
2429 	u16 dlid;
2430 
2431 	switch (event->param.rej_rcvd.reason) {
2432 	case IB_CM_REJ_PORT_CM_REDIRECT:
2433 		cpi = event->param.rej_rcvd.ari;
2434 		dlid = be16_to_cpu(cpi->redirect_lid);
2435 		sa_path_set_dlid(&ch->ib_cm.path, dlid);
2436 		ch->ib_cm.path.pkey = cpi->redirect_pkey;
2437 		cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
2438 		memcpy(ch->ib_cm.path.dgid.raw, cpi->redirect_gid, 16);
2439 
2440 		ch->status = dlid ? SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
2441 		break;
2442 
2443 	case IB_CM_REJ_PORT_REDIRECT:
2444 		if (srp_target_is_topspin(target)) {
2445 			union ib_gid *dgid = &ch->ib_cm.path.dgid;
2446 
2447 			/*
2448 			 * Topspin/Cisco SRP gateways incorrectly send
2449 			 * reject reason code 25 when they mean 24
2450 			 * (port redirect).
2451 			 */
2452 			memcpy(dgid->raw, event->param.rej_rcvd.ari, 16);
2453 
2454 			shost_printk(KERN_DEBUG, shost,
2455 				     PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
2456 				     be64_to_cpu(dgid->global.subnet_prefix),
2457 				     be64_to_cpu(dgid->global.interface_id));
2458 
2459 			ch->status = SRP_PORT_REDIRECT;
2460 		} else {
2461 			shost_printk(KERN_WARNING, shost,
2462 				     "  REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
2463 			ch->status = -ECONNRESET;
2464 		}
2465 		break;
2466 
2467 	case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
2468 		shost_printk(KERN_WARNING, shost,
2469 			    "  REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
2470 		ch->status = -ECONNRESET;
2471 		break;
2472 
2473 	case IB_CM_REJ_CONSUMER_DEFINED:
2474 		opcode = *(u8 *) event->private_data;
2475 		if (opcode == SRP_LOGIN_REJ) {
2476 			struct srp_login_rej *rej = event->private_data;
2477 			u32 reason = be32_to_cpu(rej->reason);
2478 
2479 			if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
2480 				shost_printk(KERN_WARNING, shost,
2481 					     PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
2482 			else
2483 				shost_printk(KERN_WARNING, shost, PFX
2484 					     "SRP LOGIN from %pI6 to %pI6 REJECTED, reason 0x%08x\n",
2485 					     target->sgid.raw,
2486 					     target->ib_cm.orig_dgid.raw,
2487 					     reason);
2488 		} else
2489 			shost_printk(KERN_WARNING, shost,
2490 				     "  REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
2491 				     " opcode 0x%02x\n", opcode);
2492 		ch->status = -ECONNRESET;
2493 		break;
2494 
2495 	case IB_CM_REJ_STALE_CONN:
2496 		shost_printk(KERN_WARNING, shost, "  REJ reason: stale connection\n");
2497 		ch->status = SRP_STALE_CONN;
2498 		break;
2499 
2500 	default:
2501 		shost_printk(KERN_WARNING, shost, "  REJ reason 0x%x\n",
2502 			     event->param.rej_rcvd.reason);
2503 		ch->status = -ECONNRESET;
2504 	}
2505 }
2506 
srp_ib_cm_handler(struct ib_cm_id * cm_id,const struct ib_cm_event * event)2507 static int srp_ib_cm_handler(struct ib_cm_id *cm_id,
2508 			     const struct ib_cm_event *event)
2509 {
2510 	struct srp_rdma_ch *ch = cm_id->context;
2511 	struct srp_target_port *target = ch->target;
2512 	int comp = 0;
2513 
2514 	switch (event->event) {
2515 	case IB_CM_REQ_ERROR:
2516 		shost_printk(KERN_DEBUG, target->scsi_host,
2517 			     PFX "Sending CM REQ failed\n");
2518 		comp = 1;
2519 		ch->status = -ECONNRESET;
2520 		break;
2521 
2522 	case IB_CM_REP_RECEIVED:
2523 		comp = 1;
2524 		srp_cm_rep_handler(cm_id, event->private_data, ch);
2525 		break;
2526 
2527 	case IB_CM_REJ_RECEIVED:
2528 		shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
2529 		comp = 1;
2530 
2531 		srp_ib_cm_rej_handler(cm_id, event, ch);
2532 		break;
2533 
2534 	case IB_CM_DREQ_RECEIVED:
2535 		shost_printk(KERN_WARNING, target->scsi_host,
2536 			     PFX "DREQ received - connection closed\n");
2537 		ch->connected = false;
2538 		if (ib_send_cm_drep(cm_id, NULL, 0))
2539 			shost_printk(KERN_ERR, target->scsi_host,
2540 				     PFX "Sending CM DREP failed\n");
2541 		queue_work(system_long_wq, &target->tl_err_work);
2542 		break;
2543 
2544 	case IB_CM_TIMEWAIT_EXIT:
2545 		shost_printk(KERN_ERR, target->scsi_host,
2546 			     PFX "connection closed\n");
2547 		comp = 1;
2548 
2549 		ch->status = 0;
2550 		break;
2551 
2552 	case IB_CM_MRA_RECEIVED:
2553 	case IB_CM_DREQ_ERROR:
2554 	case IB_CM_DREP_RECEIVED:
2555 		break;
2556 
2557 	default:
2558 		shost_printk(KERN_WARNING, target->scsi_host,
2559 			     PFX "Unhandled CM event %d\n", event->event);
2560 		break;
2561 	}
2562 
2563 	if (comp)
2564 		complete(&ch->done);
2565 
2566 	return 0;
2567 }
2568 
srp_rdma_cm_rej_handler(struct srp_rdma_ch * ch,struct rdma_cm_event * event)2569 static void srp_rdma_cm_rej_handler(struct srp_rdma_ch *ch,
2570 				    struct rdma_cm_event *event)
2571 {
2572 	struct srp_target_port *target = ch->target;
2573 	struct Scsi_Host *shost = target->scsi_host;
2574 	int opcode;
2575 
2576 	switch (event->status) {
2577 	case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
2578 		shost_printk(KERN_WARNING, shost,
2579 			    "  REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
2580 		ch->status = -ECONNRESET;
2581 		break;
2582 
2583 	case IB_CM_REJ_CONSUMER_DEFINED:
2584 		opcode = *(u8 *) event->param.conn.private_data;
2585 		if (opcode == SRP_LOGIN_REJ) {
2586 			struct srp_login_rej *rej =
2587 				(struct srp_login_rej *)
2588 				event->param.conn.private_data;
2589 			u32 reason = be32_to_cpu(rej->reason);
2590 
2591 			if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
2592 				shost_printk(KERN_WARNING, shost,
2593 					     PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
2594 			else
2595 				shost_printk(KERN_WARNING, shost,
2596 					    PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
2597 		} else {
2598 			shost_printk(KERN_WARNING, shost,
2599 				     "  REJ reason: IB_CM_REJ_CONSUMER_DEFINED, opcode 0x%02x\n",
2600 				     opcode);
2601 		}
2602 		ch->status = -ECONNRESET;
2603 		break;
2604 
2605 	case IB_CM_REJ_STALE_CONN:
2606 		shost_printk(KERN_WARNING, shost,
2607 			     "  REJ reason: stale connection\n");
2608 		ch->status = SRP_STALE_CONN;
2609 		break;
2610 
2611 	default:
2612 		shost_printk(KERN_WARNING, shost, "  REJ reason 0x%x\n",
2613 			     event->status);
2614 		ch->status = -ECONNRESET;
2615 		break;
2616 	}
2617 }
2618 
srp_rdma_cm_handler(struct rdma_cm_id * cm_id,struct rdma_cm_event * event)2619 static int srp_rdma_cm_handler(struct rdma_cm_id *cm_id,
2620 			       struct rdma_cm_event *event)
2621 {
2622 	struct srp_rdma_ch *ch = cm_id->context;
2623 	struct srp_target_port *target = ch->target;
2624 	int comp = 0;
2625 
2626 	switch (event->event) {
2627 	case RDMA_CM_EVENT_ADDR_RESOLVED:
2628 		ch->status = 0;
2629 		comp = 1;
2630 		break;
2631 
2632 	case RDMA_CM_EVENT_ADDR_ERROR:
2633 		ch->status = -ENXIO;
2634 		comp = 1;
2635 		break;
2636 
2637 	case RDMA_CM_EVENT_ROUTE_RESOLVED:
2638 		ch->status = 0;
2639 		comp = 1;
2640 		break;
2641 
2642 	case RDMA_CM_EVENT_ROUTE_ERROR:
2643 	case RDMA_CM_EVENT_UNREACHABLE:
2644 		ch->status = -EHOSTUNREACH;
2645 		comp = 1;
2646 		break;
2647 
2648 	case RDMA_CM_EVENT_CONNECT_ERROR:
2649 		shost_printk(KERN_DEBUG, target->scsi_host,
2650 			     PFX "Sending CM REQ failed\n");
2651 		comp = 1;
2652 		ch->status = -ECONNRESET;
2653 		break;
2654 
2655 	case RDMA_CM_EVENT_ESTABLISHED:
2656 		comp = 1;
2657 		srp_cm_rep_handler(NULL, event->param.conn.private_data, ch);
2658 		break;
2659 
2660 	case RDMA_CM_EVENT_REJECTED:
2661 		shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
2662 		comp = 1;
2663 
2664 		srp_rdma_cm_rej_handler(ch, event);
2665 		break;
2666 
2667 	case RDMA_CM_EVENT_DISCONNECTED:
2668 		if (ch->connected) {
2669 			shost_printk(KERN_WARNING, target->scsi_host,
2670 				     PFX "received DREQ\n");
2671 			rdma_disconnect(ch->rdma_cm.cm_id);
2672 			comp = 1;
2673 			ch->status = 0;
2674 			queue_work(system_long_wq, &target->tl_err_work);
2675 		}
2676 		break;
2677 
2678 	case RDMA_CM_EVENT_TIMEWAIT_EXIT:
2679 		shost_printk(KERN_ERR, target->scsi_host,
2680 			     PFX "connection closed\n");
2681 
2682 		comp = 1;
2683 		ch->status = 0;
2684 		break;
2685 
2686 	default:
2687 		shost_printk(KERN_WARNING, target->scsi_host,
2688 			     PFX "Unhandled CM event %d\n", event->event);
2689 		break;
2690 	}
2691 
2692 	if (comp)
2693 		complete(&ch->done);
2694 
2695 	return 0;
2696 }
2697 
2698 /**
2699  * srp_change_queue_depth - setting device queue depth
2700  * @sdev: scsi device struct
2701  * @qdepth: requested queue depth
2702  *
2703  * Returns queue depth.
2704  */
2705 static int
srp_change_queue_depth(struct scsi_device * sdev,int qdepth)2706 srp_change_queue_depth(struct scsi_device *sdev, int qdepth)
2707 {
2708 	if (!sdev->tagged_supported)
2709 		qdepth = 1;
2710 	return scsi_change_queue_depth(sdev, qdepth);
2711 }
2712 
srp_send_tsk_mgmt(struct srp_rdma_ch * ch,u64 req_tag,u64 lun,u8 func,u8 * status)2713 static int srp_send_tsk_mgmt(struct srp_rdma_ch *ch, u64 req_tag, u64 lun,
2714 			     u8 func, u8 *status)
2715 {
2716 	struct srp_target_port *target = ch->target;
2717 	struct srp_rport *rport = target->rport;
2718 	struct ib_device *dev = target->srp_host->srp_dev->dev;
2719 	struct srp_iu *iu;
2720 	struct srp_tsk_mgmt *tsk_mgmt;
2721 	int res;
2722 
2723 	if (!ch->connected || target->qp_in_error)
2724 		return -1;
2725 
2726 	/*
2727 	 * Lock the rport mutex to avoid that srp_create_ch_ib() is
2728 	 * invoked while a task management function is being sent.
2729 	 */
2730 	mutex_lock(&rport->mutex);
2731 	spin_lock_irq(&ch->lock);
2732 	iu = __srp_get_tx_iu(ch, SRP_IU_TSK_MGMT);
2733 	spin_unlock_irq(&ch->lock);
2734 
2735 	if (!iu) {
2736 		mutex_unlock(&rport->mutex);
2737 
2738 		return -1;
2739 	}
2740 
2741 	iu->num_sge = 1;
2742 
2743 	ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt,
2744 				   DMA_TO_DEVICE);
2745 	tsk_mgmt = iu->buf;
2746 	memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
2747 
2748 	tsk_mgmt->opcode 	= SRP_TSK_MGMT;
2749 	int_to_scsilun(lun, &tsk_mgmt->lun);
2750 	tsk_mgmt->tsk_mgmt_func = func;
2751 	tsk_mgmt->task_tag	= req_tag;
2752 
2753 	spin_lock_irq(&ch->lock);
2754 	ch->tsk_mgmt_tag = (ch->tsk_mgmt_tag + 1) | SRP_TAG_TSK_MGMT;
2755 	tsk_mgmt->tag = ch->tsk_mgmt_tag;
2756 	spin_unlock_irq(&ch->lock);
2757 
2758 	init_completion(&ch->tsk_mgmt_done);
2759 
2760 	ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt,
2761 				      DMA_TO_DEVICE);
2762 	if (srp_post_send(ch, iu, sizeof(*tsk_mgmt))) {
2763 		srp_put_tx_iu(ch, iu, SRP_IU_TSK_MGMT);
2764 		mutex_unlock(&rport->mutex);
2765 
2766 		return -1;
2767 	}
2768 	res = wait_for_completion_timeout(&ch->tsk_mgmt_done,
2769 					msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS));
2770 	if (res > 0 && status)
2771 		*status = ch->tsk_mgmt_status;
2772 	mutex_unlock(&rport->mutex);
2773 
2774 	WARN_ON_ONCE(res < 0);
2775 
2776 	return res > 0 ? 0 : -1;
2777 }
2778 
srp_abort(struct scsi_cmnd * scmnd)2779 static int srp_abort(struct scsi_cmnd *scmnd)
2780 {
2781 	struct srp_target_port *target = host_to_target(scmnd->device->host);
2782 	struct srp_request *req = scsi_cmd_priv(scmnd);
2783 	u32 tag;
2784 	u16 ch_idx;
2785 	struct srp_rdma_ch *ch;
2786 	int ret;
2787 
2788 	shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
2789 
2790 	tag = blk_mq_unique_tag(scsi_cmd_to_rq(scmnd));
2791 	ch_idx = blk_mq_unique_tag_to_hwq(tag);
2792 	if (WARN_ON_ONCE(ch_idx >= target->ch_count))
2793 		return SUCCESS;
2794 	ch = &target->ch[ch_idx];
2795 	if (!srp_claim_req(ch, req, NULL, scmnd))
2796 		return SUCCESS;
2797 	shost_printk(KERN_ERR, target->scsi_host,
2798 		     "Sending SRP abort for tag %#x\n", tag);
2799 	if (srp_send_tsk_mgmt(ch, tag, scmnd->device->lun,
2800 			      SRP_TSK_ABORT_TASK, NULL) == 0)
2801 		ret = SUCCESS;
2802 	else if (target->rport->state == SRP_RPORT_LOST)
2803 		ret = FAST_IO_FAIL;
2804 	else
2805 		ret = FAILED;
2806 	if (ret == SUCCESS) {
2807 		srp_free_req(ch, req, scmnd, 0);
2808 		scmnd->result = DID_ABORT << 16;
2809 		scmnd->scsi_done(scmnd);
2810 	}
2811 
2812 	return ret;
2813 }
2814 
srp_reset_device(struct scsi_cmnd * scmnd)2815 static int srp_reset_device(struct scsi_cmnd *scmnd)
2816 {
2817 	struct srp_target_port *target = host_to_target(scmnd->device->host);
2818 	struct srp_rdma_ch *ch;
2819 	u8 status;
2820 
2821 	shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
2822 
2823 	ch = &target->ch[0];
2824 	if (srp_send_tsk_mgmt(ch, SRP_TAG_NO_REQ, scmnd->device->lun,
2825 			      SRP_TSK_LUN_RESET, &status))
2826 		return FAILED;
2827 	if (status)
2828 		return FAILED;
2829 
2830 	return SUCCESS;
2831 }
2832 
srp_reset_host(struct scsi_cmnd * scmnd)2833 static int srp_reset_host(struct scsi_cmnd *scmnd)
2834 {
2835 	struct srp_target_port *target = host_to_target(scmnd->device->host);
2836 
2837 	shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
2838 
2839 	return srp_reconnect_rport(target->rport) == 0 ? SUCCESS : FAILED;
2840 }
2841 
srp_target_alloc(struct scsi_target * starget)2842 static int srp_target_alloc(struct scsi_target *starget)
2843 {
2844 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2845 	struct srp_target_port *target = host_to_target(shost);
2846 
2847 	if (target->target_can_queue)
2848 		starget->can_queue = target->target_can_queue;
2849 	return 0;
2850 }
2851 
srp_slave_configure(struct scsi_device * sdev)2852 static int srp_slave_configure(struct scsi_device *sdev)
2853 {
2854 	struct Scsi_Host *shost = sdev->host;
2855 	struct srp_target_port *target = host_to_target(shost);
2856 	struct request_queue *q = sdev->request_queue;
2857 	unsigned long timeout;
2858 
2859 	if (sdev->type == TYPE_DISK) {
2860 		timeout = max_t(unsigned, 30 * HZ, target->rq_tmo_jiffies);
2861 		blk_queue_rq_timeout(q, timeout);
2862 	}
2863 
2864 	return 0;
2865 }
2866 
id_ext_show(struct device * dev,struct device_attribute * attr,char * buf)2867 static ssize_t id_ext_show(struct device *dev, struct device_attribute *attr,
2868 			   char *buf)
2869 {
2870 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
2871 
2872 	return sysfs_emit(buf, "0x%016llx\n", be64_to_cpu(target->id_ext));
2873 }
2874 
2875 static DEVICE_ATTR_RO(id_ext);
2876 
ioc_guid_show(struct device * dev,struct device_attribute * attr,char * buf)2877 static ssize_t ioc_guid_show(struct device *dev, struct device_attribute *attr,
2878 			     char *buf)
2879 {
2880 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
2881 
2882 	return sysfs_emit(buf, "0x%016llx\n", be64_to_cpu(target->ioc_guid));
2883 }
2884 
2885 static DEVICE_ATTR_RO(ioc_guid);
2886 
service_id_show(struct device * dev,struct device_attribute * attr,char * buf)2887 static ssize_t service_id_show(struct device *dev,
2888 			       struct device_attribute *attr, char *buf)
2889 {
2890 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
2891 
2892 	if (target->using_rdma_cm)
2893 		return -ENOENT;
2894 	return sysfs_emit(buf, "0x%016llx\n",
2895 			  be64_to_cpu(target->ib_cm.service_id));
2896 }
2897 
2898 static DEVICE_ATTR_RO(service_id);
2899 
pkey_show(struct device * dev,struct device_attribute * attr,char * buf)2900 static ssize_t pkey_show(struct device *dev, struct device_attribute *attr,
2901 			 char *buf)
2902 {
2903 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
2904 
2905 	if (target->using_rdma_cm)
2906 		return -ENOENT;
2907 
2908 	return sysfs_emit(buf, "0x%04x\n", be16_to_cpu(target->ib_cm.pkey));
2909 }
2910 
2911 static DEVICE_ATTR_RO(pkey);
2912 
sgid_show(struct device * dev,struct device_attribute * attr,char * buf)2913 static ssize_t sgid_show(struct device *dev, struct device_attribute *attr,
2914 			 char *buf)
2915 {
2916 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
2917 
2918 	return sysfs_emit(buf, "%pI6\n", target->sgid.raw);
2919 }
2920 
2921 static DEVICE_ATTR_RO(sgid);
2922 
dgid_show(struct device * dev,struct device_attribute * attr,char * buf)2923 static ssize_t dgid_show(struct device *dev, struct device_attribute *attr,
2924 			 char *buf)
2925 {
2926 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
2927 	struct srp_rdma_ch *ch = &target->ch[0];
2928 
2929 	if (target->using_rdma_cm)
2930 		return -ENOENT;
2931 
2932 	return sysfs_emit(buf, "%pI6\n", ch->ib_cm.path.dgid.raw);
2933 }
2934 
2935 static DEVICE_ATTR_RO(dgid);
2936 
orig_dgid_show(struct device * dev,struct device_attribute * attr,char * buf)2937 static ssize_t orig_dgid_show(struct device *dev, struct device_attribute *attr,
2938 			      char *buf)
2939 {
2940 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
2941 
2942 	if (target->using_rdma_cm)
2943 		return -ENOENT;
2944 
2945 	return sysfs_emit(buf, "%pI6\n", target->ib_cm.orig_dgid.raw);
2946 }
2947 
2948 static DEVICE_ATTR_RO(orig_dgid);
2949 
req_lim_show(struct device * dev,struct device_attribute * attr,char * buf)2950 static ssize_t req_lim_show(struct device *dev, struct device_attribute *attr,
2951 			    char *buf)
2952 {
2953 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
2954 	struct srp_rdma_ch *ch;
2955 	int i, req_lim = INT_MAX;
2956 
2957 	for (i = 0; i < target->ch_count; i++) {
2958 		ch = &target->ch[i];
2959 		req_lim = min(req_lim, ch->req_lim);
2960 	}
2961 
2962 	return sysfs_emit(buf, "%d\n", req_lim);
2963 }
2964 
2965 static DEVICE_ATTR_RO(req_lim);
2966 
zero_req_lim_show(struct device * dev,struct device_attribute * attr,char * buf)2967 static ssize_t zero_req_lim_show(struct device *dev,
2968 				 struct device_attribute *attr, char *buf)
2969 {
2970 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
2971 
2972 	return sysfs_emit(buf, "%d\n", target->zero_req_lim);
2973 }
2974 
2975 static DEVICE_ATTR_RO(zero_req_lim);
2976 
local_ib_port_show(struct device * dev,struct device_attribute * attr,char * buf)2977 static ssize_t local_ib_port_show(struct device *dev,
2978 				  struct device_attribute *attr, char *buf)
2979 {
2980 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
2981 
2982 	return sysfs_emit(buf, "%d\n", target->srp_host->port);
2983 }
2984 
2985 static DEVICE_ATTR_RO(local_ib_port);
2986 
local_ib_device_show(struct device * dev,struct device_attribute * attr,char * buf)2987 static ssize_t local_ib_device_show(struct device *dev,
2988 				    struct device_attribute *attr, char *buf)
2989 {
2990 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
2991 
2992 	return sysfs_emit(buf, "%s\n",
2993 			  dev_name(&target->srp_host->srp_dev->dev->dev));
2994 }
2995 
2996 static DEVICE_ATTR_RO(local_ib_device);
2997 
ch_count_show(struct device * dev,struct device_attribute * attr,char * buf)2998 static ssize_t ch_count_show(struct device *dev, struct device_attribute *attr,
2999 			     char *buf)
3000 {
3001 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
3002 
3003 	return sysfs_emit(buf, "%d\n", target->ch_count);
3004 }
3005 
3006 static DEVICE_ATTR_RO(ch_count);
3007 
comp_vector_show(struct device * dev,struct device_attribute * attr,char * buf)3008 static ssize_t comp_vector_show(struct device *dev,
3009 				struct device_attribute *attr, char *buf)
3010 {
3011 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
3012 
3013 	return sysfs_emit(buf, "%d\n", target->comp_vector);
3014 }
3015 
3016 static DEVICE_ATTR_RO(comp_vector);
3017 
tl_retry_count_show(struct device * dev,struct device_attribute * attr,char * buf)3018 static ssize_t tl_retry_count_show(struct device *dev,
3019 				   struct device_attribute *attr, char *buf)
3020 {
3021 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
3022 
3023 	return sysfs_emit(buf, "%d\n", target->tl_retry_count);
3024 }
3025 
3026 static DEVICE_ATTR_RO(tl_retry_count);
3027 
cmd_sg_entries_show(struct device * dev,struct device_attribute * attr,char * buf)3028 static ssize_t cmd_sg_entries_show(struct device *dev,
3029 				   struct device_attribute *attr, char *buf)
3030 {
3031 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
3032 
3033 	return sysfs_emit(buf, "%u\n", target->cmd_sg_cnt);
3034 }
3035 
3036 static DEVICE_ATTR_RO(cmd_sg_entries);
3037 
allow_ext_sg_show(struct device * dev,struct device_attribute * attr,char * buf)3038 static ssize_t allow_ext_sg_show(struct device *dev,
3039 				 struct device_attribute *attr, char *buf)
3040 {
3041 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
3042 
3043 	return sysfs_emit(buf, "%s\n", target->allow_ext_sg ? "true" : "false");
3044 }
3045 
3046 static DEVICE_ATTR_RO(allow_ext_sg);
3047 
3048 static struct device_attribute *srp_host_attrs[] = {
3049 	&dev_attr_id_ext,
3050 	&dev_attr_ioc_guid,
3051 	&dev_attr_service_id,
3052 	&dev_attr_pkey,
3053 	&dev_attr_sgid,
3054 	&dev_attr_dgid,
3055 	&dev_attr_orig_dgid,
3056 	&dev_attr_req_lim,
3057 	&dev_attr_zero_req_lim,
3058 	&dev_attr_local_ib_port,
3059 	&dev_attr_local_ib_device,
3060 	&dev_attr_ch_count,
3061 	&dev_attr_comp_vector,
3062 	&dev_attr_tl_retry_count,
3063 	&dev_attr_cmd_sg_entries,
3064 	&dev_attr_allow_ext_sg,
3065 	NULL
3066 };
3067 
3068 static struct scsi_host_template srp_template = {
3069 	.module				= THIS_MODULE,
3070 	.name				= "InfiniBand SRP initiator",
3071 	.proc_name			= DRV_NAME,
3072 	.target_alloc			= srp_target_alloc,
3073 	.slave_configure		= srp_slave_configure,
3074 	.info				= srp_target_info,
3075 	.init_cmd_priv			= srp_init_cmd_priv,
3076 	.exit_cmd_priv			= srp_exit_cmd_priv,
3077 	.queuecommand			= srp_queuecommand,
3078 	.change_queue_depth             = srp_change_queue_depth,
3079 	.eh_timed_out			= srp_timed_out,
3080 	.eh_abort_handler		= srp_abort,
3081 	.eh_device_reset_handler	= srp_reset_device,
3082 	.eh_host_reset_handler		= srp_reset_host,
3083 	.skip_settle_delay		= true,
3084 	.sg_tablesize			= SRP_DEF_SG_TABLESIZE,
3085 	.can_queue			= SRP_DEFAULT_CMD_SQ_SIZE,
3086 	.this_id			= -1,
3087 	.cmd_per_lun			= SRP_DEFAULT_CMD_SQ_SIZE,
3088 	.shost_attrs			= srp_host_attrs,
3089 	.track_queue_depth		= 1,
3090 	.cmd_size			= sizeof(struct srp_request),
3091 };
3092 
srp_sdev_count(struct Scsi_Host * host)3093 static int srp_sdev_count(struct Scsi_Host *host)
3094 {
3095 	struct scsi_device *sdev;
3096 	int c = 0;
3097 
3098 	shost_for_each_device(sdev, host)
3099 		c++;
3100 
3101 	return c;
3102 }
3103 
3104 /*
3105  * Return values:
3106  * < 0 upon failure. Caller is responsible for SRP target port cleanup.
3107  * 0 and target->state == SRP_TARGET_REMOVED if asynchronous target port
3108  *    removal has been scheduled.
3109  * 0 and target->state != SRP_TARGET_REMOVED upon success.
3110  */
srp_add_target(struct srp_host * host,struct srp_target_port * target)3111 static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
3112 {
3113 	struct srp_rport_identifiers ids;
3114 	struct srp_rport *rport;
3115 
3116 	target->state = SRP_TARGET_SCANNING;
3117 	sprintf(target->target_name, "SRP.T10:%016llX",
3118 		be64_to_cpu(target->id_ext));
3119 
3120 	if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dev.parent))
3121 		return -ENODEV;
3122 
3123 	memcpy(ids.port_id, &target->id_ext, 8);
3124 	memcpy(ids.port_id + 8, &target->ioc_guid, 8);
3125 	ids.roles = SRP_RPORT_ROLE_TARGET;
3126 	rport = srp_rport_add(target->scsi_host, &ids);
3127 	if (IS_ERR(rport)) {
3128 		scsi_remove_host(target->scsi_host);
3129 		return PTR_ERR(rport);
3130 	}
3131 
3132 	rport->lld_data = target;
3133 	target->rport = rport;
3134 
3135 	spin_lock(&host->target_lock);
3136 	list_add_tail(&target->list, &host->target_list);
3137 	spin_unlock(&host->target_lock);
3138 
3139 	scsi_scan_target(&target->scsi_host->shost_gendev,
3140 			 0, target->scsi_id, SCAN_WILD_CARD, SCSI_SCAN_INITIAL);
3141 
3142 	if (srp_connected_ch(target) < target->ch_count ||
3143 	    target->qp_in_error) {
3144 		shost_printk(KERN_INFO, target->scsi_host,
3145 			     PFX "SCSI scan failed - removing SCSI host\n");
3146 		srp_queue_remove_work(target);
3147 		goto out;
3148 	}
3149 
3150 	pr_debug("%s: SCSI scan succeeded - detected %d LUNs\n",
3151 		 dev_name(&target->scsi_host->shost_gendev),
3152 		 srp_sdev_count(target->scsi_host));
3153 
3154 	spin_lock_irq(&target->lock);
3155 	if (target->state == SRP_TARGET_SCANNING)
3156 		target->state = SRP_TARGET_LIVE;
3157 	spin_unlock_irq(&target->lock);
3158 
3159 out:
3160 	return 0;
3161 }
3162 
srp_release_dev(struct device * dev)3163 static void srp_release_dev(struct device *dev)
3164 {
3165 	struct srp_host *host =
3166 		container_of(dev, struct srp_host, dev);
3167 
3168 	complete(&host->released);
3169 }
3170 
3171 static struct class srp_class = {
3172 	.name    = "infiniband_srp",
3173 	.dev_release = srp_release_dev
3174 };
3175 
3176 /**
3177  * srp_conn_unique() - check whether the connection to a target is unique
3178  * @host:   SRP host.
3179  * @target: SRP target port.
3180  */
srp_conn_unique(struct srp_host * host,struct srp_target_port * target)3181 static bool srp_conn_unique(struct srp_host *host,
3182 			    struct srp_target_port *target)
3183 {
3184 	struct srp_target_port *t;
3185 	bool ret = false;
3186 
3187 	if (target->state == SRP_TARGET_REMOVED)
3188 		goto out;
3189 
3190 	ret = true;
3191 
3192 	spin_lock(&host->target_lock);
3193 	list_for_each_entry(t, &host->target_list, list) {
3194 		if (t != target &&
3195 		    target->id_ext == t->id_ext &&
3196 		    target->ioc_guid == t->ioc_guid &&
3197 		    target->initiator_ext == t->initiator_ext) {
3198 			ret = false;
3199 			break;
3200 		}
3201 	}
3202 	spin_unlock(&host->target_lock);
3203 
3204 out:
3205 	return ret;
3206 }
3207 
3208 /*
3209  * Target ports are added by writing
3210  *
3211  *     id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
3212  *     pkey=<P_Key>,service_id=<service ID>
3213  * or
3214  *     id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,
3215  *     [src=<IPv4 address>,]dest=<IPv4 address>:<port number>
3216  *
3217  * to the add_target sysfs attribute.
3218  */
3219 enum {
3220 	SRP_OPT_ERR		= 0,
3221 	SRP_OPT_ID_EXT		= 1 << 0,
3222 	SRP_OPT_IOC_GUID	= 1 << 1,
3223 	SRP_OPT_DGID		= 1 << 2,
3224 	SRP_OPT_PKEY		= 1 << 3,
3225 	SRP_OPT_SERVICE_ID	= 1 << 4,
3226 	SRP_OPT_MAX_SECT	= 1 << 5,
3227 	SRP_OPT_MAX_CMD_PER_LUN	= 1 << 6,
3228 	SRP_OPT_IO_CLASS	= 1 << 7,
3229 	SRP_OPT_INITIATOR_EXT	= 1 << 8,
3230 	SRP_OPT_CMD_SG_ENTRIES	= 1 << 9,
3231 	SRP_OPT_ALLOW_EXT_SG	= 1 << 10,
3232 	SRP_OPT_SG_TABLESIZE	= 1 << 11,
3233 	SRP_OPT_COMP_VECTOR	= 1 << 12,
3234 	SRP_OPT_TL_RETRY_COUNT	= 1 << 13,
3235 	SRP_OPT_QUEUE_SIZE	= 1 << 14,
3236 	SRP_OPT_IP_SRC		= 1 << 15,
3237 	SRP_OPT_IP_DEST		= 1 << 16,
3238 	SRP_OPT_TARGET_CAN_QUEUE= 1 << 17,
3239 	SRP_OPT_MAX_IT_IU_SIZE  = 1 << 18,
3240 	SRP_OPT_CH_COUNT	= 1 << 19,
3241 };
3242 
3243 static unsigned int srp_opt_mandatory[] = {
3244 	SRP_OPT_ID_EXT		|
3245 	SRP_OPT_IOC_GUID	|
3246 	SRP_OPT_DGID		|
3247 	SRP_OPT_PKEY		|
3248 	SRP_OPT_SERVICE_ID,
3249 	SRP_OPT_ID_EXT		|
3250 	SRP_OPT_IOC_GUID	|
3251 	SRP_OPT_IP_DEST,
3252 };
3253 
3254 static const match_table_t srp_opt_tokens = {
3255 	{ SRP_OPT_ID_EXT,		"id_ext=%s" 		},
3256 	{ SRP_OPT_IOC_GUID,		"ioc_guid=%s" 		},
3257 	{ SRP_OPT_DGID,			"dgid=%s" 		},
3258 	{ SRP_OPT_PKEY,			"pkey=%x" 		},
3259 	{ SRP_OPT_SERVICE_ID,		"service_id=%s"		},
3260 	{ SRP_OPT_MAX_SECT,		"max_sect=%d" 		},
3261 	{ SRP_OPT_MAX_CMD_PER_LUN,	"max_cmd_per_lun=%d" 	},
3262 	{ SRP_OPT_TARGET_CAN_QUEUE,	"target_can_queue=%d"	},
3263 	{ SRP_OPT_IO_CLASS,		"io_class=%x"		},
3264 	{ SRP_OPT_INITIATOR_EXT,	"initiator_ext=%s"	},
3265 	{ SRP_OPT_CMD_SG_ENTRIES,	"cmd_sg_entries=%u"	},
3266 	{ SRP_OPT_ALLOW_EXT_SG,		"allow_ext_sg=%u"	},
3267 	{ SRP_OPT_SG_TABLESIZE,		"sg_tablesize=%u"	},
3268 	{ SRP_OPT_COMP_VECTOR,		"comp_vector=%u"	},
3269 	{ SRP_OPT_TL_RETRY_COUNT,	"tl_retry_count=%u"	},
3270 	{ SRP_OPT_QUEUE_SIZE,		"queue_size=%d"		},
3271 	{ SRP_OPT_IP_SRC,		"src=%s"		},
3272 	{ SRP_OPT_IP_DEST,		"dest=%s"		},
3273 	{ SRP_OPT_MAX_IT_IU_SIZE,	"max_it_iu_size=%d"	},
3274 	{ SRP_OPT_CH_COUNT,		"ch_count=%u",		},
3275 	{ SRP_OPT_ERR,			NULL 			}
3276 };
3277 
3278 /**
3279  * srp_parse_in - parse an IP address and port number combination
3280  * @net:	   [in]  Network namespace.
3281  * @sa:		   [out] Address family, IP address and port number.
3282  * @addr_port_str: [in]  IP address and port number.
3283  * @has_port:	   [out] Whether or not @addr_port_str includes a port number.
3284  *
3285  * Parse the following address formats:
3286  * - IPv4: <ip_address>:<port>, e.g. 1.2.3.4:5.
3287  * - IPv6: \[<ipv6_address>\]:<port>, e.g. [1::2:3%4]:5.
3288  */
srp_parse_in(struct net * net,struct sockaddr_storage * sa,const char * addr_port_str,bool * has_port)3289 static int srp_parse_in(struct net *net, struct sockaddr_storage *sa,
3290 			const char *addr_port_str, bool *has_port)
3291 {
3292 	char *addr_end, *addr = kstrdup(addr_port_str, GFP_KERNEL);
3293 	char *port_str;
3294 	int ret;
3295 
3296 	if (!addr)
3297 		return -ENOMEM;
3298 	port_str = strrchr(addr, ':');
3299 	if (port_str && strchr(port_str, ']'))
3300 		port_str = NULL;
3301 	if (port_str)
3302 		*port_str++ = '\0';
3303 	if (has_port)
3304 		*has_port = port_str != NULL;
3305 	ret = inet_pton_with_scope(net, AF_INET, addr, port_str, sa);
3306 	if (ret && addr[0]) {
3307 		addr_end = addr + strlen(addr) - 1;
3308 		if (addr[0] == '[' && *addr_end == ']') {
3309 			*addr_end = '\0';
3310 			ret = inet_pton_with_scope(net, AF_INET6, addr + 1,
3311 						   port_str, sa);
3312 		}
3313 	}
3314 	kfree(addr);
3315 	pr_debug("%s -> %pISpfsc\n", addr_port_str, sa);
3316 	return ret;
3317 }
3318 
srp_parse_options(struct net * net,const char * buf,struct srp_target_port * target)3319 static int srp_parse_options(struct net *net, const char *buf,
3320 			     struct srp_target_port *target)
3321 {
3322 	char *options, *sep_opt;
3323 	char *p;
3324 	substring_t args[MAX_OPT_ARGS];
3325 	unsigned long long ull;
3326 	bool has_port;
3327 	int opt_mask = 0;
3328 	int token;
3329 	int ret = -EINVAL;
3330 	int i;
3331 
3332 	options = kstrdup(buf, GFP_KERNEL);
3333 	if (!options)
3334 		return -ENOMEM;
3335 
3336 	sep_opt = options;
3337 	while ((p = strsep(&sep_opt, ",\n")) != NULL) {
3338 		if (!*p)
3339 			continue;
3340 
3341 		token = match_token(p, srp_opt_tokens, args);
3342 		opt_mask |= token;
3343 
3344 		switch (token) {
3345 		case SRP_OPT_ID_EXT:
3346 			p = match_strdup(args);
3347 			if (!p) {
3348 				ret = -ENOMEM;
3349 				goto out;
3350 			}
3351 			ret = kstrtoull(p, 16, &ull);
3352 			if (ret) {
3353 				pr_warn("invalid id_ext parameter '%s'\n", p);
3354 				kfree(p);
3355 				goto out;
3356 			}
3357 			target->id_ext = cpu_to_be64(ull);
3358 			kfree(p);
3359 			break;
3360 
3361 		case SRP_OPT_IOC_GUID:
3362 			p = match_strdup(args);
3363 			if (!p) {
3364 				ret = -ENOMEM;
3365 				goto out;
3366 			}
3367 			ret = kstrtoull(p, 16, &ull);
3368 			if (ret) {
3369 				pr_warn("invalid ioc_guid parameter '%s'\n", p);
3370 				kfree(p);
3371 				goto out;
3372 			}
3373 			target->ioc_guid = cpu_to_be64(ull);
3374 			kfree(p);
3375 			break;
3376 
3377 		case SRP_OPT_DGID:
3378 			p = match_strdup(args);
3379 			if (!p) {
3380 				ret = -ENOMEM;
3381 				goto out;
3382 			}
3383 			if (strlen(p) != 32) {
3384 				pr_warn("bad dest GID parameter '%s'\n", p);
3385 				kfree(p);
3386 				goto out;
3387 			}
3388 
3389 			ret = hex2bin(target->ib_cm.orig_dgid.raw, p, 16);
3390 			kfree(p);
3391 			if (ret < 0)
3392 				goto out;
3393 			break;
3394 
3395 		case SRP_OPT_PKEY:
3396 			ret = match_hex(args, &token);
3397 			if (ret) {
3398 				pr_warn("bad P_Key parameter '%s'\n", p);
3399 				goto out;
3400 			}
3401 			target->ib_cm.pkey = cpu_to_be16(token);
3402 			break;
3403 
3404 		case SRP_OPT_SERVICE_ID:
3405 			p = match_strdup(args);
3406 			if (!p) {
3407 				ret = -ENOMEM;
3408 				goto out;
3409 			}
3410 			ret = kstrtoull(p, 16, &ull);
3411 			if (ret) {
3412 				pr_warn("bad service_id parameter '%s'\n", p);
3413 				kfree(p);
3414 				goto out;
3415 			}
3416 			target->ib_cm.service_id = cpu_to_be64(ull);
3417 			kfree(p);
3418 			break;
3419 
3420 		case SRP_OPT_IP_SRC:
3421 			p = match_strdup(args);
3422 			if (!p) {
3423 				ret = -ENOMEM;
3424 				goto out;
3425 			}
3426 			ret = srp_parse_in(net, &target->rdma_cm.src.ss, p,
3427 					   NULL);
3428 			if (ret < 0) {
3429 				pr_warn("bad source parameter '%s'\n", p);
3430 				kfree(p);
3431 				goto out;
3432 			}
3433 			target->rdma_cm.src_specified = true;
3434 			kfree(p);
3435 			break;
3436 
3437 		case SRP_OPT_IP_DEST:
3438 			p = match_strdup(args);
3439 			if (!p) {
3440 				ret = -ENOMEM;
3441 				goto out;
3442 			}
3443 			ret = srp_parse_in(net, &target->rdma_cm.dst.ss, p,
3444 					   &has_port);
3445 			if (!has_port)
3446 				ret = -EINVAL;
3447 			if (ret < 0) {
3448 				pr_warn("bad dest parameter '%s'\n", p);
3449 				kfree(p);
3450 				goto out;
3451 			}
3452 			target->using_rdma_cm = true;
3453 			kfree(p);
3454 			break;
3455 
3456 		case SRP_OPT_MAX_SECT:
3457 			ret = match_int(args, &token);
3458 			if (ret) {
3459 				pr_warn("bad max sect parameter '%s'\n", p);
3460 				goto out;
3461 			}
3462 			target->scsi_host->max_sectors = token;
3463 			break;
3464 
3465 		case SRP_OPT_QUEUE_SIZE:
3466 			ret = match_int(args, &token);
3467 			if (ret) {
3468 				pr_warn("match_int() failed for queue_size parameter '%s', Error %d\n",
3469 					p, ret);
3470 				goto out;
3471 			}
3472 			if (token < 1) {
3473 				pr_warn("bad queue_size parameter '%s'\n", p);
3474 				ret = -EINVAL;
3475 				goto out;
3476 			}
3477 			target->scsi_host->can_queue = token;
3478 			target->queue_size = token + SRP_RSP_SQ_SIZE +
3479 					     SRP_TSK_MGMT_SQ_SIZE;
3480 			if (!(opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
3481 				target->scsi_host->cmd_per_lun = token;
3482 			break;
3483 
3484 		case SRP_OPT_MAX_CMD_PER_LUN:
3485 			ret = match_int(args, &token);
3486 			if (ret) {
3487 				pr_warn("match_int() failed for max cmd_per_lun parameter '%s', Error %d\n",
3488 					p, ret);
3489 				goto out;
3490 			}
3491 			if (token < 1) {
3492 				pr_warn("bad max cmd_per_lun parameter '%s'\n",
3493 					p);
3494 				ret = -EINVAL;
3495 				goto out;
3496 			}
3497 			target->scsi_host->cmd_per_lun = token;
3498 			break;
3499 
3500 		case SRP_OPT_TARGET_CAN_QUEUE:
3501 			ret = match_int(args, &token);
3502 			if (ret) {
3503 				pr_warn("match_int() failed for max target_can_queue parameter '%s', Error %d\n",
3504 					p, ret);
3505 				goto out;
3506 			}
3507 			if (token < 1) {
3508 				pr_warn("bad max target_can_queue parameter '%s'\n",
3509 					p);
3510 				ret = -EINVAL;
3511 				goto out;
3512 			}
3513 			target->target_can_queue = token;
3514 			break;
3515 
3516 		case SRP_OPT_IO_CLASS:
3517 			ret = match_hex(args, &token);
3518 			if (ret) {
3519 				pr_warn("bad IO class parameter '%s'\n", p);
3520 				goto out;
3521 			}
3522 			if (token != SRP_REV10_IB_IO_CLASS &&
3523 			    token != SRP_REV16A_IB_IO_CLASS) {
3524 				pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n",
3525 					token, SRP_REV10_IB_IO_CLASS,
3526 					SRP_REV16A_IB_IO_CLASS);
3527 				ret = -EINVAL;
3528 				goto out;
3529 			}
3530 			target->io_class = token;
3531 			break;
3532 
3533 		case SRP_OPT_INITIATOR_EXT:
3534 			p = match_strdup(args);
3535 			if (!p) {
3536 				ret = -ENOMEM;
3537 				goto out;
3538 			}
3539 			ret = kstrtoull(p, 16, &ull);
3540 			if (ret) {
3541 				pr_warn("bad initiator_ext value '%s'\n", p);
3542 				kfree(p);
3543 				goto out;
3544 			}
3545 			target->initiator_ext = cpu_to_be64(ull);
3546 			kfree(p);
3547 			break;
3548 
3549 		case SRP_OPT_CMD_SG_ENTRIES:
3550 			ret = match_int(args, &token);
3551 			if (ret) {
3552 				pr_warn("match_int() failed for max cmd_sg_entries parameter '%s', Error %d\n",
3553 					p, ret);
3554 				goto out;
3555 			}
3556 			if (token < 1 || token > 255) {
3557 				pr_warn("bad max cmd_sg_entries parameter '%s'\n",
3558 					p);
3559 				ret = -EINVAL;
3560 				goto out;
3561 			}
3562 			target->cmd_sg_cnt = token;
3563 			break;
3564 
3565 		case SRP_OPT_ALLOW_EXT_SG:
3566 			ret = match_int(args, &token);
3567 			if (ret) {
3568 				pr_warn("bad allow_ext_sg parameter '%s'\n", p);
3569 				goto out;
3570 			}
3571 			target->allow_ext_sg = !!token;
3572 			break;
3573 
3574 		case SRP_OPT_SG_TABLESIZE:
3575 			ret = match_int(args, &token);
3576 			if (ret) {
3577 				pr_warn("match_int() failed for max sg_tablesize parameter '%s', Error %d\n",
3578 					p, ret);
3579 				goto out;
3580 			}
3581 			if (token < 1 || token > SG_MAX_SEGMENTS) {
3582 				pr_warn("bad max sg_tablesize parameter '%s'\n",
3583 					p);
3584 				ret = -EINVAL;
3585 				goto out;
3586 			}
3587 			target->sg_tablesize = token;
3588 			break;
3589 
3590 		case SRP_OPT_COMP_VECTOR:
3591 			ret = match_int(args, &token);
3592 			if (ret) {
3593 				pr_warn("match_int() failed for comp_vector parameter '%s', Error %d\n",
3594 					p, ret);
3595 				goto out;
3596 			}
3597 			if (token < 0) {
3598 				pr_warn("bad comp_vector parameter '%s'\n", p);
3599 				ret = -EINVAL;
3600 				goto out;
3601 			}
3602 			target->comp_vector = token;
3603 			break;
3604 
3605 		case SRP_OPT_TL_RETRY_COUNT:
3606 			ret = match_int(args, &token);
3607 			if (ret) {
3608 				pr_warn("match_int() failed for tl_retry_count parameter '%s', Error %d\n",
3609 					p, ret);
3610 				goto out;
3611 			}
3612 			if (token < 2 || token > 7) {
3613 				pr_warn("bad tl_retry_count parameter '%s' (must be a number between 2 and 7)\n",
3614 					p);
3615 				ret = -EINVAL;
3616 				goto out;
3617 			}
3618 			target->tl_retry_count = token;
3619 			break;
3620 
3621 		case SRP_OPT_MAX_IT_IU_SIZE:
3622 			ret = match_int(args, &token);
3623 			if (ret) {
3624 				pr_warn("match_int() failed for max it_iu_size parameter '%s', Error %d\n",
3625 					p, ret);
3626 				goto out;
3627 			}
3628 			if (token < 0) {
3629 				pr_warn("bad maximum initiator to target IU size '%s'\n", p);
3630 				ret = -EINVAL;
3631 				goto out;
3632 			}
3633 			target->max_it_iu_size = token;
3634 			break;
3635 
3636 		case SRP_OPT_CH_COUNT:
3637 			ret = match_int(args, &token);
3638 			if (ret) {
3639 				pr_warn("match_int() failed for channel count parameter '%s', Error %d\n",
3640 					p, ret);
3641 				goto out;
3642 			}
3643 			if (token < 1) {
3644 				pr_warn("bad channel count %s\n", p);
3645 				ret = -EINVAL;
3646 				goto out;
3647 			}
3648 			target->ch_count = token;
3649 			break;
3650 
3651 		default:
3652 			pr_warn("unknown parameter or missing value '%s' in target creation request\n",
3653 				p);
3654 			ret = -EINVAL;
3655 			goto out;
3656 		}
3657 	}
3658 
3659 	for (i = 0; i < ARRAY_SIZE(srp_opt_mandatory); i++) {
3660 		if ((opt_mask & srp_opt_mandatory[i]) == srp_opt_mandatory[i]) {
3661 			ret = 0;
3662 			break;
3663 		}
3664 	}
3665 	if (ret)
3666 		pr_warn("target creation request is missing one or more parameters\n");
3667 
3668 	if (target->scsi_host->cmd_per_lun > target->scsi_host->can_queue
3669 	    && (opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
3670 		pr_warn("cmd_per_lun = %d > queue_size = %d\n",
3671 			target->scsi_host->cmd_per_lun,
3672 			target->scsi_host->can_queue);
3673 
3674 out:
3675 	kfree(options);
3676 	return ret;
3677 }
3678 
add_target_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3679 static ssize_t add_target_store(struct device *dev,
3680 				struct device_attribute *attr, const char *buf,
3681 				size_t count)
3682 {
3683 	struct srp_host *host =
3684 		container_of(dev, struct srp_host, dev);
3685 	struct Scsi_Host *target_host;
3686 	struct srp_target_port *target;
3687 	struct srp_rdma_ch *ch;
3688 	struct srp_device *srp_dev = host->srp_dev;
3689 	struct ib_device *ibdev = srp_dev->dev;
3690 	int ret, i, ch_idx;
3691 	unsigned int max_sectors_per_mr, mr_per_cmd = 0;
3692 	bool multich = false;
3693 	uint32_t max_iu_len;
3694 
3695 	target_host = scsi_host_alloc(&srp_template,
3696 				      sizeof (struct srp_target_port));
3697 	if (!target_host)
3698 		return -ENOMEM;
3699 
3700 	target_host->transportt  = ib_srp_transport_template;
3701 	target_host->max_channel = 0;
3702 	target_host->max_id      = 1;
3703 	target_host->max_lun     = -1LL;
3704 	target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
3705 	target_host->max_segment_size = ib_dma_max_seg_size(ibdev);
3706 
3707 	if (!(ibdev->attrs.device_cap_flags & IB_DEVICE_SG_GAPS_REG))
3708 		target_host->virt_boundary_mask = ~srp_dev->mr_page_mask;
3709 
3710 	target = host_to_target(target_host);
3711 
3712 	target->net		= kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
3713 	target->io_class	= SRP_REV16A_IB_IO_CLASS;
3714 	target->scsi_host	= target_host;
3715 	target->srp_host	= host;
3716 	target->lkey		= host->srp_dev->pd->local_dma_lkey;
3717 	target->global_rkey	= host->srp_dev->global_rkey;
3718 	target->cmd_sg_cnt	= cmd_sg_entries;
3719 	target->sg_tablesize	= indirect_sg_entries ? : cmd_sg_entries;
3720 	target->allow_ext_sg	= allow_ext_sg;
3721 	target->tl_retry_count	= 7;
3722 	target->queue_size	= SRP_DEFAULT_QUEUE_SIZE;
3723 
3724 	/*
3725 	 * Avoid that the SCSI host can be removed by srp_remove_target()
3726 	 * before this function returns.
3727 	 */
3728 	scsi_host_get(target->scsi_host);
3729 
3730 	ret = mutex_lock_interruptible(&host->add_target_mutex);
3731 	if (ret < 0)
3732 		goto put;
3733 
3734 	ret = srp_parse_options(target->net, buf, target);
3735 	if (ret)
3736 		goto out;
3737 
3738 	if (!srp_conn_unique(target->srp_host, target)) {
3739 		if (target->using_rdma_cm) {
3740 			shost_printk(KERN_INFO, target->scsi_host,
3741 				     PFX "Already connected to target port with id_ext=%016llx;ioc_guid=%016llx;dest=%pIS\n",
3742 				     be64_to_cpu(target->id_ext),
3743 				     be64_to_cpu(target->ioc_guid),
3744 				     &target->rdma_cm.dst);
3745 		} else {
3746 			shost_printk(KERN_INFO, target->scsi_host,
3747 				     PFX "Already connected to target port with id_ext=%016llx;ioc_guid=%016llx;initiator_ext=%016llx\n",
3748 				     be64_to_cpu(target->id_ext),
3749 				     be64_to_cpu(target->ioc_guid),
3750 				     be64_to_cpu(target->initiator_ext));
3751 		}
3752 		ret = -EEXIST;
3753 		goto out;
3754 	}
3755 
3756 	if (!srp_dev->has_fr && !target->allow_ext_sg &&
3757 	    target->cmd_sg_cnt < target->sg_tablesize) {
3758 		pr_warn("No MR pool and no external indirect descriptors, limiting sg_tablesize to cmd_sg_cnt\n");
3759 		target->sg_tablesize = target->cmd_sg_cnt;
3760 	}
3761 
3762 	if (srp_dev->use_fast_reg) {
3763 		bool gaps_reg = (ibdev->attrs.device_cap_flags &
3764 				 IB_DEVICE_SG_GAPS_REG);
3765 
3766 		max_sectors_per_mr = srp_dev->max_pages_per_mr <<
3767 				  (ilog2(srp_dev->mr_page_size) - 9);
3768 		if (!gaps_reg) {
3769 			/*
3770 			 * FR can only map one HCA page per entry. If the start
3771 			 * address is not aligned on a HCA page boundary two
3772 			 * entries will be used for the head and the tail
3773 			 * although these two entries combined contain at most
3774 			 * one HCA page of data. Hence the "+ 1" in the
3775 			 * calculation below.
3776 			 *
3777 			 * The indirect data buffer descriptor is contiguous
3778 			 * so the memory for that buffer will only be
3779 			 * registered if register_always is true. Hence add
3780 			 * one to mr_per_cmd if register_always has been set.
3781 			 */
3782 			mr_per_cmd = register_always +
3783 				(target->scsi_host->max_sectors + 1 +
3784 				 max_sectors_per_mr - 1) / max_sectors_per_mr;
3785 		} else {
3786 			mr_per_cmd = register_always +
3787 				(target->sg_tablesize +
3788 				 srp_dev->max_pages_per_mr - 1) /
3789 				srp_dev->max_pages_per_mr;
3790 		}
3791 		pr_debug("max_sectors = %u; max_pages_per_mr = %u; mr_page_size = %u; max_sectors_per_mr = %u; mr_per_cmd = %u\n",
3792 			 target->scsi_host->max_sectors, srp_dev->max_pages_per_mr, srp_dev->mr_page_size,
3793 			 max_sectors_per_mr, mr_per_cmd);
3794 	}
3795 
3796 	target_host->sg_tablesize = target->sg_tablesize;
3797 	target->mr_pool_size = target->scsi_host->can_queue * mr_per_cmd;
3798 	target->mr_per_cmd = mr_per_cmd;
3799 	target->indirect_size = target->sg_tablesize *
3800 				sizeof (struct srp_direct_buf);
3801 	max_iu_len = srp_max_it_iu_len(target->cmd_sg_cnt,
3802 				       srp_use_imm_data,
3803 				       target->max_it_iu_size);
3804 
3805 	INIT_WORK(&target->tl_err_work, srp_tl_err_work);
3806 	INIT_WORK(&target->remove_work, srp_remove_work);
3807 	spin_lock_init(&target->lock);
3808 	ret = rdma_query_gid(ibdev, host->port, 0, &target->sgid);
3809 	if (ret)
3810 		goto out;
3811 
3812 	ret = -ENOMEM;
3813 	if (target->ch_count == 0) {
3814 		target->ch_count =
3815 			min(ch_count ?:
3816 				max(4 * num_online_nodes(),
3817 				    ibdev->num_comp_vectors),
3818 				num_online_cpus());
3819 	}
3820 
3821 	target->ch = kcalloc(target->ch_count, sizeof(*target->ch),
3822 			     GFP_KERNEL);
3823 	if (!target->ch)
3824 		goto out;
3825 
3826 	for (ch_idx = 0; ch_idx < target->ch_count; ++ch_idx) {
3827 		ch = &target->ch[ch_idx];
3828 		ch->target = target;
3829 		ch->comp_vector = ch_idx % ibdev->num_comp_vectors;
3830 		spin_lock_init(&ch->lock);
3831 		INIT_LIST_HEAD(&ch->free_tx);
3832 		ret = srp_new_cm_id(ch);
3833 		if (ret)
3834 			goto err_disconnect;
3835 
3836 		ret = srp_create_ch_ib(ch);
3837 		if (ret)
3838 			goto err_disconnect;
3839 
3840 		ret = srp_connect_ch(ch, max_iu_len, multich);
3841 		if (ret) {
3842 			char dst[64];
3843 
3844 			if (target->using_rdma_cm)
3845 				snprintf(dst, sizeof(dst), "%pIS",
3846 					&target->rdma_cm.dst);
3847 			else
3848 				snprintf(dst, sizeof(dst), "%pI6",
3849 					target->ib_cm.orig_dgid.raw);
3850 			shost_printk(KERN_ERR, target->scsi_host,
3851 				PFX "Connection %d/%d to %s failed\n",
3852 				ch_idx,
3853 				target->ch_count, dst);
3854 			if (ch_idx == 0) {
3855 				goto free_ch;
3856 			} else {
3857 				srp_free_ch_ib(target, ch);
3858 				target->ch_count = ch - target->ch;
3859 				goto connected;
3860 			}
3861 		}
3862 		multich = true;
3863 	}
3864 
3865 connected:
3866 	target->scsi_host->nr_hw_queues = target->ch_count;
3867 
3868 	ret = srp_add_target(host, target);
3869 	if (ret)
3870 		goto err_disconnect;
3871 
3872 	if (target->state != SRP_TARGET_REMOVED) {
3873 		if (target->using_rdma_cm) {
3874 			shost_printk(KERN_DEBUG, target->scsi_host, PFX
3875 				     "new target: id_ext %016llx ioc_guid %016llx sgid %pI6 dest %pIS\n",
3876 				     be64_to_cpu(target->id_ext),
3877 				     be64_to_cpu(target->ioc_guid),
3878 				     target->sgid.raw, &target->rdma_cm.dst);
3879 		} else {
3880 			shost_printk(KERN_DEBUG, target->scsi_host, PFX
3881 				     "new target: id_ext %016llx ioc_guid %016llx pkey %04x service_id %016llx sgid %pI6 dgid %pI6\n",
3882 				     be64_to_cpu(target->id_ext),
3883 				     be64_to_cpu(target->ioc_guid),
3884 				     be16_to_cpu(target->ib_cm.pkey),
3885 				     be64_to_cpu(target->ib_cm.service_id),
3886 				     target->sgid.raw,
3887 				     target->ib_cm.orig_dgid.raw);
3888 		}
3889 	}
3890 
3891 	ret = count;
3892 
3893 out:
3894 	mutex_unlock(&host->add_target_mutex);
3895 
3896 put:
3897 	scsi_host_put(target->scsi_host);
3898 	if (ret < 0) {
3899 		/*
3900 		 * If a call to srp_remove_target() has not been scheduled,
3901 		 * drop the network namespace reference now that was obtained
3902 		 * earlier in this function.
3903 		 */
3904 		if (target->state != SRP_TARGET_REMOVED)
3905 			kobj_ns_drop(KOBJ_NS_TYPE_NET, target->net);
3906 		scsi_host_put(target->scsi_host);
3907 	}
3908 
3909 	return ret;
3910 
3911 err_disconnect:
3912 	srp_disconnect_target(target);
3913 
3914 free_ch:
3915 	for (i = 0; i < target->ch_count; i++) {
3916 		ch = &target->ch[i];
3917 		srp_free_ch_ib(target, ch);
3918 	}
3919 
3920 	kfree(target->ch);
3921 	goto out;
3922 }
3923 
3924 static DEVICE_ATTR_WO(add_target);
3925 
ibdev_show(struct device * dev,struct device_attribute * attr,char * buf)3926 static ssize_t ibdev_show(struct device *dev, struct device_attribute *attr,
3927 			  char *buf)
3928 {
3929 	struct srp_host *host = container_of(dev, struct srp_host, dev);
3930 
3931 	return sysfs_emit(buf, "%s\n", dev_name(&host->srp_dev->dev->dev));
3932 }
3933 
3934 static DEVICE_ATTR_RO(ibdev);
3935 
port_show(struct device * dev,struct device_attribute * attr,char * buf)3936 static ssize_t port_show(struct device *dev, struct device_attribute *attr,
3937 			 char *buf)
3938 {
3939 	struct srp_host *host = container_of(dev, struct srp_host, dev);
3940 
3941 	return sysfs_emit(buf, "%d\n", host->port);
3942 }
3943 
3944 static DEVICE_ATTR_RO(port);
3945 
srp_add_port(struct srp_device * device,u8 port)3946 static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
3947 {
3948 	struct srp_host *host;
3949 
3950 	host = kzalloc(sizeof *host, GFP_KERNEL);
3951 	if (!host)
3952 		return NULL;
3953 
3954 	INIT_LIST_HEAD(&host->target_list);
3955 	spin_lock_init(&host->target_lock);
3956 	init_completion(&host->released);
3957 	mutex_init(&host->add_target_mutex);
3958 	host->srp_dev = device;
3959 	host->port = port;
3960 
3961 	host->dev.class = &srp_class;
3962 	host->dev.parent = device->dev->dev.parent;
3963 	dev_set_name(&host->dev, "srp-%s-%d", dev_name(&device->dev->dev),
3964 		     port);
3965 
3966 	if (device_register(&host->dev))
3967 		goto free_host;
3968 	if (device_create_file(&host->dev, &dev_attr_add_target))
3969 		goto err_class;
3970 	if (device_create_file(&host->dev, &dev_attr_ibdev))
3971 		goto err_class;
3972 	if (device_create_file(&host->dev, &dev_attr_port))
3973 		goto err_class;
3974 
3975 	return host;
3976 
3977 err_class:
3978 	device_unregister(&host->dev);
3979 
3980 free_host:
3981 	kfree(host);
3982 
3983 	return NULL;
3984 }
3985 
srp_rename_dev(struct ib_device * device,void * client_data)3986 static void srp_rename_dev(struct ib_device *device, void *client_data)
3987 {
3988 	struct srp_device *srp_dev = client_data;
3989 	struct srp_host *host, *tmp_host;
3990 
3991 	list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
3992 		char name[IB_DEVICE_NAME_MAX + 8];
3993 
3994 		snprintf(name, sizeof(name), "srp-%s-%d",
3995 			 dev_name(&device->dev), host->port);
3996 		device_rename(&host->dev, name);
3997 	}
3998 }
3999 
srp_add_one(struct ib_device * device)4000 static int srp_add_one(struct ib_device *device)
4001 {
4002 	struct srp_device *srp_dev;
4003 	struct ib_device_attr *attr = &device->attrs;
4004 	struct srp_host *host;
4005 	int mr_page_shift;
4006 	unsigned int p;
4007 	u64 max_pages_per_mr;
4008 	unsigned int flags = 0;
4009 
4010 	srp_dev = kzalloc(sizeof(*srp_dev), GFP_KERNEL);
4011 	if (!srp_dev)
4012 		return -ENOMEM;
4013 
4014 	/*
4015 	 * Use the smallest page size supported by the HCA, down to a
4016 	 * minimum of 4096 bytes. We're unlikely to build large sglists
4017 	 * out of smaller entries.
4018 	 */
4019 	mr_page_shift		= max(12, ffs(attr->page_size_cap) - 1);
4020 	srp_dev->mr_page_size	= 1 << mr_page_shift;
4021 	srp_dev->mr_page_mask	= ~((u64) srp_dev->mr_page_size - 1);
4022 	max_pages_per_mr	= attr->max_mr_size;
4023 	do_div(max_pages_per_mr, srp_dev->mr_page_size);
4024 	pr_debug("%s: %llu / %u = %llu <> %u\n", __func__,
4025 		 attr->max_mr_size, srp_dev->mr_page_size,
4026 		 max_pages_per_mr, SRP_MAX_PAGES_PER_MR);
4027 	srp_dev->max_pages_per_mr = min_t(u64, SRP_MAX_PAGES_PER_MR,
4028 					  max_pages_per_mr);
4029 
4030 	srp_dev->has_fr = (attr->device_cap_flags &
4031 			   IB_DEVICE_MEM_MGT_EXTENSIONS);
4032 	if (!never_register && !srp_dev->has_fr)
4033 		dev_warn(&device->dev, "FR is not supported\n");
4034 	else if (!never_register &&
4035 		 attr->max_mr_size >= 2 * srp_dev->mr_page_size)
4036 		srp_dev->use_fast_reg = srp_dev->has_fr;
4037 
4038 	if (never_register || !register_always || !srp_dev->has_fr)
4039 		flags |= IB_PD_UNSAFE_GLOBAL_RKEY;
4040 
4041 	if (srp_dev->use_fast_reg) {
4042 		srp_dev->max_pages_per_mr =
4043 			min_t(u32, srp_dev->max_pages_per_mr,
4044 			      attr->max_fast_reg_page_list_len);
4045 	}
4046 	srp_dev->mr_max_size	= srp_dev->mr_page_size *
4047 				   srp_dev->max_pages_per_mr;
4048 	pr_debug("%s: mr_page_shift = %d, device->max_mr_size = %#llx, device->max_fast_reg_page_list_len = %u, max_pages_per_mr = %d, mr_max_size = %#x\n",
4049 		 dev_name(&device->dev), mr_page_shift, attr->max_mr_size,
4050 		 attr->max_fast_reg_page_list_len,
4051 		 srp_dev->max_pages_per_mr, srp_dev->mr_max_size);
4052 
4053 	INIT_LIST_HEAD(&srp_dev->dev_list);
4054 
4055 	srp_dev->dev = device;
4056 	srp_dev->pd  = ib_alloc_pd(device, flags);
4057 	if (IS_ERR(srp_dev->pd)) {
4058 		int ret = PTR_ERR(srp_dev->pd);
4059 
4060 		kfree(srp_dev);
4061 		return ret;
4062 	}
4063 
4064 	if (flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
4065 		srp_dev->global_rkey = srp_dev->pd->unsafe_global_rkey;
4066 		WARN_ON_ONCE(srp_dev->global_rkey == 0);
4067 	}
4068 
4069 	rdma_for_each_port (device, p) {
4070 		host = srp_add_port(srp_dev, p);
4071 		if (host)
4072 			list_add_tail(&host->list, &srp_dev->dev_list);
4073 	}
4074 
4075 	ib_set_client_data(device, &srp_client, srp_dev);
4076 	return 0;
4077 }
4078 
srp_remove_one(struct ib_device * device,void * client_data)4079 static void srp_remove_one(struct ib_device *device, void *client_data)
4080 {
4081 	struct srp_device *srp_dev;
4082 	struct srp_host *host, *tmp_host;
4083 	struct srp_target_port *target;
4084 
4085 	srp_dev = client_data;
4086 
4087 	list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
4088 		device_unregister(&host->dev);
4089 		/*
4090 		 * Wait for the sysfs entry to go away, so that no new
4091 		 * target ports can be created.
4092 		 */
4093 		wait_for_completion(&host->released);
4094 
4095 		/*
4096 		 * Remove all target ports.
4097 		 */
4098 		spin_lock(&host->target_lock);
4099 		list_for_each_entry(target, &host->target_list, list)
4100 			srp_queue_remove_work(target);
4101 		spin_unlock(&host->target_lock);
4102 
4103 		/*
4104 		 * srp_queue_remove_work() queues a call to
4105 		 * srp_remove_target(). The latter function cancels
4106 		 * target->tl_err_work so waiting for the remove works to
4107 		 * finish is sufficient.
4108 		 */
4109 		flush_workqueue(srp_remove_wq);
4110 
4111 		kfree(host);
4112 	}
4113 
4114 	ib_dealloc_pd(srp_dev->pd);
4115 
4116 	kfree(srp_dev);
4117 }
4118 
4119 static struct srp_function_template ib_srp_transport_functions = {
4120 	.has_rport_state	 = true,
4121 	.reset_timer_if_blocked	 = true,
4122 	.reconnect_delay	 = &srp_reconnect_delay,
4123 	.fast_io_fail_tmo	 = &srp_fast_io_fail_tmo,
4124 	.dev_loss_tmo		 = &srp_dev_loss_tmo,
4125 	.reconnect		 = srp_rport_reconnect,
4126 	.rport_delete		 = srp_rport_delete,
4127 	.terminate_rport_io	 = srp_terminate_io,
4128 };
4129 
srp_init_module(void)4130 static int __init srp_init_module(void)
4131 {
4132 	int ret;
4133 
4134 	BUILD_BUG_ON(sizeof(struct srp_aer_req) != 36);
4135 	BUILD_BUG_ON(sizeof(struct srp_cmd) != 48);
4136 	BUILD_BUG_ON(sizeof(struct srp_imm_buf) != 4);
4137 	BUILD_BUG_ON(sizeof(struct srp_indirect_buf) != 20);
4138 	BUILD_BUG_ON(sizeof(struct srp_login_req) != 64);
4139 	BUILD_BUG_ON(sizeof(struct srp_login_req_rdma) != 56);
4140 	BUILD_BUG_ON(sizeof(struct srp_rsp) != 36);
4141 
4142 	if (srp_sg_tablesize) {
4143 		pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n");
4144 		if (!cmd_sg_entries)
4145 			cmd_sg_entries = srp_sg_tablesize;
4146 	}
4147 
4148 	if (!cmd_sg_entries)
4149 		cmd_sg_entries = SRP_DEF_SG_TABLESIZE;
4150 
4151 	if (cmd_sg_entries > 255) {
4152 		pr_warn("Clamping cmd_sg_entries to 255\n");
4153 		cmd_sg_entries = 255;
4154 	}
4155 
4156 	if (!indirect_sg_entries)
4157 		indirect_sg_entries = cmd_sg_entries;
4158 	else if (indirect_sg_entries < cmd_sg_entries) {
4159 		pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n",
4160 			cmd_sg_entries);
4161 		indirect_sg_entries = cmd_sg_entries;
4162 	}
4163 
4164 	if (indirect_sg_entries > SG_MAX_SEGMENTS) {
4165 		pr_warn("Clamping indirect_sg_entries to %u\n",
4166 			SG_MAX_SEGMENTS);
4167 		indirect_sg_entries = SG_MAX_SEGMENTS;
4168 	}
4169 
4170 	srp_remove_wq = create_workqueue("srp_remove");
4171 	if (!srp_remove_wq) {
4172 		ret = -ENOMEM;
4173 		goto out;
4174 	}
4175 
4176 	ret = -ENOMEM;
4177 	ib_srp_transport_template =
4178 		srp_attach_transport(&ib_srp_transport_functions);
4179 	if (!ib_srp_transport_template)
4180 		goto destroy_wq;
4181 
4182 	ret = class_register(&srp_class);
4183 	if (ret) {
4184 		pr_err("couldn't register class infiniband_srp\n");
4185 		goto release_tr;
4186 	}
4187 
4188 	ib_sa_register_client(&srp_sa_client);
4189 
4190 	ret = ib_register_client(&srp_client);
4191 	if (ret) {
4192 		pr_err("couldn't register IB client\n");
4193 		goto unreg_sa;
4194 	}
4195 
4196 out:
4197 	return ret;
4198 
4199 unreg_sa:
4200 	ib_sa_unregister_client(&srp_sa_client);
4201 	class_unregister(&srp_class);
4202 
4203 release_tr:
4204 	srp_release_transport(ib_srp_transport_template);
4205 
4206 destroy_wq:
4207 	destroy_workqueue(srp_remove_wq);
4208 	goto out;
4209 }
4210 
srp_cleanup_module(void)4211 static void __exit srp_cleanup_module(void)
4212 {
4213 	ib_unregister_client(&srp_client);
4214 	ib_sa_unregister_client(&srp_sa_client);
4215 	class_unregister(&srp_class);
4216 	srp_release_transport(ib_srp_transport_template);
4217 	destroy_workqueue(srp_remove_wq);
4218 }
4219 
4220 module_init(srp_init_module);
4221 module_exit(srp_cleanup_module);
4222