at commit c03c1a14
| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * NVM Express device driver |
| 4 | * Copyright (c) 2011-2014, Intel Corporation. |
| 5 | */ |
| 6 | |
| 7 | #include <linux/acpi.h> |
| 8 | #include <linux/async.h> |
| 9 | #include <linux/blkdev.h> |
| 10 | #include <linux/blk-mq-dma.h> |
| 11 | #include <linux/blk-integrity.h> |
| 12 | #include <linux/dmi.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/io.h> |
| 16 | #include <linux/kstrtox.h> |
| 17 | #include <linux/memremap.h> |
| 18 | #include <linux/mm.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/mutex.h> |
| 21 | #include <linux/nodemask.h> |
| 22 | #include <linux/once.h> |
| 23 | #include <linux/pci.h> |
| 24 | #include <linux/suspend.h> |
| 25 | #include <linux/t10-pi.h> |
| 26 | #include <linux/types.h> |
| 27 | #include <linux/io-64-nonatomic-lo-hi.h> |
| 28 | #include <linux/io-64-nonatomic-hi-lo.h> |
| 29 | #include <linux/sed-opal.h> |
| 30 | |
| 31 | #include "trace.h" |
| 32 | #include "nvme.h" |
| 33 | |
| 34 | #define SQ_SIZE(q) ((q)->q_depth << (q)->sqes) |
| 35 | #define CQ_SIZE(q) ((q)->q_depth * sizeof(struct nvme_completion)) |
| 36 | |
| 37 | /* Optimisation for I/Os between 4k and 128k */ |
| 38 | #define NVME_SMALL_POOL_SIZE 256 |
| 39 | |
| 40 | /* |
| 41 | * Arbitrary upper bound. |
| 42 | */ |
| 43 | #define NVME_MAX_BYTES SZ_8M |
| 44 | #define NVME_MAX_NR_DESCRIPTORS 5 |
| 45 | |
| 46 | /* |
| 47 | * For data SGLs we support a single descriptors worth of SGL entries. |
| 48 | * For PRPs, segments don't matter at all. |
| 49 | */ |
| 50 | #define NVME_MAX_SEGS \ |
| 51 | (NVME_CTRL_PAGE_SIZE / sizeof(struct nvme_sgl_desc)) |
| 52 | |
| 53 | /* |
| 54 | * For metadata SGLs, only the small descriptor is supported, and the first |
| 55 | * entry is the segment descriptor, which for the data pointer sits in the SQE. |
| 56 | */ |
| 57 | #define NVME_MAX_META_SEGS \ |
| 58 | ((NVME_SMALL_POOL_SIZE / sizeof(struct nvme_sgl_desc)) - 1) |
| 59 | |
| 60 | /* |
| 61 | * The last entry is used to link to the next descriptor. |
| 62 | */ |
| 63 | #define PRPS_PER_PAGE \ |
| 64 | (((NVME_CTRL_PAGE_SIZE / sizeof(__le64))) - 1) |
| 65 | |
| 66 | /* |
| 67 | * I/O could be non-aligned both at the beginning and end. |
| 68 | */ |
| 69 | #define MAX_PRP_RANGE \ |
| 70 | (NVME_MAX_BYTES + 2 * (NVME_CTRL_PAGE_SIZE - 1)) |
| 71 | |
| 72 | static_assert(MAX_PRP_RANGE / NVME_CTRL_PAGE_SIZE <= |
| 73 | (1 /* prp1 */ + NVME_MAX_NR_DESCRIPTORS * PRPS_PER_PAGE)); |
| 74 | |
| 75 | struct quirk_entry { |
| 76 | u16 vendor_id; |
| 77 | u16 dev_id; |
| 78 | u32 enabled_quirks; |
| 79 | u32 disabled_quirks; |
| 80 | }; |
| 81 | |
| 82 | static int use_threaded_interrupts; |
| 83 | module_param(use_threaded_interrupts, int, 0444); |
| 84 | |
| 85 | static bool use_cmb_sqes = true; |
| 86 | module_param(use_cmb_sqes, bool, 0444); |
| 87 | MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes"); |
| 88 | |
| 89 | static unsigned int max_host_mem_size_mb = 128; |
| 90 | module_param(max_host_mem_size_mb, uint, 0444); |
| 91 | MODULE_PARM_DESC(max_host_mem_size_mb, |
| 92 | "Maximum Host Memory Buffer (HMB) size per controller (in MiB)"); |
| 93 | |
| 94 | static unsigned int sgl_threshold = SZ_32K; |
| 95 | module_param(sgl_threshold, uint, 0644); |
| 96 | MODULE_PARM_DESC(sgl_threshold, |
| 97 | "Use SGLs when average request segment size is larger or equal to " |
| 98 | "this size. Use 0 to disable SGLs."); |
| 99 | |
| 100 | #define NVME_PCI_MIN_QUEUE_SIZE 2 |
| 101 | #define NVME_PCI_MAX_QUEUE_SIZE 4095 |
| 102 | static int io_queue_depth_set(const char *val, const struct kernel_param *kp); |
| 103 | static const struct kernel_param_ops io_queue_depth_ops = { |
| 104 | .set = io_queue_depth_set, |
| 105 | .get = param_get_uint, |
| 106 | }; |
| 107 | |
| 108 | static unsigned int io_queue_depth = 1024; |
| 109 | module_param_cb(io_queue_depth, &io_queue_depth_ops, &io_queue_depth, 0644); |
| 110 | MODULE_PARM_DESC(io_queue_depth, "set io queue depth, should >= 2 and < 4096"); |
| 111 | |
| 112 | static struct quirk_entry *nvme_pci_quirk_list; |
| 113 | static unsigned int nvme_pci_quirk_count; |
| 114 | |
| 115 | /* Helper to parse individual quirk names */ |
| 116 | static int nvme_parse_quirk_names(char *quirk_str, struct quirk_entry *entry) |
| 117 | { |
| 118 | int i; |
| 119 | size_t field_len; |
| 120 | bool disabled, found; |
| 121 | char *p = quirk_str, *field; |
| 122 | |
| 123 | while ((field = strsep(&p, ",")) && *field) { |
| 124 | disabled = false; |
| 125 | found = false; |
| 126 | |
| 127 | if (*field == '^') { |
| 128 | /* Skip the '^' character */ |
| 129 | disabled = true; |
| 130 | field++; |
| 131 | } |
| 132 | |
| 133 | field_len = strlen(field); |
| 134 | for (i = 0; i < 32; i++) { |
| 135 | unsigned int bit = 1U << i; |
| 136 | char *q_name = nvme_quirk_name(bit); |
| 137 | size_t q_len = strlen(q_name); |
| 138 | |
| 139 | if (!strcmp(q_name, "unknown")) |
| 140 | break; |
| 141 | |
| 142 | if (!strcmp(q_name, field) && |
| 143 | q_len == field_len) { |
| 144 | if (disabled) |
| 145 | entry->disabled_quirks |= bit; |
| 146 | else |
| 147 | entry->enabled_quirks |= bit; |
| 148 | found = true; |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | if (!found) { |
| 154 | pr_err("nvme: unrecognized quirk %s\n", field); |
| 155 | return -EINVAL; |
| 156 | } |
| 157 | } |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | /* Helper to parse a single VID:DID:quirk_names entry */ |
| 162 | static int nvme_parse_quirk_entry(char *s, struct quirk_entry *entry) |
| 163 | { |
| 164 | char *field; |
| 165 | |
| 166 | field = strsep(&s, ":"); |
| 167 | if (!field || kstrtou16(field, 16, &entry->vendor_id)) |
| 168 | return -EINVAL; |
| 169 | |
| 170 | field = strsep(&s, ":"); |
| 171 | if (!field || kstrtou16(field, 16, &entry->dev_id)) |
| 172 | return -EINVAL; |
| 173 | |
| 174 | field = strsep(&s, ":"); |
| 175 | if (!field) |
| 176 | return -EINVAL; |
| 177 | |
| 178 | return nvme_parse_quirk_names(field, entry); |
| 179 | } |
| 180 | |
| 181 | static int quirks_param_set(const char *value, const struct kernel_param *kp) |
| 182 | { |
| 183 | int count, err, i; |
| 184 | struct quirk_entry *qlist; |
| 185 | char *field, *val, *sep_ptr; |
| 186 | |
| 187 | err = param_set_copystring(value, kp); |
| 188 | if (err) |
| 189 | return err; |
| 190 | |
| 191 | val = kstrdup(value, GFP_KERNEL); |
| 192 | if (!val) |
| 193 | return -ENOMEM; |
| 194 | |
| 195 | if (!*val) |
| 196 | goto out_free_val; |
| 197 | |
| 198 | count = 1; |
| 199 | for (i = 0; val[i]; i++) { |
| 200 | if (val[i] == '-') |
| 201 | count++; |
| 202 | } |
| 203 | |
| 204 | qlist = kzalloc_objs(*qlist, count); |
| 205 | if (!qlist) { |
| 206 | err = -ENOMEM; |
| 207 | goto out_free_val; |
| 208 | } |
| 209 | |
| 210 | i = 0; |
| 211 | sep_ptr = val; |
| 212 | while ((field = strsep(&sep_ptr, "-"))) { |
| 213 | if (nvme_parse_quirk_entry(field, &qlist[i])) { |
| 214 | pr_err("nvme: failed to parse quirk string %s\n", |
| 215 | value); |
| 216 | err = -EINVAL; |
| 217 | goto out_free_qlist; |
| 218 | } |
| 219 | |
| 220 | i++; |
| 221 | } |
| 222 | |
| 223 | kfree(nvme_pci_quirk_list); |
| 224 | nvme_pci_quirk_count = count; |
| 225 | nvme_pci_quirk_list = qlist; |
| 226 | goto out_free_val; |
| 227 | |
| 228 | out_free_qlist: |
| 229 | kfree(qlist); |
| 230 | out_free_val: |
| 231 | kfree(val); |
| 232 | return err; |
| 233 | } |
| 234 | |
| 235 | static char quirks_param[128]; |
| 236 | static const struct kernel_param_ops quirks_param_ops = { |
| 237 | .set = quirks_param_set, |
| 238 | .get = param_get_string, |
| 239 | }; |
| 240 | |
| 241 | static struct kparam_string quirks_param_string = { |
| 242 | .maxlen = sizeof(quirks_param), |
| 243 | .string = quirks_param, |
| 244 | }; |
| 245 | |
| 246 | module_param_cb(quirks, &quirks_param_ops, &quirks_param_string, 0444); |
| 247 | MODULE_PARM_DESC(quirks, "Enable/disable NVMe quirks by specifying " |
| 248 | "quirks=VID:DID:quirk_names"); |
| 249 | |
| 250 | static int io_queue_count_set(const char *val, const struct kernel_param *kp) |
| 251 | { |
| 252 | unsigned int n; |
| 253 | int ret; |
| 254 | |
| 255 | ret = kstrtouint(val, 10, &n); |
| 256 | if (ret != 0 || n > blk_mq_num_possible_queues(0)) |
| 257 | return -EINVAL; |
| 258 | return param_set_uint(val, kp); |
| 259 | } |
| 260 | |
| 261 | static const struct kernel_param_ops io_queue_count_ops = { |
| 262 | .set = io_queue_count_set, |
| 263 | .get = param_get_uint, |
| 264 | }; |
| 265 | |
| 266 | static unsigned int write_queues; |
| 267 | module_param_cb(write_queues, &io_queue_count_ops, &write_queues, 0644); |
| 268 | MODULE_PARM_DESC(write_queues, |
| 269 | "Number of queues to use for writes. If not set, reads and writes " |
| 270 | "will share a queue set."); |
| 271 | |
| 272 | static unsigned int poll_queues; |
| 273 | module_param_cb(poll_queues, &io_queue_count_ops, &poll_queues, 0644); |
| 274 | MODULE_PARM_DESC(poll_queues, "Number of queues to use for polled IO."); |
| 275 | |
| 276 | static bool noacpi; |
| 277 | module_param(noacpi, bool, 0444); |
| 278 | MODULE_PARM_DESC(noacpi, "disable acpi bios quirks"); |
| 279 | |
| 280 | struct nvme_dev; |
| 281 | struct nvme_queue; |
| 282 | |
| 283 | static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown); |
| 284 | static void nvme_delete_io_queues(struct nvme_dev *dev); |
| 285 | static void nvme_update_attrs(struct nvme_dev *dev); |
| 286 | |
| 287 | struct nvme_descriptor_pools { |
| 288 | struct dma_pool *large; |
| 289 | struct dma_pool *small; |
| 290 | }; |
| 291 | |
| 292 | /* |
| 293 | * Represents an NVM Express device. Each nvme_dev is a PCI function. |
| 294 | */ |
| 295 | struct nvme_dev { |
| 296 | struct nvme_queue *queues; |
| 297 | struct blk_mq_tag_set tagset; |
| 298 | struct blk_mq_tag_set admin_tagset; |
| 299 | u32 __iomem *dbs; |
| 300 | struct device *dev; |
| 301 | unsigned online_queues; |
| 302 | unsigned max_qid; |
| 303 | unsigned io_queues[HCTX_MAX_TYPES]; |
| 304 | unsigned int num_vecs; |
| 305 | u32 q_depth; |
| 306 | int io_sqes; |
| 307 | u32 db_stride; |
| 308 | void __iomem *bar; |
| 309 | unsigned long bar_mapped_size; |
| 310 | struct mutex shutdown_lock; |
| 311 | bool subsystem; |
| 312 | u64 cmb_size; |
| 313 | bool cmb_use_sqes; |
| 314 | u32 cmbsz; |
| 315 | u32 cmbloc; |
| 316 | struct nvme_ctrl ctrl; |
| 317 | u32 last_ps; |
| 318 | bool hmb; |
| 319 | struct sg_table *hmb_sgt; |
| 320 | mempool_t *dmavec_mempool; |
| 321 | |
| 322 | /* shadow doorbell buffer support: */ |
| 323 | __le32 *dbbuf_dbs; |
| 324 | dma_addr_t dbbuf_dbs_dma_addr; |
| 325 | __le32 *dbbuf_eis; |
| 326 | dma_addr_t dbbuf_eis_dma_addr; |
| 327 | |
| 328 | /* host memory buffer support: */ |
| 329 | u64 host_mem_size; |
| 330 | u32 nr_host_mem_descs; |
| 331 | u32 host_mem_descs_size; |
| 332 | dma_addr_t host_mem_descs_dma; |
| 333 | struct nvme_host_mem_buf_desc *host_mem_descs; |
| 334 | void **host_mem_desc_bufs; |
| 335 | unsigned int nr_allocated_queues; |
| 336 | unsigned int nr_write_queues; |
| 337 | unsigned int nr_poll_queues; |
| 338 | struct nvme_descriptor_pools descriptor_pools[]; |
| 339 | }; |
| 340 | |
| 341 | static int io_queue_depth_set(const char *val, const struct kernel_param *kp) |
| 342 | { |
| 343 | return param_set_uint_minmax(val, kp, NVME_PCI_MIN_QUEUE_SIZE, |
| 344 | NVME_PCI_MAX_QUEUE_SIZE); |
| 345 | } |
| 346 | |
| 347 | static inline unsigned int sq_idx(unsigned int qid, u32 stride) |
| 348 | { |
| 349 | return qid * 2 * stride; |
| 350 | } |
| 351 | |
| 352 | static inline unsigned int cq_idx(unsigned int qid, u32 stride) |
| 353 | { |
| 354 | return (qid * 2 + 1) * stride; |
| 355 | } |
| 356 | |
| 357 | static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl) |
| 358 | { |
| 359 | return container_of(ctrl, struct nvme_dev, ctrl); |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * An NVM Express queue. Each device has at least two (one for admin |
| 364 | * commands and one for I/O commands). |
| 365 | */ |
| 366 | struct nvme_queue { |
| 367 | struct nvme_dev *dev; |
| 368 | struct nvme_descriptor_pools descriptor_pools; |
| 369 | spinlock_t sq_lock; |
| 370 | void *sq_cmds |
| 371 | __guarded_by(&sq_lock); |
| 372 | /* only used for poll queues: */ |
| 373 | spinlock_t cq_poll_lock ____cacheline_aligned_in_smp; |
| 374 | struct nvme_completion *cqes; |
| 375 | dma_addr_t sq_dma_addr; |
| 376 | dma_addr_t cq_dma_addr; |
| 377 | u32 __iomem *q_db; |
| 378 | u32 q_depth; |
| 379 | u16 cq_vector; |
| 380 | u16 cq_head; |
| 381 | u16 sq_tail |
| 382 | __guarded_by(&sq_lock); |
| 383 | u16 last_sq_tail |
| 384 | __guarded_by(&sq_lock); |
| 385 | u16 qid; |
| 386 | u8 cq_phase; |
| 387 | u8 sqes; |
| 388 | unsigned long flags; |
| 389 | #define NVMEQ_ENABLED 0 |
| 390 | #define NVMEQ_SQ_CMB 1 |
| 391 | #define NVMEQ_DELETE_ERROR 2 |
| 392 | #define NVMEQ_POLLED 3 |
| 393 | __le32 *dbbuf_sq_db; |
| 394 | __le32 *dbbuf_cq_db; |
| 395 | __le32 *dbbuf_sq_ei; |
| 396 | __le32 *dbbuf_cq_ei; |
| 397 | struct completion delete_done; |
| 398 | }; |
| 399 | |
| 400 | /* bits for iod->flags */ |
| 401 | enum nvme_iod_flags { |
| 402 | /* this command has been aborted by the timeout handler */ |
| 403 | IOD_ABORTED = 1U << 0, |
| 404 | |
| 405 | /* uses the small descriptor pool */ |
| 406 | IOD_SMALL_DESCRIPTOR = 1U << 1, |
| 407 | |
| 408 | /* single segment dma mapping */ |
| 409 | IOD_SINGLE_SEGMENT = 1U << 2, |
| 410 | |
| 411 | /* Data payload contains p2p memory */ |
| 412 | IOD_DATA_P2P = 1U << 3, |
| 413 | |
| 414 | /* Metadata contains p2p memory */ |
| 415 | IOD_META_P2P = 1U << 4, |
| 416 | |
| 417 | /* Data payload contains MMIO memory */ |
| 418 | IOD_DATA_MMIO = 1U << 5, |
| 419 | |
| 420 | /* Metadata contains MMIO memory */ |
| 421 | IOD_META_MMIO = 1U << 6, |
| 422 | |
| 423 | /* Metadata using non-coalesced MPTR */ |
| 424 | IOD_SINGLE_META_SEGMENT = 1U << 7, |
| 425 | }; |
| 426 | |
| 427 | struct nvme_dma_vec { |
| 428 | dma_addr_t addr; |
| 429 | unsigned int len; |
| 430 | }; |
| 431 | |
| 432 | /* |
| 433 | * The nvme_iod describes the data in an I/O. |
| 434 | */ |
| 435 | struct nvme_iod { |
| 436 | struct nvme_request req; |
| 437 | struct nvme_command cmd; |
| 438 | u8 flags; |
| 439 | u8 nr_descriptors; |
| 440 | |
| 441 | size_t total_len; |
| 442 | struct dma_iova_state dma_state; |
| 443 | void *descriptors[NVME_MAX_NR_DESCRIPTORS]; |
| 444 | struct nvme_dma_vec *dma_vecs; |
| 445 | unsigned int nr_dma_vecs; |
| 446 | |
| 447 | dma_addr_t meta_dma; |
| 448 | size_t meta_total_len; |
| 449 | struct dma_iova_state meta_dma_state; |
| 450 | struct nvme_sgl_desc *meta_descriptor; |
| 451 | }; |
| 452 | |
| 453 | static inline unsigned int nvme_dbbuf_size(struct nvme_dev *dev) |
| 454 | { |
| 455 | return dev->nr_allocated_queues * 8 * dev->db_stride; |
| 456 | } |
| 457 | |
| 458 | static void nvme_dbbuf_dma_alloc(struct nvme_dev *dev) |
| 459 | { |
| 460 | unsigned int mem_size = nvme_dbbuf_size(dev); |
| 461 | |
| 462 | if (!(dev->ctrl.oacs & NVME_CTRL_OACS_DBBUF_SUPP)) |
| 463 | return; |
| 464 | |
| 465 | if (dev->dbbuf_dbs) { |
| 466 | /* |
| 467 | * Clear the dbbuf memory so the driver doesn't observe stale |
| 468 | * values from the previous instantiation. |
| 469 | */ |
| 470 | memset(dev->dbbuf_dbs, 0, mem_size); |
| 471 | memset(dev->dbbuf_eis, 0, mem_size); |
| 472 | return; |
| 473 | } |
| 474 | |
| 475 | dev->dbbuf_dbs = dma_alloc_coherent(dev->dev, mem_size, |
| 476 | &dev->dbbuf_dbs_dma_addr, |
| 477 | GFP_KERNEL); |
| 478 | if (!dev->dbbuf_dbs) |
| 479 | goto fail; |
| 480 | dev->dbbuf_eis = dma_alloc_coherent(dev->dev, mem_size, |
| 481 | &dev->dbbuf_eis_dma_addr, |
| 482 | GFP_KERNEL); |
| 483 | if (!dev->dbbuf_eis) |
| 484 | goto fail_free_dbbuf_dbs; |
| 485 | return; |
| 486 | |
| 487 | fail_free_dbbuf_dbs: |
| 488 | dma_free_coherent(dev->dev, mem_size, dev->dbbuf_dbs, |
| 489 | dev->dbbuf_dbs_dma_addr); |
| 490 | dev->dbbuf_dbs = NULL; |
| 491 | fail: |
| 492 | dev_warn(dev->dev, "unable to allocate dma for dbbuf\n"); |
| 493 | } |
| 494 | |
| 495 | static void nvme_dbbuf_dma_free(struct nvme_dev *dev) |
| 496 | { |
| 497 | unsigned int mem_size = nvme_dbbuf_size(dev); |
| 498 | |
| 499 | if (dev->dbbuf_dbs) { |
| 500 | dma_free_coherent(dev->dev, mem_size, |
| 501 | dev->dbbuf_dbs, dev->dbbuf_dbs_dma_addr); |
| 502 | dev->dbbuf_dbs = NULL; |
| 503 | } |
| 504 | if (dev->dbbuf_eis) { |
| 505 | dma_free_coherent(dev->dev, mem_size, |
| 506 | dev->dbbuf_eis, dev->dbbuf_eis_dma_addr); |
| 507 | dev->dbbuf_eis = NULL; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | static void nvme_dbbuf_init(struct nvme_dev *dev, |
| 512 | struct nvme_queue *nvmeq, int qid) |
| 513 | { |
| 514 | if (!dev->dbbuf_dbs || !qid) |
| 515 | return; |
| 516 | |
| 517 | nvmeq->dbbuf_sq_db = &dev->dbbuf_dbs[sq_idx(qid, dev->db_stride)]; |
| 518 | nvmeq->dbbuf_cq_db = &dev->dbbuf_dbs[cq_idx(qid, dev->db_stride)]; |
| 519 | nvmeq->dbbuf_sq_ei = &dev->dbbuf_eis[sq_idx(qid, dev->db_stride)]; |
| 520 | nvmeq->dbbuf_cq_ei = &dev->dbbuf_eis[cq_idx(qid, dev->db_stride)]; |
| 521 | } |
| 522 | |
| 523 | static void nvme_dbbuf_free(struct nvme_queue *nvmeq) |
| 524 | { |
| 525 | if (!nvmeq->qid) |
| 526 | return; |
| 527 | |
| 528 | nvmeq->dbbuf_sq_db = NULL; |
| 529 | nvmeq->dbbuf_cq_db = NULL; |
| 530 | nvmeq->dbbuf_sq_ei = NULL; |
| 531 | nvmeq->dbbuf_cq_ei = NULL; |
| 532 | } |
| 533 | |
| 534 | static void nvme_dbbuf_set(struct nvme_dev *dev) |
| 535 | { |
| 536 | struct nvme_command c = { }; |
| 537 | unsigned int i; |
| 538 | |
| 539 | if (!dev->dbbuf_dbs) |
| 540 | return; |
| 541 | |
| 542 | c.dbbuf.opcode = nvme_admin_dbbuf; |
| 543 | c.dbbuf.prp1 = cpu_to_le64(dev->dbbuf_dbs_dma_addr); |
| 544 | c.dbbuf.prp2 = cpu_to_le64(dev->dbbuf_eis_dma_addr); |
| 545 | |
| 546 | if (nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0)) { |
| 547 | dev_warn(dev->ctrl.device, "unable to set dbbuf\n"); |
| 548 | /* Free memory and continue on */ |
| 549 | nvme_dbbuf_dma_free(dev); |
| 550 | |
| 551 | for (i = 1; i < dev->online_queues; i++) |
| 552 | nvme_dbbuf_free(&dev->queues[i]); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | static inline int nvme_dbbuf_need_event(u16 event_idx, u16 new_idx, u16 old) |
| 557 | { |
| 558 | return (u16)(new_idx - event_idx - 1) < (u16)(new_idx - old); |
| 559 | } |
| 560 | |
| 561 | /* Update dbbuf and return true if an MMIO is required */ |
| 562 | static bool nvme_dbbuf_update_and_check_event(u16 value, __le32 *dbbuf_db, |
| 563 | volatile __le32 *dbbuf_ei) |
| 564 | { |
| 565 | if (dbbuf_db) { |
| 566 | u16 old_value, event_idx; |
| 567 | |
| 568 | /* |
| 569 | * Ensure that the queue is written before updating |
| 570 | * the doorbell in memory |
| 571 | */ |
| 572 | wmb(); |
| 573 | |
| 574 | old_value = le32_to_cpu(*dbbuf_db); |
| 575 | *dbbuf_db = cpu_to_le32(value); |
| 576 | |
| 577 | /* |
| 578 | * Ensure that the doorbell is updated before reading the event |
| 579 | * index from memory. The controller needs to provide similar |
| 580 | * ordering to ensure the event index is updated before reading |
| 581 | * the doorbell. |
| 582 | */ |
| 583 | mb(); |
| 584 | |
| 585 | event_idx = le32_to_cpu(*dbbuf_ei); |
| 586 | if (!nvme_dbbuf_need_event(event_idx, value, old_value)) |
| 587 | return false; |
| 588 | } |
| 589 | |
| 590 | return true; |
| 591 | } |
| 592 | |
| 593 | static struct nvme_descriptor_pools * |
| 594 | nvme_setup_descriptor_pools(struct nvme_dev *dev, int numa_node) |
| 595 | { |
| 596 | struct nvme_descriptor_pools *pools; |
| 597 | size_t small_align = NVME_SMALL_POOL_SIZE; |
| 598 | |
| 599 | if (numa_node == NUMA_NO_NODE) |
| 600 | numa_node = 0; |
| 601 | |
| 602 | pools = &dev->descriptor_pools[numa_node]; |
| 603 | |
| 604 | if (pools->small) |
| 605 | return pools; /* already initialized */ |
| 606 | |
| 607 | pools->large = dma_pool_create_node("nvme descriptor page", dev->dev, |
| 608 | NVME_CTRL_PAGE_SIZE, NVME_CTRL_PAGE_SIZE, 0, numa_node); |
| 609 | if (!pools->large) |
| 610 | return ERR_PTR(-ENOMEM); |
| 611 | |
| 612 | if (dev->ctrl.quirks & NVME_QUIRK_DMAPOOL_ALIGN_512) |
| 613 | small_align = 512; |
| 614 | |
| 615 | pools->small = dma_pool_create_node("nvme descriptor small", dev->dev, |
| 616 | NVME_SMALL_POOL_SIZE, small_align, 0, numa_node); |
| 617 | if (!pools->small) { |
| 618 | dma_pool_destroy(pools->large); |
| 619 | pools->large = NULL; |
| 620 | return ERR_PTR(-ENOMEM); |
| 621 | } |
| 622 | |
| 623 | return pools; |
| 624 | } |
| 625 | |
| 626 | static void nvme_release_descriptor_pools(struct nvme_dev *dev) |
| 627 | { |
| 628 | unsigned i; |
| 629 | |
| 630 | for (i = 0; i < nr_node_ids; i++) { |
| 631 | struct nvme_descriptor_pools *pools = &dev->descriptor_pools[i]; |
| 632 | |
| 633 | dma_pool_destroy(pools->large); |
| 634 | dma_pool_destroy(pools->small); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | static int nvme_init_hctx_common(struct blk_mq_hw_ctx *hctx, void *data, |
| 639 | unsigned qid) |
| 640 | { |
| 641 | struct nvme_dev *dev = to_nvme_dev(data); |
| 642 | struct nvme_queue *nvmeq = &dev->queues[qid]; |
| 643 | struct nvme_descriptor_pools *pools; |
| 644 | struct blk_mq_tags *tags; |
| 645 | |
| 646 | tags = qid ? dev->tagset.tags[qid - 1] : dev->admin_tagset.tags[0]; |
| 647 | WARN_ON(tags != hctx->tags); |
| 648 | pools = nvme_setup_descriptor_pools(dev, hctx->numa_node); |
| 649 | if (IS_ERR(pools)) |
| 650 | return PTR_ERR(pools); |
| 651 | |
| 652 | nvmeq->descriptor_pools = *pools; |
| 653 | hctx->driver_data = nvmeq; |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 658 | unsigned int hctx_idx) |
| 659 | { |
| 660 | WARN_ON(hctx_idx != 0); |
| 661 | return nvme_init_hctx_common(hctx, data, 0); |
| 662 | } |
| 663 | |
| 664 | static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 665 | unsigned int hctx_idx) |
| 666 | { |
| 667 | return nvme_init_hctx_common(hctx, data, hctx_idx + 1); |
| 668 | } |
| 669 | |
| 670 | static int nvme_pci_init_request(struct blk_mq_tag_set *set, |
| 671 | struct request *req, unsigned int hctx_idx, |
| 672 | int numa_node) |
| 673 | { |
| 674 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 675 | |
| 676 | nvme_req(req)->ctrl = set->driver_data; |
| 677 | nvme_req(req)->cmd = &iod->cmd; |
| 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | static int queue_irq_offset(struct nvme_dev *dev) |
| 682 | { |
| 683 | /* if we have more than 1 vec, admin queue offsets us by 1 */ |
| 684 | if (dev->num_vecs > 1) |
| 685 | return 1; |
| 686 | |
| 687 | return 0; |
| 688 | } |
| 689 | |
| 690 | static void nvme_pci_map_queues(struct blk_mq_tag_set *set) |
| 691 | { |
| 692 | struct nvme_dev *dev = to_nvme_dev(set->driver_data); |
| 693 | int i, qoff, offset; |
| 694 | |
| 695 | offset = queue_irq_offset(dev); |
| 696 | for (i = 0, qoff = 0; i < set->nr_maps; i++) { |
| 697 | struct blk_mq_queue_map *map = &set->map[i]; |
| 698 | |
| 699 | map->nr_queues = dev->io_queues[i]; |
| 700 | if (!map->nr_queues) { |
| 701 | BUG_ON(i == HCTX_TYPE_DEFAULT); |
| 702 | continue; |
| 703 | } |
| 704 | |
| 705 | /* |
| 706 | * The poll queue(s) doesn't have an IRQ (and hence IRQ |
| 707 | * affinity), so use the regular blk-mq cpu mapping |
| 708 | */ |
| 709 | map->queue_offset = qoff; |
| 710 | if (i != HCTX_TYPE_POLL && offset) |
| 711 | blk_mq_map_hw_queues(map, dev->dev, offset); |
| 712 | else |
| 713 | blk_mq_map_queues(map); |
| 714 | qoff += map->nr_queues; |
| 715 | offset += map->nr_queues; |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | /* |
| 720 | * Write sq tail if we are asked to, or if the next command would wrap. |
| 721 | */ |
| 722 | static inline void nvme_write_sq_db(struct nvme_queue *nvmeq, bool write_sq) |
| 723 | __must_hold(&nvmeq->sq_lock) |
| 724 | { |
| 725 | if (!write_sq) { |
| 726 | u16 next_tail = nvmeq->sq_tail + 1; |
| 727 | |
| 728 | if (next_tail == nvmeq->q_depth) |
| 729 | next_tail = 0; |
| 730 | if (next_tail != nvmeq->last_sq_tail) |
| 731 | return; |
| 732 | } |
| 733 | |
| 734 | if (nvme_dbbuf_update_and_check_event(nvmeq->sq_tail, |
| 735 | nvmeq->dbbuf_sq_db, nvmeq->dbbuf_sq_ei)) |
| 736 | writel(nvmeq->sq_tail, nvmeq->q_db); |
| 737 | nvmeq->last_sq_tail = nvmeq->sq_tail; |
| 738 | } |
| 739 | |
| 740 | static inline void nvme_sq_copy_cmd(struct nvme_queue *nvmeq, |
| 741 | struct nvme_command *cmd) |
| 742 | __must_hold(&nvmeq->sq_lock) |
| 743 | { |
| 744 | memcpy(nvmeq->sq_cmds + (nvmeq->sq_tail << nvmeq->sqes), |
| 745 | absolute_pointer(cmd), sizeof(*cmd)); |
| 746 | if (++nvmeq->sq_tail == nvmeq->q_depth) |
| 747 | nvmeq->sq_tail = 0; |
| 748 | } |
| 749 | |
| 750 | static void nvme_commit_rqs(struct blk_mq_hw_ctx *hctx) |
| 751 | { |
| 752 | struct nvme_queue *nvmeq = hctx->driver_data; |
| 753 | |
| 754 | spin_lock(&nvmeq->sq_lock); |
| 755 | if (nvmeq->sq_tail != nvmeq->last_sq_tail) |
| 756 | nvme_write_sq_db(nvmeq, true); |
| 757 | spin_unlock(&nvmeq->sq_lock); |
| 758 | } |
| 759 | |
| 760 | enum nvme_use_sgl { |
| 761 | SGL_UNSUPPORTED, |
| 762 | SGL_SUPPORTED, |
| 763 | SGL_FORCED, |
| 764 | }; |
| 765 | |
| 766 | static inline bool nvme_pci_metadata_use_sgls(struct request *req) |
| 767 | { |
| 768 | struct nvme_queue *nvmeq = req->mq_hctx->driver_data; |
| 769 | struct nvme_dev *dev = nvmeq->dev; |
| 770 | |
| 771 | if (!nvme_ctrl_meta_sgl_supported(&dev->ctrl)) |
| 772 | return false; |
| 773 | return req->nr_integrity_segments > 1 || |
| 774 | nvme_req(req)->flags & NVME_REQ_USERCMD; |
| 775 | } |
| 776 | |
| 777 | static inline enum nvme_use_sgl nvme_pci_use_sgls(struct nvme_dev *dev, |
| 778 | struct request *req) |
| 779 | { |
| 780 | struct nvme_queue *nvmeq = req->mq_hctx->driver_data; |
| 781 | |
| 782 | if (nvmeq->qid && nvme_ctrl_sgl_supported(&dev->ctrl)) { |
| 783 | /* |
| 784 | * When the controller is capable of using SGL, there are |
| 785 | * several conditions that we force to use it: |
| 786 | * |
| 787 | * 1. A request containing page gaps within the controller's |
| 788 | * mask can not use the PRP format. |
| 789 | * |
| 790 | * 2. User commands use SGL because that lets the device |
| 791 | * validate the requested transfer lengths. |
| 792 | * |
| 793 | * 3. Multiple integrity segments must use SGL as that's the |
| 794 | * only way to describe such a command in NVMe. |
| 795 | */ |
| 796 | if (req_phys_gap_mask(req) & (NVME_CTRL_PAGE_SIZE - 1) || |
| 797 | nvme_req(req)->flags & NVME_REQ_USERCMD || |
| 798 | req->nr_integrity_segments > 1) |
| 799 | return SGL_FORCED; |
| 800 | return SGL_SUPPORTED; |
| 801 | } |
| 802 | |
| 803 | return SGL_UNSUPPORTED; |
| 804 | } |
| 805 | |
| 806 | static unsigned int nvme_pci_avg_seg_size(struct request *req) |
| 807 | { |
| 808 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 809 | unsigned int nseg; |
| 810 | |
| 811 | if (blk_rq_dma_map_coalesce(&iod->dma_state)) |
| 812 | nseg = 1; |
| 813 | else |
| 814 | nseg = blk_rq_nr_phys_segments(req); |
| 815 | return DIV_ROUND_UP(blk_rq_payload_bytes(req), nseg); |
| 816 | } |
| 817 | |
| 818 | static inline struct dma_pool *nvme_dma_pool(struct nvme_queue *nvmeq, |
| 819 | struct nvme_iod *iod) |
| 820 | { |
| 821 | if (iod->flags & IOD_SMALL_DESCRIPTOR) |
| 822 | return nvmeq->descriptor_pools.small; |
| 823 | return nvmeq->descriptor_pools.large; |
| 824 | } |
| 825 | |
| 826 | static inline bool nvme_pci_cmd_use_meta_sgl(struct nvme_command *cmd) |
| 827 | { |
| 828 | return (cmd->common.flags & NVME_CMD_SGL_ALL) == NVME_CMD_SGL_METASEG; |
| 829 | } |
| 830 | |
| 831 | static inline bool nvme_pci_cmd_use_sgl(struct nvme_command *cmd) |
| 832 | { |
| 833 | return cmd->common.flags & |
| 834 | (NVME_CMD_SGL_METABUF | NVME_CMD_SGL_METASEG); |
| 835 | } |
| 836 | |
| 837 | static inline dma_addr_t nvme_pci_first_desc_dma_addr(struct nvme_command *cmd) |
| 838 | { |
| 839 | if (nvme_pci_cmd_use_sgl(cmd)) |
| 840 | return le64_to_cpu(cmd->common.dptr.sgl.addr); |
| 841 | return le64_to_cpu(cmd->common.dptr.prp2); |
| 842 | } |
| 843 | |
| 844 | static void nvme_free_descriptors(struct request *req) |
| 845 | { |
| 846 | struct nvme_queue *nvmeq = req->mq_hctx->driver_data; |
| 847 | const int last_prp = NVME_CTRL_PAGE_SIZE / sizeof(__le64) - 1; |
| 848 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 849 | dma_addr_t dma_addr = nvme_pci_first_desc_dma_addr(&iod->cmd); |
| 850 | int i; |
| 851 | |
| 852 | if (iod->nr_descriptors == 1) { |
| 853 | dma_pool_free(nvme_dma_pool(nvmeq, iod), iod->descriptors[0], |
| 854 | dma_addr); |
| 855 | return; |
| 856 | } |
| 857 | |
| 858 | for (i = 0; i < iod->nr_descriptors; i++) { |
| 859 | __le64 *prp_list = iod->descriptors[i]; |
| 860 | dma_addr_t next_dma_addr = le64_to_cpu(prp_list[last_prp]); |
| 861 | |
| 862 | dma_pool_free(nvmeq->descriptor_pools.large, prp_list, |
| 863 | dma_addr); |
| 864 | dma_addr = next_dma_addr; |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | static void nvme_free_prps(struct request *req, unsigned int attrs) |
| 869 | { |
| 870 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 871 | struct nvme_queue *nvmeq = req->mq_hctx->driver_data; |
| 872 | unsigned int i; |
| 873 | |
| 874 | for (i = 0; i < iod->nr_dma_vecs; i++) |
| 875 | dma_unmap_phys(nvmeq->dev->dev, iod->dma_vecs[i].addr, |
| 876 | iod->dma_vecs[i].len, rq_dma_dir(req), attrs); |
| 877 | mempool_free(iod->dma_vecs, nvmeq->dev->dmavec_mempool); |
| 878 | } |
| 879 | |
| 880 | static void nvme_free_sgls(struct request *req, struct nvme_sgl_desc *sge, |
| 881 | struct nvme_sgl_desc *sg_list, unsigned int attrs) |
| 882 | { |
| 883 | struct nvme_queue *nvmeq = req->mq_hctx->driver_data; |
| 884 | enum dma_data_direction dir = rq_dma_dir(req); |
| 885 | unsigned int len = le32_to_cpu(sge->length); |
| 886 | struct device *dma_dev = nvmeq->dev->dev; |
| 887 | unsigned int i; |
| 888 | |
| 889 | if (sge->type == (NVME_SGL_FMT_DATA_DESC << 4)) { |
| 890 | dma_unmap_phys(dma_dev, le64_to_cpu(sge->addr), len, dir, |
| 891 | attrs); |
| 892 | return; |
| 893 | } |
| 894 | |
| 895 | for (i = 0; i < len / sizeof(*sg_list); i++) |
| 896 | dma_unmap_phys(dma_dev, le64_to_cpu(sg_list[i].addr), |
| 897 | le32_to_cpu(sg_list[i].length), dir, attrs); |
| 898 | } |
| 899 | |
| 900 | static void nvme_unmap_metadata(struct request *req) |
| 901 | { |
| 902 | struct nvme_queue *nvmeq = req->mq_hctx->driver_data; |
| 903 | enum pci_p2pdma_map_type map = PCI_P2PDMA_MAP_NONE; |
| 904 | enum dma_data_direction dir = rq_dma_dir(req); |
| 905 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 906 | struct device *dma_dev = nvmeq->dev->dev; |
| 907 | struct nvme_sgl_desc *sge = iod->meta_descriptor; |
| 908 | unsigned int attrs = 0; |
| 909 | |
| 910 | if (iod->flags & IOD_SINGLE_META_SEGMENT) { |
| 911 | dma_unmap_page(dma_dev, iod->meta_dma, |
| 912 | rq_integrity_vec(req).bv_len, |
| 913 | rq_dma_dir(req)); |
| 914 | return; |
| 915 | } |
| 916 | |
| 917 | if (iod->flags & IOD_META_P2P) |
| 918 | map = PCI_P2PDMA_MAP_BUS_ADDR; |
| 919 | else if (iod->flags & IOD_META_MMIO) { |
| 920 | map = PCI_P2PDMA_MAP_THRU_HOST_BRIDGE; |
| 921 | attrs |= DMA_ATTR_MMIO; |
| 922 | } |
| 923 | |
| 924 | if (!blk_rq_dma_unmap(req, dma_dev, &iod->meta_dma_state, |
| 925 | iod->meta_total_len, map)) { |
| 926 | if (nvme_pci_cmd_use_meta_sgl(&iod->cmd)) |
| 927 | nvme_free_sgls(req, sge, &sge[1], attrs); |
| 928 | else |
| 929 | dma_unmap_phys(dma_dev, iod->meta_dma, |
| 930 | iod->meta_total_len, dir, attrs); |
| 931 | } |
| 932 | |
| 933 | if (iod->meta_descriptor) |
| 934 | dma_pool_free(nvmeq->descriptor_pools.small, |
| 935 | iod->meta_descriptor, iod->meta_dma); |
| 936 | } |
| 937 | |
| 938 | static void nvme_unmap_data(struct request *req) |
| 939 | { |
| 940 | enum pci_p2pdma_map_type map = PCI_P2PDMA_MAP_NONE; |
| 941 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 942 | struct nvme_queue *nvmeq = req->mq_hctx->driver_data; |
| 943 | struct device *dma_dev = nvmeq->dev->dev; |
| 944 | unsigned int attrs = 0; |
| 945 | |
| 946 | if (iod->flags & IOD_SINGLE_SEGMENT) { |
| 947 | static_assert(offsetof(union nvme_data_ptr, prp1) == |
| 948 | offsetof(union nvme_data_ptr, sgl.addr)); |
| 949 | dma_unmap_page(dma_dev, le64_to_cpu(iod->cmd.common.dptr.prp1), |
| 950 | iod->total_len, rq_dma_dir(req)); |
| 951 | return; |
| 952 | } |
| 953 | |
| 954 | if (iod->flags & IOD_DATA_P2P) |
| 955 | map = PCI_P2PDMA_MAP_BUS_ADDR; |
| 956 | else if (iod->flags & IOD_DATA_MMIO) { |
| 957 | map = PCI_P2PDMA_MAP_THRU_HOST_BRIDGE; |
| 958 | attrs |= DMA_ATTR_MMIO; |
| 959 | } |
| 960 | |
| 961 | if (!blk_rq_dma_unmap(req, dma_dev, &iod->dma_state, iod->total_len, |
| 962 | map)) { |
| 963 | if (nvme_pci_cmd_use_sgl(&iod->cmd)) |
| 964 | nvme_free_sgls(req, &iod->cmd.common.dptr.sgl, |
| 965 | iod->descriptors[0], attrs); |
| 966 | else |
| 967 | nvme_free_prps(req, attrs); |
| 968 | } |
| 969 | |
| 970 | if (iod->nr_descriptors) |
| 971 | nvme_free_descriptors(req); |
| 972 | } |
| 973 | |
| 974 | static bool nvme_pci_prp_save_mapping(struct request *req, |
| 975 | struct device *dma_dev, |
| 976 | struct blk_dma_iter *iter) |
| 977 | { |
| 978 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 979 | |
| 980 | if (dma_use_iova(&iod->dma_state) || !dma_need_unmap(dma_dev) || |
| 981 | (iod->flags & IOD_DATA_P2P)) |
| 982 | return true; |
| 983 | |
| 984 | if (!iod->nr_dma_vecs) { |
| 985 | struct nvme_queue *nvmeq = req->mq_hctx->driver_data; |
| 986 | |
| 987 | iod->dma_vecs = mempool_alloc(nvmeq->dev->dmavec_mempool, |
| 988 | GFP_ATOMIC); |
| 989 | if (!iod->dma_vecs) { |
| 990 | iter->status = BLK_STS_RESOURCE; |
| 991 | return false; |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | iod->dma_vecs[iod->nr_dma_vecs].addr = iter->addr; |
| 996 | iod->dma_vecs[iod->nr_dma_vecs].len = iter->len; |
| 997 | iod->nr_dma_vecs++; |
| 998 | return true; |
| 999 | } |
| 1000 | |
| 1001 | static bool nvme_pci_prp_iter_next(struct request *req, struct device *dma_dev, |
| 1002 | struct blk_dma_iter *iter) |
| 1003 | { |
| 1004 | if (iter->len) |
| 1005 | return true; |
| 1006 | if (!blk_rq_dma_map_iter_next(req, dma_dev, iter)) |
| 1007 | return false; |
| 1008 | return nvme_pci_prp_save_mapping(req, dma_dev, iter); |
| 1009 | } |
| 1010 | |
| 1011 | static void nvme_unmap_iter(struct request *req, struct blk_dma_iter *iter, |
| 1012 | struct dma_iova_state *state) |
| 1013 | { |
| 1014 | struct nvme_queue *nvmeq = req->mq_hctx->driver_data; |
| 1015 | struct device *dev = nvmeq->dev->dev; |
| 1016 | |
| 1017 | if (!blk_rq_dma_unmap(req, dev, state, iter->len, iter->p2pdma.map)) { |
| 1018 | unsigned int attrs = 0; |
| 1019 | |
| 1020 | if (iter->p2pdma.map == PCI_P2PDMA_MAP_THRU_HOST_BRIDGE) |
| 1021 | attrs |= DMA_ATTR_MMIO; |
| 1022 | |
| 1023 | dma_unmap_phys(dev, iter->addr, iter->len, rq_dma_dir(req), |
| 1024 | attrs); |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | static blk_status_t nvme_pci_setup_data_prp(struct request *req, |
| 1029 | struct blk_dma_iter *iter) |
| 1030 | { |
| 1031 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 1032 | struct nvme_queue *nvmeq = req->mq_hctx->driver_data; |
| 1033 | unsigned int length = blk_rq_payload_bytes(req); |
| 1034 | dma_addr_t prp1_dma, prp2_dma = 0; |
| 1035 | unsigned int prp_len, i; |
| 1036 | __le64 *prp_list; |
| 1037 | |
| 1038 | if (!nvme_pci_prp_save_mapping(req, nvmeq->dev->dev, iter)) { |
| 1039 | nvme_unmap_iter(req, iter, &iod->dma_state); |
| 1040 | return iter->status; |
| 1041 | } |
| 1042 | |
| 1043 | /* |
| 1044 | * PRP1 always points to the start of the DMA transfers. |
| 1045 | * |
| 1046 | * This is the only PRP (except for the list entries) that could be |
| 1047 | * non-aligned. |
| 1048 | */ |
| 1049 | prp1_dma = iter->addr; |
| 1050 | prp_len = min(length, NVME_CTRL_PAGE_SIZE - |
| 1051 | (iter->addr & (NVME_CTRL_PAGE_SIZE - 1))); |
| 1052 | iod->total_len += prp_len; |
| 1053 | iter->addr += prp_len; |
| 1054 | iter->len -= prp_len; |
| 1055 | length -= prp_len; |
| 1056 | if (!length) |
| 1057 | goto done; |
| 1058 | |
| 1059 | if (!nvme_pci_prp_iter_next(req, nvmeq->dev->dev, iter)) { |
| 1060 | if (WARN_ON_ONCE(!iter->status)) |
| 1061 | goto bad_sgl; |
| 1062 | goto done; |
| 1063 | } |
| 1064 | |
| 1065 | /* |
| 1066 | * PRP2 is usually a list, but can point to data if all data to be |
| 1067 | * transferred fits into PRP1 + PRP2: |
| 1068 | */ |
| 1069 | if (length <= NVME_CTRL_PAGE_SIZE) { |
| 1070 | prp2_dma = iter->addr; |
| 1071 | iod->total_len += length; |
| 1072 | goto done; |
| 1073 | } |
| 1074 | |
| 1075 | if (DIV_ROUND_UP(length, NVME_CTRL_PAGE_SIZE) <= |
| 1076 | NVME_SMALL_POOL_SIZE / sizeof(__le64)) |
| 1077 | iod->flags |= IOD_SMALL_DESCRIPTOR; |
| 1078 | |
| 1079 | prp_list = dma_pool_alloc(nvme_dma_pool(nvmeq, iod), GFP_ATOMIC, |
| 1080 | &prp2_dma); |
| 1081 | if (!prp_list) { |
| 1082 | iter->status = BLK_STS_RESOURCE; |
| 1083 | goto done; |
| 1084 | } |
| 1085 | iod->descriptors[iod->nr_descriptors++] = prp_list; |
| 1086 | |
| 1087 | i = 0; |
| 1088 | for (;;) { |
| 1089 | prp_list[i++] = cpu_to_le64(iter->addr); |
| 1090 | prp_len = min(length, NVME_CTRL_PAGE_SIZE); |
| 1091 | if (WARN_ON_ONCE(iter->len < prp_len)) |
| 1092 | goto bad_sgl; |
| 1093 | |
| 1094 | iod->total_len += prp_len; |
| 1095 | iter->addr += prp_len; |
| 1096 | iter->len -= prp_len; |
| 1097 | length -= prp_len; |
| 1098 | if (!length) |
| 1099 | break; |
| 1100 | |
| 1101 | if (!nvme_pci_prp_iter_next(req, nvmeq->dev->dev, iter)) { |
| 1102 | if (WARN_ON_ONCE(!iter->status)) |
| 1103 | goto bad_sgl; |
| 1104 | goto done; |
| 1105 | } |
| 1106 | |
| 1107 | /* |
| 1108 | * If we've filled the entire descriptor, allocate a new that is |
| 1109 | * pointed to be the last entry in the previous PRP list. To |
| 1110 | * accommodate for that move the last actual entry to the new |
| 1111 | * descriptor. |
| 1112 | */ |
| 1113 | if (i == NVME_CTRL_PAGE_SIZE >> 3) { |
| 1114 | __le64 *old_prp_list = prp_list; |
| 1115 | dma_addr_t prp_list_dma; |
| 1116 | |
| 1117 | prp_list = dma_pool_alloc(nvmeq->descriptor_pools.large, |
| 1118 | GFP_ATOMIC, &prp_list_dma); |
| 1119 | if (!prp_list) { |
| 1120 | iter->status = BLK_STS_RESOURCE; |
| 1121 | goto done; |
| 1122 | } |
| 1123 | iod->descriptors[iod->nr_descriptors++] = prp_list; |
| 1124 | |
| 1125 | prp_list[0] = old_prp_list[i - 1]; |
| 1126 | old_prp_list[i - 1] = cpu_to_le64(prp_list_dma); |
| 1127 | i = 1; |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | done: |
| 1132 | /* |
| 1133 | * nvme_unmap_data uses the DPT field in the SQE to tear down the |
| 1134 | * mapping, so initialize it even for failures. |
| 1135 | */ |
| 1136 | iod->cmd.common.dptr.prp1 = cpu_to_le64(prp1_dma); |
| 1137 | iod->cmd.common.dptr.prp2 = cpu_to_le64(prp2_dma); |
| 1138 | if (unlikely(iter->status)) |
| 1139 | nvme_unmap_data(req); |
| 1140 | return iter->status; |
| 1141 | |
| 1142 | bad_sgl: |
| 1143 | dev_err_once(nvmeq->dev->dev, |
| 1144 | "Incorrectly formed request for payload:%d nents:%d\n", |
| 1145 | blk_rq_payload_bytes(req), blk_rq_nr_phys_segments(req)); |
| 1146 | nvme_unmap_data(req); |
| 1147 | return BLK_STS_IOERR; |
| 1148 | } |
| 1149 | |
| 1150 | static void nvme_pci_sgl_set_data(struct nvme_sgl_desc *sge, |
| 1151 | struct blk_dma_iter *iter) |
| 1152 | { |
| 1153 | sge->addr = cpu_to_le64(iter->addr); |
| 1154 | sge->length = cpu_to_le32(iter->len); |
| 1155 | sge->type = NVME_SGL_FMT_DATA_DESC << 4; |
| 1156 | } |
| 1157 | |
| 1158 | static void nvme_pci_sgl_set_seg(struct nvme_sgl_desc *sge, |
| 1159 | dma_addr_t dma_addr, int entries) |
| 1160 | { |
| 1161 | sge->addr = cpu_to_le64(dma_addr); |
| 1162 | sge->length = cpu_to_le32(entries * sizeof(*sge)); |
| 1163 | sge->type = NVME_SGL_FMT_LAST_SEG_DESC << 4; |
| 1164 | } |
| 1165 | |
| 1166 | static blk_status_t nvme_pci_setup_data_sgl(struct request *req, |
| 1167 | struct blk_dma_iter *iter) |
| 1168 | { |
| 1169 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 1170 | struct nvme_queue *nvmeq = req->mq_hctx->driver_data; |
| 1171 | unsigned int entries = blk_rq_nr_phys_segments(req); |
| 1172 | struct nvme_sgl_desc *sg_list; |
| 1173 | dma_addr_t sgl_dma; |
| 1174 | unsigned int mapped = 0; |
| 1175 | |
| 1176 | /* set the transfer type as SGL */ |
| 1177 | iod->cmd.common.flags = NVME_CMD_SGL_METABUF; |
| 1178 | |
| 1179 | if (entries == 1 || blk_rq_dma_map_coalesce(&iod->dma_state)) { |
| 1180 | nvme_pci_sgl_set_data(&iod->cmd.common.dptr.sgl, iter); |
| 1181 | iod->total_len += iter->len; |
| 1182 | return BLK_STS_OK; |
| 1183 | } |
| 1184 | |
| 1185 | if (entries <= NVME_SMALL_POOL_SIZE / sizeof(*sg_list)) |
| 1186 | iod->flags |= IOD_SMALL_DESCRIPTOR; |
| 1187 | |
| 1188 | sg_list = dma_pool_alloc(nvme_dma_pool(nvmeq, iod), GFP_ATOMIC, |
| 1189 | &sgl_dma); |
| 1190 | if (!sg_list) { |
| 1191 | nvme_unmap_iter(req, iter, &iod->dma_state); |
| 1192 | return BLK_STS_RESOURCE; |
| 1193 | } |
| 1194 | |
| 1195 | iod->descriptors[iod->nr_descriptors++] = sg_list; |
| 1196 | |
| 1197 | do { |
| 1198 | if (WARN_ON_ONCE(mapped == entries)) { |
| 1199 | iter->status = BLK_STS_IOERR; |
| 1200 | break; |
| 1201 | } |
| 1202 | nvme_pci_sgl_set_data(&sg_list[mapped++], iter); |
| 1203 | iod->total_len += iter->len; |
| 1204 | } while (blk_rq_dma_map_iter_next(req, nvmeq->dev->dev, iter)); |
| 1205 | |
| 1206 | nvme_pci_sgl_set_seg(&iod->cmd.common.dptr.sgl, sgl_dma, mapped); |
| 1207 | if (unlikely(iter->status)) |
| 1208 | nvme_unmap_data(req); |
| 1209 | return iter->status; |
| 1210 | } |
| 1211 | |
| 1212 | static blk_status_t nvme_pci_setup_data_simple(struct request *req, |
| 1213 | enum nvme_use_sgl use_sgl) |
| 1214 | { |
| 1215 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 1216 | struct nvme_queue *nvmeq = req->mq_hctx->driver_data; |
| 1217 | struct bio_vec bv = req_bvec(req); |
| 1218 | unsigned int prp1_offset = bv.bv_offset & (NVME_CTRL_PAGE_SIZE - 1); |
| 1219 | bool prp_possible = prp1_offset + bv.bv_len <= NVME_CTRL_PAGE_SIZE * 2; |
| 1220 | dma_addr_t dma_addr; |
| 1221 | |
| 1222 | if (!use_sgl && !prp_possible) |
| 1223 | return BLK_STS_AGAIN; |
| 1224 | if (is_pci_p2pdma_page(bv.bv_page)) |
| 1225 | return BLK_STS_AGAIN; |
| 1226 | |
| 1227 | dma_addr = dma_map_bvec(nvmeq->dev->dev, &bv, rq_dma_dir(req), 0); |
| 1228 | if (dma_mapping_error(nvmeq->dev->dev, dma_addr)) |
| 1229 | return BLK_STS_RESOURCE; |
| 1230 | iod->total_len = bv.bv_len; |
| 1231 | iod->flags |= IOD_SINGLE_SEGMENT; |
| 1232 | |
| 1233 | if (use_sgl == SGL_FORCED || !prp_possible) { |
| 1234 | iod->cmd.common.flags = NVME_CMD_SGL_METABUF; |
| 1235 | iod->cmd.common.dptr.sgl.addr = cpu_to_le64(dma_addr); |
| 1236 | iod->cmd.common.dptr.sgl.length = cpu_to_le32(bv.bv_len); |
| 1237 | iod->cmd.common.dptr.sgl.type = NVME_SGL_FMT_DATA_DESC << 4; |
| 1238 | } else { |
| 1239 | unsigned int first_prp_len = NVME_CTRL_PAGE_SIZE - prp1_offset; |
| 1240 | |
| 1241 | iod->cmd.common.dptr.prp1 = cpu_to_le64(dma_addr); |
| 1242 | iod->cmd.common.dptr.prp2 = 0; |
| 1243 | if (bv.bv_len > first_prp_len) |
| 1244 | iod->cmd.common.dptr.prp2 = |
| 1245 | cpu_to_le64(dma_addr + first_prp_len); |
| 1246 | } |
| 1247 | |
| 1248 | return BLK_STS_OK; |
| 1249 | } |
| 1250 | |
| 1251 | static blk_status_t nvme_map_data(struct request *req) |
| 1252 | { |
| 1253 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 1254 | struct nvme_queue *nvmeq = req->mq_hctx->driver_data; |
| 1255 | struct nvme_dev *dev = nvmeq->dev; |
| 1256 | enum nvme_use_sgl use_sgl = nvme_pci_use_sgls(dev, req); |
| 1257 | struct blk_dma_iter iter; |
| 1258 | blk_status_t ret; |
| 1259 | |
| 1260 | /* |
| 1261 | * Try to skip the DMA iterator for single segment requests, as that |
| 1262 | * significantly improves performances for small I/O sizes. |
| 1263 | */ |
| 1264 | if (blk_rq_nr_phys_segments(req) == 1) { |
| 1265 | ret = nvme_pci_setup_data_simple(req, use_sgl); |
| 1266 | if (ret != BLK_STS_AGAIN) |
| 1267 | return ret; |
| 1268 | } |
| 1269 | |
| 1270 | if (!blk_rq_dma_map_iter_start(req, dev->dev, &iod->dma_state, &iter)) |
| 1271 | return iter.status; |
| 1272 | |
| 1273 | switch (iter.p2pdma.map) { |
| 1274 | case PCI_P2PDMA_MAP_BUS_ADDR: |
| 1275 | iod->flags |= IOD_DATA_P2P; |
| 1276 | break; |
| 1277 | case PCI_P2PDMA_MAP_THRU_HOST_BRIDGE: |
| 1278 | iod->flags |= IOD_DATA_MMIO; |
| 1279 | break; |
| 1280 | case PCI_P2PDMA_MAP_NONE: |
| 1281 | break; |
| 1282 | default: |
| 1283 | return BLK_STS_RESOURCE; |
| 1284 | } |
| 1285 | |
| 1286 | if (use_sgl == SGL_FORCED || |
| 1287 | (use_sgl == SGL_SUPPORTED && |
| 1288 | (sgl_threshold && nvme_pci_avg_seg_size(req) >= sgl_threshold))) |
| 1289 | return nvme_pci_setup_data_sgl(req, &iter); |
| 1290 | return nvme_pci_setup_data_prp(req, &iter); |
| 1291 | } |
| 1292 | |
| 1293 | static blk_status_t nvme_pci_setup_meta_iter(struct request *req) |
| 1294 | { |
| 1295 | struct nvme_queue *nvmeq = req->mq_hctx->driver_data; |
| 1296 | unsigned int entries = req->nr_integrity_segments; |
| 1297 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 1298 | struct nvme_dev *dev = nvmeq->dev; |
| 1299 | struct nvme_sgl_desc *sg_list; |
| 1300 | struct blk_dma_iter iter; |
| 1301 | dma_addr_t sgl_dma; |
| 1302 | int i = 0; |
| 1303 | |
| 1304 | if (!blk_rq_integrity_dma_map_iter_start(req, dev->dev, |
| 1305 | &iod->meta_dma_state, &iter)) |
| 1306 | return iter.status; |
| 1307 | |
| 1308 | switch (iter.p2pdma.map) { |
| 1309 | case PCI_P2PDMA_MAP_BUS_ADDR: |
| 1310 | iod->flags |= IOD_META_P2P; |
| 1311 | break; |
| 1312 | case PCI_P2PDMA_MAP_THRU_HOST_BRIDGE: |
| 1313 | iod->flags |= IOD_META_MMIO; |
| 1314 | break; |
| 1315 | case PCI_P2PDMA_MAP_NONE: |
| 1316 | break; |
| 1317 | default: |
| 1318 | return BLK_STS_RESOURCE; |
| 1319 | } |
| 1320 | |
| 1321 | if (blk_rq_dma_map_coalesce(&iod->meta_dma_state)) |
| 1322 | entries = 1; |
| 1323 | |
| 1324 | /* |
| 1325 | * The NVMe MPTR descriptor has an implicit length that the host and |
| 1326 | * device must agree on to avoid data/memory corruption. We trust the |
| 1327 | * kernel allocated correctly based on the format's parameters, so use |
| 1328 | * the more efficient MPTR to avoid extra dma pool allocations for the |
| 1329 | * SGL indirection. |
| 1330 | * |
| 1331 | * But for user commands, we don't necessarily know what they do, so |
| 1332 | * the driver can't validate the metadata buffer size. The SGL |
| 1333 | * descriptor provides an explicit length, so we're relying on that |
| 1334 | * mechanism to catch any misunderstandings between the application and |
| 1335 | * device. |
| 1336 | * |
| 1337 | * P2P DMA also needs to use the blk_dma_iter method, so mptr setup |
| 1338 | * leverages this routine when that happens. |
| 1339 | */ |
| 1340 | if (!nvme_ctrl_meta_sgl_supported(&dev->ctrl) || |
| 1341 | (entries == 1 && !(nvme_req(req)->flags & NVME_REQ_USERCMD))) { |
| 1342 | iod->cmd.common.metadata = cpu_to_le64(iter.addr); |
| 1343 | iod->meta_total_len = iter.len; |
| 1344 | iod->meta_dma = iter.addr; |
| 1345 | iod->meta_descriptor = NULL; |
| 1346 | return BLK_STS_OK; |
| 1347 | } |
| 1348 | |
| 1349 | sg_list = dma_pool_alloc(nvmeq->descriptor_pools.small, GFP_ATOMIC, |
| 1350 | &sgl_dma); |
| 1351 | if (!sg_list) { |
| 1352 | nvme_unmap_iter(req, &iter, &iod->meta_dma_state); |
| 1353 | return BLK_STS_RESOURCE; |
| 1354 | } |
| 1355 | |
| 1356 | iod->meta_descriptor = sg_list; |
| 1357 | iod->meta_dma = sgl_dma; |
| 1358 | iod->cmd.common.flags = NVME_CMD_SGL_METASEG; |
| 1359 | iod->cmd.common.metadata = cpu_to_le64(sgl_dma); |
| 1360 | if (entries == 1) { |
| 1361 | iod->meta_total_len = iter.len; |
| 1362 | nvme_pci_sgl_set_data(sg_list, &iter); |
| 1363 | return BLK_STS_OK; |
| 1364 | } |
| 1365 | |
| 1366 | sgl_dma += sizeof(*sg_list); |
| 1367 | do { |
| 1368 | nvme_pci_sgl_set_data(&sg_list[++i], &iter); |
| 1369 | iod->meta_total_len += iter.len; |
| 1370 | } while (blk_rq_integrity_dma_map_iter_next(req, dev->dev, &iter)); |
| 1371 | |
| 1372 | nvme_pci_sgl_set_seg(sg_list, sgl_dma, i); |
| 1373 | if (unlikely(iter.status)) |
| 1374 | nvme_unmap_metadata(req); |
| 1375 | return iter.status; |
| 1376 | } |
| 1377 | |
| 1378 | static blk_status_t nvme_pci_setup_meta_mptr(struct request *req) |
| 1379 | { |
| 1380 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 1381 | struct nvme_queue *nvmeq = req->mq_hctx->driver_data; |
| 1382 | struct bio_vec bv = rq_integrity_vec(req); |
| 1383 | |
| 1384 | if (is_pci_p2pdma_page(bv.bv_page)) |
| 1385 | return nvme_pci_setup_meta_iter(req); |
| 1386 | |
| 1387 | iod->meta_dma = dma_map_bvec(nvmeq->dev->dev, &bv, rq_dma_dir(req), 0); |
| 1388 | if (dma_mapping_error(nvmeq->dev->dev, iod->meta_dma)) |
| 1389 | return BLK_STS_IOERR; |
| 1390 | iod->cmd.common.metadata = cpu_to_le64(iod->meta_dma); |
| 1391 | iod->flags |= IOD_SINGLE_META_SEGMENT; |
| 1392 | return BLK_STS_OK; |
| 1393 | } |
| 1394 | |
| 1395 | static blk_status_t nvme_map_metadata(struct request *req) |
| 1396 | { |
| 1397 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 1398 | |
| 1399 | if ((iod->cmd.common.flags & NVME_CMD_SGL_METABUF) && |
| 1400 | nvme_pci_metadata_use_sgls(req)) |
| 1401 | return nvme_pci_setup_meta_iter(req); |
| 1402 | return nvme_pci_setup_meta_mptr(req); |
| 1403 | } |
| 1404 | |
| 1405 | static blk_status_t nvme_prep_rq(struct request *req) |
| 1406 | { |
| 1407 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 1408 | blk_status_t ret; |
| 1409 | |
| 1410 | iod->flags = 0; |
| 1411 | iod->nr_descriptors = 0; |
| 1412 | iod->total_len = 0; |
| 1413 | iod->meta_total_len = 0; |
| 1414 | iod->nr_dma_vecs = 0; |
| 1415 | |
| 1416 | ret = nvme_setup_cmd(req->q->queuedata, req); |
| 1417 | if (ret) |
| 1418 | return ret; |
| 1419 | |
| 1420 | if (blk_rq_nr_phys_segments(req)) { |
| 1421 | ret = nvme_map_data(req); |
| 1422 | if (ret) |
| 1423 | goto out_free_cmd; |
| 1424 | } |
| 1425 | |
| 1426 | if (blk_integrity_rq(req)) { |
| 1427 | ret = nvme_map_metadata(req); |
| 1428 | if (ret) |
| 1429 | goto out_unmap_data; |
| 1430 | } |
| 1431 | |
| 1432 | nvme_start_request(req); |
| 1433 | return BLK_STS_OK; |
| 1434 | out_unmap_data: |
| 1435 | if (blk_rq_nr_phys_segments(req)) |
| 1436 | nvme_unmap_data(req); |
| 1437 | out_free_cmd: |
| 1438 | nvme_cleanup_cmd(req); |
| 1439 | return ret; |
| 1440 | } |
| 1441 | |
| 1442 | static blk_status_t nvme_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 1443 | const struct blk_mq_queue_data *bd) |
| 1444 | { |
| 1445 | struct nvme_queue *nvmeq = hctx->driver_data; |
| 1446 | struct nvme_dev *dev = nvmeq->dev; |
| 1447 | struct request *req = bd->rq; |
| 1448 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 1449 | blk_status_t ret; |
| 1450 | |
| 1451 | /* |
| 1452 | * We should not need to do this, but we're still using this to |
| 1453 | * ensure we can drain requests on a dying queue. |
| 1454 | */ |
| 1455 | if (unlikely(!test_bit(NVMEQ_ENABLED, &nvmeq->flags))) |
| 1456 | return BLK_STS_IOERR; |
| 1457 | |
| 1458 | if (unlikely(!nvme_check_ready(&dev->ctrl, req, true))) |
| 1459 | return nvme_fail_nonready_command(&dev->ctrl, req); |
| 1460 | |
| 1461 | ret = nvme_prep_rq(req); |
| 1462 | if (unlikely(ret)) |
| 1463 | return ret; |
| 1464 | spin_lock(&nvmeq->sq_lock); |
| 1465 | nvme_sq_copy_cmd(nvmeq, &iod->cmd); |
| 1466 | nvme_write_sq_db(nvmeq, bd->last); |
| 1467 | spin_unlock(&nvmeq->sq_lock); |
| 1468 | return BLK_STS_OK; |
| 1469 | } |
| 1470 | |
| 1471 | static void nvme_submit_cmds(struct nvme_queue *nvmeq, struct rq_list *rqlist) |
| 1472 | { |
| 1473 | struct request *req; |
| 1474 | |
| 1475 | if (rq_list_empty(rqlist)) |
| 1476 | return; |
| 1477 | |
| 1478 | spin_lock(&nvmeq->sq_lock); |
| 1479 | while ((req = rq_list_pop(rqlist))) { |
| 1480 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 1481 | |
| 1482 | nvme_sq_copy_cmd(nvmeq, &iod->cmd); |
| 1483 | } |
| 1484 | nvme_write_sq_db(nvmeq, true); |
| 1485 | spin_unlock(&nvmeq->sq_lock); |
| 1486 | } |
| 1487 | |
| 1488 | static bool nvme_prep_rq_batch(struct nvme_queue *nvmeq, struct request *req) |
| 1489 | { |
| 1490 | /* |
| 1491 | * We should not need to do this, but we're still using this to |
| 1492 | * ensure we can drain requests on a dying queue. |
| 1493 | */ |
| 1494 | if (unlikely(!test_bit(NVMEQ_ENABLED, &nvmeq->flags))) |
| 1495 | return false; |
| 1496 | if (unlikely(!nvme_check_ready(&nvmeq->dev->ctrl, req, true))) |
| 1497 | return false; |
| 1498 | |
| 1499 | return nvme_prep_rq(req) == BLK_STS_OK; |
| 1500 | } |
| 1501 | |
| 1502 | static void nvme_queue_rqs(struct rq_list *rqlist) |
| 1503 | { |
| 1504 | struct rq_list submit_list = { }; |
| 1505 | struct rq_list requeue_list = { }; |
| 1506 | struct nvme_queue *nvmeq = NULL; |
| 1507 | struct request *req; |
| 1508 | |
| 1509 | while ((req = rq_list_pop(rqlist))) { |
| 1510 | if (nvmeq && nvmeq != req->mq_hctx->driver_data) |
| 1511 | nvme_submit_cmds(nvmeq, &submit_list); |
| 1512 | nvmeq = req->mq_hctx->driver_data; |
| 1513 | |
| 1514 | if (nvme_prep_rq_batch(nvmeq, req)) |
| 1515 | rq_list_add_tail(&submit_list, req); |
| 1516 | else |
| 1517 | rq_list_add_tail(&requeue_list, req); |
| 1518 | } |
| 1519 | |
| 1520 | if (nvmeq) |
| 1521 | nvme_submit_cmds(nvmeq, &submit_list); |
| 1522 | *rqlist = requeue_list; |
| 1523 | } |
| 1524 | |
| 1525 | static __always_inline void nvme_pci_unmap_rq(struct request *req) |
| 1526 | { |
| 1527 | if (blk_integrity_rq(req)) |
| 1528 | nvme_unmap_metadata(req); |
| 1529 | if (blk_rq_nr_phys_segments(req)) |
| 1530 | nvme_unmap_data(req); |
| 1531 | } |
| 1532 | |
| 1533 | static void nvme_pci_complete_rq(struct request *req) |
| 1534 | { |
| 1535 | nvme_pci_unmap_rq(req); |
| 1536 | nvme_complete_rq(req); |
| 1537 | } |
| 1538 | |
| 1539 | static void nvme_pci_complete_batch(struct io_comp_batch *iob) |
| 1540 | { |
| 1541 | nvme_complete_batch(iob, nvme_pci_unmap_rq); |
| 1542 | } |
| 1543 | |
| 1544 | /* We read the CQE phase first to check if the rest of the entry is valid */ |
| 1545 | static inline bool nvme_cqe_pending(struct nvme_queue *nvmeq) |
| 1546 | { |
| 1547 | struct nvme_completion *hcqe = &nvmeq->cqes[nvmeq->cq_head]; |
| 1548 | |
| 1549 | return (le16_to_cpu(READ_ONCE(hcqe->status)) & 1) == nvmeq->cq_phase; |
| 1550 | } |
| 1551 | |
| 1552 | static inline void nvme_ring_cq_doorbell(struct nvme_queue *nvmeq) |
| 1553 | { |
| 1554 | u16 head = nvmeq->cq_head; |
| 1555 | |
| 1556 | if (nvme_dbbuf_update_and_check_event(head, nvmeq->dbbuf_cq_db, |
| 1557 | nvmeq->dbbuf_cq_ei)) |
| 1558 | writel(head, nvmeq->q_db + nvmeq->dev->db_stride); |
| 1559 | } |
| 1560 | |
| 1561 | static inline struct blk_mq_tags *nvme_queue_tagset(struct nvme_queue *nvmeq) |
| 1562 | { |
| 1563 | if (!nvmeq->qid) |
| 1564 | return nvmeq->dev->admin_tagset.tags[0]; |
| 1565 | return nvmeq->dev->tagset.tags[nvmeq->qid - 1]; |
| 1566 | } |
| 1567 | |
| 1568 | static inline void nvme_handle_cqe(struct nvme_queue *nvmeq, |
| 1569 | struct io_comp_batch *iob, u16 idx) |
| 1570 | { |
| 1571 | struct nvme_completion *cqe = &nvmeq->cqes[idx]; |
| 1572 | __u16 command_id = READ_ONCE(cqe->command_id); |
| 1573 | struct request *req; |
| 1574 | |
| 1575 | /* |
| 1576 | * AEN requests are special as they don't time out and can |
| 1577 | * survive any kind of queue freeze and often don't respond to |
| 1578 | * aborts. We don't even bother to allocate a struct request |
| 1579 | * for them but rather special case them here. |
| 1580 | */ |
| 1581 | if (unlikely(nvme_is_aen_req(nvmeq->qid, command_id))) { |
| 1582 | nvme_complete_async_event(&nvmeq->dev->ctrl, |
| 1583 | cqe->status, &cqe->result); |
| 1584 | return; |
| 1585 | } |
| 1586 | |
| 1587 | req = nvme_find_rq(nvme_queue_tagset(nvmeq), command_id); |
| 1588 | if (unlikely(!req)) { |
| 1589 | dev_warn_ratelimited(nvmeq->dev->ctrl.device, |
| 1590 | "invalid id %d completed on queue %d\n", |
| 1591 | command_id, le16_to_cpu(cqe->sq_id)); |
| 1592 | return; |
| 1593 | } |
| 1594 | |
| 1595 | /* |
| 1596 | * Tracing only; annotate a lockless snapshot of nvmeq->sq_tail using |
| 1597 | * data_race(). This would also help suppress context analysis warning |
| 1598 | * while accessing nvmeq->sq_tail without acquiring ->sq_lock. |
| 1599 | */ |
| 1600 | trace_nvme_sq(req, cqe->sq_head, data_race(nvmeq->sq_tail)); |
| 1601 | if (!nvme_try_complete_req(req, cqe->status, cqe->result) && |
| 1602 | !blk_mq_add_to_batch(req, iob, |
| 1603 | nvme_req(req)->status != NVME_SC_SUCCESS, |
| 1604 | nvme_pci_complete_batch)) |
| 1605 | nvme_pci_complete_rq(req); |
| 1606 | } |
| 1607 | |
| 1608 | static inline void nvme_update_cq_head(struct nvme_queue *nvmeq) |
| 1609 | { |
| 1610 | u32 tmp = nvmeq->cq_head + 1; |
| 1611 | |
| 1612 | if (tmp == nvmeq->q_depth) { |
| 1613 | nvmeq->cq_head = 0; |
| 1614 | nvmeq->cq_phase ^= 1; |
| 1615 | } else { |
| 1616 | nvmeq->cq_head = tmp; |
| 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | static inline bool nvme_poll_cq(struct nvme_queue *nvmeq, |
| 1621 | struct io_comp_batch *iob) |
| 1622 | { |
| 1623 | bool found = false; |
| 1624 | |
| 1625 | while (nvme_cqe_pending(nvmeq)) { |
| 1626 | found = true; |
| 1627 | /* |
| 1628 | * load-load control dependency between phase and the rest of |
| 1629 | * the cqe requires a full read memory barrier |
| 1630 | */ |
| 1631 | dma_rmb(); |
| 1632 | nvme_handle_cqe(nvmeq, iob, nvmeq->cq_head); |
| 1633 | nvme_update_cq_head(nvmeq); |
| 1634 | } |
| 1635 | |
| 1636 | if (found) |
| 1637 | nvme_ring_cq_doorbell(nvmeq); |
| 1638 | return found; |
| 1639 | } |
| 1640 | |
| 1641 | static irqreturn_t nvme_irq(int irq, void *data) |
| 1642 | { |
| 1643 | struct nvme_queue *nvmeq = data; |
| 1644 | DEFINE_IO_COMP_BATCH(iob); |
| 1645 | |
| 1646 | if (nvme_poll_cq(nvmeq, &iob)) { |
| 1647 | if (!rq_list_empty(&iob.req_list)) |
| 1648 | nvme_pci_complete_batch(&iob); |
| 1649 | return IRQ_HANDLED; |
| 1650 | } |
| 1651 | return IRQ_NONE; |
| 1652 | } |
| 1653 | |
| 1654 | static irqreturn_t nvme_irq_check(int irq, void *data) |
| 1655 | { |
| 1656 | struct nvme_queue *nvmeq = data; |
| 1657 | |
| 1658 | if (nvme_cqe_pending(nvmeq)) |
| 1659 | return IRQ_WAKE_THREAD; |
| 1660 | return IRQ_NONE; |
| 1661 | } |
| 1662 | |
| 1663 | /* |
| 1664 | * Poll for completions for any interrupt driven queue |
| 1665 | * Can be called from any context. |
| 1666 | */ |
| 1667 | static void nvme_poll_irqdisable(struct nvme_queue *nvmeq) |
| 1668 | { |
| 1669 | struct pci_dev *pdev = to_pci_dev(nvmeq->dev->dev); |
| 1670 | int irq; |
| 1671 | |
| 1672 | WARN_ON_ONCE(test_bit(NVMEQ_POLLED, &nvmeq->flags)); |
| 1673 | |
| 1674 | irq = pci_irq_vector(pdev, nvmeq->cq_vector); |
| 1675 | disable_irq(irq); |
| 1676 | spin_lock(&nvmeq->cq_poll_lock); |
| 1677 | nvme_poll_cq(nvmeq, NULL); |
| 1678 | spin_unlock(&nvmeq->cq_poll_lock); |
| 1679 | enable_irq(irq); |
| 1680 | } |
| 1681 | |
| 1682 | static int nvme_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob) |
| 1683 | { |
| 1684 | struct nvme_queue *nvmeq = hctx->driver_data; |
| 1685 | bool found; |
| 1686 | |
| 1687 | if (!test_bit(NVMEQ_POLLED, &nvmeq->flags) || |
| 1688 | !nvme_cqe_pending(nvmeq)) |
| 1689 | return 0; |
| 1690 | |
| 1691 | spin_lock(&nvmeq->cq_poll_lock); |
| 1692 | found = nvme_poll_cq(nvmeq, iob); |
| 1693 | spin_unlock(&nvmeq->cq_poll_lock); |
| 1694 | |
| 1695 | return found; |
| 1696 | } |
| 1697 | |
| 1698 | static void nvme_pci_submit_async_event(struct nvme_ctrl *ctrl) |
| 1699 | { |
| 1700 | struct nvme_dev *dev = to_nvme_dev(ctrl); |
| 1701 | struct nvme_queue *nvmeq = &dev->queues[0]; |
| 1702 | struct nvme_command c = { }; |
| 1703 | |
| 1704 | c.common.opcode = nvme_admin_async_event; |
| 1705 | c.common.command_id = NVME_AQ_BLK_MQ_DEPTH; |
| 1706 | |
| 1707 | spin_lock(&nvmeq->sq_lock); |
| 1708 | nvme_sq_copy_cmd(nvmeq, &c); |
| 1709 | nvme_write_sq_db(nvmeq, true); |
| 1710 | spin_unlock(&nvmeq->sq_lock); |
| 1711 | } |
| 1712 | |
| 1713 | static int nvme_pci_subsystem_reset(struct nvme_ctrl *ctrl) |
| 1714 | { |
| 1715 | struct nvme_dev *dev = to_nvme_dev(ctrl); |
| 1716 | int ret = 0; |
| 1717 | |
| 1718 | /* |
| 1719 | * Taking the shutdown_lock ensures the BAR mapping is not being |
| 1720 | * altered by reset_work. Holding this lock before the RESETTING state |
| 1721 | * change, if successful, also ensures nvme_remove won't be able to |
| 1722 | * proceed to iounmap until we're done. |
| 1723 | */ |
| 1724 | mutex_lock(&dev->shutdown_lock); |
| 1725 | if (!dev->bar_mapped_size) { |
| 1726 | ret = -ENODEV; |
| 1727 | goto unlock; |
| 1728 | } |
| 1729 | |
| 1730 | if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING)) { |
| 1731 | ret = -EBUSY; |
| 1732 | goto unlock; |
| 1733 | } |
| 1734 | |
| 1735 | writel(NVME_SUBSYS_RESET, dev->bar + NVME_REG_NSSR); |
| 1736 | |
| 1737 | if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING) || |
| 1738 | !nvme_change_ctrl_state(ctrl, NVME_CTRL_LIVE)) |
| 1739 | goto unlock; |
| 1740 | |
| 1741 | /* |
| 1742 | * Read controller status to flush the previous write and trigger a |
| 1743 | * pcie read error. |
| 1744 | */ |
| 1745 | readl(dev->bar + NVME_REG_CSTS); |
| 1746 | unlock: |
| 1747 | mutex_unlock(&dev->shutdown_lock); |
| 1748 | return ret; |
| 1749 | } |
| 1750 | |
| 1751 | static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id) |
| 1752 | { |
| 1753 | struct nvme_command c = { }; |
| 1754 | |
| 1755 | c.delete_queue.opcode = opcode; |
| 1756 | c.delete_queue.qid = cpu_to_le16(id); |
| 1757 | |
| 1758 | return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0); |
| 1759 | } |
| 1760 | |
| 1761 | static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid, |
| 1762 | struct nvme_queue *nvmeq, s16 vector) |
| 1763 | { |
| 1764 | struct nvme_command c = { }; |
| 1765 | int flags = NVME_QUEUE_PHYS_CONTIG; |
| 1766 | |
| 1767 | if (!test_bit(NVMEQ_POLLED, &nvmeq->flags)) |
| 1768 | flags |= NVME_CQ_IRQ_ENABLED; |
| 1769 | |
| 1770 | /* |
| 1771 | * Note: we (ab)use the fact that the prp fields survive if no data |
| 1772 | * is attached to the request. |
| 1773 | */ |
| 1774 | c.create_cq.opcode = nvme_admin_create_cq; |
| 1775 | c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr); |
| 1776 | c.create_cq.cqid = cpu_to_le16(qid); |
| 1777 | c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1); |
| 1778 | c.create_cq.cq_flags = cpu_to_le16(flags); |
| 1779 | c.create_cq.irq_vector = cpu_to_le16(vector); |
| 1780 | |
| 1781 | return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0); |
| 1782 | } |
| 1783 | |
| 1784 | static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid, |
| 1785 | struct nvme_queue *nvmeq) |
| 1786 | { |
| 1787 | struct nvme_ctrl *ctrl = &dev->ctrl; |
| 1788 | struct nvme_command c = { }; |
| 1789 | int flags = NVME_QUEUE_PHYS_CONTIG; |
| 1790 | |
| 1791 | /* |
| 1792 | * Some drives have a bug that auto-enables WRRU if MEDIUM isn't |
| 1793 | * set. Since URGENT priority is zeroes, it makes all queues |
| 1794 | * URGENT. |
| 1795 | */ |
| 1796 | if (ctrl->quirks & NVME_QUIRK_MEDIUM_PRIO_SQ) |
| 1797 | flags |= NVME_SQ_PRIO_MEDIUM; |
| 1798 | |
| 1799 | /* |
| 1800 | * Note: we (ab)use the fact that the prp fields survive if no data |
| 1801 | * is attached to the request. |
| 1802 | */ |
| 1803 | c.create_sq.opcode = nvme_admin_create_sq; |
| 1804 | c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr); |
| 1805 | c.create_sq.sqid = cpu_to_le16(qid); |
| 1806 | c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1); |
| 1807 | c.create_sq.sq_flags = cpu_to_le16(flags); |
| 1808 | c.create_sq.cqid = cpu_to_le16(qid); |
| 1809 | |
| 1810 | return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0); |
| 1811 | } |
| 1812 | |
| 1813 | static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid) |
| 1814 | { |
| 1815 | return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid); |
| 1816 | } |
| 1817 | |
| 1818 | static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid) |
| 1819 | { |
| 1820 | return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid); |
| 1821 | } |
| 1822 | |
| 1823 | static enum rq_end_io_ret abort_endio(struct request *req, blk_status_t error, |
| 1824 | const struct io_comp_batch *iob) |
| 1825 | { |
| 1826 | struct nvme_queue *nvmeq = req->mq_hctx->driver_data; |
| 1827 | |
| 1828 | dev_warn(nvmeq->dev->ctrl.device, |
| 1829 | "Abort status: 0x%x", nvme_req(req)->status); |
| 1830 | atomic_inc(&nvmeq->dev->ctrl.abort_limit); |
| 1831 | blk_mq_free_request(req); |
| 1832 | return RQ_END_IO_NONE; |
| 1833 | } |
| 1834 | |
| 1835 | static bool nvme_should_reset(struct nvme_dev *dev, u32 csts) |
| 1836 | { |
| 1837 | /* If true, indicates loss of adapter communication, possibly by a |
| 1838 | * NVMe Subsystem reset. |
| 1839 | */ |
| 1840 | bool nssro = dev->subsystem && (csts & NVME_CSTS_NSSRO); |
| 1841 | |
| 1842 | /* If there is a reset/reinit ongoing, we shouldn't reset again. */ |
| 1843 | switch (nvme_ctrl_state(&dev->ctrl)) { |
| 1844 | case NVME_CTRL_RESETTING: |
| 1845 | case NVME_CTRL_CONNECTING: |
| 1846 | return false; |
| 1847 | default: |
| 1848 | break; |
| 1849 | } |
| 1850 | |
| 1851 | /* We shouldn't reset unless the controller is on fatal error state |
| 1852 | * _or_ if we lost the communication with it. |
| 1853 | */ |
| 1854 | if (!(csts & NVME_CSTS_CFS) && !nssro) |
| 1855 | return false; |
| 1856 | |
| 1857 | return true; |
| 1858 | } |
| 1859 | |
| 1860 | static void nvme_warn_reset(struct nvme_dev *dev, u32 csts) |
| 1861 | { |
| 1862 | /* Read a config register to help see what died. */ |
| 1863 | u16 pci_status; |
| 1864 | int result; |
| 1865 | |
| 1866 | result = pci_read_config_word(to_pci_dev(dev->dev), PCI_STATUS, |
| 1867 | &pci_status); |
| 1868 | if (result == PCIBIOS_SUCCESSFUL) |
| 1869 | dev_warn(dev->ctrl.device, |
| 1870 | "controller is down; will reset: CSTS=0x%x, PCI_STATUS=0x%hx\n", |
| 1871 | csts, pci_status); |
| 1872 | else |
| 1873 | dev_warn(dev->ctrl.device, |
| 1874 | "controller is down; will reset: CSTS=0x%x, PCI_STATUS read failed (%d)\n", |
| 1875 | csts, result); |
| 1876 | |
| 1877 | if (csts != ~0) |
| 1878 | return; |
| 1879 | |
| 1880 | dev_warn(dev->ctrl.device, |
| 1881 | "Does your device have a faulty power saving mode enabled?\n"); |
| 1882 | dev_warn(dev->ctrl.device, |
| 1883 | "Try \"nvme_core.default_ps_max_latency_us=0 pcie_aspm=off pcie_port_pm=off\" and report a bug\n"); |
| 1884 | } |
| 1885 | |
| 1886 | static enum blk_eh_timer_return nvme_timeout(struct request *req) |
| 1887 | { |
| 1888 | struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 1889 | struct nvme_queue *nvmeq = req->mq_hctx->driver_data; |
| 1890 | struct nvme_dev *dev = nvmeq->dev; |
| 1891 | struct request *abort_req; |
| 1892 | struct nvme_command cmd = { }; |
| 1893 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
| 1894 | u32 csts = readl(dev->bar + NVME_REG_CSTS); |
| 1895 | u8 opcode; |
| 1896 | |
| 1897 | /* |
| 1898 | * Shutdown the device immediately if we see it is disconnected. This |
| 1899 | * unblocks PCIe error handling if the nvme driver is waiting in |
| 1900 | * error_resume for a device that has been removed. We can't unbind the |
| 1901 | * driver while the driver's error callback is waiting to complete, so |
| 1902 | * we're relying on a timeout to break that deadlock if a removal |
| 1903 | * occurs while reset work is running. |
| 1904 | */ |
| 1905 | if (pci_dev_is_disconnected(pdev)) |
| 1906 | nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING); |
| 1907 | if (nvme_state_terminal(&dev->ctrl)) |
| 1908 | goto disable; |
| 1909 | |
| 1910 | /* If PCI error recovery process is happening, we cannot reset or |
| 1911 | * the recovery mechanism will surely fail. |
| 1912 | */ |
| 1913 | mb(); |
| 1914 | if (pci_channel_offline(pdev)) |
| 1915 | return BLK_EH_RESET_TIMER; |
| 1916 | |
| 1917 | /* |
| 1918 | * Reset immediately if the controller is failed |
| 1919 | */ |
| 1920 | if (nvme_should_reset(dev, csts)) { |
| 1921 | nvme_warn_reset(dev, csts); |
| 1922 | goto disable; |
| 1923 | } |
| 1924 | |
| 1925 | /* |
| 1926 | * Did we miss an interrupt? |
| 1927 | */ |
| 1928 | if (test_bit(NVMEQ_POLLED, &nvmeq->flags)) |
| 1929 | nvme_poll(req->mq_hctx, NULL); |
| 1930 | else |
| 1931 | nvme_poll_irqdisable(nvmeq); |
| 1932 | |
| 1933 | if (blk_mq_rq_state(req) != MQ_RQ_IN_FLIGHT) { |
| 1934 | dev_warn(dev->ctrl.device, |
| 1935 | "I/O tag %d (%04x) QID %d timeout, completion polled\n", |
| 1936 | req->tag, nvme_cid(req), nvmeq->qid); |
| 1937 | return BLK_EH_DONE; |
| 1938 | } |
| 1939 | |
| 1940 | /* |
| 1941 | * Shutdown immediately if controller times out while starting. The |
| 1942 | * reset work will see the pci device disabled when it gets the forced |
| 1943 | * cancellation error. All outstanding requests are completed on |
| 1944 | * shutdown, so we return BLK_EH_DONE. |
| 1945 | */ |
| 1946 | switch (nvme_ctrl_state(&dev->ctrl)) { |
| 1947 | case NVME_CTRL_CONNECTING: |
| 1948 | nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING); |
| 1949 | fallthrough; |
| 1950 | case NVME_CTRL_DELETING: |
| 1951 | dev_warn_ratelimited(dev->ctrl.device, |
| 1952 | "I/O tag %d (%04x) QID %d timeout, disable controller\n", |
| 1953 | req->tag, nvme_cid(req), nvmeq->qid); |
| 1954 | nvme_req(req)->flags |= NVME_REQ_CANCELLED; |
| 1955 | nvme_dev_disable(dev, true); |
| 1956 | return BLK_EH_DONE; |
| 1957 | case NVME_CTRL_RESETTING: |
| 1958 | return BLK_EH_RESET_TIMER; |
| 1959 | default: |
| 1960 | break; |
| 1961 | } |
| 1962 | |
| 1963 | /* |
| 1964 | * Shutdown the controller immediately and schedule a reset if the |
| 1965 | * command was already aborted once before and still hasn't been |
| 1966 | * returned to the driver, or if this is the admin queue. |
| 1967 | */ |
| 1968 | opcode = nvme_req(req)->cmd->common.opcode; |
| 1969 | if (!nvmeq->qid || (iod->flags & IOD_ABORTED)) { |
| 1970 | dev_warn(dev->ctrl.device, |
| 1971 | "I/O tag %d (%04x) opcode %#x (%s) QID %d timeout, reset controller\n", |
| 1972 | req->tag, nvme_cid(req), opcode, |
| 1973 | nvme_opcode_str(nvmeq->qid, opcode), nvmeq->qid); |
| 1974 | nvme_req(req)->flags |= NVME_REQ_CANCELLED; |
| 1975 | goto disable; |
| 1976 | } |
| 1977 | |
| 1978 | if (atomic_dec_return(&dev->ctrl.abort_limit) < 0) { |
| 1979 | atomic_inc(&dev->ctrl.abort_limit); |
| 1980 | return BLK_EH_RESET_TIMER; |
| 1981 | } |
| 1982 | iod->flags |= IOD_ABORTED; |
| 1983 | |
| 1984 | cmd.abort.opcode = nvme_admin_abort_cmd; |
| 1985 | cmd.abort.cid = nvme_cid(req); |
| 1986 | cmd.abort.sqid = cpu_to_le16(nvmeq->qid); |
| 1987 | |
| 1988 | dev_warn(nvmeq->dev->ctrl.device, |
| 1989 | "I/O tag %d (%04x) opcode %#x (%s) QID %d timeout, aborting req_op:%s(%u) size:%u\n", |
| 1990 | req->tag, nvme_cid(req), opcode, nvme_get_opcode_str(opcode), |
| 1991 | nvmeq->qid, blk_op_str(req_op(req)), req_op(req), |
| 1992 | blk_rq_bytes(req)); |
| 1993 | |
| 1994 | abort_req = blk_mq_alloc_request(dev->ctrl.admin_q, nvme_req_op(&cmd), |
| 1995 | BLK_MQ_REQ_NOWAIT); |
| 1996 | if (IS_ERR(abort_req)) { |
| 1997 | atomic_inc(&dev->ctrl.abort_limit); |
| 1998 | return BLK_EH_RESET_TIMER; |
| 1999 | } |
| 2000 | nvme_init_request(abort_req, &cmd); |
| 2001 | |
| 2002 | abort_req->end_io = abort_endio; |
| 2003 | abort_req->end_io_data = NULL; |
| 2004 | blk_execute_rq_nowait(abort_req, false); |
| 2005 | |
| 2006 | /* |
| 2007 | * The aborted req will be completed on receiving the abort req. |
| 2008 | * We enable the timer again. If hit twice, it'll cause a device reset, |
| 2009 | * as the device then is in a faulty state. |
| 2010 | */ |
| 2011 | return BLK_EH_RESET_TIMER; |
| 2012 | |
| 2013 | disable: |
| 2014 | if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_RESETTING)) { |
| 2015 | if (nvme_state_terminal(&dev->ctrl)) |
| 2016 | nvme_dev_disable(dev, true); |
| 2017 | return BLK_EH_DONE; |
| 2018 | } |
| 2019 | |
| 2020 | nvme_dev_disable(dev, false); |
| 2021 | if (nvme_try_sched_reset(&dev->ctrl)) |
| 2022 | nvme_unquiesce_io_queues(&dev->ctrl); |
| 2023 | return BLK_EH_DONE; |
| 2024 | } |
| 2025 | |
| 2026 | static void nvme_free_queue(struct nvme_queue *nvmeq) |
| 2027 | __context_unsafe(/* frees queue which is no longer in use */) |
| 2028 | { |
| 2029 | dma_free_coherent(nvmeq->dev->dev, CQ_SIZE(nvmeq), |
| 2030 | (void *)nvmeq->cqes, nvmeq->cq_dma_addr); |
| 2031 | if (!nvmeq->sq_cmds) |
| 2032 | return; |
| 2033 | |
| 2034 | if (test_and_clear_bit(NVMEQ_SQ_CMB, &nvmeq->flags)) { |
| 2035 | pci_free_p2pmem(to_pci_dev(nvmeq->dev->dev), |
| 2036 | nvmeq->sq_cmds, SQ_SIZE(nvmeq)); |
| 2037 | } else { |
| 2038 | dma_free_coherent(nvmeq->dev->dev, SQ_SIZE(nvmeq), |
| 2039 | nvmeq->sq_cmds, nvmeq->sq_dma_addr); |
| 2040 | } |
| 2041 | } |
| 2042 | |
| 2043 | static void nvme_free_queues(struct nvme_dev *dev, int lowest) |
| 2044 | { |
| 2045 | int i; |
| 2046 | |
| 2047 | for (i = dev->ctrl.queue_count - 1; i >= lowest; i--) { |
| 2048 | dev->ctrl.queue_count--; |
| 2049 | nvme_free_queue(&dev->queues[i]); |
| 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | static void nvme_suspend_queue(struct nvme_dev *dev, unsigned int qid) |
| 2054 | { |
| 2055 | struct nvme_queue *nvmeq = &dev->queues[qid]; |
| 2056 | |
| 2057 | if (!test_and_clear_bit(NVMEQ_ENABLED, &nvmeq->flags)) |
| 2058 | return; |
| 2059 | |
| 2060 | /* ensure that nvme_queue_rq() sees NVMEQ_ENABLED cleared */ |
| 2061 | mb(); |
| 2062 | |
| 2063 | nvmeq->dev->online_queues--; |
| 2064 | if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q) |
| 2065 | nvme_quiesce_admin_queue(&nvmeq->dev->ctrl); |
| 2066 | if (!test_and_clear_bit(NVMEQ_POLLED, &nvmeq->flags)) |
| 2067 | pci_free_irq(to_pci_dev(dev->dev), nvmeq->cq_vector, nvmeq); |
| 2068 | } |
| 2069 | |
| 2070 | static void nvme_suspend_io_queues(struct nvme_dev *dev) |
| 2071 | { |
| 2072 | int i; |
| 2073 | |
| 2074 | for (i = dev->ctrl.queue_count - 1; i > 0; i--) |
| 2075 | nvme_suspend_queue(dev, i); |
| 2076 | } |
| 2077 | |
| 2078 | /* |
| 2079 | * Called only on a device that has been disabled and after all other threads |
| 2080 | * that can check this device's completion queues have synced, except |
| 2081 | * nvme_poll(). This is the last chance for the driver to see a natural |
| 2082 | * completion before nvme_cancel_request() terminates all incomplete requests. |
| 2083 | */ |
| 2084 | static void nvme_reap_pending_cqes(struct nvme_dev *dev) |
| 2085 | { |
| 2086 | int i; |
| 2087 | |
| 2088 | for (i = dev->ctrl.queue_count - 1; i > 0; i--) { |
| 2089 | spin_lock(&dev->queues[i].cq_poll_lock); |
| 2090 | nvme_poll_cq(&dev->queues[i], NULL); |
| 2091 | spin_unlock(&dev->queues[i].cq_poll_lock); |
| 2092 | } |
| 2093 | } |
| 2094 | |
| 2095 | static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues, |
| 2096 | int entry_size) |
| 2097 | { |
| 2098 | int q_depth = dev->q_depth; |
| 2099 | unsigned q_size_aligned = roundup(q_depth * entry_size, |
| 2100 | NVME_CTRL_PAGE_SIZE); |
| 2101 | |
| 2102 | if (q_size_aligned * nr_io_queues > dev->cmb_size) { |
| 2103 | u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues); |
| 2104 | |
| 2105 | mem_per_q = round_down(mem_per_q, NVME_CTRL_PAGE_SIZE); |
| 2106 | q_depth = div_u64(mem_per_q, entry_size); |
| 2107 | |
| 2108 | /* |
| 2109 | * Ensure the reduced q_depth is above some threshold where it |
| 2110 | * would be better to map queues in system memory with the |
| 2111 | * original depth |
| 2112 | */ |
| 2113 | if (q_depth < 64) |
| 2114 | return -ENOMEM; |
| 2115 | } |
| 2116 | |
| 2117 | return q_depth; |
| 2118 | } |
| 2119 | |
| 2120 | static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq, |
| 2121 | int qid) |
| 2122 | __context_unsafe(/* safe to allocate sq_cmds without any protection */) |
| 2123 | { |
| 2124 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
| 2125 | |
| 2126 | if (qid && dev->cmb_use_sqes && (dev->cmbsz & NVME_CMBSZ_SQS)) { |
| 2127 | nvmeq->sq_cmds = pci_alloc_p2pmem(pdev, SQ_SIZE(nvmeq)); |
| 2128 | if (nvmeq->sq_cmds) { |
| 2129 | nvmeq->sq_dma_addr = pci_p2pmem_virt_to_bus(pdev, |
| 2130 | nvmeq->sq_cmds); |
| 2131 | if (nvmeq->sq_dma_addr) { |
| 2132 | set_bit(NVMEQ_SQ_CMB, &nvmeq->flags); |
| 2133 | return 0; |
| 2134 | } |
| 2135 | |
| 2136 | pci_free_p2pmem(pdev, nvmeq->sq_cmds, SQ_SIZE(nvmeq)); |
| 2137 | } |
| 2138 | } |
| 2139 | |
| 2140 | nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(nvmeq), |
| 2141 | &nvmeq->sq_dma_addr, GFP_KERNEL); |
| 2142 | if (!nvmeq->sq_cmds) |
| 2143 | return -ENOMEM; |
| 2144 | return 0; |
| 2145 | } |
| 2146 | |
| 2147 | static int nvme_alloc_queue(struct nvme_dev *dev, int qid, int depth) |
| 2148 | { |
| 2149 | struct nvme_queue *nvmeq = &dev->queues[qid]; |
| 2150 | |
| 2151 | if (dev->ctrl.queue_count > qid) |
| 2152 | return 0; |
| 2153 | |
| 2154 | nvmeq->sqes = qid ? dev->io_sqes : NVME_ADM_SQES; |
| 2155 | nvmeq->q_depth = depth; |
| 2156 | nvmeq->cqes = dma_alloc_coherent(dev->dev, CQ_SIZE(nvmeq), |
| 2157 | &nvmeq->cq_dma_addr, GFP_KERNEL); |
| 2158 | if (!nvmeq->cqes) |
| 2159 | goto free_nvmeq; |
| 2160 | |
| 2161 | if (nvme_alloc_sq_cmds(dev, nvmeq, qid)) |
| 2162 | goto free_cqdma; |
| 2163 | |
| 2164 | nvmeq->dev = dev; |
| 2165 | spin_lock_init(&nvmeq->sq_lock); |
| 2166 | spin_lock_init(&nvmeq->cq_poll_lock); |
| 2167 | nvmeq->cq_head = 0; |
| 2168 | nvmeq->cq_phase = 1; |
| 2169 | nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride]; |
| 2170 | nvmeq->qid = qid; |
| 2171 | dev->ctrl.queue_count++; |
| 2172 | |
| 2173 | return 0; |
| 2174 | |
| 2175 | free_cqdma: |
| 2176 | dma_free_coherent(dev->dev, CQ_SIZE(nvmeq), (void *)nvmeq->cqes, |
| 2177 | nvmeq->cq_dma_addr); |
| 2178 | free_nvmeq: |
| 2179 | return -ENOMEM; |
| 2180 | } |
| 2181 | |
| 2182 | static int queue_request_irq(struct nvme_queue *nvmeq) |
| 2183 | { |
| 2184 | struct pci_dev *pdev = to_pci_dev(nvmeq->dev->dev); |
| 2185 | int nr = nvmeq->dev->ctrl.instance; |
| 2186 | |
| 2187 | if (use_threaded_interrupts) { |
| 2188 | return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq_check, |
| 2189 | nvme_irq, nvmeq, "nvme%dq%d", nr, nvmeq->qid); |
| 2190 | } else { |
| 2191 | return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq, |
| 2192 | NULL, nvmeq, "nvme%dq%d", nr, nvmeq->qid); |
| 2193 | } |
| 2194 | } |
| 2195 | |
| 2196 | static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid) |
| 2197 | __context_unsafe(/* initialize unpublished/lock-guarded variables */) |
| 2198 | { |
| 2199 | struct nvme_dev *dev = nvmeq->dev; |
| 2200 | |
| 2201 | nvmeq->sq_tail = 0; |
| 2202 | nvmeq->last_sq_tail = 0; |
| 2203 | nvmeq->cq_head = 0; |
| 2204 | nvmeq->cq_phase = 1; |
| 2205 | nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride]; |
| 2206 | memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq)); |
| 2207 | nvme_dbbuf_init(dev, nvmeq, qid); |
| 2208 | dev->online_queues++; |
| 2209 | wmb(); /* ensure the first interrupt sees the initialization */ |
| 2210 | } |
| 2211 | |
| 2212 | /* |
| 2213 | * Try getting shutdown_lock while setting up IO queues. |
| 2214 | */ |
| 2215 | static int nvme_setup_io_queues_trylock(struct nvme_dev *dev) |
| 2216 | __cond_acquires(0, &dev->shutdown_lock) |
| 2217 | { |
| 2218 | /* |
| 2219 | * Give up if the lock is being held by nvme_dev_disable. |
| 2220 | */ |
| 2221 | if (!mutex_trylock(&dev->shutdown_lock)) |
| 2222 | return -ENODEV; |
| 2223 | |
| 2224 | /* |
| 2225 | * Controller is in wrong state, fail early. |
| 2226 | */ |
| 2227 | if (nvme_ctrl_state(&dev->ctrl) != NVME_CTRL_CONNECTING) { |
| 2228 | mutex_unlock(&dev->shutdown_lock); |
| 2229 | return -ENODEV; |
| 2230 | } |
| 2231 | |
| 2232 | return 0; |
| 2233 | } |
| 2234 | |
| 2235 | static int nvme_create_queue(struct nvme_queue *nvmeq, int qid, bool polled) |
| 2236 | { |
| 2237 | struct nvme_dev *dev = nvmeq->dev; |
| 2238 | int result; |
| 2239 | u16 vector = 0; |
| 2240 | |
| 2241 | clear_bit(NVMEQ_DELETE_ERROR, &nvmeq->flags); |
| 2242 | |
| 2243 | /* |
| 2244 | * A queue's vector matches the queue identifier unless the controller |
| 2245 | * has only one vector available. |
| 2246 | */ |
| 2247 | if (!polled) |
| 2248 | vector = dev->num_vecs == 1 ? 0 : qid; |
| 2249 | else |
| 2250 | set_bit(NVMEQ_POLLED, &nvmeq->flags); |
| 2251 | |
| 2252 | result = adapter_alloc_cq(dev, qid, nvmeq, vector); |
| 2253 | if (result) |
| 2254 | return result; |
| 2255 | |
| 2256 | result = adapter_alloc_sq(dev, qid, nvmeq); |
| 2257 | if (result < 0) |
| 2258 | return result; |
| 2259 | if (result) |
| 2260 | goto release_cq; |
| 2261 | |
| 2262 | nvmeq->cq_vector = vector; |
| 2263 | |
| 2264 | result = nvme_setup_io_queues_trylock(dev); |
| 2265 | if (result) |
| 2266 | return result; |
| 2267 | nvme_init_queue(nvmeq, qid); |
| 2268 | if (!polled) { |
| 2269 | result = queue_request_irq(nvmeq); |
| 2270 | if (result < 0) |
| 2271 | goto release_sq; |
| 2272 | } |
| 2273 | |
| 2274 | set_bit(NVMEQ_ENABLED, &nvmeq->flags); |
| 2275 | mutex_unlock(&dev->shutdown_lock); |
| 2276 | return result; |
| 2277 | |
| 2278 | release_sq: |
| 2279 | dev->online_queues--; |
| 2280 | mutex_unlock(&dev->shutdown_lock); |
| 2281 | adapter_delete_sq(dev, qid); |
| 2282 | release_cq: |
| 2283 | adapter_delete_cq(dev, qid); |
| 2284 | return result; |
| 2285 | } |
| 2286 | |
| 2287 | static const struct blk_mq_ops nvme_mq_admin_ops = { |
| 2288 | .queue_rq = nvme_queue_rq, |
| 2289 | .complete = nvme_pci_complete_rq, |
| 2290 | .commit_rqs = nvme_commit_rqs, |
| 2291 | .init_hctx = nvme_admin_init_hctx, |
| 2292 | .init_request = nvme_pci_init_request, |
| 2293 | .timeout = nvme_timeout, |
| 2294 | }; |
| 2295 | |
| 2296 | static const struct blk_mq_ops nvme_mq_ops = { |
| 2297 | .queue_rq = nvme_queue_rq, |
| 2298 | .queue_rqs = nvme_queue_rqs, |
| 2299 | .complete = nvme_pci_complete_rq, |
| 2300 | .commit_rqs = nvme_commit_rqs, |
| 2301 | .init_hctx = nvme_init_hctx, |
| 2302 | .init_request = nvme_pci_init_request, |
| 2303 | .map_queues = nvme_pci_map_queues, |
| 2304 | .timeout = nvme_timeout, |
| 2305 | .poll = nvme_poll, |
| 2306 | }; |
| 2307 | |
| 2308 | static void nvme_dev_remove_admin(struct nvme_dev *dev) |
| 2309 | { |
| 2310 | if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) { |
| 2311 | /* |
| 2312 | * If the controller was reset during removal, it's possible |
| 2313 | * user requests may be waiting on a stopped queue. Start the |
| 2314 | * queue to flush these to completion. |
| 2315 | */ |
| 2316 | nvme_unquiesce_admin_queue(&dev->ctrl); |
| 2317 | nvme_remove_admin_tag_set(&dev->ctrl); |
| 2318 | } |
| 2319 | } |
| 2320 | |
| 2321 | static unsigned long db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues) |
| 2322 | { |
| 2323 | return NVME_REG_DBS + ((nr_io_queues + 1) * 8 * dev->db_stride); |
| 2324 | } |
| 2325 | |
| 2326 | static int nvme_remap_bar(struct nvme_dev *dev, unsigned long size) |
| 2327 | { |
| 2328 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
| 2329 | |
| 2330 | if (size <= dev->bar_mapped_size) |
| 2331 | return 0; |
| 2332 | if (size > pci_resource_len(pdev, 0)) |
| 2333 | return -ENOMEM; |
| 2334 | if (dev->bar) |
| 2335 | iounmap(dev->bar); |
| 2336 | dev->bar = ioremap(pci_resource_start(pdev, 0), size); |
| 2337 | if (!dev->bar) { |
| 2338 | dev->bar_mapped_size = 0; |
| 2339 | return -ENOMEM; |
| 2340 | } |
| 2341 | dev->bar_mapped_size = size; |
| 2342 | dev->dbs = dev->bar + NVME_REG_DBS; |
| 2343 | |
| 2344 | return 0; |
| 2345 | } |
| 2346 | |
| 2347 | static int nvme_pci_configure_admin_queue(struct nvme_dev *dev) |
| 2348 | { |
| 2349 | int result; |
| 2350 | u32 aqa; |
| 2351 | struct nvme_queue *nvmeq; |
| 2352 | |
| 2353 | result = nvme_remap_bar(dev, db_bar_size(dev, 0)); |
| 2354 | if (result < 0) |
| 2355 | return result; |
| 2356 | |
| 2357 | dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1, 0) ? |
| 2358 | NVME_CAP_NSSRC(dev->ctrl.cap) : 0; |
| 2359 | |
| 2360 | if (dev->subsystem && |
| 2361 | (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO)) |
| 2362 | writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS); |
| 2363 | |
| 2364 | /* |
| 2365 | * If the device has been passed off to us in an enabled state, just |
| 2366 | * clear the enabled bit. The spec says we should set the 'shutdown |
| 2367 | * notification bits', but doing so may cause the device to complete |
| 2368 | * commands to the admin queue ... and we don't know what memory that |
| 2369 | * might be pointing at! |
| 2370 | */ |
| 2371 | result = nvme_disable_ctrl(&dev->ctrl, false); |
| 2372 | if (result < 0) { |
| 2373 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
| 2374 | |
| 2375 | /* |
| 2376 | * The NVMe Controller Reset method did not get an expected |
| 2377 | * CSTS.RDY transition, so something with the device appears to |
| 2378 | * be stuck. Use the lower level and bigger hammer PCIe |
| 2379 | * Function Level Reset to attempt restoring the device to its |
| 2380 | * initial state, and try again. |
| 2381 | */ |
| 2382 | result = pcie_reset_flr(pdev, false); |
| 2383 | if (result < 0) |
| 2384 | return result; |
| 2385 | |
| 2386 | pci_restore_state(pdev); |
| 2387 | result = nvme_disable_ctrl(&dev->ctrl, false); |
| 2388 | if (result < 0) |
| 2389 | return result; |
| 2390 | |
| 2391 | dev_info(dev->ctrl.device, |
| 2392 | "controller reset completed after pcie flr\n"); |
| 2393 | } |
| 2394 | |
| 2395 | result = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH); |
| 2396 | if (result) |
| 2397 | return result; |
| 2398 | |
| 2399 | dev->ctrl.numa_node = dev_to_node(dev->dev); |
| 2400 | |
| 2401 | nvmeq = &dev->queues[0]; |
| 2402 | aqa = nvmeq->q_depth - 1; |
| 2403 | aqa |= aqa << 16; |
| 2404 | |
| 2405 | writel(aqa, dev->bar + NVME_REG_AQA); |
| 2406 | lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ); |
| 2407 | lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ); |
| 2408 | |
| 2409 | result = nvme_enable_ctrl(&dev->ctrl); |
| 2410 | if (result) |
| 2411 | return result; |
| 2412 | |
| 2413 | nvmeq->cq_vector = 0; |
| 2414 | nvme_init_queue(nvmeq, 0); |
| 2415 | result = queue_request_irq(nvmeq); |
| 2416 | if (result) { |
| 2417 | dev->online_queues--; |
| 2418 | nvme_disable_ctrl(&dev->ctrl, false); |
| 2419 | return result; |
| 2420 | } |
| 2421 | |
| 2422 | set_bit(NVMEQ_ENABLED, &nvmeq->flags); |
| 2423 | return result; |
| 2424 | } |
| 2425 | |
| 2426 | static int nvme_create_io_queues(struct nvme_dev *dev) |
| 2427 | { |
| 2428 | unsigned i, max, rw_queues; |
| 2429 | int ret = 0; |
| 2430 | |
| 2431 | for (i = dev->ctrl.queue_count; i <= dev->max_qid; i++) { |
| 2432 | if (nvme_alloc_queue(dev, i, dev->q_depth)) { |
| 2433 | ret = -ENOMEM; |
| 2434 | break; |
| 2435 | } |
| 2436 | } |
| 2437 | |
| 2438 | max = min(dev->max_qid, dev->ctrl.queue_count - 1); |
| 2439 | if (max != 1 && dev->io_queues[HCTX_TYPE_POLL]) { |
| 2440 | rw_queues = dev->io_queues[HCTX_TYPE_DEFAULT] + |
| 2441 | dev->io_queues[HCTX_TYPE_READ]; |
| 2442 | } else { |
| 2443 | rw_queues = max; |
| 2444 | } |
| 2445 | |
| 2446 | for (i = dev->online_queues; i <= max; i++) { |
| 2447 | bool polled = i > rw_queues; |
| 2448 | |
| 2449 | ret = nvme_create_queue(&dev->queues[i], i, polled); |
| 2450 | if (ret) |
| 2451 | break; |
| 2452 | } |
| 2453 | |
| 2454 | /* |
| 2455 | * Ignore failing Create SQ/CQ commands, we can continue with less |
| 2456 | * than the desired amount of queues, and even a controller without |
| 2457 | * I/O queues can still be used to issue admin commands. This might |
| 2458 | * be useful to upgrade a buggy firmware for example. |
| 2459 | */ |
| 2460 | return ret >= 0 ? 0 : ret; |
| 2461 | } |
| 2462 | |
| 2463 | static u64 nvme_cmb_size_unit(struct nvme_dev *dev) |
| 2464 | { |
| 2465 | u8 szu = (dev->cmbsz >> NVME_CMBSZ_SZU_SHIFT) & NVME_CMBSZ_SZU_MASK; |
| 2466 | |
| 2467 | return 1ULL << (12 + 4 * szu); |
| 2468 | } |
| 2469 | |
| 2470 | static u32 nvme_cmb_size(struct nvme_dev *dev) |
| 2471 | { |
| 2472 | return (dev->cmbsz >> NVME_CMBSZ_SZ_SHIFT) & NVME_CMBSZ_SZ_MASK; |
| 2473 | } |
| 2474 | |
| 2475 | static void nvme_map_cmb(struct nvme_dev *dev) |
| 2476 | { |
| 2477 | u64 size, offset; |
| 2478 | resource_size_t bar_size; |
| 2479 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
| 2480 | int bar; |
| 2481 | |
| 2482 | if (dev->cmb_size) |
| 2483 | return; |
| 2484 | |
| 2485 | if (NVME_CAP_CMBS(dev->ctrl.cap)) |
| 2486 | writel(NVME_CMBMSC_CRE, dev->bar + NVME_REG_CMBMSC); |
| 2487 | |
| 2488 | dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ); |
| 2489 | if (!dev->cmbsz) |
| 2490 | return; |
| 2491 | dev->cmbloc = readl(dev->bar + NVME_REG_CMBLOC); |
| 2492 | |
| 2493 | size = nvme_cmb_size_unit(dev) * nvme_cmb_size(dev); |
| 2494 | offset = nvme_cmb_size_unit(dev) * NVME_CMB_OFST(dev->cmbloc); |
| 2495 | bar = NVME_CMB_BIR(dev->cmbloc); |
| 2496 | bar_size = pci_resource_len(pdev, bar); |
| 2497 | |
| 2498 | if (offset > bar_size) |
| 2499 | return; |
| 2500 | |
| 2501 | /* |
| 2502 | * Controllers may support a CMB size larger than their BAR, for |
| 2503 | * example, due to being behind a bridge. Reduce the CMB to the |
| 2504 | * reported size of the BAR |
| 2505 | */ |
| 2506 | size = min(size, bar_size - offset); |
| 2507 | |
| 2508 | if (!IS_ALIGNED(size, memremap_compat_align()) || |
| 2509 | !IS_ALIGNED(pci_resource_start(pdev, bar), |
| 2510 | memremap_compat_align())) |
| 2511 | return; |
| 2512 | |
| 2513 | /* |
| 2514 | * Tell the controller about the host side address mapping the CMB, |
| 2515 | * and enable CMB decoding for the NVMe 1.4+ scheme: |
| 2516 | */ |
| 2517 | if (NVME_CAP_CMBS(dev->ctrl.cap)) { |
| 2518 | hi_lo_writeq(NVME_CMBMSC_CRE | NVME_CMBMSC_CMSE | |
| 2519 | (pci_bus_address(pdev, bar) + offset), |
| 2520 | dev->bar + NVME_REG_CMBMSC); |
| 2521 | } |
| 2522 | |
| 2523 | if (pci_p2pdma_add_resource(pdev, bar, size, offset)) { |
| 2524 | dev_warn(dev->ctrl.device, |
| 2525 | "failed to register the CMB\n"); |
| 2526 | hi_lo_writeq(0, dev->bar + NVME_REG_CMBMSC); |
| 2527 | return; |
| 2528 | } |
| 2529 | |
| 2530 | dev->cmb_size = size; |
| 2531 | dev->cmb_use_sqes = use_cmb_sqes && (dev->cmbsz & NVME_CMBSZ_SQS); |
| 2532 | |
| 2533 | if ((dev->cmbsz & (NVME_CMBSZ_WDS | NVME_CMBSZ_RDS)) == |
| 2534 | (NVME_CMBSZ_WDS | NVME_CMBSZ_RDS)) |
| 2535 | pci_p2pmem_publish(pdev, true); |
| 2536 | } |
| 2537 | |
| 2538 | static int nvme_set_host_mem(struct nvme_dev *dev, u32 bits) |
| 2539 | { |
| 2540 | u32 host_mem_size = dev->host_mem_size >> NVME_CTRL_PAGE_SHIFT; |
| 2541 | u64 dma_addr = dev->host_mem_descs_dma; |
| 2542 | struct nvme_command c = { }; |
| 2543 | int ret; |
| 2544 | |
| 2545 | c.features.opcode = nvme_admin_set_features; |
| 2546 | c.features.fid = cpu_to_le32(NVME_FEAT_HOST_MEM_BUF); |
| 2547 | c.features.dword11 = cpu_to_le32(bits); |
| 2548 | c.features.dword12 = cpu_to_le32(host_mem_size); |
| 2549 | c.features.dword13 = cpu_to_le32(lower_32_bits(dma_addr)); |
| 2550 | c.features.dword14 = cpu_to_le32(upper_32_bits(dma_addr)); |
| 2551 | c.features.dword15 = cpu_to_le32(dev->nr_host_mem_descs); |
| 2552 | |
| 2553 | ret = nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0); |
| 2554 | if (ret) { |
| 2555 | dev_warn(dev->ctrl.device, |
| 2556 | "failed to set host mem (err %d, flags %#x).\n", |
| 2557 | ret, bits); |
| 2558 | } else |
| 2559 | dev->hmb = bits & NVME_HOST_MEM_ENABLE; |
| 2560 | |
| 2561 | return ret; |
| 2562 | } |
| 2563 | |
| 2564 | static void nvme_free_host_mem_multi(struct nvme_dev *dev) |
| 2565 | { |
| 2566 | int i; |
| 2567 | |
| 2568 | for (i = 0; i < dev->nr_host_mem_descs; i++) { |
| 2569 | struct nvme_host_mem_buf_desc *desc = &dev->host_mem_descs[i]; |
| 2570 | size_t size = le32_to_cpu(desc->size) * NVME_CTRL_PAGE_SIZE; |
| 2571 | |
| 2572 | dma_free_attrs(dev->dev, size, dev->host_mem_desc_bufs[i], |
| 2573 | le64_to_cpu(desc->addr), |
| 2574 | DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN); |
| 2575 | } |
| 2576 | |
| 2577 | kfree(dev->host_mem_desc_bufs); |
| 2578 | dev->host_mem_desc_bufs = NULL; |
| 2579 | } |
| 2580 | |
| 2581 | static void nvme_free_host_mem(struct nvme_dev *dev) |
| 2582 | { |
| 2583 | if (dev->hmb_sgt) { |
| 2584 | dma_free_noncontiguous(dev->dev, dev->host_mem_size, |
| 2585 | dev->hmb_sgt, DMA_BIDIRECTIONAL); |
| 2586 | dev->hmb_sgt = NULL; |
| 2587 | } else { |
| 2588 | nvme_free_host_mem_multi(dev); |
| 2589 | } |
| 2590 | |
| 2591 | dma_free_coherent(dev->dev, dev->host_mem_descs_size, |
| 2592 | dev->host_mem_descs, dev->host_mem_descs_dma); |
| 2593 | dev->host_mem_descs = NULL; |
| 2594 | dev->host_mem_descs_size = 0; |
| 2595 | dev->nr_host_mem_descs = 0; |
| 2596 | } |
| 2597 | |
| 2598 | static int nvme_alloc_host_mem_single(struct nvme_dev *dev, u64 size) |
| 2599 | { |
| 2600 | dev->hmb_sgt = dma_alloc_noncontiguous(dev->dev, size, |
| 2601 | DMA_BIDIRECTIONAL, GFP_KERNEL, 0); |
| 2602 | if (!dev->hmb_sgt) |
| 2603 | return -ENOMEM; |
| 2604 | |
| 2605 | dev->host_mem_descs = dma_alloc_coherent(dev->dev, |
| 2606 | sizeof(*dev->host_mem_descs), &dev->host_mem_descs_dma, |
| 2607 | GFP_KERNEL); |
| 2608 | if (!dev->host_mem_descs) { |
| 2609 | dma_free_noncontiguous(dev->dev, size, dev->hmb_sgt, |
| 2610 | DMA_BIDIRECTIONAL); |
| 2611 | dev->hmb_sgt = NULL; |
| 2612 | return -ENOMEM; |
| 2613 | } |
| 2614 | dev->host_mem_size = size; |
| 2615 | dev->host_mem_descs_size = sizeof(*dev->host_mem_descs); |
| 2616 | dev->nr_host_mem_descs = 1; |
| 2617 | |
| 2618 | dev->host_mem_descs[0].addr = |
| 2619 | cpu_to_le64(dev->hmb_sgt->sgl->dma_address); |
| 2620 | dev->host_mem_descs[0].size = cpu_to_le32(size / NVME_CTRL_PAGE_SIZE); |
| 2621 | return 0; |
| 2622 | } |
| 2623 | |
| 2624 | static int nvme_alloc_host_mem_multi(struct nvme_dev *dev, u64 preferred, |
| 2625 | u32 chunk_size) |
| 2626 | { |
| 2627 | struct nvme_host_mem_buf_desc *descs; |
| 2628 | u32 max_entries, len, descs_size; |
| 2629 | dma_addr_t descs_dma; |
| 2630 | int i = 0; |
| 2631 | void **bufs; |
| 2632 | u64 size, tmp; |
| 2633 | |
| 2634 | tmp = (preferred + chunk_size - 1); |
| 2635 | do_div(tmp, chunk_size); |
| 2636 | max_entries = tmp; |
| 2637 | |
| 2638 | if (dev->ctrl.hmmaxd && dev->ctrl.hmmaxd < max_entries) |
| 2639 | max_entries = dev->ctrl.hmmaxd; |
| 2640 | |
| 2641 | descs_size = max_entries * sizeof(*descs); |
| 2642 | descs = dma_alloc_coherent(dev->dev, descs_size, &descs_dma, |
| 2643 | GFP_KERNEL); |
| 2644 | if (!descs) |
| 2645 | goto out; |
| 2646 | |
| 2647 | bufs = kzalloc_objs(*bufs, max_entries); |
| 2648 | if (!bufs) |
| 2649 | goto out_free_descs; |
| 2650 | |
| 2651 | for (size = 0; size < preferred && i < max_entries; size += len) { |
| 2652 | dma_addr_t dma_addr; |
| 2653 | |
| 2654 | len = min_t(u64, chunk_size, preferred - size); |
| 2655 | bufs[i] = dma_alloc_attrs(dev->dev, len, &dma_addr, GFP_KERNEL, |
| 2656 | DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN); |
| 2657 | if (!bufs[i]) |
| 2658 | break; |
| 2659 | |
| 2660 | descs[i].addr = cpu_to_le64(dma_addr); |
| 2661 | descs[i].size = cpu_to_le32(len / NVME_CTRL_PAGE_SIZE); |
| 2662 | i++; |
| 2663 | } |
| 2664 | |
| 2665 | if (!size) |
| 2666 | goto out_free_bufs; |
| 2667 | |
| 2668 | dev->nr_host_mem_descs = i; |
| 2669 | dev->host_mem_size = size; |
| 2670 | dev->host_mem_descs = descs; |
| 2671 | dev->host_mem_descs_dma = descs_dma; |
| 2672 | dev->host_mem_descs_size = descs_size; |
| 2673 | dev->host_mem_desc_bufs = bufs; |
| 2674 | return 0; |
| 2675 | |
| 2676 | out_free_bufs: |
| 2677 | kfree(bufs); |
| 2678 | out_free_descs: |
| 2679 | dma_free_coherent(dev->dev, descs_size, descs, descs_dma); |
| 2680 | out: |
| 2681 | dev->host_mem_descs = NULL; |
| 2682 | return -ENOMEM; |
| 2683 | } |
| 2684 | |
| 2685 | static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred) |
| 2686 | { |
| 2687 | unsigned long dma_merge_boundary = dma_get_merge_boundary(dev->dev); |
| 2688 | u64 min_chunk = min_t(u64, preferred, PAGE_SIZE * MAX_ORDER_NR_PAGES); |
| 2689 | u64 hmminds = max_t(u32, dev->ctrl.hmminds * 4096, PAGE_SIZE * 2); |
| 2690 | u64 chunk_size; |
| 2691 | |
| 2692 | /* |
| 2693 | * If there is an IOMMU that can merge pages, try a virtually |
| 2694 | * non-contiguous allocation for a single segment first. |
| 2695 | */ |
| 2696 | if (dma_merge_boundary && (PAGE_SIZE & dma_merge_boundary) == 0) { |
| 2697 | if (!nvme_alloc_host_mem_single(dev, preferred)) |
| 2698 | return 0; |
| 2699 | } |
| 2700 | |
| 2701 | /* start big and work our way down */ |
| 2702 | for (chunk_size = min_chunk; chunk_size >= hmminds; chunk_size /= 2) { |
| 2703 | if (!nvme_alloc_host_mem_multi(dev, preferred, chunk_size)) { |
| 2704 | if (!min || dev->host_mem_size >= min) |
| 2705 | return 0; |
| 2706 | nvme_free_host_mem(dev); |
| 2707 | } |
| 2708 | } |
| 2709 | |
| 2710 | return -ENOMEM; |
| 2711 | } |
| 2712 | |
| 2713 | static int nvme_setup_host_mem(struct nvme_dev *dev) |
| 2714 | { |
| 2715 | u64 max = (u64)max_host_mem_size_mb * SZ_1M; |
| 2716 | u64 preferred = (u64)dev->ctrl.hmpre * 4096; |
| 2717 | u64 min = (u64)dev->ctrl.hmmin * 4096; |
| 2718 | u32 enable_bits = NVME_HOST_MEM_ENABLE; |
| 2719 | int ret; |
| 2720 | |
| 2721 | if (!dev->ctrl.hmpre) |
| 2722 | return 0; |
| 2723 | |
| 2724 | preferred = min(preferred, max); |
| 2725 | if (min > max) { |
| 2726 | dev_warn(dev->ctrl.device, |
| 2727 | "min host memory (%lld MiB) above limit (%d MiB).\n", |
| 2728 | min >> ilog2(SZ_1M), max_host_mem_size_mb); |
| 2729 | nvme_free_host_mem(dev); |
| 2730 | return 0; |
| 2731 | } |
| 2732 | |
| 2733 | /* |
| 2734 | * If we already have a buffer allocated check if we can reuse it. |
| 2735 | */ |
| 2736 | if (dev->host_mem_descs) { |
| 2737 | if (dev->host_mem_size >= min) |
| 2738 | enable_bits |= NVME_HOST_MEM_RETURN; |
| 2739 | else |
| 2740 | nvme_free_host_mem(dev); |
| 2741 | } |
| 2742 | |
| 2743 | if (!dev->host_mem_descs) { |
| 2744 | if (nvme_alloc_host_mem(dev, min, preferred)) { |
| 2745 | dev_warn(dev->ctrl.device, |
| 2746 | "failed to allocate host memory buffer.\n"); |
| 2747 | return 0; /* controller must work without HMB */ |
| 2748 | } |
| 2749 | |
| 2750 | dev_info(dev->ctrl.device, |
| 2751 | "allocated %lld MiB host memory buffer (%u segment%s).\n", |
| 2752 | dev->host_mem_size >> ilog2(SZ_1M), |
| 2753 | dev->nr_host_mem_descs, |
| 2754 | str_plural(dev->nr_host_mem_descs)); |
| 2755 | } |
| 2756 | |
| 2757 | ret = nvme_set_host_mem(dev, enable_bits); |
| 2758 | if (ret) |
| 2759 | nvme_free_host_mem(dev); |
| 2760 | return ret; |
| 2761 | } |
| 2762 | |
| 2763 | static ssize_t cmb_show(struct device *dev, struct device_attribute *attr, |
| 2764 | char *buf) |
| 2765 | { |
| 2766 | struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev)); |
| 2767 | |
| 2768 | return sysfs_emit(buf, "cmbloc : 0x%08x\ncmbsz : 0x%08x\n", |
| 2769 | ndev->cmbloc, ndev->cmbsz); |
| 2770 | } |
| 2771 | static DEVICE_ATTR_RO(cmb); |
| 2772 | |
| 2773 | static ssize_t cmbloc_show(struct device *dev, struct device_attribute *attr, |
| 2774 | char *buf) |
| 2775 | { |
| 2776 | struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev)); |
| 2777 | |
| 2778 | return sysfs_emit(buf, "%u\n", ndev->cmbloc); |
| 2779 | } |
| 2780 | static DEVICE_ATTR_RO(cmbloc); |
| 2781 | |
| 2782 | static ssize_t cmbsz_show(struct device *dev, struct device_attribute *attr, |
| 2783 | char *buf) |
| 2784 | { |
| 2785 | struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev)); |
| 2786 | |
| 2787 | return sysfs_emit(buf, "%u\n", ndev->cmbsz); |
| 2788 | } |
| 2789 | static DEVICE_ATTR_RO(cmbsz); |
| 2790 | |
| 2791 | static ssize_t hmb_show(struct device *dev, struct device_attribute *attr, |
| 2792 | char *buf) |
| 2793 | { |
| 2794 | struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev)); |
| 2795 | |
| 2796 | return sysfs_emit(buf, "%d\n", ndev->hmb); |
| 2797 | } |
| 2798 | |
| 2799 | static ssize_t hmb_store(struct device *dev, struct device_attribute *attr, |
| 2800 | const char *buf, size_t count) |
| 2801 | { |
| 2802 | struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev)); |
| 2803 | bool new; |
| 2804 | int ret; |
| 2805 | |
| 2806 | if (kstrtobool(buf, &new) < 0) |
| 2807 | return -EINVAL; |
| 2808 | |
| 2809 | if (new == ndev->hmb) |
| 2810 | return count; |
| 2811 | |
| 2812 | if (new) { |
| 2813 | ret = nvme_setup_host_mem(ndev); |
| 2814 | } else { |
| 2815 | ret = nvme_set_host_mem(ndev, 0); |
| 2816 | if (!ret) |
| 2817 | nvme_free_host_mem(ndev); |
| 2818 | } |
| 2819 | |
| 2820 | if (ret < 0) |
| 2821 | return ret; |
| 2822 | |
| 2823 | return count; |
| 2824 | } |
| 2825 | static DEVICE_ATTR_RW(hmb); |
| 2826 | |
| 2827 | static umode_t nvme_pci_attrs_are_visible(struct kobject *kobj, |
| 2828 | struct attribute *a, int n) |
| 2829 | { |
| 2830 | struct nvme_ctrl *ctrl = |
| 2831 | dev_get_drvdata(container_of(kobj, struct device, kobj)); |
| 2832 | struct nvme_dev *dev = to_nvme_dev(ctrl); |
| 2833 | |
| 2834 | if (a == &dev_attr_cmb.attr || |
| 2835 | a == &dev_attr_cmbloc.attr || |
| 2836 | a == &dev_attr_cmbsz.attr) { |
| 2837 | if (!dev->cmbsz) |
| 2838 | return 0; |
| 2839 | } |
| 2840 | if (a == &dev_attr_hmb.attr && !ctrl->hmpre) |
| 2841 | return 0; |
| 2842 | |
| 2843 | return a->mode; |
| 2844 | } |
| 2845 | |
| 2846 | static struct attribute *nvme_pci_attrs[] = { |
| 2847 | &dev_attr_cmb.attr, |
| 2848 | &dev_attr_cmbloc.attr, |
| 2849 | &dev_attr_cmbsz.attr, |
| 2850 | &dev_attr_hmb.attr, |
| 2851 | NULL, |
| 2852 | }; |
| 2853 | |
| 2854 | static const struct attribute_group nvme_pci_dev_attrs_group = { |
| 2855 | .attrs = nvme_pci_attrs, |
| 2856 | .is_visible = nvme_pci_attrs_are_visible, |
| 2857 | }; |
| 2858 | |
| 2859 | static const struct attribute_group *nvme_pci_dev_attr_groups[] = { |
| 2860 | &nvme_dev_attrs_group, |
| 2861 | &nvme_pci_dev_attrs_group, |
| 2862 | &nvme_dev_diag_attrs_group, |
| 2863 | NULL, |
| 2864 | }; |
| 2865 | |
| 2866 | static void nvme_update_attrs(struct nvme_dev *dev) |
| 2867 | { |
| 2868 | sysfs_update_group(&dev->ctrl.device->kobj, &nvme_pci_dev_attrs_group); |
| 2869 | } |
| 2870 | |
| 2871 | /* |
| 2872 | * nirqs is the number of interrupts available for write and read |
| 2873 | * queues. The core already reserved an interrupt for the admin queue. |
| 2874 | */ |
| 2875 | static void nvme_calc_irq_sets(struct irq_affinity *affd, unsigned int nrirqs) |
| 2876 | { |
| 2877 | struct nvme_dev *dev = affd->priv; |
| 2878 | unsigned int nr_read_queues, nr_write_queues = dev->nr_write_queues; |
| 2879 | |
| 2880 | /* |
| 2881 | * If there is no interrupt available for queues, ensure that |
| 2882 | * the default queue is set to 1. The affinity set size is |
| 2883 | * also set to one, but the irq core ignores it for this case. |
| 2884 | * |
| 2885 | * If only one interrupt is available or 'write_queue' == 0, combine |
| 2886 | * write and read queues. |
| 2887 | * |
| 2888 | * If 'write_queues' > 0, ensure it leaves room for at least one read |
| 2889 | * queue. |
| 2890 | */ |
| 2891 | if (!nrirqs) { |
| 2892 | nrirqs = 1; |
| 2893 | nr_read_queues = 0; |
| 2894 | } else if (nrirqs == 1 || !nr_write_queues) { |
| 2895 | nr_read_queues = 0; |
| 2896 | } else if (nr_write_queues >= nrirqs) { |
| 2897 | nr_read_queues = 1; |
| 2898 | } else { |
| 2899 | nr_read_queues = nrirqs - nr_write_queues; |
| 2900 | } |
| 2901 | |
| 2902 | dev->io_queues[HCTX_TYPE_DEFAULT] = nrirqs - nr_read_queues; |
| 2903 | affd->set_size[HCTX_TYPE_DEFAULT] = nrirqs - nr_read_queues; |
| 2904 | dev->io_queues[HCTX_TYPE_READ] = nr_read_queues; |
| 2905 | affd->set_size[HCTX_TYPE_READ] = nr_read_queues; |
| 2906 | affd->nr_sets = nr_read_queues ? 2 : 1; |
| 2907 | } |
| 2908 | |
| 2909 | static int nvme_setup_irqs(struct nvme_dev *dev, unsigned int nr_io_queues) |
| 2910 | { |
| 2911 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
| 2912 | struct irq_affinity affd = { |
| 2913 | .pre_vectors = 1, |
| 2914 | .calc_sets = nvme_calc_irq_sets, |
| 2915 | .priv = dev, |
| 2916 | }; |
| 2917 | unsigned int irq_queues, poll_queues; |
| 2918 | unsigned int flags = PCI_IRQ_ALL_TYPES | PCI_IRQ_AFFINITY; |
| 2919 | |
| 2920 | /* |
| 2921 | * Poll queues don't need interrupts, but we need at least one I/O queue |
| 2922 | * left over for non-polled I/O. |
| 2923 | */ |
| 2924 | poll_queues = min(dev->nr_poll_queues, nr_io_queues - 1); |
| 2925 | dev->io_queues[HCTX_TYPE_POLL] = poll_queues; |
| 2926 | |
| 2927 | /* |
| 2928 | * Initialize for the single interrupt case, will be updated in |
| 2929 | * nvme_calc_irq_sets(). |
| 2930 | */ |
| 2931 | dev->io_queues[HCTX_TYPE_DEFAULT] = 1; |
| 2932 | dev->io_queues[HCTX_TYPE_READ] = 0; |
| 2933 | |
| 2934 | /* |
| 2935 | * We need interrupts for the admin queue and each non-polled I/O queue, |
| 2936 | * but some Apple controllers require all queues to use the first |
| 2937 | * vector. |
| 2938 | */ |
| 2939 | irq_queues = 1; |
| 2940 | if (!(dev->ctrl.quirks & NVME_QUIRK_SINGLE_VECTOR)) |
| 2941 | irq_queues += (nr_io_queues - poll_queues); |
| 2942 | if (dev->ctrl.quirks & NVME_QUIRK_BROKEN_MSI) |
| 2943 | flags &= ~PCI_IRQ_MSI; |
| 2944 | return pci_alloc_irq_vectors_affinity(pdev, 1, irq_queues, flags, |
| 2945 | &affd); |
| 2946 | } |
| 2947 | |
| 2948 | static unsigned int nvme_max_io_queues(struct nvme_dev *dev) |
| 2949 | { |
| 2950 | /* |
| 2951 | * If tags are shared with admin queue (Apple bug), then |
| 2952 | * make sure we only use one IO queue. |
| 2953 | */ |
| 2954 | if (dev->ctrl.quirks & NVME_QUIRK_SHARED_TAGS) |
| 2955 | return 1; |
| 2956 | return blk_mq_num_possible_queues(0) + dev->nr_write_queues + |
| 2957 | dev->nr_poll_queues; |
| 2958 | } |
| 2959 | |
| 2960 | static int nvme_setup_io_queues(struct nvme_dev *dev) |
| 2961 | { |
| 2962 | struct nvme_queue *adminq = &dev->queues[0]; |
| 2963 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
| 2964 | unsigned int nr_io_queues; |
| 2965 | unsigned long size; |
| 2966 | int result; |
| 2967 | |
| 2968 | /* |
| 2969 | * Sample the module parameters once at reset time so that we have |
| 2970 | * stable values to work with. |
| 2971 | */ |
| 2972 | dev->nr_write_queues = write_queues; |
| 2973 | dev->nr_poll_queues = poll_queues; |
| 2974 | |
| 2975 | if (dev->ctrl.tagset) { |
| 2976 | /* |
| 2977 | * The set's maps are allocated only once at initialization |
| 2978 | * time. We can't add special queues later if their mq_map |
| 2979 | * wasn't preallocated. |
| 2980 | */ |
| 2981 | if (dev->ctrl.tagset->nr_maps < 3) |
| 2982 | dev->nr_poll_queues = 0; |
| 2983 | if (dev->ctrl.tagset->nr_maps < 2) |
| 2984 | dev->nr_write_queues = 0; |
| 2985 | } |
| 2986 | |
| 2987 | /* |
| 2988 | * The initial number of allocated queue slots may be too large if the |
| 2989 | * user reduced the special queue parameters. Cap the value to the |
| 2990 | * number we need for this round. |
| 2991 | */ |
| 2992 | nr_io_queues = min(nvme_max_io_queues(dev), |
| 2993 | dev->nr_allocated_queues - 1); |
| 2994 | result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues); |
| 2995 | if (result < 0) |
| 2996 | return result; |
| 2997 | |
| 2998 | if (nr_io_queues == 0) |
| 2999 | return 0; |
| 3000 | |
| 3001 | /* |
| 3002 | * Free IRQ resources as soon as NVMEQ_ENABLED bit transitions |
| 3003 | * from set to unset. If there is a window to it is truely freed, |
| 3004 | * pci_free_irq_vectors() jumping into this window will crash. |
| 3005 | * And take lock to avoid racing with pci_free_irq_vectors() in |
| 3006 | * nvme_dev_disable() path. |
| 3007 | */ |
| 3008 | result = nvme_setup_io_queues_trylock(dev); |
| 3009 | if (result) |
| 3010 | return result; |
| 3011 | if (test_and_clear_bit(NVMEQ_ENABLED, &adminq->flags)) |
| 3012 | pci_free_irq(pdev, 0, adminq); |
| 3013 | |
| 3014 | if (dev->cmb_use_sqes) { |
| 3015 | result = nvme_cmb_qdepth(dev, nr_io_queues, |
| 3016 | sizeof(struct nvme_command)); |
| 3017 | if (result > 0) { |
| 3018 | dev->q_depth = result; |
| 3019 | dev->ctrl.sqsize = result - 1; |
| 3020 | } else { |
| 3021 | dev->cmb_use_sqes = false; |
| 3022 | } |
| 3023 | } |
| 3024 | |
| 3025 | do { |
| 3026 | size = db_bar_size(dev, nr_io_queues); |
| 3027 | result = nvme_remap_bar(dev, size); |
| 3028 | if (!result) |
| 3029 | break; |
| 3030 | if (!--nr_io_queues) { |
| 3031 | result = -ENOMEM; |
| 3032 | goto out_unlock; |
| 3033 | } |
| 3034 | } while (1); |
| 3035 | adminq->q_db = dev->dbs; |
| 3036 | |
| 3037 | retry: |
| 3038 | /* Deregister the admin queue's interrupt */ |
| 3039 | if (test_and_clear_bit(NVMEQ_ENABLED, &adminq->flags)) |
| 3040 | pci_free_irq(pdev, 0, adminq); |
| 3041 | |
| 3042 | /* |
| 3043 | * If we enable msix early due to not intx, disable it again before |
| 3044 | * setting up the full range we need. |
| 3045 | */ |
| 3046 | pci_free_irq_vectors(pdev); |
| 3047 | |
| 3048 | result = nvme_setup_irqs(dev, nr_io_queues); |
| 3049 | if (result <= 0) { |
| 3050 | result = -EIO; |
| 3051 | goto out_unlock; |
| 3052 | } |
| 3053 | |
| 3054 | dev->num_vecs = result; |
| 3055 | result = max(result - 1, 1); |
| 3056 | dev->max_qid = result + dev->io_queues[HCTX_TYPE_POLL]; |
| 3057 | |
| 3058 | /* |
| 3059 | * Should investigate if there's a performance win from allocating |
| 3060 | * more queues than interrupt vectors; it might allow the submission |
| 3061 | * path to scale better, even if the receive path is limited by the |
| 3062 | * number of interrupts. |
| 3063 | */ |
| 3064 | result = queue_request_irq(adminq); |
| 3065 | if (result) |
| 3066 | goto out_unlock; |
| 3067 | set_bit(NVMEQ_ENABLED, &adminq->flags); |
| 3068 | mutex_unlock(&dev->shutdown_lock); |
| 3069 | |
| 3070 | result = nvme_create_io_queues(dev); |
| 3071 | if (result || dev->online_queues < 2) |
| 3072 | return result; |
| 3073 | |
| 3074 | if (dev->online_queues - 1 < dev->max_qid) { |
| 3075 | nr_io_queues = dev->online_queues - 1; |
| 3076 | nvme_delete_io_queues(dev); |
| 3077 | result = nvme_setup_io_queues_trylock(dev); |
| 3078 | if (result) |
| 3079 | return result; |
| 3080 | nvme_suspend_io_queues(dev); |
| 3081 | goto retry; |
| 3082 | } |
| 3083 | dev_info(dev->ctrl.device, "%d/%d/%d default/read/poll queues\n", |
| 3084 | dev->io_queues[HCTX_TYPE_DEFAULT], |
| 3085 | dev->io_queues[HCTX_TYPE_READ], |
| 3086 | dev->io_queues[HCTX_TYPE_POLL]); |
| 3087 | return 0; |
| 3088 | out_unlock: |
| 3089 | mutex_unlock(&dev->shutdown_lock); |
| 3090 | return result; |
| 3091 | } |
| 3092 | |
| 3093 | static enum rq_end_io_ret nvme_del_queue_end(struct request *req, |
| 3094 | blk_status_t error, |
| 3095 | const struct io_comp_batch *iob) |
| 3096 | { |
| 3097 | struct nvme_queue *nvmeq = req->end_io_data; |
| 3098 | |
| 3099 | blk_mq_free_request(req); |
| 3100 | complete(&nvmeq->delete_done); |
| 3101 | return RQ_END_IO_NONE; |
| 3102 | } |
| 3103 | |
| 3104 | static enum rq_end_io_ret nvme_del_cq_end(struct request *req, |
| 3105 | blk_status_t error, |
| 3106 | const struct io_comp_batch *iob) |
| 3107 | { |
| 3108 | struct nvme_queue *nvmeq = req->end_io_data; |
| 3109 | |
| 3110 | if (error) |
| 3111 | set_bit(NVMEQ_DELETE_ERROR, &nvmeq->flags); |
| 3112 | |
| 3113 | return nvme_del_queue_end(req, error, iob); |
| 3114 | } |
| 3115 | |
| 3116 | static int nvme_delete_queue(struct nvme_queue *nvmeq, u8 opcode) |
| 3117 | { |
| 3118 | struct request_queue *q = nvmeq->dev->ctrl.admin_q; |
| 3119 | struct request *req; |
| 3120 | struct nvme_command cmd = { }; |
| 3121 | |
| 3122 | cmd.delete_queue.opcode = opcode; |
| 3123 | cmd.delete_queue.qid = cpu_to_le16(nvmeq->qid); |
| 3124 | |
| 3125 | req = blk_mq_alloc_request(q, nvme_req_op(&cmd), BLK_MQ_REQ_NOWAIT); |
| 3126 | if (IS_ERR(req)) |
| 3127 | return PTR_ERR(req); |
| 3128 | nvme_init_request(req, &cmd); |
| 3129 | |
| 3130 | if (opcode == nvme_admin_delete_cq) |
| 3131 | req->end_io = nvme_del_cq_end; |
| 3132 | else |
| 3133 | req->end_io = nvme_del_queue_end; |
| 3134 | req->end_io_data = nvmeq; |
| 3135 | |
| 3136 | init_completion(&nvmeq->delete_done); |
| 3137 | blk_execute_rq_nowait(req, false); |
| 3138 | return 0; |
| 3139 | } |
| 3140 | |
| 3141 | static bool __nvme_delete_io_queues(struct nvme_dev *dev, u8 opcode) |
| 3142 | { |
| 3143 | int nr_queues = dev->online_queues - 1, sent = 0; |
| 3144 | unsigned long timeout; |
| 3145 | |
| 3146 | retry: |
| 3147 | timeout = dev->ctrl.admin_timeout; |
| 3148 | while (nr_queues > 0) { |
| 3149 | if (nvme_delete_queue(&dev->queues[nr_queues], opcode)) |
| 3150 | break; |
| 3151 | nr_queues--; |
| 3152 | sent++; |
| 3153 | } |
| 3154 | while (sent) { |
| 3155 | struct nvme_queue *nvmeq = &dev->queues[nr_queues + sent]; |
| 3156 | |
| 3157 | timeout = wait_for_completion_io_timeout(&nvmeq->delete_done, |
| 3158 | timeout); |
| 3159 | if (timeout == 0) |
| 3160 | return false; |
| 3161 | |
| 3162 | sent--; |
| 3163 | if (nr_queues) |
| 3164 | goto retry; |
| 3165 | } |
| 3166 | return true; |
| 3167 | } |
| 3168 | |
| 3169 | static void nvme_delete_io_queues(struct nvme_dev *dev) |
| 3170 | { |
| 3171 | if (__nvme_delete_io_queues(dev, nvme_admin_delete_sq)) |
| 3172 | __nvme_delete_io_queues(dev, nvme_admin_delete_cq); |
| 3173 | } |
| 3174 | |
| 3175 | static unsigned int nvme_pci_nr_maps(struct nvme_dev *dev) |
| 3176 | { |
| 3177 | if (dev->io_queues[HCTX_TYPE_POLL]) |
| 3178 | return 3; |
| 3179 | if (dev->io_queues[HCTX_TYPE_READ]) |
| 3180 | return 2; |
| 3181 | return 1; |
| 3182 | } |
| 3183 | |
| 3184 | static bool nvme_pci_update_nr_queues(struct nvme_dev *dev) |
| 3185 | { |
| 3186 | if (!dev->ctrl.tagset) { |
| 3187 | nvme_alloc_io_tag_set(&dev->ctrl, &dev->tagset, &nvme_mq_ops, |
| 3188 | nvme_pci_nr_maps(dev), sizeof(struct nvme_iod)); |
| 3189 | return true; |
| 3190 | } |
| 3191 | |
| 3192 | /* Give up if we are racing with nvme_dev_disable() */ |
| 3193 | if (!mutex_trylock(&dev->shutdown_lock)) |
| 3194 | return false; |
| 3195 | |
| 3196 | /* Check if nvme_dev_disable() has been executed already */ |
| 3197 | if (!dev->online_queues) { |
| 3198 | mutex_unlock(&dev->shutdown_lock); |
| 3199 | return false; |
| 3200 | } |
| 3201 | |
| 3202 | blk_mq_update_nr_hw_queues(&dev->tagset, dev->online_queues - 1); |
| 3203 | /* free previously allocated queues that are no longer usable */ |
| 3204 | nvme_free_queues(dev, dev->online_queues); |
| 3205 | mutex_unlock(&dev->shutdown_lock); |
| 3206 | return true; |
| 3207 | } |
| 3208 | |
| 3209 | static int nvme_pci_enable(struct nvme_dev *dev) |
| 3210 | { |
| 3211 | int result = -ENOMEM; |
| 3212 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
| 3213 | unsigned int flags = PCI_IRQ_ALL_TYPES; |
| 3214 | |
| 3215 | if (pci_enable_device_mem(pdev)) |
| 3216 | return result; |
| 3217 | |
| 3218 | pci_set_master(pdev); |
| 3219 | |
| 3220 | if (readl(dev->bar + NVME_REG_CSTS) == -1) { |
| 3221 | dev_dbg(dev->ctrl.device, "reading CSTS register failed\n"); |
| 3222 | result = -ENODEV; |
| 3223 | goto disable; |
| 3224 | } |
| 3225 | |
| 3226 | /* |
| 3227 | * Some devices and/or platforms don't advertise or work with INTx |
| 3228 | * interrupts. Pre-enable a single MSIX or MSI vec for setup. We'll |
| 3229 | * adjust this later. |
| 3230 | */ |
| 3231 | if (dev->ctrl.quirks & NVME_QUIRK_BROKEN_MSI) |
| 3232 | flags &= ~PCI_IRQ_MSI; |
| 3233 | result = pci_alloc_irq_vectors(pdev, 1, 1, flags); |
| 3234 | if (result < 0) |
| 3235 | goto disable; |
| 3236 | |
| 3237 | dev->ctrl.cap = lo_hi_readq(dev->bar + NVME_REG_CAP); |
| 3238 | |
| 3239 | dev->q_depth = min_t(u32, NVME_CAP_MQES(dev->ctrl.cap) + 1, |
| 3240 | io_queue_depth); |
| 3241 | dev->db_stride = 1 << NVME_CAP_STRIDE(dev->ctrl.cap); |
| 3242 | dev->dbs = dev->bar + 4096; |
| 3243 | |
| 3244 | /* |
| 3245 | * Some Apple controllers require a non-standard SQE size. |
| 3246 | * Interestingly they also seem to ignore the CC:IOSQES register |
| 3247 | * so we don't bother updating it here. |
| 3248 | */ |
| 3249 | if (dev->ctrl.quirks & NVME_QUIRK_128_BYTES_SQES) |
| 3250 | dev->io_sqes = 7; |
| 3251 | else |
| 3252 | dev->io_sqes = NVME_NVM_IOSQES; |
| 3253 | |
| 3254 | if (dev->ctrl.quirks & NVME_QUIRK_QDEPTH_ONE) { |
| 3255 | dev->q_depth = 2; |
| 3256 | } else if (pdev->vendor == PCI_VENDOR_ID_SAMSUNG && |
| 3257 | (pdev->device == 0xa821 || pdev->device == 0xa822) && |
| 3258 | NVME_CAP_MQES(dev->ctrl.cap) == 0) { |
| 3259 | dev->q_depth = 64; |
| 3260 | dev_err(dev->ctrl.device, "detected PM1725 NVMe controller, " |
| 3261 | "set queue depth=%u\n", dev->q_depth); |
| 3262 | } |
| 3263 | |
| 3264 | /* |
| 3265 | * Controllers with the shared tags quirk need the IO queue to be |
| 3266 | * big enough so that we get 32 tags for the admin queue |
| 3267 | */ |
| 3268 | if ((dev->ctrl.quirks & NVME_QUIRK_SHARED_TAGS) && |
| 3269 | (dev->q_depth < (NVME_AQ_DEPTH + 2))) { |
| 3270 | dev->q_depth = NVME_AQ_DEPTH + 2; |
| 3271 | dev_warn(dev->ctrl.device, "IO queue depth clamped to %d\n", |
| 3272 | dev->q_depth); |
| 3273 | } |
| 3274 | dev->ctrl.sqsize = dev->q_depth - 1; /* 0's based queue depth */ |
| 3275 | |
| 3276 | nvme_map_cmb(dev); |
| 3277 | |
| 3278 | pci_save_state(pdev); |
| 3279 | |
| 3280 | result = nvme_pci_configure_admin_queue(dev); |
| 3281 | if (result) |
| 3282 | goto free_irq; |
| 3283 | return result; |
| 3284 | |
| 3285 | free_irq: |
| 3286 | pci_free_irq_vectors(pdev); |
| 3287 | disable: |
| 3288 | pci_disable_device(pdev); |
| 3289 | return result; |
| 3290 | } |
| 3291 | |
| 3292 | static void nvme_dev_unmap(struct nvme_dev *dev) |
| 3293 | { |
| 3294 | if (dev->bar) |
| 3295 | iounmap(dev->bar); |
| 3296 | pci_release_mem_regions(to_pci_dev(dev->dev)); |
| 3297 | } |
| 3298 | |
| 3299 | static bool nvme_pci_ctrl_is_dead(struct nvme_dev *dev) |
| 3300 | { |
| 3301 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
| 3302 | u32 csts; |
| 3303 | |
| 3304 | if (!pci_is_enabled(pdev) || !pci_device_is_present(pdev)) |
| 3305 | return true; |
| 3306 | if (pdev->error_state != pci_channel_io_normal) |
| 3307 | return true; |
| 3308 | |
| 3309 | csts = readl(dev->bar + NVME_REG_CSTS); |
| 3310 | return (csts & NVME_CSTS_CFS) || !(csts & NVME_CSTS_RDY); |
| 3311 | } |
| 3312 | |
| 3313 | static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown) |
| 3314 | { |
| 3315 | enum nvme_ctrl_state state = nvme_ctrl_state(&dev->ctrl); |
| 3316 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
| 3317 | bool dead; |
| 3318 | |
| 3319 | mutex_lock(&dev->shutdown_lock); |
| 3320 | dead = nvme_pci_ctrl_is_dead(dev); |
| 3321 | if (state == NVME_CTRL_LIVE || state == NVME_CTRL_RESETTING) { |
| 3322 | if (pci_is_enabled(pdev)) |
| 3323 | nvme_start_freeze(&dev->ctrl); |
| 3324 | /* |
| 3325 | * Give the controller a chance to complete all entered requests |
| 3326 | * if doing a safe shutdown. |
| 3327 | */ |
| 3328 | if (!dead && shutdown) |
| 3329 | nvme_wait_freeze_timeout(&dev->ctrl); |
| 3330 | } |
| 3331 | |
| 3332 | nvme_quiesce_io_queues(&dev->ctrl); |
| 3333 | |
| 3334 | if (!dead && dev->ctrl.queue_count > 0) { |
| 3335 | nvme_delete_io_queues(dev); |
| 3336 | nvme_disable_ctrl(&dev->ctrl, shutdown); |
| 3337 | nvme_poll_irqdisable(&dev->queues[0]); |
| 3338 | } |
| 3339 | nvme_suspend_io_queues(dev); |
| 3340 | nvme_suspend_queue(dev, 0); |
| 3341 | pci_free_irq_vectors(pdev); |
| 3342 | if (pci_is_enabled(pdev)) |
| 3343 | pci_disable_device(pdev); |
| 3344 | nvme_reap_pending_cqes(dev); |
| 3345 | |
| 3346 | nvme_cancel_tagset(&dev->ctrl); |
| 3347 | nvme_cancel_admin_tagset(&dev->ctrl); |
| 3348 | |
| 3349 | /* |
| 3350 | * The driver will not be starting up queues again if shutting down so |
| 3351 | * must flush all entered requests to their failed completion to avoid |
| 3352 | * deadlocking blk-mq hot-cpu notifier. |
| 3353 | */ |
| 3354 | if (shutdown) { |
| 3355 | nvme_unquiesce_io_queues(&dev->ctrl); |
| 3356 | if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) |
| 3357 | nvme_unquiesce_admin_queue(&dev->ctrl); |
| 3358 | } |
| 3359 | mutex_unlock(&dev->shutdown_lock); |
| 3360 | } |
| 3361 | |
| 3362 | static int nvme_disable_prepare_reset(struct nvme_dev *dev, bool shutdown) |
| 3363 | { |
| 3364 | if (!nvme_wait_reset(&dev->ctrl)) |
| 3365 | return -EBUSY; |
| 3366 | nvme_dev_disable(dev, shutdown); |
| 3367 | return 0; |
| 3368 | } |
| 3369 | |
| 3370 | static int nvme_pci_alloc_iod_mempool(struct nvme_dev *dev) |
| 3371 | { |
| 3372 | size_t alloc_size = sizeof(struct nvme_dma_vec) * NVME_MAX_SEGS; |
| 3373 | |
| 3374 | dev->dmavec_mempool = mempool_create_node(1, |
| 3375 | mempool_kmalloc, mempool_kfree, |
| 3376 | (void *)alloc_size, GFP_KERNEL, |
| 3377 | dev_to_node(dev->dev)); |
| 3378 | if (!dev->dmavec_mempool) |
| 3379 | return -ENOMEM; |
| 3380 | return 0; |
| 3381 | } |
| 3382 | |
| 3383 | static void nvme_free_tagset(struct nvme_dev *dev) |
| 3384 | { |
| 3385 | if (dev->tagset.tags) |
| 3386 | nvme_remove_io_tag_set(&dev->ctrl); |
| 3387 | dev->ctrl.tagset = NULL; |
| 3388 | } |
| 3389 | |
| 3390 | /* pairs with nvme_pci_alloc_dev */ |
| 3391 | static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl) |
| 3392 | { |
| 3393 | struct nvme_dev *dev = to_nvme_dev(ctrl); |
| 3394 | |
| 3395 | nvme_free_tagset(dev); |
| 3396 | put_device(dev->dev); |
| 3397 | kfree(dev->queues); |
| 3398 | kfree(dev); |
| 3399 | } |
| 3400 | |
| 3401 | static void nvme_reset_work(struct work_struct *work) |
| 3402 | { |
| 3403 | struct nvme_dev *dev = |
| 3404 | container_of(work, struct nvme_dev, ctrl.reset_work); |
| 3405 | bool was_suspend = !!(dev->ctrl.ctrl_config & NVME_CC_SHN_NORMAL); |
| 3406 | int result; |
| 3407 | |
| 3408 | if (nvme_ctrl_state(&dev->ctrl) != NVME_CTRL_RESETTING) { |
| 3409 | dev_warn(dev->ctrl.device, "ctrl state %d is not RESETTING\n", |
| 3410 | dev->ctrl.state); |
| 3411 | result = -ENODEV; |
| 3412 | goto out; |
| 3413 | } |
| 3414 | |
| 3415 | /* |
| 3416 | * If we're called to reset a live controller first shut it down before |
| 3417 | * moving on. |
| 3418 | */ |
| 3419 | if (dev->ctrl.ctrl_config & NVME_CC_ENABLE) |
| 3420 | nvme_dev_disable(dev, false); |
| 3421 | nvme_sync_queues(&dev->ctrl); |
| 3422 | |
| 3423 | mutex_lock(&dev->shutdown_lock); |
| 3424 | result = nvme_pci_enable(dev); |
| 3425 | if (result) |
| 3426 | goto out_unlock; |
| 3427 | nvme_unquiesce_admin_queue(&dev->ctrl); |
| 3428 | mutex_unlock(&dev->shutdown_lock); |
| 3429 | |
| 3430 | /* |
| 3431 | * Introduce CONNECTING state from nvme-fc/rdma transports to mark the |
| 3432 | * initializing procedure here. |
| 3433 | */ |
| 3434 | if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_CONNECTING)) { |
| 3435 | dev_warn(dev->ctrl.device, |
| 3436 | "failed to mark controller CONNECTING\n"); |
| 3437 | result = -EBUSY; |
| 3438 | goto out; |
| 3439 | } |
| 3440 | |
| 3441 | result = nvme_init_ctrl_finish(&dev->ctrl, was_suspend); |
| 3442 | if (result) |
| 3443 | goto out; |
| 3444 | |
| 3445 | if (nvme_ctrl_meta_sgl_supported(&dev->ctrl)) |
| 3446 | dev->ctrl.max_integrity_segments = NVME_MAX_META_SEGS; |
| 3447 | else |
| 3448 | dev->ctrl.max_integrity_segments = 1; |
| 3449 | |
| 3450 | nvme_dbbuf_dma_alloc(dev); |
| 3451 | |
| 3452 | result = nvme_setup_host_mem(dev); |
| 3453 | if (result < 0) |
| 3454 | goto out; |
| 3455 | |
| 3456 | nvme_update_attrs(dev); |
| 3457 | |
| 3458 | result = nvme_setup_io_queues(dev); |
| 3459 | if (result) |
| 3460 | goto out; |
| 3461 | |
| 3462 | /* |
| 3463 | * Freeze and update the number of I/O queues as those might have |
| 3464 | * changed. If there are no I/O queues left after this reset, keep the |
| 3465 | * controller around but remove all namespaces. |
| 3466 | */ |
| 3467 | if (dev->online_queues > 1) { |
| 3468 | nvme_dbbuf_set(dev); |
| 3469 | nvme_unquiesce_io_queues(&dev->ctrl); |
| 3470 | nvme_wait_freeze(&dev->ctrl); |
| 3471 | if (!nvme_pci_update_nr_queues(dev)) |
| 3472 | goto out; |
| 3473 | nvme_unfreeze(&dev->ctrl); |
| 3474 | } else { |
| 3475 | dev_warn(dev->ctrl.device, "IO queues lost\n"); |
| 3476 | nvme_mark_namespaces_dead(&dev->ctrl); |
| 3477 | nvme_unquiesce_io_queues(&dev->ctrl); |
| 3478 | nvme_remove_namespaces(&dev->ctrl); |
| 3479 | nvme_free_tagset(dev); |
| 3480 | } |
| 3481 | |
| 3482 | /* |
| 3483 | * If only admin queue live, keep it to do further investigation or |
| 3484 | * recovery. |
| 3485 | */ |
| 3486 | if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_LIVE)) { |
| 3487 | dev_warn(dev->ctrl.device, |
| 3488 | "failed to mark controller live state\n"); |
| 3489 | result = -ENODEV; |
| 3490 | goto out; |
| 3491 | } |
| 3492 | |
| 3493 | nvme_start_ctrl(&dev->ctrl); |
| 3494 | return; |
| 3495 | |
| 3496 | out_unlock: |
| 3497 | mutex_unlock(&dev->shutdown_lock); |
| 3498 | out: |
| 3499 | /* |
| 3500 | * Set state to deleting now to avoid blocking nvme_wait_reset(), which |
| 3501 | * may be holding this pci_dev's device lock. |
| 3502 | */ |
| 3503 | dev_warn(dev->ctrl.device, "Disabling device after reset failure: %d\n", |
| 3504 | result); |
| 3505 | nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING); |
| 3506 | nvme_dev_disable(dev, true); |
| 3507 | nvme_sync_queues(&dev->ctrl); |
| 3508 | nvme_mark_namespaces_dead(&dev->ctrl); |
| 3509 | nvme_unquiesce_io_queues(&dev->ctrl); |
| 3510 | nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DEAD); |
| 3511 | } |
| 3512 | |
| 3513 | static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val) |
| 3514 | { |
| 3515 | *val = readl(to_nvme_dev(ctrl)->bar + off); |
| 3516 | return 0; |
| 3517 | } |
| 3518 | |
| 3519 | static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val) |
| 3520 | { |
| 3521 | writel(val, to_nvme_dev(ctrl)->bar + off); |
| 3522 | return 0; |
| 3523 | } |
| 3524 | |
| 3525 | static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val) |
| 3526 | { |
| 3527 | *val = lo_hi_readq(to_nvme_dev(ctrl)->bar + off); |
| 3528 | return 0; |
| 3529 | } |
| 3530 | |
| 3531 | static int nvme_pci_get_address(struct nvme_ctrl *ctrl, char *buf, int size) |
| 3532 | { |
| 3533 | struct pci_dev *pdev = to_pci_dev(to_nvme_dev(ctrl)->dev); |
| 3534 | |
| 3535 | return snprintf(buf, size, "%s\n", dev_name(&pdev->dev)); |
| 3536 | } |
| 3537 | |
| 3538 | static void nvme_pci_print_device_info(struct nvme_ctrl *ctrl) |
| 3539 | { |
| 3540 | struct pci_dev *pdev = to_pci_dev(to_nvme_dev(ctrl)->dev); |
| 3541 | struct nvme_subsystem *subsys = ctrl->subsys; |
| 3542 | |
| 3543 | dev_err(ctrl->device, |
| 3544 | "VID:DID %04x:%04x model:%.*s firmware:%.*s\n", |
| 3545 | pdev->vendor, pdev->device, |
| 3546 | nvme_strlen(subsys->model, sizeof(subsys->model)), |
| 3547 | subsys->model, nvme_strlen(subsys->firmware_rev, |
| 3548 | sizeof(subsys->firmware_rev)), |
| 3549 | subsys->firmware_rev); |
| 3550 | } |
| 3551 | |
| 3552 | static bool nvme_pci_supports_pci_p2pdma(struct nvme_ctrl *ctrl) |
| 3553 | { |
| 3554 | struct nvme_dev *dev = to_nvme_dev(ctrl); |
| 3555 | |
| 3556 | return dma_pci_p2pdma_supported(dev->dev); |
| 3557 | } |
| 3558 | |
| 3559 | static unsigned long nvme_pci_get_virt_boundary(struct nvme_ctrl *ctrl, |
| 3560 | bool is_admin) |
| 3561 | { |
| 3562 | if (!nvme_ctrl_sgl_supported(ctrl) || is_admin) |
| 3563 | return NVME_CTRL_PAGE_SIZE - 1; |
| 3564 | return 0; |
| 3565 | } |
| 3566 | |
| 3567 | static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = { |
| 3568 | .name = "pcie", |
| 3569 | .module = THIS_MODULE, |
| 3570 | .flags = NVME_F_METADATA_SUPPORTED, |
| 3571 | .dev_attr_groups = nvme_pci_dev_attr_groups, |
| 3572 | .reg_read32 = nvme_pci_reg_read32, |
| 3573 | .reg_write32 = nvme_pci_reg_write32, |
| 3574 | .reg_read64 = nvme_pci_reg_read64, |
| 3575 | .free_ctrl = nvme_pci_free_ctrl, |
| 3576 | .submit_async_event = nvme_pci_submit_async_event, |
| 3577 | .subsystem_reset = nvme_pci_subsystem_reset, |
| 3578 | .get_address = nvme_pci_get_address, |
| 3579 | .print_device_info = nvme_pci_print_device_info, |
| 3580 | .supports_pci_p2pdma = nvme_pci_supports_pci_p2pdma, |
| 3581 | .get_virt_boundary = nvme_pci_get_virt_boundary, |
| 3582 | }; |
| 3583 | |
| 3584 | static int nvme_dev_map(struct nvme_dev *dev) |
| 3585 | { |
| 3586 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
| 3587 | |
| 3588 | if (pci_request_mem_regions(pdev, "nvme")) |
| 3589 | return -ENODEV; |
| 3590 | |
| 3591 | if (nvme_remap_bar(dev, NVME_REG_DBS + 4096)) |
| 3592 | goto release; |
| 3593 | |
| 3594 | return 0; |
| 3595 | release: |
| 3596 | pci_release_mem_regions(pdev); |
| 3597 | return -ENODEV; |
| 3598 | } |
| 3599 | |
| 3600 | static unsigned long check_vendor_combination_bug(struct pci_dev *pdev) |
| 3601 | { |
| 3602 | if (pdev->vendor == 0x144d && pdev->device == 0xa802) { |
| 3603 | /* |
| 3604 | * Several Samsung devices seem to drop off the PCIe bus |
| 3605 | * randomly when APST is on and uses the deepest sleep state. |
| 3606 | * This has been observed on a Samsung "SM951 NVMe SAMSUNG |
| 3607 | * 256GB", a "PM951 NVMe SAMSUNG 512GB", and a "Samsung SSD |
| 3608 | * 950 PRO 256GB", but it seems to be restricted to two Dell |
| 3609 | * laptops. |
| 3610 | */ |
| 3611 | if (dmi_match(DMI_SYS_VENDOR, "Dell Inc.") && |
| 3612 | (dmi_match(DMI_PRODUCT_NAME, "XPS 15 9550") || |
| 3613 | dmi_match(DMI_PRODUCT_NAME, "Precision 5510"))) |
| 3614 | return NVME_QUIRK_NO_DEEPEST_PS; |
| 3615 | } else if (pdev->vendor == 0x144d && pdev->device == 0xa804) { |
| 3616 | /* |
| 3617 | * Samsung SSD 960 EVO drops off the PCIe bus after system |
| 3618 | * suspend on a Ryzen board, ASUS PRIME B350M-A, as well as |
| 3619 | * within few minutes after bootup on a Coffee Lake board - |
| 3620 | * ASUS PRIME Z370-A |
| 3621 | */ |
| 3622 | if (dmi_match(DMI_BOARD_VENDOR, "ASUSTeK COMPUTER INC.") && |
| 3623 | (dmi_match(DMI_BOARD_NAME, "PRIME B350M-A") || |
| 3624 | dmi_match(DMI_BOARD_NAME, "PRIME Z370-A"))) |
| 3625 | return NVME_QUIRK_NO_APST; |
| 3626 | } else if ((pdev->vendor == 0x144d && (pdev->device == 0xa801 || |
| 3627 | pdev->device == 0xa808 || pdev->device == 0xa809)) || |
| 3628 | (pdev->vendor == 0x1e0f && pdev->device == 0x0001)) { |
| 3629 | /* |
| 3630 | * Forcing to use host managed nvme power settings for |
| 3631 | * lowest idle power with quick resume latency on |
| 3632 | * Samsung and Toshiba SSDs based on suspend behavior |
| 3633 | * on Coffee Lake board for LENOVO C640 |
| 3634 | */ |
| 3635 | if ((dmi_match(DMI_BOARD_VENDOR, "LENOVO")) && |
| 3636 | dmi_match(DMI_BOARD_NAME, "LNVNB161216")) |
| 3637 | return NVME_QUIRK_SIMPLE_SUSPEND; |
| 3638 | } else if (pdev->vendor == 0x2646 && (pdev->device == 0x2263 || |
| 3639 | pdev->device == 0x500f)) { |
| 3640 | /* |
| 3641 | * Exclude some Kingston NV1 and A2000 devices from |
| 3642 | * NVME_QUIRK_SIMPLE_SUSPEND. Do a full suspend to save a |
| 3643 | * lot of energy with s2idle sleep on some TUXEDO platforms. |
| 3644 | */ |
| 3645 | if (dmi_match(DMI_BOARD_NAME, "NS5X_NS7XAU") || |
| 3646 | dmi_match(DMI_BOARD_NAME, "NS5x_7xAU") || |
| 3647 | dmi_match(DMI_BOARD_NAME, "NS5x_7xPU") || |
| 3648 | dmi_match(DMI_BOARD_NAME, "PH4PRX1_PH6PRX1")) |
| 3649 | return NVME_QUIRK_FORCE_NO_SIMPLE_SUSPEND; |
| 3650 | } else if (pdev->vendor == 0x144d && pdev->device == 0xa80d) { |
| 3651 | /* |
| 3652 | * Exclude Samsung 990 Evo from NVME_QUIRK_SIMPLE_SUSPEND |
| 3653 | * because of high power consumption (> 2 Watt) in s2idle |
| 3654 | * sleep. Only some boards with Intel CPU are affected. |
| 3655 | * (Note for testing: Samsung 990 Evo Plus has same PCI ID) |
| 3656 | */ |
| 3657 | if (dmi_match(DMI_BOARD_NAME, "DN50Z-140HC-YD") || |
| 3658 | dmi_match(DMI_BOARD_NAME, "GMxPXxx") || |
| 3659 | dmi_match(DMI_BOARD_NAME, "GXxMRXx") || |
| 3660 | dmi_match(DMI_BOARD_NAME, "NS5X_NS7XAU") || |
| 3661 | dmi_match(DMI_BOARD_NAME, "PH4PG31") || |
| 3662 | dmi_match(DMI_BOARD_NAME, "PH4PRX1_PH6PRX1") || |
| 3663 | dmi_match(DMI_BOARD_NAME, "PH6PG01_PH6PG71")) |
| 3664 | return NVME_QUIRK_FORCE_NO_SIMPLE_SUSPEND; |
| 3665 | } |
| 3666 | |
| 3667 | /* |
| 3668 | * NVMe SSD drops off the PCIe bus after system idle |
| 3669 | * for 10 hours on a Lenovo N60z board. |
| 3670 | */ |
| 3671 | if (dmi_match(DMI_BOARD_NAME, "LXKT-ZXEG-N6")) |
| 3672 | return NVME_QUIRK_NO_APST; |
| 3673 | |
| 3674 | return 0; |
| 3675 | } |
| 3676 | |
| 3677 | static struct quirk_entry *detect_dynamic_quirks(struct pci_dev *pdev) |
| 3678 | { |
| 3679 | int i; |
| 3680 | |
| 3681 | for (i = 0; i < nvme_pci_quirk_count; i++) |
| 3682 | if (pdev->vendor == nvme_pci_quirk_list[i].vendor_id && |
| 3683 | pdev->device == nvme_pci_quirk_list[i].dev_id) |
| 3684 | return &nvme_pci_quirk_list[i]; |
| 3685 | |
| 3686 | return NULL; |
| 3687 | } |
| 3688 | |
| 3689 | static struct nvme_dev *nvme_pci_alloc_dev(struct pci_dev *pdev, |
| 3690 | const struct pci_device_id *id) |
| 3691 | { |
| 3692 | unsigned long quirks = id->driver_data; |
| 3693 | int node = dev_to_node(&pdev->dev); |
| 3694 | struct nvme_dev *dev; |
| 3695 | struct quirk_entry *qentry; |
| 3696 | int ret = -ENOMEM; |
| 3697 | |
| 3698 | dev = kzalloc_node(struct_size(dev, descriptor_pools, nr_node_ids), |
| 3699 | GFP_KERNEL, node); |
| 3700 | if (!dev) |
| 3701 | return ERR_PTR(-ENOMEM); |
| 3702 | INIT_WORK(&dev->ctrl.reset_work, nvme_reset_work); |
| 3703 | mutex_init(&dev->shutdown_lock); |
| 3704 | |
| 3705 | dev->nr_write_queues = write_queues; |
| 3706 | dev->nr_poll_queues = poll_queues; |
| 3707 | dev->nr_allocated_queues = nvme_max_io_queues(dev) + 1; |
| 3708 | dev->queues = kcalloc_node(dev->nr_allocated_queues, |
| 3709 | sizeof(struct nvme_queue), GFP_KERNEL, node); |
| 3710 | if (!dev->queues) |
| 3711 | goto out_free_dev; |
| 3712 | |
| 3713 | dev->dev = get_device(&pdev->dev); |
| 3714 | |
| 3715 | quirks |= check_vendor_combination_bug(pdev); |
| 3716 | if (!noacpi && |
| 3717 | !(quirks & NVME_QUIRK_FORCE_NO_SIMPLE_SUSPEND) && |
| 3718 | acpi_storage_d3(&pdev->dev)) { |
| 3719 | /* |
| 3720 | * Some systems use a bios work around to ask for D3 on |
| 3721 | * platforms that support kernel managed suspend. |
| 3722 | */ |
| 3723 | dev_info(&pdev->dev, |
| 3724 | "platform quirk: setting simple suspend\n"); |
| 3725 | quirks |= NVME_QUIRK_SIMPLE_SUSPEND; |
| 3726 | } |
| 3727 | qentry = detect_dynamic_quirks(pdev); |
| 3728 | if (qentry) { |
| 3729 | quirks |= qentry->enabled_quirks; |
| 3730 | quirks &= ~qentry->disabled_quirks; |
| 3731 | } |
| 3732 | ret = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops, |
| 3733 | quirks); |
| 3734 | if (ret) |
| 3735 | goto out_put_device; |
| 3736 | |
| 3737 | if (dev->ctrl.quirks & NVME_QUIRK_DMA_ADDRESS_BITS_48) |
| 3738 | dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(48)); |
| 3739 | else |
| 3740 | dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); |
| 3741 | dma_set_min_align_mask(&pdev->dev, NVME_CTRL_PAGE_SIZE - 1); |
| 3742 | dma_set_max_seg_size(&pdev->dev, 0xffffffff); |
| 3743 | |
| 3744 | /* |
| 3745 | * Limit the max command size to prevent iod->sg allocations going |
| 3746 | * over a single page. |
| 3747 | */ |
| 3748 | dev->ctrl.max_hw_sectors = min_t(u32, |
| 3749 | NVME_MAX_BYTES >> SECTOR_SHIFT, |
| 3750 | dma_opt_mapping_size(&pdev->dev) >> 9); |
| 3751 | dev->ctrl.max_segments = NVME_MAX_SEGS; |
| 3752 | dev->ctrl.max_integrity_segments = 1; |
| 3753 | return dev; |
| 3754 | |
| 3755 | out_put_device: |
| 3756 | put_device(dev->dev); |
| 3757 | kfree(dev->queues); |
| 3758 | out_free_dev: |
| 3759 | kfree(dev); |
| 3760 | return ERR_PTR(ret); |
| 3761 | } |
| 3762 | |
| 3763 | static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
| 3764 | { |
| 3765 | struct nvme_dev *dev; |
| 3766 | int result = -ENOMEM; |
| 3767 | |
| 3768 | dev = nvme_pci_alloc_dev(pdev, id); |
| 3769 | if (IS_ERR(dev)) |
| 3770 | return PTR_ERR(dev); |
| 3771 | |
| 3772 | result = nvme_add_ctrl(&dev->ctrl); |
| 3773 | if (result) |
| 3774 | goto out_put_ctrl; |
| 3775 | |
| 3776 | result = nvme_dev_map(dev); |
| 3777 | if (result) |
| 3778 | goto out_uninit_ctrl; |
| 3779 | |
| 3780 | result = nvme_pci_alloc_iod_mempool(dev); |
| 3781 | if (result) |
| 3782 | goto out_dev_unmap; |
| 3783 | |
| 3784 | dev_info(dev->ctrl.device, "pci function %s\n", dev_name(&pdev->dev)); |
| 3785 | |
| 3786 | result = nvme_pci_enable(dev); |
| 3787 | if (result) |
| 3788 | goto out_release_iod_mempool; |
| 3789 | |
| 3790 | result = nvme_alloc_admin_tag_set(&dev->ctrl, &dev->admin_tagset, |
| 3791 | &nvme_mq_admin_ops, sizeof(struct nvme_iod)); |
| 3792 | if (result) |
| 3793 | goto out_disable; |
| 3794 | |
| 3795 | /* |
| 3796 | * Mark the controller as connecting before sending admin commands to |
| 3797 | * allow the timeout handler to do the right thing. |
| 3798 | */ |
| 3799 | if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_CONNECTING)) { |
| 3800 | dev_warn(dev->ctrl.device, |
| 3801 | "failed to mark controller CONNECTING\n"); |
| 3802 | result = -EBUSY; |
| 3803 | goto out_disable; |
| 3804 | } |
| 3805 | |
| 3806 | result = nvme_init_ctrl_finish(&dev->ctrl, false); |
| 3807 | if (result) |
| 3808 | goto out_disable; |
| 3809 | |
| 3810 | if (nvme_ctrl_meta_sgl_supported(&dev->ctrl)) |
| 3811 | dev->ctrl.max_integrity_segments = NVME_MAX_META_SEGS; |
| 3812 | else |
| 3813 | dev->ctrl.max_integrity_segments = 1; |
| 3814 | |
| 3815 | nvme_dbbuf_dma_alloc(dev); |
| 3816 | |
| 3817 | result = nvme_setup_host_mem(dev); |
| 3818 | if (result < 0) |
| 3819 | goto out_disable; |
| 3820 | |
| 3821 | nvme_update_attrs(dev); |
| 3822 | |
| 3823 | result = nvme_setup_io_queues(dev); |
| 3824 | if (result) |
| 3825 | goto out_disable; |
| 3826 | |
| 3827 | if (dev->online_queues > 1) { |
| 3828 | nvme_alloc_io_tag_set(&dev->ctrl, &dev->tagset, &nvme_mq_ops, |
| 3829 | nvme_pci_nr_maps(dev), sizeof(struct nvme_iod)); |
| 3830 | nvme_dbbuf_set(dev); |
| 3831 | } |
| 3832 | |
| 3833 | if (!dev->ctrl.tagset) |
| 3834 | dev_warn(dev->ctrl.device, "IO queues not created\n"); |
| 3835 | |
| 3836 | if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_LIVE)) { |
| 3837 | dev_warn(dev->ctrl.device, |
| 3838 | "failed to mark controller live state\n"); |
| 3839 | result = -ENODEV; |
| 3840 | goto out_disable; |
| 3841 | } |
| 3842 | |
| 3843 | pci_set_drvdata(pdev, dev); |
| 3844 | |
| 3845 | nvme_start_ctrl(&dev->ctrl); |
| 3846 | nvme_put_ctrl(&dev->ctrl); |
| 3847 | flush_work(&dev->ctrl.scan_work); |
| 3848 | return 0; |
| 3849 | |
| 3850 | out_disable: |
| 3851 | nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING); |
| 3852 | nvme_dev_disable(dev, true); |
| 3853 | nvme_free_host_mem(dev); |
| 3854 | nvme_dev_remove_admin(dev); |
| 3855 | nvme_dbbuf_dma_free(dev); |
| 3856 | nvme_free_queues(dev, 0); |
| 3857 | nvme_release_descriptor_pools(dev); |
| 3858 | out_release_iod_mempool: |
| 3859 | mempool_destroy(dev->dmavec_mempool); |
| 3860 | out_dev_unmap: |
| 3861 | nvme_dev_unmap(dev); |
| 3862 | out_uninit_ctrl: |
| 3863 | nvme_uninit_ctrl(&dev->ctrl); |
| 3864 | out_put_ctrl: |
| 3865 | nvme_put_ctrl(&dev->ctrl); |
| 3866 | dev_err_probe(&pdev->dev, result, "probe failed\n"); |
| 3867 | return result; |
| 3868 | } |
| 3869 | |
| 3870 | static void nvme_reset_prepare(struct pci_dev *pdev) |
| 3871 | { |
| 3872 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
| 3873 | |
| 3874 | /* |
| 3875 | * We don't need to check the return value from waiting for the reset |
| 3876 | * state as pci_dev device lock is held, making it impossible to race |
| 3877 | * with ->remove(). |
| 3878 | */ |
| 3879 | nvme_disable_prepare_reset(dev, false); |
| 3880 | nvme_sync_queues(&dev->ctrl); |
| 3881 | } |
| 3882 | |
| 3883 | static void nvme_reset_done(struct pci_dev *pdev) |
| 3884 | { |
| 3885 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
| 3886 | |
| 3887 | if (!nvme_try_sched_reset(&dev->ctrl)) |
| 3888 | flush_work(&dev->ctrl.reset_work); |
| 3889 | } |
| 3890 | |
| 3891 | static void nvme_shutdown(struct pci_dev *pdev) |
| 3892 | { |
| 3893 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
| 3894 | |
| 3895 | nvme_disable_prepare_reset(dev, true); |
| 3896 | } |
| 3897 | |
| 3898 | /* |
| 3899 | * The driver's remove may be called on a device in a partially initialized |
| 3900 | * state. This function must not have any dependencies on the device state in |
| 3901 | * order to proceed. |
| 3902 | */ |
| 3903 | static void nvme_remove(struct pci_dev *pdev) |
| 3904 | { |
| 3905 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
| 3906 | |
| 3907 | nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING); |
| 3908 | pci_set_drvdata(pdev, NULL); |
| 3909 | |
| 3910 | if (!pci_device_is_present(pdev)) { |
| 3911 | nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DEAD); |
| 3912 | nvme_dev_disable(dev, true); |
| 3913 | } |
| 3914 | |
| 3915 | flush_work(&dev->ctrl.reset_work); |
| 3916 | nvme_stop_ctrl(&dev->ctrl); |
| 3917 | nvme_remove_namespaces(&dev->ctrl); |
| 3918 | nvme_dev_disable(dev, true); |
| 3919 | nvme_free_host_mem(dev); |
| 3920 | nvme_dev_remove_admin(dev); |
| 3921 | nvme_dbbuf_dma_free(dev); |
| 3922 | nvme_free_queues(dev, 0); |
| 3923 | mempool_destroy(dev->dmavec_mempool); |
| 3924 | nvme_release_descriptor_pools(dev); |
| 3925 | nvme_dev_unmap(dev); |
| 3926 | nvme_uninit_ctrl(&dev->ctrl); |
| 3927 | } |
| 3928 | |
| 3929 | #ifdef CONFIG_PM_SLEEP |
| 3930 | static int nvme_get_power_state(struct nvme_ctrl *ctrl, u32 *ps) |
| 3931 | { |
| 3932 | return nvme_get_features(ctrl, NVME_FEAT_POWER_MGMT, 0, NULL, 0, ps); |
| 3933 | } |
| 3934 | |
| 3935 | static int nvme_set_power_state(struct nvme_ctrl *ctrl, u32 ps) |
| 3936 | { |
| 3937 | return nvme_set_features(ctrl, NVME_FEAT_POWER_MGMT, ps, NULL, 0, NULL); |
| 3938 | } |
| 3939 | |
| 3940 | static int nvme_resume(struct device *dev) |
| 3941 | { |
| 3942 | struct nvme_dev *ndev = pci_get_drvdata(to_pci_dev(dev)); |
| 3943 | struct nvme_ctrl *ctrl = &ndev->ctrl; |
| 3944 | |
| 3945 | if (ndev->last_ps == U32_MAX || |
| 3946 | nvme_set_power_state(ctrl, ndev->last_ps) != 0) |
| 3947 | goto reset; |
| 3948 | if (ctrl->hmpre && nvme_setup_host_mem(ndev)) |
| 3949 | goto reset; |
| 3950 | |
| 3951 | return 0; |
| 3952 | reset: |
| 3953 | return nvme_try_sched_reset(ctrl); |
| 3954 | } |
| 3955 | |
| 3956 | static int nvme_suspend(struct device *dev) |
| 3957 | { |
| 3958 | struct pci_dev *pdev = to_pci_dev(dev); |
| 3959 | struct nvme_dev *ndev = pci_get_drvdata(pdev); |
| 3960 | struct nvme_ctrl *ctrl = &ndev->ctrl; |
| 3961 | int ret = -EBUSY; |
| 3962 | |
| 3963 | ndev->last_ps = U32_MAX; |
| 3964 | |
| 3965 | /* |
| 3966 | * The platform does not remove power for a kernel managed suspend so |
| 3967 | * use host managed nvme power settings for lowest idle power if |
| 3968 | * possible. This should have quicker resume latency than a full device |
| 3969 | * shutdown. But if the firmware is involved after the suspend or the |
| 3970 | * platform has any limitation in waking from low power states or the |
| 3971 | * device does not support any non-default power states, shut down the |
| 3972 | * device fully. |
| 3973 | * |
| 3974 | * If ASPM is not enabled for the device, shut down the device and allow |
| 3975 | * the PCI bus layer to put it into D3 in order to take the PCIe link |
| 3976 | * down, so as to allow the platform to achieve its minimum low-power |
| 3977 | * state (which may not be possible if the link is up). |
| 3978 | */ |
| 3979 | if (!pci_suspend_retains_context(pdev) || !ctrl->npss || |
| 3980 | !pcie_aspm_enabled(pdev) || |
| 3981 | (ndev->ctrl.quirks & NVME_QUIRK_SIMPLE_SUSPEND)) |
| 3982 | return nvme_disable_prepare_reset(ndev, true); |
| 3983 | |
| 3984 | nvme_start_freeze(ctrl); |
| 3985 | nvme_wait_freeze(ctrl); |
| 3986 | nvme_sync_queues(ctrl); |
| 3987 | |
| 3988 | if (nvme_ctrl_state(ctrl) != NVME_CTRL_LIVE) |
| 3989 | goto unfreeze; |
| 3990 | |
| 3991 | /* |
| 3992 | * Host memory access may not be successful in a system suspend state, |
| 3993 | * but the specification allows the controller to access memory in a |
| 3994 | * non-operational power state. |
| 3995 | */ |
| 3996 | if (ndev->hmb) { |
| 3997 | ret = nvme_set_host_mem(ndev, 0); |
| 3998 | if (ret < 0) |
| 3999 | goto unfreeze; |
| 4000 | } |
| 4001 | |
| 4002 | ret = nvme_get_power_state(ctrl, &ndev->last_ps); |
| 4003 | if (ret < 0) |
| 4004 | goto unfreeze; |
| 4005 | |
| 4006 | /* |
| 4007 | * A saved state prevents pci pm from generically controlling the |
| 4008 | * device's power. If we're using protocol specific settings, we don't |
| 4009 | * want pci interfering. |
| 4010 | */ |
| 4011 | pci_save_state(pdev); |
| 4012 | |
| 4013 | ret = nvme_set_power_state(ctrl, ctrl->npss); |
| 4014 | if (ret < 0) |
| 4015 | goto unfreeze; |
| 4016 | |
| 4017 | if (ret) { |
| 4018 | /* discard the saved state */ |
| 4019 | pci_load_saved_state(pdev, NULL); |
| 4020 | |
| 4021 | /* |
| 4022 | * Clearing npss forces a controller reset on resume. The |
| 4023 | * correct value will be rediscovered then. |
| 4024 | */ |
| 4025 | ret = nvme_disable_prepare_reset(ndev, true); |
| 4026 | ctrl->npss = 0; |
| 4027 | } |
| 4028 | unfreeze: |
| 4029 | nvme_unfreeze(ctrl); |
| 4030 | return ret; |
| 4031 | } |
| 4032 | |
| 4033 | static int nvme_simple_suspend(struct device *dev) |
| 4034 | { |
| 4035 | struct nvme_dev *ndev = pci_get_drvdata(to_pci_dev(dev)); |
| 4036 | |
| 4037 | return nvme_disable_prepare_reset(ndev, true); |
| 4038 | } |
| 4039 | |
| 4040 | static int nvme_simple_resume(struct device *dev) |
| 4041 | { |
| 4042 | struct pci_dev *pdev = to_pci_dev(dev); |
| 4043 | struct nvme_dev *ndev = pci_get_drvdata(pdev); |
| 4044 | |
| 4045 | return nvme_try_sched_reset(&ndev->ctrl); |
| 4046 | } |
| 4047 | |
| 4048 | static const struct dev_pm_ops nvme_dev_pm_ops = { |
| 4049 | .suspend = nvme_suspend, |
| 4050 | .resume = nvme_resume, |
| 4051 | .freeze = nvme_simple_suspend, |
| 4052 | .thaw = nvme_simple_resume, |
| 4053 | .poweroff = nvme_simple_suspend, |
| 4054 | .restore = nvme_simple_resume, |
| 4055 | }; |
| 4056 | #endif /* CONFIG_PM_SLEEP */ |
| 4057 | |
| 4058 | static pci_ers_result_t nvme_error_detected(struct pci_dev *pdev, |
| 4059 | pci_channel_state_t state) |
| 4060 | { |
| 4061 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
| 4062 | |
| 4063 | /* |
| 4064 | * A frozen channel requires a reset. When detected, this method will |
| 4065 | * shutdown the controller to quiesce. The controller will be restarted |
| 4066 | * after the slot reset through driver's slot_reset callback. |
| 4067 | */ |
| 4068 | switch (state) { |
| 4069 | case pci_channel_io_normal: |
| 4070 | return PCI_ERS_RESULT_CAN_RECOVER; |
| 4071 | case pci_channel_io_frozen: |
| 4072 | dev_warn(dev->ctrl.device, |
| 4073 | "frozen state error detected, reset controller\n"); |
| 4074 | if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_RESETTING)) { |
| 4075 | nvme_dev_disable(dev, true); |
| 4076 | return PCI_ERS_RESULT_DISCONNECT; |
| 4077 | } |
| 4078 | nvme_dev_disable(dev, false); |
| 4079 | return PCI_ERS_RESULT_NEED_RESET; |
| 4080 | case pci_channel_io_perm_failure: |
| 4081 | dev_warn(dev->ctrl.device, |
| 4082 | "failure state error detected, request disconnect\n"); |
| 4083 | return PCI_ERS_RESULT_DISCONNECT; |
| 4084 | } |
| 4085 | return PCI_ERS_RESULT_NEED_RESET; |
| 4086 | } |
| 4087 | |
| 4088 | static pci_ers_result_t nvme_slot_reset(struct pci_dev *pdev) |
| 4089 | { |
| 4090 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
| 4091 | |
| 4092 | dev_info(dev->ctrl.device, "restart after slot reset\n"); |
| 4093 | pci_restore_state(pdev); |
| 4094 | if (nvme_try_sched_reset(&dev->ctrl)) |
| 4095 | nvme_unquiesce_io_queues(&dev->ctrl); |
| 4096 | return PCI_ERS_RESULT_RECOVERED; |
| 4097 | } |
| 4098 | |
| 4099 | static void nvme_error_resume(struct pci_dev *pdev) |
| 4100 | { |
| 4101 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
| 4102 | |
| 4103 | flush_work(&dev->ctrl.reset_work); |
| 4104 | } |
| 4105 | |
| 4106 | static const struct pci_error_handlers nvme_err_handler = { |
| 4107 | .error_detected = nvme_error_detected, |
| 4108 | .slot_reset = nvme_slot_reset, |
| 4109 | .resume = nvme_error_resume, |
| 4110 | .reset_prepare = nvme_reset_prepare, |
| 4111 | .reset_done = nvme_reset_done, |
| 4112 | }; |
| 4113 | |
| 4114 | static const struct pci_device_id nvme_id_table[] = { |
| 4115 | { PCI_VDEVICE(INTEL, 0x0953), /* Intel 750/P3500/P3600/P3700 */ |
| 4116 | .driver_data = NVME_QUIRK_STRIPE_SIZE | |
| 4117 | NVME_QUIRK_DEALLOCATE_ZEROES, }, |
| 4118 | { PCI_VDEVICE(INTEL, 0x0a53), /* Intel P3520 */ |
| 4119 | .driver_data = NVME_QUIRK_STRIPE_SIZE | |
| 4120 | NVME_QUIRK_DEALLOCATE_ZEROES, }, |
| 4121 | { PCI_VDEVICE(INTEL, 0x0a54), /* Intel P4500/P4600 */ |
| 4122 | .driver_data = NVME_QUIRK_STRIPE_SIZE | |
| 4123 | NVME_QUIRK_IGNORE_DEV_SUBNQN | |
| 4124 | NVME_QUIRK_BOGUS_NID, }, |
| 4125 | { PCI_VDEVICE(INTEL, 0x0a55), /* Dell Express Flash P4600 */ |
| 4126 | .driver_data = NVME_QUIRK_STRIPE_SIZE, }, |
| 4127 | { PCI_VDEVICE(INTEL, 0xf1a5), /* Intel 600P/P3100 */ |
| 4128 | .driver_data = NVME_QUIRK_NO_DEEPEST_PS | |
| 4129 | NVME_QUIRK_MEDIUM_PRIO_SQ | |
| 4130 | NVME_QUIRK_NO_TEMP_THRESH_CHANGE | |
| 4131 | NVME_QUIRK_DISABLE_WRITE_ZEROES, }, |
| 4132 | { PCI_VDEVICE(INTEL, 0xf1a6), /* Intel 760p/Pro 7600p */ |
| 4133 | .driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN, }, |
| 4134 | { PCI_VDEVICE(INTEL, 0x5845), /* Qemu emulated controller */ |
| 4135 | .driver_data = NVME_QUIRK_IDENTIFY_CNS | |
| 4136 | NVME_QUIRK_DISABLE_WRITE_ZEROES | |
| 4137 | NVME_QUIRK_BOGUS_NID, }, |
| 4138 | { PCI_VDEVICE(REDHAT, 0x0010), /* Qemu emulated controller */ |
| 4139 | .driver_data = NVME_QUIRK_BOGUS_NID, }, |
| 4140 | { PCI_DEVICE(0x1217, 0x8760), /* O2 Micro 64GB Steam Deck */ |
| 4141 | .driver_data = NVME_QUIRK_DMAPOOL_ALIGN_512, }, |
| 4142 | { PCI_DEVICE(0x126f, 0x1001), /* Silicon Motion generic */ |
| 4143 | .driver_data = NVME_QUIRK_NO_DEEPEST_PS | |
| 4144 | NVME_QUIRK_IGNORE_DEV_SUBNQN, }, |
| 4145 | { PCI_DEVICE(0x126f, 0x2262), /* Silicon Motion generic */ |
| 4146 | .driver_data = NVME_QUIRK_NO_DEEPEST_PS | |
| 4147 | NVME_QUIRK_BOGUS_NID, }, |
| 4148 | { PCI_DEVICE(0x126f, 0x2263), /* Silicon Motion unidentified */ |
| 4149 | .driver_data = NVME_QUIRK_NO_NS_DESC_LIST | |
| 4150 | NVME_QUIRK_BOGUS_NID, }, |
| 4151 | { PCI_DEVICE(0x1bb1, 0x0100), /* Seagate Nytro Flash Storage */ |
| 4152 | .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY | |
| 4153 | NVME_QUIRK_NO_NS_DESC_LIST, }, |
| 4154 | { PCI_DEVICE(0x1c58, 0x0003), /* HGST adapter */ |
| 4155 | .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, }, |
| 4156 | { PCI_DEVICE(0x1c58, 0x0023), /* WDC SN200 adapter */ |
| 4157 | .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, }, |
| 4158 | { PCI_DEVICE(0x1c5f, 0x0540), /* Memblaze Pblaze4 adapter */ |
| 4159 | .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, }, |
| 4160 | { PCI_DEVICE(0x1c5f, 0x0555), /* Memblaze Pblaze5 adapter */ |
| 4161 | .driver_data = NVME_QUIRK_NO_NS_DESC_LIST, }, |
| 4162 | { PCI_DEVICE(0x144d, 0xa821), /* Samsung PM1725 */ |
| 4163 | .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, }, |
| 4164 | { PCI_DEVICE(0x144d, 0xa822), /* Samsung PM1725a */ |
| 4165 | .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY | |
| 4166 | NVME_QUIRK_DISABLE_WRITE_ZEROES| |
| 4167 | NVME_QUIRK_IGNORE_DEV_SUBNQN, }, |
| 4168 | { PCI_DEVICE(0x15b7, 0x5008), /* Sandisk SN530 */ |
| 4169 | .driver_data = NVME_QUIRK_BROKEN_MSI }, |
| 4170 | { PCI_DEVICE(0x15b7, 0x5009), /* Sandisk SN550 */ |
| 4171 | .driver_data = NVME_QUIRK_BROKEN_MSI | |
| 4172 | NVME_QUIRK_NO_DEEPEST_PS }, |
| 4173 | { PCI_DEVICE(0x1987, 0x5012), /* Phison E12 */ |
| 4174 | .driver_data = NVME_QUIRK_BOGUS_NID, }, |
| 4175 | { PCI_DEVICE(0x1987, 0x5016), /* Phison E16 */ |
| 4176 | .driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN | |
| 4177 | NVME_QUIRK_BOGUS_NID, }, |
| 4178 | { PCI_DEVICE(0x1987, 0x5019), /* phison E19 */ |
| 4179 | .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, }, |
| 4180 | { PCI_DEVICE(0x1987, 0x5021), /* Phison E21 */ |
| 4181 | .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, }, |
| 4182 | { PCI_DEVICE(0x1b4b, 0x1092), /* Lexar 256 GB SSD */ |
| 4183 | .driver_data = NVME_QUIRK_NO_NS_DESC_LIST | |
| 4184 | NVME_QUIRK_IGNORE_DEV_SUBNQN, }, |
| 4185 | { PCI_DEVICE(0x1cc1, 0x33f8), /* ADATA IM2P33F8ABR1 1 TB */ |
| 4186 | .driver_data = NVME_QUIRK_BOGUS_NID, }, |
| 4187 | { PCI_DEVICE(0x10ec, 0x5762), /* ADATA SX6000LNP */ |
| 4188 | .driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN | |
| 4189 | NVME_QUIRK_BOGUS_NID, }, |
| 4190 | { PCI_DEVICE(0x10ec, 0x5763), /* ADATA SX6000PNP */ |
| 4191 | .driver_data = NVME_QUIRK_BOGUS_NID, }, |
| 4192 | { PCI_DEVICE(0x1cc1, 0x8201), /* ADATA SX8200PNP 512GB */ |
| 4193 | .driver_data = NVME_QUIRK_NO_DEEPEST_PS | |
| 4194 | NVME_QUIRK_IGNORE_DEV_SUBNQN, }, |
| 4195 | { PCI_DEVICE(0x1344, 0x5407), /* Micron Technology Inc NVMe SSD */ |
| 4196 | .driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN }, |
| 4197 | { PCI_DEVICE(0x1344, 0x6001), /* Micron Nitro NVMe */ |
| 4198 | .driver_data = NVME_QUIRK_BOGUS_NID, }, |
| 4199 | { PCI_DEVICE(0x1344, 0x6004), /* Micron 4100AT */ |
| 4200 | .driver_data = NVME_QUIRK_DMAPOOL_ALIGN_512, }, |
| 4201 | { PCI_DEVICE(0x1c5c, 0x1504), /* SK Hynix PC400 */ |
| 4202 | .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, }, |
| 4203 | { PCI_DEVICE(0x1c5c, 0x174a), /* SK Hynix P31 SSD */ |
| 4204 | .driver_data = NVME_QUIRK_BOGUS_NID, }, |
| 4205 | { PCI_DEVICE(0x1c5c, 0x1D59), /* SK Hynix BC901 */ |
| 4206 | .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, }, |
| 4207 | { PCI_DEVICE(0x15b7, 0x2001), /* Sandisk Skyhawk */ |
| 4208 | .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, }, |
| 4209 | { PCI_DEVICE(0x1d97, 0x2263), /* SPCC */ |
| 4210 | .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, }, |
| 4211 | { PCI_DEVICE(0x144d, 0xa80b), /* Samsung PM9B1 256G and 512G */ |
| 4212 | .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES | |
| 4213 | NVME_QUIRK_BOGUS_NID, }, |
| 4214 | { PCI_DEVICE(0x144d, 0xa809), /* Samsung MZALQ256HBJD 256G */ |
| 4215 | .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, }, |
| 4216 | { PCI_DEVICE(0x144d, 0xa802), /* Samsung SM953 */ |
| 4217 | .driver_data = NVME_QUIRK_BOGUS_NID, }, |
| 4218 | { PCI_DEVICE(0x1cc4, 0x6303), /* UMIS RPJTJ512MGE1QDY 512G */ |
| 4219 | .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, }, |
| 4220 | { PCI_DEVICE(0x1cc4, 0x6302), /* UMIS RPJTJ256MGE1QDY 256G */ |
| 4221 | .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, }, |
| 4222 | { PCI_DEVICE(0x2646, 0x2262), /* KINGSTON SKC2000 NVMe SSD */ |
| 4223 | .driver_data = NVME_QUIRK_NO_DEEPEST_PS, }, |
| 4224 | { PCI_DEVICE(0x2646, 0x2263), /* KINGSTON A2000 NVMe SSD */ |
| 4225 | .driver_data = NVME_QUIRK_NO_DEEPEST_PS, }, |
| 4226 | { PCI_DEVICE(0x2646, 0x5013), /* Kingston KC3000, Kingston FURY Renegade */ |
| 4227 | .driver_data = NVME_QUIRK_NO_SECONDARY_TEMP_THRESH, }, |
| 4228 | { PCI_DEVICE(0x2646, 0x5018), /* KINGSTON OM8SFP4xxxxP OS21012 NVMe SSD */ |
| 4229 | .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, }, |
| 4230 | { PCI_DEVICE(0x2646, 0x5016), /* KINGSTON OM3PGP4xxxxP OS21011 NVMe SSD */ |
| 4231 | .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, }, |
| 4232 | { PCI_DEVICE(0x2646, 0x501A), /* KINGSTON OM8PGP4xxxxP OS21005 NVMe SSD */ |
| 4233 | .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, }, |
| 4234 | { PCI_DEVICE(0x2646, 0x501B), /* KINGSTON OM8PGP4xxxxQ OS21005 NVMe SSD */ |
| 4235 | .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, }, |
| 4236 | { PCI_DEVICE(0x2646, 0x501E), /* KINGSTON OM3PGP4xxxxQ OS21011 NVMe SSD */ |
| 4237 | .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, }, |
| 4238 | { PCI_DEVICE(0x2646, 0x502F), /* KINGSTON OM3SGP4xxxxK NVMe SSD */ |
| 4239 | .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, }, |
| 4240 | { PCI_DEVICE(0x1f40, 0x1202), /* Netac Technologies Co. NV3000 NVMe SSD */ |
| 4241 | .driver_data = NVME_QUIRK_BOGUS_NID, }, |
| 4242 | { PCI_DEVICE(0x1f40, 0x5236), /* Netac Technologies Co. NV7000 NVMe SSD */ |
| 4243 | .driver_data = NVME_QUIRK_BOGUS_NID, }, |
| 4244 | { PCI_DEVICE(0x1e4B, 0x1001), /* MAXIO MAP1001 */ |
| 4245 | .driver_data = NVME_QUIRK_BOGUS_NID, }, |
| 4246 | { PCI_DEVICE(0x1e4B, 0x1002), /* MAXIO MAP1002 */ |
| 4247 | .driver_data = NVME_QUIRK_BOGUS_NID, }, |
| 4248 | { PCI_DEVICE(0x1e4B, 0x1202), /* MAXIO MAP1202 */ |
| 4249 | .driver_data = NVME_QUIRK_BOGUS_NID, }, |
| 4250 | { PCI_DEVICE(0x1e4B, 0x1602), /* MAXIO MAP1602 */ |
| 4251 | .driver_data = NVME_QUIRK_BOGUS_NID, }, |
| 4252 | { PCI_DEVICE(0x1cc1, 0x5350), /* ADATA XPG GAMMIX S50 */ |
| 4253 | .driver_data = NVME_QUIRK_BOGUS_NID, }, |
| 4254 | { PCI_DEVICE(0x1dbe, 0x5216), /* Acer/INNOGRIT FA100/5216 NVMe SSD */ |
| 4255 | .driver_data = NVME_QUIRK_BOGUS_NID, }, |
| 4256 | { PCI_DEVICE(0x1dbe, 0x5236), /* ADATA XPG GAMMIX S70 */ |
| 4257 | .driver_data = NVME_QUIRK_BOGUS_NID, }, |
| 4258 | { PCI_DEVICE(0x1e49, 0x0021), /* ZHITAI TiPro5000 NVMe SSD */ |
| 4259 | .driver_data = NVME_QUIRK_NO_DEEPEST_PS, }, |
| 4260 | { PCI_DEVICE(0x1e49, 0x0041), /* ZHITAI TiPro7000 NVMe SSD */ |
| 4261 | .driver_data = NVME_QUIRK_NO_DEEPEST_PS, }, |
| 4262 | { PCI_DEVICE(0x1fa0, 0x2283), /* Wodposit WPBSNM8-256GTP */ |
| 4263 | .driver_data = NVME_QUIRK_NO_SECONDARY_TEMP_THRESH, }, |
| 4264 | { PCI_DEVICE(0x025e, 0xf1ac), /* SOLIDIGM P44 pro SSDPFKKW020X7 */ |
| 4265 | .driver_data = NVME_QUIRK_NO_DEEPEST_PS, }, |
| 4266 | { PCI_DEVICE(0xc0a9, 0x540a), /* Crucial P2 */ |
| 4267 | .driver_data = NVME_QUIRK_BOGUS_NID, }, |
| 4268 | { PCI_DEVICE(0x1d97, 0x2263), /* Lexar NM610 */ |
| 4269 | .driver_data = NVME_QUIRK_BOGUS_NID, }, |
| 4270 | { PCI_DEVICE(0x1d97, 0x1d97), /* Lexar NM620 */ |
| 4271 | .driver_data = NVME_QUIRK_BOGUS_NID, }, |
| 4272 | { PCI_DEVICE(0x1d97, 0x2269), /* Lexar NM760 */ |
| 4273 | .driver_data = NVME_QUIRK_BOGUS_NID | |
| 4274 | NVME_QUIRK_IGNORE_DEV_SUBNQN, }, |
| 4275 | { PCI_DEVICE(0x10ec, 0x5763), /* TEAMGROUP T-FORCE CARDEA ZERO Z330 SSD */ |
| 4276 | .driver_data = NVME_QUIRK_BOGUS_NID, }, |
| 4277 | { PCI_DEVICE(0x1e4b, 0x1602), /* HS-SSD-FUTURE 2048G */ |
| 4278 | .driver_data = NVME_QUIRK_BOGUS_NID, }, |
| 4279 | { PCI_DEVICE(0x10ec, 0x5765), /* TEAMGROUP MP33 2TB SSD */ |
| 4280 | .driver_data = NVME_QUIRK_BOGUS_NID, }, |
| 4281 | { PCI_DEVICE(PCI_VENDOR_ID_AMAZON, 0x0061), |
| 4282 | .driver_data = NVME_QUIRK_DMA_ADDRESS_BITS_48, }, |
| 4283 | { PCI_DEVICE(PCI_VENDOR_ID_AMAZON, 0x0065), |
| 4284 | .driver_data = NVME_QUIRK_DMA_ADDRESS_BITS_48, }, |
| 4285 | { PCI_DEVICE(PCI_VENDOR_ID_AMAZON, 0x8061), |
| 4286 | .driver_data = NVME_QUIRK_DMA_ADDRESS_BITS_48, }, |
| 4287 | { PCI_DEVICE(PCI_VENDOR_ID_AMAZON, 0xcd00), |
| 4288 | .driver_data = NVME_QUIRK_DMA_ADDRESS_BITS_48, }, |
| 4289 | { PCI_DEVICE(PCI_VENDOR_ID_AMAZON, 0xcd01), |
| 4290 | .driver_data = NVME_QUIRK_DMA_ADDRESS_BITS_48, }, |
| 4291 | { PCI_DEVICE(PCI_VENDOR_ID_AMAZON, 0xcd02), |
| 4292 | .driver_data = NVME_QUIRK_DMA_ADDRESS_BITS_48, }, |
| 4293 | { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001), |
| 4294 | /* |
| 4295 | * Fix for the Apple controller found in the MacBook8,1 and |
| 4296 | * some MacBook7,1 to avoid controller resets and data loss. |
| 4297 | */ |
| 4298 | .driver_data = NVME_QUIRK_SINGLE_VECTOR | |
| 4299 | NVME_QUIRK_QDEPTH_ONE }, |
| 4300 | { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2003) }, |
| 4301 | { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2005), |
| 4302 | .driver_data = NVME_QUIRK_SINGLE_VECTOR | |
| 4303 | NVME_QUIRK_128_BYTES_SQES | |
| 4304 | NVME_QUIRK_SHARED_TAGS | |
| 4305 | NVME_QUIRK_SKIP_CID_GEN | |
| 4306 | NVME_QUIRK_IDENTIFY_CNS }, |
| 4307 | { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) }, |
| 4308 | { 0, } |
| 4309 | }; |
| 4310 | MODULE_DEVICE_TABLE(pci, nvme_id_table); |
| 4311 | |
| 4312 | static struct pci_driver nvme_driver = { |
| 4313 | .name = "nvme", |
| 4314 | .id_table = nvme_id_table, |
| 4315 | .probe = nvme_probe, |
| 4316 | .remove = nvme_remove, |
| 4317 | .shutdown = nvme_shutdown, |
| 4318 | .driver = { |
| 4319 | .probe_type = PROBE_PREFER_ASYNCHRONOUS, |
| 4320 | #ifdef CONFIG_PM_SLEEP |
| 4321 | .pm = &nvme_dev_pm_ops, |
| 4322 | #endif |
| 4323 | }, |
| 4324 | .sriov_configure = pci_sriov_configure_simple, |
| 4325 | .err_handler = &nvme_err_handler, |
| 4326 | }; |
| 4327 | |
| 4328 | static int __init nvme_init(void) |
| 4329 | { |
| 4330 | BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64); |
| 4331 | BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64); |
| 4332 | BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64); |
| 4333 | BUILD_BUG_ON(IRQ_AFFINITY_MAX_SETS < 2); |
| 4334 | |
| 4335 | return pci_register_driver(&nvme_driver); |
| 4336 | } |
| 4337 | |
| 4338 | static void __exit nvme_exit(void) |
| 4339 | { |
| 4340 | kfree(nvme_pci_quirk_list); |
| 4341 | pci_unregister_driver(&nvme_driver); |
| 4342 | flush_workqueue(nvme_wq); |
| 4343 | } |
| 4344 | |
| 4345 | MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>"); |
| 4346 | MODULE_LICENSE("GPL"); |
| 4347 | MODULE_VERSION("1.0"); |
| 4348 | MODULE_DESCRIPTION("NVMe host PCIe transport driver"); |
| 4349 | module_init(nvme_init); |
| 4350 | module_exit(nvme_exit); |