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