2 * Copyright (C) 2017 NXP Semiconductors
3 * Copyright (C) 2017 Bin Meng <bmeng.cn@gmail.com>
5 * SPDX-License-Identifier: GPL-2.0+
13 #include <dm/device-internal.h>
16 struct nvme_info *nvme_info;
18 #define NVME_Q_DEPTH 2
19 #define NVME_AQ_DEPTH 2
20 #define NVME_SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
21 #define NVME_CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
22 #define ADMIN_TIMEOUT 60
24 #define MAX_PRP_POOL 512
33 * An NVM Express queue. Each device has at least two (one for admin
34 * commands and one for I/O commands).
38 struct nvme_command *sq_cmds;
39 struct nvme_completion *cqes;
40 wait_queue_head_t sq_full;
50 unsigned long cmdid_data[];
53 static int nvme_wait_ready(struct nvme_dev *dev, bool enabled)
55 u32 bit = enabled ? NVME_CSTS_RDY : 0;
59 /* Timeout field in the CAP register is in 500 millisecond units */
60 timeout = NVME_CAP_TIMEOUT(dev->cap) * 500;
63 while (get_timer(start) < timeout) {
64 if ((readl(&dev->bar->csts) & NVME_CSTS_RDY) == bit)
71 static int nvme_setup_prps(struct nvme_dev *dev, u64 *prp2,
72 int total_len, u64 dma_addr)
74 u32 page_size = dev->page_size;
75 int offset = dma_addr & (page_size - 1);
77 int length = total_len;
79 length -= (page_size - offset);
87 dma_addr += (page_size - offset);
89 if (length <= page_size) {
94 nprps = DIV_ROUND_UP(length, page_size);
96 if (nprps > dev->prp_entry_num) {
98 dev->prp_pool = malloc(nprps << 3);
100 printf("Error: malloc prp_pool fail\n");
103 dev->prp_entry_num = nprps;
106 prp_pool = dev->prp_pool;
109 if (i == ((page_size >> 3) - 1)) {
110 *(prp_pool + i) = cpu_to_le64((ulong)prp_pool +
113 prp_pool += page_size;
115 *(prp_pool + i++) = cpu_to_le64(dma_addr);
116 dma_addr += page_size;
119 *prp2 = (ulong)dev->prp_pool;
124 static __le16 nvme_get_cmd_id(void)
126 static unsigned short cmdid;
128 return cpu_to_le16((cmdid < USHRT_MAX) ? cmdid++ : 0);
131 static u16 nvme_read_completion_status(struct nvme_queue *nvmeq, u16 index)
133 u64 start = (ulong)&nvmeq->cqes[index];
134 u64 stop = start + sizeof(struct nvme_completion);
136 invalidate_dcache_range(start, stop);
138 return le16_to_cpu(readw(&(nvmeq->cqes[index].status)));
142 * nvme_submit_cmd() - copy a command into a queue and ring the doorbell
144 * @nvmeq: The queue to use
145 * @cmd: The command to send
147 static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
149 u16 tail = nvmeq->sq_tail;
151 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
152 flush_dcache_range((ulong)&nvmeq->sq_cmds[tail],
153 (ulong)&nvmeq->sq_cmds[tail] + sizeof(*cmd));
155 if (++tail == nvmeq->q_depth)
157 writel(tail, nvmeq->q_db);
158 nvmeq->sq_tail = tail;
161 static int nvme_submit_sync_cmd(struct nvme_queue *nvmeq,
162 struct nvme_command *cmd,
163 u32 *result, unsigned timeout)
165 u16 head = nvmeq->cq_head;
166 u16 phase = nvmeq->cq_phase;
169 ulong timeout_us = timeout * 100000;
171 cmd->common.command_id = nvme_get_cmd_id();
172 nvme_submit_cmd(nvmeq, cmd);
174 start_time = timer_get_us();
177 status = nvme_read_completion_status(nvmeq, head);
178 if ((status & 0x01) == phase)
180 if (timeout_us > 0 && (timer_get_us() - start_time)
187 printf("ERROR: status = %x, phase = %d, head = %d\n",
188 status, phase, head);
190 if (++head == nvmeq->q_depth) {
194 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
195 nvmeq->cq_head = head;
196 nvmeq->cq_phase = phase;
202 *result = le32_to_cpu(readl(&(nvmeq->cqes[head].result)));
204 if (++head == nvmeq->q_depth) {
208 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
209 nvmeq->cq_head = head;
210 nvmeq->cq_phase = phase;
215 static int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
218 return nvme_submit_sync_cmd(dev->queues[NVME_ADMIN_Q], cmd,
219 result, ADMIN_TIMEOUT);
222 static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev,
225 struct nvme_queue *nvmeq = malloc(sizeof(*nvmeq));
228 memset(nvmeq, 0, sizeof(*nvmeq));
230 nvmeq->cqes = (void *)memalign(4096, NVME_CQ_SIZE(depth));
233 memset((void *)nvmeq->cqes, 0, NVME_CQ_SIZE(depth));
235 nvmeq->sq_cmds = (void *)memalign(4096, NVME_SQ_SIZE(depth));
238 memset((void *)nvmeq->sq_cmds, 0, NVME_SQ_SIZE(depth));
244 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
245 nvmeq->q_depth = depth;
248 dev->queues[qid] = nvmeq;
253 free((void *)nvmeq->cqes);
260 static int nvme_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
262 struct nvme_command c;
264 memset(&c, 0, sizeof(c));
265 c.delete_queue.opcode = opcode;
266 c.delete_queue.qid = cpu_to_le16(id);
268 return nvme_submit_admin_cmd(dev, &c, NULL);
271 static int nvme_delete_sq(struct nvme_dev *dev, u16 sqid)
273 return nvme_delete_queue(dev, nvme_admin_delete_sq, sqid);
276 static int nvme_delete_cq(struct nvme_dev *dev, u16 cqid)
278 return nvme_delete_queue(dev, nvme_admin_delete_cq, cqid);
281 static int nvme_enable_ctrl(struct nvme_dev *dev)
283 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
284 dev->ctrl_config |= NVME_CC_ENABLE;
285 writel(cpu_to_le32(dev->ctrl_config), &dev->bar->cc);
287 return nvme_wait_ready(dev, true);
290 static int nvme_disable_ctrl(struct nvme_dev *dev)
292 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
293 dev->ctrl_config &= ~NVME_CC_ENABLE;
294 writel(cpu_to_le32(dev->ctrl_config), &dev->bar->cc);
296 return nvme_wait_ready(dev, false);
299 static void nvme_free_queue(struct nvme_queue *nvmeq)
301 free((void *)nvmeq->cqes);
302 free(nvmeq->sq_cmds);
306 static void nvme_free_queues(struct nvme_dev *dev, int lowest)
310 for (i = dev->queue_count - 1; i >= lowest; i--) {
311 struct nvme_queue *nvmeq = dev->queues[i];
313 dev->queues[i] = NULL;
314 nvme_free_queue(nvmeq);
318 static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
320 struct nvme_dev *dev = nvmeq->dev;
325 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
326 memset((void *)nvmeq->cqes, 0, NVME_CQ_SIZE(nvmeq->q_depth));
327 flush_dcache_range((ulong)nvmeq->cqes,
328 (ulong)nvmeq->cqes + NVME_CQ_SIZE(nvmeq->q_depth));
329 dev->online_queues++;
332 static int nvme_configure_admin_queue(struct nvme_dev *dev)
337 struct nvme_queue *nvmeq;
338 /* most architectures use 4KB as the page size */
339 unsigned page_shift = 12;
340 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12;
341 unsigned dev_page_max = NVME_CAP_MPSMAX(cap) + 12;
343 if (page_shift < dev_page_min) {
344 debug("Device minimum page size (%u) too large for host (%u)\n",
345 1 << dev_page_min, 1 << page_shift);
349 if (page_shift > dev_page_max) {
350 debug("Device maximum page size (%u) smaller than host (%u)\n",
351 1 << dev_page_max, 1 << page_shift);
352 page_shift = dev_page_max;
355 result = nvme_disable_ctrl(dev);
359 nvmeq = dev->queues[NVME_ADMIN_Q];
361 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
366 aqa = nvmeq->q_depth - 1;
370 dev->page_size = 1 << page_shift;
372 dev->ctrl_config = NVME_CC_CSS_NVM;
373 dev->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
374 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
375 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
377 writel(aqa, &dev->bar->aqa);
378 nvme_writeq((ulong)nvmeq->sq_cmds, &dev->bar->asq);
379 nvme_writeq((ulong)nvmeq->cqes, &dev->bar->acq);
381 result = nvme_enable_ctrl(dev);
385 nvmeq->cq_vector = 0;
387 nvme_init_queue(dev->queues[NVME_ADMIN_Q], 0);
392 nvme_free_queues(dev, 0);
397 static int nvme_alloc_cq(struct nvme_dev *dev, u16 qid,
398 struct nvme_queue *nvmeq)
400 struct nvme_command c;
401 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
403 memset(&c, 0, sizeof(c));
404 c.create_cq.opcode = nvme_admin_create_cq;
405 c.create_cq.prp1 = cpu_to_le64((ulong)nvmeq->cqes);
406 c.create_cq.cqid = cpu_to_le16(qid);
407 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
408 c.create_cq.cq_flags = cpu_to_le16(flags);
409 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
411 return nvme_submit_admin_cmd(dev, &c, NULL);
414 static int nvme_alloc_sq(struct nvme_dev *dev, u16 qid,
415 struct nvme_queue *nvmeq)
417 struct nvme_command c;
418 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
420 memset(&c, 0, sizeof(c));
421 c.create_sq.opcode = nvme_admin_create_sq;
422 c.create_sq.prp1 = cpu_to_le64((ulong)nvmeq->sq_cmds);
423 c.create_sq.sqid = cpu_to_le16(qid);
424 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
425 c.create_sq.sq_flags = cpu_to_le16(flags);
426 c.create_sq.cqid = cpu_to_le16(qid);
428 return nvme_submit_admin_cmd(dev, &c, NULL);
431 int nvme_identify(struct nvme_dev *dev, unsigned nsid,
432 unsigned cns, dma_addr_t dma_addr)
434 struct nvme_command c;
435 u32 page_size = dev->page_size;
436 int offset = dma_addr & (page_size - 1);
437 int length = sizeof(struct nvme_id_ctrl);
439 memset(&c, 0, sizeof(c));
440 c.identify.opcode = nvme_admin_identify;
441 c.identify.nsid = cpu_to_le32(nsid);
442 c.identify.prp1 = cpu_to_le64(dma_addr);
444 length -= (page_size - offset);
448 dma_addr += (page_size - offset);
449 c.identify.prp2 = cpu_to_le64(dma_addr);
452 c.identify.cns = cpu_to_le32(cns);
454 return nvme_submit_admin_cmd(dev, &c, NULL);
457 int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
458 dma_addr_t dma_addr, u32 *result)
460 struct nvme_command c;
462 memset(&c, 0, sizeof(c));
463 c.features.opcode = nvme_admin_get_features;
464 c.features.nsid = cpu_to_le32(nsid);
465 c.features.prp1 = cpu_to_le64(dma_addr);
466 c.features.fid = cpu_to_le32(fid);
468 return nvme_submit_admin_cmd(dev, &c, result);
471 int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
472 dma_addr_t dma_addr, u32 *result)
474 struct nvme_command c;
476 memset(&c, 0, sizeof(c));
477 c.features.opcode = nvme_admin_set_features;
478 c.features.prp1 = cpu_to_le64(dma_addr);
479 c.features.fid = cpu_to_le32(fid);
480 c.features.dword11 = cpu_to_le32(dword11);
482 return nvme_submit_admin_cmd(dev, &c, result);
485 static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
487 struct nvme_dev *dev = nvmeq->dev;
490 nvmeq->cq_vector = qid - 1;
491 result = nvme_alloc_cq(dev, qid, nvmeq);
495 result = nvme_alloc_sq(dev, qid, nvmeq);
499 nvme_init_queue(nvmeq, qid);
504 nvme_delete_sq(dev, qid);
506 nvme_delete_cq(dev, qid);
511 static int nvme_set_queue_count(struct nvme_dev *dev, int count)
515 u32 q_count = (count - 1) | ((count - 1) << 16);
517 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES,
518 q_count, 0, &result);
525 return min(result & 0xffff, result >> 16) + 1;
528 static void nvme_create_io_queues(struct nvme_dev *dev)
532 for (i = dev->queue_count; i <= dev->max_qid; i++)
533 if (!nvme_alloc_queue(dev, i, dev->q_depth))
536 for (i = dev->online_queues; i <= dev->queue_count - 1; i++)
537 if (nvme_create_queue(dev->queues[i], i))
541 static int nvme_setup_io_queues(struct nvme_dev *dev)
547 result = nvme_set_queue_count(dev, nr_io_queues);
551 if (result < nr_io_queues)
552 nr_io_queues = result;
554 dev->max_qid = nr_io_queues;
556 /* Free previously allocated queues */
557 nvme_free_queues(dev, nr_io_queues + 1);
558 nvme_create_io_queues(dev);
563 static int nvme_get_info_from_identify(struct nvme_dev *dev)
565 struct nvme_id_ctrl buf, *ctrl = &buf;
567 int shift = NVME_CAP_MPSMIN(dev->cap) + 12;
569 ret = nvme_identify(dev, 0, 1, (dma_addr_t)ctrl);
573 dev->nn = le32_to_cpu(ctrl->nn);
574 dev->vwc = ctrl->vwc;
575 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
576 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
577 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
579 dev->max_transfer_shift = (ctrl->mdts + shift);
582 * Maximum Data Transfer Size (MDTS) field indicates the maximum
583 * data transfer size between the host and the controller. The
584 * host should not submit a command that exceeds this transfer
585 * size. The value is in units of the minimum memory page size
586 * and is reported as a power of two (2^n).
588 * The spec also says: a value of 0h indicates no restrictions
589 * on transfer size. But in nvme_blk_read/write() below we have
590 * the following algorithm for maximum number of logic blocks
593 * u16 lbas = 1 << (dev->max_transfer_shift - ns->lba_shift);
595 * In order for lbas not to overflow, the maximum number is 15
596 * which means dev->max_transfer_shift = 15 + 9 (ns->lba_shift).
597 * Let's use 20 which provides 1MB size.
599 dev->max_transfer_shift = 20;
605 int nvme_scan_namespace(void)
611 ret = uclass_get(UCLASS_NVME, &uc);
615 uclass_foreach_dev(dev, uc) {
616 ret = device_probe(dev);
624 static int nvme_blk_probe(struct udevice *udev)
626 struct nvme_dev *ndev = dev_get_priv(udev->parent);
627 struct blk_desc *desc = dev_get_uclass_platdata(udev);
628 struct nvme_ns *ns = dev_get_priv(udev);
630 struct nvme_id_ns buf, *id = &buf;
631 struct pci_child_platdata *pplat;
633 memset(ns, 0, sizeof(*ns));
635 ns->ns_id = desc->devnum - ndev->blk_dev_start + 1;
636 if (nvme_identify(ndev, ns->ns_id, 0, (dma_addr_t)id))
639 flbas = id->flbas & NVME_NS_FLBAS_LBA_MASK;
641 ns->lba_shift = id->lbaf[flbas].ds;
642 ns->mode_select_num_blocks = le64_to_cpu(id->nsze);
643 ns->mode_select_block_len = 1 << ns->lba_shift;
644 list_add(&ns->list, &ndev->namespaces);
646 desc->lba = ns->mode_select_num_blocks;
647 desc->log2blksz = ns->lba_shift;
648 desc->blksz = 1 << ns->lba_shift;
650 pplat = dev_get_parent_platdata(udev->parent);
651 sprintf(desc->vendor, "0x%.4x", pplat->vendor);
652 memcpy(desc->product, ndev->serial, sizeof(ndev->serial));
653 memcpy(desc->revision, ndev->firmware_rev, sizeof(ndev->firmware_rev));
659 static ulong nvme_blk_rw(struct udevice *udev, lbaint_t blknr,
660 lbaint_t blkcnt, void *buffer, bool read)
662 struct nvme_ns *ns = dev_get_priv(udev);
663 struct nvme_dev *dev = ns->dev;
664 struct nvme_command c;
665 struct blk_desc *desc = dev_get_uclass_platdata(udev);
668 u64 total_len = blkcnt << desc->log2blksz;
669 u64 temp_len = total_len;
672 u16 lbas = 1 << (dev->max_transfer_shift - ns->lba_shift);
673 u64 total_lbas = blkcnt;
675 c.rw.opcode = read ? nvme_cmd_read : nvme_cmd_write;
677 c.rw.nsid = cpu_to_le32(ns->ns_id);
686 if (total_lbas < lbas) {
687 lbas = (u16)total_lbas;
693 if (nvme_setup_prps(dev, &prp2,
694 lbas << ns->lba_shift, (ulong)buffer))
696 c.rw.slba = cpu_to_le64(slba);
698 c.rw.length = cpu_to_le16(lbas - 1);
699 c.rw.prp1 = cpu_to_le64((ulong)buffer);
700 c.rw.prp2 = cpu_to_le64(prp2);
701 status = nvme_submit_sync_cmd(dev->queues[NVME_IO_Q],
702 &c, NULL, IO_TIMEOUT);
705 temp_len -= lbas << ns->lba_shift;
706 buffer += lbas << ns->lba_shift;
709 return (total_len - temp_len) >> desc->log2blksz;
712 static ulong nvme_blk_read(struct udevice *udev, lbaint_t blknr,
713 lbaint_t blkcnt, void *buffer)
715 return nvme_blk_rw(udev, blknr, blkcnt, buffer, true);
718 static ulong nvme_blk_write(struct udevice *udev, lbaint_t blknr,
719 lbaint_t blkcnt, const void *buffer)
721 return nvme_blk_rw(udev, blknr, blkcnt, (void *)buffer, false);
724 static const struct blk_ops nvme_blk_ops = {
725 .read = nvme_blk_read,
726 .write = nvme_blk_write,
729 U_BOOT_DRIVER(nvme_blk) = {
732 .probe = nvme_blk_probe,
733 .ops = &nvme_blk_ops,
734 .priv_auto_alloc_size = sizeof(struct nvme_ns),
737 static int nvme_bind(struct udevice *udev)
740 sprintf(name, "nvme#%d", nvme_info->ndev_num++);
742 return device_set_name(udev, name);
745 static int nvme_probe(struct udevice *udev)
748 struct nvme_dev *ndev = dev_get_priv(udev);
750 ndev->instance = trailing_strtol(udev->name);
752 INIT_LIST_HEAD(&ndev->namespaces);
753 ndev->bar = dm_pci_map_bar(udev, PCI_BASE_ADDRESS_0,
755 if (readl(&ndev->bar->csts) == -1) {
757 printf("Error: %s: Out of memory!\n", udev->name);
761 ndev->queues = malloc(NVME_Q_NUM * sizeof(struct nvme_queue *));
764 printf("Error: %s: Out of memory!\n", udev->name);
767 memset(ndev->queues, 0,
768 sizeof(NVME_Q_NUM * sizeof(struct nvme_queue *)));
770 ndev->prp_pool = malloc(MAX_PRP_POOL);
771 if (!ndev->prp_pool) {
773 printf("Error: %s: Out of memory!\n", udev->name);
776 ndev->prp_entry_num = MAX_PRP_POOL >> 3;
778 ndev->cap = nvme_readq(&ndev->bar->cap);
779 ndev->q_depth = min_t(int, NVME_CAP_MQES(ndev->cap) + 1, NVME_Q_DEPTH);
780 ndev->db_stride = 1 << NVME_CAP_STRIDE(ndev->cap);
781 ndev->dbs = ((void __iomem *)ndev->bar) + 4096;
783 ret = nvme_configure_admin_queue(ndev);
787 ret = nvme_setup_io_queues(ndev);
791 nvme_get_info_from_identify(ndev);
792 ndev->blk_dev_start = nvme_info->ns_num;
793 list_add(&ndev->node, &nvme_info->dev_list);
798 free((void *)ndev->queues);
803 U_BOOT_DRIVER(nvme) = {
808 .priv_auto_alloc_size = sizeof(struct nvme_dev),
811 struct pci_device_id nvme_supported[] = {
812 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, ~0) },
816 U_BOOT_PCI_DEVICE(nvme, nvme_supported);