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>
14 static const char *if_typename_str[IF_TYPE_COUNT] = {
15 [IF_TYPE_IDE] = "ide",
16 [IF_TYPE_SCSI] = "scsi",
17 [IF_TYPE_ATAPI] = "atapi",
18 [IF_TYPE_USB] = "usb",
19 [IF_TYPE_DOC] = "doc",
20 [IF_TYPE_MMC] = "mmc",
22 [IF_TYPE_SATA] = "sata",
23 [IF_TYPE_HOST] = "host",
24 [IF_TYPE_NVME] = "nvme",
25 [IF_TYPE_EFI] = "efi",
26 [IF_TYPE_VIRTIO] = "virtio",
29 static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
30 [IF_TYPE_IDE] = UCLASS_IDE,
31 [IF_TYPE_SCSI] = UCLASS_SCSI,
32 [IF_TYPE_ATAPI] = UCLASS_INVALID,
33 [IF_TYPE_USB] = UCLASS_MASS_STORAGE,
34 [IF_TYPE_DOC] = UCLASS_INVALID,
35 [IF_TYPE_MMC] = UCLASS_MMC,
36 [IF_TYPE_SD] = UCLASS_INVALID,
37 [IF_TYPE_SATA] = UCLASS_AHCI,
38 [IF_TYPE_HOST] = UCLASS_ROOT,
39 [IF_TYPE_NVME] = UCLASS_NVME,
40 [IF_TYPE_EFI] = UCLASS_EFI,
41 [IF_TYPE_VIRTIO] = UCLASS_VIRTIO,
44 static enum if_type if_typename_to_iftype(const char *if_typename)
48 for (i = 0; i < IF_TYPE_COUNT; i++) {
49 if (if_typename_str[i] &&
50 !strcmp(if_typename, if_typename_str[i]))
54 return IF_TYPE_UNKNOWN;
57 static enum uclass_id if_type_to_uclass_id(enum if_type if_type)
59 return if_type_uclass_id[if_type];
62 const char *blk_get_if_type_name(enum if_type if_type)
64 return if_typename_str[if_type];
67 struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum)
69 struct blk_desc *desc;
73 ret = blk_get_device(if_type, devnum, &dev);
76 desc = dev_get_uclass_platdata(dev);
82 * This function is complicated with driver model. We look up the interface
83 * name in a local table. This gives us an interface type which we can match
84 * against the uclass of the block device's parent.
86 struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum)
88 enum uclass_id uclass_id;
94 if_type = if_typename_to_iftype(if_typename);
95 if (if_type == IF_TYPE_UNKNOWN) {
96 debug("%s: Unknown interface type '%s'\n", __func__,
100 uclass_id = if_type_to_uclass_id(if_type);
101 if (uclass_id == UCLASS_INVALID) {
102 debug("%s: Unknown uclass for interface type'\n",
103 if_typename_str[if_type]);
107 ret = uclass_get(UCLASS_BLK, &uc);
110 uclass_foreach_dev(dev, uc) {
111 struct blk_desc *desc = dev_get_uclass_platdata(dev);
113 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
114 if_type, devnum, dev->name, desc->if_type, desc->devnum);
115 if (desc->devnum != devnum)
118 /* Find out the parent device uclass */
119 if (device_get_uclass_id(dev->parent) != uclass_id) {
120 debug("%s: parent uclass %d, this dev %d\n", __func__,
121 device_get_uclass_id(dev->parent), uclass_id);
125 if (device_probe(dev))
128 debug("%s: Device desc %p\n", __func__, desc);
131 debug("%s: No device found\n", __func__);
137 * blk_get_by_device() - Get the block device descriptor for the given device
138 * @dev: Instance of a storage device
140 * Return: With block device descriptor on success , NULL if there is no such
143 struct blk_desc *blk_get_by_device(struct udevice *dev)
145 struct udevice *child_dev, *next;
147 device_foreach_child_safe(child_dev, next, dev) {
148 if (device_get_uclass_id(child_dev) != UCLASS_BLK)
151 return dev_get_uclass_platdata(child_dev);
154 debug("%s: No block device found\n", __func__);
160 * get_desc() - Get the block device descriptor for the given device number
162 * @if_type: Interface type
163 * @devnum: Device number (0 = first)
164 * @descp: Returns block device descriptor on success
165 * @return 0 on success, -ENODEV if there is no such device and no device
166 * with a higher device number, -ENOENT if there is no such device but there
167 * is one with a higher number, or other -ve on other error.
169 static int get_desc(enum if_type if_type, int devnum, struct blk_desc **descp)
171 bool found_more = false;
177 ret = uclass_get(UCLASS_BLK, &uc);
180 uclass_foreach_dev(dev, uc) {
181 struct blk_desc *desc = dev_get_uclass_platdata(dev);
183 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
184 if_type, devnum, dev->name, desc->if_type, desc->devnum);
185 if (desc->if_type == if_type) {
186 if (desc->devnum == devnum) {
187 ret = device_probe(dev);
193 } else if (desc->devnum > devnum) {
199 return found_more ? -ENOENT : -ENODEV;
202 int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart)
207 ret = blk_get_device(if_type, devnum, &dev);
211 return blk_select_hwpart(dev, hwpart);
214 int blk_list_part(enum if_type if_type)
216 struct blk_desc *desc;
220 for (ok = 0, devnum = 0;; ++devnum) {
221 ret = get_desc(if_type, devnum, &desc);
226 if (desc->part_type != PART_TYPE_UNKNOWN) {
239 int blk_print_part_devnum(enum if_type if_type, int devnum)
241 struct blk_desc *desc;
244 ret = get_desc(if_type, devnum, &desc);
247 if (desc->type == DEV_TYPE_UNKNOWN)
254 void blk_list_devices(enum if_type if_type)
256 struct blk_desc *desc;
261 ret = get_desc(if_type, i, &desc);
266 if (desc->type == DEV_TYPE_UNKNOWN)
267 continue; /* list only known devices */
268 printf("Device %d: ", i);
273 int blk_print_device_num(enum if_type if_type, int devnum)
275 struct blk_desc *desc;
278 ret = get_desc(if_type, devnum, &desc);
281 printf("\nIDE device %d: ", devnum);
287 int blk_show_device(enum if_type if_type, int devnum)
289 struct blk_desc *desc;
292 printf("\nDevice %d: ", devnum);
293 ret = get_desc(if_type, devnum, &desc);
294 if (ret == -ENODEV || ret == -ENOENT) {
295 printf("unknown device\n");
302 if (desc->type == DEV_TYPE_UNKNOWN)
308 ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
309 lbaint_t blkcnt, void *buffer)
311 struct blk_desc *desc;
315 ret = get_desc(if_type, devnum, &desc);
318 n = blk_dread(desc, start, blkcnt, buffer);
325 ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
326 lbaint_t blkcnt, const void *buffer)
328 struct blk_desc *desc;
331 ret = get_desc(if_type, devnum, &desc);
334 return blk_dwrite(desc, start, blkcnt, buffer);
337 int blk_select_hwpart(struct udevice *dev, int hwpart)
339 const struct blk_ops *ops = blk_get_ops(dev);
343 if (!ops->select_hwpart)
346 return ops->select_hwpart(dev, hwpart);
349 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart)
351 return blk_select_hwpart(desc->bdev, hwpart);
354 int blk_first_device(int if_type, struct udevice **devp)
356 struct blk_desc *desc;
359 ret = uclass_find_first_device(UCLASS_BLK, devp);
365 desc = dev_get_uclass_platdata(*devp);
366 if (desc->if_type == if_type)
368 ret = uclass_find_next_device(devp);
376 int blk_next_device(struct udevice **devp)
378 struct blk_desc *desc;
381 desc = dev_get_uclass_platdata(*devp);
382 if_type = desc->if_type;
384 ret = uclass_find_next_device(devp);
389 desc = dev_get_uclass_platdata(*devp);
390 if (desc->if_type == if_type)
395 int blk_find_device(int if_type, int devnum, struct udevice **devp)
401 ret = uclass_get(UCLASS_BLK, &uc);
404 uclass_foreach_dev(dev, uc) {
405 struct blk_desc *desc = dev_get_uclass_platdata(dev);
407 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
408 if_type, devnum, dev->name, desc->if_type, desc->devnum);
409 if (desc->if_type == if_type && desc->devnum == devnum) {
418 int blk_get_device(int if_type, int devnum, struct udevice **devp)
422 ret = blk_find_device(if_type, devnum, devp);
426 return device_probe(*devp);
429 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
430 lbaint_t blkcnt, void *buffer)
432 struct udevice *dev = block_dev->bdev;
433 const struct blk_ops *ops = blk_get_ops(dev);
439 if (blkcache_read(block_dev->if_type, block_dev->devnum,
440 start, blkcnt, block_dev->blksz, buffer))
442 blks_read = ops->read(dev, start, blkcnt, buffer);
443 if (blks_read == blkcnt)
444 blkcache_fill(block_dev->if_type, block_dev->devnum,
445 start, blkcnt, block_dev->blksz, buffer);
450 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
451 lbaint_t blkcnt, const void *buffer)
453 struct udevice *dev = block_dev->bdev;
454 const struct blk_ops *ops = blk_get_ops(dev);
459 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
460 return ops->write(dev, start, blkcnt, buffer);
463 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
466 struct udevice *dev = block_dev->bdev;
467 const struct blk_ops *ops = blk_get_ops(dev);
472 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
473 return ops->erase(dev, start, blkcnt);
476 int blk_get_from_parent(struct udevice *parent, struct udevice **devp)
482 device_find_first_child(parent, &dev);
484 debug("%s: No block device found for parent '%s'\n", __func__,
488 id = device_get_uclass_id(dev);
489 if (id != UCLASS_BLK) {
490 debug("%s: Incorrect uclass %s for block device '%s'\n",
491 __func__, uclass_get_name(id), dev->name);
494 ret = device_probe(dev);
502 int blk_find_max_devnum(enum if_type if_type)
505 int max_devnum = -ENODEV;
509 ret = uclass_get(UCLASS_BLK, &uc);
512 uclass_foreach_dev(dev, uc) {
513 struct blk_desc *desc = dev_get_uclass_platdata(dev);
515 if (desc->if_type == if_type && desc->devnum > max_devnum)
516 max_devnum = desc->devnum;
522 int blk_next_free_devnum(enum if_type if_type)
526 ret = blk_find_max_devnum(if_type);
535 static int blk_claim_devnum(enum if_type if_type, int devnum)
541 ret = uclass_get(UCLASS_BLK, &uc);
544 uclass_foreach_dev(dev, uc) {
545 struct blk_desc *desc = dev_get_uclass_platdata(dev);
547 if (desc->if_type == if_type && desc->devnum == devnum) {
548 int next = blk_next_free_devnum(if_type);
560 int blk_create_device(struct udevice *parent, const char *drv_name,
561 const char *name, int if_type, int devnum, int blksz,
562 lbaint_t lba, struct udevice **devp)
564 struct blk_desc *desc;
569 devnum = blk_next_free_devnum(if_type);
571 ret = blk_claim_devnum(if_type, devnum);
572 if (ret < 0 && ret != -ENOENT)
577 ret = device_bind_driver(parent, drv_name, name, &dev);
580 desc = dev_get_uclass_platdata(dev);
581 desc->if_type = if_type;
584 desc->part_type = PART_TYPE_UNKNOWN;
586 desc->devnum = devnum;
592 int blk_create_devicef(struct udevice *parent, const char *drv_name,
593 const char *name, int if_type, int devnum, int blksz,
594 lbaint_t lba, struct udevice **devp)
596 char dev_name[30], *str;
599 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
600 str = strdup(dev_name);
604 ret = blk_create_device(parent, drv_name, str, if_type, devnum,
610 device_set_name_alloced(*devp);
615 int blk_unbind_all(int if_type)
618 struct udevice *dev, *next;
621 ret = uclass_get(UCLASS_BLK, &uc);
624 uclass_foreach_dev_safe(dev, next, uc) {
625 struct blk_desc *desc = dev_get_uclass_platdata(dev);
627 if (desc->if_type == if_type) {
628 ret = device_remove(dev, DM_REMOVE_NORMAL);
631 ret = device_unbind(dev);
640 static int blk_post_probe(struct udevice *dev)
642 #if defined(CONFIG_PARTITIONS) && defined(CONFIG_HAVE_BLOCK_DEVICE)
643 struct blk_desc *desc = dev_get_uclass_platdata(dev);
651 UCLASS_DRIVER(blk) = {
654 .post_probe = blk_post_probe,
655 .per_device_platdata_auto_alloc_size = sizeof(struct blk_desc),