blob: 773dde5b231dab0e0580d71141a6535f6a1f5e23 [file] [log] [blame]
Christoph Hellwigbc50ad72019-02-18 09:36:29 +01001/* SPDX-License-Identifier: GPL-2.0 */
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +02002/*
3 * Copyright (c) 2011-2014, Intel Corporation.
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +02004 */
5
6#ifndef _NVME_H
7#define _NVME_H
8
9#include <linux/nvme.h>
Christoph Hellwiga6a51492017-10-18 16:59:25 +020010#include <linux/cdev.h>
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +020011#include <linux/pci.h>
12#include <linux/kref.h>
13#include <linux/blk-mq.h>
Matias Bjørlingb0b4e092016-09-16 14:25:07 +020014#include <linux/lightnvm.h>
Scott Bauera98e58e52017-02-03 12:50:32 -070015#include <linux/sed-opal.h>
Thomas Taib9e03852018-02-08 13:38:29 -050016#include <linux/fault-inject.h>
Johannes Thumshirn978628e2018-05-17 13:52:50 +020017#include <linux/rcupdate.h>
Keith Buschc1ac9a4b2019-09-04 10:06:11 -060018#include <linux/wait.h>
James Smart4d2ce682020-05-19 17:05:51 +030019#include <linux/t10-pi.h>
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +020020
Hannes Reinecke35fe0d12019-07-24 15:47:55 +020021#include <trace/events/block.h>
22
Marc Olson8ae4e442017-09-06 17:23:56 -070023extern unsigned int nvme_io_timeout;
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +020024#define NVME_IO_TIMEOUT (nvme_io_timeout * HZ)
25
Marc Olson8ae4e442017-09-06 17:23:56 -070026extern unsigned int admin_timeout;
Chaitanya Kulkarnidc96f932020-11-09 16:33:45 -080027#define NVME_ADMIN_TIMEOUT (admin_timeout * HZ)
Christoph Hellwig21d34712015-11-26 09:08:36 +010028
Sagi Grimberg038bd4c2016-06-13 16:45:28 +020029#define NVME_DEFAULT_KATO 5
Sagi Grimberg038bd4c2016-06-13 16:45:28 +020030
Israel Rukshin38e18002019-11-24 18:38:30 +020031#ifdef CONFIG_ARCH_NO_SG_CHAIN
32#define NVME_INLINE_SG_CNT 0
Israel Rukshinba7ca2a2020-05-19 17:05:54 +030033#define NVME_INLINE_METADATA_SG_CNT 0
Israel Rukshin38e18002019-11-24 18:38:30 +020034#else
35#define NVME_INLINE_SG_CNT 2
Israel Rukshinba7ca2a2020-05-19 17:05:54 +030036#define NVME_INLINE_METADATA_SG_CNT 1
Israel Rukshin38e18002019-11-24 18:38:30 +020037#endif
38
Chaitanya Kulkarni6c3c05b2020-07-16 17:51:37 -070039/*
40 * Default to a 4K page size, with the intention to update this
41 * path in the future to accommodate architectures with differing
42 * kernel and IO page sizes.
43 */
44#define NVME_CTRL_PAGE_SHIFT 12
45#define NVME_CTRL_PAGE_SIZE (1 << NVME_CTRL_PAGE_SHIFT)
46
Sagi Grimberg9a6327d2017-06-07 20:31:55 +020047extern struct workqueue_struct *nvme_wq;
Roy Shtermanb227c592018-01-14 12:39:02 +020048extern struct workqueue_struct *nvme_reset_wq;
49extern struct workqueue_struct *nvme_delete_wq;
Sagi Grimberg9a6327d2017-06-07 20:31:55 +020050
Matias Bjørlingca064082015-10-29 17:57:29 +090051enum {
52 NVME_NS_LBA = 0,
53 NVME_NS_LIGHTNVM = 1,
54};
55
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +020056/*
Christoph Hellwig106198e2015-11-26 10:07:41 +010057 * List of workarounds for devices that required behavior not specified in
58 * the standard.
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +020059 */
Christoph Hellwig106198e2015-11-26 10:07:41 +010060enum nvme_quirks {
61 /*
62 * Prefers I/O aligned to a stripe size specified in a vendor
63 * specific Identify field.
64 */
65 NVME_QUIRK_STRIPE_SIZE = (1 << 0),
Keith Busch540c8012015-10-22 15:45:06 -060066
67 /*
68 * The controller doesn't handle Identify value others than 0 or 1
69 * correctly.
70 */
71 NVME_QUIRK_IDENTIFY_CNS = (1 << 1),
Keith Busch08095e72016-03-04 13:15:17 -070072
73 /*
Christoph Hellwige850fd12017-04-05 19:21:13 +020074 * The controller deterministically returns O's on reads to
75 * logical blocks that deallocate was called on.
Keith Busch08095e72016-03-04 13:15:17 -070076 */
Christoph Hellwige850fd12017-04-05 19:21:13 +020077 NVME_QUIRK_DEALLOCATE_ZEROES = (1 << 2),
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -030078
79 /*
80 * The controller needs a delay before starts checking the device
81 * readiness, which is done by reading the NVME_CSTS_RDY bit.
82 */
83 NVME_QUIRK_DELAY_BEFORE_CHK_RDY = (1 << 3),
Andy Lutomirskic5552fd2017-02-07 10:08:45 -080084
85 /*
86 * APST should not be used.
87 */
88 NVME_QUIRK_NO_APST = (1 << 4),
Andy Lutomirskiff5350a2017-04-20 13:37:55 -070089
90 /*
91 * The deepest sleep state should not be used.
92 */
93 NVME_QUIRK_NO_DEEPEST_PS = (1 << 5),
Christoph Hellwig608cc4b2017-09-06 11:45:24 +020094
95 /*
96 * Supports the LighNVM command set if indicated in vs[1].
97 */
98 NVME_QUIRK_LIGHTNVM = (1 << 6),
Jens Axboe9abd68e2018-05-08 10:25:15 -060099
100 /*
101 * Set MEDIUM priority on SQ creation
102 */
103 NVME_QUIRK_MEDIUM_PRIO_SQ = (1 << 7),
James Dingwall62993582019-01-08 10:20:51 -0700104
105 /*
106 * Ignore device provided subnqn.
107 */
108 NVME_QUIRK_IGNORE_DEV_SUBNQN = (1 << 8),
Christoph Hellwig7b210e42019-03-13 18:55:05 +0100109
110 /*
111 * Broken Write Zeroes.
112 */
113 NVME_QUIRK_DISABLE_WRITE_ZEROES = (1 << 9),
Mario Limonciellocb32de12019-08-16 15:16:19 -0500114
115 /*
116 * Force simple suspend/resume path.
117 */
118 NVME_QUIRK_SIMPLE_SUSPEND = (1 << 10),
Linus Torvalds7ad67ca2019-09-17 16:57:47 -0700119
120 /*
Benjamin Herrenschmidt66341332019-08-07 17:51:21 +1000121 * Use only one interrupt vector for all queues
122 */
Linus Torvalds7ad67ca2019-09-17 16:57:47 -0700123 NVME_QUIRK_SINGLE_VECTOR = (1 << 11),
Benjamin Herrenschmidt66341332019-08-07 17:51:21 +1000124
125 /*
126 * Use non-standard 128 bytes SQEs.
127 */
Linus Torvalds7ad67ca2019-09-17 16:57:47 -0700128 NVME_QUIRK_128_BYTES_SQES = (1 << 12),
Benjamin Herrenschmidtd38e9f02019-08-07 17:51:22 +1000129
130 /*
131 * Prevent tag overlap between queues
132 */
Linus Torvalds7ad67ca2019-09-17 16:57:47 -0700133 NVME_QUIRK_SHARED_TAGS = (1 << 13),
Akinobu Mita6c6aa2f2019-11-15 00:40:01 +0900134
135 /*
136 * Don't change the value of the temperature threshold feature
137 */
138 NVME_QUIRK_NO_TEMP_THRESH_CHANGE = (1 << 14),
Christoph Hellwig5bedd3a2020-07-28 13:09:03 +0200139
140 /*
141 * The controller doesn't handle the Identify Namespace
142 * Identification Descriptor list subcommand despite claiming
143 * NVMe 1.3 compliance.
144 */
145 NVME_QUIRK_NO_NS_DESC_LIST = (1 << 15),
Filippo Sironi4bdf2602021-02-10 01:39:42 +0100146
147 /*
148 * The controller does not properly handle DMA addresses over
149 * 48 bits.
150 */
151 NVME_QUIRK_DMA_ADDRESS_BITS_48 = (1 << 16),
Christoph Hellwig106198e2015-11-26 10:07:41 +0100152};
153
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800154/*
155 * Common request structure for NVMe passthrough. All drivers must have
156 * this structure as the first member of their request-private data.
157 */
158struct nvme_request {
159 struct nvme_command *cmd;
160 union nvme_result result;
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200161 u8 retries;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200162 u8 flags;
163 u16 status;
Sagi Grimberg59e29ce2018-06-29 16:50:00 -0600164 struct nvme_ctrl *ctrl;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200165};
166
Christoph Hellwig32acab32017-11-02 12:59:30 +0100167/*
168 * Mark a bio as coming in through the mpath node.
169 */
170#define REQ_NVME_MPATH REQ_DRV
171
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200172enum {
173 NVME_REQ_CANCELLED = (1 << 0),
James Smartbb06ec312018-04-12 09:16:15 -0600174 NVME_REQ_USERCMD = (1 << 1),
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800175};
176
177static inline struct nvme_request *nvme_req(struct request *req)
178{
179 return blk_mq_rq_to_pdu(req);
180}
181
Keith Busch5d87eb92018-06-29 16:50:01 -0600182static inline u16 nvme_req_qid(struct request *req)
183{
Keith Busch643c4762020-10-15 11:36:29 -0700184 if (!req->q->queuedata)
Keith Busch5d87eb92018-06-29 16:50:01 -0600185 return 0;
Baolin Wang84115d62020-10-27 16:15:16 +0800186
187 return req->mq_hctx->queue_num + 1;
Keith Busch5d87eb92018-06-29 16:50:01 -0600188}
189
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -0300190/* The below value is the specific amount of delay needed before checking
191 * readiness in case of the PCI_DEVICE(0x1c58, 0x0003), which needs the
192 * NVME_QUIRK_DELAY_BEFORE_CHK_RDY quirk enabled. The value (in ms) was
193 * found empirically.
194 */
Jeff Lien8c97eec2017-11-21 10:44:37 -0600195#define NVME_QUIRK_DELAY_AMOUNT 2300
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -0300196
Sagi Grimberg4212f4e2020-07-22 16:32:18 -0700197/*
198 * enum nvme_ctrl_state: Controller state
199 *
200 * @NVME_CTRL_NEW: New controller just allocated, initial state
201 * @NVME_CTRL_LIVE: Controller is connected and I/O capable
202 * @NVME_CTRL_RESETTING: Controller is resetting (or scheduled reset)
203 * @NVME_CTRL_CONNECTING: Controller is disconnected, now connecting the
204 * transport
205 * @NVME_CTRL_DELETING: Controller is deleting (or scheduled deletion)
Sagi Grimbergecca390e2020-07-22 16:32:19 -0700206 * @NVME_CTRL_DELETING_NOIO: Controller is deleting and I/O is not
207 * disabled/failed immediately. This state comes
208 * after all async event processing took place and
209 * before ns removal and the controller deletion
210 * progress
Sagi Grimberg4212f4e2020-07-22 16:32:18 -0700211 * @NVME_CTRL_DEAD: Controller is non-present/unresponsive during
212 * shutdown or removal. In this case we forcibly
213 * kill all inflight I/O as they have no chance to
214 * complete
215 */
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200216enum nvme_ctrl_state {
217 NVME_CTRL_NEW,
218 NVME_CTRL_LIVE,
219 NVME_CTRL_RESETTING,
Max Gurtovoyad6a0a52018-01-31 18:31:24 +0200220 NVME_CTRL_CONNECTING,
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200221 NVME_CTRL_DELETING,
Sagi Grimbergecca390e2020-07-22 16:32:19 -0700222 NVME_CTRL_DELETING_NOIO,
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600223 NVME_CTRL_DEAD,
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200224};
225
Akinobu Mitaa3646452019-06-20 08:49:02 +0200226struct nvme_fault_inject {
227#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
228 struct fault_attr attr;
229 struct dentry *parent;
230 bool dont_retry; /* DNR, do not retry */
231 u16 status; /* status code */
232#endif
233};
234
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100235struct nvme_ctrl {
Sagi Grimberg6e3ca03e2018-11-02 10:28:15 -0700236 bool comp_seen;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200237 enum nvme_ctrl_state state;
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -0700238 bool identified;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200239 spinlock_t lock;
Keith Busche7ad43c2019-01-28 09:46:07 -0700240 struct mutex scan_lock;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100241 const struct nvme_ctrl_ops *ops;
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +0200242 struct request_queue *admin_q;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200243 struct request_queue *connect_q;
Sagi Grimberge7832cb2019-08-02 19:33:59 -0700244 struct request_queue *fabrics_q;
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +0200245 struct device *dev;
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +0200246 int instance;
Hannes Reinecke103e5152018-11-16 09:22:29 +0100247 int numa_node;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100248 struct blk_mq_tag_set *tagset;
Sagi Grimberg34b6c232017-07-10 09:22:29 +0300249 struct blk_mq_tag_set *admin_tagset;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100250 struct list_head namespaces;
Jianchao Wang765cc0312018-02-12 20:54:46 +0800251 struct rw_semaphore namespaces_rwsem;
Christoph Hellwigd22524a2017-10-18 13:25:42 +0200252 struct device ctrl_device;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100253 struct device *device; /* char device */
Hannes Reineckeed7770f2021-01-19 07:43:18 +0100254#ifdef CONFIG_NVME_HWMON
255 struct device *hwmon_device;
256#endif
Christoph Hellwiga6a51492017-10-18 16:59:25 +0200257 struct cdev cdev;
Christoph Hellwigd86c4d82017-06-15 15:41:08 +0200258 struct work_struct reset_work;
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200259 struct work_struct delete_work;
Keith Buschc1ac9a4b2019-09-04 10:06:11 -0600260 wait_queue_head_t state_wq;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100261
Christoph Hellwigab9e00c2017-11-09 13:48:55 +0100262 struct nvme_subsystem *subsys;
263 struct list_head subsys_entry;
264
Christoph Hellwig4f1244c2017-02-17 13:59:39 +0100265 struct opal_dev *opal_dev;
Scott Bauera98e58e52017-02-03 12:50:32 -0700266
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +0200267 char name[12];
Christoph Hellwig76e39142016-04-16 14:57:58 -0400268 u16 cntlid;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100269
270 u32 ctrl_config;
Arnav Dawnb6dccf72017-07-12 16:10:40 +0530271 u16 mtfa;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300272 u32 queue_count;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100273
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +0300274 u64 cap;
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +0200275 u32 max_hw_sectors;
Jens Axboe943e9422018-06-21 09:49:37 -0600276 u32 max_segments;
Max Gurtovoy95093352020-05-19 17:05:52 +0300277 u32 max_integrity_segments;
Keith Busch5befc7c2021-03-24 16:18:05 -0700278 u32 max_discard_sectors;
279 u32 max_discard_segments;
280 u32 max_zeroes_sectors;
Keith Busch240e6ee2020-06-29 12:06:41 -0700281#ifdef CONFIG_BLK_DEV_ZONED
282 u32 max_zone_append;
283#endif
Keith Busch49cd84b2018-11-27 09:40:57 -0700284 u16 crdt[3];
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +0200285 u16 oncs;
Scott Bauer8a9ae522017-02-17 13:59:40 +0100286 u16 oacs;
Jens Axboef5d11842017-06-27 12:03:06 -0600287 u16 nssa;
288 u16 nr_streams;
Keith Buschf9686882019-09-26 12:44:39 +0900289 u16 sqsize;
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200290 u32 max_namespaces;
Christoph Hellwig6bf25d12015-11-20 09:36:44 +0100291 atomic_t abort_limit;
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +0200292 u8 vwc;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +0100293 u32 vs;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200294 u32 sgls;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200295 u16 kas;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -0800296 u8 npss;
297 u8 apsta;
Guenter Roeck400b6a72019-11-06 06:35:18 -0800298 u16 wctemp;
299 u16 cctemp;
Hannes Reineckec0561f82018-05-22 11:09:55 +0200300 u32 oaes;
Keith Busche3d78742017-11-07 15:13:14 -0700301 u32 aen_result;
Sagi Grimberg3e53ba32018-11-02 10:28:14 -0700302 u32 ctratt;
Martin K. Petersen07fbd322017-08-25 19:14:50 -0400303 unsigned int shutdown_timeout;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200304 unsigned int kato;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +0100305 bool subsystem;
Christoph Hellwig106198e2015-11-26 10:07:41 +0100306 unsigned long quirks;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -0800307 struct nvme_id_power_state psd[32];
Keith Busch84fef622017-11-07 10:28:32 -0700308 struct nvme_effects_log *effects;
Chaitanya Kulkarni1cf7a122020-09-22 14:05:29 -0700309 struct xarray cels;
Christoph Hellwig5955be22016-04-26 13:51:59 +0200310 struct work_struct scan_work;
Christoph Hellwigf866fc422016-04-26 13:52:00 +0200311 struct work_struct async_event_work;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200312 struct delayed_work ka_work;
Victor Gladkov8c4dfea2020-11-24 18:34:59 +0000313 struct delayed_work failfast_work;
Roland Dreier0a34e462018-01-11 13:38:15 -0800314 struct nvme_command ka_cmd;
Arnav Dawnb6dccf72017-07-12 16:10:40 +0530315 struct work_struct fw_act_work;
Christoph Hellwig30d90962018-05-25 18:17:41 +0200316 unsigned long events;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200317
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200318#ifdef CONFIG_NVME_MULTIPATH
319 /* asymmetric namespace access: */
320 u8 anacap;
321 u8 anatt;
322 u32 anagrpmax;
323 u32 nanagrpid;
324 struct mutex ana_lock;
325 struct nvme_ana_rsp_hdr *ana_log_buf;
326 size_t ana_log_size;
327 struct timer_list anatt_timer;
328 struct work_struct ana_work;
329#endif
330
Andy Lutomirskic5552fd2017-02-07 10:08:45 -0800331 /* Power saving configuration */
332 u64 ps_max_latency_us;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -0400333 bool apst_enabled;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -0800334
Christoph Hellwig044a9df2017-09-11 12:09:28 -0400335 /* PCIe only: */
Christoph Hellwigfe6d53c2017-05-12 17:16:10 +0200336 u32 hmpre;
337 u32 hmmin;
Christoph Hellwig044a9df2017-09-11 12:09:28 -0400338 u32 hmminds;
339 u16 hmmaxd;
Christoph Hellwigfe6d53c2017-05-12 17:16:10 +0200340
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200341 /* Fabrics only */
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200342 u32 ioccsz;
343 u32 iorcsz;
344 u16 icdoff;
345 u16 maxcmd;
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +0300346 int nr_reconnects;
Victor Gladkov8c4dfea2020-11-24 18:34:59 +0000347 unsigned long flags;
348#define NVME_CTRL_FAILFAST_EXPIRED 0
Christoph Hellwig07bfcd02016-06-13 16:45:26 +0200349 struct nvmf_ctrl_options *opts;
Jens Axboecb5b7262018-12-12 09:18:11 -0700350
351 struct page *discard_page;
352 unsigned long discard_page_busy;
Akinobu Mitaf79d5fd2019-06-09 23:17:01 +0900353
354 struct nvme_fault_inject fault_inject;
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +0200355};
356
Hannes Reinecke75c10e72019-02-18 11:43:26 +0100357enum nvme_iopolicy {
358 NVME_IOPOLICY_NUMA,
359 NVME_IOPOLICY_RR,
360};
361
Christoph Hellwigab9e00c2017-11-09 13:48:55 +0100362struct nvme_subsystem {
363 int instance;
364 struct device dev;
365 /*
366 * Because we unregister the device on the last put we need
367 * a separate refcount.
368 */
369 struct kref ref;
370 struct list_head entry;
371 struct mutex lock;
372 struct list_head ctrls;
Christoph Hellwiged754e52017-11-09 13:50:43 +0100373 struct list_head nsheads;
Christoph Hellwigab9e00c2017-11-09 13:48:55 +0100374 char subnqn[NVMF_NQN_SIZE];
375 char serial[20];
376 char model[40];
377 char firmware_rev[8];
378 u8 cmic;
379 u16 vendor_id;
Bart Van Assche81adb862019-06-28 09:53:31 -0700380 u16 awupf; /* 0's based awupf value. */
Christoph Hellwiged754e52017-11-09 13:50:43 +0100381 struct ida ns_ida;
Hannes Reinecke75c10e72019-02-18 11:43:26 +0100382#ifdef CONFIG_NVME_MULTIPATH
383 enum nvme_iopolicy iopolicy;
384#endif
Christoph Hellwigab9e00c2017-11-09 13:48:55 +0100385};
386
Christoph Hellwig002fab02017-11-09 13:50:16 +0100387/*
388 * Container structure for uniqueue namespace identifiers.
389 */
390struct nvme_ns_ids {
391 u8 eui64[8];
392 u8 nguid[16];
393 uuid_t uuid;
Niklas Cassel71010c32020-06-29 12:06:39 -0700394 u8 csi;
Christoph Hellwig002fab02017-11-09 13:50:16 +0100395};
396
Christoph Hellwiged754e52017-11-09 13:50:43 +0100397/*
398 * Anchor structure for namespaces. There is one for each namespace in a
399 * NVMe subsystem that any of our controllers can see, and the namespace
400 * structure for each controller is chained of it. For private namespaces
401 * there is a 1:1 relation to our namespace structures, that is ->list
402 * only ever has a single entry for private namespaces.
403 */
404struct nvme_ns_head {
405 struct list_head list;
406 struct srcu_struct srcu;
407 struct nvme_subsystem *subsys;
408 unsigned ns_id;
409 struct nvme_ns_ids ids;
410 struct list_head entry;
411 struct kref ref;
Keith Busch0c284db2020-04-09 09:09:02 -0700412 bool shared;
Christoph Hellwiged754e52017-11-09 13:50:43 +0100413 int instance;
Keith Buschbe93e872020-06-29 12:06:40 -0700414 struct nvme_effects_log *effects;
Minwoo Im2637bae2021-04-21 16:45:04 +0900415
416 struct cdev cdev;
417 struct device cdev_device;
418
Christoph Hellwigf3334442018-09-11 09:51:29 +0200419 struct gendisk *disk;
Minwoo Im30897382021-04-07 17:49:29 +0200420#ifdef CONFIG_NVME_MULTIPATH
Christoph Hellwigf3334442018-09-11 09:51:29 +0200421 struct bio_list requeue_list;
422 spinlock_t requeue_lock;
423 struct work_struct requeue_work;
424 struct mutex lock;
Anton Eidelmand8a22f82020-06-24 01:53:11 -0700425 unsigned long flags;
426#define NVME_NSHEAD_DISK_LIVE 0
Christoph Hellwigf3334442018-09-11 09:51:29 +0200427 struct nvme_ns __rcu *current_path[];
428#endif
Christoph Hellwiged754e52017-11-09 13:50:43 +0100429};
430
Minwoo Im30897382021-04-07 17:49:29 +0200431static inline bool nvme_ns_head_multipath(struct nvme_ns_head *head)
432{
433 return IS_ENABLED(CONFIG_NVME_MULTIPATH) && head->disk;
434}
435
Max Gurtovoyffc89b12020-05-19 17:05:49 +0300436enum nvme_ns_features {
437 NVME_NS_EXT_LBAS = 1 << 0, /* support extended LBA format */
Max Gurtovoyb29f8482020-05-19 17:05:50 +0300438 NVME_NS_METADATA_SUPPORTED = 1 << 1, /* support getting generated md */
Max Gurtovoyffc89b12020-05-19 17:05:49 +0300439};
440
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +0200441struct nvme_ns {
442 struct list_head list;
443
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100444 struct nvme_ctrl *ctrl;
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +0200445 struct request_queue *queue;
446 struct gendisk *disk;
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200447#ifdef CONFIG_NVME_MULTIPATH
448 enum nvme_ana_state ana_state;
449 u32 ana_grpid;
450#endif
Christoph Hellwiged754e52017-11-09 13:50:43 +0100451 struct list_head siblings;
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200452 struct nvm_dev *ndev;
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +0200453 struct kref kref;
Christoph Hellwiged754e52017-11-09 13:50:43 +0100454 struct nvme_ns_head *head;
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +0200455
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +0200456 int lba_shift;
457 u16 ms;
Jens Axboef5d11842017-06-27 12:03:06 -0600458 u16 sgs;
459 u32 sws;
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +0200460 u8 pi_type;
Keith Busch240e6ee2020-06-29 12:06:41 -0700461#ifdef CONFIG_BLK_DEV_ZONED
462 u64 zsze;
463#endif
Max Gurtovoyffc89b12020-05-19 17:05:49 +0300464 unsigned long features;
Keith Busch646017a2016-02-24 09:15:54 -0700465 unsigned long flags;
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200466#define NVME_NS_REMOVING 0
467#define NVME_NS_DEAD 1
468#define NVME_NS_ANA_PENDING 2
Javier González2f4c9ba2020-12-01 13:02:21 +0100469#define NVME_NS_FORCE_RO 3
Thomas Taib9e03852018-02-08 13:38:29 -0500470
Minwoo Im2637bae2021-04-21 16:45:04 +0900471 struct cdev cdev;
472 struct device cdev_device;
473
Thomas Taib9e03852018-02-08 13:38:29 -0500474 struct nvme_fault_inject fault_inject;
Thomas Taib9e03852018-02-08 13:38:29 -0500475
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +0200476};
477
James Smart4d2ce682020-05-19 17:05:51 +0300478/* NVMe ns supports metadata actions by the controller (generate/strip) */
479static inline bool nvme_ns_has_pi(struct nvme_ns *ns)
480{
481 return ns->pi_type && ns->ms == sizeof(struct t10_pi_tuple);
482}
483
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100484struct nvme_ctrl_ops {
Ming Lin1a353d82016-06-13 16:45:24 +0200485 const char *name;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800486 struct module *module;
Christoph Hellwigd3d5b872017-05-20 15:14:44 +0200487 unsigned int flags;
488#define NVME_F_FABRICS (1 << 0)
Christoph Hellwigc81bfba2017-05-20 15:14:45 +0200489#define NVME_F_METADATA_SUPPORTED (1 << 1)
Logan Gunthorpee0596ab2018-10-04 15:27:44 -0600490#define NVME_F_PCI_P2PDMA (1 << 2)
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100491 int (*reg_read32)(struct nvme_ctrl *ctrl, u32 off, u32 *val);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100492 int (*reg_write32)(struct nvme_ctrl *ctrl, u32 off, u32 val);
Christoph Hellwig7fd89302015-11-28 15:37:52 +0100493 int (*reg_read64)(struct nvme_ctrl *ctrl, u32 off, u64 *val);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100494 void (*free_ctrl)(struct nvme_ctrl *ctrl);
Keith Buschad22c352017-11-07 15:13:12 -0700495 void (*submit_async_event)(struct nvme_ctrl *ctrl);
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200496 void (*delete_ctrl)(struct nvme_ctrl *ctrl);
Ming Lin1a353d82016-06-13 16:45:24 +0200497 int (*get_address)(struct nvme_ctrl *ctrl, char *buf, int size);
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +0200498};
499
Thomas Taib9e03852018-02-08 13:38:29 -0500500#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
Akinobu Mitaa3646452019-06-20 08:49:02 +0200501void nvme_fault_inject_init(struct nvme_fault_inject *fault_inj,
502 const char *dev_name);
503void nvme_fault_inject_fini(struct nvme_fault_inject *fault_inject);
Thomas Taib9e03852018-02-08 13:38:29 -0500504void nvme_should_fail(struct request *req);
505#else
Akinobu Mitaa3646452019-06-20 08:49:02 +0200506static inline void nvme_fault_inject_init(struct nvme_fault_inject *fault_inj,
507 const char *dev_name)
508{
509}
510static inline void nvme_fault_inject_fini(struct nvme_fault_inject *fault_inj)
511{
512}
Thomas Taib9e03852018-02-08 13:38:29 -0500513static inline void nvme_should_fail(struct request *req) {}
514#endif
515
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +0100516static inline int nvme_reset_subsystem(struct nvme_ctrl *ctrl)
517{
518 if (!ctrl->subsystem)
519 return -ENOTTY;
520 return ctrl->ops->reg_write32(ctrl, NVME_REG_NSSR, 0x4E564D65);
521}
522
Damien Le Moal314d48d2019-10-21 12:40:03 +0900523/*
524 * Convert a 512B sector number to a device logical block number.
525 */
526static inline u64 nvme_sect_to_lba(struct nvme_ns *ns, sector_t sector)
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +0200527{
Damien Le Moal314d48d2019-10-21 12:40:03 +0900528 return sector >> (ns->lba_shift - SECTOR_SHIFT);
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +0200529}
530
Damien Le Moale08f2ae2019-10-21 12:40:04 +0900531/*
532 * Convert a device logical block number to a 512B sector number.
533 */
534static inline sector_t nvme_lba_to_sect(struct nvme_ns *ns, u64 lba)
535{
536 return lba << (ns->lba_shift - SECTOR_SHIFT);
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +0200537}
538
Keith Busch71fb90e2020-04-03 09:24:01 -0700539/*
540 * Convert byte length to nvme's 0-based num dwords
541 */
542static inline u32 nvme_bytes_to_numd(size_t len)
543{
544 return (len >> 2) - 1;
545}
546
Christoph Hellwig5ddaabe2020-08-18 09:11:30 +0200547static inline bool nvme_is_ana_error(u16 status)
548{
549 switch (status & 0x7ff) {
550 case NVME_SC_ANA_TRANSITION:
551 case NVME_SC_ANA_INACCESSIBLE:
552 case NVME_SC_ANA_PERSISTENT_LOSS:
553 return true;
554 default:
555 return false;
556 }
557}
558
559static inline bool nvme_is_path_error(u16 status)
560{
Christoph Hellwig1e41f3b2020-08-18 09:11:31 +0200561 /* check for a status code type of 'path related status' */
562 return (status & 0x700) == 0x300;
Christoph Hellwig5ddaabe2020-08-18 09:11:30 +0200563}
564
Christoph Hellwig2eb81a32020-08-18 09:11:29 +0200565/*
566 * Fill in the status and result information from the CQE, and then figure out
567 * if blk-mq will need to use IPI magic to complete the request, and if yes do
568 * so. If not let the caller complete the request without an indirect function
569 * call.
570 */
571static inline bool nvme_try_complete_req(struct request *req, __le16 status,
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200572 union nvme_result result)
Christoph Hellwig15a190f72015-10-16 07:58:39 +0200573{
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200574 struct nvme_request *rq = nvme_req(req);
575
576 rq->status = le16_to_cpu(status) >> 1;
577 rq->result = result;
Thomas Taib9e03852018-02-08 13:38:29 -0500578 /* inject error when permitted by fault injection framework */
579 nvme_should_fail(req);
Christoph Hellwigff029452020-06-11 08:44:52 +0200580 if (unlikely(blk_should_fake_timeout(req->q)))
581 return true;
582 return blk_mq_complete_request_remote(req);
Christoph Hellwig15a190f72015-10-16 07:58:39 +0200583}
584
Christoph Hellwigd22524a2017-10-18 13:25:42 +0200585static inline void nvme_get_ctrl(struct nvme_ctrl *ctrl)
586{
587 get_device(ctrl->device);
588}
589
590static inline void nvme_put_ctrl(struct nvme_ctrl *ctrl)
591{
592 put_device(ctrl->device);
593}
594
Israel Rukshin58a8df62019-10-13 19:57:31 +0300595static inline bool nvme_is_aen_req(u16 qid, __u16 command_id)
596{
597 return !qid && command_id >= NVME_AQ_BLK_MQ_DEPTH;
598}
599
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200600void nvme_complete_rq(struct request *req);
Chao Lengdda32482021-02-04 08:55:11 +0100601blk_status_t nvme_host_path_error(struct request *req);
Jens Axboe7baa8572018-11-08 10:24:07 -0700602bool nvme_cancel_request(struct request *req, void *data, bool reserved);
Chao Leng25479062021-01-21 11:32:36 +0800603void nvme_cancel_tagset(struct nvme_ctrl *ctrl);
604void nvme_cancel_admin_tagset(struct nvme_ctrl *ctrl);
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200605bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
606 enum nvme_ctrl_state new_state);
Keith Buschc1ac9a4b2019-09-04 10:06:11 -0600607bool nvme_wait_reset(struct nvme_ctrl *ctrl);
Sagi Grimbergb5b05042019-07-22 17:06:54 -0700608int nvme_disable_ctrl(struct nvme_ctrl *ctrl);
Sagi Grimbergc0f2f452019-07-22 17:06:53 -0700609int nvme_enable_ctrl(struct nvme_ctrl *ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100610int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +0100611int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
612 const struct nvme_ctrl_ops *ops, unsigned long quirks);
Keith Busch53029b02015-11-28 15:41:02 +0100613void nvme_uninit_ctrl(struct nvme_ctrl *ctrl);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +0300614void nvme_start_ctrl(struct nvme_ctrl *ctrl);
615void nvme_stop_ctrl(struct nvme_ctrl *ctrl);
Chaitanya Kulkarnif21c47692021-02-28 18:06:04 -0800616int nvme_init_ctrl_finish(struct nvme_ctrl *ctrl);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100617
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100618void nvme_remove_namespaces(struct nvme_ctrl *ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100619
Christoph Hellwig4f1244c2017-02-17 13:59:39 +0100620int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
621 bool send);
Scott Bauera98e58e52017-02-03 12:50:32 -0700622
Christoph Hellwig7bf58532016-11-10 07:32:34 -0800623void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
Christoph Hellwig287a63e2018-05-17 18:31:46 +0200624 volatile union nvme_result *res);
Christoph Hellwigf866fc422016-04-26 13:52:00 +0200625
Keith Busch25646262016-01-04 09:10:57 -0700626void nvme_stop_queues(struct nvme_ctrl *ctrl);
627void nvme_start_queues(struct nvme_ctrl *ctrl);
Keith Busch69d9a992016-02-24 09:15:56 -0700628void nvme_kill_queues(struct nvme_ctrl *ctrl);
Keith Buschd6135c3a2019-05-14 14:46:09 -0600629void nvme_sync_queues(struct nvme_ctrl *ctrl);
Chao Leng04800fb2020-10-22 10:15:00 +0800630void nvme_sync_io_queues(struct nvme_ctrl *ctrl);
Keith Busch302ad8c2017-03-01 14:22:12 -0500631void nvme_unfreeze(struct nvme_ctrl *ctrl);
632void nvme_wait_freeze(struct nvme_ctrl *ctrl);
Sagi Grimberg7cf0d7c2020-07-30 13:24:45 -0700633int nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout);
Keith Busch302ad8c2017-03-01 14:22:12 -0500634void nvme_start_freeze(struct nvme_ctrl *ctrl);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +0100635
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200636#define NVME_QID_ANY -1
Christoph Hellwig41609822015-11-20 09:00:02 +0100637struct request *nvme_alloc_request(struct request_queue *q,
Chaitanya Kulkarni39dfe842020-11-09 18:24:00 -0800638 struct nvme_command *cmd, blk_mq_req_flags_t flags);
Max Gurtovoyf7f1fc32018-07-30 00:15:33 +0300639void nvme_cleanup_cmd(struct request *req);
Keith Buschf4b9e6c2021-03-17 13:37:03 -0700640blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req);
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +0200641int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
642 void *buf, unsigned bufflen);
643int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800644 union nvme_result *result, void *buffer, unsigned bufflen,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800645 unsigned timeout, int qid, int at_head,
Sagi Grimberg6287b512018-12-14 11:06:07 -0800646 blk_mq_req_flags_t flags, bool poll);
Keith Busch1a87ee62019-05-27 01:29:01 +0900647int nvme_set_features(struct nvme_ctrl *dev, unsigned int fid,
648 unsigned int dword11, void *buffer, size_t buflen,
649 u32 *result);
650int nvme_get_features(struct nvme_ctrl *dev, unsigned int fid,
651 unsigned int dword11, void *buffer, size_t buflen,
652 u32 *result);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100653int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200654void nvme_stop_keep_alive(struct nvme_ctrl *ctrl);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +0200655int nvme_reset_ctrl(struct nvme_ctrl *ctrl);
Christoph Hellwig24052522021-04-10 08:42:03 +0200656int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl);
Keith Buschc1ac9a4b2019-09-04 10:06:11 -0600657int nvme_try_sched_reset(struct nvme_ctrl *ctrl);
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200658int nvme_delete_ctrl(struct nvme_ctrl *ctrl);
Christoph Hellwig24052522021-04-10 08:42:03 +0200659void nvme_queue_scan(struct nvme_ctrl *ctrl);
Keith Buschbe93e872020-06-29 12:06:40 -0700660int nvme_get_log(struct nvme_ctrl *ctrl, u32 nsid, u8 log_page, u8 lsp, u8 csi,
Christoph Hellwig0e987192018-06-06 14:39:00 +0200661 void *log, size_t size, u64 offset);
Keith Busch240e6ee2020-06-29 12:06:41 -0700662struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk,
663 struct nvme_ns_head **head, int *srcu_idx);
664void nvme_put_ns_from_disk(struct nvme_ns_head *head, int idx);
Christoph Hellwig1496bd42021-04-07 14:22:12 +0200665bool nvme_tryget_ns_head(struct nvme_ns_head *head);
666void nvme_put_ns_head(struct nvme_ns_head *head);
Christoph Hellwig24052522021-04-10 08:42:03 +0200667struct nvme_ctrl *nvme_find_get_live_ctrl(struct nvme_subsystem *subsys);
Minwoo Im2637bae2021-04-21 16:45:04 +0900668int nvme_cdev_add(struct cdev *cdev, struct device *cdev_device,
669 const struct file_operations *fops, struct module *owner);
670void nvme_cdev_del(struct cdev *cdev, struct device *cdev_device);
Christoph Hellwig24052522021-04-10 08:42:03 +0200671int nvme_ioctl(struct block_device *bdev, fmode_t mode,
672 unsigned int cmd, unsigned long arg);
Minwoo Im2637bae2021-04-21 16:45:04 +0900673long nvme_ns_chr_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Christoph Hellwig24052522021-04-10 08:42:03 +0200674int nvme_ns_head_ioctl(struct block_device *bdev, fmode_t mode,
675 unsigned int cmd, unsigned long arg);
Minwoo Im2637bae2021-04-21 16:45:04 +0900676long nvme_ns_head_chr_ioctl(struct file *file, unsigned int cmd,
677 unsigned long arg);
Christoph Hellwig24052522021-04-10 08:42:03 +0200678long nvme_dev_ioctl(struct file *file, unsigned int cmd,
679 unsigned long arg);
Christoph Hellwig1496bd42021-04-07 14:22:12 +0200680int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo);
Matias Bjørlingd558fb52018-03-21 20:27:07 +0100681
Hannes Reinecke33b14f672018-09-28 08:17:20 +0200682extern const struct attribute_group *nvme_ns_id_attr_groups[];
Christoph Hellwig1496bd42021-04-07 14:22:12 +0200683extern const struct pr_ops nvme_pr_ops;
Christoph Hellwig32acab32017-11-02 12:59:30 +0100684extern const struct block_device_operations nvme_ns_head_ops;
685
686#ifdef CONFIG_NVME_MULTIPATH
Marta Rybczynska66b20ac2019-07-23 07:41:20 +0200687static inline bool nvme_ctrl_use_ana(struct nvme_ctrl *ctrl)
688{
689 return ctrl->ana_log_buf != NULL;
690}
691
Sagi Grimbergb9156da2019-07-31 11:00:26 -0700692void nvme_mpath_unfreeze(struct nvme_subsystem *subsys);
693void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys);
694void nvme_mpath_start_freeze(struct nvme_subsystem *subsys);
Christoph Hellwig9953ab02021-04-07 12:46:46 +0200695bool nvme_mpath_set_disk_name(struct nvme_ns *ns, char *disk_name, int *flags);
Christoph Hellwig5ddaabe2020-08-18 09:11:30 +0200696void nvme_failover_req(struct request *req);
Christoph Hellwig32acab32017-11-02 12:59:30 +0100697void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl);
698int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl,struct nvme_ns_head *head);
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200699void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id);
Christoph Hellwig32acab32017-11-02 12:59:30 +0100700void nvme_mpath_remove_disk(struct nvme_ns_head *head);
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200701int nvme_mpath_init(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id);
702void nvme_mpath_uninit(struct nvme_ctrl *ctrl);
703void nvme_mpath_stop(struct nvme_ctrl *ctrl);
Sagi Grimberg0157ec82019-07-25 11:56:57 -0700704bool nvme_mpath_clear_current_path(struct nvme_ns *ns);
705void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl);
Christoph Hellwig32acab32017-11-02 12:59:30 +0100706struct nvme_ns *nvme_find_path(struct nvme_ns_head *head);
Sagi Grimberg479a3222017-12-21 15:07:27 +0200707
708static inline void nvme_mpath_check_last_path(struct nvme_ns *ns)
709{
710 struct nvme_ns_head *head = ns->head;
711
712 if (head->disk && list_empty(&head->list))
713 kblockd_schedule_work(&head->requeue_work);
714}
715
Max Gurtovoy2b597872021-01-05 10:34:02 +0000716static inline void nvme_trace_bio_complete(struct request *req)
Hannes Reinecke35fe0d12019-07-24 15:47:55 +0200717{
718 struct nvme_ns *ns = req->q->queuedata;
719
720 if (req->cmd_flags & REQ_NVME_MPATH)
Christoph Hellwigd24de76a2020-06-03 07:14:43 +0200721 trace_block_bio_complete(ns->head->disk->queue, req->bio);
Hannes Reinecke35fe0d12019-07-24 15:47:55 +0200722}
723
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200724extern struct device_attribute dev_attr_ana_grpid;
725extern struct device_attribute dev_attr_ana_state;
Hannes Reinecke75c10e72019-02-18 11:43:26 +0100726extern struct device_attribute subsys_attr_iopolicy;
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200727
Christoph Hellwig32acab32017-11-02 12:59:30 +0100728#else
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200729static inline bool nvme_ctrl_use_ana(struct nvme_ctrl *ctrl)
730{
731 return false;
732}
Christoph Hellwig9953ab02021-04-07 12:46:46 +0200733static inline bool nvme_mpath_set_disk_name(struct nvme_ns *ns, char *disk_name,
734 int *flags)
Keith Buscha785dbc2018-04-26 14:22:41 -0600735{
Christoph Hellwig9953ab02021-04-07 12:46:46 +0200736 return false;
Keith Buscha785dbc2018-04-26 14:22:41 -0600737}
Christoph Hellwig5ddaabe2020-08-18 09:11:30 +0200738static inline void nvme_failover_req(struct request *req)
Christoph Hellwig32acab32017-11-02 12:59:30 +0100739{
740}
Christoph Hellwig32acab32017-11-02 12:59:30 +0100741static inline void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
742{
743}
744static inline int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl,
745 struct nvme_ns_head *head)
746{
747 return 0;
748}
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200749static inline void nvme_mpath_add_disk(struct nvme_ns *ns,
750 struct nvme_id_ns *id)
Christoph Hellwig32acab32017-11-02 12:59:30 +0100751{
752}
753static inline void nvme_mpath_remove_disk(struct nvme_ns_head *head)
754{
755}
Sagi Grimberg0157ec82019-07-25 11:56:57 -0700756static inline bool nvme_mpath_clear_current_path(struct nvme_ns *ns)
757{
758 return false;
759}
760static inline void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl)
Christoph Hellwig32acab32017-11-02 12:59:30 +0100761{
762}
Sagi Grimberg479a3222017-12-21 15:07:27 +0200763static inline void nvme_mpath_check_last_path(struct nvme_ns *ns)
764{
765}
Max Gurtovoy2b597872021-01-05 10:34:02 +0000766static inline void nvme_trace_bio_complete(struct request *req)
Hannes Reinecke35fe0d12019-07-24 15:47:55 +0200767{
768}
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200769static inline int nvme_mpath_init(struct nvme_ctrl *ctrl,
770 struct nvme_id_ctrl *id)
771{
Kanchan Joshi2bd64302021-03-09 00:48:03 +0530772 if (ctrl->subsys->cmic & NVME_CTRL_CMIC_ANA)
Christoph Hellwig14a13362018-11-20 16:57:54 +0100773 dev_warn(ctrl->device,
774"Please enable CONFIG_NVME_MULTIPATH for full support of multi-port devices.\n");
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200775 return 0;
776}
777static inline void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
778{
779}
780static inline void nvme_mpath_stop(struct nvme_ctrl *ctrl)
781{
782}
Sagi Grimbergb9156da2019-07-31 11:00:26 -0700783static inline void nvme_mpath_unfreeze(struct nvme_subsystem *subsys)
784{
785}
786static inline void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys)
787{
788}
789static inline void nvme_mpath_start_freeze(struct nvme_subsystem *subsys)
790{
791}
Christoph Hellwig32acab32017-11-02 12:59:30 +0100792#endif /* CONFIG_NVME_MULTIPATH */
793
Christoph Hellwig7fad20d2020-08-20 09:31:36 +0200794int nvme_revalidate_zones(struct nvme_ns *ns);
Keith Busch240e6ee2020-06-29 12:06:41 -0700795#ifdef CONFIG_BLK_DEV_ZONED
Christoph Hellwigd525c3c2020-08-20 14:02:18 +0200796int nvme_update_zone_info(struct nvme_ns *ns, unsigned lbaf);
Keith Busch240e6ee2020-06-29 12:06:41 -0700797int nvme_report_zones(struct gendisk *disk, sector_t sector,
798 unsigned int nr_zones, report_zones_cb cb, void *data);
799
800blk_status_t nvme_setup_zone_mgmt_send(struct nvme_ns *ns, struct request *req,
801 struct nvme_command *cmnd,
802 enum nvme_zone_mgmt_action action);
803#else
804#define nvme_report_zones NULL
805
806static inline blk_status_t nvme_setup_zone_mgmt_send(struct nvme_ns *ns,
807 struct request *req, struct nvme_command *cmnd,
808 enum nvme_zone_mgmt_action action)
809{
810 return BLK_STS_NOTSUPP;
811}
812
Christoph Hellwigd525c3c2020-08-20 14:02:18 +0200813static inline int nvme_update_zone_info(struct nvme_ns *ns, unsigned lbaf)
Keith Busch240e6ee2020-06-29 12:06:41 -0700814{
815 dev_warn(ns->ctrl->device,
816 "Please enable CONFIG_BLK_DEV_ZONED to support ZNS devices\n");
817 return -EPROTONOSUPPORT;
818}
819#endif
820
Keith Buschc4699e72015-11-28 16:49:22 +0100821#ifdef CONFIG_NVM
Matias Bjørling3dc87dd2016-11-28 22:38:53 +0100822int nvme_nvm_register(struct nvme_ns *ns, char *disk_name, int node);
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200823void nvme_nvm_unregister(struct nvme_ns *ns);
Hannes Reinecke33b14f672018-09-28 08:17:20 +0200824extern const struct attribute_group nvme_nvm_attr_group;
Christoph Hellwigd7790d32020-08-14 10:33:14 +0200825int nvme_nvm_ioctl(struct nvme_ns *ns, unsigned int cmd, void __user *argp);
Keith Buschc4699e72015-11-28 16:49:22 +0100826#else
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200827static inline int nvme_nvm_register(struct nvme_ns *ns, char *disk_name,
Matias Bjørling3dc87dd2016-11-28 22:38:53 +0100828 int node)
Keith Buschc4699e72015-11-28 16:49:22 +0100829{
830 return 0;
831}
832
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200833static inline void nvme_nvm_unregister(struct nvme_ns *ns) {};
Matias Bjørling84d4add2017-01-31 13:17:16 +0100834static inline int nvme_nvm_ioctl(struct nvme_ns *ns, unsigned int cmd,
Christoph Hellwigd7790d32020-08-14 10:33:14 +0200835 void __user *argp)
Matias Bjørling84d4add2017-01-31 13:17:16 +0100836{
837 return -ENOTTY;
838}
Matias Bjørling3dc87dd2016-11-28 22:38:53 +0100839#endif /* CONFIG_NVM */
840
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200841static inline struct nvme_ns *nvme_get_ns_from_dev(struct device *dev)
842{
843 return dev_to_disk(dev)->private_data;
844}
Matias Bjørlingca064082015-10-29 17:57:29 +0900845
Guenter Roeck400b6a72019-11-06 06:35:18 -0800846#ifdef CONFIG_NVME_HWMON
Keith Busch59e330f2020-09-17 08:50:25 -0700847int nvme_hwmon_init(struct nvme_ctrl *ctrl);
Hannes Reineckeed7770f2021-01-19 07:43:18 +0100848void nvme_hwmon_exit(struct nvme_ctrl *ctrl);
Guenter Roeck400b6a72019-11-06 06:35:18 -0800849#else
Keith Busch59e330f2020-09-17 08:50:25 -0700850static inline int nvme_hwmon_init(struct nvme_ctrl *ctrl)
851{
852 return 0;
853}
Hannes Reineckeed7770f2021-01-19 07:43:18 +0100854
855static inline void nvme_hwmon_exit(struct nvme_ctrl *ctrl)
856{
857}
Guenter Roeck400b6a72019-11-06 06:35:18 -0800858#endif
859
Logan Gunthorpedf21b6b2020-07-24 11:25:13 -0600860u32 nvme_command_effects(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
861 u8 opcode);
Logan Gunthorpe17365ae2020-07-24 11:25:14 -0600862void nvme_execute_passthru_rq(struct request *rq);
Chaitanya Kulkarnib2702aa2020-09-16 18:11:02 -0700863struct nvme_ctrl *nvme_ctrl_from_file(struct file *file);
Logan Gunthorpe24493b82020-07-24 11:25:16 -0600864struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid);
865void nvme_put_ns(struct nvme_ns *ns);
Logan Gunthorpedf21b6b2020-07-24 11:25:13 -0600866
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +0200867#endif /* _NVME_H */