xref: /linux/drivers/nvme/host/nvme.h (revision 5819086dff9a5662425f45997fc95ca0e62379be)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2011-2014, Intel Corporation.
4  */
5 
6 #ifndef _NVME_H
7 #define _NVME_H
8 
9 #include <linux/nvme.h>
10 #include <linux/cdev.h>
11 #include <linux/pci.h>
12 #include <linux/kref.h>
13 #include <linux/blk-mq.h>
14 #include <linux/sed-opal.h>
15 #include <linux/fault-inject.h>
16 #include <linux/rcupdate.h>
17 #include <linux/wait.h>
18 #include <linux/t10-pi.h>
19 #include <linux/ratelimit_types.h>
20 
21 #include <trace/events/block.h>
22 
23 extern const struct pr_ops nvme_pr_ops;
24 
25 /* Seconds to jiffies, capped for the u32 timeouts and timer delays */
26 static inline unsigned int nvme_timeout_to_jiffies(unsigned int timeout)
27 {
28 	unsigned long max_secs;
29 
30 	max_secs = min_t(unsigned long, UINT_MAX, MAX_JIFFY_OFFSET) / HZ;
31 	return min_t(unsigned long, timeout, max_secs) * HZ;
32 }
33 
34 extern unsigned int nvme_io_timeout;
35 #define NVME_IO_TIMEOUT	nvme_timeout_to_jiffies(nvme_io_timeout)
36 
37 extern unsigned int admin_timeout;
38 #define NVME_ADMIN_TIMEOUT	nvme_timeout_to_jiffies(admin_timeout)
39 
40 #define NVME_DEFAULT_KATO	5
41 
42 #ifdef CONFIG_ARCH_NO_SG_CHAIN
43 #define  NVME_INLINE_SG_CNT  0
44 #define  NVME_INLINE_METADATA_SG_CNT  0
45 #else
46 #define  NVME_INLINE_SG_CNT  2
47 #define  NVME_INLINE_METADATA_SG_CNT  1
48 #endif
49 
50 /*
51  * Default to a 4K page size, with the intention to update this
52  * path in the future to accommodate architectures with differing
53  * kernel and IO page sizes.
54  */
55 #define NVME_CTRL_PAGE_SHIFT	12
56 #define NVME_CTRL_PAGE_SIZE	(1 << NVME_CTRL_PAGE_SHIFT)
57 
58 extern struct workqueue_struct *nvme_wq;
59 extern struct workqueue_struct *nvme_reset_wq;
60 extern struct workqueue_struct *nvme_delete_wq;
61 extern struct mutex nvme_subsystems_lock;
62 
63 /*
64  * List of workarounds for devices that required behavior not specified in
65  * the standard.
66  */
67 enum nvme_quirks {
68 	/*
69 	 * Prefers I/O aligned to a stripe size specified in a vendor
70 	 * specific Identify field.
71 	 */
72 	NVME_QUIRK_STRIPE_SIZE			= (1 << 0),
73 
74 	/*
75 	 * The controller doesn't handle Identify value others than 0 or 1
76 	 * correctly.
77 	 */
78 	NVME_QUIRK_IDENTIFY_CNS			= (1 << 1),
79 
80 	/*
81 	 * The controller deterministically returns 0's on reads to
82 	 * logical blocks that deallocate was called on.
83 	 */
84 	NVME_QUIRK_DEALLOCATE_ZEROES		= (1 << 2),
85 
86 	/*
87 	 * The controller needs a delay before starts checking the device
88 	 * readiness, which is done by reading the NVME_CSTS_RDY bit.
89 	 */
90 	NVME_QUIRK_DELAY_BEFORE_CHK_RDY		= (1 << 3),
91 
92 	/*
93 	 * APST should not be used.
94 	 */
95 	NVME_QUIRK_NO_APST			= (1 << 4),
96 
97 	/*
98 	 * The deepest sleep state should not be used.
99 	 */
100 	NVME_QUIRK_NO_DEEPEST_PS		= (1 << 5),
101 
102 	/*
103 	 *  Problems seen with concurrent commands
104 	 */
105 	NVME_QUIRK_QDEPTH_ONE			= (1 << 6),
106 
107 	/*
108 	 * Set MEDIUM priority on SQ creation
109 	 */
110 	NVME_QUIRK_MEDIUM_PRIO_SQ		= (1 << 7),
111 
112 	/*
113 	 * Ignore device provided subnqn.
114 	 */
115 	NVME_QUIRK_IGNORE_DEV_SUBNQN		= (1 << 8),
116 
117 	/*
118 	 * Broken Write Zeroes.
119 	 */
120 	NVME_QUIRK_DISABLE_WRITE_ZEROES		= (1 << 9),
121 
122 	/*
123 	 * Force simple suspend/resume path.
124 	 */
125 	NVME_QUIRK_SIMPLE_SUSPEND		= (1 << 10),
126 
127 	/*
128 	 * Use only one interrupt vector for all queues
129 	 */
130 	NVME_QUIRK_SINGLE_VECTOR		= (1 << 11),
131 
132 	/*
133 	 * Use non-standard 128 bytes SQEs.
134 	 */
135 	NVME_QUIRK_128_BYTES_SQES		= (1 << 12),
136 
137 	/*
138 	 * Prevent tag overlap between queues
139 	 */
140 	NVME_QUIRK_SHARED_TAGS                  = (1 << 13),
141 
142 	/*
143 	 * Don't change the value of the temperature threshold feature
144 	 */
145 	NVME_QUIRK_NO_TEMP_THRESH_CHANGE	= (1 << 14),
146 
147 	/*
148 	 * The controller doesn't handle the Identify Namespace
149 	 * Identification Descriptor list subcommand despite claiming
150 	 * NVMe 1.3 compliance.
151 	 */
152 	NVME_QUIRK_NO_NS_DESC_LIST		= (1 << 15),
153 
154 	/*
155 	 * The controller does not properly handle DMA addresses over
156 	 * 48 bits.
157 	 */
158 	NVME_QUIRK_DMA_ADDRESS_BITS_48		= (1 << 16),
159 
160 	/*
161 	 * The controller requires the command_id value be limited, so skip
162 	 * encoding the generation sequence number.
163 	 */
164 	NVME_QUIRK_SKIP_CID_GEN			= (1 << 17),
165 
166 	/*
167 	 * Reports garbage in the namespace identifiers (eui64, nguid, uuid).
168 	 */
169 	NVME_QUIRK_BOGUS_NID			= (1 << 18),
170 
171 	/*
172 	 * No temperature thresholds for channels other than 0 (Composite).
173 	 */
174 	NVME_QUIRK_NO_SECONDARY_TEMP_THRESH	= (1 << 19),
175 
176 	/*
177 	 * Disables simple suspend/resume path.
178 	 */
179 	NVME_QUIRK_FORCE_NO_SIMPLE_SUSPEND	= (1 << 20),
180 
181 	/*
182 	 * MSI (but not MSI-X) interrupts are broken and never fire.
183 	 */
184 	NVME_QUIRK_BROKEN_MSI			= (1 << 21),
185 
186 	/*
187 	 * Align dma pool segment size to 512 bytes
188 	 */
189 	NVME_QUIRK_DMAPOOL_ALIGN_512		= (1 << 22),
190 
191 	/*
192 	 * Admin queue DMA buffers must be page aligned
193 	 */
194 	NVME_QUIRK_ADMIN_PAGE_ALIGN		= (1 << 23),
195 
196 	/*
197 	 * The device is extremely slow on FUA writes but fast on Flush;
198 	 * do not advertise FUA so the block layer emulates it.
199 	 */
200 	NVME_QUIRK_NO_FUA			= (1 << 24),
201 
202 	/*
203 	 * The controller fetches 512 bytes for the 256 byte APST table
204 	 */
205 	NVME_QUIRK_APST_BUF_512			= (1 << 25),
206 };
207 
208 static inline char *nvme_quirk_name(enum nvme_quirks q)
209 {
210 	switch (q) {
211 	case NVME_QUIRK_STRIPE_SIZE:
212 		return "stripe_size";
213 	case NVME_QUIRK_IDENTIFY_CNS:
214 		return "identify_cns";
215 	case NVME_QUIRK_DEALLOCATE_ZEROES:
216 		return "deallocate_zeroes";
217 	case NVME_QUIRK_DELAY_BEFORE_CHK_RDY:
218 		return "delay_before_chk_rdy";
219 	case NVME_QUIRK_NO_APST:
220 		return "no_apst";
221 	case NVME_QUIRK_NO_DEEPEST_PS:
222 		return "no_deepest_ps";
223 	case NVME_QUIRK_QDEPTH_ONE:
224 		return "qdepth_one";
225 	case NVME_QUIRK_MEDIUM_PRIO_SQ:
226 		return "medium_prio_sq";
227 	case NVME_QUIRK_IGNORE_DEV_SUBNQN:
228 		return "ignore_dev_subnqn";
229 	case NVME_QUIRK_DISABLE_WRITE_ZEROES:
230 		return "disable_write_zeroes";
231 	case NVME_QUIRK_SIMPLE_SUSPEND:
232 		return "simple_suspend";
233 	case NVME_QUIRK_SINGLE_VECTOR:
234 		return "single_vector";
235 	case NVME_QUIRK_128_BYTES_SQES:
236 		return "128_bytes_sqes";
237 	case NVME_QUIRK_SHARED_TAGS:
238 		return "shared_tags";
239 	case NVME_QUIRK_NO_TEMP_THRESH_CHANGE:
240 		return "no_temp_thresh_change";
241 	case NVME_QUIRK_NO_NS_DESC_LIST:
242 		return "no_ns_desc_list";
243 	case NVME_QUIRK_DMA_ADDRESS_BITS_48:
244 		return "dma_address_bits_48";
245 	case NVME_QUIRK_SKIP_CID_GEN:
246 		return "skip_cid_gen";
247 	case NVME_QUIRK_BOGUS_NID:
248 		return "bogus_nid";
249 	case NVME_QUIRK_NO_SECONDARY_TEMP_THRESH:
250 		return "no_secondary_temp_thresh";
251 	case NVME_QUIRK_FORCE_NO_SIMPLE_SUSPEND:
252 		return "force_no_simple_suspend";
253 	case NVME_QUIRK_BROKEN_MSI:
254 		return "broken_msi";
255 	case NVME_QUIRK_DMAPOOL_ALIGN_512:
256 		return "dmapool_align_512";
257 	case NVME_QUIRK_ADMIN_PAGE_ALIGN:
258 		return "admin_page_align";
259 	case NVME_QUIRK_NO_FUA:
260 		return "no_fua";
261 	case NVME_QUIRK_APST_BUF_512:
262 		return "apst_buf_512";
263 	}
264 
265 	return "unknown";
266 }
267 
268 /*
269  * Common request structure for NVMe passthrough.  All drivers must have
270  * this structure as the first member of their request-private data.
271  */
272 struct nvme_request {
273 	struct nvme_command	*cmd;
274 	union nvme_result	result;
275 	u8			genctr;
276 	u8			retries;
277 	u8			flags;
278 	u16			status;
279 #ifdef CONFIG_NVME_MULTIPATH
280 	unsigned long		start_time;
281 #endif
282 	struct nvme_ctrl	*ctrl;
283 };
284 
285 /*
286  * Mark a bio as coming in through the mpath node.
287  */
288 #define REQ_NVME_MPATH		REQ_DRV
289 
290 enum {
291 	NVME_REQ_CANCELLED		= (1 << 0),
292 	NVME_REQ_USERCMD		= (1 << 1),
293 	NVME_MPATH_IO_STATS		= (1 << 2),
294 	NVME_MPATH_CNT_ACTIVE		= (1 << 3),
295 };
296 
297 static inline struct nvme_request *nvme_req(struct request *req)
298 {
299 	return blk_mq_rq_to_pdu(req);
300 }
301 
302 static inline u16 nvme_req_qid(struct request *req)
303 {
304 	if (!req->q->queuedata)
305 		return 0;
306 
307 	return req->mq_hctx->queue_num + 1;
308 }
309 
310 /* The below value is the specific amount of delay needed before checking
311  * readiness in case of the PCI_DEVICE(0x1c58, 0x0003), which needs the
312  * NVME_QUIRK_DELAY_BEFORE_CHK_RDY quirk enabled. The value (in ms) was
313  * found empirically.
314  */
315 #define NVME_QUIRK_DELAY_AMOUNT		2300
316 
317 /*
318  * enum nvme_ctrl_state: Controller state
319  *
320  * @NVME_CTRL_NEW:		New controller just allocated, initial state
321  * @NVME_CTRL_LIVE:		Controller is connected and I/O capable
322  * @NVME_CTRL_RESETTING:	Controller is resetting (or scheduled reset)
323  * @NVME_CTRL_CONNECTING:	Controller is disconnected, now connecting the
324  *				transport
325  * @NVME_CTRL_DELETING:		Controller is deleting (or scheduled deletion)
326  * @NVME_CTRL_DELETING_NOIO:	Controller is deleting and I/O is not
327  *				disabled/failed immediately. This state comes
328  * 				after all async event processing took place and
329  * 				before ns removal and the controller deletion
330  * 				progress
331  * @NVME_CTRL_DEAD:		Controller is non-present/unresponsive during
332  *				shutdown or removal. In this case we forcibly
333  *				kill all inflight I/O as they have no chance to
334  *				complete
335  */
336 enum nvme_ctrl_state {
337 	NVME_CTRL_NEW,
338 	NVME_CTRL_LIVE,
339 	NVME_CTRL_RESETTING,
340 	NVME_CTRL_CONNECTING,
341 	NVME_CTRL_DELETING,
342 	NVME_CTRL_DELETING_NOIO,
343 	NVME_CTRL_DEAD,
344 };
345 
346 struct nvme_fault_inject {
347 #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
348 	struct fault_attr attr;
349 	struct dentry *parent;
350 	u16 opcode;
351 	bool dont_retry;	/* DNR, do not retry */
352 	u16 status;		/* status code */
353 #endif
354 };
355 
356 enum nvme_ctrl_flags {
357 	NVME_CTRL_FAILFAST_EXPIRED	= 0,
358 	NVME_CTRL_ADMIN_Q_STOPPED	= 1,
359 	NVME_CTRL_STARTED_ONCE		= 2,
360 	NVME_CTRL_STOPPED		= 3,
361 	NVME_CTRL_SKIP_ID_CNS_CS	= 4,
362 	NVME_CTRL_DIRTY_CAPABILITY	= 5,
363 	NVME_CTRL_FROZEN		= 6,
364 };
365 
366 struct nvme_ctrl {
367 	bool comp_seen;
368 	bool identified;
369 	bool passthru_err_log_enabled;
370 	enum nvme_ctrl_state state;
371 	spinlock_t lock;
372 	struct mutex scan_lock;
373 	const struct nvme_ctrl_ops *ops;
374 	struct request_queue *admin_q;
375 	struct request_queue *connect_q;
376 	struct request_queue *fabrics_q;
377 	struct device *dev;
378 	int instance;
379 	int numa_node;
380 	struct blk_mq_tag_set *tagset;
381 	struct blk_mq_tag_set *admin_tagset;
382 	struct list_head namespaces;
383 	struct mutex namespaces_lock;
384 	struct srcu_struct srcu;
385 	struct device ctrl_device;
386 	struct device *device;	/* char device */
387 #ifdef CONFIG_NVME_HWMON
388 	struct device *hwmon_device;
389 #endif
390 	struct cdev cdev;
391 	struct work_struct reset_work;
392 	struct work_struct delete_work;
393 	wait_queue_head_t state_wq;
394 
395 	struct nvme_subsystem *subsys;
396 	struct list_head subsys_entry
397 		__guarded_by(&nvme_subsystems_lock);
398 
399 	struct opal_dev *opal_dev;
400 
401 	u16 cntlid;
402 
403 	u16 mtfa;
404 	u32 ctrl_config;
405 	u32 queue_count;
406 	u32 admin_timeout;
407 	u32 io_timeout;
408 
409 	u64 cap;
410 	u32 max_hw_sectors;
411 	u32 max_segments;
412 	u32 max_integrity_segments;
413 	u32 max_zeroes_sectors;
414 #ifdef CONFIG_BLK_DEV_ZONED
415 	u32 max_zone_append;
416 #endif
417 	u16 crdt[3];
418 	u16 oncs;
419 	u8 dmrl;
420 	u32 dmrsl;
421 	u16 oacs;
422 	u16 sqsize;
423 	u32 max_namespaces;
424 	atomic_t abort_limit;
425 	u8 vwc;
426 	u32 vs;
427 	u32 sgls;
428 	u16 kas;
429 	u8 npss;
430 	u8 apsta;
431 	u16 wctemp;
432 	u16 cctemp;
433 	u32 oaes;
434 	u32 aen_result;
435 	u32 ctratt;
436 	unsigned int shutdown_timeout;
437 	unsigned int kato;
438 	bool subsystem;
439 	unsigned long quirks;
440 	struct nvme_id_power_state psd[32];
441 	struct nvme_effects_log *effects;
442 	struct work_struct scan_work;
443 	struct work_struct async_event_work;
444 	struct delayed_work ka_work;
445 	struct delayed_work failfast_work;
446 	struct nvme_command ka_cmd;
447 	unsigned long ka_last_check_time;
448 	struct work_struct fw_act_work;
449 	unsigned long events;
450 	atomic_long_t errors;
451 	atomic_long_t nr_reset;
452 
453 #ifdef CONFIG_NVME_MULTIPATH
454 	/* asymmetric namespace access: */
455 	u8 anacap;
456 	u8 anatt;
457 	u32 anagrpmax;
458 	u32 nanagrpid;
459 	struct mutex ana_lock;
460 	struct nvme_ana_rsp_hdr *ana_log_buf;
461 	size_t ana_log_size;
462 	struct timer_list anatt_timer;
463 	struct work_struct ana_work;
464 	atomic_t nr_active;
465 #endif
466 
467 #ifdef CONFIG_NVME_HOST_AUTH
468 	struct work_struct dhchap_auth_work;
469 	struct mutex dhchap_auth_mutex;
470 	struct nvme_dhchap_queue_context *dhchap_ctxs;
471 	struct nvme_dhchap_key *host_key;
472 	struct nvme_dhchap_key *ctrl_key;
473 	u16 transaction;
474 #endif
475 	key_serial_t tls_pskid;
476 
477 	/* Power saving configuration */
478 	u64 ps_max_latency_us;
479 	bool apst_enabled;
480 
481 	/* PCIe only: */
482 	u16 hmmaxd;
483 	u32 hmpre;
484 	u32 hmmin;
485 	u32 hmminds;
486 
487 	/* Fabrics only */
488 	u32 ioccsz;
489 	u32 iorcsz;
490 	u16 icdoff;
491 	u16 maxcmd;
492 	int nr_reconnects;
493 	/* accumulate reconenct attempts, as nr_reconnects can reset to zero */
494 	atomic_long_t acc_reconnects;
495 	unsigned long flags;
496 	struct nvmf_ctrl_options *opts;
497 
498 	struct page *discard_page;
499 	unsigned long discard_page_busy;
500 
501 	struct nvme_fault_inject fault_inject;
502 
503 	enum nvme_ctrl_type cntrltype;
504 	enum nvme_dctype dctype;
505 
506 	u16			awupf; /* 0's based value. */
507 };
508 
509 static inline enum nvme_ctrl_state nvme_ctrl_state(struct nvme_ctrl *ctrl)
510 {
511 	return READ_ONCE(ctrl->state);
512 }
513 
514 enum nvme_iopolicy {
515 	NVME_IOPOLICY_NUMA,
516 	NVME_IOPOLICY_RR,
517 	NVME_IOPOLICY_QD,
518 };
519 
520 struct nvme_subsystem {
521 	int			instance;
522 	struct device		dev;
523 	/*
524 	 * Because we unregister the device on the last put we need
525 	 * a separate refcount.
526 	 */
527 	struct kref		ref;
528 	struct list_head	entry
529 		__guarded_by(&nvme_subsystems_lock);
530 	struct mutex		lock;
531 	struct list_head	ctrls
532 		__guarded_by(&nvme_subsystems_lock);
533 	struct list_head	nsheads
534 		__guarded_by(&lock);
535 	/* Command effects logs, indexed by CSI and protected by lock. */
536 	struct xarray		cels;
537 	char			subnqn[NVMF_NQN_SIZE];
538 	char			serial[20];
539 	char			model[40];
540 	char			firmware_rev[8];
541 	u8			cmic;
542 	enum nvme_subsys_type	subtype;
543 	u16			vendor_id;
544 	struct ida		ns_ida;
545 #ifdef CONFIG_NVME_MULTIPATH
546 	enum nvme_iopolicy	iopolicy;
547 #endif
548 };
549 
550 /*
551  * Container structure for uniqueue namespace identifiers.
552  */
553 struct nvme_ns_ids {
554 	u8	eui64[8];
555 	u8	nguid[16];
556 	uuid_t	uuid;
557 	u8	csi;
558 };
559 
560 /*
561  * Anchor structure for namespaces.  There is one for each namespace in a
562  * NVMe subsystem that any of our controllers can see, and the namespace
563  * structure for each controller is chained of it.  For private namespaces
564  * there is a 1:1 relation to our namespace structures, that is ->list
565  * only ever has a single entry for private namespaces.
566  */
567 struct nvme_ns_head {
568 	struct list_head	list;
569 	struct srcu_struct      srcu;
570 	struct nvme_subsystem	*subsys;
571 	struct nvme_ns_ids	ids;
572 	u8			lba_shift;
573 	u16			ms;
574 	u16			pi_size;
575 	u8			pi_type;
576 	u8			guard_type;
577 	struct list_head	entry;
578 	struct kref		ref;
579 	bool			shared;
580 	bool			rotational;
581 	bool			passthru_err_log_enabled;
582 	struct nvme_effects_log *effects;
583 	u64			nuse;
584 	unsigned		ns_id;
585 	int			instance;
586 #ifdef CONFIG_BLK_DEV_ZONED
587 	u64			zsze;
588 #endif
589 	unsigned long		features;
590 
591 	struct ratelimit_state	rs_nuse;
592 
593 	struct cdev		cdev;
594 	struct device		cdev_device;
595 
596 	struct gendisk		*disk;
597 
598 	u16			nr_plids;
599 	u16			*plids;
600 	u32			write_stream_granularity;
601 #ifdef CONFIG_NVME_MULTIPATH
602 	struct bio_list		requeue_list
603 		__guarded_by(&requeue_lock);
604 	spinlock_t		requeue_lock;
605 	struct work_struct	requeue_work;
606 	unsigned long		requeue_srcu_cookie;
607 	struct work_struct	partition_scan_work;
608 	struct mutex		lock;
609 	unsigned long		flags;
610 	struct delayed_work	remove_work;
611 	unsigned int		delayed_removal_secs
612 		__guarded_by(&subsys->lock);
613 	atomic_long_t		io_requeue_no_usable_path_count;
614 	atomic_long_t		io_fail_no_available_path_count;
615 #define NVME_NSHEAD_DISK_LIVE		0
616 #define NVME_NSHEAD_QUEUE_IF_NO_PATH	1
617 #define NVME_NSHEAD_CDEV_LIVE		2
618 #define NVME_NSHEAD_FAIL_ON_CTRL_LOSS	3
619 	struct nvme_ns __rcu_guarded	*current_path[];
620 #endif
621 };
622 
623 static inline bool nvme_ns_head_multipath(struct nvme_ns_head *head)
624 {
625 	return IS_ENABLED(CONFIG_NVME_MULTIPATH) && head->disk;
626 }
627 
628 enum nvme_ns_features {
629 	NVME_NS_EXT_LBAS = 1 << 0, /* support extended LBA format */
630 	NVME_NS_METADATA_SUPPORTED = 1 << 1, /* support getting generated md */
631 	NVME_NS_DEAC = 1 << 2,		/* DEAC bit in Write Zeroes supported */
632 };
633 
634 struct nvme_ns {
635 	struct list_head list;
636 
637 	struct nvme_ctrl *ctrl;
638 	struct request_queue *queue;
639 	struct gendisk *disk;
640 #ifdef CONFIG_NVME_MULTIPATH
641 	enum nvme_ana_state ana_state;
642 	u32 ana_grpid;
643 	atomic_long_t failover;
644 #endif
645 	atomic_long_t retries;
646 	atomic_long_t errors;
647 	struct list_head siblings;
648 	struct kref kref;
649 	struct nvme_ns_head *head;
650 
651 	unsigned long flags;
652 #define NVME_NS_REMOVING		0
653 #define NVME_NS_ANA_PENDING		2
654 #define NVME_NS_FORCE_RO		3
655 #define NVME_NS_READY			4
656 #define NVME_NS_SYSFS_ATTR_LINK	5
657 #define NVME_NS_CDEV_LIVE		6
658 
659 	struct cdev		cdev;
660 	struct device		cdev_device;
661 
662 	struct nvme_fault_inject fault_inject;
663 };
664 
665 /* NVMe ns supports metadata actions by the controller (generate/strip) */
666 static inline bool nvme_ns_has_pi(struct nvme_ns_head *head)
667 {
668 	return head->pi_type && head->ms == head->pi_size;
669 }
670 
671 static inline unsigned long nvme_get_virt_boundary(struct nvme_ctrl *ctrl,
672 						   bool is_admin)
673 {
674 	return NVME_CTRL_PAGE_SIZE - 1;
675 }
676 
677 struct nvme_ctrl_ops {
678 	const char *name;
679 	struct module *module;
680 	unsigned int flags;
681 #define NVME_F_FABRICS			(1 << 0)
682 #define NVME_F_METADATA_SUPPORTED	(1 << 1)
683 #define NVME_F_BLOCKING			(1 << 2)
684 
685 	const struct attribute_group **dev_attr_groups;
686 	int (*reg_read32)(struct nvme_ctrl *ctrl, u32 off, u32 *val);
687 	int (*reg_write32)(struct nvme_ctrl *ctrl, u32 off, u32 val);
688 	int (*reg_read64)(struct nvme_ctrl *ctrl, u32 off, u64 *val);
689 	void (*free_ctrl)(struct nvme_ctrl *ctrl);
690 	void (*submit_async_event)(struct nvme_ctrl *ctrl);
691 	int (*subsystem_reset)(struct nvme_ctrl *ctrl);
692 	void (*delete_ctrl)(struct nvme_ctrl *ctrl);
693 	void (*stop_ctrl)(struct nvme_ctrl *ctrl);
694 	int (*get_address)(struct nvme_ctrl *ctrl, char *buf, int size);
695 	void (*print_device_info)(struct nvme_ctrl *ctrl);
696 	bool (*supports_pci_p2pdma)(struct nvme_ctrl *ctrl);
697 	int (*init_dma_buf_io_ctx)(struct nvme_ctrl *ctrl,
698 				   struct dma_buf_io_ctx *ctx);
699 	unsigned long (*get_virt_boundary)(struct nvme_ctrl *ctrl, bool is_admin);
700 };
701 
702 /*
703  * nvme command_id is constructed as such:
704  * | xxxx | xxxxxxxxxxxx |
705  *   gen    request tag
706  */
707 #define nvme_genctr_mask(gen)			(gen & 0xf)
708 #define nvme_cid_install_genctr(gen)		(nvme_genctr_mask(gen) << 12)
709 #define nvme_genctr_from_cid(cid)		((cid & 0xf000) >> 12)
710 #define nvme_tag_from_cid(cid)			(cid & 0xfff)
711 
712 static inline u16 nvme_cid(struct request *rq)
713 {
714 	return nvme_cid_install_genctr(nvme_req(rq)->genctr) | rq->tag;
715 }
716 
717 static inline struct request *nvme_find_rq(struct blk_mq_tags *tags,
718 		u16 command_id)
719 {
720 	u8 genctr = nvme_genctr_from_cid(command_id);
721 	u16 tag = nvme_tag_from_cid(command_id);
722 	struct request *rq;
723 
724 	rq = blk_mq_tag_to_rq(tags, tag);
725 	if (unlikely(!rq)) {
726 		pr_err_ratelimited("could not locate request for tag %#x\n",
727 				   tag);
728 		return NULL;
729 	}
730 	if (unlikely(nvme_genctr_mask(nvme_req(rq)->genctr) != genctr)) {
731 		dev_err_ratelimited(nvme_req(rq)->ctrl->device,
732 			"request %#x genctr mismatch (got %#x expected %#x)\n",
733 			tag, genctr, nvme_genctr_mask(nvme_req(rq)->genctr));
734 		return NULL;
735 	}
736 	return rq;
737 }
738 
739 static inline struct request *nvme_cid_to_rq(struct blk_mq_tags *tags,
740                 u16 command_id)
741 {
742 	return blk_mq_tag_to_rq(tags, nvme_tag_from_cid(command_id));
743 }
744 
745 /*
746  * Return the length of the string without the space padding
747  */
748 static inline int nvme_strlen(char *s, int len)
749 {
750 	while (len > 0 && s[len - 1] == ' ')
751 		len--;
752 	return len;
753 }
754 
755 static inline void nvme_print_device_info(struct nvme_ctrl *ctrl)
756 {
757 	struct nvme_subsystem *subsys = ctrl->subsys;
758 
759 	if (ctrl->ops->print_device_info) {
760 		ctrl->ops->print_device_info(ctrl);
761 		return;
762 	}
763 
764 	dev_err(ctrl->device,
765 		"VID:%04x model:%.*s firmware:%.*s\n", subsys->vendor_id,
766 		nvme_strlen(subsys->model, sizeof(subsys->model)),
767 		subsys->model, nvme_strlen(subsys->firmware_rev,
768 					   sizeof(subsys->firmware_rev)),
769 		subsys->firmware_rev);
770 }
771 
772 #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
773 void nvme_fault_inject_init(struct nvme_fault_inject *fault_inj,
774 			    const char *dev_name);
775 void nvme_fault_inject_fini(struct nvme_fault_inject *fault_inject);
776 void nvme_should_fail(struct request *req);
777 #else
778 static inline void nvme_fault_inject_init(struct nvme_fault_inject *fault_inj,
779 					  const char *dev_name)
780 {
781 }
782 static inline void nvme_fault_inject_fini(struct nvme_fault_inject *fault_inj)
783 {
784 }
785 static inline void nvme_should_fail(struct request *req) {}
786 #endif
787 
788 bool nvme_wait_reset(struct nvme_ctrl *ctrl);
789 int nvme_try_sched_reset(struct nvme_ctrl *ctrl);
790 
791 static inline int nvme_reset_subsystem(struct nvme_ctrl *ctrl)
792 {
793 	if (!ctrl->subsystem || !ctrl->ops->subsystem_reset)
794 		return -ENOTTY;
795 	return ctrl->ops->subsystem_reset(ctrl);
796 }
797 
798 /*
799  * Convert a 512B sector number to a device logical block number.
800  */
801 static inline u64 nvme_sect_to_lba(struct nvme_ns_head *head, sector_t sector)
802 {
803 	return sector >> (head->lba_shift - SECTOR_SHIFT);
804 }
805 
806 /*
807  * Convert a device logical block number to a 512B sector number.
808  */
809 static inline sector_t nvme_lba_to_sect(struct nvme_ns_head *head, u64 lba)
810 {
811 	return lba << (head->lba_shift - SECTOR_SHIFT);
812 }
813 
814 /*
815  * Convert byte length to nvme's 0-based num dwords
816  */
817 static inline u32 nvme_bytes_to_numd(size_t len)
818 {
819 	return (len >> 2) - 1;
820 }
821 
822 /* Decode a 2-byte "0's based"/"0-based" field */
823 static inline u32 from0based(__le16 value)
824 {
825 	return (u32)le16_to_cpu(value) + 1;
826 }
827 
828 static inline bool nvme_is_ana_error(u16 status)
829 {
830 	switch (status & NVME_SCT_SC_MASK) {
831 	case NVME_SC_ANA_TRANSITION:
832 	case NVME_SC_ANA_INACCESSIBLE:
833 	case NVME_SC_ANA_PERSISTENT_LOSS:
834 		return true;
835 	default:
836 		return false;
837 	}
838 }
839 
840 static inline bool nvme_is_path_error(u16 status)
841 {
842 	/* check for a status code type of 'path related status' */
843 	return (status & NVME_SCT_MASK) == NVME_SCT_PATH;
844 }
845 
846 /*
847  * Fill in the status and result information from the CQE, and then figure out
848  * if blk-mq will need to use IPI magic to complete the request, and if yes do
849  * so.  If not let the caller complete the request without an indirect function
850  * call.
851  */
852 static inline bool nvme_try_complete_req(struct request *req, __le16 status,
853 		union nvme_result result)
854 {
855 	struct nvme_request *rq = nvme_req(req);
856 	struct nvme_ctrl *ctrl = rq->ctrl;
857 
858 	if (!(ctrl->quirks & NVME_QUIRK_SKIP_CID_GEN))
859 		rq->genctr++;
860 
861 	rq->status = le16_to_cpu(status) >> 1;
862 	rq->result = result;
863 	/* inject error when permitted by fault injection framework */
864 	nvme_should_fail(req);
865 	if (unlikely(blk_should_fake_timeout(req->q)))
866 		return true;
867 	return blk_mq_complete_request_remote(req);
868 }
869 
870 static inline void nvme_get_ctrl(struct nvme_ctrl *ctrl)
871 {
872 	get_device(ctrl->device);
873 }
874 
875 static inline void nvme_put_ctrl(struct nvme_ctrl *ctrl)
876 {
877 	put_device(ctrl->device);
878 }
879 
880 static inline bool nvme_is_aen_req(u16 qid, __u16 command_id)
881 {
882 	return !qid &&
883 		nvme_tag_from_cid(command_id) >= NVME_AQ_BLK_MQ_DEPTH;
884 }
885 
886 /*
887  * Returns true for sink states that can't ever transition back to live.
888  */
889 static inline bool nvme_state_terminal(struct nvme_ctrl *ctrl)
890 {
891 	switch (nvme_ctrl_state(ctrl)) {
892 	case NVME_CTRL_NEW:
893 	case NVME_CTRL_LIVE:
894 	case NVME_CTRL_RESETTING:
895 	case NVME_CTRL_CONNECTING:
896 		return false;
897 	case NVME_CTRL_DELETING:
898 	case NVME_CTRL_DELETING_NOIO:
899 	case NVME_CTRL_DEAD:
900 		return true;
901 	default:
902 		WARN_ONCE(1, "Unhandled ctrl state:%d", ctrl->state);
903 		return true;
904 	}
905 }
906 
907 void nvme_end_req(struct request *req);
908 void nvme_complete_rq(struct request *req);
909 void nvme_complete_batch_req(struct request *req);
910 
911 static __always_inline void nvme_complete_batch(struct io_comp_batch *iob,
912 						void (*fn)(struct request *rq))
913 {
914 	struct request *req;
915 
916 	rq_list_for_each(&iob->req_list, req) {
917 		fn(req);
918 		nvme_complete_batch_req(req);
919 	}
920 	blk_mq_end_request_batch(iob);
921 }
922 
923 blk_status_t nvme_host_path_error(struct request *req);
924 bool nvme_cancel_request(struct request *req, void *data);
925 void nvme_cancel_tagset(struct nvme_ctrl *ctrl);
926 void nvme_cancel_admin_tagset(struct nvme_ctrl *ctrl);
927 bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
928 		enum nvme_ctrl_state new_state);
929 int nvme_disable_ctrl(struct nvme_ctrl *ctrl, bool shutdown);
930 int nvme_enable_ctrl(struct nvme_ctrl *ctrl);
931 int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
932 		const struct nvme_ctrl_ops *ops, unsigned long quirks);
933 int nvme_add_ctrl(struct nvme_ctrl *ctrl);
934 void nvme_uninit_ctrl(struct nvme_ctrl *ctrl);
935 void nvme_start_ctrl(struct nvme_ctrl *ctrl);
936 void nvme_stop_ctrl(struct nvme_ctrl *ctrl);
937 int nvme_init_ctrl_finish(struct nvme_ctrl *ctrl, bool was_suspended);
938 int nvme_alloc_admin_tag_set(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set,
939 		const struct blk_mq_ops *ops, unsigned int cmd_size);
940 void nvme_remove_admin_tag_set(struct nvme_ctrl *ctrl);
941 int nvme_alloc_io_tag_set(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set,
942 		const struct blk_mq_ops *ops, unsigned int nr_maps,
943 		unsigned int cmd_size);
944 void nvme_remove_io_tag_set(struct nvme_ctrl *ctrl);
945 
946 void nvme_remove_namespaces(struct nvme_ctrl *ctrl);
947 
948 void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
949 		volatile union nvme_result *res);
950 
951 void nvme_quiesce_io_queues(struct nvme_ctrl *ctrl);
952 void nvme_unquiesce_io_queues(struct nvme_ctrl *ctrl);
953 void nvme_quiesce_admin_queue(struct nvme_ctrl *ctrl);
954 void nvme_unquiesce_admin_queue(struct nvme_ctrl *ctrl);
955 void nvme_mark_namespaces_dead(struct nvme_ctrl *ctrl);
956 void nvme_sync_queues(struct nvme_ctrl *ctrl);
957 void nvme_sync_io_queues(struct nvme_ctrl *ctrl);
958 void nvme_unfreeze(struct nvme_ctrl *ctrl);
959 void nvme_wait_freeze(struct nvme_ctrl *ctrl);
960 int nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl);
961 void nvme_start_freeze(struct nvme_ctrl *ctrl);
962 
963 static inline enum req_op nvme_req_op(struct nvme_command *cmd)
964 {
965 	return nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
966 }
967 
968 #define NVME_QID_ANY -1
969 void nvme_init_request(struct request *req, struct nvme_command *cmd);
970 void nvme_cleanup_cmd(struct request *req);
971 blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req);
972 blk_status_t nvme_fail_nonready_command(struct nvme_ctrl *ctrl,
973 		struct request *req);
974 bool __nvme_check_ready(struct nvme_ctrl *ctrl, struct request *rq,
975 		bool queue_live, enum nvme_ctrl_state state);
976 
977 static inline bool nvme_check_ready(struct nvme_ctrl *ctrl, struct request *rq,
978 		bool queue_live)
979 {
980 	enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
981 
982 	if (likely(state == NVME_CTRL_LIVE))
983 		return true;
984 	if (ctrl->ops->flags & NVME_F_FABRICS && state == NVME_CTRL_DELETING)
985 		return queue_live;
986 	return __nvme_check_ready(ctrl, rq, queue_live, state);
987 }
988 
989 /*
990  * NSID shall be unique for all shared namespaces, or if at least one of the
991  * following conditions is met:
992  *   1. Namespace Management is supported by the controller
993  *   2. ANA is supported by the controller
994  *   3. NVM Set are supported by the controller
995  *
996  * In other case, private namespace are not required to report a unique NSID.
997  */
998 static inline bool nvme_is_unique_nsid(struct nvme_ctrl *ctrl,
999 		struct nvme_ns_head *head)
1000 {
1001 	return head->shared ||
1002 		(ctrl->oacs & NVME_CTRL_OACS_NS_MNGT_SUPP) ||
1003 		(ctrl->subsys->cmic & NVME_CTRL_CMIC_ANA) ||
1004 		(ctrl->ctratt & NVME_CTRL_CTRATT_NVM_SETS);
1005 }
1006 
1007 /*
1008  * Flags for __nvme_submit_sync_cmd()
1009  */
1010 typedef __u32 __bitwise nvme_submit_flags_t;
1011 
1012 enum {
1013 	/* Insert request at the head of the queue */
1014 	NVME_SUBMIT_AT_HEAD  = (__force nvme_submit_flags_t)(1 << 0),
1015 	/* Set BLK_MQ_REQ_NOWAIT when allocating request */
1016 	NVME_SUBMIT_NOWAIT = (__force nvme_submit_flags_t)(1 << 1),
1017 	/* Set BLK_MQ_REQ_RESERVED when allocating request */
1018 	NVME_SUBMIT_RESERVED = (__force nvme_submit_flags_t)(1 << 2),
1019 	/* Retry command when NVME_STATUS_DNR is not set in the result */
1020 	NVME_SUBMIT_RETRY = (__force nvme_submit_flags_t)(1 << 3),
1021 };
1022 
1023 int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
1024 		void *buf, unsigned bufflen);
1025 int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
1026 		union nvme_result *result, void *buffer, unsigned bufflen,
1027 		int qid, nvme_submit_flags_t flags);
1028 int nvme_set_features(struct nvme_ctrl *dev, unsigned int fid,
1029 		      unsigned int dword11, void *buffer, size_t buflen,
1030 		      void *result);
1031 int nvme_get_features(struct nvme_ctrl *dev, unsigned int fid,
1032 		      unsigned int dword11, void *buffer, size_t buflen,
1033 		      void *result);
1034 int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count);
1035 void nvme_stop_keep_alive(struct nvme_ctrl *ctrl);
1036 int nvme_reset_ctrl(struct nvme_ctrl *ctrl);
1037 int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl);
1038 int nvme_delete_ctrl(struct nvme_ctrl *ctrl);
1039 void nvme_queue_scan(struct nvme_ctrl *ctrl);
1040 int nvme_get_log(struct nvme_ctrl *ctrl, u32 nsid, u8 log_page, u8 lsp, u8 csi,
1041 		void *log, size_t size, u64 offset);
1042 void nvme_get_ns_head(struct nvme_ns_head *head);
1043 bool nvme_tryget_ns_head(struct nvme_ns_head *head);
1044 void nvme_put_ns_head(struct nvme_ns_head *head);
1045 int nvme_cdev_add(struct cdev *cdev, struct device *cdev_device,
1046 		const struct file_operations *fops, struct module *owner,
1047 		int ctrl, int head);
1048 void nvme_cdev_del(struct cdev *cdev, struct device *cdev_device);
1049 int nvme_ioctl(struct block_device *bdev, blk_mode_t mode,
1050 		unsigned int cmd, unsigned long arg);
1051 long nvme_ns_chr_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
1052 int nvme_ns_head_ioctl(struct block_device *bdev, blk_mode_t mode,
1053 		unsigned int cmd, unsigned long arg);
1054 long nvme_ns_head_chr_ioctl(struct file *file, unsigned int cmd,
1055 		unsigned long arg);
1056 long nvme_dev_ioctl(struct file *file, unsigned int cmd,
1057 		unsigned long arg);
1058 int nvme_ns_chr_uring_cmd_iopoll(struct io_uring_cmd *ioucmd,
1059 		struct io_comp_batch *iob, unsigned int poll_flags);
1060 int nvme_ns_chr_uring_cmd(struct io_uring_cmd *ioucmd,
1061 		unsigned int issue_flags);
1062 int nvme_ns_head_chr_uring_cmd(struct io_uring_cmd *ioucmd,
1063 		unsigned int issue_flags);
1064 int nvme_identify_ns(struct nvme_ctrl *ctrl, unsigned nsid,
1065 		struct nvme_id_ns **id);
1066 int nvme_getgeo(struct gendisk *disk, struct hd_geometry *geo);
1067 int nvme_dev_uring_cmd(struct io_uring_cmd *ioucmd, unsigned int issue_flags);
1068 
1069 extern const struct attribute_group *nvme_ns_attr_groups[];
1070 extern const struct attribute_group nvme_ns_mpath_attr_group;
1071 extern const struct pr_ops nvme_pr_ops;
1072 extern const struct block_device_operations nvme_ns_head_ops;
1073 extern const struct attribute_group nvme_dev_attrs_group;
1074 extern const struct attribute_group nvme_dev_diag_attrs_group;
1075 extern const struct attribute_group *nvme_subsys_attrs_groups[];
1076 extern const struct attribute_group *nvme_dev_attr_groups[];
1077 extern const struct block_device_operations nvme_bdev_ops;
1078 
1079 void nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl);
1080 struct nvme_ns *nvme_find_path(struct nvme_ns_head *head)
1081 	__must_hold_shared(&head->srcu);
1082 #ifdef CONFIG_NVME_MULTIPATH
1083 static inline bool nvme_ctrl_use_ana(struct nvme_ctrl *ctrl)
1084 {
1085 	return ctrl->ana_log_buf != NULL;
1086 }
1087 
1088 void nvme_mpath_unfreeze(struct nvme_subsystem *subsys)
1089 	__must_hold(&subsys->lock);
1090 void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys)
1091 	__must_hold(&subsys->lock);
1092 void nvme_mpath_start_freeze(struct nvme_subsystem *subsys)
1093 	__must_hold(&subsys->lock);
1094 void nvme_mpath_default_iopolicy(struct nvme_subsystem *subsys);
1095 void nvme_failover_req(struct request *req);
1096 void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl);
1097 int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl,struct nvme_ns_head *head);
1098 void nvme_mpath_add_sysfs_link(struct nvme_ns_head *ns);
1099 void nvme_mpath_remove_sysfs_link(struct nvme_ns *ns);
1100 void nvme_mpath_add_disk(struct nvme_ns *ns, __le32 anagrpid);
1101 void nvme_mpath_put_disk(struct nvme_ns_head *head);
1102 int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id);
1103 void nvme_mpath_init_ctrl(struct nvme_ctrl *ctrl);
1104 void nvme_mpath_update(struct nvme_ctrl *ctrl);
1105 void nvme_mpath_uninit(struct nvme_ctrl *ctrl);
1106 void nvme_mpath_stop(struct nvme_ctrl *ctrl);
1107 bool nvme_mpath_clear_current_path(struct nvme_ns *ns);
1108 void nvme_mpath_revalidate_paths(struct nvme_ns_head *head);
1109 void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl);
1110 void nvme_mpath_remove_disk(struct nvme_ns_head *head);
1111 void nvme_mpath_start_request(struct request *rq);
1112 void nvme_mpath_end_request(struct request *rq);
1113 
1114 static inline void nvme_trace_bio_complete(struct request *req)
1115 {
1116 	struct nvme_ns *ns = req->q->queuedata;
1117 
1118 	if ((req->cmd_flags & REQ_NVME_MPATH) && req->bio)
1119 		trace_block_bio_complete(ns->head->disk->queue, req->bio);
1120 }
1121 
1122 extern bool multipath;
1123 extern struct device_attribute dev_attr_ana_grpid;
1124 extern struct device_attribute dev_attr_ana_state;
1125 extern struct device_attribute dev_attr_queue_depth;
1126 extern struct device_attribute dev_attr_numa_nodes;
1127 extern struct device_attribute dev_attr_path_state;
1128 extern struct device_attribute dev_attr_delayed_removal_secs;
1129 extern struct device_attribute dev_attr_fail_on_ctrl_loss;
1130 extern struct device_attribute dev_attr_multipath_failover_count;
1131 extern struct device_attribute dev_attr_io_requeue_no_usable_path_count;
1132 extern struct device_attribute dev_attr_io_fail_no_available_path_count;
1133 extern struct device_attribute subsys_attr_iopolicy;
1134 extern struct device_attribute subsys_attr_iopolicies;
1135 
1136 static inline bool nvme_disk_is_ns_head(struct gendisk *disk)
1137 {
1138 	return disk->fops == &nvme_ns_head_ops;
1139 }
1140 static inline bool nvme_mpath_queue_if_no_path(struct nvme_ns_head *head)
1141 {
1142 	if (test_bit(NVME_NSHEAD_QUEUE_IF_NO_PATH, &head->flags))
1143 		return true;
1144 	return false;
1145 }
1146 #else
1147 #define multipath false
1148 static inline bool nvme_ctrl_use_ana(struct nvme_ctrl *ctrl)
1149 {
1150 	return false;
1151 }
1152 static inline void nvme_failover_req(struct request *req)
1153 {
1154 }
1155 static inline void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
1156 {
1157 }
1158 static inline int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl,
1159 		struct nvme_ns_head *head)
1160 {
1161 	return 0;
1162 }
1163 static inline void nvme_mpath_add_disk(struct nvme_ns *ns, __le32 anagrpid)
1164 {
1165 }
1166 static inline void nvme_mpath_put_disk(struct nvme_ns_head *head)
1167 {
1168 }
1169 static inline void nvme_mpath_add_sysfs_link(struct nvme_ns *ns)
1170 {
1171 }
1172 static inline void nvme_mpath_remove_sysfs_link(struct nvme_ns *ns)
1173 {
1174 }
1175 static inline bool nvme_mpath_clear_current_path(struct nvme_ns *ns)
1176 {
1177 	return false;
1178 }
1179 static inline void nvme_mpath_revalidate_paths(struct nvme_ns_head *head)
1180 {
1181 }
1182 static inline void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl)
1183 {
1184 }
1185 static inline void nvme_mpath_remove_disk(struct nvme_ns_head *head)
1186 {
1187 }
1188 static inline void nvme_trace_bio_complete(struct request *req)
1189 {
1190 }
1191 static inline void nvme_mpath_init_ctrl(struct nvme_ctrl *ctrl)
1192 {
1193 }
1194 static inline int nvme_mpath_init_identify(struct nvme_ctrl *ctrl,
1195 		struct nvme_id_ctrl *id)
1196 {
1197 	if (ctrl->subsys->cmic & NVME_CTRL_CMIC_ANA)
1198 		dev_warn(ctrl->device,
1199 "Please enable CONFIG_NVME_MULTIPATH for full support of multi-port devices.\n");
1200 	return 0;
1201 }
1202 static inline void nvme_mpath_update(struct nvme_ctrl *ctrl)
1203 {
1204 }
1205 static inline void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
1206 {
1207 }
1208 static inline void nvme_mpath_stop(struct nvme_ctrl *ctrl)
1209 {
1210 }
1211 static inline void nvme_mpath_unfreeze(struct nvme_subsystem *subsys)
1212 {
1213 }
1214 static inline void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys)
1215 {
1216 }
1217 static inline void nvme_mpath_start_freeze(struct nvme_subsystem *subsys)
1218 {
1219 }
1220 static inline void nvme_mpath_default_iopolicy(struct nvme_subsystem *subsys)
1221 {
1222 }
1223 static inline void nvme_mpath_start_request(struct request *rq)
1224 {
1225 }
1226 static inline void nvme_mpath_end_request(struct request *rq)
1227 {
1228 }
1229 static inline bool nvme_disk_is_ns_head(struct gendisk *disk)
1230 {
1231 	return false;
1232 }
1233 static inline bool nvme_mpath_queue_if_no_path(struct nvme_ns_head *head)
1234 {
1235 	return false;
1236 }
1237 #endif /* CONFIG_NVME_MULTIPATH */
1238 
1239 #if defined(CONFIG_NVME_MULTIPATH) && defined(CONFIG_BLK_DEV_ZONED)
1240 int nvme_mpath_revalidate_zones(struct nvme_ns_head *head);
1241 #else
1242 static inline int nvme_mpath_revalidate_zones(struct nvme_ns_head *head)
1243 {
1244 	return 0;
1245 }
1246 #endif
1247 
1248 int nvme_ns_get_unique_id(struct nvme_ns *ns, u8 id[16],
1249 		enum blk_unique_id type);
1250 
1251 struct nvme_zone_info {
1252 	u64 zone_size;
1253 	unsigned int max_open_zones;
1254 	unsigned int max_active_zones;
1255 };
1256 
1257 int nvme_ns_report_zones(struct nvme_ns *ns, sector_t sector,
1258 		unsigned int nr_zones, struct blk_report_zones_args *args);
1259 int nvme_query_zone_info(struct nvme_ns *ns, unsigned lbaf,
1260 		struct nvme_zone_info *zi);
1261 void nvme_update_zone_info(struct nvme_ns *ns, struct queue_limits *lim,
1262 		struct nvme_zone_info *zi);
1263 #ifdef CONFIG_BLK_DEV_ZONED
1264 blk_status_t nvme_setup_zone_mgmt_send(struct nvme_ns *ns, struct request *req,
1265 				       struct nvme_command *cmnd,
1266 				       enum nvme_zone_mgmt_action action);
1267 #else
1268 static inline blk_status_t nvme_setup_zone_mgmt_send(struct nvme_ns *ns,
1269 		struct request *req, struct nvme_command *cmnd,
1270 		enum nvme_zone_mgmt_action action)
1271 {
1272 	return BLK_STS_NOTSUPP;
1273 }
1274 #endif
1275 
1276 static inline struct nvme_ns *nvme_get_ns_from_dev(struct device *dev)
1277 {
1278 	struct gendisk *disk = dev_to_disk(dev);
1279 
1280 	WARN_ON(nvme_disk_is_ns_head(disk));
1281 	return disk->private_data;
1282 }
1283 
1284 #ifdef CONFIG_NVME_HWMON
1285 int nvme_hwmon_init(struct nvme_ctrl *ctrl);
1286 void nvme_hwmon_exit(struct nvme_ctrl *ctrl);
1287 #else
1288 static inline int nvme_hwmon_init(struct nvme_ctrl *ctrl)
1289 {
1290 	return 0;
1291 }
1292 
1293 static inline void nvme_hwmon_exit(struct nvme_ctrl *ctrl)
1294 {
1295 }
1296 #endif
1297 
1298 static inline void nvme_start_request(struct request *rq)
1299 {
1300 	if (rq->cmd_flags & REQ_NVME_MPATH)
1301 		nvme_mpath_start_request(rq);
1302 	blk_mq_start_request(rq);
1303 }
1304 
1305 static inline bool nvme_ctrl_sgl_supported(struct nvme_ctrl *ctrl)
1306 {
1307 	return ctrl->sgls & (NVME_CTRL_SGLS_BYTE_ALIGNED |
1308 			     NVME_CTRL_SGLS_DWORD_ALIGNED);
1309 }
1310 
1311 static inline bool nvme_ctrl_meta_sgl_supported(struct nvme_ctrl *ctrl)
1312 {
1313 	if (ctrl->ops->flags & NVME_F_FABRICS)
1314 		return true;
1315 	return ctrl->sgls & NVME_CTRL_SGLS_MSDS;
1316 }
1317 
1318 #ifdef CONFIG_NVME_HOST_AUTH
1319 int __init nvme_init_auth(void);
1320 void __exit nvme_exit_auth(void);
1321 int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl);
1322 void nvme_auth_stop(struct nvme_ctrl *ctrl);
1323 int nvme_auth_negotiate(struct nvme_ctrl *ctrl, int qid);
1324 int nvme_auth_wait(struct nvme_ctrl *ctrl, int qid);
1325 void nvme_auth_free(struct nvme_ctrl *ctrl);
1326 void nvme_auth_revoke_tls_key(struct nvme_ctrl *ctrl);
1327 #else
1328 static inline int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
1329 {
1330 	return 0;
1331 }
1332 static inline int __init nvme_init_auth(void)
1333 {
1334 	return 0;
1335 }
1336 static inline void __exit nvme_exit_auth(void)
1337 {
1338 }
1339 static inline void nvme_auth_stop(struct nvme_ctrl *ctrl) {};
1340 static inline int nvme_auth_negotiate(struct nvme_ctrl *ctrl, int qid)
1341 {
1342 	return -EPROTONOSUPPORT;
1343 }
1344 static inline int nvme_auth_wait(struct nvme_ctrl *ctrl, int qid)
1345 {
1346 	return -EPROTONOSUPPORT;
1347 }
1348 static inline void nvme_auth_free(struct nvme_ctrl *ctrl) {};
1349 static inline void nvme_auth_revoke_tls_key(struct nvme_ctrl *ctrl) {};
1350 #endif
1351 
1352 u32 nvme_command_effects(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
1353 			 u8 opcode);
1354 u32 nvme_passthru_start(struct nvme_ctrl *ctrl, struct nvme_ns *ns, u8 opcode)
1355 	__cond_acquires(nonzero, &ctrl->subsys->lock)
1356 	__cond_acquires(nonzero, &ctrl->scan_lock);
1357 
1358 int nvme_execute_rq(struct request *rq, bool at_head);
1359 u32 nvme_passthru_end(struct nvme_ctrl *ctrl, struct nvme_ns *ns, u32 effects,
1360 		       struct nvme_command *cmd, int status)
1361 		       __cond_releases(nonzero, &ctrl->scan_lock)
1362 		       __cond_releases(nonzero, &ctrl->subsys->lock);
1363 
1364 struct nvme_ctrl *nvme_ctrl_from_file(struct file *file);
1365 struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid);
1366 bool nvme_get_ns(struct nvme_ns *ns);
1367 void nvme_put_ns(struct nvme_ns *ns);
1368 
1369 static inline bool nvme_multi_css(struct nvme_ctrl *ctrl)
1370 {
1371 	return (ctrl->ctrl_config & NVME_CC_CSS_MASK) == NVME_CC_CSS_CSI;
1372 }
1373 
1374 #endif /* _NVME_H */
1375