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