blob: 64ab5fc7cca38db98ad503dbf4e8cf2a5fbd7eb3 [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
Roland Dreierf5358a12006-06-17 20:37:29 -0700125 iu->dma = dma_map_single(host->dev->dev->dma_device,
126 iu->buf, size, direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800127 if (dma_mapping_error(iu->dma))
128 goto out_free_buf;
129
130 iu->size = size;
131 iu->direction = direction;
132
133 return iu;
134
135out_free_buf:
136 kfree(iu->buf);
137out_free_iu:
138 kfree(iu);
139out:
140 return NULL;
141}
142
143static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
144{
145 if (!iu)
146 return;
147
Roland Dreierf5358a12006-06-17 20:37:29 -0700148 dma_unmap_single(host->dev->dev->dma_device,
149 iu->dma, iu->size, iu->direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800150 kfree(iu->buf);
151 kfree(iu);
152}
153
154static void srp_qp_event(struct ib_event *event, void *context)
155{
156 printk(KERN_ERR PFX "QP event %d\n", event->event);
157}
158
159static int srp_init_qp(struct srp_target_port *target,
160 struct ib_qp *qp)
161{
162 struct ib_qp_attr *attr;
163 int ret;
164
165 attr = kmalloc(sizeof *attr, GFP_KERNEL);
166 if (!attr)
167 return -ENOMEM;
168
Roland Dreierf5358a12006-06-17 20:37:29 -0700169 ret = ib_find_cached_pkey(target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800170 target->srp_host->port,
171 be16_to_cpu(target->path.pkey),
172 &attr->pkey_index);
173 if (ret)
174 goto out;
175
176 attr->qp_state = IB_QPS_INIT;
177 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
178 IB_ACCESS_REMOTE_WRITE);
179 attr->port_num = target->srp_host->port;
180
181 ret = ib_modify_qp(qp, attr,
182 IB_QP_STATE |
183 IB_QP_PKEY_INDEX |
184 IB_QP_ACCESS_FLAGS |
185 IB_QP_PORT);
186
187out:
188 kfree(attr);
189 return ret;
190}
191
192static int srp_create_target_ib(struct srp_target_port *target)
193{
194 struct ib_qp_init_attr *init_attr;
195 int ret;
196
197 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
198 if (!init_attr)
199 return -ENOMEM;
200
Roland Dreierf5358a12006-06-17 20:37:29 -0700201 target->cq = ib_create_cq(target->srp_host->dev->dev, srp_completion,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800202 NULL, target, SRP_CQ_SIZE);
203 if (IS_ERR(target->cq)) {
204 ret = PTR_ERR(target->cq);
205 goto out;
206 }
207
208 ib_req_notify_cq(target->cq, IB_CQ_NEXT_COMP);
209
210 init_attr->event_handler = srp_qp_event;
211 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
212 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
213 init_attr->cap.max_recv_sge = 1;
214 init_attr->cap.max_send_sge = 1;
215 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
216 init_attr->qp_type = IB_QPT_RC;
217 init_attr->send_cq = target->cq;
218 init_attr->recv_cq = target->cq;
219
Roland Dreierf5358a12006-06-17 20:37:29 -0700220 target->qp = ib_create_qp(target->srp_host->dev->pd, init_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800221 if (IS_ERR(target->qp)) {
222 ret = PTR_ERR(target->qp);
223 ib_destroy_cq(target->cq);
224 goto out;
225 }
226
227 ret = srp_init_qp(target, target->qp);
228 if (ret) {
229 ib_destroy_qp(target->qp);
230 ib_destroy_cq(target->cq);
231 goto out;
232 }
233
234out:
235 kfree(init_attr);
236 return ret;
237}
238
239static void srp_free_target_ib(struct srp_target_port *target)
240{
241 int i;
242
243 ib_destroy_qp(target->qp);
244 ib_destroy_cq(target->cq);
245
246 for (i = 0; i < SRP_RQ_SIZE; ++i)
247 srp_free_iu(target->srp_host, target->rx_ring[i]);
248 for (i = 0; i < SRP_SQ_SIZE + 1; ++i)
249 srp_free_iu(target->srp_host, target->tx_ring[i]);
250}
251
252static void srp_path_rec_completion(int status,
253 struct ib_sa_path_rec *pathrec,
254 void *target_ptr)
255{
256 struct srp_target_port *target = target_ptr;
257
258 target->status = status;
259 if (status)
260 printk(KERN_ERR PFX "Got failed path rec status %d\n", status);
261 else
262 target->path = *pathrec;
263 complete(&target->done);
264}
265
266static int srp_lookup_path(struct srp_target_port *target)
267{
268 target->path.numb_path = 1;
269
270 init_completion(&target->done);
271
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700272 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
273 target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800274 target->srp_host->port,
275 &target->path,
276 IB_SA_PATH_REC_DGID |
277 IB_SA_PATH_REC_SGID |
278 IB_SA_PATH_REC_NUMB_PATH |
279 IB_SA_PATH_REC_PKEY,
280 SRP_PATH_REC_TIMEOUT_MS,
281 GFP_KERNEL,
282 srp_path_rec_completion,
283 target, &target->path_query);
284 if (target->path_query_id < 0)
285 return target->path_query_id;
286
287 wait_for_completion(&target->done);
288
289 if (target->status < 0)
290 printk(KERN_WARNING PFX "Path record query failed\n");
291
292 return target->status;
293}
294
295static int srp_send_req(struct srp_target_port *target)
296{
297 struct {
298 struct ib_cm_req_param param;
299 struct srp_login_req priv;
300 } *req = NULL;
301 int status;
302
303 req = kzalloc(sizeof *req, GFP_KERNEL);
304 if (!req)
305 return -ENOMEM;
306
307 req->param.primary_path = &target->path;
308 req->param.alternate_path = NULL;
309 req->param.service_id = target->service_id;
310 req->param.qp_num = target->qp->qp_num;
311 req->param.qp_type = target->qp->qp_type;
312 req->param.private_data = &req->priv;
313 req->param.private_data_len = sizeof req->priv;
314 req->param.flow_control = 1;
315
316 get_random_bytes(&req->param.starting_psn, 4);
317 req->param.starting_psn &= 0xffffff;
318
319 /*
320 * Pick some arbitrary defaults here; we could make these
321 * module parameters if anyone cared about setting them.
322 */
323 req->param.responder_resources = 4;
324 req->param.remote_cm_response_timeout = 20;
325 req->param.local_cm_response_timeout = 20;
326 req->param.retry_count = 7;
327 req->param.rnr_retry_count = 7;
328 req->param.max_cm_retries = 15;
329
330 req->priv.opcode = SRP_LOGIN_REQ;
331 req->priv.tag = 0;
Vu Pham74b0a152006-06-17 20:37:32 -0700332 req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800333 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
334 SRP_BUF_FORMAT_INDIRECT);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700335 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700336 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450db2006-06-17 20:37:38 -0700337 * port identifier format is 8 bytes of ID extension followed
338 * by 8 bytes of GUID. Older drafts put the two halves in the
339 * opposite order, so that the GUID comes first.
340 *
341 * Targets conforming to these obsolete drafts can be
342 * recognized by the I/O Class they report.
343 */
344 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
345 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200346 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700347 memcpy(req->priv.initiator_port_id + 8,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200348 &target->initiator_ext, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700349 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
350 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
351 } else {
352 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200353 &target->initiator_ext, 8);
354 memcpy(req->priv.initiator_port_id + 8,
355 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700356 memcpy(req->priv.target_port_id, &target->id_ext, 8);
357 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
358 }
359
Roland Dreieraef9ec32005-11-02 14:07:13 -0800360 /*
361 * Topspin/Cisco SRP targets will reject our login unless we
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200362 * zero out the first 8 bytes of our initiator port ID and set
363 * the second 8 bytes to the local node GUID.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800364 */
365 if (topspin_workarounds && !memcmp(&target->ioc_guid, topspin_oui, 3)) {
366 printk(KERN_DEBUG PFX "Topspin/Cisco initiator port ID workaround "
367 "activated for target GUID %016llx\n",
368 (unsigned long long) be64_to_cpu(target->ioc_guid));
369 memset(req->priv.initiator_port_id, 0, 8);
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200370 memcpy(req->priv.initiator_port_id + 8,
371 &target->srp_host->dev->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800372 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800373
374 status = ib_send_cm_req(target->cm_id, &req->param);
375
376 kfree(req);
377
378 return status;
379}
380
381static void srp_disconnect_target(struct srp_target_port *target)
382{
383 /* XXX should send SRP_I_LOGOUT request */
384
385 init_completion(&target->done);
Roland Dreiere6581052006-05-17 09:13:21 -0700386 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
387 printk(KERN_DEBUG PFX "Sending CM DREQ failed\n");
388 return;
389 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800390 wait_for_completion(&target->done);
391}
392
393static void srp_remove_work(void *target_ptr)
394{
395 struct srp_target_port *target = target_ptr;
396
397 spin_lock_irq(target->scsi_host->host_lock);
398 if (target->state != SRP_TARGET_DEAD) {
399 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800400 return;
401 }
402 target->state = SRP_TARGET_REMOVED;
403 spin_unlock_irq(target->scsi_host->host_lock);
404
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700405 spin_lock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800406 list_del(&target->list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700407 spin_unlock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800408
409 scsi_remove_host(target->scsi_host);
410 ib_destroy_cm_id(target->cm_id);
411 srp_free_target_ib(target);
412 scsi_host_put(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800413}
414
415static int srp_connect_target(struct srp_target_port *target)
416{
417 int ret;
418
419 ret = srp_lookup_path(target);
420 if (ret)
421 return ret;
422
423 while (1) {
424 init_completion(&target->done);
425 ret = srp_send_req(target);
426 if (ret)
427 return ret;
428 wait_for_completion(&target->done);
429
430 /*
431 * The CM event handling code will set status to
432 * SRP_PORT_REDIRECT if we get a port redirect REJ
433 * back, or SRP_DLID_REDIRECT if we get a lid/qp
434 * redirect REJ back.
435 */
436 switch (target->status) {
437 case 0:
438 return 0;
439
440 case SRP_PORT_REDIRECT:
441 ret = srp_lookup_path(target);
442 if (ret)
443 return ret;
444 break;
445
446 case SRP_DLID_REDIRECT:
447 break;
448
449 default:
450 return target->status;
451 }
452 }
453}
454
Roland Dreierd945e1d2006-05-09 10:50:28 -0700455static void srp_unmap_data(struct scsi_cmnd *scmnd,
456 struct srp_target_port *target,
457 struct srp_request *req)
458{
459 struct scatterlist *scat;
460 int nents;
461
462 if (!scmnd->request_buffer ||
463 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
464 scmnd->sc_data_direction != DMA_FROM_DEVICE))
465 return;
466
Roland Dreierf5358a12006-06-17 20:37:29 -0700467 if (req->fmr) {
468 ib_fmr_pool_unmap(req->fmr);
469 req->fmr = NULL;
470 }
471
Roland Dreierd945e1d2006-05-09 10:50:28 -0700472 /*
473 * This handling of non-SG commands can be killed when the
474 * SCSI midlayer no longer generates non-SG commands.
475 */
476 if (likely(scmnd->use_sg)) {
477 nents = scmnd->use_sg;
478 scat = scmnd->request_buffer;
479 } else {
480 nents = 1;
481 scat = &req->fake_sg;
482 }
483
Roland Dreierf5358a12006-06-17 20:37:29 -0700484 dma_unmap_sg(target->srp_host->dev->dev->dma_device, scat, nents,
Roland Dreierd945e1d2006-05-09 10:50:28 -0700485 scmnd->sc_data_direction);
486}
487
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700488static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
489{
490 srp_unmap_data(req->scmnd, target, req);
491 list_move_tail(&req->list, &target->free_reqs);
492}
493
494static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
495{
496 req->scmnd->result = DID_RESET << 16;
497 req->scmnd->scsi_done(req->scmnd);
498 srp_remove_req(target, req);
499}
500
Roland Dreieraef9ec32005-11-02 14:07:13 -0800501static int srp_reconnect_target(struct srp_target_port *target)
502{
503 struct ib_cm_id *new_cm_id;
504 struct ib_qp_attr qp_attr;
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700505 struct srp_request *req, *tmp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800506 struct ib_wc wc;
507 int ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800508
509 spin_lock_irq(target->scsi_host->host_lock);
510 if (target->state != SRP_TARGET_LIVE) {
511 spin_unlock_irq(target->scsi_host->host_lock);
512 return -EAGAIN;
513 }
514 target->state = SRP_TARGET_CONNECTING;
515 spin_unlock_irq(target->scsi_host->host_lock);
516
517 srp_disconnect_target(target);
518 /*
519 * Now get a new local CM ID so that we avoid confusing the
520 * target in case things are really fouled up.
521 */
Roland Dreierf5358a12006-06-17 20:37:29 -0700522 new_cm_id = ib_create_cm_id(target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800523 srp_cm_handler, target);
524 if (IS_ERR(new_cm_id)) {
525 ret = PTR_ERR(new_cm_id);
526 goto err;
527 }
528 ib_destroy_cm_id(target->cm_id);
529 target->cm_id = new_cm_id;
530
531 qp_attr.qp_state = IB_QPS_RESET;
532 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
533 if (ret)
534 goto err;
535
536 ret = srp_init_qp(target, target->qp);
537 if (ret)
538 goto err;
539
540 while (ib_poll_cq(target->cq, 1, &wc) > 0)
541 ; /* nothing */
542
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300543 spin_lock_irq(target->scsi_host->host_lock);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700544 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
545 srp_reset_req(target, req);
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300546 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800547
548 target->rx_head = 0;
549 target->tx_head = 0;
550 target->tx_tail = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800551
552 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;
578 INIT_WORK(&target->work, srp_remove_work, target);
579 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;
Roland Dreierf5358a12006-06-17 20:37:29 -0700597
598 if (!dev->fmr_pool)
599 return -ENODEV;
600
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700601 if ((sg_dma_address(&scat[0]) & ~dev->fmr_page_mask) &&
602 mellanox_workarounds && !memcmp(&target->ioc_guid, mellanox_oui, 3))
603 return -EINVAL;
604
Roland Dreierf5358a12006-06-17 20:37:29 -0700605 len = page_cnt = 0;
606 for (i = 0; i < sg_cnt; ++i) {
607 if (sg_dma_address(&scat[i]) & ~dev->fmr_page_mask) {
608 if (i > 0)
609 return -EINVAL;
610 else
611 ++page_cnt;
612 }
613 if ((sg_dma_address(&scat[i]) + sg_dma_len(&scat[i])) &
614 ~dev->fmr_page_mask) {
615 if (i < sg_cnt - 1)
616 return -EINVAL;
617 else
618 ++page_cnt;
619 }
620
621 len += sg_dma_len(&scat[i]);
622 }
623
624 page_cnt += len >> dev->fmr_page_shift;
625 if (page_cnt > SRP_FMR_SIZE)
626 return -ENOMEM;
627
628 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
629 if (!dma_pages)
630 return -ENOMEM;
631
632 page_cnt = 0;
633 for (i = 0; i < sg_cnt; ++i)
634 for (j = 0; j < sg_dma_len(&scat[i]); j += dev->fmr_page_size)
635 dma_pages[page_cnt++] =
636 (sg_dma_address(&scat[i]) & dev->fmr_page_mask) + j;
637
638 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
Michael S. Tsirkinadfaa882006-07-14 00:23:55 -0700639 dma_pages, page_cnt, io_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -0700640 if (IS_ERR(req->fmr)) {
641 ret = PTR_ERR(req->fmr);
Vu Pham6583eb32006-07-14 00:23:53 -0700642 req->fmr = NULL;
Roland Dreierf5358a12006-06-17 20:37:29 -0700643 goto out;
644 }
645
646 buf->va = cpu_to_be64(sg_dma_address(&scat[0]) & ~dev->fmr_page_mask);
647 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
648 buf->len = cpu_to_be32(len);
649
650 ret = 0;
651
652out:
653 kfree(dma_pages);
654
655 return ret;
656}
657
Roland Dreieraef9ec32005-11-02 14:07:13 -0800658static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
659 struct srp_request *req)
660{
Roland Dreiercf368712006-03-24 15:47:26 -0800661 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800662 struct srp_cmd *cmd = req->cmd->buf;
Roland Dreiercf368712006-03-24 15:47:26 -0800663 int len, nents, count;
Roland Dreierf5358a12006-06-17 20:37:29 -0700664 u8 fmt = SRP_DATA_DESC_DIRECT;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800665
666 if (!scmnd->request_buffer || scmnd->sc_data_direction == DMA_NONE)
667 return sizeof (struct srp_cmd);
668
669 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
670 scmnd->sc_data_direction != DMA_TO_DEVICE) {
671 printk(KERN_WARNING PFX "Unhandled data direction %d\n",
672 scmnd->sc_data_direction);
673 return -EINVAL;
674 }
675
Roland Dreiercf368712006-03-24 15:47:26 -0800676 /*
677 * This handling of non-SG commands can be killed when the
678 * SCSI midlayer no longer generates non-SG commands.
679 */
680 if (likely(scmnd->use_sg)) {
681 nents = scmnd->use_sg;
682 scat = scmnd->request_buffer;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800683 } else {
Roland Dreiercf368712006-03-24 15:47:26 -0800684 nents = 1;
685 scat = &req->fake_sg;
686 sg_init_one(scat, scmnd->request_buffer, scmnd->request_bufflen);
687 }
688
Roland Dreierf5358a12006-06-17 20:37:29 -0700689 count = dma_map_sg(target->srp_host->dev->dev->dma_device,
690 scat, nents, scmnd->sc_data_direction);
691
692 fmt = SRP_DATA_DESC_DIRECT;
693 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800694
695 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700696 /*
697 * The midlayer only generated a single gather/scatter
698 * entry, or DMA mapping coalesced everything to a
699 * single entry. So a direct descriptor along with
700 * the DMA MR suffices.
701 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800702 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800703
Roland Dreiercf368712006-03-24 15:47:26 -0800704 buf->va = cpu_to_be64(sg_dma_address(scat));
Roland Dreierf5358a12006-06-17 20:37:29 -0700705 buf->key = cpu_to_be32(target->srp_host->dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800706 buf->len = cpu_to_be32(sg_dma_len(scat));
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700707 } else if (srp_map_fmr(target, scat, count, req,
Roland Dreierf5358a12006-06-17 20:37:29 -0700708 (void *) cmd->add_data)) {
709 /*
710 * FMR mapping failed, and the scatterlist has more
711 * than one entry. Generate an indirect memory
712 * descriptor.
713 */
Roland Dreiercf368712006-03-24 15:47:26 -0800714 struct srp_indirect_buf *buf = (void *) cmd->add_data;
715 u32 datalen = 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700716 int i;
Roland Dreiercf368712006-03-24 15:47:26 -0800717
718 fmt = SRP_DATA_DESC_INDIRECT;
Roland Dreierf5358a12006-06-17 20:37:29 -0700719 len = sizeof (struct srp_cmd) +
720 sizeof (struct srp_indirect_buf) +
721 count * sizeof (struct srp_direct_buf);
722
723 for (i = 0; i < count; ++i) {
724 buf->desc_list[i].va =
725 cpu_to_be64(sg_dma_address(&scat[i]));
726 buf->desc_list[i].key =
727 cpu_to_be32(target->srp_host->dev->mr->rkey);
728 buf->desc_list[i].len =
729 cpu_to_be32(sg_dma_len(&scat[i]));
730 datalen += sg_dma_len(&scat[i]);
731 }
Roland Dreiercf368712006-03-24 15:47:26 -0800732
733 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
734 cmd->data_out_desc_cnt = count;
735 else
736 cmd->data_in_desc_cnt = count;
737
Roland Dreierf5358a12006-06-17 20:37:29 -0700738 buf->table_desc.va =
739 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800740 buf->table_desc.key =
Roland Dreierf5358a12006-06-17 20:37:29 -0700741 cpu_to_be32(target->srp_host->dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800742 buf->table_desc.len =
743 cpu_to_be32(count * sizeof (struct srp_direct_buf));
744
Roland Dreiercf368712006-03-24 15:47:26 -0800745 buf->len = cpu_to_be32(datalen);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800746 }
747
748 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
749 cmd->buf_fmt = fmt << 4;
750 else
751 cmd->buf_fmt = fmt;
752
Roland Dreieraef9ec32005-11-02 14:07:13 -0800753 return len;
754}
755
Roland Dreieraef9ec32005-11-02 14:07:13 -0800756static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
757{
758 struct srp_request *req;
759 struct scsi_cmnd *scmnd;
760 unsigned long flags;
761 s32 delta;
762
763 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
764
765 spin_lock_irqsave(target->scsi_host->host_lock, flags);
766
767 target->req_lim += delta;
768
769 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
770
771 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
772 if (be32_to_cpu(rsp->resp_data_len) < 4)
773 req->tsk_status = -1;
774 else
775 req->tsk_status = rsp->data[3];
776 complete(&req->done);
777 } else {
Roland Dreierd945e1d2006-05-09 10:50:28 -0700778 scmnd = req->scmnd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800779 if (!scmnd)
780 printk(KERN_ERR "Null scmnd for RSP w/tag %016llx\n",
781 (unsigned long long) rsp->tag);
782 scmnd->result = rsp->status;
783
784 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
785 memcpy(scmnd->sense_buffer, rsp->data +
786 be32_to_cpu(rsp->resp_data_len),
787 min_t(int, be32_to_cpu(rsp->sense_data_len),
788 SCSI_SENSE_BUFFERSIZE));
789 }
790
791 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
792 scmnd->resid = be32_to_cpu(rsp->data_out_res_cnt);
793 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
794 scmnd->resid = be32_to_cpu(rsp->data_in_res_cnt);
795
Roland Dreieraef9ec32005-11-02 14:07:13 -0800796 if (!req->tsk_mgmt) {
Roland Dreieraef9ec32005-11-02 14:07:13 -0800797 scmnd->host_scribble = (void *) -1L;
798 scmnd->scsi_done(scmnd);
799
Roland Dreierd945e1d2006-05-09 10:50:28 -0700800 srp_remove_req(target, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800801 } else
802 req->cmd_done = 1;
803 }
804
805 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
806}
807
Roland Dreieraef9ec32005-11-02 14:07:13 -0800808static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
809{
810 struct srp_iu *iu;
811 u8 opcode;
812
813 iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
814
Roland Dreierf5358a12006-06-17 20:37:29 -0700815 dma_sync_single_for_cpu(target->srp_host->dev->dev->dma_device, iu->dma,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800816 target->max_ti_iu_len, DMA_FROM_DEVICE);
817
818 opcode = *(u8 *) iu->buf;
819
820 if (0) {
821 int i;
822
823 printk(KERN_ERR PFX "recv completion, opcode 0x%02x\n", opcode);
824
825 for (i = 0; i < wc->byte_len; ++i) {
826 if (i % 8 == 0)
827 printk(KERN_ERR " [%02x] ", i);
828 printk(" %02x", ((u8 *) iu->buf)[i]);
829 if ((i + 1) % 8 == 0)
830 printk("\n");
831 }
832
833 if (wc->byte_len % 8)
834 printk("\n");
835 }
836
837 switch (opcode) {
838 case SRP_RSP:
839 srp_process_rsp(target, iu->buf);
840 break;
841
842 case SRP_T_LOGOUT:
843 /* XXX Handle target logout */
844 printk(KERN_WARNING PFX "Got target logout request\n");
845 break;
846
847 default:
848 printk(KERN_WARNING PFX "Unhandled SRP opcode 0x%02x\n", opcode);
849 break;
850 }
851
Roland Dreierf5358a12006-06-17 20:37:29 -0700852 dma_sync_single_for_device(target->srp_host->dev->dev->dma_device, iu->dma,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800853 target->max_ti_iu_len, DMA_FROM_DEVICE);
854}
855
856static void srp_completion(struct ib_cq *cq, void *target_ptr)
857{
858 struct srp_target_port *target = target_ptr;
859 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800860
861 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
862 while (ib_poll_cq(cq, 1, &wc) > 0) {
863 if (wc.status) {
864 printk(KERN_ERR PFX "failed %s status %d\n",
865 wc.wr_id & SRP_OP_RECV ? "receive" : "send",
866 wc.status);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800867 break;
868 }
869
870 if (wc.wr_id & SRP_OP_RECV)
871 srp_handle_recv(target, &wc);
872 else
873 ++target->tx_tail;
874 }
875}
876
877static int __srp_post_recv(struct srp_target_port *target)
878{
879 struct srp_iu *iu;
880 struct ib_sge list;
881 struct ib_recv_wr wr, *bad_wr;
882 unsigned int next;
883 int ret;
884
885 next = target->rx_head & (SRP_RQ_SIZE - 1);
886 wr.wr_id = next | SRP_OP_RECV;
887 iu = target->rx_ring[next];
888
889 list.addr = iu->dma;
890 list.length = iu->size;
Roland Dreierf5358a12006-06-17 20:37:29 -0700891 list.lkey = target->srp_host->dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800892
893 wr.next = NULL;
894 wr.sg_list = &list;
895 wr.num_sge = 1;
896
897 ret = ib_post_recv(target->qp, &wr, &bad_wr);
898 if (!ret)
899 ++target->rx_head;
900
901 return ret;
902}
903
904static int srp_post_recv(struct srp_target_port *target)
905{
906 unsigned long flags;
907 int ret;
908
909 spin_lock_irqsave(target->scsi_host->host_lock, flags);
910 ret = __srp_post_recv(target);
911 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
912
913 return ret;
914}
915
916/*
917 * Must be called with target->scsi_host->host_lock held to protect
Roland Dreier47f2bce2005-11-15 00:19:21 -0800918 * req_lim and tx_head. Lock cannot be dropped between call here and
919 * call to __srp_post_send().
Roland Dreieraef9ec32005-11-02 14:07:13 -0800920 */
921static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target)
922{
923 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
924 return NULL;
925
Roland Dreier6bfa24f2006-06-17 20:37:33 -0700926 if (unlikely(target->req_lim < 1))
927 ++target->zero_req_lim;
Roland Dreier47f2bce2005-11-15 00:19:21 -0800928
Roland Dreieraef9ec32005-11-02 14:07:13 -0800929 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
930}
931
932/*
933 * Must be called with target->scsi_host->host_lock held to protect
934 * req_lim and tx_head.
935 */
936static int __srp_post_send(struct srp_target_port *target,
937 struct srp_iu *iu, int len)
938{
939 struct ib_sge list;
940 struct ib_send_wr wr, *bad_wr;
941 int ret = 0;
942
Roland Dreieraef9ec32005-11-02 14:07:13 -0800943 list.addr = iu->dma;
944 list.length = len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700945 list.lkey = target->srp_host->dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800946
947 wr.next = NULL;
948 wr.wr_id = target->tx_head & SRP_SQ_SIZE;
949 wr.sg_list = &list;
950 wr.num_sge = 1;
951 wr.opcode = IB_WR_SEND;
952 wr.send_flags = IB_SEND_SIGNALED;
953
954 ret = ib_post_send(target->qp, &wr, &bad_wr);
955
956 if (!ret) {
957 ++target->tx_head;
958 --target->req_lim;
959 }
960
961 return ret;
962}
963
964static int srp_queuecommand(struct scsi_cmnd *scmnd,
965 void (*done)(struct scsi_cmnd *))
966{
967 struct srp_target_port *target = host_to_target(scmnd->device->host);
968 struct srp_request *req;
969 struct srp_iu *iu;
970 struct srp_cmd *cmd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800971 int len;
972
973 if (target->state == SRP_TARGET_CONNECTING)
974 goto err;
975
976 if (target->state == SRP_TARGET_DEAD ||
977 target->state == SRP_TARGET_REMOVED) {
978 scmnd->result = DID_BAD_TARGET << 16;
979 done(scmnd);
980 return 0;
981 }
982
983 iu = __srp_get_tx_iu(target);
984 if (!iu)
985 goto err;
986
Roland Dreierf5358a12006-06-17 20:37:29 -0700987 dma_sync_single_for_cpu(target->srp_host->dev->dev->dma_device, iu->dma,
Vu Pham74b0a152006-06-17 20:37:32 -0700988 srp_max_iu_len, DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800989
Roland Dreierd945e1d2006-05-09 10:50:28 -0700990 req = list_entry(target->free_reqs.next, struct srp_request, list);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800991
992 scmnd->scsi_done = done;
993 scmnd->result = 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -0700994 scmnd->host_scribble = (void *) (long) req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800995
996 cmd = iu->buf;
997 memset(cmd, 0, sizeof *cmd);
998
999 cmd->opcode = SRP_CMD;
1000 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001001 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001002 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1003
Roland Dreieraef9ec32005-11-02 14:07:13 -08001004 req->scmnd = scmnd;
1005 req->cmd = iu;
1006 req->cmd_done = 0;
1007 req->tsk_mgmt = NULL;
1008
1009 len = srp_map_data(scmnd, target, req);
1010 if (len < 0) {
1011 printk(KERN_ERR PFX "Failed to map data\n");
1012 goto err;
1013 }
1014
1015 if (__srp_post_recv(target)) {
1016 printk(KERN_ERR PFX "Recv failed\n");
1017 goto err_unmap;
1018 }
1019
Roland Dreierf5358a12006-06-17 20:37:29 -07001020 dma_sync_single_for_device(target->srp_host->dev->dev->dma_device, iu->dma,
Vu Pham74b0a152006-06-17 20:37:32 -07001021 srp_max_iu_len, DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001022
1023 if (__srp_post_send(target, iu, len)) {
1024 printk(KERN_ERR PFX "Send failed\n");
1025 goto err_unmap;
1026 }
1027
Roland Dreierd945e1d2006-05-09 10:50:28 -07001028 list_move_tail(&req->list, &target->req_queue);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001029
1030 return 0;
1031
1032err_unmap:
1033 srp_unmap_data(scmnd, target, req);
1034
1035err:
1036 return SCSI_MLQUEUE_HOST_BUSY;
1037}
1038
1039static int srp_alloc_iu_bufs(struct srp_target_port *target)
1040{
1041 int i;
1042
1043 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1044 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1045 target->max_ti_iu_len,
1046 GFP_KERNEL, DMA_FROM_DEVICE);
1047 if (!target->rx_ring[i])
1048 goto err;
1049 }
1050
1051 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1052 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
Vu Pham74b0a152006-06-17 20:37:32 -07001053 srp_max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001054 GFP_KERNEL, DMA_TO_DEVICE);
1055 if (!target->tx_ring[i])
1056 goto err;
1057 }
1058
1059 return 0;
1060
1061err:
1062 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1063 srp_free_iu(target->srp_host, target->rx_ring[i]);
1064 target->rx_ring[i] = NULL;
1065 }
1066
1067 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1068 srp_free_iu(target->srp_host, target->tx_ring[i]);
1069 target->tx_ring[i] = NULL;
1070 }
1071
1072 return -ENOMEM;
1073}
1074
1075static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1076 struct ib_cm_event *event,
1077 struct srp_target_port *target)
1078{
1079 struct ib_class_port_info *cpi;
1080 int opcode;
1081
1082 switch (event->param.rej_rcvd.reason) {
1083 case IB_CM_REJ_PORT_CM_REDIRECT:
1084 cpi = event->param.rej_rcvd.ari;
1085 target->path.dlid = cpi->redirect_lid;
1086 target->path.pkey = cpi->redirect_pkey;
1087 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1088 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1089
1090 target->status = target->path.dlid ?
1091 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1092 break;
1093
1094 case IB_CM_REJ_PORT_REDIRECT:
1095 if (topspin_workarounds &&
1096 !memcmp(&target->ioc_guid, topspin_oui, 3)) {
1097 /*
1098 * Topspin/Cisco SRP gateways incorrectly send
1099 * reject reason code 25 when they mean 24
1100 * (port redirect).
1101 */
1102 memcpy(target->path.dgid.raw,
1103 event->param.rej_rcvd.ari, 16);
1104
1105 printk(KERN_DEBUG PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1106 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1107 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1108
1109 target->status = SRP_PORT_REDIRECT;
1110 } else {
1111 printk(KERN_WARNING " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1112 target->status = -ECONNRESET;
1113 }
1114 break;
1115
1116 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
1117 printk(KERN_WARNING " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1118 target->status = -ECONNRESET;
1119 break;
1120
1121 case IB_CM_REJ_CONSUMER_DEFINED:
1122 opcode = *(u8 *) event->private_data;
1123 if (opcode == SRP_LOGIN_REJ) {
1124 struct srp_login_rej *rej = event->private_data;
1125 u32 reason = be32_to_cpu(rej->reason);
1126
1127 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
1128 printk(KERN_WARNING PFX
1129 "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1130 else
1131 printk(KERN_WARNING PFX
1132 "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
1133 } else
1134 printk(KERN_WARNING " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1135 " opcode 0x%02x\n", opcode);
1136 target->status = -ECONNRESET;
1137 break;
1138
1139 default:
1140 printk(KERN_WARNING " REJ reason 0x%x\n",
1141 event->param.rej_rcvd.reason);
1142 target->status = -ECONNRESET;
1143 }
1144}
1145
1146static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1147{
1148 struct srp_target_port *target = cm_id->context;
1149 struct ib_qp_attr *qp_attr = NULL;
1150 int attr_mask = 0;
1151 int comp = 0;
1152 int opcode = 0;
1153
1154 switch (event->event) {
1155 case IB_CM_REQ_ERROR:
1156 printk(KERN_DEBUG PFX "Sending CM REQ failed\n");
1157 comp = 1;
1158 target->status = -ECONNRESET;
1159 break;
1160
1161 case IB_CM_REP_RECEIVED:
1162 comp = 1;
1163 opcode = *(u8 *) event->private_data;
1164
1165 if (opcode == SRP_LOGIN_RSP) {
1166 struct srp_login_rsp *rsp = event->private_data;
1167
1168 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1169 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1170
1171 target->scsi_host->can_queue = min(target->req_lim,
1172 target->scsi_host->can_queue);
1173 } else {
1174 printk(KERN_WARNING PFX "Unhandled RSP opcode %#x\n", opcode);
1175 target->status = -ECONNRESET;
1176 break;
1177 }
1178
Vu Phamd2fcea72006-11-21 14:14:10 -08001179 if (!target->rx_ring[0]) {
1180 target->status = srp_alloc_iu_bufs(target);
1181 if (target->status)
1182 break;
1183 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001184
1185 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1186 if (!qp_attr) {
1187 target->status = -ENOMEM;
1188 break;
1189 }
1190
1191 qp_attr->qp_state = IB_QPS_RTR;
1192 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1193 if (target->status)
1194 break;
1195
1196 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1197 if (target->status)
1198 break;
1199
1200 target->status = srp_post_recv(target);
1201 if (target->status)
1202 break;
1203
1204 qp_attr->qp_state = IB_QPS_RTS;
1205 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1206 if (target->status)
1207 break;
1208
1209 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1210 if (target->status)
1211 break;
1212
1213 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1214 if (target->status)
1215 break;
1216
1217 break;
1218
1219 case IB_CM_REJ_RECEIVED:
1220 printk(KERN_DEBUG PFX "REJ received\n");
1221 comp = 1;
1222
1223 srp_cm_rej_handler(cm_id, event, target);
1224 break;
1225
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001226 case IB_CM_DREQ_RECEIVED:
1227 printk(KERN_WARNING PFX "DREQ received - connection closed\n");
1228 if (ib_send_cm_drep(cm_id, NULL, 0))
1229 printk(KERN_ERR PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001230 break;
1231
1232 case IB_CM_TIMEWAIT_EXIT:
1233 printk(KERN_ERR PFX "connection closed\n");
1234
1235 comp = 1;
1236 target->status = 0;
1237 break;
1238
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001239 case IB_CM_MRA_RECEIVED:
1240 case IB_CM_DREQ_ERROR:
1241 case IB_CM_DREP_RECEIVED:
1242 break;
1243
Roland Dreieraef9ec32005-11-02 14:07:13 -08001244 default:
1245 printk(KERN_WARNING PFX "Unhandled CM event %d\n", event->event);
1246 break;
1247 }
1248
1249 if (comp)
1250 complete(&target->done);
1251
1252 kfree(qp_attr);
1253
1254 return 0;
1255}
1256
Roland Dreierd945e1d2006-05-09 10:50:28 -07001257static int srp_send_tsk_mgmt(struct srp_target_port *target,
1258 struct srp_request *req, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001259{
Roland Dreieraef9ec32005-11-02 14:07:13 -08001260 struct srp_iu *iu;
1261 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001262
1263 spin_lock_irq(target->scsi_host->host_lock);
1264
Roland Dreier1285b3a2006-03-03 15:47:25 -08001265 if (target->state == SRP_TARGET_DEAD ||
1266 target->state == SRP_TARGET_REMOVED) {
Roland Dreierd945e1d2006-05-09 10:50:28 -07001267 req->scmnd->result = DID_BAD_TARGET << 16;
Roland Dreier1285b3a2006-03-03 15:47:25 -08001268 goto out;
1269 }
1270
Roland Dreieraef9ec32005-11-02 14:07:13 -08001271 init_completion(&req->done);
1272
1273 iu = __srp_get_tx_iu(target);
1274 if (!iu)
1275 goto out;
1276
1277 tsk_mgmt = iu->buf;
1278 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1279
1280 tsk_mgmt->opcode = SRP_TSK_MGMT;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001281 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1282 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001283 tsk_mgmt->tsk_mgmt_func = func;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001284 tsk_mgmt->task_tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001285
1286 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1287 goto out;
1288
1289 req->tsk_mgmt = iu;
1290
1291 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001292
Roland Dreieraef9ec32005-11-02 14:07:13 -08001293 if (!wait_for_completion_timeout(&req->done,
1294 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001295 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001296
Roland Dreierd945e1d2006-05-09 10:50:28 -07001297 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001298
1299out:
1300 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001301 return -1;
1302}
1303
1304static int srp_find_req(struct srp_target_port *target,
1305 struct scsi_cmnd *scmnd,
1306 struct srp_request **req)
1307{
1308 if (scmnd->host_scribble == (void *) -1L)
1309 return -1;
1310
1311 *req = &target->req_ring[(long) scmnd->host_scribble];
1312
1313 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001314}
1315
1316static int srp_abort(struct scsi_cmnd *scmnd)
1317{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001318 struct srp_target_port *target = host_to_target(scmnd->device->host);
1319 struct srp_request *req;
1320 int ret = SUCCESS;
1321
Roland Dreieraef9ec32005-11-02 14:07:13 -08001322 printk(KERN_ERR "SRP abort called\n");
1323
Roland Dreierd945e1d2006-05-09 10:50:28 -07001324 if (srp_find_req(target, scmnd, &req))
1325 return FAILED;
1326 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1327 return FAILED;
1328
1329 spin_lock_irq(target->scsi_host->host_lock);
1330
1331 if (req->cmd_done) {
1332 srp_remove_req(target, req);
1333 scmnd->scsi_done(scmnd);
1334 } else if (!req->tsk_status) {
1335 srp_remove_req(target, req);
1336 scmnd->result = DID_ABORT << 16;
1337 } else
1338 ret = FAILED;
1339
1340 spin_unlock_irq(target->scsi_host->host_lock);
1341
1342 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001343}
1344
1345static int srp_reset_device(struct scsi_cmnd *scmnd)
1346{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001347 struct srp_target_port *target = host_to_target(scmnd->device->host);
1348 struct srp_request *req, *tmp;
1349
Roland Dreieraef9ec32005-11-02 14:07:13 -08001350 printk(KERN_ERR "SRP reset_device called\n");
1351
Roland Dreierd945e1d2006-05-09 10:50:28 -07001352 if (srp_find_req(target, scmnd, &req))
1353 return FAILED;
1354 if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1355 return FAILED;
1356 if (req->tsk_status)
1357 return FAILED;
1358
1359 spin_lock_irq(target->scsi_host->host_lock);
1360
1361 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001362 if (req->scmnd->device == scmnd->device)
1363 srp_reset_req(target, req);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001364
1365 spin_unlock_irq(target->scsi_host->host_lock);
1366
1367 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001368}
1369
1370static int srp_reset_host(struct scsi_cmnd *scmnd)
1371{
1372 struct srp_target_port *target = host_to_target(scmnd->device->host);
1373 int ret = FAILED;
1374
1375 printk(KERN_ERR PFX "SRP reset_host called\n");
1376
1377 if (!srp_reconnect_target(target))
1378 ret = SUCCESS;
1379
1380 return ret;
1381}
1382
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001383static ssize_t show_id_ext(struct class_device *cdev, char *buf)
1384{
1385 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1386
1387 if (target->state == SRP_TARGET_DEAD ||
1388 target->state == SRP_TARGET_REMOVED)
1389 return -ENODEV;
1390
1391 return sprintf(buf, "0x%016llx\n",
1392 (unsigned long long) be64_to_cpu(target->id_ext));
1393}
1394
1395static ssize_t show_ioc_guid(struct class_device *cdev, char *buf)
1396{
1397 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1398
1399 if (target->state == SRP_TARGET_DEAD ||
1400 target->state == SRP_TARGET_REMOVED)
1401 return -ENODEV;
1402
1403 return sprintf(buf, "0x%016llx\n",
1404 (unsigned long long) be64_to_cpu(target->ioc_guid));
1405}
1406
1407static ssize_t show_service_id(struct class_device *cdev, char *buf)
1408{
1409 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1410
1411 if (target->state == SRP_TARGET_DEAD ||
1412 target->state == SRP_TARGET_REMOVED)
1413 return -ENODEV;
1414
1415 return sprintf(buf, "0x%016llx\n",
1416 (unsigned long long) be64_to_cpu(target->service_id));
1417}
1418
1419static ssize_t show_pkey(struct class_device *cdev, char *buf)
1420{
1421 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1422
1423 if (target->state == SRP_TARGET_DEAD ||
1424 target->state == SRP_TARGET_REMOVED)
1425 return -ENODEV;
1426
1427 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1428}
1429
1430static ssize_t show_dgid(struct class_device *cdev, char *buf)
1431{
1432 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1433
1434 if (target->state == SRP_TARGET_DEAD ||
1435 target->state == SRP_TARGET_REMOVED)
1436 return -ENODEV;
1437
1438 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1439 be16_to_cpu(((__be16 *) target->path.dgid.raw)[0]),
1440 be16_to_cpu(((__be16 *) target->path.dgid.raw)[1]),
1441 be16_to_cpu(((__be16 *) target->path.dgid.raw)[2]),
1442 be16_to_cpu(((__be16 *) target->path.dgid.raw)[3]),
1443 be16_to_cpu(((__be16 *) target->path.dgid.raw)[4]),
1444 be16_to_cpu(((__be16 *) target->path.dgid.raw)[5]),
1445 be16_to_cpu(((__be16 *) target->path.dgid.raw)[6]),
1446 be16_to_cpu(((__be16 *) target->path.dgid.raw)[7]));
1447}
1448
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001449static ssize_t show_zero_req_lim(struct class_device *cdev, char *buf)
1450{
1451 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1452
1453 if (target->state == SRP_TARGET_DEAD ||
1454 target->state == SRP_TARGET_REMOVED)
1455 return -ENODEV;
1456
1457 return sprintf(buf, "%d\n", target->zero_req_lim);
1458}
1459
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001460static ssize_t show_local_ib_port(struct class_device *cdev, char *buf)
1461{
1462 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1463
1464 return sprintf(buf, "%d\n", target->srp_host->port);
1465}
1466
1467static ssize_t show_local_ib_device(struct class_device *cdev, char *buf)
1468{
1469 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1470
1471 return sprintf(buf, "%s\n", target->srp_host->dev->dev->name);
1472}
1473
1474static CLASS_DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1475static CLASS_DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1476static CLASS_DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1477static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1478static CLASS_DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1479static CLASS_DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1480static CLASS_DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1481static CLASS_DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001482
1483static struct class_device_attribute *srp_host_attrs[] = {
1484 &class_device_attr_id_ext,
1485 &class_device_attr_ioc_guid,
1486 &class_device_attr_service_id,
1487 &class_device_attr_pkey,
1488 &class_device_attr_dgid,
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001489 &class_device_attr_zero_req_lim,
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001490 &class_device_attr_local_ib_port,
1491 &class_device_attr_local_ib_device,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001492 NULL
1493};
1494
Roland Dreieraef9ec32005-11-02 14:07:13 -08001495static struct scsi_host_template srp_template = {
1496 .module = THIS_MODULE,
1497 .name = DRV_NAME,
1498 .info = srp_target_info,
1499 .queuecommand = srp_queuecommand,
1500 .eh_abort_handler = srp_abort,
1501 .eh_device_reset_handler = srp_reset_device,
1502 .eh_host_reset_handler = srp_reset_host,
1503 .can_queue = SRP_SQ_SIZE,
1504 .this_id = -1,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001505 .cmd_per_lun = SRP_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001506 .use_clustering = ENABLE_CLUSTERING,
1507 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001508};
1509
1510static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1511{
1512 sprintf(target->target_name, "SRP.T10:%016llX",
1513 (unsigned long long) be64_to_cpu(target->id_ext));
1514
Roland Dreierf5358a12006-06-17 20:37:29 -07001515 if (scsi_add_host(target->scsi_host, host->dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001516 return -ENODEV;
1517
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001518 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001519 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001520 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001521
1522 target->state = SRP_TARGET_LIVE;
1523
Roland Dreieraef9ec32005-11-02 14:07:13 -08001524 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07001525 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001526
1527 return 0;
1528}
1529
1530static void srp_release_class_dev(struct class_device *class_dev)
1531{
1532 struct srp_host *host =
1533 container_of(class_dev, struct srp_host, class_dev);
1534
1535 complete(&host->released);
1536}
1537
1538static struct class srp_class = {
1539 .name = "infiniband_srp",
1540 .release = srp_release_class_dev
1541};
1542
1543/*
1544 * Target ports are added by writing
1545 *
1546 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1547 * pkey=<P_Key>,service_id=<service ID>
1548 *
1549 * to the add_target sysfs attribute.
1550 */
1551enum {
1552 SRP_OPT_ERR = 0,
1553 SRP_OPT_ID_EXT = 1 << 0,
1554 SRP_OPT_IOC_GUID = 1 << 1,
1555 SRP_OPT_DGID = 1 << 2,
1556 SRP_OPT_PKEY = 1 << 3,
1557 SRP_OPT_SERVICE_ID = 1 << 4,
1558 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07001559 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07001560 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001561 SRP_OPT_INITIATOR_EXT = 1 << 8,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001562 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1563 SRP_OPT_IOC_GUID |
1564 SRP_OPT_DGID |
1565 SRP_OPT_PKEY |
1566 SRP_OPT_SERVICE_ID),
1567};
1568
1569static match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07001570 { SRP_OPT_ID_EXT, "id_ext=%s" },
1571 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1572 { SRP_OPT_DGID, "dgid=%s" },
1573 { SRP_OPT_PKEY, "pkey=%x" },
1574 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1575 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1576 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07001577 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001578 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
Vu Pham52fb2b502006-06-17 20:37:31 -07001579 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001580};
1581
1582static int srp_parse_options(const char *buf, struct srp_target_port *target)
1583{
1584 char *options, *sep_opt;
1585 char *p;
1586 char dgid[3];
1587 substring_t args[MAX_OPT_ARGS];
1588 int opt_mask = 0;
1589 int token;
1590 int ret = -EINVAL;
1591 int i;
1592
1593 options = kstrdup(buf, GFP_KERNEL);
1594 if (!options)
1595 return -ENOMEM;
1596
1597 sep_opt = options;
1598 while ((p = strsep(&sep_opt, ",")) != NULL) {
1599 if (!*p)
1600 continue;
1601
1602 token = match_token(p, srp_opt_tokens, args);
1603 opt_mask |= token;
1604
1605 switch (token) {
1606 case SRP_OPT_ID_EXT:
1607 p = match_strdup(args);
1608 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1609 kfree(p);
1610 break;
1611
1612 case SRP_OPT_IOC_GUID:
1613 p = match_strdup(args);
1614 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1615 kfree(p);
1616 break;
1617
1618 case SRP_OPT_DGID:
1619 p = match_strdup(args);
1620 if (strlen(p) != 32) {
1621 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07001622 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001623 goto out;
1624 }
1625
1626 for (i = 0; i < 16; ++i) {
1627 strlcpy(dgid, p + i * 2, 3);
1628 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1629 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08001630 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001631 break;
1632
1633 case SRP_OPT_PKEY:
1634 if (match_hex(args, &token)) {
1635 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1636 goto out;
1637 }
1638 target->path.pkey = cpu_to_be16(token);
1639 break;
1640
1641 case SRP_OPT_SERVICE_ID:
1642 p = match_strdup(args);
1643 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
1644 kfree(p);
1645 break;
1646
1647 case SRP_OPT_MAX_SECT:
1648 if (match_int(args, &token)) {
1649 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1650 goto out;
1651 }
1652 target->scsi_host->max_sectors = token;
1653 break;
1654
Vu Pham52fb2b502006-06-17 20:37:31 -07001655 case SRP_OPT_MAX_CMD_PER_LUN:
1656 if (match_int(args, &token)) {
1657 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1658 goto out;
1659 }
1660 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1661 break;
1662
Ramachandra K0c0450db2006-06-17 20:37:38 -07001663 case SRP_OPT_IO_CLASS:
1664 if (match_hex(args, &token)) {
1665 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1666 goto out;
1667 }
1668 if (token != SRP_REV10_IB_IO_CLASS &&
1669 token != SRP_REV16A_IB_IO_CLASS) {
1670 printk(KERN_WARNING PFX "unknown IO class parameter value"
1671 " %x specified (use %x or %x).\n",
1672 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1673 goto out;
1674 }
1675 target->io_class = token;
1676 break;
1677
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001678 case SRP_OPT_INITIATOR_EXT:
1679 p = match_strdup(args);
1680 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1681 kfree(p);
1682 break;
1683
Roland Dreieraef9ec32005-11-02 14:07:13 -08001684 default:
1685 printk(KERN_WARNING PFX "unknown parameter or missing value "
1686 "'%s' in target creation request\n", p);
1687 goto out;
1688 }
1689 }
1690
1691 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1692 ret = 0;
1693 else
1694 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1695 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1696 !(srp_opt_tokens[i].token & opt_mask))
1697 printk(KERN_WARNING PFX "target creation request is "
1698 "missing parameter '%s'\n",
1699 srp_opt_tokens[i].pattern);
1700
1701out:
1702 kfree(options);
1703 return ret;
1704}
1705
1706static ssize_t srp_create_target(struct class_device *class_dev,
1707 const char *buf, size_t count)
1708{
1709 struct srp_host *host =
1710 container_of(class_dev, struct srp_host, class_dev);
1711 struct Scsi_Host *target_host;
1712 struct srp_target_port *target;
1713 int ret;
1714 int i;
1715
1716 target_host = scsi_host_alloc(&srp_template,
1717 sizeof (struct srp_target_port));
1718 if (!target_host)
1719 return -ENOMEM;
1720
Arne Redlich3c8edf02006-11-15 12:43:00 +01001721 target_host->max_lun = SRP_MAX_LUN;
1722 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08001723
Roland Dreieraef9ec32005-11-02 14:07:13 -08001724 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001725
Ramachandra K0c0450db2006-06-17 20:37:38 -07001726 target->io_class = SRP_REV16A_IB_IO_CLASS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001727 target->scsi_host = target_host;
1728 target->srp_host = host;
1729
Roland Dreierd945e1d2006-05-09 10:50:28 -07001730 INIT_LIST_HEAD(&target->free_reqs);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001731 INIT_LIST_HEAD(&target->req_queue);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001732 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1733 target->req_ring[i].index = i;
1734 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1735 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001736
1737 ret = srp_parse_options(buf, target);
1738 if (ret)
1739 goto err;
1740
Roland Dreierf5358a12006-06-17 20:37:29 -07001741 ib_get_cached_gid(host->dev->dev, host->port, 0, &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001742
1743 printk(KERN_DEBUG PFX "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1744 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1745 (unsigned long long) be64_to_cpu(target->id_ext),
1746 (unsigned long long) be64_to_cpu(target->ioc_guid),
1747 be16_to_cpu(target->path.pkey),
1748 (unsigned long long) be64_to_cpu(target->service_id),
1749 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]),
1750 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]),
1751 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]),
1752 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]),
1753 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]),
1754 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]),
1755 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]),
1756 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14]));
1757
1758 ret = srp_create_target_ib(target);
1759 if (ret)
1760 goto err;
1761
Roland Dreierf5358a12006-06-17 20:37:29 -07001762 target->cm_id = ib_create_cm_id(host->dev->dev, srp_cm_handler, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001763 if (IS_ERR(target->cm_id)) {
1764 ret = PTR_ERR(target->cm_id);
1765 goto err_free;
1766 }
1767
1768 ret = srp_connect_target(target);
1769 if (ret) {
1770 printk(KERN_ERR PFX "Connection failed\n");
1771 goto err_cm_id;
1772 }
1773
1774 ret = srp_add_target(host, target);
1775 if (ret)
1776 goto err_disconnect;
1777
1778 return count;
1779
1780err_disconnect:
1781 srp_disconnect_target(target);
1782
1783err_cm_id:
1784 ib_destroy_cm_id(target->cm_id);
1785
1786err_free:
1787 srp_free_target_ib(target);
1788
1789err:
1790 scsi_host_put(target_host);
1791
1792 return ret;
1793}
1794
1795static CLASS_DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
1796
1797static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
1798{
1799 struct srp_host *host =
1800 container_of(class_dev, struct srp_host, class_dev);
1801
Roland Dreierf5358a12006-06-17 20:37:29 -07001802 return sprintf(buf, "%s\n", host->dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001803}
1804
1805static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1806
1807static ssize_t show_port(struct class_device *class_dev, char *buf)
1808{
1809 struct srp_host *host =
1810 container_of(class_dev, struct srp_host, class_dev);
1811
1812 return sprintf(buf, "%d\n", host->port);
1813}
1814
1815static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
1816
Roland Dreierf5358a12006-06-17 20:37:29 -07001817static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001818{
1819 struct srp_host *host;
1820
1821 host = kzalloc(sizeof *host, GFP_KERNEL);
1822 if (!host)
1823 return NULL;
1824
1825 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001826 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001827 init_completion(&host->released);
1828 host->dev = device;
1829 host->port = port;
1830
Roland Dreieraef9ec32005-11-02 14:07:13 -08001831 host->class_dev.class = &srp_class;
Roland Dreierf5358a12006-06-17 20:37:29 -07001832 host->class_dev.dev = device->dev->dma_device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001833 snprintf(host->class_dev.class_id, BUS_ID_SIZE, "srp-%s-%d",
Roland Dreierf5358a12006-06-17 20:37:29 -07001834 device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001835
1836 if (class_device_register(&host->class_dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07001837 goto free_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001838 if (class_device_create_file(&host->class_dev, &class_device_attr_add_target))
1839 goto err_class;
1840 if (class_device_create_file(&host->class_dev, &class_device_attr_ibdev))
1841 goto err_class;
1842 if (class_device_create_file(&host->class_dev, &class_device_attr_port))
1843 goto err_class;
1844
1845 return host;
1846
1847err_class:
1848 class_device_unregister(&host->class_dev);
1849
Roland Dreierf5358a12006-06-17 20:37:29 -07001850free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001851 kfree(host);
1852
1853 return NULL;
1854}
1855
1856static void srp_add_one(struct ib_device *device)
1857{
Roland Dreierf5358a12006-06-17 20:37:29 -07001858 struct srp_device *srp_dev;
1859 struct ib_device_attr *dev_attr;
1860 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001861 struct srp_host *host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001862 int s, e, p;
1863
Roland Dreierf5358a12006-06-17 20:37:29 -07001864 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
1865 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08001866 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001867
Roland Dreierf5358a12006-06-17 20:37:29 -07001868 if (ib_query_device(device, dev_attr)) {
1869 printk(KERN_WARNING PFX "Query device failed for %s\n",
1870 device->name);
1871 goto free_attr;
1872 }
1873
1874 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
1875 if (!srp_dev)
1876 goto free_attr;
1877
1878 /*
1879 * Use the smallest page size supported by the HCA, down to a
1880 * minimum of 512 bytes (which is the smallest sector that a
1881 * SCSI command will ever carry).
1882 */
1883 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
1884 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
1885 srp_dev->fmr_page_mask = ~((unsigned long) srp_dev->fmr_page_size - 1);
1886
1887 INIT_LIST_HEAD(&srp_dev->dev_list);
1888
1889 srp_dev->dev = device;
1890 srp_dev->pd = ib_alloc_pd(device);
1891 if (IS_ERR(srp_dev->pd))
1892 goto free_dev;
1893
1894 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
1895 IB_ACCESS_LOCAL_WRITE |
1896 IB_ACCESS_REMOTE_READ |
1897 IB_ACCESS_REMOTE_WRITE);
1898 if (IS_ERR(srp_dev->mr))
1899 goto err_pd;
1900
1901 memset(&fmr_param, 0, sizeof fmr_param);
1902 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
1903 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
1904 fmr_param.cache = 1;
1905 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
1906 fmr_param.page_shift = srp_dev->fmr_page_shift;
1907 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
1908 IB_ACCESS_REMOTE_WRITE |
1909 IB_ACCESS_REMOTE_READ);
1910
1911 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
1912 if (IS_ERR(srp_dev->fmr_pool))
1913 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001914
Tom Tucker07ebafb2006-08-03 16:02:42 -05001915 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001916 s = 0;
1917 e = 0;
1918 } else {
1919 s = 1;
1920 e = device->phys_port_cnt;
1921 }
1922
1923 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07001924 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001925 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07001926 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001927 }
1928
Roland Dreierf5358a12006-06-17 20:37:29 -07001929 ib_set_client_data(device, &srp_client, srp_dev);
1930
1931 goto free_attr;
1932
1933err_pd:
1934 ib_dealloc_pd(srp_dev->pd);
1935
1936free_dev:
1937 kfree(srp_dev);
1938
1939free_attr:
1940 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001941}
1942
1943static void srp_remove_one(struct ib_device *device)
1944{
Roland Dreierf5358a12006-06-17 20:37:29 -07001945 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001946 struct srp_host *host, *tmp_host;
1947 LIST_HEAD(target_list);
1948 struct srp_target_port *target, *tmp_target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001949
Roland Dreierf5358a12006-06-17 20:37:29 -07001950 srp_dev = ib_get_client_data(device, &srp_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001951
Roland Dreierf5358a12006-06-17 20:37:29 -07001952 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001953 class_device_unregister(&host->class_dev);
1954 /*
1955 * Wait for the sysfs entry to go away, so that no new
1956 * target ports can be created.
1957 */
1958 wait_for_completion(&host->released);
1959
1960 /*
1961 * Mark all target ports as removed, so we stop queueing
1962 * commands and don't try to reconnect.
1963 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001964 spin_lock(&host->target_lock);
Matthew Wilcox549c5fc22006-06-17 20:37:30 -07001965 list_for_each_entry(target, &host->target_list, list) {
Ishai Rabinovitz0c5b3952006-06-17 20:37:31 -07001966 spin_lock_irq(target->scsi_host->host_lock);
1967 target->state = SRP_TARGET_REMOVED;
1968 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001969 }
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001970 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001971
1972 /*
1973 * Wait for any reconnection tasks that may have
1974 * started before we marked our target ports as
1975 * removed, and any target port removal tasks.
1976 */
1977 flush_scheduled_work();
1978
1979 list_for_each_entry_safe(target, tmp_target,
1980 &host->target_list, list) {
1981 scsi_remove_host(target->scsi_host);
1982 srp_disconnect_target(target);
1983 ib_destroy_cm_id(target->cm_id);
1984 srp_free_target_ib(target);
1985 scsi_host_put(target->scsi_host);
1986 }
1987
Roland Dreieraef9ec32005-11-02 14:07:13 -08001988 kfree(host);
1989 }
1990
Roland Dreierf5358a12006-06-17 20:37:29 -07001991 if (srp_dev->fmr_pool)
1992 ib_destroy_fmr_pool(srp_dev->fmr_pool);
1993 ib_dereg_mr(srp_dev->mr);
1994 ib_dealloc_pd(srp_dev->pd);
1995
1996 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001997}
1998
1999static int __init srp_init_module(void)
2000{
2001 int ret;
2002
Vu Pham74b0a152006-06-17 20:37:32 -07002003 srp_template.sg_tablesize = srp_sg_tablesize;
2004 srp_max_iu_len = (sizeof (struct srp_cmd) +
2005 sizeof (struct srp_indirect_buf) +
2006 srp_sg_tablesize * 16);
2007
Roland Dreieraef9ec32005-11-02 14:07:13 -08002008 ret = class_register(&srp_class);
2009 if (ret) {
2010 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
2011 return ret;
2012 }
2013
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002014 ib_sa_register_client(&srp_sa_client);
2015
Roland Dreieraef9ec32005-11-02 14:07:13 -08002016 ret = ib_register_client(&srp_client);
2017 if (ret) {
2018 printk(KERN_ERR PFX "couldn't register IB client\n");
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002019 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002020 class_unregister(&srp_class);
2021 return ret;
2022 }
2023
2024 return 0;
2025}
2026
2027static void __exit srp_cleanup_module(void)
2028{
2029 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002030 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002031 class_unregister(&srp_class);
2032}
2033
2034module_init(srp_init_module);
2035module_exit(srp_cleanup_module);