blob: 158d313be84767f836142c25f2d1cf7ceb01b19e [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>
James Smart61bff8e2017-04-23 08:30:08 -070022#include <linux/delay.h>
James Smarte3994412016-12-02 00:28:42 -080023
24#include "nvme.h"
25#include "fabrics.h"
26#include <linux/nvme-fc-driver.h>
27#include <linux/nvme-fc.h>
28
29
30/* *************************** Data Structures/Defines ****************** */
31
32
33/*
34 * We handle AEN commands ourselves and don't even let the
35 * block layer know about them.
36 */
37#define NVME_FC_NR_AEN_COMMANDS 1
38#define NVME_FC_AQ_BLKMQ_DEPTH \
Sagi Grimberg7aa1f422017-06-18 16:15:59 +030039 (NVME_AQ_DEPTH - NVME_FC_NR_AEN_COMMANDS)
James Smarte3994412016-12-02 00:28:42 -080040#define AEN_CMDID_BASE (NVME_FC_AQ_BLKMQ_DEPTH + 1)
41
42enum nvme_fc_queue_flags {
43 NVME_FC_Q_CONNECTED = (1 << 0),
44};
45
46#define NVMEFC_QUEUE_DELAY 3 /* ms units */
47
48struct nvme_fc_queue {
49 struct nvme_fc_ctrl *ctrl;
50 struct device *dev;
51 struct blk_mq_hw_ctx *hctx;
52 void *lldd_handle;
53 int queue_size;
54 size_t cmnd_capsule_len;
55 u32 qnum;
56 u32 rqcnt;
57 u32 seqno;
58
59 u64 connection_id;
60 atomic_t csn;
61
62 unsigned long flags;
63} __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
64
James Smart8d64daf2017-04-11 11:35:09 -070065enum nvme_fcop_flags {
66 FCOP_FLAGS_TERMIO = (1 << 0),
67 FCOP_FLAGS_RELEASED = (1 << 1),
68 FCOP_FLAGS_COMPLETE = (1 << 2),
James Smart78a7ac22017-04-23 08:30:07 -070069 FCOP_FLAGS_AEN = (1 << 3),
James Smart8d64daf2017-04-11 11:35:09 -070070};
71
James Smarte3994412016-12-02 00:28:42 -080072struct nvmefc_ls_req_op {
73 struct nvmefc_ls_req ls_req;
74
James Smartc913a8b2017-04-11 11:35:08 -070075 struct nvme_fc_rport *rport;
James Smarte3994412016-12-02 00:28:42 -080076 struct nvme_fc_queue *queue;
77 struct request *rq;
James Smart8d64daf2017-04-11 11:35:09 -070078 u32 flags;
James Smarte3994412016-12-02 00:28:42 -080079
80 int ls_error;
81 struct completion ls_done;
James Smartc913a8b2017-04-11 11:35:08 -070082 struct list_head lsreq_list; /* rport->ls_req_list */
James Smarte3994412016-12-02 00:28:42 -080083 bool req_queued;
84};
85
86enum nvme_fcpop_state {
87 FCPOP_STATE_UNINIT = 0,
88 FCPOP_STATE_IDLE = 1,
89 FCPOP_STATE_ACTIVE = 2,
90 FCPOP_STATE_ABORTED = 3,
James Smart78a7ac22017-04-23 08:30:07 -070091 FCPOP_STATE_COMPLETE = 4,
James Smarte3994412016-12-02 00:28:42 -080092};
93
94struct nvme_fc_fcp_op {
95 struct nvme_request nreq; /*
96 * nvme/host/core.c
97 * requires this to be
98 * the 1st element in the
99 * private structure
100 * associated with the
101 * request.
102 */
103 struct nvmefc_fcp_req fcp_req;
104
105 struct nvme_fc_ctrl *ctrl;
106 struct nvme_fc_queue *queue;
107 struct request *rq;
108
109 atomic_t state;
James Smart78a7ac22017-04-23 08:30:07 -0700110 u32 flags;
James Smarte3994412016-12-02 00:28:42 -0800111 u32 rqno;
112 u32 nents;
113
114 struct nvme_fc_cmd_iu cmd_iu;
115 struct nvme_fc_ersp_iu rsp_iu;
116};
117
118struct nvme_fc_lport {
119 struct nvme_fc_local_port localport;
120
121 struct ida endp_cnt;
122 struct list_head port_list; /* nvme_fc_port_list */
123 struct list_head endp_list;
124 struct device *dev; /* physical device for dma */
125 struct nvme_fc_port_template *ops;
126 struct kref ref;
127} __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
128
129struct nvme_fc_rport {
130 struct nvme_fc_remote_port remoteport;
131
132 struct list_head endp_list; /* for lport->endp_list */
133 struct list_head ctrl_list;
James Smartc913a8b2017-04-11 11:35:08 -0700134 struct list_head ls_req_list;
135 struct device *dev; /* physical device for dma */
136 struct nvme_fc_lport *lport;
James Smarte3994412016-12-02 00:28:42 -0800137 spinlock_t lock;
138 struct kref ref;
139} __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
140
James Smart61bff8e2017-04-23 08:30:08 -0700141enum nvme_fcctrl_flags {
142 FCCTRL_TERMIO = (1 << 0),
James Smarte3994412016-12-02 00:28:42 -0800143};
144
145struct nvme_fc_ctrl {
146 spinlock_t lock;
147 struct nvme_fc_queue *queues;
James Smarte3994412016-12-02 00:28:42 -0800148 struct device *dev;
149 struct nvme_fc_lport *lport;
150 struct nvme_fc_rport *rport;
James Smart61bff8e2017-04-23 08:30:08 -0700151 u32 queue_count;
James Smarte3994412016-12-02 00:28:42 -0800152 u32 cnum;
153
154 u64 association_id;
155
156 u64 cap;
157
158 struct list_head ctrl_list; /* rport->ctrl_list */
James Smarte3994412016-12-02 00:28:42 -0800159
160 struct blk_mq_tag_set admin_tag_set;
161 struct blk_mq_tag_set tag_set;
162
163 struct work_struct delete_work;
James Smart61bff8e2017-04-23 08:30:08 -0700164 struct delayed_work connect_work;
James Smart61bff8e2017-04-23 08:30:08 -0700165
James Smarte3994412016-12-02 00:28:42 -0800166 struct kref ref;
James Smart61bff8e2017-04-23 08:30:08 -0700167 u32 flags;
168 u32 iocnt;
James Smarte3994412016-12-02 00:28:42 -0800169
170 struct nvme_fc_fcp_op aen_ops[NVME_FC_NR_AEN_COMMANDS];
171
172 struct nvme_ctrl ctrl;
173};
174
175static inline struct nvme_fc_ctrl *
176to_fc_ctrl(struct nvme_ctrl *ctrl)
177{
178 return container_of(ctrl, struct nvme_fc_ctrl, ctrl);
179}
180
181static inline struct nvme_fc_lport *
182localport_to_lport(struct nvme_fc_local_port *portptr)
183{
184 return container_of(portptr, struct nvme_fc_lport, localport);
185}
186
187static inline struct nvme_fc_rport *
188remoteport_to_rport(struct nvme_fc_remote_port *portptr)
189{
190 return container_of(portptr, struct nvme_fc_rport, remoteport);
191}
192
193static inline struct nvmefc_ls_req_op *
194ls_req_to_lsop(struct nvmefc_ls_req *lsreq)
195{
196 return container_of(lsreq, struct nvmefc_ls_req_op, ls_req);
197}
198
199static inline struct nvme_fc_fcp_op *
200fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq)
201{
202 return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req);
203}
204
205
206
207/* *************************** Globals **************************** */
208
209
210static DEFINE_SPINLOCK(nvme_fc_lock);
211
212static LIST_HEAD(nvme_fc_lport_list);
213static DEFINE_IDA(nvme_fc_local_port_cnt);
214static DEFINE_IDA(nvme_fc_ctrl_cnt);
215
James Smarte3994412016-12-02 00:28:42 -0800216
217
218
219/* *********************** FC-NVME Port Management ************************ */
220
221static int __nvme_fc_del_ctrl(struct nvme_fc_ctrl *);
222static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *,
223 struct nvme_fc_queue *, unsigned int);
224
225
226/**
227 * nvme_fc_register_localport - transport entry point called by an
228 * LLDD to register the existence of a NVME
229 * host FC port.
230 * @pinfo: pointer to information about the port to be registered
231 * @template: LLDD entrypoints and operational parameters for the port
232 * @dev: physical hardware device node port corresponds to. Will be
233 * used for DMA mappings
234 * @lport_p: pointer to a local port pointer. Upon success, the routine
235 * will allocate a nvme_fc_local_port structure and place its
236 * address in the local port pointer. Upon failure, local port
237 * pointer will be set to 0.
238 *
239 * Returns:
240 * a completion status. Must be 0 upon success; a negative errno
241 * (ex: -ENXIO) upon failure.
242 */
243int
244nvme_fc_register_localport(struct nvme_fc_port_info *pinfo,
245 struct nvme_fc_port_template *template,
246 struct device *dev,
247 struct nvme_fc_local_port **portptr)
248{
249 struct nvme_fc_lport *newrec;
250 unsigned long flags;
251 int ret, idx;
252
253 if (!template->localport_delete || !template->remoteport_delete ||
254 !template->ls_req || !template->fcp_io ||
255 !template->ls_abort || !template->fcp_abort ||
256 !template->max_hw_queues || !template->max_sgl_segments ||
257 !template->max_dif_sgl_segments || !template->dma_boundary) {
258 ret = -EINVAL;
259 goto out_reghost_failed;
260 }
261
262 newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz),
263 GFP_KERNEL);
264 if (!newrec) {
265 ret = -ENOMEM;
266 goto out_reghost_failed;
267 }
268
269 idx = ida_simple_get(&nvme_fc_local_port_cnt, 0, 0, GFP_KERNEL);
270 if (idx < 0) {
271 ret = -ENOSPC;
272 goto out_fail_kfree;
273 }
274
275 if (!get_device(dev) && dev) {
276 ret = -ENODEV;
277 goto out_ida_put;
278 }
279
280 INIT_LIST_HEAD(&newrec->port_list);
281 INIT_LIST_HEAD(&newrec->endp_list);
282 kref_init(&newrec->ref);
283 newrec->ops = template;
284 newrec->dev = dev;
285 ida_init(&newrec->endp_cnt);
286 newrec->localport.private = &newrec[1];
287 newrec->localport.node_name = pinfo->node_name;
288 newrec->localport.port_name = pinfo->port_name;
289 newrec->localport.port_role = pinfo->port_role;
290 newrec->localport.port_id = pinfo->port_id;
291 newrec->localport.port_state = FC_OBJSTATE_ONLINE;
292 newrec->localport.port_num = idx;
293
294 spin_lock_irqsave(&nvme_fc_lock, flags);
295 list_add_tail(&newrec->port_list, &nvme_fc_lport_list);
296 spin_unlock_irqrestore(&nvme_fc_lock, flags);
297
298 if (dev)
299 dma_set_seg_boundary(dev, template->dma_boundary);
300
301 *portptr = &newrec->localport;
302 return 0;
303
304out_ida_put:
305 ida_simple_remove(&nvme_fc_local_port_cnt, idx);
306out_fail_kfree:
307 kfree(newrec);
308out_reghost_failed:
309 *portptr = NULL;
310
311 return ret;
312}
313EXPORT_SYMBOL_GPL(nvme_fc_register_localport);
314
315static void
316nvme_fc_free_lport(struct kref *ref)
317{
318 struct nvme_fc_lport *lport =
319 container_of(ref, struct nvme_fc_lport, ref);
320 unsigned long flags;
321
322 WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED);
323 WARN_ON(!list_empty(&lport->endp_list));
324
325 /* remove from transport list */
326 spin_lock_irqsave(&nvme_fc_lock, flags);
327 list_del(&lport->port_list);
328 spin_unlock_irqrestore(&nvme_fc_lock, flags);
329
330 /* let the LLDD know we've finished tearing it down */
331 lport->ops->localport_delete(&lport->localport);
332
333 ida_simple_remove(&nvme_fc_local_port_cnt, lport->localport.port_num);
334 ida_destroy(&lport->endp_cnt);
335
336 put_device(lport->dev);
337
338 kfree(lport);
339}
340
341static void
342nvme_fc_lport_put(struct nvme_fc_lport *lport)
343{
344 kref_put(&lport->ref, nvme_fc_free_lport);
345}
346
347static int
348nvme_fc_lport_get(struct nvme_fc_lport *lport)
349{
350 return kref_get_unless_zero(&lport->ref);
351}
352
353/**
354 * nvme_fc_unregister_localport - transport entry point called by an
355 * LLDD to deregister/remove a previously
356 * registered a NVME host FC port.
357 * @localport: pointer to the (registered) local port that is to be
358 * deregistered.
359 *
360 * Returns:
361 * a completion status. Must be 0 upon success; a negative errno
362 * (ex: -ENXIO) upon failure.
363 */
364int
365nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr)
366{
367 struct nvme_fc_lport *lport = localport_to_lport(portptr);
368 unsigned long flags;
369
370 if (!portptr)
371 return -EINVAL;
372
373 spin_lock_irqsave(&nvme_fc_lock, flags);
374
375 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
376 spin_unlock_irqrestore(&nvme_fc_lock, flags);
377 return -EINVAL;
378 }
379 portptr->port_state = FC_OBJSTATE_DELETED;
380
381 spin_unlock_irqrestore(&nvme_fc_lock, flags);
382
383 nvme_fc_lport_put(lport);
384
385 return 0;
386}
387EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport);
388
389/**
390 * nvme_fc_register_remoteport - transport entry point called by an
391 * LLDD to register the existence of a NVME
392 * subsystem FC port on its fabric.
393 * @localport: pointer to the (registered) local port that the remote
394 * subsystem port is connected to.
395 * @pinfo: pointer to information about the port to be registered
396 * @rport_p: pointer to a remote port pointer. Upon success, the routine
397 * will allocate a nvme_fc_remote_port structure and place its
398 * address in the remote port pointer. Upon failure, remote port
399 * pointer will be set to 0.
400 *
401 * Returns:
402 * a completion status. Must be 0 upon success; a negative errno
403 * (ex: -ENXIO) upon failure.
404 */
405int
406nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
407 struct nvme_fc_port_info *pinfo,
408 struct nvme_fc_remote_port **portptr)
409{
410 struct nvme_fc_lport *lport = localport_to_lport(localport);
411 struct nvme_fc_rport *newrec;
412 unsigned long flags;
413 int ret, idx;
414
415 newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz),
416 GFP_KERNEL);
417 if (!newrec) {
418 ret = -ENOMEM;
419 goto out_reghost_failed;
420 }
421
422 if (!nvme_fc_lport_get(lport)) {
423 ret = -ESHUTDOWN;
424 goto out_kfree_rport;
425 }
426
427 idx = ida_simple_get(&lport->endp_cnt, 0, 0, GFP_KERNEL);
428 if (idx < 0) {
429 ret = -ENOSPC;
430 goto out_lport_put;
431 }
432
433 INIT_LIST_HEAD(&newrec->endp_list);
434 INIT_LIST_HEAD(&newrec->ctrl_list);
James Smartc913a8b2017-04-11 11:35:08 -0700435 INIT_LIST_HEAD(&newrec->ls_req_list);
James Smarte3994412016-12-02 00:28:42 -0800436 kref_init(&newrec->ref);
437 spin_lock_init(&newrec->lock);
438 newrec->remoteport.localport = &lport->localport;
James Smartc913a8b2017-04-11 11:35:08 -0700439 newrec->dev = lport->dev;
440 newrec->lport = lport;
James Smarte3994412016-12-02 00:28:42 -0800441 newrec->remoteport.private = &newrec[1];
442 newrec->remoteport.port_role = pinfo->port_role;
443 newrec->remoteport.node_name = pinfo->node_name;
444 newrec->remoteport.port_name = pinfo->port_name;
445 newrec->remoteport.port_id = pinfo->port_id;
446 newrec->remoteport.port_state = FC_OBJSTATE_ONLINE;
447 newrec->remoteport.port_num = idx;
448
449 spin_lock_irqsave(&nvme_fc_lock, flags);
450 list_add_tail(&newrec->endp_list, &lport->endp_list);
451 spin_unlock_irqrestore(&nvme_fc_lock, flags);
452
453 *portptr = &newrec->remoteport;
454 return 0;
455
456out_lport_put:
457 nvme_fc_lport_put(lport);
458out_kfree_rport:
459 kfree(newrec);
460out_reghost_failed:
461 *portptr = NULL;
462 return ret;
James Smarte3994412016-12-02 00:28:42 -0800463}
464EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport);
465
466static void
467nvme_fc_free_rport(struct kref *ref)
468{
469 struct nvme_fc_rport *rport =
470 container_of(ref, struct nvme_fc_rport, ref);
471 struct nvme_fc_lport *lport =
472 localport_to_lport(rport->remoteport.localport);
473 unsigned long flags;
474
475 WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED);
476 WARN_ON(!list_empty(&rport->ctrl_list));
477
478 /* remove from lport list */
479 spin_lock_irqsave(&nvme_fc_lock, flags);
480 list_del(&rport->endp_list);
481 spin_unlock_irqrestore(&nvme_fc_lock, flags);
482
483 /* let the LLDD know we've finished tearing it down */
484 lport->ops->remoteport_delete(&rport->remoteport);
485
486 ida_simple_remove(&lport->endp_cnt, rport->remoteport.port_num);
487
488 kfree(rport);
489
490 nvme_fc_lport_put(lport);
491}
492
493static void
494nvme_fc_rport_put(struct nvme_fc_rport *rport)
495{
496 kref_put(&rport->ref, nvme_fc_free_rport);
497}
498
499static int
500nvme_fc_rport_get(struct nvme_fc_rport *rport)
501{
502 return kref_get_unless_zero(&rport->ref);
503}
504
James Smart8d64daf2017-04-11 11:35:09 -0700505static int
506nvme_fc_abort_lsops(struct nvme_fc_rport *rport)
507{
508 struct nvmefc_ls_req_op *lsop;
509 unsigned long flags;
510
511restart:
512 spin_lock_irqsave(&rport->lock, flags);
513
514 list_for_each_entry(lsop, &rport->ls_req_list, lsreq_list) {
515 if (!(lsop->flags & FCOP_FLAGS_TERMIO)) {
516 lsop->flags |= FCOP_FLAGS_TERMIO;
517 spin_unlock_irqrestore(&rport->lock, flags);
518 rport->lport->ops->ls_abort(&rport->lport->localport,
519 &rport->remoteport,
520 &lsop->ls_req);
521 goto restart;
522 }
523 }
524 spin_unlock_irqrestore(&rport->lock, flags);
525
526 return 0;
527}
528
James Smarte3994412016-12-02 00:28:42 -0800529/**
530 * nvme_fc_unregister_remoteport - transport entry point called by an
531 * LLDD to deregister/remove a previously
532 * registered a NVME subsystem FC port.
533 * @remoteport: pointer to the (registered) remote port that is to be
534 * deregistered.
535 *
536 * Returns:
537 * a completion status. Must be 0 upon success; a negative errno
538 * (ex: -ENXIO) upon failure.
539 */
540int
541nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr)
542{
543 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
544 struct nvme_fc_ctrl *ctrl;
545 unsigned long flags;
546
547 if (!portptr)
548 return -EINVAL;
549
550 spin_lock_irqsave(&rport->lock, flags);
551
552 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
553 spin_unlock_irqrestore(&rport->lock, flags);
554 return -EINVAL;
555 }
556 portptr->port_state = FC_OBJSTATE_DELETED;
557
558 /* tear down all associations to the remote port */
559 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list)
560 __nvme_fc_del_ctrl(ctrl);
561
562 spin_unlock_irqrestore(&rport->lock, flags);
563
James Smart8d64daf2017-04-11 11:35:09 -0700564 nvme_fc_abort_lsops(rport);
565
James Smarte3994412016-12-02 00:28:42 -0800566 nvme_fc_rport_put(rport);
567 return 0;
568}
569EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport);
570
571
572/* *********************** FC-NVME DMA Handling **************************** */
573
574/*
575 * The fcloop device passes in a NULL device pointer. Real LLD's will
576 * pass in a valid device pointer. If NULL is passed to the dma mapping
577 * routines, depending on the platform, it may or may not succeed, and
578 * may crash.
579 *
580 * As such:
581 * Wrapper all the dma routines and check the dev pointer.
582 *
583 * If simple mappings (return just a dma address, we'll noop them,
584 * returning a dma address of 0.
585 *
586 * On more complex mappings (dma_map_sg), a pseudo routine fills
587 * in the scatter list, setting all dma addresses to 0.
588 */
589
590static inline dma_addr_t
591fc_dma_map_single(struct device *dev, void *ptr, size_t size,
592 enum dma_data_direction dir)
593{
594 return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
595}
596
597static inline int
598fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
599{
600 return dev ? dma_mapping_error(dev, dma_addr) : 0;
601}
602
603static inline void
604fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
605 enum dma_data_direction dir)
606{
607 if (dev)
608 dma_unmap_single(dev, addr, size, dir);
609}
610
611static inline void
612fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
613 enum dma_data_direction dir)
614{
615 if (dev)
616 dma_sync_single_for_cpu(dev, addr, size, dir);
617}
618
619static inline void
620fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
621 enum dma_data_direction dir)
622{
623 if (dev)
624 dma_sync_single_for_device(dev, addr, size, dir);
625}
626
627/* pseudo dma_map_sg call */
628static int
629fc_map_sg(struct scatterlist *sg, int nents)
630{
631 struct scatterlist *s;
632 int i;
633
634 WARN_ON(nents == 0 || sg[0].length == 0);
635
636 for_each_sg(sg, s, nents, i) {
637 s->dma_address = 0L;
638#ifdef CONFIG_NEED_SG_DMA_LENGTH
639 s->dma_length = s->length;
640#endif
641 }
642 return nents;
643}
644
645static inline int
646fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
647 enum dma_data_direction dir)
648{
649 return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
650}
651
652static inline void
653fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
654 enum dma_data_direction dir)
655{
656 if (dev)
657 dma_unmap_sg(dev, sg, nents, dir);
658}
659
660
661/* *********************** FC-NVME LS Handling **************************** */
662
663static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
664static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
665
666
667static void
James Smartc913a8b2017-04-11 11:35:08 -0700668__nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop)
James Smarte3994412016-12-02 00:28:42 -0800669{
James Smartc913a8b2017-04-11 11:35:08 -0700670 struct nvme_fc_rport *rport = lsop->rport;
James Smarte3994412016-12-02 00:28:42 -0800671 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
672 unsigned long flags;
673
James Smartc913a8b2017-04-11 11:35:08 -0700674 spin_lock_irqsave(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -0800675
676 if (!lsop->req_queued) {
James Smartc913a8b2017-04-11 11:35:08 -0700677 spin_unlock_irqrestore(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -0800678 return;
679 }
680
681 list_del(&lsop->lsreq_list);
682
683 lsop->req_queued = false;
684
James Smartc913a8b2017-04-11 11:35:08 -0700685 spin_unlock_irqrestore(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -0800686
James Smartc913a8b2017-04-11 11:35:08 -0700687 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
James Smarte3994412016-12-02 00:28:42 -0800688 (lsreq->rqstlen + lsreq->rsplen),
689 DMA_BIDIRECTIONAL);
690
James Smartc913a8b2017-04-11 11:35:08 -0700691 nvme_fc_rport_put(rport);
James Smarte3994412016-12-02 00:28:42 -0800692}
693
694static int
James Smartc913a8b2017-04-11 11:35:08 -0700695__nvme_fc_send_ls_req(struct nvme_fc_rport *rport,
James Smarte3994412016-12-02 00:28:42 -0800696 struct nvmefc_ls_req_op *lsop,
697 void (*done)(struct nvmefc_ls_req *req, int status))
698{
699 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
700 unsigned long flags;
James Smartc913a8b2017-04-11 11:35:08 -0700701 int ret = 0;
James Smarte3994412016-12-02 00:28:42 -0800702
James Smartc913a8b2017-04-11 11:35:08 -0700703 if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
704 return -ECONNREFUSED;
705
706 if (!nvme_fc_rport_get(rport))
James Smarte3994412016-12-02 00:28:42 -0800707 return -ESHUTDOWN;
708
709 lsreq->done = done;
James Smartc913a8b2017-04-11 11:35:08 -0700710 lsop->rport = rport;
James Smarte3994412016-12-02 00:28:42 -0800711 lsop->req_queued = false;
712 INIT_LIST_HEAD(&lsop->lsreq_list);
713 init_completion(&lsop->ls_done);
714
James Smartc913a8b2017-04-11 11:35:08 -0700715 lsreq->rqstdma = fc_dma_map_single(rport->dev, lsreq->rqstaddr,
James Smarte3994412016-12-02 00:28:42 -0800716 lsreq->rqstlen + lsreq->rsplen,
717 DMA_BIDIRECTIONAL);
James Smartc913a8b2017-04-11 11:35:08 -0700718 if (fc_dma_mapping_error(rport->dev, lsreq->rqstdma)) {
719 ret = -EFAULT;
720 goto out_putrport;
James Smarte3994412016-12-02 00:28:42 -0800721 }
722 lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
723
James Smartc913a8b2017-04-11 11:35:08 -0700724 spin_lock_irqsave(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -0800725
James Smartc913a8b2017-04-11 11:35:08 -0700726 list_add_tail(&lsop->lsreq_list, &rport->ls_req_list);
James Smarte3994412016-12-02 00:28:42 -0800727
728 lsop->req_queued = true;
729
James Smartc913a8b2017-04-11 11:35:08 -0700730 spin_unlock_irqrestore(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -0800731
James Smartc913a8b2017-04-11 11:35:08 -0700732 ret = rport->lport->ops->ls_req(&rport->lport->localport,
733 &rport->remoteport, lsreq);
James Smarte3994412016-12-02 00:28:42 -0800734 if (ret)
James Smartc913a8b2017-04-11 11:35:08 -0700735 goto out_unlink;
736
737 return 0;
738
739out_unlink:
740 lsop->ls_error = ret;
741 spin_lock_irqsave(&rport->lock, flags);
742 lsop->req_queued = false;
743 list_del(&lsop->lsreq_list);
744 spin_unlock_irqrestore(&rport->lock, flags);
745 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
746 (lsreq->rqstlen + lsreq->rsplen),
747 DMA_BIDIRECTIONAL);
748out_putrport:
749 nvme_fc_rport_put(rport);
James Smarte3994412016-12-02 00:28:42 -0800750
751 return ret;
752}
753
754static void
755nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status)
756{
757 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
758
759 lsop->ls_error = status;
760 complete(&lsop->ls_done);
761}
762
763static int
James Smartc913a8b2017-04-11 11:35:08 -0700764nvme_fc_send_ls_req(struct nvme_fc_rport *rport, struct nvmefc_ls_req_op *lsop)
James Smarte3994412016-12-02 00:28:42 -0800765{
766 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
767 struct fcnvme_ls_rjt *rjt = lsreq->rspaddr;
768 int ret;
769
James Smartc913a8b2017-04-11 11:35:08 -0700770 ret = __nvme_fc_send_ls_req(rport, lsop, nvme_fc_send_ls_req_done);
James Smarte3994412016-12-02 00:28:42 -0800771
James Smartc913a8b2017-04-11 11:35:08 -0700772 if (!ret) {
James Smarte3994412016-12-02 00:28:42 -0800773 /*
774 * No timeout/not interruptible as we need the struct
775 * to exist until the lldd calls us back. Thus mandate
776 * wait until driver calls back. lldd responsible for
777 * the timeout action
778 */
779 wait_for_completion(&lsop->ls_done);
780
James Smartc913a8b2017-04-11 11:35:08 -0700781 __nvme_fc_finish_ls_req(lsop);
James Smarte3994412016-12-02 00:28:42 -0800782
James Smartc913a8b2017-04-11 11:35:08 -0700783 ret = lsop->ls_error;
James Smarte3994412016-12-02 00:28:42 -0800784 }
785
James Smartc913a8b2017-04-11 11:35:08 -0700786 if (ret)
787 return ret;
788
James Smarte3994412016-12-02 00:28:42 -0800789 /* ACC or RJT payload ? */
790 if (rjt->w0.ls_cmd == FCNVME_LS_RJT)
791 return -ENXIO;
792
793 return 0;
794}
795
James Smartc913a8b2017-04-11 11:35:08 -0700796static int
797nvme_fc_send_ls_req_async(struct nvme_fc_rport *rport,
James Smarte3994412016-12-02 00:28:42 -0800798 struct nvmefc_ls_req_op *lsop,
799 void (*done)(struct nvmefc_ls_req *req, int status))
800{
James Smarte3994412016-12-02 00:28:42 -0800801 /* don't wait for completion */
802
James Smartc913a8b2017-04-11 11:35:08 -0700803 return __nvme_fc_send_ls_req(rport, lsop, done);
James Smarte3994412016-12-02 00:28:42 -0800804}
805
806/* Validation Error indexes into the string table below */
807enum {
808 VERR_NO_ERROR = 0,
809 VERR_LSACC = 1,
810 VERR_LSDESC_RQST = 2,
811 VERR_LSDESC_RQST_LEN = 3,
812 VERR_ASSOC_ID = 4,
813 VERR_ASSOC_ID_LEN = 5,
814 VERR_CONN_ID = 6,
815 VERR_CONN_ID_LEN = 7,
816 VERR_CR_ASSOC = 8,
817 VERR_CR_ASSOC_ACC_LEN = 9,
818 VERR_CR_CONN = 10,
819 VERR_CR_CONN_ACC_LEN = 11,
820 VERR_DISCONN = 12,
821 VERR_DISCONN_ACC_LEN = 13,
822};
823
824static char *validation_errors[] = {
825 "OK",
826 "Not LS_ACC",
827 "Not LSDESC_RQST",
828 "Bad LSDESC_RQST Length",
829 "Not Association ID",
830 "Bad Association ID Length",
831 "Not Connection ID",
832 "Bad Connection ID Length",
833 "Not CR_ASSOC Rqst",
834 "Bad CR_ASSOC ACC Length",
835 "Not CR_CONN Rqst",
836 "Bad CR_CONN ACC Length",
837 "Not Disconnect Rqst",
838 "Bad Disconnect ACC Length",
839};
840
841static int
842nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl,
843 struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio)
844{
845 struct nvmefc_ls_req_op *lsop;
846 struct nvmefc_ls_req *lsreq;
847 struct fcnvme_ls_cr_assoc_rqst *assoc_rqst;
848 struct fcnvme_ls_cr_assoc_acc *assoc_acc;
849 int ret, fcret = 0;
850
851 lsop = kzalloc((sizeof(*lsop) +
852 ctrl->lport->ops->lsrqst_priv_sz +
853 sizeof(*assoc_rqst) + sizeof(*assoc_acc)), GFP_KERNEL);
854 if (!lsop) {
855 ret = -ENOMEM;
856 goto out_no_memory;
857 }
858 lsreq = &lsop->ls_req;
859
860 lsreq->private = (void *)&lsop[1];
861 assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)
862 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
863 assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1];
864
865 assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION;
866 assoc_rqst->desc_list_len =
867 cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
868
869 assoc_rqst->assoc_cmd.desc_tag =
870 cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD);
871 assoc_rqst->assoc_cmd.desc_len =
872 fcnvme_lsdesc_len(
873 sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
874
875 assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
876 assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize);
877 /* Linux supports only Dynamic controllers */
878 assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff);
Christoph Hellwig8e412262017-05-17 09:54:27 +0200879 uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id);
James Smarte3994412016-12-02 00:28:42 -0800880 strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
881 min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE));
882 strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
883 min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE));
884
885 lsop->queue = queue;
886 lsreq->rqstaddr = assoc_rqst;
887 lsreq->rqstlen = sizeof(*assoc_rqst);
888 lsreq->rspaddr = assoc_acc;
889 lsreq->rsplen = sizeof(*assoc_acc);
890 lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
891
James Smartc913a8b2017-04-11 11:35:08 -0700892 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
James Smarte3994412016-12-02 00:28:42 -0800893 if (ret)
894 goto out_free_buffer;
895
896 /* process connect LS completion */
897
898 /* validate the ACC response */
899 if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
900 fcret = VERR_LSACC;
James Smartf77fc872017-03-23 20:41:25 -0700901 else if (assoc_acc->hdr.desc_list_len !=
James Smarte3994412016-12-02 00:28:42 -0800902 fcnvme_lsdesc_len(
903 sizeof(struct fcnvme_ls_cr_assoc_acc)))
904 fcret = VERR_CR_ASSOC_ACC_LEN;
James Smartf77fc872017-03-23 20:41:25 -0700905 else if (assoc_acc->hdr.rqst.desc_tag !=
906 cpu_to_be32(FCNVME_LSDESC_RQST))
James Smarte3994412016-12-02 00:28:42 -0800907 fcret = VERR_LSDESC_RQST;
908 else if (assoc_acc->hdr.rqst.desc_len !=
909 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
910 fcret = VERR_LSDESC_RQST_LEN;
911 else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION)
912 fcret = VERR_CR_ASSOC;
913 else if (assoc_acc->associd.desc_tag !=
914 cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
915 fcret = VERR_ASSOC_ID;
916 else if (assoc_acc->associd.desc_len !=
917 fcnvme_lsdesc_len(
918 sizeof(struct fcnvme_lsdesc_assoc_id)))
919 fcret = VERR_ASSOC_ID_LEN;
920 else if (assoc_acc->connectid.desc_tag !=
921 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
922 fcret = VERR_CONN_ID;
923 else if (assoc_acc->connectid.desc_len !=
924 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
925 fcret = VERR_CONN_ID_LEN;
926
927 if (fcret) {
928 ret = -EBADF;
929 dev_err(ctrl->dev,
930 "q %d connect failed: %s\n",
931 queue->qnum, validation_errors[fcret]);
932 } else {
933 ctrl->association_id =
934 be64_to_cpu(assoc_acc->associd.association_id);
935 queue->connection_id =
936 be64_to_cpu(assoc_acc->connectid.connection_id);
937 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
938 }
939
940out_free_buffer:
941 kfree(lsop);
942out_no_memory:
943 if (ret)
944 dev_err(ctrl->dev,
945 "queue %d connect admin queue failed (%d).\n",
946 queue->qnum, ret);
947 return ret;
948}
949
950static int
951nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
952 u16 qsize, u16 ersp_ratio)
953{
954 struct nvmefc_ls_req_op *lsop;
955 struct nvmefc_ls_req *lsreq;
956 struct fcnvme_ls_cr_conn_rqst *conn_rqst;
957 struct fcnvme_ls_cr_conn_acc *conn_acc;
958 int ret, fcret = 0;
959
960 lsop = kzalloc((sizeof(*lsop) +
961 ctrl->lport->ops->lsrqst_priv_sz +
962 sizeof(*conn_rqst) + sizeof(*conn_acc)), GFP_KERNEL);
963 if (!lsop) {
964 ret = -ENOMEM;
965 goto out_no_memory;
966 }
967 lsreq = &lsop->ls_req;
968
969 lsreq->private = (void *)&lsop[1];
970 conn_rqst = (struct fcnvme_ls_cr_conn_rqst *)
971 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
972 conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1];
973
974 conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION;
975 conn_rqst->desc_list_len = cpu_to_be32(
976 sizeof(struct fcnvme_lsdesc_assoc_id) +
977 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
978
979 conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
980 conn_rqst->associd.desc_len =
981 fcnvme_lsdesc_len(
982 sizeof(struct fcnvme_lsdesc_assoc_id));
983 conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
984 conn_rqst->connect_cmd.desc_tag =
985 cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD);
986 conn_rqst->connect_cmd.desc_len =
987 fcnvme_lsdesc_len(
988 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
989 conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
990 conn_rqst->connect_cmd.qid = cpu_to_be16(queue->qnum);
991 conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize);
992
993 lsop->queue = queue;
994 lsreq->rqstaddr = conn_rqst;
995 lsreq->rqstlen = sizeof(*conn_rqst);
996 lsreq->rspaddr = conn_acc;
997 lsreq->rsplen = sizeof(*conn_acc);
998 lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
999
James Smartc913a8b2017-04-11 11:35:08 -07001000 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
James Smarte3994412016-12-02 00:28:42 -08001001 if (ret)
1002 goto out_free_buffer;
1003
1004 /* process connect LS completion */
1005
1006 /* validate the ACC response */
1007 if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1008 fcret = VERR_LSACC;
James Smartf77fc872017-03-23 20:41:25 -07001009 else if (conn_acc->hdr.desc_list_len !=
James Smarte3994412016-12-02 00:28:42 -08001010 fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)))
1011 fcret = VERR_CR_CONN_ACC_LEN;
James Smartf77fc872017-03-23 20:41:25 -07001012 else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST))
James Smarte3994412016-12-02 00:28:42 -08001013 fcret = VERR_LSDESC_RQST;
1014 else if (conn_acc->hdr.rqst.desc_len !=
1015 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1016 fcret = VERR_LSDESC_RQST_LEN;
1017 else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION)
1018 fcret = VERR_CR_CONN;
1019 else if (conn_acc->connectid.desc_tag !=
1020 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1021 fcret = VERR_CONN_ID;
1022 else if (conn_acc->connectid.desc_len !=
1023 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1024 fcret = VERR_CONN_ID_LEN;
1025
1026 if (fcret) {
1027 ret = -EBADF;
1028 dev_err(ctrl->dev,
1029 "q %d connect failed: %s\n",
1030 queue->qnum, validation_errors[fcret]);
1031 } else {
1032 queue->connection_id =
1033 be64_to_cpu(conn_acc->connectid.connection_id);
1034 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1035 }
1036
1037out_free_buffer:
1038 kfree(lsop);
1039out_no_memory:
1040 if (ret)
1041 dev_err(ctrl->dev,
1042 "queue %d connect command failed (%d).\n",
1043 queue->qnum, ret);
1044 return ret;
1045}
1046
1047static void
1048nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
1049{
1050 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
James Smarte3994412016-12-02 00:28:42 -08001051
James Smartc913a8b2017-04-11 11:35:08 -07001052 __nvme_fc_finish_ls_req(lsop);
James Smarte3994412016-12-02 00:28:42 -08001053
1054 /* fc-nvme iniator doesn't care about success or failure of cmd */
1055
1056 kfree(lsop);
1057}
1058
1059/*
1060 * This routine sends a FC-NVME LS to disconnect (aka terminate)
1061 * the FC-NVME Association. Terminating the association also
1062 * terminates the FC-NVME connections (per queue, both admin and io
1063 * queues) that are part of the association. E.g. things are torn
1064 * down, and the related FC-NVME Association ID and Connection IDs
1065 * become invalid.
1066 *
1067 * The behavior of the fc-nvme initiator is such that it's
1068 * understanding of the association and connections will implicitly
1069 * be torn down. The action is implicit as it may be due to a loss of
1070 * connectivity with the fc-nvme target, so you may never get a
1071 * response even if you tried. As such, the action of this routine
1072 * is to asynchronously send the LS, ignore any results of the LS, and
1073 * continue on with terminating the association. If the fc-nvme target
1074 * is present and receives the LS, it too can tear down.
1075 */
1076static void
1077nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl)
1078{
1079 struct fcnvme_ls_disconnect_rqst *discon_rqst;
1080 struct fcnvme_ls_disconnect_acc *discon_acc;
1081 struct nvmefc_ls_req_op *lsop;
1082 struct nvmefc_ls_req *lsreq;
James Smartc913a8b2017-04-11 11:35:08 -07001083 int ret;
James Smarte3994412016-12-02 00:28:42 -08001084
1085 lsop = kzalloc((sizeof(*lsop) +
1086 ctrl->lport->ops->lsrqst_priv_sz +
1087 sizeof(*discon_rqst) + sizeof(*discon_acc)),
1088 GFP_KERNEL);
1089 if (!lsop)
1090 /* couldn't sent it... too bad */
1091 return;
1092
1093 lsreq = &lsop->ls_req;
1094
1095 lsreq->private = (void *)&lsop[1];
1096 discon_rqst = (struct fcnvme_ls_disconnect_rqst *)
1097 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1098 discon_acc = (struct fcnvme_ls_disconnect_acc *)&discon_rqst[1];
1099
1100 discon_rqst->w0.ls_cmd = FCNVME_LS_DISCONNECT;
1101 discon_rqst->desc_list_len = cpu_to_be32(
1102 sizeof(struct fcnvme_lsdesc_assoc_id) +
1103 sizeof(struct fcnvme_lsdesc_disconn_cmd));
1104
1105 discon_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1106 discon_rqst->associd.desc_len =
1107 fcnvme_lsdesc_len(
1108 sizeof(struct fcnvme_lsdesc_assoc_id));
1109
1110 discon_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1111
1112 discon_rqst->discon_cmd.desc_tag = cpu_to_be32(
1113 FCNVME_LSDESC_DISCONN_CMD);
1114 discon_rqst->discon_cmd.desc_len =
1115 fcnvme_lsdesc_len(
1116 sizeof(struct fcnvme_lsdesc_disconn_cmd));
1117 discon_rqst->discon_cmd.scope = FCNVME_DISCONN_ASSOCIATION;
1118 discon_rqst->discon_cmd.id = cpu_to_be64(ctrl->association_id);
1119
1120 lsreq->rqstaddr = discon_rqst;
1121 lsreq->rqstlen = sizeof(*discon_rqst);
1122 lsreq->rspaddr = discon_acc;
1123 lsreq->rsplen = sizeof(*discon_acc);
1124 lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
1125
James Smartc913a8b2017-04-11 11:35:08 -07001126 ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop,
1127 nvme_fc_disconnect_assoc_done);
1128 if (ret)
1129 kfree(lsop);
James Smarte3994412016-12-02 00:28:42 -08001130
1131 /* only meaningful part to terminating the association */
1132 ctrl->association_id = 0;
1133}
1134
1135
1136/* *********************** NVME Ctrl Routines **************************** */
1137
James Smart78a7ac22017-04-23 08:30:07 -07001138static void __nvme_fc_final_op_cleanup(struct request *rq);
James Smartf874d5d2017-06-01 22:54:21 -07001139static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg);
James Smarte3994412016-12-02 00:28:42 -08001140
1141static int
1142nvme_fc_reinit_request(void *data, struct request *rq)
1143{
1144 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1145 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1146
1147 memset(cmdiu, 0, sizeof(*cmdiu));
1148 cmdiu->scsi_id = NVME_CMD_SCSI_ID;
1149 cmdiu->fc_id = NVME_CMD_FC_ID;
1150 cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
1151 memset(&op->rsp_iu, 0, sizeof(op->rsp_iu));
1152
1153 return 0;
1154}
1155
1156static void
1157__nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl,
1158 struct nvme_fc_fcp_op *op)
1159{
1160 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma,
1161 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1162 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
1163 sizeof(op->cmd_iu), DMA_TO_DEVICE);
1164
1165 atomic_set(&op->state, FCPOP_STATE_UNINIT);
1166}
1167
1168static void
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001169nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1170 unsigned int hctx_idx)
James Smarte3994412016-12-02 00:28:42 -08001171{
1172 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1173
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001174 return __nvme_fc_exit_request(set->driver_data, op);
James Smarte3994412016-12-02 00:28:42 -08001175}
1176
James Smart78a7ac22017-04-23 08:30:07 -07001177static int
1178__nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
1179{
1180 int state;
1181
1182 state = atomic_xchg(&op->state, FCPOP_STATE_ABORTED);
1183 if (state != FCPOP_STATE_ACTIVE) {
1184 atomic_set(&op->state, state);
1185 return -ECANCELED;
1186 }
1187
1188 ctrl->lport->ops->fcp_abort(&ctrl->lport->localport,
1189 &ctrl->rport->remoteport,
1190 op->queue->lldd_handle,
1191 &op->fcp_req);
1192
1193 return 0;
1194}
1195
James Smarte3994412016-12-02 00:28:42 -08001196static void
James Smart78a7ac22017-04-23 08:30:07 -07001197nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl)
James Smarte3994412016-12-02 00:28:42 -08001198{
1199 struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops;
James Smart78a7ac22017-04-23 08:30:07 -07001200 unsigned long flags;
1201 int i, ret;
James Smarte3994412016-12-02 00:28:42 -08001202
1203 for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
James Smart78a7ac22017-04-23 08:30:07 -07001204 if (atomic_read(&aen_op->state) != FCPOP_STATE_ACTIVE)
James Smarte3994412016-12-02 00:28:42 -08001205 continue;
James Smart78a7ac22017-04-23 08:30:07 -07001206
1207 spin_lock_irqsave(&ctrl->lock, flags);
James Smart61bff8e2017-04-23 08:30:08 -07001208 if (ctrl->flags & FCCTRL_TERMIO) {
1209 ctrl->iocnt++;
1210 aen_op->flags |= FCOP_FLAGS_TERMIO;
1211 }
James Smart78a7ac22017-04-23 08:30:07 -07001212 spin_unlock_irqrestore(&ctrl->lock, flags);
1213
1214 ret = __nvme_fc_abort_op(ctrl, aen_op);
1215 if (ret) {
1216 /*
1217 * if __nvme_fc_abort_op failed the io wasn't
1218 * active. Thus this call path is running in
1219 * parallel to the io complete. Treat as non-error.
1220 */
1221
1222 /* back out the flags/counters */
1223 spin_lock_irqsave(&ctrl->lock, flags);
James Smart61bff8e2017-04-23 08:30:08 -07001224 if (ctrl->flags & FCCTRL_TERMIO)
1225 ctrl->iocnt--;
James Smart78a7ac22017-04-23 08:30:07 -07001226 aen_op->flags &= ~FCOP_FLAGS_TERMIO;
1227 spin_unlock_irqrestore(&ctrl->lock, flags);
1228 return;
1229 }
James Smarte3994412016-12-02 00:28:42 -08001230 }
1231}
1232
James Smart78a7ac22017-04-23 08:30:07 -07001233static inline int
1234__nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
1235 struct nvme_fc_fcp_op *op)
1236{
1237 unsigned long flags;
1238 bool complete_rq = false;
1239
1240 spin_lock_irqsave(&ctrl->lock, flags);
James Smart61bff8e2017-04-23 08:30:08 -07001241 if (unlikely(op->flags & FCOP_FLAGS_TERMIO)) {
1242 if (ctrl->flags & FCCTRL_TERMIO)
1243 ctrl->iocnt--;
1244 }
James Smart78a7ac22017-04-23 08:30:07 -07001245 if (op->flags & FCOP_FLAGS_RELEASED)
1246 complete_rq = true;
1247 else
1248 op->flags |= FCOP_FLAGS_COMPLETE;
1249 spin_unlock_irqrestore(&ctrl->lock, flags);
1250
1251 return complete_rq;
1252}
1253
Christoph Hellwigbaee29a2017-04-21 10:44:06 +02001254static void
James Smarte3994412016-12-02 00:28:42 -08001255nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
1256{
1257 struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1258 struct request *rq = op->rq;
1259 struct nvmefc_fcp_req *freq = &op->fcp_req;
1260 struct nvme_fc_ctrl *ctrl = op->ctrl;
1261 struct nvme_fc_queue *queue = op->queue;
1262 struct nvme_completion *cqe = &op->rsp_iu.cqe;
James Smart458f2802017-04-23 08:30:06 -07001263 struct nvme_command *sqe = &op->cmd_iu.sqe;
Christoph Hellwigd663b692017-04-20 16:02:56 +02001264 __le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001265 union nvme_result result;
James Smartf874d5d2017-06-01 22:54:21 -07001266 bool complete_rq, terminate_assoc = true;
James Smarte3994412016-12-02 00:28:42 -08001267
1268 /*
1269 * WARNING:
1270 * The current linux implementation of a nvme controller
1271 * allocates a single tag set for all io queues and sizes
1272 * the io queues to fully hold all possible tags. Thus, the
1273 * implementation does not reference or care about the sqhd
1274 * value as it never needs to use the sqhd/sqtail pointers
1275 * for submission pacing.
1276 *
1277 * This affects the FC-NVME implementation in two ways:
1278 * 1) As the value doesn't matter, we don't need to waste
1279 * cycles extracting it from ERSPs and stamping it in the
1280 * cases where the transport fabricates CQEs on successful
1281 * completions.
1282 * 2) The FC-NVME implementation requires that delivery of
1283 * ERSP completions are to go back to the nvme layer in order
1284 * relative to the rsn, such that the sqhd value will always
1285 * be "in order" for the nvme layer. As the nvme layer in
1286 * linux doesn't care about sqhd, there's no need to return
1287 * them in order.
1288 *
1289 * Additionally:
1290 * As the core nvme layer in linux currently does not look at
1291 * every field in the cqe - in cases where the FC transport must
1292 * fabricate a CQE, the following fields will not be set as they
1293 * are not referenced:
1294 * cqe.sqid, cqe.sqhd, cqe.command_id
James Smartf874d5d2017-06-01 22:54:21 -07001295 *
1296 * Failure or error of an individual i/o, in a transport
1297 * detected fashion unrelated to the nvme completion status,
1298 * potentially cause the initiator and target sides to get out
1299 * of sync on SQ head/tail (aka outstanding io count allowed).
1300 * Per FC-NVME spec, failure of an individual command requires
1301 * the connection to be terminated, which in turn requires the
1302 * association to be terminated.
James Smarte3994412016-12-02 00:28:42 -08001303 */
1304
1305 fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
1306 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1307
1308 if (atomic_read(&op->state) == FCPOP_STATE_ABORTED)
Christoph Hellwigd663b692017-04-20 16:02:56 +02001309 status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
James Smart62eeacb2017-03-23 20:41:27 -07001310 else if (freq->status)
Christoph Hellwigd663b692017-04-20 16:02:56 +02001311 status = cpu_to_le16(NVME_SC_FC_TRANSPORT_ERROR << 1);
James Smarte3994412016-12-02 00:28:42 -08001312
1313 /*
1314 * For the linux implementation, if we have an unsuccesful
1315 * status, they blk-mq layer can typically be called with the
1316 * non-zero status and the content of the cqe isn't important.
1317 */
1318 if (status)
1319 goto done;
1320
1321 /*
1322 * command completed successfully relative to the wire
1323 * protocol. However, validate anything received and
1324 * extract the status and result from the cqe (create it
1325 * where necessary).
1326 */
1327
1328 switch (freq->rcv_rsplen) {
1329
1330 case 0:
1331 case NVME_FC_SIZEOF_ZEROS_RSP:
1332 /*
1333 * No response payload or 12 bytes of payload (which
1334 * should all be zeros) are considered successful and
1335 * no payload in the CQE by the transport.
1336 */
1337 if (freq->transferred_length !=
1338 be32_to_cpu(op->cmd_iu.data_len)) {
Christoph Hellwigd663b692017-04-20 16:02:56 +02001339 status = cpu_to_le16(NVME_SC_FC_TRANSPORT_ERROR << 1);
James Smarte3994412016-12-02 00:28:42 -08001340 goto done;
1341 }
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001342 result.u64 = 0;
James Smarte3994412016-12-02 00:28:42 -08001343 break;
1344
1345 case sizeof(struct nvme_fc_ersp_iu):
1346 /*
1347 * The ERSP IU contains a full completion with CQE.
1348 * Validate ERSP IU and look at cqe.
1349 */
1350 if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) !=
1351 (freq->rcv_rsplen / 4) ||
1352 be32_to_cpu(op->rsp_iu.xfrd_len) !=
1353 freq->transferred_length ||
James Smart726a1082017-03-23 20:41:23 -07001354 op->rsp_iu.status_code ||
James Smart458f2802017-04-23 08:30:06 -07001355 sqe->common.command_id != cqe->command_id)) {
Christoph Hellwigd663b692017-04-20 16:02:56 +02001356 status = cpu_to_le16(NVME_SC_FC_TRANSPORT_ERROR << 1);
James Smarte3994412016-12-02 00:28:42 -08001357 goto done;
1358 }
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001359 result = cqe->result;
Christoph Hellwigd663b692017-04-20 16:02:56 +02001360 status = cqe->status;
James Smarte3994412016-12-02 00:28:42 -08001361 break;
1362
1363 default:
Christoph Hellwigd663b692017-04-20 16:02:56 +02001364 status = cpu_to_le16(NVME_SC_FC_TRANSPORT_ERROR << 1);
James Smarte3994412016-12-02 00:28:42 -08001365 goto done;
1366 }
1367
James Smartf874d5d2017-06-01 22:54:21 -07001368 terminate_assoc = false;
1369
James Smarte3994412016-12-02 00:28:42 -08001370done:
James Smart78a7ac22017-04-23 08:30:07 -07001371 if (op->flags & FCOP_FLAGS_AEN) {
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001372 nvme_complete_async_event(&queue->ctrl->ctrl, status, &result);
James Smart78a7ac22017-04-23 08:30:07 -07001373 complete_rq = __nvme_fc_fcpop_chk_teardowns(ctrl, op);
1374 atomic_set(&op->state, FCPOP_STATE_IDLE);
1375 op->flags = FCOP_FLAGS_AEN; /* clear other flags */
James Smarte3994412016-12-02 00:28:42 -08001376 nvme_fc_ctrl_put(ctrl);
James Smartf874d5d2017-06-01 22:54:21 -07001377 goto check_error;
James Smarte3994412016-12-02 00:28:42 -08001378 }
1379
James Smart78a7ac22017-04-23 08:30:07 -07001380 complete_rq = __nvme_fc_fcpop_chk_teardowns(ctrl, op);
1381 if (!complete_rq) {
1382 if (unlikely(op->flags & FCOP_FLAGS_TERMIO)) {
James Smarte392e1f2017-05-15 17:10:24 -07001383 status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
James Smart78a7ac22017-04-23 08:30:07 -07001384 if (blk_queue_dying(rq->q))
James Smarte392e1f2017-05-15 17:10:24 -07001385 status |= cpu_to_le16(NVME_SC_DNR << 1);
James Smart78a7ac22017-04-23 08:30:07 -07001386 }
1387 nvme_end_request(rq, status, result);
1388 } else
1389 __nvme_fc_final_op_cleanup(rq);
James Smartf874d5d2017-06-01 22:54:21 -07001390
1391check_error:
1392 if (terminate_assoc)
1393 nvme_fc_error_recovery(ctrl, "transport detected io error");
James Smarte3994412016-12-02 00:28:42 -08001394}
1395
1396static int
1397__nvme_fc_init_request(struct nvme_fc_ctrl *ctrl,
1398 struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op,
1399 struct request *rq, u32 rqno)
1400{
1401 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1402 int ret = 0;
1403
1404 memset(op, 0, sizeof(*op));
1405 op->fcp_req.cmdaddr = &op->cmd_iu;
1406 op->fcp_req.cmdlen = sizeof(op->cmd_iu);
1407 op->fcp_req.rspaddr = &op->rsp_iu;
1408 op->fcp_req.rsplen = sizeof(op->rsp_iu);
1409 op->fcp_req.done = nvme_fc_fcpio_done;
1410 op->fcp_req.first_sgl = (struct scatterlist *)&op[1];
1411 op->fcp_req.private = &op->fcp_req.first_sgl[SG_CHUNK_SIZE];
1412 op->ctrl = ctrl;
1413 op->queue = queue;
1414 op->rq = rq;
1415 op->rqno = rqno;
1416
1417 cmdiu->scsi_id = NVME_CMD_SCSI_ID;
1418 cmdiu->fc_id = NVME_CMD_FC_ID;
1419 cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
1420
1421 op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev,
1422 &op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE);
1423 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) {
1424 dev_err(ctrl->dev,
1425 "FCP Op failed - cmdiu dma mapping failed.\n");
1426 ret = EFAULT;
1427 goto out_on_error;
1428 }
1429
1430 op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev,
1431 &op->rsp_iu, sizeof(op->rsp_iu),
1432 DMA_FROM_DEVICE);
1433 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) {
1434 dev_err(ctrl->dev,
1435 "FCP Op failed - rspiu dma mapping failed.\n");
1436 ret = EFAULT;
1437 }
1438
1439 atomic_set(&op->state, FCPOP_STATE_IDLE);
1440out_on_error:
1441 return ret;
1442}
1443
1444static int
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001445nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq,
1446 unsigned int hctx_idx, unsigned int numa_node)
James Smarte3994412016-12-02 00:28:42 -08001447{
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001448 struct nvme_fc_ctrl *ctrl = set->driver_data;
James Smarte3994412016-12-02 00:28:42 -08001449 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
Christoph Hellwig76f983c2017-06-13 09:15:20 +02001450 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
1451 struct nvme_fc_queue *queue = &ctrl->queues[queue_idx];
James Smarte3994412016-12-02 00:28:42 -08001452
1453 return __nvme_fc_init_request(ctrl, queue, op, rq, queue->rqcnt++);
1454}
1455
1456static int
1457nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl)
1458{
1459 struct nvme_fc_fcp_op *aen_op;
1460 struct nvme_fc_cmd_iu *cmdiu;
1461 struct nvme_command *sqe;
James Smart61bff8e2017-04-23 08:30:08 -07001462 void *private;
James Smarte3994412016-12-02 00:28:42 -08001463 int i, ret;
1464
1465 aen_op = ctrl->aen_ops;
1466 for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
James Smart61bff8e2017-04-23 08:30:08 -07001467 private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz,
1468 GFP_KERNEL);
1469 if (!private)
1470 return -ENOMEM;
1471
James Smarte3994412016-12-02 00:28:42 -08001472 cmdiu = &aen_op->cmd_iu;
1473 sqe = &cmdiu->sqe;
1474 ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0],
1475 aen_op, (struct request *)NULL,
1476 (AEN_CMDID_BASE + i));
James Smart61bff8e2017-04-23 08:30:08 -07001477 if (ret) {
1478 kfree(private);
James Smarte3994412016-12-02 00:28:42 -08001479 return ret;
James Smart61bff8e2017-04-23 08:30:08 -07001480 }
James Smarte3994412016-12-02 00:28:42 -08001481
James Smart78a7ac22017-04-23 08:30:07 -07001482 aen_op->flags = FCOP_FLAGS_AEN;
James Smart61bff8e2017-04-23 08:30:08 -07001483 aen_op->fcp_req.first_sgl = NULL; /* no sg list */
1484 aen_op->fcp_req.private = private;
James Smart78a7ac22017-04-23 08:30:07 -07001485
James Smarte3994412016-12-02 00:28:42 -08001486 memset(sqe, 0, sizeof(*sqe));
1487 sqe->common.opcode = nvme_admin_async_event;
James Smart78a7ac22017-04-23 08:30:07 -07001488 /* Note: core layer may overwrite the sqe.command_id value */
James Smarte3994412016-12-02 00:28:42 -08001489 sqe->common.command_id = AEN_CMDID_BASE + i;
1490 }
1491 return 0;
1492}
1493
James Smart61bff8e2017-04-23 08:30:08 -07001494static void
1495nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl)
1496{
1497 struct nvme_fc_fcp_op *aen_op;
1498 int i;
1499
1500 aen_op = ctrl->aen_ops;
1501 for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
1502 if (!aen_op->fcp_req.private)
1503 continue;
1504
1505 __nvme_fc_exit_request(ctrl, aen_op);
1506
1507 kfree(aen_op->fcp_req.private);
1508 aen_op->fcp_req.private = NULL;
1509 }
1510}
James Smarte3994412016-12-02 00:28:42 -08001511
1512static inline void
1513__nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, struct nvme_fc_ctrl *ctrl,
1514 unsigned int qidx)
1515{
1516 struct nvme_fc_queue *queue = &ctrl->queues[qidx];
1517
1518 hctx->driver_data = queue;
1519 queue->hctx = hctx;
1520}
1521
1522static int
1523nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1524 unsigned int hctx_idx)
1525{
1526 struct nvme_fc_ctrl *ctrl = data;
1527
1528 __nvme_fc_init_hctx(hctx, ctrl, hctx_idx + 1);
1529
1530 return 0;
1531}
1532
1533static int
1534nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1535 unsigned int hctx_idx)
1536{
1537 struct nvme_fc_ctrl *ctrl = data;
1538
1539 __nvme_fc_init_hctx(hctx, ctrl, hctx_idx);
1540
1541 return 0;
1542}
1543
1544static void
1545nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx, size_t queue_size)
1546{
1547 struct nvme_fc_queue *queue;
1548
1549 queue = &ctrl->queues[idx];
1550 memset(queue, 0, sizeof(*queue));
1551 queue->ctrl = ctrl;
1552 queue->qnum = idx;
1553 atomic_set(&queue->csn, 1);
1554 queue->dev = ctrl->dev;
1555
1556 if (idx > 0)
1557 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
1558 else
1559 queue->cmnd_capsule_len = sizeof(struct nvme_command);
1560
1561 queue->queue_size = queue_size;
1562
1563 /*
1564 * Considered whether we should allocate buffers for all SQEs
1565 * and CQEs and dma map them - mapping their respective entries
1566 * into the request structures (kernel vm addr and dma address)
1567 * thus the driver could use the buffers/mappings directly.
1568 * It only makes sense if the LLDD would use them for its
1569 * messaging api. It's very unlikely most adapter api's would use
1570 * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
1571 * structures were used instead.
1572 */
1573}
1574
1575/*
1576 * This routine terminates a queue at the transport level.
1577 * The transport has already ensured that all outstanding ios on
1578 * the queue have been terminated.
1579 * The transport will send a Disconnect LS request to terminate
1580 * the queue's connection. Termination of the admin queue will also
1581 * terminate the association at the target.
1582 */
1583static void
1584nvme_fc_free_queue(struct nvme_fc_queue *queue)
1585{
1586 if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags))
1587 return;
1588
1589 /*
1590 * Current implementation never disconnects a single queue.
1591 * It always terminates a whole association. So there is never
1592 * a disconnect(queue) LS sent to the target.
1593 */
1594
1595 queue->connection_id = 0;
1596 clear_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1597}
1598
1599static void
1600__nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl,
1601 struct nvme_fc_queue *queue, unsigned int qidx)
1602{
1603 if (ctrl->lport->ops->delete_queue)
1604 ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx,
1605 queue->lldd_handle);
1606 queue->lldd_handle = NULL;
1607}
1608
1609static void
James Smarte3994412016-12-02 00:28:42 -08001610nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl)
1611{
1612 int i;
1613
1614 for (i = 1; i < ctrl->queue_count; i++)
1615 nvme_fc_free_queue(&ctrl->queues[i]);
1616}
1617
1618static int
1619__nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl,
1620 struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize)
1621{
1622 int ret = 0;
1623
1624 queue->lldd_handle = NULL;
1625 if (ctrl->lport->ops->create_queue)
1626 ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport,
1627 qidx, qsize, &queue->lldd_handle);
1628
1629 return ret;
1630}
1631
1632static void
1633nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl)
1634{
1635 struct nvme_fc_queue *queue = &ctrl->queues[ctrl->queue_count - 1];
1636 int i;
1637
1638 for (i = ctrl->queue_count - 1; i >= 1; i--, queue--)
1639 __nvme_fc_delete_hw_queue(ctrl, queue, i);
1640}
1641
1642static int
1643nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
1644{
1645 struct nvme_fc_queue *queue = &ctrl->queues[1];
Johannes Thumshirn17a1ec02016-12-15 14:20:48 +01001646 int i, ret;
James Smarte3994412016-12-02 00:28:42 -08001647
1648 for (i = 1; i < ctrl->queue_count; i++, queue++) {
1649 ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize);
Johannes Thumshirn17a1ec02016-12-15 14:20:48 +01001650 if (ret)
1651 goto delete_queues;
James Smarte3994412016-12-02 00:28:42 -08001652 }
1653
1654 return 0;
Johannes Thumshirn17a1ec02016-12-15 14:20:48 +01001655
1656delete_queues:
1657 for (; i >= 0; i--)
1658 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i);
1659 return ret;
James Smarte3994412016-12-02 00:28:42 -08001660}
1661
1662static int
1663nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
1664{
1665 int i, ret = 0;
1666
1667 for (i = 1; i < ctrl->queue_count; i++) {
1668 ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize,
1669 (qsize / 5));
1670 if (ret)
1671 break;
1672 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
1673 if (ret)
1674 break;
1675 }
1676
1677 return ret;
1678}
1679
1680static void
1681nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl)
1682{
1683 int i;
1684
1685 for (i = 1; i < ctrl->queue_count; i++)
1686 nvme_fc_init_queue(ctrl, i, ctrl->ctrl.sqsize);
1687}
1688
1689static void
1690nvme_fc_ctrl_free(struct kref *ref)
1691{
1692 struct nvme_fc_ctrl *ctrl =
1693 container_of(ref, struct nvme_fc_ctrl, ref);
1694 unsigned long flags;
1695
James Smart61bff8e2017-04-23 08:30:08 -07001696 if (ctrl->ctrl.tagset) {
1697 blk_cleanup_queue(ctrl->ctrl.connect_q);
1698 blk_mq_free_tag_set(&ctrl->tag_set);
James Smarte3994412016-12-02 00:28:42 -08001699 }
1700
James Smart61bff8e2017-04-23 08:30:08 -07001701 /* remove from rport list */
1702 spin_lock_irqsave(&ctrl->rport->lock, flags);
1703 list_del(&ctrl->ctrl_list);
1704 spin_unlock_irqrestore(&ctrl->rport->lock, flags);
1705
1706 blk_cleanup_queue(ctrl->ctrl.admin_q);
1707 blk_mq_free_tag_set(&ctrl->admin_tag_set);
1708
1709 kfree(ctrl->queues);
1710
James Smarte3994412016-12-02 00:28:42 -08001711 put_device(ctrl->dev);
1712 nvme_fc_rport_put(ctrl->rport);
1713
James Smarte3994412016-12-02 00:28:42 -08001714 ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
Ewan D. Milnede414472017-04-24 13:24:16 -04001715 if (ctrl->ctrl.opts)
1716 nvmf_free_options(ctrl->ctrl.opts);
James Smarte3994412016-12-02 00:28:42 -08001717 kfree(ctrl);
1718}
1719
1720static void
1721nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl)
1722{
1723 kref_put(&ctrl->ref, nvme_fc_ctrl_free);
1724}
1725
1726static int
1727nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
1728{
1729 return kref_get_unless_zero(&ctrl->ref);
1730}
1731
1732/*
1733 * All accesses from nvme core layer done - can now free the
1734 * controller. Called after last nvme_put_ctrl() call
1735 */
1736static void
James Smart61bff8e2017-04-23 08:30:08 -07001737nvme_fc_nvme_ctrl_freed(struct nvme_ctrl *nctrl)
James Smarte3994412016-12-02 00:28:42 -08001738{
1739 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
1740
1741 WARN_ON(nctrl != &ctrl->ctrl);
1742
James Smart61bff8e2017-04-23 08:30:08 -07001743 nvme_fc_ctrl_put(ctrl);
1744}
James Smarte3994412016-12-02 00:28:42 -08001745
James Smart61bff8e2017-04-23 08:30:08 -07001746static void
1747nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
1748{
1749 dev_warn(ctrl->ctrl.device,
1750 "NVME-FC{%d}: transport association error detected: %s\n",
1751 ctrl->cnum, errmsg);
James Smart589ff772017-05-15 17:10:19 -07001752 dev_warn(ctrl->ctrl.device,
James Smart61bff8e2017-04-23 08:30:08 -07001753 "NVME-FC{%d}: resetting controller\n", ctrl->cnum);
James Smarte3994412016-12-02 00:28:42 -08001754
James Smart2952a872017-04-25 15:32:01 -07001755 /* stop the queues on error, cleanup is in reset thread */
1756 if (ctrl->queue_count > 1)
1757 nvme_stop_queues(&ctrl->ctrl);
1758
James Smart61bff8e2017-04-23 08:30:08 -07001759 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING)) {
1760 dev_err(ctrl->ctrl.device,
1761 "NVME-FC{%d}: error_recovery: Couldn't change state "
1762 "to RECONNECTING\n", ctrl->cnum);
1763 return;
James Smarte3994412016-12-02 00:28:42 -08001764 }
1765
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001766 nvme_reset_ctrl(&ctrl->ctrl);
James Smarte3994412016-12-02 00:28:42 -08001767}
1768
Christoph Hellwigbaee29a2017-04-21 10:44:06 +02001769static enum blk_eh_timer_return
James Smarte3994412016-12-02 00:28:42 -08001770nvme_fc_timeout(struct request *rq, bool reserved)
1771{
1772 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1773 struct nvme_fc_ctrl *ctrl = op->ctrl;
1774 int ret;
1775
1776 if (reserved)
1777 return BLK_EH_RESET_TIMER;
1778
1779 ret = __nvme_fc_abort_op(ctrl, op);
1780 if (ret)
1781 /* io wasn't active to abort consider it done */
1782 return BLK_EH_HANDLED;
1783
1784 /*
James Smart61bff8e2017-04-23 08:30:08 -07001785 * we can't individually ABTS an io without affecting the queue,
1786 * thus killing the queue, adn thus the association.
1787 * So resolve by performing a controller reset, which will stop
1788 * the host/io stack, terminate the association on the link,
1789 * and recreate an association on the link.
James Smarte3994412016-12-02 00:28:42 -08001790 */
James Smart61bff8e2017-04-23 08:30:08 -07001791 nvme_fc_error_recovery(ctrl, "io timeout error");
James Smarte3994412016-12-02 00:28:42 -08001792
1793 return BLK_EH_HANDLED;
1794}
1795
1796static int
1797nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
1798 struct nvme_fc_fcp_op *op)
1799{
1800 struct nvmefc_fcp_req *freq = &op->fcp_req;
James Smarte3994412016-12-02 00:28:42 -08001801 enum dma_data_direction dir;
1802 int ret;
1803
1804 freq->sg_cnt = 0;
1805
Christoph Hellwigb131c612017-01-13 12:29:12 +01001806 if (!blk_rq_payload_bytes(rq))
James Smarte3994412016-12-02 00:28:42 -08001807 return 0;
1808
1809 freq->sg_table.sgl = freq->first_sgl;
Christoph Hellwig19e420b2017-01-19 16:55:57 +01001810 ret = sg_alloc_table_chained(&freq->sg_table,
1811 blk_rq_nr_phys_segments(rq), freq->sg_table.sgl);
James Smarte3994412016-12-02 00:28:42 -08001812 if (ret)
1813 return -ENOMEM;
1814
1815 op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl);
Christoph Hellwig19e420b2017-01-19 16:55:57 +01001816 WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
James Smarte3994412016-12-02 00:28:42 -08001817 dir = (rq_data_dir(rq) == WRITE) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
1818 freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
1819 op->nents, dir);
1820 if (unlikely(freq->sg_cnt <= 0)) {
1821 sg_free_table_chained(&freq->sg_table, true);
1822 freq->sg_cnt = 0;
1823 return -EFAULT;
1824 }
1825
1826 /*
1827 * TODO: blk_integrity_rq(rq) for DIF
1828 */
1829 return 0;
1830}
1831
1832static void
1833nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
1834 struct nvme_fc_fcp_op *op)
1835{
1836 struct nvmefc_fcp_req *freq = &op->fcp_req;
1837
1838 if (!freq->sg_cnt)
1839 return;
1840
1841 fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
1842 ((rq_data_dir(rq) == WRITE) ?
1843 DMA_TO_DEVICE : DMA_FROM_DEVICE));
1844
1845 nvme_cleanup_cmd(rq);
1846
1847 sg_free_table_chained(&freq->sg_table, true);
1848
1849 freq->sg_cnt = 0;
1850}
1851
1852/*
1853 * In FC, the queue is a logical thing. At transport connect, the target
1854 * creates its "queue" and returns a handle that is to be given to the
1855 * target whenever it posts something to the corresponding SQ. When an
1856 * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
1857 * command contained within the SQE, an io, and assigns a FC exchange
1858 * to it. The SQE and the associated SQ handle are sent in the initial
1859 * CMD IU sents on the exchange. All transfers relative to the io occur
1860 * as part of the exchange. The CQE is the last thing for the io,
1861 * which is transferred (explicitly or implicitly) with the RSP IU
1862 * sent on the exchange. After the CQE is received, the FC exchange is
1863 * terminaed and the Exchange may be used on a different io.
1864 *
1865 * The transport to LLDD api has the transport making a request for a
1866 * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
1867 * resource and transfers the command. The LLDD will then process all
1868 * steps to complete the io. Upon completion, the transport done routine
1869 * is called.
1870 *
1871 * So - while the operation is outstanding to the LLDD, there is a link
1872 * level FC exchange resource that is also outstanding. This must be
1873 * considered in all cleanup operations.
1874 */
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001875static blk_status_t
James Smarte3994412016-12-02 00:28:42 -08001876nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
1877 struct nvme_fc_fcp_op *op, u32 data_len,
1878 enum nvmefc_fcp_datadir io_dir)
1879{
1880 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1881 struct nvme_command *sqe = &cmdiu->sqe;
1882 u32 csn;
1883 int ret;
1884
James Smart61bff8e2017-04-23 08:30:08 -07001885 /*
1886 * before attempting to send the io, check to see if we believe
1887 * the target device is present
1888 */
1889 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001890 return BLK_STS_IOERR;
James Smart61bff8e2017-04-23 08:30:08 -07001891
James Smarte3994412016-12-02 00:28:42 -08001892 if (!nvme_fc_ctrl_get(ctrl))
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001893 return BLK_STS_IOERR;
James Smarte3994412016-12-02 00:28:42 -08001894
1895 /* format the FC-NVME CMD IU and fcp_req */
1896 cmdiu->connection_id = cpu_to_be64(queue->connection_id);
1897 csn = atomic_inc_return(&queue->csn);
1898 cmdiu->csn = cpu_to_be32(csn);
1899 cmdiu->data_len = cpu_to_be32(data_len);
1900 switch (io_dir) {
1901 case NVMEFC_FCP_WRITE:
1902 cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
1903 break;
1904 case NVMEFC_FCP_READ:
1905 cmdiu->flags = FCNVME_CMD_FLAGS_READ;
1906 break;
1907 case NVMEFC_FCP_NODATA:
1908 cmdiu->flags = 0;
1909 break;
1910 }
1911 op->fcp_req.payload_length = data_len;
1912 op->fcp_req.io_dir = io_dir;
1913 op->fcp_req.transferred_length = 0;
1914 op->fcp_req.rcv_rsplen = 0;
James Smart62eeacb2017-03-23 20:41:27 -07001915 op->fcp_req.status = NVME_SC_SUCCESS;
James Smarte3994412016-12-02 00:28:42 -08001916 op->fcp_req.sqid = cpu_to_le16(queue->qnum);
1917
1918 /*
1919 * validate per fabric rules, set fields mandated by fabric spec
1920 * as well as those by FC-NVME spec.
1921 */
1922 WARN_ON_ONCE(sqe->common.metadata);
1923 WARN_ON_ONCE(sqe->common.dptr.prp1);
1924 WARN_ON_ONCE(sqe->common.dptr.prp2);
1925 sqe->common.flags |= NVME_CMD_SGL_METABUF;
1926
1927 /*
1928 * format SQE DPTR field per FC-NVME rules
1929 * type=data block descr; subtype=offset;
1930 * offset is currently 0.
1931 */
1932 sqe->rw.dptr.sgl.type = NVME_SGL_FMT_OFFSET;
1933 sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
1934 sqe->rw.dptr.sgl.addr = 0;
1935
James Smart78a7ac22017-04-23 08:30:07 -07001936 if (!(op->flags & FCOP_FLAGS_AEN)) {
James Smarte3994412016-12-02 00:28:42 -08001937 ret = nvme_fc_map_data(ctrl, op->rq, op);
1938 if (ret < 0) {
James Smarte3994412016-12-02 00:28:42 -08001939 nvme_cleanup_cmd(op->rq);
1940 nvme_fc_ctrl_put(ctrl);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001941 if (ret == -ENOMEM || ret == -EAGAIN)
1942 return BLK_STS_RESOURCE;
1943 return BLK_STS_IOERR;
James Smarte3994412016-12-02 00:28:42 -08001944 }
1945 }
1946
1947 fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
1948 sizeof(op->cmd_iu), DMA_TO_DEVICE);
1949
1950 atomic_set(&op->state, FCPOP_STATE_ACTIVE);
1951
James Smart78a7ac22017-04-23 08:30:07 -07001952 if (!(op->flags & FCOP_FLAGS_AEN))
James Smarte3994412016-12-02 00:28:42 -08001953 blk_mq_start_request(op->rq);
1954
1955 ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
1956 &ctrl->rport->remoteport,
1957 queue->lldd_handle, &op->fcp_req);
1958
1959 if (ret) {
James Smarte3994412016-12-02 00:28:42 -08001960 if (op->rq) { /* normal request */
1961 nvme_fc_unmap_data(ctrl, op->rq, op);
1962 nvme_cleanup_cmd(op->rq);
1963 }
1964 /* else - aen. no cleanup needed */
1965
1966 nvme_fc_ctrl_put(ctrl);
1967
1968 if (ret != -EBUSY)
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001969 return BLK_STS_IOERR;
James Smarte3994412016-12-02 00:28:42 -08001970
1971 if (op->rq) {
1972 blk_mq_stop_hw_queues(op->rq->q);
1973 blk_mq_delay_queue(queue->hctx, NVMEFC_QUEUE_DELAY);
1974 }
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001975 return BLK_STS_RESOURCE;
James Smarte3994412016-12-02 00:28:42 -08001976 }
1977
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001978 return BLK_STS_OK;
James Smarte3994412016-12-02 00:28:42 -08001979}
1980
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001981static blk_status_t
James Smarte3994412016-12-02 00:28:42 -08001982nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
1983 const struct blk_mq_queue_data *bd)
1984{
1985 struct nvme_ns *ns = hctx->queue->queuedata;
1986 struct nvme_fc_queue *queue = hctx->driver_data;
1987 struct nvme_fc_ctrl *ctrl = queue->ctrl;
1988 struct request *rq = bd->rq;
1989 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1990 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1991 struct nvme_command *sqe = &cmdiu->sqe;
1992 enum nvmefc_fcp_datadir io_dir;
1993 u32 data_len;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001994 blk_status_t ret;
James Smarte3994412016-12-02 00:28:42 -08001995
1996 ret = nvme_setup_cmd(ns, rq, sqe);
1997 if (ret)
1998 return ret;
1999
Christoph Hellwigb131c612017-01-13 12:29:12 +01002000 data_len = blk_rq_payload_bytes(rq);
James Smarte3994412016-12-02 00:28:42 -08002001 if (data_len)
2002 io_dir = ((rq_data_dir(rq) == WRITE) ?
2003 NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
2004 else
2005 io_dir = NVMEFC_FCP_NODATA;
2006
2007 return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
2008}
2009
2010static struct blk_mq_tags *
2011nvme_fc_tagset(struct nvme_fc_queue *queue)
2012{
2013 if (queue->qnum == 0)
2014 return queue->ctrl->admin_tag_set.tags[queue->qnum];
2015
2016 return queue->ctrl->tag_set.tags[queue->qnum - 1];
2017}
2018
2019static int
2020nvme_fc_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
2021
2022{
2023 struct nvme_fc_queue *queue = hctx->driver_data;
2024 struct nvme_fc_ctrl *ctrl = queue->ctrl;
2025 struct request *req;
2026 struct nvme_fc_fcp_op *op;
2027
2028 req = blk_mq_tag_to_rq(nvme_fc_tagset(queue), tag);
James Smart61bff8e2017-04-23 08:30:08 -07002029 if (!req)
James Smarte3994412016-12-02 00:28:42 -08002030 return 0;
James Smarte3994412016-12-02 00:28:42 -08002031
2032 op = blk_mq_rq_to_pdu(req);
2033
2034 if ((atomic_read(&op->state) == FCPOP_STATE_ACTIVE) &&
2035 (ctrl->lport->ops->poll_queue))
2036 ctrl->lport->ops->poll_queue(&ctrl->lport->localport,
2037 queue->lldd_handle);
2038
2039 return ((atomic_read(&op->state) != FCPOP_STATE_ACTIVE));
2040}
2041
2042static void
2043nvme_fc_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
2044{
2045 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
2046 struct nvme_fc_fcp_op *aen_op;
James Smart61bff8e2017-04-23 08:30:08 -07002047 unsigned long flags;
2048 bool terminating = false;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002049 blk_status_t ret;
James Smarte3994412016-12-02 00:28:42 -08002050
2051 if (aer_idx > NVME_FC_NR_AEN_COMMANDS)
2052 return;
2053
James Smart61bff8e2017-04-23 08:30:08 -07002054 spin_lock_irqsave(&ctrl->lock, flags);
2055 if (ctrl->flags & FCCTRL_TERMIO)
2056 terminating = true;
2057 spin_unlock_irqrestore(&ctrl->lock, flags);
2058
2059 if (terminating)
2060 return;
2061
James Smarte3994412016-12-02 00:28:42 -08002062 aen_op = &ctrl->aen_ops[aer_idx];
2063
2064 ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
2065 NVMEFC_FCP_NODATA);
2066 if (ret)
2067 dev_err(ctrl->ctrl.device,
2068 "failed async event work [%d]\n", aer_idx);
2069}
2070
2071static void
James Smart78a7ac22017-04-23 08:30:07 -07002072__nvme_fc_final_op_cleanup(struct request *rq)
James Smarte3994412016-12-02 00:28:42 -08002073{
2074 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2075 struct nvme_fc_ctrl *ctrl = op->ctrl;
James Smarte3994412016-12-02 00:28:42 -08002076
James Smart78a7ac22017-04-23 08:30:07 -07002077 atomic_set(&op->state, FCPOP_STATE_IDLE);
2078 op->flags &= ~(FCOP_FLAGS_TERMIO | FCOP_FLAGS_RELEASED |
2079 FCOP_FLAGS_COMPLETE);
James Smarte3994412016-12-02 00:28:42 -08002080
2081 nvme_cleanup_cmd(rq);
James Smarte3994412016-12-02 00:28:42 -08002082 nvme_fc_unmap_data(ctrl, rq, op);
Christoph Hellwig77f02a72017-03-30 13:41:32 +02002083 nvme_complete_rq(rq);
James Smarte3994412016-12-02 00:28:42 -08002084 nvme_fc_ctrl_put(ctrl);
2085
James Smarte3994412016-12-02 00:28:42 -08002086}
2087
James Smart78a7ac22017-04-23 08:30:07 -07002088static void
2089nvme_fc_complete_rq(struct request *rq)
2090{
2091 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2092 struct nvme_fc_ctrl *ctrl = op->ctrl;
2093 unsigned long flags;
2094 bool completed = false;
2095
2096 /*
2097 * the core layer, on controller resets after calling
2098 * nvme_shutdown_ctrl(), calls complete_rq without our
2099 * calling blk_mq_complete_request(), thus there may still
2100 * be live i/o outstanding with the LLDD. Means transport has
2101 * to track complete calls vs fcpio_done calls to know what
2102 * path to take on completes and dones.
2103 */
2104 spin_lock_irqsave(&ctrl->lock, flags);
2105 if (op->flags & FCOP_FLAGS_COMPLETE)
2106 completed = true;
2107 else
2108 op->flags |= FCOP_FLAGS_RELEASED;
2109 spin_unlock_irqrestore(&ctrl->lock, flags);
2110
2111 if (completed)
2112 __nvme_fc_final_op_cleanup(rq);
2113}
2114
James Smarte3994412016-12-02 00:28:42 -08002115/*
2116 * This routine is used by the transport when it needs to find active
2117 * io on a queue that is to be terminated. The transport uses
2118 * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
2119 * this routine to kill them on a 1 by 1 basis.
2120 *
2121 * As FC allocates FC exchange for each io, the transport must contact
2122 * the LLDD to terminate the exchange, thus releasing the FC exchange.
2123 * After terminating the exchange the LLDD will call the transport's
2124 * normal io done path for the request, but it will have an aborted
2125 * status. The done path will return the io request back to the block
2126 * layer with an error status.
2127 */
2128static void
2129nvme_fc_terminate_exchange(struct request *req, void *data, bool reserved)
2130{
2131 struct nvme_ctrl *nctrl = data;
2132 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2133 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
James Smart78a7ac22017-04-23 08:30:07 -07002134 unsigned long flags;
2135 int status;
James Smarte3994412016-12-02 00:28:42 -08002136
2137 if (!blk_mq_request_started(req))
2138 return;
2139
James Smart78a7ac22017-04-23 08:30:07 -07002140 spin_lock_irqsave(&ctrl->lock, flags);
James Smart61bff8e2017-04-23 08:30:08 -07002141 if (ctrl->flags & FCCTRL_TERMIO) {
2142 ctrl->iocnt++;
2143 op->flags |= FCOP_FLAGS_TERMIO;
2144 }
James Smart78a7ac22017-04-23 08:30:07 -07002145 spin_unlock_irqrestore(&ctrl->lock, flags);
James Smarte3994412016-12-02 00:28:42 -08002146
James Smart78a7ac22017-04-23 08:30:07 -07002147 status = __nvme_fc_abort_op(ctrl, op);
2148 if (status) {
2149 /*
2150 * if __nvme_fc_abort_op failed the io wasn't
2151 * active. Thus this call path is running in
2152 * parallel to the io complete. Treat as non-error.
2153 */
2154
2155 /* back out the flags/counters */
2156 spin_lock_irqsave(&ctrl->lock, flags);
James Smart61bff8e2017-04-23 08:30:08 -07002157 if (ctrl->flags & FCCTRL_TERMIO)
2158 ctrl->iocnt--;
James Smart78a7ac22017-04-23 08:30:07 -07002159 op->flags &= ~FCOP_FLAGS_TERMIO;
2160 spin_unlock_irqrestore(&ctrl->lock, flags);
2161 return;
2162 }
2163}
James Smarte3994412016-12-02 00:28:42 -08002164
James Smarte3994412016-12-02 00:28:42 -08002165
James Smart61bff8e2017-04-23 08:30:08 -07002166static const struct blk_mq_ops nvme_fc_mq_ops = {
2167 .queue_rq = nvme_fc_queue_rq,
2168 .complete = nvme_fc_complete_rq,
2169 .init_request = nvme_fc_init_request,
2170 .exit_request = nvme_fc_exit_request,
2171 .reinit_request = nvme_fc_reinit_request,
2172 .init_hctx = nvme_fc_init_hctx,
2173 .poll = nvme_fc_poll,
2174 .timeout = nvme_fc_timeout,
James Smarte3994412016-12-02 00:28:42 -08002175};
2176
2177static int
2178nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl)
2179{
2180 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2181 int ret;
2182
2183 ret = nvme_set_queue_count(&ctrl->ctrl, &opts->nr_io_queues);
2184 if (ret) {
2185 dev_info(ctrl->ctrl.device,
2186 "set_queue_count failed: %d\n", ret);
2187 return ret;
2188 }
2189
2190 ctrl->queue_count = opts->nr_io_queues + 1;
2191 if (!opts->nr_io_queues)
2192 return 0;
2193
James Smarte3994412016-12-02 00:28:42 -08002194 nvme_fc_init_io_queues(ctrl);
2195
2196 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
2197 ctrl->tag_set.ops = &nvme_fc_mq_ops;
2198 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
2199 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
2200 ctrl->tag_set.numa_node = NUMA_NO_NODE;
2201 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
2202 ctrl->tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) +
2203 (SG_CHUNK_SIZE *
2204 sizeof(struct scatterlist)) +
2205 ctrl->lport->ops->fcprqst_priv_sz;
2206 ctrl->tag_set.driver_data = ctrl;
2207 ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1;
2208 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
2209
2210 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
2211 if (ret)
2212 return ret;
2213
2214 ctrl->ctrl.tagset = &ctrl->tag_set;
2215
2216 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
2217 if (IS_ERR(ctrl->ctrl.connect_q)) {
2218 ret = PTR_ERR(ctrl->ctrl.connect_q);
2219 goto out_free_tag_set;
2220 }
2221
2222 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
2223 if (ret)
2224 goto out_cleanup_blk_queue;
2225
2226 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
2227 if (ret)
2228 goto out_delete_hw_queues;
2229
2230 return 0;
2231
2232out_delete_hw_queues:
2233 nvme_fc_delete_hw_io_queues(ctrl);
2234out_cleanup_blk_queue:
2235 nvme_stop_keep_alive(&ctrl->ctrl);
2236 blk_cleanup_queue(ctrl->ctrl.connect_q);
2237out_free_tag_set:
2238 blk_mq_free_tag_set(&ctrl->tag_set);
2239 nvme_fc_free_io_queues(ctrl);
2240
2241 /* force put free routine to ignore io queues */
2242 ctrl->ctrl.tagset = NULL;
2243
2244 return ret;
2245}
2246
James Smart61bff8e2017-04-23 08:30:08 -07002247static int
2248nvme_fc_reinit_io_queues(struct nvme_fc_ctrl *ctrl)
2249{
2250 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2251 int ret;
2252
2253 ret = nvme_set_queue_count(&ctrl->ctrl, &opts->nr_io_queues);
2254 if (ret) {
2255 dev_info(ctrl->ctrl.device,
2256 "set_queue_count failed: %d\n", ret);
2257 return ret;
2258 }
2259
2260 /* check for io queues existing */
2261 if (ctrl->queue_count == 1)
2262 return 0;
2263
James Smart61bff8e2017-04-23 08:30:08 -07002264 nvme_fc_init_io_queues(ctrl);
2265
2266 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
2267 if (ret)
2268 goto out_free_io_queues;
2269
2270 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
2271 if (ret)
2272 goto out_free_io_queues;
2273
2274 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
2275 if (ret)
2276 goto out_delete_hw_queues;
2277
2278 return 0;
2279
2280out_delete_hw_queues:
2281 nvme_fc_delete_hw_io_queues(ctrl);
2282out_free_io_queues:
2283 nvme_fc_free_io_queues(ctrl);
2284 return ret;
2285}
2286
2287/*
2288 * This routine restarts the controller on the host side, and
2289 * on the link side, recreates the controller association.
2290 */
2291static int
2292nvme_fc_create_association(struct nvme_fc_ctrl *ctrl)
2293{
2294 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2295 u32 segs;
2296 int ret;
2297 bool changed;
2298
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +03002299 ++ctrl->ctrl.nr_reconnects;
James Smart61bff8e2017-04-23 08:30:08 -07002300
2301 /*
2302 * Create the admin queue
2303 */
2304
2305 nvme_fc_init_queue(ctrl, 0, NVME_FC_AQ_BLKMQ_DEPTH);
2306
2307 ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0,
2308 NVME_FC_AQ_BLKMQ_DEPTH);
2309 if (ret)
2310 goto out_free_queue;
2311
2312 ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0],
2313 NVME_FC_AQ_BLKMQ_DEPTH,
2314 (NVME_FC_AQ_BLKMQ_DEPTH / 4));
2315 if (ret)
2316 goto out_delete_hw_queue;
2317
2318 if (ctrl->ctrl.state != NVME_CTRL_NEW)
2319 blk_mq_start_stopped_hw_queues(ctrl->ctrl.admin_q, true);
2320
2321 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
2322 if (ret)
2323 goto out_disconnect_admin_queue;
2324
2325 /*
2326 * Check controller capabilities
2327 *
2328 * todo:- add code to check if ctrl attributes changed from
2329 * prior connection values
2330 */
2331
2332 ret = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap);
2333 if (ret) {
2334 dev_err(ctrl->ctrl.device,
2335 "prop_get NVME_REG_CAP failed\n");
2336 goto out_disconnect_admin_queue;
2337 }
2338
2339 ctrl->ctrl.sqsize =
2340 min_t(int, NVME_CAP_MQES(ctrl->cap) + 1, ctrl->ctrl.sqsize);
2341
2342 ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
2343 if (ret)
2344 goto out_disconnect_admin_queue;
2345
2346 segs = min_t(u32, NVME_FC_MAX_SEGMENTS,
2347 ctrl->lport->ops->max_sgl_segments);
2348 ctrl->ctrl.max_hw_sectors = (segs - 1) << (PAGE_SHIFT - 9);
2349
2350 ret = nvme_init_identify(&ctrl->ctrl);
2351 if (ret)
2352 goto out_disconnect_admin_queue;
2353
2354 /* sanity checks */
2355
2356 /* FC-NVME does not have other data in the capsule */
2357 if (ctrl->ctrl.icdoff) {
2358 dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n",
2359 ctrl->ctrl.icdoff);
2360 goto out_disconnect_admin_queue;
2361 }
2362
2363 nvme_start_keep_alive(&ctrl->ctrl);
2364
2365 /* FC-NVME supports normal SGL Data Block Descriptors */
2366
2367 if (opts->queue_size > ctrl->ctrl.maxcmd) {
2368 /* warn if maxcmd is lower than queue_size */
2369 dev_warn(ctrl->ctrl.device,
2370 "queue_size %zu > ctrl maxcmd %u, reducing "
2371 "to queue_size\n",
2372 opts->queue_size, ctrl->ctrl.maxcmd);
2373 opts->queue_size = ctrl->ctrl.maxcmd;
2374 }
2375
2376 ret = nvme_fc_init_aen_ops(ctrl);
2377 if (ret)
2378 goto out_term_aen_ops;
2379
2380 /*
2381 * Create the io queues
2382 */
2383
2384 if (ctrl->queue_count > 1) {
2385 if (ctrl->ctrl.state == NVME_CTRL_NEW)
2386 ret = nvme_fc_create_io_queues(ctrl);
2387 else
2388 ret = nvme_fc_reinit_io_queues(ctrl);
2389 if (ret)
2390 goto out_term_aen_ops;
2391 }
2392
2393 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
2394 WARN_ON_ONCE(!changed);
2395
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +03002396 ctrl->ctrl.nr_reconnects = 0;
James Smart61bff8e2017-04-23 08:30:08 -07002397
James Smart61bff8e2017-04-23 08:30:08 -07002398 if (ctrl->queue_count > 1) {
2399 nvme_start_queues(&ctrl->ctrl);
2400 nvme_queue_scan(&ctrl->ctrl);
2401 nvme_queue_async_events(&ctrl->ctrl);
2402 }
2403
2404 return 0; /* Success */
2405
2406out_term_aen_ops:
2407 nvme_fc_term_aen_ops(ctrl);
2408 nvme_stop_keep_alive(&ctrl->ctrl);
2409out_disconnect_admin_queue:
2410 /* send a Disconnect(association) LS to fc-nvme target */
2411 nvme_fc_xmt_disconnect_assoc(ctrl);
2412out_delete_hw_queue:
2413 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
2414out_free_queue:
2415 nvme_fc_free_queue(&ctrl->queues[0]);
2416
2417 return ret;
2418}
2419
2420/*
2421 * This routine stops operation of the controller on the host side.
2422 * On the host os stack side: Admin and IO queues are stopped,
2423 * outstanding ios on them terminated via FC ABTS.
2424 * On the link side: the association is terminated.
2425 */
2426static void
2427nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
2428{
2429 unsigned long flags;
2430
2431 nvme_stop_keep_alive(&ctrl->ctrl);
2432
2433 spin_lock_irqsave(&ctrl->lock, flags);
2434 ctrl->flags |= FCCTRL_TERMIO;
2435 ctrl->iocnt = 0;
2436 spin_unlock_irqrestore(&ctrl->lock, flags);
2437
2438 /*
2439 * If io queues are present, stop them and terminate all outstanding
2440 * ios on them. As FC allocates FC exchange for each io, the
2441 * transport must contact the LLDD to terminate the exchange,
2442 * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
2443 * to tell us what io's are busy and invoke a transport routine
2444 * to kill them with the LLDD. After terminating the exchange
2445 * the LLDD will call the transport's normal io done path, but it
2446 * will have an aborted status. The done path will return the
2447 * io requests back to the block layer as part of normal completions
2448 * (but with error status).
2449 */
2450 if (ctrl->queue_count > 1) {
2451 nvme_stop_queues(&ctrl->ctrl);
2452 blk_mq_tagset_busy_iter(&ctrl->tag_set,
2453 nvme_fc_terminate_exchange, &ctrl->ctrl);
2454 }
2455
2456 /*
2457 * Other transports, which don't have link-level contexts bound
2458 * to sqe's, would try to gracefully shutdown the controller by
2459 * writing the registers for shutdown and polling (call
2460 * nvme_shutdown_ctrl()). Given a bunch of i/o was potentially
2461 * just aborted and we will wait on those contexts, and given
2462 * there was no indication of how live the controlelr is on the
2463 * link, don't send more io to create more contexts for the
2464 * shutdown. Let the controller fail via keepalive failure if
2465 * its still present.
2466 */
2467
2468 /*
2469 * clean up the admin queue. Same thing as above.
2470 * use blk_mq_tagset_busy_itr() and the transport routine to
2471 * terminate the exchanges.
2472 */
2473 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
2474 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
2475 nvme_fc_terminate_exchange, &ctrl->ctrl);
2476
2477 /* kill the aens as they are a separate path */
2478 nvme_fc_abort_aen_ops(ctrl);
2479
2480 /* wait for all io that had to be aborted */
2481 spin_lock_irqsave(&ctrl->lock, flags);
2482 while (ctrl->iocnt) {
2483 spin_unlock_irqrestore(&ctrl->lock, flags);
2484 msleep(1000);
2485 spin_lock_irqsave(&ctrl->lock, flags);
2486 }
2487 ctrl->flags &= ~FCCTRL_TERMIO;
2488 spin_unlock_irqrestore(&ctrl->lock, flags);
2489
2490 nvme_fc_term_aen_ops(ctrl);
2491
2492 /*
2493 * send a Disconnect(association) LS to fc-nvme target
2494 * Note: could have been sent at top of process, but
2495 * cleaner on link traffic if after the aborts complete.
2496 * Note: if association doesn't exist, association_id will be 0
2497 */
2498 if (ctrl->association_id)
2499 nvme_fc_xmt_disconnect_assoc(ctrl);
2500
2501 if (ctrl->ctrl.tagset) {
2502 nvme_fc_delete_hw_io_queues(ctrl);
2503 nvme_fc_free_io_queues(ctrl);
2504 }
2505
2506 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
2507 nvme_fc_free_queue(&ctrl->queues[0]);
2508}
2509
2510static void
2511nvme_fc_delete_ctrl_work(struct work_struct *work)
2512{
2513 struct nvme_fc_ctrl *ctrl =
2514 container_of(work, struct nvme_fc_ctrl, delete_work);
2515
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002516 cancel_work_sync(&ctrl->ctrl.reset_work);
James Smart61bff8e2017-04-23 08:30:08 -07002517 cancel_delayed_work_sync(&ctrl->connect_work);
2518
2519 /*
2520 * kill the association on the link side. this will block
2521 * waiting for io to terminate
2522 */
2523 nvme_fc_delete_association(ctrl);
2524
2525 /*
2526 * tear down the controller
James Smarta5321aa2017-05-15 17:10:18 -07002527 * After the last reference on the nvme ctrl is removed,
2528 * the transport nvme_fc_nvme_ctrl_freed() callback will be
2529 * invoked. From there, the transport will tear down it's
2530 * logical queues and association.
James Smart61bff8e2017-04-23 08:30:08 -07002531 */
2532 nvme_uninit_ctrl(&ctrl->ctrl);
2533
2534 nvme_put_ctrl(&ctrl->ctrl);
2535}
2536
James Smart5bbecdb2017-05-15 17:10:16 -07002537static bool
2538__nvme_fc_schedule_delete_work(struct nvme_fc_ctrl *ctrl)
2539{
2540 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
2541 return true;
2542
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002543 if (!queue_work(nvme_wq, &ctrl->delete_work))
James Smart5bbecdb2017-05-15 17:10:16 -07002544 return true;
2545
2546 return false;
2547}
2548
James Smart61bff8e2017-04-23 08:30:08 -07002549static int
2550__nvme_fc_del_ctrl(struct nvme_fc_ctrl *ctrl)
2551{
James Smart5bbecdb2017-05-15 17:10:16 -07002552 return __nvme_fc_schedule_delete_work(ctrl) ? -EBUSY : 0;
James Smart61bff8e2017-04-23 08:30:08 -07002553}
2554
2555/*
2556 * Request from nvme core layer to delete the controller
2557 */
2558static int
2559nvme_fc_del_nvme_ctrl(struct nvme_ctrl *nctrl)
2560{
2561 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2562 int ret;
2563
2564 if (!kref_get_unless_zero(&ctrl->ctrl.kref))
2565 return -EBUSY;
2566
2567 ret = __nvme_fc_del_ctrl(ctrl);
2568
2569 if (!ret)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002570 flush_workqueue(nvme_wq);
James Smart61bff8e2017-04-23 08:30:08 -07002571
2572 nvme_put_ctrl(&ctrl->ctrl);
2573
2574 return ret;
2575}
2576
2577static void
James Smart5bbecdb2017-05-15 17:10:16 -07002578nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status)
2579{
2580 /* If we are resetting/deleting then do nothing */
2581 if (ctrl->ctrl.state != NVME_CTRL_RECONNECTING) {
2582 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
2583 ctrl->ctrl.state == NVME_CTRL_LIVE);
2584 return;
2585 }
2586
James Smart589ff772017-05-15 17:10:19 -07002587 dev_info(ctrl->ctrl.device,
James Smart5bbecdb2017-05-15 17:10:16 -07002588 "NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
2589 ctrl->cnum, status);
2590
2591 if (nvmf_should_reconnect(&ctrl->ctrl)) {
2592 dev_info(ctrl->ctrl.device,
2593 "NVME-FC{%d}: Reconnect attempt in %d seconds.\n",
2594 ctrl->cnum, ctrl->ctrl.opts->reconnect_delay);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002595 queue_delayed_work(nvme_wq, &ctrl->connect_work,
James Smart5bbecdb2017-05-15 17:10:16 -07002596 ctrl->ctrl.opts->reconnect_delay * HZ);
2597 } else {
James Smart589ff772017-05-15 17:10:19 -07002598 dev_warn(ctrl->ctrl.device,
James Smart5bbecdb2017-05-15 17:10:16 -07002599 "NVME-FC{%d}: Max reconnect attempts (%d) "
2600 "reached. Removing controller\n",
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +03002601 ctrl->cnum, ctrl->ctrl.nr_reconnects);
James Smart5bbecdb2017-05-15 17:10:16 -07002602 WARN_ON(__nvme_fc_schedule_delete_work(ctrl));
2603 }
2604}
2605
2606static void
James Smart61bff8e2017-04-23 08:30:08 -07002607nvme_fc_reset_ctrl_work(struct work_struct *work)
2608{
2609 struct nvme_fc_ctrl *ctrl =
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002610 container_of(work, struct nvme_fc_ctrl, ctrl.reset_work);
James Smart61bff8e2017-04-23 08:30:08 -07002611 int ret;
2612
2613 /* will block will waiting for io to terminate */
2614 nvme_fc_delete_association(ctrl);
2615
2616 ret = nvme_fc_create_association(ctrl);
James Smart5bbecdb2017-05-15 17:10:16 -07002617 if (ret)
2618 nvme_fc_reconnect_or_delete(ctrl, ret);
2619 else
James Smart61bff8e2017-04-23 08:30:08 -07002620 dev_info(ctrl->ctrl.device,
2621 "NVME-FC{%d}: controller reset complete\n", ctrl->cnum);
2622}
2623
James Smart61bff8e2017-04-23 08:30:08 -07002624static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
2625 .name = "fc",
2626 .module = THIS_MODULE,
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02002627 .flags = NVME_F_FABRICS,
James Smart61bff8e2017-04-23 08:30:08 -07002628 .reg_read32 = nvmf_reg_read32,
2629 .reg_read64 = nvmf_reg_read64,
2630 .reg_write32 = nvmf_reg_write32,
James Smart61bff8e2017-04-23 08:30:08 -07002631 .free_ctrl = nvme_fc_nvme_ctrl_freed,
2632 .submit_async_event = nvme_fc_submit_async_event,
2633 .delete_ctrl = nvme_fc_del_nvme_ctrl,
James Smart61bff8e2017-04-23 08:30:08 -07002634 .get_address = nvmf_get_address,
2635};
2636
2637static void
2638nvme_fc_connect_ctrl_work(struct work_struct *work)
2639{
2640 int ret;
2641
2642 struct nvme_fc_ctrl *ctrl =
2643 container_of(to_delayed_work(work),
2644 struct nvme_fc_ctrl, connect_work);
2645
2646 ret = nvme_fc_create_association(ctrl);
James Smart5bbecdb2017-05-15 17:10:16 -07002647 if (ret)
2648 nvme_fc_reconnect_or_delete(ctrl, ret);
2649 else
James Smart61bff8e2017-04-23 08:30:08 -07002650 dev_info(ctrl->ctrl.device,
2651 "NVME-FC{%d}: controller reconnect complete\n",
2652 ctrl->cnum);
2653}
2654
2655
2656static const struct blk_mq_ops nvme_fc_admin_mq_ops = {
2657 .queue_rq = nvme_fc_queue_rq,
2658 .complete = nvme_fc_complete_rq,
Christoph Hellwig76f983c2017-06-13 09:15:20 +02002659 .init_request = nvme_fc_init_request,
James Smart61bff8e2017-04-23 08:30:08 -07002660 .exit_request = nvme_fc_exit_request,
2661 .reinit_request = nvme_fc_reinit_request,
2662 .init_hctx = nvme_fc_init_admin_hctx,
2663 .timeout = nvme_fc_timeout,
2664};
2665
James Smarte3994412016-12-02 00:28:42 -08002666
2667static struct nvme_ctrl *
James Smart61bff8e2017-04-23 08:30:08 -07002668nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
James Smarte3994412016-12-02 00:28:42 -08002669 struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
2670{
2671 struct nvme_fc_ctrl *ctrl;
2672 unsigned long flags;
2673 int ret, idx;
James Smarte3994412016-12-02 00:28:42 -08002674
James Smart85e6a6a2017-05-05 16:13:15 -07002675 if (!(rport->remoteport.port_role &
2676 (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) {
2677 ret = -EBADR;
2678 goto out_fail;
2679 }
2680
James Smarte3994412016-12-02 00:28:42 -08002681 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
2682 if (!ctrl) {
2683 ret = -ENOMEM;
2684 goto out_fail;
2685 }
2686
2687 idx = ida_simple_get(&nvme_fc_ctrl_cnt, 0, 0, GFP_KERNEL);
2688 if (idx < 0) {
2689 ret = -ENOSPC;
2690 goto out_free_ctrl;
2691 }
2692
2693 ctrl->ctrl.opts = opts;
2694 INIT_LIST_HEAD(&ctrl->ctrl_list);
James Smarte3994412016-12-02 00:28:42 -08002695 ctrl->lport = lport;
2696 ctrl->rport = rport;
2697 ctrl->dev = lport->dev;
James Smarte3994412016-12-02 00:28:42 -08002698 ctrl->cnum = idx;
2699
James Smarte3994412016-12-02 00:28:42 -08002700 get_device(ctrl->dev);
2701 kref_init(&ctrl->ref);
2702
James Smart61bff8e2017-04-23 08:30:08 -07002703 INIT_WORK(&ctrl->delete_work, nvme_fc_delete_ctrl_work);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002704 INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work);
James Smart61bff8e2017-04-23 08:30:08 -07002705 INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
James Smarte3994412016-12-02 00:28:42 -08002706 spin_lock_init(&ctrl->lock);
2707
2708 /* io queue count */
2709 ctrl->queue_count = min_t(unsigned int,
2710 opts->nr_io_queues,
2711 lport->ops->max_hw_queues);
2712 opts->nr_io_queues = ctrl->queue_count; /* so opts has valid value */
2713 ctrl->queue_count++; /* +1 for admin queue */
2714
2715 ctrl->ctrl.sqsize = opts->queue_size - 1;
2716 ctrl->ctrl.kato = opts->kato;
2717
2718 ret = -ENOMEM;
2719 ctrl->queues = kcalloc(ctrl->queue_count, sizeof(struct nvme_fc_queue),
2720 GFP_KERNEL);
2721 if (!ctrl->queues)
James Smart61bff8e2017-04-23 08:30:08 -07002722 goto out_free_ida;
James Smarte3994412016-12-02 00:28:42 -08002723
James Smart61bff8e2017-04-23 08:30:08 -07002724 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
2725 ctrl->admin_tag_set.ops = &nvme_fc_admin_mq_ops;
2726 ctrl->admin_tag_set.queue_depth = NVME_FC_AQ_BLKMQ_DEPTH;
2727 ctrl->admin_tag_set.reserved_tags = 2; /* fabric connect + Keep-Alive */
2728 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
2729 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) +
2730 (SG_CHUNK_SIZE *
2731 sizeof(struct scatterlist)) +
2732 ctrl->lport->ops->fcprqst_priv_sz;
2733 ctrl->admin_tag_set.driver_data = ctrl;
2734 ctrl->admin_tag_set.nr_hw_queues = 1;
2735 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
2736
2737 ret = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
James Smarte3994412016-12-02 00:28:42 -08002738 if (ret)
James Smart61bff8e2017-04-23 08:30:08 -07002739 goto out_free_queues;
James Smarte3994412016-12-02 00:28:42 -08002740
James Smart61bff8e2017-04-23 08:30:08 -07002741 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
2742 if (IS_ERR(ctrl->ctrl.admin_q)) {
2743 ret = PTR_ERR(ctrl->ctrl.admin_q);
2744 goto out_free_admin_tag_set;
James Smarte3994412016-12-02 00:28:42 -08002745 }
2746
James Smart61bff8e2017-04-23 08:30:08 -07002747 /*
2748 * Would have been nice to init io queues tag set as well.
2749 * However, we require interaction from the controller
2750 * for max io queue count before we can do so.
2751 * Defer this to the connect path.
2752 */
James Smarte3994412016-12-02 00:28:42 -08002753
James Smart61bff8e2017-04-23 08:30:08 -07002754 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
James Smarte3994412016-12-02 00:28:42 -08002755 if (ret)
James Smart61bff8e2017-04-23 08:30:08 -07002756 goto out_cleanup_admin_q;
James Smarte3994412016-12-02 00:28:42 -08002757
James Smart61bff8e2017-04-23 08:30:08 -07002758 /* at this point, teardown path changes to ref counting on nvme ctrl */
James Smarte3994412016-12-02 00:28:42 -08002759
2760 spin_lock_irqsave(&rport->lock, flags);
2761 list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
2762 spin_unlock_irqrestore(&rport->lock, flags);
2763
James Smart61bff8e2017-04-23 08:30:08 -07002764 ret = nvme_fc_create_association(ctrl);
2765 if (ret) {
Ewan D. Milnede414472017-04-24 13:24:16 -04002766 ctrl->ctrl.opts = NULL;
James Smart61bff8e2017-04-23 08:30:08 -07002767 /* initiate nvme ctrl ref counting teardown */
2768 nvme_uninit_ctrl(&ctrl->ctrl);
James Smart24b7f052017-06-05 15:03:42 -07002769 nvme_put_ctrl(&ctrl->ctrl);
James Smart61bff8e2017-04-23 08:30:08 -07002770
2771 /* as we're past the point where we transition to the ref
2772 * counting teardown path, if we return a bad pointer here,
2773 * the calling routine, thinking it's prior to the
2774 * transition, will do an rport put. Since the teardown
2775 * path also does a rport put, we do an extra get here to
2776 * so proper order/teardown happens.
2777 */
2778 nvme_fc_rport_get(rport);
2779
2780 if (ret > 0)
2781 ret = -EIO;
2782 return ERR_PTR(ret);
James Smarte3994412016-12-02 00:28:42 -08002783 }
2784
James Smart2cb657b2017-05-15 17:10:22 -07002785 kref_get(&ctrl->ctrl.kref);
2786
James Smart61bff8e2017-04-23 08:30:08 -07002787 dev_info(ctrl->ctrl.device,
2788 "NVME-FC{%d}: new ctrl: NQN \"%s\"\n",
2789 ctrl->cnum, ctrl->ctrl.opts->subsysnqn);
2790
James Smarte3994412016-12-02 00:28:42 -08002791 return &ctrl->ctrl;
2792
James Smart61bff8e2017-04-23 08:30:08 -07002793out_cleanup_admin_q:
2794 blk_cleanup_queue(ctrl->ctrl.admin_q);
2795out_free_admin_tag_set:
2796 blk_mq_free_tag_set(&ctrl->admin_tag_set);
2797out_free_queues:
2798 kfree(ctrl->queues);
James Smarte3994412016-12-02 00:28:42 -08002799out_free_ida:
James Smart61bff8e2017-04-23 08:30:08 -07002800 put_device(ctrl->dev);
James Smarte3994412016-12-02 00:28:42 -08002801 ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
2802out_free_ctrl:
2803 kfree(ctrl);
2804out_fail:
James Smarte3994412016-12-02 00:28:42 -08002805 /* exit via here doesn't follow ctlr ref points */
2806 return ERR_PTR(ret);
2807}
2808
2809enum {
2810 FCT_TRADDR_ERR = 0,
2811 FCT_TRADDR_WWNN = 1 << 0,
2812 FCT_TRADDR_WWPN = 1 << 1,
2813};
2814
2815struct nvmet_fc_traddr {
2816 u64 nn;
2817 u64 pn;
2818};
2819
2820static const match_table_t traddr_opt_tokens = {
2821 { FCT_TRADDR_WWNN, "nn-%s" },
2822 { FCT_TRADDR_WWPN, "pn-%s" },
2823 { FCT_TRADDR_ERR, NULL }
2824};
2825
2826static int
2827nvme_fc_parse_address(struct nvmet_fc_traddr *traddr, char *buf)
2828{
2829 substring_t args[MAX_OPT_ARGS];
2830 char *options, *o, *p;
2831 int token, ret = 0;
2832 u64 token64;
2833
2834 options = o = kstrdup(buf, GFP_KERNEL);
2835 if (!options)
2836 return -ENOMEM;
2837
2838 while ((p = strsep(&o, ":\n")) != NULL) {
2839 if (!*p)
2840 continue;
2841
2842 token = match_token(p, traddr_opt_tokens, args);
2843 switch (token) {
2844 case FCT_TRADDR_WWNN:
2845 if (match_u64(args, &token64)) {
2846 ret = -EINVAL;
2847 goto out;
2848 }
2849 traddr->nn = token64;
2850 break;
2851 case FCT_TRADDR_WWPN:
2852 if (match_u64(args, &token64)) {
2853 ret = -EINVAL;
2854 goto out;
2855 }
2856 traddr->pn = token64;
2857 break;
2858 default:
2859 pr_warn("unknown traddr token or missing value '%s'\n",
2860 p);
2861 ret = -EINVAL;
2862 goto out;
2863 }
2864 }
2865
2866out:
2867 kfree(options);
2868 return ret;
2869}
2870
2871static struct nvme_ctrl *
2872nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
2873{
2874 struct nvme_fc_lport *lport;
2875 struct nvme_fc_rport *rport;
James Smart61bff8e2017-04-23 08:30:08 -07002876 struct nvme_ctrl *ctrl;
James Smarte3994412016-12-02 00:28:42 -08002877 struct nvmet_fc_traddr laddr = { 0L, 0L };
2878 struct nvmet_fc_traddr raddr = { 0L, 0L };
2879 unsigned long flags;
2880 int ret;
2881
2882 ret = nvme_fc_parse_address(&raddr, opts->traddr);
2883 if (ret || !raddr.nn || !raddr.pn)
2884 return ERR_PTR(-EINVAL);
2885
2886 ret = nvme_fc_parse_address(&laddr, opts->host_traddr);
2887 if (ret || !laddr.nn || !laddr.pn)
2888 return ERR_PTR(-EINVAL);
2889
2890 /* find the host and remote ports to connect together */
2891 spin_lock_irqsave(&nvme_fc_lock, flags);
2892 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
2893 if (lport->localport.node_name != laddr.nn ||
2894 lport->localport.port_name != laddr.pn)
2895 continue;
2896
2897 list_for_each_entry(rport, &lport->endp_list, endp_list) {
2898 if (rport->remoteport.node_name != raddr.nn ||
2899 rport->remoteport.port_name != raddr.pn)
2900 continue;
2901
2902 /* if fail to get reference fall through. Will error */
2903 if (!nvme_fc_rport_get(rport))
2904 break;
2905
2906 spin_unlock_irqrestore(&nvme_fc_lock, flags);
2907
James Smart61bff8e2017-04-23 08:30:08 -07002908 ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport);
2909 if (IS_ERR(ctrl))
2910 nvme_fc_rport_put(rport);
2911 return ctrl;
James Smarte3994412016-12-02 00:28:42 -08002912 }
2913 }
2914 spin_unlock_irqrestore(&nvme_fc_lock, flags);
2915
2916 return ERR_PTR(-ENOENT);
2917}
2918
2919
2920static struct nvmf_transport_ops nvme_fc_transport = {
2921 .name = "fc",
2922 .required_opts = NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
James Smart5bbecdb2017-05-15 17:10:16 -07002923 .allowed_opts = NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO,
James Smarte3994412016-12-02 00:28:42 -08002924 .create_ctrl = nvme_fc_create_ctrl,
2925};
2926
2927static int __init nvme_fc_init_module(void)
2928{
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002929 return nvmf_register_transport(&nvme_fc_transport);
James Smarte3994412016-12-02 00:28:42 -08002930}
2931
2932static void __exit nvme_fc_exit_module(void)
2933{
2934 /* sanity check - all lports should be removed */
2935 if (!list_empty(&nvme_fc_lport_list))
2936 pr_warn("%s: localport list not empty\n", __func__);
2937
2938 nvmf_unregister_transport(&nvme_fc_transport);
2939
James Smarte3994412016-12-02 00:28:42 -08002940 ida_destroy(&nvme_fc_local_port_cnt);
2941 ida_destroy(&nvme_fc_ctrl_cnt);
2942}
2943
2944module_init(nvme_fc_init_module);
2945module_exit(nvme_fc_exit_module);
2946
2947MODULE_LICENSE("GPL v2");