blob: 81cc59ca559538a4cd36debf7a38ba18a82877bd [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
Roland Dreieraef9ec32005-11-02 14:07:13 -080052#include "ib_srp.h"
53
54#define DRV_NAME "ib_srp"
55#define PFX DRV_NAME ": "
56#define DRV_VERSION "0.2"
57#define DRV_RELDATE "November 1, 2005"
58
59MODULE_AUTHOR("Roland Dreier");
60MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
61 "v" DRV_VERSION " (" DRV_RELDATE ")");
62MODULE_LICENSE("Dual BSD/GPL");
63
Vu Pham74b0a152006-06-17 20:37:32 -070064static int srp_sg_tablesize = SRP_DEF_SG_TABLESIZE;
65static int srp_max_iu_len;
66
67module_param(srp_sg_tablesize, int, 0444);
68MODULE_PARM_DESC(srp_sg_tablesize,
David Dillow1e89a192008-04-16 21:01:12 -070069 "Max number of gather/scatter entries per I/O (default is 12, max 255)");
Vu Pham74b0a152006-06-17 20:37:32 -070070
Roland Dreieraef9ec32005-11-02 14:07:13 -080071static int topspin_workarounds = 1;
72
73module_param(topspin_workarounds, int, 0444);
74MODULE_PARM_DESC(topspin_workarounds,
75 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
76
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -070077static int mellanox_workarounds = 1;
78
79module_param(mellanox_workarounds, int, 0444);
80MODULE_PARM_DESC(mellanox_workarounds,
81 "Enable workarounds for Mellanox SRP target bugs if != 0");
82
Roland Dreieraef9ec32005-11-02 14:07:13 -080083static void srp_add_one(struct ib_device *device);
84static void srp_remove_one(struct ib_device *device);
85static void srp_completion(struct ib_cq *cq, void *target_ptr);
86static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
87
FUJITA Tomonori32368222007-06-27 16:33:12 +090088static struct scsi_transport_template *ib_srp_transport_template;
89
Roland Dreieraef9ec32005-11-02 14:07:13 -080090static struct ib_client srp_client = {
91 .name = "srp",
92 .add = srp_add_one,
93 .remove = srp_remove_one
94};
95
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -070096static struct ib_sa_client srp_sa_client;
97
Roland Dreieraef9ec32005-11-02 14:07:13 -080098static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
99{
100 return (struct srp_target_port *) host->hostdata;
101}
102
103static const char *srp_target_info(struct Scsi_Host *host)
104{
105 return host_to_target(host)->target_name;
106}
107
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700108static int srp_target_is_topspin(struct srp_target_port *target)
109{
110 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700111 static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700112
113 return topspin_workarounds &&
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700114 (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
115 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700116}
117
118static int srp_target_is_mellanox(struct srp_target_port *target)
119{
120 static const u8 mellanox_oui[3] = { 0x00, 0x02, 0xc9 };
121
122 return mellanox_workarounds &&
123 !memcmp(&target->ioc_guid, mellanox_oui, sizeof mellanox_oui);
124}
125
Roland Dreieraef9ec32005-11-02 14:07:13 -0800126static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
127 gfp_t gfp_mask,
128 enum dma_data_direction direction)
129{
130 struct srp_iu *iu;
131
132 iu = kmalloc(sizeof *iu, gfp_mask);
133 if (!iu)
134 goto out;
135
136 iu->buf = kzalloc(size, gfp_mask);
137 if (!iu->buf)
138 goto out_free_iu;
139
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100140 iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
141 direction);
142 if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800143 goto out_free_buf;
144
145 iu->size = size;
146 iu->direction = direction;
147
148 return iu;
149
150out_free_buf:
151 kfree(iu->buf);
152out_free_iu:
153 kfree(iu);
154out:
155 return NULL;
156}
157
158static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
159{
160 if (!iu)
161 return;
162
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100163 ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
164 iu->direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800165 kfree(iu->buf);
166 kfree(iu);
167}
168
169static void srp_qp_event(struct ib_event *event, void *context)
170{
171 printk(KERN_ERR PFX "QP event %d\n", event->event);
172}
173
174static int srp_init_qp(struct srp_target_port *target,
175 struct ib_qp *qp)
176{
177 struct ib_qp_attr *attr;
178 int ret;
179
180 attr = kmalloc(sizeof *attr, GFP_KERNEL);
181 if (!attr)
182 return -ENOMEM;
183
Roland Dreier969a60f2008-07-14 23:48:43 -0700184 ret = ib_find_pkey(target->srp_host->srp_dev->dev,
185 target->srp_host->port,
186 be16_to_cpu(target->path.pkey),
187 &attr->pkey_index);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800188 if (ret)
189 goto out;
190
191 attr->qp_state = IB_QPS_INIT;
192 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
193 IB_ACCESS_REMOTE_WRITE);
194 attr->port_num = target->srp_host->port;
195
196 ret = ib_modify_qp(qp, attr,
197 IB_QP_STATE |
198 IB_QP_PKEY_INDEX |
199 IB_QP_ACCESS_FLAGS |
200 IB_QP_PORT);
201
202out:
203 kfree(attr);
204 return ret;
205}
206
David Dillow9fe4bcf2008-01-08 17:08:52 -0500207static int srp_new_cm_id(struct srp_target_port *target)
208{
209 struct ib_cm_id *new_cm_id;
210
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100211 new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
David Dillow9fe4bcf2008-01-08 17:08:52 -0500212 srp_cm_handler, target);
213 if (IS_ERR(new_cm_id))
214 return PTR_ERR(new_cm_id);
215
216 if (target->cm_id)
217 ib_destroy_cm_id(target->cm_id);
218 target->cm_id = new_cm_id;
219
220 return 0;
221}
222
Roland Dreieraef9ec32005-11-02 14:07:13 -0800223static int srp_create_target_ib(struct srp_target_port *target)
224{
225 struct ib_qp_init_attr *init_attr;
226 int ret;
227
228 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
229 if (!init_attr)
230 return -ENOMEM;
231
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100232 target->cq = ib_create_cq(target->srp_host->srp_dev->dev,
233 srp_completion, NULL, target, SRP_CQ_SIZE, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800234 if (IS_ERR(target->cq)) {
235 ret = PTR_ERR(target->cq);
236 goto out;
237 }
238
239 ib_req_notify_cq(target->cq, IB_CQ_NEXT_COMP);
240
241 init_attr->event_handler = srp_qp_event;
242 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
243 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
244 init_attr->cap.max_recv_sge = 1;
245 init_attr->cap.max_send_sge = 1;
246 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
247 init_attr->qp_type = IB_QPT_RC;
248 init_attr->send_cq = target->cq;
249 init_attr->recv_cq = target->cq;
250
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100251 target->qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800252 if (IS_ERR(target->qp)) {
253 ret = PTR_ERR(target->qp);
254 ib_destroy_cq(target->cq);
255 goto out;
256 }
257
258 ret = srp_init_qp(target, target->qp);
259 if (ret) {
260 ib_destroy_qp(target->qp);
261 ib_destroy_cq(target->cq);
262 goto out;
263 }
264
265out:
266 kfree(init_attr);
267 return ret;
268}
269
270static void srp_free_target_ib(struct srp_target_port *target)
271{
272 int i;
273
274 ib_destroy_qp(target->qp);
275 ib_destroy_cq(target->cq);
276
277 for (i = 0; i < SRP_RQ_SIZE; ++i)
278 srp_free_iu(target->srp_host, target->rx_ring[i]);
279 for (i = 0; i < SRP_SQ_SIZE + 1; ++i)
280 srp_free_iu(target->srp_host, target->tx_ring[i]);
281}
282
283static void srp_path_rec_completion(int status,
284 struct ib_sa_path_rec *pathrec,
285 void *target_ptr)
286{
287 struct srp_target_port *target = target_ptr;
288
289 target->status = status;
290 if (status)
David Dillow7aa54bd2008-01-07 18:23:41 -0500291 shost_printk(KERN_ERR, target->scsi_host,
292 PFX "Got failed path rec status %d\n", status);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800293 else
294 target->path = *pathrec;
295 complete(&target->done);
296}
297
298static int srp_lookup_path(struct srp_target_port *target)
299{
300 target->path.numb_path = 1;
301
302 init_completion(&target->done);
303
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700304 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100305 target->srp_host->srp_dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800306 target->srp_host->port,
307 &target->path,
Sean Hefty247e0202007-08-08 15:51:18 -0700308 IB_SA_PATH_REC_SERVICE_ID |
Roland Dreieraef9ec32005-11-02 14:07:13 -0800309 IB_SA_PATH_REC_DGID |
310 IB_SA_PATH_REC_SGID |
311 IB_SA_PATH_REC_NUMB_PATH |
312 IB_SA_PATH_REC_PKEY,
313 SRP_PATH_REC_TIMEOUT_MS,
314 GFP_KERNEL,
315 srp_path_rec_completion,
316 target, &target->path_query);
317 if (target->path_query_id < 0)
318 return target->path_query_id;
319
320 wait_for_completion(&target->done);
321
322 if (target->status < 0)
David Dillow7aa54bd2008-01-07 18:23:41 -0500323 shost_printk(KERN_WARNING, target->scsi_host,
324 PFX "Path record query failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800325
326 return target->status;
327}
328
329static int srp_send_req(struct srp_target_port *target)
330{
331 struct {
332 struct ib_cm_req_param param;
333 struct srp_login_req priv;
334 } *req = NULL;
335 int status;
336
337 req = kzalloc(sizeof *req, GFP_KERNEL);
338 if (!req)
339 return -ENOMEM;
340
341 req->param.primary_path = &target->path;
342 req->param.alternate_path = NULL;
343 req->param.service_id = target->service_id;
344 req->param.qp_num = target->qp->qp_num;
345 req->param.qp_type = target->qp->qp_type;
346 req->param.private_data = &req->priv;
347 req->param.private_data_len = sizeof req->priv;
348 req->param.flow_control = 1;
349
350 get_random_bytes(&req->param.starting_psn, 4);
351 req->param.starting_psn &= 0xffffff;
352
353 /*
354 * Pick some arbitrary defaults here; we could make these
355 * module parameters if anyone cared about setting them.
356 */
357 req->param.responder_resources = 4;
358 req->param.remote_cm_response_timeout = 20;
359 req->param.local_cm_response_timeout = 20;
360 req->param.retry_count = 7;
361 req->param.rnr_retry_count = 7;
362 req->param.max_cm_retries = 15;
363
364 req->priv.opcode = SRP_LOGIN_REQ;
365 req->priv.tag = 0;
Vu Pham74b0a152006-06-17 20:37:32 -0700366 req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800367 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
368 SRP_BUF_FORMAT_INDIRECT);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700369 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700370 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450db2006-06-17 20:37:38 -0700371 * port identifier format is 8 bytes of ID extension followed
372 * by 8 bytes of GUID. Older drafts put the two halves in the
373 * opposite order, so that the GUID comes first.
374 *
375 * Targets conforming to these obsolete drafts can be
376 * recognized by the I/O Class they report.
377 */
378 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
379 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200380 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700381 memcpy(req->priv.initiator_port_id + 8,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200382 &target->initiator_ext, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700383 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
384 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
385 } else {
386 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200387 &target->initiator_ext, 8);
388 memcpy(req->priv.initiator_port_id + 8,
389 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700390 memcpy(req->priv.target_port_id, &target->id_ext, 8);
391 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
392 }
393
Roland Dreieraef9ec32005-11-02 14:07:13 -0800394 /*
395 * Topspin/Cisco SRP targets will reject our login unless we
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200396 * zero out the first 8 bytes of our initiator port ID and set
397 * the second 8 bytes to the local node GUID.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800398 */
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700399 if (srp_target_is_topspin(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500400 shost_printk(KERN_DEBUG, target->scsi_host,
401 PFX "Topspin/Cisco initiator port ID workaround "
402 "activated for target GUID %016llx\n",
403 (unsigned long long) be64_to_cpu(target->ioc_guid));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800404 memset(req->priv.initiator_port_id, 0, 8);
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200405 memcpy(req->priv.initiator_port_id + 8,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100406 &target->srp_host->srp_dev->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800407 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800408
409 status = ib_send_cm_req(target->cm_id, &req->param);
410
411 kfree(req);
412
413 return status;
414}
415
416static void srp_disconnect_target(struct srp_target_port *target)
417{
418 /* XXX should send SRP_I_LOGOUT request */
419
420 init_completion(&target->done);
Roland Dreiere6581052006-05-17 09:13:21 -0700421 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500422 shost_printk(KERN_DEBUG, target->scsi_host,
423 PFX "Sending CM DREQ failed\n");
Roland Dreiere6581052006-05-17 09:13:21 -0700424 return;
425 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800426 wait_for_completion(&target->done);
427}
428
David Howellsc4028952006-11-22 14:57:56 +0000429static void srp_remove_work(struct work_struct *work)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800430{
David Howellsc4028952006-11-22 14:57:56 +0000431 struct srp_target_port *target =
432 container_of(work, struct srp_target_port, work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800433
434 spin_lock_irq(target->scsi_host->host_lock);
435 if (target->state != SRP_TARGET_DEAD) {
436 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800437 return;
438 }
439 target->state = SRP_TARGET_REMOVED;
440 spin_unlock_irq(target->scsi_host->host_lock);
441
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700442 spin_lock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800443 list_del(&target->list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700444 spin_unlock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800445
FUJITA Tomonori32368222007-06-27 16:33:12 +0900446 srp_remove_host(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800447 scsi_remove_host(target->scsi_host);
448 ib_destroy_cm_id(target->cm_id);
449 srp_free_target_ib(target);
450 scsi_host_put(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800451}
452
453static int srp_connect_target(struct srp_target_port *target)
454{
David Dillow9fe4bcf2008-01-08 17:08:52 -0500455 int retries = 3;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800456 int ret;
457
458 ret = srp_lookup_path(target);
459 if (ret)
460 return ret;
461
462 while (1) {
463 init_completion(&target->done);
464 ret = srp_send_req(target);
465 if (ret)
466 return ret;
467 wait_for_completion(&target->done);
468
469 /*
470 * The CM event handling code will set status to
471 * SRP_PORT_REDIRECT if we get a port redirect REJ
472 * back, or SRP_DLID_REDIRECT if we get a lid/qp
473 * redirect REJ back.
474 */
475 switch (target->status) {
476 case 0:
477 return 0;
478
479 case SRP_PORT_REDIRECT:
480 ret = srp_lookup_path(target);
481 if (ret)
482 return ret;
483 break;
484
485 case SRP_DLID_REDIRECT:
486 break;
487
David Dillow9fe4bcf2008-01-08 17:08:52 -0500488 case SRP_STALE_CONN:
489 /* Our current CM id was stale, and is now in timewait.
490 * Try to reconnect with a new one.
491 */
492 if (!retries-- || srp_new_cm_id(target)) {
493 shost_printk(KERN_ERR, target->scsi_host, PFX
494 "giving up on stale connection\n");
495 target->status = -ECONNRESET;
496 return target->status;
497 }
498
499 shost_printk(KERN_ERR, target->scsi_host, PFX
500 "retrying stale connection\n");
501 break;
502
Roland Dreieraef9ec32005-11-02 14:07:13 -0800503 default:
504 return target->status;
505 }
506 }
507}
508
Roland Dreierd945e1d2006-05-09 10:50:28 -0700509static void srp_unmap_data(struct scsi_cmnd *scmnd,
510 struct srp_target_port *target,
511 struct srp_request *req)
512{
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900513 if (!scsi_sglist(scmnd) ||
Roland Dreierd945e1d2006-05-09 10:50:28 -0700514 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
515 scmnd->sc_data_direction != DMA_FROM_DEVICE))
516 return;
517
Roland Dreierf5358a12006-06-17 20:37:29 -0700518 if (req->fmr) {
519 ib_fmr_pool_unmap(req->fmr);
520 req->fmr = NULL;
521 }
522
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100523 ib_dma_unmap_sg(target->srp_host->srp_dev->dev, scsi_sglist(scmnd),
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900524 scsi_sg_count(scmnd), scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700525}
526
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700527static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
528{
529 srp_unmap_data(req->scmnd, target, req);
530 list_move_tail(&req->list, &target->free_reqs);
531}
532
533static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
534{
535 req->scmnd->result = DID_RESET << 16;
536 req->scmnd->scsi_done(req->scmnd);
537 srp_remove_req(target, req);
538}
539
Roland Dreieraef9ec32005-11-02 14:07:13 -0800540static int srp_reconnect_target(struct srp_target_port *target)
541{
Roland Dreieraef9ec32005-11-02 14:07:13 -0800542 struct ib_qp_attr qp_attr;
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700543 struct srp_request *req, *tmp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800544 struct ib_wc wc;
545 int ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800546
547 spin_lock_irq(target->scsi_host->host_lock);
548 if (target->state != SRP_TARGET_LIVE) {
549 spin_unlock_irq(target->scsi_host->host_lock);
550 return -EAGAIN;
551 }
552 target->state = SRP_TARGET_CONNECTING;
553 spin_unlock_irq(target->scsi_host->host_lock);
554
555 srp_disconnect_target(target);
556 /*
557 * Now get a new local CM ID so that we avoid confusing the
558 * target in case things are really fouled up.
559 */
David Dillow9fe4bcf2008-01-08 17:08:52 -0500560 ret = srp_new_cm_id(target);
561 if (ret)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800562 goto err;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800563
564 qp_attr.qp_state = IB_QPS_RESET;
565 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
566 if (ret)
567 goto err;
568
569 ret = srp_init_qp(target, target->qp);
570 if (ret)
571 goto err;
572
573 while (ib_poll_cq(target->cq, 1, &wc) > 0)
574 ; /* nothing */
575
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300576 spin_lock_irq(target->scsi_host->host_lock);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700577 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
578 srp_reset_req(target, req);
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300579 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800580
581 target->rx_head = 0;
582 target->tx_head = 0;
583 target->tx_tail = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800584
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200585 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800586 ret = srp_connect_target(target);
587 if (ret)
588 goto err;
589
590 spin_lock_irq(target->scsi_host->host_lock);
591 if (target->state == SRP_TARGET_CONNECTING) {
592 ret = 0;
593 target->state = SRP_TARGET_LIVE;
594 } else
595 ret = -EAGAIN;
596 spin_unlock_irq(target->scsi_host->host_lock);
597
598 return ret;
599
600err:
David Dillow7aa54bd2008-01-07 18:23:41 -0500601 shost_printk(KERN_ERR, target->scsi_host,
602 PFX "reconnect failed (%d), removing target port.\n", ret);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800603
604 /*
605 * We couldn't reconnect, so kill our target port off.
606 * However, we have to defer the real removal because we might
607 * be in the context of the SCSI error handler now, which
608 * would deadlock if we call scsi_remove_host().
609 */
610 spin_lock_irq(target->scsi_host->host_lock);
611 if (target->state == SRP_TARGET_CONNECTING) {
612 target->state = SRP_TARGET_DEAD;
David Howellsc4028952006-11-22 14:57:56 +0000613 INIT_WORK(&target->work, srp_remove_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800614 schedule_work(&target->work);
615 }
616 spin_unlock_irq(target->scsi_host->host_lock);
617
618 return ret;
619}
620
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700621static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat,
Roland Dreierf5358a12006-06-17 20:37:29 -0700622 int sg_cnt, struct srp_request *req,
623 struct srp_direct_buf *buf)
624{
625 u64 io_addr = 0;
626 u64 *dma_pages;
627 u32 len;
628 int page_cnt;
629 int i, j;
630 int ret;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100631 struct srp_device *dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800632 struct ib_device *ibdev = dev->dev;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900633 struct scatterlist *sg;
Roland Dreierf5358a12006-06-17 20:37:29 -0700634
635 if (!dev->fmr_pool)
636 return -ENODEV;
637
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700638 if (srp_target_is_mellanox(target) &&
639 (ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask))
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700640 return -EINVAL;
641
Roland Dreierf5358a12006-06-17 20:37:29 -0700642 len = page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900643 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
644 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800645
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900646 if (ib_sg_dma_address(ibdev, sg) & ~dev->fmr_page_mask) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700647 if (i > 0)
648 return -EINVAL;
649 else
650 ++page_cnt;
651 }
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900652 if ((ib_sg_dma_address(ibdev, sg) + dma_len) &
Roland Dreierf5358a12006-06-17 20:37:29 -0700653 ~dev->fmr_page_mask) {
654 if (i < sg_cnt - 1)
655 return -EINVAL;
656 else
657 ++page_cnt;
658 }
659
Ralph Campbell85507bc2006-12-12 14:30:55 -0800660 len += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700661 }
662
663 page_cnt += len >> dev->fmr_page_shift;
664 if (page_cnt > SRP_FMR_SIZE)
665 return -ENOMEM;
666
667 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
668 if (!dma_pages)
669 return -ENOMEM;
670
671 page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900672 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
673 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800674
675 for (j = 0; j < dma_len; j += dev->fmr_page_size)
Roland Dreierf5358a12006-06-17 20:37:29 -0700676 dma_pages[page_cnt++] =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900677 (ib_sg_dma_address(ibdev, sg) &
Ralph Campbell85507bc2006-12-12 14:30:55 -0800678 dev->fmr_page_mask) + j;
679 }
Roland Dreierf5358a12006-06-17 20:37:29 -0700680
681 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
Michael S. Tsirkinadfaa882006-07-14 00:23:55 -0700682 dma_pages, page_cnt, io_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -0700683 if (IS_ERR(req->fmr)) {
684 ret = PTR_ERR(req->fmr);
Vu Pham6583eb32006-07-14 00:23:53 -0700685 req->fmr = NULL;
Roland Dreierf5358a12006-06-17 20:37:29 -0700686 goto out;
687 }
688
Ralph Campbell85507bc2006-12-12 14:30:55 -0800689 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) &
690 ~dev->fmr_page_mask);
Roland Dreierf5358a12006-06-17 20:37:29 -0700691 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
692 buf->len = cpu_to_be32(len);
693
694 ret = 0;
695
696out:
697 kfree(dma_pages);
698
699 return ret;
700}
701
Roland Dreieraef9ec32005-11-02 14:07:13 -0800702static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
703 struct srp_request *req)
704{
Roland Dreiercf368712006-03-24 15:47:26 -0800705 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800706 struct srp_cmd *cmd = req->cmd->buf;
Roland Dreiercf368712006-03-24 15:47:26 -0800707 int len, nents, count;
Roland Dreierf5358a12006-06-17 20:37:29 -0700708 u8 fmt = SRP_DATA_DESC_DIRECT;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800709 struct srp_device *dev;
710 struct ib_device *ibdev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800711
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900712 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800713 return sizeof (struct srp_cmd);
714
715 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
716 scmnd->sc_data_direction != DMA_TO_DEVICE) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500717 shost_printk(KERN_WARNING, target->scsi_host,
718 PFX "Unhandled data direction %d\n",
719 scmnd->sc_data_direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800720 return -EINVAL;
721 }
722
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900723 nents = scsi_sg_count(scmnd);
724 scat = scsi_sglist(scmnd);
Roland Dreiercf368712006-03-24 15:47:26 -0800725
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100726 dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800727 ibdev = dev->dev;
728
729 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
Roland Dreierf5358a12006-06-17 20:37:29 -0700730
731 fmt = SRP_DATA_DESC_DIRECT;
732 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800733
734 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700735 /*
736 * The midlayer only generated a single gather/scatter
737 * entry, or DMA mapping coalesced everything to a
738 * single entry. So a direct descriptor along with
739 * the DMA MR suffices.
740 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800741 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800742
Ralph Campbell85507bc2006-12-12 14:30:55 -0800743 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
744 buf->key = cpu_to_be32(dev->mr->rkey);
745 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700746 } else if (srp_map_fmr(target, scat, count, req,
Roland Dreierf5358a12006-06-17 20:37:29 -0700747 (void *) cmd->add_data)) {
748 /*
749 * FMR mapping failed, and the scatterlist has more
750 * than one entry. Generate an indirect memory
751 * descriptor.
752 */
Roland Dreiercf368712006-03-24 15:47:26 -0800753 struct srp_indirect_buf *buf = (void *) cmd->add_data;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900754 struct scatterlist *sg;
Roland Dreiercf368712006-03-24 15:47:26 -0800755 u32 datalen = 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700756 int i;
Roland Dreiercf368712006-03-24 15:47:26 -0800757
758 fmt = SRP_DATA_DESC_INDIRECT;
Roland Dreierf5358a12006-06-17 20:37:29 -0700759 len = sizeof (struct srp_cmd) +
760 sizeof (struct srp_indirect_buf) +
761 count * sizeof (struct srp_direct_buf);
762
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900763 scsi_for_each_sg(scmnd, sg, count, i) {
764 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800765
Roland Dreierf5358a12006-06-17 20:37:29 -0700766 buf->desc_list[i].va =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900767 cpu_to_be64(ib_sg_dma_address(ibdev, sg));
Roland Dreierf5358a12006-06-17 20:37:29 -0700768 buf->desc_list[i].key =
Ralph Campbell85507bc2006-12-12 14:30:55 -0800769 cpu_to_be32(dev->mr->rkey);
770 buf->desc_list[i].len = cpu_to_be32(dma_len);
771 datalen += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700772 }
Roland Dreiercf368712006-03-24 15:47:26 -0800773
774 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
775 cmd->data_out_desc_cnt = count;
776 else
777 cmd->data_in_desc_cnt = count;
778
Roland Dreierf5358a12006-06-17 20:37:29 -0700779 buf->table_desc.va =
780 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800781 buf->table_desc.key =
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100782 cpu_to_be32(target->srp_host->srp_dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800783 buf->table_desc.len =
784 cpu_to_be32(count * sizeof (struct srp_direct_buf));
785
Roland Dreiercf368712006-03-24 15:47:26 -0800786 buf->len = cpu_to_be32(datalen);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800787 }
788
789 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
790 cmd->buf_fmt = fmt << 4;
791 else
792 cmd->buf_fmt = fmt;
793
Roland Dreieraef9ec32005-11-02 14:07:13 -0800794 return len;
795}
796
Roland Dreieraef9ec32005-11-02 14:07:13 -0800797static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
798{
799 struct srp_request *req;
800 struct scsi_cmnd *scmnd;
801 unsigned long flags;
802 s32 delta;
803
804 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
805
806 spin_lock_irqsave(target->scsi_host->host_lock, flags);
807
808 target->req_lim += delta;
809
810 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
811
812 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
813 if (be32_to_cpu(rsp->resp_data_len) < 4)
814 req->tsk_status = -1;
815 else
816 req->tsk_status = rsp->data[3];
817 complete(&req->done);
818 } else {
Roland Dreierd945e1d2006-05-09 10:50:28 -0700819 scmnd = req->scmnd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800820 if (!scmnd)
David Dillow7aa54bd2008-01-07 18:23:41 -0500821 shost_printk(KERN_ERR, target->scsi_host,
822 "Null scmnd for RSP w/tag %016llx\n",
823 (unsigned long long) rsp->tag);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800824 scmnd->result = rsp->status;
825
826 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
827 memcpy(scmnd->sense_buffer, rsp->data +
828 be32_to_cpu(rsp->resp_data_len),
829 min_t(int, be32_to_cpu(rsp->sense_data_len),
830 SCSI_SENSE_BUFFERSIZE));
831 }
832
833 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900834 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800835 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900836 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800837
Roland Dreieraef9ec32005-11-02 14:07:13 -0800838 if (!req->tsk_mgmt) {
Roland Dreieraef9ec32005-11-02 14:07:13 -0800839 scmnd->host_scribble = (void *) -1L;
840 scmnd->scsi_done(scmnd);
841
Roland Dreierd945e1d2006-05-09 10:50:28 -0700842 srp_remove_req(target, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800843 } else
844 req->cmd_done = 1;
845 }
846
847 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
848}
849
Roland Dreieraef9ec32005-11-02 14:07:13 -0800850static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
851{
Ralph Campbell85507bc2006-12-12 14:30:55 -0800852 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800853 struct srp_iu *iu;
854 u8 opcode;
855
856 iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
857
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100858 dev = target->srp_host->srp_dev->dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800859 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
860 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800861
862 opcode = *(u8 *) iu->buf;
863
864 if (0) {
865 int i;
866
David Dillow7aa54bd2008-01-07 18:23:41 -0500867 shost_printk(KERN_ERR, target->scsi_host,
868 PFX "recv completion, opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800869
870 for (i = 0; i < wc->byte_len; ++i) {
871 if (i % 8 == 0)
872 printk(KERN_ERR " [%02x] ", i);
873 printk(" %02x", ((u8 *) iu->buf)[i]);
874 if ((i + 1) % 8 == 0)
875 printk("\n");
876 }
877
878 if (wc->byte_len % 8)
879 printk("\n");
880 }
881
882 switch (opcode) {
883 case SRP_RSP:
884 srp_process_rsp(target, iu->buf);
885 break;
886
887 case SRP_T_LOGOUT:
888 /* XXX Handle target logout */
David Dillow7aa54bd2008-01-07 18:23:41 -0500889 shost_printk(KERN_WARNING, target->scsi_host,
890 PFX "Got target logout request\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800891 break;
892
893 default:
David Dillow7aa54bd2008-01-07 18:23:41 -0500894 shost_printk(KERN_WARNING, target->scsi_host,
895 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800896 break;
897 }
898
Ralph Campbell85507bc2006-12-12 14:30:55 -0800899 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
900 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800901}
902
903static void srp_completion(struct ib_cq *cq, void *target_ptr)
904{
905 struct srp_target_port *target = target_ptr;
906 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800907
908 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
909 while (ib_poll_cq(cq, 1, &wc) > 0) {
910 if (wc.status) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500911 shost_printk(KERN_ERR, target->scsi_host,
912 PFX "failed %s status %d\n",
913 wc.wr_id & SRP_OP_RECV ? "receive" : "send",
914 wc.status);
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200915 target->qp_in_error = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800916 break;
917 }
918
919 if (wc.wr_id & SRP_OP_RECV)
920 srp_handle_recv(target, &wc);
921 else
922 ++target->tx_tail;
923 }
924}
925
926static int __srp_post_recv(struct srp_target_port *target)
927{
928 struct srp_iu *iu;
929 struct ib_sge list;
930 struct ib_recv_wr wr, *bad_wr;
931 unsigned int next;
932 int ret;
933
934 next = target->rx_head & (SRP_RQ_SIZE - 1);
935 wr.wr_id = next | SRP_OP_RECV;
936 iu = target->rx_ring[next];
937
938 list.addr = iu->dma;
939 list.length = iu->size;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100940 list.lkey = target->srp_host->srp_dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800941
942 wr.next = NULL;
943 wr.sg_list = &list;
944 wr.num_sge = 1;
945
946 ret = ib_post_recv(target->qp, &wr, &bad_wr);
947 if (!ret)
948 ++target->rx_head;
949
950 return ret;
951}
952
953static int srp_post_recv(struct srp_target_port *target)
954{
955 unsigned long flags;
956 int ret;
957
958 spin_lock_irqsave(target->scsi_host->host_lock, flags);
959 ret = __srp_post_recv(target);
960 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
961
962 return ret;
963}
964
965/*
966 * Must be called with target->scsi_host->host_lock held to protect
Roland Dreier47f2bce2005-11-15 00:19:21 -0800967 * req_lim and tx_head. Lock cannot be dropped between call here and
968 * call to __srp_post_send().
Roland Dreieraef9ec32005-11-02 14:07:13 -0800969 */
David Dillow8cba2072007-12-19 17:08:43 -0500970static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
971 enum srp_request_type req_type)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800972{
David Dillow8cba2072007-12-19 17:08:43 -0500973 s32 min = (req_type == SRP_REQ_TASK_MGMT) ? 1 : 2;
974
Roland Dreieraef9ec32005-11-02 14:07:13 -0800975 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
976 return NULL;
977
David Dillow8cba2072007-12-19 17:08:43 -0500978 if (target->req_lim < min) {
Roland Dreier6bfa24f2006-06-17 20:37:33 -0700979 ++target->zero_req_lim;
David Dillow8cba2072007-12-19 17:08:43 -0500980 return NULL;
981 }
Roland Dreier47f2bce2005-11-15 00:19:21 -0800982
Roland Dreieraef9ec32005-11-02 14:07:13 -0800983 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
984}
985
986/*
987 * Must be called with target->scsi_host->host_lock held to protect
988 * req_lim and tx_head.
989 */
990static int __srp_post_send(struct srp_target_port *target,
991 struct srp_iu *iu, int len)
992{
993 struct ib_sge list;
994 struct ib_send_wr wr, *bad_wr;
995 int ret = 0;
996
Roland Dreieraef9ec32005-11-02 14:07:13 -0800997 list.addr = iu->dma;
998 list.length = len;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100999 list.lkey = target->srp_host->srp_dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001000
1001 wr.next = NULL;
1002 wr.wr_id = target->tx_head & SRP_SQ_SIZE;
1003 wr.sg_list = &list;
1004 wr.num_sge = 1;
1005 wr.opcode = IB_WR_SEND;
1006 wr.send_flags = IB_SEND_SIGNALED;
1007
1008 ret = ib_post_send(target->qp, &wr, &bad_wr);
1009
1010 if (!ret) {
1011 ++target->tx_head;
1012 --target->req_lim;
1013 }
1014
1015 return ret;
1016}
1017
1018static int srp_queuecommand(struct scsi_cmnd *scmnd,
1019 void (*done)(struct scsi_cmnd *))
1020{
1021 struct srp_target_port *target = host_to_target(scmnd->device->host);
1022 struct srp_request *req;
1023 struct srp_iu *iu;
1024 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001025 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001026 int len;
1027
1028 if (target->state == SRP_TARGET_CONNECTING)
1029 goto err;
1030
1031 if (target->state == SRP_TARGET_DEAD ||
1032 target->state == SRP_TARGET_REMOVED) {
1033 scmnd->result = DID_BAD_TARGET << 16;
1034 done(scmnd);
1035 return 0;
1036 }
1037
David Dillow8cba2072007-12-19 17:08:43 -05001038 iu = __srp_get_tx_iu(target, SRP_REQ_NORMAL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001039 if (!iu)
1040 goto err;
1041
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001042 dev = target->srp_host->srp_dev->dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001043 ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len,
1044 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001045
Roland Dreierd945e1d2006-05-09 10:50:28 -07001046 req = list_entry(target->free_reqs.next, struct srp_request, list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001047
1048 scmnd->scsi_done = done;
1049 scmnd->result = 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001050 scmnd->host_scribble = (void *) (long) req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001051
1052 cmd = iu->buf;
1053 memset(cmd, 0, sizeof *cmd);
1054
1055 cmd->opcode = SRP_CMD;
1056 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001057 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001058 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1059
Roland Dreieraef9ec32005-11-02 14:07:13 -08001060 req->scmnd = scmnd;
1061 req->cmd = iu;
1062 req->cmd_done = 0;
1063 req->tsk_mgmt = NULL;
1064
1065 len = srp_map_data(scmnd, target, req);
1066 if (len < 0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001067 shost_printk(KERN_ERR, target->scsi_host,
1068 PFX "Failed to map data\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001069 goto err;
1070 }
1071
1072 if (__srp_post_recv(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001073 shost_printk(KERN_ERR, target->scsi_host, PFX "Recv failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001074 goto err_unmap;
1075 }
1076
Ralph Campbell85507bc2006-12-12 14:30:55 -08001077 ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len,
1078 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001079
1080 if (__srp_post_send(target, iu, len)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001081 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001082 goto err_unmap;
1083 }
1084
Roland Dreierd945e1d2006-05-09 10:50:28 -07001085 list_move_tail(&req->list, &target->req_queue);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001086
1087 return 0;
1088
1089err_unmap:
1090 srp_unmap_data(scmnd, target, req);
1091
1092err:
1093 return SCSI_MLQUEUE_HOST_BUSY;
1094}
1095
1096static int srp_alloc_iu_bufs(struct srp_target_port *target)
1097{
1098 int i;
1099
1100 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1101 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1102 target->max_ti_iu_len,
1103 GFP_KERNEL, DMA_FROM_DEVICE);
1104 if (!target->rx_ring[i])
1105 goto err;
1106 }
1107
1108 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1109 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
Vu Pham74b0a152006-06-17 20:37:32 -07001110 srp_max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001111 GFP_KERNEL, DMA_TO_DEVICE);
1112 if (!target->tx_ring[i])
1113 goto err;
1114 }
1115
1116 return 0;
1117
1118err:
1119 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1120 srp_free_iu(target->srp_host, target->rx_ring[i]);
1121 target->rx_ring[i] = NULL;
1122 }
1123
1124 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1125 srp_free_iu(target->srp_host, target->tx_ring[i]);
1126 target->tx_ring[i] = NULL;
1127 }
1128
1129 return -ENOMEM;
1130}
1131
1132static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1133 struct ib_cm_event *event,
1134 struct srp_target_port *target)
1135{
David Dillow7aa54bd2008-01-07 18:23:41 -05001136 struct Scsi_Host *shost = target->scsi_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001137 struct ib_class_port_info *cpi;
1138 int opcode;
1139
1140 switch (event->param.rej_rcvd.reason) {
1141 case IB_CM_REJ_PORT_CM_REDIRECT:
1142 cpi = event->param.rej_rcvd.ari;
1143 target->path.dlid = cpi->redirect_lid;
1144 target->path.pkey = cpi->redirect_pkey;
1145 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1146 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1147
1148 target->status = target->path.dlid ?
1149 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1150 break;
1151
1152 case IB_CM_REJ_PORT_REDIRECT:
Roland Dreier5d7cbfd2007-08-03 10:45:18 -07001153 if (srp_target_is_topspin(target)) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001154 /*
1155 * Topspin/Cisco SRP gateways incorrectly send
1156 * reject reason code 25 when they mean 24
1157 * (port redirect).
1158 */
1159 memcpy(target->path.dgid.raw,
1160 event->param.rej_rcvd.ari, 16);
1161
David Dillow7aa54bd2008-01-07 18:23:41 -05001162 shost_printk(KERN_DEBUG, shost,
1163 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1164 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1165 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001166
1167 target->status = SRP_PORT_REDIRECT;
1168 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001169 shost_printk(KERN_WARNING, shost,
1170 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001171 target->status = -ECONNRESET;
1172 }
1173 break;
1174
1175 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
David Dillow7aa54bd2008-01-07 18:23:41 -05001176 shost_printk(KERN_WARNING, shost,
1177 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001178 target->status = -ECONNRESET;
1179 break;
1180
1181 case IB_CM_REJ_CONSUMER_DEFINED:
1182 opcode = *(u8 *) event->private_data;
1183 if (opcode == SRP_LOGIN_REJ) {
1184 struct srp_login_rej *rej = event->private_data;
1185 u32 reason = be32_to_cpu(rej->reason);
1186
1187 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
David Dillow7aa54bd2008-01-07 18:23:41 -05001188 shost_printk(KERN_WARNING, shost,
1189 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001190 else
David Dillow7aa54bd2008-01-07 18:23:41 -05001191 shost_printk(KERN_WARNING, shost,
1192 PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001193 } else
David Dillow7aa54bd2008-01-07 18:23:41 -05001194 shost_printk(KERN_WARNING, shost,
1195 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1196 " opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001197 target->status = -ECONNRESET;
1198 break;
1199
David Dillow9fe4bcf2008-01-08 17:08:52 -05001200 case IB_CM_REJ_STALE_CONN:
1201 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
1202 target->status = SRP_STALE_CONN;
1203 break;
1204
Roland Dreieraef9ec32005-11-02 14:07:13 -08001205 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001206 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
1207 event->param.rej_rcvd.reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001208 target->status = -ECONNRESET;
1209 }
1210}
1211
1212static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1213{
1214 struct srp_target_port *target = cm_id->context;
1215 struct ib_qp_attr *qp_attr = NULL;
1216 int attr_mask = 0;
1217 int comp = 0;
1218 int opcode = 0;
1219
1220 switch (event->event) {
1221 case IB_CM_REQ_ERROR:
David Dillow7aa54bd2008-01-07 18:23:41 -05001222 shost_printk(KERN_DEBUG, target->scsi_host,
1223 PFX "Sending CM REQ failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001224 comp = 1;
1225 target->status = -ECONNRESET;
1226 break;
1227
1228 case IB_CM_REP_RECEIVED:
1229 comp = 1;
1230 opcode = *(u8 *) event->private_data;
1231
1232 if (opcode == SRP_LOGIN_RSP) {
1233 struct srp_login_rsp *rsp = event->private_data;
1234
1235 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1236 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1237
1238 target->scsi_host->can_queue = min(target->req_lim,
1239 target->scsi_host->can_queue);
1240 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001241 shost_printk(KERN_WARNING, target->scsi_host,
1242 PFX "Unhandled RSP opcode %#x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001243 target->status = -ECONNRESET;
1244 break;
1245 }
1246
Vu Phamd2fcea72006-11-21 14:14:10 -08001247 if (!target->rx_ring[0]) {
1248 target->status = srp_alloc_iu_bufs(target);
1249 if (target->status)
1250 break;
1251 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001252
1253 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1254 if (!qp_attr) {
1255 target->status = -ENOMEM;
1256 break;
1257 }
1258
1259 qp_attr->qp_state = IB_QPS_RTR;
1260 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1261 if (target->status)
1262 break;
1263
1264 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1265 if (target->status)
1266 break;
1267
1268 target->status = srp_post_recv(target);
1269 if (target->status)
1270 break;
1271
1272 qp_attr->qp_state = IB_QPS_RTS;
1273 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1274 if (target->status)
1275 break;
1276
1277 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1278 if (target->status)
1279 break;
1280
1281 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1282 if (target->status)
1283 break;
1284
1285 break;
1286
1287 case IB_CM_REJ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001288 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001289 comp = 1;
1290
1291 srp_cm_rej_handler(cm_id, event, target);
1292 break;
1293
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001294 case IB_CM_DREQ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001295 shost_printk(KERN_WARNING, target->scsi_host,
1296 PFX "DREQ received - connection closed\n");
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001297 if (ib_send_cm_drep(cm_id, NULL, 0))
David Dillow7aa54bd2008-01-07 18:23:41 -05001298 shost_printk(KERN_ERR, target->scsi_host,
1299 PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001300 break;
1301
1302 case IB_CM_TIMEWAIT_EXIT:
David Dillow7aa54bd2008-01-07 18:23:41 -05001303 shost_printk(KERN_ERR, target->scsi_host,
1304 PFX "connection closed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001305
1306 comp = 1;
1307 target->status = 0;
1308 break;
1309
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001310 case IB_CM_MRA_RECEIVED:
1311 case IB_CM_DREQ_ERROR:
1312 case IB_CM_DREP_RECEIVED:
1313 break;
1314
Roland Dreieraef9ec32005-11-02 14:07:13 -08001315 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001316 shost_printk(KERN_WARNING, target->scsi_host,
1317 PFX "Unhandled CM event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001318 break;
1319 }
1320
1321 if (comp)
1322 complete(&target->done);
1323
1324 kfree(qp_attr);
1325
1326 return 0;
1327}
1328
Roland Dreierd945e1d2006-05-09 10:50:28 -07001329static int srp_send_tsk_mgmt(struct srp_target_port *target,
1330 struct srp_request *req, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001331{
Roland Dreieraef9ec32005-11-02 14:07:13 -08001332 struct srp_iu *iu;
1333 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001334
1335 spin_lock_irq(target->scsi_host->host_lock);
1336
Roland Dreier1285b3a2006-03-03 15:47:25 -08001337 if (target->state == SRP_TARGET_DEAD ||
1338 target->state == SRP_TARGET_REMOVED) {
Roland Dreierd945e1d2006-05-09 10:50:28 -07001339 req->scmnd->result = DID_BAD_TARGET << 16;
Roland Dreier1285b3a2006-03-03 15:47:25 -08001340 goto out;
1341 }
1342
Roland Dreieraef9ec32005-11-02 14:07:13 -08001343 init_completion(&req->done);
1344
David Dillow8cba2072007-12-19 17:08:43 -05001345 iu = __srp_get_tx_iu(target, SRP_REQ_TASK_MGMT);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001346 if (!iu)
1347 goto out;
1348
1349 tsk_mgmt = iu->buf;
1350 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1351
1352 tsk_mgmt->opcode = SRP_TSK_MGMT;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001353 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1354 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001355 tsk_mgmt->tsk_mgmt_func = func;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001356 tsk_mgmt->task_tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001357
1358 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1359 goto out;
1360
1361 req->tsk_mgmt = iu;
1362
1363 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001364
Roland Dreieraef9ec32005-11-02 14:07:13 -08001365 if (!wait_for_completion_timeout(&req->done,
1366 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001367 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001368
Roland Dreierd945e1d2006-05-09 10:50:28 -07001369 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001370
1371out:
1372 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001373 return -1;
1374}
1375
1376static int srp_find_req(struct srp_target_port *target,
1377 struct scsi_cmnd *scmnd,
1378 struct srp_request **req)
1379{
1380 if (scmnd->host_scribble == (void *) -1L)
1381 return -1;
1382
1383 *req = &target->req_ring[(long) scmnd->host_scribble];
1384
1385 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001386}
1387
1388static int srp_abort(struct scsi_cmnd *scmnd)
1389{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001390 struct srp_target_port *target = host_to_target(scmnd->device->host);
1391 struct srp_request *req;
1392 int ret = SUCCESS;
1393
David Dillow7aa54bd2008-01-07 18:23:41 -05001394 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001395
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001396 if (target->qp_in_error)
1397 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001398 if (srp_find_req(target, scmnd, &req))
1399 return FAILED;
1400 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1401 return FAILED;
1402
1403 spin_lock_irq(target->scsi_host->host_lock);
1404
1405 if (req->cmd_done) {
1406 srp_remove_req(target, req);
1407 scmnd->scsi_done(scmnd);
1408 } else if (!req->tsk_status) {
1409 srp_remove_req(target, req);
1410 scmnd->result = DID_ABORT << 16;
1411 } else
1412 ret = FAILED;
1413
1414 spin_unlock_irq(target->scsi_host->host_lock);
1415
1416 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001417}
1418
1419static int srp_reset_device(struct scsi_cmnd *scmnd)
1420{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001421 struct srp_target_port *target = host_to_target(scmnd->device->host);
1422 struct srp_request *req, *tmp;
1423
David Dillow7aa54bd2008-01-07 18:23:41 -05001424 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001425
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001426 if (target->qp_in_error)
1427 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001428 if (srp_find_req(target, scmnd, &req))
1429 return FAILED;
1430 if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1431 return FAILED;
1432 if (req->tsk_status)
1433 return FAILED;
1434
1435 spin_lock_irq(target->scsi_host->host_lock);
1436
1437 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001438 if (req->scmnd->device == scmnd->device)
1439 srp_reset_req(target, req);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001440
1441 spin_unlock_irq(target->scsi_host->host_lock);
1442
1443 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001444}
1445
1446static int srp_reset_host(struct scsi_cmnd *scmnd)
1447{
1448 struct srp_target_port *target = host_to_target(scmnd->device->host);
1449 int ret = FAILED;
1450
David Dillow7aa54bd2008-01-07 18:23:41 -05001451 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001452
1453 if (!srp_reconnect_target(target))
1454 ret = SUCCESS;
1455
1456 return ret;
1457}
1458
Tony Jonesee959b02008-02-22 00:13:36 +01001459static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
1460 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001461{
Tony Jonesee959b02008-02-22 00:13:36 +01001462 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001463
1464 if (target->state == SRP_TARGET_DEAD ||
1465 target->state == SRP_TARGET_REMOVED)
1466 return -ENODEV;
1467
1468 return sprintf(buf, "0x%016llx\n",
1469 (unsigned long long) be64_to_cpu(target->id_ext));
1470}
1471
Tony Jonesee959b02008-02-22 00:13:36 +01001472static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
1473 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001474{
Tony Jonesee959b02008-02-22 00:13:36 +01001475 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001476
1477 if (target->state == SRP_TARGET_DEAD ||
1478 target->state == SRP_TARGET_REMOVED)
1479 return -ENODEV;
1480
1481 return sprintf(buf, "0x%016llx\n",
1482 (unsigned long long) be64_to_cpu(target->ioc_guid));
1483}
1484
Tony Jonesee959b02008-02-22 00:13:36 +01001485static ssize_t show_service_id(struct device *dev,
1486 struct device_attribute *attr, char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001487{
Tony Jonesee959b02008-02-22 00:13:36 +01001488 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001489
1490 if (target->state == SRP_TARGET_DEAD ||
1491 target->state == SRP_TARGET_REMOVED)
1492 return -ENODEV;
1493
1494 return sprintf(buf, "0x%016llx\n",
1495 (unsigned long long) be64_to_cpu(target->service_id));
1496}
1497
Tony Jonesee959b02008-02-22 00:13:36 +01001498static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
1499 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001500{
Tony Jonesee959b02008-02-22 00:13:36 +01001501 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001502
1503 if (target->state == SRP_TARGET_DEAD ||
1504 target->state == SRP_TARGET_REMOVED)
1505 return -ENODEV;
1506
1507 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1508}
1509
Tony Jonesee959b02008-02-22 00:13:36 +01001510static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
1511 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001512{
Tony Jonesee959b02008-02-22 00:13:36 +01001513 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001514
1515 if (target->state == SRP_TARGET_DEAD ||
1516 target->state == SRP_TARGET_REMOVED)
1517 return -ENODEV;
1518
1519 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1520 be16_to_cpu(((__be16 *) target->path.dgid.raw)[0]),
1521 be16_to_cpu(((__be16 *) target->path.dgid.raw)[1]),
1522 be16_to_cpu(((__be16 *) target->path.dgid.raw)[2]),
1523 be16_to_cpu(((__be16 *) target->path.dgid.raw)[3]),
1524 be16_to_cpu(((__be16 *) target->path.dgid.raw)[4]),
1525 be16_to_cpu(((__be16 *) target->path.dgid.raw)[5]),
1526 be16_to_cpu(((__be16 *) target->path.dgid.raw)[6]),
1527 be16_to_cpu(((__be16 *) target->path.dgid.raw)[7]));
1528}
1529
Tony Jonesee959b02008-02-22 00:13:36 +01001530static ssize_t show_orig_dgid(struct device *dev,
1531 struct device_attribute *attr, char *buf)
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001532{
Tony Jonesee959b02008-02-22 00:13:36 +01001533 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001534
1535 if (target->state == SRP_TARGET_DEAD ||
1536 target->state == SRP_TARGET_REMOVED)
1537 return -ENODEV;
1538
1539 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1540 be16_to_cpu(target->orig_dgid[0]),
1541 be16_to_cpu(target->orig_dgid[1]),
1542 be16_to_cpu(target->orig_dgid[2]),
1543 be16_to_cpu(target->orig_dgid[3]),
1544 be16_to_cpu(target->orig_dgid[4]),
1545 be16_to_cpu(target->orig_dgid[5]),
1546 be16_to_cpu(target->orig_dgid[6]),
1547 be16_to_cpu(target->orig_dgid[7]));
1548}
1549
Tony Jonesee959b02008-02-22 00:13:36 +01001550static ssize_t show_zero_req_lim(struct device *dev,
1551 struct device_attribute *attr, char *buf)
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001552{
Tony Jonesee959b02008-02-22 00:13:36 +01001553 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001554
1555 if (target->state == SRP_TARGET_DEAD ||
1556 target->state == SRP_TARGET_REMOVED)
1557 return -ENODEV;
1558
1559 return sprintf(buf, "%d\n", target->zero_req_lim);
1560}
1561
Tony Jonesee959b02008-02-22 00:13:36 +01001562static ssize_t show_local_ib_port(struct device *dev,
1563 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001564{
Tony Jonesee959b02008-02-22 00:13:36 +01001565 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001566
1567 return sprintf(buf, "%d\n", target->srp_host->port);
1568}
1569
Tony Jonesee959b02008-02-22 00:13:36 +01001570static ssize_t show_local_ib_device(struct device *dev,
1571 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001572{
Tony Jonesee959b02008-02-22 00:13:36 +01001573 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001574
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001575 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001576}
1577
Tony Jonesee959b02008-02-22 00:13:36 +01001578static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1579static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1580static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1581static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1582static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1583static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
1584static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1585static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1586static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001587
Tony Jonesee959b02008-02-22 00:13:36 +01001588static struct device_attribute *srp_host_attrs[] = {
1589 &dev_attr_id_ext,
1590 &dev_attr_ioc_guid,
1591 &dev_attr_service_id,
1592 &dev_attr_pkey,
1593 &dev_attr_dgid,
1594 &dev_attr_orig_dgid,
1595 &dev_attr_zero_req_lim,
1596 &dev_attr_local_ib_port,
1597 &dev_attr_local_ib_device,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001598 NULL
1599};
1600
Roland Dreieraef9ec32005-11-02 14:07:13 -08001601static struct scsi_host_template srp_template = {
1602 .module = THIS_MODULE,
Roland Dreierb7f008f2007-05-06 21:18:11 -07001603 .name = "InfiniBand SRP initiator",
1604 .proc_name = DRV_NAME,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001605 .info = srp_target_info,
1606 .queuecommand = srp_queuecommand,
1607 .eh_abort_handler = srp_abort,
1608 .eh_device_reset_handler = srp_reset_device,
1609 .eh_host_reset_handler = srp_reset_host,
1610 .can_queue = SRP_SQ_SIZE,
1611 .this_id = -1,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001612 .cmd_per_lun = SRP_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001613 .use_clustering = ENABLE_CLUSTERING,
1614 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001615};
1616
1617static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1618{
FUJITA Tomonori32368222007-06-27 16:33:12 +09001619 struct srp_rport_identifiers ids;
1620 struct srp_rport *rport;
1621
Roland Dreieraef9ec32005-11-02 14:07:13 -08001622 sprintf(target->target_name, "SRP.T10:%016llX",
1623 (unsigned long long) be64_to_cpu(target->id_ext));
1624
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001625 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001626 return -ENODEV;
1627
FUJITA Tomonori32368222007-06-27 16:33:12 +09001628 memcpy(ids.port_id, &target->id_ext, 8);
1629 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09001630 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori32368222007-06-27 16:33:12 +09001631 rport = srp_rport_add(target->scsi_host, &ids);
1632 if (IS_ERR(rport)) {
1633 scsi_remove_host(target->scsi_host);
1634 return PTR_ERR(rport);
1635 }
1636
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001637 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001638 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001639 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001640
1641 target->state = SRP_TARGET_LIVE;
1642
Roland Dreieraef9ec32005-11-02 14:07:13 -08001643 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07001644 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001645
1646 return 0;
1647}
1648
Tony Jonesee959b02008-02-22 00:13:36 +01001649static void srp_release_dev(struct device *dev)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001650{
1651 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01001652 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001653
1654 complete(&host->released);
1655}
1656
1657static struct class srp_class = {
1658 .name = "infiniband_srp",
Tony Jonesee959b02008-02-22 00:13:36 +01001659 .dev_release = srp_release_dev
Roland Dreieraef9ec32005-11-02 14:07:13 -08001660};
1661
1662/*
1663 * Target ports are added by writing
1664 *
1665 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1666 * pkey=<P_Key>,service_id=<service ID>
1667 *
1668 * to the add_target sysfs attribute.
1669 */
1670enum {
1671 SRP_OPT_ERR = 0,
1672 SRP_OPT_ID_EXT = 1 << 0,
1673 SRP_OPT_IOC_GUID = 1 << 1,
1674 SRP_OPT_DGID = 1 << 2,
1675 SRP_OPT_PKEY = 1 << 3,
1676 SRP_OPT_SERVICE_ID = 1 << 4,
1677 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07001678 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07001679 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001680 SRP_OPT_INITIATOR_EXT = 1 << 8,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001681 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1682 SRP_OPT_IOC_GUID |
1683 SRP_OPT_DGID |
1684 SRP_OPT_PKEY |
1685 SRP_OPT_SERVICE_ID),
1686};
1687
1688static match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07001689 { SRP_OPT_ID_EXT, "id_ext=%s" },
1690 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1691 { SRP_OPT_DGID, "dgid=%s" },
1692 { SRP_OPT_PKEY, "pkey=%x" },
1693 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1694 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1695 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07001696 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001697 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
Vu Pham52fb2b502006-06-17 20:37:31 -07001698 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001699};
1700
1701static int srp_parse_options(const char *buf, struct srp_target_port *target)
1702{
1703 char *options, *sep_opt;
1704 char *p;
1705 char dgid[3];
1706 substring_t args[MAX_OPT_ARGS];
1707 int opt_mask = 0;
1708 int token;
1709 int ret = -EINVAL;
1710 int i;
1711
1712 options = kstrdup(buf, GFP_KERNEL);
1713 if (!options)
1714 return -ENOMEM;
1715
1716 sep_opt = options;
1717 while ((p = strsep(&sep_opt, ",")) != NULL) {
1718 if (!*p)
1719 continue;
1720
1721 token = match_token(p, srp_opt_tokens, args);
1722 opt_mask |= token;
1723
1724 switch (token) {
1725 case SRP_OPT_ID_EXT:
1726 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001727 if (!p) {
1728 ret = -ENOMEM;
1729 goto out;
1730 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001731 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1732 kfree(p);
1733 break;
1734
1735 case SRP_OPT_IOC_GUID:
1736 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001737 if (!p) {
1738 ret = -ENOMEM;
1739 goto out;
1740 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001741 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1742 kfree(p);
1743 break;
1744
1745 case SRP_OPT_DGID:
1746 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001747 if (!p) {
1748 ret = -ENOMEM;
1749 goto out;
1750 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001751 if (strlen(p) != 32) {
1752 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07001753 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001754 goto out;
1755 }
1756
1757 for (i = 0; i < 16; ++i) {
1758 strlcpy(dgid, p + i * 2, 3);
1759 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1760 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08001761 kfree(p);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001762 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001763 break;
1764
1765 case SRP_OPT_PKEY:
1766 if (match_hex(args, &token)) {
1767 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1768 goto out;
1769 }
1770 target->path.pkey = cpu_to_be16(token);
1771 break;
1772
1773 case SRP_OPT_SERVICE_ID:
1774 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001775 if (!p) {
1776 ret = -ENOMEM;
1777 goto out;
1778 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001779 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
Sean Hefty247e0202007-08-08 15:51:18 -07001780 target->path.service_id = target->service_id;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001781 kfree(p);
1782 break;
1783
1784 case SRP_OPT_MAX_SECT:
1785 if (match_int(args, &token)) {
1786 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1787 goto out;
1788 }
1789 target->scsi_host->max_sectors = token;
1790 break;
1791
Vu Pham52fb2b502006-06-17 20:37:31 -07001792 case SRP_OPT_MAX_CMD_PER_LUN:
1793 if (match_int(args, &token)) {
1794 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1795 goto out;
1796 }
1797 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1798 break;
1799
Ramachandra K0c0450db2006-06-17 20:37:38 -07001800 case SRP_OPT_IO_CLASS:
1801 if (match_hex(args, &token)) {
1802 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1803 goto out;
1804 }
1805 if (token != SRP_REV10_IB_IO_CLASS &&
1806 token != SRP_REV16A_IB_IO_CLASS) {
1807 printk(KERN_WARNING PFX "unknown IO class parameter value"
1808 " %x specified (use %x or %x).\n",
1809 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1810 goto out;
1811 }
1812 target->io_class = token;
1813 break;
1814
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001815 case SRP_OPT_INITIATOR_EXT:
1816 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001817 if (!p) {
1818 ret = -ENOMEM;
1819 goto out;
1820 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001821 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1822 kfree(p);
1823 break;
1824
Roland Dreieraef9ec32005-11-02 14:07:13 -08001825 default:
1826 printk(KERN_WARNING PFX "unknown parameter or missing value "
1827 "'%s' in target creation request\n", p);
1828 goto out;
1829 }
1830 }
1831
1832 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1833 ret = 0;
1834 else
1835 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1836 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1837 !(srp_opt_tokens[i].token & opt_mask))
1838 printk(KERN_WARNING PFX "target creation request is "
1839 "missing parameter '%s'\n",
1840 srp_opt_tokens[i].pattern);
1841
1842out:
1843 kfree(options);
1844 return ret;
1845}
1846
Tony Jonesee959b02008-02-22 00:13:36 +01001847static ssize_t srp_create_target(struct device *dev,
1848 struct device_attribute *attr,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001849 const char *buf, size_t count)
1850{
1851 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01001852 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001853 struct Scsi_Host *target_host;
1854 struct srp_target_port *target;
1855 int ret;
1856 int i;
1857
1858 target_host = scsi_host_alloc(&srp_template,
1859 sizeof (struct srp_target_port));
1860 if (!target_host)
1861 return -ENOMEM;
1862
FUJITA Tomonori32368222007-06-27 16:33:12 +09001863 target_host->transportt = ib_srp_transport_template;
Arne Redlich3c8edf02006-11-15 12:43:00 +01001864 target_host->max_lun = SRP_MAX_LUN;
1865 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08001866
Roland Dreieraef9ec32005-11-02 14:07:13 -08001867 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001868
Ramachandra K0c0450db2006-06-17 20:37:38 -07001869 target->io_class = SRP_REV16A_IB_IO_CLASS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001870 target->scsi_host = target_host;
1871 target->srp_host = host;
1872
Roland Dreierd945e1d2006-05-09 10:50:28 -07001873 INIT_LIST_HEAD(&target->free_reqs);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001874 INIT_LIST_HEAD(&target->req_queue);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001875 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1876 target->req_ring[i].index = i;
1877 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1878 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001879
1880 ret = srp_parse_options(buf, target);
1881 if (ret)
1882 goto err;
1883
Roland Dreier969a60f2008-07-14 23:48:43 -07001884 ib_query_gid(host->srp_dev->dev, host->port, 0, &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001885
David Dillow7aa54bd2008-01-07 18:23:41 -05001886 shost_printk(KERN_DEBUG, target->scsi_host, PFX
1887 "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1888 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
Roland Dreieraef9ec32005-11-02 14:07:13 -08001889 (unsigned long long) be64_to_cpu(target->id_ext),
1890 (unsigned long long) be64_to_cpu(target->ioc_guid),
1891 be16_to_cpu(target->path.pkey),
1892 (unsigned long long) be64_to_cpu(target->service_id),
1893 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]),
1894 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]),
1895 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]),
1896 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]),
1897 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]),
1898 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]),
1899 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]),
1900 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14]));
1901
1902 ret = srp_create_target_ib(target);
1903 if (ret)
1904 goto err;
1905
David Dillow9fe4bcf2008-01-08 17:08:52 -05001906 ret = srp_new_cm_id(target);
1907 if (ret)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001908 goto err_free;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001909
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001910 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001911 ret = srp_connect_target(target);
1912 if (ret) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001913 shost_printk(KERN_ERR, target->scsi_host,
1914 PFX "Connection failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001915 goto err_cm_id;
1916 }
1917
1918 ret = srp_add_target(host, target);
1919 if (ret)
1920 goto err_disconnect;
1921
1922 return count;
1923
1924err_disconnect:
1925 srp_disconnect_target(target);
1926
1927err_cm_id:
1928 ib_destroy_cm_id(target->cm_id);
1929
1930err_free:
1931 srp_free_target_ib(target);
1932
1933err:
1934 scsi_host_put(target_host);
1935
1936 return ret;
1937}
1938
Tony Jonesee959b02008-02-22 00:13:36 +01001939static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001940
Tony Jonesee959b02008-02-22 00:13:36 +01001941static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
1942 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001943{
Tony Jonesee959b02008-02-22 00:13:36 +01001944 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001945
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001946 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001947}
1948
Tony Jonesee959b02008-02-22 00:13:36 +01001949static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001950
Tony Jonesee959b02008-02-22 00:13:36 +01001951static ssize_t show_port(struct device *dev, struct device_attribute *attr,
1952 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001953{
Tony Jonesee959b02008-02-22 00:13:36 +01001954 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001955
1956 return sprintf(buf, "%d\n", host->port);
1957}
1958
Tony Jonesee959b02008-02-22 00:13:36 +01001959static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001960
Roland Dreierf5358a12006-06-17 20:37:29 -07001961static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001962{
1963 struct srp_host *host;
1964
1965 host = kzalloc(sizeof *host, GFP_KERNEL);
1966 if (!host)
1967 return NULL;
1968
1969 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001970 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001971 init_completion(&host->released);
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001972 host->srp_dev = device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001973 host->port = port;
1974
Tony Jonesee959b02008-02-22 00:13:36 +01001975 host->dev.class = &srp_class;
1976 host->dev.parent = device->dev->dma_device;
1977 snprintf(host->dev.bus_id, BUS_ID_SIZE, "srp-%s-%d",
Roland Dreierf5358a12006-06-17 20:37:29 -07001978 device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001979
Tony Jonesee959b02008-02-22 00:13:36 +01001980 if (device_register(&host->dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07001981 goto free_host;
Tony Jonesee959b02008-02-22 00:13:36 +01001982 if (device_create_file(&host->dev, &dev_attr_add_target))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001983 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01001984 if (device_create_file(&host->dev, &dev_attr_ibdev))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001985 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01001986 if (device_create_file(&host->dev, &dev_attr_port))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001987 goto err_class;
1988
1989 return host;
1990
1991err_class:
Tony Jonesee959b02008-02-22 00:13:36 +01001992 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001993
Roland Dreierf5358a12006-06-17 20:37:29 -07001994free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001995 kfree(host);
1996
1997 return NULL;
1998}
1999
2000static void srp_add_one(struct ib_device *device)
2001{
Roland Dreierf5358a12006-06-17 20:37:29 -07002002 struct srp_device *srp_dev;
2003 struct ib_device_attr *dev_attr;
2004 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002005 struct srp_host *host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002006 int s, e, p;
2007
Roland Dreierf5358a12006-06-17 20:37:29 -07002008 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
2009 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08002010 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002011
Roland Dreierf5358a12006-06-17 20:37:29 -07002012 if (ib_query_device(device, dev_attr)) {
2013 printk(KERN_WARNING PFX "Query device failed for %s\n",
2014 device->name);
2015 goto free_attr;
2016 }
2017
2018 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
2019 if (!srp_dev)
2020 goto free_attr;
2021
2022 /*
2023 * Use the smallest page size supported by the HCA, down to a
2024 * minimum of 512 bytes (which is the smallest sector that a
2025 * SCSI command will ever carry).
2026 */
2027 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
2028 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
Roland Dreierbf628dc2006-12-15 14:01:49 -08002029 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
Roland Dreierf5358a12006-06-17 20:37:29 -07002030
2031 INIT_LIST_HEAD(&srp_dev->dev_list);
2032
2033 srp_dev->dev = device;
2034 srp_dev->pd = ib_alloc_pd(device);
2035 if (IS_ERR(srp_dev->pd))
2036 goto free_dev;
2037
2038 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
2039 IB_ACCESS_LOCAL_WRITE |
2040 IB_ACCESS_REMOTE_READ |
2041 IB_ACCESS_REMOTE_WRITE);
2042 if (IS_ERR(srp_dev->mr))
2043 goto err_pd;
2044
2045 memset(&fmr_param, 0, sizeof fmr_param);
2046 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
2047 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
2048 fmr_param.cache = 1;
2049 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
2050 fmr_param.page_shift = srp_dev->fmr_page_shift;
2051 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
2052 IB_ACCESS_REMOTE_WRITE |
2053 IB_ACCESS_REMOTE_READ);
2054
2055 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
2056 if (IS_ERR(srp_dev->fmr_pool))
2057 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002058
Tom Tucker07ebafb2006-08-03 16:02:42 -05002059 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002060 s = 0;
2061 e = 0;
2062 } else {
2063 s = 1;
2064 e = device->phys_port_cnt;
2065 }
2066
2067 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07002068 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002069 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07002070 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002071 }
2072
Roland Dreierf5358a12006-06-17 20:37:29 -07002073 ib_set_client_data(device, &srp_client, srp_dev);
2074
2075 goto free_attr;
2076
2077err_pd:
2078 ib_dealloc_pd(srp_dev->pd);
2079
2080free_dev:
2081 kfree(srp_dev);
2082
2083free_attr:
2084 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002085}
2086
2087static void srp_remove_one(struct ib_device *device)
2088{
Roland Dreierf5358a12006-06-17 20:37:29 -07002089 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002090 struct srp_host *host, *tmp_host;
2091 LIST_HEAD(target_list);
2092 struct srp_target_port *target, *tmp_target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002093
Roland Dreierf5358a12006-06-17 20:37:29 -07002094 srp_dev = ib_get_client_data(device, &srp_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002095
Roland Dreierf5358a12006-06-17 20:37:29 -07002096 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Tony Jonesee959b02008-02-22 00:13:36 +01002097 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002098 /*
2099 * Wait for the sysfs entry to go away, so that no new
2100 * target ports can be created.
2101 */
2102 wait_for_completion(&host->released);
2103
2104 /*
2105 * Mark all target ports as removed, so we stop queueing
2106 * commands and don't try to reconnect.
2107 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002108 spin_lock(&host->target_lock);
Matthew Wilcox549c5fc22006-06-17 20:37:30 -07002109 list_for_each_entry(target, &host->target_list, list) {
Ishai Rabinovitz0c5b3952006-06-17 20:37:31 -07002110 spin_lock_irq(target->scsi_host->host_lock);
2111 target->state = SRP_TARGET_REMOVED;
2112 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002113 }
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002114 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002115
2116 /*
2117 * Wait for any reconnection tasks that may have
2118 * started before we marked our target ports as
2119 * removed, and any target port removal tasks.
2120 */
2121 flush_scheduled_work();
2122
2123 list_for_each_entry_safe(target, tmp_target,
2124 &host->target_list, list) {
David Dillowb0e47c82008-01-03 10:25:27 -08002125 srp_remove_host(target->scsi_host);
Dave Dillowad696982008-01-03 22:35:41 -05002126 scsi_remove_host(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002127 srp_disconnect_target(target);
2128 ib_destroy_cm_id(target->cm_id);
2129 srp_free_target_ib(target);
2130 scsi_host_put(target->scsi_host);
2131 }
2132
Roland Dreieraef9ec32005-11-02 14:07:13 -08002133 kfree(host);
2134 }
2135
Roland Dreierf5358a12006-06-17 20:37:29 -07002136 if (srp_dev->fmr_pool)
2137 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2138 ib_dereg_mr(srp_dev->mr);
2139 ib_dealloc_pd(srp_dev->pd);
2140
2141 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002142}
2143
FUJITA Tomonori32368222007-06-27 16:33:12 +09002144static struct srp_function_template ib_srp_transport_functions = {
2145};
2146
Roland Dreieraef9ec32005-11-02 14:07:13 -08002147static int __init srp_init_module(void)
2148{
2149 int ret;
2150
David Dillow1e89a192008-04-16 21:01:12 -07002151 if (srp_sg_tablesize > 255) {
2152 printk(KERN_WARNING PFX "Clamping srp_sg_tablesize to 255\n");
2153 srp_sg_tablesize = 255;
2154 }
2155
FUJITA Tomonori32368222007-06-27 16:33:12 +09002156 ib_srp_transport_template =
2157 srp_attach_transport(&ib_srp_transport_functions);
2158 if (!ib_srp_transport_template)
2159 return -ENOMEM;
2160
Vu Pham74b0a152006-06-17 20:37:32 -07002161 srp_template.sg_tablesize = srp_sg_tablesize;
2162 srp_max_iu_len = (sizeof (struct srp_cmd) +
2163 sizeof (struct srp_indirect_buf) +
2164 srp_sg_tablesize * 16);
2165
Roland Dreieraef9ec32005-11-02 14:07:13 -08002166 ret = class_register(&srp_class);
2167 if (ret) {
2168 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002169 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002170 return ret;
2171 }
2172
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002173 ib_sa_register_client(&srp_sa_client);
2174
Roland Dreieraef9ec32005-11-02 14:07:13 -08002175 ret = ib_register_client(&srp_client);
2176 if (ret) {
2177 printk(KERN_ERR PFX "couldn't register IB client\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002178 srp_release_transport(ib_srp_transport_template);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002179 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002180 class_unregister(&srp_class);
2181 return ret;
2182 }
2183
2184 return 0;
2185}
2186
2187static void __exit srp_cleanup_module(void)
2188{
2189 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002190 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002191 class_unregister(&srp_class);
FUJITA Tomonori32368222007-06-27 16:33:12 +09002192 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002193}
2194
2195module_init(srp_init_module);
2196module_exit(srp_cleanup_module);