1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
11 #include <dm/device-internal.h>
13 #include <dm/uclass-internal.h>
14 #include <linux/err.h>
16 static const char *if_typename_str[IF_TYPE_COUNT] = {
17 [IF_TYPE_IDE] = "ide",
18 [IF_TYPE_SCSI] = "scsi",
19 [IF_TYPE_ATAPI] = "atapi",
20 [IF_TYPE_USB] = "usb",
21 [IF_TYPE_DOC] = "doc",
22 [IF_TYPE_MMC] = "mmc",
24 [IF_TYPE_SATA] = "sata",
25 [IF_TYPE_HOST] = "host",
26 [IF_TYPE_NVME] = "nvme",
27 [IF_TYPE_EFI] = "efi",
28 [IF_TYPE_VIRTIO] = "virtio",
31 static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
32 [IF_TYPE_IDE] = UCLASS_IDE,
33 [IF_TYPE_SCSI] = UCLASS_SCSI,
34 [IF_TYPE_ATAPI] = UCLASS_INVALID,
35 [IF_TYPE_USB] = UCLASS_MASS_STORAGE,
36 [IF_TYPE_DOC] = UCLASS_INVALID,
37 [IF_TYPE_MMC] = UCLASS_MMC,
38 [IF_TYPE_SD] = UCLASS_INVALID,
39 [IF_TYPE_SATA] = UCLASS_AHCI,
40 [IF_TYPE_HOST] = UCLASS_ROOT,
41 [IF_TYPE_NVME] = UCLASS_NVME,
42 [IF_TYPE_EFI] = UCLASS_EFI,
43 [IF_TYPE_VIRTIO] = UCLASS_VIRTIO,
46 static enum if_type if_typename_to_iftype(const char *if_typename)
50 for (i = 0; i < IF_TYPE_COUNT; i++) {
51 if (if_typename_str[i] &&
52 !strcmp(if_typename, if_typename_str[i]))
56 return IF_TYPE_UNKNOWN;
59 static enum uclass_id if_type_to_uclass_id(enum if_type if_type)
61 return if_type_uclass_id[if_type];
64 const char *blk_get_if_type_name(enum if_type if_type)
66 return if_typename_str[if_type];
69 struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum)
71 struct blk_desc *desc;
75 ret = blk_get_device(if_type, devnum, &dev);
78 desc = dev_get_uclass_platdata(dev);
84 * This function is complicated with driver model. We look up the interface
85 * name in a local table. This gives us an interface type which we can match
86 * against the uclass of the block device's parent.
88 struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum)
90 enum uclass_id uclass_id;
96 if_type = if_typename_to_iftype(if_typename);
97 if (if_type == IF_TYPE_UNKNOWN) {
98 debug("%s: Unknown interface type '%s'\n", __func__,
102 uclass_id = if_type_to_uclass_id(if_type);
103 if (uclass_id == UCLASS_INVALID) {
104 debug("%s: Unknown uclass for interface type'\n",
105 if_typename_str[if_type]);
109 ret = uclass_get(UCLASS_BLK, &uc);
112 uclass_foreach_dev(dev, uc) {
113 struct blk_desc *desc = dev_get_uclass_platdata(dev);
115 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
116 if_type, devnum, dev->name, desc->if_type, desc->devnum);
117 if (desc->devnum != devnum)
120 /* Find out the parent device uclass */
121 if (device_get_uclass_id(dev->parent) != uclass_id) {
122 debug("%s: parent uclass %d, this dev %d\n", __func__,
123 device_get_uclass_id(dev->parent), uclass_id);
127 if (device_probe(dev))
130 debug("%s: Device desc %p\n", __func__, desc);
133 debug("%s: No device found\n", __func__);
139 * blk_get_by_device() - Get the block device descriptor for the given device
140 * @dev: Instance of a storage device
142 * Return: With block device descriptor on success , NULL if there is no such
145 struct blk_desc *blk_get_by_device(struct udevice *dev)
147 struct udevice *child_dev;
149 device_foreach_child(child_dev, dev) {
150 if (device_get_uclass_id(child_dev) != UCLASS_BLK)
153 return dev_get_uclass_platdata(child_dev);
156 debug("%s: No block device found\n", __func__);
162 * get_desc() - Get the block device descriptor for the given device number
164 * @if_type: Interface type
165 * @devnum: Device number (0 = first)
166 * @descp: Returns block device descriptor on success
167 * @return 0 on success, -ENODEV if there is no such device and no device
168 * with a higher device number, -ENOENT if there is no such device but there
169 * is one with a higher number, or other -ve on other error.
171 static int get_desc(enum if_type if_type, int devnum, struct blk_desc **descp)
173 bool found_more = false;
179 ret = uclass_get(UCLASS_BLK, &uc);
182 uclass_foreach_dev(dev, uc) {
183 struct blk_desc *desc = dev_get_uclass_platdata(dev);
185 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
186 if_type, devnum, dev->name, desc->if_type, desc->devnum);
187 if (desc->if_type == if_type) {
188 if (desc->devnum == devnum) {
189 ret = device_probe(dev);
195 } else if (desc->devnum > devnum) {
201 return found_more ? -ENOENT : -ENODEV;
204 int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart)
209 ret = blk_get_device(if_type, devnum, &dev);
213 return blk_select_hwpart(dev, hwpart);
216 int blk_list_part(enum if_type if_type)
218 struct blk_desc *desc;
222 for (ok = 0, devnum = 0;; ++devnum) {
223 ret = get_desc(if_type, devnum, &desc);
228 if (desc->part_type != PART_TYPE_UNKNOWN) {
241 int blk_print_part_devnum(enum if_type if_type, int devnum)
243 struct blk_desc *desc;
246 ret = get_desc(if_type, devnum, &desc);
249 if (desc->type == DEV_TYPE_UNKNOWN)
256 void blk_list_devices(enum if_type if_type)
258 struct blk_desc *desc;
263 ret = get_desc(if_type, i, &desc);
268 if (desc->type == DEV_TYPE_UNKNOWN)
269 continue; /* list only known devices */
270 printf("Device %d: ", i);
275 int blk_print_device_num(enum if_type if_type, int devnum)
277 struct blk_desc *desc;
280 ret = get_desc(if_type, devnum, &desc);
283 printf("\nIDE device %d: ", devnum);
289 int blk_show_device(enum if_type if_type, int devnum)
291 struct blk_desc *desc;
294 printf("\nDevice %d: ", devnum);
295 ret = get_desc(if_type, devnum, &desc);
296 if (ret == -ENODEV || ret == -ENOENT) {
297 printf("unknown device\n");
304 if (desc->type == DEV_TYPE_UNKNOWN)
310 ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
311 lbaint_t blkcnt, void *buffer)
313 struct blk_desc *desc;
317 ret = get_desc(if_type, devnum, &desc);
320 n = blk_dread(desc, start, blkcnt, buffer);
327 ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
328 lbaint_t blkcnt, const void *buffer)
330 struct blk_desc *desc;
333 ret = get_desc(if_type, devnum, &desc);
336 return blk_dwrite(desc, start, blkcnt, buffer);
339 int blk_select_hwpart(struct udevice *dev, int hwpart)
341 const struct blk_ops *ops = blk_get_ops(dev);
345 if (!ops->select_hwpart)
348 return ops->select_hwpart(dev, hwpart);
351 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart)
353 return blk_select_hwpart(desc->bdev, hwpart);
356 int blk_first_device(int if_type, struct udevice **devp)
358 struct blk_desc *desc;
361 ret = uclass_find_first_device(UCLASS_BLK, devp);
367 desc = dev_get_uclass_platdata(*devp);
368 if (desc->if_type == if_type)
370 ret = uclass_find_next_device(devp);
378 int blk_next_device(struct udevice **devp)
380 struct blk_desc *desc;
383 desc = dev_get_uclass_platdata(*devp);
384 if_type = desc->if_type;
386 ret = uclass_find_next_device(devp);
391 desc = dev_get_uclass_platdata(*devp);
392 if (desc->if_type == if_type)
397 int blk_find_device(int if_type, int devnum, struct udevice **devp)
403 ret = uclass_get(UCLASS_BLK, &uc);
406 uclass_foreach_dev(dev, uc) {
407 struct blk_desc *desc = dev_get_uclass_platdata(dev);
409 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
410 if_type, devnum, dev->name, desc->if_type, desc->devnum);
411 if (desc->if_type == if_type && desc->devnum == devnum) {
420 int blk_get_device(int if_type, int devnum, struct udevice **devp)
424 ret = blk_find_device(if_type, devnum, devp);
428 return device_probe(*devp);
431 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
432 lbaint_t blkcnt, void *buffer)
434 struct udevice *dev = block_dev->bdev;
435 const struct blk_ops *ops = blk_get_ops(dev);
441 if (blkcache_read(block_dev->if_type, block_dev->devnum,
442 start, blkcnt, block_dev->blksz, buffer))
444 blks_read = ops->read(dev, start, blkcnt, buffer);
445 if (blks_read == blkcnt)
446 blkcache_fill(block_dev->if_type, block_dev->devnum,
447 start, blkcnt, block_dev->blksz, buffer);
452 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
453 lbaint_t blkcnt, const void *buffer)
455 struct udevice *dev = block_dev->bdev;
456 const struct blk_ops *ops = blk_get_ops(dev);
461 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
462 return ops->write(dev, start, blkcnt, buffer);
465 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
468 struct udevice *dev = block_dev->bdev;
469 const struct blk_ops *ops = blk_get_ops(dev);
474 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
475 return ops->erase(dev, start, blkcnt);
478 int blk_get_from_parent(struct udevice *parent, struct udevice **devp)
484 device_find_first_child(parent, &dev);
486 debug("%s: No block device found for parent '%s'\n", __func__,
490 id = device_get_uclass_id(dev);
491 if (id != UCLASS_BLK) {
492 debug("%s: Incorrect uclass %s for block device '%s'\n",
493 __func__, uclass_get_name(id), dev->name);
496 ret = device_probe(dev);
504 int blk_find_max_devnum(enum if_type if_type)
507 int max_devnum = -ENODEV;
511 ret = uclass_get(UCLASS_BLK, &uc);
514 uclass_foreach_dev(dev, uc) {
515 struct blk_desc *desc = dev_get_uclass_platdata(dev);
517 if (desc->if_type == if_type && desc->devnum > max_devnum)
518 max_devnum = desc->devnum;
524 int blk_next_free_devnum(enum if_type if_type)
528 ret = blk_find_max_devnum(if_type);
537 static int blk_claim_devnum(enum if_type if_type, int devnum)
543 ret = uclass_get(UCLASS_BLK, &uc);
546 uclass_foreach_dev(dev, uc) {
547 struct blk_desc *desc = dev_get_uclass_platdata(dev);
549 if (desc->if_type == if_type && desc->devnum == devnum) {
550 int next = blk_next_free_devnum(if_type);
562 int blk_create_device(struct udevice *parent, const char *drv_name,
563 const char *name, int if_type, int devnum, int blksz,
564 lbaint_t lba, struct udevice **devp)
566 struct blk_desc *desc;
571 devnum = blk_next_free_devnum(if_type);
573 ret = blk_claim_devnum(if_type, devnum);
574 if (ret < 0 && ret != -ENOENT)
579 ret = device_bind_driver(parent, drv_name, name, &dev);
582 desc = dev_get_uclass_platdata(dev);
583 desc->if_type = if_type;
585 desc->log2blksz = LOG2(desc->blksz);
587 desc->part_type = PART_TYPE_UNKNOWN;
589 desc->devnum = devnum;
595 int blk_create_devicef(struct udevice *parent, const char *drv_name,
596 const char *name, int if_type, int devnum, int blksz,
597 lbaint_t lba, struct udevice **devp)
599 char dev_name[30], *str;
602 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
603 str = strdup(dev_name);
607 ret = blk_create_device(parent, drv_name, str, if_type, devnum,
613 device_set_name_alloced(*devp);
618 int blk_unbind_all(int if_type)
621 struct udevice *dev, *next;
624 ret = uclass_get(UCLASS_BLK, &uc);
627 uclass_foreach_dev_safe(dev, next, uc) {
628 struct blk_desc *desc = dev_get_uclass_platdata(dev);
630 if (desc->if_type == if_type) {
631 ret = device_remove(dev, DM_REMOVE_NORMAL);
634 ret = device_unbind(dev);
643 static int blk_post_probe(struct udevice *dev)
645 #if defined(CONFIG_PARTITIONS) && defined(CONFIG_HAVE_BLOCK_DEVICE)
646 struct blk_desc *desc = dev_get_uclass_platdata(dev);
654 UCLASS_DRIVER(blk) = {
657 .post_probe = blk_post_probe,
658 .per_device_platdata_auto_alloc_size = sizeof(struct blk_desc),