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",
28 static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
29 [IF_TYPE_IDE] = UCLASS_IDE,
30 [IF_TYPE_SCSI] = UCLASS_SCSI,
31 [IF_TYPE_ATAPI] = UCLASS_INVALID,
32 [IF_TYPE_USB] = UCLASS_MASS_STORAGE,
33 [IF_TYPE_DOC] = UCLASS_INVALID,
34 [IF_TYPE_MMC] = UCLASS_MMC,
35 [IF_TYPE_SD] = UCLASS_INVALID,
36 [IF_TYPE_SATA] = UCLASS_AHCI,
37 [IF_TYPE_HOST] = UCLASS_ROOT,
38 [IF_TYPE_NVME] = UCLASS_NVME,
39 [IF_TYPE_EFI] = UCLASS_EFI,
42 static enum if_type if_typename_to_iftype(const char *if_typename)
46 for (i = 0; i < IF_TYPE_COUNT; i++) {
47 if (if_typename_str[i] &&
48 !strcmp(if_typename, if_typename_str[i]))
52 return IF_TYPE_UNKNOWN;
55 static enum uclass_id if_type_to_uclass_id(enum if_type if_type)
57 return if_type_uclass_id[if_type];
60 const char *blk_get_if_type_name(enum if_type if_type)
62 return if_typename_str[if_type];
65 struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum)
67 struct blk_desc *desc;
71 ret = blk_get_device(if_type, devnum, &dev);
74 desc = dev_get_uclass_platdata(dev);
80 * This function is complicated with driver model. We look up the interface
81 * name in a local table. This gives us an interface type which we can match
82 * against the uclass of the block device's parent.
84 struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum)
86 enum uclass_id uclass_id;
92 if_type = if_typename_to_iftype(if_typename);
93 if (if_type == IF_TYPE_UNKNOWN) {
94 debug("%s: Unknown interface type '%s'\n", __func__,
98 uclass_id = if_type_to_uclass_id(if_type);
99 if (uclass_id == UCLASS_INVALID) {
100 debug("%s: Unknown uclass for interface type'\n",
101 if_typename_str[if_type]);
105 ret = uclass_get(UCLASS_BLK, &uc);
108 uclass_foreach_dev(dev, uc) {
109 struct blk_desc *desc = dev_get_uclass_platdata(dev);
111 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
112 if_type, devnum, dev->name, desc->if_type, desc->devnum);
113 if (desc->devnum != devnum)
116 /* Find out the parent device uclass */
117 if (device_get_uclass_id(dev->parent) != uclass_id) {
118 debug("%s: parent uclass %d, this dev %d\n", __func__,
119 device_get_uclass_id(dev->parent), uclass_id);
123 if (device_probe(dev))
126 debug("%s: Device desc %p\n", __func__, desc);
129 debug("%s: No device found\n", __func__);
135 * blk_get_by_device() - Get the block device descriptor for the given device
136 * @dev: Instance of a storage device
138 * Return: With block device descriptor on success , NULL if there is no such
141 struct blk_desc *blk_get_by_device(struct udevice *dev)
143 struct udevice *child_dev, *next;
145 device_foreach_child_safe(child_dev, next, dev) {
146 if (device_get_uclass_id(child_dev) != UCLASS_BLK)
149 return dev_get_uclass_platdata(child_dev);
152 debug("%s: No block device found\n", __func__);
158 * get_desc() - Get the block device descriptor for the given device number
160 * @if_type: Interface type
161 * @devnum: Device number (0 = first)
162 * @descp: Returns block device descriptor on success
163 * @return 0 on success, -ENODEV if there is no such device and no device
164 * with a higher device number, -ENOENT if there is no such device but there
165 * is one with a higher number, or other -ve on other error.
167 static int get_desc(enum if_type if_type, int devnum, struct blk_desc **descp)
169 bool found_more = false;
175 ret = uclass_get(UCLASS_BLK, &uc);
178 uclass_foreach_dev(dev, uc) {
179 struct blk_desc *desc = dev_get_uclass_platdata(dev);
181 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
182 if_type, devnum, dev->name, desc->if_type, desc->devnum);
183 if (desc->if_type == if_type) {
184 if (desc->devnum == devnum) {
185 ret = device_probe(dev);
191 } else if (desc->devnum > devnum) {
197 return found_more ? -ENOENT : -ENODEV;
200 int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart)
205 ret = blk_get_device(if_type, devnum, &dev);
209 return blk_select_hwpart(dev, hwpart);
212 int blk_list_part(enum if_type if_type)
214 struct blk_desc *desc;
218 for (ok = 0, devnum = 0;; ++devnum) {
219 ret = get_desc(if_type, devnum, &desc);
224 if (desc->part_type != PART_TYPE_UNKNOWN) {
237 int blk_print_part_devnum(enum if_type if_type, int devnum)
239 struct blk_desc *desc;
242 ret = get_desc(if_type, devnum, &desc);
245 if (desc->type == DEV_TYPE_UNKNOWN)
252 void blk_list_devices(enum if_type if_type)
254 struct blk_desc *desc;
259 ret = get_desc(if_type, i, &desc);
264 if (desc->type == DEV_TYPE_UNKNOWN)
265 continue; /* list only known devices */
266 printf("Device %d: ", i);
271 int blk_print_device_num(enum if_type if_type, int devnum)
273 struct blk_desc *desc;
276 ret = get_desc(if_type, devnum, &desc);
279 printf("\nIDE device %d: ", devnum);
285 int blk_show_device(enum if_type if_type, int devnum)
287 struct blk_desc *desc;
290 printf("\nDevice %d: ", devnum);
291 ret = get_desc(if_type, devnum, &desc);
292 if (ret == -ENODEV || ret == -ENOENT) {
293 printf("unknown device\n");
300 if (desc->type == DEV_TYPE_UNKNOWN)
306 ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
307 lbaint_t blkcnt, void *buffer)
309 struct blk_desc *desc;
313 ret = get_desc(if_type, devnum, &desc);
316 n = blk_dread(desc, start, blkcnt, buffer);
323 ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
324 lbaint_t blkcnt, const void *buffer)
326 struct blk_desc *desc;
329 ret = get_desc(if_type, devnum, &desc);
332 return blk_dwrite(desc, start, blkcnt, buffer);
335 int blk_select_hwpart(struct udevice *dev, int hwpart)
337 const struct blk_ops *ops = blk_get_ops(dev);
341 if (!ops->select_hwpart)
344 return ops->select_hwpart(dev, hwpart);
347 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart)
349 return blk_select_hwpart(desc->bdev, hwpart);
352 int blk_first_device(int if_type, struct udevice **devp)
354 struct blk_desc *desc;
357 ret = uclass_find_first_device(UCLASS_BLK, devp);
363 desc = dev_get_uclass_platdata(*devp);
364 if (desc->if_type == if_type)
366 ret = uclass_find_next_device(devp);
374 int blk_next_device(struct udevice **devp)
376 struct blk_desc *desc;
379 desc = dev_get_uclass_platdata(*devp);
380 if_type = desc->if_type;
382 ret = uclass_find_next_device(devp);
387 desc = dev_get_uclass_platdata(*devp);
388 if (desc->if_type == if_type)
393 int blk_find_device(int if_type, int devnum, struct udevice **devp)
399 ret = uclass_get(UCLASS_BLK, &uc);
402 uclass_foreach_dev(dev, uc) {
403 struct blk_desc *desc = dev_get_uclass_platdata(dev);
405 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
406 if_type, devnum, dev->name, desc->if_type, desc->devnum);
407 if (desc->if_type == if_type && desc->devnum == devnum) {
416 int blk_get_device(int if_type, int devnum, struct udevice **devp)
420 ret = blk_find_device(if_type, devnum, devp);
424 return device_probe(*devp);
427 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
428 lbaint_t blkcnt, void *buffer)
430 struct udevice *dev = block_dev->bdev;
431 const struct blk_ops *ops = blk_get_ops(dev);
437 if (blkcache_read(block_dev->if_type, block_dev->devnum,
438 start, blkcnt, block_dev->blksz, buffer))
440 blks_read = ops->read(dev, start, blkcnt, buffer);
441 if (blks_read == blkcnt)
442 blkcache_fill(block_dev->if_type, block_dev->devnum,
443 start, blkcnt, block_dev->blksz, buffer);
448 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
449 lbaint_t blkcnt, const void *buffer)
451 struct udevice *dev = block_dev->bdev;
452 const struct blk_ops *ops = blk_get_ops(dev);
457 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
458 return ops->write(dev, start, blkcnt, buffer);
461 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
464 struct udevice *dev = block_dev->bdev;
465 const struct blk_ops *ops = blk_get_ops(dev);
470 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
471 return ops->erase(dev, start, blkcnt);
474 int blk_prepare_device(struct udevice *dev)
476 struct blk_desc *desc = dev_get_uclass_platdata(dev);
483 int blk_get_from_parent(struct udevice *parent, struct udevice **devp)
489 device_find_first_child(parent, &dev);
491 debug("%s: No block device found for parent '%s'\n", __func__,
495 id = device_get_uclass_id(dev);
496 if (id != UCLASS_BLK) {
497 debug("%s: Incorrect uclass %s for block device '%s'\n",
498 __func__, uclass_get_name(id), dev->name);
501 ret = device_probe(dev);
509 int blk_find_max_devnum(enum if_type if_type)
512 int max_devnum = -ENODEV;
516 ret = uclass_get(UCLASS_BLK, &uc);
519 uclass_foreach_dev(dev, uc) {
520 struct blk_desc *desc = dev_get_uclass_platdata(dev);
522 if (desc->if_type == if_type && desc->devnum > max_devnum)
523 max_devnum = desc->devnum;
529 static int blk_next_free_devnum(enum if_type if_type)
533 ret = blk_find_max_devnum(if_type);
542 static int blk_claim_devnum(enum if_type if_type, int devnum)
548 ret = uclass_get(UCLASS_BLK, &uc);
551 uclass_foreach_dev(dev, uc) {
552 struct blk_desc *desc = dev_get_uclass_platdata(dev);
554 if (desc->if_type == if_type && desc->devnum == devnum) {
555 int next = blk_next_free_devnum(if_type);
567 int blk_create_device(struct udevice *parent, const char *drv_name,
568 const char *name, int if_type, int devnum, int blksz,
569 lbaint_t lba, struct udevice **devp)
571 struct blk_desc *desc;
576 devnum = blk_next_free_devnum(if_type);
578 ret = blk_claim_devnum(if_type, devnum);
579 if (ret < 0 && ret != -ENOENT)
584 ret = device_bind_driver(parent, drv_name, name, &dev);
587 desc = dev_get_uclass_platdata(dev);
588 desc->if_type = if_type;
591 desc->part_type = PART_TYPE_UNKNOWN;
593 desc->devnum = devnum;
599 int blk_create_devicef(struct udevice *parent, const char *drv_name,
600 const char *name, int if_type, int devnum, int blksz,
601 lbaint_t lba, struct udevice **devp)
603 char dev_name[30], *str;
606 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
607 str = strdup(dev_name);
611 ret = blk_create_device(parent, drv_name, str, if_type, devnum,
617 device_set_name_alloced(*devp);
622 int blk_unbind_all(int if_type)
625 struct udevice *dev, *next;
628 ret = uclass_get(UCLASS_BLK, &uc);
631 uclass_foreach_dev_safe(dev, next, uc) {
632 struct blk_desc *desc = dev_get_uclass_platdata(dev);
634 if (desc->if_type == if_type) {
635 ret = device_remove(dev, DM_REMOVE_NORMAL);
638 ret = device_unbind(dev);
647 UCLASS_DRIVER(blk) = {
650 .per_device_platdata_auto_alloc_size = sizeof(struct blk_desc),