[SCSI] ib_srp: convert to use the srp transport class
[linux-2.6.git] / drivers / infiniband / ulp / srp / ib_srp.c
1 /*
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
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>
43
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 #include <scsi/scsi_transport_srp.h>
51
52 #include <rdma/ib_cache.h>
53
54 #include "ib_srp.h"
55
56 #define DRV_NAME        "ib_srp"
57 #define PFX             DRV_NAME ": "
58 #define DRV_VERSION     "0.2"
59 #define DRV_RELDATE     "November 1, 2005"
60
61 MODULE_AUTHOR("Roland Dreier");
62 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
63                    "v" DRV_VERSION " (" DRV_RELDATE ")");
64 MODULE_LICENSE("Dual BSD/GPL");
65
66 static int srp_sg_tablesize = SRP_DEF_SG_TABLESIZE;
67 static int srp_max_iu_len;
68
69 module_param(srp_sg_tablesize, int, 0444);
70 MODULE_PARM_DESC(srp_sg_tablesize,
71                  "Max number of gather/scatter entries per I/O (default is 12)");
72
73 static int topspin_workarounds = 1;
74
75 module_param(topspin_workarounds, int, 0444);
76 MODULE_PARM_DESC(topspin_workarounds,
77                  "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
78
79 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
80
81 static int mellanox_workarounds = 1;
82
83 module_param(mellanox_workarounds, int, 0444);
84 MODULE_PARM_DESC(mellanox_workarounds,
85                  "Enable workarounds for Mellanox SRP target bugs if != 0");
86
87 static const u8 mellanox_oui[3] = { 0x00, 0x02, 0xc9 };
88
89 static void srp_add_one(struct ib_device *device);
90 static void srp_remove_one(struct ib_device *device);
91 static void srp_completion(struct ib_cq *cq, void *target_ptr);
92 static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
93
94 static struct scsi_transport_template *ib_srp_transport_template;
95
96 static struct ib_client srp_client = {
97         .name   = "srp",
98         .add    = srp_add_one,
99         .remove = srp_remove_one
100 };
101
102 static struct ib_sa_client srp_sa_client;
103
104 static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
105 {
106         return (struct srp_target_port *) host->hostdata;
107 }
108
109 static const char *srp_target_info(struct Scsi_Host *host)
110 {
111         return host_to_target(host)->target_name;
112 }
113
114 static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
115                                    gfp_t gfp_mask,
116                                    enum dma_data_direction direction)
117 {
118         struct srp_iu *iu;
119
120         iu = kmalloc(sizeof *iu, gfp_mask);
121         if (!iu)
122                 goto out;
123
124         iu->buf = kzalloc(size, gfp_mask);
125         if (!iu->buf)
126                 goto out_free_iu;
127
128         iu->dma = ib_dma_map_single(host->dev->dev, iu->buf, size, direction);
129         if (ib_dma_mapping_error(host->dev->dev, iu->dma))
130                 goto out_free_buf;
131
132         iu->size      = size;
133         iu->direction = direction;
134
135         return iu;
136
137 out_free_buf:
138         kfree(iu->buf);
139 out_free_iu:
140         kfree(iu);
141 out:
142         return NULL;
143 }
144
145 static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
146 {
147         if (!iu)
148                 return;
149
150         ib_dma_unmap_single(host->dev->dev, iu->dma, iu->size, iu->direction);
151         kfree(iu->buf);
152         kfree(iu);
153 }
154
155 static void srp_qp_event(struct ib_event *event, void *context)
156 {
157         printk(KERN_ERR PFX "QP event %d\n", event->event);
158 }
159
160 static int srp_init_qp(struct srp_target_port *target,
161                        struct ib_qp *qp)
162 {
163         struct ib_qp_attr *attr;
164         int ret;
165
166         attr = kmalloc(sizeof *attr, GFP_KERNEL);
167         if (!attr)
168                 return -ENOMEM;
169
170         ret = ib_find_cached_pkey(target->srp_host->dev->dev,
171                                   target->srp_host->port,
172                                   be16_to_cpu(target->path.pkey),
173                                   &attr->pkey_index);
174         if (ret)
175                 goto out;
176
177         attr->qp_state        = IB_QPS_INIT;
178         attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
179                                     IB_ACCESS_REMOTE_WRITE);
180         attr->port_num        = target->srp_host->port;
181
182         ret = ib_modify_qp(qp, attr,
183                            IB_QP_STATE          |
184                            IB_QP_PKEY_INDEX     |
185                            IB_QP_ACCESS_FLAGS   |
186                            IB_QP_PORT);
187
188 out:
189         kfree(attr);
190         return ret;
191 }
192
193 static int srp_create_target_ib(struct srp_target_port *target)
194 {
195         struct ib_qp_init_attr *init_attr;
196         int ret;
197
198         init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
199         if (!init_attr)
200                 return -ENOMEM;
201
202         target->cq = ib_create_cq(target->srp_host->dev->dev, srp_completion,
203                                   NULL, target, SRP_CQ_SIZE, 0);
204         if (IS_ERR(target->cq)) {
205                 ret = PTR_ERR(target->cq);
206                 goto out;
207         }
208
209         ib_req_notify_cq(target->cq, IB_CQ_NEXT_COMP);
210
211         init_attr->event_handler       = srp_qp_event;
212         init_attr->cap.max_send_wr     = SRP_SQ_SIZE;
213         init_attr->cap.max_recv_wr     = SRP_RQ_SIZE;
214         init_attr->cap.max_recv_sge    = 1;
215         init_attr->cap.max_send_sge    = 1;
216         init_attr->sq_sig_type         = IB_SIGNAL_ALL_WR;
217         init_attr->qp_type             = IB_QPT_RC;
218         init_attr->send_cq             = target->cq;
219         init_attr->recv_cq             = target->cq;
220
221         target->qp = ib_create_qp(target->srp_host->dev->pd, init_attr);
222         if (IS_ERR(target->qp)) {
223                 ret = PTR_ERR(target->qp);
224                 ib_destroy_cq(target->cq);
225                 goto out;
226         }
227
228         ret = srp_init_qp(target, target->qp);
229         if (ret) {
230                 ib_destroy_qp(target->qp);
231                 ib_destroy_cq(target->cq);
232                 goto out;
233         }
234
235 out:
236         kfree(init_attr);
237         return ret;
238 }
239
240 static void srp_free_target_ib(struct srp_target_port *target)
241 {
242         int i;
243
244         ib_destroy_qp(target->qp);
245         ib_destroy_cq(target->cq);
246
247         for (i = 0; i < SRP_RQ_SIZE; ++i)
248                 srp_free_iu(target->srp_host, target->rx_ring[i]);
249         for (i = 0; i < SRP_SQ_SIZE + 1; ++i)
250                 srp_free_iu(target->srp_host, target->tx_ring[i]);
251 }
252
253 static void srp_path_rec_completion(int status,
254                                     struct ib_sa_path_rec *pathrec,
255                                     void *target_ptr)
256 {
257         struct srp_target_port *target = target_ptr;
258
259         target->status = status;
260         if (status)
261                 printk(KERN_ERR PFX "Got failed path rec status %d\n", status);
262         else
263                 target->path = *pathrec;
264         complete(&target->done);
265 }
266
267 static int srp_lookup_path(struct srp_target_port *target)
268 {
269         target->path.numb_path = 1;
270
271         init_completion(&target->done);
272
273         target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
274                                                    target->srp_host->dev->dev,
275                                                    target->srp_host->port,
276                                                    &target->path,
277                                                    IB_SA_PATH_REC_DGID          |
278                                                    IB_SA_PATH_REC_SGID          |
279                                                    IB_SA_PATH_REC_NUMB_PATH     |
280                                                    IB_SA_PATH_REC_PKEY,
281                                                    SRP_PATH_REC_TIMEOUT_MS,
282                                                    GFP_KERNEL,
283                                                    srp_path_rec_completion,
284                                                    target, &target->path_query);
285         if (target->path_query_id < 0)
286                 return target->path_query_id;
287
288         wait_for_completion(&target->done);
289
290         if (target->status < 0)
291                 printk(KERN_WARNING PFX "Path record query failed\n");
292
293         return target->status;
294 }
295
296 static int srp_send_req(struct srp_target_port *target)
297 {
298         struct {
299                 struct ib_cm_req_param param;
300                 struct srp_login_req   priv;
301         } *req = NULL;
302         int status;
303
304         req = kzalloc(sizeof *req, GFP_KERNEL);
305         if (!req)
306                 return -ENOMEM;
307
308         req->param.primary_path               = &target->path;
309         req->param.alternate_path             = NULL;
310         req->param.service_id                 = target->service_id;
311         req->param.qp_num                     = target->qp->qp_num;
312         req->param.qp_type                    = target->qp->qp_type;
313         req->param.private_data               = &req->priv;
314         req->param.private_data_len           = sizeof req->priv;
315         req->param.flow_control               = 1;
316
317         get_random_bytes(&req->param.starting_psn, 4);
318         req->param.starting_psn              &= 0xffffff;
319
320         /*
321          * Pick some arbitrary defaults here; we could make these
322          * module parameters if anyone cared about setting them.
323          */
324         req->param.responder_resources        = 4;
325         req->param.remote_cm_response_timeout = 20;
326         req->param.local_cm_response_timeout  = 20;
327         req->param.retry_count                = 7;
328         req->param.rnr_retry_count            = 7;
329         req->param.max_cm_retries             = 15;
330
331         req->priv.opcode        = SRP_LOGIN_REQ;
332         req->priv.tag           = 0;
333         req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
334         req->priv.req_buf_fmt   = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
335                                               SRP_BUF_FORMAT_INDIRECT);
336         /*
337          * In the published SRP specification (draft rev. 16a), the
338          * port identifier format is 8 bytes of ID extension followed
339          * by 8 bytes of GUID.  Older drafts put the two halves in the
340          * opposite order, so that the GUID comes first.
341          *
342          * Targets conforming to these obsolete drafts can be
343          * recognized by the I/O Class they report.
344          */
345         if (target->io_class == SRP_REV10_IB_IO_CLASS) {
346                 memcpy(req->priv.initiator_port_id,
347                        &target->path.sgid.global.interface_id, 8);
348                 memcpy(req->priv.initiator_port_id + 8,
349                        &target->initiator_ext, 8);
350                 memcpy(req->priv.target_port_id,     &target->ioc_guid, 8);
351                 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
352         } else {
353                 memcpy(req->priv.initiator_port_id,
354                        &target->initiator_ext, 8);
355                 memcpy(req->priv.initiator_port_id + 8,
356                        &target->path.sgid.global.interface_id, 8);
357                 memcpy(req->priv.target_port_id,     &target->id_ext, 8);
358                 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
359         }
360
361         /*
362          * Topspin/Cisco SRP targets will reject our login unless we
363          * zero out the first 8 bytes of our initiator port ID and set
364          * the second 8 bytes to the local node GUID.
365          */
366         if (topspin_workarounds && !memcmp(&target->ioc_guid, topspin_oui, 3)) {
367                 printk(KERN_DEBUG PFX "Topspin/Cisco initiator port ID workaround "
368                        "activated for target GUID %016llx\n",
369                        (unsigned long long) be64_to_cpu(target->ioc_guid));
370                 memset(req->priv.initiator_port_id, 0, 8);
371                 memcpy(req->priv.initiator_port_id + 8,
372                        &target->srp_host->dev->dev->node_guid, 8);
373         }
374
375         status = ib_send_cm_req(target->cm_id, &req->param);
376
377         kfree(req);
378
379         return status;
380 }
381
382 static void srp_disconnect_target(struct srp_target_port *target)
383 {
384         /* XXX should send SRP_I_LOGOUT request */
385
386         init_completion(&target->done);
387         if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
388                 printk(KERN_DEBUG PFX "Sending CM DREQ failed\n");
389                 return;
390         }
391         wait_for_completion(&target->done);
392 }
393
394 static void srp_remove_work(struct work_struct *work)
395 {
396         struct srp_target_port *target =
397                 container_of(work, struct srp_target_port, work);
398
399         spin_lock_irq(target->scsi_host->host_lock);
400         if (target->state != SRP_TARGET_DEAD) {
401                 spin_unlock_irq(target->scsi_host->host_lock);
402                 return;
403         }
404         target->state = SRP_TARGET_REMOVED;
405         spin_unlock_irq(target->scsi_host->host_lock);
406
407         spin_lock(&target->srp_host->target_lock);
408         list_del(&target->list);
409         spin_unlock(&target->srp_host->target_lock);
410
411         srp_remove_host(target->scsi_host);
412         scsi_remove_host(target->scsi_host);
413         ib_destroy_cm_id(target->cm_id);
414         srp_free_target_ib(target);
415         scsi_host_put(target->scsi_host);
416 }
417
418 static int srp_connect_target(struct srp_target_port *target)
419 {
420         int ret;
421
422         ret = srp_lookup_path(target);
423         if (ret)
424                 return ret;
425
426         while (1) {
427                 init_completion(&target->done);
428                 ret = srp_send_req(target);
429                 if (ret)
430                         return ret;
431                 wait_for_completion(&target->done);
432
433                 /*
434                  * The CM event handling code will set status to
435                  * SRP_PORT_REDIRECT if we get a port redirect REJ
436                  * back, or SRP_DLID_REDIRECT if we get a lid/qp
437                  * redirect REJ back.
438                  */
439                 switch (target->status) {
440                 case 0:
441                         return 0;
442
443                 case SRP_PORT_REDIRECT:
444                         ret = srp_lookup_path(target);
445                         if (ret)
446                                 return ret;
447                         break;
448
449                 case SRP_DLID_REDIRECT:
450                         break;
451
452                 default:
453                         return target->status;
454                 }
455         }
456 }
457
458 static void srp_unmap_data(struct scsi_cmnd *scmnd,
459                            struct srp_target_port *target,
460                            struct srp_request *req)
461 {
462         if (!scsi_sglist(scmnd) ||
463             (scmnd->sc_data_direction != DMA_TO_DEVICE &&
464              scmnd->sc_data_direction != DMA_FROM_DEVICE))
465                 return;
466
467         if (req->fmr) {
468                 ib_fmr_pool_unmap(req->fmr);
469                 req->fmr = NULL;
470         }
471
472         ib_dma_unmap_sg(target->srp_host->dev->dev, scsi_sglist(scmnd),
473                         scsi_sg_count(scmnd), scmnd->sc_data_direction);
474 }
475
476 static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
477 {
478         srp_unmap_data(req->scmnd, target, req);
479         list_move_tail(&req->list, &target->free_reqs);
480 }
481
482 static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
483 {
484         req->scmnd->result = DID_RESET << 16;
485         req->scmnd->scsi_done(req->scmnd);
486         srp_remove_req(target, req);
487 }
488
489 static int srp_reconnect_target(struct srp_target_port *target)
490 {
491         struct ib_cm_id *new_cm_id;
492         struct ib_qp_attr qp_attr;
493         struct srp_request *req, *tmp;
494         struct ib_wc wc;
495         int ret;
496
497         spin_lock_irq(target->scsi_host->host_lock);
498         if (target->state != SRP_TARGET_LIVE) {
499                 spin_unlock_irq(target->scsi_host->host_lock);
500                 return -EAGAIN;
501         }
502         target->state = SRP_TARGET_CONNECTING;
503         spin_unlock_irq(target->scsi_host->host_lock);
504
505         srp_disconnect_target(target);
506         /*
507          * Now get a new local CM ID so that we avoid confusing the
508          * target in case things are really fouled up.
509          */
510         new_cm_id = ib_create_cm_id(target->srp_host->dev->dev,
511                                     srp_cm_handler, target);
512         if (IS_ERR(new_cm_id)) {
513                 ret = PTR_ERR(new_cm_id);
514                 goto err;
515         }
516         ib_destroy_cm_id(target->cm_id);
517         target->cm_id = new_cm_id;
518
519         qp_attr.qp_state = IB_QPS_RESET;
520         ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
521         if (ret)
522                 goto err;
523
524         ret = srp_init_qp(target, target->qp);
525         if (ret)
526                 goto err;
527
528         while (ib_poll_cq(target->cq, 1, &wc) > 0)
529                 ; /* nothing */
530
531         spin_lock_irq(target->scsi_host->host_lock);
532         list_for_each_entry_safe(req, tmp, &target->req_queue, list)
533                 srp_reset_req(target, req);
534         spin_unlock_irq(target->scsi_host->host_lock);
535
536         target->rx_head  = 0;
537         target->tx_head  = 0;
538         target->tx_tail  = 0;
539
540         target->qp_in_error = 0;
541         ret = srp_connect_target(target);
542         if (ret)
543                 goto err;
544
545         spin_lock_irq(target->scsi_host->host_lock);
546         if (target->state == SRP_TARGET_CONNECTING) {
547                 ret = 0;
548                 target->state = SRP_TARGET_LIVE;
549         } else
550                 ret = -EAGAIN;
551         spin_unlock_irq(target->scsi_host->host_lock);
552
553         return ret;
554
555 err:
556         printk(KERN_ERR PFX "reconnect failed (%d), removing target port.\n", ret);
557
558         /*
559          * We couldn't reconnect, so kill our target port off.
560          * However, we have to defer the real removal because we might
561          * be in the context of the SCSI error handler now, which
562          * would deadlock if we call scsi_remove_host().
563          */
564         spin_lock_irq(target->scsi_host->host_lock);
565         if (target->state == SRP_TARGET_CONNECTING) {
566                 target->state = SRP_TARGET_DEAD;
567                 INIT_WORK(&target->work, srp_remove_work);
568                 schedule_work(&target->work);
569         }
570         spin_unlock_irq(target->scsi_host->host_lock);
571
572         return ret;
573 }
574
575 static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat,
576                        int sg_cnt, struct srp_request *req,
577                        struct srp_direct_buf *buf)
578 {
579         u64 io_addr = 0;
580         u64 *dma_pages;
581         u32 len;
582         int page_cnt;
583         int i, j;
584         int ret;
585         struct srp_device *dev = target->srp_host->dev;
586         struct ib_device *ibdev = dev->dev;
587         struct scatterlist *sg;
588
589         if (!dev->fmr_pool)
590                 return -ENODEV;
591
592         if ((ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask) &&
593             mellanox_workarounds && !memcmp(&target->ioc_guid, mellanox_oui, 3))
594                 return -EINVAL;
595
596         len = page_cnt = 0;
597         scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
598                 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
599
600                 if (ib_sg_dma_address(ibdev, sg) & ~dev->fmr_page_mask) {
601                         if (i > 0)
602                                 return -EINVAL;
603                         else
604                                 ++page_cnt;
605                 }
606                 if ((ib_sg_dma_address(ibdev, sg) + dma_len) &
607                     ~dev->fmr_page_mask) {
608                         if (i < sg_cnt - 1)
609                                 return -EINVAL;
610                         else
611                                 ++page_cnt;
612                 }
613
614                 len += dma_len;
615         }
616
617         page_cnt += len >> dev->fmr_page_shift;
618         if (page_cnt > SRP_FMR_SIZE)
619                 return -ENOMEM;
620
621         dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
622         if (!dma_pages)
623                 return -ENOMEM;
624
625         page_cnt = 0;
626         scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
627                 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
628
629                 for (j = 0; j < dma_len; j += dev->fmr_page_size)
630                         dma_pages[page_cnt++] =
631                                 (ib_sg_dma_address(ibdev, sg) &
632                                  dev->fmr_page_mask) + j;
633         }
634
635         req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
636                                         dma_pages, page_cnt, io_addr);
637         if (IS_ERR(req->fmr)) {
638                 ret = PTR_ERR(req->fmr);
639                 req->fmr = NULL;
640                 goto out;
641         }
642
643         buf->va  = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) &
644                                ~dev->fmr_page_mask);
645         buf->key = cpu_to_be32(req->fmr->fmr->rkey);
646         buf->len = cpu_to_be32(len);
647
648         ret = 0;
649
650 out:
651         kfree(dma_pages);
652
653         return ret;
654 }
655
656 static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
657                         struct srp_request *req)
658 {
659         struct scatterlist *scat;
660         struct srp_cmd *cmd = req->cmd->buf;
661         int len, nents, count;
662         u8 fmt = SRP_DATA_DESC_DIRECT;
663         struct srp_device *dev;
664         struct ib_device *ibdev;
665
666         if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
667                 return sizeof (struct srp_cmd);
668
669         if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
670             scmnd->sc_data_direction != DMA_TO_DEVICE) {
671                 printk(KERN_WARNING PFX "Unhandled data direction %d\n",
672                        scmnd->sc_data_direction);
673                 return -EINVAL;
674         }
675
676         nents = scsi_sg_count(scmnd);
677         scat  = scsi_sglist(scmnd);
678
679         dev = target->srp_host->dev;
680         ibdev = dev->dev;
681
682         count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
683
684         fmt = SRP_DATA_DESC_DIRECT;
685         len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
686
687         if (count == 1) {
688                 /*
689                  * The midlayer only generated a single gather/scatter
690                  * entry, or DMA mapping coalesced everything to a
691                  * single entry.  So a direct descriptor along with
692                  * the DMA MR suffices.
693                  */
694                 struct srp_direct_buf *buf = (void *) cmd->add_data;
695
696                 buf->va  = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
697                 buf->key = cpu_to_be32(dev->mr->rkey);
698                 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
699         } else if (srp_map_fmr(target, scat, count, req,
700                                (void *) cmd->add_data)) {
701                 /*
702                  * FMR mapping failed, and the scatterlist has more
703                  * than one entry.  Generate an indirect memory
704                  * descriptor.
705                  */
706                 struct srp_indirect_buf *buf = (void *) cmd->add_data;
707                 struct scatterlist *sg;
708                 u32 datalen = 0;
709                 int i;
710
711                 fmt = SRP_DATA_DESC_INDIRECT;
712                 len = sizeof (struct srp_cmd) +
713                         sizeof (struct srp_indirect_buf) +
714                         count * sizeof (struct srp_direct_buf);
715
716                 scsi_for_each_sg(scmnd, sg, count, i) {
717                         unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
718
719                         buf->desc_list[i].va  =
720                                 cpu_to_be64(ib_sg_dma_address(ibdev, sg));
721                         buf->desc_list[i].key =
722                                 cpu_to_be32(dev->mr->rkey);
723                         buf->desc_list[i].len = cpu_to_be32(dma_len);
724                         datalen += dma_len;
725                 }
726
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;
731
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));
738
739                 buf->len = cpu_to_be32(datalen);
740         }
741
742         if (scmnd->sc_data_direction == DMA_TO_DEVICE)
743                 cmd->buf_fmt = fmt << 4;
744         else
745                 cmd->buf_fmt = fmt;
746
747         return len;
748 }
749
750 static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
751 {
752         struct srp_request *req;
753         struct scsi_cmnd *scmnd;
754         unsigned long flags;
755         s32 delta;
756
757         delta = (s32) be32_to_cpu(rsp->req_lim_delta);
758
759         spin_lock_irqsave(target->scsi_host->host_lock, flags);
760
761         target->req_lim += delta;
762
763         req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
764
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;
777
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));
783                 }
784
785                 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
786                         scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
787                 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
788                         scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
789
790                 if (!req->tsk_mgmt) {
791                         scmnd->host_scribble = (void *) -1L;
792                         scmnd->scsi_done(scmnd);
793
794                         srp_remove_req(target, req);
795                 } else
796                         req->cmd_done = 1;
797         }
798
799         spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
800 }
801
802 static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
803 {
804         struct ib_device *dev;
805         struct srp_iu *iu;
806         u8 opcode;
807
808         iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
809
810         dev = target->srp_host->dev->dev;
811         ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
812                                    DMA_FROM_DEVICE);
813
814         opcode = *(u8 *) iu->buf;
815
816         if (0) {
817                 int i;
818
819                 printk(KERN_ERR PFX "recv completion, opcode 0x%02x\n", opcode);
820
821                 for (i = 0; i < wc->byte_len; ++i) {
822                         if (i % 8 == 0)
823                                 printk(KERN_ERR "  [%02x] ", i);
824                         printk(" %02x", ((u8 *) iu->buf)[i]);
825                         if ((i + 1) % 8 == 0)
826                                 printk("\n");
827                 }
828
829                 if (wc->byte_len % 8)
830                         printk("\n");
831         }
832
833         switch (opcode) {
834         case SRP_RSP:
835                 srp_process_rsp(target, iu->buf);
836                 break;
837
838         case SRP_T_LOGOUT:
839                 /* XXX Handle target logout */
840                 printk(KERN_WARNING PFX "Got target logout request\n");
841                 break;
842
843         default:
844                 printk(KERN_WARNING PFX "Unhandled SRP opcode 0x%02x\n", opcode);
845                 break;
846         }
847
848         ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
849                                       DMA_FROM_DEVICE);
850 }
851
852 static void srp_completion(struct ib_cq *cq, void *target_ptr)
853 {
854         struct srp_target_port *target = target_ptr;
855         struct ib_wc wc;
856
857         ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
858         while (ib_poll_cq(cq, 1, &wc) > 0) {
859                 if (wc.status) {
860                         printk(KERN_ERR PFX "failed %s status %d\n",
861                                wc.wr_id & SRP_OP_RECV ? "receive" : "send",
862                                wc.status);
863                         target->qp_in_error = 1;
864                         break;
865                 }
866
867                 if (wc.wr_id & SRP_OP_RECV)
868                         srp_handle_recv(target, &wc);
869                 else
870                         ++target->tx_tail;
871         }
872 }
873
874 static int __srp_post_recv(struct srp_target_port *target)
875 {
876         struct srp_iu *iu;
877         struct ib_sge list;
878         struct ib_recv_wr wr, *bad_wr;
879         unsigned int next;
880         int ret;
881
882         next     = target->rx_head & (SRP_RQ_SIZE - 1);
883         wr.wr_id = next | SRP_OP_RECV;
884         iu       = target->rx_ring[next];
885
886         list.addr   = iu->dma;
887         list.length = iu->size;
888         list.lkey   = target->srp_host->dev->mr->lkey;
889
890         wr.next     = NULL;
891         wr.sg_list  = &list;
892         wr.num_sge  = 1;
893
894         ret = ib_post_recv(target->qp, &wr, &bad_wr);
895         if (!ret)
896                 ++target->rx_head;
897
898         return ret;
899 }
900
901 static int srp_post_recv(struct srp_target_port *target)
902 {
903         unsigned long flags;
904         int ret;
905
906         spin_lock_irqsave(target->scsi_host->host_lock, flags);
907         ret = __srp_post_recv(target);
908         spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
909
910         return ret;
911 }
912
913 /*
914  * Must be called with target->scsi_host->host_lock held to protect
915  * req_lim and tx_head.  Lock cannot be dropped between call here and
916  * call to __srp_post_send().
917  */
918 static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target)
919 {
920         if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
921                 return NULL;
922
923         if (unlikely(target->req_lim < 1))
924                 ++target->zero_req_lim;
925
926         return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
927 }
928
929 /*
930  * Must be called with target->scsi_host->host_lock held to protect
931  * req_lim and tx_head.
932  */
933 static int __srp_post_send(struct srp_target_port *target,
934                            struct srp_iu *iu, int len)
935 {
936         struct ib_sge list;
937         struct ib_send_wr wr, *bad_wr;
938         int ret = 0;
939
940         list.addr   = iu->dma;
941         list.length = len;
942         list.lkey   = target->srp_host->dev->mr->lkey;
943
944         wr.next       = NULL;
945         wr.wr_id      = target->tx_head & SRP_SQ_SIZE;
946         wr.sg_list    = &list;
947         wr.num_sge    = 1;
948         wr.opcode     = IB_WR_SEND;
949         wr.send_flags = IB_SEND_SIGNALED;
950
951         ret = ib_post_send(target->qp, &wr, &bad_wr);
952
953         if (!ret) {
954                 ++target->tx_head;
955                 --target->req_lim;
956         }
957
958         return ret;
959 }
960
961 static int srp_queuecommand(struct scsi_cmnd *scmnd,
962                             void (*done)(struct scsi_cmnd *))
963 {
964         struct srp_target_port *target = host_to_target(scmnd->device->host);
965         struct srp_request *req;
966         struct srp_iu *iu;
967         struct srp_cmd *cmd;
968         struct ib_device *dev;
969         int len;
970
971         if (target->state == SRP_TARGET_CONNECTING)
972                 goto err;
973
974         if (target->state == SRP_TARGET_DEAD ||
975             target->state == SRP_TARGET_REMOVED) {
976                 scmnd->result = DID_BAD_TARGET << 16;
977                 done(scmnd);
978                 return 0;
979         }
980
981         iu = __srp_get_tx_iu(target);
982         if (!iu)
983                 goto err;
984
985         dev = target->srp_host->dev->dev;
986         ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len,
987                                    DMA_TO_DEVICE);
988
989         req = list_entry(target->free_reqs.next, struct srp_request, list);
990
991         scmnd->scsi_done     = done;
992         scmnd->result        = 0;
993         scmnd->host_scribble = (void *) (long) req->index;
994
995         cmd = iu->buf;
996         memset(cmd, 0, sizeof *cmd);
997
998         cmd->opcode = SRP_CMD;
999         cmd->lun    = cpu_to_be64((u64) scmnd->device->lun << 48);
1000         cmd->tag    = req->index;
1001         memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1002
1003         req->scmnd    = scmnd;
1004         req->cmd      = iu;
1005         req->cmd_done = 0;
1006         req->tsk_mgmt = NULL;
1007
1008         len = srp_map_data(scmnd, target, req);
1009         if (len < 0) {
1010                 printk(KERN_ERR PFX "Failed to map data\n");
1011                 goto err;
1012         }
1013
1014         if (__srp_post_recv(target)) {
1015                 printk(KERN_ERR PFX "Recv failed\n");
1016                 goto err_unmap;
1017         }
1018
1019         ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len,
1020                                       DMA_TO_DEVICE);
1021
1022         if (__srp_post_send(target, iu, len)) {
1023                 printk(KERN_ERR PFX "Send failed\n");
1024                 goto err_unmap;
1025         }
1026
1027         list_move_tail(&req->list, &target->req_queue);
1028
1029         return 0;
1030
1031 err_unmap:
1032         srp_unmap_data(scmnd, target, req);
1033
1034 err:
1035         return SCSI_MLQUEUE_HOST_BUSY;
1036 }
1037
1038 static int srp_alloc_iu_bufs(struct srp_target_port *target)
1039 {
1040         int i;
1041
1042         for (i = 0; i < SRP_RQ_SIZE; ++i) {
1043                 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1044                                                   target->max_ti_iu_len,
1045                                                   GFP_KERNEL, DMA_FROM_DEVICE);
1046                 if (!target->rx_ring[i])
1047                         goto err;
1048         }
1049
1050         for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1051                 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
1052                                                   srp_max_iu_len,
1053                                                   GFP_KERNEL, DMA_TO_DEVICE);
1054                 if (!target->tx_ring[i])
1055                         goto err;
1056         }
1057
1058         return 0;
1059
1060 err:
1061         for (i = 0; i < SRP_RQ_SIZE; ++i) {
1062                 srp_free_iu(target->srp_host, target->rx_ring[i]);
1063                 target->rx_ring[i] = NULL;
1064         }
1065
1066         for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1067                 srp_free_iu(target->srp_host, target->tx_ring[i]);
1068                 target->tx_ring[i] = NULL;
1069         }
1070
1071         return -ENOMEM;
1072 }
1073
1074 static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1075                                struct ib_cm_event *event,
1076                                struct srp_target_port *target)
1077 {
1078         struct ib_class_port_info *cpi;
1079         int opcode;
1080
1081         switch (event->param.rej_rcvd.reason) {
1082         case IB_CM_REJ_PORT_CM_REDIRECT:
1083                 cpi = event->param.rej_rcvd.ari;
1084                 target->path.dlid = cpi->redirect_lid;
1085                 target->path.pkey = cpi->redirect_pkey;
1086                 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1087                 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1088
1089                 target->status = target->path.dlid ?
1090                         SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1091                 break;
1092
1093         case IB_CM_REJ_PORT_REDIRECT:
1094                 if (topspin_workarounds &&
1095                     !memcmp(&target->ioc_guid, topspin_oui, 3)) {
1096                         /*
1097                          * Topspin/Cisco SRP gateways incorrectly send
1098                          * reject reason code 25 when they mean 24
1099                          * (port redirect).
1100                          */
1101                         memcpy(target->path.dgid.raw,
1102                                event->param.rej_rcvd.ari, 16);
1103
1104                         printk(KERN_DEBUG PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1105                                (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1106                                (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1107
1108                         target->status = SRP_PORT_REDIRECT;
1109                 } else {
1110                         printk(KERN_WARNING "  REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1111                         target->status = -ECONNRESET;
1112                 }
1113                 break;
1114
1115         case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
1116                 printk(KERN_WARNING "  REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1117                 target->status = -ECONNRESET;
1118                 break;
1119
1120         case IB_CM_REJ_CONSUMER_DEFINED:
1121                 opcode = *(u8 *) event->private_data;
1122                 if (opcode == SRP_LOGIN_REJ) {
1123                         struct srp_login_rej *rej = event->private_data;
1124                         u32 reason = be32_to_cpu(rej->reason);
1125
1126                         if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
1127                                 printk(KERN_WARNING PFX
1128                                        "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1129                         else
1130                                 printk(KERN_WARNING PFX
1131                                        "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
1132                 } else
1133                         printk(KERN_WARNING "  REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1134                                " opcode 0x%02x\n", opcode);
1135                 target->status = -ECONNRESET;
1136                 break;
1137
1138         default:
1139                 printk(KERN_WARNING "  REJ reason 0x%x\n",
1140                        event->param.rej_rcvd.reason);
1141                 target->status = -ECONNRESET;
1142         }
1143 }
1144
1145 static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1146 {
1147         struct srp_target_port *target = cm_id->context;
1148         struct ib_qp_attr *qp_attr = NULL;
1149         int attr_mask = 0;
1150         int comp = 0;
1151         int opcode = 0;
1152
1153         switch (event->event) {
1154         case IB_CM_REQ_ERROR:
1155                 printk(KERN_DEBUG PFX "Sending CM REQ failed\n");
1156                 comp = 1;
1157                 target->status = -ECONNRESET;
1158                 break;
1159
1160         case IB_CM_REP_RECEIVED:
1161                 comp = 1;
1162                 opcode = *(u8 *) event->private_data;
1163
1164                 if (opcode == SRP_LOGIN_RSP) {
1165                         struct srp_login_rsp *rsp = event->private_data;
1166
1167                         target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1168                         target->req_lim       = be32_to_cpu(rsp->req_lim_delta);
1169
1170                         target->scsi_host->can_queue = min(target->req_lim,
1171                                                            target->scsi_host->can_queue);
1172                 } else {
1173                         printk(KERN_WARNING PFX "Unhandled RSP opcode %#x\n", opcode);
1174                         target->status = -ECONNRESET;
1175                         break;
1176                 }
1177
1178                 if (!target->rx_ring[0]) {
1179                         target->status = srp_alloc_iu_bufs(target);
1180                         if (target->status)
1181                                 break;
1182                 }
1183
1184                 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1185                 if (!qp_attr) {
1186                         target->status = -ENOMEM;
1187                         break;
1188                 }
1189
1190                 qp_attr->qp_state = IB_QPS_RTR;
1191                 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1192                 if (target->status)
1193                         break;
1194
1195                 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1196                 if (target->status)
1197                         break;
1198
1199                 target->status = srp_post_recv(target);
1200                 if (target->status)
1201                         break;
1202
1203                 qp_attr->qp_state = IB_QPS_RTS;
1204                 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1205                 if (target->status)
1206                         break;
1207
1208                 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1209                 if (target->status)
1210                         break;
1211
1212                 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1213                 if (target->status)
1214                         break;
1215
1216                 break;
1217
1218         case IB_CM_REJ_RECEIVED:
1219                 printk(KERN_DEBUG PFX "REJ received\n");
1220                 comp = 1;
1221
1222                 srp_cm_rej_handler(cm_id, event, target);
1223                 break;
1224
1225         case IB_CM_DREQ_RECEIVED:
1226                 printk(KERN_WARNING PFX "DREQ received - connection closed\n");
1227                 if (ib_send_cm_drep(cm_id, NULL, 0))
1228                         printk(KERN_ERR PFX "Sending CM DREP failed\n");
1229                 break;
1230
1231         case IB_CM_TIMEWAIT_EXIT:
1232                 printk(KERN_ERR PFX "connection closed\n");
1233
1234                 comp = 1;
1235                 target->status = 0;
1236                 break;
1237
1238         case IB_CM_MRA_RECEIVED:
1239         case IB_CM_DREQ_ERROR:
1240         case IB_CM_DREP_RECEIVED:
1241                 break;
1242
1243         default:
1244                 printk(KERN_WARNING PFX "Unhandled CM event %d\n", event->event);
1245                 break;
1246         }
1247
1248         if (comp)
1249                 complete(&target->done);
1250
1251         kfree(qp_attr);
1252
1253         return 0;
1254 }
1255
1256 static int srp_send_tsk_mgmt(struct srp_target_port *target,
1257                              struct srp_request *req, u8 func)
1258 {
1259         struct srp_iu *iu;
1260         struct srp_tsk_mgmt *tsk_mgmt;
1261
1262         spin_lock_irq(target->scsi_host->host_lock);
1263
1264         if (target->state == SRP_TARGET_DEAD ||
1265             target->state == SRP_TARGET_REMOVED) {
1266                 req->scmnd->result = DID_BAD_TARGET << 16;
1267                 goto out;
1268         }
1269
1270         init_completion(&req->done);
1271
1272         iu = __srp_get_tx_iu(target);
1273         if (!iu)
1274                 goto out;
1275
1276         tsk_mgmt = iu->buf;
1277         memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1278
1279         tsk_mgmt->opcode        = SRP_TSK_MGMT;
1280         tsk_mgmt->lun           = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1281         tsk_mgmt->tag           = req->index | SRP_TAG_TSK_MGMT;
1282         tsk_mgmt->tsk_mgmt_func = func;
1283         tsk_mgmt->task_tag      = req->index;
1284
1285         if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1286                 goto out;
1287
1288         req->tsk_mgmt = iu;
1289
1290         spin_unlock_irq(target->scsi_host->host_lock);
1291
1292         if (!wait_for_completion_timeout(&req->done,
1293                                          msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
1294                 return -1;
1295
1296         return 0;
1297
1298 out:
1299         spin_unlock_irq(target->scsi_host->host_lock);
1300         return -1;
1301 }
1302
1303 static int srp_find_req(struct srp_target_port *target,
1304                         struct scsi_cmnd *scmnd,
1305                         struct srp_request **req)
1306 {
1307         if (scmnd->host_scribble == (void *) -1L)
1308                 return -1;
1309
1310         *req = &target->req_ring[(long) scmnd->host_scribble];
1311
1312         return 0;
1313 }
1314
1315 static int srp_abort(struct scsi_cmnd *scmnd)
1316 {
1317         struct srp_target_port *target = host_to_target(scmnd->device->host);
1318         struct srp_request *req;
1319         int ret = SUCCESS;
1320
1321         printk(KERN_ERR "SRP abort called\n");
1322
1323         if (target->qp_in_error)
1324                 return FAILED;
1325         if (srp_find_req(target, scmnd, &req))
1326                 return FAILED;
1327         if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1328                 return FAILED;
1329
1330         spin_lock_irq(target->scsi_host->host_lock);
1331
1332         if (req->cmd_done) {
1333                 srp_remove_req(target, req);
1334                 scmnd->scsi_done(scmnd);
1335         } else if (!req->tsk_status) {
1336                 srp_remove_req(target, req);
1337                 scmnd->result = DID_ABORT << 16;
1338         } else
1339                 ret = FAILED;
1340
1341         spin_unlock_irq(target->scsi_host->host_lock);
1342
1343         return ret;
1344 }
1345
1346 static int srp_reset_device(struct scsi_cmnd *scmnd)
1347 {
1348         struct srp_target_port *target = host_to_target(scmnd->device->host);
1349         struct srp_request *req, *tmp;
1350
1351         printk(KERN_ERR "SRP reset_device called\n");
1352
1353         if (target->qp_in_error)
1354                 return FAILED;
1355         if (srp_find_req(target, scmnd, &req))
1356                 return FAILED;
1357         if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1358                 return FAILED;
1359         if (req->tsk_status)
1360                 return FAILED;
1361
1362         spin_lock_irq(target->scsi_host->host_lock);
1363
1364         list_for_each_entry_safe(req, tmp, &target->req_queue, list)
1365                 if (req->scmnd->device == scmnd->device)
1366                         srp_reset_req(target, req);
1367
1368         spin_unlock_irq(target->scsi_host->host_lock);
1369
1370         return SUCCESS;
1371 }
1372
1373 static int srp_reset_host(struct scsi_cmnd *scmnd)
1374 {
1375         struct srp_target_port *target = host_to_target(scmnd->device->host);
1376         int ret = FAILED;
1377
1378         printk(KERN_ERR PFX "SRP reset_host called\n");
1379
1380         if (!srp_reconnect_target(target))
1381                 ret = SUCCESS;
1382
1383         return ret;
1384 }
1385
1386 static ssize_t show_id_ext(struct class_device *cdev, char *buf)
1387 {
1388         struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1389
1390         if (target->state == SRP_TARGET_DEAD ||
1391             target->state == SRP_TARGET_REMOVED)
1392                 return -ENODEV;
1393
1394         return sprintf(buf, "0x%016llx\n",
1395                        (unsigned long long) be64_to_cpu(target->id_ext));
1396 }
1397
1398 static ssize_t show_ioc_guid(struct class_device *cdev, char *buf)
1399 {
1400         struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1401
1402         if (target->state == SRP_TARGET_DEAD ||
1403             target->state == SRP_TARGET_REMOVED)
1404                 return -ENODEV;
1405
1406         return sprintf(buf, "0x%016llx\n",
1407                        (unsigned long long) be64_to_cpu(target->ioc_guid));
1408 }
1409
1410 static ssize_t show_service_id(struct class_device *cdev, char *buf)
1411 {
1412         struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1413
1414         if (target->state == SRP_TARGET_DEAD ||
1415             target->state == SRP_TARGET_REMOVED)
1416                 return -ENODEV;
1417
1418         return sprintf(buf, "0x%016llx\n",
1419                        (unsigned long long) be64_to_cpu(target->service_id));
1420 }
1421
1422 static ssize_t show_pkey(struct class_device *cdev, char *buf)
1423 {
1424         struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1425
1426         if (target->state == SRP_TARGET_DEAD ||
1427             target->state == SRP_TARGET_REMOVED)
1428                 return -ENODEV;
1429
1430         return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1431 }
1432
1433 static ssize_t show_dgid(struct class_device *cdev, char *buf)
1434 {
1435         struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1436
1437         if (target->state == SRP_TARGET_DEAD ||
1438             target->state == SRP_TARGET_REMOVED)
1439                 return -ENODEV;
1440
1441         return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1442                        be16_to_cpu(((__be16 *) target->path.dgid.raw)[0]),
1443                        be16_to_cpu(((__be16 *) target->path.dgid.raw)[1]),
1444                        be16_to_cpu(((__be16 *) target->path.dgid.raw)[2]),
1445                        be16_to_cpu(((__be16 *) target->path.dgid.raw)[3]),
1446                        be16_to_cpu(((__be16 *) target->path.dgid.raw)[4]),
1447                        be16_to_cpu(((__be16 *) target->path.dgid.raw)[5]),
1448                        be16_to_cpu(((__be16 *) target->path.dgid.raw)[6]),
1449                        be16_to_cpu(((__be16 *) target->path.dgid.raw)[7]));
1450 }
1451
1452 static ssize_t show_orig_dgid(struct class_device *cdev, char *buf)
1453 {
1454         struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1455
1456         if (target->state == SRP_TARGET_DEAD ||
1457             target->state == SRP_TARGET_REMOVED)
1458                 return -ENODEV;
1459
1460         return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1461                        be16_to_cpu(target->orig_dgid[0]),
1462                        be16_to_cpu(target->orig_dgid[1]),
1463                        be16_to_cpu(target->orig_dgid[2]),
1464                        be16_to_cpu(target->orig_dgid[3]),
1465                        be16_to_cpu(target->orig_dgid[4]),
1466                        be16_to_cpu(target->orig_dgid[5]),
1467                        be16_to_cpu(target->orig_dgid[6]),
1468                        be16_to_cpu(target->orig_dgid[7]));
1469 }
1470
1471 static ssize_t show_zero_req_lim(struct class_device *cdev, char *buf)
1472 {
1473         struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1474
1475         if (target->state == SRP_TARGET_DEAD ||
1476             target->state == SRP_TARGET_REMOVED)
1477                 return -ENODEV;
1478
1479         return sprintf(buf, "%d\n", target->zero_req_lim);
1480 }
1481
1482 static ssize_t show_local_ib_port(struct class_device *cdev, char *buf)
1483 {
1484         struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1485
1486         return sprintf(buf, "%d\n", target->srp_host->port);
1487 }
1488
1489 static ssize_t show_local_ib_device(struct class_device *cdev, char *buf)
1490 {
1491         struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1492
1493         return sprintf(buf, "%s\n", target->srp_host->dev->dev->name);
1494 }
1495
1496 static CLASS_DEVICE_ATTR(id_ext,          S_IRUGO, show_id_ext,          NULL);
1497 static CLASS_DEVICE_ATTR(ioc_guid,        S_IRUGO, show_ioc_guid,        NULL);
1498 static CLASS_DEVICE_ATTR(service_id,      S_IRUGO, show_service_id,      NULL);
1499 static CLASS_DEVICE_ATTR(pkey,            S_IRUGO, show_pkey,            NULL);
1500 static CLASS_DEVICE_ATTR(dgid,            S_IRUGO, show_dgid,            NULL);
1501 static CLASS_DEVICE_ATTR(orig_dgid,       S_IRUGO, show_orig_dgid,       NULL);
1502 static CLASS_DEVICE_ATTR(zero_req_lim,    S_IRUGO, show_zero_req_lim,    NULL);
1503 static CLASS_DEVICE_ATTR(local_ib_port,   S_IRUGO, show_local_ib_port,   NULL);
1504 static CLASS_DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
1505
1506 static struct class_device_attribute *srp_host_attrs[] = {
1507         &class_device_attr_id_ext,
1508         &class_device_attr_ioc_guid,
1509         &class_device_attr_service_id,
1510         &class_device_attr_pkey,
1511         &class_device_attr_dgid,
1512         &class_device_attr_orig_dgid,
1513         &class_device_attr_zero_req_lim,
1514         &class_device_attr_local_ib_port,
1515         &class_device_attr_local_ib_device,
1516         NULL
1517 };
1518
1519 static struct scsi_host_template srp_template = {
1520         .module                         = THIS_MODULE,
1521         .name                           = "InfiniBand SRP initiator",
1522         .proc_name                      = DRV_NAME,
1523         .info                           = srp_target_info,
1524         .queuecommand                   = srp_queuecommand,
1525         .eh_abort_handler               = srp_abort,
1526         .eh_device_reset_handler        = srp_reset_device,
1527         .eh_host_reset_handler          = srp_reset_host,
1528         .can_queue                      = SRP_SQ_SIZE,
1529         .this_id                        = -1,
1530         .cmd_per_lun                    = SRP_SQ_SIZE,
1531         .use_clustering                 = ENABLE_CLUSTERING,
1532         .shost_attrs                    = srp_host_attrs
1533 };
1534
1535 static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1536 {
1537         struct srp_rport_identifiers ids;
1538         struct srp_rport *rport;
1539
1540         sprintf(target->target_name, "SRP.T10:%016llX",
1541                  (unsigned long long) be64_to_cpu(target->id_ext));
1542
1543         if (scsi_add_host(target->scsi_host, host->dev->dev->dma_device))
1544                 return -ENODEV;
1545
1546         memcpy(ids.port_id, &target->id_ext, 8);
1547         memcpy(ids.port_id + 8, &target->ioc_guid, 8);
1548         rport = srp_rport_add(target->scsi_host, &ids);
1549         if (IS_ERR(rport)) {
1550                 scsi_remove_host(target->scsi_host);
1551                 return PTR_ERR(rport);
1552         }
1553
1554         spin_lock(&host->target_lock);
1555         list_add_tail(&target->list, &host->target_list);
1556         spin_unlock(&host->target_lock);
1557
1558         target->state = SRP_TARGET_LIVE;
1559
1560         scsi_scan_target(&target->scsi_host->shost_gendev,
1561                          0, target->scsi_id, SCAN_WILD_CARD, 0);
1562
1563         return 0;
1564 }
1565
1566 static void srp_release_class_dev(struct class_device *class_dev)
1567 {
1568         struct srp_host *host =
1569                 container_of(class_dev, struct srp_host, class_dev);
1570
1571         complete(&host->released);
1572 }
1573
1574 static struct class srp_class = {
1575         .name    = "infiniband_srp",
1576         .release = srp_release_class_dev
1577 };
1578
1579 /*
1580  * Target ports are added by writing
1581  *
1582  *     id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1583  *     pkey=<P_Key>,service_id=<service ID>
1584  *
1585  * to the add_target sysfs attribute.
1586  */
1587 enum {
1588         SRP_OPT_ERR             = 0,
1589         SRP_OPT_ID_EXT          = 1 << 0,
1590         SRP_OPT_IOC_GUID        = 1 << 1,
1591         SRP_OPT_DGID            = 1 << 2,
1592         SRP_OPT_PKEY            = 1 << 3,
1593         SRP_OPT_SERVICE_ID      = 1 << 4,
1594         SRP_OPT_MAX_SECT        = 1 << 5,
1595         SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
1596         SRP_OPT_IO_CLASS        = 1 << 7,
1597         SRP_OPT_INITIATOR_EXT   = 1 << 8,
1598         SRP_OPT_ALL             = (SRP_OPT_ID_EXT       |
1599                                    SRP_OPT_IOC_GUID     |
1600                                    SRP_OPT_DGID         |
1601                                    SRP_OPT_PKEY         |
1602                                    SRP_OPT_SERVICE_ID),
1603 };
1604
1605 static match_table_t srp_opt_tokens = {
1606         { SRP_OPT_ID_EXT,               "id_ext=%s"             },
1607         { SRP_OPT_IOC_GUID,             "ioc_guid=%s"           },
1608         { SRP_OPT_DGID,                 "dgid=%s"               },
1609         { SRP_OPT_PKEY,                 "pkey=%x"               },
1610         { SRP_OPT_SERVICE_ID,           "service_id=%s"         },
1611         { SRP_OPT_MAX_SECT,             "max_sect=%d"           },
1612         { SRP_OPT_MAX_CMD_PER_LUN,      "max_cmd_per_lun=%d"    },
1613         { SRP_OPT_IO_CLASS,             "io_class=%x"           },
1614         { SRP_OPT_INITIATOR_EXT,        "initiator_ext=%s"      },
1615         { SRP_OPT_ERR,                  NULL                    }
1616 };
1617
1618 static int srp_parse_options(const char *buf, struct srp_target_port *target)
1619 {
1620         char *options, *sep_opt;
1621         char *p;
1622         char dgid[3];
1623         substring_t args[MAX_OPT_ARGS];
1624         int opt_mask = 0;
1625         int token;
1626         int ret = -EINVAL;
1627         int i;
1628
1629         options = kstrdup(buf, GFP_KERNEL);
1630         if (!options)
1631                 return -ENOMEM;
1632
1633         sep_opt = options;
1634         while ((p = strsep(&sep_opt, ",")) != NULL) {
1635                 if (!*p)
1636                         continue;
1637
1638                 token = match_token(p, srp_opt_tokens, args);
1639                 opt_mask |= token;
1640
1641                 switch (token) {
1642                 case SRP_OPT_ID_EXT:
1643                         p = match_strdup(args);
1644                         if (!p) {
1645                                 ret = -ENOMEM;
1646                                 goto out;
1647                         }
1648                         target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1649                         kfree(p);
1650                         break;
1651
1652                 case SRP_OPT_IOC_GUID:
1653                         p = match_strdup(args);
1654                         if (!p) {
1655                                 ret = -ENOMEM;
1656                                 goto out;
1657                         }
1658                         target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1659                         kfree(p);
1660                         break;
1661
1662                 case SRP_OPT_DGID:
1663                         p = match_strdup(args);
1664                         if (!p) {
1665                                 ret = -ENOMEM;
1666                                 goto out;
1667                         }
1668                         if (strlen(p) != 32) {
1669                                 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
1670                                 kfree(p);
1671                                 goto out;
1672                         }
1673
1674                         for (i = 0; i < 16; ++i) {
1675                                 strlcpy(dgid, p + i * 2, 3);
1676                                 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1677                         }
1678                         kfree(p);
1679                         memcpy(target->orig_dgid, target->path.dgid.raw, 16);
1680                         break;
1681
1682                 case SRP_OPT_PKEY:
1683                         if (match_hex(args, &token)) {
1684                                 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1685                                 goto out;
1686                         }
1687                         target->path.pkey = cpu_to_be16(token);
1688                         break;
1689
1690                 case SRP_OPT_SERVICE_ID:
1691                         p = match_strdup(args);
1692                         if (!p) {
1693                                 ret = -ENOMEM;
1694                                 goto out;
1695                         }
1696                         target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
1697                         kfree(p);
1698                         break;
1699
1700                 case SRP_OPT_MAX_SECT:
1701                         if (match_int(args, &token)) {
1702                                 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1703                                 goto out;
1704                         }
1705                         target->scsi_host->max_sectors = token;
1706                         break;
1707
1708                 case SRP_OPT_MAX_CMD_PER_LUN:
1709                         if (match_int(args, &token)) {
1710                                 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1711                                 goto out;
1712                         }
1713                         target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1714                         break;
1715
1716                 case SRP_OPT_IO_CLASS:
1717                         if (match_hex(args, &token)) {
1718                                 printk(KERN_WARNING PFX "bad  IO class parameter '%s' \n", p);
1719                                 goto out;
1720                         }
1721                         if (token != SRP_REV10_IB_IO_CLASS &&
1722                             token != SRP_REV16A_IB_IO_CLASS) {
1723                                 printk(KERN_WARNING PFX "unknown IO class parameter value"
1724                                        " %x specified (use %x or %x).\n",
1725                                        token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1726                                 goto out;
1727                         }
1728                         target->io_class = token;
1729                         break;
1730
1731                 case SRP_OPT_INITIATOR_EXT:
1732                         p = match_strdup(args);
1733                         if (!p) {
1734                                 ret = -ENOMEM;
1735                                 goto out;
1736                         }
1737                         target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1738                         kfree(p);
1739                         break;
1740
1741                 default:
1742                         printk(KERN_WARNING PFX "unknown parameter or missing value "
1743                                "'%s' in target creation request\n", p);
1744                         goto out;
1745                 }
1746         }
1747
1748         if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1749                 ret = 0;
1750         else
1751                 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1752                         if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1753                             !(srp_opt_tokens[i].token & opt_mask))
1754                                 printk(KERN_WARNING PFX "target creation request is "
1755                                        "missing parameter '%s'\n",
1756                                        srp_opt_tokens[i].pattern);
1757
1758 out:
1759         kfree(options);
1760         return ret;
1761 }
1762
1763 static ssize_t srp_create_target(struct class_device *class_dev,
1764                                  const char *buf, size_t count)
1765 {
1766         struct srp_host *host =
1767                 container_of(class_dev, struct srp_host, class_dev);
1768         struct Scsi_Host *target_host;
1769         struct srp_target_port *target;
1770         int ret;
1771         int i;
1772
1773         target_host = scsi_host_alloc(&srp_template,
1774                                       sizeof (struct srp_target_port));
1775         if (!target_host)
1776                 return -ENOMEM;
1777
1778         target_host->transportt = ib_srp_transport_template;
1779         target_host->max_lun     = SRP_MAX_LUN;
1780         target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
1781
1782         target = host_to_target(target_host);
1783
1784         target->io_class   = SRP_REV16A_IB_IO_CLASS;
1785         target->scsi_host  = target_host;
1786         target->srp_host   = host;
1787
1788         INIT_LIST_HEAD(&target->free_reqs);
1789         INIT_LIST_HEAD(&target->req_queue);
1790         for (i = 0; i < SRP_SQ_SIZE; ++i) {
1791                 target->req_ring[i].index = i;
1792                 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1793         }
1794
1795         ret = srp_parse_options(buf, target);
1796         if (ret)
1797                 goto err;
1798
1799         ib_get_cached_gid(host->dev->dev, host->port, 0, &target->path.sgid);
1800
1801         printk(KERN_DEBUG PFX "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1802                "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1803                (unsigned long long) be64_to_cpu(target->id_ext),
1804                (unsigned long long) be64_to_cpu(target->ioc_guid),
1805                be16_to_cpu(target->path.pkey),
1806                (unsigned long long) be64_to_cpu(target->service_id),
1807                (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]),
1808                (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]),
1809                (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]),
1810                (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]),
1811                (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]),
1812                (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]),
1813                (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]),
1814                (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14]));
1815
1816         ret = srp_create_target_ib(target);
1817         if (ret)
1818                 goto err;
1819
1820         target->cm_id = ib_create_cm_id(host->dev->dev, srp_cm_handler, target);
1821         if (IS_ERR(target->cm_id)) {
1822                 ret = PTR_ERR(target->cm_id);
1823                 goto err_free;
1824         }
1825
1826         target->qp_in_error = 0;
1827         ret = srp_connect_target(target);
1828         if (ret) {
1829                 printk(KERN_ERR PFX "Connection failed\n");
1830                 goto err_cm_id;
1831         }
1832
1833         ret = srp_add_target(host, target);
1834         if (ret)
1835                 goto err_disconnect;
1836
1837         return count;
1838
1839 err_disconnect:
1840         srp_disconnect_target(target);
1841
1842 err_cm_id:
1843         ib_destroy_cm_id(target->cm_id);
1844
1845 err_free:
1846         srp_free_target_ib(target);
1847
1848 err:
1849         scsi_host_put(target_host);
1850
1851         return ret;
1852 }
1853
1854 static CLASS_DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
1855
1856 static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
1857 {
1858         struct srp_host *host =
1859                 container_of(class_dev, struct srp_host, class_dev);
1860
1861         return sprintf(buf, "%s\n", host->dev->dev->name);
1862 }
1863
1864 static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1865
1866 static ssize_t show_port(struct class_device *class_dev, char *buf)
1867 {
1868         struct srp_host *host =
1869                 container_of(class_dev, struct srp_host, class_dev);
1870
1871         return sprintf(buf, "%d\n", host->port);
1872 }
1873
1874 static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
1875
1876 static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
1877 {
1878         struct srp_host *host;
1879
1880         host = kzalloc(sizeof *host, GFP_KERNEL);
1881         if (!host)
1882                 return NULL;
1883
1884         INIT_LIST_HEAD(&host->target_list);
1885         spin_lock_init(&host->target_lock);
1886         init_completion(&host->released);
1887         host->dev  = device;
1888         host->port = port;
1889
1890         host->class_dev.class = &srp_class;
1891         host->class_dev.dev   = device->dev->dma_device;
1892         snprintf(host->class_dev.class_id, BUS_ID_SIZE, "srp-%s-%d",
1893                  device->dev->name, port);
1894
1895         if (class_device_register(&host->class_dev))
1896                 goto free_host;
1897         if (class_device_create_file(&host->class_dev, &class_device_attr_add_target))
1898                 goto err_class;
1899         if (class_device_create_file(&host->class_dev, &class_device_attr_ibdev))
1900                 goto err_class;
1901         if (class_device_create_file(&host->class_dev, &class_device_attr_port))
1902                 goto err_class;
1903
1904         return host;
1905
1906 err_class:
1907         class_device_unregister(&host->class_dev);
1908
1909 free_host:
1910         kfree(host);
1911
1912         return NULL;
1913 }
1914
1915 static void srp_add_one(struct ib_device *device)
1916 {
1917         struct srp_device *srp_dev;
1918         struct ib_device_attr *dev_attr;
1919         struct ib_fmr_pool_param fmr_param;
1920         struct srp_host *host;
1921         int s, e, p;
1922
1923         dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
1924         if (!dev_attr)
1925                 return;
1926
1927         if (ib_query_device(device, dev_attr)) {
1928                 printk(KERN_WARNING PFX "Query device failed for %s\n",
1929                        device->name);
1930                 goto free_attr;
1931         }
1932
1933         srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
1934         if (!srp_dev)
1935                 goto free_attr;
1936
1937         /*
1938          * Use the smallest page size supported by the HCA, down to a
1939          * minimum of 512 bytes (which is the smallest sector that a
1940          * SCSI command will ever carry).
1941          */
1942         srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
1943         srp_dev->fmr_page_size  = 1 << srp_dev->fmr_page_shift;
1944         srp_dev->fmr_page_mask  = ~((u64) srp_dev->fmr_page_size - 1);
1945
1946         INIT_LIST_HEAD(&srp_dev->dev_list);
1947
1948         srp_dev->dev = device;
1949         srp_dev->pd  = ib_alloc_pd(device);
1950         if (IS_ERR(srp_dev->pd))
1951                 goto free_dev;
1952
1953         srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
1954                                     IB_ACCESS_LOCAL_WRITE |
1955                                     IB_ACCESS_REMOTE_READ |
1956                                     IB_ACCESS_REMOTE_WRITE);
1957         if (IS_ERR(srp_dev->mr))
1958                 goto err_pd;
1959
1960         memset(&fmr_param, 0, sizeof fmr_param);
1961         fmr_param.pool_size         = SRP_FMR_POOL_SIZE;
1962         fmr_param.dirty_watermark   = SRP_FMR_DIRTY_SIZE;
1963         fmr_param.cache             = 1;
1964         fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
1965         fmr_param.page_shift        = srp_dev->fmr_page_shift;
1966         fmr_param.access            = (IB_ACCESS_LOCAL_WRITE |
1967                                        IB_ACCESS_REMOTE_WRITE |
1968                                        IB_ACCESS_REMOTE_READ);
1969
1970         srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
1971         if (IS_ERR(srp_dev->fmr_pool))
1972                 srp_dev->fmr_pool = NULL;
1973
1974         if (device->node_type == RDMA_NODE_IB_SWITCH) {
1975                 s = 0;
1976                 e = 0;
1977         } else {
1978                 s = 1;
1979                 e = device->phys_port_cnt;
1980         }
1981
1982         for (p = s; p <= e; ++p) {
1983                 host = srp_add_port(srp_dev, p);
1984                 if (host)
1985                         list_add_tail(&host->list, &srp_dev->dev_list);
1986         }
1987
1988         ib_set_client_data(device, &srp_client, srp_dev);
1989
1990         goto free_attr;
1991
1992 err_pd:
1993         ib_dealloc_pd(srp_dev->pd);
1994
1995 free_dev:
1996         kfree(srp_dev);
1997
1998 free_attr:
1999         kfree(dev_attr);
2000 }
2001
2002 static void srp_remove_one(struct ib_device *device)
2003 {
2004         struct srp_device *srp_dev;
2005         struct srp_host *host, *tmp_host;
2006         LIST_HEAD(target_list);
2007         struct srp_target_port *target, *tmp_target;
2008
2009         srp_dev = ib_get_client_data(device, &srp_client);
2010
2011         list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
2012                 class_device_unregister(&host->class_dev);
2013                 /*
2014                  * Wait for the sysfs entry to go away, so that no new
2015                  * target ports can be created.
2016                  */
2017                 wait_for_completion(&host->released);
2018
2019                 /*
2020                  * Mark all target ports as removed, so we stop queueing
2021                  * commands and don't try to reconnect.
2022                  */
2023                 spin_lock(&host->target_lock);
2024                 list_for_each_entry(target, &host->target_list, list) {
2025                         spin_lock_irq(target->scsi_host->host_lock);
2026                         target->state = SRP_TARGET_REMOVED;
2027                         spin_unlock_irq(target->scsi_host->host_lock);
2028                 }
2029                 spin_unlock(&host->target_lock);
2030
2031                 /*
2032                  * Wait for any reconnection tasks that may have
2033                  * started before we marked our target ports as
2034                  * removed, and any target port removal tasks.
2035                  */
2036                 flush_scheduled_work();
2037
2038                 list_for_each_entry_safe(target, tmp_target,
2039                                          &host->target_list, list) {
2040                         scsi_remove_host(target->scsi_host);
2041                         srp_disconnect_target(target);
2042                         ib_destroy_cm_id(target->cm_id);
2043                         srp_free_target_ib(target);
2044                         scsi_host_put(target->scsi_host);
2045                 }
2046
2047                 kfree(host);
2048         }
2049
2050         if (srp_dev->fmr_pool)
2051                 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2052         ib_dereg_mr(srp_dev->mr);
2053         ib_dealloc_pd(srp_dev->pd);
2054
2055         kfree(srp_dev);
2056 }
2057
2058 static struct srp_function_template ib_srp_transport_functions = {
2059 };
2060
2061 static int __init srp_init_module(void)
2062 {
2063         int ret;
2064
2065         ib_srp_transport_template =
2066                 srp_attach_transport(&ib_srp_transport_functions);
2067         if (!ib_srp_transport_template)
2068                 return -ENOMEM;
2069
2070         srp_template.sg_tablesize = srp_sg_tablesize;
2071         srp_max_iu_len = (sizeof (struct srp_cmd) +
2072                           sizeof (struct srp_indirect_buf) +
2073                           srp_sg_tablesize * 16);
2074
2075         ret = class_register(&srp_class);
2076         if (ret) {
2077                 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
2078                 srp_release_transport(ib_srp_transport_template);
2079                 return ret;
2080         }
2081
2082         ib_sa_register_client(&srp_sa_client);
2083
2084         ret = ib_register_client(&srp_client);
2085         if (ret) {
2086                 printk(KERN_ERR PFX "couldn't register IB client\n");
2087                 srp_release_transport(ib_srp_transport_template);
2088                 ib_sa_unregister_client(&srp_sa_client);
2089                 class_unregister(&srp_class);
2090                 return ret;
2091         }
2092
2093         return 0;
2094 }
2095
2096 static void __exit srp_cleanup_module(void)
2097 {
2098         ib_unregister_client(&srp_client);
2099         ib_sa_unregister_client(&srp_sa_client);
2100         class_unregister(&srp_class);
2101         srp_release_transport(ib_srp_transport_template);
2102 }
2103
2104 module_init(srp_init_module);
2105 module_exit(srp_cleanup_module);