blob: 435145709dd624395cf92cd2414364e58983bd89 [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.
31 *
32 * $Id: ib_srp.c 3932 2005-11-01 17:19:29Z roland $
33 */
34
Roland Dreieraef9ec32005-11-02 14:07:13 -080035#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/slab.h>
38#include <linux/err.h>
39#include <linux/string.h>
40#include <linux/parser.h>
41#include <linux/random.h>
Tim Schmielaude259682006-01-08 01:02:05 -080042#include <linux/jiffies.h>
Roland Dreieraef9ec32005-11-02 14:07:13 -080043
44#include <asm/atomic.h>
45
46#include <scsi/scsi.h>
47#include <scsi/scsi_device.h>
48#include <scsi/scsi_dbg.h>
49#include <scsi/srp.h>
FUJITA Tomonori32368222007-06-27 16:33:12 +090050#include <scsi/scsi_transport_srp.h>
Roland Dreieraef9ec32005-11-02 14:07:13 -080051
52#include <rdma/ib_cache.h>
53
54#include "ib_srp.h"
55
56#define DRV_NAME "ib_srp"
57#define PFX DRV_NAME ": "
58#define DRV_VERSION "0.2"
59#define DRV_RELDATE "November 1, 2005"
60
61MODULE_AUTHOR("Roland Dreier");
62MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
63 "v" DRV_VERSION " (" DRV_RELDATE ")");
64MODULE_LICENSE("Dual BSD/GPL");
65
Vu Pham74b0a152006-06-17 20:37:32 -070066static int srp_sg_tablesize = SRP_DEF_SG_TABLESIZE;
67static int srp_max_iu_len;
68
69module_param(srp_sg_tablesize, int, 0444);
70MODULE_PARM_DESC(srp_sg_tablesize,
David Dillow1e89a192008-04-16 21:01:12 -070071 "Max number of gather/scatter entries per I/O (default is 12, max 255)");
Vu Pham74b0a152006-06-17 20:37:32 -070072
Roland Dreieraef9ec32005-11-02 14:07:13 -080073static int topspin_workarounds = 1;
74
75module_param(topspin_workarounds, int, 0444);
76MODULE_PARM_DESC(topspin_workarounds,
77 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
78
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -070079static int mellanox_workarounds = 1;
80
81module_param(mellanox_workarounds, int, 0444);
82MODULE_PARM_DESC(mellanox_workarounds,
83 "Enable workarounds for Mellanox SRP target bugs if != 0");
84
Roland Dreieraef9ec32005-11-02 14:07:13 -080085static void srp_add_one(struct ib_device *device);
86static void srp_remove_one(struct ib_device *device);
87static void srp_completion(struct ib_cq *cq, void *target_ptr);
88static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
89
FUJITA Tomonori32368222007-06-27 16:33:12 +090090static struct scsi_transport_template *ib_srp_transport_template;
91
Roland Dreieraef9ec32005-11-02 14:07:13 -080092static struct ib_client srp_client = {
93 .name = "srp",
94 .add = srp_add_one,
95 .remove = srp_remove_one
96};
97
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -070098static struct ib_sa_client srp_sa_client;
99
Roland Dreieraef9ec32005-11-02 14:07:13 -0800100static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
101{
102 return (struct srp_target_port *) host->hostdata;
103}
104
105static const char *srp_target_info(struct Scsi_Host *host)
106{
107 return host_to_target(host)->target_name;
108}
109
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700110static int srp_target_is_topspin(struct srp_target_port *target)
111{
112 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700113 static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700114
115 return topspin_workarounds &&
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700116 (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
117 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700118}
119
120static int srp_target_is_mellanox(struct srp_target_port *target)
121{
122 static const u8 mellanox_oui[3] = { 0x00, 0x02, 0xc9 };
123
124 return mellanox_workarounds &&
125 !memcmp(&target->ioc_guid, mellanox_oui, sizeof mellanox_oui);
126}
127
Roland Dreieraef9ec32005-11-02 14:07:13 -0800128static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
129 gfp_t gfp_mask,
130 enum dma_data_direction direction)
131{
132 struct srp_iu *iu;
133
134 iu = kmalloc(sizeof *iu, gfp_mask);
135 if (!iu)
136 goto out;
137
138 iu->buf = kzalloc(size, gfp_mask);
139 if (!iu->buf)
140 goto out_free_iu;
141
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100142 iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
143 direction);
144 if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800145 goto out_free_buf;
146
147 iu->size = size;
148 iu->direction = direction;
149
150 return iu;
151
152out_free_buf:
153 kfree(iu->buf);
154out_free_iu:
155 kfree(iu);
156out:
157 return NULL;
158}
159
160static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
161{
162 if (!iu)
163 return;
164
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100165 ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
166 iu->direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800167 kfree(iu->buf);
168 kfree(iu);
169}
170
171static void srp_qp_event(struct ib_event *event, void *context)
172{
173 printk(KERN_ERR PFX "QP event %d\n", event->event);
174}
175
176static int srp_init_qp(struct srp_target_port *target,
177 struct ib_qp *qp)
178{
179 struct ib_qp_attr *attr;
180 int ret;
181
182 attr = kmalloc(sizeof *attr, GFP_KERNEL);
183 if (!attr)
184 return -ENOMEM;
185
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100186 ret = ib_find_cached_pkey(target->srp_host->srp_dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800187 target->srp_host->port,
188 be16_to_cpu(target->path.pkey),
189 &attr->pkey_index);
190 if (ret)
191 goto out;
192
193 attr->qp_state = IB_QPS_INIT;
194 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
195 IB_ACCESS_REMOTE_WRITE);
196 attr->port_num = target->srp_host->port;
197
198 ret = ib_modify_qp(qp, attr,
199 IB_QP_STATE |
200 IB_QP_PKEY_INDEX |
201 IB_QP_ACCESS_FLAGS |
202 IB_QP_PORT);
203
204out:
205 kfree(attr);
206 return ret;
207}
208
David Dillow9fe4bcf2008-01-08 17:08:52 -0500209static int srp_new_cm_id(struct srp_target_port *target)
210{
211 struct ib_cm_id *new_cm_id;
212
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100213 new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
David Dillow9fe4bcf2008-01-08 17:08:52 -0500214 srp_cm_handler, target);
215 if (IS_ERR(new_cm_id))
216 return PTR_ERR(new_cm_id);
217
218 if (target->cm_id)
219 ib_destroy_cm_id(target->cm_id);
220 target->cm_id = new_cm_id;
221
222 return 0;
223}
224
Roland Dreieraef9ec32005-11-02 14:07:13 -0800225static int srp_create_target_ib(struct srp_target_port *target)
226{
227 struct ib_qp_init_attr *init_attr;
228 int ret;
229
230 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
231 if (!init_attr)
232 return -ENOMEM;
233
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100234 target->cq = ib_create_cq(target->srp_host->srp_dev->dev,
235 srp_completion, NULL, target, SRP_CQ_SIZE, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800236 if (IS_ERR(target->cq)) {
237 ret = PTR_ERR(target->cq);
238 goto out;
239 }
240
241 ib_req_notify_cq(target->cq, IB_CQ_NEXT_COMP);
242
243 init_attr->event_handler = srp_qp_event;
244 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
245 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
246 init_attr->cap.max_recv_sge = 1;
247 init_attr->cap.max_send_sge = 1;
248 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
249 init_attr->qp_type = IB_QPT_RC;
250 init_attr->send_cq = target->cq;
251 init_attr->recv_cq = target->cq;
252
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100253 target->qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800254 if (IS_ERR(target->qp)) {
255 ret = PTR_ERR(target->qp);
256 ib_destroy_cq(target->cq);
257 goto out;
258 }
259
260 ret = srp_init_qp(target, target->qp);
261 if (ret) {
262 ib_destroy_qp(target->qp);
263 ib_destroy_cq(target->cq);
264 goto out;
265 }
266
267out:
268 kfree(init_attr);
269 return ret;
270}
271
272static void srp_free_target_ib(struct srp_target_port *target)
273{
274 int i;
275
276 ib_destroy_qp(target->qp);
277 ib_destroy_cq(target->cq);
278
279 for (i = 0; i < SRP_RQ_SIZE; ++i)
280 srp_free_iu(target->srp_host, target->rx_ring[i]);
281 for (i = 0; i < SRP_SQ_SIZE + 1; ++i)
282 srp_free_iu(target->srp_host, target->tx_ring[i]);
283}
284
285static void srp_path_rec_completion(int status,
286 struct ib_sa_path_rec *pathrec,
287 void *target_ptr)
288{
289 struct srp_target_port *target = target_ptr;
290
291 target->status = status;
292 if (status)
David Dillow7aa54bd2008-01-07 18:23:41 -0500293 shost_printk(KERN_ERR, target->scsi_host,
294 PFX "Got failed path rec status %d\n", status);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800295 else
296 target->path = *pathrec;
297 complete(&target->done);
298}
299
300static int srp_lookup_path(struct srp_target_port *target)
301{
302 target->path.numb_path = 1;
303
304 init_completion(&target->done);
305
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700306 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100307 target->srp_host->srp_dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800308 target->srp_host->port,
309 &target->path,
Sean Hefty247e0202007-08-08 15:51:18 -0700310 IB_SA_PATH_REC_SERVICE_ID |
Roland Dreieraef9ec32005-11-02 14:07:13 -0800311 IB_SA_PATH_REC_DGID |
312 IB_SA_PATH_REC_SGID |
313 IB_SA_PATH_REC_NUMB_PATH |
314 IB_SA_PATH_REC_PKEY,
315 SRP_PATH_REC_TIMEOUT_MS,
316 GFP_KERNEL,
317 srp_path_rec_completion,
318 target, &target->path_query);
319 if (target->path_query_id < 0)
320 return target->path_query_id;
321
322 wait_for_completion(&target->done);
323
324 if (target->status < 0)
David Dillow7aa54bd2008-01-07 18:23:41 -0500325 shost_printk(KERN_WARNING, target->scsi_host,
326 PFX "Path record query failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800327
328 return target->status;
329}
330
331static int srp_send_req(struct srp_target_port *target)
332{
333 struct {
334 struct ib_cm_req_param param;
335 struct srp_login_req priv;
336 } *req = NULL;
337 int status;
338
339 req = kzalloc(sizeof *req, GFP_KERNEL);
340 if (!req)
341 return -ENOMEM;
342
343 req->param.primary_path = &target->path;
344 req->param.alternate_path = NULL;
345 req->param.service_id = target->service_id;
346 req->param.qp_num = target->qp->qp_num;
347 req->param.qp_type = target->qp->qp_type;
348 req->param.private_data = &req->priv;
349 req->param.private_data_len = sizeof req->priv;
350 req->param.flow_control = 1;
351
352 get_random_bytes(&req->param.starting_psn, 4);
353 req->param.starting_psn &= 0xffffff;
354
355 /*
356 * Pick some arbitrary defaults here; we could make these
357 * module parameters if anyone cared about setting them.
358 */
359 req->param.responder_resources = 4;
360 req->param.remote_cm_response_timeout = 20;
361 req->param.local_cm_response_timeout = 20;
362 req->param.retry_count = 7;
363 req->param.rnr_retry_count = 7;
364 req->param.max_cm_retries = 15;
365
366 req->priv.opcode = SRP_LOGIN_REQ;
367 req->priv.tag = 0;
Vu Pham74b0a152006-06-17 20:37:32 -0700368 req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800369 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
370 SRP_BUF_FORMAT_INDIRECT);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700371 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700372 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450db2006-06-17 20:37:38 -0700373 * port identifier format is 8 bytes of ID extension followed
374 * by 8 bytes of GUID. Older drafts put the two halves in the
375 * opposite order, so that the GUID comes first.
376 *
377 * Targets conforming to these obsolete drafts can be
378 * recognized by the I/O Class they report.
379 */
380 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
381 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200382 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700383 memcpy(req->priv.initiator_port_id + 8,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200384 &target->initiator_ext, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700385 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
386 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
387 } else {
388 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200389 &target->initiator_ext, 8);
390 memcpy(req->priv.initiator_port_id + 8,
391 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700392 memcpy(req->priv.target_port_id, &target->id_ext, 8);
393 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
394 }
395
Roland Dreieraef9ec32005-11-02 14:07:13 -0800396 /*
397 * Topspin/Cisco SRP targets will reject our login unless we
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200398 * zero out the first 8 bytes of our initiator port ID and set
399 * the second 8 bytes to the local node GUID.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800400 */
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700401 if (srp_target_is_topspin(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500402 shost_printk(KERN_DEBUG, target->scsi_host,
403 PFX "Topspin/Cisco initiator port ID workaround "
404 "activated for target GUID %016llx\n",
405 (unsigned long long) be64_to_cpu(target->ioc_guid));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800406 memset(req->priv.initiator_port_id, 0, 8);
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200407 memcpy(req->priv.initiator_port_id + 8,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100408 &target->srp_host->srp_dev->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800409 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800410
411 status = ib_send_cm_req(target->cm_id, &req->param);
412
413 kfree(req);
414
415 return status;
416}
417
418static void srp_disconnect_target(struct srp_target_port *target)
419{
420 /* XXX should send SRP_I_LOGOUT request */
421
422 init_completion(&target->done);
Roland Dreiere6581052006-05-17 09:13:21 -0700423 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500424 shost_printk(KERN_DEBUG, target->scsi_host,
425 PFX "Sending CM DREQ failed\n");
Roland Dreiere6581052006-05-17 09:13:21 -0700426 return;
427 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800428 wait_for_completion(&target->done);
429}
430
David Howellsc4028952006-11-22 14:57:56 +0000431static void srp_remove_work(struct work_struct *work)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800432{
David Howellsc4028952006-11-22 14:57:56 +0000433 struct srp_target_port *target =
434 container_of(work, struct srp_target_port, work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800435
436 spin_lock_irq(target->scsi_host->host_lock);
437 if (target->state != SRP_TARGET_DEAD) {
438 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800439 return;
440 }
441 target->state = SRP_TARGET_REMOVED;
442 spin_unlock_irq(target->scsi_host->host_lock);
443
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700444 spin_lock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800445 list_del(&target->list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700446 spin_unlock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800447
FUJITA Tomonori32368222007-06-27 16:33:12 +0900448 srp_remove_host(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800449 scsi_remove_host(target->scsi_host);
450 ib_destroy_cm_id(target->cm_id);
451 srp_free_target_ib(target);
452 scsi_host_put(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800453}
454
455static int srp_connect_target(struct srp_target_port *target)
456{
David Dillow9fe4bcf2008-01-08 17:08:52 -0500457 int retries = 3;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800458 int ret;
459
460 ret = srp_lookup_path(target);
461 if (ret)
462 return ret;
463
464 while (1) {
465 init_completion(&target->done);
466 ret = srp_send_req(target);
467 if (ret)
468 return ret;
469 wait_for_completion(&target->done);
470
471 /*
472 * The CM event handling code will set status to
473 * SRP_PORT_REDIRECT if we get a port redirect REJ
474 * back, or SRP_DLID_REDIRECT if we get a lid/qp
475 * redirect REJ back.
476 */
477 switch (target->status) {
478 case 0:
479 return 0;
480
481 case SRP_PORT_REDIRECT:
482 ret = srp_lookup_path(target);
483 if (ret)
484 return ret;
485 break;
486
487 case SRP_DLID_REDIRECT:
488 break;
489
David Dillow9fe4bcf2008-01-08 17:08:52 -0500490 case SRP_STALE_CONN:
491 /* Our current CM id was stale, and is now in timewait.
492 * Try to reconnect with a new one.
493 */
494 if (!retries-- || srp_new_cm_id(target)) {
495 shost_printk(KERN_ERR, target->scsi_host, PFX
496 "giving up on stale connection\n");
497 target->status = -ECONNRESET;
498 return target->status;
499 }
500
501 shost_printk(KERN_ERR, target->scsi_host, PFX
502 "retrying stale connection\n");
503 break;
504
Roland Dreieraef9ec32005-11-02 14:07:13 -0800505 default:
506 return target->status;
507 }
508 }
509}
510
Roland Dreierd945e1d2006-05-09 10:50:28 -0700511static void srp_unmap_data(struct scsi_cmnd *scmnd,
512 struct srp_target_port *target,
513 struct srp_request *req)
514{
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900515 if (!scsi_sglist(scmnd) ||
Roland Dreierd945e1d2006-05-09 10:50:28 -0700516 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
517 scmnd->sc_data_direction != DMA_FROM_DEVICE))
518 return;
519
Roland Dreierf5358a12006-06-17 20:37:29 -0700520 if (req->fmr) {
521 ib_fmr_pool_unmap(req->fmr);
522 req->fmr = NULL;
523 }
524
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100525 ib_dma_unmap_sg(target->srp_host->srp_dev->dev, scsi_sglist(scmnd),
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900526 scsi_sg_count(scmnd), scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700527}
528
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700529static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
530{
531 srp_unmap_data(req->scmnd, target, req);
532 list_move_tail(&req->list, &target->free_reqs);
533}
534
535static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
536{
537 req->scmnd->result = DID_RESET << 16;
538 req->scmnd->scsi_done(req->scmnd);
539 srp_remove_req(target, req);
540}
541
Roland Dreieraef9ec32005-11-02 14:07:13 -0800542static int srp_reconnect_target(struct srp_target_port *target)
543{
Roland Dreieraef9ec32005-11-02 14:07:13 -0800544 struct ib_qp_attr qp_attr;
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700545 struct srp_request *req, *tmp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800546 struct ib_wc wc;
547 int ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800548
549 spin_lock_irq(target->scsi_host->host_lock);
550 if (target->state != SRP_TARGET_LIVE) {
551 spin_unlock_irq(target->scsi_host->host_lock);
552 return -EAGAIN;
553 }
554 target->state = SRP_TARGET_CONNECTING;
555 spin_unlock_irq(target->scsi_host->host_lock);
556
557 srp_disconnect_target(target);
558 /*
559 * Now get a new local CM ID so that we avoid confusing the
560 * target in case things are really fouled up.
561 */
David Dillow9fe4bcf2008-01-08 17:08:52 -0500562 ret = srp_new_cm_id(target);
563 if (ret)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800564 goto err;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800565
566 qp_attr.qp_state = IB_QPS_RESET;
567 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
568 if (ret)
569 goto err;
570
571 ret = srp_init_qp(target, target->qp);
572 if (ret)
573 goto err;
574
575 while (ib_poll_cq(target->cq, 1, &wc) > 0)
576 ; /* nothing */
577
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300578 spin_lock_irq(target->scsi_host->host_lock);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700579 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
580 srp_reset_req(target, req);
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300581 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800582
583 target->rx_head = 0;
584 target->tx_head = 0;
585 target->tx_tail = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800586
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200587 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800588 ret = srp_connect_target(target);
589 if (ret)
590 goto err;
591
592 spin_lock_irq(target->scsi_host->host_lock);
593 if (target->state == SRP_TARGET_CONNECTING) {
594 ret = 0;
595 target->state = SRP_TARGET_LIVE;
596 } else
597 ret = -EAGAIN;
598 spin_unlock_irq(target->scsi_host->host_lock);
599
600 return ret;
601
602err:
David Dillow7aa54bd2008-01-07 18:23:41 -0500603 shost_printk(KERN_ERR, target->scsi_host,
604 PFX "reconnect failed (%d), removing target port.\n", ret);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800605
606 /*
607 * We couldn't reconnect, so kill our target port off.
608 * However, we have to defer the real removal because we might
609 * be in the context of the SCSI error handler now, which
610 * would deadlock if we call scsi_remove_host().
611 */
612 spin_lock_irq(target->scsi_host->host_lock);
613 if (target->state == SRP_TARGET_CONNECTING) {
614 target->state = SRP_TARGET_DEAD;
David Howellsc4028952006-11-22 14:57:56 +0000615 INIT_WORK(&target->work, srp_remove_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800616 schedule_work(&target->work);
617 }
618 spin_unlock_irq(target->scsi_host->host_lock);
619
620 return ret;
621}
622
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700623static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat,
Roland Dreierf5358a12006-06-17 20:37:29 -0700624 int sg_cnt, struct srp_request *req,
625 struct srp_direct_buf *buf)
626{
627 u64 io_addr = 0;
628 u64 *dma_pages;
629 u32 len;
630 int page_cnt;
631 int i, j;
632 int ret;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100633 struct srp_device *dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800634 struct ib_device *ibdev = dev->dev;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900635 struct scatterlist *sg;
Roland Dreierf5358a12006-06-17 20:37:29 -0700636
637 if (!dev->fmr_pool)
638 return -ENODEV;
639
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700640 if (srp_target_is_mellanox(target) &&
641 (ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask))
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700642 return -EINVAL;
643
Roland Dreierf5358a12006-06-17 20:37:29 -0700644 len = page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900645 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
646 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800647
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900648 if (ib_sg_dma_address(ibdev, sg) & ~dev->fmr_page_mask) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700649 if (i > 0)
650 return -EINVAL;
651 else
652 ++page_cnt;
653 }
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900654 if ((ib_sg_dma_address(ibdev, sg) + dma_len) &
Roland Dreierf5358a12006-06-17 20:37:29 -0700655 ~dev->fmr_page_mask) {
656 if (i < sg_cnt - 1)
657 return -EINVAL;
658 else
659 ++page_cnt;
660 }
661
Ralph Campbell85507bc2006-12-12 14:30:55 -0800662 len += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700663 }
664
665 page_cnt += len >> dev->fmr_page_shift;
666 if (page_cnt > SRP_FMR_SIZE)
667 return -ENOMEM;
668
669 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
670 if (!dma_pages)
671 return -ENOMEM;
672
673 page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900674 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
675 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800676
677 for (j = 0; j < dma_len; j += dev->fmr_page_size)
Roland Dreierf5358a12006-06-17 20:37:29 -0700678 dma_pages[page_cnt++] =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900679 (ib_sg_dma_address(ibdev, sg) &
Ralph Campbell85507bc2006-12-12 14:30:55 -0800680 dev->fmr_page_mask) + j;
681 }
Roland Dreierf5358a12006-06-17 20:37:29 -0700682
683 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
Michael S. Tsirkinadfaa882006-07-14 00:23:55 -0700684 dma_pages, page_cnt, io_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -0700685 if (IS_ERR(req->fmr)) {
686 ret = PTR_ERR(req->fmr);
Vu Pham6583eb32006-07-14 00:23:53 -0700687 req->fmr = NULL;
Roland Dreierf5358a12006-06-17 20:37:29 -0700688 goto out;
689 }
690
Ralph Campbell85507bc2006-12-12 14:30:55 -0800691 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) &
692 ~dev->fmr_page_mask);
Roland Dreierf5358a12006-06-17 20:37:29 -0700693 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
694 buf->len = cpu_to_be32(len);
695
696 ret = 0;
697
698out:
699 kfree(dma_pages);
700
701 return ret;
702}
703
Roland Dreieraef9ec32005-11-02 14:07:13 -0800704static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
705 struct srp_request *req)
706{
Roland Dreiercf368712006-03-24 15:47:26 -0800707 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800708 struct srp_cmd *cmd = req->cmd->buf;
Roland Dreiercf368712006-03-24 15:47:26 -0800709 int len, nents, count;
Roland Dreierf5358a12006-06-17 20:37:29 -0700710 u8 fmt = SRP_DATA_DESC_DIRECT;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800711 struct srp_device *dev;
712 struct ib_device *ibdev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800713
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900714 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800715 return sizeof (struct srp_cmd);
716
717 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
718 scmnd->sc_data_direction != DMA_TO_DEVICE) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500719 shost_printk(KERN_WARNING, target->scsi_host,
720 PFX "Unhandled data direction %d\n",
721 scmnd->sc_data_direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800722 return -EINVAL;
723 }
724
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900725 nents = scsi_sg_count(scmnd);
726 scat = scsi_sglist(scmnd);
Roland Dreiercf368712006-03-24 15:47:26 -0800727
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100728 dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800729 ibdev = dev->dev;
730
731 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
Roland Dreierf5358a12006-06-17 20:37:29 -0700732
733 fmt = SRP_DATA_DESC_DIRECT;
734 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800735
736 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700737 /*
738 * The midlayer only generated a single gather/scatter
739 * entry, or DMA mapping coalesced everything to a
740 * single entry. So a direct descriptor along with
741 * the DMA MR suffices.
742 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800743 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800744
Ralph Campbell85507bc2006-12-12 14:30:55 -0800745 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
746 buf->key = cpu_to_be32(dev->mr->rkey);
747 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700748 } else if (srp_map_fmr(target, scat, count, req,
Roland Dreierf5358a12006-06-17 20:37:29 -0700749 (void *) cmd->add_data)) {
750 /*
751 * FMR mapping failed, and the scatterlist has more
752 * than one entry. Generate an indirect memory
753 * descriptor.
754 */
Roland Dreiercf368712006-03-24 15:47:26 -0800755 struct srp_indirect_buf *buf = (void *) cmd->add_data;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900756 struct scatterlist *sg;
Roland Dreiercf368712006-03-24 15:47:26 -0800757 u32 datalen = 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700758 int i;
Roland Dreiercf368712006-03-24 15:47:26 -0800759
760 fmt = SRP_DATA_DESC_INDIRECT;
Roland Dreierf5358a12006-06-17 20:37:29 -0700761 len = sizeof (struct srp_cmd) +
762 sizeof (struct srp_indirect_buf) +
763 count * sizeof (struct srp_direct_buf);
764
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900765 scsi_for_each_sg(scmnd, sg, count, i) {
766 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800767
Roland Dreierf5358a12006-06-17 20:37:29 -0700768 buf->desc_list[i].va =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900769 cpu_to_be64(ib_sg_dma_address(ibdev, sg));
Roland Dreierf5358a12006-06-17 20:37:29 -0700770 buf->desc_list[i].key =
Ralph Campbell85507bc2006-12-12 14:30:55 -0800771 cpu_to_be32(dev->mr->rkey);
772 buf->desc_list[i].len = cpu_to_be32(dma_len);
773 datalen += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700774 }
Roland Dreiercf368712006-03-24 15:47:26 -0800775
776 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
777 cmd->data_out_desc_cnt = count;
778 else
779 cmd->data_in_desc_cnt = count;
780
Roland Dreierf5358a12006-06-17 20:37:29 -0700781 buf->table_desc.va =
782 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800783 buf->table_desc.key =
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100784 cpu_to_be32(target->srp_host->srp_dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800785 buf->table_desc.len =
786 cpu_to_be32(count * sizeof (struct srp_direct_buf));
787
Roland Dreiercf368712006-03-24 15:47:26 -0800788 buf->len = cpu_to_be32(datalen);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800789 }
790
791 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
792 cmd->buf_fmt = fmt << 4;
793 else
794 cmd->buf_fmt = fmt;
795
Roland Dreieraef9ec32005-11-02 14:07:13 -0800796 return len;
797}
798
Roland Dreieraef9ec32005-11-02 14:07:13 -0800799static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
800{
801 struct srp_request *req;
802 struct scsi_cmnd *scmnd;
803 unsigned long flags;
804 s32 delta;
805
806 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
807
808 spin_lock_irqsave(target->scsi_host->host_lock, flags);
809
810 target->req_lim += delta;
811
812 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
813
814 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
815 if (be32_to_cpu(rsp->resp_data_len) < 4)
816 req->tsk_status = -1;
817 else
818 req->tsk_status = rsp->data[3];
819 complete(&req->done);
820 } else {
Roland Dreierd945e1d2006-05-09 10:50:28 -0700821 scmnd = req->scmnd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800822 if (!scmnd)
David Dillow7aa54bd2008-01-07 18:23:41 -0500823 shost_printk(KERN_ERR, target->scsi_host,
824 "Null scmnd for RSP w/tag %016llx\n",
825 (unsigned long long) rsp->tag);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800826 scmnd->result = rsp->status;
827
828 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
829 memcpy(scmnd->sense_buffer, rsp->data +
830 be32_to_cpu(rsp->resp_data_len),
831 min_t(int, be32_to_cpu(rsp->sense_data_len),
832 SCSI_SENSE_BUFFERSIZE));
833 }
834
835 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900836 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800837 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900838 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800839
Roland Dreieraef9ec32005-11-02 14:07:13 -0800840 if (!req->tsk_mgmt) {
Roland Dreieraef9ec32005-11-02 14:07:13 -0800841 scmnd->host_scribble = (void *) -1L;
842 scmnd->scsi_done(scmnd);
843
Roland Dreierd945e1d2006-05-09 10:50:28 -0700844 srp_remove_req(target, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800845 } else
846 req->cmd_done = 1;
847 }
848
849 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
850}
851
Roland Dreieraef9ec32005-11-02 14:07:13 -0800852static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
853{
Ralph Campbell85507bc2006-12-12 14:30:55 -0800854 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800855 struct srp_iu *iu;
856 u8 opcode;
857
858 iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
859
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100860 dev = target->srp_host->srp_dev->dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800861 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
862 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800863
864 opcode = *(u8 *) iu->buf;
865
866 if (0) {
867 int i;
868
David Dillow7aa54bd2008-01-07 18:23:41 -0500869 shost_printk(KERN_ERR, target->scsi_host,
870 PFX "recv completion, opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800871
872 for (i = 0; i < wc->byte_len; ++i) {
873 if (i % 8 == 0)
874 printk(KERN_ERR " [%02x] ", i);
875 printk(" %02x", ((u8 *) iu->buf)[i]);
876 if ((i + 1) % 8 == 0)
877 printk("\n");
878 }
879
880 if (wc->byte_len % 8)
881 printk("\n");
882 }
883
884 switch (opcode) {
885 case SRP_RSP:
886 srp_process_rsp(target, iu->buf);
887 break;
888
889 case SRP_T_LOGOUT:
890 /* XXX Handle target logout */
David Dillow7aa54bd2008-01-07 18:23:41 -0500891 shost_printk(KERN_WARNING, target->scsi_host,
892 PFX "Got target logout request\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800893 break;
894
895 default:
David Dillow7aa54bd2008-01-07 18:23:41 -0500896 shost_printk(KERN_WARNING, target->scsi_host,
897 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800898 break;
899 }
900
Ralph Campbell85507bc2006-12-12 14:30:55 -0800901 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
902 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800903}
904
905static void srp_completion(struct ib_cq *cq, void *target_ptr)
906{
907 struct srp_target_port *target = target_ptr;
908 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800909
910 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
911 while (ib_poll_cq(cq, 1, &wc) > 0) {
912 if (wc.status) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500913 shost_printk(KERN_ERR, target->scsi_host,
914 PFX "failed %s status %d\n",
915 wc.wr_id & SRP_OP_RECV ? "receive" : "send",
916 wc.status);
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200917 target->qp_in_error = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800918 break;
919 }
920
921 if (wc.wr_id & SRP_OP_RECV)
922 srp_handle_recv(target, &wc);
923 else
924 ++target->tx_tail;
925 }
926}
927
928static int __srp_post_recv(struct srp_target_port *target)
929{
930 struct srp_iu *iu;
931 struct ib_sge list;
932 struct ib_recv_wr wr, *bad_wr;
933 unsigned int next;
934 int ret;
935
936 next = target->rx_head & (SRP_RQ_SIZE - 1);
937 wr.wr_id = next | SRP_OP_RECV;
938 iu = target->rx_ring[next];
939
940 list.addr = iu->dma;
941 list.length = iu->size;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100942 list.lkey = target->srp_host->srp_dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800943
944 wr.next = NULL;
945 wr.sg_list = &list;
946 wr.num_sge = 1;
947
948 ret = ib_post_recv(target->qp, &wr, &bad_wr);
949 if (!ret)
950 ++target->rx_head;
951
952 return ret;
953}
954
955static int srp_post_recv(struct srp_target_port *target)
956{
957 unsigned long flags;
958 int ret;
959
960 spin_lock_irqsave(target->scsi_host->host_lock, flags);
961 ret = __srp_post_recv(target);
962 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
963
964 return ret;
965}
966
967/*
968 * Must be called with target->scsi_host->host_lock held to protect
Roland Dreier47f2bce2005-11-15 00:19:21 -0800969 * req_lim and tx_head. Lock cannot be dropped between call here and
970 * call to __srp_post_send().
Roland Dreieraef9ec32005-11-02 14:07:13 -0800971 */
David Dillow8cba2072007-12-19 17:08:43 -0500972static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
973 enum srp_request_type req_type)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800974{
David Dillow8cba2072007-12-19 17:08:43 -0500975 s32 min = (req_type == SRP_REQ_TASK_MGMT) ? 1 : 2;
976
Roland Dreieraef9ec32005-11-02 14:07:13 -0800977 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
978 return NULL;
979
David Dillow8cba2072007-12-19 17:08:43 -0500980 if (target->req_lim < min) {
Roland Dreier6bfa24f2006-06-17 20:37:33 -0700981 ++target->zero_req_lim;
David Dillow8cba2072007-12-19 17:08:43 -0500982 return NULL;
983 }
Roland Dreier47f2bce2005-11-15 00:19:21 -0800984
Roland Dreieraef9ec32005-11-02 14:07:13 -0800985 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
986}
987
988/*
989 * Must be called with target->scsi_host->host_lock held to protect
990 * req_lim and tx_head.
991 */
992static int __srp_post_send(struct srp_target_port *target,
993 struct srp_iu *iu, int len)
994{
995 struct ib_sge list;
996 struct ib_send_wr wr, *bad_wr;
997 int ret = 0;
998
Roland Dreieraef9ec32005-11-02 14:07:13 -0800999 list.addr = iu->dma;
1000 list.length = len;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001001 list.lkey = target->srp_host->srp_dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001002
1003 wr.next = NULL;
1004 wr.wr_id = target->tx_head & SRP_SQ_SIZE;
1005 wr.sg_list = &list;
1006 wr.num_sge = 1;
1007 wr.opcode = IB_WR_SEND;
1008 wr.send_flags = IB_SEND_SIGNALED;
1009
1010 ret = ib_post_send(target->qp, &wr, &bad_wr);
1011
1012 if (!ret) {
1013 ++target->tx_head;
1014 --target->req_lim;
1015 }
1016
1017 return ret;
1018}
1019
1020static int srp_queuecommand(struct scsi_cmnd *scmnd,
1021 void (*done)(struct scsi_cmnd *))
1022{
1023 struct srp_target_port *target = host_to_target(scmnd->device->host);
1024 struct srp_request *req;
1025 struct srp_iu *iu;
1026 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001027 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001028 int len;
1029
1030 if (target->state == SRP_TARGET_CONNECTING)
1031 goto err;
1032
1033 if (target->state == SRP_TARGET_DEAD ||
1034 target->state == SRP_TARGET_REMOVED) {
1035 scmnd->result = DID_BAD_TARGET << 16;
1036 done(scmnd);
1037 return 0;
1038 }
1039
David Dillow8cba2072007-12-19 17:08:43 -05001040 iu = __srp_get_tx_iu(target, SRP_REQ_NORMAL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001041 if (!iu)
1042 goto err;
1043
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001044 dev = target->srp_host->srp_dev->dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001045 ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len,
1046 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001047
Roland Dreierd945e1d2006-05-09 10:50:28 -07001048 req = list_entry(target->free_reqs.next, struct srp_request, list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001049
1050 scmnd->scsi_done = done;
1051 scmnd->result = 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001052 scmnd->host_scribble = (void *) (long) req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001053
1054 cmd = iu->buf;
1055 memset(cmd, 0, sizeof *cmd);
1056
1057 cmd->opcode = SRP_CMD;
1058 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001059 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001060 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1061
Roland Dreieraef9ec32005-11-02 14:07:13 -08001062 req->scmnd = scmnd;
1063 req->cmd = iu;
1064 req->cmd_done = 0;
1065 req->tsk_mgmt = NULL;
1066
1067 len = srp_map_data(scmnd, target, req);
1068 if (len < 0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001069 shost_printk(KERN_ERR, target->scsi_host,
1070 PFX "Failed to map data\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001071 goto err;
1072 }
1073
1074 if (__srp_post_recv(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001075 shost_printk(KERN_ERR, target->scsi_host, PFX "Recv failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001076 goto err_unmap;
1077 }
1078
Ralph Campbell85507bc2006-12-12 14:30:55 -08001079 ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len,
1080 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001081
1082 if (__srp_post_send(target, iu, len)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001083 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001084 goto err_unmap;
1085 }
1086
Roland Dreierd945e1d2006-05-09 10:50:28 -07001087 list_move_tail(&req->list, &target->req_queue);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001088
1089 return 0;
1090
1091err_unmap:
1092 srp_unmap_data(scmnd, target, req);
1093
1094err:
1095 return SCSI_MLQUEUE_HOST_BUSY;
1096}
1097
1098static int srp_alloc_iu_bufs(struct srp_target_port *target)
1099{
1100 int i;
1101
1102 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1103 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1104 target->max_ti_iu_len,
1105 GFP_KERNEL, DMA_FROM_DEVICE);
1106 if (!target->rx_ring[i])
1107 goto err;
1108 }
1109
1110 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1111 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
Vu Pham74b0a152006-06-17 20:37:32 -07001112 srp_max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001113 GFP_KERNEL, DMA_TO_DEVICE);
1114 if (!target->tx_ring[i])
1115 goto err;
1116 }
1117
1118 return 0;
1119
1120err:
1121 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1122 srp_free_iu(target->srp_host, target->rx_ring[i]);
1123 target->rx_ring[i] = NULL;
1124 }
1125
1126 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1127 srp_free_iu(target->srp_host, target->tx_ring[i]);
1128 target->tx_ring[i] = NULL;
1129 }
1130
1131 return -ENOMEM;
1132}
1133
1134static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1135 struct ib_cm_event *event,
1136 struct srp_target_port *target)
1137{
David Dillow7aa54bd2008-01-07 18:23:41 -05001138 struct Scsi_Host *shost = target->scsi_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001139 struct ib_class_port_info *cpi;
1140 int opcode;
1141
1142 switch (event->param.rej_rcvd.reason) {
1143 case IB_CM_REJ_PORT_CM_REDIRECT:
1144 cpi = event->param.rej_rcvd.ari;
1145 target->path.dlid = cpi->redirect_lid;
1146 target->path.pkey = cpi->redirect_pkey;
1147 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1148 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1149
1150 target->status = target->path.dlid ?
1151 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1152 break;
1153
1154 case IB_CM_REJ_PORT_REDIRECT:
Roland Dreier5d7cbfd2007-08-03 10:45:18 -07001155 if (srp_target_is_topspin(target)) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001156 /*
1157 * Topspin/Cisco SRP gateways incorrectly send
1158 * reject reason code 25 when they mean 24
1159 * (port redirect).
1160 */
1161 memcpy(target->path.dgid.raw,
1162 event->param.rej_rcvd.ari, 16);
1163
David Dillow7aa54bd2008-01-07 18:23:41 -05001164 shost_printk(KERN_DEBUG, shost,
1165 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1166 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1167 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001168
1169 target->status = SRP_PORT_REDIRECT;
1170 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001171 shost_printk(KERN_WARNING, shost,
1172 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001173 target->status = -ECONNRESET;
1174 }
1175 break;
1176
1177 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
David Dillow7aa54bd2008-01-07 18:23:41 -05001178 shost_printk(KERN_WARNING, shost,
1179 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001180 target->status = -ECONNRESET;
1181 break;
1182
1183 case IB_CM_REJ_CONSUMER_DEFINED:
1184 opcode = *(u8 *) event->private_data;
1185 if (opcode == SRP_LOGIN_REJ) {
1186 struct srp_login_rej *rej = event->private_data;
1187 u32 reason = be32_to_cpu(rej->reason);
1188
1189 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
David Dillow7aa54bd2008-01-07 18:23:41 -05001190 shost_printk(KERN_WARNING, shost,
1191 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001192 else
David Dillow7aa54bd2008-01-07 18:23:41 -05001193 shost_printk(KERN_WARNING, shost,
1194 PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001195 } else
David Dillow7aa54bd2008-01-07 18:23:41 -05001196 shost_printk(KERN_WARNING, shost,
1197 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1198 " opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001199 target->status = -ECONNRESET;
1200 break;
1201
David Dillow9fe4bcf2008-01-08 17:08:52 -05001202 case IB_CM_REJ_STALE_CONN:
1203 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
1204 target->status = SRP_STALE_CONN;
1205 break;
1206
Roland Dreieraef9ec32005-11-02 14:07:13 -08001207 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001208 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
1209 event->param.rej_rcvd.reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001210 target->status = -ECONNRESET;
1211 }
1212}
1213
1214static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1215{
1216 struct srp_target_port *target = cm_id->context;
1217 struct ib_qp_attr *qp_attr = NULL;
1218 int attr_mask = 0;
1219 int comp = 0;
1220 int opcode = 0;
1221
1222 switch (event->event) {
1223 case IB_CM_REQ_ERROR:
David Dillow7aa54bd2008-01-07 18:23:41 -05001224 shost_printk(KERN_DEBUG, target->scsi_host,
1225 PFX "Sending CM REQ failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001226 comp = 1;
1227 target->status = -ECONNRESET;
1228 break;
1229
1230 case IB_CM_REP_RECEIVED:
1231 comp = 1;
1232 opcode = *(u8 *) event->private_data;
1233
1234 if (opcode == SRP_LOGIN_RSP) {
1235 struct srp_login_rsp *rsp = event->private_data;
1236
1237 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1238 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1239
1240 target->scsi_host->can_queue = min(target->req_lim,
1241 target->scsi_host->can_queue);
1242 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001243 shost_printk(KERN_WARNING, target->scsi_host,
1244 PFX "Unhandled RSP opcode %#x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001245 target->status = -ECONNRESET;
1246 break;
1247 }
1248
Vu Phamd2fcea72006-11-21 14:14:10 -08001249 if (!target->rx_ring[0]) {
1250 target->status = srp_alloc_iu_bufs(target);
1251 if (target->status)
1252 break;
1253 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001254
1255 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1256 if (!qp_attr) {
1257 target->status = -ENOMEM;
1258 break;
1259 }
1260
1261 qp_attr->qp_state = IB_QPS_RTR;
1262 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1263 if (target->status)
1264 break;
1265
1266 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1267 if (target->status)
1268 break;
1269
1270 target->status = srp_post_recv(target);
1271 if (target->status)
1272 break;
1273
1274 qp_attr->qp_state = IB_QPS_RTS;
1275 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1276 if (target->status)
1277 break;
1278
1279 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1280 if (target->status)
1281 break;
1282
1283 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1284 if (target->status)
1285 break;
1286
1287 break;
1288
1289 case IB_CM_REJ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001290 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001291 comp = 1;
1292
1293 srp_cm_rej_handler(cm_id, event, target);
1294 break;
1295
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001296 case IB_CM_DREQ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001297 shost_printk(KERN_WARNING, target->scsi_host,
1298 PFX "DREQ received - connection closed\n");
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001299 if (ib_send_cm_drep(cm_id, NULL, 0))
David Dillow7aa54bd2008-01-07 18:23:41 -05001300 shost_printk(KERN_ERR, target->scsi_host,
1301 PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001302 break;
1303
1304 case IB_CM_TIMEWAIT_EXIT:
David Dillow7aa54bd2008-01-07 18:23:41 -05001305 shost_printk(KERN_ERR, target->scsi_host,
1306 PFX "connection closed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001307
1308 comp = 1;
1309 target->status = 0;
1310 break;
1311
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001312 case IB_CM_MRA_RECEIVED:
1313 case IB_CM_DREQ_ERROR:
1314 case IB_CM_DREP_RECEIVED:
1315 break;
1316
Roland Dreieraef9ec32005-11-02 14:07:13 -08001317 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001318 shost_printk(KERN_WARNING, target->scsi_host,
1319 PFX "Unhandled CM event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001320 break;
1321 }
1322
1323 if (comp)
1324 complete(&target->done);
1325
1326 kfree(qp_attr);
1327
1328 return 0;
1329}
1330
Roland Dreierd945e1d2006-05-09 10:50:28 -07001331static int srp_send_tsk_mgmt(struct srp_target_port *target,
1332 struct srp_request *req, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001333{
Roland Dreieraef9ec32005-11-02 14:07:13 -08001334 struct srp_iu *iu;
1335 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001336
1337 spin_lock_irq(target->scsi_host->host_lock);
1338
Roland Dreier1285b3a2006-03-03 15:47:25 -08001339 if (target->state == SRP_TARGET_DEAD ||
1340 target->state == SRP_TARGET_REMOVED) {
Roland Dreierd945e1d2006-05-09 10:50:28 -07001341 req->scmnd->result = DID_BAD_TARGET << 16;
Roland Dreier1285b3a2006-03-03 15:47:25 -08001342 goto out;
1343 }
1344
Roland Dreieraef9ec32005-11-02 14:07:13 -08001345 init_completion(&req->done);
1346
David Dillow8cba2072007-12-19 17:08:43 -05001347 iu = __srp_get_tx_iu(target, SRP_REQ_TASK_MGMT);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001348 if (!iu)
1349 goto out;
1350
1351 tsk_mgmt = iu->buf;
1352 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1353
1354 tsk_mgmt->opcode = SRP_TSK_MGMT;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001355 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1356 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001357 tsk_mgmt->tsk_mgmt_func = func;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001358 tsk_mgmt->task_tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001359
1360 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1361 goto out;
1362
1363 req->tsk_mgmt = iu;
1364
1365 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001366
Roland Dreieraef9ec32005-11-02 14:07:13 -08001367 if (!wait_for_completion_timeout(&req->done,
1368 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001369 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001370
Roland Dreierd945e1d2006-05-09 10:50:28 -07001371 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001372
1373out:
1374 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001375 return -1;
1376}
1377
1378static int srp_find_req(struct srp_target_port *target,
1379 struct scsi_cmnd *scmnd,
1380 struct srp_request **req)
1381{
1382 if (scmnd->host_scribble == (void *) -1L)
1383 return -1;
1384
1385 *req = &target->req_ring[(long) scmnd->host_scribble];
1386
1387 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001388}
1389
1390static int srp_abort(struct scsi_cmnd *scmnd)
1391{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001392 struct srp_target_port *target = host_to_target(scmnd->device->host);
1393 struct srp_request *req;
1394 int ret = SUCCESS;
1395
David Dillow7aa54bd2008-01-07 18:23:41 -05001396 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001397
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001398 if (target->qp_in_error)
1399 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001400 if (srp_find_req(target, scmnd, &req))
1401 return FAILED;
1402 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1403 return FAILED;
1404
1405 spin_lock_irq(target->scsi_host->host_lock);
1406
1407 if (req->cmd_done) {
1408 srp_remove_req(target, req);
1409 scmnd->scsi_done(scmnd);
1410 } else if (!req->tsk_status) {
1411 srp_remove_req(target, req);
1412 scmnd->result = DID_ABORT << 16;
1413 } else
1414 ret = FAILED;
1415
1416 spin_unlock_irq(target->scsi_host->host_lock);
1417
1418 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001419}
1420
1421static int srp_reset_device(struct scsi_cmnd *scmnd)
1422{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001423 struct srp_target_port *target = host_to_target(scmnd->device->host);
1424 struct srp_request *req, *tmp;
1425
David Dillow7aa54bd2008-01-07 18:23:41 -05001426 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001427
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001428 if (target->qp_in_error)
1429 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001430 if (srp_find_req(target, scmnd, &req))
1431 return FAILED;
1432 if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1433 return FAILED;
1434 if (req->tsk_status)
1435 return FAILED;
1436
1437 spin_lock_irq(target->scsi_host->host_lock);
1438
1439 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001440 if (req->scmnd->device == scmnd->device)
1441 srp_reset_req(target, req);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001442
1443 spin_unlock_irq(target->scsi_host->host_lock);
1444
1445 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001446}
1447
1448static int srp_reset_host(struct scsi_cmnd *scmnd)
1449{
1450 struct srp_target_port *target = host_to_target(scmnd->device->host);
1451 int ret = FAILED;
1452
David Dillow7aa54bd2008-01-07 18:23:41 -05001453 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001454
1455 if (!srp_reconnect_target(target))
1456 ret = SUCCESS;
1457
1458 return ret;
1459}
1460
Tony Jonesee959b02008-02-22 00:13:36 +01001461static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
1462 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001463{
Tony Jonesee959b02008-02-22 00:13:36 +01001464 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001465
1466 if (target->state == SRP_TARGET_DEAD ||
1467 target->state == SRP_TARGET_REMOVED)
1468 return -ENODEV;
1469
1470 return sprintf(buf, "0x%016llx\n",
1471 (unsigned long long) be64_to_cpu(target->id_ext));
1472}
1473
Tony Jonesee959b02008-02-22 00:13:36 +01001474static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
1475 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001476{
Tony Jonesee959b02008-02-22 00:13:36 +01001477 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001478
1479 if (target->state == SRP_TARGET_DEAD ||
1480 target->state == SRP_TARGET_REMOVED)
1481 return -ENODEV;
1482
1483 return sprintf(buf, "0x%016llx\n",
1484 (unsigned long long) be64_to_cpu(target->ioc_guid));
1485}
1486
Tony Jonesee959b02008-02-22 00:13:36 +01001487static ssize_t show_service_id(struct device *dev,
1488 struct device_attribute *attr, char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001489{
Tony Jonesee959b02008-02-22 00:13:36 +01001490 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001491
1492 if (target->state == SRP_TARGET_DEAD ||
1493 target->state == SRP_TARGET_REMOVED)
1494 return -ENODEV;
1495
1496 return sprintf(buf, "0x%016llx\n",
1497 (unsigned long long) be64_to_cpu(target->service_id));
1498}
1499
Tony Jonesee959b02008-02-22 00:13:36 +01001500static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
1501 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001502{
Tony Jonesee959b02008-02-22 00:13:36 +01001503 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001504
1505 if (target->state == SRP_TARGET_DEAD ||
1506 target->state == SRP_TARGET_REMOVED)
1507 return -ENODEV;
1508
1509 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1510}
1511
Tony Jonesee959b02008-02-22 00:13:36 +01001512static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
1513 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001514{
Tony Jonesee959b02008-02-22 00:13:36 +01001515 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001516
1517 if (target->state == SRP_TARGET_DEAD ||
1518 target->state == SRP_TARGET_REMOVED)
1519 return -ENODEV;
1520
1521 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1522 be16_to_cpu(((__be16 *) target->path.dgid.raw)[0]),
1523 be16_to_cpu(((__be16 *) target->path.dgid.raw)[1]),
1524 be16_to_cpu(((__be16 *) target->path.dgid.raw)[2]),
1525 be16_to_cpu(((__be16 *) target->path.dgid.raw)[3]),
1526 be16_to_cpu(((__be16 *) target->path.dgid.raw)[4]),
1527 be16_to_cpu(((__be16 *) target->path.dgid.raw)[5]),
1528 be16_to_cpu(((__be16 *) target->path.dgid.raw)[6]),
1529 be16_to_cpu(((__be16 *) target->path.dgid.raw)[7]));
1530}
1531
Tony Jonesee959b02008-02-22 00:13:36 +01001532static ssize_t show_orig_dgid(struct device *dev,
1533 struct device_attribute *attr, char *buf)
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001534{
Tony Jonesee959b02008-02-22 00:13:36 +01001535 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001536
1537 if (target->state == SRP_TARGET_DEAD ||
1538 target->state == SRP_TARGET_REMOVED)
1539 return -ENODEV;
1540
1541 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1542 be16_to_cpu(target->orig_dgid[0]),
1543 be16_to_cpu(target->orig_dgid[1]),
1544 be16_to_cpu(target->orig_dgid[2]),
1545 be16_to_cpu(target->orig_dgid[3]),
1546 be16_to_cpu(target->orig_dgid[4]),
1547 be16_to_cpu(target->orig_dgid[5]),
1548 be16_to_cpu(target->orig_dgid[6]),
1549 be16_to_cpu(target->orig_dgid[7]));
1550}
1551
Tony Jonesee959b02008-02-22 00:13:36 +01001552static ssize_t show_zero_req_lim(struct device *dev,
1553 struct device_attribute *attr, char *buf)
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001554{
Tony Jonesee959b02008-02-22 00:13:36 +01001555 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001556
1557 if (target->state == SRP_TARGET_DEAD ||
1558 target->state == SRP_TARGET_REMOVED)
1559 return -ENODEV;
1560
1561 return sprintf(buf, "%d\n", target->zero_req_lim);
1562}
1563
Tony Jonesee959b02008-02-22 00:13:36 +01001564static ssize_t show_local_ib_port(struct device *dev,
1565 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001566{
Tony Jonesee959b02008-02-22 00:13:36 +01001567 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001568
1569 return sprintf(buf, "%d\n", target->srp_host->port);
1570}
1571
Tony Jonesee959b02008-02-22 00:13:36 +01001572static ssize_t show_local_ib_device(struct device *dev,
1573 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001574{
Tony Jonesee959b02008-02-22 00:13:36 +01001575 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001576
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001577 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001578}
1579
Tony Jonesee959b02008-02-22 00:13:36 +01001580static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1581static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1582static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1583static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1584static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1585static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
1586static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1587static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1588static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001589
Tony Jonesee959b02008-02-22 00:13:36 +01001590static struct device_attribute *srp_host_attrs[] = {
1591 &dev_attr_id_ext,
1592 &dev_attr_ioc_guid,
1593 &dev_attr_service_id,
1594 &dev_attr_pkey,
1595 &dev_attr_dgid,
1596 &dev_attr_orig_dgid,
1597 &dev_attr_zero_req_lim,
1598 &dev_attr_local_ib_port,
1599 &dev_attr_local_ib_device,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001600 NULL
1601};
1602
Roland Dreieraef9ec32005-11-02 14:07:13 -08001603static struct scsi_host_template srp_template = {
1604 .module = THIS_MODULE,
Roland Dreierb7f008f2007-05-06 21:18:11 -07001605 .name = "InfiniBand SRP initiator",
1606 .proc_name = DRV_NAME,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001607 .info = srp_target_info,
1608 .queuecommand = srp_queuecommand,
1609 .eh_abort_handler = srp_abort,
1610 .eh_device_reset_handler = srp_reset_device,
1611 .eh_host_reset_handler = srp_reset_host,
1612 .can_queue = SRP_SQ_SIZE,
1613 .this_id = -1,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001614 .cmd_per_lun = SRP_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001615 .use_clustering = ENABLE_CLUSTERING,
1616 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001617};
1618
1619static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1620{
FUJITA Tomonori32368222007-06-27 16:33:12 +09001621 struct srp_rport_identifiers ids;
1622 struct srp_rport *rport;
1623
Roland Dreieraef9ec32005-11-02 14:07:13 -08001624 sprintf(target->target_name, "SRP.T10:%016llX",
1625 (unsigned long long) be64_to_cpu(target->id_ext));
1626
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001627 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001628 return -ENODEV;
1629
FUJITA Tomonori32368222007-06-27 16:33:12 +09001630 memcpy(ids.port_id, &target->id_ext, 8);
1631 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09001632 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori32368222007-06-27 16:33:12 +09001633 rport = srp_rport_add(target->scsi_host, &ids);
1634 if (IS_ERR(rport)) {
1635 scsi_remove_host(target->scsi_host);
1636 return PTR_ERR(rport);
1637 }
1638
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001639 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001640 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001641 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001642
1643 target->state = SRP_TARGET_LIVE;
1644
Roland Dreieraef9ec32005-11-02 14:07:13 -08001645 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07001646 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001647
1648 return 0;
1649}
1650
Tony Jonesee959b02008-02-22 00:13:36 +01001651static void srp_release_dev(struct device *dev)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001652{
1653 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01001654 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001655
1656 complete(&host->released);
1657}
1658
1659static struct class srp_class = {
1660 .name = "infiniband_srp",
Tony Jonesee959b02008-02-22 00:13:36 +01001661 .dev_release = srp_release_dev
Roland Dreieraef9ec32005-11-02 14:07:13 -08001662};
1663
1664/*
1665 * Target ports are added by writing
1666 *
1667 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1668 * pkey=<P_Key>,service_id=<service ID>
1669 *
1670 * to the add_target sysfs attribute.
1671 */
1672enum {
1673 SRP_OPT_ERR = 0,
1674 SRP_OPT_ID_EXT = 1 << 0,
1675 SRP_OPT_IOC_GUID = 1 << 1,
1676 SRP_OPT_DGID = 1 << 2,
1677 SRP_OPT_PKEY = 1 << 3,
1678 SRP_OPT_SERVICE_ID = 1 << 4,
1679 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07001680 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07001681 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001682 SRP_OPT_INITIATOR_EXT = 1 << 8,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001683 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1684 SRP_OPT_IOC_GUID |
1685 SRP_OPT_DGID |
1686 SRP_OPT_PKEY |
1687 SRP_OPT_SERVICE_ID),
1688};
1689
1690static match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07001691 { SRP_OPT_ID_EXT, "id_ext=%s" },
1692 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1693 { SRP_OPT_DGID, "dgid=%s" },
1694 { SRP_OPT_PKEY, "pkey=%x" },
1695 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1696 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1697 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07001698 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001699 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
Vu Pham52fb2b502006-06-17 20:37:31 -07001700 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001701};
1702
1703static int srp_parse_options(const char *buf, struct srp_target_port *target)
1704{
1705 char *options, *sep_opt;
1706 char *p;
1707 char dgid[3];
1708 substring_t args[MAX_OPT_ARGS];
1709 int opt_mask = 0;
1710 int token;
1711 int ret = -EINVAL;
1712 int i;
1713
1714 options = kstrdup(buf, GFP_KERNEL);
1715 if (!options)
1716 return -ENOMEM;
1717
1718 sep_opt = options;
1719 while ((p = strsep(&sep_opt, ",")) != NULL) {
1720 if (!*p)
1721 continue;
1722
1723 token = match_token(p, srp_opt_tokens, args);
1724 opt_mask |= token;
1725
1726 switch (token) {
1727 case SRP_OPT_ID_EXT:
1728 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001729 if (!p) {
1730 ret = -ENOMEM;
1731 goto out;
1732 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001733 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1734 kfree(p);
1735 break;
1736
1737 case SRP_OPT_IOC_GUID:
1738 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001739 if (!p) {
1740 ret = -ENOMEM;
1741 goto out;
1742 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001743 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1744 kfree(p);
1745 break;
1746
1747 case SRP_OPT_DGID:
1748 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001749 if (!p) {
1750 ret = -ENOMEM;
1751 goto out;
1752 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001753 if (strlen(p) != 32) {
1754 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07001755 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001756 goto out;
1757 }
1758
1759 for (i = 0; i < 16; ++i) {
1760 strlcpy(dgid, p + i * 2, 3);
1761 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1762 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08001763 kfree(p);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001764 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001765 break;
1766
1767 case SRP_OPT_PKEY:
1768 if (match_hex(args, &token)) {
1769 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1770 goto out;
1771 }
1772 target->path.pkey = cpu_to_be16(token);
1773 break;
1774
1775 case SRP_OPT_SERVICE_ID:
1776 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001777 if (!p) {
1778 ret = -ENOMEM;
1779 goto out;
1780 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001781 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
Sean Hefty247e0202007-08-08 15:51:18 -07001782 target->path.service_id = target->service_id;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001783 kfree(p);
1784 break;
1785
1786 case SRP_OPT_MAX_SECT:
1787 if (match_int(args, &token)) {
1788 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1789 goto out;
1790 }
1791 target->scsi_host->max_sectors = token;
1792 break;
1793
Vu Pham52fb2b502006-06-17 20:37:31 -07001794 case SRP_OPT_MAX_CMD_PER_LUN:
1795 if (match_int(args, &token)) {
1796 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1797 goto out;
1798 }
1799 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1800 break;
1801
Ramachandra K0c0450db2006-06-17 20:37:38 -07001802 case SRP_OPT_IO_CLASS:
1803 if (match_hex(args, &token)) {
1804 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1805 goto out;
1806 }
1807 if (token != SRP_REV10_IB_IO_CLASS &&
1808 token != SRP_REV16A_IB_IO_CLASS) {
1809 printk(KERN_WARNING PFX "unknown IO class parameter value"
1810 " %x specified (use %x or %x).\n",
1811 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1812 goto out;
1813 }
1814 target->io_class = token;
1815 break;
1816
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001817 case SRP_OPT_INITIATOR_EXT:
1818 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001819 if (!p) {
1820 ret = -ENOMEM;
1821 goto out;
1822 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001823 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1824 kfree(p);
1825 break;
1826
Roland Dreieraef9ec32005-11-02 14:07:13 -08001827 default:
1828 printk(KERN_WARNING PFX "unknown parameter or missing value "
1829 "'%s' in target creation request\n", p);
1830 goto out;
1831 }
1832 }
1833
1834 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1835 ret = 0;
1836 else
1837 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1838 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1839 !(srp_opt_tokens[i].token & opt_mask))
1840 printk(KERN_WARNING PFX "target creation request is "
1841 "missing parameter '%s'\n",
1842 srp_opt_tokens[i].pattern);
1843
1844out:
1845 kfree(options);
1846 return ret;
1847}
1848
Tony Jonesee959b02008-02-22 00:13:36 +01001849static ssize_t srp_create_target(struct device *dev,
1850 struct device_attribute *attr,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001851 const char *buf, size_t count)
1852{
1853 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01001854 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001855 struct Scsi_Host *target_host;
1856 struct srp_target_port *target;
1857 int ret;
1858 int i;
1859
1860 target_host = scsi_host_alloc(&srp_template,
1861 sizeof (struct srp_target_port));
1862 if (!target_host)
1863 return -ENOMEM;
1864
FUJITA Tomonori32368222007-06-27 16:33:12 +09001865 target_host->transportt = ib_srp_transport_template;
Arne Redlich3c8edf02006-11-15 12:43:00 +01001866 target_host->max_lun = SRP_MAX_LUN;
1867 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08001868
Roland Dreieraef9ec32005-11-02 14:07:13 -08001869 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001870
Ramachandra K0c0450db2006-06-17 20:37:38 -07001871 target->io_class = SRP_REV16A_IB_IO_CLASS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001872 target->scsi_host = target_host;
1873 target->srp_host = host;
1874
Roland Dreierd945e1d2006-05-09 10:50:28 -07001875 INIT_LIST_HEAD(&target->free_reqs);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001876 INIT_LIST_HEAD(&target->req_queue);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001877 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1878 target->req_ring[i].index = i;
1879 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1880 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001881
1882 ret = srp_parse_options(buf, target);
1883 if (ret)
1884 goto err;
1885
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001886 ib_get_cached_gid(host->srp_dev->dev, host->port, 0,
1887 &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001888
David Dillow7aa54bd2008-01-07 18:23:41 -05001889 shost_printk(KERN_DEBUG, target->scsi_host, PFX
1890 "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1891 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
Roland Dreieraef9ec32005-11-02 14:07:13 -08001892 (unsigned long long) be64_to_cpu(target->id_ext),
1893 (unsigned long long) be64_to_cpu(target->ioc_guid),
1894 be16_to_cpu(target->path.pkey),
1895 (unsigned long long) be64_to_cpu(target->service_id),
1896 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]),
1897 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]),
1898 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]),
1899 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]),
1900 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]),
1901 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]),
1902 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]),
1903 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14]));
1904
1905 ret = srp_create_target_ib(target);
1906 if (ret)
1907 goto err;
1908
David Dillow9fe4bcf2008-01-08 17:08:52 -05001909 ret = srp_new_cm_id(target);
1910 if (ret)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001911 goto err_free;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001912
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001913 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001914 ret = srp_connect_target(target);
1915 if (ret) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001916 shost_printk(KERN_ERR, target->scsi_host,
1917 PFX "Connection failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001918 goto err_cm_id;
1919 }
1920
1921 ret = srp_add_target(host, target);
1922 if (ret)
1923 goto err_disconnect;
1924
1925 return count;
1926
1927err_disconnect:
1928 srp_disconnect_target(target);
1929
1930err_cm_id:
1931 ib_destroy_cm_id(target->cm_id);
1932
1933err_free:
1934 srp_free_target_ib(target);
1935
1936err:
1937 scsi_host_put(target_host);
1938
1939 return ret;
1940}
1941
Tony Jonesee959b02008-02-22 00:13:36 +01001942static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001943
Tony Jonesee959b02008-02-22 00:13:36 +01001944static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
1945 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001946{
Tony Jonesee959b02008-02-22 00:13:36 +01001947 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001948
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001949 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001950}
1951
Tony Jonesee959b02008-02-22 00:13:36 +01001952static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001953
Tony Jonesee959b02008-02-22 00:13:36 +01001954static ssize_t show_port(struct device *dev, struct device_attribute *attr,
1955 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001956{
Tony Jonesee959b02008-02-22 00:13:36 +01001957 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001958
1959 return sprintf(buf, "%d\n", host->port);
1960}
1961
Tony Jonesee959b02008-02-22 00:13:36 +01001962static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001963
Roland Dreierf5358a12006-06-17 20:37:29 -07001964static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001965{
1966 struct srp_host *host;
1967
1968 host = kzalloc(sizeof *host, GFP_KERNEL);
1969 if (!host)
1970 return NULL;
1971
1972 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001973 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001974 init_completion(&host->released);
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001975 host->srp_dev = device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001976 host->port = port;
1977
Tony Jonesee959b02008-02-22 00:13:36 +01001978 host->dev.class = &srp_class;
1979 host->dev.parent = device->dev->dma_device;
1980 snprintf(host->dev.bus_id, BUS_ID_SIZE, "srp-%s-%d",
Roland Dreierf5358a12006-06-17 20:37:29 -07001981 device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001982
Tony Jonesee959b02008-02-22 00:13:36 +01001983 if (device_register(&host->dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07001984 goto free_host;
Tony Jonesee959b02008-02-22 00:13:36 +01001985 if (device_create_file(&host->dev, &dev_attr_add_target))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001986 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01001987 if (device_create_file(&host->dev, &dev_attr_ibdev))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001988 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01001989 if (device_create_file(&host->dev, &dev_attr_port))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001990 goto err_class;
1991
1992 return host;
1993
1994err_class:
Tony Jonesee959b02008-02-22 00:13:36 +01001995 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001996
Roland Dreierf5358a12006-06-17 20:37:29 -07001997free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001998 kfree(host);
1999
2000 return NULL;
2001}
2002
2003static void srp_add_one(struct ib_device *device)
2004{
Roland Dreierf5358a12006-06-17 20:37:29 -07002005 struct srp_device *srp_dev;
2006 struct ib_device_attr *dev_attr;
2007 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002008 struct srp_host *host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002009 int s, e, p;
2010
Roland Dreierf5358a12006-06-17 20:37:29 -07002011 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
2012 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08002013 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002014
Roland Dreierf5358a12006-06-17 20:37:29 -07002015 if (ib_query_device(device, dev_attr)) {
2016 printk(KERN_WARNING PFX "Query device failed for %s\n",
2017 device->name);
2018 goto free_attr;
2019 }
2020
2021 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
2022 if (!srp_dev)
2023 goto free_attr;
2024
2025 /*
2026 * Use the smallest page size supported by the HCA, down to a
2027 * minimum of 512 bytes (which is the smallest sector that a
2028 * SCSI command will ever carry).
2029 */
2030 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
2031 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
Roland Dreierbf628dc2006-12-15 14:01:49 -08002032 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
Roland Dreierf5358a12006-06-17 20:37:29 -07002033
2034 INIT_LIST_HEAD(&srp_dev->dev_list);
2035
2036 srp_dev->dev = device;
2037 srp_dev->pd = ib_alloc_pd(device);
2038 if (IS_ERR(srp_dev->pd))
2039 goto free_dev;
2040
2041 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
2042 IB_ACCESS_LOCAL_WRITE |
2043 IB_ACCESS_REMOTE_READ |
2044 IB_ACCESS_REMOTE_WRITE);
2045 if (IS_ERR(srp_dev->mr))
2046 goto err_pd;
2047
2048 memset(&fmr_param, 0, sizeof fmr_param);
2049 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
2050 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
2051 fmr_param.cache = 1;
2052 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
2053 fmr_param.page_shift = srp_dev->fmr_page_shift;
2054 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
2055 IB_ACCESS_REMOTE_WRITE |
2056 IB_ACCESS_REMOTE_READ);
2057
2058 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
2059 if (IS_ERR(srp_dev->fmr_pool))
2060 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002061
Tom Tucker07ebafb2006-08-03 16:02:42 -05002062 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002063 s = 0;
2064 e = 0;
2065 } else {
2066 s = 1;
2067 e = device->phys_port_cnt;
2068 }
2069
2070 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07002071 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002072 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07002073 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002074 }
2075
Roland Dreierf5358a12006-06-17 20:37:29 -07002076 ib_set_client_data(device, &srp_client, srp_dev);
2077
2078 goto free_attr;
2079
2080err_pd:
2081 ib_dealloc_pd(srp_dev->pd);
2082
2083free_dev:
2084 kfree(srp_dev);
2085
2086free_attr:
2087 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002088}
2089
2090static void srp_remove_one(struct ib_device *device)
2091{
Roland Dreierf5358a12006-06-17 20:37:29 -07002092 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002093 struct srp_host *host, *tmp_host;
2094 LIST_HEAD(target_list);
2095 struct srp_target_port *target, *tmp_target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002096
Roland Dreierf5358a12006-06-17 20:37:29 -07002097 srp_dev = ib_get_client_data(device, &srp_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002098
Roland Dreierf5358a12006-06-17 20:37:29 -07002099 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Tony Jonesee959b02008-02-22 00:13:36 +01002100 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002101 /*
2102 * Wait for the sysfs entry to go away, so that no new
2103 * target ports can be created.
2104 */
2105 wait_for_completion(&host->released);
2106
2107 /*
2108 * Mark all target ports as removed, so we stop queueing
2109 * commands and don't try to reconnect.
2110 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002111 spin_lock(&host->target_lock);
Matthew Wilcox549c5fc22006-06-17 20:37:30 -07002112 list_for_each_entry(target, &host->target_list, list) {
Ishai Rabinovitz0c5b3952006-06-17 20:37:31 -07002113 spin_lock_irq(target->scsi_host->host_lock);
2114 target->state = SRP_TARGET_REMOVED;
2115 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002116 }
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002117 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002118
2119 /*
2120 * Wait for any reconnection tasks that may have
2121 * started before we marked our target ports as
2122 * removed, and any target port removal tasks.
2123 */
2124 flush_scheduled_work();
2125
2126 list_for_each_entry_safe(target, tmp_target,
2127 &host->target_list, list) {
David Dillowb0e47c82008-01-03 10:25:27 -08002128 srp_remove_host(target->scsi_host);
Dave Dillowad696982008-01-03 22:35:41 -05002129 scsi_remove_host(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002130 srp_disconnect_target(target);
2131 ib_destroy_cm_id(target->cm_id);
2132 srp_free_target_ib(target);
2133 scsi_host_put(target->scsi_host);
2134 }
2135
Roland Dreieraef9ec32005-11-02 14:07:13 -08002136 kfree(host);
2137 }
2138
Roland Dreierf5358a12006-06-17 20:37:29 -07002139 if (srp_dev->fmr_pool)
2140 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2141 ib_dereg_mr(srp_dev->mr);
2142 ib_dealloc_pd(srp_dev->pd);
2143
2144 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002145}
2146
FUJITA Tomonori32368222007-06-27 16:33:12 +09002147static struct srp_function_template ib_srp_transport_functions = {
2148};
2149
Roland Dreieraef9ec32005-11-02 14:07:13 -08002150static int __init srp_init_module(void)
2151{
2152 int ret;
2153
David Dillow1e89a192008-04-16 21:01:12 -07002154 if (srp_sg_tablesize > 255) {
2155 printk(KERN_WARNING PFX "Clamping srp_sg_tablesize to 255\n");
2156 srp_sg_tablesize = 255;
2157 }
2158
FUJITA Tomonori32368222007-06-27 16:33:12 +09002159 ib_srp_transport_template =
2160 srp_attach_transport(&ib_srp_transport_functions);
2161 if (!ib_srp_transport_template)
2162 return -ENOMEM;
2163
Vu Pham74b0a152006-06-17 20:37:32 -07002164 srp_template.sg_tablesize = srp_sg_tablesize;
2165 srp_max_iu_len = (sizeof (struct srp_cmd) +
2166 sizeof (struct srp_indirect_buf) +
2167 srp_sg_tablesize * 16);
2168
Roland Dreieraef9ec32005-11-02 14:07:13 -08002169 ret = class_register(&srp_class);
2170 if (ret) {
2171 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002172 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002173 return ret;
2174 }
2175
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002176 ib_sa_register_client(&srp_sa_client);
2177
Roland Dreieraef9ec32005-11-02 14:07:13 -08002178 ret = ib_register_client(&srp_client);
2179 if (ret) {
2180 printk(KERN_ERR PFX "couldn't register IB client\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002181 srp_release_transport(ib_srp_transport_template);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002182 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002183 class_unregister(&srp_class);
2184 return ret;
2185 }
2186
2187 return 0;
2188}
2189
2190static void __exit srp_cleanup_module(void)
2191{
2192 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002193 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002194 class_unregister(&srp_class);
FUJITA Tomonori32368222007-06-27 16:33:12 +09002195 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002196}
2197
2198module_init(srp_init_module);
2199module_exit(srp_cleanup_module);