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 int blk_find_max_devnum(enum if_type if_type)
515 int max_devnum = -ENODEV;
519 ret = uclass_get(UCLASS_BLK, &uc);
522 uclass_foreach_dev(dev, uc) {
523 struct blk_desc *desc = dev_get_uclass_plat(dev);
525 if (desc->if_type == if_type && desc->devnum > max_devnum)
526 max_devnum = desc->devnum;
532 int blk_next_free_devnum(enum if_type if_type)
536 ret = blk_find_max_devnum(if_type);
545 static int blk_flags_check(struct udevice *dev, enum blk_flag_t req_flags)
547 const struct blk_desc *desc = dev_get_uclass_plat(dev);
548 enum blk_flag_t flags;
550 flags = desc->removable ? BLKF_REMOVABLE : BLKF_FIXED;
552 return flags & req_flags ? 0 : 1;
555 int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp)
559 for (ret = uclass_first_device_err(UCLASS_BLK, devp);
561 ret = uclass_next_device_err(devp)) {
562 if (!blk_flags_check(*devp, flags))
569 int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp)
573 for (ret = uclass_next_device_err(devp);
575 ret = uclass_next_device_err(devp)) {
576 if (!blk_flags_check(*devp, flags))
583 int blk_count_devices(enum blk_flag_t flag)
588 blk_foreach_probe(flag, dev)
594 static int blk_claim_devnum(enum if_type if_type, int devnum)
600 ret = uclass_get(UCLASS_BLK, &uc);
603 uclass_foreach_dev(dev, uc) {
604 struct blk_desc *desc = dev_get_uclass_plat(dev);
606 if (desc->if_type == if_type && desc->devnum == devnum) {
607 int next = blk_next_free_devnum(if_type);
619 int blk_create_device(struct udevice *parent, const char *drv_name,
620 const char *name, int if_type, int devnum, int blksz,
621 lbaint_t lba, struct udevice **devp)
623 struct blk_desc *desc;
628 devnum = blk_next_free_devnum(if_type);
630 ret = blk_claim_devnum(if_type, devnum);
631 if (ret < 0 && ret != -ENOENT)
636 ret = device_bind_driver(parent, drv_name, name, &dev);
639 desc = dev_get_uclass_plat(dev);
640 desc->if_type = if_type;
642 desc->log2blksz = LOG2(desc->blksz);
644 desc->part_type = PART_TYPE_UNKNOWN;
646 desc->devnum = devnum;
652 int blk_create_devicef(struct udevice *parent, const char *drv_name,
653 const char *name, int if_type, int devnum, int blksz,
654 lbaint_t lba, struct udevice **devp)
656 char dev_name[30], *str;
659 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
660 str = strdup(dev_name);
664 ret = blk_create_device(parent, drv_name, str, if_type, devnum,
670 device_set_name_alloced(*devp);
675 int blk_probe_or_unbind(struct udevice *dev)
679 ret = device_probe(dev);
681 log_debug("probing %s failed\n", dev->name);
688 int blk_unbind_all(int if_type)
691 struct udevice *dev, *next;
694 ret = uclass_get(UCLASS_BLK, &uc);
697 uclass_foreach_dev_safe(dev, next, uc) {
698 struct blk_desc *desc = dev_get_uclass_plat(dev);
700 if (desc->if_type == if_type) {
701 ret = device_remove(dev, DM_REMOVE_NORMAL);
704 ret = device_unbind(dev);
713 static int blk_post_probe(struct udevice *dev)
715 if (IS_ENABLED(CONFIG_PARTITIONS) &&
716 IS_ENABLED(CONFIG_HAVE_BLOCK_DEVICE)) {
717 struct blk_desc *desc = dev_get_uclass_plat(dev);
725 UCLASS_DRIVER(blk) = {
728 .post_probe = blk_post_probe,
729 .per_device_plat_auto = sizeof(struct blk_desc),