blob: 269f5ebe82de253bbf355d5ad92cecc8b66c1f55 [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;
Roland Dreieraef9ec32005-11-02 14:07:13 -080073static int topspin_workarounds = 1;
74
David Dillow49248642011-01-14 18:23:24 -050075module_param(srp_sg_tablesize, uint, 0444);
76MODULE_PARM_DESC(srp_sg_tablesize, "Deprecated name for cmd_sg_entries");
77
78module_param(cmd_sg_entries, uint, 0444);
79MODULE_PARM_DESC(cmd_sg_entries,
80 "Default number of gather/scatter entries in the SRP command (default is 12, max 255)");
81
David Dillowc07d4242011-01-16 13:57:10 -050082module_param(indirect_sg_entries, uint, 0444);
83MODULE_PARM_DESC(indirect_sg_entries,
84 "Default max number of gather/scatter entries (default is 12, max is " __stringify(SCSI_MAX_SG_CHAIN_SEGMENTS) ")");
85
86module_param(allow_ext_sg, bool, 0444);
87MODULE_PARM_DESC(allow_ext_sg,
88 "Default behavior when there are more than cmd_sg_entries S/G entries after mapping; fails the request when false (default false)");
89
Roland Dreieraef9ec32005-11-02 14:07:13 -080090module_param(topspin_workarounds, int, 0444);
91MODULE_PARM_DESC(topspin_workarounds,
92 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
93
Bart Van Assche5cfb1782014-05-20 15:08:34 +020094module_param(prefer_fr, bool, 0444);
95MODULE_PARM_DESC(prefer_fr,
96"Whether to use fast registration if both FMR and fast registration are supported");
97
Bart Van Asscheb1b88542014-05-20 15:06:41 +020098module_param(register_always, bool, 0444);
99MODULE_PARM_DESC(register_always,
100 "Use memory registration even for contiguous memory regions");
101
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930102static const struct kernel_param_ops srp_tmo_ops;
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200103
Bart Van Asschea95cadb2013-10-26 14:37:17 +0200104static int srp_reconnect_delay = 10;
105module_param_cb(reconnect_delay, &srp_tmo_ops, &srp_reconnect_delay,
106 S_IRUGO | S_IWUSR);
107MODULE_PARM_DESC(reconnect_delay, "Time between successive reconnect attempts");
108
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200109static int srp_fast_io_fail_tmo = 15;
110module_param_cb(fast_io_fail_tmo, &srp_tmo_ops, &srp_fast_io_fail_tmo,
111 S_IRUGO | S_IWUSR);
112MODULE_PARM_DESC(fast_io_fail_tmo,
113 "Number of seconds between the observation of a transport"
114 " layer error and failing all I/O. \"off\" means that this"
115 " functionality is disabled.");
116
Bart Van Asschea95cadb2013-10-26 14:37:17 +0200117static int srp_dev_loss_tmo = 600;
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200118module_param_cb(dev_loss_tmo, &srp_tmo_ops, &srp_dev_loss_tmo,
119 S_IRUGO | S_IWUSR);
120MODULE_PARM_DESC(dev_loss_tmo,
121 "Maximum number of seconds that the SRP transport should"
122 " insulate transport layer errors. After this time has been"
123 " exceeded the SCSI host is removed. Should be"
124 " between 1 and " __stringify(SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
125 " if fast_io_fail_tmo has not been set. \"off\" means that"
126 " this functionality is disabled.");
127
Bart Van Assched92c0da2014-10-06 17:14:36 +0200128static unsigned ch_count;
129module_param(ch_count, uint, 0444);
130MODULE_PARM_DESC(ch_count,
131 "Number of RDMA channels to use for communication with an SRP target. Using more than one channel improves performance if the HCA supports multiple completion vectors. The default value is the minimum of four times the number of online CPU sockets and the number of completion vectors supported by the HCA.");
132
Roland Dreieraef9ec32005-11-02 14:07:13 -0800133static void srp_add_one(struct ib_device *device);
Haggai Eran7c1eb452015-07-30 17:50:14 +0300134static void srp_remove_one(struct ib_device *device, void *client_data);
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +0100135static void srp_recv_done(struct ib_cq *cq, struct ib_wc *wc);
136static void srp_handle_qp_err(struct ib_cq *cq, struct ib_wc *wc,
137 const char *opname);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800138static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
139
FUJITA Tomonori32368222007-06-27 16:33:12 +0900140static struct scsi_transport_template *ib_srp_transport_template;
Bart Van Asschebcc05912014-07-09 15:57:26 +0200141static struct workqueue_struct *srp_remove_wq;
FUJITA Tomonori32368222007-06-27 16:33:12 +0900142
Roland Dreieraef9ec32005-11-02 14:07:13 -0800143static struct ib_client srp_client = {
144 .name = "srp",
145 .add = srp_add_one,
146 .remove = srp_remove_one
147};
148
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700149static struct ib_sa_client srp_sa_client;
150
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200151static int srp_tmo_get(char *buffer, const struct kernel_param *kp)
152{
153 int tmo = *(int *)kp->arg;
154
155 if (tmo >= 0)
156 return sprintf(buffer, "%d", tmo);
157 else
158 return sprintf(buffer, "off");
159}
160
161static int srp_tmo_set(const char *val, const struct kernel_param *kp)
162{
163 int tmo, res;
164
Sagi Grimberg3fdf70a2015-06-25 13:34:15 +0300165 res = srp_parse_tmo(&tmo, val);
166 if (res)
167 goto out;
168
Bart Van Asschea95cadb2013-10-26 14:37:17 +0200169 if (kp->arg == &srp_reconnect_delay)
170 res = srp_tmo_valid(tmo, srp_fast_io_fail_tmo,
171 srp_dev_loss_tmo);
172 else if (kp->arg == &srp_fast_io_fail_tmo)
173 res = srp_tmo_valid(srp_reconnect_delay, tmo, srp_dev_loss_tmo);
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200174 else
Bart Van Asschea95cadb2013-10-26 14:37:17 +0200175 res = srp_tmo_valid(srp_reconnect_delay, srp_fast_io_fail_tmo,
176 tmo);
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200177 if (res)
178 goto out;
179 *(int *)kp->arg = tmo;
180
181out:
182 return res;
183}
184
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930185static const struct kernel_param_ops srp_tmo_ops = {
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200186 .get = srp_tmo_get,
187 .set = srp_tmo_set,
188};
189
Roland Dreieraef9ec32005-11-02 14:07:13 -0800190static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
191{
192 return (struct srp_target_port *) host->hostdata;
193}
194
195static const char *srp_target_info(struct Scsi_Host *host)
196{
197 return host_to_target(host)->target_name;
198}
199
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700200static int srp_target_is_topspin(struct srp_target_port *target)
201{
202 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700203 static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700204
205 return topspin_workarounds &&
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700206 (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
207 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700208}
209
Roland Dreieraef9ec32005-11-02 14:07:13 -0800210static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
211 gfp_t gfp_mask,
212 enum dma_data_direction direction)
213{
214 struct srp_iu *iu;
215
216 iu = kmalloc(sizeof *iu, gfp_mask);
217 if (!iu)
218 goto out;
219
220 iu->buf = kzalloc(size, gfp_mask);
221 if (!iu->buf)
222 goto out_free_iu;
223
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100224 iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
225 direction);
226 if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800227 goto out_free_buf;
228
229 iu->size = size;
230 iu->direction = direction;
231
232 return iu;
233
234out_free_buf:
235 kfree(iu->buf);
236out_free_iu:
237 kfree(iu);
238out:
239 return NULL;
240}
241
242static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
243{
244 if (!iu)
245 return;
246
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100247 ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
248 iu->direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800249 kfree(iu->buf);
250 kfree(iu);
251}
252
253static void srp_qp_event(struct ib_event *event, void *context)
254{
Sagi Grimberg57363d92015-05-18 13:40:29 +0300255 pr_debug("QP event %s (%d)\n",
256 ib_event_msg(event->event), event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800257}
258
259static int srp_init_qp(struct srp_target_port *target,
260 struct ib_qp *qp)
261{
262 struct ib_qp_attr *attr;
263 int ret;
264
265 attr = kmalloc(sizeof *attr, GFP_KERNEL);
266 if (!attr)
267 return -ENOMEM;
268
Bart Van Assche56b53902014-07-09 15:58:22 +0200269 ret = ib_find_cached_pkey(target->srp_host->srp_dev->dev,
270 target->srp_host->port,
271 be16_to_cpu(target->pkey),
272 &attr->pkey_index);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800273 if (ret)
274 goto out;
275
276 attr->qp_state = IB_QPS_INIT;
277 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
278 IB_ACCESS_REMOTE_WRITE);
279 attr->port_num = target->srp_host->port;
280
281 ret = ib_modify_qp(qp, attr,
282 IB_QP_STATE |
283 IB_QP_PKEY_INDEX |
284 IB_QP_ACCESS_FLAGS |
285 IB_QP_PORT);
286
287out:
288 kfree(attr);
289 return ret;
290}
291
Bart Van Assche509c07b2014-10-30 14:48:30 +0100292static int srp_new_cm_id(struct srp_rdma_ch *ch)
David Dillow9fe4bcf2008-01-08 17:08:52 -0500293{
Bart Van Assche509c07b2014-10-30 14:48:30 +0100294 struct srp_target_port *target = ch->target;
David Dillow9fe4bcf2008-01-08 17:08:52 -0500295 struct ib_cm_id *new_cm_id;
296
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100297 new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
Bart Van Assche509c07b2014-10-30 14:48:30 +0100298 srp_cm_handler, ch);
David Dillow9fe4bcf2008-01-08 17:08:52 -0500299 if (IS_ERR(new_cm_id))
300 return PTR_ERR(new_cm_id);
301
Bart Van Assche509c07b2014-10-30 14:48:30 +0100302 if (ch->cm_id)
303 ib_destroy_cm_id(ch->cm_id);
304 ch->cm_id = new_cm_id;
305 ch->path.sgid = target->sgid;
306 ch->path.dgid = target->orig_dgid;
307 ch->path.pkey = target->pkey;
308 ch->path.service_id = target->service_id;
David Dillow9fe4bcf2008-01-08 17:08:52 -0500309
310 return 0;
311}
312
Bart Van Assched1b42892014-05-20 15:07:20 +0200313static struct ib_fmr_pool *srp_alloc_fmr_pool(struct srp_target_port *target)
314{
315 struct srp_device *dev = target->srp_host->srp_dev;
316 struct ib_fmr_pool_param fmr_param;
317
318 memset(&fmr_param, 0, sizeof(fmr_param));
319 fmr_param.pool_size = target->scsi_host->can_queue;
320 fmr_param.dirty_watermark = fmr_param.pool_size / 4;
321 fmr_param.cache = 1;
Bart Van Assche52ede082014-05-20 15:07:45 +0200322 fmr_param.max_pages_per_fmr = dev->max_pages_per_mr;
323 fmr_param.page_shift = ilog2(dev->mr_page_size);
Bart Van Assched1b42892014-05-20 15:07:20 +0200324 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
325 IB_ACCESS_REMOTE_WRITE |
326 IB_ACCESS_REMOTE_READ);
327
328 return ib_create_fmr_pool(dev->pd, &fmr_param);
329}
330
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200331/**
332 * srp_destroy_fr_pool() - free the resources owned by a pool
333 * @pool: Fast registration pool to be destroyed.
334 */
335static void srp_destroy_fr_pool(struct srp_fr_pool *pool)
336{
337 int i;
338 struct srp_fr_desc *d;
339
340 if (!pool)
341 return;
342
343 for (i = 0, d = &pool->desc[0]; i < pool->size; i++, d++) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200344 if (d->mr)
345 ib_dereg_mr(d->mr);
346 }
347 kfree(pool);
348}
349
350/**
351 * srp_create_fr_pool() - allocate and initialize a pool for fast registration
352 * @device: IB device to allocate fast registration descriptors for.
353 * @pd: Protection domain associated with the FR descriptors.
354 * @pool_size: Number of descriptors to allocate.
355 * @max_page_list_len: Maximum fast registration work request page list length.
356 */
357static struct srp_fr_pool *srp_create_fr_pool(struct ib_device *device,
358 struct ib_pd *pd, int pool_size,
359 int max_page_list_len)
360{
361 struct srp_fr_pool *pool;
362 struct srp_fr_desc *d;
363 struct ib_mr *mr;
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200364 int i, ret = -EINVAL;
365
366 if (pool_size <= 0)
367 goto err;
368 ret = -ENOMEM;
369 pool = kzalloc(sizeof(struct srp_fr_pool) +
370 pool_size * sizeof(struct srp_fr_desc), GFP_KERNEL);
371 if (!pool)
372 goto err;
373 pool->size = pool_size;
374 pool->max_page_list_len = max_page_list_len;
375 spin_lock_init(&pool->lock);
376 INIT_LIST_HEAD(&pool->free_list);
377
378 for (i = 0, d = &pool->desc[0]; i < pool->size; i++, d++) {
Sagi Grimberg563b67c2015-07-30 10:32:38 +0300379 mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
380 max_page_list_len);
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200381 if (IS_ERR(mr)) {
382 ret = PTR_ERR(mr);
383 goto destroy_pool;
384 }
385 d->mr = mr;
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200386 list_add_tail(&d->entry, &pool->free_list);
387 }
388
389out:
390 return pool;
391
392destroy_pool:
393 srp_destroy_fr_pool(pool);
394
395err:
396 pool = ERR_PTR(ret);
397 goto out;
398}
399
400/**
401 * srp_fr_pool_get() - obtain a descriptor suitable for fast registration
402 * @pool: Pool to obtain descriptor from.
403 */
404static struct srp_fr_desc *srp_fr_pool_get(struct srp_fr_pool *pool)
405{
406 struct srp_fr_desc *d = NULL;
407 unsigned long flags;
408
409 spin_lock_irqsave(&pool->lock, flags);
410 if (!list_empty(&pool->free_list)) {
411 d = list_first_entry(&pool->free_list, typeof(*d), entry);
412 list_del(&d->entry);
413 }
414 spin_unlock_irqrestore(&pool->lock, flags);
415
416 return d;
417}
418
419/**
420 * srp_fr_pool_put() - put an FR descriptor back in the free list
421 * @pool: Pool the descriptor was allocated from.
422 * @desc: Pointer to an array of fast registration descriptor pointers.
423 * @n: Number of descriptors to put back.
424 *
425 * Note: The caller must already have queued an invalidation request for
426 * desc->mr->rkey before calling this function.
427 */
428static void srp_fr_pool_put(struct srp_fr_pool *pool, struct srp_fr_desc **desc,
429 int n)
430{
431 unsigned long flags;
432 int i;
433
434 spin_lock_irqsave(&pool->lock, flags);
435 for (i = 0; i < n; i++)
436 list_add(&desc[i]->entry, &pool->free_list);
437 spin_unlock_irqrestore(&pool->lock, flags);
438}
439
440static struct srp_fr_pool *srp_alloc_fr_pool(struct srp_target_port *target)
441{
442 struct srp_device *dev = target->srp_host->srp_dev;
443
444 return srp_create_fr_pool(dev->dev, dev->pd,
445 target->scsi_host->can_queue,
446 dev->max_pages_per_mr);
447}
448
Bart Van Assche7dad6b22014-10-21 18:00:35 +0200449/**
450 * srp_destroy_qp() - destroy an RDMA queue pair
451 * @ch: SRP RDMA channel.
452 *
Steve Wise561392d2016-02-17 08:15:42 -0800453 * Drain the qp before destroying it. This avoids that the receive
454 * completion handler can access the queue pair while it is
Bart Van Assche7dad6b22014-10-21 18:00:35 +0200455 * being destroyed.
456 */
457static void srp_destroy_qp(struct srp_rdma_ch *ch)
458{
Steve Wise561392d2016-02-17 08:15:42 -0800459 ib_drain_rq(ch->qp);
Bart Van Assche7dad6b22014-10-21 18:00:35 +0200460 ib_destroy_qp(ch->qp);
461}
462
Bart Van Assche509c07b2014-10-30 14:48:30 +0100463static int srp_create_ch_ib(struct srp_rdma_ch *ch)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800464{
Bart Van Assche509c07b2014-10-30 14:48:30 +0100465 struct srp_target_port *target = ch->target;
Bart Van Assche62154b22014-05-20 15:04:45 +0200466 struct srp_device *dev = target->srp_host->srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800467 struct ib_qp_init_attr *init_attr;
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100468 struct ib_cq *recv_cq, *send_cq;
469 struct ib_qp *qp;
Bart Van Assched1b42892014-05-20 15:07:20 +0200470 struct ib_fmr_pool *fmr_pool = NULL;
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200471 struct srp_fr_pool *fr_pool = NULL;
Sagi Grimberg09c0c0b2015-12-01 10:18:03 -0800472 const int m = dev->use_fast_reg ? 3 : 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800473 int ret;
474
475 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
476 if (!init_attr)
477 return -ENOMEM;
478
Steve Wise561392d2016-02-17 08:15:42 -0800479 /* queue_size + 1 for ib_drain_rq() */
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +0100480 recv_cq = ib_alloc_cq(dev->dev, ch, target->queue_size + 1,
481 ch->comp_vector, IB_POLL_SOFTIRQ);
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100482 if (IS_ERR(recv_cq)) {
483 ret = PTR_ERR(recv_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800484 goto err;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800485 }
486
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +0100487 send_cq = ib_alloc_cq(dev->dev, ch, m * target->queue_size,
488 ch->comp_vector, IB_POLL_DIRECT);
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100489 if (IS_ERR(send_cq)) {
490 ret = PTR_ERR(send_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800491 goto err_recv_cq;
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000492 }
493
Roland Dreieraef9ec32005-11-02 14:07:13 -0800494 init_attr->event_handler = srp_qp_event;
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200495 init_attr->cap.max_send_wr = m * target->queue_size;
Bart Van Assche7dad6b22014-10-21 18:00:35 +0200496 init_attr->cap.max_recv_wr = target->queue_size + 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800497 init_attr->cap.max_recv_sge = 1;
498 init_attr->cap.max_send_sge = 1;
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200499 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800500 init_attr->qp_type = IB_QPT_RC;
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100501 init_attr->send_cq = send_cq;
502 init_attr->recv_cq = recv_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800503
Bart Van Assche62154b22014-05-20 15:04:45 +0200504 qp = ib_create_qp(dev->pd, init_attr);
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100505 if (IS_ERR(qp)) {
506 ret = PTR_ERR(qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800507 goto err_send_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800508 }
509
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100510 ret = srp_init_qp(target, qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800511 if (ret)
512 goto err_qp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800513
Bart Van Assche002f1562015-08-10 17:08:44 -0700514 if (dev->use_fast_reg) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200515 fr_pool = srp_alloc_fr_pool(target);
516 if (IS_ERR(fr_pool)) {
517 ret = PTR_ERR(fr_pool);
518 shost_printk(KERN_WARNING, target->scsi_host, PFX
519 "FR pool allocation failed (%d)\n", ret);
520 goto err_qp;
521 }
Bart Van Assche002f1562015-08-10 17:08:44 -0700522 } else if (dev->use_fmr) {
Bart Van Assched1b42892014-05-20 15:07:20 +0200523 fmr_pool = srp_alloc_fmr_pool(target);
524 if (IS_ERR(fmr_pool)) {
525 ret = PTR_ERR(fmr_pool);
526 shost_printk(KERN_WARNING, target->scsi_host, PFX
527 "FMR pool allocation failed (%d)\n", ret);
528 goto err_qp;
529 }
Bart Van Assched1b42892014-05-20 15:07:20 +0200530 }
531
Bart Van Assche509c07b2014-10-30 14:48:30 +0100532 if (ch->qp)
Bart Van Assche7dad6b22014-10-21 18:00:35 +0200533 srp_destroy_qp(ch);
Bart Van Assche509c07b2014-10-30 14:48:30 +0100534 if (ch->recv_cq)
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +0100535 ib_free_cq(ch->recv_cq);
Bart Van Assche509c07b2014-10-30 14:48:30 +0100536 if (ch->send_cq)
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +0100537 ib_free_cq(ch->send_cq);
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100538
Bart Van Assche509c07b2014-10-30 14:48:30 +0100539 ch->qp = qp;
540 ch->recv_cq = recv_cq;
541 ch->send_cq = send_cq;
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100542
Sagi Grimberg7fbc67d2015-08-24 19:04:51 +0300543 if (dev->use_fast_reg) {
544 if (ch->fr_pool)
545 srp_destroy_fr_pool(ch->fr_pool);
546 ch->fr_pool = fr_pool;
547 } else if (dev->use_fmr) {
548 if (ch->fmr_pool)
549 ib_destroy_fmr_pool(ch->fmr_pool);
550 ch->fmr_pool = fmr_pool;
551 }
552
Roland Dreierda9d2f02010-02-24 15:07:59 -0800553 kfree(init_attr);
554 return 0;
555
556err_qp:
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +0100557 srp_destroy_qp(ch);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800558
559err_send_cq:
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +0100560 ib_free_cq(send_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800561
562err_recv_cq:
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +0100563 ib_free_cq(recv_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800564
565err:
Roland Dreieraef9ec32005-11-02 14:07:13 -0800566 kfree(init_attr);
567 return ret;
568}
569
Bart Van Assche4d73f952013-10-26 14:40:37 +0200570/*
571 * Note: this function may be called without srp_alloc_iu_bufs() having been
Bart Van Assche509c07b2014-10-30 14:48:30 +0100572 * invoked. Hence the ch->[rt]x_ring checks.
Bart Van Assche4d73f952013-10-26 14:40:37 +0200573 */
Bart Van Assche509c07b2014-10-30 14:48:30 +0100574static void srp_free_ch_ib(struct srp_target_port *target,
575 struct srp_rdma_ch *ch)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800576{
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200577 struct srp_device *dev = target->srp_host->srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800578 int i;
579
Bart Van Assched92c0da2014-10-06 17:14:36 +0200580 if (!ch->target)
581 return;
582
Bart Van Assche509c07b2014-10-30 14:48:30 +0100583 if (ch->cm_id) {
584 ib_destroy_cm_id(ch->cm_id);
585 ch->cm_id = NULL;
Bart Van Assche394c5952014-10-30 14:46:27 +0100586 }
587
Bart Van Assched92c0da2014-10-06 17:14:36 +0200588 /* If srp_new_cm_id() succeeded but srp_create_ch_ib() not, return. */
589 if (!ch->qp)
590 return;
591
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200592 if (dev->use_fast_reg) {
Bart Van Assche509c07b2014-10-30 14:48:30 +0100593 if (ch->fr_pool)
594 srp_destroy_fr_pool(ch->fr_pool);
Bart Van Assche002f1562015-08-10 17:08:44 -0700595 } else if (dev->use_fmr) {
Bart Van Assche509c07b2014-10-30 14:48:30 +0100596 if (ch->fmr_pool)
597 ib_destroy_fmr_pool(ch->fmr_pool);
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200598 }
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +0100599
Bart Van Assche7dad6b22014-10-21 18:00:35 +0200600 srp_destroy_qp(ch);
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +0100601 ib_free_cq(ch->send_cq);
602 ib_free_cq(ch->recv_cq);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800603
Bart Van Assched92c0da2014-10-06 17:14:36 +0200604 /*
605 * Avoid that the SCSI error handler tries to use this channel after
606 * it has been freed. The SCSI error handler can namely continue
607 * trying to perform recovery actions after scsi_remove_host()
608 * returned.
609 */
610 ch->target = NULL;
611
Bart Van Assche509c07b2014-10-30 14:48:30 +0100612 ch->qp = NULL;
613 ch->send_cq = ch->recv_cq = NULL;
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100614
Bart Van Assche509c07b2014-10-30 14:48:30 +0100615 if (ch->rx_ring) {
Bart Van Assche4d73f952013-10-26 14:40:37 +0200616 for (i = 0; i < target->queue_size; ++i)
Bart Van Assche509c07b2014-10-30 14:48:30 +0100617 srp_free_iu(target->srp_host, ch->rx_ring[i]);
618 kfree(ch->rx_ring);
619 ch->rx_ring = NULL;
Bart Van Assche4d73f952013-10-26 14:40:37 +0200620 }
Bart Van Assche509c07b2014-10-30 14:48:30 +0100621 if (ch->tx_ring) {
Bart Van Assche4d73f952013-10-26 14:40:37 +0200622 for (i = 0; i < target->queue_size; ++i)
Bart Van Assche509c07b2014-10-30 14:48:30 +0100623 srp_free_iu(target->srp_host, ch->tx_ring[i]);
624 kfree(ch->tx_ring);
625 ch->tx_ring = NULL;
Bart Van Assche4d73f952013-10-26 14:40:37 +0200626 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800627}
628
629static void srp_path_rec_completion(int status,
630 struct ib_sa_path_rec *pathrec,
Bart Van Assche509c07b2014-10-30 14:48:30 +0100631 void *ch_ptr)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800632{
Bart Van Assche509c07b2014-10-30 14:48:30 +0100633 struct srp_rdma_ch *ch = ch_ptr;
634 struct srp_target_port *target = ch->target;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800635
Bart Van Assche509c07b2014-10-30 14:48:30 +0100636 ch->status = status;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800637 if (status)
David Dillow7aa54bd2008-01-07 18:23:41 -0500638 shost_printk(KERN_ERR, target->scsi_host,
639 PFX "Got failed path rec status %d\n", status);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800640 else
Bart Van Assche509c07b2014-10-30 14:48:30 +0100641 ch->path = *pathrec;
642 complete(&ch->done);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800643}
644
Bart Van Assche509c07b2014-10-30 14:48:30 +0100645static int srp_lookup_path(struct srp_rdma_ch *ch)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800646{
Bart Van Assche509c07b2014-10-30 14:48:30 +0100647 struct srp_target_port *target = ch->target;
Bart Van Asschea702adc2014-03-14 13:53:10 +0100648 int ret;
649
Bart Van Assche509c07b2014-10-30 14:48:30 +0100650 ch->path.numb_path = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800651
Bart Van Assche509c07b2014-10-30 14:48:30 +0100652 init_completion(&ch->done);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800653
Bart Van Assche509c07b2014-10-30 14:48:30 +0100654 ch->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
655 target->srp_host->srp_dev->dev,
656 target->srp_host->port,
657 &ch->path,
658 IB_SA_PATH_REC_SERVICE_ID |
659 IB_SA_PATH_REC_DGID |
660 IB_SA_PATH_REC_SGID |
661 IB_SA_PATH_REC_NUMB_PATH |
662 IB_SA_PATH_REC_PKEY,
663 SRP_PATH_REC_TIMEOUT_MS,
664 GFP_KERNEL,
665 srp_path_rec_completion,
666 ch, &ch->path_query);
667 if (ch->path_query_id < 0)
668 return ch->path_query_id;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800669
Bart Van Assche509c07b2014-10-30 14:48:30 +0100670 ret = wait_for_completion_interruptible(&ch->done);
Bart Van Asschea702adc2014-03-14 13:53:10 +0100671 if (ret < 0)
672 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800673
Bart Van Assche509c07b2014-10-30 14:48:30 +0100674 if (ch->status < 0)
David Dillow7aa54bd2008-01-07 18:23:41 -0500675 shost_printk(KERN_WARNING, target->scsi_host,
676 PFX "Path record query failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800677
Bart Van Assche509c07b2014-10-30 14:48:30 +0100678 return ch->status;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800679}
680
Bart Van Assched92c0da2014-10-06 17:14:36 +0200681static int srp_send_req(struct srp_rdma_ch *ch, bool multich)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800682{
Bart Van Assche509c07b2014-10-30 14:48:30 +0100683 struct srp_target_port *target = ch->target;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800684 struct {
685 struct ib_cm_req_param param;
686 struct srp_login_req priv;
687 } *req = NULL;
688 int status;
689
690 req = kzalloc(sizeof *req, GFP_KERNEL);
691 if (!req)
692 return -ENOMEM;
693
Bart Van Assche509c07b2014-10-30 14:48:30 +0100694 req->param.primary_path = &ch->path;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800695 req->param.alternate_path = NULL;
696 req->param.service_id = target->service_id;
Bart Van Assche509c07b2014-10-30 14:48:30 +0100697 req->param.qp_num = ch->qp->qp_num;
698 req->param.qp_type = ch->qp->qp_type;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800699 req->param.private_data = &req->priv;
700 req->param.private_data_len = sizeof req->priv;
701 req->param.flow_control = 1;
702
703 get_random_bytes(&req->param.starting_psn, 4);
704 req->param.starting_psn &= 0xffffff;
705
706 /*
707 * Pick some arbitrary defaults here; we could make these
708 * module parameters if anyone cared about setting them.
709 */
710 req->param.responder_resources = 4;
711 req->param.remote_cm_response_timeout = 20;
712 req->param.local_cm_response_timeout = 20;
Vu Pham7bb312e2013-10-26 14:31:27 +0200713 req->param.retry_count = target->tl_retry_count;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800714 req->param.rnr_retry_count = 7;
715 req->param.max_cm_retries = 15;
716
717 req->priv.opcode = SRP_LOGIN_REQ;
718 req->priv.tag = 0;
David Dillow49248642011-01-14 18:23:24 -0500719 req->priv.req_it_iu_len = cpu_to_be32(target->max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800720 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
721 SRP_BUF_FORMAT_INDIRECT);
Bart Van Assched92c0da2014-10-06 17:14:36 +0200722 req->priv.req_flags = (multich ? SRP_MULTICHAN_MULTI :
723 SRP_MULTICHAN_SINGLE);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700724 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700725 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450db2006-06-17 20:37:38 -0700726 * port identifier format is 8 bytes of ID extension followed
727 * by 8 bytes of GUID. Older drafts put the two halves in the
728 * opposite order, so that the GUID comes first.
729 *
730 * Targets conforming to these obsolete drafts can be
731 * recognized by the I/O Class they report.
732 */
733 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
734 memcpy(req->priv.initiator_port_id,
Bart Van Assche747fe002014-10-30 14:48:05 +0100735 &target->sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700736 memcpy(req->priv.initiator_port_id + 8,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200737 &target->initiator_ext, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700738 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
739 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
740 } else {
741 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200742 &target->initiator_ext, 8);
743 memcpy(req->priv.initiator_port_id + 8,
Bart Van Assche747fe002014-10-30 14:48:05 +0100744 &target->sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700745 memcpy(req->priv.target_port_id, &target->id_ext, 8);
746 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
747 }
748
Roland Dreieraef9ec32005-11-02 14:07:13 -0800749 /*
750 * Topspin/Cisco SRP targets will reject our login unless we
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200751 * zero out the first 8 bytes of our initiator port ID and set
752 * the second 8 bytes to the local node GUID.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800753 */
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700754 if (srp_target_is_topspin(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500755 shost_printk(KERN_DEBUG, target->scsi_host,
756 PFX "Topspin/Cisco initiator port ID workaround "
757 "activated for target GUID %016llx\n",
Bart Van Assche45c37ca2015-05-18 13:25:10 +0200758 be64_to_cpu(target->ioc_guid));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800759 memset(req->priv.initiator_port_id, 0, 8);
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200760 memcpy(req->priv.initiator_port_id + 8,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100761 &target->srp_host->srp_dev->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800762 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800763
Bart Van Assche509c07b2014-10-30 14:48:30 +0100764 status = ib_send_cm_req(ch->cm_id, &req->param);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800765
766 kfree(req);
767
768 return status;
769}
770
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000771static bool srp_queue_remove_work(struct srp_target_port *target)
772{
773 bool changed = false;
774
775 spin_lock_irq(&target->lock);
776 if (target->state != SRP_TARGET_REMOVED) {
777 target->state = SRP_TARGET_REMOVED;
778 changed = true;
779 }
780 spin_unlock_irq(&target->lock);
781
782 if (changed)
Bart Van Asschebcc05912014-07-09 15:57:26 +0200783 queue_work(srp_remove_wq, &target->remove_work);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000784
785 return changed;
786}
787
Roland Dreieraef9ec32005-11-02 14:07:13 -0800788static void srp_disconnect_target(struct srp_target_port *target)
789{
Bart Van Assched92c0da2014-10-06 17:14:36 +0200790 struct srp_rdma_ch *ch;
791 int i;
Bart Van Assche509c07b2014-10-30 14:48:30 +0100792
Bart Van Asschec014c8c2015-05-18 13:23:57 +0200793 /* XXX should send SRP_I_LOGOUT request */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800794
Bart Van Asschec014c8c2015-05-18 13:23:57 +0200795 for (i = 0; i < target->ch_count; i++) {
796 ch = &target->ch[i];
797 ch->connected = false;
798 if (ch->cm_id && ib_send_cm_dreq(ch->cm_id, NULL, 0)) {
799 shost_printk(KERN_DEBUG, target->scsi_host,
800 PFX "Sending CM DREQ failed\n");
Bart Van Assche294c8752011-12-25 12:18:12 +0000801 }
Roland Dreiere6581052006-05-17 09:13:21 -0700802 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800803}
804
Bart Van Assche509c07b2014-10-30 14:48:30 +0100805static void srp_free_req_data(struct srp_target_port *target,
806 struct srp_rdma_ch *ch)
David Dillow8f26c9f2011-01-14 19:45:50 -0500807{
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200808 struct srp_device *dev = target->srp_host->srp_dev;
809 struct ib_device *ibdev = dev->dev;
David Dillow8f26c9f2011-01-14 19:45:50 -0500810 struct srp_request *req;
811 int i;
812
Bart Van Assche47513cf2015-05-18 13:25:54 +0200813 if (!ch->req_ring)
Bart Van Assche4d73f952013-10-26 14:40:37 +0200814 return;
815
816 for (i = 0; i < target->req_ring_size; ++i) {
Bart Van Assche509c07b2014-10-30 14:48:30 +0100817 req = &ch->req_ring[i];
Sagi Grimberg9a21be52015-10-13 19:11:41 +0300818 if (dev->use_fast_reg) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200819 kfree(req->fr_list);
Sagi Grimberg9a21be52015-10-13 19:11:41 +0300820 } else {
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200821 kfree(req->fmr_list);
Sagi Grimberg9a21be52015-10-13 19:11:41 +0300822 kfree(req->map_page);
823 }
David Dillowc07d4242011-01-16 13:57:10 -0500824 if (req->indirect_dma_addr) {
825 ib_dma_unmap_single(ibdev, req->indirect_dma_addr,
826 target->indirect_size,
827 DMA_TO_DEVICE);
828 }
829 kfree(req->indirect_desc);
David Dillow8f26c9f2011-01-14 19:45:50 -0500830 }
Bart Van Assche4d73f952013-10-26 14:40:37 +0200831
Bart Van Assche509c07b2014-10-30 14:48:30 +0100832 kfree(ch->req_ring);
833 ch->req_ring = NULL;
David Dillow8f26c9f2011-01-14 19:45:50 -0500834}
835
Bart Van Assche509c07b2014-10-30 14:48:30 +0100836static int srp_alloc_req_data(struct srp_rdma_ch *ch)
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200837{
Bart Van Assche509c07b2014-10-30 14:48:30 +0100838 struct srp_target_port *target = ch->target;
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200839 struct srp_device *srp_dev = target->srp_host->srp_dev;
840 struct ib_device *ibdev = srp_dev->dev;
841 struct srp_request *req;
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200842 void *mr_list;
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200843 dma_addr_t dma_addr;
844 int i, ret = -ENOMEM;
845
Bart Van Assche509c07b2014-10-30 14:48:30 +0100846 ch->req_ring = kcalloc(target->req_ring_size, sizeof(*ch->req_ring),
847 GFP_KERNEL);
848 if (!ch->req_ring)
Bart Van Assche4d73f952013-10-26 14:40:37 +0200849 goto out;
850
851 for (i = 0; i < target->req_ring_size; ++i) {
Bart Van Assche509c07b2014-10-30 14:48:30 +0100852 req = &ch->req_ring[i];
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200853 mr_list = kmalloc(target->cmd_sg_cnt * sizeof(void *),
854 GFP_KERNEL);
855 if (!mr_list)
856 goto out;
Sagi Grimberg9a21be52015-10-13 19:11:41 +0300857 if (srp_dev->use_fast_reg) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200858 req->fr_list = mr_list;
Sagi Grimberg9a21be52015-10-13 19:11:41 +0300859 } else {
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200860 req->fmr_list = mr_list;
Sagi Grimberg9a21be52015-10-13 19:11:41 +0300861 req->map_page = kmalloc(srp_dev->max_pages_per_mr *
862 sizeof(void *), GFP_KERNEL);
863 if (!req->map_page)
864 goto out;
865 }
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200866 req->indirect_desc = kmalloc(target->indirect_size, GFP_KERNEL);
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200867 if (!req->indirect_desc)
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200868 goto out;
869
870 dma_addr = ib_dma_map_single(ibdev, req->indirect_desc,
871 target->indirect_size,
872 DMA_TO_DEVICE);
873 if (ib_dma_mapping_error(ibdev, dma_addr))
874 goto out;
875
876 req->indirect_dma_addr = dma_addr;
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200877 }
878 ret = 0;
879
880out:
881 return ret;
882}
883
Bart Van Assche683b1592012-01-14 12:40:44 +0000884/**
885 * srp_del_scsi_host_attr() - Remove attributes defined in the host template.
886 * @shost: SCSI host whose attributes to remove from sysfs.
887 *
888 * Note: Any attributes defined in the host template and that did not exist
889 * before invocation of this function will be ignored.
890 */
891static void srp_del_scsi_host_attr(struct Scsi_Host *shost)
892{
893 struct device_attribute **attr;
894
895 for (attr = shost->hostt->shost_attrs; attr && *attr; ++attr)
896 device_remove_file(&shost->shost_dev, *attr);
897}
898
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000899static void srp_remove_target(struct srp_target_port *target)
900{
Bart Van Assched92c0da2014-10-06 17:14:36 +0200901 struct srp_rdma_ch *ch;
902 int i;
Bart Van Assche509c07b2014-10-30 14:48:30 +0100903
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000904 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
905
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000906 srp_del_scsi_host_attr(target->scsi_host);
Bart Van Assche9dd69a62013-10-26 14:32:30 +0200907 srp_rport_get(target->rport);
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000908 srp_remove_host(target->scsi_host);
909 scsi_remove_host(target->scsi_host);
Bart Van Assche93079162013-12-11 17:06:14 +0100910 srp_stop_rport_timers(target->rport);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000911 srp_disconnect_target(target);
Bart Van Assched92c0da2014-10-06 17:14:36 +0200912 for (i = 0; i < target->ch_count; i++) {
913 ch = &target->ch[i];
914 srp_free_ch_ib(target, ch);
915 }
Bart Van Asschec1120f82013-10-26 14:35:08 +0200916 cancel_work_sync(&target->tl_err_work);
Bart Van Assche9dd69a62013-10-26 14:32:30 +0200917 srp_rport_put(target->rport);
Bart Van Assched92c0da2014-10-06 17:14:36 +0200918 for (i = 0; i < target->ch_count; i++) {
919 ch = &target->ch[i];
920 srp_free_req_data(target, ch);
921 }
922 kfree(target->ch);
923 target->ch = NULL;
Vu Pham65d7dd22013-10-10 13:50:29 +0200924
925 spin_lock(&target->srp_host->target_lock);
926 list_del(&target->list);
927 spin_unlock(&target->srp_host->target_lock);
928
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000929 scsi_host_put(target->scsi_host);
930}
931
David Howellsc4028952006-11-22 14:57:56 +0000932static void srp_remove_work(struct work_struct *work)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800933{
David Howellsc4028952006-11-22 14:57:56 +0000934 struct srp_target_port *target =
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000935 container_of(work, struct srp_target_port, remove_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800936
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000937 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800938
Bart Van Assche96fc2482013-06-28 14:51:26 +0200939 srp_remove_target(target);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800940}
941
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +0200942static void srp_rport_delete(struct srp_rport *rport)
943{
944 struct srp_target_port *target = rport->lld_data;
945
946 srp_queue_remove_work(target);
947}
948
Bart Van Asschec014c8c2015-05-18 13:23:57 +0200949/**
950 * srp_connected_ch() - number of connected channels
951 * @target: SRP target port.
952 */
953static int srp_connected_ch(struct srp_target_port *target)
954{
955 int i, c = 0;
956
957 for (i = 0; i < target->ch_count; i++)
958 c += target->ch[i].connected;
959
960 return c;
961}
962
Bart Van Assched92c0da2014-10-06 17:14:36 +0200963static int srp_connect_ch(struct srp_rdma_ch *ch, bool multich)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800964{
Bart Van Assche509c07b2014-10-30 14:48:30 +0100965 struct srp_target_port *target = ch->target;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800966 int ret;
967
Bart Van Asschec014c8c2015-05-18 13:23:57 +0200968 WARN_ON_ONCE(!multich && srp_connected_ch(target) > 0);
Bart Van Assche294c8752011-12-25 12:18:12 +0000969
Bart Van Assche509c07b2014-10-30 14:48:30 +0100970 ret = srp_lookup_path(ch);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800971 if (ret)
Bart Van Assche4d59ad22015-12-01 10:17:32 -0800972 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800973
974 while (1) {
Bart Van Assche509c07b2014-10-30 14:48:30 +0100975 init_completion(&ch->done);
Bart Van Assched92c0da2014-10-06 17:14:36 +0200976 ret = srp_send_req(ch, multich);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800977 if (ret)
Bart Van Assche4d59ad22015-12-01 10:17:32 -0800978 goto out;
Bart Van Assche509c07b2014-10-30 14:48:30 +0100979 ret = wait_for_completion_interruptible(&ch->done);
Bart Van Asschea702adc2014-03-14 13:53:10 +0100980 if (ret < 0)
Bart Van Assche4d59ad22015-12-01 10:17:32 -0800981 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800982
983 /*
984 * The CM event handling code will set status to
985 * SRP_PORT_REDIRECT if we get a port redirect REJ
986 * back, or SRP_DLID_REDIRECT if we get a lid/qp
987 * redirect REJ back.
988 */
Bart Van Assche4d59ad22015-12-01 10:17:32 -0800989 ret = ch->status;
990 switch (ret) {
Roland Dreieraef9ec32005-11-02 14:07:13 -0800991 case 0:
Bart Van Asschec014c8c2015-05-18 13:23:57 +0200992 ch->connected = true;
Bart Van Assche4d59ad22015-12-01 10:17:32 -0800993 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800994
995 case SRP_PORT_REDIRECT:
Bart Van Assche509c07b2014-10-30 14:48:30 +0100996 ret = srp_lookup_path(ch);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800997 if (ret)
Bart Van Assche4d59ad22015-12-01 10:17:32 -0800998 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800999 break;
1000
1001 case SRP_DLID_REDIRECT:
1002 break;
1003
David Dillow9fe4bcf2008-01-08 17:08:52 -05001004 case SRP_STALE_CONN:
David Dillow9fe4bcf2008-01-08 17:08:52 -05001005 shost_printk(KERN_ERR, target->scsi_host, PFX
Bart Van Assche205619f2014-10-30 14:46:55 +01001006 "giving up on stale connection\n");
Bart Van Assche4d59ad22015-12-01 10:17:32 -08001007 ret = -ECONNRESET;
1008 goto out;
David Dillow9fe4bcf2008-01-08 17:08:52 -05001009
Roland Dreieraef9ec32005-11-02 14:07:13 -08001010 default:
Bart Van Assche4d59ad22015-12-01 10:17:32 -08001011 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001012 }
1013 }
Bart Van Assche4d59ad22015-12-01 10:17:32 -08001014
1015out:
1016 return ret <= 0 ? ret : -ENODEV;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001017}
1018
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001019static void srp_inv_rkey_err_done(struct ib_cq *cq, struct ib_wc *wc)
1020{
1021 srp_handle_qp_err(cq, wc, "INV RKEY");
1022}
1023
1024static int srp_inv_rkey(struct srp_request *req, struct srp_rdma_ch *ch,
1025 u32 rkey)
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001026{
1027 struct ib_send_wr *bad_wr;
1028 struct ib_send_wr wr = {
1029 .opcode = IB_WR_LOCAL_INV,
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001030 .next = NULL,
1031 .num_sge = 0,
1032 .send_flags = 0,
1033 .ex.invalidate_rkey = rkey,
1034 };
1035
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001036 wr.wr_cqe = &req->reg_cqe;
1037 req->reg_cqe.done = srp_inv_rkey_err_done;
Bart Van Assche509c07b2014-10-30 14:48:30 +01001038 return ib_post_send(ch->qp, &wr, &bad_wr);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001039}
1040
Roland Dreierd945e1d2006-05-09 10:50:28 -07001041static void srp_unmap_data(struct scsi_cmnd *scmnd,
Bart Van Assche509c07b2014-10-30 14:48:30 +01001042 struct srp_rdma_ch *ch,
Roland Dreierd945e1d2006-05-09 10:50:28 -07001043 struct srp_request *req)
1044{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001045 struct srp_target_port *target = ch->target;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001046 struct srp_device *dev = target->srp_host->srp_dev;
1047 struct ib_device *ibdev = dev->dev;
1048 int i, res;
David Dillow8f26c9f2011-01-14 19:45:50 -05001049
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001050 if (!scsi_sglist(scmnd) ||
Roland Dreierd945e1d2006-05-09 10:50:28 -07001051 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
1052 scmnd->sc_data_direction != DMA_FROM_DEVICE))
1053 return;
1054
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001055 if (dev->use_fast_reg) {
1056 struct srp_fr_desc **pfr;
1057
1058 for (i = req->nmdesc, pfr = req->fr_list; i > 0; i--, pfr++) {
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001059 res = srp_inv_rkey(req, ch, (*pfr)->mr->rkey);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001060 if (res < 0) {
1061 shost_printk(KERN_ERR, target->scsi_host, PFX
1062 "Queueing INV WR for rkey %#x failed (%d)\n",
1063 (*pfr)->mr->rkey, res);
1064 queue_work(system_long_wq,
1065 &target->tl_err_work);
1066 }
1067 }
1068 if (req->nmdesc)
Bart Van Assche509c07b2014-10-30 14:48:30 +01001069 srp_fr_pool_put(ch->fr_pool, req->fr_list,
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001070 req->nmdesc);
Bart Van Assche002f1562015-08-10 17:08:44 -07001071 } else if (dev->use_fmr) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001072 struct ib_pool_fmr **pfmr;
1073
1074 for (i = req->nmdesc, pfmr = req->fmr_list; i > 0; i--, pfmr++)
1075 ib_fmr_pool_unmap(*pfmr);
1076 }
Roland Dreierf5358a12006-06-17 20:37:29 -07001077
David Dillow8f26c9f2011-01-14 19:45:50 -05001078 ib_dma_unmap_sg(ibdev, scsi_sglist(scmnd), scsi_sg_count(scmnd),
1079 scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001080}
1081
Bart Van Assche22032992012-08-14 13:18:53 +00001082/**
1083 * srp_claim_req - Take ownership of the scmnd associated with a request.
Bart Van Assche509c07b2014-10-30 14:48:30 +01001084 * @ch: SRP RDMA channel.
Bart Van Assche22032992012-08-14 13:18:53 +00001085 * @req: SRP request.
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001086 * @sdev: If not NULL, only take ownership for this SCSI device.
Bart Van Assche22032992012-08-14 13:18:53 +00001087 * @scmnd: If NULL, take ownership of @req->scmnd. If not NULL, only take
1088 * ownership of @req->scmnd if it equals @scmnd.
1089 *
1090 * Return value:
1091 * Either NULL or a pointer to the SCSI command the caller became owner of.
1092 */
Bart Van Assche509c07b2014-10-30 14:48:30 +01001093static struct scsi_cmnd *srp_claim_req(struct srp_rdma_ch *ch,
Bart Van Assche22032992012-08-14 13:18:53 +00001094 struct srp_request *req,
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001095 struct scsi_device *sdev,
Bart Van Assche22032992012-08-14 13:18:53 +00001096 struct scsi_cmnd *scmnd)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001097{
Bart Van Assche94a91742010-11-26 14:50:09 -05001098 unsigned long flags;
1099
Bart Van Assche509c07b2014-10-30 14:48:30 +01001100 spin_lock_irqsave(&ch->lock, flags);
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001101 if (req->scmnd &&
1102 (!sdev || req->scmnd->device == sdev) &&
1103 (!scmnd || req->scmnd == scmnd)) {
Bart Van Assche22032992012-08-14 13:18:53 +00001104 scmnd = req->scmnd;
1105 req->scmnd = NULL;
Bart Van Assche22032992012-08-14 13:18:53 +00001106 } else {
1107 scmnd = NULL;
1108 }
Bart Van Assche509c07b2014-10-30 14:48:30 +01001109 spin_unlock_irqrestore(&ch->lock, flags);
Bart Van Assche22032992012-08-14 13:18:53 +00001110
1111 return scmnd;
1112}
1113
1114/**
Bart Van Assche6ec2ba02016-04-22 14:12:47 -07001115 * srp_free_req() - Unmap data and adjust ch->req_lim.
Bart Van Assche509c07b2014-10-30 14:48:30 +01001116 * @ch: SRP RDMA channel.
Bart Van Asscheaf246632014-05-20 15:04:21 +02001117 * @req: Request to be freed.
1118 * @scmnd: SCSI command associated with @req.
1119 * @req_lim_delta: Amount to be added to @target->req_lim.
Bart Van Assche22032992012-08-14 13:18:53 +00001120 */
Bart Van Assche509c07b2014-10-30 14:48:30 +01001121static void srp_free_req(struct srp_rdma_ch *ch, struct srp_request *req,
1122 struct scsi_cmnd *scmnd, s32 req_lim_delta)
Bart Van Assche22032992012-08-14 13:18:53 +00001123{
1124 unsigned long flags;
1125
Bart Van Assche509c07b2014-10-30 14:48:30 +01001126 srp_unmap_data(scmnd, ch, req);
Bart Van Assche22032992012-08-14 13:18:53 +00001127
Bart Van Assche509c07b2014-10-30 14:48:30 +01001128 spin_lock_irqsave(&ch->lock, flags);
1129 ch->req_lim += req_lim_delta;
Bart Van Assche509c07b2014-10-30 14:48:30 +01001130 spin_unlock_irqrestore(&ch->lock, flags);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001131}
1132
Bart Van Assche509c07b2014-10-30 14:48:30 +01001133static void srp_finish_req(struct srp_rdma_ch *ch, struct srp_request *req,
1134 struct scsi_device *sdev, int result)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001135{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001136 struct scsi_cmnd *scmnd = srp_claim_req(ch, req, sdev, NULL);
Bart Van Assche22032992012-08-14 13:18:53 +00001137
1138 if (scmnd) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01001139 srp_free_req(ch, req, scmnd, 0);
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001140 scmnd->result = result;
Bart Van Assche22032992012-08-14 13:18:53 +00001141 scmnd->scsi_done(scmnd);
Bart Van Assche22032992012-08-14 13:18:53 +00001142 }
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001143}
1144
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001145static void srp_terminate_io(struct srp_rport *rport)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001146{
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001147 struct srp_target_port *target = rport->lld_data;
Bart Van Assched92c0da2014-10-06 17:14:36 +02001148 struct srp_rdma_ch *ch;
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001149 struct Scsi_Host *shost = target->scsi_host;
1150 struct scsi_device *sdev;
Bart Van Assched92c0da2014-10-06 17:14:36 +02001151 int i, j;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001152
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001153 /*
1154 * Invoking srp_terminate_io() while srp_queuecommand() is running
1155 * is not safe. Hence the warning statement below.
1156 */
1157 shost_for_each_device(sdev, shost)
1158 WARN_ON_ONCE(sdev->request_queue->request_fn_active);
1159
Bart Van Assched92c0da2014-10-06 17:14:36 +02001160 for (i = 0; i < target->ch_count; i++) {
1161 ch = &target->ch[i];
Bart Van Assche509c07b2014-10-30 14:48:30 +01001162
Bart Van Assched92c0da2014-10-06 17:14:36 +02001163 for (j = 0; j < target->req_ring_size; ++j) {
1164 struct srp_request *req = &ch->req_ring[j];
1165
1166 srp_finish_req(ch, req, NULL,
1167 DID_TRANSPORT_FAILFAST << 16);
1168 }
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001169 }
1170}
1171
1172/*
1173 * It is up to the caller to ensure that srp_rport_reconnect() calls are
1174 * serialized and that no concurrent srp_queuecommand(), srp_abort(),
1175 * srp_reset_device() or srp_reset_host() calls will occur while this function
1176 * is in progress. One way to realize that is not to call this function
1177 * directly but to call srp_reconnect_rport() instead since that last function
1178 * serializes calls of this function via rport->mutex and also blocks
1179 * srp_queuecommand() calls before invoking this function.
1180 */
1181static int srp_rport_reconnect(struct srp_rport *rport)
1182{
1183 struct srp_target_port *target = rport->lld_data;
Bart Van Assched92c0da2014-10-06 17:14:36 +02001184 struct srp_rdma_ch *ch;
1185 int i, j, ret = 0;
1186 bool multich = false;
Bart Van Assche09be70a2012-03-17 17:18:54 +00001187
Roland Dreieraef9ec32005-11-02 14:07:13 -08001188 srp_disconnect_target(target);
Bart Van Assche34aa6542014-10-30 14:47:22 +01001189
1190 if (target->state == SRP_TARGET_SCANNING)
1191 return -ENODEV;
1192
Roland Dreieraef9ec32005-11-02 14:07:13 -08001193 /*
Bart Van Asschec7c4e7f2013-02-21 17:19:04 +00001194 * Now get a new local CM ID so that we avoid confusing the target in
1195 * case things are really fouled up. Doing so also ensures that all CM
1196 * callbacks will have finished before a new QP is allocated.
Roland Dreieraef9ec32005-11-02 14:07:13 -08001197 */
Bart Van Assched92c0da2014-10-06 17:14:36 +02001198 for (i = 0; i < target->ch_count; i++) {
1199 ch = &target->ch[i];
Bart Van Assched92c0da2014-10-06 17:14:36 +02001200 ret += srp_new_cm_id(ch);
Bart Van Assche536ae142010-11-26 13:58:27 -05001201 }
Bart Van Assched92c0da2014-10-06 17:14:36 +02001202 for (i = 0; i < target->ch_count; i++) {
1203 ch = &target->ch[i];
Bart Van Assched92c0da2014-10-06 17:14:36 +02001204 for (j = 0; j < target->req_ring_size; ++j) {
1205 struct srp_request *req = &ch->req_ring[j];
Roland Dreieraef9ec32005-11-02 14:07:13 -08001206
Bart Van Assched92c0da2014-10-06 17:14:36 +02001207 srp_finish_req(ch, req, NULL, DID_RESET << 16);
1208 }
1209 }
1210 for (i = 0; i < target->ch_count; i++) {
1211 ch = &target->ch[i];
Bart Van Assched92c0da2014-10-06 17:14:36 +02001212 /*
1213 * Whether or not creating a new CM ID succeeded, create a new
1214 * QP. This guarantees that all completion callback function
1215 * invocations have finished before request resetting starts.
1216 */
1217 ret += srp_create_ch_ib(ch);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001218
Bart Van Assched92c0da2014-10-06 17:14:36 +02001219 INIT_LIST_HEAD(&ch->free_tx);
1220 for (j = 0; j < target->queue_size; ++j)
1221 list_add(&ch->tx_ring[j]->list, &ch->free_tx);
1222 }
Bart Van Assche8de9fe32015-05-18 13:23:36 +02001223
1224 target->qp_in_error = false;
1225
Bart Van Assched92c0da2014-10-06 17:14:36 +02001226 for (i = 0; i < target->ch_count; i++) {
1227 ch = &target->ch[i];
Bart Van Asschebbac5cc2015-05-18 13:26:17 +02001228 if (ret)
Bart Van Assched92c0da2014-10-06 17:14:36 +02001229 break;
Bart Van Assched92c0da2014-10-06 17:14:36 +02001230 ret = srp_connect_ch(ch, multich);
1231 multich = true;
1232 }
Bart Van Assche09be70a2012-03-17 17:18:54 +00001233
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001234 if (ret == 0)
1235 shost_printk(KERN_INFO, target->scsi_host,
1236 PFX "reconnect succeeded\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001237
1238 return ret;
1239}
1240
David Dillow8f26c9f2011-01-14 19:45:50 -05001241static void srp_map_desc(struct srp_map_state *state, dma_addr_t dma_addr,
1242 unsigned int dma_len, u32 rkey)
Roland Dreierf5358a12006-06-17 20:37:29 -07001243{
David Dillow8f26c9f2011-01-14 19:45:50 -05001244 struct srp_direct_buf *desc = state->desc;
1245
Bart Van Assche3ae95da2015-08-10 17:08:18 -07001246 WARN_ON_ONCE(!dma_len);
1247
David Dillow8f26c9f2011-01-14 19:45:50 -05001248 desc->va = cpu_to_be64(dma_addr);
1249 desc->key = cpu_to_be32(rkey);
1250 desc->len = cpu_to_be32(dma_len);
1251
1252 state->total_len += dma_len;
1253 state->desc++;
1254 state->ndesc++;
1255}
1256
1257static int srp_map_finish_fmr(struct srp_map_state *state,
Bart Van Assche509c07b2014-10-30 14:48:30 +01001258 struct srp_rdma_ch *ch)
David Dillow8f26c9f2011-01-14 19:45:50 -05001259{
Bart Van Assche186fbc62015-08-10 17:06:29 -07001260 struct srp_target_port *target = ch->target;
1261 struct srp_device *dev = target->srp_host->srp_dev;
David Dillow8f26c9f2011-01-14 19:45:50 -05001262 struct ib_pool_fmr *fmr;
Roland Dreierf5358a12006-06-17 20:37:29 -07001263 u64 io_addr = 0;
David Dillow8f26c9f2011-01-14 19:45:50 -05001264
Bart Van Asschef731ed62015-08-10 17:07:27 -07001265 if (state->fmr.next >= state->fmr.end)
1266 return -ENOMEM;
1267
Sagi Grimberg26630e82015-10-13 19:11:38 +03001268 WARN_ON_ONCE(!dev->use_fmr);
1269
1270 if (state->npages == 0)
1271 return 0;
1272
1273 if (state->npages == 1 && target->global_mr) {
1274 srp_map_desc(state, state->base_dma_addr, state->dma_len,
1275 target->global_mr->rkey);
1276 goto reset_state;
1277 }
1278
Bart Van Assche509c07b2014-10-30 14:48:30 +01001279 fmr = ib_fmr_pool_map_phys(ch->fmr_pool, state->pages,
David Dillow8f26c9f2011-01-14 19:45:50 -05001280 state->npages, io_addr);
1281 if (IS_ERR(fmr))
1282 return PTR_ERR(fmr);
1283
Bart Van Asschef731ed62015-08-10 17:07:27 -07001284 *state->fmr.next++ = fmr;
Bart Van Assche52ede082014-05-20 15:07:45 +02001285 state->nmdesc++;
David Dillow8f26c9f2011-01-14 19:45:50 -05001286
Bart Van Assche186fbc62015-08-10 17:06:29 -07001287 srp_map_desc(state, state->base_dma_addr & ~dev->mr_page_mask,
1288 state->dma_len, fmr->fmr->rkey);
Bart Van Assche539dde62014-05-20 15:05:46 +02001289
Sagi Grimberg26630e82015-10-13 19:11:38 +03001290reset_state:
1291 state->npages = 0;
1292 state->dma_len = 0;
1293
David Dillow8f26c9f2011-01-14 19:45:50 -05001294 return 0;
1295}
1296
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001297static void srp_reg_mr_err_done(struct ib_cq *cq, struct ib_wc *wc)
1298{
1299 srp_handle_qp_err(cq, wc, "FAST REG");
1300}
1301
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001302static int srp_map_finish_fr(struct srp_map_state *state,
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001303 struct srp_request *req,
Bart Van Assche57b0be92015-12-01 10:19:38 -08001304 struct srp_rdma_ch *ch, int sg_nents)
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001305{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001306 struct srp_target_port *target = ch->target;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001307 struct srp_device *dev = target->srp_host->srp_dev;
1308 struct ib_send_wr *bad_wr;
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001309 struct ib_reg_wr wr;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001310 struct srp_fr_desc *desc;
1311 u32 rkey;
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001312 int n, err;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001313
Bart Van Asschef731ed62015-08-10 17:07:27 -07001314 if (state->fr.next >= state->fr.end)
1315 return -ENOMEM;
1316
Sagi Grimberg26630e82015-10-13 19:11:38 +03001317 WARN_ON_ONCE(!dev->use_fast_reg);
1318
Bart Van Assche57b0be92015-12-01 10:19:38 -08001319 if (sg_nents == 0)
Sagi Grimberg26630e82015-10-13 19:11:38 +03001320 return 0;
1321
Bart Van Assche57b0be92015-12-01 10:19:38 -08001322 if (sg_nents == 1 && target->global_mr) {
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001323 srp_map_desc(state, sg_dma_address(state->sg),
1324 sg_dma_len(state->sg),
Sagi Grimberg26630e82015-10-13 19:11:38 +03001325 target->global_mr->rkey);
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001326 return 1;
Sagi Grimberg26630e82015-10-13 19:11:38 +03001327 }
1328
Bart Van Assche509c07b2014-10-30 14:48:30 +01001329 desc = srp_fr_pool_get(ch->fr_pool);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001330 if (!desc)
1331 return -ENOMEM;
1332
1333 rkey = ib_inc_rkey(desc->mr->rkey);
1334 ib_update_fast_reg_key(desc->mr, rkey);
1335
Bart Van Assche57b0be92015-12-01 10:19:38 -08001336 n = ib_map_mr_sg(desc->mr, state->sg, sg_nents, dev->mr_page_size);
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001337 if (unlikely(n < 0))
1338 return n;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001339
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001340 req->reg_cqe.done = srp_reg_mr_err_done;
1341
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001342 wr.wr.next = NULL;
1343 wr.wr.opcode = IB_WR_REG_MR;
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001344 wr.wr.wr_cqe = &req->reg_cqe;
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001345 wr.wr.num_sge = 0;
1346 wr.wr.send_flags = 0;
1347 wr.mr = desc->mr;
1348 wr.key = desc->mr->rkey;
1349 wr.access = (IB_ACCESS_LOCAL_WRITE |
1350 IB_ACCESS_REMOTE_READ |
1351 IB_ACCESS_REMOTE_WRITE);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001352
Bart Van Asschef731ed62015-08-10 17:07:27 -07001353 *state->fr.next++ = desc;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001354 state->nmdesc++;
1355
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001356 srp_map_desc(state, desc->mr->iova,
1357 desc->mr->length, desc->mr->rkey);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001358
Sagi Grimberg26630e82015-10-13 19:11:38 +03001359 err = ib_post_send(ch->qp, &wr.wr, &bad_wr);
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001360 if (unlikely(err))
Sagi Grimberg26630e82015-10-13 19:11:38 +03001361 return err;
1362
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001363 return n;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001364}
1365
David Dillow8f26c9f2011-01-14 19:45:50 -05001366static int srp_map_sg_entry(struct srp_map_state *state,
Bart Van Assche509c07b2014-10-30 14:48:30 +01001367 struct srp_rdma_ch *ch,
Bart Van Assche3ae95da2015-08-10 17:08:18 -07001368 struct scatterlist *sg, int sg_index)
David Dillow8f26c9f2011-01-14 19:45:50 -05001369{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001370 struct srp_target_port *target = ch->target;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001371 struct srp_device *dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001372 struct ib_device *ibdev = dev->dev;
David Dillow8f26c9f2011-01-14 19:45:50 -05001373 dma_addr_t dma_addr = ib_sg_dma_address(ibdev, sg);
1374 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Bart Van Assche3ae95da2015-08-10 17:08:18 -07001375 unsigned int len = 0;
David Dillow8f26c9f2011-01-14 19:45:50 -05001376 int ret;
Roland Dreierf5358a12006-06-17 20:37:29 -07001377
Bart Van Assche3ae95da2015-08-10 17:08:18 -07001378 WARN_ON_ONCE(!dma_len);
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -07001379
David Dillow8f26c9f2011-01-14 19:45:50 -05001380 while (dma_len) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001381 unsigned offset = dma_addr & ~dev->mr_page_mask;
1382 if (state->npages == dev->max_pages_per_mr || offset != 0) {
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001383 ret = srp_map_finish_fmr(state, ch);
David Dillow8f26c9f2011-01-14 19:45:50 -05001384 if (ret)
1385 return ret;
Roland Dreierf5358a12006-06-17 20:37:29 -07001386 }
1387
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001388 len = min_t(unsigned int, dma_len, dev->mr_page_size - offset);
David Dillow8f26c9f2011-01-14 19:45:50 -05001389
1390 if (!state->npages)
1391 state->base_dma_addr = dma_addr;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001392 state->pages[state->npages++] = dma_addr & dev->mr_page_mask;
Bart Van Assche52ede082014-05-20 15:07:45 +02001393 state->dma_len += len;
David Dillow8f26c9f2011-01-14 19:45:50 -05001394 dma_addr += len;
1395 dma_len -= len;
Roland Dreierf5358a12006-06-17 20:37:29 -07001396 }
1397
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001398 /*
1399 * If the last entry of the MR wasn't a full page, then we need to
David Dillow8f26c9f2011-01-14 19:45:50 -05001400 * close it out and start a new one -- we can only merge at page
Bart Van Assche1d3d98c2016-04-22 14:12:10 -07001401 * boundaries.
David Dillow8f26c9f2011-01-14 19:45:50 -05001402 */
Roland Dreierf5358a12006-06-17 20:37:29 -07001403 ret = 0;
Bart Van Assche0e0d3a42015-08-10 17:07:46 -07001404 if (len != dev->mr_page_size)
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001405 ret = srp_map_finish_fmr(state, ch);
Roland Dreierf5358a12006-06-17 20:37:29 -07001406 return ret;
1407}
1408
Sagi Grimberg26630e82015-10-13 19:11:38 +03001409static int srp_map_sg_fmr(struct srp_map_state *state, struct srp_rdma_ch *ch,
1410 struct srp_request *req, struct scatterlist *scat,
1411 int count)
1412{
1413 struct scatterlist *sg;
1414 int i, ret;
1415
1416 state->desc = req->indirect_desc;
1417 state->pages = req->map_page;
1418 state->fmr.next = req->fmr_list;
1419 state->fmr.end = req->fmr_list + ch->target->cmd_sg_cnt;
1420
1421 for_each_sg(scat, sg, count, i) {
1422 ret = srp_map_sg_entry(state, ch, sg, i);
1423 if (ret)
1424 return ret;
1425 }
1426
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001427 ret = srp_map_finish_fmr(state, ch);
Sagi Grimberg26630e82015-10-13 19:11:38 +03001428 if (ret)
1429 return ret;
1430
1431 req->nmdesc = state->nmdesc;
1432
1433 return 0;
1434}
1435
1436static int srp_map_sg_fr(struct srp_map_state *state, struct srp_rdma_ch *ch,
1437 struct srp_request *req, struct scatterlist *scat,
1438 int count)
1439{
Sagi Grimberg26630e82015-10-13 19:11:38 +03001440 state->desc = req->indirect_desc;
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001441 state->fr.next = req->fr_list;
1442 state->fr.end = req->fr_list + ch->target->cmd_sg_cnt;
1443 state->sg = scat;
Sagi Grimberg26630e82015-10-13 19:11:38 +03001444
Bart Van Assche57b0be92015-12-01 10:19:38 -08001445 while (count) {
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001446 int i, n;
1447
Doug Ledfordc6333f92015-12-15 14:10:44 -05001448 n = srp_map_finish_fr(state, req, ch, count);
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001449 if (unlikely(n < 0))
1450 return n;
1451
Bart Van Assche57b0be92015-12-01 10:19:38 -08001452 count -= n;
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001453 for (i = 0; i < n; i++)
1454 state->sg = sg_next(state->sg);
Sagi Grimberg26630e82015-10-13 19:11:38 +03001455 }
1456
Sagi Grimberg26630e82015-10-13 19:11:38 +03001457 req->nmdesc = state->nmdesc;
1458
1459 return 0;
1460}
1461
1462static int srp_map_sg_dma(struct srp_map_state *state, struct srp_rdma_ch *ch,
1463 struct srp_request *req, struct scatterlist *scat,
1464 int count)
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001465{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001466 struct srp_target_port *target = ch->target;
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001467 struct srp_device *dev = target->srp_host->srp_dev;
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001468 struct scatterlist *sg;
Sagi Grimberg26630e82015-10-13 19:11:38 +03001469 int i;
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001470
Sagi Grimberg26630e82015-10-13 19:11:38 +03001471 state->desc = req->indirect_desc;
1472 for_each_sg(scat, sg, count, i) {
1473 srp_map_desc(state, ib_sg_dma_address(dev->dev, sg),
1474 ib_sg_dma_len(dev->dev, sg),
1475 target->global_mr->rkey);
Bart Van Assche0e0d3a42015-08-10 17:07:46 -07001476 }
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001477
Bart Van Assche52ede082014-05-20 15:07:45 +02001478 req->nmdesc = state->nmdesc;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001479
Sagi Grimberg26630e82015-10-13 19:11:38 +03001480 return 0;
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001481}
1482
Bart Van Assche330179f2015-08-10 17:09:05 -07001483/*
1484 * Register the indirect data buffer descriptor with the HCA.
1485 *
1486 * Note: since the indirect data buffer descriptor has been allocated with
1487 * kmalloc() it is guaranteed that this buffer is a physically contiguous
1488 * memory buffer.
1489 */
1490static int srp_map_idb(struct srp_rdma_ch *ch, struct srp_request *req,
1491 void **next_mr, void **end_mr, u32 idb_len,
1492 __be32 *idb_rkey)
1493{
1494 struct srp_target_port *target = ch->target;
1495 struct srp_device *dev = target->srp_host->srp_dev;
1496 struct srp_map_state state;
1497 struct srp_direct_buf idb_desc;
1498 u64 idb_pages[1];
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001499 struct scatterlist idb_sg[1];
Bart Van Assche330179f2015-08-10 17:09:05 -07001500 int ret;
1501
1502 memset(&state, 0, sizeof(state));
1503 memset(&idb_desc, 0, sizeof(idb_desc));
1504 state.gen.next = next_mr;
1505 state.gen.end = end_mr;
1506 state.desc = &idb_desc;
Bart Van Assche330179f2015-08-10 17:09:05 -07001507 state.base_dma_addr = req->indirect_dma_addr;
1508 state.dma_len = idb_len;
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001509
1510 if (dev->use_fast_reg) {
1511 state.sg = idb_sg;
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001512 sg_set_buf(idb_sg, req->indirect_desc, idb_len);
1513 idb_sg->dma_address = req->indirect_dma_addr; /* hack! */
Christoph Hellwigfc925512015-12-01 10:18:30 -08001514#ifdef CONFIG_NEED_SG_DMA_LENGTH
1515 idb_sg->dma_length = idb_sg->length; /* hack^2 */
1516#endif
Doug Ledfordc6333f92015-12-15 14:10:44 -05001517 ret = srp_map_finish_fr(&state, req, ch, 1);
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001518 if (ret < 0)
1519 return ret;
1520 } else if (dev->use_fmr) {
1521 state.pages = idb_pages;
1522 state.pages[0] = (req->indirect_dma_addr &
1523 dev->mr_page_mask);
1524 state.npages = 1;
1525 ret = srp_map_finish_fmr(&state, ch);
1526 if (ret < 0)
1527 return ret;
1528 } else {
1529 return -EINVAL;
1530 }
Bart Van Assche330179f2015-08-10 17:09:05 -07001531
1532 *idb_rkey = idb_desc.key;
1533
Sagi Grimbergf7f7aab2015-10-13 19:11:39 +03001534 return 0;
Bart Van Assche330179f2015-08-10 17:09:05 -07001535}
1536
Bart Van Assche77269cd2016-04-22 14:13:09 -07001537/**
1538 * srp_map_data() - map SCSI data buffer onto an SRP request
1539 * @scmnd: SCSI command to map
1540 * @ch: SRP RDMA channel
1541 * @req: SRP request
1542 *
1543 * Returns the length in bytes of the SRP_CMD IU or a negative value if
1544 * mapping failed.
1545 */
Bart Van Assche509c07b2014-10-30 14:48:30 +01001546static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_rdma_ch *ch,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001547 struct srp_request *req)
1548{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001549 struct srp_target_port *target = ch->target;
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001550 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001551 struct srp_cmd *cmd = req->cmd->buf;
Bart Van Assche330179f2015-08-10 17:09:05 -07001552 int len, nents, count, ret;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001553 struct srp_device *dev;
1554 struct ib_device *ibdev;
David Dillow8f26c9f2011-01-14 19:45:50 -05001555 struct srp_map_state state;
1556 struct srp_indirect_buf *indirect_hdr;
Bart Van Assche330179f2015-08-10 17:09:05 -07001557 u32 idb_len, table_len;
1558 __be32 idb_rkey;
David Dillow8f26c9f2011-01-14 19:45:50 -05001559 u8 fmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001560
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001561 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001562 return sizeof (struct srp_cmd);
1563
1564 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
1565 scmnd->sc_data_direction != DMA_TO_DEVICE) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001566 shost_printk(KERN_WARNING, target->scsi_host,
1567 PFX "Unhandled data direction %d\n",
1568 scmnd->sc_data_direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001569 return -EINVAL;
1570 }
1571
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001572 nents = scsi_sg_count(scmnd);
1573 scat = scsi_sglist(scmnd);
Roland Dreiercf368712006-03-24 15:47:26 -08001574
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001575 dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001576 ibdev = dev->dev;
1577
1578 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
David Dillow8f26c9f2011-01-14 19:45:50 -05001579 if (unlikely(count == 0))
1580 return -EIO;
Roland Dreierf5358a12006-06-17 20:37:29 -07001581
1582 fmt = SRP_DATA_DESC_DIRECT;
1583 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -08001584
Bart Van Assche03f6fb92015-08-10 17:09:36 -07001585 if (count == 1 && target->global_mr) {
Roland Dreierf5358a12006-06-17 20:37:29 -07001586 /*
1587 * The midlayer only generated a single gather/scatter
1588 * entry, or DMA mapping coalesced everything to a
1589 * single entry. So a direct descriptor along with
1590 * the DMA MR suffices.
1591 */
Roland Dreieraef9ec32005-11-02 14:07:13 -08001592 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001593
Ralph Campbell85507bc2006-12-12 14:30:55 -08001594 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
Bart Van Assche03f6fb92015-08-10 17:09:36 -07001595 buf->key = cpu_to_be32(target->global_mr->rkey);
Ralph Campbell85507bc2006-12-12 14:30:55 -08001596 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Roland Dreiercf368712006-03-24 15:47:26 -08001597
Bart Van Assche52ede082014-05-20 15:07:45 +02001598 req->nmdesc = 0;
David Dillow8f26c9f2011-01-14 19:45:50 -05001599 goto map_complete;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001600 }
1601
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001602 /*
1603 * We have more than one scatter/gather entry, so build our indirect
1604 * descriptor table, trying to merge as many entries as we can.
David Dillow8f26c9f2011-01-14 19:45:50 -05001605 */
1606 indirect_hdr = (void *) cmd->add_data;
1607
David Dillowc07d4242011-01-16 13:57:10 -05001608 ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr,
1609 target->indirect_size, DMA_TO_DEVICE);
1610
David Dillow8f26c9f2011-01-14 19:45:50 -05001611 memset(&state, 0, sizeof(state));
Sagi Grimberg26630e82015-10-13 19:11:38 +03001612 if (dev->use_fast_reg)
1613 srp_map_sg_fr(&state, ch, req, scat, count);
1614 else if (dev->use_fmr)
1615 srp_map_sg_fmr(&state, ch, req, scat, count);
1616 else
1617 srp_map_sg_dma(&state, ch, req, scat, count);
David Dillow8f26c9f2011-01-14 19:45:50 -05001618
David Dillowc07d4242011-01-16 13:57:10 -05001619 /* We've mapped the request, now pull as much of the indirect
1620 * descriptor table as we can into the command buffer. If this
1621 * target is not using an external indirect table, we are
1622 * guaranteed to fit into the command, as the SCSI layer won't
1623 * give us more S/G entries than we allow.
David Dillow8f26c9f2011-01-14 19:45:50 -05001624 */
David Dillow8f26c9f2011-01-14 19:45:50 -05001625 if (state.ndesc == 1) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001626 /*
1627 * Memory registration collapsed the sg-list into one entry,
David Dillow8f26c9f2011-01-14 19:45:50 -05001628 * so use a direct descriptor.
1629 */
1630 struct srp_direct_buf *buf = (void *) cmd->add_data;
1631
David Dillowc07d4242011-01-16 13:57:10 -05001632 *buf = req->indirect_desc[0];
David Dillow8f26c9f2011-01-14 19:45:50 -05001633 goto map_complete;
1634 }
1635
David Dillowc07d4242011-01-16 13:57:10 -05001636 if (unlikely(target->cmd_sg_cnt < state.ndesc &&
1637 !target->allow_ext_sg)) {
1638 shost_printk(KERN_ERR, target->scsi_host,
1639 "Could not fit S/G list into SRP_CMD\n");
1640 return -EIO;
1641 }
1642
1643 count = min(state.ndesc, target->cmd_sg_cnt);
David Dillow8f26c9f2011-01-14 19:45:50 -05001644 table_len = state.ndesc * sizeof (struct srp_direct_buf);
Bart Van Assche330179f2015-08-10 17:09:05 -07001645 idb_len = sizeof(struct srp_indirect_buf) + table_len;
David Dillow8f26c9f2011-01-14 19:45:50 -05001646
1647 fmt = SRP_DATA_DESC_INDIRECT;
1648 len = sizeof(struct srp_cmd) + sizeof (struct srp_indirect_buf);
David Dillowc07d4242011-01-16 13:57:10 -05001649 len += count * sizeof (struct srp_direct_buf);
David Dillow8f26c9f2011-01-14 19:45:50 -05001650
David Dillowc07d4242011-01-16 13:57:10 -05001651 memcpy(indirect_hdr->desc_list, req->indirect_desc,
1652 count * sizeof (struct srp_direct_buf));
David Dillow8f26c9f2011-01-14 19:45:50 -05001653
Bart Van Assche03f6fb92015-08-10 17:09:36 -07001654 if (!target->global_mr) {
Bart Van Assche330179f2015-08-10 17:09:05 -07001655 ret = srp_map_idb(ch, req, state.gen.next, state.gen.end,
1656 idb_len, &idb_rkey);
1657 if (ret < 0)
1658 return ret;
1659 req->nmdesc++;
1660 } else {
Bart Van Asschea745f4f42015-12-01 10:18:47 -08001661 idb_rkey = cpu_to_be32(target->global_mr->rkey);
Bart Van Assche330179f2015-08-10 17:09:05 -07001662 }
1663
David Dillowc07d4242011-01-16 13:57:10 -05001664 indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr);
Bart Van Assche330179f2015-08-10 17:09:05 -07001665 indirect_hdr->table_desc.key = idb_rkey;
David Dillow8f26c9f2011-01-14 19:45:50 -05001666 indirect_hdr->table_desc.len = cpu_to_be32(table_len);
1667 indirect_hdr->len = cpu_to_be32(state.total_len);
1668
1669 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
David Dillowc07d4242011-01-16 13:57:10 -05001670 cmd->data_out_desc_cnt = count;
David Dillow8f26c9f2011-01-14 19:45:50 -05001671 else
David Dillowc07d4242011-01-16 13:57:10 -05001672 cmd->data_in_desc_cnt = count;
1673
1674 ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len,
1675 DMA_TO_DEVICE);
David Dillow8f26c9f2011-01-14 19:45:50 -05001676
1677map_complete:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001678 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
1679 cmd->buf_fmt = fmt << 4;
1680 else
1681 cmd->buf_fmt = fmt;
1682
Roland Dreieraef9ec32005-11-02 14:07:13 -08001683 return len;
1684}
1685
David Dillow05a1d752010-10-08 14:48:14 -04001686/*
Bart Van Assche76c75b22010-11-26 14:37:47 -05001687 * Return an IU and possible credit to the free pool
1688 */
Bart Van Assche509c07b2014-10-30 14:48:30 +01001689static void srp_put_tx_iu(struct srp_rdma_ch *ch, struct srp_iu *iu,
Bart Van Assche76c75b22010-11-26 14:37:47 -05001690 enum srp_iu_type iu_type)
1691{
1692 unsigned long flags;
1693
Bart Van Assche509c07b2014-10-30 14:48:30 +01001694 spin_lock_irqsave(&ch->lock, flags);
1695 list_add(&iu->list, &ch->free_tx);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001696 if (iu_type != SRP_IU_RSP)
Bart Van Assche509c07b2014-10-30 14:48:30 +01001697 ++ch->req_lim;
1698 spin_unlock_irqrestore(&ch->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001699}
1700
1701/*
Bart Van Assche509c07b2014-10-30 14:48:30 +01001702 * Must be called with ch->lock held to protect req_lim and free_tx.
Bart Van Asschee9684672010-11-26 15:08:38 -05001703 * If IU is not sent, it must be returned using srp_put_tx_iu().
David Dillow05a1d752010-10-08 14:48:14 -04001704 *
1705 * Note:
1706 * An upper limit for the number of allocated information units for each
1707 * request type is:
1708 * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues
1709 * more than Scsi_Host.can_queue requests.
1710 * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE.
1711 * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than
1712 * one unanswered SRP request to an initiator.
1713 */
Bart Van Assche509c07b2014-10-30 14:48:30 +01001714static struct srp_iu *__srp_get_tx_iu(struct srp_rdma_ch *ch,
David Dillow05a1d752010-10-08 14:48:14 -04001715 enum srp_iu_type iu_type)
1716{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001717 struct srp_target_port *target = ch->target;
David Dillow05a1d752010-10-08 14:48:14 -04001718 s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE;
1719 struct srp_iu *iu;
1720
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001721 ib_process_cq_direct(ch->send_cq, -1);
David Dillow05a1d752010-10-08 14:48:14 -04001722
Bart Van Assche509c07b2014-10-30 14:48:30 +01001723 if (list_empty(&ch->free_tx))
David Dillow05a1d752010-10-08 14:48:14 -04001724 return NULL;
1725
1726 /* Initiator responses to target requests do not consume credits */
Bart Van Assche76c75b22010-11-26 14:37:47 -05001727 if (iu_type != SRP_IU_RSP) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01001728 if (ch->req_lim <= rsv) {
Bart Van Assche76c75b22010-11-26 14:37:47 -05001729 ++target->zero_req_lim;
1730 return NULL;
1731 }
1732
Bart Van Assche509c07b2014-10-30 14:48:30 +01001733 --ch->req_lim;
David Dillow05a1d752010-10-08 14:48:14 -04001734 }
1735
Bart Van Assche509c07b2014-10-30 14:48:30 +01001736 iu = list_first_entry(&ch->free_tx, struct srp_iu, list);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001737 list_del(&iu->list);
David Dillow05a1d752010-10-08 14:48:14 -04001738 return iu;
1739}
1740
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001741static void srp_send_done(struct ib_cq *cq, struct ib_wc *wc)
1742{
1743 struct srp_iu *iu = container_of(wc->wr_cqe, struct srp_iu, cqe);
1744 struct srp_rdma_ch *ch = cq->cq_context;
1745
1746 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1747 srp_handle_qp_err(cq, wc, "SEND");
1748 return;
1749 }
1750
1751 list_add(&iu->list, &ch->free_tx);
1752}
1753
Bart Van Assche509c07b2014-10-30 14:48:30 +01001754static int srp_post_send(struct srp_rdma_ch *ch, struct srp_iu *iu, int len)
David Dillow05a1d752010-10-08 14:48:14 -04001755{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001756 struct srp_target_port *target = ch->target;
David Dillow05a1d752010-10-08 14:48:14 -04001757 struct ib_sge list;
1758 struct ib_send_wr wr, *bad_wr;
David Dillow05a1d752010-10-08 14:48:14 -04001759
1760 list.addr = iu->dma;
1761 list.length = len;
David Dillow9af76272010-11-26 15:34:46 -05001762 list.lkey = target->lkey;
David Dillow05a1d752010-10-08 14:48:14 -04001763
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001764 iu->cqe.done = srp_send_done;
1765
David Dillow05a1d752010-10-08 14:48:14 -04001766 wr.next = NULL;
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001767 wr.wr_cqe = &iu->cqe;
David Dillow05a1d752010-10-08 14:48:14 -04001768 wr.sg_list = &list;
1769 wr.num_sge = 1;
1770 wr.opcode = IB_WR_SEND;
1771 wr.send_flags = IB_SEND_SIGNALED;
1772
Bart Van Assche509c07b2014-10-30 14:48:30 +01001773 return ib_post_send(ch->qp, &wr, &bad_wr);
David Dillow05a1d752010-10-08 14:48:14 -04001774}
1775
Bart Van Assche509c07b2014-10-30 14:48:30 +01001776static int srp_post_recv(struct srp_rdma_ch *ch, struct srp_iu *iu)
Bart Van Asschec996bb42010-07-30 10:59:05 +00001777{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001778 struct srp_target_port *target = ch->target;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001779 struct ib_recv_wr wr, *bad_wr;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001780 struct ib_sge list;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001781
1782 list.addr = iu->dma;
1783 list.length = iu->size;
David Dillow9af76272010-11-26 15:34:46 -05001784 list.lkey = target->lkey;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001785
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001786 iu->cqe.done = srp_recv_done;
1787
Bart Van Asschec996bb42010-07-30 10:59:05 +00001788 wr.next = NULL;
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001789 wr.wr_cqe = &iu->cqe;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001790 wr.sg_list = &list;
1791 wr.num_sge = 1;
1792
Bart Van Assche509c07b2014-10-30 14:48:30 +01001793 return ib_post_recv(ch->qp, &wr, &bad_wr);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001794}
1795
Bart Van Assche509c07b2014-10-30 14:48:30 +01001796static void srp_process_rsp(struct srp_rdma_ch *ch, struct srp_rsp *rsp)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001797{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001798 struct srp_target_port *target = ch->target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001799 struct srp_request *req;
1800 struct scsi_cmnd *scmnd;
1801 unsigned long flags;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001802
Roland Dreieraef9ec32005-11-02 14:07:13 -08001803 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01001804 spin_lock_irqsave(&ch->lock, flags);
1805 ch->req_lim += be32_to_cpu(rsp->req_lim_delta);
1806 spin_unlock_irqrestore(&ch->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -05001807
Bart Van Assche509c07b2014-10-30 14:48:30 +01001808 ch->tsk_mgmt_status = -1;
David Dillowf8b6e312010-11-26 13:02:21 -05001809 if (be32_to_cpu(rsp->resp_data_len) >= 4)
Bart Van Assche509c07b2014-10-30 14:48:30 +01001810 ch->tsk_mgmt_status = rsp->data[3];
1811 complete(&ch->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001812 } else {
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02001813 scmnd = scsi_host_find_tag(target->scsi_host, rsp->tag);
1814 if (scmnd) {
1815 req = (void *)scmnd->host_scribble;
1816 scmnd = srp_claim_req(ch, req, NULL, scmnd);
1817 }
Bart Van Assche22032992012-08-14 13:18:53 +00001818 if (!scmnd) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001819 shost_printk(KERN_ERR, target->scsi_host,
Bart Van Assched92c0da2014-10-06 17:14:36 +02001820 "Null scmnd for RSP w/tag %#016llx received on ch %td / QP %#x\n",
1821 rsp->tag, ch - target->ch, ch->qp->qp_num);
Bart Van Assche22032992012-08-14 13:18:53 +00001822
Bart Van Assche509c07b2014-10-30 14:48:30 +01001823 spin_lock_irqsave(&ch->lock, flags);
1824 ch->req_lim += be32_to_cpu(rsp->req_lim_delta);
1825 spin_unlock_irqrestore(&ch->lock, flags);
Bart Van Assche22032992012-08-14 13:18:53 +00001826
1827 return;
1828 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001829 scmnd->result = rsp->status;
1830
1831 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
1832 memcpy(scmnd->sense_buffer, rsp->data +
1833 be32_to_cpu(rsp->resp_data_len),
1834 min_t(int, be32_to_cpu(rsp->sense_data_len),
1835 SCSI_SENSE_BUFFERSIZE));
1836 }
1837
Bart Van Asschee7145312014-07-09 15:57:51 +02001838 if (unlikely(rsp->flags & SRP_RSP_FLAG_DIUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001839 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
Bart Van Asschee7145312014-07-09 15:57:51 +02001840 else if (unlikely(rsp->flags & SRP_RSP_FLAG_DIOVER))
1841 scsi_set_resid(scmnd, -be32_to_cpu(rsp->data_in_res_cnt));
1842 else if (unlikely(rsp->flags & SRP_RSP_FLAG_DOUNDER))
1843 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
1844 else if (unlikely(rsp->flags & SRP_RSP_FLAG_DOOVER))
1845 scsi_set_resid(scmnd, -be32_to_cpu(rsp->data_out_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001846
Bart Van Assche509c07b2014-10-30 14:48:30 +01001847 srp_free_req(ch, req, scmnd,
Bart Van Assche22032992012-08-14 13:18:53 +00001848 be32_to_cpu(rsp->req_lim_delta));
1849
David Dillowf8b6e312010-11-26 13:02:21 -05001850 scmnd->host_scribble = NULL;
1851 scmnd->scsi_done(scmnd);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001852 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001853}
1854
Bart Van Assche509c07b2014-10-30 14:48:30 +01001855static int srp_response_common(struct srp_rdma_ch *ch, s32 req_delta,
David Dillowbb125882010-10-08 14:40:47 -04001856 void *rsp, int len)
1857{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001858 struct srp_target_port *target = ch->target;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001859 struct ib_device *dev = target->srp_host->srp_dev->dev;
David Dillowbb125882010-10-08 14:40:47 -04001860 unsigned long flags;
1861 struct srp_iu *iu;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001862 int err;
David Dillowbb125882010-10-08 14:40:47 -04001863
Bart Van Assche509c07b2014-10-30 14:48:30 +01001864 spin_lock_irqsave(&ch->lock, flags);
1865 ch->req_lim += req_delta;
1866 iu = __srp_get_tx_iu(ch, SRP_IU_RSP);
1867 spin_unlock_irqrestore(&ch->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001868
David Dillowbb125882010-10-08 14:40:47 -04001869 if (!iu) {
1870 shost_printk(KERN_ERR, target->scsi_host, PFX
1871 "no IU available to send response\n");
Bart Van Assche76c75b22010-11-26 14:37:47 -05001872 return 1;
David Dillowbb125882010-10-08 14:40:47 -04001873 }
1874
1875 ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE);
1876 memcpy(iu->buf, rsp, len);
1877 ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE);
1878
Bart Van Assche509c07b2014-10-30 14:48:30 +01001879 err = srp_post_send(ch, iu, len);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001880 if (err) {
David Dillowbb125882010-10-08 14:40:47 -04001881 shost_printk(KERN_ERR, target->scsi_host, PFX
1882 "unable to post response: %d\n", err);
Bart Van Assche509c07b2014-10-30 14:48:30 +01001883 srp_put_tx_iu(ch, iu, SRP_IU_RSP);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001884 }
David Dillowbb125882010-10-08 14:40:47 -04001885
David Dillowbb125882010-10-08 14:40:47 -04001886 return err;
1887}
1888
Bart Van Assche509c07b2014-10-30 14:48:30 +01001889static void srp_process_cred_req(struct srp_rdma_ch *ch,
David Dillowbb125882010-10-08 14:40:47 -04001890 struct srp_cred_req *req)
1891{
1892 struct srp_cred_rsp rsp = {
1893 .opcode = SRP_CRED_RSP,
1894 .tag = req->tag,
1895 };
1896 s32 delta = be32_to_cpu(req->req_lim_delta);
1897
Bart Van Assche509c07b2014-10-30 14:48:30 +01001898 if (srp_response_common(ch, delta, &rsp, sizeof(rsp)))
1899 shost_printk(KERN_ERR, ch->target->scsi_host, PFX
David Dillowbb125882010-10-08 14:40:47 -04001900 "problems processing SRP_CRED_REQ\n");
1901}
1902
Bart Van Assche509c07b2014-10-30 14:48:30 +01001903static void srp_process_aer_req(struct srp_rdma_ch *ch,
David Dillowbb125882010-10-08 14:40:47 -04001904 struct srp_aer_req *req)
1905{
Bart Van Assche509c07b2014-10-30 14:48:30 +01001906 struct srp_target_port *target = ch->target;
David Dillowbb125882010-10-08 14:40:47 -04001907 struct srp_aer_rsp rsp = {
1908 .opcode = SRP_AER_RSP,
1909 .tag = req->tag,
1910 };
1911 s32 delta = be32_to_cpu(req->req_lim_delta);
1912
1913 shost_printk(KERN_ERR, target->scsi_host, PFX
Bart Van Assche985aa492015-05-18 13:27:14 +02001914 "ignoring AER for LUN %llu\n", scsilun_to_int(&req->lun));
David Dillowbb125882010-10-08 14:40:47 -04001915
Bart Van Assche509c07b2014-10-30 14:48:30 +01001916 if (srp_response_common(ch, delta, &rsp, sizeof(rsp)))
David Dillowbb125882010-10-08 14:40:47 -04001917 shost_printk(KERN_ERR, target->scsi_host, PFX
1918 "problems processing SRP_AER_REQ\n");
1919}
1920
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001921static void srp_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001922{
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001923 struct srp_iu *iu = container_of(wc->wr_cqe, struct srp_iu, cqe);
1924 struct srp_rdma_ch *ch = cq->cq_context;
Bart Van Assche509c07b2014-10-30 14:48:30 +01001925 struct srp_target_port *target = ch->target;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001926 struct ib_device *dev = target->srp_host->srp_dev->dev;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001927 int res;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001928 u8 opcode;
1929
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001930 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1931 srp_handle_qp_err(cq, wc, "RECV");
1932 return;
1933 }
1934
Bart Van Assche509c07b2014-10-30 14:48:30 +01001935 ib_dma_sync_single_for_cpu(dev, iu->dma, ch->max_ti_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08001936 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001937
1938 opcode = *(u8 *) iu->buf;
1939
1940 if (0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001941 shost_printk(KERN_ERR, target->scsi_host,
1942 PFX "recv completion, opcode 0x%02x\n", opcode);
Bart Van Assche7a700812010-07-29 15:56:37 +00001943 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1,
1944 iu->buf, wc->byte_len, true);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001945 }
1946
1947 switch (opcode) {
1948 case SRP_RSP:
Bart Van Assche509c07b2014-10-30 14:48:30 +01001949 srp_process_rsp(ch, iu->buf);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001950 break;
1951
David Dillowbb125882010-10-08 14:40:47 -04001952 case SRP_CRED_REQ:
Bart Van Assche509c07b2014-10-30 14:48:30 +01001953 srp_process_cred_req(ch, iu->buf);
David Dillowbb125882010-10-08 14:40:47 -04001954 break;
1955
1956 case SRP_AER_REQ:
Bart Van Assche509c07b2014-10-30 14:48:30 +01001957 srp_process_aer_req(ch, iu->buf);
David Dillowbb125882010-10-08 14:40:47 -04001958 break;
1959
Roland Dreieraef9ec32005-11-02 14:07:13 -08001960 case SRP_T_LOGOUT:
1961 /* XXX Handle target logout */
David Dillow7aa54bd2008-01-07 18:23:41 -05001962 shost_printk(KERN_WARNING, target->scsi_host,
1963 PFX "Got target logout request\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001964 break;
1965
1966 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001967 shost_printk(KERN_WARNING, target->scsi_host,
1968 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001969 break;
1970 }
1971
Bart Van Assche509c07b2014-10-30 14:48:30 +01001972 ib_dma_sync_single_for_device(dev, iu->dma, ch->max_ti_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08001973 DMA_FROM_DEVICE);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001974
Bart Van Assche509c07b2014-10-30 14:48:30 +01001975 res = srp_post_recv(ch, iu);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001976 if (res != 0)
1977 shost_printk(KERN_ERR, target->scsi_host,
1978 PFX "Recv failed with error code %d\n", res);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001979}
1980
Bart Van Asschec1120f82013-10-26 14:35:08 +02001981/**
1982 * srp_tl_err_work() - handle a transport layer error
Bart Van Asscheaf246632014-05-20 15:04:21 +02001983 * @work: Work structure embedded in an SRP target port.
Bart Van Asschec1120f82013-10-26 14:35:08 +02001984 *
1985 * Note: This function may get invoked before the rport has been created,
1986 * hence the target->rport test.
1987 */
1988static void srp_tl_err_work(struct work_struct *work)
1989{
1990 struct srp_target_port *target;
1991
1992 target = container_of(work, struct srp_target_port, tl_err_work);
1993 if (target->rport)
1994 srp_start_tl_fail_timers(target->rport);
1995}
1996
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01001997static void srp_handle_qp_err(struct ib_cq *cq, struct ib_wc *wc,
1998 const char *opname)
Bart Van Assche948d1e82011-09-03 09:25:42 +02001999{
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01002000 struct srp_rdma_ch *ch = cq->cq_context;
Bart Van Assche7dad6b22014-10-21 18:00:35 +02002001 struct srp_target_port *target = ch->target;
2002
Bart Van Asschec014c8c2015-05-18 13:23:57 +02002003 if (ch->connected && !target->qp_in_error) {
Christoph Hellwig1dc7b1f2015-11-13 12:57:13 +01002004 shost_printk(KERN_ERR, target->scsi_host,
2005 PFX "failed %s status %s (%d) for CQE %p\n",
2006 opname, ib_wc_status_msg(wc->status), wc->status,
2007 wc->wr_cqe);
Bart Van Asschec1120f82013-10-26 14:35:08 +02002008 queue_work(system_long_wq, &target->tl_err_work);
Bart Van Assche4f0af692012-11-26 11:16:40 +01002009 }
Bart Van Assche948d1e82011-09-03 09:25:42 +02002010 target->qp_in_error = true;
2011}
2012
Bart Van Assche76c75b22010-11-26 14:37:47 -05002013static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002014{
Bart Van Assche76c75b22010-11-26 14:37:47 -05002015 struct srp_target_port *target = host_to_target(shost);
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002016 struct srp_rport *rport = target->rport;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002017 struct srp_rdma_ch *ch;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002018 struct srp_request *req;
2019 struct srp_iu *iu;
2020 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -08002021 struct ib_device *dev;
Bart Van Assche76c75b22010-11-26 14:37:47 -05002022 unsigned long flags;
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02002023 u32 tag;
2024 u16 idx;
Bart Van Assched1b42892014-05-20 15:07:20 +02002025 int len, ret;
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002026 const bool in_scsi_eh = !in_interrupt() && current == shost->ehandler;
2027
2028 /*
2029 * The SCSI EH thread is the only context from which srp_queuecommand()
2030 * can get invoked for blocked devices (SDEV_BLOCK /
2031 * SDEV_CREATED_BLOCK). Avoid racing with srp_reconnect_rport() by
2032 * locking the rport mutex if invoked from inside the SCSI EH.
2033 */
2034 if (in_scsi_eh)
2035 mutex_lock(&rport->mutex);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002036
Bart Van Assched1b42892014-05-20 15:07:20 +02002037 scmnd->result = srp_chkready(target->rport);
2038 if (unlikely(scmnd->result))
2039 goto err;
Bart Van Assche2ce19e72013-02-21 17:20:00 +00002040
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02002041 WARN_ON_ONCE(scmnd->request->tag < 0);
2042 tag = blk_mq_unique_tag(scmnd->request);
Bart Van Assched92c0da2014-10-06 17:14:36 +02002043 ch = &target->ch[blk_mq_unique_tag_to_hwq(tag)];
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02002044 idx = blk_mq_unique_tag_to_tag(tag);
2045 WARN_ONCE(idx >= target->req_ring_size, "%s: tag %#x: idx %d >= %d\n",
2046 dev_name(&shost->shost_gendev), tag, idx,
2047 target->req_ring_size);
Bart Van Assche509c07b2014-10-30 14:48:30 +01002048
2049 spin_lock_irqsave(&ch->lock, flags);
2050 iu = __srp_get_tx_iu(ch, SRP_IU_CMD);
Bart Van Assche509c07b2014-10-30 14:48:30 +01002051 spin_unlock_irqrestore(&ch->lock, flags);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002052
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02002053 if (!iu)
2054 goto err;
2055
2056 req = &ch->req_ring[idx];
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002057 dev = target->srp_host->srp_dev->dev;
David Dillow49248642011-01-14 18:23:24 -05002058 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08002059 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002060
David Dillowf8b6e312010-11-26 13:02:21 -05002061 scmnd->host_scribble = (void *) req;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002062
2063 cmd = iu->buf;
2064 memset(cmd, 0, sizeof *cmd);
2065
2066 cmd->opcode = SRP_CMD;
Bart Van Assche985aa492015-05-18 13:27:14 +02002067 int_to_scsilun(scmnd->device->lun, &cmd->lun);
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02002068 cmd->tag = tag;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002069 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
2070
Roland Dreieraef9ec32005-11-02 14:07:13 -08002071 req->scmnd = scmnd;
2072 req->cmd = iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002073
Bart Van Assche509c07b2014-10-30 14:48:30 +01002074 len = srp_map_data(scmnd, ch, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002075 if (len < 0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05002076 shost_printk(KERN_ERR, target->scsi_host,
Bart Van Assched1b42892014-05-20 15:07:20 +02002077 PFX "Failed to map data (%d)\n", len);
2078 /*
2079 * If we ran out of memory descriptors (-ENOMEM) because an
2080 * application is queuing many requests with more than
Bart Van Assche52ede082014-05-20 15:07:45 +02002081 * max_pages_per_mr sg-list elements, tell the SCSI mid-layer
Bart Van Assched1b42892014-05-20 15:07:20 +02002082 * to reduce queue depth temporarily.
2083 */
2084 scmnd->result = len == -ENOMEM ?
2085 DID_OK << 16 | QUEUE_FULL << 1 : DID_ERROR << 16;
Bart Van Assche76c75b22010-11-26 14:37:47 -05002086 goto err_iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002087 }
2088
David Dillow49248642011-01-14 18:23:24 -05002089 ib_dma_sync_single_for_device(dev, iu->dma, target->max_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08002090 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002091
Bart Van Assche509c07b2014-10-30 14:48:30 +01002092 if (srp_post_send(ch, iu, len)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05002093 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002094 goto err_unmap;
2095 }
2096
Bart Van Assched1b42892014-05-20 15:07:20 +02002097 ret = 0;
2098
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002099unlock_rport:
2100 if (in_scsi_eh)
2101 mutex_unlock(&rport->mutex);
2102
Bart Van Assched1b42892014-05-20 15:07:20 +02002103 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002104
2105err_unmap:
Bart Van Assche509c07b2014-10-30 14:48:30 +01002106 srp_unmap_data(scmnd, ch, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002107
Bart Van Assche76c75b22010-11-26 14:37:47 -05002108err_iu:
Bart Van Assche509c07b2014-10-30 14:48:30 +01002109 srp_put_tx_iu(ch, iu, SRP_IU_CMD);
Bart Van Assche76c75b22010-11-26 14:37:47 -05002110
Bart Van Assche024ca902014-05-20 15:03:49 +02002111 /*
2112 * Avoid that the loops that iterate over the request ring can
2113 * encounter a dangling SCSI command pointer.
2114 */
2115 req->scmnd = NULL;
2116
Bart Van Assched1b42892014-05-20 15:07:20 +02002117err:
2118 if (scmnd->result) {
2119 scmnd->scsi_done(scmnd);
2120 ret = 0;
2121 } else {
2122 ret = SCSI_MLQUEUE_HOST_BUSY;
2123 }
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002124
Bart Van Assched1b42892014-05-20 15:07:20 +02002125 goto unlock_rport;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002126}
2127
Bart Van Assche4d73f952013-10-26 14:40:37 +02002128/*
2129 * Note: the resources allocated in this function are freed in
Bart Van Assche509c07b2014-10-30 14:48:30 +01002130 * srp_free_ch_ib().
Bart Van Assche4d73f952013-10-26 14:40:37 +02002131 */
Bart Van Assche509c07b2014-10-30 14:48:30 +01002132static int srp_alloc_iu_bufs(struct srp_rdma_ch *ch)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002133{
Bart Van Assche509c07b2014-10-30 14:48:30 +01002134 struct srp_target_port *target = ch->target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002135 int i;
2136
Bart Van Assche509c07b2014-10-30 14:48:30 +01002137 ch->rx_ring = kcalloc(target->queue_size, sizeof(*ch->rx_ring),
2138 GFP_KERNEL);
2139 if (!ch->rx_ring)
Bart Van Assche4d73f952013-10-26 14:40:37 +02002140 goto err_no_ring;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002141 ch->tx_ring = kcalloc(target->queue_size, sizeof(*ch->tx_ring),
2142 GFP_KERNEL);
2143 if (!ch->tx_ring)
Bart Van Assche4d73f952013-10-26 14:40:37 +02002144 goto err_no_ring;
2145
2146 for (i = 0; i < target->queue_size; ++i) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01002147 ch->rx_ring[i] = srp_alloc_iu(target->srp_host,
2148 ch->max_ti_iu_len,
2149 GFP_KERNEL, DMA_FROM_DEVICE);
2150 if (!ch->rx_ring[i])
Roland Dreieraef9ec32005-11-02 14:07:13 -08002151 goto err;
2152 }
2153
Bart Van Assche4d73f952013-10-26 14:40:37 +02002154 for (i = 0; i < target->queue_size; ++i) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01002155 ch->tx_ring[i] = srp_alloc_iu(target->srp_host,
2156 target->max_iu_len,
2157 GFP_KERNEL, DMA_TO_DEVICE);
2158 if (!ch->tx_ring[i])
Roland Dreieraef9ec32005-11-02 14:07:13 -08002159 goto err;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05002160
Bart Van Assche509c07b2014-10-30 14:48:30 +01002161 list_add(&ch->tx_ring[i]->list, &ch->free_tx);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002162 }
2163
2164 return 0;
2165
2166err:
Bart Van Assche4d73f952013-10-26 14:40:37 +02002167 for (i = 0; i < target->queue_size; ++i) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01002168 srp_free_iu(target->srp_host, ch->rx_ring[i]);
2169 srp_free_iu(target->srp_host, ch->tx_ring[i]);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002170 }
2171
Bart Van Assche4d73f952013-10-26 14:40:37 +02002172
2173err_no_ring:
Bart Van Assche509c07b2014-10-30 14:48:30 +01002174 kfree(ch->tx_ring);
2175 ch->tx_ring = NULL;
2176 kfree(ch->rx_ring);
2177 ch->rx_ring = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002178
2179 return -ENOMEM;
2180}
2181
Bart Van Asschec9b03c12011-09-03 09:34:48 +02002182static uint32_t srp_compute_rq_tmo(struct ib_qp_attr *qp_attr, int attr_mask)
2183{
2184 uint64_t T_tr_ns, max_compl_time_ms;
2185 uint32_t rq_tmo_jiffies;
2186
2187 /*
2188 * According to section 11.2.4.2 in the IBTA spec (Modify Queue Pair,
2189 * table 91), both the QP timeout and the retry count have to be set
2190 * for RC QP's during the RTR to RTS transition.
2191 */
2192 WARN_ON_ONCE((attr_mask & (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)) !=
2193 (IB_QP_TIMEOUT | IB_QP_RETRY_CNT));
2194
2195 /*
2196 * Set target->rq_tmo_jiffies to one second more than the largest time
2197 * it can take before an error completion is generated. See also
2198 * C9-140..142 in the IBTA spec for more information about how to
2199 * convert the QP Local ACK Timeout value to nanoseconds.
2200 */
2201 T_tr_ns = 4096 * (1ULL << qp_attr->timeout);
2202 max_compl_time_ms = qp_attr->retry_cnt * 4 * T_tr_ns;
2203 do_div(max_compl_time_ms, NSEC_PER_MSEC);
2204 rq_tmo_jiffies = msecs_to_jiffies(max_compl_time_ms + 1000);
2205
2206 return rq_tmo_jiffies;
2207}
2208
David Dillow961e0be2011-01-14 17:32:07 -05002209static void srp_cm_rep_handler(struct ib_cm_id *cm_id,
Bart Van Asschee6300cb2015-07-31 14:12:48 -07002210 const struct srp_login_rsp *lrsp,
Bart Van Assche509c07b2014-10-30 14:48:30 +01002211 struct srp_rdma_ch *ch)
David Dillow961e0be2011-01-14 17:32:07 -05002212{
Bart Van Assche509c07b2014-10-30 14:48:30 +01002213 struct srp_target_port *target = ch->target;
David Dillow961e0be2011-01-14 17:32:07 -05002214 struct ib_qp_attr *qp_attr = NULL;
2215 int attr_mask = 0;
2216 int ret;
2217 int i;
2218
2219 if (lrsp->opcode == SRP_LOGIN_RSP) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01002220 ch->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len);
2221 ch->req_lim = be32_to_cpu(lrsp->req_lim_delta);
David Dillow961e0be2011-01-14 17:32:07 -05002222
2223 /*
2224 * Reserve credits for task management so we don't
2225 * bounce requests back to the SCSI mid-layer.
2226 */
2227 target->scsi_host->can_queue
Bart Van Assche509c07b2014-10-30 14:48:30 +01002228 = min(ch->req_lim - SRP_TSK_MGMT_SQ_SIZE,
David Dillow961e0be2011-01-14 17:32:07 -05002229 target->scsi_host->can_queue);
Bart Van Assche4d73f952013-10-26 14:40:37 +02002230 target->scsi_host->cmd_per_lun
2231 = min_t(int, target->scsi_host->can_queue,
2232 target->scsi_host->cmd_per_lun);
David Dillow961e0be2011-01-14 17:32:07 -05002233 } else {
2234 shost_printk(KERN_WARNING, target->scsi_host,
2235 PFX "Unhandled RSP opcode %#x\n", lrsp->opcode);
2236 ret = -ECONNRESET;
2237 goto error;
2238 }
2239
Bart Van Assche509c07b2014-10-30 14:48:30 +01002240 if (!ch->rx_ring) {
2241 ret = srp_alloc_iu_bufs(ch);
David Dillow961e0be2011-01-14 17:32:07 -05002242 if (ret)
2243 goto error;
2244 }
2245
2246 ret = -ENOMEM;
2247 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
2248 if (!qp_attr)
2249 goto error;
2250
2251 qp_attr->qp_state = IB_QPS_RTR;
2252 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
2253 if (ret)
2254 goto error_free;
2255
Bart Van Assche509c07b2014-10-30 14:48:30 +01002256 ret = ib_modify_qp(ch->qp, qp_attr, attr_mask);
David Dillow961e0be2011-01-14 17:32:07 -05002257 if (ret)
2258 goto error_free;
2259
Bart Van Assche4d73f952013-10-26 14:40:37 +02002260 for (i = 0; i < target->queue_size; i++) {
Bart Van Assche509c07b2014-10-30 14:48:30 +01002261 struct srp_iu *iu = ch->rx_ring[i];
2262
2263 ret = srp_post_recv(ch, iu);
David Dillow961e0be2011-01-14 17:32:07 -05002264 if (ret)
2265 goto error_free;
2266 }
2267
2268 qp_attr->qp_state = IB_QPS_RTS;
2269 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
2270 if (ret)
2271 goto error_free;
2272
Bart Van Asschec9b03c12011-09-03 09:34:48 +02002273 target->rq_tmo_jiffies = srp_compute_rq_tmo(qp_attr, attr_mask);
2274
Bart Van Assche509c07b2014-10-30 14:48:30 +01002275 ret = ib_modify_qp(ch->qp, qp_attr, attr_mask);
David Dillow961e0be2011-01-14 17:32:07 -05002276 if (ret)
2277 goto error_free;
2278
2279 ret = ib_send_cm_rtu(cm_id, NULL, 0);
2280
2281error_free:
2282 kfree(qp_attr);
2283
2284error:
Bart Van Assche509c07b2014-10-30 14:48:30 +01002285 ch->status = ret;
David Dillow961e0be2011-01-14 17:32:07 -05002286}
2287
Roland Dreieraef9ec32005-11-02 14:07:13 -08002288static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
2289 struct ib_cm_event *event,
Bart Van Assche509c07b2014-10-30 14:48:30 +01002290 struct srp_rdma_ch *ch)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002291{
Bart Van Assche509c07b2014-10-30 14:48:30 +01002292 struct srp_target_port *target = ch->target;
David Dillow7aa54bd2008-01-07 18:23:41 -05002293 struct Scsi_Host *shost = target->scsi_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002294 struct ib_class_port_info *cpi;
2295 int opcode;
2296
2297 switch (event->param.rej_rcvd.reason) {
2298 case IB_CM_REJ_PORT_CM_REDIRECT:
2299 cpi = event->param.rej_rcvd.ari;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002300 ch->path.dlid = cpi->redirect_lid;
2301 ch->path.pkey = cpi->redirect_pkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002302 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002303 memcpy(ch->path.dgid.raw, cpi->redirect_gid, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002304
Bart Van Assche509c07b2014-10-30 14:48:30 +01002305 ch->status = ch->path.dlid ?
Roland Dreieraef9ec32005-11-02 14:07:13 -08002306 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
2307 break;
2308
2309 case IB_CM_REJ_PORT_REDIRECT:
Roland Dreier5d7cbfd2007-08-03 10:45:18 -07002310 if (srp_target_is_topspin(target)) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002311 /*
2312 * Topspin/Cisco SRP gateways incorrectly send
2313 * reject reason code 25 when they mean 24
2314 * (port redirect).
2315 */
Bart Van Assche509c07b2014-10-30 14:48:30 +01002316 memcpy(ch->path.dgid.raw,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002317 event->param.rej_rcvd.ari, 16);
2318
David Dillow7aa54bd2008-01-07 18:23:41 -05002319 shost_printk(KERN_DEBUG, shost,
2320 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
Bart Van Assche509c07b2014-10-30 14:48:30 +01002321 be64_to_cpu(ch->path.dgid.global.subnet_prefix),
2322 be64_to_cpu(ch->path.dgid.global.interface_id));
Roland Dreieraef9ec32005-11-02 14:07:13 -08002323
Bart Van Assche509c07b2014-10-30 14:48:30 +01002324 ch->status = SRP_PORT_REDIRECT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002325 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05002326 shost_printk(KERN_WARNING, shost,
2327 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
Bart Van Assche509c07b2014-10-30 14:48:30 +01002328 ch->status = -ECONNRESET;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002329 }
2330 break;
2331
2332 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
David Dillow7aa54bd2008-01-07 18:23:41 -05002333 shost_printk(KERN_WARNING, shost,
2334 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
Bart Van Assche509c07b2014-10-30 14:48:30 +01002335 ch->status = -ECONNRESET;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002336 break;
2337
2338 case IB_CM_REJ_CONSUMER_DEFINED:
2339 opcode = *(u8 *) event->private_data;
2340 if (opcode == SRP_LOGIN_REJ) {
2341 struct srp_login_rej *rej = event->private_data;
2342 u32 reason = be32_to_cpu(rej->reason);
2343
2344 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
David Dillow7aa54bd2008-01-07 18:23:41 -05002345 shost_printk(KERN_WARNING, shost,
2346 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002347 else
Bart Van Asschee7ffde02014-03-14 13:52:21 +01002348 shost_printk(KERN_WARNING, shost, PFX
2349 "SRP LOGIN from %pI6 to %pI6 REJECTED, reason 0x%08x\n",
Bart Van Assche747fe002014-10-30 14:48:05 +01002350 target->sgid.raw,
2351 target->orig_dgid.raw, reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002352 } else
David Dillow7aa54bd2008-01-07 18:23:41 -05002353 shost_printk(KERN_WARNING, shost,
2354 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
2355 " opcode 0x%02x\n", opcode);
Bart Van Assche509c07b2014-10-30 14:48:30 +01002356 ch->status = -ECONNRESET;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002357 break;
2358
David Dillow9fe4bcf2008-01-08 17:08:52 -05002359 case IB_CM_REJ_STALE_CONN:
2360 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
Bart Van Assche509c07b2014-10-30 14:48:30 +01002361 ch->status = SRP_STALE_CONN;
David Dillow9fe4bcf2008-01-08 17:08:52 -05002362 break;
2363
Roland Dreieraef9ec32005-11-02 14:07:13 -08002364 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05002365 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
2366 event->param.rej_rcvd.reason);
Bart Van Assche509c07b2014-10-30 14:48:30 +01002367 ch->status = -ECONNRESET;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002368 }
2369}
2370
2371static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2372{
Bart Van Assche509c07b2014-10-30 14:48:30 +01002373 struct srp_rdma_ch *ch = cm_id->context;
2374 struct srp_target_port *target = ch->target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002375 int comp = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002376
2377 switch (event->event) {
2378 case IB_CM_REQ_ERROR:
David Dillow7aa54bd2008-01-07 18:23:41 -05002379 shost_printk(KERN_DEBUG, target->scsi_host,
2380 PFX "Sending CM REQ failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002381 comp = 1;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002382 ch->status = -ECONNRESET;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002383 break;
2384
2385 case IB_CM_REP_RECEIVED:
2386 comp = 1;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002387 srp_cm_rep_handler(cm_id, event->private_data, ch);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002388 break;
2389
2390 case IB_CM_REJ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05002391 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002392 comp = 1;
2393
Bart Van Assche509c07b2014-10-30 14:48:30 +01002394 srp_cm_rej_handler(cm_id, event, ch);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002395 break;
2396
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07002397 case IB_CM_DREQ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05002398 shost_printk(KERN_WARNING, target->scsi_host,
2399 PFX "DREQ received - connection closed\n");
Bart Van Asschec014c8c2015-05-18 13:23:57 +02002400 ch->connected = false;
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07002401 if (ib_send_cm_drep(cm_id, NULL, 0))
David Dillow7aa54bd2008-01-07 18:23:41 -05002402 shost_printk(KERN_ERR, target->scsi_host,
2403 PFX "Sending CM DREP failed\n");
Bart Van Asschec1120f82013-10-26 14:35:08 +02002404 queue_work(system_long_wq, &target->tl_err_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002405 break;
2406
2407 case IB_CM_TIMEWAIT_EXIT:
David Dillow7aa54bd2008-01-07 18:23:41 -05002408 shost_printk(KERN_ERR, target->scsi_host,
2409 PFX "connection closed\n");
Bart Van Asscheac72d762014-03-14 13:53:40 +01002410 comp = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002411
Bart Van Assche509c07b2014-10-30 14:48:30 +01002412 ch->status = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002413 break;
2414
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07002415 case IB_CM_MRA_RECEIVED:
2416 case IB_CM_DREQ_ERROR:
2417 case IB_CM_DREP_RECEIVED:
2418 break;
2419
Roland Dreieraef9ec32005-11-02 14:07:13 -08002420 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05002421 shost_printk(KERN_WARNING, target->scsi_host,
2422 PFX "Unhandled CM event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002423 break;
2424 }
2425
2426 if (comp)
Bart Van Assche509c07b2014-10-30 14:48:30 +01002427 complete(&ch->done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002428
Roland Dreieraef9ec32005-11-02 14:07:13 -08002429 return 0;
2430}
2431
Jack Wang71444b92013-11-07 11:37:37 +01002432/**
Jack Wang71444b92013-11-07 11:37:37 +01002433 * srp_change_queue_depth - setting device queue depth
2434 * @sdev: scsi device struct
2435 * @qdepth: requested queue depth
Jack Wang71444b92013-11-07 11:37:37 +01002436 *
2437 * Returns queue depth.
2438 */
2439static int
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002440srp_change_queue_depth(struct scsi_device *sdev, int qdepth)
Jack Wang71444b92013-11-07 11:37:37 +01002441{
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01002442 if (!sdev->tagged_supported)
Christoph Hellwig1e6f2412014-11-13 14:27:41 +01002443 qdepth = 1;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002444 return scsi_change_queue_depth(sdev, qdepth);
Jack Wang71444b92013-11-07 11:37:37 +01002445}
2446
Bart Van Assche985aa492015-05-18 13:27:14 +02002447static int srp_send_tsk_mgmt(struct srp_rdma_ch *ch, u64 req_tag, u64 lun,
2448 u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002449{
Bart Van Assche509c07b2014-10-30 14:48:30 +01002450 struct srp_target_port *target = ch->target;
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002451 struct srp_rport *rport = target->rport;
David Dillow19081f32010-10-18 08:54:49 -04002452 struct ib_device *dev = target->srp_host->srp_dev->dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002453 struct srp_iu *iu;
2454 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002455
Bart Van Asschec014c8c2015-05-18 13:23:57 +02002456 if (!ch->connected || target->qp_in_error)
Bart Van Assche3780d1f2013-02-21 17:18:00 +00002457 return -1;
2458
Bart Van Assche509c07b2014-10-30 14:48:30 +01002459 init_completion(&ch->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002460
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002461 /*
Bart Van Assche509c07b2014-10-30 14:48:30 +01002462 * Lock the rport mutex to avoid that srp_create_ch_ib() is
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002463 * invoked while a task management function is being sent.
2464 */
2465 mutex_lock(&rport->mutex);
Bart Van Assche509c07b2014-10-30 14:48:30 +01002466 spin_lock_irq(&ch->lock);
2467 iu = __srp_get_tx_iu(ch, SRP_IU_TSK_MGMT);
2468 spin_unlock_irq(&ch->lock);
Bart Van Assche76c75b22010-11-26 14:37:47 -05002469
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002470 if (!iu) {
2471 mutex_unlock(&rport->mutex);
2472
Bart Van Assche76c75b22010-11-26 14:37:47 -05002473 return -1;
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002474 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002475
David Dillow19081f32010-10-18 08:54:49 -04002476 ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt,
2477 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002478 tsk_mgmt = iu->buf;
2479 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
2480
2481 tsk_mgmt->opcode = SRP_TSK_MGMT;
Bart Van Assche985aa492015-05-18 13:27:14 +02002482 int_to_scsilun(lun, &tsk_mgmt->lun);
David Dillowf8b6e312010-11-26 13:02:21 -05002483 tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002484 tsk_mgmt->tsk_mgmt_func = func;
David Dillowf8b6e312010-11-26 13:02:21 -05002485 tsk_mgmt->task_tag = req_tag;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002486
David Dillow19081f32010-10-18 08:54:49 -04002487 ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt,
2488 DMA_TO_DEVICE);
Bart Van Assche509c07b2014-10-30 14:48:30 +01002489 if (srp_post_send(ch, iu, sizeof(*tsk_mgmt))) {
2490 srp_put_tx_iu(ch, iu, SRP_IU_TSK_MGMT);
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002491 mutex_unlock(&rport->mutex);
2492
Bart Van Assche76c75b22010-11-26 14:37:47 -05002493 return -1;
2494 }
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002495 mutex_unlock(&rport->mutex);
Roland Dreierd945e1d2006-05-09 10:50:28 -07002496
Bart Van Assche509c07b2014-10-30 14:48:30 +01002497 if (!wait_for_completion_timeout(&ch->tsk_mgmt_done,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002498 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07002499 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002500
Roland Dreierd945e1d2006-05-09 10:50:28 -07002501 return 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07002502}
2503
Roland Dreieraef9ec32005-11-02 14:07:13 -08002504static int srp_abort(struct scsi_cmnd *scmnd)
2505{
Roland Dreierd945e1d2006-05-09 10:50:28 -07002506 struct srp_target_port *target = host_to_target(scmnd->device->host);
David Dillowf8b6e312010-11-26 13:02:21 -05002507 struct srp_request *req = (struct srp_request *) scmnd->host_scribble;
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02002508 u32 tag;
Bart Van Assched92c0da2014-10-06 17:14:36 +02002509 u16 ch_idx;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002510 struct srp_rdma_ch *ch;
Bart Van Assche086f44f2013-06-12 15:23:04 +02002511 int ret;
Roland Dreierd945e1d2006-05-09 10:50:28 -07002512
David Dillow7aa54bd2008-01-07 18:23:41 -05002513 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002514
Bart Van Assched92c0da2014-10-06 17:14:36 +02002515 if (!req)
Bart Van Assche99b66972013-10-10 13:52:33 +02002516 return SUCCESS;
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02002517 tag = blk_mq_unique_tag(scmnd->request);
Bart Van Assched92c0da2014-10-06 17:14:36 +02002518 ch_idx = blk_mq_unique_tag_to_hwq(tag);
2519 if (WARN_ON_ONCE(ch_idx >= target->ch_count))
2520 return SUCCESS;
2521 ch = &target->ch[ch_idx];
2522 if (!srp_claim_req(ch, req, NULL, scmnd))
2523 return SUCCESS;
2524 shost_printk(KERN_ERR, target->scsi_host,
2525 "Sending SRP abort for tag %#x\n", tag);
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02002526 if (srp_send_tsk_mgmt(ch, tag, scmnd->device->lun,
Bart Van Assche80d5e8a2013-07-10 17:36:35 +02002527 SRP_TSK_ABORT_TASK) == 0)
Bart Van Assche086f44f2013-06-12 15:23:04 +02002528 ret = SUCCESS;
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002529 else if (target->rport->state == SRP_RPORT_LOST)
Bart Van Assche99e1c132013-06-28 14:49:58 +02002530 ret = FAST_IO_FAIL;
Bart Van Assche086f44f2013-06-12 15:23:04 +02002531 else
2532 ret = FAILED;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002533 srp_free_req(ch, req, scmnd, 0);
Bart Van Assche22032992012-08-14 13:18:53 +00002534 scmnd->result = DID_ABORT << 16;
Bart Van Assched8536672012-08-24 10:29:11 +00002535 scmnd->scsi_done(scmnd);
Roland Dreierd945e1d2006-05-09 10:50:28 -07002536
Bart Van Assche086f44f2013-06-12 15:23:04 +02002537 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002538}
2539
2540static int srp_reset_device(struct scsi_cmnd *scmnd)
2541{
Roland Dreierd945e1d2006-05-09 10:50:28 -07002542 struct srp_target_port *target = host_to_target(scmnd->device->host);
Bart Van Assched92c0da2014-10-06 17:14:36 +02002543 struct srp_rdma_ch *ch;
Bart Van Assche536ae142010-11-26 13:58:27 -05002544 int i;
Roland Dreierd945e1d2006-05-09 10:50:28 -07002545
David Dillow7aa54bd2008-01-07 18:23:41 -05002546 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002547
Bart Van Assched92c0da2014-10-06 17:14:36 +02002548 ch = &target->ch[0];
Bart Van Assche509c07b2014-10-30 14:48:30 +01002549 if (srp_send_tsk_mgmt(ch, SRP_TAG_NO_REQ, scmnd->device->lun,
David Dillowf8b6e312010-11-26 13:02:21 -05002550 SRP_TSK_LUN_RESET))
Roland Dreierd945e1d2006-05-09 10:50:28 -07002551 return FAILED;
Bart Van Assche509c07b2014-10-30 14:48:30 +01002552 if (ch->tsk_mgmt_status)
Roland Dreierd945e1d2006-05-09 10:50:28 -07002553 return FAILED;
2554
Bart Van Assched92c0da2014-10-06 17:14:36 +02002555 for (i = 0; i < target->ch_count; i++) {
2556 ch = &target->ch[i];
2557 for (i = 0; i < target->req_ring_size; ++i) {
2558 struct srp_request *req = &ch->req_ring[i];
Bart Van Assche509c07b2014-10-30 14:48:30 +01002559
Bart Van Assched92c0da2014-10-06 17:14:36 +02002560 srp_finish_req(ch, req, scmnd->device, DID_RESET << 16);
2561 }
Bart Van Assche536ae142010-11-26 13:58:27 -05002562 }
Roland Dreierd945e1d2006-05-09 10:50:28 -07002563
Roland Dreierd945e1d2006-05-09 10:50:28 -07002564 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002565}
2566
2567static int srp_reset_host(struct scsi_cmnd *scmnd)
2568{
2569 struct srp_target_port *target = host_to_target(scmnd->device->host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002570
David Dillow7aa54bd2008-01-07 18:23:41 -05002571 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002572
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002573 return srp_reconnect_rport(target->rport) == 0 ? SUCCESS : FAILED;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002574}
2575
Bart Van Asschec9b03c12011-09-03 09:34:48 +02002576static int srp_slave_configure(struct scsi_device *sdev)
2577{
2578 struct Scsi_Host *shost = sdev->host;
2579 struct srp_target_port *target = host_to_target(shost);
2580 struct request_queue *q = sdev->request_queue;
2581 unsigned long timeout;
2582
2583 if (sdev->type == TYPE_DISK) {
2584 timeout = max_t(unsigned, 30 * HZ, target->rq_tmo_jiffies);
2585 blk_queue_rq_timeout(q, timeout);
2586 }
2587
2588 return 0;
2589}
2590
Tony Jonesee959b02008-02-22 00:13:36 +01002591static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
2592 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002593{
Tony Jonesee959b02008-02-22 00:13:36 +01002594 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002595
Bart Van Assche45c37ca2015-05-18 13:25:10 +02002596 return sprintf(buf, "0x%016llx\n", be64_to_cpu(target->id_ext));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002597}
2598
Tony Jonesee959b02008-02-22 00:13:36 +01002599static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
2600 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002601{
Tony Jonesee959b02008-02-22 00:13:36 +01002602 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002603
Bart Van Assche45c37ca2015-05-18 13:25:10 +02002604 return sprintf(buf, "0x%016llx\n", be64_to_cpu(target->ioc_guid));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002605}
2606
Tony Jonesee959b02008-02-22 00:13:36 +01002607static ssize_t show_service_id(struct device *dev,
2608 struct device_attribute *attr, char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002609{
Tony Jonesee959b02008-02-22 00:13:36 +01002610 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002611
Bart Van Assche45c37ca2015-05-18 13:25:10 +02002612 return sprintf(buf, "0x%016llx\n", be64_to_cpu(target->service_id));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002613}
2614
Tony Jonesee959b02008-02-22 00:13:36 +01002615static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
2616 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002617{
Tony Jonesee959b02008-02-22 00:13:36 +01002618 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002619
Bart Van Assche747fe002014-10-30 14:48:05 +01002620 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->pkey));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002621}
2622
Bart Van Assche848b3082013-10-26 14:38:12 +02002623static ssize_t show_sgid(struct device *dev, struct device_attribute *attr,
2624 char *buf)
2625{
2626 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2627
Bart Van Assche747fe002014-10-30 14:48:05 +01002628 return sprintf(buf, "%pI6\n", target->sgid.raw);
Bart Van Assche848b3082013-10-26 14:38:12 +02002629}
2630
Tony Jonesee959b02008-02-22 00:13:36 +01002631static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
2632 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002633{
Tony Jonesee959b02008-02-22 00:13:36 +01002634 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Bart Van Assched92c0da2014-10-06 17:14:36 +02002635 struct srp_rdma_ch *ch = &target->ch[0];
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002636
Bart Van Assche509c07b2014-10-30 14:48:30 +01002637 return sprintf(buf, "%pI6\n", ch->path.dgid.raw);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002638}
2639
Tony Jonesee959b02008-02-22 00:13:36 +01002640static ssize_t show_orig_dgid(struct device *dev,
2641 struct device_attribute *attr, char *buf)
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002642{
Tony Jonesee959b02008-02-22 00:13:36 +01002643 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002644
Bart Van Assche747fe002014-10-30 14:48:05 +01002645 return sprintf(buf, "%pI6\n", target->orig_dgid.raw);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002646}
2647
Bart Van Assche89de7482010-08-03 14:08:45 +00002648static ssize_t show_req_lim(struct device *dev,
2649 struct device_attribute *attr, char *buf)
2650{
2651 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Bart Van Assched92c0da2014-10-06 17:14:36 +02002652 struct srp_rdma_ch *ch;
2653 int i, req_lim = INT_MAX;
Bart Van Assche89de7482010-08-03 14:08:45 +00002654
Bart Van Assched92c0da2014-10-06 17:14:36 +02002655 for (i = 0; i < target->ch_count; i++) {
2656 ch = &target->ch[i];
2657 req_lim = min(req_lim, ch->req_lim);
2658 }
2659 return sprintf(buf, "%d\n", req_lim);
Bart Van Assche89de7482010-08-03 14:08:45 +00002660}
2661
Tony Jonesee959b02008-02-22 00:13:36 +01002662static ssize_t show_zero_req_lim(struct device *dev,
2663 struct device_attribute *attr, char *buf)
Roland Dreier6bfa24f2006-06-17 20:37:33 -07002664{
Tony Jonesee959b02008-02-22 00:13:36 +01002665 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6bfa24f2006-06-17 20:37:33 -07002666
Roland Dreier6bfa24f2006-06-17 20:37:33 -07002667 return sprintf(buf, "%d\n", target->zero_req_lim);
2668}
2669
Tony Jonesee959b02008-02-22 00:13:36 +01002670static ssize_t show_local_ib_port(struct device *dev,
2671 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002672{
Tony Jonesee959b02008-02-22 00:13:36 +01002673 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002674
2675 return sprintf(buf, "%d\n", target->srp_host->port);
2676}
2677
Tony Jonesee959b02008-02-22 00:13:36 +01002678static ssize_t show_local_ib_device(struct device *dev,
2679 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002680{
Tony Jonesee959b02008-02-22 00:13:36 +01002681 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002682
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002683 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002684}
2685
Bart Van Assched92c0da2014-10-06 17:14:36 +02002686static ssize_t show_ch_count(struct device *dev, struct device_attribute *attr,
2687 char *buf)
2688{
2689 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2690
2691 return sprintf(buf, "%d\n", target->ch_count);
2692}
2693
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002694static ssize_t show_comp_vector(struct device *dev,
2695 struct device_attribute *attr, char *buf)
2696{
2697 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2698
2699 return sprintf(buf, "%d\n", target->comp_vector);
2700}
2701
Vu Pham7bb312e2013-10-26 14:31:27 +02002702static ssize_t show_tl_retry_count(struct device *dev,
2703 struct device_attribute *attr, char *buf)
2704{
2705 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2706
2707 return sprintf(buf, "%d\n", target->tl_retry_count);
2708}
2709
David Dillow49248642011-01-14 18:23:24 -05002710static ssize_t show_cmd_sg_entries(struct device *dev,
2711 struct device_attribute *attr, char *buf)
2712{
2713 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2714
2715 return sprintf(buf, "%u\n", target->cmd_sg_cnt);
2716}
2717
David Dillowc07d4242011-01-16 13:57:10 -05002718static ssize_t show_allow_ext_sg(struct device *dev,
2719 struct device_attribute *attr, char *buf)
2720{
2721 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2722
2723 return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false");
2724}
2725
Tony Jonesee959b02008-02-22 00:13:36 +01002726static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
2727static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
2728static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
2729static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
Bart Van Assche848b3082013-10-26 14:38:12 +02002730static DEVICE_ATTR(sgid, S_IRUGO, show_sgid, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01002731static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
2732static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
Bart Van Assche89de7482010-08-03 14:08:45 +00002733static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01002734static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
2735static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
2736static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Bart Van Assched92c0da2014-10-06 17:14:36 +02002737static DEVICE_ATTR(ch_count, S_IRUGO, show_ch_count, NULL);
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002738static DEVICE_ATTR(comp_vector, S_IRUGO, show_comp_vector, NULL);
Vu Pham7bb312e2013-10-26 14:31:27 +02002739static DEVICE_ATTR(tl_retry_count, S_IRUGO, show_tl_retry_count, NULL);
David Dillow49248642011-01-14 18:23:24 -05002740static DEVICE_ATTR(cmd_sg_entries, S_IRUGO, show_cmd_sg_entries, NULL);
David Dillowc07d4242011-01-16 13:57:10 -05002741static DEVICE_ATTR(allow_ext_sg, S_IRUGO, show_allow_ext_sg, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002742
Tony Jonesee959b02008-02-22 00:13:36 +01002743static struct device_attribute *srp_host_attrs[] = {
2744 &dev_attr_id_ext,
2745 &dev_attr_ioc_guid,
2746 &dev_attr_service_id,
2747 &dev_attr_pkey,
Bart Van Assche848b3082013-10-26 14:38:12 +02002748 &dev_attr_sgid,
Tony Jonesee959b02008-02-22 00:13:36 +01002749 &dev_attr_dgid,
2750 &dev_attr_orig_dgid,
Bart Van Assche89de7482010-08-03 14:08:45 +00002751 &dev_attr_req_lim,
Tony Jonesee959b02008-02-22 00:13:36 +01002752 &dev_attr_zero_req_lim,
2753 &dev_attr_local_ib_port,
2754 &dev_attr_local_ib_device,
Bart Van Assched92c0da2014-10-06 17:14:36 +02002755 &dev_attr_ch_count,
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002756 &dev_attr_comp_vector,
Vu Pham7bb312e2013-10-26 14:31:27 +02002757 &dev_attr_tl_retry_count,
David Dillow49248642011-01-14 18:23:24 -05002758 &dev_attr_cmd_sg_entries,
David Dillowc07d4242011-01-16 13:57:10 -05002759 &dev_attr_allow_ext_sg,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002760 NULL
2761};
2762
Roland Dreieraef9ec32005-11-02 14:07:13 -08002763static struct scsi_host_template srp_template = {
2764 .module = THIS_MODULE,
Roland Dreierb7f008f2007-05-06 21:18:11 -07002765 .name = "InfiniBand SRP initiator",
2766 .proc_name = DRV_NAME,
Bart Van Asschec9b03c12011-09-03 09:34:48 +02002767 .slave_configure = srp_slave_configure,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002768 .info = srp_target_info,
2769 .queuecommand = srp_queuecommand,
Jack Wang71444b92013-11-07 11:37:37 +01002770 .change_queue_depth = srp_change_queue_depth,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002771 .eh_abort_handler = srp_abort,
2772 .eh_device_reset_handler = srp_reset_device,
2773 .eh_host_reset_handler = srp_reset_host,
Bart Van Assche2742c1d2013-06-12 15:24:25 +02002774 .skip_settle_delay = true,
David Dillow49248642011-01-14 18:23:24 -05002775 .sg_tablesize = SRP_DEF_SG_TABLESIZE,
Bart Van Assche4d73f952013-10-26 14:40:37 +02002776 .can_queue = SRP_DEFAULT_CMD_SQ_SIZE,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002777 .this_id = -1,
Bart Van Assche4d73f952013-10-26 14:40:37 +02002778 .cmd_per_lun = SRP_DEFAULT_CMD_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002779 .use_clustering = ENABLE_CLUSTERING,
Bart Van Assche77f2c1a2014-10-02 15:29:25 +02002780 .shost_attrs = srp_host_attrs,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01002781 .track_queue_depth = 1,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002782};
2783
Bart Van Assche34aa6542014-10-30 14:47:22 +01002784static int srp_sdev_count(struct Scsi_Host *host)
2785{
2786 struct scsi_device *sdev;
2787 int c = 0;
2788
2789 shost_for_each_device(sdev, host)
2790 c++;
2791
2792 return c;
2793}
2794
Bart Van Asschebc44bd12015-08-14 11:01:09 -07002795/*
2796 * Return values:
2797 * < 0 upon failure. Caller is responsible for SRP target port cleanup.
2798 * 0 and target->state == SRP_TARGET_REMOVED if asynchronous target port
2799 * removal has been scheduled.
2800 * 0 and target->state != SRP_TARGET_REMOVED upon success.
2801 */
Roland Dreieraef9ec32005-11-02 14:07:13 -08002802static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
2803{
FUJITA Tomonori32368222007-06-27 16:33:12 +09002804 struct srp_rport_identifiers ids;
2805 struct srp_rport *rport;
2806
Bart Van Assche34aa6542014-10-30 14:47:22 +01002807 target->state = SRP_TARGET_SCANNING;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002808 sprintf(target->target_name, "SRP.T10:%016llX",
Bart Van Assche45c37ca2015-05-18 13:25:10 +02002809 be64_to_cpu(target->id_ext));
Roland Dreieraef9ec32005-11-02 14:07:13 -08002810
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002811 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002812 return -ENODEV;
2813
FUJITA Tomonori32368222007-06-27 16:33:12 +09002814 memcpy(ids.port_id, &target->id_ext, 8);
2815 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09002816 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori32368222007-06-27 16:33:12 +09002817 rport = srp_rport_add(target->scsi_host, &ids);
2818 if (IS_ERR(rport)) {
2819 scsi_remove_host(target->scsi_host);
2820 return PTR_ERR(rport);
2821 }
2822
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02002823 rport->lld_data = target;
Bart Van Assche9dd69a62013-10-26 14:32:30 +02002824 target->rport = rport;
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02002825
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002826 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002827 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002828 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002829
Roland Dreieraef9ec32005-11-02 14:07:13 -08002830 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07002831 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002832
Bart Van Asschec014c8c2015-05-18 13:23:57 +02002833 if (srp_connected_ch(target) < target->ch_count ||
2834 target->qp_in_error) {
Bart Van Assche34aa6542014-10-30 14:47:22 +01002835 shost_printk(KERN_INFO, target->scsi_host,
2836 PFX "SCSI scan failed - removing SCSI host\n");
2837 srp_queue_remove_work(target);
2838 goto out;
2839 }
2840
2841 pr_debug(PFX "%s: SCSI scan succeeded - detected %d LUNs\n",
2842 dev_name(&target->scsi_host->shost_gendev),
2843 srp_sdev_count(target->scsi_host));
2844
2845 spin_lock_irq(&target->lock);
2846 if (target->state == SRP_TARGET_SCANNING)
2847 target->state = SRP_TARGET_LIVE;
2848 spin_unlock_irq(&target->lock);
2849
2850out:
Roland Dreieraef9ec32005-11-02 14:07:13 -08002851 return 0;
2852}
2853
Tony Jonesee959b02008-02-22 00:13:36 +01002854static void srp_release_dev(struct device *dev)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002855{
2856 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01002857 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002858
2859 complete(&host->released);
2860}
2861
2862static struct class srp_class = {
2863 .name = "infiniband_srp",
Tony Jonesee959b02008-02-22 00:13:36 +01002864 .dev_release = srp_release_dev
Roland Dreieraef9ec32005-11-02 14:07:13 -08002865};
2866
Bart Van Assche96fc2482013-06-28 14:51:26 +02002867/**
2868 * srp_conn_unique() - check whether the connection to a target is unique
Bart Van Asscheaf246632014-05-20 15:04:21 +02002869 * @host: SRP host.
2870 * @target: SRP target port.
Bart Van Assche96fc2482013-06-28 14:51:26 +02002871 */
2872static bool srp_conn_unique(struct srp_host *host,
2873 struct srp_target_port *target)
2874{
2875 struct srp_target_port *t;
2876 bool ret = false;
2877
2878 if (target->state == SRP_TARGET_REMOVED)
2879 goto out;
2880
2881 ret = true;
2882
2883 spin_lock(&host->target_lock);
2884 list_for_each_entry(t, &host->target_list, list) {
2885 if (t != target &&
2886 target->id_ext == t->id_ext &&
2887 target->ioc_guid == t->ioc_guid &&
2888 target->initiator_ext == t->initiator_ext) {
2889 ret = false;
2890 break;
2891 }
2892 }
2893 spin_unlock(&host->target_lock);
2894
2895out:
2896 return ret;
2897}
2898
Roland Dreieraef9ec32005-11-02 14:07:13 -08002899/*
2900 * Target ports are added by writing
2901 *
2902 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
2903 * pkey=<P_Key>,service_id=<service ID>
2904 *
2905 * to the add_target sysfs attribute.
2906 */
2907enum {
2908 SRP_OPT_ERR = 0,
2909 SRP_OPT_ID_EXT = 1 << 0,
2910 SRP_OPT_IOC_GUID = 1 << 1,
2911 SRP_OPT_DGID = 1 << 2,
2912 SRP_OPT_PKEY = 1 << 3,
2913 SRP_OPT_SERVICE_ID = 1 << 4,
2914 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07002915 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07002916 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002917 SRP_OPT_INITIATOR_EXT = 1 << 8,
David Dillow49248642011-01-14 18:23:24 -05002918 SRP_OPT_CMD_SG_ENTRIES = 1 << 9,
David Dillowc07d4242011-01-16 13:57:10 -05002919 SRP_OPT_ALLOW_EXT_SG = 1 << 10,
2920 SRP_OPT_SG_TABLESIZE = 1 << 11,
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002921 SRP_OPT_COMP_VECTOR = 1 << 12,
Vu Pham7bb312e2013-10-26 14:31:27 +02002922 SRP_OPT_TL_RETRY_COUNT = 1 << 13,
Bart Van Assche4d73f952013-10-26 14:40:37 +02002923 SRP_OPT_QUEUE_SIZE = 1 << 14,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002924 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
2925 SRP_OPT_IOC_GUID |
2926 SRP_OPT_DGID |
2927 SRP_OPT_PKEY |
2928 SRP_OPT_SERVICE_ID),
2929};
2930
Steven Whitehousea447c092008-10-13 10:46:57 +01002931static const match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07002932 { SRP_OPT_ID_EXT, "id_ext=%s" },
2933 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
2934 { SRP_OPT_DGID, "dgid=%s" },
2935 { SRP_OPT_PKEY, "pkey=%x" },
2936 { SRP_OPT_SERVICE_ID, "service_id=%s" },
2937 { SRP_OPT_MAX_SECT, "max_sect=%d" },
2938 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07002939 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002940 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
David Dillow49248642011-01-14 18:23:24 -05002941 { SRP_OPT_CMD_SG_ENTRIES, "cmd_sg_entries=%u" },
David Dillowc07d4242011-01-16 13:57:10 -05002942 { SRP_OPT_ALLOW_EXT_SG, "allow_ext_sg=%u" },
2943 { SRP_OPT_SG_TABLESIZE, "sg_tablesize=%u" },
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002944 { SRP_OPT_COMP_VECTOR, "comp_vector=%u" },
Vu Pham7bb312e2013-10-26 14:31:27 +02002945 { SRP_OPT_TL_RETRY_COUNT, "tl_retry_count=%u" },
Bart Van Assche4d73f952013-10-26 14:40:37 +02002946 { SRP_OPT_QUEUE_SIZE, "queue_size=%d" },
Vu Pham52fb2b502006-06-17 20:37:31 -07002947 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002948};
2949
2950static int srp_parse_options(const char *buf, struct srp_target_port *target)
2951{
2952 char *options, *sep_opt;
2953 char *p;
2954 char dgid[3];
2955 substring_t args[MAX_OPT_ARGS];
2956 int opt_mask = 0;
2957 int token;
2958 int ret = -EINVAL;
2959 int i;
2960
2961 options = kstrdup(buf, GFP_KERNEL);
2962 if (!options)
2963 return -ENOMEM;
2964
2965 sep_opt = options;
Sagi Grimberg7dcf9c12014-10-19 18:19:02 +03002966 while ((p = strsep(&sep_opt, ",\n")) != NULL) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002967 if (!*p)
2968 continue;
2969
2970 token = match_token(p, srp_opt_tokens, args);
2971 opt_mask |= token;
2972
2973 switch (token) {
2974 case SRP_OPT_ID_EXT:
2975 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002976 if (!p) {
2977 ret = -ENOMEM;
2978 goto out;
2979 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002980 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2981 kfree(p);
2982 break;
2983
2984 case SRP_OPT_IOC_GUID:
2985 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002986 if (!p) {
2987 ret = -ENOMEM;
2988 goto out;
2989 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002990 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
2991 kfree(p);
2992 break;
2993
2994 case SRP_OPT_DGID:
2995 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002996 if (!p) {
2997 ret = -ENOMEM;
2998 goto out;
2999 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08003000 if (strlen(p) != 32) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003001 pr_warn("bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07003002 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003003 goto out;
3004 }
3005
3006 for (i = 0; i < 16; ++i) {
Bart Van Assche747fe002014-10-30 14:48:05 +01003007 strlcpy(dgid, p + i * 2, sizeof(dgid));
3008 if (sscanf(dgid, "%hhx",
3009 &target->orig_dgid.raw[i]) < 1) {
3010 ret = -EINVAL;
3011 kfree(p);
3012 goto out;
3013 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08003014 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08003015 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003016 break;
3017
3018 case SRP_OPT_PKEY:
3019 if (match_hex(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003020 pr_warn("bad P_Key parameter '%s'\n", p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003021 goto out;
3022 }
Bart Van Assche747fe002014-10-30 14:48:05 +01003023 target->pkey = cpu_to_be16(token);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003024 break;
3025
3026 case SRP_OPT_SERVICE_ID:
3027 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02003028 if (!p) {
3029 ret = -ENOMEM;
3030 goto out;
3031 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08003032 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
3033 kfree(p);
3034 break;
3035
3036 case SRP_OPT_MAX_SECT:
3037 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003038 pr_warn("bad max sect parameter '%s'\n", p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003039 goto out;
3040 }
3041 target->scsi_host->max_sectors = token;
3042 break;
3043
Bart Van Assche4d73f952013-10-26 14:40:37 +02003044 case SRP_OPT_QUEUE_SIZE:
3045 if (match_int(args, &token) || token < 1) {
3046 pr_warn("bad queue_size parameter '%s'\n", p);
3047 goto out;
3048 }
3049 target->scsi_host->can_queue = token;
3050 target->queue_size = token + SRP_RSP_SQ_SIZE +
3051 SRP_TSK_MGMT_SQ_SIZE;
3052 if (!(opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
3053 target->scsi_host->cmd_per_lun = token;
3054 break;
3055
Vu Pham52fb2b502006-06-17 20:37:31 -07003056 case SRP_OPT_MAX_CMD_PER_LUN:
Bart Van Assche4d73f952013-10-26 14:40:37 +02003057 if (match_int(args, &token) || token < 1) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003058 pr_warn("bad max cmd_per_lun parameter '%s'\n",
3059 p);
Vu Pham52fb2b502006-06-17 20:37:31 -07003060 goto out;
3061 }
Bart Van Assche4d73f952013-10-26 14:40:37 +02003062 target->scsi_host->cmd_per_lun = token;
Vu Pham52fb2b502006-06-17 20:37:31 -07003063 break;
3064
Ramachandra K0c0450db2006-06-17 20:37:38 -07003065 case SRP_OPT_IO_CLASS:
3066 if (match_hex(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003067 pr_warn("bad IO class parameter '%s'\n", p);
Ramachandra K0c0450db2006-06-17 20:37:38 -07003068 goto out;
3069 }
3070 if (token != SRP_REV10_IB_IO_CLASS &&
3071 token != SRP_REV16A_IB_IO_CLASS) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003072 pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n",
3073 token, SRP_REV10_IB_IO_CLASS,
3074 SRP_REV16A_IB_IO_CLASS);
Ramachandra K0c0450db2006-06-17 20:37:38 -07003075 goto out;
3076 }
3077 target->io_class = token;
3078 break;
3079
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02003080 case SRP_OPT_INITIATOR_EXT:
3081 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02003082 if (!p) {
3083 ret = -ENOMEM;
3084 goto out;
3085 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02003086 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
3087 kfree(p);
3088 break;
3089
David Dillow49248642011-01-14 18:23:24 -05003090 case SRP_OPT_CMD_SG_ENTRIES:
3091 if (match_int(args, &token) || token < 1 || token > 255) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003092 pr_warn("bad max cmd_sg_entries parameter '%s'\n",
3093 p);
David Dillow49248642011-01-14 18:23:24 -05003094 goto out;
3095 }
3096 target->cmd_sg_cnt = token;
3097 break;
3098
David Dillowc07d4242011-01-16 13:57:10 -05003099 case SRP_OPT_ALLOW_EXT_SG:
3100 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003101 pr_warn("bad allow_ext_sg parameter '%s'\n", p);
David Dillowc07d4242011-01-16 13:57:10 -05003102 goto out;
3103 }
3104 target->allow_ext_sg = !!token;
3105 break;
3106
3107 case SRP_OPT_SG_TABLESIZE:
3108 if (match_int(args, &token) || token < 1 ||
3109 token > SCSI_MAX_SG_CHAIN_SEGMENTS) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003110 pr_warn("bad max sg_tablesize parameter '%s'\n",
3111 p);
David Dillowc07d4242011-01-16 13:57:10 -05003112 goto out;
3113 }
3114 target->sg_tablesize = token;
3115 break;
3116
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02003117 case SRP_OPT_COMP_VECTOR:
3118 if (match_int(args, &token) || token < 0) {
3119 pr_warn("bad comp_vector parameter '%s'\n", p);
3120 goto out;
3121 }
3122 target->comp_vector = token;
3123 break;
3124
Vu Pham7bb312e2013-10-26 14:31:27 +02003125 case SRP_OPT_TL_RETRY_COUNT:
3126 if (match_int(args, &token) || token < 2 || token > 7) {
3127 pr_warn("bad tl_retry_count parameter '%s' (must be a number between 2 and 7)\n",
3128 p);
3129 goto out;
3130 }
3131 target->tl_retry_count = token;
3132 break;
3133
Roland Dreieraef9ec32005-11-02 14:07:13 -08003134 default:
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003135 pr_warn("unknown parameter or missing value '%s' in target creation request\n",
3136 p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003137 goto out;
3138 }
3139 }
3140
3141 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
3142 ret = 0;
3143 else
3144 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
3145 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
3146 !(srp_opt_tokens[i].token & opt_mask))
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003147 pr_warn("target creation request is missing parameter '%s'\n",
3148 srp_opt_tokens[i].pattern);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003149
Bart Van Assche4d73f952013-10-26 14:40:37 +02003150 if (target->scsi_host->cmd_per_lun > target->scsi_host->can_queue
3151 && (opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
3152 pr_warn("cmd_per_lun = %d > queue_size = %d\n",
3153 target->scsi_host->cmd_per_lun,
3154 target->scsi_host->can_queue);
3155
Roland Dreieraef9ec32005-11-02 14:07:13 -08003156out:
3157 kfree(options);
3158 return ret;
3159}
3160
Tony Jonesee959b02008-02-22 00:13:36 +01003161static ssize_t srp_create_target(struct device *dev,
3162 struct device_attribute *attr,
Roland Dreieraef9ec32005-11-02 14:07:13 -08003163 const char *buf, size_t count)
3164{
3165 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01003166 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003167 struct Scsi_Host *target_host;
3168 struct srp_target_port *target;
Bart Van Assche509c07b2014-10-30 14:48:30 +01003169 struct srp_rdma_ch *ch;
Bart Van Assched1b42892014-05-20 15:07:20 +02003170 struct srp_device *srp_dev = host->srp_dev;
3171 struct ib_device *ibdev = srp_dev->dev;
Bart Van Assched92c0da2014-10-06 17:14:36 +02003172 int ret, node_idx, node, cpu, i;
3173 bool multich = false;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003174
3175 target_host = scsi_host_alloc(&srp_template,
3176 sizeof (struct srp_target_port));
3177 if (!target_host)
3178 return -ENOMEM;
3179
David Dillow49248642011-01-14 18:23:24 -05003180 target_host->transportt = ib_srp_transport_template;
Bart Van Asschefd1b6c42011-07-13 09:19:16 -07003181 target_host->max_channel = 0;
3182 target_host->max_id = 1;
Bart Van Assche985aa492015-05-18 13:27:14 +02003183 target_host->max_lun = -1LL;
Arne Redlich3c8edf02006-11-15 12:43:00 +01003184 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08003185
Roland Dreieraef9ec32005-11-02 14:07:13 -08003186 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003187
David Dillow49248642011-01-14 18:23:24 -05003188 target->io_class = SRP_REV16A_IB_IO_CLASS;
3189 target->scsi_host = target_host;
3190 target->srp_host = host;
Jason Gunthorpee6bf5f482015-07-30 17:22:22 -06003191 target->lkey = host->srp_dev->pd->local_dma_lkey;
Bart Van Assche03f6fb92015-08-10 17:09:36 -07003192 target->global_mr = host->srp_dev->global_mr;
David Dillow49248642011-01-14 18:23:24 -05003193 target->cmd_sg_cnt = cmd_sg_entries;
David Dillowc07d4242011-01-16 13:57:10 -05003194 target->sg_tablesize = indirect_sg_entries ? : cmd_sg_entries;
3195 target->allow_ext_sg = allow_ext_sg;
Vu Pham7bb312e2013-10-26 14:31:27 +02003196 target->tl_retry_count = 7;
Bart Van Assche4d73f952013-10-26 14:40:37 +02003197 target->queue_size = SRP_DEFAULT_QUEUE_SIZE;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003198
Bart Van Assche34aa6542014-10-30 14:47:22 +01003199 /*
3200 * Avoid that the SCSI host can be removed by srp_remove_target()
3201 * before this function returns.
3202 */
3203 scsi_host_get(target->scsi_host);
3204
Bart Van Assche2d7091b2014-03-14 13:52:45 +01003205 mutex_lock(&host->add_target_mutex);
3206
Roland Dreieraef9ec32005-11-02 14:07:13 -08003207 ret = srp_parse_options(buf, target);
3208 if (ret)
Bart Van Asschefb49c8b2015-05-18 13:23:14 +02003209 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003210
Bart Van Assche4d73f952013-10-26 14:40:37 +02003211 target->req_ring_size = target->queue_size - SRP_TSK_MGMT_SQ_SIZE;
3212
Bart Van Assche96fc2482013-06-28 14:51:26 +02003213 if (!srp_conn_unique(target->srp_host, target)) {
3214 shost_printk(KERN_INFO, target->scsi_host,
3215 PFX "Already connected to target port with id_ext=%016llx;ioc_guid=%016llx;initiator_ext=%016llx\n",
3216 be64_to_cpu(target->id_ext),
3217 be64_to_cpu(target->ioc_guid),
3218 be64_to_cpu(target->initiator_ext));
3219 ret = -EEXIST;
Bart Van Asschefb49c8b2015-05-18 13:23:14 +02003220 goto out;
Bart Van Assche96fc2482013-06-28 14:51:26 +02003221 }
3222
Bart Van Assche5cfb1782014-05-20 15:08:34 +02003223 if (!srp_dev->has_fmr && !srp_dev->has_fr && !target->allow_ext_sg &&
Bart Van Assched1b42892014-05-20 15:07:20 +02003224 target->cmd_sg_cnt < target->sg_tablesize) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +02003225 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 -05003226 target->sg_tablesize = target->cmd_sg_cnt;
3227 }
3228
3229 target_host->sg_tablesize = target->sg_tablesize;
3230 target->indirect_size = target->sg_tablesize *
3231 sizeof (struct srp_direct_buf);
David Dillow49248642011-01-14 18:23:24 -05003232 target->max_iu_len = sizeof (struct srp_cmd) +
3233 sizeof (struct srp_indirect_buf) +
3234 target->cmd_sg_cnt * sizeof (struct srp_direct_buf);
3235
Bart Van Asschec1120f82013-10-26 14:35:08 +02003236 INIT_WORK(&target->tl_err_work, srp_tl_err_work);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00003237 INIT_WORK(&target->remove_work, srp_remove_work);
David Dillow8f26c9f2011-01-14 19:45:50 -05003238 spin_lock_init(&target->lock);
Matan Barak55ee3ab2015-10-15 18:38:45 +03003239 ret = ib_query_gid(ibdev, host->port, 0, &target->sgid, NULL);
Sagi Grimberg2088ca62014-03-14 13:51:58 +01003240 if (ret)
Bart Van Asschefb49c8b2015-05-18 13:23:14 +02003241 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003242
Bart Van Assched92c0da2014-10-06 17:14:36 +02003243 ret = -ENOMEM;
3244 target->ch_count = max_t(unsigned, num_online_nodes(),
3245 min(ch_count ? :
3246 min(4 * num_online_nodes(),
3247 ibdev->num_comp_vectors),
3248 num_online_cpus()));
3249 target->ch = kcalloc(target->ch_count, sizeof(*target->ch),
3250 GFP_KERNEL);
3251 if (!target->ch)
Bart Van Asschefb49c8b2015-05-18 13:23:14 +02003252 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003253
Bart Van Assched92c0da2014-10-06 17:14:36 +02003254 node_idx = 0;
3255 for_each_online_node(node) {
3256 const int ch_start = (node_idx * target->ch_count /
3257 num_online_nodes());
3258 const int ch_end = ((node_idx + 1) * target->ch_count /
3259 num_online_nodes());
3260 const int cv_start = (node_idx * ibdev->num_comp_vectors /
3261 num_online_nodes() + target->comp_vector)
3262 % ibdev->num_comp_vectors;
3263 const int cv_end = ((node_idx + 1) * ibdev->num_comp_vectors /
3264 num_online_nodes() + target->comp_vector)
3265 % ibdev->num_comp_vectors;
3266 int cpu_idx = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003267
Bart Van Assched92c0da2014-10-06 17:14:36 +02003268 for_each_online_cpu(cpu) {
3269 if (cpu_to_node(cpu) != node)
3270 continue;
3271 if (ch_start + cpu_idx >= ch_end)
3272 continue;
3273 ch = &target->ch[ch_start + cpu_idx];
3274 ch->target = target;
3275 ch->comp_vector = cv_start == cv_end ? cv_start :
3276 cv_start + cpu_idx % (cv_end - cv_start);
3277 spin_lock_init(&ch->lock);
3278 INIT_LIST_HEAD(&ch->free_tx);
3279 ret = srp_new_cm_id(ch);
3280 if (ret)
3281 goto err_disconnect;
3282
3283 ret = srp_create_ch_ib(ch);
3284 if (ret)
3285 goto err_disconnect;
3286
3287 ret = srp_alloc_req_data(ch);
3288 if (ret)
3289 goto err_disconnect;
3290
3291 ret = srp_connect_ch(ch, multich);
3292 if (ret) {
3293 shost_printk(KERN_ERR, target->scsi_host,
3294 PFX "Connection %d/%d failed\n",
3295 ch_start + cpu_idx,
3296 target->ch_count);
3297 if (node_idx == 0 && cpu_idx == 0) {
3298 goto err_disconnect;
3299 } else {
3300 srp_free_ch_ib(target, ch);
3301 srp_free_req_data(target, ch);
3302 target->ch_count = ch - target->ch;
Bart Van Asschec257ea62015-07-31 14:13:22 -07003303 goto connected;
Bart Van Assched92c0da2014-10-06 17:14:36 +02003304 }
3305 }
3306
3307 multich = true;
3308 cpu_idx++;
3309 }
3310 node_idx++;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003311 }
3312
Bart Van Asschec257ea62015-07-31 14:13:22 -07003313connected:
Bart Van Assched92c0da2014-10-06 17:14:36 +02003314 target->scsi_host->nr_hw_queues = target->ch_count;
3315
Roland Dreieraef9ec32005-11-02 14:07:13 -08003316 ret = srp_add_target(host, target);
3317 if (ret)
3318 goto err_disconnect;
3319
Bart Van Assche34aa6542014-10-30 14:47:22 +01003320 if (target->state != SRP_TARGET_REMOVED) {
3321 shost_printk(KERN_DEBUG, target->scsi_host, PFX
3322 "new target: id_ext %016llx ioc_guid %016llx pkey %04x service_id %016llx sgid %pI6 dgid %pI6\n",
3323 be64_to_cpu(target->id_ext),
3324 be64_to_cpu(target->ioc_guid),
Bart Van Assche747fe002014-10-30 14:48:05 +01003325 be16_to_cpu(target->pkey),
Bart Van Assche34aa6542014-10-30 14:47:22 +01003326 be64_to_cpu(target->service_id),
Bart Van Assche747fe002014-10-30 14:48:05 +01003327 target->sgid.raw, target->orig_dgid.raw);
Bart Van Assche34aa6542014-10-30 14:47:22 +01003328 }
Bart Van Asschee7ffde02014-03-14 13:52:21 +01003329
Bart Van Assche2d7091b2014-03-14 13:52:45 +01003330 ret = count;
3331
3332out:
3333 mutex_unlock(&host->add_target_mutex);
Bart Van Assche34aa6542014-10-30 14:47:22 +01003334
3335 scsi_host_put(target->scsi_host);
Bart Van Asschebc44bd12015-08-14 11:01:09 -07003336 if (ret < 0)
3337 scsi_host_put(target->scsi_host);
Bart Van Assche34aa6542014-10-30 14:47:22 +01003338
Bart Van Assche2d7091b2014-03-14 13:52:45 +01003339 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003340
3341err_disconnect:
3342 srp_disconnect_target(target);
3343
Bart Van Assched92c0da2014-10-06 17:14:36 +02003344 for (i = 0; i < target->ch_count; i++) {
3345 ch = &target->ch[i];
3346 srp_free_ch_ib(target, ch);
3347 srp_free_req_data(target, ch);
3348 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08003349
Bart Van Assched92c0da2014-10-06 17:14:36 +02003350 kfree(target->ch);
Bart Van Assche2d7091b2014-03-14 13:52:45 +01003351 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003352}
3353
Tony Jonesee959b02008-02-22 00:13:36 +01003354static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003355
Tony Jonesee959b02008-02-22 00:13:36 +01003356static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
3357 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08003358{
Tony Jonesee959b02008-02-22 00:13:36 +01003359 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003360
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01003361 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003362}
3363
Tony Jonesee959b02008-02-22 00:13:36 +01003364static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003365
Tony Jonesee959b02008-02-22 00:13:36 +01003366static ssize_t show_port(struct device *dev, struct device_attribute *attr,
3367 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08003368{
Tony Jonesee959b02008-02-22 00:13:36 +01003369 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003370
3371 return sprintf(buf, "%d\n", host->port);
3372}
3373
Tony Jonesee959b02008-02-22 00:13:36 +01003374static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003375
Roland Dreierf5358a12006-06-17 20:37:29 -07003376static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08003377{
3378 struct srp_host *host;
3379
3380 host = kzalloc(sizeof *host, GFP_KERNEL);
3381 if (!host)
3382 return NULL;
3383
3384 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07003385 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003386 init_completion(&host->released);
Bart Van Assche2d7091b2014-03-14 13:52:45 +01003387 mutex_init(&host->add_target_mutex);
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01003388 host->srp_dev = device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003389 host->port = port;
3390
Tony Jonesee959b02008-02-22 00:13:36 +01003391 host->dev.class = &srp_class;
3392 host->dev.parent = device->dev->dma_device;
Kay Sieversd927e382009-01-06 10:44:39 -08003393 dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003394
Tony Jonesee959b02008-02-22 00:13:36 +01003395 if (device_register(&host->dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07003396 goto free_host;
Tony Jonesee959b02008-02-22 00:13:36 +01003397 if (device_create_file(&host->dev, &dev_attr_add_target))
Roland Dreieraef9ec32005-11-02 14:07:13 -08003398 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01003399 if (device_create_file(&host->dev, &dev_attr_ibdev))
Roland Dreieraef9ec32005-11-02 14:07:13 -08003400 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01003401 if (device_create_file(&host->dev, &dev_attr_port))
Roland Dreieraef9ec32005-11-02 14:07:13 -08003402 goto err_class;
3403
3404 return host;
3405
3406err_class:
Tony Jonesee959b02008-02-22 00:13:36 +01003407 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003408
Roland Dreierf5358a12006-06-17 20:37:29 -07003409free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08003410 kfree(host);
3411
3412 return NULL;
3413}
3414
3415static void srp_add_one(struct ib_device *device)
3416{
Roland Dreierf5358a12006-06-17 20:37:29 -07003417 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003418 struct srp_host *host;
Hal Rosenstock41390322015-06-29 09:57:00 -04003419 int mr_page_shift, p;
Bart Van Assche52ede082014-05-20 15:07:45 +02003420 u64 max_pages_per_mr;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003421
Roland Dreierf5358a12006-06-17 20:37:29 -07003422 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
3423 if (!srp_dev)
Or Gerlitz4a061b22015-12-18 10:59:46 +02003424 return;
Roland Dreierf5358a12006-06-17 20:37:29 -07003425
Bart Van Assched1b42892014-05-20 15:07:20 +02003426 srp_dev->has_fmr = (device->alloc_fmr && device->dealloc_fmr &&
3427 device->map_phys_fmr && device->unmap_fmr);
Or Gerlitz4a061b22015-12-18 10:59:46 +02003428 srp_dev->has_fr = (device->attrs.device_cap_flags &
Bart Van Assche5cfb1782014-05-20 15:08:34 +02003429 IB_DEVICE_MEM_MGT_EXTENSIONS);
3430 if (!srp_dev->has_fmr && !srp_dev->has_fr)
3431 dev_warn(&device->dev, "neither FMR nor FR is supported\n");
3432
3433 srp_dev->use_fast_reg = (srp_dev->has_fr &&
3434 (!srp_dev->has_fmr || prefer_fr));
Bart Van Assche002f1562015-08-10 17:08:44 -07003435 srp_dev->use_fmr = !srp_dev->use_fast_reg && srp_dev->has_fmr;
Bart Van Assched1b42892014-05-20 15:07:20 +02003436
Roland Dreierf5358a12006-06-17 20:37:29 -07003437 /*
3438 * Use the smallest page size supported by the HCA, down to a
David Dillow8f26c9f2011-01-14 19:45:50 -05003439 * minimum of 4096 bytes. We're unlikely to build large sglists
3440 * out of smaller entries.
Roland Dreierf5358a12006-06-17 20:37:29 -07003441 */
Or Gerlitz4a061b22015-12-18 10:59:46 +02003442 mr_page_shift = max(12, ffs(device->attrs.page_size_cap) - 1);
Bart Van Assche52ede082014-05-20 15:07:45 +02003443 srp_dev->mr_page_size = 1 << mr_page_shift;
3444 srp_dev->mr_page_mask = ~((u64) srp_dev->mr_page_size - 1);
Or Gerlitz4a061b22015-12-18 10:59:46 +02003445 max_pages_per_mr = device->attrs.max_mr_size;
Bart Van Assche52ede082014-05-20 15:07:45 +02003446 do_div(max_pages_per_mr, srp_dev->mr_page_size);
3447 srp_dev->max_pages_per_mr = min_t(u64, SRP_MAX_PAGES_PER_MR,
3448 max_pages_per_mr);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02003449 if (srp_dev->use_fast_reg) {
3450 srp_dev->max_pages_per_mr =
3451 min_t(u32, srp_dev->max_pages_per_mr,
Or Gerlitz4a061b22015-12-18 10:59:46 +02003452 device->attrs.max_fast_reg_page_list_len);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02003453 }
Bart Van Assche52ede082014-05-20 15:07:45 +02003454 srp_dev->mr_max_size = srp_dev->mr_page_size *
3455 srp_dev->max_pages_per_mr;
Or Gerlitz4a061b22015-12-18 10:59:46 +02003456 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",
3457 device->name, mr_page_shift, device->attrs.max_mr_size,
3458 device->attrs.max_fast_reg_page_list_len,
Bart Van Assche52ede082014-05-20 15:07:45 +02003459 srp_dev->max_pages_per_mr, srp_dev->mr_max_size);
Roland Dreierf5358a12006-06-17 20:37:29 -07003460
3461 INIT_LIST_HEAD(&srp_dev->dev_list);
3462
3463 srp_dev->dev = device;
3464 srp_dev->pd = ib_alloc_pd(device);
3465 if (IS_ERR(srp_dev->pd))
3466 goto free_dev;
3467
Bart Van Assche03f6fb92015-08-10 17:09:36 -07003468 if (!register_always || (!srp_dev->has_fmr && !srp_dev->has_fr)) {
3469 srp_dev->global_mr = ib_get_dma_mr(srp_dev->pd,
3470 IB_ACCESS_LOCAL_WRITE |
3471 IB_ACCESS_REMOTE_READ |
3472 IB_ACCESS_REMOTE_WRITE);
3473 if (IS_ERR(srp_dev->global_mr))
3474 goto err_pd;
3475 } else {
3476 srp_dev->global_mr = NULL;
3477 }
Roland Dreierf5358a12006-06-17 20:37:29 -07003478
Hal Rosenstock41390322015-06-29 09:57:00 -04003479 for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07003480 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003481 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07003482 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003483 }
3484
Roland Dreierf5358a12006-06-17 20:37:29 -07003485 ib_set_client_data(device, &srp_client, srp_dev);
Or Gerlitz4a061b22015-12-18 10:59:46 +02003486 return;
Roland Dreierf5358a12006-06-17 20:37:29 -07003487
3488err_pd:
3489 ib_dealloc_pd(srp_dev->pd);
3490
3491free_dev:
3492 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003493}
3494
Haggai Eran7c1eb452015-07-30 17:50:14 +03003495static void srp_remove_one(struct ib_device *device, void *client_data)
Roland Dreieraef9ec32005-11-02 14:07:13 -08003496{
Roland Dreierf5358a12006-06-17 20:37:29 -07003497 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003498 struct srp_host *host, *tmp_host;
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00003499 struct srp_target_port *target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003500
Haggai Eran7c1eb452015-07-30 17:50:14 +03003501 srp_dev = client_data;
Dotan Barak1fe0cb82013-06-12 15:20:36 +02003502 if (!srp_dev)
3503 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003504
Roland Dreierf5358a12006-06-17 20:37:29 -07003505 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Tony Jonesee959b02008-02-22 00:13:36 +01003506 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003507 /*
3508 * Wait for the sysfs entry to go away, so that no new
3509 * target ports can be created.
3510 */
3511 wait_for_completion(&host->released);
3512
3513 /*
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00003514 * Remove all target ports.
Roland Dreieraef9ec32005-11-02 14:07:13 -08003515 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07003516 spin_lock(&host->target_lock);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00003517 list_for_each_entry(target, &host->target_list, list)
3518 srp_queue_remove_work(target);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07003519 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003520
3521 /*
Bart Van Asschebcc05912014-07-09 15:57:26 +02003522 * Wait for tl_err and target port removal tasks.
Roland Dreieraef9ec32005-11-02 14:07:13 -08003523 */
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00003524 flush_workqueue(system_long_wq);
Bart Van Asschebcc05912014-07-09 15:57:26 +02003525 flush_workqueue(srp_remove_wq);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003526
Roland Dreieraef9ec32005-11-02 14:07:13 -08003527 kfree(host);
3528 }
3529
Bart Van Assche03f6fb92015-08-10 17:09:36 -07003530 if (srp_dev->global_mr)
3531 ib_dereg_mr(srp_dev->global_mr);
Roland Dreierf5358a12006-06-17 20:37:29 -07003532 ib_dealloc_pd(srp_dev->pd);
3533
3534 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003535}
3536
FUJITA Tomonori32368222007-06-27 16:33:12 +09003537static struct srp_function_template ib_srp_transport_functions = {
Bart Van Asscheed9b2262013-10-26 14:34:27 +02003538 .has_rport_state = true,
3539 .reset_timer_if_blocked = true,
Bart Van Asschea95cadb2013-10-26 14:37:17 +02003540 .reconnect_delay = &srp_reconnect_delay,
Bart Van Asscheed9b2262013-10-26 14:34:27 +02003541 .fast_io_fail_tmo = &srp_fast_io_fail_tmo,
3542 .dev_loss_tmo = &srp_dev_loss_tmo,
3543 .reconnect = srp_rport_reconnect,
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02003544 .rport_delete = srp_rport_delete,
Bart Van Asscheed9b2262013-10-26 14:34:27 +02003545 .terminate_rport_io = srp_terminate_io,
FUJITA Tomonori32368222007-06-27 16:33:12 +09003546};
3547
Roland Dreieraef9ec32005-11-02 14:07:13 -08003548static int __init srp_init_module(void)
3549{
3550 int ret;
3551
David Dillow49248642011-01-14 18:23:24 -05003552 if (srp_sg_tablesize) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003553 pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n");
David Dillow49248642011-01-14 18:23:24 -05003554 if (!cmd_sg_entries)
3555 cmd_sg_entries = srp_sg_tablesize;
3556 }
3557
3558 if (!cmd_sg_entries)
3559 cmd_sg_entries = SRP_DEF_SG_TABLESIZE;
3560
3561 if (cmd_sg_entries > 255) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003562 pr_warn("Clamping cmd_sg_entries to 255\n");
David Dillow49248642011-01-14 18:23:24 -05003563 cmd_sg_entries = 255;
David Dillow1e89a192008-04-16 21:01:12 -07003564 }
3565
David Dillowc07d4242011-01-16 13:57:10 -05003566 if (!indirect_sg_entries)
3567 indirect_sg_entries = cmd_sg_entries;
3568 else if (indirect_sg_entries < cmd_sg_entries) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003569 pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n",
3570 cmd_sg_entries);
David Dillowc07d4242011-01-16 13:57:10 -05003571 indirect_sg_entries = cmd_sg_entries;
3572 }
3573
Bart Van Asschebcc05912014-07-09 15:57:26 +02003574 srp_remove_wq = create_workqueue("srp_remove");
Wei Yongjunda05be22014-08-14 08:56:22 +08003575 if (!srp_remove_wq) {
3576 ret = -ENOMEM;
Bart Van Asschebcc05912014-07-09 15:57:26 +02003577 goto out;
3578 }
3579
3580 ret = -ENOMEM;
FUJITA Tomonori32368222007-06-27 16:33:12 +09003581 ib_srp_transport_template =
3582 srp_attach_transport(&ib_srp_transport_functions);
3583 if (!ib_srp_transport_template)
Bart Van Asschebcc05912014-07-09 15:57:26 +02003584 goto destroy_wq;
FUJITA Tomonori32368222007-06-27 16:33:12 +09003585
Roland Dreieraef9ec32005-11-02 14:07:13 -08003586 ret = class_register(&srp_class);
3587 if (ret) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003588 pr_err("couldn't register class infiniband_srp\n");
Bart Van Asschebcc05912014-07-09 15:57:26 +02003589 goto release_tr;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003590 }
3591
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003592 ib_sa_register_client(&srp_sa_client);
3593
Roland Dreieraef9ec32005-11-02 14:07:13 -08003594 ret = ib_register_client(&srp_client);
3595 if (ret) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003596 pr_err("couldn't register IB client\n");
Bart Van Asschebcc05912014-07-09 15:57:26 +02003597 goto unreg_sa;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003598 }
3599
Bart Van Asschebcc05912014-07-09 15:57:26 +02003600out:
3601 return ret;
3602
3603unreg_sa:
3604 ib_sa_unregister_client(&srp_sa_client);
3605 class_unregister(&srp_class);
3606
3607release_tr:
3608 srp_release_transport(ib_srp_transport_template);
3609
3610destroy_wq:
3611 destroy_workqueue(srp_remove_wq);
3612 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003613}
3614
3615static void __exit srp_cleanup_module(void)
3616{
3617 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003618 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003619 class_unregister(&srp_class);
FUJITA Tomonori32368222007-06-27 16:33:12 +09003620 srp_release_transport(ib_srp_transport_template);
Bart Van Asschebcc05912014-07-09 15:57:26 +02003621 destroy_workqueue(srp_remove_wq);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003622}
3623
3624module_init(srp_init_module);
3625module_exit(srp_cleanup_module);