blob: 010ed32d3add35c434c0d9f819edd31fcb527fa0 [file] [log] [blame]
Simon Glasscceee552016-02-29 15:25:55 -07001/*
2 * Copyright (C) 2016 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <blk.h>
10#include <dm.h>
11#include <dm/device-internal.h>
12#include <dm/lists.h>
Stefan Roeseef58a902017-11-29 16:46:42 +010013#include <dm/uclass-internal.h>
Simon Glasscceee552016-02-29 15:25:55 -070014
Simon Glass4131ad52016-05-01 11:36:08 -060015static const char *if_typename_str[IF_TYPE_COUNT] = {
16 [IF_TYPE_IDE] = "ide",
17 [IF_TYPE_SCSI] = "scsi",
18 [IF_TYPE_ATAPI] = "atapi",
19 [IF_TYPE_USB] = "usb",
20 [IF_TYPE_DOC] = "doc",
21 [IF_TYPE_MMC] = "mmc",
22 [IF_TYPE_SD] = "sd",
23 [IF_TYPE_SATA] = "sata",
24 [IF_TYPE_HOST] = "host",
25 [IF_TYPE_SYSTEMACE] = "ace",
Zhikang Zhang182fccd2017-08-03 02:30:56 -070026 [IF_TYPE_NVME] = "nvme",
Simon Glass4131ad52016-05-01 11:36:08 -060027};
28
29static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
Bin Mengb650afa2017-09-10 05:12:51 -070030 [IF_TYPE_IDE] = UCLASS_IDE,
Michal Simekc886f352016-09-08 15:06:45 +020031 [IF_TYPE_SCSI] = UCLASS_SCSI,
Simon Glass4131ad52016-05-01 11:36:08 -060032 [IF_TYPE_ATAPI] = UCLASS_INVALID,
33 [IF_TYPE_USB] = UCLASS_MASS_STORAGE,
34 [IF_TYPE_DOC] = UCLASS_INVALID,
35 [IF_TYPE_MMC] = UCLASS_MMC,
36 [IF_TYPE_SD] = UCLASS_INVALID,
37 [IF_TYPE_SATA] = UCLASS_AHCI,
38 [IF_TYPE_HOST] = UCLASS_ROOT,
Zhikang Zhang182fccd2017-08-03 02:30:56 -070039 [IF_TYPE_NVME] = UCLASS_NVME,
Simon Glass4131ad52016-05-01 11:36:08 -060040 [IF_TYPE_SYSTEMACE] = UCLASS_INVALID,
41};
42
43static enum if_type if_typename_to_iftype(const char *if_typename)
44{
45 int i;
46
47 for (i = 0; i < IF_TYPE_COUNT; i++) {
48 if (if_typename_str[i] &&
49 !strcmp(if_typename, if_typename_str[i]))
50 return i;
51 }
52
53 return IF_TYPE_UNKNOWN;
54}
55
56static enum uclass_id if_type_to_uclass_id(enum if_type if_type)
57{
58 return if_type_uclass_id[if_type];
59}
60
Simon Glass85af5a42017-07-29 11:34:53 -060061const char *blk_get_if_type_name(enum if_type if_type)
62{
63 return if_typename_str[if_type];
64}
65
Simon Glass4131ad52016-05-01 11:36:08 -060066struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum)
67{
68 struct blk_desc *desc;
69 struct udevice *dev;
70 int ret;
71
72 ret = blk_get_device(if_type, devnum, &dev);
73 if (ret)
74 return NULL;
75 desc = dev_get_uclass_platdata(dev);
76
77 return desc;
78}
79
80/*
81 * This function is complicated with driver model. We look up the interface
82 * name in a local table. This gives us an interface type which we can match
83 * against the uclass of the block device's parent.
84 */
85struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum)
86{
87 enum uclass_id uclass_id;
88 enum if_type if_type;
89 struct udevice *dev;
90 struct uclass *uc;
91 int ret;
92
93 if_type = if_typename_to_iftype(if_typename);
94 if (if_type == IF_TYPE_UNKNOWN) {
95 debug("%s: Unknown interface type '%s'\n", __func__,
96 if_typename);
97 return NULL;
98 }
99 uclass_id = if_type_to_uclass_id(if_type);
100 if (uclass_id == UCLASS_INVALID) {
101 debug("%s: Unknown uclass for interface type'\n",
102 if_typename_str[if_type]);
103 return NULL;
104 }
105
106 ret = uclass_get(UCLASS_BLK, &uc);
107 if (ret)
108 return NULL;
109 uclass_foreach_dev(dev, uc) {
110 struct blk_desc *desc = dev_get_uclass_platdata(dev);
111
112 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
113 if_type, devnum, dev->name, desc->if_type, desc->devnum);
114 if (desc->devnum != devnum)
115 continue;
116
117 /* Find out the parent device uclass */
118 if (device_get_uclass_id(dev->parent) != uclass_id) {
119 debug("%s: parent uclass %d, this dev %d\n", __func__,
120 device_get_uclass_id(dev->parent), uclass_id);
121 continue;
122 }
123
124 if (device_probe(dev))
125 return NULL;
126
127 debug("%s: Device desc %p\n", __func__, desc);
128 return desc;
129 }
130 debug("%s: No device found\n", __func__);
131
132 return NULL;
133}
134
135/**
136 * get_desc() - Get the block device descriptor for the given device number
137 *
138 * @if_type: Interface type
139 * @devnum: Device number (0 = first)
140 * @descp: Returns block device descriptor on success
141 * @return 0 on success, -ENODEV if there is no such device and no device
142 * with a higher device number, -ENOENT if there is no such device but there
143 * is one with a higher number, or other -ve on other error.
144 */
145static int get_desc(enum if_type if_type, int devnum, struct blk_desc **descp)
146{
147 bool found_more = false;
148 struct udevice *dev;
149 struct uclass *uc;
150 int ret;
151
152 *descp = NULL;
153 ret = uclass_get(UCLASS_BLK, &uc);
154 if (ret)
155 return ret;
156 uclass_foreach_dev(dev, uc) {
157 struct blk_desc *desc = dev_get_uclass_platdata(dev);
158
159 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
160 if_type, devnum, dev->name, desc->if_type, desc->devnum);
161 if (desc->if_type == if_type) {
162 if (desc->devnum == devnum) {
163 ret = device_probe(dev);
164 if (ret)
165 return ret;
166
Michal Simekf438b6b2016-11-16 17:37:42 +0100167 *descp = desc;
168 return 0;
Simon Glass4131ad52016-05-01 11:36:08 -0600169 } else if (desc->devnum > devnum) {
170 found_more = true;
171 }
172 }
173 }
174
175 return found_more ? -ENOENT : -ENODEV;
176}
177
Simon Glass13c2c292016-05-01 13:52:30 -0600178int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart)
179{
180 struct udevice *dev;
181 int ret;
182
183 ret = blk_get_device(if_type, devnum, &dev);
184 if (ret)
185 return ret;
186
187 return blk_select_hwpart(dev, hwpart);
188}
189
Simon Glass4131ad52016-05-01 11:36:08 -0600190int blk_list_part(enum if_type if_type)
191{
192 struct blk_desc *desc;
193 int devnum, ok;
194 int ret;
195
196 for (ok = 0, devnum = 0;; ++devnum) {
197 ret = get_desc(if_type, devnum, &desc);
198 if (ret == -ENODEV)
199 break;
200 else if (ret)
201 continue;
202 if (desc->part_type != PART_TYPE_UNKNOWN) {
203 ++ok;
204 if (devnum)
205 putc('\n');
206 part_print(desc);
207 }
208 }
209 if (!ok)
210 return -ENODEV;
211
212 return 0;
213}
214
215int blk_print_part_devnum(enum if_type if_type, int devnum)
216{
217 struct blk_desc *desc;
218 int ret;
219
220 ret = get_desc(if_type, devnum, &desc);
221 if (ret)
222 return ret;
223 if (desc->type == DEV_TYPE_UNKNOWN)
224 return -ENOENT;
225 part_print(desc);
226
227 return 0;
228}
229
230void blk_list_devices(enum if_type if_type)
231{
232 struct blk_desc *desc;
233 int ret;
234 int i;
235
236 for (i = 0;; ++i) {
237 ret = get_desc(if_type, i, &desc);
238 if (ret == -ENODEV)
239 break;
240 else if (ret)
241 continue;
242 if (desc->type == DEV_TYPE_UNKNOWN)
243 continue; /* list only known devices */
244 printf("Device %d: ", i);
245 dev_print(desc);
246 }
247}
248
249int blk_print_device_num(enum if_type if_type, int devnum)
250{
251 struct blk_desc *desc;
252 int ret;
253
254 ret = get_desc(if_type, devnum, &desc);
255 if (ret)
256 return ret;
257 printf("\nIDE device %d: ", devnum);
258 dev_print(desc);
259
260 return 0;
261}
262
263int blk_show_device(enum if_type if_type, int devnum)
264{
265 struct blk_desc *desc;
266 int ret;
267
268 printf("\nDevice %d: ", devnum);
269 ret = get_desc(if_type, devnum, &desc);
270 if (ret == -ENODEV || ret == -ENOENT) {
271 printf("unknown device\n");
272 return -ENODEV;
273 }
274 if (ret)
275 return ret;
276 dev_print(desc);
277
278 if (desc->type == DEV_TYPE_UNKNOWN)
279 return -ENOENT;
280
281 return 0;
282}
283
284ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
285 lbaint_t blkcnt, void *buffer)
286{
287 struct blk_desc *desc;
288 ulong n;
289 int ret;
290
291 ret = get_desc(if_type, devnum, &desc);
292 if (ret)
293 return ret;
294 n = blk_dread(desc, start, blkcnt, buffer);
295 if (IS_ERR_VALUE(n))
296 return n;
297
Simon Glass4131ad52016-05-01 11:36:08 -0600298 return n;
299}
300
301ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
302 lbaint_t blkcnt, const void *buffer)
303{
304 struct blk_desc *desc;
305 int ret;
306
307 ret = get_desc(if_type, devnum, &desc);
308 if (ret)
309 return ret;
310 return blk_dwrite(desc, start, blkcnt, buffer);
311}
312
Simon Glass13c2c292016-05-01 13:52:30 -0600313int blk_select_hwpart(struct udevice *dev, int hwpart)
314{
315 const struct blk_ops *ops = blk_get_ops(dev);
316
317 if (!ops)
318 return -ENOSYS;
319 if (!ops->select_hwpart)
320 return 0;
321
322 return ops->select_hwpart(dev, hwpart);
323}
324
325int blk_dselect_hwpart(struct blk_desc *desc, int hwpart)
326{
327 return blk_select_hwpart(desc->bdev, hwpart);
328}
329
Simon Glasscceee552016-02-29 15:25:55 -0700330int blk_first_device(int if_type, struct udevice **devp)
331{
332 struct blk_desc *desc;
333 int ret;
334
Stefan Roeseef58a902017-11-29 16:46:42 +0100335 ret = uclass_find_first_device(UCLASS_BLK, devp);
Simon Glasscceee552016-02-29 15:25:55 -0700336 if (ret)
337 return ret;
338 if (!*devp)
339 return -ENODEV;
340 do {
341 desc = dev_get_uclass_platdata(*devp);
342 if (desc->if_type == if_type)
343 return 0;
Stefan Roeseef58a902017-11-29 16:46:42 +0100344 ret = uclass_find_next_device(devp);
Simon Glasscceee552016-02-29 15:25:55 -0700345 if (ret)
346 return ret;
347 } while (*devp);
348
349 return -ENODEV;
350}
351
352int blk_next_device(struct udevice **devp)
353{
354 struct blk_desc *desc;
355 int ret, if_type;
356
357 desc = dev_get_uclass_platdata(*devp);
358 if_type = desc->if_type;
359 do {
Stefan Roeseef58a902017-11-29 16:46:42 +0100360 ret = uclass_find_next_device(devp);
Simon Glasscceee552016-02-29 15:25:55 -0700361 if (ret)
362 return ret;
363 if (!*devp)
364 return -ENODEV;
365 desc = dev_get_uclass_platdata(*devp);
366 if (desc->if_type == if_type)
367 return 0;
368 } while (1);
369}
370
Simon Glassd5d4c102017-04-23 20:02:05 -0600371int blk_find_device(int if_type, int devnum, struct udevice **devp)
Simon Glasscceee552016-02-29 15:25:55 -0700372{
373 struct uclass *uc;
374 struct udevice *dev;
375 int ret;
376
377 ret = uclass_get(UCLASS_BLK, &uc);
378 if (ret)
379 return ret;
380 uclass_foreach_dev(dev, uc) {
381 struct blk_desc *desc = dev_get_uclass_platdata(dev);
382
383 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
384 if_type, devnum, dev->name, desc->if_type, desc->devnum);
385 if (desc->if_type == if_type && desc->devnum == devnum) {
386 *devp = dev;
Simon Glassd5d4c102017-04-23 20:02:05 -0600387 return 0;
Simon Glasscceee552016-02-29 15:25:55 -0700388 }
389 }
390
391 return -ENODEV;
392}
393
Simon Glassd5d4c102017-04-23 20:02:05 -0600394int blk_get_device(int if_type, int devnum, struct udevice **devp)
395{
396 int ret;
397
398 ret = blk_find_device(if_type, devnum, devp);
399 if (ret)
400 return ret;
401
402 return device_probe(*devp);
403}
404
Simon Glasscceee552016-02-29 15:25:55 -0700405unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
406 lbaint_t blkcnt, void *buffer)
407{
408 struct udevice *dev = block_dev->bdev;
409 const struct blk_ops *ops = blk_get_ops(dev);
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700410 ulong blks_read;
Simon Glasscceee552016-02-29 15:25:55 -0700411
412 if (!ops->read)
413 return -ENOSYS;
414
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700415 if (blkcache_read(block_dev->if_type, block_dev->devnum,
416 start, blkcnt, block_dev->blksz, buffer))
417 return blkcnt;
418 blks_read = ops->read(dev, start, blkcnt, buffer);
419 if (blks_read == blkcnt)
420 blkcache_fill(block_dev->if_type, block_dev->devnum,
421 start, blkcnt, block_dev->blksz, buffer);
422
423 return blks_read;
Simon Glasscceee552016-02-29 15:25:55 -0700424}
425
426unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
427 lbaint_t blkcnt, const void *buffer)
428{
429 struct udevice *dev = block_dev->bdev;
430 const struct blk_ops *ops = blk_get_ops(dev);
431
432 if (!ops->write)
433 return -ENOSYS;
434
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700435 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
Simon Glasscceee552016-02-29 15:25:55 -0700436 return ops->write(dev, start, blkcnt, buffer);
437}
438
439unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
440 lbaint_t blkcnt)
441{
442 struct udevice *dev = block_dev->bdev;
443 const struct blk_ops *ops = blk_get_ops(dev);
444
445 if (!ops->erase)
446 return -ENOSYS;
447
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700448 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
Simon Glasscceee552016-02-29 15:25:55 -0700449 return ops->erase(dev, start, blkcnt);
450}
451
452int blk_prepare_device(struct udevice *dev)
453{
454 struct blk_desc *desc = dev_get_uclass_platdata(dev);
455
456 part_init(desc);
457
458 return 0;
459}
460
Simon Glass4f269132017-05-27 11:37:17 -0600461int blk_get_from_parent(struct udevice *parent, struct udevice **devp)
462{
463 struct udevice *dev;
464 enum uclass_id id;
465 int ret;
466
467 device_find_first_child(parent, &dev);
468 if (!dev) {
469 debug("%s: No block device found for parent '%s'\n", __func__,
470 parent->name);
471 return -ENODEV;
472 }
473 id = device_get_uclass_id(dev);
474 if (id != UCLASS_BLK) {
475 debug("%s: Incorrect uclass %s for block device '%s'\n",
476 __func__, uclass_get_name(id), dev->name);
477 return -ENOTBLK;
478 }
479 ret = device_probe(dev);
480 if (ret)
481 return ret;
482 *devp = dev;
483
484 return 0;
485}
486
Simon Glassd089ba32016-05-01 11:36:28 -0600487int blk_find_max_devnum(enum if_type if_type)
488{
489 struct udevice *dev;
490 int max_devnum = -ENODEV;
491 struct uclass *uc;
492 int ret;
493
494 ret = uclass_get(UCLASS_BLK, &uc);
495 if (ret)
496 return ret;
497 uclass_foreach_dev(dev, uc) {
498 struct blk_desc *desc = dev_get_uclass_platdata(dev);
499
500 if (desc->if_type == if_type && desc->devnum > max_devnum)
501 max_devnum = desc->devnum;
502 }
503
504 return max_devnum;
505}
506
Simon Glassdbc38612017-04-23 20:02:06 -0600507static int blk_next_free_devnum(enum if_type if_type)
508{
509 int ret;
510
511 ret = blk_find_max_devnum(if_type);
512 if (ret == -ENODEV)
513 return 0;
514 if (ret < 0)
515 return ret;
516
517 return ret + 1;
518}
519
Simon Glasse4fef742017-04-23 20:02:07 -0600520static int blk_claim_devnum(enum if_type if_type, int devnum)
521{
522 struct udevice *dev;
523 struct uclass *uc;
524 int ret;
525
526 ret = uclass_get(UCLASS_BLK, &uc);
527 if (ret)
528 return ret;
529 uclass_foreach_dev(dev, uc) {
530 struct blk_desc *desc = dev_get_uclass_platdata(dev);
531
532 if (desc->if_type == if_type && desc->devnum == devnum) {
533 int next = blk_next_free_devnum(if_type);
534
535 if (next < 0)
536 return next;
537 desc->devnum = next;
538 return 0;
539 }
540 }
541
542 return -ENOENT;
543}
544
Simon Glasscceee552016-02-29 15:25:55 -0700545int blk_create_device(struct udevice *parent, const char *drv_name,
546 const char *name, int if_type, int devnum, int blksz,
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200547 lbaint_t lba, struct udevice **devp)
Simon Glasscceee552016-02-29 15:25:55 -0700548{
549 struct blk_desc *desc;
550 struct udevice *dev;
551 int ret;
552
Simon Glassd089ba32016-05-01 11:36:28 -0600553 if (devnum == -1) {
Simon Glasse4fef742017-04-23 20:02:07 -0600554 devnum = blk_next_free_devnum(if_type);
555 } else {
556 ret = blk_claim_devnum(if_type, devnum);
557 if (ret < 0 && ret != -ENOENT)
Simon Glassd089ba32016-05-01 11:36:28 -0600558 return ret;
Simon Glassd089ba32016-05-01 11:36:28 -0600559 }
Simon Glasse4fef742017-04-23 20:02:07 -0600560 if (devnum < 0)
561 return devnum;
Simon Glass77f7fb82016-05-01 13:52:22 -0600562 ret = device_bind_driver(parent, drv_name, name, &dev);
563 if (ret)
564 return ret;
565 desc = dev_get_uclass_platdata(dev);
566 desc->if_type = if_type;
567 desc->blksz = blksz;
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200568 desc->lba = lba;
Simon Glass77f7fb82016-05-01 13:52:22 -0600569 desc->part_type = PART_TYPE_UNKNOWN;
570 desc->bdev = dev;
Simon Glasscceee552016-02-29 15:25:55 -0700571 desc->devnum = devnum;
572 *devp = dev;
573
574 return 0;
575}
576
Simon Glass966b6952016-05-01 11:36:29 -0600577int blk_create_devicef(struct udevice *parent, const char *drv_name,
578 const char *name, int if_type, int devnum, int blksz,
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200579 lbaint_t lba, struct udevice **devp)
Simon Glass966b6952016-05-01 11:36:29 -0600580{
581 char dev_name[30], *str;
Simon Glass39e54922016-05-01 13:52:24 -0600582 int ret;
Simon Glass966b6952016-05-01 11:36:29 -0600583
584 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
585 str = strdup(dev_name);
586 if (!str)
587 return -ENOMEM;
588
Simon Glass39e54922016-05-01 13:52:24 -0600589 ret = blk_create_device(parent, drv_name, str, if_type, devnum,
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200590 blksz, lba, devp);
Simon Glass39e54922016-05-01 13:52:24 -0600591 if (ret) {
592 free(str);
593 return ret;
594 }
595 device_set_name_alloced(*devp);
596
Simon Glass44505972017-07-29 11:34:59 -0600597 return 0;
Simon Glass966b6952016-05-01 11:36:29 -0600598}
599
Simon Glasscceee552016-02-29 15:25:55 -0700600int blk_unbind_all(int if_type)
601{
602 struct uclass *uc;
603 struct udevice *dev, *next;
604 int ret;
605
606 ret = uclass_get(UCLASS_BLK, &uc);
607 if (ret)
608 return ret;
609 uclass_foreach_dev_safe(dev, next, uc) {
610 struct blk_desc *desc = dev_get_uclass_platdata(dev);
611
612 if (desc->if_type == if_type) {
Stefan Roese80b5bc92017-03-20 12:51:48 +0100613 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glasscceee552016-02-29 15:25:55 -0700614 if (ret)
615 return ret;
616 ret = device_unbind(dev);
617 if (ret)
618 return ret;
619 }
620 }
621
622 return 0;
623}
624
625UCLASS_DRIVER(blk) = {
626 .id = UCLASS_BLK,
627 .name = "blk",
628 .per_device_platdata_auto_alloc_size = sizeof(struct blk_desc),
629};