blob: 281f78553061ee28b953bcb33c8d3f07c45c6cbb [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
Bart Van Asschee0bda7d2012-01-14 12:39:44 +000033#define pr_fmt(fmt) PFX fmt
34
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;
Roland Dreieraef9ec32005-11-02 14:07:13 -080069static int topspin_workarounds = 1;
70
David Dillow49248642011-01-14 18:23:24 -050071module_param(srp_sg_tablesize, uint, 0444);
72MODULE_PARM_DESC(srp_sg_tablesize, "Deprecated name for cmd_sg_entries");
73
74module_param(cmd_sg_entries, uint, 0444);
75MODULE_PARM_DESC(cmd_sg_entries,
76 "Default number of gather/scatter entries in the SRP command (default is 12, max 255)");
77
David Dillowc07d4242011-01-16 13:57:10 -050078module_param(indirect_sg_entries, uint, 0444);
79MODULE_PARM_DESC(indirect_sg_entries,
80 "Default max number of gather/scatter entries (default is 12, max is " __stringify(SCSI_MAX_SG_CHAIN_SEGMENTS) ")");
81
82module_param(allow_ext_sg, bool, 0444);
83MODULE_PARM_DESC(allow_ext_sg,
84 "Default behavior when there are more than cmd_sg_entries S/G entries after mapping; fails the request when false (default false)");
85
Roland Dreieraef9ec32005-11-02 14:07:13 -080086module_param(topspin_workarounds, int, 0444);
87MODULE_PARM_DESC(topspin_workarounds,
88 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
89
Bart Van Asscheed9b2262013-10-26 14:34:27 +020090static struct kernel_param_ops srp_tmo_ops;
91
Bart Van Asschea95cadb2013-10-26 14:37:17 +020092static int srp_reconnect_delay = 10;
93module_param_cb(reconnect_delay, &srp_tmo_ops, &srp_reconnect_delay,
94 S_IRUGO | S_IWUSR);
95MODULE_PARM_DESC(reconnect_delay, "Time between successive reconnect attempts");
96
Bart Van Asscheed9b2262013-10-26 14:34:27 +020097static int srp_fast_io_fail_tmo = 15;
98module_param_cb(fast_io_fail_tmo, &srp_tmo_ops, &srp_fast_io_fail_tmo,
99 S_IRUGO | S_IWUSR);
100MODULE_PARM_DESC(fast_io_fail_tmo,
101 "Number of seconds between the observation of a transport"
102 " layer error and failing all I/O. \"off\" means that this"
103 " functionality is disabled.");
104
Bart Van Asschea95cadb2013-10-26 14:37:17 +0200105static int srp_dev_loss_tmo = 600;
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200106module_param_cb(dev_loss_tmo, &srp_tmo_ops, &srp_dev_loss_tmo,
107 S_IRUGO | S_IWUSR);
108MODULE_PARM_DESC(dev_loss_tmo,
109 "Maximum number of seconds that the SRP transport should"
110 " insulate transport layer errors. After this time has been"
111 " exceeded the SCSI host is removed. Should be"
112 " between 1 and " __stringify(SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
113 " if fast_io_fail_tmo has not been set. \"off\" means that"
114 " this functionality is disabled.");
115
Roland Dreieraef9ec32005-11-02 14:07:13 -0800116static void srp_add_one(struct ib_device *device);
117static void srp_remove_one(struct ib_device *device);
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000118static void srp_recv_completion(struct ib_cq *cq, void *target_ptr);
119static void srp_send_completion(struct ib_cq *cq, void *target_ptr);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800120static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
121
FUJITA Tomonori32368222007-06-27 16:33:12 +0900122static struct scsi_transport_template *ib_srp_transport_template;
123
Roland Dreieraef9ec32005-11-02 14:07:13 -0800124static struct ib_client srp_client = {
125 .name = "srp",
126 .add = srp_add_one,
127 .remove = srp_remove_one
128};
129
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700130static struct ib_sa_client srp_sa_client;
131
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200132static int srp_tmo_get(char *buffer, const struct kernel_param *kp)
133{
134 int tmo = *(int *)kp->arg;
135
136 if (tmo >= 0)
137 return sprintf(buffer, "%d", tmo);
138 else
139 return sprintf(buffer, "off");
140}
141
142static int srp_tmo_set(const char *val, const struct kernel_param *kp)
143{
144 int tmo, res;
145
146 if (strncmp(val, "off", 3) != 0) {
147 res = kstrtoint(val, 0, &tmo);
148 if (res)
149 goto out;
150 } else {
151 tmo = -1;
152 }
Bart Van Asschea95cadb2013-10-26 14:37:17 +0200153 if (kp->arg == &srp_reconnect_delay)
154 res = srp_tmo_valid(tmo, srp_fast_io_fail_tmo,
155 srp_dev_loss_tmo);
156 else if (kp->arg == &srp_fast_io_fail_tmo)
157 res = srp_tmo_valid(srp_reconnect_delay, tmo, srp_dev_loss_tmo);
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200158 else
Bart Van Asschea95cadb2013-10-26 14:37:17 +0200159 res = srp_tmo_valid(srp_reconnect_delay, srp_fast_io_fail_tmo,
160 tmo);
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200161 if (res)
162 goto out;
163 *(int *)kp->arg = tmo;
164
165out:
166 return res;
167}
168
169static struct kernel_param_ops srp_tmo_ops = {
170 .get = srp_tmo_get,
171 .set = srp_tmo_set,
172};
173
Roland Dreieraef9ec32005-11-02 14:07:13 -0800174static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
175{
176 return (struct srp_target_port *) host->hostdata;
177}
178
179static const char *srp_target_info(struct Scsi_Host *host)
180{
181 return host_to_target(host)->target_name;
182}
183
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700184static int srp_target_is_topspin(struct srp_target_port *target)
185{
186 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700187 static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700188
189 return topspin_workarounds &&
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700190 (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
191 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700192}
193
Roland Dreieraef9ec32005-11-02 14:07:13 -0800194static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
195 gfp_t gfp_mask,
196 enum dma_data_direction direction)
197{
198 struct srp_iu *iu;
199
200 iu = kmalloc(sizeof *iu, gfp_mask);
201 if (!iu)
202 goto out;
203
204 iu->buf = kzalloc(size, gfp_mask);
205 if (!iu->buf)
206 goto out_free_iu;
207
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100208 iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
209 direction);
210 if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800211 goto out_free_buf;
212
213 iu->size = size;
214 iu->direction = direction;
215
216 return iu;
217
218out_free_buf:
219 kfree(iu->buf);
220out_free_iu:
221 kfree(iu);
222out:
223 return NULL;
224}
225
226static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
227{
228 if (!iu)
229 return;
230
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100231 ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
232 iu->direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800233 kfree(iu->buf);
234 kfree(iu);
235}
236
237static void srp_qp_event(struct ib_event *event, void *context)
238{
Bart Van Asschee0bda7d2012-01-14 12:39:44 +0000239 pr_debug("QP event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800240}
241
242static int srp_init_qp(struct srp_target_port *target,
243 struct ib_qp *qp)
244{
245 struct ib_qp_attr *attr;
246 int ret;
247
248 attr = kmalloc(sizeof *attr, GFP_KERNEL);
249 if (!attr)
250 return -ENOMEM;
251
Roland Dreier969a60f2008-07-14 23:48:43 -0700252 ret = ib_find_pkey(target->srp_host->srp_dev->dev,
253 target->srp_host->port,
254 be16_to_cpu(target->path.pkey),
255 &attr->pkey_index);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800256 if (ret)
257 goto out;
258
259 attr->qp_state = IB_QPS_INIT;
260 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
261 IB_ACCESS_REMOTE_WRITE);
262 attr->port_num = target->srp_host->port;
263
264 ret = ib_modify_qp(qp, attr,
265 IB_QP_STATE |
266 IB_QP_PKEY_INDEX |
267 IB_QP_ACCESS_FLAGS |
268 IB_QP_PORT);
269
270out:
271 kfree(attr);
272 return ret;
273}
274
David Dillow9fe4bcf2008-01-08 17:08:52 -0500275static int srp_new_cm_id(struct srp_target_port *target)
276{
277 struct ib_cm_id *new_cm_id;
278
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100279 new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
David Dillow9fe4bcf2008-01-08 17:08:52 -0500280 srp_cm_handler, target);
281 if (IS_ERR(new_cm_id))
282 return PTR_ERR(new_cm_id);
283
284 if (target->cm_id)
285 ib_destroy_cm_id(target->cm_id);
286 target->cm_id = new_cm_id;
287
288 return 0;
289}
290
Roland Dreieraef9ec32005-11-02 14:07:13 -0800291static int srp_create_target_ib(struct srp_target_port *target)
292{
Bart Van Assche62154b22014-05-20 15:04:45 +0200293 struct srp_device *dev = target->srp_host->srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800294 struct ib_qp_init_attr *init_attr;
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100295 struct ib_cq *recv_cq, *send_cq;
296 struct ib_qp *qp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800297 int ret;
298
299 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
300 if (!init_attr)
301 return -ENOMEM;
302
Bart Van Assche62154b22014-05-20 15:04:45 +0200303 recv_cq = ib_create_cq(dev->dev, srp_recv_completion, NULL, target,
Bart Van Assche4d73f952013-10-26 14:40:37 +0200304 target->queue_size, target->comp_vector);
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100305 if (IS_ERR(recv_cq)) {
306 ret = PTR_ERR(recv_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800307 goto err;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800308 }
309
Bart Van Assche62154b22014-05-20 15:04:45 +0200310 send_cq = ib_create_cq(dev->dev, srp_send_completion, NULL, target,
Bart Van Assche4d73f952013-10-26 14:40:37 +0200311 target->queue_size, target->comp_vector);
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100312 if (IS_ERR(send_cq)) {
313 ret = PTR_ERR(send_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800314 goto err_recv_cq;
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000315 }
316
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100317 ib_req_notify_cq(recv_cq, IB_CQ_NEXT_COMP);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800318
319 init_attr->event_handler = srp_qp_event;
Bart Van Assche4d73f952013-10-26 14:40:37 +0200320 init_attr->cap.max_send_wr = target->queue_size;
321 init_attr->cap.max_recv_wr = target->queue_size;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800322 init_attr->cap.max_recv_sge = 1;
323 init_attr->cap.max_send_sge = 1;
324 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
325 init_attr->qp_type = IB_QPT_RC;
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100326 init_attr->send_cq = send_cq;
327 init_attr->recv_cq = recv_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800328
Bart Van Assche62154b22014-05-20 15:04:45 +0200329 qp = ib_create_qp(dev->pd, init_attr);
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100330 if (IS_ERR(qp)) {
331 ret = PTR_ERR(qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800332 goto err_send_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800333 }
334
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100335 ret = srp_init_qp(target, qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800336 if (ret)
337 goto err_qp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800338
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100339 if (target->qp)
340 ib_destroy_qp(target->qp);
341 if (target->recv_cq)
342 ib_destroy_cq(target->recv_cq);
343 if (target->send_cq)
344 ib_destroy_cq(target->send_cq);
345
346 target->qp = qp;
347 target->recv_cq = recv_cq;
348 target->send_cq = send_cq;
349
Roland Dreierda9d2f02010-02-24 15:07:59 -0800350 kfree(init_attr);
351 return 0;
352
353err_qp:
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100354 ib_destroy_qp(qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800355
356err_send_cq:
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100357 ib_destroy_cq(send_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800358
359err_recv_cq:
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100360 ib_destroy_cq(recv_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800361
362err:
Roland Dreieraef9ec32005-11-02 14:07:13 -0800363 kfree(init_attr);
364 return ret;
365}
366
Bart Van Assche4d73f952013-10-26 14:40:37 +0200367/*
368 * Note: this function may be called without srp_alloc_iu_bufs() having been
369 * invoked. Hence the target->[rt]x_ring checks.
370 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800371static void srp_free_target_ib(struct srp_target_port *target)
372{
373 int i;
374
375 ib_destroy_qp(target->qp);
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000376 ib_destroy_cq(target->send_cq);
377 ib_destroy_cq(target->recv_cq);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800378
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100379 target->qp = NULL;
380 target->send_cq = target->recv_cq = NULL;
381
Bart Van Assche4d73f952013-10-26 14:40:37 +0200382 if (target->rx_ring) {
383 for (i = 0; i < target->queue_size; ++i)
384 srp_free_iu(target->srp_host, target->rx_ring[i]);
385 kfree(target->rx_ring);
386 target->rx_ring = NULL;
387 }
388 if (target->tx_ring) {
389 for (i = 0; i < target->queue_size; ++i)
390 srp_free_iu(target->srp_host, target->tx_ring[i]);
391 kfree(target->tx_ring);
392 target->tx_ring = NULL;
393 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800394}
395
396static void srp_path_rec_completion(int status,
397 struct ib_sa_path_rec *pathrec,
398 void *target_ptr)
399{
400 struct srp_target_port *target = target_ptr;
401
402 target->status = status;
403 if (status)
David Dillow7aa54bd2008-01-07 18:23:41 -0500404 shost_printk(KERN_ERR, target->scsi_host,
405 PFX "Got failed path rec status %d\n", status);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800406 else
407 target->path = *pathrec;
408 complete(&target->done);
409}
410
411static int srp_lookup_path(struct srp_target_port *target)
412{
Bart Van Asschea702adc2014-03-14 13:53:10 +0100413 int ret;
414
Roland Dreieraef9ec32005-11-02 14:07:13 -0800415 target->path.numb_path = 1;
416
417 init_completion(&target->done);
418
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700419 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100420 target->srp_host->srp_dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800421 target->srp_host->port,
422 &target->path,
Sean Hefty247e0202007-08-08 15:51:18 -0700423 IB_SA_PATH_REC_SERVICE_ID |
Roland Dreieraef9ec32005-11-02 14:07:13 -0800424 IB_SA_PATH_REC_DGID |
425 IB_SA_PATH_REC_SGID |
426 IB_SA_PATH_REC_NUMB_PATH |
427 IB_SA_PATH_REC_PKEY,
428 SRP_PATH_REC_TIMEOUT_MS,
429 GFP_KERNEL,
430 srp_path_rec_completion,
431 target, &target->path_query);
432 if (target->path_query_id < 0)
433 return target->path_query_id;
434
Bart Van Asschea702adc2014-03-14 13:53:10 +0100435 ret = wait_for_completion_interruptible(&target->done);
436 if (ret < 0)
437 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800438
439 if (target->status < 0)
David Dillow7aa54bd2008-01-07 18:23:41 -0500440 shost_printk(KERN_WARNING, target->scsi_host,
441 PFX "Path record query failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800442
443 return target->status;
444}
445
446static int srp_send_req(struct srp_target_port *target)
447{
448 struct {
449 struct ib_cm_req_param param;
450 struct srp_login_req priv;
451 } *req = NULL;
452 int status;
453
454 req = kzalloc(sizeof *req, GFP_KERNEL);
455 if (!req)
456 return -ENOMEM;
457
458 req->param.primary_path = &target->path;
459 req->param.alternate_path = NULL;
460 req->param.service_id = target->service_id;
461 req->param.qp_num = target->qp->qp_num;
462 req->param.qp_type = target->qp->qp_type;
463 req->param.private_data = &req->priv;
464 req->param.private_data_len = sizeof req->priv;
465 req->param.flow_control = 1;
466
467 get_random_bytes(&req->param.starting_psn, 4);
468 req->param.starting_psn &= 0xffffff;
469
470 /*
471 * Pick some arbitrary defaults here; we could make these
472 * module parameters if anyone cared about setting them.
473 */
474 req->param.responder_resources = 4;
475 req->param.remote_cm_response_timeout = 20;
476 req->param.local_cm_response_timeout = 20;
Vu Pham7bb312e2013-10-26 14:31:27 +0200477 req->param.retry_count = target->tl_retry_count;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800478 req->param.rnr_retry_count = 7;
479 req->param.max_cm_retries = 15;
480
481 req->priv.opcode = SRP_LOGIN_REQ;
482 req->priv.tag = 0;
David Dillow49248642011-01-14 18:23:24 -0500483 req->priv.req_it_iu_len = cpu_to_be32(target->max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800484 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
485 SRP_BUF_FORMAT_INDIRECT);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700486 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700487 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450db2006-06-17 20:37:38 -0700488 * port identifier format is 8 bytes of ID extension followed
489 * by 8 bytes of GUID. Older drafts put the two halves in the
490 * opposite order, so that the GUID comes first.
491 *
492 * Targets conforming to these obsolete drafts can be
493 * recognized by the I/O Class they report.
494 */
495 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
496 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200497 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700498 memcpy(req->priv.initiator_port_id + 8,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200499 &target->initiator_ext, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700500 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
501 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
502 } else {
503 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200504 &target->initiator_ext, 8);
505 memcpy(req->priv.initiator_port_id + 8,
506 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700507 memcpy(req->priv.target_port_id, &target->id_ext, 8);
508 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
509 }
510
Roland Dreieraef9ec32005-11-02 14:07:13 -0800511 /*
512 * Topspin/Cisco SRP targets will reject our login unless we
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200513 * zero out the first 8 bytes of our initiator port ID and set
514 * the second 8 bytes to the local node GUID.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800515 */
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700516 if (srp_target_is_topspin(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500517 shost_printk(KERN_DEBUG, target->scsi_host,
518 PFX "Topspin/Cisco initiator port ID workaround "
519 "activated for target GUID %016llx\n",
520 (unsigned long long) be64_to_cpu(target->ioc_guid));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800521 memset(req->priv.initiator_port_id, 0, 8);
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200522 memcpy(req->priv.initiator_port_id + 8,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100523 &target->srp_host->srp_dev->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800524 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800525
526 status = ib_send_cm_req(target->cm_id, &req->param);
527
528 kfree(req);
529
530 return status;
531}
532
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000533static bool srp_queue_remove_work(struct srp_target_port *target)
534{
535 bool changed = false;
536
537 spin_lock_irq(&target->lock);
538 if (target->state != SRP_TARGET_REMOVED) {
539 target->state = SRP_TARGET_REMOVED;
540 changed = true;
541 }
542 spin_unlock_irq(&target->lock);
543
544 if (changed)
545 queue_work(system_long_wq, &target->remove_work);
546
547 return changed;
548}
549
Bart Van Assche294c8752011-12-25 12:18:12 +0000550static bool srp_change_conn_state(struct srp_target_port *target,
551 bool connected)
552{
553 bool changed = false;
554
555 spin_lock_irq(&target->lock);
556 if (target->connected != connected) {
557 target->connected = connected;
558 changed = true;
559 }
560 spin_unlock_irq(&target->lock);
561
562 return changed;
563}
564
Roland Dreieraef9ec32005-11-02 14:07:13 -0800565static void srp_disconnect_target(struct srp_target_port *target)
566{
Bart Van Assche294c8752011-12-25 12:18:12 +0000567 if (srp_change_conn_state(target, false)) {
568 /* XXX should send SRP_I_LOGOUT request */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800569
Bart Van Assche294c8752011-12-25 12:18:12 +0000570 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
571 shost_printk(KERN_DEBUG, target->scsi_host,
572 PFX "Sending CM DREQ failed\n");
Bart Van Assche294c8752011-12-25 12:18:12 +0000573 }
Roland Dreiere6581052006-05-17 09:13:21 -0700574 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800575}
576
David Dillow8f26c9f2011-01-14 19:45:50 -0500577static void srp_free_req_data(struct srp_target_port *target)
578{
David Dillowc07d4242011-01-16 13:57:10 -0500579 struct ib_device *ibdev = target->srp_host->srp_dev->dev;
David Dillow8f26c9f2011-01-14 19:45:50 -0500580 struct srp_request *req;
581 int i;
582
Bart Van Assche4d73f952013-10-26 14:40:37 +0200583 if (!target->req_ring)
584 return;
585
586 for (i = 0; i < target->req_ring_size; ++i) {
587 req = &target->req_ring[i];
David Dillow8f26c9f2011-01-14 19:45:50 -0500588 kfree(req->fmr_list);
589 kfree(req->map_page);
David Dillowc07d4242011-01-16 13:57:10 -0500590 if (req->indirect_dma_addr) {
591 ib_dma_unmap_single(ibdev, req->indirect_dma_addr,
592 target->indirect_size,
593 DMA_TO_DEVICE);
594 }
595 kfree(req->indirect_desc);
David Dillow8f26c9f2011-01-14 19:45:50 -0500596 }
Bart Van Assche4d73f952013-10-26 14:40:37 +0200597
598 kfree(target->req_ring);
599 target->req_ring = NULL;
David Dillow8f26c9f2011-01-14 19:45:50 -0500600}
601
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200602static int srp_alloc_req_data(struct srp_target_port *target)
603{
604 struct srp_device *srp_dev = target->srp_host->srp_dev;
605 struct ib_device *ibdev = srp_dev->dev;
606 struct srp_request *req;
607 dma_addr_t dma_addr;
608 int i, ret = -ENOMEM;
609
610 INIT_LIST_HEAD(&target->free_reqs);
611
Bart Van Assche4d73f952013-10-26 14:40:37 +0200612 target->req_ring = kzalloc(target->req_ring_size *
613 sizeof(*target->req_ring), GFP_KERNEL);
614 if (!target->req_ring)
615 goto out;
616
617 for (i = 0; i < target->req_ring_size; ++i) {
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200618 req = &target->req_ring[i];
619 req->fmr_list = kmalloc(target->cmd_sg_cnt * sizeof(void *),
620 GFP_KERNEL);
621 req->map_page = kmalloc(SRP_FMR_SIZE * sizeof(void *),
622 GFP_KERNEL);
623 req->indirect_desc = kmalloc(target->indirect_size, GFP_KERNEL);
624 if (!req->fmr_list || !req->map_page || !req->indirect_desc)
625 goto out;
626
627 dma_addr = ib_dma_map_single(ibdev, req->indirect_desc,
628 target->indirect_size,
629 DMA_TO_DEVICE);
630 if (ib_dma_mapping_error(ibdev, dma_addr))
631 goto out;
632
633 req->indirect_dma_addr = dma_addr;
634 req->index = i;
635 list_add_tail(&req->list, &target->free_reqs);
636 }
637 ret = 0;
638
639out:
640 return ret;
641}
642
Bart Van Assche683b1592012-01-14 12:40:44 +0000643/**
644 * srp_del_scsi_host_attr() - Remove attributes defined in the host template.
645 * @shost: SCSI host whose attributes to remove from sysfs.
646 *
647 * Note: Any attributes defined in the host template and that did not exist
648 * before invocation of this function will be ignored.
649 */
650static void srp_del_scsi_host_attr(struct Scsi_Host *shost)
651{
652 struct device_attribute **attr;
653
654 for (attr = shost->hostt->shost_attrs; attr && *attr; ++attr)
655 device_remove_file(&shost->shost_dev, *attr);
656}
657
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000658static void srp_remove_target(struct srp_target_port *target)
659{
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000660 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
661
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000662 srp_del_scsi_host_attr(target->scsi_host);
Bart Van Assche9dd69a62013-10-26 14:32:30 +0200663 srp_rport_get(target->rport);
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000664 srp_remove_host(target->scsi_host);
665 scsi_remove_host(target->scsi_host);
Bart Van Assche93079162013-12-11 17:06:14 +0100666 srp_stop_rport_timers(target->rport);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000667 srp_disconnect_target(target);
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000668 ib_destroy_cm_id(target->cm_id);
669 srp_free_target_ib(target);
Bart Van Asschec1120f82013-10-26 14:35:08 +0200670 cancel_work_sync(&target->tl_err_work);
Bart Van Assche9dd69a62013-10-26 14:32:30 +0200671 srp_rport_put(target->rport);
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000672 srp_free_req_data(target);
Vu Pham65d7dd22013-10-10 13:50:29 +0200673
674 spin_lock(&target->srp_host->target_lock);
675 list_del(&target->list);
676 spin_unlock(&target->srp_host->target_lock);
677
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000678 scsi_host_put(target->scsi_host);
679}
680
David Howellsc4028952006-11-22 14:57:56 +0000681static void srp_remove_work(struct work_struct *work)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800682{
David Howellsc4028952006-11-22 14:57:56 +0000683 struct srp_target_port *target =
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000684 container_of(work, struct srp_target_port, remove_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800685
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000686 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800687
Bart Van Assche96fc2482013-06-28 14:51:26 +0200688 srp_remove_target(target);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800689}
690
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +0200691static void srp_rport_delete(struct srp_rport *rport)
692{
693 struct srp_target_port *target = rport->lld_data;
694
695 srp_queue_remove_work(target);
696}
697
Roland Dreieraef9ec32005-11-02 14:07:13 -0800698static int srp_connect_target(struct srp_target_port *target)
699{
David Dillow9fe4bcf2008-01-08 17:08:52 -0500700 int retries = 3;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800701 int ret;
702
Bart Van Assche294c8752011-12-25 12:18:12 +0000703 WARN_ON_ONCE(target->connected);
704
Bart Van Assche948d1e82011-09-03 09:25:42 +0200705 target->qp_in_error = false;
706
Roland Dreieraef9ec32005-11-02 14:07:13 -0800707 ret = srp_lookup_path(target);
708 if (ret)
709 return ret;
710
711 while (1) {
712 init_completion(&target->done);
713 ret = srp_send_req(target);
714 if (ret)
715 return ret;
Bart Van Asschea702adc2014-03-14 13:53:10 +0100716 ret = wait_for_completion_interruptible(&target->done);
717 if (ret < 0)
718 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800719
720 /*
721 * The CM event handling code will set status to
722 * SRP_PORT_REDIRECT if we get a port redirect REJ
723 * back, or SRP_DLID_REDIRECT if we get a lid/qp
724 * redirect REJ back.
725 */
726 switch (target->status) {
727 case 0:
Bart Van Assche294c8752011-12-25 12:18:12 +0000728 srp_change_conn_state(target, true);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800729 return 0;
730
731 case SRP_PORT_REDIRECT:
732 ret = srp_lookup_path(target);
733 if (ret)
734 return ret;
735 break;
736
737 case SRP_DLID_REDIRECT:
738 break;
739
David Dillow9fe4bcf2008-01-08 17:08:52 -0500740 case SRP_STALE_CONN:
741 /* Our current CM id was stale, and is now in timewait.
742 * Try to reconnect with a new one.
743 */
744 if (!retries-- || srp_new_cm_id(target)) {
745 shost_printk(KERN_ERR, target->scsi_host, PFX
746 "giving up on stale connection\n");
747 target->status = -ECONNRESET;
748 return target->status;
749 }
750
751 shost_printk(KERN_ERR, target->scsi_host, PFX
752 "retrying stale connection\n");
753 break;
754
Roland Dreieraef9ec32005-11-02 14:07:13 -0800755 default:
756 return target->status;
757 }
758 }
759}
760
Roland Dreierd945e1d2006-05-09 10:50:28 -0700761static void srp_unmap_data(struct scsi_cmnd *scmnd,
762 struct srp_target_port *target,
763 struct srp_request *req)
764{
David Dillow8f26c9f2011-01-14 19:45:50 -0500765 struct ib_device *ibdev = target->srp_host->srp_dev->dev;
766 struct ib_pool_fmr **pfmr;
767
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900768 if (!scsi_sglist(scmnd) ||
Roland Dreierd945e1d2006-05-09 10:50:28 -0700769 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
770 scmnd->sc_data_direction != DMA_FROM_DEVICE))
771 return;
772
David Dillow8f26c9f2011-01-14 19:45:50 -0500773 pfmr = req->fmr_list;
774 while (req->nfmr--)
775 ib_fmr_pool_unmap(*pfmr++);
Roland Dreierf5358a12006-06-17 20:37:29 -0700776
David Dillow8f26c9f2011-01-14 19:45:50 -0500777 ib_dma_unmap_sg(ibdev, scsi_sglist(scmnd), scsi_sg_count(scmnd),
778 scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700779}
780
Bart Van Assche22032992012-08-14 13:18:53 +0000781/**
782 * srp_claim_req - Take ownership of the scmnd associated with a request.
783 * @target: SRP target port.
784 * @req: SRP request.
Bart Van Asscheb3fe6282014-03-14 13:54:11 +0100785 * @sdev: If not NULL, only take ownership for this SCSI device.
Bart Van Assche22032992012-08-14 13:18:53 +0000786 * @scmnd: If NULL, take ownership of @req->scmnd. If not NULL, only take
787 * ownership of @req->scmnd if it equals @scmnd.
788 *
789 * Return value:
790 * Either NULL or a pointer to the SCSI command the caller became owner of.
791 */
792static struct scsi_cmnd *srp_claim_req(struct srp_target_port *target,
793 struct srp_request *req,
Bart Van Asscheb3fe6282014-03-14 13:54:11 +0100794 struct scsi_device *sdev,
Bart Van Assche22032992012-08-14 13:18:53 +0000795 struct scsi_cmnd *scmnd)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700796{
Bart Van Assche94a91742010-11-26 14:50:09 -0500797 unsigned long flags;
798
Bart Van Assche22032992012-08-14 13:18:53 +0000799 spin_lock_irqsave(&target->lock, flags);
Bart Van Asscheb3fe6282014-03-14 13:54:11 +0100800 if (req->scmnd &&
801 (!sdev || req->scmnd->device == sdev) &&
802 (!scmnd || req->scmnd == scmnd)) {
Bart Van Assche22032992012-08-14 13:18:53 +0000803 scmnd = req->scmnd;
804 req->scmnd = NULL;
Bart Van Assche22032992012-08-14 13:18:53 +0000805 } else {
806 scmnd = NULL;
807 }
808 spin_unlock_irqrestore(&target->lock, flags);
809
810 return scmnd;
811}
812
813/**
814 * srp_free_req() - Unmap data and add request to the free request list.
Bart Van Asscheaf246632014-05-20 15:04:21 +0200815 * @target: SRP target port.
816 * @req: Request to be freed.
817 * @scmnd: SCSI command associated with @req.
818 * @req_lim_delta: Amount to be added to @target->req_lim.
Bart Van Assche22032992012-08-14 13:18:53 +0000819 */
820static void srp_free_req(struct srp_target_port *target,
821 struct srp_request *req, struct scsi_cmnd *scmnd,
822 s32 req_lim_delta)
823{
824 unsigned long flags;
825
826 srp_unmap_data(scmnd, target, req);
827
Bart Van Asschee9684672010-11-26 15:08:38 -0500828 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -0500829 target->req_lim += req_lim_delta;
Bart Van Assche536ae142010-11-26 13:58:27 -0500830 list_add_tail(&req->list, &target->free_reqs);
Bart Van Asschee9684672010-11-26 15:08:38 -0500831 spin_unlock_irqrestore(&target->lock, flags);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700832}
833
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200834static void srp_finish_req(struct srp_target_port *target,
Bart Van Asscheb3fe6282014-03-14 13:54:11 +0100835 struct srp_request *req, struct scsi_device *sdev,
836 int result)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700837{
Bart Van Asscheb3fe6282014-03-14 13:54:11 +0100838 struct scsi_cmnd *scmnd = srp_claim_req(target, req, sdev, NULL);
Bart Van Assche22032992012-08-14 13:18:53 +0000839
840 if (scmnd) {
Bart Van Assche9b796d02012-08-24 10:27:54 +0000841 srp_free_req(target, req, scmnd, 0);
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200842 scmnd->result = result;
Bart Van Assche22032992012-08-14 13:18:53 +0000843 scmnd->scsi_done(scmnd);
Bart Van Assche22032992012-08-14 13:18:53 +0000844 }
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700845}
846
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200847static void srp_terminate_io(struct srp_rport *rport)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800848{
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200849 struct srp_target_port *target = rport->lld_data;
Bart Van Asscheb3fe6282014-03-14 13:54:11 +0100850 struct Scsi_Host *shost = target->scsi_host;
851 struct scsi_device *sdev;
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200852 int i;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800853
Bart Van Asscheb3fe6282014-03-14 13:54:11 +0100854 /*
855 * Invoking srp_terminate_io() while srp_queuecommand() is running
856 * is not safe. Hence the warning statement below.
857 */
858 shost_for_each_device(sdev, shost)
859 WARN_ON_ONCE(sdev->request_queue->request_fn_active);
860
Bart Van Assche4d73f952013-10-26 14:40:37 +0200861 for (i = 0; i < target->req_ring_size; ++i) {
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200862 struct srp_request *req = &target->req_ring[i];
Bart Van Asscheb3fe6282014-03-14 13:54:11 +0100863 srp_finish_req(target, req, NULL, DID_TRANSPORT_FAILFAST << 16);
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200864 }
865}
866
867/*
868 * It is up to the caller to ensure that srp_rport_reconnect() calls are
869 * serialized and that no concurrent srp_queuecommand(), srp_abort(),
870 * srp_reset_device() or srp_reset_host() calls will occur while this function
871 * is in progress. One way to realize that is not to call this function
872 * directly but to call srp_reconnect_rport() instead since that last function
873 * serializes calls of this function via rport->mutex and also blocks
874 * srp_queuecommand() calls before invoking this function.
875 */
876static int srp_rport_reconnect(struct srp_rport *rport)
877{
878 struct srp_target_port *target = rport->lld_data;
879 int i, ret;
Bart Van Assche09be70a2012-03-17 17:18:54 +0000880
Roland Dreieraef9ec32005-11-02 14:07:13 -0800881 srp_disconnect_target(target);
882 /*
Bart Van Asschec7c4e7f2013-02-21 17:19:04 +0000883 * Now get a new local CM ID so that we avoid confusing the target in
884 * case things are really fouled up. Doing so also ensures that all CM
885 * callbacks will have finished before a new QP is allocated.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800886 */
David Dillow9fe4bcf2008-01-08 17:08:52 -0500887 ret = srp_new_cm_id(target);
Bart Van Asschec7c4e7f2013-02-21 17:19:04 +0000888 /*
889 * Whether or not creating a new CM ID succeeded, create a new
890 * QP. This guarantees that all completion callback function
891 * invocations have finished before request resetting starts.
892 */
893 if (ret == 0)
894 ret = srp_create_target_ib(target);
895 else
896 srp_create_target_ib(target);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800897
Bart Van Assche4d73f952013-10-26 14:40:37 +0200898 for (i = 0; i < target->req_ring_size; ++i) {
Bart Van Assche536ae142010-11-26 13:58:27 -0500899 struct srp_request *req = &target->req_ring[i];
Bart Van Asscheb3fe6282014-03-14 13:54:11 +0100900 srp_finish_req(target, req, NULL, DID_RESET << 16);
Bart Van Assche536ae142010-11-26 13:58:27 -0500901 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800902
Bart Van Assche536ae142010-11-26 13:58:27 -0500903 INIT_LIST_HEAD(&target->free_tx);
Bart Van Assche4d73f952013-10-26 14:40:37 +0200904 for (i = 0; i < target->queue_size; ++i)
Bart Van Assche536ae142010-11-26 13:58:27 -0500905 list_add(&target->tx_ring[i]->list, &target->free_tx);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800906
Bart Van Asschec7c4e7f2013-02-21 17:19:04 +0000907 if (ret == 0)
908 ret = srp_connect_target(target);
Bart Van Assche09be70a2012-03-17 17:18:54 +0000909
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200910 if (ret == 0)
911 shost_printk(KERN_INFO, target->scsi_host,
912 PFX "reconnect succeeded\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800913
914 return ret;
915}
916
David Dillow8f26c9f2011-01-14 19:45:50 -0500917static void srp_map_desc(struct srp_map_state *state, dma_addr_t dma_addr,
918 unsigned int dma_len, u32 rkey)
Roland Dreierf5358a12006-06-17 20:37:29 -0700919{
David Dillow8f26c9f2011-01-14 19:45:50 -0500920 struct srp_direct_buf *desc = state->desc;
921
922 desc->va = cpu_to_be64(dma_addr);
923 desc->key = cpu_to_be32(rkey);
924 desc->len = cpu_to_be32(dma_len);
925
926 state->total_len += dma_len;
927 state->desc++;
928 state->ndesc++;
929}
930
931static int srp_map_finish_fmr(struct srp_map_state *state,
932 struct srp_target_port *target)
933{
934 struct srp_device *dev = target->srp_host->srp_dev;
935 struct ib_pool_fmr *fmr;
Roland Dreierf5358a12006-06-17 20:37:29 -0700936 u64 io_addr = 0;
David Dillow8f26c9f2011-01-14 19:45:50 -0500937
938 if (!state->npages)
939 return 0;
940
941 if (state->npages == 1) {
942 srp_map_desc(state, state->base_dma_addr, state->fmr_len,
943 target->rkey);
944 state->npages = state->fmr_len = 0;
945 return 0;
946 }
947
948 fmr = ib_fmr_pool_map_phys(dev->fmr_pool, state->pages,
949 state->npages, io_addr);
950 if (IS_ERR(fmr))
951 return PTR_ERR(fmr);
952
953 *state->next_fmr++ = fmr;
954 state->nfmr++;
955
956 srp_map_desc(state, 0, state->fmr_len, fmr->fmr->rkey);
957 state->npages = state->fmr_len = 0;
958 return 0;
959}
960
961static void srp_map_update_start(struct srp_map_state *state,
962 struct scatterlist *sg, int sg_index,
963 dma_addr_t dma_addr)
964{
965 state->unmapped_sg = sg;
966 state->unmapped_index = sg_index;
967 state->unmapped_addr = dma_addr;
968}
969
970static int srp_map_sg_entry(struct srp_map_state *state,
971 struct srp_target_port *target,
972 struct scatterlist *sg, int sg_index,
973 int use_fmr)
974{
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100975 struct srp_device *dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800976 struct ib_device *ibdev = dev->dev;
David Dillow8f26c9f2011-01-14 19:45:50 -0500977 dma_addr_t dma_addr = ib_sg_dma_address(ibdev, sg);
978 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
979 unsigned int len;
980 int ret;
Roland Dreierf5358a12006-06-17 20:37:29 -0700981
David Dillow8f26c9f2011-01-14 19:45:50 -0500982 if (!dma_len)
983 return 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700984
David Dillow8f26c9f2011-01-14 19:45:50 -0500985 if (use_fmr == SRP_MAP_NO_FMR) {
986 /* Once we're in direct map mode for a request, we don't
987 * go back to FMR mode, so no need to update anything
988 * other than the descriptor.
989 */
990 srp_map_desc(state, dma_addr, dma_len, target->rkey);
991 return 0;
992 }
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700993
David Dillow8f26c9f2011-01-14 19:45:50 -0500994 /* If we start at an offset into the FMR page, don't merge into
995 * the current FMR. Finish it out, and use the kernel's MR for this
996 * sg entry. This is to avoid potential bugs on some SRP targets
997 * that were never quite defined, but went away when the initiator
998 * avoided using FMR on such page fragments.
999 */
1000 if (dma_addr & ~dev->fmr_page_mask || dma_len > dev->fmr_max_size) {
1001 ret = srp_map_finish_fmr(state, target);
1002 if (ret)
1003 return ret;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001004
David Dillow8f26c9f2011-01-14 19:45:50 -05001005 srp_map_desc(state, dma_addr, dma_len, target->rkey);
1006 srp_map_update_start(state, NULL, 0, 0);
1007 return 0;
1008 }
1009
1010 /* If this is the first sg to go into the FMR, save our position.
1011 * We need to know the first unmapped entry, its index, and the
1012 * first unmapped address within that entry to be able to restart
1013 * mapping after an error.
1014 */
1015 if (!state->unmapped_sg)
1016 srp_map_update_start(state, sg, sg_index, dma_addr);
1017
1018 while (dma_len) {
1019 if (state->npages == SRP_FMR_SIZE) {
1020 ret = srp_map_finish_fmr(state, target);
1021 if (ret)
1022 return ret;
1023
1024 srp_map_update_start(state, sg, sg_index, dma_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -07001025 }
1026
David Dillow8f26c9f2011-01-14 19:45:50 -05001027 len = min_t(unsigned int, dma_len, dev->fmr_page_size);
1028
1029 if (!state->npages)
1030 state->base_dma_addr = dma_addr;
1031 state->pages[state->npages++] = dma_addr;
1032 state->fmr_len += len;
1033 dma_addr += len;
1034 dma_len -= len;
Roland Dreierf5358a12006-06-17 20:37:29 -07001035 }
1036
David Dillow8f26c9f2011-01-14 19:45:50 -05001037 /* If the last entry of the FMR wasn't a full page, then we need to
1038 * close it out and start a new one -- we can only merge at page
1039 * boundries.
1040 */
Roland Dreierf5358a12006-06-17 20:37:29 -07001041 ret = 0;
David Dillow8f26c9f2011-01-14 19:45:50 -05001042 if (len != dev->fmr_page_size) {
1043 ret = srp_map_finish_fmr(state, target);
1044 if (!ret)
1045 srp_map_update_start(state, NULL, 0, 0);
1046 }
Roland Dreierf5358a12006-06-17 20:37:29 -07001047 return ret;
1048}
1049
Roland Dreieraef9ec32005-11-02 14:07:13 -08001050static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
1051 struct srp_request *req)
1052{
David Dillow8f26c9f2011-01-14 19:45:50 -05001053 struct scatterlist *scat, *sg;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001054 struct srp_cmd *cmd = req->cmd->buf;
David Dillow8f26c9f2011-01-14 19:45:50 -05001055 int i, len, nents, count, use_fmr;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001056 struct srp_device *dev;
1057 struct ib_device *ibdev;
David Dillow8f26c9f2011-01-14 19:45:50 -05001058 struct srp_map_state state;
1059 struct srp_indirect_buf *indirect_hdr;
David Dillow8f26c9f2011-01-14 19:45:50 -05001060 u32 table_len;
1061 u8 fmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001062
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001063 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001064 return sizeof (struct srp_cmd);
1065
1066 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
1067 scmnd->sc_data_direction != DMA_TO_DEVICE) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001068 shost_printk(KERN_WARNING, target->scsi_host,
1069 PFX "Unhandled data direction %d\n",
1070 scmnd->sc_data_direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001071 return -EINVAL;
1072 }
1073
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001074 nents = scsi_sg_count(scmnd);
1075 scat = scsi_sglist(scmnd);
Roland Dreiercf368712006-03-24 15:47:26 -08001076
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001077 dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001078 ibdev = dev->dev;
1079
1080 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
David Dillow8f26c9f2011-01-14 19:45:50 -05001081 if (unlikely(count == 0))
1082 return -EIO;
Roland Dreierf5358a12006-06-17 20:37:29 -07001083
1084 fmt = SRP_DATA_DESC_DIRECT;
1085 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -08001086
1087 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -07001088 /*
1089 * The midlayer only generated a single gather/scatter
1090 * entry, or DMA mapping coalesced everything to a
1091 * single entry. So a direct descriptor along with
1092 * the DMA MR suffices.
1093 */
Roland Dreieraef9ec32005-11-02 14:07:13 -08001094 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001095
Ralph Campbell85507bc2006-12-12 14:30:55 -08001096 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
David Dillow9af76272010-11-26 15:34:46 -05001097 buf->key = cpu_to_be32(target->rkey);
Ralph Campbell85507bc2006-12-12 14:30:55 -08001098 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Roland Dreiercf368712006-03-24 15:47:26 -08001099
David Dillow8f26c9f2011-01-14 19:45:50 -05001100 req->nfmr = 0;
1101 goto map_complete;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001102 }
1103
David Dillow8f26c9f2011-01-14 19:45:50 -05001104 /* We have more than one scatter/gather entry, so build our indirect
1105 * descriptor table, trying to merge as many entries with FMR as we
1106 * can.
1107 */
1108 indirect_hdr = (void *) cmd->add_data;
1109
David Dillowc07d4242011-01-16 13:57:10 -05001110 ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr,
1111 target->indirect_size, DMA_TO_DEVICE);
1112
David Dillow8f26c9f2011-01-14 19:45:50 -05001113 memset(&state, 0, sizeof(state));
David Dillowc07d4242011-01-16 13:57:10 -05001114 state.desc = req->indirect_desc;
David Dillow8f26c9f2011-01-14 19:45:50 -05001115 state.pages = req->map_page;
1116 state.next_fmr = req->fmr_list;
1117
1118 use_fmr = dev->fmr_pool ? SRP_MAP_ALLOW_FMR : SRP_MAP_NO_FMR;
1119
1120 for_each_sg(scat, sg, count, i) {
1121 if (srp_map_sg_entry(&state, target, sg, i, use_fmr)) {
1122 /* FMR mapping failed, so backtrack to the first
1123 * unmapped entry and continue on without using FMR.
1124 */
1125 dma_addr_t dma_addr;
1126 unsigned int dma_len;
1127
1128backtrack:
1129 sg = state.unmapped_sg;
1130 i = state.unmapped_index;
1131
1132 dma_addr = ib_sg_dma_address(ibdev, sg);
1133 dma_len = ib_sg_dma_len(ibdev, sg);
1134 dma_len -= (state.unmapped_addr - dma_addr);
1135 dma_addr = state.unmapped_addr;
1136 use_fmr = SRP_MAP_NO_FMR;
1137 srp_map_desc(&state, dma_addr, dma_len, target->rkey);
1138 }
1139 }
1140
1141 if (use_fmr == SRP_MAP_ALLOW_FMR && srp_map_finish_fmr(&state, target))
1142 goto backtrack;
1143
David Dillowc07d4242011-01-16 13:57:10 -05001144 /* We've mapped the request, now pull as much of the indirect
1145 * descriptor table as we can into the command buffer. If this
1146 * target is not using an external indirect table, we are
1147 * guaranteed to fit into the command, as the SCSI layer won't
1148 * give us more S/G entries than we allow.
David Dillow8f26c9f2011-01-14 19:45:50 -05001149 */
1150 req->nfmr = state.nfmr;
1151 if (state.ndesc == 1) {
1152 /* FMR mapping was able to collapse this to one entry,
1153 * so use a direct descriptor.
1154 */
1155 struct srp_direct_buf *buf = (void *) cmd->add_data;
1156
David Dillowc07d4242011-01-16 13:57:10 -05001157 *buf = req->indirect_desc[0];
David Dillow8f26c9f2011-01-14 19:45:50 -05001158 goto map_complete;
1159 }
1160
David Dillowc07d4242011-01-16 13:57:10 -05001161 if (unlikely(target->cmd_sg_cnt < state.ndesc &&
1162 !target->allow_ext_sg)) {
1163 shost_printk(KERN_ERR, target->scsi_host,
1164 "Could not fit S/G list into SRP_CMD\n");
1165 return -EIO;
1166 }
1167
1168 count = min(state.ndesc, target->cmd_sg_cnt);
David Dillow8f26c9f2011-01-14 19:45:50 -05001169 table_len = state.ndesc * sizeof (struct srp_direct_buf);
1170
1171 fmt = SRP_DATA_DESC_INDIRECT;
1172 len = sizeof(struct srp_cmd) + sizeof (struct srp_indirect_buf);
David Dillowc07d4242011-01-16 13:57:10 -05001173 len += count * sizeof (struct srp_direct_buf);
David Dillow8f26c9f2011-01-14 19:45:50 -05001174
David Dillowc07d4242011-01-16 13:57:10 -05001175 memcpy(indirect_hdr->desc_list, req->indirect_desc,
1176 count * sizeof (struct srp_direct_buf));
David Dillow8f26c9f2011-01-14 19:45:50 -05001177
David Dillowc07d4242011-01-16 13:57:10 -05001178 indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr);
David Dillow8f26c9f2011-01-14 19:45:50 -05001179 indirect_hdr->table_desc.key = cpu_to_be32(target->rkey);
1180 indirect_hdr->table_desc.len = cpu_to_be32(table_len);
1181 indirect_hdr->len = cpu_to_be32(state.total_len);
1182
1183 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
David Dillowc07d4242011-01-16 13:57:10 -05001184 cmd->data_out_desc_cnt = count;
David Dillow8f26c9f2011-01-14 19:45:50 -05001185 else
David Dillowc07d4242011-01-16 13:57:10 -05001186 cmd->data_in_desc_cnt = count;
1187
1188 ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len,
1189 DMA_TO_DEVICE);
David Dillow8f26c9f2011-01-14 19:45:50 -05001190
1191map_complete:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001192 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
1193 cmd->buf_fmt = fmt << 4;
1194 else
1195 cmd->buf_fmt = fmt;
1196
Roland Dreieraef9ec32005-11-02 14:07:13 -08001197 return len;
1198}
1199
David Dillow05a1d752010-10-08 14:48:14 -04001200/*
Bart Van Assche76c75b22010-11-26 14:37:47 -05001201 * Return an IU and possible credit to the free pool
1202 */
1203static void srp_put_tx_iu(struct srp_target_port *target, struct srp_iu *iu,
1204 enum srp_iu_type iu_type)
1205{
1206 unsigned long flags;
1207
Bart Van Asschee9684672010-11-26 15:08:38 -05001208 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001209 list_add(&iu->list, &target->free_tx);
1210 if (iu_type != SRP_IU_RSP)
1211 ++target->req_lim;
Bart Van Asschee9684672010-11-26 15:08:38 -05001212 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001213}
1214
1215/*
Bart Van Asschee9684672010-11-26 15:08:38 -05001216 * Must be called with target->lock held to protect req_lim and free_tx.
1217 * If IU is not sent, it must be returned using srp_put_tx_iu().
David Dillow05a1d752010-10-08 14:48:14 -04001218 *
1219 * Note:
1220 * An upper limit for the number of allocated information units for each
1221 * request type is:
1222 * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues
1223 * more than Scsi_Host.can_queue requests.
1224 * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE.
1225 * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than
1226 * one unanswered SRP request to an initiator.
1227 */
1228static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
1229 enum srp_iu_type iu_type)
1230{
1231 s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE;
1232 struct srp_iu *iu;
1233
1234 srp_send_completion(target->send_cq, target);
1235
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001236 if (list_empty(&target->free_tx))
David Dillow05a1d752010-10-08 14:48:14 -04001237 return NULL;
1238
1239 /* Initiator responses to target requests do not consume credits */
Bart Van Assche76c75b22010-11-26 14:37:47 -05001240 if (iu_type != SRP_IU_RSP) {
1241 if (target->req_lim <= rsv) {
1242 ++target->zero_req_lim;
1243 return NULL;
1244 }
1245
1246 --target->req_lim;
David Dillow05a1d752010-10-08 14:48:14 -04001247 }
1248
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001249 iu = list_first_entry(&target->free_tx, struct srp_iu, list);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001250 list_del(&iu->list);
David Dillow05a1d752010-10-08 14:48:14 -04001251 return iu;
1252}
1253
Bart Van Assche76c75b22010-11-26 14:37:47 -05001254static int srp_post_send(struct srp_target_port *target,
1255 struct srp_iu *iu, int len)
David Dillow05a1d752010-10-08 14:48:14 -04001256{
1257 struct ib_sge list;
1258 struct ib_send_wr wr, *bad_wr;
David Dillow05a1d752010-10-08 14:48:14 -04001259
1260 list.addr = iu->dma;
1261 list.length = len;
David Dillow9af76272010-11-26 15:34:46 -05001262 list.lkey = target->lkey;
David Dillow05a1d752010-10-08 14:48:14 -04001263
1264 wr.next = NULL;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001265 wr.wr_id = (uintptr_t) iu;
David Dillow05a1d752010-10-08 14:48:14 -04001266 wr.sg_list = &list;
1267 wr.num_sge = 1;
1268 wr.opcode = IB_WR_SEND;
1269 wr.send_flags = IB_SEND_SIGNALED;
1270
Bart Van Assche76c75b22010-11-26 14:37:47 -05001271 return ib_post_send(target->qp, &wr, &bad_wr);
David Dillow05a1d752010-10-08 14:48:14 -04001272}
1273
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001274static int srp_post_recv(struct srp_target_port *target, struct srp_iu *iu)
Bart Van Asschec996bb42010-07-30 10:59:05 +00001275{
Bart Van Asschec996bb42010-07-30 10:59:05 +00001276 struct ib_recv_wr wr, *bad_wr;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001277 struct ib_sge list;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001278
1279 list.addr = iu->dma;
1280 list.length = iu->size;
David Dillow9af76272010-11-26 15:34:46 -05001281 list.lkey = target->lkey;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001282
1283 wr.next = NULL;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001284 wr.wr_id = (uintptr_t) iu;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001285 wr.sg_list = &list;
1286 wr.num_sge = 1;
1287
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001288 return ib_post_recv(target->qp, &wr, &bad_wr);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001289}
1290
Roland Dreieraef9ec32005-11-02 14:07:13 -08001291static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
1292{
1293 struct srp_request *req;
1294 struct scsi_cmnd *scmnd;
1295 unsigned long flags;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001296
Roland Dreieraef9ec32005-11-02 14:07:13 -08001297 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
Bart Van Asschee9684672010-11-26 15:08:38 -05001298 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -05001299 target->req_lim += be32_to_cpu(rsp->req_lim_delta);
Bart Van Asschee9684672010-11-26 15:08:38 -05001300 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -05001301
David Dillowf8b6e312010-11-26 13:02:21 -05001302 target->tsk_mgmt_status = -1;
1303 if (be32_to_cpu(rsp->resp_data_len) >= 4)
1304 target->tsk_mgmt_status = rsp->data[3];
1305 complete(&target->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001306 } else {
David Dillowf8b6e312010-11-26 13:02:21 -05001307 req = &target->req_ring[rsp->tag];
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001308 scmnd = srp_claim_req(target, req, NULL, NULL);
Bart Van Assche22032992012-08-14 13:18:53 +00001309 if (!scmnd) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001310 shost_printk(KERN_ERR, target->scsi_host,
1311 "Null scmnd for RSP w/tag %016llx\n",
1312 (unsigned long long) rsp->tag);
Bart Van Assche22032992012-08-14 13:18:53 +00001313
1314 spin_lock_irqsave(&target->lock, flags);
1315 target->req_lim += be32_to_cpu(rsp->req_lim_delta);
1316 spin_unlock_irqrestore(&target->lock, flags);
1317
1318 return;
1319 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001320 scmnd->result = rsp->status;
1321
1322 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
1323 memcpy(scmnd->sense_buffer, rsp->data +
1324 be32_to_cpu(rsp->resp_data_len),
1325 min_t(int, be32_to_cpu(rsp->sense_data_len),
1326 SCSI_SENSE_BUFFERSIZE));
1327 }
1328
1329 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001330 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001331 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001332 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001333
Bart Van Assche22032992012-08-14 13:18:53 +00001334 srp_free_req(target, req, scmnd,
1335 be32_to_cpu(rsp->req_lim_delta));
1336
David Dillowf8b6e312010-11-26 13:02:21 -05001337 scmnd->host_scribble = NULL;
1338 scmnd->scsi_done(scmnd);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001339 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001340}
1341
David Dillowbb125882010-10-08 14:40:47 -04001342static int srp_response_common(struct srp_target_port *target, s32 req_delta,
1343 void *rsp, int len)
1344{
Bart Van Assche76c75b22010-11-26 14:37:47 -05001345 struct ib_device *dev = target->srp_host->srp_dev->dev;
David Dillowbb125882010-10-08 14:40:47 -04001346 unsigned long flags;
1347 struct srp_iu *iu;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001348 int err;
David Dillowbb125882010-10-08 14:40:47 -04001349
Bart Van Asschee9684672010-11-26 15:08:38 -05001350 spin_lock_irqsave(&target->lock, flags);
David Dillowbb125882010-10-08 14:40:47 -04001351 target->req_lim += req_delta;
David Dillowbb125882010-10-08 14:40:47 -04001352 iu = __srp_get_tx_iu(target, SRP_IU_RSP);
Bart Van Asschee9684672010-11-26 15:08:38 -05001353 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001354
David Dillowbb125882010-10-08 14:40:47 -04001355 if (!iu) {
1356 shost_printk(KERN_ERR, target->scsi_host, PFX
1357 "no IU available to send response\n");
Bart Van Assche76c75b22010-11-26 14:37:47 -05001358 return 1;
David Dillowbb125882010-10-08 14:40:47 -04001359 }
1360
1361 ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE);
1362 memcpy(iu->buf, rsp, len);
1363 ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE);
1364
Bart Van Assche76c75b22010-11-26 14:37:47 -05001365 err = srp_post_send(target, iu, len);
1366 if (err) {
David Dillowbb125882010-10-08 14:40:47 -04001367 shost_printk(KERN_ERR, target->scsi_host, PFX
1368 "unable to post response: %d\n", err);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001369 srp_put_tx_iu(target, iu, SRP_IU_RSP);
1370 }
David Dillowbb125882010-10-08 14:40:47 -04001371
David Dillowbb125882010-10-08 14:40:47 -04001372 return err;
1373}
1374
1375static void srp_process_cred_req(struct srp_target_port *target,
1376 struct srp_cred_req *req)
1377{
1378 struct srp_cred_rsp rsp = {
1379 .opcode = SRP_CRED_RSP,
1380 .tag = req->tag,
1381 };
1382 s32 delta = be32_to_cpu(req->req_lim_delta);
1383
1384 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1385 shost_printk(KERN_ERR, target->scsi_host, PFX
1386 "problems processing SRP_CRED_REQ\n");
1387}
1388
1389static void srp_process_aer_req(struct srp_target_port *target,
1390 struct srp_aer_req *req)
1391{
1392 struct srp_aer_rsp rsp = {
1393 .opcode = SRP_AER_RSP,
1394 .tag = req->tag,
1395 };
1396 s32 delta = be32_to_cpu(req->req_lim_delta);
1397
1398 shost_printk(KERN_ERR, target->scsi_host, PFX
1399 "ignoring AER for LUN %llu\n", be64_to_cpu(req->lun));
1400
1401 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1402 shost_printk(KERN_ERR, target->scsi_host, PFX
1403 "problems processing SRP_AER_REQ\n");
1404}
1405
Roland Dreieraef9ec32005-11-02 14:07:13 -08001406static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
1407{
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001408 struct ib_device *dev = target->srp_host->srp_dev->dev;
Roland Dreier737b94e2011-05-23 11:30:04 -07001409 struct srp_iu *iu = (struct srp_iu *) (uintptr_t) wc->wr_id;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001410 int res;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001411 u8 opcode;
1412
Ralph Campbell85507bc2006-12-12 14:30:55 -08001413 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
1414 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001415
1416 opcode = *(u8 *) iu->buf;
1417
1418 if (0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001419 shost_printk(KERN_ERR, target->scsi_host,
1420 PFX "recv completion, opcode 0x%02x\n", opcode);
Bart Van Assche7a700812010-07-29 15:56:37 +00001421 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1,
1422 iu->buf, wc->byte_len, true);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001423 }
1424
1425 switch (opcode) {
1426 case SRP_RSP:
1427 srp_process_rsp(target, iu->buf);
1428 break;
1429
David Dillowbb125882010-10-08 14:40:47 -04001430 case SRP_CRED_REQ:
1431 srp_process_cred_req(target, iu->buf);
1432 break;
1433
1434 case SRP_AER_REQ:
1435 srp_process_aer_req(target, iu->buf);
1436 break;
1437
Roland Dreieraef9ec32005-11-02 14:07:13 -08001438 case SRP_T_LOGOUT:
1439 /* XXX Handle target logout */
David Dillow7aa54bd2008-01-07 18:23:41 -05001440 shost_printk(KERN_WARNING, target->scsi_host,
1441 PFX "Got target logout request\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001442 break;
1443
1444 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001445 shost_printk(KERN_WARNING, target->scsi_host,
1446 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001447 break;
1448 }
1449
Ralph Campbell85507bc2006-12-12 14:30:55 -08001450 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
1451 DMA_FROM_DEVICE);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001452
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001453 res = srp_post_recv(target, iu);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001454 if (res != 0)
1455 shost_printk(KERN_ERR, target->scsi_host,
1456 PFX "Recv failed with error code %d\n", res);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001457}
1458
Bart Van Asschec1120f82013-10-26 14:35:08 +02001459/**
1460 * srp_tl_err_work() - handle a transport layer error
Bart Van Asscheaf246632014-05-20 15:04:21 +02001461 * @work: Work structure embedded in an SRP target port.
Bart Van Asschec1120f82013-10-26 14:35:08 +02001462 *
1463 * Note: This function may get invoked before the rport has been created,
1464 * hence the target->rport test.
1465 */
1466static void srp_tl_err_work(struct work_struct *work)
1467{
1468 struct srp_target_port *target;
1469
1470 target = container_of(work, struct srp_target_port, tl_err_work);
1471 if (target->rport)
1472 srp_start_tl_fail_timers(target->rport);
1473}
1474
Bart Van Asschecd4e3852013-10-10 13:53:25 +02001475static void srp_handle_qp_err(enum ib_wc_status wc_status, bool send_err,
Bart Van Assche948d1e82011-09-03 09:25:42 +02001476 struct srp_target_port *target)
1477{
Bart Van Assche294c8752011-12-25 12:18:12 +00001478 if (target->connected && !target->qp_in_error) {
Bart Van Assche4f0af692012-11-26 11:16:40 +01001479 shost_printk(KERN_ERR, target->scsi_host,
1480 PFX "failed %s status %d\n",
Bart Van Asschecd4e3852013-10-10 13:53:25 +02001481 send_err ? "send" : "receive",
Bart Van Assche4f0af692012-11-26 11:16:40 +01001482 wc_status);
Bart Van Asschec1120f82013-10-26 14:35:08 +02001483 queue_work(system_long_wq, &target->tl_err_work);
Bart Van Assche4f0af692012-11-26 11:16:40 +01001484 }
Bart Van Assche948d1e82011-09-03 09:25:42 +02001485 target->qp_in_error = true;
1486}
1487
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001488static void srp_recv_completion(struct ib_cq *cq, void *target_ptr)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001489{
1490 struct srp_target_port *target = target_ptr;
1491 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001492
1493 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1494 while (ib_poll_cq(cq, 1, &wc) > 0) {
Bart Van Assche948d1e82011-09-03 09:25:42 +02001495 if (likely(wc.status == IB_WC_SUCCESS)) {
1496 srp_handle_recv(target, &wc);
1497 } else {
Bart Van Asschecd4e3852013-10-10 13:53:25 +02001498 srp_handle_qp_err(wc.status, false, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001499 }
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001500 }
1501}
1502
1503static void srp_send_completion(struct ib_cq *cq, void *target_ptr)
1504{
1505 struct srp_target_port *target = target_ptr;
1506 struct ib_wc wc;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001507 struct srp_iu *iu;
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001508
1509 while (ib_poll_cq(cq, 1, &wc) > 0) {
Bart Van Assche948d1e82011-09-03 09:25:42 +02001510 if (likely(wc.status == IB_WC_SUCCESS)) {
1511 iu = (struct srp_iu *) (uintptr_t) wc.wr_id;
1512 list_add(&iu->list, &target->free_tx);
1513 } else {
Bart Van Asschecd4e3852013-10-10 13:53:25 +02001514 srp_handle_qp_err(wc.status, true, target);
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001515 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001516 }
1517}
1518
Bart Van Assche76c75b22010-11-26 14:37:47 -05001519static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001520{
Bart Van Assche76c75b22010-11-26 14:37:47 -05001521 struct srp_target_port *target = host_to_target(shost);
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001522 struct srp_rport *rport = target->rport;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001523 struct srp_request *req;
1524 struct srp_iu *iu;
1525 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001526 struct ib_device *dev;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001527 unsigned long flags;
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001528 int len, result;
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001529 const bool in_scsi_eh = !in_interrupt() && current == shost->ehandler;
1530
1531 /*
1532 * The SCSI EH thread is the only context from which srp_queuecommand()
1533 * can get invoked for blocked devices (SDEV_BLOCK /
1534 * SDEV_CREATED_BLOCK). Avoid racing with srp_reconnect_rport() by
1535 * locking the rport mutex if invoked from inside the SCSI EH.
1536 */
1537 if (in_scsi_eh)
1538 mutex_lock(&rport->mutex);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001539
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001540 result = srp_chkready(target->rport);
1541 if (unlikely(result)) {
1542 scmnd->result = result;
Bart Van Assche2ce19e72013-02-21 17:20:00 +00001543 scmnd->scsi_done(scmnd);
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001544 goto unlock_rport;
Bart Van Assche2ce19e72013-02-21 17:20:00 +00001545 }
1546
Bart Van Asschee9684672010-11-26 15:08:38 -05001547 spin_lock_irqsave(&target->lock, flags);
David Dillowbb125882010-10-08 14:40:47 -04001548 iu = __srp_get_tx_iu(target, SRP_IU_CMD);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001549 if (!iu)
Bart Van Assche695b8342011-01-13 19:02:25 +00001550 goto err_unlock;
1551
1552 req = list_first_entry(&target->free_reqs, struct srp_request, list);
1553 list_del(&req->list);
1554 spin_unlock_irqrestore(&target->lock, flags);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001555
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001556 dev = target->srp_host->srp_dev->dev;
David Dillow49248642011-01-14 18:23:24 -05001557 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08001558 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001559
Roland Dreieraef9ec32005-11-02 14:07:13 -08001560 scmnd->result = 0;
David Dillowf8b6e312010-11-26 13:02:21 -05001561 scmnd->host_scribble = (void *) req;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001562
1563 cmd = iu->buf;
1564 memset(cmd, 0, sizeof *cmd);
1565
1566 cmd->opcode = SRP_CMD;
1567 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001568 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001569 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1570
Roland Dreieraef9ec32005-11-02 14:07:13 -08001571 req->scmnd = scmnd;
1572 req->cmd = iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001573
1574 len = srp_map_data(scmnd, target, req);
1575 if (len < 0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001576 shost_printk(KERN_ERR, target->scsi_host,
1577 PFX "Failed to map data\n");
Bart Van Assche76c75b22010-11-26 14:37:47 -05001578 goto err_iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001579 }
1580
David Dillow49248642011-01-14 18:23:24 -05001581 ib_dma_sync_single_for_device(dev, iu->dma, target->max_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08001582 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001583
Bart Van Assche76c75b22010-11-26 14:37:47 -05001584 if (srp_post_send(target, iu, len)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001585 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001586 goto err_unmap;
1587 }
1588
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001589unlock_rport:
1590 if (in_scsi_eh)
1591 mutex_unlock(&rport->mutex);
1592
Roland Dreieraef9ec32005-11-02 14:07:13 -08001593 return 0;
1594
1595err_unmap:
1596 srp_unmap_data(scmnd, target, req);
1597
Bart Van Assche76c75b22010-11-26 14:37:47 -05001598err_iu:
1599 srp_put_tx_iu(target, iu, SRP_IU_CMD);
1600
Bart Van Assche024ca902014-05-20 15:03:49 +02001601 /*
1602 * Avoid that the loops that iterate over the request ring can
1603 * encounter a dangling SCSI command pointer.
1604 */
1605 req->scmnd = NULL;
1606
Bart Van Asschee9684672010-11-26 15:08:38 -05001607 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001608 list_add(&req->list, &target->free_reqs);
Bart Van Assche695b8342011-01-13 19:02:25 +00001609
1610err_unlock:
Bart Van Asschee9684672010-11-26 15:08:38 -05001611 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001612
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001613 if (in_scsi_eh)
1614 mutex_unlock(&rport->mutex);
1615
Roland Dreieraef9ec32005-11-02 14:07:13 -08001616 return SCSI_MLQUEUE_HOST_BUSY;
1617}
1618
Bart Van Assche4d73f952013-10-26 14:40:37 +02001619/*
1620 * Note: the resources allocated in this function are freed in
1621 * srp_free_target_ib().
1622 */
Roland Dreieraef9ec32005-11-02 14:07:13 -08001623static int srp_alloc_iu_bufs(struct srp_target_port *target)
1624{
1625 int i;
1626
Bart Van Assche4d73f952013-10-26 14:40:37 +02001627 target->rx_ring = kzalloc(target->queue_size * sizeof(*target->rx_ring),
1628 GFP_KERNEL);
1629 if (!target->rx_ring)
1630 goto err_no_ring;
1631 target->tx_ring = kzalloc(target->queue_size * sizeof(*target->tx_ring),
1632 GFP_KERNEL);
1633 if (!target->tx_ring)
1634 goto err_no_ring;
1635
1636 for (i = 0; i < target->queue_size; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001637 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1638 target->max_ti_iu_len,
1639 GFP_KERNEL, DMA_FROM_DEVICE);
1640 if (!target->rx_ring[i])
1641 goto err;
1642 }
1643
Bart Van Assche4d73f952013-10-26 14:40:37 +02001644 for (i = 0; i < target->queue_size; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001645 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
David Dillow49248642011-01-14 18:23:24 -05001646 target->max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001647 GFP_KERNEL, DMA_TO_DEVICE);
1648 if (!target->tx_ring[i])
1649 goto err;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001650
1651 list_add(&target->tx_ring[i]->list, &target->free_tx);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001652 }
1653
1654 return 0;
1655
1656err:
Bart Van Assche4d73f952013-10-26 14:40:37 +02001657 for (i = 0; i < target->queue_size; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001658 srp_free_iu(target->srp_host, target->rx_ring[i]);
Bart Van Assche4d73f952013-10-26 14:40:37 +02001659 srp_free_iu(target->srp_host, target->tx_ring[i]);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001660 }
1661
Bart Van Assche4d73f952013-10-26 14:40:37 +02001662
1663err_no_ring:
1664 kfree(target->tx_ring);
1665 target->tx_ring = NULL;
1666 kfree(target->rx_ring);
1667 target->rx_ring = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001668
1669 return -ENOMEM;
1670}
1671
Bart Van Asschec9b03c12011-09-03 09:34:48 +02001672static uint32_t srp_compute_rq_tmo(struct ib_qp_attr *qp_attr, int attr_mask)
1673{
1674 uint64_t T_tr_ns, max_compl_time_ms;
1675 uint32_t rq_tmo_jiffies;
1676
1677 /*
1678 * According to section 11.2.4.2 in the IBTA spec (Modify Queue Pair,
1679 * table 91), both the QP timeout and the retry count have to be set
1680 * for RC QP's during the RTR to RTS transition.
1681 */
1682 WARN_ON_ONCE((attr_mask & (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)) !=
1683 (IB_QP_TIMEOUT | IB_QP_RETRY_CNT));
1684
1685 /*
1686 * Set target->rq_tmo_jiffies to one second more than the largest time
1687 * it can take before an error completion is generated. See also
1688 * C9-140..142 in the IBTA spec for more information about how to
1689 * convert the QP Local ACK Timeout value to nanoseconds.
1690 */
1691 T_tr_ns = 4096 * (1ULL << qp_attr->timeout);
1692 max_compl_time_ms = qp_attr->retry_cnt * 4 * T_tr_ns;
1693 do_div(max_compl_time_ms, NSEC_PER_MSEC);
1694 rq_tmo_jiffies = msecs_to_jiffies(max_compl_time_ms + 1000);
1695
1696 return rq_tmo_jiffies;
1697}
1698
David Dillow961e0be2011-01-14 17:32:07 -05001699static void srp_cm_rep_handler(struct ib_cm_id *cm_id,
1700 struct srp_login_rsp *lrsp,
1701 struct srp_target_port *target)
1702{
1703 struct ib_qp_attr *qp_attr = NULL;
1704 int attr_mask = 0;
1705 int ret;
1706 int i;
1707
1708 if (lrsp->opcode == SRP_LOGIN_RSP) {
1709 target->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len);
1710 target->req_lim = be32_to_cpu(lrsp->req_lim_delta);
1711
1712 /*
1713 * Reserve credits for task management so we don't
1714 * bounce requests back to the SCSI mid-layer.
1715 */
1716 target->scsi_host->can_queue
1717 = min(target->req_lim - SRP_TSK_MGMT_SQ_SIZE,
1718 target->scsi_host->can_queue);
Bart Van Assche4d73f952013-10-26 14:40:37 +02001719 target->scsi_host->cmd_per_lun
1720 = min_t(int, target->scsi_host->can_queue,
1721 target->scsi_host->cmd_per_lun);
David Dillow961e0be2011-01-14 17:32:07 -05001722 } else {
1723 shost_printk(KERN_WARNING, target->scsi_host,
1724 PFX "Unhandled RSP opcode %#x\n", lrsp->opcode);
1725 ret = -ECONNRESET;
1726 goto error;
1727 }
1728
Bart Van Assche4d73f952013-10-26 14:40:37 +02001729 if (!target->rx_ring) {
David Dillow961e0be2011-01-14 17:32:07 -05001730 ret = srp_alloc_iu_bufs(target);
1731 if (ret)
1732 goto error;
1733 }
1734
1735 ret = -ENOMEM;
1736 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1737 if (!qp_attr)
1738 goto error;
1739
1740 qp_attr->qp_state = IB_QPS_RTR;
1741 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1742 if (ret)
1743 goto error_free;
1744
1745 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
1746 if (ret)
1747 goto error_free;
1748
Bart Van Assche4d73f952013-10-26 14:40:37 +02001749 for (i = 0; i < target->queue_size; i++) {
David Dillow961e0be2011-01-14 17:32:07 -05001750 struct srp_iu *iu = target->rx_ring[i];
1751 ret = srp_post_recv(target, iu);
1752 if (ret)
1753 goto error_free;
1754 }
1755
1756 qp_attr->qp_state = IB_QPS_RTS;
1757 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1758 if (ret)
1759 goto error_free;
1760
Bart Van Asschec9b03c12011-09-03 09:34:48 +02001761 target->rq_tmo_jiffies = srp_compute_rq_tmo(qp_attr, attr_mask);
1762
David Dillow961e0be2011-01-14 17:32:07 -05001763 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
1764 if (ret)
1765 goto error_free;
1766
1767 ret = ib_send_cm_rtu(cm_id, NULL, 0);
1768
1769error_free:
1770 kfree(qp_attr);
1771
1772error:
1773 target->status = ret;
1774}
1775
Roland Dreieraef9ec32005-11-02 14:07:13 -08001776static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1777 struct ib_cm_event *event,
1778 struct srp_target_port *target)
1779{
David Dillow7aa54bd2008-01-07 18:23:41 -05001780 struct Scsi_Host *shost = target->scsi_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001781 struct ib_class_port_info *cpi;
1782 int opcode;
1783
1784 switch (event->param.rej_rcvd.reason) {
1785 case IB_CM_REJ_PORT_CM_REDIRECT:
1786 cpi = event->param.rej_rcvd.ari;
1787 target->path.dlid = cpi->redirect_lid;
1788 target->path.pkey = cpi->redirect_pkey;
1789 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1790 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1791
1792 target->status = target->path.dlid ?
1793 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1794 break;
1795
1796 case IB_CM_REJ_PORT_REDIRECT:
Roland Dreier5d7cbfd2007-08-03 10:45:18 -07001797 if (srp_target_is_topspin(target)) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001798 /*
1799 * Topspin/Cisco SRP gateways incorrectly send
1800 * reject reason code 25 when they mean 24
1801 * (port redirect).
1802 */
1803 memcpy(target->path.dgid.raw,
1804 event->param.rej_rcvd.ari, 16);
1805
David Dillow7aa54bd2008-01-07 18:23:41 -05001806 shost_printk(KERN_DEBUG, shost,
1807 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1808 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1809 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001810
1811 target->status = SRP_PORT_REDIRECT;
1812 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001813 shost_printk(KERN_WARNING, shost,
1814 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001815 target->status = -ECONNRESET;
1816 }
1817 break;
1818
1819 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
David Dillow7aa54bd2008-01-07 18:23:41 -05001820 shost_printk(KERN_WARNING, shost,
1821 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001822 target->status = -ECONNRESET;
1823 break;
1824
1825 case IB_CM_REJ_CONSUMER_DEFINED:
1826 opcode = *(u8 *) event->private_data;
1827 if (opcode == SRP_LOGIN_REJ) {
1828 struct srp_login_rej *rej = event->private_data;
1829 u32 reason = be32_to_cpu(rej->reason);
1830
1831 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
David Dillow7aa54bd2008-01-07 18:23:41 -05001832 shost_printk(KERN_WARNING, shost,
1833 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001834 else
Bart Van Asschee7ffde02014-03-14 13:52:21 +01001835 shost_printk(KERN_WARNING, shost, PFX
1836 "SRP LOGIN from %pI6 to %pI6 REJECTED, reason 0x%08x\n",
1837 target->path.sgid.raw,
1838 target->orig_dgid, reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001839 } else
David Dillow7aa54bd2008-01-07 18:23:41 -05001840 shost_printk(KERN_WARNING, shost,
1841 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1842 " opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001843 target->status = -ECONNRESET;
1844 break;
1845
David Dillow9fe4bcf2008-01-08 17:08:52 -05001846 case IB_CM_REJ_STALE_CONN:
1847 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
1848 target->status = SRP_STALE_CONN;
1849 break;
1850
Roland Dreieraef9ec32005-11-02 14:07:13 -08001851 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001852 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
1853 event->param.rej_rcvd.reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001854 target->status = -ECONNRESET;
1855 }
1856}
1857
1858static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1859{
1860 struct srp_target_port *target = cm_id->context;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001861 int comp = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001862
1863 switch (event->event) {
1864 case IB_CM_REQ_ERROR:
David Dillow7aa54bd2008-01-07 18:23:41 -05001865 shost_printk(KERN_DEBUG, target->scsi_host,
1866 PFX "Sending CM REQ failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001867 comp = 1;
1868 target->status = -ECONNRESET;
1869 break;
1870
1871 case IB_CM_REP_RECEIVED:
1872 comp = 1;
David Dillow961e0be2011-01-14 17:32:07 -05001873 srp_cm_rep_handler(cm_id, event->private_data, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001874 break;
1875
1876 case IB_CM_REJ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001877 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001878 comp = 1;
1879
1880 srp_cm_rej_handler(cm_id, event, target);
1881 break;
1882
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001883 case IB_CM_DREQ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001884 shost_printk(KERN_WARNING, target->scsi_host,
1885 PFX "DREQ received - connection closed\n");
Bart Van Assche294c8752011-12-25 12:18:12 +00001886 srp_change_conn_state(target, false);
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001887 if (ib_send_cm_drep(cm_id, NULL, 0))
David Dillow7aa54bd2008-01-07 18:23:41 -05001888 shost_printk(KERN_ERR, target->scsi_host,
1889 PFX "Sending CM DREP failed\n");
Bart Van Asschec1120f82013-10-26 14:35:08 +02001890 queue_work(system_long_wq, &target->tl_err_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001891 break;
1892
1893 case IB_CM_TIMEWAIT_EXIT:
David Dillow7aa54bd2008-01-07 18:23:41 -05001894 shost_printk(KERN_ERR, target->scsi_host,
1895 PFX "connection closed\n");
Bart Van Asscheac72d762014-03-14 13:53:40 +01001896 comp = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001897
Roland Dreieraef9ec32005-11-02 14:07:13 -08001898 target->status = 0;
1899 break;
1900
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001901 case IB_CM_MRA_RECEIVED:
1902 case IB_CM_DREQ_ERROR:
1903 case IB_CM_DREP_RECEIVED:
1904 break;
1905
Roland Dreieraef9ec32005-11-02 14:07:13 -08001906 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001907 shost_printk(KERN_WARNING, target->scsi_host,
1908 PFX "Unhandled CM event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001909 break;
1910 }
1911
1912 if (comp)
1913 complete(&target->done);
1914
Roland Dreieraef9ec32005-11-02 14:07:13 -08001915 return 0;
1916}
1917
Jack Wang71444b92013-11-07 11:37:37 +01001918/**
1919 * srp_change_queue_type - changing device queue tag type
1920 * @sdev: scsi device struct
1921 * @tag_type: requested tag type
1922 *
1923 * Returns queue tag type.
1924 */
1925static int
1926srp_change_queue_type(struct scsi_device *sdev, int tag_type)
1927{
1928 if (sdev->tagged_supported) {
1929 scsi_set_tag_type(sdev, tag_type);
1930 if (tag_type)
1931 scsi_activate_tcq(sdev, sdev->queue_depth);
1932 else
1933 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1934 } else
1935 tag_type = 0;
1936
1937 return tag_type;
1938}
1939
1940/**
1941 * srp_change_queue_depth - setting device queue depth
1942 * @sdev: scsi device struct
1943 * @qdepth: requested queue depth
1944 * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
1945 * (see include/scsi/scsi_host.h for definition)
1946 *
1947 * Returns queue depth.
1948 */
1949static int
1950srp_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1951{
1952 struct Scsi_Host *shost = sdev->host;
1953 int max_depth;
1954 if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP) {
1955 max_depth = shost->can_queue;
1956 if (!sdev->tagged_supported)
1957 max_depth = 1;
1958 if (qdepth > max_depth)
1959 qdepth = max_depth;
1960 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1961 } else if (reason == SCSI_QDEPTH_QFULL)
1962 scsi_track_queue_full(sdev, qdepth);
1963 else
1964 return -EOPNOTSUPP;
1965
1966 return sdev->queue_depth;
1967}
1968
Roland Dreierd945e1d2006-05-09 10:50:28 -07001969static int srp_send_tsk_mgmt(struct srp_target_port *target,
David Dillowf8b6e312010-11-26 13:02:21 -05001970 u64 req_tag, unsigned int lun, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001971{
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001972 struct srp_rport *rport = target->rport;
David Dillow19081f32010-10-18 08:54:49 -04001973 struct ib_device *dev = target->srp_host->srp_dev->dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001974 struct srp_iu *iu;
1975 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001976
Bart Van Assche3780d1f2013-02-21 17:18:00 +00001977 if (!target->connected || target->qp_in_error)
1978 return -1;
1979
David Dillowf8b6e312010-11-26 13:02:21 -05001980 init_completion(&target->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001981
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001982 /*
1983 * Lock the rport mutex to avoid that srp_create_target_ib() is
1984 * invoked while a task management function is being sent.
1985 */
1986 mutex_lock(&rport->mutex);
Bart Van Asschee9684672010-11-26 15:08:38 -05001987 spin_lock_irq(&target->lock);
David Dillowbb125882010-10-08 14:40:47 -04001988 iu = __srp_get_tx_iu(target, SRP_IU_TSK_MGMT);
Bart Van Asschee9684672010-11-26 15:08:38 -05001989 spin_unlock_irq(&target->lock);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001990
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001991 if (!iu) {
1992 mutex_unlock(&rport->mutex);
1993
Bart Van Assche76c75b22010-11-26 14:37:47 -05001994 return -1;
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001995 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001996
David Dillow19081f32010-10-18 08:54:49 -04001997 ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt,
1998 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001999 tsk_mgmt = iu->buf;
2000 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
2001
2002 tsk_mgmt->opcode = SRP_TSK_MGMT;
David Dillowf8b6e312010-11-26 13:02:21 -05002003 tsk_mgmt->lun = cpu_to_be64((u64) lun << 48);
2004 tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002005 tsk_mgmt->tsk_mgmt_func = func;
David Dillowf8b6e312010-11-26 13:02:21 -05002006 tsk_mgmt->task_tag = req_tag;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002007
David Dillow19081f32010-10-18 08:54:49 -04002008 ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt,
2009 DMA_TO_DEVICE);
Bart Van Assche76c75b22010-11-26 14:37:47 -05002010 if (srp_post_send(target, iu, sizeof *tsk_mgmt)) {
2011 srp_put_tx_iu(target, iu, SRP_IU_TSK_MGMT);
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002012 mutex_unlock(&rport->mutex);
2013
Bart Van Assche76c75b22010-11-26 14:37:47 -05002014 return -1;
2015 }
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002016 mutex_unlock(&rport->mutex);
Roland Dreierd945e1d2006-05-09 10:50:28 -07002017
David Dillowf8b6e312010-11-26 13:02:21 -05002018 if (!wait_for_completion_timeout(&target->tsk_mgmt_done,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002019 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07002020 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002021
Roland Dreierd945e1d2006-05-09 10:50:28 -07002022 return 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07002023}
2024
Roland Dreieraef9ec32005-11-02 14:07:13 -08002025static int srp_abort(struct scsi_cmnd *scmnd)
2026{
Roland Dreierd945e1d2006-05-09 10:50:28 -07002027 struct srp_target_port *target = host_to_target(scmnd->device->host);
David Dillowf8b6e312010-11-26 13:02:21 -05002028 struct srp_request *req = (struct srp_request *) scmnd->host_scribble;
Bart Van Assche086f44f2013-06-12 15:23:04 +02002029 int ret;
Roland Dreierd945e1d2006-05-09 10:50:28 -07002030
David Dillow7aa54bd2008-01-07 18:23:41 -05002031 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002032
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01002033 if (!req || !srp_claim_req(target, req, NULL, scmnd))
Bart Van Assche99b66972013-10-10 13:52:33 +02002034 return SUCCESS;
Bart Van Assche086f44f2013-06-12 15:23:04 +02002035 if (srp_send_tsk_mgmt(target, req->index, scmnd->device->lun,
Bart Van Assche80d5e8a2013-07-10 17:36:35 +02002036 SRP_TSK_ABORT_TASK) == 0)
Bart Van Assche086f44f2013-06-12 15:23:04 +02002037 ret = SUCCESS;
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002038 else if (target->rport->state == SRP_RPORT_LOST)
Bart Van Assche99e1c132013-06-28 14:49:58 +02002039 ret = FAST_IO_FAIL;
Bart Van Assche086f44f2013-06-12 15:23:04 +02002040 else
2041 ret = FAILED;
Bart Van Assche22032992012-08-14 13:18:53 +00002042 srp_free_req(target, req, scmnd, 0);
2043 scmnd->result = DID_ABORT << 16;
Bart Van Assched8536672012-08-24 10:29:11 +00002044 scmnd->scsi_done(scmnd);
Roland Dreierd945e1d2006-05-09 10:50:28 -07002045
Bart Van Assche086f44f2013-06-12 15:23:04 +02002046 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002047}
2048
2049static int srp_reset_device(struct scsi_cmnd *scmnd)
2050{
Roland Dreierd945e1d2006-05-09 10:50:28 -07002051 struct srp_target_port *target = host_to_target(scmnd->device->host);
Bart Van Assche536ae142010-11-26 13:58:27 -05002052 int i;
Roland Dreierd945e1d2006-05-09 10:50:28 -07002053
David Dillow7aa54bd2008-01-07 18:23:41 -05002054 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002055
David Dillowf8b6e312010-11-26 13:02:21 -05002056 if (srp_send_tsk_mgmt(target, SRP_TAG_NO_REQ, scmnd->device->lun,
2057 SRP_TSK_LUN_RESET))
Roland Dreierd945e1d2006-05-09 10:50:28 -07002058 return FAILED;
David Dillowf8b6e312010-11-26 13:02:21 -05002059 if (target->tsk_mgmt_status)
Roland Dreierd945e1d2006-05-09 10:50:28 -07002060 return FAILED;
2061
Bart Van Assche4d73f952013-10-26 14:40:37 +02002062 for (i = 0; i < target->req_ring_size; ++i) {
Bart Van Assche536ae142010-11-26 13:58:27 -05002063 struct srp_request *req = &target->req_ring[i];
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01002064 srp_finish_req(target, req, scmnd->device, DID_RESET << 16);
Bart Van Assche536ae142010-11-26 13:58:27 -05002065 }
Roland Dreierd945e1d2006-05-09 10:50:28 -07002066
Roland Dreierd945e1d2006-05-09 10:50:28 -07002067 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002068}
2069
2070static int srp_reset_host(struct scsi_cmnd *scmnd)
2071{
2072 struct srp_target_port *target = host_to_target(scmnd->device->host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002073
David Dillow7aa54bd2008-01-07 18:23:41 -05002074 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002075
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002076 return srp_reconnect_rport(target->rport) == 0 ? SUCCESS : FAILED;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002077}
2078
Bart Van Asschec9b03c12011-09-03 09:34:48 +02002079static int srp_slave_configure(struct scsi_device *sdev)
2080{
2081 struct Scsi_Host *shost = sdev->host;
2082 struct srp_target_port *target = host_to_target(shost);
2083 struct request_queue *q = sdev->request_queue;
2084 unsigned long timeout;
2085
2086 if (sdev->type == TYPE_DISK) {
2087 timeout = max_t(unsigned, 30 * HZ, target->rq_tmo_jiffies);
2088 blk_queue_rq_timeout(q, timeout);
2089 }
2090
2091 return 0;
2092}
2093
Tony Jonesee959b02008-02-22 00:13:36 +01002094static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
2095 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002096{
Tony Jonesee959b02008-02-22 00:13:36 +01002097 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002098
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002099 return sprintf(buf, "0x%016llx\n",
2100 (unsigned long long) be64_to_cpu(target->id_ext));
2101}
2102
Tony Jonesee959b02008-02-22 00:13:36 +01002103static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
2104 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002105{
Tony Jonesee959b02008-02-22 00:13:36 +01002106 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002107
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002108 return sprintf(buf, "0x%016llx\n",
2109 (unsigned long long) be64_to_cpu(target->ioc_guid));
2110}
2111
Tony Jonesee959b02008-02-22 00:13:36 +01002112static ssize_t show_service_id(struct device *dev,
2113 struct device_attribute *attr, char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002114{
Tony Jonesee959b02008-02-22 00:13:36 +01002115 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002116
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002117 return sprintf(buf, "0x%016llx\n",
2118 (unsigned long long) be64_to_cpu(target->service_id));
2119}
2120
Tony Jonesee959b02008-02-22 00:13:36 +01002121static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
2122 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002123{
Tony Jonesee959b02008-02-22 00:13:36 +01002124 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002125
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002126 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
2127}
2128
Bart Van Assche848b3082013-10-26 14:38:12 +02002129static ssize_t show_sgid(struct device *dev, struct device_attribute *attr,
2130 char *buf)
2131{
2132 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2133
2134 return sprintf(buf, "%pI6\n", target->path.sgid.raw);
2135}
2136
Tony Jonesee959b02008-02-22 00:13:36 +01002137static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
2138 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002139{
Tony Jonesee959b02008-02-22 00:13:36 +01002140 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002141
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002142 return sprintf(buf, "%pI6\n", target->path.dgid.raw);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002143}
2144
Tony Jonesee959b02008-02-22 00:13:36 +01002145static ssize_t show_orig_dgid(struct device *dev,
2146 struct device_attribute *attr, char *buf)
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002147{
Tony Jonesee959b02008-02-22 00:13:36 +01002148 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002149
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002150 return sprintf(buf, "%pI6\n", target->orig_dgid);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002151}
2152
Bart Van Assche89de7482010-08-03 14:08:45 +00002153static ssize_t show_req_lim(struct device *dev,
2154 struct device_attribute *attr, char *buf)
2155{
2156 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2157
Bart Van Assche89de7482010-08-03 14:08:45 +00002158 return sprintf(buf, "%d\n", target->req_lim);
2159}
2160
Tony Jonesee959b02008-02-22 00:13:36 +01002161static ssize_t show_zero_req_lim(struct device *dev,
2162 struct device_attribute *attr, char *buf)
Roland Dreier6bfa24f2006-06-17 20:37:33 -07002163{
Tony Jonesee959b02008-02-22 00:13:36 +01002164 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6bfa24f2006-06-17 20:37:33 -07002165
Roland Dreier6bfa24f2006-06-17 20:37:33 -07002166 return sprintf(buf, "%d\n", target->zero_req_lim);
2167}
2168
Tony Jonesee959b02008-02-22 00:13:36 +01002169static ssize_t show_local_ib_port(struct device *dev,
2170 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002171{
Tony Jonesee959b02008-02-22 00:13:36 +01002172 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002173
2174 return sprintf(buf, "%d\n", target->srp_host->port);
2175}
2176
Tony Jonesee959b02008-02-22 00:13:36 +01002177static ssize_t show_local_ib_device(struct device *dev,
2178 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002179{
Tony Jonesee959b02008-02-22 00:13:36 +01002180 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002181
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002182 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002183}
2184
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002185static ssize_t show_comp_vector(struct device *dev,
2186 struct device_attribute *attr, char *buf)
2187{
2188 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2189
2190 return sprintf(buf, "%d\n", target->comp_vector);
2191}
2192
Vu Pham7bb312e2013-10-26 14:31:27 +02002193static ssize_t show_tl_retry_count(struct device *dev,
2194 struct device_attribute *attr, char *buf)
2195{
2196 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2197
2198 return sprintf(buf, "%d\n", target->tl_retry_count);
2199}
2200
David Dillow49248642011-01-14 18:23:24 -05002201static ssize_t show_cmd_sg_entries(struct device *dev,
2202 struct device_attribute *attr, char *buf)
2203{
2204 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2205
2206 return sprintf(buf, "%u\n", target->cmd_sg_cnt);
2207}
2208
David Dillowc07d4242011-01-16 13:57:10 -05002209static ssize_t show_allow_ext_sg(struct device *dev,
2210 struct device_attribute *attr, char *buf)
2211{
2212 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2213
2214 return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false");
2215}
2216
Tony Jonesee959b02008-02-22 00:13:36 +01002217static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
2218static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
2219static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
2220static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
Bart Van Assche848b3082013-10-26 14:38:12 +02002221static DEVICE_ATTR(sgid, S_IRUGO, show_sgid, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01002222static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
2223static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
Bart Van Assche89de7482010-08-03 14:08:45 +00002224static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01002225static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
2226static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
2227static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002228static DEVICE_ATTR(comp_vector, S_IRUGO, show_comp_vector, NULL);
Vu Pham7bb312e2013-10-26 14:31:27 +02002229static DEVICE_ATTR(tl_retry_count, S_IRUGO, show_tl_retry_count, NULL);
David Dillow49248642011-01-14 18:23:24 -05002230static DEVICE_ATTR(cmd_sg_entries, S_IRUGO, show_cmd_sg_entries, NULL);
David Dillowc07d4242011-01-16 13:57:10 -05002231static DEVICE_ATTR(allow_ext_sg, S_IRUGO, show_allow_ext_sg, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002232
Tony Jonesee959b02008-02-22 00:13:36 +01002233static struct device_attribute *srp_host_attrs[] = {
2234 &dev_attr_id_ext,
2235 &dev_attr_ioc_guid,
2236 &dev_attr_service_id,
2237 &dev_attr_pkey,
Bart Van Assche848b3082013-10-26 14:38:12 +02002238 &dev_attr_sgid,
Tony Jonesee959b02008-02-22 00:13:36 +01002239 &dev_attr_dgid,
2240 &dev_attr_orig_dgid,
Bart Van Assche89de7482010-08-03 14:08:45 +00002241 &dev_attr_req_lim,
Tony Jonesee959b02008-02-22 00:13:36 +01002242 &dev_attr_zero_req_lim,
2243 &dev_attr_local_ib_port,
2244 &dev_attr_local_ib_device,
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002245 &dev_attr_comp_vector,
Vu Pham7bb312e2013-10-26 14:31:27 +02002246 &dev_attr_tl_retry_count,
David Dillow49248642011-01-14 18:23:24 -05002247 &dev_attr_cmd_sg_entries,
David Dillowc07d4242011-01-16 13:57:10 -05002248 &dev_attr_allow_ext_sg,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002249 NULL
2250};
2251
Roland Dreieraef9ec32005-11-02 14:07:13 -08002252static struct scsi_host_template srp_template = {
2253 .module = THIS_MODULE,
Roland Dreierb7f008f2007-05-06 21:18:11 -07002254 .name = "InfiniBand SRP initiator",
2255 .proc_name = DRV_NAME,
Bart Van Asschec9b03c12011-09-03 09:34:48 +02002256 .slave_configure = srp_slave_configure,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002257 .info = srp_target_info,
2258 .queuecommand = srp_queuecommand,
Jack Wang71444b92013-11-07 11:37:37 +01002259 .change_queue_depth = srp_change_queue_depth,
2260 .change_queue_type = srp_change_queue_type,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002261 .eh_abort_handler = srp_abort,
2262 .eh_device_reset_handler = srp_reset_device,
2263 .eh_host_reset_handler = srp_reset_host,
Bart Van Assche2742c1d2013-06-12 15:24:25 +02002264 .skip_settle_delay = true,
David Dillow49248642011-01-14 18:23:24 -05002265 .sg_tablesize = SRP_DEF_SG_TABLESIZE,
Bart Van Assche4d73f952013-10-26 14:40:37 +02002266 .can_queue = SRP_DEFAULT_CMD_SQ_SIZE,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002267 .this_id = -1,
Bart Van Assche4d73f952013-10-26 14:40:37 +02002268 .cmd_per_lun = SRP_DEFAULT_CMD_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002269 .use_clustering = ENABLE_CLUSTERING,
2270 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08002271};
2272
2273static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
2274{
FUJITA Tomonori32368222007-06-27 16:33:12 +09002275 struct srp_rport_identifiers ids;
2276 struct srp_rport *rport;
2277
Roland Dreieraef9ec32005-11-02 14:07:13 -08002278 sprintf(target->target_name, "SRP.T10:%016llX",
2279 (unsigned long long) be64_to_cpu(target->id_ext));
2280
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002281 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002282 return -ENODEV;
2283
FUJITA Tomonori32368222007-06-27 16:33:12 +09002284 memcpy(ids.port_id, &target->id_ext, 8);
2285 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09002286 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori32368222007-06-27 16:33:12 +09002287 rport = srp_rport_add(target->scsi_host, &ids);
2288 if (IS_ERR(rport)) {
2289 scsi_remove_host(target->scsi_host);
2290 return PTR_ERR(rport);
2291 }
2292
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02002293 rport->lld_data = target;
Bart Van Assche9dd69a62013-10-26 14:32:30 +02002294 target->rport = rport;
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02002295
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002296 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002297 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002298 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002299
2300 target->state = SRP_TARGET_LIVE;
2301
Roland Dreieraef9ec32005-11-02 14:07:13 -08002302 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07002303 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002304
2305 return 0;
2306}
2307
Tony Jonesee959b02008-02-22 00:13:36 +01002308static void srp_release_dev(struct device *dev)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002309{
2310 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01002311 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002312
2313 complete(&host->released);
2314}
2315
2316static struct class srp_class = {
2317 .name = "infiniband_srp",
Tony Jonesee959b02008-02-22 00:13:36 +01002318 .dev_release = srp_release_dev
Roland Dreieraef9ec32005-11-02 14:07:13 -08002319};
2320
Bart Van Assche96fc2482013-06-28 14:51:26 +02002321/**
2322 * srp_conn_unique() - check whether the connection to a target is unique
Bart Van Asscheaf246632014-05-20 15:04:21 +02002323 * @host: SRP host.
2324 * @target: SRP target port.
Bart Van Assche96fc2482013-06-28 14:51:26 +02002325 */
2326static bool srp_conn_unique(struct srp_host *host,
2327 struct srp_target_port *target)
2328{
2329 struct srp_target_port *t;
2330 bool ret = false;
2331
2332 if (target->state == SRP_TARGET_REMOVED)
2333 goto out;
2334
2335 ret = true;
2336
2337 spin_lock(&host->target_lock);
2338 list_for_each_entry(t, &host->target_list, list) {
2339 if (t != target &&
2340 target->id_ext == t->id_ext &&
2341 target->ioc_guid == t->ioc_guid &&
2342 target->initiator_ext == t->initiator_ext) {
2343 ret = false;
2344 break;
2345 }
2346 }
2347 spin_unlock(&host->target_lock);
2348
2349out:
2350 return ret;
2351}
2352
Roland Dreieraef9ec32005-11-02 14:07:13 -08002353/*
2354 * Target ports are added by writing
2355 *
2356 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
2357 * pkey=<P_Key>,service_id=<service ID>
2358 *
2359 * to the add_target sysfs attribute.
2360 */
2361enum {
2362 SRP_OPT_ERR = 0,
2363 SRP_OPT_ID_EXT = 1 << 0,
2364 SRP_OPT_IOC_GUID = 1 << 1,
2365 SRP_OPT_DGID = 1 << 2,
2366 SRP_OPT_PKEY = 1 << 3,
2367 SRP_OPT_SERVICE_ID = 1 << 4,
2368 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07002369 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07002370 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002371 SRP_OPT_INITIATOR_EXT = 1 << 8,
David Dillow49248642011-01-14 18:23:24 -05002372 SRP_OPT_CMD_SG_ENTRIES = 1 << 9,
David Dillowc07d4242011-01-16 13:57:10 -05002373 SRP_OPT_ALLOW_EXT_SG = 1 << 10,
2374 SRP_OPT_SG_TABLESIZE = 1 << 11,
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002375 SRP_OPT_COMP_VECTOR = 1 << 12,
Vu Pham7bb312e2013-10-26 14:31:27 +02002376 SRP_OPT_TL_RETRY_COUNT = 1 << 13,
Bart Van Assche4d73f952013-10-26 14:40:37 +02002377 SRP_OPT_QUEUE_SIZE = 1 << 14,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002378 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
2379 SRP_OPT_IOC_GUID |
2380 SRP_OPT_DGID |
2381 SRP_OPT_PKEY |
2382 SRP_OPT_SERVICE_ID),
2383};
2384
Steven Whitehousea447c092008-10-13 10:46:57 +01002385static const match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07002386 { SRP_OPT_ID_EXT, "id_ext=%s" },
2387 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
2388 { SRP_OPT_DGID, "dgid=%s" },
2389 { SRP_OPT_PKEY, "pkey=%x" },
2390 { SRP_OPT_SERVICE_ID, "service_id=%s" },
2391 { SRP_OPT_MAX_SECT, "max_sect=%d" },
2392 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07002393 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002394 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
David Dillow49248642011-01-14 18:23:24 -05002395 { SRP_OPT_CMD_SG_ENTRIES, "cmd_sg_entries=%u" },
David Dillowc07d4242011-01-16 13:57:10 -05002396 { SRP_OPT_ALLOW_EXT_SG, "allow_ext_sg=%u" },
2397 { SRP_OPT_SG_TABLESIZE, "sg_tablesize=%u" },
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002398 { SRP_OPT_COMP_VECTOR, "comp_vector=%u" },
Vu Pham7bb312e2013-10-26 14:31:27 +02002399 { SRP_OPT_TL_RETRY_COUNT, "tl_retry_count=%u" },
Bart Van Assche4d73f952013-10-26 14:40:37 +02002400 { SRP_OPT_QUEUE_SIZE, "queue_size=%d" },
Vu Pham52fb2b502006-06-17 20:37:31 -07002401 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002402};
2403
2404static int srp_parse_options(const char *buf, struct srp_target_port *target)
2405{
2406 char *options, *sep_opt;
2407 char *p;
2408 char dgid[3];
2409 substring_t args[MAX_OPT_ARGS];
2410 int opt_mask = 0;
2411 int token;
2412 int ret = -EINVAL;
2413 int i;
2414
2415 options = kstrdup(buf, GFP_KERNEL);
2416 if (!options)
2417 return -ENOMEM;
2418
2419 sep_opt = options;
2420 while ((p = strsep(&sep_opt, ",")) != NULL) {
2421 if (!*p)
2422 continue;
2423
2424 token = match_token(p, srp_opt_tokens, args);
2425 opt_mask |= token;
2426
2427 switch (token) {
2428 case SRP_OPT_ID_EXT:
2429 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002430 if (!p) {
2431 ret = -ENOMEM;
2432 goto out;
2433 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002434 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2435 kfree(p);
2436 break;
2437
2438 case SRP_OPT_IOC_GUID:
2439 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002440 if (!p) {
2441 ret = -ENOMEM;
2442 goto out;
2443 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002444 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
2445 kfree(p);
2446 break;
2447
2448 case SRP_OPT_DGID:
2449 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002450 if (!p) {
2451 ret = -ENOMEM;
2452 goto out;
2453 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002454 if (strlen(p) != 32) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002455 pr_warn("bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07002456 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002457 goto out;
2458 }
2459
2460 for (i = 0; i < 16; ++i) {
2461 strlcpy(dgid, p + i * 2, 3);
2462 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
2463 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08002464 kfree(p);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002465 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002466 break;
2467
2468 case SRP_OPT_PKEY:
2469 if (match_hex(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002470 pr_warn("bad P_Key parameter '%s'\n", p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002471 goto out;
2472 }
2473 target->path.pkey = cpu_to_be16(token);
2474 break;
2475
2476 case SRP_OPT_SERVICE_ID:
2477 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002478 if (!p) {
2479 ret = -ENOMEM;
2480 goto out;
2481 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002482 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
Sean Hefty247e0202007-08-08 15:51:18 -07002483 target->path.service_id = target->service_id;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002484 kfree(p);
2485 break;
2486
2487 case SRP_OPT_MAX_SECT:
2488 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002489 pr_warn("bad max sect parameter '%s'\n", p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002490 goto out;
2491 }
2492 target->scsi_host->max_sectors = token;
2493 break;
2494
Bart Van Assche4d73f952013-10-26 14:40:37 +02002495 case SRP_OPT_QUEUE_SIZE:
2496 if (match_int(args, &token) || token < 1) {
2497 pr_warn("bad queue_size parameter '%s'\n", p);
2498 goto out;
2499 }
2500 target->scsi_host->can_queue = token;
2501 target->queue_size = token + SRP_RSP_SQ_SIZE +
2502 SRP_TSK_MGMT_SQ_SIZE;
2503 if (!(opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
2504 target->scsi_host->cmd_per_lun = token;
2505 break;
2506
Vu Pham52fb2b502006-06-17 20:37:31 -07002507 case SRP_OPT_MAX_CMD_PER_LUN:
Bart Van Assche4d73f952013-10-26 14:40:37 +02002508 if (match_int(args, &token) || token < 1) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002509 pr_warn("bad max cmd_per_lun parameter '%s'\n",
2510 p);
Vu Pham52fb2b502006-06-17 20:37:31 -07002511 goto out;
2512 }
Bart Van Assche4d73f952013-10-26 14:40:37 +02002513 target->scsi_host->cmd_per_lun = token;
Vu Pham52fb2b502006-06-17 20:37:31 -07002514 break;
2515
Ramachandra K0c0450db2006-06-17 20:37:38 -07002516 case SRP_OPT_IO_CLASS:
2517 if (match_hex(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002518 pr_warn("bad IO class parameter '%s'\n", p);
Ramachandra K0c0450db2006-06-17 20:37:38 -07002519 goto out;
2520 }
2521 if (token != SRP_REV10_IB_IO_CLASS &&
2522 token != SRP_REV16A_IB_IO_CLASS) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002523 pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n",
2524 token, SRP_REV10_IB_IO_CLASS,
2525 SRP_REV16A_IB_IO_CLASS);
Ramachandra K0c0450db2006-06-17 20:37:38 -07002526 goto out;
2527 }
2528 target->io_class = token;
2529 break;
2530
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002531 case SRP_OPT_INITIATOR_EXT:
2532 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002533 if (!p) {
2534 ret = -ENOMEM;
2535 goto out;
2536 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002537 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2538 kfree(p);
2539 break;
2540
David Dillow49248642011-01-14 18:23:24 -05002541 case SRP_OPT_CMD_SG_ENTRIES:
2542 if (match_int(args, &token) || token < 1 || token > 255) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002543 pr_warn("bad max cmd_sg_entries parameter '%s'\n",
2544 p);
David Dillow49248642011-01-14 18:23:24 -05002545 goto out;
2546 }
2547 target->cmd_sg_cnt = token;
2548 break;
2549
David Dillowc07d4242011-01-16 13:57:10 -05002550 case SRP_OPT_ALLOW_EXT_SG:
2551 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002552 pr_warn("bad allow_ext_sg parameter '%s'\n", p);
David Dillowc07d4242011-01-16 13:57:10 -05002553 goto out;
2554 }
2555 target->allow_ext_sg = !!token;
2556 break;
2557
2558 case SRP_OPT_SG_TABLESIZE:
2559 if (match_int(args, &token) || token < 1 ||
2560 token > SCSI_MAX_SG_CHAIN_SEGMENTS) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002561 pr_warn("bad max sg_tablesize parameter '%s'\n",
2562 p);
David Dillowc07d4242011-01-16 13:57:10 -05002563 goto out;
2564 }
2565 target->sg_tablesize = token;
2566 break;
2567
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002568 case SRP_OPT_COMP_VECTOR:
2569 if (match_int(args, &token) || token < 0) {
2570 pr_warn("bad comp_vector parameter '%s'\n", p);
2571 goto out;
2572 }
2573 target->comp_vector = token;
2574 break;
2575
Vu Pham7bb312e2013-10-26 14:31:27 +02002576 case SRP_OPT_TL_RETRY_COUNT:
2577 if (match_int(args, &token) || token < 2 || token > 7) {
2578 pr_warn("bad tl_retry_count parameter '%s' (must be a number between 2 and 7)\n",
2579 p);
2580 goto out;
2581 }
2582 target->tl_retry_count = token;
2583 break;
2584
Roland Dreieraef9ec32005-11-02 14:07:13 -08002585 default:
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002586 pr_warn("unknown parameter or missing value '%s' in target creation request\n",
2587 p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002588 goto out;
2589 }
2590 }
2591
2592 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
2593 ret = 0;
2594 else
2595 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
2596 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
2597 !(srp_opt_tokens[i].token & opt_mask))
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002598 pr_warn("target creation request is missing parameter '%s'\n",
2599 srp_opt_tokens[i].pattern);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002600
Bart Van Assche4d73f952013-10-26 14:40:37 +02002601 if (target->scsi_host->cmd_per_lun > target->scsi_host->can_queue
2602 && (opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
2603 pr_warn("cmd_per_lun = %d > queue_size = %d\n",
2604 target->scsi_host->cmd_per_lun,
2605 target->scsi_host->can_queue);
2606
Roland Dreieraef9ec32005-11-02 14:07:13 -08002607out:
2608 kfree(options);
2609 return ret;
2610}
2611
Tony Jonesee959b02008-02-22 00:13:36 +01002612static ssize_t srp_create_target(struct device *dev,
2613 struct device_attribute *attr,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002614 const char *buf, size_t count)
2615{
2616 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01002617 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002618 struct Scsi_Host *target_host;
2619 struct srp_target_port *target;
David Dillowc07d4242011-01-16 13:57:10 -05002620 struct ib_device *ibdev = host->srp_dev->dev;
Bart Van Asscheb81d00b2013-10-26 14:38:47 +02002621 int ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002622
2623 target_host = scsi_host_alloc(&srp_template,
2624 sizeof (struct srp_target_port));
2625 if (!target_host)
2626 return -ENOMEM;
2627
David Dillow49248642011-01-14 18:23:24 -05002628 target_host->transportt = ib_srp_transport_template;
Bart Van Asschefd1b6c42011-07-13 09:19:16 -07002629 target_host->max_channel = 0;
2630 target_host->max_id = 1;
Arne Redlich3c8edf02006-11-15 12:43:00 +01002631 target_host->max_lun = SRP_MAX_LUN;
2632 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08002633
Roland Dreieraef9ec32005-11-02 14:07:13 -08002634 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002635
David Dillow49248642011-01-14 18:23:24 -05002636 target->io_class = SRP_REV16A_IB_IO_CLASS;
2637 target->scsi_host = target_host;
2638 target->srp_host = host;
2639 target->lkey = host->srp_dev->mr->lkey;
2640 target->rkey = host->srp_dev->mr->rkey;
2641 target->cmd_sg_cnt = cmd_sg_entries;
David Dillowc07d4242011-01-16 13:57:10 -05002642 target->sg_tablesize = indirect_sg_entries ? : cmd_sg_entries;
2643 target->allow_ext_sg = allow_ext_sg;
Vu Pham7bb312e2013-10-26 14:31:27 +02002644 target->tl_retry_count = 7;
Bart Van Assche4d73f952013-10-26 14:40:37 +02002645 target->queue_size = SRP_DEFAULT_QUEUE_SIZE;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002646
Bart Van Assche2d7091b2014-03-14 13:52:45 +01002647 mutex_lock(&host->add_target_mutex);
2648
Roland Dreieraef9ec32005-11-02 14:07:13 -08002649 ret = srp_parse_options(buf, target);
2650 if (ret)
2651 goto err;
2652
Bart Van Assche4d73f952013-10-26 14:40:37 +02002653 target->req_ring_size = target->queue_size - SRP_TSK_MGMT_SQ_SIZE;
2654
Bart Van Assche96fc2482013-06-28 14:51:26 +02002655 if (!srp_conn_unique(target->srp_host, target)) {
2656 shost_printk(KERN_INFO, target->scsi_host,
2657 PFX "Already connected to target port with id_ext=%016llx;ioc_guid=%016llx;initiator_ext=%016llx\n",
2658 be64_to_cpu(target->id_ext),
2659 be64_to_cpu(target->ioc_guid),
2660 be64_to_cpu(target->initiator_ext));
2661 ret = -EEXIST;
2662 goto err;
2663 }
2664
David Dillowc07d4242011-01-16 13:57:10 -05002665 if (!host->srp_dev->fmr_pool && !target->allow_ext_sg &&
2666 target->cmd_sg_cnt < target->sg_tablesize) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002667 pr_warn("No FMR pool and no external indirect descriptors, limiting sg_tablesize to cmd_sg_cnt\n");
David Dillowc07d4242011-01-16 13:57:10 -05002668 target->sg_tablesize = target->cmd_sg_cnt;
2669 }
2670
2671 target_host->sg_tablesize = target->sg_tablesize;
2672 target->indirect_size = target->sg_tablesize *
2673 sizeof (struct srp_direct_buf);
David Dillow49248642011-01-14 18:23:24 -05002674 target->max_iu_len = sizeof (struct srp_cmd) +
2675 sizeof (struct srp_indirect_buf) +
2676 target->cmd_sg_cnt * sizeof (struct srp_direct_buf);
2677
Bart Van Asschec1120f82013-10-26 14:35:08 +02002678 INIT_WORK(&target->tl_err_work, srp_tl_err_work);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002679 INIT_WORK(&target->remove_work, srp_remove_work);
David Dillow8f26c9f2011-01-14 19:45:50 -05002680 spin_lock_init(&target->lock);
2681 INIT_LIST_HEAD(&target->free_tx);
Bart Van Asscheb81d00b2013-10-26 14:38:47 +02002682 ret = srp_alloc_req_data(target);
2683 if (ret)
2684 goto err_free_mem;
David Dillow8f26c9f2011-01-14 19:45:50 -05002685
Sagi Grimberg2088ca62014-03-14 13:51:58 +01002686 ret = ib_query_gid(ibdev, host->port, 0, &target->path.sgid);
2687 if (ret)
2688 goto err_free_mem;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002689
Roland Dreieraef9ec32005-11-02 14:07:13 -08002690 ret = srp_create_target_ib(target);
2691 if (ret)
David Dillow8f26c9f2011-01-14 19:45:50 -05002692 goto err_free_mem;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002693
David Dillow9fe4bcf2008-01-08 17:08:52 -05002694 ret = srp_new_cm_id(target);
2695 if (ret)
David Dillow8f26c9f2011-01-14 19:45:50 -05002696 goto err_free_ib;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002697
2698 ret = srp_connect_target(target);
2699 if (ret) {
David Dillow7aa54bd2008-01-07 18:23:41 -05002700 shost_printk(KERN_ERR, target->scsi_host,
2701 PFX "Connection failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002702 goto err_cm_id;
2703 }
2704
2705 ret = srp_add_target(host, target);
2706 if (ret)
2707 goto err_disconnect;
2708
Bart Van Asschee7ffde02014-03-14 13:52:21 +01002709 shost_printk(KERN_DEBUG, target->scsi_host, PFX
2710 "new target: id_ext %016llx ioc_guid %016llx pkey %04x service_id %016llx sgid %pI6 dgid %pI6\n",
2711 be64_to_cpu(target->id_ext),
2712 be64_to_cpu(target->ioc_guid),
2713 be16_to_cpu(target->path.pkey),
2714 be64_to_cpu(target->service_id),
2715 target->path.sgid.raw, target->path.dgid.raw);
2716
Bart Van Assche2d7091b2014-03-14 13:52:45 +01002717 ret = count;
2718
2719out:
2720 mutex_unlock(&host->add_target_mutex);
2721 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002722
2723err_disconnect:
2724 srp_disconnect_target(target);
2725
2726err_cm_id:
2727 ib_destroy_cm_id(target->cm_id);
2728
David Dillow8f26c9f2011-01-14 19:45:50 -05002729err_free_ib:
Roland Dreieraef9ec32005-11-02 14:07:13 -08002730 srp_free_target_ib(target);
2731
David Dillow8f26c9f2011-01-14 19:45:50 -05002732err_free_mem:
2733 srp_free_req_data(target);
2734
Roland Dreieraef9ec32005-11-02 14:07:13 -08002735err:
2736 scsi_host_put(target_host);
Bart Van Assche2d7091b2014-03-14 13:52:45 +01002737 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002738}
2739
Tony Jonesee959b02008-02-22 00:13:36 +01002740static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002741
Tony Jonesee959b02008-02-22 00:13:36 +01002742static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
2743 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002744{
Tony Jonesee959b02008-02-22 00:13:36 +01002745 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002746
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002747 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002748}
2749
Tony Jonesee959b02008-02-22 00:13:36 +01002750static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002751
Tony Jonesee959b02008-02-22 00:13:36 +01002752static ssize_t show_port(struct device *dev, struct device_attribute *attr,
2753 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002754{
Tony Jonesee959b02008-02-22 00:13:36 +01002755 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002756
2757 return sprintf(buf, "%d\n", host->port);
2758}
2759
Tony Jonesee959b02008-02-22 00:13:36 +01002760static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002761
Roland Dreierf5358a12006-06-17 20:37:29 -07002762static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002763{
2764 struct srp_host *host;
2765
2766 host = kzalloc(sizeof *host, GFP_KERNEL);
2767 if (!host)
2768 return NULL;
2769
2770 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002771 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002772 init_completion(&host->released);
Bart Van Assche2d7091b2014-03-14 13:52:45 +01002773 mutex_init(&host->add_target_mutex);
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002774 host->srp_dev = device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002775 host->port = port;
2776
Tony Jonesee959b02008-02-22 00:13:36 +01002777 host->dev.class = &srp_class;
2778 host->dev.parent = device->dev->dma_device;
Kay Sieversd927e382009-01-06 10:44:39 -08002779 dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002780
Tony Jonesee959b02008-02-22 00:13:36 +01002781 if (device_register(&host->dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07002782 goto free_host;
Tony Jonesee959b02008-02-22 00:13:36 +01002783 if (device_create_file(&host->dev, &dev_attr_add_target))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002784 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01002785 if (device_create_file(&host->dev, &dev_attr_ibdev))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002786 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01002787 if (device_create_file(&host->dev, &dev_attr_port))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002788 goto err_class;
2789
2790 return host;
2791
2792err_class:
Tony Jonesee959b02008-02-22 00:13:36 +01002793 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002794
Roland Dreierf5358a12006-06-17 20:37:29 -07002795free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08002796 kfree(host);
2797
2798 return NULL;
2799}
2800
2801static void srp_add_one(struct ib_device *device)
2802{
Roland Dreierf5358a12006-06-17 20:37:29 -07002803 struct srp_device *srp_dev;
2804 struct ib_device_attr *dev_attr;
2805 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002806 struct srp_host *host;
David Dillowbe8b9812011-01-18 21:58:09 -05002807 int max_pages_per_fmr, fmr_page_shift, s, e, p;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002808
Roland Dreierf5358a12006-06-17 20:37:29 -07002809 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
2810 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08002811 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002812
Roland Dreierf5358a12006-06-17 20:37:29 -07002813 if (ib_query_device(device, dev_attr)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002814 pr_warn("Query device failed for %s\n", device->name);
Roland Dreierf5358a12006-06-17 20:37:29 -07002815 goto free_attr;
2816 }
2817
2818 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
2819 if (!srp_dev)
2820 goto free_attr;
2821
2822 /*
2823 * Use the smallest page size supported by the HCA, down to a
David Dillow8f26c9f2011-01-14 19:45:50 -05002824 * minimum of 4096 bytes. We're unlikely to build large sglists
2825 * out of smaller entries.
Roland Dreierf5358a12006-06-17 20:37:29 -07002826 */
David Dillow8f26c9f2011-01-14 19:45:50 -05002827 fmr_page_shift = max(12, ffs(dev_attr->page_size_cap) - 1);
2828 srp_dev->fmr_page_size = 1 << fmr_page_shift;
2829 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
2830 srp_dev->fmr_max_size = srp_dev->fmr_page_size * SRP_FMR_SIZE;
Roland Dreierf5358a12006-06-17 20:37:29 -07002831
2832 INIT_LIST_HEAD(&srp_dev->dev_list);
2833
2834 srp_dev->dev = device;
2835 srp_dev->pd = ib_alloc_pd(device);
2836 if (IS_ERR(srp_dev->pd))
2837 goto free_dev;
2838
2839 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
2840 IB_ACCESS_LOCAL_WRITE |
2841 IB_ACCESS_REMOTE_READ |
2842 IB_ACCESS_REMOTE_WRITE);
2843 if (IS_ERR(srp_dev->mr))
2844 goto err_pd;
2845
David Dillowbe8b9812011-01-18 21:58:09 -05002846 for (max_pages_per_fmr = SRP_FMR_SIZE;
2847 max_pages_per_fmr >= SRP_FMR_MIN_SIZE;
2848 max_pages_per_fmr /= 2, srp_dev->fmr_max_size /= 2) {
2849 memset(&fmr_param, 0, sizeof fmr_param);
2850 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
2851 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
2852 fmr_param.cache = 1;
2853 fmr_param.max_pages_per_fmr = max_pages_per_fmr;
2854 fmr_param.page_shift = fmr_page_shift;
2855 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
2856 IB_ACCESS_REMOTE_WRITE |
2857 IB_ACCESS_REMOTE_READ);
Roland Dreierf5358a12006-06-17 20:37:29 -07002858
David Dillowbe8b9812011-01-18 21:58:09 -05002859 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
2860 if (!IS_ERR(srp_dev->fmr_pool))
2861 break;
2862 }
2863
Roland Dreierf5358a12006-06-17 20:37:29 -07002864 if (IS_ERR(srp_dev->fmr_pool))
2865 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002866
Tom Tucker07ebafb2006-08-03 16:02:42 -05002867 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002868 s = 0;
2869 e = 0;
2870 } else {
2871 s = 1;
2872 e = device->phys_port_cnt;
2873 }
2874
2875 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07002876 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002877 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07002878 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002879 }
2880
Roland Dreierf5358a12006-06-17 20:37:29 -07002881 ib_set_client_data(device, &srp_client, srp_dev);
2882
2883 goto free_attr;
2884
2885err_pd:
2886 ib_dealloc_pd(srp_dev->pd);
2887
2888free_dev:
2889 kfree(srp_dev);
2890
2891free_attr:
2892 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002893}
2894
2895static void srp_remove_one(struct ib_device *device)
2896{
Roland Dreierf5358a12006-06-17 20:37:29 -07002897 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002898 struct srp_host *host, *tmp_host;
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002899 struct srp_target_port *target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002900
Roland Dreierf5358a12006-06-17 20:37:29 -07002901 srp_dev = ib_get_client_data(device, &srp_client);
Dotan Barak1fe0cb82013-06-12 15:20:36 +02002902 if (!srp_dev)
2903 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002904
Roland Dreierf5358a12006-06-17 20:37:29 -07002905 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Tony Jonesee959b02008-02-22 00:13:36 +01002906 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002907 /*
2908 * Wait for the sysfs entry to go away, so that no new
2909 * target ports can be created.
2910 */
2911 wait_for_completion(&host->released);
2912
2913 /*
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002914 * Remove all target ports.
Roland Dreieraef9ec32005-11-02 14:07:13 -08002915 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002916 spin_lock(&host->target_lock);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002917 list_for_each_entry(target, &host->target_list, list)
2918 srp_queue_remove_work(target);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002919 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002920
2921 /*
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002922 * Wait for target port removal tasks.
Roland Dreieraef9ec32005-11-02 14:07:13 -08002923 */
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002924 flush_workqueue(system_long_wq);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002925
Roland Dreieraef9ec32005-11-02 14:07:13 -08002926 kfree(host);
2927 }
2928
Roland Dreierf5358a12006-06-17 20:37:29 -07002929 if (srp_dev->fmr_pool)
2930 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2931 ib_dereg_mr(srp_dev->mr);
2932 ib_dealloc_pd(srp_dev->pd);
2933
2934 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002935}
2936
FUJITA Tomonori32368222007-06-27 16:33:12 +09002937static struct srp_function_template ib_srp_transport_functions = {
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002938 .has_rport_state = true,
2939 .reset_timer_if_blocked = true,
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002940 .reconnect_delay = &srp_reconnect_delay,
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002941 .fast_io_fail_tmo = &srp_fast_io_fail_tmo,
2942 .dev_loss_tmo = &srp_dev_loss_tmo,
2943 .reconnect = srp_rport_reconnect,
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02002944 .rport_delete = srp_rport_delete,
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002945 .terminate_rport_io = srp_terminate_io,
FUJITA Tomonori32368222007-06-27 16:33:12 +09002946};
2947
Roland Dreieraef9ec32005-11-02 14:07:13 -08002948static int __init srp_init_module(void)
2949{
2950 int ret;
2951
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05002952 BUILD_BUG_ON(FIELD_SIZEOF(struct ib_wc, wr_id) < sizeof(void *));
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00002953
David Dillow49248642011-01-14 18:23:24 -05002954 if (srp_sg_tablesize) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002955 pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n");
David Dillow49248642011-01-14 18:23:24 -05002956 if (!cmd_sg_entries)
2957 cmd_sg_entries = srp_sg_tablesize;
2958 }
2959
2960 if (!cmd_sg_entries)
2961 cmd_sg_entries = SRP_DEF_SG_TABLESIZE;
2962
2963 if (cmd_sg_entries > 255) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002964 pr_warn("Clamping cmd_sg_entries to 255\n");
David Dillow49248642011-01-14 18:23:24 -05002965 cmd_sg_entries = 255;
David Dillow1e89a192008-04-16 21:01:12 -07002966 }
2967
David Dillowc07d4242011-01-16 13:57:10 -05002968 if (!indirect_sg_entries)
2969 indirect_sg_entries = cmd_sg_entries;
2970 else if (indirect_sg_entries < cmd_sg_entries) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002971 pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n",
2972 cmd_sg_entries);
David Dillowc07d4242011-01-16 13:57:10 -05002973 indirect_sg_entries = cmd_sg_entries;
2974 }
2975
FUJITA Tomonori32368222007-06-27 16:33:12 +09002976 ib_srp_transport_template =
2977 srp_attach_transport(&ib_srp_transport_functions);
2978 if (!ib_srp_transport_template)
2979 return -ENOMEM;
2980
Roland Dreieraef9ec32005-11-02 14:07:13 -08002981 ret = class_register(&srp_class);
2982 if (ret) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002983 pr_err("couldn't register class infiniband_srp\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002984 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002985 return ret;
2986 }
2987
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002988 ib_sa_register_client(&srp_sa_client);
2989
Roland Dreieraef9ec32005-11-02 14:07:13 -08002990 ret = ib_register_client(&srp_client);
2991 if (ret) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002992 pr_err("couldn't register IB client\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002993 srp_release_transport(ib_srp_transport_template);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002994 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002995 class_unregister(&srp_class);
2996 return ret;
2997 }
2998
2999 return 0;
3000}
3001
3002static void __exit srp_cleanup_module(void)
3003{
3004 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003005 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003006 class_unregister(&srp_class);
FUJITA Tomonori32368222007-06-27 16:33:12 +09003007 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003008}
3009
3010module_init(srp_init_module);
3011module_exit(srp_cleanup_module);