blob: 4ba790f54a243205d2a8b31c42ff81fdcbe0a7a5 [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);
324 memcpy(req->priv.initiator_port_id, target->srp_host->initiator_port_id, 16);
325 /*
326 * Topspin/Cisco SRP targets will reject our login unless we
327 * zero out the first 8 bytes of our initiator port ID. The
328 * second 8 bytes must be our local node GUID, but we always
329 * use that anyway.
330 */
331 if (topspin_workarounds && !memcmp(&target->ioc_guid, topspin_oui, 3)) {
332 printk(KERN_DEBUG PFX "Topspin/Cisco initiator port ID workaround "
333 "activated for target GUID %016llx\n",
334 (unsigned long long) be64_to_cpu(target->ioc_guid));
335 memset(req->priv.initiator_port_id, 0, 8);
336 }
337 memcpy(req->priv.target_port_id, &target->id_ext, 8);
338 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
339
340 status = ib_send_cm_req(target->cm_id, &req->param);
341
342 kfree(req);
343
344 return status;
345}
346
347static void srp_disconnect_target(struct srp_target_port *target)
348{
349 /* XXX should send SRP_I_LOGOUT request */
350
351 init_completion(&target->done);
Roland Dreiere6581052006-05-17 09:13:21 -0700352 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
353 printk(KERN_DEBUG PFX "Sending CM DREQ failed\n");
354 return;
355 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800356 wait_for_completion(&target->done);
357}
358
359static void srp_remove_work(void *target_ptr)
360{
361 struct srp_target_port *target = target_ptr;
362
363 spin_lock_irq(target->scsi_host->host_lock);
364 if (target->state != SRP_TARGET_DEAD) {
365 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800366 return;
367 }
368 target->state = SRP_TARGET_REMOVED;
369 spin_unlock_irq(target->scsi_host->host_lock);
370
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700371 spin_lock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800372 list_del(&target->list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700373 spin_unlock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800374
375 scsi_remove_host(target->scsi_host);
376 ib_destroy_cm_id(target->cm_id);
377 srp_free_target_ib(target);
378 scsi_host_put(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800379}
380
381static int srp_connect_target(struct srp_target_port *target)
382{
383 int ret;
384
385 ret = srp_lookup_path(target);
386 if (ret)
387 return ret;
388
389 while (1) {
390 init_completion(&target->done);
391 ret = srp_send_req(target);
392 if (ret)
393 return ret;
394 wait_for_completion(&target->done);
395
396 /*
397 * The CM event handling code will set status to
398 * SRP_PORT_REDIRECT if we get a port redirect REJ
399 * back, or SRP_DLID_REDIRECT if we get a lid/qp
400 * redirect REJ back.
401 */
402 switch (target->status) {
403 case 0:
404 return 0;
405
406 case SRP_PORT_REDIRECT:
407 ret = srp_lookup_path(target);
408 if (ret)
409 return ret;
410 break;
411
412 case SRP_DLID_REDIRECT:
413 break;
414
415 default:
416 return target->status;
417 }
418 }
419}
420
Roland Dreierd945e1d2006-05-09 10:50:28 -0700421static void srp_unmap_data(struct scsi_cmnd *scmnd,
422 struct srp_target_port *target,
423 struct srp_request *req)
424{
425 struct scatterlist *scat;
426 int nents;
427
428 if (!scmnd->request_buffer ||
429 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
430 scmnd->sc_data_direction != DMA_FROM_DEVICE))
431 return;
432
Roland Dreierf5358a12006-06-17 20:37:29 -0700433 if (req->fmr) {
434 ib_fmr_pool_unmap(req->fmr);
435 req->fmr = NULL;
436 }
437
Roland Dreierd945e1d2006-05-09 10:50:28 -0700438 /*
439 * This handling of non-SG commands can be killed when the
440 * SCSI midlayer no longer generates non-SG commands.
441 */
442 if (likely(scmnd->use_sg)) {
443 nents = scmnd->use_sg;
444 scat = scmnd->request_buffer;
445 } else {
446 nents = 1;
447 scat = &req->fake_sg;
448 }
449
Roland Dreierf5358a12006-06-17 20:37:29 -0700450 dma_unmap_sg(target->srp_host->dev->dev->dma_device, scat, nents,
Roland Dreierd945e1d2006-05-09 10:50:28 -0700451 scmnd->sc_data_direction);
452}
453
Roland Dreieraef9ec32005-11-02 14:07:13 -0800454static int srp_reconnect_target(struct srp_target_port *target)
455{
456 struct ib_cm_id *new_cm_id;
457 struct ib_qp_attr qp_attr;
458 struct srp_request *req;
459 struct ib_wc wc;
460 int ret;
461 int i;
462
463 spin_lock_irq(target->scsi_host->host_lock);
464 if (target->state != SRP_TARGET_LIVE) {
465 spin_unlock_irq(target->scsi_host->host_lock);
466 return -EAGAIN;
467 }
468 target->state = SRP_TARGET_CONNECTING;
469 spin_unlock_irq(target->scsi_host->host_lock);
470
471 srp_disconnect_target(target);
472 /*
473 * Now get a new local CM ID so that we avoid confusing the
474 * target in case things are really fouled up.
475 */
Roland Dreierf5358a12006-06-17 20:37:29 -0700476 new_cm_id = ib_create_cm_id(target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800477 srp_cm_handler, target);
478 if (IS_ERR(new_cm_id)) {
479 ret = PTR_ERR(new_cm_id);
480 goto err;
481 }
482 ib_destroy_cm_id(target->cm_id);
483 target->cm_id = new_cm_id;
484
485 qp_attr.qp_state = IB_QPS_RESET;
486 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
487 if (ret)
488 goto err;
489
490 ret = srp_init_qp(target, target->qp);
491 if (ret)
492 goto err;
493
494 while (ib_poll_cq(target->cq, 1, &wc) > 0)
495 ; /* nothing */
496
497 list_for_each_entry(req, &target->req_queue, list) {
498 req->scmnd->result = DID_RESET << 16;
499 req->scmnd->scsi_done(req->scmnd);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700500 srp_unmap_data(req->scmnd, target, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800501 }
502
503 target->rx_head = 0;
504 target->tx_head = 0;
505 target->tx_tail = 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -0700506 INIT_LIST_HEAD(&target->free_reqs);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800507 INIT_LIST_HEAD(&target->req_queue);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700508 for (i = 0; i < SRP_SQ_SIZE; ++i)
509 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800510
511 ret = srp_connect_target(target);
512 if (ret)
513 goto err;
514
515 spin_lock_irq(target->scsi_host->host_lock);
516 if (target->state == SRP_TARGET_CONNECTING) {
517 ret = 0;
518 target->state = SRP_TARGET_LIVE;
519 } else
520 ret = -EAGAIN;
521 spin_unlock_irq(target->scsi_host->host_lock);
522
523 return ret;
524
525err:
526 printk(KERN_ERR PFX "reconnect failed (%d), removing target port.\n", ret);
527
528 /*
529 * We couldn't reconnect, so kill our target port off.
530 * However, we have to defer the real removal because we might
531 * be in the context of the SCSI error handler now, which
532 * would deadlock if we call scsi_remove_host().
533 */
534 spin_lock_irq(target->scsi_host->host_lock);
535 if (target->state == SRP_TARGET_CONNECTING) {
536 target->state = SRP_TARGET_DEAD;
537 INIT_WORK(&target->work, srp_remove_work, target);
538 schedule_work(&target->work);
539 }
540 spin_unlock_irq(target->scsi_host->host_lock);
541
542 return ret;
543}
544
Roland Dreierf5358a12006-06-17 20:37:29 -0700545static int srp_map_fmr(struct srp_device *dev, struct scatterlist *scat,
546 int sg_cnt, struct srp_request *req,
547 struct srp_direct_buf *buf)
548{
549 u64 io_addr = 0;
550 u64 *dma_pages;
551 u32 len;
552 int page_cnt;
553 int i, j;
554 int ret;
555
556 if (!dev->fmr_pool)
557 return -ENODEV;
558
559 len = page_cnt = 0;
560 for (i = 0; i < sg_cnt; ++i) {
561 if (sg_dma_address(&scat[i]) & ~dev->fmr_page_mask) {
562 if (i > 0)
563 return -EINVAL;
564 else
565 ++page_cnt;
566 }
567 if ((sg_dma_address(&scat[i]) + sg_dma_len(&scat[i])) &
568 ~dev->fmr_page_mask) {
569 if (i < sg_cnt - 1)
570 return -EINVAL;
571 else
572 ++page_cnt;
573 }
574
575 len += sg_dma_len(&scat[i]);
576 }
577
578 page_cnt += len >> dev->fmr_page_shift;
579 if (page_cnt > SRP_FMR_SIZE)
580 return -ENOMEM;
581
582 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
583 if (!dma_pages)
584 return -ENOMEM;
585
586 page_cnt = 0;
587 for (i = 0; i < sg_cnt; ++i)
588 for (j = 0; j < sg_dma_len(&scat[i]); j += dev->fmr_page_size)
589 dma_pages[page_cnt++] =
590 (sg_dma_address(&scat[i]) & dev->fmr_page_mask) + j;
591
592 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
593 dma_pages, page_cnt, &io_addr);
594 if (IS_ERR(req->fmr)) {
595 ret = PTR_ERR(req->fmr);
596 goto out;
597 }
598
599 buf->va = cpu_to_be64(sg_dma_address(&scat[0]) & ~dev->fmr_page_mask);
600 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
601 buf->len = cpu_to_be32(len);
602
603 ret = 0;
604
605out:
606 kfree(dma_pages);
607
608 return ret;
609}
610
Roland Dreieraef9ec32005-11-02 14:07:13 -0800611static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
612 struct srp_request *req)
613{
Roland Dreiercf368712006-03-24 15:47:26 -0800614 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800615 struct srp_cmd *cmd = req->cmd->buf;
Roland Dreiercf368712006-03-24 15:47:26 -0800616 int len, nents, count;
Roland Dreierf5358a12006-06-17 20:37:29 -0700617 u8 fmt = SRP_DATA_DESC_DIRECT;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800618
619 if (!scmnd->request_buffer || scmnd->sc_data_direction == DMA_NONE)
620 return sizeof (struct srp_cmd);
621
622 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
623 scmnd->sc_data_direction != DMA_TO_DEVICE) {
624 printk(KERN_WARNING PFX "Unhandled data direction %d\n",
625 scmnd->sc_data_direction);
626 return -EINVAL;
627 }
628
Roland Dreiercf368712006-03-24 15:47:26 -0800629 /*
630 * This handling of non-SG commands can be killed when the
631 * SCSI midlayer no longer generates non-SG commands.
632 */
633 if (likely(scmnd->use_sg)) {
634 nents = scmnd->use_sg;
635 scat = scmnd->request_buffer;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800636 } else {
Roland Dreiercf368712006-03-24 15:47:26 -0800637 nents = 1;
638 scat = &req->fake_sg;
639 sg_init_one(scat, scmnd->request_buffer, scmnd->request_bufflen);
640 }
641
Roland Dreierf5358a12006-06-17 20:37:29 -0700642 count = dma_map_sg(target->srp_host->dev->dev->dma_device,
643 scat, nents, scmnd->sc_data_direction);
644
645 fmt = SRP_DATA_DESC_DIRECT;
646 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800647
648 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700649 /*
650 * The midlayer only generated a single gather/scatter
651 * entry, or DMA mapping coalesced everything to a
652 * single entry. So a direct descriptor along with
653 * the DMA MR suffices.
654 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800655 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800656
Roland Dreiercf368712006-03-24 15:47:26 -0800657 buf->va = cpu_to_be64(sg_dma_address(scat));
Roland Dreierf5358a12006-06-17 20:37:29 -0700658 buf->key = cpu_to_be32(target->srp_host->dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800659 buf->len = cpu_to_be32(sg_dma_len(scat));
Roland Dreierf5358a12006-06-17 20:37:29 -0700660 } else if (srp_map_fmr(target->srp_host->dev, scat, count, req,
661 (void *) cmd->add_data)) {
662 /*
663 * FMR mapping failed, and the scatterlist has more
664 * than one entry. Generate an indirect memory
665 * descriptor.
666 */
Roland Dreiercf368712006-03-24 15:47:26 -0800667 struct srp_indirect_buf *buf = (void *) cmd->add_data;
668 u32 datalen = 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700669 int i;
Roland Dreiercf368712006-03-24 15:47:26 -0800670
671 fmt = SRP_DATA_DESC_INDIRECT;
Roland Dreierf5358a12006-06-17 20:37:29 -0700672 len = sizeof (struct srp_cmd) +
673 sizeof (struct srp_indirect_buf) +
674 count * sizeof (struct srp_direct_buf);
675
676 for (i = 0; i < count; ++i) {
677 buf->desc_list[i].va =
678 cpu_to_be64(sg_dma_address(&scat[i]));
679 buf->desc_list[i].key =
680 cpu_to_be32(target->srp_host->dev->mr->rkey);
681 buf->desc_list[i].len =
682 cpu_to_be32(sg_dma_len(&scat[i]));
683 datalen += sg_dma_len(&scat[i]);
684 }
Roland Dreiercf368712006-03-24 15:47:26 -0800685
686 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
687 cmd->data_out_desc_cnt = count;
688 else
689 cmd->data_in_desc_cnt = count;
690
Roland Dreierf5358a12006-06-17 20:37:29 -0700691 buf->table_desc.va =
692 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800693 buf->table_desc.key =
Roland Dreierf5358a12006-06-17 20:37:29 -0700694 cpu_to_be32(target->srp_host->dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800695 buf->table_desc.len =
696 cpu_to_be32(count * sizeof (struct srp_direct_buf));
697
Roland Dreiercf368712006-03-24 15:47:26 -0800698 buf->len = cpu_to_be32(datalen);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800699 }
700
701 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
702 cmd->buf_fmt = fmt << 4;
703 else
704 cmd->buf_fmt = fmt;
705
Roland Dreieraef9ec32005-11-02 14:07:13 -0800706 return len;
707}
708
Roland Dreierd945e1d2006-05-09 10:50:28 -0700709static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800710{
Roland Dreierd945e1d2006-05-09 10:50:28 -0700711 srp_unmap_data(req->scmnd, target, req);
712 list_move_tail(&req->list, &target->free_reqs);
Roland Dreierf80887d2006-04-19 11:40:10 -0700713}
714
Roland Dreieraef9ec32005-11-02 14:07:13 -0800715static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
716{
717 struct srp_request *req;
718 struct scsi_cmnd *scmnd;
719 unsigned long flags;
720 s32 delta;
721
722 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
723
724 spin_lock_irqsave(target->scsi_host->host_lock, flags);
725
726 target->req_lim += delta;
727
728 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
729
730 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
731 if (be32_to_cpu(rsp->resp_data_len) < 4)
732 req->tsk_status = -1;
733 else
734 req->tsk_status = rsp->data[3];
735 complete(&req->done);
736 } else {
Roland Dreierd945e1d2006-05-09 10:50:28 -0700737 scmnd = req->scmnd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800738 if (!scmnd)
739 printk(KERN_ERR "Null scmnd for RSP w/tag %016llx\n",
740 (unsigned long long) rsp->tag);
741 scmnd->result = rsp->status;
742
743 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
744 memcpy(scmnd->sense_buffer, rsp->data +
745 be32_to_cpu(rsp->resp_data_len),
746 min_t(int, be32_to_cpu(rsp->sense_data_len),
747 SCSI_SENSE_BUFFERSIZE));
748 }
749
750 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
751 scmnd->resid = be32_to_cpu(rsp->data_out_res_cnt);
752 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
753 scmnd->resid = be32_to_cpu(rsp->data_in_res_cnt);
754
Roland Dreieraef9ec32005-11-02 14:07:13 -0800755 if (!req->tsk_mgmt) {
Roland Dreieraef9ec32005-11-02 14:07:13 -0800756 scmnd->host_scribble = (void *) -1L;
757 scmnd->scsi_done(scmnd);
758
Roland Dreierd945e1d2006-05-09 10:50:28 -0700759 srp_remove_req(target, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800760 } else
761 req->cmd_done = 1;
762 }
763
764 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
765}
766
767static void srp_reconnect_work(void *target_ptr)
768{
769 struct srp_target_port *target = target_ptr;
770
771 srp_reconnect_target(target);
772}
773
774static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
775{
776 struct srp_iu *iu;
777 u8 opcode;
778
779 iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
780
Roland Dreierf5358a12006-06-17 20:37:29 -0700781 dma_sync_single_for_cpu(target->srp_host->dev->dev->dma_device, iu->dma,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800782 target->max_ti_iu_len, DMA_FROM_DEVICE);
783
784 opcode = *(u8 *) iu->buf;
785
786 if (0) {
787 int i;
788
789 printk(KERN_ERR PFX "recv completion, opcode 0x%02x\n", opcode);
790
791 for (i = 0; i < wc->byte_len; ++i) {
792 if (i % 8 == 0)
793 printk(KERN_ERR " [%02x] ", i);
794 printk(" %02x", ((u8 *) iu->buf)[i]);
795 if ((i + 1) % 8 == 0)
796 printk("\n");
797 }
798
799 if (wc->byte_len % 8)
800 printk("\n");
801 }
802
803 switch (opcode) {
804 case SRP_RSP:
805 srp_process_rsp(target, iu->buf);
806 break;
807
808 case SRP_T_LOGOUT:
809 /* XXX Handle target logout */
810 printk(KERN_WARNING PFX "Got target logout request\n");
811 break;
812
813 default:
814 printk(KERN_WARNING PFX "Unhandled SRP opcode 0x%02x\n", opcode);
815 break;
816 }
817
Roland Dreierf5358a12006-06-17 20:37:29 -0700818 dma_sync_single_for_device(target->srp_host->dev->dev->dma_device, iu->dma,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800819 target->max_ti_iu_len, DMA_FROM_DEVICE);
820}
821
822static void srp_completion(struct ib_cq *cq, void *target_ptr)
823{
824 struct srp_target_port *target = target_ptr;
825 struct ib_wc wc;
826 unsigned long flags;
827
828 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
829 while (ib_poll_cq(cq, 1, &wc) > 0) {
830 if (wc.status) {
831 printk(KERN_ERR PFX "failed %s status %d\n",
832 wc.wr_id & SRP_OP_RECV ? "receive" : "send",
833 wc.status);
834 spin_lock_irqsave(target->scsi_host->host_lock, flags);
835 if (target->state == SRP_TARGET_LIVE)
836 schedule_work(&target->work);
837 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
838 break;
839 }
840
841 if (wc.wr_id & SRP_OP_RECV)
842 srp_handle_recv(target, &wc);
843 else
844 ++target->tx_tail;
845 }
846}
847
848static int __srp_post_recv(struct srp_target_port *target)
849{
850 struct srp_iu *iu;
851 struct ib_sge list;
852 struct ib_recv_wr wr, *bad_wr;
853 unsigned int next;
854 int ret;
855
856 next = target->rx_head & (SRP_RQ_SIZE - 1);
857 wr.wr_id = next | SRP_OP_RECV;
858 iu = target->rx_ring[next];
859
860 list.addr = iu->dma;
861 list.length = iu->size;
Roland Dreierf5358a12006-06-17 20:37:29 -0700862 list.lkey = target->srp_host->dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800863
864 wr.next = NULL;
865 wr.sg_list = &list;
866 wr.num_sge = 1;
867
868 ret = ib_post_recv(target->qp, &wr, &bad_wr);
869 if (!ret)
870 ++target->rx_head;
871
872 return ret;
873}
874
875static int srp_post_recv(struct srp_target_port *target)
876{
877 unsigned long flags;
878 int ret;
879
880 spin_lock_irqsave(target->scsi_host->host_lock, flags);
881 ret = __srp_post_recv(target);
882 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
883
884 return ret;
885}
886
887/*
888 * Must be called with target->scsi_host->host_lock held to protect
Roland Dreier47f2bce2005-11-15 00:19:21 -0800889 * req_lim and tx_head. Lock cannot be dropped between call here and
890 * call to __srp_post_send().
Roland Dreieraef9ec32005-11-02 14:07:13 -0800891 */
892static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target)
893{
894 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
895 return NULL;
896
Roland Dreier47f2bce2005-11-15 00:19:21 -0800897 if (unlikely(target->req_lim < 1)) {
898 if (printk_ratelimit())
899 printk(KERN_DEBUG PFX "Target has req_lim %d\n",
900 target->req_lim);
901 return NULL;
902 }
903
Roland Dreieraef9ec32005-11-02 14:07:13 -0800904 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
905}
906
907/*
908 * Must be called with target->scsi_host->host_lock held to protect
909 * req_lim and tx_head.
910 */
911static int __srp_post_send(struct srp_target_port *target,
912 struct srp_iu *iu, int len)
913{
914 struct ib_sge list;
915 struct ib_send_wr wr, *bad_wr;
916 int ret = 0;
917
Roland Dreieraef9ec32005-11-02 14:07:13 -0800918 list.addr = iu->dma;
919 list.length = len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700920 list.lkey = target->srp_host->dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800921
922 wr.next = NULL;
923 wr.wr_id = target->tx_head & SRP_SQ_SIZE;
924 wr.sg_list = &list;
925 wr.num_sge = 1;
926 wr.opcode = IB_WR_SEND;
927 wr.send_flags = IB_SEND_SIGNALED;
928
929 ret = ib_post_send(target->qp, &wr, &bad_wr);
930
931 if (!ret) {
932 ++target->tx_head;
933 --target->req_lim;
934 }
935
936 return ret;
937}
938
939static int srp_queuecommand(struct scsi_cmnd *scmnd,
940 void (*done)(struct scsi_cmnd *))
941{
942 struct srp_target_port *target = host_to_target(scmnd->device->host);
943 struct srp_request *req;
944 struct srp_iu *iu;
945 struct srp_cmd *cmd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800946 int len;
947
948 if (target->state == SRP_TARGET_CONNECTING)
949 goto err;
950
951 if (target->state == SRP_TARGET_DEAD ||
952 target->state == SRP_TARGET_REMOVED) {
953 scmnd->result = DID_BAD_TARGET << 16;
954 done(scmnd);
955 return 0;
956 }
957
958 iu = __srp_get_tx_iu(target);
959 if (!iu)
960 goto err;
961
Roland Dreierf5358a12006-06-17 20:37:29 -0700962 dma_sync_single_for_cpu(target->srp_host->dev->dev->dma_device, iu->dma,
Vu Pham74b0a152006-06-17 20:37:32 -0700963 srp_max_iu_len, DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800964
Roland Dreierd945e1d2006-05-09 10:50:28 -0700965 req = list_entry(target->free_reqs.next, struct srp_request, list);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800966
967 scmnd->scsi_done = done;
968 scmnd->result = 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -0700969 scmnd->host_scribble = (void *) (long) req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800970
971 cmd = iu->buf;
972 memset(cmd, 0, sizeof *cmd);
973
974 cmd->opcode = SRP_CMD;
975 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700976 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800977 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
978
Roland Dreieraef9ec32005-11-02 14:07:13 -0800979 req->scmnd = scmnd;
980 req->cmd = iu;
981 req->cmd_done = 0;
982 req->tsk_mgmt = NULL;
983
984 len = srp_map_data(scmnd, target, req);
985 if (len < 0) {
986 printk(KERN_ERR PFX "Failed to map data\n");
987 goto err;
988 }
989
990 if (__srp_post_recv(target)) {
991 printk(KERN_ERR PFX "Recv failed\n");
992 goto err_unmap;
993 }
994
Roland Dreierf5358a12006-06-17 20:37:29 -0700995 dma_sync_single_for_device(target->srp_host->dev->dev->dma_device, iu->dma,
Vu Pham74b0a152006-06-17 20:37:32 -0700996 srp_max_iu_len, DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800997
998 if (__srp_post_send(target, iu, len)) {
999 printk(KERN_ERR PFX "Send failed\n");
1000 goto err_unmap;
1001 }
1002
Roland Dreierd945e1d2006-05-09 10:50:28 -07001003 list_move_tail(&req->list, &target->req_queue);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001004
1005 return 0;
1006
1007err_unmap:
1008 srp_unmap_data(scmnd, target, req);
1009
1010err:
1011 return SCSI_MLQUEUE_HOST_BUSY;
1012}
1013
1014static int srp_alloc_iu_bufs(struct srp_target_port *target)
1015{
1016 int i;
1017
1018 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1019 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1020 target->max_ti_iu_len,
1021 GFP_KERNEL, DMA_FROM_DEVICE);
1022 if (!target->rx_ring[i])
1023 goto err;
1024 }
1025
1026 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1027 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
Vu Pham74b0a152006-06-17 20:37:32 -07001028 srp_max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001029 GFP_KERNEL, DMA_TO_DEVICE);
1030 if (!target->tx_ring[i])
1031 goto err;
1032 }
1033
1034 return 0;
1035
1036err:
1037 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1038 srp_free_iu(target->srp_host, target->rx_ring[i]);
1039 target->rx_ring[i] = NULL;
1040 }
1041
1042 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1043 srp_free_iu(target->srp_host, target->tx_ring[i]);
1044 target->tx_ring[i] = NULL;
1045 }
1046
1047 return -ENOMEM;
1048}
1049
1050static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1051 struct ib_cm_event *event,
1052 struct srp_target_port *target)
1053{
1054 struct ib_class_port_info *cpi;
1055 int opcode;
1056
1057 switch (event->param.rej_rcvd.reason) {
1058 case IB_CM_REJ_PORT_CM_REDIRECT:
1059 cpi = event->param.rej_rcvd.ari;
1060 target->path.dlid = cpi->redirect_lid;
1061 target->path.pkey = cpi->redirect_pkey;
1062 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1063 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1064
1065 target->status = target->path.dlid ?
1066 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1067 break;
1068
1069 case IB_CM_REJ_PORT_REDIRECT:
1070 if (topspin_workarounds &&
1071 !memcmp(&target->ioc_guid, topspin_oui, 3)) {
1072 /*
1073 * Topspin/Cisco SRP gateways incorrectly send
1074 * reject reason code 25 when they mean 24
1075 * (port redirect).
1076 */
1077 memcpy(target->path.dgid.raw,
1078 event->param.rej_rcvd.ari, 16);
1079
1080 printk(KERN_DEBUG PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1081 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1082 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1083
1084 target->status = SRP_PORT_REDIRECT;
1085 } else {
1086 printk(KERN_WARNING " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1087 target->status = -ECONNRESET;
1088 }
1089 break;
1090
1091 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
1092 printk(KERN_WARNING " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1093 target->status = -ECONNRESET;
1094 break;
1095
1096 case IB_CM_REJ_CONSUMER_DEFINED:
1097 opcode = *(u8 *) event->private_data;
1098 if (opcode == SRP_LOGIN_REJ) {
1099 struct srp_login_rej *rej = event->private_data;
1100 u32 reason = be32_to_cpu(rej->reason);
1101
1102 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
1103 printk(KERN_WARNING PFX
1104 "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1105 else
1106 printk(KERN_WARNING PFX
1107 "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
1108 } else
1109 printk(KERN_WARNING " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1110 " opcode 0x%02x\n", opcode);
1111 target->status = -ECONNRESET;
1112 break;
1113
1114 default:
1115 printk(KERN_WARNING " REJ reason 0x%x\n",
1116 event->param.rej_rcvd.reason);
1117 target->status = -ECONNRESET;
1118 }
1119}
1120
1121static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1122{
1123 struct srp_target_port *target = cm_id->context;
1124 struct ib_qp_attr *qp_attr = NULL;
1125 int attr_mask = 0;
1126 int comp = 0;
1127 int opcode = 0;
1128
1129 switch (event->event) {
1130 case IB_CM_REQ_ERROR:
1131 printk(KERN_DEBUG PFX "Sending CM REQ failed\n");
1132 comp = 1;
1133 target->status = -ECONNRESET;
1134 break;
1135
1136 case IB_CM_REP_RECEIVED:
1137 comp = 1;
1138 opcode = *(u8 *) event->private_data;
1139
1140 if (opcode == SRP_LOGIN_RSP) {
1141 struct srp_login_rsp *rsp = event->private_data;
1142
1143 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1144 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1145
1146 target->scsi_host->can_queue = min(target->req_lim,
1147 target->scsi_host->can_queue);
1148 } else {
1149 printk(KERN_WARNING PFX "Unhandled RSP opcode %#x\n", opcode);
1150 target->status = -ECONNRESET;
1151 break;
1152 }
1153
1154 target->status = srp_alloc_iu_bufs(target);
1155 if (target->status)
1156 break;
1157
1158 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1159 if (!qp_attr) {
1160 target->status = -ENOMEM;
1161 break;
1162 }
1163
1164 qp_attr->qp_state = IB_QPS_RTR;
1165 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1166 if (target->status)
1167 break;
1168
1169 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1170 if (target->status)
1171 break;
1172
1173 target->status = srp_post_recv(target);
1174 if (target->status)
1175 break;
1176
1177 qp_attr->qp_state = IB_QPS_RTS;
1178 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1179 if (target->status)
1180 break;
1181
1182 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1183 if (target->status)
1184 break;
1185
1186 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1187 if (target->status)
1188 break;
1189
1190 break;
1191
1192 case IB_CM_REJ_RECEIVED:
1193 printk(KERN_DEBUG PFX "REJ received\n");
1194 comp = 1;
1195
1196 srp_cm_rej_handler(cm_id, event, target);
1197 break;
1198
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001199 case IB_CM_DREQ_RECEIVED:
1200 printk(KERN_WARNING PFX "DREQ received - connection closed\n");
1201 if (ib_send_cm_drep(cm_id, NULL, 0))
1202 printk(KERN_ERR PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001203 break;
1204
1205 case IB_CM_TIMEWAIT_EXIT:
1206 printk(KERN_ERR PFX "connection closed\n");
1207
1208 comp = 1;
1209 target->status = 0;
1210 break;
1211
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001212 case IB_CM_MRA_RECEIVED:
1213 case IB_CM_DREQ_ERROR:
1214 case IB_CM_DREP_RECEIVED:
1215 break;
1216
Roland Dreieraef9ec32005-11-02 14:07:13 -08001217 default:
1218 printk(KERN_WARNING PFX "Unhandled CM event %d\n", event->event);
1219 break;
1220 }
1221
1222 if (comp)
1223 complete(&target->done);
1224
1225 kfree(qp_attr);
1226
1227 return 0;
1228}
1229
Roland Dreierd945e1d2006-05-09 10:50:28 -07001230static int srp_send_tsk_mgmt(struct srp_target_port *target,
1231 struct srp_request *req, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001232{
Roland Dreieraef9ec32005-11-02 14:07:13 -08001233 struct srp_iu *iu;
1234 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001235
1236 spin_lock_irq(target->scsi_host->host_lock);
1237
Roland Dreier1285b3a2006-03-03 15:47:25 -08001238 if (target->state == SRP_TARGET_DEAD ||
1239 target->state == SRP_TARGET_REMOVED) {
Roland Dreierd945e1d2006-05-09 10:50:28 -07001240 req->scmnd->result = DID_BAD_TARGET << 16;
Roland Dreier1285b3a2006-03-03 15:47:25 -08001241 goto out;
1242 }
1243
Roland Dreieraef9ec32005-11-02 14:07:13 -08001244 init_completion(&req->done);
1245
1246 iu = __srp_get_tx_iu(target);
1247 if (!iu)
1248 goto out;
1249
1250 tsk_mgmt = iu->buf;
1251 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1252
1253 tsk_mgmt->opcode = SRP_TSK_MGMT;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001254 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1255 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001256 tsk_mgmt->tsk_mgmt_func = func;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001257 tsk_mgmt->task_tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001258
1259 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1260 goto out;
1261
1262 req->tsk_mgmt = iu;
1263
1264 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001265
Roland Dreieraef9ec32005-11-02 14:07:13 -08001266 if (!wait_for_completion_timeout(&req->done,
1267 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001268 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001269
Roland Dreierd945e1d2006-05-09 10:50:28 -07001270 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001271
1272out:
1273 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001274 return -1;
1275}
1276
1277static int srp_find_req(struct srp_target_port *target,
1278 struct scsi_cmnd *scmnd,
1279 struct srp_request **req)
1280{
1281 if (scmnd->host_scribble == (void *) -1L)
1282 return -1;
1283
1284 *req = &target->req_ring[(long) scmnd->host_scribble];
1285
1286 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001287}
1288
1289static int srp_abort(struct scsi_cmnd *scmnd)
1290{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001291 struct srp_target_port *target = host_to_target(scmnd->device->host);
1292 struct srp_request *req;
1293 int ret = SUCCESS;
1294
Roland Dreieraef9ec32005-11-02 14:07:13 -08001295 printk(KERN_ERR "SRP abort called\n");
1296
Roland Dreierd945e1d2006-05-09 10:50:28 -07001297 if (srp_find_req(target, scmnd, &req))
1298 return FAILED;
1299 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1300 return FAILED;
1301
1302 spin_lock_irq(target->scsi_host->host_lock);
1303
1304 if (req->cmd_done) {
1305 srp_remove_req(target, req);
1306 scmnd->scsi_done(scmnd);
1307 } else if (!req->tsk_status) {
1308 srp_remove_req(target, req);
1309 scmnd->result = DID_ABORT << 16;
1310 } else
1311 ret = FAILED;
1312
1313 spin_unlock_irq(target->scsi_host->host_lock);
1314
1315 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001316}
1317
1318static int srp_reset_device(struct scsi_cmnd *scmnd)
1319{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001320 struct srp_target_port *target = host_to_target(scmnd->device->host);
1321 struct srp_request *req, *tmp;
1322
Roland Dreieraef9ec32005-11-02 14:07:13 -08001323 printk(KERN_ERR "SRP reset_device called\n");
1324
Roland Dreierd945e1d2006-05-09 10:50:28 -07001325 if (srp_find_req(target, scmnd, &req))
1326 return FAILED;
1327 if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1328 return FAILED;
1329 if (req->tsk_status)
1330 return FAILED;
1331
1332 spin_lock_irq(target->scsi_host->host_lock);
1333
1334 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
1335 if (req->scmnd->device == scmnd->device) {
1336 req->scmnd->result = DID_RESET << 16;
Ishai Rabinovitz093beac2006-05-17 09:20:48 -07001337 req->scmnd->scsi_done(req->scmnd);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001338 srp_remove_req(target, req);
1339 }
1340
1341 spin_unlock_irq(target->scsi_host->host_lock);
1342
1343 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001344}
1345
1346static int srp_reset_host(struct scsi_cmnd *scmnd)
1347{
1348 struct srp_target_port *target = host_to_target(scmnd->device->host);
1349 int ret = FAILED;
1350
1351 printk(KERN_ERR PFX "SRP reset_host called\n");
1352
1353 if (!srp_reconnect_target(target))
1354 ret = SUCCESS;
1355
1356 return ret;
1357}
1358
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001359static ssize_t show_id_ext(struct class_device *cdev, char *buf)
1360{
1361 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1362
1363 if (target->state == SRP_TARGET_DEAD ||
1364 target->state == SRP_TARGET_REMOVED)
1365 return -ENODEV;
1366
1367 return sprintf(buf, "0x%016llx\n",
1368 (unsigned long long) be64_to_cpu(target->id_ext));
1369}
1370
1371static ssize_t show_ioc_guid(struct class_device *cdev, char *buf)
1372{
1373 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1374
1375 if (target->state == SRP_TARGET_DEAD ||
1376 target->state == SRP_TARGET_REMOVED)
1377 return -ENODEV;
1378
1379 return sprintf(buf, "0x%016llx\n",
1380 (unsigned long long) be64_to_cpu(target->ioc_guid));
1381}
1382
1383static ssize_t show_service_id(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->service_id));
1393}
1394
1395static ssize_t show_pkey(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%04x\n", be16_to_cpu(target->path.pkey));
1404}
1405
1406static ssize_t show_dgid(struct class_device *cdev, char *buf)
1407{
1408 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1409
1410 if (target->state == SRP_TARGET_DEAD ||
1411 target->state == SRP_TARGET_REMOVED)
1412 return -ENODEV;
1413
1414 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1415 be16_to_cpu(((__be16 *) target->path.dgid.raw)[0]),
1416 be16_to_cpu(((__be16 *) target->path.dgid.raw)[1]),
1417 be16_to_cpu(((__be16 *) target->path.dgid.raw)[2]),
1418 be16_to_cpu(((__be16 *) target->path.dgid.raw)[3]),
1419 be16_to_cpu(((__be16 *) target->path.dgid.raw)[4]),
1420 be16_to_cpu(((__be16 *) target->path.dgid.raw)[5]),
1421 be16_to_cpu(((__be16 *) target->path.dgid.raw)[6]),
1422 be16_to_cpu(((__be16 *) target->path.dgid.raw)[7]));
1423}
1424
1425static CLASS_DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1426static CLASS_DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1427static CLASS_DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1428static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1429static CLASS_DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1430
1431static struct class_device_attribute *srp_host_attrs[] = {
1432 &class_device_attr_id_ext,
1433 &class_device_attr_ioc_guid,
1434 &class_device_attr_service_id,
1435 &class_device_attr_pkey,
1436 &class_device_attr_dgid,
1437 NULL
1438};
1439
Roland Dreieraef9ec32005-11-02 14:07:13 -08001440static struct scsi_host_template srp_template = {
1441 .module = THIS_MODULE,
1442 .name = DRV_NAME,
1443 .info = srp_target_info,
1444 .queuecommand = srp_queuecommand,
1445 .eh_abort_handler = srp_abort,
1446 .eh_device_reset_handler = srp_reset_device,
1447 .eh_host_reset_handler = srp_reset_host,
1448 .can_queue = SRP_SQ_SIZE,
1449 .this_id = -1,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001450 .cmd_per_lun = SRP_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001451 .use_clustering = ENABLE_CLUSTERING,
1452 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001453};
1454
1455static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1456{
1457 sprintf(target->target_name, "SRP.T10:%016llX",
1458 (unsigned long long) be64_to_cpu(target->id_ext));
1459
Roland Dreierf5358a12006-06-17 20:37:29 -07001460 if (scsi_add_host(target->scsi_host, host->dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001461 return -ENODEV;
1462
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001463 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001464 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001465 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001466
1467 target->state = SRP_TARGET_LIVE;
1468
Roland Dreieraef9ec32005-11-02 14:07:13 -08001469 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07001470 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001471
1472 return 0;
1473}
1474
1475static void srp_release_class_dev(struct class_device *class_dev)
1476{
1477 struct srp_host *host =
1478 container_of(class_dev, struct srp_host, class_dev);
1479
1480 complete(&host->released);
1481}
1482
1483static struct class srp_class = {
1484 .name = "infiniband_srp",
1485 .release = srp_release_class_dev
1486};
1487
1488/*
1489 * Target ports are added by writing
1490 *
1491 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1492 * pkey=<P_Key>,service_id=<service ID>
1493 *
1494 * to the add_target sysfs attribute.
1495 */
1496enum {
1497 SRP_OPT_ERR = 0,
1498 SRP_OPT_ID_EXT = 1 << 0,
1499 SRP_OPT_IOC_GUID = 1 << 1,
1500 SRP_OPT_DGID = 1 << 2,
1501 SRP_OPT_PKEY = 1 << 3,
1502 SRP_OPT_SERVICE_ID = 1 << 4,
1503 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07001504 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001505 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1506 SRP_OPT_IOC_GUID |
1507 SRP_OPT_DGID |
1508 SRP_OPT_PKEY |
1509 SRP_OPT_SERVICE_ID),
1510};
1511
1512static match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07001513 { SRP_OPT_ID_EXT, "id_ext=%s" },
1514 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1515 { SRP_OPT_DGID, "dgid=%s" },
1516 { SRP_OPT_PKEY, "pkey=%x" },
1517 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1518 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1519 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
1520 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001521};
1522
1523static int srp_parse_options(const char *buf, struct srp_target_port *target)
1524{
1525 char *options, *sep_opt;
1526 char *p;
1527 char dgid[3];
1528 substring_t args[MAX_OPT_ARGS];
1529 int opt_mask = 0;
1530 int token;
1531 int ret = -EINVAL;
1532 int i;
1533
1534 options = kstrdup(buf, GFP_KERNEL);
1535 if (!options)
1536 return -ENOMEM;
1537
1538 sep_opt = options;
1539 while ((p = strsep(&sep_opt, ",")) != NULL) {
1540 if (!*p)
1541 continue;
1542
1543 token = match_token(p, srp_opt_tokens, args);
1544 opt_mask |= token;
1545
1546 switch (token) {
1547 case SRP_OPT_ID_EXT:
1548 p = match_strdup(args);
1549 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1550 kfree(p);
1551 break;
1552
1553 case SRP_OPT_IOC_GUID:
1554 p = match_strdup(args);
1555 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1556 kfree(p);
1557 break;
1558
1559 case SRP_OPT_DGID:
1560 p = match_strdup(args);
1561 if (strlen(p) != 32) {
1562 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07001563 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001564 goto out;
1565 }
1566
1567 for (i = 0; i < 16; ++i) {
1568 strlcpy(dgid, p + i * 2, 3);
1569 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1570 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08001571 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001572 break;
1573
1574 case SRP_OPT_PKEY:
1575 if (match_hex(args, &token)) {
1576 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1577 goto out;
1578 }
1579 target->path.pkey = cpu_to_be16(token);
1580 break;
1581
1582 case SRP_OPT_SERVICE_ID:
1583 p = match_strdup(args);
1584 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
1585 kfree(p);
1586 break;
1587
1588 case SRP_OPT_MAX_SECT:
1589 if (match_int(args, &token)) {
1590 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1591 goto out;
1592 }
1593 target->scsi_host->max_sectors = token;
1594 break;
1595
Vu Pham52fb2b502006-06-17 20:37:31 -07001596 case SRP_OPT_MAX_CMD_PER_LUN:
1597 if (match_int(args, &token)) {
1598 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1599 goto out;
1600 }
1601 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1602 break;
1603
Roland Dreieraef9ec32005-11-02 14:07:13 -08001604 default:
1605 printk(KERN_WARNING PFX "unknown parameter or missing value "
1606 "'%s' in target creation request\n", p);
1607 goto out;
1608 }
1609 }
1610
1611 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1612 ret = 0;
1613 else
1614 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1615 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1616 !(srp_opt_tokens[i].token & opt_mask))
1617 printk(KERN_WARNING PFX "target creation request is "
1618 "missing parameter '%s'\n",
1619 srp_opt_tokens[i].pattern);
1620
1621out:
1622 kfree(options);
1623 return ret;
1624}
1625
1626static ssize_t srp_create_target(struct class_device *class_dev,
1627 const char *buf, size_t count)
1628{
1629 struct srp_host *host =
1630 container_of(class_dev, struct srp_host, class_dev);
1631 struct Scsi_Host *target_host;
1632 struct srp_target_port *target;
1633 int ret;
1634 int i;
1635
1636 target_host = scsi_host_alloc(&srp_template,
1637 sizeof (struct srp_target_port));
1638 if (!target_host)
1639 return -ENOMEM;
1640
Roland Dreier5f068992005-11-11 14:06:01 -08001641 target_host->max_lun = SRP_MAX_LUN;
1642
Roland Dreieraef9ec32005-11-02 14:07:13 -08001643 target = host_to_target(target_host);
1644 memset(target, 0, sizeof *target);
1645
1646 target->scsi_host = target_host;
1647 target->srp_host = host;
1648
1649 INIT_WORK(&target->work, srp_reconnect_work, target);
1650
Roland Dreierd945e1d2006-05-09 10:50:28 -07001651 INIT_LIST_HEAD(&target->free_reqs);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001652 INIT_LIST_HEAD(&target->req_queue);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001653 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1654 target->req_ring[i].index = i;
1655 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1656 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001657
1658 ret = srp_parse_options(buf, target);
1659 if (ret)
1660 goto err;
1661
Roland Dreierf5358a12006-06-17 20:37:29 -07001662 ib_get_cached_gid(host->dev->dev, host->port, 0, &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001663
1664 printk(KERN_DEBUG PFX "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1665 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1666 (unsigned long long) be64_to_cpu(target->id_ext),
1667 (unsigned long long) be64_to_cpu(target->ioc_guid),
1668 be16_to_cpu(target->path.pkey),
1669 (unsigned long long) be64_to_cpu(target->service_id),
1670 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]),
1671 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]),
1672 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]),
1673 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]),
1674 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]),
1675 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]),
1676 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]),
1677 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14]));
1678
1679 ret = srp_create_target_ib(target);
1680 if (ret)
1681 goto err;
1682
Roland Dreierf5358a12006-06-17 20:37:29 -07001683 target->cm_id = ib_create_cm_id(host->dev->dev, srp_cm_handler, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001684 if (IS_ERR(target->cm_id)) {
1685 ret = PTR_ERR(target->cm_id);
1686 goto err_free;
1687 }
1688
1689 ret = srp_connect_target(target);
1690 if (ret) {
1691 printk(KERN_ERR PFX "Connection failed\n");
1692 goto err_cm_id;
1693 }
1694
1695 ret = srp_add_target(host, target);
1696 if (ret)
1697 goto err_disconnect;
1698
1699 return count;
1700
1701err_disconnect:
1702 srp_disconnect_target(target);
1703
1704err_cm_id:
1705 ib_destroy_cm_id(target->cm_id);
1706
1707err_free:
1708 srp_free_target_ib(target);
1709
1710err:
1711 scsi_host_put(target_host);
1712
1713 return ret;
1714}
1715
1716static CLASS_DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
1717
1718static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
1719{
1720 struct srp_host *host =
1721 container_of(class_dev, struct srp_host, class_dev);
1722
Roland Dreierf5358a12006-06-17 20:37:29 -07001723 return sprintf(buf, "%s\n", host->dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001724}
1725
1726static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1727
1728static ssize_t show_port(struct class_device *class_dev, char *buf)
1729{
1730 struct srp_host *host =
1731 container_of(class_dev, struct srp_host, class_dev);
1732
1733 return sprintf(buf, "%d\n", host->port);
1734}
1735
1736static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
1737
Roland Dreierf5358a12006-06-17 20:37:29 -07001738static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001739{
1740 struct srp_host *host;
1741
1742 host = kzalloc(sizeof *host, GFP_KERNEL);
1743 if (!host)
1744 return NULL;
1745
1746 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001747 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001748 init_completion(&host->released);
1749 host->dev = device;
1750 host->port = port;
1751
1752 host->initiator_port_id[7] = port;
Roland Dreierf5358a12006-06-17 20:37:29 -07001753 memcpy(host->initiator_port_id + 8, &device->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001754
1755 host->class_dev.class = &srp_class;
Roland Dreierf5358a12006-06-17 20:37:29 -07001756 host->class_dev.dev = device->dev->dma_device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001757 snprintf(host->class_dev.class_id, BUS_ID_SIZE, "srp-%s-%d",
Roland Dreierf5358a12006-06-17 20:37:29 -07001758 device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001759
1760 if (class_device_register(&host->class_dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07001761 goto free_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001762 if (class_device_create_file(&host->class_dev, &class_device_attr_add_target))
1763 goto err_class;
1764 if (class_device_create_file(&host->class_dev, &class_device_attr_ibdev))
1765 goto err_class;
1766 if (class_device_create_file(&host->class_dev, &class_device_attr_port))
1767 goto err_class;
1768
1769 return host;
1770
1771err_class:
1772 class_device_unregister(&host->class_dev);
1773
Roland Dreierf5358a12006-06-17 20:37:29 -07001774free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001775 kfree(host);
1776
1777 return NULL;
1778}
1779
1780static void srp_add_one(struct ib_device *device)
1781{
Roland Dreierf5358a12006-06-17 20:37:29 -07001782 struct srp_device *srp_dev;
1783 struct ib_device_attr *dev_attr;
1784 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001785 struct srp_host *host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001786 int s, e, p;
1787
Roland Dreierf5358a12006-06-17 20:37:29 -07001788 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
1789 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08001790 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001791
Roland Dreierf5358a12006-06-17 20:37:29 -07001792 if (ib_query_device(device, dev_attr)) {
1793 printk(KERN_WARNING PFX "Query device failed for %s\n",
1794 device->name);
1795 goto free_attr;
1796 }
1797
1798 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
1799 if (!srp_dev)
1800 goto free_attr;
1801
1802 /*
1803 * Use the smallest page size supported by the HCA, down to a
1804 * minimum of 512 bytes (which is the smallest sector that a
1805 * SCSI command will ever carry).
1806 */
1807 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
1808 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
1809 srp_dev->fmr_page_mask = ~((unsigned long) srp_dev->fmr_page_size - 1);
1810
1811 INIT_LIST_HEAD(&srp_dev->dev_list);
1812
1813 srp_dev->dev = device;
1814 srp_dev->pd = ib_alloc_pd(device);
1815 if (IS_ERR(srp_dev->pd))
1816 goto free_dev;
1817
1818 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
1819 IB_ACCESS_LOCAL_WRITE |
1820 IB_ACCESS_REMOTE_READ |
1821 IB_ACCESS_REMOTE_WRITE);
1822 if (IS_ERR(srp_dev->mr))
1823 goto err_pd;
1824
1825 memset(&fmr_param, 0, sizeof fmr_param);
1826 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
1827 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
1828 fmr_param.cache = 1;
1829 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
1830 fmr_param.page_shift = srp_dev->fmr_page_shift;
1831 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
1832 IB_ACCESS_REMOTE_WRITE |
1833 IB_ACCESS_REMOTE_READ);
1834
1835 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
1836 if (IS_ERR(srp_dev->fmr_pool))
1837 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001838
1839 if (device->node_type == IB_NODE_SWITCH) {
1840 s = 0;
1841 e = 0;
1842 } else {
1843 s = 1;
1844 e = device->phys_port_cnt;
1845 }
1846
1847 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07001848 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001849 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07001850 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001851 }
1852
Roland Dreierf5358a12006-06-17 20:37:29 -07001853 ib_set_client_data(device, &srp_client, srp_dev);
1854
1855 goto free_attr;
1856
1857err_pd:
1858 ib_dealloc_pd(srp_dev->pd);
1859
1860free_dev:
1861 kfree(srp_dev);
1862
1863free_attr:
1864 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001865}
1866
1867static void srp_remove_one(struct ib_device *device)
1868{
Roland Dreierf5358a12006-06-17 20:37:29 -07001869 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001870 struct srp_host *host, *tmp_host;
1871 LIST_HEAD(target_list);
1872 struct srp_target_port *target, *tmp_target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001873
Roland Dreierf5358a12006-06-17 20:37:29 -07001874 srp_dev = ib_get_client_data(device, &srp_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001875
Roland Dreierf5358a12006-06-17 20:37:29 -07001876 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001877 class_device_unregister(&host->class_dev);
1878 /*
1879 * Wait for the sysfs entry to go away, so that no new
1880 * target ports can be created.
1881 */
1882 wait_for_completion(&host->released);
1883
1884 /*
1885 * Mark all target ports as removed, so we stop queueing
1886 * commands and don't try to reconnect.
1887 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001888 spin_lock(&host->target_lock);
Matthew Wilcox549c5fc22006-06-17 20:37:30 -07001889 list_for_each_entry(target, &host->target_list, list) {
Ishai Rabinovitz0c5b3952006-06-17 20:37:31 -07001890 spin_lock_irq(target->scsi_host->host_lock);
1891 target->state = SRP_TARGET_REMOVED;
1892 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001893 }
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001894 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001895
1896 /*
1897 * Wait for any reconnection tasks that may have
1898 * started before we marked our target ports as
1899 * removed, and any target port removal tasks.
1900 */
1901 flush_scheduled_work();
1902
1903 list_for_each_entry_safe(target, tmp_target,
1904 &host->target_list, list) {
1905 scsi_remove_host(target->scsi_host);
1906 srp_disconnect_target(target);
1907 ib_destroy_cm_id(target->cm_id);
1908 srp_free_target_ib(target);
1909 scsi_host_put(target->scsi_host);
1910 }
1911
Roland Dreieraef9ec32005-11-02 14:07:13 -08001912 kfree(host);
1913 }
1914
Roland Dreierf5358a12006-06-17 20:37:29 -07001915 if (srp_dev->fmr_pool)
1916 ib_destroy_fmr_pool(srp_dev->fmr_pool);
1917 ib_dereg_mr(srp_dev->mr);
1918 ib_dealloc_pd(srp_dev->pd);
1919
1920 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001921}
1922
1923static int __init srp_init_module(void)
1924{
1925 int ret;
1926
Vu Pham74b0a152006-06-17 20:37:32 -07001927 srp_template.sg_tablesize = srp_sg_tablesize;
1928 srp_max_iu_len = (sizeof (struct srp_cmd) +
1929 sizeof (struct srp_indirect_buf) +
1930 srp_sg_tablesize * 16);
1931
Roland Dreieraef9ec32005-11-02 14:07:13 -08001932 ret = class_register(&srp_class);
1933 if (ret) {
1934 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
1935 return ret;
1936 }
1937
1938 ret = ib_register_client(&srp_client);
1939 if (ret) {
1940 printk(KERN_ERR PFX "couldn't register IB client\n");
1941 class_unregister(&srp_class);
1942 return ret;
1943 }
1944
1945 return 0;
1946}
1947
1948static void __exit srp_cleanup_module(void)
1949{
1950 ib_unregister_client(&srp_client);
1951 class_unregister(&srp_class);
1952}
1953
1954module_init(srp_init_module);
1955module_exit(srp_cleanup_module);