blob: 9d826b726425d11231db947bcb2d9387fd07caa5 [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
James Smarte3994412016-12-02 00:28:42 -080033enum nvme_fc_queue_flags {
Jens Axboe26c0a262017-11-24 10:12:33 -070034 NVME_FC_Q_CONNECTED = 0,
35 NVME_FC_Q_LIVE,
James Smarte3994412016-12-02 00:28:42 -080036};
37
James Smartac7fe822017-10-25 16:43:15 -070038#define NVME_FC_DEFAULT_DEV_LOSS_TMO 60 /* seconds */
39
James Smarte3994412016-12-02 00:28:42 -080040struct nvme_fc_queue {
41 struct nvme_fc_ctrl *ctrl;
42 struct device *dev;
43 struct blk_mq_hw_ctx *hctx;
44 void *lldd_handle;
James Smarte3994412016-12-02 00:28:42 -080045 size_t cmnd_capsule_len;
46 u32 qnum;
47 u32 rqcnt;
48 u32 seqno;
49
50 u64 connection_id;
51 atomic_t csn;
52
53 unsigned long flags;
54} __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
55
James Smart8d64daf2017-04-11 11:35:09 -070056enum nvme_fcop_flags {
57 FCOP_FLAGS_TERMIO = (1 << 0),
James Smartc3aedd222018-02-06 06:48:30 -080058 FCOP_FLAGS_AEN = (1 << 1),
James Smart8d64daf2017-04-11 11:35:09 -070059};
60
James Smarte3994412016-12-02 00:28:42 -080061struct nvmefc_ls_req_op {
62 struct nvmefc_ls_req ls_req;
63
James Smartc913a8b2017-04-11 11:35:08 -070064 struct nvme_fc_rport *rport;
James Smarte3994412016-12-02 00:28:42 -080065 struct nvme_fc_queue *queue;
66 struct request *rq;
James Smart8d64daf2017-04-11 11:35:09 -070067 u32 flags;
James Smarte3994412016-12-02 00:28:42 -080068
69 int ls_error;
70 struct completion ls_done;
James Smartc913a8b2017-04-11 11:35:08 -070071 struct list_head lsreq_list; /* rport->ls_req_list */
James Smarte3994412016-12-02 00:28:42 -080072 bool req_queued;
73};
74
75enum nvme_fcpop_state {
76 FCPOP_STATE_UNINIT = 0,
77 FCPOP_STATE_IDLE = 1,
78 FCPOP_STATE_ACTIVE = 2,
79 FCPOP_STATE_ABORTED = 3,
James Smart78a7ac22017-04-23 08:30:07 -070080 FCPOP_STATE_COMPLETE = 4,
James Smarte3994412016-12-02 00:28:42 -080081};
82
83struct nvme_fc_fcp_op {
84 struct nvme_request nreq; /*
85 * nvme/host/core.c
86 * requires this to be
87 * the 1st element in the
88 * private structure
89 * associated with the
90 * request.
91 */
92 struct nvmefc_fcp_req fcp_req;
93
94 struct nvme_fc_ctrl *ctrl;
95 struct nvme_fc_queue *queue;
96 struct request *rq;
97
98 atomic_t state;
James Smart78a7ac22017-04-23 08:30:07 -070099 u32 flags;
James Smarte3994412016-12-02 00:28:42 -0800100 u32 rqno;
101 u32 nents;
102
103 struct nvme_fc_cmd_iu cmd_iu;
104 struct nvme_fc_ersp_iu rsp_iu;
105};
106
107struct nvme_fc_lport {
108 struct nvme_fc_local_port localport;
109
110 struct ida endp_cnt;
111 struct list_head port_list; /* nvme_fc_port_list */
112 struct list_head endp_list;
113 struct device *dev; /* physical device for dma */
114 struct nvme_fc_port_template *ops;
115 struct kref ref;
James Smart158bfb82017-11-03 08:13:17 -0700116 atomic_t act_rport_cnt;
James Smarte3994412016-12-02 00:28:42 -0800117} __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
118
119struct nvme_fc_rport {
120 struct nvme_fc_remote_port remoteport;
121
122 struct list_head endp_list; /* for lport->endp_list */
123 struct list_head ctrl_list;
James Smartc913a8b2017-04-11 11:35:08 -0700124 struct list_head ls_req_list;
125 struct device *dev; /* physical device for dma */
126 struct nvme_fc_lport *lport;
James Smarte3994412016-12-02 00:28:42 -0800127 spinlock_t lock;
128 struct kref ref;
James Smart158bfb82017-11-03 08:13:17 -0700129 atomic_t act_ctrl_cnt;
James Smart2b632972017-10-25 16:43:17 -0700130 unsigned long dev_loss_end;
James Smarte3994412016-12-02 00:28:42 -0800131} __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
132
James Smart61bff8e2017-04-23 08:30:08 -0700133enum nvme_fcctrl_flags {
134 FCCTRL_TERMIO = (1 << 0),
James Smarte3994412016-12-02 00:28:42 -0800135};
136
137struct nvme_fc_ctrl {
138 spinlock_t lock;
139 struct nvme_fc_queue *queues;
James Smarte3994412016-12-02 00:28:42 -0800140 struct device *dev;
141 struct nvme_fc_lport *lport;
142 struct nvme_fc_rport *rport;
143 u32 cnum;
144
James Smart4c984152018-06-13 14:07:37 -0700145 bool ioq_live;
James Smart158bfb82017-11-03 08:13:17 -0700146 bool assoc_active;
James Smarte3994412016-12-02 00:28:42 -0800147 u64 association_id;
148
James Smarte3994412016-12-02 00:28:42 -0800149 struct list_head ctrl_list; /* rport->ctrl_list */
James Smarte3994412016-12-02 00:28:42 -0800150
151 struct blk_mq_tag_set admin_tag_set;
152 struct blk_mq_tag_set tag_set;
153
James Smart61bff8e2017-04-23 08:30:08 -0700154 struct delayed_work connect_work;
James Smart61bff8e2017-04-23 08:30:08 -0700155
James Smarte3994412016-12-02 00:28:42 -0800156 struct kref ref;
James Smart61bff8e2017-04-23 08:30:08 -0700157 u32 flags;
158 u32 iocnt;
James Smart36715cf2017-05-22 15:28:42 -0700159 wait_queue_head_t ioabort_wait;
James Smarte3994412016-12-02 00:28:42 -0800160
Keith Busch38dabe22017-11-07 15:13:10 -0700161 struct nvme_fc_fcp_op aen_ops[NVME_NR_AEN_COMMANDS];
James Smarte3994412016-12-02 00:28:42 -0800162
163 struct nvme_ctrl ctrl;
164};
165
166static inline struct nvme_fc_ctrl *
167to_fc_ctrl(struct nvme_ctrl *ctrl)
168{
169 return container_of(ctrl, struct nvme_fc_ctrl, ctrl);
170}
171
172static inline struct nvme_fc_lport *
173localport_to_lport(struct nvme_fc_local_port *portptr)
174{
175 return container_of(portptr, struct nvme_fc_lport, localport);
176}
177
178static inline struct nvme_fc_rport *
179remoteport_to_rport(struct nvme_fc_remote_port *portptr)
180{
181 return container_of(portptr, struct nvme_fc_rport, remoteport);
182}
183
184static inline struct nvmefc_ls_req_op *
185ls_req_to_lsop(struct nvmefc_ls_req *lsreq)
186{
187 return container_of(lsreq, struct nvmefc_ls_req_op, ls_req);
188}
189
190static inline struct nvme_fc_fcp_op *
191fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq)
192{
193 return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req);
194}
195
196
197
198/* *************************** Globals **************************** */
199
200
201static DEFINE_SPINLOCK(nvme_fc_lock);
202
203static LIST_HEAD(nvme_fc_lport_list);
204static DEFINE_IDA(nvme_fc_local_port_cnt);
205static DEFINE_IDA(nvme_fc_ctrl_cnt);
206
James Smarte3994412016-12-02 00:28:42 -0800207
208
James Smart5f568552017-09-14 10:38:41 -0700209/*
210 * These items are short-term. They will eventually be moved into
211 * a generic FC class. See comments in module init.
212 */
213static struct class *fc_class;
214static struct device *fc_udev_device;
215
James Smarte3994412016-12-02 00:28:42 -0800216
217/* *********************** FC-NVME Port Management ************************ */
218
James Smarte3994412016-12-02 00:28:42 -0800219static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *,
220 struct nvme_fc_queue *, unsigned int);
221
James Smart5533d422017-07-31 13:20:30 -0700222static void
223nvme_fc_free_lport(struct kref *ref)
224{
225 struct nvme_fc_lport *lport =
226 container_of(ref, struct nvme_fc_lport, ref);
227 unsigned long flags;
228
229 WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED);
230 WARN_ON(!list_empty(&lport->endp_list));
231
232 /* remove from transport list */
233 spin_lock_irqsave(&nvme_fc_lock, flags);
234 list_del(&lport->port_list);
235 spin_unlock_irqrestore(&nvme_fc_lock, flags);
236
James Smart5533d422017-07-31 13:20:30 -0700237 ida_simple_remove(&nvme_fc_local_port_cnt, lport->localport.port_num);
238 ida_destroy(&lport->endp_cnt);
239
240 put_device(lport->dev);
241
242 kfree(lport);
243}
244
245static void
246nvme_fc_lport_put(struct nvme_fc_lport *lport)
247{
248 kref_put(&lport->ref, nvme_fc_free_lport);
249}
250
251static int
252nvme_fc_lport_get(struct nvme_fc_lport *lport)
253{
254 return kref_get_unless_zero(&lport->ref);
255}
256
257
258static struct nvme_fc_lport *
James Smartc5760f32017-11-03 08:13:16 -0700259nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info *pinfo,
260 struct nvme_fc_port_template *ops,
261 struct device *dev)
James Smart5533d422017-07-31 13:20:30 -0700262{
263 struct nvme_fc_lport *lport;
264 unsigned long flags;
265
266 spin_lock_irqsave(&nvme_fc_lock, flags);
267
268 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
269 if (lport->localport.node_name != pinfo->node_name ||
270 lport->localport.port_name != pinfo->port_name)
271 continue;
272
James Smartc5760f32017-11-03 08:13:16 -0700273 if (lport->dev != dev) {
274 lport = ERR_PTR(-EXDEV);
275 goto out_done;
276 }
277
James Smart5533d422017-07-31 13:20:30 -0700278 if (lport->localport.port_state != FC_OBJSTATE_DELETED) {
279 lport = ERR_PTR(-EEXIST);
280 goto out_done;
281 }
282
283 if (!nvme_fc_lport_get(lport)) {
284 /*
285 * fails if ref cnt already 0. If so,
286 * act as if lport already deleted
287 */
288 lport = NULL;
289 goto out_done;
290 }
291
292 /* resume the lport */
293
James Smartc5760f32017-11-03 08:13:16 -0700294 lport->ops = ops;
James Smart5533d422017-07-31 13:20:30 -0700295 lport->localport.port_role = pinfo->port_role;
296 lport->localport.port_id = pinfo->port_id;
297 lport->localport.port_state = FC_OBJSTATE_ONLINE;
298
299 spin_unlock_irqrestore(&nvme_fc_lock, flags);
300
301 return lport;
302 }
303
304 lport = NULL;
305
306out_done:
307 spin_unlock_irqrestore(&nvme_fc_lock, flags);
308
309 return lport;
310}
James Smarte3994412016-12-02 00:28:42 -0800311
312/**
313 * nvme_fc_register_localport - transport entry point called by an
314 * LLDD to register the existence of a NVME
315 * host FC port.
316 * @pinfo: pointer to information about the port to be registered
317 * @template: LLDD entrypoints and operational parameters for the port
318 * @dev: physical hardware device node port corresponds to. Will be
319 * used for DMA mappings
320 * @lport_p: pointer to a local port pointer. Upon success, the routine
321 * will allocate a nvme_fc_local_port structure and place its
322 * address in the local port pointer. Upon failure, local port
323 * pointer will be set to 0.
324 *
325 * Returns:
326 * a completion status. Must be 0 upon success; a negative errno
327 * (ex: -ENXIO) upon failure.
328 */
329int
330nvme_fc_register_localport(struct nvme_fc_port_info *pinfo,
331 struct nvme_fc_port_template *template,
332 struct device *dev,
333 struct nvme_fc_local_port **portptr)
334{
335 struct nvme_fc_lport *newrec;
336 unsigned long flags;
337 int ret, idx;
338
339 if (!template->localport_delete || !template->remoteport_delete ||
340 !template->ls_req || !template->fcp_io ||
341 !template->ls_abort || !template->fcp_abort ||
342 !template->max_hw_queues || !template->max_sgl_segments ||
343 !template->max_dif_sgl_segments || !template->dma_boundary) {
344 ret = -EINVAL;
345 goto out_reghost_failed;
346 }
347
James Smart5533d422017-07-31 13:20:30 -0700348 /*
349 * look to see if there is already a localport that had been
350 * deregistered and in the process of waiting for all the
351 * references to fully be removed. If the references haven't
352 * expired, we can simply re-enable the localport. Remoteports
353 * and controller reconnections should resume naturally.
354 */
James Smartc5760f32017-11-03 08:13:16 -0700355 newrec = nvme_fc_attach_to_unreg_lport(pinfo, template, dev);
James Smart5533d422017-07-31 13:20:30 -0700356
357 /* found an lport, but something about its state is bad */
358 if (IS_ERR(newrec)) {
359 ret = PTR_ERR(newrec);
360 goto out_reghost_failed;
361
362 /* found existing lport, which was resumed */
363 } else if (newrec) {
364 *portptr = &newrec->localport;
365 return 0;
366 }
367
368 /* nothing found - allocate a new localport struct */
369
James Smarte3994412016-12-02 00:28:42 -0800370 newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz),
371 GFP_KERNEL);
372 if (!newrec) {
373 ret = -ENOMEM;
374 goto out_reghost_failed;
375 }
376
377 idx = ida_simple_get(&nvme_fc_local_port_cnt, 0, 0, GFP_KERNEL);
378 if (idx < 0) {
379 ret = -ENOSPC;
380 goto out_fail_kfree;
381 }
382
383 if (!get_device(dev) && dev) {
384 ret = -ENODEV;
385 goto out_ida_put;
386 }
387
388 INIT_LIST_HEAD(&newrec->port_list);
389 INIT_LIST_HEAD(&newrec->endp_list);
390 kref_init(&newrec->ref);
James Smart158bfb82017-11-03 08:13:17 -0700391 atomic_set(&newrec->act_rport_cnt, 0);
James Smarte3994412016-12-02 00:28:42 -0800392 newrec->ops = template;
393 newrec->dev = dev;
394 ida_init(&newrec->endp_cnt);
395 newrec->localport.private = &newrec[1];
396 newrec->localport.node_name = pinfo->node_name;
397 newrec->localport.port_name = pinfo->port_name;
398 newrec->localport.port_role = pinfo->port_role;
399 newrec->localport.port_id = pinfo->port_id;
400 newrec->localport.port_state = FC_OBJSTATE_ONLINE;
401 newrec->localport.port_num = idx;
402
403 spin_lock_irqsave(&nvme_fc_lock, flags);
404 list_add_tail(&newrec->port_list, &nvme_fc_lport_list);
405 spin_unlock_irqrestore(&nvme_fc_lock, flags);
406
407 if (dev)
408 dma_set_seg_boundary(dev, template->dma_boundary);
409
410 *portptr = &newrec->localport;
411 return 0;
412
413out_ida_put:
414 ida_simple_remove(&nvme_fc_local_port_cnt, idx);
415out_fail_kfree:
416 kfree(newrec);
417out_reghost_failed:
418 *portptr = NULL;
419
420 return ret;
421}
422EXPORT_SYMBOL_GPL(nvme_fc_register_localport);
423
James Smarte3994412016-12-02 00:28:42 -0800424/**
425 * nvme_fc_unregister_localport - transport entry point called by an
426 * LLDD to deregister/remove a previously
427 * registered a NVME host FC port.
428 * @localport: pointer to the (registered) local port that is to be
429 * deregistered.
430 *
431 * Returns:
432 * a completion status. Must be 0 upon success; a negative errno
433 * (ex: -ENXIO) upon failure.
434 */
435int
436nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr)
437{
438 struct nvme_fc_lport *lport = localport_to_lport(portptr);
439 unsigned long flags;
440
441 if (!portptr)
442 return -EINVAL;
443
444 spin_lock_irqsave(&nvme_fc_lock, flags);
445
446 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
447 spin_unlock_irqrestore(&nvme_fc_lock, flags);
448 return -EINVAL;
449 }
450 portptr->port_state = FC_OBJSTATE_DELETED;
451
452 spin_unlock_irqrestore(&nvme_fc_lock, flags);
453
James Smart158bfb82017-11-03 08:13:17 -0700454 if (atomic_read(&lport->act_rport_cnt) == 0)
455 lport->ops->localport_delete(&lport->localport);
456
James Smarte3994412016-12-02 00:28:42 -0800457 nvme_fc_lport_put(lport);
458
459 return 0;
460}
461EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport);
462
James Smarteaefd5a2017-09-14 10:38:42 -0700463/*
464 * TRADDR strings, per FC-NVME are fixed format:
465 * "nn-0x<16hexdigits>:pn-0x<16hexdigits>" - 43 characters
466 * udev event will only differ by prefix of what field is
467 * being specified:
468 * "NVMEFC_HOST_TRADDR=" or "NVMEFC_TRADDR=" - 19 max characters
469 * 19 + 43 + null_fudge = 64 characters
470 */
471#define FCNVME_TRADDR_LENGTH 64
472
473static void
474nvme_fc_signal_discovery_scan(struct nvme_fc_lport *lport,
475 struct nvme_fc_rport *rport)
476{
477 char hostaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_HOST_TRADDR=...*/
478 char tgtaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_TRADDR=...*/
479 char *envp[4] = { "FC_EVENT=nvmediscovery", hostaddr, tgtaddr, NULL };
480
481 if (!(rport->remoteport.port_role & FC_PORT_ROLE_NVME_DISCOVERY))
482 return;
483
484 snprintf(hostaddr, sizeof(hostaddr),
485 "NVMEFC_HOST_TRADDR=nn-0x%016llx:pn-0x%016llx",
486 lport->localport.node_name, lport->localport.port_name);
487 snprintf(tgtaddr, sizeof(tgtaddr),
488 "NVMEFC_TRADDR=nn-0x%016llx:pn-0x%016llx",
489 rport->remoteport.node_name, rport->remoteport.port_name);
490 kobject_uevent_env(&fc_udev_device->kobj, KOBJ_CHANGE, envp);
491}
492
James Smart469d0ef2017-09-26 21:50:45 -0700493static void
494nvme_fc_free_rport(struct kref *ref)
495{
496 struct nvme_fc_rport *rport =
497 container_of(ref, struct nvme_fc_rport, ref);
498 struct nvme_fc_lport *lport =
499 localport_to_lport(rport->remoteport.localport);
500 unsigned long flags;
501
502 WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED);
503 WARN_ON(!list_empty(&rport->ctrl_list));
504
505 /* remove from lport list */
506 spin_lock_irqsave(&nvme_fc_lock, flags);
507 list_del(&rport->endp_list);
508 spin_unlock_irqrestore(&nvme_fc_lock, flags);
509
James Smart469d0ef2017-09-26 21:50:45 -0700510 ida_simple_remove(&lport->endp_cnt, rport->remoteport.port_num);
511
512 kfree(rport);
513
514 nvme_fc_lport_put(lport);
515}
516
517static void
518nvme_fc_rport_put(struct nvme_fc_rport *rport)
519{
520 kref_put(&rport->ref, nvme_fc_free_rport);
521}
522
523static int
524nvme_fc_rport_get(struct nvme_fc_rport *rport)
525{
526 return kref_get_unless_zero(&rport->ref);
527}
528
James Smart2b632972017-10-25 16:43:17 -0700529static void
530nvme_fc_resume_controller(struct nvme_fc_ctrl *ctrl)
531{
532 switch (ctrl->ctrl.state) {
533 case NVME_CTRL_NEW:
Max Gurtovoyad6a0a52018-01-31 18:31:24 +0200534 case NVME_CTRL_CONNECTING:
James Smart2b632972017-10-25 16:43:17 -0700535 /*
536 * As all reconnects were suppressed, schedule a
537 * connect.
538 */
539 dev_info(ctrl->ctrl.device,
540 "NVME-FC{%d}: connectivity re-established. "
541 "Attempting reconnect\n", ctrl->cnum);
542
543 queue_delayed_work(nvme_wq, &ctrl->connect_work, 0);
544 break;
545
546 case NVME_CTRL_RESETTING:
547 /*
548 * Controller is already in the process of terminating the
549 * association. No need to do anything further. The reconnect
550 * step will naturally occur after the reset completes.
551 */
552 break;
553
554 default:
555 /* no action to take - let it delete */
556 break;
557 }
558}
559
560static struct nvme_fc_rport *
561nvme_fc_attach_to_suspended_rport(struct nvme_fc_lport *lport,
562 struct nvme_fc_port_info *pinfo)
563{
564 struct nvme_fc_rport *rport;
565 struct nvme_fc_ctrl *ctrl;
566 unsigned long flags;
567
568 spin_lock_irqsave(&nvme_fc_lock, flags);
569
570 list_for_each_entry(rport, &lport->endp_list, endp_list) {
571 if (rport->remoteport.node_name != pinfo->node_name ||
572 rport->remoteport.port_name != pinfo->port_name)
573 continue;
574
575 if (!nvme_fc_rport_get(rport)) {
576 rport = ERR_PTR(-ENOLCK);
577 goto out_done;
578 }
579
580 spin_unlock_irqrestore(&nvme_fc_lock, flags);
581
582 spin_lock_irqsave(&rport->lock, flags);
583
584 /* has it been unregistered */
585 if (rport->remoteport.port_state != FC_OBJSTATE_DELETED) {
586 /* means lldd called us twice */
587 spin_unlock_irqrestore(&rport->lock, flags);
588 nvme_fc_rport_put(rport);
589 return ERR_PTR(-ESTALE);
590 }
591
James Smart0cdd5fc2018-03-05 20:55:49 -0800592 rport->remoteport.port_role = pinfo->port_role;
593 rport->remoteport.port_id = pinfo->port_id;
James Smart2b632972017-10-25 16:43:17 -0700594 rport->remoteport.port_state = FC_OBJSTATE_ONLINE;
595 rport->dev_loss_end = 0;
596
597 /*
598 * kick off a reconnect attempt on all associations to the
599 * remote port. A successful reconnects will resume i/o.
600 */
601 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list)
602 nvme_fc_resume_controller(ctrl);
603
604 spin_unlock_irqrestore(&rport->lock, flags);
605
606 return rport;
607 }
608
609 rport = NULL;
610
611out_done:
612 spin_unlock_irqrestore(&nvme_fc_lock, flags);
613
614 return rport;
615}
616
617static inline void
618__nvme_fc_set_dev_loss_tmo(struct nvme_fc_rport *rport,
619 struct nvme_fc_port_info *pinfo)
620{
621 if (pinfo->dev_loss_tmo)
622 rport->remoteport.dev_loss_tmo = pinfo->dev_loss_tmo;
623 else
624 rport->remoteport.dev_loss_tmo = NVME_FC_DEFAULT_DEV_LOSS_TMO;
625}
626
James Smarte3994412016-12-02 00:28:42 -0800627/**
628 * nvme_fc_register_remoteport - transport entry point called by an
629 * LLDD to register the existence of a NVME
630 * subsystem FC port on its fabric.
631 * @localport: pointer to the (registered) local port that the remote
632 * subsystem port is connected to.
633 * @pinfo: pointer to information about the port to be registered
634 * @rport_p: pointer to a remote port pointer. Upon success, the routine
635 * will allocate a nvme_fc_remote_port structure and place its
636 * address in the remote port pointer. Upon failure, remote port
637 * pointer will be set to 0.
638 *
639 * Returns:
640 * a completion status. Must be 0 upon success; a negative errno
641 * (ex: -ENXIO) upon failure.
642 */
643int
644nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
645 struct nvme_fc_port_info *pinfo,
646 struct nvme_fc_remote_port **portptr)
647{
648 struct nvme_fc_lport *lport = localport_to_lport(localport);
649 struct nvme_fc_rport *newrec;
650 unsigned long flags;
651 int ret, idx;
652
James Smart2b632972017-10-25 16:43:17 -0700653 if (!nvme_fc_lport_get(lport)) {
654 ret = -ESHUTDOWN;
655 goto out_reghost_failed;
656 }
657
658 /*
659 * look to see if there is already a remoteport that is waiting
660 * for a reconnect (within dev_loss_tmo) with the same WWN's.
661 * If so, transition to it and reconnect.
662 */
663 newrec = nvme_fc_attach_to_suspended_rport(lport, pinfo);
664
665 /* found an rport, but something about its state is bad */
666 if (IS_ERR(newrec)) {
667 ret = PTR_ERR(newrec);
668 goto out_lport_put;
669
670 /* found existing rport, which was resumed */
671 } else if (newrec) {
672 nvme_fc_lport_put(lport);
673 __nvme_fc_set_dev_loss_tmo(newrec, pinfo);
674 nvme_fc_signal_discovery_scan(lport, newrec);
675 *portptr = &newrec->remoteport;
676 return 0;
677 }
678
679 /* nothing found - allocate a new remoteport struct */
680
James Smarte3994412016-12-02 00:28:42 -0800681 newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz),
682 GFP_KERNEL);
683 if (!newrec) {
684 ret = -ENOMEM;
James Smart2b632972017-10-25 16:43:17 -0700685 goto out_lport_put;
James Smarte3994412016-12-02 00:28:42 -0800686 }
687
688 idx = ida_simple_get(&lport->endp_cnt, 0, 0, GFP_KERNEL);
689 if (idx < 0) {
690 ret = -ENOSPC;
James Smart2b632972017-10-25 16:43:17 -0700691 goto out_kfree_rport;
James Smarte3994412016-12-02 00:28:42 -0800692 }
693
694 INIT_LIST_HEAD(&newrec->endp_list);
695 INIT_LIST_HEAD(&newrec->ctrl_list);
James Smartc913a8b2017-04-11 11:35:08 -0700696 INIT_LIST_HEAD(&newrec->ls_req_list);
James Smarte3994412016-12-02 00:28:42 -0800697 kref_init(&newrec->ref);
James Smart158bfb82017-11-03 08:13:17 -0700698 atomic_set(&newrec->act_ctrl_cnt, 0);
James Smarte3994412016-12-02 00:28:42 -0800699 spin_lock_init(&newrec->lock);
700 newrec->remoteport.localport = &lport->localport;
James Smartc913a8b2017-04-11 11:35:08 -0700701 newrec->dev = lport->dev;
702 newrec->lport = lport;
James Smarte3994412016-12-02 00:28:42 -0800703 newrec->remoteport.private = &newrec[1];
704 newrec->remoteport.port_role = pinfo->port_role;
705 newrec->remoteport.node_name = pinfo->node_name;
706 newrec->remoteport.port_name = pinfo->port_name;
707 newrec->remoteport.port_id = pinfo->port_id;
708 newrec->remoteport.port_state = FC_OBJSTATE_ONLINE;
709 newrec->remoteport.port_num = idx;
James Smart2b632972017-10-25 16:43:17 -0700710 __nvme_fc_set_dev_loss_tmo(newrec, pinfo);
James Smarte3994412016-12-02 00:28:42 -0800711
712 spin_lock_irqsave(&nvme_fc_lock, flags);
713 list_add_tail(&newrec->endp_list, &lport->endp_list);
714 spin_unlock_irqrestore(&nvme_fc_lock, flags);
715
James Smarteaefd5a2017-09-14 10:38:42 -0700716 nvme_fc_signal_discovery_scan(lport, newrec);
717
James Smarte3994412016-12-02 00:28:42 -0800718 *portptr = &newrec->remoteport;
719 return 0;
720
James Smarte3994412016-12-02 00:28:42 -0800721out_kfree_rport:
722 kfree(newrec);
James Smart2b632972017-10-25 16:43:17 -0700723out_lport_put:
724 nvme_fc_lport_put(lport);
James Smarte3994412016-12-02 00:28:42 -0800725out_reghost_failed:
726 *portptr = NULL;
727 return ret;
James Smarte3994412016-12-02 00:28:42 -0800728}
729EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport);
730
James Smart8d64daf2017-04-11 11:35:09 -0700731static int
732nvme_fc_abort_lsops(struct nvme_fc_rport *rport)
733{
734 struct nvmefc_ls_req_op *lsop;
735 unsigned long flags;
736
737restart:
738 spin_lock_irqsave(&rport->lock, flags);
739
740 list_for_each_entry(lsop, &rport->ls_req_list, lsreq_list) {
741 if (!(lsop->flags & FCOP_FLAGS_TERMIO)) {
742 lsop->flags |= FCOP_FLAGS_TERMIO;
743 spin_unlock_irqrestore(&rport->lock, flags);
744 rport->lport->ops->ls_abort(&rport->lport->localport,
745 &rport->remoteport,
746 &lsop->ls_req);
747 goto restart;
748 }
749 }
750 spin_unlock_irqrestore(&rport->lock, flags);
751
752 return 0;
753}
754
James Smart2b632972017-10-25 16:43:17 -0700755static void
756nvme_fc_ctrl_connectivity_loss(struct nvme_fc_ctrl *ctrl)
757{
758 dev_info(ctrl->ctrl.device,
759 "NVME-FC{%d}: controller connectivity lost. Awaiting "
760 "Reconnect", ctrl->cnum);
761
762 switch (ctrl->ctrl.state) {
763 case NVME_CTRL_NEW:
764 case NVME_CTRL_LIVE:
765 /*
766 * Schedule a controller reset. The reset will terminate the
767 * association and schedule the reconnect timer. Reconnects
768 * will be attempted until either the ctlr_loss_tmo
769 * (max_retries * connect_delay) expires or the remoteport's
770 * dev_loss_tmo expires.
771 */
772 if (nvme_reset_ctrl(&ctrl->ctrl)) {
773 dev_warn(ctrl->ctrl.device,
Max Gurtovoy77d06122018-03-11 17:46:06 +0200774 "NVME-FC{%d}: Couldn't schedule reset.\n",
James Smart2b632972017-10-25 16:43:17 -0700775 ctrl->cnum);
776 nvme_delete_ctrl(&ctrl->ctrl);
777 }
778 break;
779
Max Gurtovoyad6a0a52018-01-31 18:31:24 +0200780 case NVME_CTRL_CONNECTING:
James Smart2b632972017-10-25 16:43:17 -0700781 /*
782 * The association has already been terminated and the
783 * controller is attempting reconnects. No need to do anything
784 * futher. Reconnects will be attempted until either the
785 * ctlr_loss_tmo (max_retries * connect_delay) expires or the
786 * remoteport's dev_loss_tmo expires.
787 */
788 break;
789
790 case NVME_CTRL_RESETTING:
791 /*
792 * Controller is already in the process of terminating the
793 * association. No need to do anything further. The reconnect
794 * step will kick in naturally after the association is
795 * terminated.
796 */
797 break;
798
799 case NVME_CTRL_DELETING:
800 default:
801 /* no action to take - let it delete */
802 break;
803 }
804}
805
James Smarte3994412016-12-02 00:28:42 -0800806/**
807 * nvme_fc_unregister_remoteport - transport entry point called by an
808 * LLDD to deregister/remove a previously
809 * registered a NVME subsystem FC port.
810 * @remoteport: pointer to the (registered) remote port that is to be
811 * deregistered.
812 *
813 * Returns:
814 * a completion status. Must be 0 upon success; a negative errno
815 * (ex: -ENXIO) upon failure.
816 */
817int
818nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr)
819{
820 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
821 struct nvme_fc_ctrl *ctrl;
822 unsigned long flags;
823
824 if (!portptr)
825 return -EINVAL;
826
827 spin_lock_irqsave(&rport->lock, flags);
828
829 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
830 spin_unlock_irqrestore(&rport->lock, flags);
831 return -EINVAL;
832 }
833 portptr->port_state = FC_OBJSTATE_DELETED;
834
James Smart2b632972017-10-25 16:43:17 -0700835 rport->dev_loss_end = jiffies + (portptr->dev_loss_tmo * HZ);
836
837 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
838 /* if dev_loss_tmo==0, dev loss is immediate */
839 if (!portptr->dev_loss_tmo) {
840 dev_warn(ctrl->ctrl.device,
Max Gurtovoy77d06122018-03-11 17:46:06 +0200841 "NVME-FC{%d}: controller connectivity lost.\n",
James Smart2b632972017-10-25 16:43:17 -0700842 ctrl->cnum);
843 nvme_delete_ctrl(&ctrl->ctrl);
844 } else
845 nvme_fc_ctrl_connectivity_loss(ctrl);
846 }
James Smarte3994412016-12-02 00:28:42 -0800847
848 spin_unlock_irqrestore(&rport->lock, flags);
849
James Smart8d64daf2017-04-11 11:35:09 -0700850 nvme_fc_abort_lsops(rport);
851
James Smart158bfb82017-11-03 08:13:17 -0700852 if (atomic_read(&rport->act_ctrl_cnt) == 0)
853 rport->lport->ops->remoteport_delete(portptr);
854
James Smart2b632972017-10-25 16:43:17 -0700855 /*
856 * release the reference, which will allow, if all controllers
857 * go away, which should only occur after dev_loss_tmo occurs,
858 * for the rport to be torn down.
859 */
James Smarte3994412016-12-02 00:28:42 -0800860 nvme_fc_rport_put(rport);
James Smart2b632972017-10-25 16:43:17 -0700861
James Smarte3994412016-12-02 00:28:42 -0800862 return 0;
863}
864EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport);
865
James Smarteaefd5a2017-09-14 10:38:42 -0700866/**
867 * nvme_fc_rescan_remoteport - transport entry point called by an
868 * LLDD to request a nvme device rescan.
869 * @remoteport: pointer to the (registered) remote port that is to be
870 * rescanned.
871 *
872 * Returns: N/A
873 */
874void
875nvme_fc_rescan_remoteport(struct nvme_fc_remote_port *remoteport)
876{
877 struct nvme_fc_rport *rport = remoteport_to_rport(remoteport);
878
879 nvme_fc_signal_discovery_scan(rport->lport, rport);
880}
881EXPORT_SYMBOL_GPL(nvme_fc_rescan_remoteport);
882
James Smartac7fe822017-10-25 16:43:15 -0700883int
884nvme_fc_set_remoteport_devloss(struct nvme_fc_remote_port *portptr,
885 u32 dev_loss_tmo)
886{
887 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
James Smartac7fe822017-10-25 16:43:15 -0700888 unsigned long flags;
889
890 spin_lock_irqsave(&rport->lock, flags);
891
892 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
893 spin_unlock_irqrestore(&rport->lock, flags);
894 return -EINVAL;
895 }
896
897 /* a dev_loss_tmo of 0 (immediate) is allowed to be set */
898 rport->remoteport.dev_loss_tmo = dev_loss_tmo;
899
900 spin_unlock_irqrestore(&rport->lock, flags);
901
902 return 0;
903}
904EXPORT_SYMBOL_GPL(nvme_fc_set_remoteport_devloss);
905
James Smarte3994412016-12-02 00:28:42 -0800906
907/* *********************** FC-NVME DMA Handling **************************** */
908
909/*
910 * The fcloop device passes in a NULL device pointer. Real LLD's will
911 * pass in a valid device pointer. If NULL is passed to the dma mapping
912 * routines, depending on the platform, it may or may not succeed, and
913 * may crash.
914 *
915 * As such:
916 * Wrapper all the dma routines and check the dev pointer.
917 *
918 * If simple mappings (return just a dma address, we'll noop them,
919 * returning a dma address of 0.
920 *
921 * On more complex mappings (dma_map_sg), a pseudo routine fills
922 * in the scatter list, setting all dma addresses to 0.
923 */
924
925static inline dma_addr_t
926fc_dma_map_single(struct device *dev, void *ptr, size_t size,
927 enum dma_data_direction dir)
928{
929 return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
930}
931
932static inline int
933fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
934{
935 return dev ? dma_mapping_error(dev, dma_addr) : 0;
936}
937
938static inline void
939fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
940 enum dma_data_direction dir)
941{
942 if (dev)
943 dma_unmap_single(dev, addr, size, dir);
944}
945
946static inline void
947fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
948 enum dma_data_direction dir)
949{
950 if (dev)
951 dma_sync_single_for_cpu(dev, addr, size, dir);
952}
953
954static inline void
955fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
956 enum dma_data_direction dir)
957{
958 if (dev)
959 dma_sync_single_for_device(dev, addr, size, dir);
960}
961
962/* pseudo dma_map_sg call */
963static int
964fc_map_sg(struct scatterlist *sg, int nents)
965{
966 struct scatterlist *s;
967 int i;
968
969 WARN_ON(nents == 0 || sg[0].length == 0);
970
971 for_each_sg(sg, s, nents, i) {
972 s->dma_address = 0L;
973#ifdef CONFIG_NEED_SG_DMA_LENGTH
974 s->dma_length = s->length;
975#endif
976 }
977 return nents;
978}
979
980static inline int
981fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
982 enum dma_data_direction dir)
983{
984 return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
985}
986
987static inline void
988fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
989 enum dma_data_direction dir)
990{
991 if (dev)
992 dma_unmap_sg(dev, sg, nents, dir);
993}
994
James Smarte3994412016-12-02 00:28:42 -0800995/* *********************** FC-NVME LS Handling **************************** */
996
997static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
998static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
999
1000
1001static void
James Smartc913a8b2017-04-11 11:35:08 -07001002__nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop)
James Smarte3994412016-12-02 00:28:42 -08001003{
James Smartc913a8b2017-04-11 11:35:08 -07001004 struct nvme_fc_rport *rport = lsop->rport;
James Smarte3994412016-12-02 00:28:42 -08001005 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1006 unsigned long flags;
1007
James Smartc913a8b2017-04-11 11:35:08 -07001008 spin_lock_irqsave(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -08001009
1010 if (!lsop->req_queued) {
James Smartc913a8b2017-04-11 11:35:08 -07001011 spin_unlock_irqrestore(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -08001012 return;
1013 }
1014
1015 list_del(&lsop->lsreq_list);
1016
1017 lsop->req_queued = false;
1018
James Smartc913a8b2017-04-11 11:35:08 -07001019 spin_unlock_irqrestore(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -08001020
James Smartc913a8b2017-04-11 11:35:08 -07001021 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
James Smarte3994412016-12-02 00:28:42 -08001022 (lsreq->rqstlen + lsreq->rsplen),
1023 DMA_BIDIRECTIONAL);
1024
James Smartc913a8b2017-04-11 11:35:08 -07001025 nvme_fc_rport_put(rport);
James Smarte3994412016-12-02 00:28:42 -08001026}
1027
1028static int
James Smartc913a8b2017-04-11 11:35:08 -07001029__nvme_fc_send_ls_req(struct nvme_fc_rport *rport,
James Smarte3994412016-12-02 00:28:42 -08001030 struct nvmefc_ls_req_op *lsop,
1031 void (*done)(struct nvmefc_ls_req *req, int status))
1032{
1033 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1034 unsigned long flags;
James Smartc913a8b2017-04-11 11:35:08 -07001035 int ret = 0;
James Smarte3994412016-12-02 00:28:42 -08001036
James Smartc913a8b2017-04-11 11:35:08 -07001037 if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
1038 return -ECONNREFUSED;
1039
1040 if (!nvme_fc_rport_get(rport))
James Smarte3994412016-12-02 00:28:42 -08001041 return -ESHUTDOWN;
1042
1043 lsreq->done = done;
James Smartc913a8b2017-04-11 11:35:08 -07001044 lsop->rport = rport;
James Smarte3994412016-12-02 00:28:42 -08001045 lsop->req_queued = false;
1046 INIT_LIST_HEAD(&lsop->lsreq_list);
1047 init_completion(&lsop->ls_done);
1048
James Smartc913a8b2017-04-11 11:35:08 -07001049 lsreq->rqstdma = fc_dma_map_single(rport->dev, lsreq->rqstaddr,
James Smarte3994412016-12-02 00:28:42 -08001050 lsreq->rqstlen + lsreq->rsplen,
1051 DMA_BIDIRECTIONAL);
James Smartc913a8b2017-04-11 11:35:08 -07001052 if (fc_dma_mapping_error(rport->dev, lsreq->rqstdma)) {
1053 ret = -EFAULT;
1054 goto out_putrport;
James Smarte3994412016-12-02 00:28:42 -08001055 }
1056 lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
1057
James Smartc913a8b2017-04-11 11:35:08 -07001058 spin_lock_irqsave(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -08001059
James Smartc913a8b2017-04-11 11:35:08 -07001060 list_add_tail(&lsop->lsreq_list, &rport->ls_req_list);
James Smarte3994412016-12-02 00:28:42 -08001061
1062 lsop->req_queued = true;
1063
James Smartc913a8b2017-04-11 11:35:08 -07001064 spin_unlock_irqrestore(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -08001065
James Smartc913a8b2017-04-11 11:35:08 -07001066 ret = rport->lport->ops->ls_req(&rport->lport->localport,
1067 &rport->remoteport, lsreq);
James Smarte3994412016-12-02 00:28:42 -08001068 if (ret)
James Smartc913a8b2017-04-11 11:35:08 -07001069 goto out_unlink;
1070
1071 return 0;
1072
1073out_unlink:
1074 lsop->ls_error = ret;
1075 spin_lock_irqsave(&rport->lock, flags);
1076 lsop->req_queued = false;
1077 list_del(&lsop->lsreq_list);
1078 spin_unlock_irqrestore(&rport->lock, flags);
1079 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1080 (lsreq->rqstlen + lsreq->rsplen),
1081 DMA_BIDIRECTIONAL);
1082out_putrport:
1083 nvme_fc_rport_put(rport);
James Smarte3994412016-12-02 00:28:42 -08001084
1085 return ret;
1086}
1087
1088static void
1089nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status)
1090{
1091 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1092
1093 lsop->ls_error = status;
1094 complete(&lsop->ls_done);
1095}
1096
1097static int
James Smartc913a8b2017-04-11 11:35:08 -07001098nvme_fc_send_ls_req(struct nvme_fc_rport *rport, struct nvmefc_ls_req_op *lsop)
James Smarte3994412016-12-02 00:28:42 -08001099{
1100 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1101 struct fcnvme_ls_rjt *rjt = lsreq->rspaddr;
1102 int ret;
1103
James Smartc913a8b2017-04-11 11:35:08 -07001104 ret = __nvme_fc_send_ls_req(rport, lsop, nvme_fc_send_ls_req_done);
James Smarte3994412016-12-02 00:28:42 -08001105
James Smartc913a8b2017-04-11 11:35:08 -07001106 if (!ret) {
James Smarte3994412016-12-02 00:28:42 -08001107 /*
1108 * No timeout/not interruptible as we need the struct
1109 * to exist until the lldd calls us back. Thus mandate
1110 * wait until driver calls back. lldd responsible for
1111 * the timeout action
1112 */
1113 wait_for_completion(&lsop->ls_done);
1114
James Smartc913a8b2017-04-11 11:35:08 -07001115 __nvme_fc_finish_ls_req(lsop);
James Smarte3994412016-12-02 00:28:42 -08001116
James Smartc913a8b2017-04-11 11:35:08 -07001117 ret = lsop->ls_error;
James Smarte3994412016-12-02 00:28:42 -08001118 }
1119
James Smartc913a8b2017-04-11 11:35:08 -07001120 if (ret)
1121 return ret;
1122
James Smarte3994412016-12-02 00:28:42 -08001123 /* ACC or RJT payload ? */
1124 if (rjt->w0.ls_cmd == FCNVME_LS_RJT)
1125 return -ENXIO;
1126
1127 return 0;
1128}
1129
James Smartc913a8b2017-04-11 11:35:08 -07001130static int
1131nvme_fc_send_ls_req_async(struct nvme_fc_rport *rport,
James Smarte3994412016-12-02 00:28:42 -08001132 struct nvmefc_ls_req_op *lsop,
1133 void (*done)(struct nvmefc_ls_req *req, int status))
1134{
James Smarte3994412016-12-02 00:28:42 -08001135 /* don't wait for completion */
1136
James Smartc913a8b2017-04-11 11:35:08 -07001137 return __nvme_fc_send_ls_req(rport, lsop, done);
James Smarte3994412016-12-02 00:28:42 -08001138}
1139
1140/* Validation Error indexes into the string table below */
1141enum {
1142 VERR_NO_ERROR = 0,
1143 VERR_LSACC = 1,
1144 VERR_LSDESC_RQST = 2,
1145 VERR_LSDESC_RQST_LEN = 3,
1146 VERR_ASSOC_ID = 4,
1147 VERR_ASSOC_ID_LEN = 5,
1148 VERR_CONN_ID = 6,
1149 VERR_CONN_ID_LEN = 7,
1150 VERR_CR_ASSOC = 8,
1151 VERR_CR_ASSOC_ACC_LEN = 9,
1152 VERR_CR_CONN = 10,
1153 VERR_CR_CONN_ACC_LEN = 11,
1154 VERR_DISCONN = 12,
1155 VERR_DISCONN_ACC_LEN = 13,
1156};
1157
1158static char *validation_errors[] = {
1159 "OK",
1160 "Not LS_ACC",
1161 "Not LSDESC_RQST",
1162 "Bad LSDESC_RQST Length",
1163 "Not Association ID",
1164 "Bad Association ID Length",
1165 "Not Connection ID",
1166 "Bad Connection ID Length",
1167 "Not CR_ASSOC Rqst",
1168 "Bad CR_ASSOC ACC Length",
1169 "Not CR_CONN Rqst",
1170 "Bad CR_CONN ACC Length",
1171 "Not Disconnect Rqst",
1172 "Bad Disconnect ACC Length",
1173};
1174
1175static int
1176nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl,
1177 struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio)
1178{
1179 struct nvmefc_ls_req_op *lsop;
1180 struct nvmefc_ls_req *lsreq;
1181 struct fcnvme_ls_cr_assoc_rqst *assoc_rqst;
1182 struct fcnvme_ls_cr_assoc_acc *assoc_acc;
1183 int ret, fcret = 0;
1184
1185 lsop = kzalloc((sizeof(*lsop) +
1186 ctrl->lport->ops->lsrqst_priv_sz +
1187 sizeof(*assoc_rqst) + sizeof(*assoc_acc)), GFP_KERNEL);
1188 if (!lsop) {
1189 ret = -ENOMEM;
1190 goto out_no_memory;
1191 }
1192 lsreq = &lsop->ls_req;
1193
1194 lsreq->private = (void *)&lsop[1];
1195 assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)
1196 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1197 assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1];
1198
1199 assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION;
1200 assoc_rqst->desc_list_len =
1201 cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1202
1203 assoc_rqst->assoc_cmd.desc_tag =
1204 cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD);
1205 assoc_rqst->assoc_cmd.desc_len =
1206 fcnvme_lsdesc_len(
1207 sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1208
1209 assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
James Smartd157e532018-03-07 15:59:36 -08001210 assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize - 1);
James Smarte3994412016-12-02 00:28:42 -08001211 /* Linux supports only Dynamic controllers */
1212 assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff);
Christoph Hellwig8e412262017-05-17 09:54:27 +02001213 uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id);
James Smarte3994412016-12-02 00:28:42 -08001214 strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
1215 min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE));
1216 strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
1217 min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE));
1218
1219 lsop->queue = queue;
1220 lsreq->rqstaddr = assoc_rqst;
1221 lsreq->rqstlen = sizeof(*assoc_rqst);
1222 lsreq->rspaddr = assoc_acc;
1223 lsreq->rsplen = sizeof(*assoc_acc);
1224 lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
1225
James Smartc913a8b2017-04-11 11:35:08 -07001226 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
James Smarte3994412016-12-02 00:28:42 -08001227 if (ret)
1228 goto out_free_buffer;
1229
1230 /* process connect LS completion */
1231
1232 /* validate the ACC response */
1233 if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1234 fcret = VERR_LSACC;
James Smartf77fc872017-03-23 20:41:25 -07001235 else if (assoc_acc->hdr.desc_list_len !=
James Smarte3994412016-12-02 00:28:42 -08001236 fcnvme_lsdesc_len(
1237 sizeof(struct fcnvme_ls_cr_assoc_acc)))
1238 fcret = VERR_CR_ASSOC_ACC_LEN;
James Smartf77fc872017-03-23 20:41:25 -07001239 else if (assoc_acc->hdr.rqst.desc_tag !=
1240 cpu_to_be32(FCNVME_LSDESC_RQST))
James Smarte3994412016-12-02 00:28:42 -08001241 fcret = VERR_LSDESC_RQST;
1242 else if (assoc_acc->hdr.rqst.desc_len !=
1243 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1244 fcret = VERR_LSDESC_RQST_LEN;
1245 else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION)
1246 fcret = VERR_CR_ASSOC;
1247 else if (assoc_acc->associd.desc_tag !=
1248 cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
1249 fcret = VERR_ASSOC_ID;
1250 else if (assoc_acc->associd.desc_len !=
1251 fcnvme_lsdesc_len(
1252 sizeof(struct fcnvme_lsdesc_assoc_id)))
1253 fcret = VERR_ASSOC_ID_LEN;
1254 else if (assoc_acc->connectid.desc_tag !=
1255 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1256 fcret = VERR_CONN_ID;
1257 else if (assoc_acc->connectid.desc_len !=
1258 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1259 fcret = VERR_CONN_ID_LEN;
1260
1261 if (fcret) {
1262 ret = -EBADF;
1263 dev_err(ctrl->dev,
1264 "q %d connect failed: %s\n",
1265 queue->qnum, validation_errors[fcret]);
1266 } else {
1267 ctrl->association_id =
1268 be64_to_cpu(assoc_acc->associd.association_id);
1269 queue->connection_id =
1270 be64_to_cpu(assoc_acc->connectid.connection_id);
1271 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1272 }
1273
1274out_free_buffer:
1275 kfree(lsop);
1276out_no_memory:
1277 if (ret)
1278 dev_err(ctrl->dev,
1279 "queue %d connect admin queue failed (%d).\n",
1280 queue->qnum, ret);
1281 return ret;
1282}
1283
1284static int
1285nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
1286 u16 qsize, u16 ersp_ratio)
1287{
1288 struct nvmefc_ls_req_op *lsop;
1289 struct nvmefc_ls_req *lsreq;
1290 struct fcnvme_ls_cr_conn_rqst *conn_rqst;
1291 struct fcnvme_ls_cr_conn_acc *conn_acc;
1292 int ret, fcret = 0;
1293
1294 lsop = kzalloc((sizeof(*lsop) +
1295 ctrl->lport->ops->lsrqst_priv_sz +
1296 sizeof(*conn_rqst) + sizeof(*conn_acc)), GFP_KERNEL);
1297 if (!lsop) {
1298 ret = -ENOMEM;
1299 goto out_no_memory;
1300 }
1301 lsreq = &lsop->ls_req;
1302
1303 lsreq->private = (void *)&lsop[1];
1304 conn_rqst = (struct fcnvme_ls_cr_conn_rqst *)
1305 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1306 conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1];
1307
1308 conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION;
1309 conn_rqst->desc_list_len = cpu_to_be32(
1310 sizeof(struct fcnvme_lsdesc_assoc_id) +
1311 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1312
1313 conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1314 conn_rqst->associd.desc_len =
1315 fcnvme_lsdesc_len(
1316 sizeof(struct fcnvme_lsdesc_assoc_id));
1317 conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1318 conn_rqst->connect_cmd.desc_tag =
1319 cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD);
1320 conn_rqst->connect_cmd.desc_len =
1321 fcnvme_lsdesc_len(
1322 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1323 conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1324 conn_rqst->connect_cmd.qid = cpu_to_be16(queue->qnum);
James Smartd157e532018-03-07 15:59:36 -08001325 conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize - 1);
James Smarte3994412016-12-02 00:28:42 -08001326
1327 lsop->queue = queue;
1328 lsreq->rqstaddr = conn_rqst;
1329 lsreq->rqstlen = sizeof(*conn_rqst);
1330 lsreq->rspaddr = conn_acc;
1331 lsreq->rsplen = sizeof(*conn_acc);
1332 lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
1333
James Smartc913a8b2017-04-11 11:35:08 -07001334 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
James Smarte3994412016-12-02 00:28:42 -08001335 if (ret)
1336 goto out_free_buffer;
1337
1338 /* process connect LS completion */
1339
1340 /* validate the ACC response */
1341 if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1342 fcret = VERR_LSACC;
James Smartf77fc872017-03-23 20:41:25 -07001343 else if (conn_acc->hdr.desc_list_len !=
James Smarte3994412016-12-02 00:28:42 -08001344 fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)))
1345 fcret = VERR_CR_CONN_ACC_LEN;
James Smartf77fc872017-03-23 20:41:25 -07001346 else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST))
James Smarte3994412016-12-02 00:28:42 -08001347 fcret = VERR_LSDESC_RQST;
1348 else if (conn_acc->hdr.rqst.desc_len !=
1349 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1350 fcret = VERR_LSDESC_RQST_LEN;
1351 else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION)
1352 fcret = VERR_CR_CONN;
1353 else if (conn_acc->connectid.desc_tag !=
1354 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1355 fcret = VERR_CONN_ID;
1356 else if (conn_acc->connectid.desc_len !=
1357 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1358 fcret = VERR_CONN_ID_LEN;
1359
1360 if (fcret) {
1361 ret = -EBADF;
1362 dev_err(ctrl->dev,
1363 "q %d connect failed: %s\n",
1364 queue->qnum, validation_errors[fcret]);
1365 } else {
1366 queue->connection_id =
1367 be64_to_cpu(conn_acc->connectid.connection_id);
1368 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1369 }
1370
1371out_free_buffer:
1372 kfree(lsop);
1373out_no_memory:
1374 if (ret)
1375 dev_err(ctrl->dev,
1376 "queue %d connect command failed (%d).\n",
1377 queue->qnum, ret);
1378 return ret;
1379}
1380
1381static void
1382nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
1383{
1384 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
James Smarte3994412016-12-02 00:28:42 -08001385
James Smartc913a8b2017-04-11 11:35:08 -07001386 __nvme_fc_finish_ls_req(lsop);
James Smarte3994412016-12-02 00:28:42 -08001387
1388 /* fc-nvme iniator doesn't care about success or failure of cmd */
1389
1390 kfree(lsop);
1391}
1392
1393/*
1394 * This routine sends a FC-NVME LS to disconnect (aka terminate)
1395 * the FC-NVME Association. Terminating the association also
1396 * terminates the FC-NVME connections (per queue, both admin and io
1397 * queues) that are part of the association. E.g. things are torn
1398 * down, and the related FC-NVME Association ID and Connection IDs
1399 * become invalid.
1400 *
1401 * The behavior of the fc-nvme initiator is such that it's
1402 * understanding of the association and connections will implicitly
1403 * be torn down. The action is implicit as it may be due to a loss of
1404 * connectivity with the fc-nvme target, so you may never get a
1405 * response even if you tried. As such, the action of this routine
1406 * is to asynchronously send the LS, ignore any results of the LS, and
1407 * continue on with terminating the association. If the fc-nvme target
1408 * is present and receives the LS, it too can tear down.
1409 */
1410static void
1411nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl)
1412{
1413 struct fcnvme_ls_disconnect_rqst *discon_rqst;
1414 struct fcnvme_ls_disconnect_acc *discon_acc;
1415 struct nvmefc_ls_req_op *lsop;
1416 struct nvmefc_ls_req *lsreq;
James Smartc913a8b2017-04-11 11:35:08 -07001417 int ret;
James Smarte3994412016-12-02 00:28:42 -08001418
1419 lsop = kzalloc((sizeof(*lsop) +
1420 ctrl->lport->ops->lsrqst_priv_sz +
1421 sizeof(*discon_rqst) + sizeof(*discon_acc)),
1422 GFP_KERNEL);
1423 if (!lsop)
1424 /* couldn't sent it... too bad */
1425 return;
1426
1427 lsreq = &lsop->ls_req;
1428
1429 lsreq->private = (void *)&lsop[1];
1430 discon_rqst = (struct fcnvme_ls_disconnect_rqst *)
1431 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1432 discon_acc = (struct fcnvme_ls_disconnect_acc *)&discon_rqst[1];
1433
1434 discon_rqst->w0.ls_cmd = FCNVME_LS_DISCONNECT;
1435 discon_rqst->desc_list_len = cpu_to_be32(
1436 sizeof(struct fcnvme_lsdesc_assoc_id) +
1437 sizeof(struct fcnvme_lsdesc_disconn_cmd));
1438
1439 discon_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1440 discon_rqst->associd.desc_len =
1441 fcnvme_lsdesc_len(
1442 sizeof(struct fcnvme_lsdesc_assoc_id));
1443
1444 discon_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1445
1446 discon_rqst->discon_cmd.desc_tag = cpu_to_be32(
1447 FCNVME_LSDESC_DISCONN_CMD);
1448 discon_rqst->discon_cmd.desc_len =
1449 fcnvme_lsdesc_len(
1450 sizeof(struct fcnvme_lsdesc_disconn_cmd));
1451 discon_rqst->discon_cmd.scope = FCNVME_DISCONN_ASSOCIATION;
1452 discon_rqst->discon_cmd.id = cpu_to_be64(ctrl->association_id);
1453
1454 lsreq->rqstaddr = discon_rqst;
1455 lsreq->rqstlen = sizeof(*discon_rqst);
1456 lsreq->rspaddr = discon_acc;
1457 lsreq->rsplen = sizeof(*discon_acc);
1458 lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
1459
James Smartc913a8b2017-04-11 11:35:08 -07001460 ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop,
1461 nvme_fc_disconnect_assoc_done);
1462 if (ret)
1463 kfree(lsop);
James Smarte3994412016-12-02 00:28:42 -08001464
1465 /* only meaningful part to terminating the association */
1466 ctrl->association_id = 0;
1467}
1468
1469
1470/* *********************** NVME Ctrl Routines **************************** */
1471
James Smartf874d5d2017-06-01 22:54:21 -07001472static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg);
James Smarte3994412016-12-02 00:28:42 -08001473
1474static int
1475nvme_fc_reinit_request(void *data, struct request *rq)
1476{
1477 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1478 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1479
1480 memset(cmdiu, 0, sizeof(*cmdiu));
1481 cmdiu->scsi_id = NVME_CMD_SCSI_ID;
1482 cmdiu->fc_id = NVME_CMD_FC_ID;
1483 cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
1484 memset(&op->rsp_iu, 0, sizeof(op->rsp_iu));
1485
1486 return 0;
1487}
1488
1489static void
1490__nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl,
1491 struct nvme_fc_fcp_op *op)
1492{
1493 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma,
1494 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1495 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
1496 sizeof(op->cmd_iu), DMA_TO_DEVICE);
1497
1498 atomic_set(&op->state, FCPOP_STATE_UNINIT);
1499}
1500
1501static void
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001502nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1503 unsigned int hctx_idx)
James Smarte3994412016-12-02 00:28:42 -08001504{
1505 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1506
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001507 return __nvme_fc_exit_request(set->driver_data, op);
James Smarte3994412016-12-02 00:28:42 -08001508}
1509
James Smart78a7ac22017-04-23 08:30:07 -07001510static int
1511__nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
1512{
James Smart3efd6e82018-02-06 06:48:29 -08001513 unsigned long flags;
1514 int opstate;
James Smart78a7ac22017-04-23 08:30:07 -07001515
James Smart3efd6e82018-02-06 06:48:29 -08001516 spin_lock_irqsave(&ctrl->lock, flags);
1517 opstate = atomic_xchg(&op->state, FCPOP_STATE_ABORTED);
1518 if (opstate != FCPOP_STATE_ACTIVE)
1519 atomic_set(&op->state, opstate);
1520 else if (ctrl->flags & FCCTRL_TERMIO)
1521 ctrl->iocnt++;
1522 spin_unlock_irqrestore(&ctrl->lock, flags);
1523
1524 if (opstate != FCPOP_STATE_ACTIVE)
James Smart78a7ac22017-04-23 08:30:07 -07001525 return -ECANCELED;
James Smart78a7ac22017-04-23 08:30:07 -07001526
1527 ctrl->lport->ops->fcp_abort(&ctrl->lport->localport,
1528 &ctrl->rport->remoteport,
1529 op->queue->lldd_handle,
1530 &op->fcp_req);
1531
1532 return 0;
1533}
1534
James Smarte3994412016-12-02 00:28:42 -08001535static void
James Smart78a7ac22017-04-23 08:30:07 -07001536nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl)
James Smarte3994412016-12-02 00:28:42 -08001537{
1538 struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops;
James Smart3efd6e82018-02-06 06:48:29 -08001539 int i;
James Smarte3994412016-12-02 00:28:42 -08001540
James Smart3efd6e82018-02-06 06:48:29 -08001541 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++)
1542 __nvme_fc_abort_op(ctrl, aen_op);
James Smarte3994412016-12-02 00:28:42 -08001543}
1544
James Smartc3aedd222018-02-06 06:48:30 -08001545static inline void
James Smart78a7ac22017-04-23 08:30:07 -07001546__nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
James Smart3efd6e82018-02-06 06:48:29 -08001547 struct nvme_fc_fcp_op *op, int opstate)
James Smart78a7ac22017-04-23 08:30:07 -07001548{
1549 unsigned long flags;
James Smart78a7ac22017-04-23 08:30:07 -07001550
James Smartc3aedd222018-02-06 06:48:30 -08001551 if (opstate == FCPOP_STATE_ABORTED) {
1552 spin_lock_irqsave(&ctrl->lock, flags);
1553 if (ctrl->flags & FCCTRL_TERMIO) {
1554 if (!--ctrl->iocnt)
1555 wake_up(&ctrl->ioabort_wait);
1556 }
1557 spin_unlock_irqrestore(&ctrl->lock, flags);
James Smart61bff8e2017-04-23 08:30:08 -07001558 }
James Smart78a7ac22017-04-23 08:30:07 -07001559}
1560
Christoph Hellwigbaee29a2017-04-21 10:44:06 +02001561static void
James Smarte3994412016-12-02 00:28:42 -08001562nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
1563{
1564 struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1565 struct request *rq = op->rq;
1566 struct nvmefc_fcp_req *freq = &op->fcp_req;
1567 struct nvme_fc_ctrl *ctrl = op->ctrl;
1568 struct nvme_fc_queue *queue = op->queue;
1569 struct nvme_completion *cqe = &op->rsp_iu.cqe;
James Smart458f2802017-04-23 08:30:06 -07001570 struct nvme_command *sqe = &op->cmd_iu.sqe;
Christoph Hellwigd663b692017-04-20 16:02:56 +02001571 __le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001572 union nvme_result result;
James Smart0a02e392017-10-19 16:11:38 -07001573 bool terminate_assoc = true;
James Smart3efd6e82018-02-06 06:48:29 -08001574 int opstate;
James Smarte3994412016-12-02 00:28:42 -08001575
1576 /*
1577 * WARNING:
1578 * The current linux implementation of a nvme controller
1579 * allocates a single tag set for all io queues and sizes
1580 * the io queues to fully hold all possible tags. Thus, the
1581 * implementation does not reference or care about the sqhd
1582 * value as it never needs to use the sqhd/sqtail pointers
1583 * for submission pacing.
1584 *
1585 * This affects the FC-NVME implementation in two ways:
1586 * 1) As the value doesn't matter, we don't need to waste
1587 * cycles extracting it from ERSPs and stamping it in the
1588 * cases where the transport fabricates CQEs on successful
1589 * completions.
1590 * 2) The FC-NVME implementation requires that delivery of
1591 * ERSP completions are to go back to the nvme layer in order
1592 * relative to the rsn, such that the sqhd value will always
1593 * be "in order" for the nvme layer. As the nvme layer in
1594 * linux doesn't care about sqhd, there's no need to return
1595 * them in order.
1596 *
1597 * Additionally:
1598 * As the core nvme layer in linux currently does not look at
1599 * every field in the cqe - in cases where the FC transport must
1600 * fabricate a CQE, the following fields will not be set as they
1601 * are not referenced:
1602 * cqe.sqid, cqe.sqhd, cqe.command_id
James Smartf874d5d2017-06-01 22:54:21 -07001603 *
1604 * Failure or error of an individual i/o, in a transport
1605 * detected fashion unrelated to the nvme completion status,
1606 * potentially cause the initiator and target sides to get out
1607 * of sync on SQ head/tail (aka outstanding io count allowed).
1608 * Per FC-NVME spec, failure of an individual command requires
1609 * the connection to be terminated, which in turn requires the
1610 * association to be terminated.
James Smarte3994412016-12-02 00:28:42 -08001611 */
1612
James Smart3efd6e82018-02-06 06:48:29 -08001613 opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
1614
James Smarte3994412016-12-02 00:28:42 -08001615 fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
1616 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1617
James Smart3efd6e82018-02-06 06:48:29 -08001618 if (opstate == FCPOP_STATE_ABORTED)
James Smart0a02e392017-10-19 16:11:38 -07001619 status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
James Smart62eeacb2017-03-23 20:41:27 -07001620 else if (freq->status)
James Smart56b7103a2017-09-07 16:27:26 -07001621 status = cpu_to_le16(NVME_SC_INTERNAL << 1);
James Smarte3994412016-12-02 00:28:42 -08001622
1623 /*
1624 * For the linux implementation, if we have an unsuccesful
1625 * status, they blk-mq layer can typically be called with the
1626 * non-zero status and the content of the cqe isn't important.
1627 */
1628 if (status)
1629 goto done;
1630
1631 /*
1632 * command completed successfully relative to the wire
1633 * protocol. However, validate anything received and
1634 * extract the status and result from the cqe (create it
1635 * where necessary).
1636 */
1637
1638 switch (freq->rcv_rsplen) {
1639
1640 case 0:
1641 case NVME_FC_SIZEOF_ZEROS_RSP:
1642 /*
1643 * No response payload or 12 bytes of payload (which
1644 * should all be zeros) are considered successful and
1645 * no payload in the CQE by the transport.
1646 */
1647 if (freq->transferred_length !=
1648 be32_to_cpu(op->cmd_iu.data_len)) {
James Smart56b7103a2017-09-07 16:27:26 -07001649 status = cpu_to_le16(NVME_SC_INTERNAL << 1);
James Smarte3994412016-12-02 00:28:42 -08001650 goto done;
1651 }
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001652 result.u64 = 0;
James Smarte3994412016-12-02 00:28:42 -08001653 break;
1654
1655 case sizeof(struct nvme_fc_ersp_iu):
1656 /*
1657 * The ERSP IU contains a full completion with CQE.
1658 * Validate ERSP IU and look at cqe.
1659 */
1660 if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) !=
1661 (freq->rcv_rsplen / 4) ||
1662 be32_to_cpu(op->rsp_iu.xfrd_len) !=
1663 freq->transferred_length ||
James Smart726a1082017-03-23 20:41:23 -07001664 op->rsp_iu.status_code ||
James Smart458f2802017-04-23 08:30:06 -07001665 sqe->common.command_id != cqe->command_id)) {
James Smart56b7103a2017-09-07 16:27:26 -07001666 status = cpu_to_le16(NVME_SC_INTERNAL << 1);
James Smarte3994412016-12-02 00:28:42 -08001667 goto done;
1668 }
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001669 result = cqe->result;
Christoph Hellwigd663b692017-04-20 16:02:56 +02001670 status = cqe->status;
James Smarte3994412016-12-02 00:28:42 -08001671 break;
1672
1673 default:
James Smart56b7103a2017-09-07 16:27:26 -07001674 status = cpu_to_le16(NVME_SC_INTERNAL << 1);
James Smarte3994412016-12-02 00:28:42 -08001675 goto done;
1676 }
1677
James Smartf874d5d2017-06-01 22:54:21 -07001678 terminate_assoc = false;
1679
James Smarte3994412016-12-02 00:28:42 -08001680done:
James Smart78a7ac22017-04-23 08:30:07 -07001681 if (op->flags & FCOP_FLAGS_AEN) {
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001682 nvme_complete_async_event(&queue->ctrl->ctrl, status, &result);
James Smart3efd6e82018-02-06 06:48:29 -08001683 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
James Smart78a7ac22017-04-23 08:30:07 -07001684 atomic_set(&op->state, FCPOP_STATE_IDLE);
1685 op->flags = FCOP_FLAGS_AEN; /* clear other flags */
James Smarte3994412016-12-02 00:28:42 -08001686 nvme_fc_ctrl_put(ctrl);
James Smartf874d5d2017-06-01 22:54:21 -07001687 goto check_error;
James Smarte3994412016-12-02 00:28:42 -08001688 }
1689
James Smartc3aedd222018-02-06 06:48:30 -08001690 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
1691 nvme_end_request(rq, status, result);
James Smartf874d5d2017-06-01 22:54:21 -07001692
1693check_error:
1694 if (terminate_assoc)
1695 nvme_fc_error_recovery(ctrl, "transport detected io error");
James Smarte3994412016-12-02 00:28:42 -08001696}
1697
1698static int
1699__nvme_fc_init_request(struct nvme_fc_ctrl *ctrl,
1700 struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op,
1701 struct request *rq, u32 rqno)
1702{
1703 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1704 int ret = 0;
1705
1706 memset(op, 0, sizeof(*op));
1707 op->fcp_req.cmdaddr = &op->cmd_iu;
1708 op->fcp_req.cmdlen = sizeof(op->cmd_iu);
1709 op->fcp_req.rspaddr = &op->rsp_iu;
1710 op->fcp_req.rsplen = sizeof(op->rsp_iu);
1711 op->fcp_req.done = nvme_fc_fcpio_done;
1712 op->fcp_req.first_sgl = (struct scatterlist *)&op[1];
1713 op->fcp_req.private = &op->fcp_req.first_sgl[SG_CHUNK_SIZE];
1714 op->ctrl = ctrl;
1715 op->queue = queue;
1716 op->rq = rq;
1717 op->rqno = rqno;
1718
1719 cmdiu->scsi_id = NVME_CMD_SCSI_ID;
1720 cmdiu->fc_id = NVME_CMD_FC_ID;
1721 cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
1722
1723 op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev,
1724 &op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE);
1725 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) {
1726 dev_err(ctrl->dev,
1727 "FCP Op failed - cmdiu dma mapping failed.\n");
1728 ret = EFAULT;
1729 goto out_on_error;
1730 }
1731
1732 op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev,
1733 &op->rsp_iu, sizeof(op->rsp_iu),
1734 DMA_FROM_DEVICE);
1735 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) {
1736 dev_err(ctrl->dev,
1737 "FCP Op failed - rspiu dma mapping failed.\n");
1738 ret = EFAULT;
1739 }
1740
1741 atomic_set(&op->state, FCPOP_STATE_IDLE);
1742out_on_error:
1743 return ret;
1744}
1745
1746static int
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001747nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq,
1748 unsigned int hctx_idx, unsigned int numa_node)
James Smarte3994412016-12-02 00:28:42 -08001749{
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001750 struct nvme_fc_ctrl *ctrl = set->driver_data;
James Smarte3994412016-12-02 00:28:42 -08001751 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
Christoph Hellwig76f983c2017-06-13 09:15:20 +02001752 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
1753 struct nvme_fc_queue *queue = &ctrl->queues[queue_idx];
James Smarte3994412016-12-02 00:28:42 -08001754
1755 return __nvme_fc_init_request(ctrl, queue, op, rq, queue->rqcnt++);
1756}
1757
1758static int
1759nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl)
1760{
1761 struct nvme_fc_fcp_op *aen_op;
1762 struct nvme_fc_cmd_iu *cmdiu;
1763 struct nvme_command *sqe;
James Smart61bff8e2017-04-23 08:30:08 -07001764 void *private;
James Smarte3994412016-12-02 00:28:42 -08001765 int i, ret;
1766
1767 aen_op = ctrl->aen_ops;
Keith Busch38dabe22017-11-07 15:13:10 -07001768 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
James Smart61bff8e2017-04-23 08:30:08 -07001769 private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz,
1770 GFP_KERNEL);
1771 if (!private)
1772 return -ENOMEM;
1773
James Smarte3994412016-12-02 00:28:42 -08001774 cmdiu = &aen_op->cmd_iu;
1775 sqe = &cmdiu->sqe;
1776 ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0],
1777 aen_op, (struct request *)NULL,
Keith Busch38dabe22017-11-07 15:13:10 -07001778 (NVME_AQ_BLK_MQ_DEPTH + i));
James Smart61bff8e2017-04-23 08:30:08 -07001779 if (ret) {
1780 kfree(private);
James Smarte3994412016-12-02 00:28:42 -08001781 return ret;
James Smart61bff8e2017-04-23 08:30:08 -07001782 }
James Smarte3994412016-12-02 00:28:42 -08001783
James Smart78a7ac22017-04-23 08:30:07 -07001784 aen_op->flags = FCOP_FLAGS_AEN;
James Smart61bff8e2017-04-23 08:30:08 -07001785 aen_op->fcp_req.first_sgl = NULL; /* no sg list */
1786 aen_op->fcp_req.private = private;
James Smart78a7ac22017-04-23 08:30:07 -07001787
James Smarte3994412016-12-02 00:28:42 -08001788 memset(sqe, 0, sizeof(*sqe));
1789 sqe->common.opcode = nvme_admin_async_event;
James Smart78a7ac22017-04-23 08:30:07 -07001790 /* Note: core layer may overwrite the sqe.command_id value */
Keith Busch38dabe22017-11-07 15:13:10 -07001791 sqe->common.command_id = NVME_AQ_BLK_MQ_DEPTH + i;
James Smarte3994412016-12-02 00:28:42 -08001792 }
1793 return 0;
1794}
1795
James Smart61bff8e2017-04-23 08:30:08 -07001796static void
1797nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl)
1798{
1799 struct nvme_fc_fcp_op *aen_op;
1800 int i;
1801
1802 aen_op = ctrl->aen_ops;
Keith Busch38dabe22017-11-07 15:13:10 -07001803 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
James Smart61bff8e2017-04-23 08:30:08 -07001804 if (!aen_op->fcp_req.private)
1805 continue;
1806
1807 __nvme_fc_exit_request(ctrl, aen_op);
1808
1809 kfree(aen_op->fcp_req.private);
1810 aen_op->fcp_req.private = NULL;
1811 }
1812}
James Smarte3994412016-12-02 00:28:42 -08001813
1814static inline void
1815__nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, struct nvme_fc_ctrl *ctrl,
1816 unsigned int qidx)
1817{
1818 struct nvme_fc_queue *queue = &ctrl->queues[qidx];
1819
1820 hctx->driver_data = queue;
1821 queue->hctx = hctx;
1822}
1823
1824static int
1825nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1826 unsigned int hctx_idx)
1827{
1828 struct nvme_fc_ctrl *ctrl = data;
1829
1830 __nvme_fc_init_hctx(hctx, ctrl, hctx_idx + 1);
1831
1832 return 0;
1833}
1834
1835static int
1836nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1837 unsigned int hctx_idx)
1838{
1839 struct nvme_fc_ctrl *ctrl = data;
1840
1841 __nvme_fc_init_hctx(hctx, ctrl, hctx_idx);
1842
1843 return 0;
1844}
1845
1846static void
Keith Busch08e15072017-11-07 15:13:11 -07001847nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx)
James Smarte3994412016-12-02 00:28:42 -08001848{
1849 struct nvme_fc_queue *queue;
1850
1851 queue = &ctrl->queues[idx];
1852 memset(queue, 0, sizeof(*queue));
1853 queue->ctrl = ctrl;
1854 queue->qnum = idx;
1855 atomic_set(&queue->csn, 1);
1856 queue->dev = ctrl->dev;
1857
1858 if (idx > 0)
1859 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
1860 else
1861 queue->cmnd_capsule_len = sizeof(struct nvme_command);
1862
James Smarte3994412016-12-02 00:28:42 -08001863 /*
1864 * Considered whether we should allocate buffers for all SQEs
1865 * and CQEs and dma map them - mapping their respective entries
1866 * into the request structures (kernel vm addr and dma address)
1867 * thus the driver could use the buffers/mappings directly.
1868 * It only makes sense if the LLDD would use them for its
1869 * messaging api. It's very unlikely most adapter api's would use
1870 * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
1871 * structures were used instead.
1872 */
1873}
1874
1875/*
1876 * This routine terminates a queue at the transport level.
1877 * The transport has already ensured that all outstanding ios on
1878 * the queue have been terminated.
1879 * The transport will send a Disconnect LS request to terminate
1880 * the queue's connection. Termination of the admin queue will also
1881 * terminate the association at the target.
1882 */
1883static void
1884nvme_fc_free_queue(struct nvme_fc_queue *queue)
1885{
1886 if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags))
1887 return;
1888
Sagi Grimberg9e0ed162017-10-24 15:25:21 +03001889 clear_bit(NVME_FC_Q_LIVE, &queue->flags);
James Smarte3994412016-12-02 00:28:42 -08001890 /*
1891 * Current implementation never disconnects a single queue.
1892 * It always terminates a whole association. So there is never
1893 * a disconnect(queue) LS sent to the target.
1894 */
1895
1896 queue->connection_id = 0;
James Smarte3994412016-12-02 00:28:42 -08001897}
1898
1899static void
1900__nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl,
1901 struct nvme_fc_queue *queue, unsigned int qidx)
1902{
1903 if (ctrl->lport->ops->delete_queue)
1904 ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx,
1905 queue->lldd_handle);
1906 queue->lldd_handle = NULL;
1907}
1908
1909static void
James Smarte3994412016-12-02 00:28:42 -08001910nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl)
1911{
1912 int i;
1913
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001914 for (i = 1; i < ctrl->ctrl.queue_count; i++)
James Smarte3994412016-12-02 00:28:42 -08001915 nvme_fc_free_queue(&ctrl->queues[i]);
1916}
1917
1918static int
1919__nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl,
1920 struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize)
1921{
1922 int ret = 0;
1923
1924 queue->lldd_handle = NULL;
1925 if (ctrl->lport->ops->create_queue)
1926 ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport,
1927 qidx, qsize, &queue->lldd_handle);
1928
1929 return ret;
1930}
1931
1932static void
1933nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl)
1934{
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001935 struct nvme_fc_queue *queue = &ctrl->queues[ctrl->ctrl.queue_count - 1];
James Smarte3994412016-12-02 00:28:42 -08001936 int i;
1937
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001938 for (i = ctrl->ctrl.queue_count - 1; i >= 1; i--, queue--)
James Smarte3994412016-12-02 00:28:42 -08001939 __nvme_fc_delete_hw_queue(ctrl, queue, i);
1940}
1941
1942static int
1943nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
1944{
1945 struct nvme_fc_queue *queue = &ctrl->queues[1];
Johannes Thumshirn17a1ec02016-12-15 14:20:48 +01001946 int i, ret;
James Smarte3994412016-12-02 00:28:42 -08001947
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001948 for (i = 1; i < ctrl->ctrl.queue_count; i++, queue++) {
James Smarte3994412016-12-02 00:28:42 -08001949 ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize);
Johannes Thumshirn17a1ec02016-12-15 14:20:48 +01001950 if (ret)
1951 goto delete_queues;
James Smarte3994412016-12-02 00:28:42 -08001952 }
1953
1954 return 0;
Johannes Thumshirn17a1ec02016-12-15 14:20:48 +01001955
1956delete_queues:
1957 for (; i >= 0; i--)
1958 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i);
1959 return ret;
James Smarte3994412016-12-02 00:28:42 -08001960}
1961
1962static int
1963nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
1964{
1965 int i, ret = 0;
1966
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001967 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
James Smarte3994412016-12-02 00:28:42 -08001968 ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize,
1969 (qsize / 5));
1970 if (ret)
1971 break;
1972 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
1973 if (ret)
1974 break;
Sagi Grimberg9e0ed162017-10-24 15:25:21 +03001975
1976 set_bit(NVME_FC_Q_LIVE, &ctrl->queues[i].flags);
James Smarte3994412016-12-02 00:28:42 -08001977 }
1978
1979 return ret;
1980}
1981
1982static void
1983nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl)
1984{
1985 int i;
1986
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001987 for (i = 1; i < ctrl->ctrl.queue_count; i++)
Keith Busch08e15072017-11-07 15:13:11 -07001988 nvme_fc_init_queue(ctrl, i);
James Smarte3994412016-12-02 00:28:42 -08001989}
1990
1991static void
1992nvme_fc_ctrl_free(struct kref *ref)
1993{
1994 struct nvme_fc_ctrl *ctrl =
1995 container_of(ref, struct nvme_fc_ctrl, ref);
1996 unsigned long flags;
1997
James Smart61bff8e2017-04-23 08:30:08 -07001998 if (ctrl->ctrl.tagset) {
1999 blk_cleanup_queue(ctrl->ctrl.connect_q);
2000 blk_mq_free_tag_set(&ctrl->tag_set);
James Smarte3994412016-12-02 00:28:42 -08002001 }
2002
James Smart61bff8e2017-04-23 08:30:08 -07002003 /* remove from rport list */
2004 spin_lock_irqsave(&ctrl->rport->lock, flags);
2005 list_del(&ctrl->ctrl_list);
2006 spin_unlock_irqrestore(&ctrl->rport->lock, flags);
2007
Sagi Grimbergf9c5af52017-07-02 15:39:34 +03002008 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
James Smart61bff8e2017-04-23 08:30:08 -07002009 blk_cleanup_queue(ctrl->ctrl.admin_q);
2010 blk_mq_free_tag_set(&ctrl->admin_tag_set);
2011
2012 kfree(ctrl->queues);
2013
James Smarte3994412016-12-02 00:28:42 -08002014 put_device(ctrl->dev);
2015 nvme_fc_rport_put(ctrl->rport);
2016
James Smarte3994412016-12-02 00:28:42 -08002017 ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
Ewan D. Milnede414472017-04-24 13:24:16 -04002018 if (ctrl->ctrl.opts)
2019 nvmf_free_options(ctrl->ctrl.opts);
James Smarte3994412016-12-02 00:28:42 -08002020 kfree(ctrl);
2021}
2022
2023static void
2024nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl)
2025{
2026 kref_put(&ctrl->ref, nvme_fc_ctrl_free);
2027}
2028
2029static int
2030nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
2031{
2032 return kref_get_unless_zero(&ctrl->ref);
2033}
2034
2035/*
2036 * All accesses from nvme core layer done - can now free the
2037 * controller. Called after last nvme_put_ctrl() call
2038 */
2039static void
James Smart61bff8e2017-04-23 08:30:08 -07002040nvme_fc_nvme_ctrl_freed(struct nvme_ctrl *nctrl)
James Smarte3994412016-12-02 00:28:42 -08002041{
2042 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2043
2044 WARN_ON(nctrl != &ctrl->ctrl);
2045
James Smart61bff8e2017-04-23 08:30:08 -07002046 nvme_fc_ctrl_put(ctrl);
2047}
James Smarte3994412016-12-02 00:28:42 -08002048
James Smart61bff8e2017-04-23 08:30:08 -07002049static void
2050nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
2051{
James Smart69fa9642017-06-21 17:43:21 -07002052 /* only proceed if in LIVE state - e.g. on first error */
2053 if (ctrl->ctrl.state != NVME_CTRL_LIVE)
2054 return;
2055
James Smart61bff8e2017-04-23 08:30:08 -07002056 dev_warn(ctrl->ctrl.device,
2057 "NVME-FC{%d}: transport association error detected: %s\n",
2058 ctrl->cnum, errmsg);
James Smart589ff772017-05-15 17:10:19 -07002059 dev_warn(ctrl->ctrl.device,
James Smart61bff8e2017-04-23 08:30:08 -07002060 "NVME-FC{%d}: resetting controller\n", ctrl->cnum);
James Smarte3994412016-12-02 00:28:42 -08002061
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002062 nvme_reset_ctrl(&ctrl->ctrl);
James Smarte3994412016-12-02 00:28:42 -08002063}
2064
Christoph Hellwigbaee29a2017-04-21 10:44:06 +02002065static enum blk_eh_timer_return
James Smarte3994412016-12-02 00:28:42 -08002066nvme_fc_timeout(struct request *rq, bool reserved)
2067{
2068 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2069 struct nvme_fc_ctrl *ctrl = op->ctrl;
James Smarte3994412016-12-02 00:28:42 -08002070
2071 /*
James Smart61bff8e2017-04-23 08:30:08 -07002072 * we can't individually ABTS an io without affecting the queue,
James Smart041018c2018-03-12 09:32:22 -07002073 * thus killing the queue, and thus the association.
James Smart61bff8e2017-04-23 08:30:08 -07002074 * So resolve by performing a controller reset, which will stop
2075 * the host/io stack, terminate the association on the link,
2076 * and recreate an association on the link.
James Smarte3994412016-12-02 00:28:42 -08002077 */
James Smart61bff8e2017-04-23 08:30:08 -07002078 nvme_fc_error_recovery(ctrl, "io timeout error");
James Smarte3994412016-12-02 00:28:42 -08002079
James Smart134aedc2017-10-19 16:11:39 -07002080 /*
2081 * the io abort has been initiated. Have the reset timer
2082 * restarted and the abort completion will complete the io
2083 * shortly. Avoids a synchronous wait while the abort finishes.
2084 */
2085 return BLK_EH_RESET_TIMER;
James Smarte3994412016-12-02 00:28:42 -08002086}
2087
2088static int
2089nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2090 struct nvme_fc_fcp_op *op)
2091{
2092 struct nvmefc_fcp_req *freq = &op->fcp_req;
James Smarte3994412016-12-02 00:28:42 -08002093 enum dma_data_direction dir;
2094 int ret;
2095
2096 freq->sg_cnt = 0;
2097
Christoph Hellwigb131c612017-01-13 12:29:12 +01002098 if (!blk_rq_payload_bytes(rq))
James Smarte3994412016-12-02 00:28:42 -08002099 return 0;
2100
2101 freq->sg_table.sgl = freq->first_sgl;
Christoph Hellwig19e420b2017-01-19 16:55:57 +01002102 ret = sg_alloc_table_chained(&freq->sg_table,
2103 blk_rq_nr_phys_segments(rq), freq->sg_table.sgl);
James Smarte3994412016-12-02 00:28:42 -08002104 if (ret)
2105 return -ENOMEM;
2106
2107 op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl);
Christoph Hellwig19e420b2017-01-19 16:55:57 +01002108 WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
James Smarte3994412016-12-02 00:28:42 -08002109 dir = (rq_data_dir(rq) == WRITE) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
2110 freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
2111 op->nents, dir);
2112 if (unlikely(freq->sg_cnt <= 0)) {
2113 sg_free_table_chained(&freq->sg_table, true);
2114 freq->sg_cnt = 0;
2115 return -EFAULT;
2116 }
2117
2118 /*
2119 * TODO: blk_integrity_rq(rq) for DIF
2120 */
2121 return 0;
2122}
2123
2124static void
2125nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2126 struct nvme_fc_fcp_op *op)
2127{
2128 struct nvmefc_fcp_req *freq = &op->fcp_req;
2129
2130 if (!freq->sg_cnt)
2131 return;
2132
2133 fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
2134 ((rq_data_dir(rq) == WRITE) ?
2135 DMA_TO_DEVICE : DMA_FROM_DEVICE));
2136
2137 nvme_cleanup_cmd(rq);
2138
2139 sg_free_table_chained(&freq->sg_table, true);
2140
2141 freq->sg_cnt = 0;
2142}
2143
2144/*
2145 * In FC, the queue is a logical thing. At transport connect, the target
2146 * creates its "queue" and returns a handle that is to be given to the
2147 * target whenever it posts something to the corresponding SQ. When an
2148 * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
2149 * command contained within the SQE, an io, and assigns a FC exchange
2150 * to it. The SQE and the associated SQ handle are sent in the initial
2151 * CMD IU sents on the exchange. All transfers relative to the io occur
2152 * as part of the exchange. The CQE is the last thing for the io,
2153 * which is transferred (explicitly or implicitly) with the RSP IU
2154 * sent on the exchange. After the CQE is received, the FC exchange is
2155 * terminaed and the Exchange may be used on a different io.
2156 *
2157 * The transport to LLDD api has the transport making a request for a
2158 * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
2159 * resource and transfers the command. The LLDD will then process all
2160 * steps to complete the io. Upon completion, the transport done routine
2161 * is called.
2162 *
2163 * So - while the operation is outstanding to the LLDD, there is a link
2164 * level FC exchange resource that is also outstanding. This must be
2165 * considered in all cleanup operations.
2166 */
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002167static blk_status_t
James Smarte3994412016-12-02 00:28:42 -08002168nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
2169 struct nvme_fc_fcp_op *op, u32 data_len,
2170 enum nvmefc_fcp_datadir io_dir)
2171{
2172 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2173 struct nvme_command *sqe = &cmdiu->sqe;
2174 u32 csn;
James Smartb12740d2018-02-28 14:49:10 -08002175 int ret, opstate;
James Smarte3994412016-12-02 00:28:42 -08002176
James Smart61bff8e2017-04-23 08:30:08 -07002177 /*
2178 * before attempting to send the io, check to see if we believe
2179 * the target device is present
2180 */
2181 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
Ming Lei86ff7c22018-01-30 22:04:57 -05002182 return BLK_STS_RESOURCE;
James Smart61bff8e2017-04-23 08:30:08 -07002183
James Smarte3994412016-12-02 00:28:42 -08002184 if (!nvme_fc_ctrl_get(ctrl))
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002185 return BLK_STS_IOERR;
James Smarte3994412016-12-02 00:28:42 -08002186
2187 /* format the FC-NVME CMD IU and fcp_req */
2188 cmdiu->connection_id = cpu_to_be64(queue->connection_id);
2189 csn = atomic_inc_return(&queue->csn);
2190 cmdiu->csn = cpu_to_be32(csn);
2191 cmdiu->data_len = cpu_to_be32(data_len);
2192 switch (io_dir) {
2193 case NVMEFC_FCP_WRITE:
2194 cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
2195 break;
2196 case NVMEFC_FCP_READ:
2197 cmdiu->flags = FCNVME_CMD_FLAGS_READ;
2198 break;
2199 case NVMEFC_FCP_NODATA:
2200 cmdiu->flags = 0;
2201 break;
2202 }
2203 op->fcp_req.payload_length = data_len;
2204 op->fcp_req.io_dir = io_dir;
2205 op->fcp_req.transferred_length = 0;
2206 op->fcp_req.rcv_rsplen = 0;
James Smart62eeacb2017-03-23 20:41:27 -07002207 op->fcp_req.status = NVME_SC_SUCCESS;
James Smarte3994412016-12-02 00:28:42 -08002208 op->fcp_req.sqid = cpu_to_le16(queue->qnum);
2209
2210 /*
2211 * validate per fabric rules, set fields mandated by fabric spec
2212 * as well as those by FC-NVME spec.
2213 */
2214 WARN_ON_ONCE(sqe->common.metadata);
James Smarte3994412016-12-02 00:28:42 -08002215 sqe->common.flags |= NVME_CMD_SGL_METABUF;
2216
2217 /*
James Smartd9d34c02017-09-07 13:20:24 -07002218 * format SQE DPTR field per FC-NVME rules:
2219 * type=0x5 Transport SGL Data Block Descriptor
2220 * subtype=0xA Transport-specific value
2221 * address=0
2222 * length=length of the data series
James Smarte3994412016-12-02 00:28:42 -08002223 */
James Smartd9d34c02017-09-07 13:20:24 -07002224 sqe->rw.dptr.sgl.type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2225 NVME_SGL_FMT_TRANSPORT_A;
James Smarte3994412016-12-02 00:28:42 -08002226 sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
2227 sqe->rw.dptr.sgl.addr = 0;
2228
James Smart78a7ac22017-04-23 08:30:07 -07002229 if (!(op->flags & FCOP_FLAGS_AEN)) {
James Smarte3994412016-12-02 00:28:42 -08002230 ret = nvme_fc_map_data(ctrl, op->rq, op);
2231 if (ret < 0) {
James Smarte3994412016-12-02 00:28:42 -08002232 nvme_cleanup_cmd(op->rq);
2233 nvme_fc_ctrl_put(ctrl);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002234 if (ret == -ENOMEM || ret == -EAGAIN)
2235 return BLK_STS_RESOURCE;
2236 return BLK_STS_IOERR;
James Smarte3994412016-12-02 00:28:42 -08002237 }
2238 }
2239
2240 fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
2241 sizeof(op->cmd_iu), DMA_TO_DEVICE);
2242
2243 atomic_set(&op->state, FCPOP_STATE_ACTIVE);
2244
James Smart78a7ac22017-04-23 08:30:07 -07002245 if (!(op->flags & FCOP_FLAGS_AEN))
James Smarte3994412016-12-02 00:28:42 -08002246 blk_mq_start_request(op->rq);
2247
2248 ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
2249 &ctrl->rport->remoteport,
2250 queue->lldd_handle, &op->fcp_req);
2251
2252 if (ret) {
James Smartb12740d2018-02-28 14:49:10 -08002253 opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
2254 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2255
James Smart8b25f352017-07-18 14:29:34 -07002256 if (!(op->flags & FCOP_FLAGS_AEN))
James Smarte3994412016-12-02 00:28:42 -08002257 nvme_fc_unmap_data(ctrl, op->rq, op);
James Smarte3994412016-12-02 00:28:42 -08002258
2259 nvme_fc_ctrl_put(ctrl);
2260
James Smart8b25f352017-07-18 14:29:34 -07002261 if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE &&
2262 ret != -EBUSY)
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002263 return BLK_STS_IOERR;
James Smarte3994412016-12-02 00:28:42 -08002264
Ming Lei86ff7c22018-01-30 22:04:57 -05002265 return BLK_STS_RESOURCE;
James Smarte3994412016-12-02 00:28:42 -08002266 }
2267
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002268 return BLK_STS_OK;
James Smarte3994412016-12-02 00:28:42 -08002269}
2270
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002271static blk_status_t
James Smarte3994412016-12-02 00:28:42 -08002272nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
2273 const struct blk_mq_queue_data *bd)
2274{
2275 struct nvme_ns *ns = hctx->queue->queuedata;
2276 struct nvme_fc_queue *queue = hctx->driver_data;
2277 struct nvme_fc_ctrl *ctrl = queue->ctrl;
2278 struct request *rq = bd->rq;
2279 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2280 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2281 struct nvme_command *sqe = &cmdiu->sqe;
2282 enum nvmefc_fcp_datadir io_dir;
2283 u32 data_len;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002284 blk_status_t ret;
James Smarte3994412016-12-02 00:28:42 -08002285
James Smartbb06ec312018-04-12 09:16:15 -06002286 ret = nvmf_check_if_ready(&queue->ctrl->ctrl, rq,
2287 test_bit(NVME_FC_Q_LIVE, &queue->flags),
2288 ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE);
Sagi Grimberg9e0ed162017-10-24 15:25:21 +03002289 if (unlikely(ret))
2290 return ret;
2291
James Smarte3994412016-12-02 00:28:42 -08002292 ret = nvme_setup_cmd(ns, rq, sqe);
2293 if (ret)
2294 return ret;
2295
Christoph Hellwigb131c612017-01-13 12:29:12 +01002296 data_len = blk_rq_payload_bytes(rq);
James Smarte3994412016-12-02 00:28:42 -08002297 if (data_len)
2298 io_dir = ((rq_data_dir(rq) == WRITE) ?
2299 NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
2300 else
2301 io_dir = NVMEFC_FCP_NODATA;
2302
2303 return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
2304}
2305
2306static struct blk_mq_tags *
2307nvme_fc_tagset(struct nvme_fc_queue *queue)
2308{
2309 if (queue->qnum == 0)
2310 return queue->ctrl->admin_tag_set.tags[queue->qnum];
2311
2312 return queue->ctrl->tag_set.tags[queue->qnum - 1];
2313}
2314
2315static int
2316nvme_fc_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
2317
2318{
2319 struct nvme_fc_queue *queue = hctx->driver_data;
2320 struct nvme_fc_ctrl *ctrl = queue->ctrl;
2321 struct request *req;
2322 struct nvme_fc_fcp_op *op;
2323
2324 req = blk_mq_tag_to_rq(nvme_fc_tagset(queue), tag);
James Smart61bff8e2017-04-23 08:30:08 -07002325 if (!req)
James Smarte3994412016-12-02 00:28:42 -08002326 return 0;
James Smarte3994412016-12-02 00:28:42 -08002327
2328 op = blk_mq_rq_to_pdu(req);
2329
2330 if ((atomic_read(&op->state) == FCPOP_STATE_ACTIVE) &&
2331 (ctrl->lport->ops->poll_queue))
2332 ctrl->lport->ops->poll_queue(&ctrl->lport->localport,
2333 queue->lldd_handle);
2334
2335 return ((atomic_read(&op->state) != FCPOP_STATE_ACTIVE));
2336}
2337
2338static void
Keith Buschad22c352017-11-07 15:13:12 -07002339nvme_fc_submit_async_event(struct nvme_ctrl *arg)
James Smarte3994412016-12-02 00:28:42 -08002340{
2341 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
2342 struct nvme_fc_fcp_op *aen_op;
James Smart61bff8e2017-04-23 08:30:08 -07002343 unsigned long flags;
2344 bool terminating = false;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002345 blk_status_t ret;
James Smarte3994412016-12-02 00:28:42 -08002346
James Smart61bff8e2017-04-23 08:30:08 -07002347 spin_lock_irqsave(&ctrl->lock, flags);
2348 if (ctrl->flags & FCCTRL_TERMIO)
2349 terminating = true;
2350 spin_unlock_irqrestore(&ctrl->lock, flags);
2351
2352 if (terminating)
2353 return;
2354
Keith Buschad22c352017-11-07 15:13:12 -07002355 aen_op = &ctrl->aen_ops[0];
James Smarte3994412016-12-02 00:28:42 -08002356
2357 ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
2358 NVMEFC_FCP_NODATA);
2359 if (ret)
2360 dev_err(ctrl->ctrl.device,
Keith Buschad22c352017-11-07 15:13:12 -07002361 "failed async event work\n");
James Smarte3994412016-12-02 00:28:42 -08002362}
2363
2364static void
James Smartc3aedd222018-02-06 06:48:30 -08002365nvme_fc_complete_rq(struct request *rq)
James Smarte3994412016-12-02 00:28:42 -08002366{
2367 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2368 struct nvme_fc_ctrl *ctrl = op->ctrl;
James Smarte3994412016-12-02 00:28:42 -08002369
James Smart78a7ac22017-04-23 08:30:07 -07002370 atomic_set(&op->state, FCPOP_STATE_IDLE);
James Smarte3994412016-12-02 00:28:42 -08002371
James Smarte3994412016-12-02 00:28:42 -08002372 nvme_fc_unmap_data(ctrl, rq, op);
Christoph Hellwig77f02a72017-03-30 13:41:32 +02002373 nvme_complete_rq(rq);
James Smarte3994412016-12-02 00:28:42 -08002374 nvme_fc_ctrl_put(ctrl);
James Smart78a7ac22017-04-23 08:30:07 -07002375}
2376
James Smarte3994412016-12-02 00:28:42 -08002377/*
2378 * This routine is used by the transport when it needs to find active
2379 * io on a queue that is to be terminated. The transport uses
2380 * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
2381 * this routine to kill them on a 1 by 1 basis.
2382 *
2383 * As FC allocates FC exchange for each io, the transport must contact
2384 * the LLDD to terminate the exchange, thus releasing the FC exchange.
2385 * After terminating the exchange the LLDD will call the transport's
2386 * normal io done path for the request, but it will have an aborted
2387 * status. The done path will return the io request back to the block
2388 * layer with an error status.
2389 */
2390static void
2391nvme_fc_terminate_exchange(struct request *req, void *data, bool reserved)
2392{
2393 struct nvme_ctrl *nctrl = data;
2394 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2395 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
James Smarte3994412016-12-02 00:28:42 -08002396
James Smart3efd6e82018-02-06 06:48:29 -08002397 __nvme_fc_abort_op(ctrl, op);
James Smart78a7ac22017-04-23 08:30:07 -07002398}
James Smarte3994412016-12-02 00:28:42 -08002399
James Smarte3994412016-12-02 00:28:42 -08002400
James Smart61bff8e2017-04-23 08:30:08 -07002401static const struct blk_mq_ops nvme_fc_mq_ops = {
2402 .queue_rq = nvme_fc_queue_rq,
2403 .complete = nvme_fc_complete_rq,
2404 .init_request = nvme_fc_init_request,
2405 .exit_request = nvme_fc_exit_request,
James Smart61bff8e2017-04-23 08:30:08 -07002406 .init_hctx = nvme_fc_init_hctx,
2407 .poll = nvme_fc_poll,
2408 .timeout = nvme_fc_timeout,
James Smarte3994412016-12-02 00:28:42 -08002409};
2410
2411static int
2412nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl)
2413{
2414 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
Sagi Grimberg73141832017-06-29 11:16:49 +03002415 unsigned int nr_io_queues;
James Smarte3994412016-12-02 00:28:42 -08002416 int ret;
2417
Sagi Grimberg73141832017-06-29 11:16:49 +03002418 nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
2419 ctrl->lport->ops->max_hw_queues);
2420 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
James Smarte3994412016-12-02 00:28:42 -08002421 if (ret) {
2422 dev_info(ctrl->ctrl.device,
2423 "set_queue_count failed: %d\n", ret);
2424 return ret;
2425 }
2426
Sagi Grimberg73141832017-06-29 11:16:49 +03002427 ctrl->ctrl.queue_count = nr_io_queues + 1;
2428 if (!nr_io_queues)
James Smarte3994412016-12-02 00:28:42 -08002429 return 0;
2430
James Smarte3994412016-12-02 00:28:42 -08002431 nvme_fc_init_io_queues(ctrl);
2432
2433 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
2434 ctrl->tag_set.ops = &nvme_fc_mq_ops;
2435 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
2436 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
2437 ctrl->tag_set.numa_node = NUMA_NO_NODE;
2438 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
2439 ctrl->tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) +
2440 (SG_CHUNK_SIZE *
2441 sizeof(struct scatterlist)) +
2442 ctrl->lport->ops->fcprqst_priv_sz;
2443 ctrl->tag_set.driver_data = ctrl;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002444 ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1;
James Smarte3994412016-12-02 00:28:42 -08002445 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
2446
2447 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
2448 if (ret)
2449 return ret;
2450
2451 ctrl->ctrl.tagset = &ctrl->tag_set;
2452
2453 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
2454 if (IS_ERR(ctrl->ctrl.connect_q)) {
2455 ret = PTR_ERR(ctrl->ctrl.connect_q);
2456 goto out_free_tag_set;
2457 }
2458
James Smartd157e532018-03-07 15:59:36 -08002459 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
James Smarte3994412016-12-02 00:28:42 -08002460 if (ret)
2461 goto out_cleanup_blk_queue;
2462
James Smartd157e532018-03-07 15:59:36 -08002463 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
James Smarte3994412016-12-02 00:28:42 -08002464 if (ret)
2465 goto out_delete_hw_queues;
2466
James Smart4c984152018-06-13 14:07:37 -07002467 ctrl->ioq_live = true;
2468
James Smarte3994412016-12-02 00:28:42 -08002469 return 0;
2470
2471out_delete_hw_queues:
2472 nvme_fc_delete_hw_io_queues(ctrl);
2473out_cleanup_blk_queue:
James Smarte3994412016-12-02 00:28:42 -08002474 blk_cleanup_queue(ctrl->ctrl.connect_q);
2475out_free_tag_set:
2476 blk_mq_free_tag_set(&ctrl->tag_set);
2477 nvme_fc_free_io_queues(ctrl);
2478
2479 /* force put free routine to ignore io queues */
2480 ctrl->ctrl.tagset = NULL;
2481
2482 return ret;
2483}
2484
James Smart61bff8e2017-04-23 08:30:08 -07002485static int
2486nvme_fc_reinit_io_queues(struct nvme_fc_ctrl *ctrl)
2487{
2488 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
Sagi Grimberg73141832017-06-29 11:16:49 +03002489 unsigned int nr_io_queues;
James Smart61bff8e2017-04-23 08:30:08 -07002490 int ret;
2491
Sagi Grimberg73141832017-06-29 11:16:49 +03002492 nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
2493 ctrl->lport->ops->max_hw_queues);
2494 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
James Smart61bff8e2017-04-23 08:30:08 -07002495 if (ret) {
2496 dev_info(ctrl->ctrl.device,
2497 "set_queue_count failed: %d\n", ret);
2498 return ret;
2499 }
2500
Sagi Grimberg73141832017-06-29 11:16:49 +03002501 ctrl->ctrl.queue_count = nr_io_queues + 1;
James Smart61bff8e2017-04-23 08:30:08 -07002502 /* check for io queues existing */
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002503 if (ctrl->ctrl.queue_count == 1)
James Smart61bff8e2017-04-23 08:30:08 -07002504 return 0;
2505
James Smart61bff8e2017-04-23 08:30:08 -07002506 nvme_fc_init_io_queues(ctrl);
2507
Sagi Grimberg31b84462017-10-11 12:53:07 +03002508 ret = nvme_reinit_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
James Smart61bff8e2017-04-23 08:30:08 -07002509 if (ret)
2510 goto out_free_io_queues;
2511
James Smartd157e532018-03-07 15:59:36 -08002512 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
James Smart61bff8e2017-04-23 08:30:08 -07002513 if (ret)
2514 goto out_free_io_queues;
2515
James Smartd157e532018-03-07 15:59:36 -08002516 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
James Smart61bff8e2017-04-23 08:30:08 -07002517 if (ret)
2518 goto out_delete_hw_queues;
2519
Sagi Grimbergcda5fd12017-06-29 11:20:10 +03002520 blk_mq_update_nr_hw_queues(&ctrl->tag_set, nr_io_queues);
2521
James Smart61bff8e2017-04-23 08:30:08 -07002522 return 0;
2523
2524out_delete_hw_queues:
2525 nvme_fc_delete_hw_io_queues(ctrl);
2526out_free_io_queues:
2527 nvme_fc_free_io_queues(ctrl);
2528 return ret;
2529}
2530
James Smart158bfb82017-11-03 08:13:17 -07002531static void
2532nvme_fc_rport_active_on_lport(struct nvme_fc_rport *rport)
2533{
2534 struct nvme_fc_lport *lport = rport->lport;
2535
2536 atomic_inc(&lport->act_rport_cnt);
2537}
2538
2539static void
2540nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport *rport)
2541{
2542 struct nvme_fc_lport *lport = rport->lport;
2543 u32 cnt;
2544
2545 cnt = atomic_dec_return(&lport->act_rport_cnt);
2546 if (cnt == 0 && lport->localport.port_state == FC_OBJSTATE_DELETED)
2547 lport->ops->localport_delete(&lport->localport);
2548}
2549
2550static int
2551nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl *ctrl)
2552{
2553 struct nvme_fc_rport *rport = ctrl->rport;
2554 u32 cnt;
2555
2556 if (ctrl->assoc_active)
2557 return 1;
2558
2559 ctrl->assoc_active = true;
2560 cnt = atomic_inc_return(&rport->act_ctrl_cnt);
2561 if (cnt == 1)
2562 nvme_fc_rport_active_on_lport(rport);
2563
2564 return 0;
2565}
2566
2567static int
2568nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl *ctrl)
2569{
2570 struct nvme_fc_rport *rport = ctrl->rport;
2571 struct nvme_fc_lport *lport = rport->lport;
2572 u32 cnt;
2573
2574 /* ctrl->assoc_active=false will be set independently */
2575
2576 cnt = atomic_dec_return(&rport->act_ctrl_cnt);
2577 if (cnt == 0) {
2578 if (rport->remoteport.port_state == FC_OBJSTATE_DELETED)
2579 lport->ops->remoteport_delete(&rport->remoteport);
2580 nvme_fc_rport_inactive_on_lport(rport);
2581 }
2582
2583 return 0;
2584}
2585
James Smart61bff8e2017-04-23 08:30:08 -07002586/*
2587 * This routine restarts the controller on the host side, and
2588 * on the link side, recreates the controller association.
2589 */
2590static int
2591nvme_fc_create_association(struct nvme_fc_ctrl *ctrl)
2592{
2593 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
James Smart61bff8e2017-04-23 08:30:08 -07002594 int ret;
2595 bool changed;
2596
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +03002597 ++ctrl->ctrl.nr_reconnects;
James Smart61bff8e2017-04-23 08:30:08 -07002598
James Smart96e24802017-10-25 16:43:16 -07002599 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
2600 return -ENODEV;
2601
James Smart158bfb82017-11-03 08:13:17 -07002602 if (nvme_fc_ctlr_active_on_rport(ctrl))
2603 return -ENOTUNIQ;
2604
James Smart61bff8e2017-04-23 08:30:08 -07002605 /*
2606 * Create the admin queue
2607 */
2608
Keith Busch08e15072017-11-07 15:13:11 -07002609 nvme_fc_init_queue(ctrl, 0);
James Smart61bff8e2017-04-23 08:30:08 -07002610
2611 ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0,
James Smartd157e532018-03-07 15:59:36 -08002612 NVME_AQ_DEPTH);
James Smart61bff8e2017-04-23 08:30:08 -07002613 if (ret)
2614 goto out_free_queue;
2615
2616 ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0],
James Smartd157e532018-03-07 15:59:36 -08002617 NVME_AQ_DEPTH, (NVME_AQ_DEPTH / 4));
James Smart61bff8e2017-04-23 08:30:08 -07002618 if (ret)
2619 goto out_delete_hw_queue;
2620
James Smart4c984152018-06-13 14:07:37 -07002621 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
James Smart61bff8e2017-04-23 08:30:08 -07002622
2623 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
2624 if (ret)
2625 goto out_disconnect_admin_queue;
2626
Sagi Grimberg9e0ed162017-10-24 15:25:21 +03002627 set_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags);
2628
James Smart61bff8e2017-04-23 08:30:08 -07002629 /*
2630 * Check controller capabilities
2631 *
2632 * todo:- add code to check if ctrl attributes changed from
2633 * prior connection values
2634 */
2635
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03002636 ret = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->ctrl.cap);
James Smart61bff8e2017-04-23 08:30:08 -07002637 if (ret) {
2638 dev_err(ctrl->ctrl.device,
2639 "prop_get NVME_REG_CAP failed\n");
2640 goto out_disconnect_admin_queue;
2641 }
2642
2643 ctrl->ctrl.sqsize =
James Smartd157e532018-03-07 15:59:36 -08002644 min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize);
James Smart61bff8e2017-04-23 08:30:08 -07002645
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03002646 ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
James Smart61bff8e2017-04-23 08:30:08 -07002647 if (ret)
2648 goto out_disconnect_admin_queue;
2649
James Smartecad0d22017-10-23 15:11:36 -07002650 ctrl->ctrl.max_hw_sectors =
2651 (ctrl->lport->ops->max_sgl_segments - 1) << (PAGE_SHIFT - 9);
James Smart61bff8e2017-04-23 08:30:08 -07002652
2653 ret = nvme_init_identify(&ctrl->ctrl);
2654 if (ret)
2655 goto out_disconnect_admin_queue;
2656
2657 /* sanity checks */
2658
2659 /* FC-NVME does not have other data in the capsule */
2660 if (ctrl->ctrl.icdoff) {
2661 dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n",
2662 ctrl->ctrl.icdoff);
2663 goto out_disconnect_admin_queue;
2664 }
2665
James Smart61bff8e2017-04-23 08:30:08 -07002666 /* FC-NVME supports normal SGL Data Block Descriptors */
2667
2668 if (opts->queue_size > ctrl->ctrl.maxcmd) {
2669 /* warn if maxcmd is lower than queue_size */
2670 dev_warn(ctrl->ctrl.device,
2671 "queue_size %zu > ctrl maxcmd %u, reducing "
2672 "to queue_size\n",
2673 opts->queue_size, ctrl->ctrl.maxcmd);
2674 opts->queue_size = ctrl->ctrl.maxcmd;
2675 }
2676
James Smartd157e532018-03-07 15:59:36 -08002677 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
2678 /* warn if sqsize is lower than queue_size */
2679 dev_warn(ctrl->ctrl.device,
2680 "queue_size %zu > ctrl sqsize %u, clamping down\n",
2681 opts->queue_size, ctrl->ctrl.sqsize + 1);
2682 opts->queue_size = ctrl->ctrl.sqsize + 1;
2683 }
2684
James Smart61bff8e2017-04-23 08:30:08 -07002685 ret = nvme_fc_init_aen_ops(ctrl);
2686 if (ret)
2687 goto out_term_aen_ops;
2688
2689 /*
2690 * Create the io queues
2691 */
2692
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002693 if (ctrl->ctrl.queue_count > 1) {
James Smart4c984152018-06-13 14:07:37 -07002694 if (!ctrl->ioq_live)
James Smart61bff8e2017-04-23 08:30:08 -07002695 ret = nvme_fc_create_io_queues(ctrl);
2696 else
2697 ret = nvme_fc_reinit_io_queues(ctrl);
2698 if (ret)
2699 goto out_term_aen_ops;
2700 }
2701
2702 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
James Smart61bff8e2017-04-23 08:30:08 -07002703
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +03002704 ctrl->ctrl.nr_reconnects = 0;
James Smart61bff8e2017-04-23 08:30:08 -07002705
James Smart44c6ec72017-10-25 16:43:14 -07002706 if (changed)
2707 nvme_start_ctrl(&ctrl->ctrl);
James Smart61bff8e2017-04-23 08:30:08 -07002708
2709 return 0; /* Success */
2710
2711out_term_aen_ops:
2712 nvme_fc_term_aen_ops(ctrl);
James Smart61bff8e2017-04-23 08:30:08 -07002713out_disconnect_admin_queue:
2714 /* send a Disconnect(association) LS to fc-nvme target */
2715 nvme_fc_xmt_disconnect_assoc(ctrl);
2716out_delete_hw_queue:
2717 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
2718out_free_queue:
2719 nvme_fc_free_queue(&ctrl->queues[0]);
James Smart158bfb82017-11-03 08:13:17 -07002720 ctrl->assoc_active = false;
2721 nvme_fc_ctlr_inactive_on_rport(ctrl);
James Smart61bff8e2017-04-23 08:30:08 -07002722
2723 return ret;
2724}
2725
2726/*
2727 * This routine stops operation of the controller on the host side.
2728 * On the host os stack side: Admin and IO queues are stopped,
2729 * outstanding ios on them terminated via FC ABTS.
2730 * On the link side: the association is terminated.
2731 */
2732static void
2733nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
2734{
2735 unsigned long flags;
2736
James Smart158bfb82017-11-03 08:13:17 -07002737 if (!ctrl->assoc_active)
2738 return;
2739 ctrl->assoc_active = false;
2740
James Smart61bff8e2017-04-23 08:30:08 -07002741 spin_lock_irqsave(&ctrl->lock, flags);
2742 ctrl->flags |= FCCTRL_TERMIO;
2743 ctrl->iocnt = 0;
2744 spin_unlock_irqrestore(&ctrl->lock, flags);
2745
2746 /*
2747 * If io queues are present, stop them and terminate all outstanding
2748 * ios on them. As FC allocates FC exchange for each io, the
2749 * transport must contact the LLDD to terminate the exchange,
2750 * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
2751 * to tell us what io's are busy and invoke a transport routine
2752 * to kill them with the LLDD. After terminating the exchange
2753 * the LLDD will call the transport's normal io done path, but it
2754 * will have an aborted status. The done path will return the
2755 * io requests back to the block layer as part of normal completions
2756 * (but with error status).
2757 */
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002758 if (ctrl->ctrl.queue_count > 1) {
James Smart61bff8e2017-04-23 08:30:08 -07002759 nvme_stop_queues(&ctrl->ctrl);
2760 blk_mq_tagset_busy_iter(&ctrl->tag_set,
2761 nvme_fc_terminate_exchange, &ctrl->ctrl);
2762 }
2763
2764 /*
2765 * Other transports, which don't have link-level contexts bound
2766 * to sqe's, would try to gracefully shutdown the controller by
2767 * writing the registers for shutdown and polling (call
2768 * nvme_shutdown_ctrl()). Given a bunch of i/o was potentially
2769 * just aborted and we will wait on those contexts, and given
2770 * there was no indication of how live the controlelr is on the
2771 * link, don't send more io to create more contexts for the
2772 * shutdown. Let the controller fail via keepalive failure if
2773 * its still present.
2774 */
2775
2776 /*
2777 * clean up the admin queue. Same thing as above.
2778 * use blk_mq_tagset_busy_itr() and the transport routine to
2779 * terminate the exchanges.
2780 */
James Smart4c984152018-06-13 14:07:37 -07002781 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
James Smart61bff8e2017-04-23 08:30:08 -07002782 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
2783 nvme_fc_terminate_exchange, &ctrl->ctrl);
2784
2785 /* kill the aens as they are a separate path */
2786 nvme_fc_abort_aen_ops(ctrl);
2787
2788 /* wait for all io that had to be aborted */
James Smart8a82dbf2017-10-09 13:39:44 -07002789 spin_lock_irq(&ctrl->lock);
James Smart36715cf2017-05-22 15:28:42 -07002790 wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock);
James Smart61bff8e2017-04-23 08:30:08 -07002791 ctrl->flags &= ~FCCTRL_TERMIO;
James Smart8a82dbf2017-10-09 13:39:44 -07002792 spin_unlock_irq(&ctrl->lock);
James Smart61bff8e2017-04-23 08:30:08 -07002793
2794 nvme_fc_term_aen_ops(ctrl);
2795
2796 /*
2797 * send a Disconnect(association) LS to fc-nvme target
2798 * Note: could have been sent at top of process, but
2799 * cleaner on link traffic if after the aborts complete.
2800 * Note: if association doesn't exist, association_id will be 0
2801 */
2802 if (ctrl->association_id)
2803 nvme_fc_xmt_disconnect_assoc(ctrl);
2804
2805 if (ctrl->ctrl.tagset) {
2806 nvme_fc_delete_hw_io_queues(ctrl);
2807 nvme_fc_free_io_queues(ctrl);
2808 }
2809
2810 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
2811 nvme_fc_free_queue(&ctrl->queues[0]);
James Smart158bfb82017-11-03 08:13:17 -07002812
James Smartd625d052018-01-11 14:29:22 -08002813 /* re-enable the admin_q so anything new can fast fail */
2814 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
2815
James Smart158bfb82017-11-03 08:13:17 -07002816 nvme_fc_ctlr_inactive_on_rport(ctrl);
James Smart61bff8e2017-04-23 08:30:08 -07002817}
2818
2819static void
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002820nvme_fc_delete_ctrl(struct nvme_ctrl *nctrl)
James Smart61bff8e2017-04-23 08:30:08 -07002821{
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002822 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
James Smart61bff8e2017-04-23 08:30:08 -07002823
James Smart61bff8e2017-04-23 08:30:08 -07002824 cancel_delayed_work_sync(&ctrl->connect_work);
James Smart61bff8e2017-04-23 08:30:08 -07002825 /*
2826 * kill the association on the link side. this will block
2827 * waiting for io to terminate
2828 */
2829 nvme_fc_delete_association(ctrl);
James Smart0fd997d2018-01-11 15:21:38 -08002830
2831 /* resume the io queues so that things will fast fail */
2832 nvme_start_queues(nctrl);
James Smart61bff8e2017-04-23 08:30:08 -07002833}
2834
2835static void
James Smart5bbecdb2017-05-15 17:10:16 -07002836nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status)
2837{
James Smart2b632972017-10-25 16:43:17 -07002838 struct nvme_fc_rport *rport = ctrl->rport;
2839 struct nvme_fc_remote_port *portptr = &rport->remoteport;
2840 unsigned long recon_delay = ctrl->ctrl.opts->reconnect_delay * HZ;
2841 bool recon = true;
2842
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02002843 if (ctrl->ctrl.state != NVME_CTRL_CONNECTING)
James Smart5bbecdb2017-05-15 17:10:16 -07002844 return;
James Smart5bbecdb2017-05-15 17:10:16 -07002845
James Smart2b632972017-10-25 16:43:17 -07002846 if (portptr->port_state == FC_OBJSTATE_ONLINE)
James Smart5bbecdb2017-05-15 17:10:16 -07002847 dev_info(ctrl->ctrl.device,
James Smart2b632972017-10-25 16:43:17 -07002848 "NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
2849 ctrl->cnum, status);
2850 else if (time_after_eq(jiffies, rport->dev_loss_end))
2851 recon = false;
2852
2853 if (recon && nvmf_should_reconnect(&ctrl->ctrl)) {
2854 if (portptr->port_state == FC_OBJSTATE_ONLINE)
2855 dev_info(ctrl->ctrl.device,
2856 "NVME-FC{%d}: Reconnect attempt in %ld "
2857 "seconds\n",
2858 ctrl->cnum, recon_delay / HZ);
2859 else if (time_after(jiffies + recon_delay, rport->dev_loss_end))
2860 recon_delay = rport->dev_loss_end - jiffies;
2861
2862 queue_delayed_work(nvme_wq, &ctrl->connect_work, recon_delay);
James Smart5bbecdb2017-05-15 17:10:16 -07002863 } else {
James Smart2b632972017-10-25 16:43:17 -07002864 if (portptr->port_state == FC_OBJSTATE_ONLINE)
2865 dev_warn(ctrl->ctrl.device,
James Smart5bbecdb2017-05-15 17:10:16 -07002866 "NVME-FC{%d}: Max reconnect attempts (%d) "
Max Gurtovoy77d06122018-03-11 17:46:06 +02002867 "reached.\n",
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +03002868 ctrl->cnum, ctrl->ctrl.nr_reconnects);
James Smart2b632972017-10-25 16:43:17 -07002869 else
2870 dev_warn(ctrl->ctrl.device,
2871 "NVME-FC{%d}: dev_loss_tmo (%d) expired "
Max Gurtovoy77d06122018-03-11 17:46:06 +02002872 "while waiting for remoteport connectivity.\n",
2873 ctrl->cnum, portptr->dev_loss_tmo);
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002874 WARN_ON(nvme_delete_ctrl(&ctrl->ctrl));
James Smart5bbecdb2017-05-15 17:10:16 -07002875 }
2876}
2877
2878static void
James Smart61bff8e2017-04-23 08:30:08 -07002879nvme_fc_reset_ctrl_work(struct work_struct *work)
2880{
2881 struct nvme_fc_ctrl *ctrl =
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002882 container_of(work, struct nvme_fc_ctrl, ctrl.reset_work);
James Smart61bff8e2017-04-23 08:30:08 -07002883 int ret;
2884
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002885 nvme_stop_ctrl(&ctrl->ctrl);
James Smart44c6ec72017-10-25 16:43:14 -07002886
James Smart61bff8e2017-04-23 08:30:08 -07002887 /* will block will waiting for io to terminate */
2888 nvme_fc_delete_association(ctrl);
2889
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02002890 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
James Smart44c6ec72017-10-25 16:43:14 -07002891 dev_err(ctrl->ctrl.device,
2892 "NVME-FC{%d}: error_recovery: Couldn't change state "
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02002893 "to CONNECTING\n", ctrl->cnum);
James Smart44c6ec72017-10-25 16:43:14 -07002894 return;
2895 }
2896
James Smart2b632972017-10-25 16:43:17 -07002897 if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE)
James Smart96e24802017-10-25 16:43:16 -07002898 ret = nvme_fc_create_association(ctrl);
James Smart2b632972017-10-25 16:43:17 -07002899 else
2900 ret = -ENOTCONN;
2901
James Smart5bbecdb2017-05-15 17:10:16 -07002902 if (ret)
2903 nvme_fc_reconnect_or_delete(ctrl, ret);
2904 else
James Smart61bff8e2017-04-23 08:30:08 -07002905 dev_info(ctrl->ctrl.device,
James Smart2b632972017-10-25 16:43:17 -07002906 "NVME-FC{%d}: controller reset complete\n",
2907 ctrl->cnum);
James Smart61bff8e2017-04-23 08:30:08 -07002908}
2909
James Smart61bff8e2017-04-23 08:30:08 -07002910static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
2911 .name = "fc",
2912 .module = THIS_MODULE,
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02002913 .flags = NVME_F_FABRICS,
James Smart61bff8e2017-04-23 08:30:08 -07002914 .reg_read32 = nvmf_reg_read32,
2915 .reg_read64 = nvmf_reg_read64,
2916 .reg_write32 = nvmf_reg_write32,
James Smart61bff8e2017-04-23 08:30:08 -07002917 .free_ctrl = nvme_fc_nvme_ctrl_freed,
2918 .submit_async_event = nvme_fc_submit_async_event,
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002919 .delete_ctrl = nvme_fc_delete_ctrl,
James Smart61bff8e2017-04-23 08:30:08 -07002920 .get_address = nvmf_get_address,
Sagi Grimberg31b84462017-10-11 12:53:07 +03002921 .reinit_request = nvme_fc_reinit_request,
James Smart61bff8e2017-04-23 08:30:08 -07002922};
2923
2924static void
2925nvme_fc_connect_ctrl_work(struct work_struct *work)
2926{
2927 int ret;
2928
2929 struct nvme_fc_ctrl *ctrl =
2930 container_of(to_delayed_work(work),
2931 struct nvme_fc_ctrl, connect_work);
2932
2933 ret = nvme_fc_create_association(ctrl);
James Smart5bbecdb2017-05-15 17:10:16 -07002934 if (ret)
2935 nvme_fc_reconnect_or_delete(ctrl, ret);
2936 else
James Smart61bff8e2017-04-23 08:30:08 -07002937 dev_info(ctrl->ctrl.device,
James Smart4c984152018-06-13 14:07:37 -07002938 "NVME-FC{%d}: controller connect complete\n",
James Smart61bff8e2017-04-23 08:30:08 -07002939 ctrl->cnum);
2940}
2941
2942
2943static const struct blk_mq_ops nvme_fc_admin_mq_ops = {
2944 .queue_rq = nvme_fc_queue_rq,
2945 .complete = nvme_fc_complete_rq,
Christoph Hellwig76f983c2017-06-13 09:15:20 +02002946 .init_request = nvme_fc_init_request,
James Smart61bff8e2017-04-23 08:30:08 -07002947 .exit_request = nvme_fc_exit_request,
James Smart61bff8e2017-04-23 08:30:08 -07002948 .init_hctx = nvme_fc_init_admin_hctx,
2949 .timeout = nvme_fc_timeout,
2950};
2951
James Smarte3994412016-12-02 00:28:42 -08002952
James Smart56d5f4f2017-10-20 16:17:08 -07002953/*
2954 * Fails a controller request if it matches an existing controller
2955 * (association) with the same tuple:
2956 * <Host NQN, Host ID, local FC port, remote FC port, SUBSYS NQN>
2957 *
2958 * The ports don't need to be compared as they are intrinsically
2959 * already matched by the port pointers supplied.
2960 */
2961static bool
2962nvme_fc_existing_controller(struct nvme_fc_rport *rport,
2963 struct nvmf_ctrl_options *opts)
2964{
2965 struct nvme_fc_ctrl *ctrl;
2966 unsigned long flags;
2967 bool found = false;
2968
2969 spin_lock_irqsave(&rport->lock, flags);
2970 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
2971 found = nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts);
2972 if (found)
2973 break;
2974 }
2975 spin_unlock_irqrestore(&rport->lock, flags);
2976
2977 return found;
2978}
2979
James Smarte3994412016-12-02 00:28:42 -08002980static struct nvme_ctrl *
James Smart61bff8e2017-04-23 08:30:08 -07002981nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
James Smarte3994412016-12-02 00:28:42 -08002982 struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
2983{
2984 struct nvme_fc_ctrl *ctrl;
2985 unsigned long flags;
James Smart4c984152018-06-13 14:07:37 -07002986 int ret, idx;
James Smarte3994412016-12-02 00:28:42 -08002987
James Smart85e6a6a2017-05-05 16:13:15 -07002988 if (!(rport->remoteport.port_role &
2989 (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) {
2990 ret = -EBADR;
2991 goto out_fail;
2992 }
2993
James Smart56d5f4f2017-10-20 16:17:08 -07002994 if (!opts->duplicate_connect &&
2995 nvme_fc_existing_controller(rport, opts)) {
2996 ret = -EALREADY;
2997 goto out_fail;
2998 }
2999
James Smarte3994412016-12-02 00:28:42 -08003000 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
3001 if (!ctrl) {
3002 ret = -ENOMEM;
3003 goto out_fail;
3004 }
3005
3006 idx = ida_simple_get(&nvme_fc_ctrl_cnt, 0, 0, GFP_KERNEL);
3007 if (idx < 0) {
3008 ret = -ENOSPC;
3009 goto out_free_ctrl;
3010 }
3011
3012 ctrl->ctrl.opts = opts;
James Smart4c984152018-06-13 14:07:37 -07003013 ctrl->ctrl.nr_reconnects = 0;
James Smarte3994412016-12-02 00:28:42 -08003014 INIT_LIST_HEAD(&ctrl->ctrl_list);
James Smarte3994412016-12-02 00:28:42 -08003015 ctrl->lport = lport;
3016 ctrl->rport = rport;
3017 ctrl->dev = lport->dev;
James Smarte3994412016-12-02 00:28:42 -08003018 ctrl->cnum = idx;
James Smart4c984152018-06-13 14:07:37 -07003019 ctrl->ioq_live = false;
James Smart158bfb82017-11-03 08:13:17 -07003020 ctrl->assoc_active = false;
James Smart8a82dbf2017-10-09 13:39:44 -07003021 init_waitqueue_head(&ctrl->ioabort_wait);
James Smarte3994412016-12-02 00:28:42 -08003022
James Smarte3994412016-12-02 00:28:42 -08003023 get_device(ctrl->dev);
3024 kref_init(&ctrl->ref);
3025
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02003026 INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work);
James Smart61bff8e2017-04-23 08:30:08 -07003027 INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
James Smarte3994412016-12-02 00:28:42 -08003028 spin_lock_init(&ctrl->lock);
3029
3030 /* io queue count */
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03003031 ctrl->ctrl.queue_count = min_t(unsigned int,
James Smarte3994412016-12-02 00:28:42 -08003032 opts->nr_io_queues,
3033 lport->ops->max_hw_queues);
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03003034 ctrl->ctrl.queue_count++; /* +1 for admin queue */
James Smarte3994412016-12-02 00:28:42 -08003035
3036 ctrl->ctrl.sqsize = opts->queue_size - 1;
3037 ctrl->ctrl.kato = opts->kato;
James Smart4c984152018-06-13 14:07:37 -07003038 ctrl->ctrl.cntlid = 0xffff;
James Smarte3994412016-12-02 00:28:42 -08003039
3040 ret = -ENOMEM;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03003041 ctrl->queues = kcalloc(ctrl->ctrl.queue_count,
3042 sizeof(struct nvme_fc_queue), GFP_KERNEL);
James Smarte3994412016-12-02 00:28:42 -08003043 if (!ctrl->queues)
James Smart61bff8e2017-04-23 08:30:08 -07003044 goto out_free_ida;
James Smarte3994412016-12-02 00:28:42 -08003045
James Smart61bff8e2017-04-23 08:30:08 -07003046 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
3047 ctrl->admin_tag_set.ops = &nvme_fc_admin_mq_ops;
Keith Busch38dabe22017-11-07 15:13:10 -07003048 ctrl->admin_tag_set.queue_depth = NVME_AQ_MQ_TAG_DEPTH;
James Smart61bff8e2017-04-23 08:30:08 -07003049 ctrl->admin_tag_set.reserved_tags = 2; /* fabric connect + Keep-Alive */
3050 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
3051 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) +
3052 (SG_CHUNK_SIZE *
3053 sizeof(struct scatterlist)) +
3054 ctrl->lport->ops->fcprqst_priv_sz;
3055 ctrl->admin_tag_set.driver_data = ctrl;
3056 ctrl->admin_tag_set.nr_hw_queues = 1;
3057 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
Israel Rukshin5a22e2b2017-10-18 12:38:25 +00003058 ctrl->admin_tag_set.flags = BLK_MQ_F_NO_SCHED;
James Smart61bff8e2017-04-23 08:30:08 -07003059
3060 ret = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
James Smarte3994412016-12-02 00:28:42 -08003061 if (ret)
James Smart61bff8e2017-04-23 08:30:08 -07003062 goto out_free_queues;
Sagi Grimberg34b6c232017-07-10 09:22:29 +03003063 ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set;
James Smarte3994412016-12-02 00:28:42 -08003064
James Smart61bff8e2017-04-23 08:30:08 -07003065 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
3066 if (IS_ERR(ctrl->ctrl.admin_q)) {
3067 ret = PTR_ERR(ctrl->ctrl.admin_q);
3068 goto out_free_admin_tag_set;
James Smarte3994412016-12-02 00:28:42 -08003069 }
3070
James Smart61bff8e2017-04-23 08:30:08 -07003071 /*
3072 * Would have been nice to init io queues tag set as well.
3073 * However, we require interaction from the controller
3074 * for max io queue count before we can do so.
3075 * Defer this to the connect path.
3076 */
James Smarte3994412016-12-02 00:28:42 -08003077
James Smart61bff8e2017-04-23 08:30:08 -07003078 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
James Smarte3994412016-12-02 00:28:42 -08003079 if (ret)
James Smart61bff8e2017-04-23 08:30:08 -07003080 goto out_cleanup_admin_q;
James Smarte3994412016-12-02 00:28:42 -08003081
James Smart61bff8e2017-04-23 08:30:08 -07003082 /* at this point, teardown path changes to ref counting on nvme ctrl */
James Smarte3994412016-12-02 00:28:42 -08003083
3084 spin_lock_irqsave(&rport->lock, flags);
3085 list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
3086 spin_unlock_irqrestore(&rport->lock, flags);
3087
James Smart4c984152018-06-13 14:07:37 -07003088 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING) ||
3089 !nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
James Smart17c4dc62017-10-09 16:39:22 -07003090 dev_err(ctrl->ctrl.device,
James Smart4c984152018-06-13 14:07:37 -07003091 "NVME-FC{%d}: failed to init ctrl state\n", ctrl->cnum);
3092 goto fail_ctrl;
James Smarte3994412016-12-02 00:28:42 -08003093 }
3094
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003095 nvme_get_ctrl(&ctrl->ctrl);
James Smart2cb657b2017-05-15 17:10:22 -07003096
James Smart4c984152018-06-13 14:07:37 -07003097 if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
3098 nvme_put_ctrl(&ctrl->ctrl);
3099 dev_err(ctrl->ctrl.device,
3100 "NVME-FC{%d}: failed to schedule initial connect\n",
3101 ctrl->cnum);
3102 goto fail_ctrl;
3103 }
3104
3105 flush_delayed_work(&ctrl->connect_work);
3106
James Smart61bff8e2017-04-23 08:30:08 -07003107 dev_info(ctrl->ctrl.device,
3108 "NVME-FC{%d}: new ctrl: NQN \"%s\"\n",
3109 ctrl->cnum, ctrl->ctrl.opts->subsysnqn);
3110
James Smarte3994412016-12-02 00:28:42 -08003111 return &ctrl->ctrl;
3112
James Smart4c984152018-06-13 14:07:37 -07003113fail_ctrl:
3114 nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING);
3115 cancel_work_sync(&ctrl->ctrl.reset_work);
3116 cancel_delayed_work_sync(&ctrl->connect_work);
3117
3118 ctrl->ctrl.opts = NULL;
3119
3120 /* initiate nvme ctrl ref counting teardown */
3121 nvme_uninit_ctrl(&ctrl->ctrl);
3122
3123 /* Remove core ctrl ref. */
3124 nvme_put_ctrl(&ctrl->ctrl);
3125
3126 /* as we're past the point where we transition to the ref
3127 * counting teardown path, if we return a bad pointer here,
3128 * the calling routine, thinking it's prior to the
3129 * transition, will do an rport put. Since the teardown
3130 * path also does a rport put, we do an extra get here to
3131 * so proper order/teardown happens.
3132 */
3133 nvme_fc_rport_get(rport);
3134
3135 return ERR_PTR(-EIO);
3136
James Smart61bff8e2017-04-23 08:30:08 -07003137out_cleanup_admin_q:
3138 blk_cleanup_queue(ctrl->ctrl.admin_q);
3139out_free_admin_tag_set:
3140 blk_mq_free_tag_set(&ctrl->admin_tag_set);
3141out_free_queues:
3142 kfree(ctrl->queues);
James Smarte3994412016-12-02 00:28:42 -08003143out_free_ida:
James Smart61bff8e2017-04-23 08:30:08 -07003144 put_device(ctrl->dev);
James Smarte3994412016-12-02 00:28:42 -08003145 ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
3146out_free_ctrl:
3147 kfree(ctrl);
3148out_fail:
James Smarte3994412016-12-02 00:28:42 -08003149 /* exit via here doesn't follow ctlr ref points */
3150 return ERR_PTR(ret);
3151}
3152
James Smarte3994412016-12-02 00:28:42 -08003153
3154struct nvmet_fc_traddr {
3155 u64 nn;
3156 u64 pn;
3157};
3158
James Smarte3994412016-12-02 00:28:42 -08003159static int
James Smart9c5358e2017-07-17 13:59:39 -07003160__nvme_fc_parse_u64(substring_t *sstr, u64 *val)
James Smarte3994412016-12-02 00:28:42 -08003161{
James Smarte3994412016-12-02 00:28:42 -08003162 u64 token64;
3163
James Smart9c5358e2017-07-17 13:59:39 -07003164 if (match_u64(sstr, &token64))
3165 return -EINVAL;
3166 *val = token64;
James Smarte3994412016-12-02 00:28:42 -08003167
James Smart9c5358e2017-07-17 13:59:39 -07003168 return 0;
3169}
James Smarte3994412016-12-02 00:28:42 -08003170
James Smart9c5358e2017-07-17 13:59:39 -07003171/*
3172 * This routine validates and extracts the WWN's from the TRADDR string.
3173 * As kernel parsers need the 0x to determine number base, universally
3174 * build string to parse with 0x prefix before parsing name strings.
3175 */
3176static int
3177nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen)
3178{
3179 char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1];
3180 substring_t wwn = { name, &name[sizeof(name)-1] };
3181 int nnoffset, pnoffset;
James Smarte3994412016-12-02 00:28:42 -08003182
James Smart9c5358e2017-07-17 13:59:39 -07003183 /* validate it string one of the 2 allowed formats */
3184 if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH &&
3185 !strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) &&
3186 !strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET],
3187 "pn-0x", NVME_FC_TRADDR_OXNNLEN)) {
3188 nnoffset = NVME_FC_TRADDR_OXNNLEN;
3189 pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET +
3190 NVME_FC_TRADDR_OXNNLEN;
3191 } else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH &&
3192 !strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) &&
3193 !strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET],
3194 "pn-", NVME_FC_TRADDR_NNLEN))) {
3195 nnoffset = NVME_FC_TRADDR_NNLEN;
3196 pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN;
3197 } else
3198 goto out_einval;
3199
3200 name[0] = '0';
3201 name[1] = 'x';
3202 name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0;
3203
3204 memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3205 if (__nvme_fc_parse_u64(&wwn, &traddr->nn))
3206 goto out_einval;
3207
3208 memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3209 if (__nvme_fc_parse_u64(&wwn, &traddr->pn))
3210 goto out_einval;
3211
3212 return 0;
3213
3214out_einval:
3215 pr_warn("%s: bad traddr string\n", __func__);
3216 return -EINVAL;
James Smarte3994412016-12-02 00:28:42 -08003217}
3218
3219static struct nvme_ctrl *
3220nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
3221{
3222 struct nvme_fc_lport *lport;
3223 struct nvme_fc_rport *rport;
James Smart61bff8e2017-04-23 08:30:08 -07003224 struct nvme_ctrl *ctrl;
James Smarte3994412016-12-02 00:28:42 -08003225 struct nvmet_fc_traddr laddr = { 0L, 0L };
3226 struct nvmet_fc_traddr raddr = { 0L, 0L };
3227 unsigned long flags;
3228 int ret;
3229
James Smart9c5358e2017-07-17 13:59:39 -07003230 ret = nvme_fc_parse_traddr(&raddr, opts->traddr, NVMF_TRADDR_SIZE);
James Smarte3994412016-12-02 00:28:42 -08003231 if (ret || !raddr.nn || !raddr.pn)
3232 return ERR_PTR(-EINVAL);
3233
James Smart9c5358e2017-07-17 13:59:39 -07003234 ret = nvme_fc_parse_traddr(&laddr, opts->host_traddr, NVMF_TRADDR_SIZE);
James Smarte3994412016-12-02 00:28:42 -08003235 if (ret || !laddr.nn || !laddr.pn)
3236 return ERR_PTR(-EINVAL);
3237
3238 /* find the host and remote ports to connect together */
3239 spin_lock_irqsave(&nvme_fc_lock, flags);
3240 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3241 if (lport->localport.node_name != laddr.nn ||
3242 lport->localport.port_name != laddr.pn)
3243 continue;
3244
3245 list_for_each_entry(rport, &lport->endp_list, endp_list) {
3246 if (rport->remoteport.node_name != raddr.nn ||
3247 rport->remoteport.port_name != raddr.pn)
3248 continue;
3249
3250 /* if fail to get reference fall through. Will error */
3251 if (!nvme_fc_rport_get(rport))
3252 break;
3253
3254 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3255
James Smart61bff8e2017-04-23 08:30:08 -07003256 ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport);
3257 if (IS_ERR(ctrl))
3258 nvme_fc_rport_put(rport);
3259 return ctrl;
James Smarte3994412016-12-02 00:28:42 -08003260 }
3261 }
3262 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3263
Johannes Thumshirn4fb135a2018-04-19 19:43:42 +02003264 pr_warn("%s: %s - %s combination not found\n",
3265 __func__, opts->traddr, opts->host_traddr);
James Smarte3994412016-12-02 00:28:42 -08003266 return ERR_PTR(-ENOENT);
3267}
3268
3269
3270static struct nvmf_transport_ops nvme_fc_transport = {
3271 .name = "fc",
Roy Shterman0de5cd32017-12-25 14:18:30 +02003272 .module = THIS_MODULE,
James Smarte3994412016-12-02 00:28:42 -08003273 .required_opts = NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
James Smart5bbecdb2017-05-15 17:10:16 -07003274 .allowed_opts = NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO,
James Smarte3994412016-12-02 00:28:42 -08003275 .create_ctrl = nvme_fc_create_ctrl,
3276};
3277
3278static int __init nvme_fc_init_module(void)
3279{
James Smart5f568552017-09-14 10:38:41 -07003280 int ret;
3281
3282 /*
3283 * NOTE:
3284 * It is expected that in the future the kernel will combine
3285 * the FC-isms that are currently under scsi and now being
3286 * added to by NVME into a new standalone FC class. The SCSI
3287 * and NVME protocols and their devices would be under this
3288 * new FC class.
3289 *
3290 * As we need something to post FC-specific udev events to,
3291 * specifically for nvme probe events, start by creating the
3292 * new device class. When the new standalone FC class is
3293 * put in place, this code will move to a more generic
3294 * location for the class.
3295 */
3296 fc_class = class_create(THIS_MODULE, "fc");
3297 if (IS_ERR(fc_class)) {
3298 pr_err("couldn't register class fc\n");
3299 return PTR_ERR(fc_class);
3300 }
3301
3302 /*
3303 * Create a device for the FC-centric udev events
3304 */
3305 fc_udev_device = device_create(fc_class, NULL, MKDEV(0, 0), NULL,
3306 "fc_udev_device");
3307 if (IS_ERR(fc_udev_device)) {
3308 pr_err("couldn't create fc_udev device!\n");
3309 ret = PTR_ERR(fc_udev_device);
3310 goto out_destroy_class;
3311 }
3312
3313 ret = nvmf_register_transport(&nvme_fc_transport);
3314 if (ret)
3315 goto out_destroy_device;
3316
3317 return 0;
3318
3319out_destroy_device:
3320 device_destroy(fc_class, MKDEV(0, 0));
3321out_destroy_class:
3322 class_destroy(fc_class);
3323 return ret;
James Smarte3994412016-12-02 00:28:42 -08003324}
3325
3326static void __exit nvme_fc_exit_module(void)
3327{
3328 /* sanity check - all lports should be removed */
3329 if (!list_empty(&nvme_fc_lport_list))
3330 pr_warn("%s: localport list not empty\n", __func__);
3331
3332 nvmf_unregister_transport(&nvme_fc_transport);
3333
James Smarte3994412016-12-02 00:28:42 -08003334 ida_destroy(&nvme_fc_local_port_cnt);
3335 ida_destroy(&nvme_fc_ctrl_cnt);
James Smart5f568552017-09-14 10:38:41 -07003336
3337 device_destroy(fc_class, MKDEV(0, 0));
3338 class_destroy(fc_class);
James Smarte3994412016-12-02 00:28:42 -08003339}
3340
3341module_init(nvme_fc_init_module);
3342module_exit(nvme_fc_exit_module);
3343
3344MODULE_LICENSE("GPL v2");