David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * NVM Express device driver |
| 4 | * Copyright (c) 2011-2014, Intel Corporation. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/aer.h> |
| 8 | #include <linux/async.h> |
| 9 | #include <linux/blkdev.h> |
| 10 | #include <linux/blk-mq.h> |
| 11 | #include <linux/blk-mq-pci.h> |
| 12 | #include <linux/dmi.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/io.h> |
| 16 | #include <linux/mm.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/mutex.h> |
| 19 | #include <linux/once.h> |
| 20 | #include <linux/pci.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 21 | #include <linux/suspend.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 22 | #include <linux/t10-pi.h> |
| 23 | #include <linux/types.h> |
| 24 | #include <linux/io-64-nonatomic-lo-hi.h> |
| 25 | #include <linux/sed-opal.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 26 | #include <linux/pci-p2pdma.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 27 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 28 | #include "trace.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 29 | #include "nvme.h" |
| 30 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 31 | #define SQ_SIZE(q) ((q)->q_depth << (q)->sqes) |
| 32 | #define CQ_SIZE(q) ((q)->q_depth * sizeof(struct nvme_completion)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 33 | |
| 34 | #define SGES_PER_PAGE (PAGE_SIZE / sizeof(struct nvme_sgl_desc)) |
| 35 | |
| 36 | /* |
| 37 | * These can be higher, but we need to ensure that any command doesn't |
| 38 | * require an sg allocation that needs more than a page of data. |
| 39 | */ |
| 40 | #define NVME_MAX_KB_SZ 4096 |
| 41 | #define NVME_MAX_SEGS 127 |
| 42 | |
| 43 | static int use_threaded_interrupts; |
| 44 | module_param(use_threaded_interrupts, int, 0); |
| 45 | |
| 46 | static bool use_cmb_sqes = true; |
| 47 | module_param(use_cmb_sqes, bool, 0444); |
| 48 | MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes"); |
| 49 | |
| 50 | static unsigned int max_host_mem_size_mb = 128; |
| 51 | module_param(max_host_mem_size_mb, uint, 0444); |
| 52 | MODULE_PARM_DESC(max_host_mem_size_mb, |
| 53 | "Maximum Host Memory Buffer (HMB) size per controller (in MiB)"); |
| 54 | |
| 55 | static unsigned int sgl_threshold = SZ_32K; |
| 56 | module_param(sgl_threshold, uint, 0644); |
| 57 | MODULE_PARM_DESC(sgl_threshold, |
| 58 | "Use SGLs when average request segment size is larger or equal to " |
| 59 | "this size. Use 0 to disable SGLs."); |
| 60 | |
| 61 | static int io_queue_depth_set(const char *val, const struct kernel_param *kp); |
| 62 | static const struct kernel_param_ops io_queue_depth_ops = { |
| 63 | .set = io_queue_depth_set, |
| 64 | .get = param_get_int, |
| 65 | }; |
| 66 | |
| 67 | static int io_queue_depth = 1024; |
| 68 | module_param_cb(io_queue_depth, &io_queue_depth_ops, &io_queue_depth, 0644); |
| 69 | MODULE_PARM_DESC(io_queue_depth, "set io queue depth, should >= 2"); |
| 70 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 71 | static unsigned int write_queues; |
| 72 | module_param(write_queues, uint, 0644); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 73 | MODULE_PARM_DESC(write_queues, |
| 74 | "Number of queues to use for writes. If not set, reads and writes " |
| 75 | "will share a queue set."); |
| 76 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 77 | static unsigned int poll_queues; |
| 78 | module_param(poll_queues, uint, 0644); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 79 | MODULE_PARM_DESC(poll_queues, "Number of queues to use for polled IO."); |
| 80 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 81 | struct nvme_dev; |
| 82 | struct nvme_queue; |
| 83 | |
| 84 | static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 85 | static bool __nvme_disable_io_queues(struct nvme_dev *dev, u8 opcode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * Represents an NVM Express device. Each nvme_dev is a PCI function. |
| 89 | */ |
| 90 | struct nvme_dev { |
| 91 | struct nvme_queue *queues; |
| 92 | struct blk_mq_tag_set tagset; |
| 93 | struct blk_mq_tag_set admin_tagset; |
| 94 | u32 __iomem *dbs; |
| 95 | struct device *dev; |
| 96 | struct dma_pool *prp_page_pool; |
| 97 | struct dma_pool *prp_small_pool; |
| 98 | unsigned online_queues; |
| 99 | unsigned max_qid; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 100 | unsigned io_queues[HCTX_MAX_TYPES]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 101 | unsigned int num_vecs; |
| 102 | int q_depth; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 103 | int io_sqes; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 104 | u32 db_stride; |
| 105 | void __iomem *bar; |
| 106 | unsigned long bar_mapped_size; |
| 107 | struct work_struct remove_work; |
| 108 | struct mutex shutdown_lock; |
| 109 | bool subsystem; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 110 | u64 cmb_size; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 111 | bool cmb_use_sqes; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 112 | u32 cmbsz; |
| 113 | u32 cmbloc; |
| 114 | struct nvme_ctrl ctrl; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 115 | u32 last_ps; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 116 | |
| 117 | mempool_t *iod_mempool; |
| 118 | |
| 119 | /* shadow doorbell buffer support: */ |
| 120 | u32 *dbbuf_dbs; |
| 121 | dma_addr_t dbbuf_dbs_dma_addr; |
| 122 | u32 *dbbuf_eis; |
| 123 | dma_addr_t dbbuf_eis_dma_addr; |
| 124 | |
| 125 | /* host memory buffer support: */ |
| 126 | u64 host_mem_size; |
| 127 | u32 nr_host_mem_descs; |
| 128 | dma_addr_t host_mem_descs_dma; |
| 129 | struct nvme_host_mem_buf_desc *host_mem_descs; |
| 130 | void **host_mem_desc_bufs; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 131 | unsigned int nr_allocated_queues; |
| 132 | unsigned int nr_write_queues; |
| 133 | unsigned int nr_poll_queues; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | static int io_queue_depth_set(const char *val, const struct kernel_param *kp) |
| 137 | { |
| 138 | int n = 0, ret; |
| 139 | |
| 140 | ret = kstrtoint(val, 10, &n); |
| 141 | if (ret != 0 || n < 2) |
| 142 | return -EINVAL; |
| 143 | |
| 144 | return param_set_int(val, kp); |
| 145 | } |
| 146 | |
| 147 | static inline unsigned int sq_idx(unsigned int qid, u32 stride) |
| 148 | { |
| 149 | return qid * 2 * stride; |
| 150 | } |
| 151 | |
| 152 | static inline unsigned int cq_idx(unsigned int qid, u32 stride) |
| 153 | { |
| 154 | return (qid * 2 + 1) * stride; |
| 155 | } |
| 156 | |
| 157 | static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl) |
| 158 | { |
| 159 | return container_of(ctrl, struct nvme_dev, ctrl); |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * An NVM Express queue. Each device has at least two (one for admin |
| 164 | * commands and one for I/O commands). |
| 165 | */ |
| 166 | struct nvme_queue { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 167 | struct nvme_dev *dev; |
| 168 | spinlock_t sq_lock; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 169 | void *sq_cmds; |
| 170 | /* only used for poll queues: */ |
| 171 | spinlock_t cq_poll_lock ____cacheline_aligned_in_smp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 172 | volatile struct nvme_completion *cqes; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 173 | dma_addr_t sq_dma_addr; |
| 174 | dma_addr_t cq_dma_addr; |
| 175 | u32 __iomem *q_db; |
| 176 | u16 q_depth; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 177 | u16 cq_vector; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 178 | u16 sq_tail; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 179 | u16 last_sq_tail; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 180 | u16 cq_head; |
| 181 | u16 last_cq_head; |
| 182 | u16 qid; |
| 183 | u8 cq_phase; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 184 | u8 sqes; |
| 185 | unsigned long flags; |
| 186 | #define NVMEQ_ENABLED 0 |
| 187 | #define NVMEQ_SQ_CMB 1 |
| 188 | #define NVMEQ_DELETE_ERROR 2 |
| 189 | #define NVMEQ_POLLED 3 |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 190 | u32 *dbbuf_sq_db; |
| 191 | u32 *dbbuf_cq_db; |
| 192 | u32 *dbbuf_sq_ei; |
| 193 | u32 *dbbuf_cq_ei; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 194 | struct completion delete_done; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | /* |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 198 | * The nvme_iod describes the data in an I/O. |
| 199 | * |
| 200 | * The sg pointer contains the list of PRP/SGL chunk allocations in addition |
| 201 | * to the actual struct scatterlist. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 202 | */ |
| 203 | struct nvme_iod { |
| 204 | struct nvme_request req; |
| 205 | struct nvme_queue *nvmeq; |
| 206 | bool use_sgl; |
| 207 | int aborted; |
| 208 | int npages; /* In the PRP list. 0 means small pool in use */ |
| 209 | int nents; /* Used in scatterlist */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 210 | dma_addr_t first_dma; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 211 | unsigned int dma_len; /* length of single DMA segment mapping */ |
| 212 | dma_addr_t meta_dma; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 213 | struct scatterlist *sg; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 214 | }; |
| 215 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 216 | static inline unsigned int nvme_dbbuf_size(struct nvme_dev *dev) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 217 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 218 | return dev->nr_allocated_queues * 8 * dev->db_stride; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | static int nvme_dbbuf_dma_alloc(struct nvme_dev *dev) |
| 222 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 223 | unsigned int mem_size = nvme_dbbuf_size(dev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 224 | |
| 225 | if (dev->dbbuf_dbs) |
| 226 | return 0; |
| 227 | |
| 228 | dev->dbbuf_dbs = dma_alloc_coherent(dev->dev, mem_size, |
| 229 | &dev->dbbuf_dbs_dma_addr, |
| 230 | GFP_KERNEL); |
| 231 | if (!dev->dbbuf_dbs) |
| 232 | return -ENOMEM; |
| 233 | dev->dbbuf_eis = dma_alloc_coherent(dev->dev, mem_size, |
| 234 | &dev->dbbuf_eis_dma_addr, |
| 235 | GFP_KERNEL); |
| 236 | if (!dev->dbbuf_eis) { |
| 237 | dma_free_coherent(dev->dev, mem_size, |
| 238 | dev->dbbuf_dbs, dev->dbbuf_dbs_dma_addr); |
| 239 | dev->dbbuf_dbs = NULL; |
| 240 | return -ENOMEM; |
| 241 | } |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static void nvme_dbbuf_dma_free(struct nvme_dev *dev) |
| 247 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 248 | unsigned int mem_size = nvme_dbbuf_size(dev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 249 | |
| 250 | if (dev->dbbuf_dbs) { |
| 251 | dma_free_coherent(dev->dev, mem_size, |
| 252 | dev->dbbuf_dbs, dev->dbbuf_dbs_dma_addr); |
| 253 | dev->dbbuf_dbs = NULL; |
| 254 | } |
| 255 | if (dev->dbbuf_eis) { |
| 256 | dma_free_coherent(dev->dev, mem_size, |
| 257 | dev->dbbuf_eis, dev->dbbuf_eis_dma_addr); |
| 258 | dev->dbbuf_eis = NULL; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | static void nvme_dbbuf_init(struct nvme_dev *dev, |
| 263 | struct nvme_queue *nvmeq, int qid) |
| 264 | { |
| 265 | if (!dev->dbbuf_dbs || !qid) |
| 266 | return; |
| 267 | |
| 268 | nvmeq->dbbuf_sq_db = &dev->dbbuf_dbs[sq_idx(qid, dev->db_stride)]; |
| 269 | nvmeq->dbbuf_cq_db = &dev->dbbuf_dbs[cq_idx(qid, dev->db_stride)]; |
| 270 | nvmeq->dbbuf_sq_ei = &dev->dbbuf_eis[sq_idx(qid, dev->db_stride)]; |
| 271 | nvmeq->dbbuf_cq_ei = &dev->dbbuf_eis[cq_idx(qid, dev->db_stride)]; |
| 272 | } |
| 273 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 274 | static void nvme_dbbuf_free(struct nvme_queue *nvmeq) |
| 275 | { |
| 276 | if (!nvmeq->qid) |
| 277 | return; |
| 278 | |
| 279 | nvmeq->dbbuf_sq_db = NULL; |
| 280 | nvmeq->dbbuf_cq_db = NULL; |
| 281 | nvmeq->dbbuf_sq_ei = NULL; |
| 282 | nvmeq->dbbuf_cq_ei = NULL; |
| 283 | } |
| 284 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 285 | static void nvme_dbbuf_set(struct nvme_dev *dev) |
| 286 | { |
| 287 | struct nvme_command c; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 288 | unsigned int i; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 289 | |
| 290 | if (!dev->dbbuf_dbs) |
| 291 | return; |
| 292 | |
| 293 | memset(&c, 0, sizeof(c)); |
| 294 | c.dbbuf.opcode = nvme_admin_dbbuf; |
| 295 | c.dbbuf.prp1 = cpu_to_le64(dev->dbbuf_dbs_dma_addr); |
| 296 | c.dbbuf.prp2 = cpu_to_le64(dev->dbbuf_eis_dma_addr); |
| 297 | |
| 298 | if (nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0)) { |
| 299 | dev_warn(dev->ctrl.device, "unable to set dbbuf\n"); |
| 300 | /* Free memory and continue on */ |
| 301 | nvme_dbbuf_dma_free(dev); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 302 | |
| 303 | for (i = 1; i <= dev->online_queues; i++) |
| 304 | nvme_dbbuf_free(&dev->queues[i]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
| 308 | static inline int nvme_dbbuf_need_event(u16 event_idx, u16 new_idx, u16 old) |
| 309 | { |
| 310 | return (u16)(new_idx - event_idx - 1) < (u16)(new_idx - old); |
| 311 | } |
| 312 | |
| 313 | /* Update dbbuf and return true if an MMIO is required */ |
| 314 | static bool nvme_dbbuf_update_and_check_event(u16 value, u32 *dbbuf_db, |
| 315 | volatile u32 *dbbuf_ei) |
| 316 | { |
| 317 | if (dbbuf_db) { |
| 318 | u16 old_value; |
| 319 | |
| 320 | /* |
| 321 | * Ensure that the queue is written before updating |
| 322 | * the doorbell in memory |
| 323 | */ |
| 324 | wmb(); |
| 325 | |
| 326 | old_value = *dbbuf_db; |
| 327 | *dbbuf_db = value; |
| 328 | |
| 329 | /* |
| 330 | * Ensure that the doorbell is updated before reading the event |
| 331 | * index from memory. The controller needs to provide similar |
| 332 | * ordering to ensure the envent index is updated before reading |
| 333 | * the doorbell. |
| 334 | */ |
| 335 | mb(); |
| 336 | |
| 337 | if (!nvme_dbbuf_need_event(*dbbuf_ei, value, old_value)) |
| 338 | return false; |
| 339 | } |
| 340 | |
| 341 | return true; |
| 342 | } |
| 343 | |
| 344 | /* |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 345 | * Will slightly overestimate the number of pages needed. This is OK |
| 346 | * as it only leads to a small amount of wasted memory for the lifetime of |
| 347 | * the I/O. |
| 348 | */ |
| 349 | static int nvme_npages(unsigned size, struct nvme_dev *dev) |
| 350 | { |
| 351 | unsigned nprps = DIV_ROUND_UP(size + dev->ctrl.page_size, |
| 352 | dev->ctrl.page_size); |
| 353 | return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8); |
| 354 | } |
| 355 | |
| 356 | /* |
| 357 | * Calculates the number of pages needed for the SGL segments. For example a 4k |
| 358 | * page can accommodate 256 SGL descriptors. |
| 359 | */ |
| 360 | static int nvme_pci_npages_sgl(unsigned int num_seg) |
| 361 | { |
| 362 | return DIV_ROUND_UP(num_seg * sizeof(struct nvme_sgl_desc), PAGE_SIZE); |
| 363 | } |
| 364 | |
| 365 | static unsigned int nvme_pci_iod_alloc_size(struct nvme_dev *dev, |
| 366 | unsigned int size, unsigned int nseg, bool use_sgl) |
| 367 | { |
| 368 | size_t alloc_size; |
| 369 | |
| 370 | if (use_sgl) |
| 371 | alloc_size = sizeof(__le64 *) * nvme_pci_npages_sgl(nseg); |
| 372 | else |
| 373 | alloc_size = sizeof(__le64 *) * nvme_npages(size, dev); |
| 374 | |
| 375 | return alloc_size + sizeof(struct scatterlist) * nseg; |
| 376 | } |
| 377 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 378 | static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 379 | unsigned int hctx_idx) |
| 380 | { |
| 381 | struct nvme_dev *dev = data; |
| 382 | struct nvme_queue *nvmeq = &dev->queues[0]; |
| 383 | |
| 384 | WARN_ON(hctx_idx != 0); |
| 385 | WARN_ON(dev->admin_tagset.tags[0] != hctx->tags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 386 | |
| 387 | hctx->driver_data = nvmeq; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 388 | return 0; |
| 389 | } |
| 390 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 391 | static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 392 | unsigned int hctx_idx) |
| 393 | { |
| 394 | struct nvme_dev *dev = data; |
| 395 | struct nvme_queue *nvmeq = &dev->queues[hctx_idx + 1]; |
| 396 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 397 | WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags); |
| 398 | hctx->driver_data = nvmeq; |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | static int nvme_init_request(struct blk_mq_tag_set *set, struct request *req, |
| 403 | unsigned int hctx_idx, unsigned int numa_node) |
| 404 | { |
| 405 | struct nvme_dev *dev = set->driver_data; |
| 406 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 407 | int queue_idx = (set == &dev->tagset) ? hctx_idx + 1 : 0; |
| 408 | struct nvme_queue *nvmeq = &dev->queues[queue_idx]; |
| 409 | |
| 410 | BUG_ON(!nvmeq); |
| 411 | iod->nvmeq = nvmeq; |
| 412 | |
| 413 | nvme_req(req)->ctrl = &dev->ctrl; |
| 414 | return 0; |
| 415 | } |
| 416 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 417 | static int queue_irq_offset(struct nvme_dev *dev) |
| 418 | { |
| 419 | /* if we have more than 1 vec, admin queue offsets us by 1 */ |
| 420 | if (dev->num_vecs > 1) |
| 421 | return 1; |
| 422 | |
| 423 | return 0; |
| 424 | } |
| 425 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 426 | static int nvme_pci_map_queues(struct blk_mq_tag_set *set) |
| 427 | { |
| 428 | struct nvme_dev *dev = set->driver_data; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 429 | int i, qoff, offset; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 430 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 431 | offset = queue_irq_offset(dev); |
| 432 | for (i = 0, qoff = 0; i < set->nr_maps; i++) { |
| 433 | struct blk_mq_queue_map *map = &set->map[i]; |
| 434 | |
| 435 | map->nr_queues = dev->io_queues[i]; |
| 436 | if (!map->nr_queues) { |
| 437 | BUG_ON(i == HCTX_TYPE_DEFAULT); |
| 438 | continue; |
| 439 | } |
| 440 | |
| 441 | /* |
| 442 | * The poll queue(s) doesn't have an IRQ (and hence IRQ |
| 443 | * affinity), so use the regular blk-mq cpu mapping |
| 444 | */ |
| 445 | map->queue_offset = qoff; |
| 446 | if (i != HCTX_TYPE_POLL && offset) |
| 447 | blk_mq_pci_map_queues(map, to_pci_dev(dev->dev), offset); |
| 448 | else |
| 449 | blk_mq_map_queues(map); |
| 450 | qoff += map->nr_queues; |
| 451 | offset += map->nr_queues; |
| 452 | } |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | /* |
| 458 | * Write sq tail if we are asked to, or if the next command would wrap. |
| 459 | */ |
| 460 | static inline void nvme_write_sq_db(struct nvme_queue *nvmeq, bool write_sq) |
| 461 | { |
| 462 | if (!write_sq) { |
| 463 | u16 next_tail = nvmeq->sq_tail + 1; |
| 464 | |
| 465 | if (next_tail == nvmeq->q_depth) |
| 466 | next_tail = 0; |
| 467 | if (next_tail != nvmeq->last_sq_tail) |
| 468 | return; |
| 469 | } |
| 470 | |
| 471 | if (nvme_dbbuf_update_and_check_event(nvmeq->sq_tail, |
| 472 | nvmeq->dbbuf_sq_db, nvmeq->dbbuf_sq_ei)) |
| 473 | writel(nvmeq->sq_tail, nvmeq->q_db); |
| 474 | nvmeq->last_sq_tail = nvmeq->sq_tail; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /** |
| 478 | * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell |
| 479 | * @nvmeq: The queue to use |
| 480 | * @cmd: The command to send |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 481 | * @write_sq: whether to write to the SQ doorbell |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 482 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 483 | static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd, |
| 484 | bool write_sq) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 485 | { |
| 486 | spin_lock(&nvmeq->sq_lock); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 487 | memcpy(nvmeq->sq_cmds + (nvmeq->sq_tail << nvmeq->sqes), |
| 488 | cmd, sizeof(*cmd)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 489 | if (++nvmeq->sq_tail == nvmeq->q_depth) |
| 490 | nvmeq->sq_tail = 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 491 | nvme_write_sq_db(nvmeq, write_sq); |
| 492 | spin_unlock(&nvmeq->sq_lock); |
| 493 | } |
| 494 | |
| 495 | static void nvme_commit_rqs(struct blk_mq_hw_ctx *hctx) |
| 496 | { |
| 497 | struct nvme_queue *nvmeq = hctx->driver_data; |
| 498 | |
| 499 | spin_lock(&nvmeq->sq_lock); |
| 500 | if (nvmeq->sq_tail != nvmeq->last_sq_tail) |
| 501 | nvme_write_sq_db(nvmeq, true); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 502 | spin_unlock(&nvmeq->sq_lock); |
| 503 | } |
| 504 | |
| 505 | static void **nvme_pci_iod_list(struct request *req) |
| 506 | { |
| 507 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 508 | return (void **)(iod->sg + blk_rq_nr_phys_segments(req)); |
| 509 | } |
| 510 | |
| 511 | static inline bool nvme_pci_use_sgls(struct nvme_dev *dev, struct request *req) |
| 512 | { |
| 513 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 514 | int nseg = blk_rq_nr_phys_segments(req); |
| 515 | unsigned int avg_seg_size; |
| 516 | |
| 517 | if (nseg == 0) |
| 518 | return false; |
| 519 | |
| 520 | avg_seg_size = DIV_ROUND_UP(blk_rq_payload_bytes(req), nseg); |
| 521 | |
| 522 | if (!(dev->ctrl.sgls & ((1 << 0) | (1 << 1)))) |
| 523 | return false; |
| 524 | if (!iod->nvmeq->qid) |
| 525 | return false; |
| 526 | if (!sgl_threshold || avg_seg_size < sgl_threshold) |
| 527 | return false; |
| 528 | return true; |
| 529 | } |
| 530 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 531 | static void nvme_free_prps(struct nvme_dev *dev, struct request *req) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 532 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 533 | const int last_prp = dev->ctrl.page_size / sizeof(__le64) - 1; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 534 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 535 | dma_addr_t dma_addr = iod->first_dma; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 536 | int i; |
| 537 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 538 | for (i = 0; i < iod->npages; i++) { |
| 539 | __le64 *prp_list = nvme_pci_iod_list(req)[i]; |
| 540 | dma_addr_t next_dma_addr = le64_to_cpu(prp_list[last_prp]); |
| 541 | |
| 542 | dma_pool_free(dev->prp_page_pool, prp_list, dma_addr); |
| 543 | dma_addr = next_dma_addr; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 546 | } |
| 547 | |
| 548 | static void nvme_free_sgls(struct nvme_dev *dev, struct request *req) |
| 549 | { |
| 550 | const int last_sg = SGES_PER_PAGE - 1; |
| 551 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 552 | dma_addr_t dma_addr = iod->first_dma; |
| 553 | int i; |
| 554 | |
| 555 | for (i = 0; i < iod->npages; i++) { |
| 556 | struct nvme_sgl_desc *sg_list = nvme_pci_iod_list(req)[i]; |
| 557 | dma_addr_t next_dma_addr = le64_to_cpu((sg_list[last_sg]).addr); |
| 558 | |
| 559 | dma_pool_free(dev->prp_page_pool, sg_list, dma_addr); |
| 560 | dma_addr = next_dma_addr; |
| 561 | } |
| 562 | |
| 563 | } |
| 564 | |
| 565 | static void nvme_unmap_sg(struct nvme_dev *dev, struct request *req) |
| 566 | { |
| 567 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 568 | |
| 569 | if (is_pci_p2pdma_page(sg_page(iod->sg))) |
| 570 | pci_p2pdma_unmap_sg(dev->dev, iod->sg, iod->nents, |
| 571 | rq_dma_dir(req)); |
| 572 | else |
| 573 | dma_unmap_sg(dev->dev, iod->sg, iod->nents, rq_dma_dir(req)); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 574 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 575 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 576 | static void nvme_unmap_data(struct nvme_dev *dev, struct request *req) |
| 577 | { |
| 578 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 579 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 580 | if (iod->dma_len) { |
| 581 | dma_unmap_page(dev->dev, iod->first_dma, iod->dma_len, |
| 582 | rq_dma_dir(req)); |
| 583 | return; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 586 | WARN_ON_ONCE(!iod->nents); |
| 587 | |
| 588 | nvme_unmap_sg(dev, req); |
| 589 | if (iod->npages == 0) |
| 590 | dma_pool_free(dev->prp_small_pool, nvme_pci_iod_list(req)[0], |
| 591 | iod->first_dma); |
| 592 | else if (iod->use_sgl) |
| 593 | nvme_free_sgls(dev, req); |
| 594 | else |
| 595 | nvme_free_prps(dev, req); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 596 | mempool_free(iod->sg, dev->iod_mempool); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | static void nvme_print_sgl(struct scatterlist *sgl, int nents) |
| 600 | { |
| 601 | int i; |
| 602 | struct scatterlist *sg; |
| 603 | |
| 604 | for_each_sg(sgl, sg, nents, i) { |
| 605 | dma_addr_t phys = sg_phys(sg); |
| 606 | pr_warn("sg[%d] phys_addr:%pad offset:%d length:%d " |
| 607 | "dma_address:%pad dma_length:%d\n", |
| 608 | i, &phys, sg->offset, sg->length, &sg_dma_address(sg), |
| 609 | sg_dma_len(sg)); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | static blk_status_t nvme_pci_setup_prps(struct nvme_dev *dev, |
| 614 | struct request *req, struct nvme_rw_command *cmnd) |
| 615 | { |
| 616 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 617 | struct dma_pool *pool; |
| 618 | int length = blk_rq_payload_bytes(req); |
| 619 | struct scatterlist *sg = iod->sg; |
| 620 | int dma_len = sg_dma_len(sg); |
| 621 | u64 dma_addr = sg_dma_address(sg); |
| 622 | u32 page_size = dev->ctrl.page_size; |
| 623 | int offset = dma_addr & (page_size - 1); |
| 624 | __le64 *prp_list; |
| 625 | void **list = nvme_pci_iod_list(req); |
| 626 | dma_addr_t prp_dma; |
| 627 | int nprps, i; |
| 628 | |
| 629 | length -= (page_size - offset); |
| 630 | if (length <= 0) { |
| 631 | iod->first_dma = 0; |
| 632 | goto done; |
| 633 | } |
| 634 | |
| 635 | dma_len -= (page_size - offset); |
| 636 | if (dma_len) { |
| 637 | dma_addr += (page_size - offset); |
| 638 | } else { |
| 639 | sg = sg_next(sg); |
| 640 | dma_addr = sg_dma_address(sg); |
| 641 | dma_len = sg_dma_len(sg); |
| 642 | } |
| 643 | |
| 644 | if (length <= page_size) { |
| 645 | iod->first_dma = dma_addr; |
| 646 | goto done; |
| 647 | } |
| 648 | |
| 649 | nprps = DIV_ROUND_UP(length, page_size); |
| 650 | if (nprps <= (256 / 8)) { |
| 651 | pool = dev->prp_small_pool; |
| 652 | iod->npages = 0; |
| 653 | } else { |
| 654 | pool = dev->prp_page_pool; |
| 655 | iod->npages = 1; |
| 656 | } |
| 657 | |
| 658 | prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma); |
| 659 | if (!prp_list) { |
| 660 | iod->first_dma = dma_addr; |
| 661 | iod->npages = -1; |
| 662 | return BLK_STS_RESOURCE; |
| 663 | } |
| 664 | list[0] = prp_list; |
| 665 | iod->first_dma = prp_dma; |
| 666 | i = 0; |
| 667 | for (;;) { |
| 668 | if (i == page_size >> 3) { |
| 669 | __le64 *old_prp_list = prp_list; |
| 670 | prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma); |
| 671 | if (!prp_list) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 672 | goto free_prps; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 673 | list[iod->npages++] = prp_list; |
| 674 | prp_list[0] = old_prp_list[i - 1]; |
| 675 | old_prp_list[i - 1] = cpu_to_le64(prp_dma); |
| 676 | i = 1; |
| 677 | } |
| 678 | prp_list[i++] = cpu_to_le64(dma_addr); |
| 679 | dma_len -= page_size; |
| 680 | dma_addr += page_size; |
| 681 | length -= page_size; |
| 682 | if (length <= 0) |
| 683 | break; |
| 684 | if (dma_len > 0) |
| 685 | continue; |
| 686 | if (unlikely(dma_len < 0)) |
| 687 | goto bad_sgl; |
| 688 | sg = sg_next(sg); |
| 689 | dma_addr = sg_dma_address(sg); |
| 690 | dma_len = sg_dma_len(sg); |
| 691 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 692 | done: |
| 693 | cmnd->dptr.prp1 = cpu_to_le64(sg_dma_address(iod->sg)); |
| 694 | cmnd->dptr.prp2 = cpu_to_le64(iod->first_dma); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 695 | return BLK_STS_OK; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 696 | free_prps: |
| 697 | nvme_free_prps(dev, req); |
| 698 | return BLK_STS_RESOURCE; |
| 699 | bad_sgl: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 700 | WARN(DO_ONCE(nvme_print_sgl, iod->sg, iod->nents), |
| 701 | "Invalid SGL for payload:%d nents:%d\n", |
| 702 | blk_rq_payload_bytes(req), iod->nents); |
| 703 | return BLK_STS_IOERR; |
| 704 | } |
| 705 | |
| 706 | static void nvme_pci_sgl_set_data(struct nvme_sgl_desc *sge, |
| 707 | struct scatterlist *sg) |
| 708 | { |
| 709 | sge->addr = cpu_to_le64(sg_dma_address(sg)); |
| 710 | sge->length = cpu_to_le32(sg_dma_len(sg)); |
| 711 | sge->type = NVME_SGL_FMT_DATA_DESC << 4; |
| 712 | } |
| 713 | |
| 714 | static void nvme_pci_sgl_set_seg(struct nvme_sgl_desc *sge, |
| 715 | dma_addr_t dma_addr, int entries) |
| 716 | { |
| 717 | sge->addr = cpu_to_le64(dma_addr); |
| 718 | if (entries < SGES_PER_PAGE) { |
| 719 | sge->length = cpu_to_le32(entries * sizeof(*sge)); |
| 720 | sge->type = NVME_SGL_FMT_LAST_SEG_DESC << 4; |
| 721 | } else { |
| 722 | sge->length = cpu_to_le32(PAGE_SIZE); |
| 723 | sge->type = NVME_SGL_FMT_SEG_DESC << 4; |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | static blk_status_t nvme_pci_setup_sgls(struct nvme_dev *dev, |
| 728 | struct request *req, struct nvme_rw_command *cmd, int entries) |
| 729 | { |
| 730 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 731 | struct dma_pool *pool; |
| 732 | struct nvme_sgl_desc *sg_list; |
| 733 | struct scatterlist *sg = iod->sg; |
| 734 | dma_addr_t sgl_dma; |
| 735 | int i = 0; |
| 736 | |
| 737 | /* setting the transfer type as SGL */ |
| 738 | cmd->flags = NVME_CMD_SGL_METABUF; |
| 739 | |
| 740 | if (entries == 1) { |
| 741 | nvme_pci_sgl_set_data(&cmd->dptr.sgl, sg); |
| 742 | return BLK_STS_OK; |
| 743 | } |
| 744 | |
| 745 | if (entries <= (256 / sizeof(struct nvme_sgl_desc))) { |
| 746 | pool = dev->prp_small_pool; |
| 747 | iod->npages = 0; |
| 748 | } else { |
| 749 | pool = dev->prp_page_pool; |
| 750 | iod->npages = 1; |
| 751 | } |
| 752 | |
| 753 | sg_list = dma_pool_alloc(pool, GFP_ATOMIC, &sgl_dma); |
| 754 | if (!sg_list) { |
| 755 | iod->npages = -1; |
| 756 | return BLK_STS_RESOURCE; |
| 757 | } |
| 758 | |
| 759 | nvme_pci_iod_list(req)[0] = sg_list; |
| 760 | iod->first_dma = sgl_dma; |
| 761 | |
| 762 | nvme_pci_sgl_set_seg(&cmd->dptr.sgl, sgl_dma, entries); |
| 763 | |
| 764 | do { |
| 765 | if (i == SGES_PER_PAGE) { |
| 766 | struct nvme_sgl_desc *old_sg_desc = sg_list; |
| 767 | struct nvme_sgl_desc *link = &old_sg_desc[i - 1]; |
| 768 | |
| 769 | sg_list = dma_pool_alloc(pool, GFP_ATOMIC, &sgl_dma); |
| 770 | if (!sg_list) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 771 | goto free_sgls; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 772 | |
| 773 | i = 0; |
| 774 | nvme_pci_iod_list(req)[iod->npages++] = sg_list; |
| 775 | sg_list[i++] = *link; |
| 776 | nvme_pci_sgl_set_seg(link, sgl_dma, entries); |
| 777 | } |
| 778 | |
| 779 | nvme_pci_sgl_set_data(&sg_list[i++], sg); |
| 780 | sg = sg_next(sg); |
| 781 | } while (--entries > 0); |
| 782 | |
| 783 | return BLK_STS_OK; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 784 | free_sgls: |
| 785 | nvme_free_sgls(dev, req); |
| 786 | return BLK_STS_RESOURCE; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 787 | } |
| 788 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 789 | static blk_status_t nvme_setup_prp_simple(struct nvme_dev *dev, |
| 790 | struct request *req, struct nvme_rw_command *cmnd, |
| 791 | struct bio_vec *bv) |
| 792 | { |
| 793 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 794 | unsigned int offset = bv->bv_offset & (dev->ctrl.page_size - 1); |
| 795 | unsigned int first_prp_len = dev->ctrl.page_size - offset; |
| 796 | |
| 797 | iod->first_dma = dma_map_bvec(dev->dev, bv, rq_dma_dir(req), 0); |
| 798 | if (dma_mapping_error(dev->dev, iod->first_dma)) |
| 799 | return BLK_STS_RESOURCE; |
| 800 | iod->dma_len = bv->bv_len; |
| 801 | |
| 802 | cmnd->dptr.prp1 = cpu_to_le64(iod->first_dma); |
| 803 | if (bv->bv_len > first_prp_len) |
| 804 | cmnd->dptr.prp2 = cpu_to_le64(iod->first_dma + first_prp_len); |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | static blk_status_t nvme_setup_sgl_simple(struct nvme_dev *dev, |
| 809 | struct request *req, struct nvme_rw_command *cmnd, |
| 810 | struct bio_vec *bv) |
| 811 | { |
| 812 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 813 | |
| 814 | iod->first_dma = dma_map_bvec(dev->dev, bv, rq_dma_dir(req), 0); |
| 815 | if (dma_mapping_error(dev->dev, iod->first_dma)) |
| 816 | return BLK_STS_RESOURCE; |
| 817 | iod->dma_len = bv->bv_len; |
| 818 | |
| 819 | cmnd->flags = NVME_CMD_SGL_METABUF; |
| 820 | cmnd->dptr.sgl.addr = cpu_to_le64(iod->first_dma); |
| 821 | cmnd->dptr.sgl.length = cpu_to_le32(iod->dma_len); |
| 822 | cmnd->dptr.sgl.type = NVME_SGL_FMT_DATA_DESC << 4; |
| 823 | return 0; |
| 824 | } |
| 825 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 826 | static blk_status_t nvme_map_data(struct nvme_dev *dev, struct request *req, |
| 827 | struct nvme_command *cmnd) |
| 828 | { |
| 829 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 830 | blk_status_t ret = BLK_STS_RESOURCE; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 831 | int nr_mapped; |
| 832 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 833 | if (blk_rq_nr_phys_segments(req) == 1) { |
| 834 | struct bio_vec bv = req_bvec(req); |
| 835 | |
| 836 | if (!is_pci_p2pdma_page(bv.bv_page)) { |
| 837 | if (bv.bv_offset + bv.bv_len <= dev->ctrl.page_size * 2) |
| 838 | return nvme_setup_prp_simple(dev, req, |
| 839 | &cmnd->rw, &bv); |
| 840 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 841 | if (iod->nvmeq->qid && sgl_threshold && |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 842 | dev->ctrl.sgls & ((1 << 0) | (1 << 1))) |
| 843 | return nvme_setup_sgl_simple(dev, req, |
| 844 | &cmnd->rw, &bv); |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | iod->dma_len = 0; |
| 849 | iod->sg = mempool_alloc(dev->iod_mempool, GFP_ATOMIC); |
| 850 | if (!iod->sg) |
| 851 | return BLK_STS_RESOURCE; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 852 | sg_init_table(iod->sg, blk_rq_nr_phys_segments(req)); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 853 | iod->nents = blk_rq_map_sg(req->q, req, iod->sg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 854 | if (!iod->nents) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 855 | goto out_free_sg; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 856 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 857 | if (is_pci_p2pdma_page(sg_page(iod->sg))) |
| 858 | nr_mapped = pci_p2pdma_map_sg_attrs(dev->dev, iod->sg, |
| 859 | iod->nents, rq_dma_dir(req), DMA_ATTR_NO_WARN); |
| 860 | else |
| 861 | nr_mapped = dma_map_sg_attrs(dev->dev, iod->sg, iod->nents, |
| 862 | rq_dma_dir(req), DMA_ATTR_NO_WARN); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 863 | if (!nr_mapped) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 864 | goto out_free_sg; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 865 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 866 | iod->use_sgl = nvme_pci_use_sgls(dev, req); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 867 | if (iod->use_sgl) |
| 868 | ret = nvme_pci_setup_sgls(dev, req, &cmnd->rw, nr_mapped); |
| 869 | else |
| 870 | ret = nvme_pci_setup_prps(dev, req, &cmnd->rw); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 871 | if (ret != BLK_STS_OK) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 872 | goto out_unmap_sg; |
| 873 | return BLK_STS_OK; |
| 874 | |
| 875 | out_unmap_sg: |
| 876 | nvme_unmap_sg(dev, req); |
| 877 | out_free_sg: |
| 878 | mempool_free(iod->sg, dev->iod_mempool); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 879 | return ret; |
| 880 | } |
| 881 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 882 | static blk_status_t nvme_map_metadata(struct nvme_dev *dev, struct request *req, |
| 883 | struct nvme_command *cmnd) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 884 | { |
| 885 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 886 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 887 | iod->meta_dma = dma_map_bvec(dev->dev, rq_integrity_vec(req), |
| 888 | rq_dma_dir(req), 0); |
| 889 | if (dma_mapping_error(dev->dev, iod->meta_dma)) |
| 890 | return BLK_STS_IOERR; |
| 891 | cmnd->rw.metadata = cpu_to_le64(iod->meta_dma); |
| 892 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | /* |
| 896 | * NOTE: ns is NULL when called on the admin queue. |
| 897 | */ |
| 898 | static blk_status_t nvme_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 899 | const struct blk_mq_queue_data *bd) |
| 900 | { |
| 901 | struct nvme_ns *ns = hctx->queue->queuedata; |
| 902 | struct nvme_queue *nvmeq = hctx->driver_data; |
| 903 | struct nvme_dev *dev = nvmeq->dev; |
| 904 | struct request *req = bd->rq; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 905 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 906 | struct nvme_command cmnd; |
| 907 | blk_status_t ret; |
| 908 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 909 | iod->aborted = 0; |
| 910 | iod->npages = -1; |
| 911 | iod->nents = 0; |
| 912 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 913 | /* |
| 914 | * We should not need to do this, but we're still using this to |
| 915 | * ensure we can drain requests on a dying queue. |
| 916 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 917 | if (unlikely(!test_bit(NVMEQ_ENABLED, &nvmeq->flags))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 918 | return BLK_STS_IOERR; |
| 919 | |
| 920 | ret = nvme_setup_cmd(ns, req, &cmnd); |
| 921 | if (ret) |
| 922 | return ret; |
| 923 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 924 | if (blk_rq_nr_phys_segments(req)) { |
| 925 | ret = nvme_map_data(dev, req, &cmnd); |
| 926 | if (ret) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 927 | goto out_free_cmd; |
| 928 | } |
| 929 | |
| 930 | if (blk_integrity_rq(req)) { |
| 931 | ret = nvme_map_metadata(dev, req, &cmnd); |
| 932 | if (ret) |
| 933 | goto out_unmap_data; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | blk_mq_start_request(req); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 937 | nvme_submit_cmd(nvmeq, &cmnd, bd->last); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 938 | return BLK_STS_OK; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 939 | out_unmap_data: |
| 940 | nvme_unmap_data(dev, req); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 941 | out_free_cmd: |
| 942 | nvme_cleanup_cmd(req); |
| 943 | return ret; |
| 944 | } |
| 945 | |
| 946 | static void nvme_pci_complete_rq(struct request *req) |
| 947 | { |
| 948 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 949 | struct nvme_dev *dev = iod->nvmeq->dev; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 950 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 951 | nvme_cleanup_cmd(req); |
| 952 | if (blk_integrity_rq(req)) |
| 953 | dma_unmap_page(dev->dev, iod->meta_dma, |
| 954 | rq_integrity_vec(req)->bv_len, rq_data_dir(req)); |
| 955 | if (blk_rq_nr_phys_segments(req)) |
| 956 | nvme_unmap_data(dev, req); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 957 | nvme_complete_rq(req); |
| 958 | } |
| 959 | |
| 960 | /* We read the CQE phase first to check if the rest of the entry is valid */ |
| 961 | static inline bool nvme_cqe_pending(struct nvme_queue *nvmeq) |
| 962 | { |
| 963 | return (le16_to_cpu(nvmeq->cqes[nvmeq->cq_head].status) & 1) == |
| 964 | nvmeq->cq_phase; |
| 965 | } |
| 966 | |
| 967 | static inline void nvme_ring_cq_doorbell(struct nvme_queue *nvmeq) |
| 968 | { |
| 969 | u16 head = nvmeq->cq_head; |
| 970 | |
| 971 | if (nvme_dbbuf_update_and_check_event(head, nvmeq->dbbuf_cq_db, |
| 972 | nvmeq->dbbuf_cq_ei)) |
| 973 | writel(head, nvmeq->q_db + nvmeq->dev->db_stride); |
| 974 | } |
| 975 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 976 | static inline struct blk_mq_tags *nvme_queue_tagset(struct nvme_queue *nvmeq) |
| 977 | { |
| 978 | if (!nvmeq->qid) |
| 979 | return nvmeq->dev->admin_tagset.tags[0]; |
| 980 | return nvmeq->dev->tagset.tags[nvmeq->qid - 1]; |
| 981 | } |
| 982 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 983 | static inline void nvme_handle_cqe(struct nvme_queue *nvmeq, u16 idx) |
| 984 | { |
| 985 | volatile struct nvme_completion *cqe = &nvmeq->cqes[idx]; |
| 986 | struct request *req; |
| 987 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 988 | /* |
| 989 | * AEN requests are special as they don't time out and can |
| 990 | * survive any kind of queue freeze and often don't respond to |
| 991 | * aborts. We don't even bother to allocate a struct request |
| 992 | * for them but rather special case them here. |
| 993 | */ |
| 994 | if (unlikely(nvmeq->qid == 0 && |
| 995 | cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH)) { |
| 996 | nvme_complete_async_event(&nvmeq->dev->ctrl, |
| 997 | cqe->status, &cqe->result); |
| 998 | return; |
| 999 | } |
| 1000 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1001 | req = blk_mq_tag_to_rq(nvme_queue_tagset(nvmeq), cqe->command_id); |
| 1002 | if (unlikely(!req)) { |
| 1003 | dev_warn(nvmeq->dev->ctrl.device, |
| 1004 | "invalid id %d completed on queue %d\n", |
| 1005 | cqe->command_id, le16_to_cpu(cqe->sq_id)); |
| 1006 | return; |
| 1007 | } |
| 1008 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1009 | trace_nvme_sq(req, cqe->sq_head, nvmeq->sq_tail); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1010 | nvme_end_request(req, cqe->status, cqe->result); |
| 1011 | } |
| 1012 | |
| 1013 | static void nvme_complete_cqes(struct nvme_queue *nvmeq, u16 start, u16 end) |
| 1014 | { |
| 1015 | while (start != end) { |
| 1016 | nvme_handle_cqe(nvmeq, start); |
| 1017 | if (++start == nvmeq->q_depth) |
| 1018 | start = 0; |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | static inline void nvme_update_cq_head(struct nvme_queue *nvmeq) |
| 1023 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1024 | if (nvmeq->cq_head == nvmeq->q_depth - 1) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1025 | nvmeq->cq_head = 0; |
| 1026 | nvmeq->cq_phase = !nvmeq->cq_phase; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1027 | } else { |
| 1028 | nvmeq->cq_head++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1032 | static inline int nvme_process_cq(struct nvme_queue *nvmeq, u16 *start, |
| 1033 | u16 *end, unsigned int tag) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1034 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1035 | int found = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1036 | |
| 1037 | *start = nvmeq->cq_head; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1038 | while (nvme_cqe_pending(nvmeq)) { |
| 1039 | if (tag == -1U || nvmeq->cqes[nvmeq->cq_head].command_id == tag) |
| 1040 | found++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1041 | nvme_update_cq_head(nvmeq); |
| 1042 | } |
| 1043 | *end = nvmeq->cq_head; |
| 1044 | |
| 1045 | if (*start != *end) |
| 1046 | nvme_ring_cq_doorbell(nvmeq); |
| 1047 | return found; |
| 1048 | } |
| 1049 | |
| 1050 | static irqreturn_t nvme_irq(int irq, void *data) |
| 1051 | { |
| 1052 | struct nvme_queue *nvmeq = data; |
| 1053 | irqreturn_t ret = IRQ_NONE; |
| 1054 | u16 start, end; |
| 1055 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1056 | /* |
| 1057 | * The rmb/wmb pair ensures we see all updates from a previous run of |
| 1058 | * the irq handler, even if that was on another CPU. |
| 1059 | */ |
| 1060 | rmb(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1061 | if (nvmeq->cq_head != nvmeq->last_cq_head) |
| 1062 | ret = IRQ_HANDLED; |
| 1063 | nvme_process_cq(nvmeq, &start, &end, -1); |
| 1064 | nvmeq->last_cq_head = nvmeq->cq_head; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1065 | wmb(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1066 | |
| 1067 | if (start != end) { |
| 1068 | nvme_complete_cqes(nvmeq, start, end); |
| 1069 | return IRQ_HANDLED; |
| 1070 | } |
| 1071 | |
| 1072 | return ret; |
| 1073 | } |
| 1074 | |
| 1075 | static irqreturn_t nvme_irq_check(int irq, void *data) |
| 1076 | { |
| 1077 | struct nvme_queue *nvmeq = data; |
| 1078 | if (nvme_cqe_pending(nvmeq)) |
| 1079 | return IRQ_WAKE_THREAD; |
| 1080 | return IRQ_NONE; |
| 1081 | } |
| 1082 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1083 | /* |
| 1084 | * Poll for completions any queue, including those not dedicated to polling. |
| 1085 | * Can be called from any context. |
| 1086 | */ |
| 1087 | static int nvme_poll_irqdisable(struct nvme_queue *nvmeq, unsigned int tag) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1088 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1089 | struct pci_dev *pdev = to_pci_dev(nvmeq->dev->dev); |
| 1090 | u16 start, end; |
| 1091 | int found; |
| 1092 | |
| 1093 | /* |
| 1094 | * For a poll queue we need to protect against the polling thread |
| 1095 | * using the CQ lock. For normal interrupt driven threads we have |
| 1096 | * to disable the interrupt to avoid racing with it. |
| 1097 | */ |
| 1098 | if (test_bit(NVMEQ_POLLED, &nvmeq->flags)) { |
| 1099 | spin_lock(&nvmeq->cq_poll_lock); |
| 1100 | found = nvme_process_cq(nvmeq, &start, &end, tag); |
| 1101 | spin_unlock(&nvmeq->cq_poll_lock); |
| 1102 | } else { |
| 1103 | disable_irq(pci_irq_vector(pdev, nvmeq->cq_vector)); |
| 1104 | found = nvme_process_cq(nvmeq, &start, &end, tag); |
| 1105 | enable_irq(pci_irq_vector(pdev, nvmeq->cq_vector)); |
| 1106 | } |
| 1107 | |
| 1108 | nvme_complete_cqes(nvmeq, start, end); |
| 1109 | return found; |
| 1110 | } |
| 1111 | |
| 1112 | static int nvme_poll(struct blk_mq_hw_ctx *hctx) |
| 1113 | { |
| 1114 | struct nvme_queue *nvmeq = hctx->driver_data; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1115 | u16 start, end; |
| 1116 | bool found; |
| 1117 | |
| 1118 | if (!nvme_cqe_pending(nvmeq)) |
| 1119 | return 0; |
| 1120 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1121 | spin_lock(&nvmeq->cq_poll_lock); |
| 1122 | found = nvme_process_cq(nvmeq, &start, &end, -1); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1123 | nvme_complete_cqes(nvmeq, start, end); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1124 | spin_unlock(&nvmeq->cq_poll_lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1125 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1126 | return found; |
| 1127 | } |
| 1128 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1129 | static void nvme_pci_submit_async_event(struct nvme_ctrl *ctrl) |
| 1130 | { |
| 1131 | struct nvme_dev *dev = to_nvme_dev(ctrl); |
| 1132 | struct nvme_queue *nvmeq = &dev->queues[0]; |
| 1133 | struct nvme_command c; |
| 1134 | |
| 1135 | memset(&c, 0, sizeof(c)); |
| 1136 | c.common.opcode = nvme_admin_async_event; |
| 1137 | c.common.command_id = NVME_AQ_BLK_MQ_DEPTH; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1138 | nvme_submit_cmd(nvmeq, &c, true); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id) |
| 1142 | { |
| 1143 | struct nvme_command c; |
| 1144 | |
| 1145 | memset(&c, 0, sizeof(c)); |
| 1146 | c.delete_queue.opcode = opcode; |
| 1147 | c.delete_queue.qid = cpu_to_le16(id); |
| 1148 | |
| 1149 | return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0); |
| 1150 | } |
| 1151 | |
| 1152 | static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid, |
| 1153 | struct nvme_queue *nvmeq, s16 vector) |
| 1154 | { |
| 1155 | struct nvme_command c; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1156 | int flags = NVME_QUEUE_PHYS_CONTIG; |
| 1157 | |
| 1158 | if (!test_bit(NVMEQ_POLLED, &nvmeq->flags)) |
| 1159 | flags |= NVME_CQ_IRQ_ENABLED; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1160 | |
| 1161 | /* |
| 1162 | * Note: we (ab)use the fact that the prp fields survive if no data |
| 1163 | * is attached to the request. |
| 1164 | */ |
| 1165 | memset(&c, 0, sizeof(c)); |
| 1166 | c.create_cq.opcode = nvme_admin_create_cq; |
| 1167 | c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr); |
| 1168 | c.create_cq.cqid = cpu_to_le16(qid); |
| 1169 | c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1); |
| 1170 | c.create_cq.cq_flags = cpu_to_le16(flags); |
| 1171 | c.create_cq.irq_vector = cpu_to_le16(vector); |
| 1172 | |
| 1173 | return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0); |
| 1174 | } |
| 1175 | |
| 1176 | static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid, |
| 1177 | struct nvme_queue *nvmeq) |
| 1178 | { |
| 1179 | struct nvme_ctrl *ctrl = &dev->ctrl; |
| 1180 | struct nvme_command c; |
| 1181 | int flags = NVME_QUEUE_PHYS_CONTIG; |
| 1182 | |
| 1183 | /* |
| 1184 | * Some drives have a bug that auto-enables WRRU if MEDIUM isn't |
| 1185 | * set. Since URGENT priority is zeroes, it makes all queues |
| 1186 | * URGENT. |
| 1187 | */ |
| 1188 | if (ctrl->quirks & NVME_QUIRK_MEDIUM_PRIO_SQ) |
| 1189 | flags |= NVME_SQ_PRIO_MEDIUM; |
| 1190 | |
| 1191 | /* |
| 1192 | * Note: we (ab)use the fact that the prp fields survive if no data |
| 1193 | * is attached to the request. |
| 1194 | */ |
| 1195 | memset(&c, 0, sizeof(c)); |
| 1196 | c.create_sq.opcode = nvme_admin_create_sq; |
| 1197 | c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr); |
| 1198 | c.create_sq.sqid = cpu_to_le16(qid); |
| 1199 | c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1); |
| 1200 | c.create_sq.sq_flags = cpu_to_le16(flags); |
| 1201 | c.create_sq.cqid = cpu_to_le16(qid); |
| 1202 | |
| 1203 | return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0); |
| 1204 | } |
| 1205 | |
| 1206 | static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid) |
| 1207 | { |
| 1208 | return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid); |
| 1209 | } |
| 1210 | |
| 1211 | static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid) |
| 1212 | { |
| 1213 | return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid); |
| 1214 | } |
| 1215 | |
| 1216 | static void abort_endio(struct request *req, blk_status_t error) |
| 1217 | { |
| 1218 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 1219 | struct nvme_queue *nvmeq = iod->nvmeq; |
| 1220 | |
| 1221 | dev_warn(nvmeq->dev->ctrl.device, |
| 1222 | "Abort status: 0x%x", nvme_req(req)->status); |
| 1223 | atomic_inc(&nvmeq->dev->ctrl.abort_limit); |
| 1224 | blk_mq_free_request(req); |
| 1225 | } |
| 1226 | |
| 1227 | static bool nvme_should_reset(struct nvme_dev *dev, u32 csts) |
| 1228 | { |
| 1229 | |
| 1230 | /* If true, indicates loss of adapter communication, possibly by a |
| 1231 | * NVMe Subsystem reset. |
| 1232 | */ |
| 1233 | bool nssro = dev->subsystem && (csts & NVME_CSTS_NSSRO); |
| 1234 | |
| 1235 | /* If there is a reset/reinit ongoing, we shouldn't reset again. */ |
| 1236 | switch (dev->ctrl.state) { |
| 1237 | case NVME_CTRL_RESETTING: |
| 1238 | case NVME_CTRL_CONNECTING: |
| 1239 | return false; |
| 1240 | default: |
| 1241 | break; |
| 1242 | } |
| 1243 | |
| 1244 | /* We shouldn't reset unless the controller is on fatal error state |
| 1245 | * _or_ if we lost the communication with it. |
| 1246 | */ |
| 1247 | if (!(csts & NVME_CSTS_CFS) && !nssro) |
| 1248 | return false; |
| 1249 | |
| 1250 | return true; |
| 1251 | } |
| 1252 | |
| 1253 | static void nvme_warn_reset(struct nvme_dev *dev, u32 csts) |
| 1254 | { |
| 1255 | /* Read a config register to help see what died. */ |
| 1256 | u16 pci_status; |
| 1257 | int result; |
| 1258 | |
| 1259 | result = pci_read_config_word(to_pci_dev(dev->dev), PCI_STATUS, |
| 1260 | &pci_status); |
| 1261 | if (result == PCIBIOS_SUCCESSFUL) |
| 1262 | dev_warn(dev->ctrl.device, |
| 1263 | "controller is down; will reset: CSTS=0x%x, PCI_STATUS=0x%hx\n", |
| 1264 | csts, pci_status); |
| 1265 | else |
| 1266 | dev_warn(dev->ctrl.device, |
| 1267 | "controller is down; will reset: CSTS=0x%x, PCI_STATUS read failed (%d)\n", |
| 1268 | csts, result); |
| 1269 | } |
| 1270 | |
| 1271 | static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved) |
| 1272 | { |
| 1273 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 1274 | struct nvme_queue *nvmeq = iod->nvmeq; |
| 1275 | struct nvme_dev *dev = nvmeq->dev; |
| 1276 | struct request *abort_req; |
| 1277 | struct nvme_command cmd; |
| 1278 | u32 csts = readl(dev->bar + NVME_REG_CSTS); |
| 1279 | |
| 1280 | /* If PCI error recovery process is happening, we cannot reset or |
| 1281 | * the recovery mechanism will surely fail. |
| 1282 | */ |
| 1283 | mb(); |
| 1284 | if (pci_channel_offline(to_pci_dev(dev->dev))) |
| 1285 | return BLK_EH_RESET_TIMER; |
| 1286 | |
| 1287 | /* |
| 1288 | * Reset immediately if the controller is failed |
| 1289 | */ |
| 1290 | if (nvme_should_reset(dev, csts)) { |
| 1291 | nvme_warn_reset(dev, csts); |
| 1292 | nvme_dev_disable(dev, false); |
| 1293 | nvme_reset_ctrl(&dev->ctrl); |
| 1294 | return BLK_EH_DONE; |
| 1295 | } |
| 1296 | |
| 1297 | /* |
| 1298 | * Did we miss an interrupt? |
| 1299 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1300 | if (nvme_poll_irqdisable(nvmeq, req->tag)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1301 | dev_warn(dev->ctrl.device, |
| 1302 | "I/O %d QID %d timeout, completion polled\n", |
| 1303 | req->tag, nvmeq->qid); |
| 1304 | return BLK_EH_DONE; |
| 1305 | } |
| 1306 | |
| 1307 | /* |
| 1308 | * Shutdown immediately if controller times out while starting. The |
| 1309 | * reset work will see the pci device disabled when it gets the forced |
| 1310 | * cancellation error. All outstanding requests are completed on |
| 1311 | * shutdown, so we return BLK_EH_DONE. |
| 1312 | */ |
| 1313 | switch (dev->ctrl.state) { |
| 1314 | case NVME_CTRL_CONNECTING: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1315 | nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING); |
| 1316 | /* fall through */ |
| 1317 | case NVME_CTRL_DELETING: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1318 | dev_warn_ratelimited(dev->ctrl.device, |
| 1319 | "I/O %d QID %d timeout, disable controller\n", |
| 1320 | req->tag, nvmeq->qid); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1321 | nvme_req(req)->flags |= NVME_REQ_CANCELLED; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1322 | nvme_dev_disable(dev, true); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1323 | return BLK_EH_DONE; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1324 | case NVME_CTRL_RESETTING: |
| 1325 | return BLK_EH_RESET_TIMER; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1326 | default: |
| 1327 | break; |
| 1328 | } |
| 1329 | |
| 1330 | /* |
| 1331 | * Shutdown the controller immediately and schedule a reset if the |
| 1332 | * command was already aborted once before and still hasn't been |
| 1333 | * returned to the driver, or if this is the admin queue. |
| 1334 | */ |
| 1335 | if (!nvmeq->qid || iod->aborted) { |
| 1336 | dev_warn(dev->ctrl.device, |
| 1337 | "I/O %d QID %d timeout, reset controller\n", |
| 1338 | req->tag, nvmeq->qid); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1339 | nvme_req(req)->flags |= NVME_REQ_CANCELLED; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1340 | nvme_dev_disable(dev, false); |
| 1341 | nvme_reset_ctrl(&dev->ctrl); |
| 1342 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1343 | return BLK_EH_DONE; |
| 1344 | } |
| 1345 | |
| 1346 | if (atomic_dec_return(&dev->ctrl.abort_limit) < 0) { |
| 1347 | atomic_inc(&dev->ctrl.abort_limit); |
| 1348 | return BLK_EH_RESET_TIMER; |
| 1349 | } |
| 1350 | iod->aborted = 1; |
| 1351 | |
| 1352 | memset(&cmd, 0, sizeof(cmd)); |
| 1353 | cmd.abort.opcode = nvme_admin_abort_cmd; |
| 1354 | cmd.abort.cid = req->tag; |
| 1355 | cmd.abort.sqid = cpu_to_le16(nvmeq->qid); |
| 1356 | |
| 1357 | dev_warn(nvmeq->dev->ctrl.device, |
| 1358 | "I/O %d QID %d timeout, aborting\n", |
| 1359 | req->tag, nvmeq->qid); |
| 1360 | |
| 1361 | abort_req = nvme_alloc_request(dev->ctrl.admin_q, &cmd, |
| 1362 | BLK_MQ_REQ_NOWAIT, NVME_QID_ANY); |
| 1363 | if (IS_ERR(abort_req)) { |
| 1364 | atomic_inc(&dev->ctrl.abort_limit); |
| 1365 | return BLK_EH_RESET_TIMER; |
| 1366 | } |
| 1367 | |
| 1368 | abort_req->timeout = ADMIN_TIMEOUT; |
| 1369 | abort_req->end_io_data = NULL; |
| 1370 | blk_execute_rq_nowait(abort_req->q, NULL, abort_req, 0, abort_endio); |
| 1371 | |
| 1372 | /* |
| 1373 | * The aborted req will be completed on receiving the abort req. |
| 1374 | * We enable the timer again. If hit twice, it'll cause a device reset, |
| 1375 | * as the device then is in a faulty state. |
| 1376 | */ |
| 1377 | return BLK_EH_RESET_TIMER; |
| 1378 | } |
| 1379 | |
| 1380 | static void nvme_free_queue(struct nvme_queue *nvmeq) |
| 1381 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1382 | dma_free_coherent(nvmeq->dev->dev, CQ_SIZE(nvmeq), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1383 | (void *)nvmeq->cqes, nvmeq->cq_dma_addr); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1384 | if (!nvmeq->sq_cmds) |
| 1385 | return; |
| 1386 | |
| 1387 | if (test_and_clear_bit(NVMEQ_SQ_CMB, &nvmeq->flags)) { |
| 1388 | pci_free_p2pmem(to_pci_dev(nvmeq->dev->dev), |
| 1389 | nvmeq->sq_cmds, SQ_SIZE(nvmeq)); |
| 1390 | } else { |
| 1391 | dma_free_coherent(nvmeq->dev->dev, SQ_SIZE(nvmeq), |
| 1392 | nvmeq->sq_cmds, nvmeq->sq_dma_addr); |
| 1393 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | static void nvme_free_queues(struct nvme_dev *dev, int lowest) |
| 1397 | { |
| 1398 | int i; |
| 1399 | |
| 1400 | for (i = dev->ctrl.queue_count - 1; i >= lowest; i--) { |
| 1401 | dev->ctrl.queue_count--; |
| 1402 | nvme_free_queue(&dev->queues[i]); |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | /** |
| 1407 | * nvme_suspend_queue - put queue into suspended state |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1408 | * @nvmeq: queue to suspend |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1409 | */ |
| 1410 | static int nvme_suspend_queue(struct nvme_queue *nvmeq) |
| 1411 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1412 | if (!test_and_clear_bit(NVMEQ_ENABLED, &nvmeq->flags)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1413 | return 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1414 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1415 | /* ensure that nvme_queue_rq() sees NVMEQ_ENABLED cleared */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1416 | mb(); |
| 1417 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1418 | nvmeq->dev->online_queues--; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1419 | if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q) |
| 1420 | blk_mq_quiesce_queue(nvmeq->dev->ctrl.admin_q); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1421 | if (!test_and_clear_bit(NVMEQ_POLLED, &nvmeq->flags)) |
| 1422 | pci_free_irq(to_pci_dev(nvmeq->dev->dev), nvmeq->cq_vector, nvmeq); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1423 | return 0; |
| 1424 | } |
| 1425 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1426 | static void nvme_suspend_io_queues(struct nvme_dev *dev) |
| 1427 | { |
| 1428 | int i; |
| 1429 | |
| 1430 | for (i = dev->ctrl.queue_count - 1; i > 0; i--) |
| 1431 | nvme_suspend_queue(&dev->queues[i]); |
| 1432 | } |
| 1433 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1434 | static void nvme_disable_admin_queue(struct nvme_dev *dev, bool shutdown) |
| 1435 | { |
| 1436 | struct nvme_queue *nvmeq = &dev->queues[0]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1437 | |
| 1438 | if (shutdown) |
| 1439 | nvme_shutdown_ctrl(&dev->ctrl); |
| 1440 | else |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1441 | nvme_disable_ctrl(&dev->ctrl); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1442 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1443 | nvme_poll_irqdisable(nvmeq, -1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1446 | /* |
| 1447 | * Called only on a device that has been disabled and after all other threads |
| 1448 | * that can check this device's completion queues have synced. This is the |
| 1449 | * last chance for the driver to see a natural completion before |
| 1450 | * nvme_cancel_request() terminates all incomplete requests. |
| 1451 | */ |
| 1452 | static void nvme_reap_pending_cqes(struct nvme_dev *dev) |
| 1453 | { |
| 1454 | u16 start, end; |
| 1455 | int i; |
| 1456 | |
| 1457 | for (i = dev->ctrl.queue_count - 1; i > 0; i--) { |
| 1458 | nvme_process_cq(&dev->queues[i], &start, &end, -1); |
| 1459 | nvme_complete_cqes(&dev->queues[i], start, end); |
| 1460 | } |
| 1461 | } |
| 1462 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1463 | static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues, |
| 1464 | int entry_size) |
| 1465 | { |
| 1466 | int q_depth = dev->q_depth; |
| 1467 | unsigned q_size_aligned = roundup(q_depth * entry_size, |
| 1468 | dev->ctrl.page_size); |
| 1469 | |
| 1470 | if (q_size_aligned * nr_io_queues > dev->cmb_size) { |
| 1471 | u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues); |
| 1472 | mem_per_q = round_down(mem_per_q, dev->ctrl.page_size); |
| 1473 | q_depth = div_u64(mem_per_q, entry_size); |
| 1474 | |
| 1475 | /* |
| 1476 | * Ensure the reduced q_depth is above some threshold where it |
| 1477 | * would be better to map queues in system memory with the |
| 1478 | * original depth |
| 1479 | */ |
| 1480 | if (q_depth < 64) |
| 1481 | return -ENOMEM; |
| 1482 | } |
| 1483 | |
| 1484 | return q_depth; |
| 1485 | } |
| 1486 | |
| 1487 | static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1488 | int qid) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1489 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1490 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1491 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1492 | if (qid && dev->cmb_use_sqes && (dev->cmbsz & NVME_CMBSZ_SQS)) { |
| 1493 | nvmeq->sq_cmds = pci_alloc_p2pmem(pdev, SQ_SIZE(nvmeq)); |
| 1494 | if (nvmeq->sq_cmds) { |
| 1495 | nvmeq->sq_dma_addr = pci_p2pmem_virt_to_bus(pdev, |
| 1496 | nvmeq->sq_cmds); |
| 1497 | if (nvmeq->sq_dma_addr) { |
| 1498 | set_bit(NVMEQ_SQ_CMB, &nvmeq->flags); |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
| 1502 | pci_free_p2pmem(pdev, nvmeq->sq_cmds, SQ_SIZE(nvmeq)); |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(nvmeq), |
| 1507 | &nvmeq->sq_dma_addr, GFP_KERNEL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1508 | if (!nvmeq->sq_cmds) |
| 1509 | return -ENOMEM; |
| 1510 | return 0; |
| 1511 | } |
| 1512 | |
| 1513 | static int nvme_alloc_queue(struct nvme_dev *dev, int qid, int depth) |
| 1514 | { |
| 1515 | struct nvme_queue *nvmeq = &dev->queues[qid]; |
| 1516 | |
| 1517 | if (dev->ctrl.queue_count > qid) |
| 1518 | return 0; |
| 1519 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1520 | nvmeq->sqes = qid ? dev->io_sqes : NVME_ADM_SQES; |
| 1521 | nvmeq->q_depth = depth; |
| 1522 | nvmeq->cqes = dma_alloc_coherent(dev->dev, CQ_SIZE(nvmeq), |
| 1523 | &nvmeq->cq_dma_addr, GFP_KERNEL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1524 | if (!nvmeq->cqes) |
| 1525 | goto free_nvmeq; |
| 1526 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1527 | if (nvme_alloc_sq_cmds(dev, nvmeq, qid)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1528 | goto free_cqdma; |
| 1529 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1530 | nvmeq->dev = dev; |
| 1531 | spin_lock_init(&nvmeq->sq_lock); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1532 | spin_lock_init(&nvmeq->cq_poll_lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1533 | nvmeq->cq_head = 0; |
| 1534 | nvmeq->cq_phase = 1; |
| 1535 | nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1536 | nvmeq->qid = qid; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1537 | dev->ctrl.queue_count++; |
| 1538 | |
| 1539 | return 0; |
| 1540 | |
| 1541 | free_cqdma: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1542 | dma_free_coherent(dev->dev, CQ_SIZE(nvmeq), (void *)nvmeq->cqes, |
| 1543 | nvmeq->cq_dma_addr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1544 | free_nvmeq: |
| 1545 | return -ENOMEM; |
| 1546 | } |
| 1547 | |
| 1548 | static int queue_request_irq(struct nvme_queue *nvmeq) |
| 1549 | { |
| 1550 | struct pci_dev *pdev = to_pci_dev(nvmeq->dev->dev); |
| 1551 | int nr = nvmeq->dev->ctrl.instance; |
| 1552 | |
| 1553 | if (use_threaded_interrupts) { |
| 1554 | return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq_check, |
| 1555 | nvme_irq, nvmeq, "nvme%dq%d", nr, nvmeq->qid); |
| 1556 | } else { |
| 1557 | return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq, |
| 1558 | NULL, nvmeq, "nvme%dq%d", nr, nvmeq->qid); |
| 1559 | } |
| 1560 | } |
| 1561 | |
| 1562 | static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid) |
| 1563 | { |
| 1564 | struct nvme_dev *dev = nvmeq->dev; |
| 1565 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1566 | nvmeq->sq_tail = 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1567 | nvmeq->last_sq_tail = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1568 | nvmeq->cq_head = 0; |
| 1569 | nvmeq->cq_phase = 1; |
| 1570 | nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride]; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1571 | memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1572 | nvme_dbbuf_init(dev, nvmeq, qid); |
| 1573 | dev->online_queues++; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1574 | wmb(); /* ensure the first interrupt sees the initialization */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1577 | static int nvme_create_queue(struct nvme_queue *nvmeq, int qid, bool polled) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1578 | { |
| 1579 | struct nvme_dev *dev = nvmeq->dev; |
| 1580 | int result; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1581 | u16 vector = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1582 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1583 | clear_bit(NVMEQ_DELETE_ERROR, &nvmeq->flags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1584 | |
| 1585 | /* |
| 1586 | * A queue's vector matches the queue identifier unless the controller |
| 1587 | * has only one vector available. |
| 1588 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1589 | if (!polled) |
| 1590 | vector = dev->num_vecs == 1 ? 0 : qid; |
| 1591 | else |
| 1592 | set_bit(NVMEQ_POLLED, &nvmeq->flags); |
| 1593 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1594 | result = adapter_alloc_cq(dev, qid, nvmeq, vector); |
| 1595 | if (result) |
| 1596 | return result; |
| 1597 | |
| 1598 | result = adapter_alloc_sq(dev, qid, nvmeq); |
| 1599 | if (result < 0) |
| 1600 | return result; |
| 1601 | else if (result) |
| 1602 | goto release_cq; |
| 1603 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1604 | nvmeq->cq_vector = vector; |
| 1605 | nvme_init_queue(nvmeq, qid); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1606 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1607 | if (!polled) { |
| 1608 | result = queue_request_irq(nvmeq); |
| 1609 | if (result < 0) |
| 1610 | goto release_sq; |
| 1611 | } |
| 1612 | |
| 1613 | set_bit(NVMEQ_ENABLED, &nvmeq->flags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1614 | return result; |
| 1615 | |
| 1616 | release_sq: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1617 | dev->online_queues--; |
| 1618 | adapter_delete_sq(dev, qid); |
| 1619 | release_cq: |
| 1620 | adapter_delete_cq(dev, qid); |
| 1621 | return result; |
| 1622 | } |
| 1623 | |
| 1624 | static const struct blk_mq_ops nvme_mq_admin_ops = { |
| 1625 | .queue_rq = nvme_queue_rq, |
| 1626 | .complete = nvme_pci_complete_rq, |
| 1627 | .init_hctx = nvme_admin_init_hctx, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1628 | .init_request = nvme_init_request, |
| 1629 | .timeout = nvme_timeout, |
| 1630 | }; |
| 1631 | |
| 1632 | static const struct blk_mq_ops nvme_mq_ops = { |
| 1633 | .queue_rq = nvme_queue_rq, |
| 1634 | .complete = nvme_pci_complete_rq, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1635 | .commit_rqs = nvme_commit_rqs, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1636 | .init_hctx = nvme_init_hctx, |
| 1637 | .init_request = nvme_init_request, |
| 1638 | .map_queues = nvme_pci_map_queues, |
| 1639 | .timeout = nvme_timeout, |
| 1640 | .poll = nvme_poll, |
| 1641 | }; |
| 1642 | |
| 1643 | static void nvme_dev_remove_admin(struct nvme_dev *dev) |
| 1644 | { |
| 1645 | if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) { |
| 1646 | /* |
| 1647 | * If the controller was reset during removal, it's possible |
| 1648 | * user requests may be waiting on a stopped queue. Start the |
| 1649 | * queue to flush these to completion. |
| 1650 | */ |
| 1651 | blk_mq_unquiesce_queue(dev->ctrl.admin_q); |
| 1652 | blk_cleanup_queue(dev->ctrl.admin_q); |
| 1653 | blk_mq_free_tag_set(&dev->admin_tagset); |
| 1654 | } |
| 1655 | } |
| 1656 | |
| 1657 | static int nvme_alloc_admin_tags(struct nvme_dev *dev) |
| 1658 | { |
| 1659 | if (!dev->ctrl.admin_q) { |
| 1660 | dev->admin_tagset.ops = &nvme_mq_admin_ops; |
| 1661 | dev->admin_tagset.nr_hw_queues = 1; |
| 1662 | |
| 1663 | dev->admin_tagset.queue_depth = NVME_AQ_MQ_TAG_DEPTH; |
| 1664 | dev->admin_tagset.timeout = ADMIN_TIMEOUT; |
| 1665 | dev->admin_tagset.numa_node = dev_to_node(dev->dev); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1666 | dev->admin_tagset.cmd_size = sizeof(struct nvme_iod); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1667 | dev->admin_tagset.flags = BLK_MQ_F_NO_SCHED; |
| 1668 | dev->admin_tagset.driver_data = dev; |
| 1669 | |
| 1670 | if (blk_mq_alloc_tag_set(&dev->admin_tagset)) |
| 1671 | return -ENOMEM; |
| 1672 | dev->ctrl.admin_tagset = &dev->admin_tagset; |
| 1673 | |
| 1674 | dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset); |
| 1675 | if (IS_ERR(dev->ctrl.admin_q)) { |
| 1676 | blk_mq_free_tag_set(&dev->admin_tagset); |
| 1677 | return -ENOMEM; |
| 1678 | } |
| 1679 | if (!blk_get_queue(dev->ctrl.admin_q)) { |
| 1680 | nvme_dev_remove_admin(dev); |
| 1681 | dev->ctrl.admin_q = NULL; |
| 1682 | return -ENODEV; |
| 1683 | } |
| 1684 | } else |
| 1685 | blk_mq_unquiesce_queue(dev->ctrl.admin_q); |
| 1686 | |
| 1687 | return 0; |
| 1688 | } |
| 1689 | |
| 1690 | static unsigned long db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues) |
| 1691 | { |
| 1692 | return NVME_REG_DBS + ((nr_io_queues + 1) * 8 * dev->db_stride); |
| 1693 | } |
| 1694 | |
| 1695 | static int nvme_remap_bar(struct nvme_dev *dev, unsigned long size) |
| 1696 | { |
| 1697 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
| 1698 | |
| 1699 | if (size <= dev->bar_mapped_size) |
| 1700 | return 0; |
| 1701 | if (size > pci_resource_len(pdev, 0)) |
| 1702 | return -ENOMEM; |
| 1703 | if (dev->bar) |
| 1704 | iounmap(dev->bar); |
| 1705 | dev->bar = ioremap(pci_resource_start(pdev, 0), size); |
| 1706 | if (!dev->bar) { |
| 1707 | dev->bar_mapped_size = 0; |
| 1708 | return -ENOMEM; |
| 1709 | } |
| 1710 | dev->bar_mapped_size = size; |
| 1711 | dev->dbs = dev->bar + NVME_REG_DBS; |
| 1712 | |
| 1713 | return 0; |
| 1714 | } |
| 1715 | |
| 1716 | static int nvme_pci_configure_admin_queue(struct nvme_dev *dev) |
| 1717 | { |
| 1718 | int result; |
| 1719 | u32 aqa; |
| 1720 | struct nvme_queue *nvmeq; |
| 1721 | |
| 1722 | result = nvme_remap_bar(dev, db_bar_size(dev, 0)); |
| 1723 | if (result < 0) |
| 1724 | return result; |
| 1725 | |
| 1726 | dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1, 0) ? |
| 1727 | NVME_CAP_NSSRC(dev->ctrl.cap) : 0; |
| 1728 | |
| 1729 | if (dev->subsystem && |
| 1730 | (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO)) |
| 1731 | writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS); |
| 1732 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1733 | result = nvme_disable_ctrl(&dev->ctrl); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1734 | if (result < 0) |
| 1735 | return result; |
| 1736 | |
| 1737 | result = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH); |
| 1738 | if (result) |
| 1739 | return result; |
| 1740 | |
| 1741 | nvmeq = &dev->queues[0]; |
| 1742 | aqa = nvmeq->q_depth - 1; |
| 1743 | aqa |= aqa << 16; |
| 1744 | |
| 1745 | writel(aqa, dev->bar + NVME_REG_AQA); |
| 1746 | lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ); |
| 1747 | lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ); |
| 1748 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1749 | result = nvme_enable_ctrl(&dev->ctrl); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1750 | if (result) |
| 1751 | return result; |
| 1752 | |
| 1753 | nvmeq->cq_vector = 0; |
| 1754 | nvme_init_queue(nvmeq, 0); |
| 1755 | result = queue_request_irq(nvmeq); |
| 1756 | if (result) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1757 | dev->online_queues--; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1758 | return result; |
| 1759 | } |
| 1760 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1761 | set_bit(NVMEQ_ENABLED, &nvmeq->flags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1762 | return result; |
| 1763 | } |
| 1764 | |
| 1765 | static int nvme_create_io_queues(struct nvme_dev *dev) |
| 1766 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1767 | unsigned i, max, rw_queues; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1768 | int ret = 0; |
| 1769 | |
| 1770 | for (i = dev->ctrl.queue_count; i <= dev->max_qid; i++) { |
| 1771 | if (nvme_alloc_queue(dev, i, dev->q_depth)) { |
| 1772 | ret = -ENOMEM; |
| 1773 | break; |
| 1774 | } |
| 1775 | } |
| 1776 | |
| 1777 | max = min(dev->max_qid, dev->ctrl.queue_count - 1); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1778 | if (max != 1 && dev->io_queues[HCTX_TYPE_POLL]) { |
| 1779 | rw_queues = dev->io_queues[HCTX_TYPE_DEFAULT] + |
| 1780 | dev->io_queues[HCTX_TYPE_READ]; |
| 1781 | } else { |
| 1782 | rw_queues = max; |
| 1783 | } |
| 1784 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1785 | for (i = dev->online_queues; i <= max; i++) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1786 | bool polled = i > rw_queues; |
| 1787 | |
| 1788 | ret = nvme_create_queue(&dev->queues[i], i, polled); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1789 | if (ret) |
| 1790 | break; |
| 1791 | } |
| 1792 | |
| 1793 | /* |
| 1794 | * Ignore failing Create SQ/CQ commands, we can continue with less |
| 1795 | * than the desired amount of queues, and even a controller without |
| 1796 | * I/O queues can still be used to issue admin commands. This might |
| 1797 | * be useful to upgrade a buggy firmware for example. |
| 1798 | */ |
| 1799 | return ret >= 0 ? 0 : ret; |
| 1800 | } |
| 1801 | |
| 1802 | static ssize_t nvme_cmb_show(struct device *dev, |
| 1803 | struct device_attribute *attr, |
| 1804 | char *buf) |
| 1805 | { |
| 1806 | struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev)); |
| 1807 | |
| 1808 | return scnprintf(buf, PAGE_SIZE, "cmbloc : x%08x\ncmbsz : x%08x\n", |
| 1809 | ndev->cmbloc, ndev->cmbsz); |
| 1810 | } |
| 1811 | static DEVICE_ATTR(cmb, S_IRUGO, nvme_cmb_show, NULL); |
| 1812 | |
| 1813 | static u64 nvme_cmb_size_unit(struct nvme_dev *dev) |
| 1814 | { |
| 1815 | u8 szu = (dev->cmbsz >> NVME_CMBSZ_SZU_SHIFT) & NVME_CMBSZ_SZU_MASK; |
| 1816 | |
| 1817 | return 1ULL << (12 + 4 * szu); |
| 1818 | } |
| 1819 | |
| 1820 | static u32 nvme_cmb_size(struct nvme_dev *dev) |
| 1821 | { |
| 1822 | return (dev->cmbsz >> NVME_CMBSZ_SZ_SHIFT) & NVME_CMBSZ_SZ_MASK; |
| 1823 | } |
| 1824 | |
| 1825 | static void nvme_map_cmb(struct nvme_dev *dev) |
| 1826 | { |
| 1827 | u64 size, offset; |
| 1828 | resource_size_t bar_size; |
| 1829 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
| 1830 | int bar; |
| 1831 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1832 | if (dev->cmb_size) |
| 1833 | return; |
| 1834 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1835 | dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ); |
| 1836 | if (!dev->cmbsz) |
| 1837 | return; |
| 1838 | dev->cmbloc = readl(dev->bar + NVME_REG_CMBLOC); |
| 1839 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1840 | size = nvme_cmb_size_unit(dev) * nvme_cmb_size(dev); |
| 1841 | offset = nvme_cmb_size_unit(dev) * NVME_CMB_OFST(dev->cmbloc); |
| 1842 | bar = NVME_CMB_BIR(dev->cmbloc); |
| 1843 | bar_size = pci_resource_len(pdev, bar); |
| 1844 | |
| 1845 | if (offset > bar_size) |
| 1846 | return; |
| 1847 | |
| 1848 | /* |
| 1849 | * Controllers may support a CMB size larger than their BAR, |
| 1850 | * for example, due to being behind a bridge. Reduce the CMB to |
| 1851 | * the reported size of the BAR |
| 1852 | */ |
| 1853 | if (size > bar_size - offset) |
| 1854 | size = bar_size - offset; |
| 1855 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1856 | if (pci_p2pdma_add_resource(pdev, bar, size, offset)) { |
| 1857 | dev_warn(dev->ctrl.device, |
| 1858 | "failed to register the CMB\n"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1859 | return; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1862 | dev->cmb_size = size; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1863 | dev->cmb_use_sqes = use_cmb_sqes && (dev->cmbsz & NVME_CMBSZ_SQS); |
| 1864 | |
| 1865 | if ((dev->cmbsz & (NVME_CMBSZ_WDS | NVME_CMBSZ_RDS)) == |
| 1866 | (NVME_CMBSZ_WDS | NVME_CMBSZ_RDS)) |
| 1867 | pci_p2pmem_publish(pdev, true); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1868 | |
| 1869 | if (sysfs_add_file_to_group(&dev->ctrl.device->kobj, |
| 1870 | &dev_attr_cmb.attr, NULL)) |
| 1871 | dev_warn(dev->ctrl.device, |
| 1872 | "failed to add sysfs attribute for CMB\n"); |
| 1873 | } |
| 1874 | |
| 1875 | static inline void nvme_release_cmb(struct nvme_dev *dev) |
| 1876 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1877 | if (dev->cmb_size) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1878 | sysfs_remove_file_from_group(&dev->ctrl.device->kobj, |
| 1879 | &dev_attr_cmb.attr, NULL); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1880 | dev->cmb_size = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1881 | } |
| 1882 | } |
| 1883 | |
| 1884 | static int nvme_set_host_mem(struct nvme_dev *dev, u32 bits) |
| 1885 | { |
| 1886 | u64 dma_addr = dev->host_mem_descs_dma; |
| 1887 | struct nvme_command c; |
| 1888 | int ret; |
| 1889 | |
| 1890 | memset(&c, 0, sizeof(c)); |
| 1891 | c.features.opcode = nvme_admin_set_features; |
| 1892 | c.features.fid = cpu_to_le32(NVME_FEAT_HOST_MEM_BUF); |
| 1893 | c.features.dword11 = cpu_to_le32(bits); |
| 1894 | c.features.dword12 = cpu_to_le32(dev->host_mem_size >> |
| 1895 | ilog2(dev->ctrl.page_size)); |
| 1896 | c.features.dword13 = cpu_to_le32(lower_32_bits(dma_addr)); |
| 1897 | c.features.dword14 = cpu_to_le32(upper_32_bits(dma_addr)); |
| 1898 | c.features.dword15 = cpu_to_le32(dev->nr_host_mem_descs); |
| 1899 | |
| 1900 | ret = nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0); |
| 1901 | if (ret) { |
| 1902 | dev_warn(dev->ctrl.device, |
| 1903 | "failed to set host mem (err %d, flags %#x).\n", |
| 1904 | ret, bits); |
| 1905 | } |
| 1906 | return ret; |
| 1907 | } |
| 1908 | |
| 1909 | static void nvme_free_host_mem(struct nvme_dev *dev) |
| 1910 | { |
| 1911 | int i; |
| 1912 | |
| 1913 | for (i = 0; i < dev->nr_host_mem_descs; i++) { |
| 1914 | struct nvme_host_mem_buf_desc *desc = &dev->host_mem_descs[i]; |
| 1915 | size_t size = le32_to_cpu(desc->size) * dev->ctrl.page_size; |
| 1916 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1917 | dma_free_attrs(dev->dev, size, dev->host_mem_desc_bufs[i], |
| 1918 | le64_to_cpu(desc->addr), |
| 1919 | DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | kfree(dev->host_mem_desc_bufs); |
| 1923 | dev->host_mem_desc_bufs = NULL; |
| 1924 | dma_free_coherent(dev->dev, |
| 1925 | dev->nr_host_mem_descs * sizeof(*dev->host_mem_descs), |
| 1926 | dev->host_mem_descs, dev->host_mem_descs_dma); |
| 1927 | dev->host_mem_descs = NULL; |
| 1928 | dev->nr_host_mem_descs = 0; |
| 1929 | } |
| 1930 | |
| 1931 | static int __nvme_alloc_host_mem(struct nvme_dev *dev, u64 preferred, |
| 1932 | u32 chunk_size) |
| 1933 | { |
| 1934 | struct nvme_host_mem_buf_desc *descs; |
| 1935 | u32 max_entries, len; |
| 1936 | dma_addr_t descs_dma; |
| 1937 | int i = 0; |
| 1938 | void **bufs; |
| 1939 | u64 size, tmp; |
| 1940 | |
| 1941 | tmp = (preferred + chunk_size - 1); |
| 1942 | do_div(tmp, chunk_size); |
| 1943 | max_entries = tmp; |
| 1944 | |
| 1945 | if (dev->ctrl.hmmaxd && dev->ctrl.hmmaxd < max_entries) |
| 1946 | max_entries = dev->ctrl.hmmaxd; |
| 1947 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1948 | descs = dma_alloc_coherent(dev->dev, max_entries * sizeof(*descs), |
| 1949 | &descs_dma, GFP_KERNEL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1950 | if (!descs) |
| 1951 | goto out; |
| 1952 | |
| 1953 | bufs = kcalloc(max_entries, sizeof(*bufs), GFP_KERNEL); |
| 1954 | if (!bufs) |
| 1955 | goto out_free_descs; |
| 1956 | |
| 1957 | for (size = 0; size < preferred && i < max_entries; size += len) { |
| 1958 | dma_addr_t dma_addr; |
| 1959 | |
| 1960 | len = min_t(u64, chunk_size, preferred - size); |
| 1961 | bufs[i] = dma_alloc_attrs(dev->dev, len, &dma_addr, GFP_KERNEL, |
| 1962 | DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN); |
| 1963 | if (!bufs[i]) |
| 1964 | break; |
| 1965 | |
| 1966 | descs[i].addr = cpu_to_le64(dma_addr); |
| 1967 | descs[i].size = cpu_to_le32(len / dev->ctrl.page_size); |
| 1968 | i++; |
| 1969 | } |
| 1970 | |
| 1971 | if (!size) |
| 1972 | goto out_free_bufs; |
| 1973 | |
| 1974 | dev->nr_host_mem_descs = i; |
| 1975 | dev->host_mem_size = size; |
| 1976 | dev->host_mem_descs = descs; |
| 1977 | dev->host_mem_descs_dma = descs_dma; |
| 1978 | dev->host_mem_desc_bufs = bufs; |
| 1979 | return 0; |
| 1980 | |
| 1981 | out_free_bufs: |
| 1982 | while (--i >= 0) { |
| 1983 | size_t size = le32_to_cpu(descs[i].size) * dev->ctrl.page_size; |
| 1984 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1985 | dma_free_attrs(dev->dev, size, bufs[i], |
| 1986 | le64_to_cpu(descs[i].addr), |
| 1987 | DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | kfree(bufs); |
| 1991 | out_free_descs: |
| 1992 | dma_free_coherent(dev->dev, max_entries * sizeof(*descs), descs, |
| 1993 | descs_dma); |
| 1994 | out: |
| 1995 | dev->host_mem_descs = NULL; |
| 1996 | return -ENOMEM; |
| 1997 | } |
| 1998 | |
| 1999 | static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred) |
| 2000 | { |
| 2001 | u32 chunk_size; |
| 2002 | |
| 2003 | /* start big and work our way down */ |
| 2004 | for (chunk_size = min_t(u64, preferred, PAGE_SIZE * MAX_ORDER_NR_PAGES); |
| 2005 | chunk_size >= max_t(u32, dev->ctrl.hmminds * 4096, PAGE_SIZE * 2); |
| 2006 | chunk_size /= 2) { |
| 2007 | if (!__nvme_alloc_host_mem(dev, preferred, chunk_size)) { |
| 2008 | if (!min || dev->host_mem_size >= min) |
| 2009 | return 0; |
| 2010 | nvme_free_host_mem(dev); |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | return -ENOMEM; |
| 2015 | } |
| 2016 | |
| 2017 | static int nvme_setup_host_mem(struct nvme_dev *dev) |
| 2018 | { |
| 2019 | u64 max = (u64)max_host_mem_size_mb * SZ_1M; |
| 2020 | u64 preferred = (u64)dev->ctrl.hmpre * 4096; |
| 2021 | u64 min = (u64)dev->ctrl.hmmin * 4096; |
| 2022 | u32 enable_bits = NVME_HOST_MEM_ENABLE; |
| 2023 | int ret; |
| 2024 | |
| 2025 | preferred = min(preferred, max); |
| 2026 | if (min > max) { |
| 2027 | dev_warn(dev->ctrl.device, |
| 2028 | "min host memory (%lld MiB) above limit (%d MiB).\n", |
| 2029 | min >> ilog2(SZ_1M), max_host_mem_size_mb); |
| 2030 | nvme_free_host_mem(dev); |
| 2031 | return 0; |
| 2032 | } |
| 2033 | |
| 2034 | /* |
| 2035 | * If we already have a buffer allocated check if we can reuse it. |
| 2036 | */ |
| 2037 | if (dev->host_mem_descs) { |
| 2038 | if (dev->host_mem_size >= min) |
| 2039 | enable_bits |= NVME_HOST_MEM_RETURN; |
| 2040 | else |
| 2041 | nvme_free_host_mem(dev); |
| 2042 | } |
| 2043 | |
| 2044 | if (!dev->host_mem_descs) { |
| 2045 | if (nvme_alloc_host_mem(dev, min, preferred)) { |
| 2046 | dev_warn(dev->ctrl.device, |
| 2047 | "failed to allocate host memory buffer.\n"); |
| 2048 | return 0; /* controller must work without HMB */ |
| 2049 | } |
| 2050 | |
| 2051 | dev_info(dev->ctrl.device, |
| 2052 | "allocated %lld MiB host memory buffer.\n", |
| 2053 | dev->host_mem_size >> ilog2(SZ_1M)); |
| 2054 | } |
| 2055 | |
| 2056 | ret = nvme_set_host_mem(dev, enable_bits); |
| 2057 | if (ret) |
| 2058 | nvme_free_host_mem(dev); |
| 2059 | return ret; |
| 2060 | } |
| 2061 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2062 | /* |
| 2063 | * nirqs is the number of interrupts available for write and read |
| 2064 | * queues. The core already reserved an interrupt for the admin queue. |
| 2065 | */ |
| 2066 | static void nvme_calc_irq_sets(struct irq_affinity *affd, unsigned int nrirqs) |
| 2067 | { |
| 2068 | struct nvme_dev *dev = affd->priv; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2069 | unsigned int nr_read_queues, nr_write_queues = dev->nr_write_queues; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2070 | |
| 2071 | /* |
| 2072 | * If there is no interupt available for queues, ensure that |
| 2073 | * the default queue is set to 1. The affinity set size is |
| 2074 | * also set to one, but the irq core ignores it for this case. |
| 2075 | * |
| 2076 | * If only one interrupt is available or 'write_queue' == 0, combine |
| 2077 | * write and read queues. |
| 2078 | * |
| 2079 | * If 'write_queues' > 0, ensure it leaves room for at least one read |
| 2080 | * queue. |
| 2081 | */ |
| 2082 | if (!nrirqs) { |
| 2083 | nrirqs = 1; |
| 2084 | nr_read_queues = 0; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2085 | } else if (nrirqs == 1 || !nr_write_queues) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2086 | nr_read_queues = 0; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2087 | } else if (nr_write_queues >= nrirqs) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2088 | nr_read_queues = 1; |
| 2089 | } else { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2090 | nr_read_queues = nrirqs - nr_write_queues; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | dev->io_queues[HCTX_TYPE_DEFAULT] = nrirqs - nr_read_queues; |
| 2094 | affd->set_size[HCTX_TYPE_DEFAULT] = nrirqs - nr_read_queues; |
| 2095 | dev->io_queues[HCTX_TYPE_READ] = nr_read_queues; |
| 2096 | affd->set_size[HCTX_TYPE_READ] = nr_read_queues; |
| 2097 | affd->nr_sets = nr_read_queues ? 2 : 1; |
| 2098 | } |
| 2099 | |
| 2100 | static int nvme_setup_irqs(struct nvme_dev *dev, unsigned int nr_io_queues) |
| 2101 | { |
| 2102 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
| 2103 | struct irq_affinity affd = { |
| 2104 | .pre_vectors = 1, |
| 2105 | .calc_sets = nvme_calc_irq_sets, |
| 2106 | .priv = dev, |
| 2107 | }; |
| 2108 | unsigned int irq_queues, this_p_queues; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2109 | |
| 2110 | /* |
| 2111 | * Poll queues don't need interrupts, but we need at least one IO |
| 2112 | * queue left over for non-polled IO. |
| 2113 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2114 | this_p_queues = dev->nr_poll_queues; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2115 | if (this_p_queues >= nr_io_queues) { |
| 2116 | this_p_queues = nr_io_queues - 1; |
| 2117 | irq_queues = 1; |
| 2118 | } else { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2119 | irq_queues = nr_io_queues - this_p_queues + 1; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2120 | } |
| 2121 | dev->io_queues[HCTX_TYPE_POLL] = this_p_queues; |
| 2122 | |
| 2123 | /* Initialize for the single interrupt case */ |
| 2124 | dev->io_queues[HCTX_TYPE_DEFAULT] = 1; |
| 2125 | dev->io_queues[HCTX_TYPE_READ] = 0; |
| 2126 | |
| 2127 | /* |
| 2128 | * Some Apple controllers require all queues to use the |
| 2129 | * first vector. |
| 2130 | */ |
| 2131 | if (dev->ctrl.quirks & NVME_QUIRK_SINGLE_VECTOR) |
| 2132 | irq_queues = 1; |
| 2133 | |
| 2134 | return pci_alloc_irq_vectors_affinity(pdev, 1, irq_queues, |
| 2135 | PCI_IRQ_ALL_TYPES | PCI_IRQ_AFFINITY, &affd); |
| 2136 | } |
| 2137 | |
| 2138 | static void nvme_disable_io_queues(struct nvme_dev *dev) |
| 2139 | { |
| 2140 | if (__nvme_disable_io_queues(dev, nvme_admin_delete_sq)) |
| 2141 | __nvme_disable_io_queues(dev, nvme_admin_delete_cq); |
| 2142 | } |
| 2143 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2144 | static unsigned int nvme_max_io_queues(struct nvme_dev *dev) |
| 2145 | { |
| 2146 | return num_possible_cpus() + dev->nr_write_queues + dev->nr_poll_queues; |
| 2147 | } |
| 2148 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2149 | static int nvme_setup_io_queues(struct nvme_dev *dev) |
| 2150 | { |
| 2151 | struct nvme_queue *adminq = &dev->queues[0]; |
| 2152 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2153 | unsigned int nr_io_queues; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2154 | unsigned long size; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2155 | int result; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2156 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2157 | /* |
| 2158 | * Sample the module parameters once at reset time so that we have |
| 2159 | * stable values to work with. |
| 2160 | */ |
| 2161 | dev->nr_write_queues = write_queues; |
| 2162 | dev->nr_poll_queues = poll_queues; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2163 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2164 | /* |
| 2165 | * If tags are shared with admin queue (Apple bug), then |
| 2166 | * make sure we only use one IO queue. |
| 2167 | */ |
| 2168 | if (dev->ctrl.quirks & NVME_QUIRK_SHARED_TAGS) |
| 2169 | nr_io_queues = 1; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2170 | else |
| 2171 | nr_io_queues = min(nvme_max_io_queues(dev), |
| 2172 | dev->nr_allocated_queues - 1); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2173 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2174 | result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues); |
| 2175 | if (result < 0) |
| 2176 | return result; |
| 2177 | |
| 2178 | if (nr_io_queues == 0) |
| 2179 | return 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2180 | |
| 2181 | clear_bit(NVMEQ_ENABLED, &adminq->flags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2182 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2183 | if (dev->cmb_use_sqes) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2184 | result = nvme_cmb_qdepth(dev, nr_io_queues, |
| 2185 | sizeof(struct nvme_command)); |
| 2186 | if (result > 0) |
| 2187 | dev->q_depth = result; |
| 2188 | else |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2189 | dev->cmb_use_sqes = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
| 2192 | do { |
| 2193 | size = db_bar_size(dev, nr_io_queues); |
| 2194 | result = nvme_remap_bar(dev, size); |
| 2195 | if (!result) |
| 2196 | break; |
| 2197 | if (!--nr_io_queues) |
| 2198 | return -ENOMEM; |
| 2199 | } while (1); |
| 2200 | adminq->q_db = dev->dbs; |
| 2201 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2202 | retry: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2203 | /* Deregister the admin queue's interrupt */ |
| 2204 | pci_free_irq(pdev, 0, adminq); |
| 2205 | |
| 2206 | /* |
| 2207 | * If we enable msix early due to not intx, disable it again before |
| 2208 | * setting up the full range we need. |
| 2209 | */ |
| 2210 | pci_free_irq_vectors(pdev); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2211 | |
| 2212 | result = nvme_setup_irqs(dev, nr_io_queues); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2213 | if (result <= 0) |
| 2214 | return -EIO; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2215 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2216 | dev->num_vecs = result; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2217 | result = max(result - 1, 1); |
| 2218 | dev->max_qid = result + dev->io_queues[HCTX_TYPE_POLL]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2219 | |
| 2220 | /* |
| 2221 | * Should investigate if there's a performance win from allocating |
| 2222 | * more queues than interrupt vectors; it might allow the submission |
| 2223 | * path to scale better, even if the receive path is limited by the |
| 2224 | * number of interrupts. |
| 2225 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2226 | result = queue_request_irq(adminq); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2227 | if (result) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2228 | return result; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2229 | set_bit(NVMEQ_ENABLED, &adminq->flags); |
| 2230 | |
| 2231 | result = nvme_create_io_queues(dev); |
| 2232 | if (result || dev->online_queues < 2) |
| 2233 | return result; |
| 2234 | |
| 2235 | if (dev->online_queues - 1 < dev->max_qid) { |
| 2236 | nr_io_queues = dev->online_queues - 1; |
| 2237 | nvme_disable_io_queues(dev); |
| 2238 | nvme_suspend_io_queues(dev); |
| 2239 | goto retry; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2240 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2241 | dev_info(dev->ctrl.device, "%d/%d/%d default/read/poll queues\n", |
| 2242 | dev->io_queues[HCTX_TYPE_DEFAULT], |
| 2243 | dev->io_queues[HCTX_TYPE_READ], |
| 2244 | dev->io_queues[HCTX_TYPE_POLL]); |
| 2245 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | static void nvme_del_queue_end(struct request *req, blk_status_t error) |
| 2249 | { |
| 2250 | struct nvme_queue *nvmeq = req->end_io_data; |
| 2251 | |
| 2252 | blk_mq_free_request(req); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2253 | complete(&nvmeq->delete_done); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | static void nvme_del_cq_end(struct request *req, blk_status_t error) |
| 2257 | { |
| 2258 | struct nvme_queue *nvmeq = req->end_io_data; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2259 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2260 | if (error) |
| 2261 | set_bit(NVMEQ_DELETE_ERROR, &nvmeq->flags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2262 | |
| 2263 | nvme_del_queue_end(req, error); |
| 2264 | } |
| 2265 | |
| 2266 | static int nvme_delete_queue(struct nvme_queue *nvmeq, u8 opcode) |
| 2267 | { |
| 2268 | struct request_queue *q = nvmeq->dev->ctrl.admin_q; |
| 2269 | struct request *req; |
| 2270 | struct nvme_command cmd; |
| 2271 | |
| 2272 | memset(&cmd, 0, sizeof(cmd)); |
| 2273 | cmd.delete_queue.opcode = opcode; |
| 2274 | cmd.delete_queue.qid = cpu_to_le16(nvmeq->qid); |
| 2275 | |
| 2276 | req = nvme_alloc_request(q, &cmd, BLK_MQ_REQ_NOWAIT, NVME_QID_ANY); |
| 2277 | if (IS_ERR(req)) |
| 2278 | return PTR_ERR(req); |
| 2279 | |
| 2280 | req->timeout = ADMIN_TIMEOUT; |
| 2281 | req->end_io_data = nvmeq; |
| 2282 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2283 | init_completion(&nvmeq->delete_done); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2284 | blk_execute_rq_nowait(q, NULL, req, false, |
| 2285 | opcode == nvme_admin_delete_cq ? |
| 2286 | nvme_del_cq_end : nvme_del_queue_end); |
| 2287 | return 0; |
| 2288 | } |
| 2289 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2290 | static bool __nvme_disable_io_queues(struct nvme_dev *dev, u8 opcode) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2291 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2292 | int nr_queues = dev->online_queues - 1, sent = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2293 | unsigned long timeout; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2294 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2295 | retry: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2296 | timeout = ADMIN_TIMEOUT; |
| 2297 | while (nr_queues > 0) { |
| 2298 | if (nvme_delete_queue(&dev->queues[nr_queues], opcode)) |
| 2299 | break; |
| 2300 | nr_queues--; |
| 2301 | sent++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2302 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2303 | while (sent) { |
| 2304 | struct nvme_queue *nvmeq = &dev->queues[nr_queues + sent]; |
| 2305 | |
| 2306 | timeout = wait_for_completion_io_timeout(&nvmeq->delete_done, |
| 2307 | timeout); |
| 2308 | if (timeout == 0) |
| 2309 | return false; |
| 2310 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2311 | sent--; |
| 2312 | if (nr_queues) |
| 2313 | goto retry; |
| 2314 | } |
| 2315 | return true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2318 | static void nvme_dev_add(struct nvme_dev *dev) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2319 | { |
| 2320 | int ret; |
| 2321 | |
| 2322 | if (!dev->ctrl.tagset) { |
| 2323 | dev->tagset.ops = &nvme_mq_ops; |
| 2324 | dev->tagset.nr_hw_queues = dev->online_queues - 1; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2325 | dev->tagset.nr_maps = 2; /* default + read */ |
| 2326 | if (dev->io_queues[HCTX_TYPE_POLL]) |
| 2327 | dev->tagset.nr_maps++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2328 | dev->tagset.timeout = NVME_IO_TIMEOUT; |
| 2329 | dev->tagset.numa_node = dev_to_node(dev->dev); |
| 2330 | dev->tagset.queue_depth = |
| 2331 | min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2332 | dev->tagset.cmd_size = sizeof(struct nvme_iod); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2333 | dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE; |
| 2334 | dev->tagset.driver_data = dev; |
| 2335 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2336 | /* |
| 2337 | * Some Apple controllers requires tags to be unique |
| 2338 | * across admin and IO queue, so reserve the first 32 |
| 2339 | * tags of the IO queue. |
| 2340 | */ |
| 2341 | if (dev->ctrl.quirks & NVME_QUIRK_SHARED_TAGS) |
| 2342 | dev->tagset.reserved_tags = NVME_AQ_DEPTH; |
| 2343 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2344 | ret = blk_mq_alloc_tag_set(&dev->tagset); |
| 2345 | if (ret) { |
| 2346 | dev_warn(dev->ctrl.device, |
| 2347 | "IO queues tagset allocation failed %d\n", ret); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2348 | return; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2349 | } |
| 2350 | dev->ctrl.tagset = &dev->tagset; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2351 | } else { |
| 2352 | blk_mq_update_nr_hw_queues(&dev->tagset, dev->online_queues - 1); |
| 2353 | |
| 2354 | /* Free previously allocated queues that are no longer usable */ |
| 2355 | nvme_free_queues(dev, dev->online_queues); |
| 2356 | } |
| 2357 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2358 | nvme_dbbuf_set(dev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | static int nvme_pci_enable(struct nvme_dev *dev) |
| 2362 | { |
| 2363 | int result = -ENOMEM; |
| 2364 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
| 2365 | |
| 2366 | if (pci_enable_device_mem(pdev)) |
| 2367 | return result; |
| 2368 | |
| 2369 | pci_set_master(pdev); |
| 2370 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2371 | if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2372 | goto disable; |
| 2373 | |
| 2374 | if (readl(dev->bar + NVME_REG_CSTS) == -1) { |
| 2375 | result = -ENODEV; |
| 2376 | goto disable; |
| 2377 | } |
| 2378 | |
| 2379 | /* |
| 2380 | * Some devices and/or platforms don't advertise or work with INTx |
| 2381 | * interrupts. Pre-enable a single MSIX or MSI vec for setup. We'll |
| 2382 | * adjust this later. |
| 2383 | */ |
| 2384 | result = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES); |
| 2385 | if (result < 0) |
| 2386 | return result; |
| 2387 | |
| 2388 | dev->ctrl.cap = lo_hi_readq(dev->bar + NVME_REG_CAP); |
| 2389 | |
| 2390 | dev->q_depth = min_t(int, NVME_CAP_MQES(dev->ctrl.cap) + 1, |
| 2391 | io_queue_depth); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2392 | dev->ctrl.sqsize = dev->q_depth - 1; /* 0's based queue depth */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2393 | dev->db_stride = 1 << NVME_CAP_STRIDE(dev->ctrl.cap); |
| 2394 | dev->dbs = dev->bar + 4096; |
| 2395 | |
| 2396 | /* |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2397 | * Some Apple controllers require a non-standard SQE size. |
| 2398 | * Interestingly they also seem to ignore the CC:IOSQES register |
| 2399 | * so we don't bother updating it here. |
| 2400 | */ |
| 2401 | if (dev->ctrl.quirks & NVME_QUIRK_128_BYTES_SQES) |
| 2402 | dev->io_sqes = 7; |
| 2403 | else |
| 2404 | dev->io_sqes = NVME_NVM_IOSQES; |
| 2405 | |
| 2406 | /* |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2407 | * Temporary fix for the Apple controller found in the MacBook8,1 and |
| 2408 | * some MacBook7,1 to avoid controller resets and data loss. |
| 2409 | */ |
| 2410 | if (pdev->vendor == PCI_VENDOR_ID_APPLE && pdev->device == 0x2001) { |
| 2411 | dev->q_depth = 2; |
| 2412 | dev_warn(dev->ctrl.device, "detected Apple NVMe controller, " |
| 2413 | "set queue depth=%u to work around controller resets\n", |
| 2414 | dev->q_depth); |
| 2415 | } else if (pdev->vendor == PCI_VENDOR_ID_SAMSUNG && |
| 2416 | (pdev->device == 0xa821 || pdev->device == 0xa822) && |
| 2417 | NVME_CAP_MQES(dev->ctrl.cap) == 0) { |
| 2418 | dev->q_depth = 64; |
| 2419 | dev_err(dev->ctrl.device, "detected PM1725 NVMe controller, " |
| 2420 | "set queue depth=%u\n", dev->q_depth); |
| 2421 | } |
| 2422 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2423 | /* |
| 2424 | * Controllers with the shared tags quirk need the IO queue to be |
| 2425 | * big enough so that we get 32 tags for the admin queue |
| 2426 | */ |
| 2427 | if ((dev->ctrl.quirks & NVME_QUIRK_SHARED_TAGS) && |
| 2428 | (dev->q_depth < (NVME_AQ_DEPTH + 2))) { |
| 2429 | dev->q_depth = NVME_AQ_DEPTH + 2; |
| 2430 | dev_warn(dev->ctrl.device, "IO queue depth clamped to %d\n", |
| 2431 | dev->q_depth); |
| 2432 | } |
| 2433 | |
| 2434 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2435 | nvme_map_cmb(dev); |
| 2436 | |
| 2437 | pci_enable_pcie_error_reporting(pdev); |
| 2438 | pci_save_state(pdev); |
| 2439 | return 0; |
| 2440 | |
| 2441 | disable: |
| 2442 | pci_disable_device(pdev); |
| 2443 | return result; |
| 2444 | } |
| 2445 | |
| 2446 | static void nvme_dev_unmap(struct nvme_dev *dev) |
| 2447 | { |
| 2448 | if (dev->bar) |
| 2449 | iounmap(dev->bar); |
| 2450 | pci_release_mem_regions(to_pci_dev(dev->dev)); |
| 2451 | } |
| 2452 | |
| 2453 | static void nvme_pci_disable(struct nvme_dev *dev) |
| 2454 | { |
| 2455 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
| 2456 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2457 | pci_free_irq_vectors(pdev); |
| 2458 | |
| 2459 | if (pci_is_enabled(pdev)) { |
| 2460 | pci_disable_pcie_error_reporting(pdev); |
| 2461 | pci_disable_device(pdev); |
| 2462 | } |
| 2463 | } |
| 2464 | |
| 2465 | static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown) |
| 2466 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2467 | bool dead = true, freeze = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2468 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
| 2469 | |
| 2470 | mutex_lock(&dev->shutdown_lock); |
| 2471 | if (pci_is_enabled(pdev)) { |
| 2472 | u32 csts = readl(dev->bar + NVME_REG_CSTS); |
| 2473 | |
| 2474 | if (dev->ctrl.state == NVME_CTRL_LIVE || |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2475 | dev->ctrl.state == NVME_CTRL_RESETTING) { |
| 2476 | freeze = true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2477 | nvme_start_freeze(&dev->ctrl); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2478 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2479 | dead = !!((csts & NVME_CSTS_CFS) || !(csts & NVME_CSTS_RDY) || |
| 2480 | pdev->error_state != pci_channel_io_normal); |
| 2481 | } |
| 2482 | |
| 2483 | /* |
| 2484 | * Give the controller a chance to complete all entered requests if |
| 2485 | * doing a safe shutdown. |
| 2486 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2487 | if (!dead && shutdown && freeze) |
| 2488 | nvme_wait_freeze_timeout(&dev->ctrl, NVME_IO_TIMEOUT); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2489 | |
| 2490 | nvme_stop_queues(&dev->ctrl); |
| 2491 | |
| 2492 | if (!dead && dev->ctrl.queue_count > 0) { |
| 2493 | nvme_disable_io_queues(dev); |
| 2494 | nvme_disable_admin_queue(dev, shutdown); |
| 2495 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2496 | nvme_suspend_io_queues(dev); |
| 2497 | nvme_suspend_queue(&dev->queues[0]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2498 | nvme_pci_disable(dev); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2499 | nvme_reap_pending_cqes(dev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2500 | |
| 2501 | blk_mq_tagset_busy_iter(&dev->tagset, nvme_cancel_request, &dev->ctrl); |
| 2502 | blk_mq_tagset_busy_iter(&dev->admin_tagset, nvme_cancel_request, &dev->ctrl); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2503 | blk_mq_tagset_wait_completed_request(&dev->tagset); |
| 2504 | blk_mq_tagset_wait_completed_request(&dev->admin_tagset); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2505 | |
| 2506 | /* |
| 2507 | * The driver will not be starting up queues again if shutting down so |
| 2508 | * must flush all entered requests to their failed completion to avoid |
| 2509 | * deadlocking blk-mq hot-cpu notifier. |
| 2510 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2511 | if (shutdown) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2512 | nvme_start_queues(&dev->ctrl); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2513 | if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) |
| 2514 | blk_mq_unquiesce_queue(dev->ctrl.admin_q); |
| 2515 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2516 | mutex_unlock(&dev->shutdown_lock); |
| 2517 | } |
| 2518 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2519 | static int nvme_disable_prepare_reset(struct nvme_dev *dev, bool shutdown) |
| 2520 | { |
| 2521 | if (!nvme_wait_reset(&dev->ctrl)) |
| 2522 | return -EBUSY; |
| 2523 | nvme_dev_disable(dev, shutdown); |
| 2524 | return 0; |
| 2525 | } |
| 2526 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2527 | static int nvme_setup_prp_pools(struct nvme_dev *dev) |
| 2528 | { |
| 2529 | dev->prp_page_pool = dma_pool_create("prp list page", dev->dev, |
| 2530 | PAGE_SIZE, PAGE_SIZE, 0); |
| 2531 | if (!dev->prp_page_pool) |
| 2532 | return -ENOMEM; |
| 2533 | |
| 2534 | /* Optimisation for I/Os between 4k and 128k */ |
| 2535 | dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev, |
| 2536 | 256, 256, 0); |
| 2537 | if (!dev->prp_small_pool) { |
| 2538 | dma_pool_destroy(dev->prp_page_pool); |
| 2539 | return -ENOMEM; |
| 2540 | } |
| 2541 | return 0; |
| 2542 | } |
| 2543 | |
| 2544 | static void nvme_release_prp_pools(struct nvme_dev *dev) |
| 2545 | { |
| 2546 | dma_pool_destroy(dev->prp_page_pool); |
| 2547 | dma_pool_destroy(dev->prp_small_pool); |
| 2548 | } |
| 2549 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2550 | static void nvme_free_tagset(struct nvme_dev *dev) |
| 2551 | { |
| 2552 | if (dev->tagset.tags) |
| 2553 | blk_mq_free_tag_set(&dev->tagset); |
| 2554 | dev->ctrl.tagset = NULL; |
| 2555 | } |
| 2556 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2557 | static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl) |
| 2558 | { |
| 2559 | struct nvme_dev *dev = to_nvme_dev(ctrl); |
| 2560 | |
| 2561 | nvme_dbbuf_dma_free(dev); |
| 2562 | put_device(dev->dev); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2563 | nvme_free_tagset(dev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2564 | if (dev->ctrl.admin_q) |
| 2565 | blk_put_queue(dev->ctrl.admin_q); |
| 2566 | kfree(dev->queues); |
| 2567 | free_opal_dev(dev->ctrl.opal_dev); |
| 2568 | mempool_destroy(dev->iod_mempool); |
| 2569 | kfree(dev); |
| 2570 | } |
| 2571 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2572 | static void nvme_remove_dead_ctrl(struct nvme_dev *dev) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2573 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2574 | /* |
| 2575 | * Set state to deleting now to avoid blocking nvme_wait_reset(), which |
| 2576 | * may be holding this pci_dev's device lock. |
| 2577 | */ |
| 2578 | nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2579 | nvme_get_ctrl(&dev->ctrl); |
| 2580 | nvme_dev_disable(dev, false); |
| 2581 | nvme_kill_queues(&dev->ctrl); |
| 2582 | if (!queue_work(nvme_wq, &dev->remove_work)) |
| 2583 | nvme_put_ctrl(&dev->ctrl); |
| 2584 | } |
| 2585 | |
| 2586 | static void nvme_reset_work(struct work_struct *work) |
| 2587 | { |
| 2588 | struct nvme_dev *dev = |
| 2589 | container_of(work, struct nvme_dev, ctrl.reset_work); |
| 2590 | bool was_suspend = !!(dev->ctrl.ctrl_config & NVME_CC_SHN_NORMAL); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2591 | int result; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2592 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2593 | if (dev->ctrl.state != NVME_CTRL_RESETTING) { |
| 2594 | dev_warn(dev->ctrl.device, "ctrl state %d is not RESETTING\n", |
| 2595 | dev->ctrl.state); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2596 | result = -ENODEV; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2597 | goto out; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2598 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2599 | |
| 2600 | /* |
| 2601 | * If we're called to reset a live controller first shut it down before |
| 2602 | * moving on. |
| 2603 | */ |
| 2604 | if (dev->ctrl.ctrl_config & NVME_CC_ENABLE) |
| 2605 | nvme_dev_disable(dev, false); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2606 | nvme_sync_queues(&dev->ctrl); |
| 2607 | |
| 2608 | mutex_lock(&dev->shutdown_lock); |
| 2609 | result = nvme_pci_enable(dev); |
| 2610 | if (result) |
| 2611 | goto out_unlock; |
| 2612 | |
| 2613 | result = nvme_pci_configure_admin_queue(dev); |
| 2614 | if (result) |
| 2615 | goto out_unlock; |
| 2616 | |
| 2617 | result = nvme_alloc_admin_tags(dev); |
| 2618 | if (result) |
| 2619 | goto out_unlock; |
| 2620 | |
| 2621 | /* |
| 2622 | * Limit the max command size to prevent iod->sg allocations going |
| 2623 | * over a single page. |
| 2624 | */ |
| 2625 | dev->ctrl.max_hw_sectors = min_t(u32, |
| 2626 | NVME_MAX_KB_SZ << 1, dma_max_mapping_size(dev->dev) >> 9); |
| 2627 | dev->ctrl.max_segments = NVME_MAX_SEGS; |
| 2628 | |
| 2629 | /* |
| 2630 | * Don't limit the IOMMU merged segment size. |
| 2631 | */ |
| 2632 | dma_set_max_seg_size(dev->dev, 0xffffffff); |
| 2633 | |
| 2634 | mutex_unlock(&dev->shutdown_lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2635 | |
| 2636 | /* |
| 2637 | * Introduce CONNECTING state from nvme-fc/rdma transports to mark the |
| 2638 | * initializing procedure here. |
| 2639 | */ |
| 2640 | if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_CONNECTING)) { |
| 2641 | dev_warn(dev->ctrl.device, |
| 2642 | "failed to mark controller CONNECTING\n"); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2643 | result = -EBUSY; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2644 | goto out; |
| 2645 | } |
| 2646 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2647 | result = nvme_init_identify(&dev->ctrl); |
| 2648 | if (result) |
| 2649 | goto out; |
| 2650 | |
| 2651 | if (dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) { |
| 2652 | if (!dev->ctrl.opal_dev) |
| 2653 | dev->ctrl.opal_dev = |
| 2654 | init_opal_dev(&dev->ctrl, &nvme_sec_submit); |
| 2655 | else if (was_suspend) |
| 2656 | opal_unlock_from_suspend(dev->ctrl.opal_dev); |
| 2657 | } else { |
| 2658 | free_opal_dev(dev->ctrl.opal_dev); |
| 2659 | dev->ctrl.opal_dev = NULL; |
| 2660 | } |
| 2661 | |
| 2662 | if (dev->ctrl.oacs & NVME_CTRL_OACS_DBBUF_SUPP) { |
| 2663 | result = nvme_dbbuf_dma_alloc(dev); |
| 2664 | if (result) |
| 2665 | dev_warn(dev->dev, |
| 2666 | "unable to allocate dma for dbbuf\n"); |
| 2667 | } |
| 2668 | |
| 2669 | if (dev->ctrl.hmpre) { |
| 2670 | result = nvme_setup_host_mem(dev); |
| 2671 | if (result < 0) |
| 2672 | goto out; |
| 2673 | } |
| 2674 | |
| 2675 | result = nvme_setup_io_queues(dev); |
| 2676 | if (result) |
| 2677 | goto out; |
| 2678 | |
| 2679 | /* |
| 2680 | * Keep the controller around but remove all namespaces if we don't have |
| 2681 | * any working I/O queue. |
| 2682 | */ |
| 2683 | if (dev->online_queues < 2) { |
| 2684 | dev_warn(dev->ctrl.device, "IO queues not created\n"); |
| 2685 | nvme_kill_queues(&dev->ctrl); |
| 2686 | nvme_remove_namespaces(&dev->ctrl); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2687 | nvme_free_tagset(dev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2688 | } else { |
| 2689 | nvme_start_queues(&dev->ctrl); |
| 2690 | nvme_wait_freeze(&dev->ctrl); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2691 | nvme_dev_add(dev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2692 | nvme_unfreeze(&dev->ctrl); |
| 2693 | } |
| 2694 | |
| 2695 | /* |
| 2696 | * If only admin queue live, keep it to do further investigation or |
| 2697 | * recovery. |
| 2698 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2699 | if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_LIVE)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2700 | dev_warn(dev->ctrl.device, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2701 | "failed to mark controller live state\n"); |
| 2702 | result = -ENODEV; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2703 | goto out; |
| 2704 | } |
| 2705 | |
| 2706 | nvme_start_ctrl(&dev->ctrl); |
| 2707 | return; |
| 2708 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2709 | out_unlock: |
| 2710 | mutex_unlock(&dev->shutdown_lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2711 | out: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2712 | if (result) |
| 2713 | dev_warn(dev->ctrl.device, |
| 2714 | "Removing after probe failure status: %d\n", result); |
| 2715 | nvme_remove_dead_ctrl(dev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | static void nvme_remove_dead_ctrl_work(struct work_struct *work) |
| 2719 | { |
| 2720 | struct nvme_dev *dev = container_of(work, struct nvme_dev, remove_work); |
| 2721 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
| 2722 | |
| 2723 | if (pci_get_drvdata(pdev)) |
| 2724 | device_release_driver(&pdev->dev); |
| 2725 | nvme_put_ctrl(&dev->ctrl); |
| 2726 | } |
| 2727 | |
| 2728 | static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val) |
| 2729 | { |
| 2730 | *val = readl(to_nvme_dev(ctrl)->bar + off); |
| 2731 | return 0; |
| 2732 | } |
| 2733 | |
| 2734 | static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val) |
| 2735 | { |
| 2736 | writel(val, to_nvme_dev(ctrl)->bar + off); |
| 2737 | return 0; |
| 2738 | } |
| 2739 | |
| 2740 | static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val) |
| 2741 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2742 | *val = lo_hi_readq(to_nvme_dev(ctrl)->bar + off); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2743 | return 0; |
| 2744 | } |
| 2745 | |
| 2746 | static int nvme_pci_get_address(struct nvme_ctrl *ctrl, char *buf, int size) |
| 2747 | { |
| 2748 | struct pci_dev *pdev = to_pci_dev(to_nvme_dev(ctrl)->dev); |
| 2749 | |
| 2750 | return snprintf(buf, size, "%s", dev_name(&pdev->dev)); |
| 2751 | } |
| 2752 | |
| 2753 | static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = { |
| 2754 | .name = "pcie", |
| 2755 | .module = THIS_MODULE, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2756 | .flags = NVME_F_METADATA_SUPPORTED | |
| 2757 | NVME_F_PCI_P2PDMA, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2758 | .reg_read32 = nvme_pci_reg_read32, |
| 2759 | .reg_write32 = nvme_pci_reg_write32, |
| 2760 | .reg_read64 = nvme_pci_reg_read64, |
| 2761 | .free_ctrl = nvme_pci_free_ctrl, |
| 2762 | .submit_async_event = nvme_pci_submit_async_event, |
| 2763 | .get_address = nvme_pci_get_address, |
| 2764 | }; |
| 2765 | |
| 2766 | static int nvme_dev_map(struct nvme_dev *dev) |
| 2767 | { |
| 2768 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
| 2769 | |
| 2770 | if (pci_request_mem_regions(pdev, "nvme")) |
| 2771 | return -ENODEV; |
| 2772 | |
| 2773 | if (nvme_remap_bar(dev, NVME_REG_DBS + 4096)) |
| 2774 | goto release; |
| 2775 | |
| 2776 | return 0; |
| 2777 | release: |
| 2778 | pci_release_mem_regions(pdev); |
| 2779 | return -ENODEV; |
| 2780 | } |
| 2781 | |
| 2782 | static unsigned long check_vendor_combination_bug(struct pci_dev *pdev) |
| 2783 | { |
| 2784 | if (pdev->vendor == 0x144d && pdev->device == 0xa802) { |
| 2785 | /* |
| 2786 | * Several Samsung devices seem to drop off the PCIe bus |
| 2787 | * randomly when APST is on and uses the deepest sleep state. |
| 2788 | * This has been observed on a Samsung "SM951 NVMe SAMSUNG |
| 2789 | * 256GB", a "PM951 NVMe SAMSUNG 512GB", and a "Samsung SSD |
| 2790 | * 950 PRO 256GB", but it seems to be restricted to two Dell |
| 2791 | * laptops. |
| 2792 | */ |
| 2793 | if (dmi_match(DMI_SYS_VENDOR, "Dell Inc.") && |
| 2794 | (dmi_match(DMI_PRODUCT_NAME, "XPS 15 9550") || |
| 2795 | dmi_match(DMI_PRODUCT_NAME, "Precision 5510"))) |
| 2796 | return NVME_QUIRK_NO_DEEPEST_PS; |
| 2797 | } else if (pdev->vendor == 0x144d && pdev->device == 0xa804) { |
| 2798 | /* |
| 2799 | * Samsung SSD 960 EVO drops off the PCIe bus after system |
| 2800 | * suspend on a Ryzen board, ASUS PRIME B350M-A, as well as |
| 2801 | * within few minutes after bootup on a Coffee Lake board - |
| 2802 | * ASUS PRIME Z370-A |
| 2803 | */ |
| 2804 | if (dmi_match(DMI_BOARD_VENDOR, "ASUSTeK COMPUTER INC.") && |
| 2805 | (dmi_match(DMI_BOARD_NAME, "PRIME B350M-A") || |
| 2806 | dmi_match(DMI_BOARD_NAME, "PRIME Z370-A"))) |
| 2807 | return NVME_QUIRK_NO_APST; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2808 | } else if ((pdev->vendor == 0x144d && (pdev->device == 0xa801 || |
| 2809 | pdev->device == 0xa808 || pdev->device == 0xa809)) || |
| 2810 | (pdev->vendor == 0x1e0f && pdev->device == 0x0001)) { |
| 2811 | /* |
| 2812 | * Forcing to use host managed nvme power settings for |
| 2813 | * lowest idle power with quick resume latency on |
| 2814 | * Samsung and Toshiba SSDs based on suspend behavior |
| 2815 | * on Coffee Lake board for LENOVO C640 |
| 2816 | */ |
| 2817 | if ((dmi_match(DMI_BOARD_VENDOR, "LENOVO")) && |
| 2818 | dmi_match(DMI_BOARD_NAME, "LNVNB161216")) |
| 2819 | return NVME_QUIRK_SIMPLE_SUSPEND; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2820 | } |
| 2821 | |
| 2822 | return 0; |
| 2823 | } |
| 2824 | |
| 2825 | static void nvme_async_probe(void *data, async_cookie_t cookie) |
| 2826 | { |
| 2827 | struct nvme_dev *dev = data; |
| 2828 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2829 | flush_work(&dev->ctrl.reset_work); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2830 | flush_work(&dev->ctrl.scan_work); |
| 2831 | nvme_put_ctrl(&dev->ctrl); |
| 2832 | } |
| 2833 | |
| 2834 | static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
| 2835 | { |
| 2836 | int node, result = -ENOMEM; |
| 2837 | struct nvme_dev *dev; |
| 2838 | unsigned long quirks = id->driver_data; |
| 2839 | size_t alloc_size; |
| 2840 | |
| 2841 | node = dev_to_node(&pdev->dev); |
| 2842 | if (node == NUMA_NO_NODE) |
| 2843 | set_dev_node(&pdev->dev, first_memory_node); |
| 2844 | |
| 2845 | dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node); |
| 2846 | if (!dev) |
| 2847 | return -ENOMEM; |
| 2848 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2849 | dev->nr_write_queues = write_queues; |
| 2850 | dev->nr_poll_queues = poll_queues; |
| 2851 | dev->nr_allocated_queues = nvme_max_io_queues(dev) + 1; |
| 2852 | dev->queues = kcalloc_node(dev->nr_allocated_queues, |
| 2853 | sizeof(struct nvme_queue), GFP_KERNEL, node); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2854 | if (!dev->queues) |
| 2855 | goto free; |
| 2856 | |
| 2857 | dev->dev = get_device(&pdev->dev); |
| 2858 | pci_set_drvdata(pdev, dev); |
| 2859 | |
| 2860 | result = nvme_dev_map(dev); |
| 2861 | if (result) |
| 2862 | goto put_pci; |
| 2863 | |
| 2864 | INIT_WORK(&dev->ctrl.reset_work, nvme_reset_work); |
| 2865 | INIT_WORK(&dev->remove_work, nvme_remove_dead_ctrl_work); |
| 2866 | mutex_init(&dev->shutdown_lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2867 | |
| 2868 | result = nvme_setup_prp_pools(dev); |
| 2869 | if (result) |
| 2870 | goto unmap; |
| 2871 | |
| 2872 | quirks |= check_vendor_combination_bug(pdev); |
| 2873 | |
| 2874 | /* |
| 2875 | * Double check that our mempool alloc size will cover the biggest |
| 2876 | * command we support. |
| 2877 | */ |
| 2878 | alloc_size = nvme_pci_iod_alloc_size(dev, NVME_MAX_KB_SZ, |
| 2879 | NVME_MAX_SEGS, true); |
| 2880 | WARN_ON_ONCE(alloc_size > PAGE_SIZE); |
| 2881 | |
| 2882 | dev->iod_mempool = mempool_create_node(1, mempool_kmalloc, |
| 2883 | mempool_kfree, |
| 2884 | (void *) alloc_size, |
| 2885 | GFP_KERNEL, node); |
| 2886 | if (!dev->iod_mempool) { |
| 2887 | result = -ENOMEM; |
| 2888 | goto release_pools; |
| 2889 | } |
| 2890 | |
| 2891 | result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops, |
| 2892 | quirks); |
| 2893 | if (result) |
| 2894 | goto release_mempool; |
| 2895 | |
| 2896 | dev_info(dev->ctrl.device, "pci function %s\n", dev_name(&pdev->dev)); |
| 2897 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2898 | nvme_reset_ctrl(&dev->ctrl); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2899 | async_schedule(nvme_async_probe, dev); |
| 2900 | |
| 2901 | return 0; |
| 2902 | |
| 2903 | release_mempool: |
| 2904 | mempool_destroy(dev->iod_mempool); |
| 2905 | release_pools: |
| 2906 | nvme_release_prp_pools(dev); |
| 2907 | unmap: |
| 2908 | nvme_dev_unmap(dev); |
| 2909 | put_pci: |
| 2910 | put_device(dev->dev); |
| 2911 | free: |
| 2912 | kfree(dev->queues); |
| 2913 | kfree(dev); |
| 2914 | return result; |
| 2915 | } |
| 2916 | |
| 2917 | static void nvme_reset_prepare(struct pci_dev *pdev) |
| 2918 | { |
| 2919 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2920 | |
| 2921 | /* |
| 2922 | * We don't need to check the return value from waiting for the reset |
| 2923 | * state as pci_dev device lock is held, making it impossible to race |
| 2924 | * with ->remove(). |
| 2925 | */ |
| 2926 | nvme_disable_prepare_reset(dev, false); |
| 2927 | nvme_sync_queues(&dev->ctrl); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2928 | } |
| 2929 | |
| 2930 | static void nvme_reset_done(struct pci_dev *pdev) |
| 2931 | { |
| 2932 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2933 | |
| 2934 | if (!nvme_try_sched_reset(&dev->ctrl)) |
| 2935 | flush_work(&dev->ctrl.reset_work); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
| 2938 | static void nvme_shutdown(struct pci_dev *pdev) |
| 2939 | { |
| 2940 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2941 | nvme_disable_prepare_reset(dev, true); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2942 | } |
| 2943 | |
| 2944 | /* |
| 2945 | * The driver's remove may be called on a device in a partially initialized |
| 2946 | * state. This function must not have any dependencies on the device state in |
| 2947 | * order to proceed. |
| 2948 | */ |
| 2949 | static void nvme_remove(struct pci_dev *pdev) |
| 2950 | { |
| 2951 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
| 2952 | |
| 2953 | nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2954 | pci_set_drvdata(pdev, NULL); |
| 2955 | |
| 2956 | if (!pci_device_is_present(pdev)) { |
| 2957 | nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DEAD); |
| 2958 | nvme_dev_disable(dev, true); |
| 2959 | } |
| 2960 | |
| 2961 | flush_work(&dev->ctrl.reset_work); |
| 2962 | nvme_stop_ctrl(&dev->ctrl); |
| 2963 | nvme_remove_namespaces(&dev->ctrl); |
| 2964 | nvme_dev_disable(dev, true); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2965 | nvme_release_cmb(dev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2966 | nvme_free_host_mem(dev); |
| 2967 | nvme_dev_remove_admin(dev); |
| 2968 | nvme_free_queues(dev, 0); |
| 2969 | nvme_uninit_ctrl(&dev->ctrl); |
| 2970 | nvme_release_prp_pools(dev); |
| 2971 | nvme_dev_unmap(dev); |
| 2972 | nvme_put_ctrl(&dev->ctrl); |
| 2973 | } |
| 2974 | |
| 2975 | #ifdef CONFIG_PM_SLEEP |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2976 | static int nvme_get_power_state(struct nvme_ctrl *ctrl, u32 *ps) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2977 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2978 | return nvme_get_features(ctrl, NVME_FEAT_POWER_MGMT, 0, NULL, 0, ps); |
| 2979 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2980 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2981 | static int nvme_set_power_state(struct nvme_ctrl *ctrl, u32 ps) |
| 2982 | { |
| 2983 | return nvme_set_features(ctrl, NVME_FEAT_POWER_MGMT, ps, NULL, 0, NULL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2984 | } |
| 2985 | |
| 2986 | static int nvme_resume(struct device *dev) |
| 2987 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2988 | struct nvme_dev *ndev = pci_get_drvdata(to_pci_dev(dev)); |
| 2989 | struct nvme_ctrl *ctrl = &ndev->ctrl; |
| 2990 | |
| 2991 | if (ndev->last_ps == U32_MAX || |
| 2992 | nvme_set_power_state(ctrl, ndev->last_ps) != 0) |
| 2993 | return nvme_try_sched_reset(&ndev->ctrl); |
| 2994 | return 0; |
| 2995 | } |
| 2996 | |
| 2997 | static int nvme_suspend(struct device *dev) |
| 2998 | { |
| 2999 | struct pci_dev *pdev = to_pci_dev(dev); |
| 3000 | struct nvme_dev *ndev = pci_get_drvdata(pdev); |
| 3001 | struct nvme_ctrl *ctrl = &ndev->ctrl; |
| 3002 | int ret = -EBUSY; |
| 3003 | |
| 3004 | ndev->last_ps = U32_MAX; |
| 3005 | |
| 3006 | /* |
| 3007 | * The platform does not remove power for a kernel managed suspend so |
| 3008 | * use host managed nvme power settings for lowest idle power if |
| 3009 | * possible. This should have quicker resume latency than a full device |
| 3010 | * shutdown. But if the firmware is involved after the suspend or the |
| 3011 | * device does not support any non-default power states, shut down the |
| 3012 | * device fully. |
| 3013 | * |
| 3014 | * If ASPM is not enabled for the device, shut down the device and allow |
| 3015 | * the PCI bus layer to put it into D3 in order to take the PCIe link |
| 3016 | * down, so as to allow the platform to achieve its minimum low-power |
| 3017 | * state (which may not be possible if the link is up). |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 3018 | * |
| 3019 | * If a host memory buffer is enabled, shut down the device as the NVMe |
| 3020 | * specification allows the device to access the host memory buffer in |
| 3021 | * host DRAM from all power states, but hosts will fail access to DRAM |
| 3022 | * during S3. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3023 | */ |
| 3024 | if (pm_suspend_via_firmware() || !ctrl->npss || |
| 3025 | !pcie_aspm_enabled(pdev) || |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 3026 | ndev->nr_host_mem_descs || |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3027 | (ndev->ctrl.quirks & NVME_QUIRK_SIMPLE_SUSPEND)) |
| 3028 | return nvme_disable_prepare_reset(ndev, true); |
| 3029 | |
| 3030 | nvme_start_freeze(ctrl); |
| 3031 | nvme_wait_freeze(ctrl); |
| 3032 | nvme_sync_queues(ctrl); |
| 3033 | |
| 3034 | if (ctrl->state != NVME_CTRL_LIVE) |
| 3035 | goto unfreeze; |
| 3036 | |
| 3037 | ret = nvme_get_power_state(ctrl, &ndev->last_ps); |
| 3038 | if (ret < 0) |
| 3039 | goto unfreeze; |
| 3040 | |
| 3041 | /* |
| 3042 | * A saved state prevents pci pm from generically controlling the |
| 3043 | * device's power. If we're using protocol specific settings, we don't |
| 3044 | * want pci interfering. |
| 3045 | */ |
| 3046 | pci_save_state(pdev); |
| 3047 | |
| 3048 | ret = nvme_set_power_state(ctrl, ctrl->npss); |
| 3049 | if (ret < 0) |
| 3050 | goto unfreeze; |
| 3051 | |
| 3052 | if (ret) { |
| 3053 | /* discard the saved state */ |
| 3054 | pci_load_saved_state(pdev, NULL); |
| 3055 | |
| 3056 | /* |
| 3057 | * Clearing npss forces a controller reset on resume. The |
| 3058 | * correct value will be resdicovered then. |
| 3059 | */ |
| 3060 | ret = nvme_disable_prepare_reset(ndev, true); |
| 3061 | ctrl->npss = 0; |
| 3062 | } |
| 3063 | unfreeze: |
| 3064 | nvme_unfreeze(ctrl); |
| 3065 | return ret; |
| 3066 | } |
| 3067 | |
| 3068 | static int nvme_simple_suspend(struct device *dev) |
| 3069 | { |
| 3070 | struct nvme_dev *ndev = pci_get_drvdata(to_pci_dev(dev)); |
| 3071 | return nvme_disable_prepare_reset(ndev, true); |
| 3072 | } |
| 3073 | |
| 3074 | static int nvme_simple_resume(struct device *dev) |
| 3075 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3076 | struct pci_dev *pdev = to_pci_dev(dev); |
| 3077 | struct nvme_dev *ndev = pci_get_drvdata(pdev); |
| 3078 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3079 | return nvme_try_sched_reset(&ndev->ctrl); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3080 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3081 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3082 | static const struct dev_pm_ops nvme_dev_pm_ops = { |
| 3083 | .suspend = nvme_suspend, |
| 3084 | .resume = nvme_resume, |
| 3085 | .freeze = nvme_simple_suspend, |
| 3086 | .thaw = nvme_simple_resume, |
| 3087 | .poweroff = nvme_simple_suspend, |
| 3088 | .restore = nvme_simple_resume, |
| 3089 | }; |
| 3090 | #endif /* CONFIG_PM_SLEEP */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3091 | |
| 3092 | static pci_ers_result_t nvme_error_detected(struct pci_dev *pdev, |
| 3093 | pci_channel_state_t state) |
| 3094 | { |
| 3095 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
| 3096 | |
| 3097 | /* |
| 3098 | * A frozen channel requires a reset. When detected, this method will |
| 3099 | * shutdown the controller to quiesce. The controller will be restarted |
| 3100 | * after the slot reset through driver's slot_reset callback. |
| 3101 | */ |
| 3102 | switch (state) { |
| 3103 | case pci_channel_io_normal: |
| 3104 | return PCI_ERS_RESULT_CAN_RECOVER; |
| 3105 | case pci_channel_io_frozen: |
| 3106 | dev_warn(dev->ctrl.device, |
| 3107 | "frozen state error detected, reset controller\n"); |
| 3108 | nvme_dev_disable(dev, false); |
| 3109 | return PCI_ERS_RESULT_NEED_RESET; |
| 3110 | case pci_channel_io_perm_failure: |
| 3111 | dev_warn(dev->ctrl.device, |
| 3112 | "failure state error detected, request disconnect\n"); |
| 3113 | return PCI_ERS_RESULT_DISCONNECT; |
| 3114 | } |
| 3115 | return PCI_ERS_RESULT_NEED_RESET; |
| 3116 | } |
| 3117 | |
| 3118 | static pci_ers_result_t nvme_slot_reset(struct pci_dev *pdev) |
| 3119 | { |
| 3120 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
| 3121 | |
| 3122 | dev_info(dev->ctrl.device, "restart after slot reset\n"); |
| 3123 | pci_restore_state(pdev); |
| 3124 | nvme_reset_ctrl(&dev->ctrl); |
| 3125 | return PCI_ERS_RESULT_RECOVERED; |
| 3126 | } |
| 3127 | |
| 3128 | static void nvme_error_resume(struct pci_dev *pdev) |
| 3129 | { |
| 3130 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
| 3131 | |
| 3132 | flush_work(&dev->ctrl.reset_work); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3133 | } |
| 3134 | |
| 3135 | static const struct pci_error_handlers nvme_err_handler = { |
| 3136 | .error_detected = nvme_error_detected, |
| 3137 | .slot_reset = nvme_slot_reset, |
| 3138 | .resume = nvme_error_resume, |
| 3139 | .reset_prepare = nvme_reset_prepare, |
| 3140 | .reset_done = nvme_reset_done, |
| 3141 | }; |
| 3142 | |
| 3143 | static const struct pci_device_id nvme_id_table[] = { |
| 3144 | { PCI_VDEVICE(INTEL, 0x0953), |
| 3145 | .driver_data = NVME_QUIRK_STRIPE_SIZE | |
| 3146 | NVME_QUIRK_DEALLOCATE_ZEROES, }, |
| 3147 | { PCI_VDEVICE(INTEL, 0x0a53), |
| 3148 | .driver_data = NVME_QUIRK_STRIPE_SIZE | |
| 3149 | NVME_QUIRK_DEALLOCATE_ZEROES, }, |
| 3150 | { PCI_VDEVICE(INTEL, 0x0a54), |
| 3151 | .driver_data = NVME_QUIRK_STRIPE_SIZE | |
| 3152 | NVME_QUIRK_DEALLOCATE_ZEROES, }, |
| 3153 | { PCI_VDEVICE(INTEL, 0x0a55), |
| 3154 | .driver_data = NVME_QUIRK_STRIPE_SIZE | |
| 3155 | NVME_QUIRK_DEALLOCATE_ZEROES, }, |
| 3156 | { PCI_VDEVICE(INTEL, 0xf1a5), /* Intel 600P/P3100 */ |
| 3157 | .driver_data = NVME_QUIRK_NO_DEEPEST_PS | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 3158 | NVME_QUIRK_MEDIUM_PRIO_SQ | |
| 3159 | NVME_QUIRK_DISABLE_WRITE_ZEROES, }, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3160 | { PCI_VDEVICE(INTEL, 0xf1a6), /* Intel 760p/Pro 7600p */ |
| 3161 | .driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN, }, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3162 | { PCI_VDEVICE(INTEL, 0x5845), /* Qemu emulated controller */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3163 | .driver_data = NVME_QUIRK_IDENTIFY_CNS | |
| 3164 | NVME_QUIRK_DISABLE_WRITE_ZEROES, }, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 3165 | { PCI_DEVICE(0x126f, 0x2263), /* Silicon Motion unidentified */ |
| 3166 | .driver_data = NVME_QUIRK_NO_NS_DESC_LIST, }, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3167 | { PCI_DEVICE(0x1bb1, 0x0100), /* Seagate Nytro Flash Storage */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 3168 | .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY | |
| 3169 | NVME_QUIRK_NO_NS_DESC_LIST, }, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3170 | { PCI_DEVICE(0x1c58, 0x0003), /* HGST adapter */ |
| 3171 | .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, }, |
| 3172 | { PCI_DEVICE(0x1c58, 0x0023), /* WDC SN200 adapter */ |
| 3173 | .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, }, |
| 3174 | { PCI_DEVICE(0x1c5f, 0x0540), /* Memblaze Pblaze4 adapter */ |
| 3175 | .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, }, |
| 3176 | { PCI_DEVICE(0x144d, 0xa821), /* Samsung PM1725 */ |
| 3177 | .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, }, |
| 3178 | { PCI_DEVICE(0x144d, 0xa822), /* Samsung PM1725a */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 3179 | .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY | |
| 3180 | NVME_QUIRK_DISABLE_WRITE_ZEROES| |
| 3181 | NVME_QUIRK_IGNORE_DEV_SUBNQN, }, |
| 3182 | { PCI_DEVICE(0x1987, 0x5016), /* Phison E16 */ |
| 3183 | .driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN, }, |
| 3184 | { PCI_DEVICE(0x1b4b, 0x1092), /* Lexar 256 GB SSD */ |
| 3185 | .driver_data = NVME_QUIRK_NO_NS_DESC_LIST | |
| 3186 | NVME_QUIRK_IGNORE_DEV_SUBNQN, }, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3187 | { PCI_DEVICE(0x1d1d, 0x1f1f), /* LighNVM qemu device */ |
| 3188 | .driver_data = NVME_QUIRK_LIGHTNVM, }, |
| 3189 | { PCI_DEVICE(0x1d1d, 0x2807), /* CNEX WL */ |
| 3190 | .driver_data = NVME_QUIRK_LIGHTNVM, }, |
| 3191 | { PCI_DEVICE(0x1d1d, 0x2601), /* CNEX Granby */ |
| 3192 | .driver_data = NVME_QUIRK_LIGHTNVM, }, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3193 | { PCI_DEVICE(0x10ec, 0x5762), /* ADATA SX6000LNP */ |
| 3194 | .driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN, }, |
| 3195 | { PCI_DEVICE(0x1cc1, 0x8201), /* ADATA SX8200PNP 512GB */ |
| 3196 | .driver_data = NVME_QUIRK_NO_DEEPEST_PS | |
| 3197 | NVME_QUIRK_IGNORE_DEV_SUBNQN, }, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 3198 | { PCI_DEVICE(0x1c5c, 0x1504), /* SK Hynix PC400 */ |
| 3199 | .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, }, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3200 | { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) }, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 3201 | { PCI_DEVICE(0x2646, 0x2263), /* KINGSTON A2000 NVMe SSD */ |
| 3202 | .driver_data = NVME_QUIRK_NO_DEEPEST_PS, }, |
| 3203 | { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001), |
| 3204 | .driver_data = NVME_QUIRK_SINGLE_VECTOR }, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3205 | { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2003) }, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3206 | { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2005), |
| 3207 | .driver_data = NVME_QUIRK_SINGLE_VECTOR | |
| 3208 | NVME_QUIRK_128_BYTES_SQES | |
| 3209 | NVME_QUIRK_SHARED_TAGS }, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3210 | { 0, } |
| 3211 | }; |
| 3212 | MODULE_DEVICE_TABLE(pci, nvme_id_table); |
| 3213 | |
| 3214 | static struct pci_driver nvme_driver = { |
| 3215 | .name = "nvme", |
| 3216 | .id_table = nvme_id_table, |
| 3217 | .probe = nvme_probe, |
| 3218 | .remove = nvme_remove, |
| 3219 | .shutdown = nvme_shutdown, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3220 | #ifdef CONFIG_PM_SLEEP |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3221 | .driver = { |
| 3222 | .pm = &nvme_dev_pm_ops, |
| 3223 | }, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3224 | #endif |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3225 | .sriov_configure = pci_sriov_configure_simple, |
| 3226 | .err_handler = &nvme_err_handler, |
| 3227 | }; |
| 3228 | |
| 3229 | static int __init nvme_init(void) |
| 3230 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3231 | BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64); |
| 3232 | BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64); |
| 3233 | BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64); |
| 3234 | BUILD_BUG_ON(IRQ_AFFINITY_MAX_SETS < 2); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3235 | return pci_register_driver(&nvme_driver); |
| 3236 | } |
| 3237 | |
| 3238 | static void __exit nvme_exit(void) |
| 3239 | { |
| 3240 | pci_unregister_driver(&nvme_driver); |
| 3241 | flush_workqueue(nvme_wq); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3242 | } |
| 3243 | |
| 3244 | MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>"); |
| 3245 | MODULE_LICENSE("GPL"); |
| 3246 | MODULE_VERSION("1.0"); |
| 3247 | module_init(nvme_init); |
| 3248 | module_exit(nvme_exit); |