nvme-fc and nvmet-fc: revise LLDD api for LS reception and LS request
[linux-2.6-microblaze.git] / drivers / nvme / host / fc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2016 Avago Technologies.  All rights reserved.
4  */
5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6 #include <linux/module.h>
7 #include <linux/parser.h>
8 #include <uapi/scsi/fc/fc_fs.h>
9 #include <uapi/scsi/fc/fc_els.h>
10 #include <linux/delay.h>
11 #include <linux/overflow.h>
12
13 #include "nvme.h"
14 #include "fabrics.h"
15 #include <linux/nvme-fc-driver.h>
16 #include <linux/nvme-fc.h>
17 #include <scsi/scsi_transport_fc.h>
18
19 /* *************************** Data Structures/Defines ****************** */
20
21
22 enum nvme_fc_queue_flags {
23         NVME_FC_Q_CONNECTED = 0,
24         NVME_FC_Q_LIVE,
25 };
26
27 #define NVME_FC_DEFAULT_DEV_LOSS_TMO    60      /* seconds */
28
29 struct nvme_fc_queue {
30         struct nvme_fc_ctrl     *ctrl;
31         struct device           *dev;
32         struct blk_mq_hw_ctx    *hctx;
33         void                    *lldd_handle;
34         size_t                  cmnd_capsule_len;
35         u32                     qnum;
36         u32                     rqcnt;
37         u32                     seqno;
38
39         u64                     connection_id;
40         atomic_t                csn;
41
42         unsigned long           flags;
43 } __aligned(sizeof(u64));       /* alignment for other things alloc'd with */
44
45 enum nvme_fcop_flags {
46         FCOP_FLAGS_TERMIO       = (1 << 0),
47         FCOP_FLAGS_AEN          = (1 << 1),
48 };
49
50 struct nvmefc_ls_req_op {
51         struct nvmefc_ls_req    ls_req;
52
53         struct nvme_fc_rport    *rport;
54         struct nvme_fc_queue    *queue;
55         struct request          *rq;
56         u32                     flags;
57
58         int                     ls_error;
59         struct completion       ls_done;
60         struct list_head        lsreq_list;     /* rport->ls_req_list */
61         bool                    req_queued;
62 };
63
64 enum nvme_fcpop_state {
65         FCPOP_STATE_UNINIT      = 0,
66         FCPOP_STATE_IDLE        = 1,
67         FCPOP_STATE_ACTIVE      = 2,
68         FCPOP_STATE_ABORTED     = 3,
69         FCPOP_STATE_COMPLETE    = 4,
70 };
71
72 struct nvme_fc_fcp_op {
73         struct nvme_request     nreq;           /*
74                                                  * nvme/host/core.c
75                                                  * requires this to be
76                                                  * the 1st element in the
77                                                  * private structure
78                                                  * associated with the
79                                                  * request.
80                                                  */
81         struct nvmefc_fcp_req   fcp_req;
82
83         struct nvme_fc_ctrl     *ctrl;
84         struct nvme_fc_queue    *queue;
85         struct request          *rq;
86
87         atomic_t                state;
88         u32                     flags;
89         u32                     rqno;
90         u32                     nents;
91
92         struct nvme_fc_cmd_iu   cmd_iu;
93         struct nvme_fc_ersp_iu  rsp_iu;
94 };
95
96 struct nvme_fcp_op_w_sgl {
97         struct nvme_fc_fcp_op   op;
98         struct scatterlist      sgl[NVME_INLINE_SG_CNT];
99         uint8_t                 priv[0];
100 };
101
102 struct nvme_fc_lport {
103         struct nvme_fc_local_port       localport;
104
105         struct ida                      endp_cnt;
106         struct list_head                port_list;      /* nvme_fc_port_list */
107         struct list_head                endp_list;
108         struct device                   *dev;   /* physical device for dma */
109         struct nvme_fc_port_template    *ops;
110         struct kref                     ref;
111         atomic_t                        act_rport_cnt;
112 } __aligned(sizeof(u64));       /* alignment for other things alloc'd with */
113
114 struct nvme_fc_rport {
115         struct nvme_fc_remote_port      remoteport;
116
117         struct list_head                endp_list; /* for lport->endp_list */
118         struct list_head                ctrl_list;
119         struct list_head                ls_req_list;
120         struct list_head                disc_list;
121         struct device                   *dev;   /* physical device for dma */
122         struct nvme_fc_lport            *lport;
123         spinlock_t                      lock;
124         struct kref                     ref;
125         atomic_t                        act_ctrl_cnt;
126         unsigned long                   dev_loss_end;
127 } __aligned(sizeof(u64));       /* alignment for other things alloc'd with */
128
129 enum nvme_fcctrl_flags {
130         FCCTRL_TERMIO           = (1 << 0),
131 };
132
133 struct nvme_fc_ctrl {
134         spinlock_t              lock;
135         struct nvme_fc_queue    *queues;
136         struct device           *dev;
137         struct nvme_fc_lport    *lport;
138         struct nvme_fc_rport    *rport;
139         u32                     cnum;
140
141         bool                    ioq_live;
142         bool                    assoc_active;
143         atomic_t                err_work_active;
144         u64                     association_id;
145
146         struct list_head        ctrl_list;      /* rport->ctrl_list */
147
148         struct blk_mq_tag_set   admin_tag_set;
149         struct blk_mq_tag_set   tag_set;
150
151         struct delayed_work     connect_work;
152         struct work_struct      err_work;
153
154         struct kref             ref;
155         u32                     flags;
156         u32                     iocnt;
157         wait_queue_head_t       ioabort_wait;
158
159         struct nvme_fc_fcp_op   aen_ops[NVME_NR_AEN_COMMANDS];
160
161         struct nvme_ctrl        ctrl;
162 };
163
164 static inline struct nvme_fc_ctrl *
165 to_fc_ctrl(struct nvme_ctrl *ctrl)
166 {
167         return container_of(ctrl, struct nvme_fc_ctrl, ctrl);
168 }
169
170 static inline struct nvme_fc_lport *
171 localport_to_lport(struct nvme_fc_local_port *portptr)
172 {
173         return container_of(portptr, struct nvme_fc_lport, localport);
174 }
175
176 static inline struct nvme_fc_rport *
177 remoteport_to_rport(struct nvme_fc_remote_port *portptr)
178 {
179         return container_of(portptr, struct nvme_fc_rport, remoteport);
180 }
181
182 static inline struct nvmefc_ls_req_op *
183 ls_req_to_lsop(struct nvmefc_ls_req *lsreq)
184 {
185         return container_of(lsreq, struct nvmefc_ls_req_op, ls_req);
186 }
187
188 static inline struct nvme_fc_fcp_op *
189 fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq)
190 {
191         return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req);
192 }
193
194
195
196 /* *************************** Globals **************************** */
197
198
199 static DEFINE_SPINLOCK(nvme_fc_lock);
200
201 static LIST_HEAD(nvme_fc_lport_list);
202 static DEFINE_IDA(nvme_fc_local_port_cnt);
203 static DEFINE_IDA(nvme_fc_ctrl_cnt);
204
205 static struct workqueue_struct *nvme_fc_wq;
206
207 static bool nvme_fc_waiting_to_unload;
208 static DECLARE_COMPLETION(nvme_fc_unload_proceed);
209
210 /*
211  * These items are short-term. They will eventually be moved into
212  * a generic FC class. See comments in module init.
213  */
214 static struct device *fc_udev_device;
215
216
217 /* *********************** FC-NVME Port Management ************************ */
218
219 static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *,
220                         struct nvme_fc_queue *, unsigned int);
221
222 static void
223 nvme_fc_free_lport(struct kref *ref)
224 {
225         struct nvme_fc_lport *lport =
226                 container_of(ref, struct nvme_fc_lport, ref);
227         unsigned long flags;
228
229         WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED);
230         WARN_ON(!list_empty(&lport->endp_list));
231
232         /* remove from transport list */
233         spin_lock_irqsave(&nvme_fc_lock, flags);
234         list_del(&lport->port_list);
235         if (nvme_fc_waiting_to_unload && list_empty(&nvme_fc_lport_list))
236                 complete(&nvme_fc_unload_proceed);
237         spin_unlock_irqrestore(&nvme_fc_lock, flags);
238
239         ida_simple_remove(&nvme_fc_local_port_cnt, lport->localport.port_num);
240         ida_destroy(&lport->endp_cnt);
241
242         put_device(lport->dev);
243
244         kfree(lport);
245 }
246
247 static void
248 nvme_fc_lport_put(struct nvme_fc_lport *lport)
249 {
250         kref_put(&lport->ref, nvme_fc_free_lport);
251 }
252
253 static int
254 nvme_fc_lport_get(struct nvme_fc_lport *lport)
255 {
256         return kref_get_unless_zero(&lport->ref);
257 }
258
259
260 static struct nvme_fc_lport *
261 nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info *pinfo,
262                         struct nvme_fc_port_template *ops,
263                         struct device *dev)
264 {
265         struct nvme_fc_lport *lport;
266         unsigned long flags;
267
268         spin_lock_irqsave(&nvme_fc_lock, flags);
269
270         list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
271                 if (lport->localport.node_name != pinfo->node_name ||
272                     lport->localport.port_name != pinfo->port_name)
273                         continue;
274
275                 if (lport->dev != dev) {
276                         lport = ERR_PTR(-EXDEV);
277                         goto out_done;
278                 }
279
280                 if (lport->localport.port_state != FC_OBJSTATE_DELETED) {
281                         lport = ERR_PTR(-EEXIST);
282                         goto out_done;
283                 }
284
285                 if (!nvme_fc_lport_get(lport)) {
286                         /*
287                          * fails if ref cnt already 0. If so,
288                          * act as if lport already deleted
289                          */
290                         lport = NULL;
291                         goto out_done;
292                 }
293
294                 /* resume the lport */
295
296                 lport->ops = ops;
297                 lport->localport.port_role = pinfo->port_role;
298                 lport->localport.port_id = pinfo->port_id;
299                 lport->localport.port_state = FC_OBJSTATE_ONLINE;
300
301                 spin_unlock_irqrestore(&nvme_fc_lock, flags);
302
303                 return lport;
304         }
305
306         lport = NULL;
307
308 out_done:
309         spin_unlock_irqrestore(&nvme_fc_lock, flags);
310
311         return lport;
312 }
313
314 /**
315  * nvme_fc_register_localport - transport entry point called by an
316  *                              LLDD to register the existence of a NVME
317  *                              host FC port.
318  * @pinfo:     pointer to information about the port to be registered
319  * @template:  LLDD entrypoints and operational parameters for the port
320  * @dev:       physical hardware device node port corresponds to. Will be
321  *             used for DMA mappings
322  * @portptr:   pointer to a local port pointer. Upon success, the routine
323  *             will allocate a nvme_fc_local_port structure and place its
324  *             address in the local port pointer. Upon failure, local port
325  *             pointer will be set to 0.
326  *
327  * Returns:
328  * a completion status. Must be 0 upon success; a negative errno
329  * (ex: -ENXIO) upon failure.
330  */
331 int
332 nvme_fc_register_localport(struct nvme_fc_port_info *pinfo,
333                         struct nvme_fc_port_template *template,
334                         struct device *dev,
335                         struct nvme_fc_local_port **portptr)
336 {
337         struct nvme_fc_lport *newrec;
338         unsigned long flags;
339         int ret, idx;
340
341         if (!template->localport_delete || !template->remoteport_delete ||
342             !template->ls_req || !template->fcp_io ||
343             !template->ls_abort || !template->fcp_abort ||
344             !template->max_hw_queues || !template->max_sgl_segments ||
345             !template->max_dif_sgl_segments || !template->dma_boundary) {
346                 ret = -EINVAL;
347                 goto out_reghost_failed;
348         }
349
350         /*
351          * look to see if there is already a localport that had been
352          * deregistered and in the process of waiting for all the
353          * references to fully be removed.  If the references haven't
354          * expired, we can simply re-enable the localport. Remoteports
355          * and controller reconnections should resume naturally.
356          */
357         newrec = nvme_fc_attach_to_unreg_lport(pinfo, template, dev);
358
359         /* found an lport, but something about its state is bad */
360         if (IS_ERR(newrec)) {
361                 ret = PTR_ERR(newrec);
362                 goto out_reghost_failed;
363
364         /* found existing lport, which was resumed */
365         } else if (newrec) {
366                 *portptr = &newrec->localport;
367                 return 0;
368         }
369
370         /* nothing found - allocate a new localport struct */
371
372         newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz),
373                          GFP_KERNEL);
374         if (!newrec) {
375                 ret = -ENOMEM;
376                 goto out_reghost_failed;
377         }
378
379         idx = ida_simple_get(&nvme_fc_local_port_cnt, 0, 0, GFP_KERNEL);
380         if (idx < 0) {
381                 ret = -ENOSPC;
382                 goto out_fail_kfree;
383         }
384
385         if (!get_device(dev) && dev) {
386                 ret = -ENODEV;
387                 goto out_ida_put;
388         }
389
390         INIT_LIST_HEAD(&newrec->port_list);
391         INIT_LIST_HEAD(&newrec->endp_list);
392         kref_init(&newrec->ref);
393         atomic_set(&newrec->act_rport_cnt, 0);
394         newrec->ops = template;
395         newrec->dev = dev;
396         ida_init(&newrec->endp_cnt);
397         newrec->localport.private = &newrec[1];
398         newrec->localport.node_name = pinfo->node_name;
399         newrec->localport.port_name = pinfo->port_name;
400         newrec->localport.port_role = pinfo->port_role;
401         newrec->localport.port_id = pinfo->port_id;
402         newrec->localport.port_state = FC_OBJSTATE_ONLINE;
403         newrec->localport.port_num = idx;
404
405         spin_lock_irqsave(&nvme_fc_lock, flags);
406         list_add_tail(&newrec->port_list, &nvme_fc_lport_list);
407         spin_unlock_irqrestore(&nvme_fc_lock, flags);
408
409         if (dev)
410                 dma_set_seg_boundary(dev, template->dma_boundary);
411
412         *portptr = &newrec->localport;
413         return 0;
414
415 out_ida_put:
416         ida_simple_remove(&nvme_fc_local_port_cnt, idx);
417 out_fail_kfree:
418         kfree(newrec);
419 out_reghost_failed:
420         *portptr = NULL;
421
422         return ret;
423 }
424 EXPORT_SYMBOL_GPL(nvme_fc_register_localport);
425
426 /**
427  * nvme_fc_unregister_localport - transport entry point called by an
428  *                              LLDD to deregister/remove a previously
429  *                              registered a NVME host FC port.
430  * @portptr: pointer to the (registered) local port that is to be deregistered.
431  *
432  * Returns:
433  * a completion status. Must be 0 upon success; a negative errno
434  * (ex: -ENXIO) upon failure.
435  */
436 int
437 nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr)
438 {
439         struct nvme_fc_lport *lport = localport_to_lport(portptr);
440         unsigned long flags;
441
442         if (!portptr)
443                 return -EINVAL;
444
445         spin_lock_irqsave(&nvme_fc_lock, flags);
446
447         if (portptr->port_state != FC_OBJSTATE_ONLINE) {
448                 spin_unlock_irqrestore(&nvme_fc_lock, flags);
449                 return -EINVAL;
450         }
451         portptr->port_state = FC_OBJSTATE_DELETED;
452
453         spin_unlock_irqrestore(&nvme_fc_lock, flags);
454
455         if (atomic_read(&lport->act_rport_cnt) == 0)
456                 lport->ops->localport_delete(&lport->localport);
457
458         nvme_fc_lport_put(lport);
459
460         return 0;
461 }
462 EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport);
463
464 /*
465  * TRADDR strings, per FC-NVME are fixed format:
466  *   "nn-0x<16hexdigits>:pn-0x<16hexdigits>" - 43 characters
467  * udev event will only differ by prefix of what field is
468  * being specified:
469  *    "NVMEFC_HOST_TRADDR=" or "NVMEFC_TRADDR=" - 19 max characters
470  *  19 + 43 + null_fudge = 64 characters
471  */
472 #define FCNVME_TRADDR_LENGTH            64
473
474 static void
475 nvme_fc_signal_discovery_scan(struct nvme_fc_lport *lport,
476                 struct nvme_fc_rport *rport)
477 {
478         char hostaddr[FCNVME_TRADDR_LENGTH];    /* NVMEFC_HOST_TRADDR=...*/
479         char tgtaddr[FCNVME_TRADDR_LENGTH];     /* NVMEFC_TRADDR=...*/
480         char *envp[4] = { "FC_EVENT=nvmediscovery", hostaddr, tgtaddr, NULL };
481
482         if (!(rport->remoteport.port_role & FC_PORT_ROLE_NVME_DISCOVERY))
483                 return;
484
485         snprintf(hostaddr, sizeof(hostaddr),
486                 "NVMEFC_HOST_TRADDR=nn-0x%016llx:pn-0x%016llx",
487                 lport->localport.node_name, lport->localport.port_name);
488         snprintf(tgtaddr, sizeof(tgtaddr),
489                 "NVMEFC_TRADDR=nn-0x%016llx:pn-0x%016llx",
490                 rport->remoteport.node_name, rport->remoteport.port_name);
491         kobject_uevent_env(&fc_udev_device->kobj, KOBJ_CHANGE, envp);
492 }
493
494 static void
495 nvme_fc_free_rport(struct kref *ref)
496 {
497         struct nvme_fc_rport *rport =
498                 container_of(ref, struct nvme_fc_rport, ref);
499         struct nvme_fc_lport *lport =
500                         localport_to_lport(rport->remoteport.localport);
501         unsigned long flags;
502
503         WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED);
504         WARN_ON(!list_empty(&rport->ctrl_list));
505
506         /* remove from lport list */
507         spin_lock_irqsave(&nvme_fc_lock, flags);
508         list_del(&rport->endp_list);
509         spin_unlock_irqrestore(&nvme_fc_lock, flags);
510
511         WARN_ON(!list_empty(&rport->disc_list));
512         ida_simple_remove(&lport->endp_cnt, rport->remoteport.port_num);
513
514         kfree(rport);
515
516         nvme_fc_lport_put(lport);
517 }
518
519 static void
520 nvme_fc_rport_put(struct nvme_fc_rport *rport)
521 {
522         kref_put(&rport->ref, nvme_fc_free_rport);
523 }
524
525 static int
526 nvme_fc_rport_get(struct nvme_fc_rport *rport)
527 {
528         return kref_get_unless_zero(&rport->ref);
529 }
530
531 static void
532 nvme_fc_resume_controller(struct nvme_fc_ctrl *ctrl)
533 {
534         switch (ctrl->ctrl.state) {
535         case NVME_CTRL_NEW:
536         case NVME_CTRL_CONNECTING:
537                 /*
538                  * As all reconnects were suppressed, schedule a
539                  * connect.
540                  */
541                 dev_info(ctrl->ctrl.device,
542                         "NVME-FC{%d}: connectivity re-established. "
543                         "Attempting reconnect\n", ctrl->cnum);
544
545                 queue_delayed_work(nvme_wq, &ctrl->connect_work, 0);
546                 break;
547
548         case NVME_CTRL_RESETTING:
549                 /*
550                  * Controller is already in the process of terminating the
551                  * association. No need to do anything further. The reconnect
552                  * step will naturally occur after the reset completes.
553                  */
554                 break;
555
556         default:
557                 /* no action to take - let it delete */
558                 break;
559         }
560 }
561
562 static struct nvme_fc_rport *
563 nvme_fc_attach_to_suspended_rport(struct nvme_fc_lport *lport,
564                                 struct nvme_fc_port_info *pinfo)
565 {
566         struct nvme_fc_rport *rport;
567         struct nvme_fc_ctrl *ctrl;
568         unsigned long flags;
569
570         spin_lock_irqsave(&nvme_fc_lock, flags);
571
572         list_for_each_entry(rport, &lport->endp_list, endp_list) {
573                 if (rport->remoteport.node_name != pinfo->node_name ||
574                     rport->remoteport.port_name != pinfo->port_name)
575                         continue;
576
577                 if (!nvme_fc_rport_get(rport)) {
578                         rport = ERR_PTR(-ENOLCK);
579                         goto out_done;
580                 }
581
582                 spin_unlock_irqrestore(&nvme_fc_lock, flags);
583
584                 spin_lock_irqsave(&rport->lock, flags);
585
586                 /* has it been unregistered */
587                 if (rport->remoteport.port_state != FC_OBJSTATE_DELETED) {
588                         /* means lldd called us twice */
589                         spin_unlock_irqrestore(&rport->lock, flags);
590                         nvme_fc_rport_put(rport);
591                         return ERR_PTR(-ESTALE);
592                 }
593
594                 rport->remoteport.port_role = pinfo->port_role;
595                 rport->remoteport.port_id = pinfo->port_id;
596                 rport->remoteport.port_state = FC_OBJSTATE_ONLINE;
597                 rport->dev_loss_end = 0;
598
599                 /*
600                  * kick off a reconnect attempt on all associations to the
601                  * remote port. A successful reconnects will resume i/o.
602                  */
603                 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list)
604                         nvme_fc_resume_controller(ctrl);
605
606                 spin_unlock_irqrestore(&rport->lock, flags);
607
608                 return rport;
609         }
610
611         rport = NULL;
612
613 out_done:
614         spin_unlock_irqrestore(&nvme_fc_lock, flags);
615
616         return rport;
617 }
618
619 static inline void
620 __nvme_fc_set_dev_loss_tmo(struct nvme_fc_rport *rport,
621                         struct nvme_fc_port_info *pinfo)
622 {
623         if (pinfo->dev_loss_tmo)
624                 rport->remoteport.dev_loss_tmo = pinfo->dev_loss_tmo;
625         else
626                 rport->remoteport.dev_loss_tmo = NVME_FC_DEFAULT_DEV_LOSS_TMO;
627 }
628
629 /**
630  * nvme_fc_register_remoteport - transport entry point called by an
631  *                              LLDD to register the existence of a NVME
632  *                              subsystem FC port on its fabric.
633  * @localport: pointer to the (registered) local port that the remote
634  *             subsystem port is connected to.
635  * @pinfo:     pointer to information about the port to be registered
636  * @portptr:   pointer to a remote port pointer. Upon success, the routine
637  *             will allocate a nvme_fc_remote_port structure and place its
638  *             address in the remote port pointer. Upon failure, remote port
639  *             pointer will be set to 0.
640  *
641  * Returns:
642  * a completion status. Must be 0 upon success; a negative errno
643  * (ex: -ENXIO) upon failure.
644  */
645 int
646 nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
647                                 struct nvme_fc_port_info *pinfo,
648                                 struct nvme_fc_remote_port **portptr)
649 {
650         struct nvme_fc_lport *lport = localport_to_lport(localport);
651         struct nvme_fc_rport *newrec;
652         unsigned long flags;
653         int ret, idx;
654
655         if (!nvme_fc_lport_get(lport)) {
656                 ret = -ESHUTDOWN;
657                 goto out_reghost_failed;
658         }
659
660         /*
661          * look to see if there is already a remoteport that is waiting
662          * for a reconnect (within dev_loss_tmo) with the same WWN's.
663          * If so, transition to it and reconnect.
664          */
665         newrec = nvme_fc_attach_to_suspended_rport(lport, pinfo);
666
667         /* found an rport, but something about its state is bad */
668         if (IS_ERR(newrec)) {
669                 ret = PTR_ERR(newrec);
670                 goto out_lport_put;
671
672         /* found existing rport, which was resumed */
673         } else if (newrec) {
674                 nvme_fc_lport_put(lport);
675                 __nvme_fc_set_dev_loss_tmo(newrec, pinfo);
676                 nvme_fc_signal_discovery_scan(lport, newrec);
677                 *portptr = &newrec->remoteport;
678                 return 0;
679         }
680
681         /* nothing found - allocate a new remoteport struct */
682
683         newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz),
684                          GFP_KERNEL);
685         if (!newrec) {
686                 ret = -ENOMEM;
687                 goto out_lport_put;
688         }
689
690         idx = ida_simple_get(&lport->endp_cnt, 0, 0, GFP_KERNEL);
691         if (idx < 0) {
692                 ret = -ENOSPC;
693                 goto out_kfree_rport;
694         }
695
696         INIT_LIST_HEAD(&newrec->endp_list);
697         INIT_LIST_HEAD(&newrec->ctrl_list);
698         INIT_LIST_HEAD(&newrec->ls_req_list);
699         INIT_LIST_HEAD(&newrec->disc_list);
700         kref_init(&newrec->ref);
701         atomic_set(&newrec->act_ctrl_cnt, 0);
702         spin_lock_init(&newrec->lock);
703         newrec->remoteport.localport = &lport->localport;
704         newrec->dev = lport->dev;
705         newrec->lport = lport;
706         newrec->remoteport.private = &newrec[1];
707         newrec->remoteport.port_role = pinfo->port_role;
708         newrec->remoteport.node_name = pinfo->node_name;
709         newrec->remoteport.port_name = pinfo->port_name;
710         newrec->remoteport.port_id = pinfo->port_id;
711         newrec->remoteport.port_state = FC_OBJSTATE_ONLINE;
712         newrec->remoteport.port_num = idx;
713         __nvme_fc_set_dev_loss_tmo(newrec, pinfo);
714
715         spin_lock_irqsave(&nvme_fc_lock, flags);
716         list_add_tail(&newrec->endp_list, &lport->endp_list);
717         spin_unlock_irqrestore(&nvme_fc_lock, flags);
718
719         nvme_fc_signal_discovery_scan(lport, newrec);
720
721         *portptr = &newrec->remoteport;
722         return 0;
723
724 out_kfree_rport:
725         kfree(newrec);
726 out_lport_put:
727         nvme_fc_lport_put(lport);
728 out_reghost_failed:
729         *portptr = NULL;
730         return ret;
731 }
732 EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport);
733
734 static int
735 nvme_fc_abort_lsops(struct nvme_fc_rport *rport)
736 {
737         struct nvmefc_ls_req_op *lsop;
738         unsigned long flags;
739
740 restart:
741         spin_lock_irqsave(&rport->lock, flags);
742
743         list_for_each_entry(lsop, &rport->ls_req_list, lsreq_list) {
744                 if (!(lsop->flags & FCOP_FLAGS_TERMIO)) {
745                         lsop->flags |= FCOP_FLAGS_TERMIO;
746                         spin_unlock_irqrestore(&rport->lock, flags);
747                         rport->lport->ops->ls_abort(&rport->lport->localport,
748                                                 &rport->remoteport,
749                                                 &lsop->ls_req);
750                         goto restart;
751                 }
752         }
753         spin_unlock_irqrestore(&rport->lock, flags);
754
755         return 0;
756 }
757
758 static void
759 nvme_fc_ctrl_connectivity_loss(struct nvme_fc_ctrl *ctrl)
760 {
761         dev_info(ctrl->ctrl.device,
762                 "NVME-FC{%d}: controller connectivity lost. Awaiting "
763                 "Reconnect", ctrl->cnum);
764
765         switch (ctrl->ctrl.state) {
766         case NVME_CTRL_NEW:
767         case NVME_CTRL_LIVE:
768                 /*
769                  * Schedule a controller reset. The reset will terminate the
770                  * association and schedule the reconnect timer.  Reconnects
771                  * will be attempted until either the ctlr_loss_tmo
772                  * (max_retries * connect_delay) expires or the remoteport's
773                  * dev_loss_tmo expires.
774                  */
775                 if (nvme_reset_ctrl(&ctrl->ctrl)) {
776                         dev_warn(ctrl->ctrl.device,
777                                 "NVME-FC{%d}: Couldn't schedule reset.\n",
778                                 ctrl->cnum);
779                         nvme_delete_ctrl(&ctrl->ctrl);
780                 }
781                 break;
782
783         case NVME_CTRL_CONNECTING:
784                 /*
785                  * The association has already been terminated and the
786                  * controller is attempting reconnects.  No need to do anything
787                  * futher.  Reconnects will be attempted until either the
788                  * ctlr_loss_tmo (max_retries * connect_delay) expires or the
789                  * remoteport's dev_loss_tmo expires.
790                  */
791                 break;
792
793         case NVME_CTRL_RESETTING:
794                 /*
795                  * Controller is already in the process of terminating the
796                  * association.  No need to do anything further. The reconnect
797                  * step will kick in naturally after the association is
798                  * terminated.
799                  */
800                 break;
801
802         case NVME_CTRL_DELETING:
803         default:
804                 /* no action to take - let it delete */
805                 break;
806         }
807 }
808
809 /**
810  * nvme_fc_unregister_remoteport - transport entry point called by an
811  *                              LLDD to deregister/remove a previously
812  *                              registered a NVME subsystem FC port.
813  * @portptr: pointer to the (registered) remote port that is to be
814  *           deregistered.
815  *
816  * Returns:
817  * a completion status. Must be 0 upon success; a negative errno
818  * (ex: -ENXIO) upon failure.
819  */
820 int
821 nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr)
822 {
823         struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
824         struct nvme_fc_ctrl *ctrl;
825         unsigned long flags;
826
827         if (!portptr)
828                 return -EINVAL;
829
830         spin_lock_irqsave(&rport->lock, flags);
831
832         if (portptr->port_state != FC_OBJSTATE_ONLINE) {
833                 spin_unlock_irqrestore(&rport->lock, flags);
834                 return -EINVAL;
835         }
836         portptr->port_state = FC_OBJSTATE_DELETED;
837
838         rport->dev_loss_end = jiffies + (portptr->dev_loss_tmo * HZ);
839
840         list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
841                 /* if dev_loss_tmo==0, dev loss is immediate */
842                 if (!portptr->dev_loss_tmo) {
843                         dev_warn(ctrl->ctrl.device,
844                                 "NVME-FC{%d}: controller connectivity lost.\n",
845                                 ctrl->cnum);
846                         nvme_delete_ctrl(&ctrl->ctrl);
847                 } else
848                         nvme_fc_ctrl_connectivity_loss(ctrl);
849         }
850
851         spin_unlock_irqrestore(&rport->lock, flags);
852
853         nvme_fc_abort_lsops(rport);
854
855         if (atomic_read(&rport->act_ctrl_cnt) == 0)
856                 rport->lport->ops->remoteport_delete(portptr);
857
858         /*
859          * release the reference, which will allow, if all controllers
860          * go away, which should only occur after dev_loss_tmo occurs,
861          * for the rport to be torn down.
862          */
863         nvme_fc_rport_put(rport);
864
865         return 0;
866 }
867 EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport);
868
869 /**
870  * nvme_fc_rescan_remoteport - transport entry point called by an
871  *                              LLDD to request a nvme device rescan.
872  * @remoteport: pointer to the (registered) remote port that is to be
873  *              rescanned.
874  *
875  * Returns: N/A
876  */
877 void
878 nvme_fc_rescan_remoteport(struct nvme_fc_remote_port *remoteport)
879 {
880         struct nvme_fc_rport *rport = remoteport_to_rport(remoteport);
881
882         nvme_fc_signal_discovery_scan(rport->lport, rport);
883 }
884 EXPORT_SYMBOL_GPL(nvme_fc_rescan_remoteport);
885
886 int
887 nvme_fc_set_remoteport_devloss(struct nvme_fc_remote_port *portptr,
888                         u32 dev_loss_tmo)
889 {
890         struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
891         unsigned long flags;
892
893         spin_lock_irqsave(&rport->lock, flags);
894
895         if (portptr->port_state != FC_OBJSTATE_ONLINE) {
896                 spin_unlock_irqrestore(&rport->lock, flags);
897                 return -EINVAL;
898         }
899
900         /* a dev_loss_tmo of 0 (immediate) is allowed to be set */
901         rport->remoteport.dev_loss_tmo = dev_loss_tmo;
902
903         spin_unlock_irqrestore(&rport->lock, flags);
904
905         return 0;
906 }
907 EXPORT_SYMBOL_GPL(nvme_fc_set_remoteport_devloss);
908
909
910 /* *********************** FC-NVME DMA Handling **************************** */
911
912 /*
913  * The fcloop device passes in a NULL device pointer. Real LLD's will
914  * pass in a valid device pointer. If NULL is passed to the dma mapping
915  * routines, depending on the platform, it may or may not succeed, and
916  * may crash.
917  *
918  * As such:
919  * Wrapper all the dma routines and check the dev pointer.
920  *
921  * If simple mappings (return just a dma address, we'll noop them,
922  * returning a dma address of 0.
923  *
924  * On more complex mappings (dma_map_sg), a pseudo routine fills
925  * in the scatter list, setting all dma addresses to 0.
926  */
927
928 static inline dma_addr_t
929 fc_dma_map_single(struct device *dev, void *ptr, size_t size,
930                 enum dma_data_direction dir)
931 {
932         return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
933 }
934
935 static inline int
936 fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
937 {
938         return dev ? dma_mapping_error(dev, dma_addr) : 0;
939 }
940
941 static inline void
942 fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
943         enum dma_data_direction dir)
944 {
945         if (dev)
946                 dma_unmap_single(dev, addr, size, dir);
947 }
948
949 static inline void
950 fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
951                 enum dma_data_direction dir)
952 {
953         if (dev)
954                 dma_sync_single_for_cpu(dev, addr, size, dir);
955 }
956
957 static inline void
958 fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
959                 enum dma_data_direction dir)
960 {
961         if (dev)
962                 dma_sync_single_for_device(dev, addr, size, dir);
963 }
964
965 /* pseudo dma_map_sg call */
966 static int
967 fc_map_sg(struct scatterlist *sg, int nents)
968 {
969         struct scatterlist *s;
970         int i;
971
972         WARN_ON(nents == 0 || sg[0].length == 0);
973
974         for_each_sg(sg, s, nents, i) {
975                 s->dma_address = 0L;
976 #ifdef CONFIG_NEED_SG_DMA_LENGTH
977                 s->dma_length = s->length;
978 #endif
979         }
980         return nents;
981 }
982
983 static inline int
984 fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
985                 enum dma_data_direction dir)
986 {
987         return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
988 }
989
990 static inline void
991 fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
992                 enum dma_data_direction dir)
993 {
994         if (dev)
995                 dma_unmap_sg(dev, sg, nents, dir);
996 }
997
998 /* *********************** FC-NVME LS Handling **************************** */
999
1000 static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
1001 static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
1002
1003
1004 static void
1005 __nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop)
1006 {
1007         struct nvme_fc_rport *rport = lsop->rport;
1008         struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1009         unsigned long flags;
1010
1011         spin_lock_irqsave(&rport->lock, flags);
1012
1013         if (!lsop->req_queued) {
1014                 spin_unlock_irqrestore(&rport->lock, flags);
1015                 return;
1016         }
1017
1018         list_del(&lsop->lsreq_list);
1019
1020         lsop->req_queued = false;
1021
1022         spin_unlock_irqrestore(&rport->lock, flags);
1023
1024         fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1025                                   (lsreq->rqstlen + lsreq->rsplen),
1026                                   DMA_BIDIRECTIONAL);
1027
1028         nvme_fc_rport_put(rport);
1029 }
1030
1031 static int
1032 __nvme_fc_send_ls_req(struct nvme_fc_rport *rport,
1033                 struct nvmefc_ls_req_op *lsop,
1034                 void (*done)(struct nvmefc_ls_req *req, int status))
1035 {
1036         struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1037         unsigned long flags;
1038         int ret = 0;
1039
1040         if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
1041                 return -ECONNREFUSED;
1042
1043         if (!nvme_fc_rport_get(rport))
1044                 return -ESHUTDOWN;
1045
1046         lsreq->done = done;
1047         lsop->rport = rport;
1048         lsop->req_queued = false;
1049         INIT_LIST_HEAD(&lsop->lsreq_list);
1050         init_completion(&lsop->ls_done);
1051
1052         lsreq->rqstdma = fc_dma_map_single(rport->dev, lsreq->rqstaddr,
1053                                   lsreq->rqstlen + lsreq->rsplen,
1054                                   DMA_BIDIRECTIONAL);
1055         if (fc_dma_mapping_error(rport->dev, lsreq->rqstdma)) {
1056                 ret = -EFAULT;
1057                 goto out_putrport;
1058         }
1059         lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
1060
1061         spin_lock_irqsave(&rport->lock, flags);
1062
1063         list_add_tail(&lsop->lsreq_list, &rport->ls_req_list);
1064
1065         lsop->req_queued = true;
1066
1067         spin_unlock_irqrestore(&rport->lock, flags);
1068
1069         ret = rport->lport->ops->ls_req(&rport->lport->localport,
1070                                         &rport->remoteport, lsreq);
1071         if (ret)
1072                 goto out_unlink;
1073
1074         return 0;
1075
1076 out_unlink:
1077         lsop->ls_error = ret;
1078         spin_lock_irqsave(&rport->lock, flags);
1079         lsop->req_queued = false;
1080         list_del(&lsop->lsreq_list);
1081         spin_unlock_irqrestore(&rport->lock, flags);
1082         fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1083                                   (lsreq->rqstlen + lsreq->rsplen),
1084                                   DMA_BIDIRECTIONAL);
1085 out_putrport:
1086         nvme_fc_rport_put(rport);
1087
1088         return ret;
1089 }
1090
1091 static void
1092 nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status)
1093 {
1094         struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1095
1096         lsop->ls_error = status;
1097         complete(&lsop->ls_done);
1098 }
1099
1100 static int
1101 nvme_fc_send_ls_req(struct nvme_fc_rport *rport, struct nvmefc_ls_req_op *lsop)
1102 {
1103         struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1104         struct fcnvme_ls_rjt *rjt = lsreq->rspaddr;
1105         int ret;
1106
1107         ret = __nvme_fc_send_ls_req(rport, lsop, nvme_fc_send_ls_req_done);
1108
1109         if (!ret) {
1110                 /*
1111                  * No timeout/not interruptible as we need the struct
1112                  * to exist until the lldd calls us back. Thus mandate
1113                  * wait until driver calls back. lldd responsible for
1114                  * the timeout action
1115                  */
1116                 wait_for_completion(&lsop->ls_done);
1117
1118                 __nvme_fc_finish_ls_req(lsop);
1119
1120                 ret = lsop->ls_error;
1121         }
1122
1123         if (ret)
1124                 return ret;
1125
1126         /* ACC or RJT payload ? */
1127         if (rjt->w0.ls_cmd == FCNVME_LS_RJT)
1128                 return -ENXIO;
1129
1130         return 0;
1131 }
1132
1133 static int
1134 nvme_fc_send_ls_req_async(struct nvme_fc_rport *rport,
1135                 struct nvmefc_ls_req_op *lsop,
1136                 void (*done)(struct nvmefc_ls_req *req, int status))
1137 {
1138         /* don't wait for completion */
1139
1140         return __nvme_fc_send_ls_req(rport, lsop, done);
1141 }
1142
1143 /* Validation Error indexes into the string table below */
1144 enum {
1145         VERR_NO_ERROR           = 0,
1146         VERR_LSACC              = 1,
1147         VERR_LSDESC_RQST        = 2,
1148         VERR_LSDESC_RQST_LEN    = 3,
1149         VERR_ASSOC_ID           = 4,
1150         VERR_ASSOC_ID_LEN       = 5,
1151         VERR_CONN_ID            = 6,
1152         VERR_CONN_ID_LEN        = 7,
1153         VERR_CR_ASSOC           = 8,
1154         VERR_CR_ASSOC_ACC_LEN   = 9,
1155         VERR_CR_CONN            = 10,
1156         VERR_CR_CONN_ACC_LEN    = 11,
1157         VERR_DISCONN            = 12,
1158         VERR_DISCONN_ACC_LEN    = 13,
1159 };
1160
1161 static char *validation_errors[] = {
1162         "OK",
1163         "Not LS_ACC",
1164         "Not LSDESC_RQST",
1165         "Bad LSDESC_RQST Length",
1166         "Not Association ID",
1167         "Bad Association ID Length",
1168         "Not Connection ID",
1169         "Bad Connection ID Length",
1170         "Not CR_ASSOC Rqst",
1171         "Bad CR_ASSOC ACC Length",
1172         "Not CR_CONN Rqst",
1173         "Bad CR_CONN ACC Length",
1174         "Not Disconnect Rqst",
1175         "Bad Disconnect ACC Length",
1176 };
1177
1178 static int
1179 nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl,
1180         struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio)
1181 {
1182         struct nvmefc_ls_req_op *lsop;
1183         struct nvmefc_ls_req *lsreq;
1184         struct fcnvme_ls_cr_assoc_rqst *assoc_rqst;
1185         struct fcnvme_ls_cr_assoc_acc *assoc_acc;
1186         int ret, fcret = 0;
1187
1188         lsop = kzalloc((sizeof(*lsop) +
1189                          ctrl->lport->ops->lsrqst_priv_sz +
1190                          sizeof(*assoc_rqst) + sizeof(*assoc_acc)), GFP_KERNEL);
1191         if (!lsop) {
1192                 ret = -ENOMEM;
1193                 goto out_no_memory;
1194         }
1195         lsreq = &lsop->ls_req;
1196
1197         lsreq->private = (void *)&lsop[1];
1198         assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)
1199                         (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1200         assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1];
1201
1202         assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION;
1203         assoc_rqst->desc_list_len =
1204                         cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1205
1206         assoc_rqst->assoc_cmd.desc_tag =
1207                         cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD);
1208         assoc_rqst->assoc_cmd.desc_len =
1209                         fcnvme_lsdesc_len(
1210                                 sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1211
1212         assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1213         assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize - 1);
1214         /* Linux supports only Dynamic controllers */
1215         assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff);
1216         uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id);
1217         strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
1218                 min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE));
1219         strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
1220                 min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE));
1221
1222         lsop->queue = queue;
1223         lsreq->rqstaddr = assoc_rqst;
1224         lsreq->rqstlen = sizeof(*assoc_rqst);
1225         lsreq->rspaddr = assoc_acc;
1226         lsreq->rsplen = sizeof(*assoc_acc);
1227         lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1228
1229         ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1230         if (ret)
1231                 goto out_free_buffer;
1232
1233         /* process connect LS completion */
1234
1235         /* validate the ACC response */
1236         if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1237                 fcret = VERR_LSACC;
1238         else if (assoc_acc->hdr.desc_list_len !=
1239                         fcnvme_lsdesc_len(
1240                                 sizeof(struct fcnvme_ls_cr_assoc_acc)))
1241                 fcret = VERR_CR_ASSOC_ACC_LEN;
1242         else if (assoc_acc->hdr.rqst.desc_tag !=
1243                         cpu_to_be32(FCNVME_LSDESC_RQST))
1244                 fcret = VERR_LSDESC_RQST;
1245         else if (assoc_acc->hdr.rqst.desc_len !=
1246                         fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1247                 fcret = VERR_LSDESC_RQST_LEN;
1248         else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION)
1249                 fcret = VERR_CR_ASSOC;
1250         else if (assoc_acc->associd.desc_tag !=
1251                         cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
1252                 fcret = VERR_ASSOC_ID;
1253         else if (assoc_acc->associd.desc_len !=
1254                         fcnvme_lsdesc_len(
1255                                 sizeof(struct fcnvme_lsdesc_assoc_id)))
1256                 fcret = VERR_ASSOC_ID_LEN;
1257         else if (assoc_acc->connectid.desc_tag !=
1258                         cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1259                 fcret = VERR_CONN_ID;
1260         else if (assoc_acc->connectid.desc_len !=
1261                         fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1262                 fcret = VERR_CONN_ID_LEN;
1263
1264         if (fcret) {
1265                 ret = -EBADF;
1266                 dev_err(ctrl->dev,
1267                         "q %d Create Association LS failed: %s\n",
1268                         queue->qnum, validation_errors[fcret]);
1269         } else {
1270                 ctrl->association_id =
1271                         be64_to_cpu(assoc_acc->associd.association_id);
1272                 queue->connection_id =
1273                         be64_to_cpu(assoc_acc->connectid.connection_id);
1274                 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1275         }
1276
1277 out_free_buffer:
1278         kfree(lsop);
1279 out_no_memory:
1280         if (ret)
1281                 dev_err(ctrl->dev,
1282                         "queue %d connect admin queue failed (%d).\n",
1283                         queue->qnum, ret);
1284         return ret;
1285 }
1286
1287 static int
1288 nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
1289                         u16 qsize, u16 ersp_ratio)
1290 {
1291         struct nvmefc_ls_req_op *lsop;
1292         struct nvmefc_ls_req *lsreq;
1293         struct fcnvme_ls_cr_conn_rqst *conn_rqst;
1294         struct fcnvme_ls_cr_conn_acc *conn_acc;
1295         int ret, fcret = 0;
1296
1297         lsop = kzalloc((sizeof(*lsop) +
1298                          ctrl->lport->ops->lsrqst_priv_sz +
1299                          sizeof(*conn_rqst) + sizeof(*conn_acc)), GFP_KERNEL);
1300         if (!lsop) {
1301                 ret = -ENOMEM;
1302                 goto out_no_memory;
1303         }
1304         lsreq = &lsop->ls_req;
1305
1306         lsreq->private = (void *)&lsop[1];
1307         conn_rqst = (struct fcnvme_ls_cr_conn_rqst *)
1308                         (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1309         conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1];
1310
1311         conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION;
1312         conn_rqst->desc_list_len = cpu_to_be32(
1313                                 sizeof(struct fcnvme_lsdesc_assoc_id) +
1314                                 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1315
1316         conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1317         conn_rqst->associd.desc_len =
1318                         fcnvme_lsdesc_len(
1319                                 sizeof(struct fcnvme_lsdesc_assoc_id));
1320         conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1321         conn_rqst->connect_cmd.desc_tag =
1322                         cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD);
1323         conn_rqst->connect_cmd.desc_len =
1324                         fcnvme_lsdesc_len(
1325                                 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1326         conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1327         conn_rqst->connect_cmd.qid  = cpu_to_be16(queue->qnum);
1328         conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize - 1);
1329
1330         lsop->queue = queue;
1331         lsreq->rqstaddr = conn_rqst;
1332         lsreq->rqstlen = sizeof(*conn_rqst);
1333         lsreq->rspaddr = conn_acc;
1334         lsreq->rsplen = sizeof(*conn_acc);
1335         lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1336
1337         ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1338         if (ret)
1339                 goto out_free_buffer;
1340
1341         /* process connect LS completion */
1342
1343         /* validate the ACC response */
1344         if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1345                 fcret = VERR_LSACC;
1346         else if (conn_acc->hdr.desc_list_len !=
1347                         fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)))
1348                 fcret = VERR_CR_CONN_ACC_LEN;
1349         else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST))
1350                 fcret = VERR_LSDESC_RQST;
1351         else if (conn_acc->hdr.rqst.desc_len !=
1352                         fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1353                 fcret = VERR_LSDESC_RQST_LEN;
1354         else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION)
1355                 fcret = VERR_CR_CONN;
1356         else if (conn_acc->connectid.desc_tag !=
1357                         cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1358                 fcret = VERR_CONN_ID;
1359         else if (conn_acc->connectid.desc_len !=
1360                         fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1361                 fcret = VERR_CONN_ID_LEN;
1362
1363         if (fcret) {
1364                 ret = -EBADF;
1365                 dev_err(ctrl->dev,
1366                         "q %d Create I/O Connection LS failed: %s\n",
1367                         queue->qnum, validation_errors[fcret]);
1368         } else {
1369                 queue->connection_id =
1370                         be64_to_cpu(conn_acc->connectid.connection_id);
1371                 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1372         }
1373
1374 out_free_buffer:
1375         kfree(lsop);
1376 out_no_memory:
1377         if (ret)
1378                 dev_err(ctrl->dev,
1379                         "queue %d connect I/O queue failed (%d).\n",
1380                         queue->qnum, ret);
1381         return ret;
1382 }
1383
1384 static void
1385 nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
1386 {
1387         struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1388
1389         __nvme_fc_finish_ls_req(lsop);
1390
1391         /* fc-nvme initiator doesn't care about success or failure of cmd */
1392
1393         kfree(lsop);
1394 }
1395
1396 /*
1397  * This routine sends a FC-NVME LS to disconnect (aka terminate)
1398  * the FC-NVME Association.  Terminating the association also
1399  * terminates the FC-NVME connections (per queue, both admin and io
1400  * queues) that are part of the association. E.g. things are torn
1401  * down, and the related FC-NVME Association ID and Connection IDs
1402  * become invalid.
1403  *
1404  * The behavior of the fc-nvme initiator is such that it's
1405  * understanding of the association and connections will implicitly
1406  * be torn down. The action is implicit as it may be due to a loss of
1407  * connectivity with the fc-nvme target, so you may never get a
1408  * response even if you tried.  As such, the action of this routine
1409  * is to asynchronously send the LS, ignore any results of the LS, and
1410  * continue on with terminating the association. If the fc-nvme target
1411  * is present and receives the LS, it too can tear down.
1412  */
1413 static void
1414 nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl)
1415 {
1416         struct fcnvme_ls_disconnect_assoc_rqst *discon_rqst;
1417         struct fcnvme_ls_disconnect_assoc_acc *discon_acc;
1418         struct nvmefc_ls_req_op *lsop;
1419         struct nvmefc_ls_req *lsreq;
1420         int ret;
1421
1422         lsop = kzalloc((sizeof(*lsop) +
1423                          ctrl->lport->ops->lsrqst_priv_sz +
1424                          sizeof(*discon_rqst) + sizeof(*discon_acc)),
1425                         GFP_KERNEL);
1426         if (!lsop)
1427                 /* couldn't sent it... too bad */
1428                 return;
1429
1430         lsreq = &lsop->ls_req;
1431
1432         lsreq->private = (void *)&lsop[1];
1433         discon_rqst = (struct fcnvme_ls_disconnect_assoc_rqst *)
1434                         (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1435         discon_acc = (struct fcnvme_ls_disconnect_assoc_acc *)&discon_rqst[1];
1436
1437         discon_rqst->w0.ls_cmd = FCNVME_LS_DISCONNECT_ASSOC;
1438         discon_rqst->desc_list_len = cpu_to_be32(
1439                                 sizeof(struct fcnvme_lsdesc_assoc_id) +
1440                                 sizeof(struct fcnvme_lsdesc_disconn_cmd));
1441
1442         discon_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1443         discon_rqst->associd.desc_len =
1444                         fcnvme_lsdesc_len(
1445                                 sizeof(struct fcnvme_lsdesc_assoc_id));
1446
1447         discon_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1448
1449         discon_rqst->discon_cmd.desc_tag = cpu_to_be32(
1450                                                 FCNVME_LSDESC_DISCONN_CMD);
1451         discon_rqst->discon_cmd.desc_len =
1452                         fcnvme_lsdesc_len(
1453                                 sizeof(struct fcnvme_lsdesc_disconn_cmd));
1454
1455         lsreq->rqstaddr = discon_rqst;
1456         lsreq->rqstlen = sizeof(*discon_rqst);
1457         lsreq->rspaddr = discon_acc;
1458         lsreq->rsplen = sizeof(*discon_acc);
1459         lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1460
1461         ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop,
1462                                 nvme_fc_disconnect_assoc_done);
1463         if (ret)
1464                 kfree(lsop);
1465 }
1466
1467 /**
1468  * nvme_fc_rcv_ls_req - transport entry point called by an LLDD
1469  *                       upon the reception of a NVME LS request.
1470  *
1471  * The nvme-fc layer will copy payload to an internal structure for
1472  * processing.  As such, upon completion of the routine, the LLDD may
1473  * immediately free/reuse the LS request buffer passed in the call.
1474  *
1475  * If this routine returns error, the LLDD should abort the exchange.
1476  *
1477  * @remoteport: pointer to the (registered) remote port that the LS
1478  *              was received from. The remoteport is associated with
1479  *              a specific localport.
1480  * @lsrsp:      pointer to a nvmefc_ls_rsp response structure to be
1481  *              used to reference the exchange corresponding to the LS
1482  *              when issuing an ls response.
1483  * @lsreqbuf:   pointer to the buffer containing the LS Request
1484  * @lsreqbuf_len: length, in bytes, of the received LS request
1485  */
1486 int
1487 nvme_fc_rcv_ls_req(struct nvme_fc_remote_port *portptr,
1488                         struct nvmefc_ls_rsp *lsrsp,
1489                         void *lsreqbuf, u32 lsreqbuf_len)
1490 {
1491         struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
1492         struct nvme_fc_lport *lport = rport->lport;
1493
1494         /* validate there's a routine to transmit a response */
1495         if (!lport->ops->xmt_ls_rsp)
1496                 return(-EINVAL);
1497
1498         return 0;
1499 }
1500 EXPORT_SYMBOL_GPL(nvme_fc_rcv_ls_req);
1501
1502
1503 /* *********************** NVME Ctrl Routines **************************** */
1504
1505 static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg);
1506
1507 static void
1508 __nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl,
1509                 struct nvme_fc_fcp_op *op)
1510 {
1511         fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma,
1512                                 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1513         fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
1514                                 sizeof(op->cmd_iu), DMA_TO_DEVICE);
1515
1516         atomic_set(&op->state, FCPOP_STATE_UNINIT);
1517 }
1518
1519 static void
1520 nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1521                 unsigned int hctx_idx)
1522 {
1523         struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1524
1525         return __nvme_fc_exit_request(set->driver_data, op);
1526 }
1527
1528 static int
1529 __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
1530 {
1531         unsigned long flags;
1532         int opstate;
1533
1534         spin_lock_irqsave(&ctrl->lock, flags);
1535         opstate = atomic_xchg(&op->state, FCPOP_STATE_ABORTED);
1536         if (opstate != FCPOP_STATE_ACTIVE)
1537                 atomic_set(&op->state, opstate);
1538         else if (ctrl->flags & FCCTRL_TERMIO)
1539                 ctrl->iocnt++;
1540         spin_unlock_irqrestore(&ctrl->lock, flags);
1541
1542         if (opstate != FCPOP_STATE_ACTIVE)
1543                 return -ECANCELED;
1544
1545         ctrl->lport->ops->fcp_abort(&ctrl->lport->localport,
1546                                         &ctrl->rport->remoteport,
1547                                         op->queue->lldd_handle,
1548                                         &op->fcp_req);
1549
1550         return 0;
1551 }
1552
1553 static void
1554 nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl)
1555 {
1556         struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops;
1557         int i;
1558
1559         /* ensure we've initialized the ops once */
1560         if (!(aen_op->flags & FCOP_FLAGS_AEN))
1561                 return;
1562
1563         for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++)
1564                 __nvme_fc_abort_op(ctrl, aen_op);
1565 }
1566
1567 static inline void
1568 __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
1569                 struct nvme_fc_fcp_op *op, int opstate)
1570 {
1571         unsigned long flags;
1572
1573         if (opstate == FCPOP_STATE_ABORTED) {
1574                 spin_lock_irqsave(&ctrl->lock, flags);
1575                 if (ctrl->flags & FCCTRL_TERMIO) {
1576                         if (!--ctrl->iocnt)
1577                                 wake_up(&ctrl->ioabort_wait);
1578                 }
1579                 spin_unlock_irqrestore(&ctrl->lock, flags);
1580         }
1581 }
1582
1583 static void
1584 nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
1585 {
1586         struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1587         struct request *rq = op->rq;
1588         struct nvmefc_fcp_req *freq = &op->fcp_req;
1589         struct nvme_fc_ctrl *ctrl = op->ctrl;
1590         struct nvme_fc_queue *queue = op->queue;
1591         struct nvme_completion *cqe = &op->rsp_iu.cqe;
1592         struct nvme_command *sqe = &op->cmd_iu.sqe;
1593         __le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1);
1594         union nvme_result result;
1595         bool terminate_assoc = true;
1596         int opstate;
1597
1598         /*
1599          * WARNING:
1600          * The current linux implementation of a nvme controller
1601          * allocates a single tag set for all io queues and sizes
1602          * the io queues to fully hold all possible tags. Thus, the
1603          * implementation does not reference or care about the sqhd
1604          * value as it never needs to use the sqhd/sqtail pointers
1605          * for submission pacing.
1606          *
1607          * This affects the FC-NVME implementation in two ways:
1608          * 1) As the value doesn't matter, we don't need to waste
1609          *    cycles extracting it from ERSPs and stamping it in the
1610          *    cases where the transport fabricates CQEs on successful
1611          *    completions.
1612          * 2) The FC-NVME implementation requires that delivery of
1613          *    ERSP completions are to go back to the nvme layer in order
1614          *    relative to the rsn, such that the sqhd value will always
1615          *    be "in order" for the nvme layer. As the nvme layer in
1616          *    linux doesn't care about sqhd, there's no need to return
1617          *    them in order.
1618          *
1619          * Additionally:
1620          * As the core nvme layer in linux currently does not look at
1621          * every field in the cqe - in cases where the FC transport must
1622          * fabricate a CQE, the following fields will not be set as they
1623          * are not referenced:
1624          *      cqe.sqid,  cqe.sqhd,  cqe.command_id
1625          *
1626          * Failure or error of an individual i/o, in a transport
1627          * detected fashion unrelated to the nvme completion status,
1628          * potentially cause the initiator and target sides to get out
1629          * of sync on SQ head/tail (aka outstanding io count allowed).
1630          * Per FC-NVME spec, failure of an individual command requires
1631          * the connection to be terminated, which in turn requires the
1632          * association to be terminated.
1633          */
1634
1635         opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
1636
1637         fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
1638                                 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1639
1640         if (opstate == FCPOP_STATE_ABORTED)
1641                 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
1642         else if (freq->status) {
1643                 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
1644                 dev_info(ctrl->ctrl.device,
1645                         "NVME-FC{%d}: io failed due to lldd error %d\n",
1646                         ctrl->cnum, freq->status);
1647         }
1648
1649         /*
1650          * For the linux implementation, if we have an unsuccesful
1651          * status, they blk-mq layer can typically be called with the
1652          * non-zero status and the content of the cqe isn't important.
1653          */
1654         if (status)
1655                 goto done;
1656
1657         /*
1658          * command completed successfully relative to the wire
1659          * protocol. However, validate anything received and
1660          * extract the status and result from the cqe (create it
1661          * where necessary).
1662          */
1663
1664         switch (freq->rcv_rsplen) {
1665
1666         case 0:
1667         case NVME_FC_SIZEOF_ZEROS_RSP:
1668                 /*
1669                  * No response payload or 12 bytes of payload (which
1670                  * should all be zeros) are considered successful and
1671                  * no payload in the CQE by the transport.
1672                  */
1673                 if (freq->transferred_length !=
1674                     be32_to_cpu(op->cmd_iu.data_len)) {
1675                         status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
1676                         dev_info(ctrl->ctrl.device,
1677                                 "NVME-FC{%d}: io failed due to bad transfer "
1678                                 "length: %d vs expected %d\n",
1679                                 ctrl->cnum, freq->transferred_length,
1680                                 be32_to_cpu(op->cmd_iu.data_len));
1681                         goto done;
1682                 }
1683                 result.u64 = 0;
1684                 break;
1685
1686         case sizeof(struct nvme_fc_ersp_iu):
1687                 /*
1688                  * The ERSP IU contains a full completion with CQE.
1689                  * Validate ERSP IU and look at cqe.
1690                  */
1691                 if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) !=
1692                                         (freq->rcv_rsplen / 4) ||
1693                              be32_to_cpu(op->rsp_iu.xfrd_len) !=
1694                                         freq->transferred_length ||
1695                              op->rsp_iu.ersp_result ||
1696                              sqe->common.command_id != cqe->command_id)) {
1697                         status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
1698                         dev_info(ctrl->ctrl.device,
1699                                 "NVME-FC{%d}: io failed due to bad NVMe_ERSP: "
1700                                 "iu len %d, xfr len %d vs %d, status code "
1701                                 "%d, cmdid %d vs %d\n",
1702                                 ctrl->cnum, be16_to_cpu(op->rsp_iu.iu_len),
1703                                 be32_to_cpu(op->rsp_iu.xfrd_len),
1704                                 freq->transferred_length,
1705                                 op->rsp_iu.ersp_result,
1706                                 sqe->common.command_id,
1707                                 cqe->command_id);
1708                         goto done;
1709                 }
1710                 result = cqe->result;
1711                 status = cqe->status;
1712                 break;
1713
1714         default:
1715                 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
1716                 dev_info(ctrl->ctrl.device,
1717                         "NVME-FC{%d}: io failed due to odd NVMe_xRSP iu "
1718                         "len %d\n",
1719                         ctrl->cnum, freq->rcv_rsplen);
1720                 goto done;
1721         }
1722
1723         terminate_assoc = false;
1724
1725 done:
1726         if (op->flags & FCOP_FLAGS_AEN) {
1727                 nvme_complete_async_event(&queue->ctrl->ctrl, status, &result);
1728                 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
1729                 atomic_set(&op->state, FCPOP_STATE_IDLE);
1730                 op->flags = FCOP_FLAGS_AEN;     /* clear other flags */
1731                 nvme_fc_ctrl_put(ctrl);
1732                 goto check_error;
1733         }
1734
1735         __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
1736         nvme_end_request(rq, status, result);
1737
1738 check_error:
1739         if (terminate_assoc)
1740                 nvme_fc_error_recovery(ctrl, "transport detected io error");
1741 }
1742
1743 static int
1744 __nvme_fc_init_request(struct nvme_fc_ctrl *ctrl,
1745                 struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op,
1746                 struct request *rq, u32 rqno)
1747 {
1748         struct nvme_fcp_op_w_sgl *op_w_sgl =
1749                 container_of(op, typeof(*op_w_sgl), op);
1750         struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1751         int ret = 0;
1752
1753         memset(op, 0, sizeof(*op));
1754         op->fcp_req.cmdaddr = &op->cmd_iu;
1755         op->fcp_req.cmdlen = sizeof(op->cmd_iu);
1756         op->fcp_req.rspaddr = &op->rsp_iu;
1757         op->fcp_req.rsplen = sizeof(op->rsp_iu);
1758         op->fcp_req.done = nvme_fc_fcpio_done;
1759         op->ctrl = ctrl;
1760         op->queue = queue;
1761         op->rq = rq;
1762         op->rqno = rqno;
1763
1764         cmdiu->format_id = NVME_CMD_FORMAT_ID;
1765         cmdiu->fc_id = NVME_CMD_FC_ID;
1766         cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
1767         if (queue->qnum)
1768                 cmdiu->rsv_cat = fccmnd_set_cat_css(0,
1769                                         (NVME_CC_CSS_NVM >> NVME_CC_CSS_SHIFT));
1770         else
1771                 cmdiu->rsv_cat = fccmnd_set_cat_admin(0);
1772
1773         op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev,
1774                                 &op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE);
1775         if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) {
1776                 dev_err(ctrl->dev,
1777                         "FCP Op failed - cmdiu dma mapping failed.\n");
1778                 ret = EFAULT;
1779                 goto out_on_error;
1780         }
1781
1782         op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev,
1783                                 &op->rsp_iu, sizeof(op->rsp_iu),
1784                                 DMA_FROM_DEVICE);
1785         if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) {
1786                 dev_err(ctrl->dev,
1787                         "FCP Op failed - rspiu dma mapping failed.\n");
1788                 ret = EFAULT;
1789         }
1790
1791         atomic_set(&op->state, FCPOP_STATE_IDLE);
1792 out_on_error:
1793         return ret;
1794 }
1795
1796 static int
1797 nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq,
1798                 unsigned int hctx_idx, unsigned int numa_node)
1799 {
1800         struct nvme_fc_ctrl *ctrl = set->driver_data;
1801         struct nvme_fcp_op_w_sgl *op = blk_mq_rq_to_pdu(rq);
1802         int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
1803         struct nvme_fc_queue *queue = &ctrl->queues[queue_idx];
1804         int res;
1805
1806         res = __nvme_fc_init_request(ctrl, queue, &op->op, rq, queue->rqcnt++);
1807         if (res)
1808                 return res;
1809         op->op.fcp_req.first_sgl = &op->sgl[0];
1810         op->op.fcp_req.private = &op->priv[0];
1811         nvme_req(rq)->ctrl = &ctrl->ctrl;
1812         return res;
1813 }
1814
1815 static int
1816 nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl)
1817 {
1818         struct nvme_fc_fcp_op *aen_op;
1819         struct nvme_fc_cmd_iu *cmdiu;
1820         struct nvme_command *sqe;
1821         void *private;
1822         int i, ret;
1823
1824         aen_op = ctrl->aen_ops;
1825         for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
1826                 private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz,
1827                                                 GFP_KERNEL);
1828                 if (!private)
1829                         return -ENOMEM;
1830
1831                 cmdiu = &aen_op->cmd_iu;
1832                 sqe = &cmdiu->sqe;
1833                 ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0],
1834                                 aen_op, (struct request *)NULL,
1835                                 (NVME_AQ_BLK_MQ_DEPTH + i));
1836                 if (ret) {
1837                         kfree(private);
1838                         return ret;
1839                 }
1840
1841                 aen_op->flags = FCOP_FLAGS_AEN;
1842                 aen_op->fcp_req.private = private;
1843
1844                 memset(sqe, 0, sizeof(*sqe));
1845                 sqe->common.opcode = nvme_admin_async_event;
1846                 /* Note: core layer may overwrite the sqe.command_id value */
1847                 sqe->common.command_id = NVME_AQ_BLK_MQ_DEPTH + i;
1848         }
1849         return 0;
1850 }
1851
1852 static void
1853 nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl)
1854 {
1855         struct nvme_fc_fcp_op *aen_op;
1856         int i;
1857
1858         aen_op = ctrl->aen_ops;
1859         for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
1860                 if (!aen_op->fcp_req.private)
1861                         continue;
1862
1863                 __nvme_fc_exit_request(ctrl, aen_op);
1864
1865                 kfree(aen_op->fcp_req.private);
1866                 aen_op->fcp_req.private = NULL;
1867         }
1868 }
1869
1870 static inline void
1871 __nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, struct nvme_fc_ctrl *ctrl,
1872                 unsigned int qidx)
1873 {
1874         struct nvme_fc_queue *queue = &ctrl->queues[qidx];
1875
1876         hctx->driver_data = queue;
1877         queue->hctx = hctx;
1878 }
1879
1880 static int
1881 nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1882                 unsigned int hctx_idx)
1883 {
1884         struct nvme_fc_ctrl *ctrl = data;
1885
1886         __nvme_fc_init_hctx(hctx, ctrl, hctx_idx + 1);
1887
1888         return 0;
1889 }
1890
1891 static int
1892 nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1893                 unsigned int hctx_idx)
1894 {
1895         struct nvme_fc_ctrl *ctrl = data;
1896
1897         __nvme_fc_init_hctx(hctx, ctrl, hctx_idx);
1898
1899         return 0;
1900 }
1901
1902 static void
1903 nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx)
1904 {
1905         struct nvme_fc_queue *queue;
1906
1907         queue = &ctrl->queues[idx];
1908         memset(queue, 0, sizeof(*queue));
1909         queue->ctrl = ctrl;
1910         queue->qnum = idx;
1911         atomic_set(&queue->csn, 0);
1912         queue->dev = ctrl->dev;
1913
1914         if (idx > 0)
1915                 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
1916         else
1917                 queue->cmnd_capsule_len = sizeof(struct nvme_command);
1918
1919         /*
1920          * Considered whether we should allocate buffers for all SQEs
1921          * and CQEs and dma map them - mapping their respective entries
1922          * into the request structures (kernel vm addr and dma address)
1923          * thus the driver could use the buffers/mappings directly.
1924          * It only makes sense if the LLDD would use them for its
1925          * messaging api. It's very unlikely most adapter api's would use
1926          * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
1927          * structures were used instead.
1928          */
1929 }
1930
1931 /*
1932  * This routine terminates a queue at the transport level.
1933  * The transport has already ensured that all outstanding ios on
1934  * the queue have been terminated.
1935  * The transport will send a Disconnect LS request to terminate
1936  * the queue's connection. Termination of the admin queue will also
1937  * terminate the association at the target.
1938  */
1939 static void
1940 nvme_fc_free_queue(struct nvme_fc_queue *queue)
1941 {
1942         if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags))
1943                 return;
1944
1945         clear_bit(NVME_FC_Q_LIVE, &queue->flags);
1946         /*
1947          * Current implementation never disconnects a single queue.
1948          * It always terminates a whole association. So there is never
1949          * a disconnect(queue) LS sent to the target.
1950          */
1951
1952         queue->connection_id = 0;
1953         atomic_set(&queue->csn, 0);
1954 }
1955
1956 static void
1957 __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl,
1958         struct nvme_fc_queue *queue, unsigned int qidx)
1959 {
1960         if (ctrl->lport->ops->delete_queue)
1961                 ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx,
1962                                 queue->lldd_handle);
1963         queue->lldd_handle = NULL;
1964 }
1965
1966 static void
1967 nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl)
1968 {
1969         int i;
1970
1971         for (i = 1; i < ctrl->ctrl.queue_count; i++)
1972                 nvme_fc_free_queue(&ctrl->queues[i]);
1973 }
1974
1975 static int
1976 __nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl,
1977         struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize)
1978 {
1979         int ret = 0;
1980
1981         queue->lldd_handle = NULL;
1982         if (ctrl->lport->ops->create_queue)
1983                 ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport,
1984                                 qidx, qsize, &queue->lldd_handle);
1985
1986         return ret;
1987 }
1988
1989 static void
1990 nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl)
1991 {
1992         struct nvme_fc_queue *queue = &ctrl->queues[ctrl->ctrl.queue_count - 1];
1993         int i;
1994
1995         for (i = ctrl->ctrl.queue_count - 1; i >= 1; i--, queue--)
1996                 __nvme_fc_delete_hw_queue(ctrl, queue, i);
1997 }
1998
1999 static int
2000 nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
2001 {
2002         struct nvme_fc_queue *queue = &ctrl->queues[1];
2003         int i, ret;
2004
2005         for (i = 1; i < ctrl->ctrl.queue_count; i++, queue++) {
2006                 ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize);
2007                 if (ret)
2008                         goto delete_queues;
2009         }
2010
2011         return 0;
2012
2013 delete_queues:
2014         for (; i >= 0; i--)
2015                 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i);
2016         return ret;
2017 }
2018
2019 static int
2020 nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
2021 {
2022         int i, ret = 0;
2023
2024         for (i = 1; i < ctrl->ctrl.queue_count; i++) {
2025                 ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize,
2026                                         (qsize / 5));
2027                 if (ret)
2028                         break;
2029                 ret = nvmf_connect_io_queue(&ctrl->ctrl, i, false);
2030                 if (ret)
2031                         break;
2032
2033                 set_bit(NVME_FC_Q_LIVE, &ctrl->queues[i].flags);
2034         }
2035
2036         return ret;
2037 }
2038
2039 static void
2040 nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl)
2041 {
2042         int i;
2043
2044         for (i = 1; i < ctrl->ctrl.queue_count; i++)
2045                 nvme_fc_init_queue(ctrl, i);
2046 }
2047
2048 static void
2049 nvme_fc_ctrl_free(struct kref *ref)
2050 {
2051         struct nvme_fc_ctrl *ctrl =
2052                 container_of(ref, struct nvme_fc_ctrl, ref);
2053         unsigned long flags;
2054
2055         if (ctrl->ctrl.tagset) {
2056                 blk_cleanup_queue(ctrl->ctrl.connect_q);
2057                 blk_mq_free_tag_set(&ctrl->tag_set);
2058         }
2059
2060         /* remove from rport list */
2061         spin_lock_irqsave(&ctrl->rport->lock, flags);
2062         list_del(&ctrl->ctrl_list);
2063         spin_unlock_irqrestore(&ctrl->rport->lock, flags);
2064
2065         blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
2066         blk_cleanup_queue(ctrl->ctrl.admin_q);
2067         blk_cleanup_queue(ctrl->ctrl.fabrics_q);
2068         blk_mq_free_tag_set(&ctrl->admin_tag_set);
2069
2070         kfree(ctrl->queues);
2071
2072         put_device(ctrl->dev);
2073         nvme_fc_rport_put(ctrl->rport);
2074
2075         ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
2076         if (ctrl->ctrl.opts)
2077                 nvmf_free_options(ctrl->ctrl.opts);
2078         kfree(ctrl);
2079 }
2080
2081 static void
2082 nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl)
2083 {
2084         kref_put(&ctrl->ref, nvme_fc_ctrl_free);
2085 }
2086
2087 static int
2088 nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
2089 {
2090         return kref_get_unless_zero(&ctrl->ref);
2091 }
2092
2093 /*
2094  * All accesses from nvme core layer done - can now free the
2095  * controller. Called after last nvme_put_ctrl() call
2096  */
2097 static void
2098 nvme_fc_nvme_ctrl_freed(struct nvme_ctrl *nctrl)
2099 {
2100         struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2101
2102         WARN_ON(nctrl != &ctrl->ctrl);
2103
2104         nvme_fc_ctrl_put(ctrl);
2105 }
2106
2107 static void
2108 nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
2109 {
2110         int active;
2111
2112         /*
2113          * if an error (io timeout, etc) while (re)connecting,
2114          * it's an error on creating the new association.
2115          * Start the error recovery thread if it hasn't already
2116          * been started. It is expected there could be multiple
2117          * ios hitting this path before things are cleaned up.
2118          */
2119         if (ctrl->ctrl.state == NVME_CTRL_CONNECTING) {
2120                 active = atomic_xchg(&ctrl->err_work_active, 1);
2121                 if (!active && !queue_work(nvme_fc_wq, &ctrl->err_work)) {
2122                         atomic_set(&ctrl->err_work_active, 0);
2123                         WARN_ON(1);
2124                 }
2125                 return;
2126         }
2127
2128         /* Otherwise, only proceed if in LIVE state - e.g. on first error */
2129         if (ctrl->ctrl.state != NVME_CTRL_LIVE)
2130                 return;
2131
2132         dev_warn(ctrl->ctrl.device,
2133                 "NVME-FC{%d}: transport association error detected: %s\n",
2134                 ctrl->cnum, errmsg);
2135         dev_warn(ctrl->ctrl.device,
2136                 "NVME-FC{%d}: resetting controller\n", ctrl->cnum);
2137
2138         nvme_reset_ctrl(&ctrl->ctrl);
2139 }
2140
2141 static enum blk_eh_timer_return
2142 nvme_fc_timeout(struct request *rq, bool reserved)
2143 {
2144         struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2145         struct nvme_fc_ctrl *ctrl = op->ctrl;
2146
2147         /*
2148          * we can't individually ABTS an io without affecting the queue,
2149          * thus killing the queue, and thus the association.
2150          * So resolve by performing a controller reset, which will stop
2151          * the host/io stack, terminate the association on the link,
2152          * and recreate an association on the link.
2153          */
2154         nvme_fc_error_recovery(ctrl, "io timeout error");
2155
2156         /*
2157          * the io abort has been initiated. Have the reset timer
2158          * restarted and the abort completion will complete the io
2159          * shortly. Avoids a synchronous wait while the abort finishes.
2160          */
2161         return BLK_EH_RESET_TIMER;
2162 }
2163
2164 static int
2165 nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2166                 struct nvme_fc_fcp_op *op)
2167 {
2168         struct nvmefc_fcp_req *freq = &op->fcp_req;
2169         int ret;
2170
2171         freq->sg_cnt = 0;
2172
2173         if (!blk_rq_nr_phys_segments(rq))
2174                 return 0;
2175
2176         freq->sg_table.sgl = freq->first_sgl;
2177         ret = sg_alloc_table_chained(&freq->sg_table,
2178                         blk_rq_nr_phys_segments(rq), freq->sg_table.sgl,
2179                         NVME_INLINE_SG_CNT);
2180         if (ret)
2181                 return -ENOMEM;
2182
2183         op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl);
2184         WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
2185         freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
2186                                 op->nents, rq_dma_dir(rq));
2187         if (unlikely(freq->sg_cnt <= 0)) {
2188                 sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2189                 freq->sg_cnt = 0;
2190                 return -EFAULT;
2191         }
2192
2193         /*
2194          * TODO: blk_integrity_rq(rq)  for DIF
2195          */
2196         return 0;
2197 }
2198
2199 static void
2200 nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2201                 struct nvme_fc_fcp_op *op)
2202 {
2203         struct nvmefc_fcp_req *freq = &op->fcp_req;
2204
2205         if (!freq->sg_cnt)
2206                 return;
2207
2208         fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
2209                         rq_dma_dir(rq));
2210
2211         sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2212
2213         freq->sg_cnt = 0;
2214 }
2215
2216 /*
2217  * In FC, the queue is a logical thing. At transport connect, the target
2218  * creates its "queue" and returns a handle that is to be given to the
2219  * target whenever it posts something to the corresponding SQ.  When an
2220  * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
2221  * command contained within the SQE, an io, and assigns a FC exchange
2222  * to it. The SQE and the associated SQ handle are sent in the initial
2223  * CMD IU sents on the exchange. All transfers relative to the io occur
2224  * as part of the exchange.  The CQE is the last thing for the io,
2225  * which is transferred (explicitly or implicitly) with the RSP IU
2226  * sent on the exchange. After the CQE is received, the FC exchange is
2227  * terminaed and the Exchange may be used on a different io.
2228  *
2229  * The transport to LLDD api has the transport making a request for a
2230  * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
2231  * resource and transfers the command. The LLDD will then process all
2232  * steps to complete the io. Upon completion, the transport done routine
2233  * is called.
2234  *
2235  * So - while the operation is outstanding to the LLDD, there is a link
2236  * level FC exchange resource that is also outstanding. This must be
2237  * considered in all cleanup operations.
2238  */
2239 static blk_status_t
2240 nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
2241         struct nvme_fc_fcp_op *op, u32 data_len,
2242         enum nvmefc_fcp_datadir io_dir)
2243 {
2244         struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2245         struct nvme_command *sqe = &cmdiu->sqe;
2246         int ret, opstate;
2247
2248         /*
2249          * before attempting to send the io, check to see if we believe
2250          * the target device is present
2251          */
2252         if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
2253                 return BLK_STS_RESOURCE;
2254
2255         if (!nvme_fc_ctrl_get(ctrl))
2256                 return BLK_STS_IOERR;
2257
2258         /* format the FC-NVME CMD IU and fcp_req */
2259         cmdiu->connection_id = cpu_to_be64(queue->connection_id);
2260         cmdiu->data_len = cpu_to_be32(data_len);
2261         switch (io_dir) {
2262         case NVMEFC_FCP_WRITE:
2263                 cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
2264                 break;
2265         case NVMEFC_FCP_READ:
2266                 cmdiu->flags = FCNVME_CMD_FLAGS_READ;
2267                 break;
2268         case NVMEFC_FCP_NODATA:
2269                 cmdiu->flags = 0;
2270                 break;
2271         }
2272         op->fcp_req.payload_length = data_len;
2273         op->fcp_req.io_dir = io_dir;
2274         op->fcp_req.transferred_length = 0;
2275         op->fcp_req.rcv_rsplen = 0;
2276         op->fcp_req.status = NVME_SC_SUCCESS;
2277         op->fcp_req.sqid = cpu_to_le16(queue->qnum);
2278
2279         /*
2280          * validate per fabric rules, set fields mandated by fabric spec
2281          * as well as those by FC-NVME spec.
2282          */
2283         WARN_ON_ONCE(sqe->common.metadata);
2284         sqe->common.flags |= NVME_CMD_SGL_METABUF;
2285
2286         /*
2287          * format SQE DPTR field per FC-NVME rules:
2288          *    type=0x5     Transport SGL Data Block Descriptor
2289          *    subtype=0xA  Transport-specific value
2290          *    address=0
2291          *    length=length of the data series
2292          */
2293         sqe->rw.dptr.sgl.type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2294                                         NVME_SGL_FMT_TRANSPORT_A;
2295         sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
2296         sqe->rw.dptr.sgl.addr = 0;
2297
2298         if (!(op->flags & FCOP_FLAGS_AEN)) {
2299                 ret = nvme_fc_map_data(ctrl, op->rq, op);
2300                 if (ret < 0) {
2301                         nvme_cleanup_cmd(op->rq);
2302                         nvme_fc_ctrl_put(ctrl);
2303                         if (ret == -ENOMEM || ret == -EAGAIN)
2304                                 return BLK_STS_RESOURCE;
2305                         return BLK_STS_IOERR;
2306                 }
2307         }
2308
2309         fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
2310                                   sizeof(op->cmd_iu), DMA_TO_DEVICE);
2311
2312         atomic_set(&op->state, FCPOP_STATE_ACTIVE);
2313
2314         if (!(op->flags & FCOP_FLAGS_AEN))
2315                 blk_mq_start_request(op->rq);
2316
2317         cmdiu->csn = cpu_to_be32(atomic_inc_return(&queue->csn));
2318         ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
2319                                         &ctrl->rport->remoteport,
2320                                         queue->lldd_handle, &op->fcp_req);
2321
2322         if (ret) {
2323                 /*
2324                  * If the lld fails to send the command is there an issue with
2325                  * the csn value?  If the command that fails is the Connect,
2326                  * no - as the connection won't be live.  If it is a command
2327                  * post-connect, it's possible a gap in csn may be created.
2328                  * Does this matter?  As Linux initiators don't send fused
2329                  * commands, no.  The gap would exist, but as there's nothing
2330                  * that depends on csn order to be delivered on the target
2331                  * side, it shouldn't hurt.  It would be difficult for a
2332                  * target to even detect the csn gap as it has no idea when the
2333                  * cmd with the csn was supposed to arrive.
2334                  */
2335                 opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
2336                 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2337
2338                 if (!(op->flags & FCOP_FLAGS_AEN))
2339                         nvme_fc_unmap_data(ctrl, op->rq, op);
2340
2341                 nvme_cleanup_cmd(op->rq);
2342                 nvme_fc_ctrl_put(ctrl);
2343
2344                 if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE &&
2345                                 ret != -EBUSY)
2346                         return BLK_STS_IOERR;
2347
2348                 return BLK_STS_RESOURCE;
2349         }
2350
2351         return BLK_STS_OK;
2352 }
2353
2354 static blk_status_t
2355 nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
2356                         const struct blk_mq_queue_data *bd)
2357 {
2358         struct nvme_ns *ns = hctx->queue->queuedata;
2359         struct nvme_fc_queue *queue = hctx->driver_data;
2360         struct nvme_fc_ctrl *ctrl = queue->ctrl;
2361         struct request *rq = bd->rq;
2362         struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2363         struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2364         struct nvme_command *sqe = &cmdiu->sqe;
2365         enum nvmefc_fcp_datadir io_dir;
2366         bool queue_ready = test_bit(NVME_FC_Q_LIVE, &queue->flags);
2367         u32 data_len;
2368         blk_status_t ret;
2369
2370         if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE ||
2371             !nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2372                 return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq);
2373
2374         ret = nvme_setup_cmd(ns, rq, sqe);
2375         if (ret)
2376                 return ret;
2377
2378         /*
2379          * nvme core doesn't quite treat the rq opaquely. Commands such
2380          * as WRITE ZEROES will return a non-zero rq payload_bytes yet
2381          * there is no actual payload to be transferred.
2382          * To get it right, key data transmission on there being 1 or
2383          * more physical segments in the sg list. If there is no
2384          * physical segments, there is no payload.
2385          */
2386         if (blk_rq_nr_phys_segments(rq)) {
2387                 data_len = blk_rq_payload_bytes(rq);
2388                 io_dir = ((rq_data_dir(rq) == WRITE) ?
2389                                         NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
2390         } else {
2391                 data_len = 0;
2392                 io_dir = NVMEFC_FCP_NODATA;
2393         }
2394
2395
2396         return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
2397 }
2398
2399 static void
2400 nvme_fc_submit_async_event(struct nvme_ctrl *arg)
2401 {
2402         struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
2403         struct nvme_fc_fcp_op *aen_op;
2404         unsigned long flags;
2405         bool terminating = false;
2406         blk_status_t ret;
2407
2408         spin_lock_irqsave(&ctrl->lock, flags);
2409         if (ctrl->flags & FCCTRL_TERMIO)
2410                 terminating = true;
2411         spin_unlock_irqrestore(&ctrl->lock, flags);
2412
2413         if (terminating)
2414                 return;
2415
2416         aen_op = &ctrl->aen_ops[0];
2417
2418         ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
2419                                         NVMEFC_FCP_NODATA);
2420         if (ret)
2421                 dev_err(ctrl->ctrl.device,
2422                         "failed async event work\n");
2423 }
2424
2425 static void
2426 nvme_fc_complete_rq(struct request *rq)
2427 {
2428         struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2429         struct nvme_fc_ctrl *ctrl = op->ctrl;
2430
2431         atomic_set(&op->state, FCPOP_STATE_IDLE);
2432
2433         nvme_fc_unmap_data(ctrl, rq, op);
2434         nvme_complete_rq(rq);
2435         nvme_fc_ctrl_put(ctrl);
2436 }
2437
2438 /*
2439  * This routine is used by the transport when it needs to find active
2440  * io on a queue that is to be terminated. The transport uses
2441  * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
2442  * this routine to kill them on a 1 by 1 basis.
2443  *
2444  * As FC allocates FC exchange for each io, the transport must contact
2445  * the LLDD to terminate the exchange, thus releasing the FC exchange.
2446  * After terminating the exchange the LLDD will call the transport's
2447  * normal io done path for the request, but it will have an aborted
2448  * status. The done path will return the io request back to the block
2449  * layer with an error status.
2450  */
2451 static bool
2452 nvme_fc_terminate_exchange(struct request *req, void *data, bool reserved)
2453 {
2454         struct nvme_ctrl *nctrl = data;
2455         struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2456         struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
2457
2458         __nvme_fc_abort_op(ctrl, op);
2459         return true;
2460 }
2461
2462
2463 static const struct blk_mq_ops nvme_fc_mq_ops = {
2464         .queue_rq       = nvme_fc_queue_rq,
2465         .complete       = nvme_fc_complete_rq,
2466         .init_request   = nvme_fc_init_request,
2467         .exit_request   = nvme_fc_exit_request,
2468         .init_hctx      = nvme_fc_init_hctx,
2469         .timeout        = nvme_fc_timeout,
2470 };
2471
2472 static int
2473 nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl)
2474 {
2475         struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2476         unsigned int nr_io_queues;
2477         int ret;
2478
2479         nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
2480                                 ctrl->lport->ops->max_hw_queues);
2481         ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2482         if (ret) {
2483                 dev_info(ctrl->ctrl.device,
2484                         "set_queue_count failed: %d\n", ret);
2485                 return ret;
2486         }
2487
2488         ctrl->ctrl.queue_count = nr_io_queues + 1;
2489         if (!nr_io_queues)
2490                 return 0;
2491
2492         nvme_fc_init_io_queues(ctrl);
2493
2494         memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
2495         ctrl->tag_set.ops = &nvme_fc_mq_ops;
2496         ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
2497         ctrl->tag_set.reserved_tags = 1; /* fabric connect */
2498         ctrl->tag_set.numa_node = ctrl->ctrl.numa_node;
2499         ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
2500         ctrl->tag_set.cmd_size =
2501                 struct_size((struct nvme_fcp_op_w_sgl *)NULL, priv,
2502                             ctrl->lport->ops->fcprqst_priv_sz);
2503         ctrl->tag_set.driver_data = ctrl;
2504         ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1;
2505         ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
2506
2507         ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
2508         if (ret)
2509                 return ret;
2510
2511         ctrl->ctrl.tagset = &ctrl->tag_set;
2512
2513         ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
2514         if (IS_ERR(ctrl->ctrl.connect_q)) {
2515                 ret = PTR_ERR(ctrl->ctrl.connect_q);
2516                 goto out_free_tag_set;
2517         }
2518
2519         ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2520         if (ret)
2521                 goto out_cleanup_blk_queue;
2522
2523         ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2524         if (ret)
2525                 goto out_delete_hw_queues;
2526
2527         ctrl->ioq_live = true;
2528
2529         return 0;
2530
2531 out_delete_hw_queues:
2532         nvme_fc_delete_hw_io_queues(ctrl);
2533 out_cleanup_blk_queue:
2534         blk_cleanup_queue(ctrl->ctrl.connect_q);
2535 out_free_tag_set:
2536         blk_mq_free_tag_set(&ctrl->tag_set);
2537         nvme_fc_free_io_queues(ctrl);
2538
2539         /* force put free routine to ignore io queues */
2540         ctrl->ctrl.tagset = NULL;
2541
2542         return ret;
2543 }
2544
2545 static int
2546 nvme_fc_recreate_io_queues(struct nvme_fc_ctrl *ctrl)
2547 {
2548         struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2549         u32 prior_ioq_cnt = ctrl->ctrl.queue_count - 1;
2550         unsigned int nr_io_queues;
2551         int ret;
2552
2553         nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
2554                                 ctrl->lport->ops->max_hw_queues);
2555         ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2556         if (ret) {
2557                 dev_info(ctrl->ctrl.device,
2558                         "set_queue_count failed: %d\n", ret);
2559                 return ret;
2560         }
2561
2562         if (!nr_io_queues && prior_ioq_cnt) {
2563                 dev_info(ctrl->ctrl.device,
2564                         "Fail Reconnect: At least 1 io queue "
2565                         "required (was %d)\n", prior_ioq_cnt);
2566                 return -ENOSPC;
2567         }
2568
2569         ctrl->ctrl.queue_count = nr_io_queues + 1;
2570         /* check for io queues existing */
2571         if (ctrl->ctrl.queue_count == 1)
2572                 return 0;
2573
2574         ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2575         if (ret)
2576                 goto out_free_io_queues;
2577
2578         ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2579         if (ret)
2580                 goto out_delete_hw_queues;
2581
2582         if (prior_ioq_cnt != nr_io_queues)
2583                 dev_info(ctrl->ctrl.device,
2584                         "reconnect: revising io queue count from %d to %d\n",
2585                         prior_ioq_cnt, nr_io_queues);
2586         blk_mq_update_nr_hw_queues(&ctrl->tag_set, nr_io_queues);
2587
2588         return 0;
2589
2590 out_delete_hw_queues:
2591         nvme_fc_delete_hw_io_queues(ctrl);
2592 out_free_io_queues:
2593         nvme_fc_free_io_queues(ctrl);
2594         return ret;
2595 }
2596
2597 static void
2598 nvme_fc_rport_active_on_lport(struct nvme_fc_rport *rport)
2599 {
2600         struct nvme_fc_lport *lport = rport->lport;
2601
2602         atomic_inc(&lport->act_rport_cnt);
2603 }
2604
2605 static void
2606 nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport *rport)
2607 {
2608         struct nvme_fc_lport *lport = rport->lport;
2609         u32 cnt;
2610
2611         cnt = atomic_dec_return(&lport->act_rport_cnt);
2612         if (cnt == 0 && lport->localport.port_state == FC_OBJSTATE_DELETED)
2613                 lport->ops->localport_delete(&lport->localport);
2614 }
2615
2616 static int
2617 nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl *ctrl)
2618 {
2619         struct nvme_fc_rport *rport = ctrl->rport;
2620         u32 cnt;
2621
2622         if (ctrl->assoc_active)
2623                 return 1;
2624
2625         ctrl->assoc_active = true;
2626         cnt = atomic_inc_return(&rport->act_ctrl_cnt);
2627         if (cnt == 1)
2628                 nvme_fc_rport_active_on_lport(rport);
2629
2630         return 0;
2631 }
2632
2633 static int
2634 nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl *ctrl)
2635 {
2636         struct nvme_fc_rport *rport = ctrl->rport;
2637         struct nvme_fc_lport *lport = rport->lport;
2638         u32 cnt;
2639
2640         /* ctrl->assoc_active=false will be set independently */
2641
2642         cnt = atomic_dec_return(&rport->act_ctrl_cnt);
2643         if (cnt == 0) {
2644                 if (rport->remoteport.port_state == FC_OBJSTATE_DELETED)
2645                         lport->ops->remoteport_delete(&rport->remoteport);
2646                 nvme_fc_rport_inactive_on_lport(rport);
2647         }
2648
2649         return 0;
2650 }
2651
2652 /*
2653  * This routine restarts the controller on the host side, and
2654  * on the link side, recreates the controller association.
2655  */
2656 static int
2657 nvme_fc_create_association(struct nvme_fc_ctrl *ctrl)
2658 {
2659         struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2660         int ret;
2661         bool changed;
2662
2663         ++ctrl->ctrl.nr_reconnects;
2664
2665         if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
2666                 return -ENODEV;
2667
2668         if (nvme_fc_ctlr_active_on_rport(ctrl))
2669                 return -ENOTUNIQ;
2670
2671         dev_info(ctrl->ctrl.device,
2672                 "NVME-FC{%d}: create association : host wwpn 0x%016llx "
2673                 " rport wwpn 0x%016llx: NQN \"%s\"\n",
2674                 ctrl->cnum, ctrl->lport->localport.port_name,
2675                 ctrl->rport->remoteport.port_name, ctrl->ctrl.opts->subsysnqn);
2676
2677         /*
2678          * Create the admin queue
2679          */
2680
2681         ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0,
2682                                 NVME_AQ_DEPTH);
2683         if (ret)
2684                 goto out_free_queue;
2685
2686         ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0],
2687                                 NVME_AQ_DEPTH, (NVME_AQ_DEPTH / 4));
2688         if (ret)
2689                 goto out_delete_hw_queue;
2690
2691         ret = nvmf_connect_admin_queue(&ctrl->ctrl);
2692         if (ret)
2693                 goto out_disconnect_admin_queue;
2694
2695         set_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags);
2696
2697         /*
2698          * Check controller capabilities
2699          *
2700          * todo:- add code to check if ctrl attributes changed from
2701          * prior connection values
2702          */
2703
2704         ret = nvme_enable_ctrl(&ctrl->ctrl);
2705         if (ret)
2706                 goto out_disconnect_admin_queue;
2707
2708         ctrl->ctrl.max_hw_sectors =
2709                 (ctrl->lport->ops->max_sgl_segments - 1) << (PAGE_SHIFT - 9);
2710
2711         blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
2712
2713         ret = nvme_init_identify(&ctrl->ctrl);
2714         if (ret)
2715                 goto out_disconnect_admin_queue;
2716
2717         /* sanity checks */
2718
2719         /* FC-NVME does not have other data in the capsule */
2720         if (ctrl->ctrl.icdoff) {
2721                 dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n",
2722                                 ctrl->ctrl.icdoff);
2723                 goto out_disconnect_admin_queue;
2724         }
2725
2726         /* FC-NVME supports normal SGL Data Block Descriptors */
2727
2728         if (opts->queue_size > ctrl->ctrl.maxcmd) {
2729                 /* warn if maxcmd is lower than queue_size */
2730                 dev_warn(ctrl->ctrl.device,
2731                         "queue_size %zu > ctrl maxcmd %u, reducing "
2732                         "to maxcmd\n",
2733                         opts->queue_size, ctrl->ctrl.maxcmd);
2734                 opts->queue_size = ctrl->ctrl.maxcmd;
2735         }
2736
2737         if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
2738                 /* warn if sqsize is lower than queue_size */
2739                 dev_warn(ctrl->ctrl.device,
2740                         "queue_size %zu > ctrl sqsize %u, reducing "
2741                         "to sqsize\n",
2742                         opts->queue_size, ctrl->ctrl.sqsize + 1);
2743                 opts->queue_size = ctrl->ctrl.sqsize + 1;
2744         }
2745
2746         ret = nvme_fc_init_aen_ops(ctrl);
2747         if (ret)
2748                 goto out_term_aen_ops;
2749
2750         /*
2751          * Create the io queues
2752          */
2753
2754         if (ctrl->ctrl.queue_count > 1) {
2755                 if (!ctrl->ioq_live)
2756                         ret = nvme_fc_create_io_queues(ctrl);
2757                 else
2758                         ret = nvme_fc_recreate_io_queues(ctrl);
2759                 if (ret)
2760                         goto out_term_aen_ops;
2761         }
2762
2763         changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
2764
2765         ctrl->ctrl.nr_reconnects = 0;
2766
2767         if (changed)
2768                 nvme_start_ctrl(&ctrl->ctrl);
2769
2770         return 0;       /* Success */
2771
2772 out_term_aen_ops:
2773         nvme_fc_term_aen_ops(ctrl);
2774 out_disconnect_admin_queue:
2775         /* send a Disconnect(association) LS to fc-nvme target */
2776         nvme_fc_xmt_disconnect_assoc(ctrl);
2777         ctrl->association_id = 0;
2778 out_delete_hw_queue:
2779         __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
2780 out_free_queue:
2781         nvme_fc_free_queue(&ctrl->queues[0]);
2782         ctrl->assoc_active = false;
2783         nvme_fc_ctlr_inactive_on_rport(ctrl);
2784
2785         return ret;
2786 }
2787
2788 /*
2789  * This routine stops operation of the controller on the host side.
2790  * On the host os stack side: Admin and IO queues are stopped,
2791  *   outstanding ios on them terminated via FC ABTS.
2792  * On the link side: the association is terminated.
2793  */
2794 static void
2795 nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
2796 {
2797         unsigned long flags;
2798
2799         if (!ctrl->assoc_active)
2800                 return;
2801         ctrl->assoc_active = false;
2802
2803         spin_lock_irqsave(&ctrl->lock, flags);
2804         ctrl->flags |= FCCTRL_TERMIO;
2805         ctrl->iocnt = 0;
2806         spin_unlock_irqrestore(&ctrl->lock, flags);
2807
2808         /*
2809          * If io queues are present, stop them and terminate all outstanding
2810          * ios on them. As FC allocates FC exchange for each io, the
2811          * transport must contact the LLDD to terminate the exchange,
2812          * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
2813          * to tell us what io's are busy and invoke a transport routine
2814          * to kill them with the LLDD.  After terminating the exchange
2815          * the LLDD will call the transport's normal io done path, but it
2816          * will have an aborted status. The done path will return the
2817          * io requests back to the block layer as part of normal completions
2818          * (but with error status).
2819          */
2820         if (ctrl->ctrl.queue_count > 1) {
2821                 nvme_stop_queues(&ctrl->ctrl);
2822                 blk_mq_tagset_busy_iter(&ctrl->tag_set,
2823                                 nvme_fc_terminate_exchange, &ctrl->ctrl);
2824                 blk_mq_tagset_wait_completed_request(&ctrl->tag_set);
2825         }
2826
2827         /*
2828          * Other transports, which don't have link-level contexts bound
2829          * to sqe's, would try to gracefully shutdown the controller by
2830          * writing the registers for shutdown and polling (call
2831          * nvme_shutdown_ctrl()). Given a bunch of i/o was potentially
2832          * just aborted and we will wait on those contexts, and given
2833          * there was no indication of how live the controlelr is on the
2834          * link, don't send more io to create more contexts for the
2835          * shutdown. Let the controller fail via keepalive failure if
2836          * its still present.
2837          */
2838
2839         /*
2840          * clean up the admin queue. Same thing as above.
2841          * use blk_mq_tagset_busy_itr() and the transport routine to
2842          * terminate the exchanges.
2843          */
2844         blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
2845         blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
2846                                 nvme_fc_terminate_exchange, &ctrl->ctrl);
2847         blk_mq_tagset_wait_completed_request(&ctrl->admin_tag_set);
2848
2849         /* kill the aens as they are a separate path */
2850         nvme_fc_abort_aen_ops(ctrl);
2851
2852         /* wait for all io that had to be aborted */
2853         spin_lock_irq(&ctrl->lock);
2854         wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock);
2855         ctrl->flags &= ~FCCTRL_TERMIO;
2856         spin_unlock_irq(&ctrl->lock);
2857
2858         nvme_fc_term_aen_ops(ctrl);
2859
2860         /*
2861          * send a Disconnect(association) LS to fc-nvme target
2862          * Note: could have been sent at top of process, but
2863          * cleaner on link traffic if after the aborts complete.
2864          * Note: if association doesn't exist, association_id will be 0
2865          */
2866         if (ctrl->association_id)
2867                 nvme_fc_xmt_disconnect_assoc(ctrl);
2868
2869         ctrl->association_id = 0;
2870
2871         if (ctrl->ctrl.tagset) {
2872                 nvme_fc_delete_hw_io_queues(ctrl);
2873                 nvme_fc_free_io_queues(ctrl);
2874         }
2875
2876         __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
2877         nvme_fc_free_queue(&ctrl->queues[0]);
2878
2879         /* re-enable the admin_q so anything new can fast fail */
2880         blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
2881
2882         /* resume the io queues so that things will fast fail */
2883         nvme_start_queues(&ctrl->ctrl);
2884
2885         nvme_fc_ctlr_inactive_on_rport(ctrl);
2886 }
2887
2888 static void
2889 nvme_fc_delete_ctrl(struct nvme_ctrl *nctrl)
2890 {
2891         struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2892
2893         cancel_work_sync(&ctrl->err_work);
2894         cancel_delayed_work_sync(&ctrl->connect_work);
2895         /*
2896          * kill the association on the link side.  this will block
2897          * waiting for io to terminate
2898          */
2899         nvme_fc_delete_association(ctrl);
2900 }
2901
2902 static void
2903 nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status)
2904 {
2905         struct nvme_fc_rport *rport = ctrl->rport;
2906         struct nvme_fc_remote_port *portptr = &rport->remoteport;
2907         unsigned long recon_delay = ctrl->ctrl.opts->reconnect_delay * HZ;
2908         bool recon = true;
2909
2910         if (ctrl->ctrl.state != NVME_CTRL_CONNECTING)
2911                 return;
2912
2913         if (portptr->port_state == FC_OBJSTATE_ONLINE)
2914                 dev_info(ctrl->ctrl.device,
2915                         "NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
2916                         ctrl->cnum, status);
2917         else if (time_after_eq(jiffies, rport->dev_loss_end))
2918                 recon = false;
2919
2920         if (recon && nvmf_should_reconnect(&ctrl->ctrl)) {
2921                 if (portptr->port_state == FC_OBJSTATE_ONLINE)
2922                         dev_info(ctrl->ctrl.device,
2923                                 "NVME-FC{%d}: Reconnect attempt in %ld "
2924                                 "seconds\n",
2925                                 ctrl->cnum, recon_delay / HZ);
2926                 else if (time_after(jiffies + recon_delay, rport->dev_loss_end))
2927                         recon_delay = rport->dev_loss_end - jiffies;
2928
2929                 queue_delayed_work(nvme_wq, &ctrl->connect_work, recon_delay);
2930         } else {
2931                 if (portptr->port_state == FC_OBJSTATE_ONLINE)
2932                         dev_warn(ctrl->ctrl.device,
2933                                 "NVME-FC{%d}: Max reconnect attempts (%d) "
2934                                 "reached.\n",
2935                                 ctrl->cnum, ctrl->ctrl.nr_reconnects);
2936                 else
2937                         dev_warn(ctrl->ctrl.device,
2938                                 "NVME-FC{%d}: dev_loss_tmo (%d) expired "
2939                                 "while waiting for remoteport connectivity.\n",
2940                                 ctrl->cnum, portptr->dev_loss_tmo);
2941                 WARN_ON(nvme_delete_ctrl(&ctrl->ctrl));
2942         }
2943 }
2944
2945 static void
2946 __nvme_fc_terminate_io(struct nvme_fc_ctrl *ctrl)
2947 {
2948         /*
2949          * if state is connecting - the error occurred as part of a
2950          * reconnect attempt. The create_association error paths will
2951          * clean up any outstanding io.
2952          *
2953          * if it's a different state - ensure all pending io is
2954          * terminated. Given this can delay while waiting for the
2955          * aborted io to return, we recheck adapter state below
2956          * before changing state.
2957          */
2958         if (ctrl->ctrl.state != NVME_CTRL_CONNECTING) {
2959                 nvme_stop_keep_alive(&ctrl->ctrl);
2960
2961                 /* will block will waiting for io to terminate */
2962                 nvme_fc_delete_association(ctrl);
2963         }
2964
2965         if (ctrl->ctrl.state != NVME_CTRL_CONNECTING &&
2966             !nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING))
2967                 dev_err(ctrl->ctrl.device,
2968                         "NVME-FC{%d}: error_recovery: Couldn't change state "
2969                         "to CONNECTING\n", ctrl->cnum);
2970 }
2971
2972 static void
2973 nvme_fc_reset_ctrl_work(struct work_struct *work)
2974 {
2975         struct nvme_fc_ctrl *ctrl =
2976                 container_of(work, struct nvme_fc_ctrl, ctrl.reset_work);
2977         int ret;
2978
2979         __nvme_fc_terminate_io(ctrl);
2980
2981         nvme_stop_ctrl(&ctrl->ctrl);
2982
2983         if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE)
2984                 ret = nvme_fc_create_association(ctrl);
2985         else
2986                 ret = -ENOTCONN;
2987
2988         if (ret)
2989                 nvme_fc_reconnect_or_delete(ctrl, ret);
2990         else
2991                 dev_info(ctrl->ctrl.device,
2992                         "NVME-FC{%d}: controller reset complete\n",
2993                         ctrl->cnum);
2994 }
2995
2996 static void
2997 nvme_fc_connect_err_work(struct work_struct *work)
2998 {
2999         struct nvme_fc_ctrl *ctrl =
3000                         container_of(work, struct nvme_fc_ctrl, err_work);
3001
3002         __nvme_fc_terminate_io(ctrl);
3003
3004         atomic_set(&ctrl->err_work_active, 0);
3005
3006         /*
3007          * Rescheduling the connection after recovering
3008          * from the io error is left to the reconnect work
3009          * item, which is what should have stalled waiting on
3010          * the io that had the error that scheduled this work.
3011          */
3012 }
3013
3014 static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
3015         .name                   = "fc",
3016         .module                 = THIS_MODULE,
3017         .flags                  = NVME_F_FABRICS,
3018         .reg_read32             = nvmf_reg_read32,
3019         .reg_read64             = nvmf_reg_read64,
3020         .reg_write32            = nvmf_reg_write32,
3021         .free_ctrl              = nvme_fc_nvme_ctrl_freed,
3022         .submit_async_event     = nvme_fc_submit_async_event,
3023         .delete_ctrl            = nvme_fc_delete_ctrl,
3024         .get_address            = nvmf_get_address,
3025 };
3026
3027 static void
3028 nvme_fc_connect_ctrl_work(struct work_struct *work)
3029 {
3030         int ret;
3031
3032         struct nvme_fc_ctrl *ctrl =
3033                         container_of(to_delayed_work(work),
3034                                 struct nvme_fc_ctrl, connect_work);
3035
3036         ret = nvme_fc_create_association(ctrl);
3037         if (ret)
3038                 nvme_fc_reconnect_or_delete(ctrl, ret);
3039         else
3040                 dev_info(ctrl->ctrl.device,
3041                         "NVME-FC{%d}: controller connect complete\n",
3042                         ctrl->cnum);
3043 }
3044
3045
3046 static const struct blk_mq_ops nvme_fc_admin_mq_ops = {
3047         .queue_rq       = nvme_fc_queue_rq,
3048         .complete       = nvme_fc_complete_rq,
3049         .init_request   = nvme_fc_init_request,
3050         .exit_request   = nvme_fc_exit_request,
3051         .init_hctx      = nvme_fc_init_admin_hctx,
3052         .timeout        = nvme_fc_timeout,
3053 };
3054
3055
3056 /*
3057  * Fails a controller request if it matches an existing controller
3058  * (association) with the same tuple:
3059  * <Host NQN, Host ID, local FC port, remote FC port, SUBSYS NQN>
3060  *
3061  * The ports don't need to be compared as they are intrinsically
3062  * already matched by the port pointers supplied.
3063  */
3064 static bool
3065 nvme_fc_existing_controller(struct nvme_fc_rport *rport,
3066                 struct nvmf_ctrl_options *opts)
3067 {
3068         struct nvme_fc_ctrl *ctrl;
3069         unsigned long flags;
3070         bool found = false;
3071
3072         spin_lock_irqsave(&rport->lock, flags);
3073         list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
3074                 found = nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts);
3075                 if (found)
3076                         break;
3077         }
3078         spin_unlock_irqrestore(&rport->lock, flags);
3079
3080         return found;
3081 }
3082
3083 static struct nvme_ctrl *
3084 nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
3085         struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
3086 {
3087         struct nvme_fc_ctrl *ctrl;
3088         unsigned long flags;
3089         int ret, idx;
3090
3091         if (!(rport->remoteport.port_role &
3092             (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) {
3093                 ret = -EBADR;
3094                 goto out_fail;
3095         }
3096
3097         if (!opts->duplicate_connect &&
3098             nvme_fc_existing_controller(rport, opts)) {
3099                 ret = -EALREADY;
3100                 goto out_fail;
3101         }
3102
3103         ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
3104         if (!ctrl) {
3105                 ret = -ENOMEM;
3106                 goto out_fail;
3107         }
3108
3109         idx = ida_simple_get(&nvme_fc_ctrl_cnt, 0, 0, GFP_KERNEL);
3110         if (idx < 0) {
3111                 ret = -ENOSPC;
3112                 goto out_free_ctrl;
3113         }
3114
3115         ctrl->ctrl.opts = opts;
3116         ctrl->ctrl.nr_reconnects = 0;
3117         if (lport->dev)
3118                 ctrl->ctrl.numa_node = dev_to_node(lport->dev);
3119         else
3120                 ctrl->ctrl.numa_node = NUMA_NO_NODE;
3121         INIT_LIST_HEAD(&ctrl->ctrl_list);
3122         ctrl->lport = lport;
3123         ctrl->rport = rport;
3124         ctrl->dev = lport->dev;
3125         ctrl->cnum = idx;
3126         ctrl->ioq_live = false;
3127         ctrl->assoc_active = false;
3128         atomic_set(&ctrl->err_work_active, 0);
3129         init_waitqueue_head(&ctrl->ioabort_wait);
3130
3131         get_device(ctrl->dev);
3132         kref_init(&ctrl->ref);
3133
3134         INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work);
3135         INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
3136         INIT_WORK(&ctrl->err_work, nvme_fc_connect_err_work);
3137         spin_lock_init(&ctrl->lock);
3138
3139         /* io queue count */
3140         ctrl->ctrl.queue_count = min_t(unsigned int,
3141                                 opts->nr_io_queues,
3142                                 lport->ops->max_hw_queues);
3143         ctrl->ctrl.queue_count++;       /* +1 for admin queue */
3144
3145         ctrl->ctrl.sqsize = opts->queue_size - 1;
3146         ctrl->ctrl.kato = opts->kato;
3147         ctrl->ctrl.cntlid = 0xffff;
3148
3149         ret = -ENOMEM;
3150         ctrl->queues = kcalloc(ctrl->ctrl.queue_count,
3151                                 sizeof(struct nvme_fc_queue), GFP_KERNEL);
3152         if (!ctrl->queues)
3153                 goto out_free_ida;
3154
3155         nvme_fc_init_queue(ctrl, 0);
3156
3157         memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
3158         ctrl->admin_tag_set.ops = &nvme_fc_admin_mq_ops;
3159         ctrl->admin_tag_set.queue_depth = NVME_AQ_MQ_TAG_DEPTH;
3160         ctrl->admin_tag_set.reserved_tags = 2; /* fabric connect + Keep-Alive */
3161         ctrl->admin_tag_set.numa_node = ctrl->ctrl.numa_node;
3162         ctrl->admin_tag_set.cmd_size =
3163                 struct_size((struct nvme_fcp_op_w_sgl *)NULL, priv,
3164                             ctrl->lport->ops->fcprqst_priv_sz);
3165         ctrl->admin_tag_set.driver_data = ctrl;
3166         ctrl->admin_tag_set.nr_hw_queues = 1;
3167         ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
3168         ctrl->admin_tag_set.flags = BLK_MQ_F_NO_SCHED;
3169
3170         ret = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
3171         if (ret)
3172                 goto out_free_queues;
3173         ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set;
3174
3175         ctrl->ctrl.fabrics_q = blk_mq_init_queue(&ctrl->admin_tag_set);
3176         if (IS_ERR(ctrl->ctrl.fabrics_q)) {
3177                 ret = PTR_ERR(ctrl->ctrl.fabrics_q);
3178                 goto out_free_admin_tag_set;
3179         }
3180
3181         ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
3182         if (IS_ERR(ctrl->ctrl.admin_q)) {
3183                 ret = PTR_ERR(ctrl->ctrl.admin_q);
3184                 goto out_cleanup_fabrics_q;
3185         }
3186
3187         /*
3188          * Would have been nice to init io queues tag set as well.
3189          * However, we require interaction from the controller
3190          * for max io queue count before we can do so.
3191          * Defer this to the connect path.
3192          */
3193
3194         ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
3195         if (ret)
3196                 goto out_cleanup_admin_q;
3197
3198         /* at this point, teardown path changes to ref counting on nvme ctrl */
3199
3200         spin_lock_irqsave(&rport->lock, flags);
3201         list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
3202         spin_unlock_irqrestore(&rport->lock, flags);
3203
3204         if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING) ||
3205             !nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
3206                 dev_err(ctrl->ctrl.device,
3207                         "NVME-FC{%d}: failed to init ctrl state\n", ctrl->cnum);
3208                 goto fail_ctrl;
3209         }
3210
3211         if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
3212                 dev_err(ctrl->ctrl.device,
3213                         "NVME-FC{%d}: failed to schedule initial connect\n",
3214                         ctrl->cnum);
3215                 goto fail_ctrl;
3216         }
3217
3218         flush_delayed_work(&ctrl->connect_work);
3219
3220         dev_info(ctrl->ctrl.device,
3221                 "NVME-FC{%d}: new ctrl: NQN \"%s\"\n",
3222                 ctrl->cnum, ctrl->ctrl.opts->subsysnqn);
3223
3224         return &ctrl->ctrl;
3225
3226 fail_ctrl:
3227         nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING);
3228         cancel_work_sync(&ctrl->ctrl.reset_work);
3229         cancel_work_sync(&ctrl->err_work);
3230         cancel_delayed_work_sync(&ctrl->connect_work);
3231
3232         ctrl->ctrl.opts = NULL;
3233
3234         /* initiate nvme ctrl ref counting teardown */
3235         nvme_uninit_ctrl(&ctrl->ctrl);
3236
3237         /* Remove core ctrl ref. */
3238         nvme_put_ctrl(&ctrl->ctrl);
3239
3240         /* as we're past the point where we transition to the ref
3241          * counting teardown path, if we return a bad pointer here,
3242          * the calling routine, thinking it's prior to the
3243          * transition, will do an rport put. Since the teardown
3244          * path also does a rport put, we do an extra get here to
3245          * so proper order/teardown happens.
3246          */
3247         nvme_fc_rport_get(rport);
3248
3249         return ERR_PTR(-EIO);
3250
3251 out_cleanup_admin_q:
3252         blk_cleanup_queue(ctrl->ctrl.admin_q);
3253 out_cleanup_fabrics_q:
3254         blk_cleanup_queue(ctrl->ctrl.fabrics_q);
3255 out_free_admin_tag_set:
3256         blk_mq_free_tag_set(&ctrl->admin_tag_set);
3257 out_free_queues:
3258         kfree(ctrl->queues);
3259 out_free_ida:
3260         put_device(ctrl->dev);
3261         ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
3262 out_free_ctrl:
3263         kfree(ctrl);
3264 out_fail:
3265         /* exit via here doesn't follow ctlr ref points */
3266         return ERR_PTR(ret);
3267 }
3268
3269
3270 struct nvmet_fc_traddr {
3271         u64     nn;
3272         u64     pn;
3273 };
3274
3275 static int
3276 __nvme_fc_parse_u64(substring_t *sstr, u64 *val)
3277 {
3278         u64 token64;
3279
3280         if (match_u64(sstr, &token64))
3281                 return -EINVAL;
3282         *val = token64;
3283
3284         return 0;
3285 }
3286
3287 /*
3288  * This routine validates and extracts the WWN's from the TRADDR string.
3289  * As kernel parsers need the 0x to determine number base, universally
3290  * build string to parse with 0x prefix before parsing name strings.
3291  */
3292 static int
3293 nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen)
3294 {
3295         char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1];
3296         substring_t wwn = { name, &name[sizeof(name)-1] };
3297         int nnoffset, pnoffset;
3298
3299         /* validate if string is one of the 2 allowed formats */
3300         if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH &&
3301                         !strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) &&
3302                         !strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET],
3303                                 "pn-0x", NVME_FC_TRADDR_OXNNLEN)) {
3304                 nnoffset = NVME_FC_TRADDR_OXNNLEN;
3305                 pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET +
3306                                                 NVME_FC_TRADDR_OXNNLEN;
3307         } else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH &&
3308                         !strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) &&
3309                         !strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET],
3310                                 "pn-", NVME_FC_TRADDR_NNLEN))) {
3311                 nnoffset = NVME_FC_TRADDR_NNLEN;
3312                 pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN;
3313         } else
3314                 goto out_einval;
3315
3316         name[0] = '0';
3317         name[1] = 'x';
3318         name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0;
3319
3320         memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3321         if (__nvme_fc_parse_u64(&wwn, &traddr->nn))
3322                 goto out_einval;
3323
3324         memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3325         if (__nvme_fc_parse_u64(&wwn, &traddr->pn))
3326                 goto out_einval;
3327
3328         return 0;
3329
3330 out_einval:
3331         pr_warn("%s: bad traddr string\n", __func__);
3332         return -EINVAL;
3333 }
3334
3335 static struct nvme_ctrl *
3336 nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
3337 {
3338         struct nvme_fc_lport *lport;
3339         struct nvme_fc_rport *rport;
3340         struct nvme_ctrl *ctrl;
3341         struct nvmet_fc_traddr laddr = { 0L, 0L };
3342         struct nvmet_fc_traddr raddr = { 0L, 0L };
3343         unsigned long flags;
3344         int ret;
3345
3346         ret = nvme_fc_parse_traddr(&raddr, opts->traddr, NVMF_TRADDR_SIZE);
3347         if (ret || !raddr.nn || !raddr.pn)
3348                 return ERR_PTR(-EINVAL);
3349
3350         ret = nvme_fc_parse_traddr(&laddr, opts->host_traddr, NVMF_TRADDR_SIZE);
3351         if (ret || !laddr.nn || !laddr.pn)
3352                 return ERR_PTR(-EINVAL);
3353
3354         /* find the host and remote ports to connect together */
3355         spin_lock_irqsave(&nvme_fc_lock, flags);
3356         list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3357                 if (lport->localport.node_name != laddr.nn ||
3358                     lport->localport.port_name != laddr.pn)
3359                         continue;
3360
3361                 list_for_each_entry(rport, &lport->endp_list, endp_list) {
3362                         if (rport->remoteport.node_name != raddr.nn ||
3363                             rport->remoteport.port_name != raddr.pn)
3364                                 continue;
3365
3366                         /* if fail to get reference fall through. Will error */
3367                         if (!nvme_fc_rport_get(rport))
3368                                 break;
3369
3370                         spin_unlock_irqrestore(&nvme_fc_lock, flags);
3371
3372                         ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport);
3373                         if (IS_ERR(ctrl))
3374                                 nvme_fc_rport_put(rport);
3375                         return ctrl;
3376                 }
3377         }
3378         spin_unlock_irqrestore(&nvme_fc_lock, flags);
3379
3380         pr_warn("%s: %s - %s combination not found\n",
3381                 __func__, opts->traddr, opts->host_traddr);
3382         return ERR_PTR(-ENOENT);
3383 }
3384
3385
3386 static struct nvmf_transport_ops nvme_fc_transport = {
3387         .name           = "fc",
3388         .module         = THIS_MODULE,
3389         .required_opts  = NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
3390         .allowed_opts   = NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO,
3391         .create_ctrl    = nvme_fc_create_ctrl,
3392 };
3393
3394 /* Arbitrary successive failures max. With lots of subsystems could be high */
3395 #define DISCOVERY_MAX_FAIL      20
3396
3397 static ssize_t nvme_fc_nvme_discovery_store(struct device *dev,
3398                 struct device_attribute *attr, const char *buf, size_t count)
3399 {
3400         unsigned long flags;
3401         LIST_HEAD(local_disc_list);
3402         struct nvme_fc_lport *lport;
3403         struct nvme_fc_rport *rport;
3404         int failcnt = 0;
3405
3406         spin_lock_irqsave(&nvme_fc_lock, flags);
3407 restart:
3408         list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3409                 list_for_each_entry(rport, &lport->endp_list, endp_list) {
3410                         if (!nvme_fc_lport_get(lport))
3411                                 continue;
3412                         if (!nvme_fc_rport_get(rport)) {
3413                                 /*
3414                                  * This is a temporary condition. Upon restart
3415                                  * this rport will be gone from the list.
3416                                  *
3417                                  * Revert the lport put and retry.  Anything
3418                                  * added to the list already will be skipped (as
3419                                  * they are no longer list_empty).  Loops should
3420                                  * resume at rports that were not yet seen.
3421                                  */
3422                                 nvme_fc_lport_put(lport);
3423
3424                                 if (failcnt++ < DISCOVERY_MAX_FAIL)
3425                                         goto restart;
3426
3427                                 pr_err("nvme_discovery: too many reference "
3428                                        "failures\n");
3429                                 goto process_local_list;
3430                         }
3431                         if (list_empty(&rport->disc_list))
3432                                 list_add_tail(&rport->disc_list,
3433                                               &local_disc_list);
3434                 }
3435         }
3436
3437 process_local_list:
3438         while (!list_empty(&local_disc_list)) {
3439                 rport = list_first_entry(&local_disc_list,
3440                                          struct nvme_fc_rport, disc_list);
3441                 list_del_init(&rport->disc_list);
3442                 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3443
3444                 lport = rport->lport;
3445                 /* signal discovery. Won't hurt if it repeats */
3446                 nvme_fc_signal_discovery_scan(lport, rport);
3447                 nvme_fc_rport_put(rport);
3448                 nvme_fc_lport_put(lport);
3449
3450                 spin_lock_irqsave(&nvme_fc_lock, flags);
3451         }
3452         spin_unlock_irqrestore(&nvme_fc_lock, flags);
3453
3454         return count;
3455 }
3456 static DEVICE_ATTR(nvme_discovery, 0200, NULL, nvme_fc_nvme_discovery_store);
3457
3458 static struct attribute *nvme_fc_attrs[] = {
3459         &dev_attr_nvme_discovery.attr,
3460         NULL
3461 };
3462
3463 static struct attribute_group nvme_fc_attr_group = {
3464         .attrs = nvme_fc_attrs,
3465 };
3466
3467 static const struct attribute_group *nvme_fc_attr_groups[] = {
3468         &nvme_fc_attr_group,
3469         NULL
3470 };
3471
3472 static struct class fc_class = {
3473         .name = "fc",
3474         .dev_groups = nvme_fc_attr_groups,
3475         .owner = THIS_MODULE,
3476 };
3477
3478 static int __init nvme_fc_init_module(void)
3479 {
3480         int ret;
3481
3482         nvme_fc_wq = alloc_workqueue("nvme_fc_wq", WQ_MEM_RECLAIM, 0);
3483         if (!nvme_fc_wq)
3484                 return -ENOMEM;
3485
3486         /*
3487          * NOTE:
3488          * It is expected that in the future the kernel will combine
3489          * the FC-isms that are currently under scsi and now being
3490          * added to by NVME into a new standalone FC class. The SCSI
3491          * and NVME protocols and their devices would be under this
3492          * new FC class.
3493          *
3494          * As we need something to post FC-specific udev events to,
3495          * specifically for nvme probe events, start by creating the
3496          * new device class.  When the new standalone FC class is
3497          * put in place, this code will move to a more generic
3498          * location for the class.
3499          */
3500         ret = class_register(&fc_class);
3501         if (ret) {
3502                 pr_err("couldn't register class fc\n");
3503                 goto out_destroy_wq;
3504         }
3505
3506         /*
3507          * Create a device for the FC-centric udev events
3508          */
3509         fc_udev_device = device_create(&fc_class, NULL, MKDEV(0, 0), NULL,
3510                                 "fc_udev_device");
3511         if (IS_ERR(fc_udev_device)) {
3512                 pr_err("couldn't create fc_udev device!\n");
3513                 ret = PTR_ERR(fc_udev_device);
3514                 goto out_destroy_class;
3515         }
3516
3517         ret = nvmf_register_transport(&nvme_fc_transport);
3518         if (ret)
3519                 goto out_destroy_device;
3520
3521         return 0;
3522
3523 out_destroy_device:
3524         device_destroy(&fc_class, MKDEV(0, 0));
3525 out_destroy_class:
3526         class_unregister(&fc_class);
3527 out_destroy_wq:
3528         destroy_workqueue(nvme_fc_wq);
3529
3530         return ret;
3531 }
3532
3533 static void
3534 nvme_fc_delete_controllers(struct nvme_fc_rport *rport)
3535 {
3536         struct nvme_fc_ctrl *ctrl;
3537
3538         spin_lock(&rport->lock);
3539         list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
3540                 dev_warn(ctrl->ctrl.device,
3541                         "NVME-FC{%d}: transport unloading: deleting ctrl\n",
3542                         ctrl->cnum);
3543                 nvme_delete_ctrl(&ctrl->ctrl);
3544         }
3545         spin_unlock(&rport->lock);
3546 }
3547
3548 static void
3549 nvme_fc_cleanup_for_unload(void)
3550 {
3551         struct nvme_fc_lport *lport;
3552         struct nvme_fc_rport *rport;
3553
3554         list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3555                 list_for_each_entry(rport, &lport->endp_list, endp_list) {
3556                         nvme_fc_delete_controllers(rport);
3557                 }
3558         }
3559 }
3560
3561 static void __exit nvme_fc_exit_module(void)
3562 {
3563         unsigned long flags;
3564         bool need_cleanup = false;
3565
3566         spin_lock_irqsave(&nvme_fc_lock, flags);
3567         nvme_fc_waiting_to_unload = true;
3568         if (!list_empty(&nvme_fc_lport_list)) {
3569                 need_cleanup = true;
3570                 nvme_fc_cleanup_for_unload();
3571         }
3572         spin_unlock_irqrestore(&nvme_fc_lock, flags);
3573         if (need_cleanup) {
3574                 pr_info("%s: waiting for ctlr deletes\n", __func__);
3575                 wait_for_completion(&nvme_fc_unload_proceed);
3576                 pr_info("%s: ctrl deletes complete\n", __func__);
3577         }
3578
3579         nvmf_unregister_transport(&nvme_fc_transport);
3580
3581         ida_destroy(&nvme_fc_local_port_cnt);
3582         ida_destroy(&nvme_fc_ctrl_cnt);
3583
3584         device_destroy(&fc_class, MKDEV(0, 0));
3585         class_unregister(&fc_class);
3586         destroy_workqueue(nvme_fc_wq);
3587 }
3588
3589 module_init(nvme_fc_init_module);
3590 module_exit(nvme_fc_exit_module);
3591
3592 MODULE_LICENSE("GPL v2");