blob: 7e3f3055a67770d52c1ad8d256ded06134819c57 [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),
Sagi Grimbergdb2044f2017-10-24 15:25:21 +030044 NVME_FC_Q_LIVE = (1 << 1),
James Smarte3994412016-12-02 00:28:42 -080045};
46
47#define NVMEFC_QUEUE_DELAY 3 /* ms units */
48
49struct nvme_fc_queue {
50 struct nvme_fc_ctrl *ctrl;
51 struct device *dev;
52 struct blk_mq_hw_ctx *hctx;
53 void *lldd_handle;
54 int queue_size;
55 size_t cmnd_capsule_len;
56 u32 qnum;
57 u32 rqcnt;
58 u32 seqno;
59
60 u64 connection_id;
61 atomic_t csn;
62
63 unsigned long flags;
64} __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
65
James Smart8d64daf2017-04-11 11:35:09 -070066enum nvme_fcop_flags {
67 FCOP_FLAGS_TERMIO = (1 << 0),
68 FCOP_FLAGS_RELEASED = (1 << 1),
69 FCOP_FLAGS_COMPLETE = (1 << 2),
James Smart78a7ac22017-04-23 08:30:07 -070070 FCOP_FLAGS_AEN = (1 << 3),
James Smart8d64daf2017-04-11 11:35:09 -070071};
72
James Smarte3994412016-12-02 00:28:42 -080073struct nvmefc_ls_req_op {
74 struct nvmefc_ls_req ls_req;
75
James Smartc913a8b2017-04-11 11:35:08 -070076 struct nvme_fc_rport *rport;
James Smarte3994412016-12-02 00:28:42 -080077 struct nvme_fc_queue *queue;
78 struct request *rq;
James Smart8d64daf2017-04-11 11:35:09 -070079 u32 flags;
James Smarte3994412016-12-02 00:28:42 -080080
81 int ls_error;
82 struct completion ls_done;
James Smartc913a8b2017-04-11 11:35:08 -070083 struct list_head lsreq_list; /* rport->ls_req_list */
James Smarte3994412016-12-02 00:28:42 -080084 bool req_queued;
85};
86
87enum nvme_fcpop_state {
88 FCPOP_STATE_UNINIT = 0,
89 FCPOP_STATE_IDLE = 1,
90 FCPOP_STATE_ACTIVE = 2,
91 FCPOP_STATE_ABORTED = 3,
James Smart78a7ac22017-04-23 08:30:07 -070092 FCPOP_STATE_COMPLETE = 4,
James Smarte3994412016-12-02 00:28:42 -080093};
94
95struct nvme_fc_fcp_op {
96 struct nvme_request nreq; /*
97 * nvme/host/core.c
98 * requires this to be
99 * the 1st element in the
100 * private structure
101 * associated with the
102 * request.
103 */
104 struct nvmefc_fcp_req fcp_req;
105
106 struct nvme_fc_ctrl *ctrl;
107 struct nvme_fc_queue *queue;
108 struct request *rq;
109
110 atomic_t state;
James Smart78a7ac22017-04-23 08:30:07 -0700111 u32 flags;
James Smarte3994412016-12-02 00:28:42 -0800112 u32 rqno;
113 u32 nents;
114
115 struct nvme_fc_cmd_iu cmd_iu;
116 struct nvme_fc_ersp_iu rsp_iu;
117};
118
119struct nvme_fc_lport {
120 struct nvme_fc_local_port localport;
121
122 struct ida endp_cnt;
123 struct list_head port_list; /* nvme_fc_port_list */
124 struct list_head endp_list;
125 struct device *dev; /* physical device for dma */
126 struct nvme_fc_port_template *ops;
127 struct kref ref;
128} __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
129
130struct nvme_fc_rport {
131 struct nvme_fc_remote_port remoteport;
132
133 struct list_head endp_list; /* for lport->endp_list */
134 struct list_head ctrl_list;
James Smartc913a8b2017-04-11 11:35:08 -0700135 struct list_head ls_req_list;
136 struct device *dev; /* physical device for dma */
137 struct nvme_fc_lport *lport;
James Smarte3994412016-12-02 00:28:42 -0800138 spinlock_t lock;
139 struct kref ref;
140} __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
141
James Smart61bff8e2017-04-23 08:30:08 -0700142enum nvme_fcctrl_flags {
143 FCCTRL_TERMIO = (1 << 0),
James Smarte3994412016-12-02 00:28:42 -0800144};
145
146struct nvme_fc_ctrl {
147 spinlock_t lock;
148 struct nvme_fc_queue *queues;
James Smarte3994412016-12-02 00:28:42 -0800149 struct device *dev;
150 struct nvme_fc_lport *lport;
151 struct nvme_fc_rport *rport;
152 u32 cnum;
153
154 u64 association_id;
155
James Smarte3994412016-12-02 00:28:42 -0800156 struct list_head ctrl_list; /* rport->ctrl_list */
James Smarte3994412016-12-02 00:28:42 -0800157
158 struct blk_mq_tag_set admin_tag_set;
159 struct blk_mq_tag_set tag_set;
160
161 struct work_struct delete_work;
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
217
218/* *********************** FC-NVME Port Management ************************ */
219
220static int __nvme_fc_del_ctrl(struct nvme_fc_ctrl *);
221static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *,
222 struct nvme_fc_queue *, unsigned int);
223
James Smart5533d422017-07-31 13:20:30 -0700224static void
225nvme_fc_free_lport(struct kref *ref)
226{
227 struct nvme_fc_lport *lport =
228 container_of(ref, struct nvme_fc_lport, ref);
229 unsigned long flags;
230
231 WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED);
232 WARN_ON(!list_empty(&lport->endp_list));
233
234 /* remove from transport list */
235 spin_lock_irqsave(&nvme_fc_lock, flags);
236 list_del(&lport->port_list);
237 spin_unlock_irqrestore(&nvme_fc_lock, flags);
238
239 /* let the LLDD know we've finished tearing it down */
240 lport->ops->localport_delete(&lport->localport);
241
242 ida_simple_remove(&nvme_fc_local_port_cnt, lport->localport.port_num);
243 ida_destroy(&lport->endp_cnt);
244
245 put_device(lport->dev);
246
247 kfree(lport);
248}
249
250static void
251nvme_fc_lport_put(struct nvme_fc_lport *lport)
252{
253 kref_put(&lport->ref, nvme_fc_free_lport);
254}
255
256static int
257nvme_fc_lport_get(struct nvme_fc_lport *lport)
258{
259 return kref_get_unless_zero(&lport->ref);
260}
261
262
263static struct nvme_fc_lport *
264nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info *pinfo)
265{
266 struct nvme_fc_lport *lport;
267 unsigned long flags;
268
269 spin_lock_irqsave(&nvme_fc_lock, flags);
270
271 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
272 if (lport->localport.node_name != pinfo->node_name ||
273 lport->localport.port_name != pinfo->port_name)
274 continue;
275
276 if (lport->localport.port_state != FC_OBJSTATE_DELETED) {
277 lport = ERR_PTR(-EEXIST);
278 goto out_done;
279 }
280
281 if (!nvme_fc_lport_get(lport)) {
282 /*
283 * fails if ref cnt already 0. If so,
284 * act as if lport already deleted
285 */
286 lport = NULL;
287 goto out_done;
288 }
289
290 /* resume the lport */
291
292 lport->localport.port_role = pinfo->port_role;
293 lport->localport.port_id = pinfo->port_id;
294 lport->localport.port_state = FC_OBJSTATE_ONLINE;
295
296 spin_unlock_irqrestore(&nvme_fc_lock, flags);
297
298 return lport;
299 }
300
301 lport = NULL;
302
303out_done:
304 spin_unlock_irqrestore(&nvme_fc_lock, flags);
305
306 return lport;
307}
James Smarte3994412016-12-02 00:28:42 -0800308
309/**
310 * nvme_fc_register_localport - transport entry point called by an
311 * LLDD to register the existence of a NVME
312 * host FC port.
313 * @pinfo: pointer to information about the port to be registered
314 * @template: LLDD entrypoints and operational parameters for the port
315 * @dev: physical hardware device node port corresponds to. Will be
316 * used for DMA mappings
317 * @lport_p: pointer to a local port pointer. Upon success, the routine
318 * will allocate a nvme_fc_local_port structure and place its
319 * address in the local port pointer. Upon failure, local port
320 * pointer will be set to 0.
321 *
322 * Returns:
323 * a completion status. Must be 0 upon success; a negative errno
324 * (ex: -ENXIO) upon failure.
325 */
326int
327nvme_fc_register_localport(struct nvme_fc_port_info *pinfo,
328 struct nvme_fc_port_template *template,
329 struct device *dev,
330 struct nvme_fc_local_port **portptr)
331{
332 struct nvme_fc_lport *newrec;
333 unsigned long flags;
334 int ret, idx;
335
336 if (!template->localport_delete || !template->remoteport_delete ||
337 !template->ls_req || !template->fcp_io ||
338 !template->ls_abort || !template->fcp_abort ||
339 !template->max_hw_queues || !template->max_sgl_segments ||
James Smart2e680192020-04-03 07:33:20 -0700340 !template->max_dif_sgl_segments || !template->dma_boundary) {
James Smarte3994412016-12-02 00:28:42 -0800341 ret = -EINVAL;
342 goto out_reghost_failed;
343 }
344
James Smart5533d422017-07-31 13:20:30 -0700345 /*
346 * look to see if there is already a localport that had been
347 * deregistered and in the process of waiting for all the
348 * references to fully be removed. If the references haven't
349 * expired, we can simply re-enable the localport. Remoteports
350 * and controller reconnections should resume naturally.
351 */
352 newrec = nvme_fc_attach_to_unreg_lport(pinfo);
353
354 /* found an lport, but something about its state is bad */
355 if (IS_ERR(newrec)) {
356 ret = PTR_ERR(newrec);
357 goto out_reghost_failed;
358
359 /* found existing lport, which was resumed */
360 } else if (newrec) {
361 *portptr = &newrec->localport;
362 return 0;
363 }
364
365 /* nothing found - allocate a new localport struct */
366
James Smarte3994412016-12-02 00:28:42 -0800367 newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz),
368 GFP_KERNEL);
369 if (!newrec) {
370 ret = -ENOMEM;
371 goto out_reghost_failed;
372 }
373
374 idx = ida_simple_get(&nvme_fc_local_port_cnt, 0, 0, GFP_KERNEL);
375 if (idx < 0) {
376 ret = -ENOSPC;
377 goto out_fail_kfree;
378 }
379
380 if (!get_device(dev) && dev) {
381 ret = -ENODEV;
382 goto out_ida_put;
383 }
384
385 INIT_LIST_HEAD(&newrec->port_list);
386 INIT_LIST_HEAD(&newrec->endp_list);
387 kref_init(&newrec->ref);
388 newrec->ops = template;
389 newrec->dev = dev;
390 ida_init(&newrec->endp_cnt);
391 newrec->localport.private = &newrec[1];
392 newrec->localport.node_name = pinfo->node_name;
393 newrec->localport.port_name = pinfo->port_name;
394 newrec->localport.port_role = pinfo->port_role;
395 newrec->localport.port_id = pinfo->port_id;
396 newrec->localport.port_state = FC_OBJSTATE_ONLINE;
397 newrec->localport.port_num = idx;
398
399 spin_lock_irqsave(&nvme_fc_lock, flags);
400 list_add_tail(&newrec->port_list, &nvme_fc_lport_list);
401 spin_unlock_irqrestore(&nvme_fc_lock, flags);
402
403 if (dev)
404 dma_set_seg_boundary(dev, template->dma_boundary);
405
406 *portptr = &newrec->localport;
407 return 0;
408
409out_ida_put:
410 ida_simple_remove(&nvme_fc_local_port_cnt, idx);
411out_fail_kfree:
412 kfree(newrec);
413out_reghost_failed:
414 *portptr = NULL;
415
416 return ret;
417}
418EXPORT_SYMBOL_GPL(nvme_fc_register_localport);
419
James Smarte3994412016-12-02 00:28:42 -0800420/**
421 * nvme_fc_unregister_localport - transport entry point called by an
422 * LLDD to deregister/remove a previously
423 * registered a NVME host FC port.
424 * @localport: pointer to the (registered) local port that is to be
425 * deregistered.
426 *
427 * Returns:
428 * a completion status. Must be 0 upon success; a negative errno
429 * (ex: -ENXIO) upon failure.
430 */
431int
432nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr)
433{
434 struct nvme_fc_lport *lport = localport_to_lport(portptr);
435 unsigned long flags;
436
437 if (!portptr)
438 return -EINVAL;
439
440 spin_lock_irqsave(&nvme_fc_lock, flags);
441
442 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
443 spin_unlock_irqrestore(&nvme_fc_lock, flags);
444 return -EINVAL;
445 }
446 portptr->port_state = FC_OBJSTATE_DELETED;
447
448 spin_unlock_irqrestore(&nvme_fc_lock, flags);
449
450 nvme_fc_lport_put(lport);
451
452 return 0;
453}
454EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport);
455
456/**
457 * nvme_fc_register_remoteport - transport entry point called by an
458 * LLDD to register the existence of a NVME
459 * subsystem FC port on its fabric.
460 * @localport: pointer to the (registered) local port that the remote
461 * subsystem port is connected to.
462 * @pinfo: pointer to information about the port to be registered
463 * @rport_p: pointer to a remote port pointer. Upon success, the routine
464 * will allocate a nvme_fc_remote_port structure and place its
465 * address in the remote port pointer. Upon failure, remote port
466 * pointer will be set to 0.
467 *
468 * Returns:
469 * a completion status. Must be 0 upon success; a negative errno
470 * (ex: -ENXIO) upon failure.
471 */
472int
473nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
474 struct nvme_fc_port_info *pinfo,
475 struct nvme_fc_remote_port **portptr)
476{
477 struct nvme_fc_lport *lport = localport_to_lport(localport);
478 struct nvme_fc_rport *newrec;
479 unsigned long flags;
480 int ret, idx;
481
482 newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz),
483 GFP_KERNEL);
484 if (!newrec) {
485 ret = -ENOMEM;
486 goto out_reghost_failed;
487 }
488
489 if (!nvme_fc_lport_get(lport)) {
490 ret = -ESHUTDOWN;
491 goto out_kfree_rport;
492 }
493
494 idx = ida_simple_get(&lport->endp_cnt, 0, 0, GFP_KERNEL);
495 if (idx < 0) {
496 ret = -ENOSPC;
497 goto out_lport_put;
498 }
499
500 INIT_LIST_HEAD(&newrec->endp_list);
501 INIT_LIST_HEAD(&newrec->ctrl_list);
James Smartc913a8b2017-04-11 11:35:08 -0700502 INIT_LIST_HEAD(&newrec->ls_req_list);
James Smarte3994412016-12-02 00:28:42 -0800503 kref_init(&newrec->ref);
504 spin_lock_init(&newrec->lock);
505 newrec->remoteport.localport = &lport->localport;
James Smartc913a8b2017-04-11 11:35:08 -0700506 newrec->dev = lport->dev;
507 newrec->lport = lport;
James Smarte3994412016-12-02 00:28:42 -0800508 newrec->remoteport.private = &newrec[1];
509 newrec->remoteport.port_role = pinfo->port_role;
510 newrec->remoteport.node_name = pinfo->node_name;
511 newrec->remoteport.port_name = pinfo->port_name;
512 newrec->remoteport.port_id = pinfo->port_id;
513 newrec->remoteport.port_state = FC_OBJSTATE_ONLINE;
514 newrec->remoteport.port_num = idx;
515
516 spin_lock_irqsave(&nvme_fc_lock, flags);
517 list_add_tail(&newrec->endp_list, &lport->endp_list);
518 spin_unlock_irqrestore(&nvme_fc_lock, flags);
519
520 *portptr = &newrec->remoteport;
521 return 0;
522
523out_lport_put:
524 nvme_fc_lport_put(lport);
525out_kfree_rport:
526 kfree(newrec);
527out_reghost_failed:
528 *portptr = NULL;
529 return ret;
James Smarte3994412016-12-02 00:28:42 -0800530}
531EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport);
532
533static void
534nvme_fc_free_rport(struct kref *ref)
535{
536 struct nvme_fc_rport *rport =
537 container_of(ref, struct nvme_fc_rport, ref);
538 struct nvme_fc_lport *lport =
539 localport_to_lport(rport->remoteport.localport);
540 unsigned long flags;
541
542 WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED);
543 WARN_ON(!list_empty(&rport->ctrl_list));
544
545 /* remove from lport list */
546 spin_lock_irqsave(&nvme_fc_lock, flags);
547 list_del(&rport->endp_list);
548 spin_unlock_irqrestore(&nvme_fc_lock, flags);
549
550 /* let the LLDD know we've finished tearing it down */
551 lport->ops->remoteport_delete(&rport->remoteport);
552
553 ida_simple_remove(&lport->endp_cnt, rport->remoteport.port_num);
554
555 kfree(rport);
556
557 nvme_fc_lport_put(lport);
558}
559
560static void
561nvme_fc_rport_put(struct nvme_fc_rport *rport)
562{
563 kref_put(&rport->ref, nvme_fc_free_rport);
564}
565
566static int
567nvme_fc_rport_get(struct nvme_fc_rport *rport)
568{
569 return kref_get_unless_zero(&rport->ref);
570}
571
James Smart8d64daf2017-04-11 11:35:09 -0700572static int
573nvme_fc_abort_lsops(struct nvme_fc_rport *rport)
574{
575 struct nvmefc_ls_req_op *lsop;
576 unsigned long flags;
577
578restart:
579 spin_lock_irqsave(&rport->lock, flags);
580
581 list_for_each_entry(lsop, &rport->ls_req_list, lsreq_list) {
582 if (!(lsop->flags & FCOP_FLAGS_TERMIO)) {
583 lsop->flags |= FCOP_FLAGS_TERMIO;
584 spin_unlock_irqrestore(&rport->lock, flags);
585 rport->lport->ops->ls_abort(&rport->lport->localport,
586 &rport->remoteport,
587 &lsop->ls_req);
588 goto restart;
589 }
590 }
591 spin_unlock_irqrestore(&rport->lock, flags);
592
593 return 0;
594}
595
James Smarte3994412016-12-02 00:28:42 -0800596/**
597 * nvme_fc_unregister_remoteport - transport entry point called by an
598 * LLDD to deregister/remove a previously
599 * registered a NVME subsystem FC port.
600 * @remoteport: pointer to the (registered) remote port that is to be
601 * deregistered.
602 *
603 * Returns:
604 * a completion status. Must be 0 upon success; a negative errno
605 * (ex: -ENXIO) upon failure.
606 */
607int
608nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr)
609{
610 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
611 struct nvme_fc_ctrl *ctrl;
612 unsigned long flags;
613
614 if (!portptr)
615 return -EINVAL;
616
617 spin_lock_irqsave(&rport->lock, flags);
618
619 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
620 spin_unlock_irqrestore(&rport->lock, flags);
621 return -EINVAL;
622 }
623 portptr->port_state = FC_OBJSTATE_DELETED;
624
625 /* tear down all associations to the remote port */
626 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list)
627 __nvme_fc_del_ctrl(ctrl);
628
629 spin_unlock_irqrestore(&rport->lock, flags);
630
James Smart8d64daf2017-04-11 11:35:09 -0700631 nvme_fc_abort_lsops(rport);
632
James Smarte3994412016-12-02 00:28:42 -0800633 nvme_fc_rport_put(rport);
634 return 0;
635}
636EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport);
637
638
639/* *********************** FC-NVME DMA Handling **************************** */
640
641/*
642 * The fcloop device passes in a NULL device pointer. Real LLD's will
643 * pass in a valid device pointer. If NULL is passed to the dma mapping
644 * routines, depending on the platform, it may or may not succeed, and
645 * may crash.
646 *
647 * As such:
648 * Wrapper all the dma routines and check the dev pointer.
649 *
650 * If simple mappings (return just a dma address, we'll noop them,
651 * returning a dma address of 0.
652 *
653 * On more complex mappings (dma_map_sg), a pseudo routine fills
654 * in the scatter list, setting all dma addresses to 0.
655 */
656
657static inline dma_addr_t
658fc_dma_map_single(struct device *dev, void *ptr, size_t size,
659 enum dma_data_direction dir)
660{
661 return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
662}
663
664static inline int
665fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
666{
667 return dev ? dma_mapping_error(dev, dma_addr) : 0;
668}
669
670static inline void
671fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
672 enum dma_data_direction dir)
673{
674 if (dev)
675 dma_unmap_single(dev, addr, size, dir);
676}
677
678static inline void
679fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
680 enum dma_data_direction dir)
681{
682 if (dev)
683 dma_sync_single_for_cpu(dev, addr, size, dir);
684}
685
686static inline void
687fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
688 enum dma_data_direction dir)
689{
690 if (dev)
691 dma_sync_single_for_device(dev, addr, size, dir);
692}
693
694/* pseudo dma_map_sg call */
695static int
696fc_map_sg(struct scatterlist *sg, int nents)
697{
698 struct scatterlist *s;
699 int i;
700
701 WARN_ON(nents == 0 || sg[0].length == 0);
702
703 for_each_sg(sg, s, nents, i) {
704 s->dma_address = 0L;
705#ifdef CONFIG_NEED_SG_DMA_LENGTH
706 s->dma_length = s->length;
707#endif
708 }
709 return nents;
710}
711
712static inline int
713fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
714 enum dma_data_direction dir)
715{
716 return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
717}
718
719static inline void
720fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
721 enum dma_data_direction dir)
722{
723 if (dev)
724 dma_unmap_sg(dev, sg, nents, dir);
725}
726
727
728/* *********************** FC-NVME LS Handling **************************** */
729
730static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
731static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
732
733
734static void
James Smartc913a8b2017-04-11 11:35:08 -0700735__nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop)
James Smarte3994412016-12-02 00:28:42 -0800736{
James Smartc913a8b2017-04-11 11:35:08 -0700737 struct nvme_fc_rport *rport = lsop->rport;
James Smarte3994412016-12-02 00:28:42 -0800738 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
739 unsigned long flags;
740
James Smartc913a8b2017-04-11 11:35:08 -0700741 spin_lock_irqsave(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -0800742
743 if (!lsop->req_queued) {
James Smartc913a8b2017-04-11 11:35:08 -0700744 spin_unlock_irqrestore(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -0800745 return;
746 }
747
748 list_del(&lsop->lsreq_list);
749
750 lsop->req_queued = false;
751
James Smartc913a8b2017-04-11 11:35:08 -0700752 spin_unlock_irqrestore(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -0800753
James Smartc913a8b2017-04-11 11:35:08 -0700754 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
James Smarte3994412016-12-02 00:28:42 -0800755 (lsreq->rqstlen + lsreq->rsplen),
756 DMA_BIDIRECTIONAL);
757
James Smartc913a8b2017-04-11 11:35:08 -0700758 nvme_fc_rport_put(rport);
James Smarte3994412016-12-02 00:28:42 -0800759}
760
761static int
James Smartc913a8b2017-04-11 11:35:08 -0700762__nvme_fc_send_ls_req(struct nvme_fc_rport *rport,
James Smarte3994412016-12-02 00:28:42 -0800763 struct nvmefc_ls_req_op *lsop,
764 void (*done)(struct nvmefc_ls_req *req, int status))
765{
766 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
767 unsigned long flags;
James Smartc913a8b2017-04-11 11:35:08 -0700768 int ret = 0;
James Smarte3994412016-12-02 00:28:42 -0800769
James Smartc913a8b2017-04-11 11:35:08 -0700770 if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
771 return -ECONNREFUSED;
772
773 if (!nvme_fc_rport_get(rport))
James Smarte3994412016-12-02 00:28:42 -0800774 return -ESHUTDOWN;
775
776 lsreq->done = done;
James Smartc913a8b2017-04-11 11:35:08 -0700777 lsop->rport = rport;
James Smarte3994412016-12-02 00:28:42 -0800778 lsop->req_queued = false;
779 INIT_LIST_HEAD(&lsop->lsreq_list);
780 init_completion(&lsop->ls_done);
781
James Smartc913a8b2017-04-11 11:35:08 -0700782 lsreq->rqstdma = fc_dma_map_single(rport->dev, lsreq->rqstaddr,
James Smarte3994412016-12-02 00:28:42 -0800783 lsreq->rqstlen + lsreq->rsplen,
784 DMA_BIDIRECTIONAL);
James Smartc913a8b2017-04-11 11:35:08 -0700785 if (fc_dma_mapping_error(rport->dev, lsreq->rqstdma)) {
786 ret = -EFAULT;
787 goto out_putrport;
James Smarte3994412016-12-02 00:28:42 -0800788 }
789 lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
790
James Smartc913a8b2017-04-11 11:35:08 -0700791 spin_lock_irqsave(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -0800792
James Smartc913a8b2017-04-11 11:35:08 -0700793 list_add_tail(&lsop->lsreq_list, &rport->ls_req_list);
James Smarte3994412016-12-02 00:28:42 -0800794
795 lsop->req_queued = true;
796
James Smartc913a8b2017-04-11 11:35:08 -0700797 spin_unlock_irqrestore(&rport->lock, flags);
James Smarte3994412016-12-02 00:28:42 -0800798
James Smartc913a8b2017-04-11 11:35:08 -0700799 ret = rport->lport->ops->ls_req(&rport->lport->localport,
800 &rport->remoteport, lsreq);
James Smarte3994412016-12-02 00:28:42 -0800801 if (ret)
James Smartc913a8b2017-04-11 11:35:08 -0700802 goto out_unlink;
803
804 return 0;
805
806out_unlink:
807 lsop->ls_error = ret;
808 spin_lock_irqsave(&rport->lock, flags);
809 lsop->req_queued = false;
810 list_del(&lsop->lsreq_list);
811 spin_unlock_irqrestore(&rport->lock, flags);
812 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
813 (lsreq->rqstlen + lsreq->rsplen),
814 DMA_BIDIRECTIONAL);
815out_putrport:
816 nvme_fc_rport_put(rport);
James Smarte3994412016-12-02 00:28:42 -0800817
818 return ret;
819}
820
821static void
822nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status)
823{
824 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
825
826 lsop->ls_error = status;
827 complete(&lsop->ls_done);
828}
829
830static int
James Smartc913a8b2017-04-11 11:35:08 -0700831nvme_fc_send_ls_req(struct nvme_fc_rport *rport, struct nvmefc_ls_req_op *lsop)
James Smarte3994412016-12-02 00:28:42 -0800832{
833 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
834 struct fcnvme_ls_rjt *rjt = lsreq->rspaddr;
835 int ret;
836
James Smartc913a8b2017-04-11 11:35:08 -0700837 ret = __nvme_fc_send_ls_req(rport, lsop, nvme_fc_send_ls_req_done);
James Smarte3994412016-12-02 00:28:42 -0800838
James Smartc913a8b2017-04-11 11:35:08 -0700839 if (!ret) {
James Smarte3994412016-12-02 00:28:42 -0800840 /*
841 * No timeout/not interruptible as we need the struct
842 * to exist until the lldd calls us back. Thus mandate
843 * wait until driver calls back. lldd responsible for
844 * the timeout action
845 */
846 wait_for_completion(&lsop->ls_done);
847
James Smartc913a8b2017-04-11 11:35:08 -0700848 __nvme_fc_finish_ls_req(lsop);
James Smarte3994412016-12-02 00:28:42 -0800849
James Smartc913a8b2017-04-11 11:35:08 -0700850 ret = lsop->ls_error;
James Smarte3994412016-12-02 00:28:42 -0800851 }
852
James Smartc913a8b2017-04-11 11:35:08 -0700853 if (ret)
854 return ret;
855
James Smarte3994412016-12-02 00:28:42 -0800856 /* ACC or RJT payload ? */
857 if (rjt->w0.ls_cmd == FCNVME_LS_RJT)
858 return -ENXIO;
859
860 return 0;
861}
862
James Smartc913a8b2017-04-11 11:35:08 -0700863static int
864nvme_fc_send_ls_req_async(struct nvme_fc_rport *rport,
James Smarte3994412016-12-02 00:28:42 -0800865 struct nvmefc_ls_req_op *lsop,
866 void (*done)(struct nvmefc_ls_req *req, int status))
867{
James Smarte3994412016-12-02 00:28:42 -0800868 /* don't wait for completion */
869
James Smartc913a8b2017-04-11 11:35:08 -0700870 return __nvme_fc_send_ls_req(rport, lsop, done);
James Smarte3994412016-12-02 00:28:42 -0800871}
872
873/* Validation Error indexes into the string table below */
874enum {
875 VERR_NO_ERROR = 0,
876 VERR_LSACC = 1,
877 VERR_LSDESC_RQST = 2,
878 VERR_LSDESC_RQST_LEN = 3,
879 VERR_ASSOC_ID = 4,
880 VERR_ASSOC_ID_LEN = 5,
881 VERR_CONN_ID = 6,
882 VERR_CONN_ID_LEN = 7,
883 VERR_CR_ASSOC = 8,
884 VERR_CR_ASSOC_ACC_LEN = 9,
885 VERR_CR_CONN = 10,
886 VERR_CR_CONN_ACC_LEN = 11,
887 VERR_DISCONN = 12,
888 VERR_DISCONN_ACC_LEN = 13,
889};
890
891static char *validation_errors[] = {
892 "OK",
893 "Not LS_ACC",
894 "Not LSDESC_RQST",
895 "Bad LSDESC_RQST Length",
896 "Not Association ID",
897 "Bad Association ID Length",
898 "Not Connection ID",
899 "Bad Connection ID Length",
900 "Not CR_ASSOC Rqst",
901 "Bad CR_ASSOC ACC Length",
902 "Not CR_CONN Rqst",
903 "Bad CR_CONN ACC Length",
904 "Not Disconnect Rqst",
905 "Bad Disconnect ACC Length",
906};
907
908static int
909nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl,
910 struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio)
911{
912 struct nvmefc_ls_req_op *lsop;
913 struct nvmefc_ls_req *lsreq;
914 struct fcnvme_ls_cr_assoc_rqst *assoc_rqst;
915 struct fcnvme_ls_cr_assoc_acc *assoc_acc;
916 int ret, fcret = 0;
917
918 lsop = kzalloc((sizeof(*lsop) +
919 ctrl->lport->ops->lsrqst_priv_sz +
920 sizeof(*assoc_rqst) + sizeof(*assoc_acc)), GFP_KERNEL);
921 if (!lsop) {
922 ret = -ENOMEM;
923 goto out_no_memory;
924 }
925 lsreq = &lsop->ls_req;
926
927 lsreq->private = (void *)&lsop[1];
928 assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)
929 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
930 assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1];
931
932 assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION;
933 assoc_rqst->desc_list_len =
934 cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
935
936 assoc_rqst->assoc_cmd.desc_tag =
937 cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD);
938 assoc_rqst->assoc_cmd.desc_len =
939 fcnvme_lsdesc_len(
940 sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
941
942 assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
943 assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize);
944 /* Linux supports only Dynamic controllers */
945 assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff);
Christoph Hellwig8e412262017-05-17 09:54:27 +0200946 uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id);
James Smarte3994412016-12-02 00:28:42 -0800947 strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
948 min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE));
949 strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
950 min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE));
951
952 lsop->queue = queue;
953 lsreq->rqstaddr = assoc_rqst;
954 lsreq->rqstlen = sizeof(*assoc_rqst);
955 lsreq->rspaddr = assoc_acc;
956 lsreq->rsplen = sizeof(*assoc_acc);
957 lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
958
James Smartc913a8b2017-04-11 11:35:08 -0700959 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
James Smarte3994412016-12-02 00:28:42 -0800960 if (ret)
961 goto out_free_buffer;
962
963 /* process connect LS completion */
964
965 /* validate the ACC response */
966 if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
967 fcret = VERR_LSACC;
James Smartf77fc872017-03-23 20:41:25 -0700968 else if (assoc_acc->hdr.desc_list_len !=
James Smarte3994412016-12-02 00:28:42 -0800969 fcnvme_lsdesc_len(
970 sizeof(struct fcnvme_ls_cr_assoc_acc)))
971 fcret = VERR_CR_ASSOC_ACC_LEN;
James Smartf77fc872017-03-23 20:41:25 -0700972 else if (assoc_acc->hdr.rqst.desc_tag !=
973 cpu_to_be32(FCNVME_LSDESC_RQST))
James Smarte3994412016-12-02 00:28:42 -0800974 fcret = VERR_LSDESC_RQST;
975 else if (assoc_acc->hdr.rqst.desc_len !=
976 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
977 fcret = VERR_LSDESC_RQST_LEN;
978 else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION)
979 fcret = VERR_CR_ASSOC;
980 else if (assoc_acc->associd.desc_tag !=
981 cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
982 fcret = VERR_ASSOC_ID;
983 else if (assoc_acc->associd.desc_len !=
984 fcnvme_lsdesc_len(
985 sizeof(struct fcnvme_lsdesc_assoc_id)))
986 fcret = VERR_ASSOC_ID_LEN;
987 else if (assoc_acc->connectid.desc_tag !=
988 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
989 fcret = VERR_CONN_ID;
990 else if (assoc_acc->connectid.desc_len !=
991 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
992 fcret = VERR_CONN_ID_LEN;
993
994 if (fcret) {
995 ret = -EBADF;
996 dev_err(ctrl->dev,
997 "q %d connect failed: %s\n",
998 queue->qnum, validation_errors[fcret]);
999 } else {
1000 ctrl->association_id =
1001 be64_to_cpu(assoc_acc->associd.association_id);
1002 queue->connection_id =
1003 be64_to_cpu(assoc_acc->connectid.connection_id);
1004 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1005 }
1006
1007out_free_buffer:
1008 kfree(lsop);
1009out_no_memory:
1010 if (ret)
1011 dev_err(ctrl->dev,
1012 "queue %d connect admin queue failed (%d).\n",
1013 queue->qnum, ret);
1014 return ret;
1015}
1016
1017static int
1018nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
1019 u16 qsize, u16 ersp_ratio)
1020{
1021 struct nvmefc_ls_req_op *lsop;
1022 struct nvmefc_ls_req *lsreq;
1023 struct fcnvme_ls_cr_conn_rqst *conn_rqst;
1024 struct fcnvme_ls_cr_conn_acc *conn_acc;
1025 int ret, fcret = 0;
1026
1027 lsop = kzalloc((sizeof(*lsop) +
1028 ctrl->lport->ops->lsrqst_priv_sz +
1029 sizeof(*conn_rqst) + sizeof(*conn_acc)), GFP_KERNEL);
1030 if (!lsop) {
1031 ret = -ENOMEM;
1032 goto out_no_memory;
1033 }
1034 lsreq = &lsop->ls_req;
1035
1036 lsreq->private = (void *)&lsop[1];
1037 conn_rqst = (struct fcnvme_ls_cr_conn_rqst *)
1038 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1039 conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1];
1040
1041 conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION;
1042 conn_rqst->desc_list_len = cpu_to_be32(
1043 sizeof(struct fcnvme_lsdesc_assoc_id) +
1044 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1045
1046 conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1047 conn_rqst->associd.desc_len =
1048 fcnvme_lsdesc_len(
1049 sizeof(struct fcnvme_lsdesc_assoc_id));
1050 conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1051 conn_rqst->connect_cmd.desc_tag =
1052 cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD);
1053 conn_rqst->connect_cmd.desc_len =
1054 fcnvme_lsdesc_len(
1055 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1056 conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1057 conn_rqst->connect_cmd.qid = cpu_to_be16(queue->qnum);
1058 conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize);
1059
1060 lsop->queue = queue;
1061 lsreq->rqstaddr = conn_rqst;
1062 lsreq->rqstlen = sizeof(*conn_rqst);
1063 lsreq->rspaddr = conn_acc;
1064 lsreq->rsplen = sizeof(*conn_acc);
1065 lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
1066
James Smartc913a8b2017-04-11 11:35:08 -07001067 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
James Smarte3994412016-12-02 00:28:42 -08001068 if (ret)
1069 goto out_free_buffer;
1070
1071 /* process connect LS completion */
1072
1073 /* validate the ACC response */
1074 if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1075 fcret = VERR_LSACC;
James Smartf77fc872017-03-23 20:41:25 -07001076 else if (conn_acc->hdr.desc_list_len !=
James Smarte3994412016-12-02 00:28:42 -08001077 fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)))
1078 fcret = VERR_CR_CONN_ACC_LEN;
James Smartf77fc872017-03-23 20:41:25 -07001079 else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST))
James Smarte3994412016-12-02 00:28:42 -08001080 fcret = VERR_LSDESC_RQST;
1081 else if (conn_acc->hdr.rqst.desc_len !=
1082 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1083 fcret = VERR_LSDESC_RQST_LEN;
1084 else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION)
1085 fcret = VERR_CR_CONN;
1086 else if (conn_acc->connectid.desc_tag !=
1087 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1088 fcret = VERR_CONN_ID;
1089 else if (conn_acc->connectid.desc_len !=
1090 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1091 fcret = VERR_CONN_ID_LEN;
1092
1093 if (fcret) {
1094 ret = -EBADF;
1095 dev_err(ctrl->dev,
1096 "q %d connect failed: %s\n",
1097 queue->qnum, validation_errors[fcret]);
1098 } else {
1099 queue->connection_id =
1100 be64_to_cpu(conn_acc->connectid.connection_id);
1101 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1102 }
1103
1104out_free_buffer:
1105 kfree(lsop);
1106out_no_memory:
1107 if (ret)
1108 dev_err(ctrl->dev,
1109 "queue %d connect command failed (%d).\n",
1110 queue->qnum, ret);
1111 return ret;
1112}
1113
1114static void
1115nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
1116{
1117 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
James Smarte3994412016-12-02 00:28:42 -08001118
James Smartc913a8b2017-04-11 11:35:08 -07001119 __nvme_fc_finish_ls_req(lsop);
James Smarte3994412016-12-02 00:28:42 -08001120
1121 /* fc-nvme iniator doesn't care about success or failure of cmd */
1122
1123 kfree(lsop);
1124}
1125
1126/*
1127 * This routine sends a FC-NVME LS to disconnect (aka terminate)
1128 * the FC-NVME Association. Terminating the association also
1129 * terminates the FC-NVME connections (per queue, both admin and io
1130 * queues) that are part of the association. E.g. things are torn
1131 * down, and the related FC-NVME Association ID and Connection IDs
1132 * become invalid.
1133 *
1134 * The behavior of the fc-nvme initiator is such that it's
1135 * understanding of the association and connections will implicitly
1136 * be torn down. The action is implicit as it may be due to a loss of
1137 * connectivity with the fc-nvme target, so you may never get a
1138 * response even if you tried. As such, the action of this routine
1139 * is to asynchronously send the LS, ignore any results of the LS, and
1140 * continue on with terminating the association. If the fc-nvme target
1141 * is present and receives the LS, it too can tear down.
1142 */
1143static void
1144nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl)
1145{
1146 struct fcnvme_ls_disconnect_rqst *discon_rqst;
1147 struct fcnvme_ls_disconnect_acc *discon_acc;
1148 struct nvmefc_ls_req_op *lsop;
1149 struct nvmefc_ls_req *lsreq;
James Smartc913a8b2017-04-11 11:35:08 -07001150 int ret;
James Smarte3994412016-12-02 00:28:42 -08001151
1152 lsop = kzalloc((sizeof(*lsop) +
1153 ctrl->lport->ops->lsrqst_priv_sz +
1154 sizeof(*discon_rqst) + sizeof(*discon_acc)),
1155 GFP_KERNEL);
1156 if (!lsop)
1157 /* couldn't sent it... too bad */
1158 return;
1159
1160 lsreq = &lsop->ls_req;
1161
1162 lsreq->private = (void *)&lsop[1];
1163 discon_rqst = (struct fcnvme_ls_disconnect_rqst *)
1164 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1165 discon_acc = (struct fcnvme_ls_disconnect_acc *)&discon_rqst[1];
1166
1167 discon_rqst->w0.ls_cmd = FCNVME_LS_DISCONNECT;
1168 discon_rqst->desc_list_len = cpu_to_be32(
1169 sizeof(struct fcnvme_lsdesc_assoc_id) +
1170 sizeof(struct fcnvme_lsdesc_disconn_cmd));
1171
1172 discon_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1173 discon_rqst->associd.desc_len =
1174 fcnvme_lsdesc_len(
1175 sizeof(struct fcnvme_lsdesc_assoc_id));
1176
1177 discon_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1178
1179 discon_rqst->discon_cmd.desc_tag = cpu_to_be32(
1180 FCNVME_LSDESC_DISCONN_CMD);
1181 discon_rqst->discon_cmd.desc_len =
1182 fcnvme_lsdesc_len(
1183 sizeof(struct fcnvme_lsdesc_disconn_cmd));
1184 discon_rqst->discon_cmd.scope = FCNVME_DISCONN_ASSOCIATION;
1185 discon_rqst->discon_cmd.id = cpu_to_be64(ctrl->association_id);
1186
1187 lsreq->rqstaddr = discon_rqst;
1188 lsreq->rqstlen = sizeof(*discon_rqst);
1189 lsreq->rspaddr = discon_acc;
1190 lsreq->rsplen = sizeof(*discon_acc);
1191 lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
1192
James Smartc913a8b2017-04-11 11:35:08 -07001193 ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop,
1194 nvme_fc_disconnect_assoc_done);
1195 if (ret)
1196 kfree(lsop);
James Smarte3994412016-12-02 00:28:42 -08001197
1198 /* only meaningful part to terminating the association */
1199 ctrl->association_id = 0;
1200}
1201
1202
1203/* *********************** NVME Ctrl Routines **************************** */
1204
James Smart78a7ac22017-04-23 08:30:07 -07001205static void __nvme_fc_final_op_cleanup(struct request *rq);
James Smartf874d5d2017-06-01 22:54:21 -07001206static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg);
James Smarte3994412016-12-02 00:28:42 -08001207
1208static int
1209nvme_fc_reinit_request(void *data, struct request *rq)
1210{
1211 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1212 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1213
1214 memset(cmdiu, 0, sizeof(*cmdiu));
1215 cmdiu->scsi_id = NVME_CMD_SCSI_ID;
1216 cmdiu->fc_id = NVME_CMD_FC_ID;
1217 cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
1218 memset(&op->rsp_iu, 0, sizeof(op->rsp_iu));
1219
1220 return 0;
1221}
1222
1223static void
1224__nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl,
1225 struct nvme_fc_fcp_op *op)
1226{
1227 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma,
1228 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1229 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
1230 sizeof(op->cmd_iu), DMA_TO_DEVICE);
1231
1232 atomic_set(&op->state, FCPOP_STATE_UNINIT);
1233}
1234
1235static void
Christoph Hellwigd6296d32017-05-01 10:19:08 -06001236nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1237 unsigned int hctx_idx)
James Smarte3994412016-12-02 00:28:42 -08001238{
1239 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1240
Christoph Hellwigd6296d32017-05-01 10:19:08 -06001241 return __nvme_fc_exit_request(set->driver_data, op);
James Smarte3994412016-12-02 00:28:42 -08001242}
1243
James Smart78a7ac22017-04-23 08:30:07 -07001244static int
1245__nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
1246{
1247 int state;
1248
1249 state = atomic_xchg(&op->state, FCPOP_STATE_ABORTED);
1250 if (state != FCPOP_STATE_ACTIVE) {
1251 atomic_set(&op->state, state);
1252 return -ECANCELED;
1253 }
1254
1255 ctrl->lport->ops->fcp_abort(&ctrl->lport->localport,
1256 &ctrl->rport->remoteport,
1257 op->queue->lldd_handle,
1258 &op->fcp_req);
1259
1260 return 0;
1261}
1262
James Smarte3994412016-12-02 00:28:42 -08001263static void
James Smart78a7ac22017-04-23 08:30:07 -07001264nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl)
James Smarte3994412016-12-02 00:28:42 -08001265{
1266 struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops;
James Smart78a7ac22017-04-23 08:30:07 -07001267 unsigned long flags;
1268 int i, ret;
James Smarte3994412016-12-02 00:28:42 -08001269
1270 for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
James Smart78a7ac22017-04-23 08:30:07 -07001271 if (atomic_read(&aen_op->state) != FCPOP_STATE_ACTIVE)
James Smarte3994412016-12-02 00:28:42 -08001272 continue;
James Smart78a7ac22017-04-23 08:30:07 -07001273
1274 spin_lock_irqsave(&ctrl->lock, flags);
James Smart61bff8e2017-04-23 08:30:08 -07001275 if (ctrl->flags & FCCTRL_TERMIO) {
1276 ctrl->iocnt++;
1277 aen_op->flags |= FCOP_FLAGS_TERMIO;
1278 }
James Smart78a7ac22017-04-23 08:30:07 -07001279 spin_unlock_irqrestore(&ctrl->lock, flags);
1280
1281 ret = __nvme_fc_abort_op(ctrl, aen_op);
1282 if (ret) {
1283 /*
1284 * if __nvme_fc_abort_op failed the io wasn't
1285 * active. Thus this call path is running in
1286 * parallel to the io complete. Treat as non-error.
1287 */
1288
1289 /* back out the flags/counters */
1290 spin_lock_irqsave(&ctrl->lock, flags);
James Smart61bff8e2017-04-23 08:30:08 -07001291 if (ctrl->flags & FCCTRL_TERMIO)
1292 ctrl->iocnt--;
James Smart78a7ac22017-04-23 08:30:07 -07001293 aen_op->flags &= ~FCOP_FLAGS_TERMIO;
1294 spin_unlock_irqrestore(&ctrl->lock, flags);
1295 return;
1296 }
James Smarte3994412016-12-02 00:28:42 -08001297 }
1298}
1299
James Smart78a7ac22017-04-23 08:30:07 -07001300static inline int
1301__nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
1302 struct nvme_fc_fcp_op *op)
1303{
1304 unsigned long flags;
1305 bool complete_rq = false;
1306
1307 spin_lock_irqsave(&ctrl->lock, flags);
James Smart61bff8e2017-04-23 08:30:08 -07001308 if (unlikely(op->flags & FCOP_FLAGS_TERMIO)) {
James Smart36715cf2017-05-22 15:28:42 -07001309 if (ctrl->flags & FCCTRL_TERMIO) {
1310 if (!--ctrl->iocnt)
1311 wake_up(&ctrl->ioabort_wait);
1312 }
James Smart61bff8e2017-04-23 08:30:08 -07001313 }
James Smart78a7ac22017-04-23 08:30:07 -07001314 if (op->flags & FCOP_FLAGS_RELEASED)
1315 complete_rq = true;
1316 else
1317 op->flags |= FCOP_FLAGS_COMPLETE;
1318 spin_unlock_irqrestore(&ctrl->lock, flags);
1319
1320 return complete_rq;
1321}
1322
Christoph Hellwigbaee29a2017-04-21 10:44:06 +02001323static void
James Smarte3994412016-12-02 00:28:42 -08001324nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
1325{
1326 struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1327 struct request *rq = op->rq;
1328 struct nvmefc_fcp_req *freq = &op->fcp_req;
1329 struct nvme_fc_ctrl *ctrl = op->ctrl;
1330 struct nvme_fc_queue *queue = op->queue;
1331 struct nvme_completion *cqe = &op->rsp_iu.cqe;
James Smart458f2802017-04-23 08:30:06 -07001332 struct nvme_command *sqe = &op->cmd_iu.sqe;
Christoph Hellwigd663b692017-04-20 16:02:56 +02001333 __le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001334 union nvme_result result;
James Smartf874d5d2017-06-01 22:54:21 -07001335 bool complete_rq, terminate_assoc = true;
James Smarte3994412016-12-02 00:28:42 -08001336
1337 /*
1338 * WARNING:
1339 * The current linux implementation of a nvme controller
1340 * allocates a single tag set for all io queues and sizes
1341 * the io queues to fully hold all possible tags. Thus, the
1342 * implementation does not reference or care about the sqhd
1343 * value as it never needs to use the sqhd/sqtail pointers
1344 * for submission pacing.
1345 *
1346 * This affects the FC-NVME implementation in two ways:
1347 * 1) As the value doesn't matter, we don't need to waste
1348 * cycles extracting it from ERSPs and stamping it in the
1349 * cases where the transport fabricates CQEs on successful
1350 * completions.
1351 * 2) The FC-NVME implementation requires that delivery of
1352 * ERSP completions are to go back to the nvme layer in order
1353 * relative to the rsn, such that the sqhd value will always
1354 * be "in order" for the nvme layer. As the nvme layer in
1355 * linux doesn't care about sqhd, there's no need to return
1356 * them in order.
1357 *
1358 * Additionally:
1359 * As the core nvme layer in linux currently does not look at
1360 * every field in the cqe - in cases where the FC transport must
1361 * fabricate a CQE, the following fields will not be set as they
1362 * are not referenced:
1363 * cqe.sqid, cqe.sqhd, cqe.command_id
James Smartf874d5d2017-06-01 22:54:21 -07001364 *
1365 * Failure or error of an individual i/o, in a transport
1366 * detected fashion unrelated to the nvme completion status,
1367 * potentially cause the initiator and target sides to get out
1368 * of sync on SQ head/tail (aka outstanding io count allowed).
1369 * Per FC-NVME spec, failure of an individual command requires
1370 * the connection to be terminated, which in turn requires the
1371 * association to be terminated.
James Smarte3994412016-12-02 00:28:42 -08001372 */
1373
1374 fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
1375 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1376
1377 if (atomic_read(&op->state) == FCPOP_STATE_ABORTED)
Christoph Hellwigd663b692017-04-20 16:02:56 +02001378 status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
James Smart62eeacb2017-03-23 20:41:27 -07001379 else if (freq->status)
James Smart56b71032017-09-07 16:27:26 -07001380 status = cpu_to_le16(NVME_SC_INTERNAL << 1);
James Smarte3994412016-12-02 00:28:42 -08001381
1382 /*
1383 * For the linux implementation, if we have an unsuccesful
1384 * status, they blk-mq layer can typically be called with the
1385 * non-zero status and the content of the cqe isn't important.
1386 */
1387 if (status)
1388 goto done;
1389
1390 /*
1391 * command completed successfully relative to the wire
1392 * protocol. However, validate anything received and
1393 * extract the status and result from the cqe (create it
1394 * where necessary).
1395 */
1396
1397 switch (freq->rcv_rsplen) {
1398
1399 case 0:
1400 case NVME_FC_SIZEOF_ZEROS_RSP:
1401 /*
1402 * No response payload or 12 bytes of payload (which
1403 * should all be zeros) are considered successful and
1404 * no payload in the CQE by the transport.
1405 */
1406 if (freq->transferred_length !=
1407 be32_to_cpu(op->cmd_iu.data_len)) {
James Smart56b71032017-09-07 16:27:26 -07001408 status = cpu_to_le16(NVME_SC_INTERNAL << 1);
James Smarte3994412016-12-02 00:28:42 -08001409 goto done;
1410 }
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001411 result.u64 = 0;
James Smarte3994412016-12-02 00:28:42 -08001412 break;
1413
1414 case sizeof(struct nvme_fc_ersp_iu):
1415 /*
1416 * The ERSP IU contains a full completion with CQE.
1417 * Validate ERSP IU and look at cqe.
1418 */
1419 if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) !=
1420 (freq->rcv_rsplen / 4) ||
1421 be32_to_cpu(op->rsp_iu.xfrd_len) !=
1422 freq->transferred_length ||
James Smart726a1082017-03-23 20:41:23 -07001423 op->rsp_iu.status_code ||
James Smart458f2802017-04-23 08:30:06 -07001424 sqe->common.command_id != cqe->command_id)) {
James Smart56b71032017-09-07 16:27:26 -07001425 status = cpu_to_le16(NVME_SC_INTERNAL << 1);
James Smarte3994412016-12-02 00:28:42 -08001426 goto done;
1427 }
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001428 result = cqe->result;
Christoph Hellwigd663b692017-04-20 16:02:56 +02001429 status = cqe->status;
James Smarte3994412016-12-02 00:28:42 -08001430 break;
1431
1432 default:
James Smart56b71032017-09-07 16:27:26 -07001433 status = cpu_to_le16(NVME_SC_INTERNAL << 1);
James Smarte3994412016-12-02 00:28:42 -08001434 goto done;
1435 }
1436
James Smartf874d5d2017-06-01 22:54:21 -07001437 terminate_assoc = false;
1438
James Smarte3994412016-12-02 00:28:42 -08001439done:
James Smart78a7ac22017-04-23 08:30:07 -07001440 if (op->flags & FCOP_FLAGS_AEN) {
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001441 nvme_complete_async_event(&queue->ctrl->ctrl, status, &result);
James Smart78a7ac22017-04-23 08:30:07 -07001442 complete_rq = __nvme_fc_fcpop_chk_teardowns(ctrl, op);
1443 atomic_set(&op->state, FCPOP_STATE_IDLE);
1444 op->flags = FCOP_FLAGS_AEN; /* clear other flags */
James Smarte3994412016-12-02 00:28:42 -08001445 nvme_fc_ctrl_put(ctrl);
James Smartf874d5d2017-06-01 22:54:21 -07001446 goto check_error;
James Smarte3994412016-12-02 00:28:42 -08001447 }
1448
James Smart78a7ac22017-04-23 08:30:07 -07001449 complete_rq = __nvme_fc_fcpop_chk_teardowns(ctrl, op);
1450 if (!complete_rq) {
1451 if (unlikely(op->flags & FCOP_FLAGS_TERMIO)) {
James Smarte392e1f2017-05-15 17:10:24 -07001452 status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
James Smart78a7ac22017-04-23 08:30:07 -07001453 if (blk_queue_dying(rq->q))
James Smarte392e1f2017-05-15 17:10:24 -07001454 status |= cpu_to_le16(NVME_SC_DNR << 1);
James Smart78a7ac22017-04-23 08:30:07 -07001455 }
1456 nvme_end_request(rq, status, result);
1457 } else
1458 __nvme_fc_final_op_cleanup(rq);
James Smartf874d5d2017-06-01 22:54:21 -07001459
1460check_error:
1461 if (terminate_assoc)
1462 nvme_fc_error_recovery(ctrl, "transport detected io error");
James Smarte3994412016-12-02 00:28:42 -08001463}
1464
1465static int
1466__nvme_fc_init_request(struct nvme_fc_ctrl *ctrl,
1467 struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op,
1468 struct request *rq, u32 rqno)
1469{
1470 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1471 int ret = 0;
1472
1473 memset(op, 0, sizeof(*op));
1474 op->fcp_req.cmdaddr = &op->cmd_iu;
1475 op->fcp_req.cmdlen = sizeof(op->cmd_iu);
1476 op->fcp_req.rspaddr = &op->rsp_iu;
1477 op->fcp_req.rsplen = sizeof(op->rsp_iu);
1478 op->fcp_req.done = nvme_fc_fcpio_done;
1479 op->fcp_req.first_sgl = (struct scatterlist *)&op[1];
1480 op->fcp_req.private = &op->fcp_req.first_sgl[SG_CHUNK_SIZE];
1481 op->ctrl = ctrl;
1482 op->queue = queue;
1483 op->rq = rq;
1484 op->rqno = rqno;
1485
1486 cmdiu->scsi_id = NVME_CMD_SCSI_ID;
1487 cmdiu->fc_id = NVME_CMD_FC_ID;
1488 cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
1489
1490 op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev,
1491 &op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE);
1492 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) {
1493 dev_err(ctrl->dev,
1494 "FCP Op failed - cmdiu dma mapping failed.\n");
Tianjia Zhangb94544a2020-08-02 19:15:45 +08001495 ret = -EFAULT;
James Smarte3994412016-12-02 00:28:42 -08001496 goto out_on_error;
1497 }
1498
1499 op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev,
1500 &op->rsp_iu, sizeof(op->rsp_iu),
1501 DMA_FROM_DEVICE);
1502 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) {
1503 dev_err(ctrl->dev,
1504 "FCP Op failed - rspiu dma mapping failed.\n");
Tianjia Zhangb94544a2020-08-02 19:15:45 +08001505 ret = -EFAULT;
James Smarte3994412016-12-02 00:28:42 -08001506 }
1507
1508 atomic_set(&op->state, FCPOP_STATE_IDLE);
1509out_on_error:
1510 return ret;
1511}
1512
1513static int
Christoph Hellwigd6296d32017-05-01 10:19:08 -06001514nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq,
1515 unsigned int hctx_idx, unsigned int numa_node)
James Smarte3994412016-12-02 00:28:42 -08001516{
Christoph Hellwigd6296d32017-05-01 10:19:08 -06001517 struct nvme_fc_ctrl *ctrl = set->driver_data;
James Smarte3994412016-12-02 00:28:42 -08001518 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
Christoph Hellwig76f983c2017-06-13 09:15:20 +02001519 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
1520 struct nvme_fc_queue *queue = &ctrl->queues[queue_idx];
James Smarte3994412016-12-02 00:28:42 -08001521
1522 return __nvme_fc_init_request(ctrl, queue, op, rq, queue->rqcnt++);
1523}
1524
1525static int
1526nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl)
1527{
1528 struct nvme_fc_fcp_op *aen_op;
1529 struct nvme_fc_cmd_iu *cmdiu;
1530 struct nvme_command *sqe;
James Smart61bff8e2017-04-23 08:30:08 -07001531 void *private;
James Smarte3994412016-12-02 00:28:42 -08001532 int i, ret;
1533
1534 aen_op = ctrl->aen_ops;
1535 for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
James Smart61bff8e2017-04-23 08:30:08 -07001536 private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz,
1537 GFP_KERNEL);
1538 if (!private)
1539 return -ENOMEM;
1540
James Smarte3994412016-12-02 00:28:42 -08001541 cmdiu = &aen_op->cmd_iu;
1542 sqe = &cmdiu->sqe;
1543 ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0],
1544 aen_op, (struct request *)NULL,
1545 (AEN_CMDID_BASE + i));
James Smart61bff8e2017-04-23 08:30:08 -07001546 if (ret) {
1547 kfree(private);
James Smarte3994412016-12-02 00:28:42 -08001548 return ret;
James Smart61bff8e2017-04-23 08:30:08 -07001549 }
James Smarte3994412016-12-02 00:28:42 -08001550
James Smart78a7ac22017-04-23 08:30:07 -07001551 aen_op->flags = FCOP_FLAGS_AEN;
James Smart61bff8e2017-04-23 08:30:08 -07001552 aen_op->fcp_req.first_sgl = NULL; /* no sg list */
1553 aen_op->fcp_req.private = private;
James Smart78a7ac22017-04-23 08:30:07 -07001554
James Smarte3994412016-12-02 00:28:42 -08001555 memset(sqe, 0, sizeof(*sqe));
1556 sqe->common.opcode = nvme_admin_async_event;
James Smart78a7ac22017-04-23 08:30:07 -07001557 /* Note: core layer may overwrite the sqe.command_id value */
James Smarte3994412016-12-02 00:28:42 -08001558 sqe->common.command_id = AEN_CMDID_BASE + i;
1559 }
1560 return 0;
1561}
1562
James Smart61bff8e2017-04-23 08:30:08 -07001563static void
1564nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl)
1565{
1566 struct nvme_fc_fcp_op *aen_op;
1567 int i;
1568
David Milburn97299872020-09-02 17:42:54 -05001569 cancel_work_sync(&ctrl->ctrl.async_event_work);
James Smart61bff8e2017-04-23 08:30:08 -07001570 aen_op = ctrl->aen_ops;
1571 for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
1572 if (!aen_op->fcp_req.private)
1573 continue;
1574
1575 __nvme_fc_exit_request(ctrl, aen_op);
1576
1577 kfree(aen_op->fcp_req.private);
1578 aen_op->fcp_req.private = NULL;
1579 }
1580}
James Smarte3994412016-12-02 00:28:42 -08001581
1582static inline void
1583__nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, struct nvme_fc_ctrl *ctrl,
1584 unsigned int qidx)
1585{
1586 struct nvme_fc_queue *queue = &ctrl->queues[qidx];
1587
1588 hctx->driver_data = queue;
1589 queue->hctx = hctx;
1590}
1591
1592static int
1593nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1594 unsigned int hctx_idx)
1595{
1596 struct nvme_fc_ctrl *ctrl = data;
1597
1598 __nvme_fc_init_hctx(hctx, ctrl, hctx_idx + 1);
1599
1600 return 0;
1601}
1602
1603static int
1604nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1605 unsigned int hctx_idx)
1606{
1607 struct nvme_fc_ctrl *ctrl = data;
1608
1609 __nvme_fc_init_hctx(hctx, ctrl, hctx_idx);
1610
1611 return 0;
1612}
1613
1614static void
1615nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx, size_t queue_size)
1616{
1617 struct nvme_fc_queue *queue;
1618
1619 queue = &ctrl->queues[idx];
1620 memset(queue, 0, sizeof(*queue));
1621 queue->ctrl = ctrl;
1622 queue->qnum = idx;
1623 atomic_set(&queue->csn, 1);
1624 queue->dev = ctrl->dev;
1625
1626 if (idx > 0)
1627 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
1628 else
1629 queue->cmnd_capsule_len = sizeof(struct nvme_command);
1630
1631 queue->queue_size = queue_size;
1632
1633 /*
1634 * Considered whether we should allocate buffers for all SQEs
1635 * and CQEs and dma map them - mapping their respective entries
1636 * into the request structures (kernel vm addr and dma address)
1637 * thus the driver could use the buffers/mappings directly.
1638 * It only makes sense if the LLDD would use them for its
1639 * messaging api. It's very unlikely most adapter api's would use
1640 * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
1641 * structures were used instead.
1642 */
1643}
1644
1645/*
1646 * This routine terminates a queue at the transport level.
1647 * The transport has already ensured that all outstanding ios on
1648 * the queue have been terminated.
1649 * The transport will send a Disconnect LS request to terminate
1650 * the queue's connection. Termination of the admin queue will also
1651 * terminate the association at the target.
1652 */
1653static void
1654nvme_fc_free_queue(struct nvme_fc_queue *queue)
1655{
1656 if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags))
1657 return;
1658
Sagi Grimbergdb2044f2017-10-24 15:25:21 +03001659 clear_bit(NVME_FC_Q_LIVE, &queue->flags);
James Smarte3994412016-12-02 00:28:42 -08001660 /*
1661 * Current implementation never disconnects a single queue.
1662 * It always terminates a whole association. So there is never
1663 * a disconnect(queue) LS sent to the target.
1664 */
1665
1666 queue->connection_id = 0;
James Smarte3994412016-12-02 00:28:42 -08001667}
1668
1669static void
1670__nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl,
1671 struct nvme_fc_queue *queue, unsigned int qidx)
1672{
1673 if (ctrl->lport->ops->delete_queue)
1674 ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx,
1675 queue->lldd_handle);
1676 queue->lldd_handle = NULL;
1677}
1678
1679static void
James Smarte3994412016-12-02 00:28:42 -08001680nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl)
1681{
1682 int i;
1683
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001684 for (i = 1; i < ctrl->ctrl.queue_count; i++)
James Smarte3994412016-12-02 00:28:42 -08001685 nvme_fc_free_queue(&ctrl->queues[i]);
1686}
1687
1688static int
1689__nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl,
1690 struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize)
1691{
1692 int ret = 0;
1693
1694 queue->lldd_handle = NULL;
1695 if (ctrl->lport->ops->create_queue)
1696 ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport,
1697 qidx, qsize, &queue->lldd_handle);
1698
1699 return ret;
1700}
1701
1702static void
1703nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl)
1704{
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001705 struct nvme_fc_queue *queue = &ctrl->queues[ctrl->ctrl.queue_count - 1];
James Smarte3994412016-12-02 00:28:42 -08001706 int i;
1707
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001708 for (i = ctrl->ctrl.queue_count - 1; i >= 1; i--, queue--)
James Smarte3994412016-12-02 00:28:42 -08001709 __nvme_fc_delete_hw_queue(ctrl, queue, i);
1710}
1711
1712static int
1713nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
1714{
1715 struct nvme_fc_queue *queue = &ctrl->queues[1];
Johannes Thumshirn17a1ec02016-12-15 14:20:48 +01001716 int i, ret;
James Smarte3994412016-12-02 00:28:42 -08001717
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001718 for (i = 1; i < ctrl->ctrl.queue_count; i++, queue++) {
James Smarte3994412016-12-02 00:28:42 -08001719 ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize);
Johannes Thumshirn17a1ec02016-12-15 14:20:48 +01001720 if (ret)
1721 goto delete_queues;
James Smarte3994412016-12-02 00:28:42 -08001722 }
1723
1724 return 0;
Johannes Thumshirn17a1ec02016-12-15 14:20:48 +01001725
1726delete_queues:
1727 for (; i >= 0; i--)
1728 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i);
1729 return ret;
James Smarte3994412016-12-02 00:28:42 -08001730}
1731
1732static int
1733nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
1734{
1735 int i, ret = 0;
1736
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001737 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
James Smarte3994412016-12-02 00:28:42 -08001738 ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize,
1739 (qsize / 5));
1740 if (ret)
1741 break;
1742 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
1743 if (ret)
1744 break;
Sagi Grimbergdb2044f2017-10-24 15:25:21 +03001745
1746 set_bit(NVME_FC_Q_LIVE, &ctrl->queues[i].flags);
James Smarte3994412016-12-02 00:28:42 -08001747 }
1748
1749 return ret;
1750}
1751
1752static void
1753nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl)
1754{
1755 int i;
1756
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001757 for (i = 1; i < ctrl->ctrl.queue_count; i++)
James Smarte3994412016-12-02 00:28:42 -08001758 nvme_fc_init_queue(ctrl, i, ctrl->ctrl.sqsize);
1759}
1760
1761static void
1762nvme_fc_ctrl_free(struct kref *ref)
1763{
1764 struct nvme_fc_ctrl *ctrl =
1765 container_of(ref, struct nvme_fc_ctrl, ref);
1766 unsigned long flags;
1767
James Smart61bff8e2017-04-23 08:30:08 -07001768 if (ctrl->ctrl.tagset) {
1769 blk_cleanup_queue(ctrl->ctrl.connect_q);
1770 blk_mq_free_tag_set(&ctrl->tag_set);
James Smarte3994412016-12-02 00:28:42 -08001771 }
1772
James Smart61bff8e2017-04-23 08:30:08 -07001773 /* remove from rport list */
1774 spin_lock_irqsave(&ctrl->rport->lock, flags);
1775 list_del(&ctrl->ctrl_list);
1776 spin_unlock_irqrestore(&ctrl->rport->lock, flags);
1777
Sagi Grimbergf9c5af52017-07-02 15:39:34 +03001778 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
James Smart61bff8e2017-04-23 08:30:08 -07001779 blk_cleanup_queue(ctrl->ctrl.admin_q);
1780 blk_mq_free_tag_set(&ctrl->admin_tag_set);
1781
1782 kfree(ctrl->queues);
1783
James Smarte3994412016-12-02 00:28:42 -08001784 put_device(ctrl->dev);
1785 nvme_fc_rport_put(ctrl->rport);
1786
James Smarte3994412016-12-02 00:28:42 -08001787 ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
Ewan D. Milnede414472017-04-24 13:24:16 -04001788 if (ctrl->ctrl.opts)
1789 nvmf_free_options(ctrl->ctrl.opts);
James Smarte3994412016-12-02 00:28:42 -08001790 kfree(ctrl);
1791}
1792
1793static void
1794nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl)
1795{
1796 kref_put(&ctrl->ref, nvme_fc_ctrl_free);
1797}
1798
1799static int
1800nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
1801{
1802 return kref_get_unless_zero(&ctrl->ref);
1803}
1804
1805/*
1806 * All accesses from nvme core layer done - can now free the
1807 * controller. Called after last nvme_put_ctrl() call
1808 */
1809static void
James Smart61bff8e2017-04-23 08:30:08 -07001810nvme_fc_nvme_ctrl_freed(struct nvme_ctrl *nctrl)
James Smarte3994412016-12-02 00:28:42 -08001811{
1812 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
1813
1814 WARN_ON(nctrl != &ctrl->ctrl);
1815
James Smart61bff8e2017-04-23 08:30:08 -07001816 nvme_fc_ctrl_put(ctrl);
1817}
James Smarte3994412016-12-02 00:28:42 -08001818
James Smart61bff8e2017-04-23 08:30:08 -07001819static void
1820nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
1821{
James Smart69fa9642017-06-21 17:43:21 -07001822 /* only proceed if in LIVE state - e.g. on first error */
1823 if (ctrl->ctrl.state != NVME_CTRL_LIVE)
1824 return;
1825
James Smart61bff8e2017-04-23 08:30:08 -07001826 dev_warn(ctrl->ctrl.device,
1827 "NVME-FC{%d}: transport association error detected: %s\n",
1828 ctrl->cnum, errmsg);
James Smart589ff772017-05-15 17:10:19 -07001829 dev_warn(ctrl->ctrl.device,
James Smart61bff8e2017-04-23 08:30:08 -07001830 "NVME-FC{%d}: resetting controller\n", ctrl->cnum);
James Smarte3994412016-12-02 00:28:42 -08001831
James Smart61bff8e2017-04-23 08:30:08 -07001832 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING)) {
1833 dev_err(ctrl->ctrl.device,
1834 "NVME-FC{%d}: error_recovery: Couldn't change state "
1835 "to RECONNECTING\n", ctrl->cnum);
1836 return;
James Smarte3994412016-12-02 00:28:42 -08001837 }
1838
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001839 nvme_reset_ctrl(&ctrl->ctrl);
James Smarte3994412016-12-02 00:28:42 -08001840}
1841
Christoph Hellwigbaee29a2017-04-21 10:44:06 +02001842static enum blk_eh_timer_return
James Smarte3994412016-12-02 00:28:42 -08001843nvme_fc_timeout(struct request *rq, bool reserved)
1844{
1845 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1846 struct nvme_fc_ctrl *ctrl = op->ctrl;
1847 int ret;
1848
1849 if (reserved)
1850 return BLK_EH_RESET_TIMER;
1851
1852 ret = __nvme_fc_abort_op(ctrl, op);
1853 if (ret)
1854 /* io wasn't active to abort consider it done */
1855 return BLK_EH_HANDLED;
1856
1857 /*
James Smart61bff8e2017-04-23 08:30:08 -07001858 * we can't individually ABTS an io without affecting the queue,
1859 * thus killing the queue, adn thus the association.
1860 * So resolve by performing a controller reset, which will stop
1861 * the host/io stack, terminate the association on the link,
1862 * and recreate an association on the link.
James Smarte3994412016-12-02 00:28:42 -08001863 */
James Smart61bff8e2017-04-23 08:30:08 -07001864 nvme_fc_error_recovery(ctrl, "io timeout error");
James Smarte3994412016-12-02 00:28:42 -08001865
1866 return BLK_EH_HANDLED;
1867}
1868
1869static int
1870nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
1871 struct nvme_fc_fcp_op *op)
1872{
1873 struct nvmefc_fcp_req *freq = &op->fcp_req;
James Smarte3994412016-12-02 00:28:42 -08001874 enum dma_data_direction dir;
1875 int ret;
1876
1877 freq->sg_cnt = 0;
1878
Christoph Hellwigb131c612017-01-13 12:29:12 +01001879 if (!blk_rq_payload_bytes(rq))
James Smarte3994412016-12-02 00:28:42 -08001880 return 0;
1881
1882 freq->sg_table.sgl = freq->first_sgl;
Christoph Hellwig19e420b2017-01-19 16:55:57 +01001883 ret = sg_alloc_table_chained(&freq->sg_table,
1884 blk_rq_nr_phys_segments(rq), freq->sg_table.sgl);
James Smarte3994412016-12-02 00:28:42 -08001885 if (ret)
1886 return -ENOMEM;
1887
1888 op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl);
Christoph Hellwig19e420b2017-01-19 16:55:57 +01001889 WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
James Smarte3994412016-12-02 00:28:42 -08001890 dir = (rq_data_dir(rq) == WRITE) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
1891 freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
1892 op->nents, dir);
1893 if (unlikely(freq->sg_cnt <= 0)) {
1894 sg_free_table_chained(&freq->sg_table, true);
1895 freq->sg_cnt = 0;
1896 return -EFAULT;
1897 }
1898
1899 /*
1900 * TODO: blk_integrity_rq(rq) for DIF
1901 */
1902 return 0;
1903}
1904
1905static void
1906nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
1907 struct nvme_fc_fcp_op *op)
1908{
1909 struct nvmefc_fcp_req *freq = &op->fcp_req;
1910
1911 if (!freq->sg_cnt)
1912 return;
1913
1914 fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
1915 ((rq_data_dir(rq) == WRITE) ?
1916 DMA_TO_DEVICE : DMA_FROM_DEVICE));
1917
1918 nvme_cleanup_cmd(rq);
1919
1920 sg_free_table_chained(&freq->sg_table, true);
1921
1922 freq->sg_cnt = 0;
1923}
1924
1925/*
1926 * In FC, the queue is a logical thing. At transport connect, the target
1927 * creates its "queue" and returns a handle that is to be given to the
1928 * target whenever it posts something to the corresponding SQ. When an
1929 * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
1930 * command contained within the SQE, an io, and assigns a FC exchange
1931 * to it. The SQE and the associated SQ handle are sent in the initial
1932 * CMD IU sents on the exchange. All transfers relative to the io occur
1933 * as part of the exchange. The CQE is the last thing for the io,
1934 * which is transferred (explicitly or implicitly) with the RSP IU
1935 * sent on the exchange. After the CQE is received, the FC exchange is
1936 * terminaed and the Exchange may be used on a different io.
1937 *
1938 * The transport to LLDD api has the transport making a request for a
1939 * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
1940 * resource and transfers the command. The LLDD will then process all
1941 * steps to complete the io. Upon completion, the transport done routine
1942 * is called.
1943 *
1944 * So - while the operation is outstanding to the LLDD, there is a link
1945 * level FC exchange resource that is also outstanding. This must be
1946 * considered in all cleanup operations.
1947 */
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001948static blk_status_t
James Smarte3994412016-12-02 00:28:42 -08001949nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
1950 struct nvme_fc_fcp_op *op, u32 data_len,
1951 enum nvmefc_fcp_datadir io_dir)
1952{
1953 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1954 struct nvme_command *sqe = &cmdiu->sqe;
1955 u32 csn;
1956 int ret;
1957
James Smart61bff8e2017-04-23 08:30:08 -07001958 /*
1959 * before attempting to send the io, check to see if we believe
1960 * the target device is present
1961 */
1962 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
James Smart8b25f352017-07-18 14:29:34 -07001963 goto busy;
James Smart61bff8e2017-04-23 08:30:08 -07001964
James Smarte3994412016-12-02 00:28:42 -08001965 if (!nvme_fc_ctrl_get(ctrl))
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001966 return BLK_STS_IOERR;
James Smarte3994412016-12-02 00:28:42 -08001967
1968 /* format the FC-NVME CMD IU and fcp_req */
1969 cmdiu->connection_id = cpu_to_be64(queue->connection_id);
1970 csn = atomic_inc_return(&queue->csn);
1971 cmdiu->csn = cpu_to_be32(csn);
1972 cmdiu->data_len = cpu_to_be32(data_len);
1973 switch (io_dir) {
1974 case NVMEFC_FCP_WRITE:
1975 cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
1976 break;
1977 case NVMEFC_FCP_READ:
1978 cmdiu->flags = FCNVME_CMD_FLAGS_READ;
1979 break;
1980 case NVMEFC_FCP_NODATA:
1981 cmdiu->flags = 0;
1982 break;
1983 }
1984 op->fcp_req.payload_length = data_len;
1985 op->fcp_req.io_dir = io_dir;
1986 op->fcp_req.transferred_length = 0;
1987 op->fcp_req.rcv_rsplen = 0;
James Smart62eeacb2017-03-23 20:41:27 -07001988 op->fcp_req.status = NVME_SC_SUCCESS;
James Smarte3994412016-12-02 00:28:42 -08001989 op->fcp_req.sqid = cpu_to_le16(queue->qnum);
1990
1991 /*
1992 * validate per fabric rules, set fields mandated by fabric spec
1993 * as well as those by FC-NVME spec.
1994 */
1995 WARN_ON_ONCE(sqe->common.metadata);
James Smarte3994412016-12-02 00:28:42 -08001996 sqe->common.flags |= NVME_CMD_SGL_METABUF;
1997
1998 /*
James Smartd9d34c02017-09-07 13:20:24 -07001999 * format SQE DPTR field per FC-NVME rules:
2000 * type=0x5 Transport SGL Data Block Descriptor
2001 * subtype=0xA Transport-specific value
2002 * address=0
2003 * length=length of the data series
James Smarte3994412016-12-02 00:28:42 -08002004 */
James Smartd9d34c02017-09-07 13:20:24 -07002005 sqe->rw.dptr.sgl.type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2006 NVME_SGL_FMT_TRANSPORT_A;
James Smarte3994412016-12-02 00:28:42 -08002007 sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
2008 sqe->rw.dptr.sgl.addr = 0;
2009
James Smart78a7ac22017-04-23 08:30:07 -07002010 if (!(op->flags & FCOP_FLAGS_AEN)) {
James Smarte3994412016-12-02 00:28:42 -08002011 ret = nvme_fc_map_data(ctrl, op->rq, op);
2012 if (ret < 0) {
James Smarte3994412016-12-02 00:28:42 -08002013 nvme_cleanup_cmd(op->rq);
2014 nvme_fc_ctrl_put(ctrl);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002015 if (ret == -ENOMEM || ret == -EAGAIN)
2016 return BLK_STS_RESOURCE;
2017 return BLK_STS_IOERR;
James Smarte3994412016-12-02 00:28:42 -08002018 }
2019 }
2020
2021 fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
2022 sizeof(op->cmd_iu), DMA_TO_DEVICE);
2023
2024 atomic_set(&op->state, FCPOP_STATE_ACTIVE);
2025
James Smart78a7ac22017-04-23 08:30:07 -07002026 if (!(op->flags & FCOP_FLAGS_AEN))
James Smarte3994412016-12-02 00:28:42 -08002027 blk_mq_start_request(op->rq);
2028
2029 ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
2030 &ctrl->rport->remoteport,
2031 queue->lldd_handle, &op->fcp_req);
2032
2033 if (ret) {
James Smart8b25f352017-07-18 14:29:34 -07002034 if (!(op->flags & FCOP_FLAGS_AEN))
James Smarte3994412016-12-02 00:28:42 -08002035 nvme_fc_unmap_data(ctrl, op->rq, op);
James Smarte3994412016-12-02 00:28:42 -08002036
2037 nvme_fc_ctrl_put(ctrl);
2038
James Smart8b25f352017-07-18 14:29:34 -07002039 if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE &&
2040 ret != -EBUSY)
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002041 return BLK_STS_IOERR;
James Smarte3994412016-12-02 00:28:42 -08002042
James Smart8b25f352017-07-18 14:29:34 -07002043 goto busy;
James Smarte3994412016-12-02 00:28:42 -08002044 }
2045
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002046 return BLK_STS_OK;
James Smart8b25f352017-07-18 14:29:34 -07002047
2048busy:
2049 if (!(op->flags & FCOP_FLAGS_AEN) && queue->hctx)
2050 blk_mq_delay_run_hw_queue(queue->hctx, NVMEFC_QUEUE_DELAY);
2051
2052 return BLK_STS_RESOURCE;
James Smarte3994412016-12-02 00:28:42 -08002053}
2054
Sagi Grimbergdb2044f2017-10-24 15:25:21 +03002055static inline blk_status_t nvme_fc_is_ready(struct nvme_fc_queue *queue,
2056 struct request *rq)
2057{
2058 if (unlikely(!test_bit(NVME_FC_Q_LIVE, &queue->flags)))
2059 return nvmf_check_init_req(&queue->ctrl->ctrl, rq);
2060 return BLK_STS_OK;
2061}
2062
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002063static blk_status_t
James Smarte3994412016-12-02 00:28:42 -08002064nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
2065 const struct blk_mq_queue_data *bd)
2066{
2067 struct nvme_ns *ns = hctx->queue->queuedata;
2068 struct nvme_fc_queue *queue = hctx->driver_data;
2069 struct nvme_fc_ctrl *ctrl = queue->ctrl;
2070 struct request *rq = bd->rq;
2071 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2072 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2073 struct nvme_command *sqe = &cmdiu->sqe;
2074 enum nvmefc_fcp_datadir io_dir;
2075 u32 data_len;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002076 blk_status_t ret;
James Smarte3994412016-12-02 00:28:42 -08002077
Sagi Grimbergdb2044f2017-10-24 15:25:21 +03002078 ret = nvme_fc_is_ready(queue, rq);
2079 if (unlikely(ret))
2080 return ret;
2081
James Smarte3994412016-12-02 00:28:42 -08002082 ret = nvme_setup_cmd(ns, rq, sqe);
2083 if (ret)
2084 return ret;
2085
Christoph Hellwigb131c612017-01-13 12:29:12 +01002086 data_len = blk_rq_payload_bytes(rq);
James Smarte3994412016-12-02 00:28:42 -08002087 if (data_len)
2088 io_dir = ((rq_data_dir(rq) == WRITE) ?
2089 NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
2090 else
2091 io_dir = NVMEFC_FCP_NODATA;
2092
2093 return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
2094}
2095
2096static struct blk_mq_tags *
2097nvme_fc_tagset(struct nvme_fc_queue *queue)
2098{
2099 if (queue->qnum == 0)
2100 return queue->ctrl->admin_tag_set.tags[queue->qnum];
2101
2102 return queue->ctrl->tag_set.tags[queue->qnum - 1];
2103}
2104
2105static int
2106nvme_fc_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
2107
2108{
2109 struct nvme_fc_queue *queue = hctx->driver_data;
2110 struct nvme_fc_ctrl *ctrl = queue->ctrl;
2111 struct request *req;
2112 struct nvme_fc_fcp_op *op;
2113
2114 req = blk_mq_tag_to_rq(nvme_fc_tagset(queue), tag);
James Smart61bff8e2017-04-23 08:30:08 -07002115 if (!req)
James Smarte3994412016-12-02 00:28:42 -08002116 return 0;
James Smarte3994412016-12-02 00:28:42 -08002117
2118 op = blk_mq_rq_to_pdu(req);
2119
2120 if ((atomic_read(&op->state) == FCPOP_STATE_ACTIVE) &&
2121 (ctrl->lport->ops->poll_queue))
2122 ctrl->lport->ops->poll_queue(&ctrl->lport->localport,
2123 queue->lldd_handle);
2124
2125 return ((atomic_read(&op->state) != FCPOP_STATE_ACTIVE));
2126}
2127
2128static void
2129nvme_fc_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
2130{
2131 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
2132 struct nvme_fc_fcp_op *aen_op;
James Smart61bff8e2017-04-23 08:30:08 -07002133 unsigned long flags;
2134 bool terminating = false;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002135 blk_status_t ret;
James Smarte3994412016-12-02 00:28:42 -08002136
2137 if (aer_idx > NVME_FC_NR_AEN_COMMANDS)
2138 return;
2139
James Smart61bff8e2017-04-23 08:30:08 -07002140 spin_lock_irqsave(&ctrl->lock, flags);
2141 if (ctrl->flags & FCCTRL_TERMIO)
2142 terminating = true;
2143 spin_unlock_irqrestore(&ctrl->lock, flags);
2144
2145 if (terminating)
2146 return;
2147
James Smarte3994412016-12-02 00:28:42 -08002148 aen_op = &ctrl->aen_ops[aer_idx];
2149
2150 ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
2151 NVMEFC_FCP_NODATA);
2152 if (ret)
2153 dev_err(ctrl->ctrl.device,
2154 "failed async event work [%d]\n", aer_idx);
2155}
2156
2157static void
James Smart78a7ac22017-04-23 08:30:07 -07002158__nvme_fc_final_op_cleanup(struct request *rq)
James Smarte3994412016-12-02 00:28:42 -08002159{
2160 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2161 struct nvme_fc_ctrl *ctrl = op->ctrl;
James Smarte3994412016-12-02 00:28:42 -08002162
James Smart78a7ac22017-04-23 08:30:07 -07002163 atomic_set(&op->state, FCPOP_STATE_IDLE);
2164 op->flags &= ~(FCOP_FLAGS_TERMIO | FCOP_FLAGS_RELEASED |
2165 FCOP_FLAGS_COMPLETE);
James Smarte3994412016-12-02 00:28:42 -08002166
James Smarte3994412016-12-02 00:28:42 -08002167 nvme_fc_unmap_data(ctrl, rq, op);
Christoph Hellwig77f02a72017-03-30 13:41:32 +02002168 nvme_complete_rq(rq);
James Smarte3994412016-12-02 00:28:42 -08002169 nvme_fc_ctrl_put(ctrl);
2170
James Smarte3994412016-12-02 00:28:42 -08002171}
2172
James Smart78a7ac22017-04-23 08:30:07 -07002173static void
2174nvme_fc_complete_rq(struct request *rq)
2175{
2176 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2177 struct nvme_fc_ctrl *ctrl = op->ctrl;
2178 unsigned long flags;
2179 bool completed = false;
2180
2181 /*
2182 * the core layer, on controller resets after calling
2183 * nvme_shutdown_ctrl(), calls complete_rq without our
2184 * calling blk_mq_complete_request(), thus there may still
2185 * be live i/o outstanding with the LLDD. Means transport has
2186 * to track complete calls vs fcpio_done calls to know what
2187 * path to take on completes and dones.
2188 */
2189 spin_lock_irqsave(&ctrl->lock, flags);
2190 if (op->flags & FCOP_FLAGS_COMPLETE)
2191 completed = true;
2192 else
2193 op->flags |= FCOP_FLAGS_RELEASED;
2194 spin_unlock_irqrestore(&ctrl->lock, flags);
2195
2196 if (completed)
2197 __nvme_fc_final_op_cleanup(rq);
2198}
2199
James Smarte3994412016-12-02 00:28:42 -08002200/*
2201 * This routine is used by the transport when it needs to find active
2202 * io on a queue that is to be terminated. The transport uses
2203 * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
2204 * this routine to kill them on a 1 by 1 basis.
2205 *
2206 * As FC allocates FC exchange for each io, the transport must contact
2207 * the LLDD to terminate the exchange, thus releasing the FC exchange.
2208 * After terminating the exchange the LLDD will call the transport's
2209 * normal io done path for the request, but it will have an aborted
2210 * status. The done path will return the io request back to the block
2211 * layer with an error status.
2212 */
2213static void
2214nvme_fc_terminate_exchange(struct request *req, void *data, bool reserved)
2215{
2216 struct nvme_ctrl *nctrl = data;
2217 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2218 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
James Smart78a7ac22017-04-23 08:30:07 -07002219 unsigned long flags;
2220 int status;
James Smarte3994412016-12-02 00:28:42 -08002221
2222 if (!blk_mq_request_started(req))
2223 return;
2224
James Smart78a7ac22017-04-23 08:30:07 -07002225 spin_lock_irqsave(&ctrl->lock, flags);
James Smart61bff8e2017-04-23 08:30:08 -07002226 if (ctrl->flags & FCCTRL_TERMIO) {
2227 ctrl->iocnt++;
2228 op->flags |= FCOP_FLAGS_TERMIO;
2229 }
James Smart78a7ac22017-04-23 08:30:07 -07002230 spin_unlock_irqrestore(&ctrl->lock, flags);
James Smarte3994412016-12-02 00:28:42 -08002231
James Smart78a7ac22017-04-23 08:30:07 -07002232 status = __nvme_fc_abort_op(ctrl, op);
2233 if (status) {
2234 /*
2235 * if __nvme_fc_abort_op failed the io wasn't
2236 * active. Thus this call path is running in
2237 * parallel to the io complete. Treat as non-error.
2238 */
2239
2240 /* back out the flags/counters */
2241 spin_lock_irqsave(&ctrl->lock, flags);
James Smart61bff8e2017-04-23 08:30:08 -07002242 if (ctrl->flags & FCCTRL_TERMIO)
2243 ctrl->iocnt--;
James Smart78a7ac22017-04-23 08:30:07 -07002244 op->flags &= ~FCOP_FLAGS_TERMIO;
2245 spin_unlock_irqrestore(&ctrl->lock, flags);
2246 return;
2247 }
2248}
James Smarte3994412016-12-02 00:28:42 -08002249
James Smarte3994412016-12-02 00:28:42 -08002250
James Smart61bff8e2017-04-23 08:30:08 -07002251static const struct blk_mq_ops nvme_fc_mq_ops = {
2252 .queue_rq = nvme_fc_queue_rq,
2253 .complete = nvme_fc_complete_rq,
2254 .init_request = nvme_fc_init_request,
2255 .exit_request = nvme_fc_exit_request,
James Smart61bff8e2017-04-23 08:30:08 -07002256 .init_hctx = nvme_fc_init_hctx,
2257 .poll = nvme_fc_poll,
2258 .timeout = nvme_fc_timeout,
James Smarte3994412016-12-02 00:28:42 -08002259};
2260
2261static int
2262nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl)
2263{
2264 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
Sagi Grimberg73141832017-06-29 11:16:49 +03002265 unsigned int nr_io_queues;
James Smarte3994412016-12-02 00:28:42 -08002266 int ret;
2267
Sagi Grimberg73141832017-06-29 11:16:49 +03002268 nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
2269 ctrl->lport->ops->max_hw_queues);
2270 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
James Smarte3994412016-12-02 00:28:42 -08002271 if (ret) {
2272 dev_info(ctrl->ctrl.device,
2273 "set_queue_count failed: %d\n", ret);
2274 return ret;
2275 }
2276
Sagi Grimberg73141832017-06-29 11:16:49 +03002277 ctrl->ctrl.queue_count = nr_io_queues + 1;
2278 if (!nr_io_queues)
James Smarte3994412016-12-02 00:28:42 -08002279 return 0;
2280
James Smarte3994412016-12-02 00:28:42 -08002281 nvme_fc_init_io_queues(ctrl);
2282
2283 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
2284 ctrl->tag_set.ops = &nvme_fc_mq_ops;
2285 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
2286 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
2287 ctrl->tag_set.numa_node = NUMA_NO_NODE;
2288 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
2289 ctrl->tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) +
2290 (SG_CHUNK_SIZE *
2291 sizeof(struct scatterlist)) +
2292 ctrl->lport->ops->fcprqst_priv_sz;
2293 ctrl->tag_set.driver_data = ctrl;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002294 ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1;
James Smarte3994412016-12-02 00:28:42 -08002295 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
2296
2297 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
2298 if (ret)
2299 return ret;
2300
2301 ctrl->ctrl.tagset = &ctrl->tag_set;
2302
2303 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
2304 if (IS_ERR(ctrl->ctrl.connect_q)) {
2305 ret = PTR_ERR(ctrl->ctrl.connect_q);
2306 goto out_free_tag_set;
2307 }
2308
2309 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
2310 if (ret)
2311 goto out_cleanup_blk_queue;
2312
2313 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
2314 if (ret)
2315 goto out_delete_hw_queues;
2316
2317 return 0;
2318
2319out_delete_hw_queues:
2320 nvme_fc_delete_hw_io_queues(ctrl);
2321out_cleanup_blk_queue:
James Smarte3994412016-12-02 00:28:42 -08002322 blk_cleanup_queue(ctrl->ctrl.connect_q);
2323out_free_tag_set:
2324 blk_mq_free_tag_set(&ctrl->tag_set);
2325 nvme_fc_free_io_queues(ctrl);
2326
2327 /* force put free routine to ignore io queues */
2328 ctrl->ctrl.tagset = NULL;
2329
2330 return ret;
2331}
2332
James Smart61bff8e2017-04-23 08:30:08 -07002333static int
2334nvme_fc_reinit_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 Smart61bff8e2017-04-23 08:30:08 -07002338 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 Smart61bff8e2017-04-23 08:30:08 -07002343 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;
James Smart61bff8e2017-04-23 08:30:08 -07002350 /* check for io queues existing */
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002351 if (ctrl->ctrl.queue_count == 1)
James Smart61bff8e2017-04-23 08:30:08 -07002352 return 0;
2353
James Smart61bff8e2017-04-23 08:30:08 -07002354 nvme_fc_init_io_queues(ctrl);
2355
Bart Van Assched352ae22017-08-17 16:23:03 -07002356 ret = blk_mq_reinit_tagset(&ctrl->tag_set, nvme_fc_reinit_request);
James Smart61bff8e2017-04-23 08:30:08 -07002357 if (ret)
2358 goto out_free_io_queues;
2359
2360 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
2361 if (ret)
2362 goto out_free_io_queues;
2363
2364 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
2365 if (ret)
2366 goto out_delete_hw_queues;
2367
Sagi Grimbergcda5fd12017-06-29 11:20:10 +03002368 blk_mq_update_nr_hw_queues(&ctrl->tag_set, nr_io_queues);
2369
James Smart61bff8e2017-04-23 08:30:08 -07002370 return 0;
2371
2372out_delete_hw_queues:
2373 nvme_fc_delete_hw_io_queues(ctrl);
2374out_free_io_queues:
2375 nvme_fc_free_io_queues(ctrl);
2376 return ret;
2377}
2378
2379/*
2380 * This routine restarts the controller on the host side, and
2381 * on the link side, recreates the controller association.
2382 */
2383static int
2384nvme_fc_create_association(struct nvme_fc_ctrl *ctrl)
2385{
2386 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2387 u32 segs;
2388 int ret;
2389 bool changed;
2390
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +03002391 ++ctrl->ctrl.nr_reconnects;
James Smart61bff8e2017-04-23 08:30:08 -07002392
2393 /*
2394 * Create the admin queue
2395 */
2396
2397 nvme_fc_init_queue(ctrl, 0, NVME_FC_AQ_BLKMQ_DEPTH);
2398
2399 ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0,
2400 NVME_FC_AQ_BLKMQ_DEPTH);
2401 if (ret)
2402 goto out_free_queue;
2403
2404 ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0],
2405 NVME_FC_AQ_BLKMQ_DEPTH,
2406 (NVME_FC_AQ_BLKMQ_DEPTH / 4));
2407 if (ret)
2408 goto out_delete_hw_queue;
2409
2410 if (ctrl->ctrl.state != NVME_CTRL_NEW)
Sagi Grimbergf9c5af52017-07-02 15:39:34 +03002411 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
James Smart61bff8e2017-04-23 08:30:08 -07002412
2413 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
2414 if (ret)
2415 goto out_disconnect_admin_queue;
2416
Sagi Grimbergdb2044f2017-10-24 15:25:21 +03002417 set_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags);
2418
James Smart61bff8e2017-04-23 08:30:08 -07002419 /*
2420 * Check controller capabilities
2421 *
2422 * todo:- add code to check if ctrl attributes changed from
2423 * prior connection values
2424 */
2425
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03002426 ret = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->ctrl.cap);
James Smart61bff8e2017-04-23 08:30:08 -07002427 if (ret) {
2428 dev_err(ctrl->ctrl.device,
2429 "prop_get NVME_REG_CAP failed\n");
2430 goto out_disconnect_admin_queue;
2431 }
2432
2433 ctrl->ctrl.sqsize =
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03002434 min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap) + 1, ctrl->ctrl.sqsize);
James Smart61bff8e2017-04-23 08:30:08 -07002435
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03002436 ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
James Smart61bff8e2017-04-23 08:30:08 -07002437 if (ret)
2438 goto out_disconnect_admin_queue;
2439
2440 segs = min_t(u32, NVME_FC_MAX_SEGMENTS,
2441 ctrl->lport->ops->max_sgl_segments);
2442 ctrl->ctrl.max_hw_sectors = (segs - 1) << (PAGE_SHIFT - 9);
2443
2444 ret = nvme_init_identify(&ctrl->ctrl);
2445 if (ret)
2446 goto out_disconnect_admin_queue;
2447
2448 /* sanity checks */
2449
2450 /* FC-NVME does not have other data in the capsule */
2451 if (ctrl->ctrl.icdoff) {
2452 dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n",
2453 ctrl->ctrl.icdoff);
2454 goto out_disconnect_admin_queue;
2455 }
2456
James Smart61bff8e2017-04-23 08:30:08 -07002457 /* FC-NVME supports normal SGL Data Block Descriptors */
2458
2459 if (opts->queue_size > ctrl->ctrl.maxcmd) {
2460 /* warn if maxcmd is lower than queue_size */
2461 dev_warn(ctrl->ctrl.device,
2462 "queue_size %zu > ctrl maxcmd %u, reducing "
2463 "to queue_size\n",
2464 opts->queue_size, ctrl->ctrl.maxcmd);
2465 opts->queue_size = ctrl->ctrl.maxcmd;
2466 }
2467
2468 ret = nvme_fc_init_aen_ops(ctrl);
2469 if (ret)
2470 goto out_term_aen_ops;
2471
2472 /*
2473 * Create the io queues
2474 */
2475
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002476 if (ctrl->ctrl.queue_count > 1) {
James Smart61bff8e2017-04-23 08:30:08 -07002477 if (ctrl->ctrl.state == NVME_CTRL_NEW)
2478 ret = nvme_fc_create_io_queues(ctrl);
2479 else
2480 ret = nvme_fc_reinit_io_queues(ctrl);
2481 if (ret)
2482 goto out_term_aen_ops;
2483 }
2484
2485 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
2486 WARN_ON_ONCE(!changed);
2487
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +03002488 ctrl->ctrl.nr_reconnects = 0;
James Smart61bff8e2017-04-23 08:30:08 -07002489
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002490 nvme_start_ctrl(&ctrl->ctrl);
James Smart61bff8e2017-04-23 08:30:08 -07002491
2492 return 0; /* Success */
2493
2494out_term_aen_ops:
2495 nvme_fc_term_aen_ops(ctrl);
James Smart61bff8e2017-04-23 08:30:08 -07002496out_disconnect_admin_queue:
2497 /* send a Disconnect(association) LS to fc-nvme target */
2498 nvme_fc_xmt_disconnect_assoc(ctrl);
2499out_delete_hw_queue:
2500 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
2501out_free_queue:
2502 nvme_fc_free_queue(&ctrl->queues[0]);
2503
2504 return ret;
2505}
2506
2507/*
2508 * This routine stops operation of the controller on the host side.
2509 * On the host os stack side: Admin and IO queues are stopped,
2510 * outstanding ios on them terminated via FC ABTS.
2511 * On the link side: the association is terminated.
2512 */
2513static void
2514nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
2515{
2516 unsigned long flags;
2517
James Smart61bff8e2017-04-23 08:30:08 -07002518 spin_lock_irqsave(&ctrl->lock, flags);
2519 ctrl->flags |= FCCTRL_TERMIO;
2520 ctrl->iocnt = 0;
2521 spin_unlock_irqrestore(&ctrl->lock, flags);
2522
2523 /*
2524 * If io queues are present, stop them and terminate all outstanding
2525 * ios on them. As FC allocates FC exchange for each io, the
2526 * transport must contact the LLDD to terminate the exchange,
2527 * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
2528 * to tell us what io's are busy and invoke a transport routine
2529 * to kill them with the LLDD. After terminating the exchange
2530 * the LLDD will call the transport's normal io done path, but it
2531 * will have an aborted status. The done path will return the
2532 * io requests back to the block layer as part of normal completions
2533 * (but with error status).
2534 */
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002535 if (ctrl->ctrl.queue_count > 1) {
James Smart61bff8e2017-04-23 08:30:08 -07002536 nvme_stop_queues(&ctrl->ctrl);
2537 blk_mq_tagset_busy_iter(&ctrl->tag_set,
2538 nvme_fc_terminate_exchange, &ctrl->ctrl);
2539 }
2540
2541 /*
2542 * Other transports, which don't have link-level contexts bound
2543 * to sqe's, would try to gracefully shutdown the controller by
2544 * writing the registers for shutdown and polling (call
2545 * nvme_shutdown_ctrl()). Given a bunch of i/o was potentially
2546 * just aborted and we will wait on those contexts, and given
2547 * there was no indication of how live the controlelr is on the
2548 * link, don't send more io to create more contexts for the
2549 * shutdown. Let the controller fail via keepalive failure if
2550 * its still present.
2551 */
2552
2553 /*
2554 * clean up the admin queue. Same thing as above.
2555 * use blk_mq_tagset_busy_itr() and the transport routine to
2556 * terminate the exchanges.
2557 */
Sagi Grimbergf9c5af52017-07-02 15:39:34 +03002558 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
James Smart61bff8e2017-04-23 08:30:08 -07002559 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
2560 nvme_fc_terminate_exchange, &ctrl->ctrl);
2561
2562 /* kill the aens as they are a separate path */
2563 nvme_fc_abort_aen_ops(ctrl);
2564
2565 /* wait for all io that had to be aborted */
James Smart8a82dbf2017-10-09 13:39:44 -07002566 spin_lock_irq(&ctrl->lock);
James Smart36715cf2017-05-22 15:28:42 -07002567 wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock);
James Smart61bff8e2017-04-23 08:30:08 -07002568 ctrl->flags &= ~FCCTRL_TERMIO;
James Smart8a82dbf2017-10-09 13:39:44 -07002569 spin_unlock_irq(&ctrl->lock);
James Smart61bff8e2017-04-23 08:30:08 -07002570
2571 nvme_fc_term_aen_ops(ctrl);
2572
2573 /*
2574 * send a Disconnect(association) LS to fc-nvme target
2575 * Note: could have been sent at top of process, but
2576 * cleaner on link traffic if after the aborts complete.
2577 * Note: if association doesn't exist, association_id will be 0
2578 */
2579 if (ctrl->association_id)
2580 nvme_fc_xmt_disconnect_assoc(ctrl);
2581
2582 if (ctrl->ctrl.tagset) {
2583 nvme_fc_delete_hw_io_queues(ctrl);
2584 nvme_fc_free_io_queues(ctrl);
2585 }
2586
2587 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
2588 nvme_fc_free_queue(&ctrl->queues[0]);
2589}
2590
2591static void
2592nvme_fc_delete_ctrl_work(struct work_struct *work)
2593{
2594 struct nvme_fc_ctrl *ctrl =
2595 container_of(work, struct nvme_fc_ctrl, delete_work);
2596
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002597 cancel_work_sync(&ctrl->ctrl.reset_work);
James Smart61bff8e2017-04-23 08:30:08 -07002598 cancel_delayed_work_sync(&ctrl->connect_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002599 nvme_stop_ctrl(&ctrl->ctrl);
2600 nvme_remove_namespaces(&ctrl->ctrl);
James Smart61bff8e2017-04-23 08:30:08 -07002601 /*
2602 * kill the association on the link side. this will block
2603 * waiting for io to terminate
2604 */
2605 nvme_fc_delete_association(ctrl);
2606
2607 /*
2608 * tear down the controller
James Smarta5321aa2017-05-15 17:10:18 -07002609 * After the last reference on the nvme ctrl is removed,
2610 * the transport nvme_fc_nvme_ctrl_freed() callback will be
2611 * invoked. From there, the transport will tear down it's
2612 * logical queues and association.
James Smart61bff8e2017-04-23 08:30:08 -07002613 */
2614 nvme_uninit_ctrl(&ctrl->ctrl);
2615
2616 nvme_put_ctrl(&ctrl->ctrl);
2617}
2618
James Smart5bbecdb2017-05-15 17:10:16 -07002619static bool
2620__nvme_fc_schedule_delete_work(struct nvme_fc_ctrl *ctrl)
2621{
2622 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
2623 return true;
2624
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002625 if (!queue_work(nvme_wq, &ctrl->delete_work))
James Smart5bbecdb2017-05-15 17:10:16 -07002626 return true;
2627
2628 return false;
2629}
2630
James Smart61bff8e2017-04-23 08:30:08 -07002631static int
2632__nvme_fc_del_ctrl(struct nvme_fc_ctrl *ctrl)
2633{
James Smart5bbecdb2017-05-15 17:10:16 -07002634 return __nvme_fc_schedule_delete_work(ctrl) ? -EBUSY : 0;
James Smart61bff8e2017-04-23 08:30:08 -07002635}
2636
2637/*
2638 * Request from nvme core layer to delete the controller
2639 */
2640static int
2641nvme_fc_del_nvme_ctrl(struct nvme_ctrl *nctrl)
2642{
2643 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2644 int ret;
2645
2646 if (!kref_get_unless_zero(&ctrl->ctrl.kref))
2647 return -EBUSY;
2648
2649 ret = __nvme_fc_del_ctrl(ctrl);
2650
2651 if (!ret)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002652 flush_workqueue(nvme_wq);
James Smart61bff8e2017-04-23 08:30:08 -07002653
2654 nvme_put_ctrl(&ctrl->ctrl);
2655
2656 return ret;
2657}
2658
2659static void
James Smart5bbecdb2017-05-15 17:10:16 -07002660nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status)
2661{
2662 /* If we are resetting/deleting then do nothing */
2663 if (ctrl->ctrl.state != NVME_CTRL_RECONNECTING) {
2664 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
2665 ctrl->ctrl.state == NVME_CTRL_LIVE);
2666 return;
2667 }
2668
James Smart589ff772017-05-15 17:10:19 -07002669 dev_info(ctrl->ctrl.device,
James Smart5bbecdb2017-05-15 17:10:16 -07002670 "NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
2671 ctrl->cnum, status);
2672
2673 if (nvmf_should_reconnect(&ctrl->ctrl)) {
2674 dev_info(ctrl->ctrl.device,
2675 "NVME-FC{%d}: Reconnect attempt in %d seconds.\n",
2676 ctrl->cnum, ctrl->ctrl.opts->reconnect_delay);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002677 queue_delayed_work(nvme_wq, &ctrl->connect_work,
James Smart5bbecdb2017-05-15 17:10:16 -07002678 ctrl->ctrl.opts->reconnect_delay * HZ);
2679 } else {
James Smart589ff772017-05-15 17:10:19 -07002680 dev_warn(ctrl->ctrl.device,
James Smart5bbecdb2017-05-15 17:10:16 -07002681 "NVME-FC{%d}: Max reconnect attempts (%d) "
2682 "reached. Removing controller\n",
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +03002683 ctrl->cnum, ctrl->ctrl.nr_reconnects);
James Smart5bbecdb2017-05-15 17:10:16 -07002684 WARN_ON(__nvme_fc_schedule_delete_work(ctrl));
2685 }
2686}
2687
2688static void
James Smart61bff8e2017-04-23 08:30:08 -07002689nvme_fc_reset_ctrl_work(struct work_struct *work)
2690{
2691 struct nvme_fc_ctrl *ctrl =
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002692 container_of(work, struct nvme_fc_ctrl, ctrl.reset_work);
James Smart61bff8e2017-04-23 08:30:08 -07002693 int ret;
2694
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002695 nvme_stop_ctrl(&ctrl->ctrl);
James Smart61bff8e2017-04-23 08:30:08 -07002696 /* will block will waiting for io to terminate */
2697 nvme_fc_delete_association(ctrl);
2698
2699 ret = nvme_fc_create_association(ctrl);
James Smart5bbecdb2017-05-15 17:10:16 -07002700 if (ret)
2701 nvme_fc_reconnect_or_delete(ctrl, ret);
2702 else
James Smart61bff8e2017-04-23 08:30:08 -07002703 dev_info(ctrl->ctrl.device,
2704 "NVME-FC{%d}: controller reset complete\n", ctrl->cnum);
2705}
2706
James Smart61bff8e2017-04-23 08:30:08 -07002707static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
2708 .name = "fc",
2709 .module = THIS_MODULE,
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02002710 .flags = NVME_F_FABRICS,
James Smart61bff8e2017-04-23 08:30:08 -07002711 .reg_read32 = nvmf_reg_read32,
2712 .reg_read64 = nvmf_reg_read64,
2713 .reg_write32 = nvmf_reg_write32,
James Smart61bff8e2017-04-23 08:30:08 -07002714 .free_ctrl = nvme_fc_nvme_ctrl_freed,
2715 .submit_async_event = nvme_fc_submit_async_event,
2716 .delete_ctrl = nvme_fc_del_nvme_ctrl,
James Smart61bff8e2017-04-23 08:30:08 -07002717 .get_address = nvmf_get_address,
2718};
2719
2720static void
2721nvme_fc_connect_ctrl_work(struct work_struct *work)
2722{
2723 int ret;
2724
2725 struct nvme_fc_ctrl *ctrl =
2726 container_of(to_delayed_work(work),
2727 struct nvme_fc_ctrl, connect_work);
2728
2729 ret = nvme_fc_create_association(ctrl);
James Smart5bbecdb2017-05-15 17:10:16 -07002730 if (ret)
2731 nvme_fc_reconnect_or_delete(ctrl, ret);
2732 else
James Smart61bff8e2017-04-23 08:30:08 -07002733 dev_info(ctrl->ctrl.device,
2734 "NVME-FC{%d}: controller reconnect complete\n",
2735 ctrl->cnum);
2736}
2737
2738
2739static const struct blk_mq_ops nvme_fc_admin_mq_ops = {
2740 .queue_rq = nvme_fc_queue_rq,
2741 .complete = nvme_fc_complete_rq,
Christoph Hellwig76f983c2017-06-13 09:15:20 +02002742 .init_request = nvme_fc_init_request,
James Smart61bff8e2017-04-23 08:30:08 -07002743 .exit_request = nvme_fc_exit_request,
James Smart61bff8e2017-04-23 08:30:08 -07002744 .init_hctx = nvme_fc_init_admin_hctx,
2745 .timeout = nvme_fc_timeout,
2746};
2747
James Smarte3994412016-12-02 00:28:42 -08002748
2749static struct nvme_ctrl *
James Smart61bff8e2017-04-23 08:30:08 -07002750nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
James Smarte3994412016-12-02 00:28:42 -08002751 struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
2752{
2753 struct nvme_fc_ctrl *ctrl;
2754 unsigned long flags;
James Smart17c4dc62017-10-09 16:39:22 -07002755 int ret, idx, retry;
James Smarte3994412016-12-02 00:28:42 -08002756
James Smart85e6a6a2017-05-05 16:13:15 -07002757 if (!(rport->remoteport.port_role &
2758 (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) {
2759 ret = -EBADR;
2760 goto out_fail;
2761 }
2762
James Smarte3994412016-12-02 00:28:42 -08002763 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
2764 if (!ctrl) {
2765 ret = -ENOMEM;
2766 goto out_fail;
2767 }
2768
2769 idx = ida_simple_get(&nvme_fc_ctrl_cnt, 0, 0, GFP_KERNEL);
2770 if (idx < 0) {
2771 ret = -ENOSPC;
James Smart2e680192020-04-03 07:33:20 -07002772 goto out_free_ctrl;
James Smarte3994412016-12-02 00:28:42 -08002773 }
2774
2775 ctrl->ctrl.opts = opts;
2776 INIT_LIST_HEAD(&ctrl->ctrl_list);
James Smarte3994412016-12-02 00:28:42 -08002777 ctrl->lport = lport;
2778 ctrl->rport = rport;
2779 ctrl->dev = lport->dev;
James Smarte3994412016-12-02 00:28:42 -08002780 ctrl->cnum = idx;
James Smart8a82dbf2017-10-09 13:39:44 -07002781 init_waitqueue_head(&ctrl->ioabort_wait);
James Smarte3994412016-12-02 00:28:42 -08002782
James Smarte3994412016-12-02 00:28:42 -08002783 get_device(ctrl->dev);
2784 kref_init(&ctrl->ref);
2785
James Smart61bff8e2017-04-23 08:30:08 -07002786 INIT_WORK(&ctrl->delete_work, nvme_fc_delete_ctrl_work);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002787 INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work);
James Smart61bff8e2017-04-23 08:30:08 -07002788 INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
James Smarte3994412016-12-02 00:28:42 -08002789 spin_lock_init(&ctrl->lock);
2790
2791 /* io queue count */
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002792 ctrl->ctrl.queue_count = min_t(unsigned int,
James Smarte3994412016-12-02 00:28:42 -08002793 opts->nr_io_queues,
2794 lport->ops->max_hw_queues);
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002795 ctrl->ctrl.queue_count++; /* +1 for admin queue */
James Smarte3994412016-12-02 00:28:42 -08002796
2797 ctrl->ctrl.sqsize = opts->queue_size - 1;
2798 ctrl->ctrl.kato = opts->kato;
2799
2800 ret = -ENOMEM;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002801 ctrl->queues = kcalloc(ctrl->ctrl.queue_count,
2802 sizeof(struct nvme_fc_queue), GFP_KERNEL);
James Smarte3994412016-12-02 00:28:42 -08002803 if (!ctrl->queues)
James Smart61bff8e2017-04-23 08:30:08 -07002804 goto out_free_ida;
James Smarte3994412016-12-02 00:28:42 -08002805
James Smart61bff8e2017-04-23 08:30:08 -07002806 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
2807 ctrl->admin_tag_set.ops = &nvme_fc_admin_mq_ops;
2808 ctrl->admin_tag_set.queue_depth = NVME_FC_AQ_BLKMQ_DEPTH;
2809 ctrl->admin_tag_set.reserved_tags = 2; /* fabric connect + Keep-Alive */
2810 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
2811 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) +
2812 (SG_CHUNK_SIZE *
2813 sizeof(struct scatterlist)) +
2814 ctrl->lport->ops->fcprqst_priv_sz;
2815 ctrl->admin_tag_set.driver_data = ctrl;
2816 ctrl->admin_tag_set.nr_hw_queues = 1;
2817 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
2818
2819 ret = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
James Smarte3994412016-12-02 00:28:42 -08002820 if (ret)
James Smart61bff8e2017-04-23 08:30:08 -07002821 goto out_free_queues;
Sagi Grimberg34b6c232017-07-10 09:22:29 +03002822 ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set;
James Smarte3994412016-12-02 00:28:42 -08002823
James Smart61bff8e2017-04-23 08:30:08 -07002824 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
2825 if (IS_ERR(ctrl->ctrl.admin_q)) {
2826 ret = PTR_ERR(ctrl->ctrl.admin_q);
2827 goto out_free_admin_tag_set;
James Smarte3994412016-12-02 00:28:42 -08002828 }
2829
James Smart61bff8e2017-04-23 08:30:08 -07002830 /*
2831 * Would have been nice to init io queues tag set as well.
2832 * However, we require interaction from the controller
2833 * for max io queue count before we can do so.
2834 * Defer this to the connect path.
2835 */
James Smarte3994412016-12-02 00:28:42 -08002836
James Smart61bff8e2017-04-23 08:30:08 -07002837 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
James Smarte3994412016-12-02 00:28:42 -08002838 if (ret)
James Smart61bff8e2017-04-23 08:30:08 -07002839 goto out_cleanup_admin_q;
James Smarte3994412016-12-02 00:28:42 -08002840
James Smart61bff8e2017-04-23 08:30:08 -07002841 /* at this point, teardown path changes to ref counting on nvme ctrl */
James Smarte3994412016-12-02 00:28:42 -08002842
2843 spin_lock_irqsave(&rport->lock, flags);
2844 list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
2845 spin_unlock_irqrestore(&rport->lock, flags);
2846
James Smart17c4dc62017-10-09 16:39:22 -07002847 /*
2848 * It's possible that transactions used to create the association
2849 * may fail. Examples: CreateAssociation LS or CreateIOConnection
2850 * LS gets dropped/corrupted/fails; or a frame gets dropped or a
2851 * command times out for one of the actions to init the controller
2852 * (Connect, Get/Set_Property, Set_Features, etc). Many of these
2853 * transport errors (frame drop, LS failure) inherently must kill
2854 * the association. The transport is coded so that any command used
2855 * to create the association (prior to a LIVE state transition
2856 * while NEW or RECONNECTING) will fail if it completes in error or
2857 * times out.
2858 *
2859 * As such: as the connect request was mostly likely due to a
2860 * udev event that discovered the remote port, meaning there is
2861 * not an admin or script there to restart if the connect
2862 * request fails, retry the initial connection creation up to
2863 * three times before giving up and declaring failure.
2864 */
2865 for (retry = 0; retry < 3; retry++) {
2866 ret = nvme_fc_create_association(ctrl);
2867 if (!ret)
2868 break;
2869 }
2870
James Smart61bff8e2017-04-23 08:30:08 -07002871 if (ret) {
James Smart0f6e2f42018-03-13 09:48:07 -07002872 nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING);
2873 cancel_work_sync(&ctrl->ctrl.reset_work);
2874 cancel_delayed_work_sync(&ctrl->connect_work);
2875
James Smart17c4dc62017-10-09 16:39:22 -07002876 /* couldn't schedule retry - fail out */
2877 dev_err(ctrl->ctrl.device,
2878 "NVME-FC{%d}: Connect retry failed\n", ctrl->cnum);
2879
Ewan D. Milnede414472017-04-24 13:24:16 -04002880 ctrl->ctrl.opts = NULL;
James Smart17c4dc62017-10-09 16:39:22 -07002881
James Smart61bff8e2017-04-23 08:30:08 -07002882 /* initiate nvme ctrl ref counting teardown */
2883 nvme_uninit_ctrl(&ctrl->ctrl);
James Smart61bff8e2017-04-23 08:30:08 -07002884
James Smart0b5a7662017-06-15 23:40:54 -07002885 /* Remove core ctrl ref. */
2886 nvme_put_ctrl(&ctrl->ctrl);
2887
James Smart61bff8e2017-04-23 08:30:08 -07002888 /* as we're past the point where we transition to the ref
2889 * counting teardown path, if we return a bad pointer here,
2890 * the calling routine, thinking it's prior to the
2891 * transition, will do an rport put. Since the teardown
2892 * path also does a rport put, we do an extra get here to
2893 * so proper order/teardown happens.
2894 */
2895 nvme_fc_rport_get(rport);
2896
2897 if (ret > 0)
2898 ret = -EIO;
2899 return ERR_PTR(ret);
James Smarte3994412016-12-02 00:28:42 -08002900 }
2901
James Smart2cb657b2017-05-15 17:10:22 -07002902 kref_get(&ctrl->ctrl.kref);
2903
James Smart61bff8e2017-04-23 08:30:08 -07002904 dev_info(ctrl->ctrl.device,
2905 "NVME-FC{%d}: new ctrl: NQN \"%s\"\n",
2906 ctrl->cnum, ctrl->ctrl.opts->subsysnqn);
2907
James Smarte3994412016-12-02 00:28:42 -08002908 return &ctrl->ctrl;
2909
James Smart61bff8e2017-04-23 08:30:08 -07002910out_cleanup_admin_q:
2911 blk_cleanup_queue(ctrl->ctrl.admin_q);
2912out_free_admin_tag_set:
2913 blk_mq_free_tag_set(&ctrl->admin_tag_set);
2914out_free_queues:
2915 kfree(ctrl->queues);
James Smarte3994412016-12-02 00:28:42 -08002916out_free_ida:
James Smart61bff8e2017-04-23 08:30:08 -07002917 put_device(ctrl->dev);
James Smarte3994412016-12-02 00:28:42 -08002918 ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
2919out_free_ctrl:
2920 kfree(ctrl);
2921out_fail:
James Smarte3994412016-12-02 00:28:42 -08002922 /* exit via here doesn't follow ctlr ref points */
2923 return ERR_PTR(ret);
2924}
2925
James Smarte3994412016-12-02 00:28:42 -08002926
2927struct nvmet_fc_traddr {
2928 u64 nn;
2929 u64 pn;
2930};
2931
James Smarte3994412016-12-02 00:28:42 -08002932static int
James Smart9c5358e2017-07-17 13:59:39 -07002933__nvme_fc_parse_u64(substring_t *sstr, u64 *val)
James Smarte3994412016-12-02 00:28:42 -08002934{
James Smarte3994412016-12-02 00:28:42 -08002935 u64 token64;
2936
James Smart9c5358e2017-07-17 13:59:39 -07002937 if (match_u64(sstr, &token64))
2938 return -EINVAL;
2939 *val = token64;
James Smarte3994412016-12-02 00:28:42 -08002940
James Smart9c5358e2017-07-17 13:59:39 -07002941 return 0;
2942}
James Smarte3994412016-12-02 00:28:42 -08002943
James Smart9c5358e2017-07-17 13:59:39 -07002944/*
2945 * This routine validates and extracts the WWN's from the TRADDR string.
2946 * As kernel parsers need the 0x to determine number base, universally
2947 * build string to parse with 0x prefix before parsing name strings.
2948 */
2949static int
2950nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen)
2951{
2952 char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1];
2953 substring_t wwn = { name, &name[sizeof(name)-1] };
2954 int nnoffset, pnoffset;
James Smarte3994412016-12-02 00:28:42 -08002955
James Smart9c5358e2017-07-17 13:59:39 -07002956 /* validate it string one of the 2 allowed formats */
2957 if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH &&
2958 !strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) &&
2959 !strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET],
2960 "pn-0x", NVME_FC_TRADDR_OXNNLEN)) {
2961 nnoffset = NVME_FC_TRADDR_OXNNLEN;
2962 pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET +
2963 NVME_FC_TRADDR_OXNNLEN;
2964 } else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH &&
2965 !strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) &&
2966 !strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET],
2967 "pn-", NVME_FC_TRADDR_NNLEN))) {
2968 nnoffset = NVME_FC_TRADDR_NNLEN;
2969 pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN;
2970 } else
2971 goto out_einval;
2972
2973 name[0] = '0';
2974 name[1] = 'x';
2975 name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0;
2976
2977 memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN);
2978 if (__nvme_fc_parse_u64(&wwn, &traddr->nn))
2979 goto out_einval;
2980
2981 memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN);
2982 if (__nvme_fc_parse_u64(&wwn, &traddr->pn))
2983 goto out_einval;
2984
2985 return 0;
2986
2987out_einval:
2988 pr_warn("%s: bad traddr string\n", __func__);
2989 return -EINVAL;
James Smarte3994412016-12-02 00:28:42 -08002990}
2991
2992static struct nvme_ctrl *
2993nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
2994{
2995 struct nvme_fc_lport *lport;
2996 struct nvme_fc_rport *rport;
James Smart61bff8e2017-04-23 08:30:08 -07002997 struct nvme_ctrl *ctrl;
James Smarte3994412016-12-02 00:28:42 -08002998 struct nvmet_fc_traddr laddr = { 0L, 0L };
2999 struct nvmet_fc_traddr raddr = { 0L, 0L };
3000 unsigned long flags;
3001 int ret;
3002
James Smart9c5358e2017-07-17 13:59:39 -07003003 ret = nvme_fc_parse_traddr(&raddr, opts->traddr, NVMF_TRADDR_SIZE);
James Smarte3994412016-12-02 00:28:42 -08003004 if (ret || !raddr.nn || !raddr.pn)
3005 return ERR_PTR(-EINVAL);
3006
James Smart9c5358e2017-07-17 13:59:39 -07003007 ret = nvme_fc_parse_traddr(&laddr, opts->host_traddr, NVMF_TRADDR_SIZE);
James Smarte3994412016-12-02 00:28:42 -08003008 if (ret || !laddr.nn || !laddr.pn)
3009 return ERR_PTR(-EINVAL);
3010
3011 /* find the host and remote ports to connect together */
3012 spin_lock_irqsave(&nvme_fc_lock, flags);
3013 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3014 if (lport->localport.node_name != laddr.nn ||
James Smartd32430f2020-09-17 13:33:22 -07003015 lport->localport.port_name != laddr.pn ||
3016 lport->localport.port_state != FC_OBJSTATE_ONLINE)
James Smarte3994412016-12-02 00:28:42 -08003017 continue;
3018
3019 list_for_each_entry(rport, &lport->endp_list, endp_list) {
3020 if (rport->remoteport.node_name != raddr.nn ||
James Smartd32430f2020-09-17 13:33:22 -07003021 rport->remoteport.port_name != raddr.pn ||
3022 rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
James Smarte3994412016-12-02 00:28:42 -08003023 continue;
3024
3025 /* if fail to get reference fall through. Will error */
3026 if (!nvme_fc_rport_get(rport))
3027 break;
3028
3029 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3030
James Smart61bff8e2017-04-23 08:30:08 -07003031 ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport);
3032 if (IS_ERR(ctrl))
3033 nvme_fc_rport_put(rport);
3034 return ctrl;
James Smarte3994412016-12-02 00:28:42 -08003035 }
3036 }
3037 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3038
3039 return ERR_PTR(-ENOENT);
3040}
3041
3042
3043static struct nvmf_transport_ops nvme_fc_transport = {
3044 .name = "fc",
3045 .required_opts = NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
James Smart5bbecdb2017-05-15 17:10:16 -07003046 .allowed_opts = NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO,
James Smarte3994412016-12-02 00:28:42 -08003047 .create_ctrl = nvme_fc_create_ctrl,
3048};
3049
3050static int __init nvme_fc_init_module(void)
3051{
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003052 return nvmf_register_transport(&nvme_fc_transport);
James Smarte3994412016-12-02 00:28:42 -08003053}
3054
3055static void __exit nvme_fc_exit_module(void)
3056{
3057 /* sanity check - all lports should be removed */
3058 if (!list_empty(&nvme_fc_lport_list))
3059 pr_warn("%s: localport list not empty\n", __func__);
3060
3061 nvmf_unregister_transport(&nvme_fc_transport);
3062
James Smarte3994412016-12-02 00:28:42 -08003063 ida_destroy(&nvme_fc_local_port_cnt);
3064 ida_destroy(&nvme_fc_ctrl_cnt);
3065}
3066
3067module_init(nvme_fc_init_module);
3068module_exit(nvme_fc_exit_module);
3069
3070MODULE_LICENSE("GPL v2");