1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
13 #include <dm/device-internal.h>
15 #include <dm/uclass-internal.h>
16 #include <linux/err.h>
18 static const char *if_typename_str[IF_TYPE_COUNT] = {
19 [IF_TYPE_IDE] = "ide",
20 [IF_TYPE_SCSI] = "scsi",
21 [IF_TYPE_ATAPI] = "atapi",
22 [IF_TYPE_USB] = "usb",
23 [IF_TYPE_DOC] = "doc",
24 [IF_TYPE_MMC] = "mmc",
26 [IF_TYPE_SATA] = "sata",
27 [IF_TYPE_HOST] = "host",
28 [IF_TYPE_NVME] = "nvme",
29 [IF_TYPE_EFI] = "efi",
30 [IF_TYPE_VIRTIO] = "virtio",
33 static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
34 [IF_TYPE_IDE] = UCLASS_IDE,
35 [IF_TYPE_SCSI] = UCLASS_SCSI,
36 [IF_TYPE_ATAPI] = UCLASS_INVALID,
37 [IF_TYPE_USB] = UCLASS_MASS_STORAGE,
38 [IF_TYPE_DOC] = UCLASS_INVALID,
39 [IF_TYPE_MMC] = UCLASS_MMC,
40 [IF_TYPE_SD] = UCLASS_INVALID,
41 [IF_TYPE_SATA] = UCLASS_AHCI,
42 [IF_TYPE_HOST] = UCLASS_ROOT,
43 [IF_TYPE_NVME] = UCLASS_NVME,
44 [IF_TYPE_EFI] = UCLASS_EFI,
45 [IF_TYPE_VIRTIO] = UCLASS_VIRTIO,
48 static enum if_type if_typename_to_iftype(const char *if_typename)
52 for (i = 0; i < IF_TYPE_COUNT; i++) {
53 if (if_typename_str[i] &&
54 !strcmp(if_typename, if_typename_str[i]))
58 return IF_TYPE_UNKNOWN;
61 static enum uclass_id if_type_to_uclass_id(enum if_type if_type)
63 return if_type_uclass_id[if_type];
66 const char *blk_get_if_type_name(enum if_type if_type)
68 return if_typename_str[if_type];
71 struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum)
73 struct blk_desc *desc;
77 ret = blk_get_device(if_type, devnum, &dev);
80 desc = dev_get_uclass_platdata(dev);
86 * This function is complicated with driver model. We look up the interface
87 * name in a local table. This gives us an interface type which we can match
88 * against the uclass of the block device's parent.
90 struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum)
92 enum uclass_id uclass_id;
98 if_type = if_typename_to_iftype(if_typename);
99 if (if_type == IF_TYPE_UNKNOWN) {
100 debug("%s: Unknown interface type '%s'\n", __func__,
104 uclass_id = if_type_to_uclass_id(if_type);
105 if (uclass_id == UCLASS_INVALID) {
106 debug("%s: Unknown uclass for interface type'\n",
107 if_typename_str[if_type]);
111 ret = uclass_get(UCLASS_BLK, &uc);
114 uclass_foreach_dev(dev, uc) {
115 struct blk_desc *desc = dev_get_uclass_platdata(dev);
117 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
118 if_type, devnum, dev->name, desc->if_type, desc->devnum);
119 if (desc->devnum != devnum)
122 /* Find out the parent device uclass */
123 if (device_get_uclass_id(dev->parent) != uclass_id) {
124 debug("%s: parent uclass %d, this dev %d\n", __func__,
125 device_get_uclass_id(dev->parent), uclass_id);
129 if (device_probe(dev))
132 debug("%s: Device desc %p\n", __func__, desc);
135 debug("%s: No device found\n", __func__);
141 * blk_get_by_device() - Get the block device descriptor for the given device
142 * @dev: Instance of a storage device
144 * Return: With block device descriptor on success , NULL if there is no such
147 struct blk_desc *blk_get_by_device(struct udevice *dev)
149 struct udevice *child_dev;
151 device_foreach_child(child_dev, dev) {
152 if (device_get_uclass_id(child_dev) != UCLASS_BLK)
155 return dev_get_uclass_platdata(child_dev);
158 debug("%s: No block device found\n", __func__);
164 * get_desc() - Get the block device descriptor for the given device number
166 * @if_type: Interface type
167 * @devnum: Device number (0 = first)
168 * @descp: Returns block device descriptor on success
169 * @return 0 on success, -ENODEV if there is no such device and no device
170 * with a higher device number, -ENOENT if there is no such device but there
171 * is one with a higher number, or other -ve on other error.
173 static int get_desc(enum if_type if_type, int devnum, struct blk_desc **descp)
175 bool found_more = false;
181 ret = uclass_get(UCLASS_BLK, &uc);
184 uclass_foreach_dev(dev, uc) {
185 struct blk_desc *desc = dev_get_uclass_platdata(dev);
187 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
188 if_type, devnum, dev->name, desc->if_type, desc->devnum);
189 if (desc->if_type == if_type) {
190 if (desc->devnum == devnum) {
191 ret = device_probe(dev);
197 } else if (desc->devnum > devnum) {
203 return found_more ? -ENOENT : -ENODEV;
206 int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart)
211 ret = blk_get_device(if_type, devnum, &dev);
215 return blk_select_hwpart(dev, hwpart);
218 int blk_list_part(enum if_type if_type)
220 struct blk_desc *desc;
224 for (ok = 0, devnum = 0;; ++devnum) {
225 ret = get_desc(if_type, devnum, &desc);
230 if (desc->part_type != PART_TYPE_UNKNOWN) {
243 int blk_print_part_devnum(enum if_type if_type, int devnum)
245 struct blk_desc *desc;
248 ret = get_desc(if_type, devnum, &desc);
251 if (desc->type == DEV_TYPE_UNKNOWN)
258 void blk_list_devices(enum if_type if_type)
260 struct blk_desc *desc;
265 ret = get_desc(if_type, i, &desc);
270 if (desc->type == DEV_TYPE_UNKNOWN)
271 continue; /* list only known devices */
272 printf("Device %d: ", i);
277 int blk_print_device_num(enum if_type if_type, int devnum)
279 struct blk_desc *desc;
282 ret = get_desc(if_type, devnum, &desc);
285 printf("\nIDE device %d: ", devnum);
291 int blk_show_device(enum if_type if_type, int devnum)
293 struct blk_desc *desc;
296 printf("\nDevice %d: ", devnum);
297 ret = get_desc(if_type, devnum, &desc);
298 if (ret == -ENODEV || ret == -ENOENT) {
299 printf("unknown device\n");
306 if (desc->type == DEV_TYPE_UNKNOWN)
312 ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
313 lbaint_t blkcnt, void *buffer)
315 struct blk_desc *desc;
319 ret = get_desc(if_type, devnum, &desc);
322 n = blk_dread(desc, start, blkcnt, buffer);
329 ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
330 lbaint_t blkcnt, const void *buffer)
332 struct blk_desc *desc;
335 ret = get_desc(if_type, devnum, &desc);
338 return blk_dwrite(desc, start, blkcnt, buffer);
341 int blk_select_hwpart(struct udevice *dev, int hwpart)
343 const struct blk_ops *ops = blk_get_ops(dev);
347 if (!ops->select_hwpart)
350 return ops->select_hwpart(dev, hwpart);
353 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart)
355 return blk_select_hwpart(desc->bdev, hwpart);
358 int blk_first_device(int if_type, struct udevice **devp)
360 struct blk_desc *desc;
363 ret = uclass_find_first_device(UCLASS_BLK, devp);
369 desc = dev_get_uclass_platdata(*devp);
370 if (desc->if_type == if_type)
372 ret = uclass_find_next_device(devp);
380 int blk_next_device(struct udevice **devp)
382 struct blk_desc *desc;
385 desc = dev_get_uclass_platdata(*devp);
386 if_type = desc->if_type;
388 ret = uclass_find_next_device(devp);
393 desc = dev_get_uclass_platdata(*devp);
394 if (desc->if_type == if_type)
399 int blk_find_device(int if_type, int devnum, struct udevice **devp)
405 ret = uclass_get(UCLASS_BLK, &uc);
408 uclass_foreach_dev(dev, uc) {
409 struct blk_desc *desc = dev_get_uclass_platdata(dev);
411 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
412 if_type, devnum, dev->name, desc->if_type, desc->devnum);
413 if (desc->if_type == if_type && desc->devnum == devnum) {
422 int blk_get_device(int if_type, int devnum, struct udevice **devp)
426 ret = blk_find_device(if_type, devnum, devp);
430 return device_probe(*devp);
433 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
434 lbaint_t blkcnt, void *buffer)
436 struct udevice *dev = block_dev->bdev;
437 const struct blk_ops *ops = blk_get_ops(dev);
443 if (blkcache_read(block_dev->if_type, block_dev->devnum,
444 start, blkcnt, block_dev->blksz, buffer))
446 blks_read = ops->read(dev, start, blkcnt, buffer);
447 if (blks_read == blkcnt)
448 blkcache_fill(block_dev->if_type, block_dev->devnum,
449 start, blkcnt, block_dev->blksz, buffer);
454 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
455 lbaint_t blkcnt, const void *buffer)
457 struct udevice *dev = block_dev->bdev;
458 const struct blk_ops *ops = blk_get_ops(dev);
463 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
464 return ops->write(dev, start, blkcnt, buffer);
467 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
470 struct udevice *dev = block_dev->bdev;
471 const struct blk_ops *ops = blk_get_ops(dev);
476 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
477 return ops->erase(dev, start, blkcnt);
480 int blk_get_from_parent(struct udevice *parent, struct udevice **devp)
486 device_find_first_child(parent, &dev);
488 debug("%s: No block device found for parent '%s'\n", __func__,
492 id = device_get_uclass_id(dev);
493 if (id != UCLASS_BLK) {
494 debug("%s: Incorrect uclass %s for block device '%s'\n",
495 __func__, uclass_get_name(id), dev->name);
498 ret = device_probe(dev);
506 int blk_find_max_devnum(enum if_type if_type)
509 int max_devnum = -ENODEV;
513 ret = uclass_get(UCLASS_BLK, &uc);
516 uclass_foreach_dev(dev, uc) {
517 struct blk_desc *desc = dev_get_uclass_platdata(dev);
519 if (desc->if_type == if_type && desc->devnum > max_devnum)
520 max_devnum = desc->devnum;
526 int blk_next_free_devnum(enum if_type if_type)
530 ret = blk_find_max_devnum(if_type);
539 static int blk_claim_devnum(enum if_type if_type, int devnum)
545 ret = uclass_get(UCLASS_BLK, &uc);
548 uclass_foreach_dev(dev, uc) {
549 struct blk_desc *desc = dev_get_uclass_platdata(dev);
551 if (desc->if_type == if_type && desc->devnum == devnum) {
552 int next = blk_next_free_devnum(if_type);
564 int blk_create_device(struct udevice *parent, const char *drv_name,
565 const char *name, int if_type, int devnum, int blksz,
566 lbaint_t lba, struct udevice **devp)
568 struct blk_desc *desc;
573 devnum = blk_next_free_devnum(if_type);
575 ret = blk_claim_devnum(if_type, devnum);
576 if (ret < 0 && ret != -ENOENT)
581 ret = device_bind_driver(parent, drv_name, name, &dev);
584 desc = dev_get_uclass_platdata(dev);
585 desc->if_type = if_type;
587 desc->log2blksz = LOG2(desc->blksz);
589 desc->part_type = PART_TYPE_UNKNOWN;
591 desc->devnum = devnum;
597 int blk_create_devicef(struct udevice *parent, const char *drv_name,
598 const char *name, int if_type, int devnum, int blksz,
599 lbaint_t lba, struct udevice **devp)
601 char dev_name[30], *str;
604 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
605 str = strdup(dev_name);
609 ret = blk_create_device(parent, drv_name, str, if_type, devnum,
615 device_set_name_alloced(*devp);
620 int blk_unbind_all(int if_type)
623 struct udevice *dev, *next;
626 ret = uclass_get(UCLASS_BLK, &uc);
629 uclass_foreach_dev_safe(dev, next, uc) {
630 struct blk_desc *desc = dev_get_uclass_platdata(dev);
632 if (desc->if_type == if_type) {
633 ret = device_remove(dev, DM_REMOVE_NORMAL);
636 ret = device_unbind(dev);
645 static int blk_post_probe(struct udevice *dev)
647 #if defined(CONFIG_PARTITIONS) && defined(CONFIG_HAVE_BLOCK_DEVICE)
648 struct blk_desc *desc = dev_get_uclass_platdata(dev);
656 UCLASS_DRIVER(blk) = {
659 .post_probe = blk_post_probe,
660 .per_device_platdata_auto_alloc_size = sizeof(struct blk_desc),