blob: 5e8ac577f0ad1c33dd9cf65a71d18dd149848822 [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>
50
51#include <rdma/ib_cache.h>
52
53#include "ib_srp.h"
54
55#define DRV_NAME "ib_srp"
56#define PFX DRV_NAME ": "
57#define DRV_VERSION "0.2"
58#define DRV_RELDATE "November 1, 2005"
59
60MODULE_AUTHOR("Roland Dreier");
61MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
62 "v" DRV_VERSION " (" DRV_RELDATE ")");
63MODULE_LICENSE("Dual BSD/GPL");
64
Vu Pham74b0a152006-06-17 20:37:32 -070065static int srp_sg_tablesize = SRP_DEF_SG_TABLESIZE;
66static int srp_max_iu_len;
67
68module_param(srp_sg_tablesize, int, 0444);
69MODULE_PARM_DESC(srp_sg_tablesize,
70 "Max number of gather/scatter entries per I/O (default is 12)");
71
Roland Dreieraef9ec32005-11-02 14:07:13 -080072static int topspin_workarounds = 1;
73
74module_param(topspin_workarounds, int, 0444);
75MODULE_PARM_DESC(topspin_workarounds,
76 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
77
78static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
79
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -070080static int mellanox_workarounds = 1;
81
82module_param(mellanox_workarounds, int, 0444);
83MODULE_PARM_DESC(mellanox_workarounds,
84 "Enable workarounds for Mellanox SRP target bugs if != 0");
85
86static const u8 mellanox_oui[3] = { 0x00, 0x02, 0xc9 };
87
Roland Dreieraef9ec32005-11-02 14:07:13 -080088static void srp_add_one(struct ib_device *device);
89static void srp_remove_one(struct ib_device *device);
90static void srp_completion(struct ib_cq *cq, void *target_ptr);
91static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
92
93static struct ib_client srp_client = {
94 .name = "srp",
95 .add = srp_add_one,
96 .remove = srp_remove_one
97};
98
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -070099static struct ib_sa_client srp_sa_client;
100
Roland Dreieraef9ec32005-11-02 14:07:13 -0800101static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
102{
103 return (struct srp_target_port *) host->hostdata;
104}
105
106static const char *srp_target_info(struct Scsi_Host *host)
107{
108 return host_to_target(host)->target_name;
109}
110
111static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
112 gfp_t gfp_mask,
113 enum dma_data_direction direction)
114{
115 struct srp_iu *iu;
116
117 iu = kmalloc(sizeof *iu, gfp_mask);
118 if (!iu)
119 goto out;
120
121 iu->buf = kzalloc(size, gfp_mask);
122 if (!iu->buf)
123 goto out_free_iu;
124
Ralph Campbell85507bc2006-12-12 14:30:55 -0800125 iu->dma = ib_dma_map_single(host->dev->dev, iu->buf, size, direction);
126 if (ib_dma_mapping_error(host->dev->dev, iu->dma))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800127 goto out_free_buf;
128
129 iu->size = size;
130 iu->direction = direction;
131
132 return iu;
133
134out_free_buf:
135 kfree(iu->buf);
136out_free_iu:
137 kfree(iu);
138out:
139 return NULL;
140}
141
142static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
143{
144 if (!iu)
145 return;
146
Ralph Campbell85507bc2006-12-12 14:30:55 -0800147 ib_dma_unmap_single(host->dev->dev, iu->dma, iu->size, iu->direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800148 kfree(iu->buf);
149 kfree(iu);
150}
151
152static void srp_qp_event(struct ib_event *event, void *context)
153{
154 printk(KERN_ERR PFX "QP event %d\n", event->event);
155}
156
157static int srp_init_qp(struct srp_target_port *target,
158 struct ib_qp *qp)
159{
160 struct ib_qp_attr *attr;
161 int ret;
162
163 attr = kmalloc(sizeof *attr, GFP_KERNEL);
164 if (!attr)
165 return -ENOMEM;
166
Roland Dreierf5358a12006-06-17 20:37:29 -0700167 ret = ib_find_cached_pkey(target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800168 target->srp_host->port,
169 be16_to_cpu(target->path.pkey),
170 &attr->pkey_index);
171 if (ret)
172 goto out;
173
174 attr->qp_state = IB_QPS_INIT;
175 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
176 IB_ACCESS_REMOTE_WRITE);
177 attr->port_num = target->srp_host->port;
178
179 ret = ib_modify_qp(qp, attr,
180 IB_QP_STATE |
181 IB_QP_PKEY_INDEX |
182 IB_QP_ACCESS_FLAGS |
183 IB_QP_PORT);
184
185out:
186 kfree(attr);
187 return ret;
188}
189
190static int srp_create_target_ib(struct srp_target_port *target)
191{
192 struct ib_qp_init_attr *init_attr;
193 int ret;
194
195 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
196 if (!init_attr)
197 return -ENOMEM;
198
Roland Dreierf5358a12006-06-17 20:37:29 -0700199 target->cq = ib_create_cq(target->srp_host->dev->dev, srp_completion,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800200 NULL, target, SRP_CQ_SIZE);
201 if (IS_ERR(target->cq)) {
202 ret = PTR_ERR(target->cq);
203 goto out;
204 }
205
206 ib_req_notify_cq(target->cq, IB_CQ_NEXT_COMP);
207
208 init_attr->event_handler = srp_qp_event;
209 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
210 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
211 init_attr->cap.max_recv_sge = 1;
212 init_attr->cap.max_send_sge = 1;
213 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
214 init_attr->qp_type = IB_QPT_RC;
215 init_attr->send_cq = target->cq;
216 init_attr->recv_cq = target->cq;
217
Roland Dreierf5358a12006-06-17 20:37:29 -0700218 target->qp = ib_create_qp(target->srp_host->dev->pd, init_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800219 if (IS_ERR(target->qp)) {
220 ret = PTR_ERR(target->qp);
221 ib_destroy_cq(target->cq);
222 goto out;
223 }
224
225 ret = srp_init_qp(target, target->qp);
226 if (ret) {
227 ib_destroy_qp(target->qp);
228 ib_destroy_cq(target->cq);
229 goto out;
230 }
231
232out:
233 kfree(init_attr);
234 return ret;
235}
236
237static void srp_free_target_ib(struct srp_target_port *target)
238{
239 int i;
240
241 ib_destroy_qp(target->qp);
242 ib_destroy_cq(target->cq);
243
244 for (i = 0; i < SRP_RQ_SIZE; ++i)
245 srp_free_iu(target->srp_host, target->rx_ring[i]);
246 for (i = 0; i < SRP_SQ_SIZE + 1; ++i)
247 srp_free_iu(target->srp_host, target->tx_ring[i]);
248}
249
250static void srp_path_rec_completion(int status,
251 struct ib_sa_path_rec *pathrec,
252 void *target_ptr)
253{
254 struct srp_target_port *target = target_ptr;
255
256 target->status = status;
257 if (status)
258 printk(KERN_ERR PFX "Got failed path rec status %d\n", status);
259 else
260 target->path = *pathrec;
261 complete(&target->done);
262}
263
264static int srp_lookup_path(struct srp_target_port *target)
265{
266 target->path.numb_path = 1;
267
268 init_completion(&target->done);
269
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700270 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
271 target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800272 target->srp_host->port,
273 &target->path,
274 IB_SA_PATH_REC_DGID |
275 IB_SA_PATH_REC_SGID |
276 IB_SA_PATH_REC_NUMB_PATH |
277 IB_SA_PATH_REC_PKEY,
278 SRP_PATH_REC_TIMEOUT_MS,
279 GFP_KERNEL,
280 srp_path_rec_completion,
281 target, &target->path_query);
282 if (target->path_query_id < 0)
283 return target->path_query_id;
284
285 wait_for_completion(&target->done);
286
287 if (target->status < 0)
288 printk(KERN_WARNING PFX "Path record query failed\n");
289
290 return target->status;
291}
292
293static int srp_send_req(struct srp_target_port *target)
294{
295 struct {
296 struct ib_cm_req_param param;
297 struct srp_login_req priv;
298 } *req = NULL;
299 int status;
300
301 req = kzalloc(sizeof *req, GFP_KERNEL);
302 if (!req)
303 return -ENOMEM;
304
305 req->param.primary_path = &target->path;
306 req->param.alternate_path = NULL;
307 req->param.service_id = target->service_id;
308 req->param.qp_num = target->qp->qp_num;
309 req->param.qp_type = target->qp->qp_type;
310 req->param.private_data = &req->priv;
311 req->param.private_data_len = sizeof req->priv;
312 req->param.flow_control = 1;
313
314 get_random_bytes(&req->param.starting_psn, 4);
315 req->param.starting_psn &= 0xffffff;
316
317 /*
318 * Pick some arbitrary defaults here; we could make these
319 * module parameters if anyone cared about setting them.
320 */
321 req->param.responder_resources = 4;
322 req->param.remote_cm_response_timeout = 20;
323 req->param.local_cm_response_timeout = 20;
324 req->param.retry_count = 7;
325 req->param.rnr_retry_count = 7;
326 req->param.max_cm_retries = 15;
327
328 req->priv.opcode = SRP_LOGIN_REQ;
329 req->priv.tag = 0;
Vu Pham74b0a152006-06-17 20:37:32 -0700330 req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800331 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
332 SRP_BUF_FORMAT_INDIRECT);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700333 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700334 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450db2006-06-17 20:37:38 -0700335 * port identifier format is 8 bytes of ID extension followed
336 * by 8 bytes of GUID. Older drafts put the two halves in the
337 * opposite order, so that the GUID comes first.
338 *
339 * Targets conforming to these obsolete drafts can be
340 * recognized by the I/O Class they report.
341 */
342 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
343 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200344 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700345 memcpy(req->priv.initiator_port_id + 8,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200346 &target->initiator_ext, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700347 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
348 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
349 } else {
350 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200351 &target->initiator_ext, 8);
352 memcpy(req->priv.initiator_port_id + 8,
353 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700354 memcpy(req->priv.target_port_id, &target->id_ext, 8);
355 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
356 }
357
Roland Dreieraef9ec32005-11-02 14:07:13 -0800358 /*
359 * Topspin/Cisco SRP targets will reject our login unless we
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200360 * zero out the first 8 bytes of our initiator port ID and set
361 * the second 8 bytes to the local node GUID.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800362 */
363 if (topspin_workarounds && !memcmp(&target->ioc_guid, topspin_oui, 3)) {
364 printk(KERN_DEBUG PFX "Topspin/Cisco initiator port ID workaround "
365 "activated for target GUID %016llx\n",
366 (unsigned long long) be64_to_cpu(target->ioc_guid));
367 memset(req->priv.initiator_port_id, 0, 8);
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200368 memcpy(req->priv.initiator_port_id + 8,
369 &target->srp_host->dev->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800370 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800371
372 status = ib_send_cm_req(target->cm_id, &req->param);
373
374 kfree(req);
375
376 return status;
377}
378
379static void srp_disconnect_target(struct srp_target_port *target)
380{
381 /* XXX should send SRP_I_LOGOUT request */
382
383 init_completion(&target->done);
Roland Dreiere6581052006-05-17 09:13:21 -0700384 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
385 printk(KERN_DEBUG PFX "Sending CM DREQ failed\n");
386 return;
387 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800388 wait_for_completion(&target->done);
389}
390
David Howellsc4028952006-11-22 14:57:56 +0000391static void srp_remove_work(struct work_struct *work)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800392{
David Howellsc4028952006-11-22 14:57:56 +0000393 struct srp_target_port *target =
394 container_of(work, struct srp_target_port, work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800395
396 spin_lock_irq(target->scsi_host->host_lock);
397 if (target->state != SRP_TARGET_DEAD) {
398 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800399 return;
400 }
401 target->state = SRP_TARGET_REMOVED;
402 spin_unlock_irq(target->scsi_host->host_lock);
403
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700404 spin_lock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800405 list_del(&target->list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700406 spin_unlock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800407
408 scsi_remove_host(target->scsi_host);
409 ib_destroy_cm_id(target->cm_id);
410 srp_free_target_ib(target);
411 scsi_host_put(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800412}
413
414static int srp_connect_target(struct srp_target_port *target)
415{
416 int ret;
417
418 ret = srp_lookup_path(target);
419 if (ret)
420 return ret;
421
422 while (1) {
423 init_completion(&target->done);
424 ret = srp_send_req(target);
425 if (ret)
426 return ret;
427 wait_for_completion(&target->done);
428
429 /*
430 * The CM event handling code will set status to
431 * SRP_PORT_REDIRECT if we get a port redirect REJ
432 * back, or SRP_DLID_REDIRECT if we get a lid/qp
433 * redirect REJ back.
434 */
435 switch (target->status) {
436 case 0:
437 return 0;
438
439 case SRP_PORT_REDIRECT:
440 ret = srp_lookup_path(target);
441 if (ret)
442 return ret;
443 break;
444
445 case SRP_DLID_REDIRECT:
446 break;
447
448 default:
449 return target->status;
450 }
451 }
452}
453
Roland Dreierd945e1d2006-05-09 10:50:28 -0700454static void srp_unmap_data(struct scsi_cmnd *scmnd,
455 struct srp_target_port *target,
456 struct srp_request *req)
457{
458 struct scatterlist *scat;
459 int nents;
460
461 if (!scmnd->request_buffer ||
462 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
463 scmnd->sc_data_direction != DMA_FROM_DEVICE))
464 return;
465
Roland Dreierf5358a12006-06-17 20:37:29 -0700466 if (req->fmr) {
467 ib_fmr_pool_unmap(req->fmr);
468 req->fmr = NULL;
469 }
470
Roland Dreierd945e1d2006-05-09 10:50:28 -0700471 /*
472 * This handling of non-SG commands can be killed when the
473 * SCSI midlayer no longer generates non-SG commands.
474 */
475 if (likely(scmnd->use_sg)) {
476 nents = scmnd->use_sg;
477 scat = scmnd->request_buffer;
478 } else {
479 nents = 1;
480 scat = &req->fake_sg;
481 }
482
Ralph Campbell85507bc2006-12-12 14:30:55 -0800483 ib_dma_unmap_sg(target->srp_host->dev->dev, scat, nents,
484 scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700485}
486
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700487static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
488{
489 srp_unmap_data(req->scmnd, target, req);
490 list_move_tail(&req->list, &target->free_reqs);
491}
492
493static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
494{
495 req->scmnd->result = DID_RESET << 16;
496 req->scmnd->scsi_done(req->scmnd);
497 srp_remove_req(target, req);
498}
499
Roland Dreieraef9ec32005-11-02 14:07:13 -0800500static int srp_reconnect_target(struct srp_target_port *target)
501{
502 struct ib_cm_id *new_cm_id;
503 struct ib_qp_attr qp_attr;
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700504 struct srp_request *req, *tmp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800505 struct ib_wc wc;
506 int ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800507
508 spin_lock_irq(target->scsi_host->host_lock);
509 if (target->state != SRP_TARGET_LIVE) {
510 spin_unlock_irq(target->scsi_host->host_lock);
511 return -EAGAIN;
512 }
513 target->state = SRP_TARGET_CONNECTING;
514 spin_unlock_irq(target->scsi_host->host_lock);
515
516 srp_disconnect_target(target);
517 /*
518 * Now get a new local CM ID so that we avoid confusing the
519 * target in case things are really fouled up.
520 */
Roland Dreierf5358a12006-06-17 20:37:29 -0700521 new_cm_id = ib_create_cm_id(target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800522 srp_cm_handler, target);
523 if (IS_ERR(new_cm_id)) {
524 ret = PTR_ERR(new_cm_id);
525 goto err;
526 }
527 ib_destroy_cm_id(target->cm_id);
528 target->cm_id = new_cm_id;
529
530 qp_attr.qp_state = IB_QPS_RESET;
531 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
532 if (ret)
533 goto err;
534
535 ret = srp_init_qp(target, target->qp);
536 if (ret)
537 goto err;
538
539 while (ib_poll_cq(target->cq, 1, &wc) > 0)
540 ; /* nothing */
541
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300542 spin_lock_irq(target->scsi_host->host_lock);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700543 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
544 srp_reset_req(target, req);
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300545 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800546
547 target->rx_head = 0;
548 target->tx_head = 0;
549 target->tx_tail = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800550
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200551 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800552 ret = srp_connect_target(target);
553 if (ret)
554 goto err;
555
556 spin_lock_irq(target->scsi_host->host_lock);
557 if (target->state == SRP_TARGET_CONNECTING) {
558 ret = 0;
559 target->state = SRP_TARGET_LIVE;
560 } else
561 ret = -EAGAIN;
562 spin_unlock_irq(target->scsi_host->host_lock);
563
564 return ret;
565
566err:
567 printk(KERN_ERR PFX "reconnect failed (%d), removing target port.\n", ret);
568
569 /*
570 * We couldn't reconnect, so kill our target port off.
571 * However, we have to defer the real removal because we might
572 * be in the context of the SCSI error handler now, which
573 * would deadlock if we call scsi_remove_host().
574 */
575 spin_lock_irq(target->scsi_host->host_lock);
576 if (target->state == SRP_TARGET_CONNECTING) {
577 target->state = SRP_TARGET_DEAD;
David Howellsc4028952006-11-22 14:57:56 +0000578 INIT_WORK(&target->work, srp_remove_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800579 schedule_work(&target->work);
580 }
581 spin_unlock_irq(target->scsi_host->host_lock);
582
583 return ret;
584}
585
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700586static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat,
Roland Dreierf5358a12006-06-17 20:37:29 -0700587 int sg_cnt, struct srp_request *req,
588 struct srp_direct_buf *buf)
589{
590 u64 io_addr = 0;
591 u64 *dma_pages;
592 u32 len;
593 int page_cnt;
594 int i, j;
595 int ret;
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700596 struct srp_device *dev = target->srp_host->dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800597 struct ib_device *ibdev = dev->dev;
Roland Dreierf5358a12006-06-17 20:37:29 -0700598
599 if (!dev->fmr_pool)
600 return -ENODEV;
601
Ralph Campbell85507bc2006-12-12 14:30:55 -0800602 if ((ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask) &&
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700603 mellanox_workarounds && !memcmp(&target->ioc_guid, mellanox_oui, 3))
604 return -EINVAL;
605
Roland Dreierf5358a12006-06-17 20:37:29 -0700606 len = page_cnt = 0;
607 for (i = 0; i < sg_cnt; ++i) {
Ralph Campbell85507bc2006-12-12 14:30:55 -0800608 unsigned int dma_len = ib_sg_dma_len(ibdev, &scat[i]);
609
610 if (ib_sg_dma_address(ibdev, &scat[i]) & ~dev->fmr_page_mask) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700611 if (i > 0)
612 return -EINVAL;
613 else
614 ++page_cnt;
615 }
Ralph Campbell85507bc2006-12-12 14:30:55 -0800616 if ((ib_sg_dma_address(ibdev, &scat[i]) + dma_len) &
Roland Dreierf5358a12006-06-17 20:37:29 -0700617 ~dev->fmr_page_mask) {
618 if (i < sg_cnt - 1)
619 return -EINVAL;
620 else
621 ++page_cnt;
622 }
623
Ralph Campbell85507bc2006-12-12 14:30:55 -0800624 len += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700625 }
626
627 page_cnt += len >> dev->fmr_page_shift;
628 if (page_cnt > SRP_FMR_SIZE)
629 return -ENOMEM;
630
631 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
632 if (!dma_pages)
633 return -ENOMEM;
634
635 page_cnt = 0;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800636 for (i = 0; i < sg_cnt; ++i) {
637 unsigned int dma_len = ib_sg_dma_len(ibdev, &scat[i]);
638
639 for (j = 0; j < dma_len; j += dev->fmr_page_size)
Roland Dreierf5358a12006-06-17 20:37:29 -0700640 dma_pages[page_cnt++] =
Ralph Campbell85507bc2006-12-12 14:30:55 -0800641 (ib_sg_dma_address(ibdev, &scat[i]) &
642 dev->fmr_page_mask) + j;
643 }
Roland Dreierf5358a12006-06-17 20:37:29 -0700644
645 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
Michael S. Tsirkinadfaa882006-07-14 00:23:55 -0700646 dma_pages, page_cnt, io_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -0700647 if (IS_ERR(req->fmr)) {
648 ret = PTR_ERR(req->fmr);
Vu Pham6583eb32006-07-14 00:23:53 -0700649 req->fmr = NULL;
Roland Dreierf5358a12006-06-17 20:37:29 -0700650 goto out;
651 }
652
Ralph Campbell85507bc2006-12-12 14:30:55 -0800653 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) &
654 ~dev->fmr_page_mask);
Roland Dreierf5358a12006-06-17 20:37:29 -0700655 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
656 buf->len = cpu_to_be32(len);
657
658 ret = 0;
659
660out:
661 kfree(dma_pages);
662
663 return ret;
664}
665
Roland Dreieraef9ec32005-11-02 14:07:13 -0800666static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
667 struct srp_request *req)
668{
Roland Dreiercf368712006-03-24 15:47:26 -0800669 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800670 struct srp_cmd *cmd = req->cmd->buf;
Roland Dreiercf368712006-03-24 15:47:26 -0800671 int len, nents, count;
Roland Dreierf5358a12006-06-17 20:37:29 -0700672 u8 fmt = SRP_DATA_DESC_DIRECT;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800673 struct srp_device *dev;
674 struct ib_device *ibdev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800675
676 if (!scmnd->request_buffer || scmnd->sc_data_direction == DMA_NONE)
677 return sizeof (struct srp_cmd);
678
679 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
680 scmnd->sc_data_direction != DMA_TO_DEVICE) {
681 printk(KERN_WARNING PFX "Unhandled data direction %d\n",
682 scmnd->sc_data_direction);
683 return -EINVAL;
684 }
685
Roland Dreiercf368712006-03-24 15:47:26 -0800686 /*
687 * This handling of non-SG commands can be killed when the
688 * SCSI midlayer no longer generates non-SG commands.
689 */
690 if (likely(scmnd->use_sg)) {
691 nents = scmnd->use_sg;
692 scat = scmnd->request_buffer;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800693 } else {
Roland Dreiercf368712006-03-24 15:47:26 -0800694 nents = 1;
695 scat = &req->fake_sg;
696 sg_init_one(scat, scmnd->request_buffer, scmnd->request_bufflen);
697 }
698
Ralph Campbell85507bc2006-12-12 14:30:55 -0800699 dev = target->srp_host->dev;
700 ibdev = dev->dev;
701
702 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
Roland Dreierf5358a12006-06-17 20:37:29 -0700703
704 fmt = SRP_DATA_DESC_DIRECT;
705 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800706
707 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700708 /*
709 * The midlayer only generated a single gather/scatter
710 * entry, or DMA mapping coalesced everything to a
711 * single entry. So a direct descriptor along with
712 * the DMA MR suffices.
713 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800714 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800715
Ralph Campbell85507bc2006-12-12 14:30:55 -0800716 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
717 buf->key = cpu_to_be32(dev->mr->rkey);
718 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700719 } else if (srp_map_fmr(target, scat, count, req,
Roland Dreierf5358a12006-06-17 20:37:29 -0700720 (void *) cmd->add_data)) {
721 /*
722 * FMR mapping failed, and the scatterlist has more
723 * than one entry. Generate an indirect memory
724 * descriptor.
725 */
Roland Dreiercf368712006-03-24 15:47:26 -0800726 struct srp_indirect_buf *buf = (void *) cmd->add_data;
727 u32 datalen = 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700728 int i;
Roland Dreiercf368712006-03-24 15:47:26 -0800729
730 fmt = SRP_DATA_DESC_INDIRECT;
Roland Dreierf5358a12006-06-17 20:37:29 -0700731 len = sizeof (struct srp_cmd) +
732 sizeof (struct srp_indirect_buf) +
733 count * sizeof (struct srp_direct_buf);
734
735 for (i = 0; i < count; ++i) {
Ralph Campbell85507bc2006-12-12 14:30:55 -0800736 unsigned int dma_len = ib_sg_dma_len(ibdev, &scat[i]);
737
Roland Dreierf5358a12006-06-17 20:37:29 -0700738 buf->desc_list[i].va =
Ralph Campbell85507bc2006-12-12 14:30:55 -0800739 cpu_to_be64(ib_sg_dma_address(ibdev, &scat[i]));
Roland Dreierf5358a12006-06-17 20:37:29 -0700740 buf->desc_list[i].key =
Ralph Campbell85507bc2006-12-12 14:30:55 -0800741 cpu_to_be32(dev->mr->rkey);
742 buf->desc_list[i].len = cpu_to_be32(dma_len);
743 datalen += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700744 }
Roland Dreiercf368712006-03-24 15:47:26 -0800745
746 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
747 cmd->data_out_desc_cnt = count;
748 else
749 cmd->data_in_desc_cnt = count;
750
Roland Dreierf5358a12006-06-17 20:37:29 -0700751 buf->table_desc.va =
752 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800753 buf->table_desc.key =
Roland Dreierf5358a12006-06-17 20:37:29 -0700754 cpu_to_be32(target->srp_host->dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800755 buf->table_desc.len =
756 cpu_to_be32(count * sizeof (struct srp_direct_buf));
757
Roland Dreiercf368712006-03-24 15:47:26 -0800758 buf->len = cpu_to_be32(datalen);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800759 }
760
761 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
762 cmd->buf_fmt = fmt << 4;
763 else
764 cmd->buf_fmt = fmt;
765
Roland Dreieraef9ec32005-11-02 14:07:13 -0800766 return len;
767}
768
Roland Dreieraef9ec32005-11-02 14:07:13 -0800769static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
770{
771 struct srp_request *req;
772 struct scsi_cmnd *scmnd;
773 unsigned long flags;
774 s32 delta;
775
776 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
777
778 spin_lock_irqsave(target->scsi_host->host_lock, flags);
779
780 target->req_lim += delta;
781
782 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
783
784 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
785 if (be32_to_cpu(rsp->resp_data_len) < 4)
786 req->tsk_status = -1;
787 else
788 req->tsk_status = rsp->data[3];
789 complete(&req->done);
790 } else {
Roland Dreierd945e1d2006-05-09 10:50:28 -0700791 scmnd = req->scmnd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800792 if (!scmnd)
793 printk(KERN_ERR "Null scmnd for RSP w/tag %016llx\n",
794 (unsigned long long) rsp->tag);
795 scmnd->result = rsp->status;
796
797 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
798 memcpy(scmnd->sense_buffer, rsp->data +
799 be32_to_cpu(rsp->resp_data_len),
800 min_t(int, be32_to_cpu(rsp->sense_data_len),
801 SCSI_SENSE_BUFFERSIZE));
802 }
803
804 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
805 scmnd->resid = be32_to_cpu(rsp->data_out_res_cnt);
806 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
807 scmnd->resid = be32_to_cpu(rsp->data_in_res_cnt);
808
Roland Dreieraef9ec32005-11-02 14:07:13 -0800809 if (!req->tsk_mgmt) {
Roland Dreieraef9ec32005-11-02 14:07:13 -0800810 scmnd->host_scribble = (void *) -1L;
811 scmnd->scsi_done(scmnd);
812
Roland Dreierd945e1d2006-05-09 10:50:28 -0700813 srp_remove_req(target, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800814 } else
815 req->cmd_done = 1;
816 }
817
818 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
819}
820
Roland Dreieraef9ec32005-11-02 14:07:13 -0800821static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
822{
Ralph Campbell85507bc2006-12-12 14:30:55 -0800823 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800824 struct srp_iu *iu;
825 u8 opcode;
826
827 iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
828
Ralph Campbell85507bc2006-12-12 14:30:55 -0800829 dev = target->srp_host->dev->dev;
830 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
831 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800832
833 opcode = *(u8 *) iu->buf;
834
835 if (0) {
836 int i;
837
838 printk(KERN_ERR PFX "recv completion, opcode 0x%02x\n", opcode);
839
840 for (i = 0; i < wc->byte_len; ++i) {
841 if (i % 8 == 0)
842 printk(KERN_ERR " [%02x] ", i);
843 printk(" %02x", ((u8 *) iu->buf)[i]);
844 if ((i + 1) % 8 == 0)
845 printk("\n");
846 }
847
848 if (wc->byte_len % 8)
849 printk("\n");
850 }
851
852 switch (opcode) {
853 case SRP_RSP:
854 srp_process_rsp(target, iu->buf);
855 break;
856
857 case SRP_T_LOGOUT:
858 /* XXX Handle target logout */
859 printk(KERN_WARNING PFX "Got target logout request\n");
860 break;
861
862 default:
863 printk(KERN_WARNING PFX "Unhandled SRP opcode 0x%02x\n", opcode);
864 break;
865 }
866
Ralph Campbell85507bc2006-12-12 14:30:55 -0800867 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
868 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800869}
870
871static void srp_completion(struct ib_cq *cq, void *target_ptr)
872{
873 struct srp_target_port *target = target_ptr;
874 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800875
876 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
877 while (ib_poll_cq(cq, 1, &wc) > 0) {
878 if (wc.status) {
879 printk(KERN_ERR PFX "failed %s status %d\n",
880 wc.wr_id & SRP_OP_RECV ? "receive" : "send",
881 wc.status);
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200882 target->qp_in_error = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800883 break;
884 }
885
886 if (wc.wr_id & SRP_OP_RECV)
887 srp_handle_recv(target, &wc);
888 else
889 ++target->tx_tail;
890 }
891}
892
893static int __srp_post_recv(struct srp_target_port *target)
894{
895 struct srp_iu *iu;
896 struct ib_sge list;
897 struct ib_recv_wr wr, *bad_wr;
898 unsigned int next;
899 int ret;
900
901 next = target->rx_head & (SRP_RQ_SIZE - 1);
902 wr.wr_id = next | SRP_OP_RECV;
903 iu = target->rx_ring[next];
904
905 list.addr = iu->dma;
906 list.length = iu->size;
Roland Dreierf5358a12006-06-17 20:37:29 -0700907 list.lkey = target->srp_host->dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800908
909 wr.next = NULL;
910 wr.sg_list = &list;
911 wr.num_sge = 1;
912
913 ret = ib_post_recv(target->qp, &wr, &bad_wr);
914 if (!ret)
915 ++target->rx_head;
916
917 return ret;
918}
919
920static int srp_post_recv(struct srp_target_port *target)
921{
922 unsigned long flags;
923 int ret;
924
925 spin_lock_irqsave(target->scsi_host->host_lock, flags);
926 ret = __srp_post_recv(target);
927 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
928
929 return ret;
930}
931
932/*
933 * Must be called with target->scsi_host->host_lock held to protect
Roland Dreier47f2bce2005-11-15 00:19:21 -0800934 * req_lim and tx_head. Lock cannot be dropped between call here and
935 * call to __srp_post_send().
Roland Dreieraef9ec32005-11-02 14:07:13 -0800936 */
937static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target)
938{
939 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
940 return NULL;
941
Roland Dreier6bfa24f2006-06-17 20:37:33 -0700942 if (unlikely(target->req_lim < 1))
943 ++target->zero_req_lim;
Roland Dreier47f2bce2005-11-15 00:19:21 -0800944
Roland Dreieraef9ec32005-11-02 14:07:13 -0800945 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
946}
947
948/*
949 * Must be called with target->scsi_host->host_lock held to protect
950 * req_lim and tx_head.
951 */
952static int __srp_post_send(struct srp_target_port *target,
953 struct srp_iu *iu, int len)
954{
955 struct ib_sge list;
956 struct ib_send_wr wr, *bad_wr;
957 int ret = 0;
958
Roland Dreieraef9ec32005-11-02 14:07:13 -0800959 list.addr = iu->dma;
960 list.length = len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700961 list.lkey = target->srp_host->dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800962
963 wr.next = NULL;
964 wr.wr_id = target->tx_head & SRP_SQ_SIZE;
965 wr.sg_list = &list;
966 wr.num_sge = 1;
967 wr.opcode = IB_WR_SEND;
968 wr.send_flags = IB_SEND_SIGNALED;
969
970 ret = ib_post_send(target->qp, &wr, &bad_wr);
971
972 if (!ret) {
973 ++target->tx_head;
974 --target->req_lim;
975 }
976
977 return ret;
978}
979
980static int srp_queuecommand(struct scsi_cmnd *scmnd,
981 void (*done)(struct scsi_cmnd *))
982{
983 struct srp_target_port *target = host_to_target(scmnd->device->host);
984 struct srp_request *req;
985 struct srp_iu *iu;
986 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800987 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800988 int len;
989
990 if (target->state == SRP_TARGET_CONNECTING)
991 goto err;
992
993 if (target->state == SRP_TARGET_DEAD ||
994 target->state == SRP_TARGET_REMOVED) {
995 scmnd->result = DID_BAD_TARGET << 16;
996 done(scmnd);
997 return 0;
998 }
999
1000 iu = __srp_get_tx_iu(target);
1001 if (!iu)
1002 goto err;
1003
Ralph Campbell85507bc2006-12-12 14:30:55 -08001004 dev = target->srp_host->dev->dev;
1005 ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len,
1006 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001007
Roland Dreierd945e1d2006-05-09 10:50:28 -07001008 req = list_entry(target->free_reqs.next, struct srp_request, list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001009
1010 scmnd->scsi_done = done;
1011 scmnd->result = 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001012 scmnd->host_scribble = (void *) (long) req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001013
1014 cmd = iu->buf;
1015 memset(cmd, 0, sizeof *cmd);
1016
1017 cmd->opcode = SRP_CMD;
1018 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001019 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001020 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1021
Roland Dreieraef9ec32005-11-02 14:07:13 -08001022 req->scmnd = scmnd;
1023 req->cmd = iu;
1024 req->cmd_done = 0;
1025 req->tsk_mgmt = NULL;
1026
1027 len = srp_map_data(scmnd, target, req);
1028 if (len < 0) {
1029 printk(KERN_ERR PFX "Failed to map data\n");
1030 goto err;
1031 }
1032
1033 if (__srp_post_recv(target)) {
1034 printk(KERN_ERR PFX "Recv failed\n");
1035 goto err_unmap;
1036 }
1037
Ralph Campbell85507bc2006-12-12 14:30:55 -08001038 ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len,
1039 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001040
1041 if (__srp_post_send(target, iu, len)) {
1042 printk(KERN_ERR PFX "Send failed\n");
1043 goto err_unmap;
1044 }
1045
Roland Dreierd945e1d2006-05-09 10:50:28 -07001046 list_move_tail(&req->list, &target->req_queue);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001047
1048 return 0;
1049
1050err_unmap:
1051 srp_unmap_data(scmnd, target, req);
1052
1053err:
1054 return SCSI_MLQUEUE_HOST_BUSY;
1055}
1056
1057static int srp_alloc_iu_bufs(struct srp_target_port *target)
1058{
1059 int i;
1060
1061 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1062 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1063 target->max_ti_iu_len,
1064 GFP_KERNEL, DMA_FROM_DEVICE);
1065 if (!target->rx_ring[i])
1066 goto err;
1067 }
1068
1069 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1070 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
Vu Pham74b0a152006-06-17 20:37:32 -07001071 srp_max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001072 GFP_KERNEL, DMA_TO_DEVICE);
1073 if (!target->tx_ring[i])
1074 goto err;
1075 }
1076
1077 return 0;
1078
1079err:
1080 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1081 srp_free_iu(target->srp_host, target->rx_ring[i]);
1082 target->rx_ring[i] = NULL;
1083 }
1084
1085 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1086 srp_free_iu(target->srp_host, target->tx_ring[i]);
1087 target->tx_ring[i] = NULL;
1088 }
1089
1090 return -ENOMEM;
1091}
1092
1093static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1094 struct ib_cm_event *event,
1095 struct srp_target_port *target)
1096{
1097 struct ib_class_port_info *cpi;
1098 int opcode;
1099
1100 switch (event->param.rej_rcvd.reason) {
1101 case IB_CM_REJ_PORT_CM_REDIRECT:
1102 cpi = event->param.rej_rcvd.ari;
1103 target->path.dlid = cpi->redirect_lid;
1104 target->path.pkey = cpi->redirect_pkey;
1105 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1106 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1107
1108 target->status = target->path.dlid ?
1109 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1110 break;
1111
1112 case IB_CM_REJ_PORT_REDIRECT:
1113 if (topspin_workarounds &&
1114 !memcmp(&target->ioc_guid, topspin_oui, 3)) {
1115 /*
1116 * Topspin/Cisco SRP gateways incorrectly send
1117 * reject reason code 25 when they mean 24
1118 * (port redirect).
1119 */
1120 memcpy(target->path.dgid.raw,
1121 event->param.rej_rcvd.ari, 16);
1122
1123 printk(KERN_DEBUG PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1124 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1125 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1126
1127 target->status = SRP_PORT_REDIRECT;
1128 } else {
1129 printk(KERN_WARNING " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1130 target->status = -ECONNRESET;
1131 }
1132 break;
1133
1134 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
1135 printk(KERN_WARNING " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1136 target->status = -ECONNRESET;
1137 break;
1138
1139 case IB_CM_REJ_CONSUMER_DEFINED:
1140 opcode = *(u8 *) event->private_data;
1141 if (opcode == SRP_LOGIN_REJ) {
1142 struct srp_login_rej *rej = event->private_data;
1143 u32 reason = be32_to_cpu(rej->reason);
1144
1145 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
1146 printk(KERN_WARNING PFX
1147 "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1148 else
1149 printk(KERN_WARNING PFX
1150 "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
1151 } else
1152 printk(KERN_WARNING " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1153 " opcode 0x%02x\n", opcode);
1154 target->status = -ECONNRESET;
1155 break;
1156
1157 default:
1158 printk(KERN_WARNING " REJ reason 0x%x\n",
1159 event->param.rej_rcvd.reason);
1160 target->status = -ECONNRESET;
1161 }
1162}
1163
1164static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1165{
1166 struct srp_target_port *target = cm_id->context;
1167 struct ib_qp_attr *qp_attr = NULL;
1168 int attr_mask = 0;
1169 int comp = 0;
1170 int opcode = 0;
1171
1172 switch (event->event) {
1173 case IB_CM_REQ_ERROR:
1174 printk(KERN_DEBUG PFX "Sending CM REQ failed\n");
1175 comp = 1;
1176 target->status = -ECONNRESET;
1177 break;
1178
1179 case IB_CM_REP_RECEIVED:
1180 comp = 1;
1181 opcode = *(u8 *) event->private_data;
1182
1183 if (opcode == SRP_LOGIN_RSP) {
1184 struct srp_login_rsp *rsp = event->private_data;
1185
1186 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1187 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1188
1189 target->scsi_host->can_queue = min(target->req_lim,
1190 target->scsi_host->can_queue);
1191 } else {
1192 printk(KERN_WARNING PFX "Unhandled RSP opcode %#x\n", opcode);
1193 target->status = -ECONNRESET;
1194 break;
1195 }
1196
Vu Phamd2fcea72006-11-21 14:14:10 -08001197 if (!target->rx_ring[0]) {
1198 target->status = srp_alloc_iu_bufs(target);
1199 if (target->status)
1200 break;
1201 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001202
1203 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1204 if (!qp_attr) {
1205 target->status = -ENOMEM;
1206 break;
1207 }
1208
1209 qp_attr->qp_state = IB_QPS_RTR;
1210 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1211 if (target->status)
1212 break;
1213
1214 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1215 if (target->status)
1216 break;
1217
1218 target->status = srp_post_recv(target);
1219 if (target->status)
1220 break;
1221
1222 qp_attr->qp_state = IB_QPS_RTS;
1223 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1224 if (target->status)
1225 break;
1226
1227 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1228 if (target->status)
1229 break;
1230
1231 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1232 if (target->status)
1233 break;
1234
1235 break;
1236
1237 case IB_CM_REJ_RECEIVED:
1238 printk(KERN_DEBUG PFX "REJ received\n");
1239 comp = 1;
1240
1241 srp_cm_rej_handler(cm_id, event, target);
1242 break;
1243
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001244 case IB_CM_DREQ_RECEIVED:
1245 printk(KERN_WARNING PFX "DREQ received - connection closed\n");
1246 if (ib_send_cm_drep(cm_id, NULL, 0))
1247 printk(KERN_ERR PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001248 break;
1249
1250 case IB_CM_TIMEWAIT_EXIT:
1251 printk(KERN_ERR PFX "connection closed\n");
1252
1253 comp = 1;
1254 target->status = 0;
1255 break;
1256
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001257 case IB_CM_MRA_RECEIVED:
1258 case IB_CM_DREQ_ERROR:
1259 case IB_CM_DREP_RECEIVED:
1260 break;
1261
Roland Dreieraef9ec32005-11-02 14:07:13 -08001262 default:
1263 printk(KERN_WARNING PFX "Unhandled CM event %d\n", event->event);
1264 break;
1265 }
1266
1267 if (comp)
1268 complete(&target->done);
1269
1270 kfree(qp_attr);
1271
1272 return 0;
1273}
1274
Roland Dreierd945e1d2006-05-09 10:50:28 -07001275static int srp_send_tsk_mgmt(struct srp_target_port *target,
1276 struct srp_request *req, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001277{
Roland Dreieraef9ec32005-11-02 14:07:13 -08001278 struct srp_iu *iu;
1279 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001280
1281 spin_lock_irq(target->scsi_host->host_lock);
1282
Roland Dreier1285b3a2006-03-03 15:47:25 -08001283 if (target->state == SRP_TARGET_DEAD ||
1284 target->state == SRP_TARGET_REMOVED) {
Roland Dreierd945e1d2006-05-09 10:50:28 -07001285 req->scmnd->result = DID_BAD_TARGET << 16;
Roland Dreier1285b3a2006-03-03 15:47:25 -08001286 goto out;
1287 }
1288
Roland Dreieraef9ec32005-11-02 14:07:13 -08001289 init_completion(&req->done);
1290
1291 iu = __srp_get_tx_iu(target);
1292 if (!iu)
1293 goto out;
1294
1295 tsk_mgmt = iu->buf;
1296 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1297
1298 tsk_mgmt->opcode = SRP_TSK_MGMT;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001299 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1300 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001301 tsk_mgmt->tsk_mgmt_func = func;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001302 tsk_mgmt->task_tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001303
1304 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1305 goto out;
1306
1307 req->tsk_mgmt = iu;
1308
1309 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001310
Roland Dreieraef9ec32005-11-02 14:07:13 -08001311 if (!wait_for_completion_timeout(&req->done,
1312 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001313 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001314
Roland Dreierd945e1d2006-05-09 10:50:28 -07001315 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001316
1317out:
1318 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001319 return -1;
1320}
1321
1322static int srp_find_req(struct srp_target_port *target,
1323 struct scsi_cmnd *scmnd,
1324 struct srp_request **req)
1325{
1326 if (scmnd->host_scribble == (void *) -1L)
1327 return -1;
1328
1329 *req = &target->req_ring[(long) scmnd->host_scribble];
1330
1331 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001332}
1333
1334static int srp_abort(struct scsi_cmnd *scmnd)
1335{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001336 struct srp_target_port *target = host_to_target(scmnd->device->host);
1337 struct srp_request *req;
1338 int ret = SUCCESS;
1339
Roland Dreieraef9ec32005-11-02 14:07:13 -08001340 printk(KERN_ERR "SRP abort called\n");
1341
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001342 if (target->qp_in_error)
1343 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001344 if (srp_find_req(target, scmnd, &req))
1345 return FAILED;
1346 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1347 return FAILED;
1348
1349 spin_lock_irq(target->scsi_host->host_lock);
1350
1351 if (req->cmd_done) {
1352 srp_remove_req(target, req);
1353 scmnd->scsi_done(scmnd);
1354 } else if (!req->tsk_status) {
1355 srp_remove_req(target, req);
1356 scmnd->result = DID_ABORT << 16;
1357 } else
1358 ret = FAILED;
1359
1360 spin_unlock_irq(target->scsi_host->host_lock);
1361
1362 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001363}
1364
1365static int srp_reset_device(struct scsi_cmnd *scmnd)
1366{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001367 struct srp_target_port *target = host_to_target(scmnd->device->host);
1368 struct srp_request *req, *tmp;
1369
Roland Dreieraef9ec32005-11-02 14:07:13 -08001370 printk(KERN_ERR "SRP reset_device called\n");
1371
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001372 if (target->qp_in_error)
1373 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001374 if (srp_find_req(target, scmnd, &req))
1375 return FAILED;
1376 if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1377 return FAILED;
1378 if (req->tsk_status)
1379 return FAILED;
1380
1381 spin_lock_irq(target->scsi_host->host_lock);
1382
1383 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001384 if (req->scmnd->device == scmnd->device)
1385 srp_reset_req(target, req);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001386
1387 spin_unlock_irq(target->scsi_host->host_lock);
1388
1389 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001390}
1391
1392static int srp_reset_host(struct scsi_cmnd *scmnd)
1393{
1394 struct srp_target_port *target = host_to_target(scmnd->device->host);
1395 int ret = FAILED;
1396
1397 printk(KERN_ERR PFX "SRP reset_host called\n");
1398
1399 if (!srp_reconnect_target(target))
1400 ret = SUCCESS;
1401
1402 return ret;
1403}
1404
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001405static ssize_t show_id_ext(struct class_device *cdev, char *buf)
1406{
1407 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1408
1409 if (target->state == SRP_TARGET_DEAD ||
1410 target->state == SRP_TARGET_REMOVED)
1411 return -ENODEV;
1412
1413 return sprintf(buf, "0x%016llx\n",
1414 (unsigned long long) be64_to_cpu(target->id_ext));
1415}
1416
1417static ssize_t show_ioc_guid(struct class_device *cdev, char *buf)
1418{
1419 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1420
1421 if (target->state == SRP_TARGET_DEAD ||
1422 target->state == SRP_TARGET_REMOVED)
1423 return -ENODEV;
1424
1425 return sprintf(buf, "0x%016llx\n",
1426 (unsigned long long) be64_to_cpu(target->ioc_guid));
1427}
1428
1429static ssize_t show_service_id(struct class_device *cdev, char *buf)
1430{
1431 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1432
1433 if (target->state == SRP_TARGET_DEAD ||
1434 target->state == SRP_TARGET_REMOVED)
1435 return -ENODEV;
1436
1437 return sprintf(buf, "0x%016llx\n",
1438 (unsigned long long) be64_to_cpu(target->service_id));
1439}
1440
1441static ssize_t show_pkey(struct class_device *cdev, char *buf)
1442{
1443 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1444
1445 if (target->state == SRP_TARGET_DEAD ||
1446 target->state == SRP_TARGET_REMOVED)
1447 return -ENODEV;
1448
1449 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1450}
1451
1452static ssize_t show_dgid(struct class_device *cdev, char *buf)
1453{
1454 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1455
1456 if (target->state == SRP_TARGET_DEAD ||
1457 target->state == SRP_TARGET_REMOVED)
1458 return -ENODEV;
1459
1460 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1461 be16_to_cpu(((__be16 *) target->path.dgid.raw)[0]),
1462 be16_to_cpu(((__be16 *) target->path.dgid.raw)[1]),
1463 be16_to_cpu(((__be16 *) target->path.dgid.raw)[2]),
1464 be16_to_cpu(((__be16 *) target->path.dgid.raw)[3]),
1465 be16_to_cpu(((__be16 *) target->path.dgid.raw)[4]),
1466 be16_to_cpu(((__be16 *) target->path.dgid.raw)[5]),
1467 be16_to_cpu(((__be16 *) target->path.dgid.raw)[6]),
1468 be16_to_cpu(((__be16 *) target->path.dgid.raw)[7]));
1469}
1470
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001471static ssize_t show_zero_req_lim(struct class_device *cdev, char *buf)
1472{
1473 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1474
1475 if (target->state == SRP_TARGET_DEAD ||
1476 target->state == SRP_TARGET_REMOVED)
1477 return -ENODEV;
1478
1479 return sprintf(buf, "%d\n", target->zero_req_lim);
1480}
1481
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001482static ssize_t show_local_ib_port(struct class_device *cdev, char *buf)
1483{
1484 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1485
1486 return sprintf(buf, "%d\n", target->srp_host->port);
1487}
1488
1489static ssize_t show_local_ib_device(struct class_device *cdev, char *buf)
1490{
1491 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1492
1493 return sprintf(buf, "%s\n", target->srp_host->dev->dev->name);
1494}
1495
1496static CLASS_DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1497static CLASS_DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1498static CLASS_DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1499static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1500static CLASS_DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1501static CLASS_DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1502static CLASS_DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1503static CLASS_DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001504
1505static struct class_device_attribute *srp_host_attrs[] = {
1506 &class_device_attr_id_ext,
1507 &class_device_attr_ioc_guid,
1508 &class_device_attr_service_id,
1509 &class_device_attr_pkey,
1510 &class_device_attr_dgid,
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001511 &class_device_attr_zero_req_lim,
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001512 &class_device_attr_local_ib_port,
1513 &class_device_attr_local_ib_device,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001514 NULL
1515};
1516
Roland Dreieraef9ec32005-11-02 14:07:13 -08001517static struct scsi_host_template srp_template = {
1518 .module = THIS_MODULE,
1519 .name = DRV_NAME,
1520 .info = srp_target_info,
1521 .queuecommand = srp_queuecommand,
1522 .eh_abort_handler = srp_abort,
1523 .eh_device_reset_handler = srp_reset_device,
1524 .eh_host_reset_handler = srp_reset_host,
1525 .can_queue = SRP_SQ_SIZE,
1526 .this_id = -1,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001527 .cmd_per_lun = SRP_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001528 .use_clustering = ENABLE_CLUSTERING,
1529 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001530};
1531
1532static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1533{
1534 sprintf(target->target_name, "SRP.T10:%016llX",
1535 (unsigned long long) be64_to_cpu(target->id_ext));
1536
Roland Dreierf5358a12006-06-17 20:37:29 -07001537 if (scsi_add_host(target->scsi_host, host->dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001538 return -ENODEV;
1539
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001540 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001541 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001542 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001543
1544 target->state = SRP_TARGET_LIVE;
1545
Roland Dreieraef9ec32005-11-02 14:07:13 -08001546 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07001547 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001548
1549 return 0;
1550}
1551
1552static void srp_release_class_dev(struct class_device *class_dev)
1553{
1554 struct srp_host *host =
1555 container_of(class_dev, struct srp_host, class_dev);
1556
1557 complete(&host->released);
1558}
1559
1560static struct class srp_class = {
1561 .name = "infiniband_srp",
1562 .release = srp_release_class_dev
1563};
1564
1565/*
1566 * Target ports are added by writing
1567 *
1568 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1569 * pkey=<P_Key>,service_id=<service ID>
1570 *
1571 * to the add_target sysfs attribute.
1572 */
1573enum {
1574 SRP_OPT_ERR = 0,
1575 SRP_OPT_ID_EXT = 1 << 0,
1576 SRP_OPT_IOC_GUID = 1 << 1,
1577 SRP_OPT_DGID = 1 << 2,
1578 SRP_OPT_PKEY = 1 << 3,
1579 SRP_OPT_SERVICE_ID = 1 << 4,
1580 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07001581 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07001582 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001583 SRP_OPT_INITIATOR_EXT = 1 << 8,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001584 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1585 SRP_OPT_IOC_GUID |
1586 SRP_OPT_DGID |
1587 SRP_OPT_PKEY |
1588 SRP_OPT_SERVICE_ID),
1589};
1590
1591static match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07001592 { SRP_OPT_ID_EXT, "id_ext=%s" },
1593 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1594 { SRP_OPT_DGID, "dgid=%s" },
1595 { SRP_OPT_PKEY, "pkey=%x" },
1596 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1597 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1598 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07001599 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001600 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
Vu Pham52fb2b502006-06-17 20:37:31 -07001601 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001602};
1603
1604static int srp_parse_options(const char *buf, struct srp_target_port *target)
1605{
1606 char *options, *sep_opt;
1607 char *p;
1608 char dgid[3];
1609 substring_t args[MAX_OPT_ARGS];
1610 int opt_mask = 0;
1611 int token;
1612 int ret = -EINVAL;
1613 int i;
1614
1615 options = kstrdup(buf, GFP_KERNEL);
1616 if (!options)
1617 return -ENOMEM;
1618
1619 sep_opt = options;
1620 while ((p = strsep(&sep_opt, ",")) != NULL) {
1621 if (!*p)
1622 continue;
1623
1624 token = match_token(p, srp_opt_tokens, args);
1625 opt_mask |= token;
1626
1627 switch (token) {
1628 case SRP_OPT_ID_EXT:
1629 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001630 if (!p) {
1631 ret = -ENOMEM;
1632 goto out;
1633 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001634 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1635 kfree(p);
1636 break;
1637
1638 case SRP_OPT_IOC_GUID:
1639 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001640 if (!p) {
1641 ret = -ENOMEM;
1642 goto out;
1643 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001644 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1645 kfree(p);
1646 break;
1647
1648 case SRP_OPT_DGID:
1649 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001650 if (!p) {
1651 ret = -ENOMEM;
1652 goto out;
1653 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001654 if (strlen(p) != 32) {
1655 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07001656 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001657 goto out;
1658 }
1659
1660 for (i = 0; i < 16; ++i) {
1661 strlcpy(dgid, p + i * 2, 3);
1662 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1663 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08001664 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001665 break;
1666
1667 case SRP_OPT_PKEY:
1668 if (match_hex(args, &token)) {
1669 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1670 goto out;
1671 }
1672 target->path.pkey = cpu_to_be16(token);
1673 break;
1674
1675 case SRP_OPT_SERVICE_ID:
1676 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001677 if (!p) {
1678 ret = -ENOMEM;
1679 goto out;
1680 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001681 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
1682 kfree(p);
1683 break;
1684
1685 case SRP_OPT_MAX_SECT:
1686 if (match_int(args, &token)) {
1687 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1688 goto out;
1689 }
1690 target->scsi_host->max_sectors = token;
1691 break;
1692
Vu Pham52fb2b502006-06-17 20:37:31 -07001693 case SRP_OPT_MAX_CMD_PER_LUN:
1694 if (match_int(args, &token)) {
1695 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1696 goto out;
1697 }
1698 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1699 break;
1700
Ramachandra K0c0450db2006-06-17 20:37:38 -07001701 case SRP_OPT_IO_CLASS:
1702 if (match_hex(args, &token)) {
1703 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1704 goto out;
1705 }
1706 if (token != SRP_REV10_IB_IO_CLASS &&
1707 token != SRP_REV16A_IB_IO_CLASS) {
1708 printk(KERN_WARNING PFX "unknown IO class parameter value"
1709 " %x specified (use %x or %x).\n",
1710 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1711 goto out;
1712 }
1713 target->io_class = token;
1714 break;
1715
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001716 case SRP_OPT_INITIATOR_EXT:
1717 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001718 if (!p) {
1719 ret = -ENOMEM;
1720 goto out;
1721 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001722 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1723 kfree(p);
1724 break;
1725
Roland Dreieraef9ec32005-11-02 14:07:13 -08001726 default:
1727 printk(KERN_WARNING PFX "unknown parameter or missing value "
1728 "'%s' in target creation request\n", p);
1729 goto out;
1730 }
1731 }
1732
1733 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1734 ret = 0;
1735 else
1736 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1737 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1738 !(srp_opt_tokens[i].token & opt_mask))
1739 printk(KERN_WARNING PFX "target creation request is "
1740 "missing parameter '%s'\n",
1741 srp_opt_tokens[i].pattern);
1742
1743out:
1744 kfree(options);
1745 return ret;
1746}
1747
1748static ssize_t srp_create_target(struct class_device *class_dev,
1749 const char *buf, size_t count)
1750{
1751 struct srp_host *host =
1752 container_of(class_dev, struct srp_host, class_dev);
1753 struct Scsi_Host *target_host;
1754 struct srp_target_port *target;
1755 int ret;
1756 int i;
1757
1758 target_host = scsi_host_alloc(&srp_template,
1759 sizeof (struct srp_target_port));
1760 if (!target_host)
1761 return -ENOMEM;
1762
Arne Redlich3c8edf02006-11-15 12:43:00 +01001763 target_host->max_lun = SRP_MAX_LUN;
1764 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08001765
Roland Dreieraef9ec32005-11-02 14:07:13 -08001766 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001767
Ramachandra K0c0450db2006-06-17 20:37:38 -07001768 target->io_class = SRP_REV16A_IB_IO_CLASS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001769 target->scsi_host = target_host;
1770 target->srp_host = host;
1771
Roland Dreierd945e1d2006-05-09 10:50:28 -07001772 INIT_LIST_HEAD(&target->free_reqs);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001773 INIT_LIST_HEAD(&target->req_queue);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001774 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1775 target->req_ring[i].index = i;
1776 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1777 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001778
1779 ret = srp_parse_options(buf, target);
1780 if (ret)
1781 goto err;
1782
Roland Dreierf5358a12006-06-17 20:37:29 -07001783 ib_get_cached_gid(host->dev->dev, host->port, 0, &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001784
1785 printk(KERN_DEBUG PFX "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1786 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1787 (unsigned long long) be64_to_cpu(target->id_ext),
1788 (unsigned long long) be64_to_cpu(target->ioc_guid),
1789 be16_to_cpu(target->path.pkey),
1790 (unsigned long long) be64_to_cpu(target->service_id),
1791 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]),
1792 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]),
1793 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]),
1794 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]),
1795 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]),
1796 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]),
1797 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]),
1798 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14]));
1799
1800 ret = srp_create_target_ib(target);
1801 if (ret)
1802 goto err;
1803
Roland Dreierf5358a12006-06-17 20:37:29 -07001804 target->cm_id = ib_create_cm_id(host->dev->dev, srp_cm_handler, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001805 if (IS_ERR(target->cm_id)) {
1806 ret = PTR_ERR(target->cm_id);
1807 goto err_free;
1808 }
1809
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001810 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001811 ret = srp_connect_target(target);
1812 if (ret) {
1813 printk(KERN_ERR PFX "Connection failed\n");
1814 goto err_cm_id;
1815 }
1816
1817 ret = srp_add_target(host, target);
1818 if (ret)
1819 goto err_disconnect;
1820
1821 return count;
1822
1823err_disconnect:
1824 srp_disconnect_target(target);
1825
1826err_cm_id:
1827 ib_destroy_cm_id(target->cm_id);
1828
1829err_free:
1830 srp_free_target_ib(target);
1831
1832err:
1833 scsi_host_put(target_host);
1834
1835 return ret;
1836}
1837
1838static CLASS_DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
1839
1840static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
1841{
1842 struct srp_host *host =
1843 container_of(class_dev, struct srp_host, class_dev);
1844
Roland Dreierf5358a12006-06-17 20:37:29 -07001845 return sprintf(buf, "%s\n", host->dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001846}
1847
1848static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1849
1850static ssize_t show_port(struct class_device *class_dev, char *buf)
1851{
1852 struct srp_host *host =
1853 container_of(class_dev, struct srp_host, class_dev);
1854
1855 return sprintf(buf, "%d\n", host->port);
1856}
1857
1858static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
1859
Roland Dreierf5358a12006-06-17 20:37:29 -07001860static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001861{
1862 struct srp_host *host;
1863
1864 host = kzalloc(sizeof *host, GFP_KERNEL);
1865 if (!host)
1866 return NULL;
1867
1868 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001869 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001870 init_completion(&host->released);
1871 host->dev = device;
1872 host->port = port;
1873
Roland Dreieraef9ec32005-11-02 14:07:13 -08001874 host->class_dev.class = &srp_class;
Roland Dreierf5358a12006-06-17 20:37:29 -07001875 host->class_dev.dev = device->dev->dma_device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001876 snprintf(host->class_dev.class_id, BUS_ID_SIZE, "srp-%s-%d",
Roland Dreierf5358a12006-06-17 20:37:29 -07001877 device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001878
1879 if (class_device_register(&host->class_dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07001880 goto free_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001881 if (class_device_create_file(&host->class_dev, &class_device_attr_add_target))
1882 goto err_class;
1883 if (class_device_create_file(&host->class_dev, &class_device_attr_ibdev))
1884 goto err_class;
1885 if (class_device_create_file(&host->class_dev, &class_device_attr_port))
1886 goto err_class;
1887
1888 return host;
1889
1890err_class:
1891 class_device_unregister(&host->class_dev);
1892
Roland Dreierf5358a12006-06-17 20:37:29 -07001893free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001894 kfree(host);
1895
1896 return NULL;
1897}
1898
1899static void srp_add_one(struct ib_device *device)
1900{
Roland Dreierf5358a12006-06-17 20:37:29 -07001901 struct srp_device *srp_dev;
1902 struct ib_device_attr *dev_attr;
1903 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001904 struct srp_host *host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001905 int s, e, p;
1906
Roland Dreierf5358a12006-06-17 20:37:29 -07001907 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
1908 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08001909 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001910
Roland Dreierf5358a12006-06-17 20:37:29 -07001911 if (ib_query_device(device, dev_attr)) {
1912 printk(KERN_WARNING PFX "Query device failed for %s\n",
1913 device->name);
1914 goto free_attr;
1915 }
1916
1917 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
1918 if (!srp_dev)
1919 goto free_attr;
1920
1921 /*
1922 * Use the smallest page size supported by the HCA, down to a
1923 * minimum of 512 bytes (which is the smallest sector that a
1924 * SCSI command will ever carry).
1925 */
1926 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
1927 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
Roland Dreierbf628dc2006-12-15 14:01:49 -08001928 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
Roland Dreierf5358a12006-06-17 20:37:29 -07001929
1930 INIT_LIST_HEAD(&srp_dev->dev_list);
1931
1932 srp_dev->dev = device;
1933 srp_dev->pd = ib_alloc_pd(device);
1934 if (IS_ERR(srp_dev->pd))
1935 goto free_dev;
1936
1937 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
1938 IB_ACCESS_LOCAL_WRITE |
1939 IB_ACCESS_REMOTE_READ |
1940 IB_ACCESS_REMOTE_WRITE);
1941 if (IS_ERR(srp_dev->mr))
1942 goto err_pd;
1943
1944 memset(&fmr_param, 0, sizeof fmr_param);
1945 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
1946 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
1947 fmr_param.cache = 1;
1948 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
1949 fmr_param.page_shift = srp_dev->fmr_page_shift;
1950 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
1951 IB_ACCESS_REMOTE_WRITE |
1952 IB_ACCESS_REMOTE_READ);
1953
1954 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
1955 if (IS_ERR(srp_dev->fmr_pool))
1956 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001957
Tom Tucker07ebafb2006-08-03 16:02:42 -05001958 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001959 s = 0;
1960 e = 0;
1961 } else {
1962 s = 1;
1963 e = device->phys_port_cnt;
1964 }
1965
1966 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07001967 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001968 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07001969 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001970 }
1971
Roland Dreierf5358a12006-06-17 20:37:29 -07001972 ib_set_client_data(device, &srp_client, srp_dev);
1973
1974 goto free_attr;
1975
1976err_pd:
1977 ib_dealloc_pd(srp_dev->pd);
1978
1979free_dev:
1980 kfree(srp_dev);
1981
1982free_attr:
1983 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001984}
1985
1986static void srp_remove_one(struct ib_device *device)
1987{
Roland Dreierf5358a12006-06-17 20:37:29 -07001988 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001989 struct srp_host *host, *tmp_host;
1990 LIST_HEAD(target_list);
1991 struct srp_target_port *target, *tmp_target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001992
Roland Dreierf5358a12006-06-17 20:37:29 -07001993 srp_dev = ib_get_client_data(device, &srp_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001994
Roland Dreierf5358a12006-06-17 20:37:29 -07001995 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001996 class_device_unregister(&host->class_dev);
1997 /*
1998 * Wait for the sysfs entry to go away, so that no new
1999 * target ports can be created.
2000 */
2001 wait_for_completion(&host->released);
2002
2003 /*
2004 * Mark all target ports as removed, so we stop queueing
2005 * commands and don't try to reconnect.
2006 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002007 spin_lock(&host->target_lock);
Matthew Wilcox549c5fc22006-06-17 20:37:30 -07002008 list_for_each_entry(target, &host->target_list, list) {
Ishai Rabinovitz0c5b3952006-06-17 20:37:31 -07002009 spin_lock_irq(target->scsi_host->host_lock);
2010 target->state = SRP_TARGET_REMOVED;
2011 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002012 }
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002013 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002014
2015 /*
2016 * Wait for any reconnection tasks that may have
2017 * started before we marked our target ports as
2018 * removed, and any target port removal tasks.
2019 */
2020 flush_scheduled_work();
2021
2022 list_for_each_entry_safe(target, tmp_target,
2023 &host->target_list, list) {
2024 scsi_remove_host(target->scsi_host);
2025 srp_disconnect_target(target);
2026 ib_destroy_cm_id(target->cm_id);
2027 srp_free_target_ib(target);
2028 scsi_host_put(target->scsi_host);
2029 }
2030
Roland Dreieraef9ec32005-11-02 14:07:13 -08002031 kfree(host);
2032 }
2033
Roland Dreierf5358a12006-06-17 20:37:29 -07002034 if (srp_dev->fmr_pool)
2035 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2036 ib_dereg_mr(srp_dev->mr);
2037 ib_dealloc_pd(srp_dev->pd);
2038
2039 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002040}
2041
2042static int __init srp_init_module(void)
2043{
2044 int ret;
2045
Vu Pham74b0a152006-06-17 20:37:32 -07002046 srp_template.sg_tablesize = srp_sg_tablesize;
2047 srp_max_iu_len = (sizeof (struct srp_cmd) +
2048 sizeof (struct srp_indirect_buf) +
2049 srp_sg_tablesize * 16);
2050
Roland Dreieraef9ec32005-11-02 14:07:13 -08002051 ret = class_register(&srp_class);
2052 if (ret) {
2053 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
2054 return ret;
2055 }
2056
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002057 ib_sa_register_client(&srp_sa_client);
2058
Roland Dreieraef9ec32005-11-02 14:07:13 -08002059 ret = ib_register_client(&srp_client);
2060 if (ret) {
2061 printk(KERN_ERR PFX "couldn't register IB client\n");
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002062 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002063 class_unregister(&srp_class);
2064 return ret;
2065 }
2066
2067 return 0;
2068}
2069
2070static void __exit srp_cleanup_module(void)
2071{
2072 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002073 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002074 class_unregister(&srp_class);
2075}
2076
2077module_init(srp_init_module);
2078module_exit(srp_cleanup_module);