blob: e07a04a91273d15e6fd82aa89f6548e47b727690 [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>
Roland Dreieraef9ec32005-11-02 14:07:13 -080043
Arun Sharma600634972011-07-26 16:09:06 -070044#include <linux/atomic.h>
Roland Dreieraef9ec32005-11-02 14:07:13 -080045
46#include <scsi/scsi.h>
47#include <scsi/scsi_device.h>
48#include <scsi/scsi_dbg.h>
Jack Wang71444b92013-11-07 11:37:37 +010049#include <scsi/scsi_tcq.h>
Roland Dreieraef9ec32005-11-02 14:07:13 -080050#include <scsi/srp.h>
FUJITA Tomonori32368222007-06-27 16:33:12 +090051#include <scsi/scsi_transport_srp.h>
Roland Dreieraef9ec32005-11-02 14:07:13 -080052
Roland Dreieraef9ec32005-11-02 14:07:13 -080053#include "ib_srp.h"
54
55#define DRV_NAME "ib_srp"
56#define PFX DRV_NAME ": "
Vu Phame8ca4132013-06-28 14:59:08 +020057#define DRV_VERSION "1.0"
58#define DRV_RELDATE "July 1, 2013"
Roland Dreieraef9ec32005-11-02 14:07:13 -080059
60MODULE_AUTHOR("Roland Dreier");
61MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
62 "v" DRV_VERSION " (" DRV_RELDATE ")");
63MODULE_LICENSE("Dual BSD/GPL");
64
David Dillow49248642011-01-14 18:23:24 -050065static unsigned int srp_sg_tablesize;
66static unsigned int cmd_sg_entries;
David Dillowc07d4242011-01-16 13:57:10 -050067static unsigned int indirect_sg_entries;
68static bool allow_ext_sg;
Bart Van Assche5cfb1782014-05-20 15:08:34 +020069static bool prefer_fr;
Bart Van Asscheb1b88542014-05-20 15:06:41 +020070static bool register_always;
Roland Dreieraef9ec32005-11-02 14:07:13 -080071static int topspin_workarounds = 1;
72
David Dillow49248642011-01-14 18:23:24 -050073module_param(srp_sg_tablesize, uint, 0444);
74MODULE_PARM_DESC(srp_sg_tablesize, "Deprecated name for cmd_sg_entries");
75
76module_param(cmd_sg_entries, uint, 0444);
77MODULE_PARM_DESC(cmd_sg_entries,
78 "Default number of gather/scatter entries in the SRP command (default is 12, max 255)");
79
David Dillowc07d4242011-01-16 13:57:10 -050080module_param(indirect_sg_entries, uint, 0444);
81MODULE_PARM_DESC(indirect_sg_entries,
82 "Default max number of gather/scatter entries (default is 12, max is " __stringify(SCSI_MAX_SG_CHAIN_SEGMENTS) ")");
83
84module_param(allow_ext_sg, bool, 0444);
85MODULE_PARM_DESC(allow_ext_sg,
86 "Default behavior when there are more than cmd_sg_entries S/G entries after mapping; fails the request when false (default false)");
87
Roland Dreieraef9ec32005-11-02 14:07:13 -080088module_param(topspin_workarounds, int, 0444);
89MODULE_PARM_DESC(topspin_workarounds,
90 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
91
Bart Van Assche5cfb1782014-05-20 15:08:34 +020092module_param(prefer_fr, bool, 0444);
93MODULE_PARM_DESC(prefer_fr,
94"Whether to use fast registration if both FMR and fast registration are supported");
95
Bart Van Asscheb1b88542014-05-20 15:06:41 +020096module_param(register_always, bool, 0444);
97MODULE_PARM_DESC(register_always,
98 "Use memory registration even for contiguous memory regions");
99
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200100static struct kernel_param_ops srp_tmo_ops;
101
Bart Van Asschea95cadb2013-10-26 14:37:17 +0200102static int srp_reconnect_delay = 10;
103module_param_cb(reconnect_delay, &srp_tmo_ops, &srp_reconnect_delay,
104 S_IRUGO | S_IWUSR);
105MODULE_PARM_DESC(reconnect_delay, "Time between successive reconnect attempts");
106
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200107static int srp_fast_io_fail_tmo = 15;
108module_param_cb(fast_io_fail_tmo, &srp_tmo_ops, &srp_fast_io_fail_tmo,
109 S_IRUGO | S_IWUSR);
110MODULE_PARM_DESC(fast_io_fail_tmo,
111 "Number of seconds between the observation of a transport"
112 " layer error and failing all I/O. \"off\" means that this"
113 " functionality is disabled.");
114
Bart Van Asschea95cadb2013-10-26 14:37:17 +0200115static int srp_dev_loss_tmo = 600;
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200116module_param_cb(dev_loss_tmo, &srp_tmo_ops, &srp_dev_loss_tmo,
117 S_IRUGO | S_IWUSR);
118MODULE_PARM_DESC(dev_loss_tmo,
119 "Maximum number of seconds that the SRP transport should"
120 " insulate transport layer errors. After this time has been"
121 " exceeded the SCSI host is removed. Should be"
122 " between 1 and " __stringify(SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
123 " if fast_io_fail_tmo has not been set. \"off\" means that"
124 " this functionality is disabled.");
125
Roland Dreieraef9ec32005-11-02 14:07:13 -0800126static void srp_add_one(struct ib_device *device);
127static void srp_remove_one(struct ib_device *device);
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000128static void srp_recv_completion(struct ib_cq *cq, void *target_ptr);
129static void srp_send_completion(struct ib_cq *cq, void *target_ptr);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800130static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
131
FUJITA Tomonori32368222007-06-27 16:33:12 +0900132static struct scsi_transport_template *ib_srp_transport_template;
Bart Van Asschebcc05912014-07-09 15:57:26 +0200133static struct workqueue_struct *srp_remove_wq;
FUJITA Tomonori32368222007-06-27 16:33:12 +0900134
Roland Dreieraef9ec32005-11-02 14:07:13 -0800135static struct ib_client srp_client = {
136 .name = "srp",
137 .add = srp_add_one,
138 .remove = srp_remove_one
139};
140
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700141static struct ib_sa_client srp_sa_client;
142
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200143static int srp_tmo_get(char *buffer, const struct kernel_param *kp)
144{
145 int tmo = *(int *)kp->arg;
146
147 if (tmo >= 0)
148 return sprintf(buffer, "%d", tmo);
149 else
150 return sprintf(buffer, "off");
151}
152
153static int srp_tmo_set(const char *val, const struct kernel_param *kp)
154{
155 int tmo, res;
156
157 if (strncmp(val, "off", 3) != 0) {
158 res = kstrtoint(val, 0, &tmo);
159 if (res)
160 goto out;
161 } else {
162 tmo = -1;
163 }
Bart Van Asschea95cadb2013-10-26 14:37:17 +0200164 if (kp->arg == &srp_reconnect_delay)
165 res = srp_tmo_valid(tmo, srp_fast_io_fail_tmo,
166 srp_dev_loss_tmo);
167 else if (kp->arg == &srp_fast_io_fail_tmo)
168 res = srp_tmo_valid(srp_reconnect_delay, tmo, srp_dev_loss_tmo);
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200169 else
Bart Van Asschea95cadb2013-10-26 14:37:17 +0200170 res = srp_tmo_valid(srp_reconnect_delay, srp_fast_io_fail_tmo,
171 tmo);
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200172 if (res)
173 goto out;
174 *(int *)kp->arg = tmo;
175
176out:
177 return res;
178}
179
180static struct kernel_param_ops srp_tmo_ops = {
181 .get = srp_tmo_get,
182 .set = srp_tmo_set,
183};
184
Roland Dreieraef9ec32005-11-02 14:07:13 -0800185static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
186{
187 return (struct srp_target_port *) host->hostdata;
188}
189
190static const char *srp_target_info(struct Scsi_Host *host)
191{
192 return host_to_target(host)->target_name;
193}
194
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700195static int srp_target_is_topspin(struct srp_target_port *target)
196{
197 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
Raghava Kondapalli3d1ff48d2007-08-03 10:45:18 -0700198 static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700199
200 return topspin_workarounds &&
Raghava Kondapalli3d1ff48d2007-08-03 10:45:18 -0700201 (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
202 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700203}
204
Roland Dreieraef9ec32005-11-02 14:07:13 -0800205static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
206 gfp_t gfp_mask,
207 enum dma_data_direction direction)
208{
209 struct srp_iu *iu;
210
211 iu = kmalloc(sizeof *iu, gfp_mask);
212 if (!iu)
213 goto out;
214
215 iu->buf = kzalloc(size, gfp_mask);
216 if (!iu->buf)
217 goto out_free_iu;
218
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100219 iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
220 direction);
221 if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800222 goto out_free_buf;
223
224 iu->size = size;
225 iu->direction = direction;
226
227 return iu;
228
229out_free_buf:
230 kfree(iu->buf);
231out_free_iu:
232 kfree(iu);
233out:
234 return NULL;
235}
236
237static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
238{
239 if (!iu)
240 return;
241
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100242 ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
243 iu->direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800244 kfree(iu->buf);
245 kfree(iu);
246}
247
248static void srp_qp_event(struct ib_event *event, void *context)
249{
Bart Van Asschee0bda7d2012-01-14 12:39:44 +0000250 pr_debug("QP event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800251}
252
253static int srp_init_qp(struct srp_target_port *target,
254 struct ib_qp *qp)
255{
256 struct ib_qp_attr *attr;
257 int ret;
258
259 attr = kmalloc(sizeof *attr, GFP_KERNEL);
260 if (!attr)
261 return -ENOMEM;
262
Roland Dreier969a60f2008-07-14 23:48:43 -0700263 ret = ib_find_pkey(target->srp_host->srp_dev->dev,
264 target->srp_host->port,
265 be16_to_cpu(target->path.pkey),
266 &attr->pkey_index);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800267 if (ret)
268 goto out;
269
270 attr->qp_state = IB_QPS_INIT;
271 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
272 IB_ACCESS_REMOTE_WRITE);
273 attr->port_num = target->srp_host->port;
274
275 ret = ib_modify_qp(qp, attr,
276 IB_QP_STATE |
277 IB_QP_PKEY_INDEX |
278 IB_QP_ACCESS_FLAGS |
279 IB_QP_PORT);
280
281out:
282 kfree(attr);
283 return ret;
284}
285
David Dillow9fe4bcf2008-01-08 17:08:52 -0500286static int srp_new_cm_id(struct srp_target_port *target)
287{
288 struct ib_cm_id *new_cm_id;
289
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100290 new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
David Dillow9fe4bcf2008-01-08 17:08:52 -0500291 srp_cm_handler, target);
292 if (IS_ERR(new_cm_id))
293 return PTR_ERR(new_cm_id);
294
295 if (target->cm_id)
296 ib_destroy_cm_id(target->cm_id);
297 target->cm_id = new_cm_id;
298
299 return 0;
300}
301
Bart Van Assched1b42892014-05-20 15:07:20 +0200302static struct ib_fmr_pool *srp_alloc_fmr_pool(struct srp_target_port *target)
303{
304 struct srp_device *dev = target->srp_host->srp_dev;
305 struct ib_fmr_pool_param fmr_param;
306
307 memset(&fmr_param, 0, sizeof(fmr_param));
308 fmr_param.pool_size = target->scsi_host->can_queue;
309 fmr_param.dirty_watermark = fmr_param.pool_size / 4;
310 fmr_param.cache = 1;
Bart Van Assche52ede082014-05-20 15:07:45 +0200311 fmr_param.max_pages_per_fmr = dev->max_pages_per_mr;
312 fmr_param.page_shift = ilog2(dev->mr_page_size);
Bart Van Assched1b42892014-05-20 15:07:20 +0200313 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
314 IB_ACCESS_REMOTE_WRITE |
315 IB_ACCESS_REMOTE_READ);
316
317 return ib_create_fmr_pool(dev->pd, &fmr_param);
318}
319
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200320/**
321 * srp_destroy_fr_pool() - free the resources owned by a pool
322 * @pool: Fast registration pool to be destroyed.
323 */
324static void srp_destroy_fr_pool(struct srp_fr_pool *pool)
325{
326 int i;
327 struct srp_fr_desc *d;
328
329 if (!pool)
330 return;
331
332 for (i = 0, d = &pool->desc[0]; i < pool->size; i++, d++) {
333 if (d->frpl)
334 ib_free_fast_reg_page_list(d->frpl);
335 if (d->mr)
336 ib_dereg_mr(d->mr);
337 }
338 kfree(pool);
339}
340
341/**
342 * srp_create_fr_pool() - allocate and initialize a pool for fast registration
343 * @device: IB device to allocate fast registration descriptors for.
344 * @pd: Protection domain associated with the FR descriptors.
345 * @pool_size: Number of descriptors to allocate.
346 * @max_page_list_len: Maximum fast registration work request page list length.
347 */
348static struct srp_fr_pool *srp_create_fr_pool(struct ib_device *device,
349 struct ib_pd *pd, int pool_size,
350 int max_page_list_len)
351{
352 struct srp_fr_pool *pool;
353 struct srp_fr_desc *d;
354 struct ib_mr *mr;
355 struct ib_fast_reg_page_list *frpl;
356 int i, ret = -EINVAL;
357
358 if (pool_size <= 0)
359 goto err;
360 ret = -ENOMEM;
361 pool = kzalloc(sizeof(struct srp_fr_pool) +
362 pool_size * sizeof(struct srp_fr_desc), GFP_KERNEL);
363 if (!pool)
364 goto err;
365 pool->size = pool_size;
366 pool->max_page_list_len = max_page_list_len;
367 spin_lock_init(&pool->lock);
368 INIT_LIST_HEAD(&pool->free_list);
369
370 for (i = 0, d = &pool->desc[0]; i < pool->size; i++, d++) {
371 mr = ib_alloc_fast_reg_mr(pd, max_page_list_len);
372 if (IS_ERR(mr)) {
373 ret = PTR_ERR(mr);
374 goto destroy_pool;
375 }
376 d->mr = mr;
377 frpl = ib_alloc_fast_reg_page_list(device, max_page_list_len);
378 if (IS_ERR(frpl)) {
379 ret = PTR_ERR(frpl);
380 goto destroy_pool;
381 }
382 d->frpl = frpl;
383 list_add_tail(&d->entry, &pool->free_list);
384 }
385
386out:
387 return pool;
388
389destroy_pool:
390 srp_destroy_fr_pool(pool);
391
392err:
393 pool = ERR_PTR(ret);
394 goto out;
395}
396
397/**
398 * srp_fr_pool_get() - obtain a descriptor suitable for fast registration
399 * @pool: Pool to obtain descriptor from.
400 */
401static struct srp_fr_desc *srp_fr_pool_get(struct srp_fr_pool *pool)
402{
403 struct srp_fr_desc *d = NULL;
404 unsigned long flags;
405
406 spin_lock_irqsave(&pool->lock, flags);
407 if (!list_empty(&pool->free_list)) {
408 d = list_first_entry(&pool->free_list, typeof(*d), entry);
409 list_del(&d->entry);
410 }
411 spin_unlock_irqrestore(&pool->lock, flags);
412
413 return d;
414}
415
416/**
417 * srp_fr_pool_put() - put an FR descriptor back in the free list
418 * @pool: Pool the descriptor was allocated from.
419 * @desc: Pointer to an array of fast registration descriptor pointers.
420 * @n: Number of descriptors to put back.
421 *
422 * Note: The caller must already have queued an invalidation request for
423 * desc->mr->rkey before calling this function.
424 */
425static void srp_fr_pool_put(struct srp_fr_pool *pool, struct srp_fr_desc **desc,
426 int n)
427{
428 unsigned long flags;
429 int i;
430
431 spin_lock_irqsave(&pool->lock, flags);
432 for (i = 0; i < n; i++)
433 list_add(&desc[i]->entry, &pool->free_list);
434 spin_unlock_irqrestore(&pool->lock, flags);
435}
436
437static struct srp_fr_pool *srp_alloc_fr_pool(struct srp_target_port *target)
438{
439 struct srp_device *dev = target->srp_host->srp_dev;
440
441 return srp_create_fr_pool(dev->dev, dev->pd,
442 target->scsi_host->can_queue,
443 dev->max_pages_per_mr);
444}
445
Roland Dreieraef9ec32005-11-02 14:07:13 -0800446static int srp_create_target_ib(struct srp_target_port *target)
447{
Bart Van Assche62154b22014-05-20 15:04:45 +0200448 struct srp_device *dev = target->srp_host->srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800449 struct ib_qp_init_attr *init_attr;
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100450 struct ib_cq *recv_cq, *send_cq;
451 struct ib_qp *qp;
Bart Van Assched1b42892014-05-20 15:07:20 +0200452 struct ib_fmr_pool *fmr_pool = NULL;
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200453 struct srp_fr_pool *fr_pool = NULL;
454 const int m = 1 + dev->use_fast_reg;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800455 int ret;
456
457 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
458 if (!init_attr)
459 return -ENOMEM;
460
Bart Van Assche62154b22014-05-20 15:04:45 +0200461 recv_cq = ib_create_cq(dev->dev, srp_recv_completion, NULL, target,
Bart Van Assche4d73f952013-10-26 14:40:37 +0200462 target->queue_size, target->comp_vector);
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100463 if (IS_ERR(recv_cq)) {
464 ret = PTR_ERR(recv_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800465 goto err;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800466 }
467
Bart Van Assche62154b22014-05-20 15:04:45 +0200468 send_cq = ib_create_cq(dev->dev, srp_send_completion, NULL, target,
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200469 m * target->queue_size, target->comp_vector);
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100470 if (IS_ERR(send_cq)) {
471 ret = PTR_ERR(send_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800472 goto err_recv_cq;
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000473 }
474
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100475 ib_req_notify_cq(recv_cq, IB_CQ_NEXT_COMP);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800476
477 init_attr->event_handler = srp_qp_event;
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200478 init_attr->cap.max_send_wr = m * target->queue_size;
Bart Van Assche4d73f952013-10-26 14:40:37 +0200479 init_attr->cap.max_recv_wr = target->queue_size;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800480 init_attr->cap.max_recv_sge = 1;
481 init_attr->cap.max_send_sge = 1;
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200482 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800483 init_attr->qp_type = IB_QPT_RC;
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100484 init_attr->send_cq = send_cq;
485 init_attr->recv_cq = recv_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800486
Bart Van Assche62154b22014-05-20 15:04:45 +0200487 qp = ib_create_qp(dev->pd, init_attr);
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100488 if (IS_ERR(qp)) {
489 ret = PTR_ERR(qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800490 goto err_send_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800491 }
492
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100493 ret = srp_init_qp(target, qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800494 if (ret)
495 goto err_qp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800496
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200497 if (dev->use_fast_reg && dev->has_fr) {
498 fr_pool = srp_alloc_fr_pool(target);
499 if (IS_ERR(fr_pool)) {
500 ret = PTR_ERR(fr_pool);
501 shost_printk(KERN_WARNING, target->scsi_host, PFX
502 "FR pool allocation failed (%d)\n", ret);
503 goto err_qp;
504 }
505 if (target->fr_pool)
506 srp_destroy_fr_pool(target->fr_pool);
507 target->fr_pool = fr_pool;
508 } else if (!dev->use_fast_reg && dev->has_fmr) {
Bart Van Assched1b42892014-05-20 15:07:20 +0200509 fmr_pool = srp_alloc_fmr_pool(target);
510 if (IS_ERR(fmr_pool)) {
511 ret = PTR_ERR(fmr_pool);
512 shost_printk(KERN_WARNING, target->scsi_host, PFX
513 "FMR pool allocation failed (%d)\n", ret);
514 goto err_qp;
515 }
516 if (target->fmr_pool)
517 ib_destroy_fmr_pool(target->fmr_pool);
518 target->fmr_pool = fmr_pool;
519 }
520
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100521 if (target->qp)
522 ib_destroy_qp(target->qp);
523 if (target->recv_cq)
524 ib_destroy_cq(target->recv_cq);
525 if (target->send_cq)
526 ib_destroy_cq(target->send_cq);
527
528 target->qp = qp;
529 target->recv_cq = recv_cq;
530 target->send_cq = send_cq;
531
Roland Dreierda9d2f02010-02-24 15:07:59 -0800532 kfree(init_attr);
533 return 0;
534
535err_qp:
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100536 ib_destroy_qp(qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800537
538err_send_cq:
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100539 ib_destroy_cq(send_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800540
541err_recv_cq:
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100542 ib_destroy_cq(recv_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800543
544err:
Roland Dreieraef9ec32005-11-02 14:07:13 -0800545 kfree(init_attr);
546 return ret;
547}
548
Bart Van Assche4d73f952013-10-26 14:40:37 +0200549/*
550 * Note: this function may be called without srp_alloc_iu_bufs() having been
551 * invoked. Hence the target->[rt]x_ring checks.
552 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800553static void srp_free_target_ib(struct srp_target_port *target)
554{
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200555 struct srp_device *dev = target->srp_host->srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800556 int i;
557
Bart Van Assche394c5952014-10-30 14:46:27 +0100558 if (target->cm_id) {
559 ib_destroy_cm_id(target->cm_id);
560 target->cm_id = NULL;
561 }
562
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200563 if (dev->use_fast_reg) {
564 if (target->fr_pool)
565 srp_destroy_fr_pool(target->fr_pool);
566 } else {
567 if (target->fmr_pool)
568 ib_destroy_fmr_pool(target->fmr_pool);
569 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800570 ib_destroy_qp(target->qp);
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000571 ib_destroy_cq(target->send_cq);
572 ib_destroy_cq(target->recv_cq);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800573
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100574 target->qp = NULL;
575 target->send_cq = target->recv_cq = NULL;
576
Bart Van Assche4d73f952013-10-26 14:40:37 +0200577 if (target->rx_ring) {
578 for (i = 0; i < target->queue_size; ++i)
579 srp_free_iu(target->srp_host, target->rx_ring[i]);
580 kfree(target->rx_ring);
581 target->rx_ring = NULL;
582 }
583 if (target->tx_ring) {
584 for (i = 0; i < target->queue_size; ++i)
585 srp_free_iu(target->srp_host, target->tx_ring[i]);
586 kfree(target->tx_ring);
587 target->tx_ring = NULL;
588 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800589}
590
591static void srp_path_rec_completion(int status,
592 struct ib_sa_path_rec *pathrec,
593 void *target_ptr)
594{
595 struct srp_target_port *target = target_ptr;
596
597 target->status = status;
598 if (status)
David Dillow7aa54bd2008-01-07 18:23:41 -0500599 shost_printk(KERN_ERR, target->scsi_host,
600 PFX "Got failed path rec status %d\n", status);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800601 else
602 target->path = *pathrec;
603 complete(&target->done);
604}
605
606static int srp_lookup_path(struct srp_target_port *target)
607{
Bart Van Asschea702adc2014-03-14 13:53:10 +0100608 int ret;
609
Roland Dreieraef9ec32005-11-02 14:07:13 -0800610 target->path.numb_path = 1;
611
612 init_completion(&target->done);
613
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700614 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100615 target->srp_host->srp_dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800616 target->srp_host->port,
617 &target->path,
Sean Hefty247e0202007-08-08 15:51:18 -0700618 IB_SA_PATH_REC_SERVICE_ID |
Roland Dreieraef9ec32005-11-02 14:07:13 -0800619 IB_SA_PATH_REC_DGID |
620 IB_SA_PATH_REC_SGID |
621 IB_SA_PATH_REC_NUMB_PATH |
622 IB_SA_PATH_REC_PKEY,
623 SRP_PATH_REC_TIMEOUT_MS,
624 GFP_KERNEL,
625 srp_path_rec_completion,
626 target, &target->path_query);
627 if (target->path_query_id < 0)
628 return target->path_query_id;
629
Bart Van Asschea702adc2014-03-14 13:53:10 +0100630 ret = wait_for_completion_interruptible(&target->done);
631 if (ret < 0)
632 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800633
634 if (target->status < 0)
David Dillow7aa54bd2008-01-07 18:23:41 -0500635 shost_printk(KERN_WARNING, target->scsi_host,
636 PFX "Path record query failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800637
638 return target->status;
639}
640
641static int srp_send_req(struct srp_target_port *target)
642{
643 struct {
644 struct ib_cm_req_param param;
645 struct srp_login_req priv;
646 } *req = NULL;
647 int status;
648
649 req = kzalloc(sizeof *req, GFP_KERNEL);
650 if (!req)
651 return -ENOMEM;
652
653 req->param.primary_path = &target->path;
654 req->param.alternate_path = NULL;
655 req->param.service_id = target->service_id;
656 req->param.qp_num = target->qp->qp_num;
657 req->param.qp_type = target->qp->qp_type;
658 req->param.private_data = &req->priv;
659 req->param.private_data_len = sizeof req->priv;
660 req->param.flow_control = 1;
661
662 get_random_bytes(&req->param.starting_psn, 4);
663 req->param.starting_psn &= 0xffffff;
664
665 /*
666 * Pick some arbitrary defaults here; we could make these
667 * module parameters if anyone cared about setting them.
668 */
669 req->param.responder_resources = 4;
670 req->param.remote_cm_response_timeout = 20;
671 req->param.local_cm_response_timeout = 20;
Vu Pham7bb312e2013-10-26 14:31:27 +0200672 req->param.retry_count = target->tl_retry_count;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800673 req->param.rnr_retry_count = 7;
674 req->param.max_cm_retries = 15;
675
676 req->priv.opcode = SRP_LOGIN_REQ;
677 req->priv.tag = 0;
David Dillow49248642011-01-14 18:23:24 -0500678 req->priv.req_it_iu_len = cpu_to_be32(target->max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800679 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
680 SRP_BUF_FORMAT_INDIRECT);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700681 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700682 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450db2006-06-17 20:37:38 -0700683 * port identifier format is 8 bytes of ID extension followed
684 * by 8 bytes of GUID. Older drafts put the two halves in the
685 * opposite order, so that the GUID comes first.
686 *
687 * Targets conforming to these obsolete drafts can be
688 * recognized by the I/O Class they report.
689 */
690 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
691 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200692 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700693 memcpy(req->priv.initiator_port_id + 8,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200694 &target->initiator_ext, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700695 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
696 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
697 } else {
698 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200699 &target->initiator_ext, 8);
700 memcpy(req->priv.initiator_port_id + 8,
701 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700702 memcpy(req->priv.target_port_id, &target->id_ext, 8);
703 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
704 }
705
Roland Dreieraef9ec32005-11-02 14:07:13 -0800706 /*
707 * Topspin/Cisco SRP targets will reject our login unless we
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200708 * zero out the first 8 bytes of our initiator port ID and set
709 * the second 8 bytes to the local node GUID.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800710 */
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700711 if (srp_target_is_topspin(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500712 shost_printk(KERN_DEBUG, target->scsi_host,
713 PFX "Topspin/Cisco initiator port ID workaround "
714 "activated for target GUID %016llx\n",
715 (unsigned long long) be64_to_cpu(target->ioc_guid));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800716 memset(req->priv.initiator_port_id, 0, 8);
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200717 memcpy(req->priv.initiator_port_id + 8,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100718 &target->srp_host->srp_dev->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800719 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800720
721 status = ib_send_cm_req(target->cm_id, &req->param);
722
723 kfree(req);
724
725 return status;
726}
727
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000728static bool srp_queue_remove_work(struct srp_target_port *target)
729{
730 bool changed = false;
731
732 spin_lock_irq(&target->lock);
733 if (target->state != SRP_TARGET_REMOVED) {
734 target->state = SRP_TARGET_REMOVED;
735 changed = true;
736 }
737 spin_unlock_irq(&target->lock);
738
739 if (changed)
Bart Van Asschebcc05912014-07-09 15:57:26 +0200740 queue_work(srp_remove_wq, &target->remove_work);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000741
742 return changed;
743}
744
Bart Van Assche294c8752011-12-25 12:18:12 +0000745static bool srp_change_conn_state(struct srp_target_port *target,
746 bool connected)
747{
748 bool changed = false;
749
750 spin_lock_irq(&target->lock);
751 if (target->connected != connected) {
752 target->connected = connected;
753 changed = true;
754 }
755 spin_unlock_irq(&target->lock);
756
757 return changed;
758}
759
Roland Dreieraef9ec32005-11-02 14:07:13 -0800760static void srp_disconnect_target(struct srp_target_port *target)
761{
Bart Van Assche294c8752011-12-25 12:18:12 +0000762 if (srp_change_conn_state(target, false)) {
763 /* XXX should send SRP_I_LOGOUT request */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800764
Bart Van Assche294c8752011-12-25 12:18:12 +0000765 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
766 shost_printk(KERN_DEBUG, target->scsi_host,
767 PFX "Sending CM DREQ failed\n");
Bart Van Assche294c8752011-12-25 12:18:12 +0000768 }
Roland Dreiere6581052006-05-17 09:13:21 -0700769 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800770}
771
David Dillow8f26c9f2011-01-14 19:45:50 -0500772static void srp_free_req_data(struct srp_target_port *target)
773{
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200774 struct srp_device *dev = target->srp_host->srp_dev;
775 struct ib_device *ibdev = dev->dev;
David Dillow8f26c9f2011-01-14 19:45:50 -0500776 struct srp_request *req;
777 int i;
778
Bart Van Assche4d73f952013-10-26 14:40:37 +0200779 if (!target->req_ring)
780 return;
781
782 for (i = 0; i < target->req_ring_size; ++i) {
783 req = &target->req_ring[i];
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200784 if (dev->use_fast_reg)
785 kfree(req->fr_list);
786 else
787 kfree(req->fmr_list);
David Dillow8f26c9f2011-01-14 19:45:50 -0500788 kfree(req->map_page);
David Dillowc07d4242011-01-16 13:57:10 -0500789 if (req->indirect_dma_addr) {
790 ib_dma_unmap_single(ibdev, req->indirect_dma_addr,
791 target->indirect_size,
792 DMA_TO_DEVICE);
793 }
794 kfree(req->indirect_desc);
David Dillow8f26c9f2011-01-14 19:45:50 -0500795 }
Bart Van Assche4d73f952013-10-26 14:40:37 +0200796
797 kfree(target->req_ring);
798 target->req_ring = NULL;
David Dillow8f26c9f2011-01-14 19:45:50 -0500799}
800
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200801static int srp_alloc_req_data(struct srp_target_port *target)
802{
803 struct srp_device *srp_dev = target->srp_host->srp_dev;
804 struct ib_device *ibdev = srp_dev->dev;
805 struct srp_request *req;
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200806 void *mr_list;
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200807 dma_addr_t dma_addr;
808 int i, ret = -ENOMEM;
809
810 INIT_LIST_HEAD(&target->free_reqs);
811
Bart Van Assche4d73f952013-10-26 14:40:37 +0200812 target->req_ring = kzalloc(target->req_ring_size *
813 sizeof(*target->req_ring), GFP_KERNEL);
814 if (!target->req_ring)
815 goto out;
816
817 for (i = 0; i < target->req_ring_size; ++i) {
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200818 req = &target->req_ring[i];
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200819 mr_list = kmalloc(target->cmd_sg_cnt * sizeof(void *),
820 GFP_KERNEL);
821 if (!mr_list)
822 goto out;
823 if (srp_dev->use_fast_reg)
824 req->fr_list = mr_list;
825 else
826 req->fmr_list = mr_list;
Bart Van Assche52ede082014-05-20 15:07:45 +0200827 req->map_page = kmalloc(srp_dev->max_pages_per_mr *
Bart Van Assched1b42892014-05-20 15:07:20 +0200828 sizeof(void *), GFP_KERNEL);
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200829 if (!req->map_page)
830 goto out;
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200831 req->indirect_desc = kmalloc(target->indirect_size, GFP_KERNEL);
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200832 if (!req->indirect_desc)
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200833 goto out;
834
835 dma_addr = ib_dma_map_single(ibdev, req->indirect_desc,
836 target->indirect_size,
837 DMA_TO_DEVICE);
838 if (ib_dma_mapping_error(ibdev, dma_addr))
839 goto out;
840
841 req->indirect_dma_addr = dma_addr;
842 req->index = i;
843 list_add_tail(&req->list, &target->free_reqs);
844 }
845 ret = 0;
846
847out:
848 return ret;
849}
850
Bart Van Assche683b1592012-01-14 12:40:44 +0000851/**
852 * srp_del_scsi_host_attr() - Remove attributes defined in the host template.
853 * @shost: SCSI host whose attributes to remove from sysfs.
854 *
855 * Note: Any attributes defined in the host template and that did not exist
856 * before invocation of this function will be ignored.
857 */
858static void srp_del_scsi_host_attr(struct Scsi_Host *shost)
859{
860 struct device_attribute **attr;
861
862 for (attr = shost->hostt->shost_attrs; attr && *attr; ++attr)
863 device_remove_file(&shost->shost_dev, *attr);
864}
865
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000866static void srp_remove_target(struct srp_target_port *target)
867{
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000868 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
869
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000870 srp_del_scsi_host_attr(target->scsi_host);
Bart Van Assche9dd69a62013-10-26 14:32:30 +0200871 srp_rport_get(target->rport);
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000872 srp_remove_host(target->scsi_host);
873 scsi_remove_host(target->scsi_host);
Bart Van Assche93079162013-12-11 17:06:14 +0100874 srp_stop_rport_timers(target->rport);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000875 srp_disconnect_target(target);
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000876 srp_free_target_ib(target);
Bart Van Asschec1120f82013-10-26 14:35:08 +0200877 cancel_work_sync(&target->tl_err_work);
Bart Van Assche9dd69a62013-10-26 14:32:30 +0200878 srp_rport_put(target->rport);
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000879 srp_free_req_data(target);
Vu Pham65d7dd22013-10-10 13:50:29 +0200880
881 spin_lock(&target->srp_host->target_lock);
882 list_del(&target->list);
883 spin_unlock(&target->srp_host->target_lock);
884
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000885 scsi_host_put(target->scsi_host);
886}
887
David Howellsc4028952006-11-22 14:57:56 +0000888static void srp_remove_work(struct work_struct *work)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800889{
David Howellsc4028952006-11-22 14:57:56 +0000890 struct srp_target_port *target =
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000891 container_of(work, struct srp_target_port, remove_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800892
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000893 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800894
Bart Van Assche96fc2482013-06-28 14:51:26 +0200895 srp_remove_target(target);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800896}
897
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +0200898static void srp_rport_delete(struct srp_rport *rport)
899{
900 struct srp_target_port *target = rport->lld_data;
901
902 srp_queue_remove_work(target);
903}
904
Roland Dreieraef9ec32005-11-02 14:07:13 -0800905static int srp_connect_target(struct srp_target_port *target)
906{
907 int ret;
908
Bart Van Assche294c8752011-12-25 12:18:12 +0000909 WARN_ON_ONCE(target->connected);
910
Bart Van Assche948d1e82011-09-03 09:25:42 +0200911 target->qp_in_error = false;
912
Roland Dreieraef9ec32005-11-02 14:07:13 -0800913 ret = srp_lookup_path(target);
914 if (ret)
915 return ret;
916
917 while (1) {
918 init_completion(&target->done);
919 ret = srp_send_req(target);
920 if (ret)
921 return ret;
Bart Van Asschea702adc2014-03-14 13:53:10 +0100922 ret = wait_for_completion_interruptible(&target->done);
923 if (ret < 0)
924 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800925
926 /*
927 * The CM event handling code will set status to
928 * SRP_PORT_REDIRECT if we get a port redirect REJ
929 * back, or SRP_DLID_REDIRECT if we get a lid/qp
930 * redirect REJ back.
931 */
932 switch (target->status) {
933 case 0:
Bart Van Assche294c8752011-12-25 12:18:12 +0000934 srp_change_conn_state(target, true);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800935 return 0;
936
937 case SRP_PORT_REDIRECT:
938 ret = srp_lookup_path(target);
939 if (ret)
940 return ret;
941 break;
942
943 case SRP_DLID_REDIRECT:
944 break;
945
David Dillow9fe4bcf2008-01-08 17:08:52 -0500946 case SRP_STALE_CONN:
David Dillow9fe4bcf2008-01-08 17:08:52 -0500947 shost_printk(KERN_ERR, target->scsi_host, PFX
Bart Van Assche205619f2014-10-30 14:46:55 +0100948 "giving up on stale connection\n");
949 target->status = -ECONNRESET;
950 return target->status;
David Dillow9fe4bcf2008-01-08 17:08:52 -0500951
Roland Dreieraef9ec32005-11-02 14:07:13 -0800952 default:
953 return target->status;
954 }
955 }
956}
957
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200958static int srp_inv_rkey(struct srp_target_port *target, u32 rkey)
959{
960 struct ib_send_wr *bad_wr;
961 struct ib_send_wr wr = {
962 .opcode = IB_WR_LOCAL_INV,
963 .wr_id = LOCAL_INV_WR_ID_MASK,
964 .next = NULL,
965 .num_sge = 0,
966 .send_flags = 0,
967 .ex.invalidate_rkey = rkey,
968 };
969
970 return ib_post_send(target->qp, &wr, &bad_wr);
971}
972
Roland Dreierd945e1d2006-05-09 10:50:28 -0700973static void srp_unmap_data(struct scsi_cmnd *scmnd,
974 struct srp_target_port *target,
975 struct srp_request *req)
976{
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200977 struct srp_device *dev = target->srp_host->srp_dev;
978 struct ib_device *ibdev = dev->dev;
979 int i, res;
David Dillow8f26c9f2011-01-14 19:45:50 -0500980
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900981 if (!scsi_sglist(scmnd) ||
Roland Dreierd945e1d2006-05-09 10:50:28 -0700982 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
983 scmnd->sc_data_direction != DMA_FROM_DEVICE))
984 return;
985
Bart Van Assche5cfb1782014-05-20 15:08:34 +0200986 if (dev->use_fast_reg) {
987 struct srp_fr_desc **pfr;
988
989 for (i = req->nmdesc, pfr = req->fr_list; i > 0; i--, pfr++) {
990 res = srp_inv_rkey(target, (*pfr)->mr->rkey);
991 if (res < 0) {
992 shost_printk(KERN_ERR, target->scsi_host, PFX
993 "Queueing INV WR for rkey %#x failed (%d)\n",
994 (*pfr)->mr->rkey, res);
995 queue_work(system_long_wq,
996 &target->tl_err_work);
997 }
998 }
999 if (req->nmdesc)
1000 srp_fr_pool_put(target->fr_pool, req->fr_list,
1001 req->nmdesc);
1002 } else {
1003 struct ib_pool_fmr **pfmr;
1004
1005 for (i = req->nmdesc, pfmr = req->fmr_list; i > 0; i--, pfmr++)
1006 ib_fmr_pool_unmap(*pfmr);
1007 }
Roland Dreierf5358a12006-06-17 20:37:29 -07001008
David Dillow8f26c9f2011-01-14 19:45:50 -05001009 ib_dma_unmap_sg(ibdev, scsi_sglist(scmnd), scsi_sg_count(scmnd),
1010 scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001011}
1012
Bart Van Assche22032992012-08-14 13:18:53 +00001013/**
1014 * srp_claim_req - Take ownership of the scmnd associated with a request.
1015 * @target: SRP target port.
1016 * @req: SRP request.
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001017 * @sdev: If not NULL, only take ownership for this SCSI device.
Bart Van Assche22032992012-08-14 13:18:53 +00001018 * @scmnd: If NULL, take ownership of @req->scmnd. If not NULL, only take
1019 * ownership of @req->scmnd if it equals @scmnd.
1020 *
1021 * Return value:
1022 * Either NULL or a pointer to the SCSI command the caller became owner of.
1023 */
1024static struct scsi_cmnd *srp_claim_req(struct srp_target_port *target,
1025 struct srp_request *req,
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001026 struct scsi_device *sdev,
Bart Van Assche22032992012-08-14 13:18:53 +00001027 struct scsi_cmnd *scmnd)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001028{
Bart Van Assche94a91742010-11-26 14:50:09 -05001029 unsigned long flags;
1030
Bart Van Assche22032992012-08-14 13:18:53 +00001031 spin_lock_irqsave(&target->lock, flags);
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001032 if (req->scmnd &&
1033 (!sdev || req->scmnd->device == sdev) &&
1034 (!scmnd || req->scmnd == scmnd)) {
Bart Van Assche22032992012-08-14 13:18:53 +00001035 scmnd = req->scmnd;
1036 req->scmnd = NULL;
Bart Van Assche22032992012-08-14 13:18:53 +00001037 } else {
1038 scmnd = NULL;
1039 }
1040 spin_unlock_irqrestore(&target->lock, flags);
1041
1042 return scmnd;
1043}
1044
1045/**
1046 * srp_free_req() - Unmap data and add request to the free request list.
Bart Van Asscheaf246632014-05-20 15:04:21 +02001047 * @target: SRP target port.
1048 * @req: Request to be freed.
1049 * @scmnd: SCSI command associated with @req.
1050 * @req_lim_delta: Amount to be added to @target->req_lim.
Bart Van Assche22032992012-08-14 13:18:53 +00001051 */
1052static void srp_free_req(struct srp_target_port *target,
1053 struct srp_request *req, struct scsi_cmnd *scmnd,
1054 s32 req_lim_delta)
1055{
1056 unsigned long flags;
1057
1058 srp_unmap_data(scmnd, target, req);
1059
Bart Van Asschee9684672010-11-26 15:08:38 -05001060 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -05001061 target->req_lim += req_lim_delta;
Bart Van Assche536ae142010-11-26 13:58:27 -05001062 list_add_tail(&req->list, &target->free_reqs);
Bart Van Asschee9684672010-11-26 15:08:38 -05001063 spin_unlock_irqrestore(&target->lock, flags);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001064}
1065
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001066static void srp_finish_req(struct srp_target_port *target,
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001067 struct srp_request *req, struct scsi_device *sdev,
1068 int result)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001069{
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001070 struct scsi_cmnd *scmnd = srp_claim_req(target, req, sdev, NULL);
Bart Van Assche22032992012-08-14 13:18:53 +00001071
1072 if (scmnd) {
Bart Van Assche9b796d02012-08-24 10:27:54 +00001073 srp_free_req(target, req, scmnd, 0);
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001074 scmnd->result = result;
Bart Van Assche22032992012-08-14 13:18:53 +00001075 scmnd->scsi_done(scmnd);
Bart Van Assche22032992012-08-14 13:18:53 +00001076 }
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001077}
1078
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001079static void srp_terminate_io(struct srp_rport *rport)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001080{
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001081 struct srp_target_port *target = rport->lld_data;
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001082 struct Scsi_Host *shost = target->scsi_host;
1083 struct scsi_device *sdev;
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001084 int i;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001085
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001086 /*
1087 * Invoking srp_terminate_io() while srp_queuecommand() is running
1088 * is not safe. Hence the warning statement below.
1089 */
1090 shost_for_each_device(sdev, shost)
1091 WARN_ON_ONCE(sdev->request_queue->request_fn_active);
1092
Bart Van Assche4d73f952013-10-26 14:40:37 +02001093 for (i = 0; i < target->req_ring_size; ++i) {
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001094 struct srp_request *req = &target->req_ring[i];
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001095 srp_finish_req(target, req, NULL, DID_TRANSPORT_FAILFAST << 16);
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001096 }
1097}
1098
1099/*
1100 * It is up to the caller to ensure that srp_rport_reconnect() calls are
1101 * serialized and that no concurrent srp_queuecommand(), srp_abort(),
1102 * srp_reset_device() or srp_reset_host() calls will occur while this function
1103 * is in progress. One way to realize that is not to call this function
1104 * directly but to call srp_reconnect_rport() instead since that last function
1105 * serializes calls of this function via rport->mutex and also blocks
1106 * srp_queuecommand() calls before invoking this function.
1107 */
1108static int srp_rport_reconnect(struct srp_rport *rport)
1109{
1110 struct srp_target_port *target = rport->lld_data;
1111 int i, ret;
Bart Van Assche09be70a2012-03-17 17:18:54 +00001112
Roland Dreieraef9ec32005-11-02 14:07:13 -08001113 srp_disconnect_target(target);
1114 /*
Bart Van Asschec7c4e7f2013-02-21 17:19:04 +00001115 * Now get a new local CM ID so that we avoid confusing the target in
1116 * case things are really fouled up. Doing so also ensures that all CM
1117 * callbacks will have finished before a new QP is allocated.
Roland Dreieraef9ec32005-11-02 14:07:13 -08001118 */
David Dillow9fe4bcf2008-01-08 17:08:52 -05001119 ret = srp_new_cm_id(target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001120
Bart Van Assche4d73f952013-10-26 14:40:37 +02001121 for (i = 0; i < target->req_ring_size; ++i) {
Bart Van Assche536ae142010-11-26 13:58:27 -05001122 struct srp_request *req = &target->req_ring[i];
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001123 srp_finish_req(target, req, NULL, DID_RESET << 16);
Bart Van Assche536ae142010-11-26 13:58:27 -05001124 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001125
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001126 /*
1127 * Whether or not creating a new CM ID succeeded, create a new
1128 * QP. This guarantees that all callback functions for the old QP have
1129 * finished before any send requests are posted on the new QP.
1130 */
1131 ret += srp_create_target_ib(target);
1132
Bart Van Assche536ae142010-11-26 13:58:27 -05001133 INIT_LIST_HEAD(&target->free_tx);
Bart Van Assche4d73f952013-10-26 14:40:37 +02001134 for (i = 0; i < target->queue_size; ++i)
Bart Van Assche536ae142010-11-26 13:58:27 -05001135 list_add(&target->tx_ring[i]->list, &target->free_tx);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001136
Bart Van Asschec7c4e7f2013-02-21 17:19:04 +00001137 if (ret == 0)
1138 ret = srp_connect_target(target);
Bart Van Assche09be70a2012-03-17 17:18:54 +00001139
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001140 if (ret == 0)
1141 shost_printk(KERN_INFO, target->scsi_host,
1142 PFX "reconnect succeeded\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001143
1144 return ret;
1145}
1146
David Dillow8f26c9f2011-01-14 19:45:50 -05001147static void srp_map_desc(struct srp_map_state *state, dma_addr_t dma_addr,
1148 unsigned int dma_len, u32 rkey)
Roland Dreierf5358a12006-06-17 20:37:29 -07001149{
David Dillow8f26c9f2011-01-14 19:45:50 -05001150 struct srp_direct_buf *desc = state->desc;
1151
1152 desc->va = cpu_to_be64(dma_addr);
1153 desc->key = cpu_to_be32(rkey);
1154 desc->len = cpu_to_be32(dma_len);
1155
1156 state->total_len += dma_len;
1157 state->desc++;
1158 state->ndesc++;
1159}
1160
1161static int srp_map_finish_fmr(struct srp_map_state *state,
1162 struct srp_target_port *target)
1163{
David Dillow8f26c9f2011-01-14 19:45:50 -05001164 struct ib_pool_fmr *fmr;
Roland Dreierf5358a12006-06-17 20:37:29 -07001165 u64 io_addr = 0;
David Dillow8f26c9f2011-01-14 19:45:50 -05001166
Bart Van Assched1b42892014-05-20 15:07:20 +02001167 fmr = ib_fmr_pool_map_phys(target->fmr_pool, state->pages,
David Dillow8f26c9f2011-01-14 19:45:50 -05001168 state->npages, io_addr);
1169 if (IS_ERR(fmr))
1170 return PTR_ERR(fmr);
1171
1172 *state->next_fmr++ = fmr;
Bart Van Assche52ede082014-05-20 15:07:45 +02001173 state->nmdesc++;
David Dillow8f26c9f2011-01-14 19:45:50 -05001174
Bart Van Assche52ede082014-05-20 15:07:45 +02001175 srp_map_desc(state, 0, state->dma_len, fmr->fmr->rkey);
Bart Van Assche539dde62014-05-20 15:05:46 +02001176
David Dillow8f26c9f2011-01-14 19:45:50 -05001177 return 0;
1178}
1179
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001180static int srp_map_finish_fr(struct srp_map_state *state,
1181 struct srp_target_port *target)
1182{
1183 struct srp_device *dev = target->srp_host->srp_dev;
1184 struct ib_send_wr *bad_wr;
1185 struct ib_send_wr wr;
1186 struct srp_fr_desc *desc;
1187 u32 rkey;
1188
1189 desc = srp_fr_pool_get(target->fr_pool);
1190 if (!desc)
1191 return -ENOMEM;
1192
1193 rkey = ib_inc_rkey(desc->mr->rkey);
1194 ib_update_fast_reg_key(desc->mr, rkey);
1195
1196 memcpy(desc->frpl->page_list, state->pages,
1197 sizeof(state->pages[0]) * state->npages);
1198
1199 memset(&wr, 0, sizeof(wr));
1200 wr.opcode = IB_WR_FAST_REG_MR;
1201 wr.wr_id = FAST_REG_WR_ID_MASK;
1202 wr.wr.fast_reg.iova_start = state->base_dma_addr;
1203 wr.wr.fast_reg.page_list = desc->frpl;
1204 wr.wr.fast_reg.page_list_len = state->npages;
1205 wr.wr.fast_reg.page_shift = ilog2(dev->mr_page_size);
1206 wr.wr.fast_reg.length = state->dma_len;
1207 wr.wr.fast_reg.access_flags = (IB_ACCESS_LOCAL_WRITE |
1208 IB_ACCESS_REMOTE_READ |
1209 IB_ACCESS_REMOTE_WRITE);
1210 wr.wr.fast_reg.rkey = desc->mr->lkey;
1211
1212 *state->next_fr++ = desc;
1213 state->nmdesc++;
1214
1215 srp_map_desc(state, state->base_dma_addr, state->dma_len,
1216 desc->mr->rkey);
1217
1218 return ib_post_send(target->qp, &wr, &bad_wr);
1219}
1220
Bart Van Assche539dde62014-05-20 15:05:46 +02001221static int srp_finish_mapping(struct srp_map_state *state,
1222 struct srp_target_port *target)
1223{
1224 int ret = 0;
1225
1226 if (state->npages == 0)
1227 return 0;
1228
Bart Van Asscheb1b88542014-05-20 15:06:41 +02001229 if (state->npages == 1 && !register_always)
Bart Van Assche52ede082014-05-20 15:07:45 +02001230 srp_map_desc(state, state->base_dma_addr, state->dma_len,
Bart Van Assche539dde62014-05-20 15:05:46 +02001231 target->rkey);
1232 else
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001233 ret = target->srp_host->srp_dev->use_fast_reg ?
1234 srp_map_finish_fr(state, target) :
1235 srp_map_finish_fmr(state, target);
Bart Van Assche539dde62014-05-20 15:05:46 +02001236
1237 if (ret == 0) {
1238 state->npages = 0;
Bart Van Assche52ede082014-05-20 15:07:45 +02001239 state->dma_len = 0;
Bart Van Assche539dde62014-05-20 15:05:46 +02001240 }
1241
1242 return ret;
1243}
1244
David Dillow8f26c9f2011-01-14 19:45:50 -05001245static void srp_map_update_start(struct srp_map_state *state,
1246 struct scatterlist *sg, int sg_index,
1247 dma_addr_t dma_addr)
1248{
1249 state->unmapped_sg = sg;
1250 state->unmapped_index = sg_index;
1251 state->unmapped_addr = dma_addr;
1252}
1253
1254static int srp_map_sg_entry(struct srp_map_state *state,
1255 struct srp_target_port *target,
1256 struct scatterlist *sg, int sg_index,
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001257 bool use_mr)
David Dillow8f26c9f2011-01-14 19:45:50 -05001258{
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001259 struct srp_device *dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001260 struct ib_device *ibdev = dev->dev;
David Dillow8f26c9f2011-01-14 19:45:50 -05001261 dma_addr_t dma_addr = ib_sg_dma_address(ibdev, sg);
1262 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
1263 unsigned int len;
1264 int ret;
Roland Dreierf5358a12006-06-17 20:37:29 -07001265
David Dillow8f26c9f2011-01-14 19:45:50 -05001266 if (!dma_len)
1267 return 0;
Roland Dreierf5358a12006-06-17 20:37:29 -07001268
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001269 if (!use_mr) {
1270 /*
1271 * Once we're in direct map mode for a request, we don't
1272 * go back to FMR or FR mode, so no need to update anything
David Dillow8f26c9f2011-01-14 19:45:50 -05001273 * other than the descriptor.
1274 */
1275 srp_map_desc(state, dma_addr, dma_len, target->rkey);
1276 return 0;
1277 }
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -07001278
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001279 /*
1280 * Since not all RDMA HW drivers support non-zero page offsets for
1281 * FMR, if we start at an offset into a page, don't merge into the
1282 * current FMR mapping. Finish it out, and use the kernel's MR for
1283 * this sg entry.
David Dillow8f26c9f2011-01-14 19:45:50 -05001284 */
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001285 if ((!dev->use_fast_reg && dma_addr & ~dev->mr_page_mask) ||
1286 dma_len > dev->mr_max_size) {
Bart Van Assche539dde62014-05-20 15:05:46 +02001287 ret = srp_finish_mapping(state, target);
David Dillow8f26c9f2011-01-14 19:45:50 -05001288 if (ret)
1289 return ret;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001290
David Dillow8f26c9f2011-01-14 19:45:50 -05001291 srp_map_desc(state, dma_addr, dma_len, target->rkey);
1292 srp_map_update_start(state, NULL, 0, 0);
1293 return 0;
1294 }
1295
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001296 /*
1297 * If this is the first sg that will be mapped via FMR or via FR, save
1298 * our position. We need to know the first unmapped entry, its index,
1299 * and the first unmapped address within that entry to be able to
1300 * restart mapping after an error.
David Dillow8f26c9f2011-01-14 19:45:50 -05001301 */
1302 if (!state->unmapped_sg)
1303 srp_map_update_start(state, sg, sg_index, dma_addr);
1304
1305 while (dma_len) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001306 unsigned offset = dma_addr & ~dev->mr_page_mask;
1307 if (state->npages == dev->max_pages_per_mr || offset != 0) {
Bart Van Assche539dde62014-05-20 15:05:46 +02001308 ret = srp_finish_mapping(state, target);
David Dillow8f26c9f2011-01-14 19:45:50 -05001309 if (ret)
1310 return ret;
1311
1312 srp_map_update_start(state, sg, sg_index, dma_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -07001313 }
1314
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001315 len = min_t(unsigned int, dma_len, dev->mr_page_size - offset);
David Dillow8f26c9f2011-01-14 19:45:50 -05001316
1317 if (!state->npages)
1318 state->base_dma_addr = dma_addr;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001319 state->pages[state->npages++] = dma_addr & dev->mr_page_mask;
Bart Van Assche52ede082014-05-20 15:07:45 +02001320 state->dma_len += len;
David Dillow8f26c9f2011-01-14 19:45:50 -05001321 dma_addr += len;
1322 dma_len -= len;
Roland Dreierf5358a12006-06-17 20:37:29 -07001323 }
1324
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001325 /*
1326 * If the last entry of the MR wasn't a full page, then we need to
David Dillow8f26c9f2011-01-14 19:45:50 -05001327 * close it out and start a new one -- we can only merge at page
1328 * boundries.
1329 */
Roland Dreierf5358a12006-06-17 20:37:29 -07001330 ret = 0;
Bart Van Assche52ede082014-05-20 15:07:45 +02001331 if (len != dev->mr_page_size) {
Bart Van Assche539dde62014-05-20 15:05:46 +02001332 ret = srp_finish_mapping(state, target);
David Dillow8f26c9f2011-01-14 19:45:50 -05001333 if (!ret)
1334 srp_map_update_start(state, NULL, 0, 0);
1335 }
Roland Dreierf5358a12006-06-17 20:37:29 -07001336 return ret;
1337}
1338
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001339static int srp_map_sg(struct srp_map_state *state,
1340 struct srp_target_port *target, struct srp_request *req,
1341 struct scatterlist *scat, int count)
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001342{
1343 struct srp_device *dev = target->srp_host->srp_dev;
1344 struct ib_device *ibdev = dev->dev;
1345 struct scatterlist *sg;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001346 int i;
1347 bool use_mr;
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001348
1349 state->desc = req->indirect_desc;
1350 state->pages = req->map_page;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001351 if (dev->use_fast_reg) {
1352 state->next_fr = req->fr_list;
1353 use_mr = !!target->fr_pool;
1354 } else {
1355 state->next_fmr = req->fmr_list;
1356 use_mr = !!target->fmr_pool;
1357 }
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001358
1359 for_each_sg(scat, sg, count, i) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001360 if (srp_map_sg_entry(state, target, sg, i, use_mr)) {
1361 /*
1362 * Memory registration failed, so backtrack to the
1363 * first unmapped entry and continue on without using
1364 * memory registration.
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001365 */
1366 dma_addr_t dma_addr;
1367 unsigned int dma_len;
1368
1369backtrack:
1370 sg = state->unmapped_sg;
1371 i = state->unmapped_index;
1372
1373 dma_addr = ib_sg_dma_address(ibdev, sg);
1374 dma_len = ib_sg_dma_len(ibdev, sg);
1375 dma_len -= (state->unmapped_addr - dma_addr);
1376 dma_addr = state->unmapped_addr;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001377 use_mr = false;
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001378 srp_map_desc(state, dma_addr, dma_len, target->rkey);
1379 }
1380 }
1381
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001382 if (use_mr && srp_finish_mapping(state, target))
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001383 goto backtrack;
1384
Bart Van Assche52ede082014-05-20 15:07:45 +02001385 req->nmdesc = state->nmdesc;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001386
1387 return 0;
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001388}
1389
Roland Dreieraef9ec32005-11-02 14:07:13 -08001390static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
1391 struct srp_request *req)
1392{
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001393 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001394 struct srp_cmd *cmd = req->cmd->buf;
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001395 int len, nents, count;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001396 struct srp_device *dev;
1397 struct ib_device *ibdev;
David Dillow8f26c9f2011-01-14 19:45:50 -05001398 struct srp_map_state state;
1399 struct srp_indirect_buf *indirect_hdr;
David Dillow8f26c9f2011-01-14 19:45:50 -05001400 u32 table_len;
1401 u8 fmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001402
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001403 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001404 return sizeof (struct srp_cmd);
1405
1406 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
1407 scmnd->sc_data_direction != DMA_TO_DEVICE) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001408 shost_printk(KERN_WARNING, target->scsi_host,
1409 PFX "Unhandled data direction %d\n",
1410 scmnd->sc_data_direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001411 return -EINVAL;
1412 }
1413
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001414 nents = scsi_sg_count(scmnd);
1415 scat = scsi_sglist(scmnd);
Roland Dreiercf368712006-03-24 15:47:26 -08001416
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001417 dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001418 ibdev = dev->dev;
1419
1420 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
David Dillow8f26c9f2011-01-14 19:45:50 -05001421 if (unlikely(count == 0))
1422 return -EIO;
Roland Dreierf5358a12006-06-17 20:37:29 -07001423
1424 fmt = SRP_DATA_DESC_DIRECT;
1425 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -08001426
Bart Van Asscheb1b88542014-05-20 15:06:41 +02001427 if (count == 1 && !register_always) {
Roland Dreierf5358a12006-06-17 20:37:29 -07001428 /*
1429 * The midlayer only generated a single gather/scatter
1430 * entry, or DMA mapping coalesced everything to a
1431 * single entry. So a direct descriptor along with
1432 * the DMA MR suffices.
1433 */
Roland Dreieraef9ec32005-11-02 14:07:13 -08001434 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001435
Ralph Campbell85507bc2006-12-12 14:30:55 -08001436 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
David Dillow9af76272010-11-26 15:34:46 -05001437 buf->key = cpu_to_be32(target->rkey);
Ralph Campbell85507bc2006-12-12 14:30:55 -08001438 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Roland Dreiercf368712006-03-24 15:47:26 -08001439
Bart Van Assche52ede082014-05-20 15:07:45 +02001440 req->nmdesc = 0;
David Dillow8f26c9f2011-01-14 19:45:50 -05001441 goto map_complete;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001442 }
1443
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001444 /*
1445 * We have more than one scatter/gather entry, so build our indirect
1446 * descriptor table, trying to merge as many entries as we can.
David Dillow8f26c9f2011-01-14 19:45:50 -05001447 */
1448 indirect_hdr = (void *) cmd->add_data;
1449
David Dillowc07d4242011-01-16 13:57:10 -05001450 ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr,
1451 target->indirect_size, DMA_TO_DEVICE);
1452
David Dillow8f26c9f2011-01-14 19:45:50 -05001453 memset(&state, 0, sizeof(state));
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001454 srp_map_sg(&state, target, req, scat, count);
David Dillow8f26c9f2011-01-14 19:45:50 -05001455
David Dillowc07d4242011-01-16 13:57:10 -05001456 /* We've mapped the request, now pull as much of the indirect
1457 * descriptor table as we can into the command buffer. If this
1458 * target is not using an external indirect table, we are
1459 * guaranteed to fit into the command, as the SCSI layer won't
1460 * give us more S/G entries than we allow.
David Dillow8f26c9f2011-01-14 19:45:50 -05001461 */
David Dillow8f26c9f2011-01-14 19:45:50 -05001462 if (state.ndesc == 1) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001463 /*
1464 * Memory registration collapsed the sg-list into one entry,
David Dillow8f26c9f2011-01-14 19:45:50 -05001465 * so use a direct descriptor.
1466 */
1467 struct srp_direct_buf *buf = (void *) cmd->add_data;
1468
David Dillowc07d4242011-01-16 13:57:10 -05001469 *buf = req->indirect_desc[0];
David Dillow8f26c9f2011-01-14 19:45:50 -05001470 goto map_complete;
1471 }
1472
David Dillowc07d4242011-01-16 13:57:10 -05001473 if (unlikely(target->cmd_sg_cnt < state.ndesc &&
1474 !target->allow_ext_sg)) {
1475 shost_printk(KERN_ERR, target->scsi_host,
1476 "Could not fit S/G list into SRP_CMD\n");
1477 return -EIO;
1478 }
1479
1480 count = min(state.ndesc, target->cmd_sg_cnt);
David Dillow8f26c9f2011-01-14 19:45:50 -05001481 table_len = state.ndesc * sizeof (struct srp_direct_buf);
1482
1483 fmt = SRP_DATA_DESC_INDIRECT;
1484 len = sizeof(struct srp_cmd) + sizeof (struct srp_indirect_buf);
David Dillowc07d4242011-01-16 13:57:10 -05001485 len += count * sizeof (struct srp_direct_buf);
David Dillow8f26c9f2011-01-14 19:45:50 -05001486
David Dillowc07d4242011-01-16 13:57:10 -05001487 memcpy(indirect_hdr->desc_list, req->indirect_desc,
1488 count * sizeof (struct srp_direct_buf));
David Dillow8f26c9f2011-01-14 19:45:50 -05001489
David Dillowc07d4242011-01-16 13:57:10 -05001490 indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr);
David Dillow8f26c9f2011-01-14 19:45:50 -05001491 indirect_hdr->table_desc.key = cpu_to_be32(target->rkey);
1492 indirect_hdr->table_desc.len = cpu_to_be32(table_len);
1493 indirect_hdr->len = cpu_to_be32(state.total_len);
1494
1495 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
David Dillowc07d4242011-01-16 13:57:10 -05001496 cmd->data_out_desc_cnt = count;
David Dillow8f26c9f2011-01-14 19:45:50 -05001497 else
David Dillowc07d4242011-01-16 13:57:10 -05001498 cmd->data_in_desc_cnt = count;
1499
1500 ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len,
1501 DMA_TO_DEVICE);
David Dillow8f26c9f2011-01-14 19:45:50 -05001502
1503map_complete:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001504 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
1505 cmd->buf_fmt = fmt << 4;
1506 else
1507 cmd->buf_fmt = fmt;
1508
Roland Dreieraef9ec32005-11-02 14:07:13 -08001509 return len;
1510}
1511
David Dillow05a1d752010-10-08 14:48:14 -04001512/*
Bart Van Assche76c75b22010-11-26 14:37:47 -05001513 * Return an IU and possible credit to the free pool
1514 */
1515static void srp_put_tx_iu(struct srp_target_port *target, struct srp_iu *iu,
1516 enum srp_iu_type iu_type)
1517{
1518 unsigned long flags;
1519
Bart Van Asschee9684672010-11-26 15:08:38 -05001520 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001521 list_add(&iu->list, &target->free_tx);
1522 if (iu_type != SRP_IU_RSP)
1523 ++target->req_lim;
Bart Van Asschee9684672010-11-26 15:08:38 -05001524 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001525}
1526
1527/*
Bart Van Asschee9684672010-11-26 15:08:38 -05001528 * Must be called with target->lock held to protect req_lim and free_tx.
1529 * If IU is not sent, it must be returned using srp_put_tx_iu().
David Dillow05a1d752010-10-08 14:48:14 -04001530 *
1531 * Note:
1532 * An upper limit for the number of allocated information units for each
1533 * request type is:
1534 * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues
1535 * more than Scsi_Host.can_queue requests.
1536 * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE.
1537 * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than
1538 * one unanswered SRP request to an initiator.
1539 */
1540static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
1541 enum srp_iu_type iu_type)
1542{
1543 s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE;
1544 struct srp_iu *iu;
1545
1546 srp_send_completion(target->send_cq, target);
1547
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001548 if (list_empty(&target->free_tx))
David Dillow05a1d752010-10-08 14:48:14 -04001549 return NULL;
1550
1551 /* Initiator responses to target requests do not consume credits */
Bart Van Assche76c75b22010-11-26 14:37:47 -05001552 if (iu_type != SRP_IU_RSP) {
1553 if (target->req_lim <= rsv) {
1554 ++target->zero_req_lim;
1555 return NULL;
1556 }
1557
1558 --target->req_lim;
David Dillow05a1d752010-10-08 14:48:14 -04001559 }
1560
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001561 iu = list_first_entry(&target->free_tx, struct srp_iu, list);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001562 list_del(&iu->list);
David Dillow05a1d752010-10-08 14:48:14 -04001563 return iu;
1564}
1565
Bart Van Assche76c75b22010-11-26 14:37:47 -05001566static int srp_post_send(struct srp_target_port *target,
1567 struct srp_iu *iu, int len)
David Dillow05a1d752010-10-08 14:48:14 -04001568{
1569 struct ib_sge list;
1570 struct ib_send_wr wr, *bad_wr;
David Dillow05a1d752010-10-08 14:48:14 -04001571
1572 list.addr = iu->dma;
1573 list.length = len;
David Dillow9af76272010-11-26 15:34:46 -05001574 list.lkey = target->lkey;
David Dillow05a1d752010-10-08 14:48:14 -04001575
1576 wr.next = NULL;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001577 wr.wr_id = (uintptr_t) iu;
David Dillow05a1d752010-10-08 14:48:14 -04001578 wr.sg_list = &list;
1579 wr.num_sge = 1;
1580 wr.opcode = IB_WR_SEND;
1581 wr.send_flags = IB_SEND_SIGNALED;
1582
Bart Van Assche76c75b22010-11-26 14:37:47 -05001583 return ib_post_send(target->qp, &wr, &bad_wr);
David Dillow05a1d752010-10-08 14:48:14 -04001584}
1585
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001586static int srp_post_recv(struct srp_target_port *target, struct srp_iu *iu)
Bart Van Asschec996bb42010-07-30 10:59:05 +00001587{
Bart Van Asschec996bb42010-07-30 10:59:05 +00001588 struct ib_recv_wr wr, *bad_wr;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001589 struct ib_sge list;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001590
1591 list.addr = iu->dma;
1592 list.length = iu->size;
David Dillow9af76272010-11-26 15:34:46 -05001593 list.lkey = target->lkey;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001594
1595 wr.next = NULL;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001596 wr.wr_id = (uintptr_t) iu;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001597 wr.sg_list = &list;
1598 wr.num_sge = 1;
1599
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001600 return ib_post_recv(target->qp, &wr, &bad_wr);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001601}
1602
Roland Dreieraef9ec32005-11-02 14:07:13 -08001603static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
1604{
1605 struct srp_request *req;
1606 struct scsi_cmnd *scmnd;
1607 unsigned long flags;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001608
Roland Dreieraef9ec32005-11-02 14:07:13 -08001609 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
Bart Van Asschee9684672010-11-26 15:08:38 -05001610 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -05001611 target->req_lim += be32_to_cpu(rsp->req_lim_delta);
Bart Van Asschee9684672010-11-26 15:08:38 -05001612 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -05001613
David Dillowf8b6e312010-11-26 13:02:21 -05001614 target->tsk_mgmt_status = -1;
1615 if (be32_to_cpu(rsp->resp_data_len) >= 4)
1616 target->tsk_mgmt_status = rsp->data[3];
1617 complete(&target->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001618 } else {
David Dillowf8b6e312010-11-26 13:02:21 -05001619 req = &target->req_ring[rsp->tag];
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001620 scmnd = srp_claim_req(target, req, NULL, NULL);
Bart Van Assche22032992012-08-14 13:18:53 +00001621 if (!scmnd) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001622 shost_printk(KERN_ERR, target->scsi_host,
1623 "Null scmnd for RSP w/tag %016llx\n",
1624 (unsigned long long) rsp->tag);
Bart Van Assche22032992012-08-14 13:18:53 +00001625
1626 spin_lock_irqsave(&target->lock, flags);
1627 target->req_lim += be32_to_cpu(rsp->req_lim_delta);
1628 spin_unlock_irqrestore(&target->lock, flags);
1629
1630 return;
1631 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001632 scmnd->result = rsp->status;
1633
1634 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
1635 memcpy(scmnd->sense_buffer, rsp->data +
1636 be32_to_cpu(rsp->resp_data_len),
1637 min_t(int, be32_to_cpu(rsp->sense_data_len),
1638 SCSI_SENSE_BUFFERSIZE));
1639 }
1640
Bart Van Asschee7145312014-07-09 15:57:51 +02001641 if (unlikely(rsp->flags & SRP_RSP_FLAG_DIUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001642 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
Bart Van Asschee7145312014-07-09 15:57:51 +02001643 else if (unlikely(rsp->flags & SRP_RSP_FLAG_DIOVER))
1644 scsi_set_resid(scmnd, -be32_to_cpu(rsp->data_in_res_cnt));
1645 else if (unlikely(rsp->flags & SRP_RSP_FLAG_DOUNDER))
1646 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
1647 else if (unlikely(rsp->flags & SRP_RSP_FLAG_DOOVER))
1648 scsi_set_resid(scmnd, -be32_to_cpu(rsp->data_out_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001649
Bart Van Assche22032992012-08-14 13:18:53 +00001650 srp_free_req(target, req, scmnd,
1651 be32_to_cpu(rsp->req_lim_delta));
1652
David Dillowf8b6e312010-11-26 13:02:21 -05001653 scmnd->host_scribble = NULL;
1654 scmnd->scsi_done(scmnd);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001655 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001656}
1657
David Dillowbb125882010-10-08 14:40:47 -04001658static int srp_response_common(struct srp_target_port *target, s32 req_delta,
1659 void *rsp, int len)
1660{
Bart Van Assche76c75b22010-11-26 14:37:47 -05001661 struct ib_device *dev = target->srp_host->srp_dev->dev;
David Dillowbb125882010-10-08 14:40:47 -04001662 unsigned long flags;
1663 struct srp_iu *iu;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001664 int err;
David Dillowbb125882010-10-08 14:40:47 -04001665
Bart Van Asschee9684672010-11-26 15:08:38 -05001666 spin_lock_irqsave(&target->lock, flags);
David Dillowbb125882010-10-08 14:40:47 -04001667 target->req_lim += req_delta;
David Dillowbb125882010-10-08 14:40:47 -04001668 iu = __srp_get_tx_iu(target, SRP_IU_RSP);
Bart Van Asschee9684672010-11-26 15:08:38 -05001669 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001670
David Dillowbb125882010-10-08 14:40:47 -04001671 if (!iu) {
1672 shost_printk(KERN_ERR, target->scsi_host, PFX
1673 "no IU available to send response\n");
Bart Van Assche76c75b22010-11-26 14:37:47 -05001674 return 1;
David Dillowbb125882010-10-08 14:40:47 -04001675 }
1676
1677 ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE);
1678 memcpy(iu->buf, rsp, len);
1679 ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE);
1680
Bart Van Assche76c75b22010-11-26 14:37:47 -05001681 err = srp_post_send(target, iu, len);
1682 if (err) {
David Dillowbb125882010-10-08 14:40:47 -04001683 shost_printk(KERN_ERR, target->scsi_host, PFX
1684 "unable to post response: %d\n", err);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001685 srp_put_tx_iu(target, iu, SRP_IU_RSP);
1686 }
David Dillowbb125882010-10-08 14:40:47 -04001687
David Dillowbb125882010-10-08 14:40:47 -04001688 return err;
1689}
1690
1691static void srp_process_cred_req(struct srp_target_port *target,
1692 struct srp_cred_req *req)
1693{
1694 struct srp_cred_rsp rsp = {
1695 .opcode = SRP_CRED_RSP,
1696 .tag = req->tag,
1697 };
1698 s32 delta = be32_to_cpu(req->req_lim_delta);
1699
1700 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1701 shost_printk(KERN_ERR, target->scsi_host, PFX
1702 "problems processing SRP_CRED_REQ\n");
1703}
1704
1705static void srp_process_aer_req(struct srp_target_port *target,
1706 struct srp_aer_req *req)
1707{
1708 struct srp_aer_rsp rsp = {
1709 .opcode = SRP_AER_RSP,
1710 .tag = req->tag,
1711 };
1712 s32 delta = be32_to_cpu(req->req_lim_delta);
1713
1714 shost_printk(KERN_ERR, target->scsi_host, PFX
1715 "ignoring AER for LUN %llu\n", be64_to_cpu(req->lun));
1716
1717 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1718 shost_printk(KERN_ERR, target->scsi_host, PFX
1719 "problems processing SRP_AER_REQ\n");
1720}
1721
Roland Dreieraef9ec32005-11-02 14:07:13 -08001722static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
1723{
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001724 struct ib_device *dev = target->srp_host->srp_dev->dev;
Roland Dreier737b94e2011-05-23 11:30:04 -07001725 struct srp_iu *iu = (struct srp_iu *) (uintptr_t) wc->wr_id;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001726 int res;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001727 u8 opcode;
1728
Ralph Campbell85507bc2006-12-12 14:30:55 -08001729 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
1730 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001731
1732 opcode = *(u8 *) iu->buf;
1733
1734 if (0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001735 shost_printk(KERN_ERR, target->scsi_host,
1736 PFX "recv completion, opcode 0x%02x\n", opcode);
Bart Van Assche7a700812010-07-29 15:56:37 +00001737 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1,
1738 iu->buf, wc->byte_len, true);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001739 }
1740
1741 switch (opcode) {
1742 case SRP_RSP:
1743 srp_process_rsp(target, iu->buf);
1744 break;
1745
David Dillowbb125882010-10-08 14:40:47 -04001746 case SRP_CRED_REQ:
1747 srp_process_cred_req(target, iu->buf);
1748 break;
1749
1750 case SRP_AER_REQ:
1751 srp_process_aer_req(target, iu->buf);
1752 break;
1753
Roland Dreieraef9ec32005-11-02 14:07:13 -08001754 case SRP_T_LOGOUT:
1755 /* XXX Handle target logout */
David Dillow7aa54bd2008-01-07 18:23:41 -05001756 shost_printk(KERN_WARNING, target->scsi_host,
1757 PFX "Got target logout request\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001758 break;
1759
1760 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001761 shost_printk(KERN_WARNING, target->scsi_host,
1762 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001763 break;
1764 }
1765
Ralph Campbell85507bc2006-12-12 14:30:55 -08001766 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
1767 DMA_FROM_DEVICE);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001768
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001769 res = srp_post_recv(target, iu);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001770 if (res != 0)
1771 shost_printk(KERN_ERR, target->scsi_host,
1772 PFX "Recv failed with error code %d\n", res);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001773}
1774
Bart Van Asschec1120f82013-10-26 14:35:08 +02001775/**
1776 * srp_tl_err_work() - handle a transport layer error
Bart Van Asscheaf246632014-05-20 15:04:21 +02001777 * @work: Work structure embedded in an SRP target port.
Bart Van Asschec1120f82013-10-26 14:35:08 +02001778 *
1779 * Note: This function may get invoked before the rport has been created,
1780 * hence the target->rport test.
1781 */
1782static void srp_tl_err_work(struct work_struct *work)
1783{
1784 struct srp_target_port *target;
1785
1786 target = container_of(work, struct srp_target_port, tl_err_work);
1787 if (target->rport)
1788 srp_start_tl_fail_timers(target->rport);
1789}
1790
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001791static void srp_handle_qp_err(u64 wr_id, enum ib_wc_status wc_status,
1792 bool send_err, struct srp_target_port *target)
Bart Van Assche948d1e82011-09-03 09:25:42 +02001793{
Bart Van Assche294c8752011-12-25 12:18:12 +00001794 if (target->connected && !target->qp_in_error) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001795 if (wr_id & LOCAL_INV_WR_ID_MASK) {
1796 shost_printk(KERN_ERR, target->scsi_host, PFX
1797 "LOCAL_INV failed with status %d\n",
1798 wc_status);
1799 } else if (wr_id & FAST_REG_WR_ID_MASK) {
1800 shost_printk(KERN_ERR, target->scsi_host, PFX
1801 "FAST_REG_MR failed status %d\n",
1802 wc_status);
1803 } else {
1804 shost_printk(KERN_ERR, target->scsi_host,
1805 PFX "failed %s status %d for iu %p\n",
1806 send_err ? "send" : "receive",
1807 wc_status, (void *)(uintptr_t)wr_id);
1808 }
Bart Van Asschec1120f82013-10-26 14:35:08 +02001809 queue_work(system_long_wq, &target->tl_err_work);
Bart Van Assche4f0af692012-11-26 11:16:40 +01001810 }
Bart Van Assche948d1e82011-09-03 09:25:42 +02001811 target->qp_in_error = true;
1812}
1813
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001814static void srp_recv_completion(struct ib_cq *cq, void *target_ptr)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001815{
1816 struct srp_target_port *target = target_ptr;
1817 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001818
1819 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1820 while (ib_poll_cq(cq, 1, &wc) > 0) {
Bart Van Assche948d1e82011-09-03 09:25:42 +02001821 if (likely(wc.status == IB_WC_SUCCESS)) {
1822 srp_handle_recv(target, &wc);
1823 } else {
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001824 srp_handle_qp_err(wc.wr_id, wc.status, false, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001825 }
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001826 }
1827}
1828
1829static void srp_send_completion(struct ib_cq *cq, void *target_ptr)
1830{
1831 struct srp_target_port *target = target_ptr;
1832 struct ib_wc wc;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001833 struct srp_iu *iu;
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001834
1835 while (ib_poll_cq(cq, 1, &wc) > 0) {
Bart Van Assche948d1e82011-09-03 09:25:42 +02001836 if (likely(wc.status == IB_WC_SUCCESS)) {
1837 iu = (struct srp_iu *) (uintptr_t) wc.wr_id;
1838 list_add(&iu->list, &target->free_tx);
1839 } else {
Bart Van Assche5cfb1782014-05-20 15:08:34 +02001840 srp_handle_qp_err(wc.wr_id, wc.status, true, target);
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001841 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001842 }
1843}
1844
Bart Van Assche76c75b22010-11-26 14:37:47 -05001845static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001846{
Bart Van Assche76c75b22010-11-26 14:37:47 -05001847 struct srp_target_port *target = host_to_target(shost);
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001848 struct srp_rport *rport = target->rport;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001849 struct srp_request *req;
1850 struct srp_iu *iu;
1851 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001852 struct ib_device *dev;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001853 unsigned long flags;
Bart Van Assched1b42892014-05-20 15:07:20 +02001854 int len, ret;
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001855 const bool in_scsi_eh = !in_interrupt() && current == shost->ehandler;
1856
1857 /*
1858 * The SCSI EH thread is the only context from which srp_queuecommand()
1859 * can get invoked for blocked devices (SDEV_BLOCK /
1860 * SDEV_CREATED_BLOCK). Avoid racing with srp_reconnect_rport() by
1861 * locking the rport mutex if invoked from inside the SCSI EH.
1862 */
1863 if (in_scsi_eh)
1864 mutex_lock(&rport->mutex);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001865
Bart Van Assched1b42892014-05-20 15:07:20 +02001866 scmnd->result = srp_chkready(target->rport);
1867 if (unlikely(scmnd->result))
1868 goto err;
Bart Van Assche2ce19e72013-02-21 17:20:00 +00001869
Bart Van Asschee9684672010-11-26 15:08:38 -05001870 spin_lock_irqsave(&target->lock, flags);
David Dillowbb125882010-10-08 14:40:47 -04001871 iu = __srp_get_tx_iu(target, SRP_IU_CMD);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001872 if (!iu)
Bart Van Assche695b8342011-01-13 19:02:25 +00001873 goto err_unlock;
1874
1875 req = list_first_entry(&target->free_reqs, struct srp_request, list);
1876 list_del(&req->list);
1877 spin_unlock_irqrestore(&target->lock, flags);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001878
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001879 dev = target->srp_host->srp_dev->dev;
David Dillow49248642011-01-14 18:23:24 -05001880 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08001881 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001882
David Dillowf8b6e312010-11-26 13:02:21 -05001883 scmnd->host_scribble = (void *) req;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001884
1885 cmd = iu->buf;
1886 memset(cmd, 0, sizeof *cmd);
1887
1888 cmd->opcode = SRP_CMD;
1889 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001890 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001891 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1892
Roland Dreieraef9ec32005-11-02 14:07:13 -08001893 req->scmnd = scmnd;
1894 req->cmd = iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001895
1896 len = srp_map_data(scmnd, target, req);
1897 if (len < 0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001898 shost_printk(KERN_ERR, target->scsi_host,
Bart Van Assched1b42892014-05-20 15:07:20 +02001899 PFX "Failed to map data (%d)\n", len);
1900 /*
1901 * If we ran out of memory descriptors (-ENOMEM) because an
1902 * application is queuing many requests with more than
Bart Van Assche52ede082014-05-20 15:07:45 +02001903 * max_pages_per_mr sg-list elements, tell the SCSI mid-layer
Bart Van Assched1b42892014-05-20 15:07:20 +02001904 * to reduce queue depth temporarily.
1905 */
1906 scmnd->result = len == -ENOMEM ?
1907 DID_OK << 16 | QUEUE_FULL << 1 : DID_ERROR << 16;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001908 goto err_iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001909 }
1910
David Dillow49248642011-01-14 18:23:24 -05001911 ib_dma_sync_single_for_device(dev, iu->dma, target->max_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08001912 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001913
Bart Van Assche76c75b22010-11-26 14:37:47 -05001914 if (srp_post_send(target, iu, len)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001915 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001916 goto err_unmap;
1917 }
1918
Bart Van Assched1b42892014-05-20 15:07:20 +02001919 ret = 0;
1920
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001921unlock_rport:
1922 if (in_scsi_eh)
1923 mutex_unlock(&rport->mutex);
1924
Bart Van Assched1b42892014-05-20 15:07:20 +02001925 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001926
1927err_unmap:
1928 srp_unmap_data(scmnd, target, req);
1929
Bart Van Assche76c75b22010-11-26 14:37:47 -05001930err_iu:
1931 srp_put_tx_iu(target, iu, SRP_IU_CMD);
1932
Bart Van Assche024ca902014-05-20 15:03:49 +02001933 /*
1934 * Avoid that the loops that iterate over the request ring can
1935 * encounter a dangling SCSI command pointer.
1936 */
1937 req->scmnd = NULL;
1938
Bart Van Asschee9684672010-11-26 15:08:38 -05001939 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001940 list_add(&req->list, &target->free_reqs);
Bart Van Assche695b8342011-01-13 19:02:25 +00001941
1942err_unlock:
Bart Van Asschee9684672010-11-26 15:08:38 -05001943 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001944
Bart Van Assched1b42892014-05-20 15:07:20 +02001945err:
1946 if (scmnd->result) {
1947 scmnd->scsi_done(scmnd);
1948 ret = 0;
1949 } else {
1950 ret = SCSI_MLQUEUE_HOST_BUSY;
1951 }
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001952
Bart Van Assched1b42892014-05-20 15:07:20 +02001953 goto unlock_rport;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001954}
1955
Bart Van Assche4d73f952013-10-26 14:40:37 +02001956/*
1957 * Note: the resources allocated in this function are freed in
1958 * srp_free_target_ib().
1959 */
Roland Dreieraef9ec32005-11-02 14:07:13 -08001960static int srp_alloc_iu_bufs(struct srp_target_port *target)
1961{
1962 int i;
1963
Bart Van Assche4d73f952013-10-26 14:40:37 +02001964 target->rx_ring = kzalloc(target->queue_size * sizeof(*target->rx_ring),
1965 GFP_KERNEL);
1966 if (!target->rx_ring)
1967 goto err_no_ring;
1968 target->tx_ring = kzalloc(target->queue_size * sizeof(*target->tx_ring),
1969 GFP_KERNEL);
1970 if (!target->tx_ring)
1971 goto err_no_ring;
1972
1973 for (i = 0; i < target->queue_size; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001974 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1975 target->max_ti_iu_len,
1976 GFP_KERNEL, DMA_FROM_DEVICE);
1977 if (!target->rx_ring[i])
1978 goto err;
1979 }
1980
Bart Van Assche4d73f952013-10-26 14:40:37 +02001981 for (i = 0; i < target->queue_size; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001982 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
David Dillow49248642011-01-14 18:23:24 -05001983 target->max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001984 GFP_KERNEL, DMA_TO_DEVICE);
1985 if (!target->tx_ring[i])
1986 goto err;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001987
1988 list_add(&target->tx_ring[i]->list, &target->free_tx);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001989 }
1990
1991 return 0;
1992
1993err:
Bart Van Assche4d73f952013-10-26 14:40:37 +02001994 for (i = 0; i < target->queue_size; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001995 srp_free_iu(target->srp_host, target->rx_ring[i]);
Bart Van Assche4d73f952013-10-26 14:40:37 +02001996 srp_free_iu(target->srp_host, target->tx_ring[i]);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001997 }
1998
Bart Van Assche4d73f952013-10-26 14:40:37 +02001999
2000err_no_ring:
2001 kfree(target->tx_ring);
2002 target->tx_ring = NULL;
2003 kfree(target->rx_ring);
2004 target->rx_ring = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002005
2006 return -ENOMEM;
2007}
2008
Bart Van Asschec9b03c12011-09-03 09:34:48 +02002009static uint32_t srp_compute_rq_tmo(struct ib_qp_attr *qp_attr, int attr_mask)
2010{
2011 uint64_t T_tr_ns, max_compl_time_ms;
2012 uint32_t rq_tmo_jiffies;
2013
2014 /*
2015 * According to section 11.2.4.2 in the IBTA spec (Modify Queue Pair,
2016 * table 91), both the QP timeout and the retry count have to be set
2017 * for RC QP's during the RTR to RTS transition.
2018 */
2019 WARN_ON_ONCE((attr_mask & (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)) !=
2020 (IB_QP_TIMEOUT | IB_QP_RETRY_CNT));
2021
2022 /*
2023 * Set target->rq_tmo_jiffies to one second more than the largest time
2024 * it can take before an error completion is generated. See also
2025 * C9-140..142 in the IBTA spec for more information about how to
2026 * convert the QP Local ACK Timeout value to nanoseconds.
2027 */
2028 T_tr_ns = 4096 * (1ULL << qp_attr->timeout);
2029 max_compl_time_ms = qp_attr->retry_cnt * 4 * T_tr_ns;
2030 do_div(max_compl_time_ms, NSEC_PER_MSEC);
2031 rq_tmo_jiffies = msecs_to_jiffies(max_compl_time_ms + 1000);
2032
2033 return rq_tmo_jiffies;
2034}
2035
David Dillow961e0be2011-01-14 17:32:07 -05002036static void srp_cm_rep_handler(struct ib_cm_id *cm_id,
2037 struct srp_login_rsp *lrsp,
2038 struct srp_target_port *target)
2039{
2040 struct ib_qp_attr *qp_attr = NULL;
2041 int attr_mask = 0;
2042 int ret;
2043 int i;
2044
2045 if (lrsp->opcode == SRP_LOGIN_RSP) {
2046 target->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len);
2047 target->req_lim = be32_to_cpu(lrsp->req_lim_delta);
2048
2049 /*
2050 * Reserve credits for task management so we don't
2051 * bounce requests back to the SCSI mid-layer.
2052 */
2053 target->scsi_host->can_queue
2054 = min(target->req_lim - SRP_TSK_MGMT_SQ_SIZE,
2055 target->scsi_host->can_queue);
Bart Van Assche4d73f952013-10-26 14:40:37 +02002056 target->scsi_host->cmd_per_lun
2057 = min_t(int, target->scsi_host->can_queue,
2058 target->scsi_host->cmd_per_lun);
David Dillow961e0be2011-01-14 17:32:07 -05002059 } else {
2060 shost_printk(KERN_WARNING, target->scsi_host,
2061 PFX "Unhandled RSP opcode %#x\n", lrsp->opcode);
2062 ret = -ECONNRESET;
2063 goto error;
2064 }
2065
Bart Van Assche4d73f952013-10-26 14:40:37 +02002066 if (!target->rx_ring) {
David Dillow961e0be2011-01-14 17:32:07 -05002067 ret = srp_alloc_iu_bufs(target);
2068 if (ret)
2069 goto error;
2070 }
2071
2072 ret = -ENOMEM;
2073 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
2074 if (!qp_attr)
2075 goto error;
2076
2077 qp_attr->qp_state = IB_QPS_RTR;
2078 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
2079 if (ret)
2080 goto error_free;
2081
2082 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
2083 if (ret)
2084 goto error_free;
2085
Bart Van Assche4d73f952013-10-26 14:40:37 +02002086 for (i = 0; i < target->queue_size; i++) {
David Dillow961e0be2011-01-14 17:32:07 -05002087 struct srp_iu *iu = target->rx_ring[i];
2088 ret = srp_post_recv(target, iu);
2089 if (ret)
2090 goto error_free;
2091 }
2092
2093 qp_attr->qp_state = IB_QPS_RTS;
2094 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
2095 if (ret)
2096 goto error_free;
2097
Bart Van Asschec9b03c12011-09-03 09:34:48 +02002098 target->rq_tmo_jiffies = srp_compute_rq_tmo(qp_attr, attr_mask);
2099
David Dillow961e0be2011-01-14 17:32:07 -05002100 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
2101 if (ret)
2102 goto error_free;
2103
2104 ret = ib_send_cm_rtu(cm_id, NULL, 0);
2105
2106error_free:
2107 kfree(qp_attr);
2108
2109error:
2110 target->status = ret;
2111}
2112
Roland Dreieraef9ec32005-11-02 14:07:13 -08002113static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
2114 struct ib_cm_event *event,
2115 struct srp_target_port *target)
2116{
David Dillow7aa54bd2008-01-07 18:23:41 -05002117 struct Scsi_Host *shost = target->scsi_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002118 struct ib_class_port_info *cpi;
2119 int opcode;
2120
2121 switch (event->param.rej_rcvd.reason) {
2122 case IB_CM_REJ_PORT_CM_REDIRECT:
2123 cpi = event->param.rej_rcvd.ari;
2124 target->path.dlid = cpi->redirect_lid;
2125 target->path.pkey = cpi->redirect_pkey;
2126 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
2127 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
2128
2129 target->status = target->path.dlid ?
2130 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
2131 break;
2132
2133 case IB_CM_REJ_PORT_REDIRECT:
Roland Dreier5d7cbfd2007-08-03 10:45:18 -07002134 if (srp_target_is_topspin(target)) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002135 /*
2136 * Topspin/Cisco SRP gateways incorrectly send
2137 * reject reason code 25 when they mean 24
2138 * (port redirect).
2139 */
2140 memcpy(target->path.dgid.raw,
2141 event->param.rej_rcvd.ari, 16);
2142
David Dillow7aa54bd2008-01-07 18:23:41 -05002143 shost_printk(KERN_DEBUG, shost,
2144 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
2145 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
2146 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
Roland Dreieraef9ec32005-11-02 14:07:13 -08002147
2148 target->status = SRP_PORT_REDIRECT;
2149 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05002150 shost_printk(KERN_WARNING, shost,
2151 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002152 target->status = -ECONNRESET;
2153 }
2154 break;
2155
2156 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
David Dillow7aa54bd2008-01-07 18:23:41 -05002157 shost_printk(KERN_WARNING, shost,
2158 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002159 target->status = -ECONNRESET;
2160 break;
2161
2162 case IB_CM_REJ_CONSUMER_DEFINED:
2163 opcode = *(u8 *) event->private_data;
2164 if (opcode == SRP_LOGIN_REJ) {
2165 struct srp_login_rej *rej = event->private_data;
2166 u32 reason = be32_to_cpu(rej->reason);
2167
2168 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
David Dillow7aa54bd2008-01-07 18:23:41 -05002169 shost_printk(KERN_WARNING, shost,
2170 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002171 else
Bart Van Asschee7ffde02014-03-14 13:52:21 +01002172 shost_printk(KERN_WARNING, shost, PFX
2173 "SRP LOGIN from %pI6 to %pI6 REJECTED, reason 0x%08x\n",
2174 target->path.sgid.raw,
2175 target->orig_dgid, reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002176 } else
David Dillow7aa54bd2008-01-07 18:23:41 -05002177 shost_printk(KERN_WARNING, shost,
2178 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
2179 " opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002180 target->status = -ECONNRESET;
2181 break;
2182
David Dillow9fe4bcf2008-01-08 17:08:52 -05002183 case IB_CM_REJ_STALE_CONN:
2184 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
2185 target->status = SRP_STALE_CONN;
2186 break;
2187
Roland Dreieraef9ec32005-11-02 14:07:13 -08002188 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05002189 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
2190 event->param.rej_rcvd.reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002191 target->status = -ECONNRESET;
2192 }
2193}
2194
2195static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2196{
2197 struct srp_target_port *target = cm_id->context;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002198 int comp = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002199
2200 switch (event->event) {
2201 case IB_CM_REQ_ERROR:
David Dillow7aa54bd2008-01-07 18:23:41 -05002202 shost_printk(KERN_DEBUG, target->scsi_host,
2203 PFX "Sending CM REQ failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002204 comp = 1;
2205 target->status = -ECONNRESET;
2206 break;
2207
2208 case IB_CM_REP_RECEIVED:
2209 comp = 1;
David Dillow961e0be2011-01-14 17:32:07 -05002210 srp_cm_rep_handler(cm_id, event->private_data, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002211 break;
2212
2213 case IB_CM_REJ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05002214 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002215 comp = 1;
2216
2217 srp_cm_rej_handler(cm_id, event, target);
2218 break;
2219
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07002220 case IB_CM_DREQ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05002221 shost_printk(KERN_WARNING, target->scsi_host,
2222 PFX "DREQ received - connection closed\n");
Bart Van Assche294c8752011-12-25 12:18:12 +00002223 srp_change_conn_state(target, false);
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07002224 if (ib_send_cm_drep(cm_id, NULL, 0))
David Dillow7aa54bd2008-01-07 18:23:41 -05002225 shost_printk(KERN_ERR, target->scsi_host,
2226 PFX "Sending CM DREP failed\n");
Bart Van Asschec1120f82013-10-26 14:35:08 +02002227 queue_work(system_long_wq, &target->tl_err_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002228 break;
2229
2230 case IB_CM_TIMEWAIT_EXIT:
David Dillow7aa54bd2008-01-07 18:23:41 -05002231 shost_printk(KERN_ERR, target->scsi_host,
2232 PFX "connection closed\n");
Bart Van Asscheac72d762014-03-14 13:53:40 +01002233 comp = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002234
Roland Dreieraef9ec32005-11-02 14:07:13 -08002235 target->status = 0;
2236 break;
2237
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07002238 case IB_CM_MRA_RECEIVED:
2239 case IB_CM_DREQ_ERROR:
2240 case IB_CM_DREP_RECEIVED:
2241 break;
2242
Roland Dreieraef9ec32005-11-02 14:07:13 -08002243 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05002244 shost_printk(KERN_WARNING, target->scsi_host,
2245 PFX "Unhandled CM event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002246 break;
2247 }
2248
2249 if (comp)
2250 complete(&target->done);
2251
Roland Dreieraef9ec32005-11-02 14:07:13 -08002252 return 0;
2253}
2254
Jack Wang71444b92013-11-07 11:37:37 +01002255/**
Jack Wang71444b92013-11-07 11:37:37 +01002256 * srp_change_queue_depth - setting device queue depth
2257 * @sdev: scsi device struct
2258 * @qdepth: requested queue depth
2259 * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
2260 * (see include/scsi/scsi_host.h for definition)
2261 *
2262 * Returns queue depth.
2263 */
2264static int
2265srp_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
2266{
2267 struct Scsi_Host *shost = sdev->host;
2268 int max_depth;
2269 if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP) {
2270 max_depth = shost->can_queue;
2271 if (!sdev->tagged_supported)
2272 max_depth = 1;
2273 if (qdepth > max_depth)
2274 qdepth = max_depth;
Christoph Hellwigc8b09f62014-11-03 20:15:14 +01002275 scsi_adjust_queue_depth(sdev, qdepth);
Jack Wang71444b92013-11-07 11:37:37 +01002276 } else if (reason == SCSI_QDEPTH_QFULL)
2277 scsi_track_queue_full(sdev, qdepth);
2278 else
2279 return -EOPNOTSUPP;
2280
2281 return sdev->queue_depth;
2282}
2283
Roland Dreierd945e1d2006-05-09 10:50:28 -07002284static int srp_send_tsk_mgmt(struct srp_target_port *target,
David Dillowf8b6e312010-11-26 13:02:21 -05002285 u64 req_tag, unsigned int lun, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002286{
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002287 struct srp_rport *rport = target->rport;
David Dillow19081f32010-10-18 08:54:49 -04002288 struct ib_device *dev = target->srp_host->srp_dev->dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002289 struct srp_iu *iu;
2290 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002291
Bart Van Assche3780d1f2013-02-21 17:18:00 +00002292 if (!target->connected || target->qp_in_error)
2293 return -1;
2294
David Dillowf8b6e312010-11-26 13:02:21 -05002295 init_completion(&target->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002296
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002297 /*
2298 * Lock the rport mutex to avoid that srp_create_target_ib() is
2299 * invoked while a task management function is being sent.
2300 */
2301 mutex_lock(&rport->mutex);
Bart Van Asschee9684672010-11-26 15:08:38 -05002302 spin_lock_irq(&target->lock);
David Dillowbb125882010-10-08 14:40:47 -04002303 iu = __srp_get_tx_iu(target, SRP_IU_TSK_MGMT);
Bart Van Asschee9684672010-11-26 15:08:38 -05002304 spin_unlock_irq(&target->lock);
Bart Van Assche76c75b22010-11-26 14:37:47 -05002305
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002306 if (!iu) {
2307 mutex_unlock(&rport->mutex);
2308
Bart Van Assche76c75b22010-11-26 14:37:47 -05002309 return -1;
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002310 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002311
David Dillow19081f32010-10-18 08:54:49 -04002312 ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt,
2313 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002314 tsk_mgmt = iu->buf;
2315 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
2316
2317 tsk_mgmt->opcode = SRP_TSK_MGMT;
David Dillowf8b6e312010-11-26 13:02:21 -05002318 tsk_mgmt->lun = cpu_to_be64((u64) lun << 48);
2319 tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002320 tsk_mgmt->tsk_mgmt_func = func;
David Dillowf8b6e312010-11-26 13:02:21 -05002321 tsk_mgmt->task_tag = req_tag;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002322
David Dillow19081f32010-10-18 08:54:49 -04002323 ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt,
2324 DMA_TO_DEVICE);
Bart Van Assche76c75b22010-11-26 14:37:47 -05002325 if (srp_post_send(target, iu, sizeof *tsk_mgmt)) {
2326 srp_put_tx_iu(target, iu, SRP_IU_TSK_MGMT);
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002327 mutex_unlock(&rport->mutex);
2328
Bart Van Assche76c75b22010-11-26 14:37:47 -05002329 return -1;
2330 }
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002331 mutex_unlock(&rport->mutex);
Roland Dreierd945e1d2006-05-09 10:50:28 -07002332
David Dillowf8b6e312010-11-26 13:02:21 -05002333 if (!wait_for_completion_timeout(&target->tsk_mgmt_done,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002334 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07002335 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002336
Roland Dreierd945e1d2006-05-09 10:50:28 -07002337 return 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07002338}
2339
Roland Dreieraef9ec32005-11-02 14:07:13 -08002340static int srp_abort(struct scsi_cmnd *scmnd)
2341{
Roland Dreierd945e1d2006-05-09 10:50:28 -07002342 struct srp_target_port *target = host_to_target(scmnd->device->host);
David Dillowf8b6e312010-11-26 13:02:21 -05002343 struct srp_request *req = (struct srp_request *) scmnd->host_scribble;
Bart Van Assche086f44f2013-06-12 15:23:04 +02002344 int ret;
Roland Dreierd945e1d2006-05-09 10:50:28 -07002345
David Dillow7aa54bd2008-01-07 18:23:41 -05002346 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002347
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01002348 if (!req || !srp_claim_req(target, req, NULL, scmnd))
Bart Van Assche99b66972013-10-10 13:52:33 +02002349 return SUCCESS;
Bart Van Assche086f44f2013-06-12 15:23:04 +02002350 if (srp_send_tsk_mgmt(target, req->index, scmnd->device->lun,
Bart Van Assche80d5e8a2013-07-10 17:36:35 +02002351 SRP_TSK_ABORT_TASK) == 0)
Bart Van Assche086f44f2013-06-12 15:23:04 +02002352 ret = SUCCESS;
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002353 else if (target->rport->state == SRP_RPORT_LOST)
Bart Van Assche99e1c132013-06-28 14:49:58 +02002354 ret = FAST_IO_FAIL;
Bart Van Assche086f44f2013-06-12 15:23:04 +02002355 else
2356 ret = FAILED;
Bart Van Assche22032992012-08-14 13:18:53 +00002357 srp_free_req(target, req, scmnd, 0);
2358 scmnd->result = DID_ABORT << 16;
Bart Van Assched8536672012-08-24 10:29:11 +00002359 scmnd->scsi_done(scmnd);
Roland Dreierd945e1d2006-05-09 10:50:28 -07002360
Bart Van Assche086f44f2013-06-12 15:23:04 +02002361 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002362}
2363
2364static int srp_reset_device(struct scsi_cmnd *scmnd)
2365{
Roland Dreierd945e1d2006-05-09 10:50:28 -07002366 struct srp_target_port *target = host_to_target(scmnd->device->host);
Bart Van Assche536ae142010-11-26 13:58:27 -05002367 int i;
Roland Dreierd945e1d2006-05-09 10:50:28 -07002368
David Dillow7aa54bd2008-01-07 18:23:41 -05002369 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002370
David Dillowf8b6e312010-11-26 13:02:21 -05002371 if (srp_send_tsk_mgmt(target, SRP_TAG_NO_REQ, scmnd->device->lun,
2372 SRP_TSK_LUN_RESET))
Roland Dreierd945e1d2006-05-09 10:50:28 -07002373 return FAILED;
David Dillowf8b6e312010-11-26 13:02:21 -05002374 if (target->tsk_mgmt_status)
Roland Dreierd945e1d2006-05-09 10:50:28 -07002375 return FAILED;
2376
Bart Van Assche4d73f952013-10-26 14:40:37 +02002377 for (i = 0; i < target->req_ring_size; ++i) {
Bart Van Assche536ae142010-11-26 13:58:27 -05002378 struct srp_request *req = &target->req_ring[i];
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01002379 srp_finish_req(target, req, scmnd->device, DID_RESET << 16);
Bart Van Assche536ae142010-11-26 13:58:27 -05002380 }
Roland Dreierd945e1d2006-05-09 10:50:28 -07002381
Roland Dreierd945e1d2006-05-09 10:50:28 -07002382 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002383}
2384
2385static int srp_reset_host(struct scsi_cmnd *scmnd)
2386{
2387 struct srp_target_port *target = host_to_target(scmnd->device->host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002388
David Dillow7aa54bd2008-01-07 18:23:41 -05002389 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002390
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002391 return srp_reconnect_rport(target->rport) == 0 ? SUCCESS : FAILED;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002392}
2393
Bart Van Asschec9b03c12011-09-03 09:34:48 +02002394static int srp_slave_configure(struct scsi_device *sdev)
2395{
2396 struct Scsi_Host *shost = sdev->host;
2397 struct srp_target_port *target = host_to_target(shost);
2398 struct request_queue *q = sdev->request_queue;
2399 unsigned long timeout;
2400
2401 if (sdev->type == TYPE_DISK) {
2402 timeout = max_t(unsigned, 30 * HZ, target->rq_tmo_jiffies);
2403 blk_queue_rq_timeout(q, timeout);
2404 }
2405
2406 return 0;
2407}
2408
Tony Jonesee959b02008-02-22 00:13:36 +01002409static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
2410 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002411{
Tony Jonesee959b02008-02-22 00:13:36 +01002412 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002413
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002414 return sprintf(buf, "0x%016llx\n",
2415 (unsigned long long) be64_to_cpu(target->id_ext));
2416}
2417
Tony Jonesee959b02008-02-22 00:13:36 +01002418static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
2419 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002420{
Tony Jonesee959b02008-02-22 00:13:36 +01002421 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002422
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002423 return sprintf(buf, "0x%016llx\n",
2424 (unsigned long long) be64_to_cpu(target->ioc_guid));
2425}
2426
Tony Jonesee959b02008-02-22 00:13:36 +01002427static ssize_t show_service_id(struct device *dev,
2428 struct device_attribute *attr, char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002429{
Tony Jonesee959b02008-02-22 00:13:36 +01002430 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002431
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002432 return sprintf(buf, "0x%016llx\n",
2433 (unsigned long long) be64_to_cpu(target->service_id));
2434}
2435
Tony Jonesee959b02008-02-22 00:13:36 +01002436static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
2437 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002438{
Tony Jonesee959b02008-02-22 00:13:36 +01002439 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002440
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002441 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
2442}
2443
Bart Van Assche848b3082013-10-26 14:38:12 +02002444static ssize_t show_sgid(struct device *dev, struct device_attribute *attr,
2445 char *buf)
2446{
2447 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2448
2449 return sprintf(buf, "%pI6\n", target->path.sgid.raw);
2450}
2451
Tony Jonesee959b02008-02-22 00:13:36 +01002452static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
2453 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002454{
Tony Jonesee959b02008-02-22 00:13:36 +01002455 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002456
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002457 return sprintf(buf, "%pI6\n", target->path.dgid.raw);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002458}
2459
Tony Jonesee959b02008-02-22 00:13:36 +01002460static ssize_t show_orig_dgid(struct device *dev,
2461 struct device_attribute *attr, char *buf)
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002462{
Tony Jonesee959b02008-02-22 00:13:36 +01002463 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002464
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002465 return sprintf(buf, "%pI6\n", target->orig_dgid);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002466}
2467
Bart Van Assche89de7482010-08-03 14:08:45 +00002468static ssize_t show_req_lim(struct device *dev,
2469 struct device_attribute *attr, char *buf)
2470{
2471 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2472
Bart Van Assche89de7482010-08-03 14:08:45 +00002473 return sprintf(buf, "%d\n", target->req_lim);
2474}
2475
Tony Jonesee959b02008-02-22 00:13:36 +01002476static ssize_t show_zero_req_lim(struct device *dev,
2477 struct device_attribute *attr, char *buf)
Roland Dreier6bfa24f2006-06-17 20:37:33 -07002478{
Tony Jonesee959b02008-02-22 00:13:36 +01002479 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6bfa24f2006-06-17 20:37:33 -07002480
Roland Dreier6bfa24f2006-06-17 20:37:33 -07002481 return sprintf(buf, "%d\n", target->zero_req_lim);
2482}
2483
Tony Jonesee959b02008-02-22 00:13:36 +01002484static ssize_t show_local_ib_port(struct device *dev,
2485 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002486{
Tony Jonesee959b02008-02-22 00:13:36 +01002487 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002488
2489 return sprintf(buf, "%d\n", target->srp_host->port);
2490}
2491
Tony Jonesee959b02008-02-22 00:13:36 +01002492static ssize_t show_local_ib_device(struct device *dev,
2493 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002494{
Tony Jonesee959b02008-02-22 00:13:36 +01002495 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002496
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002497 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002498}
2499
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002500static ssize_t show_comp_vector(struct device *dev,
2501 struct device_attribute *attr, char *buf)
2502{
2503 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2504
2505 return sprintf(buf, "%d\n", target->comp_vector);
2506}
2507
Vu Pham7bb312e2013-10-26 14:31:27 +02002508static ssize_t show_tl_retry_count(struct device *dev,
2509 struct device_attribute *attr, char *buf)
2510{
2511 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2512
2513 return sprintf(buf, "%d\n", target->tl_retry_count);
2514}
2515
David Dillow49248642011-01-14 18:23:24 -05002516static ssize_t show_cmd_sg_entries(struct device *dev,
2517 struct device_attribute *attr, char *buf)
2518{
2519 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2520
2521 return sprintf(buf, "%u\n", target->cmd_sg_cnt);
2522}
2523
David Dillowc07d4242011-01-16 13:57:10 -05002524static ssize_t show_allow_ext_sg(struct device *dev,
2525 struct device_attribute *attr, char *buf)
2526{
2527 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2528
2529 return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false");
2530}
2531
Tony Jonesee959b02008-02-22 00:13:36 +01002532static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
2533static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
2534static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
2535static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
Bart Van Assche848b3082013-10-26 14:38:12 +02002536static DEVICE_ATTR(sgid, S_IRUGO, show_sgid, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01002537static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
2538static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
Bart Van Assche89de7482010-08-03 14:08:45 +00002539static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01002540static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
2541static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
2542static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002543static DEVICE_ATTR(comp_vector, S_IRUGO, show_comp_vector, NULL);
Vu Pham7bb312e2013-10-26 14:31:27 +02002544static DEVICE_ATTR(tl_retry_count, S_IRUGO, show_tl_retry_count, NULL);
David Dillow49248642011-01-14 18:23:24 -05002545static DEVICE_ATTR(cmd_sg_entries, S_IRUGO, show_cmd_sg_entries, NULL);
David Dillowc07d4242011-01-16 13:57:10 -05002546static DEVICE_ATTR(allow_ext_sg, S_IRUGO, show_allow_ext_sg, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002547
Tony Jonesee959b02008-02-22 00:13:36 +01002548static struct device_attribute *srp_host_attrs[] = {
2549 &dev_attr_id_ext,
2550 &dev_attr_ioc_guid,
2551 &dev_attr_service_id,
2552 &dev_attr_pkey,
Bart Van Assche848b3082013-10-26 14:38:12 +02002553 &dev_attr_sgid,
Tony Jonesee959b02008-02-22 00:13:36 +01002554 &dev_attr_dgid,
2555 &dev_attr_orig_dgid,
Bart Van Assche89de7482010-08-03 14:08:45 +00002556 &dev_attr_req_lim,
Tony Jonesee959b02008-02-22 00:13:36 +01002557 &dev_attr_zero_req_lim,
2558 &dev_attr_local_ib_port,
2559 &dev_attr_local_ib_device,
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002560 &dev_attr_comp_vector,
Vu Pham7bb312e2013-10-26 14:31:27 +02002561 &dev_attr_tl_retry_count,
David Dillow49248642011-01-14 18:23:24 -05002562 &dev_attr_cmd_sg_entries,
David Dillowc07d4242011-01-16 13:57:10 -05002563 &dev_attr_allow_ext_sg,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002564 NULL
2565};
2566
Roland Dreieraef9ec32005-11-02 14:07:13 -08002567static struct scsi_host_template srp_template = {
2568 .module = THIS_MODULE,
Roland Dreierb7f008f2007-05-06 21:18:11 -07002569 .name = "InfiniBand SRP initiator",
2570 .proc_name = DRV_NAME,
Bart Van Asschec9b03c12011-09-03 09:34:48 +02002571 .slave_configure = srp_slave_configure,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002572 .info = srp_target_info,
2573 .queuecommand = srp_queuecommand,
Jack Wang71444b92013-11-07 11:37:37 +01002574 .change_queue_depth = srp_change_queue_depth,
Christoph Hellwiga62182f2014-10-02 14:39:55 +02002575 .change_queue_type = scsi_change_queue_type,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002576 .eh_abort_handler = srp_abort,
2577 .eh_device_reset_handler = srp_reset_device,
2578 .eh_host_reset_handler = srp_reset_host,
Bart Van Assche2742c1d2013-06-12 15:24:25 +02002579 .skip_settle_delay = true,
David Dillow49248642011-01-14 18:23:24 -05002580 .sg_tablesize = SRP_DEF_SG_TABLESIZE,
Bart Van Assche4d73f952013-10-26 14:40:37 +02002581 .can_queue = SRP_DEFAULT_CMD_SQ_SIZE,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002582 .this_id = -1,
Bart Van Assche4d73f952013-10-26 14:40:37 +02002583 .cmd_per_lun = SRP_DEFAULT_CMD_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002584 .use_clustering = ENABLE_CLUSTERING,
2585 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08002586};
2587
2588static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
2589{
FUJITA Tomonori32368222007-06-27 16:33:12 +09002590 struct srp_rport_identifiers ids;
2591 struct srp_rport *rport;
2592
Roland Dreieraef9ec32005-11-02 14:07:13 -08002593 sprintf(target->target_name, "SRP.T10:%016llX",
2594 (unsigned long long) be64_to_cpu(target->id_ext));
2595
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002596 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002597 return -ENODEV;
2598
FUJITA Tomonori32368222007-06-27 16:33:12 +09002599 memcpy(ids.port_id, &target->id_ext, 8);
2600 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09002601 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori32368222007-06-27 16:33:12 +09002602 rport = srp_rport_add(target->scsi_host, &ids);
2603 if (IS_ERR(rport)) {
2604 scsi_remove_host(target->scsi_host);
2605 return PTR_ERR(rport);
2606 }
2607
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02002608 rport->lld_data = target;
Bart Van Assche9dd69a62013-10-26 14:32:30 +02002609 target->rport = rport;
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02002610
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002611 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002612 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002613 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002614
2615 target->state = SRP_TARGET_LIVE;
2616
Roland Dreieraef9ec32005-11-02 14:07:13 -08002617 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07002618 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002619
2620 return 0;
2621}
2622
Tony Jonesee959b02008-02-22 00:13:36 +01002623static void srp_release_dev(struct device *dev)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002624{
2625 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01002626 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002627
2628 complete(&host->released);
2629}
2630
2631static struct class srp_class = {
2632 .name = "infiniband_srp",
Tony Jonesee959b02008-02-22 00:13:36 +01002633 .dev_release = srp_release_dev
Roland Dreieraef9ec32005-11-02 14:07:13 -08002634};
2635
Bart Van Assche96fc2482013-06-28 14:51:26 +02002636/**
2637 * srp_conn_unique() - check whether the connection to a target is unique
Bart Van Asscheaf246632014-05-20 15:04:21 +02002638 * @host: SRP host.
2639 * @target: SRP target port.
Bart Van Assche96fc2482013-06-28 14:51:26 +02002640 */
2641static bool srp_conn_unique(struct srp_host *host,
2642 struct srp_target_port *target)
2643{
2644 struct srp_target_port *t;
2645 bool ret = false;
2646
2647 if (target->state == SRP_TARGET_REMOVED)
2648 goto out;
2649
2650 ret = true;
2651
2652 spin_lock(&host->target_lock);
2653 list_for_each_entry(t, &host->target_list, list) {
2654 if (t != target &&
2655 target->id_ext == t->id_ext &&
2656 target->ioc_guid == t->ioc_guid &&
2657 target->initiator_ext == t->initiator_ext) {
2658 ret = false;
2659 break;
2660 }
2661 }
2662 spin_unlock(&host->target_lock);
2663
2664out:
2665 return ret;
2666}
2667
Roland Dreieraef9ec32005-11-02 14:07:13 -08002668/*
2669 * Target ports are added by writing
2670 *
2671 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
2672 * pkey=<P_Key>,service_id=<service ID>
2673 *
2674 * to the add_target sysfs attribute.
2675 */
2676enum {
2677 SRP_OPT_ERR = 0,
2678 SRP_OPT_ID_EXT = 1 << 0,
2679 SRP_OPT_IOC_GUID = 1 << 1,
2680 SRP_OPT_DGID = 1 << 2,
2681 SRP_OPT_PKEY = 1 << 3,
2682 SRP_OPT_SERVICE_ID = 1 << 4,
2683 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07002684 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07002685 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002686 SRP_OPT_INITIATOR_EXT = 1 << 8,
David Dillow49248642011-01-14 18:23:24 -05002687 SRP_OPT_CMD_SG_ENTRIES = 1 << 9,
David Dillowc07d4242011-01-16 13:57:10 -05002688 SRP_OPT_ALLOW_EXT_SG = 1 << 10,
2689 SRP_OPT_SG_TABLESIZE = 1 << 11,
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002690 SRP_OPT_COMP_VECTOR = 1 << 12,
Vu Pham7bb312e2013-10-26 14:31:27 +02002691 SRP_OPT_TL_RETRY_COUNT = 1 << 13,
Bart Van Assche4d73f952013-10-26 14:40:37 +02002692 SRP_OPT_QUEUE_SIZE = 1 << 14,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002693 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
2694 SRP_OPT_IOC_GUID |
2695 SRP_OPT_DGID |
2696 SRP_OPT_PKEY |
2697 SRP_OPT_SERVICE_ID),
2698};
2699
Steven Whitehousea447c092008-10-13 10:46:57 +01002700static const match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07002701 { SRP_OPT_ID_EXT, "id_ext=%s" },
2702 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
2703 { SRP_OPT_DGID, "dgid=%s" },
2704 { SRP_OPT_PKEY, "pkey=%x" },
2705 { SRP_OPT_SERVICE_ID, "service_id=%s" },
2706 { SRP_OPT_MAX_SECT, "max_sect=%d" },
2707 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07002708 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002709 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
David Dillow49248642011-01-14 18:23:24 -05002710 { SRP_OPT_CMD_SG_ENTRIES, "cmd_sg_entries=%u" },
David Dillowc07d4242011-01-16 13:57:10 -05002711 { SRP_OPT_ALLOW_EXT_SG, "allow_ext_sg=%u" },
2712 { SRP_OPT_SG_TABLESIZE, "sg_tablesize=%u" },
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002713 { SRP_OPT_COMP_VECTOR, "comp_vector=%u" },
Vu Pham7bb312e2013-10-26 14:31:27 +02002714 { SRP_OPT_TL_RETRY_COUNT, "tl_retry_count=%u" },
Bart Van Assche4d73f952013-10-26 14:40:37 +02002715 { SRP_OPT_QUEUE_SIZE, "queue_size=%d" },
Vu Pham52fb2b502006-06-17 20:37:31 -07002716 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002717};
2718
2719static int srp_parse_options(const char *buf, struct srp_target_port *target)
2720{
2721 char *options, *sep_opt;
2722 char *p;
2723 char dgid[3];
2724 substring_t args[MAX_OPT_ARGS];
2725 int opt_mask = 0;
2726 int token;
2727 int ret = -EINVAL;
2728 int i;
2729
2730 options = kstrdup(buf, GFP_KERNEL);
2731 if (!options)
2732 return -ENOMEM;
2733
2734 sep_opt = options;
2735 while ((p = strsep(&sep_opt, ",")) != NULL) {
2736 if (!*p)
2737 continue;
2738
2739 token = match_token(p, srp_opt_tokens, args);
2740 opt_mask |= token;
2741
2742 switch (token) {
2743 case SRP_OPT_ID_EXT:
2744 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002745 if (!p) {
2746 ret = -ENOMEM;
2747 goto out;
2748 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002749 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2750 kfree(p);
2751 break;
2752
2753 case SRP_OPT_IOC_GUID:
2754 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002755 if (!p) {
2756 ret = -ENOMEM;
2757 goto out;
2758 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002759 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
2760 kfree(p);
2761 break;
2762
2763 case SRP_OPT_DGID:
2764 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002765 if (!p) {
2766 ret = -ENOMEM;
2767 goto out;
2768 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002769 if (strlen(p) != 32) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002770 pr_warn("bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07002771 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002772 goto out;
2773 }
2774
2775 for (i = 0; i < 16; ++i) {
2776 strlcpy(dgid, p + i * 2, 3);
2777 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
2778 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08002779 kfree(p);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002780 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002781 break;
2782
2783 case SRP_OPT_PKEY:
2784 if (match_hex(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002785 pr_warn("bad P_Key parameter '%s'\n", p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002786 goto out;
2787 }
2788 target->path.pkey = cpu_to_be16(token);
2789 break;
2790
2791 case SRP_OPT_SERVICE_ID:
2792 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002793 if (!p) {
2794 ret = -ENOMEM;
2795 goto out;
2796 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002797 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
Sean Hefty247e0202007-08-08 15:51:18 -07002798 target->path.service_id = target->service_id;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002799 kfree(p);
2800 break;
2801
2802 case SRP_OPT_MAX_SECT:
2803 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002804 pr_warn("bad max sect parameter '%s'\n", p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002805 goto out;
2806 }
2807 target->scsi_host->max_sectors = token;
2808 break;
2809
Bart Van Assche4d73f952013-10-26 14:40:37 +02002810 case SRP_OPT_QUEUE_SIZE:
2811 if (match_int(args, &token) || token < 1) {
2812 pr_warn("bad queue_size parameter '%s'\n", p);
2813 goto out;
2814 }
2815 target->scsi_host->can_queue = token;
2816 target->queue_size = token + SRP_RSP_SQ_SIZE +
2817 SRP_TSK_MGMT_SQ_SIZE;
2818 if (!(opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
2819 target->scsi_host->cmd_per_lun = token;
2820 break;
2821
Vu Pham52fb2b502006-06-17 20:37:31 -07002822 case SRP_OPT_MAX_CMD_PER_LUN:
Bart Van Assche4d73f952013-10-26 14:40:37 +02002823 if (match_int(args, &token) || token < 1) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002824 pr_warn("bad max cmd_per_lun parameter '%s'\n",
2825 p);
Vu Pham52fb2b502006-06-17 20:37:31 -07002826 goto out;
2827 }
Bart Van Assche4d73f952013-10-26 14:40:37 +02002828 target->scsi_host->cmd_per_lun = token;
Vu Pham52fb2b502006-06-17 20:37:31 -07002829 break;
2830
Ramachandra K0c0450db2006-06-17 20:37:38 -07002831 case SRP_OPT_IO_CLASS:
2832 if (match_hex(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002833 pr_warn("bad IO class parameter '%s'\n", p);
Ramachandra K0c0450db2006-06-17 20:37:38 -07002834 goto out;
2835 }
2836 if (token != SRP_REV10_IB_IO_CLASS &&
2837 token != SRP_REV16A_IB_IO_CLASS) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002838 pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n",
2839 token, SRP_REV10_IB_IO_CLASS,
2840 SRP_REV16A_IB_IO_CLASS);
Ramachandra K0c0450db2006-06-17 20:37:38 -07002841 goto out;
2842 }
2843 target->io_class = token;
2844 break;
2845
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002846 case SRP_OPT_INITIATOR_EXT:
2847 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002848 if (!p) {
2849 ret = -ENOMEM;
2850 goto out;
2851 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002852 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2853 kfree(p);
2854 break;
2855
David Dillow49248642011-01-14 18:23:24 -05002856 case SRP_OPT_CMD_SG_ENTRIES:
2857 if (match_int(args, &token) || token < 1 || token > 255) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002858 pr_warn("bad max cmd_sg_entries parameter '%s'\n",
2859 p);
David Dillow49248642011-01-14 18:23:24 -05002860 goto out;
2861 }
2862 target->cmd_sg_cnt = token;
2863 break;
2864
David Dillowc07d4242011-01-16 13:57:10 -05002865 case SRP_OPT_ALLOW_EXT_SG:
2866 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002867 pr_warn("bad allow_ext_sg parameter '%s'\n", p);
David Dillowc07d4242011-01-16 13:57:10 -05002868 goto out;
2869 }
2870 target->allow_ext_sg = !!token;
2871 break;
2872
2873 case SRP_OPT_SG_TABLESIZE:
2874 if (match_int(args, &token) || token < 1 ||
2875 token > SCSI_MAX_SG_CHAIN_SEGMENTS) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002876 pr_warn("bad max sg_tablesize parameter '%s'\n",
2877 p);
David Dillowc07d4242011-01-16 13:57:10 -05002878 goto out;
2879 }
2880 target->sg_tablesize = token;
2881 break;
2882
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002883 case SRP_OPT_COMP_VECTOR:
2884 if (match_int(args, &token) || token < 0) {
2885 pr_warn("bad comp_vector parameter '%s'\n", p);
2886 goto out;
2887 }
2888 target->comp_vector = token;
2889 break;
2890
Vu Pham7bb312e2013-10-26 14:31:27 +02002891 case SRP_OPT_TL_RETRY_COUNT:
2892 if (match_int(args, &token) || token < 2 || token > 7) {
2893 pr_warn("bad tl_retry_count parameter '%s' (must be a number between 2 and 7)\n",
2894 p);
2895 goto out;
2896 }
2897 target->tl_retry_count = token;
2898 break;
2899
Roland Dreieraef9ec32005-11-02 14:07:13 -08002900 default:
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002901 pr_warn("unknown parameter or missing value '%s' in target creation request\n",
2902 p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002903 goto out;
2904 }
2905 }
2906
2907 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
2908 ret = 0;
2909 else
2910 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
2911 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
2912 !(srp_opt_tokens[i].token & opt_mask))
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002913 pr_warn("target creation request is missing parameter '%s'\n",
2914 srp_opt_tokens[i].pattern);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002915
Bart Van Assche4d73f952013-10-26 14:40:37 +02002916 if (target->scsi_host->cmd_per_lun > target->scsi_host->can_queue
2917 && (opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
2918 pr_warn("cmd_per_lun = %d > queue_size = %d\n",
2919 target->scsi_host->cmd_per_lun,
2920 target->scsi_host->can_queue);
2921
Roland Dreieraef9ec32005-11-02 14:07:13 -08002922out:
2923 kfree(options);
2924 return ret;
2925}
2926
Tony Jonesee959b02008-02-22 00:13:36 +01002927static ssize_t srp_create_target(struct device *dev,
2928 struct device_attribute *attr,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002929 const char *buf, size_t count)
2930{
2931 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01002932 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002933 struct Scsi_Host *target_host;
2934 struct srp_target_port *target;
Bart Van Assched1b42892014-05-20 15:07:20 +02002935 struct srp_device *srp_dev = host->srp_dev;
2936 struct ib_device *ibdev = srp_dev->dev;
Bart Van Asscheb81d00b2013-10-26 14:38:47 +02002937 int ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002938
2939 target_host = scsi_host_alloc(&srp_template,
2940 sizeof (struct srp_target_port));
2941 if (!target_host)
2942 return -ENOMEM;
2943
David Dillow49248642011-01-14 18:23:24 -05002944 target_host->transportt = ib_srp_transport_template;
Bart Van Asschefd1b6c42011-07-13 09:19:16 -07002945 target_host->max_channel = 0;
2946 target_host->max_id = 1;
Arne Redlich3c8edf02006-11-15 12:43:00 +01002947 target_host->max_lun = SRP_MAX_LUN;
2948 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08002949
Roland Dreieraef9ec32005-11-02 14:07:13 -08002950 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002951
David Dillow49248642011-01-14 18:23:24 -05002952 target->io_class = SRP_REV16A_IB_IO_CLASS;
2953 target->scsi_host = target_host;
2954 target->srp_host = host;
2955 target->lkey = host->srp_dev->mr->lkey;
2956 target->rkey = host->srp_dev->mr->rkey;
2957 target->cmd_sg_cnt = cmd_sg_entries;
David Dillowc07d4242011-01-16 13:57:10 -05002958 target->sg_tablesize = indirect_sg_entries ? : cmd_sg_entries;
2959 target->allow_ext_sg = allow_ext_sg;
Vu Pham7bb312e2013-10-26 14:31:27 +02002960 target->tl_retry_count = 7;
Bart Van Assche4d73f952013-10-26 14:40:37 +02002961 target->queue_size = SRP_DEFAULT_QUEUE_SIZE;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002962
Bart Van Assche2d7091b2014-03-14 13:52:45 +01002963 mutex_lock(&host->add_target_mutex);
2964
Roland Dreieraef9ec32005-11-02 14:07:13 -08002965 ret = srp_parse_options(buf, target);
2966 if (ret)
2967 goto err;
2968
Bart Van Assche4d73f952013-10-26 14:40:37 +02002969 target->req_ring_size = target->queue_size - SRP_TSK_MGMT_SQ_SIZE;
2970
Bart Van Assche96fc2482013-06-28 14:51:26 +02002971 if (!srp_conn_unique(target->srp_host, target)) {
2972 shost_printk(KERN_INFO, target->scsi_host,
2973 PFX "Already connected to target port with id_ext=%016llx;ioc_guid=%016llx;initiator_ext=%016llx\n",
2974 be64_to_cpu(target->id_ext),
2975 be64_to_cpu(target->ioc_guid),
2976 be64_to_cpu(target->initiator_ext));
2977 ret = -EEXIST;
2978 goto err;
2979 }
2980
Bart Van Assche5cfb1782014-05-20 15:08:34 +02002981 if (!srp_dev->has_fmr && !srp_dev->has_fr && !target->allow_ext_sg &&
Bart Van Assched1b42892014-05-20 15:07:20 +02002982 target->cmd_sg_cnt < target->sg_tablesize) {
Bart Van Assche5cfb1782014-05-20 15:08:34 +02002983 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 -05002984 target->sg_tablesize = target->cmd_sg_cnt;
2985 }
2986
2987 target_host->sg_tablesize = target->sg_tablesize;
2988 target->indirect_size = target->sg_tablesize *
2989 sizeof (struct srp_direct_buf);
David Dillow49248642011-01-14 18:23:24 -05002990 target->max_iu_len = sizeof (struct srp_cmd) +
2991 sizeof (struct srp_indirect_buf) +
2992 target->cmd_sg_cnt * sizeof (struct srp_direct_buf);
2993
Bart Van Asschec1120f82013-10-26 14:35:08 +02002994 INIT_WORK(&target->tl_err_work, srp_tl_err_work);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002995 INIT_WORK(&target->remove_work, srp_remove_work);
David Dillow8f26c9f2011-01-14 19:45:50 -05002996 spin_lock_init(&target->lock);
2997 INIT_LIST_HEAD(&target->free_tx);
Bart Van Asscheb81d00b2013-10-26 14:38:47 +02002998 ret = srp_alloc_req_data(target);
2999 if (ret)
3000 goto err_free_mem;
David Dillow8f26c9f2011-01-14 19:45:50 -05003001
Sagi Grimberg2088ca62014-03-14 13:51:58 +01003002 ret = ib_query_gid(ibdev, host->port, 0, &target->path.sgid);
3003 if (ret)
3004 goto err_free_mem;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003005
Roland Dreieraef9ec32005-11-02 14:07:13 -08003006 ret = srp_create_target_ib(target);
3007 if (ret)
David Dillow8f26c9f2011-01-14 19:45:50 -05003008 goto err_free_mem;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003009
David Dillow9fe4bcf2008-01-08 17:08:52 -05003010 ret = srp_new_cm_id(target);
3011 if (ret)
David Dillow8f26c9f2011-01-14 19:45:50 -05003012 goto err_free_ib;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003013
3014 ret = srp_connect_target(target);
3015 if (ret) {
David Dillow7aa54bd2008-01-07 18:23:41 -05003016 shost_printk(KERN_ERR, target->scsi_host,
3017 PFX "Connection failed\n");
Bart Van Assche394c5952014-10-30 14:46:27 +01003018 goto err_free_ib;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003019 }
3020
3021 ret = srp_add_target(host, target);
3022 if (ret)
3023 goto err_disconnect;
3024
Bart Van Asschee7ffde02014-03-14 13:52:21 +01003025 shost_printk(KERN_DEBUG, target->scsi_host, PFX
3026 "new target: id_ext %016llx ioc_guid %016llx pkey %04x service_id %016llx sgid %pI6 dgid %pI6\n",
3027 be64_to_cpu(target->id_ext),
3028 be64_to_cpu(target->ioc_guid),
3029 be16_to_cpu(target->path.pkey),
3030 be64_to_cpu(target->service_id),
3031 target->path.sgid.raw, target->path.dgid.raw);
3032
Bart Van Assche2d7091b2014-03-14 13:52:45 +01003033 ret = count;
3034
3035out:
3036 mutex_unlock(&host->add_target_mutex);
3037 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003038
3039err_disconnect:
3040 srp_disconnect_target(target);
3041
David Dillow8f26c9f2011-01-14 19:45:50 -05003042err_free_ib:
Roland Dreieraef9ec32005-11-02 14:07:13 -08003043 srp_free_target_ib(target);
3044
David Dillow8f26c9f2011-01-14 19:45:50 -05003045err_free_mem:
3046 srp_free_req_data(target);
3047
Roland Dreieraef9ec32005-11-02 14:07:13 -08003048err:
3049 scsi_host_put(target_host);
Bart Van Assche2d7091b2014-03-14 13:52:45 +01003050 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003051}
3052
Tony Jonesee959b02008-02-22 00:13:36 +01003053static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003054
Tony Jonesee959b02008-02-22 00:13:36 +01003055static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
3056 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08003057{
Tony Jonesee959b02008-02-22 00:13:36 +01003058 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003059
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01003060 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003061}
3062
Tony Jonesee959b02008-02-22 00:13:36 +01003063static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003064
Tony Jonesee959b02008-02-22 00:13:36 +01003065static ssize_t show_port(struct device *dev, struct device_attribute *attr,
3066 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08003067{
Tony Jonesee959b02008-02-22 00:13:36 +01003068 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003069
3070 return sprintf(buf, "%d\n", host->port);
3071}
3072
Tony Jonesee959b02008-02-22 00:13:36 +01003073static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003074
Roland Dreierf5358a12006-06-17 20:37:29 -07003075static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08003076{
3077 struct srp_host *host;
3078
3079 host = kzalloc(sizeof *host, GFP_KERNEL);
3080 if (!host)
3081 return NULL;
3082
3083 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07003084 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003085 init_completion(&host->released);
Bart Van Assche2d7091b2014-03-14 13:52:45 +01003086 mutex_init(&host->add_target_mutex);
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01003087 host->srp_dev = device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003088 host->port = port;
3089
Tony Jonesee959b02008-02-22 00:13:36 +01003090 host->dev.class = &srp_class;
3091 host->dev.parent = device->dev->dma_device;
Kay Sieversd927e382009-01-06 10:44:39 -08003092 dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003093
Tony Jonesee959b02008-02-22 00:13:36 +01003094 if (device_register(&host->dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07003095 goto free_host;
Tony Jonesee959b02008-02-22 00:13:36 +01003096 if (device_create_file(&host->dev, &dev_attr_add_target))
Roland Dreieraef9ec32005-11-02 14:07:13 -08003097 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01003098 if (device_create_file(&host->dev, &dev_attr_ibdev))
Roland Dreieraef9ec32005-11-02 14:07:13 -08003099 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01003100 if (device_create_file(&host->dev, &dev_attr_port))
Roland Dreieraef9ec32005-11-02 14:07:13 -08003101 goto err_class;
3102
3103 return host;
3104
3105err_class:
Tony Jonesee959b02008-02-22 00:13:36 +01003106 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003107
Roland Dreierf5358a12006-06-17 20:37:29 -07003108free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08003109 kfree(host);
3110
3111 return NULL;
3112}
3113
3114static void srp_add_one(struct ib_device *device)
3115{
Roland Dreierf5358a12006-06-17 20:37:29 -07003116 struct srp_device *srp_dev;
3117 struct ib_device_attr *dev_attr;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003118 struct srp_host *host;
Bart Van Assche52ede082014-05-20 15:07:45 +02003119 int mr_page_shift, s, e, p;
3120 u64 max_pages_per_mr;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003121
Roland Dreierf5358a12006-06-17 20:37:29 -07003122 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
3123 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08003124 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003125
Roland Dreierf5358a12006-06-17 20:37:29 -07003126 if (ib_query_device(device, dev_attr)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003127 pr_warn("Query device failed for %s\n", device->name);
Roland Dreierf5358a12006-06-17 20:37:29 -07003128 goto free_attr;
3129 }
3130
3131 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
3132 if (!srp_dev)
3133 goto free_attr;
3134
Bart Van Assched1b42892014-05-20 15:07:20 +02003135 srp_dev->has_fmr = (device->alloc_fmr && device->dealloc_fmr &&
3136 device->map_phys_fmr && device->unmap_fmr);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02003137 srp_dev->has_fr = (dev_attr->device_cap_flags &
3138 IB_DEVICE_MEM_MGT_EXTENSIONS);
3139 if (!srp_dev->has_fmr && !srp_dev->has_fr)
3140 dev_warn(&device->dev, "neither FMR nor FR is supported\n");
3141
3142 srp_dev->use_fast_reg = (srp_dev->has_fr &&
3143 (!srp_dev->has_fmr || prefer_fr));
Bart Van Assched1b42892014-05-20 15:07:20 +02003144
Roland Dreierf5358a12006-06-17 20:37:29 -07003145 /*
3146 * Use the smallest page size supported by the HCA, down to a
David Dillow8f26c9f2011-01-14 19:45:50 -05003147 * minimum of 4096 bytes. We're unlikely to build large sglists
3148 * out of smaller entries.
Roland Dreierf5358a12006-06-17 20:37:29 -07003149 */
Bart Van Assche52ede082014-05-20 15:07:45 +02003150 mr_page_shift = max(12, ffs(dev_attr->page_size_cap) - 1);
3151 srp_dev->mr_page_size = 1 << mr_page_shift;
3152 srp_dev->mr_page_mask = ~((u64) srp_dev->mr_page_size - 1);
3153 max_pages_per_mr = dev_attr->max_mr_size;
3154 do_div(max_pages_per_mr, srp_dev->mr_page_size);
3155 srp_dev->max_pages_per_mr = min_t(u64, SRP_MAX_PAGES_PER_MR,
3156 max_pages_per_mr);
Bart Van Assche5cfb1782014-05-20 15:08:34 +02003157 if (srp_dev->use_fast_reg) {
3158 srp_dev->max_pages_per_mr =
3159 min_t(u32, srp_dev->max_pages_per_mr,
3160 dev_attr->max_fast_reg_page_list_len);
3161 }
Bart Van Assche52ede082014-05-20 15:07:45 +02003162 srp_dev->mr_max_size = srp_dev->mr_page_size *
3163 srp_dev->max_pages_per_mr;
Bart Van Assche5cfb1782014-05-20 15:08:34 +02003164 pr_debug("%s: mr_page_shift = %d, dev_attr->max_mr_size = %#llx, dev_attr->max_fast_reg_page_list_len = %u, max_pages_per_mr = %d, mr_max_size = %#x\n",
Bart Van Assche52ede082014-05-20 15:07:45 +02003165 device->name, mr_page_shift, dev_attr->max_mr_size,
Bart Van Assche5cfb1782014-05-20 15:08:34 +02003166 dev_attr->max_fast_reg_page_list_len,
Bart Van Assche52ede082014-05-20 15:07:45 +02003167 srp_dev->max_pages_per_mr, srp_dev->mr_max_size);
Roland Dreierf5358a12006-06-17 20:37:29 -07003168
3169 INIT_LIST_HEAD(&srp_dev->dev_list);
3170
3171 srp_dev->dev = device;
3172 srp_dev->pd = ib_alloc_pd(device);
3173 if (IS_ERR(srp_dev->pd))
3174 goto free_dev;
3175
3176 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
3177 IB_ACCESS_LOCAL_WRITE |
3178 IB_ACCESS_REMOTE_READ |
3179 IB_ACCESS_REMOTE_WRITE);
3180 if (IS_ERR(srp_dev->mr))
3181 goto err_pd;
3182
Tom Tucker07ebafb2006-08-03 16:02:42 -05003183 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08003184 s = 0;
3185 e = 0;
3186 } else {
3187 s = 1;
3188 e = device->phys_port_cnt;
3189 }
3190
3191 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07003192 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003193 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07003194 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003195 }
3196
Roland Dreierf5358a12006-06-17 20:37:29 -07003197 ib_set_client_data(device, &srp_client, srp_dev);
3198
3199 goto free_attr;
3200
3201err_pd:
3202 ib_dealloc_pd(srp_dev->pd);
3203
3204free_dev:
3205 kfree(srp_dev);
3206
3207free_attr:
3208 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003209}
3210
3211static void srp_remove_one(struct ib_device *device)
3212{
Roland Dreierf5358a12006-06-17 20:37:29 -07003213 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003214 struct srp_host *host, *tmp_host;
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00003215 struct srp_target_port *target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003216
Roland Dreierf5358a12006-06-17 20:37:29 -07003217 srp_dev = ib_get_client_data(device, &srp_client);
Dotan Barak1fe0cb82013-06-12 15:20:36 +02003218 if (!srp_dev)
3219 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003220
Roland Dreierf5358a12006-06-17 20:37:29 -07003221 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Tony Jonesee959b02008-02-22 00:13:36 +01003222 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003223 /*
3224 * Wait for the sysfs entry to go away, so that no new
3225 * target ports can be created.
3226 */
3227 wait_for_completion(&host->released);
3228
3229 /*
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00003230 * Remove all target ports.
Roland Dreieraef9ec32005-11-02 14:07:13 -08003231 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07003232 spin_lock(&host->target_lock);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00003233 list_for_each_entry(target, &host->target_list, list)
3234 srp_queue_remove_work(target);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07003235 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003236
3237 /*
Bart Van Asschebcc05912014-07-09 15:57:26 +02003238 * Wait for tl_err and target port removal tasks.
Roland Dreieraef9ec32005-11-02 14:07:13 -08003239 */
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00003240 flush_workqueue(system_long_wq);
Bart Van Asschebcc05912014-07-09 15:57:26 +02003241 flush_workqueue(srp_remove_wq);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003242
Roland Dreieraef9ec32005-11-02 14:07:13 -08003243 kfree(host);
3244 }
3245
Roland Dreierf5358a12006-06-17 20:37:29 -07003246 ib_dereg_mr(srp_dev->mr);
3247 ib_dealloc_pd(srp_dev->pd);
3248
3249 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003250}
3251
FUJITA Tomonori32368222007-06-27 16:33:12 +09003252static struct srp_function_template ib_srp_transport_functions = {
Bart Van Asscheed9b2262013-10-26 14:34:27 +02003253 .has_rport_state = true,
3254 .reset_timer_if_blocked = true,
Bart Van Asschea95cadb2013-10-26 14:37:17 +02003255 .reconnect_delay = &srp_reconnect_delay,
Bart Van Asscheed9b2262013-10-26 14:34:27 +02003256 .fast_io_fail_tmo = &srp_fast_io_fail_tmo,
3257 .dev_loss_tmo = &srp_dev_loss_tmo,
3258 .reconnect = srp_rport_reconnect,
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02003259 .rport_delete = srp_rport_delete,
Bart Van Asscheed9b2262013-10-26 14:34:27 +02003260 .terminate_rport_io = srp_terminate_io,
FUJITA Tomonori32368222007-06-27 16:33:12 +09003261};
3262
Roland Dreieraef9ec32005-11-02 14:07:13 -08003263static int __init srp_init_module(void)
3264{
3265 int ret;
3266
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05003267 BUILD_BUG_ON(FIELD_SIZEOF(struct ib_wc, wr_id) < sizeof(void *));
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00003268
David Dillow49248642011-01-14 18:23:24 -05003269 if (srp_sg_tablesize) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003270 pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n");
David Dillow49248642011-01-14 18:23:24 -05003271 if (!cmd_sg_entries)
3272 cmd_sg_entries = srp_sg_tablesize;
3273 }
3274
3275 if (!cmd_sg_entries)
3276 cmd_sg_entries = SRP_DEF_SG_TABLESIZE;
3277
3278 if (cmd_sg_entries > 255) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003279 pr_warn("Clamping cmd_sg_entries to 255\n");
David Dillow49248642011-01-14 18:23:24 -05003280 cmd_sg_entries = 255;
David Dillow1e89a192008-04-16 21:01:12 -07003281 }
3282
David Dillowc07d4242011-01-16 13:57:10 -05003283 if (!indirect_sg_entries)
3284 indirect_sg_entries = cmd_sg_entries;
3285 else if (indirect_sg_entries < cmd_sg_entries) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003286 pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n",
3287 cmd_sg_entries);
David Dillowc07d4242011-01-16 13:57:10 -05003288 indirect_sg_entries = cmd_sg_entries;
3289 }
3290
Bart Van Asschebcc05912014-07-09 15:57:26 +02003291 srp_remove_wq = create_workqueue("srp_remove");
Wei Yongjunda05be22014-08-14 08:56:22 +08003292 if (!srp_remove_wq) {
3293 ret = -ENOMEM;
Bart Van Asschebcc05912014-07-09 15:57:26 +02003294 goto out;
3295 }
3296
3297 ret = -ENOMEM;
FUJITA Tomonori32368222007-06-27 16:33:12 +09003298 ib_srp_transport_template =
3299 srp_attach_transport(&ib_srp_transport_functions);
3300 if (!ib_srp_transport_template)
Bart Van Asschebcc05912014-07-09 15:57:26 +02003301 goto destroy_wq;
FUJITA Tomonori32368222007-06-27 16:33:12 +09003302
Roland Dreieraef9ec32005-11-02 14:07:13 -08003303 ret = class_register(&srp_class);
3304 if (ret) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003305 pr_err("couldn't register class infiniband_srp\n");
Bart Van Asschebcc05912014-07-09 15:57:26 +02003306 goto release_tr;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003307 }
3308
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003309 ib_sa_register_client(&srp_sa_client);
3310
Roland Dreieraef9ec32005-11-02 14:07:13 -08003311 ret = ib_register_client(&srp_client);
3312 if (ret) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003313 pr_err("couldn't register IB client\n");
Bart Van Asschebcc05912014-07-09 15:57:26 +02003314 goto unreg_sa;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003315 }
3316
Bart Van Asschebcc05912014-07-09 15:57:26 +02003317out:
3318 return ret;
3319
3320unreg_sa:
3321 ib_sa_unregister_client(&srp_sa_client);
3322 class_unregister(&srp_class);
3323
3324release_tr:
3325 srp_release_transport(ib_srp_transport_template);
3326
3327destroy_wq:
3328 destroy_workqueue(srp_remove_wq);
3329 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -08003330}
3331
3332static void __exit srp_cleanup_module(void)
3333{
3334 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003335 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003336 class_unregister(&srp_class);
FUJITA Tomonori32368222007-06-27 16:33:12 +09003337 srp_release_transport(ib_srp_transport_template);
Bart Van Asschebcc05912014-07-09 15:57:26 +02003338 destroy_workqueue(srp_remove_wq);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003339}
3340
3341module_init(srp_init_module);
3342module_exit(srp_cleanup_module);