blob: c194f9f9745063a4ff23705cdc95e809ada38db1 [file] [log] [blame]
Julian Stecklinac83e15b2017-02-13 10:03:59 +01001// Low level NVMe disk access
2//
3// Copyright 2017 Amazon.com, Inc. or its affiliates.
4//
5// This file may be distributed under the terms of the GNU LGPLv3 license.
6
7#include "blockcmd.h"
8#include "fw/paravirt.h" // runningOnQEMU
9#include "malloc.h" // malloc_high
10#include "output.h" // dprintf
11#include "pci.h"
12#include "pci_ids.h" // PCI_CLASS_STORAGE_NVME
13#include "pci_regs.h" // PCI_BASE_ADDRESS_0
14#include "pcidevice.h" // foreachpci
15#include "stacks.h" // yield
16#include "std/disk.h" // DISK_RET_
17#include "string.h" // memset
18#include "util.h" // boot_add_hd
19#include "x86.h" // readl
20
21#include "nvme.h"
22#include "nvme-int.h"
23
24static void *
25zalloc_page_aligned(struct zone_s *zone, u32 size)
26{
27 void *res = _malloc(zone, size, NVME_PAGE_SIZE);
28 if (res) memset(res, 0, size);
29 return res;
30}
31
32static void
33nvme_init_queue_common(struct nvme_ctrl *ctrl, struct nvme_queue *q, u16 q_idx,
34 u16 length)
35{
36 memset(q, 0, sizeof(*q));
37 q->dbl = (u32 *)((char *)ctrl->reg + 0x1000 + q_idx * ctrl->doorbell_stride);
38 dprintf(3, " q %p q_idx %u dbl %p\n", q, q_idx, q->dbl);
39 q->mask = length - 1;
40}
41
42static void
43nvme_init_sq(struct nvme_ctrl *ctrl, struct nvme_sq *sq, u16 q_idx, u16 length,
44 struct nvme_cq *cq)
45{
46 nvme_init_queue_common(ctrl, &sq->common, q_idx, length);
47 sq->sqe = zalloc_page_aligned(&ZoneHigh, sizeof(*sq->sqe) * length);
48 dprintf(3, "sq %p q_idx %u sqe %p\n", sq, q_idx, sq->sqe);
49 sq->cq = cq;
50 sq->head = 0;
51 sq->tail = 0;
52}
53
54static void
55nvme_init_cq(struct nvme_ctrl *ctrl, struct nvme_cq *cq, u16 q_idx, u16 length)
56{
57 nvme_init_queue_common(ctrl, &cq->common, q_idx, length);
58 cq->cqe = zalloc_page_aligned(&ZoneHigh, sizeof(*cq->cqe) * length);
59
60 cq->head = 0;
61
62 /* All CQE phase bits are initialized to zero. This means initially we wait
63 for the host controller to set these to 1. */
64 cq->phase = 1;
65}
66
67static int
68nvme_poll_cq(struct nvme_cq *cq)
69{
70 u32 dw3 = readl(&cq->cqe[cq->head].dword[3]);
71 return (!!(dw3 & NVME_CQE_DW3_P) == cq->phase);
72}
73
74static int
75nvme_is_cqe_success(struct nvme_cqe const *cqe)
76{
77 return (cqe->status & 0xFF) >> 1 == 0;
78}
79
80
81static struct nvme_cqe
82nvme_error_cqe(void)
83{
84 struct nvme_cqe r;
85
86 /* 0xFF is a vendor specific status code != success. Should be okay for
87 indicating failure. */
88 memset(&r, 0xFF, sizeof(r));
89 return r;
90}
91
92static struct nvme_cqe
93nvme_consume_cqe(struct nvme_sq *sq)
94{
95 struct nvme_cq *cq = sq->cq;
96
97 if (!nvme_poll_cq(cq)) {
98 /* Cannot consume a completion queue entry, if there is none ready. */
99 return nvme_error_cqe();
100 }
101
102 struct nvme_cqe *cqe = &cq->cqe[cq->head];
103 u16 cq_next_head = (cq->head + 1) & cq->common.mask;
104 dprintf(4, "cq %p head %u -> %u\n", cq, cq->head, cq_next_head);
105 if (cq_next_head < cq->head) {
106 dprintf(3, "cq %p wrap\n", cq);
107 cq->phase = ~cq->phase;
108 }
109 cq->head = cq_next_head;
110
111 /* Update the submission queue head. */
112 if (cqe->sq_head != sq->head) {
113 sq->head = cqe->sq_head;
114 dprintf(4, "sq %p advanced to %u\n", sq, cqe->sq_head);
115 }
116
117 /* Tell the controller that we consumed the completion. */
118 writel(cq->common.dbl, cq->head);
119
120 return *cqe;
121}
122
123static struct nvme_cqe
124nvme_wait(struct nvme_sq *sq)
125{
Daniel Verkamp21de72f2017-02-23 23:27:54 -0700126 static const unsigned nvme_timeout = 5000 /* ms */;
Julian Stecklinac83e15b2017-02-13 10:03:59 +0100127 u32 to = timer_calc(nvme_timeout);
128 while (!nvme_poll_cq(sq->cq)) {
129 yield();
130
131 if (timer_check(to)) {
132 warn_timeout();
133 return nvme_error_cqe();
134 }
135 }
136
137 return nvme_consume_cqe(sq);
138}
139
140/* Returns the next submission queue entry (or NULL if the queue is full). It
141 also fills out Command Dword 0 and clears the rest. */
142static struct nvme_sqe *
143nvme_get_next_sqe(struct nvme_sq *sq, u8 opc, void *metadata, void *data)
144{
145 if (((sq->head + 1) & sq->common.mask) == sq->tail) {
146 dprintf(3, "submission queue is full");
147 return NULL;
148 }
149
150 struct nvme_sqe *sqe = &sq->sqe[sq->tail];
151 dprintf(4, "sq %p next_sqe %u\n", sq, sq->tail);
152
153 memset(sqe, 0, sizeof(*sqe));
154 sqe->cdw0 = opc | (sq->tail << 16 /* CID */);
155 sqe->mptr = (u32)metadata;
156 sqe->dptr_prp1 = (u32)data;
157
158 if (sqe->dptr_prp1 & (NVME_PAGE_SIZE - 1)) {
159 /* Data buffer not page aligned. */
160 warn_internalerror();
161 }
162
163 return sqe;
164}
165
166/* Call this after you've filled out an sqe that you've got from nvme_get_next_sqe. */
167static void
168nvme_commit_sqe(struct nvme_sq *sq)
169{
170 dprintf(4, "sq %p commit_sqe %u\n", sq, sq->tail);
171 sq->tail = (sq->tail + 1) & sq->common.mask;
172 writel(sq->common.dbl, sq->tail);
173}
174
175/* Perform an identify command on the admin queue and return the resulting
176 buffer. This may be a NULL pointer, if something failed. This function
177 cannot be used after initialization, because it uses buffers in tmp zone. */
178static union nvme_identify *
179nvme_admin_identify(struct nvme_ctrl *ctrl, u8 cns, u32 nsid)
180{
181 union nvme_identify *identify_buf = zalloc_page_aligned(&ZoneTmpHigh, 4096);
182 if (!identify_buf) {
183 /* Could not allocate identify buffer. */
184 warn_internalerror();
185 return NULL;
186 }
187
188 struct nvme_sqe *cmd_identify;
189 cmd_identify = nvme_get_next_sqe(&ctrl->admin_sq,
190 NVME_SQE_OPC_ADMIN_IDENTIFY, NULL,
191 identify_buf);
192
193 if (!cmd_identify) {
194 warn_internalerror();
195 goto error;
196 }
197
198 cmd_identify->nsid = nsid;
199 cmd_identify->dword[10] = cns;
200
201 nvme_commit_sqe(&ctrl->admin_sq);
202
203 struct nvme_cqe cqe = nvme_wait(&ctrl->admin_sq);
204
205 if (!nvme_is_cqe_success(&cqe)) {
206 goto error;
207 }
208
209 return identify_buf;
210 error:
211 free(identify_buf);
212 return NULL;
213}
214
215static struct nvme_identify_ctrl *
216nvme_admin_identify_ctrl(struct nvme_ctrl *ctrl)
217{
218 return &nvme_admin_identify(ctrl, NVME_ADMIN_IDENTIFY_CNS_ID_CTRL, 0)->ctrl;
219}
220
Julian Stecklinac83e15b2017-02-13 10:03:59 +0100221static struct nvme_identify_ns *
222nvme_admin_identify_ns(struct nvme_ctrl *ctrl, u32 ns_id)
223{
224 return &nvme_admin_identify(ctrl, NVME_ADMIN_IDENTIFY_CNS_ID_NS,
225 ns_id)->ns;
226}
227
228static void
229nvme_probe_ns(struct nvme_ctrl *ctrl, struct nvme_namespace *ns, u32 ns_id)
230{
231 ns->ctrl = ctrl;
232 ns->ns_id = ns_id;
233
234 struct nvme_identify_ns *id = nvme_admin_identify_ns(ctrl, ns_id);
235 if (!id) {
236 dprintf(2, "NVMe couldn't identify namespace %u.\n", ns_id);
237 goto free_buffer;
238 }
239
240 u8 current_lba_format = id->flbas & 0xF;
241 if (current_lba_format > id->nlbaf) {
242 dprintf(2, "NVMe NS %u: current LBA format %u is beyond what the "
243 " namespace supports (%u)?\n",
244 ns_id, current_lba_format, id->nlbaf + 1);
245 goto free_buffer;
246 }
247
248 ns->lba_count = id->nsze;
Daniel Verkampf21e3042017-02-23 23:27:53 -0700249 if (!ns->lba_count) {
250 dprintf(2, "NVMe NS %u is inactive.\n", ns_id);
251 goto free_buffer;
252 }
Julian Stecklinac83e15b2017-02-13 10:03:59 +0100253
254 struct nvme_lba_format *fmt = &id->lbaf[current_lba_format];
255
256 ns->block_size = 1U << fmt->lbads;
257 ns->metadata_size = fmt->ms;
258
259 if (ns->block_size > NVME_PAGE_SIZE) {
260 /* If we see devices that trigger this path, we need to increase our
261 buffer size. */
262 warn_internalerror();
263 goto free_buffer;
264 }
265
266 ns->drive.cntl_id = ns - ctrl->ns;
267 ns->drive.removable = 0;
268 ns->drive.type = DTYPE_NVME;
269 ns->drive.blksize = ns->block_size;
270 ns->drive.sectors = ns->lba_count;
271
272 ns->dma_buffer = zalloc_page_aligned(&ZoneHigh, NVME_PAGE_SIZE);
273
274 char *desc = znprintf(MAXDESCSIZE, "NVMe NS %u: %llu MiB (%llu %u-byte "
275 "blocks + %u-byte metadata)\n",
276 ns_id, (ns->lba_count * ns->block_size) >> 20,
277 ns->lba_count, ns->block_size, ns->metadata_size);
278
279 dprintf(3, "%s", desc);
280 boot_add_hd(&ns->drive, desc, bootprio_find_pci_device(ctrl->pci));
281
282 free_buffer:
283 free (id);
284 }
285
286/* Returns 0 on success. */
287static int
288nvme_create_io_cq(struct nvme_ctrl *ctrl, struct nvme_cq *cq, u16 q_idx)
289{
290 struct nvme_sqe *cmd_create_cq;
291
292 nvme_init_cq(ctrl, cq, q_idx, NVME_PAGE_SIZE / sizeof(struct nvme_cqe));
293 cmd_create_cq = nvme_get_next_sqe(&ctrl->admin_sq,
294 NVME_SQE_OPC_ADMIN_CREATE_IO_CQ, NULL,
295 cq->cqe);
296 if (!cmd_create_cq) {
297 return -1;
298 }
299
300 cmd_create_cq->dword[10] = (cq->common.mask << 16) | (q_idx >> 1);
301 cmd_create_cq->dword[11] = 1 /* physically contiguous */;
302
303 nvme_commit_sqe(&ctrl->admin_sq);
304
305 struct nvme_cqe cqe = nvme_wait(&ctrl->admin_sq);
306
307 if (!nvme_is_cqe_success(&cqe)) {
308 dprintf(2, "create io cq failed: %08x %08x %08x %08x\n",
309 cqe.dword[0], cqe.dword[1], cqe.dword[2], cqe.dword[3]);
310
311 return -1;
312 }
313
314 return 0;
315}
316
317/* Returns 0 on success. */
318static int
319nvme_create_io_sq(struct nvme_ctrl *ctrl, struct nvme_sq *sq, u16 q_idx, struct nvme_cq *cq)
320{
321 struct nvme_sqe *cmd_create_sq;
322
323 nvme_init_sq(ctrl, sq, q_idx, NVME_PAGE_SIZE / sizeof(struct nvme_cqe), cq);
324 cmd_create_sq = nvme_get_next_sqe(&ctrl->admin_sq,
325 NVME_SQE_OPC_ADMIN_CREATE_IO_SQ, NULL,
326 sq->sqe);
327 if (!cmd_create_sq) {
328 return -1;
329 }
330
331 cmd_create_sq->dword[10] = (sq->common.mask << 16) | (q_idx >> 1);
332 cmd_create_sq->dword[11] = (q_idx >> 1) << 16 | 1 /* contiguous */;
333 dprintf(3, "sq %p create dword10 %08x dword11 %08x\n", sq,
334 cmd_create_sq->dword[10], cmd_create_sq->dword[11]);
335
336 nvme_commit_sqe(&ctrl->admin_sq);
337
338 struct nvme_cqe cqe = nvme_wait(&ctrl->admin_sq);
339
340 if (!nvme_is_cqe_success(&cqe)) {
341 dprintf(2, "create io sq failed: %08x %08x %08x %08x\n",
342 cqe.dword[0], cqe.dword[1], cqe.dword[2], cqe.dword[3]);
343 return -1;
344 }
345
346 return 0;
347}
348
349/* Reads count sectors into buf. Returns DISK_RET_*. The buffer cannot cross
350 page boundaries. */
351static int
352nvme_io_readwrite(struct nvme_namespace *ns, u64 lba, char *buf, u16 count,
353 int write)
354{
355 u32 buf_addr = (u32)buf;
356
357 if ((buf_addr & 0x3) ||
358 ((buf_addr & ~(NVME_PAGE_SIZE - 1)) !=
359 ((buf_addr + ns->block_size * count - 1) & ~(NVME_PAGE_SIZE - 1)))) {
360 /* Buffer is misaligned or crosses page boundary */
361 warn_internalerror();
362 return DISK_RET_EBADTRACK;
363 }
364
365 struct nvme_sqe *io_read = nvme_get_next_sqe(&ns->ctrl->io_sq,
366 write ? NVME_SQE_OPC_IO_WRITE
367 : NVME_SQE_OPC_IO_READ,
368 NULL, buf);
369 io_read->nsid = ns->ns_id;
370 io_read->dword[10] = (u32)lba;
371 io_read->dword[11] = (u32)(lba >> 32);
372 io_read->dword[12] = (1U << 31 /* limited retry */) | (count - 1);
373
374 nvme_commit_sqe(&ns->ctrl->io_sq);
375
376 struct nvme_cqe cqe = nvme_wait(&ns->ctrl->io_sq);
377
378 if (!nvme_is_cqe_success(&cqe)) {
379 dprintf(2, "read io: %08x %08x %08x %08x\n",
380 cqe.dword[0], cqe.dword[1], cqe.dword[2], cqe.dword[3]);
381
382 return DISK_RET_EBADTRACK;
383 }
384
385 return DISK_RET_SUCCESS;
386}
387
388
389static int
390nvme_create_io_queues(struct nvme_ctrl *ctrl)
391{
392 if (nvme_create_io_cq(ctrl, &ctrl->io_cq, 3))
393 return -1;
394
395 if (nvme_create_io_sq(ctrl, &ctrl->io_sq, 2, &ctrl->io_cq))
396 return -1;
397
398 return 0;
399}
400
401/* Waits for CSTS.RDY to match rdy. Returns 0 on success. */
402static int
403nvme_wait_csts_rdy(struct nvme_ctrl *ctrl, unsigned rdy)
404{
405 u32 const max_to = 500 /* ms */ * ((ctrl->reg->cap >> 24) & 0xFFU);
406 u32 to = timer_calc(max_to);
407 u32 csts;
408
409 while (rdy != ((csts = ctrl->reg->csts) & NVME_CSTS_RDY)) {
410 yield();
411
412 if (csts & NVME_CSTS_FATAL) {
413 dprintf(3, "NVMe fatal error during controller shutdown\n");
414 return -1;
415 }
416
417 if (timer_check(to)) {
418 warn_timeout();
419 return -1;
420 }
421 }
422
423 return 0;
424}
425
426/* Returns 0 on success. */
427static int
428nvme_controller_enable(struct nvme_ctrl *ctrl)
429{
430 pci_enable_busmaster(ctrl->pci);
431
432 /* Turn the controller off. */
433 ctrl->reg->cc = 0;
434 if (nvme_wait_csts_rdy(ctrl, 0)) {
435 dprintf(2, "NVMe fatal error during controller shutdown\n");
436 return -1;
437 }
438
439 ctrl->doorbell_stride = 4U << ((ctrl->reg->cap >> 32) & 0xF);
440
441 nvme_init_cq(ctrl, &ctrl->admin_cq, 1,
442 NVME_PAGE_SIZE / sizeof(struct nvme_cqe));
443
444 nvme_init_sq(ctrl, &ctrl->admin_sq, 0,
445 NVME_PAGE_SIZE / sizeof(struct nvme_sqe), &ctrl->admin_cq);
446
447 ctrl->reg->aqa = ctrl->admin_cq.common.mask << 16
448 | ctrl->admin_sq.common.mask;
449
450 /* Create the admin queue pair */
451 if (!ctrl->admin_sq.sqe || !ctrl->admin_cq.cqe) goto out_of_memory;
452
453 ctrl->reg->asq = (u32)ctrl->admin_sq.sqe;
454 ctrl->reg->acq = (u32)ctrl->admin_cq.cqe;
455
456 dprintf(3, " admin submission queue: %p\n", ctrl->admin_sq.sqe);
457 dprintf(3, " admin completion queue: %p\n", ctrl->admin_cq.cqe);
458
459 ctrl->reg->cc = NVME_CC_EN | (NVME_CQE_SIZE_LOG << 20)
460 | (NVME_SQE_SIZE_LOG << 16 /* IOSQES */);
461
462 if (nvme_wait_csts_rdy(ctrl, 1)) {
463 dprintf(2, "NVMe fatal error while enabling controller\n");
464 goto failed;
465 }
466 /* The admin queue is set up and the controller is ready. Let's figure out
467 what namespaces we have. */
468
469 struct nvme_identify_ctrl *identify = nvme_admin_identify_ctrl(ctrl);
470
471 if (!identify) {
472 dprintf(2, "NVMe couldn't identify controller.\n");
473 goto failed;
474 }
475
476 /* TODO Print model/serial info. */
477 dprintf(3, "NVMe has %u namespace%s.\n",
478 identify->nn, (identify->nn == 1) ? "" : "s");
479
480 ctrl->ns_count = identify->nn;
481 free(identify);
482
483 if ((ctrl->ns_count == 0) || nvme_create_io_queues(ctrl)) {
484 /* No point to continue, if the controller says it doesn't have
485 namespaces or we couldn't create I/O queues. */
486 goto failed;
487 }
488
489 ctrl->ns = malloc_fseg(sizeof(*ctrl->ns) * ctrl->ns_count);
490 if (!ctrl->ns) goto out_of_memory;
491 memset(ctrl->ns, 0, sizeof(*ctrl->ns) * ctrl->ns_count);
492
Julian Stecklinac83e15b2017-02-13 10:03:59 +0100493 /* Populate namespace IDs */
494 int ns_idx;
Daniel Verkampf21e3042017-02-23 23:27:53 -0700495 for (ns_idx = 0; ns_idx < ctrl->ns_count; ns_idx++) {
496 nvme_probe_ns(ctrl, &ctrl->ns[ns_idx], ns_idx + 1);
Julian Stecklinac83e15b2017-02-13 10:03:59 +0100497 }
498
499 dprintf(3, "NVMe initialization complete!\n");
500 return 0;
501
502 out_of_memory:
503 warn_noalloc();
504 failed:
505 free(ctrl->admin_sq.sqe);
506 free(ctrl->admin_cq.cqe);
507 free(ctrl->ns);
508 return -1;
509}
510
511/* Initialize an NVMe controller and detect its drives. */
512static void
513nvme_controller_setup(void *opaque)
514{
515 struct pci_device *pci = opaque;
516
517 struct nvme_reg volatile *reg = pci_enable_membar(pci, PCI_BASE_ADDRESS_0);
518 if (!reg)
519 return;
520
521 u32 version = reg->vs;
522 dprintf(3, "Found NVMe controller with version %u.%u.%u.\n",
523 version >> 16, (version >> 8) & 0xFF, version & 0xFF);
524 dprintf(3, " Capabilities %016llx\n", reg->cap);
525
Julian Stecklinac83e15b2017-02-13 10:03:59 +0100526 if (~reg->cap & NVME_CAP_CSS_NVME) {
527 dprintf(3, "Controller doesn't speak NVMe command set. Skipping.\n");
528 return;
529 }
530
531 struct nvme_ctrl *ctrl = malloc_high(sizeof(*ctrl));
532 if (!ctrl) {
533 warn_noalloc();
534 return;
535 }
536
537 memset(ctrl, 0, sizeof(*ctrl));
538
539 ctrl->reg = reg;
540 ctrl->pci = pci;
541
542 if (nvme_controller_enable(ctrl)) {
543 /* Initialization failed */
544 free(ctrl);
545 }
546}
547
548// Locate and init NVMe controllers
549static void
550nvme_scan(void)
551{
552 // Scan PCI bus for ATA adapters
553 struct pci_device *pci;
554
555 foreachpci(pci) {
556 if (pci->class != PCI_CLASS_STORAGE_NVME)
557 continue;
558 if (pci->prog_if != 2 /* as of NVM 1.0e */) {
559 dprintf(3, "Found incompatble NVMe: prog-if=%02x\n", pci->prog_if);
560 continue;
561 }
562
563 run_thread(nvme_controller_setup, pci);
564 }
565}
566
567static int
568nvme_cmd_readwrite(struct nvme_namespace *ns, struct disk_op_s *op, int write)
569{
570 int res = DISK_RET_SUCCESS;
571 u16 const max_blocks = NVME_PAGE_SIZE / ns->block_size;
572 u16 i;
573
574 for (i = 0; i < op->count || res != DISK_RET_SUCCESS;) {
575 u16 blocks_remaining = op->count - i;
576 u16 blocks = blocks_remaining < max_blocks ? blocks_remaining
577 : max_blocks;
578 char *op_buf = op->buf_fl + i * ns->block_size;
579
580 if (write) {
581 memcpy(ns->dma_buffer, op_buf, blocks * ns->block_size);
582 }
583
584 res = nvme_io_readwrite(ns, op->lba + i, ns->dma_buffer, blocks, write);
585 dprintf(3, "ns %u %s lba %llu+%u: %d\n", ns->ns_id, write ? "write"
586 : "read",
587 op->lba + i, blocks, res);
588
589 if (!write && res == DISK_RET_SUCCESS) {
590 memcpy(op_buf, ns->dma_buffer, blocks * ns->block_size);
591 }
592
593 i += blocks;
594 }
595
596 return res;
597}
598
599int
600nvme_process_op(struct disk_op_s *op)
601{
602 if (!CONFIG_NVME || !runningOnQEMU())
603 return DISK_RET_SUCCESS;
604
605 struct nvme_namespace *ns = container_of(op->drive_gf, struct nvme_namespace,
606 drive);
607
608 switch (op->command) {
609 case CMD_READ:
610 case CMD_WRITE:
611 return nvme_cmd_readwrite(ns, op, op->command == CMD_WRITE);
612 default:
613 return default_process_op(op);
614 }
615}
616
617void
618nvme_setup(void)
619{
620 ASSERT32FLAT();
621 if (!CONFIG_NVME || !runningOnQEMU())
622 return;
623
624 dprintf(3, "init nvme\n");
625 nvme_scan();
626}
627
628/* EOF */