blob: 11ebf3e7646a6bd6b808edfb8286289ad0f3db67 [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>
49#include <scsi/srp.h>
FUJITA Tomonori32368222007-06-27 16:33:12 +090050#include <scsi/scsi_transport_srp.h>
Roland Dreieraef9ec32005-11-02 14:07:13 -080051
Roland Dreieraef9ec32005-11-02 14:07:13 -080052#include "ib_srp.h"
53
54#define DRV_NAME "ib_srp"
55#define PFX DRV_NAME ": "
Vu Phame8ca4132013-06-28 14:59:08 +020056#define DRV_VERSION "1.0"
57#define DRV_RELDATE "July 1, 2013"
Roland Dreieraef9ec32005-11-02 14:07:13 -080058
59MODULE_AUTHOR("Roland Dreier");
60MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
61 "v" DRV_VERSION " (" DRV_RELDATE ")");
62MODULE_LICENSE("Dual BSD/GPL");
63
David Dillow49248642011-01-14 18:23:24 -050064static unsigned int srp_sg_tablesize;
65static unsigned int cmd_sg_entries;
David Dillowc07d4242011-01-16 13:57:10 -050066static unsigned int indirect_sg_entries;
67static bool allow_ext_sg;
Roland Dreieraef9ec32005-11-02 14:07:13 -080068static int topspin_workarounds = 1;
69
David Dillow49248642011-01-14 18:23:24 -050070module_param(srp_sg_tablesize, uint, 0444);
71MODULE_PARM_DESC(srp_sg_tablesize, "Deprecated name for cmd_sg_entries");
72
73module_param(cmd_sg_entries, uint, 0444);
74MODULE_PARM_DESC(cmd_sg_entries,
75 "Default number of gather/scatter entries in the SRP command (default is 12, max 255)");
76
David Dillowc07d4242011-01-16 13:57:10 -050077module_param(indirect_sg_entries, uint, 0444);
78MODULE_PARM_DESC(indirect_sg_entries,
79 "Default max number of gather/scatter entries (default is 12, max is " __stringify(SCSI_MAX_SG_CHAIN_SEGMENTS) ")");
80
81module_param(allow_ext_sg, bool, 0444);
82MODULE_PARM_DESC(allow_ext_sg,
83 "Default behavior when there are more than cmd_sg_entries S/G entries after mapping; fails the request when false (default false)");
84
Roland Dreieraef9ec32005-11-02 14:07:13 -080085module_param(topspin_workarounds, int, 0444);
86MODULE_PARM_DESC(topspin_workarounds,
87 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
88
Roland Dreieraef9ec32005-11-02 14:07:13 -080089static void srp_add_one(struct ib_device *device);
90static void srp_remove_one(struct ib_device *device);
Bart Van Assche9c03dc92010-02-02 19:23:54 +000091static void srp_recv_completion(struct ib_cq *cq, void *target_ptr);
92static void srp_send_completion(struct ib_cq *cq, void *target_ptr);
Roland Dreieraef9ec32005-11-02 14:07:13 -080093static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
94
FUJITA Tomonori32368222007-06-27 16:33:12 +090095static struct scsi_transport_template *ib_srp_transport_template;
96
Roland Dreieraef9ec32005-11-02 14:07:13 -080097static struct ib_client srp_client = {
98 .name = "srp",
99 .add = srp_add_one,
100 .remove = srp_remove_one
101};
102
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700103static struct ib_sa_client srp_sa_client;
104
Roland Dreieraef9ec32005-11-02 14:07:13 -0800105static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
106{
107 return (struct srp_target_port *) host->hostdata;
108}
109
110static const char *srp_target_info(struct Scsi_Host *host)
111{
112 return host_to_target(host)->target_name;
113}
114
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700115static int srp_target_is_topspin(struct srp_target_port *target)
116{
117 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700118 static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700119
120 return topspin_workarounds &&
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700121 (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
122 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700123}
124
Roland Dreieraef9ec32005-11-02 14:07:13 -0800125static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
126 gfp_t gfp_mask,
127 enum dma_data_direction direction)
128{
129 struct srp_iu *iu;
130
131 iu = kmalloc(sizeof *iu, gfp_mask);
132 if (!iu)
133 goto out;
134
135 iu->buf = kzalloc(size, gfp_mask);
136 if (!iu->buf)
137 goto out_free_iu;
138
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100139 iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
140 direction);
141 if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800142 goto out_free_buf;
143
144 iu->size = size;
145 iu->direction = direction;
146
147 return iu;
148
149out_free_buf:
150 kfree(iu->buf);
151out_free_iu:
152 kfree(iu);
153out:
154 return NULL;
155}
156
157static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
158{
159 if (!iu)
160 return;
161
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100162 ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
163 iu->direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800164 kfree(iu->buf);
165 kfree(iu);
166}
167
168static void srp_qp_event(struct ib_event *event, void *context)
169{
Bart Van Asschee0bda7d2012-01-14 12:39:44 +0000170 pr_debug("QP event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800171}
172
173static int srp_init_qp(struct srp_target_port *target,
174 struct ib_qp *qp)
175{
176 struct ib_qp_attr *attr;
177 int ret;
178
179 attr = kmalloc(sizeof *attr, GFP_KERNEL);
180 if (!attr)
181 return -ENOMEM;
182
Roland Dreier969a60f2008-07-14 23:48:43 -0700183 ret = ib_find_pkey(target->srp_host->srp_dev->dev,
184 target->srp_host->port,
185 be16_to_cpu(target->path.pkey),
186 &attr->pkey_index);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800187 if (ret)
188 goto out;
189
190 attr->qp_state = IB_QPS_INIT;
191 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
192 IB_ACCESS_REMOTE_WRITE);
193 attr->port_num = target->srp_host->port;
194
195 ret = ib_modify_qp(qp, attr,
196 IB_QP_STATE |
197 IB_QP_PKEY_INDEX |
198 IB_QP_ACCESS_FLAGS |
199 IB_QP_PORT);
200
201out:
202 kfree(attr);
203 return ret;
204}
205
David Dillow9fe4bcf2008-01-08 17:08:52 -0500206static int srp_new_cm_id(struct srp_target_port *target)
207{
208 struct ib_cm_id *new_cm_id;
209
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100210 new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
David Dillow9fe4bcf2008-01-08 17:08:52 -0500211 srp_cm_handler, target);
212 if (IS_ERR(new_cm_id))
213 return PTR_ERR(new_cm_id);
214
215 if (target->cm_id)
216 ib_destroy_cm_id(target->cm_id);
217 target->cm_id = new_cm_id;
218
219 return 0;
220}
221
Roland Dreieraef9ec32005-11-02 14:07:13 -0800222static int srp_create_target_ib(struct srp_target_port *target)
223{
224 struct ib_qp_init_attr *init_attr;
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100225 struct ib_cq *recv_cq, *send_cq;
226 struct ib_qp *qp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800227 int ret;
228
229 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
230 if (!init_attr)
231 return -ENOMEM;
232
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100233 recv_cq = ib_create_cq(target->srp_host->srp_dev->dev,
Bart Van Assche4b5e5f42013-06-28 14:57:42 +0200234 srp_recv_completion, NULL, target, SRP_RQ_SIZE,
235 target->comp_vector);
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100236 if (IS_ERR(recv_cq)) {
237 ret = PTR_ERR(recv_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800238 goto err;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800239 }
240
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100241 send_cq = ib_create_cq(target->srp_host->srp_dev->dev,
Bart Van Assche4b5e5f42013-06-28 14:57:42 +0200242 srp_send_completion, NULL, target, SRP_SQ_SIZE,
243 target->comp_vector);
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100244 if (IS_ERR(send_cq)) {
245 ret = PTR_ERR(send_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800246 goto err_recv_cq;
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000247 }
248
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100249 ib_req_notify_cq(recv_cq, IB_CQ_NEXT_COMP);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800250
251 init_attr->event_handler = srp_qp_event;
252 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
253 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
254 init_attr->cap.max_recv_sge = 1;
255 init_attr->cap.max_send_sge = 1;
256 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
257 init_attr->qp_type = IB_QPT_RC;
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100258 init_attr->send_cq = send_cq;
259 init_attr->recv_cq = recv_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800260
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100261 qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr);
262 if (IS_ERR(qp)) {
263 ret = PTR_ERR(qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800264 goto err_send_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800265 }
266
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100267 ret = srp_init_qp(target, qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800268 if (ret)
269 goto err_qp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800270
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100271 if (target->qp)
272 ib_destroy_qp(target->qp);
273 if (target->recv_cq)
274 ib_destroy_cq(target->recv_cq);
275 if (target->send_cq)
276 ib_destroy_cq(target->send_cq);
277
278 target->qp = qp;
279 target->recv_cq = recv_cq;
280 target->send_cq = send_cq;
281
Roland Dreierda9d2f02010-02-24 15:07:59 -0800282 kfree(init_attr);
283 return 0;
284
285err_qp:
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100286 ib_destroy_qp(qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800287
288err_send_cq:
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100289 ib_destroy_cq(send_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800290
291err_recv_cq:
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100292 ib_destroy_cq(recv_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800293
294err:
Roland Dreieraef9ec32005-11-02 14:07:13 -0800295 kfree(init_attr);
296 return ret;
297}
298
299static void srp_free_target_ib(struct srp_target_port *target)
300{
301 int i;
302
303 ib_destroy_qp(target->qp);
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000304 ib_destroy_cq(target->send_cq);
305 ib_destroy_cq(target->recv_cq);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800306
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100307 target->qp = NULL;
308 target->send_cq = target->recv_cq = NULL;
309
Roland Dreieraef9ec32005-11-02 14:07:13 -0800310 for (i = 0; i < SRP_RQ_SIZE; ++i)
311 srp_free_iu(target->srp_host, target->rx_ring[i]);
Bart Van Asschedd5e6e32010-08-30 19:27:20 +0000312 for (i = 0; i < SRP_SQ_SIZE; ++i)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800313 srp_free_iu(target->srp_host, target->tx_ring[i]);
314}
315
316static void srp_path_rec_completion(int status,
317 struct ib_sa_path_rec *pathrec,
318 void *target_ptr)
319{
320 struct srp_target_port *target = target_ptr;
321
322 target->status = status;
323 if (status)
David Dillow7aa54bd2008-01-07 18:23:41 -0500324 shost_printk(KERN_ERR, target->scsi_host,
325 PFX "Got failed path rec status %d\n", status);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800326 else
327 target->path = *pathrec;
328 complete(&target->done);
329}
330
331static int srp_lookup_path(struct srp_target_port *target)
332{
333 target->path.numb_path = 1;
334
335 init_completion(&target->done);
336
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700337 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100338 target->srp_host->srp_dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800339 target->srp_host->port,
340 &target->path,
Sean Hefty247e0202007-08-08 15:51:18 -0700341 IB_SA_PATH_REC_SERVICE_ID |
Roland Dreieraef9ec32005-11-02 14:07:13 -0800342 IB_SA_PATH_REC_DGID |
343 IB_SA_PATH_REC_SGID |
344 IB_SA_PATH_REC_NUMB_PATH |
345 IB_SA_PATH_REC_PKEY,
346 SRP_PATH_REC_TIMEOUT_MS,
347 GFP_KERNEL,
348 srp_path_rec_completion,
349 target, &target->path_query);
350 if (target->path_query_id < 0)
351 return target->path_query_id;
352
353 wait_for_completion(&target->done);
354
355 if (target->status < 0)
David Dillow7aa54bd2008-01-07 18:23:41 -0500356 shost_printk(KERN_WARNING, target->scsi_host,
357 PFX "Path record query failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800358
359 return target->status;
360}
361
362static int srp_send_req(struct srp_target_port *target)
363{
364 struct {
365 struct ib_cm_req_param param;
366 struct srp_login_req priv;
367 } *req = NULL;
368 int status;
369
370 req = kzalloc(sizeof *req, GFP_KERNEL);
371 if (!req)
372 return -ENOMEM;
373
374 req->param.primary_path = &target->path;
375 req->param.alternate_path = NULL;
376 req->param.service_id = target->service_id;
377 req->param.qp_num = target->qp->qp_num;
378 req->param.qp_type = target->qp->qp_type;
379 req->param.private_data = &req->priv;
380 req->param.private_data_len = sizeof req->priv;
381 req->param.flow_control = 1;
382
383 get_random_bytes(&req->param.starting_psn, 4);
384 req->param.starting_psn &= 0xffffff;
385
386 /*
387 * Pick some arbitrary defaults here; we could make these
388 * module parameters if anyone cared about setting them.
389 */
390 req->param.responder_resources = 4;
391 req->param.remote_cm_response_timeout = 20;
392 req->param.local_cm_response_timeout = 20;
Vu Pham7bb312e2013-10-26 14:31:27 +0200393 req->param.retry_count = target->tl_retry_count;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800394 req->param.rnr_retry_count = 7;
395 req->param.max_cm_retries = 15;
396
397 req->priv.opcode = SRP_LOGIN_REQ;
398 req->priv.tag = 0;
David Dillow49248642011-01-14 18:23:24 -0500399 req->priv.req_it_iu_len = cpu_to_be32(target->max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800400 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
401 SRP_BUF_FORMAT_INDIRECT);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700402 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700403 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450db2006-06-17 20:37:38 -0700404 * port identifier format is 8 bytes of ID extension followed
405 * by 8 bytes of GUID. Older drafts put the two halves in the
406 * opposite order, so that the GUID comes first.
407 *
408 * Targets conforming to these obsolete drafts can be
409 * recognized by the I/O Class they report.
410 */
411 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
412 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200413 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700414 memcpy(req->priv.initiator_port_id + 8,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200415 &target->initiator_ext, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700416 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
417 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
418 } else {
419 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200420 &target->initiator_ext, 8);
421 memcpy(req->priv.initiator_port_id + 8,
422 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700423 memcpy(req->priv.target_port_id, &target->id_ext, 8);
424 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
425 }
426
Roland Dreieraef9ec32005-11-02 14:07:13 -0800427 /*
428 * Topspin/Cisco SRP targets will reject our login unless we
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200429 * zero out the first 8 bytes of our initiator port ID and set
430 * the second 8 bytes to the local node GUID.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800431 */
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700432 if (srp_target_is_topspin(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500433 shost_printk(KERN_DEBUG, target->scsi_host,
434 PFX "Topspin/Cisco initiator port ID workaround "
435 "activated for target GUID %016llx\n",
436 (unsigned long long) be64_to_cpu(target->ioc_guid));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800437 memset(req->priv.initiator_port_id, 0, 8);
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200438 memcpy(req->priv.initiator_port_id + 8,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100439 &target->srp_host->srp_dev->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800440 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800441
442 status = ib_send_cm_req(target->cm_id, &req->param);
443
444 kfree(req);
445
446 return status;
447}
448
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000449static bool srp_queue_remove_work(struct srp_target_port *target)
450{
451 bool changed = false;
452
453 spin_lock_irq(&target->lock);
454 if (target->state != SRP_TARGET_REMOVED) {
455 target->state = SRP_TARGET_REMOVED;
456 changed = true;
457 }
458 spin_unlock_irq(&target->lock);
459
460 if (changed)
461 queue_work(system_long_wq, &target->remove_work);
462
463 return changed;
464}
465
Bart Van Assche294c8752011-12-25 12:18:12 +0000466static bool srp_change_conn_state(struct srp_target_port *target,
467 bool connected)
468{
469 bool changed = false;
470
471 spin_lock_irq(&target->lock);
472 if (target->connected != connected) {
473 target->connected = connected;
474 changed = true;
475 }
476 spin_unlock_irq(&target->lock);
477
478 return changed;
479}
480
Roland Dreieraef9ec32005-11-02 14:07:13 -0800481static void srp_disconnect_target(struct srp_target_port *target)
482{
Bart Van Assche294c8752011-12-25 12:18:12 +0000483 if (srp_change_conn_state(target, false)) {
484 /* XXX should send SRP_I_LOGOUT request */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800485
Bart Van Assche294c8752011-12-25 12:18:12 +0000486 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
487 shost_printk(KERN_DEBUG, target->scsi_host,
488 PFX "Sending CM DREQ failed\n");
Bart Van Assche294c8752011-12-25 12:18:12 +0000489 }
Roland Dreiere6581052006-05-17 09:13:21 -0700490 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800491}
492
David Dillow8f26c9f2011-01-14 19:45:50 -0500493static void srp_free_req_data(struct srp_target_port *target)
494{
David Dillowc07d4242011-01-16 13:57:10 -0500495 struct ib_device *ibdev = target->srp_host->srp_dev->dev;
David Dillow8f26c9f2011-01-14 19:45:50 -0500496 struct srp_request *req;
497 int i;
498
499 for (i = 0, req = target->req_ring; i < SRP_CMD_SQ_SIZE; ++i, ++req) {
500 kfree(req->fmr_list);
501 kfree(req->map_page);
David Dillowc07d4242011-01-16 13:57:10 -0500502 if (req->indirect_dma_addr) {
503 ib_dma_unmap_single(ibdev, req->indirect_dma_addr,
504 target->indirect_size,
505 DMA_TO_DEVICE);
506 }
507 kfree(req->indirect_desc);
David Dillow8f26c9f2011-01-14 19:45:50 -0500508 }
509}
510
Bart Van Assche683b1592012-01-14 12:40:44 +0000511/**
512 * srp_del_scsi_host_attr() - Remove attributes defined in the host template.
513 * @shost: SCSI host whose attributes to remove from sysfs.
514 *
515 * Note: Any attributes defined in the host template and that did not exist
516 * before invocation of this function will be ignored.
517 */
518static void srp_del_scsi_host_attr(struct Scsi_Host *shost)
519{
520 struct device_attribute **attr;
521
522 for (attr = shost->hostt->shost_attrs; attr && *attr; ++attr)
523 device_remove_file(&shost->shost_dev, *attr);
524}
525
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000526static void srp_remove_target(struct srp_target_port *target)
527{
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000528 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
529
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000530 srp_del_scsi_host_attr(target->scsi_host);
531 srp_remove_host(target->scsi_host);
532 scsi_remove_host(target->scsi_host);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000533 srp_disconnect_target(target);
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000534 ib_destroy_cm_id(target->cm_id);
535 srp_free_target_ib(target);
536 srp_free_req_data(target);
537 scsi_host_put(target->scsi_host);
538}
539
David Howellsc4028952006-11-22 14:57:56 +0000540static void srp_remove_work(struct work_struct *work)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800541{
David Howellsc4028952006-11-22 14:57:56 +0000542 struct srp_target_port *target =
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000543 container_of(work, struct srp_target_port, remove_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800544
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000545 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800546
Bart Van Assche96fc2482013-06-28 14:51:26 +0200547 srp_remove_target(target);
548
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700549 spin_lock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800550 list_del(&target->list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700551 spin_unlock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800552}
553
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +0200554static void srp_rport_delete(struct srp_rport *rport)
555{
556 struct srp_target_port *target = rport->lld_data;
557
558 srp_queue_remove_work(target);
559}
560
Roland Dreieraef9ec32005-11-02 14:07:13 -0800561static int srp_connect_target(struct srp_target_port *target)
562{
David Dillow9fe4bcf2008-01-08 17:08:52 -0500563 int retries = 3;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800564 int ret;
565
Bart Van Assche294c8752011-12-25 12:18:12 +0000566 WARN_ON_ONCE(target->connected);
567
Bart Van Assche948d1e82011-09-03 09:25:42 +0200568 target->qp_in_error = false;
569
Roland Dreieraef9ec32005-11-02 14:07:13 -0800570 ret = srp_lookup_path(target);
571 if (ret)
572 return ret;
573
574 while (1) {
575 init_completion(&target->done);
576 ret = srp_send_req(target);
577 if (ret)
578 return ret;
579 wait_for_completion(&target->done);
580
581 /*
582 * The CM event handling code will set status to
583 * SRP_PORT_REDIRECT if we get a port redirect REJ
584 * back, or SRP_DLID_REDIRECT if we get a lid/qp
585 * redirect REJ back.
586 */
587 switch (target->status) {
588 case 0:
Bart Van Assche294c8752011-12-25 12:18:12 +0000589 srp_change_conn_state(target, true);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800590 return 0;
591
592 case SRP_PORT_REDIRECT:
593 ret = srp_lookup_path(target);
594 if (ret)
595 return ret;
596 break;
597
598 case SRP_DLID_REDIRECT:
599 break;
600
David Dillow9fe4bcf2008-01-08 17:08:52 -0500601 case SRP_STALE_CONN:
602 /* Our current CM id was stale, and is now in timewait.
603 * Try to reconnect with a new one.
604 */
605 if (!retries-- || srp_new_cm_id(target)) {
606 shost_printk(KERN_ERR, target->scsi_host, PFX
607 "giving up on stale connection\n");
608 target->status = -ECONNRESET;
609 return target->status;
610 }
611
612 shost_printk(KERN_ERR, target->scsi_host, PFX
613 "retrying stale connection\n");
614 break;
615
Roland Dreieraef9ec32005-11-02 14:07:13 -0800616 default:
617 return target->status;
618 }
619 }
620}
621
Roland Dreierd945e1d2006-05-09 10:50:28 -0700622static void srp_unmap_data(struct scsi_cmnd *scmnd,
623 struct srp_target_port *target,
624 struct srp_request *req)
625{
David Dillow8f26c9f2011-01-14 19:45:50 -0500626 struct ib_device *ibdev = target->srp_host->srp_dev->dev;
627 struct ib_pool_fmr **pfmr;
628
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900629 if (!scsi_sglist(scmnd) ||
Roland Dreierd945e1d2006-05-09 10:50:28 -0700630 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
631 scmnd->sc_data_direction != DMA_FROM_DEVICE))
632 return;
633
David Dillow8f26c9f2011-01-14 19:45:50 -0500634 pfmr = req->fmr_list;
635 while (req->nfmr--)
636 ib_fmr_pool_unmap(*pfmr++);
Roland Dreierf5358a12006-06-17 20:37:29 -0700637
David Dillow8f26c9f2011-01-14 19:45:50 -0500638 ib_dma_unmap_sg(ibdev, scsi_sglist(scmnd), scsi_sg_count(scmnd),
639 scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700640}
641
Bart Van Assche22032992012-08-14 13:18:53 +0000642/**
643 * srp_claim_req - Take ownership of the scmnd associated with a request.
644 * @target: SRP target port.
645 * @req: SRP request.
646 * @scmnd: If NULL, take ownership of @req->scmnd. If not NULL, only take
647 * ownership of @req->scmnd if it equals @scmnd.
648 *
649 * Return value:
650 * Either NULL or a pointer to the SCSI command the caller became owner of.
651 */
652static struct scsi_cmnd *srp_claim_req(struct srp_target_port *target,
653 struct srp_request *req,
654 struct scsi_cmnd *scmnd)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700655{
Bart Van Assche94a91742010-11-26 14:50:09 -0500656 unsigned long flags;
657
Bart Van Assche22032992012-08-14 13:18:53 +0000658 spin_lock_irqsave(&target->lock, flags);
659 if (!scmnd) {
660 scmnd = req->scmnd;
661 req->scmnd = NULL;
662 } else if (req->scmnd == scmnd) {
663 req->scmnd = NULL;
664 } else {
665 scmnd = NULL;
666 }
667 spin_unlock_irqrestore(&target->lock, flags);
668
669 return scmnd;
670}
671
672/**
673 * srp_free_req() - Unmap data and add request to the free request list.
674 */
675static void srp_free_req(struct srp_target_port *target,
676 struct srp_request *req, struct scsi_cmnd *scmnd,
677 s32 req_lim_delta)
678{
679 unsigned long flags;
680
681 srp_unmap_data(scmnd, target, req);
682
Bart Van Asschee9684672010-11-26 15:08:38 -0500683 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -0500684 target->req_lim += req_lim_delta;
Bart Van Assche536ae142010-11-26 13:58:27 -0500685 list_add_tail(&req->list, &target->free_reqs);
Bart Van Asschee9684672010-11-26 15:08:38 -0500686 spin_unlock_irqrestore(&target->lock, flags);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700687}
688
689static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
690{
Bart Van Assche22032992012-08-14 13:18:53 +0000691 struct scsi_cmnd *scmnd = srp_claim_req(target, req, NULL);
692
693 if (scmnd) {
Bart Van Assche9b796d02012-08-24 10:27:54 +0000694 srp_free_req(target, req, scmnd, 0);
Bart Van Assche22032992012-08-14 13:18:53 +0000695 scmnd->result = DID_RESET << 16;
696 scmnd->scsi_done(scmnd);
Bart Van Assche22032992012-08-14 13:18:53 +0000697 }
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700698}
699
Roland Dreieraef9ec32005-11-02 14:07:13 -0800700static int srp_reconnect_target(struct srp_target_port *target)
701{
Bart Van Assche09be70a2012-03-17 17:18:54 +0000702 struct Scsi_Host *shost = target->scsi_host;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -0500703 int i, ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800704
Bart Van Assche09be70a2012-03-17 17:18:54 +0000705 scsi_target_block(&shost->shost_gendev);
706
Roland Dreieraef9ec32005-11-02 14:07:13 -0800707 srp_disconnect_target(target);
708 /*
Bart Van Asschec7c4e7f2013-02-21 17:19:04 +0000709 * Now get a new local CM ID so that we avoid confusing the target in
710 * case things are really fouled up. Doing so also ensures that all CM
711 * callbacks will have finished before a new QP is allocated.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800712 */
David Dillow9fe4bcf2008-01-08 17:08:52 -0500713 ret = srp_new_cm_id(target);
Bart Van Asschec7c4e7f2013-02-21 17:19:04 +0000714 /*
715 * Whether or not creating a new CM ID succeeded, create a new
716 * QP. This guarantees that all completion callback function
717 * invocations have finished before request resetting starts.
718 */
719 if (ret == 0)
720 ret = srp_create_target_ib(target);
721 else
722 srp_create_target_ib(target);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800723
Bart Van Assche536ae142010-11-26 13:58:27 -0500724 for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
725 struct srp_request *req = &target->req_ring[i];
726 if (req->scmnd)
727 srp_reset_req(target, req);
728 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800729
Bart Van Assche536ae142010-11-26 13:58:27 -0500730 INIT_LIST_HEAD(&target->free_tx);
Bart Van Asschedcb4cb82010-11-26 13:22:48 -0500731 for (i = 0; i < SRP_SQ_SIZE; ++i)
Bart Van Assche536ae142010-11-26 13:58:27 -0500732 list_add(&target->tx_ring[i]->list, &target->free_tx);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800733
Bart Van Asschec7c4e7f2013-02-21 17:19:04 +0000734 if (ret == 0)
735 ret = srp_connect_target(target);
Bart Van Assche09be70a2012-03-17 17:18:54 +0000736
Bart Van Assche09be70a2012-03-17 17:18:54 +0000737 scsi_target_unblock(&shost->shost_gendev, ret == 0 ? SDEV_RUNNING :
738 SDEV_TRANSPORT_OFFLINE);
Bart Van Assche2ce19e72013-02-21 17:20:00 +0000739 target->transport_offline = !!ret;
Bart Van Assche09be70a2012-03-17 17:18:54 +0000740
Roland Dreieraef9ec32005-11-02 14:07:13 -0800741 if (ret)
742 goto err;
743
Bart Van Assche09be70a2012-03-17 17:18:54 +0000744 shost_printk(KERN_INFO, target->scsi_host, PFX "reconnect succeeded\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800745
746 return ret;
747
748err:
David Dillow7aa54bd2008-01-07 18:23:41 -0500749 shost_printk(KERN_ERR, target->scsi_host,
750 PFX "reconnect failed (%d), removing target port.\n", ret);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800751
752 /*
753 * We couldn't reconnect, so kill our target port off.
Bart Van Assche9709f0e2010-11-26 13:13:06 -0500754 * However, we have to defer the real removal because we
755 * are in the context of the SCSI error handler now, which
756 * will deadlock if we call scsi_remove_host().
Roland Dreieraef9ec32005-11-02 14:07:13 -0800757 */
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000758 srp_queue_remove_work(target);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800759
760 return ret;
761}
762
David Dillow8f26c9f2011-01-14 19:45:50 -0500763static void srp_map_desc(struct srp_map_state *state, dma_addr_t dma_addr,
764 unsigned int dma_len, u32 rkey)
Roland Dreierf5358a12006-06-17 20:37:29 -0700765{
David Dillow8f26c9f2011-01-14 19:45:50 -0500766 struct srp_direct_buf *desc = state->desc;
767
768 desc->va = cpu_to_be64(dma_addr);
769 desc->key = cpu_to_be32(rkey);
770 desc->len = cpu_to_be32(dma_len);
771
772 state->total_len += dma_len;
773 state->desc++;
774 state->ndesc++;
775}
776
777static int srp_map_finish_fmr(struct srp_map_state *state,
778 struct srp_target_port *target)
779{
780 struct srp_device *dev = target->srp_host->srp_dev;
781 struct ib_pool_fmr *fmr;
Roland Dreierf5358a12006-06-17 20:37:29 -0700782 u64 io_addr = 0;
David Dillow8f26c9f2011-01-14 19:45:50 -0500783
784 if (!state->npages)
785 return 0;
786
787 if (state->npages == 1) {
788 srp_map_desc(state, state->base_dma_addr, state->fmr_len,
789 target->rkey);
790 state->npages = state->fmr_len = 0;
791 return 0;
792 }
793
794 fmr = ib_fmr_pool_map_phys(dev->fmr_pool, state->pages,
795 state->npages, io_addr);
796 if (IS_ERR(fmr))
797 return PTR_ERR(fmr);
798
799 *state->next_fmr++ = fmr;
800 state->nfmr++;
801
802 srp_map_desc(state, 0, state->fmr_len, fmr->fmr->rkey);
803 state->npages = state->fmr_len = 0;
804 return 0;
805}
806
807static void srp_map_update_start(struct srp_map_state *state,
808 struct scatterlist *sg, int sg_index,
809 dma_addr_t dma_addr)
810{
811 state->unmapped_sg = sg;
812 state->unmapped_index = sg_index;
813 state->unmapped_addr = dma_addr;
814}
815
816static int srp_map_sg_entry(struct srp_map_state *state,
817 struct srp_target_port *target,
818 struct scatterlist *sg, int sg_index,
819 int use_fmr)
820{
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100821 struct srp_device *dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800822 struct ib_device *ibdev = dev->dev;
David Dillow8f26c9f2011-01-14 19:45:50 -0500823 dma_addr_t dma_addr = ib_sg_dma_address(ibdev, sg);
824 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
825 unsigned int len;
826 int ret;
Roland Dreierf5358a12006-06-17 20:37:29 -0700827
David Dillow8f26c9f2011-01-14 19:45:50 -0500828 if (!dma_len)
829 return 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700830
David Dillow8f26c9f2011-01-14 19:45:50 -0500831 if (use_fmr == SRP_MAP_NO_FMR) {
832 /* Once we're in direct map mode for a request, we don't
833 * go back to FMR mode, so no need to update anything
834 * other than the descriptor.
835 */
836 srp_map_desc(state, dma_addr, dma_len, target->rkey);
837 return 0;
838 }
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700839
David Dillow8f26c9f2011-01-14 19:45:50 -0500840 /* If we start at an offset into the FMR page, don't merge into
841 * the current FMR. Finish it out, and use the kernel's MR for this
842 * sg entry. This is to avoid potential bugs on some SRP targets
843 * that were never quite defined, but went away when the initiator
844 * avoided using FMR on such page fragments.
845 */
846 if (dma_addr & ~dev->fmr_page_mask || dma_len > dev->fmr_max_size) {
847 ret = srp_map_finish_fmr(state, target);
848 if (ret)
849 return ret;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800850
David Dillow8f26c9f2011-01-14 19:45:50 -0500851 srp_map_desc(state, dma_addr, dma_len, target->rkey);
852 srp_map_update_start(state, NULL, 0, 0);
853 return 0;
854 }
855
856 /* If this is the first sg to go into the FMR, save our position.
857 * We need to know the first unmapped entry, its index, and the
858 * first unmapped address within that entry to be able to restart
859 * mapping after an error.
860 */
861 if (!state->unmapped_sg)
862 srp_map_update_start(state, sg, sg_index, dma_addr);
863
864 while (dma_len) {
865 if (state->npages == SRP_FMR_SIZE) {
866 ret = srp_map_finish_fmr(state, target);
867 if (ret)
868 return ret;
869
870 srp_map_update_start(state, sg, sg_index, dma_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -0700871 }
872
David Dillow8f26c9f2011-01-14 19:45:50 -0500873 len = min_t(unsigned int, dma_len, dev->fmr_page_size);
874
875 if (!state->npages)
876 state->base_dma_addr = dma_addr;
877 state->pages[state->npages++] = dma_addr;
878 state->fmr_len += len;
879 dma_addr += len;
880 dma_len -= len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700881 }
882
David Dillow8f26c9f2011-01-14 19:45:50 -0500883 /* If the last entry of the FMR wasn't a full page, then we need to
884 * close it out and start a new one -- we can only merge at page
885 * boundries.
886 */
Roland Dreierf5358a12006-06-17 20:37:29 -0700887 ret = 0;
David Dillow8f26c9f2011-01-14 19:45:50 -0500888 if (len != dev->fmr_page_size) {
889 ret = srp_map_finish_fmr(state, target);
890 if (!ret)
891 srp_map_update_start(state, NULL, 0, 0);
892 }
Roland Dreierf5358a12006-06-17 20:37:29 -0700893 return ret;
894}
895
Roland Dreieraef9ec32005-11-02 14:07:13 -0800896static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
897 struct srp_request *req)
898{
David Dillow8f26c9f2011-01-14 19:45:50 -0500899 struct scatterlist *scat, *sg;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800900 struct srp_cmd *cmd = req->cmd->buf;
David Dillow8f26c9f2011-01-14 19:45:50 -0500901 int i, len, nents, count, use_fmr;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800902 struct srp_device *dev;
903 struct ib_device *ibdev;
David Dillow8f26c9f2011-01-14 19:45:50 -0500904 struct srp_map_state state;
905 struct srp_indirect_buf *indirect_hdr;
David Dillow8f26c9f2011-01-14 19:45:50 -0500906 u32 table_len;
907 u8 fmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800908
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900909 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800910 return sizeof (struct srp_cmd);
911
912 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
913 scmnd->sc_data_direction != DMA_TO_DEVICE) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500914 shost_printk(KERN_WARNING, target->scsi_host,
915 PFX "Unhandled data direction %d\n",
916 scmnd->sc_data_direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800917 return -EINVAL;
918 }
919
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900920 nents = scsi_sg_count(scmnd);
921 scat = scsi_sglist(scmnd);
Roland Dreiercf368712006-03-24 15:47:26 -0800922
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100923 dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800924 ibdev = dev->dev;
925
926 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
David Dillow8f26c9f2011-01-14 19:45:50 -0500927 if (unlikely(count == 0))
928 return -EIO;
Roland Dreierf5358a12006-06-17 20:37:29 -0700929
930 fmt = SRP_DATA_DESC_DIRECT;
931 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800932
933 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700934 /*
935 * The midlayer only generated a single gather/scatter
936 * entry, or DMA mapping coalesced everything to a
937 * single entry. So a direct descriptor along with
938 * the DMA MR suffices.
939 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800940 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800941
Ralph Campbell85507bc2006-12-12 14:30:55 -0800942 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
David Dillow9af76272010-11-26 15:34:46 -0500943 buf->key = cpu_to_be32(target->rkey);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800944 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Roland Dreiercf368712006-03-24 15:47:26 -0800945
David Dillow8f26c9f2011-01-14 19:45:50 -0500946 req->nfmr = 0;
947 goto map_complete;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800948 }
949
David Dillow8f26c9f2011-01-14 19:45:50 -0500950 /* We have more than one scatter/gather entry, so build our indirect
951 * descriptor table, trying to merge as many entries with FMR as we
952 * can.
953 */
954 indirect_hdr = (void *) cmd->add_data;
955
David Dillowc07d4242011-01-16 13:57:10 -0500956 ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr,
957 target->indirect_size, DMA_TO_DEVICE);
958
David Dillow8f26c9f2011-01-14 19:45:50 -0500959 memset(&state, 0, sizeof(state));
David Dillowc07d4242011-01-16 13:57:10 -0500960 state.desc = req->indirect_desc;
David Dillow8f26c9f2011-01-14 19:45:50 -0500961 state.pages = req->map_page;
962 state.next_fmr = req->fmr_list;
963
964 use_fmr = dev->fmr_pool ? SRP_MAP_ALLOW_FMR : SRP_MAP_NO_FMR;
965
966 for_each_sg(scat, sg, count, i) {
967 if (srp_map_sg_entry(&state, target, sg, i, use_fmr)) {
968 /* FMR mapping failed, so backtrack to the first
969 * unmapped entry and continue on without using FMR.
970 */
971 dma_addr_t dma_addr;
972 unsigned int dma_len;
973
974backtrack:
975 sg = state.unmapped_sg;
976 i = state.unmapped_index;
977
978 dma_addr = ib_sg_dma_address(ibdev, sg);
979 dma_len = ib_sg_dma_len(ibdev, sg);
980 dma_len -= (state.unmapped_addr - dma_addr);
981 dma_addr = state.unmapped_addr;
982 use_fmr = SRP_MAP_NO_FMR;
983 srp_map_desc(&state, dma_addr, dma_len, target->rkey);
984 }
985 }
986
987 if (use_fmr == SRP_MAP_ALLOW_FMR && srp_map_finish_fmr(&state, target))
988 goto backtrack;
989
David Dillowc07d4242011-01-16 13:57:10 -0500990 /* We've mapped the request, now pull as much of the indirect
991 * descriptor table as we can into the command buffer. If this
992 * target is not using an external indirect table, we are
993 * guaranteed to fit into the command, as the SCSI layer won't
994 * give us more S/G entries than we allow.
David Dillow8f26c9f2011-01-14 19:45:50 -0500995 */
996 req->nfmr = state.nfmr;
997 if (state.ndesc == 1) {
998 /* FMR mapping was able to collapse this to one entry,
999 * so use a direct descriptor.
1000 */
1001 struct srp_direct_buf *buf = (void *) cmd->add_data;
1002
David Dillowc07d4242011-01-16 13:57:10 -05001003 *buf = req->indirect_desc[0];
David Dillow8f26c9f2011-01-14 19:45:50 -05001004 goto map_complete;
1005 }
1006
David Dillowc07d4242011-01-16 13:57:10 -05001007 if (unlikely(target->cmd_sg_cnt < state.ndesc &&
1008 !target->allow_ext_sg)) {
1009 shost_printk(KERN_ERR, target->scsi_host,
1010 "Could not fit S/G list into SRP_CMD\n");
1011 return -EIO;
1012 }
1013
1014 count = min(state.ndesc, target->cmd_sg_cnt);
David Dillow8f26c9f2011-01-14 19:45:50 -05001015 table_len = state.ndesc * sizeof (struct srp_direct_buf);
1016
1017 fmt = SRP_DATA_DESC_INDIRECT;
1018 len = sizeof(struct srp_cmd) + sizeof (struct srp_indirect_buf);
David Dillowc07d4242011-01-16 13:57:10 -05001019 len += count * sizeof (struct srp_direct_buf);
David Dillow8f26c9f2011-01-14 19:45:50 -05001020
David Dillowc07d4242011-01-16 13:57:10 -05001021 memcpy(indirect_hdr->desc_list, req->indirect_desc,
1022 count * sizeof (struct srp_direct_buf));
David Dillow8f26c9f2011-01-14 19:45:50 -05001023
David Dillowc07d4242011-01-16 13:57:10 -05001024 indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr);
David Dillow8f26c9f2011-01-14 19:45:50 -05001025 indirect_hdr->table_desc.key = cpu_to_be32(target->rkey);
1026 indirect_hdr->table_desc.len = cpu_to_be32(table_len);
1027 indirect_hdr->len = cpu_to_be32(state.total_len);
1028
1029 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
David Dillowc07d4242011-01-16 13:57:10 -05001030 cmd->data_out_desc_cnt = count;
David Dillow8f26c9f2011-01-14 19:45:50 -05001031 else
David Dillowc07d4242011-01-16 13:57:10 -05001032 cmd->data_in_desc_cnt = count;
1033
1034 ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len,
1035 DMA_TO_DEVICE);
David Dillow8f26c9f2011-01-14 19:45:50 -05001036
1037map_complete:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001038 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
1039 cmd->buf_fmt = fmt << 4;
1040 else
1041 cmd->buf_fmt = fmt;
1042
Roland Dreieraef9ec32005-11-02 14:07:13 -08001043 return len;
1044}
1045
David Dillow05a1d752010-10-08 14:48:14 -04001046/*
Bart Van Assche76c75b22010-11-26 14:37:47 -05001047 * Return an IU and possible credit to the free pool
1048 */
1049static void srp_put_tx_iu(struct srp_target_port *target, struct srp_iu *iu,
1050 enum srp_iu_type iu_type)
1051{
1052 unsigned long flags;
1053
Bart Van Asschee9684672010-11-26 15:08:38 -05001054 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001055 list_add(&iu->list, &target->free_tx);
1056 if (iu_type != SRP_IU_RSP)
1057 ++target->req_lim;
Bart Van Asschee9684672010-11-26 15:08:38 -05001058 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001059}
1060
1061/*
Bart Van Asschee9684672010-11-26 15:08:38 -05001062 * Must be called with target->lock held to protect req_lim and free_tx.
1063 * If IU is not sent, it must be returned using srp_put_tx_iu().
David Dillow05a1d752010-10-08 14:48:14 -04001064 *
1065 * Note:
1066 * An upper limit for the number of allocated information units for each
1067 * request type is:
1068 * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues
1069 * more than Scsi_Host.can_queue requests.
1070 * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE.
1071 * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than
1072 * one unanswered SRP request to an initiator.
1073 */
1074static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
1075 enum srp_iu_type iu_type)
1076{
1077 s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE;
1078 struct srp_iu *iu;
1079
1080 srp_send_completion(target->send_cq, target);
1081
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001082 if (list_empty(&target->free_tx))
David Dillow05a1d752010-10-08 14:48:14 -04001083 return NULL;
1084
1085 /* Initiator responses to target requests do not consume credits */
Bart Van Assche76c75b22010-11-26 14:37:47 -05001086 if (iu_type != SRP_IU_RSP) {
1087 if (target->req_lim <= rsv) {
1088 ++target->zero_req_lim;
1089 return NULL;
1090 }
1091
1092 --target->req_lim;
David Dillow05a1d752010-10-08 14:48:14 -04001093 }
1094
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001095 iu = list_first_entry(&target->free_tx, struct srp_iu, list);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001096 list_del(&iu->list);
David Dillow05a1d752010-10-08 14:48:14 -04001097 return iu;
1098}
1099
Bart Van Assche76c75b22010-11-26 14:37:47 -05001100static int srp_post_send(struct srp_target_port *target,
1101 struct srp_iu *iu, int len)
David Dillow05a1d752010-10-08 14:48:14 -04001102{
1103 struct ib_sge list;
1104 struct ib_send_wr wr, *bad_wr;
David Dillow05a1d752010-10-08 14:48:14 -04001105
1106 list.addr = iu->dma;
1107 list.length = len;
David Dillow9af76272010-11-26 15:34:46 -05001108 list.lkey = target->lkey;
David Dillow05a1d752010-10-08 14:48:14 -04001109
1110 wr.next = NULL;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001111 wr.wr_id = (uintptr_t) iu;
David Dillow05a1d752010-10-08 14:48:14 -04001112 wr.sg_list = &list;
1113 wr.num_sge = 1;
1114 wr.opcode = IB_WR_SEND;
1115 wr.send_flags = IB_SEND_SIGNALED;
1116
Bart Van Assche76c75b22010-11-26 14:37:47 -05001117 return ib_post_send(target->qp, &wr, &bad_wr);
David Dillow05a1d752010-10-08 14:48:14 -04001118}
1119
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001120static int srp_post_recv(struct srp_target_port *target, struct srp_iu *iu)
Bart Van Asschec996bb42010-07-30 10:59:05 +00001121{
Bart Van Asschec996bb42010-07-30 10:59:05 +00001122 struct ib_recv_wr wr, *bad_wr;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001123 struct ib_sge list;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001124
1125 list.addr = iu->dma;
1126 list.length = iu->size;
David Dillow9af76272010-11-26 15:34:46 -05001127 list.lkey = target->lkey;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001128
1129 wr.next = NULL;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001130 wr.wr_id = (uintptr_t) iu;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001131 wr.sg_list = &list;
1132 wr.num_sge = 1;
1133
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001134 return ib_post_recv(target->qp, &wr, &bad_wr);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001135}
1136
Roland Dreieraef9ec32005-11-02 14:07:13 -08001137static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
1138{
1139 struct srp_request *req;
1140 struct scsi_cmnd *scmnd;
1141 unsigned long flags;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001142
Roland Dreieraef9ec32005-11-02 14:07:13 -08001143 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
Bart Van Asschee9684672010-11-26 15:08:38 -05001144 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -05001145 target->req_lim += be32_to_cpu(rsp->req_lim_delta);
Bart Van Asschee9684672010-11-26 15:08:38 -05001146 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -05001147
David Dillowf8b6e312010-11-26 13:02:21 -05001148 target->tsk_mgmt_status = -1;
1149 if (be32_to_cpu(rsp->resp_data_len) >= 4)
1150 target->tsk_mgmt_status = rsp->data[3];
1151 complete(&target->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001152 } else {
David Dillowf8b6e312010-11-26 13:02:21 -05001153 req = &target->req_ring[rsp->tag];
Bart Van Assche22032992012-08-14 13:18:53 +00001154 scmnd = srp_claim_req(target, req, NULL);
1155 if (!scmnd) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001156 shost_printk(KERN_ERR, target->scsi_host,
1157 "Null scmnd for RSP w/tag %016llx\n",
1158 (unsigned long long) rsp->tag);
Bart Van Assche22032992012-08-14 13:18:53 +00001159
1160 spin_lock_irqsave(&target->lock, flags);
1161 target->req_lim += be32_to_cpu(rsp->req_lim_delta);
1162 spin_unlock_irqrestore(&target->lock, flags);
1163
1164 return;
1165 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001166 scmnd->result = rsp->status;
1167
1168 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
1169 memcpy(scmnd->sense_buffer, rsp->data +
1170 be32_to_cpu(rsp->resp_data_len),
1171 min_t(int, be32_to_cpu(rsp->sense_data_len),
1172 SCSI_SENSE_BUFFERSIZE));
1173 }
1174
1175 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001176 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001177 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001178 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001179
Bart Van Assche22032992012-08-14 13:18:53 +00001180 srp_free_req(target, req, scmnd,
1181 be32_to_cpu(rsp->req_lim_delta));
1182
David Dillowf8b6e312010-11-26 13:02:21 -05001183 scmnd->host_scribble = NULL;
1184 scmnd->scsi_done(scmnd);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001185 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001186}
1187
David Dillowbb125882010-10-08 14:40:47 -04001188static int srp_response_common(struct srp_target_port *target, s32 req_delta,
1189 void *rsp, int len)
1190{
Bart Van Assche76c75b22010-11-26 14:37:47 -05001191 struct ib_device *dev = target->srp_host->srp_dev->dev;
David Dillowbb125882010-10-08 14:40:47 -04001192 unsigned long flags;
1193 struct srp_iu *iu;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001194 int err;
David Dillowbb125882010-10-08 14:40:47 -04001195
Bart Van Asschee9684672010-11-26 15:08:38 -05001196 spin_lock_irqsave(&target->lock, flags);
David Dillowbb125882010-10-08 14:40:47 -04001197 target->req_lim += req_delta;
David Dillowbb125882010-10-08 14:40:47 -04001198 iu = __srp_get_tx_iu(target, SRP_IU_RSP);
Bart Van Asschee9684672010-11-26 15:08:38 -05001199 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001200
David Dillowbb125882010-10-08 14:40:47 -04001201 if (!iu) {
1202 shost_printk(KERN_ERR, target->scsi_host, PFX
1203 "no IU available to send response\n");
Bart Van Assche76c75b22010-11-26 14:37:47 -05001204 return 1;
David Dillowbb125882010-10-08 14:40:47 -04001205 }
1206
1207 ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE);
1208 memcpy(iu->buf, rsp, len);
1209 ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE);
1210
Bart Van Assche76c75b22010-11-26 14:37:47 -05001211 err = srp_post_send(target, iu, len);
1212 if (err) {
David Dillowbb125882010-10-08 14:40:47 -04001213 shost_printk(KERN_ERR, target->scsi_host, PFX
1214 "unable to post response: %d\n", err);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001215 srp_put_tx_iu(target, iu, SRP_IU_RSP);
1216 }
David Dillowbb125882010-10-08 14:40:47 -04001217
David Dillowbb125882010-10-08 14:40:47 -04001218 return err;
1219}
1220
1221static void srp_process_cred_req(struct srp_target_port *target,
1222 struct srp_cred_req *req)
1223{
1224 struct srp_cred_rsp rsp = {
1225 .opcode = SRP_CRED_RSP,
1226 .tag = req->tag,
1227 };
1228 s32 delta = be32_to_cpu(req->req_lim_delta);
1229
1230 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1231 shost_printk(KERN_ERR, target->scsi_host, PFX
1232 "problems processing SRP_CRED_REQ\n");
1233}
1234
1235static void srp_process_aer_req(struct srp_target_port *target,
1236 struct srp_aer_req *req)
1237{
1238 struct srp_aer_rsp rsp = {
1239 .opcode = SRP_AER_RSP,
1240 .tag = req->tag,
1241 };
1242 s32 delta = be32_to_cpu(req->req_lim_delta);
1243
1244 shost_printk(KERN_ERR, target->scsi_host, PFX
1245 "ignoring AER for LUN %llu\n", be64_to_cpu(req->lun));
1246
1247 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1248 shost_printk(KERN_ERR, target->scsi_host, PFX
1249 "problems processing SRP_AER_REQ\n");
1250}
1251
Roland Dreieraef9ec32005-11-02 14:07:13 -08001252static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
1253{
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001254 struct ib_device *dev = target->srp_host->srp_dev->dev;
Roland Dreier737b94e2011-05-23 11:30:04 -07001255 struct srp_iu *iu = (struct srp_iu *) (uintptr_t) wc->wr_id;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001256 int res;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001257 u8 opcode;
1258
Ralph Campbell85507bc2006-12-12 14:30:55 -08001259 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
1260 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001261
1262 opcode = *(u8 *) iu->buf;
1263
1264 if (0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001265 shost_printk(KERN_ERR, target->scsi_host,
1266 PFX "recv completion, opcode 0x%02x\n", opcode);
Bart Van Assche7a700812010-07-29 15:56:37 +00001267 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1,
1268 iu->buf, wc->byte_len, true);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001269 }
1270
1271 switch (opcode) {
1272 case SRP_RSP:
1273 srp_process_rsp(target, iu->buf);
1274 break;
1275
David Dillowbb125882010-10-08 14:40:47 -04001276 case SRP_CRED_REQ:
1277 srp_process_cred_req(target, iu->buf);
1278 break;
1279
1280 case SRP_AER_REQ:
1281 srp_process_aer_req(target, iu->buf);
1282 break;
1283
Roland Dreieraef9ec32005-11-02 14:07:13 -08001284 case SRP_T_LOGOUT:
1285 /* XXX Handle target logout */
David Dillow7aa54bd2008-01-07 18:23:41 -05001286 shost_printk(KERN_WARNING, target->scsi_host,
1287 PFX "Got target logout request\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001288 break;
1289
1290 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001291 shost_printk(KERN_WARNING, target->scsi_host,
1292 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001293 break;
1294 }
1295
Ralph Campbell85507bc2006-12-12 14:30:55 -08001296 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
1297 DMA_FROM_DEVICE);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001298
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001299 res = srp_post_recv(target, iu);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001300 if (res != 0)
1301 shost_printk(KERN_ERR, target->scsi_host,
1302 PFX "Recv failed with error code %d\n", res);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001303}
1304
Bart Van Assche948d1e82011-09-03 09:25:42 +02001305static void srp_handle_qp_err(enum ib_wc_status wc_status,
1306 enum ib_wc_opcode wc_opcode,
1307 struct srp_target_port *target)
1308{
Bart Van Assche294c8752011-12-25 12:18:12 +00001309 if (target->connected && !target->qp_in_error) {
Bart Van Assche4f0af692012-11-26 11:16:40 +01001310 shost_printk(KERN_ERR, target->scsi_host,
1311 PFX "failed %s status %d\n",
1312 wc_opcode & IB_WC_RECV ? "receive" : "send",
1313 wc_status);
1314 }
Bart Van Assche948d1e82011-09-03 09:25:42 +02001315 target->qp_in_error = true;
1316}
1317
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001318static void srp_recv_completion(struct ib_cq *cq, void *target_ptr)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001319{
1320 struct srp_target_port *target = target_ptr;
1321 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001322
1323 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1324 while (ib_poll_cq(cq, 1, &wc) > 0) {
Bart Van Assche948d1e82011-09-03 09:25:42 +02001325 if (likely(wc.status == IB_WC_SUCCESS)) {
1326 srp_handle_recv(target, &wc);
1327 } else {
1328 srp_handle_qp_err(wc.status, wc.opcode, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001329 }
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001330 }
1331}
1332
1333static void srp_send_completion(struct ib_cq *cq, void *target_ptr)
1334{
1335 struct srp_target_port *target = target_ptr;
1336 struct ib_wc wc;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001337 struct srp_iu *iu;
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001338
1339 while (ib_poll_cq(cq, 1, &wc) > 0) {
Bart Van Assche948d1e82011-09-03 09:25:42 +02001340 if (likely(wc.status == IB_WC_SUCCESS)) {
1341 iu = (struct srp_iu *) (uintptr_t) wc.wr_id;
1342 list_add(&iu->list, &target->free_tx);
1343 } else {
1344 srp_handle_qp_err(wc.status, wc.opcode, target);
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001345 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001346 }
1347}
1348
Bart Van Assche76c75b22010-11-26 14:37:47 -05001349static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001350{
Bart Van Assche76c75b22010-11-26 14:37:47 -05001351 struct srp_target_port *target = host_to_target(shost);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001352 struct srp_request *req;
1353 struct srp_iu *iu;
1354 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001355 struct ib_device *dev;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001356 unsigned long flags;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001357 int len;
1358
Bart Van Assche2ce19e72013-02-21 17:20:00 +00001359 if (unlikely(target->transport_offline)) {
1360 scmnd->result = DID_NO_CONNECT << 16;
1361 scmnd->scsi_done(scmnd);
1362 return 0;
1363 }
1364
Bart Van Asschee9684672010-11-26 15:08:38 -05001365 spin_lock_irqsave(&target->lock, flags);
David Dillowbb125882010-10-08 14:40:47 -04001366 iu = __srp_get_tx_iu(target, SRP_IU_CMD);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001367 if (!iu)
Bart Van Assche695b8342011-01-13 19:02:25 +00001368 goto err_unlock;
1369
1370 req = list_first_entry(&target->free_reqs, struct srp_request, list);
1371 list_del(&req->list);
1372 spin_unlock_irqrestore(&target->lock, flags);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001373
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001374 dev = target->srp_host->srp_dev->dev;
David Dillow49248642011-01-14 18:23:24 -05001375 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08001376 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001377
Roland Dreieraef9ec32005-11-02 14:07:13 -08001378 scmnd->result = 0;
David Dillowf8b6e312010-11-26 13:02:21 -05001379 scmnd->host_scribble = (void *) req;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001380
1381 cmd = iu->buf;
1382 memset(cmd, 0, sizeof *cmd);
1383
1384 cmd->opcode = SRP_CMD;
1385 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001386 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001387 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1388
Roland Dreieraef9ec32005-11-02 14:07:13 -08001389 req->scmnd = scmnd;
1390 req->cmd = iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001391
1392 len = srp_map_data(scmnd, target, req);
1393 if (len < 0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001394 shost_printk(KERN_ERR, target->scsi_host,
1395 PFX "Failed to map data\n");
Bart Van Assche76c75b22010-11-26 14:37:47 -05001396 goto err_iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001397 }
1398
David Dillow49248642011-01-14 18:23:24 -05001399 ib_dma_sync_single_for_device(dev, iu->dma, target->max_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08001400 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001401
Bart Van Assche76c75b22010-11-26 14:37:47 -05001402 if (srp_post_send(target, iu, len)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001403 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001404 goto err_unmap;
1405 }
1406
Roland Dreieraef9ec32005-11-02 14:07:13 -08001407 return 0;
1408
1409err_unmap:
1410 srp_unmap_data(scmnd, target, req);
1411
Bart Van Assche76c75b22010-11-26 14:37:47 -05001412err_iu:
1413 srp_put_tx_iu(target, iu, SRP_IU_CMD);
1414
Bart Van Asschee9684672010-11-26 15:08:38 -05001415 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001416 list_add(&req->list, &target->free_reqs);
Bart Van Assche695b8342011-01-13 19:02:25 +00001417
1418err_unlock:
Bart Van Asschee9684672010-11-26 15:08:38 -05001419 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001420
Roland Dreieraef9ec32005-11-02 14:07:13 -08001421 return SCSI_MLQUEUE_HOST_BUSY;
1422}
1423
1424static int srp_alloc_iu_bufs(struct srp_target_port *target)
1425{
1426 int i;
1427
1428 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1429 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1430 target->max_ti_iu_len,
1431 GFP_KERNEL, DMA_FROM_DEVICE);
1432 if (!target->rx_ring[i])
1433 goto err;
1434 }
1435
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001436 for (i = 0; i < SRP_SQ_SIZE; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001437 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
David Dillow49248642011-01-14 18:23:24 -05001438 target->max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001439 GFP_KERNEL, DMA_TO_DEVICE);
1440 if (!target->tx_ring[i])
1441 goto err;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001442
1443 list_add(&target->tx_ring[i]->list, &target->free_tx);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001444 }
1445
1446 return 0;
1447
1448err:
1449 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1450 srp_free_iu(target->srp_host, target->rx_ring[i]);
1451 target->rx_ring[i] = NULL;
1452 }
1453
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001454 for (i = 0; i < SRP_SQ_SIZE; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001455 srp_free_iu(target->srp_host, target->tx_ring[i]);
1456 target->tx_ring[i] = NULL;
1457 }
1458
1459 return -ENOMEM;
1460}
1461
Bart Van Asschec9b03c12011-09-03 09:34:48 +02001462static uint32_t srp_compute_rq_tmo(struct ib_qp_attr *qp_attr, int attr_mask)
1463{
1464 uint64_t T_tr_ns, max_compl_time_ms;
1465 uint32_t rq_tmo_jiffies;
1466
1467 /*
1468 * According to section 11.2.4.2 in the IBTA spec (Modify Queue Pair,
1469 * table 91), both the QP timeout and the retry count have to be set
1470 * for RC QP's during the RTR to RTS transition.
1471 */
1472 WARN_ON_ONCE((attr_mask & (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)) !=
1473 (IB_QP_TIMEOUT | IB_QP_RETRY_CNT));
1474
1475 /*
1476 * Set target->rq_tmo_jiffies to one second more than the largest time
1477 * it can take before an error completion is generated. See also
1478 * C9-140..142 in the IBTA spec for more information about how to
1479 * convert the QP Local ACK Timeout value to nanoseconds.
1480 */
1481 T_tr_ns = 4096 * (1ULL << qp_attr->timeout);
1482 max_compl_time_ms = qp_attr->retry_cnt * 4 * T_tr_ns;
1483 do_div(max_compl_time_ms, NSEC_PER_MSEC);
1484 rq_tmo_jiffies = msecs_to_jiffies(max_compl_time_ms + 1000);
1485
1486 return rq_tmo_jiffies;
1487}
1488
David Dillow961e0be2011-01-14 17:32:07 -05001489static void srp_cm_rep_handler(struct ib_cm_id *cm_id,
1490 struct srp_login_rsp *lrsp,
1491 struct srp_target_port *target)
1492{
1493 struct ib_qp_attr *qp_attr = NULL;
1494 int attr_mask = 0;
1495 int ret;
1496 int i;
1497
1498 if (lrsp->opcode == SRP_LOGIN_RSP) {
1499 target->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len);
1500 target->req_lim = be32_to_cpu(lrsp->req_lim_delta);
1501
1502 /*
1503 * Reserve credits for task management so we don't
1504 * bounce requests back to the SCSI mid-layer.
1505 */
1506 target->scsi_host->can_queue
1507 = min(target->req_lim - SRP_TSK_MGMT_SQ_SIZE,
1508 target->scsi_host->can_queue);
1509 } else {
1510 shost_printk(KERN_WARNING, target->scsi_host,
1511 PFX "Unhandled RSP opcode %#x\n", lrsp->opcode);
1512 ret = -ECONNRESET;
1513 goto error;
1514 }
1515
1516 if (!target->rx_ring[0]) {
1517 ret = srp_alloc_iu_bufs(target);
1518 if (ret)
1519 goto error;
1520 }
1521
1522 ret = -ENOMEM;
1523 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1524 if (!qp_attr)
1525 goto error;
1526
1527 qp_attr->qp_state = IB_QPS_RTR;
1528 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1529 if (ret)
1530 goto error_free;
1531
1532 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
1533 if (ret)
1534 goto error_free;
1535
1536 for (i = 0; i < SRP_RQ_SIZE; i++) {
1537 struct srp_iu *iu = target->rx_ring[i];
1538 ret = srp_post_recv(target, iu);
1539 if (ret)
1540 goto error_free;
1541 }
1542
1543 qp_attr->qp_state = IB_QPS_RTS;
1544 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1545 if (ret)
1546 goto error_free;
1547
Bart Van Asschec9b03c12011-09-03 09:34:48 +02001548 target->rq_tmo_jiffies = srp_compute_rq_tmo(qp_attr, attr_mask);
1549
David Dillow961e0be2011-01-14 17:32:07 -05001550 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
1551 if (ret)
1552 goto error_free;
1553
1554 ret = ib_send_cm_rtu(cm_id, NULL, 0);
1555
1556error_free:
1557 kfree(qp_attr);
1558
1559error:
1560 target->status = ret;
1561}
1562
Roland Dreieraef9ec32005-11-02 14:07:13 -08001563static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1564 struct ib_cm_event *event,
1565 struct srp_target_port *target)
1566{
David Dillow7aa54bd2008-01-07 18:23:41 -05001567 struct Scsi_Host *shost = target->scsi_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001568 struct ib_class_port_info *cpi;
1569 int opcode;
1570
1571 switch (event->param.rej_rcvd.reason) {
1572 case IB_CM_REJ_PORT_CM_REDIRECT:
1573 cpi = event->param.rej_rcvd.ari;
1574 target->path.dlid = cpi->redirect_lid;
1575 target->path.pkey = cpi->redirect_pkey;
1576 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1577 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1578
1579 target->status = target->path.dlid ?
1580 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1581 break;
1582
1583 case IB_CM_REJ_PORT_REDIRECT:
Roland Dreier5d7cbfd2007-08-03 10:45:18 -07001584 if (srp_target_is_topspin(target)) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001585 /*
1586 * Topspin/Cisco SRP gateways incorrectly send
1587 * reject reason code 25 when they mean 24
1588 * (port redirect).
1589 */
1590 memcpy(target->path.dgid.raw,
1591 event->param.rej_rcvd.ari, 16);
1592
David Dillow7aa54bd2008-01-07 18:23:41 -05001593 shost_printk(KERN_DEBUG, shost,
1594 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1595 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1596 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001597
1598 target->status = SRP_PORT_REDIRECT;
1599 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001600 shost_printk(KERN_WARNING, shost,
1601 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001602 target->status = -ECONNRESET;
1603 }
1604 break;
1605
1606 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
David Dillow7aa54bd2008-01-07 18:23:41 -05001607 shost_printk(KERN_WARNING, shost,
1608 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001609 target->status = -ECONNRESET;
1610 break;
1611
1612 case IB_CM_REJ_CONSUMER_DEFINED:
1613 opcode = *(u8 *) event->private_data;
1614 if (opcode == SRP_LOGIN_REJ) {
1615 struct srp_login_rej *rej = event->private_data;
1616 u32 reason = be32_to_cpu(rej->reason);
1617
1618 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
David Dillow7aa54bd2008-01-07 18:23:41 -05001619 shost_printk(KERN_WARNING, shost,
1620 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001621 else
David Dillow7aa54bd2008-01-07 18:23:41 -05001622 shost_printk(KERN_WARNING, shost,
1623 PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001624 } else
David Dillow7aa54bd2008-01-07 18:23:41 -05001625 shost_printk(KERN_WARNING, shost,
1626 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1627 " opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001628 target->status = -ECONNRESET;
1629 break;
1630
David Dillow9fe4bcf2008-01-08 17:08:52 -05001631 case IB_CM_REJ_STALE_CONN:
1632 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
1633 target->status = SRP_STALE_CONN;
1634 break;
1635
Roland Dreieraef9ec32005-11-02 14:07:13 -08001636 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001637 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
1638 event->param.rej_rcvd.reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001639 target->status = -ECONNRESET;
1640 }
1641}
1642
1643static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1644{
1645 struct srp_target_port *target = cm_id->context;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001646 int comp = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001647
1648 switch (event->event) {
1649 case IB_CM_REQ_ERROR:
David Dillow7aa54bd2008-01-07 18:23:41 -05001650 shost_printk(KERN_DEBUG, target->scsi_host,
1651 PFX "Sending CM REQ failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001652 comp = 1;
1653 target->status = -ECONNRESET;
1654 break;
1655
1656 case IB_CM_REP_RECEIVED:
1657 comp = 1;
David Dillow961e0be2011-01-14 17:32:07 -05001658 srp_cm_rep_handler(cm_id, event->private_data, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001659 break;
1660
1661 case IB_CM_REJ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001662 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001663 comp = 1;
1664
1665 srp_cm_rej_handler(cm_id, event, target);
1666 break;
1667
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001668 case IB_CM_DREQ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001669 shost_printk(KERN_WARNING, target->scsi_host,
1670 PFX "DREQ received - connection closed\n");
Bart Van Assche294c8752011-12-25 12:18:12 +00001671 srp_change_conn_state(target, false);
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001672 if (ib_send_cm_drep(cm_id, NULL, 0))
David Dillow7aa54bd2008-01-07 18:23:41 -05001673 shost_printk(KERN_ERR, target->scsi_host,
1674 PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001675 break;
1676
1677 case IB_CM_TIMEWAIT_EXIT:
David Dillow7aa54bd2008-01-07 18:23:41 -05001678 shost_printk(KERN_ERR, target->scsi_host,
1679 PFX "connection closed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001680
Roland Dreieraef9ec32005-11-02 14:07:13 -08001681 target->status = 0;
1682 break;
1683
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001684 case IB_CM_MRA_RECEIVED:
1685 case IB_CM_DREQ_ERROR:
1686 case IB_CM_DREP_RECEIVED:
1687 break;
1688
Roland Dreieraef9ec32005-11-02 14:07:13 -08001689 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001690 shost_printk(KERN_WARNING, target->scsi_host,
1691 PFX "Unhandled CM event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001692 break;
1693 }
1694
1695 if (comp)
1696 complete(&target->done);
1697
Roland Dreieraef9ec32005-11-02 14:07:13 -08001698 return 0;
1699}
1700
Roland Dreierd945e1d2006-05-09 10:50:28 -07001701static int srp_send_tsk_mgmt(struct srp_target_port *target,
David Dillowf8b6e312010-11-26 13:02:21 -05001702 u64 req_tag, unsigned int lun, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001703{
David Dillow19081f32010-10-18 08:54:49 -04001704 struct ib_device *dev = target->srp_host->srp_dev->dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001705 struct srp_iu *iu;
1706 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001707
Bart Van Assche3780d1f2013-02-21 17:18:00 +00001708 if (!target->connected || target->qp_in_error)
1709 return -1;
1710
David Dillowf8b6e312010-11-26 13:02:21 -05001711 init_completion(&target->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001712
Bart Van Asschee9684672010-11-26 15:08:38 -05001713 spin_lock_irq(&target->lock);
David Dillowbb125882010-10-08 14:40:47 -04001714 iu = __srp_get_tx_iu(target, SRP_IU_TSK_MGMT);
Bart Van Asschee9684672010-11-26 15:08:38 -05001715 spin_unlock_irq(&target->lock);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001716
Roland Dreieraef9ec32005-11-02 14:07:13 -08001717 if (!iu)
Bart Van Assche76c75b22010-11-26 14:37:47 -05001718 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001719
David Dillow19081f32010-10-18 08:54:49 -04001720 ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt,
1721 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001722 tsk_mgmt = iu->buf;
1723 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1724
1725 tsk_mgmt->opcode = SRP_TSK_MGMT;
David Dillowf8b6e312010-11-26 13:02:21 -05001726 tsk_mgmt->lun = cpu_to_be64((u64) lun << 48);
1727 tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001728 tsk_mgmt->tsk_mgmt_func = func;
David Dillowf8b6e312010-11-26 13:02:21 -05001729 tsk_mgmt->task_tag = req_tag;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001730
David Dillow19081f32010-10-18 08:54:49 -04001731 ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt,
1732 DMA_TO_DEVICE);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001733 if (srp_post_send(target, iu, sizeof *tsk_mgmt)) {
1734 srp_put_tx_iu(target, iu, SRP_IU_TSK_MGMT);
1735 return -1;
1736 }
Roland Dreierd945e1d2006-05-09 10:50:28 -07001737
David Dillowf8b6e312010-11-26 13:02:21 -05001738 if (!wait_for_completion_timeout(&target->tsk_mgmt_done,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001739 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001740 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001741
Roland Dreierd945e1d2006-05-09 10:50:28 -07001742 return 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001743}
1744
Roland Dreieraef9ec32005-11-02 14:07:13 -08001745static int srp_abort(struct scsi_cmnd *scmnd)
1746{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001747 struct srp_target_port *target = host_to_target(scmnd->device->host);
David Dillowf8b6e312010-11-26 13:02:21 -05001748 struct srp_request *req = (struct srp_request *) scmnd->host_scribble;
Bart Van Assche086f44f2013-06-12 15:23:04 +02001749 int ret;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001750
David Dillow7aa54bd2008-01-07 18:23:41 -05001751 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001752
Bart Van Asschec7c4e7f2013-02-21 17:19:04 +00001753 if (!req || !srp_claim_req(target, req, scmnd))
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001754 return FAILED;
Bart Van Assche086f44f2013-06-12 15:23:04 +02001755 if (srp_send_tsk_mgmt(target, req->index, scmnd->device->lun,
Bart Van Assche80d5e8a2013-07-10 17:36:35 +02001756 SRP_TSK_ABORT_TASK) == 0)
Bart Van Assche086f44f2013-06-12 15:23:04 +02001757 ret = SUCCESS;
Bart Van Assche99e1c132013-06-28 14:49:58 +02001758 else if (target->transport_offline)
1759 ret = FAST_IO_FAIL;
Bart Van Assche086f44f2013-06-12 15:23:04 +02001760 else
1761 ret = FAILED;
Bart Van Assche22032992012-08-14 13:18:53 +00001762 srp_free_req(target, req, scmnd, 0);
1763 scmnd->result = DID_ABORT << 16;
Bart Van Assched8536672012-08-24 10:29:11 +00001764 scmnd->scsi_done(scmnd);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001765
Bart Van Assche086f44f2013-06-12 15:23:04 +02001766 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001767}
1768
1769static int srp_reset_device(struct scsi_cmnd *scmnd)
1770{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001771 struct srp_target_port *target = host_to_target(scmnd->device->host);
Bart Van Assche536ae142010-11-26 13:58:27 -05001772 int i;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001773
David Dillow7aa54bd2008-01-07 18:23:41 -05001774 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001775
David Dillowf8b6e312010-11-26 13:02:21 -05001776 if (srp_send_tsk_mgmt(target, SRP_TAG_NO_REQ, scmnd->device->lun,
1777 SRP_TSK_LUN_RESET))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001778 return FAILED;
David Dillowf8b6e312010-11-26 13:02:21 -05001779 if (target->tsk_mgmt_status)
Roland Dreierd945e1d2006-05-09 10:50:28 -07001780 return FAILED;
1781
Bart Van Assche536ae142010-11-26 13:58:27 -05001782 for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
1783 struct srp_request *req = &target->req_ring[i];
David Dillowf8b6e312010-11-26 13:02:21 -05001784 if (req->scmnd && req->scmnd->device == scmnd->device)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001785 srp_reset_req(target, req);
Bart Van Assche536ae142010-11-26 13:58:27 -05001786 }
Roland Dreierd945e1d2006-05-09 10:50:28 -07001787
Roland Dreierd945e1d2006-05-09 10:50:28 -07001788 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001789}
1790
1791static int srp_reset_host(struct scsi_cmnd *scmnd)
1792{
1793 struct srp_target_port *target = host_to_target(scmnd->device->host);
1794 int ret = FAILED;
1795
David Dillow7aa54bd2008-01-07 18:23:41 -05001796 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001797
1798 if (!srp_reconnect_target(target))
1799 ret = SUCCESS;
1800
1801 return ret;
1802}
1803
Bart Van Asschec9b03c12011-09-03 09:34:48 +02001804static int srp_slave_configure(struct scsi_device *sdev)
1805{
1806 struct Scsi_Host *shost = sdev->host;
1807 struct srp_target_port *target = host_to_target(shost);
1808 struct request_queue *q = sdev->request_queue;
1809 unsigned long timeout;
1810
1811 if (sdev->type == TYPE_DISK) {
1812 timeout = max_t(unsigned, 30 * HZ, target->rq_tmo_jiffies);
1813 blk_queue_rq_timeout(q, timeout);
1814 }
1815
1816 return 0;
1817}
1818
Tony Jonesee959b02008-02-22 00:13:36 +01001819static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
1820 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001821{
Tony Jonesee959b02008-02-22 00:13:36 +01001822 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001823
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001824 return sprintf(buf, "0x%016llx\n",
1825 (unsigned long long) be64_to_cpu(target->id_ext));
1826}
1827
Tony Jonesee959b02008-02-22 00:13:36 +01001828static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
1829 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001830{
Tony Jonesee959b02008-02-22 00:13:36 +01001831 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001832
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001833 return sprintf(buf, "0x%016llx\n",
1834 (unsigned long long) be64_to_cpu(target->ioc_guid));
1835}
1836
Tony Jonesee959b02008-02-22 00:13:36 +01001837static ssize_t show_service_id(struct device *dev,
1838 struct device_attribute *attr, char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001839{
Tony Jonesee959b02008-02-22 00:13:36 +01001840 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001841
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001842 return sprintf(buf, "0x%016llx\n",
1843 (unsigned long long) be64_to_cpu(target->service_id));
1844}
1845
Tony Jonesee959b02008-02-22 00:13:36 +01001846static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
1847 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001848{
Tony Jonesee959b02008-02-22 00:13:36 +01001849 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001850
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001851 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1852}
1853
Tony Jonesee959b02008-02-22 00:13:36 +01001854static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
1855 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001856{
Tony Jonesee959b02008-02-22 00:13:36 +01001857 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001858
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001859 return sprintf(buf, "%pI6\n", target->path.dgid.raw);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001860}
1861
Tony Jonesee959b02008-02-22 00:13:36 +01001862static ssize_t show_orig_dgid(struct device *dev,
1863 struct device_attribute *attr, char *buf)
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001864{
Tony Jonesee959b02008-02-22 00:13:36 +01001865 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001866
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001867 return sprintf(buf, "%pI6\n", target->orig_dgid);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001868}
1869
Bart Van Assche89de7482010-08-03 14:08:45 +00001870static ssize_t show_req_lim(struct device *dev,
1871 struct device_attribute *attr, char *buf)
1872{
1873 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1874
Bart Van Assche89de7482010-08-03 14:08:45 +00001875 return sprintf(buf, "%d\n", target->req_lim);
1876}
1877
Tony Jonesee959b02008-02-22 00:13:36 +01001878static ssize_t show_zero_req_lim(struct device *dev,
1879 struct device_attribute *attr, char *buf)
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001880{
Tony Jonesee959b02008-02-22 00:13:36 +01001881 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001882
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001883 return sprintf(buf, "%d\n", target->zero_req_lim);
1884}
1885
Tony Jonesee959b02008-02-22 00:13:36 +01001886static ssize_t show_local_ib_port(struct device *dev,
1887 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001888{
Tony Jonesee959b02008-02-22 00:13:36 +01001889 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001890
1891 return sprintf(buf, "%d\n", target->srp_host->port);
1892}
1893
Tony Jonesee959b02008-02-22 00:13:36 +01001894static ssize_t show_local_ib_device(struct device *dev,
1895 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001896{
Tony Jonesee959b02008-02-22 00:13:36 +01001897 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001898
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001899 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001900}
1901
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02001902static ssize_t show_comp_vector(struct device *dev,
1903 struct device_attribute *attr, char *buf)
1904{
1905 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1906
1907 return sprintf(buf, "%d\n", target->comp_vector);
1908}
1909
Vu Pham7bb312e2013-10-26 14:31:27 +02001910static ssize_t show_tl_retry_count(struct device *dev,
1911 struct device_attribute *attr, char *buf)
1912{
1913 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1914
1915 return sprintf(buf, "%d\n", target->tl_retry_count);
1916}
1917
David Dillow49248642011-01-14 18:23:24 -05001918static ssize_t show_cmd_sg_entries(struct device *dev,
1919 struct device_attribute *attr, char *buf)
1920{
1921 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1922
1923 return sprintf(buf, "%u\n", target->cmd_sg_cnt);
1924}
1925
David Dillowc07d4242011-01-16 13:57:10 -05001926static ssize_t show_allow_ext_sg(struct device *dev,
1927 struct device_attribute *attr, char *buf)
1928{
1929 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1930
1931 return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false");
1932}
1933
Tony Jonesee959b02008-02-22 00:13:36 +01001934static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1935static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1936static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1937static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1938static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1939static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
Bart Van Assche89de7482010-08-03 14:08:45 +00001940static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01001941static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1942static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1943static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02001944static DEVICE_ATTR(comp_vector, S_IRUGO, show_comp_vector, NULL);
Vu Pham7bb312e2013-10-26 14:31:27 +02001945static DEVICE_ATTR(tl_retry_count, S_IRUGO, show_tl_retry_count, NULL);
David Dillow49248642011-01-14 18:23:24 -05001946static DEVICE_ATTR(cmd_sg_entries, S_IRUGO, show_cmd_sg_entries, NULL);
David Dillowc07d4242011-01-16 13:57:10 -05001947static DEVICE_ATTR(allow_ext_sg, S_IRUGO, show_allow_ext_sg, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001948
Tony Jonesee959b02008-02-22 00:13:36 +01001949static struct device_attribute *srp_host_attrs[] = {
1950 &dev_attr_id_ext,
1951 &dev_attr_ioc_guid,
1952 &dev_attr_service_id,
1953 &dev_attr_pkey,
1954 &dev_attr_dgid,
1955 &dev_attr_orig_dgid,
Bart Van Assche89de7482010-08-03 14:08:45 +00001956 &dev_attr_req_lim,
Tony Jonesee959b02008-02-22 00:13:36 +01001957 &dev_attr_zero_req_lim,
1958 &dev_attr_local_ib_port,
1959 &dev_attr_local_ib_device,
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02001960 &dev_attr_comp_vector,
Vu Pham7bb312e2013-10-26 14:31:27 +02001961 &dev_attr_tl_retry_count,
David Dillow49248642011-01-14 18:23:24 -05001962 &dev_attr_cmd_sg_entries,
David Dillowc07d4242011-01-16 13:57:10 -05001963 &dev_attr_allow_ext_sg,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001964 NULL
1965};
1966
Roland Dreieraef9ec32005-11-02 14:07:13 -08001967static struct scsi_host_template srp_template = {
1968 .module = THIS_MODULE,
Roland Dreierb7f008f2007-05-06 21:18:11 -07001969 .name = "InfiniBand SRP initiator",
1970 .proc_name = DRV_NAME,
Bart Van Asschec9b03c12011-09-03 09:34:48 +02001971 .slave_configure = srp_slave_configure,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001972 .info = srp_target_info,
1973 .queuecommand = srp_queuecommand,
1974 .eh_abort_handler = srp_abort,
1975 .eh_device_reset_handler = srp_reset_device,
1976 .eh_host_reset_handler = srp_reset_host,
Bart Van Assche2742c1d2013-06-12 15:24:25 +02001977 .skip_settle_delay = true,
David Dillow49248642011-01-14 18:23:24 -05001978 .sg_tablesize = SRP_DEF_SG_TABLESIZE,
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001979 .can_queue = SRP_CMD_SQ_SIZE,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001980 .this_id = -1,
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001981 .cmd_per_lun = SRP_CMD_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001982 .use_clustering = ENABLE_CLUSTERING,
1983 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001984};
1985
1986static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1987{
FUJITA Tomonori32368222007-06-27 16:33:12 +09001988 struct srp_rport_identifiers ids;
1989 struct srp_rport *rport;
1990
Roland Dreieraef9ec32005-11-02 14:07:13 -08001991 sprintf(target->target_name, "SRP.T10:%016llX",
1992 (unsigned long long) be64_to_cpu(target->id_ext));
1993
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001994 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001995 return -ENODEV;
1996
FUJITA Tomonori32368222007-06-27 16:33:12 +09001997 memcpy(ids.port_id, &target->id_ext, 8);
1998 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09001999 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori32368222007-06-27 16:33:12 +09002000 rport = srp_rport_add(target->scsi_host, &ids);
2001 if (IS_ERR(rport)) {
2002 scsi_remove_host(target->scsi_host);
2003 return PTR_ERR(rport);
2004 }
2005
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02002006 rport->lld_data = target;
2007
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002008 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002009 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002010 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002011
2012 target->state = SRP_TARGET_LIVE;
2013
Roland Dreieraef9ec32005-11-02 14:07:13 -08002014 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07002015 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002016
2017 return 0;
2018}
2019
Tony Jonesee959b02008-02-22 00:13:36 +01002020static void srp_release_dev(struct device *dev)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002021{
2022 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01002023 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002024
2025 complete(&host->released);
2026}
2027
2028static struct class srp_class = {
2029 .name = "infiniband_srp",
Tony Jonesee959b02008-02-22 00:13:36 +01002030 .dev_release = srp_release_dev
Roland Dreieraef9ec32005-11-02 14:07:13 -08002031};
2032
Bart Van Assche96fc2482013-06-28 14:51:26 +02002033/**
2034 * srp_conn_unique() - check whether the connection to a target is unique
2035 */
2036static bool srp_conn_unique(struct srp_host *host,
2037 struct srp_target_port *target)
2038{
2039 struct srp_target_port *t;
2040 bool ret = false;
2041
2042 if (target->state == SRP_TARGET_REMOVED)
2043 goto out;
2044
2045 ret = true;
2046
2047 spin_lock(&host->target_lock);
2048 list_for_each_entry(t, &host->target_list, list) {
2049 if (t != target &&
2050 target->id_ext == t->id_ext &&
2051 target->ioc_guid == t->ioc_guid &&
2052 target->initiator_ext == t->initiator_ext) {
2053 ret = false;
2054 break;
2055 }
2056 }
2057 spin_unlock(&host->target_lock);
2058
2059out:
2060 return ret;
2061}
2062
Roland Dreieraef9ec32005-11-02 14:07:13 -08002063/*
2064 * Target ports are added by writing
2065 *
2066 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
2067 * pkey=<P_Key>,service_id=<service ID>
2068 *
2069 * to the add_target sysfs attribute.
2070 */
2071enum {
2072 SRP_OPT_ERR = 0,
2073 SRP_OPT_ID_EXT = 1 << 0,
2074 SRP_OPT_IOC_GUID = 1 << 1,
2075 SRP_OPT_DGID = 1 << 2,
2076 SRP_OPT_PKEY = 1 << 3,
2077 SRP_OPT_SERVICE_ID = 1 << 4,
2078 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07002079 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07002080 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002081 SRP_OPT_INITIATOR_EXT = 1 << 8,
David Dillow49248642011-01-14 18:23:24 -05002082 SRP_OPT_CMD_SG_ENTRIES = 1 << 9,
David Dillowc07d4242011-01-16 13:57:10 -05002083 SRP_OPT_ALLOW_EXT_SG = 1 << 10,
2084 SRP_OPT_SG_TABLESIZE = 1 << 11,
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002085 SRP_OPT_COMP_VECTOR = 1 << 12,
Vu Pham7bb312e2013-10-26 14:31:27 +02002086 SRP_OPT_TL_RETRY_COUNT = 1 << 13,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002087 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
2088 SRP_OPT_IOC_GUID |
2089 SRP_OPT_DGID |
2090 SRP_OPT_PKEY |
2091 SRP_OPT_SERVICE_ID),
2092};
2093
Steven Whitehousea447c092008-10-13 10:46:57 +01002094static const match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07002095 { SRP_OPT_ID_EXT, "id_ext=%s" },
2096 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
2097 { SRP_OPT_DGID, "dgid=%s" },
2098 { SRP_OPT_PKEY, "pkey=%x" },
2099 { SRP_OPT_SERVICE_ID, "service_id=%s" },
2100 { SRP_OPT_MAX_SECT, "max_sect=%d" },
2101 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07002102 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002103 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
David Dillow49248642011-01-14 18:23:24 -05002104 { SRP_OPT_CMD_SG_ENTRIES, "cmd_sg_entries=%u" },
David Dillowc07d4242011-01-16 13:57:10 -05002105 { SRP_OPT_ALLOW_EXT_SG, "allow_ext_sg=%u" },
2106 { SRP_OPT_SG_TABLESIZE, "sg_tablesize=%u" },
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002107 { SRP_OPT_COMP_VECTOR, "comp_vector=%u" },
Vu Pham7bb312e2013-10-26 14:31:27 +02002108 { SRP_OPT_TL_RETRY_COUNT, "tl_retry_count=%u" },
Vu Pham52fb2b502006-06-17 20:37:31 -07002109 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002110};
2111
2112static int srp_parse_options(const char *buf, struct srp_target_port *target)
2113{
2114 char *options, *sep_opt;
2115 char *p;
2116 char dgid[3];
2117 substring_t args[MAX_OPT_ARGS];
2118 int opt_mask = 0;
2119 int token;
2120 int ret = -EINVAL;
2121 int i;
2122
2123 options = kstrdup(buf, GFP_KERNEL);
2124 if (!options)
2125 return -ENOMEM;
2126
2127 sep_opt = options;
2128 while ((p = strsep(&sep_opt, ",")) != NULL) {
2129 if (!*p)
2130 continue;
2131
2132 token = match_token(p, srp_opt_tokens, args);
2133 opt_mask |= token;
2134
2135 switch (token) {
2136 case SRP_OPT_ID_EXT:
2137 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002138 if (!p) {
2139 ret = -ENOMEM;
2140 goto out;
2141 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002142 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2143 kfree(p);
2144 break;
2145
2146 case SRP_OPT_IOC_GUID:
2147 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002148 if (!p) {
2149 ret = -ENOMEM;
2150 goto out;
2151 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002152 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
2153 kfree(p);
2154 break;
2155
2156 case SRP_OPT_DGID:
2157 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002158 if (!p) {
2159 ret = -ENOMEM;
2160 goto out;
2161 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002162 if (strlen(p) != 32) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002163 pr_warn("bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07002164 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002165 goto out;
2166 }
2167
2168 for (i = 0; i < 16; ++i) {
2169 strlcpy(dgid, p + i * 2, 3);
2170 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
2171 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08002172 kfree(p);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002173 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002174 break;
2175
2176 case SRP_OPT_PKEY:
2177 if (match_hex(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002178 pr_warn("bad P_Key parameter '%s'\n", p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002179 goto out;
2180 }
2181 target->path.pkey = cpu_to_be16(token);
2182 break;
2183
2184 case SRP_OPT_SERVICE_ID:
2185 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002186 if (!p) {
2187 ret = -ENOMEM;
2188 goto out;
2189 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002190 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
Sean Hefty247e0202007-08-08 15:51:18 -07002191 target->path.service_id = target->service_id;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002192 kfree(p);
2193 break;
2194
2195 case SRP_OPT_MAX_SECT:
2196 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002197 pr_warn("bad max sect parameter '%s'\n", p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002198 goto out;
2199 }
2200 target->scsi_host->max_sectors = token;
2201 break;
2202
Vu Pham52fb2b502006-06-17 20:37:31 -07002203 case SRP_OPT_MAX_CMD_PER_LUN:
2204 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002205 pr_warn("bad max cmd_per_lun parameter '%s'\n",
2206 p);
Vu Pham52fb2b502006-06-17 20:37:31 -07002207 goto out;
2208 }
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00002209 target->scsi_host->cmd_per_lun = min(token, SRP_CMD_SQ_SIZE);
Vu Pham52fb2b502006-06-17 20:37:31 -07002210 break;
2211
Ramachandra K0c0450db2006-06-17 20:37:38 -07002212 case SRP_OPT_IO_CLASS:
2213 if (match_hex(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002214 pr_warn("bad IO class parameter '%s'\n", p);
Ramachandra K0c0450db2006-06-17 20:37:38 -07002215 goto out;
2216 }
2217 if (token != SRP_REV10_IB_IO_CLASS &&
2218 token != SRP_REV16A_IB_IO_CLASS) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002219 pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n",
2220 token, SRP_REV10_IB_IO_CLASS,
2221 SRP_REV16A_IB_IO_CLASS);
Ramachandra K0c0450db2006-06-17 20:37:38 -07002222 goto out;
2223 }
2224 target->io_class = token;
2225 break;
2226
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002227 case SRP_OPT_INITIATOR_EXT:
2228 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002229 if (!p) {
2230 ret = -ENOMEM;
2231 goto out;
2232 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002233 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2234 kfree(p);
2235 break;
2236
David Dillow49248642011-01-14 18:23:24 -05002237 case SRP_OPT_CMD_SG_ENTRIES:
2238 if (match_int(args, &token) || token < 1 || token > 255) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002239 pr_warn("bad max cmd_sg_entries parameter '%s'\n",
2240 p);
David Dillow49248642011-01-14 18:23:24 -05002241 goto out;
2242 }
2243 target->cmd_sg_cnt = token;
2244 break;
2245
David Dillowc07d4242011-01-16 13:57:10 -05002246 case SRP_OPT_ALLOW_EXT_SG:
2247 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002248 pr_warn("bad allow_ext_sg parameter '%s'\n", p);
David Dillowc07d4242011-01-16 13:57:10 -05002249 goto out;
2250 }
2251 target->allow_ext_sg = !!token;
2252 break;
2253
2254 case SRP_OPT_SG_TABLESIZE:
2255 if (match_int(args, &token) || token < 1 ||
2256 token > SCSI_MAX_SG_CHAIN_SEGMENTS) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002257 pr_warn("bad max sg_tablesize parameter '%s'\n",
2258 p);
David Dillowc07d4242011-01-16 13:57:10 -05002259 goto out;
2260 }
2261 target->sg_tablesize = token;
2262 break;
2263
Bart Van Assche4b5e5f42013-06-28 14:57:42 +02002264 case SRP_OPT_COMP_VECTOR:
2265 if (match_int(args, &token) || token < 0) {
2266 pr_warn("bad comp_vector parameter '%s'\n", p);
2267 goto out;
2268 }
2269 target->comp_vector = token;
2270 break;
2271
Vu Pham7bb312e2013-10-26 14:31:27 +02002272 case SRP_OPT_TL_RETRY_COUNT:
2273 if (match_int(args, &token) || token < 2 || token > 7) {
2274 pr_warn("bad tl_retry_count parameter '%s' (must be a number between 2 and 7)\n",
2275 p);
2276 goto out;
2277 }
2278 target->tl_retry_count = token;
2279 break;
2280
Roland Dreieraef9ec32005-11-02 14:07:13 -08002281 default:
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002282 pr_warn("unknown parameter or missing value '%s' in target creation request\n",
2283 p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002284 goto out;
2285 }
2286 }
2287
2288 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
2289 ret = 0;
2290 else
2291 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
2292 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
2293 !(srp_opt_tokens[i].token & opt_mask))
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002294 pr_warn("target creation request is missing parameter '%s'\n",
2295 srp_opt_tokens[i].pattern);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002296
2297out:
2298 kfree(options);
2299 return ret;
2300}
2301
Tony Jonesee959b02008-02-22 00:13:36 +01002302static ssize_t srp_create_target(struct device *dev,
2303 struct device_attribute *attr,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002304 const char *buf, size_t count)
2305{
2306 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01002307 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002308 struct Scsi_Host *target_host;
2309 struct srp_target_port *target;
David Dillowc07d4242011-01-16 13:57:10 -05002310 struct ib_device *ibdev = host->srp_dev->dev;
2311 dma_addr_t dma_addr;
David Dillow8f26c9f2011-01-14 19:45:50 -05002312 int i, ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002313
2314 target_host = scsi_host_alloc(&srp_template,
2315 sizeof (struct srp_target_port));
2316 if (!target_host)
2317 return -ENOMEM;
2318
David Dillow49248642011-01-14 18:23:24 -05002319 target_host->transportt = ib_srp_transport_template;
Bart Van Asschefd1b6c42011-07-13 09:19:16 -07002320 target_host->max_channel = 0;
2321 target_host->max_id = 1;
Arne Redlich3c8edf02006-11-15 12:43:00 +01002322 target_host->max_lun = SRP_MAX_LUN;
2323 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08002324
Roland Dreieraef9ec32005-11-02 14:07:13 -08002325 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002326
David Dillow49248642011-01-14 18:23:24 -05002327 target->io_class = SRP_REV16A_IB_IO_CLASS;
2328 target->scsi_host = target_host;
2329 target->srp_host = host;
2330 target->lkey = host->srp_dev->mr->lkey;
2331 target->rkey = host->srp_dev->mr->rkey;
2332 target->cmd_sg_cnt = cmd_sg_entries;
David Dillowc07d4242011-01-16 13:57:10 -05002333 target->sg_tablesize = indirect_sg_entries ? : cmd_sg_entries;
2334 target->allow_ext_sg = allow_ext_sg;
Vu Pham7bb312e2013-10-26 14:31:27 +02002335 target->tl_retry_count = 7;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002336
Roland Dreieraef9ec32005-11-02 14:07:13 -08002337 ret = srp_parse_options(buf, target);
2338 if (ret)
2339 goto err;
2340
Bart Van Assche96fc2482013-06-28 14:51:26 +02002341 if (!srp_conn_unique(target->srp_host, target)) {
2342 shost_printk(KERN_INFO, target->scsi_host,
2343 PFX "Already connected to target port with id_ext=%016llx;ioc_guid=%016llx;initiator_ext=%016llx\n",
2344 be64_to_cpu(target->id_ext),
2345 be64_to_cpu(target->ioc_guid),
2346 be64_to_cpu(target->initiator_ext));
2347 ret = -EEXIST;
2348 goto err;
2349 }
2350
David Dillowc07d4242011-01-16 13:57:10 -05002351 if (!host->srp_dev->fmr_pool && !target->allow_ext_sg &&
2352 target->cmd_sg_cnt < target->sg_tablesize) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002353 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 -05002354 target->sg_tablesize = target->cmd_sg_cnt;
2355 }
2356
2357 target_host->sg_tablesize = target->sg_tablesize;
2358 target->indirect_size = target->sg_tablesize *
2359 sizeof (struct srp_direct_buf);
David Dillow49248642011-01-14 18:23:24 -05002360 target->max_iu_len = sizeof (struct srp_cmd) +
2361 sizeof (struct srp_indirect_buf) +
2362 target->cmd_sg_cnt * sizeof (struct srp_direct_buf);
2363
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002364 INIT_WORK(&target->remove_work, srp_remove_work);
David Dillow8f26c9f2011-01-14 19:45:50 -05002365 spin_lock_init(&target->lock);
2366 INIT_LIST_HEAD(&target->free_tx);
2367 INIT_LIST_HEAD(&target->free_reqs);
2368 for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
2369 struct srp_request *req = &target->req_ring[i];
2370
2371 req->fmr_list = kmalloc(target->cmd_sg_cnt * sizeof (void *),
2372 GFP_KERNEL);
2373 req->map_page = kmalloc(SRP_FMR_SIZE * sizeof (void *),
2374 GFP_KERNEL);
David Dillowc07d4242011-01-16 13:57:10 -05002375 req->indirect_desc = kmalloc(target->indirect_size, GFP_KERNEL);
2376 if (!req->fmr_list || !req->map_page || !req->indirect_desc)
David Dillow8f26c9f2011-01-14 19:45:50 -05002377 goto err_free_mem;
2378
David Dillowc07d4242011-01-16 13:57:10 -05002379 dma_addr = ib_dma_map_single(ibdev, req->indirect_desc,
2380 target->indirect_size,
2381 DMA_TO_DEVICE);
2382 if (ib_dma_mapping_error(ibdev, dma_addr))
2383 goto err_free_mem;
2384
2385 req->indirect_dma_addr = dma_addr;
David Dillow8f26c9f2011-01-14 19:45:50 -05002386 req->index = i;
2387 list_add_tail(&req->list, &target->free_reqs);
2388 }
2389
David Dillowc07d4242011-01-16 13:57:10 -05002390 ib_query_gid(ibdev, host->port, 0, &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002391
David Dillow7aa54bd2008-01-07 18:23:41 -05002392 shost_printk(KERN_DEBUG, target->scsi_host, PFX
2393 "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002394 "service_id %016llx dgid %pI6\n",
Roland Dreieraef9ec32005-11-02 14:07:13 -08002395 (unsigned long long) be64_to_cpu(target->id_ext),
2396 (unsigned long long) be64_to_cpu(target->ioc_guid),
2397 be16_to_cpu(target->path.pkey),
2398 (unsigned long long) be64_to_cpu(target->service_id),
Harvey Harrison8867cd72008-10-28 22:36:33 -07002399 target->path.dgid.raw);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002400
2401 ret = srp_create_target_ib(target);
2402 if (ret)
David Dillow8f26c9f2011-01-14 19:45:50 -05002403 goto err_free_mem;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002404
David Dillow9fe4bcf2008-01-08 17:08:52 -05002405 ret = srp_new_cm_id(target);
2406 if (ret)
David Dillow8f26c9f2011-01-14 19:45:50 -05002407 goto err_free_ib;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002408
2409 ret = srp_connect_target(target);
2410 if (ret) {
David Dillow7aa54bd2008-01-07 18:23:41 -05002411 shost_printk(KERN_ERR, target->scsi_host,
2412 PFX "Connection failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002413 goto err_cm_id;
2414 }
2415
2416 ret = srp_add_target(host, target);
2417 if (ret)
2418 goto err_disconnect;
2419
2420 return count;
2421
2422err_disconnect:
2423 srp_disconnect_target(target);
2424
2425err_cm_id:
2426 ib_destroy_cm_id(target->cm_id);
2427
David Dillow8f26c9f2011-01-14 19:45:50 -05002428err_free_ib:
Roland Dreieraef9ec32005-11-02 14:07:13 -08002429 srp_free_target_ib(target);
2430
David Dillow8f26c9f2011-01-14 19:45:50 -05002431err_free_mem:
2432 srp_free_req_data(target);
2433
Roland Dreieraef9ec32005-11-02 14:07:13 -08002434err:
2435 scsi_host_put(target_host);
2436
2437 return ret;
2438}
2439
Tony Jonesee959b02008-02-22 00:13:36 +01002440static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002441
Tony Jonesee959b02008-02-22 00:13:36 +01002442static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
2443 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002444{
Tony Jonesee959b02008-02-22 00:13:36 +01002445 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002446
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002447 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002448}
2449
Tony Jonesee959b02008-02-22 00:13:36 +01002450static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002451
Tony Jonesee959b02008-02-22 00:13:36 +01002452static ssize_t show_port(struct device *dev, struct device_attribute *attr,
2453 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002454{
Tony Jonesee959b02008-02-22 00:13:36 +01002455 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002456
2457 return sprintf(buf, "%d\n", host->port);
2458}
2459
Tony Jonesee959b02008-02-22 00:13:36 +01002460static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002461
Roland Dreierf5358a12006-06-17 20:37:29 -07002462static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002463{
2464 struct srp_host *host;
2465
2466 host = kzalloc(sizeof *host, GFP_KERNEL);
2467 if (!host)
2468 return NULL;
2469
2470 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002471 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002472 init_completion(&host->released);
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002473 host->srp_dev = device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002474 host->port = port;
2475
Tony Jonesee959b02008-02-22 00:13:36 +01002476 host->dev.class = &srp_class;
2477 host->dev.parent = device->dev->dma_device;
Kay Sieversd927e382009-01-06 10:44:39 -08002478 dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002479
Tony Jonesee959b02008-02-22 00:13:36 +01002480 if (device_register(&host->dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07002481 goto free_host;
Tony Jonesee959b02008-02-22 00:13:36 +01002482 if (device_create_file(&host->dev, &dev_attr_add_target))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002483 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01002484 if (device_create_file(&host->dev, &dev_attr_ibdev))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002485 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01002486 if (device_create_file(&host->dev, &dev_attr_port))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002487 goto err_class;
2488
2489 return host;
2490
2491err_class:
Tony Jonesee959b02008-02-22 00:13:36 +01002492 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002493
Roland Dreierf5358a12006-06-17 20:37:29 -07002494free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08002495 kfree(host);
2496
2497 return NULL;
2498}
2499
2500static void srp_add_one(struct ib_device *device)
2501{
Roland Dreierf5358a12006-06-17 20:37:29 -07002502 struct srp_device *srp_dev;
2503 struct ib_device_attr *dev_attr;
2504 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002505 struct srp_host *host;
David Dillowbe8b9812011-01-18 21:58:09 -05002506 int max_pages_per_fmr, fmr_page_shift, s, e, p;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002507
Roland Dreierf5358a12006-06-17 20:37:29 -07002508 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
2509 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08002510 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002511
Roland Dreierf5358a12006-06-17 20:37:29 -07002512 if (ib_query_device(device, dev_attr)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002513 pr_warn("Query device failed for %s\n", device->name);
Roland Dreierf5358a12006-06-17 20:37:29 -07002514 goto free_attr;
2515 }
2516
2517 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
2518 if (!srp_dev)
2519 goto free_attr;
2520
2521 /*
2522 * Use the smallest page size supported by the HCA, down to a
David Dillow8f26c9f2011-01-14 19:45:50 -05002523 * minimum of 4096 bytes. We're unlikely to build large sglists
2524 * out of smaller entries.
Roland Dreierf5358a12006-06-17 20:37:29 -07002525 */
David Dillow8f26c9f2011-01-14 19:45:50 -05002526 fmr_page_shift = max(12, ffs(dev_attr->page_size_cap) - 1);
2527 srp_dev->fmr_page_size = 1 << fmr_page_shift;
2528 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
2529 srp_dev->fmr_max_size = srp_dev->fmr_page_size * SRP_FMR_SIZE;
Roland Dreierf5358a12006-06-17 20:37:29 -07002530
2531 INIT_LIST_HEAD(&srp_dev->dev_list);
2532
2533 srp_dev->dev = device;
2534 srp_dev->pd = ib_alloc_pd(device);
2535 if (IS_ERR(srp_dev->pd))
2536 goto free_dev;
2537
2538 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
2539 IB_ACCESS_LOCAL_WRITE |
2540 IB_ACCESS_REMOTE_READ |
2541 IB_ACCESS_REMOTE_WRITE);
2542 if (IS_ERR(srp_dev->mr))
2543 goto err_pd;
2544
David Dillowbe8b9812011-01-18 21:58:09 -05002545 for (max_pages_per_fmr = SRP_FMR_SIZE;
2546 max_pages_per_fmr >= SRP_FMR_MIN_SIZE;
2547 max_pages_per_fmr /= 2, srp_dev->fmr_max_size /= 2) {
2548 memset(&fmr_param, 0, sizeof fmr_param);
2549 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
2550 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
2551 fmr_param.cache = 1;
2552 fmr_param.max_pages_per_fmr = max_pages_per_fmr;
2553 fmr_param.page_shift = fmr_page_shift;
2554 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
2555 IB_ACCESS_REMOTE_WRITE |
2556 IB_ACCESS_REMOTE_READ);
Roland Dreierf5358a12006-06-17 20:37:29 -07002557
David Dillowbe8b9812011-01-18 21:58:09 -05002558 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
2559 if (!IS_ERR(srp_dev->fmr_pool))
2560 break;
2561 }
2562
Roland Dreierf5358a12006-06-17 20:37:29 -07002563 if (IS_ERR(srp_dev->fmr_pool))
2564 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002565
Tom Tucker07ebafb2006-08-03 16:02:42 -05002566 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002567 s = 0;
2568 e = 0;
2569 } else {
2570 s = 1;
2571 e = device->phys_port_cnt;
2572 }
2573
2574 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07002575 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002576 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07002577 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002578 }
2579
Roland Dreierf5358a12006-06-17 20:37:29 -07002580 ib_set_client_data(device, &srp_client, srp_dev);
2581
2582 goto free_attr;
2583
2584err_pd:
2585 ib_dealloc_pd(srp_dev->pd);
2586
2587free_dev:
2588 kfree(srp_dev);
2589
2590free_attr:
2591 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002592}
2593
2594static void srp_remove_one(struct ib_device *device)
2595{
Roland Dreierf5358a12006-06-17 20:37:29 -07002596 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002597 struct srp_host *host, *tmp_host;
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002598 struct srp_target_port *target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002599
Roland Dreierf5358a12006-06-17 20:37:29 -07002600 srp_dev = ib_get_client_data(device, &srp_client);
Dotan Barak1fe0cb82013-06-12 15:20:36 +02002601 if (!srp_dev)
2602 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002603
Roland Dreierf5358a12006-06-17 20:37:29 -07002604 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Tony Jonesee959b02008-02-22 00:13:36 +01002605 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002606 /*
2607 * Wait for the sysfs entry to go away, so that no new
2608 * target ports can be created.
2609 */
2610 wait_for_completion(&host->released);
2611
2612 /*
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002613 * Remove all target ports.
Roland Dreieraef9ec32005-11-02 14:07:13 -08002614 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002615 spin_lock(&host->target_lock);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002616 list_for_each_entry(target, &host->target_list, list)
2617 srp_queue_remove_work(target);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002618 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002619
2620 /*
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002621 * Wait for target port removal tasks.
Roland Dreieraef9ec32005-11-02 14:07:13 -08002622 */
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002623 flush_workqueue(system_long_wq);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002624
Roland Dreieraef9ec32005-11-02 14:07:13 -08002625 kfree(host);
2626 }
2627
Roland Dreierf5358a12006-06-17 20:37:29 -07002628 if (srp_dev->fmr_pool)
2629 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2630 ib_dereg_mr(srp_dev->mr);
2631 ib_dealloc_pd(srp_dev->pd);
2632
2633 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002634}
2635
FUJITA Tomonori32368222007-06-27 16:33:12 +09002636static struct srp_function_template ib_srp_transport_functions = {
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02002637 .rport_delete = srp_rport_delete,
FUJITA Tomonori32368222007-06-27 16:33:12 +09002638};
2639
Roland Dreieraef9ec32005-11-02 14:07:13 -08002640static int __init srp_init_module(void)
2641{
2642 int ret;
2643
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05002644 BUILD_BUG_ON(FIELD_SIZEOF(struct ib_wc, wr_id) < sizeof(void *));
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00002645
David Dillow49248642011-01-14 18:23:24 -05002646 if (srp_sg_tablesize) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002647 pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n");
David Dillow49248642011-01-14 18:23:24 -05002648 if (!cmd_sg_entries)
2649 cmd_sg_entries = srp_sg_tablesize;
2650 }
2651
2652 if (!cmd_sg_entries)
2653 cmd_sg_entries = SRP_DEF_SG_TABLESIZE;
2654
2655 if (cmd_sg_entries > 255) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002656 pr_warn("Clamping cmd_sg_entries to 255\n");
David Dillow49248642011-01-14 18:23:24 -05002657 cmd_sg_entries = 255;
David Dillow1e89a192008-04-16 21:01:12 -07002658 }
2659
David Dillowc07d4242011-01-16 13:57:10 -05002660 if (!indirect_sg_entries)
2661 indirect_sg_entries = cmd_sg_entries;
2662 else if (indirect_sg_entries < cmd_sg_entries) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002663 pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n",
2664 cmd_sg_entries);
David Dillowc07d4242011-01-16 13:57:10 -05002665 indirect_sg_entries = cmd_sg_entries;
2666 }
2667
FUJITA Tomonori32368222007-06-27 16:33:12 +09002668 ib_srp_transport_template =
2669 srp_attach_transport(&ib_srp_transport_functions);
2670 if (!ib_srp_transport_template)
2671 return -ENOMEM;
2672
Roland Dreieraef9ec32005-11-02 14:07:13 -08002673 ret = class_register(&srp_class);
2674 if (ret) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002675 pr_err("couldn't register class infiniband_srp\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002676 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002677 return ret;
2678 }
2679
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002680 ib_sa_register_client(&srp_sa_client);
2681
Roland Dreieraef9ec32005-11-02 14:07:13 -08002682 ret = ib_register_client(&srp_client);
2683 if (ret) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002684 pr_err("couldn't register IB client\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002685 srp_release_transport(ib_srp_transport_template);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002686 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002687 class_unregister(&srp_class);
2688 return ret;
2689 }
2690
2691 return 0;
2692}
2693
2694static void __exit srp_cleanup_module(void)
2695{
2696 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002697 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002698 class_unregister(&srp_class);
FUJITA Tomonori32368222007-06-27 16:33:12 +09002699 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002700}
2701
2702module_init(srp_init_module);
2703module_exit(srp_cleanup_module);