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