1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
7 #define LOG_CATEGORY UCLASS_BLK
15 #include <dm/device-internal.h>
17 #include <dm/uclass-internal.h>
18 #include <linux/err.h>
20 static const char *if_typename_str[IF_TYPE_COUNT] = {
21 [IF_TYPE_IDE] = "ide",
22 [IF_TYPE_SCSI] = "scsi",
23 [IF_TYPE_ATAPI] = "atapi",
24 [IF_TYPE_USB] = "usb",
25 [IF_TYPE_DOC] = "doc",
26 [IF_TYPE_MMC] = "mmc",
28 [IF_TYPE_SATA] = "sata",
29 [IF_TYPE_HOST] = "host",
30 [IF_TYPE_NVME] = "nvme",
31 [IF_TYPE_EFI_MEDIA] = "efi",
32 [IF_TYPE_EFI_LOADER] = "efiloader",
33 [IF_TYPE_VIRTIO] = "virtio",
34 [IF_TYPE_PVBLOCK] = "pvblock",
37 static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
38 [IF_TYPE_IDE] = UCLASS_IDE,
39 [IF_TYPE_SCSI] = UCLASS_SCSI,
40 [IF_TYPE_ATAPI] = UCLASS_INVALID,
41 [IF_TYPE_USB] = UCLASS_MASS_STORAGE,
42 [IF_TYPE_DOC] = UCLASS_INVALID,
43 [IF_TYPE_MMC] = UCLASS_MMC,
44 [IF_TYPE_SD] = UCLASS_INVALID,
45 [IF_TYPE_SATA] = UCLASS_AHCI,
46 [IF_TYPE_HOST] = UCLASS_ROOT,
47 [IF_TYPE_NVME] = UCLASS_NVME,
48 [IF_TYPE_EFI_MEDIA] = UCLASS_EFI_MEDIA,
49 [IF_TYPE_EFI_LOADER] = UCLASS_EFI_LOADER,
50 [IF_TYPE_VIRTIO] = UCLASS_VIRTIO,
51 [IF_TYPE_PVBLOCK] = UCLASS_PVBLOCK,
54 static enum if_type if_typename_to_iftype(const char *if_typename)
58 for (i = 0; i < IF_TYPE_COUNT; i++) {
59 if (if_typename_str[i] &&
60 !strcmp(if_typename, if_typename_str[i]))
64 return IF_TYPE_UNKNOWN;
67 static enum uclass_id if_type_to_uclass_id(enum if_type if_type)
69 return if_type_uclass_id[if_type];
72 const char *blk_get_if_type_name(enum if_type if_type)
74 return if_typename_str[if_type];
77 struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum)
79 struct blk_desc *desc;
83 ret = blk_get_device(if_type, devnum, &dev);
86 desc = dev_get_uclass_plat(dev);
92 * This function is complicated with driver model. We look up the interface
93 * name in a local table. This gives us an interface type which we can match
94 * against the uclass of the block device's parent.
96 struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum)
98 enum uclass_id uclass_id;
104 if_type = if_typename_to_iftype(if_typename);
105 if (if_type == IF_TYPE_UNKNOWN) {
106 debug("%s: Unknown interface type '%s'\n", __func__,
110 uclass_id = if_type_to_uclass_id(if_type);
111 if (uclass_id == UCLASS_INVALID) {
112 debug("%s: Unknown uclass for interface type'\n",
113 if_typename_str[if_type]);
117 ret = uclass_get(UCLASS_BLK, &uc);
120 uclass_foreach_dev(dev, uc) {
121 struct blk_desc *desc = dev_get_uclass_plat(dev);
123 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
124 if_type, devnum, dev->name, desc->if_type, desc->devnum);
125 if (desc->devnum != devnum)
128 /* Find out the parent device uclass */
129 if (device_get_uclass_id(dev->parent) != uclass_id) {
130 debug("%s: parent uclass %d, this dev %d\n", __func__,
131 device_get_uclass_id(dev->parent), uclass_id);
135 if (device_probe(dev))
138 debug("%s: Device desc %p\n", __func__, desc);
141 debug("%s: No device found\n", __func__);
147 * blk_get_by_device() - Get the block device descriptor for the given device
148 * @dev: Instance of a storage device
150 * Return: With block device descriptor on success , NULL if there is no such
153 struct blk_desc *blk_get_by_device(struct udevice *dev)
155 struct udevice *child_dev;
157 device_foreach_child(child_dev, dev) {
158 if (device_get_uclass_id(child_dev) != UCLASS_BLK)
161 return dev_get_uclass_plat(child_dev);
164 debug("%s: No block device found\n", __func__);
170 * get_desc() - Get the block device descriptor for the given device number
172 * @if_type: Interface type
173 * @devnum: Device number (0 = first)
174 * @descp: Returns block device descriptor on success
175 * Return: 0 on success, -ENODEV if there is no such device and no device
176 * with a higher device number, -ENOENT if there is no such device but there
177 * is one with a higher number, or other -ve on other error.
179 static int get_desc(enum if_type if_type, int devnum, struct blk_desc **descp)
181 bool found_more = false;
187 ret = uclass_get(UCLASS_BLK, &uc);
190 uclass_foreach_dev(dev, uc) {
191 struct blk_desc *desc = dev_get_uclass_plat(dev);
193 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
194 if_type, devnum, dev->name, desc->if_type, desc->devnum);
195 if (desc->if_type == if_type) {
196 if (desc->devnum == devnum) {
197 ret = device_probe(dev);
203 } else if (desc->devnum > devnum) {
209 return found_more ? -ENOENT : -ENODEV;
212 int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart)
217 ret = blk_get_device(if_type, devnum, &dev);
221 return blk_select_hwpart(dev, hwpart);
224 int blk_list_part(enum if_type if_type)
226 struct blk_desc *desc;
230 for (ok = 0, devnum = 0;; ++devnum) {
231 ret = get_desc(if_type, devnum, &desc);
236 if (desc->part_type != PART_TYPE_UNKNOWN) {
249 int blk_print_part_devnum(enum if_type if_type, int devnum)
251 struct blk_desc *desc;
254 ret = get_desc(if_type, devnum, &desc);
257 if (desc->type == DEV_TYPE_UNKNOWN)
264 void blk_list_devices(enum if_type if_type)
266 struct blk_desc *desc;
271 ret = get_desc(if_type, i, &desc);
276 if (desc->type == DEV_TYPE_UNKNOWN)
277 continue; /* list only known devices */
278 printf("Device %d: ", i);
283 int blk_print_device_num(enum if_type if_type, int devnum)
285 struct blk_desc *desc;
288 ret = get_desc(if_type, devnum, &desc);
291 printf("\nIDE device %d: ", devnum);
297 int blk_show_device(enum if_type if_type, int devnum)
299 struct blk_desc *desc;
302 printf("\nDevice %d: ", devnum);
303 ret = get_desc(if_type, devnum, &desc);
304 if (ret == -ENODEV || ret == -ENOENT) {
305 printf("unknown device\n");
312 if (desc->type == DEV_TYPE_UNKNOWN)
318 ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
319 lbaint_t blkcnt, void *buffer)
321 struct blk_desc *desc;
325 ret = get_desc(if_type, devnum, &desc);
328 n = blk_dread(desc, start, blkcnt, buffer);
335 ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
336 lbaint_t blkcnt, const void *buffer)
338 struct blk_desc *desc;
341 ret = get_desc(if_type, devnum, &desc);
344 return blk_dwrite(desc, start, blkcnt, buffer);
347 int blk_select_hwpart(struct udevice *dev, int hwpart)
349 const struct blk_ops *ops = blk_get_ops(dev);
353 if (!ops->select_hwpart)
356 return ops->select_hwpart(dev, hwpart);
359 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart)
361 return blk_select_hwpart(desc->bdev, hwpart);
364 int blk_first_device(int if_type, struct udevice **devp)
366 struct blk_desc *desc;
369 ret = uclass_find_first_device(UCLASS_BLK, devp);
375 desc = dev_get_uclass_plat(*devp);
376 if (desc->if_type == if_type)
378 ret = uclass_find_next_device(devp);
386 int blk_next_device(struct udevice **devp)
388 struct blk_desc *desc;
391 desc = dev_get_uclass_plat(*devp);
392 if_type = desc->if_type;
394 ret = uclass_find_next_device(devp);
399 desc = dev_get_uclass_plat(*devp);
400 if (desc->if_type == if_type)
405 int blk_find_device(int if_type, int devnum, struct udevice **devp)
411 ret = uclass_get(UCLASS_BLK, &uc);
414 uclass_foreach_dev(dev, uc) {
415 struct blk_desc *desc = dev_get_uclass_plat(dev);
417 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
418 if_type, devnum, dev->name, desc->if_type, desc->devnum);
419 if (desc->if_type == if_type && desc->devnum == devnum) {
428 int blk_get_device(int if_type, int devnum, struct udevice **devp)
432 ret = blk_find_device(if_type, devnum, devp);
436 return device_probe(*devp);
439 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
440 lbaint_t blkcnt, void *buffer)
442 struct udevice *dev = block_dev->bdev;
443 const struct blk_ops *ops = blk_get_ops(dev);
449 if (blkcache_read(block_dev->if_type, block_dev->devnum,
450 start, blkcnt, block_dev->blksz, buffer))
452 blks_read = ops->read(dev, start, blkcnt, buffer);
453 if (blks_read == blkcnt)
454 blkcache_fill(block_dev->if_type, block_dev->devnum,
455 start, blkcnt, block_dev->blksz, buffer);
460 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
461 lbaint_t blkcnt, const void *buffer)
463 struct udevice *dev = block_dev->bdev;
464 const struct blk_ops *ops = blk_get_ops(dev);
469 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
470 return ops->write(dev, start, blkcnt, buffer);
473 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
476 struct udevice *dev = block_dev->bdev;
477 const struct blk_ops *ops = blk_get_ops(dev);
482 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
483 return ops->erase(dev, start, blkcnt);
486 int blk_get_from_parent(struct udevice *parent, struct udevice **devp)
492 device_find_first_child(parent, &dev);
494 debug("%s: No block device found for parent '%s'\n", __func__,
498 id = device_get_uclass_id(dev);
499 if (id != UCLASS_BLK) {
500 debug("%s: Incorrect uclass %s for block device '%s'\n",
501 __func__, uclass_get_name(id), dev->name);
504 ret = device_probe(dev);
512 const char *blk_get_devtype(struct udevice *dev)
514 struct udevice *parent = dev_get_parent(dev);
516 return uclass_get_name(device_get_uclass_id(parent));
519 int blk_find_max_devnum(enum if_type if_type)
522 int max_devnum = -ENODEV;
526 ret = uclass_get(UCLASS_BLK, &uc);
529 uclass_foreach_dev(dev, uc) {
530 struct blk_desc *desc = dev_get_uclass_plat(dev);
532 if (desc->if_type == if_type && desc->devnum > max_devnum)
533 max_devnum = desc->devnum;
539 int blk_next_free_devnum(enum if_type if_type)
543 ret = blk_find_max_devnum(if_type);
552 static int blk_flags_check(struct udevice *dev, enum blk_flag_t req_flags)
554 const struct blk_desc *desc = dev_get_uclass_plat(dev);
555 enum blk_flag_t flags;
557 flags = desc->removable ? BLKF_REMOVABLE : BLKF_FIXED;
559 return flags & req_flags ? 0 : 1;
562 int blk_find_first(enum blk_flag_t flags, struct udevice **devp)
566 for (ret = uclass_find_first_device(UCLASS_BLK, devp);
567 *devp && !blk_flags_check(*devp, flags);
568 ret = uclass_find_next_device(devp))
574 int blk_find_next(enum blk_flag_t flags, struct udevice **devp)
578 for (ret = uclass_find_next_device(devp);
579 *devp && !blk_flags_check(*devp, flags);
580 ret = uclass_find_next_device(devp))
586 int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp)
590 for (ret = uclass_first_device_err(UCLASS_BLK, devp);
592 ret = uclass_next_device_err(devp)) {
593 if (!blk_flags_check(*devp, flags))
600 int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp)
604 for (ret = uclass_next_device_err(devp);
606 ret = uclass_next_device_err(devp)) {
607 if (!blk_flags_check(*devp, flags))
614 int blk_count_devices(enum blk_flag_t flag)
619 blk_foreach_probe(flag, dev)
625 static int blk_claim_devnum(enum if_type if_type, int devnum)
631 ret = uclass_get(UCLASS_BLK, &uc);
634 uclass_foreach_dev(dev, uc) {
635 struct blk_desc *desc = dev_get_uclass_plat(dev);
637 if (desc->if_type == if_type && desc->devnum == devnum) {
638 int next = blk_next_free_devnum(if_type);
650 int blk_create_device(struct udevice *parent, const char *drv_name,
651 const char *name, int if_type, int devnum, int blksz,
652 lbaint_t lba, struct udevice **devp)
654 struct blk_desc *desc;
659 devnum = blk_next_free_devnum(if_type);
661 ret = blk_claim_devnum(if_type, devnum);
662 if (ret < 0 && ret != -ENOENT)
667 ret = device_bind_driver(parent, drv_name, name, &dev);
670 desc = dev_get_uclass_plat(dev);
671 desc->if_type = if_type;
673 desc->log2blksz = LOG2(desc->blksz);
675 desc->part_type = PART_TYPE_UNKNOWN;
677 desc->devnum = devnum;
683 int blk_create_devicef(struct udevice *parent, const char *drv_name,
684 const char *name, int if_type, int devnum, int blksz,
685 lbaint_t lba, struct udevice **devp)
687 char dev_name[30], *str;
690 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
691 str = strdup(dev_name);
695 ret = blk_create_device(parent, drv_name, str, if_type, devnum,
701 device_set_name_alloced(*devp);
706 int blk_probe_or_unbind(struct udevice *dev)
710 ret = device_probe(dev);
712 log_debug("probing %s failed\n", dev->name);
719 int blk_unbind_all(int if_type)
722 struct udevice *dev, *next;
725 ret = uclass_get(UCLASS_BLK, &uc);
728 uclass_foreach_dev_safe(dev, next, uc) {
729 struct blk_desc *desc = dev_get_uclass_plat(dev);
731 if (desc->if_type == if_type) {
732 ret = device_remove(dev, DM_REMOVE_NORMAL);
735 ret = device_unbind(dev);
744 static int blk_post_probe(struct udevice *dev)
746 if (CONFIG_IS_ENABLED(PARTITIONS) &&
747 IS_ENABLED(CONFIG_HAVE_BLOCK_DEVICE)) {
748 struct blk_desc *desc = dev_get_uclass_plat(dev);
756 UCLASS_DRIVER(blk) = {
759 .post_probe = blk_post_probe,
760 .per_device_plat_auto = sizeof(struct blk_desc),