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_SYSTEMACE
] = "ace",
26 [IF_TYPE_NVME
] = "nvme",
27 [IF_TYPE_EFI
] = "efi",
30 static enum uclass_id if_type_uclass_id
[IF_TYPE_COUNT
] = {
31 [IF_TYPE_IDE
] = UCLASS_IDE
,
32 [IF_TYPE_SCSI
] = UCLASS_SCSI
,
33 [IF_TYPE_ATAPI
] = UCLASS_INVALID
,
34 [IF_TYPE_USB
] = UCLASS_MASS_STORAGE
,
35 [IF_TYPE_DOC
] = UCLASS_INVALID
,
36 [IF_TYPE_MMC
] = UCLASS_MMC
,
37 [IF_TYPE_SD
] = UCLASS_INVALID
,
38 [IF_TYPE_SATA
] = UCLASS_AHCI
,
39 [IF_TYPE_HOST
] = UCLASS_ROOT
,
40 [IF_TYPE_SYSTEMACE
] = UCLASS_INVALID
,
41 [IF_TYPE_NVME
] = UCLASS_NVME
,
42 [IF_TYPE_EFI
] = UCLASS_EFI
,
45 static enum if_type
if_typename_to_iftype(const char *if_typename
)
49 for (i
= 0; i
< IF_TYPE_COUNT
; i
++) {
50 if (if_typename_str
[i
] &&
51 !strcmp(if_typename
, if_typename_str
[i
]))
55 return IF_TYPE_UNKNOWN
;
58 static enum uclass_id
if_type_to_uclass_id(enum if_type if_type
)
60 return if_type_uclass_id
[if_type
];
63 const char *blk_get_if_type_name(enum if_type if_type
)
65 return if_typename_str
[if_type
];
68 struct blk_desc
*blk_get_devnum_by_type(enum if_type if_type
, int devnum
)
70 struct blk_desc
*desc
;
74 ret
= blk_get_device(if_type
, devnum
, &dev
);
77 desc
= dev_get_uclass_platdata(dev
);
83 * This function is complicated with driver model. We look up the interface
84 * name in a local table. This gives us an interface type which we can match
85 * against the uclass of the block device's parent.
87 struct blk_desc
*blk_get_devnum_by_typename(const char *if_typename
, int devnum
)
89 enum uclass_id uclass_id
;
95 if_type
= if_typename_to_iftype(if_typename
);
96 if (if_type
== IF_TYPE_UNKNOWN
) {
97 debug("%s: Unknown interface type '%s'\n", __func__
,
101 uclass_id
= if_type_to_uclass_id(if_type
);
102 if (uclass_id
== UCLASS_INVALID
) {
103 debug("%s: Unknown uclass for interface type'\n",
104 if_typename_str
[if_type
]);
108 ret
= uclass_get(UCLASS_BLK
, &uc
);
111 uclass_foreach_dev(dev
, uc
) {
112 struct blk_desc
*desc
= dev_get_uclass_platdata(dev
);
114 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__
,
115 if_type
, devnum
, dev
->name
, desc
->if_type
, desc
->devnum
);
116 if (desc
->devnum
!= devnum
)
119 /* Find out the parent device uclass */
120 if (device_get_uclass_id(dev
->parent
) != uclass_id
) {
121 debug("%s: parent uclass %d, this dev %d\n", __func__
,
122 device_get_uclass_id(dev
->parent
), uclass_id
);
126 if (device_probe(dev
))
129 debug("%s: Device desc %p\n", __func__
, desc
);
132 debug("%s: No device found\n", __func__
);
138 * get_desc() - Get the block device descriptor for the given device number
140 * @if_type: Interface type
141 * @devnum: Device number (0 = first)
142 * @descp: Returns block device descriptor on success
143 * @return 0 on success, -ENODEV if there is no such device and no device
144 * with a higher device number, -ENOENT if there is no such device but there
145 * is one with a higher number, or other -ve on other error.
147 static int get_desc(enum if_type if_type
, int devnum
, struct blk_desc
**descp
)
149 bool found_more
= false;
155 ret
= uclass_get(UCLASS_BLK
, &uc
);
158 uclass_foreach_dev(dev
, uc
) {
159 struct blk_desc
*desc
= dev_get_uclass_platdata(dev
);
161 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__
,
162 if_type
, devnum
, dev
->name
, desc
->if_type
, desc
->devnum
);
163 if (desc
->if_type
== if_type
) {
164 if (desc
->devnum
== devnum
) {
165 ret
= device_probe(dev
);
171 } else if (desc
->devnum
> devnum
) {
177 return found_more
? -ENOENT
: -ENODEV
;
180 int blk_select_hwpart_devnum(enum if_type if_type
, int devnum
, int hwpart
)
185 ret
= blk_get_device(if_type
, devnum
, &dev
);
189 return blk_select_hwpart(dev
, hwpart
);
192 int blk_list_part(enum if_type if_type
)
194 struct blk_desc
*desc
;
198 for (ok
= 0, devnum
= 0;; ++devnum
) {
199 ret
= get_desc(if_type
, devnum
, &desc
);
204 if (desc
->part_type
!= PART_TYPE_UNKNOWN
) {
217 int blk_print_part_devnum(enum if_type if_type
, int devnum
)
219 struct blk_desc
*desc
;
222 ret
= get_desc(if_type
, devnum
, &desc
);
225 if (desc
->type
== DEV_TYPE_UNKNOWN
)
232 void blk_list_devices(enum if_type if_type
)
234 struct blk_desc
*desc
;
239 ret
= get_desc(if_type
, i
, &desc
);
244 if (desc
->type
== DEV_TYPE_UNKNOWN
)
245 continue; /* list only known devices */
246 printf("Device %d: ", i
);
251 int blk_print_device_num(enum if_type if_type
, int devnum
)
253 struct blk_desc
*desc
;
256 ret
= get_desc(if_type
, devnum
, &desc
);
259 printf("\nIDE device %d: ", devnum
);
265 int blk_show_device(enum if_type if_type
, int devnum
)
267 struct blk_desc
*desc
;
270 printf("\nDevice %d: ", devnum
);
271 ret
= get_desc(if_type
, devnum
, &desc
);
272 if (ret
== -ENODEV
|| ret
== -ENOENT
) {
273 printf("unknown device\n");
280 if (desc
->type
== DEV_TYPE_UNKNOWN
)
286 ulong
blk_read_devnum(enum if_type if_type
, int devnum
, lbaint_t start
,
287 lbaint_t blkcnt
, void *buffer
)
289 struct blk_desc
*desc
;
293 ret
= get_desc(if_type
, devnum
, &desc
);
296 n
= blk_dread(desc
, start
, blkcnt
, buffer
);
303 ulong
blk_write_devnum(enum if_type if_type
, int devnum
, lbaint_t start
,
304 lbaint_t blkcnt
, const void *buffer
)
306 struct blk_desc
*desc
;
309 ret
= get_desc(if_type
, devnum
, &desc
);
312 return blk_dwrite(desc
, start
, blkcnt
, buffer
);
315 int blk_select_hwpart(struct udevice
*dev
, int hwpart
)
317 const struct blk_ops
*ops
= blk_get_ops(dev
);
321 if (!ops
->select_hwpart
)
324 return ops
->select_hwpart(dev
, hwpart
);
327 int blk_dselect_hwpart(struct blk_desc
*desc
, int hwpart
)
329 return blk_select_hwpart(desc
->bdev
, hwpart
);
332 int blk_first_device(int if_type
, struct udevice
**devp
)
334 struct blk_desc
*desc
;
337 ret
= uclass_find_first_device(UCLASS_BLK
, devp
);
343 desc
= dev_get_uclass_platdata(*devp
);
344 if (desc
->if_type
== if_type
)
346 ret
= uclass_find_next_device(devp
);
354 int blk_next_device(struct udevice
**devp
)
356 struct blk_desc
*desc
;
359 desc
= dev_get_uclass_platdata(*devp
);
360 if_type
= desc
->if_type
;
362 ret
= uclass_find_next_device(devp
);
367 desc
= dev_get_uclass_platdata(*devp
);
368 if (desc
->if_type
== if_type
)
373 int blk_find_device(int if_type
, int devnum
, struct udevice
**devp
)
379 ret
= uclass_get(UCLASS_BLK
, &uc
);
382 uclass_foreach_dev(dev
, uc
) {
383 struct blk_desc
*desc
= dev_get_uclass_platdata(dev
);
385 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__
,
386 if_type
, devnum
, dev
->name
, desc
->if_type
, desc
->devnum
);
387 if (desc
->if_type
== if_type
&& desc
->devnum
== devnum
) {
396 int blk_get_device(int if_type
, int devnum
, struct udevice
**devp
)
400 ret
= blk_find_device(if_type
, devnum
, devp
);
404 return device_probe(*devp
);
407 unsigned long blk_dread(struct blk_desc
*block_dev
, lbaint_t start
,
408 lbaint_t blkcnt
, void *buffer
)
410 struct udevice
*dev
= block_dev
->bdev
;
411 const struct blk_ops
*ops
= blk_get_ops(dev
);
417 if (blkcache_read(block_dev
->if_type
, block_dev
->devnum
,
418 start
, blkcnt
, block_dev
->blksz
, buffer
))
420 blks_read
= ops
->read(dev
, start
, blkcnt
, buffer
);
421 if (blks_read
== blkcnt
)
422 blkcache_fill(block_dev
->if_type
, block_dev
->devnum
,
423 start
, blkcnt
, block_dev
->blksz
, buffer
);
428 unsigned long blk_dwrite(struct blk_desc
*block_dev
, lbaint_t start
,
429 lbaint_t blkcnt
, const void *buffer
)
431 struct udevice
*dev
= block_dev
->bdev
;
432 const struct blk_ops
*ops
= blk_get_ops(dev
);
437 blkcache_invalidate(block_dev
->if_type
, block_dev
->devnum
);
438 return ops
->write(dev
, start
, blkcnt
, buffer
);
441 unsigned long blk_derase(struct blk_desc
*block_dev
, lbaint_t start
,
444 struct udevice
*dev
= block_dev
->bdev
;
445 const struct blk_ops
*ops
= blk_get_ops(dev
);
450 blkcache_invalidate(block_dev
->if_type
, block_dev
->devnum
);
451 return ops
->erase(dev
, start
, blkcnt
);
454 int blk_prepare_device(struct udevice
*dev
)
456 struct blk_desc
*desc
= dev_get_uclass_platdata(dev
);
463 int blk_get_from_parent(struct udevice
*parent
, struct udevice
**devp
)
469 device_find_first_child(parent
, &dev
);
471 debug("%s: No block device found for parent '%s'\n", __func__
,
475 id
= device_get_uclass_id(dev
);
476 if (id
!= UCLASS_BLK
) {
477 debug("%s: Incorrect uclass %s for block device '%s'\n",
478 __func__
, uclass_get_name(id
), dev
->name
);
481 ret
= device_probe(dev
);
489 int blk_find_max_devnum(enum if_type if_type
)
492 int max_devnum
= -ENODEV
;
496 ret
= uclass_get(UCLASS_BLK
, &uc
);
499 uclass_foreach_dev(dev
, uc
) {
500 struct blk_desc
*desc
= dev_get_uclass_platdata(dev
);
502 if (desc
->if_type
== if_type
&& desc
->devnum
> max_devnum
)
503 max_devnum
= desc
->devnum
;
509 static int blk_next_free_devnum(enum if_type if_type
)
513 ret
= blk_find_max_devnum(if_type
);
522 static int blk_claim_devnum(enum if_type if_type
, int devnum
)
528 ret
= uclass_get(UCLASS_BLK
, &uc
);
531 uclass_foreach_dev(dev
, uc
) {
532 struct blk_desc
*desc
= dev_get_uclass_platdata(dev
);
534 if (desc
->if_type
== if_type
&& desc
->devnum
== devnum
) {
535 int next
= blk_next_free_devnum(if_type
);
547 int blk_create_device(struct udevice
*parent
, const char *drv_name
,
548 const char *name
, int if_type
, int devnum
, int blksz
,
549 lbaint_t lba
, struct udevice
**devp
)
551 struct blk_desc
*desc
;
556 devnum
= blk_next_free_devnum(if_type
);
558 ret
= blk_claim_devnum(if_type
, devnum
);
559 if (ret
< 0 && ret
!= -ENOENT
)
564 ret
= device_bind_driver(parent
, drv_name
, name
, &dev
);
567 desc
= dev_get_uclass_platdata(dev
);
568 desc
->if_type
= if_type
;
571 desc
->part_type
= PART_TYPE_UNKNOWN
;
573 desc
->devnum
= devnum
;
579 int blk_create_devicef(struct udevice
*parent
, const char *drv_name
,
580 const char *name
, int if_type
, int devnum
, int blksz
,
581 lbaint_t lba
, struct udevice
**devp
)
583 char dev_name
[30], *str
;
586 snprintf(dev_name
, sizeof(dev_name
), "%s.%s", parent
->name
, name
);
587 str
= strdup(dev_name
);
591 ret
= blk_create_device(parent
, drv_name
, str
, if_type
, devnum
,
597 device_set_name_alloced(*devp
);
602 int blk_unbind_all(int if_type
)
605 struct udevice
*dev
, *next
;
608 ret
= uclass_get(UCLASS_BLK
, &uc
);
611 uclass_foreach_dev_safe(dev
, next
, uc
) {
612 struct blk_desc
*desc
= dev_get_uclass_platdata(dev
);
614 if (desc
->if_type
== if_type
) {
615 ret
= device_remove(dev
, DM_REMOVE_NORMAL
);
618 ret
= device_unbind(dev
);
627 UCLASS_DRIVER(blk
) = {
630 .per_device_platdata_auto_alloc_size
= sizeof(struct blk_desc
),