blob: cdd138c1f22330365ef960c6aa5cd17c97d81940 [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 Smart36715cf2017-05-22 15:28:42 -0700169 wait_queue_head_t ioabort_wait;
James Smarte3994412016-12-02 00:28:42 -0800170
171 struct nvme_fc_fcp_op aen_ops[NVME_FC_NR_AEN_COMMANDS];
172
173 struct nvme_ctrl ctrl;
174};
175
176static inline struct nvme_fc_ctrl *
177to_fc_ctrl(struct nvme_ctrl *ctrl)
178{
179 return container_of(ctrl, struct nvme_fc_ctrl, ctrl);
180}
181
182static inline struct nvme_fc_lport *
183localport_to_lport(struct nvme_fc_local_port *portptr)
184{
185 return container_of(portptr, struct nvme_fc_lport, localport);
186}
187
188static inline struct nvme_fc_rport *
189remoteport_to_rport(struct nvme_fc_remote_port *portptr)
190{
191 return container_of(portptr, struct nvme_fc_rport, remoteport);
192}
193
194static inline struct nvmefc_ls_req_op *
195ls_req_to_lsop(struct nvmefc_ls_req *lsreq)
196{
197 return container_of(lsreq, struct nvmefc_ls_req_op, ls_req);
198}
199
200static inline struct nvme_fc_fcp_op *
201fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq)
202{
203 return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req);
204}
205
206
207
208/* *************************** Globals **************************** */
209
210
211static DEFINE_SPINLOCK(nvme_fc_lock);
212
213static LIST_HEAD(nvme_fc_lport_list);
214static DEFINE_IDA(nvme_fc_local_port_cnt);
215static DEFINE_IDA(nvme_fc_ctrl_cnt);
216
James Smarte3994412016-12-02 00:28:42 -0800217
218
219
220/* *********************** FC-NVME Port Management ************************ */
221
222static int __nvme_fc_del_ctrl(struct nvme_fc_ctrl *);
223static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *,
224 struct nvme_fc_queue *, unsigned int);
225
226
227/**
228 * nvme_fc_register_localport - transport entry point called by an
229 * LLDD to register the existence of a NVME
230 * host FC port.
231 * @pinfo: pointer to information about the port to be registered
232 * @template: LLDD entrypoints and operational parameters for the port
233 * @dev: physical hardware device node port corresponds to. Will be
234 * used for DMA mappings
235 * @lport_p: pointer to a local port pointer. Upon success, the routine
236 * will allocate a nvme_fc_local_port structure and place its
237 * address in the local port pointer. Upon failure, local port
238 * pointer will be set to 0.
239 *
240 * Returns:
241 * a completion status. Must be 0 upon success; a negative errno
242 * (ex: -ENXIO) upon failure.
243 */
244int
245nvme_fc_register_localport(struct nvme_fc_port_info *pinfo,
246 struct nvme_fc_port_template *template,
247 struct device *dev,
248 struct nvme_fc_local_port **portptr)
249{
250 struct nvme_fc_lport *newrec;
251 unsigned long flags;
252 int ret, idx;
253
254 if (!template->localport_delete || !template->remoteport_delete ||
255 !template->ls_req || !template->fcp_io ||
256 !template->ls_abort || !template->fcp_abort ||
257 !template->max_hw_queues || !template->max_sgl_segments ||
258 !template->max_dif_sgl_segments || !template->dma_boundary) {
259 ret = -EINVAL;
260 goto out_reghost_failed;
261 }
262
263 newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz),
264 GFP_KERNEL);
265 if (!newrec) {
266 ret = -ENOMEM;
267 goto out_reghost_failed;
268 }
269
270 idx = ida_simple_get(&nvme_fc_local_port_cnt, 0, 0, GFP_KERNEL);
271 if (idx < 0) {
272 ret = -ENOSPC;
273 goto out_fail_kfree;
274 }
275
276 if (!get_device(dev) && dev) {
277 ret = -ENODEV;
278 goto out_ida_put;
279 }
280
281 INIT_LIST_HEAD(&newrec->port_list);
282 INIT_LIST_HEAD(&newrec->endp_list);
283 kref_init(&newrec->ref);
284 newrec->ops = template;
285 newrec->dev = dev;
286 ida_init(&newrec->endp_cnt);
287 newrec->localport.private = &newrec[1];
288 newrec->localport.node_name = pinfo->node_name;
289 newrec->localport.port_name = pinfo->port_name;
290 newrec->localport.port_role = pinfo->port_role;
291 newrec->localport.port_id = pinfo->port_id;
292 newrec->localport.port_state = FC_OBJSTATE_ONLINE;
293 newrec->localport.port_num = idx;
294
295 spin_lock_irqsave(&nvme_fc_lock, flags);
296 list_add_tail(&newrec->port_list, &nvme_fc_lport_list);
297 spin_unlock_irqrestore(&nvme_fc_lock, flags);
298
299 if (dev)
300 dma_set_seg_boundary(dev, template->dma_boundary);
301
302 *portptr = &newrec->localport;
303 return 0;
304
305out_ida_put:
306 ida_simple_remove(&nvme_fc_local_port_cnt, idx);
307out_fail_kfree:
308 kfree(newrec);
309out_reghost_failed:
310 *portptr = NULL;
311
312 return ret;
313}
314EXPORT_SYMBOL_GPL(nvme_fc_register_localport);
315
316static void
317nvme_fc_free_lport(struct kref *ref)
318{
319 struct nvme_fc_lport *lport =
320 container_of(ref, struct nvme_fc_lport, ref);
321 unsigned long flags;
322
323 WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED);
324 WARN_ON(!list_empty(&lport->endp_list));
325
326 /* remove from transport list */
327 spin_lock_irqsave(&nvme_fc_lock, flags);
328 list_del(&lport->port_list);
329 spin_unlock_irqrestore(&nvme_fc_lock, flags);
330
331 /* let the LLDD know we've finished tearing it down */
332 lport->ops->localport_delete(&lport->localport);
333
334 ida_simple_remove(&nvme_fc_local_port_cnt, lport->localport.port_num);
335 ida_destroy(&lport->endp_cnt);
336
337 put_device(lport->dev);
338
339 kfree(lport);
340}
341
342static void
343nvme_fc_lport_put(struct nvme_fc_lport *lport)
344{
345 kref_put(&lport->ref, nvme_fc_free_lport);
346}
347
348static int
349nvme_fc_lport_get(struct nvme_fc_lport *lport)
350{
351 return kref_get_unless_zero(&lport->ref);
352}
353
354/**
355 * nvme_fc_unregister_localport - transport entry point called by an
356 * LLDD to deregister/remove a previously
357 * registered a NVME host FC port.
358 * @localport: pointer to the (registered) local port that is to be
359 * deregistered.
360 *
361 * Returns:
362 * a completion status. Must be 0 upon success; a negative errno
363 * (ex: -ENXIO) upon failure.
364 */
365int
366nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr)
367{
368 struct nvme_fc_lport *lport = localport_to_lport(portptr);
369 unsigned long flags;
370
371 if (!portptr)
372 return -EINVAL;
373
374 spin_lock_irqsave(&nvme_fc_lock, flags);
375
376 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
377 spin_unlock_irqrestore(&nvme_fc_lock, flags);
378 return -EINVAL;
379 }
380 portptr->port_state = FC_OBJSTATE_DELETED;
381
382 spin_unlock_irqrestore(&nvme_fc_lock, flags);
383
384 nvme_fc_lport_put(lport);
385
386 return 0;
387}
388EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport);
389
390/**
391 * nvme_fc_register_remoteport - transport entry point called by an
392 * LLDD to register the existence of a NVME
393 * subsystem FC port on its fabric.
394 * @localport: pointer to the (registered) local port that the remote
395 * subsystem port is connected to.
396 * @pinfo: pointer to information about the port to be registered
397 * @rport_p: pointer to a remote port pointer. Upon success, the routine
398 * will allocate a nvme_fc_remote_port structure and place its
399 * address in the remote port pointer. Upon failure, remote port
400 * pointer will be set to 0.
401 *
402 * Returns:
403 * a completion status. Must be 0 upon success; a negative errno
404 * (ex: -ENXIO) upon failure.
405 */
406int
407nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
408 struct nvme_fc_port_info *pinfo,
409 struct nvme_fc_remote_port **portptr)
410{
411 struct nvme_fc_lport *lport = localport_to_lport(localport);
412 struct nvme_fc_rport *newrec;
413 unsigned long flags;
414 int ret, idx;
415
416 newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz),
417 GFP_KERNEL);
418 if (!newrec) {
419 ret = -ENOMEM;
420 goto out_reghost_failed;
421 }
422
423 if (!nvme_fc_lport_get(lport)) {
424 ret = -ESHUTDOWN;
425 goto out_kfree_rport;
426 }
427
428 idx = ida_simple_get(&lport->endp_cnt, 0, 0, GFP_KERNEL);
429 if (idx < 0) {
430 ret = -ENOSPC;
431 goto out_lport_put;
432 }
433
434 INIT_LIST_HEAD(&newrec->endp_list);
435 INIT_LIST_HEAD(&newrec->ctrl_list);
James Smartc913a8b2017-04-11 11:35:08 -0700436 INIT_LIST_HEAD(&newrec->ls_req_list);
James Smarte3994412016-12-02 00:28:42 -0800437 kref_init(&newrec->ref);
438 spin_lock_init(&newrec->lock);
439 newrec->remoteport.localport = &lport->localport;
James Smartc913a8b2017-04-11 11:35:08 -0700440 newrec->dev = lport->dev;
441 newrec->lport = lport;
James Smarte3994412016-12-02 00:28:42 -0800442 newrec->remoteport.private = &newrec[1];
443 newrec->remoteport.port_role = pinfo->port_role;
444 newrec->remoteport.node_name = pinfo->node_name;
445 newrec->remoteport.port_name = pinfo->port_name;
446 newrec->remoteport.port_id = pinfo->port_id;
447 newrec->remoteport.port_state = FC_OBJSTATE_ONLINE;
448 newrec->remoteport.port_num = idx;
449
450 spin_lock_irqsave(&nvme_fc_lock, flags);
451 list_add_tail(&newrec->endp_list, &lport->endp_list);
452 spin_unlock_irqrestore(&nvme_fc_lock, flags);
453
454 *portptr = &newrec->remoteport;
455 return 0;
456
457out_lport_put:
458 nvme_fc_lport_put(lport);
459out_kfree_rport:
460 kfree(newrec);
461out_reghost_failed:
462 *portptr = NULL;
463 return ret;
James Smarte3994412016-12-02 00:28:42 -0800464}
465EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport);
466
467static void
468nvme_fc_free_rport(struct kref *ref)
469{
470 struct nvme_fc_rport *rport =
471 container_of(ref, struct nvme_fc_rport, ref);
472 struct nvme_fc_lport *lport =
473 localport_to_lport(rport->remoteport.localport);
474 unsigned long flags;
475
476 WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED);
477 WARN_ON(!list_empty(&rport->ctrl_list));
478
479 /* remove from lport list */
480 spin_lock_irqsave(&nvme_fc_lock, flags);
481 list_del(&rport->endp_list);
482 spin_unlock_irqrestore(&nvme_fc_lock, flags);
483
484 /* let the LLDD know we've finished tearing it down */
485 lport->ops->remoteport_delete(&rport->remoteport);
486
487 ida_simple_remove(&lport->endp_cnt, rport->remoteport.port_num);
488
489 kfree(rport);
490
491 nvme_fc_lport_put(lport);
492}
493
494static void
495nvme_fc_rport_put(struct nvme_fc_rport *rport)
496{
497 kref_put(&rport->ref, nvme_fc_free_rport);
498}
499
500static int
501nvme_fc_rport_get(struct nvme_fc_rport *rport)
502{
503 return kref_get_unless_zero(&rport->ref);
504}
505
James Smart8d64daf2017-04-11 11:35:09 -0700506static int
507nvme_fc_abort_lsops(struct nvme_fc_rport *rport)
508{
509 struct nvmefc_ls_req_op *lsop;
510 unsigned long flags;
511
512restart:
513 spin_lock_irqsave(&rport->lock, flags);
514
515 list_for_each_entry(lsop, &rport->ls_req_list, lsreq_list) {
516 if (!(lsop->flags & FCOP_FLAGS_TERMIO)) {
517 lsop->flags |= FCOP_FLAGS_TERMIO;
518 spin_unlock_irqrestore(&rport->lock, flags);
519 rport->lport->ops->ls_abort(&rport->lport->localport,
520 &rport->remoteport,
521 &lsop->ls_req);
522 goto restart;
523 }
524 }
525 spin_unlock_irqrestore(&rport->lock, flags);
526
527 return 0;
528}
529
James Smarte3994412016-12-02 00:28:42 -0800530/**
531 * nvme_fc_unregister_remoteport - transport entry point called by an
532 * LLDD to deregister/remove a previously
533 * registered a NVME subsystem FC port.
534 * @remoteport: pointer to the (registered) remote port that is to be
535 * deregistered.
536 *
537 * Returns:
538 * a completion status. Must be 0 upon success; a negative errno
539 * (ex: -ENXIO) upon failure.
540 */
541int
542nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr)
543{
544 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
545 struct nvme_fc_ctrl *ctrl;
546 unsigned long flags;
547
548 if (!portptr)
549 return -EINVAL;
550
551 spin_lock_irqsave(&rport->lock, flags);
552
553 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
554 spin_unlock_irqrestore(&rport->lock, flags);
555 return -EINVAL;
556 }
557 portptr->port_state = FC_OBJSTATE_DELETED;
558
559 /* tear down all associations to the remote port */
560 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list)
561 __nvme_fc_del_ctrl(ctrl);
562
563 spin_unlock_irqrestore(&rport->lock, flags);
564
James Smart8d64daf2017-04-11 11:35:09 -0700565 nvme_fc_abort_lsops(rport);
566
James Smarte3994412016-12-02 00:28:42 -0800567 nvme_fc_rport_put(rport);
568 return 0;
569}
570EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport);
571
572
573/* *********************** FC-NVME DMA Handling **************************** */
574
575/*
576 * The fcloop device passes in a NULL device pointer. Real LLD's will
577 * pass in a valid device pointer. If NULL is passed to the dma mapping
578 * routines, depending on the platform, it may or may not succeed, and
579 * may crash.
580 *
581 * As such:
582 * Wrapper all the dma routines and check the dev pointer.
583 *
584 * If simple mappings (return just a dma address, we'll noop them,
585 * returning a dma address of 0.
586 *
587 * On more complex mappings (dma_map_sg), a pseudo routine fills
588 * in the scatter list, setting all dma addresses to 0.
589 */
590
591static inline dma_addr_t
592fc_dma_map_single(struct device *dev, void *ptr, size_t size,
593 enum dma_data_direction dir)
594{
595 return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
596}
597
598static inline int
599fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
600{
601 return dev ? dma_mapping_error(dev, dma_addr) : 0;
602}
603
604static inline void
605fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
606 enum dma_data_direction dir)
607{
608 if (dev)
609 dma_unmap_single(dev, addr, size, dir);
610}
611
612static inline void
613fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
614 enum dma_data_direction dir)
615{
616 if (dev)
617 dma_sync_single_for_cpu(dev, addr, size, dir);
618}
619
620static inline void
621fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
622 enum dma_data_direction dir)
623{
624 if (dev)
625 dma_sync_single_for_device(dev, addr, size, dir);
626}
627
628/* pseudo dma_map_sg call */
629static int
630fc_map_sg(struct scatterlist *sg, int nents)
631{
632 struct scatterlist *s;
633 int i;
634
635 WARN_ON(nents == 0 || sg[0].length == 0);
636
637 for_each_sg(sg, s, nents, i) {
638 s->dma_address = 0L;
639#ifdef CONFIG_NEED_SG_DMA_LENGTH
640 s->dma_length = s->length;
641#endif
642 }
643 return nents;
644}
645
646static inline int
647fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
648 enum dma_data_direction dir)
649{
650 return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
651}
652
653static inline void
654fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
655 enum dma_data_direction dir)
656{
657 if (dev)
658 dma_unmap_sg(dev, sg, nents, dir);
659}
660
661
662/* *********************** FC-NVME LS Handling **************************** */
663
664static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
665static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
666
667
668static void
James Smartc913a8b2017-04-11 11:35:08 -0700669__nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop)
James Smarte3994412016-12-02 00:28:42 -0800670{
James Smartc913a8b2017-04-11 11:35:08 -0700671 struct nvme_fc_rport *rport = lsop->rport;
James Smarte3994412016-12-02 00:28:42 -0800672 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
673 unsigned long flags;
674
James Smartc913a8b2017-04-11 11:35:08 -0700675 spin_lock_irqsave(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -0800676
677 if (!lsop->req_queued) {
James Smartc913a8b2017-04-11 11:35:08 -0700678 spin_unlock_irqrestore(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -0800679 return;
680 }
681
682 list_del(&lsop->lsreq_list);
683
684 lsop->req_queued = false;
685
James Smartc913a8b2017-04-11 11:35:08 -0700686 spin_unlock_irqrestore(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -0800687
James Smartc913a8b2017-04-11 11:35:08 -0700688 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
James Smarte3994412016-12-02 00:28:42 -0800689 (lsreq->rqstlen + lsreq->rsplen),
690 DMA_BIDIRECTIONAL);
691
James Smartc913a8b2017-04-11 11:35:08 -0700692 nvme_fc_rport_put(rport);
James Smarte3994412016-12-02 00:28:42 -0800693}
694
695static int
James Smartc913a8b2017-04-11 11:35:08 -0700696__nvme_fc_send_ls_req(struct nvme_fc_rport *rport,
James Smarte3994412016-12-02 00:28:42 -0800697 struct nvmefc_ls_req_op *lsop,
698 void (*done)(struct nvmefc_ls_req *req, int status))
699{
700 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
701 unsigned long flags;
James Smartc913a8b2017-04-11 11:35:08 -0700702 int ret = 0;
James Smarte3994412016-12-02 00:28:42 -0800703
James Smartc913a8b2017-04-11 11:35:08 -0700704 if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
705 return -ECONNREFUSED;
706
707 if (!nvme_fc_rport_get(rport))
James Smarte3994412016-12-02 00:28:42 -0800708 return -ESHUTDOWN;
709
710 lsreq->done = done;
James Smartc913a8b2017-04-11 11:35:08 -0700711 lsop->rport = rport;
James Smarte3994412016-12-02 00:28:42 -0800712 lsop->req_queued = false;
713 INIT_LIST_HEAD(&lsop->lsreq_list);
714 init_completion(&lsop->ls_done);
715
James Smartc913a8b2017-04-11 11:35:08 -0700716 lsreq->rqstdma = fc_dma_map_single(rport->dev, lsreq->rqstaddr,
James Smarte3994412016-12-02 00:28:42 -0800717 lsreq->rqstlen + lsreq->rsplen,
718 DMA_BIDIRECTIONAL);
James Smartc913a8b2017-04-11 11:35:08 -0700719 if (fc_dma_mapping_error(rport->dev, lsreq->rqstdma)) {
720 ret = -EFAULT;
721 goto out_putrport;
James Smarte3994412016-12-02 00:28:42 -0800722 }
723 lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
724
James Smartc913a8b2017-04-11 11:35:08 -0700725 spin_lock_irqsave(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -0800726
James Smartc913a8b2017-04-11 11:35:08 -0700727 list_add_tail(&lsop->lsreq_list, &rport->ls_req_list);
James Smarte3994412016-12-02 00:28:42 -0800728
729 lsop->req_queued = true;
730
James Smartc913a8b2017-04-11 11:35:08 -0700731 spin_unlock_irqrestore(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -0800732
James Smartc913a8b2017-04-11 11:35:08 -0700733 ret = rport->lport->ops->ls_req(&rport->lport->localport,
734 &rport->remoteport, lsreq);
James Smarte3994412016-12-02 00:28:42 -0800735 if (ret)
James Smartc913a8b2017-04-11 11:35:08 -0700736 goto out_unlink;
737
738 return 0;
739
740out_unlink:
741 lsop->ls_error = ret;
742 spin_lock_irqsave(&rport->lock, flags);
743 lsop->req_queued = false;
744 list_del(&lsop->lsreq_list);
745 spin_unlock_irqrestore(&rport->lock, flags);
746 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
747 (lsreq->rqstlen + lsreq->rsplen),
748 DMA_BIDIRECTIONAL);
749out_putrport:
750 nvme_fc_rport_put(rport);
James Smarte3994412016-12-02 00:28:42 -0800751
752 return ret;
753}
754
755static void
756nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status)
757{
758 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
759
760 lsop->ls_error = status;
761 complete(&lsop->ls_done);
762}
763
764static int
James Smartc913a8b2017-04-11 11:35:08 -0700765nvme_fc_send_ls_req(struct nvme_fc_rport *rport, struct nvmefc_ls_req_op *lsop)
James Smarte3994412016-12-02 00:28:42 -0800766{
767 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
768 struct fcnvme_ls_rjt *rjt = lsreq->rspaddr;
769 int ret;
770
James Smartc913a8b2017-04-11 11:35:08 -0700771 ret = __nvme_fc_send_ls_req(rport, lsop, nvme_fc_send_ls_req_done);
James Smarte3994412016-12-02 00:28:42 -0800772
James Smartc913a8b2017-04-11 11:35:08 -0700773 if (!ret) {
James Smarte3994412016-12-02 00:28:42 -0800774 /*
775 * No timeout/not interruptible as we need the struct
776 * to exist until the lldd calls us back. Thus mandate
777 * wait until driver calls back. lldd responsible for
778 * the timeout action
779 */
780 wait_for_completion(&lsop->ls_done);
781
James Smartc913a8b2017-04-11 11:35:08 -0700782 __nvme_fc_finish_ls_req(lsop);
James Smarte3994412016-12-02 00:28:42 -0800783
James Smartc913a8b2017-04-11 11:35:08 -0700784 ret = lsop->ls_error;
James Smarte3994412016-12-02 00:28:42 -0800785 }
786
James Smartc913a8b2017-04-11 11:35:08 -0700787 if (ret)
788 return ret;
789
James Smarte3994412016-12-02 00:28:42 -0800790 /* ACC or RJT payload ? */
791 if (rjt->w0.ls_cmd == FCNVME_LS_RJT)
792 return -ENXIO;
793
794 return 0;
795}
796
James Smartc913a8b2017-04-11 11:35:08 -0700797static int
798nvme_fc_send_ls_req_async(struct nvme_fc_rport *rport,
James Smarte3994412016-12-02 00:28:42 -0800799 struct nvmefc_ls_req_op *lsop,
800 void (*done)(struct nvmefc_ls_req *req, int status))
801{
James Smarte3994412016-12-02 00:28:42 -0800802 /* don't wait for completion */
803
James Smartc913a8b2017-04-11 11:35:08 -0700804 return __nvme_fc_send_ls_req(rport, lsop, done);
James Smarte3994412016-12-02 00:28:42 -0800805}
806
807/* Validation Error indexes into the string table below */
808enum {
809 VERR_NO_ERROR = 0,
810 VERR_LSACC = 1,
811 VERR_LSDESC_RQST = 2,
812 VERR_LSDESC_RQST_LEN = 3,
813 VERR_ASSOC_ID = 4,
814 VERR_ASSOC_ID_LEN = 5,
815 VERR_CONN_ID = 6,
816 VERR_CONN_ID_LEN = 7,
817 VERR_CR_ASSOC = 8,
818 VERR_CR_ASSOC_ACC_LEN = 9,
819 VERR_CR_CONN = 10,
820 VERR_CR_CONN_ACC_LEN = 11,
821 VERR_DISCONN = 12,
822 VERR_DISCONN_ACC_LEN = 13,
823};
824
825static char *validation_errors[] = {
826 "OK",
827 "Not LS_ACC",
828 "Not LSDESC_RQST",
829 "Bad LSDESC_RQST Length",
830 "Not Association ID",
831 "Bad Association ID Length",
832 "Not Connection ID",
833 "Bad Connection ID Length",
834 "Not CR_ASSOC Rqst",
835 "Bad CR_ASSOC ACC Length",
836 "Not CR_CONN Rqst",
837 "Bad CR_CONN ACC Length",
838 "Not Disconnect Rqst",
839 "Bad Disconnect ACC Length",
840};
841
842static int
843nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl,
844 struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio)
845{
846 struct nvmefc_ls_req_op *lsop;
847 struct nvmefc_ls_req *lsreq;
848 struct fcnvme_ls_cr_assoc_rqst *assoc_rqst;
849 struct fcnvme_ls_cr_assoc_acc *assoc_acc;
850 int ret, fcret = 0;
851
852 lsop = kzalloc((sizeof(*lsop) +
853 ctrl->lport->ops->lsrqst_priv_sz +
854 sizeof(*assoc_rqst) + sizeof(*assoc_acc)), GFP_KERNEL);
855 if (!lsop) {
856 ret = -ENOMEM;
857 goto out_no_memory;
858 }
859 lsreq = &lsop->ls_req;
860
861 lsreq->private = (void *)&lsop[1];
862 assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)
863 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
864 assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1];
865
866 assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION;
867 assoc_rqst->desc_list_len =
868 cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
869
870 assoc_rqst->assoc_cmd.desc_tag =
871 cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD);
872 assoc_rqst->assoc_cmd.desc_len =
873 fcnvme_lsdesc_len(
874 sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
875
876 assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
877 assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize);
878 /* Linux supports only Dynamic controllers */
879 assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff);
Christoph Hellwig8e412262017-05-17 09:54:27 +0200880 uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id);
James Smarte3994412016-12-02 00:28:42 -0800881 strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
882 min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE));
883 strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
884 min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE));
885
886 lsop->queue = queue;
887 lsreq->rqstaddr = assoc_rqst;
888 lsreq->rqstlen = sizeof(*assoc_rqst);
889 lsreq->rspaddr = assoc_acc;
890 lsreq->rsplen = sizeof(*assoc_acc);
891 lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
892
James Smartc913a8b2017-04-11 11:35:08 -0700893 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
James Smarte3994412016-12-02 00:28:42 -0800894 if (ret)
895 goto out_free_buffer;
896
897 /* process connect LS completion */
898
899 /* validate the ACC response */
900 if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
901 fcret = VERR_LSACC;
James Smartf77fc872017-03-23 20:41:25 -0700902 else if (assoc_acc->hdr.desc_list_len !=
James Smarte3994412016-12-02 00:28:42 -0800903 fcnvme_lsdesc_len(
904 sizeof(struct fcnvme_ls_cr_assoc_acc)))
905 fcret = VERR_CR_ASSOC_ACC_LEN;
James Smartf77fc872017-03-23 20:41:25 -0700906 else if (assoc_acc->hdr.rqst.desc_tag !=
907 cpu_to_be32(FCNVME_LSDESC_RQST))
James Smarte3994412016-12-02 00:28:42 -0800908 fcret = VERR_LSDESC_RQST;
909 else if (assoc_acc->hdr.rqst.desc_len !=
910 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
911 fcret = VERR_LSDESC_RQST_LEN;
912 else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION)
913 fcret = VERR_CR_ASSOC;
914 else if (assoc_acc->associd.desc_tag !=
915 cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
916 fcret = VERR_ASSOC_ID;
917 else if (assoc_acc->associd.desc_len !=
918 fcnvme_lsdesc_len(
919 sizeof(struct fcnvme_lsdesc_assoc_id)))
920 fcret = VERR_ASSOC_ID_LEN;
921 else if (assoc_acc->connectid.desc_tag !=
922 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
923 fcret = VERR_CONN_ID;
924 else if (assoc_acc->connectid.desc_len !=
925 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
926 fcret = VERR_CONN_ID_LEN;
927
928 if (fcret) {
929 ret = -EBADF;
930 dev_err(ctrl->dev,
931 "q %d connect failed: %s\n",
932 queue->qnum, validation_errors[fcret]);
933 } else {
934 ctrl->association_id =
935 be64_to_cpu(assoc_acc->associd.association_id);
936 queue->connection_id =
937 be64_to_cpu(assoc_acc->connectid.connection_id);
938 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
939 }
940
941out_free_buffer:
942 kfree(lsop);
943out_no_memory:
944 if (ret)
945 dev_err(ctrl->dev,
946 "queue %d connect admin queue failed (%d).\n",
947 queue->qnum, ret);
948 return ret;
949}
950
951static int
952nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
953 u16 qsize, u16 ersp_ratio)
954{
955 struct nvmefc_ls_req_op *lsop;
956 struct nvmefc_ls_req *lsreq;
957 struct fcnvme_ls_cr_conn_rqst *conn_rqst;
958 struct fcnvme_ls_cr_conn_acc *conn_acc;
959 int ret, fcret = 0;
960
961 lsop = kzalloc((sizeof(*lsop) +
962 ctrl->lport->ops->lsrqst_priv_sz +
963 sizeof(*conn_rqst) + sizeof(*conn_acc)), GFP_KERNEL);
964 if (!lsop) {
965 ret = -ENOMEM;
966 goto out_no_memory;
967 }
968 lsreq = &lsop->ls_req;
969
970 lsreq->private = (void *)&lsop[1];
971 conn_rqst = (struct fcnvme_ls_cr_conn_rqst *)
972 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
973 conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1];
974
975 conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION;
976 conn_rqst->desc_list_len = cpu_to_be32(
977 sizeof(struct fcnvme_lsdesc_assoc_id) +
978 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
979
980 conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
981 conn_rqst->associd.desc_len =
982 fcnvme_lsdesc_len(
983 sizeof(struct fcnvme_lsdesc_assoc_id));
984 conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
985 conn_rqst->connect_cmd.desc_tag =
986 cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD);
987 conn_rqst->connect_cmd.desc_len =
988 fcnvme_lsdesc_len(
989 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
990 conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
991 conn_rqst->connect_cmd.qid = cpu_to_be16(queue->qnum);
992 conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize);
993
994 lsop->queue = queue;
995 lsreq->rqstaddr = conn_rqst;
996 lsreq->rqstlen = sizeof(*conn_rqst);
997 lsreq->rspaddr = conn_acc;
998 lsreq->rsplen = sizeof(*conn_acc);
999 lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
1000
James Smartc913a8b2017-04-11 11:35:08 -07001001 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
James Smarte3994412016-12-02 00:28:42 -08001002 if (ret)
1003 goto out_free_buffer;
1004
1005 /* process connect LS completion */
1006
1007 /* validate the ACC response */
1008 if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1009 fcret = VERR_LSACC;
James Smartf77fc872017-03-23 20:41:25 -07001010 else if (conn_acc->hdr.desc_list_len !=
James Smarte3994412016-12-02 00:28:42 -08001011 fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)))
1012 fcret = VERR_CR_CONN_ACC_LEN;
James Smartf77fc872017-03-23 20:41:25 -07001013 else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST))
James Smarte3994412016-12-02 00:28:42 -08001014 fcret = VERR_LSDESC_RQST;
1015 else if (conn_acc->hdr.rqst.desc_len !=
1016 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1017 fcret = VERR_LSDESC_RQST_LEN;
1018 else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION)
1019 fcret = VERR_CR_CONN;
1020 else if (conn_acc->connectid.desc_tag !=
1021 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1022 fcret = VERR_CONN_ID;
1023 else if (conn_acc->connectid.desc_len !=
1024 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1025 fcret = VERR_CONN_ID_LEN;
1026
1027 if (fcret) {
1028 ret = -EBADF;
1029 dev_err(ctrl->dev,
1030 "q %d connect failed: %s\n",
1031 queue->qnum, validation_errors[fcret]);
1032 } else {
1033 queue->connection_id =
1034 be64_to_cpu(conn_acc->connectid.connection_id);
1035 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1036 }
1037
1038out_free_buffer:
1039 kfree(lsop);
1040out_no_memory:
1041 if (ret)
1042 dev_err(ctrl->dev,
1043 "queue %d connect command failed (%d).\n",
1044 queue->qnum, ret);
1045 return ret;
1046}
1047
1048static void
1049nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
1050{
1051 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
James Smarte3994412016-12-02 00:28:42 -08001052
James Smartc913a8b2017-04-11 11:35:08 -07001053 __nvme_fc_finish_ls_req(lsop);
James Smarte3994412016-12-02 00:28:42 -08001054
1055 /* fc-nvme iniator doesn't care about success or failure of cmd */
1056
1057 kfree(lsop);
1058}
1059
1060/*
1061 * This routine sends a FC-NVME LS to disconnect (aka terminate)
1062 * the FC-NVME Association. Terminating the association also
1063 * terminates the FC-NVME connections (per queue, both admin and io
1064 * queues) that are part of the association. E.g. things are torn
1065 * down, and the related FC-NVME Association ID and Connection IDs
1066 * become invalid.
1067 *
1068 * The behavior of the fc-nvme initiator is such that it's
1069 * understanding of the association and connections will implicitly
1070 * be torn down. The action is implicit as it may be due to a loss of
1071 * connectivity with the fc-nvme target, so you may never get a
1072 * response even if you tried. As such, the action of this routine
1073 * is to asynchronously send the LS, ignore any results of the LS, and
1074 * continue on with terminating the association. If the fc-nvme target
1075 * is present and receives the LS, it too can tear down.
1076 */
1077static void
1078nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl)
1079{
1080 struct fcnvme_ls_disconnect_rqst *discon_rqst;
1081 struct fcnvme_ls_disconnect_acc *discon_acc;
1082 struct nvmefc_ls_req_op *lsop;
1083 struct nvmefc_ls_req *lsreq;
James Smartc913a8b2017-04-11 11:35:08 -07001084 int ret;
James Smarte3994412016-12-02 00:28:42 -08001085
1086 lsop = kzalloc((sizeof(*lsop) +
1087 ctrl->lport->ops->lsrqst_priv_sz +
1088 sizeof(*discon_rqst) + sizeof(*discon_acc)),
1089 GFP_KERNEL);
1090 if (!lsop)
1091 /* couldn't sent it... too bad */
1092 return;
1093
1094 lsreq = &lsop->ls_req;
1095
1096 lsreq->private = (void *)&lsop[1];
1097 discon_rqst = (struct fcnvme_ls_disconnect_rqst *)
1098 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1099 discon_acc = (struct fcnvme_ls_disconnect_acc *)&discon_rqst[1];
1100
1101 discon_rqst->w0.ls_cmd = FCNVME_LS_DISCONNECT;
1102 discon_rqst->desc_list_len = cpu_to_be32(
1103 sizeof(struct fcnvme_lsdesc_assoc_id) +
1104 sizeof(struct fcnvme_lsdesc_disconn_cmd));
1105
1106 discon_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1107 discon_rqst->associd.desc_len =
1108 fcnvme_lsdesc_len(
1109 sizeof(struct fcnvme_lsdesc_assoc_id));
1110
1111 discon_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1112
1113 discon_rqst->discon_cmd.desc_tag = cpu_to_be32(
1114 FCNVME_LSDESC_DISCONN_CMD);
1115 discon_rqst->discon_cmd.desc_len =
1116 fcnvme_lsdesc_len(
1117 sizeof(struct fcnvme_lsdesc_disconn_cmd));
1118 discon_rqst->discon_cmd.scope = FCNVME_DISCONN_ASSOCIATION;
1119 discon_rqst->discon_cmd.id = cpu_to_be64(ctrl->association_id);
1120
1121 lsreq->rqstaddr = discon_rqst;
1122 lsreq->rqstlen = sizeof(*discon_rqst);
1123 lsreq->rspaddr = discon_acc;
1124 lsreq->rsplen = sizeof(*discon_acc);
1125 lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
1126
James Smartc913a8b2017-04-11 11:35:08 -07001127 ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop,
1128 nvme_fc_disconnect_assoc_done);
1129 if (ret)
1130 kfree(lsop);
James Smarte3994412016-12-02 00:28:42 -08001131
1132 /* only meaningful part to terminating the association */
1133 ctrl->association_id = 0;
1134}
1135
1136
1137/* *********************** NVME Ctrl Routines **************************** */
1138
James Smart78a7ac22017-04-23 08:30:07 -07001139static void __nvme_fc_final_op_cleanup(struct request *rq);
James Smartf874d5d2017-06-01 22:54:21 -07001140static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg);
James Smarte3994412016-12-02 00:28:42 -08001141
1142static int
1143nvme_fc_reinit_request(void *data, struct request *rq)
1144{
1145 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1146 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1147
1148 memset(cmdiu, 0, sizeof(*cmdiu));
1149 cmdiu->scsi_id = NVME_CMD_SCSI_ID;
1150 cmdiu->fc_id = NVME_CMD_FC_ID;
1151 cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
1152 memset(&op->rsp_iu, 0, sizeof(op->rsp_iu));
1153
1154 return 0;
1155}
1156
1157static void
1158__nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl,
1159 struct nvme_fc_fcp_op *op)
1160{
1161 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma,
1162 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1163 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
1164 sizeof(op->cmd_iu), DMA_TO_DEVICE);
1165
1166 atomic_set(&op->state, FCPOP_STATE_UNINIT);
1167}
1168
1169static void
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001170nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1171 unsigned int hctx_idx)
James Smarte3994412016-12-02 00:28:42 -08001172{
1173 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1174
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001175 return __nvme_fc_exit_request(set->driver_data, op);
James Smarte3994412016-12-02 00:28:42 -08001176}
1177
James Smart78a7ac22017-04-23 08:30:07 -07001178static int
1179__nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
1180{
1181 int state;
1182
1183 state = atomic_xchg(&op->state, FCPOP_STATE_ABORTED);
1184 if (state != FCPOP_STATE_ACTIVE) {
1185 atomic_set(&op->state, state);
1186 return -ECANCELED;
1187 }
1188
1189 ctrl->lport->ops->fcp_abort(&ctrl->lport->localport,
1190 &ctrl->rport->remoteport,
1191 op->queue->lldd_handle,
1192 &op->fcp_req);
1193
1194 return 0;
1195}
1196
James Smarte3994412016-12-02 00:28:42 -08001197static void
James Smart78a7ac22017-04-23 08:30:07 -07001198nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl)
James Smarte3994412016-12-02 00:28:42 -08001199{
1200 struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops;
James Smart78a7ac22017-04-23 08:30:07 -07001201 unsigned long flags;
1202 int i, ret;
James Smarte3994412016-12-02 00:28:42 -08001203
1204 for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
James Smart78a7ac22017-04-23 08:30:07 -07001205 if (atomic_read(&aen_op->state) != FCPOP_STATE_ACTIVE)
James Smarte3994412016-12-02 00:28:42 -08001206 continue;
James Smart78a7ac22017-04-23 08:30:07 -07001207
1208 spin_lock_irqsave(&ctrl->lock, flags);
James Smart61bff8e2017-04-23 08:30:08 -07001209 if (ctrl->flags & FCCTRL_TERMIO) {
1210 ctrl->iocnt++;
1211 aen_op->flags |= FCOP_FLAGS_TERMIO;
1212 }
James Smart78a7ac22017-04-23 08:30:07 -07001213 spin_unlock_irqrestore(&ctrl->lock, flags);
1214
1215 ret = __nvme_fc_abort_op(ctrl, aen_op);
1216 if (ret) {
1217 /*
1218 * if __nvme_fc_abort_op failed the io wasn't
1219 * active. Thus this call path is running in
1220 * parallel to the io complete. Treat as non-error.
1221 */
1222
1223 /* back out the flags/counters */
1224 spin_lock_irqsave(&ctrl->lock, flags);
James Smart61bff8e2017-04-23 08:30:08 -07001225 if (ctrl->flags & FCCTRL_TERMIO)
1226 ctrl->iocnt--;
James Smart78a7ac22017-04-23 08:30:07 -07001227 aen_op->flags &= ~FCOP_FLAGS_TERMIO;
1228 spin_unlock_irqrestore(&ctrl->lock, flags);
1229 return;
1230 }
James Smarte3994412016-12-02 00:28:42 -08001231 }
1232}
1233
James Smart78a7ac22017-04-23 08:30:07 -07001234static inline int
1235__nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
1236 struct nvme_fc_fcp_op *op)
1237{
1238 unsigned long flags;
1239 bool complete_rq = false;
1240
1241 spin_lock_irqsave(&ctrl->lock, flags);
James Smart61bff8e2017-04-23 08:30:08 -07001242 if (unlikely(op->flags & FCOP_FLAGS_TERMIO)) {
James Smart36715cf2017-05-22 15:28:42 -07001243 if (ctrl->flags & FCCTRL_TERMIO) {
1244 if (!--ctrl->iocnt)
1245 wake_up(&ctrl->ioabort_wait);
1246 }
James Smart61bff8e2017-04-23 08:30:08 -07001247 }
James Smart78a7ac22017-04-23 08:30:07 -07001248 if (op->flags & FCOP_FLAGS_RELEASED)
1249 complete_rq = true;
1250 else
1251 op->flags |= FCOP_FLAGS_COMPLETE;
1252 spin_unlock_irqrestore(&ctrl->lock, flags);
1253
1254 return complete_rq;
1255}
1256
Christoph Hellwigbaee29a2017-04-21 10:44:06 +02001257static void
James Smarte3994412016-12-02 00:28:42 -08001258nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
1259{
1260 struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1261 struct request *rq = op->rq;
1262 struct nvmefc_fcp_req *freq = &op->fcp_req;
1263 struct nvme_fc_ctrl *ctrl = op->ctrl;
1264 struct nvme_fc_queue *queue = op->queue;
1265 struct nvme_completion *cqe = &op->rsp_iu.cqe;
James Smart458f2802017-04-23 08:30:06 -07001266 struct nvme_command *sqe = &op->cmd_iu.sqe;
Christoph Hellwigd663b692017-04-20 16:02:56 +02001267 __le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001268 union nvme_result result;
James Smartf874d5d2017-06-01 22:54:21 -07001269 bool complete_rq, terminate_assoc = true;
James Smarte3994412016-12-02 00:28:42 -08001270
1271 /*
1272 * WARNING:
1273 * The current linux implementation of a nvme controller
1274 * allocates a single tag set for all io queues and sizes
1275 * the io queues to fully hold all possible tags. Thus, the
1276 * implementation does not reference or care about the sqhd
1277 * value as it never needs to use the sqhd/sqtail pointers
1278 * for submission pacing.
1279 *
1280 * This affects the FC-NVME implementation in two ways:
1281 * 1) As the value doesn't matter, we don't need to waste
1282 * cycles extracting it from ERSPs and stamping it in the
1283 * cases where the transport fabricates CQEs on successful
1284 * completions.
1285 * 2) The FC-NVME implementation requires that delivery of
1286 * ERSP completions are to go back to the nvme layer in order
1287 * relative to the rsn, such that the sqhd value will always
1288 * be "in order" for the nvme layer. As the nvme layer in
1289 * linux doesn't care about sqhd, there's no need to return
1290 * them in order.
1291 *
1292 * Additionally:
1293 * As the core nvme layer in linux currently does not look at
1294 * every field in the cqe - in cases where the FC transport must
1295 * fabricate a CQE, the following fields will not be set as they
1296 * are not referenced:
1297 * cqe.sqid, cqe.sqhd, cqe.command_id
James Smartf874d5d2017-06-01 22:54:21 -07001298 *
1299 * Failure or error of an individual i/o, in a transport
1300 * detected fashion unrelated to the nvme completion status,
1301 * potentially cause the initiator and target sides to get out
1302 * of sync on SQ head/tail (aka outstanding io count allowed).
1303 * Per FC-NVME spec, failure of an individual command requires
1304 * the connection to be terminated, which in turn requires the
1305 * association to be terminated.
James Smarte3994412016-12-02 00:28:42 -08001306 */
1307
1308 fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
1309 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1310
1311 if (atomic_read(&op->state) == FCPOP_STATE_ABORTED)
Christoph Hellwigd663b692017-04-20 16:02:56 +02001312 status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
James Smart62eeacb2017-03-23 20:41:27 -07001313 else if (freq->status)
Christoph Hellwigd663b692017-04-20 16:02:56 +02001314 status = cpu_to_le16(NVME_SC_FC_TRANSPORT_ERROR << 1);
James Smarte3994412016-12-02 00:28:42 -08001315
1316 /*
1317 * For the linux implementation, if we have an unsuccesful
1318 * status, they blk-mq layer can typically be called with the
1319 * non-zero status and the content of the cqe isn't important.
1320 */
1321 if (status)
1322 goto done;
1323
1324 /*
1325 * command completed successfully relative to the wire
1326 * protocol. However, validate anything received and
1327 * extract the status and result from the cqe (create it
1328 * where necessary).
1329 */
1330
1331 switch (freq->rcv_rsplen) {
1332
1333 case 0:
1334 case NVME_FC_SIZEOF_ZEROS_RSP:
1335 /*
1336 * No response payload or 12 bytes of payload (which
1337 * should all be zeros) are considered successful and
1338 * no payload in the CQE by the transport.
1339 */
1340 if (freq->transferred_length !=
1341 be32_to_cpu(op->cmd_iu.data_len)) {
Christoph Hellwigd663b692017-04-20 16:02:56 +02001342 status = cpu_to_le16(NVME_SC_FC_TRANSPORT_ERROR << 1);
James Smarte3994412016-12-02 00:28:42 -08001343 goto done;
1344 }
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001345 result.u64 = 0;
James Smarte3994412016-12-02 00:28:42 -08001346 break;
1347
1348 case sizeof(struct nvme_fc_ersp_iu):
1349 /*
1350 * The ERSP IU contains a full completion with CQE.
1351 * Validate ERSP IU and look at cqe.
1352 */
1353 if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) !=
1354 (freq->rcv_rsplen / 4) ||
1355 be32_to_cpu(op->rsp_iu.xfrd_len) !=
1356 freq->transferred_length ||
James Smart726a1082017-03-23 20:41:23 -07001357 op->rsp_iu.status_code ||
James Smart458f2802017-04-23 08:30:06 -07001358 sqe->common.command_id != cqe->command_id)) {
Christoph Hellwigd663b692017-04-20 16:02:56 +02001359 status = cpu_to_le16(NVME_SC_FC_TRANSPORT_ERROR << 1);
James Smarte3994412016-12-02 00:28:42 -08001360 goto done;
1361 }
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001362 result = cqe->result;
Christoph Hellwigd663b692017-04-20 16:02:56 +02001363 status = cqe->status;
James Smarte3994412016-12-02 00:28:42 -08001364 break;
1365
1366 default:
Christoph Hellwigd663b692017-04-20 16:02:56 +02001367 status = cpu_to_le16(NVME_SC_FC_TRANSPORT_ERROR << 1);
James Smarte3994412016-12-02 00:28:42 -08001368 goto done;
1369 }
1370
James Smartf874d5d2017-06-01 22:54:21 -07001371 terminate_assoc = false;
1372
James Smarte3994412016-12-02 00:28:42 -08001373done:
James Smart78a7ac22017-04-23 08:30:07 -07001374 if (op->flags & FCOP_FLAGS_AEN) {
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001375 nvme_complete_async_event(&queue->ctrl->ctrl, status, &result);
James Smart78a7ac22017-04-23 08:30:07 -07001376 complete_rq = __nvme_fc_fcpop_chk_teardowns(ctrl, op);
1377 atomic_set(&op->state, FCPOP_STATE_IDLE);
1378 op->flags = FCOP_FLAGS_AEN; /* clear other flags */
James Smarte3994412016-12-02 00:28:42 -08001379 nvme_fc_ctrl_put(ctrl);
James Smartf874d5d2017-06-01 22:54:21 -07001380 goto check_error;
James Smarte3994412016-12-02 00:28:42 -08001381 }
1382
James Smart78a7ac22017-04-23 08:30:07 -07001383 complete_rq = __nvme_fc_fcpop_chk_teardowns(ctrl, op);
1384 if (!complete_rq) {
1385 if (unlikely(op->flags & FCOP_FLAGS_TERMIO)) {
James Smarte392e1f2017-05-15 17:10:24 -07001386 status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
James Smart78a7ac22017-04-23 08:30:07 -07001387 if (blk_queue_dying(rq->q))
James Smarte392e1f2017-05-15 17:10:24 -07001388 status |= cpu_to_le16(NVME_SC_DNR << 1);
James Smart78a7ac22017-04-23 08:30:07 -07001389 }
1390 nvme_end_request(rq, status, result);
1391 } else
1392 __nvme_fc_final_op_cleanup(rq);
James Smartf874d5d2017-06-01 22:54:21 -07001393
1394check_error:
1395 if (terminate_assoc)
1396 nvme_fc_error_recovery(ctrl, "transport detected io error");
James Smarte3994412016-12-02 00:28:42 -08001397}
1398
1399static int
1400__nvme_fc_init_request(struct nvme_fc_ctrl *ctrl,
1401 struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op,
1402 struct request *rq, u32 rqno)
1403{
1404 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1405 int ret = 0;
1406
1407 memset(op, 0, sizeof(*op));
1408 op->fcp_req.cmdaddr = &op->cmd_iu;
1409 op->fcp_req.cmdlen = sizeof(op->cmd_iu);
1410 op->fcp_req.rspaddr = &op->rsp_iu;
1411 op->fcp_req.rsplen = sizeof(op->rsp_iu);
1412 op->fcp_req.done = nvme_fc_fcpio_done;
1413 op->fcp_req.first_sgl = (struct scatterlist *)&op[1];
1414 op->fcp_req.private = &op->fcp_req.first_sgl[SG_CHUNK_SIZE];
1415 op->ctrl = ctrl;
1416 op->queue = queue;
1417 op->rq = rq;
1418 op->rqno = rqno;
1419
1420 cmdiu->scsi_id = NVME_CMD_SCSI_ID;
1421 cmdiu->fc_id = NVME_CMD_FC_ID;
1422 cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
1423
1424 op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev,
1425 &op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE);
1426 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) {
1427 dev_err(ctrl->dev,
1428 "FCP Op failed - cmdiu dma mapping failed.\n");
1429 ret = EFAULT;
1430 goto out_on_error;
1431 }
1432
1433 op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev,
1434 &op->rsp_iu, sizeof(op->rsp_iu),
1435 DMA_FROM_DEVICE);
1436 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) {
1437 dev_err(ctrl->dev,
1438 "FCP Op failed - rspiu dma mapping failed.\n");
1439 ret = EFAULT;
1440 }
1441
1442 atomic_set(&op->state, FCPOP_STATE_IDLE);
1443out_on_error:
1444 return ret;
1445}
1446
1447static int
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001448nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq,
1449 unsigned int hctx_idx, unsigned int numa_node)
James Smarte3994412016-12-02 00:28:42 -08001450{
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001451 struct nvme_fc_ctrl *ctrl = set->driver_data;
James Smarte3994412016-12-02 00:28:42 -08001452 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
Christoph Hellwig76f983c2017-06-13 09:15:20 +02001453 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
1454 struct nvme_fc_queue *queue = &ctrl->queues[queue_idx];
James Smarte3994412016-12-02 00:28:42 -08001455
1456 return __nvme_fc_init_request(ctrl, queue, op, rq, queue->rqcnt++);
1457}
1458
1459static int
1460nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl)
1461{
1462 struct nvme_fc_fcp_op *aen_op;
1463 struct nvme_fc_cmd_iu *cmdiu;
1464 struct nvme_command *sqe;
James Smart61bff8e2017-04-23 08:30:08 -07001465 void *private;
James Smarte3994412016-12-02 00:28:42 -08001466 int i, ret;
1467
1468 aen_op = ctrl->aen_ops;
1469 for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
James Smart61bff8e2017-04-23 08:30:08 -07001470 private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz,
1471 GFP_KERNEL);
1472 if (!private)
1473 return -ENOMEM;
1474
James Smarte3994412016-12-02 00:28:42 -08001475 cmdiu = &aen_op->cmd_iu;
1476 sqe = &cmdiu->sqe;
1477 ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0],
1478 aen_op, (struct request *)NULL,
1479 (AEN_CMDID_BASE + i));
James Smart61bff8e2017-04-23 08:30:08 -07001480 if (ret) {
1481 kfree(private);
James Smarte3994412016-12-02 00:28:42 -08001482 return ret;
James Smart61bff8e2017-04-23 08:30:08 -07001483 }
James Smarte3994412016-12-02 00:28:42 -08001484
James Smart78a7ac22017-04-23 08:30:07 -07001485 aen_op->flags = FCOP_FLAGS_AEN;
James Smart61bff8e2017-04-23 08:30:08 -07001486 aen_op->fcp_req.first_sgl = NULL; /* no sg list */
1487 aen_op->fcp_req.private = private;
James Smart78a7ac22017-04-23 08:30:07 -07001488
James Smarte3994412016-12-02 00:28:42 -08001489 memset(sqe, 0, sizeof(*sqe));
1490 sqe->common.opcode = nvme_admin_async_event;
James Smart78a7ac22017-04-23 08:30:07 -07001491 /* Note: core layer may overwrite the sqe.command_id value */
James Smarte3994412016-12-02 00:28:42 -08001492 sqe->common.command_id = AEN_CMDID_BASE + i;
1493 }
1494 return 0;
1495}
1496
James Smart61bff8e2017-04-23 08:30:08 -07001497static void
1498nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl)
1499{
1500 struct nvme_fc_fcp_op *aen_op;
1501 int i;
1502
1503 aen_op = ctrl->aen_ops;
1504 for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
1505 if (!aen_op->fcp_req.private)
1506 continue;
1507
1508 __nvme_fc_exit_request(ctrl, aen_op);
1509
1510 kfree(aen_op->fcp_req.private);
1511 aen_op->fcp_req.private = NULL;
1512 }
1513}
James Smarte3994412016-12-02 00:28:42 -08001514
1515static inline void
1516__nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, struct nvme_fc_ctrl *ctrl,
1517 unsigned int qidx)
1518{
1519 struct nvme_fc_queue *queue = &ctrl->queues[qidx];
1520
1521 hctx->driver_data = queue;
1522 queue->hctx = hctx;
1523}
1524
1525static int
1526nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1527 unsigned int hctx_idx)
1528{
1529 struct nvme_fc_ctrl *ctrl = data;
1530
1531 __nvme_fc_init_hctx(hctx, ctrl, hctx_idx + 1);
1532
1533 return 0;
1534}
1535
1536static int
1537nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1538 unsigned int hctx_idx)
1539{
1540 struct nvme_fc_ctrl *ctrl = data;
1541
1542 __nvme_fc_init_hctx(hctx, ctrl, hctx_idx);
1543
1544 return 0;
1545}
1546
1547static void
1548nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx, size_t queue_size)
1549{
1550 struct nvme_fc_queue *queue;
1551
1552 queue = &ctrl->queues[idx];
1553 memset(queue, 0, sizeof(*queue));
1554 queue->ctrl = ctrl;
1555 queue->qnum = idx;
1556 atomic_set(&queue->csn, 1);
1557 queue->dev = ctrl->dev;
1558
1559 if (idx > 0)
1560 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
1561 else
1562 queue->cmnd_capsule_len = sizeof(struct nvme_command);
1563
1564 queue->queue_size = queue_size;
1565
1566 /*
1567 * Considered whether we should allocate buffers for all SQEs
1568 * and CQEs and dma map them - mapping their respective entries
1569 * into the request structures (kernel vm addr and dma address)
1570 * thus the driver could use the buffers/mappings directly.
1571 * It only makes sense if the LLDD would use them for its
1572 * messaging api. It's very unlikely most adapter api's would use
1573 * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
1574 * structures were used instead.
1575 */
1576}
1577
1578/*
1579 * This routine terminates a queue at the transport level.
1580 * The transport has already ensured that all outstanding ios on
1581 * the queue have been terminated.
1582 * The transport will send a Disconnect LS request to terminate
1583 * the queue's connection. Termination of the admin queue will also
1584 * terminate the association at the target.
1585 */
1586static void
1587nvme_fc_free_queue(struct nvme_fc_queue *queue)
1588{
1589 if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags))
1590 return;
1591
1592 /*
1593 * Current implementation never disconnects a single queue.
1594 * It always terminates a whole association. So there is never
1595 * a disconnect(queue) LS sent to the target.
1596 */
1597
1598 queue->connection_id = 0;
1599 clear_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1600}
1601
1602static void
1603__nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl,
1604 struct nvme_fc_queue *queue, unsigned int qidx)
1605{
1606 if (ctrl->lport->ops->delete_queue)
1607 ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx,
1608 queue->lldd_handle);
1609 queue->lldd_handle = NULL;
1610}
1611
1612static void
James Smarte3994412016-12-02 00:28:42 -08001613nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl)
1614{
1615 int i;
1616
1617 for (i = 1; i < ctrl->queue_count; i++)
1618 nvme_fc_free_queue(&ctrl->queues[i]);
1619}
1620
1621static int
1622__nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl,
1623 struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize)
1624{
1625 int ret = 0;
1626
1627 queue->lldd_handle = NULL;
1628 if (ctrl->lport->ops->create_queue)
1629 ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport,
1630 qidx, qsize, &queue->lldd_handle);
1631
1632 return ret;
1633}
1634
1635static void
1636nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl)
1637{
1638 struct nvme_fc_queue *queue = &ctrl->queues[ctrl->queue_count - 1];
1639 int i;
1640
1641 for (i = ctrl->queue_count - 1; i >= 1; i--, queue--)
1642 __nvme_fc_delete_hw_queue(ctrl, queue, i);
1643}
1644
1645static int
1646nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
1647{
1648 struct nvme_fc_queue *queue = &ctrl->queues[1];
Johannes Thumshirn17a1ec02016-12-15 14:20:48 +01001649 int i, ret;
James Smarte3994412016-12-02 00:28:42 -08001650
1651 for (i = 1; i < ctrl->queue_count; i++, queue++) {
1652 ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize);
Johannes Thumshirn17a1ec02016-12-15 14:20:48 +01001653 if (ret)
1654 goto delete_queues;
James Smarte3994412016-12-02 00:28:42 -08001655 }
1656
1657 return 0;
Johannes Thumshirn17a1ec02016-12-15 14:20:48 +01001658
1659delete_queues:
1660 for (; i >= 0; i--)
1661 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i);
1662 return ret;
James Smarte3994412016-12-02 00:28:42 -08001663}
1664
1665static int
1666nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
1667{
1668 int i, ret = 0;
1669
1670 for (i = 1; i < ctrl->queue_count; i++) {
1671 ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize,
1672 (qsize / 5));
1673 if (ret)
1674 break;
1675 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
1676 if (ret)
1677 break;
1678 }
1679
1680 return ret;
1681}
1682
1683static void
1684nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl)
1685{
1686 int i;
1687
1688 for (i = 1; i < ctrl->queue_count; i++)
1689 nvme_fc_init_queue(ctrl, i, ctrl->ctrl.sqsize);
1690}
1691
1692static void
1693nvme_fc_ctrl_free(struct kref *ref)
1694{
1695 struct nvme_fc_ctrl *ctrl =
1696 container_of(ref, struct nvme_fc_ctrl, ref);
1697 unsigned long flags;
1698
James Smart61bff8e2017-04-23 08:30:08 -07001699 if (ctrl->ctrl.tagset) {
1700 blk_cleanup_queue(ctrl->ctrl.connect_q);
1701 blk_mq_free_tag_set(&ctrl->tag_set);
James Smarte3994412016-12-02 00:28:42 -08001702 }
1703
James Smart61bff8e2017-04-23 08:30:08 -07001704 /* remove from rport list */
1705 spin_lock_irqsave(&ctrl->rport->lock, flags);
1706 list_del(&ctrl->ctrl_list);
1707 spin_unlock_irqrestore(&ctrl->rport->lock, flags);
1708
1709 blk_cleanup_queue(ctrl->ctrl.admin_q);
1710 blk_mq_free_tag_set(&ctrl->admin_tag_set);
1711
1712 kfree(ctrl->queues);
1713
James Smarte3994412016-12-02 00:28:42 -08001714 put_device(ctrl->dev);
1715 nvme_fc_rport_put(ctrl->rport);
1716
James Smarte3994412016-12-02 00:28:42 -08001717 ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
Ewan D. Milnede414472017-04-24 13:24:16 -04001718 if (ctrl->ctrl.opts)
1719 nvmf_free_options(ctrl->ctrl.opts);
James Smarte3994412016-12-02 00:28:42 -08001720 kfree(ctrl);
1721}
1722
1723static void
1724nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl)
1725{
1726 kref_put(&ctrl->ref, nvme_fc_ctrl_free);
1727}
1728
1729static int
1730nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
1731{
1732 return kref_get_unless_zero(&ctrl->ref);
1733}
1734
1735/*
1736 * All accesses from nvme core layer done - can now free the
1737 * controller. Called after last nvme_put_ctrl() call
1738 */
1739static void
James Smart61bff8e2017-04-23 08:30:08 -07001740nvme_fc_nvme_ctrl_freed(struct nvme_ctrl *nctrl)
James Smarte3994412016-12-02 00:28:42 -08001741{
1742 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
1743
1744 WARN_ON(nctrl != &ctrl->ctrl);
1745
James Smart61bff8e2017-04-23 08:30:08 -07001746 nvme_fc_ctrl_put(ctrl);
1747}
James Smarte3994412016-12-02 00:28:42 -08001748
James Smart61bff8e2017-04-23 08:30:08 -07001749static void
1750nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
1751{
1752 dev_warn(ctrl->ctrl.device,
1753 "NVME-FC{%d}: transport association error detected: %s\n",
1754 ctrl->cnum, errmsg);
James Smart589ff772017-05-15 17:10:19 -07001755 dev_warn(ctrl->ctrl.device,
James Smart61bff8e2017-04-23 08:30:08 -07001756 "NVME-FC{%d}: resetting controller\n", ctrl->cnum);
James Smarte3994412016-12-02 00:28:42 -08001757
James Smart2952a872017-04-25 15:32:01 -07001758 /* stop the queues on error, cleanup is in reset thread */
1759 if (ctrl->queue_count > 1)
1760 nvme_stop_queues(&ctrl->ctrl);
1761
James Smart61bff8e2017-04-23 08:30:08 -07001762 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING)) {
1763 dev_err(ctrl->ctrl.device,
1764 "NVME-FC{%d}: error_recovery: Couldn't change state "
1765 "to RECONNECTING\n", ctrl->cnum);
1766 return;
James Smarte3994412016-12-02 00:28:42 -08001767 }
1768
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001769 nvme_reset_ctrl(&ctrl->ctrl);
James Smarte3994412016-12-02 00:28:42 -08001770}
1771
Christoph Hellwigbaee29a2017-04-21 10:44:06 +02001772static enum blk_eh_timer_return
James Smarte3994412016-12-02 00:28:42 -08001773nvme_fc_timeout(struct request *rq, bool reserved)
1774{
1775 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1776 struct nvme_fc_ctrl *ctrl = op->ctrl;
1777 int ret;
1778
1779 if (reserved)
1780 return BLK_EH_RESET_TIMER;
1781
1782 ret = __nvme_fc_abort_op(ctrl, op);
1783 if (ret)
1784 /* io wasn't active to abort consider it done */
1785 return BLK_EH_HANDLED;
1786
1787 /*
James Smart61bff8e2017-04-23 08:30:08 -07001788 * we can't individually ABTS an io without affecting the queue,
1789 * thus killing the queue, adn thus the association.
1790 * So resolve by performing a controller reset, which will stop
1791 * the host/io stack, terminate the association on the link,
1792 * and recreate an association on the link.
James Smarte3994412016-12-02 00:28:42 -08001793 */
James Smart61bff8e2017-04-23 08:30:08 -07001794 nvme_fc_error_recovery(ctrl, "io timeout error");
James Smarte3994412016-12-02 00:28:42 -08001795
1796 return BLK_EH_HANDLED;
1797}
1798
1799static int
1800nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
1801 struct nvme_fc_fcp_op *op)
1802{
1803 struct nvmefc_fcp_req *freq = &op->fcp_req;
James Smarte3994412016-12-02 00:28:42 -08001804 enum dma_data_direction dir;
1805 int ret;
1806
1807 freq->sg_cnt = 0;
1808
Christoph Hellwigb131c612017-01-13 12:29:12 +01001809 if (!blk_rq_payload_bytes(rq))
James Smarte3994412016-12-02 00:28:42 -08001810 return 0;
1811
1812 freq->sg_table.sgl = freq->first_sgl;
Christoph Hellwig19e420b2017-01-19 16:55:57 +01001813 ret = sg_alloc_table_chained(&freq->sg_table,
1814 blk_rq_nr_phys_segments(rq), freq->sg_table.sgl);
James Smarte3994412016-12-02 00:28:42 -08001815 if (ret)
1816 return -ENOMEM;
1817
1818 op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl);
Christoph Hellwig19e420b2017-01-19 16:55:57 +01001819 WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
James Smarte3994412016-12-02 00:28:42 -08001820 dir = (rq_data_dir(rq) == WRITE) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
1821 freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
1822 op->nents, dir);
1823 if (unlikely(freq->sg_cnt <= 0)) {
1824 sg_free_table_chained(&freq->sg_table, true);
1825 freq->sg_cnt = 0;
1826 return -EFAULT;
1827 }
1828
1829 /*
1830 * TODO: blk_integrity_rq(rq) for DIF
1831 */
1832 return 0;
1833}
1834
1835static void
1836nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
1837 struct nvme_fc_fcp_op *op)
1838{
1839 struct nvmefc_fcp_req *freq = &op->fcp_req;
1840
1841 if (!freq->sg_cnt)
1842 return;
1843
1844 fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
1845 ((rq_data_dir(rq) == WRITE) ?
1846 DMA_TO_DEVICE : DMA_FROM_DEVICE));
1847
1848 nvme_cleanup_cmd(rq);
1849
1850 sg_free_table_chained(&freq->sg_table, true);
1851
1852 freq->sg_cnt = 0;
1853}
1854
1855/*
1856 * In FC, the queue is a logical thing. At transport connect, the target
1857 * creates its "queue" and returns a handle that is to be given to the
1858 * target whenever it posts something to the corresponding SQ. When an
1859 * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
1860 * command contained within the SQE, an io, and assigns a FC exchange
1861 * to it. The SQE and the associated SQ handle are sent in the initial
1862 * CMD IU sents on the exchange. All transfers relative to the io occur
1863 * as part of the exchange. The CQE is the last thing for the io,
1864 * which is transferred (explicitly or implicitly) with the RSP IU
1865 * sent on the exchange. After the CQE is received, the FC exchange is
1866 * terminaed and the Exchange may be used on a different io.
1867 *
1868 * The transport to LLDD api has the transport making a request for a
1869 * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
1870 * resource and transfers the command. The LLDD will then process all
1871 * steps to complete the io. Upon completion, the transport done routine
1872 * is called.
1873 *
1874 * So - while the operation is outstanding to the LLDD, there is a link
1875 * level FC exchange resource that is also outstanding. This must be
1876 * considered in all cleanup operations.
1877 */
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001878static blk_status_t
James Smarte3994412016-12-02 00:28:42 -08001879nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
1880 struct nvme_fc_fcp_op *op, u32 data_len,
1881 enum nvmefc_fcp_datadir io_dir)
1882{
1883 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1884 struct nvme_command *sqe = &cmdiu->sqe;
1885 u32 csn;
1886 int ret;
1887
James Smart61bff8e2017-04-23 08:30:08 -07001888 /*
1889 * before attempting to send the io, check to see if we believe
1890 * the target device is present
1891 */
1892 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001893 return BLK_STS_IOERR;
James Smart61bff8e2017-04-23 08:30:08 -07001894
James Smarte3994412016-12-02 00:28:42 -08001895 if (!nvme_fc_ctrl_get(ctrl))
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001896 return BLK_STS_IOERR;
James Smarte3994412016-12-02 00:28:42 -08001897
1898 /* format the FC-NVME CMD IU and fcp_req */
1899 cmdiu->connection_id = cpu_to_be64(queue->connection_id);
1900 csn = atomic_inc_return(&queue->csn);
1901 cmdiu->csn = cpu_to_be32(csn);
1902 cmdiu->data_len = cpu_to_be32(data_len);
1903 switch (io_dir) {
1904 case NVMEFC_FCP_WRITE:
1905 cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
1906 break;
1907 case NVMEFC_FCP_READ:
1908 cmdiu->flags = FCNVME_CMD_FLAGS_READ;
1909 break;
1910 case NVMEFC_FCP_NODATA:
1911 cmdiu->flags = 0;
1912 break;
1913 }
1914 op->fcp_req.payload_length = data_len;
1915 op->fcp_req.io_dir = io_dir;
1916 op->fcp_req.transferred_length = 0;
1917 op->fcp_req.rcv_rsplen = 0;
James Smart62eeacb2017-03-23 20:41:27 -07001918 op->fcp_req.status = NVME_SC_SUCCESS;
James Smarte3994412016-12-02 00:28:42 -08001919 op->fcp_req.sqid = cpu_to_le16(queue->qnum);
1920
1921 /*
1922 * validate per fabric rules, set fields mandated by fabric spec
1923 * as well as those by FC-NVME spec.
1924 */
1925 WARN_ON_ONCE(sqe->common.metadata);
1926 WARN_ON_ONCE(sqe->common.dptr.prp1);
1927 WARN_ON_ONCE(sqe->common.dptr.prp2);
1928 sqe->common.flags |= NVME_CMD_SGL_METABUF;
1929
1930 /*
1931 * format SQE DPTR field per FC-NVME rules
1932 * type=data block descr; subtype=offset;
1933 * offset is currently 0.
1934 */
1935 sqe->rw.dptr.sgl.type = NVME_SGL_FMT_OFFSET;
1936 sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
1937 sqe->rw.dptr.sgl.addr = 0;
1938
James Smart78a7ac22017-04-23 08:30:07 -07001939 if (!(op->flags & FCOP_FLAGS_AEN)) {
James Smarte3994412016-12-02 00:28:42 -08001940 ret = nvme_fc_map_data(ctrl, op->rq, op);
1941 if (ret < 0) {
James Smarte3994412016-12-02 00:28:42 -08001942 nvme_cleanup_cmd(op->rq);
1943 nvme_fc_ctrl_put(ctrl);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001944 if (ret == -ENOMEM || ret == -EAGAIN)
1945 return BLK_STS_RESOURCE;
1946 return BLK_STS_IOERR;
James Smarte3994412016-12-02 00:28:42 -08001947 }
1948 }
1949
1950 fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
1951 sizeof(op->cmd_iu), DMA_TO_DEVICE);
1952
1953 atomic_set(&op->state, FCPOP_STATE_ACTIVE);
1954
James Smart78a7ac22017-04-23 08:30:07 -07001955 if (!(op->flags & FCOP_FLAGS_AEN))
James Smarte3994412016-12-02 00:28:42 -08001956 blk_mq_start_request(op->rq);
1957
1958 ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
1959 &ctrl->rport->remoteport,
1960 queue->lldd_handle, &op->fcp_req);
1961
1962 if (ret) {
James Smartb4dfd6e2017-06-21 17:43:05 -07001963 if (op->rq) /* normal request */
James Smarte3994412016-12-02 00:28:42 -08001964 nvme_fc_unmap_data(ctrl, op->rq, op);
James Smarte3994412016-12-02 00:28:42 -08001965 /* else - aen. no cleanup needed */
1966
1967 nvme_fc_ctrl_put(ctrl);
1968
1969 if (ret != -EBUSY)
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001970 return BLK_STS_IOERR;
James Smarte3994412016-12-02 00:28:42 -08001971
1972 if (op->rq) {
1973 blk_mq_stop_hw_queues(op->rq->q);
1974 blk_mq_delay_queue(queue->hctx, NVMEFC_QUEUE_DELAY);
1975 }
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001976 return BLK_STS_RESOURCE;
James Smarte3994412016-12-02 00:28:42 -08001977 }
1978
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001979 return BLK_STS_OK;
James Smarte3994412016-12-02 00:28:42 -08001980}
1981
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001982static blk_status_t
James Smarte3994412016-12-02 00:28:42 -08001983nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
1984 const struct blk_mq_queue_data *bd)
1985{
1986 struct nvme_ns *ns = hctx->queue->queuedata;
1987 struct nvme_fc_queue *queue = hctx->driver_data;
1988 struct nvme_fc_ctrl *ctrl = queue->ctrl;
1989 struct request *rq = bd->rq;
1990 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1991 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1992 struct nvme_command *sqe = &cmdiu->sqe;
1993 enum nvmefc_fcp_datadir io_dir;
1994 u32 data_len;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001995 blk_status_t ret;
James Smarte3994412016-12-02 00:28:42 -08001996
1997 ret = nvme_setup_cmd(ns, rq, sqe);
1998 if (ret)
1999 return ret;
2000
Christoph Hellwigb131c612017-01-13 12:29:12 +01002001 data_len = blk_rq_payload_bytes(rq);
James Smarte3994412016-12-02 00:28:42 -08002002 if (data_len)
2003 io_dir = ((rq_data_dir(rq) == WRITE) ?
2004 NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
2005 else
2006 io_dir = NVMEFC_FCP_NODATA;
2007
2008 return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
2009}
2010
2011static struct blk_mq_tags *
2012nvme_fc_tagset(struct nvme_fc_queue *queue)
2013{
2014 if (queue->qnum == 0)
2015 return queue->ctrl->admin_tag_set.tags[queue->qnum];
2016
2017 return queue->ctrl->tag_set.tags[queue->qnum - 1];
2018}
2019
2020static int
2021nvme_fc_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
2022
2023{
2024 struct nvme_fc_queue *queue = hctx->driver_data;
2025 struct nvme_fc_ctrl *ctrl = queue->ctrl;
2026 struct request *req;
2027 struct nvme_fc_fcp_op *op;
2028
2029 req = blk_mq_tag_to_rq(nvme_fc_tagset(queue), tag);
James Smart61bff8e2017-04-23 08:30:08 -07002030 if (!req)
James Smarte3994412016-12-02 00:28:42 -08002031 return 0;
James Smarte3994412016-12-02 00:28:42 -08002032
2033 op = blk_mq_rq_to_pdu(req);
2034
2035 if ((atomic_read(&op->state) == FCPOP_STATE_ACTIVE) &&
2036 (ctrl->lport->ops->poll_queue))
2037 ctrl->lport->ops->poll_queue(&ctrl->lport->localport,
2038 queue->lldd_handle);
2039
2040 return ((atomic_read(&op->state) != FCPOP_STATE_ACTIVE));
2041}
2042
2043static void
2044nvme_fc_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
2045{
2046 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
2047 struct nvme_fc_fcp_op *aen_op;
James Smart61bff8e2017-04-23 08:30:08 -07002048 unsigned long flags;
2049 bool terminating = false;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002050 blk_status_t ret;
James Smarte3994412016-12-02 00:28:42 -08002051
2052 if (aer_idx > NVME_FC_NR_AEN_COMMANDS)
2053 return;
2054
James Smart61bff8e2017-04-23 08:30:08 -07002055 spin_lock_irqsave(&ctrl->lock, flags);
2056 if (ctrl->flags & FCCTRL_TERMIO)
2057 terminating = true;
2058 spin_unlock_irqrestore(&ctrl->lock, flags);
2059
2060 if (terminating)
2061 return;
2062
James Smarte3994412016-12-02 00:28:42 -08002063 aen_op = &ctrl->aen_ops[aer_idx];
2064
2065 ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
2066 NVMEFC_FCP_NODATA);
2067 if (ret)
2068 dev_err(ctrl->ctrl.device,
2069 "failed async event work [%d]\n", aer_idx);
2070}
2071
2072static void
James Smart78a7ac22017-04-23 08:30:07 -07002073__nvme_fc_final_op_cleanup(struct request *rq)
James Smarte3994412016-12-02 00:28:42 -08002074{
2075 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2076 struct nvme_fc_ctrl *ctrl = op->ctrl;
James Smarte3994412016-12-02 00:28:42 -08002077
James Smart78a7ac22017-04-23 08:30:07 -07002078 atomic_set(&op->state, FCPOP_STATE_IDLE);
2079 op->flags &= ~(FCOP_FLAGS_TERMIO | FCOP_FLAGS_RELEASED |
2080 FCOP_FLAGS_COMPLETE);
James Smarte3994412016-12-02 00:28:42 -08002081
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);
James Smart36715cf2017-05-22 15:28:42 -07002482 wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock);
James Smart61bff8e2017-04-23 08:30:08 -07002483 ctrl->flags &= ~FCCTRL_TERMIO;
2484 spin_unlock_irqrestore(&ctrl->lock, flags);
2485
2486 nvme_fc_term_aen_ops(ctrl);
2487
2488 /*
2489 * send a Disconnect(association) LS to fc-nvme target
2490 * Note: could have been sent at top of process, but
2491 * cleaner on link traffic if after the aborts complete.
2492 * Note: if association doesn't exist, association_id will be 0
2493 */
2494 if (ctrl->association_id)
2495 nvme_fc_xmt_disconnect_assoc(ctrl);
2496
2497 if (ctrl->ctrl.tagset) {
2498 nvme_fc_delete_hw_io_queues(ctrl);
2499 nvme_fc_free_io_queues(ctrl);
2500 }
2501
2502 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
2503 nvme_fc_free_queue(&ctrl->queues[0]);
2504}
2505
2506static void
2507nvme_fc_delete_ctrl_work(struct work_struct *work)
2508{
2509 struct nvme_fc_ctrl *ctrl =
2510 container_of(work, struct nvme_fc_ctrl, delete_work);
2511
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002512 cancel_work_sync(&ctrl->ctrl.reset_work);
James Smart61bff8e2017-04-23 08:30:08 -07002513 cancel_delayed_work_sync(&ctrl->connect_work);
2514
2515 /*
2516 * kill the association on the link side. this will block
2517 * waiting for io to terminate
2518 */
2519 nvme_fc_delete_association(ctrl);
2520
2521 /*
2522 * tear down the controller
James Smarta5321aa2017-05-15 17:10:18 -07002523 * After the last reference on the nvme ctrl is removed,
2524 * the transport nvme_fc_nvme_ctrl_freed() callback will be
2525 * invoked. From there, the transport will tear down it's
2526 * logical queues and association.
James Smart61bff8e2017-04-23 08:30:08 -07002527 */
2528 nvme_uninit_ctrl(&ctrl->ctrl);
2529
2530 nvme_put_ctrl(&ctrl->ctrl);
2531}
2532
James Smart5bbecdb2017-05-15 17:10:16 -07002533static bool
2534__nvme_fc_schedule_delete_work(struct nvme_fc_ctrl *ctrl)
2535{
2536 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
2537 return true;
2538
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002539 if (!queue_work(nvme_wq, &ctrl->delete_work))
James Smart5bbecdb2017-05-15 17:10:16 -07002540 return true;
2541
2542 return false;
2543}
2544
James Smart61bff8e2017-04-23 08:30:08 -07002545static int
2546__nvme_fc_del_ctrl(struct nvme_fc_ctrl *ctrl)
2547{
James Smart5bbecdb2017-05-15 17:10:16 -07002548 return __nvme_fc_schedule_delete_work(ctrl) ? -EBUSY : 0;
James Smart61bff8e2017-04-23 08:30:08 -07002549}
2550
2551/*
2552 * Request from nvme core layer to delete the controller
2553 */
2554static int
2555nvme_fc_del_nvme_ctrl(struct nvme_ctrl *nctrl)
2556{
2557 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2558 int ret;
2559
2560 if (!kref_get_unless_zero(&ctrl->ctrl.kref))
2561 return -EBUSY;
2562
2563 ret = __nvme_fc_del_ctrl(ctrl);
2564
2565 if (!ret)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002566 flush_workqueue(nvme_wq);
James Smart61bff8e2017-04-23 08:30:08 -07002567
2568 nvme_put_ctrl(&ctrl->ctrl);
2569
2570 return ret;
2571}
2572
2573static void
James Smart5bbecdb2017-05-15 17:10:16 -07002574nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status)
2575{
2576 /* If we are resetting/deleting then do nothing */
2577 if (ctrl->ctrl.state != NVME_CTRL_RECONNECTING) {
2578 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
2579 ctrl->ctrl.state == NVME_CTRL_LIVE);
2580 return;
2581 }
2582
James Smart589ff772017-05-15 17:10:19 -07002583 dev_info(ctrl->ctrl.device,
James Smart5bbecdb2017-05-15 17:10:16 -07002584 "NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
2585 ctrl->cnum, status);
2586
2587 if (nvmf_should_reconnect(&ctrl->ctrl)) {
2588 dev_info(ctrl->ctrl.device,
2589 "NVME-FC{%d}: Reconnect attempt in %d seconds.\n",
2590 ctrl->cnum, ctrl->ctrl.opts->reconnect_delay);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002591 queue_delayed_work(nvme_wq, &ctrl->connect_work,
James Smart5bbecdb2017-05-15 17:10:16 -07002592 ctrl->ctrl.opts->reconnect_delay * HZ);
2593 } else {
James Smart589ff772017-05-15 17:10:19 -07002594 dev_warn(ctrl->ctrl.device,
James Smart5bbecdb2017-05-15 17:10:16 -07002595 "NVME-FC{%d}: Max reconnect attempts (%d) "
2596 "reached. Removing controller\n",
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +03002597 ctrl->cnum, ctrl->ctrl.nr_reconnects);
James Smart5bbecdb2017-05-15 17:10:16 -07002598 WARN_ON(__nvme_fc_schedule_delete_work(ctrl));
2599 }
2600}
2601
2602static void
James Smart61bff8e2017-04-23 08:30:08 -07002603nvme_fc_reset_ctrl_work(struct work_struct *work)
2604{
2605 struct nvme_fc_ctrl *ctrl =
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002606 container_of(work, struct nvme_fc_ctrl, ctrl.reset_work);
James Smart61bff8e2017-04-23 08:30:08 -07002607 int ret;
2608
2609 /* will block will waiting for io to terminate */
2610 nvme_fc_delete_association(ctrl);
2611
2612 ret = nvme_fc_create_association(ctrl);
James Smart5bbecdb2017-05-15 17:10:16 -07002613 if (ret)
2614 nvme_fc_reconnect_or_delete(ctrl, ret);
2615 else
James Smart61bff8e2017-04-23 08:30:08 -07002616 dev_info(ctrl->ctrl.device,
2617 "NVME-FC{%d}: controller reset complete\n", ctrl->cnum);
2618}
2619
James Smart61bff8e2017-04-23 08:30:08 -07002620static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
2621 .name = "fc",
2622 .module = THIS_MODULE,
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02002623 .flags = NVME_F_FABRICS,
James Smart61bff8e2017-04-23 08:30:08 -07002624 .reg_read32 = nvmf_reg_read32,
2625 .reg_read64 = nvmf_reg_read64,
2626 .reg_write32 = nvmf_reg_write32,
James Smart61bff8e2017-04-23 08:30:08 -07002627 .free_ctrl = nvme_fc_nvme_ctrl_freed,
2628 .submit_async_event = nvme_fc_submit_async_event,
2629 .delete_ctrl = nvme_fc_del_nvme_ctrl,
James Smart61bff8e2017-04-23 08:30:08 -07002630 .get_address = nvmf_get_address,
2631};
2632
2633static void
2634nvme_fc_connect_ctrl_work(struct work_struct *work)
2635{
2636 int ret;
2637
2638 struct nvme_fc_ctrl *ctrl =
2639 container_of(to_delayed_work(work),
2640 struct nvme_fc_ctrl, connect_work);
2641
2642 ret = nvme_fc_create_association(ctrl);
James Smart5bbecdb2017-05-15 17:10:16 -07002643 if (ret)
2644 nvme_fc_reconnect_or_delete(ctrl, ret);
2645 else
James Smart61bff8e2017-04-23 08:30:08 -07002646 dev_info(ctrl->ctrl.device,
2647 "NVME-FC{%d}: controller reconnect complete\n",
2648 ctrl->cnum);
2649}
2650
2651
2652static const struct blk_mq_ops nvme_fc_admin_mq_ops = {
2653 .queue_rq = nvme_fc_queue_rq,
2654 .complete = nvme_fc_complete_rq,
Christoph Hellwig76f983c2017-06-13 09:15:20 +02002655 .init_request = nvme_fc_init_request,
James Smart61bff8e2017-04-23 08:30:08 -07002656 .exit_request = nvme_fc_exit_request,
2657 .reinit_request = nvme_fc_reinit_request,
2658 .init_hctx = nvme_fc_init_admin_hctx,
2659 .timeout = nvme_fc_timeout,
2660};
2661
James Smarte3994412016-12-02 00:28:42 -08002662
2663static struct nvme_ctrl *
James Smart61bff8e2017-04-23 08:30:08 -07002664nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
James Smarte3994412016-12-02 00:28:42 -08002665 struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
2666{
2667 struct nvme_fc_ctrl *ctrl;
2668 unsigned long flags;
2669 int ret, idx;
James Smarte3994412016-12-02 00:28:42 -08002670
James Smart85e6a6a2017-05-05 16:13:15 -07002671 if (!(rport->remoteport.port_role &
2672 (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) {
2673 ret = -EBADR;
2674 goto out_fail;
2675 }
2676
James Smarte3994412016-12-02 00:28:42 -08002677 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
2678 if (!ctrl) {
2679 ret = -ENOMEM;
2680 goto out_fail;
2681 }
2682
2683 idx = ida_simple_get(&nvme_fc_ctrl_cnt, 0, 0, GFP_KERNEL);
2684 if (idx < 0) {
2685 ret = -ENOSPC;
2686 goto out_free_ctrl;
2687 }
2688
2689 ctrl->ctrl.opts = opts;
2690 INIT_LIST_HEAD(&ctrl->ctrl_list);
James Smarte3994412016-12-02 00:28:42 -08002691 ctrl->lport = lport;
2692 ctrl->rport = rport;
2693 ctrl->dev = lport->dev;
James Smarte3994412016-12-02 00:28:42 -08002694 ctrl->cnum = idx;
2695
James Smarte3994412016-12-02 00:28:42 -08002696 get_device(ctrl->dev);
2697 kref_init(&ctrl->ref);
2698
James Smart61bff8e2017-04-23 08:30:08 -07002699 INIT_WORK(&ctrl->delete_work, nvme_fc_delete_ctrl_work);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002700 INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work);
James Smart61bff8e2017-04-23 08:30:08 -07002701 INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
James Smarte3994412016-12-02 00:28:42 -08002702 spin_lock_init(&ctrl->lock);
2703
2704 /* io queue count */
2705 ctrl->queue_count = min_t(unsigned int,
2706 opts->nr_io_queues,
2707 lport->ops->max_hw_queues);
2708 opts->nr_io_queues = ctrl->queue_count; /* so opts has valid value */
2709 ctrl->queue_count++; /* +1 for admin queue */
2710
2711 ctrl->ctrl.sqsize = opts->queue_size - 1;
2712 ctrl->ctrl.kato = opts->kato;
2713
2714 ret = -ENOMEM;
2715 ctrl->queues = kcalloc(ctrl->queue_count, sizeof(struct nvme_fc_queue),
2716 GFP_KERNEL);
2717 if (!ctrl->queues)
James Smart61bff8e2017-04-23 08:30:08 -07002718 goto out_free_ida;
James Smarte3994412016-12-02 00:28:42 -08002719
James Smart61bff8e2017-04-23 08:30:08 -07002720 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
2721 ctrl->admin_tag_set.ops = &nvme_fc_admin_mq_ops;
2722 ctrl->admin_tag_set.queue_depth = NVME_FC_AQ_BLKMQ_DEPTH;
2723 ctrl->admin_tag_set.reserved_tags = 2; /* fabric connect + Keep-Alive */
2724 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
2725 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) +
2726 (SG_CHUNK_SIZE *
2727 sizeof(struct scatterlist)) +
2728 ctrl->lport->ops->fcprqst_priv_sz;
2729 ctrl->admin_tag_set.driver_data = ctrl;
2730 ctrl->admin_tag_set.nr_hw_queues = 1;
2731 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
2732
2733 ret = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
James Smarte3994412016-12-02 00:28:42 -08002734 if (ret)
James Smart61bff8e2017-04-23 08:30:08 -07002735 goto out_free_queues;
James Smarte3994412016-12-02 00:28:42 -08002736
James Smart61bff8e2017-04-23 08:30:08 -07002737 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
2738 if (IS_ERR(ctrl->ctrl.admin_q)) {
2739 ret = PTR_ERR(ctrl->ctrl.admin_q);
2740 goto out_free_admin_tag_set;
James Smarte3994412016-12-02 00:28:42 -08002741 }
2742
James Smart61bff8e2017-04-23 08:30:08 -07002743 /*
2744 * Would have been nice to init io queues tag set as well.
2745 * However, we require interaction from the controller
2746 * for max io queue count before we can do so.
2747 * Defer this to the connect path.
2748 */
James Smarte3994412016-12-02 00:28:42 -08002749
James Smart61bff8e2017-04-23 08:30:08 -07002750 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
James Smarte3994412016-12-02 00:28:42 -08002751 if (ret)
James Smart61bff8e2017-04-23 08:30:08 -07002752 goto out_cleanup_admin_q;
James Smarte3994412016-12-02 00:28:42 -08002753
James Smart61bff8e2017-04-23 08:30:08 -07002754 /* at this point, teardown path changes to ref counting on nvme ctrl */
James Smarte3994412016-12-02 00:28:42 -08002755
2756 spin_lock_irqsave(&rport->lock, flags);
2757 list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
2758 spin_unlock_irqrestore(&rport->lock, flags);
2759
James Smart61bff8e2017-04-23 08:30:08 -07002760 ret = nvme_fc_create_association(ctrl);
2761 if (ret) {
Ewan D. Milnede414472017-04-24 13:24:16 -04002762 ctrl->ctrl.opts = NULL;
James Smart61bff8e2017-04-23 08:30:08 -07002763 /* initiate nvme ctrl ref counting teardown */
2764 nvme_uninit_ctrl(&ctrl->ctrl);
James Smart24b7f052017-06-05 15:03:42 -07002765 nvme_put_ctrl(&ctrl->ctrl);
James Smart61bff8e2017-04-23 08:30:08 -07002766
2767 /* as we're past the point where we transition to the ref
2768 * counting teardown path, if we return a bad pointer here,
2769 * the calling routine, thinking it's prior to the
2770 * transition, will do an rport put. Since the teardown
2771 * path also does a rport put, we do an extra get here to
2772 * so proper order/teardown happens.
2773 */
2774 nvme_fc_rport_get(rport);
2775
2776 if (ret > 0)
2777 ret = -EIO;
2778 return ERR_PTR(ret);
James Smarte3994412016-12-02 00:28:42 -08002779 }
2780
James Smart2cb657b2017-05-15 17:10:22 -07002781 kref_get(&ctrl->ctrl.kref);
2782
James Smart61bff8e2017-04-23 08:30:08 -07002783 dev_info(ctrl->ctrl.device,
2784 "NVME-FC{%d}: new ctrl: NQN \"%s\"\n",
2785 ctrl->cnum, ctrl->ctrl.opts->subsysnqn);
2786
James Smarte3994412016-12-02 00:28:42 -08002787 return &ctrl->ctrl;
2788
James Smart61bff8e2017-04-23 08:30:08 -07002789out_cleanup_admin_q:
2790 blk_cleanup_queue(ctrl->ctrl.admin_q);
2791out_free_admin_tag_set:
2792 blk_mq_free_tag_set(&ctrl->admin_tag_set);
2793out_free_queues:
2794 kfree(ctrl->queues);
James Smarte3994412016-12-02 00:28:42 -08002795out_free_ida:
James Smart61bff8e2017-04-23 08:30:08 -07002796 put_device(ctrl->dev);
James Smarte3994412016-12-02 00:28:42 -08002797 ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
2798out_free_ctrl:
2799 kfree(ctrl);
2800out_fail:
James Smarte3994412016-12-02 00:28:42 -08002801 /* exit via here doesn't follow ctlr ref points */
2802 return ERR_PTR(ret);
2803}
2804
2805enum {
2806 FCT_TRADDR_ERR = 0,
2807 FCT_TRADDR_WWNN = 1 << 0,
2808 FCT_TRADDR_WWPN = 1 << 1,
2809};
2810
2811struct nvmet_fc_traddr {
2812 u64 nn;
2813 u64 pn;
2814};
2815
2816static const match_table_t traddr_opt_tokens = {
2817 { FCT_TRADDR_WWNN, "nn-%s" },
2818 { FCT_TRADDR_WWPN, "pn-%s" },
2819 { FCT_TRADDR_ERR, NULL }
2820};
2821
2822static int
2823nvme_fc_parse_address(struct nvmet_fc_traddr *traddr, char *buf)
2824{
2825 substring_t args[MAX_OPT_ARGS];
2826 char *options, *o, *p;
2827 int token, ret = 0;
2828 u64 token64;
2829
2830 options = o = kstrdup(buf, GFP_KERNEL);
2831 if (!options)
2832 return -ENOMEM;
2833
2834 while ((p = strsep(&o, ":\n")) != NULL) {
2835 if (!*p)
2836 continue;
2837
2838 token = match_token(p, traddr_opt_tokens, args);
2839 switch (token) {
2840 case FCT_TRADDR_WWNN:
2841 if (match_u64(args, &token64)) {
2842 ret = -EINVAL;
2843 goto out;
2844 }
2845 traddr->nn = token64;
2846 break;
2847 case FCT_TRADDR_WWPN:
2848 if (match_u64(args, &token64)) {
2849 ret = -EINVAL;
2850 goto out;
2851 }
2852 traddr->pn = token64;
2853 break;
2854 default:
2855 pr_warn("unknown traddr token or missing value '%s'\n",
2856 p);
2857 ret = -EINVAL;
2858 goto out;
2859 }
2860 }
2861
2862out:
2863 kfree(options);
2864 return ret;
2865}
2866
2867static struct nvme_ctrl *
2868nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
2869{
2870 struct nvme_fc_lport *lport;
2871 struct nvme_fc_rport *rport;
James Smart61bff8e2017-04-23 08:30:08 -07002872 struct nvme_ctrl *ctrl;
James Smarte3994412016-12-02 00:28:42 -08002873 struct nvmet_fc_traddr laddr = { 0L, 0L };
2874 struct nvmet_fc_traddr raddr = { 0L, 0L };
2875 unsigned long flags;
2876 int ret;
2877
2878 ret = nvme_fc_parse_address(&raddr, opts->traddr);
2879 if (ret || !raddr.nn || !raddr.pn)
2880 return ERR_PTR(-EINVAL);
2881
2882 ret = nvme_fc_parse_address(&laddr, opts->host_traddr);
2883 if (ret || !laddr.nn || !laddr.pn)
2884 return ERR_PTR(-EINVAL);
2885
2886 /* find the host and remote ports to connect together */
2887 spin_lock_irqsave(&nvme_fc_lock, flags);
2888 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
2889 if (lport->localport.node_name != laddr.nn ||
2890 lport->localport.port_name != laddr.pn)
2891 continue;
2892
2893 list_for_each_entry(rport, &lport->endp_list, endp_list) {
2894 if (rport->remoteport.node_name != raddr.nn ||
2895 rport->remoteport.port_name != raddr.pn)
2896 continue;
2897
2898 /* if fail to get reference fall through. Will error */
2899 if (!nvme_fc_rport_get(rport))
2900 break;
2901
2902 spin_unlock_irqrestore(&nvme_fc_lock, flags);
2903
James Smart61bff8e2017-04-23 08:30:08 -07002904 ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport);
2905 if (IS_ERR(ctrl))
2906 nvme_fc_rport_put(rport);
2907 return ctrl;
James Smarte3994412016-12-02 00:28:42 -08002908 }
2909 }
2910 spin_unlock_irqrestore(&nvme_fc_lock, flags);
2911
2912 return ERR_PTR(-ENOENT);
2913}
2914
2915
2916static struct nvmf_transport_ops nvme_fc_transport = {
2917 .name = "fc",
2918 .required_opts = NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
James Smart5bbecdb2017-05-15 17:10:16 -07002919 .allowed_opts = NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO,
James Smarte3994412016-12-02 00:28:42 -08002920 .create_ctrl = nvme_fc_create_ctrl,
2921};
2922
2923static int __init nvme_fc_init_module(void)
2924{
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002925 return nvmf_register_transport(&nvme_fc_transport);
James Smarte3994412016-12-02 00:28:42 -08002926}
2927
2928static void __exit nvme_fc_exit_module(void)
2929{
2930 /* sanity check - all lports should be removed */
2931 if (!list_empty(&nvme_fc_lport_list))
2932 pr_warn("%s: localport list not empty\n", __func__);
2933
2934 nvmf_unregister_transport(&nvme_fc_transport);
2935
James Smarte3994412016-12-02 00:28:42 -08002936 ida_destroy(&nvme_fc_local_port_cnt);
2937 ida_destroy(&nvme_fc_ctrl_cnt);
2938}
2939
2940module_init(nvme_fc_init_module);
2941module_exit(nvme_fc_exit_module);
2942
2943MODULE_LICENSE("GPL v2");