blob: a8c06c4451c0f00853a5b99a0c4fbfaf37b0406e [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{
293 struct ib_qp_init_attr *init_attr;
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100294 struct ib_cq *recv_cq, *send_cq;
295 struct ib_qp *qp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800296 int ret;
297
298 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
299 if (!init_attr)
300 return -ENOMEM;
301
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100302 recv_cq = ib_create_cq(target->srp_host->srp_dev->dev,
Bart Van Assche4d73f952013-10-26 14:40:37 +0200303 srp_recv_completion, NULL, target,
304 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
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100310 send_cq = ib_create_cq(target->srp_host->srp_dev->dev,
Bart Van Assche4d73f952013-10-26 14:40:37 +0200311 srp_send_completion, NULL, target,
312 target->queue_size, target->comp_vector);
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100313 if (IS_ERR(send_cq)) {
314 ret = PTR_ERR(send_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800315 goto err_recv_cq;
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000316 }
317
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100318 ib_req_notify_cq(recv_cq, IB_CQ_NEXT_COMP);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800319
320 init_attr->event_handler = srp_qp_event;
Bart Van Assche4d73f952013-10-26 14:40:37 +0200321 init_attr->cap.max_send_wr = target->queue_size;
322 init_attr->cap.max_recv_wr = target->queue_size;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800323 init_attr->cap.max_recv_sge = 1;
324 init_attr->cap.max_send_sge = 1;
325 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
326 init_attr->qp_type = IB_QPT_RC;
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100327 init_attr->send_cq = send_cq;
328 init_attr->recv_cq = recv_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800329
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100330 qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr);
331 if (IS_ERR(qp)) {
332 ret = PTR_ERR(qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800333 goto err_send_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800334 }
335
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100336 ret = srp_init_qp(target, qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800337 if (ret)
338 goto err_qp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800339
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100340 if (target->qp)
341 ib_destroy_qp(target->qp);
342 if (target->recv_cq)
343 ib_destroy_cq(target->recv_cq);
344 if (target->send_cq)
345 ib_destroy_cq(target->send_cq);
346
347 target->qp = qp;
348 target->recv_cq = recv_cq;
349 target->send_cq = send_cq;
350
Roland Dreierda9d2f02010-02-24 15:07:59 -0800351 kfree(init_attr);
352 return 0;
353
354err_qp:
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100355 ib_destroy_qp(qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800356
357err_send_cq:
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100358 ib_destroy_cq(send_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800359
360err_recv_cq:
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100361 ib_destroy_cq(recv_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800362
363err:
Roland Dreieraef9ec32005-11-02 14:07:13 -0800364 kfree(init_attr);
365 return ret;
366}
367
Bart Van Assche4d73f952013-10-26 14:40:37 +0200368/*
369 * Note: this function may be called without srp_alloc_iu_bufs() having been
370 * invoked. Hence the target->[rt]x_ring checks.
371 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800372static void srp_free_target_ib(struct srp_target_port *target)
373{
374 int i;
375
376 ib_destroy_qp(target->qp);
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000377 ib_destroy_cq(target->send_cq);
378 ib_destroy_cq(target->recv_cq);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800379
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100380 target->qp = NULL;
381 target->send_cq = target->recv_cq = NULL;
382
Bart Van Assche4d73f952013-10-26 14:40:37 +0200383 if (target->rx_ring) {
384 for (i = 0; i < target->queue_size; ++i)
385 srp_free_iu(target->srp_host, target->rx_ring[i]);
386 kfree(target->rx_ring);
387 target->rx_ring = NULL;
388 }
389 if (target->tx_ring) {
390 for (i = 0; i < target->queue_size; ++i)
391 srp_free_iu(target->srp_host, target->tx_ring[i]);
392 kfree(target->tx_ring);
393 target->tx_ring = NULL;
394 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800395}
396
397static void srp_path_rec_completion(int status,
398 struct ib_sa_path_rec *pathrec,
399 void *target_ptr)
400{
401 struct srp_target_port *target = target_ptr;
402
403 target->status = status;
404 if (status)
David Dillow7aa54bd2008-01-07 18:23:41 -0500405 shost_printk(KERN_ERR, target->scsi_host,
406 PFX "Got failed path rec status %d\n", status);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800407 else
408 target->path = *pathrec;
409 complete(&target->done);
410}
411
412static int srp_lookup_path(struct srp_target_port *target)
413{
414 target->path.numb_path = 1;
415
416 init_completion(&target->done);
417
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700418 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100419 target->srp_host->srp_dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800420 target->srp_host->port,
421 &target->path,
Sean Hefty247e0202007-08-08 15:51:18 -0700422 IB_SA_PATH_REC_SERVICE_ID |
Roland Dreieraef9ec32005-11-02 14:07:13 -0800423 IB_SA_PATH_REC_DGID |
424 IB_SA_PATH_REC_SGID |
425 IB_SA_PATH_REC_NUMB_PATH |
426 IB_SA_PATH_REC_PKEY,
427 SRP_PATH_REC_TIMEOUT_MS,
428 GFP_KERNEL,
429 srp_path_rec_completion,
430 target, &target->path_query);
431 if (target->path_query_id < 0)
432 return target->path_query_id;
433
434 wait_for_completion(&target->done);
435
436 if (target->status < 0)
David Dillow7aa54bd2008-01-07 18:23:41 -0500437 shost_printk(KERN_WARNING, target->scsi_host,
438 PFX "Path record query failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800439
440 return target->status;
441}
442
443static int srp_send_req(struct srp_target_port *target)
444{
445 struct {
446 struct ib_cm_req_param param;
447 struct srp_login_req priv;
448 } *req = NULL;
449 int status;
450
451 req = kzalloc(sizeof *req, GFP_KERNEL);
452 if (!req)
453 return -ENOMEM;
454
455 req->param.primary_path = &target->path;
456 req->param.alternate_path = NULL;
457 req->param.service_id = target->service_id;
458 req->param.qp_num = target->qp->qp_num;
459 req->param.qp_type = target->qp->qp_type;
460 req->param.private_data = &req->priv;
461 req->param.private_data_len = sizeof req->priv;
462 req->param.flow_control = 1;
463
464 get_random_bytes(&req->param.starting_psn, 4);
465 req->param.starting_psn &= 0xffffff;
466
467 /*
468 * Pick some arbitrary defaults here; we could make these
469 * module parameters if anyone cared about setting them.
470 */
471 req->param.responder_resources = 4;
472 req->param.remote_cm_response_timeout = 20;
473 req->param.local_cm_response_timeout = 20;
Vu Pham7bb312e2013-10-26 14:31:27 +0200474 req->param.retry_count = target->tl_retry_count;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800475 req->param.rnr_retry_count = 7;
476 req->param.max_cm_retries = 15;
477
478 req->priv.opcode = SRP_LOGIN_REQ;
479 req->priv.tag = 0;
David Dillow49248642011-01-14 18:23:24 -0500480 req->priv.req_it_iu_len = cpu_to_be32(target->max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800481 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
482 SRP_BUF_FORMAT_INDIRECT);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700483 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700484 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450db2006-06-17 20:37:38 -0700485 * port identifier format is 8 bytes of ID extension followed
486 * by 8 bytes of GUID. Older drafts put the two halves in the
487 * opposite order, so that the GUID comes first.
488 *
489 * Targets conforming to these obsolete drafts can be
490 * recognized by the I/O Class they report.
491 */
492 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
493 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200494 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700495 memcpy(req->priv.initiator_port_id + 8,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200496 &target->initiator_ext, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700497 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
498 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
499 } else {
500 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200501 &target->initiator_ext, 8);
502 memcpy(req->priv.initiator_port_id + 8,
503 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700504 memcpy(req->priv.target_port_id, &target->id_ext, 8);
505 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
506 }
507
Roland Dreieraef9ec32005-11-02 14:07:13 -0800508 /*
509 * Topspin/Cisco SRP targets will reject our login unless we
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200510 * zero out the first 8 bytes of our initiator port ID and set
511 * the second 8 bytes to the local node GUID.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800512 */
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700513 if (srp_target_is_topspin(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500514 shost_printk(KERN_DEBUG, target->scsi_host,
515 PFX "Topspin/Cisco initiator port ID workaround "
516 "activated for target GUID %016llx\n",
517 (unsigned long long) be64_to_cpu(target->ioc_guid));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800518 memset(req->priv.initiator_port_id, 0, 8);
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200519 memcpy(req->priv.initiator_port_id + 8,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100520 &target->srp_host->srp_dev->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800521 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800522
523 status = ib_send_cm_req(target->cm_id, &req->param);
524
525 kfree(req);
526
527 return status;
528}
529
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000530static bool srp_queue_remove_work(struct srp_target_port *target)
531{
532 bool changed = false;
533
534 spin_lock_irq(&target->lock);
535 if (target->state != SRP_TARGET_REMOVED) {
536 target->state = SRP_TARGET_REMOVED;
537 changed = true;
538 }
539 spin_unlock_irq(&target->lock);
540
541 if (changed)
542 queue_work(system_long_wq, &target->remove_work);
543
544 return changed;
545}
546
Bart Van Assche294c8752011-12-25 12:18:12 +0000547static bool srp_change_conn_state(struct srp_target_port *target,
548 bool connected)
549{
550 bool changed = false;
551
552 spin_lock_irq(&target->lock);
553 if (target->connected != connected) {
554 target->connected = connected;
555 changed = true;
556 }
557 spin_unlock_irq(&target->lock);
558
559 return changed;
560}
561
Roland Dreieraef9ec32005-11-02 14:07:13 -0800562static void srp_disconnect_target(struct srp_target_port *target)
563{
Bart Van Assche294c8752011-12-25 12:18:12 +0000564 if (srp_change_conn_state(target, false)) {
565 /* XXX should send SRP_I_LOGOUT request */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800566
Bart Van Assche294c8752011-12-25 12:18:12 +0000567 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
568 shost_printk(KERN_DEBUG, target->scsi_host,
569 PFX "Sending CM DREQ failed\n");
Bart Van Assche294c8752011-12-25 12:18:12 +0000570 }
Roland Dreiere6581052006-05-17 09:13:21 -0700571 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800572}
573
David Dillow8f26c9f2011-01-14 19:45:50 -0500574static void srp_free_req_data(struct srp_target_port *target)
575{
David Dillowc07d4242011-01-16 13:57:10 -0500576 struct ib_device *ibdev = target->srp_host->srp_dev->dev;
David Dillow8f26c9f2011-01-14 19:45:50 -0500577 struct srp_request *req;
578 int i;
579
Bart Van Assche4d73f952013-10-26 14:40:37 +0200580 if (!target->req_ring)
581 return;
582
583 for (i = 0; i < target->req_ring_size; ++i) {
584 req = &target->req_ring[i];
David Dillow8f26c9f2011-01-14 19:45:50 -0500585 kfree(req->fmr_list);
586 kfree(req->map_page);
David Dillowc07d4242011-01-16 13:57:10 -0500587 if (req->indirect_dma_addr) {
588 ib_dma_unmap_single(ibdev, req->indirect_dma_addr,
589 target->indirect_size,
590 DMA_TO_DEVICE);
591 }
592 kfree(req->indirect_desc);
David Dillow8f26c9f2011-01-14 19:45:50 -0500593 }
Bart Van Assche4d73f952013-10-26 14:40:37 +0200594
595 kfree(target->req_ring);
596 target->req_ring = NULL;
David Dillow8f26c9f2011-01-14 19:45:50 -0500597}
598
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200599static int srp_alloc_req_data(struct srp_target_port *target)
600{
601 struct srp_device *srp_dev = target->srp_host->srp_dev;
602 struct ib_device *ibdev = srp_dev->dev;
603 struct srp_request *req;
604 dma_addr_t dma_addr;
605 int i, ret = -ENOMEM;
606
607 INIT_LIST_HEAD(&target->free_reqs);
608
Bart Van Assche4d73f952013-10-26 14:40:37 +0200609 target->req_ring = kzalloc(target->req_ring_size *
610 sizeof(*target->req_ring), GFP_KERNEL);
611 if (!target->req_ring)
612 goto out;
613
614 for (i = 0; i < target->req_ring_size; ++i) {
Bart Van Asscheb81d00b2013-10-26 14:38:47 +0200615 req = &target->req_ring[i];
616 req->fmr_list = kmalloc(target->cmd_sg_cnt * sizeof(void *),
617 GFP_KERNEL);
618 req->map_page = kmalloc(SRP_FMR_SIZE * sizeof(void *),
619 GFP_KERNEL);
620 req->indirect_desc = kmalloc(target->indirect_size, GFP_KERNEL);
621 if (!req->fmr_list || !req->map_page || !req->indirect_desc)
622 goto out;
623
624 dma_addr = ib_dma_map_single(ibdev, req->indirect_desc,
625 target->indirect_size,
626 DMA_TO_DEVICE);
627 if (ib_dma_mapping_error(ibdev, dma_addr))
628 goto out;
629
630 req->indirect_dma_addr = dma_addr;
631 req->index = i;
632 list_add_tail(&req->list, &target->free_reqs);
633 }
634 ret = 0;
635
636out:
637 return ret;
638}
639
Bart Van Assche683b1592012-01-14 12:40:44 +0000640/**
641 * srp_del_scsi_host_attr() - Remove attributes defined in the host template.
642 * @shost: SCSI host whose attributes to remove from sysfs.
643 *
644 * Note: Any attributes defined in the host template and that did not exist
645 * before invocation of this function will be ignored.
646 */
647static void srp_del_scsi_host_attr(struct Scsi_Host *shost)
648{
649 struct device_attribute **attr;
650
651 for (attr = shost->hostt->shost_attrs; attr && *attr; ++attr)
652 device_remove_file(&shost->shost_dev, *attr);
653}
654
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000655static void srp_remove_target(struct srp_target_port *target)
656{
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000657 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
658
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000659 srp_del_scsi_host_attr(target->scsi_host);
Bart Van Assche9dd69a62013-10-26 14:32:30 +0200660 srp_rport_get(target->rport);
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000661 srp_remove_host(target->scsi_host);
662 scsi_remove_host(target->scsi_host);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000663 srp_disconnect_target(target);
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000664 ib_destroy_cm_id(target->cm_id);
665 srp_free_target_ib(target);
Bart Van Asschec1120f82013-10-26 14:35:08 +0200666 cancel_work_sync(&target->tl_err_work);
Bart Van Assche9dd69a62013-10-26 14:32:30 +0200667 srp_rport_put(target->rport);
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000668 srp_free_req_data(target);
Vu Pham65d7dd22013-10-10 13:50:29 +0200669
670 spin_lock(&target->srp_host->target_lock);
671 list_del(&target->list);
672 spin_unlock(&target->srp_host->target_lock);
673
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000674 scsi_host_put(target->scsi_host);
675}
676
David Howellsc4028952006-11-22 14:57:56 +0000677static void srp_remove_work(struct work_struct *work)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800678{
David Howellsc4028952006-11-22 14:57:56 +0000679 struct srp_target_port *target =
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000680 container_of(work, struct srp_target_port, remove_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800681
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000682 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800683
Bart Van Assche96fc2482013-06-28 14:51:26 +0200684 srp_remove_target(target);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800685}
686
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +0200687static void srp_rport_delete(struct srp_rport *rport)
688{
689 struct srp_target_port *target = rport->lld_data;
690
691 srp_queue_remove_work(target);
692}
693
Roland Dreieraef9ec32005-11-02 14:07:13 -0800694static int srp_connect_target(struct srp_target_port *target)
695{
David Dillow9fe4bcf2008-01-08 17:08:52 -0500696 int retries = 3;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800697 int ret;
698
Bart Van Assche294c8752011-12-25 12:18:12 +0000699 WARN_ON_ONCE(target->connected);
700
Bart Van Assche948d1e82011-09-03 09:25:42 +0200701 target->qp_in_error = false;
702
Roland Dreieraef9ec32005-11-02 14:07:13 -0800703 ret = srp_lookup_path(target);
704 if (ret)
705 return ret;
706
707 while (1) {
708 init_completion(&target->done);
709 ret = srp_send_req(target);
710 if (ret)
711 return ret;
712 wait_for_completion(&target->done);
713
714 /*
715 * The CM event handling code will set status to
716 * SRP_PORT_REDIRECT if we get a port redirect REJ
717 * back, or SRP_DLID_REDIRECT if we get a lid/qp
718 * redirect REJ back.
719 */
720 switch (target->status) {
721 case 0:
Bart Van Assche294c8752011-12-25 12:18:12 +0000722 srp_change_conn_state(target, true);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800723 return 0;
724
725 case SRP_PORT_REDIRECT:
726 ret = srp_lookup_path(target);
727 if (ret)
728 return ret;
729 break;
730
731 case SRP_DLID_REDIRECT:
732 break;
733
David Dillow9fe4bcf2008-01-08 17:08:52 -0500734 case SRP_STALE_CONN:
735 /* Our current CM id was stale, and is now in timewait.
736 * Try to reconnect with a new one.
737 */
738 if (!retries-- || srp_new_cm_id(target)) {
739 shost_printk(KERN_ERR, target->scsi_host, PFX
740 "giving up on stale connection\n");
741 target->status = -ECONNRESET;
742 return target->status;
743 }
744
745 shost_printk(KERN_ERR, target->scsi_host, PFX
746 "retrying stale connection\n");
747 break;
748
Roland Dreieraef9ec32005-11-02 14:07:13 -0800749 default:
750 return target->status;
751 }
752 }
753}
754
Roland Dreierd945e1d2006-05-09 10:50:28 -0700755static void srp_unmap_data(struct scsi_cmnd *scmnd,
756 struct srp_target_port *target,
757 struct srp_request *req)
758{
David Dillow8f26c9f2011-01-14 19:45:50 -0500759 struct ib_device *ibdev = target->srp_host->srp_dev->dev;
760 struct ib_pool_fmr **pfmr;
761
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900762 if (!scsi_sglist(scmnd) ||
Roland Dreierd945e1d2006-05-09 10:50:28 -0700763 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
764 scmnd->sc_data_direction != DMA_FROM_DEVICE))
765 return;
766
David Dillow8f26c9f2011-01-14 19:45:50 -0500767 pfmr = req->fmr_list;
768 while (req->nfmr--)
769 ib_fmr_pool_unmap(*pfmr++);
Roland Dreierf5358a12006-06-17 20:37:29 -0700770
David Dillow8f26c9f2011-01-14 19:45:50 -0500771 ib_dma_unmap_sg(ibdev, scsi_sglist(scmnd), scsi_sg_count(scmnd),
772 scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700773}
774
Bart Van Assche22032992012-08-14 13:18:53 +0000775/**
776 * srp_claim_req - Take ownership of the scmnd associated with a request.
777 * @target: SRP target port.
778 * @req: SRP request.
779 * @scmnd: If NULL, take ownership of @req->scmnd. If not NULL, only take
780 * ownership of @req->scmnd if it equals @scmnd.
781 *
782 * Return value:
783 * Either NULL or a pointer to the SCSI command the caller became owner of.
784 */
785static struct scsi_cmnd *srp_claim_req(struct srp_target_port *target,
786 struct srp_request *req,
787 struct scsi_cmnd *scmnd)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700788{
Bart Van Assche94a91742010-11-26 14:50:09 -0500789 unsigned long flags;
790
Bart Van Assche22032992012-08-14 13:18:53 +0000791 spin_lock_irqsave(&target->lock, flags);
792 if (!scmnd) {
793 scmnd = req->scmnd;
794 req->scmnd = NULL;
795 } else if (req->scmnd == scmnd) {
796 req->scmnd = NULL;
797 } else {
798 scmnd = NULL;
799 }
800 spin_unlock_irqrestore(&target->lock, flags);
801
802 return scmnd;
803}
804
805/**
806 * srp_free_req() - Unmap data and add request to the free request list.
807 */
808static void srp_free_req(struct srp_target_port *target,
809 struct srp_request *req, struct scsi_cmnd *scmnd,
810 s32 req_lim_delta)
811{
812 unsigned long flags;
813
814 srp_unmap_data(scmnd, target, req);
815
Bart Van Asschee9684672010-11-26 15:08:38 -0500816 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -0500817 target->req_lim += req_lim_delta;
Bart Van Assche536ae142010-11-26 13:58:27 -0500818 list_add_tail(&req->list, &target->free_reqs);
Bart Van Asschee9684672010-11-26 15:08:38 -0500819 spin_unlock_irqrestore(&target->lock, flags);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700820}
821
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200822static void srp_finish_req(struct srp_target_port *target,
823 struct srp_request *req, int result)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700824{
Bart Van Assche22032992012-08-14 13:18:53 +0000825 struct scsi_cmnd *scmnd = srp_claim_req(target, req, NULL);
826
827 if (scmnd) {
Bart Van Assche9b796d02012-08-24 10:27:54 +0000828 srp_free_req(target, req, scmnd, 0);
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200829 scmnd->result = result;
Bart Van Assche22032992012-08-14 13:18:53 +0000830 scmnd->scsi_done(scmnd);
Bart Van Assche22032992012-08-14 13:18:53 +0000831 }
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700832}
833
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200834static void srp_terminate_io(struct srp_rport *rport)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800835{
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200836 struct srp_target_port *target = rport->lld_data;
837 int i;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800838
Bart Van Assche4d73f952013-10-26 14:40:37 +0200839 for (i = 0; i < target->req_ring_size; ++i) {
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200840 struct srp_request *req = &target->req_ring[i];
841 srp_finish_req(target, req, DID_TRANSPORT_FAILFAST << 16);
842 }
843}
844
845/*
846 * It is up to the caller to ensure that srp_rport_reconnect() calls are
847 * serialized and that no concurrent srp_queuecommand(), srp_abort(),
848 * srp_reset_device() or srp_reset_host() calls will occur while this function
849 * is in progress. One way to realize that is not to call this function
850 * directly but to call srp_reconnect_rport() instead since that last function
851 * serializes calls of this function via rport->mutex and also blocks
852 * srp_queuecommand() calls before invoking this function.
853 */
854static int srp_rport_reconnect(struct srp_rport *rport)
855{
856 struct srp_target_port *target = rport->lld_data;
857 int i, ret;
Bart Van Assche09be70a2012-03-17 17:18:54 +0000858
Roland Dreieraef9ec32005-11-02 14:07:13 -0800859 srp_disconnect_target(target);
860 /*
Bart Van Asschec7c4e7f2013-02-21 17:19:04 +0000861 * Now get a new local CM ID so that we avoid confusing the target in
862 * case things are really fouled up. Doing so also ensures that all CM
863 * callbacks will have finished before a new QP is allocated.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800864 */
David Dillow9fe4bcf2008-01-08 17:08:52 -0500865 ret = srp_new_cm_id(target);
Bart Van Asschec7c4e7f2013-02-21 17:19:04 +0000866 /*
867 * Whether or not creating a new CM ID succeeded, create a new
868 * QP. This guarantees that all completion callback function
869 * invocations have finished before request resetting starts.
870 */
871 if (ret == 0)
872 ret = srp_create_target_ib(target);
873 else
874 srp_create_target_ib(target);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800875
Bart Van Assche4d73f952013-10-26 14:40:37 +0200876 for (i = 0; i < target->req_ring_size; ++i) {
Bart Van Assche536ae142010-11-26 13:58:27 -0500877 struct srp_request *req = &target->req_ring[i];
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200878 srp_finish_req(target, req, DID_RESET << 16);
Bart Van Assche536ae142010-11-26 13:58:27 -0500879 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800880
Bart Van Assche536ae142010-11-26 13:58:27 -0500881 INIT_LIST_HEAD(&target->free_tx);
Bart Van Assche4d73f952013-10-26 14:40:37 +0200882 for (i = 0; i < target->queue_size; ++i)
Bart Van Assche536ae142010-11-26 13:58:27 -0500883 list_add(&target->tx_ring[i]->list, &target->free_tx);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800884
Bart Van Asschec7c4e7f2013-02-21 17:19:04 +0000885 if (ret == 0)
886 ret = srp_connect_target(target);
Bart Van Assche09be70a2012-03-17 17:18:54 +0000887
Bart Van Asscheed9b2262013-10-26 14:34:27 +0200888 if (ret == 0)
889 shost_printk(KERN_INFO, target->scsi_host,
890 PFX "reconnect succeeded\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800891
892 return ret;
893}
894
David Dillow8f26c9f2011-01-14 19:45:50 -0500895static void srp_map_desc(struct srp_map_state *state, dma_addr_t dma_addr,
896 unsigned int dma_len, u32 rkey)
Roland Dreierf5358a12006-06-17 20:37:29 -0700897{
David Dillow8f26c9f2011-01-14 19:45:50 -0500898 struct srp_direct_buf *desc = state->desc;
899
900 desc->va = cpu_to_be64(dma_addr);
901 desc->key = cpu_to_be32(rkey);
902 desc->len = cpu_to_be32(dma_len);
903
904 state->total_len += dma_len;
905 state->desc++;
906 state->ndesc++;
907}
908
909static int srp_map_finish_fmr(struct srp_map_state *state,
910 struct srp_target_port *target)
911{
912 struct srp_device *dev = target->srp_host->srp_dev;
913 struct ib_pool_fmr *fmr;
Roland Dreierf5358a12006-06-17 20:37:29 -0700914 u64 io_addr = 0;
David Dillow8f26c9f2011-01-14 19:45:50 -0500915
916 if (!state->npages)
917 return 0;
918
919 if (state->npages == 1) {
920 srp_map_desc(state, state->base_dma_addr, state->fmr_len,
921 target->rkey);
922 state->npages = state->fmr_len = 0;
923 return 0;
924 }
925
926 fmr = ib_fmr_pool_map_phys(dev->fmr_pool, state->pages,
927 state->npages, io_addr);
928 if (IS_ERR(fmr))
929 return PTR_ERR(fmr);
930
931 *state->next_fmr++ = fmr;
932 state->nfmr++;
933
934 srp_map_desc(state, 0, state->fmr_len, fmr->fmr->rkey);
935 state->npages = state->fmr_len = 0;
936 return 0;
937}
938
939static void srp_map_update_start(struct srp_map_state *state,
940 struct scatterlist *sg, int sg_index,
941 dma_addr_t dma_addr)
942{
943 state->unmapped_sg = sg;
944 state->unmapped_index = sg_index;
945 state->unmapped_addr = dma_addr;
946}
947
948static int srp_map_sg_entry(struct srp_map_state *state,
949 struct srp_target_port *target,
950 struct scatterlist *sg, int sg_index,
951 int use_fmr)
952{
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100953 struct srp_device *dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800954 struct ib_device *ibdev = dev->dev;
David Dillow8f26c9f2011-01-14 19:45:50 -0500955 dma_addr_t dma_addr = ib_sg_dma_address(ibdev, sg);
956 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
957 unsigned int len;
958 int ret;
Roland Dreierf5358a12006-06-17 20:37:29 -0700959
David Dillow8f26c9f2011-01-14 19:45:50 -0500960 if (!dma_len)
961 return 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700962
David Dillow8f26c9f2011-01-14 19:45:50 -0500963 if (use_fmr == SRP_MAP_NO_FMR) {
964 /* Once we're in direct map mode for a request, we don't
965 * go back to FMR mode, so no need to update anything
966 * other than the descriptor.
967 */
968 srp_map_desc(state, dma_addr, dma_len, target->rkey);
969 return 0;
970 }
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700971
David Dillow8f26c9f2011-01-14 19:45:50 -0500972 /* If we start at an offset into the FMR page, don't merge into
973 * the current FMR. Finish it out, and use the kernel's MR for this
974 * sg entry. This is to avoid potential bugs on some SRP targets
975 * that were never quite defined, but went away when the initiator
976 * avoided using FMR on such page fragments.
977 */
978 if (dma_addr & ~dev->fmr_page_mask || dma_len > dev->fmr_max_size) {
979 ret = srp_map_finish_fmr(state, target);
980 if (ret)
981 return ret;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800982
David Dillow8f26c9f2011-01-14 19:45:50 -0500983 srp_map_desc(state, dma_addr, dma_len, target->rkey);
984 srp_map_update_start(state, NULL, 0, 0);
985 return 0;
986 }
987
988 /* If this is the first sg to go into the FMR, save our position.
989 * We need to know the first unmapped entry, its index, and the
990 * first unmapped address within that entry to be able to restart
991 * mapping after an error.
992 */
993 if (!state->unmapped_sg)
994 srp_map_update_start(state, sg, sg_index, dma_addr);
995
996 while (dma_len) {
997 if (state->npages == SRP_FMR_SIZE) {
998 ret = srp_map_finish_fmr(state, target);
999 if (ret)
1000 return ret;
1001
1002 srp_map_update_start(state, sg, sg_index, dma_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -07001003 }
1004
David Dillow8f26c9f2011-01-14 19:45:50 -05001005 len = min_t(unsigned int, dma_len, dev->fmr_page_size);
1006
1007 if (!state->npages)
1008 state->base_dma_addr = dma_addr;
1009 state->pages[state->npages++] = dma_addr;
1010 state->fmr_len += len;
1011 dma_addr += len;
1012 dma_len -= len;
Roland Dreierf5358a12006-06-17 20:37:29 -07001013 }
1014
David Dillow8f26c9f2011-01-14 19:45:50 -05001015 /* If the last entry of the FMR wasn't a full page, then we need to
1016 * close it out and start a new one -- we can only merge at page
1017 * boundries.
1018 */
Roland Dreierf5358a12006-06-17 20:37:29 -07001019 ret = 0;
David Dillow8f26c9f2011-01-14 19:45:50 -05001020 if (len != dev->fmr_page_size) {
1021 ret = srp_map_finish_fmr(state, target);
1022 if (!ret)
1023 srp_map_update_start(state, NULL, 0, 0);
1024 }
Roland Dreierf5358a12006-06-17 20:37:29 -07001025 return ret;
1026}
1027
Roland Dreieraef9ec32005-11-02 14:07:13 -08001028static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
1029 struct srp_request *req)
1030{
David Dillow8f26c9f2011-01-14 19:45:50 -05001031 struct scatterlist *scat, *sg;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001032 struct srp_cmd *cmd = req->cmd->buf;
David Dillow8f26c9f2011-01-14 19:45:50 -05001033 int i, len, nents, count, use_fmr;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001034 struct srp_device *dev;
1035 struct ib_device *ibdev;
David Dillow8f26c9f2011-01-14 19:45:50 -05001036 struct srp_map_state state;
1037 struct srp_indirect_buf *indirect_hdr;
David Dillow8f26c9f2011-01-14 19:45:50 -05001038 u32 table_len;
1039 u8 fmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001040
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001041 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001042 return sizeof (struct srp_cmd);
1043
1044 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
1045 scmnd->sc_data_direction != DMA_TO_DEVICE) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001046 shost_printk(KERN_WARNING, target->scsi_host,
1047 PFX "Unhandled data direction %d\n",
1048 scmnd->sc_data_direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001049 return -EINVAL;
1050 }
1051
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001052 nents = scsi_sg_count(scmnd);
1053 scat = scsi_sglist(scmnd);
Roland Dreiercf368712006-03-24 15:47:26 -08001054
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001055 dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001056 ibdev = dev->dev;
1057
1058 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
David Dillow8f26c9f2011-01-14 19:45:50 -05001059 if (unlikely(count == 0))
1060 return -EIO;
Roland Dreierf5358a12006-06-17 20:37:29 -07001061
1062 fmt = SRP_DATA_DESC_DIRECT;
1063 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -08001064
1065 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -07001066 /*
1067 * The midlayer only generated a single gather/scatter
1068 * entry, or DMA mapping coalesced everything to a
1069 * single entry. So a direct descriptor along with
1070 * the DMA MR suffices.
1071 */
Roland Dreieraef9ec32005-11-02 14:07:13 -08001072 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001073
Ralph Campbell85507bc2006-12-12 14:30:55 -08001074 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
David Dillow9af76272010-11-26 15:34:46 -05001075 buf->key = cpu_to_be32(target->rkey);
Ralph Campbell85507bc2006-12-12 14:30:55 -08001076 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Roland Dreiercf368712006-03-24 15:47:26 -08001077
David Dillow8f26c9f2011-01-14 19:45:50 -05001078 req->nfmr = 0;
1079 goto map_complete;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001080 }
1081
David Dillow8f26c9f2011-01-14 19:45:50 -05001082 /* We have more than one scatter/gather entry, so build our indirect
1083 * descriptor table, trying to merge as many entries with FMR as we
1084 * can.
1085 */
1086 indirect_hdr = (void *) cmd->add_data;
1087
David Dillowc07d4242011-01-16 13:57:10 -05001088 ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr,
1089 target->indirect_size, DMA_TO_DEVICE);
1090
David Dillow8f26c9f2011-01-14 19:45:50 -05001091 memset(&state, 0, sizeof(state));
David Dillowc07d4242011-01-16 13:57:10 -05001092 state.desc = req->indirect_desc;
David Dillow8f26c9f2011-01-14 19:45:50 -05001093 state.pages = req->map_page;
1094 state.next_fmr = req->fmr_list;
1095
1096 use_fmr = dev->fmr_pool ? SRP_MAP_ALLOW_FMR : SRP_MAP_NO_FMR;
1097
1098 for_each_sg(scat, sg, count, i) {
1099 if (srp_map_sg_entry(&state, target, sg, i, use_fmr)) {
1100 /* FMR mapping failed, so backtrack to the first
1101 * unmapped entry and continue on without using FMR.
1102 */
1103 dma_addr_t dma_addr;
1104 unsigned int dma_len;
1105
1106backtrack:
1107 sg = state.unmapped_sg;
1108 i = state.unmapped_index;
1109
1110 dma_addr = ib_sg_dma_address(ibdev, sg);
1111 dma_len = ib_sg_dma_len(ibdev, sg);
1112 dma_len -= (state.unmapped_addr - dma_addr);
1113 dma_addr = state.unmapped_addr;
1114 use_fmr = SRP_MAP_NO_FMR;
1115 srp_map_desc(&state, dma_addr, dma_len, target->rkey);
1116 }
1117 }
1118
1119 if (use_fmr == SRP_MAP_ALLOW_FMR && srp_map_finish_fmr(&state, target))
1120 goto backtrack;
1121
David Dillowc07d4242011-01-16 13:57:10 -05001122 /* We've mapped the request, now pull as much of the indirect
1123 * descriptor table as we can into the command buffer. If this
1124 * target is not using an external indirect table, we are
1125 * guaranteed to fit into the command, as the SCSI layer won't
1126 * give us more S/G entries than we allow.
David Dillow8f26c9f2011-01-14 19:45:50 -05001127 */
1128 req->nfmr = state.nfmr;
1129 if (state.ndesc == 1) {
1130 /* FMR mapping was able to collapse this to one entry,
1131 * so use a direct descriptor.
1132 */
1133 struct srp_direct_buf *buf = (void *) cmd->add_data;
1134
David Dillowc07d4242011-01-16 13:57:10 -05001135 *buf = req->indirect_desc[0];
David Dillow8f26c9f2011-01-14 19:45:50 -05001136 goto map_complete;
1137 }
1138
David Dillowc07d4242011-01-16 13:57:10 -05001139 if (unlikely(target->cmd_sg_cnt < state.ndesc &&
1140 !target->allow_ext_sg)) {
1141 shost_printk(KERN_ERR, target->scsi_host,
1142 "Could not fit S/G list into SRP_CMD\n");
1143 return -EIO;
1144 }
1145
1146 count = min(state.ndesc, target->cmd_sg_cnt);
David Dillow8f26c9f2011-01-14 19:45:50 -05001147 table_len = state.ndesc * sizeof (struct srp_direct_buf);
1148
1149 fmt = SRP_DATA_DESC_INDIRECT;
1150 len = sizeof(struct srp_cmd) + sizeof (struct srp_indirect_buf);
David Dillowc07d4242011-01-16 13:57:10 -05001151 len += count * sizeof (struct srp_direct_buf);
David Dillow8f26c9f2011-01-14 19:45:50 -05001152
David Dillowc07d4242011-01-16 13:57:10 -05001153 memcpy(indirect_hdr->desc_list, req->indirect_desc,
1154 count * sizeof (struct srp_direct_buf));
David Dillow8f26c9f2011-01-14 19:45:50 -05001155
David Dillowc07d4242011-01-16 13:57:10 -05001156 indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr);
David Dillow8f26c9f2011-01-14 19:45:50 -05001157 indirect_hdr->table_desc.key = cpu_to_be32(target->rkey);
1158 indirect_hdr->table_desc.len = cpu_to_be32(table_len);
1159 indirect_hdr->len = cpu_to_be32(state.total_len);
1160
1161 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
David Dillowc07d4242011-01-16 13:57:10 -05001162 cmd->data_out_desc_cnt = count;
David Dillow8f26c9f2011-01-14 19:45:50 -05001163 else
David Dillowc07d4242011-01-16 13:57:10 -05001164 cmd->data_in_desc_cnt = count;
1165
1166 ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len,
1167 DMA_TO_DEVICE);
David Dillow8f26c9f2011-01-14 19:45:50 -05001168
1169map_complete:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001170 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
1171 cmd->buf_fmt = fmt << 4;
1172 else
1173 cmd->buf_fmt = fmt;
1174
Roland Dreieraef9ec32005-11-02 14:07:13 -08001175 return len;
1176}
1177
David Dillow05a1d752010-10-08 14:48:14 -04001178/*
Bart Van Assche76c75b22010-11-26 14:37:47 -05001179 * Return an IU and possible credit to the free pool
1180 */
1181static void srp_put_tx_iu(struct srp_target_port *target, struct srp_iu *iu,
1182 enum srp_iu_type iu_type)
1183{
1184 unsigned long flags;
1185
Bart Van Asschee9684672010-11-26 15:08:38 -05001186 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001187 list_add(&iu->list, &target->free_tx);
1188 if (iu_type != SRP_IU_RSP)
1189 ++target->req_lim;
Bart Van Asschee9684672010-11-26 15:08:38 -05001190 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001191}
1192
1193/*
Bart Van Asschee9684672010-11-26 15:08:38 -05001194 * Must be called with target->lock held to protect req_lim and free_tx.
1195 * If IU is not sent, it must be returned using srp_put_tx_iu().
David Dillow05a1d752010-10-08 14:48:14 -04001196 *
1197 * Note:
1198 * An upper limit for the number of allocated information units for each
1199 * request type is:
1200 * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues
1201 * more than Scsi_Host.can_queue requests.
1202 * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE.
1203 * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than
1204 * one unanswered SRP request to an initiator.
1205 */
1206static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
1207 enum srp_iu_type iu_type)
1208{
1209 s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE;
1210 struct srp_iu *iu;
1211
1212 srp_send_completion(target->send_cq, target);
1213
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001214 if (list_empty(&target->free_tx))
David Dillow05a1d752010-10-08 14:48:14 -04001215 return NULL;
1216
1217 /* Initiator responses to target requests do not consume credits */
Bart Van Assche76c75b22010-11-26 14:37:47 -05001218 if (iu_type != SRP_IU_RSP) {
1219 if (target->req_lim <= rsv) {
1220 ++target->zero_req_lim;
1221 return NULL;
1222 }
1223
1224 --target->req_lim;
David Dillow05a1d752010-10-08 14:48:14 -04001225 }
1226
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001227 iu = list_first_entry(&target->free_tx, struct srp_iu, list);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001228 list_del(&iu->list);
David Dillow05a1d752010-10-08 14:48:14 -04001229 return iu;
1230}
1231
Bart Van Assche76c75b22010-11-26 14:37:47 -05001232static int srp_post_send(struct srp_target_port *target,
1233 struct srp_iu *iu, int len)
David Dillow05a1d752010-10-08 14:48:14 -04001234{
1235 struct ib_sge list;
1236 struct ib_send_wr wr, *bad_wr;
David Dillow05a1d752010-10-08 14:48:14 -04001237
1238 list.addr = iu->dma;
1239 list.length = len;
David Dillow9af76272010-11-26 15:34:46 -05001240 list.lkey = target->lkey;
David Dillow05a1d752010-10-08 14:48:14 -04001241
1242 wr.next = NULL;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001243 wr.wr_id = (uintptr_t) iu;
David Dillow05a1d752010-10-08 14:48:14 -04001244 wr.sg_list = &list;
1245 wr.num_sge = 1;
1246 wr.opcode = IB_WR_SEND;
1247 wr.send_flags = IB_SEND_SIGNALED;
1248
Bart Van Assche76c75b22010-11-26 14:37:47 -05001249 return ib_post_send(target->qp, &wr, &bad_wr);
David Dillow05a1d752010-10-08 14:48:14 -04001250}
1251
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001252static int srp_post_recv(struct srp_target_port *target, struct srp_iu *iu)
Bart Van Asschec996bb42010-07-30 10:59:05 +00001253{
Bart Van Asschec996bb42010-07-30 10:59:05 +00001254 struct ib_recv_wr wr, *bad_wr;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001255 struct ib_sge list;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001256
1257 list.addr = iu->dma;
1258 list.length = iu->size;
David Dillow9af76272010-11-26 15:34:46 -05001259 list.lkey = target->lkey;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001260
1261 wr.next = NULL;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001262 wr.wr_id = (uintptr_t) iu;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001263 wr.sg_list = &list;
1264 wr.num_sge = 1;
1265
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001266 return ib_post_recv(target->qp, &wr, &bad_wr);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001267}
1268
Roland Dreieraef9ec32005-11-02 14:07:13 -08001269static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
1270{
1271 struct srp_request *req;
1272 struct scsi_cmnd *scmnd;
1273 unsigned long flags;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001274
Roland Dreieraef9ec32005-11-02 14:07:13 -08001275 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
Bart Van Asschee9684672010-11-26 15:08:38 -05001276 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -05001277 target->req_lim += be32_to_cpu(rsp->req_lim_delta);
Bart Van Asschee9684672010-11-26 15:08:38 -05001278 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -05001279
David Dillowf8b6e312010-11-26 13:02:21 -05001280 target->tsk_mgmt_status = -1;
1281 if (be32_to_cpu(rsp->resp_data_len) >= 4)
1282 target->tsk_mgmt_status = rsp->data[3];
1283 complete(&target->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001284 } else {
David Dillowf8b6e312010-11-26 13:02:21 -05001285 req = &target->req_ring[rsp->tag];
Bart Van Assche22032992012-08-14 13:18:53 +00001286 scmnd = srp_claim_req(target, req, NULL);
1287 if (!scmnd) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001288 shost_printk(KERN_ERR, target->scsi_host,
1289 "Null scmnd for RSP w/tag %016llx\n",
1290 (unsigned long long) rsp->tag);
Bart Van Assche22032992012-08-14 13:18:53 +00001291
1292 spin_lock_irqsave(&target->lock, flags);
1293 target->req_lim += be32_to_cpu(rsp->req_lim_delta);
1294 spin_unlock_irqrestore(&target->lock, flags);
1295
1296 return;
1297 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001298 scmnd->result = rsp->status;
1299
1300 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
1301 memcpy(scmnd->sense_buffer, rsp->data +
1302 be32_to_cpu(rsp->resp_data_len),
1303 min_t(int, be32_to_cpu(rsp->sense_data_len),
1304 SCSI_SENSE_BUFFERSIZE));
1305 }
1306
1307 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001308 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001309 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001310 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001311
Bart Van Assche22032992012-08-14 13:18:53 +00001312 srp_free_req(target, req, scmnd,
1313 be32_to_cpu(rsp->req_lim_delta));
1314
David Dillowf8b6e312010-11-26 13:02:21 -05001315 scmnd->host_scribble = NULL;
1316 scmnd->scsi_done(scmnd);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001317 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001318}
1319
David Dillowbb125882010-10-08 14:40:47 -04001320static int srp_response_common(struct srp_target_port *target, s32 req_delta,
1321 void *rsp, int len)
1322{
Bart Van Assche76c75b22010-11-26 14:37:47 -05001323 struct ib_device *dev = target->srp_host->srp_dev->dev;
David Dillowbb125882010-10-08 14:40:47 -04001324 unsigned long flags;
1325 struct srp_iu *iu;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001326 int err;
David Dillowbb125882010-10-08 14:40:47 -04001327
Bart Van Asschee9684672010-11-26 15:08:38 -05001328 spin_lock_irqsave(&target->lock, flags);
David Dillowbb125882010-10-08 14:40:47 -04001329 target->req_lim += req_delta;
David Dillowbb125882010-10-08 14:40:47 -04001330 iu = __srp_get_tx_iu(target, SRP_IU_RSP);
Bart Van Asschee9684672010-11-26 15:08:38 -05001331 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001332
David Dillowbb125882010-10-08 14:40:47 -04001333 if (!iu) {
1334 shost_printk(KERN_ERR, target->scsi_host, PFX
1335 "no IU available to send response\n");
Bart Van Assche76c75b22010-11-26 14:37:47 -05001336 return 1;
David Dillowbb125882010-10-08 14:40:47 -04001337 }
1338
1339 ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE);
1340 memcpy(iu->buf, rsp, len);
1341 ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE);
1342
Bart Van Assche76c75b22010-11-26 14:37:47 -05001343 err = srp_post_send(target, iu, len);
1344 if (err) {
David Dillowbb125882010-10-08 14:40:47 -04001345 shost_printk(KERN_ERR, target->scsi_host, PFX
1346 "unable to post response: %d\n", err);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001347 srp_put_tx_iu(target, iu, SRP_IU_RSP);
1348 }
David Dillowbb125882010-10-08 14:40:47 -04001349
David Dillowbb125882010-10-08 14:40:47 -04001350 return err;
1351}
1352
1353static void srp_process_cred_req(struct srp_target_port *target,
1354 struct srp_cred_req *req)
1355{
1356 struct srp_cred_rsp rsp = {
1357 .opcode = SRP_CRED_RSP,
1358 .tag = req->tag,
1359 };
1360 s32 delta = be32_to_cpu(req->req_lim_delta);
1361
1362 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1363 shost_printk(KERN_ERR, target->scsi_host, PFX
1364 "problems processing SRP_CRED_REQ\n");
1365}
1366
1367static void srp_process_aer_req(struct srp_target_port *target,
1368 struct srp_aer_req *req)
1369{
1370 struct srp_aer_rsp rsp = {
1371 .opcode = SRP_AER_RSP,
1372 .tag = req->tag,
1373 };
1374 s32 delta = be32_to_cpu(req->req_lim_delta);
1375
1376 shost_printk(KERN_ERR, target->scsi_host, PFX
1377 "ignoring AER for LUN %llu\n", be64_to_cpu(req->lun));
1378
1379 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1380 shost_printk(KERN_ERR, target->scsi_host, PFX
1381 "problems processing SRP_AER_REQ\n");
1382}
1383
Roland Dreieraef9ec32005-11-02 14:07:13 -08001384static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
1385{
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001386 struct ib_device *dev = target->srp_host->srp_dev->dev;
Roland Dreier737b94e2011-05-23 11:30:04 -07001387 struct srp_iu *iu = (struct srp_iu *) (uintptr_t) wc->wr_id;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001388 int res;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001389 u8 opcode;
1390
Ralph Campbell85507bc2006-12-12 14:30:55 -08001391 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
1392 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001393
1394 opcode = *(u8 *) iu->buf;
1395
1396 if (0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001397 shost_printk(KERN_ERR, target->scsi_host,
1398 PFX "recv completion, opcode 0x%02x\n", opcode);
Bart Van Assche7a700812010-07-29 15:56:37 +00001399 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1,
1400 iu->buf, wc->byte_len, true);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001401 }
1402
1403 switch (opcode) {
1404 case SRP_RSP:
1405 srp_process_rsp(target, iu->buf);
1406 break;
1407
David Dillowbb125882010-10-08 14:40:47 -04001408 case SRP_CRED_REQ:
1409 srp_process_cred_req(target, iu->buf);
1410 break;
1411
1412 case SRP_AER_REQ:
1413 srp_process_aer_req(target, iu->buf);
1414 break;
1415
Roland Dreieraef9ec32005-11-02 14:07:13 -08001416 case SRP_T_LOGOUT:
1417 /* XXX Handle target logout */
David Dillow7aa54bd2008-01-07 18:23:41 -05001418 shost_printk(KERN_WARNING, target->scsi_host,
1419 PFX "Got target logout request\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001420 break;
1421
1422 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001423 shost_printk(KERN_WARNING, target->scsi_host,
1424 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001425 break;
1426 }
1427
Ralph Campbell85507bc2006-12-12 14:30:55 -08001428 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
1429 DMA_FROM_DEVICE);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001430
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001431 res = srp_post_recv(target, iu);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001432 if (res != 0)
1433 shost_printk(KERN_ERR, target->scsi_host,
1434 PFX "Recv failed with error code %d\n", res);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001435}
1436
Bart Van Asschec1120f82013-10-26 14:35:08 +02001437/**
1438 * srp_tl_err_work() - handle a transport layer error
1439 *
1440 * Note: This function may get invoked before the rport has been created,
1441 * hence the target->rport test.
1442 */
1443static void srp_tl_err_work(struct work_struct *work)
1444{
1445 struct srp_target_port *target;
1446
1447 target = container_of(work, struct srp_target_port, tl_err_work);
1448 if (target->rport)
1449 srp_start_tl_fail_timers(target->rport);
1450}
1451
Bart Van Assche948d1e82011-09-03 09:25:42 +02001452static void srp_handle_qp_err(enum ib_wc_status wc_status,
1453 enum ib_wc_opcode wc_opcode,
1454 struct srp_target_port *target)
1455{
Bart Van Assche294c8752011-12-25 12:18:12 +00001456 if (target->connected && !target->qp_in_error) {
Bart Van Assche4f0af692012-11-26 11:16:40 +01001457 shost_printk(KERN_ERR, target->scsi_host,
1458 PFX "failed %s status %d\n",
1459 wc_opcode & IB_WC_RECV ? "receive" : "send",
1460 wc_status);
Bart Van Asschec1120f82013-10-26 14:35:08 +02001461 queue_work(system_long_wq, &target->tl_err_work);
Bart Van Assche4f0af692012-11-26 11:16:40 +01001462 }
Bart Van Assche948d1e82011-09-03 09:25:42 +02001463 target->qp_in_error = true;
1464}
1465
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001466static void srp_recv_completion(struct ib_cq *cq, void *target_ptr)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001467{
1468 struct srp_target_port *target = target_ptr;
1469 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001470
1471 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1472 while (ib_poll_cq(cq, 1, &wc) > 0) {
Bart Van Assche948d1e82011-09-03 09:25:42 +02001473 if (likely(wc.status == IB_WC_SUCCESS)) {
1474 srp_handle_recv(target, &wc);
1475 } else {
1476 srp_handle_qp_err(wc.status, wc.opcode, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001477 }
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001478 }
1479}
1480
1481static void srp_send_completion(struct ib_cq *cq, void *target_ptr)
1482{
1483 struct srp_target_port *target = target_ptr;
1484 struct ib_wc wc;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001485 struct srp_iu *iu;
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001486
1487 while (ib_poll_cq(cq, 1, &wc) > 0) {
Bart Van Assche948d1e82011-09-03 09:25:42 +02001488 if (likely(wc.status == IB_WC_SUCCESS)) {
1489 iu = (struct srp_iu *) (uintptr_t) wc.wr_id;
1490 list_add(&iu->list, &target->free_tx);
1491 } else {
1492 srp_handle_qp_err(wc.status, wc.opcode, target);
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001493 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001494 }
1495}
1496
Bart Van Assche76c75b22010-11-26 14:37:47 -05001497static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001498{
Bart Van Assche76c75b22010-11-26 14:37:47 -05001499 struct srp_target_port *target = host_to_target(shost);
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001500 struct srp_rport *rport = target->rport;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001501 struct srp_request *req;
1502 struct srp_iu *iu;
1503 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001504 struct ib_device *dev;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001505 unsigned long flags;
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001506 int len, result;
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001507 const bool in_scsi_eh = !in_interrupt() && current == shost->ehandler;
1508
1509 /*
1510 * The SCSI EH thread is the only context from which srp_queuecommand()
1511 * can get invoked for blocked devices (SDEV_BLOCK /
1512 * SDEV_CREATED_BLOCK). Avoid racing with srp_reconnect_rport() by
1513 * locking the rport mutex if invoked from inside the SCSI EH.
1514 */
1515 if (in_scsi_eh)
1516 mutex_lock(&rport->mutex);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001517
Bart Van Asscheed9b2262013-10-26 14:34:27 +02001518 result = srp_chkready(target->rport);
1519 if (unlikely(result)) {
1520 scmnd->result = result;
Bart Van Assche2ce19e72013-02-21 17:20:00 +00001521 scmnd->scsi_done(scmnd);
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001522 goto unlock_rport;
Bart Van Assche2ce19e72013-02-21 17:20:00 +00001523 }
1524
Bart Van Asschee9684672010-11-26 15:08:38 -05001525 spin_lock_irqsave(&target->lock, flags);
David Dillowbb125882010-10-08 14:40:47 -04001526 iu = __srp_get_tx_iu(target, SRP_IU_CMD);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001527 if (!iu)
Bart Van Assche695b8342011-01-13 19:02:25 +00001528 goto err_unlock;
1529
1530 req = list_first_entry(&target->free_reqs, struct srp_request, list);
1531 list_del(&req->list);
1532 spin_unlock_irqrestore(&target->lock, flags);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001533
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001534 dev = target->srp_host->srp_dev->dev;
David Dillow49248642011-01-14 18:23:24 -05001535 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08001536 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001537
Roland Dreieraef9ec32005-11-02 14:07:13 -08001538 scmnd->result = 0;
David Dillowf8b6e312010-11-26 13:02:21 -05001539 scmnd->host_scribble = (void *) req;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001540
1541 cmd = iu->buf;
1542 memset(cmd, 0, sizeof *cmd);
1543
1544 cmd->opcode = SRP_CMD;
1545 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001546 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001547 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1548
Roland Dreieraef9ec32005-11-02 14:07:13 -08001549 req->scmnd = scmnd;
1550 req->cmd = iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001551
1552 len = srp_map_data(scmnd, target, req);
1553 if (len < 0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001554 shost_printk(KERN_ERR, target->scsi_host,
1555 PFX "Failed to map data\n");
Bart Van Assche76c75b22010-11-26 14:37:47 -05001556 goto err_iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001557 }
1558
David Dillow49248642011-01-14 18:23:24 -05001559 ib_dma_sync_single_for_device(dev, iu->dma, target->max_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08001560 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001561
Bart Van Assche76c75b22010-11-26 14:37:47 -05001562 if (srp_post_send(target, iu, len)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001563 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001564 goto err_unmap;
1565 }
1566
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001567unlock_rport:
1568 if (in_scsi_eh)
1569 mutex_unlock(&rport->mutex);
1570
Roland Dreieraef9ec32005-11-02 14:07:13 -08001571 return 0;
1572
1573err_unmap:
1574 srp_unmap_data(scmnd, target, req);
1575
Bart Van Assche76c75b22010-11-26 14:37:47 -05001576err_iu:
1577 srp_put_tx_iu(target, iu, SRP_IU_CMD);
1578
Bart Van Asschee9684672010-11-26 15:08:38 -05001579 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001580 list_add(&req->list, &target->free_reqs);
Bart Van Assche695b8342011-01-13 19:02:25 +00001581
1582err_unlock:
Bart Van Asschee9684672010-11-26 15:08:38 -05001583 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001584
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001585 if (in_scsi_eh)
1586 mutex_unlock(&rport->mutex);
1587
Roland Dreieraef9ec32005-11-02 14:07:13 -08001588 return SCSI_MLQUEUE_HOST_BUSY;
1589}
1590
Bart Van Assche4d73f952013-10-26 14:40:37 +02001591/*
1592 * Note: the resources allocated in this function are freed in
1593 * srp_free_target_ib().
1594 */
Roland Dreieraef9ec32005-11-02 14:07:13 -08001595static int srp_alloc_iu_bufs(struct srp_target_port *target)
1596{
1597 int i;
1598
Bart Van Assche4d73f952013-10-26 14:40:37 +02001599 target->rx_ring = kzalloc(target->queue_size * sizeof(*target->rx_ring),
1600 GFP_KERNEL);
1601 if (!target->rx_ring)
1602 goto err_no_ring;
1603 target->tx_ring = kzalloc(target->queue_size * sizeof(*target->tx_ring),
1604 GFP_KERNEL);
1605 if (!target->tx_ring)
1606 goto err_no_ring;
1607
1608 for (i = 0; i < target->queue_size; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001609 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1610 target->max_ti_iu_len,
1611 GFP_KERNEL, DMA_FROM_DEVICE);
1612 if (!target->rx_ring[i])
1613 goto err;
1614 }
1615
Bart Van Assche4d73f952013-10-26 14:40:37 +02001616 for (i = 0; i < target->queue_size; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001617 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
David Dillow49248642011-01-14 18:23:24 -05001618 target->max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001619 GFP_KERNEL, DMA_TO_DEVICE);
1620 if (!target->tx_ring[i])
1621 goto err;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001622
1623 list_add(&target->tx_ring[i]->list, &target->free_tx);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001624 }
1625
1626 return 0;
1627
1628err:
Bart Van Assche4d73f952013-10-26 14:40:37 +02001629 for (i = 0; i < target->queue_size; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001630 srp_free_iu(target->srp_host, target->rx_ring[i]);
Bart Van Assche4d73f952013-10-26 14:40:37 +02001631 srp_free_iu(target->srp_host, target->tx_ring[i]);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001632 }
1633
Bart Van Assche4d73f952013-10-26 14:40:37 +02001634
1635err_no_ring:
1636 kfree(target->tx_ring);
1637 target->tx_ring = NULL;
1638 kfree(target->rx_ring);
1639 target->rx_ring = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001640
1641 return -ENOMEM;
1642}
1643
Bart Van Asschec9b03c12011-09-03 09:34:48 +02001644static uint32_t srp_compute_rq_tmo(struct ib_qp_attr *qp_attr, int attr_mask)
1645{
1646 uint64_t T_tr_ns, max_compl_time_ms;
1647 uint32_t rq_tmo_jiffies;
1648
1649 /*
1650 * According to section 11.2.4.2 in the IBTA spec (Modify Queue Pair,
1651 * table 91), both the QP timeout and the retry count have to be set
1652 * for RC QP's during the RTR to RTS transition.
1653 */
1654 WARN_ON_ONCE((attr_mask & (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)) !=
1655 (IB_QP_TIMEOUT | IB_QP_RETRY_CNT));
1656
1657 /*
1658 * Set target->rq_tmo_jiffies to one second more than the largest time
1659 * it can take before an error completion is generated. See also
1660 * C9-140..142 in the IBTA spec for more information about how to
1661 * convert the QP Local ACK Timeout value to nanoseconds.
1662 */
1663 T_tr_ns = 4096 * (1ULL << qp_attr->timeout);
1664 max_compl_time_ms = qp_attr->retry_cnt * 4 * T_tr_ns;
1665 do_div(max_compl_time_ms, NSEC_PER_MSEC);
1666 rq_tmo_jiffies = msecs_to_jiffies(max_compl_time_ms + 1000);
1667
1668 return rq_tmo_jiffies;
1669}
1670
David Dillow961e0be2011-01-14 17:32:07 -05001671static void srp_cm_rep_handler(struct ib_cm_id *cm_id,
1672 struct srp_login_rsp *lrsp,
1673 struct srp_target_port *target)
1674{
1675 struct ib_qp_attr *qp_attr = NULL;
1676 int attr_mask = 0;
1677 int ret;
1678 int i;
1679
1680 if (lrsp->opcode == SRP_LOGIN_RSP) {
1681 target->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len);
1682 target->req_lim = be32_to_cpu(lrsp->req_lim_delta);
1683
1684 /*
1685 * Reserve credits for task management so we don't
1686 * bounce requests back to the SCSI mid-layer.
1687 */
1688 target->scsi_host->can_queue
1689 = min(target->req_lim - SRP_TSK_MGMT_SQ_SIZE,
1690 target->scsi_host->can_queue);
Bart Van Assche4d73f952013-10-26 14:40:37 +02001691 target->scsi_host->cmd_per_lun
1692 = min_t(int, target->scsi_host->can_queue,
1693 target->scsi_host->cmd_per_lun);
David Dillow961e0be2011-01-14 17:32:07 -05001694 } else {
1695 shost_printk(KERN_WARNING, target->scsi_host,
1696 PFX "Unhandled RSP opcode %#x\n", lrsp->opcode);
1697 ret = -ECONNRESET;
1698 goto error;
1699 }
1700
Bart Van Assche4d73f952013-10-26 14:40:37 +02001701 if (!target->rx_ring) {
David Dillow961e0be2011-01-14 17:32:07 -05001702 ret = srp_alloc_iu_bufs(target);
1703 if (ret)
1704 goto error;
1705 }
1706
1707 ret = -ENOMEM;
1708 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1709 if (!qp_attr)
1710 goto error;
1711
1712 qp_attr->qp_state = IB_QPS_RTR;
1713 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1714 if (ret)
1715 goto error_free;
1716
1717 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
1718 if (ret)
1719 goto error_free;
1720
Bart Van Assche4d73f952013-10-26 14:40:37 +02001721 for (i = 0; i < target->queue_size; i++) {
David Dillow961e0be2011-01-14 17:32:07 -05001722 struct srp_iu *iu = target->rx_ring[i];
1723 ret = srp_post_recv(target, iu);
1724 if (ret)
1725 goto error_free;
1726 }
1727
1728 qp_attr->qp_state = IB_QPS_RTS;
1729 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1730 if (ret)
1731 goto error_free;
1732
Bart Van Asschec9b03c12011-09-03 09:34:48 +02001733 target->rq_tmo_jiffies = srp_compute_rq_tmo(qp_attr, attr_mask);
1734
David Dillow961e0be2011-01-14 17:32:07 -05001735 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
1736 if (ret)
1737 goto error_free;
1738
1739 ret = ib_send_cm_rtu(cm_id, NULL, 0);
1740
1741error_free:
1742 kfree(qp_attr);
1743
1744error:
1745 target->status = ret;
1746}
1747
Roland Dreieraef9ec32005-11-02 14:07:13 -08001748static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1749 struct ib_cm_event *event,
1750 struct srp_target_port *target)
1751{
David Dillow7aa54bd2008-01-07 18:23:41 -05001752 struct Scsi_Host *shost = target->scsi_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001753 struct ib_class_port_info *cpi;
1754 int opcode;
1755
1756 switch (event->param.rej_rcvd.reason) {
1757 case IB_CM_REJ_PORT_CM_REDIRECT:
1758 cpi = event->param.rej_rcvd.ari;
1759 target->path.dlid = cpi->redirect_lid;
1760 target->path.pkey = cpi->redirect_pkey;
1761 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1762 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1763
1764 target->status = target->path.dlid ?
1765 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1766 break;
1767
1768 case IB_CM_REJ_PORT_REDIRECT:
Roland Dreier5d7cbfd2007-08-03 10:45:18 -07001769 if (srp_target_is_topspin(target)) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001770 /*
1771 * Topspin/Cisco SRP gateways incorrectly send
1772 * reject reason code 25 when they mean 24
1773 * (port redirect).
1774 */
1775 memcpy(target->path.dgid.raw,
1776 event->param.rej_rcvd.ari, 16);
1777
David Dillow7aa54bd2008-01-07 18:23:41 -05001778 shost_printk(KERN_DEBUG, shost,
1779 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1780 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1781 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001782
1783 target->status = SRP_PORT_REDIRECT;
1784 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001785 shost_printk(KERN_WARNING, shost,
1786 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001787 target->status = -ECONNRESET;
1788 }
1789 break;
1790
1791 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
David Dillow7aa54bd2008-01-07 18:23:41 -05001792 shost_printk(KERN_WARNING, shost,
1793 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001794 target->status = -ECONNRESET;
1795 break;
1796
1797 case IB_CM_REJ_CONSUMER_DEFINED:
1798 opcode = *(u8 *) event->private_data;
1799 if (opcode == SRP_LOGIN_REJ) {
1800 struct srp_login_rej *rej = event->private_data;
1801 u32 reason = be32_to_cpu(rej->reason);
1802
1803 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
David Dillow7aa54bd2008-01-07 18:23:41 -05001804 shost_printk(KERN_WARNING, shost,
1805 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001806 else
David Dillow7aa54bd2008-01-07 18:23:41 -05001807 shost_printk(KERN_WARNING, shost,
1808 PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001809 } else
David Dillow7aa54bd2008-01-07 18:23:41 -05001810 shost_printk(KERN_WARNING, shost,
1811 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1812 " opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001813 target->status = -ECONNRESET;
1814 break;
1815
David Dillow9fe4bcf2008-01-08 17:08:52 -05001816 case IB_CM_REJ_STALE_CONN:
1817 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
1818 target->status = SRP_STALE_CONN;
1819 break;
1820
Roland Dreieraef9ec32005-11-02 14:07:13 -08001821 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001822 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
1823 event->param.rej_rcvd.reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001824 target->status = -ECONNRESET;
1825 }
1826}
1827
1828static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1829{
1830 struct srp_target_port *target = cm_id->context;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001831 int comp = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001832
1833 switch (event->event) {
1834 case IB_CM_REQ_ERROR:
David Dillow7aa54bd2008-01-07 18:23:41 -05001835 shost_printk(KERN_DEBUG, target->scsi_host,
1836 PFX "Sending CM REQ failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001837 comp = 1;
1838 target->status = -ECONNRESET;
1839 break;
1840
1841 case IB_CM_REP_RECEIVED:
1842 comp = 1;
David Dillow961e0be2011-01-14 17:32:07 -05001843 srp_cm_rep_handler(cm_id, event->private_data, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001844 break;
1845
1846 case IB_CM_REJ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001847 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001848 comp = 1;
1849
1850 srp_cm_rej_handler(cm_id, event, target);
1851 break;
1852
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001853 case IB_CM_DREQ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001854 shost_printk(KERN_WARNING, target->scsi_host,
1855 PFX "DREQ received - connection closed\n");
Bart Van Assche294c8752011-12-25 12:18:12 +00001856 srp_change_conn_state(target, false);
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001857 if (ib_send_cm_drep(cm_id, NULL, 0))
David Dillow7aa54bd2008-01-07 18:23:41 -05001858 shost_printk(KERN_ERR, target->scsi_host,
1859 PFX "Sending CM DREP failed\n");
Bart Van Asschec1120f82013-10-26 14:35:08 +02001860 queue_work(system_long_wq, &target->tl_err_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001861 break;
1862
1863 case IB_CM_TIMEWAIT_EXIT:
David Dillow7aa54bd2008-01-07 18:23:41 -05001864 shost_printk(KERN_ERR, target->scsi_host,
1865 PFX "connection closed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001866
Roland Dreieraef9ec32005-11-02 14:07:13 -08001867 target->status = 0;
1868 break;
1869
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001870 case IB_CM_MRA_RECEIVED:
1871 case IB_CM_DREQ_ERROR:
1872 case IB_CM_DREP_RECEIVED:
1873 break;
1874
Roland Dreieraef9ec32005-11-02 14:07:13 -08001875 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001876 shost_printk(KERN_WARNING, target->scsi_host,
1877 PFX "Unhandled CM event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001878 break;
1879 }
1880
1881 if (comp)
1882 complete(&target->done);
1883
Roland Dreieraef9ec32005-11-02 14:07:13 -08001884 return 0;
1885}
1886
Jack Wang71444b92013-11-07 11:37:37 +01001887/**
1888 * srp_change_queue_type - changing device queue tag type
1889 * @sdev: scsi device struct
1890 * @tag_type: requested tag type
1891 *
1892 * Returns queue tag type.
1893 */
1894static int
1895srp_change_queue_type(struct scsi_device *sdev, int tag_type)
1896{
1897 if (sdev->tagged_supported) {
1898 scsi_set_tag_type(sdev, tag_type);
1899 if (tag_type)
1900 scsi_activate_tcq(sdev, sdev->queue_depth);
1901 else
1902 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1903 } else
1904 tag_type = 0;
1905
1906 return tag_type;
1907}
1908
1909/**
1910 * srp_change_queue_depth - setting device queue depth
1911 * @sdev: scsi device struct
1912 * @qdepth: requested queue depth
1913 * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
1914 * (see include/scsi/scsi_host.h for definition)
1915 *
1916 * Returns queue depth.
1917 */
1918static int
1919srp_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1920{
1921 struct Scsi_Host *shost = sdev->host;
1922 int max_depth;
1923 if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP) {
1924 max_depth = shost->can_queue;
1925 if (!sdev->tagged_supported)
1926 max_depth = 1;
1927 if (qdepth > max_depth)
1928 qdepth = max_depth;
1929 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1930 } else if (reason == SCSI_QDEPTH_QFULL)
1931 scsi_track_queue_full(sdev, qdepth);
1932 else
1933 return -EOPNOTSUPP;
1934
1935 return sdev->queue_depth;
1936}
1937
Roland Dreierd945e1d2006-05-09 10:50:28 -07001938static int srp_send_tsk_mgmt(struct srp_target_port *target,
David Dillowf8b6e312010-11-26 13:02:21 -05001939 u64 req_tag, unsigned int lun, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001940{
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001941 struct srp_rport *rport = target->rport;
David Dillow19081f32010-10-18 08:54:49 -04001942 struct ib_device *dev = target->srp_host->srp_dev->dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001943 struct srp_iu *iu;
1944 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001945
Bart Van Assche3780d1f2013-02-21 17:18:00 +00001946 if (!target->connected || target->qp_in_error)
1947 return -1;
1948
David Dillowf8b6e312010-11-26 13:02:21 -05001949 init_completion(&target->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001950
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001951 /*
1952 * Lock the rport mutex to avoid that srp_create_target_ib() is
1953 * invoked while a task management function is being sent.
1954 */
1955 mutex_lock(&rport->mutex);
Bart Van Asschee9684672010-11-26 15:08:38 -05001956 spin_lock_irq(&target->lock);
David Dillowbb125882010-10-08 14:40:47 -04001957 iu = __srp_get_tx_iu(target, SRP_IU_TSK_MGMT);
Bart Van Asschee9684672010-11-26 15:08:38 -05001958 spin_unlock_irq(&target->lock);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001959
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001960 if (!iu) {
1961 mutex_unlock(&rport->mutex);
1962
Bart Van Assche76c75b22010-11-26 14:37:47 -05001963 return -1;
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001964 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001965
David Dillow19081f32010-10-18 08:54:49 -04001966 ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt,
1967 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001968 tsk_mgmt = iu->buf;
1969 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1970
1971 tsk_mgmt->opcode = SRP_TSK_MGMT;
David Dillowf8b6e312010-11-26 13:02:21 -05001972 tsk_mgmt->lun = cpu_to_be64((u64) lun << 48);
1973 tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001974 tsk_mgmt->tsk_mgmt_func = func;
David Dillowf8b6e312010-11-26 13:02:21 -05001975 tsk_mgmt->task_tag = req_tag;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001976
David Dillow19081f32010-10-18 08:54:49 -04001977 ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt,
1978 DMA_TO_DEVICE);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001979 if (srp_post_send(target, iu, sizeof *tsk_mgmt)) {
1980 srp_put_tx_iu(target, iu, SRP_IU_TSK_MGMT);
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001981 mutex_unlock(&rport->mutex);
1982
Bart Van Assche76c75b22010-11-26 14:37:47 -05001983 return -1;
1984 }
Bart Van Asschea95cadb2013-10-26 14:37:17 +02001985 mutex_unlock(&rport->mutex);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001986
David Dillowf8b6e312010-11-26 13:02:21 -05001987 if (!wait_for_completion_timeout(&target->tsk_mgmt_done,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001988 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001989 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001990
Roland Dreierd945e1d2006-05-09 10:50:28 -07001991 return 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001992}
1993
Roland Dreieraef9ec32005-11-02 14:07:13 -08001994static int srp_abort(struct scsi_cmnd *scmnd)
1995{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001996 struct srp_target_port *target = host_to_target(scmnd->device->host);
David Dillowf8b6e312010-11-26 13:02:21 -05001997 struct srp_request *req = (struct srp_request *) scmnd->host_scribble;
Bart Van Assche086f44f2013-06-12 15:23:04 +02001998 int ret;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001999
David Dillow7aa54bd2008-01-07 18:23:41 -05002000 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002001
Bart Van Asschec7c4e7f2013-02-21 17:19:04 +00002002 if (!req || !srp_claim_req(target, req, scmnd))
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02002003 return FAILED;
Bart Van Assche086f44f2013-06-12 15:23:04 +02002004 if (srp_send_tsk_mgmt(target, req->index, scmnd->device->lun,
Bart Van Assche80d5e8a2013-07-10 17:36:35 +02002005 SRP_TSK_ABORT_TASK) == 0)
Bart Van Assche086f44f2013-06-12 15:23:04 +02002006 ret = SUCCESS;
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002007 else if (target->rport->state == SRP_RPORT_LOST)
Bart Van Assche99e1c132013-06-28 14:49:58 +02002008 ret = FAST_IO_FAIL;
Bart Van Assche086f44f2013-06-12 15:23:04 +02002009 else
2010 ret = FAILED;
Bart Van Assche22032992012-08-14 13:18:53 +00002011 srp_free_req(target, req, scmnd, 0);
2012 scmnd->result = DID_ABORT << 16;
Bart Van Assched8536672012-08-24 10:29:11 +00002013 scmnd->scsi_done(scmnd);
Roland Dreierd945e1d2006-05-09 10:50:28 -07002014
Bart Van Assche086f44f2013-06-12 15:23:04 +02002015 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002016}
2017
2018static int srp_reset_device(struct scsi_cmnd *scmnd)
2019{
Roland Dreierd945e1d2006-05-09 10:50:28 -07002020 struct srp_target_port *target = host_to_target(scmnd->device->host);
Bart Van Assche536ae142010-11-26 13:58:27 -05002021 int i;
Roland Dreierd945e1d2006-05-09 10:50:28 -07002022
David Dillow7aa54bd2008-01-07 18:23:41 -05002023 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002024
David Dillowf8b6e312010-11-26 13:02:21 -05002025 if (srp_send_tsk_mgmt(target, SRP_TAG_NO_REQ, scmnd->device->lun,
2026 SRP_TSK_LUN_RESET))
Roland Dreierd945e1d2006-05-09 10:50:28 -07002027 return FAILED;
David Dillowf8b6e312010-11-26 13:02:21 -05002028 if (target->tsk_mgmt_status)
Roland Dreierd945e1d2006-05-09 10:50:28 -07002029 return FAILED;
2030
Bart Van Assche4d73f952013-10-26 14:40:37 +02002031 for (i = 0; i < target->req_ring_size; ++i) {
Bart Van Assche536ae142010-11-26 13:58:27 -05002032 struct srp_request *req = &target->req_ring[i];
David Dillowf8b6e312010-11-26 13:02:21 -05002033 if (req->scmnd && req->scmnd->device == scmnd->device)
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002034 srp_finish_req(target, req, DID_RESET << 16);
Bart Van Assche536ae142010-11-26 13:58:27 -05002035 }
Roland Dreierd945e1d2006-05-09 10:50:28 -07002036
Roland Dreierd945e1d2006-05-09 10:50:28 -07002037 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002038}
2039
2040static int srp_reset_host(struct scsi_cmnd *scmnd)
2041{
2042 struct srp_target_port *target = host_to_target(scmnd->device->host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002043
David Dillow7aa54bd2008-01-07 18:23:41 -05002044 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002045
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002046 return srp_reconnect_rport(target->rport) == 0 ? SUCCESS : FAILED;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002047}
2048
Bart Van Asschec9b03c12011-09-03 09:34:48 +02002049static int srp_slave_configure(struct scsi_device *sdev)
2050{
2051 struct Scsi_Host *shost = sdev->host;
2052 struct srp_target_port *target = host_to_target(shost);
2053 struct request_queue *q = sdev->request_queue;
2054 unsigned long timeout;
2055
2056 if (sdev->type == TYPE_DISK) {
2057 timeout = max_t(unsigned, 30 * HZ, target->rq_tmo_jiffies);
2058 blk_queue_rq_timeout(q, timeout);
2059 }
2060
2061 return 0;
2062}
2063
Tony Jonesee959b02008-02-22 00:13:36 +01002064static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
2065 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002066{
Tony Jonesee959b02008-02-22 00:13:36 +01002067 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002068
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002069 return sprintf(buf, "0x%016llx\n",
2070 (unsigned long long) be64_to_cpu(target->id_ext));
2071}
2072
Tony Jonesee959b02008-02-22 00:13:36 +01002073static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
2074 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002075{
Tony Jonesee959b02008-02-22 00:13:36 +01002076 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002077
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002078 return sprintf(buf, "0x%016llx\n",
2079 (unsigned long long) be64_to_cpu(target->ioc_guid));
2080}
2081
Tony Jonesee959b02008-02-22 00:13:36 +01002082static ssize_t show_service_id(struct device *dev,
2083 struct device_attribute *attr, char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002084{
Tony Jonesee959b02008-02-22 00:13:36 +01002085 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002086
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002087 return sprintf(buf, "0x%016llx\n",
2088 (unsigned long long) be64_to_cpu(target->service_id));
2089}
2090
Tony Jonesee959b02008-02-22 00:13:36 +01002091static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
2092 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002093{
Tony Jonesee959b02008-02-22 00:13:36 +01002094 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002095
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002096 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
2097}
2098
Bart Van Assche848b3082013-10-26 14:38:12 +02002099static ssize_t show_sgid(struct device *dev, struct device_attribute *attr,
2100 char *buf)
2101{
2102 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2103
2104 return sprintf(buf, "%pI6\n", target->path.sgid.raw);
2105}
2106
Tony Jonesee959b02008-02-22 00:13:36 +01002107static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
2108 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002109{
Tony Jonesee959b02008-02-22 00:13:36 +01002110 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002111
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002112 return sprintf(buf, "%pI6\n", target->path.dgid.raw);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002113}
2114
Tony Jonesee959b02008-02-22 00:13:36 +01002115static ssize_t show_orig_dgid(struct device *dev,
2116 struct device_attribute *attr, char *buf)
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002117{
Tony Jonesee959b02008-02-22 00:13:36 +01002118 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002119
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002120 return sprintf(buf, "%pI6\n", target->orig_dgid);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002121}
2122
Bart Van Assche89de7482010-08-03 14:08:45 +00002123static ssize_t show_req_lim(struct device *dev,
2124 struct device_attribute *attr, char *buf)
2125{
2126 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2127
Bart Van Assche89de7482010-08-03 14:08:45 +00002128 return sprintf(buf, "%d\n", target->req_lim);
2129}
2130
Tony Jonesee959b02008-02-22 00:13:36 +01002131static ssize_t show_zero_req_lim(struct device *dev,
2132 struct device_attribute *attr, char *buf)
Roland Dreier6bfa24f2006-06-17 20:37:33 -07002133{
Tony Jonesee959b02008-02-22 00:13:36 +01002134 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6bfa24f2006-06-17 20:37:33 -07002135
Roland Dreier6bfa24f2006-06-17 20:37:33 -07002136 return sprintf(buf, "%d\n", target->zero_req_lim);
2137}
2138
Tony Jonesee959b02008-02-22 00:13:36 +01002139static ssize_t show_local_ib_port(struct device *dev,
2140 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002141{
Tony Jonesee959b02008-02-22 00:13:36 +01002142 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002143
2144 return sprintf(buf, "%d\n", target->srp_host->port);
2145}
2146
Tony Jonesee959b02008-02-22 00:13:36 +01002147static ssize_t show_local_ib_device(struct device *dev,
2148 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002149{
Tony Jonesee959b02008-02-22 00:13:36 +01002150 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002151
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002152 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03002153}
2154
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002155static ssize_t show_comp_vector(struct device *dev,
2156 struct device_attribute *attr, char *buf)
2157{
2158 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2159
2160 return sprintf(buf, "%d\n", target->comp_vector);
2161}
2162
Vu Pham7bb312e2013-10-26 14:31:27 +02002163static ssize_t show_tl_retry_count(struct device *dev,
2164 struct device_attribute *attr, char *buf)
2165{
2166 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2167
2168 return sprintf(buf, "%d\n", target->tl_retry_count);
2169}
2170
David Dillow49248642011-01-14 18:23:24 -05002171static ssize_t show_cmd_sg_entries(struct device *dev,
2172 struct device_attribute *attr, char *buf)
2173{
2174 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2175
2176 return sprintf(buf, "%u\n", target->cmd_sg_cnt);
2177}
2178
David Dillowc07d4242011-01-16 13:57:10 -05002179static ssize_t show_allow_ext_sg(struct device *dev,
2180 struct device_attribute *attr, char *buf)
2181{
2182 struct srp_target_port *target = host_to_target(class_to_shost(dev));
2183
2184 return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false");
2185}
2186
Tony Jonesee959b02008-02-22 00:13:36 +01002187static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
2188static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
2189static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
2190static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
Bart Van Assche848b3082013-10-26 14:38:12 +02002191static DEVICE_ATTR(sgid, S_IRUGO, show_sgid, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01002192static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
2193static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
Bart Van Assche89de7482010-08-03 14:08:45 +00002194static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01002195static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
2196static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
2197static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002198static DEVICE_ATTR(comp_vector, S_IRUGO, show_comp_vector, NULL);
Vu Pham7bb312e2013-10-26 14:31:27 +02002199static DEVICE_ATTR(tl_retry_count, S_IRUGO, show_tl_retry_count, NULL);
David Dillow49248642011-01-14 18:23:24 -05002200static DEVICE_ATTR(cmd_sg_entries, S_IRUGO, show_cmd_sg_entries, NULL);
David Dillowc07d4242011-01-16 13:57:10 -05002201static DEVICE_ATTR(allow_ext_sg, S_IRUGO, show_allow_ext_sg, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002202
Tony Jonesee959b02008-02-22 00:13:36 +01002203static struct device_attribute *srp_host_attrs[] = {
2204 &dev_attr_id_ext,
2205 &dev_attr_ioc_guid,
2206 &dev_attr_service_id,
2207 &dev_attr_pkey,
Bart Van Assche848b3082013-10-26 14:38:12 +02002208 &dev_attr_sgid,
Tony Jonesee959b02008-02-22 00:13:36 +01002209 &dev_attr_dgid,
2210 &dev_attr_orig_dgid,
Bart Van Assche89de7482010-08-03 14:08:45 +00002211 &dev_attr_req_lim,
Tony Jonesee959b02008-02-22 00:13:36 +01002212 &dev_attr_zero_req_lim,
2213 &dev_attr_local_ib_port,
2214 &dev_attr_local_ib_device,
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002215 &dev_attr_comp_vector,
Vu Pham7bb312e2013-10-26 14:31:27 +02002216 &dev_attr_tl_retry_count,
David Dillow49248642011-01-14 18:23:24 -05002217 &dev_attr_cmd_sg_entries,
David Dillowc07d4242011-01-16 13:57:10 -05002218 &dev_attr_allow_ext_sg,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002219 NULL
2220};
2221
Roland Dreieraef9ec32005-11-02 14:07:13 -08002222static struct scsi_host_template srp_template = {
2223 .module = THIS_MODULE,
Roland Dreierb7f008f2007-05-06 21:18:11 -07002224 .name = "InfiniBand SRP initiator",
2225 .proc_name = DRV_NAME,
Bart Van Asschec9b03c12011-09-03 09:34:48 +02002226 .slave_configure = srp_slave_configure,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002227 .info = srp_target_info,
2228 .queuecommand = srp_queuecommand,
Jack Wang71444b92013-11-07 11:37:37 +01002229 .change_queue_depth = srp_change_queue_depth,
2230 .change_queue_type = srp_change_queue_type,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002231 .eh_abort_handler = srp_abort,
2232 .eh_device_reset_handler = srp_reset_device,
2233 .eh_host_reset_handler = srp_reset_host,
Bart Van Assche2742c1d2013-06-12 15:24:25 +02002234 .skip_settle_delay = true,
David Dillow49248642011-01-14 18:23:24 -05002235 .sg_tablesize = SRP_DEF_SG_TABLESIZE,
Bart Van Assche4d73f952013-10-26 14:40:37 +02002236 .can_queue = SRP_DEFAULT_CMD_SQ_SIZE,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002237 .this_id = -1,
Bart Van Assche4d73f952013-10-26 14:40:37 +02002238 .cmd_per_lun = SRP_DEFAULT_CMD_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08002239 .use_clustering = ENABLE_CLUSTERING,
2240 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08002241};
2242
2243static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
2244{
FUJITA Tomonori32368222007-06-27 16:33:12 +09002245 struct srp_rport_identifiers ids;
2246 struct srp_rport *rport;
2247
Roland Dreieraef9ec32005-11-02 14:07:13 -08002248 sprintf(target->target_name, "SRP.T10:%016llX",
2249 (unsigned long long) be64_to_cpu(target->id_ext));
2250
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002251 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002252 return -ENODEV;
2253
FUJITA Tomonori32368222007-06-27 16:33:12 +09002254 memcpy(ids.port_id, &target->id_ext, 8);
2255 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09002256 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori32368222007-06-27 16:33:12 +09002257 rport = srp_rport_add(target->scsi_host, &ids);
2258 if (IS_ERR(rport)) {
2259 scsi_remove_host(target->scsi_host);
2260 return PTR_ERR(rport);
2261 }
2262
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02002263 rport->lld_data = target;
Bart Van Assche9dd69a62013-10-26 14:32:30 +02002264 target->rport = rport;
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02002265
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002266 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002267 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002268 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002269
2270 target->state = SRP_TARGET_LIVE;
2271
Roland Dreieraef9ec32005-11-02 14:07:13 -08002272 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07002273 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002274
2275 return 0;
2276}
2277
Tony Jonesee959b02008-02-22 00:13:36 +01002278static void srp_release_dev(struct device *dev)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002279{
2280 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01002281 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002282
2283 complete(&host->released);
2284}
2285
2286static struct class srp_class = {
2287 .name = "infiniband_srp",
Tony Jonesee959b02008-02-22 00:13:36 +01002288 .dev_release = srp_release_dev
Roland Dreieraef9ec32005-11-02 14:07:13 -08002289};
2290
Bart Van Assche96fc2482013-06-28 14:51:26 +02002291/**
2292 * srp_conn_unique() - check whether the connection to a target is unique
2293 */
2294static bool srp_conn_unique(struct srp_host *host,
2295 struct srp_target_port *target)
2296{
2297 struct srp_target_port *t;
2298 bool ret = false;
2299
2300 if (target->state == SRP_TARGET_REMOVED)
2301 goto out;
2302
2303 ret = true;
2304
2305 spin_lock(&host->target_lock);
2306 list_for_each_entry(t, &host->target_list, list) {
2307 if (t != target &&
2308 target->id_ext == t->id_ext &&
2309 target->ioc_guid == t->ioc_guid &&
2310 target->initiator_ext == t->initiator_ext) {
2311 ret = false;
2312 break;
2313 }
2314 }
2315 spin_unlock(&host->target_lock);
2316
2317out:
2318 return ret;
2319}
2320
Roland Dreieraef9ec32005-11-02 14:07:13 -08002321/*
2322 * Target ports are added by writing
2323 *
2324 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
2325 * pkey=<P_Key>,service_id=<service ID>
2326 *
2327 * to the add_target sysfs attribute.
2328 */
2329enum {
2330 SRP_OPT_ERR = 0,
2331 SRP_OPT_ID_EXT = 1 << 0,
2332 SRP_OPT_IOC_GUID = 1 << 1,
2333 SRP_OPT_DGID = 1 << 2,
2334 SRP_OPT_PKEY = 1 << 3,
2335 SRP_OPT_SERVICE_ID = 1 << 4,
2336 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07002337 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07002338 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002339 SRP_OPT_INITIATOR_EXT = 1 << 8,
David Dillow49248642011-01-14 18:23:24 -05002340 SRP_OPT_CMD_SG_ENTRIES = 1 << 9,
David Dillowc07d4242011-01-16 13:57:10 -05002341 SRP_OPT_ALLOW_EXT_SG = 1 << 10,
2342 SRP_OPT_SG_TABLESIZE = 1 << 11,
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002343 SRP_OPT_COMP_VECTOR = 1 << 12,
Vu Pham7bb312e2013-10-26 14:31:27 +02002344 SRP_OPT_TL_RETRY_COUNT = 1 << 13,
Bart Van Assche4d73f952013-10-26 14:40:37 +02002345 SRP_OPT_QUEUE_SIZE = 1 << 14,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002346 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
2347 SRP_OPT_IOC_GUID |
2348 SRP_OPT_DGID |
2349 SRP_OPT_PKEY |
2350 SRP_OPT_SERVICE_ID),
2351};
2352
Steven Whitehousea447c092008-10-13 10:46:57 +01002353static const match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07002354 { SRP_OPT_ID_EXT, "id_ext=%s" },
2355 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
2356 { SRP_OPT_DGID, "dgid=%s" },
2357 { SRP_OPT_PKEY, "pkey=%x" },
2358 { SRP_OPT_SERVICE_ID, "service_id=%s" },
2359 { SRP_OPT_MAX_SECT, "max_sect=%d" },
2360 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07002361 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002362 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
David Dillow49248642011-01-14 18:23:24 -05002363 { SRP_OPT_CMD_SG_ENTRIES, "cmd_sg_entries=%u" },
David Dillowc07d4242011-01-16 13:57:10 -05002364 { SRP_OPT_ALLOW_EXT_SG, "allow_ext_sg=%u" },
2365 { SRP_OPT_SG_TABLESIZE, "sg_tablesize=%u" },
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002366 { SRP_OPT_COMP_VECTOR, "comp_vector=%u" },
Vu Pham7bb312e2013-10-26 14:31:27 +02002367 { SRP_OPT_TL_RETRY_COUNT, "tl_retry_count=%u" },
Bart Van Assche4d73f952013-10-26 14:40:37 +02002368 { SRP_OPT_QUEUE_SIZE, "queue_size=%d" },
Vu Pham52fb2b502006-06-17 20:37:31 -07002369 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002370};
2371
2372static int srp_parse_options(const char *buf, struct srp_target_port *target)
2373{
2374 char *options, *sep_opt;
2375 char *p;
2376 char dgid[3];
2377 substring_t args[MAX_OPT_ARGS];
2378 int opt_mask = 0;
2379 int token;
2380 int ret = -EINVAL;
2381 int i;
2382
2383 options = kstrdup(buf, GFP_KERNEL);
2384 if (!options)
2385 return -ENOMEM;
2386
2387 sep_opt = options;
2388 while ((p = strsep(&sep_opt, ",")) != NULL) {
2389 if (!*p)
2390 continue;
2391
2392 token = match_token(p, srp_opt_tokens, args);
2393 opt_mask |= token;
2394
2395 switch (token) {
2396 case SRP_OPT_ID_EXT:
2397 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002398 if (!p) {
2399 ret = -ENOMEM;
2400 goto out;
2401 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002402 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2403 kfree(p);
2404 break;
2405
2406 case SRP_OPT_IOC_GUID:
2407 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002408 if (!p) {
2409 ret = -ENOMEM;
2410 goto out;
2411 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002412 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
2413 kfree(p);
2414 break;
2415
2416 case SRP_OPT_DGID:
2417 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002418 if (!p) {
2419 ret = -ENOMEM;
2420 goto out;
2421 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002422 if (strlen(p) != 32) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002423 pr_warn("bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07002424 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002425 goto out;
2426 }
2427
2428 for (i = 0; i < 16; ++i) {
2429 strlcpy(dgid, p + i * 2, 3);
2430 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
2431 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08002432 kfree(p);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002433 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002434 break;
2435
2436 case SRP_OPT_PKEY:
2437 if (match_hex(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002438 pr_warn("bad P_Key parameter '%s'\n", p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002439 goto out;
2440 }
2441 target->path.pkey = cpu_to_be16(token);
2442 break;
2443
2444 case SRP_OPT_SERVICE_ID:
2445 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002446 if (!p) {
2447 ret = -ENOMEM;
2448 goto out;
2449 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002450 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
Sean Hefty247e0202007-08-08 15:51:18 -07002451 target->path.service_id = target->service_id;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002452 kfree(p);
2453 break;
2454
2455 case SRP_OPT_MAX_SECT:
2456 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002457 pr_warn("bad max sect parameter '%s'\n", p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002458 goto out;
2459 }
2460 target->scsi_host->max_sectors = token;
2461 break;
2462
Bart Van Assche4d73f952013-10-26 14:40:37 +02002463 case SRP_OPT_QUEUE_SIZE:
2464 if (match_int(args, &token) || token < 1) {
2465 pr_warn("bad queue_size parameter '%s'\n", p);
2466 goto out;
2467 }
2468 target->scsi_host->can_queue = token;
2469 target->queue_size = token + SRP_RSP_SQ_SIZE +
2470 SRP_TSK_MGMT_SQ_SIZE;
2471 if (!(opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
2472 target->scsi_host->cmd_per_lun = token;
2473 break;
2474
Vu Pham52fb2b502006-06-17 20:37:31 -07002475 case SRP_OPT_MAX_CMD_PER_LUN:
Bart Van Assche4d73f952013-10-26 14:40:37 +02002476 if (match_int(args, &token) || token < 1) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002477 pr_warn("bad max cmd_per_lun parameter '%s'\n",
2478 p);
Vu Pham52fb2b502006-06-17 20:37:31 -07002479 goto out;
2480 }
Bart Van Assche4d73f952013-10-26 14:40:37 +02002481 target->scsi_host->cmd_per_lun = token;
Vu Pham52fb2b502006-06-17 20:37:31 -07002482 break;
2483
Ramachandra K0c0450db2006-06-17 20:37:38 -07002484 case SRP_OPT_IO_CLASS:
2485 if (match_hex(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002486 pr_warn("bad IO class parameter '%s'\n", p);
Ramachandra K0c0450db2006-06-17 20:37:38 -07002487 goto out;
2488 }
2489 if (token != SRP_REV10_IB_IO_CLASS &&
2490 token != SRP_REV16A_IB_IO_CLASS) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002491 pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n",
2492 token, SRP_REV10_IB_IO_CLASS,
2493 SRP_REV16A_IB_IO_CLASS);
Ramachandra K0c0450db2006-06-17 20:37:38 -07002494 goto out;
2495 }
2496 target->io_class = token;
2497 break;
2498
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002499 case SRP_OPT_INITIATOR_EXT:
2500 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002501 if (!p) {
2502 ret = -ENOMEM;
2503 goto out;
2504 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002505 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2506 kfree(p);
2507 break;
2508
David Dillow49248642011-01-14 18:23:24 -05002509 case SRP_OPT_CMD_SG_ENTRIES:
2510 if (match_int(args, &token) || token < 1 || token > 255) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002511 pr_warn("bad max cmd_sg_entries parameter '%s'\n",
2512 p);
David Dillow49248642011-01-14 18:23:24 -05002513 goto out;
2514 }
2515 target->cmd_sg_cnt = token;
2516 break;
2517
David Dillowc07d4242011-01-16 13:57:10 -05002518 case SRP_OPT_ALLOW_EXT_SG:
2519 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002520 pr_warn("bad allow_ext_sg parameter '%s'\n", p);
David Dillowc07d4242011-01-16 13:57:10 -05002521 goto out;
2522 }
2523 target->allow_ext_sg = !!token;
2524 break;
2525
2526 case SRP_OPT_SG_TABLESIZE:
2527 if (match_int(args, &token) || token < 1 ||
2528 token > SCSI_MAX_SG_CHAIN_SEGMENTS) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002529 pr_warn("bad max sg_tablesize parameter '%s'\n",
2530 p);
David Dillowc07d4242011-01-16 13:57:10 -05002531 goto out;
2532 }
2533 target->sg_tablesize = token;
2534 break;
2535
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002536 case SRP_OPT_COMP_VECTOR:
2537 if (match_int(args, &token) || token < 0) {
2538 pr_warn("bad comp_vector parameter '%s'\n", p);
2539 goto out;
2540 }
2541 target->comp_vector = token;
2542 break;
2543
Vu Pham7bb312e2013-10-26 14:31:27 +02002544 case SRP_OPT_TL_RETRY_COUNT:
2545 if (match_int(args, &token) || token < 2 || token > 7) {
2546 pr_warn("bad tl_retry_count parameter '%s' (must be a number between 2 and 7)\n",
2547 p);
2548 goto out;
2549 }
2550 target->tl_retry_count = token;
2551 break;
2552
Roland Dreieraef9ec32005-11-02 14:07:13 -08002553 default:
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002554 pr_warn("unknown parameter or missing value '%s' in target creation request\n",
2555 p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002556 goto out;
2557 }
2558 }
2559
2560 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
2561 ret = 0;
2562 else
2563 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
2564 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
2565 !(srp_opt_tokens[i].token & opt_mask))
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002566 pr_warn("target creation request is missing parameter '%s'\n",
2567 srp_opt_tokens[i].pattern);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002568
Bart Van Assche4d73f952013-10-26 14:40:37 +02002569 if (target->scsi_host->cmd_per_lun > target->scsi_host->can_queue
2570 && (opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
2571 pr_warn("cmd_per_lun = %d > queue_size = %d\n",
2572 target->scsi_host->cmd_per_lun,
2573 target->scsi_host->can_queue);
2574
Roland Dreieraef9ec32005-11-02 14:07:13 -08002575out:
2576 kfree(options);
2577 return ret;
2578}
2579
Tony Jonesee959b02008-02-22 00:13:36 +01002580static ssize_t srp_create_target(struct device *dev,
2581 struct device_attribute *attr,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002582 const char *buf, size_t count)
2583{
2584 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01002585 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002586 struct Scsi_Host *target_host;
2587 struct srp_target_port *target;
David Dillowc07d4242011-01-16 13:57:10 -05002588 struct ib_device *ibdev = host->srp_dev->dev;
Bart Van Asscheb81d00b2013-10-26 14:38:47 +02002589 int ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002590
2591 target_host = scsi_host_alloc(&srp_template,
2592 sizeof (struct srp_target_port));
2593 if (!target_host)
2594 return -ENOMEM;
2595
David Dillow49248642011-01-14 18:23:24 -05002596 target_host->transportt = ib_srp_transport_template;
Bart Van Asschefd1b6c42011-07-13 09:19:16 -07002597 target_host->max_channel = 0;
2598 target_host->max_id = 1;
Arne Redlich3c8edf02006-11-15 12:43:00 +01002599 target_host->max_lun = SRP_MAX_LUN;
2600 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08002601
Roland Dreieraef9ec32005-11-02 14:07:13 -08002602 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002603
David Dillow49248642011-01-14 18:23:24 -05002604 target->io_class = SRP_REV16A_IB_IO_CLASS;
2605 target->scsi_host = target_host;
2606 target->srp_host = host;
2607 target->lkey = host->srp_dev->mr->lkey;
2608 target->rkey = host->srp_dev->mr->rkey;
2609 target->cmd_sg_cnt = cmd_sg_entries;
David Dillowc07d4242011-01-16 13:57:10 -05002610 target->sg_tablesize = indirect_sg_entries ? : cmd_sg_entries;
2611 target->allow_ext_sg = allow_ext_sg;
Vu Pham7bb312e2013-10-26 14:31:27 +02002612 target->tl_retry_count = 7;
Bart Van Assche4d73f952013-10-26 14:40:37 +02002613 target->queue_size = SRP_DEFAULT_QUEUE_SIZE;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002614
Roland Dreieraef9ec32005-11-02 14:07:13 -08002615 ret = srp_parse_options(buf, target);
2616 if (ret)
2617 goto err;
2618
Bart Van Assche4d73f952013-10-26 14:40:37 +02002619 target->req_ring_size = target->queue_size - SRP_TSK_MGMT_SQ_SIZE;
2620
Bart Van Assche96fc2482013-06-28 14:51:26 +02002621 if (!srp_conn_unique(target->srp_host, target)) {
2622 shost_printk(KERN_INFO, target->scsi_host,
2623 PFX "Already connected to target port with id_ext=%016llx;ioc_guid=%016llx;initiator_ext=%016llx\n",
2624 be64_to_cpu(target->id_ext),
2625 be64_to_cpu(target->ioc_guid),
2626 be64_to_cpu(target->initiator_ext));
2627 ret = -EEXIST;
2628 goto err;
2629 }
2630
David Dillowc07d4242011-01-16 13:57:10 -05002631 if (!host->srp_dev->fmr_pool && !target->allow_ext_sg &&
2632 target->cmd_sg_cnt < target->sg_tablesize) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002633 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 -05002634 target->sg_tablesize = target->cmd_sg_cnt;
2635 }
2636
2637 target_host->sg_tablesize = target->sg_tablesize;
2638 target->indirect_size = target->sg_tablesize *
2639 sizeof (struct srp_direct_buf);
David Dillow49248642011-01-14 18:23:24 -05002640 target->max_iu_len = sizeof (struct srp_cmd) +
2641 sizeof (struct srp_indirect_buf) +
2642 target->cmd_sg_cnt * sizeof (struct srp_direct_buf);
2643
Bart Van Asschec1120f82013-10-26 14:35:08 +02002644 INIT_WORK(&target->tl_err_work, srp_tl_err_work);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002645 INIT_WORK(&target->remove_work, srp_remove_work);
David Dillow8f26c9f2011-01-14 19:45:50 -05002646 spin_lock_init(&target->lock);
2647 INIT_LIST_HEAD(&target->free_tx);
Bart Van Asscheb81d00b2013-10-26 14:38:47 +02002648 ret = srp_alloc_req_data(target);
2649 if (ret)
2650 goto err_free_mem;
David Dillow8f26c9f2011-01-14 19:45:50 -05002651
David Dillowc07d4242011-01-16 13:57:10 -05002652 ib_query_gid(ibdev, host->port, 0, &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002653
David Dillow7aa54bd2008-01-07 18:23:41 -05002654 shost_printk(KERN_DEBUG, target->scsi_host, PFX
2655 "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002656 "service_id %016llx dgid %pI6\n",
Roland Dreieraef9ec32005-11-02 14:07:13 -08002657 (unsigned long long) be64_to_cpu(target->id_ext),
2658 (unsigned long long) be64_to_cpu(target->ioc_guid),
2659 be16_to_cpu(target->path.pkey),
2660 (unsigned long long) be64_to_cpu(target->service_id),
Harvey Harrison8867cd72008-10-28 22:36:33 -07002661 target->path.dgid.raw);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002662
2663 ret = srp_create_target_ib(target);
2664 if (ret)
David Dillow8f26c9f2011-01-14 19:45:50 -05002665 goto err_free_mem;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002666
David Dillow9fe4bcf2008-01-08 17:08:52 -05002667 ret = srp_new_cm_id(target);
2668 if (ret)
David Dillow8f26c9f2011-01-14 19:45:50 -05002669 goto err_free_ib;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002670
2671 ret = srp_connect_target(target);
2672 if (ret) {
David Dillow7aa54bd2008-01-07 18:23:41 -05002673 shost_printk(KERN_ERR, target->scsi_host,
2674 PFX "Connection failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002675 goto err_cm_id;
2676 }
2677
2678 ret = srp_add_target(host, target);
2679 if (ret)
2680 goto err_disconnect;
2681
2682 return count;
2683
2684err_disconnect:
2685 srp_disconnect_target(target);
2686
2687err_cm_id:
2688 ib_destroy_cm_id(target->cm_id);
2689
David Dillow8f26c9f2011-01-14 19:45:50 -05002690err_free_ib:
Roland Dreieraef9ec32005-11-02 14:07:13 -08002691 srp_free_target_ib(target);
2692
David Dillow8f26c9f2011-01-14 19:45:50 -05002693err_free_mem:
2694 srp_free_req_data(target);
2695
Roland Dreieraef9ec32005-11-02 14:07:13 -08002696err:
2697 scsi_host_put(target_host);
2698
2699 return ret;
2700}
2701
Tony Jonesee959b02008-02-22 00:13:36 +01002702static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002703
Tony Jonesee959b02008-02-22 00:13:36 +01002704static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
2705 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002706{
Tony Jonesee959b02008-02-22 00:13:36 +01002707 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002708
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002709 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002710}
2711
Tony Jonesee959b02008-02-22 00:13:36 +01002712static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002713
Tony Jonesee959b02008-02-22 00:13:36 +01002714static ssize_t show_port(struct device *dev, struct device_attribute *attr,
2715 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002716{
Tony Jonesee959b02008-02-22 00:13:36 +01002717 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002718
2719 return sprintf(buf, "%d\n", host->port);
2720}
2721
Tony Jonesee959b02008-02-22 00:13:36 +01002722static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002723
Roland Dreierf5358a12006-06-17 20:37:29 -07002724static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002725{
2726 struct srp_host *host;
2727
2728 host = kzalloc(sizeof *host, GFP_KERNEL);
2729 if (!host)
2730 return NULL;
2731
2732 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002733 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002734 init_completion(&host->released);
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002735 host->srp_dev = device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002736 host->port = port;
2737
Tony Jonesee959b02008-02-22 00:13:36 +01002738 host->dev.class = &srp_class;
2739 host->dev.parent = device->dev->dma_device;
Kay Sieversd927e382009-01-06 10:44:39 -08002740 dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002741
Tony Jonesee959b02008-02-22 00:13:36 +01002742 if (device_register(&host->dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07002743 goto free_host;
Tony Jonesee959b02008-02-22 00:13:36 +01002744 if (device_create_file(&host->dev, &dev_attr_add_target))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002745 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01002746 if (device_create_file(&host->dev, &dev_attr_ibdev))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002747 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01002748 if (device_create_file(&host->dev, &dev_attr_port))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002749 goto err_class;
2750
2751 return host;
2752
2753err_class:
Tony Jonesee959b02008-02-22 00:13:36 +01002754 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002755
Roland Dreierf5358a12006-06-17 20:37:29 -07002756free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08002757 kfree(host);
2758
2759 return NULL;
2760}
2761
2762static void srp_add_one(struct ib_device *device)
2763{
Roland Dreierf5358a12006-06-17 20:37:29 -07002764 struct srp_device *srp_dev;
2765 struct ib_device_attr *dev_attr;
2766 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002767 struct srp_host *host;
David Dillowbe8b9812011-01-18 21:58:09 -05002768 int max_pages_per_fmr, fmr_page_shift, s, e, p;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002769
Roland Dreierf5358a12006-06-17 20:37:29 -07002770 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
2771 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08002772 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002773
Roland Dreierf5358a12006-06-17 20:37:29 -07002774 if (ib_query_device(device, dev_attr)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002775 pr_warn("Query device failed for %s\n", device->name);
Roland Dreierf5358a12006-06-17 20:37:29 -07002776 goto free_attr;
2777 }
2778
2779 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
2780 if (!srp_dev)
2781 goto free_attr;
2782
2783 /*
2784 * Use the smallest page size supported by the HCA, down to a
David Dillow8f26c9f2011-01-14 19:45:50 -05002785 * minimum of 4096 bytes. We're unlikely to build large sglists
2786 * out of smaller entries.
Roland Dreierf5358a12006-06-17 20:37:29 -07002787 */
David Dillow8f26c9f2011-01-14 19:45:50 -05002788 fmr_page_shift = max(12, ffs(dev_attr->page_size_cap) - 1);
2789 srp_dev->fmr_page_size = 1 << fmr_page_shift;
2790 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
2791 srp_dev->fmr_max_size = srp_dev->fmr_page_size * SRP_FMR_SIZE;
Roland Dreierf5358a12006-06-17 20:37:29 -07002792
2793 INIT_LIST_HEAD(&srp_dev->dev_list);
2794
2795 srp_dev->dev = device;
2796 srp_dev->pd = ib_alloc_pd(device);
2797 if (IS_ERR(srp_dev->pd))
2798 goto free_dev;
2799
2800 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
2801 IB_ACCESS_LOCAL_WRITE |
2802 IB_ACCESS_REMOTE_READ |
2803 IB_ACCESS_REMOTE_WRITE);
2804 if (IS_ERR(srp_dev->mr))
2805 goto err_pd;
2806
David Dillowbe8b9812011-01-18 21:58:09 -05002807 for (max_pages_per_fmr = SRP_FMR_SIZE;
2808 max_pages_per_fmr >= SRP_FMR_MIN_SIZE;
2809 max_pages_per_fmr /= 2, srp_dev->fmr_max_size /= 2) {
2810 memset(&fmr_param, 0, sizeof fmr_param);
2811 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
2812 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
2813 fmr_param.cache = 1;
2814 fmr_param.max_pages_per_fmr = max_pages_per_fmr;
2815 fmr_param.page_shift = fmr_page_shift;
2816 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
2817 IB_ACCESS_REMOTE_WRITE |
2818 IB_ACCESS_REMOTE_READ);
Roland Dreierf5358a12006-06-17 20:37:29 -07002819
David Dillowbe8b9812011-01-18 21:58:09 -05002820 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
2821 if (!IS_ERR(srp_dev->fmr_pool))
2822 break;
2823 }
2824
Roland Dreierf5358a12006-06-17 20:37:29 -07002825 if (IS_ERR(srp_dev->fmr_pool))
2826 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002827
Tom Tucker07ebafb2006-08-03 16:02:42 -05002828 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002829 s = 0;
2830 e = 0;
2831 } else {
2832 s = 1;
2833 e = device->phys_port_cnt;
2834 }
2835
2836 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07002837 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002838 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07002839 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002840 }
2841
Roland Dreierf5358a12006-06-17 20:37:29 -07002842 ib_set_client_data(device, &srp_client, srp_dev);
2843
2844 goto free_attr;
2845
2846err_pd:
2847 ib_dealloc_pd(srp_dev->pd);
2848
2849free_dev:
2850 kfree(srp_dev);
2851
2852free_attr:
2853 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002854}
2855
2856static void srp_remove_one(struct ib_device *device)
2857{
Roland Dreierf5358a12006-06-17 20:37:29 -07002858 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002859 struct srp_host *host, *tmp_host;
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002860 struct srp_target_port *target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002861
Roland Dreierf5358a12006-06-17 20:37:29 -07002862 srp_dev = ib_get_client_data(device, &srp_client);
Dotan Barak1fe0cb82013-06-12 15:20:36 +02002863 if (!srp_dev)
2864 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002865
Roland Dreierf5358a12006-06-17 20:37:29 -07002866 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Tony Jonesee959b02008-02-22 00:13:36 +01002867 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002868 /*
2869 * Wait for the sysfs entry to go away, so that no new
2870 * target ports can be created.
2871 */
2872 wait_for_completion(&host->released);
2873
2874 /*
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002875 * Remove all target ports.
Roland Dreieraef9ec32005-11-02 14:07:13 -08002876 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002877 spin_lock(&host->target_lock);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002878 list_for_each_entry(target, &host->target_list, list)
2879 srp_queue_remove_work(target);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002880 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002881
2882 /*
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002883 * Wait for target port removal tasks.
Roland Dreieraef9ec32005-11-02 14:07:13 -08002884 */
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002885 flush_workqueue(system_long_wq);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002886
Roland Dreieraef9ec32005-11-02 14:07:13 -08002887 kfree(host);
2888 }
2889
Roland Dreierf5358a12006-06-17 20:37:29 -07002890 if (srp_dev->fmr_pool)
2891 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2892 ib_dereg_mr(srp_dev->mr);
2893 ib_dealloc_pd(srp_dev->pd);
2894
2895 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002896}
2897
FUJITA Tomonori32368222007-06-27 16:33:12 +09002898static struct srp_function_template ib_srp_transport_functions = {
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002899 .has_rport_state = true,
2900 .reset_timer_if_blocked = true,
Bart Van Asschea95cadb2013-10-26 14:37:17 +02002901 .reconnect_delay = &srp_reconnect_delay,
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002902 .fast_io_fail_tmo = &srp_fast_io_fail_tmo,
2903 .dev_loss_tmo = &srp_dev_loss_tmo,
2904 .reconnect = srp_rport_reconnect,
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02002905 .rport_delete = srp_rport_delete,
Bart Van Asscheed9b2262013-10-26 14:34:27 +02002906 .terminate_rport_io = srp_terminate_io,
FUJITA Tomonori32368222007-06-27 16:33:12 +09002907};
2908
Roland Dreieraef9ec32005-11-02 14:07:13 -08002909static int __init srp_init_module(void)
2910{
2911 int ret;
2912
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05002913 BUILD_BUG_ON(FIELD_SIZEOF(struct ib_wc, wr_id) < sizeof(void *));
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00002914
David Dillow49248642011-01-14 18:23:24 -05002915 if (srp_sg_tablesize) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002916 pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n");
David Dillow49248642011-01-14 18:23:24 -05002917 if (!cmd_sg_entries)
2918 cmd_sg_entries = srp_sg_tablesize;
2919 }
2920
2921 if (!cmd_sg_entries)
2922 cmd_sg_entries = SRP_DEF_SG_TABLESIZE;
2923
2924 if (cmd_sg_entries > 255) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002925 pr_warn("Clamping cmd_sg_entries to 255\n");
David Dillow49248642011-01-14 18:23:24 -05002926 cmd_sg_entries = 255;
David Dillow1e89a192008-04-16 21:01:12 -07002927 }
2928
David Dillowc07d4242011-01-16 13:57:10 -05002929 if (!indirect_sg_entries)
2930 indirect_sg_entries = cmd_sg_entries;
2931 else if (indirect_sg_entries < cmd_sg_entries) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002932 pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n",
2933 cmd_sg_entries);
David Dillowc07d4242011-01-16 13:57:10 -05002934 indirect_sg_entries = cmd_sg_entries;
2935 }
2936
FUJITA Tomonori32368222007-06-27 16:33:12 +09002937 ib_srp_transport_template =
2938 srp_attach_transport(&ib_srp_transport_functions);
2939 if (!ib_srp_transport_template)
2940 return -ENOMEM;
2941
Roland Dreieraef9ec32005-11-02 14:07:13 -08002942 ret = class_register(&srp_class);
2943 if (ret) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002944 pr_err("couldn't register class infiniband_srp\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002945 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002946 return ret;
2947 }
2948
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002949 ib_sa_register_client(&srp_sa_client);
2950
Roland Dreieraef9ec32005-11-02 14:07:13 -08002951 ret = ib_register_client(&srp_client);
2952 if (ret) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002953 pr_err("couldn't register IB client\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002954 srp_release_transport(ib_srp_transport_template);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002955 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002956 class_unregister(&srp_class);
2957 return ret;
2958 }
2959
2960 return 0;
2961}
2962
2963static void __exit srp_cleanup_module(void)
2964{
2965 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002966 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002967 class_unregister(&srp_class);
FUJITA Tomonori32368222007-06-27 16:33:12 +09002968 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002969}
2970
2971module_init(srp_init_module);
2972module_exit(srp_cleanup_module);