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 #define NVME_Q_DEPTH 2
17 #define NVME_AQ_DEPTH 2
18 #define NVME_SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
19 #define NVME_CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
20 #define ADMIN_TIMEOUT 60
22 #define MAX_PRP_POOL 512
31 * An NVM Express queue. Each device has at least two (one for admin
32 * commands and one for I/O commands).
36 struct nvme_command *sq_cmds;
37 struct nvme_completion *cqes;
38 wait_queue_head_t sq_full;
48 unsigned long cmdid_data[];
51 static int nvme_wait_ready(struct nvme_dev *dev, bool enabled)
53 u32 bit = enabled ? NVME_CSTS_RDY : 0;
57 /* Timeout field in the CAP register is in 500 millisecond units */
58 timeout = NVME_CAP_TIMEOUT(dev->cap) * 500;
61 while (get_timer(start) < timeout) {
62 if ((readl(&dev->bar->csts) & NVME_CSTS_RDY) == bit)
69 static int nvme_setup_prps(struct nvme_dev *dev, u64 *prp2,
70 int total_len, u64 dma_addr)
72 u32 page_size = dev->page_size;
73 int offset = dma_addr & (page_size - 1);
75 int length = total_len;
77 length -= (page_size - offset);
85 dma_addr += (page_size - offset);
87 if (length <= page_size) {
92 nprps = DIV_ROUND_UP(length, page_size);
94 if (nprps > dev->prp_entry_num) {
96 dev->prp_pool = malloc(nprps << 3);
98 printf("Error: malloc prp_pool fail\n");
101 dev->prp_entry_num = nprps;
104 prp_pool = dev->prp_pool;
107 if (i == ((page_size >> 3) - 1)) {
108 *(prp_pool + i) = cpu_to_le64((ulong)prp_pool +
111 prp_pool += page_size;
113 *(prp_pool + i++) = cpu_to_le64(dma_addr);
114 dma_addr += page_size;
117 *prp2 = (ulong)dev->prp_pool;
122 static __le16 nvme_get_cmd_id(void)
124 static unsigned short cmdid;
126 return cpu_to_le16((cmdid < USHRT_MAX) ? cmdid++ : 0);
129 static u16 nvme_read_completion_status(struct nvme_queue *nvmeq, u16 index)
131 u64 start = (ulong)&nvmeq->cqes[index];
132 u64 stop = start + sizeof(struct nvme_completion);
134 invalidate_dcache_range(start, stop);
136 return le16_to_cpu(readw(&(nvmeq->cqes[index].status)));
140 * nvme_submit_cmd() - copy a command into a queue and ring the doorbell
142 * @nvmeq: The queue to use
143 * @cmd: The command to send
145 static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
147 u16 tail = nvmeq->sq_tail;
149 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
150 flush_dcache_range((ulong)&nvmeq->sq_cmds[tail],
151 (ulong)&nvmeq->sq_cmds[tail] + sizeof(*cmd));
153 if (++tail == nvmeq->q_depth)
155 writel(tail, nvmeq->q_db);
156 nvmeq->sq_tail = tail;
159 static int nvme_submit_sync_cmd(struct nvme_queue *nvmeq,
160 struct nvme_command *cmd,
161 u32 *result, unsigned timeout)
163 u16 head = nvmeq->cq_head;
164 u16 phase = nvmeq->cq_phase;
167 ulong timeout_us = timeout * 100000;
169 cmd->common.command_id = nvme_get_cmd_id();
170 nvme_submit_cmd(nvmeq, cmd);
172 start_time = timer_get_us();
175 status = nvme_read_completion_status(nvmeq, head);
176 if ((status & 0x01) == phase)
178 if (timeout_us > 0 && (timer_get_us() - start_time)
185 printf("ERROR: status = %x, phase = %d, head = %d\n",
186 status, phase, head);
188 if (++head == nvmeq->q_depth) {
192 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
193 nvmeq->cq_head = head;
194 nvmeq->cq_phase = phase;
200 *result = le32_to_cpu(readl(&(nvmeq->cqes[head].result)));
202 if (++head == nvmeq->q_depth) {
206 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
207 nvmeq->cq_head = head;
208 nvmeq->cq_phase = phase;
213 static int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
216 return nvme_submit_sync_cmd(dev->queues[NVME_ADMIN_Q], cmd,
217 result, ADMIN_TIMEOUT);
220 static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev,
223 struct nvme_queue *nvmeq = malloc(sizeof(*nvmeq));
226 memset(nvmeq, 0, sizeof(*nvmeq));
228 nvmeq->cqes = (void *)memalign(4096, NVME_CQ_SIZE(depth));
231 memset((void *)nvmeq->cqes, 0, NVME_CQ_SIZE(depth));
233 nvmeq->sq_cmds = (void *)memalign(4096, NVME_SQ_SIZE(depth));
236 memset((void *)nvmeq->sq_cmds, 0, NVME_SQ_SIZE(depth));
242 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
243 nvmeq->q_depth = depth;
246 dev->queues[qid] = nvmeq;
251 free((void *)nvmeq->cqes);
258 static int nvme_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
260 struct nvme_command c;
262 memset(&c, 0, sizeof(c));
263 c.delete_queue.opcode = opcode;
264 c.delete_queue.qid = cpu_to_le16(id);
266 return nvme_submit_admin_cmd(dev, &c, NULL);
269 static int nvme_delete_sq(struct nvme_dev *dev, u16 sqid)
271 return nvme_delete_queue(dev, nvme_admin_delete_sq, sqid);
274 static int nvme_delete_cq(struct nvme_dev *dev, u16 cqid)
276 return nvme_delete_queue(dev, nvme_admin_delete_cq, cqid);
279 static int nvme_enable_ctrl(struct nvme_dev *dev)
281 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
282 dev->ctrl_config |= NVME_CC_ENABLE;
283 writel(cpu_to_le32(dev->ctrl_config), &dev->bar->cc);
285 return nvme_wait_ready(dev, true);
288 static int nvme_disable_ctrl(struct nvme_dev *dev)
290 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
291 dev->ctrl_config &= ~NVME_CC_ENABLE;
292 writel(cpu_to_le32(dev->ctrl_config), &dev->bar->cc);
294 return nvme_wait_ready(dev, false);
297 static void nvme_free_queue(struct nvme_queue *nvmeq)
299 free((void *)nvmeq->cqes);
300 free(nvmeq->sq_cmds);
304 static void nvme_free_queues(struct nvme_dev *dev, int lowest)
308 for (i = dev->queue_count - 1; i >= lowest; i--) {
309 struct nvme_queue *nvmeq = dev->queues[i];
311 dev->queues[i] = NULL;
312 nvme_free_queue(nvmeq);
316 static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
318 struct nvme_dev *dev = nvmeq->dev;
323 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
324 memset((void *)nvmeq->cqes, 0, NVME_CQ_SIZE(nvmeq->q_depth));
325 flush_dcache_range((ulong)nvmeq->cqes,
326 (ulong)nvmeq->cqes + NVME_CQ_SIZE(nvmeq->q_depth));
327 dev->online_queues++;
330 static int nvme_configure_admin_queue(struct nvme_dev *dev)
335 struct nvme_queue *nvmeq;
336 /* most architectures use 4KB as the page size */
337 unsigned page_shift = 12;
338 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12;
339 unsigned dev_page_max = NVME_CAP_MPSMAX(cap) + 12;
341 if (page_shift < dev_page_min) {
342 debug("Device minimum page size (%u) too large for host (%u)\n",
343 1 << dev_page_min, 1 << page_shift);
347 if (page_shift > dev_page_max) {
348 debug("Device maximum page size (%u) smaller than host (%u)\n",
349 1 << dev_page_max, 1 << page_shift);
350 page_shift = dev_page_max;
353 result = nvme_disable_ctrl(dev);
357 nvmeq = dev->queues[NVME_ADMIN_Q];
359 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
364 aqa = nvmeq->q_depth - 1;
368 dev->page_size = 1 << page_shift;
370 dev->ctrl_config = NVME_CC_CSS_NVM;
371 dev->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
372 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
373 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
375 writel(aqa, &dev->bar->aqa);
376 nvme_writeq((ulong)nvmeq->sq_cmds, &dev->bar->asq);
377 nvme_writeq((ulong)nvmeq->cqes, &dev->bar->acq);
379 result = nvme_enable_ctrl(dev);
383 nvmeq->cq_vector = 0;
385 nvme_init_queue(dev->queues[NVME_ADMIN_Q], 0);
390 nvme_free_queues(dev, 0);
395 static int nvme_alloc_cq(struct nvme_dev *dev, u16 qid,
396 struct nvme_queue *nvmeq)
398 struct nvme_command c;
399 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
401 memset(&c, 0, sizeof(c));
402 c.create_cq.opcode = nvme_admin_create_cq;
403 c.create_cq.prp1 = cpu_to_le64((ulong)nvmeq->cqes);
404 c.create_cq.cqid = cpu_to_le16(qid);
405 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
406 c.create_cq.cq_flags = cpu_to_le16(flags);
407 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
409 return nvme_submit_admin_cmd(dev, &c, NULL);
412 static int nvme_alloc_sq(struct nvme_dev *dev, u16 qid,
413 struct nvme_queue *nvmeq)
415 struct nvme_command c;
416 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
418 memset(&c, 0, sizeof(c));
419 c.create_sq.opcode = nvme_admin_create_sq;
420 c.create_sq.prp1 = cpu_to_le64((ulong)nvmeq->sq_cmds);
421 c.create_sq.sqid = cpu_to_le16(qid);
422 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
423 c.create_sq.sq_flags = cpu_to_le16(flags);
424 c.create_sq.cqid = cpu_to_le16(qid);
426 return nvme_submit_admin_cmd(dev, &c, NULL);
429 int nvme_identify(struct nvme_dev *dev, unsigned nsid,
430 unsigned cns, dma_addr_t dma_addr)
432 struct nvme_command c;
433 u32 page_size = dev->page_size;
434 int offset = dma_addr & (page_size - 1);
435 int length = sizeof(struct nvme_id_ctrl);
438 memset(&c, 0, sizeof(c));
439 c.identify.opcode = nvme_admin_identify;
440 c.identify.nsid = cpu_to_le32(nsid);
441 c.identify.prp1 = cpu_to_le64(dma_addr);
443 length -= (page_size - offset);
447 dma_addr += (page_size - offset);
448 c.identify.prp2 = cpu_to_le64(dma_addr);
451 c.identify.cns = cpu_to_le32(cns);
453 ret = nvme_submit_admin_cmd(dev, &c, NULL);
455 invalidate_dcache_range(dma_addr,
456 dma_addr + sizeof(struct nvme_id_ctrl));
461 int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
462 dma_addr_t dma_addr, u32 *result)
464 struct nvme_command c;
466 memset(&c, 0, sizeof(c));
467 c.features.opcode = nvme_admin_get_features;
468 c.features.nsid = cpu_to_le32(nsid);
469 c.features.prp1 = cpu_to_le64(dma_addr);
470 c.features.fid = cpu_to_le32(fid);
473 * TODO: add cache invalidate operation when the size of
474 * the DMA buffer is known
477 return nvme_submit_admin_cmd(dev, &c, result);
480 int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
481 dma_addr_t dma_addr, u32 *result)
483 struct nvme_command c;
485 memset(&c, 0, sizeof(c));
486 c.features.opcode = nvme_admin_set_features;
487 c.features.prp1 = cpu_to_le64(dma_addr);
488 c.features.fid = cpu_to_le32(fid);
489 c.features.dword11 = cpu_to_le32(dword11);
492 * TODO: add cache flush operation when the size of
493 * the DMA buffer is known
496 return nvme_submit_admin_cmd(dev, &c, result);
499 static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
501 struct nvme_dev *dev = nvmeq->dev;
504 nvmeq->cq_vector = qid - 1;
505 result = nvme_alloc_cq(dev, qid, nvmeq);
509 result = nvme_alloc_sq(dev, qid, nvmeq);
513 nvme_init_queue(nvmeq, qid);
518 nvme_delete_sq(dev, qid);
520 nvme_delete_cq(dev, qid);
525 static int nvme_set_queue_count(struct nvme_dev *dev, int count)
529 u32 q_count = (count - 1) | ((count - 1) << 16);
531 status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES,
532 q_count, 0, &result);
539 return min(result & 0xffff, result >> 16) + 1;
542 static void nvme_create_io_queues(struct nvme_dev *dev)
546 for (i = dev->queue_count; i <= dev->max_qid; i++)
547 if (!nvme_alloc_queue(dev, i, dev->q_depth))
550 for (i = dev->online_queues; i <= dev->queue_count - 1; i++)
551 if (nvme_create_queue(dev->queues[i], i))
555 static int nvme_setup_io_queues(struct nvme_dev *dev)
561 result = nvme_set_queue_count(dev, nr_io_queues);
565 if (result < nr_io_queues)
566 nr_io_queues = result;
568 dev->max_qid = nr_io_queues;
570 /* Free previously allocated queues */
571 nvme_free_queues(dev, nr_io_queues + 1);
572 nvme_create_io_queues(dev);
577 static int nvme_get_info_from_identify(struct nvme_dev *dev)
579 ALLOC_CACHE_ALIGN_BUFFER(char, buf, sizeof(struct nvme_id_ctrl));
580 struct nvme_id_ctrl *ctrl = (struct nvme_id_ctrl *)buf;
582 int shift = NVME_CAP_MPSMIN(dev->cap) + 12;
584 ret = nvme_identify(dev, 0, 1, (dma_addr_t)ctrl);
588 dev->nn = le32_to_cpu(ctrl->nn);
589 dev->vwc = ctrl->vwc;
590 memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
591 memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
592 memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
594 dev->max_transfer_shift = (ctrl->mdts + shift);
597 * Maximum Data Transfer Size (MDTS) field indicates the maximum
598 * data transfer size between the host and the controller. The
599 * host should not submit a command that exceeds this transfer
600 * size. The value is in units of the minimum memory page size
601 * and is reported as a power of two (2^n).
603 * The spec also says: a value of 0h indicates no restrictions
604 * on transfer size. But in nvme_blk_read/write() below we have
605 * the following algorithm for maximum number of logic blocks
608 * u16 lbas = 1 << (dev->max_transfer_shift - ns->lba_shift);
610 * In order for lbas not to overflow, the maximum number is 15
611 * which means dev->max_transfer_shift = 15 + 9 (ns->lba_shift).
612 * Let's use 20 which provides 1MB size.
614 dev->max_transfer_shift = 20;
620 int nvme_scan_namespace(void)
626 ret = uclass_get(UCLASS_NVME, &uc);
630 uclass_foreach_dev(dev, uc) {
631 ret = device_probe(dev);
639 static int nvme_blk_probe(struct udevice *udev)
641 struct nvme_dev *ndev = dev_get_priv(udev->parent);
642 struct blk_desc *desc = dev_get_uclass_platdata(udev);
643 struct nvme_ns *ns = dev_get_priv(udev);
645 ALLOC_CACHE_ALIGN_BUFFER(char, buf, sizeof(struct nvme_id_ns));
646 struct nvme_id_ns *id = (struct nvme_id_ns *)buf;
647 struct pci_child_platdata *pplat;
649 memset(ns, 0, sizeof(*ns));
651 /* extract the namespace id from the block device name */
652 ns->ns_id = trailing_strtol(udev->name) + 1;
653 if (nvme_identify(ndev, ns->ns_id, 0, (dma_addr_t)id))
656 flbas = id->flbas & NVME_NS_FLBAS_LBA_MASK;
658 ns->lba_shift = id->lbaf[flbas].ds;
659 ns->mode_select_num_blocks = le64_to_cpu(id->nsze);
660 ns->mode_select_block_len = 1 << ns->lba_shift;
661 list_add(&ns->list, &ndev->namespaces);
663 desc->lba = ns->mode_select_num_blocks;
664 desc->log2blksz = ns->lba_shift;
665 desc->blksz = 1 << ns->lba_shift;
667 pplat = dev_get_parent_platdata(udev->parent);
668 sprintf(desc->vendor, "0x%.4x", pplat->vendor);
669 memcpy(desc->product, ndev->serial, sizeof(ndev->serial));
670 memcpy(desc->revision, ndev->firmware_rev, sizeof(ndev->firmware_rev));
676 static ulong nvme_blk_rw(struct udevice *udev, lbaint_t blknr,
677 lbaint_t blkcnt, void *buffer, bool read)
679 struct nvme_ns *ns = dev_get_priv(udev);
680 struct nvme_dev *dev = ns->dev;
681 struct nvme_command c;
682 struct blk_desc *desc = dev_get_uclass_platdata(udev);
685 u64 total_len = blkcnt << desc->log2blksz;
686 u64 temp_len = total_len;
689 u16 lbas = 1 << (dev->max_transfer_shift - ns->lba_shift);
690 u64 total_lbas = blkcnt;
693 flush_dcache_range((unsigned long)buffer,
694 (unsigned long)buffer + total_len);
696 c.rw.opcode = read ? nvme_cmd_read : nvme_cmd_write;
698 c.rw.nsid = cpu_to_le32(ns->ns_id);
707 if (total_lbas < lbas) {
708 lbas = (u16)total_lbas;
714 if (nvme_setup_prps(dev, &prp2,
715 lbas << ns->lba_shift, (ulong)buffer))
717 c.rw.slba = cpu_to_le64(slba);
719 c.rw.length = cpu_to_le16(lbas - 1);
720 c.rw.prp1 = cpu_to_le64((ulong)buffer);
721 c.rw.prp2 = cpu_to_le64(prp2);
722 status = nvme_submit_sync_cmd(dev->queues[NVME_IO_Q],
723 &c, NULL, IO_TIMEOUT);
726 temp_len -= lbas << ns->lba_shift;
727 buffer += lbas << ns->lba_shift;
731 invalidate_dcache_range((unsigned long)buffer,
732 (unsigned long)buffer + total_len);
734 return (total_len - temp_len) >> desc->log2blksz;
737 static ulong nvme_blk_read(struct udevice *udev, lbaint_t blknr,
738 lbaint_t blkcnt, void *buffer)
740 return nvme_blk_rw(udev, blknr, blkcnt, buffer, true);
743 static ulong nvme_blk_write(struct udevice *udev, lbaint_t blknr,
744 lbaint_t blkcnt, const void *buffer)
746 return nvme_blk_rw(udev, blknr, blkcnt, (void *)buffer, false);
749 static const struct blk_ops nvme_blk_ops = {
750 .read = nvme_blk_read,
751 .write = nvme_blk_write,
754 U_BOOT_DRIVER(nvme_blk) = {
757 .probe = nvme_blk_probe,
758 .ops = &nvme_blk_ops,
759 .priv_auto_alloc_size = sizeof(struct nvme_ns),
762 static int nvme_bind(struct udevice *udev)
767 sprintf(name, "nvme#%d", ndev_num++);
769 return device_set_name(udev, name);
772 static int nvme_probe(struct udevice *udev)
775 struct nvme_dev *ndev = dev_get_priv(udev);
777 ndev->instance = trailing_strtol(udev->name);
779 INIT_LIST_HEAD(&ndev->namespaces);
780 ndev->bar = dm_pci_map_bar(udev, PCI_BASE_ADDRESS_0,
782 if (readl(&ndev->bar->csts) == -1) {
784 printf("Error: %s: Out of memory!\n", udev->name);
788 ndev->queues = malloc(NVME_Q_NUM * sizeof(struct nvme_queue *));
791 printf("Error: %s: Out of memory!\n", udev->name);
794 memset(ndev->queues, 0,
795 sizeof(NVME_Q_NUM * sizeof(struct nvme_queue *)));
797 ndev->prp_pool = malloc(MAX_PRP_POOL);
798 if (!ndev->prp_pool) {
800 printf("Error: %s: Out of memory!\n", udev->name);
803 ndev->prp_entry_num = MAX_PRP_POOL >> 3;
805 ndev->cap = nvme_readq(&ndev->bar->cap);
806 ndev->q_depth = min_t(int, NVME_CAP_MQES(ndev->cap) + 1, NVME_Q_DEPTH);
807 ndev->db_stride = 1 << NVME_CAP_STRIDE(ndev->cap);
808 ndev->dbs = ((void __iomem *)ndev->bar) + 4096;
810 ret = nvme_configure_admin_queue(ndev);
814 ret = nvme_setup_io_queues(ndev);
818 nvme_get_info_from_identify(ndev);
823 free((void *)ndev->queues);
828 U_BOOT_DRIVER(nvme) = {
833 .priv_auto_alloc_size = sizeof(struct nvme_dev),
836 struct pci_device_id nvme_supported[] = {
837 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, ~0) },
841 U_BOOT_PCI_DEVICE(nvme, nvme_supported);