blob: e7fa8b680cb60137314750759577b695820e7140 [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
80static void srp_add_one(struct ib_device *device);
81static void srp_remove_one(struct ib_device *device);
82static void srp_completion(struct ib_cq *cq, void *target_ptr);
83static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
84
85static struct ib_client srp_client = {
86 .name = "srp",
87 .add = srp_add_one,
88 .remove = srp_remove_one
89};
90
91static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
92{
93 return (struct srp_target_port *) host->hostdata;
94}
95
96static const char *srp_target_info(struct Scsi_Host *host)
97{
98 return host_to_target(host)->target_name;
99}
100
101static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
102 gfp_t gfp_mask,
103 enum dma_data_direction direction)
104{
105 struct srp_iu *iu;
106
107 iu = kmalloc(sizeof *iu, gfp_mask);
108 if (!iu)
109 goto out;
110
111 iu->buf = kzalloc(size, gfp_mask);
112 if (!iu->buf)
113 goto out_free_iu;
114
Roland Dreierf5358a12006-06-17 20:37:29 -0700115 iu->dma = dma_map_single(host->dev->dev->dma_device,
116 iu->buf, size, direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800117 if (dma_mapping_error(iu->dma))
118 goto out_free_buf;
119
120 iu->size = size;
121 iu->direction = direction;
122
123 return iu;
124
125out_free_buf:
126 kfree(iu->buf);
127out_free_iu:
128 kfree(iu);
129out:
130 return NULL;
131}
132
133static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
134{
135 if (!iu)
136 return;
137
Roland Dreierf5358a12006-06-17 20:37:29 -0700138 dma_unmap_single(host->dev->dev->dma_device,
139 iu->dma, iu->size, iu->direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800140 kfree(iu->buf);
141 kfree(iu);
142}
143
144static void srp_qp_event(struct ib_event *event, void *context)
145{
146 printk(KERN_ERR PFX "QP event %d\n", event->event);
147}
148
149static int srp_init_qp(struct srp_target_port *target,
150 struct ib_qp *qp)
151{
152 struct ib_qp_attr *attr;
153 int ret;
154
155 attr = kmalloc(sizeof *attr, GFP_KERNEL);
156 if (!attr)
157 return -ENOMEM;
158
Roland Dreierf5358a12006-06-17 20:37:29 -0700159 ret = ib_find_cached_pkey(target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800160 target->srp_host->port,
161 be16_to_cpu(target->path.pkey),
162 &attr->pkey_index);
163 if (ret)
164 goto out;
165
166 attr->qp_state = IB_QPS_INIT;
167 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
168 IB_ACCESS_REMOTE_WRITE);
169 attr->port_num = target->srp_host->port;
170
171 ret = ib_modify_qp(qp, attr,
172 IB_QP_STATE |
173 IB_QP_PKEY_INDEX |
174 IB_QP_ACCESS_FLAGS |
175 IB_QP_PORT);
176
177out:
178 kfree(attr);
179 return ret;
180}
181
182static int srp_create_target_ib(struct srp_target_port *target)
183{
184 struct ib_qp_init_attr *init_attr;
185 int ret;
186
187 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
188 if (!init_attr)
189 return -ENOMEM;
190
Roland Dreierf5358a12006-06-17 20:37:29 -0700191 target->cq = ib_create_cq(target->srp_host->dev->dev, srp_completion,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800192 NULL, target, SRP_CQ_SIZE);
193 if (IS_ERR(target->cq)) {
194 ret = PTR_ERR(target->cq);
195 goto out;
196 }
197
198 ib_req_notify_cq(target->cq, IB_CQ_NEXT_COMP);
199
200 init_attr->event_handler = srp_qp_event;
201 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
202 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
203 init_attr->cap.max_recv_sge = 1;
204 init_attr->cap.max_send_sge = 1;
205 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
206 init_attr->qp_type = IB_QPT_RC;
207 init_attr->send_cq = target->cq;
208 init_attr->recv_cq = target->cq;
209
Roland Dreierf5358a12006-06-17 20:37:29 -0700210 target->qp = ib_create_qp(target->srp_host->dev->pd, init_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800211 if (IS_ERR(target->qp)) {
212 ret = PTR_ERR(target->qp);
213 ib_destroy_cq(target->cq);
214 goto out;
215 }
216
217 ret = srp_init_qp(target, target->qp);
218 if (ret) {
219 ib_destroy_qp(target->qp);
220 ib_destroy_cq(target->cq);
221 goto out;
222 }
223
224out:
225 kfree(init_attr);
226 return ret;
227}
228
229static void srp_free_target_ib(struct srp_target_port *target)
230{
231 int i;
232
233 ib_destroy_qp(target->qp);
234 ib_destroy_cq(target->cq);
235
236 for (i = 0; i < SRP_RQ_SIZE; ++i)
237 srp_free_iu(target->srp_host, target->rx_ring[i]);
238 for (i = 0; i < SRP_SQ_SIZE + 1; ++i)
239 srp_free_iu(target->srp_host, target->tx_ring[i]);
240}
241
242static void srp_path_rec_completion(int status,
243 struct ib_sa_path_rec *pathrec,
244 void *target_ptr)
245{
246 struct srp_target_port *target = target_ptr;
247
248 target->status = status;
249 if (status)
250 printk(KERN_ERR PFX "Got failed path rec status %d\n", status);
251 else
252 target->path = *pathrec;
253 complete(&target->done);
254}
255
256static int srp_lookup_path(struct srp_target_port *target)
257{
258 target->path.numb_path = 1;
259
260 init_completion(&target->done);
261
Roland Dreierf5358a12006-06-17 20:37:29 -0700262 target->path_query_id = ib_sa_path_rec_get(target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800263 target->srp_host->port,
264 &target->path,
265 IB_SA_PATH_REC_DGID |
266 IB_SA_PATH_REC_SGID |
267 IB_SA_PATH_REC_NUMB_PATH |
268 IB_SA_PATH_REC_PKEY,
269 SRP_PATH_REC_TIMEOUT_MS,
270 GFP_KERNEL,
271 srp_path_rec_completion,
272 target, &target->path_query);
273 if (target->path_query_id < 0)
274 return target->path_query_id;
275
276 wait_for_completion(&target->done);
277
278 if (target->status < 0)
279 printk(KERN_WARNING PFX "Path record query failed\n");
280
281 return target->status;
282}
283
284static int srp_send_req(struct srp_target_port *target)
285{
286 struct {
287 struct ib_cm_req_param param;
288 struct srp_login_req priv;
289 } *req = NULL;
290 int status;
291
292 req = kzalloc(sizeof *req, GFP_KERNEL);
293 if (!req)
294 return -ENOMEM;
295
296 req->param.primary_path = &target->path;
297 req->param.alternate_path = NULL;
298 req->param.service_id = target->service_id;
299 req->param.qp_num = target->qp->qp_num;
300 req->param.qp_type = target->qp->qp_type;
301 req->param.private_data = &req->priv;
302 req->param.private_data_len = sizeof req->priv;
303 req->param.flow_control = 1;
304
305 get_random_bytes(&req->param.starting_psn, 4);
306 req->param.starting_psn &= 0xffffff;
307
308 /*
309 * Pick some arbitrary defaults here; we could make these
310 * module parameters if anyone cared about setting them.
311 */
312 req->param.responder_resources = 4;
313 req->param.remote_cm_response_timeout = 20;
314 req->param.local_cm_response_timeout = 20;
315 req->param.retry_count = 7;
316 req->param.rnr_retry_count = 7;
317 req->param.max_cm_retries = 15;
318
319 req->priv.opcode = SRP_LOGIN_REQ;
320 req->priv.tag = 0;
Vu Pham74b0a152006-06-17 20:37:32 -0700321 req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800322 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
323 SRP_BUF_FORMAT_INDIRECT);
Ramachandra K0c0450d2006-06-17 20:37:38 -0700324 /*
325 * In the published SRP specification (draft rev. 16a), the
326 * port identifier format is 8 bytes of ID extension followed
327 * by 8 bytes of GUID. Older drafts put the two halves in the
328 * opposite order, so that the GUID comes first.
329 *
330 * Targets conforming to these obsolete drafts can be
331 * recognized by the I/O Class they report.
332 */
333 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
334 memcpy(req->priv.initiator_port_id,
335 target->srp_host->initiator_port_id + 8, 8);
336 memcpy(req->priv.initiator_port_id + 8,
337 target->srp_host->initiator_port_id, 8);
338 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
339 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
340 } else {
341 memcpy(req->priv.initiator_port_id,
342 target->srp_host->initiator_port_id, 16);
343 memcpy(req->priv.target_port_id, &target->id_ext, 8);
344 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
345 }
346
Roland Dreieraef9ec32005-11-02 14:07:13 -0800347 /*
348 * Topspin/Cisco SRP targets will reject our login unless we
349 * zero out the first 8 bytes of our initiator port ID. The
350 * second 8 bytes must be our local node GUID, but we always
351 * use that anyway.
352 */
353 if (topspin_workarounds && !memcmp(&target->ioc_guid, topspin_oui, 3)) {
354 printk(KERN_DEBUG PFX "Topspin/Cisco initiator port ID workaround "
355 "activated for target GUID %016llx\n",
356 (unsigned long long) be64_to_cpu(target->ioc_guid));
357 memset(req->priv.initiator_port_id, 0, 8);
358 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800359
360 status = ib_send_cm_req(target->cm_id, &req->param);
361
362 kfree(req);
363
364 return status;
365}
366
367static void srp_disconnect_target(struct srp_target_port *target)
368{
369 /* XXX should send SRP_I_LOGOUT request */
370
371 init_completion(&target->done);
Roland Dreiere6581052006-05-17 09:13:21 -0700372 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
373 printk(KERN_DEBUG PFX "Sending CM DREQ failed\n");
374 return;
375 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800376 wait_for_completion(&target->done);
377}
378
379static void srp_remove_work(void *target_ptr)
380{
381 struct srp_target_port *target = target_ptr;
382
383 spin_lock_irq(target->scsi_host->host_lock);
384 if (target->state != SRP_TARGET_DEAD) {
385 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800386 return;
387 }
388 target->state = SRP_TARGET_REMOVED;
389 spin_unlock_irq(target->scsi_host->host_lock);
390
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700391 spin_lock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800392 list_del(&target->list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700393 spin_unlock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800394
395 scsi_remove_host(target->scsi_host);
396 ib_destroy_cm_id(target->cm_id);
397 srp_free_target_ib(target);
398 scsi_host_put(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800399}
400
401static int srp_connect_target(struct srp_target_port *target)
402{
403 int ret;
404
405 ret = srp_lookup_path(target);
406 if (ret)
407 return ret;
408
409 while (1) {
410 init_completion(&target->done);
411 ret = srp_send_req(target);
412 if (ret)
413 return ret;
414 wait_for_completion(&target->done);
415
416 /*
417 * The CM event handling code will set status to
418 * SRP_PORT_REDIRECT if we get a port redirect REJ
419 * back, or SRP_DLID_REDIRECT if we get a lid/qp
420 * redirect REJ back.
421 */
422 switch (target->status) {
423 case 0:
424 return 0;
425
426 case SRP_PORT_REDIRECT:
427 ret = srp_lookup_path(target);
428 if (ret)
429 return ret;
430 break;
431
432 case SRP_DLID_REDIRECT:
433 break;
434
435 default:
436 return target->status;
437 }
438 }
439}
440
Roland Dreierd945e1d2006-05-09 10:50:28 -0700441static void srp_unmap_data(struct scsi_cmnd *scmnd,
442 struct srp_target_port *target,
443 struct srp_request *req)
444{
445 struct scatterlist *scat;
446 int nents;
447
448 if (!scmnd->request_buffer ||
449 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
450 scmnd->sc_data_direction != DMA_FROM_DEVICE))
451 return;
452
Roland Dreierf5358a12006-06-17 20:37:29 -0700453 if (req->fmr) {
454 ib_fmr_pool_unmap(req->fmr);
455 req->fmr = NULL;
456 }
457
Roland Dreierd945e1d2006-05-09 10:50:28 -0700458 /*
459 * This handling of non-SG commands can be killed when the
460 * SCSI midlayer no longer generates non-SG commands.
461 */
462 if (likely(scmnd->use_sg)) {
463 nents = scmnd->use_sg;
464 scat = scmnd->request_buffer;
465 } else {
466 nents = 1;
467 scat = &req->fake_sg;
468 }
469
Roland Dreierf5358a12006-06-17 20:37:29 -0700470 dma_unmap_sg(target->srp_host->dev->dev->dma_device, scat, nents,
Roland Dreierd945e1d2006-05-09 10:50:28 -0700471 scmnd->sc_data_direction);
472}
473
Roland Dreieraef9ec32005-11-02 14:07:13 -0800474static int srp_reconnect_target(struct srp_target_port *target)
475{
476 struct ib_cm_id *new_cm_id;
477 struct ib_qp_attr qp_attr;
478 struct srp_request *req;
479 struct ib_wc wc;
480 int ret;
481 int i;
482
483 spin_lock_irq(target->scsi_host->host_lock);
484 if (target->state != SRP_TARGET_LIVE) {
485 spin_unlock_irq(target->scsi_host->host_lock);
486 return -EAGAIN;
487 }
488 target->state = SRP_TARGET_CONNECTING;
489 spin_unlock_irq(target->scsi_host->host_lock);
490
491 srp_disconnect_target(target);
492 /*
493 * Now get a new local CM ID so that we avoid confusing the
494 * target in case things are really fouled up.
495 */
Roland Dreierf5358a12006-06-17 20:37:29 -0700496 new_cm_id = ib_create_cm_id(target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800497 srp_cm_handler, target);
498 if (IS_ERR(new_cm_id)) {
499 ret = PTR_ERR(new_cm_id);
500 goto err;
501 }
502 ib_destroy_cm_id(target->cm_id);
503 target->cm_id = new_cm_id;
504
505 qp_attr.qp_state = IB_QPS_RESET;
506 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
507 if (ret)
508 goto err;
509
510 ret = srp_init_qp(target, target->qp);
511 if (ret)
512 goto err;
513
514 while (ib_poll_cq(target->cq, 1, &wc) > 0)
515 ; /* nothing */
516
517 list_for_each_entry(req, &target->req_queue, list) {
518 req->scmnd->result = DID_RESET << 16;
519 req->scmnd->scsi_done(req->scmnd);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700520 srp_unmap_data(req->scmnd, target, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800521 }
522
523 target->rx_head = 0;
524 target->tx_head = 0;
525 target->tx_tail = 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -0700526 INIT_LIST_HEAD(&target->free_reqs);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800527 INIT_LIST_HEAD(&target->req_queue);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700528 for (i = 0; i < SRP_SQ_SIZE; ++i)
529 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800530
531 ret = srp_connect_target(target);
532 if (ret)
533 goto err;
534
535 spin_lock_irq(target->scsi_host->host_lock);
536 if (target->state == SRP_TARGET_CONNECTING) {
537 ret = 0;
538 target->state = SRP_TARGET_LIVE;
539 } else
540 ret = -EAGAIN;
541 spin_unlock_irq(target->scsi_host->host_lock);
542
543 return ret;
544
545err:
546 printk(KERN_ERR PFX "reconnect failed (%d), removing target port.\n", ret);
547
548 /*
549 * We couldn't reconnect, so kill our target port off.
550 * However, we have to defer the real removal because we might
551 * be in the context of the SCSI error handler now, which
552 * would deadlock if we call scsi_remove_host().
553 */
554 spin_lock_irq(target->scsi_host->host_lock);
555 if (target->state == SRP_TARGET_CONNECTING) {
556 target->state = SRP_TARGET_DEAD;
557 INIT_WORK(&target->work, srp_remove_work, target);
558 schedule_work(&target->work);
559 }
560 spin_unlock_irq(target->scsi_host->host_lock);
561
562 return ret;
563}
564
Roland Dreierf5358a12006-06-17 20:37:29 -0700565static int srp_map_fmr(struct srp_device *dev, struct scatterlist *scat,
566 int sg_cnt, struct srp_request *req,
567 struct srp_direct_buf *buf)
568{
569 u64 io_addr = 0;
570 u64 *dma_pages;
571 u32 len;
572 int page_cnt;
573 int i, j;
574 int ret;
575
576 if (!dev->fmr_pool)
577 return -ENODEV;
578
579 len = page_cnt = 0;
580 for (i = 0; i < sg_cnt; ++i) {
581 if (sg_dma_address(&scat[i]) & ~dev->fmr_page_mask) {
582 if (i > 0)
583 return -EINVAL;
584 else
585 ++page_cnt;
586 }
587 if ((sg_dma_address(&scat[i]) + sg_dma_len(&scat[i])) &
588 ~dev->fmr_page_mask) {
589 if (i < sg_cnt - 1)
590 return -EINVAL;
591 else
592 ++page_cnt;
593 }
594
595 len += sg_dma_len(&scat[i]);
596 }
597
598 page_cnt += len >> dev->fmr_page_shift;
599 if (page_cnt > SRP_FMR_SIZE)
600 return -ENOMEM;
601
602 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
603 if (!dma_pages)
604 return -ENOMEM;
605
606 page_cnt = 0;
607 for (i = 0; i < sg_cnt; ++i)
608 for (j = 0; j < sg_dma_len(&scat[i]); j += dev->fmr_page_size)
609 dma_pages[page_cnt++] =
610 (sg_dma_address(&scat[i]) & dev->fmr_page_mask) + j;
611
612 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
613 dma_pages, page_cnt, &io_addr);
614 if (IS_ERR(req->fmr)) {
615 ret = PTR_ERR(req->fmr);
616 goto out;
617 }
618
619 buf->va = cpu_to_be64(sg_dma_address(&scat[0]) & ~dev->fmr_page_mask);
620 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
621 buf->len = cpu_to_be32(len);
622
623 ret = 0;
624
625out:
626 kfree(dma_pages);
627
628 return ret;
629}
630
Roland Dreieraef9ec32005-11-02 14:07:13 -0800631static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
632 struct srp_request *req)
633{
Roland Dreiercf368712006-03-24 15:47:26 -0800634 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800635 struct srp_cmd *cmd = req->cmd->buf;
Roland Dreiercf368712006-03-24 15:47:26 -0800636 int len, nents, count;
Roland Dreierf5358a12006-06-17 20:37:29 -0700637 u8 fmt = SRP_DATA_DESC_DIRECT;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800638
639 if (!scmnd->request_buffer || scmnd->sc_data_direction == DMA_NONE)
640 return sizeof (struct srp_cmd);
641
642 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
643 scmnd->sc_data_direction != DMA_TO_DEVICE) {
644 printk(KERN_WARNING PFX "Unhandled data direction %d\n",
645 scmnd->sc_data_direction);
646 return -EINVAL;
647 }
648
Roland Dreiercf368712006-03-24 15:47:26 -0800649 /*
650 * This handling of non-SG commands can be killed when the
651 * SCSI midlayer no longer generates non-SG commands.
652 */
653 if (likely(scmnd->use_sg)) {
654 nents = scmnd->use_sg;
655 scat = scmnd->request_buffer;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800656 } else {
Roland Dreiercf368712006-03-24 15:47:26 -0800657 nents = 1;
658 scat = &req->fake_sg;
659 sg_init_one(scat, scmnd->request_buffer, scmnd->request_bufflen);
660 }
661
Roland Dreierf5358a12006-06-17 20:37:29 -0700662 count = dma_map_sg(target->srp_host->dev->dev->dma_device,
663 scat, nents, scmnd->sc_data_direction);
664
665 fmt = SRP_DATA_DESC_DIRECT;
666 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800667
668 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700669 /*
670 * The midlayer only generated a single gather/scatter
671 * entry, or DMA mapping coalesced everything to a
672 * single entry. So a direct descriptor along with
673 * the DMA MR suffices.
674 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800675 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800676
Roland Dreiercf368712006-03-24 15:47:26 -0800677 buf->va = cpu_to_be64(sg_dma_address(scat));
Roland Dreierf5358a12006-06-17 20:37:29 -0700678 buf->key = cpu_to_be32(target->srp_host->dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800679 buf->len = cpu_to_be32(sg_dma_len(scat));
Roland Dreierf5358a12006-06-17 20:37:29 -0700680 } else if (srp_map_fmr(target->srp_host->dev, scat, count, req,
681 (void *) cmd->add_data)) {
682 /*
683 * FMR mapping failed, and the scatterlist has more
684 * than one entry. Generate an indirect memory
685 * descriptor.
686 */
Roland Dreiercf368712006-03-24 15:47:26 -0800687 struct srp_indirect_buf *buf = (void *) cmd->add_data;
688 u32 datalen = 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700689 int i;
Roland Dreiercf368712006-03-24 15:47:26 -0800690
691 fmt = SRP_DATA_DESC_INDIRECT;
Roland Dreierf5358a12006-06-17 20:37:29 -0700692 len = sizeof (struct srp_cmd) +
693 sizeof (struct srp_indirect_buf) +
694 count * sizeof (struct srp_direct_buf);
695
696 for (i = 0; i < count; ++i) {
697 buf->desc_list[i].va =
698 cpu_to_be64(sg_dma_address(&scat[i]));
699 buf->desc_list[i].key =
700 cpu_to_be32(target->srp_host->dev->mr->rkey);
701 buf->desc_list[i].len =
702 cpu_to_be32(sg_dma_len(&scat[i]));
703 datalen += sg_dma_len(&scat[i]);
704 }
Roland Dreiercf368712006-03-24 15:47:26 -0800705
706 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
707 cmd->data_out_desc_cnt = count;
708 else
709 cmd->data_in_desc_cnt = count;
710
Roland Dreierf5358a12006-06-17 20:37:29 -0700711 buf->table_desc.va =
712 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800713 buf->table_desc.key =
Roland Dreierf5358a12006-06-17 20:37:29 -0700714 cpu_to_be32(target->srp_host->dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800715 buf->table_desc.len =
716 cpu_to_be32(count * sizeof (struct srp_direct_buf));
717
Roland Dreiercf368712006-03-24 15:47:26 -0800718 buf->len = cpu_to_be32(datalen);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800719 }
720
721 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
722 cmd->buf_fmt = fmt << 4;
723 else
724 cmd->buf_fmt = fmt;
725
Roland Dreieraef9ec32005-11-02 14:07:13 -0800726 return len;
727}
728
Roland Dreierd945e1d2006-05-09 10:50:28 -0700729static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800730{
Roland Dreierd945e1d2006-05-09 10:50:28 -0700731 srp_unmap_data(req->scmnd, target, req);
732 list_move_tail(&req->list, &target->free_reqs);
Roland Dreierf80887d2006-04-19 11:40:10 -0700733}
734
Roland Dreieraef9ec32005-11-02 14:07:13 -0800735static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
736{
737 struct srp_request *req;
738 struct scsi_cmnd *scmnd;
739 unsigned long flags;
740 s32 delta;
741
742 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
743
744 spin_lock_irqsave(target->scsi_host->host_lock, flags);
745
746 target->req_lim += delta;
747
748 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
749
750 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
751 if (be32_to_cpu(rsp->resp_data_len) < 4)
752 req->tsk_status = -1;
753 else
754 req->tsk_status = rsp->data[3];
755 complete(&req->done);
756 } else {
Roland Dreierd945e1d2006-05-09 10:50:28 -0700757 scmnd = req->scmnd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800758 if (!scmnd)
759 printk(KERN_ERR "Null scmnd for RSP w/tag %016llx\n",
760 (unsigned long long) rsp->tag);
761 scmnd->result = rsp->status;
762
763 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
764 memcpy(scmnd->sense_buffer, rsp->data +
765 be32_to_cpu(rsp->resp_data_len),
766 min_t(int, be32_to_cpu(rsp->sense_data_len),
767 SCSI_SENSE_BUFFERSIZE));
768 }
769
770 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
771 scmnd->resid = be32_to_cpu(rsp->data_out_res_cnt);
772 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
773 scmnd->resid = be32_to_cpu(rsp->data_in_res_cnt);
774
Roland Dreieraef9ec32005-11-02 14:07:13 -0800775 if (!req->tsk_mgmt) {
Roland Dreieraef9ec32005-11-02 14:07:13 -0800776 scmnd->host_scribble = (void *) -1L;
777 scmnd->scsi_done(scmnd);
778
Roland Dreierd945e1d2006-05-09 10:50:28 -0700779 srp_remove_req(target, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800780 } else
781 req->cmd_done = 1;
782 }
783
784 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
785}
786
787static void srp_reconnect_work(void *target_ptr)
788{
789 struct srp_target_port *target = target_ptr;
790
791 srp_reconnect_target(target);
792}
793
794static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
795{
796 struct srp_iu *iu;
797 u8 opcode;
798
799 iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
800
Roland Dreierf5358a12006-06-17 20:37:29 -0700801 dma_sync_single_for_cpu(target->srp_host->dev->dev->dma_device, iu->dma,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800802 target->max_ti_iu_len, DMA_FROM_DEVICE);
803
804 opcode = *(u8 *) iu->buf;
805
806 if (0) {
807 int i;
808
809 printk(KERN_ERR PFX "recv completion, opcode 0x%02x\n", opcode);
810
811 for (i = 0; i < wc->byte_len; ++i) {
812 if (i % 8 == 0)
813 printk(KERN_ERR " [%02x] ", i);
814 printk(" %02x", ((u8 *) iu->buf)[i]);
815 if ((i + 1) % 8 == 0)
816 printk("\n");
817 }
818
819 if (wc->byte_len % 8)
820 printk("\n");
821 }
822
823 switch (opcode) {
824 case SRP_RSP:
825 srp_process_rsp(target, iu->buf);
826 break;
827
828 case SRP_T_LOGOUT:
829 /* XXX Handle target logout */
830 printk(KERN_WARNING PFX "Got target logout request\n");
831 break;
832
833 default:
834 printk(KERN_WARNING PFX "Unhandled SRP opcode 0x%02x\n", opcode);
835 break;
836 }
837
Roland Dreierf5358a12006-06-17 20:37:29 -0700838 dma_sync_single_for_device(target->srp_host->dev->dev->dma_device, iu->dma,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800839 target->max_ti_iu_len, DMA_FROM_DEVICE);
840}
841
842static void srp_completion(struct ib_cq *cq, void *target_ptr)
843{
844 struct srp_target_port *target = target_ptr;
845 struct ib_wc wc;
846 unsigned long flags;
847
848 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
849 while (ib_poll_cq(cq, 1, &wc) > 0) {
850 if (wc.status) {
851 printk(KERN_ERR PFX "failed %s status %d\n",
852 wc.wr_id & SRP_OP_RECV ? "receive" : "send",
853 wc.status);
854 spin_lock_irqsave(target->scsi_host->host_lock, flags);
855 if (target->state == SRP_TARGET_LIVE)
856 schedule_work(&target->work);
857 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
858 break;
859 }
860
861 if (wc.wr_id & SRP_OP_RECV)
862 srp_handle_recv(target, &wc);
863 else
864 ++target->tx_tail;
865 }
866}
867
868static int __srp_post_recv(struct srp_target_port *target)
869{
870 struct srp_iu *iu;
871 struct ib_sge list;
872 struct ib_recv_wr wr, *bad_wr;
873 unsigned int next;
874 int ret;
875
876 next = target->rx_head & (SRP_RQ_SIZE - 1);
877 wr.wr_id = next | SRP_OP_RECV;
878 iu = target->rx_ring[next];
879
880 list.addr = iu->dma;
881 list.length = iu->size;
Roland Dreierf5358a12006-06-17 20:37:29 -0700882 list.lkey = target->srp_host->dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800883
884 wr.next = NULL;
885 wr.sg_list = &list;
886 wr.num_sge = 1;
887
888 ret = ib_post_recv(target->qp, &wr, &bad_wr);
889 if (!ret)
890 ++target->rx_head;
891
892 return ret;
893}
894
895static int srp_post_recv(struct srp_target_port *target)
896{
897 unsigned long flags;
898 int ret;
899
900 spin_lock_irqsave(target->scsi_host->host_lock, flags);
901 ret = __srp_post_recv(target);
902 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
903
904 return ret;
905}
906
907/*
908 * Must be called with target->scsi_host->host_lock held to protect
Roland Dreier47f2bce2005-11-15 00:19:21 -0800909 * req_lim and tx_head. Lock cannot be dropped between call here and
910 * call to __srp_post_send().
Roland Dreieraef9ec32005-11-02 14:07:13 -0800911 */
912static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target)
913{
914 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
915 return NULL;
916
Roland Dreier6bfa24f2006-06-17 20:37:33 -0700917 if (unlikely(target->req_lim < 1))
918 ++target->zero_req_lim;
Roland Dreier47f2bce2005-11-15 00:19:21 -0800919
Roland Dreieraef9ec32005-11-02 14:07:13 -0800920 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
921}
922
923/*
924 * Must be called with target->scsi_host->host_lock held to protect
925 * req_lim and tx_head.
926 */
927static int __srp_post_send(struct srp_target_port *target,
928 struct srp_iu *iu, int len)
929{
930 struct ib_sge list;
931 struct ib_send_wr wr, *bad_wr;
932 int ret = 0;
933
Roland Dreieraef9ec32005-11-02 14:07:13 -0800934 list.addr = iu->dma;
935 list.length = len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700936 list.lkey = target->srp_host->dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800937
938 wr.next = NULL;
939 wr.wr_id = target->tx_head & SRP_SQ_SIZE;
940 wr.sg_list = &list;
941 wr.num_sge = 1;
942 wr.opcode = IB_WR_SEND;
943 wr.send_flags = IB_SEND_SIGNALED;
944
945 ret = ib_post_send(target->qp, &wr, &bad_wr);
946
947 if (!ret) {
948 ++target->tx_head;
949 --target->req_lim;
950 }
951
952 return ret;
953}
954
955static int srp_queuecommand(struct scsi_cmnd *scmnd,
956 void (*done)(struct scsi_cmnd *))
957{
958 struct srp_target_port *target = host_to_target(scmnd->device->host);
959 struct srp_request *req;
960 struct srp_iu *iu;
961 struct srp_cmd *cmd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800962 int len;
963
964 if (target->state == SRP_TARGET_CONNECTING)
965 goto err;
966
967 if (target->state == SRP_TARGET_DEAD ||
968 target->state == SRP_TARGET_REMOVED) {
969 scmnd->result = DID_BAD_TARGET << 16;
970 done(scmnd);
971 return 0;
972 }
973
974 iu = __srp_get_tx_iu(target);
975 if (!iu)
976 goto err;
977
Roland Dreierf5358a12006-06-17 20:37:29 -0700978 dma_sync_single_for_cpu(target->srp_host->dev->dev->dma_device, iu->dma,
Vu Pham74b0a152006-06-17 20:37:32 -0700979 srp_max_iu_len, DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800980
Roland Dreierd945e1d2006-05-09 10:50:28 -0700981 req = list_entry(target->free_reqs.next, struct srp_request, list);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800982
983 scmnd->scsi_done = done;
984 scmnd->result = 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -0700985 scmnd->host_scribble = (void *) (long) req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800986
987 cmd = iu->buf;
988 memset(cmd, 0, sizeof *cmd);
989
990 cmd->opcode = SRP_CMD;
991 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700992 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800993 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
994
Roland Dreieraef9ec32005-11-02 14:07:13 -0800995 req->scmnd = scmnd;
996 req->cmd = iu;
997 req->cmd_done = 0;
998 req->tsk_mgmt = NULL;
999
1000 len = srp_map_data(scmnd, target, req);
1001 if (len < 0) {
1002 printk(KERN_ERR PFX "Failed to map data\n");
1003 goto err;
1004 }
1005
1006 if (__srp_post_recv(target)) {
1007 printk(KERN_ERR PFX "Recv failed\n");
1008 goto err_unmap;
1009 }
1010
Roland Dreierf5358a12006-06-17 20:37:29 -07001011 dma_sync_single_for_device(target->srp_host->dev->dev->dma_device, iu->dma,
Vu Pham74b0a152006-06-17 20:37:32 -07001012 srp_max_iu_len, DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001013
1014 if (__srp_post_send(target, iu, len)) {
1015 printk(KERN_ERR PFX "Send failed\n");
1016 goto err_unmap;
1017 }
1018
Roland Dreierd945e1d2006-05-09 10:50:28 -07001019 list_move_tail(&req->list, &target->req_queue);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001020
1021 return 0;
1022
1023err_unmap:
1024 srp_unmap_data(scmnd, target, req);
1025
1026err:
1027 return SCSI_MLQUEUE_HOST_BUSY;
1028}
1029
1030static int srp_alloc_iu_bufs(struct srp_target_port *target)
1031{
1032 int i;
1033
1034 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1035 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1036 target->max_ti_iu_len,
1037 GFP_KERNEL, DMA_FROM_DEVICE);
1038 if (!target->rx_ring[i])
1039 goto err;
1040 }
1041
1042 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1043 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
Vu Pham74b0a152006-06-17 20:37:32 -07001044 srp_max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001045 GFP_KERNEL, DMA_TO_DEVICE);
1046 if (!target->tx_ring[i])
1047 goto err;
1048 }
1049
1050 return 0;
1051
1052err:
1053 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1054 srp_free_iu(target->srp_host, target->rx_ring[i]);
1055 target->rx_ring[i] = NULL;
1056 }
1057
1058 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1059 srp_free_iu(target->srp_host, target->tx_ring[i]);
1060 target->tx_ring[i] = NULL;
1061 }
1062
1063 return -ENOMEM;
1064}
1065
1066static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1067 struct ib_cm_event *event,
1068 struct srp_target_port *target)
1069{
1070 struct ib_class_port_info *cpi;
1071 int opcode;
1072
1073 switch (event->param.rej_rcvd.reason) {
1074 case IB_CM_REJ_PORT_CM_REDIRECT:
1075 cpi = event->param.rej_rcvd.ari;
1076 target->path.dlid = cpi->redirect_lid;
1077 target->path.pkey = cpi->redirect_pkey;
1078 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1079 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1080
1081 target->status = target->path.dlid ?
1082 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1083 break;
1084
1085 case IB_CM_REJ_PORT_REDIRECT:
1086 if (topspin_workarounds &&
1087 !memcmp(&target->ioc_guid, topspin_oui, 3)) {
1088 /*
1089 * Topspin/Cisco SRP gateways incorrectly send
1090 * reject reason code 25 when they mean 24
1091 * (port redirect).
1092 */
1093 memcpy(target->path.dgid.raw,
1094 event->param.rej_rcvd.ari, 16);
1095
1096 printk(KERN_DEBUG PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1097 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1098 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1099
1100 target->status = SRP_PORT_REDIRECT;
1101 } else {
1102 printk(KERN_WARNING " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1103 target->status = -ECONNRESET;
1104 }
1105 break;
1106
1107 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
1108 printk(KERN_WARNING " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1109 target->status = -ECONNRESET;
1110 break;
1111
1112 case IB_CM_REJ_CONSUMER_DEFINED:
1113 opcode = *(u8 *) event->private_data;
1114 if (opcode == SRP_LOGIN_REJ) {
1115 struct srp_login_rej *rej = event->private_data;
1116 u32 reason = be32_to_cpu(rej->reason);
1117
1118 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
1119 printk(KERN_WARNING PFX
1120 "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1121 else
1122 printk(KERN_WARNING PFX
1123 "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
1124 } else
1125 printk(KERN_WARNING " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1126 " opcode 0x%02x\n", opcode);
1127 target->status = -ECONNRESET;
1128 break;
1129
1130 default:
1131 printk(KERN_WARNING " REJ reason 0x%x\n",
1132 event->param.rej_rcvd.reason);
1133 target->status = -ECONNRESET;
1134 }
1135}
1136
1137static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1138{
1139 struct srp_target_port *target = cm_id->context;
1140 struct ib_qp_attr *qp_attr = NULL;
1141 int attr_mask = 0;
1142 int comp = 0;
1143 int opcode = 0;
1144
1145 switch (event->event) {
1146 case IB_CM_REQ_ERROR:
1147 printk(KERN_DEBUG PFX "Sending CM REQ failed\n");
1148 comp = 1;
1149 target->status = -ECONNRESET;
1150 break;
1151
1152 case IB_CM_REP_RECEIVED:
1153 comp = 1;
1154 opcode = *(u8 *) event->private_data;
1155
1156 if (opcode == SRP_LOGIN_RSP) {
1157 struct srp_login_rsp *rsp = event->private_data;
1158
1159 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1160 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1161
1162 target->scsi_host->can_queue = min(target->req_lim,
1163 target->scsi_host->can_queue);
1164 } else {
1165 printk(KERN_WARNING PFX "Unhandled RSP opcode %#x\n", opcode);
1166 target->status = -ECONNRESET;
1167 break;
1168 }
1169
1170 target->status = srp_alloc_iu_bufs(target);
1171 if (target->status)
1172 break;
1173
1174 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1175 if (!qp_attr) {
1176 target->status = -ENOMEM;
1177 break;
1178 }
1179
1180 qp_attr->qp_state = IB_QPS_RTR;
1181 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1182 if (target->status)
1183 break;
1184
1185 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1186 if (target->status)
1187 break;
1188
1189 target->status = srp_post_recv(target);
1190 if (target->status)
1191 break;
1192
1193 qp_attr->qp_state = IB_QPS_RTS;
1194 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1195 if (target->status)
1196 break;
1197
1198 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1199 if (target->status)
1200 break;
1201
1202 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1203 if (target->status)
1204 break;
1205
1206 break;
1207
1208 case IB_CM_REJ_RECEIVED:
1209 printk(KERN_DEBUG PFX "REJ received\n");
1210 comp = 1;
1211
1212 srp_cm_rej_handler(cm_id, event, target);
1213 break;
1214
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001215 case IB_CM_DREQ_RECEIVED:
1216 printk(KERN_WARNING PFX "DREQ received - connection closed\n");
1217 if (ib_send_cm_drep(cm_id, NULL, 0))
1218 printk(KERN_ERR PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001219 break;
1220
1221 case IB_CM_TIMEWAIT_EXIT:
1222 printk(KERN_ERR PFX "connection closed\n");
1223
1224 comp = 1;
1225 target->status = 0;
1226 break;
1227
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001228 case IB_CM_MRA_RECEIVED:
1229 case IB_CM_DREQ_ERROR:
1230 case IB_CM_DREP_RECEIVED:
1231 break;
1232
Roland Dreieraef9ec32005-11-02 14:07:13 -08001233 default:
1234 printk(KERN_WARNING PFX "Unhandled CM event %d\n", event->event);
1235 break;
1236 }
1237
1238 if (comp)
1239 complete(&target->done);
1240
1241 kfree(qp_attr);
1242
1243 return 0;
1244}
1245
Roland Dreierd945e1d2006-05-09 10:50:28 -07001246static int srp_send_tsk_mgmt(struct srp_target_port *target,
1247 struct srp_request *req, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001248{
Roland Dreieraef9ec32005-11-02 14:07:13 -08001249 struct srp_iu *iu;
1250 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001251
1252 spin_lock_irq(target->scsi_host->host_lock);
1253
Roland Dreier1285b3a2006-03-03 15:47:25 -08001254 if (target->state == SRP_TARGET_DEAD ||
1255 target->state == SRP_TARGET_REMOVED) {
Roland Dreierd945e1d2006-05-09 10:50:28 -07001256 req->scmnd->result = DID_BAD_TARGET << 16;
Roland Dreier1285b3a2006-03-03 15:47:25 -08001257 goto out;
1258 }
1259
Roland Dreieraef9ec32005-11-02 14:07:13 -08001260 init_completion(&req->done);
1261
1262 iu = __srp_get_tx_iu(target);
1263 if (!iu)
1264 goto out;
1265
1266 tsk_mgmt = iu->buf;
1267 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1268
1269 tsk_mgmt->opcode = SRP_TSK_MGMT;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001270 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1271 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001272 tsk_mgmt->tsk_mgmt_func = func;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001273 tsk_mgmt->task_tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001274
1275 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1276 goto out;
1277
1278 req->tsk_mgmt = iu;
1279
1280 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001281
Roland Dreieraef9ec32005-11-02 14:07:13 -08001282 if (!wait_for_completion_timeout(&req->done,
1283 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001284 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001285
Roland Dreierd945e1d2006-05-09 10:50:28 -07001286 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001287
1288out:
1289 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001290 return -1;
1291}
1292
1293static int srp_find_req(struct srp_target_port *target,
1294 struct scsi_cmnd *scmnd,
1295 struct srp_request **req)
1296{
1297 if (scmnd->host_scribble == (void *) -1L)
1298 return -1;
1299
1300 *req = &target->req_ring[(long) scmnd->host_scribble];
1301
1302 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001303}
1304
1305static int srp_abort(struct scsi_cmnd *scmnd)
1306{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001307 struct srp_target_port *target = host_to_target(scmnd->device->host);
1308 struct srp_request *req;
1309 int ret = SUCCESS;
1310
Roland Dreieraef9ec32005-11-02 14:07:13 -08001311 printk(KERN_ERR "SRP abort called\n");
1312
Roland Dreierd945e1d2006-05-09 10:50:28 -07001313 if (srp_find_req(target, scmnd, &req))
1314 return FAILED;
1315 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1316 return FAILED;
1317
1318 spin_lock_irq(target->scsi_host->host_lock);
1319
1320 if (req->cmd_done) {
1321 srp_remove_req(target, req);
1322 scmnd->scsi_done(scmnd);
1323 } else if (!req->tsk_status) {
1324 srp_remove_req(target, req);
1325 scmnd->result = DID_ABORT << 16;
1326 } else
1327 ret = FAILED;
1328
1329 spin_unlock_irq(target->scsi_host->host_lock);
1330
1331 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001332}
1333
1334static int srp_reset_device(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, *tmp;
1338
Roland Dreieraef9ec32005-11-02 14:07:13 -08001339 printk(KERN_ERR "SRP reset_device called\n");
1340
Roland Dreierd945e1d2006-05-09 10:50:28 -07001341 if (srp_find_req(target, scmnd, &req))
1342 return FAILED;
1343 if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1344 return FAILED;
1345 if (req->tsk_status)
1346 return FAILED;
1347
1348 spin_lock_irq(target->scsi_host->host_lock);
1349
1350 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
1351 if (req->scmnd->device == scmnd->device) {
1352 req->scmnd->result = DID_RESET << 16;
Ishai Rabinovitz093beac2006-05-17 09:20:48 -07001353 req->scmnd->scsi_done(req->scmnd);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001354 srp_remove_req(target, req);
1355 }
1356
1357 spin_unlock_irq(target->scsi_host->host_lock);
1358
1359 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001360}
1361
1362static int srp_reset_host(struct scsi_cmnd *scmnd)
1363{
1364 struct srp_target_port *target = host_to_target(scmnd->device->host);
1365 int ret = FAILED;
1366
1367 printk(KERN_ERR PFX "SRP reset_host called\n");
1368
1369 if (!srp_reconnect_target(target))
1370 ret = SUCCESS;
1371
1372 return ret;
1373}
1374
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001375static ssize_t show_id_ext(struct class_device *cdev, char *buf)
1376{
1377 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1378
1379 if (target->state == SRP_TARGET_DEAD ||
1380 target->state == SRP_TARGET_REMOVED)
1381 return -ENODEV;
1382
1383 return sprintf(buf, "0x%016llx\n",
1384 (unsigned long long) be64_to_cpu(target->id_ext));
1385}
1386
1387static ssize_t show_ioc_guid(struct class_device *cdev, char *buf)
1388{
1389 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1390
1391 if (target->state == SRP_TARGET_DEAD ||
1392 target->state == SRP_TARGET_REMOVED)
1393 return -ENODEV;
1394
1395 return sprintf(buf, "0x%016llx\n",
1396 (unsigned long long) be64_to_cpu(target->ioc_guid));
1397}
1398
1399static ssize_t show_service_id(struct class_device *cdev, char *buf)
1400{
1401 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1402
1403 if (target->state == SRP_TARGET_DEAD ||
1404 target->state == SRP_TARGET_REMOVED)
1405 return -ENODEV;
1406
1407 return sprintf(buf, "0x%016llx\n",
1408 (unsigned long long) be64_to_cpu(target->service_id));
1409}
1410
1411static ssize_t show_pkey(struct class_device *cdev, char *buf)
1412{
1413 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1414
1415 if (target->state == SRP_TARGET_DEAD ||
1416 target->state == SRP_TARGET_REMOVED)
1417 return -ENODEV;
1418
1419 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1420}
1421
1422static ssize_t show_dgid(struct class_device *cdev, char *buf)
1423{
1424 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1425
1426 if (target->state == SRP_TARGET_DEAD ||
1427 target->state == SRP_TARGET_REMOVED)
1428 return -ENODEV;
1429
1430 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1431 be16_to_cpu(((__be16 *) target->path.dgid.raw)[0]),
1432 be16_to_cpu(((__be16 *) target->path.dgid.raw)[1]),
1433 be16_to_cpu(((__be16 *) target->path.dgid.raw)[2]),
1434 be16_to_cpu(((__be16 *) target->path.dgid.raw)[3]),
1435 be16_to_cpu(((__be16 *) target->path.dgid.raw)[4]),
1436 be16_to_cpu(((__be16 *) target->path.dgid.raw)[5]),
1437 be16_to_cpu(((__be16 *) target->path.dgid.raw)[6]),
1438 be16_to_cpu(((__be16 *) target->path.dgid.raw)[7]));
1439}
1440
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001441static ssize_t show_zero_req_lim(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, "%d\n", target->zero_req_lim);
1450}
1451
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001452static CLASS_DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1453static CLASS_DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1454static CLASS_DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1455static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1456static CLASS_DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001457static CLASS_DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001458
1459static struct class_device_attribute *srp_host_attrs[] = {
1460 &class_device_attr_id_ext,
1461 &class_device_attr_ioc_guid,
1462 &class_device_attr_service_id,
1463 &class_device_attr_pkey,
1464 &class_device_attr_dgid,
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001465 &class_device_attr_zero_req_lim,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001466 NULL
1467};
1468
Roland Dreieraef9ec32005-11-02 14:07:13 -08001469static struct scsi_host_template srp_template = {
1470 .module = THIS_MODULE,
1471 .name = DRV_NAME,
1472 .info = srp_target_info,
1473 .queuecommand = srp_queuecommand,
1474 .eh_abort_handler = srp_abort,
1475 .eh_device_reset_handler = srp_reset_device,
1476 .eh_host_reset_handler = srp_reset_host,
1477 .can_queue = SRP_SQ_SIZE,
1478 .this_id = -1,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001479 .cmd_per_lun = SRP_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001480 .use_clustering = ENABLE_CLUSTERING,
1481 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001482};
1483
1484static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1485{
1486 sprintf(target->target_name, "SRP.T10:%016llX",
1487 (unsigned long long) be64_to_cpu(target->id_ext));
1488
Roland Dreierf5358a12006-06-17 20:37:29 -07001489 if (scsi_add_host(target->scsi_host, host->dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001490 return -ENODEV;
1491
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001492 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001493 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001494 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001495
1496 target->state = SRP_TARGET_LIVE;
1497
Roland Dreieraef9ec32005-11-02 14:07:13 -08001498 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07001499 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001500
1501 return 0;
1502}
1503
1504static void srp_release_class_dev(struct class_device *class_dev)
1505{
1506 struct srp_host *host =
1507 container_of(class_dev, struct srp_host, class_dev);
1508
1509 complete(&host->released);
1510}
1511
1512static struct class srp_class = {
1513 .name = "infiniband_srp",
1514 .release = srp_release_class_dev
1515};
1516
1517/*
1518 * Target ports are added by writing
1519 *
1520 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1521 * pkey=<P_Key>,service_id=<service ID>
1522 *
1523 * to the add_target sysfs attribute.
1524 */
1525enum {
1526 SRP_OPT_ERR = 0,
1527 SRP_OPT_ID_EXT = 1 << 0,
1528 SRP_OPT_IOC_GUID = 1 << 1,
1529 SRP_OPT_DGID = 1 << 2,
1530 SRP_OPT_PKEY = 1 << 3,
1531 SRP_OPT_SERVICE_ID = 1 << 4,
1532 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07001533 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450d2006-06-17 20:37:38 -07001534 SRP_OPT_IO_CLASS = 1 << 7,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001535 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1536 SRP_OPT_IOC_GUID |
1537 SRP_OPT_DGID |
1538 SRP_OPT_PKEY |
1539 SRP_OPT_SERVICE_ID),
1540};
1541
1542static match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07001543 { SRP_OPT_ID_EXT, "id_ext=%s" },
1544 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1545 { SRP_OPT_DGID, "dgid=%s" },
1546 { SRP_OPT_PKEY, "pkey=%x" },
1547 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1548 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1549 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450d2006-06-17 20:37:38 -07001550 { SRP_OPT_IO_CLASS, "io_class=%x" },
Vu Pham52fb2b502006-06-17 20:37:31 -07001551 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001552};
1553
1554static int srp_parse_options(const char *buf, struct srp_target_port *target)
1555{
1556 char *options, *sep_opt;
1557 char *p;
1558 char dgid[3];
1559 substring_t args[MAX_OPT_ARGS];
1560 int opt_mask = 0;
1561 int token;
1562 int ret = -EINVAL;
1563 int i;
1564
1565 options = kstrdup(buf, GFP_KERNEL);
1566 if (!options)
1567 return -ENOMEM;
1568
1569 sep_opt = options;
1570 while ((p = strsep(&sep_opt, ",")) != NULL) {
1571 if (!*p)
1572 continue;
1573
1574 token = match_token(p, srp_opt_tokens, args);
1575 opt_mask |= token;
1576
1577 switch (token) {
1578 case SRP_OPT_ID_EXT:
1579 p = match_strdup(args);
1580 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1581 kfree(p);
1582 break;
1583
1584 case SRP_OPT_IOC_GUID:
1585 p = match_strdup(args);
1586 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1587 kfree(p);
1588 break;
1589
1590 case SRP_OPT_DGID:
1591 p = match_strdup(args);
1592 if (strlen(p) != 32) {
1593 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07001594 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001595 goto out;
1596 }
1597
1598 for (i = 0; i < 16; ++i) {
1599 strlcpy(dgid, p + i * 2, 3);
1600 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1601 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08001602 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001603 break;
1604
1605 case SRP_OPT_PKEY:
1606 if (match_hex(args, &token)) {
1607 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1608 goto out;
1609 }
1610 target->path.pkey = cpu_to_be16(token);
1611 break;
1612
1613 case SRP_OPT_SERVICE_ID:
1614 p = match_strdup(args);
1615 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
1616 kfree(p);
1617 break;
1618
1619 case SRP_OPT_MAX_SECT:
1620 if (match_int(args, &token)) {
1621 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1622 goto out;
1623 }
1624 target->scsi_host->max_sectors = token;
1625 break;
1626
Vu Pham52fb2b502006-06-17 20:37:31 -07001627 case SRP_OPT_MAX_CMD_PER_LUN:
1628 if (match_int(args, &token)) {
1629 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1630 goto out;
1631 }
1632 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1633 break;
1634
Ramachandra K0c0450d2006-06-17 20:37:38 -07001635 case SRP_OPT_IO_CLASS:
1636 if (match_hex(args, &token)) {
1637 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1638 goto out;
1639 }
1640 if (token != SRP_REV10_IB_IO_CLASS &&
1641 token != SRP_REV16A_IB_IO_CLASS) {
1642 printk(KERN_WARNING PFX "unknown IO class parameter value"
1643 " %x specified (use %x or %x).\n",
1644 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1645 goto out;
1646 }
1647 target->io_class = token;
1648 break;
1649
Roland Dreieraef9ec32005-11-02 14:07:13 -08001650 default:
1651 printk(KERN_WARNING PFX "unknown parameter or missing value "
1652 "'%s' in target creation request\n", p);
1653 goto out;
1654 }
1655 }
1656
1657 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1658 ret = 0;
1659 else
1660 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1661 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1662 !(srp_opt_tokens[i].token & opt_mask))
1663 printk(KERN_WARNING PFX "target creation request is "
1664 "missing parameter '%s'\n",
1665 srp_opt_tokens[i].pattern);
1666
1667out:
1668 kfree(options);
1669 return ret;
1670}
1671
1672static ssize_t srp_create_target(struct class_device *class_dev,
1673 const char *buf, size_t count)
1674{
1675 struct srp_host *host =
1676 container_of(class_dev, struct srp_host, class_dev);
1677 struct Scsi_Host *target_host;
1678 struct srp_target_port *target;
1679 int ret;
1680 int i;
1681
1682 target_host = scsi_host_alloc(&srp_template,
1683 sizeof (struct srp_target_port));
1684 if (!target_host)
1685 return -ENOMEM;
1686
Roland Dreier5f068992005-11-11 14:06:01 -08001687 target_host->max_lun = SRP_MAX_LUN;
1688
Roland Dreieraef9ec32005-11-02 14:07:13 -08001689 target = host_to_target(target_host);
1690 memset(target, 0, sizeof *target);
1691
Ramachandra K0c0450d2006-06-17 20:37:38 -07001692 target->io_class = SRP_REV16A_IB_IO_CLASS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001693 target->scsi_host = target_host;
1694 target->srp_host = host;
1695
1696 INIT_WORK(&target->work, srp_reconnect_work, target);
1697
Roland Dreierd945e1d2006-05-09 10:50:28 -07001698 INIT_LIST_HEAD(&target->free_reqs);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001699 INIT_LIST_HEAD(&target->req_queue);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001700 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1701 target->req_ring[i].index = i;
1702 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1703 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001704
1705 ret = srp_parse_options(buf, target);
1706 if (ret)
1707 goto err;
1708
Roland Dreierf5358a12006-06-17 20:37:29 -07001709 ib_get_cached_gid(host->dev->dev, host->port, 0, &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001710
1711 printk(KERN_DEBUG PFX "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1712 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1713 (unsigned long long) be64_to_cpu(target->id_ext),
1714 (unsigned long long) be64_to_cpu(target->ioc_guid),
1715 be16_to_cpu(target->path.pkey),
1716 (unsigned long long) be64_to_cpu(target->service_id),
1717 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]),
1718 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]),
1719 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]),
1720 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]),
1721 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]),
1722 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]),
1723 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]),
1724 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14]));
1725
1726 ret = srp_create_target_ib(target);
1727 if (ret)
1728 goto err;
1729
Roland Dreierf5358a12006-06-17 20:37:29 -07001730 target->cm_id = ib_create_cm_id(host->dev->dev, srp_cm_handler, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001731 if (IS_ERR(target->cm_id)) {
1732 ret = PTR_ERR(target->cm_id);
1733 goto err_free;
1734 }
1735
1736 ret = srp_connect_target(target);
1737 if (ret) {
1738 printk(KERN_ERR PFX "Connection failed\n");
1739 goto err_cm_id;
1740 }
1741
1742 ret = srp_add_target(host, target);
1743 if (ret)
1744 goto err_disconnect;
1745
1746 return count;
1747
1748err_disconnect:
1749 srp_disconnect_target(target);
1750
1751err_cm_id:
1752 ib_destroy_cm_id(target->cm_id);
1753
1754err_free:
1755 srp_free_target_ib(target);
1756
1757err:
1758 scsi_host_put(target_host);
1759
1760 return ret;
1761}
1762
1763static CLASS_DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
1764
1765static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
1766{
1767 struct srp_host *host =
1768 container_of(class_dev, struct srp_host, class_dev);
1769
Roland Dreierf5358a12006-06-17 20:37:29 -07001770 return sprintf(buf, "%s\n", host->dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001771}
1772
1773static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1774
1775static ssize_t show_port(struct class_device *class_dev, char *buf)
1776{
1777 struct srp_host *host =
1778 container_of(class_dev, struct srp_host, class_dev);
1779
1780 return sprintf(buf, "%d\n", host->port);
1781}
1782
1783static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
1784
Roland Dreierf5358a12006-06-17 20:37:29 -07001785static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001786{
1787 struct srp_host *host;
1788
1789 host = kzalloc(sizeof *host, GFP_KERNEL);
1790 if (!host)
1791 return NULL;
1792
1793 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001794 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001795 init_completion(&host->released);
1796 host->dev = device;
1797 host->port = port;
1798
1799 host->initiator_port_id[7] = port;
Roland Dreierf5358a12006-06-17 20:37:29 -07001800 memcpy(host->initiator_port_id + 8, &device->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001801
1802 host->class_dev.class = &srp_class;
Roland Dreierf5358a12006-06-17 20:37:29 -07001803 host->class_dev.dev = device->dev->dma_device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001804 snprintf(host->class_dev.class_id, BUS_ID_SIZE, "srp-%s-%d",
Roland Dreierf5358a12006-06-17 20:37:29 -07001805 device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001806
1807 if (class_device_register(&host->class_dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07001808 goto free_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001809 if (class_device_create_file(&host->class_dev, &class_device_attr_add_target))
1810 goto err_class;
1811 if (class_device_create_file(&host->class_dev, &class_device_attr_ibdev))
1812 goto err_class;
1813 if (class_device_create_file(&host->class_dev, &class_device_attr_port))
1814 goto err_class;
1815
1816 return host;
1817
1818err_class:
1819 class_device_unregister(&host->class_dev);
1820
Roland Dreierf5358a12006-06-17 20:37:29 -07001821free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001822 kfree(host);
1823
1824 return NULL;
1825}
1826
1827static void srp_add_one(struct ib_device *device)
1828{
Roland Dreierf5358a12006-06-17 20:37:29 -07001829 struct srp_device *srp_dev;
1830 struct ib_device_attr *dev_attr;
1831 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001832 struct srp_host *host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001833 int s, e, p;
1834
Roland Dreierf5358a12006-06-17 20:37:29 -07001835 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
1836 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08001837 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001838
Roland Dreierf5358a12006-06-17 20:37:29 -07001839 if (ib_query_device(device, dev_attr)) {
1840 printk(KERN_WARNING PFX "Query device failed for %s\n",
1841 device->name);
1842 goto free_attr;
1843 }
1844
1845 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
1846 if (!srp_dev)
1847 goto free_attr;
1848
1849 /*
1850 * Use the smallest page size supported by the HCA, down to a
1851 * minimum of 512 bytes (which is the smallest sector that a
1852 * SCSI command will ever carry).
1853 */
1854 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
1855 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
1856 srp_dev->fmr_page_mask = ~((unsigned long) srp_dev->fmr_page_size - 1);
1857
1858 INIT_LIST_HEAD(&srp_dev->dev_list);
1859
1860 srp_dev->dev = device;
1861 srp_dev->pd = ib_alloc_pd(device);
1862 if (IS_ERR(srp_dev->pd))
1863 goto free_dev;
1864
1865 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
1866 IB_ACCESS_LOCAL_WRITE |
1867 IB_ACCESS_REMOTE_READ |
1868 IB_ACCESS_REMOTE_WRITE);
1869 if (IS_ERR(srp_dev->mr))
1870 goto err_pd;
1871
1872 memset(&fmr_param, 0, sizeof fmr_param);
1873 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
1874 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
1875 fmr_param.cache = 1;
1876 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
1877 fmr_param.page_shift = srp_dev->fmr_page_shift;
1878 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
1879 IB_ACCESS_REMOTE_WRITE |
1880 IB_ACCESS_REMOTE_READ);
1881
1882 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
1883 if (IS_ERR(srp_dev->fmr_pool))
1884 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001885
1886 if (device->node_type == IB_NODE_SWITCH) {
1887 s = 0;
1888 e = 0;
1889 } else {
1890 s = 1;
1891 e = device->phys_port_cnt;
1892 }
1893
1894 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07001895 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001896 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07001897 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001898 }
1899
Roland Dreierf5358a12006-06-17 20:37:29 -07001900 ib_set_client_data(device, &srp_client, srp_dev);
1901
1902 goto free_attr;
1903
1904err_pd:
1905 ib_dealloc_pd(srp_dev->pd);
1906
1907free_dev:
1908 kfree(srp_dev);
1909
1910free_attr:
1911 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001912}
1913
1914static void srp_remove_one(struct ib_device *device)
1915{
Roland Dreierf5358a12006-06-17 20:37:29 -07001916 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001917 struct srp_host *host, *tmp_host;
1918 LIST_HEAD(target_list);
1919 struct srp_target_port *target, *tmp_target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001920
Roland Dreierf5358a12006-06-17 20:37:29 -07001921 srp_dev = ib_get_client_data(device, &srp_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001922
Roland Dreierf5358a12006-06-17 20:37:29 -07001923 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001924 class_device_unregister(&host->class_dev);
1925 /*
1926 * Wait for the sysfs entry to go away, so that no new
1927 * target ports can be created.
1928 */
1929 wait_for_completion(&host->released);
1930
1931 /*
1932 * Mark all target ports as removed, so we stop queueing
1933 * commands and don't try to reconnect.
1934 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001935 spin_lock(&host->target_lock);
Matthew Wilcox549c5fc22006-06-17 20:37:30 -07001936 list_for_each_entry(target, &host->target_list, list) {
Ishai Rabinovitz0c5b3952006-06-17 20:37:31 -07001937 spin_lock_irq(target->scsi_host->host_lock);
1938 target->state = SRP_TARGET_REMOVED;
1939 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001940 }
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001941 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001942
1943 /*
1944 * Wait for any reconnection tasks that may have
1945 * started before we marked our target ports as
1946 * removed, and any target port removal tasks.
1947 */
1948 flush_scheduled_work();
1949
1950 list_for_each_entry_safe(target, tmp_target,
1951 &host->target_list, list) {
1952 scsi_remove_host(target->scsi_host);
1953 srp_disconnect_target(target);
1954 ib_destroy_cm_id(target->cm_id);
1955 srp_free_target_ib(target);
1956 scsi_host_put(target->scsi_host);
1957 }
1958
Roland Dreieraef9ec32005-11-02 14:07:13 -08001959 kfree(host);
1960 }
1961
Roland Dreierf5358a12006-06-17 20:37:29 -07001962 if (srp_dev->fmr_pool)
1963 ib_destroy_fmr_pool(srp_dev->fmr_pool);
1964 ib_dereg_mr(srp_dev->mr);
1965 ib_dealloc_pd(srp_dev->pd);
1966
1967 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001968}
1969
1970static int __init srp_init_module(void)
1971{
1972 int ret;
1973
Vu Pham74b0a152006-06-17 20:37:32 -07001974 srp_template.sg_tablesize = srp_sg_tablesize;
1975 srp_max_iu_len = (sizeof (struct srp_cmd) +
1976 sizeof (struct srp_indirect_buf) +
1977 srp_sg_tablesize * 16);
1978
Roland Dreieraef9ec32005-11-02 14:07:13 -08001979 ret = class_register(&srp_class);
1980 if (ret) {
1981 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
1982 return ret;
1983 }
1984
1985 ret = ib_register_client(&srp_client);
1986 if (ret) {
1987 printk(KERN_ERR PFX "couldn't register IB client\n");
1988 class_unregister(&srp_class);
1989 return ret;
1990 }
1991
1992 return 0;
1993}
1994
1995static void __exit srp_cleanup_module(void)
1996{
1997 ib_unregister_client(&srp_client);
1998 class_unregister(&srp_class);
1999}
2000
2001module_init(srp_init_module);
2002module_exit(srp_cleanup_module);