blob: ac66e6b43ee200030b9882e9e3d49d8d0cda049b [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 Kondapalli3d1ff482007-08-03 10:45:18 -0700118 static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700119
120 return topspin_workarounds &&
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700121 (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
122 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700123}
124
Roland Dreieraef9ec32005-11-02 14:07:13 -0800125static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
126 gfp_t gfp_mask,
127 enum dma_data_direction direction)
128{
129 struct srp_iu *iu;
130
131 iu = kmalloc(sizeof *iu, gfp_mask);
132 if (!iu)
133 goto out;
134
135 iu->buf = kzalloc(size, gfp_mask);
136 if (!iu->buf)
137 goto out_free_iu;
138
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100139 iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
140 direction);
141 if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800142 goto out_free_buf;
143
144 iu->size = size;
145 iu->direction = direction;
146
147 return iu;
148
149out_free_buf:
150 kfree(iu->buf);
151out_free_iu:
152 kfree(iu);
153out:
154 return NULL;
155}
156
157static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
158{
159 if (!iu)
160 return;
161
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100162 ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
163 iu->direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800164 kfree(iu->buf);
165 kfree(iu);
166}
167
168static void srp_qp_event(struct ib_event *event, void *context)
169{
Bart Van Asschee0bda7d2012-01-14 12:39:44 +0000170 pr_debug("QP event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800171}
172
173static int srp_init_qp(struct srp_target_port *target,
174 struct ib_qp *qp)
175{
176 struct ib_qp_attr *attr;
177 int ret;
178
179 attr = kmalloc(sizeof *attr, GFP_KERNEL);
180 if (!attr)
181 return -ENOMEM;
182
Roland Dreier969a60f2008-07-14 23:48:43 -0700183 ret = ib_find_pkey(target->srp_host->srp_dev->dev,
184 target->srp_host->port,
185 be16_to_cpu(target->path.pkey),
186 &attr->pkey_index);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800187 if (ret)
188 goto out;
189
190 attr->qp_state = IB_QPS_INIT;
191 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
192 IB_ACCESS_REMOTE_WRITE);
193 attr->port_num = target->srp_host->port;
194
195 ret = ib_modify_qp(qp, attr,
196 IB_QP_STATE |
197 IB_QP_PKEY_INDEX |
198 IB_QP_ACCESS_FLAGS |
199 IB_QP_PORT);
200
201out:
202 kfree(attr);
203 return ret;
204}
205
David Dillow9fe4bcf2008-01-08 17:08:52 -0500206static int srp_new_cm_id(struct srp_target_port *target)
207{
208 struct ib_cm_id *new_cm_id;
209
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100210 new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
David Dillow9fe4bcf2008-01-08 17:08:52 -0500211 srp_cm_handler, target);
212 if (IS_ERR(new_cm_id))
213 return PTR_ERR(new_cm_id);
214
215 if (target->cm_id)
216 ib_destroy_cm_id(target->cm_id);
217 target->cm_id = new_cm_id;
218
219 return 0;
220}
221
Roland Dreieraef9ec32005-11-02 14:07:13 -0800222static int srp_create_target_ib(struct srp_target_port *target)
223{
224 struct ib_qp_init_attr *init_attr;
225 int ret;
226
227 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
228 if (!init_attr)
229 return -ENOMEM;
230
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000231 target->recv_cq = ib_create_cq(target->srp_host->srp_dev->dev,
232 srp_recv_completion, NULL, target, SRP_RQ_SIZE, 0);
233 if (IS_ERR(target->recv_cq)) {
234 ret = PTR_ERR(target->recv_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800235 goto err;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800236 }
237
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000238 target->send_cq = ib_create_cq(target->srp_host->srp_dev->dev,
239 srp_send_completion, NULL, target, SRP_SQ_SIZE, 0);
240 if (IS_ERR(target->send_cq)) {
241 ret = PTR_ERR(target->send_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800242 goto err_recv_cq;
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000243 }
244
245 ib_req_notify_cq(target->recv_cq, IB_CQ_NEXT_COMP);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800246
247 init_attr->event_handler = srp_qp_event;
248 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
249 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
250 init_attr->cap.max_recv_sge = 1;
251 init_attr->cap.max_send_sge = 1;
252 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
253 init_attr->qp_type = IB_QPT_RC;
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000254 init_attr->send_cq = target->send_cq;
255 init_attr->recv_cq = target->recv_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800256
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100257 target->qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800258 if (IS_ERR(target->qp)) {
259 ret = PTR_ERR(target->qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800260 goto err_send_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800261 }
262
263 ret = srp_init_qp(target, target->qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800264 if (ret)
265 goto err_qp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800266
Roland Dreierda9d2f02010-02-24 15:07:59 -0800267 kfree(init_attr);
268 return 0;
269
270err_qp:
271 ib_destroy_qp(target->qp);
272
273err_send_cq:
274 ib_destroy_cq(target->send_cq);
275
276err_recv_cq:
277 ib_destroy_cq(target->recv_cq);
278
279err:
Roland Dreieraef9ec32005-11-02 14:07:13 -0800280 kfree(init_attr);
281 return ret;
282}
283
284static void srp_free_target_ib(struct srp_target_port *target)
285{
286 int i;
287
288 ib_destroy_qp(target->qp);
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000289 ib_destroy_cq(target->send_cq);
290 ib_destroy_cq(target->recv_cq);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800291
292 for (i = 0; i < SRP_RQ_SIZE; ++i)
293 srp_free_iu(target->srp_host, target->rx_ring[i]);
Bart Van Asschedd5e6e32010-08-30 19:27:20 +0000294 for (i = 0; i < SRP_SQ_SIZE; ++i)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800295 srp_free_iu(target->srp_host, target->tx_ring[i]);
296}
297
298static void srp_path_rec_completion(int status,
299 struct ib_sa_path_rec *pathrec,
300 void *target_ptr)
301{
302 struct srp_target_port *target = target_ptr;
303
304 target->status = status;
305 if (status)
David Dillow7aa54bd2008-01-07 18:23:41 -0500306 shost_printk(KERN_ERR, target->scsi_host,
307 PFX "Got failed path rec status %d\n", status);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800308 else
309 target->path = *pathrec;
310 complete(&target->done);
311}
312
313static int srp_lookup_path(struct srp_target_port *target)
314{
315 target->path.numb_path = 1;
316
317 init_completion(&target->done);
318
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700319 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100320 target->srp_host->srp_dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800321 target->srp_host->port,
322 &target->path,
Sean Hefty247e0202007-08-08 15:51:18 -0700323 IB_SA_PATH_REC_SERVICE_ID |
Roland Dreieraef9ec32005-11-02 14:07:13 -0800324 IB_SA_PATH_REC_DGID |
325 IB_SA_PATH_REC_SGID |
326 IB_SA_PATH_REC_NUMB_PATH |
327 IB_SA_PATH_REC_PKEY,
328 SRP_PATH_REC_TIMEOUT_MS,
329 GFP_KERNEL,
330 srp_path_rec_completion,
331 target, &target->path_query);
332 if (target->path_query_id < 0)
333 return target->path_query_id;
334
335 wait_for_completion(&target->done);
336
337 if (target->status < 0)
David Dillow7aa54bd2008-01-07 18:23:41 -0500338 shost_printk(KERN_WARNING, target->scsi_host,
339 PFX "Path record query failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800340
341 return target->status;
342}
343
344static int srp_send_req(struct srp_target_port *target)
345{
346 struct {
347 struct ib_cm_req_param param;
348 struct srp_login_req priv;
349 } *req = NULL;
350 int status;
351
352 req = kzalloc(sizeof *req, GFP_KERNEL);
353 if (!req)
354 return -ENOMEM;
355
356 req->param.primary_path = &target->path;
357 req->param.alternate_path = NULL;
358 req->param.service_id = target->service_id;
359 req->param.qp_num = target->qp->qp_num;
360 req->param.qp_type = target->qp->qp_type;
361 req->param.private_data = &req->priv;
362 req->param.private_data_len = sizeof req->priv;
363 req->param.flow_control = 1;
364
365 get_random_bytes(&req->param.starting_psn, 4);
366 req->param.starting_psn &= 0xffffff;
367
368 /*
369 * Pick some arbitrary defaults here; we could make these
370 * module parameters if anyone cared about setting them.
371 */
372 req->param.responder_resources = 4;
373 req->param.remote_cm_response_timeout = 20;
374 req->param.local_cm_response_timeout = 20;
375 req->param.retry_count = 7;
376 req->param.rnr_retry_count = 7;
377 req->param.max_cm_retries = 15;
378
379 req->priv.opcode = SRP_LOGIN_REQ;
380 req->priv.tag = 0;
David Dillow49248642011-01-14 18:23:24 -0500381 req->priv.req_it_iu_len = cpu_to_be32(target->max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800382 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
383 SRP_BUF_FORMAT_INDIRECT);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700384 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700385 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450db2006-06-17 20:37:38 -0700386 * port identifier format is 8 bytes of ID extension followed
387 * by 8 bytes of GUID. Older drafts put the two halves in the
388 * opposite order, so that the GUID comes first.
389 *
390 * Targets conforming to these obsolete drafts can be
391 * recognized by the I/O Class they report.
392 */
393 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
394 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200395 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700396 memcpy(req->priv.initiator_port_id + 8,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200397 &target->initiator_ext, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700398 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
399 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
400 } else {
401 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200402 &target->initiator_ext, 8);
403 memcpy(req->priv.initiator_port_id + 8,
404 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700405 memcpy(req->priv.target_port_id, &target->id_ext, 8);
406 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
407 }
408
Roland Dreieraef9ec32005-11-02 14:07:13 -0800409 /*
410 * Topspin/Cisco SRP targets will reject our login unless we
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200411 * zero out the first 8 bytes of our initiator port ID and set
412 * the second 8 bytes to the local node GUID.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800413 */
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700414 if (srp_target_is_topspin(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500415 shost_printk(KERN_DEBUG, target->scsi_host,
416 PFX "Topspin/Cisco initiator port ID workaround "
417 "activated for target GUID %016llx\n",
418 (unsigned long long) be64_to_cpu(target->ioc_guid));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800419 memset(req->priv.initiator_port_id, 0, 8);
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200420 memcpy(req->priv.initiator_port_id + 8,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100421 &target->srp_host->srp_dev->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800422 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800423
424 status = ib_send_cm_req(target->cm_id, &req->param);
425
426 kfree(req);
427
428 return status;
429}
430
431static void srp_disconnect_target(struct srp_target_port *target)
432{
433 /* XXX should send SRP_I_LOGOUT request */
434
435 init_completion(&target->done);
Roland Dreiere6581052006-05-17 09:13:21 -0700436 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500437 shost_printk(KERN_DEBUG, target->scsi_host,
438 PFX "Sending CM DREQ failed\n");
Roland Dreiere6581052006-05-17 09:13:21 -0700439 return;
440 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800441 wait_for_completion(&target->done);
442}
443
Bart Van Assche9709f0e2010-11-26 13:13:06 -0500444static bool srp_change_state(struct srp_target_port *target,
445 enum srp_target_state old,
446 enum srp_target_state new)
447{
448 bool changed = false;
449
Bart Van Asschee9684672010-11-26 15:08:38 -0500450 spin_lock_irq(&target->lock);
Bart Van Assche9709f0e2010-11-26 13:13:06 -0500451 if (target->state == old) {
452 target->state = new;
453 changed = true;
454 }
Bart Van Asschee9684672010-11-26 15:08:38 -0500455 spin_unlock_irq(&target->lock);
Bart Van Assche9709f0e2010-11-26 13:13:06 -0500456 return changed;
457}
458
David Dillow8f26c9f2011-01-14 19:45:50 -0500459static void srp_free_req_data(struct srp_target_port *target)
460{
David Dillowc07d4242011-01-16 13:57:10 -0500461 struct ib_device *ibdev = target->srp_host->srp_dev->dev;
David Dillow8f26c9f2011-01-14 19:45:50 -0500462 struct srp_request *req;
463 int i;
464
465 for (i = 0, req = target->req_ring; i < SRP_CMD_SQ_SIZE; ++i, ++req) {
466 kfree(req->fmr_list);
467 kfree(req->map_page);
David Dillowc07d4242011-01-16 13:57:10 -0500468 if (req->indirect_dma_addr) {
469 ib_dma_unmap_single(ibdev, req->indirect_dma_addr,
470 target->indirect_size,
471 DMA_TO_DEVICE);
472 }
473 kfree(req->indirect_desc);
David Dillow8f26c9f2011-01-14 19:45:50 -0500474 }
475}
476
Bart Van Assche683b1592012-01-14 12:40:44 +0000477/**
478 * srp_del_scsi_host_attr() - Remove attributes defined in the host template.
479 * @shost: SCSI host whose attributes to remove from sysfs.
480 *
481 * Note: Any attributes defined in the host template and that did not exist
482 * before invocation of this function will be ignored.
483 */
484static void srp_del_scsi_host_attr(struct Scsi_Host *shost)
485{
486 struct device_attribute **attr;
487
488 for (attr = shost->hostt->shost_attrs; attr && *attr; ++attr)
489 device_remove_file(&shost->shost_dev, *attr);
490}
491
David Howellsc4028952006-11-22 14:57:56 +0000492static void srp_remove_work(struct work_struct *work)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800493{
David Howellsc4028952006-11-22 14:57:56 +0000494 struct srp_target_port *target =
495 container_of(work, struct srp_target_port, work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800496
Bart Van Assche9709f0e2010-11-26 13:13:06 -0500497 if (!srp_change_state(target, SRP_TARGET_DEAD, SRP_TARGET_REMOVED))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800498 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800499
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700500 spin_lock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800501 list_del(&target->list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700502 spin_unlock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800503
Bart Van Assche683b1592012-01-14 12:40:44 +0000504 srp_del_scsi_host_attr(target->scsi_host);
FUJITA Tomonori32368222007-06-27 16:33:12 +0900505 srp_remove_host(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800506 scsi_remove_host(target->scsi_host);
507 ib_destroy_cm_id(target->cm_id);
508 srp_free_target_ib(target);
David Dillow8f26c9f2011-01-14 19:45:50 -0500509 srp_free_req_data(target);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800510 scsi_host_put(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800511}
512
513static int srp_connect_target(struct srp_target_port *target)
514{
David Dillow9fe4bcf2008-01-08 17:08:52 -0500515 int retries = 3;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800516 int ret;
517
518 ret = srp_lookup_path(target);
519 if (ret)
520 return ret;
521
522 while (1) {
523 init_completion(&target->done);
524 ret = srp_send_req(target);
525 if (ret)
526 return ret;
527 wait_for_completion(&target->done);
528
529 /*
530 * The CM event handling code will set status to
531 * SRP_PORT_REDIRECT if we get a port redirect REJ
532 * back, or SRP_DLID_REDIRECT if we get a lid/qp
533 * redirect REJ back.
534 */
535 switch (target->status) {
536 case 0:
537 return 0;
538
539 case SRP_PORT_REDIRECT:
540 ret = srp_lookup_path(target);
541 if (ret)
542 return ret;
543 break;
544
545 case SRP_DLID_REDIRECT:
546 break;
547
David Dillow9fe4bcf2008-01-08 17:08:52 -0500548 case SRP_STALE_CONN:
549 /* Our current CM id was stale, and is now in timewait.
550 * Try to reconnect with a new one.
551 */
552 if (!retries-- || srp_new_cm_id(target)) {
553 shost_printk(KERN_ERR, target->scsi_host, PFX
554 "giving up on stale connection\n");
555 target->status = -ECONNRESET;
556 return target->status;
557 }
558
559 shost_printk(KERN_ERR, target->scsi_host, PFX
560 "retrying stale connection\n");
561 break;
562
Roland Dreieraef9ec32005-11-02 14:07:13 -0800563 default:
564 return target->status;
565 }
566 }
567}
568
Roland Dreierd945e1d2006-05-09 10:50:28 -0700569static void srp_unmap_data(struct scsi_cmnd *scmnd,
570 struct srp_target_port *target,
571 struct srp_request *req)
572{
David Dillow8f26c9f2011-01-14 19:45:50 -0500573 struct ib_device *ibdev = target->srp_host->srp_dev->dev;
574 struct ib_pool_fmr **pfmr;
575
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900576 if (!scsi_sglist(scmnd) ||
Roland Dreierd945e1d2006-05-09 10:50:28 -0700577 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
578 scmnd->sc_data_direction != DMA_FROM_DEVICE))
579 return;
580
David Dillow8f26c9f2011-01-14 19:45:50 -0500581 pfmr = req->fmr_list;
582 while (req->nfmr--)
583 ib_fmr_pool_unmap(*pfmr++);
Roland Dreierf5358a12006-06-17 20:37:29 -0700584
David Dillow8f26c9f2011-01-14 19:45:50 -0500585 ib_dma_unmap_sg(ibdev, scsi_sglist(scmnd), scsi_sg_count(scmnd),
586 scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700587}
588
Bart Van Assche22032992012-08-14 13:18:53 +0000589/**
590 * srp_claim_req - Take ownership of the scmnd associated with a request.
591 * @target: SRP target port.
592 * @req: SRP request.
593 * @scmnd: If NULL, take ownership of @req->scmnd. If not NULL, only take
594 * ownership of @req->scmnd if it equals @scmnd.
595 *
596 * Return value:
597 * Either NULL or a pointer to the SCSI command the caller became owner of.
598 */
599static struct scsi_cmnd *srp_claim_req(struct srp_target_port *target,
600 struct srp_request *req,
601 struct scsi_cmnd *scmnd)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700602{
Bart Van Assche94a91742010-11-26 14:50:09 -0500603 unsigned long flags;
604
Bart Van Assche22032992012-08-14 13:18:53 +0000605 spin_lock_irqsave(&target->lock, flags);
606 if (!scmnd) {
607 scmnd = req->scmnd;
608 req->scmnd = NULL;
609 } else if (req->scmnd == scmnd) {
610 req->scmnd = NULL;
611 } else {
612 scmnd = NULL;
613 }
614 spin_unlock_irqrestore(&target->lock, flags);
615
616 return scmnd;
617}
618
619/**
620 * srp_free_req() - Unmap data and add request to the free request list.
621 */
622static void srp_free_req(struct srp_target_port *target,
623 struct srp_request *req, struct scsi_cmnd *scmnd,
624 s32 req_lim_delta)
625{
626 unsigned long flags;
627
628 srp_unmap_data(scmnd, target, req);
629
Bart Van Asschee9684672010-11-26 15:08:38 -0500630 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -0500631 target->req_lim += req_lim_delta;
Bart Van Assche536ae142010-11-26 13:58:27 -0500632 list_add_tail(&req->list, &target->free_reqs);
Bart Van Asschee9684672010-11-26 15:08:38 -0500633 spin_unlock_irqrestore(&target->lock, flags);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700634}
635
636static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
637{
Bart Van Assche22032992012-08-14 13:18:53 +0000638 struct scsi_cmnd *scmnd = srp_claim_req(target, req, NULL);
639
640 if (scmnd) {
Bart Van Assche9b796d02012-08-24 10:27:54 +0000641 srp_free_req(target, req, scmnd, 0);
Bart Van Assche22032992012-08-14 13:18:53 +0000642 scmnd->result = DID_RESET << 16;
643 scmnd->scsi_done(scmnd);
Bart Van Assche22032992012-08-14 13:18:53 +0000644 }
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700645}
646
Roland Dreieraef9ec32005-11-02 14:07:13 -0800647static int srp_reconnect_target(struct srp_target_port *target)
648{
Roland Dreieraef9ec32005-11-02 14:07:13 -0800649 struct ib_qp_attr qp_attr;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800650 struct ib_wc wc;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -0500651 int i, ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800652
Bart Van Assche9709f0e2010-11-26 13:13:06 -0500653 if (!srp_change_state(target, SRP_TARGET_LIVE, SRP_TARGET_CONNECTING))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800654 return -EAGAIN;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800655
656 srp_disconnect_target(target);
657 /*
658 * Now get a new local CM ID so that we avoid confusing the
659 * target in case things are really fouled up.
660 */
David Dillow9fe4bcf2008-01-08 17:08:52 -0500661 ret = srp_new_cm_id(target);
662 if (ret)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800663 goto err;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800664
665 qp_attr.qp_state = IB_QPS_RESET;
666 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
667 if (ret)
668 goto err;
669
670 ret = srp_init_qp(target, target->qp);
671 if (ret)
672 goto err;
673
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000674 while (ib_poll_cq(target->recv_cq, 1, &wc) > 0)
675 ; /* nothing */
676 while (ib_poll_cq(target->send_cq, 1, &wc) > 0)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800677 ; /* nothing */
678
Bart Van Assche536ae142010-11-26 13:58:27 -0500679 for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
680 struct srp_request *req = &target->req_ring[i];
681 if (req->scmnd)
682 srp_reset_req(target, req);
683 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800684
Bart Van Assche536ae142010-11-26 13:58:27 -0500685 INIT_LIST_HEAD(&target->free_tx);
Bart Van Asschedcb4cb82010-11-26 13:22:48 -0500686 for (i = 0; i < SRP_SQ_SIZE; ++i)
Bart Van Assche536ae142010-11-26 13:58:27 -0500687 list_add(&target->tx_ring[i]->list, &target->free_tx);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800688
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200689 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800690 ret = srp_connect_target(target);
691 if (ret)
692 goto err;
693
Bart Van Assche9709f0e2010-11-26 13:13:06 -0500694 if (!srp_change_state(target, SRP_TARGET_CONNECTING, SRP_TARGET_LIVE))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800695 ret = -EAGAIN;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800696
697 return ret;
698
699err:
David Dillow7aa54bd2008-01-07 18:23:41 -0500700 shost_printk(KERN_ERR, target->scsi_host,
701 PFX "reconnect failed (%d), removing target port.\n", ret);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800702
703 /*
704 * We couldn't reconnect, so kill our target port off.
Bart Van Assche9709f0e2010-11-26 13:13:06 -0500705 * However, we have to defer the real removal because we
706 * are in the context of the SCSI error handler now, which
707 * will deadlock if we call scsi_remove_host().
708 *
709 * Schedule our work inside the lock to avoid a race with
710 * the flush_scheduled_work() in srp_remove_one().
Roland Dreieraef9ec32005-11-02 14:07:13 -0800711 */
Bart Van Asschee9684672010-11-26 15:08:38 -0500712 spin_lock_irq(&target->lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800713 if (target->state == SRP_TARGET_CONNECTING) {
714 target->state = SRP_TARGET_DEAD;
David Howellsc4028952006-11-22 14:57:56 +0000715 INIT_WORK(&target->work, srp_remove_work);
Tejun Heof0626712010-10-19 15:24:36 +0000716 queue_work(ib_wq, &target->work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800717 }
Bart Van Asschee9684672010-11-26 15:08:38 -0500718 spin_unlock_irq(&target->lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800719
720 return ret;
721}
722
David Dillow8f26c9f2011-01-14 19:45:50 -0500723static void srp_map_desc(struct srp_map_state *state, dma_addr_t dma_addr,
724 unsigned int dma_len, u32 rkey)
Roland Dreierf5358a12006-06-17 20:37:29 -0700725{
David Dillow8f26c9f2011-01-14 19:45:50 -0500726 struct srp_direct_buf *desc = state->desc;
727
728 desc->va = cpu_to_be64(dma_addr);
729 desc->key = cpu_to_be32(rkey);
730 desc->len = cpu_to_be32(dma_len);
731
732 state->total_len += dma_len;
733 state->desc++;
734 state->ndesc++;
735}
736
737static int srp_map_finish_fmr(struct srp_map_state *state,
738 struct srp_target_port *target)
739{
740 struct srp_device *dev = target->srp_host->srp_dev;
741 struct ib_pool_fmr *fmr;
Roland Dreierf5358a12006-06-17 20:37:29 -0700742 u64 io_addr = 0;
David Dillow8f26c9f2011-01-14 19:45:50 -0500743
744 if (!state->npages)
745 return 0;
746
747 if (state->npages == 1) {
748 srp_map_desc(state, state->base_dma_addr, state->fmr_len,
749 target->rkey);
750 state->npages = state->fmr_len = 0;
751 return 0;
752 }
753
754 fmr = ib_fmr_pool_map_phys(dev->fmr_pool, state->pages,
755 state->npages, io_addr);
756 if (IS_ERR(fmr))
757 return PTR_ERR(fmr);
758
759 *state->next_fmr++ = fmr;
760 state->nfmr++;
761
762 srp_map_desc(state, 0, state->fmr_len, fmr->fmr->rkey);
763 state->npages = state->fmr_len = 0;
764 return 0;
765}
766
767static void srp_map_update_start(struct srp_map_state *state,
768 struct scatterlist *sg, int sg_index,
769 dma_addr_t dma_addr)
770{
771 state->unmapped_sg = sg;
772 state->unmapped_index = sg_index;
773 state->unmapped_addr = dma_addr;
774}
775
776static int srp_map_sg_entry(struct srp_map_state *state,
777 struct srp_target_port *target,
778 struct scatterlist *sg, int sg_index,
779 int use_fmr)
780{
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100781 struct srp_device *dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800782 struct ib_device *ibdev = dev->dev;
David Dillow8f26c9f2011-01-14 19:45:50 -0500783 dma_addr_t dma_addr = ib_sg_dma_address(ibdev, sg);
784 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
785 unsigned int len;
786 int ret;
Roland Dreierf5358a12006-06-17 20:37:29 -0700787
David Dillow8f26c9f2011-01-14 19:45:50 -0500788 if (!dma_len)
789 return 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700790
David Dillow8f26c9f2011-01-14 19:45:50 -0500791 if (use_fmr == SRP_MAP_NO_FMR) {
792 /* Once we're in direct map mode for a request, we don't
793 * go back to FMR mode, so no need to update anything
794 * other than the descriptor.
795 */
796 srp_map_desc(state, dma_addr, dma_len, target->rkey);
797 return 0;
798 }
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700799
David Dillow8f26c9f2011-01-14 19:45:50 -0500800 /* If we start at an offset into the FMR page, don't merge into
801 * the current FMR. Finish it out, and use the kernel's MR for this
802 * sg entry. This is to avoid potential bugs on some SRP targets
803 * that were never quite defined, but went away when the initiator
804 * avoided using FMR on such page fragments.
805 */
806 if (dma_addr & ~dev->fmr_page_mask || dma_len > dev->fmr_max_size) {
807 ret = srp_map_finish_fmr(state, target);
808 if (ret)
809 return ret;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800810
David Dillow8f26c9f2011-01-14 19:45:50 -0500811 srp_map_desc(state, dma_addr, dma_len, target->rkey);
812 srp_map_update_start(state, NULL, 0, 0);
813 return 0;
814 }
815
816 /* If this is the first sg to go into the FMR, save our position.
817 * We need to know the first unmapped entry, its index, and the
818 * first unmapped address within that entry to be able to restart
819 * mapping after an error.
820 */
821 if (!state->unmapped_sg)
822 srp_map_update_start(state, sg, sg_index, dma_addr);
823
824 while (dma_len) {
825 if (state->npages == SRP_FMR_SIZE) {
826 ret = srp_map_finish_fmr(state, target);
827 if (ret)
828 return ret;
829
830 srp_map_update_start(state, sg, sg_index, dma_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -0700831 }
832
David Dillow8f26c9f2011-01-14 19:45:50 -0500833 len = min_t(unsigned int, dma_len, dev->fmr_page_size);
834
835 if (!state->npages)
836 state->base_dma_addr = dma_addr;
837 state->pages[state->npages++] = dma_addr;
838 state->fmr_len += len;
839 dma_addr += len;
840 dma_len -= len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700841 }
842
David Dillow8f26c9f2011-01-14 19:45:50 -0500843 /* If the last entry of the FMR wasn't a full page, then we need to
844 * close it out and start a new one -- we can only merge at page
845 * boundries.
846 */
Roland Dreierf5358a12006-06-17 20:37:29 -0700847 ret = 0;
David Dillow8f26c9f2011-01-14 19:45:50 -0500848 if (len != dev->fmr_page_size) {
849 ret = srp_map_finish_fmr(state, target);
850 if (!ret)
851 srp_map_update_start(state, NULL, 0, 0);
852 }
Roland Dreierf5358a12006-06-17 20:37:29 -0700853 return ret;
854}
855
Roland Dreieraef9ec32005-11-02 14:07:13 -0800856static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
857 struct srp_request *req)
858{
David Dillow8f26c9f2011-01-14 19:45:50 -0500859 struct scatterlist *scat, *sg;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800860 struct srp_cmd *cmd = req->cmd->buf;
David Dillow8f26c9f2011-01-14 19:45:50 -0500861 int i, len, nents, count, use_fmr;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800862 struct srp_device *dev;
863 struct ib_device *ibdev;
David Dillow8f26c9f2011-01-14 19:45:50 -0500864 struct srp_map_state state;
865 struct srp_indirect_buf *indirect_hdr;
David Dillow8f26c9f2011-01-14 19:45:50 -0500866 u32 table_len;
867 u8 fmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800868
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900869 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800870 return sizeof (struct srp_cmd);
871
872 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
873 scmnd->sc_data_direction != DMA_TO_DEVICE) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500874 shost_printk(KERN_WARNING, target->scsi_host,
875 PFX "Unhandled data direction %d\n",
876 scmnd->sc_data_direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800877 return -EINVAL;
878 }
879
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900880 nents = scsi_sg_count(scmnd);
881 scat = scsi_sglist(scmnd);
Roland Dreiercf368712006-03-24 15:47:26 -0800882
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100883 dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800884 ibdev = dev->dev;
885
886 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
David Dillow8f26c9f2011-01-14 19:45:50 -0500887 if (unlikely(count == 0))
888 return -EIO;
Roland Dreierf5358a12006-06-17 20:37:29 -0700889
890 fmt = SRP_DATA_DESC_DIRECT;
891 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800892
893 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700894 /*
895 * The midlayer only generated a single gather/scatter
896 * entry, or DMA mapping coalesced everything to a
897 * single entry. So a direct descriptor along with
898 * the DMA MR suffices.
899 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800900 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800901
Ralph Campbell85507bc2006-12-12 14:30:55 -0800902 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
David Dillow9af76272010-11-26 15:34:46 -0500903 buf->key = cpu_to_be32(target->rkey);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800904 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Roland Dreiercf368712006-03-24 15:47:26 -0800905
David Dillow8f26c9f2011-01-14 19:45:50 -0500906 req->nfmr = 0;
907 goto map_complete;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800908 }
909
David Dillow8f26c9f2011-01-14 19:45:50 -0500910 /* We have more than one scatter/gather entry, so build our indirect
911 * descriptor table, trying to merge as many entries with FMR as we
912 * can.
913 */
914 indirect_hdr = (void *) cmd->add_data;
915
David Dillowc07d4242011-01-16 13:57:10 -0500916 ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr,
917 target->indirect_size, DMA_TO_DEVICE);
918
David Dillow8f26c9f2011-01-14 19:45:50 -0500919 memset(&state, 0, sizeof(state));
David Dillowc07d4242011-01-16 13:57:10 -0500920 state.desc = req->indirect_desc;
David Dillow8f26c9f2011-01-14 19:45:50 -0500921 state.pages = req->map_page;
922 state.next_fmr = req->fmr_list;
923
924 use_fmr = dev->fmr_pool ? SRP_MAP_ALLOW_FMR : SRP_MAP_NO_FMR;
925
926 for_each_sg(scat, sg, count, i) {
927 if (srp_map_sg_entry(&state, target, sg, i, use_fmr)) {
928 /* FMR mapping failed, so backtrack to the first
929 * unmapped entry and continue on without using FMR.
930 */
931 dma_addr_t dma_addr;
932 unsigned int dma_len;
933
934backtrack:
935 sg = state.unmapped_sg;
936 i = state.unmapped_index;
937
938 dma_addr = ib_sg_dma_address(ibdev, sg);
939 dma_len = ib_sg_dma_len(ibdev, sg);
940 dma_len -= (state.unmapped_addr - dma_addr);
941 dma_addr = state.unmapped_addr;
942 use_fmr = SRP_MAP_NO_FMR;
943 srp_map_desc(&state, dma_addr, dma_len, target->rkey);
944 }
945 }
946
947 if (use_fmr == SRP_MAP_ALLOW_FMR && srp_map_finish_fmr(&state, target))
948 goto backtrack;
949
David Dillowc07d4242011-01-16 13:57:10 -0500950 /* We've mapped the request, now pull as much of the indirect
951 * descriptor table as we can into the command buffer. If this
952 * target is not using an external indirect table, we are
953 * guaranteed to fit into the command, as the SCSI layer won't
954 * give us more S/G entries than we allow.
David Dillow8f26c9f2011-01-14 19:45:50 -0500955 */
956 req->nfmr = state.nfmr;
957 if (state.ndesc == 1) {
958 /* FMR mapping was able to collapse this to one entry,
959 * so use a direct descriptor.
960 */
961 struct srp_direct_buf *buf = (void *) cmd->add_data;
962
David Dillowc07d4242011-01-16 13:57:10 -0500963 *buf = req->indirect_desc[0];
David Dillow8f26c9f2011-01-14 19:45:50 -0500964 goto map_complete;
965 }
966
David Dillowc07d4242011-01-16 13:57:10 -0500967 if (unlikely(target->cmd_sg_cnt < state.ndesc &&
968 !target->allow_ext_sg)) {
969 shost_printk(KERN_ERR, target->scsi_host,
970 "Could not fit S/G list into SRP_CMD\n");
971 return -EIO;
972 }
973
974 count = min(state.ndesc, target->cmd_sg_cnt);
David Dillow8f26c9f2011-01-14 19:45:50 -0500975 table_len = state.ndesc * sizeof (struct srp_direct_buf);
976
977 fmt = SRP_DATA_DESC_INDIRECT;
978 len = sizeof(struct srp_cmd) + sizeof (struct srp_indirect_buf);
David Dillowc07d4242011-01-16 13:57:10 -0500979 len += count * sizeof (struct srp_direct_buf);
David Dillow8f26c9f2011-01-14 19:45:50 -0500980
David Dillowc07d4242011-01-16 13:57:10 -0500981 memcpy(indirect_hdr->desc_list, req->indirect_desc,
982 count * sizeof (struct srp_direct_buf));
David Dillow8f26c9f2011-01-14 19:45:50 -0500983
David Dillowc07d4242011-01-16 13:57:10 -0500984 indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr);
David Dillow8f26c9f2011-01-14 19:45:50 -0500985 indirect_hdr->table_desc.key = cpu_to_be32(target->rkey);
986 indirect_hdr->table_desc.len = cpu_to_be32(table_len);
987 indirect_hdr->len = cpu_to_be32(state.total_len);
988
989 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
David Dillowc07d4242011-01-16 13:57:10 -0500990 cmd->data_out_desc_cnt = count;
David Dillow8f26c9f2011-01-14 19:45:50 -0500991 else
David Dillowc07d4242011-01-16 13:57:10 -0500992 cmd->data_in_desc_cnt = count;
993
994 ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len,
995 DMA_TO_DEVICE);
David Dillow8f26c9f2011-01-14 19:45:50 -0500996
997map_complete:
Roland Dreieraef9ec32005-11-02 14:07:13 -0800998 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
999 cmd->buf_fmt = fmt << 4;
1000 else
1001 cmd->buf_fmt = fmt;
1002
Roland Dreieraef9ec32005-11-02 14:07:13 -08001003 return len;
1004}
1005
David Dillow05a1d752010-10-08 14:48:14 -04001006/*
Bart Van Assche76c75b22010-11-26 14:37:47 -05001007 * Return an IU and possible credit to the free pool
1008 */
1009static void srp_put_tx_iu(struct srp_target_port *target, struct srp_iu *iu,
1010 enum srp_iu_type iu_type)
1011{
1012 unsigned long flags;
1013
Bart Van Asschee9684672010-11-26 15:08:38 -05001014 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001015 list_add(&iu->list, &target->free_tx);
1016 if (iu_type != SRP_IU_RSP)
1017 ++target->req_lim;
Bart Van Asschee9684672010-11-26 15:08:38 -05001018 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001019}
1020
1021/*
Bart Van Asschee9684672010-11-26 15:08:38 -05001022 * Must be called with target->lock held to protect req_lim and free_tx.
1023 * If IU is not sent, it must be returned using srp_put_tx_iu().
David Dillow05a1d752010-10-08 14:48:14 -04001024 *
1025 * Note:
1026 * An upper limit for the number of allocated information units for each
1027 * request type is:
1028 * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues
1029 * more than Scsi_Host.can_queue requests.
1030 * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE.
1031 * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than
1032 * one unanswered SRP request to an initiator.
1033 */
1034static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
1035 enum srp_iu_type iu_type)
1036{
1037 s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE;
1038 struct srp_iu *iu;
1039
1040 srp_send_completion(target->send_cq, target);
1041
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001042 if (list_empty(&target->free_tx))
David Dillow05a1d752010-10-08 14:48:14 -04001043 return NULL;
1044
1045 /* Initiator responses to target requests do not consume credits */
Bart Van Assche76c75b22010-11-26 14:37:47 -05001046 if (iu_type != SRP_IU_RSP) {
1047 if (target->req_lim <= rsv) {
1048 ++target->zero_req_lim;
1049 return NULL;
1050 }
1051
1052 --target->req_lim;
David Dillow05a1d752010-10-08 14:48:14 -04001053 }
1054
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001055 iu = list_first_entry(&target->free_tx, struct srp_iu, list);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001056 list_del(&iu->list);
David Dillow05a1d752010-10-08 14:48:14 -04001057 return iu;
1058}
1059
Bart Van Assche76c75b22010-11-26 14:37:47 -05001060static int srp_post_send(struct srp_target_port *target,
1061 struct srp_iu *iu, int len)
David Dillow05a1d752010-10-08 14:48:14 -04001062{
1063 struct ib_sge list;
1064 struct ib_send_wr wr, *bad_wr;
David Dillow05a1d752010-10-08 14:48:14 -04001065
1066 list.addr = iu->dma;
1067 list.length = len;
David Dillow9af76272010-11-26 15:34:46 -05001068 list.lkey = target->lkey;
David Dillow05a1d752010-10-08 14:48:14 -04001069
1070 wr.next = NULL;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001071 wr.wr_id = (uintptr_t) iu;
David Dillow05a1d752010-10-08 14:48:14 -04001072 wr.sg_list = &list;
1073 wr.num_sge = 1;
1074 wr.opcode = IB_WR_SEND;
1075 wr.send_flags = IB_SEND_SIGNALED;
1076
Bart Van Assche76c75b22010-11-26 14:37:47 -05001077 return ib_post_send(target->qp, &wr, &bad_wr);
David Dillow05a1d752010-10-08 14:48:14 -04001078}
1079
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001080static int srp_post_recv(struct srp_target_port *target, struct srp_iu *iu)
Bart Van Asschec996bb42010-07-30 10:59:05 +00001081{
Bart Van Asschec996bb42010-07-30 10:59:05 +00001082 struct ib_recv_wr wr, *bad_wr;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001083 struct ib_sge list;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001084
1085 list.addr = iu->dma;
1086 list.length = iu->size;
David Dillow9af76272010-11-26 15:34:46 -05001087 list.lkey = target->lkey;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001088
1089 wr.next = NULL;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001090 wr.wr_id = (uintptr_t) iu;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001091 wr.sg_list = &list;
1092 wr.num_sge = 1;
1093
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001094 return ib_post_recv(target->qp, &wr, &bad_wr);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001095}
1096
Roland Dreieraef9ec32005-11-02 14:07:13 -08001097static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
1098{
1099 struct srp_request *req;
1100 struct scsi_cmnd *scmnd;
1101 unsigned long flags;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001102
Roland Dreieraef9ec32005-11-02 14:07:13 -08001103 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
Bart Van Asschee9684672010-11-26 15:08:38 -05001104 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -05001105 target->req_lim += be32_to_cpu(rsp->req_lim_delta);
Bart Van Asschee9684672010-11-26 15:08:38 -05001106 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -05001107
David Dillowf8b6e312010-11-26 13:02:21 -05001108 target->tsk_mgmt_status = -1;
1109 if (be32_to_cpu(rsp->resp_data_len) >= 4)
1110 target->tsk_mgmt_status = rsp->data[3];
1111 complete(&target->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001112 } else {
David Dillowf8b6e312010-11-26 13:02:21 -05001113 req = &target->req_ring[rsp->tag];
Bart Van Assche22032992012-08-14 13:18:53 +00001114 scmnd = srp_claim_req(target, req, NULL);
1115 if (!scmnd) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001116 shost_printk(KERN_ERR, target->scsi_host,
1117 "Null scmnd for RSP w/tag %016llx\n",
1118 (unsigned long long) rsp->tag);
Bart Van Assche22032992012-08-14 13:18:53 +00001119
1120 spin_lock_irqsave(&target->lock, flags);
1121 target->req_lim += be32_to_cpu(rsp->req_lim_delta);
1122 spin_unlock_irqrestore(&target->lock, flags);
1123
1124 return;
1125 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001126 scmnd->result = rsp->status;
1127
1128 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
1129 memcpy(scmnd->sense_buffer, rsp->data +
1130 be32_to_cpu(rsp->resp_data_len),
1131 min_t(int, be32_to_cpu(rsp->sense_data_len),
1132 SCSI_SENSE_BUFFERSIZE));
1133 }
1134
1135 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001136 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001137 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001138 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001139
Bart Van Assche22032992012-08-14 13:18:53 +00001140 srp_free_req(target, req, scmnd,
1141 be32_to_cpu(rsp->req_lim_delta));
1142
David Dillowf8b6e312010-11-26 13:02:21 -05001143 scmnd->host_scribble = NULL;
1144 scmnd->scsi_done(scmnd);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001145 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001146}
1147
David Dillowbb125882010-10-08 14:40:47 -04001148static int srp_response_common(struct srp_target_port *target, s32 req_delta,
1149 void *rsp, int len)
1150{
Bart Van Assche76c75b22010-11-26 14:37:47 -05001151 struct ib_device *dev = target->srp_host->srp_dev->dev;
David Dillowbb125882010-10-08 14:40:47 -04001152 unsigned long flags;
1153 struct srp_iu *iu;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001154 int err;
David Dillowbb125882010-10-08 14:40:47 -04001155
Bart Van Asschee9684672010-11-26 15:08:38 -05001156 spin_lock_irqsave(&target->lock, flags);
David Dillowbb125882010-10-08 14:40:47 -04001157 target->req_lim += req_delta;
David Dillowbb125882010-10-08 14:40:47 -04001158 iu = __srp_get_tx_iu(target, SRP_IU_RSP);
Bart Van Asschee9684672010-11-26 15:08:38 -05001159 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001160
David Dillowbb125882010-10-08 14:40:47 -04001161 if (!iu) {
1162 shost_printk(KERN_ERR, target->scsi_host, PFX
1163 "no IU available to send response\n");
Bart Van Assche76c75b22010-11-26 14:37:47 -05001164 return 1;
David Dillowbb125882010-10-08 14:40:47 -04001165 }
1166
1167 ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE);
1168 memcpy(iu->buf, rsp, len);
1169 ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE);
1170
Bart Van Assche76c75b22010-11-26 14:37:47 -05001171 err = srp_post_send(target, iu, len);
1172 if (err) {
David Dillowbb125882010-10-08 14:40:47 -04001173 shost_printk(KERN_ERR, target->scsi_host, PFX
1174 "unable to post response: %d\n", err);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001175 srp_put_tx_iu(target, iu, SRP_IU_RSP);
1176 }
David Dillowbb125882010-10-08 14:40:47 -04001177
David Dillowbb125882010-10-08 14:40:47 -04001178 return err;
1179}
1180
1181static void srp_process_cred_req(struct srp_target_port *target,
1182 struct srp_cred_req *req)
1183{
1184 struct srp_cred_rsp rsp = {
1185 .opcode = SRP_CRED_RSP,
1186 .tag = req->tag,
1187 };
1188 s32 delta = be32_to_cpu(req->req_lim_delta);
1189
1190 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1191 shost_printk(KERN_ERR, target->scsi_host, PFX
1192 "problems processing SRP_CRED_REQ\n");
1193}
1194
1195static void srp_process_aer_req(struct srp_target_port *target,
1196 struct srp_aer_req *req)
1197{
1198 struct srp_aer_rsp rsp = {
1199 .opcode = SRP_AER_RSP,
1200 .tag = req->tag,
1201 };
1202 s32 delta = be32_to_cpu(req->req_lim_delta);
1203
1204 shost_printk(KERN_ERR, target->scsi_host, PFX
1205 "ignoring AER for LUN %llu\n", be64_to_cpu(req->lun));
1206
1207 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1208 shost_printk(KERN_ERR, target->scsi_host, PFX
1209 "problems processing SRP_AER_REQ\n");
1210}
1211
Roland Dreieraef9ec32005-11-02 14:07:13 -08001212static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
1213{
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001214 struct ib_device *dev = target->srp_host->srp_dev->dev;
Roland Dreier737b94e2011-05-23 11:30:04 -07001215 struct srp_iu *iu = (struct srp_iu *) (uintptr_t) wc->wr_id;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001216 int res;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001217 u8 opcode;
1218
Ralph Campbell85507bc2006-12-12 14:30:55 -08001219 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
1220 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001221
1222 opcode = *(u8 *) iu->buf;
1223
1224 if (0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001225 shost_printk(KERN_ERR, target->scsi_host,
1226 PFX "recv completion, opcode 0x%02x\n", opcode);
Bart Van Assche7a700812010-07-29 15:56:37 +00001227 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1,
1228 iu->buf, wc->byte_len, true);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001229 }
1230
1231 switch (opcode) {
1232 case SRP_RSP:
1233 srp_process_rsp(target, iu->buf);
1234 break;
1235
David Dillowbb125882010-10-08 14:40:47 -04001236 case SRP_CRED_REQ:
1237 srp_process_cred_req(target, iu->buf);
1238 break;
1239
1240 case SRP_AER_REQ:
1241 srp_process_aer_req(target, iu->buf);
1242 break;
1243
Roland Dreieraef9ec32005-11-02 14:07:13 -08001244 case SRP_T_LOGOUT:
1245 /* XXX Handle target logout */
David Dillow7aa54bd2008-01-07 18:23:41 -05001246 shost_printk(KERN_WARNING, target->scsi_host,
1247 PFX "Got target logout request\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001248 break;
1249
1250 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001251 shost_printk(KERN_WARNING, target->scsi_host,
1252 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001253 break;
1254 }
1255
Ralph Campbell85507bc2006-12-12 14:30:55 -08001256 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
1257 DMA_FROM_DEVICE);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001258
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001259 res = srp_post_recv(target, iu);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001260 if (res != 0)
1261 shost_printk(KERN_ERR, target->scsi_host,
1262 PFX "Recv failed with error code %d\n", res);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001263}
1264
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001265static void srp_recv_completion(struct ib_cq *cq, void *target_ptr)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001266{
1267 struct srp_target_port *target = target_ptr;
1268 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001269
1270 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1271 while (ib_poll_cq(cq, 1, &wc) > 0) {
1272 if (wc.status) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001273 shost_printk(KERN_ERR, target->scsi_host,
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001274 PFX "failed receive status %d\n",
David Dillow7aa54bd2008-01-07 18:23:41 -05001275 wc.status);
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001276 target->qp_in_error = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001277 break;
1278 }
1279
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001280 srp_handle_recv(target, &wc);
1281 }
1282}
1283
1284static void srp_send_completion(struct ib_cq *cq, void *target_ptr)
1285{
1286 struct srp_target_port *target = target_ptr;
1287 struct ib_wc wc;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001288 struct srp_iu *iu;
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001289
1290 while (ib_poll_cq(cq, 1, &wc) > 0) {
1291 if (wc.status) {
1292 shost_printk(KERN_ERR, target->scsi_host,
1293 PFX "failed send status %d\n",
1294 wc.status);
1295 target->qp_in_error = 1;
1296 break;
1297 }
1298
Roland Dreier737b94e2011-05-23 11:30:04 -07001299 iu = (struct srp_iu *) (uintptr_t) wc.wr_id;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001300 list_add(&iu->list, &target->free_tx);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001301 }
1302}
1303
Bart Van Assche76c75b22010-11-26 14:37:47 -05001304static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001305{
Bart Van Assche76c75b22010-11-26 14:37:47 -05001306 struct srp_target_port *target = host_to_target(shost);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001307 struct srp_request *req;
1308 struct srp_iu *iu;
1309 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001310 struct ib_device *dev;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001311 unsigned long flags;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001312 int len;
1313
1314 if (target->state == SRP_TARGET_CONNECTING)
1315 goto err;
1316
1317 if (target->state == SRP_TARGET_DEAD ||
1318 target->state == SRP_TARGET_REMOVED) {
1319 scmnd->result = DID_BAD_TARGET << 16;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001320 scmnd->scsi_done(scmnd);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001321 return 0;
1322 }
1323
Bart Van Asschee9684672010-11-26 15:08:38 -05001324 spin_lock_irqsave(&target->lock, flags);
David Dillowbb125882010-10-08 14:40:47 -04001325 iu = __srp_get_tx_iu(target, SRP_IU_CMD);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001326 if (!iu)
Bart Van Assche695b8342011-01-13 19:02:25 +00001327 goto err_unlock;
1328
1329 req = list_first_entry(&target->free_reqs, struct srp_request, list);
1330 list_del(&req->list);
1331 spin_unlock_irqrestore(&target->lock, flags);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001332
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001333 dev = target->srp_host->srp_dev->dev;
David Dillow49248642011-01-14 18:23:24 -05001334 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08001335 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001336
Roland Dreieraef9ec32005-11-02 14:07:13 -08001337 scmnd->result = 0;
David Dillowf8b6e312010-11-26 13:02:21 -05001338 scmnd->host_scribble = (void *) req;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001339
1340 cmd = iu->buf;
1341 memset(cmd, 0, sizeof *cmd);
1342
1343 cmd->opcode = SRP_CMD;
1344 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001345 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001346 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1347
Roland Dreieraef9ec32005-11-02 14:07:13 -08001348 req->scmnd = scmnd;
1349 req->cmd = iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001350
1351 len = srp_map_data(scmnd, target, req);
1352 if (len < 0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001353 shost_printk(KERN_ERR, target->scsi_host,
1354 PFX "Failed to map data\n");
Bart Van Assche76c75b22010-11-26 14:37:47 -05001355 goto err_iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001356 }
1357
David Dillow49248642011-01-14 18:23:24 -05001358 ib_dma_sync_single_for_device(dev, iu->dma, target->max_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08001359 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001360
Bart Van Assche76c75b22010-11-26 14:37:47 -05001361 if (srp_post_send(target, iu, len)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001362 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001363 goto err_unmap;
1364 }
1365
Roland Dreieraef9ec32005-11-02 14:07:13 -08001366 return 0;
1367
1368err_unmap:
1369 srp_unmap_data(scmnd, target, req);
1370
Bart Van Assche76c75b22010-11-26 14:37:47 -05001371err_iu:
1372 srp_put_tx_iu(target, iu, SRP_IU_CMD);
1373
Bart Van Asschee9684672010-11-26 15:08:38 -05001374 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001375 list_add(&req->list, &target->free_reqs);
Bart Van Assche695b8342011-01-13 19:02:25 +00001376
1377err_unlock:
Bart Van Asschee9684672010-11-26 15:08:38 -05001378 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001379
Roland Dreieraef9ec32005-11-02 14:07:13 -08001380err:
1381 return SCSI_MLQUEUE_HOST_BUSY;
1382}
1383
1384static int srp_alloc_iu_bufs(struct srp_target_port *target)
1385{
1386 int i;
1387
1388 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1389 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1390 target->max_ti_iu_len,
1391 GFP_KERNEL, DMA_FROM_DEVICE);
1392 if (!target->rx_ring[i])
1393 goto err;
1394 }
1395
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001396 for (i = 0; i < SRP_SQ_SIZE; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001397 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
David Dillow49248642011-01-14 18:23:24 -05001398 target->max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001399 GFP_KERNEL, DMA_TO_DEVICE);
1400 if (!target->tx_ring[i])
1401 goto err;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001402
1403 list_add(&target->tx_ring[i]->list, &target->free_tx);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001404 }
1405
1406 return 0;
1407
1408err:
1409 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1410 srp_free_iu(target->srp_host, target->rx_ring[i]);
1411 target->rx_ring[i] = NULL;
1412 }
1413
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001414 for (i = 0; i < SRP_SQ_SIZE; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001415 srp_free_iu(target->srp_host, target->tx_ring[i]);
1416 target->tx_ring[i] = NULL;
1417 }
1418
1419 return -ENOMEM;
1420}
1421
David Dillow961e0be2011-01-14 17:32:07 -05001422static void srp_cm_rep_handler(struct ib_cm_id *cm_id,
1423 struct srp_login_rsp *lrsp,
1424 struct srp_target_port *target)
1425{
1426 struct ib_qp_attr *qp_attr = NULL;
1427 int attr_mask = 0;
1428 int ret;
1429 int i;
1430
1431 if (lrsp->opcode == SRP_LOGIN_RSP) {
1432 target->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len);
1433 target->req_lim = be32_to_cpu(lrsp->req_lim_delta);
1434
1435 /*
1436 * Reserve credits for task management so we don't
1437 * bounce requests back to the SCSI mid-layer.
1438 */
1439 target->scsi_host->can_queue
1440 = min(target->req_lim - SRP_TSK_MGMT_SQ_SIZE,
1441 target->scsi_host->can_queue);
1442 } else {
1443 shost_printk(KERN_WARNING, target->scsi_host,
1444 PFX "Unhandled RSP opcode %#x\n", lrsp->opcode);
1445 ret = -ECONNRESET;
1446 goto error;
1447 }
1448
1449 if (!target->rx_ring[0]) {
1450 ret = srp_alloc_iu_bufs(target);
1451 if (ret)
1452 goto error;
1453 }
1454
1455 ret = -ENOMEM;
1456 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1457 if (!qp_attr)
1458 goto error;
1459
1460 qp_attr->qp_state = IB_QPS_RTR;
1461 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1462 if (ret)
1463 goto error_free;
1464
1465 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
1466 if (ret)
1467 goto error_free;
1468
1469 for (i = 0; i < SRP_RQ_SIZE; i++) {
1470 struct srp_iu *iu = target->rx_ring[i];
1471 ret = srp_post_recv(target, iu);
1472 if (ret)
1473 goto error_free;
1474 }
1475
1476 qp_attr->qp_state = IB_QPS_RTS;
1477 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1478 if (ret)
1479 goto error_free;
1480
1481 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
1482 if (ret)
1483 goto error_free;
1484
1485 ret = ib_send_cm_rtu(cm_id, NULL, 0);
1486
1487error_free:
1488 kfree(qp_attr);
1489
1490error:
1491 target->status = ret;
1492}
1493
Roland Dreieraef9ec32005-11-02 14:07:13 -08001494static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1495 struct ib_cm_event *event,
1496 struct srp_target_port *target)
1497{
David Dillow7aa54bd2008-01-07 18:23:41 -05001498 struct Scsi_Host *shost = target->scsi_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001499 struct ib_class_port_info *cpi;
1500 int opcode;
1501
1502 switch (event->param.rej_rcvd.reason) {
1503 case IB_CM_REJ_PORT_CM_REDIRECT:
1504 cpi = event->param.rej_rcvd.ari;
1505 target->path.dlid = cpi->redirect_lid;
1506 target->path.pkey = cpi->redirect_pkey;
1507 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1508 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1509
1510 target->status = target->path.dlid ?
1511 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1512 break;
1513
1514 case IB_CM_REJ_PORT_REDIRECT:
Roland Dreier5d7cbfd2007-08-03 10:45:18 -07001515 if (srp_target_is_topspin(target)) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001516 /*
1517 * Topspin/Cisco SRP gateways incorrectly send
1518 * reject reason code 25 when they mean 24
1519 * (port redirect).
1520 */
1521 memcpy(target->path.dgid.raw,
1522 event->param.rej_rcvd.ari, 16);
1523
David Dillow7aa54bd2008-01-07 18:23:41 -05001524 shost_printk(KERN_DEBUG, shost,
1525 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1526 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1527 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001528
1529 target->status = SRP_PORT_REDIRECT;
1530 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001531 shost_printk(KERN_WARNING, shost,
1532 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001533 target->status = -ECONNRESET;
1534 }
1535 break;
1536
1537 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
David Dillow7aa54bd2008-01-07 18:23:41 -05001538 shost_printk(KERN_WARNING, shost,
1539 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001540 target->status = -ECONNRESET;
1541 break;
1542
1543 case IB_CM_REJ_CONSUMER_DEFINED:
1544 opcode = *(u8 *) event->private_data;
1545 if (opcode == SRP_LOGIN_REJ) {
1546 struct srp_login_rej *rej = event->private_data;
1547 u32 reason = be32_to_cpu(rej->reason);
1548
1549 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
David Dillow7aa54bd2008-01-07 18:23:41 -05001550 shost_printk(KERN_WARNING, shost,
1551 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001552 else
David Dillow7aa54bd2008-01-07 18:23:41 -05001553 shost_printk(KERN_WARNING, shost,
1554 PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001555 } else
David Dillow7aa54bd2008-01-07 18:23:41 -05001556 shost_printk(KERN_WARNING, shost,
1557 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1558 " opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001559 target->status = -ECONNRESET;
1560 break;
1561
David Dillow9fe4bcf2008-01-08 17:08:52 -05001562 case IB_CM_REJ_STALE_CONN:
1563 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
1564 target->status = SRP_STALE_CONN;
1565 break;
1566
Roland Dreieraef9ec32005-11-02 14:07:13 -08001567 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001568 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
1569 event->param.rej_rcvd.reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001570 target->status = -ECONNRESET;
1571 }
1572}
1573
1574static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1575{
1576 struct srp_target_port *target = cm_id->context;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001577 int comp = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001578
1579 switch (event->event) {
1580 case IB_CM_REQ_ERROR:
David Dillow7aa54bd2008-01-07 18:23:41 -05001581 shost_printk(KERN_DEBUG, target->scsi_host,
1582 PFX "Sending CM REQ failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001583 comp = 1;
1584 target->status = -ECONNRESET;
1585 break;
1586
1587 case IB_CM_REP_RECEIVED:
1588 comp = 1;
David Dillow961e0be2011-01-14 17:32:07 -05001589 srp_cm_rep_handler(cm_id, event->private_data, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001590 break;
1591
1592 case IB_CM_REJ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001593 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001594 comp = 1;
1595
1596 srp_cm_rej_handler(cm_id, event, target);
1597 break;
1598
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001599 case IB_CM_DREQ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001600 shost_printk(KERN_WARNING, target->scsi_host,
1601 PFX "DREQ received - connection closed\n");
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001602 if (ib_send_cm_drep(cm_id, NULL, 0))
David Dillow7aa54bd2008-01-07 18:23:41 -05001603 shost_printk(KERN_ERR, target->scsi_host,
1604 PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001605 break;
1606
1607 case IB_CM_TIMEWAIT_EXIT:
David Dillow7aa54bd2008-01-07 18:23:41 -05001608 shost_printk(KERN_ERR, target->scsi_host,
1609 PFX "connection closed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001610
1611 comp = 1;
1612 target->status = 0;
1613 break;
1614
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001615 case IB_CM_MRA_RECEIVED:
1616 case IB_CM_DREQ_ERROR:
1617 case IB_CM_DREP_RECEIVED:
1618 break;
1619
Roland Dreieraef9ec32005-11-02 14:07:13 -08001620 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001621 shost_printk(KERN_WARNING, target->scsi_host,
1622 PFX "Unhandled CM event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001623 break;
1624 }
1625
1626 if (comp)
1627 complete(&target->done);
1628
Roland Dreieraef9ec32005-11-02 14:07:13 -08001629 return 0;
1630}
1631
Roland Dreierd945e1d2006-05-09 10:50:28 -07001632static int srp_send_tsk_mgmt(struct srp_target_port *target,
David Dillowf8b6e312010-11-26 13:02:21 -05001633 u64 req_tag, unsigned int lun, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001634{
David Dillow19081f32010-10-18 08:54:49 -04001635 struct ib_device *dev = target->srp_host->srp_dev->dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001636 struct srp_iu *iu;
1637 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001638
Roland Dreier1285b3a2006-03-03 15:47:25 -08001639 if (target->state == SRP_TARGET_DEAD ||
David Dillowf8b6e312010-11-26 13:02:21 -05001640 target->state == SRP_TARGET_REMOVED)
Bart Van Assche76c75b22010-11-26 14:37:47 -05001641 return -1;
Roland Dreier1285b3a2006-03-03 15:47:25 -08001642
David Dillowf8b6e312010-11-26 13:02:21 -05001643 init_completion(&target->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001644
Bart Van Asschee9684672010-11-26 15:08:38 -05001645 spin_lock_irq(&target->lock);
David Dillowbb125882010-10-08 14:40:47 -04001646 iu = __srp_get_tx_iu(target, SRP_IU_TSK_MGMT);
Bart Van Asschee9684672010-11-26 15:08:38 -05001647 spin_unlock_irq(&target->lock);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001648
Roland Dreieraef9ec32005-11-02 14:07:13 -08001649 if (!iu)
Bart Van Assche76c75b22010-11-26 14:37:47 -05001650 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001651
David Dillow19081f32010-10-18 08:54:49 -04001652 ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt,
1653 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001654 tsk_mgmt = iu->buf;
1655 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1656
1657 tsk_mgmt->opcode = SRP_TSK_MGMT;
David Dillowf8b6e312010-11-26 13:02:21 -05001658 tsk_mgmt->lun = cpu_to_be64((u64) lun << 48);
1659 tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001660 tsk_mgmt->tsk_mgmt_func = func;
David Dillowf8b6e312010-11-26 13:02:21 -05001661 tsk_mgmt->task_tag = req_tag;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001662
David Dillow19081f32010-10-18 08:54:49 -04001663 ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt,
1664 DMA_TO_DEVICE);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001665 if (srp_post_send(target, iu, sizeof *tsk_mgmt)) {
1666 srp_put_tx_iu(target, iu, SRP_IU_TSK_MGMT);
1667 return -1;
1668 }
Roland Dreierd945e1d2006-05-09 10:50:28 -07001669
David Dillowf8b6e312010-11-26 13:02:21 -05001670 if (!wait_for_completion_timeout(&target->tsk_mgmt_done,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001671 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001672 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001673
Roland Dreierd945e1d2006-05-09 10:50:28 -07001674 return 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001675}
1676
Roland Dreieraef9ec32005-11-02 14:07:13 -08001677static int srp_abort(struct scsi_cmnd *scmnd)
1678{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001679 struct srp_target_port *target = host_to_target(scmnd->device->host);
David Dillowf8b6e312010-11-26 13:02:21 -05001680 struct srp_request *req = (struct srp_request *) scmnd->host_scribble;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001681
David Dillow7aa54bd2008-01-07 18:23:41 -05001682 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001683
Bart Van Assche22032992012-08-14 13:18:53 +00001684 if (!req || target->qp_in_error || !srp_claim_req(target, req, scmnd))
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001685 return FAILED;
Bart Van Assche22032992012-08-14 13:18:53 +00001686 srp_send_tsk_mgmt(target, req->index, scmnd->device->lun,
1687 SRP_TSK_ABORT_TASK);
1688 srp_free_req(target, req, scmnd, 0);
1689 scmnd->result = DID_ABORT << 16;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001690
Bart Van Assche22032992012-08-14 13:18:53 +00001691 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001692}
1693
1694static int srp_reset_device(struct scsi_cmnd *scmnd)
1695{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001696 struct srp_target_port *target = host_to_target(scmnd->device->host);
Bart Van Assche536ae142010-11-26 13:58:27 -05001697 int i;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001698
David Dillow7aa54bd2008-01-07 18:23:41 -05001699 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001700
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001701 if (target->qp_in_error)
1702 return FAILED;
David Dillowf8b6e312010-11-26 13:02:21 -05001703 if (srp_send_tsk_mgmt(target, SRP_TAG_NO_REQ, scmnd->device->lun,
1704 SRP_TSK_LUN_RESET))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001705 return FAILED;
David Dillowf8b6e312010-11-26 13:02:21 -05001706 if (target->tsk_mgmt_status)
Roland Dreierd945e1d2006-05-09 10:50:28 -07001707 return FAILED;
1708
Bart Van Assche536ae142010-11-26 13:58:27 -05001709 for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
1710 struct srp_request *req = &target->req_ring[i];
David Dillowf8b6e312010-11-26 13:02:21 -05001711 if (req->scmnd && req->scmnd->device == scmnd->device)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001712 srp_reset_req(target, req);
Bart Van Assche536ae142010-11-26 13:58:27 -05001713 }
Roland Dreierd945e1d2006-05-09 10:50:28 -07001714
Roland Dreierd945e1d2006-05-09 10:50:28 -07001715 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001716}
1717
1718static int srp_reset_host(struct scsi_cmnd *scmnd)
1719{
1720 struct srp_target_port *target = host_to_target(scmnd->device->host);
1721 int ret = FAILED;
1722
David Dillow7aa54bd2008-01-07 18:23:41 -05001723 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001724
1725 if (!srp_reconnect_target(target))
1726 ret = SUCCESS;
1727
1728 return ret;
1729}
1730
Tony Jonesee959b02008-02-22 00:13:36 +01001731static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
1732 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001733{
Tony Jonesee959b02008-02-22 00:13:36 +01001734 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001735
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001736 return sprintf(buf, "0x%016llx\n",
1737 (unsigned long long) be64_to_cpu(target->id_ext));
1738}
1739
Tony Jonesee959b02008-02-22 00:13:36 +01001740static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
1741 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001742{
Tony Jonesee959b02008-02-22 00:13:36 +01001743 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001744
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001745 return sprintf(buf, "0x%016llx\n",
1746 (unsigned long long) be64_to_cpu(target->ioc_guid));
1747}
1748
Tony Jonesee959b02008-02-22 00:13:36 +01001749static ssize_t show_service_id(struct device *dev,
1750 struct device_attribute *attr, char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001751{
Tony Jonesee959b02008-02-22 00:13:36 +01001752 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001753
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001754 return sprintf(buf, "0x%016llx\n",
1755 (unsigned long long) be64_to_cpu(target->service_id));
1756}
1757
Tony Jonesee959b02008-02-22 00:13:36 +01001758static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
1759 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001760{
Tony Jonesee959b02008-02-22 00:13:36 +01001761 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001762
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001763 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1764}
1765
Tony Jonesee959b02008-02-22 00:13:36 +01001766static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
1767 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001768{
Tony Jonesee959b02008-02-22 00:13:36 +01001769 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001770
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001771 return sprintf(buf, "%pI6\n", target->path.dgid.raw);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001772}
1773
Tony Jonesee959b02008-02-22 00:13:36 +01001774static ssize_t show_orig_dgid(struct device *dev,
1775 struct device_attribute *attr, char *buf)
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001776{
Tony Jonesee959b02008-02-22 00:13:36 +01001777 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001778
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001779 return sprintf(buf, "%pI6\n", target->orig_dgid);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001780}
1781
Bart Van Assche89de7482010-08-03 14:08:45 +00001782static ssize_t show_req_lim(struct device *dev,
1783 struct device_attribute *attr, char *buf)
1784{
1785 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1786
Bart Van Assche89de7482010-08-03 14:08:45 +00001787 return sprintf(buf, "%d\n", target->req_lim);
1788}
1789
Tony Jonesee959b02008-02-22 00:13:36 +01001790static ssize_t show_zero_req_lim(struct device *dev,
1791 struct device_attribute *attr, char *buf)
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001792{
Tony Jonesee959b02008-02-22 00:13:36 +01001793 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001794
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001795 return sprintf(buf, "%d\n", target->zero_req_lim);
1796}
1797
Tony Jonesee959b02008-02-22 00:13:36 +01001798static ssize_t show_local_ib_port(struct device *dev,
1799 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001800{
Tony Jonesee959b02008-02-22 00:13:36 +01001801 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001802
1803 return sprintf(buf, "%d\n", target->srp_host->port);
1804}
1805
Tony Jonesee959b02008-02-22 00:13:36 +01001806static ssize_t show_local_ib_device(struct device *dev,
1807 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001808{
Tony Jonesee959b02008-02-22 00:13:36 +01001809 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001810
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001811 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001812}
1813
David Dillow49248642011-01-14 18:23:24 -05001814static ssize_t show_cmd_sg_entries(struct device *dev,
1815 struct device_attribute *attr, char *buf)
1816{
1817 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1818
1819 return sprintf(buf, "%u\n", target->cmd_sg_cnt);
1820}
1821
David Dillowc07d4242011-01-16 13:57:10 -05001822static ssize_t show_allow_ext_sg(struct device *dev,
1823 struct device_attribute *attr, char *buf)
1824{
1825 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1826
1827 return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false");
1828}
1829
Tony Jonesee959b02008-02-22 00:13:36 +01001830static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1831static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1832static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1833static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1834static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1835static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
Bart Van Assche89de7482010-08-03 14:08:45 +00001836static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01001837static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1838static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1839static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
David Dillow49248642011-01-14 18:23:24 -05001840static DEVICE_ATTR(cmd_sg_entries, S_IRUGO, show_cmd_sg_entries, NULL);
David Dillowc07d4242011-01-16 13:57:10 -05001841static DEVICE_ATTR(allow_ext_sg, S_IRUGO, show_allow_ext_sg, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001842
Tony Jonesee959b02008-02-22 00:13:36 +01001843static struct device_attribute *srp_host_attrs[] = {
1844 &dev_attr_id_ext,
1845 &dev_attr_ioc_guid,
1846 &dev_attr_service_id,
1847 &dev_attr_pkey,
1848 &dev_attr_dgid,
1849 &dev_attr_orig_dgid,
Bart Van Assche89de7482010-08-03 14:08:45 +00001850 &dev_attr_req_lim,
Tony Jonesee959b02008-02-22 00:13:36 +01001851 &dev_attr_zero_req_lim,
1852 &dev_attr_local_ib_port,
1853 &dev_attr_local_ib_device,
David Dillow49248642011-01-14 18:23:24 -05001854 &dev_attr_cmd_sg_entries,
David Dillowc07d4242011-01-16 13:57:10 -05001855 &dev_attr_allow_ext_sg,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001856 NULL
1857};
1858
Roland Dreieraef9ec32005-11-02 14:07:13 -08001859static struct scsi_host_template srp_template = {
1860 .module = THIS_MODULE,
Roland Dreierb7f008f2007-05-06 21:18:11 -07001861 .name = "InfiniBand SRP initiator",
1862 .proc_name = DRV_NAME,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001863 .info = srp_target_info,
1864 .queuecommand = srp_queuecommand,
1865 .eh_abort_handler = srp_abort,
1866 .eh_device_reset_handler = srp_reset_device,
1867 .eh_host_reset_handler = srp_reset_host,
David Dillow49248642011-01-14 18:23:24 -05001868 .sg_tablesize = SRP_DEF_SG_TABLESIZE,
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001869 .can_queue = SRP_CMD_SQ_SIZE,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001870 .this_id = -1,
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001871 .cmd_per_lun = SRP_CMD_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001872 .use_clustering = ENABLE_CLUSTERING,
1873 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001874};
1875
1876static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1877{
FUJITA Tomonori32368222007-06-27 16:33:12 +09001878 struct srp_rport_identifiers ids;
1879 struct srp_rport *rport;
1880
Roland Dreieraef9ec32005-11-02 14:07:13 -08001881 sprintf(target->target_name, "SRP.T10:%016llX",
1882 (unsigned long long) be64_to_cpu(target->id_ext));
1883
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001884 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001885 return -ENODEV;
1886
FUJITA Tomonori32368222007-06-27 16:33:12 +09001887 memcpy(ids.port_id, &target->id_ext, 8);
1888 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09001889 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori32368222007-06-27 16:33:12 +09001890 rport = srp_rport_add(target->scsi_host, &ids);
1891 if (IS_ERR(rport)) {
1892 scsi_remove_host(target->scsi_host);
1893 return PTR_ERR(rport);
1894 }
1895
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001896 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001897 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001898 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001899
1900 target->state = SRP_TARGET_LIVE;
1901
Roland Dreieraef9ec32005-11-02 14:07:13 -08001902 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07001903 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001904
1905 return 0;
1906}
1907
Tony Jonesee959b02008-02-22 00:13:36 +01001908static void srp_release_dev(struct device *dev)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001909{
1910 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01001911 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001912
1913 complete(&host->released);
1914}
1915
1916static struct class srp_class = {
1917 .name = "infiniband_srp",
Tony Jonesee959b02008-02-22 00:13:36 +01001918 .dev_release = srp_release_dev
Roland Dreieraef9ec32005-11-02 14:07:13 -08001919};
1920
1921/*
1922 * Target ports are added by writing
1923 *
1924 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1925 * pkey=<P_Key>,service_id=<service ID>
1926 *
1927 * to the add_target sysfs attribute.
1928 */
1929enum {
1930 SRP_OPT_ERR = 0,
1931 SRP_OPT_ID_EXT = 1 << 0,
1932 SRP_OPT_IOC_GUID = 1 << 1,
1933 SRP_OPT_DGID = 1 << 2,
1934 SRP_OPT_PKEY = 1 << 3,
1935 SRP_OPT_SERVICE_ID = 1 << 4,
1936 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07001937 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07001938 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001939 SRP_OPT_INITIATOR_EXT = 1 << 8,
David Dillow49248642011-01-14 18:23:24 -05001940 SRP_OPT_CMD_SG_ENTRIES = 1 << 9,
David Dillowc07d4242011-01-16 13:57:10 -05001941 SRP_OPT_ALLOW_EXT_SG = 1 << 10,
1942 SRP_OPT_SG_TABLESIZE = 1 << 11,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001943 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1944 SRP_OPT_IOC_GUID |
1945 SRP_OPT_DGID |
1946 SRP_OPT_PKEY |
1947 SRP_OPT_SERVICE_ID),
1948};
1949
Steven Whitehousea447c092008-10-13 10:46:57 +01001950static const match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07001951 { SRP_OPT_ID_EXT, "id_ext=%s" },
1952 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1953 { SRP_OPT_DGID, "dgid=%s" },
1954 { SRP_OPT_PKEY, "pkey=%x" },
1955 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1956 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1957 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07001958 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001959 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
David Dillow49248642011-01-14 18:23:24 -05001960 { SRP_OPT_CMD_SG_ENTRIES, "cmd_sg_entries=%u" },
David Dillowc07d4242011-01-16 13:57:10 -05001961 { SRP_OPT_ALLOW_EXT_SG, "allow_ext_sg=%u" },
1962 { SRP_OPT_SG_TABLESIZE, "sg_tablesize=%u" },
Vu Pham52fb2b502006-06-17 20:37:31 -07001963 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001964};
1965
1966static int srp_parse_options(const char *buf, struct srp_target_port *target)
1967{
1968 char *options, *sep_opt;
1969 char *p;
1970 char dgid[3];
1971 substring_t args[MAX_OPT_ARGS];
1972 int opt_mask = 0;
1973 int token;
1974 int ret = -EINVAL;
1975 int i;
1976
1977 options = kstrdup(buf, GFP_KERNEL);
1978 if (!options)
1979 return -ENOMEM;
1980
1981 sep_opt = options;
1982 while ((p = strsep(&sep_opt, ",")) != NULL) {
1983 if (!*p)
1984 continue;
1985
1986 token = match_token(p, srp_opt_tokens, args);
1987 opt_mask |= token;
1988
1989 switch (token) {
1990 case SRP_OPT_ID_EXT:
1991 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001992 if (!p) {
1993 ret = -ENOMEM;
1994 goto out;
1995 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001996 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1997 kfree(p);
1998 break;
1999
2000 case SRP_OPT_IOC_GUID:
2001 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002002 if (!p) {
2003 ret = -ENOMEM;
2004 goto out;
2005 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002006 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
2007 kfree(p);
2008 break;
2009
2010 case SRP_OPT_DGID:
2011 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002012 if (!p) {
2013 ret = -ENOMEM;
2014 goto out;
2015 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002016 if (strlen(p) != 32) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002017 pr_warn("bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07002018 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002019 goto out;
2020 }
2021
2022 for (i = 0; i < 16; ++i) {
2023 strlcpy(dgid, p + i * 2, 3);
2024 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
2025 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08002026 kfree(p);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002027 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002028 break;
2029
2030 case SRP_OPT_PKEY:
2031 if (match_hex(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002032 pr_warn("bad P_Key parameter '%s'\n", p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002033 goto out;
2034 }
2035 target->path.pkey = cpu_to_be16(token);
2036 break;
2037
2038 case SRP_OPT_SERVICE_ID:
2039 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002040 if (!p) {
2041 ret = -ENOMEM;
2042 goto out;
2043 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002044 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
Sean Hefty247e0202007-08-08 15:51:18 -07002045 target->path.service_id = target->service_id;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002046 kfree(p);
2047 break;
2048
2049 case SRP_OPT_MAX_SECT:
2050 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002051 pr_warn("bad max sect parameter '%s'\n", p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002052 goto out;
2053 }
2054 target->scsi_host->max_sectors = token;
2055 break;
2056
Vu Pham52fb2b502006-06-17 20:37:31 -07002057 case SRP_OPT_MAX_CMD_PER_LUN:
2058 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002059 pr_warn("bad max cmd_per_lun parameter '%s'\n",
2060 p);
Vu Pham52fb2b502006-06-17 20:37:31 -07002061 goto out;
2062 }
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00002063 target->scsi_host->cmd_per_lun = min(token, SRP_CMD_SQ_SIZE);
Vu Pham52fb2b502006-06-17 20:37:31 -07002064 break;
2065
Ramachandra K0c0450db2006-06-17 20:37:38 -07002066 case SRP_OPT_IO_CLASS:
2067 if (match_hex(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002068 pr_warn("bad IO class parameter '%s'\n", p);
Ramachandra K0c0450db2006-06-17 20:37:38 -07002069 goto out;
2070 }
2071 if (token != SRP_REV10_IB_IO_CLASS &&
2072 token != SRP_REV16A_IB_IO_CLASS) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002073 pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n",
2074 token, SRP_REV10_IB_IO_CLASS,
2075 SRP_REV16A_IB_IO_CLASS);
Ramachandra K0c0450db2006-06-17 20:37:38 -07002076 goto out;
2077 }
2078 target->io_class = token;
2079 break;
2080
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002081 case SRP_OPT_INITIATOR_EXT:
2082 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002083 if (!p) {
2084 ret = -ENOMEM;
2085 goto out;
2086 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002087 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2088 kfree(p);
2089 break;
2090
David Dillow49248642011-01-14 18:23:24 -05002091 case SRP_OPT_CMD_SG_ENTRIES:
2092 if (match_int(args, &token) || token < 1 || token > 255) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002093 pr_warn("bad max cmd_sg_entries parameter '%s'\n",
2094 p);
David Dillow49248642011-01-14 18:23:24 -05002095 goto out;
2096 }
2097 target->cmd_sg_cnt = token;
2098 break;
2099
David Dillowc07d4242011-01-16 13:57:10 -05002100 case SRP_OPT_ALLOW_EXT_SG:
2101 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002102 pr_warn("bad allow_ext_sg parameter '%s'\n", p);
David Dillowc07d4242011-01-16 13:57:10 -05002103 goto out;
2104 }
2105 target->allow_ext_sg = !!token;
2106 break;
2107
2108 case SRP_OPT_SG_TABLESIZE:
2109 if (match_int(args, &token) || token < 1 ||
2110 token > SCSI_MAX_SG_CHAIN_SEGMENTS) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002111 pr_warn("bad max sg_tablesize parameter '%s'\n",
2112 p);
David Dillowc07d4242011-01-16 13:57:10 -05002113 goto out;
2114 }
2115 target->sg_tablesize = token;
2116 break;
2117
Roland Dreieraef9ec32005-11-02 14:07:13 -08002118 default:
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002119 pr_warn("unknown parameter or missing value '%s' in target creation request\n",
2120 p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002121 goto out;
2122 }
2123 }
2124
2125 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
2126 ret = 0;
2127 else
2128 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
2129 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
2130 !(srp_opt_tokens[i].token & opt_mask))
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002131 pr_warn("target creation request is missing parameter '%s'\n",
2132 srp_opt_tokens[i].pattern);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002133
2134out:
2135 kfree(options);
2136 return ret;
2137}
2138
Tony Jonesee959b02008-02-22 00:13:36 +01002139static ssize_t srp_create_target(struct device *dev,
2140 struct device_attribute *attr,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002141 const char *buf, size_t count)
2142{
2143 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01002144 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002145 struct Scsi_Host *target_host;
2146 struct srp_target_port *target;
David Dillowc07d4242011-01-16 13:57:10 -05002147 struct ib_device *ibdev = host->srp_dev->dev;
2148 dma_addr_t dma_addr;
David Dillow8f26c9f2011-01-14 19:45:50 -05002149 int i, ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002150
2151 target_host = scsi_host_alloc(&srp_template,
2152 sizeof (struct srp_target_port));
2153 if (!target_host)
2154 return -ENOMEM;
2155
David Dillow49248642011-01-14 18:23:24 -05002156 target_host->transportt = ib_srp_transport_template;
Bart Van Asschefd1b6c42011-07-13 09:19:16 -07002157 target_host->max_channel = 0;
2158 target_host->max_id = 1;
Arne Redlich3c8edf02006-11-15 12:43:00 +01002159 target_host->max_lun = SRP_MAX_LUN;
2160 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08002161
Roland Dreieraef9ec32005-11-02 14:07:13 -08002162 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002163
David Dillow49248642011-01-14 18:23:24 -05002164 target->io_class = SRP_REV16A_IB_IO_CLASS;
2165 target->scsi_host = target_host;
2166 target->srp_host = host;
2167 target->lkey = host->srp_dev->mr->lkey;
2168 target->rkey = host->srp_dev->mr->rkey;
2169 target->cmd_sg_cnt = cmd_sg_entries;
David Dillowc07d4242011-01-16 13:57:10 -05002170 target->sg_tablesize = indirect_sg_entries ? : cmd_sg_entries;
2171 target->allow_ext_sg = allow_ext_sg;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002172
Roland Dreieraef9ec32005-11-02 14:07:13 -08002173 ret = srp_parse_options(buf, target);
2174 if (ret)
2175 goto err;
2176
David Dillowc07d4242011-01-16 13:57:10 -05002177 if (!host->srp_dev->fmr_pool && !target->allow_ext_sg &&
2178 target->cmd_sg_cnt < target->sg_tablesize) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002179 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 -05002180 target->sg_tablesize = target->cmd_sg_cnt;
2181 }
2182
2183 target_host->sg_tablesize = target->sg_tablesize;
2184 target->indirect_size = target->sg_tablesize *
2185 sizeof (struct srp_direct_buf);
David Dillow49248642011-01-14 18:23:24 -05002186 target->max_iu_len = sizeof (struct srp_cmd) +
2187 sizeof (struct srp_indirect_buf) +
2188 target->cmd_sg_cnt * sizeof (struct srp_direct_buf);
2189
David Dillow8f26c9f2011-01-14 19:45:50 -05002190 spin_lock_init(&target->lock);
2191 INIT_LIST_HEAD(&target->free_tx);
2192 INIT_LIST_HEAD(&target->free_reqs);
2193 for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
2194 struct srp_request *req = &target->req_ring[i];
2195
2196 req->fmr_list = kmalloc(target->cmd_sg_cnt * sizeof (void *),
2197 GFP_KERNEL);
2198 req->map_page = kmalloc(SRP_FMR_SIZE * sizeof (void *),
2199 GFP_KERNEL);
David Dillowc07d4242011-01-16 13:57:10 -05002200 req->indirect_desc = kmalloc(target->indirect_size, GFP_KERNEL);
2201 if (!req->fmr_list || !req->map_page || !req->indirect_desc)
David Dillow8f26c9f2011-01-14 19:45:50 -05002202 goto err_free_mem;
2203
David Dillowc07d4242011-01-16 13:57:10 -05002204 dma_addr = ib_dma_map_single(ibdev, req->indirect_desc,
2205 target->indirect_size,
2206 DMA_TO_DEVICE);
2207 if (ib_dma_mapping_error(ibdev, dma_addr))
2208 goto err_free_mem;
2209
2210 req->indirect_dma_addr = dma_addr;
David Dillow8f26c9f2011-01-14 19:45:50 -05002211 req->index = i;
2212 list_add_tail(&req->list, &target->free_reqs);
2213 }
2214
David Dillowc07d4242011-01-16 13:57:10 -05002215 ib_query_gid(ibdev, host->port, 0, &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002216
David Dillow7aa54bd2008-01-07 18:23:41 -05002217 shost_printk(KERN_DEBUG, target->scsi_host, PFX
2218 "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002219 "service_id %016llx dgid %pI6\n",
Roland Dreieraef9ec32005-11-02 14:07:13 -08002220 (unsigned long long) be64_to_cpu(target->id_ext),
2221 (unsigned long long) be64_to_cpu(target->ioc_guid),
2222 be16_to_cpu(target->path.pkey),
2223 (unsigned long long) be64_to_cpu(target->service_id),
Harvey Harrison8867cd72008-10-28 22:36:33 -07002224 target->path.dgid.raw);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002225
2226 ret = srp_create_target_ib(target);
2227 if (ret)
David Dillow8f26c9f2011-01-14 19:45:50 -05002228 goto err_free_mem;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002229
David Dillow9fe4bcf2008-01-08 17:08:52 -05002230 ret = srp_new_cm_id(target);
2231 if (ret)
David Dillow8f26c9f2011-01-14 19:45:50 -05002232 goto err_free_ib;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002233
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02002234 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002235 ret = srp_connect_target(target);
2236 if (ret) {
David Dillow7aa54bd2008-01-07 18:23:41 -05002237 shost_printk(KERN_ERR, target->scsi_host,
2238 PFX "Connection failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002239 goto err_cm_id;
2240 }
2241
2242 ret = srp_add_target(host, target);
2243 if (ret)
2244 goto err_disconnect;
2245
2246 return count;
2247
2248err_disconnect:
2249 srp_disconnect_target(target);
2250
2251err_cm_id:
2252 ib_destroy_cm_id(target->cm_id);
2253
David Dillow8f26c9f2011-01-14 19:45:50 -05002254err_free_ib:
Roland Dreieraef9ec32005-11-02 14:07:13 -08002255 srp_free_target_ib(target);
2256
David Dillow8f26c9f2011-01-14 19:45:50 -05002257err_free_mem:
2258 srp_free_req_data(target);
2259
Roland Dreieraef9ec32005-11-02 14:07:13 -08002260err:
2261 scsi_host_put(target_host);
2262
2263 return ret;
2264}
2265
Tony Jonesee959b02008-02-22 00:13:36 +01002266static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002267
Tony Jonesee959b02008-02-22 00:13:36 +01002268static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
2269 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002270{
Tony Jonesee959b02008-02-22 00:13:36 +01002271 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002272
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002273 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002274}
2275
Tony Jonesee959b02008-02-22 00:13:36 +01002276static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002277
Tony Jonesee959b02008-02-22 00:13:36 +01002278static ssize_t show_port(struct device *dev, struct device_attribute *attr,
2279 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002280{
Tony Jonesee959b02008-02-22 00:13:36 +01002281 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002282
2283 return sprintf(buf, "%d\n", host->port);
2284}
2285
Tony Jonesee959b02008-02-22 00:13:36 +01002286static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002287
Roland Dreierf5358a12006-06-17 20:37:29 -07002288static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002289{
2290 struct srp_host *host;
2291
2292 host = kzalloc(sizeof *host, GFP_KERNEL);
2293 if (!host)
2294 return NULL;
2295
2296 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002297 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002298 init_completion(&host->released);
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002299 host->srp_dev = device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002300 host->port = port;
2301
Tony Jonesee959b02008-02-22 00:13:36 +01002302 host->dev.class = &srp_class;
2303 host->dev.parent = device->dev->dma_device;
Kay Sieversd927e382009-01-06 10:44:39 -08002304 dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002305
Tony Jonesee959b02008-02-22 00:13:36 +01002306 if (device_register(&host->dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07002307 goto free_host;
Tony Jonesee959b02008-02-22 00:13:36 +01002308 if (device_create_file(&host->dev, &dev_attr_add_target))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002309 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01002310 if (device_create_file(&host->dev, &dev_attr_ibdev))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002311 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01002312 if (device_create_file(&host->dev, &dev_attr_port))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002313 goto err_class;
2314
2315 return host;
2316
2317err_class:
Tony Jonesee959b02008-02-22 00:13:36 +01002318 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002319
Roland Dreierf5358a12006-06-17 20:37:29 -07002320free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08002321 kfree(host);
2322
2323 return NULL;
2324}
2325
2326static void srp_add_one(struct ib_device *device)
2327{
Roland Dreierf5358a12006-06-17 20:37:29 -07002328 struct srp_device *srp_dev;
2329 struct ib_device_attr *dev_attr;
2330 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002331 struct srp_host *host;
David Dillowbe8b9812011-01-18 21:58:09 -05002332 int max_pages_per_fmr, fmr_page_shift, s, e, p;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002333
Roland Dreierf5358a12006-06-17 20:37:29 -07002334 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
2335 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08002336 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002337
Roland Dreierf5358a12006-06-17 20:37:29 -07002338 if (ib_query_device(device, dev_attr)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002339 pr_warn("Query device failed for %s\n", device->name);
Roland Dreierf5358a12006-06-17 20:37:29 -07002340 goto free_attr;
2341 }
2342
2343 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
2344 if (!srp_dev)
2345 goto free_attr;
2346
2347 /*
2348 * Use the smallest page size supported by the HCA, down to a
David Dillow8f26c9f2011-01-14 19:45:50 -05002349 * minimum of 4096 bytes. We're unlikely to build large sglists
2350 * out of smaller entries.
Roland Dreierf5358a12006-06-17 20:37:29 -07002351 */
David Dillow8f26c9f2011-01-14 19:45:50 -05002352 fmr_page_shift = max(12, ffs(dev_attr->page_size_cap) - 1);
2353 srp_dev->fmr_page_size = 1 << fmr_page_shift;
2354 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
2355 srp_dev->fmr_max_size = srp_dev->fmr_page_size * SRP_FMR_SIZE;
Roland Dreierf5358a12006-06-17 20:37:29 -07002356
2357 INIT_LIST_HEAD(&srp_dev->dev_list);
2358
2359 srp_dev->dev = device;
2360 srp_dev->pd = ib_alloc_pd(device);
2361 if (IS_ERR(srp_dev->pd))
2362 goto free_dev;
2363
2364 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
2365 IB_ACCESS_LOCAL_WRITE |
2366 IB_ACCESS_REMOTE_READ |
2367 IB_ACCESS_REMOTE_WRITE);
2368 if (IS_ERR(srp_dev->mr))
2369 goto err_pd;
2370
David Dillowbe8b9812011-01-18 21:58:09 -05002371 for (max_pages_per_fmr = SRP_FMR_SIZE;
2372 max_pages_per_fmr >= SRP_FMR_MIN_SIZE;
2373 max_pages_per_fmr /= 2, srp_dev->fmr_max_size /= 2) {
2374 memset(&fmr_param, 0, sizeof fmr_param);
2375 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
2376 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
2377 fmr_param.cache = 1;
2378 fmr_param.max_pages_per_fmr = max_pages_per_fmr;
2379 fmr_param.page_shift = fmr_page_shift;
2380 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
2381 IB_ACCESS_REMOTE_WRITE |
2382 IB_ACCESS_REMOTE_READ);
Roland Dreierf5358a12006-06-17 20:37:29 -07002383
David Dillowbe8b9812011-01-18 21:58:09 -05002384 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
2385 if (!IS_ERR(srp_dev->fmr_pool))
2386 break;
2387 }
2388
Roland Dreierf5358a12006-06-17 20:37:29 -07002389 if (IS_ERR(srp_dev->fmr_pool))
2390 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002391
Tom Tucker07ebafb2006-08-03 16:02:42 -05002392 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002393 s = 0;
2394 e = 0;
2395 } else {
2396 s = 1;
2397 e = device->phys_port_cnt;
2398 }
2399
2400 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07002401 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002402 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07002403 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002404 }
2405
Roland Dreierf5358a12006-06-17 20:37:29 -07002406 ib_set_client_data(device, &srp_client, srp_dev);
2407
2408 goto free_attr;
2409
2410err_pd:
2411 ib_dealloc_pd(srp_dev->pd);
2412
2413free_dev:
2414 kfree(srp_dev);
2415
2416free_attr:
2417 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002418}
2419
2420static void srp_remove_one(struct ib_device *device)
2421{
Roland Dreierf5358a12006-06-17 20:37:29 -07002422 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002423 struct srp_host *host, *tmp_host;
2424 LIST_HEAD(target_list);
2425 struct srp_target_port *target, *tmp_target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002426
Roland Dreierf5358a12006-06-17 20:37:29 -07002427 srp_dev = ib_get_client_data(device, &srp_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002428
Roland Dreierf5358a12006-06-17 20:37:29 -07002429 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Tony Jonesee959b02008-02-22 00:13:36 +01002430 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002431 /*
2432 * Wait for the sysfs entry to go away, so that no new
2433 * target ports can be created.
2434 */
2435 wait_for_completion(&host->released);
2436
2437 /*
2438 * Mark all target ports as removed, so we stop queueing
2439 * commands and don't try to reconnect.
2440 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002441 spin_lock(&host->target_lock);
Matthew Wilcox549c5fc22006-06-17 20:37:30 -07002442 list_for_each_entry(target, &host->target_list, list) {
Bart Van Asschee9684672010-11-26 15:08:38 -05002443 spin_lock_irq(&target->lock);
Ishai Rabinovitz0c5b3952006-06-17 20:37:31 -07002444 target->state = SRP_TARGET_REMOVED;
Bart Van Asschee9684672010-11-26 15:08:38 -05002445 spin_unlock_irq(&target->lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002446 }
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002447 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002448
2449 /*
2450 * Wait for any reconnection tasks that may have
2451 * started before we marked our target ports as
2452 * removed, and any target port removal tasks.
2453 */
Tejun Heof0626712010-10-19 15:24:36 +00002454 flush_workqueue(ib_wq);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002455
2456 list_for_each_entry_safe(target, tmp_target,
2457 &host->target_list, list) {
Bart Van Assche683b1592012-01-14 12:40:44 +00002458 srp_del_scsi_host_attr(target->scsi_host);
David Dillowb0e47c82008-01-03 10:25:27 -08002459 srp_remove_host(target->scsi_host);
Dave Dillowad696982008-01-03 22:35:41 -05002460 scsi_remove_host(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002461 srp_disconnect_target(target);
2462 ib_destroy_cm_id(target->cm_id);
2463 srp_free_target_ib(target);
David Dillow8f26c9f2011-01-14 19:45:50 -05002464 srp_free_req_data(target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002465 scsi_host_put(target->scsi_host);
2466 }
2467
Roland Dreieraef9ec32005-11-02 14:07:13 -08002468 kfree(host);
2469 }
2470
Roland Dreierf5358a12006-06-17 20:37:29 -07002471 if (srp_dev->fmr_pool)
2472 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2473 ib_dereg_mr(srp_dev->mr);
2474 ib_dealloc_pd(srp_dev->pd);
2475
2476 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002477}
2478
FUJITA Tomonori32368222007-06-27 16:33:12 +09002479static struct srp_function_template ib_srp_transport_functions = {
2480};
2481
Roland Dreieraef9ec32005-11-02 14:07:13 -08002482static int __init srp_init_module(void)
2483{
2484 int ret;
2485
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05002486 BUILD_BUG_ON(FIELD_SIZEOF(struct ib_wc, wr_id) < sizeof(void *));
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00002487
David Dillow49248642011-01-14 18:23:24 -05002488 if (srp_sg_tablesize) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002489 pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n");
David Dillow49248642011-01-14 18:23:24 -05002490 if (!cmd_sg_entries)
2491 cmd_sg_entries = srp_sg_tablesize;
2492 }
2493
2494 if (!cmd_sg_entries)
2495 cmd_sg_entries = SRP_DEF_SG_TABLESIZE;
2496
2497 if (cmd_sg_entries > 255) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002498 pr_warn("Clamping cmd_sg_entries to 255\n");
David Dillow49248642011-01-14 18:23:24 -05002499 cmd_sg_entries = 255;
David Dillow1e89a192008-04-16 21:01:12 -07002500 }
2501
David Dillowc07d4242011-01-16 13:57:10 -05002502 if (!indirect_sg_entries)
2503 indirect_sg_entries = cmd_sg_entries;
2504 else if (indirect_sg_entries < cmd_sg_entries) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002505 pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n",
2506 cmd_sg_entries);
David Dillowc07d4242011-01-16 13:57:10 -05002507 indirect_sg_entries = cmd_sg_entries;
2508 }
2509
FUJITA Tomonori32368222007-06-27 16:33:12 +09002510 ib_srp_transport_template =
2511 srp_attach_transport(&ib_srp_transport_functions);
2512 if (!ib_srp_transport_template)
2513 return -ENOMEM;
2514
Roland Dreieraef9ec32005-11-02 14:07:13 -08002515 ret = class_register(&srp_class);
2516 if (ret) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002517 pr_err("couldn't register class infiniband_srp\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002518 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002519 return ret;
2520 }
2521
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002522 ib_sa_register_client(&srp_sa_client);
2523
Roland Dreieraef9ec32005-11-02 14:07:13 -08002524 ret = ib_register_client(&srp_client);
2525 if (ret) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002526 pr_err("couldn't register IB client\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002527 srp_release_transport(ib_srp_transport_template);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002528 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002529 class_unregister(&srp_class);
2530 return ret;
2531 }
2532
2533 return 0;
2534}
2535
2536static void __exit srp_cleanup_module(void)
2537{
2538 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002539 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002540 class_unregister(&srp_class);
FUJITA Tomonori32368222007-06-27 16:33:12 +09002541 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002542}
2543
2544module_init(srp_init_module);
2545module_exit(srp_cleanup_module);