blob: e5bd181dbce5847bd175b78acc00cb4cc1009c0b [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);
86
FUJITA Tomonori32368222007-06-27 16:33:12 +090087static struct scsi_transport_template *ib_srp_transport_template;
88
Roland Dreieraef9ec32005-11-02 14:07:13 -080089static struct ib_client srp_client = {
90 .name = "srp",
91 .add = srp_add_one,
92 .remove = srp_remove_one
93};
94
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -070095static struct ib_sa_client srp_sa_client;
96
Roland Dreieraef9ec32005-11-02 14:07:13 -080097static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
98{
99 return (struct srp_target_port *) host->hostdata;
100}
101
102static const char *srp_target_info(struct Scsi_Host *host)
103{
104 return host_to_target(host)->target_name;
105}
106
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700107static int srp_target_is_topspin(struct srp_target_port *target)
108{
109 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700110 static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700111
112 return topspin_workarounds &&
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700113 (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
114 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700115}
116
117static int srp_target_is_mellanox(struct srp_target_port *target)
118{
119 static const u8 mellanox_oui[3] = { 0x00, 0x02, 0xc9 };
120
121 return mellanox_workarounds &&
122 !memcmp(&target->ioc_guid, mellanox_oui, sizeof mellanox_oui);
123}
124
Roland Dreieraef9ec32005-11-02 14:07:13 -0800125static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
126 gfp_t gfp_mask,
127 enum dma_data_direction direction)
128{
129 struct srp_iu *iu;
130
131 iu = kmalloc(sizeof *iu, gfp_mask);
132 if (!iu)
133 goto out;
134
135 iu->buf = kzalloc(size, gfp_mask);
136 if (!iu->buf)
137 goto out_free_iu;
138
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100139 iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
140 direction);
141 if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800142 goto out_free_buf;
143
144 iu->size = size;
145 iu->direction = direction;
146
147 return iu;
148
149out_free_buf:
150 kfree(iu->buf);
151out_free_iu:
152 kfree(iu);
153out:
154 return NULL;
155}
156
157static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
158{
159 if (!iu)
160 return;
161
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100162 ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
163 iu->direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800164 kfree(iu->buf);
165 kfree(iu);
166}
167
168static void srp_qp_event(struct ib_event *event, void *context)
169{
170 printk(KERN_ERR PFX "QP event %d\n", event->event);
171}
172
173static int srp_init_qp(struct srp_target_port *target,
174 struct ib_qp *qp)
175{
176 struct ib_qp_attr *attr;
177 int ret;
178
179 attr = kmalloc(sizeof *attr, GFP_KERNEL);
180 if (!attr)
181 return -ENOMEM;
182
Roland Dreier969a60f2008-07-14 23:48:43 -0700183 ret = ib_find_pkey(target->srp_host->srp_dev->dev,
184 target->srp_host->port,
185 be16_to_cpu(target->path.pkey),
186 &attr->pkey_index);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800187 if (ret)
188 goto out;
189
190 attr->qp_state = IB_QPS_INIT;
191 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
192 IB_ACCESS_REMOTE_WRITE);
193 attr->port_num = target->srp_host->port;
194
195 ret = ib_modify_qp(qp, attr,
196 IB_QP_STATE |
197 IB_QP_PKEY_INDEX |
198 IB_QP_ACCESS_FLAGS |
199 IB_QP_PORT);
200
201out:
202 kfree(attr);
203 return ret;
204}
205
David Dillow9fe4bcf2008-01-08 17:08:52 -0500206static int srp_new_cm_id(struct srp_target_port *target)
207{
208 struct ib_cm_id *new_cm_id;
209
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100210 new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
David Dillow9fe4bcf2008-01-08 17:08:52 -0500211 srp_cm_handler, target);
212 if (IS_ERR(new_cm_id))
213 return PTR_ERR(new_cm_id);
214
215 if (target->cm_id)
216 ib_destroy_cm_id(target->cm_id);
217 target->cm_id = new_cm_id;
218
219 return 0;
220}
221
Roland Dreieraef9ec32005-11-02 14:07:13 -0800222static int srp_create_target_ib(struct srp_target_port *target)
223{
224 struct ib_qp_init_attr *init_attr;
225 int ret;
226
227 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
228 if (!init_attr)
229 return -ENOMEM;
230
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000231 target->recv_cq = ib_create_cq(target->srp_host->srp_dev->dev,
232 srp_recv_completion, NULL, target, SRP_RQ_SIZE, 0);
233 if (IS_ERR(target->recv_cq)) {
234 ret = PTR_ERR(target->recv_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800235 goto err;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800236 }
237
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000238 target->send_cq = ib_create_cq(target->srp_host->srp_dev->dev,
239 srp_send_completion, NULL, target, SRP_SQ_SIZE, 0);
240 if (IS_ERR(target->send_cq)) {
241 ret = PTR_ERR(target->send_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800242 goto err_recv_cq;
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000243 }
244
245 ib_req_notify_cq(target->recv_cq, IB_CQ_NEXT_COMP);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800246
247 init_attr->event_handler = srp_qp_event;
248 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
249 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
250 init_attr->cap.max_recv_sge = 1;
251 init_attr->cap.max_send_sge = 1;
252 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
253 init_attr->qp_type = IB_QPT_RC;
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000254 init_attr->send_cq = target->send_cq;
255 init_attr->recv_cq = target->recv_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800256
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100257 target->qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800258 if (IS_ERR(target->qp)) {
259 ret = PTR_ERR(target->qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800260 goto err_send_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800261 }
262
263 ret = srp_init_qp(target, target->qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800264 if (ret)
265 goto err_qp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800266
Roland Dreierda9d2f02010-02-24 15:07:59 -0800267 kfree(init_attr);
268 return 0;
269
270err_qp:
271 ib_destroy_qp(target->qp);
272
273err_send_cq:
274 ib_destroy_cq(target->send_cq);
275
276err_recv_cq:
277 ib_destroy_cq(target->recv_cq);
278
279err:
Roland Dreieraef9ec32005-11-02 14:07:13 -0800280 kfree(init_attr);
281 return ret;
282}
283
284static void srp_free_target_ib(struct srp_target_port *target)
285{
286 int i;
287
288 ib_destroy_qp(target->qp);
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000289 ib_destroy_cq(target->send_cq);
290 ib_destroy_cq(target->recv_cq);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800291
292 for (i = 0; i < SRP_RQ_SIZE; ++i)
293 srp_free_iu(target->srp_host, target->rx_ring[i]);
Bart Van Asschedd5e6e32010-08-30 19:27:20 +0000294 for (i = 0; i < SRP_SQ_SIZE; ++i)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800295 srp_free_iu(target->srp_host, target->tx_ring[i]);
296}
297
298static void srp_path_rec_completion(int status,
299 struct ib_sa_path_rec *pathrec,
300 void *target_ptr)
301{
302 struct srp_target_port *target = target_ptr;
303
304 target->status = status;
305 if (status)
David Dillow7aa54bd2008-01-07 18:23:41 -0500306 shost_printk(KERN_ERR, target->scsi_host,
307 PFX "Got failed path rec status %d\n", status);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800308 else
309 target->path = *pathrec;
310 complete(&target->done);
311}
312
313static int srp_lookup_path(struct srp_target_port *target)
314{
315 target->path.numb_path = 1;
316
317 init_completion(&target->done);
318
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700319 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100320 target->srp_host->srp_dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800321 target->srp_host->port,
322 &target->path,
Sean Hefty247e0202007-08-08 15:51:18 -0700323 IB_SA_PATH_REC_SERVICE_ID |
Roland Dreieraef9ec32005-11-02 14:07:13 -0800324 IB_SA_PATH_REC_DGID |
325 IB_SA_PATH_REC_SGID |
326 IB_SA_PATH_REC_NUMB_PATH |
327 IB_SA_PATH_REC_PKEY,
328 SRP_PATH_REC_TIMEOUT_MS,
329 GFP_KERNEL,
330 srp_path_rec_completion,
331 target, &target->path_query);
332 if (target->path_query_id < 0)
333 return target->path_query_id;
334
335 wait_for_completion(&target->done);
336
337 if (target->status < 0)
David Dillow7aa54bd2008-01-07 18:23:41 -0500338 shost_printk(KERN_WARNING, target->scsi_host,
339 PFX "Path record query failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800340
341 return target->status;
342}
343
344static int srp_send_req(struct srp_target_port *target)
345{
346 struct {
347 struct ib_cm_req_param param;
348 struct srp_login_req priv;
349 } *req = NULL;
350 int status;
351
352 req = kzalloc(sizeof *req, GFP_KERNEL);
353 if (!req)
354 return -ENOMEM;
355
356 req->param.primary_path = &target->path;
357 req->param.alternate_path = NULL;
358 req->param.service_id = target->service_id;
359 req->param.qp_num = target->qp->qp_num;
360 req->param.qp_type = target->qp->qp_type;
361 req->param.private_data = &req->priv;
362 req->param.private_data_len = sizeof req->priv;
363 req->param.flow_control = 1;
364
365 get_random_bytes(&req->param.starting_psn, 4);
366 req->param.starting_psn &= 0xffffff;
367
368 /*
369 * Pick some arbitrary defaults here; we could make these
370 * module parameters if anyone cared about setting them.
371 */
372 req->param.responder_resources = 4;
373 req->param.remote_cm_response_timeout = 20;
374 req->param.local_cm_response_timeout = 20;
375 req->param.retry_count = 7;
376 req->param.rnr_retry_count = 7;
377 req->param.max_cm_retries = 15;
378
379 req->priv.opcode = SRP_LOGIN_REQ;
380 req->priv.tag = 0;
Vu Pham74b0a152006-06-17 20:37:32 -0700381 req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800382 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
383 SRP_BUF_FORMAT_INDIRECT);
Ramachandra K0c0450d2006-06-17 20:37:38 -0700384 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700385 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450d2006-06-17 20:37:38 -0700386 * port identifier format is 8 bytes of ID extension followed
387 * by 8 bytes of GUID. Older drafts put the two halves in the
388 * opposite order, so that the GUID comes first.
389 *
390 * Targets conforming to these obsolete drafts can be
391 * recognized by the I/O Class they report.
392 */
393 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
394 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200395 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450d2006-06-17 20:37:38 -0700396 memcpy(req->priv.initiator_port_id + 8,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200397 &target->initiator_ext, 8);
Ramachandra K0c0450d2006-06-17 20:37:38 -0700398 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
399 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
400 } else {
401 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200402 &target->initiator_ext, 8);
403 memcpy(req->priv.initiator_port_id + 8,
404 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450d2006-06-17 20:37:38 -0700405 memcpy(req->priv.target_port_id, &target->id_ext, 8);
406 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
407 }
408
Roland Dreieraef9ec32005-11-02 14:07:13 -0800409 /*
410 * Topspin/Cisco SRP targets will reject our login unless we
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200411 * zero out the first 8 bytes of our initiator port ID and set
412 * the second 8 bytes to the local node GUID.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800413 */
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700414 if (srp_target_is_topspin(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500415 shost_printk(KERN_DEBUG, target->scsi_host,
416 PFX "Topspin/Cisco initiator port ID workaround "
417 "activated for target GUID %016llx\n",
418 (unsigned long long) be64_to_cpu(target->ioc_guid));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800419 memset(req->priv.initiator_port_id, 0, 8);
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200420 memcpy(req->priv.initiator_port_id + 8,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100421 &target->srp_host->srp_dev->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800422 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800423
424 status = ib_send_cm_req(target->cm_id, &req->param);
425
426 kfree(req);
427
428 return status;
429}
430
431static void srp_disconnect_target(struct srp_target_port *target)
432{
433 /* XXX should send SRP_I_LOGOUT request */
434
435 init_completion(&target->done);
Roland Dreiere6581052006-05-17 09:13:21 -0700436 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500437 shost_printk(KERN_DEBUG, target->scsi_host,
438 PFX "Sending CM DREQ failed\n");
Roland Dreiere6581052006-05-17 09:13:21 -0700439 return;
440 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800441 wait_for_completion(&target->done);
442}
443
Bart Van Assche9709f0e2010-11-26 13:13:06 -0500444static bool srp_change_state(struct srp_target_port *target,
445 enum srp_target_state old,
446 enum srp_target_state new)
447{
448 bool changed = false;
449
450 spin_lock_irq(target->scsi_host->host_lock);
451 if (target->state == old) {
452 target->state = new;
453 changed = true;
454 }
455 spin_unlock_irq(target->scsi_host->host_lock);
456 return changed;
457}
458
David Howellsc4028952006-11-22 14:57:56 +0000459static void srp_remove_work(struct work_struct *work)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800460{
David Howellsc4028952006-11-22 14:57:56 +0000461 struct srp_target_port *target =
462 container_of(work, struct srp_target_port, work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800463
Bart Van Assche9709f0e2010-11-26 13:13:06 -0500464 if (!srp_change_state(target, SRP_TARGET_DEAD, SRP_TARGET_REMOVED))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800465 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800466
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700467 spin_lock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800468 list_del(&target->list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700469 spin_unlock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800470
FUJITA Tomonori32368222007-06-27 16:33:12 +0900471 srp_remove_host(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800472 scsi_remove_host(target->scsi_host);
473 ib_destroy_cm_id(target->cm_id);
474 srp_free_target_ib(target);
475 scsi_host_put(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800476}
477
478static int srp_connect_target(struct srp_target_port *target)
479{
David Dillow9fe4bcf2008-01-08 17:08:52 -0500480 int retries = 3;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800481 int ret;
482
483 ret = srp_lookup_path(target);
484 if (ret)
485 return ret;
486
487 while (1) {
488 init_completion(&target->done);
489 ret = srp_send_req(target);
490 if (ret)
491 return ret;
492 wait_for_completion(&target->done);
493
494 /*
495 * The CM event handling code will set status to
496 * SRP_PORT_REDIRECT if we get a port redirect REJ
497 * back, or SRP_DLID_REDIRECT if we get a lid/qp
498 * redirect REJ back.
499 */
500 switch (target->status) {
501 case 0:
502 return 0;
503
504 case SRP_PORT_REDIRECT:
505 ret = srp_lookup_path(target);
506 if (ret)
507 return ret;
508 break;
509
510 case SRP_DLID_REDIRECT:
511 break;
512
David Dillow9fe4bcf2008-01-08 17:08:52 -0500513 case SRP_STALE_CONN:
514 /* Our current CM id was stale, and is now in timewait.
515 * Try to reconnect with a new one.
516 */
517 if (!retries-- || srp_new_cm_id(target)) {
518 shost_printk(KERN_ERR, target->scsi_host, PFX
519 "giving up on stale connection\n");
520 target->status = -ECONNRESET;
521 return target->status;
522 }
523
524 shost_printk(KERN_ERR, target->scsi_host, PFX
525 "retrying stale connection\n");
526 break;
527
Roland Dreieraef9ec32005-11-02 14:07:13 -0800528 default:
529 return target->status;
530 }
531 }
532}
533
Roland Dreierd945e1d2006-05-09 10:50:28 -0700534static void srp_unmap_data(struct scsi_cmnd *scmnd,
535 struct srp_target_port *target,
536 struct srp_request *req)
537{
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900538 if (!scsi_sglist(scmnd) ||
Roland Dreierd945e1d2006-05-09 10:50:28 -0700539 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
540 scmnd->sc_data_direction != DMA_FROM_DEVICE))
541 return;
542
Roland Dreierf5358a12006-06-17 20:37:29 -0700543 if (req->fmr) {
544 ib_fmr_pool_unmap(req->fmr);
545 req->fmr = NULL;
546 }
547
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100548 ib_dma_unmap_sg(target->srp_host->srp_dev->dev, scsi_sglist(scmnd),
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900549 scsi_sg_count(scmnd), scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700550}
551
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700552static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
553{
554 srp_unmap_data(req->scmnd, target, req);
David Dillowf8b6e312010-11-26 13:02:21 -0500555 req->scmnd = NULL;
Bart Van Assche536ae142010-11-26 13:58:27 -0500556 list_add_tail(&req->list, &target->free_reqs);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700557}
558
559static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
560{
561 req->scmnd->result = DID_RESET << 16;
562 req->scmnd->scsi_done(req->scmnd);
563 srp_remove_req(target, req);
564}
565
Roland Dreieraef9ec32005-11-02 14:07:13 -0800566static int srp_reconnect_target(struct srp_target_port *target)
567{
Roland Dreieraef9ec32005-11-02 14:07:13 -0800568 struct ib_qp_attr qp_attr;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800569 struct ib_wc wc;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -0500570 int i, ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800571
Bart Van Assche9709f0e2010-11-26 13:13:06 -0500572 if (!srp_change_state(target, SRP_TARGET_LIVE, SRP_TARGET_CONNECTING))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800573 return -EAGAIN;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800574
575 srp_disconnect_target(target);
576 /*
577 * Now get a new local CM ID so that we avoid confusing the
578 * target in case things are really fouled up.
579 */
David Dillow9fe4bcf2008-01-08 17:08:52 -0500580 ret = srp_new_cm_id(target);
581 if (ret)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800582 goto err;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800583
584 qp_attr.qp_state = IB_QPS_RESET;
585 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
586 if (ret)
587 goto err;
588
589 ret = srp_init_qp(target, target->qp);
590 if (ret)
591 goto err;
592
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000593 while (ib_poll_cq(target->recv_cq, 1, &wc) > 0)
594 ; /* nothing */
595 while (ib_poll_cq(target->send_cq, 1, &wc) > 0)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800596 ; /* nothing */
597
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300598 spin_lock_irq(target->scsi_host->host_lock);
Bart Van Assche536ae142010-11-26 13:58:27 -0500599 for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
600 struct srp_request *req = &target->req_ring[i];
601 if (req->scmnd)
602 srp_reset_req(target, req);
603 }
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300604 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800605
Bart Van Assche536ae142010-11-26 13:58:27 -0500606 INIT_LIST_HEAD(&target->free_tx);
Bart Van Asschedcb4cb82010-11-26 13:22:48 -0500607 for (i = 0; i < SRP_SQ_SIZE; ++i)
Bart Van Assche536ae142010-11-26 13:58:27 -0500608 list_add(&target->tx_ring[i]->list, &target->free_tx);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800609
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200610 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800611 ret = srp_connect_target(target);
612 if (ret)
613 goto err;
614
Bart Van Assche9709f0e2010-11-26 13:13:06 -0500615 if (!srp_change_state(target, SRP_TARGET_CONNECTING, SRP_TARGET_LIVE))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800616 ret = -EAGAIN;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800617
618 return ret;
619
620err:
David Dillow7aa54bd2008-01-07 18:23:41 -0500621 shost_printk(KERN_ERR, target->scsi_host,
622 PFX "reconnect failed (%d), removing target port.\n", ret);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800623
624 /*
625 * We couldn't reconnect, so kill our target port off.
Bart Van Assche9709f0e2010-11-26 13:13:06 -0500626 * However, we have to defer the real removal because we
627 * are in the context of the SCSI error handler now, which
628 * will deadlock if we call scsi_remove_host().
629 *
630 * Schedule our work inside the lock to avoid a race with
631 * the flush_scheduled_work() in srp_remove_one().
Roland Dreieraef9ec32005-11-02 14:07:13 -0800632 */
633 spin_lock_irq(target->scsi_host->host_lock);
634 if (target->state == SRP_TARGET_CONNECTING) {
635 target->state = SRP_TARGET_DEAD;
David Howellsc4028952006-11-22 14:57:56 +0000636 INIT_WORK(&target->work, srp_remove_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800637 schedule_work(&target->work);
638 }
639 spin_unlock_irq(target->scsi_host->host_lock);
640
641 return ret;
642}
643
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700644static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat,
Roland Dreierf5358a12006-06-17 20:37:29 -0700645 int sg_cnt, struct srp_request *req,
646 struct srp_direct_buf *buf)
647{
648 u64 io_addr = 0;
649 u64 *dma_pages;
650 u32 len;
651 int page_cnt;
652 int i, j;
653 int ret;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100654 struct srp_device *dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800655 struct ib_device *ibdev = dev->dev;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900656 struct scatterlist *sg;
Roland Dreierf5358a12006-06-17 20:37:29 -0700657
658 if (!dev->fmr_pool)
659 return -ENODEV;
660
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700661 if (srp_target_is_mellanox(target) &&
662 (ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask))
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700663 return -EINVAL;
664
Roland Dreierf5358a12006-06-17 20:37:29 -0700665 len = page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900666 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
667 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800668
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900669 if (ib_sg_dma_address(ibdev, sg) & ~dev->fmr_page_mask) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700670 if (i > 0)
671 return -EINVAL;
672 else
673 ++page_cnt;
674 }
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900675 if ((ib_sg_dma_address(ibdev, sg) + dma_len) &
Roland Dreierf5358a12006-06-17 20:37:29 -0700676 ~dev->fmr_page_mask) {
677 if (i < sg_cnt - 1)
678 return -EINVAL;
679 else
680 ++page_cnt;
681 }
682
Ralph Campbell85507bc2006-12-12 14:30:55 -0800683 len += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700684 }
685
686 page_cnt += len >> dev->fmr_page_shift;
687 if (page_cnt > SRP_FMR_SIZE)
688 return -ENOMEM;
689
690 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
691 if (!dma_pages)
692 return -ENOMEM;
693
694 page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900695 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
696 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800697
698 for (j = 0; j < dma_len; j += dev->fmr_page_size)
Roland Dreierf5358a12006-06-17 20:37:29 -0700699 dma_pages[page_cnt++] =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900700 (ib_sg_dma_address(ibdev, sg) &
Ralph Campbell85507bc2006-12-12 14:30:55 -0800701 dev->fmr_page_mask) + j;
702 }
Roland Dreierf5358a12006-06-17 20:37:29 -0700703
704 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
Michael S. Tsirkinadfaa882006-07-14 00:23:55 -0700705 dma_pages, page_cnt, io_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -0700706 if (IS_ERR(req->fmr)) {
707 ret = PTR_ERR(req->fmr);
Vu Pham6583eb32006-07-14 00:23:53 -0700708 req->fmr = NULL;
Roland Dreierf5358a12006-06-17 20:37:29 -0700709 goto out;
710 }
711
Ralph Campbell85507bc2006-12-12 14:30:55 -0800712 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) &
713 ~dev->fmr_page_mask);
Roland Dreierf5358a12006-06-17 20:37:29 -0700714 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
715 buf->len = cpu_to_be32(len);
716
717 ret = 0;
718
719out:
720 kfree(dma_pages);
721
722 return ret;
723}
724
Roland Dreieraef9ec32005-11-02 14:07:13 -0800725static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
726 struct srp_request *req)
727{
Roland Dreiercf368712006-03-24 15:47:26 -0800728 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800729 struct srp_cmd *cmd = req->cmd->buf;
Roland Dreiercf368712006-03-24 15:47:26 -0800730 int len, nents, count;
Roland Dreierf5358a12006-06-17 20:37:29 -0700731 u8 fmt = SRP_DATA_DESC_DIRECT;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800732 struct srp_device *dev;
733 struct ib_device *ibdev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800734
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900735 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800736 return sizeof (struct srp_cmd);
737
738 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
739 scmnd->sc_data_direction != DMA_TO_DEVICE) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500740 shost_printk(KERN_WARNING, target->scsi_host,
741 PFX "Unhandled data direction %d\n",
742 scmnd->sc_data_direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800743 return -EINVAL;
744 }
745
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900746 nents = scsi_sg_count(scmnd);
747 scat = scsi_sglist(scmnd);
Roland Dreiercf368712006-03-24 15:47:26 -0800748
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100749 dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800750 ibdev = dev->dev;
751
752 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
Roland Dreierf5358a12006-06-17 20:37:29 -0700753
754 fmt = SRP_DATA_DESC_DIRECT;
755 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800756
757 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700758 /*
759 * The midlayer only generated a single gather/scatter
760 * entry, or DMA mapping coalesced everything to a
761 * single entry. So a direct descriptor along with
762 * the DMA MR suffices.
763 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800764 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800765
Ralph Campbell85507bc2006-12-12 14:30:55 -0800766 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
767 buf->key = cpu_to_be32(dev->mr->rkey);
768 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700769 } else if (srp_map_fmr(target, scat, count, req,
Roland Dreierf5358a12006-06-17 20:37:29 -0700770 (void *) cmd->add_data)) {
771 /*
772 * FMR mapping failed, and the scatterlist has more
773 * than one entry. Generate an indirect memory
774 * descriptor.
775 */
Roland Dreiercf368712006-03-24 15:47:26 -0800776 struct srp_indirect_buf *buf = (void *) cmd->add_data;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900777 struct scatterlist *sg;
Roland Dreiercf368712006-03-24 15:47:26 -0800778 u32 datalen = 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700779 int i;
Roland Dreiercf368712006-03-24 15:47:26 -0800780
781 fmt = SRP_DATA_DESC_INDIRECT;
Roland Dreierf5358a12006-06-17 20:37:29 -0700782 len = sizeof (struct srp_cmd) +
783 sizeof (struct srp_indirect_buf) +
784 count * sizeof (struct srp_direct_buf);
785
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900786 scsi_for_each_sg(scmnd, sg, count, i) {
787 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800788
Roland Dreierf5358a12006-06-17 20:37:29 -0700789 buf->desc_list[i].va =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900790 cpu_to_be64(ib_sg_dma_address(ibdev, sg));
Roland Dreierf5358a12006-06-17 20:37:29 -0700791 buf->desc_list[i].key =
Ralph Campbell85507bc2006-12-12 14:30:55 -0800792 cpu_to_be32(dev->mr->rkey);
793 buf->desc_list[i].len = cpu_to_be32(dma_len);
794 datalen += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700795 }
Roland Dreiercf368712006-03-24 15:47:26 -0800796
797 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
798 cmd->data_out_desc_cnt = count;
799 else
800 cmd->data_in_desc_cnt = count;
801
Roland Dreierf5358a12006-06-17 20:37:29 -0700802 buf->table_desc.va =
803 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800804 buf->table_desc.key =
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100805 cpu_to_be32(target->srp_host->srp_dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800806 buf->table_desc.len =
807 cpu_to_be32(count * sizeof (struct srp_direct_buf));
808
Roland Dreiercf368712006-03-24 15:47:26 -0800809 buf->len = cpu_to_be32(datalen);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800810 }
811
812 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
813 cmd->buf_fmt = fmt << 4;
814 else
815 cmd->buf_fmt = fmt;
816
Roland Dreieraef9ec32005-11-02 14:07:13 -0800817 return len;
818}
819
David Dillow05a1d752010-10-08 14:48:14 -0400820/*
Bart Van Assche76c75b22010-11-26 14:37:47 -0500821 * Return an IU and possible credit to the free pool
822 */
823static void srp_put_tx_iu(struct srp_target_port *target, struct srp_iu *iu,
824 enum srp_iu_type iu_type)
825{
826 unsigned long flags;
827
828 spin_lock_irqsave(target->scsi_host->host_lock, flags);
829 list_add(&iu->list, &target->free_tx);
830 if (iu_type != SRP_IU_RSP)
831 ++target->req_lim;
832 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
833}
834
835/*
David Dillow05a1d752010-10-08 14:48:14 -0400836 * Must be called with target->scsi_host->host_lock held to protect
Bart Van Assche76c75b22010-11-26 14:37:47 -0500837 * req_lim and free_tx. If IU is not sent, it must be returned using
838 * srp_put_tx_iu().
David Dillow05a1d752010-10-08 14:48:14 -0400839 *
840 * Note:
841 * An upper limit for the number of allocated information units for each
842 * request type is:
843 * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues
844 * more than Scsi_Host.can_queue requests.
845 * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE.
846 * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than
847 * one unanswered SRP request to an initiator.
848 */
849static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
850 enum srp_iu_type iu_type)
851{
852 s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE;
853 struct srp_iu *iu;
854
855 srp_send_completion(target->send_cq, target);
856
Bart Van Asschedcb4cb82010-11-26 13:22:48 -0500857 if (list_empty(&target->free_tx))
David Dillow05a1d752010-10-08 14:48:14 -0400858 return NULL;
859
860 /* Initiator responses to target requests do not consume credits */
Bart Van Assche76c75b22010-11-26 14:37:47 -0500861 if (iu_type != SRP_IU_RSP) {
862 if (target->req_lim <= rsv) {
863 ++target->zero_req_lim;
864 return NULL;
865 }
866
867 --target->req_lim;
David Dillow05a1d752010-10-08 14:48:14 -0400868 }
869
Bart Van Asschedcb4cb82010-11-26 13:22:48 -0500870 iu = list_first_entry(&target->free_tx, struct srp_iu, list);
Bart Van Assche76c75b22010-11-26 14:37:47 -0500871 list_del(&iu->list);
David Dillow05a1d752010-10-08 14:48:14 -0400872 return iu;
873}
874
Bart Van Assche76c75b22010-11-26 14:37:47 -0500875static int srp_post_send(struct srp_target_port *target,
876 struct srp_iu *iu, int len)
David Dillow05a1d752010-10-08 14:48:14 -0400877{
878 struct ib_sge list;
879 struct ib_send_wr wr, *bad_wr;
David Dillow05a1d752010-10-08 14:48:14 -0400880
881 list.addr = iu->dma;
882 list.length = len;
883 list.lkey = target->srp_host->srp_dev->mr->lkey;
884
885 wr.next = NULL;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -0500886 wr.wr_id = (uintptr_t) iu;
David Dillow05a1d752010-10-08 14:48:14 -0400887 wr.sg_list = &list;
888 wr.num_sge = 1;
889 wr.opcode = IB_WR_SEND;
890 wr.send_flags = IB_SEND_SIGNALED;
891
Bart Van Assche76c75b22010-11-26 14:37:47 -0500892 return ib_post_send(target->qp, &wr, &bad_wr);
David Dillow05a1d752010-10-08 14:48:14 -0400893}
894
Bart Van Asschedcb4cb82010-11-26 13:22:48 -0500895static int srp_post_recv(struct srp_target_port *target, struct srp_iu *iu)
Bart Van Asschec996bb42010-07-30 10:59:05 +0000896{
Bart Van Asschec996bb42010-07-30 10:59:05 +0000897 struct ib_recv_wr wr, *bad_wr;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -0500898 struct ib_sge list;
Bart Van Asschec996bb42010-07-30 10:59:05 +0000899
900 list.addr = iu->dma;
901 list.length = iu->size;
902 list.lkey = target->srp_host->srp_dev->mr->lkey;
903
904 wr.next = NULL;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -0500905 wr.wr_id = (uintptr_t) iu;
Bart Van Asschec996bb42010-07-30 10:59:05 +0000906 wr.sg_list = &list;
907 wr.num_sge = 1;
908
Bart Van Asschedcb4cb82010-11-26 13:22:48 -0500909 return ib_post_recv(target->qp, &wr, &bad_wr);
Bart Van Asschec996bb42010-07-30 10:59:05 +0000910}
911
Roland Dreieraef9ec32005-11-02 14:07:13 -0800912static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
913{
914 struct srp_request *req;
915 struct scsi_cmnd *scmnd;
916 unsigned long flags;
917 s32 delta;
918
919 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
920
921 spin_lock_irqsave(target->scsi_host->host_lock, flags);
922
923 target->req_lim += delta;
924
Roland Dreieraef9ec32005-11-02 14:07:13 -0800925 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
David Dillowf8b6e312010-11-26 13:02:21 -0500926 target->tsk_mgmt_status = -1;
927 if (be32_to_cpu(rsp->resp_data_len) >= 4)
928 target->tsk_mgmt_status = rsp->data[3];
929 complete(&target->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800930 } else {
David Dillowf8b6e312010-11-26 13:02:21 -0500931 req = &target->req_ring[rsp->tag];
Roland Dreierd945e1d2006-05-09 10:50:28 -0700932 scmnd = req->scmnd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800933 if (!scmnd)
David Dillow7aa54bd2008-01-07 18:23:41 -0500934 shost_printk(KERN_ERR, target->scsi_host,
935 "Null scmnd for RSP w/tag %016llx\n",
936 (unsigned long long) rsp->tag);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800937 scmnd->result = rsp->status;
938
939 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
940 memcpy(scmnd->sense_buffer, rsp->data +
941 be32_to_cpu(rsp->resp_data_len),
942 min_t(int, be32_to_cpu(rsp->sense_data_len),
943 SCSI_SENSE_BUFFERSIZE));
944 }
945
946 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900947 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800948 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900949 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800950
David Dillowf8b6e312010-11-26 13:02:21 -0500951 scmnd->host_scribble = NULL;
952 scmnd->scsi_done(scmnd);
953 srp_remove_req(target, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800954 }
955
956 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
957}
958
David Dillowbb125882010-10-08 14:40:47 -0400959static int srp_response_common(struct srp_target_port *target, s32 req_delta,
960 void *rsp, int len)
961{
Bart Van Assche76c75b22010-11-26 14:37:47 -0500962 struct ib_device *dev = target->srp_host->srp_dev->dev;
David Dillowbb125882010-10-08 14:40:47 -0400963 unsigned long flags;
964 struct srp_iu *iu;
Bart Van Assche76c75b22010-11-26 14:37:47 -0500965 int err;
David Dillowbb125882010-10-08 14:40:47 -0400966
967 spin_lock_irqsave(target->scsi_host->host_lock, flags);
968 target->req_lim += req_delta;
David Dillowbb125882010-10-08 14:40:47 -0400969 iu = __srp_get_tx_iu(target, SRP_IU_RSP);
Bart Van Assche76c75b22010-11-26 14:37:47 -0500970 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
971
David Dillowbb125882010-10-08 14:40:47 -0400972 if (!iu) {
973 shost_printk(KERN_ERR, target->scsi_host, PFX
974 "no IU available to send response\n");
Bart Van Assche76c75b22010-11-26 14:37:47 -0500975 return 1;
David Dillowbb125882010-10-08 14:40:47 -0400976 }
977
978 ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE);
979 memcpy(iu->buf, rsp, len);
980 ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE);
981
Bart Van Assche76c75b22010-11-26 14:37:47 -0500982 err = srp_post_send(target, iu, len);
983 if (err) {
David Dillowbb125882010-10-08 14:40:47 -0400984 shost_printk(KERN_ERR, target->scsi_host, PFX
985 "unable to post response: %d\n", err);
Bart Van Assche76c75b22010-11-26 14:37:47 -0500986 srp_put_tx_iu(target, iu, SRP_IU_RSP);
987 }
David Dillowbb125882010-10-08 14:40:47 -0400988
David Dillowbb125882010-10-08 14:40:47 -0400989 return err;
990}
991
992static void srp_process_cred_req(struct srp_target_port *target,
993 struct srp_cred_req *req)
994{
995 struct srp_cred_rsp rsp = {
996 .opcode = SRP_CRED_RSP,
997 .tag = req->tag,
998 };
999 s32 delta = be32_to_cpu(req->req_lim_delta);
1000
1001 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1002 shost_printk(KERN_ERR, target->scsi_host, PFX
1003 "problems processing SRP_CRED_REQ\n");
1004}
1005
1006static void srp_process_aer_req(struct srp_target_port *target,
1007 struct srp_aer_req *req)
1008{
1009 struct srp_aer_rsp rsp = {
1010 .opcode = SRP_AER_RSP,
1011 .tag = req->tag,
1012 };
1013 s32 delta = be32_to_cpu(req->req_lim_delta);
1014
1015 shost_printk(KERN_ERR, target->scsi_host, PFX
1016 "ignoring AER for LUN %llu\n", be64_to_cpu(req->lun));
1017
1018 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1019 shost_printk(KERN_ERR, target->scsi_host, PFX
1020 "problems processing SRP_AER_REQ\n");
1021}
1022
Roland Dreieraef9ec32005-11-02 14:07:13 -08001023static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
1024{
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001025 struct ib_device *dev = target->srp_host->srp_dev->dev;
1026 struct srp_iu *iu = (struct srp_iu *) wc->wr_id;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001027 int res;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001028 u8 opcode;
1029
Ralph Campbell85507bc2006-12-12 14:30:55 -08001030 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
1031 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001032
1033 opcode = *(u8 *) iu->buf;
1034
1035 if (0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001036 shost_printk(KERN_ERR, target->scsi_host,
1037 PFX "recv completion, opcode 0x%02x\n", opcode);
Bart Van Assche7a700812010-07-29 15:56:37 +00001038 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1,
1039 iu->buf, wc->byte_len, true);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001040 }
1041
1042 switch (opcode) {
1043 case SRP_RSP:
1044 srp_process_rsp(target, iu->buf);
1045 break;
1046
David Dillowbb125882010-10-08 14:40:47 -04001047 case SRP_CRED_REQ:
1048 srp_process_cred_req(target, iu->buf);
1049 break;
1050
1051 case SRP_AER_REQ:
1052 srp_process_aer_req(target, iu->buf);
1053 break;
1054
Roland Dreieraef9ec32005-11-02 14:07:13 -08001055 case SRP_T_LOGOUT:
1056 /* XXX Handle target logout */
David Dillow7aa54bd2008-01-07 18:23:41 -05001057 shost_printk(KERN_WARNING, target->scsi_host,
1058 PFX "Got target logout request\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001059 break;
1060
1061 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001062 shost_printk(KERN_WARNING, target->scsi_host,
1063 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001064 break;
1065 }
1066
Ralph Campbell85507bc2006-12-12 14:30:55 -08001067 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
1068 DMA_FROM_DEVICE);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001069
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001070 res = srp_post_recv(target, iu);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001071 if (res != 0)
1072 shost_printk(KERN_ERR, target->scsi_host,
1073 PFX "Recv failed with error code %d\n", res);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001074}
1075
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001076static void srp_recv_completion(struct ib_cq *cq, void *target_ptr)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001077{
1078 struct srp_target_port *target = target_ptr;
1079 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001080
1081 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1082 while (ib_poll_cq(cq, 1, &wc) > 0) {
1083 if (wc.status) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001084 shost_printk(KERN_ERR, target->scsi_host,
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001085 PFX "failed receive status %d\n",
David Dillow7aa54bd2008-01-07 18:23:41 -05001086 wc.status);
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001087 target->qp_in_error = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001088 break;
1089 }
1090
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001091 srp_handle_recv(target, &wc);
1092 }
1093}
1094
1095static void srp_send_completion(struct ib_cq *cq, void *target_ptr)
1096{
1097 struct srp_target_port *target = target_ptr;
1098 struct ib_wc wc;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001099 struct srp_iu *iu;
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001100
1101 while (ib_poll_cq(cq, 1, &wc) > 0) {
1102 if (wc.status) {
1103 shost_printk(KERN_ERR, target->scsi_host,
1104 PFX "failed send status %d\n",
1105 wc.status);
1106 target->qp_in_error = 1;
1107 break;
1108 }
1109
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001110 iu = (struct srp_iu *) wc.wr_id;
1111 list_add(&iu->list, &target->free_tx);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001112 }
1113}
1114
Bart Van Assche76c75b22010-11-26 14:37:47 -05001115static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001116{
Bart Van Assche76c75b22010-11-26 14:37:47 -05001117 struct srp_target_port *target = host_to_target(shost);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001118 struct srp_request *req;
1119 struct srp_iu *iu;
1120 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001121 struct ib_device *dev;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001122 unsigned long flags;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001123 int len;
1124
1125 if (target->state == SRP_TARGET_CONNECTING)
1126 goto err;
1127
1128 if (target->state == SRP_TARGET_DEAD ||
1129 target->state == SRP_TARGET_REMOVED) {
1130 scmnd->result = DID_BAD_TARGET << 16;
Bart Van Assche76c75b22010-11-26 14:37:47 -05001131 scmnd->scsi_done(scmnd);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001132 return 0;
1133 }
1134
Bart Van Assche76c75b22010-11-26 14:37:47 -05001135 spin_lock_irqsave(shost->host_lock, flags);
David Dillowbb125882010-10-08 14:40:47 -04001136 iu = __srp_get_tx_iu(target, SRP_IU_CMD);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001137 if (iu) {
1138 req = list_first_entry(&target->free_reqs, struct srp_request,
1139 list);
1140 list_del(&req->list);
1141 }
1142 spin_unlock_irqrestore(shost->host_lock, flags);
1143
Roland Dreieraef9ec32005-11-02 14:07:13 -08001144 if (!iu)
1145 goto err;
1146
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001147 dev = target->srp_host->srp_dev->dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001148 ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len,
1149 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001150
Roland Dreieraef9ec32005-11-02 14:07:13 -08001151 scmnd->result = 0;
David Dillowf8b6e312010-11-26 13:02:21 -05001152 scmnd->host_scribble = (void *) req;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001153
1154 cmd = iu->buf;
1155 memset(cmd, 0, sizeof *cmd);
1156
1157 cmd->opcode = SRP_CMD;
1158 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001159 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001160 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1161
Roland Dreieraef9ec32005-11-02 14:07:13 -08001162 req->scmnd = scmnd;
1163 req->cmd = iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001164
1165 len = srp_map_data(scmnd, target, req);
1166 if (len < 0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001167 shost_printk(KERN_ERR, target->scsi_host,
1168 PFX "Failed to map data\n");
Bart Van Assche76c75b22010-11-26 14:37:47 -05001169 goto err_iu;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001170 }
1171
Ralph Campbell85507bc2006-12-12 14:30:55 -08001172 ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len,
1173 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001174
Bart Van Assche76c75b22010-11-26 14:37:47 -05001175 if (srp_post_send(target, iu, len)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001176 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001177 goto err_unmap;
1178 }
1179
Roland Dreieraef9ec32005-11-02 14:07:13 -08001180 return 0;
1181
1182err_unmap:
1183 srp_unmap_data(scmnd, target, req);
1184
Bart Van Assche76c75b22010-11-26 14:37:47 -05001185err_iu:
1186 srp_put_tx_iu(target, iu, SRP_IU_CMD);
1187
1188 spin_lock_irqsave(shost->host_lock, flags);
1189 list_add(&req->list, &target->free_reqs);
1190 spin_unlock_irqrestore(shost->host_lock, flags);
1191
Roland Dreieraef9ec32005-11-02 14:07:13 -08001192err:
1193 return SCSI_MLQUEUE_HOST_BUSY;
1194}
1195
1196static int srp_alloc_iu_bufs(struct srp_target_port *target)
1197{
1198 int i;
1199
1200 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1201 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1202 target->max_ti_iu_len,
1203 GFP_KERNEL, DMA_FROM_DEVICE);
1204 if (!target->rx_ring[i])
1205 goto err;
1206 }
1207
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001208 for (i = 0; i < SRP_SQ_SIZE; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001209 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
Vu Pham74b0a152006-06-17 20:37:32 -07001210 srp_max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001211 GFP_KERNEL, DMA_TO_DEVICE);
1212 if (!target->tx_ring[i])
1213 goto err;
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001214
1215 list_add(&target->tx_ring[i]->list, &target->free_tx);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001216 }
1217
1218 return 0;
1219
1220err:
1221 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1222 srp_free_iu(target->srp_host, target->rx_ring[i]);
1223 target->rx_ring[i] = NULL;
1224 }
1225
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001226 for (i = 0; i < SRP_SQ_SIZE; ++i) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001227 srp_free_iu(target->srp_host, target->tx_ring[i]);
1228 target->tx_ring[i] = NULL;
1229 }
1230
1231 return -ENOMEM;
1232}
1233
1234static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1235 struct ib_cm_event *event,
1236 struct srp_target_port *target)
1237{
David Dillow7aa54bd2008-01-07 18:23:41 -05001238 struct Scsi_Host *shost = target->scsi_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001239 struct ib_class_port_info *cpi;
1240 int opcode;
1241
1242 switch (event->param.rej_rcvd.reason) {
1243 case IB_CM_REJ_PORT_CM_REDIRECT:
1244 cpi = event->param.rej_rcvd.ari;
1245 target->path.dlid = cpi->redirect_lid;
1246 target->path.pkey = cpi->redirect_pkey;
1247 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1248 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1249
1250 target->status = target->path.dlid ?
1251 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1252 break;
1253
1254 case IB_CM_REJ_PORT_REDIRECT:
Roland Dreier5d7cbfd2007-08-03 10:45:18 -07001255 if (srp_target_is_topspin(target)) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001256 /*
1257 * Topspin/Cisco SRP gateways incorrectly send
1258 * reject reason code 25 when they mean 24
1259 * (port redirect).
1260 */
1261 memcpy(target->path.dgid.raw,
1262 event->param.rej_rcvd.ari, 16);
1263
David Dillow7aa54bd2008-01-07 18:23:41 -05001264 shost_printk(KERN_DEBUG, shost,
1265 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1266 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1267 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001268
1269 target->status = SRP_PORT_REDIRECT;
1270 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001271 shost_printk(KERN_WARNING, shost,
1272 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001273 target->status = -ECONNRESET;
1274 }
1275 break;
1276
1277 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
David Dillow7aa54bd2008-01-07 18:23:41 -05001278 shost_printk(KERN_WARNING, shost,
1279 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001280 target->status = -ECONNRESET;
1281 break;
1282
1283 case IB_CM_REJ_CONSUMER_DEFINED:
1284 opcode = *(u8 *) event->private_data;
1285 if (opcode == SRP_LOGIN_REJ) {
1286 struct srp_login_rej *rej = event->private_data;
1287 u32 reason = be32_to_cpu(rej->reason);
1288
1289 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
David Dillow7aa54bd2008-01-07 18:23:41 -05001290 shost_printk(KERN_WARNING, shost,
1291 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001292 else
David Dillow7aa54bd2008-01-07 18:23:41 -05001293 shost_printk(KERN_WARNING, shost,
1294 PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001295 } else
David Dillow7aa54bd2008-01-07 18:23:41 -05001296 shost_printk(KERN_WARNING, shost,
1297 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1298 " opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001299 target->status = -ECONNRESET;
1300 break;
1301
David Dillow9fe4bcf2008-01-08 17:08:52 -05001302 case IB_CM_REJ_STALE_CONN:
1303 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
1304 target->status = SRP_STALE_CONN;
1305 break;
1306
Roland Dreieraef9ec32005-11-02 14:07:13 -08001307 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001308 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
1309 event->param.rej_rcvd.reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001310 target->status = -ECONNRESET;
1311 }
1312}
1313
1314static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1315{
1316 struct srp_target_port *target = cm_id->context;
1317 struct ib_qp_attr *qp_attr = NULL;
1318 int attr_mask = 0;
1319 int comp = 0;
1320 int opcode = 0;
Bart Van Asschec996bb42010-07-30 10:59:05 +00001321 int i;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001322
1323 switch (event->event) {
1324 case IB_CM_REQ_ERROR:
David Dillow7aa54bd2008-01-07 18:23:41 -05001325 shost_printk(KERN_DEBUG, target->scsi_host,
1326 PFX "Sending CM REQ failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001327 comp = 1;
1328 target->status = -ECONNRESET;
1329 break;
1330
1331 case IB_CM_REP_RECEIVED:
1332 comp = 1;
1333 opcode = *(u8 *) event->private_data;
1334
1335 if (opcode == SRP_LOGIN_RSP) {
1336 struct srp_login_rsp *rsp = event->private_data;
1337
1338 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1339 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1340
Bart Van Assche7ade4002010-08-30 19:27:36 +00001341 /*
1342 * Reserve credits for task management so we don't
1343 * bounce requests back to the SCSI mid-layer.
1344 */
1345 target->scsi_host->can_queue
1346 = min(target->req_lim - SRP_TSK_MGMT_SQ_SIZE,
1347 target->scsi_host->can_queue);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001348 } 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++) {
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001377 struct srp_iu *iu = target->rx_ring[i];
1378 target->status = srp_post_recv(target, iu);
Bart Van Asschec996bb42010-07-30 10:59:05 +00001379 if (target->status)
1380 break;
1381 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001382 if (target->status)
1383 break;
1384
1385 qp_attr->qp_state = IB_QPS_RTS;
1386 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1387 if (target->status)
1388 break;
1389
1390 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1391 if (target->status)
1392 break;
1393
1394 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1395 if (target->status)
1396 break;
1397
1398 break;
1399
1400 case IB_CM_REJ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001401 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001402 comp = 1;
1403
1404 srp_cm_rej_handler(cm_id, event, target);
1405 break;
1406
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001407 case IB_CM_DREQ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001408 shost_printk(KERN_WARNING, target->scsi_host,
1409 PFX "DREQ received - connection closed\n");
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001410 if (ib_send_cm_drep(cm_id, NULL, 0))
David Dillow7aa54bd2008-01-07 18:23:41 -05001411 shost_printk(KERN_ERR, target->scsi_host,
1412 PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001413 break;
1414
1415 case IB_CM_TIMEWAIT_EXIT:
David Dillow7aa54bd2008-01-07 18:23:41 -05001416 shost_printk(KERN_ERR, target->scsi_host,
1417 PFX "connection closed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001418
1419 comp = 1;
1420 target->status = 0;
1421 break;
1422
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001423 case IB_CM_MRA_RECEIVED:
1424 case IB_CM_DREQ_ERROR:
1425 case IB_CM_DREP_RECEIVED:
1426 break;
1427
Roland Dreieraef9ec32005-11-02 14:07:13 -08001428 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001429 shost_printk(KERN_WARNING, target->scsi_host,
1430 PFX "Unhandled CM event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001431 break;
1432 }
1433
1434 if (comp)
1435 complete(&target->done);
1436
1437 kfree(qp_attr);
1438
1439 return 0;
1440}
1441
Roland Dreierd945e1d2006-05-09 10:50:28 -07001442static int srp_send_tsk_mgmt(struct srp_target_port *target,
David Dillowf8b6e312010-11-26 13:02:21 -05001443 u64 req_tag, unsigned int lun, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001444{
David Dillow19081f32010-10-18 08:54:49 -04001445 struct ib_device *dev = target->srp_host->srp_dev->dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001446 struct srp_iu *iu;
1447 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001448
Roland Dreier1285b3a2006-03-03 15:47:25 -08001449 if (target->state == SRP_TARGET_DEAD ||
David Dillowf8b6e312010-11-26 13:02:21 -05001450 target->state == SRP_TARGET_REMOVED)
Bart Van Assche76c75b22010-11-26 14:37:47 -05001451 return -1;
Roland Dreier1285b3a2006-03-03 15:47:25 -08001452
David Dillowf8b6e312010-11-26 13:02:21 -05001453 init_completion(&target->tsk_mgmt_done);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001454
Bart Van Assche76c75b22010-11-26 14:37:47 -05001455 spin_lock_irq(target->scsi_host->host_lock);
David Dillowbb125882010-10-08 14:40:47 -04001456 iu = __srp_get_tx_iu(target, SRP_IU_TSK_MGMT);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001457 spin_unlock_irq(target->scsi_host->host_lock);
1458
Roland Dreieraef9ec32005-11-02 14:07:13 -08001459 if (!iu)
Bart Van Assche76c75b22010-11-26 14:37:47 -05001460 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001461
David Dillow19081f32010-10-18 08:54:49 -04001462 ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt,
1463 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001464 tsk_mgmt = iu->buf;
1465 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1466
1467 tsk_mgmt->opcode = SRP_TSK_MGMT;
David Dillowf8b6e312010-11-26 13:02:21 -05001468 tsk_mgmt->lun = cpu_to_be64((u64) lun << 48);
1469 tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001470 tsk_mgmt->tsk_mgmt_func = func;
David Dillowf8b6e312010-11-26 13:02:21 -05001471 tsk_mgmt->task_tag = req_tag;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001472
David Dillow19081f32010-10-18 08:54:49 -04001473 ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt,
1474 DMA_TO_DEVICE);
Bart Van Assche76c75b22010-11-26 14:37:47 -05001475 if (srp_post_send(target, iu, sizeof *tsk_mgmt)) {
1476 srp_put_tx_iu(target, iu, SRP_IU_TSK_MGMT);
1477 return -1;
1478 }
Roland Dreierd945e1d2006-05-09 10:50:28 -07001479
David Dillowf8b6e312010-11-26 13:02:21 -05001480 if (!wait_for_completion_timeout(&target->tsk_mgmt_done,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001481 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001482 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001483
Roland Dreierd945e1d2006-05-09 10:50:28 -07001484 return 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001485}
1486
Roland Dreieraef9ec32005-11-02 14:07:13 -08001487static int srp_abort(struct scsi_cmnd *scmnd)
1488{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001489 struct srp_target_port *target = host_to_target(scmnd->device->host);
David Dillowf8b6e312010-11-26 13:02:21 -05001490 struct srp_request *req = (struct srp_request *) scmnd->host_scribble;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001491 int ret = SUCCESS;
1492
David Dillow7aa54bd2008-01-07 18:23:41 -05001493 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001494
David Dillowf8b6e312010-11-26 13:02:21 -05001495 if (!req || target->qp_in_error)
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001496 return FAILED;
David Dillowf8b6e312010-11-26 13:02:21 -05001497 if (srp_send_tsk_mgmt(target, req->index, scmnd->device->lun,
1498 SRP_TSK_ABORT_TASK))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001499 return FAILED;
1500
1501 spin_lock_irq(target->scsi_host->host_lock);
1502
David Dillowf8b6e312010-11-26 13:02:21 -05001503 if (req->scmnd) {
1504 if (!target->tsk_mgmt_status) {
1505 srp_remove_req(target, req);
1506 scmnd->result = DID_ABORT << 16;
1507 } else
1508 ret = FAILED;
1509 }
Roland Dreierd945e1d2006-05-09 10:50:28 -07001510
1511 spin_unlock_irq(target->scsi_host->host_lock);
1512
1513 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001514}
1515
1516static int srp_reset_device(struct scsi_cmnd *scmnd)
1517{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001518 struct srp_target_port *target = host_to_target(scmnd->device->host);
Bart Van Assche536ae142010-11-26 13:58:27 -05001519 int i;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001520
David Dillow7aa54bd2008-01-07 18:23:41 -05001521 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001522
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001523 if (target->qp_in_error)
1524 return FAILED;
David Dillowf8b6e312010-11-26 13:02:21 -05001525 if (srp_send_tsk_mgmt(target, SRP_TAG_NO_REQ, scmnd->device->lun,
1526 SRP_TSK_LUN_RESET))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001527 return FAILED;
David Dillowf8b6e312010-11-26 13:02:21 -05001528 if (target->tsk_mgmt_status)
Roland Dreierd945e1d2006-05-09 10:50:28 -07001529 return FAILED;
1530
1531 spin_lock_irq(target->scsi_host->host_lock);
1532
Bart Van Assche536ae142010-11-26 13:58:27 -05001533 for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
1534 struct srp_request *req = &target->req_ring[i];
David Dillowf8b6e312010-11-26 13:02:21 -05001535 if (req->scmnd && req->scmnd->device == scmnd->device)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001536 srp_reset_req(target, req);
Bart Van Assche536ae142010-11-26 13:58:27 -05001537 }
Roland Dreierd945e1d2006-05-09 10:50:28 -07001538
1539 spin_unlock_irq(target->scsi_host->host_lock);
1540
1541 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001542}
1543
1544static int srp_reset_host(struct scsi_cmnd *scmnd)
1545{
1546 struct srp_target_port *target = host_to_target(scmnd->device->host);
1547 int ret = FAILED;
1548
David Dillow7aa54bd2008-01-07 18:23:41 -05001549 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001550
1551 if (!srp_reconnect_target(target))
1552 ret = SUCCESS;
1553
1554 return ret;
1555}
1556
Tony Jonesee959b02008-02-22 00:13:36 +01001557static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
1558 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001559{
Tony Jonesee959b02008-02-22 00:13:36 +01001560 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001561
1562 if (target->state == SRP_TARGET_DEAD ||
1563 target->state == SRP_TARGET_REMOVED)
1564 return -ENODEV;
1565
1566 return sprintf(buf, "0x%016llx\n",
1567 (unsigned long long) be64_to_cpu(target->id_ext));
1568}
1569
Tony Jonesee959b02008-02-22 00:13:36 +01001570static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
1571 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001572{
Tony Jonesee959b02008-02-22 00:13:36 +01001573 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001574
1575 if (target->state == SRP_TARGET_DEAD ||
1576 target->state == SRP_TARGET_REMOVED)
1577 return -ENODEV;
1578
1579 return sprintf(buf, "0x%016llx\n",
1580 (unsigned long long) be64_to_cpu(target->ioc_guid));
1581}
1582
Tony Jonesee959b02008-02-22 00:13:36 +01001583static ssize_t show_service_id(struct device *dev,
1584 struct device_attribute *attr, char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001585{
Tony Jonesee959b02008-02-22 00:13:36 +01001586 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001587
1588 if (target->state == SRP_TARGET_DEAD ||
1589 target->state == SRP_TARGET_REMOVED)
1590 return -ENODEV;
1591
1592 return sprintf(buf, "0x%016llx\n",
1593 (unsigned long long) be64_to_cpu(target->service_id));
1594}
1595
Tony Jonesee959b02008-02-22 00:13:36 +01001596static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
1597 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001598{
Tony Jonesee959b02008-02-22 00:13:36 +01001599 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001600
1601 if (target->state == SRP_TARGET_DEAD ||
1602 target->state == SRP_TARGET_REMOVED)
1603 return -ENODEV;
1604
1605 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1606}
1607
Tony Jonesee959b02008-02-22 00:13:36 +01001608static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
1609 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001610{
Tony Jonesee959b02008-02-22 00:13:36 +01001611 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001612
1613 if (target->state == SRP_TARGET_DEAD ||
1614 target->state == SRP_TARGET_REMOVED)
1615 return -ENODEV;
1616
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001617 return sprintf(buf, "%pI6\n", target->path.dgid.raw);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001618}
1619
Tony Jonesee959b02008-02-22 00:13:36 +01001620static ssize_t show_orig_dgid(struct device *dev,
1621 struct device_attribute *attr, char *buf)
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001622{
Tony Jonesee959b02008-02-22 00:13:36 +01001623 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001624
1625 if (target->state == SRP_TARGET_DEAD ||
1626 target->state == SRP_TARGET_REMOVED)
1627 return -ENODEV;
1628
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001629 return sprintf(buf, "%pI6\n", target->orig_dgid);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001630}
1631
Bart Van Assche89de7482010-08-03 14:08:45 +00001632static ssize_t show_req_lim(struct device *dev,
1633 struct device_attribute *attr, char *buf)
1634{
1635 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1636
1637 if (target->state == SRP_TARGET_DEAD ||
1638 target->state == SRP_TARGET_REMOVED)
1639 return -ENODEV;
1640
1641 return sprintf(buf, "%d\n", target->req_lim);
1642}
1643
Tony Jonesee959b02008-02-22 00:13:36 +01001644static ssize_t show_zero_req_lim(struct device *dev,
1645 struct device_attribute *attr, char *buf)
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001646{
Tony Jonesee959b02008-02-22 00:13:36 +01001647 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001648
1649 if (target->state == SRP_TARGET_DEAD ||
1650 target->state == SRP_TARGET_REMOVED)
1651 return -ENODEV;
1652
1653 return sprintf(buf, "%d\n", target->zero_req_lim);
1654}
1655
Tony Jonesee959b02008-02-22 00:13:36 +01001656static ssize_t show_local_ib_port(struct device *dev,
1657 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001658{
Tony Jonesee959b02008-02-22 00:13:36 +01001659 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001660
1661 return sprintf(buf, "%d\n", target->srp_host->port);
1662}
1663
Tony Jonesee959b02008-02-22 00:13:36 +01001664static ssize_t show_local_ib_device(struct device *dev,
1665 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001666{
Tony Jonesee959b02008-02-22 00:13:36 +01001667 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001668
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001669 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001670}
1671
Tony Jonesee959b02008-02-22 00:13:36 +01001672static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1673static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1674static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1675static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1676static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1677static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
Bart Van Assche89de7482010-08-03 14:08:45 +00001678static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01001679static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1680static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1681static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001682
Tony Jonesee959b02008-02-22 00:13:36 +01001683static struct device_attribute *srp_host_attrs[] = {
1684 &dev_attr_id_ext,
1685 &dev_attr_ioc_guid,
1686 &dev_attr_service_id,
1687 &dev_attr_pkey,
1688 &dev_attr_dgid,
1689 &dev_attr_orig_dgid,
Bart Van Assche89de7482010-08-03 14:08:45 +00001690 &dev_attr_req_lim,
Tony Jonesee959b02008-02-22 00:13:36 +01001691 &dev_attr_zero_req_lim,
1692 &dev_attr_local_ib_port,
1693 &dev_attr_local_ib_device,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001694 NULL
1695};
1696
Roland Dreieraef9ec32005-11-02 14:07:13 -08001697static struct scsi_host_template srp_template = {
1698 .module = THIS_MODULE,
Roland Dreierb7f008f2007-05-06 21:18:11 -07001699 .name = "InfiniBand SRP initiator",
1700 .proc_name = DRV_NAME,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001701 .info = srp_target_info,
1702 .queuecommand = srp_queuecommand,
1703 .eh_abort_handler = srp_abort,
1704 .eh_device_reset_handler = srp_reset_device,
1705 .eh_host_reset_handler = srp_reset_host,
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001706 .can_queue = SRP_CMD_SQ_SIZE,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001707 .this_id = -1,
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001708 .cmd_per_lun = SRP_CMD_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001709 .use_clustering = ENABLE_CLUSTERING,
1710 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001711};
1712
1713static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1714{
FUJITA Tomonori32368222007-06-27 16:33:12 +09001715 struct srp_rport_identifiers ids;
1716 struct srp_rport *rport;
1717
Roland Dreieraef9ec32005-11-02 14:07:13 -08001718 sprintf(target->target_name, "SRP.T10:%016llX",
1719 (unsigned long long) be64_to_cpu(target->id_ext));
1720
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001721 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001722 return -ENODEV;
1723
FUJITA Tomonori32368222007-06-27 16:33:12 +09001724 memcpy(ids.port_id, &target->id_ext, 8);
1725 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09001726 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori32368222007-06-27 16:33:12 +09001727 rport = srp_rport_add(target->scsi_host, &ids);
1728 if (IS_ERR(rport)) {
1729 scsi_remove_host(target->scsi_host);
1730 return PTR_ERR(rport);
1731 }
1732
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001733 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001734 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001735 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001736
1737 target->state = SRP_TARGET_LIVE;
1738
Roland Dreieraef9ec32005-11-02 14:07:13 -08001739 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07001740 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001741
1742 return 0;
1743}
1744
Tony Jonesee959b02008-02-22 00:13:36 +01001745static void srp_release_dev(struct device *dev)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001746{
1747 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01001748 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001749
1750 complete(&host->released);
1751}
1752
1753static struct class srp_class = {
1754 .name = "infiniband_srp",
Tony Jonesee959b02008-02-22 00:13:36 +01001755 .dev_release = srp_release_dev
Roland Dreieraef9ec32005-11-02 14:07:13 -08001756};
1757
1758/*
1759 * Target ports are added by writing
1760 *
1761 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1762 * pkey=<P_Key>,service_id=<service ID>
1763 *
1764 * to the add_target sysfs attribute.
1765 */
1766enum {
1767 SRP_OPT_ERR = 0,
1768 SRP_OPT_ID_EXT = 1 << 0,
1769 SRP_OPT_IOC_GUID = 1 << 1,
1770 SRP_OPT_DGID = 1 << 2,
1771 SRP_OPT_PKEY = 1 << 3,
1772 SRP_OPT_SERVICE_ID = 1 << 4,
1773 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07001774 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450d2006-06-17 20:37:38 -07001775 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001776 SRP_OPT_INITIATOR_EXT = 1 << 8,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001777 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1778 SRP_OPT_IOC_GUID |
1779 SRP_OPT_DGID |
1780 SRP_OPT_PKEY |
1781 SRP_OPT_SERVICE_ID),
1782};
1783
Steven Whitehousea447c092008-10-13 10:46:57 +01001784static const match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07001785 { SRP_OPT_ID_EXT, "id_ext=%s" },
1786 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1787 { SRP_OPT_DGID, "dgid=%s" },
1788 { SRP_OPT_PKEY, "pkey=%x" },
1789 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1790 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1791 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450d2006-06-17 20:37:38 -07001792 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001793 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
Vu Pham52fb2b502006-06-17 20:37:31 -07001794 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001795};
1796
1797static int srp_parse_options(const char *buf, struct srp_target_port *target)
1798{
1799 char *options, *sep_opt;
1800 char *p;
1801 char dgid[3];
1802 substring_t args[MAX_OPT_ARGS];
1803 int opt_mask = 0;
1804 int token;
1805 int ret = -EINVAL;
1806 int i;
1807
1808 options = kstrdup(buf, GFP_KERNEL);
1809 if (!options)
1810 return -ENOMEM;
1811
1812 sep_opt = options;
1813 while ((p = strsep(&sep_opt, ",")) != NULL) {
1814 if (!*p)
1815 continue;
1816
1817 token = match_token(p, srp_opt_tokens, args);
1818 opt_mask |= token;
1819
1820 switch (token) {
1821 case SRP_OPT_ID_EXT:
1822 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001823 if (!p) {
1824 ret = -ENOMEM;
1825 goto out;
1826 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001827 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1828 kfree(p);
1829 break;
1830
1831 case SRP_OPT_IOC_GUID:
1832 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001833 if (!p) {
1834 ret = -ENOMEM;
1835 goto out;
1836 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001837 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1838 kfree(p);
1839 break;
1840
1841 case SRP_OPT_DGID:
1842 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001843 if (!p) {
1844 ret = -ENOMEM;
1845 goto out;
1846 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001847 if (strlen(p) != 32) {
1848 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07001849 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001850 goto out;
1851 }
1852
1853 for (i = 0; i < 16; ++i) {
1854 strlcpy(dgid, p + i * 2, 3);
1855 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1856 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08001857 kfree(p);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001858 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001859 break;
1860
1861 case SRP_OPT_PKEY:
1862 if (match_hex(args, &token)) {
1863 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1864 goto out;
1865 }
1866 target->path.pkey = cpu_to_be16(token);
1867 break;
1868
1869 case SRP_OPT_SERVICE_ID:
1870 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001871 if (!p) {
1872 ret = -ENOMEM;
1873 goto out;
1874 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001875 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
Sean Hefty247e0202007-08-08 15:51:18 -07001876 target->path.service_id = target->service_id;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001877 kfree(p);
1878 break;
1879
1880 case SRP_OPT_MAX_SECT:
1881 if (match_int(args, &token)) {
1882 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1883 goto out;
1884 }
1885 target->scsi_host->max_sectors = token;
1886 break;
1887
Vu Pham52fb2b502006-06-17 20:37:31 -07001888 case SRP_OPT_MAX_CMD_PER_LUN:
1889 if (match_int(args, &token)) {
1890 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1891 goto out;
1892 }
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001893 target->scsi_host->cmd_per_lun = min(token, SRP_CMD_SQ_SIZE);
Vu Pham52fb2b502006-06-17 20:37:31 -07001894 break;
1895
Ramachandra K0c0450d2006-06-17 20:37:38 -07001896 case SRP_OPT_IO_CLASS:
1897 if (match_hex(args, &token)) {
1898 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1899 goto out;
1900 }
1901 if (token != SRP_REV10_IB_IO_CLASS &&
1902 token != SRP_REV16A_IB_IO_CLASS) {
1903 printk(KERN_WARNING PFX "unknown IO class parameter value"
1904 " %x specified (use %x or %x).\n",
1905 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1906 goto out;
1907 }
1908 target->io_class = token;
1909 break;
1910
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001911 case SRP_OPT_INITIATOR_EXT:
1912 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001913 if (!p) {
1914 ret = -ENOMEM;
1915 goto out;
1916 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001917 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1918 kfree(p);
1919 break;
1920
Roland Dreieraef9ec32005-11-02 14:07:13 -08001921 default:
1922 printk(KERN_WARNING PFX "unknown parameter or missing value "
1923 "'%s' in target creation request\n", p);
1924 goto out;
1925 }
1926 }
1927
1928 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1929 ret = 0;
1930 else
1931 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1932 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1933 !(srp_opt_tokens[i].token & opt_mask))
1934 printk(KERN_WARNING PFX "target creation request is "
1935 "missing parameter '%s'\n",
1936 srp_opt_tokens[i].pattern);
1937
1938out:
1939 kfree(options);
1940 return ret;
1941}
1942
Tony Jonesee959b02008-02-22 00:13:36 +01001943static ssize_t srp_create_target(struct device *dev,
1944 struct device_attribute *attr,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001945 const char *buf, size_t count)
1946{
1947 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01001948 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001949 struct Scsi_Host *target_host;
1950 struct srp_target_port *target;
1951 int ret;
1952 int i;
1953
1954 target_host = scsi_host_alloc(&srp_template,
1955 sizeof (struct srp_target_port));
1956 if (!target_host)
1957 return -ENOMEM;
1958
FUJITA Tomonori32368222007-06-27 16:33:12 +09001959 target_host->transportt = ib_srp_transport_template;
Arne Redlich3c8edf02006-11-15 12:43:00 +01001960 target_host->max_lun = SRP_MAX_LUN;
1961 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08001962
Roland Dreieraef9ec32005-11-02 14:07:13 -08001963 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001964
Ramachandra K0c0450d2006-06-17 20:37:38 -07001965 target->io_class = SRP_REV16A_IB_IO_CLASS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001966 target->scsi_host = target_host;
1967 target->srp_host = host;
1968
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05001969 INIT_LIST_HEAD(&target->free_tx);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001970 INIT_LIST_HEAD(&target->free_reqs);
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00001971 for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
Roland Dreierd945e1d2006-05-09 10:50:28 -07001972 target->req_ring[i].index = i;
1973 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1974 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001975
1976 ret = srp_parse_options(buf, target);
1977 if (ret)
1978 goto err;
1979
Roland Dreier969a60f2008-07-14 23:48:43 -07001980 ib_query_gid(host->srp_dev->dev, host->port, 0, &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001981
David Dillow7aa54bd2008-01-07 18:23:41 -05001982 shost_printk(KERN_DEBUG, target->scsi_host, PFX
1983 "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001984 "service_id %016llx dgid %pI6\n",
Roland Dreieraef9ec32005-11-02 14:07:13 -08001985 (unsigned long long) be64_to_cpu(target->id_ext),
1986 (unsigned long long) be64_to_cpu(target->ioc_guid),
1987 be16_to_cpu(target->path.pkey),
1988 (unsigned long long) be64_to_cpu(target->service_id),
Harvey Harrison8867cd72008-10-28 22:36:33 -07001989 target->path.dgid.raw);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001990
1991 ret = srp_create_target_ib(target);
1992 if (ret)
1993 goto err;
1994
David Dillow9fe4bcf2008-01-08 17:08:52 -05001995 ret = srp_new_cm_id(target);
1996 if (ret)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001997 goto err_free;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001998
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001999 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002000 ret = srp_connect_target(target);
2001 if (ret) {
David Dillow7aa54bd2008-01-07 18:23:41 -05002002 shost_printk(KERN_ERR, target->scsi_host,
2003 PFX "Connection failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08002004 goto err_cm_id;
2005 }
2006
2007 ret = srp_add_target(host, target);
2008 if (ret)
2009 goto err_disconnect;
2010
2011 return count;
2012
2013err_disconnect:
2014 srp_disconnect_target(target);
2015
2016err_cm_id:
2017 ib_destroy_cm_id(target->cm_id);
2018
2019err_free:
2020 srp_free_target_ib(target);
2021
2022err:
2023 scsi_host_put(target_host);
2024
2025 return ret;
2026}
2027
Tony Jonesee959b02008-02-22 00:13:36 +01002028static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002029
Tony Jonesee959b02008-02-22 00:13:36 +01002030static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
2031 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002032{
Tony Jonesee959b02008-02-22 00:13:36 +01002033 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002034
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002035 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002036}
2037
Tony Jonesee959b02008-02-22 00:13:36 +01002038static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002039
Tony Jonesee959b02008-02-22 00:13:36 +01002040static ssize_t show_port(struct device *dev, struct device_attribute *attr,
2041 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002042{
Tony Jonesee959b02008-02-22 00:13:36 +01002043 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002044
2045 return sprintf(buf, "%d\n", host->port);
2046}
2047
Tony Jonesee959b02008-02-22 00:13:36 +01002048static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002049
Roland Dreierf5358a12006-06-17 20:37:29 -07002050static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08002051{
2052 struct srp_host *host;
2053
2054 host = kzalloc(sizeof *host, GFP_KERNEL);
2055 if (!host)
2056 return NULL;
2057
2058 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002059 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002060 init_completion(&host->released);
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01002061 host->srp_dev = device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002062 host->port = port;
2063
Tony Jonesee959b02008-02-22 00:13:36 +01002064 host->dev.class = &srp_class;
2065 host->dev.parent = device->dev->dma_device;
Kay Sieversd927e382009-01-06 10:44:39 -08002066 dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002067
Tony Jonesee959b02008-02-22 00:13:36 +01002068 if (device_register(&host->dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07002069 goto free_host;
Tony Jonesee959b02008-02-22 00:13:36 +01002070 if (device_create_file(&host->dev, &dev_attr_add_target))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002071 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01002072 if (device_create_file(&host->dev, &dev_attr_ibdev))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002073 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01002074 if (device_create_file(&host->dev, &dev_attr_port))
Roland Dreieraef9ec32005-11-02 14:07:13 -08002075 goto err_class;
2076
2077 return host;
2078
2079err_class:
Tony Jonesee959b02008-02-22 00:13:36 +01002080 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002081
Roland Dreierf5358a12006-06-17 20:37:29 -07002082free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08002083 kfree(host);
2084
2085 return NULL;
2086}
2087
2088static void srp_add_one(struct ib_device *device)
2089{
Roland Dreierf5358a12006-06-17 20:37:29 -07002090 struct srp_device *srp_dev;
2091 struct ib_device_attr *dev_attr;
2092 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002093 struct srp_host *host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002094 int s, e, p;
2095
Roland Dreierf5358a12006-06-17 20:37:29 -07002096 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
2097 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08002098 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002099
Roland Dreierf5358a12006-06-17 20:37:29 -07002100 if (ib_query_device(device, dev_attr)) {
2101 printk(KERN_WARNING PFX "Query device failed for %s\n",
2102 device->name);
2103 goto free_attr;
2104 }
2105
2106 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
2107 if (!srp_dev)
2108 goto free_attr;
2109
2110 /*
2111 * Use the smallest page size supported by the HCA, down to a
2112 * minimum of 512 bytes (which is the smallest sector that a
2113 * SCSI command will ever carry).
2114 */
2115 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
2116 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
Roland Dreierbf628dc2006-12-15 14:01:49 -08002117 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
Roland Dreierf5358a12006-06-17 20:37:29 -07002118
2119 INIT_LIST_HEAD(&srp_dev->dev_list);
2120
2121 srp_dev->dev = device;
2122 srp_dev->pd = ib_alloc_pd(device);
2123 if (IS_ERR(srp_dev->pd))
2124 goto free_dev;
2125
2126 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
2127 IB_ACCESS_LOCAL_WRITE |
2128 IB_ACCESS_REMOTE_READ |
2129 IB_ACCESS_REMOTE_WRITE);
2130 if (IS_ERR(srp_dev->mr))
2131 goto err_pd;
2132
2133 memset(&fmr_param, 0, sizeof fmr_param);
2134 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
2135 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
2136 fmr_param.cache = 1;
2137 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
2138 fmr_param.page_shift = srp_dev->fmr_page_shift;
2139 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
2140 IB_ACCESS_REMOTE_WRITE |
2141 IB_ACCESS_REMOTE_READ);
2142
2143 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
2144 if (IS_ERR(srp_dev->fmr_pool))
2145 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002146
Tom Tucker07ebafb2006-08-03 16:02:42 -05002147 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002148 s = 0;
2149 e = 0;
2150 } else {
2151 s = 1;
2152 e = device->phys_port_cnt;
2153 }
2154
2155 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07002156 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002157 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07002158 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002159 }
2160
Roland Dreierf5358a12006-06-17 20:37:29 -07002161 ib_set_client_data(device, &srp_client, srp_dev);
2162
2163 goto free_attr;
2164
2165err_pd:
2166 ib_dealloc_pd(srp_dev->pd);
2167
2168free_dev:
2169 kfree(srp_dev);
2170
2171free_attr:
2172 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002173}
2174
2175static void srp_remove_one(struct ib_device *device)
2176{
Roland Dreierf5358a12006-06-17 20:37:29 -07002177 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002178 struct srp_host *host, *tmp_host;
2179 LIST_HEAD(target_list);
2180 struct srp_target_port *target, *tmp_target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002181
Roland Dreierf5358a12006-06-17 20:37:29 -07002182 srp_dev = ib_get_client_data(device, &srp_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002183
Roland Dreierf5358a12006-06-17 20:37:29 -07002184 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Tony Jonesee959b02008-02-22 00:13:36 +01002185 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002186 /*
2187 * Wait for the sysfs entry to go away, so that no new
2188 * target ports can be created.
2189 */
2190 wait_for_completion(&host->released);
2191
2192 /*
2193 * Mark all target ports as removed, so we stop queueing
2194 * commands and don't try to reconnect.
2195 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002196 spin_lock(&host->target_lock);
Matthew Wilcox549c5fc22006-06-17 20:37:30 -07002197 list_for_each_entry(target, &host->target_list, list) {
Ishai Rabinovitz0c5b3952006-06-17 20:37:31 -07002198 spin_lock_irq(target->scsi_host->host_lock);
2199 target->state = SRP_TARGET_REMOVED;
2200 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002201 }
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002202 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002203
2204 /*
2205 * Wait for any reconnection tasks that may have
2206 * started before we marked our target ports as
2207 * removed, and any target port removal tasks.
2208 */
2209 flush_scheduled_work();
2210
2211 list_for_each_entry_safe(target, tmp_target,
2212 &host->target_list, list) {
David Dillowb0e47c82008-01-03 10:25:27 -08002213 srp_remove_host(target->scsi_host);
Dave Dillowad696982008-01-03 22:35:41 -05002214 scsi_remove_host(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002215 srp_disconnect_target(target);
2216 ib_destroy_cm_id(target->cm_id);
2217 srp_free_target_ib(target);
2218 scsi_host_put(target->scsi_host);
2219 }
2220
Roland Dreieraef9ec32005-11-02 14:07:13 -08002221 kfree(host);
2222 }
2223
Roland Dreierf5358a12006-06-17 20:37:29 -07002224 if (srp_dev->fmr_pool)
2225 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2226 ib_dereg_mr(srp_dev->mr);
2227 ib_dealloc_pd(srp_dev->pd);
2228
2229 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002230}
2231
FUJITA Tomonori32368222007-06-27 16:33:12 +09002232static struct srp_function_template ib_srp_transport_functions = {
2233};
2234
Roland Dreieraef9ec32005-11-02 14:07:13 -08002235static int __init srp_init_module(void)
2236{
2237 int ret;
2238
Bart Van Asschedcb4cb82010-11-26 13:22:48 -05002239 BUILD_BUG_ON(FIELD_SIZEOF(struct ib_wc, wr_id) < sizeof(void *));
Bart Van Asschedd5e6e32010-08-30 19:27:20 +00002240
David Dillow1e89a192008-04-16 21:01:12 -07002241 if (srp_sg_tablesize > 255) {
2242 printk(KERN_WARNING PFX "Clamping srp_sg_tablesize to 255\n");
2243 srp_sg_tablesize = 255;
2244 }
2245
FUJITA Tomonori32368222007-06-27 16:33:12 +09002246 ib_srp_transport_template =
2247 srp_attach_transport(&ib_srp_transport_functions);
2248 if (!ib_srp_transport_template)
2249 return -ENOMEM;
2250
Vu Pham74b0a152006-06-17 20:37:32 -07002251 srp_template.sg_tablesize = srp_sg_tablesize;
2252 srp_max_iu_len = (sizeof (struct srp_cmd) +
2253 sizeof (struct srp_indirect_buf) +
2254 srp_sg_tablesize * 16);
2255
Roland Dreieraef9ec32005-11-02 14:07:13 -08002256 ret = class_register(&srp_class);
2257 if (ret) {
2258 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002259 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002260 return ret;
2261 }
2262
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002263 ib_sa_register_client(&srp_sa_client);
2264
Roland Dreieraef9ec32005-11-02 14:07:13 -08002265 ret = ib_register_client(&srp_client);
2266 if (ret) {
2267 printk(KERN_ERR PFX "couldn't register IB client\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002268 srp_release_transport(ib_srp_transport_template);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002269 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002270 class_unregister(&srp_class);
2271 return ret;
2272 }
2273
2274 return 0;
2275}
2276
2277static void __exit srp_cleanup_module(void)
2278{
2279 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002280 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002281 class_unregister(&srp_class);
FUJITA Tomonori32368222007-06-27 16:33:12 +09002282 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002283}
2284
2285module_init(srp_init_module);
2286module_exit(srp_cleanup_module);