blob: 895a9ff17379a53673f8c32e81c0070cb2ea7b48 [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
David Howellsc4028952006-11-22 14:57:56 +0000477static void srp_remove_work(struct work_struct *work)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800478{
David Howellsc4028952006-11-22 14:57:56 +0000479 struct srp_target_port *target =
480 container_of(work, struct srp_target_port, work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800481
Bart Van Assche9709f0e2010-11-26 13:13:06 -0500482 if (!srp_change_state(target, SRP_TARGET_DEAD, SRP_TARGET_REMOVED))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800483 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800484
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700485 spin_lock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800486 list_del(&target->list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700487 spin_unlock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800488
FUJITA Tomonori32368222007-06-27 16:33:12 +0900489 srp_remove_host(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800490 scsi_remove_host(target->scsi_host);
491 ib_destroy_cm_id(target->cm_id);
492 srp_free_target_ib(target);
David Dillow8f26c9f2011-01-14 19:45:50 -0500493 srp_free_req_data(target);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800494 scsi_host_put(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800495}
496
497static int srp_connect_target(struct srp_target_port *target)
498{
David Dillow9fe4bcf2008-01-08 17:08:52 -0500499 int retries = 3;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800500 int ret;
501
502 ret = srp_lookup_path(target);
503 if (ret)
504 return ret;
505
506 while (1) {
507 init_completion(&target->done);
508 ret = srp_send_req(target);
509 if (ret)
510 return ret;
511 wait_for_completion(&target->done);
512
513 /*
514 * The CM event handling code will set status to
515 * SRP_PORT_REDIRECT if we get a port redirect REJ
516 * back, or SRP_DLID_REDIRECT if we get a lid/qp
517 * redirect REJ back.
518 */
519 switch (target->status) {
520 case 0:
521 return 0;
522
523 case SRP_PORT_REDIRECT:
524 ret = srp_lookup_path(target);
525 if (ret)
526 return ret;
527 break;
528
529 case SRP_DLID_REDIRECT:
530 break;
531
David Dillow9fe4bcf2008-01-08 17:08:52 -0500532 case SRP_STALE_CONN:
533 /* Our current CM id was stale, and is now in timewait.
534 * Try to reconnect with a new one.
535 */
536 if (!retries-- || srp_new_cm_id(target)) {
537 shost_printk(KERN_ERR, target->scsi_host, PFX
538 "giving up on stale connection\n");
539 target->status = -ECONNRESET;
540 return target->status;
541 }
542
543 shost_printk(KERN_ERR, target->scsi_host, PFX
544 "retrying stale connection\n");
545 break;
546
Roland Dreieraef9ec32005-11-02 14:07:13 -0800547 default:
548 return target->status;
549 }
550 }
551}
552
Roland Dreierd945e1d2006-05-09 10:50:28 -0700553static void srp_unmap_data(struct scsi_cmnd *scmnd,
554 struct srp_target_port *target,
555 struct srp_request *req)
556{
David Dillow8f26c9f2011-01-14 19:45:50 -0500557 struct ib_device *ibdev = target->srp_host->srp_dev->dev;
558 struct ib_pool_fmr **pfmr;
559
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900560 if (!scsi_sglist(scmnd) ||
Roland Dreierd945e1d2006-05-09 10:50:28 -0700561 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
562 scmnd->sc_data_direction != DMA_FROM_DEVICE))
563 return;
564
David Dillow8f26c9f2011-01-14 19:45:50 -0500565 pfmr = req->fmr_list;
566 while (req->nfmr--)
567 ib_fmr_pool_unmap(*pfmr++);
Roland Dreierf5358a12006-06-17 20:37:29 -0700568
David Dillow8f26c9f2011-01-14 19:45:50 -0500569 ib_dma_unmap_sg(ibdev, scsi_sglist(scmnd), scsi_sg_count(scmnd),
570 scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700571}
572
Bart Van Assche94a91742010-11-26 14:50:09 -0500573static void srp_remove_req(struct srp_target_port *target,
574 struct srp_request *req, s32 req_lim_delta)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700575{
Bart Van Assche94a91742010-11-26 14:50:09 -0500576 unsigned long flags;
577
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700578 srp_unmap_data(req->scmnd, target, req);
Bart Van Asschee9684672010-11-26 15:08:38 -0500579 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -0500580 target->req_lim += req_lim_delta;
David Dillowf8b6e312010-11-26 13:02:21 -0500581 req->scmnd = NULL;
Bart Van Assche536ae142010-11-26 13:58:27 -0500582 list_add_tail(&req->list, &target->free_reqs);
Bart Van Asschee9684672010-11-26 15:08:38 -0500583 spin_unlock_irqrestore(&target->lock, flags);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700584}
585
586static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
587{
588 req->scmnd->result = DID_RESET << 16;
589 req->scmnd->scsi_done(req->scmnd);
Bart Van Assche94a91742010-11-26 14:50:09 -0500590 srp_remove_req(target, req, 0);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700591}
592
Roland Dreieraef9ec32005-11-02 14:07:13 -0800593static int srp_reconnect_target(struct srp_target_port *target)
594{
Roland Dreieraef9ec32005-11-02 14:07:13 -0800595 struct ib_qp_attr qp_attr;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800596 struct ib_wc wc;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -0500597 int i, ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800598
Bart Van Assche9709f0e2010-11-26 13:13:06 -0500599 if (!srp_change_state(target, SRP_TARGET_LIVE, SRP_TARGET_CONNECTING))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800600 return -EAGAIN;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800601
602 srp_disconnect_target(target);
603 /*
604 * Now get a new local CM ID so that we avoid confusing the
605 * target in case things are really fouled up.
606 */
David Dillow9fe4bcf2008-01-08 17:08:52 -0500607 ret = srp_new_cm_id(target);
608 if (ret)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800609 goto err;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800610
611 qp_attr.qp_state = IB_QPS_RESET;
612 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
613 if (ret)
614 goto err;
615
616 ret = srp_init_qp(target, target->qp);
617 if (ret)
618 goto err;
619
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000620 while (ib_poll_cq(target->recv_cq, 1, &wc) > 0)
621 ; /* nothing */
622 while (ib_poll_cq(target->send_cq, 1, &wc) > 0)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800623 ; /* nothing */
624
Bart Van Assche536ae142010-11-26 13:58:27 -0500625 for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
626 struct srp_request *req = &target->req_ring[i];
627 if (req->scmnd)
628 srp_reset_req(target, req);
629 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800630
Bart Van Assche536ae142010-11-26 13:58:27 -0500631 INIT_LIST_HEAD(&target->free_tx);
Bart Van Asschedcb4cb82010-11-26 13:22:48 -0500632 for (i = 0; i < SRP_SQ_SIZE; ++i)
Bart Van Assche536ae142010-11-26 13:58:27 -0500633 list_add(&target->tx_ring[i]->list, &target->free_tx);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800634
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200635 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800636 ret = srp_connect_target(target);
637 if (ret)
638 goto err;
639
Bart Van Assche9709f0e2010-11-26 13:13:06 -0500640 if (!srp_change_state(target, SRP_TARGET_CONNECTING, SRP_TARGET_LIVE))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800641 ret = -EAGAIN;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800642
643 return ret;
644
645err:
David Dillow7aa54bd2008-01-07 18:23:41 -0500646 shost_printk(KERN_ERR, target->scsi_host,
647 PFX "reconnect failed (%d), removing target port.\n", ret);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800648
649 /*
650 * We couldn't reconnect, so kill our target port off.
Bart Van Assche9709f0e2010-11-26 13:13:06 -0500651 * However, we have to defer the real removal because we
652 * are in the context of the SCSI error handler now, which
653 * will deadlock if we call scsi_remove_host().
654 *
655 * Schedule our work inside the lock to avoid a race with
656 * the flush_scheduled_work() in srp_remove_one().
Roland Dreieraef9ec32005-11-02 14:07:13 -0800657 */
Bart Van Asschee9684672010-11-26 15:08:38 -0500658 spin_lock_irq(&target->lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800659 if (target->state == SRP_TARGET_CONNECTING) {
660 target->state = SRP_TARGET_DEAD;
David Howellsc4028952006-11-22 14:57:56 +0000661 INIT_WORK(&target->work, srp_remove_work);
Tejun Heof0626712010-10-19 15:24:36 +0000662 queue_work(ib_wq, &target->work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800663 }
Bart Van Asschee9684672010-11-26 15:08:38 -0500664 spin_unlock_irq(&target->lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800665
666 return ret;
667}
668
David Dillow8f26c9f2011-01-14 19:45:50 -0500669static void srp_map_desc(struct srp_map_state *state, dma_addr_t dma_addr,
670 unsigned int dma_len, u32 rkey)
Roland Dreierf5358a12006-06-17 20:37:29 -0700671{
David Dillow8f26c9f2011-01-14 19:45:50 -0500672 struct srp_direct_buf *desc = state->desc;
673
674 desc->va = cpu_to_be64(dma_addr);
675 desc->key = cpu_to_be32(rkey);
676 desc->len = cpu_to_be32(dma_len);
677
678 state->total_len += dma_len;
679 state->desc++;
680 state->ndesc++;
681}
682
683static int srp_map_finish_fmr(struct srp_map_state *state,
684 struct srp_target_port *target)
685{
686 struct srp_device *dev = target->srp_host->srp_dev;
687 struct ib_pool_fmr *fmr;
Roland Dreierf5358a12006-06-17 20:37:29 -0700688 u64 io_addr = 0;
David Dillow8f26c9f2011-01-14 19:45:50 -0500689
690 if (!state->npages)
691 return 0;
692
693 if (state->npages == 1) {
694 srp_map_desc(state, state->base_dma_addr, state->fmr_len,
695 target->rkey);
696 state->npages = state->fmr_len = 0;
697 return 0;
698 }
699
700 fmr = ib_fmr_pool_map_phys(dev->fmr_pool, state->pages,
701 state->npages, io_addr);
702 if (IS_ERR(fmr))
703 return PTR_ERR(fmr);
704
705 *state->next_fmr++ = fmr;
706 state->nfmr++;
707
708 srp_map_desc(state, 0, state->fmr_len, fmr->fmr->rkey);
709 state->npages = state->fmr_len = 0;
710 return 0;
711}
712
713static void srp_map_update_start(struct srp_map_state *state,
714 struct scatterlist *sg, int sg_index,
715 dma_addr_t dma_addr)
716{
717 state->unmapped_sg = sg;
718 state->unmapped_index = sg_index;
719 state->unmapped_addr = dma_addr;
720}
721
722static int srp_map_sg_entry(struct srp_map_state *state,
723 struct srp_target_port *target,
724 struct scatterlist *sg, int sg_index,
725 int use_fmr)
726{
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100727 struct srp_device *dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800728 struct ib_device *ibdev = dev->dev;
David Dillow8f26c9f2011-01-14 19:45:50 -0500729 dma_addr_t dma_addr = ib_sg_dma_address(ibdev, sg);
730 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
731 unsigned int len;
732 int ret;
Roland Dreierf5358a12006-06-17 20:37:29 -0700733
David Dillow8f26c9f2011-01-14 19:45:50 -0500734 if (!dma_len)
735 return 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700736
David Dillow8f26c9f2011-01-14 19:45:50 -0500737 if (use_fmr == SRP_MAP_NO_FMR) {
738 /* Once we're in direct map mode for a request, we don't
739 * go back to FMR mode, so no need to update anything
740 * other than the descriptor.
741 */
742 srp_map_desc(state, dma_addr, dma_len, target->rkey);
743 return 0;
744 }
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700745
David Dillow8f26c9f2011-01-14 19:45:50 -0500746 /* If we start at an offset into the FMR page, don't merge into
747 * the current FMR. Finish it out, and use the kernel's MR for this
748 * sg entry. This is to avoid potential bugs on some SRP targets
749 * that were never quite defined, but went away when the initiator
750 * avoided using FMR on such page fragments.
751 */
752 if (dma_addr & ~dev->fmr_page_mask || dma_len > dev->fmr_max_size) {
753 ret = srp_map_finish_fmr(state, target);
754 if (ret)
755 return ret;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800756
David Dillow8f26c9f2011-01-14 19:45:50 -0500757 srp_map_desc(state, dma_addr, dma_len, target->rkey);
758 srp_map_update_start(state, NULL, 0, 0);
759 return 0;
760 }
761
762 /* If this is the first sg to go into the FMR, save our position.
763 * We need to know the first unmapped entry, its index, and the
764 * first unmapped address within that entry to be able to restart
765 * mapping after an error.
766 */
767 if (!state->unmapped_sg)
768 srp_map_update_start(state, sg, sg_index, dma_addr);
769
770 while (dma_len) {
771 if (state->npages == SRP_FMR_SIZE) {
772 ret = srp_map_finish_fmr(state, target);
773 if (ret)
774 return ret;
775
776 srp_map_update_start(state, sg, sg_index, dma_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -0700777 }
778
David Dillow8f26c9f2011-01-14 19:45:50 -0500779 len = min_t(unsigned int, dma_len, dev->fmr_page_size);
780
781 if (!state->npages)
782 state->base_dma_addr = dma_addr;
783 state->pages[state->npages++] = dma_addr;
784 state->fmr_len += len;
785 dma_addr += len;
786 dma_len -= len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700787 }
788
David Dillow8f26c9f2011-01-14 19:45:50 -0500789 /* If the last entry of the FMR wasn't a full page, then we need to
790 * close it out and start a new one -- we can only merge at page
791 * boundries.
792 */
Roland Dreierf5358a12006-06-17 20:37:29 -0700793 ret = 0;
David Dillow8f26c9f2011-01-14 19:45:50 -0500794 if (len != dev->fmr_page_size) {
795 ret = srp_map_finish_fmr(state, target);
796 if (!ret)
797 srp_map_update_start(state, NULL, 0, 0);
798 }
Roland Dreierf5358a12006-06-17 20:37:29 -0700799 return ret;
800}
801
Roland Dreieraef9ec32005-11-02 14:07:13 -0800802static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
803 struct srp_request *req)
804{
David Dillow8f26c9f2011-01-14 19:45:50 -0500805 struct scatterlist *scat, *sg;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800806 struct srp_cmd *cmd = req->cmd->buf;
David Dillow8f26c9f2011-01-14 19:45:50 -0500807 int i, len, nents, count, use_fmr;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800808 struct srp_device *dev;
809 struct ib_device *ibdev;
David Dillow8f26c9f2011-01-14 19:45:50 -0500810 struct srp_map_state state;
811 struct srp_indirect_buf *indirect_hdr;
David Dillow8f26c9f2011-01-14 19:45:50 -0500812 u32 table_len;
813 u8 fmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800814
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900815 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800816 return sizeof (struct srp_cmd);
817
818 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
819 scmnd->sc_data_direction != DMA_TO_DEVICE) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500820 shost_printk(KERN_WARNING, target->scsi_host,
821 PFX "Unhandled data direction %d\n",
822 scmnd->sc_data_direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800823 return -EINVAL;
824 }
825
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900826 nents = scsi_sg_count(scmnd);
827 scat = scsi_sglist(scmnd);
Roland Dreiercf368712006-03-24 15:47:26 -0800828
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100829 dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800830 ibdev = dev->dev;
831
832 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
David Dillow8f26c9f2011-01-14 19:45:50 -0500833 if (unlikely(count == 0))
834 return -EIO;
Roland Dreierf5358a12006-06-17 20:37:29 -0700835
836 fmt = SRP_DATA_DESC_DIRECT;
837 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800838
839 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700840 /*
841 * The midlayer only generated a single gather/scatter
842 * entry, or DMA mapping coalesced everything to a
843 * single entry. So a direct descriptor along with
844 * the DMA MR suffices.
845 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800846 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800847
Ralph Campbell85507bc2006-12-12 14:30:55 -0800848 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
David Dillow9af76272010-11-26 15:34:46 -0500849 buf->key = cpu_to_be32(target->rkey);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800850 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Roland Dreiercf368712006-03-24 15:47:26 -0800851
David Dillow8f26c9f2011-01-14 19:45:50 -0500852 req->nfmr = 0;
853 goto map_complete;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800854 }
855
David Dillow8f26c9f2011-01-14 19:45:50 -0500856 /* We have more than one scatter/gather entry, so build our indirect
857 * descriptor table, trying to merge as many entries with FMR as we
858 * can.
859 */
860 indirect_hdr = (void *) cmd->add_data;
861
David Dillowc07d4242011-01-16 13:57:10 -0500862 ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr,
863 target->indirect_size, DMA_TO_DEVICE);
864
David Dillow8f26c9f2011-01-14 19:45:50 -0500865 memset(&state, 0, sizeof(state));
David Dillowc07d4242011-01-16 13:57:10 -0500866 state.desc = req->indirect_desc;
David Dillow8f26c9f2011-01-14 19:45:50 -0500867 state.pages = req->map_page;
868 state.next_fmr = req->fmr_list;
869
870 use_fmr = dev->fmr_pool ? SRP_MAP_ALLOW_FMR : SRP_MAP_NO_FMR;
871
872 for_each_sg(scat, sg, count, i) {
873 if (srp_map_sg_entry(&state, target, sg, i, use_fmr)) {
874 /* FMR mapping failed, so backtrack to the first
875 * unmapped entry and continue on without using FMR.
876 */
877 dma_addr_t dma_addr;
878 unsigned int dma_len;
879
880backtrack:
881 sg = state.unmapped_sg;
882 i = state.unmapped_index;
883
884 dma_addr = ib_sg_dma_address(ibdev, sg);
885 dma_len = ib_sg_dma_len(ibdev, sg);
886 dma_len -= (state.unmapped_addr - dma_addr);
887 dma_addr = state.unmapped_addr;
888 use_fmr = SRP_MAP_NO_FMR;
889 srp_map_desc(&state, dma_addr, dma_len, target->rkey);
890 }
891 }
892
893 if (use_fmr == SRP_MAP_ALLOW_FMR && srp_map_finish_fmr(&state, target))
894 goto backtrack;
895
David Dillowc07d4242011-01-16 13:57:10 -0500896 /* We've mapped the request, now pull as much of the indirect
897 * descriptor table as we can into the command buffer. If this
898 * target is not using an external indirect table, we are
899 * guaranteed to fit into the command, as the SCSI layer won't
900 * give us more S/G entries than we allow.
David Dillow8f26c9f2011-01-14 19:45:50 -0500901 */
902 req->nfmr = state.nfmr;
903 if (state.ndesc == 1) {
904 /* FMR mapping was able to collapse this to one entry,
905 * so use a direct descriptor.
906 */
907 struct srp_direct_buf *buf = (void *) cmd->add_data;
908
David Dillowc07d4242011-01-16 13:57:10 -0500909 *buf = req->indirect_desc[0];
David Dillow8f26c9f2011-01-14 19:45:50 -0500910 goto map_complete;
911 }
912
David Dillowc07d4242011-01-16 13:57:10 -0500913 if (unlikely(target->cmd_sg_cnt < state.ndesc &&
914 !target->allow_ext_sg)) {
915 shost_printk(KERN_ERR, target->scsi_host,
916 "Could not fit S/G list into SRP_CMD\n");
917 return -EIO;
918 }
919
920 count = min(state.ndesc, target->cmd_sg_cnt);
David Dillow8f26c9f2011-01-14 19:45:50 -0500921 table_len = state.ndesc * sizeof (struct srp_direct_buf);
922
923 fmt = SRP_DATA_DESC_INDIRECT;
924 len = sizeof(struct srp_cmd) + sizeof (struct srp_indirect_buf);
David Dillowc07d4242011-01-16 13:57:10 -0500925 len += count * sizeof (struct srp_direct_buf);
David Dillow8f26c9f2011-01-14 19:45:50 -0500926
David Dillowc07d4242011-01-16 13:57:10 -0500927 memcpy(indirect_hdr->desc_list, req->indirect_desc,
928 count * sizeof (struct srp_direct_buf));
David Dillow8f26c9f2011-01-14 19:45:50 -0500929
David Dillowc07d4242011-01-16 13:57:10 -0500930 indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr);
David Dillow8f26c9f2011-01-14 19:45:50 -0500931 indirect_hdr->table_desc.key = cpu_to_be32(target->rkey);
932 indirect_hdr->table_desc.len = cpu_to_be32(table_len);
933 indirect_hdr->len = cpu_to_be32(state.total_len);
934
935 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
David Dillowc07d4242011-01-16 13:57:10 -0500936 cmd->data_out_desc_cnt = count;
David Dillow8f26c9f2011-01-14 19:45:50 -0500937 else
David Dillowc07d4242011-01-16 13:57:10 -0500938 cmd->data_in_desc_cnt = count;
939
940 ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len,
941 DMA_TO_DEVICE);
David Dillow8f26c9f2011-01-14 19:45:50 -0500942
943map_complete:
Roland Dreieraef9ec32005-11-02 14:07:13 -0800944 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
945 cmd->buf_fmt = fmt << 4;
946 else
947 cmd->buf_fmt = fmt;
948
Roland Dreieraef9ec32005-11-02 14:07:13 -0800949 return len;
950}
951
David Dillow05a1d752010-10-08 14:48:14 -0400952/*
Bart Van Assche76c75b22010-11-26 14:37:47 -0500953 * Return an IU and possible credit to the free pool
954 */
955static void srp_put_tx_iu(struct srp_target_port *target, struct srp_iu *iu,
956 enum srp_iu_type iu_type)
957{
958 unsigned long flags;
959
Bart Van Asschee9684672010-11-26 15:08:38 -0500960 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -0500961 list_add(&iu->list, &target->free_tx);
962 if (iu_type != SRP_IU_RSP)
963 ++target->req_lim;
Bart Van Asschee9684672010-11-26 15:08:38 -0500964 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -0500965}
966
967/*
Bart Van Asschee9684672010-11-26 15:08:38 -0500968 * Must be called with target->lock held to protect req_lim and free_tx.
969 * If IU is not sent, it must be returned using srp_put_tx_iu().
David Dillow05a1d752010-10-08 14:48:14 -0400970 *
971 * Note:
972 * An upper limit for the number of allocated information units for each
973 * request type is:
974 * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues
975 * more than Scsi_Host.can_queue requests.
976 * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE.
977 * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than
978 * one unanswered SRP request to an initiator.
979 */
980static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
981 enum srp_iu_type iu_type)
982{
983 s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE;
984 struct srp_iu *iu;
985
986 srp_send_completion(target->send_cq, target);
987
Bart Van Asschedcb4cb82010-11-26 13:22:48 -0500988 if (list_empty(&target->free_tx))
David Dillow05a1d752010-10-08 14:48:14 -0400989 return NULL;
990
991 /* Initiator responses to target requests do not consume credits */
Bart Van Assche76c75b22010-11-26 14:37:47 -0500992 if (iu_type != SRP_IU_RSP) {
993 if (target->req_lim <= rsv) {
994 ++target->zero_req_lim;
995 return NULL;
996 }
997
998 --target->req_lim;
David Dillow05a1d752010-10-08 14:48:14 -0400999 }
1000
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001001 iu = list_first_entry(&target->free_tx, struct srp_iu, list);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001002 list_del(&iu->list);
David Dillow05a1d752010-10-08 14:48:14 -04001003 return iu;
1004}
1005
Bart Van Assche76c75b22010-11-26 14:37:47 -05001006static int srp_post_send(struct srp_target_port *target,
1007 struct srp_iu *iu, int len)
David Dillow05a1d752010-10-08 14:48:14 -04001008{
1009 struct ib_sge list;
1010 struct ib_send_wr wr, *bad_wr;
David Dillow05a1d752010-10-08 14:48:14 -04001011
1012 list.addr = iu->dma;
1013 list.length = len;
David Dillow9af76272010-11-26 15:34:46 -05001014 list.lkey = target->lkey;
David Dillow05a1d752010-10-08 14:48:14 -04001015
1016 wr.next = NULL;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001017 wr.wr_id = (uintptr_t) iu;
David Dillow05a1d752010-10-08 14:48:14 -04001018 wr.sg_list = &list;
1019 wr.num_sge = 1;
1020 wr.opcode = IB_WR_SEND;
1021 wr.send_flags = IB_SEND_SIGNALED;
1022
Bart Van Assche76c75b22010-11-26 14:37:47 -05001023 return ib_post_send(target->qp, &wr, &bad_wr);
David Dillow05a1d752010-10-08 14:48:14 -04001024}
1025
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001026static int srp_post_recv(struct srp_target_port *target, struct srp_iu *iu)
Bart Van Asschec996bb42010-07-30 10:59:05 +00001027{
Bart Van Asschec996bb42010-07-30 10:59:05 +00001028 struct ib_recv_wr wr, *bad_wr;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001029 struct ib_sge list;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001030
1031 list.addr = iu->dma;
1032 list.length = iu->size;
David Dillow9af76272010-11-26 15:34:46 -05001033 list.lkey = target->lkey;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001034
1035 wr.next = NULL;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001036 wr.wr_id = (uintptr_t) iu;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001037 wr.sg_list = &list;
1038 wr.num_sge = 1;
1039
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001040 return ib_post_recv(target->qp, &wr, &bad_wr);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001041}
1042
Roland Dreieraef9ec32005-11-02 14:07:13 -08001043static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
1044{
1045 struct srp_request *req;
1046 struct scsi_cmnd *scmnd;
1047 unsigned long flags;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001048
Roland Dreieraef9ec32005-11-02 14:07:13 -08001049 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
Bart Van Asschee9684672010-11-26 15:08:38 -05001050 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -05001051 target->req_lim += be32_to_cpu(rsp->req_lim_delta);
Bart Van Asschee9684672010-11-26 15:08:38 -05001052 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche94a91742010-11-26 14:50:09 -05001053
David Dillowf8b6e312010-11-26 13:02:21 -05001054 target->tsk_mgmt_status = -1;
1055 if (be32_to_cpu(rsp->resp_data_len) >= 4)
1056 target->tsk_mgmt_status = rsp->data[3];
1057 complete(&target->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001058 } else {
David Dillowf8b6e312010-11-26 13:02:21 -05001059 req = &target->req_ring[rsp->tag];
Roland Dreierd945e1d2006-05-09 10:50:28 -07001060 scmnd = req->scmnd;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001061 if (!scmnd)
David Dillow7aa54bd2008-01-07 18:23:41 -05001062 shost_printk(KERN_ERR, target->scsi_host,
1063 "Null scmnd for RSP w/tag %016llx\n",
1064 (unsigned long long) rsp->tag);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001065 scmnd->result = rsp->status;
1066
1067 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
1068 memcpy(scmnd->sense_buffer, rsp->data +
1069 be32_to_cpu(rsp->resp_data_len),
1070 min_t(int, be32_to_cpu(rsp->sense_data_len),
1071 SCSI_SENSE_BUFFERSIZE));
1072 }
1073
1074 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001075 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001076 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +09001077 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001078
Bart Van Assche94a91742010-11-26 14:50:09 -05001079 srp_remove_req(target, req, be32_to_cpu(rsp->req_lim_delta));
David Dillowf8b6e312010-11-26 13:02:21 -05001080 scmnd->host_scribble = NULL;
1081 scmnd->scsi_done(scmnd);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001082 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001083}
1084
David Dillowbb125882010-10-08 14:40:47 -04001085static int srp_response_common(struct srp_target_port *target, s32 req_delta,
1086 void *rsp, int len)
1087{
Bart Van Assche76c75b22010-11-26 14:37:47 -05001088 struct ib_device *dev = target->srp_host->srp_dev->dev;
David Dillowbb125882010-10-08 14:40:47 -04001089 unsigned long flags;
1090 struct srp_iu *iu;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001091 int err;
David Dillowbb125882010-10-08 14:40:47 -04001092
Bart Van Asschee9684672010-11-26 15:08:38 -05001093 spin_lock_irqsave(&target->lock, flags);
David Dillowbb125882010-10-08 14:40:47 -04001094 target->req_lim += req_delta;
David Dillowbb125882010-10-08 14:40:47 -04001095 iu = __srp_get_tx_iu(target, SRP_IU_RSP);
Bart Van Asschee9684672010-11-26 15:08:38 -05001096 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001097
David Dillowbb125882010-10-08 14:40:47 -04001098 if (!iu) {
1099 shost_printk(KERN_ERR, target->scsi_host, PFX
1100 "no IU available to send response\n");
Bart Van Assche76c75b22010-11-26 14:37:47 -05001101 return 1;
David Dillowbb125882010-10-08 14:40:47 -04001102 }
1103
1104 ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE);
1105 memcpy(iu->buf, rsp, len);
1106 ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE);
1107
Bart Van Assche76c75b22010-11-26 14:37:47 -05001108 err = srp_post_send(target, iu, len);
1109 if (err) {
David Dillowbb125882010-10-08 14:40:47 -04001110 shost_printk(KERN_ERR, target->scsi_host, PFX
1111 "unable to post response: %d\n", err);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001112 srp_put_tx_iu(target, iu, SRP_IU_RSP);
1113 }
David Dillowbb125882010-10-08 14:40:47 -04001114
David Dillowbb125882010-10-08 14:40:47 -04001115 return err;
1116}
1117
1118static void srp_process_cred_req(struct srp_target_port *target,
1119 struct srp_cred_req *req)
1120{
1121 struct srp_cred_rsp rsp = {
1122 .opcode = SRP_CRED_RSP,
1123 .tag = req->tag,
1124 };
1125 s32 delta = be32_to_cpu(req->req_lim_delta);
1126
1127 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1128 shost_printk(KERN_ERR, target->scsi_host, PFX
1129 "problems processing SRP_CRED_REQ\n");
1130}
1131
1132static void srp_process_aer_req(struct srp_target_port *target,
1133 struct srp_aer_req *req)
1134{
1135 struct srp_aer_rsp rsp = {
1136 .opcode = SRP_AER_RSP,
1137 .tag = req->tag,
1138 };
1139 s32 delta = be32_to_cpu(req->req_lim_delta);
1140
1141 shost_printk(KERN_ERR, target->scsi_host, PFX
1142 "ignoring AER for LUN %llu\n", be64_to_cpu(req->lun));
1143
1144 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1145 shost_printk(KERN_ERR, target->scsi_host, PFX
1146 "problems processing SRP_AER_REQ\n");
1147}
1148
Roland Dreieraef9ec32005-11-02 14:07:13 -08001149static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
1150{
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001151 struct ib_device *dev = target->srp_host->srp_dev->dev;
Roland Dreier737b94e2011-05-23 11:30:04 -07001152 struct srp_iu *iu = (struct srp_iu *) (uintptr_t) wc->wr_id;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001153 int res;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001154 u8 opcode;
1155
Ralph Campbell85507bc2006-12-12 14:30:55 -08001156 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
1157 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001158
1159 opcode = *(u8 *) iu->buf;
1160
1161 if (0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001162 shost_printk(KERN_ERR, target->scsi_host,
1163 PFX "recv completion, opcode 0x%02x\n", opcode);
Bart Van Assche7a700812010-07-29 15:56:37 +00001164 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1,
1165 iu->buf, wc->byte_len, true);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001166 }
1167
1168 switch (opcode) {
1169 case SRP_RSP:
1170 srp_process_rsp(target, iu->buf);
1171 break;
1172
David Dillowbb125882010-10-08 14:40:47 -04001173 case SRP_CRED_REQ:
1174 srp_process_cred_req(target, iu->buf);
1175 break;
1176
1177 case SRP_AER_REQ:
1178 srp_process_aer_req(target, iu->buf);
1179 break;
1180
Roland Dreieraef9ec32005-11-02 14:07:13 -08001181 case SRP_T_LOGOUT:
1182 /* XXX Handle target logout */
David Dillow7aa54bd2008-01-07 18:23:41 -05001183 shost_printk(KERN_WARNING, target->scsi_host,
1184 PFX "Got target logout request\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001185 break;
1186
1187 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001188 shost_printk(KERN_WARNING, target->scsi_host,
1189 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001190 break;
1191 }
1192
Ralph Campbell85507bc2006-12-12 14:30:55 -08001193 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
1194 DMA_FROM_DEVICE);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001195
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001196 res = srp_post_recv(target, iu);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001197 if (res != 0)
1198 shost_printk(KERN_ERR, target->scsi_host,
1199 PFX "Recv failed with error code %d\n", res);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001200}
1201
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001202static void srp_recv_completion(struct ib_cq *cq, void *target_ptr)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001203{
1204 struct srp_target_port *target = target_ptr;
1205 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001206
1207 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1208 while (ib_poll_cq(cq, 1, &wc) > 0) {
1209 if (wc.status) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001210 shost_printk(KERN_ERR, target->scsi_host,
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001211 PFX "failed receive status %d\n",
David Dillow7aa54bd2008-01-07 18:23:41 -05001212 wc.status);
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001213 target->qp_in_error = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001214 break;
1215 }
1216
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001217 srp_handle_recv(target, &wc);
1218 }
1219}
1220
1221static void srp_send_completion(struct ib_cq *cq, void *target_ptr)
1222{
1223 struct srp_target_port *target = target_ptr;
1224 struct ib_wc wc;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001225 struct srp_iu *iu;
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001226
1227 while (ib_poll_cq(cq, 1, &wc) > 0) {
1228 if (wc.status) {
1229 shost_printk(KERN_ERR, target->scsi_host,
1230 PFX "failed send status %d\n",
1231 wc.status);
1232 target->qp_in_error = 1;
1233 break;
1234 }
1235
Roland Dreier737b94e2011-05-23 11:30:04 -07001236 iu = (struct srp_iu *) (uintptr_t) wc.wr_id;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001237 list_add(&iu->list, &target->free_tx);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001238 }
1239}
1240
Bart Van Assche76c75b22010-11-26 14:37:47 -05001241static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001242{
Bart Van Assche76c75b22010-11-26 14:37:47 -05001243 struct srp_target_port *target = host_to_target(shost);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001244 struct srp_request *req;
1245 struct srp_iu *iu;
1246 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001247 struct ib_device *dev;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001248 unsigned long flags;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001249 int len;
1250
1251 if (target->state == SRP_TARGET_CONNECTING)
1252 goto err;
1253
1254 if (target->state == SRP_TARGET_DEAD ||
1255 target->state == SRP_TARGET_REMOVED) {
1256 scmnd->result = DID_BAD_TARGET << 16;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001257 scmnd->scsi_done(scmnd);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001258 return 0;
1259 }
1260
Bart Van Asschee9684672010-11-26 15:08:38 -05001261 spin_lock_irqsave(&target->lock, flags);
David Dillowbb125882010-10-08 14:40:47 -04001262 iu = __srp_get_tx_iu(target, SRP_IU_CMD);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001263 if (!iu)
Bart Van Assche695b8342011-01-13 19:02:25 +00001264 goto err_unlock;
1265
1266 req = list_first_entry(&target->free_reqs, struct srp_request, list);
1267 list_del(&req->list);
1268 spin_unlock_irqrestore(&target->lock, flags);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001269
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001270 dev = target->srp_host->srp_dev->dev;
David Dillow49248642011-01-14 18:23:24 -05001271 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08001272 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001273
Roland Dreieraef9ec32005-11-02 14:07:13 -08001274 scmnd->result = 0;
David Dillowf8b6e312010-11-26 13:02:21 -05001275 scmnd->host_scribble = (void *) req;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001276
1277 cmd = iu->buf;
1278 memset(cmd, 0, sizeof *cmd);
1279
1280 cmd->opcode = SRP_CMD;
1281 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001282 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001283 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1284
Roland Dreieraef9ec32005-11-02 14:07:13 -08001285 req->scmnd = scmnd;
1286 req->cmd = iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001287
1288 len = srp_map_data(scmnd, target, req);
1289 if (len < 0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001290 shost_printk(KERN_ERR, target->scsi_host,
1291 PFX "Failed to map data\n");
Bart Van Assche76c75b22010-11-26 14:37:47 -05001292 goto err_iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001293 }
1294
David Dillow49248642011-01-14 18:23:24 -05001295 ib_dma_sync_single_for_device(dev, iu->dma, target->max_iu_len,
Ralph Campbell85507bc2006-12-12 14:30:55 -08001296 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001297
Bart Van Assche76c75b22010-11-26 14:37:47 -05001298 if (srp_post_send(target, iu, len)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001299 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001300 goto err_unmap;
1301 }
1302
Roland Dreieraef9ec32005-11-02 14:07:13 -08001303 return 0;
1304
1305err_unmap:
1306 srp_unmap_data(scmnd, target, req);
1307
Bart Van Assche76c75b22010-11-26 14:37:47 -05001308err_iu:
1309 srp_put_tx_iu(target, iu, SRP_IU_CMD);
1310
Bart Van Asschee9684672010-11-26 15:08:38 -05001311 spin_lock_irqsave(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001312 list_add(&req->list, &target->free_reqs);
Bart Van Assche695b8342011-01-13 19:02:25 +00001313
1314err_unlock:
Bart Van Asschee9684672010-11-26 15:08:38 -05001315 spin_unlock_irqrestore(&target->lock, flags);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001316
Roland Dreieraef9ec32005-11-02 14:07:13 -08001317err:
1318 return SCSI_MLQUEUE_HOST_BUSY;
1319}
1320
1321static int srp_alloc_iu_bufs(struct srp_target_port *target)
1322{
1323 int i;
1324
1325 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1326 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1327 target->max_ti_iu_len,
1328 GFP_KERNEL, DMA_FROM_DEVICE);
1329 if (!target->rx_ring[i])
1330 goto err;
1331 }
1332
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001333 for (i = 0; i < SRP_SQ_SIZE; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001334 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
David Dillow49248642011-01-14 18:23:24 -05001335 target->max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001336 GFP_KERNEL, DMA_TO_DEVICE);
1337 if (!target->tx_ring[i])
1338 goto err;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001339
1340 list_add(&target->tx_ring[i]->list, &target->free_tx);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001341 }
1342
1343 return 0;
1344
1345err:
1346 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1347 srp_free_iu(target->srp_host, target->rx_ring[i]);
1348 target->rx_ring[i] = NULL;
1349 }
1350
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001351 for (i = 0; i < SRP_SQ_SIZE; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001352 srp_free_iu(target->srp_host, target->tx_ring[i]);
1353 target->tx_ring[i] = NULL;
1354 }
1355
1356 return -ENOMEM;
1357}
1358
David Dillow961e0be2011-01-14 17:32:07 -05001359static void srp_cm_rep_handler(struct ib_cm_id *cm_id,
1360 struct srp_login_rsp *lrsp,
1361 struct srp_target_port *target)
1362{
1363 struct ib_qp_attr *qp_attr = NULL;
1364 int attr_mask = 0;
1365 int ret;
1366 int i;
1367
1368 if (lrsp->opcode == SRP_LOGIN_RSP) {
1369 target->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len);
1370 target->req_lim = be32_to_cpu(lrsp->req_lim_delta);
1371
1372 /*
1373 * Reserve credits for task management so we don't
1374 * bounce requests back to the SCSI mid-layer.
1375 */
1376 target->scsi_host->can_queue
1377 = min(target->req_lim - SRP_TSK_MGMT_SQ_SIZE,
1378 target->scsi_host->can_queue);
1379 } else {
1380 shost_printk(KERN_WARNING, target->scsi_host,
1381 PFX "Unhandled RSP opcode %#x\n", lrsp->opcode);
1382 ret = -ECONNRESET;
1383 goto error;
1384 }
1385
1386 if (!target->rx_ring[0]) {
1387 ret = srp_alloc_iu_bufs(target);
1388 if (ret)
1389 goto error;
1390 }
1391
1392 ret = -ENOMEM;
1393 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1394 if (!qp_attr)
1395 goto error;
1396
1397 qp_attr->qp_state = IB_QPS_RTR;
1398 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1399 if (ret)
1400 goto error_free;
1401
1402 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
1403 if (ret)
1404 goto error_free;
1405
1406 for (i = 0; i < SRP_RQ_SIZE; i++) {
1407 struct srp_iu *iu = target->rx_ring[i];
1408 ret = srp_post_recv(target, iu);
1409 if (ret)
1410 goto error_free;
1411 }
1412
1413 qp_attr->qp_state = IB_QPS_RTS;
1414 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1415 if (ret)
1416 goto error_free;
1417
1418 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
1419 if (ret)
1420 goto error_free;
1421
1422 ret = ib_send_cm_rtu(cm_id, NULL, 0);
1423
1424error_free:
1425 kfree(qp_attr);
1426
1427error:
1428 target->status = ret;
1429}
1430
Roland Dreieraef9ec32005-11-02 14:07:13 -08001431static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1432 struct ib_cm_event *event,
1433 struct srp_target_port *target)
1434{
David Dillow7aa54bd2008-01-07 18:23:41 -05001435 struct Scsi_Host *shost = target->scsi_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001436 struct ib_class_port_info *cpi;
1437 int opcode;
1438
1439 switch (event->param.rej_rcvd.reason) {
1440 case IB_CM_REJ_PORT_CM_REDIRECT:
1441 cpi = event->param.rej_rcvd.ari;
1442 target->path.dlid = cpi->redirect_lid;
1443 target->path.pkey = cpi->redirect_pkey;
1444 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1445 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1446
1447 target->status = target->path.dlid ?
1448 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1449 break;
1450
1451 case IB_CM_REJ_PORT_REDIRECT:
Roland Dreier5d7cbfd2007-08-03 10:45:18 -07001452 if (srp_target_is_topspin(target)) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001453 /*
1454 * Topspin/Cisco SRP gateways incorrectly send
1455 * reject reason code 25 when they mean 24
1456 * (port redirect).
1457 */
1458 memcpy(target->path.dgid.raw,
1459 event->param.rej_rcvd.ari, 16);
1460
David Dillow7aa54bd2008-01-07 18:23:41 -05001461 shost_printk(KERN_DEBUG, shost,
1462 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1463 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1464 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001465
1466 target->status = SRP_PORT_REDIRECT;
1467 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001468 shost_printk(KERN_WARNING, shost,
1469 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001470 target->status = -ECONNRESET;
1471 }
1472 break;
1473
1474 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
David Dillow7aa54bd2008-01-07 18:23:41 -05001475 shost_printk(KERN_WARNING, shost,
1476 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001477 target->status = -ECONNRESET;
1478 break;
1479
1480 case IB_CM_REJ_CONSUMER_DEFINED:
1481 opcode = *(u8 *) event->private_data;
1482 if (opcode == SRP_LOGIN_REJ) {
1483 struct srp_login_rej *rej = event->private_data;
1484 u32 reason = be32_to_cpu(rej->reason);
1485
1486 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
David Dillow7aa54bd2008-01-07 18:23:41 -05001487 shost_printk(KERN_WARNING, shost,
1488 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001489 else
David Dillow7aa54bd2008-01-07 18:23:41 -05001490 shost_printk(KERN_WARNING, shost,
1491 PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001492 } else
David Dillow7aa54bd2008-01-07 18:23:41 -05001493 shost_printk(KERN_WARNING, shost,
1494 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1495 " opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001496 target->status = -ECONNRESET;
1497 break;
1498
David Dillow9fe4bcf2008-01-08 17:08:52 -05001499 case IB_CM_REJ_STALE_CONN:
1500 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
1501 target->status = SRP_STALE_CONN;
1502 break;
1503
Roland Dreieraef9ec32005-11-02 14:07:13 -08001504 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001505 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
1506 event->param.rej_rcvd.reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001507 target->status = -ECONNRESET;
1508 }
1509}
1510
1511static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1512{
1513 struct srp_target_port *target = cm_id->context;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001514 int comp = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001515
1516 switch (event->event) {
1517 case IB_CM_REQ_ERROR:
David Dillow7aa54bd2008-01-07 18:23:41 -05001518 shost_printk(KERN_DEBUG, target->scsi_host,
1519 PFX "Sending CM REQ failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001520 comp = 1;
1521 target->status = -ECONNRESET;
1522 break;
1523
1524 case IB_CM_REP_RECEIVED:
1525 comp = 1;
David Dillow961e0be2011-01-14 17:32:07 -05001526 srp_cm_rep_handler(cm_id, event->private_data, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001527 break;
1528
1529 case IB_CM_REJ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001530 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001531 comp = 1;
1532
1533 srp_cm_rej_handler(cm_id, event, target);
1534 break;
1535
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001536 case IB_CM_DREQ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001537 shost_printk(KERN_WARNING, target->scsi_host,
1538 PFX "DREQ received - connection closed\n");
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001539 if (ib_send_cm_drep(cm_id, NULL, 0))
David Dillow7aa54bd2008-01-07 18:23:41 -05001540 shost_printk(KERN_ERR, target->scsi_host,
1541 PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001542 break;
1543
1544 case IB_CM_TIMEWAIT_EXIT:
David Dillow7aa54bd2008-01-07 18:23:41 -05001545 shost_printk(KERN_ERR, target->scsi_host,
1546 PFX "connection closed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001547
1548 comp = 1;
1549 target->status = 0;
1550 break;
1551
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001552 case IB_CM_MRA_RECEIVED:
1553 case IB_CM_DREQ_ERROR:
1554 case IB_CM_DREP_RECEIVED:
1555 break;
1556
Roland Dreieraef9ec32005-11-02 14:07:13 -08001557 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001558 shost_printk(KERN_WARNING, target->scsi_host,
1559 PFX "Unhandled CM event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001560 break;
1561 }
1562
1563 if (comp)
1564 complete(&target->done);
1565
Roland Dreieraef9ec32005-11-02 14:07:13 -08001566 return 0;
1567}
1568
Roland Dreierd945e1d2006-05-09 10:50:28 -07001569static int srp_send_tsk_mgmt(struct srp_target_port *target,
David Dillowf8b6e312010-11-26 13:02:21 -05001570 u64 req_tag, unsigned int lun, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001571{
David Dillow19081f32010-10-18 08:54:49 -04001572 struct ib_device *dev = target->srp_host->srp_dev->dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001573 struct srp_iu *iu;
1574 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001575
Roland Dreier1285b3a2006-03-03 15:47:25 -08001576 if (target->state == SRP_TARGET_DEAD ||
David Dillowf8b6e312010-11-26 13:02:21 -05001577 target->state == SRP_TARGET_REMOVED)
Bart Van Assche76c75b22010-11-26 14:37:47 -05001578 return -1;
Roland Dreier1285b3a2006-03-03 15:47:25 -08001579
David Dillowf8b6e312010-11-26 13:02:21 -05001580 init_completion(&target->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001581
Bart Van Asschee9684672010-11-26 15:08:38 -05001582 spin_lock_irq(&target->lock);
David Dillowbb125882010-10-08 14:40:47 -04001583 iu = __srp_get_tx_iu(target, SRP_IU_TSK_MGMT);
Bart Van Asschee9684672010-11-26 15:08:38 -05001584 spin_unlock_irq(&target->lock);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001585
Roland Dreieraef9ec32005-11-02 14:07:13 -08001586 if (!iu)
Bart Van Assche76c75b22010-11-26 14:37:47 -05001587 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001588
David Dillow19081f32010-10-18 08:54:49 -04001589 ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt,
1590 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001591 tsk_mgmt = iu->buf;
1592 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1593
1594 tsk_mgmt->opcode = SRP_TSK_MGMT;
David Dillowf8b6e312010-11-26 13:02:21 -05001595 tsk_mgmt->lun = cpu_to_be64((u64) lun << 48);
1596 tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001597 tsk_mgmt->tsk_mgmt_func = func;
David Dillowf8b6e312010-11-26 13:02:21 -05001598 tsk_mgmt->task_tag = req_tag;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001599
David Dillow19081f32010-10-18 08:54:49 -04001600 ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt,
1601 DMA_TO_DEVICE);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001602 if (srp_post_send(target, iu, sizeof *tsk_mgmt)) {
1603 srp_put_tx_iu(target, iu, SRP_IU_TSK_MGMT);
1604 return -1;
1605 }
Roland Dreierd945e1d2006-05-09 10:50:28 -07001606
David Dillowf8b6e312010-11-26 13:02:21 -05001607 if (!wait_for_completion_timeout(&target->tsk_mgmt_done,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001608 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001609 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001610
Roland Dreierd945e1d2006-05-09 10:50:28 -07001611 return 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001612}
1613
Roland Dreieraef9ec32005-11-02 14:07:13 -08001614static int srp_abort(struct scsi_cmnd *scmnd)
1615{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001616 struct srp_target_port *target = host_to_target(scmnd->device->host);
David Dillowf8b6e312010-11-26 13:02:21 -05001617 struct srp_request *req = (struct srp_request *) scmnd->host_scribble;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001618 int ret = SUCCESS;
1619
David Dillow7aa54bd2008-01-07 18:23:41 -05001620 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001621
David Dillowf8b6e312010-11-26 13:02:21 -05001622 if (!req || target->qp_in_error)
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001623 return FAILED;
David Dillowf8b6e312010-11-26 13:02:21 -05001624 if (srp_send_tsk_mgmt(target, req->index, scmnd->device->lun,
1625 SRP_TSK_ABORT_TASK))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001626 return FAILED;
1627
David Dillowf8b6e312010-11-26 13:02:21 -05001628 if (req->scmnd) {
1629 if (!target->tsk_mgmt_status) {
Bart Van Assche94a91742010-11-26 14:50:09 -05001630 srp_remove_req(target, req, 0);
David Dillowf8b6e312010-11-26 13:02:21 -05001631 scmnd->result = DID_ABORT << 16;
1632 } else
1633 ret = FAILED;
1634 }
Roland Dreierd945e1d2006-05-09 10:50:28 -07001635
Roland Dreierd945e1d2006-05-09 10:50:28 -07001636 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001637}
1638
1639static int srp_reset_device(struct scsi_cmnd *scmnd)
1640{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001641 struct srp_target_port *target = host_to_target(scmnd->device->host);
Bart Van Assche536ae142010-11-26 13:58:27 -05001642 int i;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001643
David Dillow7aa54bd2008-01-07 18:23:41 -05001644 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001645
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001646 if (target->qp_in_error)
1647 return FAILED;
David Dillowf8b6e312010-11-26 13:02:21 -05001648 if (srp_send_tsk_mgmt(target, SRP_TAG_NO_REQ, scmnd->device->lun,
1649 SRP_TSK_LUN_RESET))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001650 return FAILED;
David Dillowf8b6e312010-11-26 13:02:21 -05001651 if (target->tsk_mgmt_status)
Roland Dreierd945e1d2006-05-09 10:50:28 -07001652 return FAILED;
1653
Bart Van Assche536ae142010-11-26 13:58:27 -05001654 for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
1655 struct srp_request *req = &target->req_ring[i];
David Dillowf8b6e312010-11-26 13:02:21 -05001656 if (req->scmnd && req->scmnd->device == scmnd->device)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001657 srp_reset_req(target, req);
Bart Van Assche536ae142010-11-26 13:58:27 -05001658 }
Roland Dreierd945e1d2006-05-09 10:50:28 -07001659
Roland Dreierd945e1d2006-05-09 10:50:28 -07001660 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001661}
1662
1663static int srp_reset_host(struct scsi_cmnd *scmnd)
1664{
1665 struct srp_target_port *target = host_to_target(scmnd->device->host);
1666 int ret = FAILED;
1667
David Dillow7aa54bd2008-01-07 18:23:41 -05001668 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001669
1670 if (!srp_reconnect_target(target))
1671 ret = SUCCESS;
1672
1673 return ret;
1674}
1675
Tony Jonesee959b02008-02-22 00:13:36 +01001676static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
1677 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001678{
Tony Jonesee959b02008-02-22 00:13:36 +01001679 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001680
1681 if (target->state == SRP_TARGET_DEAD ||
1682 target->state == SRP_TARGET_REMOVED)
1683 return -ENODEV;
1684
1685 return sprintf(buf, "0x%016llx\n",
1686 (unsigned long long) be64_to_cpu(target->id_ext));
1687}
1688
Tony Jonesee959b02008-02-22 00:13:36 +01001689static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
1690 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001691{
Tony Jonesee959b02008-02-22 00:13:36 +01001692 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001693
1694 if (target->state == SRP_TARGET_DEAD ||
1695 target->state == SRP_TARGET_REMOVED)
1696 return -ENODEV;
1697
1698 return sprintf(buf, "0x%016llx\n",
1699 (unsigned long long) be64_to_cpu(target->ioc_guid));
1700}
1701
Tony Jonesee959b02008-02-22 00:13:36 +01001702static ssize_t show_service_id(struct device *dev,
1703 struct device_attribute *attr, char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001704{
Tony Jonesee959b02008-02-22 00:13:36 +01001705 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001706
1707 if (target->state == SRP_TARGET_DEAD ||
1708 target->state == SRP_TARGET_REMOVED)
1709 return -ENODEV;
1710
1711 return sprintf(buf, "0x%016llx\n",
1712 (unsigned long long) be64_to_cpu(target->service_id));
1713}
1714
Tony Jonesee959b02008-02-22 00:13:36 +01001715static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
1716 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001717{
Tony Jonesee959b02008-02-22 00:13:36 +01001718 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001719
1720 if (target->state == SRP_TARGET_DEAD ||
1721 target->state == SRP_TARGET_REMOVED)
1722 return -ENODEV;
1723
1724 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1725}
1726
Tony Jonesee959b02008-02-22 00:13:36 +01001727static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
1728 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001729{
Tony Jonesee959b02008-02-22 00:13:36 +01001730 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001731
1732 if (target->state == SRP_TARGET_DEAD ||
1733 target->state == SRP_TARGET_REMOVED)
1734 return -ENODEV;
1735
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001736 return sprintf(buf, "%pI6\n", target->path.dgid.raw);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001737}
1738
Tony Jonesee959b02008-02-22 00:13:36 +01001739static ssize_t show_orig_dgid(struct device *dev,
1740 struct device_attribute *attr, char *buf)
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001741{
Tony Jonesee959b02008-02-22 00:13:36 +01001742 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001743
1744 if (target->state == SRP_TARGET_DEAD ||
1745 target->state == SRP_TARGET_REMOVED)
1746 return -ENODEV;
1747
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001748 return sprintf(buf, "%pI6\n", target->orig_dgid);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001749}
1750
Bart Van Assche89de7482010-08-03 14:08:45 +00001751static ssize_t show_req_lim(struct device *dev,
1752 struct device_attribute *attr, char *buf)
1753{
1754 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1755
1756 if (target->state == SRP_TARGET_DEAD ||
1757 target->state == SRP_TARGET_REMOVED)
1758 return -ENODEV;
1759
1760 return sprintf(buf, "%d\n", target->req_lim);
1761}
1762
Tony Jonesee959b02008-02-22 00:13:36 +01001763static ssize_t show_zero_req_lim(struct device *dev,
1764 struct device_attribute *attr, char *buf)
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001765{
Tony Jonesee959b02008-02-22 00:13:36 +01001766 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001767
1768 if (target->state == SRP_TARGET_DEAD ||
1769 target->state == SRP_TARGET_REMOVED)
1770 return -ENODEV;
1771
1772 return sprintf(buf, "%d\n", target->zero_req_lim);
1773}
1774
Tony Jonesee959b02008-02-22 00:13:36 +01001775static ssize_t show_local_ib_port(struct device *dev,
1776 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001777{
Tony Jonesee959b02008-02-22 00:13:36 +01001778 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001779
1780 return sprintf(buf, "%d\n", target->srp_host->port);
1781}
1782
Tony Jonesee959b02008-02-22 00:13:36 +01001783static ssize_t show_local_ib_device(struct device *dev,
1784 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001785{
Tony Jonesee959b02008-02-22 00:13:36 +01001786 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001787
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001788 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001789}
1790
David Dillow49248642011-01-14 18:23:24 -05001791static ssize_t show_cmd_sg_entries(struct device *dev,
1792 struct device_attribute *attr, char *buf)
1793{
1794 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1795
1796 return sprintf(buf, "%u\n", target->cmd_sg_cnt);
1797}
1798
David Dillowc07d4242011-01-16 13:57:10 -05001799static ssize_t show_allow_ext_sg(struct device *dev,
1800 struct device_attribute *attr, char *buf)
1801{
1802 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1803
1804 return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false");
1805}
1806
Tony Jonesee959b02008-02-22 00:13:36 +01001807static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1808static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1809static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1810static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1811static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1812static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
Bart Van Assche89de7482010-08-03 14:08:45 +00001813static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01001814static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1815static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1816static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
David Dillow49248642011-01-14 18:23:24 -05001817static DEVICE_ATTR(cmd_sg_entries, S_IRUGO, show_cmd_sg_entries, NULL);
David Dillowc07d4242011-01-16 13:57:10 -05001818static DEVICE_ATTR(allow_ext_sg, S_IRUGO, show_allow_ext_sg, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001819
Tony Jonesee959b02008-02-22 00:13:36 +01001820static struct device_attribute *srp_host_attrs[] = {
1821 &dev_attr_id_ext,
1822 &dev_attr_ioc_guid,
1823 &dev_attr_service_id,
1824 &dev_attr_pkey,
1825 &dev_attr_dgid,
1826 &dev_attr_orig_dgid,
Bart Van Assche89de7482010-08-03 14:08:45 +00001827 &dev_attr_req_lim,
Tony Jonesee959b02008-02-22 00:13:36 +01001828 &dev_attr_zero_req_lim,
1829 &dev_attr_local_ib_port,
1830 &dev_attr_local_ib_device,
David Dillow49248642011-01-14 18:23:24 -05001831 &dev_attr_cmd_sg_entries,
David Dillowc07d4242011-01-16 13:57:10 -05001832 &dev_attr_allow_ext_sg,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001833 NULL
1834};
1835
Roland Dreieraef9ec32005-11-02 14:07:13 -08001836static struct scsi_host_template srp_template = {
1837 .module = THIS_MODULE,
Roland Dreierb7f008f2007-05-06 21:18:11 -07001838 .name = "InfiniBand SRP initiator",
1839 .proc_name = DRV_NAME,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001840 .info = srp_target_info,
1841 .queuecommand = srp_queuecommand,
1842 .eh_abort_handler = srp_abort,
1843 .eh_device_reset_handler = srp_reset_device,
1844 .eh_host_reset_handler = srp_reset_host,
David Dillow49248642011-01-14 18:23:24 -05001845 .sg_tablesize = SRP_DEF_SG_TABLESIZE,
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001846 .can_queue = SRP_CMD_SQ_SIZE,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001847 .this_id = -1,
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001848 .cmd_per_lun = SRP_CMD_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001849 .use_clustering = ENABLE_CLUSTERING,
1850 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001851};
1852
1853static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1854{
FUJITA Tomonori32368222007-06-27 16:33:12 +09001855 struct srp_rport_identifiers ids;
1856 struct srp_rport *rport;
1857
Roland Dreieraef9ec32005-11-02 14:07:13 -08001858 sprintf(target->target_name, "SRP.T10:%016llX",
1859 (unsigned long long) be64_to_cpu(target->id_ext));
1860
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001861 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001862 return -ENODEV;
1863
FUJITA Tomonori32368222007-06-27 16:33:12 +09001864 memcpy(ids.port_id, &target->id_ext, 8);
1865 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09001866 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori32368222007-06-27 16:33:12 +09001867 rport = srp_rport_add(target->scsi_host, &ids);
1868 if (IS_ERR(rport)) {
1869 scsi_remove_host(target->scsi_host);
1870 return PTR_ERR(rport);
1871 }
1872
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001873 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001874 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001875 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001876
1877 target->state = SRP_TARGET_LIVE;
1878
Roland Dreieraef9ec32005-11-02 14:07:13 -08001879 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07001880 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001881
1882 return 0;
1883}
1884
Tony Jonesee959b02008-02-22 00:13:36 +01001885static void srp_release_dev(struct device *dev)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001886{
1887 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01001888 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001889
1890 complete(&host->released);
1891}
1892
1893static struct class srp_class = {
1894 .name = "infiniband_srp",
Tony Jonesee959b02008-02-22 00:13:36 +01001895 .dev_release = srp_release_dev
Roland Dreieraef9ec32005-11-02 14:07:13 -08001896};
1897
1898/*
1899 * Target ports are added by writing
1900 *
1901 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1902 * pkey=<P_Key>,service_id=<service ID>
1903 *
1904 * to the add_target sysfs attribute.
1905 */
1906enum {
1907 SRP_OPT_ERR = 0,
1908 SRP_OPT_ID_EXT = 1 << 0,
1909 SRP_OPT_IOC_GUID = 1 << 1,
1910 SRP_OPT_DGID = 1 << 2,
1911 SRP_OPT_PKEY = 1 << 3,
1912 SRP_OPT_SERVICE_ID = 1 << 4,
1913 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07001914 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07001915 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001916 SRP_OPT_INITIATOR_EXT = 1 << 8,
David Dillow49248642011-01-14 18:23:24 -05001917 SRP_OPT_CMD_SG_ENTRIES = 1 << 9,
David Dillowc07d4242011-01-16 13:57:10 -05001918 SRP_OPT_ALLOW_EXT_SG = 1 << 10,
1919 SRP_OPT_SG_TABLESIZE = 1 << 11,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001920 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1921 SRP_OPT_IOC_GUID |
1922 SRP_OPT_DGID |
1923 SRP_OPT_PKEY |
1924 SRP_OPT_SERVICE_ID),
1925};
1926
Steven Whitehousea447c092008-10-13 10:46:57 +01001927static const match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07001928 { SRP_OPT_ID_EXT, "id_ext=%s" },
1929 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1930 { SRP_OPT_DGID, "dgid=%s" },
1931 { SRP_OPT_PKEY, "pkey=%x" },
1932 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1933 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1934 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07001935 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001936 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
David Dillow49248642011-01-14 18:23:24 -05001937 { SRP_OPT_CMD_SG_ENTRIES, "cmd_sg_entries=%u" },
David Dillowc07d4242011-01-16 13:57:10 -05001938 { SRP_OPT_ALLOW_EXT_SG, "allow_ext_sg=%u" },
1939 { SRP_OPT_SG_TABLESIZE, "sg_tablesize=%u" },
Vu Pham52fb2b502006-06-17 20:37:31 -07001940 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001941};
1942
1943static int srp_parse_options(const char *buf, struct srp_target_port *target)
1944{
1945 char *options, *sep_opt;
1946 char *p;
1947 char dgid[3];
1948 substring_t args[MAX_OPT_ARGS];
1949 int opt_mask = 0;
1950 int token;
1951 int ret = -EINVAL;
1952 int i;
1953
1954 options = kstrdup(buf, GFP_KERNEL);
1955 if (!options)
1956 return -ENOMEM;
1957
1958 sep_opt = options;
1959 while ((p = strsep(&sep_opt, ",")) != NULL) {
1960 if (!*p)
1961 continue;
1962
1963 token = match_token(p, srp_opt_tokens, args);
1964 opt_mask |= token;
1965
1966 switch (token) {
1967 case SRP_OPT_ID_EXT:
1968 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001969 if (!p) {
1970 ret = -ENOMEM;
1971 goto out;
1972 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001973 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1974 kfree(p);
1975 break;
1976
1977 case SRP_OPT_IOC_GUID:
1978 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001979 if (!p) {
1980 ret = -ENOMEM;
1981 goto out;
1982 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001983 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1984 kfree(p);
1985 break;
1986
1987 case SRP_OPT_DGID:
1988 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001989 if (!p) {
1990 ret = -ENOMEM;
1991 goto out;
1992 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001993 if (strlen(p) != 32) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00001994 pr_warn("bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07001995 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001996 goto out;
1997 }
1998
1999 for (i = 0; i < 16; ++i) {
2000 strlcpy(dgid, p + i * 2, 3);
2001 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
2002 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08002003 kfree(p);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07002004 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002005 break;
2006
2007 case SRP_OPT_PKEY:
2008 if (match_hex(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002009 pr_warn("bad P_Key parameter '%s'\n", p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002010 goto out;
2011 }
2012 target->path.pkey = cpu_to_be16(token);
2013 break;
2014
2015 case SRP_OPT_SERVICE_ID:
2016 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002017 if (!p) {
2018 ret = -ENOMEM;
2019 goto out;
2020 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08002021 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
Sean Hefty247e0202007-08-08 15:51:18 -07002022 target->path.service_id = target->service_id;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002023 kfree(p);
2024 break;
2025
2026 case SRP_OPT_MAX_SECT:
2027 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002028 pr_warn("bad max sect parameter '%s'\n", p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002029 goto out;
2030 }
2031 target->scsi_host->max_sectors = token;
2032 break;
2033
Vu Pham52fb2b502006-06-17 20:37:31 -07002034 case SRP_OPT_MAX_CMD_PER_LUN:
2035 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002036 pr_warn("bad max cmd_per_lun parameter '%s'\n",
2037 p);
Vu Pham52fb2b502006-06-17 20:37:31 -07002038 goto out;
2039 }
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00002040 target->scsi_host->cmd_per_lun = min(token, SRP_CMD_SQ_SIZE);
Vu Pham52fb2b502006-06-17 20:37:31 -07002041 break;
2042
Ramachandra K0c0450db2006-06-17 20:37:38 -07002043 case SRP_OPT_IO_CLASS:
2044 if (match_hex(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002045 pr_warn("bad IO class parameter '%s'\n", p);
Ramachandra K0c0450db2006-06-17 20:37:38 -07002046 goto out;
2047 }
2048 if (token != SRP_REV10_IB_IO_CLASS &&
2049 token != SRP_REV16A_IB_IO_CLASS) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002050 pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n",
2051 token, SRP_REV10_IB_IO_CLASS,
2052 SRP_REV16A_IB_IO_CLASS);
Ramachandra K0c0450db2006-06-17 20:37:38 -07002053 goto out;
2054 }
2055 target->io_class = token;
2056 break;
2057
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002058 case SRP_OPT_INITIATOR_EXT:
2059 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02002060 if (!p) {
2061 ret = -ENOMEM;
2062 goto out;
2063 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02002064 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2065 kfree(p);
2066 break;
2067
David Dillow49248642011-01-14 18:23:24 -05002068 case SRP_OPT_CMD_SG_ENTRIES:
2069 if (match_int(args, &token) || token < 1 || token > 255) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002070 pr_warn("bad max cmd_sg_entries parameter '%s'\n",
2071 p);
David Dillow49248642011-01-14 18:23:24 -05002072 goto out;
2073 }
2074 target->cmd_sg_cnt = token;
2075 break;
2076
David Dillowc07d4242011-01-16 13:57:10 -05002077 case SRP_OPT_ALLOW_EXT_SG:
2078 if (match_int(args, &token)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002079 pr_warn("bad allow_ext_sg parameter '%s'\n", p);
David Dillowc07d4242011-01-16 13:57:10 -05002080 goto out;
2081 }
2082 target->allow_ext_sg = !!token;
2083 break;
2084
2085 case SRP_OPT_SG_TABLESIZE:
2086 if (match_int(args, &token) || token < 1 ||
2087 token > SCSI_MAX_SG_CHAIN_SEGMENTS) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002088 pr_warn("bad max sg_tablesize parameter '%s'\n",
2089 p);
David Dillowc07d4242011-01-16 13:57:10 -05002090 goto out;
2091 }
2092 target->sg_tablesize = token;
2093 break;
2094
Roland Dreieraef9ec32005-11-02 14:07:13 -08002095 default:
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002096 pr_warn("unknown parameter or missing value '%s' in target creation request\n",
2097 p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002098 goto out;
2099 }
2100 }
2101
2102 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
2103 ret = 0;
2104 else
2105 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
2106 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
2107 !(srp_opt_tokens[i].token & opt_mask))
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002108 pr_warn("target creation request is missing parameter '%s'\n",
2109 srp_opt_tokens[i].pattern);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002110
2111out:
2112 kfree(options);
2113 return ret;
2114}
2115
Tony Jonesee959b02008-02-22 00:13:36 +01002116static ssize_t srp_create_target(struct device *dev,
2117 struct device_attribute *attr,
Roland Dreieraef9ec32005-11-02 14:07:13 -08002118 const char *buf, size_t count)
2119{
2120 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01002121 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002122 struct Scsi_Host *target_host;
2123 struct srp_target_port *target;
David Dillowc07d4242011-01-16 13:57:10 -05002124 struct ib_device *ibdev = host->srp_dev->dev;
2125 dma_addr_t dma_addr;
David Dillow8f26c9f2011-01-14 19:45:50 -05002126 int i, ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002127
2128 target_host = scsi_host_alloc(&srp_template,
2129 sizeof (struct srp_target_port));
2130 if (!target_host)
2131 return -ENOMEM;
2132
David Dillow49248642011-01-14 18:23:24 -05002133 target_host->transportt = ib_srp_transport_template;
Bart Van Asschefd1b6c42011-07-13 09:19:16 -07002134 target_host->max_channel = 0;
2135 target_host->max_id = 1;
Arne Redlich3c8edf02006-11-15 12:43:00 +01002136 target_host->max_lun = SRP_MAX_LUN;
2137 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08002138
Roland Dreieraef9ec32005-11-02 14:07:13 -08002139 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002140
David Dillow49248642011-01-14 18:23:24 -05002141 target->io_class = SRP_REV16A_IB_IO_CLASS;
2142 target->scsi_host = target_host;
2143 target->srp_host = host;
2144 target->lkey = host->srp_dev->mr->lkey;
2145 target->rkey = host->srp_dev->mr->rkey;
2146 target->cmd_sg_cnt = cmd_sg_entries;
David Dillowc07d4242011-01-16 13:57:10 -05002147 target->sg_tablesize = indirect_sg_entries ? : cmd_sg_entries;
2148 target->allow_ext_sg = allow_ext_sg;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002149
Roland Dreieraef9ec32005-11-02 14:07:13 -08002150 ret = srp_parse_options(buf, target);
2151 if (ret)
2152 goto err;
2153
David Dillowc07d4242011-01-16 13:57:10 -05002154 if (!host->srp_dev->fmr_pool && !target->allow_ext_sg &&
2155 target->cmd_sg_cnt < target->sg_tablesize) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002156 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 -05002157 target->sg_tablesize = target->cmd_sg_cnt;
2158 }
2159
2160 target_host->sg_tablesize = target->sg_tablesize;
2161 target->indirect_size = target->sg_tablesize *
2162 sizeof (struct srp_direct_buf);
David Dillow49248642011-01-14 18:23:24 -05002163 target->max_iu_len = sizeof (struct srp_cmd) +
2164 sizeof (struct srp_indirect_buf) +
2165 target->cmd_sg_cnt * sizeof (struct srp_direct_buf);
2166
David Dillow8f26c9f2011-01-14 19:45:50 -05002167 spin_lock_init(&target->lock);
2168 INIT_LIST_HEAD(&target->free_tx);
2169 INIT_LIST_HEAD(&target->free_reqs);
2170 for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
2171 struct srp_request *req = &target->req_ring[i];
2172
2173 req->fmr_list = kmalloc(target->cmd_sg_cnt * sizeof (void *),
2174 GFP_KERNEL);
2175 req->map_page = kmalloc(SRP_FMR_SIZE * sizeof (void *),
2176 GFP_KERNEL);
David Dillowc07d4242011-01-16 13:57:10 -05002177 req->indirect_desc = kmalloc(target->indirect_size, GFP_KERNEL);
2178 if (!req->fmr_list || !req->map_page || !req->indirect_desc)
David Dillow8f26c9f2011-01-14 19:45:50 -05002179 goto err_free_mem;
2180
David Dillowc07d4242011-01-16 13:57:10 -05002181 dma_addr = ib_dma_map_single(ibdev, req->indirect_desc,
2182 target->indirect_size,
2183 DMA_TO_DEVICE);
2184 if (ib_dma_mapping_error(ibdev, dma_addr))
2185 goto err_free_mem;
2186
2187 req->indirect_dma_addr = dma_addr;
David Dillow8f26c9f2011-01-14 19:45:50 -05002188 req->index = i;
2189 list_add_tail(&req->list, &target->free_reqs);
2190 }
2191
David Dillowc07d4242011-01-16 13:57:10 -05002192 ib_query_gid(ibdev, host->port, 0, &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002193
David Dillow7aa54bd2008-01-07 18:23:41 -05002194 shost_printk(KERN_DEBUG, target->scsi_host, PFX
2195 "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002196 "service_id %016llx dgid %pI6\n",
Roland Dreieraef9ec32005-11-02 14:07:13 -08002197 (unsigned long long) be64_to_cpu(target->id_ext),
2198 (unsigned long long) be64_to_cpu(target->ioc_guid),
2199 be16_to_cpu(target->path.pkey),
2200 (unsigned long long) be64_to_cpu(target->service_id),
Harvey Harrison8867cd72008-10-28 22:36:33 -07002201 target->path.dgid.raw);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002202
2203 ret = srp_create_target_ib(target);
2204 if (ret)
David Dillow8f26c9f2011-01-14 19:45:50 -05002205 goto err_free_mem;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002206
David Dillow9fe4bcf2008-01-08 17:08:52 -05002207 ret = srp_new_cm_id(target);
2208 if (ret)
David Dillow8f26c9f2011-01-14 19:45:50 -05002209 goto err_free_ib;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002210
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02002211 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002212 ret = srp_connect_target(target);
2213 if (ret) {
David Dillow7aa54bd2008-01-07 18:23:41 -05002214 shost_printk(KERN_ERR, target->scsi_host,
2215 PFX "Connection failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002216 goto err_cm_id;
2217 }
2218
2219 ret = srp_add_target(host, target);
2220 if (ret)
2221 goto err_disconnect;
2222
2223 return count;
2224
2225err_disconnect:
2226 srp_disconnect_target(target);
2227
2228err_cm_id:
2229 ib_destroy_cm_id(target->cm_id);
2230
David Dillow8f26c9f2011-01-14 19:45:50 -05002231err_free_ib:
Roland Dreieraef9ec32005-11-02 14:07:13 -08002232 srp_free_target_ib(target);
2233
David Dillow8f26c9f2011-01-14 19:45:50 -05002234err_free_mem:
2235 srp_free_req_data(target);
2236
Roland Dreieraef9ec32005-11-02 14:07:13 -08002237err:
2238 scsi_host_put(target_host);
2239
2240 return ret;
2241}
2242
Tony Jonesee959b02008-02-22 00:13:36 +01002243static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002244
Tony Jonesee959b02008-02-22 00:13:36 +01002245static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
2246 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002247{
Tony Jonesee959b02008-02-22 00:13:36 +01002248 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002249
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002250 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002251}
2252
Tony Jonesee959b02008-02-22 00:13:36 +01002253static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002254
Tony Jonesee959b02008-02-22 00:13:36 +01002255static ssize_t show_port(struct device *dev, struct device_attribute *attr,
2256 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002257{
Tony Jonesee959b02008-02-22 00:13:36 +01002258 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002259
2260 return sprintf(buf, "%d\n", host->port);
2261}
2262
Tony Jonesee959b02008-02-22 00:13:36 +01002263static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002264
Roland Dreierf5358a12006-06-17 20:37:29 -07002265static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002266{
2267 struct srp_host *host;
2268
2269 host = kzalloc(sizeof *host, GFP_KERNEL);
2270 if (!host)
2271 return NULL;
2272
2273 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002274 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002275 init_completion(&host->released);
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002276 host->srp_dev = device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002277 host->port = port;
2278
Tony Jonesee959b02008-02-22 00:13:36 +01002279 host->dev.class = &srp_class;
2280 host->dev.parent = device->dev->dma_device;
Kay Sieversd927e382009-01-06 10:44:39 -08002281 dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002282
Tony Jonesee959b02008-02-22 00:13:36 +01002283 if (device_register(&host->dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07002284 goto free_host;
Tony Jonesee959b02008-02-22 00:13:36 +01002285 if (device_create_file(&host->dev, &dev_attr_add_target))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002286 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01002287 if (device_create_file(&host->dev, &dev_attr_ibdev))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002288 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01002289 if (device_create_file(&host->dev, &dev_attr_port))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002290 goto err_class;
2291
2292 return host;
2293
2294err_class:
Tony Jonesee959b02008-02-22 00:13:36 +01002295 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002296
Roland Dreierf5358a12006-06-17 20:37:29 -07002297free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08002298 kfree(host);
2299
2300 return NULL;
2301}
2302
2303static void srp_add_one(struct ib_device *device)
2304{
Roland Dreierf5358a12006-06-17 20:37:29 -07002305 struct srp_device *srp_dev;
2306 struct ib_device_attr *dev_attr;
2307 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002308 struct srp_host *host;
David Dillowbe8b9812011-01-18 21:58:09 -05002309 int max_pages_per_fmr, fmr_page_shift, s, e, p;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002310
Roland Dreierf5358a12006-06-17 20:37:29 -07002311 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
2312 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08002313 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002314
Roland Dreierf5358a12006-06-17 20:37:29 -07002315 if (ib_query_device(device, dev_attr)) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002316 pr_warn("Query device failed for %s\n", device->name);
Roland Dreierf5358a12006-06-17 20:37:29 -07002317 goto free_attr;
2318 }
2319
2320 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
2321 if (!srp_dev)
2322 goto free_attr;
2323
2324 /*
2325 * Use the smallest page size supported by the HCA, down to a
David Dillow8f26c9f2011-01-14 19:45:50 -05002326 * minimum of 4096 bytes. We're unlikely to build large sglists
2327 * out of smaller entries.
Roland Dreierf5358a12006-06-17 20:37:29 -07002328 */
David Dillow8f26c9f2011-01-14 19:45:50 -05002329 fmr_page_shift = max(12, ffs(dev_attr->page_size_cap) - 1);
2330 srp_dev->fmr_page_size = 1 << fmr_page_shift;
2331 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
2332 srp_dev->fmr_max_size = srp_dev->fmr_page_size * SRP_FMR_SIZE;
Roland Dreierf5358a12006-06-17 20:37:29 -07002333
2334 INIT_LIST_HEAD(&srp_dev->dev_list);
2335
2336 srp_dev->dev = device;
2337 srp_dev->pd = ib_alloc_pd(device);
2338 if (IS_ERR(srp_dev->pd))
2339 goto free_dev;
2340
2341 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
2342 IB_ACCESS_LOCAL_WRITE |
2343 IB_ACCESS_REMOTE_READ |
2344 IB_ACCESS_REMOTE_WRITE);
2345 if (IS_ERR(srp_dev->mr))
2346 goto err_pd;
2347
David Dillowbe8b9812011-01-18 21:58:09 -05002348 for (max_pages_per_fmr = SRP_FMR_SIZE;
2349 max_pages_per_fmr >= SRP_FMR_MIN_SIZE;
2350 max_pages_per_fmr /= 2, srp_dev->fmr_max_size /= 2) {
2351 memset(&fmr_param, 0, sizeof fmr_param);
2352 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
2353 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
2354 fmr_param.cache = 1;
2355 fmr_param.max_pages_per_fmr = max_pages_per_fmr;
2356 fmr_param.page_shift = fmr_page_shift;
2357 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
2358 IB_ACCESS_REMOTE_WRITE |
2359 IB_ACCESS_REMOTE_READ);
Roland Dreierf5358a12006-06-17 20:37:29 -07002360
David Dillowbe8b9812011-01-18 21:58:09 -05002361 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
2362 if (!IS_ERR(srp_dev->fmr_pool))
2363 break;
2364 }
2365
Roland Dreierf5358a12006-06-17 20:37:29 -07002366 if (IS_ERR(srp_dev->fmr_pool))
2367 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002368
Tom Tucker07ebafb2006-08-03 16:02:42 -05002369 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002370 s = 0;
2371 e = 0;
2372 } else {
2373 s = 1;
2374 e = device->phys_port_cnt;
2375 }
2376
2377 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07002378 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002379 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07002380 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002381 }
2382
Roland Dreierf5358a12006-06-17 20:37:29 -07002383 ib_set_client_data(device, &srp_client, srp_dev);
2384
2385 goto free_attr;
2386
2387err_pd:
2388 ib_dealloc_pd(srp_dev->pd);
2389
2390free_dev:
2391 kfree(srp_dev);
2392
2393free_attr:
2394 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002395}
2396
2397static void srp_remove_one(struct ib_device *device)
2398{
Roland Dreierf5358a12006-06-17 20:37:29 -07002399 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002400 struct srp_host *host, *tmp_host;
2401 LIST_HEAD(target_list);
2402 struct srp_target_port *target, *tmp_target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002403
Roland Dreierf5358a12006-06-17 20:37:29 -07002404 srp_dev = ib_get_client_data(device, &srp_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002405
Roland Dreierf5358a12006-06-17 20:37:29 -07002406 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Tony Jonesee959b02008-02-22 00:13:36 +01002407 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002408 /*
2409 * Wait for the sysfs entry to go away, so that no new
2410 * target ports can be created.
2411 */
2412 wait_for_completion(&host->released);
2413
2414 /*
2415 * Mark all target ports as removed, so we stop queueing
2416 * commands and don't try to reconnect.
2417 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002418 spin_lock(&host->target_lock);
Matthew Wilcox549c5fc22006-06-17 20:37:30 -07002419 list_for_each_entry(target, &host->target_list, list) {
Bart Van Asschee9684672010-11-26 15:08:38 -05002420 spin_lock_irq(&target->lock);
Ishai Rabinovitz0c5b3952006-06-17 20:37:31 -07002421 target->state = SRP_TARGET_REMOVED;
Bart Van Asschee9684672010-11-26 15:08:38 -05002422 spin_unlock_irq(&target->lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002423 }
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002424 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002425
2426 /*
2427 * Wait for any reconnection tasks that may have
2428 * started before we marked our target ports as
2429 * removed, and any target port removal tasks.
2430 */
Tejun Heof0626712010-10-19 15:24:36 +00002431 flush_workqueue(ib_wq);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002432
2433 list_for_each_entry_safe(target, tmp_target,
2434 &host->target_list, list) {
David Dillowb0e47c82008-01-03 10:25:27 -08002435 srp_remove_host(target->scsi_host);
Dave Dillowad696982008-01-03 22:35:41 -05002436 scsi_remove_host(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002437 srp_disconnect_target(target);
2438 ib_destroy_cm_id(target->cm_id);
2439 srp_free_target_ib(target);
David Dillow8f26c9f2011-01-14 19:45:50 -05002440 srp_free_req_data(target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002441 scsi_host_put(target->scsi_host);
2442 }
2443
Roland Dreieraef9ec32005-11-02 14:07:13 -08002444 kfree(host);
2445 }
2446
Roland Dreierf5358a12006-06-17 20:37:29 -07002447 if (srp_dev->fmr_pool)
2448 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2449 ib_dereg_mr(srp_dev->mr);
2450 ib_dealloc_pd(srp_dev->pd);
2451
2452 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002453}
2454
FUJITA Tomonori32368222007-06-27 16:33:12 +09002455static struct srp_function_template ib_srp_transport_functions = {
2456};
2457
Roland Dreieraef9ec32005-11-02 14:07:13 -08002458static int __init srp_init_module(void)
2459{
2460 int ret;
2461
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05002462 BUILD_BUG_ON(FIELD_SIZEOF(struct ib_wc, wr_id) < sizeof(void *));
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00002463
David Dillow49248642011-01-14 18:23:24 -05002464 if (srp_sg_tablesize) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002465 pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n");
David Dillow49248642011-01-14 18:23:24 -05002466 if (!cmd_sg_entries)
2467 cmd_sg_entries = srp_sg_tablesize;
2468 }
2469
2470 if (!cmd_sg_entries)
2471 cmd_sg_entries = SRP_DEF_SG_TABLESIZE;
2472
2473 if (cmd_sg_entries > 255) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002474 pr_warn("Clamping cmd_sg_entries to 255\n");
David Dillow49248642011-01-14 18:23:24 -05002475 cmd_sg_entries = 255;
David Dillow1e89a192008-04-16 21:01:12 -07002476 }
2477
David Dillowc07d4242011-01-16 13:57:10 -05002478 if (!indirect_sg_entries)
2479 indirect_sg_entries = cmd_sg_entries;
2480 else if (indirect_sg_entries < cmd_sg_entries) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002481 pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n",
2482 cmd_sg_entries);
David Dillowc07d4242011-01-16 13:57:10 -05002483 indirect_sg_entries = cmd_sg_entries;
2484 }
2485
FUJITA Tomonori32368222007-06-27 16:33:12 +09002486 ib_srp_transport_template =
2487 srp_attach_transport(&ib_srp_transport_functions);
2488 if (!ib_srp_transport_template)
2489 return -ENOMEM;
2490
Roland Dreieraef9ec32005-11-02 14:07:13 -08002491 ret = class_register(&srp_class);
2492 if (ret) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002493 pr_err("couldn't register class infiniband_srp\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002494 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002495 return ret;
2496 }
2497
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002498 ib_sa_register_client(&srp_sa_client);
2499
Roland Dreieraef9ec32005-11-02 14:07:13 -08002500 ret = ib_register_client(&srp_client);
2501 if (ret) {
Bart Van Asschee0bda7d2012-01-14 12:39:44 +00002502 pr_err("couldn't register IB client\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002503 srp_release_transport(ib_srp_transport_template);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002504 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002505 class_unregister(&srp_class);
2506 return ret;
2507 }
2508
2509 return 0;
2510}
2511
2512static void __exit srp_cleanup_module(void)
2513{
2514 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002515 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002516 class_unregister(&srp_class);
FUJITA Tomonori32368222007-06-27 16:33:12 +09002517 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002518}
2519
2520module_init(srp_init_module);
2521module_exit(srp_cleanup_module);