1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2016 Avago Technologies. All rights reserved.
4 */
5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6 #include <linux/module.h>
7 #include <linux/parser.h>
8 #include <uapi/scsi/fc/fc_fs.h>
9 #include <uapi/scsi/fc/fc_els.h>
10 #include <linux/delay.h>
11 #include <linux/overflow.h>
12 #include <linux/blk-cgroup.h>
13 #include "nvme.h"
14 #include "fabrics.h"
15 #include <linux/nvme-fc-driver.h>
16 #include <linux/nvme-fc.h>
17 #include "fc.h"
18 #include <scsi/scsi_transport_fc.h>
19
20 /* *************************** Data Structures/Defines ****************** */
21
22
23 enum nvme_fc_queue_flags {
24 NVME_FC_Q_CONNECTED = 0,
25 NVME_FC_Q_LIVE,
26 };
27
28 #define NVME_FC_DEFAULT_DEV_LOSS_TMO 60 /* seconds */
29 #define NVME_FC_DEFAULT_RECONNECT_TMO 2 /* delay between reconnects
30 * when connected and a
31 * connection failure.
32 */
33
34 struct nvme_fc_queue {
35 struct nvme_fc_ctrl *ctrl;
36 struct device *dev;
37 struct blk_mq_hw_ctx *hctx;
38 void *lldd_handle;
39 size_t cmnd_capsule_len;
40 u32 qnum;
41 u32 rqcnt;
42 u32 seqno;
43
44 u64 connection_id;
45 atomic_t csn;
46
47 unsigned long flags;
48 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
49
50 enum nvme_fcop_flags {
51 FCOP_FLAGS_TERMIO = (1 << 0),
52 FCOP_FLAGS_AEN = (1 << 1),
53 };
54
55 struct nvmefc_ls_req_op {
56 struct nvmefc_ls_req ls_req;
57
58 struct nvme_fc_rport *rport;
59 struct nvme_fc_queue *queue;
60 struct request *rq;
61 u32 flags;
62
63 int ls_error;
64 struct completion ls_done;
65 struct list_head lsreq_list; /* rport->ls_req_list */
66 bool req_queued;
67 };
68
69 struct nvmefc_ls_rcv_op {
70 struct nvme_fc_rport *rport;
71 struct nvmefc_ls_rsp *lsrsp;
72 union nvmefc_ls_requests *rqstbuf;
73 union nvmefc_ls_responses *rspbuf;
74 u16 rqstdatalen;
75 bool handled;
76 dma_addr_t rspdma;
77 struct list_head lsrcv_list; /* rport->ls_rcv_list */
78 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
79
80 enum nvme_fcpop_state {
81 FCPOP_STATE_UNINIT = 0,
82 FCPOP_STATE_IDLE = 1,
83 FCPOP_STATE_ACTIVE = 2,
84 FCPOP_STATE_ABORTED = 3,
85 FCPOP_STATE_COMPLETE = 4,
86 };
87
88 struct nvme_fc_fcp_op {
89 struct nvme_request nreq; /*
90 * nvme/host/core.c
91 * requires this to be
92 * the 1st element in the
93 * private structure
94 * associated with the
95 * request.
96 */
97 struct nvmefc_fcp_req fcp_req;
98
99 struct nvme_fc_ctrl *ctrl;
100 struct nvme_fc_queue *queue;
101 struct request *rq;
102
103 atomic_t state;
104 u32 flags;
105 u32 rqno;
106 u32 nents;
107
108 struct nvme_fc_cmd_iu cmd_iu;
109 struct nvme_fc_ersp_iu rsp_iu;
110 };
111
112 struct nvme_fcp_op_w_sgl {
113 struct nvme_fc_fcp_op op;
114 struct scatterlist sgl[NVME_INLINE_SG_CNT];
115 uint8_t priv[];
116 };
117
118 struct nvme_fc_lport {
119 struct nvme_fc_local_port localport;
120
121 struct ida endp_cnt;
122 struct list_head port_list; /* nvme_fc_port_list */
123 struct list_head endp_list;
124 struct device *dev; /* physical device for dma */
125 struct nvme_fc_port_template *ops;
126 struct kref ref;
127 atomic_t act_rport_cnt;
128 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
129
130 struct 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;
135 struct list_head ls_req_list;
136 struct list_head ls_rcv_list;
137 struct list_head disc_list;
138 struct device *dev; /* physical device for dma */
139 struct nvme_fc_lport *lport;
140 spinlock_t lock;
141 struct kref ref;
142 atomic_t act_ctrl_cnt;
143 unsigned long dev_loss_end;
144 struct work_struct lsrcv_work;
145 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
146
147 /* fc_ctrl flags values - specified as bit positions */
148 #define ASSOC_ACTIVE 0
149 #define ASSOC_FAILED 1
150 #define FCCTRL_TERMIO 2
151
152 struct nvme_fc_ctrl {
153 spinlock_t lock;
154 struct nvme_fc_queue *queues;
155 struct device *dev;
156 struct nvme_fc_lport *lport;
157 struct nvme_fc_rport *rport;
158 u32 cnum;
159
160 bool ioq_live;
161 u64 association_id;
162 struct nvmefc_ls_rcv_op *rcv_disconn;
163
164 struct list_head ctrl_list; /* rport->ctrl_list */
165
166 struct blk_mq_tag_set admin_tag_set;
167 struct blk_mq_tag_set tag_set;
168
169 struct work_struct ioerr_work;
170 struct delayed_work connect_work;
171
172 struct kref ref;
173 unsigned long flags;
174 u32 iocnt;
175 wait_queue_head_t ioabort_wait;
176
177 struct nvme_fc_fcp_op aen_ops[NVME_NR_AEN_COMMANDS];
178
179 struct nvme_ctrl ctrl;
180 };
181
182 static inline struct nvme_fc_ctrl *
to_fc_ctrl(struct nvme_ctrl * ctrl)183 to_fc_ctrl(struct nvme_ctrl *ctrl)
184 {
185 return container_of(ctrl, struct nvme_fc_ctrl, ctrl);
186 }
187
188 static inline struct nvme_fc_lport *
localport_to_lport(struct nvme_fc_local_port * portptr)189 localport_to_lport(struct nvme_fc_local_port *portptr)
190 {
191 return container_of(portptr, struct nvme_fc_lport, localport);
192 }
193
194 static inline struct nvme_fc_rport *
remoteport_to_rport(struct nvme_fc_remote_port * portptr)195 remoteport_to_rport(struct nvme_fc_remote_port *portptr)
196 {
197 return container_of(portptr, struct nvme_fc_rport, remoteport);
198 }
199
200 static inline struct nvmefc_ls_req_op *
ls_req_to_lsop(struct nvmefc_ls_req * lsreq)201 ls_req_to_lsop(struct nvmefc_ls_req *lsreq)
202 {
203 return container_of(lsreq, struct nvmefc_ls_req_op, ls_req);
204 }
205
206 static inline struct nvme_fc_fcp_op *
fcp_req_to_fcp_op(struct nvmefc_fcp_req * fcpreq)207 fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq)
208 {
209 return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req);
210 }
211
212
213
214 /* *************************** Globals **************************** */
215
216
217 static DEFINE_SPINLOCK(nvme_fc_lock);
218
219 static LIST_HEAD(nvme_fc_lport_list);
220 static DEFINE_IDA(nvme_fc_local_port_cnt);
221 static DEFINE_IDA(nvme_fc_ctrl_cnt);
222
223 /*
224 * These items are short-term. They will eventually be moved into
225 * a generic FC class. See comments in module init.
226 */
227 static struct device *fc_udev_device;
228
229 static void nvme_fc_complete_rq(struct request *rq);
230
231 /* *********************** FC-NVME Port Management ************************ */
232
233 static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *,
234 struct nvme_fc_queue *, unsigned int);
235
236 static void nvme_fc_handle_ls_rqst_work(struct work_struct *work);
237
238
239 static void
nvme_fc_free_lport(struct kref * ref)240 nvme_fc_free_lport(struct kref *ref)
241 {
242 struct nvme_fc_lport *lport =
243 container_of(ref, struct nvme_fc_lport, ref);
244 unsigned long flags;
245
246 WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED);
247 WARN_ON(!list_empty(&lport->endp_list));
248
249 /* remove from transport list */
250 spin_lock_irqsave(&nvme_fc_lock, flags);
251 list_del(&lport->port_list);
252 spin_unlock_irqrestore(&nvme_fc_lock, flags);
253
254 ida_free(&nvme_fc_local_port_cnt, lport->localport.port_num);
255 ida_destroy(&lport->endp_cnt);
256
257 put_device(lport->dev);
258
259 kfree(lport);
260 }
261
262 static void
nvme_fc_lport_put(struct nvme_fc_lport * lport)263 nvme_fc_lport_put(struct nvme_fc_lport *lport)
264 {
265 kref_put(&lport->ref, nvme_fc_free_lport);
266 }
267
268 static int
nvme_fc_lport_get(struct nvme_fc_lport * lport)269 nvme_fc_lport_get(struct nvme_fc_lport *lport)
270 {
271 return kref_get_unless_zero(&lport->ref);
272 }
273
274
275 static struct nvme_fc_lport *
nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info * pinfo,struct nvme_fc_port_template * ops,struct device * dev)276 nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info *pinfo,
277 struct nvme_fc_port_template *ops,
278 struct device *dev)
279 {
280 struct nvme_fc_lport *lport;
281 unsigned long flags;
282
283 spin_lock_irqsave(&nvme_fc_lock, flags);
284
285 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
286 if (lport->localport.node_name != pinfo->node_name ||
287 lport->localport.port_name != pinfo->port_name)
288 continue;
289
290 if (lport->dev != dev) {
291 lport = ERR_PTR(-EXDEV);
292 goto out_done;
293 }
294
295 if (lport->localport.port_state != FC_OBJSTATE_DELETED) {
296 lport = ERR_PTR(-EEXIST);
297 goto out_done;
298 }
299
300 if (!nvme_fc_lport_get(lport)) {
301 /*
302 * fails if ref cnt already 0. If so,
303 * act as if lport already deleted
304 */
305 lport = NULL;
306 goto out_done;
307 }
308
309 /* resume the lport */
310
311 lport->ops = ops;
312 lport->localport.port_role = pinfo->port_role;
313 lport->localport.port_id = pinfo->port_id;
314 lport->localport.port_state = FC_OBJSTATE_ONLINE;
315
316 spin_unlock_irqrestore(&nvme_fc_lock, flags);
317
318 return lport;
319 }
320
321 lport = NULL;
322
323 out_done:
324 spin_unlock_irqrestore(&nvme_fc_lock, flags);
325
326 return lport;
327 }
328
329 /**
330 * nvme_fc_register_localport - transport entry point called by an
331 * LLDD to register the existence of a NVME
332 * host FC port.
333 * @pinfo: pointer to information about the port to be registered
334 * @template: LLDD entrypoints and operational parameters for the port
335 * @dev: physical hardware device node port corresponds to. Will be
336 * used for DMA mappings
337 * @portptr: pointer to a local port pointer. Upon success, the routine
338 * will allocate a nvme_fc_local_port structure and place its
339 * address in the local port pointer. Upon failure, local port
340 * pointer will be set to 0.
341 *
342 * Returns:
343 * a completion status. Must be 0 upon success; a negative errno
344 * (ex: -ENXIO) upon failure.
345 */
346 int
nvme_fc_register_localport(struct nvme_fc_port_info * pinfo,struct nvme_fc_port_template * template,struct device * dev,struct nvme_fc_local_port ** portptr)347 nvme_fc_register_localport(struct nvme_fc_port_info *pinfo,
348 struct nvme_fc_port_template *template,
349 struct device *dev,
350 struct nvme_fc_local_port **portptr)
351 {
352 struct nvme_fc_lport *newrec;
353 unsigned long flags;
354 int ret, idx;
355
356 if (!template->localport_delete || !template->remoteport_delete ||
357 !template->ls_req || !template->fcp_io ||
358 !template->ls_abort || !template->fcp_abort ||
359 !template->max_hw_queues || !template->max_sgl_segments ||
360 !template->max_dif_sgl_segments || !template->dma_boundary) {
361 ret = -EINVAL;
362 goto out_reghost_failed;
363 }
364
365 /*
366 * look to see if there is already a localport that had been
367 * deregistered and in the process of waiting for all the
368 * references to fully be removed. If the references haven't
369 * expired, we can simply re-enable the localport. Remoteports
370 * and controller reconnections should resume naturally.
371 */
372 newrec = nvme_fc_attach_to_unreg_lport(pinfo, template, dev);
373
374 /* found an lport, but something about its state is bad */
375 if (IS_ERR(newrec)) {
376 ret = PTR_ERR(newrec);
377 goto out_reghost_failed;
378
379 /* found existing lport, which was resumed */
380 } else if (newrec) {
381 *portptr = &newrec->localport;
382 return 0;
383 }
384
385 /* nothing found - allocate a new localport struct */
386
387 newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz),
388 GFP_KERNEL);
389 if (!newrec) {
390 ret = -ENOMEM;
391 goto out_reghost_failed;
392 }
393
394 idx = ida_alloc(&nvme_fc_local_port_cnt, GFP_KERNEL);
395 if (idx < 0) {
396 ret = -ENOSPC;
397 goto out_fail_kfree;
398 }
399
400 if (!get_device(dev) && dev) {
401 ret = -ENODEV;
402 goto out_ida_put;
403 }
404
405 INIT_LIST_HEAD(&newrec->port_list);
406 INIT_LIST_HEAD(&newrec->endp_list);
407 kref_init(&newrec->ref);
408 atomic_set(&newrec->act_rport_cnt, 0);
409 newrec->ops = template;
410 newrec->dev = dev;
411 ida_init(&newrec->endp_cnt);
412 if (template->local_priv_sz)
413 newrec->localport.private = &newrec[1];
414 else
415 newrec->localport.private = NULL;
416 newrec->localport.node_name = pinfo->node_name;
417 newrec->localport.port_name = pinfo->port_name;
418 newrec->localport.port_role = pinfo->port_role;
419 newrec->localport.port_id = pinfo->port_id;
420 newrec->localport.port_state = FC_OBJSTATE_ONLINE;
421 newrec->localport.port_num = idx;
422
423 spin_lock_irqsave(&nvme_fc_lock, flags);
424 list_add_tail(&newrec->port_list, &nvme_fc_lport_list);
425 spin_unlock_irqrestore(&nvme_fc_lock, flags);
426
427 if (dev)
428 dma_set_seg_boundary(dev, template->dma_boundary);
429
430 *portptr = &newrec->localport;
431 return 0;
432
433 out_ida_put:
434 ida_free(&nvme_fc_local_port_cnt, idx);
435 out_fail_kfree:
436 kfree(newrec);
437 out_reghost_failed:
438 *portptr = NULL;
439
440 return ret;
441 }
442 EXPORT_SYMBOL_GPL(nvme_fc_register_localport);
443
444 /**
445 * nvme_fc_unregister_localport - transport entry point called by an
446 * LLDD to deregister/remove a previously
447 * registered a NVME host FC port.
448 * @portptr: pointer to the (registered) local port that is to be deregistered.
449 *
450 * Returns:
451 * a completion status. Must be 0 upon success; a negative errno
452 * (ex: -ENXIO) upon failure.
453 */
454 int
nvme_fc_unregister_localport(struct nvme_fc_local_port * portptr)455 nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr)
456 {
457 struct nvme_fc_lport *lport = localport_to_lport(portptr);
458 unsigned long flags;
459
460 if (!portptr)
461 return -EINVAL;
462
463 spin_lock_irqsave(&nvme_fc_lock, flags);
464
465 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
466 spin_unlock_irqrestore(&nvme_fc_lock, flags);
467 return -EINVAL;
468 }
469 portptr->port_state = FC_OBJSTATE_DELETED;
470
471 spin_unlock_irqrestore(&nvme_fc_lock, flags);
472
473 if (atomic_read(&lport->act_rport_cnt) == 0)
474 lport->ops->localport_delete(&lport->localport);
475
476 nvme_fc_lport_put(lport);
477
478 return 0;
479 }
480 EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport);
481
482 /*
483 * TRADDR strings, per FC-NVME are fixed format:
484 * "nn-0x<16hexdigits>:pn-0x<16hexdigits>" - 43 characters
485 * udev event will only differ by prefix of what field is
486 * being specified:
487 * "NVMEFC_HOST_TRADDR=" or "NVMEFC_TRADDR=" - 19 max characters
488 * 19 + 43 + null_fudge = 64 characters
489 */
490 #define FCNVME_TRADDR_LENGTH 64
491
492 static void
nvme_fc_signal_discovery_scan(struct nvme_fc_lport * lport,struct nvme_fc_rport * rport)493 nvme_fc_signal_discovery_scan(struct nvme_fc_lport *lport,
494 struct nvme_fc_rport *rport)
495 {
496 char hostaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_HOST_TRADDR=...*/
497 char tgtaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_TRADDR=...*/
498 char *envp[4] = { "FC_EVENT=nvmediscovery", hostaddr, tgtaddr, NULL };
499
500 if (!(rport->remoteport.port_role & FC_PORT_ROLE_NVME_DISCOVERY))
501 return;
502
503 snprintf(hostaddr, sizeof(hostaddr),
504 "NVMEFC_HOST_TRADDR=nn-0x%016llx:pn-0x%016llx",
505 lport->localport.node_name, lport->localport.port_name);
506 snprintf(tgtaddr, sizeof(tgtaddr),
507 "NVMEFC_TRADDR=nn-0x%016llx:pn-0x%016llx",
508 rport->remoteport.node_name, rport->remoteport.port_name);
509 kobject_uevent_env(&fc_udev_device->kobj, KOBJ_CHANGE, envp);
510 }
511
512 static void
nvme_fc_free_rport(struct kref * ref)513 nvme_fc_free_rport(struct kref *ref)
514 {
515 struct nvme_fc_rport *rport =
516 container_of(ref, struct nvme_fc_rport, ref);
517 struct nvme_fc_lport *lport =
518 localport_to_lport(rport->remoteport.localport);
519 unsigned long flags;
520
521 WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED);
522 WARN_ON(!list_empty(&rport->ctrl_list));
523 WARN_ON(!list_empty(&rport->ls_req_list));
524 WARN_ON(!list_empty(&rport->ls_rcv_list));
525
526 /* remove from lport list */
527 spin_lock_irqsave(&nvme_fc_lock, flags);
528 list_del(&rport->endp_list);
529 spin_unlock_irqrestore(&nvme_fc_lock, flags);
530
531 WARN_ON(!list_empty(&rport->disc_list));
532 ida_free(&lport->endp_cnt, rport->remoteport.port_num);
533
534 kfree(rport);
535
536 nvme_fc_lport_put(lport);
537 }
538
539 static void
nvme_fc_rport_put(struct nvme_fc_rport * rport)540 nvme_fc_rport_put(struct nvme_fc_rport *rport)
541 {
542 kref_put(&rport->ref, nvme_fc_free_rport);
543 }
544
545 static int
nvme_fc_rport_get(struct nvme_fc_rport * rport)546 nvme_fc_rport_get(struct nvme_fc_rport *rport)
547 {
548 return kref_get_unless_zero(&rport->ref);
549 }
550
551 static void
nvme_fc_resume_controller(struct nvme_fc_ctrl * ctrl)552 nvme_fc_resume_controller(struct nvme_fc_ctrl *ctrl)
553 {
554 switch (nvme_ctrl_state(&ctrl->ctrl)) {
555 case NVME_CTRL_NEW:
556 case NVME_CTRL_CONNECTING:
557 /*
558 * As all reconnects were suppressed, schedule a
559 * connect.
560 */
561 dev_info(ctrl->ctrl.device,
562 "NVME-FC{%d}: connectivity re-established. "
563 "Attempting reconnect\n", ctrl->cnum);
564
565 queue_delayed_work(nvme_wq, &ctrl->connect_work, 0);
566 break;
567
568 case NVME_CTRL_RESETTING:
569 /*
570 * Controller is already in the process of terminating the
571 * association. No need to do anything further. The reconnect
572 * step will naturally occur after the reset completes.
573 */
574 break;
575
576 default:
577 /* no action to take - let it delete */
578 break;
579 }
580 }
581
582 static struct nvme_fc_rport *
nvme_fc_attach_to_suspended_rport(struct nvme_fc_lport * lport,struct nvme_fc_port_info * pinfo)583 nvme_fc_attach_to_suspended_rport(struct nvme_fc_lport *lport,
584 struct nvme_fc_port_info *pinfo)
585 {
586 struct nvme_fc_rport *rport;
587 struct nvme_fc_ctrl *ctrl;
588 unsigned long flags;
589
590 spin_lock_irqsave(&nvme_fc_lock, flags);
591
592 list_for_each_entry(rport, &lport->endp_list, endp_list) {
593 if (rport->remoteport.node_name != pinfo->node_name ||
594 rport->remoteport.port_name != pinfo->port_name)
595 continue;
596
597 if (!nvme_fc_rport_get(rport)) {
598 rport = ERR_PTR(-ENOLCK);
599 goto out_done;
600 }
601
602 spin_unlock_irqrestore(&nvme_fc_lock, flags);
603
604 spin_lock_irqsave(&rport->lock, flags);
605
606 /* has it been unregistered */
607 if (rport->remoteport.port_state != FC_OBJSTATE_DELETED) {
608 /* means lldd called us twice */
609 spin_unlock_irqrestore(&rport->lock, flags);
610 nvme_fc_rport_put(rport);
611 return ERR_PTR(-ESTALE);
612 }
613
614 rport->remoteport.port_role = pinfo->port_role;
615 rport->remoteport.port_id = pinfo->port_id;
616 rport->remoteport.port_state = FC_OBJSTATE_ONLINE;
617 rport->dev_loss_end = 0;
618
619 /*
620 * kick off a reconnect attempt on all associations to the
621 * remote port. A successful reconnects will resume i/o.
622 */
623 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list)
624 nvme_fc_resume_controller(ctrl);
625
626 spin_unlock_irqrestore(&rport->lock, flags);
627
628 return rport;
629 }
630
631 rport = NULL;
632
633 out_done:
634 spin_unlock_irqrestore(&nvme_fc_lock, flags);
635
636 return rport;
637 }
638
639 static inline void
__nvme_fc_set_dev_loss_tmo(struct nvme_fc_rport * rport,struct nvme_fc_port_info * pinfo)640 __nvme_fc_set_dev_loss_tmo(struct nvme_fc_rport *rport,
641 struct nvme_fc_port_info *pinfo)
642 {
643 if (pinfo->dev_loss_tmo)
644 rport->remoteport.dev_loss_tmo = pinfo->dev_loss_tmo;
645 else
646 rport->remoteport.dev_loss_tmo = NVME_FC_DEFAULT_DEV_LOSS_TMO;
647 }
648
649 /**
650 * nvme_fc_register_remoteport - transport entry point called by an
651 * LLDD to register the existence of a NVME
652 * subsystem FC port on its fabric.
653 * @localport: pointer to the (registered) local port that the remote
654 * subsystem port is connected to.
655 * @pinfo: pointer to information about the port to be registered
656 * @portptr: pointer to a remote port pointer. Upon success, the routine
657 * will allocate a nvme_fc_remote_port structure and place its
658 * address in the remote port pointer. Upon failure, remote port
659 * pointer will be set to 0.
660 *
661 * Returns:
662 * a completion status. Must be 0 upon success; a negative errno
663 * (ex: -ENXIO) upon failure.
664 */
665 int
nvme_fc_register_remoteport(struct nvme_fc_local_port * localport,struct nvme_fc_port_info * pinfo,struct nvme_fc_remote_port ** portptr)666 nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
667 struct nvme_fc_port_info *pinfo,
668 struct nvme_fc_remote_port **portptr)
669 {
670 struct nvme_fc_lport *lport = localport_to_lport(localport);
671 struct nvme_fc_rport *newrec;
672 unsigned long flags;
673 int ret, idx;
674
675 if (!nvme_fc_lport_get(lport)) {
676 ret = -ESHUTDOWN;
677 goto out_reghost_failed;
678 }
679
680 /*
681 * look to see if there is already a remoteport that is waiting
682 * for a reconnect (within dev_loss_tmo) with the same WWN's.
683 * If so, transition to it and reconnect.
684 */
685 newrec = nvme_fc_attach_to_suspended_rport(lport, pinfo);
686
687 /* found an rport, but something about its state is bad */
688 if (IS_ERR(newrec)) {
689 ret = PTR_ERR(newrec);
690 goto out_lport_put;
691
692 /* found existing rport, which was resumed */
693 } else if (newrec) {
694 nvme_fc_lport_put(lport);
695 __nvme_fc_set_dev_loss_tmo(newrec, pinfo);
696 nvme_fc_signal_discovery_scan(lport, newrec);
697 *portptr = &newrec->remoteport;
698 return 0;
699 }
700
701 /* nothing found - allocate a new remoteport struct */
702
703 newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz),
704 GFP_KERNEL);
705 if (!newrec) {
706 ret = -ENOMEM;
707 goto out_lport_put;
708 }
709
710 idx = ida_alloc(&lport->endp_cnt, GFP_KERNEL);
711 if (idx < 0) {
712 ret = -ENOSPC;
713 goto out_kfree_rport;
714 }
715
716 INIT_LIST_HEAD(&newrec->endp_list);
717 INIT_LIST_HEAD(&newrec->ctrl_list);
718 INIT_LIST_HEAD(&newrec->ls_req_list);
719 INIT_LIST_HEAD(&newrec->disc_list);
720 kref_init(&newrec->ref);
721 atomic_set(&newrec->act_ctrl_cnt, 0);
722 spin_lock_init(&newrec->lock);
723 newrec->remoteport.localport = &lport->localport;
724 INIT_LIST_HEAD(&newrec->ls_rcv_list);
725 newrec->dev = lport->dev;
726 newrec->lport = lport;
727 if (lport->ops->remote_priv_sz)
728 newrec->remoteport.private = &newrec[1];
729 else
730 newrec->remoteport.private = NULL;
731 newrec->remoteport.port_role = pinfo->port_role;
732 newrec->remoteport.node_name = pinfo->node_name;
733 newrec->remoteport.port_name = pinfo->port_name;
734 newrec->remoteport.port_id = pinfo->port_id;
735 newrec->remoteport.port_state = FC_OBJSTATE_ONLINE;
736 newrec->remoteport.port_num = idx;
737 __nvme_fc_set_dev_loss_tmo(newrec, pinfo);
738 INIT_WORK(&newrec->lsrcv_work, nvme_fc_handle_ls_rqst_work);
739
740 spin_lock_irqsave(&nvme_fc_lock, flags);
741 list_add_tail(&newrec->endp_list, &lport->endp_list);
742 spin_unlock_irqrestore(&nvme_fc_lock, flags);
743
744 nvme_fc_signal_discovery_scan(lport, newrec);
745
746 *portptr = &newrec->remoteport;
747 return 0;
748
749 out_kfree_rport:
750 kfree(newrec);
751 out_lport_put:
752 nvme_fc_lport_put(lport);
753 out_reghost_failed:
754 *portptr = NULL;
755 return ret;
756 }
757 EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport);
758
759 static int
nvme_fc_abort_lsops(struct nvme_fc_rport * rport)760 nvme_fc_abort_lsops(struct nvme_fc_rport *rport)
761 {
762 struct nvmefc_ls_req_op *lsop;
763 unsigned long flags;
764
765 restart:
766 spin_lock_irqsave(&rport->lock, flags);
767
768 list_for_each_entry(lsop, &rport->ls_req_list, lsreq_list) {
769 if (!(lsop->flags & FCOP_FLAGS_TERMIO)) {
770 lsop->flags |= FCOP_FLAGS_TERMIO;
771 spin_unlock_irqrestore(&rport->lock, flags);
772 rport->lport->ops->ls_abort(&rport->lport->localport,
773 &rport->remoteport,
774 &lsop->ls_req);
775 goto restart;
776 }
777 }
778 spin_unlock_irqrestore(&rport->lock, flags);
779
780 return 0;
781 }
782
783 static void
nvme_fc_ctrl_connectivity_loss(struct nvme_fc_ctrl * ctrl)784 nvme_fc_ctrl_connectivity_loss(struct nvme_fc_ctrl *ctrl)
785 {
786 dev_info(ctrl->ctrl.device,
787 "NVME-FC{%d}: controller connectivity lost. Awaiting "
788 "Reconnect", ctrl->cnum);
789
790 set_bit(ASSOC_FAILED, &ctrl->flags);
791 nvme_reset_ctrl(&ctrl->ctrl);
792 }
793
794 /**
795 * nvme_fc_unregister_remoteport - transport entry point called by an
796 * LLDD to deregister/remove a previously
797 * registered a NVME subsystem FC port.
798 * @portptr: pointer to the (registered) remote port that is to be
799 * deregistered.
800 *
801 * Returns:
802 * a completion status. Must be 0 upon success; a negative errno
803 * (ex: -ENXIO) upon failure.
804 */
805 int
nvme_fc_unregister_remoteport(struct nvme_fc_remote_port * portptr)806 nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr)
807 {
808 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
809 struct nvme_fc_ctrl *ctrl;
810 unsigned long flags;
811
812 if (!portptr)
813 return -EINVAL;
814
815 spin_lock_irqsave(&rport->lock, flags);
816
817 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
818 spin_unlock_irqrestore(&rport->lock, flags);
819 return -EINVAL;
820 }
821 portptr->port_state = FC_OBJSTATE_DELETED;
822
823 rport->dev_loss_end = jiffies + (portptr->dev_loss_tmo * HZ);
824
825 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
826 /* if dev_loss_tmo==0, dev loss is immediate */
827 if (!portptr->dev_loss_tmo) {
828 dev_warn(ctrl->ctrl.device,
829 "NVME-FC{%d}: controller connectivity lost.\n",
830 ctrl->cnum);
831 nvme_delete_ctrl(&ctrl->ctrl);
832 } else
833 nvme_fc_ctrl_connectivity_loss(ctrl);
834 }
835
836 spin_unlock_irqrestore(&rport->lock, flags);
837
838 nvme_fc_abort_lsops(rport);
839
840 if (atomic_read(&rport->act_ctrl_cnt) == 0)
841 rport->lport->ops->remoteport_delete(portptr);
842
843 /*
844 * release the reference, which will allow, if all controllers
845 * go away, which should only occur after dev_loss_tmo occurs,
846 * for the rport to be torn down.
847 */
848 nvme_fc_rport_put(rport);
849
850 return 0;
851 }
852 EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport);
853
854 /**
855 * nvme_fc_rescan_remoteport - transport entry point called by an
856 * LLDD to request a nvme device rescan.
857 * @remoteport: pointer to the (registered) remote port that is to be
858 * rescanned.
859 *
860 * Returns: N/A
861 */
862 void
nvme_fc_rescan_remoteport(struct nvme_fc_remote_port * remoteport)863 nvme_fc_rescan_remoteport(struct nvme_fc_remote_port *remoteport)
864 {
865 struct nvme_fc_rport *rport = remoteport_to_rport(remoteport);
866
867 nvme_fc_signal_discovery_scan(rport->lport, rport);
868 }
869 EXPORT_SYMBOL_GPL(nvme_fc_rescan_remoteport);
870
871 int
nvme_fc_set_remoteport_devloss(struct nvme_fc_remote_port * portptr,u32 dev_loss_tmo)872 nvme_fc_set_remoteport_devloss(struct nvme_fc_remote_port *portptr,
873 u32 dev_loss_tmo)
874 {
875 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
876 unsigned long flags;
877
878 spin_lock_irqsave(&rport->lock, flags);
879
880 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
881 spin_unlock_irqrestore(&rport->lock, flags);
882 return -EINVAL;
883 }
884
885 /* a dev_loss_tmo of 0 (immediate) is allowed to be set */
886 rport->remoteport.dev_loss_tmo = dev_loss_tmo;
887
888 spin_unlock_irqrestore(&rport->lock, flags);
889
890 return 0;
891 }
892 EXPORT_SYMBOL_GPL(nvme_fc_set_remoteport_devloss);
893
894
895 /* *********************** FC-NVME DMA Handling **************************** */
896
897 /*
898 * The fcloop device passes in a NULL device pointer. Real LLD's will
899 * pass in a valid device pointer. If NULL is passed to the dma mapping
900 * routines, depending on the platform, it may or may not succeed, and
901 * may crash.
902 *
903 * As such:
904 * Wrap all the dma routines and check the dev pointer.
905 *
906 * If simple mappings (return just a dma address, we'll noop them,
907 * returning a dma address of 0.
908 *
909 * On more complex mappings (dma_map_sg), a pseudo routine fills
910 * in the scatter list, setting all dma addresses to 0.
911 */
912
913 static inline dma_addr_t
fc_dma_map_single(struct device * dev,void * ptr,size_t size,enum dma_data_direction dir)914 fc_dma_map_single(struct device *dev, void *ptr, size_t size,
915 enum dma_data_direction dir)
916 {
917 return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
918 }
919
920 static inline int
fc_dma_mapping_error(struct device * dev,dma_addr_t dma_addr)921 fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
922 {
923 return dev ? dma_mapping_error(dev, dma_addr) : 0;
924 }
925
926 static inline void
fc_dma_unmap_single(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)927 fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
928 enum dma_data_direction dir)
929 {
930 if (dev)
931 dma_unmap_single(dev, addr, size, dir);
932 }
933
934 static inline void
fc_dma_sync_single_for_cpu(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)935 fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
936 enum dma_data_direction dir)
937 {
938 if (dev)
939 dma_sync_single_for_cpu(dev, addr, size, dir);
940 }
941
942 static inline void
fc_dma_sync_single_for_device(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)943 fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
944 enum dma_data_direction dir)
945 {
946 if (dev)
947 dma_sync_single_for_device(dev, addr, size, dir);
948 }
949
950 /* pseudo dma_map_sg call */
951 static int
fc_map_sg(struct scatterlist * sg,int nents)952 fc_map_sg(struct scatterlist *sg, int nents)
953 {
954 struct scatterlist *s;
955 int i;
956
957 WARN_ON(nents == 0 || sg[0].length == 0);
958
959 for_each_sg(sg, s, nents, i) {
960 s->dma_address = 0L;
961 #ifdef CONFIG_NEED_SG_DMA_LENGTH
962 s->dma_length = s->length;
963 #endif
964 }
965 return nents;
966 }
967
968 static inline int
fc_dma_map_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir)969 fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
970 enum dma_data_direction dir)
971 {
972 return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
973 }
974
975 static inline void
fc_dma_unmap_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir)976 fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
977 enum dma_data_direction dir)
978 {
979 if (dev)
980 dma_unmap_sg(dev, sg, nents, dir);
981 }
982
983 /* *********************** FC-NVME LS Handling **************************** */
984
985 static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
986 static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
987
988 static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg);
989
990 static void
__nvme_fc_finish_ls_req(struct nvmefc_ls_req_op * lsop)991 __nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop)
992 {
993 struct nvme_fc_rport *rport = lsop->rport;
994 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
995 unsigned long flags;
996
997 spin_lock_irqsave(&rport->lock, flags);
998
999 if (!lsop->req_queued) {
1000 spin_unlock_irqrestore(&rport->lock, flags);
1001 return;
1002 }
1003
1004 list_del(&lsop->lsreq_list);
1005
1006 lsop->req_queued = false;
1007
1008 spin_unlock_irqrestore(&rport->lock, flags);
1009
1010 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1011 (lsreq->rqstlen + lsreq->rsplen),
1012 DMA_BIDIRECTIONAL);
1013
1014 nvme_fc_rport_put(rport);
1015 }
1016
1017 static int
__nvme_fc_send_ls_req(struct nvme_fc_rport * rport,struct nvmefc_ls_req_op * lsop,void (* done)(struct nvmefc_ls_req * req,int status))1018 __nvme_fc_send_ls_req(struct nvme_fc_rport *rport,
1019 struct nvmefc_ls_req_op *lsop,
1020 void (*done)(struct nvmefc_ls_req *req, int status))
1021 {
1022 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1023 unsigned long flags;
1024 int ret = 0;
1025
1026 if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
1027 return -ECONNREFUSED;
1028
1029 if (!nvme_fc_rport_get(rport))
1030 return -ESHUTDOWN;
1031
1032 lsreq->done = done;
1033 lsop->rport = rport;
1034 lsop->req_queued = false;
1035 INIT_LIST_HEAD(&lsop->lsreq_list);
1036 init_completion(&lsop->ls_done);
1037
1038 lsreq->rqstdma = fc_dma_map_single(rport->dev, lsreq->rqstaddr,
1039 lsreq->rqstlen + lsreq->rsplen,
1040 DMA_BIDIRECTIONAL);
1041 if (fc_dma_mapping_error(rport->dev, lsreq->rqstdma)) {
1042 ret = -EFAULT;
1043 goto out_putrport;
1044 }
1045 lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
1046
1047 spin_lock_irqsave(&rport->lock, flags);
1048
1049 list_add_tail(&lsop->lsreq_list, &rport->ls_req_list);
1050
1051 lsop->req_queued = true;
1052
1053 spin_unlock_irqrestore(&rport->lock, flags);
1054
1055 ret = rport->lport->ops->ls_req(&rport->lport->localport,
1056 &rport->remoteport, lsreq);
1057 if (ret)
1058 goto out_unlink;
1059
1060 return 0;
1061
1062 out_unlink:
1063 lsop->ls_error = ret;
1064 spin_lock_irqsave(&rport->lock, flags);
1065 lsop->req_queued = false;
1066 list_del(&lsop->lsreq_list);
1067 spin_unlock_irqrestore(&rport->lock, flags);
1068 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1069 (lsreq->rqstlen + lsreq->rsplen),
1070 DMA_BIDIRECTIONAL);
1071 out_putrport:
1072 nvme_fc_rport_put(rport);
1073
1074 return ret;
1075 }
1076
1077 static void
nvme_fc_send_ls_req_done(struct nvmefc_ls_req * lsreq,int status)1078 nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status)
1079 {
1080 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1081
1082 lsop->ls_error = status;
1083 complete(&lsop->ls_done);
1084 }
1085
1086 static int
nvme_fc_send_ls_req(struct nvme_fc_rport * rport,struct nvmefc_ls_req_op * lsop)1087 nvme_fc_send_ls_req(struct nvme_fc_rport *rport, struct nvmefc_ls_req_op *lsop)
1088 {
1089 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1090 struct fcnvme_ls_rjt *rjt = lsreq->rspaddr;
1091 int ret;
1092
1093 ret = __nvme_fc_send_ls_req(rport, lsop, nvme_fc_send_ls_req_done);
1094
1095 if (!ret) {
1096 /*
1097 * No timeout/not interruptible as we need the struct
1098 * to exist until the lldd calls us back. Thus mandate
1099 * wait until driver calls back. lldd responsible for
1100 * the timeout action
1101 */
1102 wait_for_completion(&lsop->ls_done);
1103
1104 __nvme_fc_finish_ls_req(lsop);
1105
1106 ret = lsop->ls_error;
1107 }
1108
1109 if (ret)
1110 return ret;
1111
1112 /* ACC or RJT payload ? */
1113 if (rjt->w0.ls_cmd == FCNVME_LS_RJT)
1114 return -ENXIO;
1115
1116 return 0;
1117 }
1118
1119 static int
nvme_fc_send_ls_req_async(struct nvme_fc_rport * rport,struct nvmefc_ls_req_op * lsop,void (* done)(struct nvmefc_ls_req * req,int status))1120 nvme_fc_send_ls_req_async(struct nvme_fc_rport *rport,
1121 struct nvmefc_ls_req_op *lsop,
1122 void (*done)(struct nvmefc_ls_req *req, int status))
1123 {
1124 /* don't wait for completion */
1125
1126 return __nvme_fc_send_ls_req(rport, lsop, done);
1127 }
1128
1129 static int
nvme_fc_connect_admin_queue(struct nvme_fc_ctrl * ctrl,struct nvme_fc_queue * queue,u16 qsize,u16 ersp_ratio)1130 nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl,
1131 struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio)
1132 {
1133 struct nvmefc_ls_req_op *lsop;
1134 struct nvmefc_ls_req *lsreq;
1135 struct fcnvme_ls_cr_assoc_rqst *assoc_rqst;
1136 struct fcnvme_ls_cr_assoc_acc *assoc_acc;
1137 unsigned long flags;
1138 int ret, fcret = 0;
1139
1140 lsop = kzalloc((sizeof(*lsop) +
1141 sizeof(*assoc_rqst) + sizeof(*assoc_acc) +
1142 ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1143 if (!lsop) {
1144 dev_info(ctrl->ctrl.device,
1145 "NVME-FC{%d}: send Create Association failed: ENOMEM\n",
1146 ctrl->cnum);
1147 ret = -ENOMEM;
1148 goto out_no_memory;
1149 }
1150
1151 assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)&lsop[1];
1152 assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1];
1153 lsreq = &lsop->ls_req;
1154 if (ctrl->lport->ops->lsrqst_priv_sz)
1155 lsreq->private = &assoc_acc[1];
1156 else
1157 lsreq->private = NULL;
1158
1159 assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION;
1160 assoc_rqst->desc_list_len =
1161 cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1162
1163 assoc_rqst->assoc_cmd.desc_tag =
1164 cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD);
1165 assoc_rqst->assoc_cmd.desc_len =
1166 fcnvme_lsdesc_len(
1167 sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1168
1169 assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1170 assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize - 1);
1171 /* Linux supports only Dynamic controllers */
1172 assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff);
1173 uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id);
1174 strscpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
1175 sizeof(assoc_rqst->assoc_cmd.hostnqn));
1176 strscpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
1177 sizeof(assoc_rqst->assoc_cmd.subnqn));
1178
1179 lsop->queue = queue;
1180 lsreq->rqstaddr = assoc_rqst;
1181 lsreq->rqstlen = sizeof(*assoc_rqst);
1182 lsreq->rspaddr = assoc_acc;
1183 lsreq->rsplen = sizeof(*assoc_acc);
1184 lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1185
1186 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1187 if (ret)
1188 goto out_free_buffer;
1189
1190 /* process connect LS completion */
1191
1192 /* validate the ACC response */
1193 if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1194 fcret = VERR_LSACC;
1195 else if (assoc_acc->hdr.desc_list_len !=
1196 fcnvme_lsdesc_len(
1197 sizeof(struct fcnvme_ls_cr_assoc_acc)))
1198 fcret = VERR_CR_ASSOC_ACC_LEN;
1199 else if (assoc_acc->hdr.rqst.desc_tag !=
1200 cpu_to_be32(FCNVME_LSDESC_RQST))
1201 fcret = VERR_LSDESC_RQST;
1202 else if (assoc_acc->hdr.rqst.desc_len !=
1203 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1204 fcret = VERR_LSDESC_RQST_LEN;
1205 else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION)
1206 fcret = VERR_CR_ASSOC;
1207 else if (assoc_acc->associd.desc_tag !=
1208 cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
1209 fcret = VERR_ASSOC_ID;
1210 else if (assoc_acc->associd.desc_len !=
1211 fcnvme_lsdesc_len(
1212 sizeof(struct fcnvme_lsdesc_assoc_id)))
1213 fcret = VERR_ASSOC_ID_LEN;
1214 else if (assoc_acc->connectid.desc_tag !=
1215 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1216 fcret = VERR_CONN_ID;
1217 else if (assoc_acc->connectid.desc_len !=
1218 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1219 fcret = VERR_CONN_ID_LEN;
1220
1221 if (fcret) {
1222 ret = -EBADF;
1223 dev_err(ctrl->dev,
1224 "q %d Create Association LS failed: %s\n",
1225 queue->qnum, validation_errors[fcret]);
1226 } else {
1227 spin_lock_irqsave(&ctrl->lock, flags);
1228 ctrl->association_id =
1229 be64_to_cpu(assoc_acc->associd.association_id);
1230 queue->connection_id =
1231 be64_to_cpu(assoc_acc->connectid.connection_id);
1232 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1233 spin_unlock_irqrestore(&ctrl->lock, flags);
1234 }
1235
1236 out_free_buffer:
1237 kfree(lsop);
1238 out_no_memory:
1239 if (ret)
1240 dev_err(ctrl->dev,
1241 "queue %d connect admin queue failed (%d).\n",
1242 queue->qnum, ret);
1243 return ret;
1244 }
1245
1246 static int
nvme_fc_connect_queue(struct nvme_fc_ctrl * ctrl,struct nvme_fc_queue * queue,u16 qsize,u16 ersp_ratio)1247 nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
1248 u16 qsize, u16 ersp_ratio)
1249 {
1250 struct nvmefc_ls_req_op *lsop;
1251 struct nvmefc_ls_req *lsreq;
1252 struct fcnvme_ls_cr_conn_rqst *conn_rqst;
1253 struct fcnvme_ls_cr_conn_acc *conn_acc;
1254 int ret, fcret = 0;
1255
1256 lsop = kzalloc((sizeof(*lsop) +
1257 sizeof(*conn_rqst) + sizeof(*conn_acc) +
1258 ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1259 if (!lsop) {
1260 dev_info(ctrl->ctrl.device,
1261 "NVME-FC{%d}: send Create Connection failed: ENOMEM\n",
1262 ctrl->cnum);
1263 ret = -ENOMEM;
1264 goto out_no_memory;
1265 }
1266
1267 conn_rqst = (struct fcnvme_ls_cr_conn_rqst *)&lsop[1];
1268 conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1];
1269 lsreq = &lsop->ls_req;
1270 if (ctrl->lport->ops->lsrqst_priv_sz)
1271 lsreq->private = (void *)&conn_acc[1];
1272 else
1273 lsreq->private = NULL;
1274
1275 conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION;
1276 conn_rqst->desc_list_len = cpu_to_be32(
1277 sizeof(struct fcnvme_lsdesc_assoc_id) +
1278 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1279
1280 conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1281 conn_rqst->associd.desc_len =
1282 fcnvme_lsdesc_len(
1283 sizeof(struct fcnvme_lsdesc_assoc_id));
1284 conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1285 conn_rqst->connect_cmd.desc_tag =
1286 cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD);
1287 conn_rqst->connect_cmd.desc_len =
1288 fcnvme_lsdesc_len(
1289 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1290 conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1291 conn_rqst->connect_cmd.qid = cpu_to_be16(queue->qnum);
1292 conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize - 1);
1293
1294 lsop->queue = queue;
1295 lsreq->rqstaddr = conn_rqst;
1296 lsreq->rqstlen = sizeof(*conn_rqst);
1297 lsreq->rspaddr = conn_acc;
1298 lsreq->rsplen = sizeof(*conn_acc);
1299 lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1300
1301 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1302 if (ret)
1303 goto out_free_buffer;
1304
1305 /* process connect LS completion */
1306
1307 /* validate the ACC response */
1308 if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1309 fcret = VERR_LSACC;
1310 else if (conn_acc->hdr.desc_list_len !=
1311 fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)))
1312 fcret = VERR_CR_CONN_ACC_LEN;
1313 else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST))
1314 fcret = VERR_LSDESC_RQST;
1315 else if (conn_acc->hdr.rqst.desc_len !=
1316 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1317 fcret = VERR_LSDESC_RQST_LEN;
1318 else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION)
1319 fcret = VERR_CR_CONN;
1320 else if (conn_acc->connectid.desc_tag !=
1321 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1322 fcret = VERR_CONN_ID;
1323 else if (conn_acc->connectid.desc_len !=
1324 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1325 fcret = VERR_CONN_ID_LEN;
1326
1327 if (fcret) {
1328 ret = -EBADF;
1329 dev_err(ctrl->dev,
1330 "q %d Create I/O Connection LS failed: %s\n",
1331 queue->qnum, validation_errors[fcret]);
1332 } else {
1333 queue->connection_id =
1334 be64_to_cpu(conn_acc->connectid.connection_id);
1335 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1336 }
1337
1338 out_free_buffer:
1339 kfree(lsop);
1340 out_no_memory:
1341 if (ret)
1342 dev_err(ctrl->dev,
1343 "queue %d connect I/O queue failed (%d).\n",
1344 queue->qnum, ret);
1345 return ret;
1346 }
1347
1348 static void
nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req * lsreq,int status)1349 nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
1350 {
1351 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1352
1353 __nvme_fc_finish_ls_req(lsop);
1354
1355 /* fc-nvme initiator doesn't care about success or failure of cmd */
1356
1357 kfree(lsop);
1358 }
1359
1360 /*
1361 * This routine sends a FC-NVME LS to disconnect (aka terminate)
1362 * the FC-NVME Association. Terminating the association also
1363 * terminates the FC-NVME connections (per queue, both admin and io
1364 * queues) that are part of the association. E.g. things are torn
1365 * down, and the related FC-NVME Association ID and Connection IDs
1366 * become invalid.
1367 *
1368 * The behavior of the fc-nvme initiator is such that its
1369 * understanding of the association and connections will implicitly
1370 * be torn down. The action is implicit as it may be due to a loss of
1371 * connectivity with the fc-nvme target, so you may never get a
1372 * response even if you tried. As such, the action of this routine
1373 * is to asynchronously send the LS, ignore any results of the LS, and
1374 * continue on with terminating the association. If the fc-nvme target
1375 * is present and receives the LS, it too can tear down.
1376 */
1377 static void
nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl * ctrl)1378 nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl)
1379 {
1380 struct fcnvme_ls_disconnect_assoc_rqst *discon_rqst;
1381 struct fcnvme_ls_disconnect_assoc_acc *discon_acc;
1382 struct nvmefc_ls_req_op *lsop;
1383 struct nvmefc_ls_req *lsreq;
1384 int ret;
1385
1386 lsop = kzalloc((sizeof(*lsop) +
1387 sizeof(*discon_rqst) + sizeof(*discon_acc) +
1388 ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1389 if (!lsop) {
1390 dev_info(ctrl->ctrl.device,
1391 "NVME-FC{%d}: send Disconnect Association "
1392 "failed: ENOMEM\n",
1393 ctrl->cnum);
1394 return;
1395 }
1396
1397 discon_rqst = (struct fcnvme_ls_disconnect_assoc_rqst *)&lsop[1];
1398 discon_acc = (struct fcnvme_ls_disconnect_assoc_acc *)&discon_rqst[1];
1399 lsreq = &lsop->ls_req;
1400 if (ctrl->lport->ops->lsrqst_priv_sz)
1401 lsreq->private = (void *)&discon_acc[1];
1402 else
1403 lsreq->private = NULL;
1404
1405 nvmefc_fmt_lsreq_discon_assoc(lsreq, discon_rqst, discon_acc,
1406 ctrl->association_id);
1407
1408 ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop,
1409 nvme_fc_disconnect_assoc_done);
1410 if (ret)
1411 kfree(lsop);
1412 }
1413
1414 static void
nvme_fc_xmt_ls_rsp_free(struct nvmefc_ls_rcv_op * lsop)1415 nvme_fc_xmt_ls_rsp_free(struct nvmefc_ls_rcv_op *lsop)
1416 {
1417 struct nvme_fc_rport *rport = lsop->rport;
1418 struct nvme_fc_lport *lport = rport->lport;
1419 unsigned long flags;
1420
1421 spin_lock_irqsave(&rport->lock, flags);
1422 list_del(&lsop->lsrcv_list);
1423 spin_unlock_irqrestore(&rport->lock, flags);
1424
1425 fc_dma_sync_single_for_cpu(lport->dev, lsop->rspdma,
1426 sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1427 fc_dma_unmap_single(lport->dev, lsop->rspdma,
1428 sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1429
1430 kfree(lsop->rspbuf);
1431 kfree(lsop->rqstbuf);
1432 kfree(lsop);
1433
1434 nvme_fc_rport_put(rport);
1435 }
1436
1437 static void
nvme_fc_xmt_ls_rsp_done(struct nvmefc_ls_rsp * lsrsp)1438 nvme_fc_xmt_ls_rsp_done(struct nvmefc_ls_rsp *lsrsp)
1439 {
1440 struct nvmefc_ls_rcv_op *lsop = lsrsp->nvme_fc_private;
1441
1442 nvme_fc_xmt_ls_rsp_free(lsop);
1443 }
1444
1445 static void
nvme_fc_xmt_ls_rsp(struct nvmefc_ls_rcv_op * lsop)1446 nvme_fc_xmt_ls_rsp(struct nvmefc_ls_rcv_op *lsop)
1447 {
1448 struct nvme_fc_rport *rport = lsop->rport;
1449 struct nvme_fc_lport *lport = rport->lport;
1450 struct fcnvme_ls_rqst_w0 *w0 = &lsop->rqstbuf->w0;
1451 int ret;
1452
1453 fc_dma_sync_single_for_device(lport->dev, lsop->rspdma,
1454 sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1455
1456 ret = lport->ops->xmt_ls_rsp(&lport->localport, &rport->remoteport,
1457 lsop->lsrsp);
1458 if (ret) {
1459 dev_warn(lport->dev,
1460 "LLDD rejected LS RSP xmt: LS %d status %d\n",
1461 w0->ls_cmd, ret);
1462 nvme_fc_xmt_ls_rsp_free(lsop);
1463 return;
1464 }
1465 }
1466
1467 static struct nvme_fc_ctrl *
nvme_fc_match_disconn_ls(struct nvme_fc_rport * rport,struct nvmefc_ls_rcv_op * lsop)1468 nvme_fc_match_disconn_ls(struct nvme_fc_rport *rport,
1469 struct nvmefc_ls_rcv_op *lsop)
1470 {
1471 struct fcnvme_ls_disconnect_assoc_rqst *rqst =
1472 &lsop->rqstbuf->rq_dis_assoc;
1473 struct nvme_fc_ctrl *ctrl, *tmp, *ret = NULL;
1474 struct nvmefc_ls_rcv_op *oldls = NULL;
1475 u64 association_id = be64_to_cpu(rqst->associd.association_id);
1476 unsigned long flags;
1477
1478 spin_lock_irqsave(&rport->lock, flags);
1479
1480 list_for_each_entry_safe(ctrl, tmp, &rport->ctrl_list, ctrl_list) {
1481 if (!nvme_fc_ctrl_get(ctrl))
1482 continue;
1483 spin_lock(&ctrl->lock);
1484 if (association_id == ctrl->association_id) {
1485 oldls = ctrl->rcv_disconn;
1486 ctrl->rcv_disconn = lsop;
1487 ret = ctrl;
1488 }
1489 spin_unlock(&ctrl->lock);
1490 if (ret)
1491 /* leave the ctrl get reference */
1492 break;
1493 spin_unlock_irqrestore(&rport->lock, flags);
1494 nvme_fc_ctrl_put(ctrl);
1495 spin_lock_irqsave(&rport->lock, flags);
1496 }
1497
1498 spin_unlock_irqrestore(&rport->lock, flags);
1499
1500 /* transmit a response for anything that was pending */
1501 if (oldls) {
1502 dev_info(rport->lport->dev,
1503 "NVME-FC{%d}: Multiple Disconnect Association "
1504 "LS's received\n", ctrl->cnum);
1505 /* overwrite good response with bogus failure */
1506 oldls->lsrsp->rsplen = nvme_fc_format_rjt(oldls->rspbuf,
1507 sizeof(*oldls->rspbuf),
1508 rqst->w0.ls_cmd,
1509 FCNVME_RJT_RC_UNAB,
1510 FCNVME_RJT_EXP_NONE, 0);
1511 nvme_fc_xmt_ls_rsp(oldls);
1512 }
1513
1514 return ret;
1515 }
1516
1517 /*
1518 * returns true to mean LS handled and ls_rsp can be sent
1519 * returns false to defer ls_rsp xmt (will be done as part of
1520 * association termination)
1521 */
1522 static bool
nvme_fc_ls_disconnect_assoc(struct nvmefc_ls_rcv_op * lsop)1523 nvme_fc_ls_disconnect_assoc(struct nvmefc_ls_rcv_op *lsop)
1524 {
1525 struct nvme_fc_rport *rport = lsop->rport;
1526 struct fcnvme_ls_disconnect_assoc_rqst *rqst =
1527 &lsop->rqstbuf->rq_dis_assoc;
1528 struct fcnvme_ls_disconnect_assoc_acc *acc =
1529 &lsop->rspbuf->rsp_dis_assoc;
1530 struct nvme_fc_ctrl *ctrl = NULL;
1531 int ret = 0;
1532
1533 memset(acc, 0, sizeof(*acc));
1534
1535 ret = nvmefc_vldt_lsreq_discon_assoc(lsop->rqstdatalen, rqst);
1536 if (!ret) {
1537 /* match an active association */
1538 ctrl = nvme_fc_match_disconn_ls(rport, lsop);
1539 if (!ctrl)
1540 ret = VERR_NO_ASSOC;
1541 }
1542
1543 if (ret) {
1544 dev_info(rport->lport->dev,
1545 "Disconnect LS failed: %s\n",
1546 validation_errors[ret]);
1547 lsop->lsrsp->rsplen = nvme_fc_format_rjt(acc,
1548 sizeof(*acc), rqst->w0.ls_cmd,
1549 (ret == VERR_NO_ASSOC) ?
1550 FCNVME_RJT_RC_INV_ASSOC :
1551 FCNVME_RJT_RC_LOGIC,
1552 FCNVME_RJT_EXP_NONE, 0);
1553 return true;
1554 }
1555
1556 /* format an ACCept response */
1557
1558 lsop->lsrsp->rsplen = sizeof(*acc);
1559
1560 nvme_fc_format_rsp_hdr(acc, FCNVME_LS_ACC,
1561 fcnvme_lsdesc_len(
1562 sizeof(struct fcnvme_ls_disconnect_assoc_acc)),
1563 FCNVME_LS_DISCONNECT_ASSOC);
1564
1565 /*
1566 * the transmit of the response will occur after the exchanges
1567 * for the association have been ABTS'd by
1568 * nvme_fc_delete_association().
1569 */
1570
1571 /* fail the association */
1572 nvme_fc_error_recovery(ctrl, "Disconnect Association LS received");
1573
1574 /* release the reference taken by nvme_fc_match_disconn_ls() */
1575 nvme_fc_ctrl_put(ctrl);
1576
1577 return false;
1578 }
1579
1580 /*
1581 * Actual Processing routine for received FC-NVME LS Requests from the LLD
1582 * returns true if a response should be sent afterward, false if rsp will
1583 * be sent asynchronously.
1584 */
1585 static bool
nvme_fc_handle_ls_rqst(struct nvmefc_ls_rcv_op * lsop)1586 nvme_fc_handle_ls_rqst(struct nvmefc_ls_rcv_op *lsop)
1587 {
1588 struct fcnvme_ls_rqst_w0 *w0 = &lsop->rqstbuf->w0;
1589 bool ret = true;
1590
1591 lsop->lsrsp->nvme_fc_private = lsop;
1592 lsop->lsrsp->rspbuf = lsop->rspbuf;
1593 lsop->lsrsp->rspdma = lsop->rspdma;
1594 lsop->lsrsp->done = nvme_fc_xmt_ls_rsp_done;
1595 /* Be preventative. handlers will later set to valid length */
1596 lsop->lsrsp->rsplen = 0;
1597
1598 /*
1599 * handlers:
1600 * parse request input, execute the request, and format the
1601 * LS response
1602 */
1603 switch (w0->ls_cmd) {
1604 case FCNVME_LS_DISCONNECT_ASSOC:
1605 ret = nvme_fc_ls_disconnect_assoc(lsop);
1606 break;
1607 case FCNVME_LS_DISCONNECT_CONN:
1608 lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
1609 sizeof(*lsop->rspbuf), w0->ls_cmd,
1610 FCNVME_RJT_RC_UNSUP, FCNVME_RJT_EXP_NONE, 0);
1611 break;
1612 case FCNVME_LS_CREATE_ASSOCIATION:
1613 case FCNVME_LS_CREATE_CONNECTION:
1614 lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
1615 sizeof(*lsop->rspbuf), w0->ls_cmd,
1616 FCNVME_RJT_RC_LOGIC, FCNVME_RJT_EXP_NONE, 0);
1617 break;
1618 default:
1619 lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
1620 sizeof(*lsop->rspbuf), w0->ls_cmd,
1621 FCNVME_RJT_RC_INVAL, FCNVME_RJT_EXP_NONE, 0);
1622 break;
1623 }
1624
1625 return(ret);
1626 }
1627
1628 static void
nvme_fc_handle_ls_rqst_work(struct work_struct * work)1629 nvme_fc_handle_ls_rqst_work(struct work_struct *work)
1630 {
1631 struct nvme_fc_rport *rport =
1632 container_of(work, struct nvme_fc_rport, lsrcv_work);
1633 struct fcnvme_ls_rqst_w0 *w0;
1634 struct nvmefc_ls_rcv_op *lsop;
1635 unsigned long flags;
1636 bool sendrsp;
1637
1638 restart:
1639 sendrsp = true;
1640 spin_lock_irqsave(&rport->lock, flags);
1641 list_for_each_entry(lsop, &rport->ls_rcv_list, lsrcv_list) {
1642 if (lsop->handled)
1643 continue;
1644
1645 lsop->handled = true;
1646 if (rport->remoteport.port_state == FC_OBJSTATE_ONLINE) {
1647 spin_unlock_irqrestore(&rport->lock, flags);
1648 sendrsp = nvme_fc_handle_ls_rqst(lsop);
1649 } else {
1650 spin_unlock_irqrestore(&rport->lock, flags);
1651 w0 = &lsop->rqstbuf->w0;
1652 lsop->lsrsp->rsplen = nvme_fc_format_rjt(
1653 lsop->rspbuf,
1654 sizeof(*lsop->rspbuf),
1655 w0->ls_cmd,
1656 FCNVME_RJT_RC_UNAB,
1657 FCNVME_RJT_EXP_NONE, 0);
1658 }
1659 if (sendrsp)
1660 nvme_fc_xmt_ls_rsp(lsop);
1661 goto restart;
1662 }
1663 spin_unlock_irqrestore(&rport->lock, flags);
1664 }
1665
1666 static
nvme_fc_rcv_ls_req_err_msg(struct nvme_fc_lport * lport,struct fcnvme_ls_rqst_w0 * w0)1667 void nvme_fc_rcv_ls_req_err_msg(struct nvme_fc_lport *lport,
1668 struct fcnvme_ls_rqst_w0 *w0)
1669 {
1670 dev_info(lport->dev, "RCV %s LS failed: No memory\n",
1671 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1672 nvmefc_ls_names[w0->ls_cmd] : "");
1673 }
1674
1675 /**
1676 * nvme_fc_rcv_ls_req - transport entry point called by an LLDD
1677 * upon the reception of a NVME LS request.
1678 *
1679 * The nvme-fc layer will copy payload to an internal structure for
1680 * processing. As such, upon completion of the routine, the LLDD may
1681 * immediately free/reuse the LS request buffer passed in the call.
1682 *
1683 * If this routine returns error, the LLDD should abort the exchange.
1684 *
1685 * @portptr: pointer to the (registered) remote port that the LS
1686 * was received from. The remoteport is associated with
1687 * a specific localport.
1688 * @lsrsp: pointer to a nvmefc_ls_rsp response structure to be
1689 * used to reference the exchange corresponding to the LS
1690 * when issuing an ls response.
1691 * @lsreqbuf: pointer to the buffer containing the LS Request
1692 * @lsreqbuf_len: length, in bytes, of the received LS request
1693 */
1694 int
nvme_fc_rcv_ls_req(struct nvme_fc_remote_port * portptr,struct nvmefc_ls_rsp * lsrsp,void * lsreqbuf,u32 lsreqbuf_len)1695 nvme_fc_rcv_ls_req(struct nvme_fc_remote_port *portptr,
1696 struct nvmefc_ls_rsp *lsrsp,
1697 void *lsreqbuf, u32 lsreqbuf_len)
1698 {
1699 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
1700 struct nvme_fc_lport *lport = rport->lport;
1701 struct fcnvme_ls_rqst_w0 *w0 = (struct fcnvme_ls_rqst_w0 *)lsreqbuf;
1702 struct nvmefc_ls_rcv_op *lsop;
1703 unsigned long flags;
1704 int ret;
1705
1706 nvme_fc_rport_get(rport);
1707
1708 /* validate there's a routine to transmit a response */
1709 if (!lport->ops->xmt_ls_rsp) {
1710 dev_info(lport->dev,
1711 "RCV %s LS failed: no LLDD xmt_ls_rsp\n",
1712 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1713 nvmefc_ls_names[w0->ls_cmd] : "");
1714 ret = -EINVAL;
1715 goto out_put;
1716 }
1717
1718 if (lsreqbuf_len > sizeof(union nvmefc_ls_requests)) {
1719 dev_info(lport->dev,
1720 "RCV %s LS failed: payload too large\n",
1721 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1722 nvmefc_ls_names[w0->ls_cmd] : "");
1723 ret = -E2BIG;
1724 goto out_put;
1725 }
1726
1727 lsop = kzalloc_obj(*lsop);
1728 if (!lsop) {
1729 nvme_fc_rcv_ls_req_err_msg(lport, w0);
1730 ret = -ENOMEM;
1731 goto out_put;
1732 }
1733
1734 lsop->rqstbuf = kzalloc_obj(*lsop->rqstbuf);
1735 lsop->rspbuf = kzalloc_obj(*lsop->rspbuf);
1736 if (!lsop->rqstbuf || !lsop->rspbuf) {
1737 nvme_fc_rcv_ls_req_err_msg(lport, w0);
1738 ret = -ENOMEM;
1739 goto out_free;
1740 }
1741
1742 lsop->rspdma = fc_dma_map_single(lport->dev, lsop->rspbuf,
1743 sizeof(*lsop->rspbuf),
1744 DMA_TO_DEVICE);
1745 if (fc_dma_mapping_error(lport->dev, lsop->rspdma)) {
1746 dev_info(lport->dev,
1747 "RCV %s LS failed: DMA mapping failure\n",
1748 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1749 nvmefc_ls_names[w0->ls_cmd] : "");
1750 ret = -EFAULT;
1751 goto out_free;
1752 }
1753
1754 lsop->rport = rport;
1755 lsop->lsrsp = lsrsp;
1756
1757 memcpy(lsop->rqstbuf, lsreqbuf, lsreqbuf_len);
1758 lsop->rqstdatalen = lsreqbuf_len;
1759
1760 spin_lock_irqsave(&rport->lock, flags);
1761 if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE) {
1762 spin_unlock_irqrestore(&rport->lock, flags);
1763 ret = -ENOTCONN;
1764 goto out_unmap;
1765 }
1766 list_add_tail(&lsop->lsrcv_list, &rport->ls_rcv_list);
1767 spin_unlock_irqrestore(&rport->lock, flags);
1768
1769 schedule_work(&rport->lsrcv_work);
1770
1771 return 0;
1772
1773 out_unmap:
1774 fc_dma_unmap_single(lport->dev, lsop->rspdma,
1775 sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1776 out_free:
1777 kfree(lsop->rspbuf);
1778 kfree(lsop->rqstbuf);
1779 kfree(lsop);
1780 out_put:
1781 nvme_fc_rport_put(rport);
1782 return ret;
1783 }
1784 EXPORT_SYMBOL_GPL(nvme_fc_rcv_ls_req);
1785
1786
1787 /* *********************** NVME Ctrl Routines **************************** */
1788
1789 static void
__nvme_fc_exit_request(struct nvme_fc_ctrl * ctrl,struct nvme_fc_fcp_op * op)1790 __nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl,
1791 struct nvme_fc_fcp_op *op)
1792 {
1793 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma,
1794 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1795 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
1796 sizeof(op->cmd_iu), DMA_TO_DEVICE);
1797
1798 atomic_set(&op->state, FCPOP_STATE_UNINIT);
1799 }
1800
1801 static void
nvme_fc_exit_request(struct blk_mq_tag_set * set,struct request * rq,unsigned int hctx_idx)1802 nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1803 unsigned int hctx_idx)
1804 {
1805 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1806
1807 return __nvme_fc_exit_request(to_fc_ctrl(set->driver_data), op);
1808 }
1809
1810 static int
__nvme_fc_abort_op(struct nvme_fc_ctrl * ctrl,struct nvme_fc_fcp_op * op)1811 __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
1812 {
1813 unsigned long flags;
1814 int opstate;
1815
1816 spin_lock_irqsave(&ctrl->lock, flags);
1817 opstate = atomic_xchg(&op->state, FCPOP_STATE_ABORTED);
1818 if (opstate != FCPOP_STATE_ACTIVE)
1819 atomic_set(&op->state, opstate);
1820 else if (test_bit(FCCTRL_TERMIO, &ctrl->flags)) {
1821 op->flags |= FCOP_FLAGS_TERMIO;
1822 ctrl->iocnt++;
1823 }
1824 spin_unlock_irqrestore(&ctrl->lock, flags);
1825
1826 if (opstate != FCPOP_STATE_ACTIVE)
1827 return -ECANCELED;
1828
1829 ctrl->lport->ops->fcp_abort(&ctrl->lport->localport,
1830 &ctrl->rport->remoteport,
1831 op->queue->lldd_handle,
1832 &op->fcp_req);
1833
1834 return 0;
1835 }
1836
1837 static void
nvme_fc_abort_aen_ops(struct nvme_fc_ctrl * ctrl)1838 nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl)
1839 {
1840 struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops;
1841 int i;
1842
1843 /* ensure we've initialized the ops once */
1844 if (!(aen_op->flags & FCOP_FLAGS_AEN))
1845 return;
1846
1847 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++)
1848 __nvme_fc_abort_op(ctrl, aen_op);
1849 }
1850
1851 static inline void
__nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl * ctrl,struct nvme_fc_fcp_op * op,int opstate)1852 __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
1853 struct nvme_fc_fcp_op *op, int opstate)
1854 {
1855 unsigned long flags;
1856
1857 if (opstate == FCPOP_STATE_ABORTED) {
1858 spin_lock_irqsave(&ctrl->lock, flags);
1859 if (test_bit(FCCTRL_TERMIO, &ctrl->flags) &&
1860 op->flags & FCOP_FLAGS_TERMIO) {
1861 if (!--ctrl->iocnt)
1862 wake_up(&ctrl->ioabort_wait);
1863 }
1864 spin_unlock_irqrestore(&ctrl->lock, flags);
1865 }
1866 }
1867
1868 static void
nvme_fc_ctrl_ioerr_work(struct work_struct * work)1869 nvme_fc_ctrl_ioerr_work(struct work_struct *work)
1870 {
1871 struct nvme_fc_ctrl *ctrl =
1872 container_of(work, struct nvme_fc_ctrl, ioerr_work);
1873
1874 nvme_fc_error_recovery(ctrl, "transport detected io error");
1875 }
1876
1877 /*
1878 * nvme_fc_io_getuuid - Routine called to get the appid field
1879 * associated with request by the lldd
1880 * @req:IO request from nvme fc to driver
1881 * Returns: UUID if there is an appid associated with VM or
1882 * NULL if the user/libvirt has not set the appid to VM
1883 */
nvme_fc_io_getuuid(struct nvmefc_fcp_req * req)1884 char *nvme_fc_io_getuuid(struct nvmefc_fcp_req *req)
1885 {
1886 struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1887 struct request *rq = op->rq;
1888
1889 if (!IS_ENABLED(CONFIG_BLK_CGROUP_FC_APPID) || !rq || !rq->bio)
1890 return NULL;
1891 return blkcg_get_fc_appid(rq->bio);
1892 }
1893 EXPORT_SYMBOL_GPL(nvme_fc_io_getuuid);
1894
1895 static void
nvme_fc_fcpio_done(struct nvmefc_fcp_req * req)1896 nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
1897 {
1898 struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1899 struct request *rq = op->rq;
1900 struct nvmefc_fcp_req *freq = &op->fcp_req;
1901 struct nvme_fc_ctrl *ctrl = op->ctrl;
1902 struct nvme_fc_queue *queue = op->queue;
1903 struct nvme_completion *cqe = &op->rsp_iu.cqe;
1904 struct nvme_command *sqe = &op->cmd_iu.sqe;
1905 __le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1);
1906 union nvme_result result;
1907 bool terminate_assoc = true;
1908 int opstate;
1909
1910 /*
1911 * WARNING:
1912 * The current linux implementation of a nvme controller
1913 * allocates a single tag set for all io queues and sizes
1914 * the io queues to fully hold all possible tags. Thus, the
1915 * implementation does not reference or care about the sqhd
1916 * value as it never needs to use the sqhd/sqtail pointers
1917 * for submission pacing.
1918 *
1919 * This affects the FC-NVME implementation in two ways:
1920 * 1) As the value doesn't matter, we don't need to waste
1921 * cycles extracting it from ERSPs and stamping it in the
1922 * cases where the transport fabricates CQEs on successful
1923 * completions.
1924 * 2) The FC-NVME implementation requires that delivery of
1925 * ERSP completions are to go back to the nvme layer in order
1926 * relative to the rsn, such that the sqhd value will always
1927 * be "in order" for the nvme layer. As the nvme layer in
1928 * linux doesn't care about sqhd, there's no need to return
1929 * them in order.
1930 *
1931 * Additionally:
1932 * As the core nvme layer in linux currently does not look at
1933 * every field in the cqe - in cases where the FC transport must
1934 * fabricate a CQE, the following fields will not be set as they
1935 * are not referenced:
1936 * cqe.sqid, cqe.sqhd, cqe.command_id
1937 *
1938 * Failure or error of an individual i/o, in a transport
1939 * detected fashion unrelated to the nvme completion status,
1940 * potentially cause the initiator and target sides to get out
1941 * of sync on SQ head/tail (aka outstanding io count allowed).
1942 * Per FC-NVME spec, failure of an individual command requires
1943 * the connection to be terminated, which in turn requires the
1944 * association to be terminated.
1945 */
1946
1947 opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
1948
1949 fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
1950 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1951
1952 if (opstate == FCPOP_STATE_ABORTED)
1953 status = cpu_to_le16(NVME_SC_HOST_ABORTED_CMD << 1);
1954 else if (freq->status) {
1955 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
1956 dev_info(ctrl->ctrl.device,
1957 "NVME-FC{%d}: io failed due to lldd error %d\n",
1958 ctrl->cnum, freq->status);
1959 }
1960
1961 /*
1962 * For the linux implementation, if we have an unsuccessful
1963 * status, the blk-mq layer can typically be called with the
1964 * non-zero status and the content of the cqe isn't important.
1965 */
1966 if (status)
1967 goto done;
1968
1969 /*
1970 * command completed successfully relative to the wire
1971 * protocol. However, validate anything received and
1972 * extract the status and result from the cqe (create it
1973 * where necessary).
1974 */
1975
1976 switch (freq->rcv_rsplen) {
1977
1978 case 0:
1979 case NVME_FC_SIZEOF_ZEROS_RSP:
1980 /*
1981 * No response payload or 12 bytes of payload (which
1982 * should all be zeros) are considered successful and
1983 * no payload in the CQE by the transport.
1984 */
1985 if (freq->transferred_length !=
1986 be32_to_cpu(op->cmd_iu.data_len)) {
1987 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
1988 dev_info(ctrl->ctrl.device,
1989 "NVME-FC{%d}: io failed due to bad transfer "
1990 "length: %d vs expected %d\n",
1991 ctrl->cnum, freq->transferred_length,
1992 be32_to_cpu(op->cmd_iu.data_len));
1993 goto done;
1994 }
1995 result.u64 = 0;
1996 break;
1997
1998 case sizeof(struct nvme_fc_ersp_iu):
1999 /*
2000 * The ERSP IU contains a full completion with CQE.
2001 * Validate ERSP IU and look at cqe.
2002 */
2003 if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) !=
2004 (freq->rcv_rsplen / 4) ||
2005 be32_to_cpu(op->rsp_iu.xfrd_len) !=
2006 freq->transferred_length ||
2007 op->rsp_iu.ersp_result ||
2008 sqe->common.command_id != cqe->command_id)) {
2009 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
2010 dev_info(ctrl->ctrl.device,
2011 "NVME-FC{%d}: io failed due to bad NVMe_ERSP: "
2012 "iu len %d, xfr len %d vs %d, status code "
2013 "%d, cmdid %d vs %d\n",
2014 ctrl->cnum, be16_to_cpu(op->rsp_iu.iu_len),
2015 be32_to_cpu(op->rsp_iu.xfrd_len),
2016 freq->transferred_length,
2017 op->rsp_iu.ersp_result,
2018 sqe->common.command_id,
2019 cqe->command_id);
2020 goto done;
2021 }
2022 result = cqe->result;
2023 status = cqe->status;
2024 break;
2025
2026 default:
2027 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
2028 dev_info(ctrl->ctrl.device,
2029 "NVME-FC{%d}: io failed due to odd NVMe_xRSP iu "
2030 "len %d\n",
2031 ctrl->cnum, freq->rcv_rsplen);
2032 goto done;
2033 }
2034
2035 terminate_assoc = false;
2036
2037 done:
2038 if (op->flags & FCOP_FLAGS_AEN) {
2039 nvme_complete_async_event(&queue->ctrl->ctrl, status, &result);
2040 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2041 atomic_set(&op->state, FCPOP_STATE_IDLE);
2042 op->flags = FCOP_FLAGS_AEN; /* clear other flags */
2043 nvme_fc_ctrl_put(ctrl);
2044 goto check_error;
2045 }
2046
2047 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2048 if (!nvme_try_complete_req(rq, status, result))
2049 nvme_fc_complete_rq(rq);
2050
2051 check_error:
2052 if (terminate_assoc &&
2053 nvme_ctrl_state(&ctrl->ctrl) != NVME_CTRL_RESETTING)
2054 queue_work(nvme_reset_wq, &ctrl->ioerr_work);
2055 }
2056
2057 static int
__nvme_fc_init_request(struct nvme_fc_ctrl * ctrl,struct nvme_fc_queue * queue,struct nvme_fc_fcp_op * op,struct request * rq,u32 rqno)2058 __nvme_fc_init_request(struct nvme_fc_ctrl *ctrl,
2059 struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op,
2060 struct request *rq, u32 rqno)
2061 {
2062 struct nvme_fcp_op_w_sgl *op_w_sgl =
2063 container_of(op, typeof(*op_w_sgl), op);
2064 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2065 int ret = 0;
2066
2067 memset(op, 0, sizeof(*op));
2068 op->fcp_req.cmdaddr = &op->cmd_iu;
2069 op->fcp_req.cmdlen = sizeof(op->cmd_iu);
2070 op->fcp_req.rspaddr = &op->rsp_iu;
2071 op->fcp_req.rsplen = sizeof(op->rsp_iu);
2072 op->fcp_req.done = nvme_fc_fcpio_done;
2073 op->ctrl = ctrl;
2074 op->queue = queue;
2075 op->rq = rq;
2076 op->rqno = rqno;
2077
2078 cmdiu->format_id = NVME_CMD_FORMAT_ID;
2079 cmdiu->fc_id = NVME_CMD_FC_ID;
2080 cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
2081 if (queue->qnum)
2082 cmdiu->rsv_cat = fccmnd_set_cat_css(0,
2083 (NVME_CC_CSS_NVM >> NVME_CC_CSS_SHIFT));
2084 else
2085 cmdiu->rsv_cat = fccmnd_set_cat_admin(0);
2086
2087 op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev,
2088 &op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE);
2089 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) {
2090 dev_err(ctrl->dev,
2091 "FCP Op failed - cmdiu dma mapping failed.\n");
2092 ret = -EFAULT;
2093 goto out_on_error;
2094 }
2095
2096 op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev,
2097 &op->rsp_iu, sizeof(op->rsp_iu),
2098 DMA_FROM_DEVICE);
2099 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) {
2100 dev_err(ctrl->dev,
2101 "FCP Op failed - rspiu dma mapping failed.\n");
2102 ret = -EFAULT;
2103 goto out_unmap;
2104 }
2105
2106 atomic_set(&op->state, FCPOP_STATE_IDLE);
2107 return 0;
2108
2109 out_unmap:
2110 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
2111 sizeof(op->cmd_iu), DMA_TO_DEVICE);
2112 out_on_error:
2113 return ret;
2114 }
2115
2116 static int
nvme_fc_init_request(struct blk_mq_tag_set * set,struct request * rq,unsigned int hctx_idx,int numa_node)2117 nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq,
2118 unsigned int hctx_idx, int numa_node)
2119 {
2120 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(set->driver_data);
2121 struct nvme_fcp_op_w_sgl *op = blk_mq_rq_to_pdu(rq);
2122 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
2123 struct nvme_fc_queue *queue = &ctrl->queues[queue_idx];
2124 int res;
2125
2126 res = __nvme_fc_init_request(ctrl, queue, &op->op, rq, queue->rqcnt++);
2127 if (res)
2128 return res;
2129 op->op.fcp_req.first_sgl = op->sgl;
2130 op->op.fcp_req.private = &op->priv[0];
2131 nvme_req(rq)->ctrl = &ctrl->ctrl;
2132 nvme_req(rq)->cmd = &op->op.cmd_iu.sqe;
2133 return res;
2134 }
2135
2136 static int
nvme_fc_init_aen_ops(struct nvme_fc_ctrl * ctrl)2137 nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl)
2138 {
2139 struct nvme_fc_fcp_op *aen_op;
2140 struct nvme_fc_cmd_iu *cmdiu;
2141 struct nvme_command *sqe;
2142 void *private = NULL;
2143 int i, ret;
2144
2145 aen_op = ctrl->aen_ops;
2146 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
2147 if (ctrl->lport->ops->fcprqst_priv_sz) {
2148 private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz,
2149 GFP_KERNEL);
2150 if (!private)
2151 return -ENOMEM;
2152 }
2153
2154 cmdiu = &aen_op->cmd_iu;
2155 sqe = &cmdiu->sqe;
2156 ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0],
2157 aen_op, (struct request *)NULL,
2158 (NVME_AQ_BLK_MQ_DEPTH + i));
2159 if (ret) {
2160 kfree(private);
2161 return ret;
2162 }
2163
2164 aen_op->flags = FCOP_FLAGS_AEN;
2165 aen_op->fcp_req.private = private;
2166
2167 memset(sqe, 0, sizeof(*sqe));
2168 sqe->common.opcode = nvme_admin_async_event;
2169 /* Note: core layer may overwrite the sqe.command_id value */
2170 sqe->common.command_id = NVME_AQ_BLK_MQ_DEPTH + i;
2171 }
2172 return 0;
2173 }
2174
2175 static void
nvme_fc_term_aen_ops(struct nvme_fc_ctrl * ctrl)2176 nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl)
2177 {
2178 struct nvme_fc_fcp_op *aen_op;
2179 int i;
2180
2181 cancel_work_sync(&ctrl->ctrl.async_event_work);
2182 aen_op = ctrl->aen_ops;
2183 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
2184 __nvme_fc_exit_request(ctrl, aen_op);
2185
2186 kfree(aen_op->fcp_req.private);
2187 aen_op->fcp_req.private = NULL;
2188 }
2189 }
2190
2191 static inline int
__nvme_fc_init_hctx(struct blk_mq_hw_ctx * hctx,void * data,unsigned int qidx)2192 __nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, unsigned int qidx)
2193 {
2194 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(data);
2195 struct nvme_fc_queue *queue = &ctrl->queues[qidx];
2196
2197 hctx->driver_data = queue;
2198 queue->hctx = hctx;
2199 return 0;
2200 }
2201
2202 static int
nvme_fc_init_hctx(struct blk_mq_hw_ctx * hctx,void * data,unsigned int hctx_idx)2203 nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, unsigned int hctx_idx)
2204 {
2205 return __nvme_fc_init_hctx(hctx, data, hctx_idx + 1);
2206 }
2207
2208 static int
nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx * hctx,void * data,unsigned int hctx_idx)2209 nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
2210 unsigned int hctx_idx)
2211 {
2212 return __nvme_fc_init_hctx(hctx, data, hctx_idx);
2213 }
2214
2215 static void
nvme_fc_init_queue(struct nvme_fc_ctrl * ctrl,int idx)2216 nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx)
2217 {
2218 struct nvme_fc_queue *queue;
2219
2220 queue = &ctrl->queues[idx];
2221 memset(queue, 0, sizeof(*queue));
2222 queue->ctrl = ctrl;
2223 queue->qnum = idx;
2224 atomic_set(&queue->csn, 0);
2225 queue->dev = ctrl->dev;
2226
2227 if (idx > 0)
2228 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
2229 else
2230 queue->cmnd_capsule_len = sizeof(struct nvme_command);
2231
2232 /*
2233 * Considered whether we should allocate buffers for all SQEs
2234 * and CQEs and dma map them - mapping their respective entries
2235 * into the request structures (kernel vm addr and dma address)
2236 * thus the driver could use the buffers/mappings directly.
2237 * It only makes sense if the LLDD would use them for its
2238 * messaging api. It's very unlikely most adapter api's would use
2239 * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
2240 * structures were used instead.
2241 */
2242 }
2243
2244 /*
2245 * This routine terminates a queue at the transport level.
2246 * The transport has already ensured that all outstanding ios on
2247 * the queue have been terminated.
2248 * The transport will send a Disconnect LS request to terminate
2249 * the queue's connection. Termination of the admin queue will also
2250 * terminate the association at the target.
2251 */
2252 static void
nvme_fc_free_queue(struct nvme_fc_queue * queue)2253 nvme_fc_free_queue(struct nvme_fc_queue *queue)
2254 {
2255 if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags))
2256 return;
2257
2258 clear_bit(NVME_FC_Q_LIVE, &queue->flags);
2259 /*
2260 * Current implementation never disconnects a single queue.
2261 * It always terminates a whole association. So there is never
2262 * a disconnect(queue) LS sent to the target.
2263 */
2264
2265 queue->connection_id = 0;
2266 atomic_set(&queue->csn, 0);
2267 }
2268
2269 static void
__nvme_fc_delete_hw_queue(struct nvme_fc_ctrl * ctrl,struct nvme_fc_queue * queue,unsigned int qidx)2270 __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl,
2271 struct nvme_fc_queue *queue, unsigned int qidx)
2272 {
2273 if (ctrl->lport->ops->delete_queue)
2274 ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx,
2275 queue->lldd_handle);
2276 queue->lldd_handle = NULL;
2277 }
2278
2279 static void
nvme_fc_free_io_queues(struct nvme_fc_ctrl * ctrl)2280 nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl)
2281 {
2282 int i;
2283
2284 for (i = 1; i < ctrl->ctrl.queue_count; i++)
2285 nvme_fc_free_queue(&ctrl->queues[i]);
2286 }
2287
2288 static int
__nvme_fc_create_hw_queue(struct nvme_fc_ctrl * ctrl,struct nvme_fc_queue * queue,unsigned int qidx,u16 qsize)2289 __nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl,
2290 struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize)
2291 {
2292 int ret = 0;
2293
2294 queue->lldd_handle = NULL;
2295 if (ctrl->lport->ops->create_queue)
2296 ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport,
2297 qidx, qsize, &queue->lldd_handle);
2298
2299 return ret;
2300 }
2301
2302 static void
nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl * ctrl)2303 nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl)
2304 {
2305 struct nvme_fc_queue *queue = &ctrl->queues[ctrl->ctrl.queue_count - 1];
2306 int i;
2307
2308 for (i = ctrl->ctrl.queue_count - 1; i >= 1; i--, queue--)
2309 __nvme_fc_delete_hw_queue(ctrl, queue, i);
2310 }
2311
2312 static int
nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl * ctrl,u16 qsize)2313 nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
2314 {
2315 struct nvme_fc_queue *queue = &ctrl->queues[1];
2316 int i, ret;
2317
2318 for (i = 1; i < ctrl->ctrl.queue_count; i++, queue++) {
2319 ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize);
2320 if (ret)
2321 goto delete_queues;
2322 }
2323
2324 return 0;
2325
2326 delete_queues:
2327 for (--i; i > 0; i--)
2328 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i);
2329 return ret;
2330 }
2331
2332 static int
nvme_fc_connect_io_queues(struct nvme_fc_ctrl * ctrl,u16 qsize)2333 nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
2334 {
2335 int i, ret = 0;
2336
2337 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
2338 ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize,
2339 (qsize / 5));
2340 if (ret)
2341 break;
2342 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
2343 if (ret)
2344 break;
2345
2346 set_bit(NVME_FC_Q_LIVE, &ctrl->queues[i].flags);
2347 }
2348
2349 return ret;
2350 }
2351
2352 static void
nvme_fc_init_io_queues(struct nvme_fc_ctrl * ctrl)2353 nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl)
2354 {
2355 int i;
2356
2357 for (i = 1; i < ctrl->ctrl.queue_count; i++)
2358 nvme_fc_init_queue(ctrl, i);
2359 }
2360
2361 static void
nvme_fc_ctrl_free(struct kref * ref)2362 nvme_fc_ctrl_free(struct kref *ref)
2363 {
2364 struct nvme_fc_ctrl *ctrl =
2365 container_of(ref, struct nvme_fc_ctrl, ref);
2366 unsigned long flags;
2367 bool owns_opts;
2368
2369 /*
2370 * Presence on the rport list means nvme_fc_init_ctrl() completed,
2371 * and with it ownership of the fabrics options passed to it. If it
2372 * failed instead, the options still belong to nvmf_create_ctrl().
2373 */
2374 spin_lock_irqsave(&ctrl->rport->lock, flags);
2375 owns_opts = !list_empty(&ctrl->ctrl_list);
2376 list_del(&ctrl->ctrl_list);
2377 spin_unlock_irqrestore(&ctrl->rport->lock, flags);
2378
2379 kfree(ctrl->queues);
2380
2381 put_device(ctrl->dev);
2382 nvme_fc_rport_put(ctrl->rport);
2383
2384 ida_free(&nvme_fc_ctrl_cnt, ctrl->cnum);
2385 if (owns_opts)
2386 nvmf_free_options(ctrl->ctrl.opts);
2387 kfree(ctrl);
2388 }
2389
2390 static void
nvme_fc_ctrl_put(struct nvme_fc_ctrl * ctrl)2391 nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl)
2392 {
2393 kref_put(&ctrl->ref, nvme_fc_ctrl_free);
2394 }
2395
2396 static int
nvme_fc_ctrl_get(struct nvme_fc_ctrl * ctrl)2397 nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
2398 {
2399 return kref_get_unless_zero(&ctrl->ref);
2400 }
2401
2402 /*
2403 * All accesses from nvme core layer done - can now free the
2404 * controller. Called after last nvme_put_ctrl() call
2405 */
2406 static void
nvme_fc_free_ctrl(struct nvme_ctrl * nctrl)2407 nvme_fc_free_ctrl(struct nvme_ctrl *nctrl)
2408 {
2409 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2410
2411 WARN_ON(nctrl != &ctrl->ctrl);
2412
2413 nvme_fc_ctrl_put(ctrl);
2414 }
2415
2416 /*
2417 * This routine is used by the transport when it needs to find active
2418 * io on a queue that is to be terminated. The transport uses
2419 * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
2420 * this routine to kill them on a 1 by 1 basis.
2421 *
2422 * As FC allocates FC exchange for each io, the transport must contact
2423 * the LLDD to terminate the exchange, thus releasing the FC exchange.
2424 * After terminating the exchange the LLDD will call the transport's
2425 * normal io done path for the request, but it will have an aborted
2426 * status. The done path will return the io request back to the block
2427 * layer with an error status.
2428 */
nvme_fc_terminate_exchange(struct request * req,void * data)2429 static bool nvme_fc_terminate_exchange(struct request *req, void *data)
2430 {
2431 struct nvme_ctrl *nctrl = data;
2432 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2433 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
2434
2435 op->nreq.flags |= NVME_REQ_CANCELLED;
2436 __nvme_fc_abort_op(ctrl, op);
2437 return true;
2438 }
2439
2440 /*
2441 * This routine runs through all outstanding commands on the association
2442 * and aborts them. This routine is typically called by the
2443 * delete_association routine. It is also called due to an error during
2444 * reconnect. In that scenario, it is most likely a command that initializes
2445 * the controller, including fabric Connect commands on io queues, that
2446 * may have timed out or failed thus the io must be killed for the connect
2447 * thread to see the error.
2448 */
2449 static void
__nvme_fc_abort_outstanding_ios(struct nvme_fc_ctrl * ctrl,bool start_queues)2450 __nvme_fc_abort_outstanding_ios(struct nvme_fc_ctrl *ctrl, bool start_queues)
2451 {
2452 int q;
2453
2454 /*
2455 * if aborting io, the queues are no longer good, mark them
2456 * all as not live.
2457 */
2458 if (ctrl->ctrl.queue_count > 1) {
2459 for (q = 1; q < ctrl->ctrl.queue_count; q++)
2460 clear_bit(NVME_FC_Q_LIVE, &ctrl->queues[q].flags);
2461 }
2462 clear_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags);
2463
2464 /*
2465 * If io queues are present, stop them and terminate all outstanding
2466 * ios on them. As FC allocates FC exchange for each io, the
2467 * transport must contact the LLDD to terminate the exchange,
2468 * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
2469 * to tell us what io's are busy and invoke a transport routine
2470 * to kill them with the LLDD. After terminating the exchange
2471 * the LLDD will call the transport's normal io done path, but it
2472 * will have an aborted status. The done path will return the
2473 * io requests back to the block layer as part of normal completions
2474 * (but with error status).
2475 */
2476 if (ctrl->ctrl.queue_count > 1 && ctrl->ctrl.tagset) {
2477 nvme_quiesce_io_queues(&ctrl->ctrl);
2478 nvme_sync_io_queues(&ctrl->ctrl);
2479 blk_mq_tagset_busy_iter(&ctrl->tag_set,
2480 nvme_fc_terminate_exchange, &ctrl->ctrl);
2481 blk_mq_tagset_wait_completed_request(&ctrl->tag_set);
2482 if (start_queues)
2483 nvme_unquiesce_io_queues(&ctrl->ctrl);
2484 }
2485
2486 /*
2487 * Other transports, which don't have link-level contexts bound
2488 * to sqe's, would try to gracefully shutdown the controller by
2489 * writing the registers for shutdown and polling (call
2490 * nvme_disable_ctrl()). Given a bunch of i/o was potentially
2491 * just aborted and we will wait on those contexts, and given
2492 * there was no indication of how live the controller is on the
2493 * link, don't send more io to create more contexts for the
2494 * shutdown. Let the controller fail via keepalive failure if
2495 * its still present.
2496 */
2497
2498 /*
2499 * clean up the admin queue. Same thing as above.
2500 */
2501 nvme_quiesce_admin_queue(&ctrl->ctrl);
2502 blk_sync_queue(ctrl->ctrl.admin_q);
2503 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
2504 nvme_fc_terminate_exchange, &ctrl->ctrl);
2505 blk_mq_tagset_wait_completed_request(&ctrl->admin_tag_set);
2506 if (start_queues)
2507 nvme_unquiesce_admin_queue(&ctrl->ctrl);
2508 }
2509
2510 static void
nvme_fc_error_recovery(struct nvme_fc_ctrl * ctrl,char * errmsg)2511 nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
2512 {
2513 enum nvme_ctrl_state state = nvme_ctrl_state(&ctrl->ctrl);
2514
2515 /*
2516 * if an error (io timeout, etc) while (re)connecting, the remote
2517 * port requested terminating of the association (disconnect_ls)
2518 * or an error (timeout or abort) occurred on an io while creating
2519 * the controller. Abort any ios on the association and let the
2520 * create_association error path resolve things.
2521 */
2522 if (state == NVME_CTRL_CONNECTING) {
2523 __nvme_fc_abort_outstanding_ios(ctrl, true);
2524 dev_warn(ctrl->ctrl.device,
2525 "NVME-FC{%d}: transport error during (re)connect\n",
2526 ctrl->cnum);
2527 return;
2528 }
2529
2530 /* Otherwise, only proceed if in LIVE state - e.g. on first error */
2531 if (state != NVME_CTRL_LIVE)
2532 return;
2533
2534 dev_warn(ctrl->ctrl.device,
2535 "NVME-FC{%d}: transport association event: %s\n",
2536 ctrl->cnum, errmsg);
2537 dev_warn(ctrl->ctrl.device,
2538 "NVME-FC{%d}: resetting controller\n", ctrl->cnum);
2539
2540 nvme_reset_ctrl(&ctrl->ctrl);
2541 }
2542
nvme_fc_timeout(struct request * rq)2543 static enum blk_eh_timer_return nvme_fc_timeout(struct request *rq)
2544 {
2545 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2546 struct nvme_fc_ctrl *ctrl = op->ctrl;
2547 u16 qnum = op->queue->qnum;
2548 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2549 struct nvme_command *sqe = &cmdiu->sqe;
2550
2551 /*
2552 * Attempt to abort the offending command. Command completion
2553 * will detect the aborted io and will fail the connection.
2554 */
2555 dev_info(ctrl->ctrl.device,
2556 "NVME-FC{%d.%d}: io timeout: opcode %d fctype %d (%s) w10/11: "
2557 "x%08x/x%08x\n",
2558 ctrl->cnum, qnum, sqe->common.opcode, sqe->fabrics.fctype,
2559 nvme_fabrics_opcode_str(qnum, sqe),
2560 sqe->common.cdw10, sqe->common.cdw11);
2561 if (__nvme_fc_abort_op(ctrl, op))
2562 nvme_fc_error_recovery(ctrl, "io timeout abort failed");
2563
2564 /*
2565 * the io abort has been initiated. Have the reset timer
2566 * restarted and the abort completion will complete the io
2567 * shortly. Avoids a synchronous wait while the abort finishes.
2568 */
2569 return BLK_EH_RESET_TIMER;
2570 }
2571
2572 static int
nvme_fc_map_data(struct nvme_fc_ctrl * ctrl,struct request * rq,struct nvme_fc_fcp_op * op)2573 nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2574 struct nvme_fc_fcp_op *op)
2575 {
2576 struct nvmefc_fcp_req *freq = &op->fcp_req;
2577 int ret;
2578
2579 freq->sg_cnt = 0;
2580
2581 if (!blk_rq_nr_phys_segments(rq))
2582 return 0;
2583
2584 freq->sg_table.sgl = freq->first_sgl;
2585 ret = sg_alloc_table_chained(&freq->sg_table,
2586 blk_rq_nr_phys_segments(rq), freq->sg_table.sgl,
2587 NVME_INLINE_SG_CNT);
2588 if (ret)
2589 return -ENOMEM;
2590
2591 op->nents = blk_rq_map_sg(rq, freq->sg_table.sgl);
2592 WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
2593 freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
2594 op->nents, rq_dma_dir(rq));
2595 if (unlikely(freq->sg_cnt <= 0)) {
2596 sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2597 freq->sg_cnt = 0;
2598 return -EFAULT;
2599 }
2600
2601 /*
2602 * TODO: blk_integrity_rq(rq) for DIF
2603 */
2604 return 0;
2605 }
2606
2607 static void
nvme_fc_unmap_data(struct nvme_fc_ctrl * ctrl,struct request * rq,struct nvme_fc_fcp_op * op)2608 nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2609 struct nvme_fc_fcp_op *op)
2610 {
2611 struct nvmefc_fcp_req *freq = &op->fcp_req;
2612
2613 if (!freq->sg_cnt)
2614 return;
2615
2616 fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
2617 rq_dma_dir(rq));
2618
2619 sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2620
2621 freq->sg_cnt = 0;
2622 }
2623
2624 /*
2625 * In FC, the queue is a logical thing. At transport connect, the target
2626 * creates its "queue" and returns a handle that is to be given to the
2627 * target whenever it posts something to the corresponding SQ. When an
2628 * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
2629 * command contained within the SQE, an io, and assigns a FC exchange
2630 * to it. The SQE and the associated SQ handle are sent in the initial
2631 * CMD IU sents on the exchange. All transfers relative to the io occur
2632 * as part of the exchange. The CQE is the last thing for the io,
2633 * which is transferred (explicitly or implicitly) with the RSP IU
2634 * sent on the exchange. After the CQE is received, the FC exchange is
2635 * terminated and the Exchange may be used on a different io.
2636 *
2637 * The transport to LLDD api has the transport making a request for a
2638 * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
2639 * resource and transfers the command. The LLDD will then process all
2640 * steps to complete the io. Upon completion, the transport done routine
2641 * is called.
2642 *
2643 * So - while the operation is outstanding to the LLDD, there is a link
2644 * level FC exchange resource that is also outstanding. This must be
2645 * considered in all cleanup operations.
2646 */
2647 static blk_status_t
nvme_fc_start_fcp_op(struct nvme_fc_ctrl * ctrl,struct nvme_fc_queue * queue,struct nvme_fc_fcp_op * op,u32 data_len,enum nvmefc_fcp_datadir io_dir)2648 nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
2649 struct nvme_fc_fcp_op *op, u32 data_len,
2650 enum nvmefc_fcp_datadir io_dir)
2651 {
2652 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2653 struct nvme_command *sqe = &cmdiu->sqe;
2654 int ret, opstate;
2655
2656 /*
2657 * before attempting to send the io, check to see if we believe
2658 * the target device is present
2659 */
2660 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
2661 return BLK_STS_RESOURCE;
2662
2663 if (!nvme_fc_ctrl_get(ctrl))
2664 return BLK_STS_IOERR;
2665
2666 /* format the FC-NVME CMD IU and fcp_req */
2667 cmdiu->connection_id = cpu_to_be64(queue->connection_id);
2668 cmdiu->data_len = cpu_to_be32(data_len);
2669 switch (io_dir) {
2670 case NVMEFC_FCP_WRITE:
2671 cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
2672 break;
2673 case NVMEFC_FCP_READ:
2674 cmdiu->flags = FCNVME_CMD_FLAGS_READ;
2675 break;
2676 case NVMEFC_FCP_NODATA:
2677 cmdiu->flags = 0;
2678 break;
2679 }
2680 op->fcp_req.payload_length = data_len;
2681 op->fcp_req.io_dir = io_dir;
2682 op->fcp_req.transferred_length = 0;
2683 op->fcp_req.rcv_rsplen = 0;
2684 op->fcp_req.status = NVME_SC_SUCCESS;
2685 op->fcp_req.sqid = cpu_to_le16(queue->qnum);
2686
2687 /*
2688 * validate per fabric rules, set fields mandated by fabric spec
2689 * as well as those by FC-NVME spec.
2690 */
2691 WARN_ON_ONCE(sqe->common.metadata);
2692 sqe->common.flags |= NVME_CMD_SGL_METABUF;
2693
2694 /*
2695 * format SQE DPTR field per FC-NVME rules:
2696 * type=0x5 Transport SGL Data Block Descriptor
2697 * subtype=0xA Transport-specific value
2698 * address=0
2699 * length=length of the data series
2700 */
2701 sqe->rw.dptr.sgl.type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2702 NVME_SGL_FMT_TRANSPORT_A;
2703 sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
2704 sqe->rw.dptr.sgl.addr = 0;
2705
2706 if (!(op->flags & FCOP_FLAGS_AEN)) {
2707 ret = nvme_fc_map_data(ctrl, op->rq, op);
2708 if (ret < 0) {
2709 nvme_cleanup_cmd(op->rq);
2710 nvme_fc_ctrl_put(ctrl);
2711 if (ret == -ENOMEM || ret == -EAGAIN)
2712 return BLK_STS_RESOURCE;
2713 return BLK_STS_IOERR;
2714 }
2715 }
2716
2717 fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
2718 sizeof(op->cmd_iu), DMA_TO_DEVICE);
2719
2720 atomic_set(&op->state, FCPOP_STATE_ACTIVE);
2721
2722 if (!(op->flags & FCOP_FLAGS_AEN))
2723 nvme_start_request(op->rq);
2724
2725 cmdiu->csn = cpu_to_be32(atomic_inc_return(&queue->csn));
2726 ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
2727 &ctrl->rport->remoteport,
2728 queue->lldd_handle, &op->fcp_req);
2729
2730 if (ret) {
2731 /*
2732 * If the lld fails to send the command is there an issue with
2733 * the csn value? If the command that fails is the Connect,
2734 * no - as the connection won't be live. If it is a command
2735 * post-connect, it's possible a gap in csn may be created.
2736 * Does this matter? As Linux initiators don't send fused
2737 * commands, no. The gap would exist, but as there's nothing
2738 * that depends on csn order to be delivered on the target
2739 * side, it shouldn't hurt. It would be difficult for a
2740 * target to even detect the csn gap as it has no idea when the
2741 * cmd with the csn was supposed to arrive.
2742 */
2743 opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
2744 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2745
2746 if (!(op->flags & FCOP_FLAGS_AEN)) {
2747 nvme_fc_unmap_data(ctrl, op->rq, op);
2748 nvme_cleanup_cmd(op->rq);
2749 }
2750
2751 nvme_fc_ctrl_put(ctrl);
2752
2753 if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE &&
2754 ret != -EBUSY)
2755 return BLK_STS_IOERR;
2756
2757 return BLK_STS_RESOURCE;
2758 }
2759
2760 return BLK_STS_OK;
2761 }
2762
2763 static blk_status_t
nvme_fc_queue_rq(struct blk_mq_hw_ctx * hctx,const struct blk_mq_queue_data * bd)2764 nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
2765 const struct blk_mq_queue_data *bd)
2766 {
2767 struct nvme_ns *ns = hctx->queue->queuedata;
2768 struct nvme_fc_queue *queue = hctx->driver_data;
2769 struct nvme_fc_ctrl *ctrl = queue->ctrl;
2770 struct request *rq = bd->rq;
2771 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2772 enum nvmefc_fcp_datadir io_dir;
2773 bool queue_ready = test_bit(NVME_FC_Q_LIVE, &queue->flags);
2774 u32 data_len;
2775 blk_status_t ret;
2776
2777 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE ||
2778 !nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2779 return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq);
2780
2781 ret = nvme_setup_cmd(ns, rq);
2782 if (ret)
2783 return ret;
2784
2785 /*
2786 * nvme core doesn't quite treat the rq opaquely. Commands such
2787 * as WRITE ZEROES will return a non-zero rq payload_bytes yet
2788 * there is no actual payload to be transferred.
2789 * To get it right, key data transmission on there being 1 or
2790 * more physical segments in the sg list. If there are no
2791 * physical segments, there is no payload.
2792 */
2793 if (blk_rq_nr_phys_segments(rq)) {
2794 data_len = blk_rq_payload_bytes(rq);
2795 io_dir = ((rq_data_dir(rq) == WRITE) ?
2796 NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
2797 } else {
2798 data_len = 0;
2799 io_dir = NVMEFC_FCP_NODATA;
2800 }
2801
2802
2803 return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
2804 }
2805
2806 static void
nvme_fc_submit_async_event(struct nvme_ctrl * arg)2807 nvme_fc_submit_async_event(struct nvme_ctrl *arg)
2808 {
2809 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
2810 struct nvme_fc_fcp_op *aen_op;
2811 blk_status_t ret;
2812
2813 if (test_bit(FCCTRL_TERMIO, &ctrl->flags))
2814 return;
2815
2816 aen_op = &ctrl->aen_ops[0];
2817
2818 ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
2819 NVMEFC_FCP_NODATA);
2820 if (ret)
2821 dev_err(ctrl->ctrl.device,
2822 "failed async event work\n");
2823 }
2824
2825 static void
nvme_fc_complete_rq(struct request * rq)2826 nvme_fc_complete_rq(struct request *rq)
2827 {
2828 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2829 struct nvme_fc_ctrl *ctrl = op->ctrl;
2830
2831 atomic_set(&op->state, FCPOP_STATE_IDLE);
2832 op->flags &= ~FCOP_FLAGS_TERMIO;
2833
2834 nvme_fc_unmap_data(ctrl, rq, op);
2835 nvme_complete_rq(rq);
2836 nvme_fc_ctrl_put(ctrl);
2837 }
2838
nvme_fc_map_queues(struct blk_mq_tag_set * set)2839 static void nvme_fc_map_queues(struct blk_mq_tag_set *set)
2840 {
2841 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(set->driver_data);
2842 int i;
2843
2844 for (i = 0; i < set->nr_maps; i++) {
2845 struct blk_mq_queue_map *map = &set->map[i];
2846
2847 if (!map->nr_queues) {
2848 WARN_ON(i == HCTX_TYPE_DEFAULT);
2849 continue;
2850 }
2851
2852 /* Call LLDD map queue functionality if defined */
2853 if (ctrl->lport->ops->map_queues)
2854 ctrl->lport->ops->map_queues(&ctrl->lport->localport,
2855 map);
2856 else
2857 blk_mq_map_queues(map);
2858 }
2859 }
2860
2861 static const struct blk_mq_ops nvme_fc_mq_ops = {
2862 .queue_rq = nvme_fc_queue_rq,
2863 .complete = nvme_fc_complete_rq,
2864 .init_request = nvme_fc_init_request,
2865 .exit_request = nvme_fc_exit_request,
2866 .init_hctx = nvme_fc_init_hctx,
2867 .timeout = nvme_fc_timeout,
2868 .map_queues = nvme_fc_map_queues,
2869 };
2870
2871 static int
nvme_fc_create_io_queues(struct nvme_fc_ctrl * ctrl)2872 nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl)
2873 {
2874 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2875 unsigned int nr_io_queues;
2876 int ret;
2877
2878 nr_io_queues = min3(opts->nr_io_queues, num_online_cpus(),
2879 ctrl->lport->ops->max_hw_queues);
2880 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2881 if (ret) {
2882 dev_info(ctrl->ctrl.device,
2883 "set_queue_count failed: %d\n", ret);
2884 return ret;
2885 }
2886
2887 ctrl->ctrl.queue_count = nr_io_queues + 1;
2888 if (!nr_io_queues)
2889 return 0;
2890
2891 nvme_fc_init_io_queues(ctrl);
2892
2893 ret = nvme_alloc_io_tag_set(&ctrl->ctrl, &ctrl->tag_set,
2894 &nvme_fc_mq_ops, 1,
2895 struct_size_t(struct nvme_fcp_op_w_sgl, priv,
2896 ctrl->lport->ops->fcprqst_priv_sz));
2897 if (ret)
2898 return ret;
2899
2900 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2901 if (ret)
2902 goto out_cleanup_tagset;
2903
2904 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2905 if (ret)
2906 goto out_delete_hw_queues;
2907
2908 ctrl->ioq_live = true;
2909
2910 return 0;
2911
2912 out_delete_hw_queues:
2913 nvme_fc_delete_hw_io_queues(ctrl);
2914 out_cleanup_tagset:
2915 /*
2916 * In CONNECTING state ctrl->ioerr_work will abort both admin
2917 * and io tagsets. Cancel it first before removing io tagset.
2918 */
2919 cancel_work_sync(&ctrl->ioerr_work);
2920 nvme_remove_io_tag_set(&ctrl->ctrl);
2921 nvme_fc_free_io_queues(ctrl);
2922
2923 /* force put free routine to ignore io queues */
2924 ctrl->ctrl.tagset = NULL;
2925
2926 return ret;
2927 }
2928
2929 static int
nvme_fc_recreate_io_queues(struct nvme_fc_ctrl * ctrl)2930 nvme_fc_recreate_io_queues(struct nvme_fc_ctrl *ctrl)
2931 {
2932 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2933 u32 prior_ioq_cnt = ctrl->ctrl.queue_count - 1;
2934 unsigned int nr_io_queues;
2935 int ret;
2936
2937 nr_io_queues = min3(opts->nr_io_queues, num_online_cpus(),
2938 ctrl->lport->ops->max_hw_queues);
2939 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2940 if (ret) {
2941 dev_info(ctrl->ctrl.device,
2942 "set_queue_count failed: %d\n", ret);
2943 return ret;
2944 }
2945
2946 if (!nr_io_queues && prior_ioq_cnt) {
2947 dev_info(ctrl->ctrl.device,
2948 "Fail Reconnect: At least 1 io queue "
2949 "required (was %d)\n", prior_ioq_cnt);
2950 return -ENOSPC;
2951 }
2952
2953 ctrl->ctrl.queue_count = nr_io_queues + 1;
2954 /* check for io queues existing */
2955 if (ctrl->ctrl.queue_count == 1)
2956 return 0;
2957
2958 if (prior_ioq_cnt != nr_io_queues) {
2959 dev_info(ctrl->ctrl.device,
2960 "reconnect: revising io queue count from %d to %d\n",
2961 prior_ioq_cnt, nr_io_queues);
2962 blk_mq_update_nr_hw_queues(&ctrl->tag_set, nr_io_queues);
2963 }
2964
2965 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2966 if (ret)
2967 goto out_free_io_queues;
2968
2969 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2970 if (ret)
2971 goto out_delete_hw_queues;
2972
2973 return 0;
2974
2975 out_delete_hw_queues:
2976 nvme_fc_delete_hw_io_queues(ctrl);
2977 out_free_io_queues:
2978 nvme_fc_free_io_queues(ctrl);
2979 return ret;
2980 }
2981
2982 static void
nvme_fc_rport_active_on_lport(struct nvme_fc_rport * rport)2983 nvme_fc_rport_active_on_lport(struct nvme_fc_rport *rport)
2984 {
2985 struct nvme_fc_lport *lport = rport->lport;
2986
2987 atomic_inc(&lport->act_rport_cnt);
2988 }
2989
2990 static void
nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport * rport)2991 nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport *rport)
2992 {
2993 struct nvme_fc_lport *lport = rport->lport;
2994 u32 cnt;
2995
2996 cnt = atomic_dec_return(&lport->act_rport_cnt);
2997 if (cnt == 0 && lport->localport.port_state == FC_OBJSTATE_DELETED)
2998 lport->ops->localport_delete(&lport->localport);
2999 }
3000
3001 static int
nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl * ctrl)3002 nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl *ctrl)
3003 {
3004 struct nvme_fc_rport *rport = ctrl->rport;
3005 u32 cnt;
3006
3007 if (test_and_set_bit(ASSOC_ACTIVE, &ctrl->flags))
3008 return 1;
3009
3010 cnt = atomic_inc_return(&rport->act_ctrl_cnt);
3011 if (cnt == 1)
3012 nvme_fc_rport_active_on_lport(rport);
3013
3014 return 0;
3015 }
3016
3017 static int
nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl * ctrl)3018 nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl *ctrl)
3019 {
3020 struct nvme_fc_rport *rport = ctrl->rport;
3021 struct nvme_fc_lport *lport = rport->lport;
3022 u32 cnt;
3023
3024 /* clearing of ctrl->flags ASSOC_ACTIVE bit is in association delete */
3025
3026 cnt = atomic_dec_return(&rport->act_ctrl_cnt);
3027 if (cnt == 0) {
3028 if (rport->remoteport.port_state == FC_OBJSTATE_DELETED)
3029 lport->ops->remoteport_delete(&rport->remoteport);
3030 nvme_fc_rport_inactive_on_lport(rport);
3031 }
3032
3033 return 0;
3034 }
3035
3036 /*
3037 * This routine restarts the controller on the host side, and
3038 * on the link side, recreates the controller association.
3039 */
3040 static int
nvme_fc_create_association(struct nvme_fc_ctrl * ctrl)3041 nvme_fc_create_association(struct nvme_fc_ctrl *ctrl)
3042 {
3043 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
3044 struct nvmefc_ls_rcv_op *disls = NULL;
3045 unsigned long flags;
3046 int ret;
3047
3048 ++ctrl->ctrl.nr_reconnects;
3049
3050 spin_lock_irqsave(&ctrl->rport->lock, flags);
3051 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE) {
3052 spin_unlock_irqrestore(&ctrl->rport->lock, flags);
3053 return -ENODEV;
3054 }
3055
3056 if (nvme_fc_ctlr_active_on_rport(ctrl)) {
3057 spin_unlock_irqrestore(&ctrl->rport->lock, flags);
3058 return -ENOTUNIQ;
3059 }
3060 spin_unlock_irqrestore(&ctrl->rport->lock, flags);
3061
3062 dev_info(ctrl->ctrl.device,
3063 "NVME-FC{%d}: create association : host wwpn 0x%016llx "
3064 " rport wwpn 0x%016llx: NQN \"%s\"\n",
3065 ctrl->cnum, ctrl->lport->localport.port_name,
3066 ctrl->rport->remoteport.port_name, ctrl->ctrl.opts->subsysnqn);
3067
3068 clear_bit(ASSOC_FAILED, &ctrl->flags);
3069
3070 /*
3071 * Create the admin queue
3072 */
3073
3074 ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0,
3075 NVME_AQ_DEPTH);
3076 if (ret)
3077 goto out_free_queue;
3078
3079 ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0],
3080 NVME_AQ_DEPTH, (NVME_AQ_DEPTH / 4));
3081 if (ret)
3082 goto out_delete_hw_queue;
3083
3084 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
3085 if (ret)
3086 goto out_disconnect_admin_queue;
3087
3088 set_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags);
3089
3090 /*
3091 * Check controller capabilities
3092 *
3093 * todo:- add code to check if ctrl attributes changed from
3094 * prior connection values
3095 */
3096
3097 ret = nvme_enable_ctrl(&ctrl->ctrl);
3098 if (!ret && test_bit(ASSOC_FAILED, &ctrl->flags))
3099 ret = -EIO;
3100 if (ret)
3101 goto out_disconnect_admin_queue;
3102
3103 ctrl->ctrl.max_segments = ctrl->lport->ops->max_sgl_segments;
3104 ctrl->ctrl.max_hw_sectors = ctrl->ctrl.max_segments <<
3105 (ilog2(SZ_4K) - 9);
3106
3107 nvme_unquiesce_admin_queue(&ctrl->ctrl);
3108
3109 ret = nvme_init_ctrl_finish(&ctrl->ctrl, false);
3110 if (ret)
3111 goto out_disconnect_admin_queue;
3112 if (test_bit(ASSOC_FAILED, &ctrl->flags)) {
3113 ret = -EIO;
3114 goto out_stop_keep_alive;
3115 }
3116 /* sanity checks */
3117
3118 /* FC-NVME does not have other data in the capsule */
3119 if (ctrl->ctrl.icdoff) {
3120 dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n",
3121 ctrl->ctrl.icdoff);
3122 ret = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
3123 goto out_stop_keep_alive;
3124 }
3125
3126 /* FC-NVME supports normal SGL Data Block Descriptors */
3127 if (!nvme_ctrl_sgl_supported(&ctrl->ctrl)) {
3128 dev_err(ctrl->ctrl.device,
3129 "Mandatory sgls are not supported!\n");
3130 ret = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
3131 goto out_stop_keep_alive;
3132 }
3133
3134 if (opts->queue_size > ctrl->ctrl.maxcmd) {
3135 /* warn if maxcmd is lower than queue_size */
3136 dev_warn(ctrl->ctrl.device,
3137 "queue_size %zu > ctrl maxcmd %u, reducing "
3138 "to maxcmd\n",
3139 opts->queue_size, ctrl->ctrl.maxcmd);
3140 opts->queue_size = ctrl->ctrl.maxcmd;
3141 ctrl->ctrl.sqsize = opts->queue_size - 1;
3142 }
3143
3144 ret = nvme_fc_init_aen_ops(ctrl);
3145 if (ret)
3146 goto out_term_aen_ops;
3147
3148 /*
3149 * Create the io queues
3150 */
3151
3152 if (ctrl->ctrl.queue_count > 1) {
3153 if (!ctrl->ioq_live)
3154 ret = nvme_fc_create_io_queues(ctrl);
3155 else
3156 ret = nvme_fc_recreate_io_queues(ctrl);
3157 }
3158 if (!ret && test_bit(ASSOC_FAILED, &ctrl->flags))
3159 ret = -EIO;
3160 if (ret)
3161 goto out_term_aen_ops;
3162
3163 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE)) {
3164 ret = -EIO;
3165 goto out_term_aen_ops;
3166 }
3167
3168 /* accumulate reconnect attempts before resetting it to zero */
3169 atomic_long_add(ctrl->ctrl.nr_reconnects, &ctrl->ctrl.acc_reconnects);
3170 ctrl->ctrl.nr_reconnects = 0;
3171 nvme_start_ctrl(&ctrl->ctrl);
3172
3173 return 0; /* Success */
3174
3175 out_term_aen_ops:
3176 nvme_fc_term_aen_ops(ctrl);
3177 out_stop_keep_alive:
3178 nvme_stop_keep_alive(&ctrl->ctrl);
3179 out_disconnect_admin_queue:
3180 dev_warn(ctrl->ctrl.device,
3181 "NVME-FC{%d}: create_assoc failed, assoc_id %llx ret %d\n",
3182 ctrl->cnum, ctrl->association_id, ret);
3183 /* send a Disconnect(association) LS to fc-nvme target */
3184 nvme_fc_xmt_disconnect_assoc(ctrl);
3185 spin_lock_irqsave(&ctrl->lock, flags);
3186 ctrl->association_id = 0;
3187 disls = ctrl->rcv_disconn;
3188 ctrl->rcv_disconn = NULL;
3189 spin_unlock_irqrestore(&ctrl->lock, flags);
3190 if (disls)
3191 nvme_fc_xmt_ls_rsp(disls);
3192 out_delete_hw_queue:
3193 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
3194 out_free_queue:
3195 nvme_fc_free_queue(&ctrl->queues[0]);
3196 clear_bit(ASSOC_ACTIVE, &ctrl->flags);
3197 nvme_fc_ctlr_inactive_on_rport(ctrl);
3198
3199 return ret;
3200 }
3201
3202
3203 /*
3204 * This routine stops operation of the controller on the host side.
3205 * On the host os stack side: Admin and IO queues are stopped,
3206 * outstanding ios on them terminated via FC ABTS.
3207 * On the link side: the association is terminated.
3208 */
3209 static void
nvme_fc_delete_association(struct nvme_fc_ctrl * ctrl)3210 nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
3211 {
3212 struct nvmefc_ls_rcv_op *disls = NULL;
3213 unsigned long flags;
3214
3215 if (!test_and_clear_bit(ASSOC_ACTIVE, &ctrl->flags))
3216 return;
3217
3218 spin_lock_irqsave(&ctrl->lock, flags);
3219 set_bit(FCCTRL_TERMIO, &ctrl->flags);
3220 ctrl->iocnt = 0;
3221 spin_unlock_irqrestore(&ctrl->lock, flags);
3222
3223 __nvme_fc_abort_outstanding_ios(ctrl, false);
3224
3225 /* kill the aens as they are a separate path */
3226 nvme_fc_abort_aen_ops(ctrl);
3227
3228 /* wait for all io that had to be aborted */
3229 spin_lock_irq(&ctrl->lock);
3230 wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock);
3231 clear_bit(FCCTRL_TERMIO, &ctrl->flags);
3232 spin_unlock_irq(&ctrl->lock);
3233
3234 nvme_fc_term_aen_ops(ctrl);
3235
3236 /*
3237 * send a Disconnect(association) LS to fc-nvme target
3238 * Note: could have been sent at top of process, but
3239 * cleaner on link traffic if after the aborts complete.
3240 * Note: if association doesn't exist, association_id will be 0
3241 */
3242 if (ctrl->association_id)
3243 nvme_fc_xmt_disconnect_assoc(ctrl);
3244
3245 spin_lock_irqsave(&ctrl->lock, flags);
3246 ctrl->association_id = 0;
3247 disls = ctrl->rcv_disconn;
3248 ctrl->rcv_disconn = NULL;
3249 spin_unlock_irqrestore(&ctrl->lock, flags);
3250 if (disls)
3251 /*
3252 * if a Disconnect Request was waiting for a response, send
3253 * now that all ABTS's have been issued (and are complete).
3254 */
3255 nvme_fc_xmt_ls_rsp(disls);
3256
3257 if (ctrl->ctrl.tagset) {
3258 nvme_fc_delete_hw_io_queues(ctrl);
3259 nvme_fc_free_io_queues(ctrl);
3260 }
3261
3262 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
3263 nvme_fc_free_queue(&ctrl->queues[0]);
3264
3265 /* re-enable the admin_q so anything new can fast fail */
3266 nvme_unquiesce_admin_queue(&ctrl->ctrl);
3267
3268 /* resume the io queues so that things will fast fail */
3269 nvme_unquiesce_io_queues(&ctrl->ctrl);
3270
3271 nvme_fc_ctlr_inactive_on_rport(ctrl);
3272 }
3273
3274 static void
nvme_fc_delete_ctrl(struct nvme_ctrl * nctrl)3275 nvme_fc_delete_ctrl(struct nvme_ctrl *nctrl)
3276 {
3277 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
3278
3279 cancel_delayed_work_sync(&ctrl->connect_work);
3280
3281 /*
3282 * kill the association on the link side. this will block
3283 * waiting for io to terminate
3284 */
3285 nvme_fc_delete_association(ctrl);
3286 cancel_work_sync(&ctrl->ioerr_work);
3287
3288 if (ctrl->ctrl.tagset)
3289 nvme_remove_io_tag_set(&ctrl->ctrl);
3290
3291 nvme_unquiesce_admin_queue(&ctrl->ctrl);
3292 nvme_remove_admin_tag_set(&ctrl->ctrl);
3293 }
3294
3295 static void
nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl * ctrl,int status)3296 nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status)
3297 {
3298 struct nvme_fc_rport *rport = ctrl->rport;
3299 struct nvme_fc_remote_port *portptr = &rport->remoteport;
3300 unsigned long recon_delay = ctrl->ctrl.opts->reconnect_delay * HZ;
3301 bool recon = true;
3302
3303 if (nvme_ctrl_state(&ctrl->ctrl) != NVME_CTRL_CONNECTING)
3304 return;
3305
3306 if (portptr->port_state == FC_OBJSTATE_ONLINE) {
3307 dev_info(ctrl->ctrl.device,
3308 "NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
3309 ctrl->cnum, status);
3310 } else if (time_after_eq(jiffies, rport->dev_loss_end))
3311 recon = false;
3312
3313 if (recon && nvmf_should_reconnect(&ctrl->ctrl, status)) {
3314 if (portptr->port_state == FC_OBJSTATE_ONLINE)
3315 dev_info(ctrl->ctrl.device,
3316 "NVME-FC{%d}: Reconnect attempt in %ld "
3317 "seconds\n",
3318 ctrl->cnum, recon_delay / HZ);
3319 else if (time_after(jiffies + recon_delay, rport->dev_loss_end))
3320 recon_delay = rport->dev_loss_end - jiffies;
3321
3322 queue_delayed_work(nvme_wq, &ctrl->connect_work, recon_delay);
3323 } else {
3324 if (portptr->port_state == FC_OBJSTATE_ONLINE) {
3325 if (status > 0 && (status & NVME_STATUS_DNR))
3326 dev_warn(ctrl->ctrl.device,
3327 "NVME-FC{%d}: reconnect failure\n",
3328 ctrl->cnum);
3329 else
3330 dev_warn(ctrl->ctrl.device,
3331 "NVME-FC{%d}: Max reconnect attempts "
3332 "(%d) reached.\n",
3333 ctrl->cnum, ctrl->ctrl.nr_reconnects);
3334 } else
3335 dev_warn(ctrl->ctrl.device,
3336 "NVME-FC{%d}: dev_loss_tmo (%d) expired "
3337 "while waiting for remoteport connectivity.\n",
3338 ctrl->cnum, min_t(int, portptr->dev_loss_tmo,
3339 (ctrl->ctrl.opts->max_reconnects *
3340 ctrl->ctrl.opts->reconnect_delay)));
3341 WARN_ON(nvme_delete_ctrl(&ctrl->ctrl));
3342 }
3343 }
3344
3345 static void
nvme_fc_reset_ctrl_work(struct work_struct * work)3346 nvme_fc_reset_ctrl_work(struct work_struct *work)
3347 {
3348 struct nvme_fc_ctrl *ctrl =
3349 container_of(work, struct nvme_fc_ctrl, ctrl.reset_work);
3350
3351 nvme_stop_ctrl(&ctrl->ctrl);
3352
3353 /* will block will waiting for io to terminate */
3354 nvme_fc_delete_association(ctrl);
3355
3356 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING))
3357 dev_err(ctrl->ctrl.device,
3358 "NVME-FC{%d}: error_recovery: Couldn't change state "
3359 "to CONNECTING\n", ctrl->cnum);
3360
3361 if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE) {
3362 if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
3363 dev_err(ctrl->ctrl.device,
3364 "NVME-FC{%d}: failed to schedule connect "
3365 "after reset\n", ctrl->cnum);
3366 } else {
3367 flush_delayed_work(&ctrl->connect_work);
3368 }
3369 } else {
3370 nvme_fc_reconnect_or_delete(ctrl, -ENOTCONN);
3371 }
3372 }
3373
3374
3375 static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
3376 .name = "fc",
3377 .module = THIS_MODULE,
3378 .flags = NVME_F_FABRICS,
3379 .reg_read32 = nvmf_reg_read32,
3380 .reg_read64 = nvmf_reg_read64,
3381 .reg_write32 = nvmf_reg_write32,
3382 .subsystem_reset = nvmf_subsystem_reset,
3383 .free_ctrl = nvme_fc_free_ctrl,
3384 .submit_async_event = nvme_fc_submit_async_event,
3385 .delete_ctrl = nvme_fc_delete_ctrl,
3386 .get_address = nvmf_get_address,
3387 .get_virt_boundary = nvmf_get_virt_boundary,
3388 };
3389
3390 static void
nvme_fc_connect_ctrl_work(struct work_struct * work)3391 nvme_fc_connect_ctrl_work(struct work_struct *work)
3392 {
3393 int ret;
3394
3395 struct nvme_fc_ctrl *ctrl =
3396 container_of(to_delayed_work(work),
3397 struct nvme_fc_ctrl, connect_work);
3398
3399 ret = nvme_fc_create_association(ctrl);
3400 if (ret)
3401 nvme_fc_reconnect_or_delete(ctrl, ret);
3402 else
3403 dev_info(ctrl->ctrl.device,
3404 "NVME-FC{%d}: controller connect complete\n",
3405 ctrl->cnum);
3406 }
3407
3408
3409 static const struct blk_mq_ops nvme_fc_admin_mq_ops = {
3410 .queue_rq = nvme_fc_queue_rq,
3411 .complete = nvme_fc_complete_rq,
3412 .init_request = nvme_fc_init_request,
3413 .exit_request = nvme_fc_exit_request,
3414 .init_hctx = nvme_fc_init_admin_hctx,
3415 .timeout = nvme_fc_timeout,
3416 };
3417
3418
3419 /*
3420 * Fails a controller request if it matches an existing controller
3421 * (association) with the same tuple:
3422 * <Host NQN, Host ID, local FC port, remote FC port, SUBSYS NQN>
3423 *
3424 * The ports don't need to be compared as they are intrinsically
3425 * already matched by the port pointers supplied.
3426 */
3427 static bool
nvme_fc_existing_controller(struct nvme_fc_rport * rport,struct nvmf_ctrl_options * opts)3428 nvme_fc_existing_controller(struct nvme_fc_rport *rport,
3429 struct nvmf_ctrl_options *opts)
3430 {
3431 struct nvme_fc_ctrl *ctrl;
3432 unsigned long flags;
3433 bool found = false;
3434
3435 spin_lock_irqsave(&rport->lock, flags);
3436 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
3437 found = nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts);
3438 if (found)
3439 break;
3440 }
3441 spin_unlock_irqrestore(&rport->lock, flags);
3442
3443 return found;
3444 }
3445
3446 static struct nvme_fc_ctrl *
nvme_fc_alloc_ctrl(struct device * dev,struct nvmf_ctrl_options * opts,struct nvme_fc_lport * lport,struct nvme_fc_rport * rport)3447 nvme_fc_alloc_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
3448 struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
3449 {
3450 struct nvme_fc_ctrl *ctrl;
3451 int ret, idx, ctrl_loss_tmo;
3452
3453 if (!(rport->remoteport.port_role &
3454 (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) {
3455 ret = -EBADR;
3456 goto out_fail;
3457 }
3458
3459 if (!opts->duplicate_connect &&
3460 nvme_fc_existing_controller(rport, opts)) {
3461 ret = -EALREADY;
3462 goto out_fail;
3463 }
3464
3465 ctrl = kzalloc_obj(*ctrl);
3466 if (!ctrl) {
3467 ret = -ENOMEM;
3468 goto out_fail;
3469 }
3470
3471 idx = ida_alloc(&nvme_fc_ctrl_cnt, GFP_KERNEL);
3472 if (idx < 0) {
3473 ret = -ENOSPC;
3474 goto out_free_ctrl;
3475 }
3476
3477 /*
3478 * if ctrl_loss_tmo is being enforced and the default reconnect delay
3479 * is being used, change to a shorter reconnect delay for FC.
3480 */
3481 if (opts->max_reconnects != -1 &&
3482 opts->reconnect_delay == NVMF_DEF_RECONNECT_DELAY &&
3483 opts->reconnect_delay > NVME_FC_DEFAULT_RECONNECT_TMO) {
3484 ctrl_loss_tmo = opts->max_reconnects * opts->reconnect_delay;
3485 opts->reconnect_delay = NVME_FC_DEFAULT_RECONNECT_TMO;
3486 opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
3487 opts->reconnect_delay);
3488 }
3489
3490 ctrl->ctrl.opts = opts;
3491 ctrl->ctrl.nr_reconnects = 0;
3492 atomic_long_set(&ctrl->ctrl.acc_reconnects, 0);
3493 INIT_LIST_HEAD(&ctrl->ctrl_list);
3494 ctrl->lport = lport;
3495 ctrl->rport = rport;
3496 ctrl->dev = lport->dev;
3497 ctrl->cnum = idx;
3498 ctrl->ioq_live = false;
3499 init_waitqueue_head(&ctrl->ioabort_wait);
3500
3501 get_device(ctrl->dev);
3502 kref_init(&ctrl->ref);
3503
3504 INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work);
3505 INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
3506 INIT_WORK(&ctrl->ioerr_work, nvme_fc_ctrl_ioerr_work);
3507 spin_lock_init(&ctrl->lock);
3508
3509 /* io queue count */
3510 ctrl->ctrl.queue_count = min_t(unsigned int,
3511 opts->nr_io_queues,
3512 lport->ops->max_hw_queues);
3513 ctrl->ctrl.queue_count++; /* +1 for admin queue */
3514
3515 ctrl->ctrl.sqsize = opts->queue_size - 1;
3516 ctrl->ctrl.kato = opts->kato;
3517 ctrl->ctrl.cntlid = 0xffff;
3518
3519 ret = -ENOMEM;
3520 ctrl->queues = kzalloc_objs(struct nvme_fc_queue,
3521 ctrl->ctrl.queue_count);
3522 if (!ctrl->queues)
3523 goto out_free_ida;
3524
3525 nvme_fc_init_queue(ctrl, 0);
3526
3527 /*
3528 * Would have been nice to init io queues tag set as well.
3529 * However, we require interaction from the controller
3530 * for max io queue count before we can do so.
3531 * Defer this to the connect path.
3532 */
3533
3534 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
3535 if (ret)
3536 goto out_free_queues;
3537 if (lport->dev)
3538 ctrl->ctrl.numa_node = dev_to_node(lport->dev);
3539
3540 return ctrl;
3541
3542 out_free_queues:
3543 kfree(ctrl->queues);
3544 out_free_ida:
3545 put_device(ctrl->dev);
3546 ida_free(&nvme_fc_ctrl_cnt, ctrl->cnum);
3547 out_free_ctrl:
3548 kfree(ctrl);
3549 out_fail:
3550 /* exit via here doesn't follow ctlr ref points */
3551 return ERR_PTR(ret);
3552 }
3553
3554 static struct nvme_ctrl *
nvme_fc_init_ctrl(struct device * dev,struct nvmf_ctrl_options * opts,struct nvme_fc_lport * lport,struct nvme_fc_rport * rport)3555 nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
3556 struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
3557 {
3558 struct nvme_fc_ctrl *ctrl;
3559 unsigned long flags;
3560 int ret;
3561
3562 ctrl = nvme_fc_alloc_ctrl(dev, opts, lport, rport);
3563 if (IS_ERR(ctrl))
3564 return ERR_CAST(ctrl);
3565
3566 ret = nvme_add_ctrl(&ctrl->ctrl);
3567 if (ret)
3568 goto out_put_ctrl;
3569
3570 ret = nvme_alloc_admin_tag_set(&ctrl->ctrl, &ctrl->admin_tag_set,
3571 &nvme_fc_admin_mq_ops,
3572 struct_size_t(struct nvme_fcp_op_w_sgl, priv,
3573 ctrl->lport->ops->fcprqst_priv_sz));
3574 if (ret)
3575 goto fail_ctrl;
3576
3577 spin_lock_irqsave(&rport->lock, flags);
3578 list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
3579 spin_unlock_irqrestore(&rport->lock, flags);
3580
3581 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
3582 dev_err(ctrl->ctrl.device,
3583 "NVME-FC{%d}: failed to init ctrl state\n", ctrl->cnum);
3584 goto fail_unlist;
3585 }
3586
3587 if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
3588 dev_err(ctrl->ctrl.device,
3589 "NVME-FC{%d}: failed to schedule initial connect\n",
3590 ctrl->cnum);
3591 goto fail_unlist;
3592 }
3593
3594 flush_delayed_work(&ctrl->connect_work);
3595
3596 dev_info(ctrl->ctrl.device,
3597 "NVME-FC{%d}: new ctrl: NQN \"%s\", hostnqn: %s\n",
3598 ctrl->cnum, nvmf_ctrl_subsysnqn(&ctrl->ctrl), opts->host->nqn);
3599
3600 return &ctrl->ctrl;
3601
3602 fail_unlist:
3603 /*
3604 * Leaving the list hands the options back to nvmf_create_ctrl();
3605 * see nvme_fc_ctrl_free(). Re-init so that list_empty() there
3606 * reports the controller as unlisted.
3607 */
3608 spin_lock_irqsave(&rport->lock, flags);
3609 list_del_init(&ctrl->ctrl_list);
3610 spin_unlock_irqrestore(&rport->lock, flags);
3611
3612 fail_ctrl:
3613 nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING);
3614 cancel_work_sync(&ctrl->ioerr_work);
3615 cancel_work_sync(&ctrl->ctrl.reset_work);
3616 cancel_delayed_work_sync(&ctrl->connect_work);
3617
3618 if (ctrl->ctrl.admin_tagset)
3619 nvme_remove_admin_tag_set(&ctrl->ctrl);
3620 /* initiate nvme ctrl ref counting teardown */
3621 nvme_uninit_ctrl(&ctrl->ctrl);
3622
3623 out_put_ctrl:
3624 /* Remove core ctrl ref. */
3625 nvme_put_ctrl(&ctrl->ctrl);
3626
3627 /* as we're past the point where we transition to the ref
3628 * counting teardown path, if we return a bad pointer here,
3629 * the calling routine, thinking it's prior to the
3630 * transition, will do an rport put. Since the teardown
3631 * path also does a rport put, we do an extra get here to
3632 * so proper order/teardown happens.
3633 */
3634 nvme_fc_rport_get(rport);
3635
3636 return ERR_PTR(-EIO);
3637 }
3638
3639 struct nvmet_fc_traddr {
3640 u64 nn;
3641 u64 pn;
3642 };
3643
3644 static int
__nvme_fc_parse_u64(substring_t * sstr,u64 * val)3645 __nvme_fc_parse_u64(substring_t *sstr, u64 *val)
3646 {
3647 u64 token64;
3648
3649 if (match_u64(sstr, &token64))
3650 return -EINVAL;
3651 *val = token64;
3652
3653 return 0;
3654 }
3655
3656 /*
3657 * This routine validates and extracts the WWN's from the TRADDR string.
3658 * As kernel parsers need the 0x to determine number base, universally
3659 * build string to parse with 0x prefix before parsing name strings.
3660 */
3661 static int
nvme_fc_parse_traddr(struct nvmet_fc_traddr * traddr,char * buf,size_t blen)3662 nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen)
3663 {
3664 char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1];
3665 substring_t wwn = { name, &name[sizeof(name)-1] };
3666 int nnoffset, pnoffset;
3667
3668 /* validate if string is one of the 2 allowed formats */
3669 if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH &&
3670 !strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) &&
3671 !strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET],
3672 "pn-0x", NVME_FC_TRADDR_OXNNLEN)) {
3673 nnoffset = NVME_FC_TRADDR_OXNNLEN;
3674 pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET +
3675 NVME_FC_TRADDR_OXNNLEN;
3676 } else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH &&
3677 !strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) &&
3678 !strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET],
3679 "pn-", NVME_FC_TRADDR_NNLEN))) {
3680 nnoffset = NVME_FC_TRADDR_NNLEN;
3681 pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN;
3682 } else
3683 goto out_einval;
3684
3685 name[0] = '0';
3686 name[1] = 'x';
3687 name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0;
3688
3689 memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3690 if (__nvme_fc_parse_u64(&wwn, &traddr->nn))
3691 goto out_einval;
3692
3693 memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3694 if (__nvme_fc_parse_u64(&wwn, &traddr->pn))
3695 goto out_einval;
3696
3697 return 0;
3698
3699 out_einval:
3700 pr_warn("%s: bad traddr string\n", __func__);
3701 return -EINVAL;
3702 }
3703
3704 static struct nvme_ctrl *
nvme_fc_create_ctrl(struct device * dev,struct nvmf_ctrl_options * opts)3705 nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
3706 {
3707 struct nvme_fc_lport *lport;
3708 struct nvme_fc_rport *rport;
3709 struct nvme_ctrl *ctrl;
3710 struct nvmet_fc_traddr laddr = { 0L, 0L };
3711 struct nvmet_fc_traddr raddr = { 0L, 0L };
3712 unsigned long flags;
3713 int ret;
3714
3715 ret = nvme_fc_parse_traddr(&raddr, opts->traddr, NVMF_TRADDR_SIZE);
3716 if (ret || !raddr.nn || !raddr.pn)
3717 return ERR_PTR(-EINVAL);
3718
3719 ret = nvme_fc_parse_traddr(&laddr, opts->host_traddr, NVMF_TRADDR_SIZE);
3720 if (ret || !laddr.nn || !laddr.pn)
3721 return ERR_PTR(-EINVAL);
3722
3723 /* find the host and remote ports to connect together */
3724 spin_lock_irqsave(&nvme_fc_lock, flags);
3725 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3726 if (lport->localport.node_name != laddr.nn ||
3727 lport->localport.port_name != laddr.pn ||
3728 lport->localport.port_state != FC_OBJSTATE_ONLINE)
3729 continue;
3730
3731 list_for_each_entry(rport, &lport->endp_list, endp_list) {
3732 if (rport->remoteport.node_name != raddr.nn ||
3733 rport->remoteport.port_name != raddr.pn ||
3734 rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
3735 continue;
3736
3737 /* if fail to get reference fall through. Will error */
3738 if (!nvme_fc_rport_get(rport))
3739 break;
3740
3741 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3742
3743 ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport);
3744 if (IS_ERR(ctrl))
3745 nvme_fc_rport_put(rport);
3746 return ctrl;
3747 }
3748 }
3749 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3750
3751 pr_warn("%s: %s - %s combination not found\n",
3752 __func__, opts->traddr, opts->host_traddr);
3753 return ERR_PTR(-ENOENT);
3754 }
3755
3756
3757 static struct nvmf_transport_ops nvme_fc_transport = {
3758 .name = "fc",
3759 .module = THIS_MODULE,
3760 .required_opts = NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
3761 .allowed_opts = NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO,
3762 .create_ctrl = nvme_fc_create_ctrl,
3763 };
3764
3765 /* Arbitrary successive failures max. With lots of subsystems could be high */
3766 #define DISCOVERY_MAX_FAIL 20
3767
nvme_fc_nvme_discovery_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3768 static ssize_t nvme_fc_nvme_discovery_store(struct device *dev,
3769 struct device_attribute *attr, const char *buf, size_t count)
3770 {
3771 unsigned long flags;
3772 LIST_HEAD(local_disc_list);
3773 struct nvme_fc_lport *lport;
3774 struct nvme_fc_rport *rport;
3775 int failcnt = 0;
3776
3777 spin_lock_irqsave(&nvme_fc_lock, flags);
3778 restart:
3779 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3780 list_for_each_entry(rport, &lport->endp_list, endp_list) {
3781 if (!nvme_fc_lport_get(lport))
3782 continue;
3783 if (!nvme_fc_rport_get(rport)) {
3784 /*
3785 * This is a temporary condition. Upon restart
3786 * this rport will be gone from the list.
3787 *
3788 * Revert the lport put and retry. Anything
3789 * added to the list already will be skipped (as
3790 * they are no longer list_empty). Loops should
3791 * resume at rports that were not yet seen.
3792 */
3793 nvme_fc_lport_put(lport);
3794
3795 if (failcnt++ < DISCOVERY_MAX_FAIL)
3796 goto restart;
3797
3798 pr_err("nvme_discovery: too many reference "
3799 "failures\n");
3800 goto process_local_list;
3801 }
3802 if (list_empty(&rport->disc_list))
3803 list_add_tail(&rport->disc_list,
3804 &local_disc_list);
3805 }
3806 }
3807
3808 process_local_list:
3809 while (!list_empty(&local_disc_list)) {
3810 rport = list_first_entry(&local_disc_list,
3811 struct nvme_fc_rport, disc_list);
3812 list_del_init(&rport->disc_list);
3813 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3814
3815 lport = rport->lport;
3816 /* signal discovery. Won't hurt if it repeats */
3817 nvme_fc_signal_discovery_scan(lport, rport);
3818 nvme_fc_rport_put(rport);
3819 nvme_fc_lport_put(lport);
3820
3821 spin_lock_irqsave(&nvme_fc_lock, flags);
3822 }
3823 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3824
3825 return count;
3826 }
3827
3828 static DEVICE_ATTR(nvme_discovery, 0200, NULL, nvme_fc_nvme_discovery_store);
3829
3830 #ifdef CONFIG_BLK_CGROUP_FC_APPID
3831 /* Parse the cgroup id from a buf and return the length of cgrpid */
fc_parse_cgrpid(const char * buf,u64 * id)3832 static int fc_parse_cgrpid(const char *buf, u64 *id)
3833 {
3834 char cgrp_id[16+1];
3835 int cgrpid_len, j;
3836
3837 memset(cgrp_id, 0x0, sizeof(cgrp_id));
3838 for (cgrpid_len = 0, j = 0; cgrpid_len < 17; cgrpid_len++) {
3839 if (buf[cgrpid_len] != ':')
3840 cgrp_id[cgrpid_len] = buf[cgrpid_len];
3841 else {
3842 j = 1;
3843 break;
3844 }
3845 }
3846 if (!j)
3847 return -EINVAL;
3848 if (kstrtou64(cgrp_id, 16, id) < 0)
3849 return -EINVAL;
3850 return cgrpid_len;
3851 }
3852
3853 /*
3854 * Parse and update the appid in the blkcg associated with the cgroupid.
3855 */
fc_appid_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3856 static ssize_t fc_appid_store(struct device *dev,
3857 struct device_attribute *attr, const char *buf, size_t count)
3858 {
3859 size_t orig_count = count;
3860 u64 cgrp_id;
3861 int appid_len = 0;
3862 int cgrpid_len = 0;
3863 char app_id[FC_APPID_LEN];
3864 int ret = 0;
3865
3866 if (buf[count-1] == '\n')
3867 count--;
3868
3869 if ((count > (16+1+FC_APPID_LEN)) || (!strchr(buf, ':')))
3870 return -EINVAL;
3871
3872 cgrpid_len = fc_parse_cgrpid(buf, &cgrp_id);
3873 if (cgrpid_len < 0)
3874 return -EINVAL;
3875 appid_len = count - cgrpid_len - 1;
3876 if (appid_len > FC_APPID_LEN)
3877 return -EINVAL;
3878
3879 memset(app_id, 0x0, sizeof(app_id));
3880 memcpy(app_id, &buf[cgrpid_len+1], appid_len);
3881 ret = blkcg_set_fc_appid(app_id, cgrp_id, sizeof(app_id));
3882 if (ret < 0)
3883 return ret;
3884 return orig_count;
3885 }
3886 static DEVICE_ATTR(appid_store, 0200, NULL, fc_appid_store);
3887 #endif /* CONFIG_BLK_CGROUP_FC_APPID */
3888
3889 static struct attribute *nvme_fc_attrs[] = {
3890 &dev_attr_nvme_discovery.attr,
3891 #ifdef CONFIG_BLK_CGROUP_FC_APPID
3892 &dev_attr_appid_store.attr,
3893 #endif
3894 NULL
3895 };
3896
3897 static const struct attribute_group nvme_fc_attr_group = {
3898 .attrs = nvme_fc_attrs,
3899 };
3900
3901 static const struct attribute_group *nvme_fc_attr_groups[] = {
3902 &nvme_fc_attr_group,
3903 NULL
3904 };
3905
3906 static struct class fc_class = {
3907 .name = "fc",
3908 .dev_groups = nvme_fc_attr_groups,
3909 };
3910
nvme_fc_init_module(void)3911 static int __init nvme_fc_init_module(void)
3912 {
3913 int ret;
3914
3915 /*
3916 * NOTE:
3917 * It is expected that in the future the kernel will combine
3918 * the FC-isms that are currently under scsi and now being
3919 * added to by NVME into a new standalone FC class. The SCSI
3920 * and NVME protocols and their devices would be under this
3921 * new FC class.
3922 *
3923 * As we need something to post FC-specific udev events to,
3924 * specifically for nvme probe events, start by creating the
3925 * new device class. When the new standalone FC class is
3926 * put in place, this code will move to a more generic
3927 * location for the class.
3928 */
3929 ret = class_register(&fc_class);
3930 if (ret) {
3931 pr_err("couldn't register class fc\n");
3932 return ret;
3933 }
3934
3935 /*
3936 * Create a device for the FC-centric udev events
3937 */
3938 fc_udev_device = device_create(&fc_class, NULL, MKDEV(0, 0), NULL,
3939 "fc_udev_device");
3940 if (IS_ERR(fc_udev_device)) {
3941 pr_err("couldn't create fc_udev device!\n");
3942 ret = PTR_ERR(fc_udev_device);
3943 goto out_destroy_class;
3944 }
3945
3946 ret = nvmf_register_transport(&nvme_fc_transport);
3947 if (ret)
3948 goto out_destroy_device;
3949
3950 return 0;
3951
3952 out_destroy_device:
3953 device_destroy(&fc_class, MKDEV(0, 0));
3954 out_destroy_class:
3955 class_unregister(&fc_class);
3956
3957 return ret;
3958 }
3959
3960 static void
nvme_fc_delete_controllers(struct nvme_fc_rport * rport)3961 nvme_fc_delete_controllers(struct nvme_fc_rport *rport)
3962 {
3963 struct nvme_fc_ctrl *ctrl;
3964
3965 spin_lock(&rport->lock);
3966 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
3967 dev_warn(ctrl->ctrl.device,
3968 "NVME-FC{%d}: transport unloading: deleting ctrl\n",
3969 ctrl->cnum);
3970 nvme_delete_ctrl(&ctrl->ctrl);
3971 }
3972 spin_unlock(&rport->lock);
3973 }
3974
nvme_fc_exit_module(void)3975 static void __exit nvme_fc_exit_module(void)
3976 {
3977 struct nvme_fc_lport *lport;
3978 struct nvme_fc_rport *rport;
3979 unsigned long flags;
3980
3981 spin_lock_irqsave(&nvme_fc_lock, flags);
3982 list_for_each_entry(lport, &nvme_fc_lport_list, port_list)
3983 list_for_each_entry(rport, &lport->endp_list, endp_list)
3984 nvme_fc_delete_controllers(rport);
3985 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3986 flush_workqueue(nvme_delete_wq);
3987
3988 nvmf_unregister_transport(&nvme_fc_transport);
3989
3990 device_destroy(&fc_class, MKDEV(0, 0));
3991 class_unregister(&fc_class);
3992 }
3993
3994 module_init(nvme_fc_init_module);
3995 module_exit(nvme_fc_exit_module);
3996
3997 MODULE_DESCRIPTION("NVMe host FC transport driver");
3998 MODULE_LICENSE("GPL v2");
3999 MODULE_ALIAS("nvme-fc");
4000