157dacad5SJay Sternberg /* 257dacad5SJay Sternberg * Copyright (c) 2011-2014, Intel Corporation. 357dacad5SJay Sternberg * 457dacad5SJay Sternberg * This program is free software; you can redistribute it and/or modify it 557dacad5SJay Sternberg * under the terms and conditions of the GNU General Public License, 657dacad5SJay Sternberg * version 2, as published by the Free Software Foundation. 757dacad5SJay Sternberg * 857dacad5SJay Sternberg * This program is distributed in the hope it will be useful, but WITHOUT 957dacad5SJay Sternberg * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1057dacad5SJay Sternberg * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 1157dacad5SJay Sternberg * more details. 1257dacad5SJay Sternberg */ 1357dacad5SJay Sternberg 1457dacad5SJay Sternberg #ifndef _NVME_H 1557dacad5SJay Sternberg #define _NVME_H 1657dacad5SJay Sternberg 1757dacad5SJay Sternberg #include <linux/nvme.h> 18a6a5149bSChristoph Hellwig #include <linux/cdev.h> 1957dacad5SJay Sternberg #include <linux/pci.h> 2057dacad5SJay Sternberg #include <linux/kref.h> 2157dacad5SJay Sternberg #include <linux/blk-mq.h> 22b0b4e09cSMatias Bjørling #include <linux/lightnvm.h> 23a98e58e5SScott Bauer #include <linux/sed-opal.h> 24b9e03857SThomas Tai #include <linux/fault-inject.h> 2557dacad5SJay Sternberg 268ae4e447SMarc Olson extern unsigned int nvme_io_timeout; 2757dacad5SJay Sternberg #define NVME_IO_TIMEOUT (nvme_io_timeout * HZ) 2857dacad5SJay Sternberg 298ae4e447SMarc Olson extern unsigned int admin_timeout; 3021d34711SChristoph Hellwig #define ADMIN_TIMEOUT (admin_timeout * HZ) 3121d34711SChristoph Hellwig 32038bd4cbSSagi Grimberg #define NVME_DEFAULT_KATO 5 33038bd4cbSSagi Grimberg #define NVME_KATO_GRACE 10 34038bd4cbSSagi Grimberg 359a6327d2SSagi Grimberg extern struct workqueue_struct *nvme_wq; 36b227c59bSRoy Shterman extern struct workqueue_struct *nvme_reset_wq; 37b227c59bSRoy Shterman extern struct workqueue_struct *nvme_delete_wq; 389a6327d2SSagi Grimberg 39ca064085SMatias Bjørling enum { 40ca064085SMatias Bjørling NVME_NS_LBA = 0, 41ca064085SMatias Bjørling NVME_NS_LIGHTNVM = 1, 42ca064085SMatias Bjørling }; 43ca064085SMatias Bjørling 4457dacad5SJay Sternberg /* 45106198edSChristoph Hellwig * List of workarounds for devices that required behavior not specified in 46106198edSChristoph Hellwig * the standard. 4757dacad5SJay Sternberg */ 48106198edSChristoph Hellwig enum nvme_quirks { 49106198edSChristoph Hellwig /* 50106198edSChristoph Hellwig * Prefers I/O aligned to a stripe size specified in a vendor 51106198edSChristoph Hellwig * specific Identify field. 52106198edSChristoph Hellwig */ 53106198edSChristoph Hellwig NVME_QUIRK_STRIPE_SIZE = (1 << 0), 54540c801cSKeith Busch 55540c801cSKeith Busch /* 56540c801cSKeith Busch * The controller doesn't handle Identify value others than 0 or 1 57540c801cSKeith Busch * correctly. 58540c801cSKeith Busch */ 59540c801cSKeith Busch NVME_QUIRK_IDENTIFY_CNS = (1 << 1), 6008095e70SKeith Busch 6108095e70SKeith Busch /* 62e850fd16SChristoph Hellwig * The controller deterministically returns O's on reads to 63e850fd16SChristoph Hellwig * logical blocks that deallocate was called on. 6408095e70SKeith Busch */ 65e850fd16SChristoph Hellwig NVME_QUIRK_DEALLOCATE_ZEROES = (1 << 2), 6654adc010SGuilherme G. Piccoli 6754adc010SGuilherme G. Piccoli /* 6854adc010SGuilherme G. Piccoli * The controller needs a delay before starts checking the device 6954adc010SGuilherme G. Piccoli * readiness, which is done by reading the NVME_CSTS_RDY bit. 7054adc010SGuilherme G. Piccoli */ 7154adc010SGuilherme G. Piccoli NVME_QUIRK_DELAY_BEFORE_CHK_RDY = (1 << 3), 72c5552fdeSAndy Lutomirski 73c5552fdeSAndy Lutomirski /* 74c5552fdeSAndy Lutomirski * APST should not be used. 75c5552fdeSAndy Lutomirski */ 76c5552fdeSAndy Lutomirski NVME_QUIRK_NO_APST = (1 << 4), 77ff5350a8SAndy Lutomirski 78ff5350a8SAndy Lutomirski /* 79ff5350a8SAndy Lutomirski * The deepest sleep state should not be used. 80ff5350a8SAndy Lutomirski */ 81ff5350a8SAndy Lutomirski NVME_QUIRK_NO_DEEPEST_PS = (1 << 5), 82608cc4b1SChristoph Hellwig 83608cc4b1SChristoph Hellwig /* 84608cc4b1SChristoph Hellwig * Supports the LighNVM command set if indicated in vs[1]. 85608cc4b1SChristoph Hellwig */ 86608cc4b1SChristoph Hellwig NVME_QUIRK_LIGHTNVM = (1 << 6), 87106198edSChristoph Hellwig }; 88106198edSChristoph Hellwig 89d49187e9SChristoph Hellwig /* 90d49187e9SChristoph Hellwig * Common request structure for NVMe passthrough. All drivers must have 91d49187e9SChristoph Hellwig * this structure as the first member of their request-private data. 92d49187e9SChristoph Hellwig */ 93d49187e9SChristoph Hellwig struct nvme_request { 94d49187e9SChristoph Hellwig struct nvme_command *cmd; 95d49187e9SChristoph Hellwig union nvme_result result; 9644e44b29SChristoph Hellwig u8 retries; 9727fa9bc5SChristoph Hellwig u8 flags; 9827fa9bc5SChristoph Hellwig u16 status; 9927fa9bc5SChristoph Hellwig }; 10027fa9bc5SChristoph Hellwig 10132acab31SChristoph Hellwig /* 10232acab31SChristoph Hellwig * Mark a bio as coming in through the mpath node. 10332acab31SChristoph Hellwig */ 10432acab31SChristoph Hellwig #define REQ_NVME_MPATH REQ_DRV 10532acab31SChristoph Hellwig 10627fa9bc5SChristoph Hellwig enum { 10727fa9bc5SChristoph Hellwig NVME_REQ_CANCELLED = (1 << 0), 108bb06ec31SJames Smart NVME_REQ_USERCMD = (1 << 1), 109d49187e9SChristoph Hellwig }; 110d49187e9SChristoph Hellwig 111d49187e9SChristoph Hellwig static inline struct nvme_request *nvme_req(struct request *req) 112d49187e9SChristoph Hellwig { 113d49187e9SChristoph Hellwig return blk_mq_rq_to_pdu(req); 114d49187e9SChristoph Hellwig } 115d49187e9SChristoph Hellwig 11654adc010SGuilherme G. Piccoli /* The below value is the specific amount of delay needed before checking 11754adc010SGuilherme G. Piccoli * readiness in case of the PCI_DEVICE(0x1c58, 0x0003), which needs the 11854adc010SGuilherme G. Piccoli * NVME_QUIRK_DELAY_BEFORE_CHK_RDY quirk enabled. The value (in ms) was 11954adc010SGuilherme G. Piccoli * found empirically. 12054adc010SGuilherme G. Piccoli */ 1218c97eeccSJeff Lien #define NVME_QUIRK_DELAY_AMOUNT 2300 12254adc010SGuilherme G. Piccoli 123bb8d261eSChristoph Hellwig enum nvme_ctrl_state { 124bb8d261eSChristoph Hellwig NVME_CTRL_NEW, 125bb8d261eSChristoph Hellwig NVME_CTRL_LIVE, 1262b1b7e78SJianchao Wang NVME_CTRL_ADMIN_ONLY, /* Only admin queue live */ 127bb8d261eSChristoph Hellwig NVME_CTRL_RESETTING, 128ad6a0a52SMax Gurtovoy NVME_CTRL_CONNECTING, 129bb8d261eSChristoph Hellwig NVME_CTRL_DELETING, 1300ff9d4e1SKeith Busch NVME_CTRL_DEAD, 131bb8d261eSChristoph Hellwig }; 132bb8d261eSChristoph Hellwig 1331c63dc66SChristoph Hellwig struct nvme_ctrl { 134bb8d261eSChristoph Hellwig enum nvme_ctrl_state state; 135bd4da3abSAndy Lutomirski bool identified; 136bb8d261eSChristoph Hellwig spinlock_t lock; 1371c63dc66SChristoph Hellwig const struct nvme_ctrl_ops *ops; 13857dacad5SJay Sternberg struct request_queue *admin_q; 13907bfcd09SChristoph Hellwig struct request_queue *connect_q; 14057dacad5SJay Sternberg struct device *dev; 14157dacad5SJay Sternberg int instance; 1425bae7f73SChristoph Hellwig struct blk_mq_tag_set *tagset; 14334b6c231SSagi Grimberg struct blk_mq_tag_set *admin_tagset; 1445bae7f73SChristoph Hellwig struct list_head namespaces; 145765cc031SJianchao Wang struct rw_semaphore namespaces_rwsem; 146d22524a4SChristoph Hellwig struct device ctrl_device; 1475bae7f73SChristoph Hellwig struct device *device; /* char device */ 148a6a5149bSChristoph Hellwig struct cdev cdev; 149d86c4d8eSChristoph Hellwig struct work_struct reset_work; 150c5017e85SChristoph Hellwig struct work_struct delete_work; 1511c63dc66SChristoph Hellwig 152ab9e00ccSChristoph Hellwig struct nvme_subsystem *subsys; 153ab9e00ccSChristoph Hellwig struct list_head subsys_entry; 154ab9e00ccSChristoph Hellwig 1554f1244c8SChristoph Hellwig struct opal_dev *opal_dev; 156a98e58e5SScott Bauer 15757dacad5SJay Sternberg char name[12]; 15876e3914aSChristoph Hellwig u16 cntlid; 1595fd4ce1bSChristoph Hellwig 1605fd4ce1bSChristoph Hellwig u32 ctrl_config; 161b6dccf7fSArnav Dawn u16 mtfa; 162d858e5f0SSagi Grimberg u32 queue_count; 1635fd4ce1bSChristoph Hellwig 16420d0dfe6SSagi Grimberg u64 cap; 1655fd4ce1bSChristoph Hellwig u32 page_size; 16657dacad5SJay Sternberg u32 max_hw_sectors; 16757dacad5SJay Sternberg u16 oncs; 1688a9ae523SScott Bauer u16 oacs; 169f5d11840SJens Axboe u16 nssa; 170f5d11840SJens Axboe u16 nr_streams; 1716bf25d16SChristoph Hellwig atomic_t abort_limit; 17257dacad5SJay Sternberg u8 vwc; 173f3ca80fcSChristoph Hellwig u32 vs; 17407bfcd09SChristoph Hellwig u32 sgls; 175038bd4cbSSagi Grimberg u16 kas; 176c5552fdeSAndy Lutomirski u8 npss; 177c5552fdeSAndy Lutomirski u8 apsta; 178e3d7874dSKeith Busch u32 aen_result; 17907fbd32aSMartin K. Petersen unsigned int shutdown_timeout; 180038bd4cbSSagi Grimberg unsigned int kato; 181f3ca80fcSChristoph Hellwig bool subsystem; 182106198edSChristoph Hellwig unsigned long quirks; 183c5552fdeSAndy Lutomirski struct nvme_id_power_state psd[32]; 18484fef62dSKeith Busch struct nvme_effects_log *effects; 1855955be21SChristoph Hellwig struct work_struct scan_work; 186f866fc42SChristoph Hellwig struct work_struct async_event_work; 187038bd4cbSSagi Grimberg struct delayed_work ka_work; 1880a34e466SRoland Dreier struct nvme_command ka_cmd; 189b6dccf7fSArnav Dawn struct work_struct fw_act_work; 19007bfcd09SChristoph Hellwig 191c5552fdeSAndy Lutomirski /* Power saving configuration */ 192c5552fdeSAndy Lutomirski u64 ps_max_latency_us; 19376a5af84SKai-Heng Feng bool apst_enabled; 194c5552fdeSAndy Lutomirski 195044a9df1SChristoph Hellwig /* PCIe only: */ 196fe6d53c9SChristoph Hellwig u32 hmpre; 197fe6d53c9SChristoph Hellwig u32 hmmin; 198044a9df1SChristoph Hellwig u32 hmminds; 199044a9df1SChristoph Hellwig u16 hmmaxd; 200fe6d53c9SChristoph Hellwig 20107bfcd09SChristoph Hellwig /* Fabrics only */ 20207bfcd09SChristoph Hellwig u16 sqsize; 20307bfcd09SChristoph Hellwig u32 ioccsz; 20407bfcd09SChristoph Hellwig u32 iorcsz; 20507bfcd09SChristoph Hellwig u16 icdoff; 20607bfcd09SChristoph Hellwig u16 maxcmd; 207fdf9dfa8SSagi Grimberg int nr_reconnects; 20807bfcd09SChristoph Hellwig struct nvmf_ctrl_options *opts; 20957dacad5SJay Sternberg }; 21057dacad5SJay Sternberg 211ab9e00ccSChristoph Hellwig struct nvme_subsystem { 212ab9e00ccSChristoph Hellwig int instance; 213ab9e00ccSChristoph Hellwig struct device dev; 214ab9e00ccSChristoph Hellwig /* 215ab9e00ccSChristoph Hellwig * Because we unregister the device on the last put we need 216ab9e00ccSChristoph Hellwig * a separate refcount. 217ab9e00ccSChristoph Hellwig */ 218ab9e00ccSChristoph Hellwig struct kref ref; 219ab9e00ccSChristoph Hellwig struct list_head entry; 220ab9e00ccSChristoph Hellwig struct mutex lock; 221ab9e00ccSChristoph Hellwig struct list_head ctrls; 222ed754e5dSChristoph Hellwig struct list_head nsheads; 223ab9e00ccSChristoph Hellwig char subnqn[NVMF_NQN_SIZE]; 224ab9e00ccSChristoph Hellwig char serial[20]; 225ab9e00ccSChristoph Hellwig char model[40]; 226ab9e00ccSChristoph Hellwig char firmware_rev[8]; 227ab9e00ccSChristoph Hellwig u8 cmic; 228ab9e00ccSChristoph Hellwig u16 vendor_id; 229ed754e5dSChristoph Hellwig struct ida ns_ida; 230ab9e00ccSChristoph Hellwig }; 231ab9e00ccSChristoph Hellwig 232002fab04SChristoph Hellwig /* 233002fab04SChristoph Hellwig * Container structure for uniqueue namespace identifiers. 234002fab04SChristoph Hellwig */ 235002fab04SChristoph Hellwig struct nvme_ns_ids { 236002fab04SChristoph Hellwig u8 eui64[8]; 237002fab04SChristoph Hellwig u8 nguid[16]; 238002fab04SChristoph Hellwig uuid_t uuid; 239002fab04SChristoph Hellwig }; 240002fab04SChristoph Hellwig 241ed754e5dSChristoph Hellwig /* 242ed754e5dSChristoph Hellwig * Anchor structure for namespaces. There is one for each namespace in a 243ed754e5dSChristoph Hellwig * NVMe subsystem that any of our controllers can see, and the namespace 244ed754e5dSChristoph Hellwig * structure for each controller is chained of it. For private namespaces 245ed754e5dSChristoph Hellwig * there is a 1:1 relation to our namespace structures, that is ->list 246ed754e5dSChristoph Hellwig * only ever has a single entry for private namespaces. 247ed754e5dSChristoph Hellwig */ 248ed754e5dSChristoph Hellwig struct nvme_ns_head { 24932acab31SChristoph Hellwig #ifdef CONFIG_NVME_MULTIPATH 25032acab31SChristoph Hellwig struct gendisk *disk; 25132acab31SChristoph Hellwig struct nvme_ns __rcu *current_path; 25232acab31SChristoph Hellwig struct bio_list requeue_list; 25332acab31SChristoph Hellwig spinlock_t requeue_lock; 25432acab31SChristoph Hellwig struct work_struct requeue_work; 25532acab31SChristoph Hellwig #endif 256ed754e5dSChristoph Hellwig struct list_head list; 257ed754e5dSChristoph Hellwig struct srcu_struct srcu; 258ed754e5dSChristoph Hellwig struct nvme_subsystem *subsys; 259ed754e5dSChristoph Hellwig unsigned ns_id; 260ed754e5dSChristoph Hellwig struct nvme_ns_ids ids; 261ed754e5dSChristoph Hellwig struct list_head entry; 262ed754e5dSChristoph Hellwig struct kref ref; 263ed754e5dSChristoph Hellwig int instance; 264ed754e5dSChristoph Hellwig }; 265ed754e5dSChristoph Hellwig 266b9e03857SThomas Tai #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS 267b9e03857SThomas Tai struct nvme_fault_inject { 268b9e03857SThomas Tai struct fault_attr attr; 269b9e03857SThomas Tai struct dentry *parent; 270b9e03857SThomas Tai bool dont_retry; /* DNR, do not retry */ 271b9e03857SThomas Tai u16 status; /* status code */ 272b9e03857SThomas Tai }; 273b9e03857SThomas Tai #endif 274b9e03857SThomas Tai 27557dacad5SJay Sternberg struct nvme_ns { 27657dacad5SJay Sternberg struct list_head list; 27757dacad5SJay Sternberg 2781c63dc66SChristoph Hellwig struct nvme_ctrl *ctrl; 27957dacad5SJay Sternberg struct request_queue *queue; 28057dacad5SJay Sternberg struct gendisk *disk; 281ed754e5dSChristoph Hellwig struct list_head siblings; 282b0b4e09cSMatias Bjørling struct nvm_dev *ndev; 28357dacad5SJay Sternberg struct kref kref; 284ed754e5dSChristoph Hellwig struct nvme_ns_head *head; 28557dacad5SJay Sternberg 28657dacad5SJay Sternberg int lba_shift; 28757dacad5SJay Sternberg u16 ms; 288f5d11840SJens Axboe u16 sgs; 289f5d11840SJens Axboe u32 sws; 29057dacad5SJay Sternberg bool ext; 29157dacad5SJay Sternberg u8 pi_type; 292646017a6SKeith Busch unsigned long flags; 293646017a6SKeith Busch #define NVME_NS_REMOVING 0 29469d9a99cSKeith Busch #define NVME_NS_DEAD 1 29557eeaf8eSChristoph Hellwig u16 noiob; 296b9e03857SThomas Tai 297b9e03857SThomas Tai #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS 298b9e03857SThomas Tai struct nvme_fault_inject fault_inject; 299b9e03857SThomas Tai #endif 300b9e03857SThomas Tai 30157dacad5SJay Sternberg }; 30257dacad5SJay Sternberg 3031c63dc66SChristoph Hellwig struct nvme_ctrl_ops { 3041a353d85SMing Lin const char *name; 305e439bb12SSagi Grimberg struct module *module; 306d3d5b87dSChristoph Hellwig unsigned int flags; 307d3d5b87dSChristoph Hellwig #define NVME_F_FABRICS (1 << 0) 308c81bfba9SChristoph Hellwig #define NVME_F_METADATA_SUPPORTED (1 << 1) 3091c63dc66SChristoph Hellwig int (*reg_read32)(struct nvme_ctrl *ctrl, u32 off, u32 *val); 3105fd4ce1bSChristoph Hellwig int (*reg_write32)(struct nvme_ctrl *ctrl, u32 off, u32 val); 3117fd8930fSChristoph Hellwig int (*reg_read64)(struct nvme_ctrl *ctrl, u32 off, u64 *val); 3121673f1f0SChristoph Hellwig void (*free_ctrl)(struct nvme_ctrl *ctrl); 313ad22c355SKeith Busch void (*submit_async_event)(struct nvme_ctrl *ctrl); 314c5017e85SChristoph Hellwig void (*delete_ctrl)(struct nvme_ctrl *ctrl); 3151a353d85SMing Lin int (*get_address)(struct nvme_ctrl *ctrl, char *buf, int size); 31631b84460SSagi Grimberg int (*reinit_request)(void *data, struct request *rq); 317b435eceaSNitzan Carmi void (*stop_ctrl)(struct nvme_ctrl *ctrl); 31857dacad5SJay Sternberg }; 31957dacad5SJay Sternberg 320b9e03857SThomas Tai #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS 321b9e03857SThomas Tai void nvme_fault_inject_init(struct nvme_ns *ns); 322b9e03857SThomas Tai void nvme_fault_inject_fini(struct nvme_ns *ns); 323b9e03857SThomas Tai void nvme_should_fail(struct request *req); 324b9e03857SThomas Tai #else 325b9e03857SThomas Tai static inline void nvme_fault_inject_init(struct nvme_ns *ns) {} 326b9e03857SThomas Tai static inline void nvme_fault_inject_fini(struct nvme_ns *ns) {} 327b9e03857SThomas Tai static inline void nvme_should_fail(struct request *req) {} 328b9e03857SThomas Tai #endif 329b9e03857SThomas Tai 3301c63dc66SChristoph Hellwig static inline bool nvme_ctrl_ready(struct nvme_ctrl *ctrl) 3311c63dc66SChristoph Hellwig { 3321c63dc66SChristoph Hellwig u32 val = 0; 3331c63dc66SChristoph Hellwig 3341c63dc66SChristoph Hellwig if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &val)) 3351c63dc66SChristoph Hellwig return false; 3361c63dc66SChristoph Hellwig return val & NVME_CSTS_RDY; 3371c63dc66SChristoph Hellwig } 3381c63dc66SChristoph Hellwig 339f3ca80fcSChristoph Hellwig static inline int nvme_reset_subsystem(struct nvme_ctrl *ctrl) 340f3ca80fcSChristoph Hellwig { 341f3ca80fcSChristoph Hellwig if (!ctrl->subsystem) 342f3ca80fcSChristoph Hellwig return -ENOTTY; 343f3ca80fcSChristoph Hellwig return ctrl->ops->reg_write32(ctrl, NVME_REG_NSSR, 0x4E564D65); 344f3ca80fcSChristoph Hellwig } 345f3ca80fcSChristoph Hellwig 34657dacad5SJay Sternberg static inline u64 nvme_block_nr(struct nvme_ns *ns, sector_t sector) 34757dacad5SJay Sternberg { 34857dacad5SJay Sternberg return (sector >> (ns->lba_shift - 9)); 34957dacad5SJay Sternberg } 35057dacad5SJay Sternberg 3516904242dSMing Lin static inline void nvme_cleanup_cmd(struct request *req) 3526904242dSMing Lin { 353f9d03f96SChristoph Hellwig if (req->rq_flags & RQF_SPECIAL_PAYLOAD) { 354f9d03f96SChristoph Hellwig kfree(page_address(req->special_vec.bv_page) + 355f9d03f96SChristoph Hellwig req->special_vec.bv_offset); 356f9d03f96SChristoph Hellwig } 3576904242dSMing Lin } 3586904242dSMing Lin 35927fa9bc5SChristoph Hellwig static inline void nvme_end_request(struct request *req, __le16 status, 36027fa9bc5SChristoph Hellwig union nvme_result result) 36115a190f7SChristoph Hellwig { 36227fa9bc5SChristoph Hellwig struct nvme_request *rq = nvme_req(req); 36327fa9bc5SChristoph Hellwig 36427fa9bc5SChristoph Hellwig rq->status = le16_to_cpu(status) >> 1; 36527fa9bc5SChristoph Hellwig rq->result = result; 366b9e03857SThomas Tai /* inject error when permitted by fault injection framework */ 367b9e03857SThomas Tai nvme_should_fail(req); 36808e0029aSChristoph Hellwig blk_mq_complete_request(req); 36915a190f7SChristoph Hellwig } 37015a190f7SChristoph Hellwig 371d22524a4SChristoph Hellwig static inline void nvme_get_ctrl(struct nvme_ctrl *ctrl) 372d22524a4SChristoph Hellwig { 373d22524a4SChristoph Hellwig get_device(ctrl->device); 374d22524a4SChristoph Hellwig } 375d22524a4SChristoph Hellwig 376d22524a4SChristoph Hellwig static inline void nvme_put_ctrl(struct nvme_ctrl *ctrl) 377d22524a4SChristoph Hellwig { 378d22524a4SChristoph Hellwig put_device(ctrl->device); 379d22524a4SChristoph Hellwig } 380d22524a4SChristoph Hellwig 38177f02a7aSChristoph Hellwig void nvme_complete_rq(struct request *req); 382c55a2fd4SMing Lin void nvme_cancel_request(struct request *req, void *data, bool reserved); 383bb8d261eSChristoph Hellwig bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl, 384bb8d261eSChristoph Hellwig enum nvme_ctrl_state new_state); 3855fd4ce1bSChristoph Hellwig int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap); 3865fd4ce1bSChristoph Hellwig int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap); 3875fd4ce1bSChristoph Hellwig int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl); 388f3ca80fcSChristoph Hellwig int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev, 389f3ca80fcSChristoph Hellwig const struct nvme_ctrl_ops *ops, unsigned long quirks); 39053029b04SKeith Busch void nvme_uninit_ctrl(struct nvme_ctrl *ctrl); 391d09f2b45SSagi Grimberg void nvme_start_ctrl(struct nvme_ctrl *ctrl); 392d09f2b45SSagi Grimberg void nvme_stop_ctrl(struct nvme_ctrl *ctrl); 3931673f1f0SChristoph Hellwig void nvme_put_ctrl(struct nvme_ctrl *ctrl); 3947fd8930fSChristoph Hellwig int nvme_init_identify(struct nvme_ctrl *ctrl); 3955bae7f73SChristoph Hellwig 3965955be21SChristoph Hellwig void nvme_queue_scan(struct nvme_ctrl *ctrl); 3975bae7f73SChristoph Hellwig void nvme_remove_namespaces(struct nvme_ctrl *ctrl); 3981673f1f0SChristoph Hellwig 3994f1244c8SChristoph Hellwig int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len, 4004f1244c8SChristoph Hellwig bool send); 401a98e58e5SScott Bauer 4027bf58533SChristoph Hellwig void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status, 403287a63ebSChristoph Hellwig volatile union nvme_result *res); 404f866fc42SChristoph Hellwig 40525646264SKeith Busch void nvme_stop_queues(struct nvme_ctrl *ctrl); 40625646264SKeith Busch void nvme_start_queues(struct nvme_ctrl *ctrl); 40769d9a99cSKeith Busch void nvme_kill_queues(struct nvme_ctrl *ctrl); 408302ad8ccSKeith Busch void nvme_unfreeze(struct nvme_ctrl *ctrl); 409302ad8ccSKeith Busch void nvme_wait_freeze(struct nvme_ctrl *ctrl); 410302ad8ccSKeith Busch void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout); 411302ad8ccSKeith Busch void nvme_start_freeze(struct nvme_ctrl *ctrl); 41231b84460SSagi Grimberg int nvme_reinit_tagset(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set); 413363c9aacSSagi Grimberg 414eb71f435SChristoph Hellwig #define NVME_QID_ANY -1 4154160982eSChristoph Hellwig struct request *nvme_alloc_request(struct request_queue *q, 4169a95e4efSBart Van Assche struct nvme_command *cmd, blk_mq_req_flags_t flags, int qid); 417fc17b653SChristoph Hellwig blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req, 4188093f7caSMing Lin struct nvme_command *cmd); 41957dacad5SJay Sternberg int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd, 42057dacad5SJay Sternberg void *buf, unsigned bufflen); 42157dacad5SJay Sternberg int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd, 422d49187e9SChristoph Hellwig union nvme_result *result, void *buffer, unsigned bufflen, 4239a95e4efSBart Van Assche unsigned timeout, int qid, int at_head, 4249a95e4efSBart Van Assche blk_mq_req_flags_t flags); 4259a0be7abSChristoph Hellwig int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count); 426038bd4cbSSagi Grimberg void nvme_stop_keep_alive(struct nvme_ctrl *ctrl); 427d86c4d8eSChristoph Hellwig int nvme_reset_ctrl(struct nvme_ctrl *ctrl); 42879c48ccfSSagi Grimberg int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl); 429c5017e85SChristoph Hellwig int nvme_delete_ctrl(struct nvme_ctrl *ctrl); 430c5017e85SChristoph Hellwig int nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl); 43157dacad5SJay Sternberg 432d558fb51SMatias Bjørling int nvme_get_log_ext(struct nvme_ctrl *ctrl, struct nvme_ns *ns, 4337ec6074fSMatias Bjørling u8 log_page, void *log, size_t size, u64 offset); 434d558fb51SMatias Bjørling 4355b85b826SChristoph Hellwig extern const struct attribute_group nvme_ns_id_attr_group; 43632acab31SChristoph Hellwig extern const struct block_device_operations nvme_ns_head_ops; 43732acab31SChristoph Hellwig 43832acab31SChristoph Hellwig #ifdef CONFIG_NVME_MULTIPATH 43932acab31SChristoph Hellwig void nvme_failover_req(struct request *req); 440908e4564SKeith Busch bool nvme_req_needs_failover(struct request *req, blk_status_t error); 44132acab31SChristoph Hellwig void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl); 44232acab31SChristoph Hellwig int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl,struct nvme_ns_head *head); 44332acab31SChristoph Hellwig void nvme_mpath_add_disk(struct nvme_ns_head *head); 44432acab31SChristoph Hellwig void nvme_mpath_remove_disk(struct nvme_ns_head *head); 44532acab31SChristoph Hellwig 44632acab31SChristoph Hellwig static inline void nvme_mpath_clear_current_path(struct nvme_ns *ns) 44732acab31SChristoph Hellwig { 44832acab31SChristoph Hellwig struct nvme_ns_head *head = ns->head; 44932acab31SChristoph Hellwig 45032acab31SChristoph Hellwig if (head && ns == srcu_dereference(head->current_path, &head->srcu)) 45132acab31SChristoph Hellwig rcu_assign_pointer(head->current_path, NULL); 45232acab31SChristoph Hellwig } 45332acab31SChristoph Hellwig struct nvme_ns *nvme_find_path(struct nvme_ns_head *head); 454479a322fSSagi Grimberg 455479a322fSSagi Grimberg static inline void nvme_mpath_check_last_path(struct nvme_ns *ns) 456479a322fSSagi Grimberg { 457479a322fSSagi Grimberg struct nvme_ns_head *head = ns->head; 458479a322fSSagi Grimberg 459479a322fSSagi Grimberg if (head->disk && list_empty(&head->list)) 460479a322fSSagi Grimberg kblockd_schedule_work(&head->requeue_work); 461479a322fSSagi Grimberg } 462479a322fSSagi Grimberg 46332acab31SChristoph Hellwig #else 46432acab31SChristoph Hellwig static inline void nvme_failover_req(struct request *req) 46532acab31SChristoph Hellwig { 46632acab31SChristoph Hellwig } 467908e4564SKeith Busch static inline bool nvme_req_needs_failover(struct request *req, 468908e4564SKeith Busch blk_status_t error) 46932acab31SChristoph Hellwig { 47032acab31SChristoph Hellwig return false; 47132acab31SChristoph Hellwig } 47232acab31SChristoph Hellwig static inline void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl) 47332acab31SChristoph Hellwig { 47432acab31SChristoph Hellwig } 47532acab31SChristoph Hellwig static inline int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, 47632acab31SChristoph Hellwig struct nvme_ns_head *head) 47732acab31SChristoph Hellwig { 47832acab31SChristoph Hellwig return 0; 47932acab31SChristoph Hellwig } 48032acab31SChristoph Hellwig static inline void nvme_mpath_add_disk(struct nvme_ns_head *head) 48132acab31SChristoph Hellwig { 48232acab31SChristoph Hellwig } 48332acab31SChristoph Hellwig static inline void nvme_mpath_remove_disk(struct nvme_ns_head *head) 48432acab31SChristoph Hellwig { 48532acab31SChristoph Hellwig } 48632acab31SChristoph Hellwig static inline void nvme_mpath_clear_current_path(struct nvme_ns *ns) 48732acab31SChristoph Hellwig { 48832acab31SChristoph Hellwig } 489479a322fSSagi Grimberg static inline void nvme_mpath_check_last_path(struct nvme_ns *ns) 490479a322fSSagi Grimberg { 491479a322fSSagi Grimberg } 49232acab31SChristoph Hellwig #endif /* CONFIG_NVME_MULTIPATH */ 49332acab31SChristoph Hellwig 494c4699e70SKeith Busch #ifdef CONFIG_NVM 49596257a8aSMatias Bjørling void nvme_nvm_update_nvm_info(struct nvme_ns *ns); 4963dc87dd0SMatias Bjørling int nvme_nvm_register(struct nvme_ns *ns, char *disk_name, int node); 497b0b4e09cSMatias Bjørling void nvme_nvm_unregister(struct nvme_ns *ns); 4983dc87dd0SMatias Bjørling int nvme_nvm_register_sysfs(struct nvme_ns *ns); 4993dc87dd0SMatias Bjørling void nvme_nvm_unregister_sysfs(struct nvme_ns *ns); 50084d4add7SMatias Bjørling int nvme_nvm_ioctl(struct nvme_ns *ns, unsigned int cmd, unsigned long arg); 501c4699e70SKeith Busch #else 50296257a8aSMatias Bjørling static inline void nvme_nvm_update_nvm_info(struct nvme_ns *ns) {}; 503b0b4e09cSMatias Bjørling static inline int nvme_nvm_register(struct nvme_ns *ns, char *disk_name, 5043dc87dd0SMatias Bjørling int node) 505c4699e70SKeith Busch { 506c4699e70SKeith Busch return 0; 507c4699e70SKeith Busch } 508c4699e70SKeith Busch 509b0b4e09cSMatias Bjørling static inline void nvme_nvm_unregister(struct nvme_ns *ns) {}; 5103dc87dd0SMatias Bjørling static inline int nvme_nvm_register_sysfs(struct nvme_ns *ns) 5113dc87dd0SMatias Bjørling { 5123dc87dd0SMatias Bjørling return 0; 5133dc87dd0SMatias Bjørling } 5143dc87dd0SMatias Bjørling static inline void nvme_nvm_unregister_sysfs(struct nvme_ns *ns) {}; 51584d4add7SMatias Bjørling static inline int nvme_nvm_ioctl(struct nvme_ns *ns, unsigned int cmd, 51684d4add7SMatias Bjørling unsigned long arg) 51784d4add7SMatias Bjørling { 51884d4add7SMatias Bjørling return -ENOTTY; 51984d4add7SMatias Bjørling } 5203dc87dd0SMatias Bjørling #endif /* CONFIG_NVM */ 5213dc87dd0SMatias Bjørling 52240267efdSSimon A. F. Lund static inline struct nvme_ns *nvme_get_ns_from_dev(struct device *dev) 52340267efdSSimon A. F. Lund { 52440267efdSSimon A. F. Lund return dev_to_disk(dev)->private_data; 52540267efdSSimon A. F. Lund } 526ca064085SMatias Bjørling 5275bae7f73SChristoph Hellwig int __init nvme_core_init(void); 5285bae7f73SChristoph Hellwig void nvme_core_exit(void); 5295bae7f73SChristoph Hellwig 53057dacad5SJay Sternberg #endif /* _NVME_H */ 531