1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
12 #include <dm/device-internal.h>
14 #include <dm/uclass-internal.h>
15 #include <linux/err.h>
17 static const char *if_typename_str[IF_TYPE_COUNT] = {
18 [IF_TYPE_IDE] = "ide",
19 [IF_TYPE_SCSI] = "scsi",
20 [IF_TYPE_ATAPI] = "atapi",
21 [IF_TYPE_USB] = "usb",
22 [IF_TYPE_DOC] = "doc",
23 [IF_TYPE_MMC] = "mmc",
25 [IF_TYPE_SATA] = "sata",
26 [IF_TYPE_HOST] = "host",
27 [IF_TYPE_NVME] = "nvme",
28 [IF_TYPE_EFI] = "efi",
29 [IF_TYPE_VIRTIO] = "virtio",
32 static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
33 [IF_TYPE_IDE] = UCLASS_IDE,
34 [IF_TYPE_SCSI] = UCLASS_SCSI,
35 [IF_TYPE_ATAPI] = UCLASS_INVALID,
36 [IF_TYPE_USB] = UCLASS_MASS_STORAGE,
37 [IF_TYPE_DOC] = UCLASS_INVALID,
38 [IF_TYPE_MMC] = UCLASS_MMC,
39 [IF_TYPE_SD] = UCLASS_INVALID,
40 [IF_TYPE_SATA] = UCLASS_AHCI,
41 [IF_TYPE_HOST] = UCLASS_ROOT,
42 [IF_TYPE_NVME] = UCLASS_NVME,
43 [IF_TYPE_EFI] = UCLASS_EFI,
44 [IF_TYPE_VIRTIO] = UCLASS_VIRTIO,
47 static enum if_type if_typename_to_iftype(const char *if_typename)
51 for (i = 0; i < IF_TYPE_COUNT; i++) {
52 if (if_typename_str[i] &&
53 !strcmp(if_typename, if_typename_str[i]))
57 return IF_TYPE_UNKNOWN;
60 static enum uclass_id if_type_to_uclass_id(enum if_type if_type)
62 return if_type_uclass_id[if_type];
65 const char *blk_get_if_type_name(enum if_type if_type)
67 return if_typename_str[if_type];
70 struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum)
72 struct blk_desc *desc;
76 ret = blk_get_device(if_type, devnum, &dev);
79 desc = dev_get_uclass_platdata(dev);
85 * This function is complicated with driver model. We look up the interface
86 * name in a local table. This gives us an interface type which we can match
87 * against the uclass of the block device's parent.
89 struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum)
91 enum uclass_id uclass_id;
97 if_type = if_typename_to_iftype(if_typename);
98 if (if_type == IF_TYPE_UNKNOWN) {
99 debug("%s: Unknown interface type '%s'\n", __func__,
103 uclass_id = if_type_to_uclass_id(if_type);
104 if (uclass_id == UCLASS_INVALID) {
105 debug("%s: Unknown uclass for interface type'\n",
106 if_typename_str[if_type]);
110 ret = uclass_get(UCLASS_BLK, &uc);
113 uclass_foreach_dev(dev, uc) {
114 struct blk_desc *desc = dev_get_uclass_platdata(dev);
116 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
117 if_type, devnum, dev->name, desc->if_type, desc->devnum);
118 if (desc->devnum != devnum)
121 /* Find out the parent device uclass */
122 if (device_get_uclass_id(dev->parent) != uclass_id) {
123 debug("%s: parent uclass %d, this dev %d\n", __func__,
124 device_get_uclass_id(dev->parent), uclass_id);
128 if (device_probe(dev))
131 debug("%s: Device desc %p\n", __func__, desc);
134 debug("%s: No device found\n", __func__);
140 * blk_get_by_device() - Get the block device descriptor for the given device
141 * @dev: Instance of a storage device
143 * Return: With block device descriptor on success , NULL if there is no such
146 struct blk_desc *blk_get_by_device(struct udevice *dev)
148 struct udevice *child_dev;
150 device_foreach_child(child_dev, dev) {
151 if (device_get_uclass_id(child_dev) != UCLASS_BLK)
154 return dev_get_uclass_platdata(child_dev);
157 debug("%s: No block device found\n", __func__);
163 * get_desc() - Get the block device descriptor for the given device number
165 * @if_type: Interface type
166 * @devnum: Device number (0 = first)
167 * @descp: Returns block device descriptor on success
168 * @return 0 on success, -ENODEV if there is no such device and no device
169 * with a higher device number, -ENOENT if there is no such device but there
170 * is one with a higher number, or other -ve on other error.
172 static int get_desc(enum if_type if_type, int devnum, struct blk_desc **descp)
174 bool found_more = false;
180 ret = uclass_get(UCLASS_BLK, &uc);
183 uclass_foreach_dev(dev, uc) {
184 struct blk_desc *desc = dev_get_uclass_platdata(dev);
186 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
187 if_type, devnum, dev->name, desc->if_type, desc->devnum);
188 if (desc->if_type == if_type) {
189 if (desc->devnum == devnum) {
190 ret = device_probe(dev);
196 } else if (desc->devnum > devnum) {
202 return found_more ? -ENOENT : -ENODEV;
205 int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart)
210 ret = blk_get_device(if_type, devnum, &dev);
214 return blk_select_hwpart(dev, hwpart);
217 int blk_list_part(enum if_type if_type)
219 struct blk_desc *desc;
223 for (ok = 0, devnum = 0;; ++devnum) {
224 ret = get_desc(if_type, devnum, &desc);
229 if (desc->part_type != PART_TYPE_UNKNOWN) {
242 int blk_print_part_devnum(enum if_type if_type, int devnum)
244 struct blk_desc *desc;
247 ret = get_desc(if_type, devnum, &desc);
250 if (desc->type == DEV_TYPE_UNKNOWN)
257 void blk_list_devices(enum if_type if_type)
259 struct blk_desc *desc;
264 ret = get_desc(if_type, i, &desc);
269 if (desc->type == DEV_TYPE_UNKNOWN)
270 continue; /* list only known devices */
271 printf("Device %d: ", i);
276 int blk_print_device_num(enum if_type if_type, int devnum)
278 struct blk_desc *desc;
281 ret = get_desc(if_type, devnum, &desc);
284 printf("\nIDE device %d: ", devnum);
290 int blk_show_device(enum if_type if_type, int devnum)
292 struct blk_desc *desc;
295 printf("\nDevice %d: ", devnum);
296 ret = get_desc(if_type, devnum, &desc);
297 if (ret == -ENODEV || ret == -ENOENT) {
298 printf("unknown device\n");
305 if (desc->type == DEV_TYPE_UNKNOWN)
311 ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
312 lbaint_t blkcnt, void *buffer)
314 struct blk_desc *desc;
318 ret = get_desc(if_type, devnum, &desc);
321 n = blk_dread(desc, start, blkcnt, buffer);
328 ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
329 lbaint_t blkcnt, const void *buffer)
331 struct blk_desc *desc;
334 ret = get_desc(if_type, devnum, &desc);
337 return blk_dwrite(desc, start, blkcnt, buffer);
340 int blk_select_hwpart(struct udevice *dev, int hwpart)
342 const struct blk_ops *ops = blk_get_ops(dev);
346 if (!ops->select_hwpart)
349 return ops->select_hwpart(dev, hwpart);
352 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart)
354 return blk_select_hwpart(desc->bdev, hwpart);
357 int blk_first_device(int if_type, struct udevice **devp)
359 struct blk_desc *desc;
362 ret = uclass_find_first_device(UCLASS_BLK, devp);
368 desc = dev_get_uclass_platdata(*devp);
369 if (desc->if_type == if_type)
371 ret = uclass_find_next_device(devp);
379 int blk_next_device(struct udevice **devp)
381 struct blk_desc *desc;
384 desc = dev_get_uclass_platdata(*devp);
385 if_type = desc->if_type;
387 ret = uclass_find_next_device(devp);
392 desc = dev_get_uclass_platdata(*devp);
393 if (desc->if_type == if_type)
398 int blk_find_device(int if_type, int devnum, struct udevice **devp)
404 ret = uclass_get(UCLASS_BLK, &uc);
407 uclass_foreach_dev(dev, uc) {
408 struct blk_desc *desc = dev_get_uclass_platdata(dev);
410 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
411 if_type, devnum, dev->name, desc->if_type, desc->devnum);
412 if (desc->if_type == if_type && desc->devnum == devnum) {
421 int blk_get_device(int if_type, int devnum, struct udevice **devp)
425 ret = blk_find_device(if_type, devnum, devp);
429 return device_probe(*devp);
432 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
433 lbaint_t blkcnt, void *buffer)
435 struct udevice *dev = block_dev->bdev;
436 const struct blk_ops *ops = blk_get_ops(dev);
442 if (blkcache_read(block_dev->if_type, block_dev->devnum,
443 start, blkcnt, block_dev->blksz, buffer))
445 blks_read = ops->read(dev, start, blkcnt, buffer);
446 if (blks_read == blkcnt)
447 blkcache_fill(block_dev->if_type, block_dev->devnum,
448 start, blkcnt, block_dev->blksz, buffer);
453 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
454 lbaint_t blkcnt, const void *buffer)
456 struct udevice *dev = block_dev->bdev;
457 const struct blk_ops *ops = blk_get_ops(dev);
462 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
463 return ops->write(dev, start, blkcnt, buffer);
466 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
469 struct udevice *dev = block_dev->bdev;
470 const struct blk_ops *ops = blk_get_ops(dev);
475 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
476 return ops->erase(dev, start, blkcnt);
479 int blk_get_from_parent(struct udevice *parent, struct udevice **devp)
485 device_find_first_child(parent, &dev);
487 debug("%s: No block device found for parent '%s'\n", __func__,
491 id = device_get_uclass_id(dev);
492 if (id != UCLASS_BLK) {
493 debug("%s: Incorrect uclass %s for block device '%s'\n",
494 __func__, uclass_get_name(id), dev->name);
497 ret = device_probe(dev);
505 int blk_find_max_devnum(enum if_type if_type)
508 int max_devnum = -ENODEV;
512 ret = uclass_get(UCLASS_BLK, &uc);
515 uclass_foreach_dev(dev, uc) {
516 struct blk_desc *desc = dev_get_uclass_platdata(dev);
518 if (desc->if_type == if_type && desc->devnum > max_devnum)
519 max_devnum = desc->devnum;
525 int blk_next_free_devnum(enum if_type if_type)
529 ret = blk_find_max_devnum(if_type);
538 static int blk_claim_devnum(enum if_type if_type, int devnum)
544 ret = uclass_get(UCLASS_BLK, &uc);
547 uclass_foreach_dev(dev, uc) {
548 struct blk_desc *desc = dev_get_uclass_platdata(dev);
550 if (desc->if_type == if_type && desc->devnum == devnum) {
551 int next = blk_next_free_devnum(if_type);
563 int blk_create_device(struct udevice *parent, const char *drv_name,
564 const char *name, int if_type, int devnum, int blksz,
565 lbaint_t lba, struct udevice **devp)
567 struct blk_desc *desc;
572 devnum = blk_next_free_devnum(if_type);
574 ret = blk_claim_devnum(if_type, devnum);
575 if (ret < 0 && ret != -ENOENT)
580 ret = device_bind_driver(parent, drv_name, name, &dev);
583 desc = dev_get_uclass_platdata(dev);
584 desc->if_type = if_type;
586 desc->log2blksz = LOG2(desc->blksz);
588 desc->part_type = PART_TYPE_UNKNOWN;
590 desc->devnum = devnum;
596 int blk_create_devicef(struct udevice *parent, const char *drv_name,
597 const char *name, int if_type, int devnum, int blksz,
598 lbaint_t lba, struct udevice **devp)
600 char dev_name[30], *str;
603 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
604 str = strdup(dev_name);
608 ret = blk_create_device(parent, drv_name, str, if_type, devnum,
614 device_set_name_alloced(*devp);
619 int blk_unbind_all(int if_type)
622 struct udevice *dev, *next;
625 ret = uclass_get(UCLASS_BLK, &uc);
628 uclass_foreach_dev_safe(dev, next, uc) {
629 struct blk_desc *desc = dev_get_uclass_platdata(dev);
631 if (desc->if_type == if_type) {
632 ret = device_remove(dev, DM_REMOVE_NORMAL);
635 ret = device_unbind(dev);
644 static int blk_post_probe(struct udevice *dev)
646 #if defined(CONFIG_PARTITIONS) && defined(CONFIG_HAVE_BLOCK_DEVICE)
647 struct blk_desc *desc = dev_get_uclass_platdata(dev);
655 UCLASS_DRIVER(blk) = {
658 .post_probe = blk_post_probe,
659 .per_device_platdata_auto_alloc_size = sizeof(struct blk_desc),