blob: 60031b9159d9fe7453aef9a65bbab84844f45f98 [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 Assche03f6fb92015-08-10 17:09:36 -070071static bool prefer_fr = true;
72static bool register_always = true;
Bart Van Asschec222a392016-05-12 10:51:01 -070073static bool never_register;
Roland Dreieraef9ec32005-11-02 14:07:13 -080074static int topspin_workarounds = 1;
75
David Dillow49248642011-01-14 18:23:24 -050076module_param(srp_sg_tablesize, uint, 0444);
77MODULE_PARM_DESC(srp_sg_tablesize, "Deprecated name for cmd_sg_entries");
78
79module_param(cmd_sg_entries, uint, 0444);
80MODULE_PARM_DESC(cmd_sg_entries,
81 "Default number of gather/scatter entries in the SRP command (default is 12, max 255)");
82
David Dillowc07d4242011-01-16 13:57:10 -050083module_param(indirect_sg_entries, uint, 0444);
84MODULE_PARM_DESC(indirect_sg_entries,
Ming Lin65e86172016-04-04 14:48:10 -070085 "Default max number of gather/scatter entries (default is 12, max is " __stringify(SG_MAX_SEGMENTS) ")");
David Dillowc07d4242011-01-16 13:57:10 -050086
87module_param(allow_ext_sg, bool, 0444);
88MODULE_PARM_DESC(allow_ext_sg,
89 "Default behavior when there are more than cmd_sg_entries S/G entries after mapping; fails the request when false (default false)");
90
Roland Dreieraef9ec32005-11-02 14:07:13 -080091module_param(topspin_workarounds, int, 0444);
92MODULE_PARM_DESC(topspin_workarounds,
93 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
94
Bart Van Assche5cfb1782014-05-20 15:08:34 +020095module_param(prefer_fr, bool, 0444);
96MODULE_PARM_DESC(prefer_fr,
97"Whether to use fast registration if both FMR and fast registration are supported");
98
Bart Van Asscheb1b88542014-05-20 15:06:41 +020099module_param(register_always, bool, 0444);
100MODULE_PARM_DESC(register_always,
101 "Use memory registration even for contiguous memory regions");
102
Bart Van Asschec222a392016-05-12 10:51:01 -0700103module_param(never_register, bool, 0444);
104MODULE_PARM_DESC(never_register, "Never register memory");
105
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930106static const struct kernel_param_ops srp_tmo_ops;
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200107
Bart Van Asschea95cadb2013-10-26 14:37:17 +0200108static int srp_reconnect_delay = 10;
109module_param_cb(reconnect_delay, &srp_tmo_ops, &srp_reconnect_delay,
110 S_IRUGO | S_IWUSR);
111MODULE_PARM_DESC(reconnect_delay, "Time between successive reconnect attempts");
112
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200113static int srp_fast_io_fail_tmo = 15;
114module_param_cb(fast_io_fail_tmo, &srp_tmo_ops, &srp_fast_io_fail_tmo,
115 S_IRUGO | S_IWUSR);
116MODULE_PARM_DESC(fast_io_fail_tmo,
117 "Number of seconds between the observation of a transport"
118 " layer error and failing all I/O. \"off\" means that this"
119 " functionality is disabled.");
120
Bart Van Asschea95cadb2013-10-26 14:37:17 +0200121static int srp_dev_loss_tmo = 600;
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200122module_param_cb(dev_loss_tmo, &srp_tmo_ops, &srp_dev_loss_tmo,
123 S_IRUGO | S_IWUSR);
124MODULE_PARM_DESC(dev_loss_tmo,
125 "Maximum number of seconds that the SRP transport should"
126 " insulate transport layer errors. After this time has been"
127 " exceeded the SCSI host is removed. Should be"
128 " between 1 and " __stringify(SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
129 " if fast_io_fail_tmo has not been set. \"off\" means that"
130 " this functionality is disabled.");
131
Bart Van Assched92c0da2014-10-06 17:14:36 +0200132static unsigned ch_count;
133module_param(ch_count, uint, 0444);
134MODULE_PARM_DESC(ch_count,
135 "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.");
136
Roland Dreieraef9ec32005-11-02 14:07:13 -0800137static void srp_add_one(struct ib_device *device);
Haggai Eran7c1eb452015-07-30 17:50:14 +0300138static void srp_remove_one(struct ib_device *device, void *client_data);
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +0100139static void srp_recv_done(struct ib_cq *cq, struct ib_wc *wc);
140static void srp_handle_qp_err(struct ib_cq *cq, struct ib_wc *wc,
141 const char *opname);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800142static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
143
FUJITA Tomonori32368222007-06-27 16:33:12 +0900144static struct scsi_transport_template *ib_srp_transport_template;
Bart Van Asschebcc05912014-07-09 15:57:26 +0200145static struct workqueue_struct *srp_remove_wq;
FUJITA Tomonori32368222007-06-27 16:33:12 +0900146
Roland Dreieraef9ec32005-11-02 14:07:13 -0800147static struct ib_client srp_client = {
148 .name = "srp",
149 .add = srp_add_one,
150 .remove = srp_remove_one
151};
152
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700153static struct ib_sa_client srp_sa_client;
154
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200155static int srp_tmo_get(char *buffer, const struct kernel_param *kp)
156{
157 int tmo = *(int *)kp->arg;
158
159 if (tmo >= 0)
160 return sprintf(buffer, "%d", tmo);
161 else
162 return sprintf(buffer, "off");
163}
164
165static int srp_tmo_set(const char *val, const struct kernel_param *kp)
166{
167 int tmo, res;
168
Sagi Grimberg3fdf70a2015-06-25 13:34:15 +0300169 res = srp_parse_tmo(&tmo, val);
170 if (res)
171 goto out;
172
Bart Van Asschea95cadb2013-10-26 14:37:17 +0200173 if (kp->arg == &srp_reconnect_delay)
174 res = srp_tmo_valid(tmo, srp_fast_io_fail_tmo,
175 srp_dev_loss_tmo);
176 else if (kp->arg == &srp_fast_io_fail_tmo)
177 res = srp_tmo_valid(srp_reconnect_delay, tmo, srp_dev_loss_tmo);
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200178 else
Bart Van Asschea95cadb2013-10-26 14:37:17 +0200179 res = srp_tmo_valid(srp_reconnect_delay, srp_fast_io_fail_tmo,
180 tmo);
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200181 if (res)
182 goto out;
183 *(int *)kp->arg = tmo;
184
185out:
186 return res;
187}
188
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930189static const struct kernel_param_ops srp_tmo_ops = {
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200190 .get = srp_tmo_get,
191 .set = srp_tmo_set,
192};
193
Roland Dreieraef9ec32005-11-02 14:07:13 -0800194static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
195{
196 return (struct srp_target_port *) host->hostdata;
197}
198
199static const char *srp_target_info(struct Scsi_Host *host)
200{
201 return host_to_target(host)->target_name;
202}
203
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700204static int srp_target_is_topspin(struct srp_target_port *target)
205{
206 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700207 static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700208
209 return topspin_workarounds &&
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700210 (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
211 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700212}
213
Roland Dreieraef9ec32005-11-02 14:07:13 -0800214static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
215 gfp_t gfp_mask,
216 enum dma_data_direction direction)
217{
218 struct srp_iu *iu;
219
220 iu = kmalloc(sizeof *iu, gfp_mask);
221 if (!iu)
222 goto out;
223
224 iu->buf = kzalloc(size, gfp_mask);
225 if (!iu->buf)
226 goto out_free_iu;
227
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100228 iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
229 direction);
230 if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800231 goto out_free_buf;
232
233 iu->size = size;
234 iu->direction = direction;
235
236 return iu;
237
238out_free_buf:
239 kfree(iu->buf);
240out_free_iu:
241 kfree(iu);
242out:
243 return NULL;
244}
245
246static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
247{
248 if (!iu)
249 return;
250
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100251 ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
252 iu->direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800253 kfree(iu->buf);
254 kfree(iu);
255}
256
257static void srp_qp_event(struct ib_event *event, void *context)
258{
Sagi Grimberg57363d92015-05-18 13:40:29 +0300259 pr_debug("QP event %s (%d)\n",
260 ib_event_msg(event->event), event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800261}
262
263static int srp_init_qp(struct srp_target_port *target,
264 struct ib_qp *qp)
265{
266 struct ib_qp_attr *attr;
267 int ret;
268
269 attr = kmalloc(sizeof *attr, GFP_KERNEL);
270 if (!attr)
271 return -ENOMEM;
272
Bart Van Assche56b53902014-07-09 15:58:22 +0200273 ret = ib_find_cached_pkey(target->srp_host->srp_dev->dev,
274 target->srp_host->port,
275 be16_to_cpu(target->pkey),
276 &attr->pkey_index);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800277 if (ret)
278 goto out;
279
280 attr->qp_state = IB_QPS_INIT;
281 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
282 IB_ACCESS_REMOTE_WRITE);
283 attr->port_num = target->srp_host->port;
284
285 ret = ib_modify_qp(qp, attr,
286 IB_QP_STATE |
287 IB_QP_PKEY_INDEX |
288 IB_QP_ACCESS_FLAGS |
289 IB_QP_PORT);
290
291out:
292 kfree(attr);
293 return ret;
294}
295
Bart Van Assche509c07b2014-10-30 14:48:30 +0100296static int srp_new_cm_id(struct srp_rdma_ch *ch)
David Dillow9fe4bcf2008-01-08 17:08:52 -0500297{
Bart Van Assche509c07b2014-10-30 14:48:30 +0100298 struct srp_target_port *target = ch->target;
David Dillow9fe4bcf2008-01-08 17:08:52 -0500299 struct ib_cm_id *new_cm_id;
300
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100301 new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
Bart Van Assche509c07b2014-10-30 14:48:30 +0100302 srp_cm_handler, ch);
David Dillow9fe4bcf2008-01-08 17:08:52 -0500303 if (IS_ERR(new_cm_id))
304 return PTR_ERR(new_cm_id);
305
Bart Van Assche509c07b2014-10-30 14:48:30 +0100306 if (ch->cm_id)
307 ib_destroy_cm_id(ch->cm_id);
308 ch->cm_id = new_cm_id;
309 ch->path.sgid = target->sgid;
310 ch->path.dgid = target->orig_dgid;
311 ch->path.pkey = target->pkey;
312 ch->path.service_id = target->service_id;
David Dillow9fe4bcf2008-01-08 17:08:52 -0500313
314 return 0;
315}
316
Bart Van Assched1b42892014-05-20 15:07:20 +0200317static struct ib_fmr_pool *srp_alloc_fmr_pool(struct srp_target_port *target)
318{
319 struct srp_device *dev = target->srp_host->srp_dev;
320 struct ib_fmr_pool_param fmr_param;
321
322 memset(&fmr_param, 0, sizeof(fmr_param));
Bart Van Asschefa9863f2016-04-22 14:13:57 -0700323 fmr_param.pool_size = target->mr_pool_size;
Bart Van Assched1b42892014-05-20 15:07:20 +0200324 fmr_param.dirty_watermark = fmr_param.pool_size / 4;
325 fmr_param.cache = 1;
Bart Van Assche52ede082014-05-20 15:07:45 +0200326 fmr_param.max_pages_per_fmr = dev->max_pages_per_mr;
327 fmr_param.page_shift = ilog2(dev->mr_page_size);
Bart Van Assched1b42892014-05-20 15:07:20 +0200328 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
329 IB_ACCESS_REMOTE_WRITE |
330 IB_ACCESS_REMOTE_READ);
331
332 return ib_create_fmr_pool(dev->pd, &fmr_param);
333}
334
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200335/**
336 * srp_destroy_fr_pool() - free the resources owned by a pool
337 * @pool: Fast registration pool to be destroyed.
338 */
339static void srp_destroy_fr_pool(struct srp_fr_pool *pool)
340{
341 int i;
342 struct srp_fr_desc *d;
343
344 if (!pool)
345 return;
346
347 for (i = 0, d = &pool->desc[0]; i < pool->size; i++, d++) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200348 if (d->mr)
349 ib_dereg_mr(d->mr);
350 }
351 kfree(pool);
352}
353
354/**
355 * srp_create_fr_pool() - allocate and initialize a pool for fast registration
356 * @device: IB device to allocate fast registration descriptors for.
357 * @pd: Protection domain associated with the FR descriptors.
358 * @pool_size: Number of descriptors to allocate.
359 * @max_page_list_len: Maximum fast registration work request page list length.
360 */
361static struct srp_fr_pool *srp_create_fr_pool(struct ib_device *device,
362 struct ib_pd *pd, int pool_size,
363 int max_page_list_len)
364{
365 struct srp_fr_pool *pool;
366 struct srp_fr_desc *d;
367 struct ib_mr *mr;
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200368 int i, ret = -EINVAL;
369
370 if (pool_size <= 0)
371 goto err;
372 ret = -ENOMEM;
373 pool = kzalloc(sizeof(struct srp_fr_pool) +
374 pool_size * sizeof(struct srp_fr_desc), GFP_KERNEL);
375 if (!pool)
376 goto err;
377 pool->size = pool_size;
378 pool->max_page_list_len = max_page_list_len;
379 spin_lock_init(&pool->lock);
380 INIT_LIST_HEAD(&pool->free_list);
381
382 for (i = 0, d = &pool->desc[0]; i < pool->size; i++, d++) {
Sagi Grimberg563b67c2015-07-30 10:32:38 +0300383 mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
384 max_page_list_len);
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200385 if (IS_ERR(mr)) {
386 ret = PTR_ERR(mr);
387 goto destroy_pool;
388 }
389 d->mr = mr;
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200390 list_add_tail(&d->entry, &pool->free_list);
391 }
392
393out:
394 return pool;
395
396destroy_pool:
397 srp_destroy_fr_pool(pool);
398
399err:
400 pool = ERR_PTR(ret);
401 goto out;
402}
403
404/**
405 * srp_fr_pool_get() - obtain a descriptor suitable for fast registration
406 * @pool: Pool to obtain descriptor from.
407 */
408static struct srp_fr_desc *srp_fr_pool_get(struct srp_fr_pool *pool)
409{
410 struct srp_fr_desc *d = NULL;
411 unsigned long flags;
412
413 spin_lock_irqsave(&pool->lock, flags);
414 if (!list_empty(&pool->free_list)) {
415 d = list_first_entry(&pool->free_list, typeof(*d), entry);
416 list_del(&d->entry);
417 }
418 spin_unlock_irqrestore(&pool->lock, flags);
419
420 return d;
421}
422
423/**
424 * srp_fr_pool_put() - put an FR descriptor back in the free list
425 * @pool: Pool the descriptor was allocated from.
426 * @desc: Pointer to an array of fast registration descriptor pointers.
427 * @n: Number of descriptors to put back.
428 *
429 * Note: The caller must already have queued an invalidation request for
430 * desc->mr->rkey before calling this function.
431 */
432static void srp_fr_pool_put(struct srp_fr_pool *pool, struct srp_fr_desc **desc,
433 int n)
434{
435 unsigned long flags;
436 int i;
437
438 spin_lock_irqsave(&pool->lock, flags);
439 for (i = 0; i < n; i++)
440 list_add(&desc[i]->entry, &pool->free_list);
441 spin_unlock_irqrestore(&pool->lock, flags);
442}
443
444static struct srp_fr_pool *srp_alloc_fr_pool(struct srp_target_port *target)
445{
446 struct srp_device *dev = target->srp_host->srp_dev;
447
Bart Van Asschefa9863f2016-04-22 14:13:57 -0700448 return srp_create_fr_pool(dev->dev, dev->pd, target->mr_pool_size,
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200449 dev->max_pages_per_mr);
450}
451
Bart Van Assche7dad6b22014-10-21 18:00:35 +0200452/**
453 * srp_destroy_qp() - destroy an RDMA queue pair
Bart Van Asschef83b2562016-05-12 10:48:48 -0700454 * @qp: RDMA queue pair.
Bart Van Assche7dad6b22014-10-21 18:00:35 +0200455 *
Steve Wise561392d2016-02-17 08:15:42 -0800456 * Drain the qp before destroying it. This avoids that the receive
457 * completion handler can access the queue pair while it is
Bart Van Assche7dad6b22014-10-21 18:00:35 +0200458 * being destroyed.
459 */
Bart Van Asschef83b2562016-05-12 10:48:48 -0700460static void srp_destroy_qp(struct ib_qp *qp)
Bart Van Assche7dad6b22014-10-21 18:00:35 +0200461{
Bart Van Asschef83b2562016-05-12 10:48:48 -0700462 ib_drain_rq(qp);
463 ib_destroy_qp(qp);
Bart Van Assche7dad6b22014-10-21 18:00:35 +0200464}
465
Bart Van Assche509c07b2014-10-30 14:48:30 +0100466static int srp_create_ch_ib(struct srp_rdma_ch *ch)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800467{
Bart Van Assche509c07b2014-10-30 14:48:30 +0100468 struct srp_target_port *target = ch->target;
Bart Van Assche62154b22014-05-20 15:04:45 +0200469 struct srp_device *dev = target->srp_host->srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800470 struct ib_qp_init_attr *init_attr;
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100471 struct ib_cq *recv_cq, *send_cq;
472 struct ib_qp *qp;
Bart Van Assched1b42892014-05-20 15:07:20 +0200473 struct ib_fmr_pool *fmr_pool = NULL;
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200474 struct srp_fr_pool *fr_pool = NULL;
Bart Van Assche509c5f32016-05-12 10:50:35 -0700475 const int m = 1 + dev->use_fast_reg * target->mr_per_cmd * 2;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800476 int ret;
477
478 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
479 if (!init_attr)
480 return -ENOMEM;
481
Steve Wise561392d2016-02-17 08:15:42 -0800482 /* queue_size + 1 for ib_drain_rq() */
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +0100483 recv_cq = ib_alloc_cq(dev->dev, ch, target->queue_size + 1,
484 ch->comp_vector, IB_POLL_SOFTIRQ);
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100485 if (IS_ERR(recv_cq)) {
486 ret = PTR_ERR(recv_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800487 goto err;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800488 }
489
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +0100490 send_cq = ib_alloc_cq(dev->dev, ch, m * target->queue_size,
491 ch->comp_vector, IB_POLL_DIRECT);
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100492 if (IS_ERR(send_cq)) {
493 ret = PTR_ERR(send_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800494 goto err_recv_cq;
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000495 }
496
Roland Dreieraef9ec32005-11-02 14:07:13 -0800497 init_attr->event_handler = srp_qp_event;
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200498 init_attr->cap.max_send_wr = m * target->queue_size;
Bart Van Assche7dad6b22014-10-21 18:00:35 +0200499 init_attr->cap.max_recv_wr = target->queue_size + 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800500 init_attr->cap.max_recv_sge = 1;
501 init_attr->cap.max_send_sge = 1;
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200502 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800503 init_attr->qp_type = IB_QPT_RC;
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100504 init_attr->send_cq = send_cq;
505 init_attr->recv_cq = recv_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800506
Bart Van Assche62154b22014-05-20 15:04:45 +0200507 qp = ib_create_qp(dev->pd, init_attr);
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100508 if (IS_ERR(qp)) {
509 ret = PTR_ERR(qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800510 goto err_send_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800511 }
512
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100513 ret = srp_init_qp(target, qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800514 if (ret)
515 goto err_qp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800516
Bart Van Assche002f1562015-08-10 17:08:44 -0700517 if (dev->use_fast_reg) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200518 fr_pool = srp_alloc_fr_pool(target);
519 if (IS_ERR(fr_pool)) {
520 ret = PTR_ERR(fr_pool);
521 shost_printk(KERN_WARNING, target->scsi_host, PFX
522 "FR pool allocation failed (%d)\n", ret);
523 goto err_qp;
524 }
Bart Van Assche002f1562015-08-10 17:08:44 -0700525 } else if (dev->use_fmr) {
Bart Van Assched1b42892014-05-20 15:07:20 +0200526 fmr_pool = srp_alloc_fmr_pool(target);
527 if (IS_ERR(fmr_pool)) {
528 ret = PTR_ERR(fmr_pool);
529 shost_printk(KERN_WARNING, target->scsi_host, PFX
530 "FMR pool allocation failed (%d)\n", ret);
531 goto err_qp;
532 }
Bart Van Assched1b42892014-05-20 15:07:20 +0200533 }
534
Bart Van Assche509c07b2014-10-30 14:48:30 +0100535 if (ch->qp)
Bart Van Asschef83b2562016-05-12 10:48:48 -0700536 srp_destroy_qp(ch->qp);
Bart Van Assche509c07b2014-10-30 14:48:30 +0100537 if (ch->recv_cq)
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +0100538 ib_free_cq(ch->recv_cq);
Bart Van Assche509c07b2014-10-30 14:48:30 +0100539 if (ch->send_cq)
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +0100540 ib_free_cq(ch->send_cq);
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100541
Bart Van Assche509c07b2014-10-30 14:48:30 +0100542 ch->qp = qp;
543 ch->recv_cq = recv_cq;
544 ch->send_cq = send_cq;
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100545
Sagi Grimberg7fbc67d2015-08-24 19:04:51 +0300546 if (dev->use_fast_reg) {
547 if (ch->fr_pool)
548 srp_destroy_fr_pool(ch->fr_pool);
549 ch->fr_pool = fr_pool;
550 } else if (dev->use_fmr) {
551 if (ch->fmr_pool)
552 ib_destroy_fmr_pool(ch->fmr_pool);
553 ch->fmr_pool = fmr_pool;
554 }
555
Roland Dreierda9d2f02010-02-24 15:07:59 -0800556 kfree(init_attr);
557 return 0;
558
559err_qp:
Bart Van Asschef83b2562016-05-12 10:48:48 -0700560 srp_destroy_qp(qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800561
562err_send_cq:
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +0100563 ib_free_cq(send_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800564
565err_recv_cq:
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +0100566 ib_free_cq(recv_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800567
568err:
Roland Dreieraef9ec32005-11-02 14:07:13 -0800569 kfree(init_attr);
570 return ret;
571}
572
Bart Van Assche4d73f952013-10-26 14:40:37 +0200573/*
574 * Note: this function may be called without srp_alloc_iu_bufs() having been
Bart Van Assche509c07b2014-10-30 14:48:30 +0100575 * invoked. Hence the ch->[rt]x_ring checks.
Bart Van Assche4d73f952013-10-26 14:40:37 +0200576 */
Bart Van Assche509c07b2014-10-30 14:48:30 +0100577static void srp_free_ch_ib(struct srp_target_port *target,
578 struct srp_rdma_ch *ch)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800579{
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200580 struct srp_device *dev = target->srp_host->srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800581 int i;
582
Bart Van Assched92c0da2014-10-06 17:14:36 +0200583 if (!ch->target)
584 return;
585
Bart Van Assche509c07b2014-10-30 14:48:30 +0100586 if (ch->cm_id) {
587 ib_destroy_cm_id(ch->cm_id);
588 ch->cm_id = NULL;
Bart Van Assche394c5952014-10-30 14:46:27 +0100589 }
590
Bart Van Assched92c0da2014-10-06 17:14:36 +0200591 /* If srp_new_cm_id() succeeded but srp_create_ch_ib() not, return. */
592 if (!ch->qp)
593 return;
594
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200595 if (dev->use_fast_reg) {
Bart Van Assche509c07b2014-10-30 14:48:30 +0100596 if (ch->fr_pool)
597 srp_destroy_fr_pool(ch->fr_pool);
Bart Van Assche002f1562015-08-10 17:08:44 -0700598 } else if (dev->use_fmr) {
Bart Van Assche509c07b2014-10-30 14:48:30 +0100599 if (ch->fmr_pool)
600 ib_destroy_fmr_pool(ch->fmr_pool);
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200601 }
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +0100602
Bart Van Asschef83b2562016-05-12 10:48:48 -0700603 srp_destroy_qp(ch->qp);
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +0100604 ib_free_cq(ch->send_cq);
605 ib_free_cq(ch->recv_cq);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800606
Bart Van Assched92c0da2014-10-06 17:14:36 +0200607 /*
608 * Avoid that the SCSI error handler tries to use this channel after
609 * it has been freed. The SCSI error handler can namely continue
610 * trying to perform recovery actions after scsi_remove_host()
611 * returned.
612 */
613 ch->target = NULL;
614
Bart Van Assche509c07b2014-10-30 14:48:30 +0100615 ch->qp = NULL;
616 ch->send_cq = ch->recv_cq = NULL;
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100617
Bart Van Assche509c07b2014-10-30 14:48:30 +0100618 if (ch->rx_ring) {
Bart Van Assche4d73f952013-10-26 14:40:37 +0200619 for (i = 0; i < target->queue_size; ++i)
Bart Van Assche509c07b2014-10-30 14:48:30 +0100620 srp_free_iu(target->srp_host, ch->rx_ring[i]);
621 kfree(ch->rx_ring);
622 ch->rx_ring = NULL;
Bart Van Assche4d73f952013-10-26 14:40:37 +0200623 }
Bart Van Assche509c07b2014-10-30 14:48:30 +0100624 if (ch->tx_ring) {
Bart Van Assche4d73f952013-10-26 14:40:37 +0200625 for (i = 0; i < target->queue_size; ++i)
Bart Van Assche509c07b2014-10-30 14:48:30 +0100626 srp_free_iu(target->srp_host, ch->tx_ring[i]);
627 kfree(ch->tx_ring);
628 ch->tx_ring = NULL;
Bart Van Assche4d73f952013-10-26 14:40:37 +0200629 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800630}
631
632static void srp_path_rec_completion(int status,
633 struct ib_sa_path_rec *pathrec,
Bart Van Assche509c07b2014-10-30 14:48:30 +0100634 void *ch_ptr)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800635{
Bart Van Assche509c07b2014-10-30 14:48:30 +0100636 struct srp_rdma_ch *ch = ch_ptr;
637 struct srp_target_port *target = ch->target;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800638
Bart Van Assche509c07b2014-10-30 14:48:30 +0100639 ch->status = status;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800640 if (status)
David Dillow7aa54bd2008-01-07 18:23:41 -0500641 shost_printk(KERN_ERR, target->scsi_host,
642 PFX "Got failed path rec status %d\n", status);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800643 else
Bart Van Assche509c07b2014-10-30 14:48:30 +0100644 ch->path = *pathrec;
645 complete(&ch->done);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800646}
647
Bart Van Assche509c07b2014-10-30 14:48:30 +0100648static int srp_lookup_path(struct srp_rdma_ch *ch)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800649{
Bart Van Assche509c07b2014-10-30 14:48:30 +0100650 struct srp_target_port *target = ch->target;
Bart Van Asschea702adc2014-03-14 13:53:10 +0100651 int ret;
652
Bart Van Assche509c07b2014-10-30 14:48:30 +0100653 ch->path.numb_path = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800654
Bart Van Assche509c07b2014-10-30 14:48:30 +0100655 init_completion(&ch->done);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800656
Bart Van Assche509c07b2014-10-30 14:48:30 +0100657 ch->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
658 target->srp_host->srp_dev->dev,
659 target->srp_host->port,
660 &ch->path,
661 IB_SA_PATH_REC_SERVICE_ID |
662 IB_SA_PATH_REC_DGID |
663 IB_SA_PATH_REC_SGID |
664 IB_SA_PATH_REC_NUMB_PATH |
665 IB_SA_PATH_REC_PKEY,
666 SRP_PATH_REC_TIMEOUT_MS,
667 GFP_KERNEL,
668 srp_path_rec_completion,
669 ch, &ch->path_query);
670 if (ch->path_query_id < 0)
671 return ch->path_query_id;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800672
Bart Van Assche509c07b2014-10-30 14:48:30 +0100673 ret = wait_for_completion_interruptible(&ch->done);
Bart Van Asschea702adc2014-03-14 13:53:10 +0100674 if (ret < 0)
675 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800676
Bart Van Assche509c07b2014-10-30 14:48:30 +0100677 if (ch->status < 0)
David Dillow7aa54bd2008-01-07 18:23:41 -0500678 shost_printk(KERN_WARNING, target->scsi_host,
679 PFX "Path record query failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800680
Bart Van Assche509c07b2014-10-30 14:48:30 +0100681 return ch->status;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800682}
683
Bart Van Assched92c0da2014-10-06 17:14:36 +0200684static int srp_send_req(struct srp_rdma_ch *ch, bool multich)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800685{
Bart Van Assche509c07b2014-10-30 14:48:30 +0100686 struct srp_target_port *target = ch->target;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800687 struct {
688 struct ib_cm_req_param param;
689 struct srp_login_req priv;
690 } *req = NULL;
691 int status;
692
693 req = kzalloc(sizeof *req, GFP_KERNEL);
694 if (!req)
695 return -ENOMEM;
696
Bart Van Assche509c07b2014-10-30 14:48:30 +0100697 req->param.primary_path = &ch->path;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800698 req->param.alternate_path = NULL;
699 req->param.service_id = target->service_id;
Bart Van Assche509c07b2014-10-30 14:48:30 +0100700 req->param.qp_num = ch->qp->qp_num;
701 req->param.qp_type = ch->qp->qp_type;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800702 req->param.private_data = &req->priv;
703 req->param.private_data_len = sizeof req->priv;
704 req->param.flow_control = 1;
705
706 get_random_bytes(&req->param.starting_psn, 4);
707 req->param.starting_psn &= 0xffffff;
708
709 /*
710 * Pick some arbitrary defaults here; we could make these
711 * module parameters if anyone cared about setting them.
712 */
713 req->param.responder_resources = 4;
714 req->param.remote_cm_response_timeout = 20;
715 req->param.local_cm_response_timeout = 20;
Vu Pham7bb312e2013-10-26 14:31:27 +0200716 req->param.retry_count = target->tl_retry_count;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800717 req->param.rnr_retry_count = 7;
718 req->param.max_cm_retries = 15;
719
720 req->priv.opcode = SRP_LOGIN_REQ;
721 req->priv.tag = 0;
David Dillow49248642011-01-14 18:23:24 -0500722 req->priv.req_it_iu_len = cpu_to_be32(target->max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800723 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
724 SRP_BUF_FORMAT_INDIRECT);
Bart Van Assched92c0da2014-10-06 17:14:36 +0200725 req->priv.req_flags = (multich ? SRP_MULTICHAN_MULTI :
726 SRP_MULTICHAN_SINGLE);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700727 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700728 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450db2006-06-17 20:37:38 -0700729 * port identifier format is 8 bytes of ID extension followed
730 * by 8 bytes of GUID. Older drafts put the two halves in the
731 * opposite order, so that the GUID comes first.
732 *
733 * Targets conforming to these obsolete drafts can be
734 * recognized by the I/O Class they report.
735 */
736 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
737 memcpy(req->priv.initiator_port_id,
Bart Van Assche747fe002014-10-30 14:48:05 +0100738 &target->sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700739 memcpy(req->priv.initiator_port_id + 8,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200740 &target->initiator_ext, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700741 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
742 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
743 } else {
744 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200745 &target->initiator_ext, 8);
746 memcpy(req->priv.initiator_port_id + 8,
Bart Van Assche747fe002014-10-30 14:48:05 +0100747 &target->sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700748 memcpy(req->priv.target_port_id, &target->id_ext, 8);
749 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
750 }
751
Roland Dreieraef9ec32005-11-02 14:07:13 -0800752 /*
753 * Topspin/Cisco SRP targets will reject our login unless we
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200754 * zero out the first 8 bytes of our initiator port ID and set
755 * the second 8 bytes to the local node GUID.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800756 */
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700757 if (srp_target_is_topspin(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500758 shost_printk(KERN_DEBUG, target->scsi_host,
759 PFX "Topspin/Cisco initiator port ID workaround "
760 "activated for target GUID %016llx\n",
Bart Van Assche45c37ca2015-05-18 13:25:10 +0200761 be64_to_cpu(target->ioc_guid));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800762 memset(req->priv.initiator_port_id, 0, 8);
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200763 memcpy(req->priv.initiator_port_id + 8,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100764 &target->srp_host->srp_dev->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800765 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800766
Bart Van Assche509c07b2014-10-30 14:48:30 +0100767 status = ib_send_cm_req(ch->cm_id, &req->param);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800768
769 kfree(req);
770
771 return status;
772}
773
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000774static bool srp_queue_remove_work(struct srp_target_port *target)
775{
776 bool changed = false;
777
778 spin_lock_irq(&target->lock);
779 if (target->state != SRP_TARGET_REMOVED) {
780 target->state = SRP_TARGET_REMOVED;
781 changed = true;
782 }
783 spin_unlock_irq(&target->lock);
784
785 if (changed)
Bart Van Asschebcc05912014-07-09 15:57:26 +0200786 queue_work(srp_remove_wq, &target->remove_work);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000787
788 return changed;
789}
790
Roland Dreieraef9ec32005-11-02 14:07:13 -0800791static void srp_disconnect_target(struct srp_target_port *target)
792{
Bart Van Assched92c0da2014-10-06 17:14:36 +0200793 struct srp_rdma_ch *ch;
794 int i;
Bart Van Assche509c07b2014-10-30 14:48:30 +0100795
Bart Van Asschec014c8c2015-05-18 13:23:57 +0200796 /* XXX should send SRP_I_LOGOUT request */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800797
Bart Van Asschec014c8c2015-05-18 13:23:57 +0200798 for (i = 0; i < target->ch_count; i++) {
799 ch = &target->ch[i];
800 ch->connected = false;
801 if (ch->cm_id && ib_send_cm_dreq(ch->cm_id, NULL, 0)) {
802 shost_printk(KERN_DEBUG, target->scsi_host,
803 PFX "Sending CM DREQ failed\n");
Bart Van Assche294c8752011-12-25 12:18:12 +0000804 }
Roland Dreiere6581052006-05-17 09:13:21 -0700805 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800806}
807
Bart Van Assche509c07b2014-10-30 14:48:30 +0100808static void srp_free_req_data(struct srp_target_port *target,
809 struct srp_rdma_ch *ch)
David Dillow8f26c9f2011-01-14 19:45:50 -0500810{
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200811 struct srp_device *dev = target->srp_host->srp_dev;
812 struct ib_device *ibdev = dev->dev;
David Dillow8f26c9f2011-01-14 19:45:50 -0500813 struct srp_request *req;
814 int i;
815
Bart Van Assche47513cf2015-05-18 13:25:54 +0200816 if (!ch->req_ring)
Bart Van Assche4d73f952013-10-26 14:40:37 +0200817 return;
818
819 for (i = 0; i < target->req_ring_size; ++i) {
Bart Van Assche509c07b2014-10-30 14:48:30 +0100820 req = &ch->req_ring[i];
Sagi Grimberg9a21be52015-10-13 19:11:41 +0300821 if (dev->use_fast_reg) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200822 kfree(req->fr_list);
Sagi Grimberg9a21be52015-10-13 19:11:41 +0300823 } else {
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200824 kfree(req->fmr_list);
Sagi Grimberg9a21be52015-10-13 19:11:41 +0300825 kfree(req->map_page);
826 }
David Dillowc07d4242011-01-16 13:57:10 -0500827 if (req->indirect_dma_addr) {
828 ib_dma_unmap_single(ibdev, req->indirect_dma_addr,
829 target->indirect_size,
830 DMA_TO_DEVICE);
831 }
832 kfree(req->indirect_desc);
David Dillow8f26c9f2011-01-14 19:45:50 -0500833 }
Bart Van Assche4d73f952013-10-26 14:40:37 +0200834
Bart Van Assche509c07b2014-10-30 14:48:30 +0100835 kfree(ch->req_ring);
836 ch->req_ring = NULL;
David Dillow8f26c9f2011-01-14 19:45:50 -0500837}
838
Bart Van Assche509c07b2014-10-30 14:48:30 +0100839static int srp_alloc_req_data(struct srp_rdma_ch *ch)
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200840{
Bart Van Assche509c07b2014-10-30 14:48:30 +0100841 struct srp_target_port *target = ch->target;
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200842 struct srp_device *srp_dev = target->srp_host->srp_dev;
843 struct ib_device *ibdev = srp_dev->dev;
844 struct srp_request *req;
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200845 void *mr_list;
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200846 dma_addr_t dma_addr;
847 int i, ret = -ENOMEM;
848
Bart Van Assche509c07b2014-10-30 14:48:30 +0100849 ch->req_ring = kcalloc(target->req_ring_size, sizeof(*ch->req_ring),
850 GFP_KERNEL);
851 if (!ch->req_ring)
Bart Van Assche4d73f952013-10-26 14:40:37 +0200852 goto out;
853
854 for (i = 0; i < target->req_ring_size; ++i) {
Bart Van Assche509c07b2014-10-30 14:48:30 +0100855 req = &ch->req_ring[i];
Bart Van Assche509c5f32016-05-12 10:50:35 -0700856 mr_list = kmalloc(target->mr_per_cmd * sizeof(void *),
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200857 GFP_KERNEL);
858 if (!mr_list)
859 goto out;
Sagi Grimberg9a21be52015-10-13 19:11:41 +0300860 if (srp_dev->use_fast_reg) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200861 req->fr_list = mr_list;
Sagi Grimberg9a21be52015-10-13 19:11:41 +0300862 } else {
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200863 req->fmr_list = mr_list;
Sagi Grimberg9a21be52015-10-13 19:11:41 +0300864 req->map_page = kmalloc(srp_dev->max_pages_per_mr *
865 sizeof(void *), GFP_KERNEL);
866 if (!req->map_page)
867 goto out;
868 }
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200869 req->indirect_desc = kmalloc(target->indirect_size, GFP_KERNEL);
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200870 if (!req->indirect_desc)
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200871 goto out;
872
873 dma_addr = ib_dma_map_single(ibdev, req->indirect_desc,
874 target->indirect_size,
875 DMA_TO_DEVICE);
876 if (ib_dma_mapping_error(ibdev, dma_addr))
877 goto out;
878
879 req->indirect_dma_addr = dma_addr;
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200880 }
881 ret = 0;
882
883out:
884 return ret;
885}
886
Bart Van Assche683b1592012-01-14 12:40:44 +0000887/**
888 * srp_del_scsi_host_attr() - Remove attributes defined in the host template.
889 * @shost: SCSI host whose attributes to remove from sysfs.
890 *
891 * Note: Any attributes defined in the host template and that did not exist
892 * before invocation of this function will be ignored.
893 */
894static void srp_del_scsi_host_attr(struct Scsi_Host *shost)
895{
896 struct device_attribute **attr;
897
898 for (attr = shost->hostt->shost_attrs; attr && *attr; ++attr)
899 device_remove_file(&shost->shost_dev, *attr);
900}
901
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000902static void srp_remove_target(struct srp_target_port *target)
903{
Bart Van Assched92c0da2014-10-06 17:14:36 +0200904 struct srp_rdma_ch *ch;
905 int i;
Bart Van Assche509c07b2014-10-30 14:48:30 +0100906
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000907 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
908
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000909 srp_del_scsi_host_attr(target->scsi_host);
Bart Van Assche9dd69a62013-10-26 14:32:30 +0200910 srp_rport_get(target->rport);
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000911 srp_remove_host(target->scsi_host);
912 scsi_remove_host(target->scsi_host);
Bart Van Assche93079162013-12-11 17:06:14 +0100913 srp_stop_rport_timers(target->rport);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000914 srp_disconnect_target(target);
Bart Van Assched92c0da2014-10-06 17:14:36 +0200915 for (i = 0; i < target->ch_count; i++) {
916 ch = &target->ch[i];
917 srp_free_ch_ib(target, ch);
918 }
Bart Van Asschec1120f82013-10-26 14:35:08 +0200919 cancel_work_sync(&target->tl_err_work);
Bart Van Assche9dd69a62013-10-26 14:32:30 +0200920 srp_rport_put(target->rport);
Bart Van Assched92c0da2014-10-06 17:14:36 +0200921 for (i = 0; i < target->ch_count; i++) {
922 ch = &target->ch[i];
923 srp_free_req_data(target, ch);
924 }
925 kfree(target->ch);
926 target->ch = NULL;
Vu Pham65d7dd22013-10-10 13:50:29 +0200927
928 spin_lock(&target->srp_host->target_lock);
929 list_del(&target->list);
930 spin_unlock(&target->srp_host->target_lock);
931
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000932 scsi_host_put(target->scsi_host);
933}
934
David Howellsc4028952006-11-22 14:57:56 +0000935static void srp_remove_work(struct work_struct *work)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800936{
David Howellsc4028952006-11-22 14:57:56 +0000937 struct srp_target_port *target =
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000938 container_of(work, struct srp_target_port, remove_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800939
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000940 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800941
Bart Van Assche96fc2482013-06-28 14:51:26 +0200942 srp_remove_target(target);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800943}
944
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +0200945static void srp_rport_delete(struct srp_rport *rport)
946{
947 struct srp_target_port *target = rport->lld_data;
948
949 srp_queue_remove_work(target);
950}
951
Bart Van Asschec014c8c2015-05-18 13:23:57 +0200952/**
953 * srp_connected_ch() - number of connected channels
954 * @target: SRP target port.
955 */
956static int srp_connected_ch(struct srp_target_port *target)
957{
958 int i, c = 0;
959
960 for (i = 0; i < target->ch_count; i++)
961 c += target->ch[i].connected;
962
963 return c;
964}
965
Bart Van Assched92c0da2014-10-06 17:14:36 +0200966static int srp_connect_ch(struct srp_rdma_ch *ch, bool multich)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800967{
Bart Van Assche509c07b2014-10-30 14:48:30 +0100968 struct srp_target_port *target = ch->target;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800969 int ret;
970
Bart Van Asschec014c8c2015-05-18 13:23:57 +0200971 WARN_ON_ONCE(!multich && srp_connected_ch(target) > 0);
Bart Van Assche294c8752011-12-25 12:18:12 +0000972
Bart Van Assche509c07b2014-10-30 14:48:30 +0100973 ret = srp_lookup_path(ch);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800974 if (ret)
Bart Van Assche4d59ad22015-12-01 10:17:32 -0800975 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800976
977 while (1) {
Bart Van Assche509c07b2014-10-30 14:48:30 +0100978 init_completion(&ch->done);
Bart Van Assched92c0da2014-10-06 17:14:36 +0200979 ret = srp_send_req(ch, multich);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800980 if (ret)
Bart Van Assche4d59ad22015-12-01 10:17:32 -0800981 goto out;
Bart Van Assche509c07b2014-10-30 14:48:30 +0100982 ret = wait_for_completion_interruptible(&ch->done);
Bart Van Asschea702adc2014-03-14 13:53:10 +0100983 if (ret < 0)
Bart Van Assche4d59ad22015-12-01 10:17:32 -0800984 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800985
986 /*
987 * The CM event handling code will set status to
988 * SRP_PORT_REDIRECT if we get a port redirect REJ
989 * back, or SRP_DLID_REDIRECT if we get a lid/qp
990 * redirect REJ back.
991 */
Bart Van Assche4d59ad22015-12-01 10:17:32 -0800992 ret = ch->status;
993 switch (ret) {
Roland Dreieraef9ec32005-11-02 14:07:13 -0800994 case 0:
Bart Van Asschec014c8c2015-05-18 13:23:57 +0200995 ch->connected = true;
Bart Van Assche4d59ad22015-12-01 10:17:32 -0800996 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800997
998 case SRP_PORT_REDIRECT:
Bart Van Assche509c07b2014-10-30 14:48:30 +0100999 ret = srp_lookup_path(ch);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001000 if (ret)
Bart Van Assche4d59ad22015-12-01 10:17:32 -08001001 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001002 break;
1003
1004 case SRP_DLID_REDIRECT:
1005 break;
1006
David Dillow9fe4bcf2008-01-08 17:08:52 -05001007 case SRP_STALE_CONN:
David Dillow9fe4bcf2008-01-08 17:08:52 -05001008 shost_printk(KERN_ERR, target->scsi_host, PFX
Bart Van Assche205619f2014-10-30 14:46:55 +01001009 "giving up on stale connection\n");
Bart Van Assche4d59ad22015-12-01 10:17:32 -08001010 ret = -ECONNRESET;
1011 goto out;
David Dillow9fe4bcf2008-01-08 17:08:52 -05001012
Roland Dreieraef9ec32005-11-02 14:07:13 -08001013 default:
Bart Van Assche4d59ad22015-12-01 10:17:32 -08001014 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001015 }
1016 }
Bart Van Assche4d59ad22015-12-01 10:17:32 -08001017
1018out:
1019 return ret <= 0 ? ret : -ENODEV;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001020}
1021
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001022static void srp_inv_rkey_err_done(struct ib_cq *cq, struct ib_wc *wc)
1023{
1024 srp_handle_qp_err(cq, wc, "INV RKEY");
1025}
1026
1027static int srp_inv_rkey(struct srp_request *req, struct srp_rdma_ch *ch,
1028 u32 rkey)
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001029{
1030 struct ib_send_wr *bad_wr;
1031 struct ib_send_wr wr = {
1032 .opcode = IB_WR_LOCAL_INV,
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001033 .next = NULL,
1034 .num_sge = 0,
1035 .send_flags = 0,
1036 .ex.invalidate_rkey = rkey,
1037 };
1038
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001039 wr.wr_cqe = &req->reg_cqe;
1040 req->reg_cqe.done = srp_inv_rkey_err_done;
Bart Van Assche509c07b2014-10-30 14:48:30 +01001041 return ib_post_send(ch->qp, &wr, &bad_wr);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001042}
1043
Roland Dreierd945e1d2006-05-09 10:50:28 -07001044static void srp_unmap_data(struct scsi_cmnd *scmnd,
Bart Van Assche509c07b2014-10-30 14:48:30 +01001045 struct srp_rdma_ch *ch,
Roland Dreierd945e1d2006-05-09 10:50:28 -07001046 struct srp_request *req)
1047{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001048 struct srp_target_port *target = ch->target;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001049 struct srp_device *dev = target->srp_host->srp_dev;
1050 struct ib_device *ibdev = dev->dev;
1051 int i, res;
David Dillow8f26c9f2011-01-14 19:45:50 -05001052
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001053 if (!scsi_sglist(scmnd) ||
Roland Dreierd945e1d2006-05-09 10:50:28 -07001054 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
1055 scmnd->sc_data_direction != DMA_FROM_DEVICE))
1056 return;
1057
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001058 if (dev->use_fast_reg) {
1059 struct srp_fr_desc **pfr;
1060
1061 for (i = req->nmdesc, pfr = req->fr_list; i > 0; i--, pfr++) {
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001062 res = srp_inv_rkey(req, ch, (*pfr)->mr->rkey);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001063 if (res < 0) {
1064 shost_printk(KERN_ERR, target->scsi_host, PFX
1065 "Queueing INV WR for rkey %#x failed (%d)\n",
1066 (*pfr)->mr->rkey, res);
1067 queue_work(system_long_wq,
1068 &target->tl_err_work);
1069 }
1070 }
1071 if (req->nmdesc)
Bart Van Assche509c07b2014-10-30 14:48:30 +01001072 srp_fr_pool_put(ch->fr_pool, req->fr_list,
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001073 req->nmdesc);
Bart Van Assche002f1562015-08-10 17:08:44 -07001074 } else if (dev->use_fmr) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001075 struct ib_pool_fmr **pfmr;
1076
1077 for (i = req->nmdesc, pfmr = req->fmr_list; i > 0; i--, pfmr++)
1078 ib_fmr_pool_unmap(*pfmr);
1079 }
Roland Dreierf5358a12006-06-17 20:37:29 -07001080
David Dillow8f26c9f2011-01-14 19:45:50 -05001081 ib_dma_unmap_sg(ibdev, scsi_sglist(scmnd), scsi_sg_count(scmnd),
1082 scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001083}
1084
Bart Van Assche22032992012-08-14 13:18:53 +00001085/**
1086 * srp_claim_req - Take ownership of the scmnd associated with a request.
Bart Van Assche509c07b2014-10-30 14:48:30 +01001087 * @ch: SRP RDMA channel.
Bart Van Assche22032992012-08-14 13:18:53 +00001088 * @req: SRP request.
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001089 * @sdev: If not NULL, only take ownership for this SCSI device.
Bart Van Assche22032992012-08-14 13:18:53 +00001090 * @scmnd: If NULL, take ownership of @req->scmnd. If not NULL, only take
1091 * ownership of @req->scmnd if it equals @scmnd.
1092 *
1093 * Return value:
1094 * Either NULL or a pointer to the SCSI command the caller became owner of.
1095 */
Bart Van Assche509c07b2014-10-30 14:48:30 +01001096static struct scsi_cmnd *srp_claim_req(struct srp_rdma_ch *ch,
Bart Van Assche22032992012-08-14 13:18:53 +00001097 struct srp_request *req,
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001098 struct scsi_device *sdev,
Bart Van Assche22032992012-08-14 13:18:53 +00001099 struct scsi_cmnd *scmnd)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001100{
Bart Van Assche94a91742010-11-26 14:50:09 -05001101 unsigned long flags;
1102
Bart Van Assche509c07b2014-10-30 14:48:30 +01001103 spin_lock_irqsave(&ch->lock, flags);
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001104 if (req->scmnd &&
1105 (!sdev || req->scmnd->device == sdev) &&
1106 (!scmnd || req->scmnd == scmnd)) {
Bart Van Assche22032992012-08-14 13:18:53 +00001107 scmnd = req->scmnd;
1108 req->scmnd = NULL;
Bart Van Assche22032992012-08-14 13:18:53 +00001109 } else {
1110 scmnd = NULL;
1111 }
Bart Van Assche509c07b2014-10-30 14:48:30 +01001112 spin_unlock_irqrestore(&ch->lock, flags);
Bart Van Assche22032992012-08-14 13:18:53 +00001113
1114 return scmnd;
1115}
1116
1117/**
Bart Van Assche6ec2ba02016-04-22 14:12:47 -07001118 * srp_free_req() - Unmap data and adjust ch->req_lim.
Bart Van Assche509c07b2014-10-30 14:48:30 +01001119 * @ch: SRP RDMA channel.
Bart Van Asscheaf246632014-05-20 15:04:21 +02001120 * @req: Request to be freed.
1121 * @scmnd: SCSI command associated with @req.
1122 * @req_lim_delta: Amount to be added to @target->req_lim.
Bart Van Assche22032992012-08-14 13:18:53 +00001123 */
Bart Van Assche509c07b2014-10-30 14:48:30 +01001124static void srp_free_req(struct srp_rdma_ch *ch, struct srp_request *req,
1125 struct scsi_cmnd *scmnd, s32 req_lim_delta)
Bart Van Assche22032992012-08-14 13:18:53 +00001126{
1127 unsigned long flags;
1128
Bart Van Assche509c07b2014-10-30 14:48:30 +01001129 srp_unmap_data(scmnd, ch, req);
Bart Van Assche22032992012-08-14 13:18:53 +00001130
Bart Van Assche509c07b2014-10-30 14:48:30 +01001131 spin_lock_irqsave(&ch->lock, flags);
1132 ch->req_lim += req_lim_delta;
Bart Van Assche509c07b2014-10-30 14:48:30 +01001133 spin_unlock_irqrestore(&ch->lock, flags);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001134}
1135
Bart Van Assche509c07b2014-10-30 14:48:30 +01001136static void srp_finish_req(struct srp_rdma_ch *ch, struct srp_request *req,
1137 struct scsi_device *sdev, int result)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001138{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001139 struct scsi_cmnd *scmnd = srp_claim_req(ch, req, sdev, NULL);
Bart Van Assche22032992012-08-14 13:18:53 +00001140
1141 if (scmnd) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01001142 srp_free_req(ch, req, scmnd, 0);
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001143 scmnd->result = result;
Bart Van Assche22032992012-08-14 13:18:53 +00001144 scmnd->scsi_done(scmnd);
Bart Van Assche22032992012-08-14 13:18:53 +00001145 }
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001146}
1147
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001148static void srp_terminate_io(struct srp_rport *rport)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001149{
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001150 struct srp_target_port *target = rport->lld_data;
Bart Van Assched92c0da2014-10-06 17:14:36 +02001151 struct srp_rdma_ch *ch;
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001152 struct Scsi_Host *shost = target->scsi_host;
1153 struct scsi_device *sdev;
Bart Van Assched92c0da2014-10-06 17:14:36 +02001154 int i, j;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001155
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001156 /*
1157 * Invoking srp_terminate_io() while srp_queuecommand() is running
1158 * is not safe. Hence the warning statement below.
1159 */
1160 shost_for_each_device(sdev, shost)
1161 WARN_ON_ONCE(sdev->request_queue->request_fn_active);
1162
Bart Van Assched92c0da2014-10-06 17:14:36 +02001163 for (i = 0; i < target->ch_count; i++) {
1164 ch = &target->ch[i];
Bart Van Assche509c07b2014-10-30 14:48:30 +01001165
Bart Van Assched92c0da2014-10-06 17:14:36 +02001166 for (j = 0; j < target->req_ring_size; ++j) {
1167 struct srp_request *req = &ch->req_ring[j];
1168
1169 srp_finish_req(ch, req, NULL,
1170 DID_TRANSPORT_FAILFAST << 16);
1171 }
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001172 }
1173}
1174
1175/*
1176 * It is up to the caller to ensure that srp_rport_reconnect() calls are
1177 * serialized and that no concurrent srp_queuecommand(), srp_abort(),
1178 * srp_reset_device() or srp_reset_host() calls will occur while this function
1179 * is in progress. One way to realize that is not to call this function
1180 * directly but to call srp_reconnect_rport() instead since that last function
1181 * serializes calls of this function via rport->mutex and also blocks
1182 * srp_queuecommand() calls before invoking this function.
1183 */
1184static int srp_rport_reconnect(struct srp_rport *rport)
1185{
1186 struct srp_target_port *target = rport->lld_data;
Bart Van Assched92c0da2014-10-06 17:14:36 +02001187 struct srp_rdma_ch *ch;
1188 int i, j, ret = 0;
1189 bool multich = false;
Bart Van Assche09be70a2012-03-17 17:18:54 +00001190
Roland Dreieraef9ec32005-11-02 14:07:13 -08001191 srp_disconnect_target(target);
Bart Van Assche34aa6542014-10-30 14:47:22 +01001192
1193 if (target->state == SRP_TARGET_SCANNING)
1194 return -ENODEV;
1195
Roland Dreieraef9ec32005-11-02 14:07:13 -08001196 /*
Bart Van Asschec7c4e7f2013-02-21 17:19:04 +00001197 * Now get a new local CM ID so that we avoid confusing the target in
1198 * case things are really fouled up. Doing so also ensures that all CM
1199 * callbacks will have finished before a new QP is allocated.
Roland Dreieraef9ec32005-11-02 14:07:13 -08001200 */
Bart Van Assched92c0da2014-10-06 17:14:36 +02001201 for (i = 0; i < target->ch_count; i++) {
1202 ch = &target->ch[i];
Bart Van Assched92c0da2014-10-06 17:14:36 +02001203 ret += srp_new_cm_id(ch);
Bart Van Assche536ae142010-11-26 13:58:27 -05001204 }
Bart Van Assched92c0da2014-10-06 17:14:36 +02001205 for (i = 0; i < target->ch_count; i++) {
1206 ch = &target->ch[i];
Bart Van Assched92c0da2014-10-06 17:14:36 +02001207 for (j = 0; j < target->req_ring_size; ++j) {
1208 struct srp_request *req = &ch->req_ring[j];
Roland Dreieraef9ec32005-11-02 14:07:13 -08001209
Bart Van Assched92c0da2014-10-06 17:14:36 +02001210 srp_finish_req(ch, req, NULL, DID_RESET << 16);
1211 }
1212 }
1213 for (i = 0; i < target->ch_count; i++) {
1214 ch = &target->ch[i];
Bart Van Assched92c0da2014-10-06 17:14:36 +02001215 /*
1216 * Whether or not creating a new CM ID succeeded, create a new
1217 * QP. This guarantees that all completion callback function
1218 * invocations have finished before request resetting starts.
1219 */
1220 ret += srp_create_ch_ib(ch);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001221
Bart Van Assched92c0da2014-10-06 17:14:36 +02001222 INIT_LIST_HEAD(&ch->free_tx);
1223 for (j = 0; j < target->queue_size; ++j)
1224 list_add(&ch->tx_ring[j]->list, &ch->free_tx);
1225 }
Bart Van Assche8de9fe32015-05-18 13:23:36 +02001226
1227 target->qp_in_error = false;
1228
Bart Van Assched92c0da2014-10-06 17:14:36 +02001229 for (i = 0; i < target->ch_count; i++) {
1230 ch = &target->ch[i];
Bart Van Asschebbac5cc2015-05-18 13:26:17 +02001231 if (ret)
Bart Van Assched92c0da2014-10-06 17:14:36 +02001232 break;
Bart Van Assched92c0da2014-10-06 17:14:36 +02001233 ret = srp_connect_ch(ch, multich);
1234 multich = true;
1235 }
Bart Van Assche09be70a2012-03-17 17:18:54 +00001236
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001237 if (ret == 0)
1238 shost_printk(KERN_INFO, target->scsi_host,
1239 PFX "reconnect succeeded\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001240
1241 return ret;
1242}
1243
David Dillow8f26c9f2011-01-14 19:45:50 -05001244static void srp_map_desc(struct srp_map_state *state, dma_addr_t dma_addr,
1245 unsigned int dma_len, u32 rkey)
Roland Dreierf5358a12006-06-17 20:37:29 -07001246{
David Dillow8f26c9f2011-01-14 19:45:50 -05001247 struct srp_direct_buf *desc = state->desc;
1248
Bart Van Assche3ae95da2015-08-10 17:08:18 -07001249 WARN_ON_ONCE(!dma_len);
1250
David Dillow8f26c9f2011-01-14 19:45:50 -05001251 desc->va = cpu_to_be64(dma_addr);
1252 desc->key = cpu_to_be32(rkey);
1253 desc->len = cpu_to_be32(dma_len);
1254
1255 state->total_len += dma_len;
1256 state->desc++;
1257 state->ndesc++;
1258}
1259
1260static int srp_map_finish_fmr(struct srp_map_state *state,
Bart Van Assche509c07b2014-10-30 14:48:30 +01001261 struct srp_rdma_ch *ch)
David Dillow8f26c9f2011-01-14 19:45:50 -05001262{
Bart Van Assche186fbc62015-08-10 17:06:29 -07001263 struct srp_target_port *target = ch->target;
1264 struct srp_device *dev = target->srp_host->srp_dev;
Christoph Hellwig5f071772016-09-05 12:56:19 +02001265 struct ib_pd *pd = target->pd;
David Dillow8f26c9f2011-01-14 19:45:50 -05001266 struct ib_pool_fmr *fmr;
Roland Dreierf5358a12006-06-17 20:37:29 -07001267 u64 io_addr = 0;
David Dillow8f26c9f2011-01-14 19:45:50 -05001268
Bart Van Asschef731ed62015-08-10 17:07:27 -07001269 if (state->fmr.next >= state->fmr.end)
1270 return -ENOMEM;
1271
Sagi Grimberg26630e82015-10-13 19:11:38 +03001272 WARN_ON_ONCE(!dev->use_fmr);
1273
1274 if (state->npages == 0)
1275 return 0;
1276
Christoph Hellwig5f071772016-09-05 12:56:19 +02001277 if (state->npages == 1 && (pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)) {
Sagi Grimberg26630e82015-10-13 19:11:38 +03001278 srp_map_desc(state, state->base_dma_addr, state->dma_len,
Christoph Hellwig5f071772016-09-05 12:56:19 +02001279 pd->unsafe_global_rkey);
Sagi Grimberg26630e82015-10-13 19:11:38 +03001280 goto reset_state;
1281 }
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
Sagi Grimberg26630e82015-10-13 19:11:38 +03001294reset_state:
1295 state->npages = 0;
1296 state->dma_len = 0;
1297
David Dillow8f26c9f2011-01-14 19:45:50 -05001298 return 0;
1299}
1300
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001301static void srp_reg_mr_err_done(struct ib_cq *cq, struct ib_wc *wc)
1302{
1303 srp_handle_qp_err(cq, wc, "FAST REG");
1304}
1305
Bart Van Assche509c5f32016-05-12 10:50:35 -07001306/*
1307 * Map up to sg_nents elements of state->sg where *sg_offset_p is the offset
1308 * where to start in the first element. If sg_offset_p != NULL then
1309 * *sg_offset_p is updated to the offset in state->sg[retval] of the first
1310 * byte that has not yet been mapped.
1311 */
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001312static int srp_map_finish_fr(struct srp_map_state *state,
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001313 struct srp_request *req,
Bart Van Assche509c5f32016-05-12 10:50:35 -07001314 struct srp_rdma_ch *ch, int sg_nents,
1315 unsigned int *sg_offset_p)
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001316{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001317 struct srp_target_port *target = ch->target;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001318 struct srp_device *dev = target->srp_host->srp_dev;
Christoph Hellwig5f071772016-09-05 12:56:19 +02001319 struct ib_pd *pd = target->pd;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001320 struct ib_send_wr *bad_wr;
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001321 struct ib_reg_wr wr;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001322 struct srp_fr_desc *desc;
1323 u32 rkey;
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001324 int n, err;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001325
Bart Van Asschef731ed62015-08-10 17:07:27 -07001326 if (state->fr.next >= state->fr.end)
1327 return -ENOMEM;
1328
Sagi Grimberg26630e82015-10-13 19:11:38 +03001329 WARN_ON_ONCE(!dev->use_fast_reg);
1330
Christoph Hellwig5f071772016-09-05 12:56:19 +02001331 if (sg_nents == 1 && (pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)) {
Bart Van Assche509c5f32016-05-12 10:50:35 -07001332 unsigned int sg_offset = sg_offset_p ? *sg_offset_p : 0;
1333
1334 srp_map_desc(state, sg_dma_address(state->sg) + sg_offset,
1335 sg_dma_len(state->sg) - sg_offset,
Christoph Hellwig5f071772016-09-05 12:56:19 +02001336 pd->unsafe_global_rkey);
Bart Van Assche509c5f32016-05-12 10:50:35 -07001337 if (sg_offset_p)
1338 *sg_offset_p = 0;
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001339 return 1;
Sagi Grimberg26630e82015-10-13 19:11:38 +03001340 }
1341
Bart Van Assche509c07b2014-10-30 14:48:30 +01001342 desc = srp_fr_pool_get(ch->fr_pool);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001343 if (!desc)
1344 return -ENOMEM;
1345
1346 rkey = ib_inc_rkey(desc->mr->rkey);
1347 ib_update_fast_reg_key(desc->mr, rkey);
1348
Bart Van Assche509c5f32016-05-12 10:50:35 -07001349 n = ib_map_mr_sg(desc->mr, state->sg, sg_nents, sg_offset_p,
1350 dev->mr_page_size);
Bart Van Assche9d8e7d02016-05-12 10:48:13 -07001351 if (unlikely(n < 0)) {
1352 srp_fr_pool_put(ch->fr_pool, &desc, 1);
Bart Van Assche509c5f32016-05-12 10:50:35 -07001353 pr_debug("%s: ib_map_mr_sg(%d, %d) returned %d.\n",
Bart Van Assche9d8e7d02016-05-12 10:48:13 -07001354 dev_name(&req->scmnd->device->sdev_gendev), sg_nents,
Bart Van Assche509c5f32016-05-12 10:50:35 -07001355 sg_offset_p ? *sg_offset_p : -1, n);
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001356 return n;
Bart Van Assche9d8e7d02016-05-12 10:48:13 -07001357 }
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001358
Bart Van Assche509c5f32016-05-12 10:50:35 -07001359 WARN_ON_ONCE(desc->mr->length == 0);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001360
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001361 req->reg_cqe.done = srp_reg_mr_err_done;
1362
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001363 wr.wr.next = NULL;
1364 wr.wr.opcode = IB_WR_REG_MR;
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001365 wr.wr.wr_cqe = &req->reg_cqe;
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001366 wr.wr.num_sge = 0;
1367 wr.wr.send_flags = 0;
1368 wr.mr = desc->mr;
1369 wr.key = desc->mr->rkey;
1370 wr.access = (IB_ACCESS_LOCAL_WRITE |
1371 IB_ACCESS_REMOTE_READ |
1372 IB_ACCESS_REMOTE_WRITE);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001373
Bart Van Asschef731ed62015-08-10 17:07:27 -07001374 *state->fr.next++ = desc;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001375 state->nmdesc++;
1376
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001377 srp_map_desc(state, desc->mr->iova,
1378 desc->mr->length, desc->mr->rkey);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001379
Sagi Grimberg26630e82015-10-13 19:11:38 +03001380 err = ib_post_send(ch->qp, &wr.wr, &bad_wr);
Bart Van Assche509c5f32016-05-12 10:50:35 -07001381 if (unlikely(err)) {
1382 WARN_ON_ONCE(err == -ENOMEM);
Sagi Grimberg26630e82015-10-13 19:11:38 +03001383 return err;
Bart Van Assche509c5f32016-05-12 10:50:35 -07001384 }
Sagi Grimberg26630e82015-10-13 19:11:38 +03001385
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001386 return n;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001387}
1388
David Dillow8f26c9f2011-01-14 19:45:50 -05001389static int srp_map_sg_entry(struct srp_map_state *state,
Bart Van Assche509c07b2014-10-30 14:48:30 +01001390 struct srp_rdma_ch *ch,
Bart Van Assche52bb8c62016-09-26 12:57:10 -07001391 struct scatterlist *sg)
David Dillow8f26c9f2011-01-14 19:45:50 -05001392{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001393 struct srp_target_port *target = ch->target;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001394 struct srp_device *dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001395 struct ib_device *ibdev = dev->dev;
David Dillow8f26c9f2011-01-14 19:45:50 -05001396 dma_addr_t dma_addr = ib_sg_dma_address(ibdev, sg);
1397 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Bart Van Assche3ae95da2015-08-10 17:08:18 -07001398 unsigned int len = 0;
David Dillow8f26c9f2011-01-14 19:45:50 -05001399 int ret;
Roland Dreierf5358a12006-06-17 20:37:29 -07001400
Bart Van Assche3ae95da2015-08-10 17:08:18 -07001401 WARN_ON_ONCE(!dma_len);
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -07001402
David Dillow8f26c9f2011-01-14 19:45:50 -05001403 while (dma_len) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001404 unsigned offset = dma_addr & ~dev->mr_page_mask;
1405 if (state->npages == dev->max_pages_per_mr || offset != 0) {
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001406 ret = srp_map_finish_fmr(state, ch);
David Dillow8f26c9f2011-01-14 19:45:50 -05001407 if (ret)
1408 return ret;
Roland Dreierf5358a12006-06-17 20:37:29 -07001409 }
1410
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001411 len = min_t(unsigned int, dma_len, dev->mr_page_size - offset);
David Dillow8f26c9f2011-01-14 19:45:50 -05001412
1413 if (!state->npages)
1414 state->base_dma_addr = dma_addr;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001415 state->pages[state->npages++] = dma_addr & dev->mr_page_mask;
Bart Van Assche52ede082014-05-20 15:07:45 +02001416 state->dma_len += len;
David Dillow8f26c9f2011-01-14 19:45:50 -05001417 dma_addr += len;
1418 dma_len -= len;
Roland Dreierf5358a12006-06-17 20:37:29 -07001419 }
1420
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001421 /*
1422 * If the last entry of the MR wasn't a full page, then we need to
David Dillow8f26c9f2011-01-14 19:45:50 -05001423 * close it out and start a new one -- we can only merge at page
Bart Van Assche1d3d98c2016-04-22 14:12:10 -07001424 * boundaries.
David Dillow8f26c9f2011-01-14 19:45:50 -05001425 */
Roland Dreierf5358a12006-06-17 20:37:29 -07001426 ret = 0;
Bart Van Assche0e0d3a42015-08-10 17:07:46 -07001427 if (len != dev->mr_page_size)
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001428 ret = srp_map_finish_fmr(state, ch);
Roland Dreierf5358a12006-06-17 20:37:29 -07001429 return ret;
1430}
1431
Sagi Grimberg26630e82015-10-13 19:11:38 +03001432static int srp_map_sg_fmr(struct srp_map_state *state, struct srp_rdma_ch *ch,
1433 struct srp_request *req, struct scatterlist *scat,
1434 int count)
1435{
1436 struct scatterlist *sg;
1437 int i, ret;
1438
Sagi Grimberg26630e82015-10-13 19:11:38 +03001439 state->pages = req->map_page;
1440 state->fmr.next = req->fmr_list;
Bart Van Assche509c5f32016-05-12 10:50:35 -07001441 state->fmr.end = req->fmr_list + ch->target->mr_per_cmd;
Sagi Grimberg26630e82015-10-13 19:11:38 +03001442
1443 for_each_sg(scat, sg, count, i) {
Bart Van Assche52bb8c62016-09-26 12:57:10 -07001444 ret = srp_map_sg_entry(state, ch, sg);
Sagi Grimberg26630e82015-10-13 19:11:38 +03001445 if (ret)
1446 return ret;
1447 }
1448
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001449 ret = srp_map_finish_fmr(state, ch);
Sagi Grimberg26630e82015-10-13 19:11:38 +03001450 if (ret)
1451 return ret;
1452
Sagi Grimberg26630e82015-10-13 19:11:38 +03001453 return 0;
1454}
1455
1456static int srp_map_sg_fr(struct srp_map_state *state, struct srp_rdma_ch *ch,
1457 struct srp_request *req, struct scatterlist *scat,
1458 int count)
1459{
Bart Van Assche509c5f32016-05-12 10:50:35 -07001460 unsigned int sg_offset = 0;
1461
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001462 state->fr.next = req->fr_list;
Bart Van Assche509c5f32016-05-12 10:50:35 -07001463 state->fr.end = req->fr_list + ch->target->mr_per_cmd;
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001464 state->sg = scat;
Sagi Grimberg26630e82015-10-13 19:11:38 +03001465
Bart Van Assche3b59b7a62016-04-22 14:14:43 -07001466 if (count == 0)
1467 return 0;
1468
Bart Van Assche57b0be92015-12-01 10:19:38 -08001469 while (count) {
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001470 int i, n;
1471
Bart Van Assche509c5f32016-05-12 10:50:35 -07001472 n = srp_map_finish_fr(state, req, ch, count, &sg_offset);
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001473 if (unlikely(n < 0))
1474 return n;
1475
Bart Van Assche57b0be92015-12-01 10:19:38 -08001476 count -= n;
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001477 for (i = 0; i < n; i++)
1478 state->sg = sg_next(state->sg);
Sagi Grimberg26630e82015-10-13 19:11:38 +03001479 }
1480
Sagi Grimberg26630e82015-10-13 19:11:38 +03001481 return 0;
1482}
1483
1484static int srp_map_sg_dma(struct srp_map_state *state, struct srp_rdma_ch *ch,
1485 struct srp_request *req, struct scatterlist *scat,
1486 int count)
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001487{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001488 struct srp_target_port *target = ch->target;
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001489 struct srp_device *dev = target->srp_host->srp_dev;
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001490 struct scatterlist *sg;
Sagi Grimberg26630e82015-10-13 19:11:38 +03001491 int i;
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001492
Sagi Grimberg26630e82015-10-13 19:11:38 +03001493 for_each_sg(scat, sg, count, i) {
1494 srp_map_desc(state, ib_sg_dma_address(dev->dev, sg),
1495 ib_sg_dma_len(dev->dev, sg),
Christoph Hellwig5f071772016-09-05 12:56:19 +02001496 target->pd->unsafe_global_rkey);
Bart Van Assche0e0d3a42015-08-10 17:07:46 -07001497 }
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001498
Sagi Grimberg26630e82015-10-13 19:11:38 +03001499 return 0;
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001500}
1501
Bart Van Assche330179f2015-08-10 17:09:05 -07001502/*
1503 * Register the indirect data buffer descriptor with the HCA.
1504 *
1505 * Note: since the indirect data buffer descriptor has been allocated with
1506 * kmalloc() it is guaranteed that this buffer is a physically contiguous
1507 * memory buffer.
1508 */
1509static int srp_map_idb(struct srp_rdma_ch *ch, struct srp_request *req,
1510 void **next_mr, void **end_mr, u32 idb_len,
1511 __be32 *idb_rkey)
1512{
1513 struct srp_target_port *target = ch->target;
1514 struct srp_device *dev = target->srp_host->srp_dev;
1515 struct srp_map_state state;
1516 struct srp_direct_buf idb_desc;
1517 u64 idb_pages[1];
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001518 struct scatterlist idb_sg[1];
Bart Van Assche330179f2015-08-10 17:09:05 -07001519 int ret;
1520
1521 memset(&state, 0, sizeof(state));
1522 memset(&idb_desc, 0, sizeof(idb_desc));
1523 state.gen.next = next_mr;
1524 state.gen.end = end_mr;
1525 state.desc = &idb_desc;
Bart Van Assche330179f2015-08-10 17:09:05 -07001526 state.base_dma_addr = req->indirect_dma_addr;
1527 state.dma_len = idb_len;
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001528
1529 if (dev->use_fast_reg) {
1530 state.sg = idb_sg;
Bart Van Assche54f5c9c2016-04-12 14:39:18 -07001531 sg_init_one(idb_sg, req->indirect_desc, idb_len);
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001532 idb_sg->dma_address = req->indirect_dma_addr; /* hack! */
Christoph Hellwigfc925512015-12-01 10:18:30 -08001533#ifdef CONFIG_NEED_SG_DMA_LENGTH
1534 idb_sg->dma_length = idb_sg->length; /* hack^2 */
1535#endif
Bart Van Assche509c5f32016-05-12 10:50:35 -07001536 ret = srp_map_finish_fr(&state, req, ch, 1, NULL);
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001537 if (ret < 0)
1538 return ret;
Bart Van Assche509c5f32016-05-12 10:50:35 -07001539 WARN_ON_ONCE(ret < 1);
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001540 } else if (dev->use_fmr) {
1541 state.pages = idb_pages;
1542 state.pages[0] = (req->indirect_dma_addr &
1543 dev->mr_page_mask);
1544 state.npages = 1;
1545 ret = srp_map_finish_fmr(&state, ch);
1546 if (ret < 0)
1547 return ret;
1548 } else {
1549 return -EINVAL;
1550 }
Bart Van Assche330179f2015-08-10 17:09:05 -07001551
1552 *idb_rkey = idb_desc.key;
1553
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001554 return 0;
Bart Van Assche330179f2015-08-10 17:09:05 -07001555}
1556
Bart Van Assche509c5f32016-05-12 10:50:35 -07001557#if defined(DYNAMIC_DATA_DEBUG)
1558static void srp_check_mapping(struct srp_map_state *state,
1559 struct srp_rdma_ch *ch, struct srp_request *req,
1560 struct scatterlist *scat, int count)
1561{
1562 struct srp_device *dev = ch->target->srp_host->srp_dev;
1563 struct srp_fr_desc **pfr;
1564 u64 desc_len = 0, mr_len = 0;
1565 int i;
1566
1567 for (i = 0; i < state->ndesc; i++)
1568 desc_len += be32_to_cpu(req->indirect_desc[i].len);
1569 if (dev->use_fast_reg)
1570 for (i = 0, pfr = req->fr_list; i < state->nmdesc; i++, pfr++)
1571 mr_len += (*pfr)->mr->length;
1572 else if (dev->use_fmr)
1573 for (i = 0; i < state->nmdesc; i++)
1574 mr_len += be32_to_cpu(req->indirect_desc[i].len);
1575 if (desc_len != scsi_bufflen(req->scmnd) ||
1576 mr_len > scsi_bufflen(req->scmnd))
1577 pr_err("Inconsistent: scsi len %d <> desc len %lld <> mr len %lld; ndesc %d; nmdesc = %d\n",
1578 scsi_bufflen(req->scmnd), desc_len, mr_len,
1579 state->ndesc, state->nmdesc);
1580}
1581#endif
1582
Bart Van Assche77269cd2016-04-22 14:13:09 -07001583/**
1584 * srp_map_data() - map SCSI data buffer onto an SRP request
1585 * @scmnd: SCSI command to map
1586 * @ch: SRP RDMA channel
1587 * @req: SRP request
1588 *
1589 * Returns the length in bytes of the SRP_CMD IU or a negative value if
1590 * mapping failed.
1591 */
Bart Van Assche509c07b2014-10-30 14:48:30 +01001592static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_rdma_ch *ch,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001593 struct srp_request *req)
1594{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001595 struct srp_target_port *target = ch->target;
Christoph Hellwig5f071772016-09-05 12:56:19 +02001596 struct ib_pd *pd = target->pd;
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001597 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001598 struct srp_cmd *cmd = req->cmd->buf;
Bart Van Assche330179f2015-08-10 17:09:05 -07001599 int len, nents, count, ret;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001600 struct srp_device *dev;
1601 struct ib_device *ibdev;
David Dillow8f26c9f2011-01-14 19:45:50 -05001602 struct srp_map_state state;
1603 struct srp_indirect_buf *indirect_hdr;
Bart Van Assche330179f2015-08-10 17:09:05 -07001604 u32 idb_len, table_len;
1605 __be32 idb_rkey;
David Dillow8f26c9f2011-01-14 19:45:50 -05001606 u8 fmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001607
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001608 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001609 return sizeof (struct srp_cmd);
1610
1611 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
1612 scmnd->sc_data_direction != DMA_TO_DEVICE) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001613 shost_printk(KERN_WARNING, target->scsi_host,
1614 PFX "Unhandled data direction %d\n",
1615 scmnd->sc_data_direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001616 return -EINVAL;
1617 }
1618
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001619 nents = scsi_sg_count(scmnd);
1620 scat = scsi_sglist(scmnd);
Roland Dreiercf368712006-03-24 15:47:26 -08001621
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001622 dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001623 ibdev = dev->dev;
1624
1625 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
David Dillow8f26c9f2011-01-14 19:45:50 -05001626 if (unlikely(count == 0))
1627 return -EIO;
Roland Dreierf5358a12006-06-17 20:37:29 -07001628
1629 fmt = SRP_DATA_DESC_DIRECT;
1630 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -08001631
Christoph Hellwig5f071772016-09-05 12:56:19 +02001632 if (count == 1 && (pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)) {
Roland Dreierf5358a12006-06-17 20:37:29 -07001633 /*
1634 * The midlayer only generated a single gather/scatter
1635 * entry, or DMA mapping coalesced everything to a
1636 * single entry. So a direct descriptor along with
1637 * the DMA MR suffices.
1638 */
Roland Dreieraef9ec32005-11-02 14:07:13 -08001639 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001640
Ralph Campbell85507bc2006-12-12 14:30:55 -08001641 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
Christoph Hellwig5f071772016-09-05 12:56:19 +02001642 buf->key = cpu_to_be32(pd->unsafe_global_rkey);
Ralph Campbell85507bc2006-12-12 14:30:55 -08001643 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Roland Dreiercf368712006-03-24 15:47:26 -08001644
Bart Van Assche52ede082014-05-20 15:07:45 +02001645 req->nmdesc = 0;
David Dillow8f26c9f2011-01-14 19:45:50 -05001646 goto map_complete;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001647 }
1648
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001649 /*
1650 * We have more than one scatter/gather entry, so build our indirect
1651 * descriptor table, trying to merge as many entries as we can.
David Dillow8f26c9f2011-01-14 19:45:50 -05001652 */
1653 indirect_hdr = (void *) cmd->add_data;
1654
David Dillowc07d4242011-01-16 13:57:10 -05001655 ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr,
1656 target->indirect_size, DMA_TO_DEVICE);
1657
David Dillow8f26c9f2011-01-14 19:45:50 -05001658 memset(&state, 0, sizeof(state));
Bart Van Assche9edba792016-06-03 11:40:24 -07001659 state.desc = req->indirect_desc;
Sagi Grimberg26630e82015-10-13 19:11:38 +03001660 if (dev->use_fast_reg)
Bart Van Asschee012f362016-04-22 14:13:35 -07001661 ret = srp_map_sg_fr(&state, ch, req, scat, count);
Sagi Grimberg26630e82015-10-13 19:11:38 +03001662 else if (dev->use_fmr)
Bart Van Asschee012f362016-04-22 14:13:35 -07001663 ret = srp_map_sg_fmr(&state, ch, req, scat, count);
Sagi Grimberg26630e82015-10-13 19:11:38 +03001664 else
Bart Van Asschee012f362016-04-22 14:13:35 -07001665 ret = srp_map_sg_dma(&state, ch, req, scat, count);
1666 req->nmdesc = state.nmdesc;
1667 if (ret < 0)
1668 goto unmap;
David Dillow8f26c9f2011-01-14 19:45:50 -05001669
Bart Van Assche509c5f32016-05-12 10:50:35 -07001670#if defined(DYNAMIC_DEBUG)
1671 {
1672 DEFINE_DYNAMIC_DEBUG_METADATA(ddm,
1673 "Memory mapping consistency check");
1674 if (unlikely(ddm.flags & _DPRINTK_FLAGS_PRINT))
1675 srp_check_mapping(&state, ch, req, scat, count);
1676 }
1677#endif
David Dillow8f26c9f2011-01-14 19:45:50 -05001678
David Dillowc07d4242011-01-16 13:57:10 -05001679 /* We've mapped the request, now pull as much of the indirect
1680 * descriptor table as we can into the command buffer. If this
1681 * target is not using an external indirect table, we are
1682 * guaranteed to fit into the command, as the SCSI layer won't
1683 * give us more S/G entries than we allow.
David Dillow8f26c9f2011-01-14 19:45:50 -05001684 */
David Dillow8f26c9f2011-01-14 19:45:50 -05001685 if (state.ndesc == 1) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001686 /*
1687 * Memory registration collapsed the sg-list into one entry,
David Dillow8f26c9f2011-01-14 19:45:50 -05001688 * so use a direct descriptor.
1689 */
1690 struct srp_direct_buf *buf = (void *) cmd->add_data;
1691
David Dillowc07d4242011-01-16 13:57:10 -05001692 *buf = req->indirect_desc[0];
David Dillow8f26c9f2011-01-14 19:45:50 -05001693 goto map_complete;
1694 }
1695
David Dillowc07d4242011-01-16 13:57:10 -05001696 if (unlikely(target->cmd_sg_cnt < state.ndesc &&
1697 !target->allow_ext_sg)) {
1698 shost_printk(KERN_ERR, target->scsi_host,
1699 "Could not fit S/G list into SRP_CMD\n");
Bart Van Asschee012f362016-04-22 14:13:35 -07001700 ret = -EIO;
1701 goto unmap;
David Dillowc07d4242011-01-16 13:57:10 -05001702 }
1703
1704 count = min(state.ndesc, target->cmd_sg_cnt);
David Dillow8f26c9f2011-01-14 19:45:50 -05001705 table_len = state.ndesc * sizeof (struct srp_direct_buf);
Bart Van Assche330179f2015-08-10 17:09:05 -07001706 idb_len = sizeof(struct srp_indirect_buf) + table_len;
David Dillow8f26c9f2011-01-14 19:45:50 -05001707
1708 fmt = SRP_DATA_DESC_INDIRECT;
1709 len = sizeof(struct srp_cmd) + sizeof (struct srp_indirect_buf);
David Dillowc07d4242011-01-16 13:57:10 -05001710 len += count * sizeof (struct srp_direct_buf);
David Dillow8f26c9f2011-01-14 19:45:50 -05001711
David Dillowc07d4242011-01-16 13:57:10 -05001712 memcpy(indirect_hdr->desc_list, req->indirect_desc,
1713 count * sizeof (struct srp_direct_buf));
David Dillow8f26c9f2011-01-14 19:45:50 -05001714
Christoph Hellwig5f071772016-09-05 12:56:19 +02001715 if (!(pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)) {
Bart Van Assche330179f2015-08-10 17:09:05 -07001716 ret = srp_map_idb(ch, req, state.gen.next, state.gen.end,
1717 idb_len, &idb_rkey);
1718 if (ret < 0)
Bart Van Asschee012f362016-04-22 14:13:35 -07001719 goto unmap;
Bart Van Assche330179f2015-08-10 17:09:05 -07001720 req->nmdesc++;
1721 } else {
Christoph Hellwig5f071772016-09-05 12:56:19 +02001722 idb_rkey = cpu_to_be32(pd->unsafe_global_rkey);
Bart Van Assche330179f2015-08-10 17:09:05 -07001723 }
1724
David Dillowc07d4242011-01-16 13:57:10 -05001725 indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr);
Bart Van Assche330179f2015-08-10 17:09:05 -07001726 indirect_hdr->table_desc.key = idb_rkey;
David Dillow8f26c9f2011-01-14 19:45:50 -05001727 indirect_hdr->table_desc.len = cpu_to_be32(table_len);
1728 indirect_hdr->len = cpu_to_be32(state.total_len);
1729
1730 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
David Dillowc07d4242011-01-16 13:57:10 -05001731 cmd->data_out_desc_cnt = count;
David Dillow8f26c9f2011-01-14 19:45:50 -05001732 else
David Dillowc07d4242011-01-16 13:57:10 -05001733 cmd->data_in_desc_cnt = count;
1734
1735 ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len,
1736 DMA_TO_DEVICE);
David Dillow8f26c9f2011-01-14 19:45:50 -05001737
1738map_complete:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001739 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
1740 cmd->buf_fmt = fmt << 4;
1741 else
1742 cmd->buf_fmt = fmt;
1743
Roland Dreieraef9ec32005-11-02 14:07:13 -08001744 return len;
Bart Van Asschee012f362016-04-22 14:13:35 -07001745
1746unmap:
1747 srp_unmap_data(scmnd, ch, req);
Bart Van Asscheffc548b2016-04-22 14:14:15 -07001748 if (ret == -ENOMEM && req->nmdesc >= target->mr_pool_size)
1749 ret = -E2BIG;
Bart Van Asschee012f362016-04-22 14:13:35 -07001750 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001751}
1752
David Dillow05a1d752010-10-08 14:48:14 -04001753/*
Bart Van Assche76c75b22010-11-26 14:37:47 -05001754 * Return an IU and possible credit to the free pool
1755 */
Bart Van Assche509c07b2014-10-30 14:48:30 +01001756static void srp_put_tx_iu(struct srp_rdma_ch *ch, struct srp_iu *iu,
Bart Van Assche76c75b22010-11-26 14:37:47 -05001757 enum srp_iu_type iu_type)
1758{
1759 unsigned long flags;
1760
Bart Van Assche509c07b2014-10-30 14:48:30 +01001761 spin_lock_irqsave(&ch->lock, flags);
1762 list_add(&iu->list, &ch->free_tx);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001763 if (iu_type != SRP_IU_RSP)
Bart Van Assche509c07b2014-10-30 14:48:30 +01001764 ++ch->req_lim;
1765 spin_unlock_irqrestore(&ch->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001766}
1767
1768/*
Bart Van Assche509c07b2014-10-30 14:48:30 +01001769 * Must be called with ch->lock held to protect req_lim and free_tx.
Bart Van Asschee9684672010-11-26 15:08:38 -05001770 * If IU is not sent, it must be returned using srp_put_tx_iu().
David Dillow05a1d752010-10-08 14:48:14 -04001771 *
1772 * Note:
1773 * An upper limit for the number of allocated information units for each
1774 * request type is:
1775 * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues
1776 * more than Scsi_Host.can_queue requests.
1777 * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE.
1778 * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than
1779 * one unanswered SRP request to an initiator.
1780 */
Bart Van Assche509c07b2014-10-30 14:48:30 +01001781static struct srp_iu *__srp_get_tx_iu(struct srp_rdma_ch *ch,
David Dillow05a1d752010-10-08 14:48:14 -04001782 enum srp_iu_type iu_type)
1783{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001784 struct srp_target_port *target = ch->target;
David Dillow05a1d752010-10-08 14:48:14 -04001785 s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE;
1786 struct srp_iu *iu;
1787
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001788 ib_process_cq_direct(ch->send_cq, -1);
David Dillow05a1d752010-10-08 14:48:14 -04001789
Bart Van Assche509c07b2014-10-30 14:48:30 +01001790 if (list_empty(&ch->free_tx))
David Dillow05a1d752010-10-08 14:48:14 -04001791 return NULL;
1792
1793 /* Initiator responses to target requests do not consume credits */
Bart Van Assche76c75b22010-11-26 14:37:47 -05001794 if (iu_type != SRP_IU_RSP) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01001795 if (ch->req_lim <= rsv) {
Bart Van Assche76c75b22010-11-26 14:37:47 -05001796 ++target->zero_req_lim;
1797 return NULL;
1798 }
1799
Bart Van Assche509c07b2014-10-30 14:48:30 +01001800 --ch->req_lim;
David Dillow05a1d752010-10-08 14:48:14 -04001801 }
1802
Bart Van Assche509c07b2014-10-30 14:48:30 +01001803 iu = list_first_entry(&ch->free_tx, struct srp_iu, list);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001804 list_del(&iu->list);
David Dillow05a1d752010-10-08 14:48:14 -04001805 return iu;
1806}
1807
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001808static void srp_send_done(struct ib_cq *cq, struct ib_wc *wc)
1809{
1810 struct srp_iu *iu = container_of(wc->wr_cqe, struct srp_iu, cqe);
1811 struct srp_rdma_ch *ch = cq->cq_context;
1812
1813 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1814 srp_handle_qp_err(cq, wc, "SEND");
1815 return;
1816 }
1817
1818 list_add(&iu->list, &ch->free_tx);
1819}
1820
Bart Van Assche509c07b2014-10-30 14:48:30 +01001821static int srp_post_send(struct srp_rdma_ch *ch, struct srp_iu *iu, int len)
David Dillow05a1d752010-10-08 14:48:14 -04001822{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001823 struct srp_target_port *target = ch->target;
David Dillow05a1d752010-10-08 14:48:14 -04001824 struct ib_sge list;
1825 struct ib_send_wr wr, *bad_wr;
David Dillow05a1d752010-10-08 14:48:14 -04001826
1827 list.addr = iu->dma;
1828 list.length = len;
David Dillow9af76272010-11-26 15:34:46 -05001829 list.lkey = target->lkey;
David Dillow05a1d752010-10-08 14:48:14 -04001830
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001831 iu->cqe.done = srp_send_done;
1832
David Dillow05a1d752010-10-08 14:48:14 -04001833 wr.next = NULL;
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001834 wr.wr_cqe = &iu->cqe;
David Dillow05a1d752010-10-08 14:48:14 -04001835 wr.sg_list = &list;
1836 wr.num_sge = 1;
1837 wr.opcode = IB_WR_SEND;
1838 wr.send_flags = IB_SEND_SIGNALED;
1839
Bart Van Assche509c07b2014-10-30 14:48:30 +01001840 return ib_post_send(ch->qp, &wr, &bad_wr);
David Dillow05a1d752010-10-08 14:48:14 -04001841}
1842
Bart Van Assche509c07b2014-10-30 14:48:30 +01001843static int srp_post_recv(struct srp_rdma_ch *ch, struct srp_iu *iu)
Bart Van Asschec996bb42010-07-30 10:59:05 +00001844{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001845 struct srp_target_port *target = ch->target;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001846 struct ib_recv_wr wr, *bad_wr;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001847 struct ib_sge list;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001848
1849 list.addr = iu->dma;
1850 list.length = iu->size;
David Dillow9af76272010-11-26 15:34:46 -05001851 list.lkey = target->lkey;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001852
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001853 iu->cqe.done = srp_recv_done;
1854
Bart Van Asschec996bb42010-07-30 10:59:05 +00001855 wr.next = NULL;
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001856 wr.wr_cqe = &iu->cqe;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001857 wr.sg_list = &list;
1858 wr.num_sge = 1;
1859
Bart Van Assche509c07b2014-10-30 14:48:30 +01001860 return ib_post_recv(ch->qp, &wr, &bad_wr);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001861}
1862
Bart Van Assche509c07b2014-10-30 14:48:30 +01001863static void srp_process_rsp(struct srp_rdma_ch *ch, struct srp_rsp *rsp)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001864{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001865 struct srp_target_port *target = ch->target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001866 struct srp_request *req;
1867 struct scsi_cmnd *scmnd;
1868 unsigned long flags;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001869
Roland Dreieraef9ec32005-11-02 14:07:13 -08001870 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01001871 spin_lock_irqsave(&ch->lock, flags);
1872 ch->req_lim += be32_to_cpu(rsp->req_lim_delta);
1873 spin_unlock_irqrestore(&ch->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -05001874
Bart Van Assche509c07b2014-10-30 14:48:30 +01001875 ch->tsk_mgmt_status = -1;
David Dillowf8b6e312010-11-26 13:02:21 -05001876 if (be32_to_cpu(rsp->resp_data_len) >= 4)
Bart Van Assche509c07b2014-10-30 14:48:30 +01001877 ch->tsk_mgmt_status = rsp->data[3];
1878 complete(&ch->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001879 } else {
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02001880 scmnd = scsi_host_find_tag(target->scsi_host, rsp->tag);
1881 if (scmnd) {
1882 req = (void *)scmnd->host_scribble;
1883 scmnd = srp_claim_req(ch, req, NULL, scmnd);
1884 }
Bart Van Assche22032992012-08-14 13:18:53 +00001885 if (!scmnd) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001886 shost_printk(KERN_ERR, target->scsi_host,
Bart Van Assched92c0da2014-10-06 17:14:36 +02001887 "Null scmnd for RSP w/tag %#016llx received on ch %td / QP %#x\n",
1888 rsp->tag, ch - target->ch, ch->qp->qp_num);
Bart Van Assche22032992012-08-14 13:18:53 +00001889
Bart Van Assche509c07b2014-10-30 14:48:30 +01001890 spin_lock_irqsave(&ch->lock, flags);
1891 ch->req_lim += be32_to_cpu(rsp->req_lim_delta);
1892 spin_unlock_irqrestore(&ch->lock, flags);
Bart Van Assche22032992012-08-14 13:18:53 +00001893
1894 return;
1895 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001896 scmnd->result = rsp->status;
1897
1898 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
1899 memcpy(scmnd->sense_buffer, rsp->data +
1900 be32_to_cpu(rsp->resp_data_len),
1901 min_t(int, be32_to_cpu(rsp->sense_data_len),
1902 SCSI_SENSE_BUFFERSIZE));
1903 }
1904
Bart Van Asschee7145312014-07-09 15:57:51 +02001905 if (unlikely(rsp->flags & SRP_RSP_FLAG_DIUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001906 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
Bart Van Asschee7145312014-07-09 15:57:51 +02001907 else if (unlikely(rsp->flags & SRP_RSP_FLAG_DIOVER))
1908 scsi_set_resid(scmnd, -be32_to_cpu(rsp->data_in_res_cnt));
1909 else if (unlikely(rsp->flags & SRP_RSP_FLAG_DOUNDER))
1910 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
1911 else if (unlikely(rsp->flags & SRP_RSP_FLAG_DOOVER))
1912 scsi_set_resid(scmnd, -be32_to_cpu(rsp->data_out_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001913
Bart Van Assche509c07b2014-10-30 14:48:30 +01001914 srp_free_req(ch, req, scmnd,
Bart Van Assche22032992012-08-14 13:18:53 +00001915 be32_to_cpu(rsp->req_lim_delta));
1916
David Dillowf8b6e312010-11-26 13:02:21 -05001917 scmnd->host_scribble = NULL;
1918 scmnd->scsi_done(scmnd);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001919 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001920}
1921
Bart Van Assche509c07b2014-10-30 14:48:30 +01001922static int srp_response_common(struct srp_rdma_ch *ch, s32 req_delta,
David Dillowbb125882010-10-08 14:40:47 -04001923 void *rsp, int len)
1924{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001925 struct srp_target_port *target = ch->target;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001926 struct ib_device *dev = target->srp_host->srp_dev->dev;
David Dillowbb125882010-10-08 14:40:47 -04001927 unsigned long flags;
1928 struct srp_iu *iu;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001929 int err;
David Dillowbb125882010-10-08 14:40:47 -04001930
Bart Van Assche509c07b2014-10-30 14:48:30 +01001931 spin_lock_irqsave(&ch->lock, flags);
1932 ch->req_lim += req_delta;
1933 iu = __srp_get_tx_iu(ch, SRP_IU_RSP);
1934 spin_unlock_irqrestore(&ch->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001935
David Dillowbb125882010-10-08 14:40:47 -04001936 if (!iu) {
1937 shost_printk(KERN_ERR, target->scsi_host, PFX
1938 "no IU available to send response\n");
Bart Van Assche76c75b22010-11-26 14:37:47 -05001939 return 1;
David Dillowbb125882010-10-08 14:40:47 -04001940 }
1941
1942 ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE);
1943 memcpy(iu->buf, rsp, len);
1944 ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE);
1945
Bart Van Assche509c07b2014-10-30 14:48:30 +01001946 err = srp_post_send(ch, iu, len);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001947 if (err) {
David Dillowbb125882010-10-08 14:40:47 -04001948 shost_printk(KERN_ERR, target->scsi_host, PFX
1949 "unable to post response: %d\n", err);
Bart Van Assche509c07b2014-10-30 14:48:30 +01001950 srp_put_tx_iu(ch, iu, SRP_IU_RSP);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001951 }
David Dillowbb125882010-10-08 14:40:47 -04001952
David Dillowbb125882010-10-08 14:40:47 -04001953 return err;
1954}
1955
Bart Van Assche509c07b2014-10-30 14:48:30 +01001956static void srp_process_cred_req(struct srp_rdma_ch *ch,
David Dillowbb125882010-10-08 14:40:47 -04001957 struct srp_cred_req *req)
1958{
1959 struct srp_cred_rsp rsp = {
1960 .opcode = SRP_CRED_RSP,
1961 .tag = req->tag,
1962 };
1963 s32 delta = be32_to_cpu(req->req_lim_delta);
1964
Bart Van Assche509c07b2014-10-30 14:48:30 +01001965 if (srp_response_common(ch, delta, &rsp, sizeof(rsp)))
1966 shost_printk(KERN_ERR, ch->target->scsi_host, PFX
David Dillowbb125882010-10-08 14:40:47 -04001967 "problems processing SRP_CRED_REQ\n");
1968}
1969
Bart Van Assche509c07b2014-10-30 14:48:30 +01001970static void srp_process_aer_req(struct srp_rdma_ch *ch,
David Dillowbb125882010-10-08 14:40:47 -04001971 struct srp_aer_req *req)
1972{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001973 struct srp_target_port *target = ch->target;
David Dillowbb125882010-10-08 14:40:47 -04001974 struct srp_aer_rsp rsp = {
1975 .opcode = SRP_AER_RSP,
1976 .tag = req->tag,
1977 };
1978 s32 delta = be32_to_cpu(req->req_lim_delta);
1979
1980 shost_printk(KERN_ERR, target->scsi_host, PFX
Bart Van Assche985aa492015-05-18 13:27:14 +02001981 "ignoring AER for LUN %llu\n", scsilun_to_int(&req->lun));
David Dillowbb125882010-10-08 14:40:47 -04001982
Bart Van Assche509c07b2014-10-30 14:48:30 +01001983 if (srp_response_common(ch, delta, &rsp, sizeof(rsp)))
David Dillowbb125882010-10-08 14:40:47 -04001984 shost_printk(KERN_ERR, target->scsi_host, PFX
1985 "problems processing SRP_AER_REQ\n");
1986}
1987
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001988static void srp_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001989{
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001990 struct srp_iu *iu = container_of(wc->wr_cqe, struct srp_iu, cqe);
1991 struct srp_rdma_ch *ch = cq->cq_context;
Bart Van Assche509c07b2014-10-30 14:48:30 +01001992 struct srp_target_port *target = ch->target;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001993 struct ib_device *dev = target->srp_host->srp_dev->dev;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001994 int res;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001995 u8 opcode;
1996
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001997 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1998 srp_handle_qp_err(cq, wc, "RECV");
1999 return;
2000 }
2001
Bart Van Assche509c07b2014-10-30 14:48:30 +01002002 ib_dma_sync_single_for_cpu(dev, iu->dma, ch->max_ti_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08002003 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002004
2005 opcode = *(u8 *) iu->buf;
2006
2007 if (0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05002008 shost_printk(KERN_ERR, target->scsi_host,
2009 PFX "recv completion, opcode 0x%02x\n", opcode);
Bart Van Assche7a700812010-07-29 15:56:37 +00002010 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1,
2011 iu->buf, wc->byte_len, true);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002012 }
2013
2014 switch (opcode) {
2015 case SRP_RSP:
Bart Van Assche509c07b2014-10-30 14:48:30 +01002016 srp_process_rsp(ch, iu->buf);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002017 break;
2018
David Dillowbb125882010-10-08 14:40:47 -04002019 case SRP_CRED_REQ:
Bart Van Assche509c07b2014-10-30 14:48:30 +01002020 srp_process_cred_req(ch, iu->buf);
David Dillowbb125882010-10-08 14:40:47 -04002021 break;
2022
2023 case SRP_AER_REQ:
Bart Van Assche509c07b2014-10-30 14:48:30 +01002024 srp_process_aer_req(ch, iu->buf);
David Dillowbb125882010-10-08 14:40:47 -04002025 break;
2026
Roland Dreieraef9ec32005-11-02 14:07:13 -08002027 case SRP_T_LOGOUT:
2028 /* XXX Handle target logout */
David Dillow7aa54bd2008-01-07 18:23:41 -05002029 shost_printk(KERN_WARNING, target->scsi_host,
2030 PFX "Got target logout request\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002031 break;
2032
2033 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05002034 shost_printk(KERN_WARNING, target->scsi_host,
2035 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002036 break;
2037 }
2038
Bart Van Assche509c07b2014-10-30 14:48:30 +01002039 ib_dma_sync_single_for_device(dev, iu->dma, ch->max_ti_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08002040 DMA_FROM_DEVICE);
Bart Van Asschec996bb42010-07-30 10:59:05 +00002041
Bart Van Assche509c07b2014-10-30 14:48:30 +01002042 res = srp_post_recv(ch, iu);
Bart Van Asschec996bb42010-07-30 10:59:05 +00002043 if (res != 0)
2044 shost_printk(KERN_ERR, target->scsi_host,
2045 PFX "Recv failed with error code %d\n", res);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002046}
2047
Bart Van Asschec1120f82013-10-26 14:35:08 +02002048/**
2049 * srp_tl_err_work() - handle a transport layer error
Bart Van Asscheaf246632014-05-20 15:04:21 +02002050 * @work: Work structure embedded in an SRP target port.
Bart Van Asschec1120f82013-10-26 14:35:08 +02002051 *
2052 * Note: This function may get invoked before the rport has been created,
2053 * hence the target->rport test.
2054 */
2055static void srp_tl_err_work(struct work_struct *work)
2056{
2057 struct srp_target_port *target;
2058
2059 target = container_of(work, struct srp_target_port, tl_err_work);
2060 if (target->rport)
2061 srp_start_tl_fail_timers(target->rport);
2062}
2063
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01002064static void srp_handle_qp_err(struct ib_cq *cq, struct ib_wc *wc,
2065 const char *opname)
Bart Van Assche948d1e82011-09-03 09:25:42 +02002066{
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01002067 struct srp_rdma_ch *ch = cq->cq_context;
Bart Van Assche7dad6b22014-10-21 18:00:35 +02002068 struct srp_target_port *target = ch->target;
2069
Bart Van Asschec014c8c2015-05-18 13:23:57 +02002070 if (ch->connected && !target->qp_in_error) {
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01002071 shost_printk(KERN_ERR, target->scsi_host,
2072 PFX "failed %s status %s (%d) for CQE %p\n",
2073 opname, ib_wc_status_msg(wc->status), wc->status,
2074 wc->wr_cqe);
Bart Van Asschec1120f82013-10-26 14:35:08 +02002075 queue_work(system_long_wq, &target->tl_err_work);
Bart Van Assche4f0af692012-11-26 11:16:40 +01002076 }
Bart Van Assche948d1e82011-09-03 09:25:42 +02002077 target->qp_in_error = true;
2078}
2079
Bart Van Assche76c75b22010-11-26 14:37:47 -05002080static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002081{
Bart Van Assche76c75b22010-11-26 14:37:47 -05002082 struct srp_target_port *target = host_to_target(shost);
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002083 struct srp_rport *rport = target->rport;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002084 struct srp_rdma_ch *ch;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002085 struct srp_request *req;
2086 struct srp_iu *iu;
2087 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -08002088 struct ib_device *dev;
Bart Van Assche76c75b22010-11-26 14:37:47 -05002089 unsigned long flags;
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02002090 u32 tag;
2091 u16 idx;
Bart Van Assched1b42892014-05-20 15:07:20 +02002092 int len, ret;
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002093 const bool in_scsi_eh = !in_interrupt() && current == shost->ehandler;
2094
2095 /*
2096 * The SCSI EH thread is the only context from which srp_queuecommand()
2097 * can get invoked for blocked devices (SDEV_BLOCK /
2098 * SDEV_CREATED_BLOCK). Avoid racing with srp_reconnect_rport() by
2099 * locking the rport mutex if invoked from inside the SCSI EH.
2100 */
2101 if (in_scsi_eh)
2102 mutex_lock(&rport->mutex);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002103
Bart Van Assched1b42892014-05-20 15:07:20 +02002104 scmnd->result = srp_chkready(target->rport);
2105 if (unlikely(scmnd->result))
2106 goto err;
Bart Van Assche2ce19e72013-02-21 17:20:00 +00002107
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02002108 WARN_ON_ONCE(scmnd->request->tag < 0);
2109 tag = blk_mq_unique_tag(scmnd->request);
Bart Van Assched92c0da2014-10-06 17:14:36 +02002110 ch = &target->ch[blk_mq_unique_tag_to_hwq(tag)];
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02002111 idx = blk_mq_unique_tag_to_tag(tag);
2112 WARN_ONCE(idx >= target->req_ring_size, "%s: tag %#x: idx %d >= %d\n",
2113 dev_name(&shost->shost_gendev), tag, idx,
2114 target->req_ring_size);
Bart Van Assche509c07b2014-10-30 14:48:30 +01002115
2116 spin_lock_irqsave(&ch->lock, flags);
2117 iu = __srp_get_tx_iu(ch, SRP_IU_CMD);
Bart Van Assche509c07b2014-10-30 14:48:30 +01002118 spin_unlock_irqrestore(&ch->lock, flags);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002119
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02002120 if (!iu)
2121 goto err;
2122
2123 req = &ch->req_ring[idx];
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002124 dev = target->srp_host->srp_dev->dev;
David Dillow49248642011-01-14 18:23:24 -05002125 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08002126 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002127
David Dillowf8b6e312010-11-26 13:02:21 -05002128 scmnd->host_scribble = (void *) req;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002129
2130 cmd = iu->buf;
2131 memset(cmd, 0, sizeof *cmd);
2132
2133 cmd->opcode = SRP_CMD;
Bart Van Assche985aa492015-05-18 13:27:14 +02002134 int_to_scsilun(scmnd->device->lun, &cmd->lun);
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02002135 cmd->tag = tag;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002136 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
2137
Roland Dreieraef9ec32005-11-02 14:07:13 -08002138 req->scmnd = scmnd;
2139 req->cmd = iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002140
Bart Van Assche509c07b2014-10-30 14:48:30 +01002141 len = srp_map_data(scmnd, ch, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002142 if (len < 0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05002143 shost_printk(KERN_ERR, target->scsi_host,
Bart Van Assched1b42892014-05-20 15:07:20 +02002144 PFX "Failed to map data (%d)\n", len);
2145 /*
2146 * If we ran out of memory descriptors (-ENOMEM) because an
2147 * application is queuing many requests with more than
Bart Van Assche52ede082014-05-20 15:07:45 +02002148 * max_pages_per_mr sg-list elements, tell the SCSI mid-layer
Bart Van Assched1b42892014-05-20 15:07:20 +02002149 * to reduce queue depth temporarily.
2150 */
2151 scmnd->result = len == -ENOMEM ?
2152 DID_OK << 16 | QUEUE_FULL << 1 : DID_ERROR << 16;
Bart Van Assche76c75b22010-11-26 14:37:47 -05002153 goto err_iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002154 }
2155
David Dillow49248642011-01-14 18:23:24 -05002156 ib_dma_sync_single_for_device(dev, iu->dma, target->max_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08002157 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002158
Bart Van Assche509c07b2014-10-30 14:48:30 +01002159 if (srp_post_send(ch, iu, len)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05002160 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002161 goto err_unmap;
2162 }
2163
Bart Van Assched1b42892014-05-20 15:07:20 +02002164 ret = 0;
2165
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002166unlock_rport:
2167 if (in_scsi_eh)
2168 mutex_unlock(&rport->mutex);
2169
Bart Van Assched1b42892014-05-20 15:07:20 +02002170 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002171
2172err_unmap:
Bart Van Assche509c07b2014-10-30 14:48:30 +01002173 srp_unmap_data(scmnd, ch, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002174
Bart Van Assche76c75b22010-11-26 14:37:47 -05002175err_iu:
Bart Van Assche509c07b2014-10-30 14:48:30 +01002176 srp_put_tx_iu(ch, iu, SRP_IU_CMD);
Bart Van Assche76c75b22010-11-26 14:37:47 -05002177
Bart Van Assche024ca902014-05-20 15:03:49 +02002178 /*
2179 * Avoid that the loops that iterate over the request ring can
2180 * encounter a dangling SCSI command pointer.
2181 */
2182 req->scmnd = NULL;
2183
Bart Van Assched1b42892014-05-20 15:07:20 +02002184err:
2185 if (scmnd->result) {
2186 scmnd->scsi_done(scmnd);
2187 ret = 0;
2188 } else {
2189 ret = SCSI_MLQUEUE_HOST_BUSY;
2190 }
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002191
Bart Van Assched1b42892014-05-20 15:07:20 +02002192 goto unlock_rport;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002193}
2194
Bart Van Assche4d73f952013-10-26 14:40:37 +02002195/*
2196 * Note: the resources allocated in this function are freed in
Bart Van Assche509c07b2014-10-30 14:48:30 +01002197 * srp_free_ch_ib().
Bart Van Assche4d73f952013-10-26 14:40:37 +02002198 */
Bart Van Assche509c07b2014-10-30 14:48:30 +01002199static int srp_alloc_iu_bufs(struct srp_rdma_ch *ch)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002200{
Bart Van Assche509c07b2014-10-30 14:48:30 +01002201 struct srp_target_port *target = ch->target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002202 int i;
2203
Bart Van Assche509c07b2014-10-30 14:48:30 +01002204 ch->rx_ring = kcalloc(target->queue_size, sizeof(*ch->rx_ring),
2205 GFP_KERNEL);
2206 if (!ch->rx_ring)
Bart Van Assche4d73f952013-10-26 14:40:37 +02002207 goto err_no_ring;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002208 ch->tx_ring = kcalloc(target->queue_size, sizeof(*ch->tx_ring),
2209 GFP_KERNEL);
2210 if (!ch->tx_ring)
Bart Van Assche4d73f952013-10-26 14:40:37 +02002211 goto err_no_ring;
2212
2213 for (i = 0; i < target->queue_size; ++i) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01002214 ch->rx_ring[i] = srp_alloc_iu(target->srp_host,
2215 ch->max_ti_iu_len,
2216 GFP_KERNEL, DMA_FROM_DEVICE);
2217 if (!ch->rx_ring[i])
Roland Dreieraef9ec32005-11-02 14:07:13 -08002218 goto err;
2219 }
2220
Bart Van Assche4d73f952013-10-26 14:40:37 +02002221 for (i = 0; i < target->queue_size; ++i) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01002222 ch->tx_ring[i] = srp_alloc_iu(target->srp_host,
2223 target->max_iu_len,
2224 GFP_KERNEL, DMA_TO_DEVICE);
2225 if (!ch->tx_ring[i])
Roland Dreieraef9ec32005-11-02 14:07:13 -08002226 goto err;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05002227
Bart Van Assche509c07b2014-10-30 14:48:30 +01002228 list_add(&ch->tx_ring[i]->list, &ch->free_tx);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002229 }
2230
2231 return 0;
2232
2233err:
Bart Van Assche4d73f952013-10-26 14:40:37 +02002234 for (i = 0; i < target->queue_size; ++i) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01002235 srp_free_iu(target->srp_host, ch->rx_ring[i]);
2236 srp_free_iu(target->srp_host, ch->tx_ring[i]);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002237 }
2238
Bart Van Assche4d73f952013-10-26 14:40:37 +02002239
2240err_no_ring:
Bart Van Assche509c07b2014-10-30 14:48:30 +01002241 kfree(ch->tx_ring);
2242 ch->tx_ring = NULL;
2243 kfree(ch->rx_ring);
2244 ch->rx_ring = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002245
2246 return -ENOMEM;
2247}
2248
Bart Van Asschec9b03c12011-09-03 09:34:48 +02002249static uint32_t srp_compute_rq_tmo(struct ib_qp_attr *qp_attr, int attr_mask)
2250{
2251 uint64_t T_tr_ns, max_compl_time_ms;
2252 uint32_t rq_tmo_jiffies;
2253
2254 /*
2255 * According to section 11.2.4.2 in the IBTA spec (Modify Queue Pair,
2256 * table 91), both the QP timeout and the retry count have to be set
2257 * for RC QP's during the RTR to RTS transition.
2258 */
2259 WARN_ON_ONCE((attr_mask & (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)) !=
2260 (IB_QP_TIMEOUT | IB_QP_RETRY_CNT));
2261
2262 /*
2263 * Set target->rq_tmo_jiffies to one second more than the largest time
2264 * it can take before an error completion is generated. See also
2265 * C9-140..142 in the IBTA spec for more information about how to
2266 * convert the QP Local ACK Timeout value to nanoseconds.
2267 */
2268 T_tr_ns = 4096 * (1ULL << qp_attr->timeout);
2269 max_compl_time_ms = qp_attr->retry_cnt * 4 * T_tr_ns;
2270 do_div(max_compl_time_ms, NSEC_PER_MSEC);
2271 rq_tmo_jiffies = msecs_to_jiffies(max_compl_time_ms + 1000);
2272
2273 return rq_tmo_jiffies;
2274}
2275
David Dillow961e0be2011-01-14 17:32:07 -05002276static void srp_cm_rep_handler(struct ib_cm_id *cm_id,
Bart Van Asschee6300cb2015-07-31 14:12:48 -07002277 const struct srp_login_rsp *lrsp,
Bart Van Assche509c07b2014-10-30 14:48:30 +01002278 struct srp_rdma_ch *ch)
David Dillow961e0be2011-01-14 17:32:07 -05002279{
Bart Van Assche509c07b2014-10-30 14:48:30 +01002280 struct srp_target_port *target = ch->target;
David Dillow961e0be2011-01-14 17:32:07 -05002281 struct ib_qp_attr *qp_attr = NULL;
2282 int attr_mask = 0;
2283 int ret;
2284 int i;
2285
2286 if (lrsp->opcode == SRP_LOGIN_RSP) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01002287 ch->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len);
2288 ch->req_lim = be32_to_cpu(lrsp->req_lim_delta);
David Dillow961e0be2011-01-14 17:32:07 -05002289
2290 /*
2291 * Reserve credits for task management so we don't
2292 * bounce requests back to the SCSI mid-layer.
2293 */
2294 target->scsi_host->can_queue
Bart Van Assche509c07b2014-10-30 14:48:30 +01002295 = min(ch->req_lim - SRP_TSK_MGMT_SQ_SIZE,
David Dillow961e0be2011-01-14 17:32:07 -05002296 target->scsi_host->can_queue);
Bart Van Assche4d73f952013-10-26 14:40:37 +02002297 target->scsi_host->cmd_per_lun
2298 = min_t(int, target->scsi_host->can_queue,
2299 target->scsi_host->cmd_per_lun);
David Dillow961e0be2011-01-14 17:32:07 -05002300 } else {
2301 shost_printk(KERN_WARNING, target->scsi_host,
2302 PFX "Unhandled RSP opcode %#x\n", lrsp->opcode);
2303 ret = -ECONNRESET;
2304 goto error;
2305 }
2306
Bart Van Assche509c07b2014-10-30 14:48:30 +01002307 if (!ch->rx_ring) {
2308 ret = srp_alloc_iu_bufs(ch);
David Dillow961e0be2011-01-14 17:32:07 -05002309 if (ret)
2310 goto error;
2311 }
2312
2313 ret = -ENOMEM;
2314 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
2315 if (!qp_attr)
2316 goto error;
2317
2318 qp_attr->qp_state = IB_QPS_RTR;
2319 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
2320 if (ret)
2321 goto error_free;
2322
Bart Van Assche509c07b2014-10-30 14:48:30 +01002323 ret = ib_modify_qp(ch->qp, qp_attr, attr_mask);
David Dillow961e0be2011-01-14 17:32:07 -05002324 if (ret)
2325 goto error_free;
2326
Bart Van Assche4d73f952013-10-26 14:40:37 +02002327 for (i = 0; i < target->queue_size; i++) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01002328 struct srp_iu *iu = ch->rx_ring[i];
2329
2330 ret = srp_post_recv(ch, iu);
David Dillow961e0be2011-01-14 17:32:07 -05002331 if (ret)
2332 goto error_free;
2333 }
2334
2335 qp_attr->qp_state = IB_QPS_RTS;
2336 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
2337 if (ret)
2338 goto error_free;
2339
Bart Van Asschec9b03c12011-09-03 09:34:48 +02002340 target->rq_tmo_jiffies = srp_compute_rq_tmo(qp_attr, attr_mask);
2341
Bart Van Assche509c07b2014-10-30 14:48:30 +01002342 ret = ib_modify_qp(ch->qp, qp_attr, attr_mask);
David Dillow961e0be2011-01-14 17:32:07 -05002343 if (ret)
2344 goto error_free;
2345
2346 ret = ib_send_cm_rtu(cm_id, NULL, 0);
2347
2348error_free:
2349 kfree(qp_attr);
2350
2351error:
Bart Van Assche509c07b2014-10-30 14:48:30 +01002352 ch->status = ret;
David Dillow961e0be2011-01-14 17:32:07 -05002353}
2354
Roland Dreieraef9ec32005-11-02 14:07:13 -08002355static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
2356 struct ib_cm_event *event,
Bart Van Assche509c07b2014-10-30 14:48:30 +01002357 struct srp_rdma_ch *ch)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002358{
Bart Van Assche509c07b2014-10-30 14:48:30 +01002359 struct srp_target_port *target = ch->target;
David Dillow7aa54bd2008-01-07 18:23:41 -05002360 struct Scsi_Host *shost = target->scsi_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002361 struct ib_class_port_info *cpi;
2362 int opcode;
2363
2364 switch (event->param.rej_rcvd.reason) {
2365 case IB_CM_REJ_PORT_CM_REDIRECT:
2366 cpi = event->param.rej_rcvd.ari;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002367 ch->path.dlid = cpi->redirect_lid;
2368 ch->path.pkey = cpi->redirect_pkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002369 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002370 memcpy(ch->path.dgid.raw, cpi->redirect_gid, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002371
Bart Van Assche509c07b2014-10-30 14:48:30 +01002372 ch->status = ch->path.dlid ?
Roland Dreieraef9ec32005-11-02 14:07:13 -08002373 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
2374 break;
2375
2376 case IB_CM_REJ_PORT_REDIRECT:
Roland Dreier5d7cbfd2007-08-03 10:45:18 -07002377 if (srp_target_is_topspin(target)) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002378 /*
2379 * Topspin/Cisco SRP gateways incorrectly send
2380 * reject reason code 25 when they mean 24
2381 * (port redirect).
2382 */
Bart Van Assche509c07b2014-10-30 14:48:30 +01002383 memcpy(ch->path.dgid.raw,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002384 event->param.rej_rcvd.ari, 16);
2385
David Dillow7aa54bd2008-01-07 18:23:41 -05002386 shost_printk(KERN_DEBUG, shost,
2387 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
Bart Van Assche509c07b2014-10-30 14:48:30 +01002388 be64_to_cpu(ch->path.dgid.global.subnet_prefix),
2389 be64_to_cpu(ch->path.dgid.global.interface_id));
Roland Dreieraef9ec32005-11-02 14:07:13 -08002390
Bart Van Assche509c07b2014-10-30 14:48:30 +01002391 ch->status = SRP_PORT_REDIRECT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002392 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05002393 shost_printk(KERN_WARNING, shost,
2394 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
Bart Van Assche509c07b2014-10-30 14:48:30 +01002395 ch->status = -ECONNRESET;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002396 }
2397 break;
2398
2399 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
David Dillow7aa54bd2008-01-07 18:23:41 -05002400 shost_printk(KERN_WARNING, shost,
2401 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
Bart Van Assche509c07b2014-10-30 14:48:30 +01002402 ch->status = -ECONNRESET;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002403 break;
2404
2405 case IB_CM_REJ_CONSUMER_DEFINED:
2406 opcode = *(u8 *) event->private_data;
2407 if (opcode == SRP_LOGIN_REJ) {
2408 struct srp_login_rej *rej = event->private_data;
2409 u32 reason = be32_to_cpu(rej->reason);
2410
2411 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
David Dillow7aa54bd2008-01-07 18:23:41 -05002412 shost_printk(KERN_WARNING, shost,
2413 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002414 else
Bart Van Asschee7ffde02014-03-14 13:52:21 +01002415 shost_printk(KERN_WARNING, shost, PFX
2416 "SRP LOGIN from %pI6 to %pI6 REJECTED, reason 0x%08x\n",
Bart Van Assche747fe002014-10-30 14:48:05 +01002417 target->sgid.raw,
2418 target->orig_dgid.raw, reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002419 } else
David Dillow7aa54bd2008-01-07 18:23:41 -05002420 shost_printk(KERN_WARNING, shost,
2421 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
2422 " opcode 0x%02x\n", opcode);
Bart Van Assche509c07b2014-10-30 14:48:30 +01002423 ch->status = -ECONNRESET;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002424 break;
2425
David Dillow9fe4bcf2008-01-08 17:08:52 -05002426 case IB_CM_REJ_STALE_CONN:
2427 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
Bart Van Assche509c07b2014-10-30 14:48:30 +01002428 ch->status = SRP_STALE_CONN;
David Dillow9fe4bcf2008-01-08 17:08:52 -05002429 break;
2430
Roland Dreieraef9ec32005-11-02 14:07:13 -08002431 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05002432 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
2433 event->param.rej_rcvd.reason);
Bart Van Assche509c07b2014-10-30 14:48:30 +01002434 ch->status = -ECONNRESET;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002435 }
2436}
2437
2438static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2439{
Bart Van Assche509c07b2014-10-30 14:48:30 +01002440 struct srp_rdma_ch *ch = cm_id->context;
2441 struct srp_target_port *target = ch->target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002442 int comp = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002443
2444 switch (event->event) {
2445 case IB_CM_REQ_ERROR:
David Dillow7aa54bd2008-01-07 18:23:41 -05002446 shost_printk(KERN_DEBUG, target->scsi_host,
2447 PFX "Sending CM REQ failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002448 comp = 1;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002449 ch->status = -ECONNRESET;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002450 break;
2451
2452 case IB_CM_REP_RECEIVED:
2453 comp = 1;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002454 srp_cm_rep_handler(cm_id, event->private_data, ch);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002455 break;
2456
2457 case IB_CM_REJ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05002458 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002459 comp = 1;
2460
Bart Van Assche509c07b2014-10-30 14:48:30 +01002461 srp_cm_rej_handler(cm_id, event, ch);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002462 break;
2463
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07002464 case IB_CM_DREQ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05002465 shost_printk(KERN_WARNING, target->scsi_host,
2466 PFX "DREQ received - connection closed\n");
Bart Van Asschec014c8c2015-05-18 13:23:57 +02002467 ch->connected = false;
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07002468 if (ib_send_cm_drep(cm_id, NULL, 0))
David Dillow7aa54bd2008-01-07 18:23:41 -05002469 shost_printk(KERN_ERR, target->scsi_host,
2470 PFX "Sending CM DREP failed\n");
Bart Van Asschec1120f82013-10-26 14:35:08 +02002471 queue_work(system_long_wq, &target->tl_err_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002472 break;
2473
2474 case IB_CM_TIMEWAIT_EXIT:
David Dillow7aa54bd2008-01-07 18:23:41 -05002475 shost_printk(KERN_ERR, target->scsi_host,
2476 PFX "connection closed\n");
Bart Van Asscheac72d762014-03-14 13:53:40 +01002477 comp = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002478
Bart Van Assche509c07b2014-10-30 14:48:30 +01002479 ch->status = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002480 break;
2481
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07002482 case IB_CM_MRA_RECEIVED:
2483 case IB_CM_DREQ_ERROR:
2484 case IB_CM_DREP_RECEIVED:
2485 break;
2486
Roland Dreieraef9ec32005-11-02 14:07:13 -08002487 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05002488 shost_printk(KERN_WARNING, target->scsi_host,
2489 PFX "Unhandled CM event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002490 break;
2491 }
2492
2493 if (comp)
Bart Van Assche509c07b2014-10-30 14:48:30 +01002494 complete(&ch->done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002495
Roland Dreieraef9ec32005-11-02 14:07:13 -08002496 return 0;
2497}
2498
Jack Wang71444b92013-11-07 11:37:37 +01002499/**
Jack Wang71444b92013-11-07 11:37:37 +01002500 * srp_change_queue_depth - setting device queue depth
2501 * @sdev: scsi device struct
2502 * @qdepth: requested queue depth
Jack Wang71444b92013-11-07 11:37:37 +01002503 *
2504 * Returns queue depth.
2505 */
2506static int
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002507srp_change_queue_depth(struct scsi_device *sdev, int qdepth)
Jack Wang71444b92013-11-07 11:37:37 +01002508{
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01002509 if (!sdev->tagged_supported)
Christoph Hellwig1e6f2412014-11-13 14:27:41 +01002510 qdepth = 1;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002511 return scsi_change_queue_depth(sdev, qdepth);
Jack Wang71444b92013-11-07 11:37:37 +01002512}
2513
Bart Van Assche985aa492015-05-18 13:27:14 +02002514static int srp_send_tsk_mgmt(struct srp_rdma_ch *ch, u64 req_tag, u64 lun,
2515 u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002516{
Bart Van Assche509c07b2014-10-30 14:48:30 +01002517 struct srp_target_port *target = ch->target;
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002518 struct srp_rport *rport = target->rport;
David Dillow19081f32010-10-18 08:54:49 -04002519 struct ib_device *dev = target->srp_host->srp_dev->dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002520 struct srp_iu *iu;
2521 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002522
Bart Van Asschec014c8c2015-05-18 13:23:57 +02002523 if (!ch->connected || target->qp_in_error)
Bart Van Assche3780d1f2013-02-21 17:18:00 +00002524 return -1;
2525
Bart Van Assche509c07b2014-10-30 14:48:30 +01002526 init_completion(&ch->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002527
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002528 /*
Bart Van Assche509c07b2014-10-30 14:48:30 +01002529 * Lock the rport mutex to avoid that srp_create_ch_ib() is
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002530 * invoked while a task management function is being sent.
2531 */
2532 mutex_lock(&rport->mutex);
Bart Van Assche509c07b2014-10-30 14:48:30 +01002533 spin_lock_irq(&ch->lock);
2534 iu = __srp_get_tx_iu(ch, SRP_IU_TSK_MGMT);
2535 spin_unlock_irq(&ch->lock);
Bart Van Assche76c75b22010-11-26 14:37:47 -05002536
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002537 if (!iu) {
2538 mutex_unlock(&rport->mutex);
2539
Bart Van Assche76c75b22010-11-26 14:37:47 -05002540 return -1;
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002541 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002542
David Dillow19081f32010-10-18 08:54:49 -04002543 ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt,
2544 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002545 tsk_mgmt = iu->buf;
2546 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
2547
2548 tsk_mgmt->opcode = SRP_TSK_MGMT;
Bart Van Assche985aa492015-05-18 13:27:14 +02002549 int_to_scsilun(lun, &tsk_mgmt->lun);
David Dillowf8b6e312010-11-26 13:02:21 -05002550 tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002551 tsk_mgmt->tsk_mgmt_func = func;
David Dillowf8b6e312010-11-26 13:02:21 -05002552 tsk_mgmt->task_tag = req_tag;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002553
David Dillow19081f32010-10-18 08:54:49 -04002554 ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt,
2555 DMA_TO_DEVICE);
Bart Van Assche509c07b2014-10-30 14:48:30 +01002556 if (srp_post_send(ch, iu, sizeof(*tsk_mgmt))) {
2557 srp_put_tx_iu(ch, iu, SRP_IU_TSK_MGMT);
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002558 mutex_unlock(&rport->mutex);
2559
Bart Van Assche76c75b22010-11-26 14:37:47 -05002560 return -1;
2561 }
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002562 mutex_unlock(&rport->mutex);
Roland Dreierd945e1d2006-05-09 10:50:28 -07002563
Bart Van Assche509c07b2014-10-30 14:48:30 +01002564 if (!wait_for_completion_timeout(&ch->tsk_mgmt_done,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002565 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07002566 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002567
Roland Dreierd945e1d2006-05-09 10:50:28 -07002568 return 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07002569}
2570
Roland Dreieraef9ec32005-11-02 14:07:13 -08002571static int srp_abort(struct scsi_cmnd *scmnd)
2572{
Roland Dreierd945e1d2006-05-09 10:50:28 -07002573 struct srp_target_port *target = host_to_target(scmnd->device->host);
David Dillowf8b6e312010-11-26 13:02:21 -05002574 struct srp_request *req = (struct srp_request *) scmnd->host_scribble;
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02002575 u32 tag;
Bart Van Assched92c0da2014-10-06 17:14:36 +02002576 u16 ch_idx;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002577 struct srp_rdma_ch *ch;
Bart Van Assche086f44f2013-06-12 15:23:04 +02002578 int ret;
Roland Dreierd945e1d2006-05-09 10:50:28 -07002579
David Dillow7aa54bd2008-01-07 18:23:41 -05002580 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002581
Bart Van Assched92c0da2014-10-06 17:14:36 +02002582 if (!req)
Bart Van Assche99b66972013-10-10 13:52:33 +02002583 return SUCCESS;
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02002584 tag = blk_mq_unique_tag(scmnd->request);
Bart Van Assched92c0da2014-10-06 17:14:36 +02002585 ch_idx = blk_mq_unique_tag_to_hwq(tag);
2586 if (WARN_ON_ONCE(ch_idx >= target->ch_count))
2587 return SUCCESS;
2588 ch = &target->ch[ch_idx];
2589 if (!srp_claim_req(ch, req, NULL, scmnd))
2590 return SUCCESS;
2591 shost_printk(KERN_ERR, target->scsi_host,
2592 "Sending SRP abort for tag %#x\n", tag);
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02002593 if (srp_send_tsk_mgmt(ch, tag, scmnd->device->lun,
Bart Van Assche80d5e8a2013-07-10 17:36:35 +02002594 SRP_TSK_ABORT_TASK) == 0)
Bart Van Assche086f44f2013-06-12 15:23:04 +02002595 ret = SUCCESS;
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002596 else if (target->rport->state == SRP_RPORT_LOST)
Bart Van Assche99e1c132013-06-28 14:49:58 +02002597 ret = FAST_IO_FAIL;
Bart Van Assche086f44f2013-06-12 15:23:04 +02002598 else
2599 ret = FAILED;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002600 srp_free_req(ch, req, scmnd, 0);
Bart Van Assche22032992012-08-14 13:18:53 +00002601 scmnd->result = DID_ABORT << 16;
Bart Van Assched8536672012-08-24 10:29:11 +00002602 scmnd->scsi_done(scmnd);
Roland Dreierd945e1d2006-05-09 10:50:28 -07002603
Bart Van Assche086f44f2013-06-12 15:23:04 +02002604 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002605}
2606
2607static int srp_reset_device(struct scsi_cmnd *scmnd)
2608{
Roland Dreierd945e1d2006-05-09 10:50:28 -07002609 struct srp_target_port *target = host_to_target(scmnd->device->host);
Bart Van Assched92c0da2014-10-06 17:14:36 +02002610 struct srp_rdma_ch *ch;
Bart Van Assche536ae142010-11-26 13:58:27 -05002611 int i;
Roland Dreierd945e1d2006-05-09 10:50:28 -07002612
David Dillow7aa54bd2008-01-07 18:23:41 -05002613 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002614
Bart Van Assched92c0da2014-10-06 17:14:36 +02002615 ch = &target->ch[0];
Bart Van Assche509c07b2014-10-30 14:48:30 +01002616 if (srp_send_tsk_mgmt(ch, SRP_TAG_NO_REQ, scmnd->device->lun,
David Dillowf8b6e312010-11-26 13:02:21 -05002617 SRP_TSK_LUN_RESET))
Roland Dreierd945e1d2006-05-09 10:50:28 -07002618 return FAILED;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002619 if (ch->tsk_mgmt_status)
Roland Dreierd945e1d2006-05-09 10:50:28 -07002620 return FAILED;
2621
Bart Van Assched92c0da2014-10-06 17:14:36 +02002622 for (i = 0; i < target->ch_count; i++) {
2623 ch = &target->ch[i];
2624 for (i = 0; i < target->req_ring_size; ++i) {
2625 struct srp_request *req = &ch->req_ring[i];
Bart Van Assche509c07b2014-10-30 14:48:30 +01002626
Bart Van Assched92c0da2014-10-06 17:14:36 +02002627 srp_finish_req(ch, req, scmnd->device, DID_RESET << 16);
2628 }
Bart Van Assche536ae142010-11-26 13:58:27 -05002629 }
Roland Dreierd945e1d2006-05-09 10:50:28 -07002630
Roland Dreierd945e1d2006-05-09 10:50:28 -07002631 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002632}
2633
2634static int srp_reset_host(struct scsi_cmnd *scmnd)
2635{
2636 struct srp_target_port *target = host_to_target(scmnd->device->host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002637
David Dillow7aa54bd2008-01-07 18:23:41 -05002638 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002639
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002640 return srp_reconnect_rport(target->rport) == 0 ? SUCCESS : FAILED;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002641}
2642
Bart Van Assche509c5f32016-05-12 10:50:35 -07002643static int srp_slave_alloc(struct scsi_device *sdev)
2644{
2645 struct Scsi_Host *shost = sdev->host;
2646 struct srp_target_port *target = host_to_target(shost);
2647 struct srp_device *srp_dev = target->srp_host->srp_dev;
2648 struct ib_device *ibdev = srp_dev->dev;
2649
2650 if (!(ibdev->attrs.device_cap_flags & IB_DEVICE_SG_GAPS_REG))
2651 blk_queue_virt_boundary(sdev->request_queue,
2652 ~srp_dev->mr_page_mask);
2653
2654 return 0;
2655}
2656
Bart Van Asschec9b03c12011-09-03 09:34:48 +02002657static int srp_slave_configure(struct scsi_device *sdev)
2658{
2659 struct Scsi_Host *shost = sdev->host;
2660 struct srp_target_port *target = host_to_target(shost);
2661 struct request_queue *q = sdev->request_queue;
2662 unsigned long timeout;
2663
2664 if (sdev->type == TYPE_DISK) {
2665 timeout = max_t(unsigned, 30 * HZ, target->rq_tmo_jiffies);
2666 blk_queue_rq_timeout(q, timeout);
2667 }
2668
2669 return 0;
2670}
2671
Tony Jonesee959b02008-02-22 00:13:36 +01002672static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
2673 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002674{
Tony Jonesee959b02008-02-22 00:13:36 +01002675 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002676
Bart Van Assche45c37ca2015-05-18 13:25:10 +02002677 return sprintf(buf, "0x%016llx\n", be64_to_cpu(target->id_ext));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002678}
2679
Tony Jonesee959b02008-02-22 00:13:36 +01002680static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
2681 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002682{
Tony Jonesee959b02008-02-22 00:13:36 +01002683 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002684
Bart Van Assche45c37ca2015-05-18 13:25:10 +02002685 return sprintf(buf, "0x%016llx\n", be64_to_cpu(target->ioc_guid));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002686}
2687
Tony Jonesee959b02008-02-22 00:13:36 +01002688static ssize_t show_service_id(struct device *dev,
2689 struct device_attribute *attr, char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002690{
Tony Jonesee959b02008-02-22 00:13:36 +01002691 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002692
Bart Van Assche45c37ca2015-05-18 13:25:10 +02002693 return sprintf(buf, "0x%016llx\n", be64_to_cpu(target->service_id));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002694}
2695
Tony Jonesee959b02008-02-22 00:13:36 +01002696static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
2697 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002698{
Tony Jonesee959b02008-02-22 00:13:36 +01002699 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002700
Bart Van Assche747fe002014-10-30 14:48:05 +01002701 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->pkey));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002702}
2703
Bart Van Assche848b3082013-10-26 14:38:12 +02002704static ssize_t show_sgid(struct device *dev, struct device_attribute *attr,
2705 char *buf)
2706{
2707 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2708
Bart Van Assche747fe002014-10-30 14:48:05 +01002709 return sprintf(buf, "%pI6\n", target->sgid.raw);
Bart Van Assche848b3082013-10-26 14:38:12 +02002710}
2711
Tony Jonesee959b02008-02-22 00:13:36 +01002712static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
2713 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002714{
Tony Jonesee959b02008-02-22 00:13:36 +01002715 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Bart Van Assched92c0da2014-10-06 17:14:36 +02002716 struct srp_rdma_ch *ch = &target->ch[0];
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002717
Bart Van Assche509c07b2014-10-30 14:48:30 +01002718 return sprintf(buf, "%pI6\n", ch->path.dgid.raw);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002719}
2720
Tony Jonesee959b02008-02-22 00:13:36 +01002721static ssize_t show_orig_dgid(struct device *dev,
2722 struct device_attribute *attr, char *buf)
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002723{
Tony Jonesee959b02008-02-22 00:13:36 +01002724 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002725
Bart Van Assche747fe002014-10-30 14:48:05 +01002726 return sprintf(buf, "%pI6\n", target->orig_dgid.raw);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002727}
2728
Bart Van Assche89de7482010-08-03 14:08:45 +00002729static ssize_t show_req_lim(struct device *dev,
2730 struct device_attribute *attr, char *buf)
2731{
2732 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Bart Van Assched92c0da2014-10-06 17:14:36 +02002733 struct srp_rdma_ch *ch;
2734 int i, req_lim = INT_MAX;
Bart Van Assche89de7482010-08-03 14:08:45 +00002735
Bart Van Assched92c0da2014-10-06 17:14:36 +02002736 for (i = 0; i < target->ch_count; i++) {
2737 ch = &target->ch[i];
2738 req_lim = min(req_lim, ch->req_lim);
2739 }
2740 return sprintf(buf, "%d\n", req_lim);
Bart Van Assche89de7482010-08-03 14:08:45 +00002741}
2742
Tony Jonesee959b02008-02-22 00:13:36 +01002743static ssize_t show_zero_req_lim(struct device *dev,
2744 struct device_attribute *attr, char *buf)
Roland Dreier6bfa24f2006-06-17 20:37:33 -07002745{
Tony Jonesee959b02008-02-22 00:13:36 +01002746 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6bfa24f2006-06-17 20:37:33 -07002747
Roland Dreier6bfa24f2006-06-17 20:37:33 -07002748 return sprintf(buf, "%d\n", target->zero_req_lim);
2749}
2750
Tony Jonesee959b02008-02-22 00:13:36 +01002751static ssize_t show_local_ib_port(struct device *dev,
2752 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002753{
Tony Jonesee959b02008-02-22 00:13:36 +01002754 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002755
2756 return sprintf(buf, "%d\n", target->srp_host->port);
2757}
2758
Tony Jonesee959b02008-02-22 00:13:36 +01002759static ssize_t show_local_ib_device(struct device *dev,
2760 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002761{
Tony Jonesee959b02008-02-22 00:13:36 +01002762 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002763
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002764 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002765}
2766
Bart Van Assched92c0da2014-10-06 17:14:36 +02002767static ssize_t show_ch_count(struct device *dev, struct device_attribute *attr,
2768 char *buf)
2769{
2770 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2771
2772 return sprintf(buf, "%d\n", target->ch_count);
2773}
2774
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002775static ssize_t show_comp_vector(struct device *dev,
2776 struct device_attribute *attr, char *buf)
2777{
2778 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2779
2780 return sprintf(buf, "%d\n", target->comp_vector);
2781}
2782
Vu Pham7bb312e2013-10-26 14:31:27 +02002783static ssize_t show_tl_retry_count(struct device *dev,
2784 struct device_attribute *attr, char *buf)
2785{
2786 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2787
2788 return sprintf(buf, "%d\n", target->tl_retry_count);
2789}
2790
David Dillow49248642011-01-14 18:23:24 -05002791static ssize_t show_cmd_sg_entries(struct device *dev,
2792 struct device_attribute *attr, char *buf)
2793{
2794 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2795
2796 return sprintf(buf, "%u\n", target->cmd_sg_cnt);
2797}
2798
David Dillowc07d4242011-01-16 13:57:10 -05002799static ssize_t show_allow_ext_sg(struct device *dev,
2800 struct device_attribute *attr, char *buf)
2801{
2802 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2803
2804 return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false");
2805}
2806
Tony Jonesee959b02008-02-22 00:13:36 +01002807static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
2808static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
2809static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
2810static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
Bart Van Assche848b3082013-10-26 14:38:12 +02002811static DEVICE_ATTR(sgid, S_IRUGO, show_sgid, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01002812static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
2813static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
Bart Van Assche89de7482010-08-03 14:08:45 +00002814static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01002815static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
2816static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
2817static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Bart Van Assched92c0da2014-10-06 17:14:36 +02002818static DEVICE_ATTR(ch_count, S_IRUGO, show_ch_count, NULL);
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002819static DEVICE_ATTR(comp_vector, S_IRUGO, show_comp_vector, NULL);
Vu Pham7bb312e2013-10-26 14:31:27 +02002820static DEVICE_ATTR(tl_retry_count, S_IRUGO, show_tl_retry_count, NULL);
David Dillow49248642011-01-14 18:23:24 -05002821static DEVICE_ATTR(cmd_sg_entries, S_IRUGO, show_cmd_sg_entries, NULL);
David Dillowc07d4242011-01-16 13:57:10 -05002822static DEVICE_ATTR(allow_ext_sg, S_IRUGO, show_allow_ext_sg, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002823
Tony Jonesee959b02008-02-22 00:13:36 +01002824static struct device_attribute *srp_host_attrs[] = {
2825 &dev_attr_id_ext,
2826 &dev_attr_ioc_guid,
2827 &dev_attr_service_id,
2828 &dev_attr_pkey,
Bart Van Assche848b3082013-10-26 14:38:12 +02002829 &dev_attr_sgid,
Tony Jonesee959b02008-02-22 00:13:36 +01002830 &dev_attr_dgid,
2831 &dev_attr_orig_dgid,
Bart Van Assche89de7482010-08-03 14:08:45 +00002832 &dev_attr_req_lim,
Tony Jonesee959b02008-02-22 00:13:36 +01002833 &dev_attr_zero_req_lim,
2834 &dev_attr_local_ib_port,
2835 &dev_attr_local_ib_device,
Bart Van Assched92c0da2014-10-06 17:14:36 +02002836 &dev_attr_ch_count,
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002837 &dev_attr_comp_vector,
Vu Pham7bb312e2013-10-26 14:31:27 +02002838 &dev_attr_tl_retry_count,
David Dillow49248642011-01-14 18:23:24 -05002839 &dev_attr_cmd_sg_entries,
David Dillowc07d4242011-01-16 13:57:10 -05002840 &dev_attr_allow_ext_sg,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002841 NULL
2842};
2843
Roland Dreieraef9ec32005-11-02 14:07:13 -08002844static struct scsi_host_template srp_template = {
2845 .module = THIS_MODULE,
Roland Dreierb7f008f2007-05-06 21:18:11 -07002846 .name = "InfiniBand SRP initiator",
2847 .proc_name = DRV_NAME,
Bart Van Assche509c5f32016-05-12 10:50:35 -07002848 .slave_alloc = srp_slave_alloc,
Bart Van Asschec9b03c12011-09-03 09:34:48 +02002849 .slave_configure = srp_slave_configure,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002850 .info = srp_target_info,
2851 .queuecommand = srp_queuecommand,
Jack Wang71444b92013-11-07 11:37:37 +01002852 .change_queue_depth = srp_change_queue_depth,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002853 .eh_abort_handler = srp_abort,
2854 .eh_device_reset_handler = srp_reset_device,
2855 .eh_host_reset_handler = srp_reset_host,
Bart Van Assche2742c1d2013-06-12 15:24:25 +02002856 .skip_settle_delay = true,
David Dillow49248642011-01-14 18:23:24 -05002857 .sg_tablesize = SRP_DEF_SG_TABLESIZE,
Bart Van Assche4d73f952013-10-26 14:40:37 +02002858 .can_queue = SRP_DEFAULT_CMD_SQ_SIZE,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002859 .this_id = -1,
Bart Van Assche4d73f952013-10-26 14:40:37 +02002860 .cmd_per_lun = SRP_DEFAULT_CMD_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002861 .use_clustering = ENABLE_CLUSTERING,
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02002862 .shost_attrs = srp_host_attrs,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01002863 .track_queue_depth = 1,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002864};
2865
Bart Van Assche34aa6542014-10-30 14:47:22 +01002866static int srp_sdev_count(struct Scsi_Host *host)
2867{
2868 struct scsi_device *sdev;
2869 int c = 0;
2870
2871 shost_for_each_device(sdev, host)
2872 c++;
2873
2874 return c;
2875}
2876
Bart Van Asschebc44bd12015-08-14 11:01:09 -07002877/*
2878 * Return values:
2879 * < 0 upon failure. Caller is responsible for SRP target port cleanup.
2880 * 0 and target->state == SRP_TARGET_REMOVED if asynchronous target port
2881 * removal has been scheduled.
2882 * 0 and target->state != SRP_TARGET_REMOVED upon success.
2883 */
Roland Dreieraef9ec32005-11-02 14:07:13 -08002884static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
2885{
FUJITA Tomonori32368222007-06-27 16:33:12 +09002886 struct srp_rport_identifiers ids;
2887 struct srp_rport *rport;
2888
Bart Van Assche34aa6542014-10-30 14:47:22 +01002889 target->state = SRP_TARGET_SCANNING;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002890 sprintf(target->target_name, "SRP.T10:%016llX",
Bart Van Assche45c37ca2015-05-18 13:25:10 +02002891 be64_to_cpu(target->id_ext));
Roland Dreieraef9ec32005-11-02 14:07:13 -08002892
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002893 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002894 return -ENODEV;
2895
FUJITA Tomonori32368222007-06-27 16:33:12 +09002896 memcpy(ids.port_id, &target->id_ext, 8);
2897 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09002898 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori32368222007-06-27 16:33:12 +09002899 rport = srp_rport_add(target->scsi_host, &ids);
2900 if (IS_ERR(rport)) {
2901 scsi_remove_host(target->scsi_host);
2902 return PTR_ERR(rport);
2903 }
2904
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02002905 rport->lld_data = target;
Bart Van Assche9dd69a62013-10-26 14:32:30 +02002906 target->rport = rport;
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02002907
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002908 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002909 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002910 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002911
Roland Dreieraef9ec32005-11-02 14:07:13 -08002912 scsi_scan_target(&target->scsi_host->shost_gendev,
Hannes Reinecke1d645082016-03-17 08:39:45 +01002913 0, target->scsi_id, SCAN_WILD_CARD, SCSI_SCAN_INITIAL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002914
Bart Van Asschec014c8c2015-05-18 13:23:57 +02002915 if (srp_connected_ch(target) < target->ch_count ||
2916 target->qp_in_error) {
Bart Van Assche34aa6542014-10-30 14:47:22 +01002917 shost_printk(KERN_INFO, target->scsi_host,
2918 PFX "SCSI scan failed - removing SCSI host\n");
2919 srp_queue_remove_work(target);
2920 goto out;
2921 }
2922
Bart Van Asschecf1acab2016-05-12 10:47:38 -07002923 pr_debug("%s: SCSI scan succeeded - detected %d LUNs\n",
Bart Van Assche34aa6542014-10-30 14:47:22 +01002924 dev_name(&target->scsi_host->shost_gendev),
2925 srp_sdev_count(target->scsi_host));
2926
2927 spin_lock_irq(&target->lock);
2928 if (target->state == SRP_TARGET_SCANNING)
2929 target->state = SRP_TARGET_LIVE;
2930 spin_unlock_irq(&target->lock);
2931
2932out:
Roland Dreieraef9ec32005-11-02 14:07:13 -08002933 return 0;
2934}
2935
Tony Jonesee959b02008-02-22 00:13:36 +01002936static void srp_release_dev(struct device *dev)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002937{
2938 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01002939 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002940
2941 complete(&host->released);
2942}
2943
2944static struct class srp_class = {
2945 .name = "infiniband_srp",
Tony Jonesee959b02008-02-22 00:13:36 +01002946 .dev_release = srp_release_dev
Roland Dreieraef9ec32005-11-02 14:07:13 -08002947};
2948
Bart Van Assche96fc2482013-06-28 14:51:26 +02002949/**
2950 * srp_conn_unique() - check whether the connection to a target is unique
Bart Van Asscheaf246632014-05-20 15:04:21 +02002951 * @host: SRP host.
2952 * @target: SRP target port.
Bart Van Assche96fc2482013-06-28 14:51:26 +02002953 */
2954static bool srp_conn_unique(struct srp_host *host,
2955 struct srp_target_port *target)
2956{
2957 struct srp_target_port *t;
2958 bool ret = false;
2959
2960 if (target->state == SRP_TARGET_REMOVED)
2961 goto out;
2962
2963 ret = true;
2964
2965 spin_lock(&host->target_lock);
2966 list_for_each_entry(t, &host->target_list, list) {
2967 if (t != target &&
2968 target->id_ext == t->id_ext &&
2969 target->ioc_guid == t->ioc_guid &&
2970 target->initiator_ext == t->initiator_ext) {
2971 ret = false;
2972 break;
2973 }
2974 }
2975 spin_unlock(&host->target_lock);
2976
2977out:
2978 return ret;
2979}
2980
Roland Dreieraef9ec32005-11-02 14:07:13 -08002981/*
2982 * Target ports are added by writing
2983 *
2984 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
2985 * pkey=<P_Key>,service_id=<service ID>
2986 *
2987 * to the add_target sysfs attribute.
2988 */
2989enum {
2990 SRP_OPT_ERR = 0,
2991 SRP_OPT_ID_EXT = 1 << 0,
2992 SRP_OPT_IOC_GUID = 1 << 1,
2993 SRP_OPT_DGID = 1 << 2,
2994 SRP_OPT_PKEY = 1 << 3,
2995 SRP_OPT_SERVICE_ID = 1 << 4,
2996 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07002997 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07002998 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002999 SRP_OPT_INITIATOR_EXT = 1 << 8,
David Dillow49248642011-01-14 18:23:24 -05003000 SRP_OPT_CMD_SG_ENTRIES = 1 << 9,
David Dillowc07d4242011-01-16 13:57:10 -05003001 SRP_OPT_ALLOW_EXT_SG = 1 << 10,
3002 SRP_OPT_SG_TABLESIZE = 1 << 11,
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02003003 SRP_OPT_COMP_VECTOR = 1 << 12,
Vu Pham7bb312e2013-10-26 14:31:27 +02003004 SRP_OPT_TL_RETRY_COUNT = 1 << 13,
Bart Van Assche4d73f952013-10-26 14:40:37 +02003005 SRP_OPT_QUEUE_SIZE = 1 << 14,
Roland Dreieraef9ec32005-11-02 14:07:13 -08003006 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
3007 SRP_OPT_IOC_GUID |
3008 SRP_OPT_DGID |
3009 SRP_OPT_PKEY |
3010 SRP_OPT_SERVICE_ID),
3011};
3012
Steven Whitehousea447c092008-10-13 10:46:57 +01003013static const match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07003014 { SRP_OPT_ID_EXT, "id_ext=%s" },
3015 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
3016 { SRP_OPT_DGID, "dgid=%s" },
3017 { SRP_OPT_PKEY, "pkey=%x" },
3018 { SRP_OPT_SERVICE_ID, "service_id=%s" },
3019 { SRP_OPT_MAX_SECT, "max_sect=%d" },
3020 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07003021 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02003022 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
David Dillow49248642011-01-14 18:23:24 -05003023 { SRP_OPT_CMD_SG_ENTRIES, "cmd_sg_entries=%u" },
David Dillowc07d4242011-01-16 13:57:10 -05003024 { SRP_OPT_ALLOW_EXT_SG, "allow_ext_sg=%u" },
3025 { SRP_OPT_SG_TABLESIZE, "sg_tablesize=%u" },
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02003026 { SRP_OPT_COMP_VECTOR, "comp_vector=%u" },
Vu Pham7bb312e2013-10-26 14:31:27 +02003027 { SRP_OPT_TL_RETRY_COUNT, "tl_retry_count=%u" },
Bart Van Assche4d73f952013-10-26 14:40:37 +02003028 { SRP_OPT_QUEUE_SIZE, "queue_size=%d" },
Vu Pham52fb2b502006-06-17 20:37:31 -07003029 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08003030};
3031
3032static int srp_parse_options(const char *buf, struct srp_target_port *target)
3033{
3034 char *options, *sep_opt;
3035 char *p;
3036 char dgid[3];
3037 substring_t args[MAX_OPT_ARGS];
3038 int opt_mask = 0;
3039 int token;
3040 int ret = -EINVAL;
3041 int i;
3042
3043 options = kstrdup(buf, GFP_KERNEL);
3044 if (!options)
3045 return -ENOMEM;
3046
3047 sep_opt = options;
Sagi Grimberg7dcf9c12014-10-19 18:19:02 +03003048 while ((p = strsep(&sep_opt, ",\n")) != NULL) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08003049 if (!*p)
3050 continue;
3051
3052 token = match_token(p, srp_opt_tokens, args);
3053 opt_mask |= token;
3054
3055 switch (token) {
3056 case SRP_OPT_ID_EXT:
3057 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02003058 if (!p) {
3059 ret = -ENOMEM;
3060 goto out;
3061 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08003062 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
3063 kfree(p);
3064 break;
3065
3066 case SRP_OPT_IOC_GUID:
3067 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02003068 if (!p) {
3069 ret = -ENOMEM;
3070 goto out;
3071 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08003072 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
3073 kfree(p);
3074 break;
3075
3076 case SRP_OPT_DGID:
3077 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02003078 if (!p) {
3079 ret = -ENOMEM;
3080 goto out;
3081 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08003082 if (strlen(p) != 32) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003083 pr_warn("bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07003084 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003085 goto out;
3086 }
3087
3088 for (i = 0; i < 16; ++i) {
Bart Van Assche747fe002014-10-30 14:48:05 +01003089 strlcpy(dgid, p + i * 2, sizeof(dgid));
3090 if (sscanf(dgid, "%hhx",
3091 &target->orig_dgid.raw[i]) < 1) {
3092 ret = -EINVAL;
3093 kfree(p);
3094 goto out;
3095 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08003096 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08003097 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003098 break;
3099
3100 case SRP_OPT_PKEY:
3101 if (match_hex(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003102 pr_warn("bad P_Key parameter '%s'\n", p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003103 goto out;
3104 }
Bart Van Assche747fe002014-10-30 14:48:05 +01003105 target->pkey = cpu_to_be16(token);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003106 break;
3107
3108 case SRP_OPT_SERVICE_ID:
3109 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02003110 if (!p) {
3111 ret = -ENOMEM;
3112 goto out;
3113 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08003114 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
3115 kfree(p);
3116 break;
3117
3118 case SRP_OPT_MAX_SECT:
3119 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003120 pr_warn("bad max sect parameter '%s'\n", p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003121 goto out;
3122 }
3123 target->scsi_host->max_sectors = token;
3124 break;
3125
Bart Van Assche4d73f952013-10-26 14:40:37 +02003126 case SRP_OPT_QUEUE_SIZE:
3127 if (match_int(args, &token) || token < 1) {
3128 pr_warn("bad queue_size parameter '%s'\n", p);
3129 goto out;
3130 }
3131 target->scsi_host->can_queue = token;
3132 target->queue_size = token + SRP_RSP_SQ_SIZE +
3133 SRP_TSK_MGMT_SQ_SIZE;
3134 if (!(opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
3135 target->scsi_host->cmd_per_lun = token;
3136 break;
3137
Vu Pham52fb2b502006-06-17 20:37:31 -07003138 case SRP_OPT_MAX_CMD_PER_LUN:
Bart Van Assche4d73f952013-10-26 14:40:37 +02003139 if (match_int(args, &token) || token < 1) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003140 pr_warn("bad max cmd_per_lun parameter '%s'\n",
3141 p);
Vu Pham52fb2b502006-06-17 20:37:31 -07003142 goto out;
3143 }
Bart Van Assche4d73f952013-10-26 14:40:37 +02003144 target->scsi_host->cmd_per_lun = token;
Vu Pham52fb2b502006-06-17 20:37:31 -07003145 break;
3146
Ramachandra K0c0450db2006-06-17 20:37:38 -07003147 case SRP_OPT_IO_CLASS:
3148 if (match_hex(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003149 pr_warn("bad IO class parameter '%s'\n", p);
Ramachandra K0c0450db2006-06-17 20:37:38 -07003150 goto out;
3151 }
3152 if (token != SRP_REV10_IB_IO_CLASS &&
3153 token != SRP_REV16A_IB_IO_CLASS) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003154 pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n",
3155 token, SRP_REV10_IB_IO_CLASS,
3156 SRP_REV16A_IB_IO_CLASS);
Ramachandra K0c0450db2006-06-17 20:37:38 -07003157 goto out;
3158 }
3159 target->io_class = token;
3160 break;
3161
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02003162 case SRP_OPT_INITIATOR_EXT:
3163 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02003164 if (!p) {
3165 ret = -ENOMEM;
3166 goto out;
3167 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02003168 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
3169 kfree(p);
3170 break;
3171
David Dillow49248642011-01-14 18:23:24 -05003172 case SRP_OPT_CMD_SG_ENTRIES:
3173 if (match_int(args, &token) || token < 1 || token > 255) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003174 pr_warn("bad max cmd_sg_entries parameter '%s'\n",
3175 p);
David Dillow49248642011-01-14 18:23:24 -05003176 goto out;
3177 }
3178 target->cmd_sg_cnt = token;
3179 break;
3180
David Dillowc07d4242011-01-16 13:57:10 -05003181 case SRP_OPT_ALLOW_EXT_SG:
3182 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003183 pr_warn("bad allow_ext_sg parameter '%s'\n", p);
David Dillowc07d4242011-01-16 13:57:10 -05003184 goto out;
3185 }
3186 target->allow_ext_sg = !!token;
3187 break;
3188
3189 case SRP_OPT_SG_TABLESIZE:
3190 if (match_int(args, &token) || token < 1 ||
Ming Lin65e86172016-04-04 14:48:10 -07003191 token > SG_MAX_SEGMENTS) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003192 pr_warn("bad max sg_tablesize parameter '%s'\n",
3193 p);
David Dillowc07d4242011-01-16 13:57:10 -05003194 goto out;
3195 }
3196 target->sg_tablesize = token;
3197 break;
3198
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02003199 case SRP_OPT_COMP_VECTOR:
3200 if (match_int(args, &token) || token < 0) {
3201 pr_warn("bad comp_vector parameter '%s'\n", p);
3202 goto out;
3203 }
3204 target->comp_vector = token;
3205 break;
3206
Vu Pham7bb312e2013-10-26 14:31:27 +02003207 case SRP_OPT_TL_RETRY_COUNT:
3208 if (match_int(args, &token) || token < 2 || token > 7) {
3209 pr_warn("bad tl_retry_count parameter '%s' (must be a number between 2 and 7)\n",
3210 p);
3211 goto out;
3212 }
3213 target->tl_retry_count = token;
3214 break;
3215
Roland Dreieraef9ec32005-11-02 14:07:13 -08003216 default:
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003217 pr_warn("unknown parameter or missing value '%s' in target creation request\n",
3218 p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003219 goto out;
3220 }
3221 }
3222
3223 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
3224 ret = 0;
3225 else
3226 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
3227 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
3228 !(srp_opt_tokens[i].token & opt_mask))
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003229 pr_warn("target creation request is missing parameter '%s'\n",
3230 srp_opt_tokens[i].pattern);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003231
Bart Van Assche4d73f952013-10-26 14:40:37 +02003232 if (target->scsi_host->cmd_per_lun > target->scsi_host->can_queue
3233 && (opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
3234 pr_warn("cmd_per_lun = %d > queue_size = %d\n",
3235 target->scsi_host->cmd_per_lun,
3236 target->scsi_host->can_queue);
3237
Roland Dreieraef9ec32005-11-02 14:07:13 -08003238out:
3239 kfree(options);
3240 return ret;
3241}
3242
Tony Jonesee959b02008-02-22 00:13:36 +01003243static ssize_t srp_create_target(struct device *dev,
3244 struct device_attribute *attr,
Roland Dreieraef9ec32005-11-02 14:07:13 -08003245 const char *buf, size_t count)
3246{
3247 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01003248 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003249 struct Scsi_Host *target_host;
3250 struct srp_target_port *target;
Bart Van Assche509c07b2014-10-30 14:48:30 +01003251 struct srp_rdma_ch *ch;
Bart Van Assched1b42892014-05-20 15:07:20 +02003252 struct srp_device *srp_dev = host->srp_dev;
3253 struct ib_device *ibdev = srp_dev->dev;
Bart Van Assched92c0da2014-10-06 17:14:36 +02003254 int ret, node_idx, node, cpu, i;
Bart Van Assche509c5f32016-05-12 10:50:35 -07003255 unsigned int max_sectors_per_mr, mr_per_cmd = 0;
Bart Van Assched92c0da2014-10-06 17:14:36 +02003256 bool multich = false;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003257
3258 target_host = scsi_host_alloc(&srp_template,
3259 sizeof (struct srp_target_port));
3260 if (!target_host)
3261 return -ENOMEM;
3262
David Dillow49248642011-01-14 18:23:24 -05003263 target_host->transportt = ib_srp_transport_template;
Bart Van Asschefd1b6c42011-07-13 09:19:16 -07003264 target_host->max_channel = 0;
3265 target_host->max_id = 1;
Bart Van Assche985aa492015-05-18 13:27:14 +02003266 target_host->max_lun = -1LL;
Arne Redlich3c8edf02006-11-15 12:43:00 +01003267 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08003268
Roland Dreieraef9ec32005-11-02 14:07:13 -08003269 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003270
David Dillow49248642011-01-14 18:23:24 -05003271 target->io_class = SRP_REV16A_IB_IO_CLASS;
3272 target->scsi_host = target_host;
3273 target->srp_host = host;
Christoph Hellwig5f071772016-09-05 12:56:19 +02003274 target->pd = host->srp_dev->pd;
Jason Gunthorpee6bf5f482015-07-30 17:22:22 -06003275 target->lkey = host->srp_dev->pd->local_dma_lkey;
David Dillow49248642011-01-14 18:23:24 -05003276 target->cmd_sg_cnt = cmd_sg_entries;
David Dillowc07d4242011-01-16 13:57:10 -05003277 target->sg_tablesize = indirect_sg_entries ? : cmd_sg_entries;
3278 target->allow_ext_sg = allow_ext_sg;
Vu Pham7bb312e2013-10-26 14:31:27 +02003279 target->tl_retry_count = 7;
Bart Van Assche4d73f952013-10-26 14:40:37 +02003280 target->queue_size = SRP_DEFAULT_QUEUE_SIZE;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003281
Bart Van Assche34aa6542014-10-30 14:47:22 +01003282 /*
3283 * Avoid that the SCSI host can be removed by srp_remove_target()
3284 * before this function returns.
3285 */
3286 scsi_host_get(target->scsi_host);
3287
Bart Van Assche2d7091b2014-03-14 13:52:45 +01003288 mutex_lock(&host->add_target_mutex);
3289
Roland Dreieraef9ec32005-11-02 14:07:13 -08003290 ret = srp_parse_options(buf, target);
3291 if (ret)
Bart Van Asschefb49c8b2015-05-18 13:23:14 +02003292 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003293
Bart Van Assche4d73f952013-10-26 14:40:37 +02003294 target->req_ring_size = target->queue_size - SRP_TSK_MGMT_SQ_SIZE;
3295
Bart Van Assche96fc2482013-06-28 14:51:26 +02003296 if (!srp_conn_unique(target->srp_host, target)) {
3297 shost_printk(KERN_INFO, target->scsi_host,
3298 PFX "Already connected to target port with id_ext=%016llx;ioc_guid=%016llx;initiator_ext=%016llx\n",
3299 be64_to_cpu(target->id_ext),
3300 be64_to_cpu(target->ioc_guid),
3301 be64_to_cpu(target->initiator_ext));
3302 ret = -EEXIST;
Bart Van Asschefb49c8b2015-05-18 13:23:14 +02003303 goto out;
Bart Van Assche96fc2482013-06-28 14:51:26 +02003304 }
3305
Bart Van Assche5cfb1782014-05-20 15:08:34 +02003306 if (!srp_dev->has_fmr && !srp_dev->has_fr && !target->allow_ext_sg &&
Bart Van Assched1b42892014-05-20 15:07:20 +02003307 target->cmd_sg_cnt < target->sg_tablesize) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +02003308 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 -05003309 target->sg_tablesize = target->cmd_sg_cnt;
3310 }
3311
Bart Van Assche509c5f32016-05-12 10:50:35 -07003312 if (srp_dev->use_fast_reg || srp_dev->use_fmr) {
3313 /*
3314 * FR and FMR can only map one HCA page per entry. If the
3315 * start address is not aligned on a HCA page boundary two
3316 * entries will be used for the head and the tail although
3317 * these two entries combined contain at most one HCA page of
3318 * data. Hence the "+ 1" in the calculation below.
3319 *
3320 * The indirect data buffer descriptor is contiguous so the
3321 * memory for that buffer will only be registered if
3322 * register_always is true. Hence add one to mr_per_cmd if
3323 * register_always has been set.
3324 */
3325 max_sectors_per_mr = srp_dev->max_pages_per_mr <<
3326 (ilog2(srp_dev->mr_page_size) - 9);
3327 mr_per_cmd = register_always +
3328 (target->scsi_host->max_sectors + 1 +
3329 max_sectors_per_mr - 1) / max_sectors_per_mr;
3330 pr_debug("max_sectors = %u; max_pages_per_mr = %u; mr_page_size = %u; max_sectors_per_mr = %u; mr_per_cmd = %u\n",
3331 target->scsi_host->max_sectors,
3332 srp_dev->max_pages_per_mr, srp_dev->mr_page_size,
3333 max_sectors_per_mr, mr_per_cmd);
3334 }
3335
David Dillowc07d4242011-01-16 13:57:10 -05003336 target_host->sg_tablesize = target->sg_tablesize;
Bart Van Assche509c5f32016-05-12 10:50:35 -07003337 target->mr_pool_size = target->scsi_host->can_queue * mr_per_cmd;
3338 target->mr_per_cmd = mr_per_cmd;
David Dillowc07d4242011-01-16 13:57:10 -05003339 target->indirect_size = target->sg_tablesize *
3340 sizeof (struct srp_direct_buf);
David Dillow49248642011-01-14 18:23:24 -05003341 target->max_iu_len = sizeof (struct srp_cmd) +
3342 sizeof (struct srp_indirect_buf) +
3343 target->cmd_sg_cnt * sizeof (struct srp_direct_buf);
3344
Bart Van Asschec1120f82013-10-26 14:35:08 +02003345 INIT_WORK(&target->tl_err_work, srp_tl_err_work);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00003346 INIT_WORK(&target->remove_work, srp_remove_work);
David Dillow8f26c9f2011-01-14 19:45:50 -05003347 spin_lock_init(&target->lock);
Matan Barak55ee3ab2015-10-15 18:38:45 +03003348 ret = ib_query_gid(ibdev, host->port, 0, &target->sgid, NULL);
Sagi Grimberg2088ca62014-03-14 13:51:58 +01003349 if (ret)
Bart Van Asschefb49c8b2015-05-18 13:23:14 +02003350 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003351
Bart Van Assched92c0da2014-10-06 17:14:36 +02003352 ret = -ENOMEM;
3353 target->ch_count = max_t(unsigned, num_online_nodes(),
3354 min(ch_count ? :
3355 min(4 * num_online_nodes(),
3356 ibdev->num_comp_vectors),
3357 num_online_cpus()));
3358 target->ch = kcalloc(target->ch_count, sizeof(*target->ch),
3359 GFP_KERNEL);
3360 if (!target->ch)
Bart Van Asschefb49c8b2015-05-18 13:23:14 +02003361 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003362
Bart Van Assched92c0da2014-10-06 17:14:36 +02003363 node_idx = 0;
3364 for_each_online_node(node) {
3365 const int ch_start = (node_idx * target->ch_count /
3366 num_online_nodes());
3367 const int ch_end = ((node_idx + 1) * target->ch_count /
3368 num_online_nodes());
3369 const int cv_start = (node_idx * ibdev->num_comp_vectors /
3370 num_online_nodes() + target->comp_vector)
3371 % ibdev->num_comp_vectors;
3372 const int cv_end = ((node_idx + 1) * ibdev->num_comp_vectors /
3373 num_online_nodes() + target->comp_vector)
3374 % ibdev->num_comp_vectors;
3375 int cpu_idx = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003376
Bart Van Assched92c0da2014-10-06 17:14:36 +02003377 for_each_online_cpu(cpu) {
3378 if (cpu_to_node(cpu) != node)
3379 continue;
3380 if (ch_start + cpu_idx >= ch_end)
3381 continue;
3382 ch = &target->ch[ch_start + cpu_idx];
3383 ch->target = target;
3384 ch->comp_vector = cv_start == cv_end ? cv_start :
3385 cv_start + cpu_idx % (cv_end - cv_start);
3386 spin_lock_init(&ch->lock);
3387 INIT_LIST_HEAD(&ch->free_tx);
3388 ret = srp_new_cm_id(ch);
3389 if (ret)
3390 goto err_disconnect;
3391
3392 ret = srp_create_ch_ib(ch);
3393 if (ret)
3394 goto err_disconnect;
3395
3396 ret = srp_alloc_req_data(ch);
3397 if (ret)
3398 goto err_disconnect;
3399
3400 ret = srp_connect_ch(ch, multich);
3401 if (ret) {
3402 shost_printk(KERN_ERR, target->scsi_host,
3403 PFX "Connection %d/%d failed\n",
3404 ch_start + cpu_idx,
3405 target->ch_count);
3406 if (node_idx == 0 && cpu_idx == 0) {
3407 goto err_disconnect;
3408 } else {
3409 srp_free_ch_ib(target, ch);
3410 srp_free_req_data(target, ch);
3411 target->ch_count = ch - target->ch;
Bart Van Asschec257ea62015-07-31 14:13:22 -07003412 goto connected;
Bart Van Assched92c0da2014-10-06 17:14:36 +02003413 }
3414 }
3415
3416 multich = true;
3417 cpu_idx++;
3418 }
3419 node_idx++;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003420 }
3421
Bart Van Asschec257ea62015-07-31 14:13:22 -07003422connected:
Bart Van Assched92c0da2014-10-06 17:14:36 +02003423 target->scsi_host->nr_hw_queues = target->ch_count;
3424
Roland Dreieraef9ec32005-11-02 14:07:13 -08003425 ret = srp_add_target(host, target);
3426 if (ret)
3427 goto err_disconnect;
3428
Bart Van Assche34aa6542014-10-30 14:47:22 +01003429 if (target->state != SRP_TARGET_REMOVED) {
3430 shost_printk(KERN_DEBUG, target->scsi_host, PFX
3431 "new target: id_ext %016llx ioc_guid %016llx pkey %04x service_id %016llx sgid %pI6 dgid %pI6\n",
3432 be64_to_cpu(target->id_ext),
3433 be64_to_cpu(target->ioc_guid),
Bart Van Assche747fe002014-10-30 14:48:05 +01003434 be16_to_cpu(target->pkey),
Bart Van Assche34aa6542014-10-30 14:47:22 +01003435 be64_to_cpu(target->service_id),
Bart Van Assche747fe002014-10-30 14:48:05 +01003436 target->sgid.raw, target->orig_dgid.raw);
Bart Van Assche34aa6542014-10-30 14:47:22 +01003437 }
Bart Van Asschee7ffde02014-03-14 13:52:21 +01003438
Bart Van Assche2d7091b2014-03-14 13:52:45 +01003439 ret = count;
3440
3441out:
3442 mutex_unlock(&host->add_target_mutex);
Bart Van Assche34aa6542014-10-30 14:47:22 +01003443
3444 scsi_host_put(target->scsi_host);
Bart Van Asschebc44bd12015-08-14 11:01:09 -07003445 if (ret < 0)
3446 scsi_host_put(target->scsi_host);
Bart Van Assche34aa6542014-10-30 14:47:22 +01003447
Bart Van Assche2d7091b2014-03-14 13:52:45 +01003448 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003449
3450err_disconnect:
3451 srp_disconnect_target(target);
3452
Bart Van Assched92c0da2014-10-06 17:14:36 +02003453 for (i = 0; i < target->ch_count; i++) {
3454 ch = &target->ch[i];
3455 srp_free_ch_ib(target, ch);
3456 srp_free_req_data(target, ch);
3457 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08003458
Bart Van Assched92c0da2014-10-06 17:14:36 +02003459 kfree(target->ch);
Bart Van Assche2d7091b2014-03-14 13:52:45 +01003460 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003461}
3462
Tony Jonesee959b02008-02-22 00:13:36 +01003463static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003464
Tony Jonesee959b02008-02-22 00:13:36 +01003465static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
3466 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08003467{
Tony Jonesee959b02008-02-22 00:13:36 +01003468 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003469
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01003470 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003471}
3472
Tony Jonesee959b02008-02-22 00:13:36 +01003473static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003474
Tony Jonesee959b02008-02-22 00:13:36 +01003475static ssize_t show_port(struct device *dev, struct device_attribute *attr,
3476 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08003477{
Tony Jonesee959b02008-02-22 00:13:36 +01003478 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003479
3480 return sprintf(buf, "%d\n", host->port);
3481}
3482
Tony Jonesee959b02008-02-22 00:13:36 +01003483static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003484
Roland Dreierf5358a12006-06-17 20:37:29 -07003485static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08003486{
3487 struct srp_host *host;
3488
3489 host = kzalloc(sizeof *host, GFP_KERNEL);
3490 if (!host)
3491 return NULL;
3492
3493 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07003494 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003495 init_completion(&host->released);
Bart Van Assche2d7091b2014-03-14 13:52:45 +01003496 mutex_init(&host->add_target_mutex);
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01003497 host->srp_dev = device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003498 host->port = port;
3499
Tony Jonesee959b02008-02-22 00:13:36 +01003500 host->dev.class = &srp_class;
3501 host->dev.parent = device->dev->dma_device;
Kay Sieversd927e382009-01-06 10:44:39 -08003502 dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003503
Tony Jonesee959b02008-02-22 00:13:36 +01003504 if (device_register(&host->dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07003505 goto free_host;
Tony Jonesee959b02008-02-22 00:13:36 +01003506 if (device_create_file(&host->dev, &dev_attr_add_target))
Roland Dreieraef9ec32005-11-02 14:07:13 -08003507 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01003508 if (device_create_file(&host->dev, &dev_attr_ibdev))
Roland Dreieraef9ec32005-11-02 14:07:13 -08003509 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01003510 if (device_create_file(&host->dev, &dev_attr_port))
Roland Dreieraef9ec32005-11-02 14:07:13 -08003511 goto err_class;
3512
3513 return host;
3514
3515err_class:
Tony Jonesee959b02008-02-22 00:13:36 +01003516 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003517
Roland Dreierf5358a12006-06-17 20:37:29 -07003518free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08003519 kfree(host);
3520
3521 return NULL;
3522}
3523
3524static void srp_add_one(struct ib_device *device)
3525{
Roland Dreierf5358a12006-06-17 20:37:29 -07003526 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003527 struct srp_host *host;
Hal Rosenstock41390322015-06-29 09:57:00 -04003528 int mr_page_shift, p;
Bart Van Assche52ede082014-05-20 15:07:45 +02003529 u64 max_pages_per_mr;
Christoph Hellwig5f071772016-09-05 12:56:19 +02003530 unsigned int flags = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003531
Bart Van Assche249f0652016-06-03 11:39:35 -07003532 srp_dev = kzalloc(sizeof(*srp_dev), GFP_KERNEL);
Roland Dreierf5358a12006-06-17 20:37:29 -07003533 if (!srp_dev)
Or Gerlitz4a061b22015-12-18 10:59:46 +02003534 return;
Roland Dreierf5358a12006-06-17 20:37:29 -07003535
3536 /*
3537 * Use the smallest page size supported by the HCA, down to a
David Dillow8f26c9f2011-01-14 19:45:50 -05003538 * minimum of 4096 bytes. We're unlikely to build large sglists
3539 * out of smaller entries.
Roland Dreierf5358a12006-06-17 20:37:29 -07003540 */
Or Gerlitz4a061b22015-12-18 10:59:46 +02003541 mr_page_shift = max(12, ffs(device->attrs.page_size_cap) - 1);
Bart Van Assche52ede082014-05-20 15:07:45 +02003542 srp_dev->mr_page_size = 1 << mr_page_shift;
3543 srp_dev->mr_page_mask = ~((u64) srp_dev->mr_page_size - 1);
Or Gerlitz4a061b22015-12-18 10:59:46 +02003544 max_pages_per_mr = device->attrs.max_mr_size;
Bart Van Assche52ede082014-05-20 15:07:45 +02003545 do_div(max_pages_per_mr, srp_dev->mr_page_size);
Bart Van Assche509c5f32016-05-12 10:50:35 -07003546 pr_debug("%s: %llu / %u = %llu <> %u\n", __func__,
3547 device->attrs.max_mr_size, srp_dev->mr_page_size,
3548 max_pages_per_mr, SRP_MAX_PAGES_PER_MR);
Bart Van Assche52ede082014-05-20 15:07:45 +02003549 srp_dev->max_pages_per_mr = min_t(u64, SRP_MAX_PAGES_PER_MR,
3550 max_pages_per_mr);
Bart Van Assche835ee622016-05-12 10:49:39 -07003551
3552 srp_dev->has_fmr = (device->alloc_fmr && device->dealloc_fmr &&
3553 device->map_phys_fmr && device->unmap_fmr);
3554 srp_dev->has_fr = (device->attrs.device_cap_flags &
3555 IB_DEVICE_MEM_MGT_EXTENSIONS);
Bart Van Asschec222a392016-05-12 10:51:01 -07003556 if (!never_register && !srp_dev->has_fmr && !srp_dev->has_fr) {
Bart Van Assche835ee622016-05-12 10:49:39 -07003557 dev_warn(&device->dev, "neither FMR nor FR is supported\n");
Bart Van Asschec222a392016-05-12 10:51:01 -07003558 } else if (!never_register &&
3559 device->attrs.max_mr_size >= 2 * srp_dev->mr_page_size) {
Bart Van Assche509c5f32016-05-12 10:50:35 -07003560 srp_dev->use_fast_reg = (srp_dev->has_fr &&
3561 (!srp_dev->has_fmr || prefer_fr));
3562 srp_dev->use_fmr = !srp_dev->use_fast_reg && srp_dev->has_fmr;
3563 }
Bart Van Assche835ee622016-05-12 10:49:39 -07003564
Christoph Hellwig5f071772016-09-05 12:56:19 +02003565 if (never_register || !register_always ||
3566 (!srp_dev->has_fmr && !srp_dev->has_fr))
3567 flags |= IB_PD_UNSAFE_GLOBAL_RKEY;
3568
Bart Van Assche5cfb1782014-05-20 15:08:34 +02003569 if (srp_dev->use_fast_reg) {
3570 srp_dev->max_pages_per_mr =
3571 min_t(u32, srp_dev->max_pages_per_mr,
Or Gerlitz4a061b22015-12-18 10:59:46 +02003572 device->attrs.max_fast_reg_page_list_len);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02003573 }
Bart Van Assche52ede082014-05-20 15:07:45 +02003574 srp_dev->mr_max_size = srp_dev->mr_page_size *
3575 srp_dev->max_pages_per_mr;
Or Gerlitz4a061b22015-12-18 10:59:46 +02003576 pr_debug("%s: mr_page_shift = %d, device->max_mr_size = %#llx, device->max_fast_reg_page_list_len = %u, max_pages_per_mr = %d, mr_max_size = %#x\n",
3577 device->name, mr_page_shift, device->attrs.max_mr_size,
3578 device->attrs.max_fast_reg_page_list_len,
Bart Van Assche52ede082014-05-20 15:07:45 +02003579 srp_dev->max_pages_per_mr, srp_dev->mr_max_size);
Roland Dreierf5358a12006-06-17 20:37:29 -07003580
3581 INIT_LIST_HEAD(&srp_dev->dev_list);
3582
3583 srp_dev->dev = device;
Christoph Hellwig5f071772016-09-05 12:56:19 +02003584 srp_dev->pd = ib_alloc_pd(device, flags);
Roland Dreierf5358a12006-06-17 20:37:29 -07003585 if (IS_ERR(srp_dev->pd))
3586 goto free_dev;
3587
Roland Dreierf5358a12006-06-17 20:37:29 -07003588
Hal Rosenstock41390322015-06-29 09:57:00 -04003589 for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07003590 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003591 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07003592 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003593 }
3594
Roland Dreierf5358a12006-06-17 20:37:29 -07003595 ib_set_client_data(device, &srp_client, srp_dev);
Or Gerlitz4a061b22015-12-18 10:59:46 +02003596 return;
Roland Dreierf5358a12006-06-17 20:37:29 -07003597
Roland Dreierf5358a12006-06-17 20:37:29 -07003598free_dev:
3599 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003600}
3601
Haggai Eran7c1eb452015-07-30 17:50:14 +03003602static void srp_remove_one(struct ib_device *device, void *client_data)
Roland Dreieraef9ec32005-11-02 14:07:13 -08003603{
Roland Dreierf5358a12006-06-17 20:37:29 -07003604 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003605 struct srp_host *host, *tmp_host;
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00003606 struct srp_target_port *target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003607
Haggai Eran7c1eb452015-07-30 17:50:14 +03003608 srp_dev = client_data;
Dotan Barak1fe0cb82013-06-12 15:20:36 +02003609 if (!srp_dev)
3610 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003611
Roland Dreierf5358a12006-06-17 20:37:29 -07003612 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Tony Jonesee959b02008-02-22 00:13:36 +01003613 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003614 /*
3615 * Wait for the sysfs entry to go away, so that no new
3616 * target ports can be created.
3617 */
3618 wait_for_completion(&host->released);
3619
3620 /*
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00003621 * Remove all target ports.
Roland Dreieraef9ec32005-11-02 14:07:13 -08003622 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07003623 spin_lock(&host->target_lock);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00003624 list_for_each_entry(target, &host->target_list, list)
3625 srp_queue_remove_work(target);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07003626 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003627
3628 /*
Bart Van Asschebcc05912014-07-09 15:57:26 +02003629 * Wait for tl_err and target port removal tasks.
Roland Dreieraef9ec32005-11-02 14:07:13 -08003630 */
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00003631 flush_workqueue(system_long_wq);
Bart Van Asschebcc05912014-07-09 15:57:26 +02003632 flush_workqueue(srp_remove_wq);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003633
Roland Dreieraef9ec32005-11-02 14:07:13 -08003634 kfree(host);
3635 }
3636
Roland Dreierf5358a12006-06-17 20:37:29 -07003637 ib_dealloc_pd(srp_dev->pd);
3638
3639 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003640}
3641
FUJITA Tomonori32368222007-06-27 16:33:12 +09003642static struct srp_function_template ib_srp_transport_functions = {
Bart Van Asscheed9b2262013-10-26 14:34:27 +02003643 .has_rport_state = true,
3644 .reset_timer_if_blocked = true,
Bart Van Asschea95cadb2013-10-26 14:37:17 +02003645 .reconnect_delay = &srp_reconnect_delay,
Bart Van Asscheed9b2262013-10-26 14:34:27 +02003646 .fast_io_fail_tmo = &srp_fast_io_fail_tmo,
3647 .dev_loss_tmo = &srp_dev_loss_tmo,
3648 .reconnect = srp_rport_reconnect,
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02003649 .rport_delete = srp_rport_delete,
Bart Van Asscheed9b2262013-10-26 14:34:27 +02003650 .terminate_rport_io = srp_terminate_io,
FUJITA Tomonori32368222007-06-27 16:33:12 +09003651};
3652
Roland Dreieraef9ec32005-11-02 14:07:13 -08003653static int __init srp_init_module(void)
3654{
3655 int ret;
3656
David Dillow49248642011-01-14 18:23:24 -05003657 if (srp_sg_tablesize) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003658 pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n");
David Dillow49248642011-01-14 18:23:24 -05003659 if (!cmd_sg_entries)
3660 cmd_sg_entries = srp_sg_tablesize;
3661 }
3662
3663 if (!cmd_sg_entries)
3664 cmd_sg_entries = SRP_DEF_SG_TABLESIZE;
3665
3666 if (cmd_sg_entries > 255) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003667 pr_warn("Clamping cmd_sg_entries to 255\n");
David Dillow49248642011-01-14 18:23:24 -05003668 cmd_sg_entries = 255;
David Dillow1e89a192008-04-16 21:01:12 -07003669 }
3670
David Dillowc07d4242011-01-16 13:57:10 -05003671 if (!indirect_sg_entries)
3672 indirect_sg_entries = cmd_sg_entries;
3673 else if (indirect_sg_entries < cmd_sg_entries) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003674 pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n",
3675 cmd_sg_entries);
David Dillowc07d4242011-01-16 13:57:10 -05003676 indirect_sg_entries = cmd_sg_entries;
3677 }
3678
Bart Van Asschebcc05912014-07-09 15:57:26 +02003679 srp_remove_wq = create_workqueue("srp_remove");
Wei Yongjunda05be22014-08-14 08:56:22 +08003680 if (!srp_remove_wq) {
3681 ret = -ENOMEM;
Bart Van Asschebcc05912014-07-09 15:57:26 +02003682 goto out;
3683 }
3684
3685 ret = -ENOMEM;
FUJITA Tomonori32368222007-06-27 16:33:12 +09003686 ib_srp_transport_template =
3687 srp_attach_transport(&ib_srp_transport_functions);
3688 if (!ib_srp_transport_template)
Bart Van Asschebcc05912014-07-09 15:57:26 +02003689 goto destroy_wq;
FUJITA Tomonori32368222007-06-27 16:33:12 +09003690
Roland Dreieraef9ec32005-11-02 14:07:13 -08003691 ret = class_register(&srp_class);
3692 if (ret) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003693 pr_err("couldn't register class infiniband_srp\n");
Bart Van Asschebcc05912014-07-09 15:57:26 +02003694 goto release_tr;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003695 }
3696
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003697 ib_sa_register_client(&srp_sa_client);
3698
Roland Dreieraef9ec32005-11-02 14:07:13 -08003699 ret = ib_register_client(&srp_client);
3700 if (ret) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003701 pr_err("couldn't register IB client\n");
Bart Van Asschebcc05912014-07-09 15:57:26 +02003702 goto unreg_sa;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003703 }
3704
Bart Van Asschebcc05912014-07-09 15:57:26 +02003705out:
3706 return ret;
3707
3708unreg_sa:
3709 ib_sa_unregister_client(&srp_sa_client);
3710 class_unregister(&srp_class);
3711
3712release_tr:
3713 srp_release_transport(ib_srp_transport_template);
3714
3715destroy_wq:
3716 destroy_workqueue(srp_remove_wq);
3717 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003718}
3719
3720static void __exit srp_cleanup_module(void)
3721{
3722 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003723 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003724 class_unregister(&srp_class);
FUJITA Tomonori32368222007-06-27 16:33:12 +09003725 srp_release_transport(ib_srp_transport_template);
Bart Van Asschebcc05912014-07-09 15:57:26 +02003726 destroy_workqueue(srp_remove_wq);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003727}
3728
3729module_init(srp_init_module);
3730module_exit(srp_cleanup_module);