1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
10 #include <dm/device-internal.h>
12 #include <dm/uclass-internal.h>
14 static const char *if_typename_str[IF_TYPE_COUNT] = {
15 [IF_TYPE_IDE] = "ide",
16 [IF_TYPE_SCSI] = "scsi",
17 [IF_TYPE_ATAPI] = "atapi",
18 [IF_TYPE_USB] = "usb",
19 [IF_TYPE_DOC] = "doc",
20 [IF_TYPE_MMC] = "mmc",
22 [IF_TYPE_SATA] = "sata",
23 [IF_TYPE_HOST] = "host",
24 [IF_TYPE_NVME] = "nvme",
25 [IF_TYPE_EFI] = "efi",
28 static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
29 [IF_TYPE_IDE] = UCLASS_IDE,
30 [IF_TYPE_SCSI] = UCLASS_SCSI,
31 [IF_TYPE_ATAPI] = UCLASS_INVALID,
32 [IF_TYPE_USB] = UCLASS_MASS_STORAGE,
33 [IF_TYPE_DOC] = UCLASS_INVALID,
34 [IF_TYPE_MMC] = UCLASS_MMC,
35 [IF_TYPE_SD] = UCLASS_INVALID,
36 [IF_TYPE_SATA] = UCLASS_AHCI,
37 [IF_TYPE_HOST] = UCLASS_ROOT,
38 [IF_TYPE_NVME] = UCLASS_NVME,
39 [IF_TYPE_EFI] = UCLASS_EFI,
42 static enum if_type if_typename_to_iftype(const char *if_typename)
46 for (i = 0; i < IF_TYPE_COUNT; i++) {
47 if (if_typename_str[i] &&
48 !strcmp(if_typename, if_typename_str[i]))
52 return IF_TYPE_UNKNOWN;
55 static enum uclass_id if_type_to_uclass_id(enum if_type if_type)
57 return if_type_uclass_id[if_type];
60 const char *blk_get_if_type_name(enum if_type if_type)
62 return if_typename_str[if_type];
65 struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum)
67 struct blk_desc *desc;
71 ret = blk_get_device(if_type, devnum, &dev);
74 desc = dev_get_uclass_platdata(dev);
80 * This function is complicated with driver model. We look up the interface
81 * name in a local table. This gives us an interface type which we can match
82 * against the uclass of the block device's parent.
84 struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum)
86 enum uclass_id uclass_id;
92 if_type = if_typename_to_iftype(if_typename);
93 if (if_type == IF_TYPE_UNKNOWN) {
94 debug("%s: Unknown interface type '%s'\n", __func__,
98 uclass_id = if_type_to_uclass_id(if_type);
99 if (uclass_id == UCLASS_INVALID) {
100 debug("%s: Unknown uclass for interface type'\n",
101 if_typename_str[if_type]);
105 ret = uclass_get(UCLASS_BLK, &uc);
108 uclass_foreach_dev(dev, uc) {
109 struct blk_desc *desc = dev_get_uclass_platdata(dev);
111 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
112 if_type, devnum, dev->name, desc->if_type, desc->devnum);
113 if (desc->devnum != devnum)
116 /* Find out the parent device uclass */
117 if (device_get_uclass_id(dev->parent) != uclass_id) {
118 debug("%s: parent uclass %d, this dev %d\n", __func__,
119 device_get_uclass_id(dev->parent), uclass_id);
123 if (device_probe(dev))
126 debug("%s: Device desc %p\n", __func__, desc);
129 debug("%s: No device found\n", __func__);
135 * blk_get_by_device() - Get the block device descriptor for the given device
136 * @dev: Instance of a storage device
138 * Return: With block device descriptor on success , NULL if there is no such
141 struct blk_desc *blk_get_by_device(struct udevice *dev)
143 struct udevice *child_dev, *next;
145 device_foreach_child_safe(child_dev, next, dev) {
146 if (device_get_uclass_id(child_dev) != UCLASS_BLK)
149 return dev_get_uclass_platdata(child_dev);
152 debug("%s: No block device found\n", __func__);
158 * get_desc() - Get the block device descriptor for the given device number
160 * @if_type: Interface type
161 * @devnum: Device number (0 = first)
162 * @descp: Returns block device descriptor on success
163 * @return 0 on success, -ENODEV if there is no such device and no device
164 * with a higher device number, -ENOENT if there is no such device but there
165 * is one with a higher number, or other -ve on other error.
167 static int get_desc(enum if_type if_type, int devnum, struct blk_desc **descp)
169 bool found_more = false;
175 ret = uclass_get(UCLASS_BLK, &uc);
178 uclass_foreach_dev(dev, uc) {
179 struct blk_desc *desc = dev_get_uclass_platdata(dev);
181 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
182 if_type, devnum, dev->name, desc->if_type, desc->devnum);
183 if (desc->if_type == if_type) {
184 if (desc->devnum == devnum) {
185 ret = device_probe(dev);
191 } else if (desc->devnum > devnum) {
197 return found_more ? -ENOENT : -ENODEV;
200 int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart)
205 ret = blk_get_device(if_type, devnum, &dev);
209 return blk_select_hwpart(dev, hwpart);
212 int blk_list_part(enum if_type if_type)
214 struct blk_desc *desc;
218 for (ok = 0, devnum = 0;; ++devnum) {
219 ret = get_desc(if_type, devnum, &desc);
224 if (desc->part_type != PART_TYPE_UNKNOWN) {
237 int blk_print_part_devnum(enum if_type if_type, int devnum)
239 struct blk_desc *desc;
242 ret = get_desc(if_type, devnum, &desc);
245 if (desc->type == DEV_TYPE_UNKNOWN)
252 void blk_list_devices(enum if_type if_type)
254 struct blk_desc *desc;
259 ret = get_desc(if_type, i, &desc);
264 if (desc->type == DEV_TYPE_UNKNOWN)
265 continue; /* list only known devices */
266 printf("Device %d: ", i);
271 int blk_print_device_num(enum if_type if_type, int devnum)
273 struct blk_desc *desc;
276 ret = get_desc(if_type, devnum, &desc);
279 printf("\nIDE device %d: ", devnum);
285 int blk_show_device(enum if_type if_type, int devnum)
287 struct blk_desc *desc;
290 printf("\nDevice %d: ", devnum);
291 ret = get_desc(if_type, devnum, &desc);
292 if (ret == -ENODEV || ret == -ENOENT) {
293 printf("unknown device\n");
300 if (desc->type == DEV_TYPE_UNKNOWN)
306 ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
307 lbaint_t blkcnt, void *buffer)
309 struct blk_desc *desc;
313 ret = get_desc(if_type, devnum, &desc);
316 n = blk_dread(desc, start, blkcnt, buffer);
323 ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
324 lbaint_t blkcnt, const void *buffer)
326 struct blk_desc *desc;
329 ret = get_desc(if_type, devnum, &desc);
332 return blk_dwrite(desc, start, blkcnt, buffer);
335 int blk_select_hwpart(struct udevice *dev, int hwpart)
337 const struct blk_ops *ops = blk_get_ops(dev);
341 if (!ops->select_hwpart)
344 return ops->select_hwpart(dev, hwpart);
347 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart)
349 return blk_select_hwpart(desc->bdev, hwpart);
352 int blk_first_device(int if_type, struct udevice **devp)
354 struct blk_desc *desc;
357 ret = uclass_find_first_device(UCLASS_BLK, devp);
363 desc = dev_get_uclass_platdata(*devp);
364 if (desc->if_type == if_type)
366 ret = uclass_find_next_device(devp);
374 int blk_next_device(struct udevice **devp)
376 struct blk_desc *desc;
379 desc = dev_get_uclass_platdata(*devp);
380 if_type = desc->if_type;
382 ret = uclass_find_next_device(devp);
387 desc = dev_get_uclass_platdata(*devp);
388 if (desc->if_type == if_type)
393 int blk_find_device(int if_type, int devnum, struct udevice **devp)
399 ret = uclass_get(UCLASS_BLK, &uc);
402 uclass_foreach_dev(dev, uc) {
403 struct blk_desc *desc = dev_get_uclass_platdata(dev);
405 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
406 if_type, devnum, dev->name, desc->if_type, desc->devnum);
407 if (desc->if_type == if_type && desc->devnum == devnum) {
416 int blk_get_device(int if_type, int devnum, struct udevice **devp)
420 ret = blk_find_device(if_type, devnum, devp);
424 return device_probe(*devp);
427 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
428 lbaint_t blkcnt, void *buffer)
430 struct udevice *dev = block_dev->bdev;
431 const struct blk_ops *ops = blk_get_ops(dev);
437 if (blkcache_read(block_dev->if_type, block_dev->devnum,
438 start, blkcnt, block_dev->blksz, buffer))
440 blks_read = ops->read(dev, start, blkcnt, buffer);
441 if (blks_read == blkcnt)
442 blkcache_fill(block_dev->if_type, block_dev->devnum,
443 start, blkcnt, block_dev->blksz, buffer);
448 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
449 lbaint_t blkcnt, const void *buffer)
451 struct udevice *dev = block_dev->bdev;
452 const struct blk_ops *ops = blk_get_ops(dev);
457 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
458 return ops->write(dev, start, blkcnt, buffer);
461 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
464 struct udevice *dev = block_dev->bdev;
465 const struct blk_ops *ops = blk_get_ops(dev);
470 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
471 return ops->erase(dev, start, blkcnt);
474 int blk_get_from_parent(struct udevice *parent, struct udevice **devp)
480 device_find_first_child(parent, &dev);
482 debug("%s: No block device found for parent '%s'\n", __func__,
486 id = device_get_uclass_id(dev);
487 if (id != UCLASS_BLK) {
488 debug("%s: Incorrect uclass %s for block device '%s'\n",
489 __func__, uclass_get_name(id), dev->name);
492 ret = device_probe(dev);
500 int blk_find_max_devnum(enum if_type if_type)
503 int max_devnum = -ENODEV;
507 ret = uclass_get(UCLASS_BLK, &uc);
510 uclass_foreach_dev(dev, uc) {
511 struct blk_desc *desc = dev_get_uclass_platdata(dev);
513 if (desc->if_type == if_type && desc->devnum > max_devnum)
514 max_devnum = desc->devnum;
520 int blk_next_free_devnum(enum if_type if_type)
524 ret = blk_find_max_devnum(if_type);
533 static int blk_claim_devnum(enum if_type if_type, int devnum)
539 ret = uclass_get(UCLASS_BLK, &uc);
542 uclass_foreach_dev(dev, uc) {
543 struct blk_desc *desc = dev_get_uclass_platdata(dev);
545 if (desc->if_type == if_type && desc->devnum == devnum) {
546 int next = blk_next_free_devnum(if_type);
558 int blk_create_device(struct udevice *parent, const char *drv_name,
559 const char *name, int if_type, int devnum, int blksz,
560 lbaint_t lba, struct udevice **devp)
562 struct blk_desc *desc;
567 devnum = blk_next_free_devnum(if_type);
569 ret = blk_claim_devnum(if_type, devnum);
570 if (ret < 0 && ret != -ENOENT)
575 ret = device_bind_driver(parent, drv_name, name, &dev);
578 desc = dev_get_uclass_platdata(dev);
579 desc->if_type = if_type;
582 desc->part_type = PART_TYPE_UNKNOWN;
584 desc->devnum = devnum;
590 int blk_create_devicef(struct udevice *parent, const char *drv_name,
591 const char *name, int if_type, int devnum, int blksz,
592 lbaint_t lba, struct udevice **devp)
594 char dev_name[30], *str;
597 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
598 str = strdup(dev_name);
602 ret = blk_create_device(parent, drv_name, str, if_type, devnum,
608 device_set_name_alloced(*devp);
613 int blk_unbind_all(int if_type)
616 struct udevice *dev, *next;
619 ret = uclass_get(UCLASS_BLK, &uc);
622 uclass_foreach_dev_safe(dev, next, uc) {
623 struct blk_desc *desc = dev_get_uclass_platdata(dev);
625 if (desc->if_type == if_type) {
626 ret = device_remove(dev, DM_REMOVE_NORMAL);
629 ret = device_unbind(dev);
638 static int blk_post_probe(struct udevice *dev)
640 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
641 struct blk_desc *desc = dev_get_uclass_platdata(dev);
649 UCLASS_DRIVER(blk) = {
652 .post_probe = blk_post_probe,
653 .per_device_platdata_auto_alloc_size = sizeof(struct blk_desc),