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