blob: acda65936588d8b8d9d3803cf9cf497f0fda0506 [file] [log] [blame]
Roland Dreieraef9ec32005-11-02 14:07:13 -08001/*
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.
Roland Dreieraef9ec32005-11-02 14:07:13 -080031 */
32
Joe Perchesd236cd02013-02-01 14:33:58 -080033#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Bart Van Asschee0bda7d2012-01-14 12:39:44 +000034
Roland Dreieraef9ec32005-11-02 14:07:13 -080035#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>
Tim Schmielaude259682006-01-08 01:02:05 -080042#include <linux/jiffies.h>
Bart Van Assche56b53902014-07-09 15:58:22 +020043#include <rdma/ib_cache.h>
Roland Dreieraef9ec32005-11-02 14:07:13 -080044
Arun Sharma600634972011-07-26 16:09:06 -070045#include <linux/atomic.h>
Roland Dreieraef9ec32005-11-02 14:07:13 -080046
47#include <scsi/scsi.h>
48#include <scsi/scsi_device.h>
49#include <scsi/scsi_dbg.h>
Jack Wang71444b92013-11-07 11:37:37 +010050#include <scsi/scsi_tcq.h>
Roland Dreieraef9ec32005-11-02 14:07:13 -080051#include <scsi/srp.h>
FUJITA Tomonori32368222007-06-27 16:33:12 +090052#include <scsi/scsi_transport_srp.h>
Roland Dreieraef9ec32005-11-02 14:07:13 -080053
Roland Dreieraef9ec32005-11-02 14:07:13 -080054#include "ib_srp.h"
55
56#define DRV_NAME "ib_srp"
57#define PFX DRV_NAME ": "
Bart Van Assche713ef242015-07-31 14:13:52 -070058#define DRV_VERSION "2.0"
59#define DRV_RELDATE "July 26, 2015"
Roland Dreieraef9ec32005-11-02 14:07:13 -080060
61MODULE_AUTHOR("Roland Dreier");
Bart Van Assche33ab3e52015-05-18 13:25:27 +020062MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator");
Roland Dreieraef9ec32005-11-02 14:07:13 -080063MODULE_LICENSE("Dual BSD/GPL");
Bart Van Assche33ab3e52015-05-18 13:25:27 +020064MODULE_VERSION(DRV_VERSION);
65MODULE_INFO(release_date, DRV_RELDATE);
Roland Dreieraef9ec32005-11-02 14:07:13 -080066
David Dillow49248642011-01-14 18:23:24 -050067static unsigned int srp_sg_tablesize;
68static unsigned int cmd_sg_entries;
David Dillowc07d4242011-01-16 13:57:10 -050069static unsigned int indirect_sg_entries;
70static bool allow_ext_sg;
Bart Van Assche5cfb1782014-05-20 15:08:34 +020071static bool prefer_fr;
Bart Van Asscheb1b88542014-05-20 15:06:41 +020072static bool register_always;
Roland Dreieraef9ec32005-11-02 14:07:13 -080073static int topspin_workarounds = 1;
74
David Dillow49248642011-01-14 18:23:24 -050075module_param(srp_sg_tablesize, uint, 0444);
76MODULE_PARM_DESC(srp_sg_tablesize, "Deprecated name for cmd_sg_entries");
77
78module_param(cmd_sg_entries, uint, 0444);
79MODULE_PARM_DESC(cmd_sg_entries,
80 "Default number of gather/scatter entries in the SRP command (default is 12, max 255)");
81
David Dillowc07d4242011-01-16 13:57:10 -050082module_param(indirect_sg_entries, uint, 0444);
83MODULE_PARM_DESC(indirect_sg_entries,
84 "Default max number of gather/scatter entries (default is 12, max is " __stringify(SCSI_MAX_SG_CHAIN_SEGMENTS) ")");
85
86module_param(allow_ext_sg, bool, 0444);
87MODULE_PARM_DESC(allow_ext_sg,
88 "Default behavior when there are more than cmd_sg_entries S/G entries after mapping; fails the request when false (default false)");
89
Roland Dreieraef9ec32005-11-02 14:07:13 -080090module_param(topspin_workarounds, int, 0444);
91MODULE_PARM_DESC(topspin_workarounds,
92 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
93
Bart Van Assche5cfb1782014-05-20 15:08:34 +020094module_param(prefer_fr, bool, 0444);
95MODULE_PARM_DESC(prefer_fr,
96"Whether to use fast registration if both FMR and fast registration are supported");
97
Bart Van Asscheb1b88542014-05-20 15:06:41 +020098module_param(register_always, bool, 0444);
99MODULE_PARM_DESC(register_always,
100 "Use memory registration even for contiguous memory regions");
101
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930102static const struct kernel_param_ops srp_tmo_ops;
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200103
Bart Van Asschea95cadb2013-10-26 14:37:17 +0200104static int srp_reconnect_delay = 10;
105module_param_cb(reconnect_delay, &srp_tmo_ops, &srp_reconnect_delay,
106 S_IRUGO | S_IWUSR);
107MODULE_PARM_DESC(reconnect_delay, "Time between successive reconnect attempts");
108
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200109static int srp_fast_io_fail_tmo = 15;
110module_param_cb(fast_io_fail_tmo, &srp_tmo_ops, &srp_fast_io_fail_tmo,
111 S_IRUGO | S_IWUSR);
112MODULE_PARM_DESC(fast_io_fail_tmo,
113 "Number of seconds between the observation of a transport"
114 " layer error and failing all I/O. \"off\" means that this"
115 " functionality is disabled.");
116
Bart Van Asschea95cadb2013-10-26 14:37:17 +0200117static int srp_dev_loss_tmo = 600;
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200118module_param_cb(dev_loss_tmo, &srp_tmo_ops, &srp_dev_loss_tmo,
119 S_IRUGO | S_IWUSR);
120MODULE_PARM_DESC(dev_loss_tmo,
121 "Maximum number of seconds that the SRP transport should"
122 " insulate transport layer errors. After this time has been"
123 " exceeded the SCSI host is removed. Should be"
124 " between 1 and " __stringify(SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
125 " if fast_io_fail_tmo has not been set. \"off\" means that"
126 " this functionality is disabled.");
127
Bart Van Assched92c0da2014-10-06 17:14:36 +0200128static unsigned ch_count;
129module_param(ch_count, uint, 0444);
130MODULE_PARM_DESC(ch_count,
131 "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.");
132
Roland Dreieraef9ec32005-11-02 14:07:13 -0800133static void srp_add_one(struct ib_device *device);
Haggai Eran7c1eb452015-07-30 17:50:14 +0300134static void srp_remove_one(struct ib_device *device, void *client_data);
Bart Van Assche509c07b2014-10-30 14:48:30 +0100135static void srp_recv_completion(struct ib_cq *cq, void *ch_ptr);
136static void srp_send_completion(struct ib_cq *cq, void *ch_ptr);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800137static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
138
FUJITA Tomonori32368222007-06-27 16:33:12 +0900139static struct scsi_transport_template *ib_srp_transport_template;
Bart Van Asschebcc05912014-07-09 15:57:26 +0200140static struct workqueue_struct *srp_remove_wq;
FUJITA Tomonori32368222007-06-27 16:33:12 +0900141
Roland Dreieraef9ec32005-11-02 14:07:13 -0800142static struct ib_client srp_client = {
143 .name = "srp",
144 .add = srp_add_one,
145 .remove = srp_remove_one
146};
147
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700148static struct ib_sa_client srp_sa_client;
149
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200150static int srp_tmo_get(char *buffer, const struct kernel_param *kp)
151{
152 int tmo = *(int *)kp->arg;
153
154 if (tmo >= 0)
155 return sprintf(buffer, "%d", tmo);
156 else
157 return sprintf(buffer, "off");
158}
159
160static int srp_tmo_set(const char *val, const struct kernel_param *kp)
161{
162 int tmo, res;
163
Sagi Grimberg3fdf70a2015-06-25 13:34:15 +0300164 res = srp_parse_tmo(&tmo, val);
165 if (res)
166 goto out;
167
Bart Van Asschea95cadb2013-10-26 14:37:17 +0200168 if (kp->arg == &srp_reconnect_delay)
169 res = srp_tmo_valid(tmo, srp_fast_io_fail_tmo,
170 srp_dev_loss_tmo);
171 else if (kp->arg == &srp_fast_io_fail_tmo)
172 res = srp_tmo_valid(srp_reconnect_delay, tmo, srp_dev_loss_tmo);
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200173 else
Bart Van Asschea95cadb2013-10-26 14:37:17 +0200174 res = srp_tmo_valid(srp_reconnect_delay, srp_fast_io_fail_tmo,
175 tmo);
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200176 if (res)
177 goto out;
178 *(int *)kp->arg = tmo;
179
180out:
181 return res;
182}
183
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930184static const struct kernel_param_ops srp_tmo_ops = {
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200185 .get = srp_tmo_get,
186 .set = srp_tmo_set,
187};
188
Roland Dreieraef9ec32005-11-02 14:07:13 -0800189static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
190{
191 return (struct srp_target_port *) host->hostdata;
192}
193
194static const char *srp_target_info(struct Scsi_Host *host)
195{
196 return host_to_target(host)->target_name;
197}
198
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700199static int srp_target_is_topspin(struct srp_target_port *target)
200{
201 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700202 static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700203
204 return topspin_workarounds &&
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700205 (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
206 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700207}
208
Roland Dreieraef9ec32005-11-02 14:07:13 -0800209static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
210 gfp_t gfp_mask,
211 enum dma_data_direction direction)
212{
213 struct srp_iu *iu;
214
215 iu = kmalloc(sizeof *iu, gfp_mask);
216 if (!iu)
217 goto out;
218
219 iu->buf = kzalloc(size, gfp_mask);
220 if (!iu->buf)
221 goto out_free_iu;
222
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100223 iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
224 direction);
225 if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800226 goto out_free_buf;
227
228 iu->size = size;
229 iu->direction = direction;
230
231 return iu;
232
233out_free_buf:
234 kfree(iu->buf);
235out_free_iu:
236 kfree(iu);
237out:
238 return NULL;
239}
240
241static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
242{
243 if (!iu)
244 return;
245
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100246 ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
247 iu->direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800248 kfree(iu->buf);
249 kfree(iu);
250}
251
252static void srp_qp_event(struct ib_event *event, void *context)
253{
Sagi Grimberg57363d92015-05-18 13:40:29 +0300254 pr_debug("QP event %s (%d)\n",
255 ib_event_msg(event->event), event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800256}
257
258static int srp_init_qp(struct srp_target_port *target,
259 struct ib_qp *qp)
260{
261 struct ib_qp_attr *attr;
262 int ret;
263
264 attr = kmalloc(sizeof *attr, GFP_KERNEL);
265 if (!attr)
266 return -ENOMEM;
267
Bart Van Assche56b53902014-07-09 15:58:22 +0200268 ret = ib_find_cached_pkey(target->srp_host->srp_dev->dev,
269 target->srp_host->port,
270 be16_to_cpu(target->pkey),
271 &attr->pkey_index);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800272 if (ret)
273 goto out;
274
275 attr->qp_state = IB_QPS_INIT;
276 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
277 IB_ACCESS_REMOTE_WRITE);
278 attr->port_num = target->srp_host->port;
279
280 ret = ib_modify_qp(qp, attr,
281 IB_QP_STATE |
282 IB_QP_PKEY_INDEX |
283 IB_QP_ACCESS_FLAGS |
284 IB_QP_PORT);
285
286out:
287 kfree(attr);
288 return ret;
289}
290
Bart Van Assche509c07b2014-10-30 14:48:30 +0100291static int srp_new_cm_id(struct srp_rdma_ch *ch)
David Dillow9fe4bcf2008-01-08 17:08:52 -0500292{
Bart Van Assche509c07b2014-10-30 14:48:30 +0100293 struct srp_target_port *target = ch->target;
David Dillow9fe4bcf2008-01-08 17:08:52 -0500294 struct ib_cm_id *new_cm_id;
295
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100296 new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
Bart Van Assche509c07b2014-10-30 14:48:30 +0100297 srp_cm_handler, ch);
David Dillow9fe4bcf2008-01-08 17:08:52 -0500298 if (IS_ERR(new_cm_id))
299 return PTR_ERR(new_cm_id);
300
Bart Van Assche509c07b2014-10-30 14:48:30 +0100301 if (ch->cm_id)
302 ib_destroy_cm_id(ch->cm_id);
303 ch->cm_id = new_cm_id;
304 ch->path.sgid = target->sgid;
305 ch->path.dgid = target->orig_dgid;
306 ch->path.pkey = target->pkey;
307 ch->path.service_id = target->service_id;
David Dillow9fe4bcf2008-01-08 17:08:52 -0500308
309 return 0;
310}
311
Bart Van Assched1b42892014-05-20 15:07:20 +0200312static struct ib_fmr_pool *srp_alloc_fmr_pool(struct srp_target_port *target)
313{
314 struct srp_device *dev = target->srp_host->srp_dev;
315 struct ib_fmr_pool_param fmr_param;
316
317 memset(&fmr_param, 0, sizeof(fmr_param));
318 fmr_param.pool_size = target->scsi_host->can_queue;
319 fmr_param.dirty_watermark = fmr_param.pool_size / 4;
320 fmr_param.cache = 1;
Bart Van Assche52ede082014-05-20 15:07:45 +0200321 fmr_param.max_pages_per_fmr = dev->max_pages_per_mr;
322 fmr_param.page_shift = ilog2(dev->mr_page_size);
Bart Van Assched1b42892014-05-20 15:07:20 +0200323 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
324 IB_ACCESS_REMOTE_WRITE |
325 IB_ACCESS_REMOTE_READ);
326
327 return ib_create_fmr_pool(dev->pd, &fmr_param);
328}
329
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200330/**
331 * srp_destroy_fr_pool() - free the resources owned by a pool
332 * @pool: Fast registration pool to be destroyed.
333 */
334static void srp_destroy_fr_pool(struct srp_fr_pool *pool)
335{
336 int i;
337 struct srp_fr_desc *d;
338
339 if (!pool)
340 return;
341
342 for (i = 0, d = &pool->desc[0]; i < pool->size; i++, d++) {
343 if (d->frpl)
344 ib_free_fast_reg_page_list(d->frpl);
345 if (d->mr)
346 ib_dereg_mr(d->mr);
347 }
348 kfree(pool);
349}
350
351/**
352 * srp_create_fr_pool() - allocate and initialize a pool for fast registration
353 * @device: IB device to allocate fast registration descriptors for.
354 * @pd: Protection domain associated with the FR descriptors.
355 * @pool_size: Number of descriptors to allocate.
356 * @max_page_list_len: Maximum fast registration work request page list length.
357 */
358static struct srp_fr_pool *srp_create_fr_pool(struct ib_device *device,
359 struct ib_pd *pd, int pool_size,
360 int max_page_list_len)
361{
362 struct srp_fr_pool *pool;
363 struct srp_fr_desc *d;
364 struct ib_mr *mr;
365 struct ib_fast_reg_page_list *frpl;
366 int i, ret = -EINVAL;
367
368 if (pool_size <= 0)
369 goto err;
370 ret = -ENOMEM;
371 pool = kzalloc(sizeof(struct srp_fr_pool) +
372 pool_size * sizeof(struct srp_fr_desc), GFP_KERNEL);
373 if (!pool)
374 goto err;
375 pool->size = pool_size;
376 pool->max_page_list_len = max_page_list_len;
377 spin_lock_init(&pool->lock);
378 INIT_LIST_HEAD(&pool->free_list);
379
380 for (i = 0, d = &pool->desc[0]; i < pool->size; i++, d++) {
Sagi Grimberg563b67c2015-07-30 10:32:38 +0300381 mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
382 max_page_list_len);
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200383 if (IS_ERR(mr)) {
384 ret = PTR_ERR(mr);
385 goto destroy_pool;
386 }
387 d->mr = mr;
388 frpl = ib_alloc_fast_reg_page_list(device, max_page_list_len);
389 if (IS_ERR(frpl)) {
390 ret = PTR_ERR(frpl);
391 goto destroy_pool;
392 }
393 d->frpl = frpl;
394 list_add_tail(&d->entry, &pool->free_list);
395 }
396
397out:
398 return pool;
399
400destroy_pool:
401 srp_destroy_fr_pool(pool);
402
403err:
404 pool = ERR_PTR(ret);
405 goto out;
406}
407
408/**
409 * srp_fr_pool_get() - obtain a descriptor suitable for fast registration
410 * @pool: Pool to obtain descriptor from.
411 */
412static struct srp_fr_desc *srp_fr_pool_get(struct srp_fr_pool *pool)
413{
414 struct srp_fr_desc *d = NULL;
415 unsigned long flags;
416
417 spin_lock_irqsave(&pool->lock, flags);
418 if (!list_empty(&pool->free_list)) {
419 d = list_first_entry(&pool->free_list, typeof(*d), entry);
420 list_del(&d->entry);
421 }
422 spin_unlock_irqrestore(&pool->lock, flags);
423
424 return d;
425}
426
427/**
428 * srp_fr_pool_put() - put an FR descriptor back in the free list
429 * @pool: Pool the descriptor was allocated from.
430 * @desc: Pointer to an array of fast registration descriptor pointers.
431 * @n: Number of descriptors to put back.
432 *
433 * Note: The caller must already have queued an invalidation request for
434 * desc->mr->rkey before calling this function.
435 */
436static void srp_fr_pool_put(struct srp_fr_pool *pool, struct srp_fr_desc **desc,
437 int n)
438{
439 unsigned long flags;
440 int i;
441
442 spin_lock_irqsave(&pool->lock, flags);
443 for (i = 0; i < n; i++)
444 list_add(&desc[i]->entry, &pool->free_list);
445 spin_unlock_irqrestore(&pool->lock, flags);
446}
447
448static struct srp_fr_pool *srp_alloc_fr_pool(struct srp_target_port *target)
449{
450 struct srp_device *dev = target->srp_host->srp_dev;
451
452 return srp_create_fr_pool(dev->dev, dev->pd,
453 target->scsi_host->can_queue,
454 dev->max_pages_per_mr);
455}
456
Bart Van Assche7dad6b22014-10-21 18:00:35 +0200457/**
458 * srp_destroy_qp() - destroy an RDMA queue pair
459 * @ch: SRP RDMA channel.
460 *
461 * Change a queue pair into the error state and wait until all receive
462 * completions have been processed before destroying it. This avoids that
463 * the receive completion handler can access the queue pair while it is
464 * being destroyed.
465 */
466static void srp_destroy_qp(struct srp_rdma_ch *ch)
467{
Bart Van Assche7dad6b22014-10-21 18:00:35 +0200468 static struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
469 static struct ib_recv_wr wr = { .wr_id = SRP_LAST_WR_ID };
470 struct ib_recv_wr *bad_wr;
471 int ret;
472
473 /* Destroying a QP and reusing ch->done is only safe if not connected */
Bart Van Asschec014c8c2015-05-18 13:23:57 +0200474 WARN_ON_ONCE(ch->connected);
Bart Van Assche7dad6b22014-10-21 18:00:35 +0200475
476 ret = ib_modify_qp(ch->qp, &attr, IB_QP_STATE);
477 WARN_ONCE(ret, "ib_cm_init_qp_attr() returned %d\n", ret);
478 if (ret)
479 goto out;
480
481 init_completion(&ch->done);
482 ret = ib_post_recv(ch->qp, &wr, &bad_wr);
483 WARN_ONCE(ret, "ib_post_recv() returned %d\n", ret);
484 if (ret == 0)
485 wait_for_completion(&ch->done);
486
487out:
488 ib_destroy_qp(ch->qp);
489}
490
Bart Van Assche509c07b2014-10-30 14:48:30 +0100491static int srp_create_ch_ib(struct srp_rdma_ch *ch)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800492{
Bart Van Assche509c07b2014-10-30 14:48:30 +0100493 struct srp_target_port *target = ch->target;
Bart Van Assche62154b22014-05-20 15:04:45 +0200494 struct srp_device *dev = target->srp_host->srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800495 struct ib_qp_init_attr *init_attr;
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100496 struct ib_cq *recv_cq, *send_cq;
497 struct ib_qp *qp;
Bart Van Assched1b42892014-05-20 15:07:20 +0200498 struct ib_fmr_pool *fmr_pool = NULL;
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200499 struct srp_fr_pool *fr_pool = NULL;
500 const int m = 1 + dev->use_fast_reg;
Matan Barak8e372102015-06-11 16:35:21 +0300501 struct ib_cq_init_attr cq_attr = {};
Roland Dreieraef9ec32005-11-02 14:07:13 -0800502 int ret;
503
504 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
505 if (!init_attr)
506 return -ENOMEM;
507
Bart Van Assche7dad6b22014-10-21 18:00:35 +0200508 /* + 1 for SRP_LAST_WR_ID */
Matan Barak8e372102015-06-11 16:35:21 +0300509 cq_attr.cqe = target->queue_size + 1;
510 cq_attr.comp_vector = ch->comp_vector;
Bart Van Assche509c07b2014-10-30 14:48:30 +0100511 recv_cq = ib_create_cq(dev->dev, srp_recv_completion, NULL, ch,
Matan Barak8e372102015-06-11 16:35:21 +0300512 &cq_attr);
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100513 if (IS_ERR(recv_cq)) {
514 ret = PTR_ERR(recv_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800515 goto err;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800516 }
517
Matan Barak8e372102015-06-11 16:35:21 +0300518 cq_attr.cqe = m * target->queue_size;
519 cq_attr.comp_vector = ch->comp_vector;
Bart Van Assche509c07b2014-10-30 14:48:30 +0100520 send_cq = ib_create_cq(dev->dev, srp_send_completion, NULL, ch,
Matan Barak8e372102015-06-11 16:35:21 +0300521 &cq_attr);
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100522 if (IS_ERR(send_cq)) {
523 ret = PTR_ERR(send_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800524 goto err_recv_cq;
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000525 }
526
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100527 ib_req_notify_cq(recv_cq, IB_CQ_NEXT_COMP);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800528
529 init_attr->event_handler = srp_qp_event;
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200530 init_attr->cap.max_send_wr = m * target->queue_size;
Bart Van Assche7dad6b22014-10-21 18:00:35 +0200531 init_attr->cap.max_recv_wr = target->queue_size + 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800532 init_attr->cap.max_recv_sge = 1;
533 init_attr->cap.max_send_sge = 1;
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200534 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800535 init_attr->qp_type = IB_QPT_RC;
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100536 init_attr->send_cq = send_cq;
537 init_attr->recv_cq = recv_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800538
Bart Van Assche62154b22014-05-20 15:04:45 +0200539 qp = ib_create_qp(dev->pd, init_attr);
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100540 if (IS_ERR(qp)) {
541 ret = PTR_ERR(qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800542 goto err_send_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800543 }
544
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100545 ret = srp_init_qp(target, qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800546 if (ret)
547 goto err_qp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800548
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200549 if (dev->use_fast_reg && dev->has_fr) {
550 fr_pool = srp_alloc_fr_pool(target);
551 if (IS_ERR(fr_pool)) {
552 ret = PTR_ERR(fr_pool);
553 shost_printk(KERN_WARNING, target->scsi_host, PFX
554 "FR pool allocation failed (%d)\n", ret);
555 goto err_qp;
556 }
Bart Van Assche509c07b2014-10-30 14:48:30 +0100557 if (ch->fr_pool)
558 srp_destroy_fr_pool(ch->fr_pool);
559 ch->fr_pool = fr_pool;
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200560 } else if (!dev->use_fast_reg && dev->has_fmr) {
Bart Van Assched1b42892014-05-20 15:07:20 +0200561 fmr_pool = srp_alloc_fmr_pool(target);
562 if (IS_ERR(fmr_pool)) {
563 ret = PTR_ERR(fmr_pool);
564 shost_printk(KERN_WARNING, target->scsi_host, PFX
565 "FMR pool allocation failed (%d)\n", ret);
566 goto err_qp;
567 }
Bart Van Assche509c07b2014-10-30 14:48:30 +0100568 if (ch->fmr_pool)
569 ib_destroy_fmr_pool(ch->fmr_pool);
570 ch->fmr_pool = fmr_pool;
Bart Van Assched1b42892014-05-20 15:07:20 +0200571 }
572
Bart Van Assche509c07b2014-10-30 14:48:30 +0100573 if (ch->qp)
Bart Van Assche7dad6b22014-10-21 18:00:35 +0200574 srp_destroy_qp(ch);
Bart Van Assche509c07b2014-10-30 14:48:30 +0100575 if (ch->recv_cq)
576 ib_destroy_cq(ch->recv_cq);
577 if (ch->send_cq)
578 ib_destroy_cq(ch->send_cq);
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100579
Bart Van Assche509c07b2014-10-30 14:48:30 +0100580 ch->qp = qp;
581 ch->recv_cq = recv_cq;
582 ch->send_cq = send_cq;
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100583
Roland Dreierda9d2f02010-02-24 15:07:59 -0800584 kfree(init_attr);
585 return 0;
586
587err_qp:
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100588 ib_destroy_qp(qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800589
590err_send_cq:
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100591 ib_destroy_cq(send_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800592
593err_recv_cq:
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100594 ib_destroy_cq(recv_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800595
596err:
Roland Dreieraef9ec32005-11-02 14:07:13 -0800597 kfree(init_attr);
598 return ret;
599}
600
Bart Van Assche4d73f952013-10-26 14:40:37 +0200601/*
602 * Note: this function may be called without srp_alloc_iu_bufs() having been
Bart Van Assche509c07b2014-10-30 14:48:30 +0100603 * invoked. Hence the ch->[rt]x_ring checks.
Bart Van Assche4d73f952013-10-26 14:40:37 +0200604 */
Bart Van Assche509c07b2014-10-30 14:48:30 +0100605static void srp_free_ch_ib(struct srp_target_port *target,
606 struct srp_rdma_ch *ch)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800607{
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200608 struct srp_device *dev = target->srp_host->srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800609 int i;
610
Bart Van Assched92c0da2014-10-06 17:14:36 +0200611 if (!ch->target)
612 return;
613
Bart Van Assche509c07b2014-10-30 14:48:30 +0100614 if (ch->cm_id) {
615 ib_destroy_cm_id(ch->cm_id);
616 ch->cm_id = NULL;
Bart Van Assche394c5952014-10-30 14:46:27 +0100617 }
618
Bart Van Assched92c0da2014-10-06 17:14:36 +0200619 /* If srp_new_cm_id() succeeded but srp_create_ch_ib() not, return. */
620 if (!ch->qp)
621 return;
622
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200623 if (dev->use_fast_reg) {
Bart Van Assche509c07b2014-10-30 14:48:30 +0100624 if (ch->fr_pool)
625 srp_destroy_fr_pool(ch->fr_pool);
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200626 } else {
Bart Van Assche509c07b2014-10-30 14:48:30 +0100627 if (ch->fmr_pool)
628 ib_destroy_fmr_pool(ch->fmr_pool);
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200629 }
Bart Van Assche7dad6b22014-10-21 18:00:35 +0200630 srp_destroy_qp(ch);
Bart Van Assche509c07b2014-10-30 14:48:30 +0100631 ib_destroy_cq(ch->send_cq);
632 ib_destroy_cq(ch->recv_cq);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800633
Bart Van Assched92c0da2014-10-06 17:14:36 +0200634 /*
635 * Avoid that the SCSI error handler tries to use this channel after
636 * it has been freed. The SCSI error handler can namely continue
637 * trying to perform recovery actions after scsi_remove_host()
638 * returned.
639 */
640 ch->target = NULL;
641
Bart Van Assche509c07b2014-10-30 14:48:30 +0100642 ch->qp = NULL;
643 ch->send_cq = ch->recv_cq = NULL;
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100644
Bart Van Assche509c07b2014-10-30 14:48:30 +0100645 if (ch->rx_ring) {
Bart Van Assche4d73f952013-10-26 14:40:37 +0200646 for (i = 0; i < target->queue_size; ++i)
Bart Van Assche509c07b2014-10-30 14:48:30 +0100647 srp_free_iu(target->srp_host, ch->rx_ring[i]);
648 kfree(ch->rx_ring);
649 ch->rx_ring = NULL;
Bart Van Assche4d73f952013-10-26 14:40:37 +0200650 }
Bart Van Assche509c07b2014-10-30 14:48:30 +0100651 if (ch->tx_ring) {
Bart Van Assche4d73f952013-10-26 14:40:37 +0200652 for (i = 0; i < target->queue_size; ++i)
Bart Van Assche509c07b2014-10-30 14:48:30 +0100653 srp_free_iu(target->srp_host, ch->tx_ring[i]);
654 kfree(ch->tx_ring);
655 ch->tx_ring = NULL;
Bart Van Assche4d73f952013-10-26 14:40:37 +0200656 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800657}
658
659static void srp_path_rec_completion(int status,
660 struct ib_sa_path_rec *pathrec,
Bart Van Assche509c07b2014-10-30 14:48:30 +0100661 void *ch_ptr)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800662{
Bart Van Assche509c07b2014-10-30 14:48:30 +0100663 struct srp_rdma_ch *ch = ch_ptr;
664 struct srp_target_port *target = ch->target;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800665
Bart Van Assche509c07b2014-10-30 14:48:30 +0100666 ch->status = status;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800667 if (status)
David Dillow7aa54bd2008-01-07 18:23:41 -0500668 shost_printk(KERN_ERR, target->scsi_host,
669 PFX "Got failed path rec status %d\n", status);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800670 else
Bart Van Assche509c07b2014-10-30 14:48:30 +0100671 ch->path = *pathrec;
672 complete(&ch->done);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800673}
674
Bart Van Assche509c07b2014-10-30 14:48:30 +0100675static int srp_lookup_path(struct srp_rdma_ch *ch)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800676{
Bart Van Assche509c07b2014-10-30 14:48:30 +0100677 struct srp_target_port *target = ch->target;
Bart Van Asschea702adc2014-03-14 13:53:10 +0100678 int ret;
679
Bart Van Assche509c07b2014-10-30 14:48:30 +0100680 ch->path.numb_path = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800681
Bart Van Assche509c07b2014-10-30 14:48:30 +0100682 init_completion(&ch->done);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800683
Bart Van Assche509c07b2014-10-30 14:48:30 +0100684 ch->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
685 target->srp_host->srp_dev->dev,
686 target->srp_host->port,
687 &ch->path,
688 IB_SA_PATH_REC_SERVICE_ID |
689 IB_SA_PATH_REC_DGID |
690 IB_SA_PATH_REC_SGID |
691 IB_SA_PATH_REC_NUMB_PATH |
692 IB_SA_PATH_REC_PKEY,
693 SRP_PATH_REC_TIMEOUT_MS,
694 GFP_KERNEL,
695 srp_path_rec_completion,
696 ch, &ch->path_query);
697 if (ch->path_query_id < 0)
698 return ch->path_query_id;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800699
Bart Van Assche509c07b2014-10-30 14:48:30 +0100700 ret = wait_for_completion_interruptible(&ch->done);
Bart Van Asschea702adc2014-03-14 13:53:10 +0100701 if (ret < 0)
702 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800703
Bart Van Assche509c07b2014-10-30 14:48:30 +0100704 if (ch->status < 0)
David Dillow7aa54bd2008-01-07 18:23:41 -0500705 shost_printk(KERN_WARNING, target->scsi_host,
706 PFX "Path record query failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800707
Bart Van Assche509c07b2014-10-30 14:48:30 +0100708 return ch->status;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800709}
710
Bart Van Assched92c0da2014-10-06 17:14:36 +0200711static int srp_send_req(struct srp_rdma_ch *ch, bool multich)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800712{
Bart Van Assche509c07b2014-10-30 14:48:30 +0100713 struct srp_target_port *target = ch->target;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800714 struct {
715 struct ib_cm_req_param param;
716 struct srp_login_req priv;
717 } *req = NULL;
718 int status;
719
720 req = kzalloc(sizeof *req, GFP_KERNEL);
721 if (!req)
722 return -ENOMEM;
723
Bart Van Assche509c07b2014-10-30 14:48:30 +0100724 req->param.primary_path = &ch->path;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800725 req->param.alternate_path = NULL;
726 req->param.service_id = target->service_id;
Bart Van Assche509c07b2014-10-30 14:48:30 +0100727 req->param.qp_num = ch->qp->qp_num;
728 req->param.qp_type = ch->qp->qp_type;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800729 req->param.private_data = &req->priv;
730 req->param.private_data_len = sizeof req->priv;
731 req->param.flow_control = 1;
732
733 get_random_bytes(&req->param.starting_psn, 4);
734 req->param.starting_psn &= 0xffffff;
735
736 /*
737 * Pick some arbitrary defaults here; we could make these
738 * module parameters if anyone cared about setting them.
739 */
740 req->param.responder_resources = 4;
741 req->param.remote_cm_response_timeout = 20;
742 req->param.local_cm_response_timeout = 20;
Vu Pham7bb312e2013-10-26 14:31:27 +0200743 req->param.retry_count = target->tl_retry_count;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800744 req->param.rnr_retry_count = 7;
745 req->param.max_cm_retries = 15;
746
747 req->priv.opcode = SRP_LOGIN_REQ;
748 req->priv.tag = 0;
David Dillow49248642011-01-14 18:23:24 -0500749 req->priv.req_it_iu_len = cpu_to_be32(target->max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800750 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
751 SRP_BUF_FORMAT_INDIRECT);
Bart Van Assched92c0da2014-10-06 17:14:36 +0200752 req->priv.req_flags = (multich ? SRP_MULTICHAN_MULTI :
753 SRP_MULTICHAN_SINGLE);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700754 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700755 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450db2006-06-17 20:37:38 -0700756 * port identifier format is 8 bytes of ID extension followed
757 * by 8 bytes of GUID. Older drafts put the two halves in the
758 * opposite order, so that the GUID comes first.
759 *
760 * Targets conforming to these obsolete drafts can be
761 * recognized by the I/O Class they report.
762 */
763 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
764 memcpy(req->priv.initiator_port_id,
Bart Van Assche747fe002014-10-30 14:48:05 +0100765 &target->sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700766 memcpy(req->priv.initiator_port_id + 8,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200767 &target->initiator_ext, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700768 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
769 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
770 } else {
771 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200772 &target->initiator_ext, 8);
773 memcpy(req->priv.initiator_port_id + 8,
Bart Van Assche747fe002014-10-30 14:48:05 +0100774 &target->sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700775 memcpy(req->priv.target_port_id, &target->id_ext, 8);
776 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
777 }
778
Roland Dreieraef9ec32005-11-02 14:07:13 -0800779 /*
780 * Topspin/Cisco SRP targets will reject our login unless we
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200781 * zero out the first 8 bytes of our initiator port ID and set
782 * the second 8 bytes to the local node GUID.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800783 */
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700784 if (srp_target_is_topspin(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500785 shost_printk(KERN_DEBUG, target->scsi_host,
786 PFX "Topspin/Cisco initiator port ID workaround "
787 "activated for target GUID %016llx\n",
Bart Van Assche45c37ca2015-05-18 13:25:10 +0200788 be64_to_cpu(target->ioc_guid));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800789 memset(req->priv.initiator_port_id, 0, 8);
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200790 memcpy(req->priv.initiator_port_id + 8,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100791 &target->srp_host->srp_dev->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800792 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800793
Bart Van Assche509c07b2014-10-30 14:48:30 +0100794 status = ib_send_cm_req(ch->cm_id, &req->param);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800795
796 kfree(req);
797
798 return status;
799}
800
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000801static bool srp_queue_remove_work(struct srp_target_port *target)
802{
803 bool changed = false;
804
805 spin_lock_irq(&target->lock);
806 if (target->state != SRP_TARGET_REMOVED) {
807 target->state = SRP_TARGET_REMOVED;
808 changed = true;
809 }
810 spin_unlock_irq(&target->lock);
811
812 if (changed)
Bart Van Asschebcc05912014-07-09 15:57:26 +0200813 queue_work(srp_remove_wq, &target->remove_work);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000814
815 return changed;
816}
817
Roland Dreieraef9ec32005-11-02 14:07:13 -0800818static void srp_disconnect_target(struct srp_target_port *target)
819{
Bart Van Assched92c0da2014-10-06 17:14:36 +0200820 struct srp_rdma_ch *ch;
821 int i;
Bart Van Assche509c07b2014-10-30 14:48:30 +0100822
Bart Van Asschec014c8c2015-05-18 13:23:57 +0200823 /* XXX should send SRP_I_LOGOUT request */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800824
Bart Van Asschec014c8c2015-05-18 13:23:57 +0200825 for (i = 0; i < target->ch_count; i++) {
826 ch = &target->ch[i];
827 ch->connected = false;
828 if (ch->cm_id && ib_send_cm_dreq(ch->cm_id, NULL, 0)) {
829 shost_printk(KERN_DEBUG, target->scsi_host,
830 PFX "Sending CM DREQ failed\n");
Bart Van Assche294c8752011-12-25 12:18:12 +0000831 }
Roland Dreiere6581052006-05-17 09:13:21 -0700832 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800833}
834
Bart Van Assche509c07b2014-10-30 14:48:30 +0100835static void srp_free_req_data(struct srp_target_port *target,
836 struct srp_rdma_ch *ch)
David Dillow8f26c9f2011-01-14 19:45:50 -0500837{
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200838 struct srp_device *dev = target->srp_host->srp_dev;
839 struct ib_device *ibdev = dev->dev;
David Dillow8f26c9f2011-01-14 19:45:50 -0500840 struct srp_request *req;
841 int i;
842
Bart Van Assche47513cf2015-05-18 13:25:54 +0200843 if (!ch->req_ring)
Bart Van Assche4d73f952013-10-26 14:40:37 +0200844 return;
845
846 for (i = 0; i < target->req_ring_size; ++i) {
Bart Van Assche509c07b2014-10-30 14:48:30 +0100847 req = &ch->req_ring[i];
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200848 if (dev->use_fast_reg)
849 kfree(req->fr_list);
850 else
851 kfree(req->fmr_list);
David Dillow8f26c9f2011-01-14 19:45:50 -0500852 kfree(req->map_page);
David Dillowc07d4242011-01-16 13:57:10 -0500853 if (req->indirect_dma_addr) {
854 ib_dma_unmap_single(ibdev, req->indirect_dma_addr,
855 target->indirect_size,
856 DMA_TO_DEVICE);
857 }
858 kfree(req->indirect_desc);
David Dillow8f26c9f2011-01-14 19:45:50 -0500859 }
Bart Van Assche4d73f952013-10-26 14:40:37 +0200860
Bart Van Assche509c07b2014-10-30 14:48:30 +0100861 kfree(ch->req_ring);
862 ch->req_ring = NULL;
David Dillow8f26c9f2011-01-14 19:45:50 -0500863}
864
Bart Van Assche509c07b2014-10-30 14:48:30 +0100865static int srp_alloc_req_data(struct srp_rdma_ch *ch)
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200866{
Bart Van Assche509c07b2014-10-30 14:48:30 +0100867 struct srp_target_port *target = ch->target;
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200868 struct srp_device *srp_dev = target->srp_host->srp_dev;
869 struct ib_device *ibdev = srp_dev->dev;
870 struct srp_request *req;
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200871 void *mr_list;
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200872 dma_addr_t dma_addr;
873 int i, ret = -ENOMEM;
874
Bart Van Assche509c07b2014-10-30 14:48:30 +0100875 ch->req_ring = kcalloc(target->req_ring_size, sizeof(*ch->req_ring),
876 GFP_KERNEL);
877 if (!ch->req_ring)
Bart Van Assche4d73f952013-10-26 14:40:37 +0200878 goto out;
879
880 for (i = 0; i < target->req_ring_size; ++i) {
Bart Van Assche509c07b2014-10-30 14:48:30 +0100881 req = &ch->req_ring[i];
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200882 mr_list = kmalloc(target->cmd_sg_cnt * sizeof(void *),
883 GFP_KERNEL);
884 if (!mr_list)
885 goto out;
886 if (srp_dev->use_fast_reg)
887 req->fr_list = mr_list;
888 else
889 req->fmr_list = mr_list;
Bart Van Assche52ede082014-05-20 15:07:45 +0200890 req->map_page = kmalloc(srp_dev->max_pages_per_mr *
Bart Van Assched1b42892014-05-20 15:07:20 +0200891 sizeof(void *), GFP_KERNEL);
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200892 if (!req->map_page)
893 goto out;
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200894 req->indirect_desc = kmalloc(target->indirect_size, GFP_KERNEL);
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200895 if (!req->indirect_desc)
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200896 goto out;
897
898 dma_addr = ib_dma_map_single(ibdev, req->indirect_desc,
899 target->indirect_size,
900 DMA_TO_DEVICE);
901 if (ib_dma_mapping_error(ibdev, dma_addr))
902 goto out;
903
904 req->indirect_dma_addr = dma_addr;
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200905 }
906 ret = 0;
907
908out:
909 return ret;
910}
911
Bart Van Assche683b1592012-01-14 12:40:44 +0000912/**
913 * srp_del_scsi_host_attr() - Remove attributes defined in the host template.
914 * @shost: SCSI host whose attributes to remove from sysfs.
915 *
916 * Note: Any attributes defined in the host template and that did not exist
917 * before invocation of this function will be ignored.
918 */
919static void srp_del_scsi_host_attr(struct Scsi_Host *shost)
920{
921 struct device_attribute **attr;
922
923 for (attr = shost->hostt->shost_attrs; attr && *attr; ++attr)
924 device_remove_file(&shost->shost_dev, *attr);
925}
926
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000927static void srp_remove_target(struct srp_target_port *target)
928{
Bart Van Assched92c0da2014-10-06 17:14:36 +0200929 struct srp_rdma_ch *ch;
930 int i;
Bart Van Assche509c07b2014-10-30 14:48:30 +0100931
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000932 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
933
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000934 srp_del_scsi_host_attr(target->scsi_host);
Bart Van Assche9dd69a62013-10-26 14:32:30 +0200935 srp_rport_get(target->rport);
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000936 srp_remove_host(target->scsi_host);
937 scsi_remove_host(target->scsi_host);
Bart Van Assche93079162013-12-11 17:06:14 +0100938 srp_stop_rport_timers(target->rport);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000939 srp_disconnect_target(target);
Bart Van Assched92c0da2014-10-06 17:14:36 +0200940 for (i = 0; i < target->ch_count; i++) {
941 ch = &target->ch[i];
942 srp_free_ch_ib(target, ch);
943 }
Bart Van Asschec1120f82013-10-26 14:35:08 +0200944 cancel_work_sync(&target->tl_err_work);
Bart Van Assche9dd69a62013-10-26 14:32:30 +0200945 srp_rport_put(target->rport);
Bart Van Assched92c0da2014-10-06 17:14:36 +0200946 for (i = 0; i < target->ch_count; i++) {
947 ch = &target->ch[i];
948 srp_free_req_data(target, ch);
949 }
950 kfree(target->ch);
951 target->ch = NULL;
Vu Pham65d7dd22013-10-10 13:50:29 +0200952
953 spin_lock(&target->srp_host->target_lock);
954 list_del(&target->list);
955 spin_unlock(&target->srp_host->target_lock);
956
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000957 scsi_host_put(target->scsi_host);
958}
959
David Howellsc4028952006-11-22 14:57:56 +0000960static void srp_remove_work(struct work_struct *work)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800961{
David Howellsc4028952006-11-22 14:57:56 +0000962 struct srp_target_port *target =
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000963 container_of(work, struct srp_target_port, remove_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800964
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000965 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800966
Bart Van Assche96fc2482013-06-28 14:51:26 +0200967 srp_remove_target(target);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800968}
969
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +0200970static void srp_rport_delete(struct srp_rport *rport)
971{
972 struct srp_target_port *target = rport->lld_data;
973
974 srp_queue_remove_work(target);
975}
976
Bart Van Asschec014c8c2015-05-18 13:23:57 +0200977/**
978 * srp_connected_ch() - number of connected channels
979 * @target: SRP target port.
980 */
981static int srp_connected_ch(struct srp_target_port *target)
982{
983 int i, c = 0;
984
985 for (i = 0; i < target->ch_count; i++)
986 c += target->ch[i].connected;
987
988 return c;
989}
990
Bart Van Assched92c0da2014-10-06 17:14:36 +0200991static int srp_connect_ch(struct srp_rdma_ch *ch, bool multich)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800992{
Bart Van Assche509c07b2014-10-30 14:48:30 +0100993 struct srp_target_port *target = ch->target;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800994 int ret;
995
Bart Van Asschec014c8c2015-05-18 13:23:57 +0200996 WARN_ON_ONCE(!multich && srp_connected_ch(target) > 0);
Bart Van Assche294c8752011-12-25 12:18:12 +0000997
Bart Van Assche509c07b2014-10-30 14:48:30 +0100998 ret = srp_lookup_path(ch);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800999 if (ret)
1000 return ret;
1001
1002 while (1) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01001003 init_completion(&ch->done);
Bart Van Assched92c0da2014-10-06 17:14:36 +02001004 ret = srp_send_req(ch, multich);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001005 if (ret)
1006 return ret;
Bart Van Assche509c07b2014-10-30 14:48:30 +01001007 ret = wait_for_completion_interruptible(&ch->done);
Bart Van Asschea702adc2014-03-14 13:53:10 +01001008 if (ret < 0)
1009 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001010
1011 /*
1012 * The CM event handling code will set status to
1013 * SRP_PORT_REDIRECT if we get a port redirect REJ
1014 * back, or SRP_DLID_REDIRECT if we get a lid/qp
1015 * redirect REJ back.
1016 */
Bart Van Assche509c07b2014-10-30 14:48:30 +01001017 switch (ch->status) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001018 case 0:
Bart Van Asschec014c8c2015-05-18 13:23:57 +02001019 ch->connected = true;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001020 return 0;
1021
1022 case SRP_PORT_REDIRECT:
Bart Van Assche509c07b2014-10-30 14:48:30 +01001023 ret = srp_lookup_path(ch);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001024 if (ret)
1025 return ret;
1026 break;
1027
1028 case SRP_DLID_REDIRECT:
1029 break;
1030
David Dillow9fe4bcf2008-01-08 17:08:52 -05001031 case SRP_STALE_CONN:
David Dillow9fe4bcf2008-01-08 17:08:52 -05001032 shost_printk(KERN_ERR, target->scsi_host, PFX
Bart Van Assche205619f2014-10-30 14:46:55 +01001033 "giving up on stale connection\n");
Bart Van Assche509c07b2014-10-30 14:48:30 +01001034 ch->status = -ECONNRESET;
1035 return ch->status;
David Dillow9fe4bcf2008-01-08 17:08:52 -05001036
Roland Dreieraef9ec32005-11-02 14:07:13 -08001037 default:
Bart Van Assche509c07b2014-10-30 14:48:30 +01001038 return ch->status;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001039 }
1040 }
1041}
1042
Bart Van Assche509c07b2014-10-30 14:48:30 +01001043static int srp_inv_rkey(struct srp_rdma_ch *ch, u32 rkey)
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001044{
1045 struct ib_send_wr *bad_wr;
1046 struct ib_send_wr wr = {
1047 .opcode = IB_WR_LOCAL_INV,
1048 .wr_id = LOCAL_INV_WR_ID_MASK,
1049 .next = NULL,
1050 .num_sge = 0,
1051 .send_flags = 0,
1052 .ex.invalidate_rkey = rkey,
1053 };
1054
Bart Van Assche509c07b2014-10-30 14:48:30 +01001055 return ib_post_send(ch->qp, &wr, &bad_wr);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001056}
1057
Roland Dreierd945e1d2006-05-09 10:50:28 -07001058static void srp_unmap_data(struct scsi_cmnd *scmnd,
Bart Van Assche509c07b2014-10-30 14:48:30 +01001059 struct srp_rdma_ch *ch,
Roland Dreierd945e1d2006-05-09 10:50:28 -07001060 struct srp_request *req)
1061{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001062 struct srp_target_port *target = ch->target;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001063 struct srp_device *dev = target->srp_host->srp_dev;
1064 struct ib_device *ibdev = dev->dev;
1065 int i, res;
David Dillow8f26c9f2011-01-14 19:45:50 -05001066
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001067 if (!scsi_sglist(scmnd) ||
Roland Dreierd945e1d2006-05-09 10:50:28 -07001068 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
1069 scmnd->sc_data_direction != DMA_FROM_DEVICE))
1070 return;
1071
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001072 if (dev->use_fast_reg) {
1073 struct srp_fr_desc **pfr;
1074
1075 for (i = req->nmdesc, pfr = req->fr_list; i > 0; i--, pfr++) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01001076 res = srp_inv_rkey(ch, (*pfr)->mr->rkey);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001077 if (res < 0) {
1078 shost_printk(KERN_ERR, target->scsi_host, PFX
1079 "Queueing INV WR for rkey %#x failed (%d)\n",
1080 (*pfr)->mr->rkey, res);
1081 queue_work(system_long_wq,
1082 &target->tl_err_work);
1083 }
1084 }
1085 if (req->nmdesc)
Bart Van Assche509c07b2014-10-30 14:48:30 +01001086 srp_fr_pool_put(ch->fr_pool, req->fr_list,
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001087 req->nmdesc);
1088 } else {
1089 struct ib_pool_fmr **pfmr;
1090
1091 for (i = req->nmdesc, pfmr = req->fmr_list; i > 0; i--, pfmr++)
1092 ib_fmr_pool_unmap(*pfmr);
1093 }
Roland Dreierf5358a12006-06-17 20:37:29 -07001094
David Dillow8f26c9f2011-01-14 19:45:50 -05001095 ib_dma_unmap_sg(ibdev, scsi_sglist(scmnd), scsi_sg_count(scmnd),
1096 scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001097}
1098
Bart Van Assche22032992012-08-14 13:18:53 +00001099/**
1100 * srp_claim_req - Take ownership of the scmnd associated with a request.
Bart Van Assche509c07b2014-10-30 14:48:30 +01001101 * @ch: SRP RDMA channel.
Bart Van Assche22032992012-08-14 13:18:53 +00001102 * @req: SRP request.
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001103 * @sdev: If not NULL, only take ownership for this SCSI device.
Bart Van Assche22032992012-08-14 13:18:53 +00001104 * @scmnd: If NULL, take ownership of @req->scmnd. If not NULL, only take
1105 * ownership of @req->scmnd if it equals @scmnd.
1106 *
1107 * Return value:
1108 * Either NULL or a pointer to the SCSI command the caller became owner of.
1109 */
Bart Van Assche509c07b2014-10-30 14:48:30 +01001110static struct scsi_cmnd *srp_claim_req(struct srp_rdma_ch *ch,
Bart Van Assche22032992012-08-14 13:18:53 +00001111 struct srp_request *req,
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001112 struct scsi_device *sdev,
Bart Van Assche22032992012-08-14 13:18:53 +00001113 struct scsi_cmnd *scmnd)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001114{
Bart Van Assche94a91742010-11-26 14:50:09 -05001115 unsigned long flags;
1116
Bart Van Assche509c07b2014-10-30 14:48:30 +01001117 spin_lock_irqsave(&ch->lock, flags);
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001118 if (req->scmnd &&
1119 (!sdev || req->scmnd->device == sdev) &&
1120 (!scmnd || req->scmnd == scmnd)) {
Bart Van Assche22032992012-08-14 13:18:53 +00001121 scmnd = req->scmnd;
1122 req->scmnd = NULL;
Bart Van Assche22032992012-08-14 13:18:53 +00001123 } else {
1124 scmnd = NULL;
1125 }
Bart Van Assche509c07b2014-10-30 14:48:30 +01001126 spin_unlock_irqrestore(&ch->lock, flags);
Bart Van Assche22032992012-08-14 13:18:53 +00001127
1128 return scmnd;
1129}
1130
1131/**
1132 * srp_free_req() - Unmap data and add request to the free request list.
Bart Van Assche509c07b2014-10-30 14:48:30 +01001133 * @ch: SRP RDMA channel.
Bart Van Asscheaf246632014-05-20 15:04:21 +02001134 * @req: Request to be freed.
1135 * @scmnd: SCSI command associated with @req.
1136 * @req_lim_delta: Amount to be added to @target->req_lim.
Bart Van Assche22032992012-08-14 13:18:53 +00001137 */
Bart Van Assche509c07b2014-10-30 14:48:30 +01001138static void srp_free_req(struct srp_rdma_ch *ch, struct srp_request *req,
1139 struct scsi_cmnd *scmnd, s32 req_lim_delta)
Bart Van Assche22032992012-08-14 13:18:53 +00001140{
1141 unsigned long flags;
1142
Bart Van Assche509c07b2014-10-30 14:48:30 +01001143 srp_unmap_data(scmnd, ch, req);
Bart Van Assche22032992012-08-14 13:18:53 +00001144
Bart Van Assche509c07b2014-10-30 14:48:30 +01001145 spin_lock_irqsave(&ch->lock, flags);
1146 ch->req_lim += req_lim_delta;
Bart Van Assche509c07b2014-10-30 14:48:30 +01001147 spin_unlock_irqrestore(&ch->lock, flags);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001148}
1149
Bart Van Assche509c07b2014-10-30 14:48:30 +01001150static void srp_finish_req(struct srp_rdma_ch *ch, struct srp_request *req,
1151 struct scsi_device *sdev, int result)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001152{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001153 struct scsi_cmnd *scmnd = srp_claim_req(ch, req, sdev, NULL);
Bart Van Assche22032992012-08-14 13:18:53 +00001154
1155 if (scmnd) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01001156 srp_free_req(ch, req, scmnd, 0);
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001157 scmnd->result = result;
Bart Van Assche22032992012-08-14 13:18:53 +00001158 scmnd->scsi_done(scmnd);
Bart Van Assche22032992012-08-14 13:18:53 +00001159 }
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001160}
1161
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001162static void srp_terminate_io(struct srp_rport *rport)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001163{
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001164 struct srp_target_port *target = rport->lld_data;
Bart Van Assched92c0da2014-10-06 17:14:36 +02001165 struct srp_rdma_ch *ch;
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001166 struct Scsi_Host *shost = target->scsi_host;
1167 struct scsi_device *sdev;
Bart Van Assched92c0da2014-10-06 17:14:36 +02001168 int i, j;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001169
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001170 /*
1171 * Invoking srp_terminate_io() while srp_queuecommand() is running
1172 * is not safe. Hence the warning statement below.
1173 */
1174 shost_for_each_device(sdev, shost)
1175 WARN_ON_ONCE(sdev->request_queue->request_fn_active);
1176
Bart Van Assched92c0da2014-10-06 17:14:36 +02001177 for (i = 0; i < target->ch_count; i++) {
1178 ch = &target->ch[i];
Bart Van Assche509c07b2014-10-30 14:48:30 +01001179
Bart Van Assched92c0da2014-10-06 17:14:36 +02001180 for (j = 0; j < target->req_ring_size; ++j) {
1181 struct srp_request *req = &ch->req_ring[j];
1182
1183 srp_finish_req(ch, req, NULL,
1184 DID_TRANSPORT_FAILFAST << 16);
1185 }
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001186 }
1187}
1188
1189/*
1190 * It is up to the caller to ensure that srp_rport_reconnect() calls are
1191 * serialized and that no concurrent srp_queuecommand(), srp_abort(),
1192 * srp_reset_device() or srp_reset_host() calls will occur while this function
1193 * is in progress. One way to realize that is not to call this function
1194 * directly but to call srp_reconnect_rport() instead since that last function
1195 * serializes calls of this function via rport->mutex and also blocks
1196 * srp_queuecommand() calls before invoking this function.
1197 */
1198static int srp_rport_reconnect(struct srp_rport *rport)
1199{
1200 struct srp_target_port *target = rport->lld_data;
Bart Van Assched92c0da2014-10-06 17:14:36 +02001201 struct srp_rdma_ch *ch;
1202 int i, j, ret = 0;
1203 bool multich = false;
Bart Van Assche09be70a2012-03-17 17:18:54 +00001204
Roland Dreieraef9ec32005-11-02 14:07:13 -08001205 srp_disconnect_target(target);
Bart Van Assche34aa6542014-10-30 14:47:22 +01001206
1207 if (target->state == SRP_TARGET_SCANNING)
1208 return -ENODEV;
1209
Roland Dreieraef9ec32005-11-02 14:07:13 -08001210 /*
Bart Van Asschec7c4e7f2013-02-21 17:19:04 +00001211 * Now get a new local CM ID so that we avoid confusing the target in
1212 * case things are really fouled up. Doing so also ensures that all CM
1213 * callbacks will have finished before a new QP is allocated.
Roland Dreieraef9ec32005-11-02 14:07:13 -08001214 */
Bart Van Assched92c0da2014-10-06 17:14:36 +02001215 for (i = 0; i < target->ch_count; i++) {
1216 ch = &target->ch[i];
Bart Van Assched92c0da2014-10-06 17:14:36 +02001217 ret += srp_new_cm_id(ch);
Bart Van Assche536ae142010-11-26 13:58:27 -05001218 }
Bart Van Assched92c0da2014-10-06 17:14:36 +02001219 for (i = 0; i < target->ch_count; i++) {
1220 ch = &target->ch[i];
Bart Van Assched92c0da2014-10-06 17:14:36 +02001221 for (j = 0; j < target->req_ring_size; ++j) {
1222 struct srp_request *req = &ch->req_ring[j];
Roland Dreieraef9ec32005-11-02 14:07:13 -08001223
Bart Van Assched92c0da2014-10-06 17:14:36 +02001224 srp_finish_req(ch, req, NULL, DID_RESET << 16);
1225 }
1226 }
1227 for (i = 0; i < target->ch_count; i++) {
1228 ch = &target->ch[i];
Bart Van Assched92c0da2014-10-06 17:14:36 +02001229 /*
1230 * Whether or not creating a new CM ID succeeded, create a new
1231 * QP. This guarantees that all completion callback function
1232 * invocations have finished before request resetting starts.
1233 */
1234 ret += srp_create_ch_ib(ch);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001235
Bart Van Assched92c0da2014-10-06 17:14:36 +02001236 INIT_LIST_HEAD(&ch->free_tx);
1237 for (j = 0; j < target->queue_size; ++j)
1238 list_add(&ch->tx_ring[j]->list, &ch->free_tx);
1239 }
Bart Van Assche8de9fe32015-05-18 13:23:36 +02001240
1241 target->qp_in_error = false;
1242
Bart Van Assched92c0da2014-10-06 17:14:36 +02001243 for (i = 0; i < target->ch_count; i++) {
1244 ch = &target->ch[i];
Bart Van Asschebbac5cc2015-05-18 13:26:17 +02001245 if (ret)
Bart Van Assched92c0da2014-10-06 17:14:36 +02001246 break;
Bart Van Assched92c0da2014-10-06 17:14:36 +02001247 ret = srp_connect_ch(ch, multich);
1248 multich = true;
1249 }
Bart Van Assche09be70a2012-03-17 17:18:54 +00001250
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001251 if (ret == 0)
1252 shost_printk(KERN_INFO, target->scsi_host,
1253 PFX "reconnect succeeded\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001254
1255 return ret;
1256}
1257
David Dillow8f26c9f2011-01-14 19:45:50 -05001258static void srp_map_desc(struct srp_map_state *state, dma_addr_t dma_addr,
1259 unsigned int dma_len, u32 rkey)
Roland Dreierf5358a12006-06-17 20:37:29 -07001260{
David Dillow8f26c9f2011-01-14 19:45:50 -05001261 struct srp_direct_buf *desc = state->desc;
1262
1263 desc->va = cpu_to_be64(dma_addr);
1264 desc->key = cpu_to_be32(rkey);
1265 desc->len = cpu_to_be32(dma_len);
1266
1267 state->total_len += dma_len;
1268 state->desc++;
1269 state->ndesc++;
1270}
1271
1272static int srp_map_finish_fmr(struct srp_map_state *state,
Bart Van Assche509c07b2014-10-30 14:48:30 +01001273 struct srp_rdma_ch *ch)
David Dillow8f26c9f2011-01-14 19:45:50 -05001274{
Bart Van Assche186fbc62015-08-10 17:06:29 -07001275 struct srp_target_port *target = ch->target;
1276 struct srp_device *dev = target->srp_host->srp_dev;
David Dillow8f26c9f2011-01-14 19:45:50 -05001277 struct ib_pool_fmr *fmr;
Roland Dreierf5358a12006-06-17 20:37:29 -07001278 u64 io_addr = 0;
David Dillow8f26c9f2011-01-14 19:45:50 -05001279
Bart Van Asschef731ed62015-08-10 17:07:27 -07001280 if (state->fmr.next >= state->fmr.end)
1281 return -ENOMEM;
1282
Bart Van Assche509c07b2014-10-30 14:48:30 +01001283 fmr = ib_fmr_pool_map_phys(ch->fmr_pool, state->pages,
David Dillow8f26c9f2011-01-14 19:45:50 -05001284 state->npages, io_addr);
1285 if (IS_ERR(fmr))
1286 return PTR_ERR(fmr);
1287
Bart Van Asschef731ed62015-08-10 17:07:27 -07001288 *state->fmr.next++ = fmr;
Bart Van Assche52ede082014-05-20 15:07:45 +02001289 state->nmdesc++;
David Dillow8f26c9f2011-01-14 19:45:50 -05001290
Bart Van Assche186fbc62015-08-10 17:06:29 -07001291 srp_map_desc(state, state->base_dma_addr & ~dev->mr_page_mask,
1292 state->dma_len, fmr->fmr->rkey);
Bart Van Assche539dde62014-05-20 15:05:46 +02001293
David Dillow8f26c9f2011-01-14 19:45:50 -05001294 return 0;
1295}
1296
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001297static int srp_map_finish_fr(struct srp_map_state *state,
Bart Van Assche509c07b2014-10-30 14:48:30 +01001298 struct srp_rdma_ch *ch)
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001299{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001300 struct srp_target_port *target = ch->target;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001301 struct srp_device *dev = target->srp_host->srp_dev;
1302 struct ib_send_wr *bad_wr;
1303 struct ib_send_wr wr;
1304 struct srp_fr_desc *desc;
1305 u32 rkey;
1306
Bart Van Asschef731ed62015-08-10 17:07:27 -07001307 if (state->fr.next >= state->fr.end)
1308 return -ENOMEM;
1309
Bart Van Assche509c07b2014-10-30 14:48:30 +01001310 desc = srp_fr_pool_get(ch->fr_pool);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001311 if (!desc)
1312 return -ENOMEM;
1313
1314 rkey = ib_inc_rkey(desc->mr->rkey);
1315 ib_update_fast_reg_key(desc->mr, rkey);
1316
1317 memcpy(desc->frpl->page_list, state->pages,
1318 sizeof(state->pages[0]) * state->npages);
1319
1320 memset(&wr, 0, sizeof(wr));
1321 wr.opcode = IB_WR_FAST_REG_MR;
1322 wr.wr_id = FAST_REG_WR_ID_MASK;
1323 wr.wr.fast_reg.iova_start = state->base_dma_addr;
1324 wr.wr.fast_reg.page_list = desc->frpl;
1325 wr.wr.fast_reg.page_list_len = state->npages;
1326 wr.wr.fast_reg.page_shift = ilog2(dev->mr_page_size);
1327 wr.wr.fast_reg.length = state->dma_len;
1328 wr.wr.fast_reg.access_flags = (IB_ACCESS_LOCAL_WRITE |
1329 IB_ACCESS_REMOTE_READ |
1330 IB_ACCESS_REMOTE_WRITE);
1331 wr.wr.fast_reg.rkey = desc->mr->lkey;
1332
Bart Van Asschef731ed62015-08-10 17:07:27 -07001333 *state->fr.next++ = desc;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001334 state->nmdesc++;
1335
1336 srp_map_desc(state, state->base_dma_addr, state->dma_len,
1337 desc->mr->rkey);
1338
Bart Van Assche509c07b2014-10-30 14:48:30 +01001339 return ib_post_send(ch->qp, &wr, &bad_wr);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001340}
1341
Bart Van Assche539dde62014-05-20 15:05:46 +02001342static int srp_finish_mapping(struct srp_map_state *state,
Bart Van Assche509c07b2014-10-30 14:48:30 +01001343 struct srp_rdma_ch *ch)
Bart Van Assche539dde62014-05-20 15:05:46 +02001344{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001345 struct srp_target_port *target = ch->target;
Bart Van Assche539dde62014-05-20 15:05:46 +02001346 int ret = 0;
1347
1348 if (state->npages == 0)
1349 return 0;
1350
Bart Van Asscheb1b88542014-05-20 15:06:41 +02001351 if (state->npages == 1 && !register_always)
Bart Van Assche52ede082014-05-20 15:07:45 +02001352 srp_map_desc(state, state->base_dma_addr, state->dma_len,
Bart Van Assche539dde62014-05-20 15:05:46 +02001353 target->rkey);
1354 else
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001355 ret = target->srp_host->srp_dev->use_fast_reg ?
Bart Van Assche509c07b2014-10-30 14:48:30 +01001356 srp_map_finish_fr(state, ch) :
1357 srp_map_finish_fmr(state, ch);
Bart Van Assche539dde62014-05-20 15:05:46 +02001358
1359 if (ret == 0) {
1360 state->npages = 0;
Bart Van Assche52ede082014-05-20 15:07:45 +02001361 state->dma_len = 0;
Bart Van Assche539dde62014-05-20 15:05:46 +02001362 }
1363
1364 return ret;
1365}
1366
David Dillow8f26c9f2011-01-14 19:45:50 -05001367static int srp_map_sg_entry(struct srp_map_state *state,
Bart Van Assche509c07b2014-10-30 14:48:30 +01001368 struct srp_rdma_ch *ch,
David Dillow8f26c9f2011-01-14 19:45:50 -05001369 struct scatterlist *sg, int sg_index,
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001370 bool use_mr)
David Dillow8f26c9f2011-01-14 19:45:50 -05001371{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001372 struct srp_target_port *target = ch->target;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001373 struct srp_device *dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001374 struct ib_device *ibdev = dev->dev;
David Dillow8f26c9f2011-01-14 19:45:50 -05001375 dma_addr_t dma_addr = ib_sg_dma_address(ibdev, sg);
1376 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
1377 unsigned int len;
1378 int ret;
Roland Dreierf5358a12006-06-17 20:37:29 -07001379
David Dillow8f26c9f2011-01-14 19:45:50 -05001380 if (!dma_len)
1381 return 0;
Roland Dreierf5358a12006-06-17 20:37:29 -07001382
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001383 if (!use_mr) {
1384 /*
1385 * Once we're in direct map mode for a request, we don't
1386 * go back to FMR or FR mode, so no need to update anything
David Dillow8f26c9f2011-01-14 19:45:50 -05001387 * other than the descriptor.
1388 */
1389 srp_map_desc(state, dma_addr, dma_len, target->rkey);
1390 return 0;
1391 }
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -07001392
David Dillow8f26c9f2011-01-14 19:45:50 -05001393 while (dma_len) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001394 unsigned offset = dma_addr & ~dev->mr_page_mask;
1395 if (state->npages == dev->max_pages_per_mr || offset != 0) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01001396 ret = srp_finish_mapping(state, ch);
David Dillow8f26c9f2011-01-14 19:45:50 -05001397 if (ret)
1398 return ret;
Roland Dreierf5358a12006-06-17 20:37:29 -07001399 }
1400
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001401 len = min_t(unsigned int, dma_len, dev->mr_page_size - offset);
David Dillow8f26c9f2011-01-14 19:45:50 -05001402
1403 if (!state->npages)
1404 state->base_dma_addr = dma_addr;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001405 state->pages[state->npages++] = dma_addr & dev->mr_page_mask;
Bart Van Assche52ede082014-05-20 15:07:45 +02001406 state->dma_len += len;
David Dillow8f26c9f2011-01-14 19:45:50 -05001407 dma_addr += len;
1408 dma_len -= len;
Roland Dreierf5358a12006-06-17 20:37:29 -07001409 }
1410
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001411 /*
1412 * If the last entry of the MR wasn't a full page, then we need to
David Dillow8f26c9f2011-01-14 19:45:50 -05001413 * close it out and start a new one -- we can only merge at page
1414 * boundries.
1415 */
Roland Dreierf5358a12006-06-17 20:37:29 -07001416 ret = 0;
Bart Van Assche0e0d3a42015-08-10 17:07:46 -07001417 if (len != dev->mr_page_size)
Bart Van Assche509c07b2014-10-30 14:48:30 +01001418 ret = srp_finish_mapping(state, ch);
Roland Dreierf5358a12006-06-17 20:37:29 -07001419 return ret;
1420}
1421
Bart Van Assche509c07b2014-10-30 14:48:30 +01001422static int srp_map_sg(struct srp_map_state *state, struct srp_rdma_ch *ch,
1423 struct srp_request *req, struct scatterlist *scat,
1424 int count)
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001425{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001426 struct srp_target_port *target = ch->target;
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001427 struct srp_device *dev = target->srp_host->srp_dev;
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001428 struct scatterlist *sg;
Bart Van Assche0e0d3a42015-08-10 17:07:46 -07001429 int i, ret;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001430 bool use_mr;
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001431
1432 state->desc = req->indirect_desc;
1433 state->pages = req->map_page;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001434 if (dev->use_fast_reg) {
Bart Van Asschef731ed62015-08-10 17:07:27 -07001435 state->fr.next = req->fr_list;
1436 state->fr.end = req->fr_list + target->cmd_sg_cnt;
Bart Van Assche509c07b2014-10-30 14:48:30 +01001437 use_mr = !!ch->fr_pool;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001438 } else {
Bart Van Asschef731ed62015-08-10 17:07:27 -07001439 state->fmr.next = req->fmr_list;
1440 state->fmr.end = req->fmr_list + target->cmd_sg_cnt;
Bart Van Assche509c07b2014-10-30 14:48:30 +01001441 use_mr = !!ch->fmr_pool;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001442 }
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001443
1444 for_each_sg(scat, sg, count, i) {
Bart Van Assche0e0d3a42015-08-10 17:07:46 -07001445 ret = srp_map_sg_entry(state, ch, sg, i, use_mr);
1446 if (ret)
1447 goto out;
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001448 }
1449
Bart Van Assche0e0d3a42015-08-10 17:07:46 -07001450 if (use_mr) {
1451 ret = srp_finish_mapping(state, ch);
1452 if (ret)
1453 goto out;
1454 }
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001455
Bart Van Assche52ede082014-05-20 15:07:45 +02001456 req->nmdesc = state->nmdesc;
Bart Van Assche0e0d3a42015-08-10 17:07:46 -07001457 ret = 0;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001458
Bart Van Assche0e0d3a42015-08-10 17:07:46 -07001459out:
1460 return ret;
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001461}
1462
Bart Van Assche509c07b2014-10-30 14:48:30 +01001463static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_rdma_ch *ch,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001464 struct srp_request *req)
1465{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001466 struct srp_target_port *target = ch->target;
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001467 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001468 struct srp_cmd *cmd = req->cmd->buf;
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001469 int len, nents, count;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001470 struct srp_device *dev;
1471 struct ib_device *ibdev;
David Dillow8f26c9f2011-01-14 19:45:50 -05001472 struct srp_map_state state;
1473 struct srp_indirect_buf *indirect_hdr;
David Dillow8f26c9f2011-01-14 19:45:50 -05001474 u32 table_len;
1475 u8 fmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001476
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001477 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001478 return sizeof (struct srp_cmd);
1479
1480 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
1481 scmnd->sc_data_direction != DMA_TO_DEVICE) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001482 shost_printk(KERN_WARNING, target->scsi_host,
1483 PFX "Unhandled data direction %d\n",
1484 scmnd->sc_data_direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001485 return -EINVAL;
1486 }
1487
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001488 nents = scsi_sg_count(scmnd);
1489 scat = scsi_sglist(scmnd);
Roland Dreiercf368712006-03-24 15:47:26 -08001490
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001491 dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001492 ibdev = dev->dev;
1493
1494 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
David Dillow8f26c9f2011-01-14 19:45:50 -05001495 if (unlikely(count == 0))
1496 return -EIO;
Roland Dreierf5358a12006-06-17 20:37:29 -07001497
1498 fmt = SRP_DATA_DESC_DIRECT;
1499 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -08001500
Bart Van Asscheb1b88542014-05-20 15:06:41 +02001501 if (count == 1 && !register_always) {
Roland Dreierf5358a12006-06-17 20:37:29 -07001502 /*
1503 * The midlayer only generated a single gather/scatter
1504 * entry, or DMA mapping coalesced everything to a
1505 * single entry. So a direct descriptor along with
1506 * the DMA MR suffices.
1507 */
Roland Dreieraef9ec32005-11-02 14:07:13 -08001508 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001509
Ralph Campbell85507bc2006-12-12 14:30:55 -08001510 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
David Dillow9af76272010-11-26 15:34:46 -05001511 buf->key = cpu_to_be32(target->rkey);
Ralph Campbell85507bc2006-12-12 14:30:55 -08001512 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Roland Dreiercf368712006-03-24 15:47:26 -08001513
Bart Van Assche52ede082014-05-20 15:07:45 +02001514 req->nmdesc = 0;
David Dillow8f26c9f2011-01-14 19:45:50 -05001515 goto map_complete;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001516 }
1517
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001518 /*
1519 * We have more than one scatter/gather entry, so build our indirect
1520 * descriptor table, trying to merge as many entries as we can.
David Dillow8f26c9f2011-01-14 19:45:50 -05001521 */
1522 indirect_hdr = (void *) cmd->add_data;
1523
David Dillowc07d4242011-01-16 13:57:10 -05001524 ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr,
1525 target->indirect_size, DMA_TO_DEVICE);
1526
David Dillow8f26c9f2011-01-14 19:45:50 -05001527 memset(&state, 0, sizeof(state));
Bart Van Assche509c07b2014-10-30 14:48:30 +01001528 srp_map_sg(&state, ch, req, scat, count);
David Dillow8f26c9f2011-01-14 19:45:50 -05001529
David Dillowc07d4242011-01-16 13:57:10 -05001530 /* We've mapped the request, now pull as much of the indirect
1531 * descriptor table as we can into the command buffer. If this
1532 * target is not using an external indirect table, we are
1533 * guaranteed to fit into the command, as the SCSI layer won't
1534 * give us more S/G entries than we allow.
David Dillow8f26c9f2011-01-14 19:45:50 -05001535 */
David Dillow8f26c9f2011-01-14 19:45:50 -05001536 if (state.ndesc == 1) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001537 /*
1538 * Memory registration collapsed the sg-list into one entry,
David Dillow8f26c9f2011-01-14 19:45:50 -05001539 * so use a direct descriptor.
1540 */
1541 struct srp_direct_buf *buf = (void *) cmd->add_data;
1542
David Dillowc07d4242011-01-16 13:57:10 -05001543 *buf = req->indirect_desc[0];
David Dillow8f26c9f2011-01-14 19:45:50 -05001544 goto map_complete;
1545 }
1546
David Dillowc07d4242011-01-16 13:57:10 -05001547 if (unlikely(target->cmd_sg_cnt < state.ndesc &&
1548 !target->allow_ext_sg)) {
1549 shost_printk(KERN_ERR, target->scsi_host,
1550 "Could not fit S/G list into SRP_CMD\n");
1551 return -EIO;
1552 }
1553
1554 count = min(state.ndesc, target->cmd_sg_cnt);
David Dillow8f26c9f2011-01-14 19:45:50 -05001555 table_len = state.ndesc * sizeof (struct srp_direct_buf);
1556
1557 fmt = SRP_DATA_DESC_INDIRECT;
1558 len = sizeof(struct srp_cmd) + sizeof (struct srp_indirect_buf);
David Dillowc07d4242011-01-16 13:57:10 -05001559 len += count * sizeof (struct srp_direct_buf);
David Dillow8f26c9f2011-01-14 19:45:50 -05001560
David Dillowc07d4242011-01-16 13:57:10 -05001561 memcpy(indirect_hdr->desc_list, req->indirect_desc,
1562 count * sizeof (struct srp_direct_buf));
David Dillow8f26c9f2011-01-14 19:45:50 -05001563
David Dillowc07d4242011-01-16 13:57:10 -05001564 indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr);
David Dillow8f26c9f2011-01-14 19:45:50 -05001565 indirect_hdr->table_desc.key = cpu_to_be32(target->rkey);
1566 indirect_hdr->table_desc.len = cpu_to_be32(table_len);
1567 indirect_hdr->len = cpu_to_be32(state.total_len);
1568
1569 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
David Dillowc07d4242011-01-16 13:57:10 -05001570 cmd->data_out_desc_cnt = count;
David Dillow8f26c9f2011-01-14 19:45:50 -05001571 else
David Dillowc07d4242011-01-16 13:57:10 -05001572 cmd->data_in_desc_cnt = count;
1573
1574 ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len,
1575 DMA_TO_DEVICE);
David Dillow8f26c9f2011-01-14 19:45:50 -05001576
1577map_complete:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001578 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
1579 cmd->buf_fmt = fmt << 4;
1580 else
1581 cmd->buf_fmt = fmt;
1582
Roland Dreieraef9ec32005-11-02 14:07:13 -08001583 return len;
1584}
1585
David Dillow05a1d752010-10-08 14:48:14 -04001586/*
Bart Van Assche76c75b22010-11-26 14:37:47 -05001587 * Return an IU and possible credit to the free pool
1588 */
Bart Van Assche509c07b2014-10-30 14:48:30 +01001589static void srp_put_tx_iu(struct srp_rdma_ch *ch, struct srp_iu *iu,
Bart Van Assche76c75b22010-11-26 14:37:47 -05001590 enum srp_iu_type iu_type)
1591{
1592 unsigned long flags;
1593
Bart Van Assche509c07b2014-10-30 14:48:30 +01001594 spin_lock_irqsave(&ch->lock, flags);
1595 list_add(&iu->list, &ch->free_tx);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001596 if (iu_type != SRP_IU_RSP)
Bart Van Assche509c07b2014-10-30 14:48:30 +01001597 ++ch->req_lim;
1598 spin_unlock_irqrestore(&ch->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001599}
1600
1601/*
Bart Van Assche509c07b2014-10-30 14:48:30 +01001602 * Must be called with ch->lock held to protect req_lim and free_tx.
Bart Van Asschee9684672010-11-26 15:08:38 -05001603 * If IU is not sent, it must be returned using srp_put_tx_iu().
David Dillow05a1d752010-10-08 14:48:14 -04001604 *
1605 * Note:
1606 * An upper limit for the number of allocated information units for each
1607 * request type is:
1608 * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues
1609 * more than Scsi_Host.can_queue requests.
1610 * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE.
1611 * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than
1612 * one unanswered SRP request to an initiator.
1613 */
Bart Van Assche509c07b2014-10-30 14:48:30 +01001614static struct srp_iu *__srp_get_tx_iu(struct srp_rdma_ch *ch,
David Dillow05a1d752010-10-08 14:48:14 -04001615 enum srp_iu_type iu_type)
1616{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001617 struct srp_target_port *target = ch->target;
David Dillow05a1d752010-10-08 14:48:14 -04001618 s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE;
1619 struct srp_iu *iu;
1620
Bart Van Assche509c07b2014-10-30 14:48:30 +01001621 srp_send_completion(ch->send_cq, ch);
David Dillow05a1d752010-10-08 14:48:14 -04001622
Bart Van Assche509c07b2014-10-30 14:48:30 +01001623 if (list_empty(&ch->free_tx))
David Dillow05a1d752010-10-08 14:48:14 -04001624 return NULL;
1625
1626 /* Initiator responses to target requests do not consume credits */
Bart Van Assche76c75b22010-11-26 14:37:47 -05001627 if (iu_type != SRP_IU_RSP) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01001628 if (ch->req_lim <= rsv) {
Bart Van Assche76c75b22010-11-26 14:37:47 -05001629 ++target->zero_req_lim;
1630 return NULL;
1631 }
1632
Bart Van Assche509c07b2014-10-30 14:48:30 +01001633 --ch->req_lim;
David Dillow05a1d752010-10-08 14:48:14 -04001634 }
1635
Bart Van Assche509c07b2014-10-30 14:48:30 +01001636 iu = list_first_entry(&ch->free_tx, struct srp_iu, list);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001637 list_del(&iu->list);
David Dillow05a1d752010-10-08 14:48:14 -04001638 return iu;
1639}
1640
Bart Van Assche509c07b2014-10-30 14:48:30 +01001641static int srp_post_send(struct srp_rdma_ch *ch, struct srp_iu *iu, int len)
David Dillow05a1d752010-10-08 14:48:14 -04001642{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001643 struct srp_target_port *target = ch->target;
David Dillow05a1d752010-10-08 14:48:14 -04001644 struct ib_sge list;
1645 struct ib_send_wr wr, *bad_wr;
David Dillow05a1d752010-10-08 14:48:14 -04001646
1647 list.addr = iu->dma;
1648 list.length = len;
David Dillow9af76272010-11-26 15:34:46 -05001649 list.lkey = target->lkey;
David Dillow05a1d752010-10-08 14:48:14 -04001650
1651 wr.next = NULL;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001652 wr.wr_id = (uintptr_t) iu;
David Dillow05a1d752010-10-08 14:48:14 -04001653 wr.sg_list = &list;
1654 wr.num_sge = 1;
1655 wr.opcode = IB_WR_SEND;
1656 wr.send_flags = IB_SEND_SIGNALED;
1657
Bart Van Assche509c07b2014-10-30 14:48:30 +01001658 return ib_post_send(ch->qp, &wr, &bad_wr);
David Dillow05a1d752010-10-08 14:48:14 -04001659}
1660
Bart Van Assche509c07b2014-10-30 14:48:30 +01001661static int srp_post_recv(struct srp_rdma_ch *ch, struct srp_iu *iu)
Bart Van Asschec996bb42010-07-30 10:59:05 +00001662{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001663 struct srp_target_port *target = ch->target;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001664 struct ib_recv_wr wr, *bad_wr;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001665 struct ib_sge list;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001666
1667 list.addr = iu->dma;
1668 list.length = iu->size;
David Dillow9af76272010-11-26 15:34:46 -05001669 list.lkey = target->lkey;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001670
1671 wr.next = NULL;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001672 wr.wr_id = (uintptr_t) iu;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001673 wr.sg_list = &list;
1674 wr.num_sge = 1;
1675
Bart Van Assche509c07b2014-10-30 14:48:30 +01001676 return ib_post_recv(ch->qp, &wr, &bad_wr);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001677}
1678
Bart Van Assche509c07b2014-10-30 14:48:30 +01001679static void srp_process_rsp(struct srp_rdma_ch *ch, struct srp_rsp *rsp)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001680{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001681 struct srp_target_port *target = ch->target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001682 struct srp_request *req;
1683 struct scsi_cmnd *scmnd;
1684 unsigned long flags;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001685
Roland Dreieraef9ec32005-11-02 14:07:13 -08001686 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01001687 spin_lock_irqsave(&ch->lock, flags);
1688 ch->req_lim += be32_to_cpu(rsp->req_lim_delta);
1689 spin_unlock_irqrestore(&ch->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -05001690
Bart Van Assche509c07b2014-10-30 14:48:30 +01001691 ch->tsk_mgmt_status = -1;
David Dillowf8b6e312010-11-26 13:02:21 -05001692 if (be32_to_cpu(rsp->resp_data_len) >= 4)
Bart Van Assche509c07b2014-10-30 14:48:30 +01001693 ch->tsk_mgmt_status = rsp->data[3];
1694 complete(&ch->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001695 } else {
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02001696 scmnd = scsi_host_find_tag(target->scsi_host, rsp->tag);
1697 if (scmnd) {
1698 req = (void *)scmnd->host_scribble;
1699 scmnd = srp_claim_req(ch, req, NULL, scmnd);
1700 }
Bart Van Assche22032992012-08-14 13:18:53 +00001701 if (!scmnd) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001702 shost_printk(KERN_ERR, target->scsi_host,
Bart Van Assched92c0da2014-10-06 17:14:36 +02001703 "Null scmnd for RSP w/tag %#016llx received on ch %td / QP %#x\n",
1704 rsp->tag, ch - target->ch, ch->qp->qp_num);
Bart Van Assche22032992012-08-14 13:18:53 +00001705
Bart Van Assche509c07b2014-10-30 14:48:30 +01001706 spin_lock_irqsave(&ch->lock, flags);
1707 ch->req_lim += be32_to_cpu(rsp->req_lim_delta);
1708 spin_unlock_irqrestore(&ch->lock, flags);
Bart Van Assche22032992012-08-14 13:18:53 +00001709
1710 return;
1711 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001712 scmnd->result = rsp->status;
1713
1714 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
1715 memcpy(scmnd->sense_buffer, rsp->data +
1716 be32_to_cpu(rsp->resp_data_len),
1717 min_t(int, be32_to_cpu(rsp->sense_data_len),
1718 SCSI_SENSE_BUFFERSIZE));
1719 }
1720
Bart Van Asschee7145312014-07-09 15:57:51 +02001721 if (unlikely(rsp->flags & SRP_RSP_FLAG_DIUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001722 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
Bart Van Asschee7145312014-07-09 15:57:51 +02001723 else if (unlikely(rsp->flags & SRP_RSP_FLAG_DIOVER))
1724 scsi_set_resid(scmnd, -be32_to_cpu(rsp->data_in_res_cnt));
1725 else if (unlikely(rsp->flags & SRP_RSP_FLAG_DOUNDER))
1726 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
1727 else if (unlikely(rsp->flags & SRP_RSP_FLAG_DOOVER))
1728 scsi_set_resid(scmnd, -be32_to_cpu(rsp->data_out_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001729
Bart Van Assche509c07b2014-10-30 14:48:30 +01001730 srp_free_req(ch, req, scmnd,
Bart Van Assche22032992012-08-14 13:18:53 +00001731 be32_to_cpu(rsp->req_lim_delta));
1732
David Dillowf8b6e312010-11-26 13:02:21 -05001733 scmnd->host_scribble = NULL;
1734 scmnd->scsi_done(scmnd);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001735 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001736}
1737
Bart Van Assche509c07b2014-10-30 14:48:30 +01001738static int srp_response_common(struct srp_rdma_ch *ch, s32 req_delta,
David Dillowbb125882010-10-08 14:40:47 -04001739 void *rsp, int len)
1740{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001741 struct srp_target_port *target = ch->target;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001742 struct ib_device *dev = target->srp_host->srp_dev->dev;
David Dillowbb125882010-10-08 14:40:47 -04001743 unsigned long flags;
1744 struct srp_iu *iu;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001745 int err;
David Dillowbb125882010-10-08 14:40:47 -04001746
Bart Van Assche509c07b2014-10-30 14:48:30 +01001747 spin_lock_irqsave(&ch->lock, flags);
1748 ch->req_lim += req_delta;
1749 iu = __srp_get_tx_iu(ch, SRP_IU_RSP);
1750 spin_unlock_irqrestore(&ch->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001751
David Dillowbb125882010-10-08 14:40:47 -04001752 if (!iu) {
1753 shost_printk(KERN_ERR, target->scsi_host, PFX
1754 "no IU available to send response\n");
Bart Van Assche76c75b22010-11-26 14:37:47 -05001755 return 1;
David Dillowbb125882010-10-08 14:40:47 -04001756 }
1757
1758 ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE);
1759 memcpy(iu->buf, rsp, len);
1760 ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE);
1761
Bart Van Assche509c07b2014-10-30 14:48:30 +01001762 err = srp_post_send(ch, iu, len);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001763 if (err) {
David Dillowbb125882010-10-08 14:40:47 -04001764 shost_printk(KERN_ERR, target->scsi_host, PFX
1765 "unable to post response: %d\n", err);
Bart Van Assche509c07b2014-10-30 14:48:30 +01001766 srp_put_tx_iu(ch, iu, SRP_IU_RSP);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001767 }
David Dillowbb125882010-10-08 14:40:47 -04001768
David Dillowbb125882010-10-08 14:40:47 -04001769 return err;
1770}
1771
Bart Van Assche509c07b2014-10-30 14:48:30 +01001772static void srp_process_cred_req(struct srp_rdma_ch *ch,
David Dillowbb125882010-10-08 14:40:47 -04001773 struct srp_cred_req *req)
1774{
1775 struct srp_cred_rsp rsp = {
1776 .opcode = SRP_CRED_RSP,
1777 .tag = req->tag,
1778 };
1779 s32 delta = be32_to_cpu(req->req_lim_delta);
1780
Bart Van Assche509c07b2014-10-30 14:48:30 +01001781 if (srp_response_common(ch, delta, &rsp, sizeof(rsp)))
1782 shost_printk(KERN_ERR, ch->target->scsi_host, PFX
David Dillowbb125882010-10-08 14:40:47 -04001783 "problems processing SRP_CRED_REQ\n");
1784}
1785
Bart Van Assche509c07b2014-10-30 14:48:30 +01001786static void srp_process_aer_req(struct srp_rdma_ch *ch,
David Dillowbb125882010-10-08 14:40:47 -04001787 struct srp_aer_req *req)
1788{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001789 struct srp_target_port *target = ch->target;
David Dillowbb125882010-10-08 14:40:47 -04001790 struct srp_aer_rsp rsp = {
1791 .opcode = SRP_AER_RSP,
1792 .tag = req->tag,
1793 };
1794 s32 delta = be32_to_cpu(req->req_lim_delta);
1795
1796 shost_printk(KERN_ERR, target->scsi_host, PFX
Bart Van Assche985aa492015-05-18 13:27:14 +02001797 "ignoring AER for LUN %llu\n", scsilun_to_int(&req->lun));
David Dillowbb125882010-10-08 14:40:47 -04001798
Bart Van Assche509c07b2014-10-30 14:48:30 +01001799 if (srp_response_common(ch, delta, &rsp, sizeof(rsp)))
David Dillowbb125882010-10-08 14:40:47 -04001800 shost_printk(KERN_ERR, target->scsi_host, PFX
1801 "problems processing SRP_AER_REQ\n");
1802}
1803
Bart Van Assche509c07b2014-10-30 14:48:30 +01001804static void srp_handle_recv(struct srp_rdma_ch *ch, struct ib_wc *wc)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001805{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001806 struct srp_target_port *target = ch->target;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001807 struct ib_device *dev = target->srp_host->srp_dev->dev;
Roland Dreier737b94e2011-05-23 11:30:04 -07001808 struct srp_iu *iu = (struct srp_iu *) (uintptr_t) wc->wr_id;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001809 int res;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001810 u8 opcode;
1811
Bart Van Assche509c07b2014-10-30 14:48:30 +01001812 ib_dma_sync_single_for_cpu(dev, iu->dma, ch->max_ti_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08001813 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001814
1815 opcode = *(u8 *) iu->buf;
1816
1817 if (0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001818 shost_printk(KERN_ERR, target->scsi_host,
1819 PFX "recv completion, opcode 0x%02x\n", opcode);
Bart Van Assche7a700812010-07-29 15:56:37 +00001820 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1,
1821 iu->buf, wc->byte_len, true);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001822 }
1823
1824 switch (opcode) {
1825 case SRP_RSP:
Bart Van Assche509c07b2014-10-30 14:48:30 +01001826 srp_process_rsp(ch, iu->buf);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001827 break;
1828
David Dillowbb125882010-10-08 14:40:47 -04001829 case SRP_CRED_REQ:
Bart Van Assche509c07b2014-10-30 14:48:30 +01001830 srp_process_cred_req(ch, iu->buf);
David Dillowbb125882010-10-08 14:40:47 -04001831 break;
1832
1833 case SRP_AER_REQ:
Bart Van Assche509c07b2014-10-30 14:48:30 +01001834 srp_process_aer_req(ch, iu->buf);
David Dillowbb125882010-10-08 14:40:47 -04001835 break;
1836
Roland Dreieraef9ec32005-11-02 14:07:13 -08001837 case SRP_T_LOGOUT:
1838 /* XXX Handle target logout */
David Dillow7aa54bd2008-01-07 18:23:41 -05001839 shost_printk(KERN_WARNING, target->scsi_host,
1840 PFX "Got target logout request\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001841 break;
1842
1843 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001844 shost_printk(KERN_WARNING, target->scsi_host,
1845 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001846 break;
1847 }
1848
Bart Van Assche509c07b2014-10-30 14:48:30 +01001849 ib_dma_sync_single_for_device(dev, iu->dma, ch->max_ti_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08001850 DMA_FROM_DEVICE);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001851
Bart Van Assche509c07b2014-10-30 14:48:30 +01001852 res = srp_post_recv(ch, iu);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001853 if (res != 0)
1854 shost_printk(KERN_ERR, target->scsi_host,
1855 PFX "Recv failed with error code %d\n", res);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001856}
1857
Bart Van Asschec1120f82013-10-26 14:35:08 +02001858/**
1859 * srp_tl_err_work() - handle a transport layer error
Bart Van Asscheaf246632014-05-20 15:04:21 +02001860 * @work: Work structure embedded in an SRP target port.
Bart Van Asschec1120f82013-10-26 14:35:08 +02001861 *
1862 * Note: This function may get invoked before the rport has been created,
1863 * hence the target->rport test.
1864 */
1865static void srp_tl_err_work(struct work_struct *work)
1866{
1867 struct srp_target_port *target;
1868
1869 target = container_of(work, struct srp_target_port, tl_err_work);
1870 if (target->rport)
1871 srp_start_tl_fail_timers(target->rport);
1872}
1873
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001874static void srp_handle_qp_err(u64 wr_id, enum ib_wc_status wc_status,
Bart Van Assche7dad6b22014-10-21 18:00:35 +02001875 bool send_err, struct srp_rdma_ch *ch)
Bart Van Assche948d1e82011-09-03 09:25:42 +02001876{
Bart Van Assche7dad6b22014-10-21 18:00:35 +02001877 struct srp_target_port *target = ch->target;
1878
1879 if (wr_id == SRP_LAST_WR_ID) {
1880 complete(&ch->done);
1881 return;
1882 }
1883
Bart Van Asschec014c8c2015-05-18 13:23:57 +02001884 if (ch->connected && !target->qp_in_error) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001885 if (wr_id & LOCAL_INV_WR_ID_MASK) {
1886 shost_printk(KERN_ERR, target->scsi_host, PFX
Sagi Grimberg57363d92015-05-18 13:40:29 +03001887 "LOCAL_INV failed with status %s (%d)\n",
1888 ib_wc_status_msg(wc_status), wc_status);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001889 } else if (wr_id & FAST_REG_WR_ID_MASK) {
1890 shost_printk(KERN_ERR, target->scsi_host, PFX
Sagi Grimberg57363d92015-05-18 13:40:29 +03001891 "FAST_REG_MR failed status %s (%d)\n",
1892 ib_wc_status_msg(wc_status), wc_status);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001893 } else {
1894 shost_printk(KERN_ERR, target->scsi_host,
Sagi Grimberg57363d92015-05-18 13:40:29 +03001895 PFX "failed %s status %s (%d) for iu %p\n",
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001896 send_err ? "send" : "receive",
Sagi Grimberg57363d92015-05-18 13:40:29 +03001897 ib_wc_status_msg(wc_status), wc_status,
1898 (void *)(uintptr_t)wr_id);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001899 }
Bart Van Asschec1120f82013-10-26 14:35:08 +02001900 queue_work(system_long_wq, &target->tl_err_work);
Bart Van Assche4f0af692012-11-26 11:16:40 +01001901 }
Bart Van Assche948d1e82011-09-03 09:25:42 +02001902 target->qp_in_error = true;
1903}
1904
Bart Van Assche509c07b2014-10-30 14:48:30 +01001905static void srp_recv_completion(struct ib_cq *cq, void *ch_ptr)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001906{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001907 struct srp_rdma_ch *ch = ch_ptr;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001908 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001909
1910 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1911 while (ib_poll_cq(cq, 1, &wc) > 0) {
Bart Van Assche948d1e82011-09-03 09:25:42 +02001912 if (likely(wc.status == IB_WC_SUCCESS)) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01001913 srp_handle_recv(ch, &wc);
Bart Van Assche948d1e82011-09-03 09:25:42 +02001914 } else {
Bart Van Assche7dad6b22014-10-21 18:00:35 +02001915 srp_handle_qp_err(wc.wr_id, wc.status, false, ch);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001916 }
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001917 }
1918}
1919
Bart Van Assche509c07b2014-10-30 14:48:30 +01001920static void srp_send_completion(struct ib_cq *cq, void *ch_ptr)
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001921{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001922 struct srp_rdma_ch *ch = ch_ptr;
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001923 struct ib_wc wc;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001924 struct srp_iu *iu;
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001925
1926 while (ib_poll_cq(cq, 1, &wc) > 0) {
Bart Van Assche948d1e82011-09-03 09:25:42 +02001927 if (likely(wc.status == IB_WC_SUCCESS)) {
1928 iu = (struct srp_iu *) (uintptr_t) wc.wr_id;
Bart Van Assche509c07b2014-10-30 14:48:30 +01001929 list_add(&iu->list, &ch->free_tx);
Bart Van Assche948d1e82011-09-03 09:25:42 +02001930 } else {
Bart Van Assche7dad6b22014-10-21 18:00:35 +02001931 srp_handle_qp_err(wc.wr_id, wc.status, true, ch);
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001932 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001933 }
1934}
1935
Bart Van Assche76c75b22010-11-26 14:37:47 -05001936static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001937{
Bart Van Assche76c75b22010-11-26 14:37:47 -05001938 struct srp_target_port *target = host_to_target(shost);
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001939 struct srp_rport *rport = target->rport;
Bart Van Assche509c07b2014-10-30 14:48:30 +01001940 struct srp_rdma_ch *ch;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001941 struct srp_request *req;
1942 struct srp_iu *iu;
1943 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001944 struct ib_device *dev;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001945 unsigned long flags;
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02001946 u32 tag;
1947 u16 idx;
Bart Van Assched1b42892014-05-20 15:07:20 +02001948 int len, ret;
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001949 const bool in_scsi_eh = !in_interrupt() && current == shost->ehandler;
1950
1951 /*
1952 * The SCSI EH thread is the only context from which srp_queuecommand()
1953 * can get invoked for blocked devices (SDEV_BLOCK /
1954 * SDEV_CREATED_BLOCK). Avoid racing with srp_reconnect_rport() by
1955 * locking the rport mutex if invoked from inside the SCSI EH.
1956 */
1957 if (in_scsi_eh)
1958 mutex_lock(&rport->mutex);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001959
Bart Van Assched1b42892014-05-20 15:07:20 +02001960 scmnd->result = srp_chkready(target->rport);
1961 if (unlikely(scmnd->result))
1962 goto err;
Bart Van Assche2ce19e72013-02-21 17:20:00 +00001963
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02001964 WARN_ON_ONCE(scmnd->request->tag < 0);
1965 tag = blk_mq_unique_tag(scmnd->request);
Bart Van Assched92c0da2014-10-06 17:14:36 +02001966 ch = &target->ch[blk_mq_unique_tag_to_hwq(tag)];
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02001967 idx = blk_mq_unique_tag_to_tag(tag);
1968 WARN_ONCE(idx >= target->req_ring_size, "%s: tag %#x: idx %d >= %d\n",
1969 dev_name(&shost->shost_gendev), tag, idx,
1970 target->req_ring_size);
Bart Van Assche509c07b2014-10-30 14:48:30 +01001971
1972 spin_lock_irqsave(&ch->lock, flags);
1973 iu = __srp_get_tx_iu(ch, SRP_IU_CMD);
Bart Van Assche509c07b2014-10-30 14:48:30 +01001974 spin_unlock_irqrestore(&ch->lock, flags);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001975
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02001976 if (!iu)
1977 goto err;
1978
1979 req = &ch->req_ring[idx];
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001980 dev = target->srp_host->srp_dev->dev;
David Dillow49248642011-01-14 18:23:24 -05001981 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08001982 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001983
David Dillowf8b6e312010-11-26 13:02:21 -05001984 scmnd->host_scribble = (void *) req;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001985
1986 cmd = iu->buf;
1987 memset(cmd, 0, sizeof *cmd);
1988
1989 cmd->opcode = SRP_CMD;
Bart Van Assche985aa492015-05-18 13:27:14 +02001990 int_to_scsilun(scmnd->device->lun, &cmd->lun);
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02001991 cmd->tag = tag;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001992 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1993
Roland Dreieraef9ec32005-11-02 14:07:13 -08001994 req->scmnd = scmnd;
1995 req->cmd = iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001996
Bart Van Assche509c07b2014-10-30 14:48:30 +01001997 len = srp_map_data(scmnd, ch, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001998 if (len < 0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001999 shost_printk(KERN_ERR, target->scsi_host,
Bart Van Assched1b42892014-05-20 15:07:20 +02002000 PFX "Failed to map data (%d)\n", len);
2001 /*
2002 * If we ran out of memory descriptors (-ENOMEM) because an
2003 * application is queuing many requests with more than
Bart Van Assche52ede082014-05-20 15:07:45 +02002004 * max_pages_per_mr sg-list elements, tell the SCSI mid-layer
Bart Van Assched1b42892014-05-20 15:07:20 +02002005 * to reduce queue depth temporarily.
2006 */
2007 scmnd->result = len == -ENOMEM ?
2008 DID_OK << 16 | QUEUE_FULL << 1 : DID_ERROR << 16;
Bart Van Assche76c75b22010-11-26 14:37:47 -05002009 goto err_iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002010 }
2011
David Dillow49248642011-01-14 18:23:24 -05002012 ib_dma_sync_single_for_device(dev, iu->dma, target->max_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08002013 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002014
Bart Van Assche509c07b2014-10-30 14:48:30 +01002015 if (srp_post_send(ch, iu, len)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05002016 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002017 goto err_unmap;
2018 }
2019
Bart Van Assched1b42892014-05-20 15:07:20 +02002020 ret = 0;
2021
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002022unlock_rport:
2023 if (in_scsi_eh)
2024 mutex_unlock(&rport->mutex);
2025
Bart Van Assched1b42892014-05-20 15:07:20 +02002026 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002027
2028err_unmap:
Bart Van Assche509c07b2014-10-30 14:48:30 +01002029 srp_unmap_data(scmnd, ch, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002030
Bart Van Assche76c75b22010-11-26 14:37:47 -05002031err_iu:
Bart Van Assche509c07b2014-10-30 14:48:30 +01002032 srp_put_tx_iu(ch, iu, SRP_IU_CMD);
Bart Van Assche76c75b22010-11-26 14:37:47 -05002033
Bart Van Assche024ca902014-05-20 15:03:49 +02002034 /*
2035 * Avoid that the loops that iterate over the request ring can
2036 * encounter a dangling SCSI command pointer.
2037 */
2038 req->scmnd = NULL;
2039
Bart Van Assched1b42892014-05-20 15:07:20 +02002040err:
2041 if (scmnd->result) {
2042 scmnd->scsi_done(scmnd);
2043 ret = 0;
2044 } else {
2045 ret = SCSI_MLQUEUE_HOST_BUSY;
2046 }
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002047
Bart Van Assched1b42892014-05-20 15:07:20 +02002048 goto unlock_rport;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002049}
2050
Bart Van Assche4d73f952013-10-26 14:40:37 +02002051/*
2052 * Note: the resources allocated in this function are freed in
Bart Van Assche509c07b2014-10-30 14:48:30 +01002053 * srp_free_ch_ib().
Bart Van Assche4d73f952013-10-26 14:40:37 +02002054 */
Bart Van Assche509c07b2014-10-30 14:48:30 +01002055static int srp_alloc_iu_bufs(struct srp_rdma_ch *ch)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002056{
Bart Van Assche509c07b2014-10-30 14:48:30 +01002057 struct srp_target_port *target = ch->target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002058 int i;
2059
Bart Van Assche509c07b2014-10-30 14:48:30 +01002060 ch->rx_ring = kcalloc(target->queue_size, sizeof(*ch->rx_ring),
2061 GFP_KERNEL);
2062 if (!ch->rx_ring)
Bart Van Assche4d73f952013-10-26 14:40:37 +02002063 goto err_no_ring;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002064 ch->tx_ring = kcalloc(target->queue_size, sizeof(*ch->tx_ring),
2065 GFP_KERNEL);
2066 if (!ch->tx_ring)
Bart Van Assche4d73f952013-10-26 14:40:37 +02002067 goto err_no_ring;
2068
2069 for (i = 0; i < target->queue_size; ++i) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01002070 ch->rx_ring[i] = srp_alloc_iu(target->srp_host,
2071 ch->max_ti_iu_len,
2072 GFP_KERNEL, DMA_FROM_DEVICE);
2073 if (!ch->rx_ring[i])
Roland Dreieraef9ec32005-11-02 14:07:13 -08002074 goto err;
2075 }
2076
Bart Van Assche4d73f952013-10-26 14:40:37 +02002077 for (i = 0; i < target->queue_size; ++i) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01002078 ch->tx_ring[i] = srp_alloc_iu(target->srp_host,
2079 target->max_iu_len,
2080 GFP_KERNEL, DMA_TO_DEVICE);
2081 if (!ch->tx_ring[i])
Roland Dreieraef9ec32005-11-02 14:07:13 -08002082 goto err;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05002083
Bart Van Assche509c07b2014-10-30 14:48:30 +01002084 list_add(&ch->tx_ring[i]->list, &ch->free_tx);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002085 }
2086
2087 return 0;
2088
2089err:
Bart Van Assche4d73f952013-10-26 14:40:37 +02002090 for (i = 0; i < target->queue_size; ++i) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01002091 srp_free_iu(target->srp_host, ch->rx_ring[i]);
2092 srp_free_iu(target->srp_host, ch->tx_ring[i]);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002093 }
2094
Bart Van Assche4d73f952013-10-26 14:40:37 +02002095
2096err_no_ring:
Bart Van Assche509c07b2014-10-30 14:48:30 +01002097 kfree(ch->tx_ring);
2098 ch->tx_ring = NULL;
2099 kfree(ch->rx_ring);
2100 ch->rx_ring = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002101
2102 return -ENOMEM;
2103}
2104
Bart Van Asschec9b03c12011-09-03 09:34:48 +02002105static uint32_t srp_compute_rq_tmo(struct ib_qp_attr *qp_attr, int attr_mask)
2106{
2107 uint64_t T_tr_ns, max_compl_time_ms;
2108 uint32_t rq_tmo_jiffies;
2109
2110 /*
2111 * According to section 11.2.4.2 in the IBTA spec (Modify Queue Pair,
2112 * table 91), both the QP timeout and the retry count have to be set
2113 * for RC QP's during the RTR to RTS transition.
2114 */
2115 WARN_ON_ONCE((attr_mask & (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)) !=
2116 (IB_QP_TIMEOUT | IB_QP_RETRY_CNT));
2117
2118 /*
2119 * Set target->rq_tmo_jiffies to one second more than the largest time
2120 * it can take before an error completion is generated. See also
2121 * C9-140..142 in the IBTA spec for more information about how to
2122 * convert the QP Local ACK Timeout value to nanoseconds.
2123 */
2124 T_tr_ns = 4096 * (1ULL << qp_attr->timeout);
2125 max_compl_time_ms = qp_attr->retry_cnt * 4 * T_tr_ns;
2126 do_div(max_compl_time_ms, NSEC_PER_MSEC);
2127 rq_tmo_jiffies = msecs_to_jiffies(max_compl_time_ms + 1000);
2128
2129 return rq_tmo_jiffies;
2130}
2131
David Dillow961e0be2011-01-14 17:32:07 -05002132static void srp_cm_rep_handler(struct ib_cm_id *cm_id,
Bart Van Asschee6300cb2015-07-31 14:12:48 -07002133 const struct srp_login_rsp *lrsp,
Bart Van Assche509c07b2014-10-30 14:48:30 +01002134 struct srp_rdma_ch *ch)
David Dillow961e0be2011-01-14 17:32:07 -05002135{
Bart Van Assche509c07b2014-10-30 14:48:30 +01002136 struct srp_target_port *target = ch->target;
David Dillow961e0be2011-01-14 17:32:07 -05002137 struct ib_qp_attr *qp_attr = NULL;
2138 int attr_mask = 0;
2139 int ret;
2140 int i;
2141
2142 if (lrsp->opcode == SRP_LOGIN_RSP) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01002143 ch->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len);
2144 ch->req_lim = be32_to_cpu(lrsp->req_lim_delta);
David Dillow961e0be2011-01-14 17:32:07 -05002145
2146 /*
2147 * Reserve credits for task management so we don't
2148 * bounce requests back to the SCSI mid-layer.
2149 */
2150 target->scsi_host->can_queue
Bart Van Assche509c07b2014-10-30 14:48:30 +01002151 = min(ch->req_lim - SRP_TSK_MGMT_SQ_SIZE,
David Dillow961e0be2011-01-14 17:32:07 -05002152 target->scsi_host->can_queue);
Bart Van Assche4d73f952013-10-26 14:40:37 +02002153 target->scsi_host->cmd_per_lun
2154 = min_t(int, target->scsi_host->can_queue,
2155 target->scsi_host->cmd_per_lun);
David Dillow961e0be2011-01-14 17:32:07 -05002156 } else {
2157 shost_printk(KERN_WARNING, target->scsi_host,
2158 PFX "Unhandled RSP opcode %#x\n", lrsp->opcode);
2159 ret = -ECONNRESET;
2160 goto error;
2161 }
2162
Bart Van Assche509c07b2014-10-30 14:48:30 +01002163 if (!ch->rx_ring) {
2164 ret = srp_alloc_iu_bufs(ch);
David Dillow961e0be2011-01-14 17:32:07 -05002165 if (ret)
2166 goto error;
2167 }
2168
2169 ret = -ENOMEM;
2170 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
2171 if (!qp_attr)
2172 goto error;
2173
2174 qp_attr->qp_state = IB_QPS_RTR;
2175 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
2176 if (ret)
2177 goto error_free;
2178
Bart Van Assche509c07b2014-10-30 14:48:30 +01002179 ret = ib_modify_qp(ch->qp, qp_attr, attr_mask);
David Dillow961e0be2011-01-14 17:32:07 -05002180 if (ret)
2181 goto error_free;
2182
Bart Van Assche4d73f952013-10-26 14:40:37 +02002183 for (i = 0; i < target->queue_size; i++) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01002184 struct srp_iu *iu = ch->rx_ring[i];
2185
2186 ret = srp_post_recv(ch, iu);
David Dillow961e0be2011-01-14 17:32:07 -05002187 if (ret)
2188 goto error_free;
2189 }
2190
2191 qp_attr->qp_state = IB_QPS_RTS;
2192 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
2193 if (ret)
2194 goto error_free;
2195
Bart Van Asschec9b03c12011-09-03 09:34:48 +02002196 target->rq_tmo_jiffies = srp_compute_rq_tmo(qp_attr, attr_mask);
2197
Bart Van Assche509c07b2014-10-30 14:48:30 +01002198 ret = ib_modify_qp(ch->qp, qp_attr, attr_mask);
David Dillow961e0be2011-01-14 17:32:07 -05002199 if (ret)
2200 goto error_free;
2201
2202 ret = ib_send_cm_rtu(cm_id, NULL, 0);
2203
2204error_free:
2205 kfree(qp_attr);
2206
2207error:
Bart Van Assche509c07b2014-10-30 14:48:30 +01002208 ch->status = ret;
David Dillow961e0be2011-01-14 17:32:07 -05002209}
2210
Roland Dreieraef9ec32005-11-02 14:07:13 -08002211static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
2212 struct ib_cm_event *event,
Bart Van Assche509c07b2014-10-30 14:48:30 +01002213 struct srp_rdma_ch *ch)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002214{
Bart Van Assche509c07b2014-10-30 14:48:30 +01002215 struct srp_target_port *target = ch->target;
David Dillow7aa54bd2008-01-07 18:23:41 -05002216 struct Scsi_Host *shost = target->scsi_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002217 struct ib_class_port_info *cpi;
2218 int opcode;
2219
2220 switch (event->param.rej_rcvd.reason) {
2221 case IB_CM_REJ_PORT_CM_REDIRECT:
2222 cpi = event->param.rej_rcvd.ari;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002223 ch->path.dlid = cpi->redirect_lid;
2224 ch->path.pkey = cpi->redirect_pkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002225 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002226 memcpy(ch->path.dgid.raw, cpi->redirect_gid, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002227
Bart Van Assche509c07b2014-10-30 14:48:30 +01002228 ch->status = ch->path.dlid ?
Roland Dreieraef9ec32005-11-02 14:07:13 -08002229 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
2230 break;
2231
2232 case IB_CM_REJ_PORT_REDIRECT:
Roland Dreier5d7cbfd2007-08-03 10:45:18 -07002233 if (srp_target_is_topspin(target)) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002234 /*
2235 * Topspin/Cisco SRP gateways incorrectly send
2236 * reject reason code 25 when they mean 24
2237 * (port redirect).
2238 */
Bart Van Assche509c07b2014-10-30 14:48:30 +01002239 memcpy(ch->path.dgid.raw,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002240 event->param.rej_rcvd.ari, 16);
2241
David Dillow7aa54bd2008-01-07 18:23:41 -05002242 shost_printk(KERN_DEBUG, shost,
2243 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
Bart Van Assche509c07b2014-10-30 14:48:30 +01002244 be64_to_cpu(ch->path.dgid.global.subnet_prefix),
2245 be64_to_cpu(ch->path.dgid.global.interface_id));
Roland Dreieraef9ec32005-11-02 14:07:13 -08002246
Bart Van Assche509c07b2014-10-30 14:48:30 +01002247 ch->status = SRP_PORT_REDIRECT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002248 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05002249 shost_printk(KERN_WARNING, shost,
2250 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
Bart Van Assche509c07b2014-10-30 14:48:30 +01002251 ch->status = -ECONNRESET;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002252 }
2253 break;
2254
2255 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
David Dillow7aa54bd2008-01-07 18:23:41 -05002256 shost_printk(KERN_WARNING, shost,
2257 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
Bart Van Assche509c07b2014-10-30 14:48:30 +01002258 ch->status = -ECONNRESET;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002259 break;
2260
2261 case IB_CM_REJ_CONSUMER_DEFINED:
2262 opcode = *(u8 *) event->private_data;
2263 if (opcode == SRP_LOGIN_REJ) {
2264 struct srp_login_rej *rej = event->private_data;
2265 u32 reason = be32_to_cpu(rej->reason);
2266
2267 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
David Dillow7aa54bd2008-01-07 18:23:41 -05002268 shost_printk(KERN_WARNING, shost,
2269 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002270 else
Bart Van Asschee7ffde02014-03-14 13:52:21 +01002271 shost_printk(KERN_WARNING, shost, PFX
2272 "SRP LOGIN from %pI6 to %pI6 REJECTED, reason 0x%08x\n",
Bart Van Assche747fe002014-10-30 14:48:05 +01002273 target->sgid.raw,
2274 target->orig_dgid.raw, reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002275 } else
David Dillow7aa54bd2008-01-07 18:23:41 -05002276 shost_printk(KERN_WARNING, shost,
2277 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
2278 " opcode 0x%02x\n", opcode);
Bart Van Assche509c07b2014-10-30 14:48:30 +01002279 ch->status = -ECONNRESET;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002280 break;
2281
David Dillow9fe4bcf2008-01-08 17:08:52 -05002282 case IB_CM_REJ_STALE_CONN:
2283 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
Bart Van Assche509c07b2014-10-30 14:48:30 +01002284 ch->status = SRP_STALE_CONN;
David Dillow9fe4bcf2008-01-08 17:08:52 -05002285 break;
2286
Roland Dreieraef9ec32005-11-02 14:07:13 -08002287 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05002288 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
2289 event->param.rej_rcvd.reason);
Bart Van Assche509c07b2014-10-30 14:48:30 +01002290 ch->status = -ECONNRESET;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002291 }
2292}
2293
2294static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2295{
Bart Van Assche509c07b2014-10-30 14:48:30 +01002296 struct srp_rdma_ch *ch = cm_id->context;
2297 struct srp_target_port *target = ch->target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002298 int comp = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002299
2300 switch (event->event) {
2301 case IB_CM_REQ_ERROR:
David Dillow7aa54bd2008-01-07 18:23:41 -05002302 shost_printk(KERN_DEBUG, target->scsi_host,
2303 PFX "Sending CM REQ failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002304 comp = 1;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002305 ch->status = -ECONNRESET;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002306 break;
2307
2308 case IB_CM_REP_RECEIVED:
2309 comp = 1;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002310 srp_cm_rep_handler(cm_id, event->private_data, ch);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002311 break;
2312
2313 case IB_CM_REJ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05002314 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002315 comp = 1;
2316
Bart Van Assche509c07b2014-10-30 14:48:30 +01002317 srp_cm_rej_handler(cm_id, event, ch);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002318 break;
2319
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07002320 case IB_CM_DREQ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05002321 shost_printk(KERN_WARNING, target->scsi_host,
2322 PFX "DREQ received - connection closed\n");
Bart Van Asschec014c8c2015-05-18 13:23:57 +02002323 ch->connected = false;
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07002324 if (ib_send_cm_drep(cm_id, NULL, 0))
David Dillow7aa54bd2008-01-07 18:23:41 -05002325 shost_printk(KERN_ERR, target->scsi_host,
2326 PFX "Sending CM DREP failed\n");
Bart Van Asschec1120f82013-10-26 14:35:08 +02002327 queue_work(system_long_wq, &target->tl_err_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002328 break;
2329
2330 case IB_CM_TIMEWAIT_EXIT:
David Dillow7aa54bd2008-01-07 18:23:41 -05002331 shost_printk(KERN_ERR, target->scsi_host,
2332 PFX "connection closed\n");
Bart Van Asscheac72d762014-03-14 13:53:40 +01002333 comp = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002334
Bart Van Assche509c07b2014-10-30 14:48:30 +01002335 ch->status = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002336 break;
2337
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07002338 case IB_CM_MRA_RECEIVED:
2339 case IB_CM_DREQ_ERROR:
2340 case IB_CM_DREP_RECEIVED:
2341 break;
2342
Roland Dreieraef9ec32005-11-02 14:07:13 -08002343 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05002344 shost_printk(KERN_WARNING, target->scsi_host,
2345 PFX "Unhandled CM event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002346 break;
2347 }
2348
2349 if (comp)
Bart Van Assche509c07b2014-10-30 14:48:30 +01002350 complete(&ch->done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002351
Roland Dreieraef9ec32005-11-02 14:07:13 -08002352 return 0;
2353}
2354
Jack Wang71444b92013-11-07 11:37:37 +01002355/**
Jack Wang71444b92013-11-07 11:37:37 +01002356 * srp_change_queue_depth - setting device queue depth
2357 * @sdev: scsi device struct
2358 * @qdepth: requested queue depth
Jack Wang71444b92013-11-07 11:37:37 +01002359 *
2360 * Returns queue depth.
2361 */
2362static int
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002363srp_change_queue_depth(struct scsi_device *sdev, int qdepth)
Jack Wang71444b92013-11-07 11:37:37 +01002364{
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01002365 if (!sdev->tagged_supported)
Christoph Hellwig1e6f2412014-11-13 14:27:41 +01002366 qdepth = 1;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002367 return scsi_change_queue_depth(sdev, qdepth);
Jack Wang71444b92013-11-07 11:37:37 +01002368}
2369
Bart Van Assche985aa492015-05-18 13:27:14 +02002370static int srp_send_tsk_mgmt(struct srp_rdma_ch *ch, u64 req_tag, u64 lun,
2371 u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002372{
Bart Van Assche509c07b2014-10-30 14:48:30 +01002373 struct srp_target_port *target = ch->target;
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002374 struct srp_rport *rport = target->rport;
David Dillow19081f32010-10-18 08:54:49 -04002375 struct ib_device *dev = target->srp_host->srp_dev->dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002376 struct srp_iu *iu;
2377 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002378
Bart Van Asschec014c8c2015-05-18 13:23:57 +02002379 if (!ch->connected || target->qp_in_error)
Bart Van Assche3780d1f2013-02-21 17:18:00 +00002380 return -1;
2381
Bart Van Assche509c07b2014-10-30 14:48:30 +01002382 init_completion(&ch->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002383
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002384 /*
Bart Van Assche509c07b2014-10-30 14:48:30 +01002385 * Lock the rport mutex to avoid that srp_create_ch_ib() is
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002386 * invoked while a task management function is being sent.
2387 */
2388 mutex_lock(&rport->mutex);
Bart Van Assche509c07b2014-10-30 14:48:30 +01002389 spin_lock_irq(&ch->lock);
2390 iu = __srp_get_tx_iu(ch, SRP_IU_TSK_MGMT);
2391 spin_unlock_irq(&ch->lock);
Bart Van Assche76c75b22010-11-26 14:37:47 -05002392
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002393 if (!iu) {
2394 mutex_unlock(&rport->mutex);
2395
Bart Van Assche76c75b22010-11-26 14:37:47 -05002396 return -1;
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002397 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002398
David Dillow19081f32010-10-18 08:54:49 -04002399 ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt,
2400 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002401 tsk_mgmt = iu->buf;
2402 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
2403
2404 tsk_mgmt->opcode = SRP_TSK_MGMT;
Bart Van Assche985aa492015-05-18 13:27:14 +02002405 int_to_scsilun(lun, &tsk_mgmt->lun);
David Dillowf8b6e312010-11-26 13:02:21 -05002406 tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002407 tsk_mgmt->tsk_mgmt_func = func;
David Dillowf8b6e312010-11-26 13:02:21 -05002408 tsk_mgmt->task_tag = req_tag;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002409
David Dillow19081f32010-10-18 08:54:49 -04002410 ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt,
2411 DMA_TO_DEVICE);
Bart Van Assche509c07b2014-10-30 14:48:30 +01002412 if (srp_post_send(ch, iu, sizeof(*tsk_mgmt))) {
2413 srp_put_tx_iu(ch, iu, SRP_IU_TSK_MGMT);
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002414 mutex_unlock(&rport->mutex);
2415
Bart Van Assche76c75b22010-11-26 14:37:47 -05002416 return -1;
2417 }
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002418 mutex_unlock(&rport->mutex);
Roland Dreierd945e1d2006-05-09 10:50:28 -07002419
Bart Van Assche509c07b2014-10-30 14:48:30 +01002420 if (!wait_for_completion_timeout(&ch->tsk_mgmt_done,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002421 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07002422 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002423
Roland Dreierd945e1d2006-05-09 10:50:28 -07002424 return 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07002425}
2426
Roland Dreieraef9ec32005-11-02 14:07:13 -08002427static int srp_abort(struct scsi_cmnd *scmnd)
2428{
Roland Dreierd945e1d2006-05-09 10:50:28 -07002429 struct srp_target_port *target = host_to_target(scmnd->device->host);
David Dillowf8b6e312010-11-26 13:02:21 -05002430 struct srp_request *req = (struct srp_request *) scmnd->host_scribble;
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02002431 u32 tag;
Bart Van Assched92c0da2014-10-06 17:14:36 +02002432 u16 ch_idx;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002433 struct srp_rdma_ch *ch;
Bart Van Assche086f44f2013-06-12 15:23:04 +02002434 int ret;
Roland Dreierd945e1d2006-05-09 10:50:28 -07002435
David Dillow7aa54bd2008-01-07 18:23:41 -05002436 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002437
Bart Van Assched92c0da2014-10-06 17:14:36 +02002438 if (!req)
Bart Van Assche99b66972013-10-10 13:52:33 +02002439 return SUCCESS;
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02002440 tag = blk_mq_unique_tag(scmnd->request);
Bart Van Assched92c0da2014-10-06 17:14:36 +02002441 ch_idx = blk_mq_unique_tag_to_hwq(tag);
2442 if (WARN_ON_ONCE(ch_idx >= target->ch_count))
2443 return SUCCESS;
2444 ch = &target->ch[ch_idx];
2445 if (!srp_claim_req(ch, req, NULL, scmnd))
2446 return SUCCESS;
2447 shost_printk(KERN_ERR, target->scsi_host,
2448 "Sending SRP abort for tag %#x\n", tag);
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02002449 if (srp_send_tsk_mgmt(ch, tag, scmnd->device->lun,
Bart Van Assche80d5e8a2013-07-10 17:36:35 +02002450 SRP_TSK_ABORT_TASK) == 0)
Bart Van Assche086f44f2013-06-12 15:23:04 +02002451 ret = SUCCESS;
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002452 else if (target->rport->state == SRP_RPORT_LOST)
Bart Van Assche99e1c132013-06-28 14:49:58 +02002453 ret = FAST_IO_FAIL;
Bart Van Assche086f44f2013-06-12 15:23:04 +02002454 else
2455 ret = FAILED;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002456 srp_free_req(ch, req, scmnd, 0);
Bart Van Assche22032992012-08-14 13:18:53 +00002457 scmnd->result = DID_ABORT << 16;
Bart Van Assched8536672012-08-24 10:29:11 +00002458 scmnd->scsi_done(scmnd);
Roland Dreierd945e1d2006-05-09 10:50:28 -07002459
Bart Van Assche086f44f2013-06-12 15:23:04 +02002460 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002461}
2462
2463static int srp_reset_device(struct scsi_cmnd *scmnd)
2464{
Roland Dreierd945e1d2006-05-09 10:50:28 -07002465 struct srp_target_port *target = host_to_target(scmnd->device->host);
Bart Van Assched92c0da2014-10-06 17:14:36 +02002466 struct srp_rdma_ch *ch;
Bart Van Assche536ae142010-11-26 13:58:27 -05002467 int i;
Roland Dreierd945e1d2006-05-09 10:50:28 -07002468
David Dillow7aa54bd2008-01-07 18:23:41 -05002469 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002470
Bart Van Assched92c0da2014-10-06 17:14:36 +02002471 ch = &target->ch[0];
Bart Van Assche509c07b2014-10-30 14:48:30 +01002472 if (srp_send_tsk_mgmt(ch, SRP_TAG_NO_REQ, scmnd->device->lun,
David Dillowf8b6e312010-11-26 13:02:21 -05002473 SRP_TSK_LUN_RESET))
Roland Dreierd945e1d2006-05-09 10:50:28 -07002474 return FAILED;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002475 if (ch->tsk_mgmt_status)
Roland Dreierd945e1d2006-05-09 10:50:28 -07002476 return FAILED;
2477
Bart Van Assched92c0da2014-10-06 17:14:36 +02002478 for (i = 0; i < target->ch_count; i++) {
2479 ch = &target->ch[i];
2480 for (i = 0; i < target->req_ring_size; ++i) {
2481 struct srp_request *req = &ch->req_ring[i];
Bart Van Assche509c07b2014-10-30 14:48:30 +01002482
Bart Van Assched92c0da2014-10-06 17:14:36 +02002483 srp_finish_req(ch, req, scmnd->device, DID_RESET << 16);
2484 }
Bart Van Assche536ae142010-11-26 13:58:27 -05002485 }
Roland Dreierd945e1d2006-05-09 10:50:28 -07002486
Roland Dreierd945e1d2006-05-09 10:50:28 -07002487 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002488}
2489
2490static int srp_reset_host(struct scsi_cmnd *scmnd)
2491{
2492 struct srp_target_port *target = host_to_target(scmnd->device->host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002493
David Dillow7aa54bd2008-01-07 18:23:41 -05002494 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002495
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002496 return srp_reconnect_rport(target->rport) == 0 ? SUCCESS : FAILED;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002497}
2498
Bart Van Asschec9b03c12011-09-03 09:34:48 +02002499static int srp_slave_configure(struct scsi_device *sdev)
2500{
2501 struct Scsi_Host *shost = sdev->host;
2502 struct srp_target_port *target = host_to_target(shost);
2503 struct request_queue *q = sdev->request_queue;
2504 unsigned long timeout;
2505
2506 if (sdev->type == TYPE_DISK) {
2507 timeout = max_t(unsigned, 30 * HZ, target->rq_tmo_jiffies);
2508 blk_queue_rq_timeout(q, timeout);
2509 }
2510
2511 return 0;
2512}
2513
Tony Jonesee959b02008-02-22 00:13:36 +01002514static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
2515 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002516{
Tony Jonesee959b02008-02-22 00:13:36 +01002517 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002518
Bart Van Assche45c37ca2015-05-18 13:25:10 +02002519 return sprintf(buf, "0x%016llx\n", be64_to_cpu(target->id_ext));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002520}
2521
Tony Jonesee959b02008-02-22 00:13:36 +01002522static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
2523 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002524{
Tony Jonesee959b02008-02-22 00:13:36 +01002525 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002526
Bart Van Assche45c37ca2015-05-18 13:25:10 +02002527 return sprintf(buf, "0x%016llx\n", be64_to_cpu(target->ioc_guid));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002528}
2529
Tony Jonesee959b02008-02-22 00:13:36 +01002530static ssize_t show_service_id(struct device *dev,
2531 struct device_attribute *attr, char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002532{
Tony Jonesee959b02008-02-22 00:13:36 +01002533 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002534
Bart Van Assche45c37ca2015-05-18 13:25:10 +02002535 return sprintf(buf, "0x%016llx\n", be64_to_cpu(target->service_id));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002536}
2537
Tony Jonesee959b02008-02-22 00:13:36 +01002538static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
2539 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002540{
Tony Jonesee959b02008-02-22 00:13:36 +01002541 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002542
Bart Van Assche747fe002014-10-30 14:48:05 +01002543 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->pkey));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002544}
2545
Bart Van Assche848b3082013-10-26 14:38:12 +02002546static ssize_t show_sgid(struct device *dev, struct device_attribute *attr,
2547 char *buf)
2548{
2549 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2550
Bart Van Assche747fe002014-10-30 14:48:05 +01002551 return sprintf(buf, "%pI6\n", target->sgid.raw);
Bart Van Assche848b3082013-10-26 14:38:12 +02002552}
2553
Tony Jonesee959b02008-02-22 00:13:36 +01002554static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
2555 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002556{
Tony Jonesee959b02008-02-22 00:13:36 +01002557 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Bart Van Assched92c0da2014-10-06 17:14:36 +02002558 struct srp_rdma_ch *ch = &target->ch[0];
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002559
Bart Van Assche509c07b2014-10-30 14:48:30 +01002560 return sprintf(buf, "%pI6\n", ch->path.dgid.raw);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002561}
2562
Tony Jonesee959b02008-02-22 00:13:36 +01002563static ssize_t show_orig_dgid(struct device *dev,
2564 struct device_attribute *attr, char *buf)
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002565{
Tony Jonesee959b02008-02-22 00:13:36 +01002566 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002567
Bart Van Assche747fe002014-10-30 14:48:05 +01002568 return sprintf(buf, "%pI6\n", target->orig_dgid.raw);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002569}
2570
Bart Van Assche89de7482010-08-03 14:08:45 +00002571static ssize_t show_req_lim(struct device *dev,
2572 struct device_attribute *attr, char *buf)
2573{
2574 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Bart Van Assched92c0da2014-10-06 17:14:36 +02002575 struct srp_rdma_ch *ch;
2576 int i, req_lim = INT_MAX;
Bart Van Assche89de7482010-08-03 14:08:45 +00002577
Bart Van Assched92c0da2014-10-06 17:14:36 +02002578 for (i = 0; i < target->ch_count; i++) {
2579 ch = &target->ch[i];
2580 req_lim = min(req_lim, ch->req_lim);
2581 }
2582 return sprintf(buf, "%d\n", req_lim);
Bart Van Assche89de7482010-08-03 14:08:45 +00002583}
2584
Tony Jonesee959b02008-02-22 00:13:36 +01002585static ssize_t show_zero_req_lim(struct device *dev,
2586 struct device_attribute *attr, char *buf)
Roland Dreier6bfa24f2006-06-17 20:37:33 -07002587{
Tony Jonesee959b02008-02-22 00:13:36 +01002588 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6bfa24f2006-06-17 20:37:33 -07002589
Roland Dreier6bfa24f2006-06-17 20:37:33 -07002590 return sprintf(buf, "%d\n", target->zero_req_lim);
2591}
2592
Tony Jonesee959b02008-02-22 00:13:36 +01002593static ssize_t show_local_ib_port(struct device *dev,
2594 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002595{
Tony Jonesee959b02008-02-22 00:13:36 +01002596 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002597
2598 return sprintf(buf, "%d\n", target->srp_host->port);
2599}
2600
Tony Jonesee959b02008-02-22 00:13:36 +01002601static ssize_t show_local_ib_device(struct device *dev,
2602 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002603{
Tony Jonesee959b02008-02-22 00:13:36 +01002604 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002605
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002606 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002607}
2608
Bart Van Assched92c0da2014-10-06 17:14:36 +02002609static ssize_t show_ch_count(struct device *dev, struct device_attribute *attr,
2610 char *buf)
2611{
2612 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2613
2614 return sprintf(buf, "%d\n", target->ch_count);
2615}
2616
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002617static ssize_t show_comp_vector(struct device *dev,
2618 struct device_attribute *attr, char *buf)
2619{
2620 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2621
2622 return sprintf(buf, "%d\n", target->comp_vector);
2623}
2624
Vu Pham7bb312e2013-10-26 14:31:27 +02002625static ssize_t show_tl_retry_count(struct device *dev,
2626 struct device_attribute *attr, char *buf)
2627{
2628 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2629
2630 return sprintf(buf, "%d\n", target->tl_retry_count);
2631}
2632
David Dillow49248642011-01-14 18:23:24 -05002633static ssize_t show_cmd_sg_entries(struct device *dev,
2634 struct device_attribute *attr, char *buf)
2635{
2636 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2637
2638 return sprintf(buf, "%u\n", target->cmd_sg_cnt);
2639}
2640
David Dillowc07d4242011-01-16 13:57:10 -05002641static ssize_t show_allow_ext_sg(struct device *dev,
2642 struct device_attribute *attr, char *buf)
2643{
2644 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2645
2646 return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false");
2647}
2648
Tony Jonesee959b02008-02-22 00:13:36 +01002649static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
2650static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
2651static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
2652static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
Bart Van Assche848b3082013-10-26 14:38:12 +02002653static DEVICE_ATTR(sgid, S_IRUGO, show_sgid, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01002654static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
2655static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
Bart Van Assche89de7482010-08-03 14:08:45 +00002656static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01002657static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
2658static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
2659static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Bart Van Assched92c0da2014-10-06 17:14:36 +02002660static DEVICE_ATTR(ch_count, S_IRUGO, show_ch_count, NULL);
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002661static DEVICE_ATTR(comp_vector, S_IRUGO, show_comp_vector, NULL);
Vu Pham7bb312e2013-10-26 14:31:27 +02002662static DEVICE_ATTR(tl_retry_count, S_IRUGO, show_tl_retry_count, NULL);
David Dillow49248642011-01-14 18:23:24 -05002663static DEVICE_ATTR(cmd_sg_entries, S_IRUGO, show_cmd_sg_entries, NULL);
David Dillowc07d4242011-01-16 13:57:10 -05002664static DEVICE_ATTR(allow_ext_sg, S_IRUGO, show_allow_ext_sg, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002665
Tony Jonesee959b02008-02-22 00:13:36 +01002666static struct device_attribute *srp_host_attrs[] = {
2667 &dev_attr_id_ext,
2668 &dev_attr_ioc_guid,
2669 &dev_attr_service_id,
2670 &dev_attr_pkey,
Bart Van Assche848b3082013-10-26 14:38:12 +02002671 &dev_attr_sgid,
Tony Jonesee959b02008-02-22 00:13:36 +01002672 &dev_attr_dgid,
2673 &dev_attr_orig_dgid,
Bart Van Assche89de7482010-08-03 14:08:45 +00002674 &dev_attr_req_lim,
Tony Jonesee959b02008-02-22 00:13:36 +01002675 &dev_attr_zero_req_lim,
2676 &dev_attr_local_ib_port,
2677 &dev_attr_local_ib_device,
Bart Van Assched92c0da2014-10-06 17:14:36 +02002678 &dev_attr_ch_count,
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002679 &dev_attr_comp_vector,
Vu Pham7bb312e2013-10-26 14:31:27 +02002680 &dev_attr_tl_retry_count,
David Dillow49248642011-01-14 18:23:24 -05002681 &dev_attr_cmd_sg_entries,
David Dillowc07d4242011-01-16 13:57:10 -05002682 &dev_attr_allow_ext_sg,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002683 NULL
2684};
2685
Roland Dreieraef9ec32005-11-02 14:07:13 -08002686static struct scsi_host_template srp_template = {
2687 .module = THIS_MODULE,
Roland Dreierb7f008f2007-05-06 21:18:11 -07002688 .name = "InfiniBand SRP initiator",
2689 .proc_name = DRV_NAME,
Bart Van Asschec9b03c12011-09-03 09:34:48 +02002690 .slave_configure = srp_slave_configure,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002691 .info = srp_target_info,
2692 .queuecommand = srp_queuecommand,
Jack Wang71444b92013-11-07 11:37:37 +01002693 .change_queue_depth = srp_change_queue_depth,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002694 .eh_abort_handler = srp_abort,
2695 .eh_device_reset_handler = srp_reset_device,
2696 .eh_host_reset_handler = srp_reset_host,
Bart Van Assche2742c1d2013-06-12 15:24:25 +02002697 .skip_settle_delay = true,
David Dillow49248642011-01-14 18:23:24 -05002698 .sg_tablesize = SRP_DEF_SG_TABLESIZE,
Bart Van Assche4d73f952013-10-26 14:40:37 +02002699 .can_queue = SRP_DEFAULT_CMD_SQ_SIZE,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002700 .this_id = -1,
Bart Van Assche4d73f952013-10-26 14:40:37 +02002701 .cmd_per_lun = SRP_DEFAULT_CMD_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002702 .use_clustering = ENABLE_CLUSTERING,
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02002703 .shost_attrs = srp_host_attrs,
2704 .use_blk_tags = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01002705 .track_queue_depth = 1,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002706};
2707
Bart Van Assche34aa6542014-10-30 14:47:22 +01002708static int srp_sdev_count(struct Scsi_Host *host)
2709{
2710 struct scsi_device *sdev;
2711 int c = 0;
2712
2713 shost_for_each_device(sdev, host)
2714 c++;
2715
2716 return c;
2717}
2718
Bart Van Asschebc44bd12015-08-14 11:01:09 -07002719/*
2720 * Return values:
2721 * < 0 upon failure. Caller is responsible for SRP target port cleanup.
2722 * 0 and target->state == SRP_TARGET_REMOVED if asynchronous target port
2723 * removal has been scheduled.
2724 * 0 and target->state != SRP_TARGET_REMOVED upon success.
2725 */
Roland Dreieraef9ec32005-11-02 14:07:13 -08002726static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
2727{
FUJITA Tomonori32368222007-06-27 16:33:12 +09002728 struct srp_rport_identifiers ids;
2729 struct srp_rport *rport;
2730
Bart Van Assche34aa6542014-10-30 14:47:22 +01002731 target->state = SRP_TARGET_SCANNING;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002732 sprintf(target->target_name, "SRP.T10:%016llX",
Bart Van Assche45c37ca2015-05-18 13:25:10 +02002733 be64_to_cpu(target->id_ext));
Roland Dreieraef9ec32005-11-02 14:07:13 -08002734
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002735 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002736 return -ENODEV;
2737
FUJITA Tomonori32368222007-06-27 16:33:12 +09002738 memcpy(ids.port_id, &target->id_ext, 8);
2739 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09002740 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori32368222007-06-27 16:33:12 +09002741 rport = srp_rport_add(target->scsi_host, &ids);
2742 if (IS_ERR(rport)) {
2743 scsi_remove_host(target->scsi_host);
2744 return PTR_ERR(rport);
2745 }
2746
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02002747 rport->lld_data = target;
Bart Van Assche9dd69a62013-10-26 14:32:30 +02002748 target->rport = rport;
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02002749
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002750 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002751 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002752 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002753
Roland Dreieraef9ec32005-11-02 14:07:13 -08002754 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07002755 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002756
Bart Van Asschec014c8c2015-05-18 13:23:57 +02002757 if (srp_connected_ch(target) < target->ch_count ||
2758 target->qp_in_error) {
Bart Van Assche34aa6542014-10-30 14:47:22 +01002759 shost_printk(KERN_INFO, target->scsi_host,
2760 PFX "SCSI scan failed - removing SCSI host\n");
2761 srp_queue_remove_work(target);
2762 goto out;
2763 }
2764
2765 pr_debug(PFX "%s: SCSI scan succeeded - detected %d LUNs\n",
2766 dev_name(&target->scsi_host->shost_gendev),
2767 srp_sdev_count(target->scsi_host));
2768
2769 spin_lock_irq(&target->lock);
2770 if (target->state == SRP_TARGET_SCANNING)
2771 target->state = SRP_TARGET_LIVE;
2772 spin_unlock_irq(&target->lock);
2773
2774out:
Roland Dreieraef9ec32005-11-02 14:07:13 -08002775 return 0;
2776}
2777
Tony Jonesee959b02008-02-22 00:13:36 +01002778static void srp_release_dev(struct device *dev)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002779{
2780 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01002781 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002782
2783 complete(&host->released);
2784}
2785
2786static struct class srp_class = {
2787 .name = "infiniband_srp",
Tony Jonesee959b02008-02-22 00:13:36 +01002788 .dev_release = srp_release_dev
Roland Dreieraef9ec32005-11-02 14:07:13 -08002789};
2790
Bart Van Assche96fc2482013-06-28 14:51:26 +02002791/**
2792 * srp_conn_unique() - check whether the connection to a target is unique
Bart Van Asscheaf246632014-05-20 15:04:21 +02002793 * @host: SRP host.
2794 * @target: SRP target port.
Bart Van Assche96fc2482013-06-28 14:51:26 +02002795 */
2796static bool srp_conn_unique(struct srp_host *host,
2797 struct srp_target_port *target)
2798{
2799 struct srp_target_port *t;
2800 bool ret = false;
2801
2802 if (target->state == SRP_TARGET_REMOVED)
2803 goto out;
2804
2805 ret = true;
2806
2807 spin_lock(&host->target_lock);
2808 list_for_each_entry(t, &host->target_list, list) {
2809 if (t != target &&
2810 target->id_ext == t->id_ext &&
2811 target->ioc_guid == t->ioc_guid &&
2812 target->initiator_ext == t->initiator_ext) {
2813 ret = false;
2814 break;
2815 }
2816 }
2817 spin_unlock(&host->target_lock);
2818
2819out:
2820 return ret;
2821}
2822
Roland Dreieraef9ec32005-11-02 14:07:13 -08002823/*
2824 * Target ports are added by writing
2825 *
2826 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
2827 * pkey=<P_Key>,service_id=<service ID>
2828 *
2829 * to the add_target sysfs attribute.
2830 */
2831enum {
2832 SRP_OPT_ERR = 0,
2833 SRP_OPT_ID_EXT = 1 << 0,
2834 SRP_OPT_IOC_GUID = 1 << 1,
2835 SRP_OPT_DGID = 1 << 2,
2836 SRP_OPT_PKEY = 1 << 3,
2837 SRP_OPT_SERVICE_ID = 1 << 4,
2838 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07002839 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07002840 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002841 SRP_OPT_INITIATOR_EXT = 1 << 8,
David Dillow49248642011-01-14 18:23:24 -05002842 SRP_OPT_CMD_SG_ENTRIES = 1 << 9,
David Dillowc07d4242011-01-16 13:57:10 -05002843 SRP_OPT_ALLOW_EXT_SG = 1 << 10,
2844 SRP_OPT_SG_TABLESIZE = 1 << 11,
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002845 SRP_OPT_COMP_VECTOR = 1 << 12,
Vu Pham7bb312e2013-10-26 14:31:27 +02002846 SRP_OPT_TL_RETRY_COUNT = 1 << 13,
Bart Van Assche4d73f952013-10-26 14:40:37 +02002847 SRP_OPT_QUEUE_SIZE = 1 << 14,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002848 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
2849 SRP_OPT_IOC_GUID |
2850 SRP_OPT_DGID |
2851 SRP_OPT_PKEY |
2852 SRP_OPT_SERVICE_ID),
2853};
2854
Steven Whitehousea447c092008-10-13 10:46:57 +01002855static const match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07002856 { SRP_OPT_ID_EXT, "id_ext=%s" },
2857 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
2858 { SRP_OPT_DGID, "dgid=%s" },
2859 { SRP_OPT_PKEY, "pkey=%x" },
2860 { SRP_OPT_SERVICE_ID, "service_id=%s" },
2861 { SRP_OPT_MAX_SECT, "max_sect=%d" },
2862 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07002863 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002864 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
David Dillow49248642011-01-14 18:23:24 -05002865 { SRP_OPT_CMD_SG_ENTRIES, "cmd_sg_entries=%u" },
David Dillowc07d4242011-01-16 13:57:10 -05002866 { SRP_OPT_ALLOW_EXT_SG, "allow_ext_sg=%u" },
2867 { SRP_OPT_SG_TABLESIZE, "sg_tablesize=%u" },
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002868 { SRP_OPT_COMP_VECTOR, "comp_vector=%u" },
Vu Pham7bb312e2013-10-26 14:31:27 +02002869 { SRP_OPT_TL_RETRY_COUNT, "tl_retry_count=%u" },
Bart Van Assche4d73f952013-10-26 14:40:37 +02002870 { SRP_OPT_QUEUE_SIZE, "queue_size=%d" },
Vu Pham52fb2b502006-06-17 20:37:31 -07002871 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002872};
2873
2874static int srp_parse_options(const char *buf, struct srp_target_port *target)
2875{
2876 char *options, *sep_opt;
2877 char *p;
2878 char dgid[3];
2879 substring_t args[MAX_OPT_ARGS];
2880 int opt_mask = 0;
2881 int token;
2882 int ret = -EINVAL;
2883 int i;
2884
2885 options = kstrdup(buf, GFP_KERNEL);
2886 if (!options)
2887 return -ENOMEM;
2888
2889 sep_opt = options;
Sagi Grimberg7dcf9c12014-10-19 18:19:02 +03002890 while ((p = strsep(&sep_opt, ",\n")) != NULL) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002891 if (!*p)
2892 continue;
2893
2894 token = match_token(p, srp_opt_tokens, args);
2895 opt_mask |= token;
2896
2897 switch (token) {
2898 case SRP_OPT_ID_EXT:
2899 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002900 if (!p) {
2901 ret = -ENOMEM;
2902 goto out;
2903 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002904 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2905 kfree(p);
2906 break;
2907
2908 case SRP_OPT_IOC_GUID:
2909 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002910 if (!p) {
2911 ret = -ENOMEM;
2912 goto out;
2913 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002914 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
2915 kfree(p);
2916 break;
2917
2918 case SRP_OPT_DGID:
2919 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002920 if (!p) {
2921 ret = -ENOMEM;
2922 goto out;
2923 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002924 if (strlen(p) != 32) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002925 pr_warn("bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07002926 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002927 goto out;
2928 }
2929
2930 for (i = 0; i < 16; ++i) {
Bart Van Assche747fe002014-10-30 14:48:05 +01002931 strlcpy(dgid, p + i * 2, sizeof(dgid));
2932 if (sscanf(dgid, "%hhx",
2933 &target->orig_dgid.raw[i]) < 1) {
2934 ret = -EINVAL;
2935 kfree(p);
2936 goto out;
2937 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002938 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08002939 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002940 break;
2941
2942 case SRP_OPT_PKEY:
2943 if (match_hex(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002944 pr_warn("bad P_Key parameter '%s'\n", p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002945 goto out;
2946 }
Bart Van Assche747fe002014-10-30 14:48:05 +01002947 target->pkey = cpu_to_be16(token);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002948 break;
2949
2950 case SRP_OPT_SERVICE_ID:
2951 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002952 if (!p) {
2953 ret = -ENOMEM;
2954 goto out;
2955 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002956 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
2957 kfree(p);
2958 break;
2959
2960 case SRP_OPT_MAX_SECT:
2961 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002962 pr_warn("bad max sect parameter '%s'\n", p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002963 goto out;
2964 }
2965 target->scsi_host->max_sectors = token;
2966 break;
2967
Bart Van Assche4d73f952013-10-26 14:40:37 +02002968 case SRP_OPT_QUEUE_SIZE:
2969 if (match_int(args, &token) || token < 1) {
2970 pr_warn("bad queue_size parameter '%s'\n", p);
2971 goto out;
2972 }
2973 target->scsi_host->can_queue = token;
2974 target->queue_size = token + SRP_RSP_SQ_SIZE +
2975 SRP_TSK_MGMT_SQ_SIZE;
2976 if (!(opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
2977 target->scsi_host->cmd_per_lun = token;
2978 break;
2979
Vu Pham52fb2b502006-06-17 20:37:31 -07002980 case SRP_OPT_MAX_CMD_PER_LUN:
Bart Van Assche4d73f952013-10-26 14:40:37 +02002981 if (match_int(args, &token) || token < 1) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002982 pr_warn("bad max cmd_per_lun parameter '%s'\n",
2983 p);
Vu Pham52fb2b502006-06-17 20:37:31 -07002984 goto out;
2985 }
Bart Van Assche4d73f952013-10-26 14:40:37 +02002986 target->scsi_host->cmd_per_lun = token;
Vu Pham52fb2b502006-06-17 20:37:31 -07002987 break;
2988
Ramachandra K0c0450db2006-06-17 20:37:38 -07002989 case SRP_OPT_IO_CLASS:
2990 if (match_hex(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002991 pr_warn("bad IO class parameter '%s'\n", p);
Ramachandra K0c0450db2006-06-17 20:37:38 -07002992 goto out;
2993 }
2994 if (token != SRP_REV10_IB_IO_CLASS &&
2995 token != SRP_REV16A_IB_IO_CLASS) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002996 pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n",
2997 token, SRP_REV10_IB_IO_CLASS,
2998 SRP_REV16A_IB_IO_CLASS);
Ramachandra K0c0450db2006-06-17 20:37:38 -07002999 goto out;
3000 }
3001 target->io_class = token;
3002 break;
3003
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02003004 case SRP_OPT_INITIATOR_EXT:
3005 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02003006 if (!p) {
3007 ret = -ENOMEM;
3008 goto out;
3009 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02003010 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
3011 kfree(p);
3012 break;
3013
David Dillow49248642011-01-14 18:23:24 -05003014 case SRP_OPT_CMD_SG_ENTRIES:
3015 if (match_int(args, &token) || token < 1 || token > 255) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003016 pr_warn("bad max cmd_sg_entries parameter '%s'\n",
3017 p);
David Dillow49248642011-01-14 18:23:24 -05003018 goto out;
3019 }
3020 target->cmd_sg_cnt = token;
3021 break;
3022
David Dillowc07d4242011-01-16 13:57:10 -05003023 case SRP_OPT_ALLOW_EXT_SG:
3024 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003025 pr_warn("bad allow_ext_sg parameter '%s'\n", p);
David Dillowc07d4242011-01-16 13:57:10 -05003026 goto out;
3027 }
3028 target->allow_ext_sg = !!token;
3029 break;
3030
3031 case SRP_OPT_SG_TABLESIZE:
3032 if (match_int(args, &token) || token < 1 ||
3033 token > SCSI_MAX_SG_CHAIN_SEGMENTS) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003034 pr_warn("bad max sg_tablesize parameter '%s'\n",
3035 p);
David Dillowc07d4242011-01-16 13:57:10 -05003036 goto out;
3037 }
3038 target->sg_tablesize = token;
3039 break;
3040
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02003041 case SRP_OPT_COMP_VECTOR:
3042 if (match_int(args, &token) || token < 0) {
3043 pr_warn("bad comp_vector parameter '%s'\n", p);
3044 goto out;
3045 }
3046 target->comp_vector = token;
3047 break;
3048
Vu Pham7bb312e2013-10-26 14:31:27 +02003049 case SRP_OPT_TL_RETRY_COUNT:
3050 if (match_int(args, &token) || token < 2 || token > 7) {
3051 pr_warn("bad tl_retry_count parameter '%s' (must be a number between 2 and 7)\n",
3052 p);
3053 goto out;
3054 }
3055 target->tl_retry_count = token;
3056 break;
3057
Roland Dreieraef9ec32005-11-02 14:07:13 -08003058 default:
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003059 pr_warn("unknown parameter or missing value '%s' in target creation request\n",
3060 p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003061 goto out;
3062 }
3063 }
3064
3065 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
3066 ret = 0;
3067 else
3068 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
3069 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
3070 !(srp_opt_tokens[i].token & opt_mask))
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003071 pr_warn("target creation request is missing parameter '%s'\n",
3072 srp_opt_tokens[i].pattern);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003073
Bart Van Assche4d73f952013-10-26 14:40:37 +02003074 if (target->scsi_host->cmd_per_lun > target->scsi_host->can_queue
3075 && (opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
3076 pr_warn("cmd_per_lun = %d > queue_size = %d\n",
3077 target->scsi_host->cmd_per_lun,
3078 target->scsi_host->can_queue);
3079
Roland Dreieraef9ec32005-11-02 14:07:13 -08003080out:
3081 kfree(options);
3082 return ret;
3083}
3084
Tony Jonesee959b02008-02-22 00:13:36 +01003085static ssize_t srp_create_target(struct device *dev,
3086 struct device_attribute *attr,
Roland Dreieraef9ec32005-11-02 14:07:13 -08003087 const char *buf, size_t count)
3088{
3089 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01003090 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003091 struct Scsi_Host *target_host;
3092 struct srp_target_port *target;
Bart Van Assche509c07b2014-10-30 14:48:30 +01003093 struct srp_rdma_ch *ch;
Bart Van Assched1b42892014-05-20 15:07:20 +02003094 struct srp_device *srp_dev = host->srp_dev;
3095 struct ib_device *ibdev = srp_dev->dev;
Bart Van Assched92c0da2014-10-06 17:14:36 +02003096 int ret, node_idx, node, cpu, i;
3097 bool multich = false;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003098
3099 target_host = scsi_host_alloc(&srp_template,
3100 sizeof (struct srp_target_port));
3101 if (!target_host)
3102 return -ENOMEM;
3103
David Dillow49248642011-01-14 18:23:24 -05003104 target_host->transportt = ib_srp_transport_template;
Bart Van Asschefd1b6c42011-07-13 09:19:16 -07003105 target_host->max_channel = 0;
3106 target_host->max_id = 1;
Bart Van Assche985aa492015-05-18 13:27:14 +02003107 target_host->max_lun = -1LL;
Arne Redlich3c8edf02006-11-15 12:43:00 +01003108 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08003109
Roland Dreieraef9ec32005-11-02 14:07:13 -08003110 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003111
David Dillow49248642011-01-14 18:23:24 -05003112 target->io_class = SRP_REV16A_IB_IO_CLASS;
3113 target->scsi_host = target_host;
3114 target->srp_host = host;
Jason Gunthorpee6bf5f482015-07-30 17:22:22 -06003115 target->lkey = host->srp_dev->pd->local_dma_lkey;
David Dillow49248642011-01-14 18:23:24 -05003116 target->rkey = host->srp_dev->mr->rkey;
3117 target->cmd_sg_cnt = cmd_sg_entries;
David Dillowc07d4242011-01-16 13:57:10 -05003118 target->sg_tablesize = indirect_sg_entries ? : cmd_sg_entries;
3119 target->allow_ext_sg = allow_ext_sg;
Vu Pham7bb312e2013-10-26 14:31:27 +02003120 target->tl_retry_count = 7;
Bart Van Assche4d73f952013-10-26 14:40:37 +02003121 target->queue_size = SRP_DEFAULT_QUEUE_SIZE;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003122
Bart Van Assche34aa6542014-10-30 14:47:22 +01003123 /*
3124 * Avoid that the SCSI host can be removed by srp_remove_target()
3125 * before this function returns.
3126 */
3127 scsi_host_get(target->scsi_host);
3128
Bart Van Assche2d7091b2014-03-14 13:52:45 +01003129 mutex_lock(&host->add_target_mutex);
3130
Roland Dreieraef9ec32005-11-02 14:07:13 -08003131 ret = srp_parse_options(buf, target);
3132 if (ret)
Bart Van Asschefb49c8b2015-05-18 13:23:14 +02003133 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003134
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02003135 ret = scsi_init_shared_tag_map(target_host, target_host->can_queue);
3136 if (ret)
Bart Van Asschefb49c8b2015-05-18 13:23:14 +02003137 goto out;
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02003138
Bart Van Assche4d73f952013-10-26 14:40:37 +02003139 target->req_ring_size = target->queue_size - SRP_TSK_MGMT_SQ_SIZE;
3140
Bart Van Assche96fc2482013-06-28 14:51:26 +02003141 if (!srp_conn_unique(target->srp_host, target)) {
3142 shost_printk(KERN_INFO, target->scsi_host,
3143 PFX "Already connected to target port with id_ext=%016llx;ioc_guid=%016llx;initiator_ext=%016llx\n",
3144 be64_to_cpu(target->id_ext),
3145 be64_to_cpu(target->ioc_guid),
3146 be64_to_cpu(target->initiator_ext));
3147 ret = -EEXIST;
Bart Van Asschefb49c8b2015-05-18 13:23:14 +02003148 goto out;
Bart Van Assche96fc2482013-06-28 14:51:26 +02003149 }
3150
Bart Van Assche5cfb1782014-05-20 15:08:34 +02003151 if (!srp_dev->has_fmr && !srp_dev->has_fr && !target->allow_ext_sg &&
Bart Van Assched1b42892014-05-20 15:07:20 +02003152 target->cmd_sg_cnt < target->sg_tablesize) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +02003153 pr_warn("No MR pool and no external indirect descriptors, limiting sg_tablesize to cmd_sg_cnt\n");
David Dillowc07d4242011-01-16 13:57:10 -05003154 target->sg_tablesize = target->cmd_sg_cnt;
3155 }
3156
3157 target_host->sg_tablesize = target->sg_tablesize;
3158 target->indirect_size = target->sg_tablesize *
3159 sizeof (struct srp_direct_buf);
David Dillow49248642011-01-14 18:23:24 -05003160 target->max_iu_len = sizeof (struct srp_cmd) +
3161 sizeof (struct srp_indirect_buf) +
3162 target->cmd_sg_cnt * sizeof (struct srp_direct_buf);
3163
Bart Van Asschec1120f82013-10-26 14:35:08 +02003164 INIT_WORK(&target->tl_err_work, srp_tl_err_work);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00003165 INIT_WORK(&target->remove_work, srp_remove_work);
David Dillow8f26c9f2011-01-14 19:45:50 -05003166 spin_lock_init(&target->lock);
Bart Van Assche747fe002014-10-30 14:48:05 +01003167 ret = ib_query_gid(ibdev, host->port, 0, &target->sgid);
Sagi Grimberg2088ca62014-03-14 13:51:58 +01003168 if (ret)
Bart Van Asschefb49c8b2015-05-18 13:23:14 +02003169 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003170
Bart Van Assched92c0da2014-10-06 17:14:36 +02003171 ret = -ENOMEM;
3172 target->ch_count = max_t(unsigned, num_online_nodes(),
3173 min(ch_count ? :
3174 min(4 * num_online_nodes(),
3175 ibdev->num_comp_vectors),
3176 num_online_cpus()));
3177 target->ch = kcalloc(target->ch_count, sizeof(*target->ch),
3178 GFP_KERNEL);
3179 if (!target->ch)
Bart Van Asschefb49c8b2015-05-18 13:23:14 +02003180 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003181
Bart Van Assched92c0da2014-10-06 17:14:36 +02003182 node_idx = 0;
3183 for_each_online_node(node) {
3184 const int ch_start = (node_idx * target->ch_count /
3185 num_online_nodes());
3186 const int ch_end = ((node_idx + 1) * target->ch_count /
3187 num_online_nodes());
3188 const int cv_start = (node_idx * ibdev->num_comp_vectors /
3189 num_online_nodes() + target->comp_vector)
3190 % ibdev->num_comp_vectors;
3191 const int cv_end = ((node_idx + 1) * ibdev->num_comp_vectors /
3192 num_online_nodes() + target->comp_vector)
3193 % ibdev->num_comp_vectors;
3194 int cpu_idx = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003195
Bart Van Assched92c0da2014-10-06 17:14:36 +02003196 for_each_online_cpu(cpu) {
3197 if (cpu_to_node(cpu) != node)
3198 continue;
3199 if (ch_start + cpu_idx >= ch_end)
3200 continue;
3201 ch = &target->ch[ch_start + cpu_idx];
3202 ch->target = target;
3203 ch->comp_vector = cv_start == cv_end ? cv_start :
3204 cv_start + cpu_idx % (cv_end - cv_start);
3205 spin_lock_init(&ch->lock);
3206 INIT_LIST_HEAD(&ch->free_tx);
3207 ret = srp_new_cm_id(ch);
3208 if (ret)
3209 goto err_disconnect;
3210
3211 ret = srp_create_ch_ib(ch);
3212 if (ret)
3213 goto err_disconnect;
3214
3215 ret = srp_alloc_req_data(ch);
3216 if (ret)
3217 goto err_disconnect;
3218
3219 ret = srp_connect_ch(ch, multich);
3220 if (ret) {
3221 shost_printk(KERN_ERR, target->scsi_host,
3222 PFX "Connection %d/%d failed\n",
3223 ch_start + cpu_idx,
3224 target->ch_count);
3225 if (node_idx == 0 && cpu_idx == 0) {
3226 goto err_disconnect;
3227 } else {
3228 srp_free_ch_ib(target, ch);
3229 srp_free_req_data(target, ch);
3230 target->ch_count = ch - target->ch;
Bart Van Asschec257ea62015-07-31 14:13:22 -07003231 goto connected;
Bart Van Assched92c0da2014-10-06 17:14:36 +02003232 }
3233 }
3234
3235 multich = true;
3236 cpu_idx++;
3237 }
3238 node_idx++;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003239 }
3240
Bart Van Asschec257ea62015-07-31 14:13:22 -07003241connected:
Bart Van Assched92c0da2014-10-06 17:14:36 +02003242 target->scsi_host->nr_hw_queues = target->ch_count;
3243
Roland Dreieraef9ec32005-11-02 14:07:13 -08003244 ret = srp_add_target(host, target);
3245 if (ret)
3246 goto err_disconnect;
3247
Bart Van Assche34aa6542014-10-30 14:47:22 +01003248 if (target->state != SRP_TARGET_REMOVED) {
3249 shost_printk(KERN_DEBUG, target->scsi_host, PFX
3250 "new target: id_ext %016llx ioc_guid %016llx pkey %04x service_id %016llx sgid %pI6 dgid %pI6\n",
3251 be64_to_cpu(target->id_ext),
3252 be64_to_cpu(target->ioc_guid),
Bart Van Assche747fe002014-10-30 14:48:05 +01003253 be16_to_cpu(target->pkey),
Bart Van Assche34aa6542014-10-30 14:47:22 +01003254 be64_to_cpu(target->service_id),
Bart Van Assche747fe002014-10-30 14:48:05 +01003255 target->sgid.raw, target->orig_dgid.raw);
Bart Van Assche34aa6542014-10-30 14:47:22 +01003256 }
Bart Van Asschee7ffde02014-03-14 13:52:21 +01003257
Bart Van Assche2d7091b2014-03-14 13:52:45 +01003258 ret = count;
3259
3260out:
3261 mutex_unlock(&host->add_target_mutex);
Bart Van Assche34aa6542014-10-30 14:47:22 +01003262
3263 scsi_host_put(target->scsi_host);
Bart Van Asschebc44bd12015-08-14 11:01:09 -07003264 if (ret < 0)
3265 scsi_host_put(target->scsi_host);
Bart Van Assche34aa6542014-10-30 14:47:22 +01003266
Bart Van Assche2d7091b2014-03-14 13:52:45 +01003267 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003268
3269err_disconnect:
3270 srp_disconnect_target(target);
3271
Bart Van Assched92c0da2014-10-06 17:14:36 +02003272 for (i = 0; i < target->ch_count; i++) {
3273 ch = &target->ch[i];
3274 srp_free_ch_ib(target, ch);
3275 srp_free_req_data(target, ch);
3276 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08003277
Bart Van Assched92c0da2014-10-06 17:14:36 +02003278 kfree(target->ch);
Bart Van Assche2d7091b2014-03-14 13:52:45 +01003279 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003280}
3281
Tony Jonesee959b02008-02-22 00:13:36 +01003282static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003283
Tony Jonesee959b02008-02-22 00:13:36 +01003284static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
3285 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08003286{
Tony Jonesee959b02008-02-22 00:13:36 +01003287 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003288
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01003289 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003290}
3291
Tony Jonesee959b02008-02-22 00:13:36 +01003292static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003293
Tony Jonesee959b02008-02-22 00:13:36 +01003294static ssize_t show_port(struct device *dev, struct device_attribute *attr,
3295 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08003296{
Tony Jonesee959b02008-02-22 00:13:36 +01003297 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003298
3299 return sprintf(buf, "%d\n", host->port);
3300}
3301
Tony Jonesee959b02008-02-22 00:13:36 +01003302static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003303
Roland Dreierf5358a12006-06-17 20:37:29 -07003304static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08003305{
3306 struct srp_host *host;
3307
3308 host = kzalloc(sizeof *host, GFP_KERNEL);
3309 if (!host)
3310 return NULL;
3311
3312 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07003313 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003314 init_completion(&host->released);
Bart Van Assche2d7091b2014-03-14 13:52:45 +01003315 mutex_init(&host->add_target_mutex);
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01003316 host->srp_dev = device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003317 host->port = port;
3318
Tony Jonesee959b02008-02-22 00:13:36 +01003319 host->dev.class = &srp_class;
3320 host->dev.parent = device->dev->dma_device;
Kay Sieversd927e382009-01-06 10:44:39 -08003321 dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003322
Tony Jonesee959b02008-02-22 00:13:36 +01003323 if (device_register(&host->dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07003324 goto free_host;
Tony Jonesee959b02008-02-22 00:13:36 +01003325 if (device_create_file(&host->dev, &dev_attr_add_target))
Roland Dreieraef9ec32005-11-02 14:07:13 -08003326 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01003327 if (device_create_file(&host->dev, &dev_attr_ibdev))
Roland Dreieraef9ec32005-11-02 14:07:13 -08003328 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01003329 if (device_create_file(&host->dev, &dev_attr_port))
Roland Dreieraef9ec32005-11-02 14:07:13 -08003330 goto err_class;
3331
3332 return host;
3333
3334err_class:
Tony Jonesee959b02008-02-22 00:13:36 +01003335 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003336
Roland Dreierf5358a12006-06-17 20:37:29 -07003337free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08003338 kfree(host);
3339
3340 return NULL;
3341}
3342
3343static void srp_add_one(struct ib_device *device)
3344{
Roland Dreierf5358a12006-06-17 20:37:29 -07003345 struct srp_device *srp_dev;
3346 struct ib_device_attr *dev_attr;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003347 struct srp_host *host;
Hal Rosenstock41390322015-06-29 09:57:00 -04003348 int mr_page_shift, p;
Bart Van Assche52ede082014-05-20 15:07:45 +02003349 u64 max_pages_per_mr;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003350
Roland Dreierf5358a12006-06-17 20:37:29 -07003351 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
3352 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08003353 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003354
Roland Dreierf5358a12006-06-17 20:37:29 -07003355 if (ib_query_device(device, dev_attr)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003356 pr_warn("Query device failed for %s\n", device->name);
Roland Dreierf5358a12006-06-17 20:37:29 -07003357 goto free_attr;
3358 }
3359
3360 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
3361 if (!srp_dev)
3362 goto free_attr;
3363
Bart Van Assched1b42892014-05-20 15:07:20 +02003364 srp_dev->has_fmr = (device->alloc_fmr && device->dealloc_fmr &&
3365 device->map_phys_fmr && device->unmap_fmr);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02003366 srp_dev->has_fr = (dev_attr->device_cap_flags &
3367 IB_DEVICE_MEM_MGT_EXTENSIONS);
3368 if (!srp_dev->has_fmr && !srp_dev->has_fr)
3369 dev_warn(&device->dev, "neither FMR nor FR is supported\n");
3370
3371 srp_dev->use_fast_reg = (srp_dev->has_fr &&
3372 (!srp_dev->has_fmr || prefer_fr));
Bart Van Assched1b42892014-05-20 15:07:20 +02003373
Roland Dreierf5358a12006-06-17 20:37:29 -07003374 /*
3375 * Use the smallest page size supported by the HCA, down to a
David Dillow8f26c9f2011-01-14 19:45:50 -05003376 * minimum of 4096 bytes. We're unlikely to build large sglists
3377 * out of smaller entries.
Roland Dreierf5358a12006-06-17 20:37:29 -07003378 */
Bart Van Assche52ede082014-05-20 15:07:45 +02003379 mr_page_shift = max(12, ffs(dev_attr->page_size_cap) - 1);
3380 srp_dev->mr_page_size = 1 << mr_page_shift;
3381 srp_dev->mr_page_mask = ~((u64) srp_dev->mr_page_size - 1);
3382 max_pages_per_mr = dev_attr->max_mr_size;
3383 do_div(max_pages_per_mr, srp_dev->mr_page_size);
3384 srp_dev->max_pages_per_mr = min_t(u64, SRP_MAX_PAGES_PER_MR,
3385 max_pages_per_mr);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02003386 if (srp_dev->use_fast_reg) {
3387 srp_dev->max_pages_per_mr =
3388 min_t(u32, srp_dev->max_pages_per_mr,
3389 dev_attr->max_fast_reg_page_list_len);
3390 }
Bart Van Assche52ede082014-05-20 15:07:45 +02003391 srp_dev->mr_max_size = srp_dev->mr_page_size *
3392 srp_dev->max_pages_per_mr;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02003393 pr_debug("%s: mr_page_shift = %d, dev_attr->max_mr_size = %#llx, dev_attr->max_fast_reg_page_list_len = %u, max_pages_per_mr = %d, mr_max_size = %#x\n",
Bart Van Assche52ede082014-05-20 15:07:45 +02003394 device->name, mr_page_shift, dev_attr->max_mr_size,
Bart Van Assche5cfb1782014-05-20 15:08:34 +02003395 dev_attr->max_fast_reg_page_list_len,
Bart Van Assche52ede082014-05-20 15:07:45 +02003396 srp_dev->max_pages_per_mr, srp_dev->mr_max_size);
Roland Dreierf5358a12006-06-17 20:37:29 -07003397
3398 INIT_LIST_HEAD(&srp_dev->dev_list);
3399
3400 srp_dev->dev = device;
3401 srp_dev->pd = ib_alloc_pd(device);
3402 if (IS_ERR(srp_dev->pd))
3403 goto free_dev;
3404
3405 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
3406 IB_ACCESS_LOCAL_WRITE |
3407 IB_ACCESS_REMOTE_READ |
3408 IB_ACCESS_REMOTE_WRITE);
3409 if (IS_ERR(srp_dev->mr))
3410 goto err_pd;
3411
Hal Rosenstock41390322015-06-29 09:57:00 -04003412 for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07003413 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003414 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07003415 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003416 }
3417
Roland Dreierf5358a12006-06-17 20:37:29 -07003418 ib_set_client_data(device, &srp_client, srp_dev);
3419
3420 goto free_attr;
3421
3422err_pd:
3423 ib_dealloc_pd(srp_dev->pd);
3424
3425free_dev:
3426 kfree(srp_dev);
3427
3428free_attr:
3429 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003430}
3431
Haggai Eran7c1eb452015-07-30 17:50:14 +03003432static void srp_remove_one(struct ib_device *device, void *client_data)
Roland Dreieraef9ec32005-11-02 14:07:13 -08003433{
Roland Dreierf5358a12006-06-17 20:37:29 -07003434 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003435 struct srp_host *host, *tmp_host;
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00003436 struct srp_target_port *target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003437
Haggai Eran7c1eb452015-07-30 17:50:14 +03003438 srp_dev = client_data;
Dotan Barak1fe0cb82013-06-12 15:20:36 +02003439 if (!srp_dev)
3440 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003441
Roland Dreierf5358a12006-06-17 20:37:29 -07003442 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Tony Jonesee959b02008-02-22 00:13:36 +01003443 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003444 /*
3445 * Wait for the sysfs entry to go away, so that no new
3446 * target ports can be created.
3447 */
3448 wait_for_completion(&host->released);
3449
3450 /*
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00003451 * Remove all target ports.
Roland Dreieraef9ec32005-11-02 14:07:13 -08003452 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07003453 spin_lock(&host->target_lock);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00003454 list_for_each_entry(target, &host->target_list, list)
3455 srp_queue_remove_work(target);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07003456 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003457
3458 /*
Bart Van Asschebcc05912014-07-09 15:57:26 +02003459 * Wait for tl_err and target port removal tasks.
Roland Dreieraef9ec32005-11-02 14:07:13 -08003460 */
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00003461 flush_workqueue(system_long_wq);
Bart Van Asschebcc05912014-07-09 15:57:26 +02003462 flush_workqueue(srp_remove_wq);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003463
Roland Dreieraef9ec32005-11-02 14:07:13 -08003464 kfree(host);
3465 }
3466
Roland Dreierf5358a12006-06-17 20:37:29 -07003467 ib_dereg_mr(srp_dev->mr);
3468 ib_dealloc_pd(srp_dev->pd);
3469
3470 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003471}
3472
FUJITA Tomonori32368222007-06-27 16:33:12 +09003473static struct srp_function_template ib_srp_transport_functions = {
Bart Van Asscheed9b2262013-10-26 14:34:27 +02003474 .has_rport_state = true,
3475 .reset_timer_if_blocked = true,
Bart Van Asschea95cadb2013-10-26 14:37:17 +02003476 .reconnect_delay = &srp_reconnect_delay,
Bart Van Asscheed9b2262013-10-26 14:34:27 +02003477 .fast_io_fail_tmo = &srp_fast_io_fail_tmo,
3478 .dev_loss_tmo = &srp_dev_loss_tmo,
3479 .reconnect = srp_rport_reconnect,
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02003480 .rport_delete = srp_rport_delete,
Bart Van Asscheed9b2262013-10-26 14:34:27 +02003481 .terminate_rport_io = srp_terminate_io,
FUJITA Tomonori32368222007-06-27 16:33:12 +09003482};
3483
Roland Dreieraef9ec32005-11-02 14:07:13 -08003484static int __init srp_init_module(void)
3485{
3486 int ret;
3487
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05003488 BUILD_BUG_ON(FIELD_SIZEOF(struct ib_wc, wr_id) < sizeof(void *));
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00003489
David Dillow49248642011-01-14 18:23:24 -05003490 if (srp_sg_tablesize) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003491 pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n");
David Dillow49248642011-01-14 18:23:24 -05003492 if (!cmd_sg_entries)
3493 cmd_sg_entries = srp_sg_tablesize;
3494 }
3495
3496 if (!cmd_sg_entries)
3497 cmd_sg_entries = SRP_DEF_SG_TABLESIZE;
3498
3499 if (cmd_sg_entries > 255) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003500 pr_warn("Clamping cmd_sg_entries to 255\n");
David Dillow49248642011-01-14 18:23:24 -05003501 cmd_sg_entries = 255;
David Dillow1e89a192008-04-16 21:01:12 -07003502 }
3503
David Dillowc07d4242011-01-16 13:57:10 -05003504 if (!indirect_sg_entries)
3505 indirect_sg_entries = cmd_sg_entries;
3506 else if (indirect_sg_entries < cmd_sg_entries) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003507 pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n",
3508 cmd_sg_entries);
David Dillowc07d4242011-01-16 13:57:10 -05003509 indirect_sg_entries = cmd_sg_entries;
3510 }
3511
Bart Van Asschebcc05912014-07-09 15:57:26 +02003512 srp_remove_wq = create_workqueue("srp_remove");
Wei Yongjunda05be22014-08-14 08:56:22 +08003513 if (!srp_remove_wq) {
3514 ret = -ENOMEM;
Bart Van Asschebcc05912014-07-09 15:57:26 +02003515 goto out;
3516 }
3517
3518 ret = -ENOMEM;
FUJITA Tomonori32368222007-06-27 16:33:12 +09003519 ib_srp_transport_template =
3520 srp_attach_transport(&ib_srp_transport_functions);
3521 if (!ib_srp_transport_template)
Bart Van Asschebcc05912014-07-09 15:57:26 +02003522 goto destroy_wq;
FUJITA Tomonori32368222007-06-27 16:33:12 +09003523
Roland Dreieraef9ec32005-11-02 14:07:13 -08003524 ret = class_register(&srp_class);
3525 if (ret) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003526 pr_err("couldn't register class infiniband_srp\n");
Bart Van Asschebcc05912014-07-09 15:57:26 +02003527 goto release_tr;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003528 }
3529
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003530 ib_sa_register_client(&srp_sa_client);
3531
Roland Dreieraef9ec32005-11-02 14:07:13 -08003532 ret = ib_register_client(&srp_client);
3533 if (ret) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003534 pr_err("couldn't register IB client\n");
Bart Van Asschebcc05912014-07-09 15:57:26 +02003535 goto unreg_sa;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003536 }
3537
Bart Van Asschebcc05912014-07-09 15:57:26 +02003538out:
3539 return ret;
3540
3541unreg_sa:
3542 ib_sa_unregister_client(&srp_sa_client);
3543 class_unregister(&srp_class);
3544
3545release_tr:
3546 srp_release_transport(ib_srp_transport_template);
3547
3548destroy_wq:
3549 destroy_workqueue(srp_remove_wq);
3550 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003551}
3552
3553static void __exit srp_cleanup_module(void)
3554{
3555 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003556 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003557 class_unregister(&srp_class);
FUJITA Tomonori32368222007-06-27 16:33:12 +09003558 srp_release_transport(ib_srp_transport_template);
Bart Van Asschebcc05912014-07-09 15:57:26 +02003559 destroy_workqueue(srp_remove_wq);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003560}
3561
3562module_init(srp_init_module);
3563module_exit(srp_cleanup_module);