2 * Copyright (C) 2016 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
11 #include <dm/device-internal.h>
13 #include <dm/uclass-internal.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",
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,
43 static enum if_type if_typename_to_iftype(const char *if_typename)
47 for (i = 0; i < IF_TYPE_COUNT; i++) {
48 if (if_typename_str[i] &&
49 !strcmp(if_typename, if_typename_str[i]))
53 return IF_TYPE_UNKNOWN;
56 static enum uclass_id if_type_to_uclass_id(enum if_type if_type)
58 return if_type_uclass_id[if_type];
61 const char *blk_get_if_type_name(enum if_type if_type)
63 return if_typename_str[if_type];
66 struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum)
68 struct blk_desc *desc;
72 ret = blk_get_device(if_type, devnum, &dev);
75 desc = dev_get_uclass_platdata(dev);
81 * This function is complicated with driver model. We look up the interface
82 * name in a local table. This gives us an interface type which we can match
83 * against the uclass of the block device's parent.
85 struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum)
87 enum uclass_id uclass_id;
93 if_type = if_typename_to_iftype(if_typename);
94 if (if_type == IF_TYPE_UNKNOWN) {
95 debug("%s: Unknown interface type '%s'\n", __func__,
99 uclass_id = if_type_to_uclass_id(if_type);
100 if (uclass_id == UCLASS_INVALID) {
101 debug("%s: Unknown uclass for interface type'\n",
102 if_typename_str[if_type]);
106 ret = uclass_get(UCLASS_BLK, &uc);
109 uclass_foreach_dev(dev, uc) {
110 struct blk_desc *desc = dev_get_uclass_platdata(dev);
112 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
113 if_type, devnum, dev->name, desc->if_type, desc->devnum);
114 if (desc->devnum != devnum)
117 /* Find out the parent device uclass */
118 if (device_get_uclass_id(dev->parent) != uclass_id) {
119 debug("%s: parent uclass %d, this dev %d\n", __func__,
120 device_get_uclass_id(dev->parent), uclass_id);
124 if (device_probe(dev))
127 debug("%s: Device desc %p\n", __func__, desc);
130 debug("%s: No device found\n", __func__);
136 * get_desc() - Get the block device descriptor for the given device number
138 * @if_type: Interface type
139 * @devnum: Device number (0 = first)
140 * @descp: Returns block device descriptor on success
141 * @return 0 on success, -ENODEV if there is no such device and no device
142 * with a higher device number, -ENOENT if there is no such device but there
143 * is one with a higher number, or other -ve on other error.
145 static int get_desc(enum if_type if_type, int devnum, struct blk_desc **descp)
147 bool found_more = false;
153 ret = uclass_get(UCLASS_BLK, &uc);
156 uclass_foreach_dev(dev, uc) {
157 struct blk_desc *desc = dev_get_uclass_platdata(dev);
159 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
160 if_type, devnum, dev->name, desc->if_type, desc->devnum);
161 if (desc->if_type == if_type) {
162 if (desc->devnum == devnum) {
163 ret = device_probe(dev);
169 } else if (desc->devnum > devnum) {
175 return found_more ? -ENOENT : -ENODEV;
178 int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart)
183 ret = blk_get_device(if_type, devnum, &dev);
187 return blk_select_hwpart(dev, hwpart);
190 int blk_list_part(enum if_type if_type)
192 struct blk_desc *desc;
196 for (ok = 0, devnum = 0;; ++devnum) {
197 ret = get_desc(if_type, devnum, &desc);
202 if (desc->part_type != PART_TYPE_UNKNOWN) {
215 int blk_print_part_devnum(enum if_type if_type, int devnum)
217 struct blk_desc *desc;
220 ret = get_desc(if_type, devnum, &desc);
223 if (desc->type == DEV_TYPE_UNKNOWN)
230 void blk_list_devices(enum if_type if_type)
232 struct blk_desc *desc;
237 ret = get_desc(if_type, i, &desc);
242 if (desc->type == DEV_TYPE_UNKNOWN)
243 continue; /* list only known devices */
244 printf("Device %d: ", i);
249 int blk_print_device_num(enum if_type if_type, int devnum)
251 struct blk_desc *desc;
254 ret = get_desc(if_type, devnum, &desc);
257 printf("\nIDE device %d: ", devnum);
263 int blk_show_device(enum if_type if_type, int devnum)
265 struct blk_desc *desc;
268 printf("\nDevice %d: ", devnum);
269 ret = get_desc(if_type, devnum, &desc);
270 if (ret == -ENODEV || ret == -ENOENT) {
271 printf("unknown device\n");
278 if (desc->type == DEV_TYPE_UNKNOWN)
284 ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
285 lbaint_t blkcnt, void *buffer)
287 struct blk_desc *desc;
291 ret = get_desc(if_type, devnum, &desc);
294 n = blk_dread(desc, start, blkcnt, buffer);
301 ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
302 lbaint_t blkcnt, const void *buffer)
304 struct blk_desc *desc;
307 ret = get_desc(if_type, devnum, &desc);
310 return blk_dwrite(desc, start, blkcnt, buffer);
313 int blk_select_hwpart(struct udevice *dev, int hwpart)
315 const struct blk_ops *ops = blk_get_ops(dev);
319 if (!ops->select_hwpart)
322 return ops->select_hwpart(dev, hwpart);
325 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart)
327 return blk_select_hwpart(desc->bdev, hwpart);
330 int blk_first_device(int if_type, struct udevice **devp)
332 struct blk_desc *desc;
335 ret = uclass_find_first_device(UCLASS_BLK, devp);
341 desc = dev_get_uclass_platdata(*devp);
342 if (desc->if_type == if_type)
344 ret = uclass_find_next_device(devp);
352 int blk_next_device(struct udevice **devp)
354 struct blk_desc *desc;
357 desc = dev_get_uclass_platdata(*devp);
358 if_type = desc->if_type;
360 ret = uclass_find_next_device(devp);
365 desc = dev_get_uclass_platdata(*devp);
366 if (desc->if_type == if_type)
371 int blk_find_device(int if_type, int devnum, struct udevice **devp)
377 ret = uclass_get(UCLASS_BLK, &uc);
380 uclass_foreach_dev(dev, uc) {
381 struct blk_desc *desc = dev_get_uclass_platdata(dev);
383 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
384 if_type, devnum, dev->name, desc->if_type, desc->devnum);
385 if (desc->if_type == if_type && desc->devnum == devnum) {
394 int blk_get_device(int if_type, int devnum, struct udevice **devp)
398 ret = blk_find_device(if_type, devnum, devp);
402 return device_probe(*devp);
405 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
406 lbaint_t blkcnt, void *buffer)
408 struct udevice *dev = block_dev->bdev;
409 const struct blk_ops *ops = blk_get_ops(dev);
415 if (blkcache_read(block_dev->if_type, block_dev->devnum,
416 start, blkcnt, block_dev->blksz, buffer))
418 blks_read = ops->read(dev, start, blkcnt, buffer);
419 if (blks_read == blkcnt)
420 blkcache_fill(block_dev->if_type, block_dev->devnum,
421 start, blkcnt, block_dev->blksz, buffer);
426 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
427 lbaint_t blkcnt, const void *buffer)
429 struct udevice *dev = block_dev->bdev;
430 const struct blk_ops *ops = blk_get_ops(dev);
435 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
436 return ops->write(dev, start, blkcnt, buffer);
439 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
442 struct udevice *dev = block_dev->bdev;
443 const struct blk_ops *ops = blk_get_ops(dev);
448 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
449 return ops->erase(dev, start, blkcnt);
452 int blk_prepare_device(struct udevice *dev)
454 struct blk_desc *desc = dev_get_uclass_platdata(dev);
461 int blk_get_from_parent(struct udevice *parent, struct udevice **devp)
467 device_find_first_child(parent, &dev);
469 debug("%s: No block device found for parent '%s'\n", __func__,
473 id = device_get_uclass_id(dev);
474 if (id != UCLASS_BLK) {
475 debug("%s: Incorrect uclass %s for block device '%s'\n",
476 __func__, uclass_get_name(id), dev->name);
479 ret = device_probe(dev);
487 int blk_find_max_devnum(enum if_type if_type)
490 int max_devnum = -ENODEV;
494 ret = uclass_get(UCLASS_BLK, &uc);
497 uclass_foreach_dev(dev, uc) {
498 struct blk_desc *desc = dev_get_uclass_platdata(dev);
500 if (desc->if_type == if_type && desc->devnum > max_devnum)
501 max_devnum = desc->devnum;
507 static int blk_next_free_devnum(enum if_type if_type)
511 ret = blk_find_max_devnum(if_type);
520 static int blk_claim_devnum(enum if_type if_type, int devnum)
526 ret = uclass_get(UCLASS_BLK, &uc);
529 uclass_foreach_dev(dev, uc) {
530 struct blk_desc *desc = dev_get_uclass_platdata(dev);
532 if (desc->if_type == if_type && desc->devnum == devnum) {
533 int next = blk_next_free_devnum(if_type);
545 int blk_create_device(struct udevice *parent, const char *drv_name,
546 const char *name, int if_type, int devnum, int blksz,
547 lbaint_t lba, struct udevice **devp)
549 struct blk_desc *desc;
554 devnum = blk_next_free_devnum(if_type);
556 ret = blk_claim_devnum(if_type, devnum);
557 if (ret < 0 && ret != -ENOENT)
562 ret = device_bind_driver(parent, drv_name, name, &dev);
565 desc = dev_get_uclass_platdata(dev);
566 desc->if_type = if_type;
569 desc->part_type = PART_TYPE_UNKNOWN;
571 desc->devnum = devnum;
577 int blk_create_devicef(struct udevice *parent, const char *drv_name,
578 const char *name, int if_type, int devnum, int blksz,
579 lbaint_t lba, struct udevice **devp)
581 char dev_name[30], *str;
584 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
585 str = strdup(dev_name);
589 ret = blk_create_device(parent, drv_name, str, if_type, devnum,
595 device_set_name_alloced(*devp);
600 int blk_unbind_all(int if_type)
603 struct udevice *dev, *next;
606 ret = uclass_get(UCLASS_BLK, &uc);
609 uclass_foreach_dev_safe(dev, next, uc) {
610 struct blk_desc *desc = dev_get_uclass_platdata(dev);
612 if (desc->if_type == if_type) {
613 ret = device_remove(dev, DM_REMOVE_NORMAL);
616 ret = device_unbind(dev);
625 UCLASS_DRIVER(blk) = {
628 .per_device_platdata_auto_alloc_size = sizeof(struct blk_desc),