Merge master.kernel.org:/pub/scm/linux/kernel/git/perex/alsa
[wrt350n-kernel.git] / drivers / infiniband / ulp / srp / ib_srp.c
blob8257d5a2c8f8955124138cedca609f2719d37e1d
1 /*
2 * Copyright (c) 2005 Cisco Systems. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
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.
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.
32 * $Id: ib_srp.c 3932 2005-11-01 17:19:29Z roland $
35 #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>
42 #include <linux/jiffies.h>
44 #include <asm/atomic.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_device.h>
48 #include <scsi/scsi_dbg.h>
49 #include <scsi/srp.h>
51 #include <rdma/ib_cache.h>
53 #include "ib_srp.h"
55 #define DRV_NAME "ib_srp"
56 #define PFX DRV_NAME ": "
57 #define DRV_VERSION "0.2"
58 #define DRV_RELDATE "November 1, 2005"
60 MODULE_AUTHOR("Roland Dreier");
61 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
62 "v" DRV_VERSION " (" DRV_RELDATE ")");
63 MODULE_LICENSE("Dual BSD/GPL");
65 static int srp_sg_tablesize = SRP_DEF_SG_TABLESIZE;
66 static int srp_max_iu_len;
68 module_param(srp_sg_tablesize, int, 0444);
69 MODULE_PARM_DESC(srp_sg_tablesize,
70 "Max number of gather/scatter entries per I/O (default is 12)");
72 static int topspin_workarounds = 1;
74 module_param(topspin_workarounds, int, 0444);
75 MODULE_PARM_DESC(topspin_workarounds,
76 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
78 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
80 static int mellanox_workarounds = 1;
82 module_param(mellanox_workarounds, int, 0444);
83 MODULE_PARM_DESC(mellanox_workarounds,
84 "Enable workarounds for Mellanox SRP target bugs if != 0");
86 static const u8 mellanox_oui[3] = { 0x00, 0x02, 0xc9 };
88 static void srp_add_one(struct ib_device *device);
89 static void srp_remove_one(struct ib_device *device);
90 static void srp_completion(struct ib_cq *cq, void *target_ptr);
91 static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
93 static struct ib_client srp_client = {
94 .name = "srp",
95 .add = srp_add_one,
96 .remove = srp_remove_one
99 static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
101 return (struct srp_target_port *) host->hostdata;
104 static const char *srp_target_info(struct Scsi_Host *host)
106 return host_to_target(host)->target_name;
109 static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
110 gfp_t gfp_mask,
111 enum dma_data_direction direction)
113 struct srp_iu *iu;
115 iu = kmalloc(sizeof *iu, gfp_mask);
116 if (!iu)
117 goto out;
119 iu->buf = kzalloc(size, gfp_mask);
120 if (!iu->buf)
121 goto out_free_iu;
123 iu->dma = dma_map_single(host->dev->dev->dma_device,
124 iu->buf, size, direction);
125 if (dma_mapping_error(iu->dma))
126 goto out_free_buf;
128 iu->size = size;
129 iu->direction = direction;
131 return iu;
133 out_free_buf:
134 kfree(iu->buf);
135 out_free_iu:
136 kfree(iu);
137 out:
138 return NULL;
141 static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
143 if (!iu)
144 return;
146 dma_unmap_single(host->dev->dev->dma_device,
147 iu->dma, iu->size, iu->direction);
148 kfree(iu->buf);
149 kfree(iu);
152 static void srp_qp_event(struct ib_event *event, void *context)
154 printk(KERN_ERR PFX "QP event %d\n", event->event);
157 static int srp_init_qp(struct srp_target_port *target,
158 struct ib_qp *qp)
160 struct ib_qp_attr *attr;
161 int ret;
163 attr = kmalloc(sizeof *attr, GFP_KERNEL);
164 if (!attr)
165 return -ENOMEM;
167 ret = ib_find_cached_pkey(target->srp_host->dev->dev,
168 target->srp_host->port,
169 be16_to_cpu(target->path.pkey),
170 &attr->pkey_index);
171 if (ret)
172 goto out;
174 attr->qp_state = IB_QPS_INIT;
175 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
176 IB_ACCESS_REMOTE_WRITE);
177 attr->port_num = target->srp_host->port;
179 ret = ib_modify_qp(qp, attr,
180 IB_QP_STATE |
181 IB_QP_PKEY_INDEX |
182 IB_QP_ACCESS_FLAGS |
183 IB_QP_PORT);
185 out:
186 kfree(attr);
187 return ret;
190 static int srp_create_target_ib(struct srp_target_port *target)
192 struct ib_qp_init_attr *init_attr;
193 int ret;
195 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
196 if (!init_attr)
197 return -ENOMEM;
199 target->cq = ib_create_cq(target->srp_host->dev->dev, srp_completion,
200 NULL, target, SRP_CQ_SIZE);
201 if (IS_ERR(target->cq)) {
202 ret = PTR_ERR(target->cq);
203 goto out;
206 ib_req_notify_cq(target->cq, IB_CQ_NEXT_COMP);
208 init_attr->event_handler = srp_qp_event;
209 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
210 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
211 init_attr->cap.max_recv_sge = 1;
212 init_attr->cap.max_send_sge = 1;
213 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
214 init_attr->qp_type = IB_QPT_RC;
215 init_attr->send_cq = target->cq;
216 init_attr->recv_cq = target->cq;
218 target->qp = ib_create_qp(target->srp_host->dev->pd, init_attr);
219 if (IS_ERR(target->qp)) {
220 ret = PTR_ERR(target->qp);
221 ib_destroy_cq(target->cq);
222 goto out;
225 ret = srp_init_qp(target, target->qp);
226 if (ret) {
227 ib_destroy_qp(target->qp);
228 ib_destroy_cq(target->cq);
229 goto out;
232 out:
233 kfree(init_attr);
234 return ret;
237 static void srp_free_target_ib(struct srp_target_port *target)
239 int i;
241 ib_destroy_qp(target->qp);
242 ib_destroy_cq(target->cq);
244 for (i = 0; i < SRP_RQ_SIZE; ++i)
245 srp_free_iu(target->srp_host, target->rx_ring[i]);
246 for (i = 0; i < SRP_SQ_SIZE + 1; ++i)
247 srp_free_iu(target->srp_host, target->tx_ring[i]);
250 static void srp_path_rec_completion(int status,
251 struct ib_sa_path_rec *pathrec,
252 void *target_ptr)
254 struct srp_target_port *target = target_ptr;
256 target->status = status;
257 if (status)
258 printk(KERN_ERR PFX "Got failed path rec status %d\n", status);
259 else
260 target->path = *pathrec;
261 complete(&target->done);
264 static int srp_lookup_path(struct srp_target_port *target)
266 target->path.numb_path = 1;
268 init_completion(&target->done);
270 target->path_query_id = ib_sa_path_rec_get(target->srp_host->dev->dev,
271 target->srp_host->port,
272 &target->path,
273 IB_SA_PATH_REC_DGID |
274 IB_SA_PATH_REC_SGID |
275 IB_SA_PATH_REC_NUMB_PATH |
276 IB_SA_PATH_REC_PKEY,
277 SRP_PATH_REC_TIMEOUT_MS,
278 GFP_KERNEL,
279 srp_path_rec_completion,
280 target, &target->path_query);
281 if (target->path_query_id < 0)
282 return target->path_query_id;
284 wait_for_completion(&target->done);
286 if (target->status < 0)
287 printk(KERN_WARNING PFX "Path record query failed\n");
289 return target->status;
292 static int srp_send_req(struct srp_target_port *target)
294 struct {
295 struct ib_cm_req_param param;
296 struct srp_login_req priv;
297 } *req = NULL;
298 int status;
300 req = kzalloc(sizeof *req, GFP_KERNEL);
301 if (!req)
302 return -ENOMEM;
304 req->param.primary_path = &target->path;
305 req->param.alternate_path = NULL;
306 req->param.service_id = target->service_id;
307 req->param.qp_num = target->qp->qp_num;
308 req->param.qp_type = target->qp->qp_type;
309 req->param.private_data = &req->priv;
310 req->param.private_data_len = sizeof req->priv;
311 req->param.flow_control = 1;
313 get_random_bytes(&req->param.starting_psn, 4);
314 req->param.starting_psn &= 0xffffff;
317 * Pick some arbitrary defaults here; we could make these
318 * module parameters if anyone cared about setting them.
320 req->param.responder_resources = 4;
321 req->param.remote_cm_response_timeout = 20;
322 req->param.local_cm_response_timeout = 20;
323 req->param.retry_count = 7;
324 req->param.rnr_retry_count = 7;
325 req->param.max_cm_retries = 15;
327 req->priv.opcode = SRP_LOGIN_REQ;
328 req->priv.tag = 0;
329 req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
330 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
331 SRP_BUF_FORMAT_INDIRECT);
333 * In the published SRP specification (draft rev. 16a), the
334 * port identifier format is 8 bytes of ID extension followed
335 * by 8 bytes of GUID. Older drafts put the two halves in the
336 * opposite order, so that the GUID comes first.
338 * Targets conforming to these obsolete drafts can be
339 * recognized by the I/O Class they report.
341 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
342 memcpy(req->priv.initiator_port_id,
343 target->srp_host->initiator_port_id + 8, 8);
344 memcpy(req->priv.initiator_port_id + 8,
345 target->srp_host->initiator_port_id, 8);
346 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
347 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
348 } else {
349 memcpy(req->priv.initiator_port_id,
350 target->srp_host->initiator_port_id, 16);
351 memcpy(req->priv.target_port_id, &target->id_ext, 8);
352 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
356 * Topspin/Cisco SRP targets will reject our login unless we
357 * zero out the first 8 bytes of our initiator port ID. The
358 * second 8 bytes must be our local node GUID, but we always
359 * use that anyway.
361 if (topspin_workarounds && !memcmp(&target->ioc_guid, topspin_oui, 3)) {
362 printk(KERN_DEBUG PFX "Topspin/Cisco initiator port ID workaround "
363 "activated for target GUID %016llx\n",
364 (unsigned long long) be64_to_cpu(target->ioc_guid));
365 memset(req->priv.initiator_port_id, 0, 8);
368 status = ib_send_cm_req(target->cm_id, &req->param);
370 kfree(req);
372 return status;
375 static void srp_disconnect_target(struct srp_target_port *target)
377 /* XXX should send SRP_I_LOGOUT request */
379 init_completion(&target->done);
380 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
381 printk(KERN_DEBUG PFX "Sending CM DREQ failed\n");
382 return;
384 wait_for_completion(&target->done);
387 static void srp_remove_work(void *target_ptr)
389 struct srp_target_port *target = target_ptr;
391 spin_lock_irq(target->scsi_host->host_lock);
392 if (target->state != SRP_TARGET_DEAD) {
393 spin_unlock_irq(target->scsi_host->host_lock);
394 return;
396 target->state = SRP_TARGET_REMOVED;
397 spin_unlock_irq(target->scsi_host->host_lock);
399 spin_lock(&target->srp_host->target_lock);
400 list_del(&target->list);
401 spin_unlock(&target->srp_host->target_lock);
403 scsi_remove_host(target->scsi_host);
404 ib_destroy_cm_id(target->cm_id);
405 srp_free_target_ib(target);
406 scsi_host_put(target->scsi_host);
409 static int srp_connect_target(struct srp_target_port *target)
411 int ret;
413 ret = srp_lookup_path(target);
414 if (ret)
415 return ret;
417 while (1) {
418 init_completion(&target->done);
419 ret = srp_send_req(target);
420 if (ret)
421 return ret;
422 wait_for_completion(&target->done);
425 * The CM event handling code will set status to
426 * SRP_PORT_REDIRECT if we get a port redirect REJ
427 * back, or SRP_DLID_REDIRECT if we get a lid/qp
428 * redirect REJ back.
430 switch (target->status) {
431 case 0:
432 return 0;
434 case SRP_PORT_REDIRECT:
435 ret = srp_lookup_path(target);
436 if (ret)
437 return ret;
438 break;
440 case SRP_DLID_REDIRECT:
441 break;
443 default:
444 return target->status;
449 static void srp_unmap_data(struct scsi_cmnd *scmnd,
450 struct srp_target_port *target,
451 struct srp_request *req)
453 struct scatterlist *scat;
454 int nents;
456 if (!scmnd->request_buffer ||
457 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
458 scmnd->sc_data_direction != DMA_FROM_DEVICE))
459 return;
461 if (req->fmr) {
462 ib_fmr_pool_unmap(req->fmr);
463 req->fmr = NULL;
467 * This handling of non-SG commands can be killed when the
468 * SCSI midlayer no longer generates non-SG commands.
470 if (likely(scmnd->use_sg)) {
471 nents = scmnd->use_sg;
472 scat = scmnd->request_buffer;
473 } else {
474 nents = 1;
475 scat = &req->fake_sg;
478 dma_unmap_sg(target->srp_host->dev->dev->dma_device, scat, nents,
479 scmnd->sc_data_direction);
482 static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
484 srp_unmap_data(req->scmnd, target, req);
485 list_move_tail(&req->list, &target->free_reqs);
488 static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
490 req->scmnd->result = DID_RESET << 16;
491 req->scmnd->scsi_done(req->scmnd);
492 srp_remove_req(target, req);
495 static int srp_reconnect_target(struct srp_target_port *target)
497 struct ib_cm_id *new_cm_id;
498 struct ib_qp_attr qp_attr;
499 struct srp_request *req, *tmp;
500 struct ib_wc wc;
501 int ret;
503 spin_lock_irq(target->scsi_host->host_lock);
504 if (target->state != SRP_TARGET_LIVE) {
505 spin_unlock_irq(target->scsi_host->host_lock);
506 return -EAGAIN;
508 target->state = SRP_TARGET_CONNECTING;
509 spin_unlock_irq(target->scsi_host->host_lock);
511 srp_disconnect_target(target);
513 * Now get a new local CM ID so that we avoid confusing the
514 * target in case things are really fouled up.
516 new_cm_id = ib_create_cm_id(target->srp_host->dev->dev,
517 srp_cm_handler, target);
518 if (IS_ERR(new_cm_id)) {
519 ret = PTR_ERR(new_cm_id);
520 goto err;
522 ib_destroy_cm_id(target->cm_id);
523 target->cm_id = new_cm_id;
525 qp_attr.qp_state = IB_QPS_RESET;
526 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
527 if (ret)
528 goto err;
530 ret = srp_init_qp(target, target->qp);
531 if (ret)
532 goto err;
534 while (ib_poll_cq(target->cq, 1, &wc) > 0)
535 ; /* nothing */
537 spin_lock_irq(target->scsi_host->host_lock);
538 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
539 srp_reset_req(target, req);
540 spin_unlock_irq(target->scsi_host->host_lock);
542 target->rx_head = 0;
543 target->tx_head = 0;
544 target->tx_tail = 0;
546 ret = srp_connect_target(target);
547 if (ret)
548 goto err;
550 spin_lock_irq(target->scsi_host->host_lock);
551 if (target->state == SRP_TARGET_CONNECTING) {
552 ret = 0;
553 target->state = SRP_TARGET_LIVE;
554 } else
555 ret = -EAGAIN;
556 spin_unlock_irq(target->scsi_host->host_lock);
558 return ret;
560 err:
561 printk(KERN_ERR PFX "reconnect failed (%d), removing target port.\n", ret);
564 * We couldn't reconnect, so kill our target port off.
565 * However, we have to defer the real removal because we might
566 * be in the context of the SCSI error handler now, which
567 * would deadlock if we call scsi_remove_host().
569 spin_lock_irq(target->scsi_host->host_lock);
570 if (target->state == SRP_TARGET_CONNECTING) {
571 target->state = SRP_TARGET_DEAD;
572 INIT_WORK(&target->work, srp_remove_work, target);
573 schedule_work(&target->work);
575 spin_unlock_irq(target->scsi_host->host_lock);
577 return ret;
580 static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat,
581 int sg_cnt, struct srp_request *req,
582 struct srp_direct_buf *buf)
584 u64 io_addr = 0;
585 u64 *dma_pages;
586 u32 len;
587 int page_cnt;
588 int i, j;
589 int ret;
590 struct srp_device *dev = target->srp_host->dev;
592 if (!dev->fmr_pool)
593 return -ENODEV;
595 if ((sg_dma_address(&scat[0]) & ~dev->fmr_page_mask) &&
596 mellanox_workarounds && !memcmp(&target->ioc_guid, mellanox_oui, 3))
597 return -EINVAL;
599 len = page_cnt = 0;
600 for (i = 0; i < sg_cnt; ++i) {
601 if (sg_dma_address(&scat[i]) & ~dev->fmr_page_mask) {
602 if (i > 0)
603 return -EINVAL;
604 else
605 ++page_cnt;
607 if ((sg_dma_address(&scat[i]) + sg_dma_len(&scat[i])) &
608 ~dev->fmr_page_mask) {
609 if (i < sg_cnt - 1)
610 return -EINVAL;
611 else
612 ++page_cnt;
615 len += sg_dma_len(&scat[i]);
618 page_cnt += len >> dev->fmr_page_shift;
619 if (page_cnt > SRP_FMR_SIZE)
620 return -ENOMEM;
622 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
623 if (!dma_pages)
624 return -ENOMEM;
626 page_cnt = 0;
627 for (i = 0; i < sg_cnt; ++i)
628 for (j = 0; j < sg_dma_len(&scat[i]); j += dev->fmr_page_size)
629 dma_pages[page_cnt++] =
630 (sg_dma_address(&scat[i]) & dev->fmr_page_mask) + j;
632 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
633 dma_pages, page_cnt, io_addr);
634 if (IS_ERR(req->fmr)) {
635 ret = PTR_ERR(req->fmr);
636 req->fmr = NULL;
637 goto out;
640 buf->va = cpu_to_be64(sg_dma_address(&scat[0]) & ~dev->fmr_page_mask);
641 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
642 buf->len = cpu_to_be32(len);
644 ret = 0;
646 out:
647 kfree(dma_pages);
649 return ret;
652 static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
653 struct srp_request *req)
655 struct scatterlist *scat;
656 struct srp_cmd *cmd = req->cmd->buf;
657 int len, nents, count;
658 u8 fmt = SRP_DATA_DESC_DIRECT;
660 if (!scmnd->request_buffer || scmnd->sc_data_direction == DMA_NONE)
661 return sizeof (struct srp_cmd);
663 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
664 scmnd->sc_data_direction != DMA_TO_DEVICE) {
665 printk(KERN_WARNING PFX "Unhandled data direction %d\n",
666 scmnd->sc_data_direction);
667 return -EINVAL;
671 * This handling of non-SG commands can be killed when the
672 * SCSI midlayer no longer generates non-SG commands.
674 if (likely(scmnd->use_sg)) {
675 nents = scmnd->use_sg;
676 scat = scmnd->request_buffer;
677 } else {
678 nents = 1;
679 scat = &req->fake_sg;
680 sg_init_one(scat, scmnd->request_buffer, scmnd->request_bufflen);
683 count = dma_map_sg(target->srp_host->dev->dev->dma_device,
684 scat, nents, scmnd->sc_data_direction);
686 fmt = SRP_DATA_DESC_DIRECT;
687 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
689 if (count == 1) {
691 * The midlayer only generated a single gather/scatter
692 * entry, or DMA mapping coalesced everything to a
693 * single entry. So a direct descriptor along with
694 * the DMA MR suffices.
696 struct srp_direct_buf *buf = (void *) cmd->add_data;
698 buf->va = cpu_to_be64(sg_dma_address(scat));
699 buf->key = cpu_to_be32(target->srp_host->dev->mr->rkey);
700 buf->len = cpu_to_be32(sg_dma_len(scat));
701 } else if (srp_map_fmr(target, scat, count, req,
702 (void *) cmd->add_data)) {
704 * FMR mapping failed, and the scatterlist has more
705 * than one entry. Generate an indirect memory
706 * descriptor.
708 struct srp_indirect_buf *buf = (void *) cmd->add_data;
709 u32 datalen = 0;
710 int i;
712 fmt = SRP_DATA_DESC_INDIRECT;
713 len = sizeof (struct srp_cmd) +
714 sizeof (struct srp_indirect_buf) +
715 count * sizeof (struct srp_direct_buf);
717 for (i = 0; i < count; ++i) {
718 buf->desc_list[i].va =
719 cpu_to_be64(sg_dma_address(&scat[i]));
720 buf->desc_list[i].key =
721 cpu_to_be32(target->srp_host->dev->mr->rkey);
722 buf->desc_list[i].len =
723 cpu_to_be32(sg_dma_len(&scat[i]));
724 datalen += sg_dma_len(&scat[i]);
727 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
728 cmd->data_out_desc_cnt = count;
729 else
730 cmd->data_in_desc_cnt = count;
732 buf->table_desc.va =
733 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
734 buf->table_desc.key =
735 cpu_to_be32(target->srp_host->dev->mr->rkey);
736 buf->table_desc.len =
737 cpu_to_be32(count * sizeof (struct srp_direct_buf));
739 buf->len = cpu_to_be32(datalen);
742 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
743 cmd->buf_fmt = fmt << 4;
744 else
745 cmd->buf_fmt = fmt;
747 return len;
750 static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
752 struct srp_request *req;
753 struct scsi_cmnd *scmnd;
754 unsigned long flags;
755 s32 delta;
757 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
759 spin_lock_irqsave(target->scsi_host->host_lock, flags);
761 target->req_lim += delta;
763 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
765 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
766 if (be32_to_cpu(rsp->resp_data_len) < 4)
767 req->tsk_status = -1;
768 else
769 req->tsk_status = rsp->data[3];
770 complete(&req->done);
771 } else {
772 scmnd = req->scmnd;
773 if (!scmnd)
774 printk(KERN_ERR "Null scmnd for RSP w/tag %016llx\n",
775 (unsigned long long) rsp->tag);
776 scmnd->result = rsp->status;
778 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
779 memcpy(scmnd->sense_buffer, rsp->data +
780 be32_to_cpu(rsp->resp_data_len),
781 min_t(int, be32_to_cpu(rsp->sense_data_len),
782 SCSI_SENSE_BUFFERSIZE));
785 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
786 scmnd->resid = be32_to_cpu(rsp->data_out_res_cnt);
787 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
788 scmnd->resid = be32_to_cpu(rsp->data_in_res_cnt);
790 if (!req->tsk_mgmt) {
791 scmnd->host_scribble = (void *) -1L;
792 scmnd->scsi_done(scmnd);
794 srp_remove_req(target, req);
795 } else
796 req->cmd_done = 1;
799 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
802 static void srp_reconnect_work(void *target_ptr)
804 struct srp_target_port *target = target_ptr;
806 srp_reconnect_target(target);
809 static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
811 struct srp_iu *iu;
812 u8 opcode;
814 iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
816 dma_sync_single_for_cpu(target->srp_host->dev->dev->dma_device, iu->dma,
817 target->max_ti_iu_len, DMA_FROM_DEVICE);
819 opcode = *(u8 *) iu->buf;
821 if (0) {
822 int i;
824 printk(KERN_ERR PFX "recv completion, opcode 0x%02x\n", opcode);
826 for (i = 0; i < wc->byte_len; ++i) {
827 if (i % 8 == 0)
828 printk(KERN_ERR " [%02x] ", i);
829 printk(" %02x", ((u8 *) iu->buf)[i]);
830 if ((i + 1) % 8 == 0)
831 printk("\n");
834 if (wc->byte_len % 8)
835 printk("\n");
838 switch (opcode) {
839 case SRP_RSP:
840 srp_process_rsp(target, iu->buf);
841 break;
843 case SRP_T_LOGOUT:
844 /* XXX Handle target logout */
845 printk(KERN_WARNING PFX "Got target logout request\n");
846 break;
848 default:
849 printk(KERN_WARNING PFX "Unhandled SRP opcode 0x%02x\n", opcode);
850 break;
853 dma_sync_single_for_device(target->srp_host->dev->dev->dma_device, iu->dma,
854 target->max_ti_iu_len, DMA_FROM_DEVICE);
857 static void srp_completion(struct ib_cq *cq, void *target_ptr)
859 struct srp_target_port *target = target_ptr;
860 struct ib_wc wc;
861 unsigned long flags;
863 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
864 while (ib_poll_cq(cq, 1, &wc) > 0) {
865 if (wc.status) {
866 printk(KERN_ERR PFX "failed %s status %d\n",
867 wc.wr_id & SRP_OP_RECV ? "receive" : "send",
868 wc.status);
869 spin_lock_irqsave(target->scsi_host->host_lock, flags);
870 if (target->state == SRP_TARGET_LIVE)
871 schedule_work(&target->work);
872 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
873 break;
876 if (wc.wr_id & SRP_OP_RECV)
877 srp_handle_recv(target, &wc);
878 else
879 ++target->tx_tail;
883 static int __srp_post_recv(struct srp_target_port *target)
885 struct srp_iu *iu;
886 struct ib_sge list;
887 struct ib_recv_wr wr, *bad_wr;
888 unsigned int next;
889 int ret;
891 next = target->rx_head & (SRP_RQ_SIZE - 1);
892 wr.wr_id = next | SRP_OP_RECV;
893 iu = target->rx_ring[next];
895 list.addr = iu->dma;
896 list.length = iu->size;
897 list.lkey = target->srp_host->dev->mr->lkey;
899 wr.next = NULL;
900 wr.sg_list = &list;
901 wr.num_sge = 1;
903 ret = ib_post_recv(target->qp, &wr, &bad_wr);
904 if (!ret)
905 ++target->rx_head;
907 return ret;
910 static int srp_post_recv(struct srp_target_port *target)
912 unsigned long flags;
913 int ret;
915 spin_lock_irqsave(target->scsi_host->host_lock, flags);
916 ret = __srp_post_recv(target);
917 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
919 return ret;
923 * Must be called with target->scsi_host->host_lock held to protect
924 * req_lim and tx_head. Lock cannot be dropped between call here and
925 * call to __srp_post_send().
927 static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target)
929 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
930 return NULL;
932 if (unlikely(target->req_lim < 1))
933 ++target->zero_req_lim;
935 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
939 * Must be called with target->scsi_host->host_lock held to protect
940 * req_lim and tx_head.
942 static int __srp_post_send(struct srp_target_port *target,
943 struct srp_iu *iu, int len)
945 struct ib_sge list;
946 struct ib_send_wr wr, *bad_wr;
947 int ret = 0;
949 list.addr = iu->dma;
950 list.length = len;
951 list.lkey = target->srp_host->dev->mr->lkey;
953 wr.next = NULL;
954 wr.wr_id = target->tx_head & SRP_SQ_SIZE;
955 wr.sg_list = &list;
956 wr.num_sge = 1;
957 wr.opcode = IB_WR_SEND;
958 wr.send_flags = IB_SEND_SIGNALED;
960 ret = ib_post_send(target->qp, &wr, &bad_wr);
962 if (!ret) {
963 ++target->tx_head;
964 --target->req_lim;
967 return ret;
970 static int srp_queuecommand(struct scsi_cmnd *scmnd,
971 void (*done)(struct scsi_cmnd *))
973 struct srp_target_port *target = host_to_target(scmnd->device->host);
974 struct srp_request *req;
975 struct srp_iu *iu;
976 struct srp_cmd *cmd;
977 int len;
979 if (target->state == SRP_TARGET_CONNECTING)
980 goto err;
982 if (target->state == SRP_TARGET_DEAD ||
983 target->state == SRP_TARGET_REMOVED) {
984 scmnd->result = DID_BAD_TARGET << 16;
985 done(scmnd);
986 return 0;
989 iu = __srp_get_tx_iu(target);
990 if (!iu)
991 goto err;
993 dma_sync_single_for_cpu(target->srp_host->dev->dev->dma_device, iu->dma,
994 srp_max_iu_len, DMA_TO_DEVICE);
996 req = list_entry(target->free_reqs.next, struct srp_request, list);
998 scmnd->scsi_done = done;
999 scmnd->result = 0;
1000 scmnd->host_scribble = (void *) (long) req->index;
1002 cmd = iu->buf;
1003 memset(cmd, 0, sizeof *cmd);
1005 cmd->opcode = SRP_CMD;
1006 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
1007 cmd->tag = req->index;
1008 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1010 req->scmnd = scmnd;
1011 req->cmd = iu;
1012 req->cmd_done = 0;
1013 req->tsk_mgmt = NULL;
1015 len = srp_map_data(scmnd, target, req);
1016 if (len < 0) {
1017 printk(KERN_ERR PFX "Failed to map data\n");
1018 goto err;
1021 if (__srp_post_recv(target)) {
1022 printk(KERN_ERR PFX "Recv failed\n");
1023 goto err_unmap;
1026 dma_sync_single_for_device(target->srp_host->dev->dev->dma_device, iu->dma,
1027 srp_max_iu_len, DMA_TO_DEVICE);
1029 if (__srp_post_send(target, iu, len)) {
1030 printk(KERN_ERR PFX "Send failed\n");
1031 goto err_unmap;
1034 list_move_tail(&req->list, &target->req_queue);
1036 return 0;
1038 err_unmap:
1039 srp_unmap_data(scmnd, target, req);
1041 err:
1042 return SCSI_MLQUEUE_HOST_BUSY;
1045 static int srp_alloc_iu_bufs(struct srp_target_port *target)
1047 int i;
1049 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1050 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1051 target->max_ti_iu_len,
1052 GFP_KERNEL, DMA_FROM_DEVICE);
1053 if (!target->rx_ring[i])
1054 goto err;
1057 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1058 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
1059 srp_max_iu_len,
1060 GFP_KERNEL, DMA_TO_DEVICE);
1061 if (!target->tx_ring[i])
1062 goto err;
1065 return 0;
1067 err:
1068 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1069 srp_free_iu(target->srp_host, target->rx_ring[i]);
1070 target->rx_ring[i] = NULL;
1073 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1074 srp_free_iu(target->srp_host, target->tx_ring[i]);
1075 target->tx_ring[i] = NULL;
1078 return -ENOMEM;
1081 static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1082 struct ib_cm_event *event,
1083 struct srp_target_port *target)
1085 struct ib_class_port_info *cpi;
1086 int opcode;
1088 switch (event->param.rej_rcvd.reason) {
1089 case IB_CM_REJ_PORT_CM_REDIRECT:
1090 cpi = event->param.rej_rcvd.ari;
1091 target->path.dlid = cpi->redirect_lid;
1092 target->path.pkey = cpi->redirect_pkey;
1093 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1094 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1096 target->status = target->path.dlid ?
1097 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1098 break;
1100 case IB_CM_REJ_PORT_REDIRECT:
1101 if (topspin_workarounds &&
1102 !memcmp(&target->ioc_guid, topspin_oui, 3)) {
1104 * Topspin/Cisco SRP gateways incorrectly send
1105 * reject reason code 25 when they mean 24
1106 * (port redirect).
1108 memcpy(target->path.dgid.raw,
1109 event->param.rej_rcvd.ari, 16);
1111 printk(KERN_DEBUG PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1112 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1113 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1115 target->status = SRP_PORT_REDIRECT;
1116 } else {
1117 printk(KERN_WARNING " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1118 target->status = -ECONNRESET;
1120 break;
1122 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
1123 printk(KERN_WARNING " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1124 target->status = -ECONNRESET;
1125 break;
1127 case IB_CM_REJ_CONSUMER_DEFINED:
1128 opcode = *(u8 *) event->private_data;
1129 if (opcode == SRP_LOGIN_REJ) {
1130 struct srp_login_rej *rej = event->private_data;
1131 u32 reason = be32_to_cpu(rej->reason);
1133 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
1134 printk(KERN_WARNING PFX
1135 "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1136 else
1137 printk(KERN_WARNING PFX
1138 "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
1139 } else
1140 printk(KERN_WARNING " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1141 " opcode 0x%02x\n", opcode);
1142 target->status = -ECONNRESET;
1143 break;
1145 default:
1146 printk(KERN_WARNING " REJ reason 0x%x\n",
1147 event->param.rej_rcvd.reason);
1148 target->status = -ECONNRESET;
1152 static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1154 struct srp_target_port *target = cm_id->context;
1155 struct ib_qp_attr *qp_attr = NULL;
1156 int attr_mask = 0;
1157 int comp = 0;
1158 int opcode = 0;
1160 switch (event->event) {
1161 case IB_CM_REQ_ERROR:
1162 printk(KERN_DEBUG PFX "Sending CM REQ failed\n");
1163 comp = 1;
1164 target->status = -ECONNRESET;
1165 break;
1167 case IB_CM_REP_RECEIVED:
1168 comp = 1;
1169 opcode = *(u8 *) event->private_data;
1171 if (opcode == SRP_LOGIN_RSP) {
1172 struct srp_login_rsp *rsp = event->private_data;
1174 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1175 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1177 target->scsi_host->can_queue = min(target->req_lim,
1178 target->scsi_host->can_queue);
1179 } else {
1180 printk(KERN_WARNING PFX "Unhandled RSP opcode %#x\n", opcode);
1181 target->status = -ECONNRESET;
1182 break;
1185 target->status = srp_alloc_iu_bufs(target);
1186 if (target->status)
1187 break;
1189 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1190 if (!qp_attr) {
1191 target->status = -ENOMEM;
1192 break;
1195 qp_attr->qp_state = IB_QPS_RTR;
1196 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1197 if (target->status)
1198 break;
1200 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1201 if (target->status)
1202 break;
1204 target->status = srp_post_recv(target);
1205 if (target->status)
1206 break;
1208 qp_attr->qp_state = IB_QPS_RTS;
1209 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1210 if (target->status)
1211 break;
1213 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1214 if (target->status)
1215 break;
1217 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1218 if (target->status)
1219 break;
1221 break;
1223 case IB_CM_REJ_RECEIVED:
1224 printk(KERN_DEBUG PFX "REJ received\n");
1225 comp = 1;
1227 srp_cm_rej_handler(cm_id, event, target);
1228 break;
1230 case IB_CM_DREQ_RECEIVED:
1231 printk(KERN_WARNING PFX "DREQ received - connection closed\n");
1232 if (ib_send_cm_drep(cm_id, NULL, 0))
1233 printk(KERN_ERR PFX "Sending CM DREP failed\n");
1234 break;
1236 case IB_CM_TIMEWAIT_EXIT:
1237 printk(KERN_ERR PFX "connection closed\n");
1239 comp = 1;
1240 target->status = 0;
1241 break;
1243 case IB_CM_MRA_RECEIVED:
1244 case IB_CM_DREQ_ERROR:
1245 case IB_CM_DREP_RECEIVED:
1246 break;
1248 default:
1249 printk(KERN_WARNING PFX "Unhandled CM event %d\n", event->event);
1250 break;
1253 if (comp)
1254 complete(&target->done);
1256 kfree(qp_attr);
1258 return 0;
1261 static int srp_send_tsk_mgmt(struct srp_target_port *target,
1262 struct srp_request *req, u8 func)
1264 struct srp_iu *iu;
1265 struct srp_tsk_mgmt *tsk_mgmt;
1267 spin_lock_irq(target->scsi_host->host_lock);
1269 if (target->state == SRP_TARGET_DEAD ||
1270 target->state == SRP_TARGET_REMOVED) {
1271 req->scmnd->result = DID_BAD_TARGET << 16;
1272 goto out;
1275 init_completion(&req->done);
1277 iu = __srp_get_tx_iu(target);
1278 if (!iu)
1279 goto out;
1281 tsk_mgmt = iu->buf;
1282 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1284 tsk_mgmt->opcode = SRP_TSK_MGMT;
1285 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1286 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
1287 tsk_mgmt->tsk_mgmt_func = func;
1288 tsk_mgmt->task_tag = req->index;
1290 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1291 goto out;
1293 req->tsk_mgmt = iu;
1295 spin_unlock_irq(target->scsi_host->host_lock);
1297 if (!wait_for_completion_timeout(&req->done,
1298 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
1299 return -1;
1301 return 0;
1303 out:
1304 spin_unlock_irq(target->scsi_host->host_lock);
1305 return -1;
1308 static int srp_find_req(struct srp_target_port *target,
1309 struct scsi_cmnd *scmnd,
1310 struct srp_request **req)
1312 if (scmnd->host_scribble == (void *) -1L)
1313 return -1;
1315 *req = &target->req_ring[(long) scmnd->host_scribble];
1317 return 0;
1320 static int srp_abort(struct scsi_cmnd *scmnd)
1322 struct srp_target_port *target = host_to_target(scmnd->device->host);
1323 struct srp_request *req;
1324 int ret = SUCCESS;
1326 printk(KERN_ERR "SRP abort called\n");
1328 if (srp_find_req(target, scmnd, &req))
1329 return FAILED;
1330 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1331 return FAILED;
1333 spin_lock_irq(target->scsi_host->host_lock);
1335 if (req->cmd_done) {
1336 srp_remove_req(target, req);
1337 scmnd->scsi_done(scmnd);
1338 } else if (!req->tsk_status) {
1339 srp_remove_req(target, req);
1340 scmnd->result = DID_ABORT << 16;
1341 } else
1342 ret = FAILED;
1344 spin_unlock_irq(target->scsi_host->host_lock);
1346 return ret;
1349 static int srp_reset_device(struct scsi_cmnd *scmnd)
1351 struct srp_target_port *target = host_to_target(scmnd->device->host);
1352 struct srp_request *req, *tmp;
1354 printk(KERN_ERR "SRP reset_device called\n");
1356 if (srp_find_req(target, scmnd, &req))
1357 return FAILED;
1358 if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1359 return FAILED;
1360 if (req->tsk_status)
1361 return FAILED;
1363 spin_lock_irq(target->scsi_host->host_lock);
1365 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
1366 if (req->scmnd->device == scmnd->device)
1367 srp_reset_req(target, req);
1369 spin_unlock_irq(target->scsi_host->host_lock);
1371 return SUCCESS;
1374 static int srp_reset_host(struct scsi_cmnd *scmnd)
1376 struct srp_target_port *target = host_to_target(scmnd->device->host);
1377 int ret = FAILED;
1379 printk(KERN_ERR PFX "SRP reset_host called\n");
1381 if (!srp_reconnect_target(target))
1382 ret = SUCCESS;
1384 return ret;
1387 static ssize_t show_id_ext(struct class_device *cdev, char *buf)
1389 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1391 if (target->state == SRP_TARGET_DEAD ||
1392 target->state == SRP_TARGET_REMOVED)
1393 return -ENODEV;
1395 return sprintf(buf, "0x%016llx\n",
1396 (unsigned long long) be64_to_cpu(target->id_ext));
1399 static ssize_t show_ioc_guid(struct class_device *cdev, char *buf)
1401 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1403 if (target->state == SRP_TARGET_DEAD ||
1404 target->state == SRP_TARGET_REMOVED)
1405 return -ENODEV;
1407 return sprintf(buf, "0x%016llx\n",
1408 (unsigned long long) be64_to_cpu(target->ioc_guid));
1411 static ssize_t show_service_id(struct class_device *cdev, char *buf)
1413 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1415 if (target->state == SRP_TARGET_DEAD ||
1416 target->state == SRP_TARGET_REMOVED)
1417 return -ENODEV;
1419 return sprintf(buf, "0x%016llx\n",
1420 (unsigned long long) be64_to_cpu(target->service_id));
1423 static ssize_t show_pkey(struct class_device *cdev, char *buf)
1425 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1427 if (target->state == SRP_TARGET_DEAD ||
1428 target->state == SRP_TARGET_REMOVED)
1429 return -ENODEV;
1431 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1434 static ssize_t show_dgid(struct class_device *cdev, char *buf)
1436 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1438 if (target->state == SRP_TARGET_DEAD ||
1439 target->state == SRP_TARGET_REMOVED)
1440 return -ENODEV;
1442 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1443 be16_to_cpu(((__be16 *) target->path.dgid.raw)[0]),
1444 be16_to_cpu(((__be16 *) target->path.dgid.raw)[1]),
1445 be16_to_cpu(((__be16 *) target->path.dgid.raw)[2]),
1446 be16_to_cpu(((__be16 *) target->path.dgid.raw)[3]),
1447 be16_to_cpu(((__be16 *) target->path.dgid.raw)[4]),
1448 be16_to_cpu(((__be16 *) target->path.dgid.raw)[5]),
1449 be16_to_cpu(((__be16 *) target->path.dgid.raw)[6]),
1450 be16_to_cpu(((__be16 *) target->path.dgid.raw)[7]));
1453 static ssize_t show_zero_req_lim(struct class_device *cdev, char *buf)
1455 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1457 if (target->state == SRP_TARGET_DEAD ||
1458 target->state == SRP_TARGET_REMOVED)
1459 return -ENODEV;
1461 return sprintf(buf, "%d\n", target->zero_req_lim);
1464 static CLASS_DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1465 static CLASS_DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1466 static CLASS_DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1467 static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1468 static CLASS_DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1469 static CLASS_DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1471 static struct class_device_attribute *srp_host_attrs[] = {
1472 &class_device_attr_id_ext,
1473 &class_device_attr_ioc_guid,
1474 &class_device_attr_service_id,
1475 &class_device_attr_pkey,
1476 &class_device_attr_dgid,
1477 &class_device_attr_zero_req_lim,
1478 NULL
1481 static struct scsi_host_template srp_template = {
1482 .module = THIS_MODULE,
1483 .name = DRV_NAME,
1484 .info = srp_target_info,
1485 .queuecommand = srp_queuecommand,
1486 .eh_abort_handler = srp_abort,
1487 .eh_device_reset_handler = srp_reset_device,
1488 .eh_host_reset_handler = srp_reset_host,
1489 .can_queue = SRP_SQ_SIZE,
1490 .this_id = -1,
1491 .cmd_per_lun = SRP_SQ_SIZE,
1492 .use_clustering = ENABLE_CLUSTERING,
1493 .shost_attrs = srp_host_attrs
1496 static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1498 sprintf(target->target_name, "SRP.T10:%016llX",
1499 (unsigned long long) be64_to_cpu(target->id_ext));
1501 if (scsi_add_host(target->scsi_host, host->dev->dev->dma_device))
1502 return -ENODEV;
1504 spin_lock(&host->target_lock);
1505 list_add_tail(&target->list, &host->target_list);
1506 spin_unlock(&host->target_lock);
1508 target->state = SRP_TARGET_LIVE;
1510 scsi_scan_target(&target->scsi_host->shost_gendev,
1511 0, target->scsi_id, SCAN_WILD_CARD, 0);
1513 return 0;
1516 static void srp_release_class_dev(struct class_device *class_dev)
1518 struct srp_host *host =
1519 container_of(class_dev, struct srp_host, class_dev);
1521 complete(&host->released);
1524 static struct class srp_class = {
1525 .name = "infiniband_srp",
1526 .release = srp_release_class_dev
1530 * Target ports are added by writing
1532 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1533 * pkey=<P_Key>,service_id=<service ID>
1535 * to the add_target sysfs attribute.
1537 enum {
1538 SRP_OPT_ERR = 0,
1539 SRP_OPT_ID_EXT = 1 << 0,
1540 SRP_OPT_IOC_GUID = 1 << 1,
1541 SRP_OPT_DGID = 1 << 2,
1542 SRP_OPT_PKEY = 1 << 3,
1543 SRP_OPT_SERVICE_ID = 1 << 4,
1544 SRP_OPT_MAX_SECT = 1 << 5,
1545 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
1546 SRP_OPT_IO_CLASS = 1 << 7,
1547 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1548 SRP_OPT_IOC_GUID |
1549 SRP_OPT_DGID |
1550 SRP_OPT_PKEY |
1551 SRP_OPT_SERVICE_ID),
1554 static match_table_t srp_opt_tokens = {
1555 { SRP_OPT_ID_EXT, "id_ext=%s" },
1556 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1557 { SRP_OPT_DGID, "dgid=%s" },
1558 { SRP_OPT_PKEY, "pkey=%x" },
1559 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1560 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1561 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
1562 { SRP_OPT_IO_CLASS, "io_class=%x" },
1563 { SRP_OPT_ERR, NULL }
1566 static int srp_parse_options(const char *buf, struct srp_target_port *target)
1568 char *options, *sep_opt;
1569 char *p;
1570 char dgid[3];
1571 substring_t args[MAX_OPT_ARGS];
1572 int opt_mask = 0;
1573 int token;
1574 int ret = -EINVAL;
1575 int i;
1577 options = kstrdup(buf, GFP_KERNEL);
1578 if (!options)
1579 return -ENOMEM;
1581 sep_opt = options;
1582 while ((p = strsep(&sep_opt, ",")) != NULL) {
1583 if (!*p)
1584 continue;
1586 token = match_token(p, srp_opt_tokens, args);
1587 opt_mask |= token;
1589 switch (token) {
1590 case SRP_OPT_ID_EXT:
1591 p = match_strdup(args);
1592 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1593 kfree(p);
1594 break;
1596 case SRP_OPT_IOC_GUID:
1597 p = match_strdup(args);
1598 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1599 kfree(p);
1600 break;
1602 case SRP_OPT_DGID:
1603 p = match_strdup(args);
1604 if (strlen(p) != 32) {
1605 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
1606 kfree(p);
1607 goto out;
1610 for (i = 0; i < 16; ++i) {
1611 strlcpy(dgid, p + i * 2, 3);
1612 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1614 kfree(p);
1615 break;
1617 case SRP_OPT_PKEY:
1618 if (match_hex(args, &token)) {
1619 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1620 goto out;
1622 target->path.pkey = cpu_to_be16(token);
1623 break;
1625 case SRP_OPT_SERVICE_ID:
1626 p = match_strdup(args);
1627 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
1628 kfree(p);
1629 break;
1631 case SRP_OPT_MAX_SECT:
1632 if (match_int(args, &token)) {
1633 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1634 goto out;
1636 target->scsi_host->max_sectors = token;
1637 break;
1639 case SRP_OPT_MAX_CMD_PER_LUN:
1640 if (match_int(args, &token)) {
1641 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1642 goto out;
1644 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1645 break;
1647 case SRP_OPT_IO_CLASS:
1648 if (match_hex(args, &token)) {
1649 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1650 goto out;
1652 if (token != SRP_REV10_IB_IO_CLASS &&
1653 token != SRP_REV16A_IB_IO_CLASS) {
1654 printk(KERN_WARNING PFX "unknown IO class parameter value"
1655 " %x specified (use %x or %x).\n",
1656 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1657 goto out;
1659 target->io_class = token;
1660 break;
1662 default:
1663 printk(KERN_WARNING PFX "unknown parameter or missing value "
1664 "'%s' in target creation request\n", p);
1665 goto out;
1669 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1670 ret = 0;
1671 else
1672 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1673 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1674 !(srp_opt_tokens[i].token & opt_mask))
1675 printk(KERN_WARNING PFX "target creation request is "
1676 "missing parameter '%s'\n",
1677 srp_opt_tokens[i].pattern);
1679 out:
1680 kfree(options);
1681 return ret;
1684 static ssize_t srp_create_target(struct class_device *class_dev,
1685 const char *buf, size_t count)
1687 struct srp_host *host =
1688 container_of(class_dev, struct srp_host, class_dev);
1689 struct Scsi_Host *target_host;
1690 struct srp_target_port *target;
1691 int ret;
1692 int i;
1694 target_host = scsi_host_alloc(&srp_template,
1695 sizeof (struct srp_target_port));
1696 if (!target_host)
1697 return -ENOMEM;
1699 target_host->max_lun = SRP_MAX_LUN;
1701 target = host_to_target(target_host);
1702 memset(target, 0, sizeof *target);
1704 target->io_class = SRP_REV16A_IB_IO_CLASS;
1705 target->scsi_host = target_host;
1706 target->srp_host = host;
1708 INIT_WORK(&target->work, srp_reconnect_work, target);
1710 INIT_LIST_HEAD(&target->free_reqs);
1711 INIT_LIST_HEAD(&target->req_queue);
1712 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1713 target->req_ring[i].index = i;
1714 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1717 ret = srp_parse_options(buf, target);
1718 if (ret)
1719 goto err;
1721 ib_get_cached_gid(host->dev->dev, host->port, 0, &target->path.sgid);
1723 printk(KERN_DEBUG PFX "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1724 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1725 (unsigned long long) be64_to_cpu(target->id_ext),
1726 (unsigned long long) be64_to_cpu(target->ioc_guid),
1727 be16_to_cpu(target->path.pkey),
1728 (unsigned long long) be64_to_cpu(target->service_id),
1729 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]),
1730 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]),
1731 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]),
1732 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]),
1733 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]),
1734 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]),
1735 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]),
1736 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14]));
1738 ret = srp_create_target_ib(target);
1739 if (ret)
1740 goto err;
1742 target->cm_id = ib_create_cm_id(host->dev->dev, srp_cm_handler, target);
1743 if (IS_ERR(target->cm_id)) {
1744 ret = PTR_ERR(target->cm_id);
1745 goto err_free;
1748 ret = srp_connect_target(target);
1749 if (ret) {
1750 printk(KERN_ERR PFX "Connection failed\n");
1751 goto err_cm_id;
1754 ret = srp_add_target(host, target);
1755 if (ret)
1756 goto err_disconnect;
1758 return count;
1760 err_disconnect:
1761 srp_disconnect_target(target);
1763 err_cm_id:
1764 ib_destroy_cm_id(target->cm_id);
1766 err_free:
1767 srp_free_target_ib(target);
1769 err:
1770 scsi_host_put(target_host);
1772 return ret;
1775 static CLASS_DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
1777 static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
1779 struct srp_host *host =
1780 container_of(class_dev, struct srp_host, class_dev);
1782 return sprintf(buf, "%s\n", host->dev->dev->name);
1785 static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1787 static ssize_t show_port(struct class_device *class_dev, char *buf)
1789 struct srp_host *host =
1790 container_of(class_dev, struct srp_host, class_dev);
1792 return sprintf(buf, "%d\n", host->port);
1795 static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
1797 static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
1799 struct srp_host *host;
1801 host = kzalloc(sizeof *host, GFP_KERNEL);
1802 if (!host)
1803 return NULL;
1805 INIT_LIST_HEAD(&host->target_list);
1806 spin_lock_init(&host->target_lock);
1807 init_completion(&host->released);
1808 host->dev = device;
1809 host->port = port;
1811 host->initiator_port_id[7] = port;
1812 memcpy(host->initiator_port_id + 8, &device->dev->node_guid, 8);
1814 host->class_dev.class = &srp_class;
1815 host->class_dev.dev = device->dev->dma_device;
1816 snprintf(host->class_dev.class_id, BUS_ID_SIZE, "srp-%s-%d",
1817 device->dev->name, port);
1819 if (class_device_register(&host->class_dev))
1820 goto free_host;
1821 if (class_device_create_file(&host->class_dev, &class_device_attr_add_target))
1822 goto err_class;
1823 if (class_device_create_file(&host->class_dev, &class_device_attr_ibdev))
1824 goto err_class;
1825 if (class_device_create_file(&host->class_dev, &class_device_attr_port))
1826 goto err_class;
1828 return host;
1830 err_class:
1831 class_device_unregister(&host->class_dev);
1833 free_host:
1834 kfree(host);
1836 return NULL;
1839 static void srp_add_one(struct ib_device *device)
1841 struct srp_device *srp_dev;
1842 struct ib_device_attr *dev_attr;
1843 struct ib_fmr_pool_param fmr_param;
1844 struct srp_host *host;
1845 int s, e, p;
1847 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
1848 if (!dev_attr)
1849 return;
1851 if (ib_query_device(device, dev_attr)) {
1852 printk(KERN_WARNING PFX "Query device failed for %s\n",
1853 device->name);
1854 goto free_attr;
1857 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
1858 if (!srp_dev)
1859 goto free_attr;
1862 * Use the smallest page size supported by the HCA, down to a
1863 * minimum of 512 bytes (which is the smallest sector that a
1864 * SCSI command will ever carry).
1866 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
1867 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
1868 srp_dev->fmr_page_mask = ~((unsigned long) srp_dev->fmr_page_size - 1);
1870 INIT_LIST_HEAD(&srp_dev->dev_list);
1872 srp_dev->dev = device;
1873 srp_dev->pd = ib_alloc_pd(device);
1874 if (IS_ERR(srp_dev->pd))
1875 goto free_dev;
1877 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
1878 IB_ACCESS_LOCAL_WRITE |
1879 IB_ACCESS_REMOTE_READ |
1880 IB_ACCESS_REMOTE_WRITE);
1881 if (IS_ERR(srp_dev->mr))
1882 goto err_pd;
1884 memset(&fmr_param, 0, sizeof fmr_param);
1885 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
1886 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
1887 fmr_param.cache = 1;
1888 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
1889 fmr_param.page_shift = srp_dev->fmr_page_shift;
1890 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
1891 IB_ACCESS_REMOTE_WRITE |
1892 IB_ACCESS_REMOTE_READ);
1894 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
1895 if (IS_ERR(srp_dev->fmr_pool))
1896 srp_dev->fmr_pool = NULL;
1898 if (device->node_type == IB_NODE_SWITCH) {
1899 s = 0;
1900 e = 0;
1901 } else {
1902 s = 1;
1903 e = device->phys_port_cnt;
1906 for (p = s; p <= e; ++p) {
1907 host = srp_add_port(srp_dev, p);
1908 if (host)
1909 list_add_tail(&host->list, &srp_dev->dev_list);
1912 ib_set_client_data(device, &srp_client, srp_dev);
1914 goto free_attr;
1916 err_pd:
1917 ib_dealloc_pd(srp_dev->pd);
1919 free_dev:
1920 kfree(srp_dev);
1922 free_attr:
1923 kfree(dev_attr);
1926 static void srp_remove_one(struct ib_device *device)
1928 struct srp_device *srp_dev;
1929 struct srp_host *host, *tmp_host;
1930 LIST_HEAD(target_list);
1931 struct srp_target_port *target, *tmp_target;
1933 srp_dev = ib_get_client_data(device, &srp_client);
1935 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
1936 class_device_unregister(&host->class_dev);
1938 * Wait for the sysfs entry to go away, so that no new
1939 * target ports can be created.
1941 wait_for_completion(&host->released);
1944 * Mark all target ports as removed, so we stop queueing
1945 * commands and don't try to reconnect.
1947 spin_lock(&host->target_lock);
1948 list_for_each_entry(target, &host->target_list, list) {
1949 spin_lock_irq(target->scsi_host->host_lock);
1950 target->state = SRP_TARGET_REMOVED;
1951 spin_unlock_irq(target->scsi_host->host_lock);
1953 spin_unlock(&host->target_lock);
1956 * Wait for any reconnection tasks that may have
1957 * started before we marked our target ports as
1958 * removed, and any target port removal tasks.
1960 flush_scheduled_work();
1962 list_for_each_entry_safe(target, tmp_target,
1963 &host->target_list, list) {
1964 scsi_remove_host(target->scsi_host);
1965 srp_disconnect_target(target);
1966 ib_destroy_cm_id(target->cm_id);
1967 srp_free_target_ib(target);
1968 scsi_host_put(target->scsi_host);
1971 kfree(host);
1974 if (srp_dev->fmr_pool)
1975 ib_destroy_fmr_pool(srp_dev->fmr_pool);
1976 ib_dereg_mr(srp_dev->mr);
1977 ib_dealloc_pd(srp_dev->pd);
1979 kfree(srp_dev);
1982 static int __init srp_init_module(void)
1984 int ret;
1986 srp_template.sg_tablesize = srp_sg_tablesize;
1987 srp_max_iu_len = (sizeof (struct srp_cmd) +
1988 sizeof (struct srp_indirect_buf) +
1989 srp_sg_tablesize * 16);
1991 ret = class_register(&srp_class);
1992 if (ret) {
1993 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
1994 return ret;
1997 ret = ib_register_client(&srp_client);
1998 if (ret) {
1999 printk(KERN_ERR PFX "couldn't register IB client\n");
2000 class_unregister(&srp_class);
2001 return ret;
2004 return 0;
2007 static void __exit srp_cleanup_module(void)
2009 ib_unregister_client(&srp_client);
2010 class_unregister(&srp_class);
2013 module_init(srp_init_module);
2014 module_exit(srp_cleanup_module);