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