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 * get_desc() - Get the block device descriptor for the given device number
137 * @if_type: Interface type
138 * @devnum: Device number (0 = first)
139 * @descp: Returns block device descriptor on success
140 * @return 0 on success, -ENODEV if there is no such device and no device
141 * with a higher device number, -ENOENT if there is no such device but there
142 * is one with a higher number, or other -ve on other error.
144 static int get_desc(enum if_type if_type, int devnum, struct blk_desc **descp)
146 bool found_more = false;
152 ret = uclass_get(UCLASS_BLK, &uc);
155 uclass_foreach_dev(dev, uc) {
156 struct blk_desc *desc = dev_get_uclass_platdata(dev);
158 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
159 if_type, devnum, dev->name, desc->if_type, desc->devnum);
160 if (desc->if_type == if_type) {
161 if (desc->devnum == devnum) {
162 ret = device_probe(dev);
168 } else if (desc->devnum > devnum) {
174 return found_more ? -ENOENT : -ENODEV;
177 int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart)
182 ret = blk_get_device(if_type, devnum, &dev);
186 return blk_select_hwpart(dev, hwpart);
189 int blk_list_part(enum if_type if_type)
191 struct blk_desc *desc;
195 for (ok = 0, devnum = 0;; ++devnum) {
196 ret = get_desc(if_type, devnum, &desc);
201 if (desc->part_type != PART_TYPE_UNKNOWN) {
214 int blk_print_part_devnum(enum if_type if_type, int devnum)
216 struct blk_desc *desc;
219 ret = get_desc(if_type, devnum, &desc);
222 if (desc->type == DEV_TYPE_UNKNOWN)
229 void blk_list_devices(enum if_type if_type)
231 struct blk_desc *desc;
236 ret = get_desc(if_type, i, &desc);
241 if (desc->type == DEV_TYPE_UNKNOWN)
242 continue; /* list only known devices */
243 printf("Device %d: ", i);
248 int blk_print_device_num(enum if_type if_type, int devnum)
250 struct blk_desc *desc;
253 ret = get_desc(if_type, devnum, &desc);
256 printf("\nIDE device %d: ", devnum);
262 int blk_show_device(enum if_type if_type, int devnum)
264 struct blk_desc *desc;
267 printf("\nDevice %d: ", devnum);
268 ret = get_desc(if_type, devnum, &desc);
269 if (ret == -ENODEV || ret == -ENOENT) {
270 printf("unknown device\n");
277 if (desc->type == DEV_TYPE_UNKNOWN)
283 ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
284 lbaint_t blkcnt, void *buffer)
286 struct blk_desc *desc;
290 ret = get_desc(if_type, devnum, &desc);
293 n = blk_dread(desc, start, blkcnt, buffer);
300 ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
301 lbaint_t blkcnt, const void *buffer)
303 struct blk_desc *desc;
306 ret = get_desc(if_type, devnum, &desc);
309 return blk_dwrite(desc, start, blkcnt, buffer);
312 int blk_select_hwpart(struct udevice *dev, int hwpart)
314 const struct blk_ops *ops = blk_get_ops(dev);
318 if (!ops->select_hwpart)
321 return ops->select_hwpart(dev, hwpart);
324 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart)
326 return blk_select_hwpart(desc->bdev, hwpart);
329 int blk_first_device(int if_type, struct udevice **devp)
331 struct blk_desc *desc;
334 ret = uclass_find_first_device(UCLASS_BLK, devp);
340 desc = dev_get_uclass_platdata(*devp);
341 if (desc->if_type == if_type)
343 ret = uclass_find_next_device(devp);
351 int blk_next_device(struct udevice **devp)
353 struct blk_desc *desc;
356 desc = dev_get_uclass_platdata(*devp);
357 if_type = desc->if_type;
359 ret = uclass_find_next_device(devp);
364 desc = dev_get_uclass_platdata(*devp);
365 if (desc->if_type == if_type)
370 int blk_find_device(int if_type, int devnum, struct udevice **devp)
376 ret = uclass_get(UCLASS_BLK, &uc);
379 uclass_foreach_dev(dev, uc) {
380 struct blk_desc *desc = dev_get_uclass_platdata(dev);
382 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
383 if_type, devnum, dev->name, desc->if_type, desc->devnum);
384 if (desc->if_type == if_type && desc->devnum == devnum) {
393 int blk_get_device(int if_type, int devnum, struct udevice **devp)
397 ret = blk_find_device(if_type, devnum, devp);
401 return device_probe(*devp);
404 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
405 lbaint_t blkcnt, void *buffer)
407 struct udevice *dev = block_dev->bdev;
408 const struct blk_ops *ops = blk_get_ops(dev);
414 if (blkcache_read(block_dev->if_type, block_dev->devnum,
415 start, blkcnt, block_dev->blksz, buffer))
417 blks_read = ops->read(dev, start, blkcnt, buffer);
418 if (blks_read == blkcnt)
419 blkcache_fill(block_dev->if_type, block_dev->devnum,
420 start, blkcnt, block_dev->blksz, buffer);
425 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
426 lbaint_t blkcnt, const void *buffer)
428 struct udevice *dev = block_dev->bdev;
429 const struct blk_ops *ops = blk_get_ops(dev);
434 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
435 return ops->write(dev, start, blkcnt, buffer);
438 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
441 struct udevice *dev = block_dev->bdev;
442 const struct blk_ops *ops = blk_get_ops(dev);
447 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
448 return ops->erase(dev, start, blkcnt);
451 int blk_prepare_device(struct udevice *dev)
453 struct blk_desc *desc = dev_get_uclass_platdata(dev);
460 int blk_get_from_parent(struct udevice *parent, struct udevice **devp)
466 device_find_first_child(parent, &dev);
468 debug("%s: No block device found for parent '%s'\n", __func__,
472 id = device_get_uclass_id(dev);
473 if (id != UCLASS_BLK) {
474 debug("%s: Incorrect uclass %s for block device '%s'\n",
475 __func__, uclass_get_name(id), dev->name);
478 ret = device_probe(dev);
486 int blk_find_max_devnum(enum if_type if_type)
489 int max_devnum = -ENODEV;
493 ret = uclass_get(UCLASS_BLK, &uc);
496 uclass_foreach_dev(dev, uc) {
497 struct blk_desc *desc = dev_get_uclass_platdata(dev);
499 if (desc->if_type == if_type && desc->devnum > max_devnum)
500 max_devnum = desc->devnum;
506 static int blk_next_free_devnum(enum if_type if_type)
510 ret = blk_find_max_devnum(if_type);
519 static int blk_claim_devnum(enum if_type if_type, int devnum)
525 ret = uclass_get(UCLASS_BLK, &uc);
528 uclass_foreach_dev(dev, uc) {
529 struct blk_desc *desc = dev_get_uclass_platdata(dev);
531 if (desc->if_type == if_type && desc->devnum == devnum) {
532 int next = blk_next_free_devnum(if_type);
544 int blk_create_device(struct udevice *parent, const char *drv_name,
545 const char *name, int if_type, int devnum, int blksz,
546 lbaint_t lba, struct udevice **devp)
548 struct blk_desc *desc;
553 devnum = blk_next_free_devnum(if_type);
555 ret = blk_claim_devnum(if_type, devnum);
556 if (ret < 0 && ret != -ENOENT)
561 ret = device_bind_driver(parent, drv_name, name, &dev);
564 desc = dev_get_uclass_platdata(dev);
565 desc->if_type = if_type;
568 desc->part_type = PART_TYPE_UNKNOWN;
570 desc->devnum = devnum;
576 int blk_create_devicef(struct udevice *parent, const char *drv_name,
577 const char *name, int if_type, int devnum, int blksz,
578 lbaint_t lba, struct udevice **devp)
580 char dev_name[30], *str;
583 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
584 str = strdup(dev_name);
588 ret = blk_create_device(parent, drv_name, str, if_type, devnum,
594 device_set_name_alloced(*devp);
599 int blk_unbind_all(int if_type)
602 struct udevice *dev, *next;
605 ret = uclass_get(UCLASS_BLK, &uc);
608 uclass_foreach_dev_safe(dev, next, uc) {
609 struct blk_desc *desc = dev_get_uclass_platdata(dev);
611 if (desc->if_type == if_type) {
612 ret = device_remove(dev, DM_REMOVE_NORMAL);
615 ret = device_unbind(dev);
624 UCLASS_DRIVER(blk) = {
627 .per_device_platdata_auto_alloc_size = sizeof(struct blk_desc),