]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
982388ea ZZ |
2 | /* |
3 | * Copyright (C) 2017 NXP Semiconductors | |
4 | * Copyright (C) 2017 Bin Meng <[email protected]> | |
982388ea ZZ |
5 | */ |
6 | ||
7 | #include <common.h> | |
8 | #include <dm.h> | |
9 | #include <errno.h> | |
10 | #include <memalign.h> | |
11 | #include <pci.h> | |
1045315d | 12 | #include <time.h> |
982388ea ZZ |
13 | #include <dm/device-internal.h> |
14 | #include "nvme.h" | |
15 | ||
982388ea ZZ |
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 | |
21 | #define IO_TIMEOUT 30 | |
22 | #define MAX_PRP_POOL 512 | |
23 | ||
722e668d BM |
24 | enum nvme_queue_id { |
25 | NVME_ADMIN_Q, | |
26 | NVME_IO_Q, | |
27 | NVME_Q_NUM, | |
28 | }; | |
29 | ||
982388ea ZZ |
30 | /* |
31 | * An NVM Express queue. Each device has at least two (one for admin | |
32 | * commands and one for I/O commands). | |
33 | */ | |
34 | struct nvme_queue { | |
35 | struct nvme_dev *dev; | |
36 | struct nvme_command *sq_cmds; | |
37 | struct nvme_completion *cqes; | |
38 | wait_queue_head_t sq_full; | |
39 | u32 __iomem *q_db; | |
40 | u16 q_depth; | |
41 | s16 cq_vector; | |
42 | u16 sq_head; | |
43 | u16 sq_tail; | |
44 | u16 cq_head; | |
45 | u16 qid; | |
46 | u8 cq_phase; | |
47 | u8 cqe_seen; | |
48 | unsigned long cmdid_data[]; | |
49 | }; | |
50 | ||
51 | static int nvme_wait_ready(struct nvme_dev *dev, bool enabled) | |
52 | { | |
53 | u32 bit = enabled ? NVME_CSTS_RDY : 0; | |
04d2a384 BM |
54 | int timeout; |
55 | ulong start; | |
982388ea | 56 | |
04d2a384 BM |
57 | /* Timeout field in the CAP register is in 500 millisecond units */ |
58 | timeout = NVME_CAP_TIMEOUT(dev->cap) * 500; | |
982388ea | 59 | |
04d2a384 BM |
60 | start = get_timer(0); |
61 | while (get_timer(start) < timeout) { | |
62 | if ((readl(&dev->bar->csts) & NVME_CSTS_RDY) == bit) | |
63 | return 0; | |
64 | } | |
65 | ||
66 | return -ETIME; | |
982388ea ZZ |
67 | } |
68 | ||
69 | static int nvme_setup_prps(struct nvme_dev *dev, u64 *prp2, | |
70 | int total_len, u64 dma_addr) | |
71 | { | |
72 | u32 page_size = dev->page_size; | |
73 | int offset = dma_addr & (page_size - 1); | |
74 | u64 *prp_pool; | |
75 | int length = total_len; | |
76 | int i, nprps; | |
b21dcebf AW |
77 | u32 prps_per_page = (page_size >> 3) - 1; |
78 | u32 num_pages; | |
79 | ||
982388ea ZZ |
80 | length -= (page_size - offset); |
81 | ||
82 | if (length <= 0) { | |
83 | *prp2 = 0; | |
84 | return 0; | |
85 | } | |
86 | ||
87 | if (length) | |
88 | dma_addr += (page_size - offset); | |
89 | ||
90 | if (length <= page_size) { | |
91 | *prp2 = dma_addr; | |
92 | return 0; | |
93 | } | |
94 | ||
95 | nprps = DIV_ROUND_UP(length, page_size); | |
b21dcebf | 96 | num_pages = DIV_ROUND_UP(nprps, prps_per_page); |
982388ea ZZ |
97 | |
98 | if (nprps > dev->prp_entry_num) { | |
99 | free(dev->prp_pool); | |
b21dcebf AW |
100 | /* |
101 | * Always increase in increments of pages. It doesn't waste | |
102 | * much memory and reduces the number of allocations. | |
103 | */ | |
104 | dev->prp_pool = memalign(page_size, num_pages * page_size); | |
982388ea ZZ |
105 | if (!dev->prp_pool) { |
106 | printf("Error: malloc prp_pool fail\n"); | |
107 | return -ENOMEM; | |
108 | } | |
b21dcebf | 109 | dev->prp_entry_num = prps_per_page * num_pages; |
982388ea ZZ |
110 | } |
111 | ||
112 | prp_pool = dev->prp_pool; | |
113 | i = 0; | |
114 | while (nprps) { | |
115 | if (i == ((page_size >> 3) - 1)) { | |
116 | *(prp_pool + i) = cpu_to_le64((ulong)prp_pool + | |
117 | page_size); | |
118 | i = 0; | |
119 | prp_pool += page_size; | |
120 | } | |
121 | *(prp_pool + i++) = cpu_to_le64(dma_addr); | |
122 | dma_addr += page_size; | |
123 | nprps--; | |
124 | } | |
125 | *prp2 = (ulong)dev->prp_pool; | |
126 | ||
8c403402 PW |
127 | flush_dcache_range((ulong)dev->prp_pool, (ulong)dev->prp_pool + |
128 | dev->prp_entry_num * sizeof(u64)); | |
129 | ||
982388ea ZZ |
130 | return 0; |
131 | } | |
132 | ||
133 | static __le16 nvme_get_cmd_id(void) | |
134 | { | |
135 | static unsigned short cmdid; | |
136 | ||
137 | return cpu_to_le16((cmdid < USHRT_MAX) ? cmdid++ : 0); | |
138 | } | |
139 | ||
140 | static u16 nvme_read_completion_status(struct nvme_queue *nvmeq, u16 index) | |
141 | { | |
142 | u64 start = (ulong)&nvmeq->cqes[index]; | |
143 | u64 stop = start + sizeof(struct nvme_completion); | |
144 | ||
145 | invalidate_dcache_range(start, stop); | |
146 | ||
147 | return le16_to_cpu(readw(&(nvmeq->cqes[index].status))); | |
148 | } | |
149 | ||
150 | /** | |
151 | * nvme_submit_cmd() - copy a command into a queue and ring the doorbell | |
152 | * | |
153 | * @nvmeq: The queue to use | |
154 | * @cmd: The command to send | |
155 | */ | |
156 | static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd) | |
157 | { | |
158 | u16 tail = nvmeq->sq_tail; | |
159 | ||
160 | memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd)); | |
161 | flush_dcache_range((ulong)&nvmeq->sq_cmds[tail], | |
162 | (ulong)&nvmeq->sq_cmds[tail] + sizeof(*cmd)); | |
163 | ||
164 | if (++tail == nvmeq->q_depth) | |
165 | tail = 0; | |
166 | writel(tail, nvmeq->q_db); | |
167 | nvmeq->sq_tail = tail; | |
168 | } | |
169 | ||
170 | static int nvme_submit_sync_cmd(struct nvme_queue *nvmeq, | |
171 | struct nvme_command *cmd, | |
172 | u32 *result, unsigned timeout) | |
173 | { | |
174 | u16 head = nvmeq->cq_head; | |
175 | u16 phase = nvmeq->cq_phase; | |
176 | u16 status; | |
177 | ulong start_time; | |
178 | ulong timeout_us = timeout * 100000; | |
179 | ||
180 | cmd->common.command_id = nvme_get_cmd_id(); | |
181 | nvme_submit_cmd(nvmeq, cmd); | |
182 | ||
183 | start_time = timer_get_us(); | |
184 | ||
185 | for (;;) { | |
186 | status = nvme_read_completion_status(nvmeq, head); | |
187 | if ((status & 0x01) == phase) | |
188 | break; | |
189 | if (timeout_us > 0 && (timer_get_us() - start_time) | |
190 | >= timeout_us) | |
191 | return -ETIMEDOUT; | |
192 | } | |
193 | ||
194 | status >>= 1; | |
195 | if (status) { | |
196 | printf("ERROR: status = %x, phase = %d, head = %d\n", | |
197 | status, phase, head); | |
198 | status = 0; | |
199 | if (++head == nvmeq->q_depth) { | |
200 | head = 0; | |
201 | phase = !phase; | |
202 | } | |
203 | writel(head, nvmeq->q_db + nvmeq->dev->db_stride); | |
204 | nvmeq->cq_head = head; | |
205 | nvmeq->cq_phase = phase; | |
206 | ||
207 | return -EIO; | |
208 | } | |
209 | ||
210 | if (result) | |
211 | *result = le32_to_cpu(readl(&(nvmeq->cqes[head].result))); | |
212 | ||
213 | if (++head == nvmeq->q_depth) { | |
214 | head = 0; | |
215 | phase = !phase; | |
216 | } | |
217 | writel(head, nvmeq->q_db + nvmeq->dev->db_stride); | |
218 | nvmeq->cq_head = head; | |
219 | nvmeq->cq_phase = phase; | |
220 | ||
221 | return status; | |
222 | } | |
223 | ||
224 | static int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd, | |
225 | u32 *result) | |
226 | { | |
722e668d BM |
227 | return nvme_submit_sync_cmd(dev->queues[NVME_ADMIN_Q], cmd, |
228 | result, ADMIN_TIMEOUT); | |
982388ea ZZ |
229 | } |
230 | ||
231 | static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, | |
232 | int qid, int depth) | |
233 | { | |
234 | struct nvme_queue *nvmeq = malloc(sizeof(*nvmeq)); | |
235 | if (!nvmeq) | |
236 | return NULL; | |
237 | memset(nvmeq, 0, sizeof(*nvmeq)); | |
238 | ||
239 | nvmeq->cqes = (void *)memalign(4096, NVME_CQ_SIZE(depth)); | |
240 | if (!nvmeq->cqes) | |
241 | goto free_nvmeq; | |
242 | memset((void *)nvmeq->cqes, 0, NVME_CQ_SIZE(depth)); | |
243 | ||
244 | nvmeq->sq_cmds = (void *)memalign(4096, NVME_SQ_SIZE(depth)); | |
245 | if (!nvmeq->sq_cmds) | |
246 | goto free_queue; | |
247 | memset((void *)nvmeq->sq_cmds, 0, NVME_SQ_SIZE(depth)); | |
248 | ||
249 | nvmeq->dev = dev; | |
250 | ||
251 | nvmeq->cq_head = 0; | |
252 | nvmeq->cq_phase = 1; | |
253 | nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride]; | |
254 | nvmeq->q_depth = depth; | |
255 | nvmeq->qid = qid; | |
256 | dev->queue_count++; | |
257 | dev->queues[qid] = nvmeq; | |
258 | ||
259 | return nvmeq; | |
260 | ||
261 | free_queue: | |
262 | free((void *)nvmeq->cqes); | |
263 | free_nvmeq: | |
264 | free(nvmeq); | |
265 | ||
266 | return NULL; | |
267 | } | |
268 | ||
269 | static int nvme_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id) | |
270 | { | |
271 | struct nvme_command c; | |
272 | ||
273 | memset(&c, 0, sizeof(c)); | |
274 | c.delete_queue.opcode = opcode; | |
275 | c.delete_queue.qid = cpu_to_le16(id); | |
276 | ||
277 | return nvme_submit_admin_cmd(dev, &c, NULL); | |
278 | } | |
279 | ||
280 | static int nvme_delete_sq(struct nvme_dev *dev, u16 sqid) | |
281 | { | |
282 | return nvme_delete_queue(dev, nvme_admin_delete_sq, sqid); | |
283 | } | |
284 | ||
285 | static int nvme_delete_cq(struct nvme_dev *dev, u16 cqid) | |
286 | { | |
287 | return nvme_delete_queue(dev, nvme_admin_delete_cq, cqid); | |
288 | } | |
289 | ||
290 | static int nvme_enable_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, true); | |
297 | } | |
298 | ||
299 | static int nvme_disable_ctrl(struct nvme_dev *dev) | |
300 | { | |
301 | dev->ctrl_config &= ~NVME_CC_SHN_MASK; | |
302 | dev->ctrl_config &= ~NVME_CC_ENABLE; | |
303 | writel(cpu_to_le32(dev->ctrl_config), &dev->bar->cc); | |
304 | ||
305 | return nvme_wait_ready(dev, false); | |
306 | } | |
307 | ||
308 | static void nvme_free_queue(struct nvme_queue *nvmeq) | |
309 | { | |
310 | free((void *)nvmeq->cqes); | |
311 | free(nvmeq->sq_cmds); | |
312 | free(nvmeq); | |
313 | } | |
314 | ||
315 | static void nvme_free_queues(struct nvme_dev *dev, int lowest) | |
316 | { | |
317 | int i; | |
318 | ||
319 | for (i = dev->queue_count - 1; i >= lowest; i--) { | |
320 | struct nvme_queue *nvmeq = dev->queues[i]; | |
321 | dev->queue_count--; | |
322 | dev->queues[i] = NULL; | |
323 | nvme_free_queue(nvmeq); | |
324 | } | |
325 | } | |
326 | ||
327 | static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid) | |
328 | { | |
329 | struct nvme_dev *dev = nvmeq->dev; | |
330 | ||
331 | nvmeq->sq_tail = 0; | |
332 | nvmeq->cq_head = 0; | |
333 | nvmeq->cq_phase = 1; | |
334 | nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride]; | |
335 | memset((void *)nvmeq->cqes, 0, NVME_CQ_SIZE(nvmeq->q_depth)); | |
336 | flush_dcache_range((ulong)nvmeq->cqes, | |
337 | (ulong)nvmeq->cqes + NVME_CQ_SIZE(nvmeq->q_depth)); | |
338 | dev->online_queues++; | |
339 | } | |
340 | ||
341 | static int nvme_configure_admin_queue(struct nvme_dev *dev) | |
342 | { | |
343 | int result; | |
344 | u32 aqa; | |
b65c6921 | 345 | u64 cap = dev->cap; |
982388ea ZZ |
346 | struct nvme_queue *nvmeq; |
347 | /* most architectures use 4KB as the page size */ | |
348 | unsigned page_shift = 12; | |
349 | unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12; | |
350 | unsigned dev_page_max = NVME_CAP_MPSMAX(cap) + 12; | |
351 | ||
352 | if (page_shift < dev_page_min) { | |
353 | debug("Device minimum page size (%u) too large for host (%u)\n", | |
354 | 1 << dev_page_min, 1 << page_shift); | |
355 | return -ENODEV; | |
356 | } | |
357 | ||
358 | if (page_shift > dev_page_max) { | |
359 | debug("Device maximum page size (%u) smaller than host (%u)\n", | |
360 | 1 << dev_page_max, 1 << page_shift); | |
361 | page_shift = dev_page_max; | |
362 | } | |
363 | ||
364 | result = nvme_disable_ctrl(dev); | |
365 | if (result < 0) | |
366 | return result; | |
367 | ||
722e668d | 368 | nvmeq = dev->queues[NVME_ADMIN_Q]; |
982388ea ZZ |
369 | if (!nvmeq) { |
370 | nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH); | |
371 | if (!nvmeq) | |
372 | return -ENOMEM; | |
373 | } | |
374 | ||
375 | aqa = nvmeq->q_depth - 1; | |
376 | aqa |= aqa << 16; | |
377 | aqa |= aqa << 16; | |
378 | ||
379 | dev->page_size = 1 << page_shift; | |
380 | ||
381 | dev->ctrl_config = NVME_CC_CSS_NVM; | |
382 | dev->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT; | |
383 | dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE; | |
384 | dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES; | |
385 | ||
386 | writel(aqa, &dev->bar->aqa); | |
387 | nvme_writeq((ulong)nvmeq->sq_cmds, &dev->bar->asq); | |
388 | nvme_writeq((ulong)nvmeq->cqes, &dev->bar->acq); | |
389 | ||
390 | result = nvme_enable_ctrl(dev); | |
391 | if (result) | |
392 | goto free_nvmeq; | |
393 | ||
394 | nvmeq->cq_vector = 0; | |
395 | ||
722e668d | 396 | nvme_init_queue(dev->queues[NVME_ADMIN_Q], 0); |
982388ea ZZ |
397 | |
398 | return result; | |
399 | ||
400 | free_nvmeq: | |
401 | nvme_free_queues(dev, 0); | |
402 | ||
403 | return result; | |
404 | } | |
405 | ||
406 | static int nvme_alloc_cq(struct nvme_dev *dev, u16 qid, | |
407 | struct nvme_queue *nvmeq) | |
408 | { | |
409 | struct nvme_command c; | |
410 | int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED; | |
411 | ||
412 | memset(&c, 0, sizeof(c)); | |
413 | c.create_cq.opcode = nvme_admin_create_cq; | |
414 | c.create_cq.prp1 = cpu_to_le64((ulong)nvmeq->cqes); | |
415 | c.create_cq.cqid = cpu_to_le16(qid); | |
416 | c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1); | |
417 | c.create_cq.cq_flags = cpu_to_le16(flags); | |
418 | c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector); | |
419 | ||
420 | return nvme_submit_admin_cmd(dev, &c, NULL); | |
421 | } | |
422 | ||
423 | static int nvme_alloc_sq(struct nvme_dev *dev, u16 qid, | |
424 | struct nvme_queue *nvmeq) | |
425 | { | |
426 | struct nvme_command c; | |
427 | int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM; | |
428 | ||
429 | memset(&c, 0, sizeof(c)); | |
430 | c.create_sq.opcode = nvme_admin_create_sq; | |
431 | c.create_sq.prp1 = cpu_to_le64((ulong)nvmeq->sq_cmds); | |
432 | c.create_sq.sqid = cpu_to_le16(qid); | |
433 | c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1); | |
434 | c.create_sq.sq_flags = cpu_to_le16(flags); | |
435 | c.create_sq.cqid = cpu_to_le16(qid); | |
436 | ||
437 | return nvme_submit_admin_cmd(dev, &c, NULL); | |
438 | } | |
439 | ||
440 | int nvme_identify(struct nvme_dev *dev, unsigned nsid, | |
441 | unsigned cns, dma_addr_t dma_addr) | |
442 | { | |
443 | struct nvme_command c; | |
444 | u32 page_size = dev->page_size; | |
445 | int offset = dma_addr & (page_size - 1); | |
446 | int length = sizeof(struct nvme_id_ctrl); | |
704e040a | 447 | int ret; |
982388ea ZZ |
448 | |
449 | memset(&c, 0, sizeof(c)); | |
450 | c.identify.opcode = nvme_admin_identify; | |
451 | c.identify.nsid = cpu_to_le32(nsid); | |
452 | c.identify.prp1 = cpu_to_le64(dma_addr); | |
453 | ||
454 | length -= (page_size - offset); | |
455 | if (length <= 0) { | |
456 | c.identify.prp2 = 0; | |
457 | } else { | |
458 | dma_addr += (page_size - offset); | |
3e185629 | 459 | c.identify.prp2 = cpu_to_le64(dma_addr); |
982388ea ZZ |
460 | } |
461 | ||
462 | c.identify.cns = cpu_to_le32(cns); | |
463 | ||
704e040a BM |
464 | ret = nvme_submit_admin_cmd(dev, &c, NULL); |
465 | if (!ret) | |
466 | invalidate_dcache_range(dma_addr, | |
467 | dma_addr + sizeof(struct nvme_id_ctrl)); | |
468 | ||
469 | return ret; | |
982388ea ZZ |
470 | } |
471 | ||
472 | int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid, | |
473 | dma_addr_t dma_addr, u32 *result) | |
474 | { | |
475 | struct nvme_command c; | |
476 | ||
477 | memset(&c, 0, sizeof(c)); | |
478 | c.features.opcode = nvme_admin_get_features; | |
479 | c.features.nsid = cpu_to_le32(nsid); | |
480 | c.features.prp1 = cpu_to_le64(dma_addr); | |
481 | c.features.fid = cpu_to_le32(fid); | |
482 | ||
704e040a BM |
483 | /* |
484 | * TODO: add cache invalidate operation when the size of | |
485 | * the DMA buffer is known | |
486 | */ | |
487 | ||
982388ea ZZ |
488 | return nvme_submit_admin_cmd(dev, &c, result); |
489 | } | |
490 | ||
491 | int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11, | |
492 | dma_addr_t dma_addr, u32 *result) | |
493 | { | |
494 | struct nvme_command c; | |
495 | ||
496 | memset(&c, 0, sizeof(c)); | |
497 | c.features.opcode = nvme_admin_set_features; | |
498 | c.features.prp1 = cpu_to_le64(dma_addr); | |
499 | c.features.fid = cpu_to_le32(fid); | |
500 | c.features.dword11 = cpu_to_le32(dword11); | |
501 | ||
704e040a BM |
502 | /* |
503 | * TODO: add cache flush operation when the size of | |
504 | * the DMA buffer is known | |
505 | */ | |
506 | ||
982388ea ZZ |
507 | return nvme_submit_admin_cmd(dev, &c, result); |
508 | } | |
509 | ||
510 | static int nvme_create_queue(struct nvme_queue *nvmeq, int qid) | |
511 | { | |
512 | struct nvme_dev *dev = nvmeq->dev; | |
513 | int result; | |
514 | ||
515 | nvmeq->cq_vector = qid - 1; | |
516 | result = nvme_alloc_cq(dev, qid, nvmeq); | |
517 | if (result < 0) | |
518 | goto release_cq; | |
519 | ||
520 | result = nvme_alloc_sq(dev, qid, nvmeq); | |
521 | if (result < 0) | |
522 | goto release_sq; | |
523 | ||
524 | nvme_init_queue(nvmeq, qid); | |
525 | ||
526 | return result; | |
527 | ||
528 | release_sq: | |
529 | nvme_delete_sq(dev, qid); | |
530 | release_cq: | |
531 | nvme_delete_cq(dev, qid); | |
532 | ||
533 | return result; | |
534 | } | |
535 | ||
536 | static int nvme_set_queue_count(struct nvme_dev *dev, int count) | |
537 | { | |
538 | int status; | |
539 | u32 result; | |
540 | u32 q_count = (count - 1) | ((count - 1) << 16); | |
541 | ||
542 | status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, | |
543 | q_count, 0, &result); | |
544 | ||
545 | if (status < 0) | |
546 | return status; | |
547 | if (status > 1) | |
548 | return 0; | |
549 | ||
550 | return min(result & 0xffff, result >> 16) + 1; | |
551 | } | |
552 | ||
553 | static void nvme_create_io_queues(struct nvme_dev *dev) | |
554 | { | |
555 | unsigned int i; | |
556 | ||
557 | for (i = dev->queue_count; i <= dev->max_qid; i++) | |
558 | if (!nvme_alloc_queue(dev, i, dev->q_depth)) | |
559 | break; | |
560 | ||
561 | for (i = dev->online_queues; i <= dev->queue_count - 1; i++) | |
562 | if (nvme_create_queue(dev->queues[i], i)) | |
563 | break; | |
564 | } | |
565 | ||
566 | static int nvme_setup_io_queues(struct nvme_dev *dev) | |
567 | { | |
568 | int nr_io_queues; | |
569 | int result; | |
570 | ||
571 | nr_io_queues = 1; | |
572 | result = nvme_set_queue_count(dev, nr_io_queues); | |
573 | if (result <= 0) | |
574 | return result; | |
575 | ||
982388ea ZZ |
576 | dev->max_qid = nr_io_queues; |
577 | ||
578 | /* Free previously allocated queues */ | |
579 | nvme_free_queues(dev, nr_io_queues + 1); | |
580 | nvme_create_io_queues(dev); | |
581 | ||
582 | return 0; | |
583 | } | |
584 | ||
585 | static int nvme_get_info_from_identify(struct nvme_dev *dev) | |
586 | { | |
2f83481d | 587 | struct nvme_id_ctrl *ctrl; |
982388ea | 588 | int ret; |
b65c6921 | 589 | int shift = NVME_CAP_MPSMIN(dev->cap) + 12; |
982388ea | 590 | |
2f83481d PW |
591 | ctrl = memalign(dev->page_size, sizeof(struct nvme_id_ctrl)); |
592 | if (!ctrl) | |
593 | return -ENOMEM; | |
594 | ||
5b2a20e9 | 595 | ret = nvme_identify(dev, 0, 1, (dma_addr_t)(long)ctrl); |
2f83481d PW |
596 | if (ret) { |
597 | free(ctrl); | |
982388ea | 598 | return -EIO; |
2f83481d | 599 | } |
982388ea ZZ |
600 | |
601 | dev->nn = le32_to_cpu(ctrl->nn); | |
602 | dev->vwc = ctrl->vwc; | |
603 | memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn)); | |
604 | memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn)); | |
605 | memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr)); | |
606 | if (ctrl->mdts) | |
607 | dev->max_transfer_shift = (ctrl->mdts + shift); | |
beb5f521 BM |
608 | else { |
609 | /* | |
610 | * Maximum Data Transfer Size (MDTS) field indicates the maximum | |
611 | * data transfer size between the host and the controller. The | |
612 | * host should not submit a command that exceeds this transfer | |
613 | * size. The value is in units of the minimum memory page size | |
614 | * and is reported as a power of two (2^n). | |
615 | * | |
616 | * The spec also says: a value of 0h indicates no restrictions | |
617 | * on transfer size. But in nvme_blk_read/write() below we have | |
618 | * the following algorithm for maximum number of logic blocks | |
619 | * per transfer: | |
620 | * | |
621 | * u16 lbas = 1 << (dev->max_transfer_shift - ns->lba_shift); | |
622 | * | |
623 | * In order for lbas not to overflow, the maximum number is 15 | |
624 | * which means dev->max_transfer_shift = 15 + 9 (ns->lba_shift). | |
625 | * Let's use 20 which provides 1MB size. | |
626 | */ | |
627 | dev->max_transfer_shift = 20; | |
628 | } | |
982388ea | 629 | |
2f83481d | 630 | free(ctrl); |
982388ea ZZ |
631 | return 0; |
632 | } | |
633 | ||
c50b2883 PW |
634 | int nvme_get_namespace_id(struct udevice *udev, u32 *ns_id, u8 *eui64) |
635 | { | |
636 | struct nvme_ns *ns = dev_get_priv(udev); | |
637 | ||
638 | if (ns_id) | |
639 | *ns_id = ns->ns_id; | |
640 | if (eui64) | |
641 | memcpy(eui64, ns->eui64, sizeof(ns->eui64)); | |
642 | ||
643 | return 0; | |
644 | } | |
645 | ||
982388ea ZZ |
646 | int nvme_scan_namespace(void) |
647 | { | |
648 | struct uclass *uc; | |
649 | struct udevice *dev; | |
650 | int ret; | |
651 | ||
652 | ret = uclass_get(UCLASS_NVME, &uc); | |
653 | if (ret) | |
654 | return ret; | |
655 | ||
656 | uclass_foreach_dev(dev, uc) { | |
657 | ret = device_probe(dev); | |
658 | if (ret) | |
659 | return ret; | |
660 | } | |
661 | ||
662 | return 0; | |
663 | } | |
664 | ||
665 | static int nvme_blk_probe(struct udevice *udev) | |
666 | { | |
667 | struct nvme_dev *ndev = dev_get_priv(udev->parent); | |
668 | struct blk_desc *desc = dev_get_uclass_platdata(udev); | |
669 | struct nvme_ns *ns = dev_get_priv(udev); | |
670 | u8 flbas; | |
e5dc2d26 | 671 | struct pci_child_platdata *pplat; |
2f83481d PW |
672 | struct nvme_id_ns *id; |
673 | ||
674 | id = memalign(ndev->page_size, sizeof(struct nvme_id_ns)); | |
675 | if (!id) | |
676 | return -ENOMEM; | |
982388ea ZZ |
677 | |
678 | memset(ns, 0, sizeof(*ns)); | |
679 | ns->dev = ndev; | |
18aa5a41 BM |
680 | /* extract the namespace id from the block device name */ |
681 | ns->ns_id = trailing_strtol(udev->name) + 1; | |
2f83481d PW |
682 | if (nvme_identify(ndev, ns->ns_id, 0, (dma_addr_t)(long)id)) { |
683 | free(id); | |
982388ea | 684 | return -EIO; |
2f83481d | 685 | } |
982388ea | 686 | |
c50b2883 | 687 | memcpy(&ns->eui64, &id->eui64, sizeof(id->eui64)); |
982388ea ZZ |
688 | flbas = id->flbas & NVME_NS_FLBAS_LBA_MASK; |
689 | ns->flbas = flbas; | |
690 | ns->lba_shift = id->lbaf[flbas].ds; | |
f81d83d5 | 691 | ns->mode_select_num_blocks = le64_to_cpu(id->nsze); |
982388ea ZZ |
692 | ns->mode_select_block_len = 1 << ns->lba_shift; |
693 | list_add(&ns->list, &ndev->namespaces); | |
694 | ||
695 | desc->lba = ns->mode_select_num_blocks; | |
696 | desc->log2blksz = ns->lba_shift; | |
697 | desc->blksz = 1 << ns->lba_shift; | |
698 | desc->bdev = udev; | |
e5dc2d26 BM |
699 | pplat = dev_get_parent_platdata(udev->parent); |
700 | sprintf(desc->vendor, "0x%.4x", pplat->vendor); | |
982388ea ZZ |
701 | memcpy(desc->product, ndev->serial, sizeof(ndev->serial)); |
702 | memcpy(desc->revision, ndev->firmware_rev, sizeof(ndev->firmware_rev)); | |
982388ea | 703 | |
2f83481d | 704 | free(id); |
982388ea ZZ |
705 | return 0; |
706 | } | |
707 | ||
625a483c BM |
708 | static ulong nvme_blk_rw(struct udevice *udev, lbaint_t blknr, |
709 | lbaint_t blkcnt, void *buffer, bool read) | |
982388ea ZZ |
710 | { |
711 | struct nvme_ns *ns = dev_get_priv(udev); | |
712 | struct nvme_dev *dev = ns->dev; | |
713 | struct nvme_command c; | |
714 | struct blk_desc *desc = dev_get_uclass_platdata(udev); | |
715 | int status; | |
716 | u64 prp2; | |
717 | u64 total_len = blkcnt << desc->log2blksz; | |
718 | u64 temp_len = total_len; | |
719 | ||
720 | u64 slba = blknr; | |
721 | u16 lbas = 1 << (dev->max_transfer_shift - ns->lba_shift); | |
722 | u64 total_lbas = blkcnt; | |
723 | ||
8c403402 PW |
724 | flush_dcache_range((unsigned long)buffer, |
725 | (unsigned long)buffer + total_len); | |
704e040a | 726 | |
625a483c | 727 | c.rw.opcode = read ? nvme_cmd_read : nvme_cmd_write; |
982388ea ZZ |
728 | c.rw.flags = 0; |
729 | c.rw.nsid = cpu_to_le32(ns->ns_id); | |
730 | c.rw.control = 0; | |
731 | c.rw.dsmgmt = 0; | |
732 | c.rw.reftag = 0; | |
733 | c.rw.apptag = 0; | |
734 | c.rw.appmask = 0; | |
735 | c.rw.metadata = 0; | |
736 | ||
737 | while (total_lbas) { | |
738 | if (total_lbas < lbas) { | |
739 | lbas = (u16)total_lbas; | |
740 | total_lbas = 0; | |
741 | } else { | |
742 | total_lbas -= lbas; | |
743 | } | |
744 | ||
625a483c BM |
745 | if (nvme_setup_prps(dev, &prp2, |
746 | lbas << ns->lba_shift, (ulong)buffer)) | |
982388ea ZZ |
747 | return -EIO; |
748 | c.rw.slba = cpu_to_le64(slba); | |
749 | slba += lbas; | |
750 | c.rw.length = cpu_to_le16(lbas - 1); | |
751 | c.rw.prp1 = cpu_to_le64((ulong)buffer); | |
752 | c.rw.prp2 = cpu_to_le64(prp2); | |
722e668d | 753 | status = nvme_submit_sync_cmd(dev->queues[NVME_IO_Q], |
982388ea ZZ |
754 | &c, NULL, IO_TIMEOUT); |
755 | if (status) | |
756 | break; | |
52a5690e | 757 | temp_len -= (u32)lbas << ns->lba_shift; |
982388ea ZZ |
758 | buffer += lbas << ns->lba_shift; |
759 | } | |
760 | ||
704e040a BM |
761 | if (read) |
762 | invalidate_dcache_range((unsigned long)buffer, | |
763 | (unsigned long)buffer + total_len); | |
764 | ||
982388ea ZZ |
765 | return (total_len - temp_len) >> desc->log2blksz; |
766 | } | |
767 | ||
625a483c BM |
768 | static ulong nvme_blk_read(struct udevice *udev, lbaint_t blknr, |
769 | lbaint_t blkcnt, void *buffer) | |
770 | { | |
771 | return nvme_blk_rw(udev, blknr, blkcnt, buffer, true); | |
772 | } | |
773 | ||
982388ea ZZ |
774 | static ulong nvme_blk_write(struct udevice *udev, lbaint_t blknr, |
775 | lbaint_t blkcnt, const void *buffer) | |
776 | { | |
625a483c | 777 | return nvme_blk_rw(udev, blknr, blkcnt, (void *)buffer, false); |
982388ea ZZ |
778 | } |
779 | ||
780 | static const struct blk_ops nvme_blk_ops = { | |
781 | .read = nvme_blk_read, | |
782 | .write = nvme_blk_write, | |
783 | }; | |
784 | ||
785 | U_BOOT_DRIVER(nvme_blk) = { | |
786 | .name = "nvme-blk", | |
787 | .id = UCLASS_BLK, | |
788 | .probe = nvme_blk_probe, | |
789 | .ops = &nvme_blk_ops, | |
790 | .priv_auto_alloc_size = sizeof(struct nvme_ns), | |
791 | }; | |
792 | ||
793 | static int nvme_bind(struct udevice *udev) | |
794 | { | |
18aa5a41 | 795 | static int ndev_num; |
982388ea | 796 | char name[20]; |
18aa5a41 BM |
797 | |
798 | sprintf(name, "nvme#%d", ndev_num++); | |
982388ea ZZ |
799 | |
800 | return device_set_name(udev, name); | |
801 | } | |
802 | ||
803 | static int nvme_probe(struct udevice *udev) | |
804 | { | |
805 | int ret; | |
806 | struct nvme_dev *ndev = dev_get_priv(udev); | |
982388ea | 807 | |
982388ea ZZ |
808 | ndev->instance = trailing_strtol(udev->name); |
809 | ||
810 | INIT_LIST_HEAD(&ndev->namespaces); | |
811 | ndev->bar = dm_pci_map_bar(udev, PCI_BASE_ADDRESS_0, | |
812 | PCI_REGION_MEM); | |
813 | if (readl(&ndev->bar->csts) == -1) { | |
814 | ret = -ENODEV; | |
815 | printf("Error: %s: Out of memory!\n", udev->name); | |
816 | goto free_nvme; | |
817 | } | |
818 | ||
722e668d | 819 | ndev->queues = malloc(NVME_Q_NUM * sizeof(struct nvme_queue *)); |
982388ea ZZ |
820 | if (!ndev->queues) { |
821 | ret = -ENOMEM; | |
822 | printf("Error: %s: Out of memory!\n", udev->name); | |
823 | goto free_nvme; | |
824 | } | |
37d46870 | 825 | memset(ndev->queues, 0, NVME_Q_NUM * sizeof(struct nvme_queue *)); |
982388ea | 826 | |
b65c6921 BM |
827 | ndev->cap = nvme_readq(&ndev->bar->cap); |
828 | ndev->q_depth = min_t(int, NVME_CAP_MQES(ndev->cap) + 1, NVME_Q_DEPTH); | |
829 | ndev->db_stride = 1 << NVME_CAP_STRIDE(ndev->cap); | |
982388ea ZZ |
830 | ndev->dbs = ((void __iomem *)ndev->bar) + 4096; |
831 | ||
832 | ret = nvme_configure_admin_queue(ndev); | |
833 | if (ret) | |
834 | goto free_queue; | |
835 | ||
b21dcebf AW |
836 | /* Allocate after the page size is known */ |
837 | ndev->prp_pool = memalign(ndev->page_size, MAX_PRP_POOL); | |
838 | if (!ndev->prp_pool) { | |
839 | ret = -ENOMEM; | |
840 | printf("Error: %s: Out of memory!\n", udev->name); | |
841 | goto free_nvme; | |
842 | } | |
843 | ndev->prp_entry_num = MAX_PRP_POOL >> 3; | |
844 | ||
982388ea ZZ |
845 | ret = nvme_setup_io_queues(ndev); |
846 | if (ret) | |
847 | goto free_queue; | |
848 | ||
849 | nvme_get_info_from_identify(ndev); | |
982388ea ZZ |
850 | |
851 | return 0; | |
852 | ||
853 | free_queue: | |
854 | free((void *)ndev->queues); | |
855 | free_nvme: | |
856 | return ret; | |
857 | } | |
858 | ||
859 | U_BOOT_DRIVER(nvme) = { | |
860 | .name = "nvme", | |
861 | .id = UCLASS_NVME, | |
862 | .bind = nvme_bind, | |
863 | .probe = nvme_probe, | |
864 | .priv_auto_alloc_size = sizeof(struct nvme_dev), | |
865 | }; | |
866 | ||
867 | struct pci_device_id nvme_supported[] = { | |
0deb9131 | 868 | { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, ~0) }, |
982388ea ZZ |
869 | {} |
870 | }; | |
871 | ||
872 | U_BOOT_PCI_DEVICE(nvme, nvme_supported); |