]> git.sur5r.net Git - u-boot/blob - drivers/nvme/nvme.c
nvme: Consolidate block read and write routines
[u-boot] / drivers / nvme / nvme.c
1 /*
2  * Copyright (C) 2017 NXP Semiconductors
3  * Copyright (C) 2017 Bin Meng <bmeng.cn@gmail.com>
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 #include <common.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <memalign.h>
12 #include <pci.h>
13 #include <dm/device-internal.h>
14 #include "nvme.h"
15
16 struct nvme_info *nvme_info;
17
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
23 #define IO_TIMEOUT              30
24 #define MAX_PRP_POOL            512
25
26 enum nvme_queue_id {
27         NVME_ADMIN_Q,
28         NVME_IO_Q,
29         NVME_Q_NUM,
30 };
31
32 /*
33  * An NVM Express queue. Each device has at least two (one for admin
34  * commands and one for I/O commands).
35  */
36 struct nvme_queue {
37         struct nvme_dev *dev;
38         struct nvme_command *sq_cmds;
39         struct nvme_completion *cqes;
40         wait_queue_head_t sq_full;
41         u32 __iomem *q_db;
42         u16 q_depth;
43         s16 cq_vector;
44         u16 sq_head;
45         u16 sq_tail;
46         u16 cq_head;
47         u16 qid;
48         u8 cq_phase;
49         u8 cqe_seen;
50         unsigned long cmdid_data[];
51 };
52
53 static int nvme_wait_ready(struct nvme_dev *dev, bool enabled)
54 {
55         u32 bit = enabled ? NVME_CSTS_RDY : 0;
56         int timeout;
57         ulong start;
58
59         /* Timeout field in the CAP register is in 500 millisecond units */
60         timeout = NVME_CAP_TIMEOUT(dev->cap) * 500;
61
62         start = get_timer(0);
63         while (get_timer(start) < timeout) {
64                 if ((readl(&dev->bar->csts) & NVME_CSTS_RDY) == bit)
65                         return 0;
66         }
67
68         return -ETIME;
69 }
70
71 static int nvme_setup_prps(struct nvme_dev *dev, u64 *prp2,
72                            int total_len, u64 dma_addr)
73 {
74         u32 page_size = dev->page_size;
75         int offset = dma_addr & (page_size - 1);
76         u64 *prp_pool;
77         int length = total_len;
78         int i, nprps;
79         length -= (page_size - offset);
80
81         if (length <= 0) {
82                 *prp2 = 0;
83                 return 0;
84         }
85
86         if (length)
87                 dma_addr += (page_size - offset);
88
89         if (length <= page_size) {
90                 *prp2 = dma_addr;
91                 return 0;
92         }
93
94         nprps = DIV_ROUND_UP(length, page_size);
95
96         if (nprps > dev->prp_entry_num) {
97                 free(dev->prp_pool);
98                 dev->prp_pool = malloc(nprps << 3);
99                 if (!dev->prp_pool) {
100                         printf("Error: malloc prp_pool fail\n");
101                         return -ENOMEM;
102                 }
103                 dev->prp_entry_num = nprps;
104         }
105
106         prp_pool = dev->prp_pool;
107         i = 0;
108         while (nprps) {
109                 if (i == ((page_size >> 3) - 1)) {
110                         *(prp_pool + i) = cpu_to_le64((ulong)prp_pool +
111                                         page_size);
112                         i = 0;
113                         prp_pool += page_size;
114                 }
115                 *(prp_pool + i++) = cpu_to_le64(dma_addr);
116                 dma_addr += page_size;
117                 nprps--;
118         }
119         *prp2 = (ulong)dev->prp_pool;
120
121         return 0;
122 }
123
124 static __le16 nvme_get_cmd_id(void)
125 {
126         static unsigned short cmdid;
127
128         return cpu_to_le16((cmdid < USHRT_MAX) ? cmdid++ : 0);
129 }
130
131 static u16 nvme_read_completion_status(struct nvme_queue *nvmeq, u16 index)
132 {
133         u64 start = (ulong)&nvmeq->cqes[index];
134         u64 stop = start + sizeof(struct nvme_completion);
135
136         invalidate_dcache_range(start, stop);
137
138         return le16_to_cpu(readw(&(nvmeq->cqes[index].status)));
139 }
140
141 /**
142  * nvme_submit_cmd() - copy a command into a queue and ring the doorbell
143  *
144  * @nvmeq:      The queue to use
145  * @cmd:        The command to send
146  */
147 static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
148 {
149         u16 tail = nvmeq->sq_tail;
150
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));
154
155         if (++tail == nvmeq->q_depth)
156                 tail = 0;
157         writel(tail, nvmeq->q_db);
158         nvmeq->sq_tail = tail;
159 }
160
161 static int nvme_submit_sync_cmd(struct nvme_queue *nvmeq,
162                                 struct nvme_command *cmd,
163                                 u32 *result, unsigned timeout)
164 {
165         u16 head = nvmeq->cq_head;
166         u16 phase = nvmeq->cq_phase;
167         u16 status;
168         ulong start_time;
169         ulong timeout_us = timeout * 100000;
170
171         cmd->common.command_id = nvme_get_cmd_id();
172         nvme_submit_cmd(nvmeq, cmd);
173
174         start_time = timer_get_us();
175
176         for (;;) {
177                 status = nvme_read_completion_status(nvmeq, head);
178                 if ((status & 0x01) == phase)
179                         break;
180                 if (timeout_us > 0 && (timer_get_us() - start_time)
181                     >= timeout_us)
182                         return -ETIMEDOUT;
183         }
184
185         status >>= 1;
186         if (status) {
187                 printf("ERROR: status = %x, phase = %d, head = %d\n",
188                        status, phase, head);
189                 status = 0;
190                 if (++head == nvmeq->q_depth) {
191                         head = 0;
192                         phase = !phase;
193                 }
194                 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
195                 nvmeq->cq_head = head;
196                 nvmeq->cq_phase = phase;
197
198                 return -EIO;
199         }
200
201         if (result)
202                 *result = le32_to_cpu(readl(&(nvmeq->cqes[head].result)));
203
204         if (++head == nvmeq->q_depth) {
205                 head = 0;
206                 phase = !phase;
207         }
208         writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
209         nvmeq->cq_head = head;
210         nvmeq->cq_phase = phase;
211
212         return status;
213 }
214
215 static int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
216                                  u32 *result)
217 {
218         return nvme_submit_sync_cmd(dev->queues[NVME_ADMIN_Q], cmd,
219                                     result, ADMIN_TIMEOUT);
220 }
221
222 static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev,
223                                            int qid, int depth)
224 {
225         struct nvme_queue *nvmeq = malloc(sizeof(*nvmeq));
226         if (!nvmeq)
227                 return NULL;
228         memset(nvmeq, 0, sizeof(*nvmeq));
229
230         nvmeq->cqes = (void *)memalign(4096, NVME_CQ_SIZE(depth));
231         if (!nvmeq->cqes)
232                 goto free_nvmeq;
233         memset((void *)nvmeq->cqes, 0, NVME_CQ_SIZE(depth));
234
235         nvmeq->sq_cmds = (void *)memalign(4096, NVME_SQ_SIZE(depth));
236         if (!nvmeq->sq_cmds)
237                 goto free_queue;
238         memset((void *)nvmeq->sq_cmds, 0, NVME_SQ_SIZE(depth));
239
240         nvmeq->dev = dev;
241
242         nvmeq->cq_head = 0;
243         nvmeq->cq_phase = 1;
244         nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
245         nvmeq->q_depth = depth;
246         nvmeq->qid = qid;
247         dev->queue_count++;
248         dev->queues[qid] = nvmeq;
249
250         return nvmeq;
251
252  free_queue:
253         free((void *)nvmeq->cqes);
254  free_nvmeq:
255         free(nvmeq);
256
257         return NULL;
258 }
259
260 static int nvme_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
261 {
262         struct nvme_command c;
263
264         memset(&c, 0, sizeof(c));
265         c.delete_queue.opcode = opcode;
266         c.delete_queue.qid = cpu_to_le16(id);
267
268         return nvme_submit_admin_cmd(dev, &c, NULL);
269 }
270
271 static int nvme_delete_sq(struct nvme_dev *dev, u16 sqid)
272 {
273         return nvme_delete_queue(dev, nvme_admin_delete_sq, sqid);
274 }
275
276 static int nvme_delete_cq(struct nvme_dev *dev, u16 cqid)
277 {
278         return nvme_delete_queue(dev, nvme_admin_delete_cq, cqid);
279 }
280
281 static int nvme_enable_ctrl(struct nvme_dev *dev)
282 {
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);
286
287         return nvme_wait_ready(dev, true);
288 }
289
290 static int nvme_disable_ctrl(struct nvme_dev *dev)
291 {
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);
295
296         return nvme_wait_ready(dev, false);
297 }
298
299 static void nvme_free_queue(struct nvme_queue *nvmeq)
300 {
301         free((void *)nvmeq->cqes);
302         free(nvmeq->sq_cmds);
303         free(nvmeq);
304 }
305
306 static void nvme_free_queues(struct nvme_dev *dev, int lowest)
307 {
308         int i;
309
310         for (i = dev->queue_count - 1; i >= lowest; i--) {
311                 struct nvme_queue *nvmeq = dev->queues[i];
312                 dev->queue_count--;
313                 dev->queues[i] = NULL;
314                 nvme_free_queue(nvmeq);
315         }
316 }
317
318 static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
319 {
320         struct nvme_dev *dev = nvmeq->dev;
321
322         nvmeq->sq_tail = 0;
323         nvmeq->cq_head = 0;
324         nvmeq->cq_phase = 1;
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++;
330 }
331
332 static int nvme_configure_admin_queue(struct nvme_dev *dev)
333 {
334         int result;
335         u32 aqa;
336         u64 cap = dev->cap;
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;
342
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);
346                 return -ENODEV;
347         }
348
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;
353         }
354
355         result = nvme_disable_ctrl(dev);
356         if (result < 0)
357                 return result;
358
359         nvmeq = dev->queues[NVME_ADMIN_Q];
360         if (!nvmeq) {
361                 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
362                 if (!nvmeq)
363                         return -ENOMEM;
364         }
365
366         aqa = nvmeq->q_depth - 1;
367         aqa |= aqa << 16;
368         aqa |= aqa << 16;
369
370         dev->page_size = 1 << page_shift;
371
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;
376
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);
380
381         result = nvme_enable_ctrl(dev);
382         if (result)
383                 goto free_nvmeq;
384
385         nvmeq->cq_vector = 0;
386
387         nvme_init_queue(dev->queues[NVME_ADMIN_Q], 0);
388
389         return result;
390
391  free_nvmeq:
392         nvme_free_queues(dev, 0);
393
394         return result;
395 }
396
397 static int nvme_alloc_cq(struct nvme_dev *dev, u16 qid,
398                             struct nvme_queue *nvmeq)
399 {
400         struct nvme_command c;
401         int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
402
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);
410
411         return nvme_submit_admin_cmd(dev, &c, NULL);
412 }
413
414 static int nvme_alloc_sq(struct nvme_dev *dev, u16 qid,
415                             struct nvme_queue *nvmeq)
416 {
417         struct nvme_command c;
418         int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
419
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);
427
428         return nvme_submit_admin_cmd(dev, &c, NULL);
429 }
430
431 int nvme_identify(struct nvme_dev *dev, unsigned nsid,
432                   unsigned cns, dma_addr_t dma_addr)
433 {
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);
438
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);
443
444         length -= (page_size - offset);
445         if (length <= 0) {
446                 c.identify.prp2 = 0;
447         } else {
448                 dma_addr += (page_size - offset);
449                 c.identify.prp2 = cpu_to_le64(dma_addr);
450         }
451
452         c.identify.cns = cpu_to_le32(cns);
453
454         return nvme_submit_admin_cmd(dev, &c, NULL);
455 }
456
457 int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
458                       dma_addr_t dma_addr, u32 *result)
459 {
460         struct nvme_command c;
461
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);
467
468         return nvme_submit_admin_cmd(dev, &c, result);
469 }
470
471 int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
472                       dma_addr_t dma_addr, u32 *result)
473 {
474         struct nvme_command c;
475
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);
481
482         return nvme_submit_admin_cmd(dev, &c, result);
483 }
484
485 static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
486 {
487         struct nvme_dev *dev = nvmeq->dev;
488         int result;
489
490         nvmeq->cq_vector = qid - 1;
491         result = nvme_alloc_cq(dev, qid, nvmeq);
492         if (result < 0)
493                 goto release_cq;
494
495         result = nvme_alloc_sq(dev, qid, nvmeq);
496         if (result < 0)
497                 goto release_sq;
498
499         nvme_init_queue(nvmeq, qid);
500
501         return result;
502
503  release_sq:
504         nvme_delete_sq(dev, qid);
505  release_cq:
506         nvme_delete_cq(dev, qid);
507
508         return result;
509 }
510
511 static int nvme_set_queue_count(struct nvme_dev *dev, int count)
512 {
513         int status;
514         u32 result;
515         u32 q_count = (count - 1) | ((count - 1) << 16);
516
517         status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES,
518                         q_count, 0, &result);
519
520         if (status < 0)
521                 return status;
522         if (status > 1)
523                 return 0;
524
525         return min(result & 0xffff, result >> 16) + 1;
526 }
527
528 static void nvme_create_io_queues(struct nvme_dev *dev)
529 {
530         unsigned int i;
531
532         for (i = dev->queue_count; i <= dev->max_qid; i++)
533                 if (!nvme_alloc_queue(dev, i, dev->q_depth))
534                         break;
535
536         for (i = dev->online_queues; i <= dev->queue_count - 1; i++)
537                 if (nvme_create_queue(dev->queues[i], i))
538                         break;
539 }
540
541 static int nvme_setup_io_queues(struct nvme_dev *dev)
542 {
543         int nr_io_queues;
544         int result;
545
546         nr_io_queues = 1;
547         result = nvme_set_queue_count(dev, nr_io_queues);
548         if (result <= 0)
549                 return result;
550
551         if (result < nr_io_queues)
552                 nr_io_queues = result;
553
554         dev->max_qid = nr_io_queues;
555
556         /* Free previously allocated queues */
557         nvme_free_queues(dev, nr_io_queues + 1);
558         nvme_create_io_queues(dev);
559
560         return 0;
561 }
562
563 static int nvme_get_info_from_identify(struct nvme_dev *dev)
564 {
565         struct nvme_id_ctrl buf, *ctrl = &buf;
566         int ret;
567         int shift = NVME_CAP_MPSMIN(dev->cap) + 12;
568
569         ret = nvme_identify(dev, 0, 1, (dma_addr_t)ctrl);
570         if (ret)
571                 return -EIO;
572
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));
578         if (ctrl->mdts)
579                 dev->max_transfer_shift = (ctrl->mdts + shift);
580         else {
581                 /*
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).
587                  *
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
591                  * per transfer:
592                  *
593                  * u16 lbas = 1 << (dev->max_transfer_shift - ns->lba_shift);
594                  *
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.
598                  */
599                 dev->max_transfer_shift = 20;
600         }
601
602         return 0;
603 }
604
605 int nvme_scan_namespace(void)
606 {
607         struct uclass *uc;
608         struct udevice *dev;
609         int ret;
610
611         ret = uclass_get(UCLASS_NVME, &uc);
612         if (ret)
613                 return ret;
614
615         uclass_foreach_dev(dev, uc) {
616                 ret = device_probe(dev);
617                 if (ret)
618                         return ret;
619         }
620
621         return 0;
622 }
623
624 static int nvme_blk_probe(struct udevice *udev)
625 {
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);
629         u8 flbas;
630         struct nvme_id_ns buf, *id = &buf;
631         struct pci_child_platdata *pplat;
632
633         memset(ns, 0, sizeof(*ns));
634         ns->dev = ndev;
635         ns->ns_id = desc->devnum - ndev->blk_dev_start + 1;
636         if (nvme_identify(ndev, ns->ns_id, 0, (dma_addr_t)id))
637                 return -EIO;
638
639         flbas = id->flbas & NVME_NS_FLBAS_LBA_MASK;
640         ns->flbas = flbas;
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);
645
646         desc->lba = ns->mode_select_num_blocks;
647         desc->log2blksz = ns->lba_shift;
648         desc->blksz = 1 << ns->lba_shift;
649         desc->bdev = udev;
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));
654         part_init(desc);
655
656         return 0;
657 }
658
659 static ulong nvme_blk_rw(struct udevice *udev, lbaint_t blknr,
660                          lbaint_t blkcnt, void *buffer, bool read)
661 {
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);
666         int status;
667         u64 prp2;
668         u64 total_len = blkcnt << desc->log2blksz;
669         u64 temp_len = total_len;
670
671         u64 slba = blknr;
672         u16 lbas = 1 << (dev->max_transfer_shift - ns->lba_shift);
673         u64 total_lbas = blkcnt;
674
675         c.rw.opcode = read ? nvme_cmd_read : nvme_cmd_write;
676         c.rw.flags = 0;
677         c.rw.nsid = cpu_to_le32(ns->ns_id);
678         c.rw.control = 0;
679         c.rw.dsmgmt = 0;
680         c.rw.reftag = 0;
681         c.rw.apptag = 0;
682         c.rw.appmask = 0;
683         c.rw.metadata = 0;
684
685         while (total_lbas) {
686                 if (total_lbas < lbas) {
687                         lbas = (u16)total_lbas;
688                         total_lbas = 0;
689                 } else {
690                         total_lbas -= lbas;
691                 }
692
693                 if (nvme_setup_prps(dev, &prp2,
694                                     lbas << ns->lba_shift, (ulong)buffer))
695                         return -EIO;
696                 c.rw.slba = cpu_to_le64(slba);
697                 slba += lbas;
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);
703                 if (status)
704                         break;
705                 temp_len -= lbas << ns->lba_shift;
706                 buffer += lbas << ns->lba_shift;
707         }
708
709         return (total_len - temp_len) >> desc->log2blksz;
710 }
711
712 static ulong nvme_blk_read(struct udevice *udev, lbaint_t blknr,
713                            lbaint_t blkcnt, void *buffer)
714 {
715         return nvme_blk_rw(udev, blknr, blkcnt, buffer, true);
716 }
717
718 static ulong nvme_blk_write(struct udevice *udev, lbaint_t blknr,
719                             lbaint_t blkcnt, const void *buffer)
720 {
721         return nvme_blk_rw(udev, blknr, blkcnt, (void *)buffer, false);
722 }
723
724 static const struct blk_ops nvme_blk_ops = {
725         .read   = nvme_blk_read,
726         .write  = nvme_blk_write,
727 };
728
729 U_BOOT_DRIVER(nvme_blk) = {
730         .name   = "nvme-blk",
731         .id     = UCLASS_BLK,
732         .probe  = nvme_blk_probe,
733         .ops    = &nvme_blk_ops,
734         .priv_auto_alloc_size = sizeof(struct nvme_ns),
735 };
736
737 static int nvme_bind(struct udevice *udev)
738 {
739         char name[20];
740         sprintf(name, "nvme#%d", nvme_info->ndev_num++);
741
742         return device_set_name(udev, name);
743 }
744
745 static int nvme_probe(struct udevice *udev)
746 {
747         int ret;
748         struct nvme_dev *ndev = dev_get_priv(udev);
749
750         ndev->instance = trailing_strtol(udev->name);
751
752         INIT_LIST_HEAD(&ndev->namespaces);
753         ndev->bar = dm_pci_map_bar(udev, PCI_BASE_ADDRESS_0,
754                         PCI_REGION_MEM);
755         if (readl(&ndev->bar->csts) == -1) {
756                 ret = -ENODEV;
757                 printf("Error: %s: Out of memory!\n", udev->name);
758                 goto free_nvme;
759         }
760
761         ndev->queues = malloc(NVME_Q_NUM * sizeof(struct nvme_queue *));
762         if (!ndev->queues) {
763                 ret = -ENOMEM;
764                 printf("Error: %s: Out of memory!\n", udev->name);
765                 goto free_nvme;
766         }
767         memset(ndev->queues, 0,
768                sizeof(NVME_Q_NUM * sizeof(struct nvme_queue *)));
769
770         ndev->prp_pool = malloc(MAX_PRP_POOL);
771         if (!ndev->prp_pool) {
772                 ret = -ENOMEM;
773                 printf("Error: %s: Out of memory!\n", udev->name);
774                 goto free_nvme;
775         }
776         ndev->prp_entry_num = MAX_PRP_POOL >> 3;
777
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;
782
783         ret = nvme_configure_admin_queue(ndev);
784         if (ret)
785                 goto free_queue;
786
787         ret = nvme_setup_io_queues(ndev);
788         if (ret)
789                 goto free_queue;
790
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);
794
795         return 0;
796
797 free_queue:
798         free((void *)ndev->queues);
799 free_nvme:
800         return ret;
801 }
802
803 U_BOOT_DRIVER(nvme) = {
804         .name   = "nvme",
805         .id     = UCLASS_NVME,
806         .bind   = nvme_bind,
807         .probe  = nvme_probe,
808         .priv_auto_alloc_size = sizeof(struct nvme_dev),
809 };
810
811 struct pci_device_id nvme_supported[] = {
812         { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, ~0) },
813         {}
814 };
815
816 U_BOOT_PCI_DEVICE(nvme, nvme_supported);