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