blob: b64cc100245205e6b44701dda926687a62710469 [file] [log] [blame]
James Smarte3994412016-12-02 00:28:42 -08001/*
2 * Copyright (c) 2016 Avago Technologies. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful.
9 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
10 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
11 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO
12 * THE EXTENT THAT SUCH DISCLAIMERS ARE HELD TO BE LEGALLY INVALID.
13 * See the GNU General Public License for more details, a copy of which
14 * can be found in the file COPYING included with this package
15 *
16 */
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18#include <linux/module.h>
19#include <linux/parser.h>
20#include <uapi/scsi/fc/fc_fs.h>
21#include <uapi/scsi/fc/fc_els.h>
James Smart61bff8e2017-04-23 08:30:08 -070022#include <linux/delay.h>
James Smarte3994412016-12-02 00:28:42 -080023
24#include "nvme.h"
25#include "fabrics.h"
26#include <linux/nvme-fc-driver.h>
27#include <linux/nvme-fc.h>
28
29
30/* *************************** Data Structures/Defines ****************** */
31
32
33/*
34 * We handle AEN commands ourselves and don't even let the
35 * block layer know about them.
36 */
37#define NVME_FC_NR_AEN_COMMANDS 1
38#define NVME_FC_AQ_BLKMQ_DEPTH \
Sagi Grimberg7aa1f422017-06-18 16:15:59 +030039 (NVME_AQ_DEPTH - NVME_FC_NR_AEN_COMMANDS)
James Smarte3994412016-12-02 00:28:42 -080040#define AEN_CMDID_BASE (NVME_FC_AQ_BLKMQ_DEPTH + 1)
41
42enum nvme_fc_queue_flags {
43 NVME_FC_Q_CONNECTED = (1 << 0),
44};
45
46#define NVMEFC_QUEUE_DELAY 3 /* ms units */
47
James Smartac7fe822017-10-25 16:43:15 -070048#define NVME_FC_DEFAULT_DEV_LOSS_TMO 60 /* seconds */
49
James Smarte3994412016-12-02 00:28:42 -080050struct nvme_fc_queue {
51 struct nvme_fc_ctrl *ctrl;
52 struct device *dev;
53 struct blk_mq_hw_ctx *hctx;
54 void *lldd_handle;
55 int queue_size;
56 size_t cmnd_capsule_len;
57 u32 qnum;
58 u32 rqcnt;
59 u32 seqno;
60
61 u64 connection_id;
62 atomic_t csn;
63
64 unsigned long flags;
65} __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
66
James Smart8d64daf2017-04-11 11:35:09 -070067enum nvme_fcop_flags {
68 FCOP_FLAGS_TERMIO = (1 << 0),
69 FCOP_FLAGS_RELEASED = (1 << 1),
70 FCOP_FLAGS_COMPLETE = (1 << 2),
James Smart78a7ac22017-04-23 08:30:07 -070071 FCOP_FLAGS_AEN = (1 << 3),
James Smart8d64daf2017-04-11 11:35:09 -070072};
73
James Smarte3994412016-12-02 00:28:42 -080074struct nvmefc_ls_req_op {
75 struct nvmefc_ls_req ls_req;
76
James Smartc913a8b2017-04-11 11:35:08 -070077 struct nvme_fc_rport *rport;
James Smarte3994412016-12-02 00:28:42 -080078 struct nvme_fc_queue *queue;
79 struct request *rq;
James Smart8d64daf2017-04-11 11:35:09 -070080 u32 flags;
James Smarte3994412016-12-02 00:28:42 -080081
82 int ls_error;
83 struct completion ls_done;
James Smartc913a8b2017-04-11 11:35:08 -070084 struct list_head lsreq_list; /* rport->ls_req_list */
James Smarte3994412016-12-02 00:28:42 -080085 bool req_queued;
86};
87
88enum nvme_fcpop_state {
89 FCPOP_STATE_UNINIT = 0,
90 FCPOP_STATE_IDLE = 1,
91 FCPOP_STATE_ACTIVE = 2,
92 FCPOP_STATE_ABORTED = 3,
James Smart78a7ac22017-04-23 08:30:07 -070093 FCPOP_STATE_COMPLETE = 4,
James Smarte3994412016-12-02 00:28:42 -080094};
95
96struct nvme_fc_fcp_op {
97 struct nvme_request nreq; /*
98 * nvme/host/core.c
99 * requires this to be
100 * the 1st element in the
101 * private structure
102 * associated with the
103 * request.
104 */
105 struct nvmefc_fcp_req fcp_req;
106
107 struct nvme_fc_ctrl *ctrl;
108 struct nvme_fc_queue *queue;
109 struct request *rq;
110
111 atomic_t state;
James Smart78a7ac22017-04-23 08:30:07 -0700112 u32 flags;
James Smarte3994412016-12-02 00:28:42 -0800113 u32 rqno;
114 u32 nents;
115
116 struct nvme_fc_cmd_iu cmd_iu;
117 struct nvme_fc_ersp_iu rsp_iu;
118};
119
120struct nvme_fc_lport {
121 struct nvme_fc_local_port localport;
122
123 struct ida endp_cnt;
124 struct list_head port_list; /* nvme_fc_port_list */
125 struct list_head endp_list;
126 struct device *dev; /* physical device for dma */
127 struct nvme_fc_port_template *ops;
128 struct kref ref;
129} __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
130
131struct nvme_fc_rport {
132 struct nvme_fc_remote_port remoteport;
133
134 struct list_head endp_list; /* for lport->endp_list */
135 struct list_head ctrl_list;
James Smartc913a8b2017-04-11 11:35:08 -0700136 struct list_head ls_req_list;
137 struct device *dev; /* physical device for dma */
138 struct nvme_fc_lport *lport;
James Smarte3994412016-12-02 00:28:42 -0800139 spinlock_t lock;
140 struct kref ref;
141} __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
142
James Smart61bff8e2017-04-23 08:30:08 -0700143enum nvme_fcctrl_flags {
144 FCCTRL_TERMIO = (1 << 0),
James Smarte3994412016-12-02 00:28:42 -0800145};
146
147struct nvme_fc_ctrl {
148 spinlock_t lock;
149 struct nvme_fc_queue *queues;
James Smarte3994412016-12-02 00:28:42 -0800150 struct device *dev;
151 struct nvme_fc_lport *lport;
152 struct nvme_fc_rport *rport;
153 u32 cnum;
154
155 u64 association_id;
156
James Smarte3994412016-12-02 00:28:42 -0800157 struct list_head ctrl_list; /* rport->ctrl_list */
James Smarte3994412016-12-02 00:28:42 -0800158
159 struct blk_mq_tag_set admin_tag_set;
160 struct blk_mq_tag_set tag_set;
161
James Smart61bff8e2017-04-23 08:30:08 -0700162 struct delayed_work connect_work;
James Smart61bff8e2017-04-23 08:30:08 -0700163
James Smarte3994412016-12-02 00:28:42 -0800164 struct kref ref;
James Smart61bff8e2017-04-23 08:30:08 -0700165 u32 flags;
166 u32 iocnt;
James Smart36715cf2017-05-22 15:28:42 -0700167 wait_queue_head_t ioabort_wait;
James Smarte3994412016-12-02 00:28:42 -0800168
169 struct nvme_fc_fcp_op aen_ops[NVME_FC_NR_AEN_COMMANDS];
170
171 struct nvme_ctrl ctrl;
172};
173
174static inline struct nvme_fc_ctrl *
175to_fc_ctrl(struct nvme_ctrl *ctrl)
176{
177 return container_of(ctrl, struct nvme_fc_ctrl, ctrl);
178}
179
180static inline struct nvme_fc_lport *
181localport_to_lport(struct nvme_fc_local_port *portptr)
182{
183 return container_of(portptr, struct nvme_fc_lport, localport);
184}
185
186static inline struct nvme_fc_rport *
187remoteport_to_rport(struct nvme_fc_remote_port *portptr)
188{
189 return container_of(portptr, struct nvme_fc_rport, remoteport);
190}
191
192static inline struct nvmefc_ls_req_op *
193ls_req_to_lsop(struct nvmefc_ls_req *lsreq)
194{
195 return container_of(lsreq, struct nvmefc_ls_req_op, ls_req);
196}
197
198static inline struct nvme_fc_fcp_op *
199fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq)
200{
201 return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req);
202}
203
204
205
206/* *************************** Globals **************************** */
207
208
209static DEFINE_SPINLOCK(nvme_fc_lock);
210
211static LIST_HEAD(nvme_fc_lport_list);
212static DEFINE_IDA(nvme_fc_local_port_cnt);
213static DEFINE_IDA(nvme_fc_ctrl_cnt);
214
James Smarte3994412016-12-02 00:28:42 -0800215
216
James Smart5f568552017-09-14 10:38:41 -0700217/*
218 * These items are short-term. They will eventually be moved into
219 * a generic FC class. See comments in module init.
220 */
221static struct class *fc_class;
222static struct device *fc_udev_device;
223
James Smarte3994412016-12-02 00:28:42 -0800224
225/* *********************** FC-NVME Port Management ************************ */
226
James Smarte3994412016-12-02 00:28:42 -0800227static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *,
228 struct nvme_fc_queue *, unsigned int);
229
James Smart5533d422017-07-31 13:20:30 -0700230static void
231nvme_fc_free_lport(struct kref *ref)
232{
233 struct nvme_fc_lport *lport =
234 container_of(ref, struct nvme_fc_lport, ref);
235 unsigned long flags;
236
237 WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED);
238 WARN_ON(!list_empty(&lport->endp_list));
239
240 /* remove from transport list */
241 spin_lock_irqsave(&nvme_fc_lock, flags);
242 list_del(&lport->port_list);
243 spin_unlock_irqrestore(&nvme_fc_lock, flags);
244
245 /* let the LLDD know we've finished tearing it down */
246 lport->ops->localport_delete(&lport->localport);
247
248 ida_simple_remove(&nvme_fc_local_port_cnt, lport->localport.port_num);
249 ida_destroy(&lport->endp_cnt);
250
251 put_device(lport->dev);
252
253 kfree(lport);
254}
255
256static void
257nvme_fc_lport_put(struct nvme_fc_lport *lport)
258{
259 kref_put(&lport->ref, nvme_fc_free_lport);
260}
261
262static int
263nvme_fc_lport_get(struct nvme_fc_lport *lport)
264{
265 return kref_get_unless_zero(&lport->ref);
266}
267
268
269static struct nvme_fc_lport *
270nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info *pinfo)
271{
272 struct nvme_fc_lport *lport;
273 unsigned long flags;
274
275 spin_lock_irqsave(&nvme_fc_lock, flags);
276
277 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
278 if (lport->localport.node_name != pinfo->node_name ||
279 lport->localport.port_name != pinfo->port_name)
280 continue;
281
282 if (lport->localport.port_state != FC_OBJSTATE_DELETED) {
283 lport = ERR_PTR(-EEXIST);
284 goto out_done;
285 }
286
287 if (!nvme_fc_lport_get(lport)) {
288 /*
289 * fails if ref cnt already 0. If so,
290 * act as if lport already deleted
291 */
292 lport = NULL;
293 goto out_done;
294 }
295
296 /* resume the lport */
297
298 lport->localport.port_role = pinfo->port_role;
299 lport->localport.port_id = pinfo->port_id;
300 lport->localport.port_state = FC_OBJSTATE_ONLINE;
301
302 spin_unlock_irqrestore(&nvme_fc_lock, flags);
303
304 return lport;
305 }
306
307 lport = NULL;
308
309out_done:
310 spin_unlock_irqrestore(&nvme_fc_lock, flags);
311
312 return lport;
313}
James Smarte3994412016-12-02 00:28:42 -0800314
315/**
316 * nvme_fc_register_localport - transport entry point called by an
317 * LLDD to register the existence of a NVME
318 * host FC port.
319 * @pinfo: pointer to information about the port to be registered
320 * @template: LLDD entrypoints and operational parameters for the port
321 * @dev: physical hardware device node port corresponds to. Will be
322 * used for DMA mappings
323 * @lport_p: pointer to a local port pointer. Upon success, the routine
324 * will allocate a nvme_fc_local_port structure and place its
325 * address in the local port pointer. Upon failure, local port
326 * pointer will be set to 0.
327 *
328 * Returns:
329 * a completion status. Must be 0 upon success; a negative errno
330 * (ex: -ENXIO) upon failure.
331 */
332int
333nvme_fc_register_localport(struct nvme_fc_port_info *pinfo,
334 struct nvme_fc_port_template *template,
335 struct device *dev,
336 struct nvme_fc_local_port **portptr)
337{
338 struct nvme_fc_lport *newrec;
339 unsigned long flags;
340 int ret, idx;
341
342 if (!template->localport_delete || !template->remoteport_delete ||
343 !template->ls_req || !template->fcp_io ||
344 !template->ls_abort || !template->fcp_abort ||
345 !template->max_hw_queues || !template->max_sgl_segments ||
346 !template->max_dif_sgl_segments || !template->dma_boundary) {
347 ret = -EINVAL;
348 goto out_reghost_failed;
349 }
350
James Smart5533d422017-07-31 13:20:30 -0700351 /*
352 * look to see if there is already a localport that had been
353 * deregistered and in the process of waiting for all the
354 * references to fully be removed. If the references haven't
355 * expired, we can simply re-enable the localport. Remoteports
356 * and controller reconnections should resume naturally.
357 */
358 newrec = nvme_fc_attach_to_unreg_lport(pinfo);
359
360 /* found an lport, but something about its state is bad */
361 if (IS_ERR(newrec)) {
362 ret = PTR_ERR(newrec);
363 goto out_reghost_failed;
364
365 /* found existing lport, which was resumed */
366 } else if (newrec) {
367 *portptr = &newrec->localport;
368 return 0;
369 }
370
371 /* nothing found - allocate a new localport struct */
372
James Smarte3994412016-12-02 00:28:42 -0800373 newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz),
374 GFP_KERNEL);
375 if (!newrec) {
376 ret = -ENOMEM;
377 goto out_reghost_failed;
378 }
379
380 idx = ida_simple_get(&nvme_fc_local_port_cnt, 0, 0, GFP_KERNEL);
381 if (idx < 0) {
382 ret = -ENOSPC;
383 goto out_fail_kfree;
384 }
385
386 if (!get_device(dev) && dev) {
387 ret = -ENODEV;
388 goto out_ida_put;
389 }
390
391 INIT_LIST_HEAD(&newrec->port_list);
392 INIT_LIST_HEAD(&newrec->endp_list);
393 kref_init(&newrec->ref);
394 newrec->ops = template;
395 newrec->dev = dev;
396 ida_init(&newrec->endp_cnt);
397 newrec->localport.private = &newrec[1];
398 newrec->localport.node_name = pinfo->node_name;
399 newrec->localport.port_name = pinfo->port_name;
400 newrec->localport.port_role = pinfo->port_role;
401 newrec->localport.port_id = pinfo->port_id;
402 newrec->localport.port_state = FC_OBJSTATE_ONLINE;
403 newrec->localport.port_num = idx;
404
405 spin_lock_irqsave(&nvme_fc_lock, flags);
406 list_add_tail(&newrec->port_list, &nvme_fc_lport_list);
407 spin_unlock_irqrestore(&nvme_fc_lock, flags);
408
409 if (dev)
410 dma_set_seg_boundary(dev, template->dma_boundary);
411
412 *portptr = &newrec->localport;
413 return 0;
414
415out_ida_put:
416 ida_simple_remove(&nvme_fc_local_port_cnt, idx);
417out_fail_kfree:
418 kfree(newrec);
419out_reghost_failed:
420 *portptr = NULL;
421
422 return ret;
423}
424EXPORT_SYMBOL_GPL(nvme_fc_register_localport);
425
James Smarte3994412016-12-02 00:28:42 -0800426/**
427 * nvme_fc_unregister_localport - transport entry point called by an
428 * LLDD to deregister/remove a previously
429 * registered a NVME host FC port.
430 * @localport: pointer to the (registered) local port that is to be
431 * deregistered.
432 *
433 * Returns:
434 * a completion status. Must be 0 upon success; a negative errno
435 * (ex: -ENXIO) upon failure.
436 */
437int
438nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr)
439{
440 struct nvme_fc_lport *lport = localport_to_lport(portptr);
441 unsigned long flags;
442
443 if (!portptr)
444 return -EINVAL;
445
446 spin_lock_irqsave(&nvme_fc_lock, flags);
447
448 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
449 spin_unlock_irqrestore(&nvme_fc_lock, flags);
450 return -EINVAL;
451 }
452 portptr->port_state = FC_OBJSTATE_DELETED;
453
454 spin_unlock_irqrestore(&nvme_fc_lock, flags);
455
456 nvme_fc_lport_put(lport);
457
458 return 0;
459}
460EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport);
461
James Smarteaefd5a2017-09-14 10:38:42 -0700462/*
463 * TRADDR strings, per FC-NVME are fixed format:
464 * "nn-0x<16hexdigits>:pn-0x<16hexdigits>" - 43 characters
465 * udev event will only differ by prefix of what field is
466 * being specified:
467 * "NVMEFC_HOST_TRADDR=" or "NVMEFC_TRADDR=" - 19 max characters
468 * 19 + 43 + null_fudge = 64 characters
469 */
470#define FCNVME_TRADDR_LENGTH 64
471
472static void
473nvme_fc_signal_discovery_scan(struct nvme_fc_lport *lport,
474 struct nvme_fc_rport *rport)
475{
476 char hostaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_HOST_TRADDR=...*/
477 char tgtaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_TRADDR=...*/
478 char *envp[4] = { "FC_EVENT=nvmediscovery", hostaddr, tgtaddr, NULL };
479
480 if (!(rport->remoteport.port_role & FC_PORT_ROLE_NVME_DISCOVERY))
481 return;
482
483 snprintf(hostaddr, sizeof(hostaddr),
484 "NVMEFC_HOST_TRADDR=nn-0x%016llx:pn-0x%016llx",
485 lport->localport.node_name, lport->localport.port_name);
486 snprintf(tgtaddr, sizeof(tgtaddr),
487 "NVMEFC_TRADDR=nn-0x%016llx:pn-0x%016llx",
488 rport->remoteport.node_name, rport->remoteport.port_name);
489 kobject_uevent_env(&fc_udev_device->kobj, KOBJ_CHANGE, envp);
490}
491
James Smart469d0ef2017-09-26 21:50:45 -0700492static void
493nvme_fc_free_rport(struct kref *ref)
494{
495 struct nvme_fc_rport *rport =
496 container_of(ref, struct nvme_fc_rport, ref);
497 struct nvme_fc_lport *lport =
498 localport_to_lport(rport->remoteport.localport);
499 unsigned long flags;
500
501 WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED);
502 WARN_ON(!list_empty(&rport->ctrl_list));
503
504 /* remove from lport list */
505 spin_lock_irqsave(&nvme_fc_lock, flags);
506 list_del(&rport->endp_list);
507 spin_unlock_irqrestore(&nvme_fc_lock, flags);
508
509 /* let the LLDD know we've finished tearing it down */
510 lport->ops->remoteport_delete(&rport->remoteport);
511
512 ida_simple_remove(&lport->endp_cnt, rport->remoteport.port_num);
513
514 kfree(rport);
515
516 nvme_fc_lport_put(lport);
517}
518
519static void
520nvme_fc_rport_put(struct nvme_fc_rport *rport)
521{
522 kref_put(&rport->ref, nvme_fc_free_rport);
523}
524
525static int
526nvme_fc_rport_get(struct nvme_fc_rport *rport)
527{
528 return kref_get_unless_zero(&rport->ref);
529}
530
James Smarte3994412016-12-02 00:28:42 -0800531/**
532 * nvme_fc_register_remoteport - transport entry point called by an
533 * LLDD to register the existence of a NVME
534 * subsystem FC port on its fabric.
535 * @localport: pointer to the (registered) local port that the remote
536 * subsystem port is connected to.
537 * @pinfo: pointer to information about the port to be registered
538 * @rport_p: pointer to a remote port pointer. Upon success, the routine
539 * will allocate a nvme_fc_remote_port structure and place its
540 * address in the remote port pointer. Upon failure, remote port
541 * pointer will be set to 0.
542 *
543 * Returns:
544 * a completion status. Must be 0 upon success; a negative errno
545 * (ex: -ENXIO) upon failure.
546 */
547int
548nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
549 struct nvme_fc_port_info *pinfo,
550 struct nvme_fc_remote_port **portptr)
551{
552 struct nvme_fc_lport *lport = localport_to_lport(localport);
553 struct nvme_fc_rport *newrec;
554 unsigned long flags;
555 int ret, idx;
556
557 newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz),
558 GFP_KERNEL);
559 if (!newrec) {
560 ret = -ENOMEM;
561 goto out_reghost_failed;
562 }
563
564 if (!nvme_fc_lport_get(lport)) {
565 ret = -ESHUTDOWN;
566 goto out_kfree_rport;
567 }
568
569 idx = ida_simple_get(&lport->endp_cnt, 0, 0, GFP_KERNEL);
570 if (idx < 0) {
571 ret = -ENOSPC;
572 goto out_lport_put;
573 }
574
575 INIT_LIST_HEAD(&newrec->endp_list);
576 INIT_LIST_HEAD(&newrec->ctrl_list);
James Smartc913a8b2017-04-11 11:35:08 -0700577 INIT_LIST_HEAD(&newrec->ls_req_list);
James Smarte3994412016-12-02 00:28:42 -0800578 kref_init(&newrec->ref);
579 spin_lock_init(&newrec->lock);
580 newrec->remoteport.localport = &lport->localport;
James Smartc913a8b2017-04-11 11:35:08 -0700581 newrec->dev = lport->dev;
582 newrec->lport = lport;
James Smarte3994412016-12-02 00:28:42 -0800583 newrec->remoteport.private = &newrec[1];
584 newrec->remoteport.port_role = pinfo->port_role;
585 newrec->remoteport.node_name = pinfo->node_name;
586 newrec->remoteport.port_name = pinfo->port_name;
587 newrec->remoteport.port_id = pinfo->port_id;
588 newrec->remoteport.port_state = FC_OBJSTATE_ONLINE;
589 newrec->remoteport.port_num = idx;
James Smartac7fe822017-10-25 16:43:15 -0700590 /* a registration value of dev_loss_tmo=0 results in the default */
591 if (pinfo->dev_loss_tmo)
592 newrec->remoteport.dev_loss_tmo = pinfo->dev_loss_tmo;
593 else
594 newrec->remoteport.dev_loss_tmo = NVME_FC_DEFAULT_DEV_LOSS_TMO;
James Smarte3994412016-12-02 00:28:42 -0800595
596 spin_lock_irqsave(&nvme_fc_lock, flags);
597 list_add_tail(&newrec->endp_list, &lport->endp_list);
598 spin_unlock_irqrestore(&nvme_fc_lock, flags);
599
James Smarteaefd5a2017-09-14 10:38:42 -0700600 nvme_fc_signal_discovery_scan(lport, newrec);
601
James Smarte3994412016-12-02 00:28:42 -0800602 *portptr = &newrec->remoteport;
603 return 0;
604
605out_lport_put:
606 nvme_fc_lport_put(lport);
607out_kfree_rport:
608 kfree(newrec);
609out_reghost_failed:
610 *portptr = NULL;
611 return ret;
James Smarte3994412016-12-02 00:28:42 -0800612}
613EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport);
614
James Smart8d64daf2017-04-11 11:35:09 -0700615static int
616nvme_fc_abort_lsops(struct nvme_fc_rport *rport)
617{
618 struct nvmefc_ls_req_op *lsop;
619 unsigned long flags;
620
621restart:
622 spin_lock_irqsave(&rport->lock, flags);
623
624 list_for_each_entry(lsop, &rport->ls_req_list, lsreq_list) {
625 if (!(lsop->flags & FCOP_FLAGS_TERMIO)) {
626 lsop->flags |= FCOP_FLAGS_TERMIO;
627 spin_unlock_irqrestore(&rport->lock, flags);
628 rport->lport->ops->ls_abort(&rport->lport->localport,
629 &rport->remoteport,
630 &lsop->ls_req);
631 goto restart;
632 }
633 }
634 spin_unlock_irqrestore(&rport->lock, flags);
635
636 return 0;
637}
638
James Smarte3994412016-12-02 00:28:42 -0800639/**
640 * nvme_fc_unregister_remoteport - transport entry point called by an
641 * LLDD to deregister/remove a previously
642 * registered a NVME subsystem FC port.
643 * @remoteport: pointer to the (registered) remote port that is to be
644 * deregistered.
645 *
646 * Returns:
647 * a completion status. Must be 0 upon success; a negative errno
648 * (ex: -ENXIO) upon failure.
649 */
650int
651nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr)
652{
653 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
654 struct nvme_fc_ctrl *ctrl;
655 unsigned long flags;
656
657 if (!portptr)
658 return -EINVAL;
659
660 spin_lock_irqsave(&rport->lock, flags);
661
662 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
663 spin_unlock_irqrestore(&rport->lock, flags);
664 return -EINVAL;
665 }
666 portptr->port_state = FC_OBJSTATE_DELETED;
667
668 /* tear down all associations to the remote port */
669 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list)
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200670 nvme_delete_ctrl(&ctrl->ctrl);
James Smarte3994412016-12-02 00:28:42 -0800671
672 spin_unlock_irqrestore(&rport->lock, flags);
673
James Smart8d64daf2017-04-11 11:35:09 -0700674 nvme_fc_abort_lsops(rport);
675
James Smarte3994412016-12-02 00:28:42 -0800676 nvme_fc_rport_put(rport);
677 return 0;
678}
679EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport);
680
James Smarteaefd5a2017-09-14 10:38:42 -0700681/**
682 * nvme_fc_rescan_remoteport - transport entry point called by an
683 * LLDD to request a nvme device rescan.
684 * @remoteport: pointer to the (registered) remote port that is to be
685 * rescanned.
686 *
687 * Returns: N/A
688 */
689void
690nvme_fc_rescan_remoteport(struct nvme_fc_remote_port *remoteport)
691{
692 struct nvme_fc_rport *rport = remoteport_to_rport(remoteport);
693
694 nvme_fc_signal_discovery_scan(rport->lport, rport);
695}
696EXPORT_SYMBOL_GPL(nvme_fc_rescan_remoteport);
697
James Smartac7fe822017-10-25 16:43:15 -0700698int
699nvme_fc_set_remoteport_devloss(struct nvme_fc_remote_port *portptr,
700 u32 dev_loss_tmo)
701{
702 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
703 struct nvme_fc_ctrl *ctrl;
704 unsigned long flags;
705
706 spin_lock_irqsave(&rport->lock, flags);
707
708 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
709 spin_unlock_irqrestore(&rport->lock, flags);
710 return -EINVAL;
711 }
712
713 /* a dev_loss_tmo of 0 (immediate) is allowed to be set */
714 rport->remoteport.dev_loss_tmo = dev_loss_tmo;
715
716 spin_unlock_irqrestore(&rport->lock, flags);
717
718 return 0;
719}
720EXPORT_SYMBOL_GPL(nvme_fc_set_remoteport_devloss);
721
James Smarte3994412016-12-02 00:28:42 -0800722
723/* *********************** FC-NVME DMA Handling **************************** */
724
725/*
726 * The fcloop device passes in a NULL device pointer. Real LLD's will
727 * pass in a valid device pointer. If NULL is passed to the dma mapping
728 * routines, depending on the platform, it may or may not succeed, and
729 * may crash.
730 *
731 * As such:
732 * Wrapper all the dma routines and check the dev pointer.
733 *
734 * If simple mappings (return just a dma address, we'll noop them,
735 * returning a dma address of 0.
736 *
737 * On more complex mappings (dma_map_sg), a pseudo routine fills
738 * in the scatter list, setting all dma addresses to 0.
739 */
740
741static inline dma_addr_t
742fc_dma_map_single(struct device *dev, void *ptr, size_t size,
743 enum dma_data_direction dir)
744{
745 return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
746}
747
748static inline int
749fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
750{
751 return dev ? dma_mapping_error(dev, dma_addr) : 0;
752}
753
754static inline void
755fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
756 enum dma_data_direction dir)
757{
758 if (dev)
759 dma_unmap_single(dev, addr, size, dir);
760}
761
762static inline void
763fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
764 enum dma_data_direction dir)
765{
766 if (dev)
767 dma_sync_single_for_cpu(dev, addr, size, dir);
768}
769
770static inline void
771fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
772 enum dma_data_direction dir)
773{
774 if (dev)
775 dma_sync_single_for_device(dev, addr, size, dir);
776}
777
778/* pseudo dma_map_sg call */
779static int
780fc_map_sg(struct scatterlist *sg, int nents)
781{
782 struct scatterlist *s;
783 int i;
784
785 WARN_ON(nents == 0 || sg[0].length == 0);
786
787 for_each_sg(sg, s, nents, i) {
788 s->dma_address = 0L;
789#ifdef CONFIG_NEED_SG_DMA_LENGTH
790 s->dma_length = s->length;
791#endif
792 }
793 return nents;
794}
795
796static inline int
797fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
798 enum dma_data_direction dir)
799{
800 return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
801}
802
803static inline void
804fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
805 enum dma_data_direction dir)
806{
807 if (dev)
808 dma_unmap_sg(dev, sg, nents, dir);
809}
810
James Smarte3994412016-12-02 00:28:42 -0800811/* *********************** FC-NVME LS Handling **************************** */
812
813static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
814static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
815
816
817static void
James Smartc913a8b2017-04-11 11:35:08 -0700818__nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop)
James Smarte3994412016-12-02 00:28:42 -0800819{
James Smartc913a8b2017-04-11 11:35:08 -0700820 struct nvme_fc_rport *rport = lsop->rport;
James Smarte3994412016-12-02 00:28:42 -0800821 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
822 unsigned long flags;
823
James Smartc913a8b2017-04-11 11:35:08 -0700824 spin_lock_irqsave(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -0800825
826 if (!lsop->req_queued) {
James Smartc913a8b2017-04-11 11:35:08 -0700827 spin_unlock_irqrestore(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -0800828 return;
829 }
830
831 list_del(&lsop->lsreq_list);
832
833 lsop->req_queued = false;
834
James Smartc913a8b2017-04-11 11:35:08 -0700835 spin_unlock_irqrestore(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -0800836
James Smartc913a8b2017-04-11 11:35:08 -0700837 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
James Smarte3994412016-12-02 00:28:42 -0800838 (lsreq->rqstlen + lsreq->rsplen),
839 DMA_BIDIRECTIONAL);
840
James Smartc913a8b2017-04-11 11:35:08 -0700841 nvme_fc_rport_put(rport);
James Smarte3994412016-12-02 00:28:42 -0800842}
843
844static int
James Smartc913a8b2017-04-11 11:35:08 -0700845__nvme_fc_send_ls_req(struct nvme_fc_rport *rport,
James Smarte3994412016-12-02 00:28:42 -0800846 struct nvmefc_ls_req_op *lsop,
847 void (*done)(struct nvmefc_ls_req *req, int status))
848{
849 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
850 unsigned long flags;
James Smartc913a8b2017-04-11 11:35:08 -0700851 int ret = 0;
James Smarte3994412016-12-02 00:28:42 -0800852
James Smartc913a8b2017-04-11 11:35:08 -0700853 if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
854 return -ECONNREFUSED;
855
856 if (!nvme_fc_rport_get(rport))
James Smarte3994412016-12-02 00:28:42 -0800857 return -ESHUTDOWN;
858
859 lsreq->done = done;
James Smartc913a8b2017-04-11 11:35:08 -0700860 lsop->rport = rport;
James Smarte3994412016-12-02 00:28:42 -0800861 lsop->req_queued = false;
862 INIT_LIST_HEAD(&lsop->lsreq_list);
863 init_completion(&lsop->ls_done);
864
James Smartc913a8b2017-04-11 11:35:08 -0700865 lsreq->rqstdma = fc_dma_map_single(rport->dev, lsreq->rqstaddr,
James Smarte3994412016-12-02 00:28:42 -0800866 lsreq->rqstlen + lsreq->rsplen,
867 DMA_BIDIRECTIONAL);
James Smartc913a8b2017-04-11 11:35:08 -0700868 if (fc_dma_mapping_error(rport->dev, lsreq->rqstdma)) {
869 ret = -EFAULT;
870 goto out_putrport;
James Smarte3994412016-12-02 00:28:42 -0800871 }
872 lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
873
James Smartc913a8b2017-04-11 11:35:08 -0700874 spin_lock_irqsave(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -0800875
James Smartc913a8b2017-04-11 11:35:08 -0700876 list_add_tail(&lsop->lsreq_list, &rport->ls_req_list);
James Smarte3994412016-12-02 00:28:42 -0800877
878 lsop->req_queued = true;
879
James Smartc913a8b2017-04-11 11:35:08 -0700880 spin_unlock_irqrestore(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -0800881
James Smartc913a8b2017-04-11 11:35:08 -0700882 ret = rport->lport->ops->ls_req(&rport->lport->localport,
883 &rport->remoteport, lsreq);
James Smarte3994412016-12-02 00:28:42 -0800884 if (ret)
James Smartc913a8b2017-04-11 11:35:08 -0700885 goto out_unlink;
886
887 return 0;
888
889out_unlink:
890 lsop->ls_error = ret;
891 spin_lock_irqsave(&rport->lock, flags);
892 lsop->req_queued = false;
893 list_del(&lsop->lsreq_list);
894 spin_unlock_irqrestore(&rport->lock, flags);
895 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
896 (lsreq->rqstlen + lsreq->rsplen),
897 DMA_BIDIRECTIONAL);
898out_putrport:
899 nvme_fc_rport_put(rport);
James Smarte3994412016-12-02 00:28:42 -0800900
901 return ret;
902}
903
904static void
905nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status)
906{
907 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
908
909 lsop->ls_error = status;
910 complete(&lsop->ls_done);
911}
912
913static int
James Smartc913a8b2017-04-11 11:35:08 -0700914nvme_fc_send_ls_req(struct nvme_fc_rport *rport, struct nvmefc_ls_req_op *lsop)
James Smarte3994412016-12-02 00:28:42 -0800915{
916 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
917 struct fcnvme_ls_rjt *rjt = lsreq->rspaddr;
918 int ret;
919
James Smartc913a8b2017-04-11 11:35:08 -0700920 ret = __nvme_fc_send_ls_req(rport, lsop, nvme_fc_send_ls_req_done);
James Smarte3994412016-12-02 00:28:42 -0800921
James Smartc913a8b2017-04-11 11:35:08 -0700922 if (!ret) {
James Smarte3994412016-12-02 00:28:42 -0800923 /*
924 * No timeout/not interruptible as we need the struct
925 * to exist until the lldd calls us back. Thus mandate
926 * wait until driver calls back. lldd responsible for
927 * the timeout action
928 */
929 wait_for_completion(&lsop->ls_done);
930
James Smartc913a8b2017-04-11 11:35:08 -0700931 __nvme_fc_finish_ls_req(lsop);
James Smarte3994412016-12-02 00:28:42 -0800932
James Smartc913a8b2017-04-11 11:35:08 -0700933 ret = lsop->ls_error;
James Smarte3994412016-12-02 00:28:42 -0800934 }
935
James Smartc913a8b2017-04-11 11:35:08 -0700936 if (ret)
937 return ret;
938
James Smarte3994412016-12-02 00:28:42 -0800939 /* ACC or RJT payload ? */
940 if (rjt->w0.ls_cmd == FCNVME_LS_RJT)
941 return -ENXIO;
942
943 return 0;
944}
945
James Smartc913a8b2017-04-11 11:35:08 -0700946static int
947nvme_fc_send_ls_req_async(struct nvme_fc_rport *rport,
James Smarte3994412016-12-02 00:28:42 -0800948 struct nvmefc_ls_req_op *lsop,
949 void (*done)(struct nvmefc_ls_req *req, int status))
950{
James Smarte3994412016-12-02 00:28:42 -0800951 /* don't wait for completion */
952
James Smartc913a8b2017-04-11 11:35:08 -0700953 return __nvme_fc_send_ls_req(rport, lsop, done);
James Smarte3994412016-12-02 00:28:42 -0800954}
955
956/* Validation Error indexes into the string table below */
957enum {
958 VERR_NO_ERROR = 0,
959 VERR_LSACC = 1,
960 VERR_LSDESC_RQST = 2,
961 VERR_LSDESC_RQST_LEN = 3,
962 VERR_ASSOC_ID = 4,
963 VERR_ASSOC_ID_LEN = 5,
964 VERR_CONN_ID = 6,
965 VERR_CONN_ID_LEN = 7,
966 VERR_CR_ASSOC = 8,
967 VERR_CR_ASSOC_ACC_LEN = 9,
968 VERR_CR_CONN = 10,
969 VERR_CR_CONN_ACC_LEN = 11,
970 VERR_DISCONN = 12,
971 VERR_DISCONN_ACC_LEN = 13,
972};
973
974static char *validation_errors[] = {
975 "OK",
976 "Not LS_ACC",
977 "Not LSDESC_RQST",
978 "Bad LSDESC_RQST Length",
979 "Not Association ID",
980 "Bad Association ID Length",
981 "Not Connection ID",
982 "Bad Connection ID Length",
983 "Not CR_ASSOC Rqst",
984 "Bad CR_ASSOC ACC Length",
985 "Not CR_CONN Rqst",
986 "Bad CR_CONN ACC Length",
987 "Not Disconnect Rqst",
988 "Bad Disconnect ACC Length",
989};
990
991static int
992nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl,
993 struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio)
994{
995 struct nvmefc_ls_req_op *lsop;
996 struct nvmefc_ls_req *lsreq;
997 struct fcnvme_ls_cr_assoc_rqst *assoc_rqst;
998 struct fcnvme_ls_cr_assoc_acc *assoc_acc;
999 int ret, fcret = 0;
1000
1001 lsop = kzalloc((sizeof(*lsop) +
1002 ctrl->lport->ops->lsrqst_priv_sz +
1003 sizeof(*assoc_rqst) + sizeof(*assoc_acc)), GFP_KERNEL);
1004 if (!lsop) {
1005 ret = -ENOMEM;
1006 goto out_no_memory;
1007 }
1008 lsreq = &lsop->ls_req;
1009
1010 lsreq->private = (void *)&lsop[1];
1011 assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)
1012 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1013 assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1];
1014
1015 assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION;
1016 assoc_rqst->desc_list_len =
1017 cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1018
1019 assoc_rqst->assoc_cmd.desc_tag =
1020 cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD);
1021 assoc_rqst->assoc_cmd.desc_len =
1022 fcnvme_lsdesc_len(
1023 sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1024
1025 assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1026 assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize);
1027 /* Linux supports only Dynamic controllers */
1028 assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff);
Christoph Hellwig8e412262017-05-17 09:54:27 +02001029 uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id);
James Smarte3994412016-12-02 00:28:42 -08001030 strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
1031 min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE));
1032 strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
1033 min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE));
1034
1035 lsop->queue = queue;
1036 lsreq->rqstaddr = assoc_rqst;
1037 lsreq->rqstlen = sizeof(*assoc_rqst);
1038 lsreq->rspaddr = assoc_acc;
1039 lsreq->rsplen = sizeof(*assoc_acc);
1040 lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
1041
James Smartc913a8b2017-04-11 11:35:08 -07001042 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
James Smarte3994412016-12-02 00:28:42 -08001043 if (ret)
1044 goto out_free_buffer;
1045
1046 /* process connect LS completion */
1047
1048 /* validate the ACC response */
1049 if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1050 fcret = VERR_LSACC;
James Smartf77fc872017-03-23 20:41:25 -07001051 else if (assoc_acc->hdr.desc_list_len !=
James Smarte3994412016-12-02 00:28:42 -08001052 fcnvme_lsdesc_len(
1053 sizeof(struct fcnvme_ls_cr_assoc_acc)))
1054 fcret = VERR_CR_ASSOC_ACC_LEN;
James Smartf77fc872017-03-23 20:41:25 -07001055 else if (assoc_acc->hdr.rqst.desc_tag !=
1056 cpu_to_be32(FCNVME_LSDESC_RQST))
James Smarte3994412016-12-02 00:28:42 -08001057 fcret = VERR_LSDESC_RQST;
1058 else if (assoc_acc->hdr.rqst.desc_len !=
1059 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1060 fcret = VERR_LSDESC_RQST_LEN;
1061 else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION)
1062 fcret = VERR_CR_ASSOC;
1063 else if (assoc_acc->associd.desc_tag !=
1064 cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
1065 fcret = VERR_ASSOC_ID;
1066 else if (assoc_acc->associd.desc_len !=
1067 fcnvme_lsdesc_len(
1068 sizeof(struct fcnvme_lsdesc_assoc_id)))
1069 fcret = VERR_ASSOC_ID_LEN;
1070 else if (assoc_acc->connectid.desc_tag !=
1071 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1072 fcret = VERR_CONN_ID;
1073 else if (assoc_acc->connectid.desc_len !=
1074 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1075 fcret = VERR_CONN_ID_LEN;
1076
1077 if (fcret) {
1078 ret = -EBADF;
1079 dev_err(ctrl->dev,
1080 "q %d connect failed: %s\n",
1081 queue->qnum, validation_errors[fcret]);
1082 } else {
1083 ctrl->association_id =
1084 be64_to_cpu(assoc_acc->associd.association_id);
1085 queue->connection_id =
1086 be64_to_cpu(assoc_acc->connectid.connection_id);
1087 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1088 }
1089
1090out_free_buffer:
1091 kfree(lsop);
1092out_no_memory:
1093 if (ret)
1094 dev_err(ctrl->dev,
1095 "queue %d connect admin queue failed (%d).\n",
1096 queue->qnum, ret);
1097 return ret;
1098}
1099
1100static int
1101nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
1102 u16 qsize, u16 ersp_ratio)
1103{
1104 struct nvmefc_ls_req_op *lsop;
1105 struct nvmefc_ls_req *lsreq;
1106 struct fcnvme_ls_cr_conn_rqst *conn_rqst;
1107 struct fcnvme_ls_cr_conn_acc *conn_acc;
1108 int ret, fcret = 0;
1109
1110 lsop = kzalloc((sizeof(*lsop) +
1111 ctrl->lport->ops->lsrqst_priv_sz +
1112 sizeof(*conn_rqst) + sizeof(*conn_acc)), GFP_KERNEL);
1113 if (!lsop) {
1114 ret = -ENOMEM;
1115 goto out_no_memory;
1116 }
1117 lsreq = &lsop->ls_req;
1118
1119 lsreq->private = (void *)&lsop[1];
1120 conn_rqst = (struct fcnvme_ls_cr_conn_rqst *)
1121 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1122 conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1];
1123
1124 conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION;
1125 conn_rqst->desc_list_len = cpu_to_be32(
1126 sizeof(struct fcnvme_lsdesc_assoc_id) +
1127 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1128
1129 conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1130 conn_rqst->associd.desc_len =
1131 fcnvme_lsdesc_len(
1132 sizeof(struct fcnvme_lsdesc_assoc_id));
1133 conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1134 conn_rqst->connect_cmd.desc_tag =
1135 cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD);
1136 conn_rqst->connect_cmd.desc_len =
1137 fcnvme_lsdesc_len(
1138 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1139 conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1140 conn_rqst->connect_cmd.qid = cpu_to_be16(queue->qnum);
1141 conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize);
1142
1143 lsop->queue = queue;
1144 lsreq->rqstaddr = conn_rqst;
1145 lsreq->rqstlen = sizeof(*conn_rqst);
1146 lsreq->rspaddr = conn_acc;
1147 lsreq->rsplen = sizeof(*conn_acc);
1148 lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
1149
James Smartc913a8b2017-04-11 11:35:08 -07001150 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
James Smarte3994412016-12-02 00:28:42 -08001151 if (ret)
1152 goto out_free_buffer;
1153
1154 /* process connect LS completion */
1155
1156 /* validate the ACC response */
1157 if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1158 fcret = VERR_LSACC;
James Smartf77fc872017-03-23 20:41:25 -07001159 else if (conn_acc->hdr.desc_list_len !=
James Smarte3994412016-12-02 00:28:42 -08001160 fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)))
1161 fcret = VERR_CR_CONN_ACC_LEN;
James Smartf77fc872017-03-23 20:41:25 -07001162 else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST))
James Smarte3994412016-12-02 00:28:42 -08001163 fcret = VERR_LSDESC_RQST;
1164 else if (conn_acc->hdr.rqst.desc_len !=
1165 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1166 fcret = VERR_LSDESC_RQST_LEN;
1167 else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION)
1168 fcret = VERR_CR_CONN;
1169 else if (conn_acc->connectid.desc_tag !=
1170 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1171 fcret = VERR_CONN_ID;
1172 else if (conn_acc->connectid.desc_len !=
1173 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1174 fcret = VERR_CONN_ID_LEN;
1175
1176 if (fcret) {
1177 ret = -EBADF;
1178 dev_err(ctrl->dev,
1179 "q %d connect failed: %s\n",
1180 queue->qnum, validation_errors[fcret]);
1181 } else {
1182 queue->connection_id =
1183 be64_to_cpu(conn_acc->connectid.connection_id);
1184 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1185 }
1186
1187out_free_buffer:
1188 kfree(lsop);
1189out_no_memory:
1190 if (ret)
1191 dev_err(ctrl->dev,
1192 "queue %d connect command failed (%d).\n",
1193 queue->qnum, ret);
1194 return ret;
1195}
1196
1197static void
1198nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
1199{
1200 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
James Smarte3994412016-12-02 00:28:42 -08001201
James Smartc913a8b2017-04-11 11:35:08 -07001202 __nvme_fc_finish_ls_req(lsop);
James Smarte3994412016-12-02 00:28:42 -08001203
1204 /* fc-nvme iniator doesn't care about success or failure of cmd */
1205
1206 kfree(lsop);
1207}
1208
1209/*
1210 * This routine sends a FC-NVME LS to disconnect (aka terminate)
1211 * the FC-NVME Association. Terminating the association also
1212 * terminates the FC-NVME connections (per queue, both admin and io
1213 * queues) that are part of the association. E.g. things are torn
1214 * down, and the related FC-NVME Association ID and Connection IDs
1215 * become invalid.
1216 *
1217 * The behavior of the fc-nvme initiator is such that it's
1218 * understanding of the association and connections will implicitly
1219 * be torn down. The action is implicit as it may be due to a loss of
1220 * connectivity with the fc-nvme target, so you may never get a
1221 * response even if you tried. As such, the action of this routine
1222 * is to asynchronously send the LS, ignore any results of the LS, and
1223 * continue on with terminating the association. If the fc-nvme target
1224 * is present and receives the LS, it too can tear down.
1225 */
1226static void
1227nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl)
1228{
1229 struct fcnvme_ls_disconnect_rqst *discon_rqst;
1230 struct fcnvme_ls_disconnect_acc *discon_acc;
1231 struct nvmefc_ls_req_op *lsop;
1232 struct nvmefc_ls_req *lsreq;
James Smartc913a8b2017-04-11 11:35:08 -07001233 int ret;
James Smarte3994412016-12-02 00:28:42 -08001234
1235 lsop = kzalloc((sizeof(*lsop) +
1236 ctrl->lport->ops->lsrqst_priv_sz +
1237 sizeof(*discon_rqst) + sizeof(*discon_acc)),
1238 GFP_KERNEL);
1239 if (!lsop)
1240 /* couldn't sent it... too bad */
1241 return;
1242
1243 lsreq = &lsop->ls_req;
1244
1245 lsreq->private = (void *)&lsop[1];
1246 discon_rqst = (struct fcnvme_ls_disconnect_rqst *)
1247 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1248 discon_acc = (struct fcnvme_ls_disconnect_acc *)&discon_rqst[1];
1249
1250 discon_rqst->w0.ls_cmd = FCNVME_LS_DISCONNECT;
1251 discon_rqst->desc_list_len = cpu_to_be32(
1252 sizeof(struct fcnvme_lsdesc_assoc_id) +
1253 sizeof(struct fcnvme_lsdesc_disconn_cmd));
1254
1255 discon_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1256 discon_rqst->associd.desc_len =
1257 fcnvme_lsdesc_len(
1258 sizeof(struct fcnvme_lsdesc_assoc_id));
1259
1260 discon_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1261
1262 discon_rqst->discon_cmd.desc_tag = cpu_to_be32(
1263 FCNVME_LSDESC_DISCONN_CMD);
1264 discon_rqst->discon_cmd.desc_len =
1265 fcnvme_lsdesc_len(
1266 sizeof(struct fcnvme_lsdesc_disconn_cmd));
1267 discon_rqst->discon_cmd.scope = FCNVME_DISCONN_ASSOCIATION;
1268 discon_rqst->discon_cmd.id = cpu_to_be64(ctrl->association_id);
1269
1270 lsreq->rqstaddr = discon_rqst;
1271 lsreq->rqstlen = sizeof(*discon_rqst);
1272 lsreq->rspaddr = discon_acc;
1273 lsreq->rsplen = sizeof(*discon_acc);
1274 lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
1275
James Smartc913a8b2017-04-11 11:35:08 -07001276 ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop,
1277 nvme_fc_disconnect_assoc_done);
1278 if (ret)
1279 kfree(lsop);
James Smarte3994412016-12-02 00:28:42 -08001280
1281 /* only meaningful part to terminating the association */
1282 ctrl->association_id = 0;
1283}
1284
1285
1286/* *********************** NVME Ctrl Routines **************************** */
1287
James Smart78a7ac22017-04-23 08:30:07 -07001288static void __nvme_fc_final_op_cleanup(struct request *rq);
James Smartf874d5d2017-06-01 22:54:21 -07001289static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg);
James Smarte3994412016-12-02 00:28:42 -08001290
1291static int
1292nvme_fc_reinit_request(void *data, struct request *rq)
1293{
1294 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1295 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1296
1297 memset(cmdiu, 0, sizeof(*cmdiu));
1298 cmdiu->scsi_id = NVME_CMD_SCSI_ID;
1299 cmdiu->fc_id = NVME_CMD_FC_ID;
1300 cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
1301 memset(&op->rsp_iu, 0, sizeof(op->rsp_iu));
1302
1303 return 0;
1304}
1305
1306static void
1307__nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl,
1308 struct nvme_fc_fcp_op *op)
1309{
1310 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma,
1311 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1312 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
1313 sizeof(op->cmd_iu), DMA_TO_DEVICE);
1314
1315 atomic_set(&op->state, FCPOP_STATE_UNINIT);
1316}
1317
1318static void
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001319nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1320 unsigned int hctx_idx)
James Smarte3994412016-12-02 00:28:42 -08001321{
1322 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1323
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001324 return __nvme_fc_exit_request(set->driver_data, op);
James Smarte3994412016-12-02 00:28:42 -08001325}
1326
James Smart78a7ac22017-04-23 08:30:07 -07001327static int
1328__nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
1329{
1330 int state;
1331
1332 state = atomic_xchg(&op->state, FCPOP_STATE_ABORTED);
1333 if (state != FCPOP_STATE_ACTIVE) {
1334 atomic_set(&op->state, state);
1335 return -ECANCELED;
1336 }
1337
1338 ctrl->lport->ops->fcp_abort(&ctrl->lport->localport,
1339 &ctrl->rport->remoteport,
1340 op->queue->lldd_handle,
1341 &op->fcp_req);
1342
1343 return 0;
1344}
1345
James Smarte3994412016-12-02 00:28:42 -08001346static void
James Smart78a7ac22017-04-23 08:30:07 -07001347nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl)
James Smarte3994412016-12-02 00:28:42 -08001348{
1349 struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops;
James Smart78a7ac22017-04-23 08:30:07 -07001350 unsigned long flags;
1351 int i, ret;
James Smarte3994412016-12-02 00:28:42 -08001352
1353 for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
James Smart78a7ac22017-04-23 08:30:07 -07001354 if (atomic_read(&aen_op->state) != FCPOP_STATE_ACTIVE)
James Smarte3994412016-12-02 00:28:42 -08001355 continue;
James Smart78a7ac22017-04-23 08:30:07 -07001356
1357 spin_lock_irqsave(&ctrl->lock, flags);
James Smart61bff8e2017-04-23 08:30:08 -07001358 if (ctrl->flags & FCCTRL_TERMIO) {
1359 ctrl->iocnt++;
1360 aen_op->flags |= FCOP_FLAGS_TERMIO;
1361 }
James Smart78a7ac22017-04-23 08:30:07 -07001362 spin_unlock_irqrestore(&ctrl->lock, flags);
1363
1364 ret = __nvme_fc_abort_op(ctrl, aen_op);
1365 if (ret) {
1366 /*
1367 * if __nvme_fc_abort_op failed the io wasn't
1368 * active. Thus this call path is running in
1369 * parallel to the io complete. Treat as non-error.
1370 */
1371
1372 /* back out the flags/counters */
1373 spin_lock_irqsave(&ctrl->lock, flags);
James Smart61bff8e2017-04-23 08:30:08 -07001374 if (ctrl->flags & FCCTRL_TERMIO)
1375 ctrl->iocnt--;
James Smart78a7ac22017-04-23 08:30:07 -07001376 aen_op->flags &= ~FCOP_FLAGS_TERMIO;
1377 spin_unlock_irqrestore(&ctrl->lock, flags);
1378 return;
1379 }
James Smarte3994412016-12-02 00:28:42 -08001380 }
1381}
1382
James Smart78a7ac22017-04-23 08:30:07 -07001383static inline int
1384__nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
1385 struct nvme_fc_fcp_op *op)
1386{
1387 unsigned long flags;
1388 bool complete_rq = false;
1389
1390 spin_lock_irqsave(&ctrl->lock, flags);
James Smart61bff8e2017-04-23 08:30:08 -07001391 if (unlikely(op->flags & FCOP_FLAGS_TERMIO)) {
James Smart36715cf2017-05-22 15:28:42 -07001392 if (ctrl->flags & FCCTRL_TERMIO) {
1393 if (!--ctrl->iocnt)
1394 wake_up(&ctrl->ioabort_wait);
1395 }
James Smart61bff8e2017-04-23 08:30:08 -07001396 }
James Smart78a7ac22017-04-23 08:30:07 -07001397 if (op->flags & FCOP_FLAGS_RELEASED)
1398 complete_rq = true;
1399 else
1400 op->flags |= FCOP_FLAGS_COMPLETE;
1401 spin_unlock_irqrestore(&ctrl->lock, flags);
1402
1403 return complete_rq;
1404}
1405
Christoph Hellwigbaee29a2017-04-21 10:44:06 +02001406static void
James Smarte3994412016-12-02 00:28:42 -08001407nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
1408{
1409 struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1410 struct request *rq = op->rq;
1411 struct nvmefc_fcp_req *freq = &op->fcp_req;
1412 struct nvme_fc_ctrl *ctrl = op->ctrl;
1413 struct nvme_fc_queue *queue = op->queue;
1414 struct nvme_completion *cqe = &op->rsp_iu.cqe;
James Smart458f2802017-04-23 08:30:06 -07001415 struct nvme_command *sqe = &op->cmd_iu.sqe;
Christoph Hellwigd663b692017-04-20 16:02:56 +02001416 __le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001417 union nvme_result result;
James Smart0a02e392017-10-19 16:11:38 -07001418 bool terminate_assoc = true;
James Smarte3994412016-12-02 00:28:42 -08001419
1420 /*
1421 * WARNING:
1422 * The current linux implementation of a nvme controller
1423 * allocates a single tag set for all io queues and sizes
1424 * the io queues to fully hold all possible tags. Thus, the
1425 * implementation does not reference or care about the sqhd
1426 * value as it never needs to use the sqhd/sqtail pointers
1427 * for submission pacing.
1428 *
1429 * This affects the FC-NVME implementation in two ways:
1430 * 1) As the value doesn't matter, we don't need to waste
1431 * cycles extracting it from ERSPs and stamping it in the
1432 * cases where the transport fabricates CQEs on successful
1433 * completions.
1434 * 2) The FC-NVME implementation requires that delivery of
1435 * ERSP completions are to go back to the nvme layer in order
1436 * relative to the rsn, such that the sqhd value will always
1437 * be "in order" for the nvme layer. As the nvme layer in
1438 * linux doesn't care about sqhd, there's no need to return
1439 * them in order.
1440 *
1441 * Additionally:
1442 * As the core nvme layer in linux currently does not look at
1443 * every field in the cqe - in cases where the FC transport must
1444 * fabricate a CQE, the following fields will not be set as they
1445 * are not referenced:
1446 * cqe.sqid, cqe.sqhd, cqe.command_id
James Smartf874d5d2017-06-01 22:54:21 -07001447 *
1448 * Failure or error of an individual i/o, in a transport
1449 * detected fashion unrelated to the nvme completion status,
1450 * potentially cause the initiator and target sides to get out
1451 * of sync on SQ head/tail (aka outstanding io count allowed).
1452 * Per FC-NVME spec, failure of an individual command requires
1453 * the connection to be terminated, which in turn requires the
1454 * association to be terminated.
James Smarte3994412016-12-02 00:28:42 -08001455 */
1456
1457 fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
1458 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1459
James Smart0a02e392017-10-19 16:11:38 -07001460 if (atomic_read(&op->state) == FCPOP_STATE_ABORTED ||
1461 op->flags & FCOP_FLAGS_TERMIO)
1462 status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
James Smart62eeacb2017-03-23 20:41:27 -07001463 else if (freq->status)
James Smart56b7103a2017-09-07 16:27:26 -07001464 status = cpu_to_le16(NVME_SC_INTERNAL << 1);
James Smarte3994412016-12-02 00:28:42 -08001465
1466 /*
1467 * For the linux implementation, if we have an unsuccesful
1468 * status, they blk-mq layer can typically be called with the
1469 * non-zero status and the content of the cqe isn't important.
1470 */
1471 if (status)
1472 goto done;
1473
1474 /*
1475 * command completed successfully relative to the wire
1476 * protocol. However, validate anything received and
1477 * extract the status and result from the cqe (create it
1478 * where necessary).
1479 */
1480
1481 switch (freq->rcv_rsplen) {
1482
1483 case 0:
1484 case NVME_FC_SIZEOF_ZEROS_RSP:
1485 /*
1486 * No response payload or 12 bytes of payload (which
1487 * should all be zeros) are considered successful and
1488 * no payload in the CQE by the transport.
1489 */
1490 if (freq->transferred_length !=
1491 be32_to_cpu(op->cmd_iu.data_len)) {
James Smart56b7103a2017-09-07 16:27:26 -07001492 status = cpu_to_le16(NVME_SC_INTERNAL << 1);
James Smarte3994412016-12-02 00:28:42 -08001493 goto done;
1494 }
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001495 result.u64 = 0;
James Smarte3994412016-12-02 00:28:42 -08001496 break;
1497
1498 case sizeof(struct nvme_fc_ersp_iu):
1499 /*
1500 * The ERSP IU contains a full completion with CQE.
1501 * Validate ERSP IU and look at cqe.
1502 */
1503 if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) !=
1504 (freq->rcv_rsplen / 4) ||
1505 be32_to_cpu(op->rsp_iu.xfrd_len) !=
1506 freq->transferred_length ||
James Smart726a1082017-03-23 20:41:23 -07001507 op->rsp_iu.status_code ||
James Smart458f2802017-04-23 08:30:06 -07001508 sqe->common.command_id != cqe->command_id)) {
James Smart56b7103a2017-09-07 16:27:26 -07001509 status = cpu_to_le16(NVME_SC_INTERNAL << 1);
James Smarte3994412016-12-02 00:28:42 -08001510 goto done;
1511 }
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001512 result = cqe->result;
Christoph Hellwigd663b692017-04-20 16:02:56 +02001513 status = cqe->status;
James Smarte3994412016-12-02 00:28:42 -08001514 break;
1515
1516 default:
James Smart56b7103a2017-09-07 16:27:26 -07001517 status = cpu_to_le16(NVME_SC_INTERNAL << 1);
James Smarte3994412016-12-02 00:28:42 -08001518 goto done;
1519 }
1520
James Smartf874d5d2017-06-01 22:54:21 -07001521 terminate_assoc = false;
1522
James Smarte3994412016-12-02 00:28:42 -08001523done:
James Smart78a7ac22017-04-23 08:30:07 -07001524 if (op->flags & FCOP_FLAGS_AEN) {
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001525 nvme_complete_async_event(&queue->ctrl->ctrl, status, &result);
James Smart0a02e392017-10-19 16:11:38 -07001526 __nvme_fc_fcpop_chk_teardowns(ctrl, op);
James Smart78a7ac22017-04-23 08:30:07 -07001527 atomic_set(&op->state, FCPOP_STATE_IDLE);
1528 op->flags = FCOP_FLAGS_AEN; /* clear other flags */
James Smarte3994412016-12-02 00:28:42 -08001529 nvme_fc_ctrl_put(ctrl);
James Smartf874d5d2017-06-01 22:54:21 -07001530 goto check_error;
James Smarte3994412016-12-02 00:28:42 -08001531 }
1532
James Smart0a02e392017-10-19 16:11:38 -07001533 /*
1534 * Force failures of commands if we're killing the controller
1535 * or have an error on a command used to create an new association
1536 */
1537 if (status &&
1538 (blk_queue_dying(rq->q) ||
1539 ctrl->ctrl.state == NVME_CTRL_NEW ||
1540 ctrl->ctrl.state == NVME_CTRL_RECONNECTING))
1541 status |= cpu_to_le16(NVME_SC_DNR << 1);
1542
1543 if (__nvme_fc_fcpop_chk_teardowns(ctrl, op))
James Smart78a7ac22017-04-23 08:30:07 -07001544 __nvme_fc_final_op_cleanup(rq);
James Smart0a02e392017-10-19 16:11:38 -07001545 else
1546 nvme_end_request(rq, status, result);
James Smartf874d5d2017-06-01 22:54:21 -07001547
1548check_error:
1549 if (terminate_assoc)
1550 nvme_fc_error_recovery(ctrl, "transport detected io error");
James Smarte3994412016-12-02 00:28:42 -08001551}
1552
1553static int
1554__nvme_fc_init_request(struct nvme_fc_ctrl *ctrl,
1555 struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op,
1556 struct request *rq, u32 rqno)
1557{
1558 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1559 int ret = 0;
1560
1561 memset(op, 0, sizeof(*op));
1562 op->fcp_req.cmdaddr = &op->cmd_iu;
1563 op->fcp_req.cmdlen = sizeof(op->cmd_iu);
1564 op->fcp_req.rspaddr = &op->rsp_iu;
1565 op->fcp_req.rsplen = sizeof(op->rsp_iu);
1566 op->fcp_req.done = nvme_fc_fcpio_done;
1567 op->fcp_req.first_sgl = (struct scatterlist *)&op[1];
1568 op->fcp_req.private = &op->fcp_req.first_sgl[SG_CHUNK_SIZE];
1569 op->ctrl = ctrl;
1570 op->queue = queue;
1571 op->rq = rq;
1572 op->rqno = rqno;
1573
1574 cmdiu->scsi_id = NVME_CMD_SCSI_ID;
1575 cmdiu->fc_id = NVME_CMD_FC_ID;
1576 cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
1577
1578 op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev,
1579 &op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE);
1580 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) {
1581 dev_err(ctrl->dev,
1582 "FCP Op failed - cmdiu dma mapping failed.\n");
1583 ret = EFAULT;
1584 goto out_on_error;
1585 }
1586
1587 op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev,
1588 &op->rsp_iu, sizeof(op->rsp_iu),
1589 DMA_FROM_DEVICE);
1590 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) {
1591 dev_err(ctrl->dev,
1592 "FCP Op failed - rspiu dma mapping failed.\n");
1593 ret = EFAULT;
1594 }
1595
1596 atomic_set(&op->state, FCPOP_STATE_IDLE);
1597out_on_error:
1598 return ret;
1599}
1600
1601static int
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001602nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq,
1603 unsigned int hctx_idx, unsigned int numa_node)
James Smarte3994412016-12-02 00:28:42 -08001604{
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001605 struct nvme_fc_ctrl *ctrl = set->driver_data;
James Smarte3994412016-12-02 00:28:42 -08001606 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
Christoph Hellwig76f983c2017-06-13 09:15:20 +02001607 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
1608 struct nvme_fc_queue *queue = &ctrl->queues[queue_idx];
James Smarte3994412016-12-02 00:28:42 -08001609
1610 return __nvme_fc_init_request(ctrl, queue, op, rq, queue->rqcnt++);
1611}
1612
1613static int
1614nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl)
1615{
1616 struct nvme_fc_fcp_op *aen_op;
1617 struct nvme_fc_cmd_iu *cmdiu;
1618 struct nvme_command *sqe;
James Smart61bff8e2017-04-23 08:30:08 -07001619 void *private;
James Smarte3994412016-12-02 00:28:42 -08001620 int i, ret;
1621
1622 aen_op = ctrl->aen_ops;
1623 for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
James Smart61bff8e2017-04-23 08:30:08 -07001624 private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz,
1625 GFP_KERNEL);
1626 if (!private)
1627 return -ENOMEM;
1628
James Smarte3994412016-12-02 00:28:42 -08001629 cmdiu = &aen_op->cmd_iu;
1630 sqe = &cmdiu->sqe;
1631 ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0],
1632 aen_op, (struct request *)NULL,
1633 (AEN_CMDID_BASE + i));
James Smart61bff8e2017-04-23 08:30:08 -07001634 if (ret) {
1635 kfree(private);
James Smarte3994412016-12-02 00:28:42 -08001636 return ret;
James Smart61bff8e2017-04-23 08:30:08 -07001637 }
James Smarte3994412016-12-02 00:28:42 -08001638
James Smart78a7ac22017-04-23 08:30:07 -07001639 aen_op->flags = FCOP_FLAGS_AEN;
James Smart61bff8e2017-04-23 08:30:08 -07001640 aen_op->fcp_req.first_sgl = NULL; /* no sg list */
1641 aen_op->fcp_req.private = private;
James Smart78a7ac22017-04-23 08:30:07 -07001642
James Smarte3994412016-12-02 00:28:42 -08001643 memset(sqe, 0, sizeof(*sqe));
1644 sqe->common.opcode = nvme_admin_async_event;
James Smart78a7ac22017-04-23 08:30:07 -07001645 /* Note: core layer may overwrite the sqe.command_id value */
James Smarte3994412016-12-02 00:28:42 -08001646 sqe->common.command_id = AEN_CMDID_BASE + i;
1647 }
1648 return 0;
1649}
1650
James Smart61bff8e2017-04-23 08:30:08 -07001651static void
1652nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl)
1653{
1654 struct nvme_fc_fcp_op *aen_op;
1655 int i;
1656
1657 aen_op = ctrl->aen_ops;
1658 for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
1659 if (!aen_op->fcp_req.private)
1660 continue;
1661
1662 __nvme_fc_exit_request(ctrl, aen_op);
1663
1664 kfree(aen_op->fcp_req.private);
1665 aen_op->fcp_req.private = NULL;
1666 }
1667}
James Smarte3994412016-12-02 00:28:42 -08001668
1669static inline void
1670__nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, struct nvme_fc_ctrl *ctrl,
1671 unsigned int qidx)
1672{
1673 struct nvme_fc_queue *queue = &ctrl->queues[qidx];
1674
1675 hctx->driver_data = queue;
1676 queue->hctx = hctx;
1677}
1678
1679static int
1680nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1681 unsigned int hctx_idx)
1682{
1683 struct nvme_fc_ctrl *ctrl = data;
1684
1685 __nvme_fc_init_hctx(hctx, ctrl, hctx_idx + 1);
1686
1687 return 0;
1688}
1689
1690static int
1691nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1692 unsigned int hctx_idx)
1693{
1694 struct nvme_fc_ctrl *ctrl = data;
1695
1696 __nvme_fc_init_hctx(hctx, ctrl, hctx_idx);
1697
1698 return 0;
1699}
1700
1701static void
1702nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx, size_t queue_size)
1703{
1704 struct nvme_fc_queue *queue;
1705
1706 queue = &ctrl->queues[idx];
1707 memset(queue, 0, sizeof(*queue));
1708 queue->ctrl = ctrl;
1709 queue->qnum = idx;
1710 atomic_set(&queue->csn, 1);
1711 queue->dev = ctrl->dev;
1712
1713 if (idx > 0)
1714 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
1715 else
1716 queue->cmnd_capsule_len = sizeof(struct nvme_command);
1717
1718 queue->queue_size = queue_size;
1719
1720 /*
1721 * Considered whether we should allocate buffers for all SQEs
1722 * and CQEs and dma map them - mapping their respective entries
1723 * into the request structures (kernel vm addr and dma address)
1724 * thus the driver could use the buffers/mappings directly.
1725 * It only makes sense if the LLDD would use them for its
1726 * messaging api. It's very unlikely most adapter api's would use
1727 * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
1728 * structures were used instead.
1729 */
1730}
1731
1732/*
1733 * This routine terminates a queue at the transport level.
1734 * The transport has already ensured that all outstanding ios on
1735 * the queue have been terminated.
1736 * The transport will send a Disconnect LS request to terminate
1737 * the queue's connection. Termination of the admin queue will also
1738 * terminate the association at the target.
1739 */
1740static void
1741nvme_fc_free_queue(struct nvme_fc_queue *queue)
1742{
1743 if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags))
1744 return;
1745
1746 /*
1747 * Current implementation never disconnects a single queue.
1748 * It always terminates a whole association. So there is never
1749 * a disconnect(queue) LS sent to the target.
1750 */
1751
1752 queue->connection_id = 0;
1753 clear_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1754}
1755
1756static void
1757__nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl,
1758 struct nvme_fc_queue *queue, unsigned int qidx)
1759{
1760 if (ctrl->lport->ops->delete_queue)
1761 ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx,
1762 queue->lldd_handle);
1763 queue->lldd_handle = NULL;
1764}
1765
1766static void
James Smarte3994412016-12-02 00:28:42 -08001767nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl)
1768{
1769 int i;
1770
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001771 for (i = 1; i < ctrl->ctrl.queue_count; i++)
James Smarte3994412016-12-02 00:28:42 -08001772 nvme_fc_free_queue(&ctrl->queues[i]);
1773}
1774
1775static int
1776__nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl,
1777 struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize)
1778{
1779 int ret = 0;
1780
1781 queue->lldd_handle = NULL;
1782 if (ctrl->lport->ops->create_queue)
1783 ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport,
1784 qidx, qsize, &queue->lldd_handle);
1785
1786 return ret;
1787}
1788
1789static void
1790nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl)
1791{
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001792 struct nvme_fc_queue *queue = &ctrl->queues[ctrl->ctrl.queue_count - 1];
James Smarte3994412016-12-02 00:28:42 -08001793 int i;
1794
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001795 for (i = ctrl->ctrl.queue_count - 1; i >= 1; i--, queue--)
James Smarte3994412016-12-02 00:28:42 -08001796 __nvme_fc_delete_hw_queue(ctrl, queue, i);
1797}
1798
1799static int
1800nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
1801{
1802 struct nvme_fc_queue *queue = &ctrl->queues[1];
Johannes Thumshirn17a1ec02016-12-15 14:20:48 +01001803 int i, ret;
James Smarte3994412016-12-02 00:28:42 -08001804
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001805 for (i = 1; i < ctrl->ctrl.queue_count; i++, queue++) {
James Smarte3994412016-12-02 00:28:42 -08001806 ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize);
Johannes Thumshirn17a1ec02016-12-15 14:20:48 +01001807 if (ret)
1808 goto delete_queues;
James Smarte3994412016-12-02 00:28:42 -08001809 }
1810
1811 return 0;
Johannes Thumshirn17a1ec02016-12-15 14:20:48 +01001812
1813delete_queues:
1814 for (; i >= 0; i--)
1815 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i);
1816 return ret;
James Smarte3994412016-12-02 00:28:42 -08001817}
1818
1819static int
1820nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
1821{
1822 int i, ret = 0;
1823
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001824 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
James Smarte3994412016-12-02 00:28:42 -08001825 ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize,
1826 (qsize / 5));
1827 if (ret)
1828 break;
1829 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
1830 if (ret)
1831 break;
1832 }
1833
1834 return ret;
1835}
1836
1837static void
1838nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl)
1839{
1840 int i;
1841
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001842 for (i = 1; i < ctrl->ctrl.queue_count; i++)
James Smarte3994412016-12-02 00:28:42 -08001843 nvme_fc_init_queue(ctrl, i, ctrl->ctrl.sqsize);
1844}
1845
1846static void
1847nvme_fc_ctrl_free(struct kref *ref)
1848{
1849 struct nvme_fc_ctrl *ctrl =
1850 container_of(ref, struct nvme_fc_ctrl, ref);
1851 unsigned long flags;
1852
James Smart61bff8e2017-04-23 08:30:08 -07001853 if (ctrl->ctrl.tagset) {
1854 blk_cleanup_queue(ctrl->ctrl.connect_q);
1855 blk_mq_free_tag_set(&ctrl->tag_set);
James Smarte3994412016-12-02 00:28:42 -08001856 }
1857
James Smart61bff8e2017-04-23 08:30:08 -07001858 /* remove from rport list */
1859 spin_lock_irqsave(&ctrl->rport->lock, flags);
1860 list_del(&ctrl->ctrl_list);
1861 spin_unlock_irqrestore(&ctrl->rport->lock, flags);
1862
Sagi Grimbergf9c5af52017-07-02 15:39:34 +03001863 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
James Smart61bff8e2017-04-23 08:30:08 -07001864 blk_cleanup_queue(ctrl->ctrl.admin_q);
1865 blk_mq_free_tag_set(&ctrl->admin_tag_set);
1866
1867 kfree(ctrl->queues);
1868
James Smarte3994412016-12-02 00:28:42 -08001869 put_device(ctrl->dev);
1870 nvme_fc_rport_put(ctrl->rport);
1871
James Smarte3994412016-12-02 00:28:42 -08001872 ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
Ewan D. Milnede414472017-04-24 13:24:16 -04001873 if (ctrl->ctrl.opts)
1874 nvmf_free_options(ctrl->ctrl.opts);
James Smarte3994412016-12-02 00:28:42 -08001875 kfree(ctrl);
1876}
1877
1878static void
1879nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl)
1880{
1881 kref_put(&ctrl->ref, nvme_fc_ctrl_free);
1882}
1883
1884static int
1885nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
1886{
1887 return kref_get_unless_zero(&ctrl->ref);
1888}
1889
1890/*
1891 * All accesses from nvme core layer done - can now free the
1892 * controller. Called after last nvme_put_ctrl() call
1893 */
1894static void
James Smart61bff8e2017-04-23 08:30:08 -07001895nvme_fc_nvme_ctrl_freed(struct nvme_ctrl *nctrl)
James Smarte3994412016-12-02 00:28:42 -08001896{
1897 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
1898
1899 WARN_ON(nctrl != &ctrl->ctrl);
1900
James Smart61bff8e2017-04-23 08:30:08 -07001901 nvme_fc_ctrl_put(ctrl);
1902}
James Smarte3994412016-12-02 00:28:42 -08001903
James Smart61bff8e2017-04-23 08:30:08 -07001904static void
1905nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
1906{
James Smart69fa9642017-06-21 17:43:21 -07001907 /* only proceed if in LIVE state - e.g. on first error */
1908 if (ctrl->ctrl.state != NVME_CTRL_LIVE)
1909 return;
1910
James Smart61bff8e2017-04-23 08:30:08 -07001911 dev_warn(ctrl->ctrl.device,
1912 "NVME-FC{%d}: transport association error detected: %s\n",
1913 ctrl->cnum, errmsg);
James Smart589ff772017-05-15 17:10:19 -07001914 dev_warn(ctrl->ctrl.device,
James Smart61bff8e2017-04-23 08:30:08 -07001915 "NVME-FC{%d}: resetting controller\n", ctrl->cnum);
James Smarte3994412016-12-02 00:28:42 -08001916
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001917 nvme_reset_ctrl(&ctrl->ctrl);
James Smarte3994412016-12-02 00:28:42 -08001918}
1919
Christoph Hellwigbaee29a2017-04-21 10:44:06 +02001920static enum blk_eh_timer_return
James Smarte3994412016-12-02 00:28:42 -08001921nvme_fc_timeout(struct request *rq, bool reserved)
1922{
1923 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1924 struct nvme_fc_ctrl *ctrl = op->ctrl;
1925 int ret;
1926
James Smart134aedc2017-10-19 16:11:39 -07001927 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE ||
1928 atomic_read(&op->state) == FCPOP_STATE_ABORTED)
James Smarte3994412016-12-02 00:28:42 -08001929 return BLK_EH_RESET_TIMER;
1930
1931 ret = __nvme_fc_abort_op(ctrl, op);
1932 if (ret)
James Smart134aedc2017-10-19 16:11:39 -07001933 /* io wasn't active to abort */
1934 return BLK_EH_NOT_HANDLED;
James Smarte3994412016-12-02 00:28:42 -08001935
1936 /*
James Smart61bff8e2017-04-23 08:30:08 -07001937 * we can't individually ABTS an io without affecting the queue,
1938 * thus killing the queue, adn thus the association.
1939 * So resolve by performing a controller reset, which will stop
1940 * the host/io stack, terminate the association on the link,
1941 * and recreate an association on the link.
James Smarte3994412016-12-02 00:28:42 -08001942 */
James Smart61bff8e2017-04-23 08:30:08 -07001943 nvme_fc_error_recovery(ctrl, "io timeout error");
James Smarte3994412016-12-02 00:28:42 -08001944
James Smart134aedc2017-10-19 16:11:39 -07001945 /*
1946 * the io abort has been initiated. Have the reset timer
1947 * restarted and the abort completion will complete the io
1948 * shortly. Avoids a synchronous wait while the abort finishes.
1949 */
1950 return BLK_EH_RESET_TIMER;
James Smarte3994412016-12-02 00:28:42 -08001951}
1952
1953static int
1954nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
1955 struct nvme_fc_fcp_op *op)
1956{
1957 struct nvmefc_fcp_req *freq = &op->fcp_req;
James Smarte3994412016-12-02 00:28:42 -08001958 enum dma_data_direction dir;
1959 int ret;
1960
1961 freq->sg_cnt = 0;
1962
Christoph Hellwigb131c612017-01-13 12:29:12 +01001963 if (!blk_rq_payload_bytes(rq))
James Smarte3994412016-12-02 00:28:42 -08001964 return 0;
1965
1966 freq->sg_table.sgl = freq->first_sgl;
Christoph Hellwig19e420b2017-01-19 16:55:57 +01001967 ret = sg_alloc_table_chained(&freq->sg_table,
1968 blk_rq_nr_phys_segments(rq), freq->sg_table.sgl);
James Smarte3994412016-12-02 00:28:42 -08001969 if (ret)
1970 return -ENOMEM;
1971
1972 op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl);
Christoph Hellwig19e420b2017-01-19 16:55:57 +01001973 WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
James Smarte3994412016-12-02 00:28:42 -08001974 dir = (rq_data_dir(rq) == WRITE) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
1975 freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
1976 op->nents, dir);
1977 if (unlikely(freq->sg_cnt <= 0)) {
1978 sg_free_table_chained(&freq->sg_table, true);
1979 freq->sg_cnt = 0;
1980 return -EFAULT;
1981 }
1982
1983 /*
1984 * TODO: blk_integrity_rq(rq) for DIF
1985 */
1986 return 0;
1987}
1988
1989static void
1990nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
1991 struct nvme_fc_fcp_op *op)
1992{
1993 struct nvmefc_fcp_req *freq = &op->fcp_req;
1994
1995 if (!freq->sg_cnt)
1996 return;
1997
1998 fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
1999 ((rq_data_dir(rq) == WRITE) ?
2000 DMA_TO_DEVICE : DMA_FROM_DEVICE));
2001
2002 nvme_cleanup_cmd(rq);
2003
2004 sg_free_table_chained(&freq->sg_table, true);
2005
2006 freq->sg_cnt = 0;
2007}
2008
2009/*
2010 * In FC, the queue is a logical thing. At transport connect, the target
2011 * creates its "queue" and returns a handle that is to be given to the
2012 * target whenever it posts something to the corresponding SQ. When an
2013 * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
2014 * command contained within the SQE, an io, and assigns a FC exchange
2015 * to it. The SQE and the associated SQ handle are sent in the initial
2016 * CMD IU sents on the exchange. All transfers relative to the io occur
2017 * as part of the exchange. The CQE is the last thing for the io,
2018 * which is transferred (explicitly or implicitly) with the RSP IU
2019 * sent on the exchange. After the CQE is received, the FC exchange is
2020 * terminaed and the Exchange may be used on a different io.
2021 *
2022 * The transport to LLDD api has the transport making a request for a
2023 * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
2024 * resource and transfers the command. The LLDD will then process all
2025 * steps to complete the io. Upon completion, the transport done routine
2026 * is called.
2027 *
2028 * So - while the operation is outstanding to the LLDD, there is a link
2029 * level FC exchange resource that is also outstanding. This must be
2030 * considered in all cleanup operations.
2031 */
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002032static blk_status_t
James Smarte3994412016-12-02 00:28:42 -08002033nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
2034 struct nvme_fc_fcp_op *op, u32 data_len,
2035 enum nvmefc_fcp_datadir io_dir)
2036{
2037 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2038 struct nvme_command *sqe = &cmdiu->sqe;
2039 u32 csn;
2040 int ret;
2041
James Smart61bff8e2017-04-23 08:30:08 -07002042 /*
2043 * before attempting to send the io, check to see if we believe
2044 * the target device is present
2045 */
2046 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
James Smart8b25f352017-07-18 14:29:34 -07002047 goto busy;
James Smart61bff8e2017-04-23 08:30:08 -07002048
James Smarte3994412016-12-02 00:28:42 -08002049 if (!nvme_fc_ctrl_get(ctrl))
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002050 return BLK_STS_IOERR;
James Smarte3994412016-12-02 00:28:42 -08002051
2052 /* format the FC-NVME CMD IU and fcp_req */
2053 cmdiu->connection_id = cpu_to_be64(queue->connection_id);
2054 csn = atomic_inc_return(&queue->csn);
2055 cmdiu->csn = cpu_to_be32(csn);
2056 cmdiu->data_len = cpu_to_be32(data_len);
2057 switch (io_dir) {
2058 case NVMEFC_FCP_WRITE:
2059 cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
2060 break;
2061 case NVMEFC_FCP_READ:
2062 cmdiu->flags = FCNVME_CMD_FLAGS_READ;
2063 break;
2064 case NVMEFC_FCP_NODATA:
2065 cmdiu->flags = 0;
2066 break;
2067 }
2068 op->fcp_req.payload_length = data_len;
2069 op->fcp_req.io_dir = io_dir;
2070 op->fcp_req.transferred_length = 0;
2071 op->fcp_req.rcv_rsplen = 0;
James Smart62eeacb2017-03-23 20:41:27 -07002072 op->fcp_req.status = NVME_SC_SUCCESS;
James Smarte3994412016-12-02 00:28:42 -08002073 op->fcp_req.sqid = cpu_to_le16(queue->qnum);
2074
2075 /*
2076 * validate per fabric rules, set fields mandated by fabric spec
2077 * as well as those by FC-NVME spec.
2078 */
2079 WARN_ON_ONCE(sqe->common.metadata);
James Smarte3994412016-12-02 00:28:42 -08002080 sqe->common.flags |= NVME_CMD_SGL_METABUF;
2081
2082 /*
James Smartd9d34c02017-09-07 13:20:24 -07002083 * format SQE DPTR field per FC-NVME rules:
2084 * type=0x5 Transport SGL Data Block Descriptor
2085 * subtype=0xA Transport-specific value
2086 * address=0
2087 * length=length of the data series
James Smarte3994412016-12-02 00:28:42 -08002088 */
James Smartd9d34c02017-09-07 13:20:24 -07002089 sqe->rw.dptr.sgl.type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2090 NVME_SGL_FMT_TRANSPORT_A;
James Smarte3994412016-12-02 00:28:42 -08002091 sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
2092 sqe->rw.dptr.sgl.addr = 0;
2093
James Smart78a7ac22017-04-23 08:30:07 -07002094 if (!(op->flags & FCOP_FLAGS_AEN)) {
James Smarte3994412016-12-02 00:28:42 -08002095 ret = nvme_fc_map_data(ctrl, op->rq, op);
2096 if (ret < 0) {
James Smarte3994412016-12-02 00:28:42 -08002097 nvme_cleanup_cmd(op->rq);
2098 nvme_fc_ctrl_put(ctrl);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002099 if (ret == -ENOMEM || ret == -EAGAIN)
2100 return BLK_STS_RESOURCE;
2101 return BLK_STS_IOERR;
James Smarte3994412016-12-02 00:28:42 -08002102 }
2103 }
2104
2105 fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
2106 sizeof(op->cmd_iu), DMA_TO_DEVICE);
2107
2108 atomic_set(&op->state, FCPOP_STATE_ACTIVE);
2109
James Smart78a7ac22017-04-23 08:30:07 -07002110 if (!(op->flags & FCOP_FLAGS_AEN))
James Smarte3994412016-12-02 00:28:42 -08002111 blk_mq_start_request(op->rq);
2112
2113 ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
2114 &ctrl->rport->remoteport,
2115 queue->lldd_handle, &op->fcp_req);
2116
2117 if (ret) {
James Smart8b25f352017-07-18 14:29:34 -07002118 if (!(op->flags & FCOP_FLAGS_AEN))
James Smarte3994412016-12-02 00:28:42 -08002119 nvme_fc_unmap_data(ctrl, op->rq, op);
James Smarte3994412016-12-02 00:28:42 -08002120
2121 nvme_fc_ctrl_put(ctrl);
2122
James Smart8b25f352017-07-18 14:29:34 -07002123 if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE &&
2124 ret != -EBUSY)
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002125 return BLK_STS_IOERR;
James Smarte3994412016-12-02 00:28:42 -08002126
James Smart8b25f352017-07-18 14:29:34 -07002127 goto busy;
James Smarte3994412016-12-02 00:28:42 -08002128 }
2129
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002130 return BLK_STS_OK;
James Smart8b25f352017-07-18 14:29:34 -07002131
2132busy:
2133 if (!(op->flags & FCOP_FLAGS_AEN) && queue->hctx)
2134 blk_mq_delay_run_hw_queue(queue->hctx, NVMEFC_QUEUE_DELAY);
2135
2136 return BLK_STS_RESOURCE;
James Smarte3994412016-12-02 00:28:42 -08002137}
2138
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002139static blk_status_t
James Smarte3994412016-12-02 00:28:42 -08002140nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
2141 const struct blk_mq_queue_data *bd)
2142{
2143 struct nvme_ns *ns = hctx->queue->queuedata;
2144 struct nvme_fc_queue *queue = hctx->driver_data;
2145 struct nvme_fc_ctrl *ctrl = queue->ctrl;
2146 struct request *rq = bd->rq;
2147 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2148 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2149 struct nvme_command *sqe = &cmdiu->sqe;
2150 enum nvmefc_fcp_datadir io_dir;
2151 u32 data_len;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002152 blk_status_t ret;
James Smarte3994412016-12-02 00:28:42 -08002153
2154 ret = nvme_setup_cmd(ns, rq, sqe);
2155 if (ret)
2156 return ret;
2157
Christoph Hellwigb131c612017-01-13 12:29:12 +01002158 data_len = blk_rq_payload_bytes(rq);
James Smarte3994412016-12-02 00:28:42 -08002159 if (data_len)
2160 io_dir = ((rq_data_dir(rq) == WRITE) ?
2161 NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
2162 else
2163 io_dir = NVMEFC_FCP_NODATA;
2164
2165 return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
2166}
2167
2168static struct blk_mq_tags *
2169nvme_fc_tagset(struct nvme_fc_queue *queue)
2170{
2171 if (queue->qnum == 0)
2172 return queue->ctrl->admin_tag_set.tags[queue->qnum];
2173
2174 return queue->ctrl->tag_set.tags[queue->qnum - 1];
2175}
2176
2177static int
2178nvme_fc_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
2179
2180{
2181 struct nvme_fc_queue *queue = hctx->driver_data;
2182 struct nvme_fc_ctrl *ctrl = queue->ctrl;
2183 struct request *req;
2184 struct nvme_fc_fcp_op *op;
2185
2186 req = blk_mq_tag_to_rq(nvme_fc_tagset(queue), tag);
James Smart61bff8e2017-04-23 08:30:08 -07002187 if (!req)
James Smarte3994412016-12-02 00:28:42 -08002188 return 0;
James Smarte3994412016-12-02 00:28:42 -08002189
2190 op = blk_mq_rq_to_pdu(req);
2191
2192 if ((atomic_read(&op->state) == FCPOP_STATE_ACTIVE) &&
2193 (ctrl->lport->ops->poll_queue))
2194 ctrl->lport->ops->poll_queue(&ctrl->lport->localport,
2195 queue->lldd_handle);
2196
2197 return ((atomic_read(&op->state) != FCPOP_STATE_ACTIVE));
2198}
2199
2200static void
2201nvme_fc_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
2202{
2203 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
2204 struct nvme_fc_fcp_op *aen_op;
James Smart61bff8e2017-04-23 08:30:08 -07002205 unsigned long flags;
2206 bool terminating = false;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002207 blk_status_t ret;
James Smarte3994412016-12-02 00:28:42 -08002208
2209 if (aer_idx > NVME_FC_NR_AEN_COMMANDS)
2210 return;
2211
James Smart61bff8e2017-04-23 08:30:08 -07002212 spin_lock_irqsave(&ctrl->lock, flags);
2213 if (ctrl->flags & FCCTRL_TERMIO)
2214 terminating = true;
2215 spin_unlock_irqrestore(&ctrl->lock, flags);
2216
2217 if (terminating)
2218 return;
2219
James Smarte3994412016-12-02 00:28:42 -08002220 aen_op = &ctrl->aen_ops[aer_idx];
2221
2222 ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
2223 NVMEFC_FCP_NODATA);
2224 if (ret)
2225 dev_err(ctrl->ctrl.device,
2226 "failed async event work [%d]\n", aer_idx);
2227}
2228
2229static void
James Smart78a7ac22017-04-23 08:30:07 -07002230__nvme_fc_final_op_cleanup(struct request *rq)
James Smarte3994412016-12-02 00:28:42 -08002231{
2232 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2233 struct nvme_fc_ctrl *ctrl = op->ctrl;
James Smarte3994412016-12-02 00:28:42 -08002234
James Smart78a7ac22017-04-23 08:30:07 -07002235 atomic_set(&op->state, FCPOP_STATE_IDLE);
2236 op->flags &= ~(FCOP_FLAGS_TERMIO | FCOP_FLAGS_RELEASED |
2237 FCOP_FLAGS_COMPLETE);
James Smarte3994412016-12-02 00:28:42 -08002238
James Smarte3994412016-12-02 00:28:42 -08002239 nvme_fc_unmap_data(ctrl, rq, op);
Christoph Hellwig77f02a72017-03-30 13:41:32 +02002240 nvme_complete_rq(rq);
James Smarte3994412016-12-02 00:28:42 -08002241 nvme_fc_ctrl_put(ctrl);
2242
James Smarte3994412016-12-02 00:28:42 -08002243}
2244
James Smart78a7ac22017-04-23 08:30:07 -07002245static void
2246nvme_fc_complete_rq(struct request *rq)
2247{
2248 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2249 struct nvme_fc_ctrl *ctrl = op->ctrl;
2250 unsigned long flags;
2251 bool completed = false;
2252
2253 /*
2254 * the core layer, on controller resets after calling
2255 * nvme_shutdown_ctrl(), calls complete_rq without our
2256 * calling blk_mq_complete_request(), thus there may still
2257 * be live i/o outstanding with the LLDD. Means transport has
2258 * to track complete calls vs fcpio_done calls to know what
2259 * path to take on completes and dones.
2260 */
2261 spin_lock_irqsave(&ctrl->lock, flags);
2262 if (op->flags & FCOP_FLAGS_COMPLETE)
2263 completed = true;
2264 else
2265 op->flags |= FCOP_FLAGS_RELEASED;
2266 spin_unlock_irqrestore(&ctrl->lock, flags);
2267
2268 if (completed)
2269 __nvme_fc_final_op_cleanup(rq);
2270}
2271
James Smarte3994412016-12-02 00:28:42 -08002272/*
2273 * This routine is used by the transport when it needs to find active
2274 * io on a queue that is to be terminated. The transport uses
2275 * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
2276 * this routine to kill them on a 1 by 1 basis.
2277 *
2278 * As FC allocates FC exchange for each io, the transport must contact
2279 * the LLDD to terminate the exchange, thus releasing the FC exchange.
2280 * After terminating the exchange the LLDD will call the transport's
2281 * normal io done path for the request, but it will have an aborted
2282 * status. The done path will return the io request back to the block
2283 * layer with an error status.
2284 */
2285static void
2286nvme_fc_terminate_exchange(struct request *req, void *data, bool reserved)
2287{
2288 struct nvme_ctrl *nctrl = data;
2289 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2290 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
James Smart78a7ac22017-04-23 08:30:07 -07002291 unsigned long flags;
2292 int status;
James Smarte3994412016-12-02 00:28:42 -08002293
2294 if (!blk_mq_request_started(req))
2295 return;
2296
James Smart78a7ac22017-04-23 08:30:07 -07002297 spin_lock_irqsave(&ctrl->lock, flags);
James Smart61bff8e2017-04-23 08:30:08 -07002298 if (ctrl->flags & FCCTRL_TERMIO) {
2299 ctrl->iocnt++;
2300 op->flags |= FCOP_FLAGS_TERMIO;
2301 }
James Smart78a7ac22017-04-23 08:30:07 -07002302 spin_unlock_irqrestore(&ctrl->lock, flags);
James Smarte3994412016-12-02 00:28:42 -08002303
James Smart78a7ac22017-04-23 08:30:07 -07002304 status = __nvme_fc_abort_op(ctrl, op);
2305 if (status) {
2306 /*
2307 * if __nvme_fc_abort_op failed the io wasn't
2308 * active. Thus this call path is running in
2309 * parallel to the io complete. Treat as non-error.
2310 */
2311
2312 /* back out the flags/counters */
2313 spin_lock_irqsave(&ctrl->lock, flags);
James Smart61bff8e2017-04-23 08:30:08 -07002314 if (ctrl->flags & FCCTRL_TERMIO)
2315 ctrl->iocnt--;
James Smart78a7ac22017-04-23 08:30:07 -07002316 op->flags &= ~FCOP_FLAGS_TERMIO;
2317 spin_unlock_irqrestore(&ctrl->lock, flags);
2318 return;
2319 }
2320}
James Smarte3994412016-12-02 00:28:42 -08002321
James Smarte3994412016-12-02 00:28:42 -08002322
James Smart61bff8e2017-04-23 08:30:08 -07002323static const struct blk_mq_ops nvme_fc_mq_ops = {
2324 .queue_rq = nvme_fc_queue_rq,
2325 .complete = nvme_fc_complete_rq,
2326 .init_request = nvme_fc_init_request,
2327 .exit_request = nvme_fc_exit_request,
James Smart61bff8e2017-04-23 08:30:08 -07002328 .init_hctx = nvme_fc_init_hctx,
2329 .poll = nvme_fc_poll,
2330 .timeout = nvme_fc_timeout,
James Smarte3994412016-12-02 00:28:42 -08002331};
2332
2333static int
2334nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl)
2335{
2336 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
Sagi Grimberg73141832017-06-29 11:16:49 +03002337 unsigned int nr_io_queues;
James Smarte3994412016-12-02 00:28:42 -08002338 int ret;
2339
Sagi Grimberg73141832017-06-29 11:16:49 +03002340 nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
2341 ctrl->lport->ops->max_hw_queues);
2342 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
James Smarte3994412016-12-02 00:28:42 -08002343 if (ret) {
2344 dev_info(ctrl->ctrl.device,
2345 "set_queue_count failed: %d\n", ret);
2346 return ret;
2347 }
2348
Sagi Grimberg73141832017-06-29 11:16:49 +03002349 ctrl->ctrl.queue_count = nr_io_queues + 1;
2350 if (!nr_io_queues)
James Smarte3994412016-12-02 00:28:42 -08002351 return 0;
2352
James Smarte3994412016-12-02 00:28:42 -08002353 nvme_fc_init_io_queues(ctrl);
2354
2355 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
2356 ctrl->tag_set.ops = &nvme_fc_mq_ops;
2357 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
2358 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
2359 ctrl->tag_set.numa_node = NUMA_NO_NODE;
2360 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
2361 ctrl->tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) +
2362 (SG_CHUNK_SIZE *
2363 sizeof(struct scatterlist)) +
2364 ctrl->lport->ops->fcprqst_priv_sz;
2365 ctrl->tag_set.driver_data = ctrl;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002366 ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1;
James Smarte3994412016-12-02 00:28:42 -08002367 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
2368
2369 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
2370 if (ret)
2371 return ret;
2372
2373 ctrl->ctrl.tagset = &ctrl->tag_set;
2374
2375 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
2376 if (IS_ERR(ctrl->ctrl.connect_q)) {
2377 ret = PTR_ERR(ctrl->ctrl.connect_q);
2378 goto out_free_tag_set;
2379 }
2380
2381 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
2382 if (ret)
2383 goto out_cleanup_blk_queue;
2384
2385 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
2386 if (ret)
2387 goto out_delete_hw_queues;
2388
2389 return 0;
2390
2391out_delete_hw_queues:
2392 nvme_fc_delete_hw_io_queues(ctrl);
2393out_cleanup_blk_queue:
James Smarte3994412016-12-02 00:28:42 -08002394 blk_cleanup_queue(ctrl->ctrl.connect_q);
2395out_free_tag_set:
2396 blk_mq_free_tag_set(&ctrl->tag_set);
2397 nvme_fc_free_io_queues(ctrl);
2398
2399 /* force put free routine to ignore io queues */
2400 ctrl->ctrl.tagset = NULL;
2401
2402 return ret;
2403}
2404
James Smart61bff8e2017-04-23 08:30:08 -07002405static int
2406nvme_fc_reinit_io_queues(struct nvme_fc_ctrl *ctrl)
2407{
2408 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
Sagi Grimberg73141832017-06-29 11:16:49 +03002409 unsigned int nr_io_queues;
James Smart61bff8e2017-04-23 08:30:08 -07002410 int ret;
2411
Sagi Grimberg73141832017-06-29 11:16:49 +03002412 nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
2413 ctrl->lport->ops->max_hw_queues);
2414 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
James Smart61bff8e2017-04-23 08:30:08 -07002415 if (ret) {
2416 dev_info(ctrl->ctrl.device,
2417 "set_queue_count failed: %d\n", ret);
2418 return ret;
2419 }
2420
Sagi Grimberg73141832017-06-29 11:16:49 +03002421 ctrl->ctrl.queue_count = nr_io_queues + 1;
James Smart61bff8e2017-04-23 08:30:08 -07002422 /* check for io queues existing */
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002423 if (ctrl->ctrl.queue_count == 1)
James Smart61bff8e2017-04-23 08:30:08 -07002424 return 0;
2425
James Smart61bff8e2017-04-23 08:30:08 -07002426 nvme_fc_init_io_queues(ctrl);
2427
Sagi Grimberg31b84462017-10-11 12:53:07 +03002428 ret = nvme_reinit_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
James Smart61bff8e2017-04-23 08:30:08 -07002429 if (ret)
2430 goto out_free_io_queues;
2431
2432 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
2433 if (ret)
2434 goto out_free_io_queues;
2435
2436 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
2437 if (ret)
2438 goto out_delete_hw_queues;
2439
Sagi Grimbergcda5fd12017-06-29 11:20:10 +03002440 blk_mq_update_nr_hw_queues(&ctrl->tag_set, nr_io_queues);
2441
James Smart61bff8e2017-04-23 08:30:08 -07002442 return 0;
2443
2444out_delete_hw_queues:
2445 nvme_fc_delete_hw_io_queues(ctrl);
2446out_free_io_queues:
2447 nvme_fc_free_io_queues(ctrl);
2448 return ret;
2449}
2450
2451/*
2452 * This routine restarts the controller on the host side, and
2453 * on the link side, recreates the controller association.
2454 */
2455static int
2456nvme_fc_create_association(struct nvme_fc_ctrl *ctrl)
2457{
2458 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
James Smart61bff8e2017-04-23 08:30:08 -07002459 int ret;
2460 bool changed;
2461
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +03002462 ++ctrl->ctrl.nr_reconnects;
James Smart61bff8e2017-04-23 08:30:08 -07002463
James Smart96e24802017-10-25 16:43:16 -07002464 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
2465 return -ENODEV;
2466
James Smart61bff8e2017-04-23 08:30:08 -07002467 /*
2468 * Create the admin queue
2469 */
2470
2471 nvme_fc_init_queue(ctrl, 0, NVME_FC_AQ_BLKMQ_DEPTH);
2472
2473 ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0,
2474 NVME_FC_AQ_BLKMQ_DEPTH);
2475 if (ret)
2476 goto out_free_queue;
2477
2478 ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0],
2479 NVME_FC_AQ_BLKMQ_DEPTH,
2480 (NVME_FC_AQ_BLKMQ_DEPTH / 4));
2481 if (ret)
2482 goto out_delete_hw_queue;
2483
2484 if (ctrl->ctrl.state != NVME_CTRL_NEW)
Sagi Grimbergf9c5af52017-07-02 15:39:34 +03002485 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
James Smart61bff8e2017-04-23 08:30:08 -07002486
2487 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
2488 if (ret)
2489 goto out_disconnect_admin_queue;
2490
2491 /*
2492 * Check controller capabilities
2493 *
2494 * todo:- add code to check if ctrl attributes changed from
2495 * prior connection values
2496 */
2497
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03002498 ret = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->ctrl.cap);
James Smart61bff8e2017-04-23 08:30:08 -07002499 if (ret) {
2500 dev_err(ctrl->ctrl.device,
2501 "prop_get NVME_REG_CAP failed\n");
2502 goto out_disconnect_admin_queue;
2503 }
2504
2505 ctrl->ctrl.sqsize =
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03002506 min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap) + 1, ctrl->ctrl.sqsize);
James Smart61bff8e2017-04-23 08:30:08 -07002507
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03002508 ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
James Smart61bff8e2017-04-23 08:30:08 -07002509 if (ret)
2510 goto out_disconnect_admin_queue;
2511
James Smartecad0d22017-10-23 15:11:36 -07002512 ctrl->ctrl.max_hw_sectors =
2513 (ctrl->lport->ops->max_sgl_segments - 1) << (PAGE_SHIFT - 9);
James Smart61bff8e2017-04-23 08:30:08 -07002514
2515 ret = nvme_init_identify(&ctrl->ctrl);
2516 if (ret)
2517 goto out_disconnect_admin_queue;
2518
2519 /* sanity checks */
2520
2521 /* FC-NVME does not have other data in the capsule */
2522 if (ctrl->ctrl.icdoff) {
2523 dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n",
2524 ctrl->ctrl.icdoff);
2525 goto out_disconnect_admin_queue;
2526 }
2527
James Smart61bff8e2017-04-23 08:30:08 -07002528 /* FC-NVME supports normal SGL Data Block Descriptors */
2529
2530 if (opts->queue_size > ctrl->ctrl.maxcmd) {
2531 /* warn if maxcmd is lower than queue_size */
2532 dev_warn(ctrl->ctrl.device,
2533 "queue_size %zu > ctrl maxcmd %u, reducing "
2534 "to queue_size\n",
2535 opts->queue_size, ctrl->ctrl.maxcmd);
2536 opts->queue_size = ctrl->ctrl.maxcmd;
2537 }
2538
2539 ret = nvme_fc_init_aen_ops(ctrl);
2540 if (ret)
2541 goto out_term_aen_ops;
2542
2543 /*
2544 * Create the io queues
2545 */
2546
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002547 if (ctrl->ctrl.queue_count > 1) {
James Smart61bff8e2017-04-23 08:30:08 -07002548 if (ctrl->ctrl.state == NVME_CTRL_NEW)
2549 ret = nvme_fc_create_io_queues(ctrl);
2550 else
2551 ret = nvme_fc_reinit_io_queues(ctrl);
2552 if (ret)
2553 goto out_term_aen_ops;
2554 }
2555
2556 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
James Smart61bff8e2017-04-23 08:30:08 -07002557
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +03002558 ctrl->ctrl.nr_reconnects = 0;
James Smart61bff8e2017-04-23 08:30:08 -07002559
James Smart44c6ec72017-10-25 16:43:14 -07002560 if (changed)
2561 nvme_start_ctrl(&ctrl->ctrl);
James Smart61bff8e2017-04-23 08:30:08 -07002562
2563 return 0; /* Success */
2564
2565out_term_aen_ops:
2566 nvme_fc_term_aen_ops(ctrl);
James Smart61bff8e2017-04-23 08:30:08 -07002567out_disconnect_admin_queue:
2568 /* send a Disconnect(association) LS to fc-nvme target */
2569 nvme_fc_xmt_disconnect_assoc(ctrl);
2570out_delete_hw_queue:
2571 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
2572out_free_queue:
2573 nvme_fc_free_queue(&ctrl->queues[0]);
2574
2575 return ret;
2576}
2577
2578/*
2579 * This routine stops operation of the controller on the host side.
2580 * On the host os stack side: Admin and IO queues are stopped,
2581 * outstanding ios on them terminated via FC ABTS.
2582 * On the link side: the association is terminated.
2583 */
2584static void
2585nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
2586{
2587 unsigned long flags;
2588
James Smart61bff8e2017-04-23 08:30:08 -07002589 spin_lock_irqsave(&ctrl->lock, flags);
2590 ctrl->flags |= FCCTRL_TERMIO;
2591 ctrl->iocnt = 0;
2592 spin_unlock_irqrestore(&ctrl->lock, flags);
2593
2594 /*
2595 * If io queues are present, stop them and terminate all outstanding
2596 * ios on them. As FC allocates FC exchange for each io, the
2597 * transport must contact the LLDD to terminate the exchange,
2598 * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
2599 * to tell us what io's are busy and invoke a transport routine
2600 * to kill them with the LLDD. After terminating the exchange
2601 * the LLDD will call the transport's normal io done path, but it
2602 * will have an aborted status. The done path will return the
2603 * io requests back to the block layer as part of normal completions
2604 * (but with error status).
2605 */
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002606 if (ctrl->ctrl.queue_count > 1) {
James Smart61bff8e2017-04-23 08:30:08 -07002607 nvme_stop_queues(&ctrl->ctrl);
2608 blk_mq_tagset_busy_iter(&ctrl->tag_set,
2609 nvme_fc_terminate_exchange, &ctrl->ctrl);
2610 }
2611
2612 /*
2613 * Other transports, which don't have link-level contexts bound
2614 * to sqe's, would try to gracefully shutdown the controller by
2615 * writing the registers for shutdown and polling (call
2616 * nvme_shutdown_ctrl()). Given a bunch of i/o was potentially
2617 * just aborted and we will wait on those contexts, and given
2618 * there was no indication of how live the controlelr is on the
2619 * link, don't send more io to create more contexts for the
2620 * shutdown. Let the controller fail via keepalive failure if
2621 * its still present.
2622 */
2623
2624 /*
2625 * clean up the admin queue. Same thing as above.
2626 * use blk_mq_tagset_busy_itr() and the transport routine to
2627 * terminate the exchanges.
2628 */
James Smart44c6ec72017-10-25 16:43:14 -07002629 if (ctrl->ctrl.state != NVME_CTRL_NEW)
2630 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
James Smart61bff8e2017-04-23 08:30:08 -07002631 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
2632 nvme_fc_terminate_exchange, &ctrl->ctrl);
2633
2634 /* kill the aens as they are a separate path */
2635 nvme_fc_abort_aen_ops(ctrl);
2636
2637 /* wait for all io that had to be aborted */
2638 spin_lock_irqsave(&ctrl->lock, flags);
James Smart36715cf2017-05-22 15:28:42 -07002639 wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock);
James Smart61bff8e2017-04-23 08:30:08 -07002640 ctrl->flags &= ~FCCTRL_TERMIO;
2641 spin_unlock_irqrestore(&ctrl->lock, flags);
2642
2643 nvme_fc_term_aen_ops(ctrl);
2644
2645 /*
2646 * send a Disconnect(association) LS to fc-nvme target
2647 * Note: could have been sent at top of process, but
2648 * cleaner on link traffic if after the aborts complete.
2649 * Note: if association doesn't exist, association_id will be 0
2650 */
2651 if (ctrl->association_id)
2652 nvme_fc_xmt_disconnect_assoc(ctrl);
2653
2654 if (ctrl->ctrl.tagset) {
2655 nvme_fc_delete_hw_io_queues(ctrl);
2656 nvme_fc_free_io_queues(ctrl);
2657 }
2658
2659 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
2660 nvme_fc_free_queue(&ctrl->queues[0]);
2661}
2662
2663static void
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002664nvme_fc_delete_ctrl(struct nvme_ctrl *nctrl)
James Smart61bff8e2017-04-23 08:30:08 -07002665{
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002666 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
James Smart61bff8e2017-04-23 08:30:08 -07002667
James Smart61bff8e2017-04-23 08:30:08 -07002668 cancel_delayed_work_sync(&ctrl->connect_work);
James Smart61bff8e2017-04-23 08:30:08 -07002669 /*
2670 * kill the association on the link side. this will block
2671 * waiting for io to terminate
2672 */
2673 nvme_fc_delete_association(ctrl);
James Smart61bff8e2017-04-23 08:30:08 -07002674}
2675
James Smart61bff8e2017-04-23 08:30:08 -07002676static void
James Smart5bbecdb2017-05-15 17:10:16 -07002677nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status)
2678{
James Smart44c6ec72017-10-25 16:43:14 -07002679 if (ctrl->ctrl.state != NVME_CTRL_RECONNECTING)
James Smart5bbecdb2017-05-15 17:10:16 -07002680 return;
James Smart5bbecdb2017-05-15 17:10:16 -07002681
James Smart589ff772017-05-15 17:10:19 -07002682 dev_info(ctrl->ctrl.device,
James Smart5bbecdb2017-05-15 17:10:16 -07002683 "NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
2684 ctrl->cnum, status);
2685
2686 if (nvmf_should_reconnect(&ctrl->ctrl)) {
James Smart96e24802017-10-25 16:43:16 -07002687 /* Only schedule the reconnect if the remote port is online */
2688 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
2689 return;
2690
James Smart5bbecdb2017-05-15 17:10:16 -07002691 dev_info(ctrl->ctrl.device,
2692 "NVME-FC{%d}: Reconnect attempt in %d seconds.\n",
2693 ctrl->cnum, ctrl->ctrl.opts->reconnect_delay);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002694 queue_delayed_work(nvme_wq, &ctrl->connect_work,
James Smart5bbecdb2017-05-15 17:10:16 -07002695 ctrl->ctrl.opts->reconnect_delay * HZ);
2696 } else {
James Smart589ff772017-05-15 17:10:19 -07002697 dev_warn(ctrl->ctrl.device,
James Smart5bbecdb2017-05-15 17:10:16 -07002698 "NVME-FC{%d}: Max reconnect attempts (%d) "
2699 "reached. Removing controller\n",
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +03002700 ctrl->cnum, ctrl->ctrl.nr_reconnects);
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002701 WARN_ON(nvme_delete_ctrl(&ctrl->ctrl));
James Smart5bbecdb2017-05-15 17:10:16 -07002702 }
2703}
2704
2705static void
James Smart61bff8e2017-04-23 08:30:08 -07002706nvme_fc_reset_ctrl_work(struct work_struct *work)
2707{
2708 struct nvme_fc_ctrl *ctrl =
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002709 container_of(work, struct nvme_fc_ctrl, ctrl.reset_work);
James Smart61bff8e2017-04-23 08:30:08 -07002710 int ret;
2711
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002712 nvme_stop_ctrl(&ctrl->ctrl);
James Smart44c6ec72017-10-25 16:43:14 -07002713
James Smart61bff8e2017-04-23 08:30:08 -07002714 /* will block will waiting for io to terminate */
2715 nvme_fc_delete_association(ctrl);
2716
James Smart44c6ec72017-10-25 16:43:14 -07002717 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING)) {
2718 dev_err(ctrl->ctrl.device,
2719 "NVME-FC{%d}: error_recovery: Couldn't change state "
2720 "to RECONNECTING\n", ctrl->cnum);
2721 return;
2722 }
2723
James Smart96e24802017-10-25 16:43:16 -07002724 if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE) {
2725 ret = nvme_fc_create_association(ctrl);
2726 if (ret)
2727 nvme_fc_reconnect_or_delete(ctrl, ret);
2728 else
2729 dev_info(ctrl->ctrl.device,
2730 "NVME-FC{%d}: controller reset complete\n",
2731 ctrl->cnum);
2732 }
James Smart61bff8e2017-04-23 08:30:08 -07002733}
2734
James Smart61bff8e2017-04-23 08:30:08 -07002735static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
2736 .name = "fc",
2737 .module = THIS_MODULE,
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02002738 .flags = NVME_F_FABRICS,
James Smart61bff8e2017-04-23 08:30:08 -07002739 .reg_read32 = nvmf_reg_read32,
2740 .reg_read64 = nvmf_reg_read64,
2741 .reg_write32 = nvmf_reg_write32,
James Smart61bff8e2017-04-23 08:30:08 -07002742 .free_ctrl = nvme_fc_nvme_ctrl_freed,
2743 .submit_async_event = nvme_fc_submit_async_event,
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002744 .delete_ctrl = nvme_fc_delete_ctrl,
James Smart61bff8e2017-04-23 08:30:08 -07002745 .get_address = nvmf_get_address,
Sagi Grimberg31b84462017-10-11 12:53:07 +03002746 .reinit_request = nvme_fc_reinit_request,
James Smart61bff8e2017-04-23 08:30:08 -07002747};
2748
2749static void
2750nvme_fc_connect_ctrl_work(struct work_struct *work)
2751{
2752 int ret;
2753
2754 struct nvme_fc_ctrl *ctrl =
2755 container_of(to_delayed_work(work),
2756 struct nvme_fc_ctrl, connect_work);
2757
2758 ret = nvme_fc_create_association(ctrl);
James Smart5bbecdb2017-05-15 17:10:16 -07002759 if (ret)
2760 nvme_fc_reconnect_or_delete(ctrl, ret);
2761 else
James Smart61bff8e2017-04-23 08:30:08 -07002762 dev_info(ctrl->ctrl.device,
2763 "NVME-FC{%d}: controller reconnect complete\n",
2764 ctrl->cnum);
2765}
2766
2767
2768static const struct blk_mq_ops nvme_fc_admin_mq_ops = {
2769 .queue_rq = nvme_fc_queue_rq,
2770 .complete = nvme_fc_complete_rq,
Christoph Hellwig76f983c2017-06-13 09:15:20 +02002771 .init_request = nvme_fc_init_request,
James Smart61bff8e2017-04-23 08:30:08 -07002772 .exit_request = nvme_fc_exit_request,
James Smart61bff8e2017-04-23 08:30:08 -07002773 .init_hctx = nvme_fc_init_admin_hctx,
2774 .timeout = nvme_fc_timeout,
2775};
2776
James Smarte3994412016-12-02 00:28:42 -08002777
James Smart56d5f4f2017-10-20 16:17:08 -07002778/*
2779 * Fails a controller request if it matches an existing controller
2780 * (association) with the same tuple:
2781 * <Host NQN, Host ID, local FC port, remote FC port, SUBSYS NQN>
2782 *
2783 * The ports don't need to be compared as they are intrinsically
2784 * already matched by the port pointers supplied.
2785 */
2786static bool
2787nvme_fc_existing_controller(struct nvme_fc_rport *rport,
2788 struct nvmf_ctrl_options *opts)
2789{
2790 struct nvme_fc_ctrl *ctrl;
2791 unsigned long flags;
2792 bool found = false;
2793
2794 spin_lock_irqsave(&rport->lock, flags);
2795 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
2796 found = nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts);
2797 if (found)
2798 break;
2799 }
2800 spin_unlock_irqrestore(&rport->lock, flags);
2801
2802 return found;
2803}
2804
James Smarte3994412016-12-02 00:28:42 -08002805static struct nvme_ctrl *
James Smart61bff8e2017-04-23 08:30:08 -07002806nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
James Smarte3994412016-12-02 00:28:42 -08002807 struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
2808{
2809 struct nvme_fc_ctrl *ctrl;
2810 unsigned long flags;
2811 int ret, idx;
James Smarte3994412016-12-02 00:28:42 -08002812
James Smart85e6a6a2017-05-05 16:13:15 -07002813 if (!(rport->remoteport.port_role &
2814 (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) {
2815 ret = -EBADR;
2816 goto out_fail;
2817 }
2818
James Smart56d5f4f2017-10-20 16:17:08 -07002819 if (!opts->duplicate_connect &&
2820 nvme_fc_existing_controller(rport, opts)) {
2821 ret = -EALREADY;
2822 goto out_fail;
2823 }
2824
James Smarte3994412016-12-02 00:28:42 -08002825 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
2826 if (!ctrl) {
2827 ret = -ENOMEM;
2828 goto out_fail;
2829 }
2830
2831 idx = ida_simple_get(&nvme_fc_ctrl_cnt, 0, 0, GFP_KERNEL);
2832 if (idx < 0) {
2833 ret = -ENOSPC;
2834 goto out_free_ctrl;
2835 }
2836
2837 ctrl->ctrl.opts = opts;
2838 INIT_LIST_HEAD(&ctrl->ctrl_list);
James Smarte3994412016-12-02 00:28:42 -08002839 ctrl->lport = lport;
2840 ctrl->rport = rport;
2841 ctrl->dev = lport->dev;
James Smarte3994412016-12-02 00:28:42 -08002842 ctrl->cnum = idx;
2843
James Smarte3994412016-12-02 00:28:42 -08002844 get_device(ctrl->dev);
2845 kref_init(&ctrl->ref);
2846
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002847 INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work);
James Smart61bff8e2017-04-23 08:30:08 -07002848 INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
James Smarte3994412016-12-02 00:28:42 -08002849 spin_lock_init(&ctrl->lock);
2850
2851 /* io queue count */
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002852 ctrl->ctrl.queue_count = min_t(unsigned int,
James Smarte3994412016-12-02 00:28:42 -08002853 opts->nr_io_queues,
2854 lport->ops->max_hw_queues);
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002855 ctrl->ctrl.queue_count++; /* +1 for admin queue */
James Smarte3994412016-12-02 00:28:42 -08002856
2857 ctrl->ctrl.sqsize = opts->queue_size - 1;
2858 ctrl->ctrl.kato = opts->kato;
2859
2860 ret = -ENOMEM;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002861 ctrl->queues = kcalloc(ctrl->ctrl.queue_count,
2862 sizeof(struct nvme_fc_queue), GFP_KERNEL);
James Smarte3994412016-12-02 00:28:42 -08002863 if (!ctrl->queues)
James Smart61bff8e2017-04-23 08:30:08 -07002864 goto out_free_ida;
James Smarte3994412016-12-02 00:28:42 -08002865
James Smart61bff8e2017-04-23 08:30:08 -07002866 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
2867 ctrl->admin_tag_set.ops = &nvme_fc_admin_mq_ops;
2868 ctrl->admin_tag_set.queue_depth = NVME_FC_AQ_BLKMQ_DEPTH;
2869 ctrl->admin_tag_set.reserved_tags = 2; /* fabric connect + Keep-Alive */
2870 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
2871 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) +
2872 (SG_CHUNK_SIZE *
2873 sizeof(struct scatterlist)) +
2874 ctrl->lport->ops->fcprqst_priv_sz;
2875 ctrl->admin_tag_set.driver_data = ctrl;
2876 ctrl->admin_tag_set.nr_hw_queues = 1;
2877 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
Israel Rukshin5a22e2b2017-10-18 12:38:25 +00002878 ctrl->admin_tag_set.flags = BLK_MQ_F_NO_SCHED;
James Smart61bff8e2017-04-23 08:30:08 -07002879
2880 ret = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
James Smarte3994412016-12-02 00:28:42 -08002881 if (ret)
James Smart61bff8e2017-04-23 08:30:08 -07002882 goto out_free_queues;
Sagi Grimberg34b6c232017-07-10 09:22:29 +03002883 ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set;
James Smarte3994412016-12-02 00:28:42 -08002884
James Smart61bff8e2017-04-23 08:30:08 -07002885 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
2886 if (IS_ERR(ctrl->ctrl.admin_q)) {
2887 ret = PTR_ERR(ctrl->ctrl.admin_q);
2888 goto out_free_admin_tag_set;
James Smarte3994412016-12-02 00:28:42 -08002889 }
2890
James Smart61bff8e2017-04-23 08:30:08 -07002891 /*
2892 * Would have been nice to init io queues tag set as well.
2893 * However, we require interaction from the controller
2894 * for max io queue count before we can do so.
2895 * Defer this to the connect path.
2896 */
James Smarte3994412016-12-02 00:28:42 -08002897
James Smart61bff8e2017-04-23 08:30:08 -07002898 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
James Smarte3994412016-12-02 00:28:42 -08002899 if (ret)
James Smart61bff8e2017-04-23 08:30:08 -07002900 goto out_cleanup_admin_q;
James Smarte3994412016-12-02 00:28:42 -08002901
James Smart61bff8e2017-04-23 08:30:08 -07002902 /* at this point, teardown path changes to ref counting on nvme ctrl */
James Smarte3994412016-12-02 00:28:42 -08002903
2904 spin_lock_irqsave(&rport->lock, flags);
2905 list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
2906 spin_unlock_irqrestore(&rport->lock, flags);
2907
James Smart61bff8e2017-04-23 08:30:08 -07002908 ret = nvme_fc_create_association(ctrl);
2909 if (ret) {
Ewan D. Milnede414472017-04-24 13:24:16 -04002910 ctrl->ctrl.opts = NULL;
James Smart61bff8e2017-04-23 08:30:08 -07002911 /* initiate nvme ctrl ref counting teardown */
2912 nvme_uninit_ctrl(&ctrl->ctrl);
James Smart24b7f052017-06-05 15:03:42 -07002913 nvme_put_ctrl(&ctrl->ctrl);
James Smart61bff8e2017-04-23 08:30:08 -07002914
James Smart0b5a7662017-06-15 23:40:54 -07002915 /* Remove core ctrl ref. */
2916 nvme_put_ctrl(&ctrl->ctrl);
2917
James Smart61bff8e2017-04-23 08:30:08 -07002918 /* as we're past the point where we transition to the ref
2919 * counting teardown path, if we return a bad pointer here,
2920 * the calling routine, thinking it's prior to the
2921 * transition, will do an rport put. Since the teardown
2922 * path also does a rport put, we do an extra get here to
2923 * so proper order/teardown happens.
2924 */
2925 nvme_fc_rport_get(rport);
2926
2927 if (ret > 0)
2928 ret = -EIO;
2929 return ERR_PTR(ret);
James Smarte3994412016-12-02 00:28:42 -08002930 }
2931
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002932 nvme_get_ctrl(&ctrl->ctrl);
James Smart2cb657b2017-05-15 17:10:22 -07002933
James Smart61bff8e2017-04-23 08:30:08 -07002934 dev_info(ctrl->ctrl.device,
2935 "NVME-FC{%d}: new ctrl: NQN \"%s\"\n",
2936 ctrl->cnum, ctrl->ctrl.opts->subsysnqn);
2937
James Smarte3994412016-12-02 00:28:42 -08002938 return &ctrl->ctrl;
2939
James Smart61bff8e2017-04-23 08:30:08 -07002940out_cleanup_admin_q:
2941 blk_cleanup_queue(ctrl->ctrl.admin_q);
2942out_free_admin_tag_set:
2943 blk_mq_free_tag_set(&ctrl->admin_tag_set);
2944out_free_queues:
2945 kfree(ctrl->queues);
James Smarte3994412016-12-02 00:28:42 -08002946out_free_ida:
James Smart61bff8e2017-04-23 08:30:08 -07002947 put_device(ctrl->dev);
James Smarte3994412016-12-02 00:28:42 -08002948 ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
2949out_free_ctrl:
2950 kfree(ctrl);
2951out_fail:
James Smarte3994412016-12-02 00:28:42 -08002952 /* exit via here doesn't follow ctlr ref points */
2953 return ERR_PTR(ret);
2954}
2955
James Smarte3994412016-12-02 00:28:42 -08002956
2957struct nvmet_fc_traddr {
2958 u64 nn;
2959 u64 pn;
2960};
2961
James Smarte3994412016-12-02 00:28:42 -08002962static int
James Smart9c5358e2017-07-17 13:59:39 -07002963__nvme_fc_parse_u64(substring_t *sstr, u64 *val)
James Smarte3994412016-12-02 00:28:42 -08002964{
James Smarte3994412016-12-02 00:28:42 -08002965 u64 token64;
2966
James Smart9c5358e2017-07-17 13:59:39 -07002967 if (match_u64(sstr, &token64))
2968 return -EINVAL;
2969 *val = token64;
James Smarte3994412016-12-02 00:28:42 -08002970
James Smart9c5358e2017-07-17 13:59:39 -07002971 return 0;
2972}
James Smarte3994412016-12-02 00:28:42 -08002973
James Smart9c5358e2017-07-17 13:59:39 -07002974/*
2975 * This routine validates and extracts the WWN's from the TRADDR string.
2976 * As kernel parsers need the 0x to determine number base, universally
2977 * build string to parse with 0x prefix before parsing name strings.
2978 */
2979static int
2980nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen)
2981{
2982 char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1];
2983 substring_t wwn = { name, &name[sizeof(name)-1] };
2984 int nnoffset, pnoffset;
James Smarte3994412016-12-02 00:28:42 -08002985
James Smart9c5358e2017-07-17 13:59:39 -07002986 /* validate it string one of the 2 allowed formats */
2987 if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH &&
2988 !strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) &&
2989 !strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET],
2990 "pn-0x", NVME_FC_TRADDR_OXNNLEN)) {
2991 nnoffset = NVME_FC_TRADDR_OXNNLEN;
2992 pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET +
2993 NVME_FC_TRADDR_OXNNLEN;
2994 } else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH &&
2995 !strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) &&
2996 !strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET],
2997 "pn-", NVME_FC_TRADDR_NNLEN))) {
2998 nnoffset = NVME_FC_TRADDR_NNLEN;
2999 pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN;
3000 } else
3001 goto out_einval;
3002
3003 name[0] = '0';
3004 name[1] = 'x';
3005 name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0;
3006
3007 memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3008 if (__nvme_fc_parse_u64(&wwn, &traddr->nn))
3009 goto out_einval;
3010
3011 memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3012 if (__nvme_fc_parse_u64(&wwn, &traddr->pn))
3013 goto out_einval;
3014
3015 return 0;
3016
3017out_einval:
3018 pr_warn("%s: bad traddr string\n", __func__);
3019 return -EINVAL;
James Smarte3994412016-12-02 00:28:42 -08003020}
3021
3022static struct nvme_ctrl *
3023nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
3024{
3025 struct nvme_fc_lport *lport;
3026 struct nvme_fc_rport *rport;
James Smart61bff8e2017-04-23 08:30:08 -07003027 struct nvme_ctrl *ctrl;
James Smarte3994412016-12-02 00:28:42 -08003028 struct nvmet_fc_traddr laddr = { 0L, 0L };
3029 struct nvmet_fc_traddr raddr = { 0L, 0L };
3030 unsigned long flags;
3031 int ret;
3032
James Smart9c5358e2017-07-17 13:59:39 -07003033 ret = nvme_fc_parse_traddr(&raddr, opts->traddr, NVMF_TRADDR_SIZE);
James Smarte3994412016-12-02 00:28:42 -08003034 if (ret || !raddr.nn || !raddr.pn)
3035 return ERR_PTR(-EINVAL);
3036
James Smart9c5358e2017-07-17 13:59:39 -07003037 ret = nvme_fc_parse_traddr(&laddr, opts->host_traddr, NVMF_TRADDR_SIZE);
James Smarte3994412016-12-02 00:28:42 -08003038 if (ret || !laddr.nn || !laddr.pn)
3039 return ERR_PTR(-EINVAL);
3040
3041 /* find the host and remote ports to connect together */
3042 spin_lock_irqsave(&nvme_fc_lock, flags);
3043 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3044 if (lport->localport.node_name != laddr.nn ||
3045 lport->localport.port_name != laddr.pn)
3046 continue;
3047
3048 list_for_each_entry(rport, &lport->endp_list, endp_list) {
3049 if (rport->remoteport.node_name != raddr.nn ||
3050 rport->remoteport.port_name != raddr.pn)
3051 continue;
3052
3053 /* if fail to get reference fall through. Will error */
3054 if (!nvme_fc_rport_get(rport))
3055 break;
3056
3057 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3058
James Smart61bff8e2017-04-23 08:30:08 -07003059 ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport);
3060 if (IS_ERR(ctrl))
3061 nvme_fc_rport_put(rport);
3062 return ctrl;
James Smarte3994412016-12-02 00:28:42 -08003063 }
3064 }
3065 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3066
3067 return ERR_PTR(-ENOENT);
3068}
3069
3070
3071static struct nvmf_transport_ops nvme_fc_transport = {
3072 .name = "fc",
3073 .required_opts = NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
James Smart5bbecdb2017-05-15 17:10:16 -07003074 .allowed_opts = NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO,
James Smarte3994412016-12-02 00:28:42 -08003075 .create_ctrl = nvme_fc_create_ctrl,
3076};
3077
3078static int __init nvme_fc_init_module(void)
3079{
James Smart5f568552017-09-14 10:38:41 -07003080 int ret;
3081
3082 /*
3083 * NOTE:
3084 * It is expected that in the future the kernel will combine
3085 * the FC-isms that are currently under scsi and now being
3086 * added to by NVME into a new standalone FC class. The SCSI
3087 * and NVME protocols and their devices would be under this
3088 * new FC class.
3089 *
3090 * As we need something to post FC-specific udev events to,
3091 * specifically for nvme probe events, start by creating the
3092 * new device class. When the new standalone FC class is
3093 * put in place, this code will move to a more generic
3094 * location for the class.
3095 */
3096 fc_class = class_create(THIS_MODULE, "fc");
3097 if (IS_ERR(fc_class)) {
3098 pr_err("couldn't register class fc\n");
3099 return PTR_ERR(fc_class);
3100 }
3101
3102 /*
3103 * Create a device for the FC-centric udev events
3104 */
3105 fc_udev_device = device_create(fc_class, NULL, MKDEV(0, 0), NULL,
3106 "fc_udev_device");
3107 if (IS_ERR(fc_udev_device)) {
3108 pr_err("couldn't create fc_udev device!\n");
3109 ret = PTR_ERR(fc_udev_device);
3110 goto out_destroy_class;
3111 }
3112
3113 ret = nvmf_register_transport(&nvme_fc_transport);
3114 if (ret)
3115 goto out_destroy_device;
3116
3117 return 0;
3118
3119out_destroy_device:
3120 device_destroy(fc_class, MKDEV(0, 0));
3121out_destroy_class:
3122 class_destroy(fc_class);
3123 return ret;
James Smarte3994412016-12-02 00:28:42 -08003124}
3125
3126static void __exit nvme_fc_exit_module(void)
3127{
3128 /* sanity check - all lports should be removed */
3129 if (!list_empty(&nvme_fc_lport_list))
3130 pr_warn("%s: localport list not empty\n", __func__);
3131
3132 nvmf_unregister_transport(&nvme_fc_transport);
3133
James Smarte3994412016-12-02 00:28:42 -08003134 ida_destroy(&nvme_fc_local_port_cnt);
3135 ida_destroy(&nvme_fc_ctrl_cnt);
James Smart5f568552017-09-14 10:38:41 -07003136
3137 device_destroy(fc_class, MKDEV(0, 0));
3138 class_destroy(fc_class);
James Smarte3994412016-12-02 00:28:42 -08003139}
3140
3141module_init(nvme_fc_init_module);
3142module_exit(nvme_fc_exit_module);
3143
3144MODULE_LICENSE("GPL v2");