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