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