blob: bc13c8db7fc2a0048c5d05bf272f53abcc985dbc [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 ": "
56#define DRV_VERSION "0.2"
57#define DRV_RELDATE "November 1, 2005"
58
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 Kondapalli3d1ff48d2007-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 Kondapalli3d1ff48d2007-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,
234 srp_recv_completion, NULL, target, SRP_RQ_SIZE, 0);
235 if (IS_ERR(recv_cq)) {
236 ret = PTR_ERR(recv_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800237 goto err;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800238 }
239
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100240 send_cq = ib_create_cq(target->srp_host->srp_dev->dev,
241 srp_send_completion, NULL, target, SRP_SQ_SIZE, 0);
242 if (IS_ERR(send_cq)) {
243 ret = PTR_ERR(send_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800244 goto err_recv_cq;
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000245 }
246
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100247 ib_req_notify_cq(recv_cq, IB_CQ_NEXT_COMP);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800248
249 init_attr->event_handler = srp_qp_event;
250 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
251 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
252 init_attr->cap.max_recv_sge = 1;
253 init_attr->cap.max_send_sge = 1;
254 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
255 init_attr->qp_type = IB_QPT_RC;
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100256 init_attr->send_cq = send_cq;
257 init_attr->recv_cq = recv_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800258
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100259 qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr);
260 if (IS_ERR(qp)) {
261 ret = PTR_ERR(qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800262 goto err_send_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800263 }
264
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100265 ret = srp_init_qp(target, qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800266 if (ret)
267 goto err_qp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800268
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100269 if (target->qp)
270 ib_destroy_qp(target->qp);
271 if (target->recv_cq)
272 ib_destroy_cq(target->recv_cq);
273 if (target->send_cq)
274 ib_destroy_cq(target->send_cq);
275
276 target->qp = qp;
277 target->recv_cq = recv_cq;
278 target->send_cq = send_cq;
279
Roland Dreierda9d2f02010-02-24 15:07:59 -0800280 kfree(init_attr);
281 return 0;
282
283err_qp:
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100284 ib_destroy_qp(qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800285
286err_send_cq:
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100287 ib_destroy_cq(send_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800288
289err_recv_cq:
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100290 ib_destroy_cq(recv_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800291
292err:
Roland Dreieraef9ec32005-11-02 14:07:13 -0800293 kfree(init_attr);
294 return ret;
295}
296
297static void srp_free_target_ib(struct srp_target_port *target)
298{
299 int i;
300
301 ib_destroy_qp(target->qp);
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000302 ib_destroy_cq(target->send_cq);
303 ib_destroy_cq(target->recv_cq);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800304
Ishai Rabinovitz73aa89e2012-11-26 11:44:53 +0100305 target->qp = NULL;
306 target->send_cq = target->recv_cq = NULL;
307
Roland Dreieraef9ec32005-11-02 14:07:13 -0800308 for (i = 0; i < SRP_RQ_SIZE; ++i)
309 srp_free_iu(target->srp_host, target->rx_ring[i]);
Bart Van Asschedd5e6e32010-08-30 19:27:20 +0000310 for (i = 0; i < SRP_SQ_SIZE; ++i)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800311 srp_free_iu(target->srp_host, target->tx_ring[i]);
312}
313
314static void srp_path_rec_completion(int status,
315 struct ib_sa_path_rec *pathrec,
316 void *target_ptr)
317{
318 struct srp_target_port *target = target_ptr;
319
320 target->status = status;
321 if (status)
David Dillow7aa54bd2008-01-07 18:23:41 -0500322 shost_printk(KERN_ERR, target->scsi_host,
323 PFX "Got failed path rec status %d\n", status);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800324 else
325 target->path = *pathrec;
326 complete(&target->done);
327}
328
329static int srp_lookup_path(struct srp_target_port *target)
330{
331 target->path.numb_path = 1;
332
333 init_completion(&target->done);
334
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700335 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100336 target->srp_host->srp_dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800337 target->srp_host->port,
338 &target->path,
Sean Hefty247e0202007-08-08 15:51:18 -0700339 IB_SA_PATH_REC_SERVICE_ID |
Roland Dreieraef9ec32005-11-02 14:07:13 -0800340 IB_SA_PATH_REC_DGID |
341 IB_SA_PATH_REC_SGID |
342 IB_SA_PATH_REC_NUMB_PATH |
343 IB_SA_PATH_REC_PKEY,
344 SRP_PATH_REC_TIMEOUT_MS,
345 GFP_KERNEL,
346 srp_path_rec_completion,
347 target, &target->path_query);
348 if (target->path_query_id < 0)
349 return target->path_query_id;
350
351 wait_for_completion(&target->done);
352
353 if (target->status < 0)
David Dillow7aa54bd2008-01-07 18:23:41 -0500354 shost_printk(KERN_WARNING, target->scsi_host,
355 PFX "Path record query failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800356
357 return target->status;
358}
359
360static int srp_send_req(struct srp_target_port *target)
361{
362 struct {
363 struct ib_cm_req_param param;
364 struct srp_login_req priv;
365 } *req = NULL;
366 int status;
367
368 req = kzalloc(sizeof *req, GFP_KERNEL);
369 if (!req)
370 return -ENOMEM;
371
372 req->param.primary_path = &target->path;
373 req->param.alternate_path = NULL;
374 req->param.service_id = target->service_id;
375 req->param.qp_num = target->qp->qp_num;
376 req->param.qp_type = target->qp->qp_type;
377 req->param.private_data = &req->priv;
378 req->param.private_data_len = sizeof req->priv;
379 req->param.flow_control = 1;
380
381 get_random_bytes(&req->param.starting_psn, 4);
382 req->param.starting_psn &= 0xffffff;
383
384 /*
385 * Pick some arbitrary defaults here; we could make these
386 * module parameters if anyone cared about setting them.
387 */
388 req->param.responder_resources = 4;
389 req->param.remote_cm_response_timeout = 20;
390 req->param.local_cm_response_timeout = 20;
391 req->param.retry_count = 7;
392 req->param.rnr_retry_count = 7;
393 req->param.max_cm_retries = 15;
394
395 req->priv.opcode = SRP_LOGIN_REQ;
396 req->priv.tag = 0;
David Dillow49248642011-01-14 18:23:24 -0500397 req->priv.req_it_iu_len = cpu_to_be32(target->max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800398 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
399 SRP_BUF_FORMAT_INDIRECT);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700400 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700401 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450db2006-06-17 20:37:38 -0700402 * port identifier format is 8 bytes of ID extension followed
403 * by 8 bytes of GUID. Older drafts put the two halves in the
404 * opposite order, so that the GUID comes first.
405 *
406 * Targets conforming to these obsolete drafts can be
407 * recognized by the I/O Class they report.
408 */
409 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
410 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200411 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700412 memcpy(req->priv.initiator_port_id + 8,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200413 &target->initiator_ext, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700414 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
415 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
416 } else {
417 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200418 &target->initiator_ext, 8);
419 memcpy(req->priv.initiator_port_id + 8,
420 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700421 memcpy(req->priv.target_port_id, &target->id_ext, 8);
422 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
423 }
424
Roland Dreieraef9ec32005-11-02 14:07:13 -0800425 /*
426 * Topspin/Cisco SRP targets will reject our login unless we
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200427 * zero out the first 8 bytes of our initiator port ID and set
428 * the second 8 bytes to the local node GUID.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800429 */
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700430 if (srp_target_is_topspin(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500431 shost_printk(KERN_DEBUG, target->scsi_host,
432 PFX "Topspin/Cisco initiator port ID workaround "
433 "activated for target GUID %016llx\n",
434 (unsigned long long) be64_to_cpu(target->ioc_guid));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800435 memset(req->priv.initiator_port_id, 0, 8);
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200436 memcpy(req->priv.initiator_port_id + 8,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100437 &target->srp_host->srp_dev->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800438 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800439
440 status = ib_send_cm_req(target->cm_id, &req->param);
441
442 kfree(req);
443
444 return status;
445}
446
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000447static bool srp_queue_remove_work(struct srp_target_port *target)
448{
449 bool changed = false;
450
451 spin_lock_irq(&target->lock);
452 if (target->state != SRP_TARGET_REMOVED) {
453 target->state = SRP_TARGET_REMOVED;
454 changed = true;
455 }
456 spin_unlock_irq(&target->lock);
457
458 if (changed)
459 queue_work(system_long_wq, &target->remove_work);
460
461 return changed;
462}
463
Bart Van Assche294c8752011-12-25 12:18:12 +0000464static bool srp_change_conn_state(struct srp_target_port *target,
465 bool connected)
466{
467 bool changed = false;
468
469 spin_lock_irq(&target->lock);
470 if (target->connected != connected) {
471 target->connected = connected;
472 changed = true;
473 }
474 spin_unlock_irq(&target->lock);
475
476 return changed;
477}
478
Roland Dreieraef9ec32005-11-02 14:07:13 -0800479static void srp_disconnect_target(struct srp_target_port *target)
480{
Bart Van Assche294c8752011-12-25 12:18:12 +0000481 if (srp_change_conn_state(target, false)) {
482 /* XXX should send SRP_I_LOGOUT request */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800483
Bart Van Assche294c8752011-12-25 12:18:12 +0000484 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
485 shost_printk(KERN_DEBUG, target->scsi_host,
486 PFX "Sending CM DREQ failed\n");
Bart Van Assche294c8752011-12-25 12:18:12 +0000487 }
Roland Dreiere6581052006-05-17 09:13:21 -0700488 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800489}
490
David Dillow8f26c9f2011-01-14 19:45:50 -0500491static void srp_free_req_data(struct srp_target_port *target)
492{
David Dillowc07d4242011-01-16 13:57:10 -0500493 struct ib_device *ibdev = target->srp_host->srp_dev->dev;
David Dillow8f26c9f2011-01-14 19:45:50 -0500494 struct srp_request *req;
495 int i;
496
497 for (i = 0, req = target->req_ring; i < SRP_CMD_SQ_SIZE; ++i, ++req) {
498 kfree(req->fmr_list);
499 kfree(req->map_page);
David Dillowc07d4242011-01-16 13:57:10 -0500500 if (req->indirect_dma_addr) {
501 ib_dma_unmap_single(ibdev, req->indirect_dma_addr,
502 target->indirect_size,
503 DMA_TO_DEVICE);
504 }
505 kfree(req->indirect_desc);
David Dillow8f26c9f2011-01-14 19:45:50 -0500506 }
507}
508
Bart Van Assche683b1592012-01-14 12:40:44 +0000509/**
510 * srp_del_scsi_host_attr() - Remove attributes defined in the host template.
511 * @shost: SCSI host whose attributes to remove from sysfs.
512 *
513 * Note: Any attributes defined in the host template and that did not exist
514 * before invocation of this function will be ignored.
515 */
516static void srp_del_scsi_host_attr(struct Scsi_Host *shost)
517{
518 struct device_attribute **attr;
519
520 for (attr = shost->hostt->shost_attrs; attr && *attr; ++attr)
521 device_remove_file(&shost->shost_dev, *attr);
522}
523
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000524static void srp_remove_target(struct srp_target_port *target)
525{
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000526 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
527
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000528 srp_del_scsi_host_attr(target->scsi_host);
529 srp_remove_host(target->scsi_host);
530 scsi_remove_host(target->scsi_host);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000531 srp_disconnect_target(target);
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000532 ib_destroy_cm_id(target->cm_id);
533 srp_free_target_ib(target);
534 srp_free_req_data(target);
535 scsi_host_put(target->scsi_host);
536}
537
David Howellsc4028952006-11-22 14:57:56 +0000538static void srp_remove_work(struct work_struct *work)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800539{
David Howellsc4028952006-11-22 14:57:56 +0000540 struct srp_target_port *target =
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000541 container_of(work, struct srp_target_port, remove_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800542
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000543 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800544
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700545 spin_lock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800546 list_del(&target->list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700547 spin_unlock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800548
Bart Van Asscheee12d6a2011-12-25 19:41:07 +0000549 srp_remove_target(target);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800550}
551
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +0200552static void srp_rport_delete(struct srp_rport *rport)
553{
554 struct srp_target_port *target = rport->lld_data;
555
556 srp_queue_remove_work(target);
557}
558
Roland Dreieraef9ec32005-11-02 14:07:13 -0800559static int srp_connect_target(struct srp_target_port *target)
560{
David Dillow9fe4bcf2008-01-08 17:08:52 -0500561 int retries = 3;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800562 int ret;
563
Bart Van Assche294c8752011-12-25 12:18:12 +0000564 WARN_ON_ONCE(target->connected);
565
Bart Van Assche948d1e82011-09-03 09:25:42 +0200566 target->qp_in_error = false;
567
Roland Dreieraef9ec32005-11-02 14:07:13 -0800568 ret = srp_lookup_path(target);
569 if (ret)
570 return ret;
571
572 while (1) {
573 init_completion(&target->done);
574 ret = srp_send_req(target);
575 if (ret)
576 return ret;
577 wait_for_completion(&target->done);
578
579 /*
580 * The CM event handling code will set status to
581 * SRP_PORT_REDIRECT if we get a port redirect REJ
582 * back, or SRP_DLID_REDIRECT if we get a lid/qp
583 * redirect REJ back.
584 */
585 switch (target->status) {
586 case 0:
Bart Van Assche294c8752011-12-25 12:18:12 +0000587 srp_change_conn_state(target, true);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800588 return 0;
589
590 case SRP_PORT_REDIRECT:
591 ret = srp_lookup_path(target);
592 if (ret)
593 return ret;
594 break;
595
596 case SRP_DLID_REDIRECT:
597 break;
598
David Dillow9fe4bcf2008-01-08 17:08:52 -0500599 case SRP_STALE_CONN:
600 /* Our current CM id was stale, and is now in timewait.
601 * Try to reconnect with a new one.
602 */
603 if (!retries-- || srp_new_cm_id(target)) {
604 shost_printk(KERN_ERR, target->scsi_host, PFX
605 "giving up on stale connection\n");
606 target->status = -ECONNRESET;
607 return target->status;
608 }
609
610 shost_printk(KERN_ERR, target->scsi_host, PFX
611 "retrying stale connection\n");
612 break;
613
Roland Dreieraef9ec32005-11-02 14:07:13 -0800614 default:
615 return target->status;
616 }
617 }
618}
619
Roland Dreierd945e1d2006-05-09 10:50:28 -0700620static void srp_unmap_data(struct scsi_cmnd *scmnd,
621 struct srp_target_port *target,
622 struct srp_request *req)
623{
David Dillow8f26c9f2011-01-14 19:45:50 -0500624 struct ib_device *ibdev = target->srp_host->srp_dev->dev;
625 struct ib_pool_fmr **pfmr;
626
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900627 if (!scsi_sglist(scmnd) ||
Roland Dreierd945e1d2006-05-09 10:50:28 -0700628 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
629 scmnd->sc_data_direction != DMA_FROM_DEVICE))
630 return;
631
David Dillow8f26c9f2011-01-14 19:45:50 -0500632 pfmr = req->fmr_list;
633 while (req->nfmr--)
634 ib_fmr_pool_unmap(*pfmr++);
Roland Dreierf5358a12006-06-17 20:37:29 -0700635
David Dillow8f26c9f2011-01-14 19:45:50 -0500636 ib_dma_unmap_sg(ibdev, scsi_sglist(scmnd), scsi_sg_count(scmnd),
637 scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700638}
639
Bart Van Assche22032992012-08-14 13:18:53 +0000640/**
641 * srp_claim_req - Take ownership of the scmnd associated with a request.
642 * @target: SRP target port.
643 * @req: SRP request.
644 * @scmnd: If NULL, take ownership of @req->scmnd. If not NULL, only take
645 * ownership of @req->scmnd if it equals @scmnd.
646 *
647 * Return value:
648 * Either NULL or a pointer to the SCSI command the caller became owner of.
649 */
650static struct scsi_cmnd *srp_claim_req(struct srp_target_port *target,
651 struct srp_request *req,
652 struct scsi_cmnd *scmnd)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700653{
Bart Van Assche94a91742010-11-26 14:50:09 -0500654 unsigned long flags;
655
Bart Van Assche22032992012-08-14 13:18:53 +0000656 spin_lock_irqsave(&target->lock, flags);
657 if (!scmnd) {
658 scmnd = req->scmnd;
659 req->scmnd = NULL;
660 } else if (req->scmnd == scmnd) {
661 req->scmnd = NULL;
662 } else {
663 scmnd = NULL;
664 }
665 spin_unlock_irqrestore(&target->lock, flags);
666
667 return scmnd;
668}
669
670/**
671 * srp_free_req() - Unmap data and add request to the free request list.
672 */
673static void srp_free_req(struct srp_target_port *target,
674 struct srp_request *req, struct scsi_cmnd *scmnd,
675 s32 req_lim_delta)
676{
677 unsigned long flags;
678
679 srp_unmap_data(scmnd, target, req);
680
Bart Van Asschee9684672010-11-26 15:08:38 -0500681 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -0500682 target->req_lim += req_lim_delta;
Bart Van Assche536ae142010-11-26 13:58:27 -0500683 list_add_tail(&req->list, &target->free_reqs);
Bart Van Asschee9684672010-11-26 15:08:38 -0500684 spin_unlock_irqrestore(&target->lock, flags);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700685}
686
687static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
688{
Bart Van Assche22032992012-08-14 13:18:53 +0000689 struct scsi_cmnd *scmnd = srp_claim_req(target, req, NULL);
690
691 if (scmnd) {
Bart Van Assche9b796d02012-08-24 10:27:54 +0000692 srp_free_req(target, req, scmnd, 0);
Bart Van Assche22032992012-08-14 13:18:53 +0000693 scmnd->result = DID_RESET << 16;
694 scmnd->scsi_done(scmnd);
Bart Van Assche22032992012-08-14 13:18:53 +0000695 }
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700696}
697
Roland Dreieraef9ec32005-11-02 14:07:13 -0800698static int srp_reconnect_target(struct srp_target_port *target)
699{
Bart Van Assche09be70a2012-03-17 17:18:54 +0000700 struct Scsi_Host *shost = target->scsi_host;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -0500701 int i, ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800702
Bart Van Assche09be70a2012-03-17 17:18:54 +0000703 scsi_target_block(&shost->shost_gendev);
704
Roland Dreieraef9ec32005-11-02 14:07:13 -0800705 srp_disconnect_target(target);
706 /*
Bart Van Asschec7c4e7f2013-02-21 17:19:04 +0000707 * Now get a new local CM ID so that we avoid confusing the target in
708 * case things are really fouled up. Doing so also ensures that all CM
709 * callbacks will have finished before a new QP is allocated.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800710 */
David Dillow9fe4bcf2008-01-08 17:08:52 -0500711 ret = srp_new_cm_id(target);
Bart Van Asschec7c4e7f2013-02-21 17:19:04 +0000712 /*
713 * Whether or not creating a new CM ID succeeded, create a new
714 * QP. This guarantees that all completion callback function
715 * invocations have finished before request resetting starts.
716 */
717 if (ret == 0)
718 ret = srp_create_target_ib(target);
719 else
720 srp_create_target_ib(target);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800721
Bart Van Assche536ae142010-11-26 13:58:27 -0500722 for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
723 struct srp_request *req = &target->req_ring[i];
724 if (req->scmnd)
725 srp_reset_req(target, req);
726 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800727
Bart Van Assche536ae142010-11-26 13:58:27 -0500728 INIT_LIST_HEAD(&target->free_tx);
Bart Van Asschedcb4cb82010-11-26 13:22:48 -0500729 for (i = 0; i < SRP_SQ_SIZE; ++i)
Bart Van Assche536ae142010-11-26 13:58:27 -0500730 list_add(&target->tx_ring[i]->list, &target->free_tx);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800731
Bart Van Asschec7c4e7f2013-02-21 17:19:04 +0000732 if (ret == 0)
733 ret = srp_connect_target(target);
Bart Van Assche09be70a2012-03-17 17:18:54 +0000734
Bart Van Assche09be70a2012-03-17 17:18:54 +0000735 scsi_target_unblock(&shost->shost_gendev, ret == 0 ? SDEV_RUNNING :
736 SDEV_TRANSPORT_OFFLINE);
Bart Van Assche2ce19e72013-02-21 17:20:00 +0000737 target->transport_offline = !!ret;
Bart Van Assche09be70a2012-03-17 17:18:54 +0000738
Roland Dreieraef9ec32005-11-02 14:07:13 -0800739 if (ret)
740 goto err;
741
Bart Van Assche09be70a2012-03-17 17:18:54 +0000742 shost_printk(KERN_INFO, target->scsi_host, PFX "reconnect succeeded\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800743
744 return ret;
745
746err:
David Dillow7aa54bd2008-01-07 18:23:41 -0500747 shost_printk(KERN_ERR, target->scsi_host,
748 PFX "reconnect failed (%d), removing target port.\n", ret);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800749
750 /*
751 * We couldn't reconnect, so kill our target port off.
Bart Van Assche9709f0e2010-11-26 13:13:06 -0500752 * However, we have to defer the real removal because we
753 * are in the context of the SCSI error handler now, which
754 * will deadlock if we call scsi_remove_host().
Roland Dreieraef9ec32005-11-02 14:07:13 -0800755 */
Bart Van Asscheef6c49d2011-12-26 16:49:18 +0000756 srp_queue_remove_work(target);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800757
758 return ret;
759}
760
David Dillow8f26c9f2011-01-14 19:45:50 -0500761static void srp_map_desc(struct srp_map_state *state, dma_addr_t dma_addr,
762 unsigned int dma_len, u32 rkey)
Roland Dreierf5358a12006-06-17 20:37:29 -0700763{
David Dillow8f26c9f2011-01-14 19:45:50 -0500764 struct srp_direct_buf *desc = state->desc;
765
766 desc->va = cpu_to_be64(dma_addr);
767 desc->key = cpu_to_be32(rkey);
768 desc->len = cpu_to_be32(dma_len);
769
770 state->total_len += dma_len;
771 state->desc++;
772 state->ndesc++;
773}
774
775static int srp_map_finish_fmr(struct srp_map_state *state,
776 struct srp_target_port *target)
777{
778 struct srp_device *dev = target->srp_host->srp_dev;
779 struct ib_pool_fmr *fmr;
Roland Dreierf5358a12006-06-17 20:37:29 -0700780 u64 io_addr = 0;
David Dillow8f26c9f2011-01-14 19:45:50 -0500781
782 if (!state->npages)
783 return 0;
784
785 if (state->npages == 1) {
786 srp_map_desc(state, state->base_dma_addr, state->fmr_len,
787 target->rkey);
788 state->npages = state->fmr_len = 0;
789 return 0;
790 }
791
792 fmr = ib_fmr_pool_map_phys(dev->fmr_pool, state->pages,
793 state->npages, io_addr);
794 if (IS_ERR(fmr))
795 return PTR_ERR(fmr);
796
797 *state->next_fmr++ = fmr;
798 state->nfmr++;
799
800 srp_map_desc(state, 0, state->fmr_len, fmr->fmr->rkey);
801 state->npages = state->fmr_len = 0;
802 return 0;
803}
804
805static void srp_map_update_start(struct srp_map_state *state,
806 struct scatterlist *sg, int sg_index,
807 dma_addr_t dma_addr)
808{
809 state->unmapped_sg = sg;
810 state->unmapped_index = sg_index;
811 state->unmapped_addr = dma_addr;
812}
813
814static int srp_map_sg_entry(struct srp_map_state *state,
815 struct srp_target_port *target,
816 struct scatterlist *sg, int sg_index,
817 int use_fmr)
818{
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100819 struct srp_device *dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800820 struct ib_device *ibdev = dev->dev;
David Dillow8f26c9f2011-01-14 19:45:50 -0500821 dma_addr_t dma_addr = ib_sg_dma_address(ibdev, sg);
822 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
823 unsigned int len;
824 int ret;
Roland Dreierf5358a12006-06-17 20:37:29 -0700825
David Dillow8f26c9f2011-01-14 19:45:50 -0500826 if (!dma_len)
827 return 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700828
David Dillow8f26c9f2011-01-14 19:45:50 -0500829 if (use_fmr == SRP_MAP_NO_FMR) {
830 /* Once we're in direct map mode for a request, we don't
831 * go back to FMR mode, so no need to update anything
832 * other than the descriptor.
833 */
834 srp_map_desc(state, dma_addr, dma_len, target->rkey);
835 return 0;
836 }
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700837
David Dillow8f26c9f2011-01-14 19:45:50 -0500838 /* If we start at an offset into the FMR page, don't merge into
839 * the current FMR. Finish it out, and use the kernel's MR for this
840 * sg entry. This is to avoid potential bugs on some SRP targets
841 * that were never quite defined, but went away when the initiator
842 * avoided using FMR on such page fragments.
843 */
844 if (dma_addr & ~dev->fmr_page_mask || dma_len > dev->fmr_max_size) {
845 ret = srp_map_finish_fmr(state, target);
846 if (ret)
847 return ret;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800848
David Dillow8f26c9f2011-01-14 19:45:50 -0500849 srp_map_desc(state, dma_addr, dma_len, target->rkey);
850 srp_map_update_start(state, NULL, 0, 0);
851 return 0;
852 }
853
854 /* If this is the first sg to go into the FMR, save our position.
855 * We need to know the first unmapped entry, its index, and the
856 * first unmapped address within that entry to be able to restart
857 * mapping after an error.
858 */
859 if (!state->unmapped_sg)
860 srp_map_update_start(state, sg, sg_index, dma_addr);
861
862 while (dma_len) {
863 if (state->npages == SRP_FMR_SIZE) {
864 ret = srp_map_finish_fmr(state, target);
865 if (ret)
866 return ret;
867
868 srp_map_update_start(state, sg, sg_index, dma_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -0700869 }
870
David Dillow8f26c9f2011-01-14 19:45:50 -0500871 len = min_t(unsigned int, dma_len, dev->fmr_page_size);
872
873 if (!state->npages)
874 state->base_dma_addr = dma_addr;
875 state->pages[state->npages++] = dma_addr;
876 state->fmr_len += len;
877 dma_addr += len;
878 dma_len -= len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700879 }
880
David Dillow8f26c9f2011-01-14 19:45:50 -0500881 /* If the last entry of the FMR wasn't a full page, then we need to
882 * close it out and start a new one -- we can only merge at page
883 * boundries.
884 */
Roland Dreierf5358a12006-06-17 20:37:29 -0700885 ret = 0;
David Dillow8f26c9f2011-01-14 19:45:50 -0500886 if (len != dev->fmr_page_size) {
887 ret = srp_map_finish_fmr(state, target);
888 if (!ret)
889 srp_map_update_start(state, NULL, 0, 0);
890 }
Roland Dreierf5358a12006-06-17 20:37:29 -0700891 return ret;
892}
893
Roland Dreieraef9ec32005-11-02 14:07:13 -0800894static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
895 struct srp_request *req)
896{
David Dillow8f26c9f2011-01-14 19:45:50 -0500897 struct scatterlist *scat, *sg;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800898 struct srp_cmd *cmd = req->cmd->buf;
David Dillow8f26c9f2011-01-14 19:45:50 -0500899 int i, len, nents, count, use_fmr;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800900 struct srp_device *dev;
901 struct ib_device *ibdev;
David Dillow8f26c9f2011-01-14 19:45:50 -0500902 struct srp_map_state state;
903 struct srp_indirect_buf *indirect_hdr;
David Dillow8f26c9f2011-01-14 19:45:50 -0500904 u32 table_len;
905 u8 fmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800906
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900907 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800908 return sizeof (struct srp_cmd);
909
910 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
911 scmnd->sc_data_direction != DMA_TO_DEVICE) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500912 shost_printk(KERN_WARNING, target->scsi_host,
913 PFX "Unhandled data direction %d\n",
914 scmnd->sc_data_direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800915 return -EINVAL;
916 }
917
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900918 nents = scsi_sg_count(scmnd);
919 scat = scsi_sglist(scmnd);
Roland Dreiercf368712006-03-24 15:47:26 -0800920
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100921 dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800922 ibdev = dev->dev;
923
924 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
David Dillow8f26c9f2011-01-14 19:45:50 -0500925 if (unlikely(count == 0))
926 return -EIO;
Roland Dreierf5358a12006-06-17 20:37:29 -0700927
928 fmt = SRP_DATA_DESC_DIRECT;
929 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800930
931 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700932 /*
933 * The midlayer only generated a single gather/scatter
934 * entry, or DMA mapping coalesced everything to a
935 * single entry. So a direct descriptor along with
936 * the DMA MR suffices.
937 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800938 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800939
Ralph Campbell85507bc2006-12-12 14:30:55 -0800940 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
David Dillow9af76272010-11-26 15:34:46 -0500941 buf->key = cpu_to_be32(target->rkey);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800942 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Roland Dreiercf368712006-03-24 15:47:26 -0800943
David Dillow8f26c9f2011-01-14 19:45:50 -0500944 req->nfmr = 0;
945 goto map_complete;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800946 }
947
David Dillow8f26c9f2011-01-14 19:45:50 -0500948 /* We have more than one scatter/gather entry, so build our indirect
949 * descriptor table, trying to merge as many entries with FMR as we
950 * can.
951 */
952 indirect_hdr = (void *) cmd->add_data;
953
David Dillowc07d4242011-01-16 13:57:10 -0500954 ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr,
955 target->indirect_size, DMA_TO_DEVICE);
956
David Dillow8f26c9f2011-01-14 19:45:50 -0500957 memset(&state, 0, sizeof(state));
David Dillowc07d4242011-01-16 13:57:10 -0500958 state.desc = req->indirect_desc;
David Dillow8f26c9f2011-01-14 19:45:50 -0500959 state.pages = req->map_page;
960 state.next_fmr = req->fmr_list;
961
962 use_fmr = dev->fmr_pool ? SRP_MAP_ALLOW_FMR : SRP_MAP_NO_FMR;
963
964 for_each_sg(scat, sg, count, i) {
965 if (srp_map_sg_entry(&state, target, sg, i, use_fmr)) {
966 /* FMR mapping failed, so backtrack to the first
967 * unmapped entry and continue on without using FMR.
968 */
969 dma_addr_t dma_addr;
970 unsigned int dma_len;
971
972backtrack:
973 sg = state.unmapped_sg;
974 i = state.unmapped_index;
975
976 dma_addr = ib_sg_dma_address(ibdev, sg);
977 dma_len = ib_sg_dma_len(ibdev, sg);
978 dma_len -= (state.unmapped_addr - dma_addr);
979 dma_addr = state.unmapped_addr;
980 use_fmr = SRP_MAP_NO_FMR;
981 srp_map_desc(&state, dma_addr, dma_len, target->rkey);
982 }
983 }
984
985 if (use_fmr == SRP_MAP_ALLOW_FMR && srp_map_finish_fmr(&state, target))
986 goto backtrack;
987
David Dillowc07d4242011-01-16 13:57:10 -0500988 /* We've mapped the request, now pull as much of the indirect
989 * descriptor table as we can into the command buffer. If this
990 * target is not using an external indirect table, we are
991 * guaranteed to fit into the command, as the SCSI layer won't
992 * give us more S/G entries than we allow.
David Dillow8f26c9f2011-01-14 19:45:50 -0500993 */
994 req->nfmr = state.nfmr;
995 if (state.ndesc == 1) {
996 /* FMR mapping was able to collapse this to one entry,
997 * so use a direct descriptor.
998 */
999 struct srp_direct_buf *buf = (void *) cmd->add_data;
1000
David Dillowc07d4242011-01-16 13:57:10 -05001001 *buf = req->indirect_desc[0];
David Dillow8f26c9f2011-01-14 19:45:50 -05001002 goto map_complete;
1003 }
1004
David Dillowc07d4242011-01-16 13:57:10 -05001005 if (unlikely(target->cmd_sg_cnt < state.ndesc &&
1006 !target->allow_ext_sg)) {
1007 shost_printk(KERN_ERR, target->scsi_host,
1008 "Could not fit S/G list into SRP_CMD\n");
1009 return -EIO;
1010 }
1011
1012 count = min(state.ndesc, target->cmd_sg_cnt);
David Dillow8f26c9f2011-01-14 19:45:50 -05001013 table_len = state.ndesc * sizeof (struct srp_direct_buf);
1014
1015 fmt = SRP_DATA_DESC_INDIRECT;
1016 len = sizeof(struct srp_cmd) + sizeof (struct srp_indirect_buf);
David Dillowc07d4242011-01-16 13:57:10 -05001017 len += count * sizeof (struct srp_direct_buf);
David Dillow8f26c9f2011-01-14 19:45:50 -05001018
David Dillowc07d4242011-01-16 13:57:10 -05001019 memcpy(indirect_hdr->desc_list, req->indirect_desc,
1020 count * sizeof (struct srp_direct_buf));
David Dillow8f26c9f2011-01-14 19:45:50 -05001021
David Dillowc07d4242011-01-16 13:57:10 -05001022 indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr);
David Dillow8f26c9f2011-01-14 19:45:50 -05001023 indirect_hdr->table_desc.key = cpu_to_be32(target->rkey);
1024 indirect_hdr->table_desc.len = cpu_to_be32(table_len);
1025 indirect_hdr->len = cpu_to_be32(state.total_len);
1026
1027 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
David Dillowc07d4242011-01-16 13:57:10 -05001028 cmd->data_out_desc_cnt = count;
David Dillow8f26c9f2011-01-14 19:45:50 -05001029 else
David Dillowc07d4242011-01-16 13:57:10 -05001030 cmd->data_in_desc_cnt = count;
1031
1032 ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len,
1033 DMA_TO_DEVICE);
David Dillow8f26c9f2011-01-14 19:45:50 -05001034
1035map_complete:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001036 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
1037 cmd->buf_fmt = fmt << 4;
1038 else
1039 cmd->buf_fmt = fmt;
1040
Roland Dreieraef9ec32005-11-02 14:07:13 -08001041 return len;
1042}
1043
David Dillow05a1d752010-10-08 14:48:14 -04001044/*
Bart Van Assche76c75b22010-11-26 14:37:47 -05001045 * Return an IU and possible credit to the free pool
1046 */
1047static void srp_put_tx_iu(struct srp_target_port *target, struct srp_iu *iu,
1048 enum srp_iu_type iu_type)
1049{
1050 unsigned long flags;
1051
Bart Van Asschee9684672010-11-26 15:08:38 -05001052 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001053 list_add(&iu->list, &target->free_tx);
1054 if (iu_type != SRP_IU_RSP)
1055 ++target->req_lim;
Bart Van Asschee9684672010-11-26 15:08:38 -05001056 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001057}
1058
1059/*
Bart Van Asschee9684672010-11-26 15:08:38 -05001060 * Must be called with target->lock held to protect req_lim and free_tx.
1061 * If IU is not sent, it must be returned using srp_put_tx_iu().
David Dillow05a1d752010-10-08 14:48:14 -04001062 *
1063 * Note:
1064 * An upper limit for the number of allocated information units for each
1065 * request type is:
1066 * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues
1067 * more than Scsi_Host.can_queue requests.
1068 * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE.
1069 * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than
1070 * one unanswered SRP request to an initiator.
1071 */
1072static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
1073 enum srp_iu_type iu_type)
1074{
1075 s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE;
1076 struct srp_iu *iu;
1077
1078 srp_send_completion(target->send_cq, target);
1079
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001080 if (list_empty(&target->free_tx))
David Dillow05a1d752010-10-08 14:48:14 -04001081 return NULL;
1082
1083 /* Initiator responses to target requests do not consume credits */
Bart Van Assche76c75b22010-11-26 14:37:47 -05001084 if (iu_type != SRP_IU_RSP) {
1085 if (target->req_lim <= rsv) {
1086 ++target->zero_req_lim;
1087 return NULL;
1088 }
1089
1090 --target->req_lim;
David Dillow05a1d752010-10-08 14:48:14 -04001091 }
1092
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001093 iu = list_first_entry(&target->free_tx, struct srp_iu, list);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001094 list_del(&iu->list);
David Dillow05a1d752010-10-08 14:48:14 -04001095 return iu;
1096}
1097
Bart Van Assche76c75b22010-11-26 14:37:47 -05001098static int srp_post_send(struct srp_target_port *target,
1099 struct srp_iu *iu, int len)
David Dillow05a1d752010-10-08 14:48:14 -04001100{
1101 struct ib_sge list;
1102 struct ib_send_wr wr, *bad_wr;
David Dillow05a1d752010-10-08 14:48:14 -04001103
1104 list.addr = iu->dma;
1105 list.length = len;
David Dillow9af76272010-11-26 15:34:46 -05001106 list.lkey = target->lkey;
David Dillow05a1d752010-10-08 14:48:14 -04001107
1108 wr.next = NULL;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001109 wr.wr_id = (uintptr_t) iu;
David Dillow05a1d752010-10-08 14:48:14 -04001110 wr.sg_list = &list;
1111 wr.num_sge = 1;
1112 wr.opcode = IB_WR_SEND;
1113 wr.send_flags = IB_SEND_SIGNALED;
1114
Bart Van Assche76c75b22010-11-26 14:37:47 -05001115 return ib_post_send(target->qp, &wr, &bad_wr);
David Dillow05a1d752010-10-08 14:48:14 -04001116}
1117
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001118static int srp_post_recv(struct srp_target_port *target, struct srp_iu *iu)
Bart Van Asschec996bb42010-07-30 10:59:05 +00001119{
Bart Van Asschec996bb42010-07-30 10:59:05 +00001120 struct ib_recv_wr wr, *bad_wr;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001121 struct ib_sge list;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001122
1123 list.addr = iu->dma;
1124 list.length = iu->size;
David Dillow9af76272010-11-26 15:34:46 -05001125 list.lkey = target->lkey;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001126
1127 wr.next = NULL;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001128 wr.wr_id = (uintptr_t) iu;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001129 wr.sg_list = &list;
1130 wr.num_sge = 1;
1131
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001132 return ib_post_recv(target->qp, &wr, &bad_wr);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001133}
1134
Roland Dreieraef9ec32005-11-02 14:07:13 -08001135static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
1136{
1137 struct srp_request *req;
1138 struct scsi_cmnd *scmnd;
1139 unsigned long flags;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001140
Roland Dreieraef9ec32005-11-02 14:07:13 -08001141 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
Bart Van Asschee9684672010-11-26 15:08:38 -05001142 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -05001143 target->req_lim += be32_to_cpu(rsp->req_lim_delta);
Bart Van Asschee9684672010-11-26 15:08:38 -05001144 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -05001145
David Dillowf8b6e312010-11-26 13:02:21 -05001146 target->tsk_mgmt_status = -1;
1147 if (be32_to_cpu(rsp->resp_data_len) >= 4)
1148 target->tsk_mgmt_status = rsp->data[3];
1149 complete(&target->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001150 } else {
David Dillowf8b6e312010-11-26 13:02:21 -05001151 req = &target->req_ring[rsp->tag];
Bart Van Assche22032992012-08-14 13:18:53 +00001152 scmnd = srp_claim_req(target, req, NULL);
1153 if (!scmnd) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001154 shost_printk(KERN_ERR, target->scsi_host,
1155 "Null scmnd for RSP w/tag %016llx\n",
1156 (unsigned long long) rsp->tag);
Bart Van Assche22032992012-08-14 13:18:53 +00001157
1158 spin_lock_irqsave(&target->lock, flags);
1159 target->req_lim += be32_to_cpu(rsp->req_lim_delta);
1160 spin_unlock_irqrestore(&target->lock, flags);
1161
1162 return;
1163 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001164 scmnd->result = rsp->status;
1165
1166 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
1167 memcpy(scmnd->sense_buffer, rsp->data +
1168 be32_to_cpu(rsp->resp_data_len),
1169 min_t(int, be32_to_cpu(rsp->sense_data_len),
1170 SCSI_SENSE_BUFFERSIZE));
1171 }
1172
1173 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001174 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001175 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001176 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001177
Bart Van Assche22032992012-08-14 13:18:53 +00001178 srp_free_req(target, req, scmnd,
1179 be32_to_cpu(rsp->req_lim_delta));
1180
David Dillowf8b6e312010-11-26 13:02:21 -05001181 scmnd->host_scribble = NULL;
1182 scmnd->scsi_done(scmnd);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001183 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001184}
1185
David Dillowbb125882010-10-08 14:40:47 -04001186static int srp_response_common(struct srp_target_port *target, s32 req_delta,
1187 void *rsp, int len)
1188{
Bart Van Assche76c75b22010-11-26 14:37:47 -05001189 struct ib_device *dev = target->srp_host->srp_dev->dev;
David Dillowbb125882010-10-08 14:40:47 -04001190 unsigned long flags;
1191 struct srp_iu *iu;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001192 int err;
David Dillowbb125882010-10-08 14:40:47 -04001193
Bart Van Asschee9684672010-11-26 15:08:38 -05001194 spin_lock_irqsave(&target->lock, flags);
David Dillowbb125882010-10-08 14:40:47 -04001195 target->req_lim += req_delta;
David Dillowbb125882010-10-08 14:40:47 -04001196 iu = __srp_get_tx_iu(target, SRP_IU_RSP);
Bart Van Asschee9684672010-11-26 15:08:38 -05001197 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001198
David Dillowbb125882010-10-08 14:40:47 -04001199 if (!iu) {
1200 shost_printk(KERN_ERR, target->scsi_host, PFX
1201 "no IU available to send response\n");
Bart Van Assche76c75b22010-11-26 14:37:47 -05001202 return 1;
David Dillowbb125882010-10-08 14:40:47 -04001203 }
1204
1205 ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE);
1206 memcpy(iu->buf, rsp, len);
1207 ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE);
1208
Bart Van Assche76c75b22010-11-26 14:37:47 -05001209 err = srp_post_send(target, iu, len);
1210 if (err) {
David Dillowbb125882010-10-08 14:40:47 -04001211 shost_printk(KERN_ERR, target->scsi_host, PFX
1212 "unable to post response: %d\n", err);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001213 srp_put_tx_iu(target, iu, SRP_IU_RSP);
1214 }
David Dillowbb125882010-10-08 14:40:47 -04001215
David Dillowbb125882010-10-08 14:40:47 -04001216 return err;
1217}
1218
1219static void srp_process_cred_req(struct srp_target_port *target,
1220 struct srp_cred_req *req)
1221{
1222 struct srp_cred_rsp rsp = {
1223 .opcode = SRP_CRED_RSP,
1224 .tag = req->tag,
1225 };
1226 s32 delta = be32_to_cpu(req->req_lim_delta);
1227
1228 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1229 shost_printk(KERN_ERR, target->scsi_host, PFX
1230 "problems processing SRP_CRED_REQ\n");
1231}
1232
1233static void srp_process_aer_req(struct srp_target_port *target,
1234 struct srp_aer_req *req)
1235{
1236 struct srp_aer_rsp rsp = {
1237 .opcode = SRP_AER_RSP,
1238 .tag = req->tag,
1239 };
1240 s32 delta = be32_to_cpu(req->req_lim_delta);
1241
1242 shost_printk(KERN_ERR, target->scsi_host, PFX
1243 "ignoring AER for LUN %llu\n", be64_to_cpu(req->lun));
1244
1245 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1246 shost_printk(KERN_ERR, target->scsi_host, PFX
1247 "problems processing SRP_AER_REQ\n");
1248}
1249
Roland Dreieraef9ec32005-11-02 14:07:13 -08001250static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
1251{
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001252 struct ib_device *dev = target->srp_host->srp_dev->dev;
Roland Dreier737b94e2011-05-23 11:30:04 -07001253 struct srp_iu *iu = (struct srp_iu *) (uintptr_t) wc->wr_id;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001254 int res;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001255 u8 opcode;
1256
Ralph Campbell85507bc2006-12-12 14:30:55 -08001257 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
1258 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001259
1260 opcode = *(u8 *) iu->buf;
1261
1262 if (0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001263 shost_printk(KERN_ERR, target->scsi_host,
1264 PFX "recv completion, opcode 0x%02x\n", opcode);
Bart Van Assche7a700812010-07-29 15:56:37 +00001265 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1,
1266 iu->buf, wc->byte_len, true);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001267 }
1268
1269 switch (opcode) {
1270 case SRP_RSP:
1271 srp_process_rsp(target, iu->buf);
1272 break;
1273
David Dillowbb125882010-10-08 14:40:47 -04001274 case SRP_CRED_REQ:
1275 srp_process_cred_req(target, iu->buf);
1276 break;
1277
1278 case SRP_AER_REQ:
1279 srp_process_aer_req(target, iu->buf);
1280 break;
1281
Roland Dreieraef9ec32005-11-02 14:07:13 -08001282 case SRP_T_LOGOUT:
1283 /* XXX Handle target logout */
David Dillow7aa54bd2008-01-07 18:23:41 -05001284 shost_printk(KERN_WARNING, target->scsi_host,
1285 PFX "Got target logout request\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001286 break;
1287
1288 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001289 shost_printk(KERN_WARNING, target->scsi_host,
1290 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001291 break;
1292 }
1293
Ralph Campbell85507bc2006-12-12 14:30:55 -08001294 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
1295 DMA_FROM_DEVICE);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001296
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001297 res = srp_post_recv(target, iu);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001298 if (res != 0)
1299 shost_printk(KERN_ERR, target->scsi_host,
1300 PFX "Recv failed with error code %d\n", res);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001301}
1302
Bart Van Assche948d1e82011-09-03 09:25:42 +02001303static void srp_handle_qp_err(enum ib_wc_status wc_status,
1304 enum ib_wc_opcode wc_opcode,
1305 struct srp_target_port *target)
1306{
Bart Van Assche294c8752011-12-25 12:18:12 +00001307 if (target->connected && !target->qp_in_error) {
Bart Van Assche4f0af692012-11-26 11:16:40 +01001308 shost_printk(KERN_ERR, target->scsi_host,
1309 PFX "failed %s status %d\n",
1310 wc_opcode & IB_WC_RECV ? "receive" : "send",
1311 wc_status);
1312 }
Bart Van Assche948d1e82011-09-03 09:25:42 +02001313 target->qp_in_error = true;
1314}
1315
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001316static void srp_recv_completion(struct ib_cq *cq, void *target_ptr)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001317{
1318 struct srp_target_port *target = target_ptr;
1319 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001320
1321 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1322 while (ib_poll_cq(cq, 1, &wc) > 0) {
Bart Van Assche948d1e82011-09-03 09:25:42 +02001323 if (likely(wc.status == IB_WC_SUCCESS)) {
1324 srp_handle_recv(target, &wc);
1325 } else {
1326 srp_handle_qp_err(wc.status, wc.opcode, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001327 }
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001328 }
1329}
1330
1331static void srp_send_completion(struct ib_cq *cq, void *target_ptr)
1332{
1333 struct srp_target_port *target = target_ptr;
1334 struct ib_wc wc;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001335 struct srp_iu *iu;
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001336
1337 while (ib_poll_cq(cq, 1, &wc) > 0) {
Bart Van Assche948d1e82011-09-03 09:25:42 +02001338 if (likely(wc.status == IB_WC_SUCCESS)) {
1339 iu = (struct srp_iu *) (uintptr_t) wc.wr_id;
1340 list_add(&iu->list, &target->free_tx);
1341 } else {
1342 srp_handle_qp_err(wc.status, wc.opcode, target);
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001343 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001344 }
1345}
1346
Bart Van Assche76c75b22010-11-26 14:37:47 -05001347static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001348{
Bart Van Assche76c75b22010-11-26 14:37:47 -05001349 struct srp_target_port *target = host_to_target(shost);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001350 struct srp_request *req;
1351 struct srp_iu *iu;
1352 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001353 struct ib_device *dev;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001354 unsigned long flags;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001355 int len;
1356
Bart Van Assche2ce19e72013-02-21 17:20:00 +00001357 if (unlikely(target->transport_offline)) {
1358 scmnd->result = DID_NO_CONNECT << 16;
1359 scmnd->scsi_done(scmnd);
1360 return 0;
1361 }
1362
Bart Van Asschee9684672010-11-26 15:08:38 -05001363 spin_lock_irqsave(&target->lock, flags);
David Dillowbb125882010-10-08 14:40:47 -04001364 iu = __srp_get_tx_iu(target, SRP_IU_CMD);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001365 if (!iu)
Bart Van Assche695b8342011-01-13 19:02:25 +00001366 goto err_unlock;
1367
1368 req = list_first_entry(&target->free_reqs, struct srp_request, list);
1369 list_del(&req->list);
1370 spin_unlock_irqrestore(&target->lock, flags);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001371
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001372 dev = target->srp_host->srp_dev->dev;
David Dillow49248642011-01-14 18:23:24 -05001373 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08001374 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001375
Roland Dreieraef9ec32005-11-02 14:07:13 -08001376 scmnd->result = 0;
David Dillowf8b6e312010-11-26 13:02:21 -05001377 scmnd->host_scribble = (void *) req;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001378
1379 cmd = iu->buf;
1380 memset(cmd, 0, sizeof *cmd);
1381
1382 cmd->opcode = SRP_CMD;
1383 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001384 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001385 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1386
Roland Dreieraef9ec32005-11-02 14:07:13 -08001387 req->scmnd = scmnd;
1388 req->cmd = iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001389
1390 len = srp_map_data(scmnd, target, req);
1391 if (len < 0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001392 shost_printk(KERN_ERR, target->scsi_host,
1393 PFX "Failed to map data\n");
Bart Van Assche76c75b22010-11-26 14:37:47 -05001394 goto err_iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001395 }
1396
David Dillow49248642011-01-14 18:23:24 -05001397 ib_dma_sync_single_for_device(dev, iu->dma, target->max_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08001398 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001399
Bart Van Assche76c75b22010-11-26 14:37:47 -05001400 if (srp_post_send(target, iu, len)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001401 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001402 goto err_unmap;
1403 }
1404
Roland Dreieraef9ec32005-11-02 14:07:13 -08001405 return 0;
1406
1407err_unmap:
1408 srp_unmap_data(scmnd, target, req);
1409
Bart Van Assche76c75b22010-11-26 14:37:47 -05001410err_iu:
1411 srp_put_tx_iu(target, iu, SRP_IU_CMD);
1412
Bart Van Asschee9684672010-11-26 15:08:38 -05001413 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001414 list_add(&req->list, &target->free_reqs);
Bart Van Assche695b8342011-01-13 19:02:25 +00001415
1416err_unlock:
Bart Van Asschee9684672010-11-26 15:08:38 -05001417 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001418
Roland Dreieraef9ec32005-11-02 14:07:13 -08001419 return SCSI_MLQUEUE_HOST_BUSY;
1420}
1421
1422static int srp_alloc_iu_bufs(struct srp_target_port *target)
1423{
1424 int i;
1425
1426 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1427 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1428 target->max_ti_iu_len,
1429 GFP_KERNEL, DMA_FROM_DEVICE);
1430 if (!target->rx_ring[i])
1431 goto err;
1432 }
1433
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001434 for (i = 0; i < SRP_SQ_SIZE; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001435 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
David Dillow49248642011-01-14 18:23:24 -05001436 target->max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001437 GFP_KERNEL, DMA_TO_DEVICE);
1438 if (!target->tx_ring[i])
1439 goto err;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001440
1441 list_add(&target->tx_ring[i]->list, &target->free_tx);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001442 }
1443
1444 return 0;
1445
1446err:
1447 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1448 srp_free_iu(target->srp_host, target->rx_ring[i]);
1449 target->rx_ring[i] = NULL;
1450 }
1451
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001452 for (i = 0; i < SRP_SQ_SIZE; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001453 srp_free_iu(target->srp_host, target->tx_ring[i]);
1454 target->tx_ring[i] = NULL;
1455 }
1456
1457 return -ENOMEM;
1458}
1459
Bart Van Asschec9b03c12011-09-03 09:34:48 +02001460static uint32_t srp_compute_rq_tmo(struct ib_qp_attr *qp_attr, int attr_mask)
1461{
1462 uint64_t T_tr_ns, max_compl_time_ms;
1463 uint32_t rq_tmo_jiffies;
1464
1465 /*
1466 * According to section 11.2.4.2 in the IBTA spec (Modify Queue Pair,
1467 * table 91), both the QP timeout and the retry count have to be set
1468 * for RC QP's during the RTR to RTS transition.
1469 */
1470 WARN_ON_ONCE((attr_mask & (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)) !=
1471 (IB_QP_TIMEOUT | IB_QP_RETRY_CNT));
1472
1473 /*
1474 * Set target->rq_tmo_jiffies to one second more than the largest time
1475 * it can take before an error completion is generated. See also
1476 * C9-140..142 in the IBTA spec for more information about how to
1477 * convert the QP Local ACK Timeout value to nanoseconds.
1478 */
1479 T_tr_ns = 4096 * (1ULL << qp_attr->timeout);
1480 max_compl_time_ms = qp_attr->retry_cnt * 4 * T_tr_ns;
1481 do_div(max_compl_time_ms, NSEC_PER_MSEC);
1482 rq_tmo_jiffies = msecs_to_jiffies(max_compl_time_ms + 1000);
1483
1484 return rq_tmo_jiffies;
1485}
1486
David Dillow961e0be2011-01-14 17:32:07 -05001487static void srp_cm_rep_handler(struct ib_cm_id *cm_id,
1488 struct srp_login_rsp *lrsp,
1489 struct srp_target_port *target)
1490{
1491 struct ib_qp_attr *qp_attr = NULL;
1492 int attr_mask = 0;
1493 int ret;
1494 int i;
1495
1496 if (lrsp->opcode == SRP_LOGIN_RSP) {
1497 target->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len);
1498 target->req_lim = be32_to_cpu(lrsp->req_lim_delta);
1499
1500 /*
1501 * Reserve credits for task management so we don't
1502 * bounce requests back to the SCSI mid-layer.
1503 */
1504 target->scsi_host->can_queue
1505 = min(target->req_lim - SRP_TSK_MGMT_SQ_SIZE,
1506 target->scsi_host->can_queue);
1507 } else {
1508 shost_printk(KERN_WARNING, target->scsi_host,
1509 PFX "Unhandled RSP opcode %#x\n", lrsp->opcode);
1510 ret = -ECONNRESET;
1511 goto error;
1512 }
1513
1514 if (!target->rx_ring[0]) {
1515 ret = srp_alloc_iu_bufs(target);
1516 if (ret)
1517 goto error;
1518 }
1519
1520 ret = -ENOMEM;
1521 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1522 if (!qp_attr)
1523 goto error;
1524
1525 qp_attr->qp_state = IB_QPS_RTR;
1526 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1527 if (ret)
1528 goto error_free;
1529
1530 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
1531 if (ret)
1532 goto error_free;
1533
1534 for (i = 0; i < SRP_RQ_SIZE; i++) {
1535 struct srp_iu *iu = target->rx_ring[i];
1536 ret = srp_post_recv(target, iu);
1537 if (ret)
1538 goto error_free;
1539 }
1540
1541 qp_attr->qp_state = IB_QPS_RTS;
1542 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1543 if (ret)
1544 goto error_free;
1545
Bart Van Asschec9b03c12011-09-03 09:34:48 +02001546 target->rq_tmo_jiffies = srp_compute_rq_tmo(qp_attr, attr_mask);
1547
David Dillow961e0be2011-01-14 17:32:07 -05001548 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
1549 if (ret)
1550 goto error_free;
1551
1552 ret = ib_send_cm_rtu(cm_id, NULL, 0);
1553
1554error_free:
1555 kfree(qp_attr);
1556
1557error:
1558 target->status = ret;
1559}
1560
Roland Dreieraef9ec32005-11-02 14:07:13 -08001561static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1562 struct ib_cm_event *event,
1563 struct srp_target_port *target)
1564{
David Dillow7aa54bd2008-01-07 18:23:41 -05001565 struct Scsi_Host *shost = target->scsi_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001566 struct ib_class_port_info *cpi;
1567 int opcode;
1568
1569 switch (event->param.rej_rcvd.reason) {
1570 case IB_CM_REJ_PORT_CM_REDIRECT:
1571 cpi = event->param.rej_rcvd.ari;
1572 target->path.dlid = cpi->redirect_lid;
1573 target->path.pkey = cpi->redirect_pkey;
1574 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1575 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1576
1577 target->status = target->path.dlid ?
1578 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1579 break;
1580
1581 case IB_CM_REJ_PORT_REDIRECT:
Roland Dreier5d7cbfd2007-08-03 10:45:18 -07001582 if (srp_target_is_topspin(target)) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001583 /*
1584 * Topspin/Cisco SRP gateways incorrectly send
1585 * reject reason code 25 when they mean 24
1586 * (port redirect).
1587 */
1588 memcpy(target->path.dgid.raw,
1589 event->param.rej_rcvd.ari, 16);
1590
David Dillow7aa54bd2008-01-07 18:23:41 -05001591 shost_printk(KERN_DEBUG, shost,
1592 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1593 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1594 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001595
1596 target->status = SRP_PORT_REDIRECT;
1597 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001598 shost_printk(KERN_WARNING, shost,
1599 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001600 target->status = -ECONNRESET;
1601 }
1602 break;
1603
1604 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
David Dillow7aa54bd2008-01-07 18:23:41 -05001605 shost_printk(KERN_WARNING, shost,
1606 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001607 target->status = -ECONNRESET;
1608 break;
1609
1610 case IB_CM_REJ_CONSUMER_DEFINED:
1611 opcode = *(u8 *) event->private_data;
1612 if (opcode == SRP_LOGIN_REJ) {
1613 struct srp_login_rej *rej = event->private_data;
1614 u32 reason = be32_to_cpu(rej->reason);
1615
1616 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
David Dillow7aa54bd2008-01-07 18:23:41 -05001617 shost_printk(KERN_WARNING, shost,
1618 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001619 else
David Dillow7aa54bd2008-01-07 18:23:41 -05001620 shost_printk(KERN_WARNING, shost,
1621 PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001622 } else
David Dillow7aa54bd2008-01-07 18:23:41 -05001623 shost_printk(KERN_WARNING, shost,
1624 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1625 " opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001626 target->status = -ECONNRESET;
1627 break;
1628
David Dillow9fe4bcf2008-01-08 17:08:52 -05001629 case IB_CM_REJ_STALE_CONN:
1630 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
1631 target->status = SRP_STALE_CONN;
1632 break;
1633
Roland Dreieraef9ec32005-11-02 14:07:13 -08001634 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001635 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
1636 event->param.rej_rcvd.reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001637 target->status = -ECONNRESET;
1638 }
1639}
1640
1641static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1642{
1643 struct srp_target_port *target = cm_id->context;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001644 int comp = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001645
1646 switch (event->event) {
1647 case IB_CM_REQ_ERROR:
David Dillow7aa54bd2008-01-07 18:23:41 -05001648 shost_printk(KERN_DEBUG, target->scsi_host,
1649 PFX "Sending CM REQ failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001650 comp = 1;
1651 target->status = -ECONNRESET;
1652 break;
1653
1654 case IB_CM_REP_RECEIVED:
1655 comp = 1;
David Dillow961e0be2011-01-14 17:32:07 -05001656 srp_cm_rep_handler(cm_id, event->private_data, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001657 break;
1658
1659 case IB_CM_REJ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001660 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001661 comp = 1;
1662
1663 srp_cm_rej_handler(cm_id, event, target);
1664 break;
1665
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001666 case IB_CM_DREQ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001667 shost_printk(KERN_WARNING, target->scsi_host,
1668 PFX "DREQ received - connection closed\n");
Bart Van Assche294c8752011-12-25 12:18:12 +00001669 srp_change_conn_state(target, false);
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001670 if (ib_send_cm_drep(cm_id, NULL, 0))
David Dillow7aa54bd2008-01-07 18:23:41 -05001671 shost_printk(KERN_ERR, target->scsi_host,
1672 PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001673 break;
1674
1675 case IB_CM_TIMEWAIT_EXIT:
David Dillow7aa54bd2008-01-07 18:23:41 -05001676 shost_printk(KERN_ERR, target->scsi_host,
1677 PFX "connection closed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001678
Roland Dreieraef9ec32005-11-02 14:07:13 -08001679 target->status = 0;
1680 break;
1681
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001682 case IB_CM_MRA_RECEIVED:
1683 case IB_CM_DREQ_ERROR:
1684 case IB_CM_DREP_RECEIVED:
1685 break;
1686
Roland Dreieraef9ec32005-11-02 14:07:13 -08001687 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001688 shost_printk(KERN_WARNING, target->scsi_host,
1689 PFX "Unhandled CM event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001690 break;
1691 }
1692
1693 if (comp)
1694 complete(&target->done);
1695
Roland Dreieraef9ec32005-11-02 14:07:13 -08001696 return 0;
1697}
1698
Roland Dreierd945e1d2006-05-09 10:50:28 -07001699static int srp_send_tsk_mgmt(struct srp_target_port *target,
David Dillowf8b6e312010-11-26 13:02:21 -05001700 u64 req_tag, unsigned int lun, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001701{
David Dillow19081f32010-10-18 08:54:49 -04001702 struct ib_device *dev = target->srp_host->srp_dev->dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001703 struct srp_iu *iu;
1704 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001705
Bart Van Assche3780d1f2013-02-21 17:18:00 +00001706 if (!target->connected || target->qp_in_error)
1707 return -1;
1708
David Dillowf8b6e312010-11-26 13:02:21 -05001709 init_completion(&target->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001710
Bart Van Asschee9684672010-11-26 15:08:38 -05001711 spin_lock_irq(&target->lock);
David Dillowbb125882010-10-08 14:40:47 -04001712 iu = __srp_get_tx_iu(target, SRP_IU_TSK_MGMT);
Bart Van Asschee9684672010-11-26 15:08:38 -05001713 spin_unlock_irq(&target->lock);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001714
Roland Dreieraef9ec32005-11-02 14:07:13 -08001715 if (!iu)
Bart Van Assche76c75b22010-11-26 14:37:47 -05001716 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001717
David Dillow19081f32010-10-18 08:54:49 -04001718 ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt,
1719 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001720 tsk_mgmt = iu->buf;
1721 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1722
1723 tsk_mgmt->opcode = SRP_TSK_MGMT;
David Dillowf8b6e312010-11-26 13:02:21 -05001724 tsk_mgmt->lun = cpu_to_be64((u64) lun << 48);
1725 tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001726 tsk_mgmt->tsk_mgmt_func = func;
David Dillowf8b6e312010-11-26 13:02:21 -05001727 tsk_mgmt->task_tag = req_tag;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001728
David Dillow19081f32010-10-18 08:54:49 -04001729 ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt,
1730 DMA_TO_DEVICE);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001731 if (srp_post_send(target, iu, sizeof *tsk_mgmt)) {
1732 srp_put_tx_iu(target, iu, SRP_IU_TSK_MGMT);
1733 return -1;
1734 }
Roland Dreierd945e1d2006-05-09 10:50:28 -07001735
David Dillowf8b6e312010-11-26 13:02:21 -05001736 if (!wait_for_completion_timeout(&target->tsk_mgmt_done,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001737 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001738 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001739
Roland Dreierd945e1d2006-05-09 10:50:28 -07001740 return 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001741}
1742
Roland Dreieraef9ec32005-11-02 14:07:13 -08001743static int srp_abort(struct scsi_cmnd *scmnd)
1744{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001745 struct srp_target_port *target = host_to_target(scmnd->device->host);
David Dillowf8b6e312010-11-26 13:02:21 -05001746 struct srp_request *req = (struct srp_request *) scmnd->host_scribble;
Bart Van Assche086f44f2013-06-12 15:23:04 +02001747 int ret;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001748
David Dillow7aa54bd2008-01-07 18:23:41 -05001749 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001750
Bart Van Asschec7c4e7f2013-02-21 17:19:04 +00001751 if (!req || !srp_claim_req(target, req, scmnd))
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001752 return FAILED;
Bart Van Assche086f44f2013-06-12 15:23:04 +02001753 if (srp_send_tsk_mgmt(target, req->index, scmnd->device->lun,
1754 SRP_TSK_ABORT_TASK) == 0 ||
1755 target->transport_offline)
1756 ret = SUCCESS;
1757 else
1758 ret = FAILED;
Bart Van Assche22032992012-08-14 13:18:53 +00001759 srp_free_req(target, req, scmnd, 0);
1760 scmnd->result = DID_ABORT << 16;
Bart Van Assched8536672012-08-24 10:29:11 +00001761 scmnd->scsi_done(scmnd);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001762
Bart Van Assche086f44f2013-06-12 15:23:04 +02001763 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001764}
1765
1766static int srp_reset_device(struct scsi_cmnd *scmnd)
1767{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001768 struct srp_target_port *target = host_to_target(scmnd->device->host);
Bart Van Assche536ae142010-11-26 13:58:27 -05001769 int i;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001770
David Dillow7aa54bd2008-01-07 18:23:41 -05001771 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001772
David Dillowf8b6e312010-11-26 13:02:21 -05001773 if (srp_send_tsk_mgmt(target, SRP_TAG_NO_REQ, scmnd->device->lun,
1774 SRP_TSK_LUN_RESET))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001775 return FAILED;
David Dillowf8b6e312010-11-26 13:02:21 -05001776 if (target->tsk_mgmt_status)
Roland Dreierd945e1d2006-05-09 10:50:28 -07001777 return FAILED;
1778
Bart Van Assche536ae142010-11-26 13:58:27 -05001779 for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
1780 struct srp_request *req = &target->req_ring[i];
David Dillowf8b6e312010-11-26 13:02:21 -05001781 if (req->scmnd && req->scmnd->device == scmnd->device)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001782 srp_reset_req(target, req);
Bart Van Assche536ae142010-11-26 13:58:27 -05001783 }
Roland Dreierd945e1d2006-05-09 10:50:28 -07001784
Roland Dreierd945e1d2006-05-09 10:50:28 -07001785 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001786}
1787
1788static int srp_reset_host(struct scsi_cmnd *scmnd)
1789{
1790 struct srp_target_port *target = host_to_target(scmnd->device->host);
1791 int ret = FAILED;
1792
David Dillow7aa54bd2008-01-07 18:23:41 -05001793 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001794
1795 if (!srp_reconnect_target(target))
1796 ret = SUCCESS;
1797
1798 return ret;
1799}
1800
Bart Van Asschec9b03c12011-09-03 09:34:48 +02001801static int srp_slave_configure(struct scsi_device *sdev)
1802{
1803 struct Scsi_Host *shost = sdev->host;
1804 struct srp_target_port *target = host_to_target(shost);
1805 struct request_queue *q = sdev->request_queue;
1806 unsigned long timeout;
1807
1808 if (sdev->type == TYPE_DISK) {
1809 timeout = max_t(unsigned, 30 * HZ, target->rq_tmo_jiffies);
1810 blk_queue_rq_timeout(q, timeout);
1811 }
1812
1813 return 0;
1814}
1815
Tony Jonesee959b02008-02-22 00:13:36 +01001816static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
1817 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001818{
Tony Jonesee959b02008-02-22 00:13:36 +01001819 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001820
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001821 return sprintf(buf, "0x%016llx\n",
1822 (unsigned long long) be64_to_cpu(target->id_ext));
1823}
1824
Tony Jonesee959b02008-02-22 00:13:36 +01001825static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
1826 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001827{
Tony Jonesee959b02008-02-22 00:13:36 +01001828 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001829
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001830 return sprintf(buf, "0x%016llx\n",
1831 (unsigned long long) be64_to_cpu(target->ioc_guid));
1832}
1833
Tony Jonesee959b02008-02-22 00:13:36 +01001834static ssize_t show_service_id(struct device *dev,
1835 struct device_attribute *attr, char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001836{
Tony Jonesee959b02008-02-22 00:13:36 +01001837 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001838
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001839 return sprintf(buf, "0x%016llx\n",
1840 (unsigned long long) be64_to_cpu(target->service_id));
1841}
1842
Tony Jonesee959b02008-02-22 00:13:36 +01001843static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
1844 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001845{
Tony Jonesee959b02008-02-22 00:13:36 +01001846 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001847
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001848 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1849}
1850
Tony Jonesee959b02008-02-22 00:13:36 +01001851static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
1852 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001853{
Tony Jonesee959b02008-02-22 00:13:36 +01001854 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001855
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001856 return sprintf(buf, "%pI6\n", target->path.dgid.raw);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001857}
1858
Tony Jonesee959b02008-02-22 00:13:36 +01001859static ssize_t show_orig_dgid(struct device *dev,
1860 struct device_attribute *attr, char *buf)
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001861{
Tony Jonesee959b02008-02-22 00:13:36 +01001862 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001863
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001864 return sprintf(buf, "%pI6\n", target->orig_dgid);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001865}
1866
Bart Van Assche89de7482010-08-03 14:08:45 +00001867static ssize_t show_req_lim(struct device *dev,
1868 struct device_attribute *attr, char *buf)
1869{
1870 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1871
Bart Van Assche89de7482010-08-03 14:08:45 +00001872 return sprintf(buf, "%d\n", target->req_lim);
1873}
1874
Tony Jonesee959b02008-02-22 00:13:36 +01001875static ssize_t show_zero_req_lim(struct device *dev,
1876 struct device_attribute *attr, char *buf)
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001877{
Tony Jonesee959b02008-02-22 00:13:36 +01001878 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001879
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001880 return sprintf(buf, "%d\n", target->zero_req_lim);
1881}
1882
Tony Jonesee959b02008-02-22 00:13:36 +01001883static ssize_t show_local_ib_port(struct device *dev,
1884 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001885{
Tony Jonesee959b02008-02-22 00:13:36 +01001886 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001887
1888 return sprintf(buf, "%d\n", target->srp_host->port);
1889}
1890
Tony Jonesee959b02008-02-22 00:13:36 +01001891static ssize_t show_local_ib_device(struct device *dev,
1892 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001893{
Tony Jonesee959b02008-02-22 00:13:36 +01001894 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001895
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001896 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001897}
1898
David Dillow49248642011-01-14 18:23:24 -05001899static ssize_t show_cmd_sg_entries(struct device *dev,
1900 struct device_attribute *attr, char *buf)
1901{
1902 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1903
1904 return sprintf(buf, "%u\n", target->cmd_sg_cnt);
1905}
1906
David Dillowc07d4242011-01-16 13:57:10 -05001907static ssize_t show_allow_ext_sg(struct device *dev,
1908 struct device_attribute *attr, char *buf)
1909{
1910 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1911
1912 return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false");
1913}
1914
Tony Jonesee959b02008-02-22 00:13:36 +01001915static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1916static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1917static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1918static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1919static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1920static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
Bart Van Assche89de7482010-08-03 14:08:45 +00001921static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01001922static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1923static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1924static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
David Dillow49248642011-01-14 18:23:24 -05001925static DEVICE_ATTR(cmd_sg_entries, S_IRUGO, show_cmd_sg_entries, NULL);
David Dillowc07d4242011-01-16 13:57:10 -05001926static DEVICE_ATTR(allow_ext_sg, S_IRUGO, show_allow_ext_sg, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001927
Tony Jonesee959b02008-02-22 00:13:36 +01001928static struct device_attribute *srp_host_attrs[] = {
1929 &dev_attr_id_ext,
1930 &dev_attr_ioc_guid,
1931 &dev_attr_service_id,
1932 &dev_attr_pkey,
1933 &dev_attr_dgid,
1934 &dev_attr_orig_dgid,
Bart Van Assche89de7482010-08-03 14:08:45 +00001935 &dev_attr_req_lim,
Tony Jonesee959b02008-02-22 00:13:36 +01001936 &dev_attr_zero_req_lim,
1937 &dev_attr_local_ib_port,
1938 &dev_attr_local_ib_device,
David Dillow49248642011-01-14 18:23:24 -05001939 &dev_attr_cmd_sg_entries,
David Dillowc07d4242011-01-16 13:57:10 -05001940 &dev_attr_allow_ext_sg,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001941 NULL
1942};
1943
Roland Dreieraef9ec32005-11-02 14:07:13 -08001944static struct scsi_host_template srp_template = {
1945 .module = THIS_MODULE,
Roland Dreierb7f008f2007-05-06 21:18:11 -07001946 .name = "InfiniBand SRP initiator",
1947 .proc_name = DRV_NAME,
Bart Van Asschec9b03c12011-09-03 09:34:48 +02001948 .slave_configure = srp_slave_configure,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001949 .info = srp_target_info,
1950 .queuecommand = srp_queuecommand,
1951 .eh_abort_handler = srp_abort,
1952 .eh_device_reset_handler = srp_reset_device,
1953 .eh_host_reset_handler = srp_reset_host,
Bart Van Assche2742c1d2013-06-12 15:24:25 +02001954 .skip_settle_delay = true,
David Dillow49248642011-01-14 18:23:24 -05001955 .sg_tablesize = SRP_DEF_SG_TABLESIZE,
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001956 .can_queue = SRP_CMD_SQ_SIZE,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001957 .this_id = -1,
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001958 .cmd_per_lun = SRP_CMD_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001959 .use_clustering = ENABLE_CLUSTERING,
1960 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001961};
1962
1963static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1964{
FUJITA Tomonori32368222007-06-27 16:33:12 +09001965 struct srp_rport_identifiers ids;
1966 struct srp_rport *rport;
1967
Roland Dreieraef9ec32005-11-02 14:07:13 -08001968 sprintf(target->target_name, "SRP.T10:%016llX",
1969 (unsigned long long) be64_to_cpu(target->id_ext));
1970
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001971 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001972 return -ENODEV;
1973
FUJITA Tomonori32368222007-06-27 16:33:12 +09001974 memcpy(ids.port_id, &target->id_ext, 8);
1975 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09001976 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori32368222007-06-27 16:33:12 +09001977 rport = srp_rport_add(target->scsi_host, &ids);
1978 if (IS_ERR(rport)) {
1979 scsi_remove_host(target->scsi_host);
1980 return PTR_ERR(rport);
1981 }
1982
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02001983 rport->lld_data = target;
1984
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001985 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001986 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001987 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001988
1989 target->state = SRP_TARGET_LIVE;
1990
Roland Dreieraef9ec32005-11-02 14:07:13 -08001991 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07001992 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001993
1994 return 0;
1995}
1996
Tony Jonesee959b02008-02-22 00:13:36 +01001997static void srp_release_dev(struct device *dev)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001998{
1999 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01002000 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002001
2002 complete(&host->released);
2003}
2004
2005static struct class srp_class = {
2006 .name = "infiniband_srp",
Tony Jonesee959b02008-02-22 00:13:36 +01002007 .dev_release = srp_release_dev
Roland Dreieraef9ec32005-11-02 14:07:13 -08002008};
2009
2010/*
2011 * Target ports are added by writing
2012 *
2013 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
2014 * pkey=<P_Key>,service_id=<service ID>
2015 *
2016 * to the add_target sysfs attribute.
2017 */
2018enum {
2019 SRP_OPT_ERR = 0,
2020 SRP_OPT_ID_EXT = 1 << 0,
2021 SRP_OPT_IOC_GUID = 1 << 1,
2022 SRP_OPT_DGID = 1 << 2,
2023 SRP_OPT_PKEY = 1 << 3,
2024 SRP_OPT_SERVICE_ID = 1 << 4,
2025 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07002026 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07002027 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002028 SRP_OPT_INITIATOR_EXT = 1 << 8,
David Dillow49248642011-01-14 18:23:24 -05002029 SRP_OPT_CMD_SG_ENTRIES = 1 << 9,
David Dillowc07d4242011-01-16 13:57:10 -05002030 SRP_OPT_ALLOW_EXT_SG = 1 << 10,
2031 SRP_OPT_SG_TABLESIZE = 1 << 11,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002032 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
2033 SRP_OPT_IOC_GUID |
2034 SRP_OPT_DGID |
2035 SRP_OPT_PKEY |
2036 SRP_OPT_SERVICE_ID),
2037};
2038
Steven Whitehousea447c092008-10-13 10:46:57 +01002039static const match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07002040 { SRP_OPT_ID_EXT, "id_ext=%s" },
2041 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
2042 { SRP_OPT_DGID, "dgid=%s" },
2043 { SRP_OPT_PKEY, "pkey=%x" },
2044 { SRP_OPT_SERVICE_ID, "service_id=%s" },
2045 { SRP_OPT_MAX_SECT, "max_sect=%d" },
2046 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07002047 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002048 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
David Dillow49248642011-01-14 18:23:24 -05002049 { SRP_OPT_CMD_SG_ENTRIES, "cmd_sg_entries=%u" },
David Dillowc07d4242011-01-16 13:57:10 -05002050 { SRP_OPT_ALLOW_EXT_SG, "allow_ext_sg=%u" },
2051 { SRP_OPT_SG_TABLESIZE, "sg_tablesize=%u" },
Vu Pham52fb2b502006-06-17 20:37:31 -07002052 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002053};
2054
2055static int srp_parse_options(const char *buf, struct srp_target_port *target)
2056{
2057 char *options, *sep_opt;
2058 char *p;
2059 char dgid[3];
2060 substring_t args[MAX_OPT_ARGS];
2061 int opt_mask = 0;
2062 int token;
2063 int ret = -EINVAL;
2064 int i;
2065
2066 options = kstrdup(buf, GFP_KERNEL);
2067 if (!options)
2068 return -ENOMEM;
2069
2070 sep_opt = options;
2071 while ((p = strsep(&sep_opt, ",")) != NULL) {
2072 if (!*p)
2073 continue;
2074
2075 token = match_token(p, srp_opt_tokens, args);
2076 opt_mask |= token;
2077
2078 switch (token) {
2079 case SRP_OPT_ID_EXT:
2080 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002081 if (!p) {
2082 ret = -ENOMEM;
2083 goto out;
2084 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002085 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2086 kfree(p);
2087 break;
2088
2089 case SRP_OPT_IOC_GUID:
2090 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002091 if (!p) {
2092 ret = -ENOMEM;
2093 goto out;
2094 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002095 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
2096 kfree(p);
2097 break;
2098
2099 case SRP_OPT_DGID:
2100 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002101 if (!p) {
2102 ret = -ENOMEM;
2103 goto out;
2104 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002105 if (strlen(p) != 32) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002106 pr_warn("bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07002107 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002108 goto out;
2109 }
2110
2111 for (i = 0; i < 16; ++i) {
2112 strlcpy(dgid, p + i * 2, 3);
2113 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
2114 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08002115 kfree(p);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002116 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002117 break;
2118
2119 case SRP_OPT_PKEY:
2120 if (match_hex(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002121 pr_warn("bad P_Key parameter '%s'\n", p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002122 goto out;
2123 }
2124 target->path.pkey = cpu_to_be16(token);
2125 break;
2126
2127 case SRP_OPT_SERVICE_ID:
2128 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002129 if (!p) {
2130 ret = -ENOMEM;
2131 goto out;
2132 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002133 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
Sean Hefty247e0202007-08-08 15:51:18 -07002134 target->path.service_id = target->service_id;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002135 kfree(p);
2136 break;
2137
2138 case SRP_OPT_MAX_SECT:
2139 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002140 pr_warn("bad max sect parameter '%s'\n", p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002141 goto out;
2142 }
2143 target->scsi_host->max_sectors = token;
2144 break;
2145
Vu Pham52fb2b502006-06-17 20:37:31 -07002146 case SRP_OPT_MAX_CMD_PER_LUN:
2147 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002148 pr_warn("bad max cmd_per_lun parameter '%s'\n",
2149 p);
Vu Pham52fb2b502006-06-17 20:37:31 -07002150 goto out;
2151 }
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00002152 target->scsi_host->cmd_per_lun = min(token, SRP_CMD_SQ_SIZE);
Vu Pham52fb2b502006-06-17 20:37:31 -07002153 break;
2154
Ramachandra K0c0450db2006-06-17 20:37:38 -07002155 case SRP_OPT_IO_CLASS:
2156 if (match_hex(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002157 pr_warn("bad IO class parameter '%s'\n", p);
Ramachandra K0c0450db2006-06-17 20:37:38 -07002158 goto out;
2159 }
2160 if (token != SRP_REV10_IB_IO_CLASS &&
2161 token != SRP_REV16A_IB_IO_CLASS) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002162 pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n",
2163 token, SRP_REV10_IB_IO_CLASS,
2164 SRP_REV16A_IB_IO_CLASS);
Ramachandra K0c0450db2006-06-17 20:37:38 -07002165 goto out;
2166 }
2167 target->io_class = token;
2168 break;
2169
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002170 case SRP_OPT_INITIATOR_EXT:
2171 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002172 if (!p) {
2173 ret = -ENOMEM;
2174 goto out;
2175 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002176 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2177 kfree(p);
2178 break;
2179
David Dillow49248642011-01-14 18:23:24 -05002180 case SRP_OPT_CMD_SG_ENTRIES:
2181 if (match_int(args, &token) || token < 1 || token > 255) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002182 pr_warn("bad max cmd_sg_entries parameter '%s'\n",
2183 p);
David Dillow49248642011-01-14 18:23:24 -05002184 goto out;
2185 }
2186 target->cmd_sg_cnt = token;
2187 break;
2188
David Dillowc07d4242011-01-16 13:57:10 -05002189 case SRP_OPT_ALLOW_EXT_SG:
2190 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002191 pr_warn("bad allow_ext_sg parameter '%s'\n", p);
David Dillowc07d4242011-01-16 13:57:10 -05002192 goto out;
2193 }
2194 target->allow_ext_sg = !!token;
2195 break;
2196
2197 case SRP_OPT_SG_TABLESIZE:
2198 if (match_int(args, &token) || token < 1 ||
2199 token > SCSI_MAX_SG_CHAIN_SEGMENTS) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002200 pr_warn("bad max sg_tablesize parameter '%s'\n",
2201 p);
David Dillowc07d4242011-01-16 13:57:10 -05002202 goto out;
2203 }
2204 target->sg_tablesize = token;
2205 break;
2206
Roland Dreieraef9ec32005-11-02 14:07:13 -08002207 default:
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002208 pr_warn("unknown parameter or missing value '%s' in target creation request\n",
2209 p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002210 goto out;
2211 }
2212 }
2213
2214 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
2215 ret = 0;
2216 else
2217 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
2218 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
2219 !(srp_opt_tokens[i].token & opt_mask))
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002220 pr_warn("target creation request is missing parameter '%s'\n",
2221 srp_opt_tokens[i].pattern);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002222
2223out:
2224 kfree(options);
2225 return ret;
2226}
2227
Tony Jonesee959b02008-02-22 00:13:36 +01002228static ssize_t srp_create_target(struct device *dev,
2229 struct device_attribute *attr,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002230 const char *buf, size_t count)
2231{
2232 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01002233 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002234 struct Scsi_Host *target_host;
2235 struct srp_target_port *target;
David Dillowc07d4242011-01-16 13:57:10 -05002236 struct ib_device *ibdev = host->srp_dev->dev;
2237 dma_addr_t dma_addr;
David Dillow8f26c9f2011-01-14 19:45:50 -05002238 int i, ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002239
2240 target_host = scsi_host_alloc(&srp_template,
2241 sizeof (struct srp_target_port));
2242 if (!target_host)
2243 return -ENOMEM;
2244
David Dillow49248642011-01-14 18:23:24 -05002245 target_host->transportt = ib_srp_transport_template;
Bart Van Asschefd1b6c42011-07-13 09:19:16 -07002246 target_host->max_channel = 0;
2247 target_host->max_id = 1;
Arne Redlich3c8edf02006-11-15 12:43:00 +01002248 target_host->max_lun = SRP_MAX_LUN;
2249 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08002250
Roland Dreieraef9ec32005-11-02 14:07:13 -08002251 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002252
David Dillow49248642011-01-14 18:23:24 -05002253 target->io_class = SRP_REV16A_IB_IO_CLASS;
2254 target->scsi_host = target_host;
2255 target->srp_host = host;
2256 target->lkey = host->srp_dev->mr->lkey;
2257 target->rkey = host->srp_dev->mr->rkey;
2258 target->cmd_sg_cnt = cmd_sg_entries;
David Dillowc07d4242011-01-16 13:57:10 -05002259 target->sg_tablesize = indirect_sg_entries ? : cmd_sg_entries;
2260 target->allow_ext_sg = allow_ext_sg;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002261
Roland Dreieraef9ec32005-11-02 14:07:13 -08002262 ret = srp_parse_options(buf, target);
2263 if (ret)
2264 goto err;
2265
David Dillowc07d4242011-01-16 13:57:10 -05002266 if (!host->srp_dev->fmr_pool && !target->allow_ext_sg &&
2267 target->cmd_sg_cnt < target->sg_tablesize) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002268 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 -05002269 target->sg_tablesize = target->cmd_sg_cnt;
2270 }
2271
2272 target_host->sg_tablesize = target->sg_tablesize;
2273 target->indirect_size = target->sg_tablesize *
2274 sizeof (struct srp_direct_buf);
David Dillow49248642011-01-14 18:23:24 -05002275 target->max_iu_len = sizeof (struct srp_cmd) +
2276 sizeof (struct srp_indirect_buf) +
2277 target->cmd_sg_cnt * sizeof (struct srp_direct_buf);
2278
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002279 INIT_WORK(&target->remove_work, srp_remove_work);
David Dillow8f26c9f2011-01-14 19:45:50 -05002280 spin_lock_init(&target->lock);
2281 INIT_LIST_HEAD(&target->free_tx);
2282 INIT_LIST_HEAD(&target->free_reqs);
2283 for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
2284 struct srp_request *req = &target->req_ring[i];
2285
2286 req->fmr_list = kmalloc(target->cmd_sg_cnt * sizeof (void *),
2287 GFP_KERNEL);
2288 req->map_page = kmalloc(SRP_FMR_SIZE * sizeof (void *),
2289 GFP_KERNEL);
David Dillowc07d4242011-01-16 13:57:10 -05002290 req->indirect_desc = kmalloc(target->indirect_size, GFP_KERNEL);
2291 if (!req->fmr_list || !req->map_page || !req->indirect_desc)
David Dillow8f26c9f2011-01-14 19:45:50 -05002292 goto err_free_mem;
2293
David Dillowc07d4242011-01-16 13:57:10 -05002294 dma_addr = ib_dma_map_single(ibdev, req->indirect_desc,
2295 target->indirect_size,
2296 DMA_TO_DEVICE);
2297 if (ib_dma_mapping_error(ibdev, dma_addr))
2298 goto err_free_mem;
2299
2300 req->indirect_dma_addr = dma_addr;
David Dillow8f26c9f2011-01-14 19:45:50 -05002301 req->index = i;
2302 list_add_tail(&req->list, &target->free_reqs);
2303 }
2304
David Dillowc07d4242011-01-16 13:57:10 -05002305 ib_query_gid(ibdev, host->port, 0, &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002306
David Dillow7aa54bd2008-01-07 18:23:41 -05002307 shost_printk(KERN_DEBUG, target->scsi_host, PFX
2308 "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002309 "service_id %016llx dgid %pI6\n",
Roland Dreieraef9ec32005-11-02 14:07:13 -08002310 (unsigned long long) be64_to_cpu(target->id_ext),
2311 (unsigned long long) be64_to_cpu(target->ioc_guid),
2312 be16_to_cpu(target->path.pkey),
2313 (unsigned long long) be64_to_cpu(target->service_id),
Harvey Harrison8867cd72008-10-28 22:36:33 -07002314 target->path.dgid.raw);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002315
2316 ret = srp_create_target_ib(target);
2317 if (ret)
David Dillow8f26c9f2011-01-14 19:45:50 -05002318 goto err_free_mem;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002319
David Dillow9fe4bcf2008-01-08 17:08:52 -05002320 ret = srp_new_cm_id(target);
2321 if (ret)
David Dillow8f26c9f2011-01-14 19:45:50 -05002322 goto err_free_ib;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002323
2324 ret = srp_connect_target(target);
2325 if (ret) {
David Dillow7aa54bd2008-01-07 18:23:41 -05002326 shost_printk(KERN_ERR, target->scsi_host,
2327 PFX "Connection failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002328 goto err_cm_id;
2329 }
2330
2331 ret = srp_add_target(host, target);
2332 if (ret)
2333 goto err_disconnect;
2334
2335 return count;
2336
2337err_disconnect:
2338 srp_disconnect_target(target);
2339
2340err_cm_id:
2341 ib_destroy_cm_id(target->cm_id);
2342
David Dillow8f26c9f2011-01-14 19:45:50 -05002343err_free_ib:
Roland Dreieraef9ec32005-11-02 14:07:13 -08002344 srp_free_target_ib(target);
2345
David Dillow8f26c9f2011-01-14 19:45:50 -05002346err_free_mem:
2347 srp_free_req_data(target);
2348
Roland Dreieraef9ec32005-11-02 14:07:13 -08002349err:
2350 scsi_host_put(target_host);
2351
2352 return ret;
2353}
2354
Tony Jonesee959b02008-02-22 00:13:36 +01002355static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002356
Tony Jonesee959b02008-02-22 00:13:36 +01002357static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
2358 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002359{
Tony Jonesee959b02008-02-22 00:13:36 +01002360 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002361
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002362 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002363}
2364
Tony Jonesee959b02008-02-22 00:13:36 +01002365static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002366
Tony Jonesee959b02008-02-22 00:13:36 +01002367static ssize_t show_port(struct device *dev, struct device_attribute *attr,
2368 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002369{
Tony Jonesee959b02008-02-22 00:13:36 +01002370 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002371
2372 return sprintf(buf, "%d\n", host->port);
2373}
2374
Tony Jonesee959b02008-02-22 00:13:36 +01002375static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002376
Roland Dreierf5358a12006-06-17 20:37:29 -07002377static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002378{
2379 struct srp_host *host;
2380
2381 host = kzalloc(sizeof *host, GFP_KERNEL);
2382 if (!host)
2383 return NULL;
2384
2385 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002386 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002387 init_completion(&host->released);
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002388 host->srp_dev = device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002389 host->port = port;
2390
Tony Jonesee959b02008-02-22 00:13:36 +01002391 host->dev.class = &srp_class;
2392 host->dev.parent = device->dev->dma_device;
Kay Sieversd927e382009-01-06 10:44:39 -08002393 dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002394
Tony Jonesee959b02008-02-22 00:13:36 +01002395 if (device_register(&host->dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07002396 goto free_host;
Tony Jonesee959b02008-02-22 00:13:36 +01002397 if (device_create_file(&host->dev, &dev_attr_add_target))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002398 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01002399 if (device_create_file(&host->dev, &dev_attr_ibdev))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002400 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01002401 if (device_create_file(&host->dev, &dev_attr_port))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002402 goto err_class;
2403
2404 return host;
2405
2406err_class:
Tony Jonesee959b02008-02-22 00:13:36 +01002407 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002408
Roland Dreierf5358a12006-06-17 20:37:29 -07002409free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08002410 kfree(host);
2411
2412 return NULL;
2413}
2414
2415static void srp_add_one(struct ib_device *device)
2416{
Roland Dreierf5358a12006-06-17 20:37:29 -07002417 struct srp_device *srp_dev;
2418 struct ib_device_attr *dev_attr;
2419 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002420 struct srp_host *host;
David Dillowbe8b9812011-01-18 21:58:09 -05002421 int max_pages_per_fmr, fmr_page_shift, s, e, p;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002422
Roland Dreierf5358a12006-06-17 20:37:29 -07002423 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
2424 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08002425 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002426
Roland Dreierf5358a12006-06-17 20:37:29 -07002427 if (ib_query_device(device, dev_attr)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002428 pr_warn("Query device failed for %s\n", device->name);
Roland Dreierf5358a12006-06-17 20:37:29 -07002429 goto free_attr;
2430 }
2431
2432 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
2433 if (!srp_dev)
2434 goto free_attr;
2435
2436 /*
2437 * Use the smallest page size supported by the HCA, down to a
David Dillow8f26c9f2011-01-14 19:45:50 -05002438 * minimum of 4096 bytes. We're unlikely to build large sglists
2439 * out of smaller entries.
Roland Dreierf5358a12006-06-17 20:37:29 -07002440 */
David Dillow8f26c9f2011-01-14 19:45:50 -05002441 fmr_page_shift = max(12, ffs(dev_attr->page_size_cap) - 1);
2442 srp_dev->fmr_page_size = 1 << fmr_page_shift;
2443 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
2444 srp_dev->fmr_max_size = srp_dev->fmr_page_size * SRP_FMR_SIZE;
Roland Dreierf5358a12006-06-17 20:37:29 -07002445
2446 INIT_LIST_HEAD(&srp_dev->dev_list);
2447
2448 srp_dev->dev = device;
2449 srp_dev->pd = ib_alloc_pd(device);
2450 if (IS_ERR(srp_dev->pd))
2451 goto free_dev;
2452
2453 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
2454 IB_ACCESS_LOCAL_WRITE |
2455 IB_ACCESS_REMOTE_READ |
2456 IB_ACCESS_REMOTE_WRITE);
2457 if (IS_ERR(srp_dev->mr))
2458 goto err_pd;
2459
David Dillowbe8b9812011-01-18 21:58:09 -05002460 for (max_pages_per_fmr = SRP_FMR_SIZE;
2461 max_pages_per_fmr >= SRP_FMR_MIN_SIZE;
2462 max_pages_per_fmr /= 2, srp_dev->fmr_max_size /= 2) {
2463 memset(&fmr_param, 0, sizeof fmr_param);
2464 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
2465 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
2466 fmr_param.cache = 1;
2467 fmr_param.max_pages_per_fmr = max_pages_per_fmr;
2468 fmr_param.page_shift = fmr_page_shift;
2469 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
2470 IB_ACCESS_REMOTE_WRITE |
2471 IB_ACCESS_REMOTE_READ);
Roland Dreierf5358a12006-06-17 20:37:29 -07002472
David Dillowbe8b9812011-01-18 21:58:09 -05002473 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
2474 if (!IS_ERR(srp_dev->fmr_pool))
2475 break;
2476 }
2477
Roland Dreierf5358a12006-06-17 20:37:29 -07002478 if (IS_ERR(srp_dev->fmr_pool))
2479 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002480
Tom Tucker07ebafb2006-08-03 16:02:42 -05002481 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002482 s = 0;
2483 e = 0;
2484 } else {
2485 s = 1;
2486 e = device->phys_port_cnt;
2487 }
2488
2489 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07002490 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002491 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07002492 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002493 }
2494
Roland Dreierf5358a12006-06-17 20:37:29 -07002495 ib_set_client_data(device, &srp_client, srp_dev);
2496
2497 goto free_attr;
2498
2499err_pd:
2500 ib_dealloc_pd(srp_dev->pd);
2501
2502free_dev:
2503 kfree(srp_dev);
2504
2505free_attr:
2506 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002507}
2508
2509static void srp_remove_one(struct ib_device *device)
2510{
Roland Dreierf5358a12006-06-17 20:37:29 -07002511 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002512 struct srp_host *host, *tmp_host;
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002513 struct srp_target_port *target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002514
Roland Dreierf5358a12006-06-17 20:37:29 -07002515 srp_dev = ib_get_client_data(device, &srp_client);
Dotan Barak1fe0cb82013-06-12 15:20:36 +02002516 if (!srp_dev)
2517 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002518
Roland Dreierf5358a12006-06-17 20:37:29 -07002519 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Tony Jonesee959b02008-02-22 00:13:36 +01002520 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002521 /*
2522 * Wait for the sysfs entry to go away, so that no new
2523 * target ports can be created.
2524 */
2525 wait_for_completion(&host->released);
2526
2527 /*
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002528 * Remove all target ports.
Roland Dreieraef9ec32005-11-02 14:07:13 -08002529 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002530 spin_lock(&host->target_lock);
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002531 list_for_each_entry(target, &host->target_list, list)
2532 srp_queue_remove_work(target);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002533 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002534
2535 /*
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002536 * Wait for target port removal tasks.
Roland Dreieraef9ec32005-11-02 14:07:13 -08002537 */
Bart Van Asscheef6c49d2011-12-26 16:49:18 +00002538 flush_workqueue(system_long_wq);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002539
Roland Dreieraef9ec32005-11-02 14:07:13 -08002540 kfree(host);
2541 }
2542
Roland Dreierf5358a12006-06-17 20:37:29 -07002543 if (srp_dev->fmr_pool)
2544 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2545 ib_dereg_mr(srp_dev->mr);
2546 ib_dealloc_pd(srp_dev->pd);
2547
2548 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002549}
2550
FUJITA Tomonori32368222007-06-27 16:33:12 +09002551static struct srp_function_template ib_srp_transport_functions = {
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +02002552 .rport_delete = srp_rport_delete,
FUJITA Tomonori32368222007-06-27 16:33:12 +09002553};
2554
Roland Dreieraef9ec32005-11-02 14:07:13 -08002555static int __init srp_init_module(void)
2556{
2557 int ret;
2558
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05002559 BUILD_BUG_ON(FIELD_SIZEOF(struct ib_wc, wr_id) < sizeof(void *));
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00002560
David Dillow49248642011-01-14 18:23:24 -05002561 if (srp_sg_tablesize) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002562 pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n");
David Dillow49248642011-01-14 18:23:24 -05002563 if (!cmd_sg_entries)
2564 cmd_sg_entries = srp_sg_tablesize;
2565 }
2566
2567 if (!cmd_sg_entries)
2568 cmd_sg_entries = SRP_DEF_SG_TABLESIZE;
2569
2570 if (cmd_sg_entries > 255) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002571 pr_warn("Clamping cmd_sg_entries to 255\n");
David Dillow49248642011-01-14 18:23:24 -05002572 cmd_sg_entries = 255;
David Dillow1e89a192008-04-16 21:01:12 -07002573 }
2574
David Dillowc07d4242011-01-16 13:57:10 -05002575 if (!indirect_sg_entries)
2576 indirect_sg_entries = cmd_sg_entries;
2577 else if (indirect_sg_entries < cmd_sg_entries) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002578 pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n",
2579 cmd_sg_entries);
David Dillowc07d4242011-01-16 13:57:10 -05002580 indirect_sg_entries = cmd_sg_entries;
2581 }
2582
FUJITA Tomonori32368222007-06-27 16:33:12 +09002583 ib_srp_transport_template =
2584 srp_attach_transport(&ib_srp_transport_functions);
2585 if (!ib_srp_transport_template)
2586 return -ENOMEM;
2587
Roland Dreieraef9ec32005-11-02 14:07:13 -08002588 ret = class_register(&srp_class);
2589 if (ret) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002590 pr_err("couldn't register class infiniband_srp\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002591 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002592 return ret;
2593 }
2594
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002595 ib_sa_register_client(&srp_sa_client);
2596
Roland Dreieraef9ec32005-11-02 14:07:13 -08002597 ret = ib_register_client(&srp_client);
2598 if (ret) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002599 pr_err("couldn't register IB client\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002600 srp_release_transport(ib_srp_transport_template);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002601 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002602 class_unregister(&srp_class);
2603 return ret;
2604 }
2605
2606 return 0;
2607}
2608
2609static void __exit srp_cleanup_module(void)
2610{
2611 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002612 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002613 class_unregister(&srp_class);
FUJITA Tomonori32368222007-06-27 16:33:12 +09002614 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002615}
2616
2617module_init(srp_init_module);
2618module_exit(srp_cleanup_module);