blob: 5b8b533f29089a7ccd428abffa65136ae8cb30e0 [file] [log] [blame]
Roland Dreieraef9ec32005-11-02 14:07:13 -08001/*
2 * Copyright (c) 2005 Cisco Systems. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
Roland Dreieraef9ec32005-11-02 14:07:13 -080031 */
32
Roland Dreieraef9ec32005-11-02 14:07:13 -080033#include <linux/module.h>
34#include <linux/init.h>
35#include <linux/slab.h>
36#include <linux/err.h>
37#include <linux/string.h>
38#include <linux/parser.h>
39#include <linux/random.h>
Tim Schmielaude259682006-01-08 01:02:05 -080040#include <linux/jiffies.h>
Roland Dreieraef9ec32005-11-02 14:07:13 -080041
42#include <asm/atomic.h>
43
44#include <scsi/scsi.h>
45#include <scsi/scsi_device.h>
46#include <scsi/scsi_dbg.h>
47#include <scsi/srp.h>
FUJITA Tomonori32368222007-06-27 16:33:12 +090048#include <scsi/scsi_transport_srp.h>
Roland Dreieraef9ec32005-11-02 14:07:13 -080049
Roland Dreieraef9ec32005-11-02 14:07:13 -080050#include "ib_srp.h"
51
52#define DRV_NAME "ib_srp"
53#define PFX DRV_NAME ": "
54#define DRV_VERSION "0.2"
55#define DRV_RELDATE "November 1, 2005"
56
57MODULE_AUTHOR("Roland Dreier");
58MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
59 "v" DRV_VERSION " (" DRV_RELDATE ")");
60MODULE_LICENSE("Dual BSD/GPL");
61
Vu Pham74b0a152006-06-17 20:37:32 -070062static int srp_sg_tablesize = SRP_DEF_SG_TABLESIZE;
63static int srp_max_iu_len;
64
65module_param(srp_sg_tablesize, int, 0444);
66MODULE_PARM_DESC(srp_sg_tablesize,
David Dillow1e89a192008-04-16 21:01:12 -070067 "Max number of gather/scatter entries per I/O (default is 12, max 255)");
Vu Pham74b0a152006-06-17 20:37:32 -070068
Roland Dreieraef9ec32005-11-02 14:07:13 -080069static int topspin_workarounds = 1;
70
71module_param(topspin_workarounds, int, 0444);
72MODULE_PARM_DESC(topspin_workarounds,
73 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
74
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -070075static int mellanox_workarounds = 1;
76
77module_param(mellanox_workarounds, int, 0444);
78MODULE_PARM_DESC(mellanox_workarounds,
79 "Enable workarounds for Mellanox SRP target bugs if != 0");
80
Roland Dreieraef9ec32005-11-02 14:07:13 -080081static void srp_add_one(struct ib_device *device);
82static void srp_remove_one(struct ib_device *device);
83static void srp_completion(struct ib_cq *cq, void *target_ptr);
84static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
85
FUJITA Tomonori32368222007-06-27 16:33:12 +090086static struct scsi_transport_template *ib_srp_transport_template;
87
Roland Dreieraef9ec32005-11-02 14:07:13 -080088static struct ib_client srp_client = {
89 .name = "srp",
90 .add = srp_add_one,
91 .remove = srp_remove_one
92};
93
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -070094static struct ib_sa_client srp_sa_client;
95
Roland Dreieraef9ec32005-11-02 14:07:13 -080096static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
97{
98 return (struct srp_target_port *) host->hostdata;
99}
100
101static const char *srp_target_info(struct Scsi_Host *host)
102{
103 return host_to_target(host)->target_name;
104}
105
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700106static int srp_target_is_topspin(struct srp_target_port *target)
107{
108 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700109 static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700110
111 return topspin_workarounds &&
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700112 (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
113 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700114}
115
116static int srp_target_is_mellanox(struct srp_target_port *target)
117{
118 static const u8 mellanox_oui[3] = { 0x00, 0x02, 0xc9 };
119
120 return mellanox_workarounds &&
121 !memcmp(&target->ioc_guid, mellanox_oui, sizeof mellanox_oui);
122}
123
Roland Dreieraef9ec32005-11-02 14:07:13 -0800124static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
125 gfp_t gfp_mask,
126 enum dma_data_direction direction)
127{
128 struct srp_iu *iu;
129
130 iu = kmalloc(sizeof *iu, gfp_mask);
131 if (!iu)
132 goto out;
133
134 iu->buf = kzalloc(size, gfp_mask);
135 if (!iu->buf)
136 goto out_free_iu;
137
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100138 iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
139 direction);
140 if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800141 goto out_free_buf;
142
143 iu->size = size;
144 iu->direction = direction;
145
146 return iu;
147
148out_free_buf:
149 kfree(iu->buf);
150out_free_iu:
151 kfree(iu);
152out:
153 return NULL;
154}
155
156static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
157{
158 if (!iu)
159 return;
160
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100161 ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
162 iu->direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800163 kfree(iu->buf);
164 kfree(iu);
165}
166
167static void srp_qp_event(struct ib_event *event, void *context)
168{
169 printk(KERN_ERR PFX "QP event %d\n", event->event);
170}
171
172static int srp_init_qp(struct srp_target_port *target,
173 struct ib_qp *qp)
174{
175 struct ib_qp_attr *attr;
176 int ret;
177
178 attr = kmalloc(sizeof *attr, GFP_KERNEL);
179 if (!attr)
180 return -ENOMEM;
181
Roland Dreier969a60f2008-07-14 23:48:43 -0700182 ret = ib_find_pkey(target->srp_host->srp_dev->dev,
183 target->srp_host->port,
184 be16_to_cpu(target->path.pkey),
185 &attr->pkey_index);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800186 if (ret)
187 goto out;
188
189 attr->qp_state = IB_QPS_INIT;
190 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
191 IB_ACCESS_REMOTE_WRITE);
192 attr->port_num = target->srp_host->port;
193
194 ret = ib_modify_qp(qp, attr,
195 IB_QP_STATE |
196 IB_QP_PKEY_INDEX |
197 IB_QP_ACCESS_FLAGS |
198 IB_QP_PORT);
199
200out:
201 kfree(attr);
202 return ret;
203}
204
David Dillow9fe4bcf2008-01-08 17:08:52 -0500205static int srp_new_cm_id(struct srp_target_port *target)
206{
207 struct ib_cm_id *new_cm_id;
208
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100209 new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
David Dillow9fe4bcf2008-01-08 17:08:52 -0500210 srp_cm_handler, target);
211 if (IS_ERR(new_cm_id))
212 return PTR_ERR(new_cm_id);
213
214 if (target->cm_id)
215 ib_destroy_cm_id(target->cm_id);
216 target->cm_id = new_cm_id;
217
218 return 0;
219}
220
Roland Dreieraef9ec32005-11-02 14:07:13 -0800221static int srp_create_target_ib(struct srp_target_port *target)
222{
223 struct ib_qp_init_attr *init_attr;
224 int ret;
225
226 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
227 if (!init_attr)
228 return -ENOMEM;
229
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100230 target->cq = ib_create_cq(target->srp_host->srp_dev->dev,
231 srp_completion, NULL, target, SRP_CQ_SIZE, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800232 if (IS_ERR(target->cq)) {
233 ret = PTR_ERR(target->cq);
234 goto out;
235 }
236
237 ib_req_notify_cq(target->cq, IB_CQ_NEXT_COMP);
238
239 init_attr->event_handler = srp_qp_event;
240 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
241 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
242 init_attr->cap.max_recv_sge = 1;
243 init_attr->cap.max_send_sge = 1;
244 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
245 init_attr->qp_type = IB_QPT_RC;
246 init_attr->send_cq = target->cq;
247 init_attr->recv_cq = target->cq;
248
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100249 target->qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800250 if (IS_ERR(target->qp)) {
251 ret = PTR_ERR(target->qp);
252 ib_destroy_cq(target->cq);
253 goto out;
254 }
255
256 ret = srp_init_qp(target, target->qp);
257 if (ret) {
258 ib_destroy_qp(target->qp);
259 ib_destroy_cq(target->cq);
260 goto out;
261 }
262
263out:
264 kfree(init_attr);
265 return ret;
266}
267
268static void srp_free_target_ib(struct srp_target_port *target)
269{
270 int i;
271
272 ib_destroy_qp(target->qp);
273 ib_destroy_cq(target->cq);
274
275 for (i = 0; i < SRP_RQ_SIZE; ++i)
276 srp_free_iu(target->srp_host, target->rx_ring[i]);
277 for (i = 0; i < SRP_SQ_SIZE + 1; ++i)
278 srp_free_iu(target->srp_host, target->tx_ring[i]);
279}
280
281static void srp_path_rec_completion(int status,
282 struct ib_sa_path_rec *pathrec,
283 void *target_ptr)
284{
285 struct srp_target_port *target = target_ptr;
286
287 target->status = status;
288 if (status)
David Dillow7aa54bd2008-01-07 18:23:41 -0500289 shost_printk(KERN_ERR, target->scsi_host,
290 PFX "Got failed path rec status %d\n", status);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800291 else
292 target->path = *pathrec;
293 complete(&target->done);
294}
295
296static int srp_lookup_path(struct srp_target_port *target)
297{
298 target->path.numb_path = 1;
299
300 init_completion(&target->done);
301
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700302 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100303 target->srp_host->srp_dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800304 target->srp_host->port,
305 &target->path,
Sean Hefty247e0202007-08-08 15:51:18 -0700306 IB_SA_PATH_REC_SERVICE_ID |
Roland Dreieraef9ec32005-11-02 14:07:13 -0800307 IB_SA_PATH_REC_DGID |
308 IB_SA_PATH_REC_SGID |
309 IB_SA_PATH_REC_NUMB_PATH |
310 IB_SA_PATH_REC_PKEY,
311 SRP_PATH_REC_TIMEOUT_MS,
312 GFP_KERNEL,
313 srp_path_rec_completion,
314 target, &target->path_query);
315 if (target->path_query_id < 0)
316 return target->path_query_id;
317
318 wait_for_completion(&target->done);
319
320 if (target->status < 0)
David Dillow7aa54bd2008-01-07 18:23:41 -0500321 shost_printk(KERN_WARNING, target->scsi_host,
322 PFX "Path record query failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800323
324 return target->status;
325}
326
327static int srp_send_req(struct srp_target_port *target)
328{
329 struct {
330 struct ib_cm_req_param param;
331 struct srp_login_req priv;
332 } *req = NULL;
333 int status;
334
335 req = kzalloc(sizeof *req, GFP_KERNEL);
336 if (!req)
337 return -ENOMEM;
338
339 req->param.primary_path = &target->path;
340 req->param.alternate_path = NULL;
341 req->param.service_id = target->service_id;
342 req->param.qp_num = target->qp->qp_num;
343 req->param.qp_type = target->qp->qp_type;
344 req->param.private_data = &req->priv;
345 req->param.private_data_len = sizeof req->priv;
346 req->param.flow_control = 1;
347
348 get_random_bytes(&req->param.starting_psn, 4);
349 req->param.starting_psn &= 0xffffff;
350
351 /*
352 * Pick some arbitrary defaults here; we could make these
353 * module parameters if anyone cared about setting them.
354 */
355 req->param.responder_resources = 4;
356 req->param.remote_cm_response_timeout = 20;
357 req->param.local_cm_response_timeout = 20;
358 req->param.retry_count = 7;
359 req->param.rnr_retry_count = 7;
360 req->param.max_cm_retries = 15;
361
362 req->priv.opcode = SRP_LOGIN_REQ;
363 req->priv.tag = 0;
Vu Pham74b0a152006-06-17 20:37:32 -0700364 req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800365 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
366 SRP_BUF_FORMAT_INDIRECT);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700367 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700368 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450db2006-06-17 20:37:38 -0700369 * port identifier format is 8 bytes of ID extension followed
370 * by 8 bytes of GUID. Older drafts put the two halves in the
371 * opposite order, so that the GUID comes first.
372 *
373 * Targets conforming to these obsolete drafts can be
374 * recognized by the I/O Class they report.
375 */
376 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
377 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200378 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700379 memcpy(req->priv.initiator_port_id + 8,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200380 &target->initiator_ext, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700381 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
382 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
383 } else {
384 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200385 &target->initiator_ext, 8);
386 memcpy(req->priv.initiator_port_id + 8,
387 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700388 memcpy(req->priv.target_port_id, &target->id_ext, 8);
389 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
390 }
391
Roland Dreieraef9ec32005-11-02 14:07:13 -0800392 /*
393 * Topspin/Cisco SRP targets will reject our login unless we
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200394 * zero out the first 8 bytes of our initiator port ID and set
395 * the second 8 bytes to the local node GUID.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800396 */
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700397 if (srp_target_is_topspin(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500398 shost_printk(KERN_DEBUG, target->scsi_host,
399 PFX "Topspin/Cisco initiator port ID workaround "
400 "activated for target GUID %016llx\n",
401 (unsigned long long) be64_to_cpu(target->ioc_guid));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800402 memset(req->priv.initiator_port_id, 0, 8);
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200403 memcpy(req->priv.initiator_port_id + 8,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100404 &target->srp_host->srp_dev->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800405 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800406
407 status = ib_send_cm_req(target->cm_id, &req->param);
408
409 kfree(req);
410
411 return status;
412}
413
414static void srp_disconnect_target(struct srp_target_port *target)
415{
416 /* XXX should send SRP_I_LOGOUT request */
417
418 init_completion(&target->done);
Roland Dreiere6581052006-05-17 09:13:21 -0700419 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500420 shost_printk(KERN_DEBUG, target->scsi_host,
421 PFX "Sending CM DREQ failed\n");
Roland Dreiere6581052006-05-17 09:13:21 -0700422 return;
423 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800424 wait_for_completion(&target->done);
425}
426
David Howellsc4028952006-11-22 14:57:56 +0000427static void srp_remove_work(struct work_struct *work)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800428{
David Howellsc4028952006-11-22 14:57:56 +0000429 struct srp_target_port *target =
430 container_of(work, struct srp_target_port, work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800431
432 spin_lock_irq(target->scsi_host->host_lock);
433 if (target->state != SRP_TARGET_DEAD) {
434 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800435 return;
436 }
437 target->state = SRP_TARGET_REMOVED;
438 spin_unlock_irq(target->scsi_host->host_lock);
439
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700440 spin_lock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800441 list_del(&target->list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700442 spin_unlock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800443
FUJITA Tomonori32368222007-06-27 16:33:12 +0900444 srp_remove_host(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800445 scsi_remove_host(target->scsi_host);
446 ib_destroy_cm_id(target->cm_id);
447 srp_free_target_ib(target);
448 scsi_host_put(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800449}
450
451static int srp_connect_target(struct srp_target_port *target)
452{
David Dillow9fe4bcf2008-01-08 17:08:52 -0500453 int retries = 3;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800454 int ret;
455
456 ret = srp_lookup_path(target);
457 if (ret)
458 return ret;
459
460 while (1) {
461 init_completion(&target->done);
462 ret = srp_send_req(target);
463 if (ret)
464 return ret;
465 wait_for_completion(&target->done);
466
467 /*
468 * The CM event handling code will set status to
469 * SRP_PORT_REDIRECT if we get a port redirect REJ
470 * back, or SRP_DLID_REDIRECT if we get a lid/qp
471 * redirect REJ back.
472 */
473 switch (target->status) {
474 case 0:
475 return 0;
476
477 case SRP_PORT_REDIRECT:
478 ret = srp_lookup_path(target);
479 if (ret)
480 return ret;
481 break;
482
483 case SRP_DLID_REDIRECT:
484 break;
485
David Dillow9fe4bcf2008-01-08 17:08:52 -0500486 case SRP_STALE_CONN:
487 /* Our current CM id was stale, and is now in timewait.
488 * Try to reconnect with a new one.
489 */
490 if (!retries-- || srp_new_cm_id(target)) {
491 shost_printk(KERN_ERR, target->scsi_host, PFX
492 "giving up on stale connection\n");
493 target->status = -ECONNRESET;
494 return target->status;
495 }
496
497 shost_printk(KERN_ERR, target->scsi_host, PFX
498 "retrying stale connection\n");
499 break;
500
Roland Dreieraef9ec32005-11-02 14:07:13 -0800501 default:
502 return target->status;
503 }
504 }
505}
506
Roland Dreierd945e1d2006-05-09 10:50:28 -0700507static void srp_unmap_data(struct scsi_cmnd *scmnd,
508 struct srp_target_port *target,
509 struct srp_request *req)
510{
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900511 if (!scsi_sglist(scmnd) ||
Roland Dreierd945e1d2006-05-09 10:50:28 -0700512 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
513 scmnd->sc_data_direction != DMA_FROM_DEVICE))
514 return;
515
Roland Dreierf5358a12006-06-17 20:37:29 -0700516 if (req->fmr) {
517 ib_fmr_pool_unmap(req->fmr);
518 req->fmr = NULL;
519 }
520
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100521 ib_dma_unmap_sg(target->srp_host->srp_dev->dev, scsi_sglist(scmnd),
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900522 scsi_sg_count(scmnd), scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700523}
524
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700525static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
526{
527 srp_unmap_data(req->scmnd, target, req);
528 list_move_tail(&req->list, &target->free_reqs);
529}
530
531static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
532{
533 req->scmnd->result = DID_RESET << 16;
534 req->scmnd->scsi_done(req->scmnd);
535 srp_remove_req(target, req);
536}
537
Roland Dreieraef9ec32005-11-02 14:07:13 -0800538static int srp_reconnect_target(struct srp_target_port *target)
539{
Roland Dreieraef9ec32005-11-02 14:07:13 -0800540 struct ib_qp_attr qp_attr;
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700541 struct srp_request *req, *tmp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800542 struct ib_wc wc;
543 int ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800544
545 spin_lock_irq(target->scsi_host->host_lock);
546 if (target->state != SRP_TARGET_LIVE) {
547 spin_unlock_irq(target->scsi_host->host_lock);
548 return -EAGAIN;
549 }
550 target->state = SRP_TARGET_CONNECTING;
551 spin_unlock_irq(target->scsi_host->host_lock);
552
553 srp_disconnect_target(target);
554 /*
555 * Now get a new local CM ID so that we avoid confusing the
556 * target in case things are really fouled up.
557 */
David Dillow9fe4bcf2008-01-08 17:08:52 -0500558 ret = srp_new_cm_id(target);
559 if (ret)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800560 goto err;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800561
562 qp_attr.qp_state = IB_QPS_RESET;
563 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
564 if (ret)
565 goto err;
566
567 ret = srp_init_qp(target, target->qp);
568 if (ret)
569 goto err;
570
571 while (ib_poll_cq(target->cq, 1, &wc) > 0)
572 ; /* nothing */
573
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300574 spin_lock_irq(target->scsi_host->host_lock);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700575 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
576 srp_reset_req(target, req);
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300577 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800578
579 target->rx_head = 0;
580 target->tx_head = 0;
581 target->tx_tail = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800582
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200583 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800584 ret = srp_connect_target(target);
585 if (ret)
586 goto err;
587
588 spin_lock_irq(target->scsi_host->host_lock);
589 if (target->state == SRP_TARGET_CONNECTING) {
590 ret = 0;
591 target->state = SRP_TARGET_LIVE;
592 } else
593 ret = -EAGAIN;
594 spin_unlock_irq(target->scsi_host->host_lock);
595
596 return ret;
597
598err:
David Dillow7aa54bd2008-01-07 18:23:41 -0500599 shost_printk(KERN_ERR, target->scsi_host,
600 PFX "reconnect failed (%d), removing target port.\n", ret);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800601
602 /*
603 * We couldn't reconnect, so kill our target port off.
604 * However, we have to defer the real removal because we might
605 * be in the context of the SCSI error handler now, which
606 * would deadlock if we call scsi_remove_host().
607 */
608 spin_lock_irq(target->scsi_host->host_lock);
609 if (target->state == SRP_TARGET_CONNECTING) {
610 target->state = SRP_TARGET_DEAD;
David Howellsc4028952006-11-22 14:57:56 +0000611 INIT_WORK(&target->work, srp_remove_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800612 schedule_work(&target->work);
613 }
614 spin_unlock_irq(target->scsi_host->host_lock);
615
616 return ret;
617}
618
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700619static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat,
Roland Dreierf5358a12006-06-17 20:37:29 -0700620 int sg_cnt, struct srp_request *req,
621 struct srp_direct_buf *buf)
622{
623 u64 io_addr = 0;
624 u64 *dma_pages;
625 u32 len;
626 int page_cnt;
627 int i, j;
628 int ret;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100629 struct srp_device *dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800630 struct ib_device *ibdev = dev->dev;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900631 struct scatterlist *sg;
Roland Dreierf5358a12006-06-17 20:37:29 -0700632
633 if (!dev->fmr_pool)
634 return -ENODEV;
635
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700636 if (srp_target_is_mellanox(target) &&
637 (ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask))
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700638 return -EINVAL;
639
Roland Dreierf5358a12006-06-17 20:37:29 -0700640 len = page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900641 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
642 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800643
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900644 if (ib_sg_dma_address(ibdev, sg) & ~dev->fmr_page_mask) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700645 if (i > 0)
646 return -EINVAL;
647 else
648 ++page_cnt;
649 }
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900650 if ((ib_sg_dma_address(ibdev, sg) + dma_len) &
Roland Dreierf5358a12006-06-17 20:37:29 -0700651 ~dev->fmr_page_mask) {
652 if (i < sg_cnt - 1)
653 return -EINVAL;
654 else
655 ++page_cnt;
656 }
657
Ralph Campbell85507bc2006-12-12 14:30:55 -0800658 len += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700659 }
660
661 page_cnt += len >> dev->fmr_page_shift;
662 if (page_cnt > SRP_FMR_SIZE)
663 return -ENOMEM;
664
665 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
666 if (!dma_pages)
667 return -ENOMEM;
668
669 page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900670 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
671 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800672
673 for (j = 0; j < dma_len; j += dev->fmr_page_size)
Roland Dreierf5358a12006-06-17 20:37:29 -0700674 dma_pages[page_cnt++] =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900675 (ib_sg_dma_address(ibdev, sg) &
Ralph Campbell85507bc2006-12-12 14:30:55 -0800676 dev->fmr_page_mask) + j;
677 }
Roland Dreierf5358a12006-06-17 20:37:29 -0700678
679 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
Michael S. Tsirkinadfaa882006-07-14 00:23:55 -0700680 dma_pages, page_cnt, io_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -0700681 if (IS_ERR(req->fmr)) {
682 ret = PTR_ERR(req->fmr);
Vu Pham6583eb32006-07-14 00:23:53 -0700683 req->fmr = NULL;
Roland Dreierf5358a12006-06-17 20:37:29 -0700684 goto out;
685 }
686
Ralph Campbell85507bc2006-12-12 14:30:55 -0800687 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) &
688 ~dev->fmr_page_mask);
Roland Dreierf5358a12006-06-17 20:37:29 -0700689 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
690 buf->len = cpu_to_be32(len);
691
692 ret = 0;
693
694out:
695 kfree(dma_pages);
696
697 return ret;
698}
699
Roland Dreieraef9ec32005-11-02 14:07:13 -0800700static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
701 struct srp_request *req)
702{
Roland Dreiercf368712006-03-24 15:47:26 -0800703 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800704 struct srp_cmd *cmd = req->cmd->buf;
Roland Dreiercf368712006-03-24 15:47:26 -0800705 int len, nents, count;
Roland Dreierf5358a12006-06-17 20:37:29 -0700706 u8 fmt = SRP_DATA_DESC_DIRECT;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800707 struct srp_device *dev;
708 struct ib_device *ibdev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800709
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900710 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800711 return sizeof (struct srp_cmd);
712
713 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
714 scmnd->sc_data_direction != DMA_TO_DEVICE) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500715 shost_printk(KERN_WARNING, target->scsi_host,
716 PFX "Unhandled data direction %d\n",
717 scmnd->sc_data_direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800718 return -EINVAL;
719 }
720
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900721 nents = scsi_sg_count(scmnd);
722 scat = scsi_sglist(scmnd);
Roland Dreiercf368712006-03-24 15:47:26 -0800723
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100724 dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800725 ibdev = dev->dev;
726
727 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
Roland Dreierf5358a12006-06-17 20:37:29 -0700728
729 fmt = SRP_DATA_DESC_DIRECT;
730 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800731
732 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700733 /*
734 * The midlayer only generated a single gather/scatter
735 * entry, or DMA mapping coalesced everything to a
736 * single entry. So a direct descriptor along with
737 * the DMA MR suffices.
738 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800739 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800740
Ralph Campbell85507bc2006-12-12 14:30:55 -0800741 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
742 buf->key = cpu_to_be32(dev->mr->rkey);
743 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700744 } else if (srp_map_fmr(target, scat, count, req,
Roland Dreierf5358a12006-06-17 20:37:29 -0700745 (void *) cmd->add_data)) {
746 /*
747 * FMR mapping failed, and the scatterlist has more
748 * than one entry. Generate an indirect memory
749 * descriptor.
750 */
Roland Dreiercf368712006-03-24 15:47:26 -0800751 struct srp_indirect_buf *buf = (void *) cmd->add_data;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900752 struct scatterlist *sg;
Roland Dreiercf368712006-03-24 15:47:26 -0800753 u32 datalen = 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700754 int i;
Roland Dreiercf368712006-03-24 15:47:26 -0800755
756 fmt = SRP_DATA_DESC_INDIRECT;
Roland Dreierf5358a12006-06-17 20:37:29 -0700757 len = sizeof (struct srp_cmd) +
758 sizeof (struct srp_indirect_buf) +
759 count * sizeof (struct srp_direct_buf);
760
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900761 scsi_for_each_sg(scmnd, sg, count, i) {
762 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800763
Roland Dreierf5358a12006-06-17 20:37:29 -0700764 buf->desc_list[i].va =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900765 cpu_to_be64(ib_sg_dma_address(ibdev, sg));
Roland Dreierf5358a12006-06-17 20:37:29 -0700766 buf->desc_list[i].key =
Ralph Campbell85507bc2006-12-12 14:30:55 -0800767 cpu_to_be32(dev->mr->rkey);
768 buf->desc_list[i].len = cpu_to_be32(dma_len);
769 datalen += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700770 }
Roland Dreiercf368712006-03-24 15:47:26 -0800771
772 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
773 cmd->data_out_desc_cnt = count;
774 else
775 cmd->data_in_desc_cnt = count;
776
Roland Dreierf5358a12006-06-17 20:37:29 -0700777 buf->table_desc.va =
778 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800779 buf->table_desc.key =
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100780 cpu_to_be32(target->srp_host->srp_dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800781 buf->table_desc.len =
782 cpu_to_be32(count * sizeof (struct srp_direct_buf));
783
Roland Dreiercf368712006-03-24 15:47:26 -0800784 buf->len = cpu_to_be32(datalen);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800785 }
786
787 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
788 cmd->buf_fmt = fmt << 4;
789 else
790 cmd->buf_fmt = fmt;
791
Roland Dreieraef9ec32005-11-02 14:07:13 -0800792 return len;
793}
794
Roland Dreieraef9ec32005-11-02 14:07:13 -0800795static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
796{
797 struct srp_request *req;
798 struct scsi_cmnd *scmnd;
799 unsigned long flags;
800 s32 delta;
801
802 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
803
804 spin_lock_irqsave(target->scsi_host->host_lock, flags);
805
806 target->req_lim += delta;
807
808 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
809
810 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
811 if (be32_to_cpu(rsp->resp_data_len) < 4)
812 req->tsk_status = -1;
813 else
814 req->tsk_status = rsp->data[3];
815 complete(&req->done);
816 } else {
Roland Dreierd945e1d2006-05-09 10:50:28 -0700817 scmnd = req->scmnd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800818 if (!scmnd)
David Dillow7aa54bd2008-01-07 18:23:41 -0500819 shost_printk(KERN_ERR, target->scsi_host,
820 "Null scmnd for RSP w/tag %016llx\n",
821 (unsigned long long) rsp->tag);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800822 scmnd->result = rsp->status;
823
824 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
825 memcpy(scmnd->sense_buffer, rsp->data +
826 be32_to_cpu(rsp->resp_data_len),
827 min_t(int, be32_to_cpu(rsp->sense_data_len),
828 SCSI_SENSE_BUFFERSIZE));
829 }
830
831 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900832 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800833 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900834 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800835
Roland Dreieraef9ec32005-11-02 14:07:13 -0800836 if (!req->tsk_mgmt) {
Roland Dreieraef9ec32005-11-02 14:07:13 -0800837 scmnd->host_scribble = (void *) -1L;
838 scmnd->scsi_done(scmnd);
839
Roland Dreierd945e1d2006-05-09 10:50:28 -0700840 srp_remove_req(target, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800841 } else
842 req->cmd_done = 1;
843 }
844
845 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
846}
847
Roland Dreieraef9ec32005-11-02 14:07:13 -0800848static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
849{
Ralph Campbell85507bc2006-12-12 14:30:55 -0800850 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800851 struct srp_iu *iu;
852 u8 opcode;
853
854 iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
855
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100856 dev = target->srp_host->srp_dev->dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800857 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
858 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800859
860 opcode = *(u8 *) iu->buf;
861
862 if (0) {
863 int i;
864
David Dillow7aa54bd2008-01-07 18:23:41 -0500865 shost_printk(KERN_ERR, target->scsi_host,
866 PFX "recv completion, opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800867
868 for (i = 0; i < wc->byte_len; ++i) {
869 if (i % 8 == 0)
870 printk(KERN_ERR " [%02x] ", i);
871 printk(" %02x", ((u8 *) iu->buf)[i]);
872 if ((i + 1) % 8 == 0)
873 printk("\n");
874 }
875
876 if (wc->byte_len % 8)
877 printk("\n");
878 }
879
880 switch (opcode) {
881 case SRP_RSP:
882 srp_process_rsp(target, iu->buf);
883 break;
884
885 case SRP_T_LOGOUT:
886 /* XXX Handle target logout */
David Dillow7aa54bd2008-01-07 18:23:41 -0500887 shost_printk(KERN_WARNING, target->scsi_host,
888 PFX "Got target logout request\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800889 break;
890
891 default:
David Dillow7aa54bd2008-01-07 18:23:41 -0500892 shost_printk(KERN_WARNING, target->scsi_host,
893 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800894 break;
895 }
896
Ralph Campbell85507bc2006-12-12 14:30:55 -0800897 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
898 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800899}
900
901static void srp_completion(struct ib_cq *cq, void *target_ptr)
902{
903 struct srp_target_port *target = target_ptr;
904 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800905
906 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
907 while (ib_poll_cq(cq, 1, &wc) > 0) {
908 if (wc.status) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500909 shost_printk(KERN_ERR, target->scsi_host,
910 PFX "failed %s status %d\n",
911 wc.wr_id & SRP_OP_RECV ? "receive" : "send",
912 wc.status);
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200913 target->qp_in_error = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800914 break;
915 }
916
917 if (wc.wr_id & SRP_OP_RECV)
918 srp_handle_recv(target, &wc);
919 else
920 ++target->tx_tail;
921 }
922}
923
924static int __srp_post_recv(struct srp_target_port *target)
925{
926 struct srp_iu *iu;
927 struct ib_sge list;
928 struct ib_recv_wr wr, *bad_wr;
929 unsigned int next;
930 int ret;
931
932 next = target->rx_head & (SRP_RQ_SIZE - 1);
933 wr.wr_id = next | SRP_OP_RECV;
934 iu = target->rx_ring[next];
935
936 list.addr = iu->dma;
937 list.length = iu->size;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100938 list.lkey = target->srp_host->srp_dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800939
940 wr.next = NULL;
941 wr.sg_list = &list;
942 wr.num_sge = 1;
943
944 ret = ib_post_recv(target->qp, &wr, &bad_wr);
945 if (!ret)
946 ++target->rx_head;
947
948 return ret;
949}
950
951static int srp_post_recv(struct srp_target_port *target)
952{
953 unsigned long flags;
954 int ret;
955
956 spin_lock_irqsave(target->scsi_host->host_lock, flags);
957 ret = __srp_post_recv(target);
958 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
959
960 return ret;
961}
962
963/*
964 * Must be called with target->scsi_host->host_lock held to protect
Roland Dreier47f2bce2005-11-15 00:19:21 -0800965 * req_lim and tx_head. Lock cannot be dropped between call here and
966 * call to __srp_post_send().
Roland Dreieraef9ec32005-11-02 14:07:13 -0800967 */
David Dillow8cba2072007-12-19 17:08:43 -0500968static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
969 enum srp_request_type req_type)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800970{
David Dillow8cba2072007-12-19 17:08:43 -0500971 s32 min = (req_type == SRP_REQ_TASK_MGMT) ? 1 : 2;
972
Roland Dreieraef9ec32005-11-02 14:07:13 -0800973 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
974 return NULL;
975
David Dillow8cba2072007-12-19 17:08:43 -0500976 if (target->req_lim < min) {
Roland Dreier6bfa24f2006-06-17 20:37:33 -0700977 ++target->zero_req_lim;
David Dillow8cba2072007-12-19 17:08:43 -0500978 return NULL;
979 }
Roland Dreier47f2bce2005-11-15 00:19:21 -0800980
Roland Dreieraef9ec32005-11-02 14:07:13 -0800981 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
982}
983
984/*
985 * Must be called with target->scsi_host->host_lock held to protect
986 * req_lim and tx_head.
987 */
988static int __srp_post_send(struct srp_target_port *target,
989 struct srp_iu *iu, int len)
990{
991 struct ib_sge list;
992 struct ib_send_wr wr, *bad_wr;
993 int ret = 0;
994
Roland Dreieraef9ec32005-11-02 14:07:13 -0800995 list.addr = iu->dma;
996 list.length = len;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100997 list.lkey = target->srp_host->srp_dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800998
999 wr.next = NULL;
1000 wr.wr_id = target->tx_head & SRP_SQ_SIZE;
1001 wr.sg_list = &list;
1002 wr.num_sge = 1;
1003 wr.opcode = IB_WR_SEND;
1004 wr.send_flags = IB_SEND_SIGNALED;
1005
1006 ret = ib_post_send(target->qp, &wr, &bad_wr);
1007
1008 if (!ret) {
1009 ++target->tx_head;
1010 --target->req_lim;
1011 }
1012
1013 return ret;
1014}
1015
1016static int srp_queuecommand(struct scsi_cmnd *scmnd,
1017 void (*done)(struct scsi_cmnd *))
1018{
1019 struct srp_target_port *target = host_to_target(scmnd->device->host);
1020 struct srp_request *req;
1021 struct srp_iu *iu;
1022 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001023 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001024 int len;
1025
1026 if (target->state == SRP_TARGET_CONNECTING)
1027 goto err;
1028
1029 if (target->state == SRP_TARGET_DEAD ||
1030 target->state == SRP_TARGET_REMOVED) {
1031 scmnd->result = DID_BAD_TARGET << 16;
1032 done(scmnd);
1033 return 0;
1034 }
1035
David Dillow8cba2072007-12-19 17:08:43 -05001036 iu = __srp_get_tx_iu(target, SRP_REQ_NORMAL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001037 if (!iu)
1038 goto err;
1039
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001040 dev = target->srp_host->srp_dev->dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001041 ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len,
1042 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001043
Roland Dreierd945e1d2006-05-09 10:50:28 -07001044 req = list_entry(target->free_reqs.next, struct srp_request, list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001045
1046 scmnd->scsi_done = done;
1047 scmnd->result = 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001048 scmnd->host_scribble = (void *) (long) req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001049
1050 cmd = iu->buf;
1051 memset(cmd, 0, sizeof *cmd);
1052
1053 cmd->opcode = SRP_CMD;
1054 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001055 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001056 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1057
Roland Dreieraef9ec32005-11-02 14:07:13 -08001058 req->scmnd = scmnd;
1059 req->cmd = iu;
1060 req->cmd_done = 0;
1061 req->tsk_mgmt = NULL;
1062
1063 len = srp_map_data(scmnd, target, req);
1064 if (len < 0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001065 shost_printk(KERN_ERR, target->scsi_host,
1066 PFX "Failed to map data\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001067 goto err;
1068 }
1069
1070 if (__srp_post_recv(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001071 shost_printk(KERN_ERR, target->scsi_host, PFX "Recv failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001072 goto err_unmap;
1073 }
1074
Ralph Campbell85507bc2006-12-12 14:30:55 -08001075 ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len,
1076 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001077
1078 if (__srp_post_send(target, iu, len)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001079 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001080 goto err_unmap;
1081 }
1082
Roland Dreierd945e1d2006-05-09 10:50:28 -07001083 list_move_tail(&req->list, &target->req_queue);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001084
1085 return 0;
1086
1087err_unmap:
1088 srp_unmap_data(scmnd, target, req);
1089
1090err:
1091 return SCSI_MLQUEUE_HOST_BUSY;
1092}
1093
1094static int srp_alloc_iu_bufs(struct srp_target_port *target)
1095{
1096 int i;
1097
1098 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1099 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1100 target->max_ti_iu_len,
1101 GFP_KERNEL, DMA_FROM_DEVICE);
1102 if (!target->rx_ring[i])
1103 goto err;
1104 }
1105
1106 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1107 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
Vu Pham74b0a152006-06-17 20:37:32 -07001108 srp_max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001109 GFP_KERNEL, DMA_TO_DEVICE);
1110 if (!target->tx_ring[i])
1111 goto err;
1112 }
1113
1114 return 0;
1115
1116err:
1117 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1118 srp_free_iu(target->srp_host, target->rx_ring[i]);
1119 target->rx_ring[i] = NULL;
1120 }
1121
1122 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1123 srp_free_iu(target->srp_host, target->tx_ring[i]);
1124 target->tx_ring[i] = NULL;
1125 }
1126
1127 return -ENOMEM;
1128}
1129
1130static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1131 struct ib_cm_event *event,
1132 struct srp_target_port *target)
1133{
David Dillow7aa54bd2008-01-07 18:23:41 -05001134 struct Scsi_Host *shost = target->scsi_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001135 struct ib_class_port_info *cpi;
1136 int opcode;
1137
1138 switch (event->param.rej_rcvd.reason) {
1139 case IB_CM_REJ_PORT_CM_REDIRECT:
1140 cpi = event->param.rej_rcvd.ari;
1141 target->path.dlid = cpi->redirect_lid;
1142 target->path.pkey = cpi->redirect_pkey;
1143 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1144 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1145
1146 target->status = target->path.dlid ?
1147 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1148 break;
1149
1150 case IB_CM_REJ_PORT_REDIRECT:
Roland Dreier5d7cbfd2007-08-03 10:45:18 -07001151 if (srp_target_is_topspin(target)) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001152 /*
1153 * Topspin/Cisco SRP gateways incorrectly send
1154 * reject reason code 25 when they mean 24
1155 * (port redirect).
1156 */
1157 memcpy(target->path.dgid.raw,
1158 event->param.rej_rcvd.ari, 16);
1159
David Dillow7aa54bd2008-01-07 18:23:41 -05001160 shost_printk(KERN_DEBUG, shost,
1161 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1162 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1163 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001164
1165 target->status = SRP_PORT_REDIRECT;
1166 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001167 shost_printk(KERN_WARNING, shost,
1168 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001169 target->status = -ECONNRESET;
1170 }
1171 break;
1172
1173 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
David Dillow7aa54bd2008-01-07 18:23:41 -05001174 shost_printk(KERN_WARNING, shost,
1175 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001176 target->status = -ECONNRESET;
1177 break;
1178
1179 case IB_CM_REJ_CONSUMER_DEFINED:
1180 opcode = *(u8 *) event->private_data;
1181 if (opcode == SRP_LOGIN_REJ) {
1182 struct srp_login_rej *rej = event->private_data;
1183 u32 reason = be32_to_cpu(rej->reason);
1184
1185 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
David Dillow7aa54bd2008-01-07 18:23:41 -05001186 shost_printk(KERN_WARNING, shost,
1187 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001188 else
David Dillow7aa54bd2008-01-07 18:23:41 -05001189 shost_printk(KERN_WARNING, shost,
1190 PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001191 } else
David Dillow7aa54bd2008-01-07 18:23:41 -05001192 shost_printk(KERN_WARNING, shost,
1193 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1194 " opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001195 target->status = -ECONNRESET;
1196 break;
1197
David Dillow9fe4bcf2008-01-08 17:08:52 -05001198 case IB_CM_REJ_STALE_CONN:
1199 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
1200 target->status = SRP_STALE_CONN;
1201 break;
1202
Roland Dreieraef9ec32005-11-02 14:07:13 -08001203 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001204 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
1205 event->param.rej_rcvd.reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001206 target->status = -ECONNRESET;
1207 }
1208}
1209
1210static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1211{
1212 struct srp_target_port *target = cm_id->context;
1213 struct ib_qp_attr *qp_attr = NULL;
1214 int attr_mask = 0;
1215 int comp = 0;
1216 int opcode = 0;
1217
1218 switch (event->event) {
1219 case IB_CM_REQ_ERROR:
David Dillow7aa54bd2008-01-07 18:23:41 -05001220 shost_printk(KERN_DEBUG, target->scsi_host,
1221 PFX "Sending CM REQ failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001222 comp = 1;
1223 target->status = -ECONNRESET;
1224 break;
1225
1226 case IB_CM_REP_RECEIVED:
1227 comp = 1;
1228 opcode = *(u8 *) event->private_data;
1229
1230 if (opcode == SRP_LOGIN_RSP) {
1231 struct srp_login_rsp *rsp = event->private_data;
1232
1233 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1234 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1235
1236 target->scsi_host->can_queue = min(target->req_lim,
1237 target->scsi_host->can_queue);
1238 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001239 shost_printk(KERN_WARNING, target->scsi_host,
1240 PFX "Unhandled RSP opcode %#x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001241 target->status = -ECONNRESET;
1242 break;
1243 }
1244
Vu Phamd2fcea72006-11-21 14:14:10 -08001245 if (!target->rx_ring[0]) {
1246 target->status = srp_alloc_iu_bufs(target);
1247 if (target->status)
1248 break;
1249 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001250
1251 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1252 if (!qp_attr) {
1253 target->status = -ENOMEM;
1254 break;
1255 }
1256
1257 qp_attr->qp_state = IB_QPS_RTR;
1258 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1259 if (target->status)
1260 break;
1261
1262 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1263 if (target->status)
1264 break;
1265
1266 target->status = srp_post_recv(target);
1267 if (target->status)
1268 break;
1269
1270 qp_attr->qp_state = IB_QPS_RTS;
1271 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1272 if (target->status)
1273 break;
1274
1275 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1276 if (target->status)
1277 break;
1278
1279 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1280 if (target->status)
1281 break;
1282
1283 break;
1284
1285 case IB_CM_REJ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001286 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001287 comp = 1;
1288
1289 srp_cm_rej_handler(cm_id, event, target);
1290 break;
1291
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001292 case IB_CM_DREQ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001293 shost_printk(KERN_WARNING, target->scsi_host,
1294 PFX "DREQ received - connection closed\n");
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001295 if (ib_send_cm_drep(cm_id, NULL, 0))
David Dillow7aa54bd2008-01-07 18:23:41 -05001296 shost_printk(KERN_ERR, target->scsi_host,
1297 PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001298 break;
1299
1300 case IB_CM_TIMEWAIT_EXIT:
David Dillow7aa54bd2008-01-07 18:23:41 -05001301 shost_printk(KERN_ERR, target->scsi_host,
1302 PFX "connection closed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001303
1304 comp = 1;
1305 target->status = 0;
1306 break;
1307
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001308 case IB_CM_MRA_RECEIVED:
1309 case IB_CM_DREQ_ERROR:
1310 case IB_CM_DREP_RECEIVED:
1311 break;
1312
Roland Dreieraef9ec32005-11-02 14:07:13 -08001313 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001314 shost_printk(KERN_WARNING, target->scsi_host,
1315 PFX "Unhandled CM event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001316 break;
1317 }
1318
1319 if (comp)
1320 complete(&target->done);
1321
1322 kfree(qp_attr);
1323
1324 return 0;
1325}
1326
Roland Dreierd945e1d2006-05-09 10:50:28 -07001327static int srp_send_tsk_mgmt(struct srp_target_port *target,
1328 struct srp_request *req, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001329{
Roland Dreieraef9ec32005-11-02 14:07:13 -08001330 struct srp_iu *iu;
1331 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001332
1333 spin_lock_irq(target->scsi_host->host_lock);
1334
Roland Dreier1285b3a2006-03-03 15:47:25 -08001335 if (target->state == SRP_TARGET_DEAD ||
1336 target->state == SRP_TARGET_REMOVED) {
Roland Dreierd945e1d2006-05-09 10:50:28 -07001337 req->scmnd->result = DID_BAD_TARGET << 16;
Roland Dreier1285b3a2006-03-03 15:47:25 -08001338 goto out;
1339 }
1340
Roland Dreieraef9ec32005-11-02 14:07:13 -08001341 init_completion(&req->done);
1342
David Dillow8cba2072007-12-19 17:08:43 -05001343 iu = __srp_get_tx_iu(target, SRP_REQ_TASK_MGMT);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001344 if (!iu)
1345 goto out;
1346
1347 tsk_mgmt = iu->buf;
1348 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1349
1350 tsk_mgmt->opcode = SRP_TSK_MGMT;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001351 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1352 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001353 tsk_mgmt->tsk_mgmt_func = func;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001354 tsk_mgmt->task_tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001355
1356 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1357 goto out;
1358
1359 req->tsk_mgmt = iu;
1360
1361 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001362
Roland Dreieraef9ec32005-11-02 14:07:13 -08001363 if (!wait_for_completion_timeout(&req->done,
1364 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001365 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001366
Roland Dreierd945e1d2006-05-09 10:50:28 -07001367 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001368
1369out:
1370 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001371 return -1;
1372}
1373
1374static int srp_find_req(struct srp_target_port *target,
1375 struct scsi_cmnd *scmnd,
1376 struct srp_request **req)
1377{
1378 if (scmnd->host_scribble == (void *) -1L)
1379 return -1;
1380
1381 *req = &target->req_ring[(long) scmnd->host_scribble];
1382
1383 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001384}
1385
1386static int srp_abort(struct scsi_cmnd *scmnd)
1387{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001388 struct srp_target_port *target = host_to_target(scmnd->device->host);
1389 struct srp_request *req;
1390 int ret = SUCCESS;
1391
David Dillow7aa54bd2008-01-07 18:23:41 -05001392 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001393
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001394 if (target->qp_in_error)
1395 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001396 if (srp_find_req(target, scmnd, &req))
1397 return FAILED;
1398 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1399 return FAILED;
1400
1401 spin_lock_irq(target->scsi_host->host_lock);
1402
1403 if (req->cmd_done) {
1404 srp_remove_req(target, req);
1405 scmnd->scsi_done(scmnd);
1406 } else if (!req->tsk_status) {
1407 srp_remove_req(target, req);
1408 scmnd->result = DID_ABORT << 16;
1409 } else
1410 ret = FAILED;
1411
1412 spin_unlock_irq(target->scsi_host->host_lock);
1413
1414 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001415}
1416
1417static int srp_reset_device(struct scsi_cmnd *scmnd)
1418{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001419 struct srp_target_port *target = host_to_target(scmnd->device->host);
1420 struct srp_request *req, *tmp;
1421
David Dillow7aa54bd2008-01-07 18:23:41 -05001422 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001423
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001424 if (target->qp_in_error)
1425 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001426 if (srp_find_req(target, scmnd, &req))
1427 return FAILED;
1428 if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1429 return FAILED;
1430 if (req->tsk_status)
1431 return FAILED;
1432
1433 spin_lock_irq(target->scsi_host->host_lock);
1434
1435 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001436 if (req->scmnd->device == scmnd->device)
1437 srp_reset_req(target, req);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001438
1439 spin_unlock_irq(target->scsi_host->host_lock);
1440
1441 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001442}
1443
1444static int srp_reset_host(struct scsi_cmnd *scmnd)
1445{
1446 struct srp_target_port *target = host_to_target(scmnd->device->host);
1447 int ret = FAILED;
1448
David Dillow7aa54bd2008-01-07 18:23:41 -05001449 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001450
1451 if (!srp_reconnect_target(target))
1452 ret = SUCCESS;
1453
1454 return ret;
1455}
1456
Tony Jonesee959b02008-02-22 00:13:36 +01001457static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
1458 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001459{
Tony Jonesee959b02008-02-22 00:13:36 +01001460 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001461
1462 if (target->state == SRP_TARGET_DEAD ||
1463 target->state == SRP_TARGET_REMOVED)
1464 return -ENODEV;
1465
1466 return sprintf(buf, "0x%016llx\n",
1467 (unsigned long long) be64_to_cpu(target->id_ext));
1468}
1469
Tony Jonesee959b02008-02-22 00:13:36 +01001470static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
1471 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001472{
Tony Jonesee959b02008-02-22 00:13:36 +01001473 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001474
1475 if (target->state == SRP_TARGET_DEAD ||
1476 target->state == SRP_TARGET_REMOVED)
1477 return -ENODEV;
1478
1479 return sprintf(buf, "0x%016llx\n",
1480 (unsigned long long) be64_to_cpu(target->ioc_guid));
1481}
1482
Tony Jonesee959b02008-02-22 00:13:36 +01001483static ssize_t show_service_id(struct device *dev,
1484 struct device_attribute *attr, char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001485{
Tony Jonesee959b02008-02-22 00:13:36 +01001486 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001487
1488 if (target->state == SRP_TARGET_DEAD ||
1489 target->state == SRP_TARGET_REMOVED)
1490 return -ENODEV;
1491
1492 return sprintf(buf, "0x%016llx\n",
1493 (unsigned long long) be64_to_cpu(target->service_id));
1494}
1495
Tony Jonesee959b02008-02-22 00:13:36 +01001496static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
1497 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001498{
Tony Jonesee959b02008-02-22 00:13:36 +01001499 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001500
1501 if (target->state == SRP_TARGET_DEAD ||
1502 target->state == SRP_TARGET_REMOVED)
1503 return -ENODEV;
1504
1505 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1506}
1507
Tony Jonesee959b02008-02-22 00:13:36 +01001508static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
1509 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001510{
Tony Jonesee959b02008-02-22 00:13:36 +01001511 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001512
1513 if (target->state == SRP_TARGET_DEAD ||
1514 target->state == SRP_TARGET_REMOVED)
1515 return -ENODEV;
1516
1517 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1518 be16_to_cpu(((__be16 *) target->path.dgid.raw)[0]),
1519 be16_to_cpu(((__be16 *) target->path.dgid.raw)[1]),
1520 be16_to_cpu(((__be16 *) target->path.dgid.raw)[2]),
1521 be16_to_cpu(((__be16 *) target->path.dgid.raw)[3]),
1522 be16_to_cpu(((__be16 *) target->path.dgid.raw)[4]),
1523 be16_to_cpu(((__be16 *) target->path.dgid.raw)[5]),
1524 be16_to_cpu(((__be16 *) target->path.dgid.raw)[6]),
1525 be16_to_cpu(((__be16 *) target->path.dgid.raw)[7]));
1526}
1527
Tony Jonesee959b02008-02-22 00:13:36 +01001528static ssize_t show_orig_dgid(struct device *dev,
1529 struct device_attribute *attr, char *buf)
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001530{
Tony Jonesee959b02008-02-22 00:13:36 +01001531 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001532
1533 if (target->state == SRP_TARGET_DEAD ||
1534 target->state == SRP_TARGET_REMOVED)
1535 return -ENODEV;
1536
1537 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1538 be16_to_cpu(target->orig_dgid[0]),
1539 be16_to_cpu(target->orig_dgid[1]),
1540 be16_to_cpu(target->orig_dgid[2]),
1541 be16_to_cpu(target->orig_dgid[3]),
1542 be16_to_cpu(target->orig_dgid[4]),
1543 be16_to_cpu(target->orig_dgid[5]),
1544 be16_to_cpu(target->orig_dgid[6]),
1545 be16_to_cpu(target->orig_dgid[7]));
1546}
1547
Tony Jonesee959b02008-02-22 00:13:36 +01001548static ssize_t show_zero_req_lim(struct device *dev,
1549 struct device_attribute *attr, char *buf)
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001550{
Tony Jonesee959b02008-02-22 00:13:36 +01001551 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001552
1553 if (target->state == SRP_TARGET_DEAD ||
1554 target->state == SRP_TARGET_REMOVED)
1555 return -ENODEV;
1556
1557 return sprintf(buf, "%d\n", target->zero_req_lim);
1558}
1559
Tony Jonesee959b02008-02-22 00:13:36 +01001560static ssize_t show_local_ib_port(struct device *dev,
1561 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001562{
Tony Jonesee959b02008-02-22 00:13:36 +01001563 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001564
1565 return sprintf(buf, "%d\n", target->srp_host->port);
1566}
1567
Tony Jonesee959b02008-02-22 00:13:36 +01001568static ssize_t show_local_ib_device(struct device *dev,
1569 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001570{
Tony Jonesee959b02008-02-22 00:13:36 +01001571 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001572
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001573 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001574}
1575
Tony Jonesee959b02008-02-22 00:13:36 +01001576static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1577static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1578static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1579static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1580static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1581static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
1582static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1583static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1584static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001585
Tony Jonesee959b02008-02-22 00:13:36 +01001586static struct device_attribute *srp_host_attrs[] = {
1587 &dev_attr_id_ext,
1588 &dev_attr_ioc_guid,
1589 &dev_attr_service_id,
1590 &dev_attr_pkey,
1591 &dev_attr_dgid,
1592 &dev_attr_orig_dgid,
1593 &dev_attr_zero_req_lim,
1594 &dev_attr_local_ib_port,
1595 &dev_attr_local_ib_device,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001596 NULL
1597};
1598
Roland Dreieraef9ec32005-11-02 14:07:13 -08001599static struct scsi_host_template srp_template = {
1600 .module = THIS_MODULE,
Roland Dreierb7f008f2007-05-06 21:18:11 -07001601 .name = "InfiniBand SRP initiator",
1602 .proc_name = DRV_NAME,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001603 .info = srp_target_info,
1604 .queuecommand = srp_queuecommand,
1605 .eh_abort_handler = srp_abort,
1606 .eh_device_reset_handler = srp_reset_device,
1607 .eh_host_reset_handler = srp_reset_host,
1608 .can_queue = SRP_SQ_SIZE,
1609 .this_id = -1,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001610 .cmd_per_lun = SRP_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001611 .use_clustering = ENABLE_CLUSTERING,
1612 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001613};
1614
1615static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1616{
FUJITA Tomonori32368222007-06-27 16:33:12 +09001617 struct srp_rport_identifiers ids;
1618 struct srp_rport *rport;
1619
Roland Dreieraef9ec32005-11-02 14:07:13 -08001620 sprintf(target->target_name, "SRP.T10:%016llX",
1621 (unsigned long long) be64_to_cpu(target->id_ext));
1622
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001623 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001624 return -ENODEV;
1625
FUJITA Tomonori32368222007-06-27 16:33:12 +09001626 memcpy(ids.port_id, &target->id_ext, 8);
1627 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09001628 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori32368222007-06-27 16:33:12 +09001629 rport = srp_rport_add(target->scsi_host, &ids);
1630 if (IS_ERR(rport)) {
1631 scsi_remove_host(target->scsi_host);
1632 return PTR_ERR(rport);
1633 }
1634
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001635 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001636 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001637 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001638
1639 target->state = SRP_TARGET_LIVE;
1640
Roland Dreieraef9ec32005-11-02 14:07:13 -08001641 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07001642 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001643
1644 return 0;
1645}
1646
Tony Jonesee959b02008-02-22 00:13:36 +01001647static void srp_release_dev(struct device *dev)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001648{
1649 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01001650 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001651
1652 complete(&host->released);
1653}
1654
1655static struct class srp_class = {
1656 .name = "infiniband_srp",
Tony Jonesee959b02008-02-22 00:13:36 +01001657 .dev_release = srp_release_dev
Roland Dreieraef9ec32005-11-02 14:07:13 -08001658};
1659
1660/*
1661 * Target ports are added by writing
1662 *
1663 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1664 * pkey=<P_Key>,service_id=<service ID>
1665 *
1666 * to the add_target sysfs attribute.
1667 */
1668enum {
1669 SRP_OPT_ERR = 0,
1670 SRP_OPT_ID_EXT = 1 << 0,
1671 SRP_OPT_IOC_GUID = 1 << 1,
1672 SRP_OPT_DGID = 1 << 2,
1673 SRP_OPT_PKEY = 1 << 3,
1674 SRP_OPT_SERVICE_ID = 1 << 4,
1675 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07001676 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07001677 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001678 SRP_OPT_INITIATOR_EXT = 1 << 8,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001679 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1680 SRP_OPT_IOC_GUID |
1681 SRP_OPT_DGID |
1682 SRP_OPT_PKEY |
1683 SRP_OPT_SERVICE_ID),
1684};
1685
Steven Whitehousea447c092008-10-13 10:46:57 +01001686static const match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07001687 { SRP_OPT_ID_EXT, "id_ext=%s" },
1688 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1689 { SRP_OPT_DGID, "dgid=%s" },
1690 { SRP_OPT_PKEY, "pkey=%x" },
1691 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1692 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1693 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07001694 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001695 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
Vu Pham52fb2b502006-06-17 20:37:31 -07001696 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001697};
1698
1699static int srp_parse_options(const char *buf, struct srp_target_port *target)
1700{
1701 char *options, *sep_opt;
1702 char *p;
1703 char dgid[3];
1704 substring_t args[MAX_OPT_ARGS];
1705 int opt_mask = 0;
1706 int token;
1707 int ret = -EINVAL;
1708 int i;
1709
1710 options = kstrdup(buf, GFP_KERNEL);
1711 if (!options)
1712 return -ENOMEM;
1713
1714 sep_opt = options;
1715 while ((p = strsep(&sep_opt, ",")) != NULL) {
1716 if (!*p)
1717 continue;
1718
1719 token = match_token(p, srp_opt_tokens, args);
1720 opt_mask |= token;
1721
1722 switch (token) {
1723 case SRP_OPT_ID_EXT:
1724 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001725 if (!p) {
1726 ret = -ENOMEM;
1727 goto out;
1728 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001729 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1730 kfree(p);
1731 break;
1732
1733 case SRP_OPT_IOC_GUID:
1734 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001735 if (!p) {
1736 ret = -ENOMEM;
1737 goto out;
1738 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001739 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1740 kfree(p);
1741 break;
1742
1743 case SRP_OPT_DGID:
1744 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001745 if (!p) {
1746 ret = -ENOMEM;
1747 goto out;
1748 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001749 if (strlen(p) != 32) {
1750 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07001751 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001752 goto out;
1753 }
1754
1755 for (i = 0; i < 16; ++i) {
1756 strlcpy(dgid, p + i * 2, 3);
1757 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1758 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08001759 kfree(p);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001760 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001761 break;
1762
1763 case SRP_OPT_PKEY:
1764 if (match_hex(args, &token)) {
1765 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1766 goto out;
1767 }
1768 target->path.pkey = cpu_to_be16(token);
1769 break;
1770
1771 case SRP_OPT_SERVICE_ID:
1772 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001773 if (!p) {
1774 ret = -ENOMEM;
1775 goto out;
1776 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001777 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
Sean Hefty247e0202007-08-08 15:51:18 -07001778 target->path.service_id = target->service_id;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001779 kfree(p);
1780 break;
1781
1782 case SRP_OPT_MAX_SECT:
1783 if (match_int(args, &token)) {
1784 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1785 goto out;
1786 }
1787 target->scsi_host->max_sectors = token;
1788 break;
1789
Vu Pham52fb2b502006-06-17 20:37:31 -07001790 case SRP_OPT_MAX_CMD_PER_LUN:
1791 if (match_int(args, &token)) {
1792 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1793 goto out;
1794 }
1795 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1796 break;
1797
Ramachandra K0c0450db2006-06-17 20:37:38 -07001798 case SRP_OPT_IO_CLASS:
1799 if (match_hex(args, &token)) {
1800 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1801 goto out;
1802 }
1803 if (token != SRP_REV10_IB_IO_CLASS &&
1804 token != SRP_REV16A_IB_IO_CLASS) {
1805 printk(KERN_WARNING PFX "unknown IO class parameter value"
1806 " %x specified (use %x or %x).\n",
1807 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1808 goto out;
1809 }
1810 target->io_class = token;
1811 break;
1812
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001813 case SRP_OPT_INITIATOR_EXT:
1814 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001815 if (!p) {
1816 ret = -ENOMEM;
1817 goto out;
1818 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001819 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1820 kfree(p);
1821 break;
1822
Roland Dreieraef9ec32005-11-02 14:07:13 -08001823 default:
1824 printk(KERN_WARNING PFX "unknown parameter or missing value "
1825 "'%s' in target creation request\n", p);
1826 goto out;
1827 }
1828 }
1829
1830 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1831 ret = 0;
1832 else
1833 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1834 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1835 !(srp_opt_tokens[i].token & opt_mask))
1836 printk(KERN_WARNING PFX "target creation request is "
1837 "missing parameter '%s'\n",
1838 srp_opt_tokens[i].pattern);
1839
1840out:
1841 kfree(options);
1842 return ret;
1843}
1844
Tony Jonesee959b02008-02-22 00:13:36 +01001845static ssize_t srp_create_target(struct device *dev,
1846 struct device_attribute *attr,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001847 const char *buf, size_t count)
1848{
1849 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01001850 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001851 struct Scsi_Host *target_host;
1852 struct srp_target_port *target;
1853 int ret;
1854 int i;
1855
1856 target_host = scsi_host_alloc(&srp_template,
1857 sizeof (struct srp_target_port));
1858 if (!target_host)
1859 return -ENOMEM;
1860
FUJITA Tomonori32368222007-06-27 16:33:12 +09001861 target_host->transportt = ib_srp_transport_template;
Arne Redlich3c8edf02006-11-15 12:43:00 +01001862 target_host->max_lun = SRP_MAX_LUN;
1863 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08001864
Roland Dreieraef9ec32005-11-02 14:07:13 -08001865 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001866
Ramachandra K0c0450db2006-06-17 20:37:38 -07001867 target->io_class = SRP_REV16A_IB_IO_CLASS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001868 target->scsi_host = target_host;
1869 target->srp_host = host;
1870
Roland Dreierd945e1d2006-05-09 10:50:28 -07001871 INIT_LIST_HEAD(&target->free_reqs);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001872 INIT_LIST_HEAD(&target->req_queue);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001873 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1874 target->req_ring[i].index = i;
1875 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1876 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001877
1878 ret = srp_parse_options(buf, target);
1879 if (ret)
1880 goto err;
1881
Roland Dreier969a60f2008-07-14 23:48:43 -07001882 ib_query_gid(host->srp_dev->dev, host->port, 0, &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001883
David Dillow7aa54bd2008-01-07 18:23:41 -05001884 shost_printk(KERN_DEBUG, target->scsi_host, PFX
1885 "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1886 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
Roland Dreieraef9ec32005-11-02 14:07:13 -08001887 (unsigned long long) be64_to_cpu(target->id_ext),
1888 (unsigned long long) be64_to_cpu(target->ioc_guid),
1889 be16_to_cpu(target->path.pkey),
1890 (unsigned long long) be64_to_cpu(target->service_id),
1891 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]),
1892 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]),
1893 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]),
1894 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]),
1895 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]),
1896 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]),
1897 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]),
1898 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14]));
1899
1900 ret = srp_create_target_ib(target);
1901 if (ret)
1902 goto err;
1903
David Dillow9fe4bcf2008-01-08 17:08:52 -05001904 ret = srp_new_cm_id(target);
1905 if (ret)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001906 goto err_free;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001907
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001908 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001909 ret = srp_connect_target(target);
1910 if (ret) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001911 shost_printk(KERN_ERR, target->scsi_host,
1912 PFX "Connection failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001913 goto err_cm_id;
1914 }
1915
1916 ret = srp_add_target(host, target);
1917 if (ret)
1918 goto err_disconnect;
1919
1920 return count;
1921
1922err_disconnect:
1923 srp_disconnect_target(target);
1924
1925err_cm_id:
1926 ib_destroy_cm_id(target->cm_id);
1927
1928err_free:
1929 srp_free_target_ib(target);
1930
1931err:
1932 scsi_host_put(target_host);
1933
1934 return ret;
1935}
1936
Tony Jonesee959b02008-02-22 00:13:36 +01001937static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001938
Tony Jonesee959b02008-02-22 00:13:36 +01001939static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
1940 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001941{
Tony Jonesee959b02008-02-22 00:13:36 +01001942 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001943
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001944 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001945}
1946
Tony Jonesee959b02008-02-22 00:13:36 +01001947static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001948
Tony Jonesee959b02008-02-22 00:13:36 +01001949static ssize_t show_port(struct device *dev, struct device_attribute *attr,
1950 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001951{
Tony Jonesee959b02008-02-22 00:13:36 +01001952 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001953
1954 return sprintf(buf, "%d\n", host->port);
1955}
1956
Tony Jonesee959b02008-02-22 00:13:36 +01001957static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001958
Roland Dreierf5358a12006-06-17 20:37:29 -07001959static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001960{
1961 struct srp_host *host;
1962
1963 host = kzalloc(sizeof *host, GFP_KERNEL);
1964 if (!host)
1965 return NULL;
1966
1967 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001968 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001969 init_completion(&host->released);
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001970 host->srp_dev = device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001971 host->port = port;
1972
Tony Jonesee959b02008-02-22 00:13:36 +01001973 host->dev.class = &srp_class;
1974 host->dev.parent = device->dev->dma_device;
1975 snprintf(host->dev.bus_id, BUS_ID_SIZE, "srp-%s-%d",
Roland Dreierf5358a12006-06-17 20:37:29 -07001976 device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001977
Tony Jonesee959b02008-02-22 00:13:36 +01001978 if (device_register(&host->dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07001979 goto free_host;
Tony Jonesee959b02008-02-22 00:13:36 +01001980 if (device_create_file(&host->dev, &dev_attr_add_target))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001981 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01001982 if (device_create_file(&host->dev, &dev_attr_ibdev))
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_port))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001985 goto err_class;
1986
1987 return host;
1988
1989err_class:
Tony Jonesee959b02008-02-22 00:13:36 +01001990 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001991
Roland Dreierf5358a12006-06-17 20:37:29 -07001992free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001993 kfree(host);
1994
1995 return NULL;
1996}
1997
1998static void srp_add_one(struct ib_device *device)
1999{
Roland Dreierf5358a12006-06-17 20:37:29 -07002000 struct srp_device *srp_dev;
2001 struct ib_device_attr *dev_attr;
2002 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002003 struct srp_host *host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002004 int s, e, p;
2005
Roland Dreierf5358a12006-06-17 20:37:29 -07002006 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
2007 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08002008 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002009
Roland Dreierf5358a12006-06-17 20:37:29 -07002010 if (ib_query_device(device, dev_attr)) {
2011 printk(KERN_WARNING PFX "Query device failed for %s\n",
2012 device->name);
2013 goto free_attr;
2014 }
2015
2016 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
2017 if (!srp_dev)
2018 goto free_attr;
2019
2020 /*
2021 * Use the smallest page size supported by the HCA, down to a
2022 * minimum of 512 bytes (which is the smallest sector that a
2023 * SCSI command will ever carry).
2024 */
2025 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
2026 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
Roland Dreierbf628dc2006-12-15 14:01:49 -08002027 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
Roland Dreierf5358a12006-06-17 20:37:29 -07002028
2029 INIT_LIST_HEAD(&srp_dev->dev_list);
2030
2031 srp_dev->dev = device;
2032 srp_dev->pd = ib_alloc_pd(device);
2033 if (IS_ERR(srp_dev->pd))
2034 goto free_dev;
2035
2036 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
2037 IB_ACCESS_LOCAL_WRITE |
2038 IB_ACCESS_REMOTE_READ |
2039 IB_ACCESS_REMOTE_WRITE);
2040 if (IS_ERR(srp_dev->mr))
2041 goto err_pd;
2042
2043 memset(&fmr_param, 0, sizeof fmr_param);
2044 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
2045 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
2046 fmr_param.cache = 1;
2047 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
2048 fmr_param.page_shift = srp_dev->fmr_page_shift;
2049 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
2050 IB_ACCESS_REMOTE_WRITE |
2051 IB_ACCESS_REMOTE_READ);
2052
2053 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
2054 if (IS_ERR(srp_dev->fmr_pool))
2055 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002056
Tom Tucker07ebafb2006-08-03 16:02:42 -05002057 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002058 s = 0;
2059 e = 0;
2060 } else {
2061 s = 1;
2062 e = device->phys_port_cnt;
2063 }
2064
2065 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07002066 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002067 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07002068 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002069 }
2070
Roland Dreierf5358a12006-06-17 20:37:29 -07002071 ib_set_client_data(device, &srp_client, srp_dev);
2072
2073 goto free_attr;
2074
2075err_pd:
2076 ib_dealloc_pd(srp_dev->pd);
2077
2078free_dev:
2079 kfree(srp_dev);
2080
2081free_attr:
2082 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002083}
2084
2085static void srp_remove_one(struct ib_device *device)
2086{
Roland Dreierf5358a12006-06-17 20:37:29 -07002087 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002088 struct srp_host *host, *tmp_host;
2089 LIST_HEAD(target_list);
2090 struct srp_target_port *target, *tmp_target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002091
Roland Dreierf5358a12006-06-17 20:37:29 -07002092 srp_dev = ib_get_client_data(device, &srp_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002093
Roland Dreierf5358a12006-06-17 20:37:29 -07002094 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Tony Jonesee959b02008-02-22 00:13:36 +01002095 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002096 /*
2097 * Wait for the sysfs entry to go away, so that no new
2098 * target ports can be created.
2099 */
2100 wait_for_completion(&host->released);
2101
2102 /*
2103 * Mark all target ports as removed, so we stop queueing
2104 * commands and don't try to reconnect.
2105 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002106 spin_lock(&host->target_lock);
Matthew Wilcox549c5fc22006-06-17 20:37:30 -07002107 list_for_each_entry(target, &host->target_list, list) {
Ishai Rabinovitz0c5b3952006-06-17 20:37:31 -07002108 spin_lock_irq(target->scsi_host->host_lock);
2109 target->state = SRP_TARGET_REMOVED;
2110 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002111 }
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002112 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002113
2114 /*
2115 * Wait for any reconnection tasks that may have
2116 * started before we marked our target ports as
2117 * removed, and any target port removal tasks.
2118 */
2119 flush_scheduled_work();
2120
2121 list_for_each_entry_safe(target, tmp_target,
2122 &host->target_list, list) {
David Dillowb0e47c82008-01-03 10:25:27 -08002123 srp_remove_host(target->scsi_host);
Dave Dillowad696982008-01-03 22:35:41 -05002124 scsi_remove_host(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002125 srp_disconnect_target(target);
2126 ib_destroy_cm_id(target->cm_id);
2127 srp_free_target_ib(target);
2128 scsi_host_put(target->scsi_host);
2129 }
2130
Roland Dreieraef9ec32005-11-02 14:07:13 -08002131 kfree(host);
2132 }
2133
Roland Dreierf5358a12006-06-17 20:37:29 -07002134 if (srp_dev->fmr_pool)
2135 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2136 ib_dereg_mr(srp_dev->mr);
2137 ib_dealloc_pd(srp_dev->pd);
2138
2139 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002140}
2141
FUJITA Tomonori32368222007-06-27 16:33:12 +09002142static struct srp_function_template ib_srp_transport_functions = {
2143};
2144
Roland Dreieraef9ec32005-11-02 14:07:13 -08002145static int __init srp_init_module(void)
2146{
2147 int ret;
2148
David Dillow1e89a192008-04-16 21:01:12 -07002149 if (srp_sg_tablesize > 255) {
2150 printk(KERN_WARNING PFX "Clamping srp_sg_tablesize to 255\n");
2151 srp_sg_tablesize = 255;
2152 }
2153
FUJITA Tomonori32368222007-06-27 16:33:12 +09002154 ib_srp_transport_template =
2155 srp_attach_transport(&ib_srp_transport_functions);
2156 if (!ib_srp_transport_template)
2157 return -ENOMEM;
2158
Vu Pham74b0a152006-06-17 20:37:32 -07002159 srp_template.sg_tablesize = srp_sg_tablesize;
2160 srp_max_iu_len = (sizeof (struct srp_cmd) +
2161 sizeof (struct srp_indirect_buf) +
2162 srp_sg_tablesize * 16);
2163
Roland Dreieraef9ec32005-11-02 14:07:13 -08002164 ret = class_register(&srp_class);
2165 if (ret) {
2166 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002167 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002168 return ret;
2169 }
2170
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002171 ib_sa_register_client(&srp_sa_client);
2172
Roland Dreieraef9ec32005-11-02 14:07:13 -08002173 ret = ib_register_client(&srp_client);
2174 if (ret) {
2175 printk(KERN_ERR PFX "couldn't register IB client\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002176 srp_release_transport(ib_srp_transport_template);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002177 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002178 class_unregister(&srp_class);
2179 return ret;
2180 }
2181
2182 return 0;
2183}
2184
2185static void __exit srp_cleanup_module(void)
2186{
2187 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002188 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002189 class_unregister(&srp_class);
FUJITA Tomonori32368222007-06-27 16:33:12 +09002190 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002191}
2192
2193module_init(srp_init_module);
2194module_exit(srp_cleanup_module);