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