blob: c2e0ad3992131318e5f4668b75a01be7ef2b07c7 [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
David Dillow8f26c9f2011-01-14 19:45:50 -0500938 fmr = ib_fmr_pool_map_phys(dev->fmr_pool, state->pages,
939 state->npages, io_addr);
940 if (IS_ERR(fmr))
941 return PTR_ERR(fmr);
942
943 *state->next_fmr++ = fmr;
944 state->nfmr++;
945
946 srp_map_desc(state, 0, state->fmr_len, fmr->fmr->rkey);
Bart Van Assche539dde62014-05-20 15:05:46 +0200947
David Dillow8f26c9f2011-01-14 19:45:50 -0500948 return 0;
949}
950
Bart Van Assche539dde62014-05-20 15:05:46 +0200951static int srp_finish_mapping(struct srp_map_state *state,
952 struct srp_target_port *target)
953{
954 int ret = 0;
955
956 if (state->npages == 0)
957 return 0;
958
959 if (state->npages == 1)
960 srp_map_desc(state, state->base_dma_addr, state->fmr_len,
961 target->rkey);
962 else
963 ret = srp_map_finish_fmr(state, target);
964
965 if (ret == 0) {
966 state->npages = 0;
967 state->fmr_len = 0;
968 }
969
970 return ret;
971}
972
David Dillow8f26c9f2011-01-14 19:45:50 -0500973static void srp_map_update_start(struct srp_map_state *state,
974 struct scatterlist *sg, int sg_index,
975 dma_addr_t dma_addr)
976{
977 state->unmapped_sg = sg;
978 state->unmapped_index = sg_index;
979 state->unmapped_addr = dma_addr;
980}
981
982static int srp_map_sg_entry(struct srp_map_state *state,
983 struct srp_target_port *target,
984 struct scatterlist *sg, int sg_index,
985 int use_fmr)
986{
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100987 struct srp_device *dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800988 struct ib_device *ibdev = dev->dev;
David Dillow8f26c9f2011-01-14 19:45:50 -0500989 dma_addr_t dma_addr = ib_sg_dma_address(ibdev, sg);
990 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
991 unsigned int len;
992 int ret;
Roland Dreierf5358a12006-06-17 20:37:29 -0700993
David Dillow8f26c9f2011-01-14 19:45:50 -0500994 if (!dma_len)
995 return 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700996
David Dillow8f26c9f2011-01-14 19:45:50 -0500997 if (use_fmr == SRP_MAP_NO_FMR) {
998 /* Once we're in direct map mode for a request, we don't
999 * go back to FMR mode, so no need to update anything
1000 * other than the descriptor.
1001 */
1002 srp_map_desc(state, dma_addr, dma_len, target->rkey);
1003 return 0;
1004 }
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -07001005
David Dillow8f26c9f2011-01-14 19:45:50 -05001006 /* If we start at an offset into the FMR page, don't merge into
1007 * the current FMR. Finish it out, and use the kernel's MR for this
1008 * sg entry. This is to avoid potential bugs on some SRP targets
1009 * that were never quite defined, but went away when the initiator
1010 * avoided using FMR on such page fragments.
1011 */
1012 if (dma_addr & ~dev->fmr_page_mask || dma_len > dev->fmr_max_size) {
Bart Van Assche539dde62014-05-20 15:05:46 +02001013 ret = srp_finish_mapping(state, target);
David Dillow8f26c9f2011-01-14 19:45:50 -05001014 if (ret)
1015 return ret;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001016
David Dillow8f26c9f2011-01-14 19:45:50 -05001017 srp_map_desc(state, dma_addr, dma_len, target->rkey);
1018 srp_map_update_start(state, NULL, 0, 0);
1019 return 0;
1020 }
1021
1022 /* If this is the first sg to go into the FMR, save our position.
1023 * We need to know the first unmapped entry, its index, and the
1024 * first unmapped address within that entry to be able to restart
1025 * mapping after an error.
1026 */
1027 if (!state->unmapped_sg)
1028 srp_map_update_start(state, sg, sg_index, dma_addr);
1029
1030 while (dma_len) {
1031 if (state->npages == SRP_FMR_SIZE) {
Bart Van Assche539dde62014-05-20 15:05:46 +02001032 ret = srp_finish_mapping(state, target);
David Dillow8f26c9f2011-01-14 19:45:50 -05001033 if (ret)
1034 return ret;
1035
1036 srp_map_update_start(state, sg, sg_index, dma_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -07001037 }
1038
David Dillow8f26c9f2011-01-14 19:45:50 -05001039 len = min_t(unsigned int, dma_len, dev->fmr_page_size);
1040
1041 if (!state->npages)
1042 state->base_dma_addr = dma_addr;
1043 state->pages[state->npages++] = dma_addr;
1044 state->fmr_len += len;
1045 dma_addr += len;
1046 dma_len -= len;
Roland Dreierf5358a12006-06-17 20:37:29 -07001047 }
1048
David Dillow8f26c9f2011-01-14 19:45:50 -05001049 /* If the last entry of the FMR wasn't a full page, then we need to
1050 * close it out and start a new one -- we can only merge at page
1051 * boundries.
1052 */
Roland Dreierf5358a12006-06-17 20:37:29 -07001053 ret = 0;
David Dillow8f26c9f2011-01-14 19:45:50 -05001054 if (len != dev->fmr_page_size) {
Bart Van Assche539dde62014-05-20 15:05:46 +02001055 ret = srp_finish_mapping(state, target);
David Dillow8f26c9f2011-01-14 19:45:50 -05001056 if (!ret)
1057 srp_map_update_start(state, NULL, 0, 0);
1058 }
Roland Dreierf5358a12006-06-17 20:37:29 -07001059 return ret;
1060}
1061
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001062static void srp_map_fmr(struct srp_map_state *state,
1063 struct srp_target_port *target, struct srp_request *req,
1064 struct scatterlist *scat, int count)
1065{
1066 struct srp_device *dev = target->srp_host->srp_dev;
1067 struct ib_device *ibdev = dev->dev;
1068 struct scatterlist *sg;
1069 int i, use_fmr;
1070
1071 state->desc = req->indirect_desc;
1072 state->pages = req->map_page;
1073 state->next_fmr = req->fmr_list;
1074
1075 use_fmr = dev->fmr_pool ? SRP_MAP_ALLOW_FMR : SRP_MAP_NO_FMR;
1076
1077 for_each_sg(scat, sg, count, i) {
1078 if (srp_map_sg_entry(state, target, sg, i, use_fmr)) {
1079 /* FMR mapping failed, so backtrack to the first
1080 * unmapped entry and continue on without using FMR.
1081 */
1082 dma_addr_t dma_addr;
1083 unsigned int dma_len;
1084
1085backtrack:
1086 sg = state->unmapped_sg;
1087 i = state->unmapped_index;
1088
1089 dma_addr = ib_sg_dma_address(ibdev, sg);
1090 dma_len = ib_sg_dma_len(ibdev, sg);
1091 dma_len -= (state->unmapped_addr - dma_addr);
1092 dma_addr = state->unmapped_addr;
1093 use_fmr = SRP_MAP_NO_FMR;
1094 srp_map_desc(state, dma_addr, dma_len, target->rkey);
1095 }
1096 }
1097
Bart Van Assche539dde62014-05-20 15:05:46 +02001098 if (use_fmr == SRP_MAP_ALLOW_FMR && srp_finish_mapping(state, target))
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001099 goto backtrack;
1100
1101 req->nfmr = state->nfmr;
1102}
1103
Roland Dreieraef9ec32005-11-02 14:07:13 -08001104static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
1105 struct srp_request *req)
1106{
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001107 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001108 struct srp_cmd *cmd = req->cmd->buf;
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001109 int len, nents, count;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001110 struct srp_device *dev;
1111 struct ib_device *ibdev;
David Dillow8f26c9f2011-01-14 19:45:50 -05001112 struct srp_map_state state;
1113 struct srp_indirect_buf *indirect_hdr;
David Dillow8f26c9f2011-01-14 19:45:50 -05001114 u32 table_len;
1115 u8 fmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001116
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001117 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001118 return sizeof (struct srp_cmd);
1119
1120 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
1121 scmnd->sc_data_direction != DMA_TO_DEVICE) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001122 shost_printk(KERN_WARNING, target->scsi_host,
1123 PFX "Unhandled data direction %d\n",
1124 scmnd->sc_data_direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001125 return -EINVAL;
1126 }
1127
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001128 nents = scsi_sg_count(scmnd);
1129 scat = scsi_sglist(scmnd);
Roland Dreiercf368712006-03-24 15:47:26 -08001130
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001131 dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001132 ibdev = dev->dev;
1133
1134 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
David Dillow8f26c9f2011-01-14 19:45:50 -05001135 if (unlikely(count == 0))
1136 return -EIO;
Roland Dreierf5358a12006-06-17 20:37:29 -07001137
1138 fmt = SRP_DATA_DESC_DIRECT;
1139 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -08001140
1141 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -07001142 /*
1143 * The midlayer only generated a single gather/scatter
1144 * entry, or DMA mapping coalesced everything to a
1145 * single entry. So a direct descriptor along with
1146 * the DMA MR suffices.
1147 */
Roland Dreieraef9ec32005-11-02 14:07:13 -08001148 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001149
Ralph Campbell85507bc2006-12-12 14:30:55 -08001150 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
David Dillow9af76272010-11-26 15:34:46 -05001151 buf->key = cpu_to_be32(target->rkey);
Ralph Campbell85507bc2006-12-12 14:30:55 -08001152 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Roland Dreiercf368712006-03-24 15:47:26 -08001153
David Dillow8f26c9f2011-01-14 19:45:50 -05001154 req->nfmr = 0;
1155 goto map_complete;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001156 }
1157
David Dillow8f26c9f2011-01-14 19:45:50 -05001158 /* We have more than one scatter/gather entry, so build our indirect
1159 * descriptor table, trying to merge as many entries with FMR as we
1160 * can.
1161 */
1162 indirect_hdr = (void *) cmd->add_data;
1163
David Dillowc07d4242011-01-16 13:57:10 -05001164 ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr,
1165 target->indirect_size, DMA_TO_DEVICE);
1166
David Dillow8f26c9f2011-01-14 19:45:50 -05001167 memset(&state, 0, sizeof(state));
Bart Van Assche76bc1e12014-05-20 15:05:21 +02001168 srp_map_fmr(&state, target, req, scat, count);
David Dillow8f26c9f2011-01-14 19:45:50 -05001169
David Dillowc07d4242011-01-16 13:57:10 -05001170 /* We've mapped the request, now pull as much of the indirect
1171 * descriptor table as we can into the command buffer. If this
1172 * target is not using an external indirect table, we are
1173 * guaranteed to fit into the command, as the SCSI layer won't
1174 * give us more S/G entries than we allow.
David Dillow8f26c9f2011-01-14 19:45:50 -05001175 */
David Dillow8f26c9f2011-01-14 19:45:50 -05001176 if (state.ndesc == 1) {
1177 /* FMR mapping was able to collapse this to one entry,
1178 * so use a direct descriptor.
1179 */
1180 struct srp_direct_buf *buf = (void *) cmd->add_data;
1181
David Dillowc07d4242011-01-16 13:57:10 -05001182 *buf = req->indirect_desc[0];
David Dillow8f26c9f2011-01-14 19:45:50 -05001183 goto map_complete;
1184 }
1185
David Dillowc07d4242011-01-16 13:57:10 -05001186 if (unlikely(target->cmd_sg_cnt < state.ndesc &&
1187 !target->allow_ext_sg)) {
1188 shost_printk(KERN_ERR, target->scsi_host,
1189 "Could not fit S/G list into SRP_CMD\n");
1190 return -EIO;
1191 }
1192
1193 count = min(state.ndesc, target->cmd_sg_cnt);
David Dillow8f26c9f2011-01-14 19:45:50 -05001194 table_len = state.ndesc * sizeof (struct srp_direct_buf);
1195
1196 fmt = SRP_DATA_DESC_INDIRECT;
1197 len = sizeof(struct srp_cmd) + sizeof (struct srp_indirect_buf);
David Dillowc07d4242011-01-16 13:57:10 -05001198 len += count * sizeof (struct srp_direct_buf);
David Dillow8f26c9f2011-01-14 19:45:50 -05001199
David Dillowc07d4242011-01-16 13:57:10 -05001200 memcpy(indirect_hdr->desc_list, req->indirect_desc,
1201 count * sizeof (struct srp_direct_buf));
David Dillow8f26c9f2011-01-14 19:45:50 -05001202
David Dillowc07d4242011-01-16 13:57:10 -05001203 indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr);
David Dillow8f26c9f2011-01-14 19:45:50 -05001204 indirect_hdr->table_desc.key = cpu_to_be32(target->rkey);
1205 indirect_hdr->table_desc.len = cpu_to_be32(table_len);
1206 indirect_hdr->len = cpu_to_be32(state.total_len);
1207
1208 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
David Dillowc07d4242011-01-16 13:57:10 -05001209 cmd->data_out_desc_cnt = count;
David Dillow8f26c9f2011-01-14 19:45:50 -05001210 else
David Dillowc07d4242011-01-16 13:57:10 -05001211 cmd->data_in_desc_cnt = count;
1212
1213 ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len,
1214 DMA_TO_DEVICE);
David Dillow8f26c9f2011-01-14 19:45:50 -05001215
1216map_complete:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001217 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
1218 cmd->buf_fmt = fmt << 4;
1219 else
1220 cmd->buf_fmt = fmt;
1221
Roland Dreieraef9ec32005-11-02 14:07:13 -08001222 return len;
1223}
1224
David Dillow05a1d752010-10-08 14:48:14 -04001225/*
Bart Van Assche76c75b22010-11-26 14:37:47 -05001226 * Return an IU and possible credit to the free pool
1227 */
1228static void srp_put_tx_iu(struct srp_target_port *target, struct srp_iu *iu,
1229 enum srp_iu_type iu_type)
1230{
1231 unsigned long flags;
1232
Bart Van Asschee9684672010-11-26 15:08:38 -05001233 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001234 list_add(&iu->list, &target->free_tx);
1235 if (iu_type != SRP_IU_RSP)
1236 ++target->req_lim;
Bart Van Asschee9684672010-11-26 15:08:38 -05001237 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001238}
1239
1240/*
Bart Van Asschee9684672010-11-26 15:08:38 -05001241 * Must be called with target->lock held to protect req_lim and free_tx.
1242 * If IU is not sent, it must be returned using srp_put_tx_iu().
David Dillow05a1d752010-10-08 14:48:14 -04001243 *
1244 * Note:
1245 * An upper limit for the number of allocated information units for each
1246 * request type is:
1247 * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues
1248 * more than Scsi_Host.can_queue requests.
1249 * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE.
1250 * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than
1251 * one unanswered SRP request to an initiator.
1252 */
1253static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
1254 enum srp_iu_type iu_type)
1255{
1256 s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE;
1257 struct srp_iu *iu;
1258
1259 srp_send_completion(target->send_cq, target);
1260
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001261 if (list_empty(&target->free_tx))
David Dillow05a1d752010-10-08 14:48:14 -04001262 return NULL;
1263
1264 /* Initiator responses to target requests do not consume credits */
Bart Van Assche76c75b22010-11-26 14:37:47 -05001265 if (iu_type != SRP_IU_RSP) {
1266 if (target->req_lim <= rsv) {
1267 ++target->zero_req_lim;
1268 return NULL;
1269 }
1270
1271 --target->req_lim;
David Dillow05a1d752010-10-08 14:48:14 -04001272 }
1273
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001274 iu = list_first_entry(&target->free_tx, struct srp_iu, list);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001275 list_del(&iu->list);
David Dillow05a1d752010-10-08 14:48:14 -04001276 return iu;
1277}
1278
Bart Van Assche76c75b22010-11-26 14:37:47 -05001279static int srp_post_send(struct srp_target_port *target,
1280 struct srp_iu *iu, int len)
David Dillow05a1d752010-10-08 14:48:14 -04001281{
1282 struct ib_sge list;
1283 struct ib_send_wr wr, *bad_wr;
David Dillow05a1d752010-10-08 14:48:14 -04001284
1285 list.addr = iu->dma;
1286 list.length = len;
David Dillow9af76272010-11-26 15:34:46 -05001287 list.lkey = target->lkey;
David Dillow05a1d752010-10-08 14:48:14 -04001288
1289 wr.next = NULL;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001290 wr.wr_id = (uintptr_t) iu;
David Dillow05a1d752010-10-08 14:48:14 -04001291 wr.sg_list = &list;
1292 wr.num_sge = 1;
1293 wr.opcode = IB_WR_SEND;
1294 wr.send_flags = IB_SEND_SIGNALED;
1295
Bart Van Assche76c75b22010-11-26 14:37:47 -05001296 return ib_post_send(target->qp, &wr, &bad_wr);
David Dillow05a1d752010-10-08 14:48:14 -04001297}
1298
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001299static int srp_post_recv(struct srp_target_port *target, struct srp_iu *iu)
Bart Van Asschec996bb42010-07-30 10:59:05 +00001300{
Bart Van Asschec996bb42010-07-30 10:59:05 +00001301 struct ib_recv_wr wr, *bad_wr;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001302 struct ib_sge list;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001303
1304 list.addr = iu->dma;
1305 list.length = iu->size;
David Dillow9af76272010-11-26 15:34:46 -05001306 list.lkey = target->lkey;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001307
1308 wr.next = NULL;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001309 wr.wr_id = (uintptr_t) iu;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001310 wr.sg_list = &list;
1311 wr.num_sge = 1;
1312
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001313 return ib_post_recv(target->qp, &wr, &bad_wr);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001314}
1315
Roland Dreieraef9ec32005-11-02 14:07:13 -08001316static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
1317{
1318 struct srp_request *req;
1319 struct scsi_cmnd *scmnd;
1320 unsigned long flags;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001321
Roland Dreieraef9ec32005-11-02 14:07:13 -08001322 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
Bart Van Asschee9684672010-11-26 15:08:38 -05001323 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -05001324 target->req_lim += be32_to_cpu(rsp->req_lim_delta);
Bart Van Asschee9684672010-11-26 15:08:38 -05001325 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -05001326
David Dillowf8b6e312010-11-26 13:02:21 -05001327 target->tsk_mgmt_status = -1;
1328 if (be32_to_cpu(rsp->resp_data_len) >= 4)
1329 target->tsk_mgmt_status = rsp->data[3];
1330 complete(&target->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001331 } else {
David Dillowf8b6e312010-11-26 13:02:21 -05001332 req = &target->req_ring[rsp->tag];
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01001333 scmnd = srp_claim_req(target, req, NULL, NULL);
Bart Van Assche22032992012-08-14 13:18:53 +00001334 if (!scmnd) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001335 shost_printk(KERN_ERR, target->scsi_host,
1336 "Null scmnd for RSP w/tag %016llx\n",
1337 (unsigned long long) rsp->tag);
Bart Van Assche22032992012-08-14 13:18:53 +00001338
1339 spin_lock_irqsave(&target->lock, flags);
1340 target->req_lim += be32_to_cpu(rsp->req_lim_delta);
1341 spin_unlock_irqrestore(&target->lock, flags);
1342
1343 return;
1344 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001345 scmnd->result = rsp->status;
1346
1347 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
1348 memcpy(scmnd->sense_buffer, rsp->data +
1349 be32_to_cpu(rsp->resp_data_len),
1350 min_t(int, be32_to_cpu(rsp->sense_data_len),
1351 SCSI_SENSE_BUFFERSIZE));
1352 }
1353
1354 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001355 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001356 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001357 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001358
Bart Van Assche22032992012-08-14 13:18:53 +00001359 srp_free_req(target, req, scmnd,
1360 be32_to_cpu(rsp->req_lim_delta));
1361
David Dillowf8b6e312010-11-26 13:02:21 -05001362 scmnd->host_scribble = NULL;
1363 scmnd->scsi_done(scmnd);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001364 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001365}
1366
David Dillowbb125882010-10-08 14:40:47 -04001367static int srp_response_common(struct srp_target_port *target, s32 req_delta,
1368 void *rsp, int len)
1369{
Bart Van Assche76c75b22010-11-26 14:37:47 -05001370 struct ib_device *dev = target->srp_host->srp_dev->dev;
David Dillowbb125882010-10-08 14:40:47 -04001371 unsigned long flags;
1372 struct srp_iu *iu;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001373 int err;
David Dillowbb125882010-10-08 14:40:47 -04001374
Bart Van Asschee9684672010-11-26 15:08:38 -05001375 spin_lock_irqsave(&target->lock, flags);
David Dillowbb125882010-10-08 14:40:47 -04001376 target->req_lim += req_delta;
David Dillowbb125882010-10-08 14:40:47 -04001377 iu = __srp_get_tx_iu(target, SRP_IU_RSP);
Bart Van Asschee9684672010-11-26 15:08:38 -05001378 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001379
David Dillowbb125882010-10-08 14:40:47 -04001380 if (!iu) {
1381 shost_printk(KERN_ERR, target->scsi_host, PFX
1382 "no IU available to send response\n");
Bart Van Assche76c75b22010-11-26 14:37:47 -05001383 return 1;
David Dillowbb125882010-10-08 14:40:47 -04001384 }
1385
1386 ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE);
1387 memcpy(iu->buf, rsp, len);
1388 ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE);
1389
Bart Van Assche76c75b22010-11-26 14:37:47 -05001390 err = srp_post_send(target, iu, len);
1391 if (err) {
David Dillowbb125882010-10-08 14:40:47 -04001392 shost_printk(KERN_ERR, target->scsi_host, PFX
1393 "unable to post response: %d\n", err);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001394 srp_put_tx_iu(target, iu, SRP_IU_RSP);
1395 }
David Dillowbb125882010-10-08 14:40:47 -04001396
David Dillowbb125882010-10-08 14:40:47 -04001397 return err;
1398}
1399
1400static void srp_process_cred_req(struct srp_target_port *target,
1401 struct srp_cred_req *req)
1402{
1403 struct srp_cred_rsp rsp = {
1404 .opcode = SRP_CRED_RSP,
1405 .tag = req->tag,
1406 };
1407 s32 delta = be32_to_cpu(req->req_lim_delta);
1408
1409 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1410 shost_printk(KERN_ERR, target->scsi_host, PFX
1411 "problems processing SRP_CRED_REQ\n");
1412}
1413
1414static void srp_process_aer_req(struct srp_target_port *target,
1415 struct srp_aer_req *req)
1416{
1417 struct srp_aer_rsp rsp = {
1418 .opcode = SRP_AER_RSP,
1419 .tag = req->tag,
1420 };
1421 s32 delta = be32_to_cpu(req->req_lim_delta);
1422
1423 shost_printk(KERN_ERR, target->scsi_host, PFX
1424 "ignoring AER for LUN %llu\n", be64_to_cpu(req->lun));
1425
1426 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1427 shost_printk(KERN_ERR, target->scsi_host, PFX
1428 "problems processing SRP_AER_REQ\n");
1429}
1430
Roland Dreieraef9ec32005-11-02 14:07:13 -08001431static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
1432{
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001433 struct ib_device *dev = target->srp_host->srp_dev->dev;
Roland Dreier737b94e2011-05-23 11:30:04 -07001434 struct srp_iu *iu = (struct srp_iu *) (uintptr_t) wc->wr_id;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001435 int res;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001436 u8 opcode;
1437
Ralph Campbell85507bc2006-12-12 14:30:55 -08001438 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
1439 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001440
1441 opcode = *(u8 *) iu->buf;
1442
1443 if (0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001444 shost_printk(KERN_ERR, target->scsi_host,
1445 PFX "recv completion, opcode 0x%02x\n", opcode);
Bart Van Assche7a700812010-07-29 15:56:37 +00001446 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1,
1447 iu->buf, wc->byte_len, true);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001448 }
1449
1450 switch (opcode) {
1451 case SRP_RSP:
1452 srp_process_rsp(target, iu->buf);
1453 break;
1454
David Dillowbb125882010-10-08 14:40:47 -04001455 case SRP_CRED_REQ:
1456 srp_process_cred_req(target, iu->buf);
1457 break;
1458
1459 case SRP_AER_REQ:
1460 srp_process_aer_req(target, iu->buf);
1461 break;
1462
Roland Dreieraef9ec32005-11-02 14:07:13 -08001463 case SRP_T_LOGOUT:
1464 /* XXX Handle target logout */
David Dillow7aa54bd2008-01-07 18:23:41 -05001465 shost_printk(KERN_WARNING, target->scsi_host,
1466 PFX "Got target logout request\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001467 break;
1468
1469 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001470 shost_printk(KERN_WARNING, target->scsi_host,
1471 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001472 break;
1473 }
1474
Ralph Campbell85507bc2006-12-12 14:30:55 -08001475 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
1476 DMA_FROM_DEVICE);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001477
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001478 res = srp_post_recv(target, iu);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001479 if (res != 0)
1480 shost_printk(KERN_ERR, target->scsi_host,
1481 PFX "Recv failed with error code %d\n", res);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001482}
1483
Bart Van Asschec1120f82013-10-26 14:35:08 +02001484/**
1485 * srp_tl_err_work() - handle a transport layer error
Bart Van Asscheaf246632014-05-20 15:04:21 +02001486 * @work: Work structure embedded in an SRP target port.
Bart Van Asschec1120f82013-10-26 14:35:08 +02001487 *
1488 * Note: This function may get invoked before the rport has been created,
1489 * hence the target->rport test.
1490 */
1491static void srp_tl_err_work(struct work_struct *work)
1492{
1493 struct srp_target_port *target;
1494
1495 target = container_of(work, struct srp_target_port, tl_err_work);
1496 if (target->rport)
1497 srp_start_tl_fail_timers(target->rport);
1498}
1499
Bart Van Asschecd4e3852013-10-10 13:53:25 +02001500static void srp_handle_qp_err(enum ib_wc_status wc_status, bool send_err,
Bart Van Assche948d1e82011-09-03 09:25:42 +02001501 struct srp_target_port *target)
1502{
Bart Van Assche294c8752011-12-25 12:18:12 +00001503 if (target->connected && !target->qp_in_error) {
Bart Van Assche4f0af692012-11-26 11:16:40 +01001504 shost_printk(KERN_ERR, target->scsi_host,
1505 PFX "failed %s status %d\n",
Bart Van Asschecd4e3852013-10-10 13:53:25 +02001506 send_err ? "send" : "receive",
Bart Van Assche4f0af692012-11-26 11:16:40 +01001507 wc_status);
Bart Van Asschec1120f82013-10-26 14:35:08 +02001508 queue_work(system_long_wq, &target->tl_err_work);
Bart Van Assche4f0af692012-11-26 11:16:40 +01001509 }
Bart Van Assche948d1e82011-09-03 09:25:42 +02001510 target->qp_in_error = true;
1511}
1512
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001513static void srp_recv_completion(struct ib_cq *cq, void *target_ptr)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001514{
1515 struct srp_target_port *target = target_ptr;
1516 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001517
1518 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1519 while (ib_poll_cq(cq, 1, &wc) > 0) {
Bart Van Assche948d1e82011-09-03 09:25:42 +02001520 if (likely(wc.status == IB_WC_SUCCESS)) {
1521 srp_handle_recv(target, &wc);
1522 } else {
Bart Van Asschecd4e3852013-10-10 13:53:25 +02001523 srp_handle_qp_err(wc.status, false, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001524 }
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001525 }
1526}
1527
1528static void srp_send_completion(struct ib_cq *cq, void *target_ptr)
1529{
1530 struct srp_target_port *target = target_ptr;
1531 struct ib_wc wc;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001532 struct srp_iu *iu;
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001533
1534 while (ib_poll_cq(cq, 1, &wc) > 0) {
Bart Van Assche948d1e82011-09-03 09:25:42 +02001535 if (likely(wc.status == IB_WC_SUCCESS)) {
1536 iu = (struct srp_iu *) (uintptr_t) wc.wr_id;
1537 list_add(&iu->list, &target->free_tx);
1538 } else {
Bart Van Asschecd4e3852013-10-10 13:53:25 +02001539 srp_handle_qp_err(wc.status, true, target);
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001540 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001541 }
1542}
1543
Bart Van Assche76c75b22010-11-26 14:37:47 -05001544static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001545{
Bart Van Assche76c75b22010-11-26 14:37:47 -05001546 struct srp_target_port *target = host_to_target(shost);
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001547 struct srp_rport *rport = target->rport;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001548 struct srp_request *req;
1549 struct srp_iu *iu;
1550 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001551 struct ib_device *dev;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001552 unsigned long flags;
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001553 int len, result;
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001554 const bool in_scsi_eh = !in_interrupt() && current == shost->ehandler;
1555
1556 /*
1557 * The SCSI EH thread is the only context from which srp_queuecommand()
1558 * can get invoked for blocked devices (SDEV_BLOCK /
1559 * SDEV_CREATED_BLOCK). Avoid racing with srp_reconnect_rport() by
1560 * locking the rport mutex if invoked from inside the SCSI EH.
1561 */
1562 if (in_scsi_eh)
1563 mutex_lock(&rport->mutex);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001564
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001565 result = srp_chkready(target->rport);
1566 if (unlikely(result)) {
1567 scmnd->result = result;
Bart Van Assche2ce19e72013-02-21 17:20:00 +00001568 scmnd->scsi_done(scmnd);
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001569 goto unlock_rport;
Bart Van Assche2ce19e72013-02-21 17:20:00 +00001570 }
1571
Bart Van Asschee9684672010-11-26 15:08:38 -05001572 spin_lock_irqsave(&target->lock, flags);
David Dillowbb125882010-10-08 14:40:47 -04001573 iu = __srp_get_tx_iu(target, SRP_IU_CMD);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001574 if (!iu)
Bart Van Assche695b8342011-01-13 19:02:25 +00001575 goto err_unlock;
1576
1577 req = list_first_entry(&target->free_reqs, struct srp_request, list);
1578 list_del(&req->list);
1579 spin_unlock_irqrestore(&target->lock, flags);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001580
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001581 dev = target->srp_host->srp_dev->dev;
David Dillow49248642011-01-14 18:23:24 -05001582 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08001583 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001584
Roland Dreieraef9ec32005-11-02 14:07:13 -08001585 scmnd->result = 0;
David Dillowf8b6e312010-11-26 13:02:21 -05001586 scmnd->host_scribble = (void *) req;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001587
1588 cmd = iu->buf;
1589 memset(cmd, 0, sizeof *cmd);
1590
1591 cmd->opcode = SRP_CMD;
1592 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001593 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001594 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1595
Roland Dreieraef9ec32005-11-02 14:07:13 -08001596 req->scmnd = scmnd;
1597 req->cmd = iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001598
1599 len = srp_map_data(scmnd, target, req);
1600 if (len < 0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001601 shost_printk(KERN_ERR, target->scsi_host,
1602 PFX "Failed to map data\n");
Bart Van Assche76c75b22010-11-26 14:37:47 -05001603 goto err_iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001604 }
1605
David Dillow49248642011-01-14 18:23:24 -05001606 ib_dma_sync_single_for_device(dev, iu->dma, target->max_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08001607 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001608
Bart Van Assche76c75b22010-11-26 14:37:47 -05001609 if (srp_post_send(target, iu, len)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001610 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001611 goto err_unmap;
1612 }
1613
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001614unlock_rport:
1615 if (in_scsi_eh)
1616 mutex_unlock(&rport->mutex);
1617
Roland Dreieraef9ec32005-11-02 14:07:13 -08001618 return 0;
1619
1620err_unmap:
1621 srp_unmap_data(scmnd, target, req);
1622
Bart Van Assche76c75b22010-11-26 14:37:47 -05001623err_iu:
1624 srp_put_tx_iu(target, iu, SRP_IU_CMD);
1625
Bart Van Assche024ca902014-05-20 15:03:49 +02001626 /*
1627 * Avoid that the loops that iterate over the request ring can
1628 * encounter a dangling SCSI command pointer.
1629 */
1630 req->scmnd = NULL;
1631
Bart Van Asschee9684672010-11-26 15:08:38 -05001632 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001633 list_add(&req->list, &target->free_reqs);
Bart Van Assche695b8342011-01-13 19:02:25 +00001634
1635err_unlock:
Bart Van Asschee9684672010-11-26 15:08:38 -05001636 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001637
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001638 if (in_scsi_eh)
1639 mutex_unlock(&rport->mutex);
1640
Roland Dreieraef9ec32005-11-02 14:07:13 -08001641 return SCSI_MLQUEUE_HOST_BUSY;
1642}
1643
Bart Van Assche4d73f952013-10-26 14:40:37 +02001644/*
1645 * Note: the resources allocated in this function are freed in
1646 * srp_free_target_ib().
1647 */
Roland Dreieraef9ec32005-11-02 14:07:13 -08001648static int srp_alloc_iu_bufs(struct srp_target_port *target)
1649{
1650 int i;
1651
Bart Van Assche4d73f952013-10-26 14:40:37 +02001652 target->rx_ring = kzalloc(target->queue_size * sizeof(*target->rx_ring),
1653 GFP_KERNEL);
1654 if (!target->rx_ring)
1655 goto err_no_ring;
1656 target->tx_ring = kzalloc(target->queue_size * sizeof(*target->tx_ring),
1657 GFP_KERNEL);
1658 if (!target->tx_ring)
1659 goto err_no_ring;
1660
1661 for (i = 0; i < target->queue_size; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001662 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1663 target->max_ti_iu_len,
1664 GFP_KERNEL, DMA_FROM_DEVICE);
1665 if (!target->rx_ring[i])
1666 goto err;
1667 }
1668
Bart Van Assche4d73f952013-10-26 14:40:37 +02001669 for (i = 0; i < target->queue_size; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001670 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
David Dillow49248642011-01-14 18:23:24 -05001671 target->max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001672 GFP_KERNEL, DMA_TO_DEVICE);
1673 if (!target->tx_ring[i])
1674 goto err;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001675
1676 list_add(&target->tx_ring[i]->list, &target->free_tx);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001677 }
1678
1679 return 0;
1680
1681err:
Bart Van Assche4d73f952013-10-26 14:40:37 +02001682 for (i = 0; i < target->queue_size; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001683 srp_free_iu(target->srp_host, target->rx_ring[i]);
Bart Van Assche4d73f952013-10-26 14:40:37 +02001684 srp_free_iu(target->srp_host, target->tx_ring[i]);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001685 }
1686
Bart Van Assche4d73f952013-10-26 14:40:37 +02001687
1688err_no_ring:
1689 kfree(target->tx_ring);
1690 target->tx_ring = NULL;
1691 kfree(target->rx_ring);
1692 target->rx_ring = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001693
1694 return -ENOMEM;
1695}
1696
Bart Van Asschec9b03c12011-09-03 09:34:48 +02001697static uint32_t srp_compute_rq_tmo(struct ib_qp_attr *qp_attr, int attr_mask)
1698{
1699 uint64_t T_tr_ns, max_compl_time_ms;
1700 uint32_t rq_tmo_jiffies;
1701
1702 /*
1703 * According to section 11.2.4.2 in the IBTA spec (Modify Queue Pair,
1704 * table 91), both the QP timeout and the retry count have to be set
1705 * for RC QP's during the RTR to RTS transition.
1706 */
1707 WARN_ON_ONCE((attr_mask & (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)) !=
1708 (IB_QP_TIMEOUT | IB_QP_RETRY_CNT));
1709
1710 /*
1711 * Set target->rq_tmo_jiffies to one second more than the largest time
1712 * it can take before an error completion is generated. See also
1713 * C9-140..142 in the IBTA spec for more information about how to
1714 * convert the QP Local ACK Timeout value to nanoseconds.
1715 */
1716 T_tr_ns = 4096 * (1ULL << qp_attr->timeout);
1717 max_compl_time_ms = qp_attr->retry_cnt * 4 * T_tr_ns;
1718 do_div(max_compl_time_ms, NSEC_PER_MSEC);
1719 rq_tmo_jiffies = msecs_to_jiffies(max_compl_time_ms + 1000);
1720
1721 return rq_tmo_jiffies;
1722}
1723
David Dillow961e0be2011-01-14 17:32:07 -05001724static void srp_cm_rep_handler(struct ib_cm_id *cm_id,
1725 struct srp_login_rsp *lrsp,
1726 struct srp_target_port *target)
1727{
1728 struct ib_qp_attr *qp_attr = NULL;
1729 int attr_mask = 0;
1730 int ret;
1731 int i;
1732
1733 if (lrsp->opcode == SRP_LOGIN_RSP) {
1734 target->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len);
1735 target->req_lim = be32_to_cpu(lrsp->req_lim_delta);
1736
1737 /*
1738 * Reserve credits for task management so we don't
1739 * bounce requests back to the SCSI mid-layer.
1740 */
1741 target->scsi_host->can_queue
1742 = min(target->req_lim - SRP_TSK_MGMT_SQ_SIZE,
1743 target->scsi_host->can_queue);
Bart Van Assche4d73f952013-10-26 14:40:37 +02001744 target->scsi_host->cmd_per_lun
1745 = min_t(int, target->scsi_host->can_queue,
1746 target->scsi_host->cmd_per_lun);
David Dillow961e0be2011-01-14 17:32:07 -05001747 } else {
1748 shost_printk(KERN_WARNING, target->scsi_host,
1749 PFX "Unhandled RSP opcode %#x\n", lrsp->opcode);
1750 ret = -ECONNRESET;
1751 goto error;
1752 }
1753
Bart Van Assche4d73f952013-10-26 14:40:37 +02001754 if (!target->rx_ring) {
David Dillow961e0be2011-01-14 17:32:07 -05001755 ret = srp_alloc_iu_bufs(target);
1756 if (ret)
1757 goto error;
1758 }
1759
1760 ret = -ENOMEM;
1761 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1762 if (!qp_attr)
1763 goto error;
1764
1765 qp_attr->qp_state = IB_QPS_RTR;
1766 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1767 if (ret)
1768 goto error_free;
1769
1770 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
1771 if (ret)
1772 goto error_free;
1773
Bart Van Assche4d73f952013-10-26 14:40:37 +02001774 for (i = 0; i < target->queue_size; i++) {
David Dillow961e0be2011-01-14 17:32:07 -05001775 struct srp_iu *iu = target->rx_ring[i];
1776 ret = srp_post_recv(target, iu);
1777 if (ret)
1778 goto error_free;
1779 }
1780
1781 qp_attr->qp_state = IB_QPS_RTS;
1782 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1783 if (ret)
1784 goto error_free;
1785
Bart Van Asschec9b03c12011-09-03 09:34:48 +02001786 target->rq_tmo_jiffies = srp_compute_rq_tmo(qp_attr, attr_mask);
1787
David Dillow961e0be2011-01-14 17:32:07 -05001788 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
1789 if (ret)
1790 goto error_free;
1791
1792 ret = ib_send_cm_rtu(cm_id, NULL, 0);
1793
1794error_free:
1795 kfree(qp_attr);
1796
1797error:
1798 target->status = ret;
1799}
1800
Roland Dreieraef9ec32005-11-02 14:07:13 -08001801static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1802 struct ib_cm_event *event,
1803 struct srp_target_port *target)
1804{
David Dillow7aa54bd2008-01-07 18:23:41 -05001805 struct Scsi_Host *shost = target->scsi_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001806 struct ib_class_port_info *cpi;
1807 int opcode;
1808
1809 switch (event->param.rej_rcvd.reason) {
1810 case IB_CM_REJ_PORT_CM_REDIRECT:
1811 cpi = event->param.rej_rcvd.ari;
1812 target->path.dlid = cpi->redirect_lid;
1813 target->path.pkey = cpi->redirect_pkey;
1814 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1815 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1816
1817 target->status = target->path.dlid ?
1818 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1819 break;
1820
1821 case IB_CM_REJ_PORT_REDIRECT:
Roland Dreier5d7cbfd2007-08-03 10:45:18 -07001822 if (srp_target_is_topspin(target)) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001823 /*
1824 * Topspin/Cisco SRP gateways incorrectly send
1825 * reject reason code 25 when they mean 24
1826 * (port redirect).
1827 */
1828 memcpy(target->path.dgid.raw,
1829 event->param.rej_rcvd.ari, 16);
1830
David Dillow7aa54bd2008-01-07 18:23:41 -05001831 shost_printk(KERN_DEBUG, shost,
1832 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1833 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1834 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001835
1836 target->status = SRP_PORT_REDIRECT;
1837 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001838 shost_printk(KERN_WARNING, shost,
1839 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001840 target->status = -ECONNRESET;
1841 }
1842 break;
1843
1844 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
David Dillow7aa54bd2008-01-07 18:23:41 -05001845 shost_printk(KERN_WARNING, shost,
1846 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001847 target->status = -ECONNRESET;
1848 break;
1849
1850 case IB_CM_REJ_CONSUMER_DEFINED:
1851 opcode = *(u8 *) event->private_data;
1852 if (opcode == SRP_LOGIN_REJ) {
1853 struct srp_login_rej *rej = event->private_data;
1854 u32 reason = be32_to_cpu(rej->reason);
1855
1856 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
David Dillow7aa54bd2008-01-07 18:23:41 -05001857 shost_printk(KERN_WARNING, shost,
1858 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001859 else
Bart Van Asschee7ffde02014-03-14 13:52:21 +01001860 shost_printk(KERN_WARNING, shost, PFX
1861 "SRP LOGIN from %pI6 to %pI6 REJECTED, reason 0x%08x\n",
1862 target->path.sgid.raw,
1863 target->orig_dgid, reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001864 } else
David Dillow7aa54bd2008-01-07 18:23:41 -05001865 shost_printk(KERN_WARNING, shost,
1866 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1867 " opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001868 target->status = -ECONNRESET;
1869 break;
1870
David Dillow9fe4bcf2008-01-08 17:08:52 -05001871 case IB_CM_REJ_STALE_CONN:
1872 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
1873 target->status = SRP_STALE_CONN;
1874 break;
1875
Roland Dreieraef9ec32005-11-02 14:07:13 -08001876 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001877 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
1878 event->param.rej_rcvd.reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001879 target->status = -ECONNRESET;
1880 }
1881}
1882
1883static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1884{
1885 struct srp_target_port *target = cm_id->context;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001886 int comp = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001887
1888 switch (event->event) {
1889 case IB_CM_REQ_ERROR:
David Dillow7aa54bd2008-01-07 18:23:41 -05001890 shost_printk(KERN_DEBUG, target->scsi_host,
1891 PFX "Sending CM REQ failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001892 comp = 1;
1893 target->status = -ECONNRESET;
1894 break;
1895
1896 case IB_CM_REP_RECEIVED:
1897 comp = 1;
David Dillow961e0be2011-01-14 17:32:07 -05001898 srp_cm_rep_handler(cm_id, event->private_data, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001899 break;
1900
1901 case IB_CM_REJ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001902 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001903 comp = 1;
1904
1905 srp_cm_rej_handler(cm_id, event, target);
1906 break;
1907
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001908 case IB_CM_DREQ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001909 shost_printk(KERN_WARNING, target->scsi_host,
1910 PFX "DREQ received - connection closed\n");
Bart Van Assche294c8752011-12-25 12:18:12 +00001911 srp_change_conn_state(target, false);
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001912 if (ib_send_cm_drep(cm_id, NULL, 0))
David Dillow7aa54bd2008-01-07 18:23:41 -05001913 shost_printk(KERN_ERR, target->scsi_host,
1914 PFX "Sending CM DREP failed\n");
Bart Van Asschec1120f82013-10-26 14:35:08 +02001915 queue_work(system_long_wq, &target->tl_err_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001916 break;
1917
1918 case IB_CM_TIMEWAIT_EXIT:
David Dillow7aa54bd2008-01-07 18:23:41 -05001919 shost_printk(KERN_ERR, target->scsi_host,
1920 PFX "connection closed\n");
Bart Van Asscheac72d762014-03-14 13:53:40 +01001921 comp = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001922
Roland Dreieraef9ec32005-11-02 14:07:13 -08001923 target->status = 0;
1924 break;
1925
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001926 case IB_CM_MRA_RECEIVED:
1927 case IB_CM_DREQ_ERROR:
1928 case IB_CM_DREP_RECEIVED:
1929 break;
1930
Roland Dreieraef9ec32005-11-02 14:07:13 -08001931 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001932 shost_printk(KERN_WARNING, target->scsi_host,
1933 PFX "Unhandled CM event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001934 break;
1935 }
1936
1937 if (comp)
1938 complete(&target->done);
1939
Roland Dreieraef9ec32005-11-02 14:07:13 -08001940 return 0;
1941}
1942
Jack Wang71444b92013-11-07 11:37:37 +01001943/**
1944 * srp_change_queue_type - changing device queue tag type
1945 * @sdev: scsi device struct
1946 * @tag_type: requested tag type
1947 *
1948 * Returns queue tag type.
1949 */
1950static int
1951srp_change_queue_type(struct scsi_device *sdev, int tag_type)
1952{
1953 if (sdev->tagged_supported) {
1954 scsi_set_tag_type(sdev, tag_type);
1955 if (tag_type)
1956 scsi_activate_tcq(sdev, sdev->queue_depth);
1957 else
1958 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1959 } else
1960 tag_type = 0;
1961
1962 return tag_type;
1963}
1964
1965/**
1966 * srp_change_queue_depth - setting device queue depth
1967 * @sdev: scsi device struct
1968 * @qdepth: requested queue depth
1969 * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
1970 * (see include/scsi/scsi_host.h for definition)
1971 *
1972 * Returns queue depth.
1973 */
1974static int
1975srp_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1976{
1977 struct Scsi_Host *shost = sdev->host;
1978 int max_depth;
1979 if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP) {
1980 max_depth = shost->can_queue;
1981 if (!sdev->tagged_supported)
1982 max_depth = 1;
1983 if (qdepth > max_depth)
1984 qdepth = max_depth;
1985 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1986 } else if (reason == SCSI_QDEPTH_QFULL)
1987 scsi_track_queue_full(sdev, qdepth);
1988 else
1989 return -EOPNOTSUPP;
1990
1991 return sdev->queue_depth;
1992}
1993
Roland Dreierd945e1d2006-05-09 10:50:28 -07001994static int srp_send_tsk_mgmt(struct srp_target_port *target,
David Dillowf8b6e312010-11-26 13:02:21 -05001995 u64 req_tag, unsigned int lun, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001996{
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001997 struct srp_rport *rport = target->rport;
David Dillow19081f32010-10-18 08:54:49 -04001998 struct ib_device *dev = target->srp_host->srp_dev->dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001999 struct srp_iu *iu;
2000 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002001
Bart Van Assche3780d1f2013-02-21 17:18:00 +00002002 if (!target->connected || target->qp_in_error)
2003 return -1;
2004
David Dillowf8b6e312010-11-26 13:02:21 -05002005 init_completion(&target->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002006
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002007 /*
2008 * Lock the rport mutex to avoid that srp_create_target_ib() is
2009 * invoked while a task management function is being sent.
2010 */
2011 mutex_lock(&rport->mutex);
Bart Van Asschee9684672010-11-26 15:08:38 -05002012 spin_lock_irq(&target->lock);
David Dillowbb125882010-10-08 14:40:47 -04002013 iu = __srp_get_tx_iu(target, SRP_IU_TSK_MGMT);
Bart Van Asschee9684672010-11-26 15:08:38 -05002014 spin_unlock_irq(&target->lock);
Bart Van Assche76c75b22010-11-26 14:37:47 -05002015
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002016 if (!iu) {
2017 mutex_unlock(&rport->mutex);
2018
Bart Van Assche76c75b22010-11-26 14:37:47 -05002019 return -1;
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002020 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002021
David Dillow19081f32010-10-18 08:54:49 -04002022 ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt,
2023 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002024 tsk_mgmt = iu->buf;
2025 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
2026
2027 tsk_mgmt->opcode = SRP_TSK_MGMT;
David Dillowf8b6e312010-11-26 13:02:21 -05002028 tsk_mgmt->lun = cpu_to_be64((u64) lun << 48);
2029 tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002030 tsk_mgmt->tsk_mgmt_func = func;
David Dillowf8b6e312010-11-26 13:02:21 -05002031 tsk_mgmt->task_tag = req_tag;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002032
David Dillow19081f32010-10-18 08:54:49 -04002033 ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt,
2034 DMA_TO_DEVICE);
Bart Van Assche76c75b22010-11-26 14:37:47 -05002035 if (srp_post_send(target, iu, sizeof *tsk_mgmt)) {
2036 srp_put_tx_iu(target, iu, SRP_IU_TSK_MGMT);
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002037 mutex_unlock(&rport->mutex);
2038
Bart Van Assche76c75b22010-11-26 14:37:47 -05002039 return -1;
2040 }
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002041 mutex_unlock(&rport->mutex);
Roland Dreierd945e1d2006-05-09 10:50:28 -07002042
David Dillowf8b6e312010-11-26 13:02:21 -05002043 if (!wait_for_completion_timeout(&target->tsk_mgmt_done,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002044 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07002045 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002046
Roland Dreierd945e1d2006-05-09 10:50:28 -07002047 return 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07002048}
2049
Roland Dreieraef9ec32005-11-02 14:07:13 -08002050static int srp_abort(struct scsi_cmnd *scmnd)
2051{
Roland Dreierd945e1d2006-05-09 10:50:28 -07002052 struct srp_target_port *target = host_to_target(scmnd->device->host);
David Dillowf8b6e312010-11-26 13:02:21 -05002053 struct srp_request *req = (struct srp_request *) scmnd->host_scribble;
Bart Van Assche086f44f2013-06-12 15:23:04 +02002054 int ret;
Roland Dreierd945e1d2006-05-09 10:50:28 -07002055
David Dillow7aa54bd2008-01-07 18:23:41 -05002056 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002057
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01002058 if (!req || !srp_claim_req(target, req, NULL, scmnd))
Bart Van Assche99b66972013-10-10 13:52:33 +02002059 return SUCCESS;
Bart Van Assche086f44f2013-06-12 15:23:04 +02002060 if (srp_send_tsk_mgmt(target, req->index, scmnd->device->lun,
Bart Van Assche80d5e8a2013-07-10 17:36:35 +02002061 SRP_TSK_ABORT_TASK) == 0)
Bart Van Assche086f44f2013-06-12 15:23:04 +02002062 ret = SUCCESS;
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002063 else if (target->rport->state == SRP_RPORT_LOST)
Bart Van Assche99e1c132013-06-28 14:49:58 +02002064 ret = FAST_IO_FAIL;
Bart Van Assche086f44f2013-06-12 15:23:04 +02002065 else
2066 ret = FAILED;
Bart Van Assche22032992012-08-14 13:18:53 +00002067 srp_free_req(target, req, scmnd, 0);
2068 scmnd->result = DID_ABORT << 16;
Bart Van Assched8536672012-08-24 10:29:11 +00002069 scmnd->scsi_done(scmnd);
Roland Dreierd945e1d2006-05-09 10:50:28 -07002070
Bart Van Assche086f44f2013-06-12 15:23:04 +02002071 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002072}
2073
2074static int srp_reset_device(struct scsi_cmnd *scmnd)
2075{
Roland Dreierd945e1d2006-05-09 10:50:28 -07002076 struct srp_target_port *target = host_to_target(scmnd->device->host);
Bart Van Assche536ae142010-11-26 13:58:27 -05002077 int i;
Roland Dreierd945e1d2006-05-09 10:50:28 -07002078
David Dillow7aa54bd2008-01-07 18:23:41 -05002079 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002080
David Dillowf8b6e312010-11-26 13:02:21 -05002081 if (srp_send_tsk_mgmt(target, SRP_TAG_NO_REQ, scmnd->device->lun,
2082 SRP_TSK_LUN_RESET))
Roland Dreierd945e1d2006-05-09 10:50:28 -07002083 return FAILED;
David Dillowf8b6e312010-11-26 13:02:21 -05002084 if (target->tsk_mgmt_status)
Roland Dreierd945e1d2006-05-09 10:50:28 -07002085 return FAILED;
2086
Bart Van Assche4d73f952013-10-26 14:40:37 +02002087 for (i = 0; i < target->req_ring_size; ++i) {
Bart Van Assche536ae142010-11-26 13:58:27 -05002088 struct srp_request *req = &target->req_ring[i];
Bart Van Asscheb3fe6282014-03-14 13:54:11 +01002089 srp_finish_req(target, req, scmnd->device, DID_RESET << 16);
Bart Van Assche536ae142010-11-26 13:58:27 -05002090 }
Roland Dreierd945e1d2006-05-09 10:50:28 -07002091
Roland Dreierd945e1d2006-05-09 10:50:28 -07002092 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002093}
2094
2095static int srp_reset_host(struct scsi_cmnd *scmnd)
2096{
2097 struct srp_target_port *target = host_to_target(scmnd->device->host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002098
David Dillow7aa54bd2008-01-07 18:23:41 -05002099 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002100
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002101 return srp_reconnect_rport(target->rport) == 0 ? SUCCESS : FAILED;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002102}
2103
Bart Van Asschec9b03c12011-09-03 09:34:48 +02002104static int srp_slave_configure(struct scsi_device *sdev)
2105{
2106 struct Scsi_Host *shost = sdev->host;
2107 struct srp_target_port *target = host_to_target(shost);
2108 struct request_queue *q = sdev->request_queue;
2109 unsigned long timeout;
2110
2111 if (sdev->type == TYPE_DISK) {
2112 timeout = max_t(unsigned, 30 * HZ, target->rq_tmo_jiffies);
2113 blk_queue_rq_timeout(q, timeout);
2114 }
2115
2116 return 0;
2117}
2118
Tony Jonesee959b02008-02-22 00:13:36 +01002119static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
2120 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002121{
Tony Jonesee959b02008-02-22 00:13:36 +01002122 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002123
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002124 return sprintf(buf, "0x%016llx\n",
2125 (unsigned long long) be64_to_cpu(target->id_ext));
2126}
2127
Tony Jonesee959b02008-02-22 00:13:36 +01002128static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
2129 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002130{
Tony Jonesee959b02008-02-22 00:13:36 +01002131 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002132
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002133 return sprintf(buf, "0x%016llx\n",
2134 (unsigned long long) be64_to_cpu(target->ioc_guid));
2135}
2136
Tony Jonesee959b02008-02-22 00:13:36 +01002137static ssize_t show_service_id(struct device *dev,
2138 struct device_attribute *attr, 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
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002142 return sprintf(buf, "0x%016llx\n",
2143 (unsigned long long) be64_to_cpu(target->service_id));
2144}
2145
Tony Jonesee959b02008-02-22 00:13:36 +01002146static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
2147 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002148{
Tony Jonesee959b02008-02-22 00:13:36 +01002149 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002150
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002151 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
2152}
2153
Bart Van Assche848b3082013-10-26 14:38:12 +02002154static ssize_t show_sgid(struct device *dev, struct device_attribute *attr,
2155 char *buf)
2156{
2157 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2158
2159 return sprintf(buf, "%pI6\n", target->path.sgid.raw);
2160}
2161
Tony Jonesee959b02008-02-22 00:13:36 +01002162static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
2163 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002164{
Tony Jonesee959b02008-02-22 00:13:36 +01002165 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002166
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002167 return sprintf(buf, "%pI6\n", target->path.dgid.raw);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002168}
2169
Tony Jonesee959b02008-02-22 00:13:36 +01002170static ssize_t show_orig_dgid(struct device *dev,
2171 struct device_attribute *attr, char *buf)
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002172{
Tony Jonesee959b02008-02-22 00:13:36 +01002173 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002174
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002175 return sprintf(buf, "%pI6\n", target->orig_dgid);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002176}
2177
Bart Van Assche89de7482010-08-03 14:08:45 +00002178static ssize_t show_req_lim(struct device *dev,
2179 struct device_attribute *attr, char *buf)
2180{
2181 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2182
Bart Van Assche89de7482010-08-03 14:08:45 +00002183 return sprintf(buf, "%d\n", target->req_lim);
2184}
2185
Tony Jonesee959b02008-02-22 00:13:36 +01002186static ssize_t show_zero_req_lim(struct device *dev,
2187 struct device_attribute *attr, char *buf)
Roland Dreier6bfa24f2006-06-17 20:37:33 -07002188{
Tony Jonesee959b02008-02-22 00:13:36 +01002189 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6bfa24f2006-06-17 20:37:33 -07002190
Roland Dreier6bfa24f2006-06-17 20:37:33 -07002191 return sprintf(buf, "%d\n", target->zero_req_lim);
2192}
2193
Tony Jonesee959b02008-02-22 00:13:36 +01002194static ssize_t show_local_ib_port(struct device *dev,
2195 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002196{
Tony Jonesee959b02008-02-22 00:13:36 +01002197 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002198
2199 return sprintf(buf, "%d\n", target->srp_host->port);
2200}
2201
Tony Jonesee959b02008-02-22 00:13:36 +01002202static ssize_t show_local_ib_device(struct device *dev,
2203 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002204{
Tony Jonesee959b02008-02-22 00:13:36 +01002205 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002206
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002207 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002208}
2209
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002210static ssize_t show_comp_vector(struct device *dev,
2211 struct device_attribute *attr, char *buf)
2212{
2213 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2214
2215 return sprintf(buf, "%d\n", target->comp_vector);
2216}
2217
Vu Pham7bb312e2013-10-26 14:31:27 +02002218static ssize_t show_tl_retry_count(struct device *dev,
2219 struct device_attribute *attr, char *buf)
2220{
2221 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2222
2223 return sprintf(buf, "%d\n", target->tl_retry_count);
2224}
2225
David Dillow49248642011-01-14 18:23:24 -05002226static ssize_t show_cmd_sg_entries(struct device *dev,
2227 struct device_attribute *attr, char *buf)
2228{
2229 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2230
2231 return sprintf(buf, "%u\n", target->cmd_sg_cnt);
2232}
2233
David Dillowc07d4242011-01-16 13:57:10 -05002234static ssize_t show_allow_ext_sg(struct device *dev,
2235 struct device_attribute *attr, char *buf)
2236{
2237 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2238
2239 return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false");
2240}
2241
Tony Jonesee959b02008-02-22 00:13:36 +01002242static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
2243static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
2244static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
2245static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
Bart Van Assche848b3082013-10-26 14:38:12 +02002246static DEVICE_ATTR(sgid, S_IRUGO, show_sgid, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01002247static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
2248static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
Bart Van Assche89de7482010-08-03 14:08:45 +00002249static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01002250static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
2251static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
2252static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002253static DEVICE_ATTR(comp_vector, S_IRUGO, show_comp_vector, NULL);
Vu Pham7bb312e2013-10-26 14:31:27 +02002254static DEVICE_ATTR(tl_retry_count, S_IRUGO, show_tl_retry_count, NULL);
David Dillow49248642011-01-14 18:23:24 -05002255static DEVICE_ATTR(cmd_sg_entries, S_IRUGO, show_cmd_sg_entries, NULL);
David Dillowc07d4242011-01-16 13:57:10 -05002256static DEVICE_ATTR(allow_ext_sg, S_IRUGO, show_allow_ext_sg, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002257
Tony Jonesee959b02008-02-22 00:13:36 +01002258static struct device_attribute *srp_host_attrs[] = {
2259 &dev_attr_id_ext,
2260 &dev_attr_ioc_guid,
2261 &dev_attr_service_id,
2262 &dev_attr_pkey,
Bart Van Assche848b3082013-10-26 14:38:12 +02002263 &dev_attr_sgid,
Tony Jonesee959b02008-02-22 00:13:36 +01002264 &dev_attr_dgid,
2265 &dev_attr_orig_dgid,
Bart Van Assche89de7482010-08-03 14:08:45 +00002266 &dev_attr_req_lim,
Tony Jonesee959b02008-02-22 00:13:36 +01002267 &dev_attr_zero_req_lim,
2268 &dev_attr_local_ib_port,
2269 &dev_attr_local_ib_device,
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002270 &dev_attr_comp_vector,
Vu Pham7bb312e2013-10-26 14:31:27 +02002271 &dev_attr_tl_retry_count,
David Dillow49248642011-01-14 18:23:24 -05002272 &dev_attr_cmd_sg_entries,
David Dillowc07d4242011-01-16 13:57:10 -05002273 &dev_attr_allow_ext_sg,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002274 NULL
2275};
2276
Roland Dreieraef9ec32005-11-02 14:07:13 -08002277static struct scsi_host_template srp_template = {
2278 .module = THIS_MODULE,
Roland Dreierb7f008f2007-05-06 21:18:11 -07002279 .name = "InfiniBand SRP initiator",
2280 .proc_name = DRV_NAME,
Bart Van Asschec9b03c12011-09-03 09:34:48 +02002281 .slave_configure = srp_slave_configure,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002282 .info = srp_target_info,
2283 .queuecommand = srp_queuecommand,
Jack Wang71444b92013-11-07 11:37:37 +01002284 .change_queue_depth = srp_change_queue_depth,
2285 .change_queue_type = srp_change_queue_type,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002286 .eh_abort_handler = srp_abort,
2287 .eh_device_reset_handler = srp_reset_device,
2288 .eh_host_reset_handler = srp_reset_host,
Bart Van Assche2742c1d2013-06-12 15:24:25 +02002289 .skip_settle_delay = true,
David Dillow49248642011-01-14 18:23:24 -05002290 .sg_tablesize = SRP_DEF_SG_TABLESIZE,
Bart Van Assche4d73f952013-10-26 14:40:37 +02002291 .can_queue = SRP_DEFAULT_CMD_SQ_SIZE,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002292 .this_id = -1,
Bart Van Assche4d73f952013-10-26 14:40:37 +02002293 .cmd_per_lun = SRP_DEFAULT_CMD_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002294 .use_clustering = ENABLE_CLUSTERING,
2295 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08002296};
2297
2298static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
2299{
FUJITA Tomonori32368222007-06-27 16:33:12 +09002300 struct srp_rport_identifiers ids;
2301 struct srp_rport *rport;
2302
Roland Dreieraef9ec32005-11-02 14:07:13 -08002303 sprintf(target->target_name, "SRP.T10:%016llX",
2304 (unsigned long long) be64_to_cpu(target->id_ext));
2305
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002306 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002307 return -ENODEV;
2308
FUJITA Tomonori32368222007-06-27 16:33:12 +09002309 memcpy(ids.port_id, &target->id_ext, 8);
2310 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09002311 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori32368222007-06-27 16:33:12 +09002312 rport = srp_rport_add(target->scsi_host, &ids);
2313 if (IS_ERR(rport)) {
2314 scsi_remove_host(target->scsi_host);
2315 return PTR_ERR(rport);
2316 }
2317
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02002318 rport->lld_data = target;
Bart Van Assche9dd69a62013-10-26 14:32:30 +02002319 target->rport = rport;
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02002320
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002321 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002322 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002323 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002324
2325 target->state = SRP_TARGET_LIVE;
2326
Roland Dreieraef9ec32005-11-02 14:07:13 -08002327 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07002328 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002329
2330 return 0;
2331}
2332
Tony Jonesee959b02008-02-22 00:13:36 +01002333static void srp_release_dev(struct device *dev)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002334{
2335 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01002336 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002337
2338 complete(&host->released);
2339}
2340
2341static struct class srp_class = {
2342 .name = "infiniband_srp",
Tony Jonesee959b02008-02-22 00:13:36 +01002343 .dev_release = srp_release_dev
Roland Dreieraef9ec32005-11-02 14:07:13 -08002344};
2345
Bart Van Assche96fc2482013-06-28 14:51:26 +02002346/**
2347 * srp_conn_unique() - check whether the connection to a target is unique
Bart Van Asscheaf246632014-05-20 15:04:21 +02002348 * @host: SRP host.
2349 * @target: SRP target port.
Bart Van Assche96fc2482013-06-28 14:51:26 +02002350 */
2351static bool srp_conn_unique(struct srp_host *host,
2352 struct srp_target_port *target)
2353{
2354 struct srp_target_port *t;
2355 bool ret = false;
2356
2357 if (target->state == SRP_TARGET_REMOVED)
2358 goto out;
2359
2360 ret = true;
2361
2362 spin_lock(&host->target_lock);
2363 list_for_each_entry(t, &host->target_list, list) {
2364 if (t != target &&
2365 target->id_ext == t->id_ext &&
2366 target->ioc_guid == t->ioc_guid &&
2367 target->initiator_ext == t->initiator_ext) {
2368 ret = false;
2369 break;
2370 }
2371 }
2372 spin_unlock(&host->target_lock);
2373
2374out:
2375 return ret;
2376}
2377
Roland Dreieraef9ec32005-11-02 14:07:13 -08002378/*
2379 * Target ports are added by writing
2380 *
2381 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
2382 * pkey=<P_Key>,service_id=<service ID>
2383 *
2384 * to the add_target sysfs attribute.
2385 */
2386enum {
2387 SRP_OPT_ERR = 0,
2388 SRP_OPT_ID_EXT = 1 << 0,
2389 SRP_OPT_IOC_GUID = 1 << 1,
2390 SRP_OPT_DGID = 1 << 2,
2391 SRP_OPT_PKEY = 1 << 3,
2392 SRP_OPT_SERVICE_ID = 1 << 4,
2393 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07002394 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07002395 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002396 SRP_OPT_INITIATOR_EXT = 1 << 8,
David Dillow49248642011-01-14 18:23:24 -05002397 SRP_OPT_CMD_SG_ENTRIES = 1 << 9,
David Dillowc07d4242011-01-16 13:57:10 -05002398 SRP_OPT_ALLOW_EXT_SG = 1 << 10,
2399 SRP_OPT_SG_TABLESIZE = 1 << 11,
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002400 SRP_OPT_COMP_VECTOR = 1 << 12,
Vu Pham7bb312e2013-10-26 14:31:27 +02002401 SRP_OPT_TL_RETRY_COUNT = 1 << 13,
Bart Van Assche4d73f952013-10-26 14:40:37 +02002402 SRP_OPT_QUEUE_SIZE = 1 << 14,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002403 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
2404 SRP_OPT_IOC_GUID |
2405 SRP_OPT_DGID |
2406 SRP_OPT_PKEY |
2407 SRP_OPT_SERVICE_ID),
2408};
2409
Steven Whitehousea447c092008-10-13 10:46:57 +01002410static const match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07002411 { SRP_OPT_ID_EXT, "id_ext=%s" },
2412 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
2413 { SRP_OPT_DGID, "dgid=%s" },
2414 { SRP_OPT_PKEY, "pkey=%x" },
2415 { SRP_OPT_SERVICE_ID, "service_id=%s" },
2416 { SRP_OPT_MAX_SECT, "max_sect=%d" },
2417 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07002418 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002419 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
David Dillow49248642011-01-14 18:23:24 -05002420 { SRP_OPT_CMD_SG_ENTRIES, "cmd_sg_entries=%u" },
David Dillowc07d4242011-01-16 13:57:10 -05002421 { SRP_OPT_ALLOW_EXT_SG, "allow_ext_sg=%u" },
2422 { SRP_OPT_SG_TABLESIZE, "sg_tablesize=%u" },
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002423 { SRP_OPT_COMP_VECTOR, "comp_vector=%u" },
Vu Pham7bb312e2013-10-26 14:31:27 +02002424 { SRP_OPT_TL_RETRY_COUNT, "tl_retry_count=%u" },
Bart Van Assche4d73f952013-10-26 14:40:37 +02002425 { SRP_OPT_QUEUE_SIZE, "queue_size=%d" },
Vu Pham52fb2b502006-06-17 20:37:31 -07002426 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002427};
2428
2429static int srp_parse_options(const char *buf, struct srp_target_port *target)
2430{
2431 char *options, *sep_opt;
2432 char *p;
2433 char dgid[3];
2434 substring_t args[MAX_OPT_ARGS];
2435 int opt_mask = 0;
2436 int token;
2437 int ret = -EINVAL;
2438 int i;
2439
2440 options = kstrdup(buf, GFP_KERNEL);
2441 if (!options)
2442 return -ENOMEM;
2443
2444 sep_opt = options;
2445 while ((p = strsep(&sep_opt, ",")) != NULL) {
2446 if (!*p)
2447 continue;
2448
2449 token = match_token(p, srp_opt_tokens, args);
2450 opt_mask |= token;
2451
2452 switch (token) {
2453 case SRP_OPT_ID_EXT:
2454 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002455 if (!p) {
2456 ret = -ENOMEM;
2457 goto out;
2458 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002459 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2460 kfree(p);
2461 break;
2462
2463 case SRP_OPT_IOC_GUID:
2464 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002465 if (!p) {
2466 ret = -ENOMEM;
2467 goto out;
2468 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002469 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
2470 kfree(p);
2471 break;
2472
2473 case SRP_OPT_DGID:
2474 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002475 if (!p) {
2476 ret = -ENOMEM;
2477 goto out;
2478 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002479 if (strlen(p) != 32) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002480 pr_warn("bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07002481 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002482 goto out;
2483 }
2484
2485 for (i = 0; i < 16; ++i) {
2486 strlcpy(dgid, p + i * 2, 3);
2487 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
2488 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08002489 kfree(p);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002490 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002491 break;
2492
2493 case SRP_OPT_PKEY:
2494 if (match_hex(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002495 pr_warn("bad P_Key parameter '%s'\n", p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002496 goto out;
2497 }
2498 target->path.pkey = cpu_to_be16(token);
2499 break;
2500
2501 case SRP_OPT_SERVICE_ID:
2502 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002503 if (!p) {
2504 ret = -ENOMEM;
2505 goto out;
2506 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002507 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
Sean Hefty247e0202007-08-08 15:51:18 -07002508 target->path.service_id = target->service_id;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002509 kfree(p);
2510 break;
2511
2512 case SRP_OPT_MAX_SECT:
2513 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002514 pr_warn("bad max sect parameter '%s'\n", p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002515 goto out;
2516 }
2517 target->scsi_host->max_sectors = token;
2518 break;
2519
Bart Van Assche4d73f952013-10-26 14:40:37 +02002520 case SRP_OPT_QUEUE_SIZE:
2521 if (match_int(args, &token) || token < 1) {
2522 pr_warn("bad queue_size parameter '%s'\n", p);
2523 goto out;
2524 }
2525 target->scsi_host->can_queue = token;
2526 target->queue_size = token + SRP_RSP_SQ_SIZE +
2527 SRP_TSK_MGMT_SQ_SIZE;
2528 if (!(opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
2529 target->scsi_host->cmd_per_lun = token;
2530 break;
2531
Vu Pham52fb2b502006-06-17 20:37:31 -07002532 case SRP_OPT_MAX_CMD_PER_LUN:
Bart Van Assche4d73f952013-10-26 14:40:37 +02002533 if (match_int(args, &token) || token < 1) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002534 pr_warn("bad max cmd_per_lun parameter '%s'\n",
2535 p);
Vu Pham52fb2b502006-06-17 20:37:31 -07002536 goto out;
2537 }
Bart Van Assche4d73f952013-10-26 14:40:37 +02002538 target->scsi_host->cmd_per_lun = token;
Vu Pham52fb2b502006-06-17 20:37:31 -07002539 break;
2540
Ramachandra K0c0450db2006-06-17 20:37:38 -07002541 case SRP_OPT_IO_CLASS:
2542 if (match_hex(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002543 pr_warn("bad IO class parameter '%s'\n", p);
Ramachandra K0c0450db2006-06-17 20:37:38 -07002544 goto out;
2545 }
2546 if (token != SRP_REV10_IB_IO_CLASS &&
2547 token != SRP_REV16A_IB_IO_CLASS) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002548 pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n",
2549 token, SRP_REV10_IB_IO_CLASS,
2550 SRP_REV16A_IB_IO_CLASS);
Ramachandra K0c0450db2006-06-17 20:37:38 -07002551 goto out;
2552 }
2553 target->io_class = token;
2554 break;
2555
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002556 case SRP_OPT_INITIATOR_EXT:
2557 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002558 if (!p) {
2559 ret = -ENOMEM;
2560 goto out;
2561 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002562 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2563 kfree(p);
2564 break;
2565
David Dillow49248642011-01-14 18:23:24 -05002566 case SRP_OPT_CMD_SG_ENTRIES:
2567 if (match_int(args, &token) || token < 1 || token > 255) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002568 pr_warn("bad max cmd_sg_entries parameter '%s'\n",
2569 p);
David Dillow49248642011-01-14 18:23:24 -05002570 goto out;
2571 }
2572 target->cmd_sg_cnt = token;
2573 break;
2574
David Dillowc07d4242011-01-16 13:57:10 -05002575 case SRP_OPT_ALLOW_EXT_SG:
2576 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002577 pr_warn("bad allow_ext_sg parameter '%s'\n", p);
David Dillowc07d4242011-01-16 13:57:10 -05002578 goto out;
2579 }
2580 target->allow_ext_sg = !!token;
2581 break;
2582
2583 case SRP_OPT_SG_TABLESIZE:
2584 if (match_int(args, &token) || token < 1 ||
2585 token > SCSI_MAX_SG_CHAIN_SEGMENTS) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002586 pr_warn("bad max sg_tablesize parameter '%s'\n",
2587 p);
David Dillowc07d4242011-01-16 13:57:10 -05002588 goto out;
2589 }
2590 target->sg_tablesize = token;
2591 break;
2592
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002593 case SRP_OPT_COMP_VECTOR:
2594 if (match_int(args, &token) || token < 0) {
2595 pr_warn("bad comp_vector parameter '%s'\n", p);
2596 goto out;
2597 }
2598 target->comp_vector = token;
2599 break;
2600
Vu Pham7bb312e2013-10-26 14:31:27 +02002601 case SRP_OPT_TL_RETRY_COUNT:
2602 if (match_int(args, &token) || token < 2 || token > 7) {
2603 pr_warn("bad tl_retry_count parameter '%s' (must be a number between 2 and 7)\n",
2604 p);
2605 goto out;
2606 }
2607 target->tl_retry_count = token;
2608 break;
2609
Roland Dreieraef9ec32005-11-02 14:07:13 -08002610 default:
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002611 pr_warn("unknown parameter or missing value '%s' in target creation request\n",
2612 p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002613 goto out;
2614 }
2615 }
2616
2617 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
2618 ret = 0;
2619 else
2620 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
2621 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
2622 !(srp_opt_tokens[i].token & opt_mask))
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002623 pr_warn("target creation request is missing parameter '%s'\n",
2624 srp_opt_tokens[i].pattern);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002625
Bart Van Assche4d73f952013-10-26 14:40:37 +02002626 if (target->scsi_host->cmd_per_lun > target->scsi_host->can_queue
2627 && (opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
2628 pr_warn("cmd_per_lun = %d > queue_size = %d\n",
2629 target->scsi_host->cmd_per_lun,
2630 target->scsi_host->can_queue);
2631
Roland Dreieraef9ec32005-11-02 14:07:13 -08002632out:
2633 kfree(options);
2634 return ret;
2635}
2636
Tony Jonesee959b02008-02-22 00:13:36 +01002637static ssize_t srp_create_target(struct device *dev,
2638 struct device_attribute *attr,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002639 const char *buf, size_t count)
2640{
2641 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01002642 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002643 struct Scsi_Host *target_host;
2644 struct srp_target_port *target;
David Dillowc07d4242011-01-16 13:57:10 -05002645 struct ib_device *ibdev = host->srp_dev->dev;
Bart Van Asscheb81d00b2013-10-26 14:38:47 +02002646 int ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002647
2648 target_host = scsi_host_alloc(&srp_template,
2649 sizeof (struct srp_target_port));
2650 if (!target_host)
2651 return -ENOMEM;
2652
David Dillow49248642011-01-14 18:23:24 -05002653 target_host->transportt = ib_srp_transport_template;
Bart Van Asschefd1b6c42011-07-13 09:19:16 -07002654 target_host->max_channel = 0;
2655 target_host->max_id = 1;
Arne Redlich3c8edf02006-11-15 12:43:00 +01002656 target_host->max_lun = SRP_MAX_LUN;
2657 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08002658
Roland Dreieraef9ec32005-11-02 14:07:13 -08002659 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002660
David Dillow49248642011-01-14 18:23:24 -05002661 target->io_class = SRP_REV16A_IB_IO_CLASS;
2662 target->scsi_host = target_host;
2663 target->srp_host = host;
2664 target->lkey = host->srp_dev->mr->lkey;
2665 target->rkey = host->srp_dev->mr->rkey;
2666 target->cmd_sg_cnt = cmd_sg_entries;
David Dillowc07d4242011-01-16 13:57:10 -05002667 target->sg_tablesize = indirect_sg_entries ? : cmd_sg_entries;
2668 target->allow_ext_sg = allow_ext_sg;
Vu Pham7bb312e2013-10-26 14:31:27 +02002669 target->tl_retry_count = 7;
Bart Van Assche4d73f952013-10-26 14:40:37 +02002670 target->queue_size = SRP_DEFAULT_QUEUE_SIZE;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002671
Bart Van Assche2d7091b2014-03-14 13:52:45 +01002672 mutex_lock(&host->add_target_mutex);
2673
Roland Dreieraef9ec32005-11-02 14:07:13 -08002674 ret = srp_parse_options(buf, target);
2675 if (ret)
2676 goto err;
2677
Bart Van Assche4d73f952013-10-26 14:40:37 +02002678 target->req_ring_size = target->queue_size - SRP_TSK_MGMT_SQ_SIZE;
2679
Bart Van Assche96fc2482013-06-28 14:51:26 +02002680 if (!srp_conn_unique(target->srp_host, target)) {
2681 shost_printk(KERN_INFO, target->scsi_host,
2682 PFX "Already connected to target port with id_ext=%016llx;ioc_guid=%016llx;initiator_ext=%016llx\n",
2683 be64_to_cpu(target->id_ext),
2684 be64_to_cpu(target->ioc_guid),
2685 be64_to_cpu(target->initiator_ext));
2686 ret = -EEXIST;
2687 goto err;
2688 }
2689
David Dillowc07d4242011-01-16 13:57:10 -05002690 if (!host->srp_dev->fmr_pool && !target->allow_ext_sg &&
2691 target->cmd_sg_cnt < target->sg_tablesize) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002692 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 -05002693 target->sg_tablesize = target->cmd_sg_cnt;
2694 }
2695
2696 target_host->sg_tablesize = target->sg_tablesize;
2697 target->indirect_size = target->sg_tablesize *
2698 sizeof (struct srp_direct_buf);
David Dillow49248642011-01-14 18:23:24 -05002699 target->max_iu_len = sizeof (struct srp_cmd) +
2700 sizeof (struct srp_indirect_buf) +
2701 target->cmd_sg_cnt * sizeof (struct srp_direct_buf);
2702
Bart Van Asschec1120f82013-10-26 14:35:08 +02002703 INIT_WORK(&target->tl_err_work, srp_tl_err_work);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002704 INIT_WORK(&target->remove_work, srp_remove_work);
David Dillow8f26c9f2011-01-14 19:45:50 -05002705 spin_lock_init(&target->lock);
2706 INIT_LIST_HEAD(&target->free_tx);
Bart Van Asscheb81d00b2013-10-26 14:38:47 +02002707 ret = srp_alloc_req_data(target);
2708 if (ret)
2709 goto err_free_mem;
David Dillow8f26c9f2011-01-14 19:45:50 -05002710
Sagi Grimberg2088ca62014-03-14 13:51:58 +01002711 ret = ib_query_gid(ibdev, host->port, 0, &target->path.sgid);
2712 if (ret)
2713 goto err_free_mem;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002714
Roland Dreieraef9ec32005-11-02 14:07:13 -08002715 ret = srp_create_target_ib(target);
2716 if (ret)
David Dillow8f26c9f2011-01-14 19:45:50 -05002717 goto err_free_mem;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002718
David Dillow9fe4bcf2008-01-08 17:08:52 -05002719 ret = srp_new_cm_id(target);
2720 if (ret)
David Dillow8f26c9f2011-01-14 19:45:50 -05002721 goto err_free_ib;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002722
2723 ret = srp_connect_target(target);
2724 if (ret) {
David Dillow7aa54bd2008-01-07 18:23:41 -05002725 shost_printk(KERN_ERR, target->scsi_host,
2726 PFX "Connection failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002727 goto err_cm_id;
2728 }
2729
2730 ret = srp_add_target(host, target);
2731 if (ret)
2732 goto err_disconnect;
2733
Bart Van Asschee7ffde02014-03-14 13:52:21 +01002734 shost_printk(KERN_DEBUG, target->scsi_host, PFX
2735 "new target: id_ext %016llx ioc_guid %016llx pkey %04x service_id %016llx sgid %pI6 dgid %pI6\n",
2736 be64_to_cpu(target->id_ext),
2737 be64_to_cpu(target->ioc_guid),
2738 be16_to_cpu(target->path.pkey),
2739 be64_to_cpu(target->service_id),
2740 target->path.sgid.raw, target->path.dgid.raw);
2741
Bart Van Assche2d7091b2014-03-14 13:52:45 +01002742 ret = count;
2743
2744out:
2745 mutex_unlock(&host->add_target_mutex);
2746 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002747
2748err_disconnect:
2749 srp_disconnect_target(target);
2750
2751err_cm_id:
2752 ib_destroy_cm_id(target->cm_id);
2753
David Dillow8f26c9f2011-01-14 19:45:50 -05002754err_free_ib:
Roland Dreieraef9ec32005-11-02 14:07:13 -08002755 srp_free_target_ib(target);
2756
David Dillow8f26c9f2011-01-14 19:45:50 -05002757err_free_mem:
2758 srp_free_req_data(target);
2759
Roland Dreieraef9ec32005-11-02 14:07:13 -08002760err:
2761 scsi_host_put(target_host);
Bart Van Assche2d7091b2014-03-14 13:52:45 +01002762 goto out;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002763}
2764
Tony Jonesee959b02008-02-22 00:13:36 +01002765static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002766
Tony Jonesee959b02008-02-22 00:13:36 +01002767static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
2768 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002769{
Tony Jonesee959b02008-02-22 00:13:36 +01002770 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002771
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002772 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002773}
2774
Tony Jonesee959b02008-02-22 00:13:36 +01002775static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002776
Tony Jonesee959b02008-02-22 00:13:36 +01002777static ssize_t show_port(struct device *dev, struct device_attribute *attr,
2778 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002779{
Tony Jonesee959b02008-02-22 00:13:36 +01002780 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002781
2782 return sprintf(buf, "%d\n", host->port);
2783}
2784
Tony Jonesee959b02008-02-22 00:13:36 +01002785static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002786
Roland Dreierf5358a12006-06-17 20:37:29 -07002787static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002788{
2789 struct srp_host *host;
2790
2791 host = kzalloc(sizeof *host, GFP_KERNEL);
2792 if (!host)
2793 return NULL;
2794
2795 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002796 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002797 init_completion(&host->released);
Bart Van Assche2d7091b2014-03-14 13:52:45 +01002798 mutex_init(&host->add_target_mutex);
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002799 host->srp_dev = device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002800 host->port = port;
2801
Tony Jonesee959b02008-02-22 00:13:36 +01002802 host->dev.class = &srp_class;
2803 host->dev.parent = device->dev->dma_device;
Kay Sieversd927e382009-01-06 10:44:39 -08002804 dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002805
Tony Jonesee959b02008-02-22 00:13:36 +01002806 if (device_register(&host->dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07002807 goto free_host;
Tony Jonesee959b02008-02-22 00:13:36 +01002808 if (device_create_file(&host->dev, &dev_attr_add_target))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002809 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01002810 if (device_create_file(&host->dev, &dev_attr_ibdev))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002811 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01002812 if (device_create_file(&host->dev, &dev_attr_port))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002813 goto err_class;
2814
2815 return host;
2816
2817err_class:
Tony Jonesee959b02008-02-22 00:13:36 +01002818 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002819
Roland Dreierf5358a12006-06-17 20:37:29 -07002820free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08002821 kfree(host);
2822
2823 return NULL;
2824}
2825
2826static void srp_add_one(struct ib_device *device)
2827{
Roland Dreierf5358a12006-06-17 20:37:29 -07002828 struct srp_device *srp_dev;
2829 struct ib_device_attr *dev_attr;
2830 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002831 struct srp_host *host;
David Dillowbe8b9812011-01-18 21:58:09 -05002832 int max_pages_per_fmr, fmr_page_shift, s, e, p;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002833
Roland Dreierf5358a12006-06-17 20:37:29 -07002834 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
2835 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08002836 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002837
Roland Dreierf5358a12006-06-17 20:37:29 -07002838 if (ib_query_device(device, dev_attr)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002839 pr_warn("Query device failed for %s\n", device->name);
Roland Dreierf5358a12006-06-17 20:37:29 -07002840 goto free_attr;
2841 }
2842
2843 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
2844 if (!srp_dev)
2845 goto free_attr;
2846
2847 /*
2848 * Use the smallest page size supported by the HCA, down to a
David Dillow8f26c9f2011-01-14 19:45:50 -05002849 * minimum of 4096 bytes. We're unlikely to build large sglists
2850 * out of smaller entries.
Roland Dreierf5358a12006-06-17 20:37:29 -07002851 */
David Dillow8f26c9f2011-01-14 19:45:50 -05002852 fmr_page_shift = max(12, ffs(dev_attr->page_size_cap) - 1);
2853 srp_dev->fmr_page_size = 1 << fmr_page_shift;
2854 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
2855 srp_dev->fmr_max_size = srp_dev->fmr_page_size * SRP_FMR_SIZE;
Roland Dreierf5358a12006-06-17 20:37:29 -07002856
2857 INIT_LIST_HEAD(&srp_dev->dev_list);
2858
2859 srp_dev->dev = device;
2860 srp_dev->pd = ib_alloc_pd(device);
2861 if (IS_ERR(srp_dev->pd))
2862 goto free_dev;
2863
2864 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
2865 IB_ACCESS_LOCAL_WRITE |
2866 IB_ACCESS_REMOTE_READ |
2867 IB_ACCESS_REMOTE_WRITE);
2868 if (IS_ERR(srp_dev->mr))
2869 goto err_pd;
2870
David Dillowbe8b9812011-01-18 21:58:09 -05002871 for (max_pages_per_fmr = SRP_FMR_SIZE;
2872 max_pages_per_fmr >= SRP_FMR_MIN_SIZE;
2873 max_pages_per_fmr /= 2, srp_dev->fmr_max_size /= 2) {
2874 memset(&fmr_param, 0, sizeof fmr_param);
2875 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
2876 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
2877 fmr_param.cache = 1;
2878 fmr_param.max_pages_per_fmr = max_pages_per_fmr;
2879 fmr_param.page_shift = fmr_page_shift;
2880 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
2881 IB_ACCESS_REMOTE_WRITE |
2882 IB_ACCESS_REMOTE_READ);
Roland Dreierf5358a12006-06-17 20:37:29 -07002883
David Dillowbe8b9812011-01-18 21:58:09 -05002884 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
2885 if (!IS_ERR(srp_dev->fmr_pool))
2886 break;
2887 }
2888
Roland Dreierf5358a12006-06-17 20:37:29 -07002889 if (IS_ERR(srp_dev->fmr_pool))
2890 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002891
Tom Tucker07ebafb2006-08-03 16:02:42 -05002892 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002893 s = 0;
2894 e = 0;
2895 } else {
2896 s = 1;
2897 e = device->phys_port_cnt;
2898 }
2899
2900 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07002901 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002902 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07002903 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002904 }
2905
Roland Dreierf5358a12006-06-17 20:37:29 -07002906 ib_set_client_data(device, &srp_client, srp_dev);
2907
2908 goto free_attr;
2909
2910err_pd:
2911 ib_dealloc_pd(srp_dev->pd);
2912
2913free_dev:
2914 kfree(srp_dev);
2915
2916free_attr:
2917 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002918}
2919
2920static void srp_remove_one(struct ib_device *device)
2921{
Roland Dreierf5358a12006-06-17 20:37:29 -07002922 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002923 struct srp_host *host, *tmp_host;
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002924 struct srp_target_port *target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002925
Roland Dreierf5358a12006-06-17 20:37:29 -07002926 srp_dev = ib_get_client_data(device, &srp_client);
Dotan Barak1fe0cb82013-06-12 15:20:36 +02002927 if (!srp_dev)
2928 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002929
Roland Dreierf5358a12006-06-17 20:37:29 -07002930 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Tony Jonesee959b02008-02-22 00:13:36 +01002931 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002932 /*
2933 * Wait for the sysfs entry to go away, so that no new
2934 * target ports can be created.
2935 */
2936 wait_for_completion(&host->released);
2937
2938 /*
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002939 * Remove all target ports.
Roland Dreieraef9ec32005-11-02 14:07:13 -08002940 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002941 spin_lock(&host->target_lock);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002942 list_for_each_entry(target, &host->target_list, list)
2943 srp_queue_remove_work(target);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002944 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002945
2946 /*
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002947 * Wait for target port removal tasks.
Roland Dreieraef9ec32005-11-02 14:07:13 -08002948 */
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002949 flush_workqueue(system_long_wq);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002950
Roland Dreieraef9ec32005-11-02 14:07:13 -08002951 kfree(host);
2952 }
2953
Roland Dreierf5358a12006-06-17 20:37:29 -07002954 if (srp_dev->fmr_pool)
2955 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2956 ib_dereg_mr(srp_dev->mr);
2957 ib_dealloc_pd(srp_dev->pd);
2958
2959 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002960}
2961
FUJITA Tomonori32368222007-06-27 16:33:12 +09002962static struct srp_function_template ib_srp_transport_functions = {
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002963 .has_rport_state = true,
2964 .reset_timer_if_blocked = true,
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002965 .reconnect_delay = &srp_reconnect_delay,
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002966 .fast_io_fail_tmo = &srp_fast_io_fail_tmo,
2967 .dev_loss_tmo = &srp_dev_loss_tmo,
2968 .reconnect = srp_rport_reconnect,
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02002969 .rport_delete = srp_rport_delete,
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002970 .terminate_rport_io = srp_terminate_io,
FUJITA Tomonori32368222007-06-27 16:33:12 +09002971};
2972
Roland Dreieraef9ec32005-11-02 14:07:13 -08002973static int __init srp_init_module(void)
2974{
2975 int ret;
2976
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05002977 BUILD_BUG_ON(FIELD_SIZEOF(struct ib_wc, wr_id) < sizeof(void *));
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00002978
David Dillow49248642011-01-14 18:23:24 -05002979 if (srp_sg_tablesize) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002980 pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n");
David Dillow49248642011-01-14 18:23:24 -05002981 if (!cmd_sg_entries)
2982 cmd_sg_entries = srp_sg_tablesize;
2983 }
2984
2985 if (!cmd_sg_entries)
2986 cmd_sg_entries = SRP_DEF_SG_TABLESIZE;
2987
2988 if (cmd_sg_entries > 255) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002989 pr_warn("Clamping cmd_sg_entries to 255\n");
David Dillow49248642011-01-14 18:23:24 -05002990 cmd_sg_entries = 255;
David Dillow1e89a192008-04-16 21:01:12 -07002991 }
2992
David Dillowc07d4242011-01-16 13:57:10 -05002993 if (!indirect_sg_entries)
2994 indirect_sg_entries = cmd_sg_entries;
2995 else if (indirect_sg_entries < cmd_sg_entries) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002996 pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n",
2997 cmd_sg_entries);
David Dillowc07d4242011-01-16 13:57:10 -05002998 indirect_sg_entries = cmd_sg_entries;
2999 }
3000
FUJITA Tomonori32368222007-06-27 16:33:12 +09003001 ib_srp_transport_template =
3002 srp_attach_transport(&ib_srp_transport_functions);
3003 if (!ib_srp_transport_template)
3004 return -ENOMEM;
3005
Roland Dreieraef9ec32005-11-02 14:07:13 -08003006 ret = class_register(&srp_class);
3007 if (ret) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003008 pr_err("couldn't register class infiniband_srp\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09003009 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003010 return ret;
3011 }
3012
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003013 ib_sa_register_client(&srp_sa_client);
3014
Roland Dreieraef9ec32005-11-02 14:07:13 -08003015 ret = ib_register_client(&srp_client);
3016 if (ret) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00003017 pr_err("couldn't register IB client\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09003018 srp_release_transport(ib_srp_transport_template);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003019 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003020 class_unregister(&srp_class);
3021 return ret;
3022 }
3023
3024 return 0;
3025}
3026
3027static void __exit srp_cleanup_module(void)
3028{
3029 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003030 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003031 class_unregister(&srp_class);
FUJITA Tomonori32368222007-06-27 16:33:12 +09003032 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08003033}
3034
3035module_init(srp_init_module);
3036module_exit(srp_cleanup_module);