blob: a54eee9324b61833b13164b93a8696bf2689c8ea [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
Roland Dreieraef9ec32005-11-02 14:07:13 -080033#include <linux/module.h>
34#include <linux/init.h>
35#include <linux/slab.h>
36#include <linux/err.h>
37#include <linux/string.h>
38#include <linux/parser.h>
39#include <linux/random.h>
Tim Schmielaude259682006-01-08 01:02:05 -080040#include <linux/jiffies.h>
Roland Dreieraef9ec32005-11-02 14:07:13 -080041
42#include <asm/atomic.h>
43
44#include <scsi/scsi.h>
45#include <scsi/scsi_device.h>
46#include <scsi/scsi_dbg.h>
47#include <scsi/srp.h>
FUJITA Tomonori32368222007-06-27 16:33:12 +090048#include <scsi/scsi_transport_srp.h>
Roland Dreieraef9ec32005-11-02 14:07:13 -080049
Roland Dreieraef9ec32005-11-02 14:07:13 -080050#include "ib_srp.h"
51
52#define DRV_NAME "ib_srp"
53#define PFX DRV_NAME ": "
54#define DRV_VERSION "0.2"
55#define DRV_RELDATE "November 1, 2005"
56
57MODULE_AUTHOR("Roland Dreier");
58MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
59 "v" DRV_VERSION " (" DRV_RELDATE ")");
60MODULE_LICENSE("Dual BSD/GPL");
61
Vu Pham74b0a152006-06-17 20:37:32 -070062static int srp_sg_tablesize = SRP_DEF_SG_TABLESIZE;
63static int srp_max_iu_len;
64
65module_param(srp_sg_tablesize, int, 0444);
66MODULE_PARM_DESC(srp_sg_tablesize,
David Dillow1e89a192008-04-16 21:01:12 -070067 "Max number of gather/scatter entries per I/O (default is 12, max 255)");
Vu Pham74b0a152006-06-17 20:37:32 -070068
Roland Dreieraef9ec32005-11-02 14:07:13 -080069static int topspin_workarounds = 1;
70
71module_param(topspin_workarounds, int, 0444);
72MODULE_PARM_DESC(topspin_workarounds,
73 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
74
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -070075static int mellanox_workarounds = 1;
76
77module_param(mellanox_workarounds, int, 0444);
78MODULE_PARM_DESC(mellanox_workarounds,
79 "Enable workarounds for Mellanox SRP target bugs if != 0");
80
Roland Dreieraef9ec32005-11-02 14:07:13 -080081static void srp_add_one(struct ib_device *device);
82static void srp_remove_one(struct ib_device *device);
Bart Van Assche9c03dc92010-02-02 19:23:54 +000083static void srp_recv_completion(struct ib_cq *cq, void *target_ptr);
84static void srp_send_completion(struct ib_cq *cq, void *target_ptr);
Roland Dreieraef9ec32005-11-02 14:07:13 -080085static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
David Dillowbb125882010-10-08 14:40:47 -040086static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
87 enum srp_iu_type iu_type);
88static int __srp_post_send(struct srp_target_port *target,
89 struct srp_iu *iu, int len);
Roland Dreieraef9ec32005-11-02 14:07:13 -080090
FUJITA Tomonori32368222007-06-27 16:33:12 +090091static struct scsi_transport_template *ib_srp_transport_template;
92
Roland Dreieraef9ec32005-11-02 14:07:13 -080093static struct ib_client srp_client = {
94 .name = "srp",
95 .add = srp_add_one,
96 .remove = srp_remove_one
97};
98
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -070099static struct ib_sa_client srp_sa_client;
100
Roland Dreieraef9ec32005-11-02 14:07:13 -0800101static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
102{
103 return (struct srp_target_port *) host->hostdata;
104}
105
106static const char *srp_target_info(struct Scsi_Host *host)
107{
108 return host_to_target(host)->target_name;
109}
110
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700111static int srp_target_is_topspin(struct srp_target_port *target)
112{
113 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700114 static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700115
116 return topspin_workarounds &&
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700117 (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
118 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700119}
120
121static int srp_target_is_mellanox(struct srp_target_port *target)
122{
123 static const u8 mellanox_oui[3] = { 0x00, 0x02, 0xc9 };
124
125 return mellanox_workarounds &&
126 !memcmp(&target->ioc_guid, mellanox_oui, sizeof mellanox_oui);
127}
128
Roland Dreieraef9ec32005-11-02 14:07:13 -0800129static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
130 gfp_t gfp_mask,
131 enum dma_data_direction direction)
132{
133 struct srp_iu *iu;
134
135 iu = kmalloc(sizeof *iu, gfp_mask);
136 if (!iu)
137 goto out;
138
139 iu->buf = kzalloc(size, gfp_mask);
140 if (!iu->buf)
141 goto out_free_iu;
142
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100143 iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
144 direction);
145 if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800146 goto out_free_buf;
147
148 iu->size = size;
149 iu->direction = direction;
150
151 return iu;
152
153out_free_buf:
154 kfree(iu->buf);
155out_free_iu:
156 kfree(iu);
157out:
158 return NULL;
159}
160
161static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
162{
163 if (!iu)
164 return;
165
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100166 ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
167 iu->direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800168 kfree(iu->buf);
169 kfree(iu);
170}
171
172static void srp_qp_event(struct ib_event *event, void *context)
173{
174 printk(KERN_ERR PFX "QP event %d\n", event->event);
175}
176
177static int srp_init_qp(struct srp_target_port *target,
178 struct ib_qp *qp)
179{
180 struct ib_qp_attr *attr;
181 int ret;
182
183 attr = kmalloc(sizeof *attr, GFP_KERNEL);
184 if (!attr)
185 return -ENOMEM;
186
Roland Dreier969a60f2008-07-14 23:48:43 -0700187 ret = ib_find_pkey(target->srp_host->srp_dev->dev,
188 target->srp_host->port,
189 be16_to_cpu(target->path.pkey),
190 &attr->pkey_index);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800191 if (ret)
192 goto out;
193
194 attr->qp_state = IB_QPS_INIT;
195 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
196 IB_ACCESS_REMOTE_WRITE);
197 attr->port_num = target->srp_host->port;
198
199 ret = ib_modify_qp(qp, attr,
200 IB_QP_STATE |
201 IB_QP_PKEY_INDEX |
202 IB_QP_ACCESS_FLAGS |
203 IB_QP_PORT);
204
205out:
206 kfree(attr);
207 return ret;
208}
209
David Dillow9fe4bcf2008-01-08 17:08:52 -0500210static int srp_new_cm_id(struct srp_target_port *target)
211{
212 struct ib_cm_id *new_cm_id;
213
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100214 new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
David Dillow9fe4bcf2008-01-08 17:08:52 -0500215 srp_cm_handler, target);
216 if (IS_ERR(new_cm_id))
217 return PTR_ERR(new_cm_id);
218
219 if (target->cm_id)
220 ib_destroy_cm_id(target->cm_id);
221 target->cm_id = new_cm_id;
222
223 return 0;
224}
225
Roland Dreieraef9ec32005-11-02 14:07:13 -0800226static int srp_create_target_ib(struct srp_target_port *target)
227{
228 struct ib_qp_init_attr *init_attr;
229 int ret;
230
231 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
232 if (!init_attr)
233 return -ENOMEM;
234
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000235 target->recv_cq = ib_create_cq(target->srp_host->srp_dev->dev,
236 srp_recv_completion, NULL, target, SRP_RQ_SIZE, 0);
237 if (IS_ERR(target->recv_cq)) {
238 ret = PTR_ERR(target->recv_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800239 goto err;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800240 }
241
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000242 target->send_cq = ib_create_cq(target->srp_host->srp_dev->dev,
243 srp_send_completion, NULL, target, SRP_SQ_SIZE, 0);
244 if (IS_ERR(target->send_cq)) {
245 ret = PTR_ERR(target->send_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800246 goto err_recv_cq;
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000247 }
248
249 ib_req_notify_cq(target->recv_cq, IB_CQ_NEXT_COMP);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800250
251 init_attr->event_handler = srp_qp_event;
252 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
253 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
254 init_attr->cap.max_recv_sge = 1;
255 init_attr->cap.max_send_sge = 1;
256 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
257 init_attr->qp_type = IB_QPT_RC;
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000258 init_attr->send_cq = target->send_cq;
259 init_attr->recv_cq = target->recv_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800260
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100261 target->qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800262 if (IS_ERR(target->qp)) {
263 ret = PTR_ERR(target->qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800264 goto err_send_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800265 }
266
267 ret = srp_init_qp(target, target->qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800268 if (ret)
269 goto err_qp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800270
Roland Dreierda9d2f02010-02-24 15:07:59 -0800271 kfree(init_attr);
272 return 0;
273
274err_qp:
275 ib_destroy_qp(target->qp);
276
277err_send_cq:
278 ib_destroy_cq(target->send_cq);
279
280err_recv_cq:
281 ib_destroy_cq(target->recv_cq);
282
283err:
Roland Dreieraef9ec32005-11-02 14:07:13 -0800284 kfree(init_attr);
285 return ret;
286}
287
288static void srp_free_target_ib(struct srp_target_port *target)
289{
290 int i;
291
292 ib_destroy_qp(target->qp);
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000293 ib_destroy_cq(target->send_cq);
294 ib_destroy_cq(target->recv_cq);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800295
296 for (i = 0; i < SRP_RQ_SIZE; ++i)
297 srp_free_iu(target->srp_host, target->rx_ring[i]);
Bart Van Asschedd5e6e32010-08-30 19:27:20 +0000298 for (i = 0; i < SRP_SQ_SIZE; ++i)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800299 srp_free_iu(target->srp_host, target->tx_ring[i]);
300}
301
302static void srp_path_rec_completion(int status,
303 struct ib_sa_path_rec *pathrec,
304 void *target_ptr)
305{
306 struct srp_target_port *target = target_ptr;
307
308 target->status = status;
309 if (status)
David Dillow7aa54bd2008-01-07 18:23:41 -0500310 shost_printk(KERN_ERR, target->scsi_host,
311 PFX "Got failed path rec status %d\n", status);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800312 else
313 target->path = *pathrec;
314 complete(&target->done);
315}
316
317static int srp_lookup_path(struct srp_target_port *target)
318{
319 target->path.numb_path = 1;
320
321 init_completion(&target->done);
322
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700323 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100324 target->srp_host->srp_dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800325 target->srp_host->port,
326 &target->path,
Sean Hefty247e0202007-08-08 15:51:18 -0700327 IB_SA_PATH_REC_SERVICE_ID |
Roland Dreieraef9ec32005-11-02 14:07:13 -0800328 IB_SA_PATH_REC_DGID |
329 IB_SA_PATH_REC_SGID |
330 IB_SA_PATH_REC_NUMB_PATH |
331 IB_SA_PATH_REC_PKEY,
332 SRP_PATH_REC_TIMEOUT_MS,
333 GFP_KERNEL,
334 srp_path_rec_completion,
335 target, &target->path_query);
336 if (target->path_query_id < 0)
337 return target->path_query_id;
338
339 wait_for_completion(&target->done);
340
341 if (target->status < 0)
David Dillow7aa54bd2008-01-07 18:23:41 -0500342 shost_printk(KERN_WARNING, target->scsi_host,
343 PFX "Path record query failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800344
345 return target->status;
346}
347
348static int srp_send_req(struct srp_target_port *target)
349{
350 struct {
351 struct ib_cm_req_param param;
352 struct srp_login_req priv;
353 } *req = NULL;
354 int status;
355
356 req = kzalloc(sizeof *req, GFP_KERNEL);
357 if (!req)
358 return -ENOMEM;
359
360 req->param.primary_path = &target->path;
361 req->param.alternate_path = NULL;
362 req->param.service_id = target->service_id;
363 req->param.qp_num = target->qp->qp_num;
364 req->param.qp_type = target->qp->qp_type;
365 req->param.private_data = &req->priv;
366 req->param.private_data_len = sizeof req->priv;
367 req->param.flow_control = 1;
368
369 get_random_bytes(&req->param.starting_psn, 4);
370 req->param.starting_psn &= 0xffffff;
371
372 /*
373 * Pick some arbitrary defaults here; we could make these
374 * module parameters if anyone cared about setting them.
375 */
376 req->param.responder_resources = 4;
377 req->param.remote_cm_response_timeout = 20;
378 req->param.local_cm_response_timeout = 20;
379 req->param.retry_count = 7;
380 req->param.rnr_retry_count = 7;
381 req->param.max_cm_retries = 15;
382
383 req->priv.opcode = SRP_LOGIN_REQ;
384 req->priv.tag = 0;
Vu Pham74b0a152006-06-17 20:37:32 -0700385 req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800386 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
387 SRP_BUF_FORMAT_INDIRECT);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700388 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700389 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450db2006-06-17 20:37:38 -0700390 * port identifier format is 8 bytes of ID extension followed
391 * by 8 bytes of GUID. Older drafts put the two halves in the
392 * opposite order, so that the GUID comes first.
393 *
394 * Targets conforming to these obsolete drafts can be
395 * recognized by the I/O Class they report.
396 */
397 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
398 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200399 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700400 memcpy(req->priv.initiator_port_id + 8,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200401 &target->initiator_ext, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700402 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
403 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
404 } else {
405 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200406 &target->initiator_ext, 8);
407 memcpy(req->priv.initiator_port_id + 8,
408 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700409 memcpy(req->priv.target_port_id, &target->id_ext, 8);
410 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
411 }
412
Roland Dreieraef9ec32005-11-02 14:07:13 -0800413 /*
414 * Topspin/Cisco SRP targets will reject our login unless we
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200415 * zero out the first 8 bytes of our initiator port ID and set
416 * the second 8 bytes to the local node GUID.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800417 */
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700418 if (srp_target_is_topspin(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500419 shost_printk(KERN_DEBUG, target->scsi_host,
420 PFX "Topspin/Cisco initiator port ID workaround "
421 "activated for target GUID %016llx\n",
422 (unsigned long long) be64_to_cpu(target->ioc_guid));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800423 memset(req->priv.initiator_port_id, 0, 8);
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200424 memcpy(req->priv.initiator_port_id + 8,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100425 &target->srp_host->srp_dev->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800426 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800427
428 status = ib_send_cm_req(target->cm_id, &req->param);
429
430 kfree(req);
431
432 return status;
433}
434
435static void srp_disconnect_target(struct srp_target_port *target)
436{
437 /* XXX should send SRP_I_LOGOUT request */
438
439 init_completion(&target->done);
Roland Dreiere6581052006-05-17 09:13:21 -0700440 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500441 shost_printk(KERN_DEBUG, target->scsi_host,
442 PFX "Sending CM DREQ failed\n");
Roland Dreiere6581052006-05-17 09:13:21 -0700443 return;
444 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800445 wait_for_completion(&target->done);
446}
447
David Howellsc4028952006-11-22 14:57:56 +0000448static void srp_remove_work(struct work_struct *work)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800449{
David Howellsc4028952006-11-22 14:57:56 +0000450 struct srp_target_port *target =
451 container_of(work, struct srp_target_port, work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800452
453 spin_lock_irq(target->scsi_host->host_lock);
454 if (target->state != SRP_TARGET_DEAD) {
455 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800456 return;
457 }
458 target->state = SRP_TARGET_REMOVED;
459 spin_unlock_irq(target->scsi_host->host_lock);
460
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700461 spin_lock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800462 list_del(&target->list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700463 spin_unlock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800464
FUJITA Tomonori32368222007-06-27 16:33:12 +0900465 srp_remove_host(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800466 scsi_remove_host(target->scsi_host);
467 ib_destroy_cm_id(target->cm_id);
468 srp_free_target_ib(target);
469 scsi_host_put(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800470}
471
472static int srp_connect_target(struct srp_target_port *target)
473{
David Dillow9fe4bcf2008-01-08 17:08:52 -0500474 int retries = 3;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800475 int ret;
476
477 ret = srp_lookup_path(target);
478 if (ret)
479 return ret;
480
481 while (1) {
482 init_completion(&target->done);
483 ret = srp_send_req(target);
484 if (ret)
485 return ret;
486 wait_for_completion(&target->done);
487
488 /*
489 * The CM event handling code will set status to
490 * SRP_PORT_REDIRECT if we get a port redirect REJ
491 * back, or SRP_DLID_REDIRECT if we get a lid/qp
492 * redirect REJ back.
493 */
494 switch (target->status) {
495 case 0:
496 return 0;
497
498 case SRP_PORT_REDIRECT:
499 ret = srp_lookup_path(target);
500 if (ret)
501 return ret;
502 break;
503
504 case SRP_DLID_REDIRECT:
505 break;
506
David Dillow9fe4bcf2008-01-08 17:08:52 -0500507 case SRP_STALE_CONN:
508 /* Our current CM id was stale, and is now in timewait.
509 * Try to reconnect with a new one.
510 */
511 if (!retries-- || srp_new_cm_id(target)) {
512 shost_printk(KERN_ERR, target->scsi_host, PFX
513 "giving up on stale connection\n");
514 target->status = -ECONNRESET;
515 return target->status;
516 }
517
518 shost_printk(KERN_ERR, target->scsi_host, PFX
519 "retrying stale connection\n");
520 break;
521
Roland Dreieraef9ec32005-11-02 14:07:13 -0800522 default:
523 return target->status;
524 }
525 }
526}
527
Roland Dreierd945e1d2006-05-09 10:50:28 -0700528static void srp_unmap_data(struct scsi_cmnd *scmnd,
529 struct srp_target_port *target,
530 struct srp_request *req)
531{
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900532 if (!scsi_sglist(scmnd) ||
Roland Dreierd945e1d2006-05-09 10:50:28 -0700533 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
534 scmnd->sc_data_direction != DMA_FROM_DEVICE))
535 return;
536
Roland Dreierf5358a12006-06-17 20:37:29 -0700537 if (req->fmr) {
538 ib_fmr_pool_unmap(req->fmr);
539 req->fmr = NULL;
540 }
541
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100542 ib_dma_unmap_sg(target->srp_host->srp_dev->dev, scsi_sglist(scmnd),
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900543 scsi_sg_count(scmnd), scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700544}
545
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700546static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
547{
548 srp_unmap_data(req->scmnd, target, req);
549 list_move_tail(&req->list, &target->free_reqs);
550}
551
552static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
553{
554 req->scmnd->result = DID_RESET << 16;
555 req->scmnd->scsi_done(req->scmnd);
556 srp_remove_req(target, req);
557}
558
Roland Dreieraef9ec32005-11-02 14:07:13 -0800559static int srp_reconnect_target(struct srp_target_port *target)
560{
Roland Dreieraef9ec32005-11-02 14:07:13 -0800561 struct ib_qp_attr qp_attr;
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700562 struct srp_request *req, *tmp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800563 struct ib_wc wc;
564 int ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800565
566 spin_lock_irq(target->scsi_host->host_lock);
567 if (target->state != SRP_TARGET_LIVE) {
568 spin_unlock_irq(target->scsi_host->host_lock);
569 return -EAGAIN;
570 }
571 target->state = SRP_TARGET_CONNECTING;
572 spin_unlock_irq(target->scsi_host->host_lock);
573
574 srp_disconnect_target(target);
575 /*
576 * Now get a new local CM ID so that we avoid confusing the
577 * target in case things are really fouled up.
578 */
David Dillow9fe4bcf2008-01-08 17:08:52 -0500579 ret = srp_new_cm_id(target);
580 if (ret)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800581 goto err;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800582
583 qp_attr.qp_state = IB_QPS_RESET;
584 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
585 if (ret)
586 goto err;
587
588 ret = srp_init_qp(target, target->qp);
589 if (ret)
590 goto err;
591
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000592 while (ib_poll_cq(target->recv_cq, 1, &wc) > 0)
593 ; /* nothing */
594 while (ib_poll_cq(target->send_cq, 1, &wc) > 0)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800595 ; /* nothing */
596
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300597 spin_lock_irq(target->scsi_host->host_lock);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700598 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
599 srp_reset_req(target, req);
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300600 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800601
602 target->rx_head = 0;
603 target->tx_head = 0;
604 target->tx_tail = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800605
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200606 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800607 ret = srp_connect_target(target);
608 if (ret)
609 goto err;
610
611 spin_lock_irq(target->scsi_host->host_lock);
612 if (target->state == SRP_TARGET_CONNECTING) {
613 ret = 0;
614 target->state = SRP_TARGET_LIVE;
615 } else
616 ret = -EAGAIN;
617 spin_unlock_irq(target->scsi_host->host_lock);
618
619 return ret;
620
621err:
David Dillow7aa54bd2008-01-07 18:23:41 -0500622 shost_printk(KERN_ERR, target->scsi_host,
623 PFX "reconnect failed (%d), removing target port.\n", ret);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800624
625 /*
626 * We couldn't reconnect, so kill our target port off.
627 * However, we have to defer the real removal because we might
628 * be in the context of the SCSI error handler now, which
629 * would deadlock if we call scsi_remove_host().
630 */
631 spin_lock_irq(target->scsi_host->host_lock);
632 if (target->state == SRP_TARGET_CONNECTING) {
633 target->state = SRP_TARGET_DEAD;
David Howellsc4028952006-11-22 14:57:56 +0000634 INIT_WORK(&target->work, srp_remove_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800635 schedule_work(&target->work);
636 }
637 spin_unlock_irq(target->scsi_host->host_lock);
638
639 return ret;
640}
641
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700642static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat,
Roland Dreierf5358a12006-06-17 20:37:29 -0700643 int sg_cnt, struct srp_request *req,
644 struct srp_direct_buf *buf)
645{
646 u64 io_addr = 0;
647 u64 *dma_pages;
648 u32 len;
649 int page_cnt;
650 int i, j;
651 int ret;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100652 struct srp_device *dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800653 struct ib_device *ibdev = dev->dev;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900654 struct scatterlist *sg;
Roland Dreierf5358a12006-06-17 20:37:29 -0700655
656 if (!dev->fmr_pool)
657 return -ENODEV;
658
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700659 if (srp_target_is_mellanox(target) &&
660 (ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask))
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700661 return -EINVAL;
662
Roland Dreierf5358a12006-06-17 20:37:29 -0700663 len = page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900664 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
665 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800666
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900667 if (ib_sg_dma_address(ibdev, sg) & ~dev->fmr_page_mask) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700668 if (i > 0)
669 return -EINVAL;
670 else
671 ++page_cnt;
672 }
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900673 if ((ib_sg_dma_address(ibdev, sg) + dma_len) &
Roland Dreierf5358a12006-06-17 20:37:29 -0700674 ~dev->fmr_page_mask) {
675 if (i < sg_cnt - 1)
676 return -EINVAL;
677 else
678 ++page_cnt;
679 }
680
Ralph Campbell85507bc2006-12-12 14:30:55 -0800681 len += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700682 }
683
684 page_cnt += len >> dev->fmr_page_shift;
685 if (page_cnt > SRP_FMR_SIZE)
686 return -ENOMEM;
687
688 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
689 if (!dma_pages)
690 return -ENOMEM;
691
692 page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900693 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
694 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800695
696 for (j = 0; j < dma_len; j += dev->fmr_page_size)
Roland Dreierf5358a12006-06-17 20:37:29 -0700697 dma_pages[page_cnt++] =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900698 (ib_sg_dma_address(ibdev, sg) &
Ralph Campbell85507bc2006-12-12 14:30:55 -0800699 dev->fmr_page_mask) + j;
700 }
Roland Dreierf5358a12006-06-17 20:37:29 -0700701
702 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
Michael S. Tsirkinadfaa882006-07-14 00:23:55 -0700703 dma_pages, page_cnt, io_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -0700704 if (IS_ERR(req->fmr)) {
705 ret = PTR_ERR(req->fmr);
Vu Pham6583eb32006-07-14 00:23:53 -0700706 req->fmr = NULL;
Roland Dreierf5358a12006-06-17 20:37:29 -0700707 goto out;
708 }
709
Ralph Campbell85507bc2006-12-12 14:30:55 -0800710 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) &
711 ~dev->fmr_page_mask);
Roland Dreierf5358a12006-06-17 20:37:29 -0700712 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
713 buf->len = cpu_to_be32(len);
714
715 ret = 0;
716
717out:
718 kfree(dma_pages);
719
720 return ret;
721}
722
Roland Dreieraef9ec32005-11-02 14:07:13 -0800723static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
724 struct srp_request *req)
725{
Roland Dreiercf368712006-03-24 15:47:26 -0800726 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800727 struct srp_cmd *cmd = req->cmd->buf;
Roland Dreiercf368712006-03-24 15:47:26 -0800728 int len, nents, count;
Roland Dreierf5358a12006-06-17 20:37:29 -0700729 u8 fmt = SRP_DATA_DESC_DIRECT;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800730 struct srp_device *dev;
731 struct ib_device *ibdev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800732
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900733 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800734 return sizeof (struct srp_cmd);
735
736 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
737 scmnd->sc_data_direction != DMA_TO_DEVICE) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500738 shost_printk(KERN_WARNING, target->scsi_host,
739 PFX "Unhandled data direction %d\n",
740 scmnd->sc_data_direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800741 return -EINVAL;
742 }
743
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900744 nents = scsi_sg_count(scmnd);
745 scat = scsi_sglist(scmnd);
Roland Dreiercf368712006-03-24 15:47:26 -0800746
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100747 dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800748 ibdev = dev->dev;
749
750 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
Roland Dreierf5358a12006-06-17 20:37:29 -0700751
752 fmt = SRP_DATA_DESC_DIRECT;
753 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800754
755 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700756 /*
757 * The midlayer only generated a single gather/scatter
758 * entry, or DMA mapping coalesced everything to a
759 * single entry. So a direct descriptor along with
760 * the DMA MR suffices.
761 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800762 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800763
Ralph Campbell85507bc2006-12-12 14:30:55 -0800764 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
765 buf->key = cpu_to_be32(dev->mr->rkey);
766 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700767 } else if (srp_map_fmr(target, scat, count, req,
Roland Dreierf5358a12006-06-17 20:37:29 -0700768 (void *) cmd->add_data)) {
769 /*
770 * FMR mapping failed, and the scatterlist has more
771 * than one entry. Generate an indirect memory
772 * descriptor.
773 */
Roland Dreiercf368712006-03-24 15:47:26 -0800774 struct srp_indirect_buf *buf = (void *) cmd->add_data;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900775 struct scatterlist *sg;
Roland Dreiercf368712006-03-24 15:47:26 -0800776 u32 datalen = 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700777 int i;
Roland Dreiercf368712006-03-24 15:47:26 -0800778
779 fmt = SRP_DATA_DESC_INDIRECT;
Roland Dreierf5358a12006-06-17 20:37:29 -0700780 len = sizeof (struct srp_cmd) +
781 sizeof (struct srp_indirect_buf) +
782 count * sizeof (struct srp_direct_buf);
783
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900784 scsi_for_each_sg(scmnd, sg, count, i) {
785 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800786
Roland Dreierf5358a12006-06-17 20:37:29 -0700787 buf->desc_list[i].va =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900788 cpu_to_be64(ib_sg_dma_address(ibdev, sg));
Roland Dreierf5358a12006-06-17 20:37:29 -0700789 buf->desc_list[i].key =
Ralph Campbell85507bc2006-12-12 14:30:55 -0800790 cpu_to_be32(dev->mr->rkey);
791 buf->desc_list[i].len = cpu_to_be32(dma_len);
792 datalen += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700793 }
Roland Dreiercf368712006-03-24 15:47:26 -0800794
795 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
796 cmd->data_out_desc_cnt = count;
797 else
798 cmd->data_in_desc_cnt = count;
799
Roland Dreierf5358a12006-06-17 20:37:29 -0700800 buf->table_desc.va =
801 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800802 buf->table_desc.key =
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100803 cpu_to_be32(target->srp_host->srp_dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800804 buf->table_desc.len =
805 cpu_to_be32(count * sizeof (struct srp_direct_buf));
806
Roland Dreiercf368712006-03-24 15:47:26 -0800807 buf->len = cpu_to_be32(datalen);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800808 }
809
810 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
811 cmd->buf_fmt = fmt << 4;
812 else
813 cmd->buf_fmt = fmt;
814
Roland Dreieraef9ec32005-11-02 14:07:13 -0800815 return len;
816}
817
Bart Van Asschec996bb42010-07-30 10:59:05 +0000818static int srp_post_recv(struct srp_target_port *target)
819{
820 unsigned long flags;
821 struct srp_iu *iu;
822 struct ib_sge list;
823 struct ib_recv_wr wr, *bad_wr;
824 unsigned int next;
825 int ret;
826
827 spin_lock_irqsave(target->scsi_host->host_lock, flags);
828
Bart Van Asschedd5e6e32010-08-30 19:27:20 +0000829 next = target->rx_head & SRP_RQ_MASK;
Bart Van Asschec996bb42010-07-30 10:59:05 +0000830 wr.wr_id = next;
831 iu = target->rx_ring[next];
832
833 list.addr = iu->dma;
834 list.length = iu->size;
835 list.lkey = target->srp_host->srp_dev->mr->lkey;
836
837 wr.next = NULL;
838 wr.sg_list = &list;
839 wr.num_sge = 1;
840
841 ret = ib_post_recv(target->qp, &wr, &bad_wr);
842 if (!ret)
843 ++target->rx_head;
844
845 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
846
847 return ret;
848}
849
Roland Dreieraef9ec32005-11-02 14:07:13 -0800850static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
851{
852 struct srp_request *req;
853 struct scsi_cmnd *scmnd;
854 unsigned long flags;
855 s32 delta;
856
857 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
858
859 spin_lock_irqsave(target->scsi_host->host_lock, flags);
860
861 target->req_lim += delta;
862
863 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
864
865 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
866 if (be32_to_cpu(rsp->resp_data_len) < 4)
867 req->tsk_status = -1;
868 else
869 req->tsk_status = rsp->data[3];
870 complete(&req->done);
871 } else {
Roland Dreierd945e1d2006-05-09 10:50:28 -0700872 scmnd = req->scmnd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800873 if (!scmnd)
David Dillow7aa54bd2008-01-07 18:23:41 -0500874 shost_printk(KERN_ERR, target->scsi_host,
875 "Null scmnd for RSP w/tag %016llx\n",
876 (unsigned long long) rsp->tag);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800877 scmnd->result = rsp->status;
878
879 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
880 memcpy(scmnd->sense_buffer, rsp->data +
881 be32_to_cpu(rsp->resp_data_len),
882 min_t(int, be32_to_cpu(rsp->sense_data_len),
883 SCSI_SENSE_BUFFERSIZE));
884 }
885
886 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900887 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800888 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900889 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800890
Roland Dreieraef9ec32005-11-02 14:07:13 -0800891 if (!req->tsk_mgmt) {
Roland Dreieraef9ec32005-11-02 14:07:13 -0800892 scmnd->host_scribble = (void *) -1L;
893 scmnd->scsi_done(scmnd);
894
Roland Dreierd945e1d2006-05-09 10:50:28 -0700895 srp_remove_req(target, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800896 } else
897 req->cmd_done = 1;
898 }
899
900 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
901}
902
David Dillowbb125882010-10-08 14:40:47 -0400903static int srp_response_common(struct srp_target_port *target, s32 req_delta,
904 void *rsp, int len)
905{
906 struct ib_device *dev;
907 unsigned long flags;
908 struct srp_iu *iu;
909 int err = 1;
910
911 dev = target->srp_host->srp_dev->dev;
912
913 spin_lock_irqsave(target->scsi_host->host_lock, flags);
914 target->req_lim += req_delta;
915
916 iu = __srp_get_tx_iu(target, SRP_IU_RSP);
917 if (!iu) {
918 shost_printk(KERN_ERR, target->scsi_host, PFX
919 "no IU available to send response\n");
920 goto out;
921 }
922
923 ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE);
924 memcpy(iu->buf, rsp, len);
925 ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE);
926
927 err = __srp_post_send(target, iu, len);
928 if (err)
929 shost_printk(KERN_ERR, target->scsi_host, PFX
930 "unable to post response: %d\n", err);
931
932out:
933 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
934 return err;
935}
936
937static void srp_process_cred_req(struct srp_target_port *target,
938 struct srp_cred_req *req)
939{
940 struct srp_cred_rsp rsp = {
941 .opcode = SRP_CRED_RSP,
942 .tag = req->tag,
943 };
944 s32 delta = be32_to_cpu(req->req_lim_delta);
945
946 if (srp_response_common(target, delta, &rsp, sizeof rsp))
947 shost_printk(KERN_ERR, target->scsi_host, PFX
948 "problems processing SRP_CRED_REQ\n");
949}
950
951static void srp_process_aer_req(struct srp_target_port *target,
952 struct srp_aer_req *req)
953{
954 struct srp_aer_rsp rsp = {
955 .opcode = SRP_AER_RSP,
956 .tag = req->tag,
957 };
958 s32 delta = be32_to_cpu(req->req_lim_delta);
959
960 shost_printk(KERN_ERR, target->scsi_host, PFX
961 "ignoring AER for LUN %llu\n", be64_to_cpu(req->lun));
962
963 if (srp_response_common(target, delta, &rsp, sizeof rsp))
964 shost_printk(KERN_ERR, target->scsi_host, PFX
965 "problems processing SRP_AER_REQ\n");
966}
967
Roland Dreieraef9ec32005-11-02 14:07:13 -0800968static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
969{
Ralph Campbell85507bc2006-12-12 14:30:55 -0800970 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800971 struct srp_iu *iu;
Bart Van Asschec996bb42010-07-30 10:59:05 +0000972 int res;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800973 u8 opcode;
974
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000975 iu = target->rx_ring[wc->wr_id];
Roland Dreieraef9ec32005-11-02 14:07:13 -0800976
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100977 dev = target->srp_host->srp_dev->dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800978 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
979 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800980
981 opcode = *(u8 *) iu->buf;
982
983 if (0) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500984 shost_printk(KERN_ERR, target->scsi_host,
985 PFX "recv completion, opcode 0x%02x\n", opcode);
Bart Van Assche7a700812010-07-29 15:56:37 +0000986 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1,
987 iu->buf, wc->byte_len, true);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800988 }
989
990 switch (opcode) {
991 case SRP_RSP:
992 srp_process_rsp(target, iu->buf);
993 break;
994
David Dillowbb125882010-10-08 14:40:47 -0400995 case SRP_CRED_REQ:
996 srp_process_cred_req(target, iu->buf);
997 break;
998
999 case SRP_AER_REQ:
1000 srp_process_aer_req(target, iu->buf);
1001 break;
1002
Roland Dreieraef9ec32005-11-02 14:07:13 -08001003 case SRP_T_LOGOUT:
1004 /* XXX Handle target logout */
David Dillow7aa54bd2008-01-07 18:23:41 -05001005 shost_printk(KERN_WARNING, target->scsi_host,
1006 PFX "Got target logout request\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001007 break;
1008
1009 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001010 shost_printk(KERN_WARNING, target->scsi_host,
1011 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001012 break;
1013 }
1014
Ralph Campbell85507bc2006-12-12 14:30:55 -08001015 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
1016 DMA_FROM_DEVICE);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001017
1018 res = srp_post_recv(target);
1019 if (res != 0)
1020 shost_printk(KERN_ERR, target->scsi_host,
1021 PFX "Recv failed with error code %d\n", res);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001022}
1023
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001024static void srp_recv_completion(struct ib_cq *cq, void *target_ptr)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001025{
1026 struct srp_target_port *target = target_ptr;
1027 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001028
1029 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1030 while (ib_poll_cq(cq, 1, &wc) > 0) {
1031 if (wc.status) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001032 shost_printk(KERN_ERR, target->scsi_host,
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001033 PFX "failed receive status %d\n",
David Dillow7aa54bd2008-01-07 18:23:41 -05001034 wc.status);
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001035 target->qp_in_error = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001036 break;
1037 }
1038
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001039 srp_handle_recv(target, &wc);
1040 }
1041}
1042
1043static void srp_send_completion(struct ib_cq *cq, void *target_ptr)
1044{
1045 struct srp_target_port *target = target_ptr;
1046 struct ib_wc wc;
1047
1048 while (ib_poll_cq(cq, 1, &wc) > 0) {
1049 if (wc.status) {
1050 shost_printk(KERN_ERR, target->scsi_host,
1051 PFX "failed send status %d\n",
1052 wc.status);
1053 target->qp_in_error = 1;
1054 break;
1055 }
1056
1057 ++target->tx_tail;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001058 }
1059}
1060
Roland Dreieraef9ec32005-11-02 14:07:13 -08001061/*
1062 * Must be called with target->scsi_host->host_lock held to protect
Roland Dreier47f2bce2005-11-15 00:19:21 -08001063 * req_lim and tx_head. Lock cannot be dropped between call here and
1064 * call to __srp_post_send().
David Dillowbb125882010-10-08 14:40:47 -04001065 *
1066 * Note:
1067 * An upper limit for the number of allocated information units for each
1068 * request type is:
1069 * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues
1070 * more than Scsi_Host.can_queue requests.
1071 * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE.
1072 * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than
1073 * one unanswered SRP request to an initiator.
Roland Dreieraef9ec32005-11-02 14:07:13 -08001074 */
David Dillow8cba2072007-12-19 17:08:43 -05001075static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
David Dillowbb125882010-10-08 14:40:47 -04001076 enum srp_iu_type iu_type)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001077{
David Dillowbb125882010-10-08 14:40:47 -04001078 s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE;
1079 struct srp_iu *iu;
David Dillow8cba2072007-12-19 17:08:43 -05001080
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001081 srp_send_completion(target->send_cq, target);
1082
Roland Dreieraef9ec32005-11-02 14:07:13 -08001083 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
1084 return NULL;
1085
David Dillowbb125882010-10-08 14:40:47 -04001086 /* Initiator responses to target requests do not consume credits */
1087 if (target->req_lim <= rsv && iu_type != SRP_IU_RSP) {
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001088 ++target->zero_req_lim;
David Dillow8cba2072007-12-19 17:08:43 -05001089 return NULL;
1090 }
Roland Dreier47f2bce2005-11-15 00:19:21 -08001091
David Dillowbb125882010-10-08 14:40:47 -04001092 iu = target->tx_ring[target->tx_head & SRP_SQ_MASK];
1093 iu->type = iu_type;
1094 return iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001095}
1096
1097/*
1098 * Must be called with target->scsi_host->host_lock held to protect
1099 * req_lim and tx_head.
1100 */
1101static int __srp_post_send(struct srp_target_port *target,
1102 struct srp_iu *iu, int len)
1103{
1104 struct ib_sge list;
1105 struct ib_send_wr wr, *bad_wr;
1106 int ret = 0;
1107
Roland Dreieraef9ec32005-11-02 14:07:13 -08001108 list.addr = iu->dma;
1109 list.length = len;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001110 list.lkey = target->srp_host->srp_dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001111
1112 wr.next = NULL;
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001113 wr.wr_id = target->tx_head & SRP_SQ_MASK;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001114 wr.sg_list = &list;
1115 wr.num_sge = 1;
1116 wr.opcode = IB_WR_SEND;
1117 wr.send_flags = IB_SEND_SIGNALED;
1118
1119 ret = ib_post_send(target->qp, &wr, &bad_wr);
1120
1121 if (!ret) {
1122 ++target->tx_head;
David Dillowbb125882010-10-08 14:40:47 -04001123 if (iu->type != SRP_IU_RSP)
1124 --target->req_lim;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001125 }
1126
1127 return ret;
1128}
1129
1130static int srp_queuecommand(struct scsi_cmnd *scmnd,
1131 void (*done)(struct scsi_cmnd *))
1132{
1133 struct srp_target_port *target = host_to_target(scmnd->device->host);
1134 struct srp_request *req;
1135 struct srp_iu *iu;
1136 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001137 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001138 int len;
1139
1140 if (target->state == SRP_TARGET_CONNECTING)
1141 goto err;
1142
1143 if (target->state == SRP_TARGET_DEAD ||
1144 target->state == SRP_TARGET_REMOVED) {
1145 scmnd->result = DID_BAD_TARGET << 16;
1146 done(scmnd);
1147 return 0;
1148 }
1149
David Dillowbb125882010-10-08 14:40:47 -04001150 iu = __srp_get_tx_iu(target, SRP_IU_CMD);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001151 if (!iu)
1152 goto err;
1153
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001154 dev = target->srp_host->srp_dev->dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001155 ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len,
1156 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001157
Roland Dreierd945e1d2006-05-09 10:50:28 -07001158 req = list_entry(target->free_reqs.next, struct srp_request, list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001159
1160 scmnd->scsi_done = done;
1161 scmnd->result = 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001162 scmnd->host_scribble = (void *) (long) req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001163
1164 cmd = iu->buf;
1165 memset(cmd, 0, sizeof *cmd);
1166
1167 cmd->opcode = SRP_CMD;
1168 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001169 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001170 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1171
Roland Dreieraef9ec32005-11-02 14:07:13 -08001172 req->scmnd = scmnd;
1173 req->cmd = iu;
1174 req->cmd_done = 0;
1175 req->tsk_mgmt = NULL;
1176
1177 len = srp_map_data(scmnd, target, req);
1178 if (len < 0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001179 shost_printk(KERN_ERR, target->scsi_host,
1180 PFX "Failed to map data\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001181 goto err;
1182 }
1183
Ralph Campbell85507bc2006-12-12 14:30:55 -08001184 ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len,
1185 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001186
1187 if (__srp_post_send(target, iu, len)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001188 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001189 goto err_unmap;
1190 }
1191
Roland Dreierd945e1d2006-05-09 10:50:28 -07001192 list_move_tail(&req->list, &target->req_queue);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001193
1194 return 0;
1195
1196err_unmap:
1197 srp_unmap_data(scmnd, target, req);
1198
1199err:
1200 return SCSI_MLQUEUE_HOST_BUSY;
1201}
1202
1203static int srp_alloc_iu_bufs(struct srp_target_port *target)
1204{
1205 int i;
1206
1207 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1208 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1209 target->max_ti_iu_len,
1210 GFP_KERNEL, DMA_FROM_DEVICE);
1211 if (!target->rx_ring[i])
1212 goto err;
1213 }
1214
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001215 for (i = 0; i < SRP_SQ_SIZE; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001216 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
Vu Pham74b0a152006-06-17 20:37:32 -07001217 srp_max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001218 GFP_KERNEL, DMA_TO_DEVICE);
1219 if (!target->tx_ring[i])
1220 goto err;
1221 }
1222
1223 return 0;
1224
1225err:
1226 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1227 srp_free_iu(target->srp_host, target->rx_ring[i]);
1228 target->rx_ring[i] = NULL;
1229 }
1230
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001231 for (i = 0; i < SRP_SQ_SIZE; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001232 srp_free_iu(target->srp_host, target->tx_ring[i]);
1233 target->tx_ring[i] = NULL;
1234 }
1235
1236 return -ENOMEM;
1237}
1238
1239static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1240 struct ib_cm_event *event,
1241 struct srp_target_port *target)
1242{
David Dillow7aa54bd2008-01-07 18:23:41 -05001243 struct Scsi_Host *shost = target->scsi_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001244 struct ib_class_port_info *cpi;
1245 int opcode;
1246
1247 switch (event->param.rej_rcvd.reason) {
1248 case IB_CM_REJ_PORT_CM_REDIRECT:
1249 cpi = event->param.rej_rcvd.ari;
1250 target->path.dlid = cpi->redirect_lid;
1251 target->path.pkey = cpi->redirect_pkey;
1252 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1253 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1254
1255 target->status = target->path.dlid ?
1256 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1257 break;
1258
1259 case IB_CM_REJ_PORT_REDIRECT:
Roland Dreier5d7cbfd2007-08-03 10:45:18 -07001260 if (srp_target_is_topspin(target)) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001261 /*
1262 * Topspin/Cisco SRP gateways incorrectly send
1263 * reject reason code 25 when they mean 24
1264 * (port redirect).
1265 */
1266 memcpy(target->path.dgid.raw,
1267 event->param.rej_rcvd.ari, 16);
1268
David Dillow7aa54bd2008-01-07 18:23:41 -05001269 shost_printk(KERN_DEBUG, shost,
1270 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1271 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1272 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001273
1274 target->status = SRP_PORT_REDIRECT;
1275 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001276 shost_printk(KERN_WARNING, shost,
1277 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001278 target->status = -ECONNRESET;
1279 }
1280 break;
1281
1282 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
David Dillow7aa54bd2008-01-07 18:23:41 -05001283 shost_printk(KERN_WARNING, shost,
1284 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001285 target->status = -ECONNRESET;
1286 break;
1287
1288 case IB_CM_REJ_CONSUMER_DEFINED:
1289 opcode = *(u8 *) event->private_data;
1290 if (opcode == SRP_LOGIN_REJ) {
1291 struct srp_login_rej *rej = event->private_data;
1292 u32 reason = be32_to_cpu(rej->reason);
1293
1294 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
David Dillow7aa54bd2008-01-07 18:23:41 -05001295 shost_printk(KERN_WARNING, shost,
1296 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001297 else
David Dillow7aa54bd2008-01-07 18:23:41 -05001298 shost_printk(KERN_WARNING, shost,
1299 PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001300 } else
David Dillow7aa54bd2008-01-07 18:23:41 -05001301 shost_printk(KERN_WARNING, shost,
1302 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1303 " opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001304 target->status = -ECONNRESET;
1305 break;
1306
David Dillow9fe4bcf2008-01-08 17:08:52 -05001307 case IB_CM_REJ_STALE_CONN:
1308 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
1309 target->status = SRP_STALE_CONN;
1310 break;
1311
Roland Dreieraef9ec32005-11-02 14:07:13 -08001312 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001313 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
1314 event->param.rej_rcvd.reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001315 target->status = -ECONNRESET;
1316 }
1317}
1318
1319static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1320{
1321 struct srp_target_port *target = cm_id->context;
1322 struct ib_qp_attr *qp_attr = NULL;
1323 int attr_mask = 0;
1324 int comp = 0;
1325 int opcode = 0;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001326 int i;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001327
1328 switch (event->event) {
1329 case IB_CM_REQ_ERROR:
David Dillow7aa54bd2008-01-07 18:23:41 -05001330 shost_printk(KERN_DEBUG, target->scsi_host,
1331 PFX "Sending CM REQ failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001332 comp = 1;
1333 target->status = -ECONNRESET;
1334 break;
1335
1336 case IB_CM_REP_RECEIVED:
1337 comp = 1;
1338 opcode = *(u8 *) event->private_data;
1339
1340 if (opcode == SRP_LOGIN_RSP) {
1341 struct srp_login_rsp *rsp = event->private_data;
1342
1343 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1344 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1345
1346 target->scsi_host->can_queue = min(target->req_lim,
1347 target->scsi_host->can_queue);
1348 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001349 shost_printk(KERN_WARNING, target->scsi_host,
1350 PFX "Unhandled RSP opcode %#x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001351 target->status = -ECONNRESET;
1352 break;
1353 }
1354
Vu Phamd2fcea72006-11-21 14:14:10 -08001355 if (!target->rx_ring[0]) {
1356 target->status = srp_alloc_iu_bufs(target);
1357 if (target->status)
1358 break;
1359 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001360
1361 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1362 if (!qp_attr) {
1363 target->status = -ENOMEM;
1364 break;
1365 }
1366
1367 qp_attr->qp_state = IB_QPS_RTR;
1368 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1369 if (target->status)
1370 break;
1371
1372 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1373 if (target->status)
1374 break;
1375
Bart Van Asschec996bb42010-07-30 10:59:05 +00001376 for (i = 0; i < SRP_RQ_SIZE; i++) {
1377 target->status = srp_post_recv(target);
1378 if (target->status)
1379 break;
1380 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001381 if (target->status)
1382 break;
1383
1384 qp_attr->qp_state = IB_QPS_RTS;
1385 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1386 if (target->status)
1387 break;
1388
1389 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1390 if (target->status)
1391 break;
1392
1393 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1394 if (target->status)
1395 break;
1396
1397 break;
1398
1399 case IB_CM_REJ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001400 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001401 comp = 1;
1402
1403 srp_cm_rej_handler(cm_id, event, target);
1404 break;
1405
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001406 case IB_CM_DREQ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001407 shost_printk(KERN_WARNING, target->scsi_host,
1408 PFX "DREQ received - connection closed\n");
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001409 if (ib_send_cm_drep(cm_id, NULL, 0))
David Dillow7aa54bd2008-01-07 18:23:41 -05001410 shost_printk(KERN_ERR, target->scsi_host,
1411 PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001412 break;
1413
1414 case IB_CM_TIMEWAIT_EXIT:
David Dillow7aa54bd2008-01-07 18:23:41 -05001415 shost_printk(KERN_ERR, target->scsi_host,
1416 PFX "connection closed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001417
1418 comp = 1;
1419 target->status = 0;
1420 break;
1421
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001422 case IB_CM_MRA_RECEIVED:
1423 case IB_CM_DREQ_ERROR:
1424 case IB_CM_DREP_RECEIVED:
1425 break;
1426
Roland Dreieraef9ec32005-11-02 14:07:13 -08001427 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001428 shost_printk(KERN_WARNING, target->scsi_host,
1429 PFX "Unhandled CM event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001430 break;
1431 }
1432
1433 if (comp)
1434 complete(&target->done);
1435
1436 kfree(qp_attr);
1437
1438 return 0;
1439}
1440
Roland Dreierd945e1d2006-05-09 10:50:28 -07001441static int srp_send_tsk_mgmt(struct srp_target_port *target,
1442 struct srp_request *req, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001443{
Roland Dreieraef9ec32005-11-02 14:07:13 -08001444 struct srp_iu *iu;
1445 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001446
1447 spin_lock_irq(target->scsi_host->host_lock);
1448
Roland Dreier1285b3a2006-03-03 15:47:25 -08001449 if (target->state == SRP_TARGET_DEAD ||
1450 target->state == SRP_TARGET_REMOVED) {
Roland Dreierd945e1d2006-05-09 10:50:28 -07001451 req->scmnd->result = DID_BAD_TARGET << 16;
Roland Dreier1285b3a2006-03-03 15:47:25 -08001452 goto out;
1453 }
1454
Roland Dreieraef9ec32005-11-02 14:07:13 -08001455 init_completion(&req->done);
1456
David Dillowbb125882010-10-08 14:40:47 -04001457 iu = __srp_get_tx_iu(target, SRP_IU_TSK_MGMT);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001458 if (!iu)
1459 goto out;
1460
1461 tsk_mgmt = iu->buf;
1462 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1463
1464 tsk_mgmt->opcode = SRP_TSK_MGMT;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001465 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1466 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001467 tsk_mgmt->tsk_mgmt_func = func;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001468 tsk_mgmt->task_tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001469
1470 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1471 goto out;
1472
1473 req->tsk_mgmt = iu;
1474
1475 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001476
Roland Dreieraef9ec32005-11-02 14:07:13 -08001477 if (!wait_for_completion_timeout(&req->done,
1478 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001479 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001480
Roland Dreierd945e1d2006-05-09 10:50:28 -07001481 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001482
1483out:
1484 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001485 return -1;
1486}
1487
1488static int srp_find_req(struct srp_target_port *target,
1489 struct scsi_cmnd *scmnd,
1490 struct srp_request **req)
1491{
1492 if (scmnd->host_scribble == (void *) -1L)
1493 return -1;
1494
1495 *req = &target->req_ring[(long) scmnd->host_scribble];
1496
1497 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001498}
1499
1500static int srp_abort(struct scsi_cmnd *scmnd)
1501{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001502 struct srp_target_port *target = host_to_target(scmnd->device->host);
1503 struct srp_request *req;
1504 int ret = SUCCESS;
1505
David Dillow7aa54bd2008-01-07 18:23:41 -05001506 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001507
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001508 if (target->qp_in_error)
1509 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001510 if (srp_find_req(target, scmnd, &req))
1511 return FAILED;
1512 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1513 return FAILED;
1514
1515 spin_lock_irq(target->scsi_host->host_lock);
1516
1517 if (req->cmd_done) {
1518 srp_remove_req(target, req);
1519 scmnd->scsi_done(scmnd);
1520 } else if (!req->tsk_status) {
1521 srp_remove_req(target, req);
1522 scmnd->result = DID_ABORT << 16;
1523 } else
1524 ret = FAILED;
1525
1526 spin_unlock_irq(target->scsi_host->host_lock);
1527
1528 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001529}
1530
1531static int srp_reset_device(struct scsi_cmnd *scmnd)
1532{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001533 struct srp_target_port *target = host_to_target(scmnd->device->host);
1534 struct srp_request *req, *tmp;
1535
David Dillow7aa54bd2008-01-07 18:23:41 -05001536 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001537
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001538 if (target->qp_in_error)
1539 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001540 if (srp_find_req(target, scmnd, &req))
1541 return FAILED;
1542 if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1543 return FAILED;
1544 if (req->tsk_status)
1545 return FAILED;
1546
1547 spin_lock_irq(target->scsi_host->host_lock);
1548
1549 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001550 if (req->scmnd->device == scmnd->device)
1551 srp_reset_req(target, req);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001552
1553 spin_unlock_irq(target->scsi_host->host_lock);
1554
1555 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001556}
1557
1558static int srp_reset_host(struct scsi_cmnd *scmnd)
1559{
1560 struct srp_target_port *target = host_to_target(scmnd->device->host);
1561 int ret = FAILED;
1562
David Dillow7aa54bd2008-01-07 18:23:41 -05001563 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001564
1565 if (!srp_reconnect_target(target))
1566 ret = SUCCESS;
1567
1568 return ret;
1569}
1570
Tony Jonesee959b02008-02-22 00:13:36 +01001571static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
1572 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001573{
Tony Jonesee959b02008-02-22 00:13:36 +01001574 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001575
1576 if (target->state == SRP_TARGET_DEAD ||
1577 target->state == SRP_TARGET_REMOVED)
1578 return -ENODEV;
1579
1580 return sprintf(buf, "0x%016llx\n",
1581 (unsigned long long) be64_to_cpu(target->id_ext));
1582}
1583
Tony Jonesee959b02008-02-22 00:13:36 +01001584static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
1585 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001586{
Tony Jonesee959b02008-02-22 00:13:36 +01001587 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001588
1589 if (target->state == SRP_TARGET_DEAD ||
1590 target->state == SRP_TARGET_REMOVED)
1591 return -ENODEV;
1592
1593 return sprintf(buf, "0x%016llx\n",
1594 (unsigned long long) be64_to_cpu(target->ioc_guid));
1595}
1596
Tony Jonesee959b02008-02-22 00:13:36 +01001597static ssize_t show_service_id(struct device *dev,
1598 struct device_attribute *attr, char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001599{
Tony Jonesee959b02008-02-22 00:13:36 +01001600 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001601
1602 if (target->state == SRP_TARGET_DEAD ||
1603 target->state == SRP_TARGET_REMOVED)
1604 return -ENODEV;
1605
1606 return sprintf(buf, "0x%016llx\n",
1607 (unsigned long long) be64_to_cpu(target->service_id));
1608}
1609
Tony Jonesee959b02008-02-22 00:13:36 +01001610static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
1611 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001612{
Tony Jonesee959b02008-02-22 00:13:36 +01001613 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001614
1615 if (target->state == SRP_TARGET_DEAD ||
1616 target->state == SRP_TARGET_REMOVED)
1617 return -ENODEV;
1618
1619 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1620}
1621
Tony Jonesee959b02008-02-22 00:13:36 +01001622static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
1623 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001624{
Tony Jonesee959b02008-02-22 00:13:36 +01001625 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001626
1627 if (target->state == SRP_TARGET_DEAD ||
1628 target->state == SRP_TARGET_REMOVED)
1629 return -ENODEV;
1630
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001631 return sprintf(buf, "%pI6\n", target->path.dgid.raw);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001632}
1633
Tony Jonesee959b02008-02-22 00:13:36 +01001634static ssize_t show_orig_dgid(struct device *dev,
1635 struct device_attribute *attr, char *buf)
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001636{
Tony Jonesee959b02008-02-22 00:13:36 +01001637 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001638
1639 if (target->state == SRP_TARGET_DEAD ||
1640 target->state == SRP_TARGET_REMOVED)
1641 return -ENODEV;
1642
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001643 return sprintf(buf, "%pI6\n", target->orig_dgid);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001644}
1645
Bart Van Assche89de7482010-08-03 14:08:45 +00001646static ssize_t show_req_lim(struct device *dev,
1647 struct device_attribute *attr, char *buf)
1648{
1649 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1650
1651 if (target->state == SRP_TARGET_DEAD ||
1652 target->state == SRP_TARGET_REMOVED)
1653 return -ENODEV;
1654
1655 return sprintf(buf, "%d\n", target->req_lim);
1656}
1657
Tony Jonesee959b02008-02-22 00:13:36 +01001658static ssize_t show_zero_req_lim(struct device *dev,
1659 struct device_attribute *attr, char *buf)
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001660{
Tony Jonesee959b02008-02-22 00:13:36 +01001661 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001662
1663 if (target->state == SRP_TARGET_DEAD ||
1664 target->state == SRP_TARGET_REMOVED)
1665 return -ENODEV;
1666
1667 return sprintf(buf, "%d\n", target->zero_req_lim);
1668}
1669
Tony Jonesee959b02008-02-22 00:13:36 +01001670static ssize_t show_local_ib_port(struct device *dev,
1671 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001672{
Tony Jonesee959b02008-02-22 00:13:36 +01001673 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001674
1675 return sprintf(buf, "%d\n", target->srp_host->port);
1676}
1677
Tony Jonesee959b02008-02-22 00:13:36 +01001678static ssize_t show_local_ib_device(struct device *dev,
1679 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001680{
Tony Jonesee959b02008-02-22 00:13:36 +01001681 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001682
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001683 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001684}
1685
Tony Jonesee959b02008-02-22 00:13:36 +01001686static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1687static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1688static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1689static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1690static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1691static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
Bart Van Assche89de7482010-08-03 14:08:45 +00001692static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01001693static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1694static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1695static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001696
Tony Jonesee959b02008-02-22 00:13:36 +01001697static struct device_attribute *srp_host_attrs[] = {
1698 &dev_attr_id_ext,
1699 &dev_attr_ioc_guid,
1700 &dev_attr_service_id,
1701 &dev_attr_pkey,
1702 &dev_attr_dgid,
1703 &dev_attr_orig_dgid,
Bart Van Assche89de7482010-08-03 14:08:45 +00001704 &dev_attr_req_lim,
Tony Jonesee959b02008-02-22 00:13:36 +01001705 &dev_attr_zero_req_lim,
1706 &dev_attr_local_ib_port,
1707 &dev_attr_local_ib_device,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001708 NULL
1709};
1710
Roland Dreieraef9ec32005-11-02 14:07:13 -08001711static struct scsi_host_template srp_template = {
1712 .module = THIS_MODULE,
Roland Dreierb7f008f2007-05-06 21:18:11 -07001713 .name = "InfiniBand SRP initiator",
1714 .proc_name = DRV_NAME,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001715 .info = srp_target_info,
1716 .queuecommand = srp_queuecommand,
1717 .eh_abort_handler = srp_abort,
1718 .eh_device_reset_handler = srp_reset_device,
1719 .eh_host_reset_handler = srp_reset_host,
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001720 .can_queue = SRP_CMD_SQ_SIZE,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001721 .this_id = -1,
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001722 .cmd_per_lun = SRP_CMD_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001723 .use_clustering = ENABLE_CLUSTERING,
1724 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001725};
1726
1727static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1728{
FUJITA Tomonori32368222007-06-27 16:33:12 +09001729 struct srp_rport_identifiers ids;
1730 struct srp_rport *rport;
1731
Roland Dreieraef9ec32005-11-02 14:07:13 -08001732 sprintf(target->target_name, "SRP.T10:%016llX",
1733 (unsigned long long) be64_to_cpu(target->id_ext));
1734
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001735 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001736 return -ENODEV;
1737
FUJITA Tomonori32368222007-06-27 16:33:12 +09001738 memcpy(ids.port_id, &target->id_ext, 8);
1739 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09001740 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori32368222007-06-27 16:33:12 +09001741 rport = srp_rport_add(target->scsi_host, &ids);
1742 if (IS_ERR(rport)) {
1743 scsi_remove_host(target->scsi_host);
1744 return PTR_ERR(rport);
1745 }
1746
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001747 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001748 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001749 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001750
1751 target->state = SRP_TARGET_LIVE;
1752
Roland Dreieraef9ec32005-11-02 14:07:13 -08001753 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07001754 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001755
1756 return 0;
1757}
1758
Tony Jonesee959b02008-02-22 00:13:36 +01001759static void srp_release_dev(struct device *dev)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001760{
1761 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01001762 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001763
1764 complete(&host->released);
1765}
1766
1767static struct class srp_class = {
1768 .name = "infiniband_srp",
Tony Jonesee959b02008-02-22 00:13:36 +01001769 .dev_release = srp_release_dev
Roland Dreieraef9ec32005-11-02 14:07:13 -08001770};
1771
1772/*
1773 * Target ports are added by writing
1774 *
1775 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1776 * pkey=<P_Key>,service_id=<service ID>
1777 *
1778 * to the add_target sysfs attribute.
1779 */
1780enum {
1781 SRP_OPT_ERR = 0,
1782 SRP_OPT_ID_EXT = 1 << 0,
1783 SRP_OPT_IOC_GUID = 1 << 1,
1784 SRP_OPT_DGID = 1 << 2,
1785 SRP_OPT_PKEY = 1 << 3,
1786 SRP_OPT_SERVICE_ID = 1 << 4,
1787 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07001788 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07001789 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001790 SRP_OPT_INITIATOR_EXT = 1 << 8,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001791 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1792 SRP_OPT_IOC_GUID |
1793 SRP_OPT_DGID |
1794 SRP_OPT_PKEY |
1795 SRP_OPT_SERVICE_ID),
1796};
1797
Steven Whitehousea447c092008-10-13 10:46:57 +01001798static const match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07001799 { SRP_OPT_ID_EXT, "id_ext=%s" },
1800 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1801 { SRP_OPT_DGID, "dgid=%s" },
1802 { SRP_OPT_PKEY, "pkey=%x" },
1803 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1804 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1805 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07001806 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001807 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
Vu Pham52fb2b502006-06-17 20:37:31 -07001808 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001809};
1810
1811static int srp_parse_options(const char *buf, struct srp_target_port *target)
1812{
1813 char *options, *sep_opt;
1814 char *p;
1815 char dgid[3];
1816 substring_t args[MAX_OPT_ARGS];
1817 int opt_mask = 0;
1818 int token;
1819 int ret = -EINVAL;
1820 int i;
1821
1822 options = kstrdup(buf, GFP_KERNEL);
1823 if (!options)
1824 return -ENOMEM;
1825
1826 sep_opt = options;
1827 while ((p = strsep(&sep_opt, ",")) != NULL) {
1828 if (!*p)
1829 continue;
1830
1831 token = match_token(p, srp_opt_tokens, args);
1832 opt_mask |= token;
1833
1834 switch (token) {
1835 case SRP_OPT_ID_EXT:
1836 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001837 if (!p) {
1838 ret = -ENOMEM;
1839 goto out;
1840 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001841 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1842 kfree(p);
1843 break;
1844
1845 case SRP_OPT_IOC_GUID:
1846 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001847 if (!p) {
1848 ret = -ENOMEM;
1849 goto out;
1850 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001851 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1852 kfree(p);
1853 break;
1854
1855 case SRP_OPT_DGID:
1856 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001857 if (!p) {
1858 ret = -ENOMEM;
1859 goto out;
1860 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001861 if (strlen(p) != 32) {
1862 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07001863 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001864 goto out;
1865 }
1866
1867 for (i = 0; i < 16; ++i) {
1868 strlcpy(dgid, p + i * 2, 3);
1869 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1870 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08001871 kfree(p);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001872 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001873 break;
1874
1875 case SRP_OPT_PKEY:
1876 if (match_hex(args, &token)) {
1877 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1878 goto out;
1879 }
1880 target->path.pkey = cpu_to_be16(token);
1881 break;
1882
1883 case SRP_OPT_SERVICE_ID:
1884 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001885 if (!p) {
1886 ret = -ENOMEM;
1887 goto out;
1888 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001889 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
Sean Hefty247e0202007-08-08 15:51:18 -07001890 target->path.service_id = target->service_id;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001891 kfree(p);
1892 break;
1893
1894 case SRP_OPT_MAX_SECT:
1895 if (match_int(args, &token)) {
1896 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1897 goto out;
1898 }
1899 target->scsi_host->max_sectors = token;
1900 break;
1901
Vu Pham52fb2b502006-06-17 20:37:31 -07001902 case SRP_OPT_MAX_CMD_PER_LUN:
1903 if (match_int(args, &token)) {
1904 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1905 goto out;
1906 }
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001907 target->scsi_host->cmd_per_lun = min(token, SRP_CMD_SQ_SIZE);
Vu Pham52fb2b502006-06-17 20:37:31 -07001908 break;
1909
Ramachandra K0c0450db2006-06-17 20:37:38 -07001910 case SRP_OPT_IO_CLASS:
1911 if (match_hex(args, &token)) {
1912 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1913 goto out;
1914 }
1915 if (token != SRP_REV10_IB_IO_CLASS &&
1916 token != SRP_REV16A_IB_IO_CLASS) {
1917 printk(KERN_WARNING PFX "unknown IO class parameter value"
1918 " %x specified (use %x or %x).\n",
1919 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1920 goto out;
1921 }
1922 target->io_class = token;
1923 break;
1924
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001925 case SRP_OPT_INITIATOR_EXT:
1926 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001927 if (!p) {
1928 ret = -ENOMEM;
1929 goto out;
1930 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001931 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1932 kfree(p);
1933 break;
1934
Roland Dreieraef9ec32005-11-02 14:07:13 -08001935 default:
1936 printk(KERN_WARNING PFX "unknown parameter or missing value "
1937 "'%s' in target creation request\n", p);
1938 goto out;
1939 }
1940 }
1941
1942 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1943 ret = 0;
1944 else
1945 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1946 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1947 !(srp_opt_tokens[i].token & opt_mask))
1948 printk(KERN_WARNING PFX "target creation request is "
1949 "missing parameter '%s'\n",
1950 srp_opt_tokens[i].pattern);
1951
1952out:
1953 kfree(options);
1954 return ret;
1955}
1956
Tony Jonesee959b02008-02-22 00:13:36 +01001957static ssize_t srp_create_target(struct device *dev,
1958 struct device_attribute *attr,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001959 const char *buf, size_t count)
1960{
1961 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01001962 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001963 struct Scsi_Host *target_host;
1964 struct srp_target_port *target;
1965 int ret;
1966 int i;
1967
1968 target_host = scsi_host_alloc(&srp_template,
1969 sizeof (struct srp_target_port));
1970 if (!target_host)
1971 return -ENOMEM;
1972
FUJITA Tomonori32368222007-06-27 16:33:12 +09001973 target_host->transportt = ib_srp_transport_template;
Arne Redlich3c8edf02006-11-15 12:43:00 +01001974 target_host->max_lun = SRP_MAX_LUN;
1975 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08001976
Roland Dreieraef9ec32005-11-02 14:07:13 -08001977 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001978
Ramachandra K0c0450db2006-06-17 20:37:38 -07001979 target->io_class = SRP_REV16A_IB_IO_CLASS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001980 target->scsi_host = target_host;
1981 target->srp_host = host;
1982
Roland Dreierd945e1d2006-05-09 10:50:28 -07001983 INIT_LIST_HEAD(&target->free_reqs);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001984 INIT_LIST_HEAD(&target->req_queue);
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001985 for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
Roland Dreierd945e1d2006-05-09 10:50:28 -07001986 target->req_ring[i].index = i;
1987 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1988 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001989
1990 ret = srp_parse_options(buf, target);
1991 if (ret)
1992 goto err;
1993
Roland Dreier969a60f2008-07-14 23:48:43 -07001994 ib_query_gid(host->srp_dev->dev, host->port, 0, &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001995
David Dillow7aa54bd2008-01-07 18:23:41 -05001996 shost_printk(KERN_DEBUG, target->scsi_host, PFX
1997 "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001998 "service_id %016llx dgid %pI6\n",
Roland Dreieraef9ec32005-11-02 14:07:13 -08001999 (unsigned long long) be64_to_cpu(target->id_ext),
2000 (unsigned long long) be64_to_cpu(target->ioc_guid),
2001 be16_to_cpu(target->path.pkey),
2002 (unsigned long long) be64_to_cpu(target->service_id),
Harvey Harrison8867cd72008-10-28 22:36:33 -07002003 target->path.dgid.raw);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002004
2005 ret = srp_create_target_ib(target);
2006 if (ret)
2007 goto err;
2008
David Dillow9fe4bcf2008-01-08 17:08:52 -05002009 ret = srp_new_cm_id(target);
2010 if (ret)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002011 goto err_free;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002012
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02002013 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002014 ret = srp_connect_target(target);
2015 if (ret) {
David Dillow7aa54bd2008-01-07 18:23:41 -05002016 shost_printk(KERN_ERR, target->scsi_host,
2017 PFX "Connection failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002018 goto err_cm_id;
2019 }
2020
2021 ret = srp_add_target(host, target);
2022 if (ret)
2023 goto err_disconnect;
2024
2025 return count;
2026
2027err_disconnect:
2028 srp_disconnect_target(target);
2029
2030err_cm_id:
2031 ib_destroy_cm_id(target->cm_id);
2032
2033err_free:
2034 srp_free_target_ib(target);
2035
2036err:
2037 scsi_host_put(target_host);
2038
2039 return ret;
2040}
2041
Tony Jonesee959b02008-02-22 00:13:36 +01002042static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002043
Tony Jonesee959b02008-02-22 00:13:36 +01002044static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
2045 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002046{
Tony Jonesee959b02008-02-22 00:13:36 +01002047 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002048
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002049 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002050}
2051
Tony Jonesee959b02008-02-22 00:13:36 +01002052static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002053
Tony Jonesee959b02008-02-22 00:13:36 +01002054static ssize_t show_port(struct device *dev, struct device_attribute *attr,
2055 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002056{
Tony Jonesee959b02008-02-22 00:13:36 +01002057 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002058
2059 return sprintf(buf, "%d\n", host->port);
2060}
2061
Tony Jonesee959b02008-02-22 00:13:36 +01002062static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002063
Roland Dreierf5358a12006-06-17 20:37:29 -07002064static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002065{
2066 struct srp_host *host;
2067
2068 host = kzalloc(sizeof *host, GFP_KERNEL);
2069 if (!host)
2070 return NULL;
2071
2072 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002073 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002074 init_completion(&host->released);
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002075 host->srp_dev = device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002076 host->port = port;
2077
Tony Jonesee959b02008-02-22 00:13:36 +01002078 host->dev.class = &srp_class;
2079 host->dev.parent = device->dev->dma_device;
Kay Sieversd927e382009-01-06 10:44:39 -08002080 dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002081
Tony Jonesee959b02008-02-22 00:13:36 +01002082 if (device_register(&host->dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07002083 goto free_host;
Tony Jonesee959b02008-02-22 00:13:36 +01002084 if (device_create_file(&host->dev, &dev_attr_add_target))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002085 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01002086 if (device_create_file(&host->dev, &dev_attr_ibdev))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002087 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01002088 if (device_create_file(&host->dev, &dev_attr_port))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002089 goto err_class;
2090
2091 return host;
2092
2093err_class:
Tony Jonesee959b02008-02-22 00:13:36 +01002094 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002095
Roland Dreierf5358a12006-06-17 20:37:29 -07002096free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08002097 kfree(host);
2098
2099 return NULL;
2100}
2101
2102static void srp_add_one(struct ib_device *device)
2103{
Roland Dreierf5358a12006-06-17 20:37:29 -07002104 struct srp_device *srp_dev;
2105 struct ib_device_attr *dev_attr;
2106 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002107 struct srp_host *host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002108 int s, e, p;
2109
Roland Dreierf5358a12006-06-17 20:37:29 -07002110 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
2111 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08002112 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002113
Roland Dreierf5358a12006-06-17 20:37:29 -07002114 if (ib_query_device(device, dev_attr)) {
2115 printk(KERN_WARNING PFX "Query device failed for %s\n",
2116 device->name);
2117 goto free_attr;
2118 }
2119
2120 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
2121 if (!srp_dev)
2122 goto free_attr;
2123
2124 /*
2125 * Use the smallest page size supported by the HCA, down to a
2126 * minimum of 512 bytes (which is the smallest sector that a
2127 * SCSI command will ever carry).
2128 */
2129 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
2130 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
Roland Dreierbf628dc2006-12-15 14:01:49 -08002131 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
Roland Dreierf5358a12006-06-17 20:37:29 -07002132
2133 INIT_LIST_HEAD(&srp_dev->dev_list);
2134
2135 srp_dev->dev = device;
2136 srp_dev->pd = ib_alloc_pd(device);
2137 if (IS_ERR(srp_dev->pd))
2138 goto free_dev;
2139
2140 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
2141 IB_ACCESS_LOCAL_WRITE |
2142 IB_ACCESS_REMOTE_READ |
2143 IB_ACCESS_REMOTE_WRITE);
2144 if (IS_ERR(srp_dev->mr))
2145 goto err_pd;
2146
2147 memset(&fmr_param, 0, sizeof fmr_param);
2148 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
2149 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
2150 fmr_param.cache = 1;
2151 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
2152 fmr_param.page_shift = srp_dev->fmr_page_shift;
2153 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
2154 IB_ACCESS_REMOTE_WRITE |
2155 IB_ACCESS_REMOTE_READ);
2156
2157 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
2158 if (IS_ERR(srp_dev->fmr_pool))
2159 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002160
Tom Tucker07ebafb2006-08-03 16:02:42 -05002161 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002162 s = 0;
2163 e = 0;
2164 } else {
2165 s = 1;
2166 e = device->phys_port_cnt;
2167 }
2168
2169 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07002170 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002171 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07002172 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002173 }
2174
Roland Dreierf5358a12006-06-17 20:37:29 -07002175 ib_set_client_data(device, &srp_client, srp_dev);
2176
2177 goto free_attr;
2178
2179err_pd:
2180 ib_dealloc_pd(srp_dev->pd);
2181
2182free_dev:
2183 kfree(srp_dev);
2184
2185free_attr:
2186 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002187}
2188
2189static void srp_remove_one(struct ib_device *device)
2190{
Roland Dreierf5358a12006-06-17 20:37:29 -07002191 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002192 struct srp_host *host, *tmp_host;
2193 LIST_HEAD(target_list);
2194 struct srp_target_port *target, *tmp_target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002195
Roland Dreierf5358a12006-06-17 20:37:29 -07002196 srp_dev = ib_get_client_data(device, &srp_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002197
Roland Dreierf5358a12006-06-17 20:37:29 -07002198 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Tony Jonesee959b02008-02-22 00:13:36 +01002199 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002200 /*
2201 * Wait for the sysfs entry to go away, so that no new
2202 * target ports can be created.
2203 */
2204 wait_for_completion(&host->released);
2205
2206 /*
2207 * Mark all target ports as removed, so we stop queueing
2208 * commands and don't try to reconnect.
2209 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002210 spin_lock(&host->target_lock);
Matthew Wilcox549c5fc22006-06-17 20:37:30 -07002211 list_for_each_entry(target, &host->target_list, list) {
Ishai Rabinovitz0c5b3952006-06-17 20:37:31 -07002212 spin_lock_irq(target->scsi_host->host_lock);
2213 target->state = SRP_TARGET_REMOVED;
2214 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002215 }
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002216 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002217
2218 /*
2219 * Wait for any reconnection tasks that may have
2220 * started before we marked our target ports as
2221 * removed, and any target port removal tasks.
2222 */
2223 flush_scheduled_work();
2224
2225 list_for_each_entry_safe(target, tmp_target,
2226 &host->target_list, list) {
David Dillowb0e47c82008-01-03 10:25:27 -08002227 srp_remove_host(target->scsi_host);
Dave Dillowad696982008-01-03 22:35:41 -05002228 scsi_remove_host(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002229 srp_disconnect_target(target);
2230 ib_destroy_cm_id(target->cm_id);
2231 srp_free_target_ib(target);
2232 scsi_host_put(target->scsi_host);
2233 }
2234
Roland Dreieraef9ec32005-11-02 14:07:13 -08002235 kfree(host);
2236 }
2237
Roland Dreierf5358a12006-06-17 20:37:29 -07002238 if (srp_dev->fmr_pool)
2239 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2240 ib_dereg_mr(srp_dev->mr);
2241 ib_dealloc_pd(srp_dev->pd);
2242
2243 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002244}
2245
FUJITA Tomonori32368222007-06-27 16:33:12 +09002246static struct srp_function_template ib_srp_transport_functions = {
2247};
2248
Roland Dreieraef9ec32005-11-02 14:07:13 -08002249static int __init srp_init_module(void)
2250{
2251 int ret;
2252
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00002253 BUILD_BUG_ON_NOT_POWER_OF_2(SRP_SQ_SIZE);
2254 BUILD_BUG_ON_NOT_POWER_OF_2(SRP_RQ_SIZE);
2255
David Dillow1e89a192008-04-16 21:01:12 -07002256 if (srp_sg_tablesize > 255) {
2257 printk(KERN_WARNING PFX "Clamping srp_sg_tablesize to 255\n");
2258 srp_sg_tablesize = 255;
2259 }
2260
FUJITA Tomonori32368222007-06-27 16:33:12 +09002261 ib_srp_transport_template =
2262 srp_attach_transport(&ib_srp_transport_functions);
2263 if (!ib_srp_transport_template)
2264 return -ENOMEM;
2265
Vu Pham74b0a152006-06-17 20:37:32 -07002266 srp_template.sg_tablesize = srp_sg_tablesize;
2267 srp_max_iu_len = (sizeof (struct srp_cmd) +
2268 sizeof (struct srp_indirect_buf) +
2269 srp_sg_tablesize * 16);
2270
Roland Dreieraef9ec32005-11-02 14:07:13 -08002271 ret = class_register(&srp_class);
2272 if (ret) {
2273 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002274 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002275 return ret;
2276 }
2277
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002278 ib_sa_register_client(&srp_sa_client);
2279
Roland Dreieraef9ec32005-11-02 14:07:13 -08002280 ret = ib_register_client(&srp_client);
2281 if (ret) {
2282 printk(KERN_ERR PFX "couldn't register IB client\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002283 srp_release_transport(ib_srp_transport_template);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002284 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002285 class_unregister(&srp_class);
2286 return ret;
2287 }
2288
2289 return 0;
2290}
2291
2292static void __exit srp_cleanup_module(void)
2293{
2294 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002295 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002296 class_unregister(&srp_class);
FUJITA Tomonori32368222007-06-27 16:33:12 +09002297 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002298}
2299
2300module_init(srp_init_module);
2301module_exit(srp_cleanup_module);