blob: 3738517d8d0880bc6cb2021a8183fe84f740599f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasscceee552016-02-29 15:25:55 -07002/*
3 * Copyright (C) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glasscceee552016-02-29 15:25:55 -07005 */
6
Patrick Delaunay81313352021-04-27 11:02:19 +02007#define LOG_CATEGORY UCLASS_BLK
8
Simon Glasscceee552016-02-29 15:25:55 -07009#include <common.h>
10#include <blk.h>
11#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <malloc.h>
Simon Glass655306c2020-05-10 11:39:58 -060014#include <part.h>
Simon Glasscceee552016-02-29 15:25:55 -070015#include <dm/device-internal.h>
16#include <dm/lists.h>
Stefan Roeseef58a902017-11-29 16:46:42 +010017#include <dm/uclass-internal.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070018#include <linux/err.h>
Simon Glasscceee552016-02-29 15:25:55 -070019
Simon Glass4131ad52016-05-01 11:36:08 -060020static const char *if_typename_str[IF_TYPE_COUNT] = {
21 [IF_TYPE_IDE] = "ide",
22 [IF_TYPE_SCSI] = "scsi",
Simon Glass4131ad52016-05-01 11:36:08 -060023 [IF_TYPE_USB] = "usb",
Simon Glass4131ad52016-05-01 11:36:08 -060024 [IF_TYPE_MMC] = "mmc",
Simon Glass4131ad52016-05-01 11:36:08 -060025 [IF_TYPE_SATA] = "sata",
26 [IF_TYPE_HOST] = "host",
Zhikang Zhang182fccd2017-08-03 02:30:56 -070027 [IF_TYPE_NVME] = "nvme",
Simon Glass507ab962021-12-04 08:56:31 -070028 [IF_TYPE_EFI_MEDIA] = "efi",
Simon Glass15c4d672021-12-04 08:56:30 -070029 [IF_TYPE_EFI_LOADER] = "efiloader",
Tuomas Tynkkynend4580062018-10-15 02:21:10 -070030 [IF_TYPE_VIRTIO] = "virtio",
Anastasiia Lukianenko4fec7f82020-08-06 12:42:55 +030031 [IF_TYPE_PVBLOCK] = "pvblock",
Simon Glass4131ad52016-05-01 11:36:08 -060032};
33
34static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
Bin Mengb650afa2017-09-10 05:12:51 -070035 [IF_TYPE_IDE] = UCLASS_IDE,
Michal Simekc886f352016-09-08 15:06:45 +020036 [IF_TYPE_SCSI] = UCLASS_SCSI,
Simon Glass4131ad52016-05-01 11:36:08 -060037 [IF_TYPE_USB] = UCLASS_MASS_STORAGE,
Simon Glass4131ad52016-05-01 11:36:08 -060038 [IF_TYPE_MMC] = UCLASS_MMC,
Simon Glass4131ad52016-05-01 11:36:08 -060039 [IF_TYPE_SATA] = UCLASS_AHCI,
40 [IF_TYPE_HOST] = UCLASS_ROOT,
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010041 [IF_TYPE_NVME] = UCLASS_NVME,
Simon Glass507ab962021-12-04 08:56:31 -070042 [IF_TYPE_EFI_MEDIA] = UCLASS_EFI_MEDIA,
Simon Glass15c4d672021-12-04 08:56:30 -070043 [IF_TYPE_EFI_LOADER] = UCLASS_EFI_LOADER,
Tuomas Tynkkynend4580062018-10-15 02:21:10 -070044 [IF_TYPE_VIRTIO] = UCLASS_VIRTIO,
Anastasiia Lukianenko4fec7f82020-08-06 12:42:55 +030045 [IF_TYPE_PVBLOCK] = UCLASS_PVBLOCK,
Simon Glass4131ad52016-05-01 11:36:08 -060046};
47
48static enum if_type if_typename_to_iftype(const char *if_typename)
49{
50 int i;
51
52 for (i = 0; i < IF_TYPE_COUNT; i++) {
53 if (if_typename_str[i] &&
54 !strcmp(if_typename, if_typename_str[i]))
55 return i;
56 }
57
58 return IF_TYPE_UNKNOWN;
59}
60
61static enum uclass_id if_type_to_uclass_id(enum if_type if_type)
62{
63 return if_type_uclass_id[if_type];
64}
65
Simon Glass85af5a42017-07-29 11:34:53 -060066const char *blk_get_if_type_name(enum if_type if_type)
67{
68 return if_typename_str[if_type];
69}
70
Simon Glass4131ad52016-05-01 11:36:08 -060071struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum)
72{
73 struct blk_desc *desc;
74 struct udevice *dev;
75 int ret;
76
77 ret = blk_get_device(if_type, devnum, &dev);
78 if (ret)
79 return NULL;
Simon Glass71fa5b42020-12-03 16:55:18 -070080 desc = dev_get_uclass_plat(dev);
Simon Glass4131ad52016-05-01 11:36:08 -060081
82 return desc;
83}
84
85/*
86 * This function is complicated with driver model. We look up the interface
87 * name in a local table. This gives us an interface type which we can match
88 * against the uclass of the block device's parent.
89 */
90struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum)
91{
92 enum uclass_id uclass_id;
Simon Glassb00361a2022-08-11 19:34:56 -060093 enum if_type type;
Simon Glass4131ad52016-05-01 11:36:08 -060094 struct udevice *dev;
95 struct uclass *uc;
96 int ret;
97
Simon Glassb00361a2022-08-11 19:34:56 -060098 type = if_typename_to_iftype(if_typename);
99 if (type == IF_TYPE_UNKNOWN) {
Simon Glass4131ad52016-05-01 11:36:08 -0600100 debug("%s: Unknown interface type '%s'\n", __func__,
101 if_typename);
102 return NULL;
103 }
Simon Glassb00361a2022-08-11 19:34:56 -0600104 uclass_id = if_type_to_uclass_id(type);
Simon Glass4131ad52016-05-01 11:36:08 -0600105 if (uclass_id == UCLASS_INVALID) {
106 debug("%s: Unknown uclass for interface type'\n",
Simon Glassb00361a2022-08-11 19:34:56 -0600107 if_typename_str[type]);
Simon Glass4131ad52016-05-01 11:36:08 -0600108 return NULL;
109 }
110
111 ret = uclass_get(UCLASS_BLK, &uc);
112 if (ret)
113 return NULL;
114 uclass_foreach_dev(dev, uc) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700115 struct blk_desc *desc = dev_get_uclass_plat(dev);
Simon Glass4131ad52016-05-01 11:36:08 -0600116
117 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
Simon Glassb00361a2022-08-11 19:34:56 -0600118 type, devnum, dev->name, desc->if_type, desc->devnum);
Simon Glass4131ad52016-05-01 11:36:08 -0600119 if (desc->devnum != devnum)
120 continue;
121
122 /* Find out the parent device uclass */
123 if (device_get_uclass_id(dev->parent) != uclass_id) {
124 debug("%s: parent uclass %d, this dev %d\n", __func__,
125 device_get_uclass_id(dev->parent), uclass_id);
126 continue;
127 }
128
129 if (device_probe(dev))
130 return NULL;
131
132 debug("%s: Device desc %p\n", __func__, desc);
133 return desc;
134 }
135 debug("%s: No device found\n", __func__);
136
137 return NULL;
138}
139
140/**
Tien Fong Chee10378522018-07-06 16:26:36 +0800141 * blk_get_by_device() - Get the block device descriptor for the given device
142 * @dev: Instance of a storage device
143 *
144 * Return: With block device descriptor on success , NULL if there is no such
145 * block device.
146 */
147struct blk_desc *blk_get_by_device(struct udevice *dev)
148{
Simon Glasscfd72932019-09-25 08:55:56 -0600149 struct udevice *child_dev;
Tien Fong Chee10378522018-07-06 16:26:36 +0800150
Simon Glasscfd72932019-09-25 08:55:56 -0600151 device_foreach_child(child_dev, dev) {
Tien Fong Chee10378522018-07-06 16:26:36 +0800152 if (device_get_uclass_id(child_dev) != UCLASS_BLK)
153 continue;
154
Simon Glass71fa5b42020-12-03 16:55:18 -0700155 return dev_get_uclass_plat(child_dev);
Tien Fong Chee10378522018-07-06 16:26:36 +0800156 }
157
158 debug("%s: No block device found\n", __func__);
159
160 return NULL;
161}
162
163/**
Simon Glass4131ad52016-05-01 11:36:08 -0600164 * get_desc() - Get the block device descriptor for the given device number
165 *
166 * @if_type: Interface type
167 * @devnum: Device number (0 = first)
168 * @descp: Returns block device descriptor on success
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100169 * Return: 0 on success, -ENODEV if there is no such device and no device
Simon Glass4131ad52016-05-01 11:36:08 -0600170 * with a higher device number, -ENOENT if there is no such device but there
171 * is one with a higher number, or other -ve on other error.
172 */
173static int get_desc(enum if_type if_type, int devnum, struct blk_desc **descp)
174{
175 bool found_more = false;
176 struct udevice *dev;
177 struct uclass *uc;
178 int ret;
179
180 *descp = NULL;
181 ret = uclass_get(UCLASS_BLK, &uc);
182 if (ret)
183 return ret;
184 uclass_foreach_dev(dev, uc) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700185 struct blk_desc *desc = dev_get_uclass_plat(dev);
Simon Glass4131ad52016-05-01 11:36:08 -0600186
187 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
188 if_type, devnum, dev->name, desc->if_type, desc->devnum);
189 if (desc->if_type == if_type) {
190 if (desc->devnum == devnum) {
191 ret = device_probe(dev);
192 if (ret)
193 return ret;
194
Michal Simekf438b6b2016-11-16 17:37:42 +0100195 *descp = desc;
196 return 0;
Simon Glass4131ad52016-05-01 11:36:08 -0600197 } else if (desc->devnum > devnum) {
198 found_more = true;
199 }
200 }
201 }
202
203 return found_more ? -ENOENT : -ENODEV;
204}
205
Simon Glass13c2c292016-05-01 13:52:30 -0600206int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart)
207{
208 struct udevice *dev;
209 int ret;
210
211 ret = blk_get_device(if_type, devnum, &dev);
212 if (ret)
213 return ret;
214
developerf50d8d12019-08-27 15:32:18 +0800215 return blk_select_hwpart(dev, hwpart);
Simon Glass13c2c292016-05-01 13:52:30 -0600216}
217
Simon Glass4131ad52016-05-01 11:36:08 -0600218int blk_list_part(enum if_type if_type)
219{
220 struct blk_desc *desc;
221 int devnum, ok;
222 int ret;
223
224 for (ok = 0, devnum = 0;; ++devnum) {
225 ret = get_desc(if_type, devnum, &desc);
226 if (ret == -ENODEV)
227 break;
228 else if (ret)
229 continue;
230 if (desc->part_type != PART_TYPE_UNKNOWN) {
231 ++ok;
232 if (devnum)
233 putc('\n');
234 part_print(desc);
235 }
236 }
237 if (!ok)
238 return -ENODEV;
239
240 return 0;
241}
242
243int blk_print_part_devnum(enum if_type if_type, int devnum)
244{
245 struct blk_desc *desc;
246 int ret;
247
248 ret = get_desc(if_type, devnum, &desc);
249 if (ret)
250 return ret;
251 if (desc->type == DEV_TYPE_UNKNOWN)
252 return -ENOENT;
253 part_print(desc);
254
255 return 0;
256}
257
258void blk_list_devices(enum if_type if_type)
259{
260 struct blk_desc *desc;
261 int ret;
262 int i;
263
264 for (i = 0;; ++i) {
265 ret = get_desc(if_type, i, &desc);
266 if (ret == -ENODEV)
267 break;
268 else if (ret)
269 continue;
270 if (desc->type == DEV_TYPE_UNKNOWN)
271 continue; /* list only known devices */
272 printf("Device %d: ", i);
273 dev_print(desc);
274 }
275}
276
277int blk_print_device_num(enum if_type if_type, int devnum)
278{
279 struct blk_desc *desc;
280 int ret;
281
282 ret = get_desc(if_type, devnum, &desc);
283 if (ret)
284 return ret;
285 printf("\nIDE device %d: ", devnum);
286 dev_print(desc);
287
288 return 0;
289}
290
291int blk_show_device(enum if_type if_type, int devnum)
292{
293 struct blk_desc *desc;
294 int ret;
295
296 printf("\nDevice %d: ", devnum);
297 ret = get_desc(if_type, devnum, &desc);
298 if (ret == -ENODEV || ret == -ENOENT) {
299 printf("unknown device\n");
300 return -ENODEV;
301 }
302 if (ret)
303 return ret;
304 dev_print(desc);
305
306 if (desc->type == DEV_TYPE_UNKNOWN)
307 return -ENOENT;
308
309 return 0;
310}
311
312ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
313 lbaint_t blkcnt, void *buffer)
314{
315 struct blk_desc *desc;
316 ulong n;
317 int ret;
318
319 ret = get_desc(if_type, devnum, &desc);
320 if (ret)
321 return ret;
322 n = blk_dread(desc, start, blkcnt, buffer);
323 if (IS_ERR_VALUE(n))
324 return n;
325
Simon Glass4131ad52016-05-01 11:36:08 -0600326 return n;
327}
328
329ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
330 lbaint_t blkcnt, const void *buffer)
331{
332 struct blk_desc *desc;
333 int ret;
334
335 ret = get_desc(if_type, devnum, &desc);
336 if (ret)
337 return ret;
338 return blk_dwrite(desc, start, blkcnt, buffer);
339}
340
Simon Glass13c2c292016-05-01 13:52:30 -0600341int blk_select_hwpart(struct udevice *dev, int hwpart)
342{
343 const struct blk_ops *ops = blk_get_ops(dev);
344
345 if (!ops)
346 return -ENOSYS;
347 if (!ops->select_hwpart)
348 return 0;
349
350 return ops->select_hwpart(dev, hwpart);
351}
352
353int blk_dselect_hwpart(struct blk_desc *desc, int hwpart)
354{
developerf50d8d12019-08-27 15:32:18 +0800355 return blk_select_hwpart(desc->bdev, hwpart);
Simon Glass13c2c292016-05-01 13:52:30 -0600356}
357
Simon Glasscceee552016-02-29 15:25:55 -0700358int blk_first_device(int if_type, struct udevice **devp)
359{
360 struct blk_desc *desc;
361 int ret;
362
Stefan Roeseef58a902017-11-29 16:46:42 +0100363 ret = uclass_find_first_device(UCLASS_BLK, devp);
Simon Glasscceee552016-02-29 15:25:55 -0700364 if (ret)
365 return ret;
366 if (!*devp)
367 return -ENODEV;
368 do {
Simon Glass71fa5b42020-12-03 16:55:18 -0700369 desc = dev_get_uclass_plat(*devp);
Simon Glasscceee552016-02-29 15:25:55 -0700370 if (desc->if_type == if_type)
371 return 0;
Stefan Roeseef58a902017-11-29 16:46:42 +0100372 ret = uclass_find_next_device(devp);
Simon Glasscceee552016-02-29 15:25:55 -0700373 if (ret)
374 return ret;
375 } while (*devp);
376
377 return -ENODEV;
378}
379
380int blk_next_device(struct udevice **devp)
381{
382 struct blk_desc *desc;
383 int ret, if_type;
384
Simon Glass71fa5b42020-12-03 16:55:18 -0700385 desc = dev_get_uclass_plat(*devp);
Simon Glasscceee552016-02-29 15:25:55 -0700386 if_type = desc->if_type;
387 do {
Stefan Roeseef58a902017-11-29 16:46:42 +0100388 ret = uclass_find_next_device(devp);
Simon Glasscceee552016-02-29 15:25:55 -0700389 if (ret)
390 return ret;
391 if (!*devp)
392 return -ENODEV;
Simon Glass71fa5b42020-12-03 16:55:18 -0700393 desc = dev_get_uclass_plat(*devp);
Simon Glasscceee552016-02-29 15:25:55 -0700394 if (desc->if_type == if_type)
395 return 0;
396 } while (1);
397}
398
Simon Glassd5d4c102017-04-23 20:02:05 -0600399int blk_find_device(int if_type, int devnum, struct udevice **devp)
Simon Glasscceee552016-02-29 15:25:55 -0700400{
401 struct uclass *uc;
402 struct udevice *dev;
403 int ret;
404
405 ret = uclass_get(UCLASS_BLK, &uc);
406 if (ret)
407 return ret;
408 uclass_foreach_dev(dev, uc) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700409 struct blk_desc *desc = dev_get_uclass_plat(dev);
Simon Glasscceee552016-02-29 15:25:55 -0700410
411 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
412 if_type, devnum, dev->name, desc->if_type, desc->devnum);
413 if (desc->if_type == if_type && desc->devnum == devnum) {
414 *devp = dev;
Simon Glassd5d4c102017-04-23 20:02:05 -0600415 return 0;
Simon Glasscceee552016-02-29 15:25:55 -0700416 }
417 }
418
419 return -ENODEV;
420}
421
Simon Glassd5d4c102017-04-23 20:02:05 -0600422int blk_get_device(int if_type, int devnum, struct udevice **devp)
423{
424 int ret;
425
426 ret = blk_find_device(if_type, devnum, devp);
427 if (ret)
428 return ret;
429
430 return device_probe(*devp);
431}
432
Simon Glasscceee552016-02-29 15:25:55 -0700433unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
434 lbaint_t blkcnt, void *buffer)
435{
436 struct udevice *dev = block_dev->bdev;
437 const struct blk_ops *ops = blk_get_ops(dev);
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700438 ulong blks_read;
Simon Glasscceee552016-02-29 15:25:55 -0700439
440 if (!ops->read)
441 return -ENOSYS;
442
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700443 if (blkcache_read(block_dev->if_type, block_dev->devnum,
444 start, blkcnt, block_dev->blksz, buffer))
445 return blkcnt;
446 blks_read = ops->read(dev, start, blkcnt, buffer);
447 if (blks_read == blkcnt)
448 blkcache_fill(block_dev->if_type, block_dev->devnum,
449 start, blkcnt, block_dev->blksz, buffer);
450
451 return blks_read;
Simon Glasscceee552016-02-29 15:25:55 -0700452}
453
454unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
455 lbaint_t blkcnt, const void *buffer)
456{
457 struct udevice *dev = block_dev->bdev;
458 const struct blk_ops *ops = blk_get_ops(dev);
459
460 if (!ops->write)
461 return -ENOSYS;
462
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700463 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
Simon Glasscceee552016-02-29 15:25:55 -0700464 return ops->write(dev, start, blkcnt, buffer);
465}
466
467unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
468 lbaint_t blkcnt)
469{
470 struct udevice *dev = block_dev->bdev;
471 const struct blk_ops *ops = blk_get_ops(dev);
472
473 if (!ops->erase)
474 return -ENOSYS;
475
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700476 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
Simon Glasscceee552016-02-29 15:25:55 -0700477 return ops->erase(dev, start, blkcnt);
478}
479
Simon Glass4f269132017-05-27 11:37:17 -0600480int blk_get_from_parent(struct udevice *parent, struct udevice **devp)
481{
482 struct udevice *dev;
483 enum uclass_id id;
484 int ret;
485
486 device_find_first_child(parent, &dev);
487 if (!dev) {
488 debug("%s: No block device found for parent '%s'\n", __func__,
489 parent->name);
490 return -ENODEV;
491 }
492 id = device_get_uclass_id(dev);
493 if (id != UCLASS_BLK) {
494 debug("%s: Incorrect uclass %s for block device '%s'\n",
495 __func__, uclass_get_name(id), dev->name);
496 return -ENOTBLK;
497 }
498 ret = device_probe(dev);
499 if (ret)
500 return ret;
501 *devp = dev;
502
503 return 0;
504}
505
Simon Glassf3086cf2022-04-24 23:31:03 -0600506const char *blk_get_devtype(struct udevice *dev)
507{
508 struct udevice *parent = dev_get_parent(dev);
509
510 return uclass_get_name(device_get_uclass_id(parent));
511};
512
Simon Glassd089ba32016-05-01 11:36:28 -0600513int blk_find_max_devnum(enum if_type if_type)
514{
515 struct udevice *dev;
516 int max_devnum = -ENODEV;
517 struct uclass *uc;
518 int ret;
519
520 ret = uclass_get(UCLASS_BLK, &uc);
521 if (ret)
522 return ret;
523 uclass_foreach_dev(dev, uc) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700524 struct blk_desc *desc = dev_get_uclass_plat(dev);
Simon Glassd089ba32016-05-01 11:36:28 -0600525
526 if (desc->if_type == if_type && desc->devnum > max_devnum)
527 max_devnum = desc->devnum;
528 }
529
530 return max_devnum;
531}
532
Bin Mengfd5eda72018-10-15 02:21:09 -0700533int blk_next_free_devnum(enum if_type if_type)
Simon Glassdbc38612017-04-23 20:02:06 -0600534{
535 int ret;
536
537 ret = blk_find_max_devnum(if_type);
538 if (ret == -ENODEV)
539 return 0;
540 if (ret < 0)
541 return ret;
542
543 return ret + 1;
544}
545
Simon Glassfc7a7442021-07-05 16:32:59 -0600546static int blk_flags_check(struct udevice *dev, enum blk_flag_t req_flags)
547{
548 const struct blk_desc *desc = dev_get_uclass_plat(dev);
549 enum blk_flag_t flags;
550
551 flags = desc->removable ? BLKF_REMOVABLE : BLKF_FIXED;
552
553 return flags & req_flags ? 0 : 1;
554}
555
Simon Glass8e61f932022-02-28 12:08:35 -0700556int blk_find_first(enum blk_flag_t flags, struct udevice **devp)
557{
558 int ret;
559
560 for (ret = uclass_find_first_device(UCLASS_BLK, devp);
561 *devp && !blk_flags_check(*devp, flags);
562 ret = uclass_find_next_device(devp))
563 return 0;
564
565 return -ENODEV;
566}
567
568int blk_find_next(enum blk_flag_t flags, struct udevice **devp)
569{
570 int ret;
571
572 for (ret = uclass_find_next_device(devp);
573 *devp && !blk_flags_check(*devp, flags);
574 ret = uclass_find_next_device(devp))
575 return 0;
576
577 return -ENODEV;
578}
579
Simon Glassfc7a7442021-07-05 16:32:59 -0600580int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp)
581{
582 int ret;
583
584 for (ret = uclass_first_device_err(UCLASS_BLK, devp);
585 !ret;
586 ret = uclass_next_device_err(devp)) {
587 if (!blk_flags_check(*devp, flags))
588 return 0;
589 }
590
591 return -ENODEV;
592}
593
594int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp)
595{
596 int ret;
597
598 for (ret = uclass_next_device_err(devp);
599 !ret;
600 ret = uclass_next_device_err(devp)) {
601 if (!blk_flags_check(*devp, flags))
602 return 0;
603 }
604
605 return -ENODEV;
606}
607
608int blk_count_devices(enum blk_flag_t flag)
609{
610 struct udevice *dev;
611 int count = 0;
612
613 blk_foreach_probe(flag, dev)
614 count++;
615
616 return count;
617}
618
Simon Glasse4fef742017-04-23 20:02:07 -0600619static int blk_claim_devnum(enum if_type if_type, int devnum)
620{
621 struct udevice *dev;
622 struct uclass *uc;
623 int ret;
624
625 ret = uclass_get(UCLASS_BLK, &uc);
626 if (ret)
627 return ret;
628 uclass_foreach_dev(dev, uc) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700629 struct blk_desc *desc = dev_get_uclass_plat(dev);
Simon Glasse4fef742017-04-23 20:02:07 -0600630
631 if (desc->if_type == if_type && desc->devnum == devnum) {
632 int next = blk_next_free_devnum(if_type);
633
634 if (next < 0)
635 return next;
636 desc->devnum = next;
637 return 0;
638 }
639 }
640
641 return -ENOENT;
642}
643
Simon Glasscceee552016-02-29 15:25:55 -0700644int blk_create_device(struct udevice *parent, const char *drv_name,
645 const char *name, int if_type, int devnum, int blksz,
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200646 lbaint_t lba, struct udevice **devp)
Simon Glasscceee552016-02-29 15:25:55 -0700647{
648 struct blk_desc *desc;
649 struct udevice *dev;
650 int ret;
651
Simon Glassd089ba32016-05-01 11:36:28 -0600652 if (devnum == -1) {
Simon Glasse4fef742017-04-23 20:02:07 -0600653 devnum = blk_next_free_devnum(if_type);
654 } else {
655 ret = blk_claim_devnum(if_type, devnum);
656 if (ret < 0 && ret != -ENOENT)
Simon Glassd089ba32016-05-01 11:36:28 -0600657 return ret;
Simon Glassd089ba32016-05-01 11:36:28 -0600658 }
Simon Glasse4fef742017-04-23 20:02:07 -0600659 if (devnum < 0)
660 return devnum;
Simon Glass77f7fb82016-05-01 13:52:22 -0600661 ret = device_bind_driver(parent, drv_name, name, &dev);
662 if (ret)
663 return ret;
Simon Glass71fa5b42020-12-03 16:55:18 -0700664 desc = dev_get_uclass_plat(dev);
Simon Glass77f7fb82016-05-01 13:52:22 -0600665 desc->if_type = if_type;
666 desc->blksz = blksz;
Heinrich Schuchardt09ba4ea2019-10-25 12:15:31 +0200667 desc->log2blksz = LOG2(desc->blksz);
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200668 desc->lba = lba;
Simon Glass77f7fb82016-05-01 13:52:22 -0600669 desc->part_type = PART_TYPE_UNKNOWN;
670 desc->bdev = dev;
Simon Glasscceee552016-02-29 15:25:55 -0700671 desc->devnum = devnum;
672 *devp = dev;
673
674 return 0;
675}
676
Simon Glass966b6952016-05-01 11:36:29 -0600677int blk_create_devicef(struct udevice *parent, const char *drv_name,
678 const char *name, int if_type, int devnum, int blksz,
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200679 lbaint_t lba, struct udevice **devp)
Simon Glass966b6952016-05-01 11:36:29 -0600680{
681 char dev_name[30], *str;
Simon Glass39e54922016-05-01 13:52:24 -0600682 int ret;
Simon Glass966b6952016-05-01 11:36:29 -0600683
684 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
685 str = strdup(dev_name);
686 if (!str)
687 return -ENOMEM;
688
Simon Glass39e54922016-05-01 13:52:24 -0600689 ret = blk_create_device(parent, drv_name, str, if_type, devnum,
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200690 blksz, lba, devp);
Simon Glass39e54922016-05-01 13:52:24 -0600691 if (ret) {
692 free(str);
693 return ret;
694 }
695 device_set_name_alloced(*devp);
696
Simon Glass44505972017-07-29 11:34:59 -0600697 return 0;
Simon Glass966b6952016-05-01 11:36:29 -0600698}
699
AKASHI Takahiro3e32dbe2021-12-10 15:49:29 +0900700int blk_probe_or_unbind(struct udevice *dev)
701{
702 int ret;
703
704 ret = device_probe(dev);
705 if (ret) {
706 log_debug("probing %s failed\n", dev->name);
707 device_unbind(dev);
708 }
709
710 return ret;
711}
712
Simon Glasscceee552016-02-29 15:25:55 -0700713int blk_unbind_all(int if_type)
714{
715 struct uclass *uc;
716 struct udevice *dev, *next;
717 int ret;
718
719 ret = uclass_get(UCLASS_BLK, &uc);
720 if (ret)
721 return ret;
722 uclass_foreach_dev_safe(dev, next, uc) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700723 struct blk_desc *desc = dev_get_uclass_plat(dev);
Simon Glasscceee552016-02-29 15:25:55 -0700724
725 if (desc->if_type == if_type) {
Stefan Roese80b5bc92017-03-20 12:51:48 +0100726 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glasscceee552016-02-29 15:25:55 -0700727 if (ret)
728 return ret;
729 ret = device_unbind(dev);
730 if (ret)
731 return ret;
732 }
733 }
734
735 return 0;
736}
737
Bin Mengcbc3da82018-10-15 02:21:07 -0700738static int blk_post_probe(struct udevice *dev)
739{
Simon Glassf5ac3032022-08-11 19:34:45 -0600740 if (CONFIG_IS_ENABLED(PARTITIONS) && blk_enabled()) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700741 struct blk_desc *desc = dev_get_uclass_plat(dev);
Bin Mengcbc3da82018-10-15 02:21:07 -0700742
Ovidiu Panait76160a02020-07-24 14:12:21 +0300743 part_init(desc);
AKASHI Takahiro358e6c72022-04-19 10:05:10 +0900744
745 if (desc->part_type != PART_TYPE_UNKNOWN &&
746 part_create_block_devices(dev))
747 debug("*** creating partitions failed\n");
Ovidiu Panait76160a02020-07-24 14:12:21 +0300748 }
Bin Mengcbc3da82018-10-15 02:21:07 -0700749
750 return 0;
751}
752
Simon Glasscceee552016-02-29 15:25:55 -0700753UCLASS_DRIVER(blk) = {
754 .id = UCLASS_BLK,
755 .name = "blk",
Bin Mengcbc3da82018-10-15 02:21:07 -0700756 .post_probe = blk_post_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -0700757 .per_device_plat_auto = sizeof(struct blk_desc),
Simon Glasscceee552016-02-29 15:25:55 -0700758};