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