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