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