1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 NXP Semiconductors
4 * Copyright (C) 2017 Bin Meng <bmeng.cn@gmail.com>
12 #include <dm/device-internal.h>
15 #define NVME_Q_DEPTH 2
16 #define NVME_AQ_DEPTH 2
17 #define NVME_SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
18 #define NVME_CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
19 #define ADMIN_TIMEOUT 60
21 #define MAX_PRP_POOL 512
30 * An NVM Express queue. Each device has at least two (one for admin
31 * commands and one for I/O commands).
35 struct nvme_command *sq_cmds;
36 struct nvme_completion *cqes;
37 wait_queue_head_t sq_full;
47 unsigned long cmdid_data[];
50 static int nvme_wait_ready(struct nvme_dev *dev, bool enabled)
52 u32 bit = enabled ? NVME_CSTS_RDY : 0;
56 /* Timeout field in the CAP register is in 500 millisecond units */
57 timeout = NVME_CAP_TIMEOUT(dev->cap) * 500;
60 while (get_timer(start) < timeout) {
61 if ((readl(&dev->bar->csts) & NVME_CSTS_RDY) == bit)
68 static int nvme_setup_prps(struct nvme_dev *dev, u64 *prp2,
69 int total_len, u64 dma_addr)
71 u32 page_size = dev->page_size;
72 int offset = dma_addr & (page_size - 1);
74 int length = total_len;
76 u32 prps_per_page = (page_size >> 3) - 1;
79 length -= (page_size - offset);
87 dma_addr += (page_size - offset);
89 if (length <= page_size) {
94 nprps = DIV_ROUND_UP(length, page_size);
95 num_pages = DIV_ROUND_UP(nprps, prps_per_page);
97 if (nprps > dev->prp_entry_num) {
100 * Always increase in increments of pages. It doesn't waste
101 * much memory and reduces the number of allocations.
103 dev->prp_pool = memalign(page_size, num_pages * page_size);
104 if (!dev->prp_pool) {
105 printf("Error: malloc prp_pool fail\n");
108 dev->prp_entry_num = prps_per_page * num_pages;
111 prp_pool = dev->prp_pool;
114 if (i == ((page_size >> 3) - 1)) {
115 *(prp_pool + i) = cpu_to_le64((ulong)prp_pool +
118 prp_pool += page_size;
120 *(prp_pool + i++) = cpu_to_le64(dma_addr);
121 dma_addr += page_size;
124 *prp2 = (ulong)dev->prp_pool;
129 static __le16 nvme_get_cmd_id(void)
131 static unsigned short cmdid;
133 return cpu_to_le16((cmdid < USHRT_MAX) ? cmdid++ : 0);
136 static u16 nvme_read_completion_status(struct nvme_queue *nvmeq, u16 index)
138 u64 start = (ulong)&nvmeq->cqes[index];
139 u64 stop = start + sizeof(struct nvme_completion);
141 invalidate_dcache_range(start, stop);
143 return le16_to_cpu(readw(&(nvmeq->cqes[index].status)));
147 * nvme_submit_cmd() - copy a command into a queue and ring the doorbell
149 * @nvmeq: The queue to use
150 * @cmd: The command to send
152 static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
154 u16 tail = nvmeq->sq_tail;
156 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
157 flush_dcache_range((ulong)&nvmeq->sq_cmds[tail],
158 (ulong)&nvmeq->sq_cmds[tail] + sizeof(*cmd));
160 if (++tail == nvmeq->q_depth)
162 writel(tail, nvmeq->q_db);
163 nvmeq->sq_tail = tail;
166 static int nvme_submit_sync_cmd(struct nvme_queue *nvmeq,
167 struct nvme_command *cmd,
168 u32 *result, unsigned timeout)
170 u16 head = nvmeq->cq_head;
171 u16 phase = nvmeq->cq_phase;
174 ulong timeout_us = timeout * 100000;
176 cmd->common.command_id = nvme_get_cmd_id();
177 nvme_submit_cmd(nvmeq, cmd);
179 start_time = timer_get_us();
182 status = nvme_read_completion_status(nvmeq, head);
183 if ((status & 0x01) == phase)
185 if (timeout_us > 0 && (timer_get_us() - start_time)
192 printf("ERROR: status = %x, phase = %d, head = %d\n",
193 status, phase, head);
195 if (++head == nvmeq->q_depth) {
199 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
200 nvmeq->cq_head = head;
201 nvmeq->cq_phase = phase;
207 *result = le32_to_cpu(readl(&(nvmeq->cqes[head].result)));
209 if (++head == nvmeq->q_depth) {
213 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
214 nvmeq->cq_head = head;
215 nvmeq->cq_phase = phase;
220 static int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
223 return nvme_submit_sync_cmd(dev->queues[NVME_ADMIN_Q], cmd,
224 result, ADMIN_TIMEOUT);
227 static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev,
230 struct nvme_queue *nvmeq = malloc(sizeof(*nvmeq));
233 memset(nvmeq, 0, sizeof(*nvmeq));
235 nvmeq->cqes = (void *)memalign(4096, NVME_CQ_SIZE(depth));
238 memset((void *)nvmeq->cqes, 0, NVME_CQ_SIZE(depth));
240 nvmeq->sq_cmds = (void *)memalign(4096, NVME_SQ_SIZE(depth));
243 memset((void *)nvmeq->sq_cmds, 0, NVME_SQ_SIZE(depth));
249 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
250 nvmeq->q_depth = depth;
253 dev->queues[qid] = nvmeq;
258 free((void *)nvmeq->cqes);
265 static int nvme_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
267 struct nvme_command c;
269 memset(&c, 0, sizeof(c));
270 c.delete_queue.opcode = opcode;
271 c.delete_queue.qid = cpu_to_le16(id);
273 return nvme_submit_admin_cmd(dev, &c, NULL);
276 static int nvme_delete_sq(struct nvme_dev *dev, u16 sqid)
278 return nvme_delete_queue(dev, nvme_admin_delete_sq, sqid);
281 static int nvme_delete_cq(struct nvme_dev *dev, u16 cqid)
283 return nvme_delete_queue(dev, nvme_admin_delete_cq, cqid);
286 static int nvme_enable_ctrl(struct nvme_dev *dev)
288 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
289 dev->ctrl_config |= NVME_CC_ENABLE;
290 writel(cpu_to_le32(dev->ctrl_config), &dev->bar->cc);
292 return nvme_wait_ready(dev, true);
295 static int nvme_disable_ctrl(struct nvme_dev *dev)
297 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
298 dev->ctrl_config &= ~NVME_CC_ENABLE;
299 writel(cpu_to_le32(dev->ctrl_config), &dev->bar->cc);
301 return nvme_wait_ready(dev, false);
304 static void nvme_free_queue(struct nvme_queue *nvmeq)
306 free((void *)nvmeq->cqes);
307 free(nvmeq->sq_cmds);
311 static void nvme_free_queues(struct nvme_dev *dev, int lowest)
315 for (i = dev->queue_count - 1; i >= lowest; i--) {
316 struct nvme_queue *nvmeq = dev->queues[i];
318 dev->queues[i] = NULL;
319 nvme_free_queue(nvmeq);
323 static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
325 struct nvme_dev *dev = nvmeq->dev;
330 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
331 memset((void *)nvmeq->cqes, 0, NVME_CQ_SIZE(nvmeq->q_depth));
332 flush_dcache_range((ulong)nvmeq->cqes,
333 (ulong)nvmeq->cqes + NVME_CQ_SIZE(nvmeq->q_depth));
334 dev->online_queues++;
337 static int nvme_configure_admin_queue(struct nvme_dev *dev)
342 struct nvme_queue *nvmeq;
343 /* most architectures use 4KB as the page size */
344 unsigned page_shift = 12;
345 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12;
346 unsigned dev_page_max = NVME_CAP_MPSMAX(cap) + 12;
348 if (page_shift < dev_page_min) {
349 debug("Device minimum page size (%u) too large for host (%u)\n",
350 1 << dev_page_min, 1 << page_shift);
354 if (page_shift > dev_page_max) {
355 debug("Device maximum page size (%u) smaller than host (%u)\n",
356 1 << dev_page_max, 1 << page_shift);
357 page_shift = dev_page_max;
360 result = nvme_disable_ctrl(dev);
364 nvmeq = dev->queues[NVME_ADMIN_Q];
366 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
371 aqa = nvmeq->q_depth - 1;
375 dev->page_size = 1 << page_shift;
377 dev->ctrl_config = NVME_CC_CSS_NVM;
378 dev->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
379 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
380 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
382 writel(aqa, &dev->bar->aqa);
383 nvme_writeq((ulong)nvmeq->sq_cmds, &dev->bar->asq);
384 nvme_writeq((ulong)nvmeq->cqes, &dev->bar->acq);
386 result = nvme_enable_ctrl(dev);
390 nvmeq->cq_vector = 0;
392 nvme_init_queue(dev->queues[NVME_ADMIN_Q], 0);
397 nvme_free_queues(dev, 0);
402 static int nvme_alloc_cq(struct nvme_dev *dev, u16 qid,
403 struct nvme_queue *nvmeq)
405 struct nvme_command c;
406 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
408 memset(&c, 0, sizeof(c));
409 c.create_cq.opcode = nvme_admin_create_cq;
410 c.create_cq.prp1 = cpu_to_le64((ulong)nvmeq->cqes);
411 c.create_cq.cqid = cpu_to_le16(qid);
412 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
413 c.create_cq.cq_flags = cpu_to_le16(flags);
414 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
416 return nvme_submit_admin_cmd(dev, &c, NULL);
419 static int nvme_alloc_sq(struct nvme_dev *dev, u16 qid,
420 struct nvme_queue *nvmeq)
422 struct nvme_command c;
423 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
425 memset(&c, 0, sizeof(c));
426 c.create_sq.opcode = nvme_admin_create_sq;
427 c.create_sq.prp1 = cpu_to_le64((ulong)nvmeq->sq_cmds);
428 c.create_sq.sqid = cpu_to_le16(qid);
429 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
430 c.create_sq.sq_flags = cpu_to_le16(flags);
431 c.create_sq.cqid = cpu_to_le16(qid);
433 return nvme_submit_admin_cmd(dev, &c, NULL);
436 int nvme_identify(struct nvme_dev *dev, unsigned nsid,
437 unsigned cns, dma_addr_t dma_addr)
439 struct nvme_command c;
440 u32 page_size = dev->page_size;
441 int offset = dma_addr & (page_size - 1);
442 int length = sizeof(struct nvme_id_ctrl);
445 memset(&c, 0, sizeof(c));
446 c.identify.opcode = nvme_admin_identify;
447 c.identify.nsid = cpu_to_le32(nsid);
448 c.identify.prp1 = cpu_to_le64(dma_addr);
450 length -= (page_size - offset);
454 dma_addr += (page_size - offset);
455 c.identify.prp2 = cpu_to_le64(dma_addr);
458 c.identify.cns = cpu_to_le32(cns);
460 ret = nvme_submit_admin_cmd(dev, &c, NULL);
462 invalidate_dcache_range(dma_addr,
463 dma_addr + sizeof(struct nvme_id_ctrl));
468 int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
469 dma_addr_t dma_addr, u32 *result)
471 struct nvme_command c;
473 memset(&c, 0, sizeof(c));
474 c.features.opcode = nvme_admin_get_features;
475 c.features.nsid = cpu_to_le32(nsid);
476 c.features.prp1 = cpu_to_le64(dma_addr);
477 c.features.fid = cpu_to_le32(fid);
480 * TODO: add cache invalidate operation when the size of
481 * the DMA buffer is known
484 return nvme_submit_admin_cmd(dev, &c, result);
487 int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
488 dma_addr_t dma_addr, u32 *result)
490 struct nvme_command c;
492 memset(&c, 0, sizeof(c));
493 c.features.opcode = nvme_admin_set_features;
494 c.features.prp1 = cpu_to_le64(dma_addr);
495 c.features.fid = cpu_to_le32(fid);
496 c.features.dword11 = cpu_to_le32(dword11);
499 * TODO: add cache flush operation when the size of
500 * the DMA buffer is known
503 return nvme_submit_admin_cmd(dev, &c, result);
506 static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
508 struct nvme_dev *dev = nvmeq->dev;
511 nvmeq->cq_vector = qid - 1;
512 result = nvme_alloc_cq(dev, qid, nvmeq);
516 result = nvme_alloc_sq(dev, qid, nvmeq);
520 nvme_init_queue(nvmeq, qid);
525 nvme_delete_sq(dev, qid);
527 nvme_delete_cq(dev, qid);
532 static int nvme_set_queue_count(struct nvme_dev *dev, int count)
536 u32 q_count = (count - 1) | ((count - 1) << 16);
538 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES,
539 q_count, 0, &result);
546 return min(result & 0xffff, result >> 16) + 1;
549 static void nvme_create_io_queues(struct nvme_dev *dev)
553 for (i = dev->queue_count; i <= dev->max_qid; i++)
554 if (!nvme_alloc_queue(dev, i, dev->q_depth))
557 for (i = dev->online_queues; i <= dev->queue_count - 1; i++)
558 if (nvme_create_queue(dev->queues[i], i))
562 static int nvme_setup_io_queues(struct nvme_dev *dev)
568 result = nvme_set_queue_count(dev, nr_io_queues);
572 dev->max_qid = nr_io_queues;
574 /* Free previously allocated queues */
575 nvme_free_queues(dev, nr_io_queues + 1);
576 nvme_create_io_queues(dev);
581 static int nvme_get_info_from_identify(struct nvme_dev *dev)
583 ALLOC_CACHE_ALIGN_BUFFER(char, buf, sizeof(struct nvme_id_ctrl));
584 struct nvme_id_ctrl *ctrl = (struct nvme_id_ctrl *)buf;
586 int shift = NVME_CAP_MPSMIN(dev->cap) + 12;
588 ret = nvme_identify(dev, 0, 1, (dma_addr_t)(long)ctrl);
592 dev->nn = le32_to_cpu(ctrl->nn);
593 dev->vwc = ctrl->vwc;
594 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
595 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
596 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
598 dev->max_transfer_shift = (ctrl->mdts + shift);
601 * Maximum Data Transfer Size (MDTS) field indicates the maximum
602 * data transfer size between the host and the controller. The
603 * host should not submit a command that exceeds this transfer
604 * size. The value is in units of the minimum memory page size
605 * and is reported as a power of two (2^n).
607 * The spec also says: a value of 0h indicates no restrictions
608 * on transfer size. But in nvme_blk_read/write() below we have
609 * the following algorithm for maximum number of logic blocks
612 * u16 lbas = 1 << (dev->max_transfer_shift - ns->lba_shift);
614 * In order for lbas not to overflow, the maximum number is 15
615 * which means dev->max_transfer_shift = 15 + 9 (ns->lba_shift).
616 * Let's use 20 which provides 1MB size.
618 dev->max_transfer_shift = 20;
624 int nvme_get_namespace_id(struct udevice *udev, u32 *ns_id, u8 *eui64)
626 struct nvme_ns *ns = dev_get_priv(udev);
631 memcpy(eui64, ns->eui64, sizeof(ns->eui64));
636 int nvme_scan_namespace(void)
642 ret = uclass_get(UCLASS_NVME, &uc);
646 uclass_foreach_dev(dev, uc) {
647 ret = device_probe(dev);
655 static int nvme_blk_probe(struct udevice *udev)
657 struct nvme_dev *ndev = dev_get_priv(udev->parent);
658 struct blk_desc *desc = dev_get_uclass_platdata(udev);
659 struct nvme_ns *ns = dev_get_priv(udev);
661 ALLOC_CACHE_ALIGN_BUFFER(char, buf, sizeof(struct nvme_id_ns));
662 struct nvme_id_ns *id = (struct nvme_id_ns *)buf;
663 struct pci_child_platdata *pplat;
665 memset(ns, 0, sizeof(*ns));
667 /* extract the namespace id from the block device name */
668 ns->ns_id = trailing_strtol(udev->name) + 1;
669 if (nvme_identify(ndev, ns->ns_id, 0, (dma_addr_t)(long)id))
672 memcpy(&ns->eui64, &id->eui64, sizeof(id->eui64));
673 flbas = id->flbas & NVME_NS_FLBAS_LBA_MASK;
675 ns->lba_shift = id->lbaf[flbas].ds;
676 ns->mode_select_num_blocks = le64_to_cpu(id->nsze);
677 ns->mode_select_block_len = 1 << ns->lba_shift;
678 list_add(&ns->list, &ndev->namespaces);
680 desc->lba = ns->mode_select_num_blocks;
681 desc->log2blksz = ns->lba_shift;
682 desc->blksz = 1 << ns->lba_shift;
684 pplat = dev_get_parent_platdata(udev->parent);
685 sprintf(desc->vendor, "0x%.4x", pplat->vendor);
686 memcpy(desc->product, ndev->serial, sizeof(ndev->serial));
687 memcpy(desc->revision, ndev->firmware_rev, sizeof(ndev->firmware_rev));
692 static ulong nvme_blk_rw(struct udevice *udev, lbaint_t blknr,
693 lbaint_t blkcnt, void *buffer, bool read)
695 struct nvme_ns *ns = dev_get_priv(udev);
696 struct nvme_dev *dev = ns->dev;
697 struct nvme_command c;
698 struct blk_desc *desc = dev_get_uclass_platdata(udev);
701 u64 total_len = blkcnt << desc->log2blksz;
702 u64 temp_len = total_len;
705 u16 lbas = 1 << (dev->max_transfer_shift - ns->lba_shift);
706 u64 total_lbas = blkcnt;
709 flush_dcache_range((unsigned long)buffer,
710 (unsigned long)buffer + total_len);
712 c.rw.opcode = read ? nvme_cmd_read : nvme_cmd_write;
714 c.rw.nsid = cpu_to_le32(ns->ns_id);
723 if (total_lbas < lbas) {
724 lbas = (u16)total_lbas;
730 if (nvme_setup_prps(dev, &prp2,
731 lbas << ns->lba_shift, (ulong)buffer))
733 c.rw.slba = cpu_to_le64(slba);
735 c.rw.length = cpu_to_le16(lbas - 1);
736 c.rw.prp1 = cpu_to_le64((ulong)buffer);
737 c.rw.prp2 = cpu_to_le64(prp2);
738 status = nvme_submit_sync_cmd(dev->queues[NVME_IO_Q],
739 &c, NULL, IO_TIMEOUT);
742 temp_len -= (u32)lbas << ns->lba_shift;
743 buffer += lbas << ns->lba_shift;
747 invalidate_dcache_range((unsigned long)buffer,
748 (unsigned long)buffer + total_len);
750 return (total_len - temp_len) >> desc->log2blksz;
753 static ulong nvme_blk_read(struct udevice *udev, lbaint_t blknr,
754 lbaint_t blkcnt, void *buffer)
756 return nvme_blk_rw(udev, blknr, blkcnt, buffer, true);
759 static ulong nvme_blk_write(struct udevice *udev, lbaint_t blknr,
760 lbaint_t blkcnt, const void *buffer)
762 return nvme_blk_rw(udev, blknr, blkcnt, (void *)buffer, false);
765 static const struct blk_ops nvme_blk_ops = {
766 .read = nvme_blk_read,
767 .write = nvme_blk_write,
770 U_BOOT_DRIVER(nvme_blk) = {
773 .probe = nvme_blk_probe,
774 .ops = &nvme_blk_ops,
775 .priv_auto_alloc_size = sizeof(struct nvme_ns),
778 static int nvme_bind(struct udevice *udev)
783 sprintf(name, "nvme#%d", ndev_num++);
785 return device_set_name(udev, name);
788 static int nvme_probe(struct udevice *udev)
791 struct nvme_dev *ndev = dev_get_priv(udev);
793 ndev->instance = trailing_strtol(udev->name);
795 INIT_LIST_HEAD(&ndev->namespaces);
796 ndev->bar = dm_pci_map_bar(udev, PCI_BASE_ADDRESS_0,
798 if (readl(&ndev->bar->csts) == -1) {
800 printf("Error: %s: Out of memory!\n", udev->name);
804 ndev->queues = malloc(NVME_Q_NUM * sizeof(struct nvme_queue *));
807 printf("Error: %s: Out of memory!\n", udev->name);
810 memset(ndev->queues, 0, NVME_Q_NUM * sizeof(struct nvme_queue *));
812 ndev->cap = nvme_readq(&ndev->bar->cap);
813 ndev->q_depth = min_t(int, NVME_CAP_MQES(ndev->cap) + 1, NVME_Q_DEPTH);
814 ndev->db_stride = 1 << NVME_CAP_STRIDE(ndev->cap);
815 ndev->dbs = ((void __iomem *)ndev->bar) + 4096;
817 ret = nvme_configure_admin_queue(ndev);
821 /* Allocate after the page size is known */
822 ndev->prp_pool = memalign(ndev->page_size, MAX_PRP_POOL);
823 if (!ndev->prp_pool) {
825 printf("Error: %s: Out of memory!\n", udev->name);
828 ndev->prp_entry_num = MAX_PRP_POOL >> 3;
830 ret = nvme_setup_io_queues(ndev);
834 nvme_get_info_from_identify(ndev);
839 free((void *)ndev->queues);
844 U_BOOT_DRIVER(nvme) = {
849 .priv_auto_alloc_size = sizeof(struct nvme_dev),
852 struct pci_device_id nvme_supported[] = {
853 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, ~0) },
857 U_BOOT_PCI_DEVICE(nvme, nvme_supported);