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>
13 #include <linux/err.h>
15 static const char *if_typename_str[IF_TYPE_COUNT] = {
16 [IF_TYPE_IDE] = "ide",
17 [IF_TYPE_SCSI] = "scsi",
18 [IF_TYPE_ATAPI] = "atapi",
19 [IF_TYPE_USB] = "usb",
20 [IF_TYPE_DOC] = "doc",
21 [IF_TYPE_MMC] = "mmc",
23 [IF_TYPE_SATA] = "sata",
24 [IF_TYPE_HOST] = "host",
25 [IF_TYPE_NVME] = "nvme",
26 [IF_TYPE_EFI] = "efi",
27 [IF_TYPE_VIRTIO] = "virtio",
30 static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
31 [IF_TYPE_IDE] = UCLASS_IDE,
32 [IF_TYPE_SCSI] = UCLASS_SCSI,
33 [IF_TYPE_ATAPI] = UCLASS_INVALID,
34 [IF_TYPE_USB] = UCLASS_MASS_STORAGE,
35 [IF_TYPE_DOC] = UCLASS_INVALID,
36 [IF_TYPE_MMC] = UCLASS_MMC,
37 [IF_TYPE_SD] = UCLASS_INVALID,
38 [IF_TYPE_SATA] = UCLASS_AHCI,
39 [IF_TYPE_HOST] = UCLASS_ROOT,
40 [IF_TYPE_NVME] = UCLASS_NVME,
41 [IF_TYPE_EFI] = UCLASS_EFI,
42 [IF_TYPE_VIRTIO] = UCLASS_VIRTIO,
45 static enum if_type if_typename_to_iftype(const char *if_typename)
49 for (i = 0; i < IF_TYPE_COUNT; i++) {
50 if (if_typename_str[i] &&
51 !strcmp(if_typename, if_typename_str[i]))
55 return IF_TYPE_UNKNOWN;
58 static enum uclass_id if_type_to_uclass_id(enum if_type if_type)
60 return if_type_uclass_id[if_type];
63 const char *blk_get_if_type_name(enum if_type if_type)
65 return if_typename_str[if_type];
68 struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum)
70 struct blk_desc *desc;
74 ret = blk_get_device(if_type, devnum, &dev);
77 desc = dev_get_uclass_platdata(dev);
83 * This function is complicated with driver model. We look up the interface
84 * name in a local table. This gives us an interface type which we can match
85 * against the uclass of the block device's parent.
87 struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum)
89 enum uclass_id uclass_id;
95 if_type = if_typename_to_iftype(if_typename);
96 if (if_type == IF_TYPE_UNKNOWN) {
97 debug("%s: Unknown interface type '%s'\n", __func__,
101 uclass_id = if_type_to_uclass_id(if_type);
102 if (uclass_id == UCLASS_INVALID) {
103 debug("%s: Unknown uclass for interface type'\n",
104 if_typename_str[if_type]);
108 ret = uclass_get(UCLASS_BLK, &uc);
111 uclass_foreach_dev(dev, uc) {
112 struct blk_desc *desc = dev_get_uclass_platdata(dev);
114 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
115 if_type, devnum, dev->name, desc->if_type, desc->devnum);
116 if (desc->devnum != devnum)
119 /* Find out the parent device uclass */
120 if (device_get_uclass_id(dev->parent) != uclass_id) {
121 debug("%s: parent uclass %d, this dev %d\n", __func__,
122 device_get_uclass_id(dev->parent), uclass_id);
126 if (device_probe(dev))
129 debug("%s: Device desc %p\n", __func__, desc);
132 debug("%s: No device found\n", __func__);
138 * blk_get_by_device() - Get the block device descriptor for the given device
139 * @dev: Instance of a storage device
141 * Return: With block device descriptor on success , NULL if there is no such
144 struct blk_desc *blk_get_by_device(struct udevice *dev)
146 struct udevice *child_dev;
148 device_foreach_child(child_dev, dev) {
149 if (device_get_uclass_id(child_dev) != UCLASS_BLK)
152 return dev_get_uclass_platdata(child_dev);
155 debug("%s: No block device found\n", __func__);
161 * get_desc() - Get the block device descriptor for the given device number
163 * @if_type: Interface type
164 * @devnum: Device number (0 = first)
165 * @descp: Returns block device descriptor on success
166 * @return 0 on success, -ENODEV if there is no such device and no device
167 * with a higher device number, -ENOENT if there is no such device but there
168 * is one with a higher number, or other -ve on other error.
170 static int get_desc(enum if_type if_type, int devnum, struct blk_desc **descp)
172 bool found_more = false;
178 ret = uclass_get(UCLASS_BLK, &uc);
181 uclass_foreach_dev(dev, uc) {
182 struct blk_desc *desc = dev_get_uclass_platdata(dev);
184 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
185 if_type, devnum, dev->name, desc->if_type, desc->devnum);
186 if (desc->if_type == if_type) {
187 if (desc->devnum == devnum) {
188 ret = device_probe(dev);
194 } else if (desc->devnum > devnum) {
200 return found_more ? -ENOENT : -ENODEV;
203 int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart)
208 ret = blk_get_device(if_type, devnum, &dev);
212 return blk_select_hwpart(dev, hwpart);
215 int blk_list_part(enum if_type if_type)
217 struct blk_desc *desc;
221 for (ok = 0, devnum = 0;; ++devnum) {
222 ret = get_desc(if_type, devnum, &desc);
227 if (desc->part_type != PART_TYPE_UNKNOWN) {
240 int blk_print_part_devnum(enum if_type if_type, int devnum)
242 struct blk_desc *desc;
245 ret = get_desc(if_type, devnum, &desc);
248 if (desc->type == DEV_TYPE_UNKNOWN)
255 void blk_list_devices(enum if_type if_type)
257 struct blk_desc *desc;
262 ret = get_desc(if_type, i, &desc);
267 if (desc->type == DEV_TYPE_UNKNOWN)
268 continue; /* list only known devices */
269 printf("Device %d: ", i);
274 int blk_print_device_num(enum if_type if_type, int devnum)
276 struct blk_desc *desc;
279 ret = get_desc(if_type, devnum, &desc);
282 printf("\nIDE device %d: ", devnum);
288 int blk_show_device(enum if_type if_type, int devnum)
290 struct blk_desc *desc;
293 printf("\nDevice %d: ", devnum);
294 ret = get_desc(if_type, devnum, &desc);
295 if (ret == -ENODEV || ret == -ENOENT) {
296 printf("unknown device\n");
303 if (desc->type == DEV_TYPE_UNKNOWN)
309 ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
310 lbaint_t blkcnt, void *buffer)
312 struct blk_desc *desc;
316 ret = get_desc(if_type, devnum, &desc);
319 n = blk_dread(desc, start, blkcnt, buffer);
326 ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
327 lbaint_t blkcnt, const void *buffer)
329 struct blk_desc *desc;
332 ret = get_desc(if_type, devnum, &desc);
335 return blk_dwrite(desc, start, blkcnt, buffer);
338 int blk_select_hwpart(struct udevice *dev, int hwpart)
340 const struct blk_ops *ops = blk_get_ops(dev);
344 if (!ops->select_hwpart)
347 return ops->select_hwpart(dev, hwpart);
350 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart)
352 return blk_select_hwpart(desc->bdev, hwpart);
355 int blk_first_device(int if_type, struct udevice **devp)
357 struct blk_desc *desc;
360 ret = uclass_find_first_device(UCLASS_BLK, devp);
366 desc = dev_get_uclass_platdata(*devp);
367 if (desc->if_type == if_type)
369 ret = uclass_find_next_device(devp);
377 int blk_next_device(struct udevice **devp)
379 struct blk_desc *desc;
382 desc = dev_get_uclass_platdata(*devp);
383 if_type = desc->if_type;
385 ret = uclass_find_next_device(devp);
390 desc = dev_get_uclass_platdata(*devp);
391 if (desc->if_type == if_type)
396 int blk_find_device(int if_type, int devnum, struct udevice **devp)
402 ret = uclass_get(UCLASS_BLK, &uc);
405 uclass_foreach_dev(dev, uc) {
406 struct blk_desc *desc = dev_get_uclass_platdata(dev);
408 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
409 if_type, devnum, dev->name, desc->if_type, desc->devnum);
410 if (desc->if_type == if_type && desc->devnum == devnum) {
419 int blk_get_device(int if_type, int devnum, struct udevice **devp)
423 ret = blk_find_device(if_type, devnum, devp);
427 return device_probe(*devp);
430 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
431 lbaint_t blkcnt, void *buffer)
433 struct udevice *dev = block_dev->bdev;
434 const struct blk_ops *ops = blk_get_ops(dev);
440 if (blkcache_read(block_dev->if_type, block_dev->devnum,
441 start, blkcnt, block_dev->blksz, buffer))
443 blks_read = ops->read(dev, start, blkcnt, buffer);
444 if (blks_read == blkcnt)
445 blkcache_fill(block_dev->if_type, block_dev->devnum,
446 start, blkcnt, block_dev->blksz, buffer);
451 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
452 lbaint_t blkcnt, const void *buffer)
454 struct udevice *dev = block_dev->bdev;
455 const struct blk_ops *ops = blk_get_ops(dev);
460 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
461 return ops->write(dev, start, blkcnt, buffer);
464 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
467 struct udevice *dev = block_dev->bdev;
468 const struct blk_ops *ops = blk_get_ops(dev);
473 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
474 return ops->erase(dev, start, blkcnt);
477 int blk_get_from_parent(struct udevice *parent, struct udevice **devp)
483 device_find_first_child(parent, &dev);
485 debug("%s: No block device found for parent '%s'\n", __func__,
489 id = device_get_uclass_id(dev);
490 if (id != UCLASS_BLK) {
491 debug("%s: Incorrect uclass %s for block device '%s'\n",
492 __func__, uclass_get_name(id), dev->name);
495 ret = device_probe(dev);
503 int blk_find_max_devnum(enum if_type if_type)
506 int max_devnum = -ENODEV;
510 ret = uclass_get(UCLASS_BLK, &uc);
513 uclass_foreach_dev(dev, uc) {
514 struct blk_desc *desc = dev_get_uclass_platdata(dev);
516 if (desc->if_type == if_type && desc->devnum > max_devnum)
517 max_devnum = desc->devnum;
523 int blk_next_free_devnum(enum if_type if_type)
527 ret = blk_find_max_devnum(if_type);
536 static int blk_claim_devnum(enum if_type if_type, int devnum)
542 ret = uclass_get(UCLASS_BLK, &uc);
545 uclass_foreach_dev(dev, uc) {
546 struct blk_desc *desc = dev_get_uclass_platdata(dev);
548 if (desc->if_type == if_type && desc->devnum == devnum) {
549 int next = blk_next_free_devnum(if_type);
561 int blk_create_device(struct udevice *parent, const char *drv_name,
562 const char *name, int if_type, int devnum, int blksz,
563 lbaint_t lba, struct udevice **devp)
565 struct blk_desc *desc;
570 devnum = blk_next_free_devnum(if_type);
572 ret = blk_claim_devnum(if_type, devnum);
573 if (ret < 0 && ret != -ENOENT)
578 ret = device_bind_driver(parent, drv_name, name, &dev);
581 desc = dev_get_uclass_platdata(dev);
582 desc->if_type = if_type;
584 desc->log2blksz = LOG2(desc->blksz);
586 desc->part_type = PART_TYPE_UNKNOWN;
588 desc->devnum = devnum;
594 int blk_create_devicef(struct udevice *parent, const char *drv_name,
595 const char *name, int if_type, int devnum, int blksz,
596 lbaint_t lba, struct udevice **devp)
598 char dev_name[30], *str;
601 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
602 str = strdup(dev_name);
606 ret = blk_create_device(parent, drv_name, str, if_type, devnum,
612 device_set_name_alloced(*devp);
617 int blk_unbind_all(int if_type)
620 struct udevice *dev, *next;
623 ret = uclass_get(UCLASS_BLK, &uc);
626 uclass_foreach_dev_safe(dev, next, uc) {
627 struct blk_desc *desc = dev_get_uclass_platdata(dev);
629 if (desc->if_type == if_type) {
630 ret = device_remove(dev, DM_REMOVE_NORMAL);
633 ret = device_unbind(dev);
642 static int blk_post_probe(struct udevice *dev)
644 #if defined(CONFIG_PARTITIONS) && defined(CONFIG_HAVE_BLOCK_DEVICE)
645 struct blk_desc *desc = dev_get_uclass_platdata(dev);
653 UCLASS_DRIVER(blk) = {
656 .post_probe = blk_post_probe,
657 .per_device_platdata_auto_alloc_size = sizeof(struct blk_desc),