blob: e82789f4a389184d0542161fd28c11ab4972b184 [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 Glass95e6e032022-08-11 19:34:57 -060020static struct {
21 enum uclass_id id;
22 const char *name;
Simon Glassfada3f92022-09-17 09:00:09 -060023} uclass_idname_str[] = {
Simon Glassdbfa32c2022-08-11 19:34:59 -060024 { UCLASS_IDE, "ide" },
25 { UCLASS_SCSI, "scsi" },
26 { UCLASS_USB, "usb" },
27 { UCLASS_MMC, "mmc" },
28 { UCLASS_AHCI, "sata" },
29 { UCLASS_ROOT, "host" },
30 { UCLASS_NVME, "nvme" },
31 { UCLASS_EFI_MEDIA, "efi" },
32 { UCLASS_EFI_LOADER, "efiloader" },
33 { UCLASS_VIRTIO, "virtio" },
34 { UCLASS_PVBLOCK, "pvblock" },
Simon Glass4131ad52016-05-01 11:36:08 -060035};
36
Simon Glassfada3f92022-09-17 09:00:09 -060037static enum uclass_id uclass_name_to_iftype(const char *uclass_idname)
Simon Glass4131ad52016-05-01 11:36:08 -060038{
39 int i;
40
Simon Glassfada3f92022-09-17 09:00:09 -060041 for (i = 0; i < ARRAY_SIZE(uclass_idname_str); i++) {
42 if (!strcmp(uclass_idname, uclass_idname_str[i].name))
43 return uclass_idname_str[i].id;
Simon Glass4131ad52016-05-01 11:36:08 -060044 }
45
Simon Glassdbfa32c2022-08-11 19:34:59 -060046 return UCLASS_INVALID;
Simon Glass4131ad52016-05-01 11:36:08 -060047}
48
Simon Glassfada3f92022-09-17 09:00:09 -060049static enum uclass_id conv_uclass_id(enum uclass_id uclass_id)
Simon Glass4131ad52016-05-01 11:36:08 -060050{
Simon Glassdbfa32c2022-08-11 19:34:59 -060051 /*
52 * This strange adjustment is used because we use UCLASS_MASS_STORAGE
53 * for USB storage devices, so need to return this as the uclass to
54 * use for USB. In fact USB_UCLASS is for USB controllers, not
55 * peripherals.
56 *
57 * The name of the UCLASS_MASS_STORAGE uclass driver is
58 * "usb_mass_storage", but we want to use "usb" in things like the
59 * 'part list' command and when showing interfaces.
60 *
61 * So for now we have this one-way conversion.
62 *
63 * The fix for this is possibly to:
64 * - rename UCLASS_MASS_STORAGE name to "usb"
65 * - rename UCLASS_USB name to "usb_ctlr"
66 * - use UCLASS_MASS_STORAGE instead of UCLASS_USB in if_typename_str
67 */
Simon Glassfada3f92022-09-17 09:00:09 -060068 if (uclass_id == UCLASS_USB)
Simon Glassdbfa32c2022-08-11 19:34:59 -060069 return UCLASS_MASS_STORAGE;
Simon Glassfada3f92022-09-17 09:00:09 -060070 return uclass_id;
Simon Glass4131ad52016-05-01 11:36:08 -060071}
72
Simon Glassfada3f92022-09-17 09:00:09 -060073const char *blk_get_uclass_name(enum uclass_id uclass_id)
Simon Glass85af5a42017-07-29 11:34:53 -060074{
Simon Glass95e6e032022-08-11 19:34:57 -060075 int i;
76
Simon Glassfada3f92022-09-17 09:00:09 -060077 for (i = 0; i < ARRAY_SIZE(uclass_idname_str); i++) {
78 if (uclass_idname_str[i].id == uclass_id)
79 return uclass_idname_str[i].name;
Simon Glass95e6e032022-08-11 19:34:57 -060080 }
81
82 return "(none)";
Simon Glass85af5a42017-07-29 11:34:53 -060083}
84
Simon Glassfada3f92022-09-17 09:00:09 -060085struct blk_desc *blk_get_devnum_by_uclass_id(enum uclass_id uclass_id, int devnum)
Simon Glass4131ad52016-05-01 11:36:08 -060086{
87 struct blk_desc *desc;
88 struct udevice *dev;
89 int ret;
90
Simon Glassfada3f92022-09-17 09:00:09 -060091 ret = blk_get_device(uclass_id, devnum, &dev);
Simon Glass4131ad52016-05-01 11:36:08 -060092 if (ret)
93 return NULL;
Simon Glass71fa5b42020-12-03 16:55:18 -070094 desc = dev_get_uclass_plat(dev);
Simon Glass4131ad52016-05-01 11:36:08 -060095
96 return desc;
97}
98
99/*
100 * This function is complicated with driver model. We look up the interface
101 * name in a local table. This gives us an interface type which we can match
102 * against the uclass of the block device's parent.
103 */
Simon Glassfada3f92022-09-17 09:00:09 -0600104struct blk_desc *blk_get_devnum_by_uclass_idname(const char *uclass_idname, int devnum)
Simon Glass4131ad52016-05-01 11:36:08 -0600105{
106 enum uclass_id uclass_id;
Simon Glass57b3d2e2022-08-11 19:35:01 -0600107 enum uclass_id type;
Simon Glass4131ad52016-05-01 11:36:08 -0600108 struct udevice *dev;
109 struct uclass *uc;
110 int ret;
111
Simon Glassfada3f92022-09-17 09:00:09 -0600112 type = uclass_name_to_iftype(uclass_idname);
Simon Glassdbfa32c2022-08-11 19:34:59 -0600113 if (type == UCLASS_INVALID) {
Simon Glass4131ad52016-05-01 11:36:08 -0600114 debug("%s: Unknown interface type '%s'\n", __func__,
Simon Glassfada3f92022-09-17 09:00:09 -0600115 uclass_idname);
Simon Glass4131ad52016-05-01 11:36:08 -0600116 return NULL;
117 }
Simon Glassfada3f92022-09-17 09:00:09 -0600118 uclass_id = conv_uclass_id(type);
Simon Glass4131ad52016-05-01 11:36:08 -0600119 if (uclass_id == UCLASS_INVALID) {
120 debug("%s: Unknown uclass for interface type'\n",
Simon Glassfada3f92022-09-17 09:00:09 -0600121 blk_get_uclass_name(type));
Simon Glass4131ad52016-05-01 11:36:08 -0600122 return NULL;
123 }
124
125 ret = uclass_get(UCLASS_BLK, &uc);
126 if (ret)
127 return NULL;
128 uclass_foreach_dev(dev, uc) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700129 struct blk_desc *desc = dev_get_uclass_plat(dev);
Simon Glass4131ad52016-05-01 11:36:08 -0600130
Simon Glassfada3f92022-09-17 09:00:09 -0600131 debug("%s: uclass_id=%d, devnum=%d: %s, %d, %d\n", __func__,
132 type, devnum, dev->name, desc->uclass_id, desc->devnum);
Simon Glass4131ad52016-05-01 11:36:08 -0600133 if (desc->devnum != devnum)
134 continue;
135
136 /* Find out the parent device uclass */
137 if (device_get_uclass_id(dev->parent) != uclass_id) {
138 debug("%s: parent uclass %d, this dev %d\n", __func__,
139 device_get_uclass_id(dev->parent), uclass_id);
140 continue;
141 }
142
143 if (device_probe(dev))
144 return NULL;
145
146 debug("%s: Device desc %p\n", __func__, desc);
147 return desc;
148 }
149 debug("%s: No device found\n", __func__);
150
151 return NULL;
152}
153
154/**
Tien Fong Chee10378522018-07-06 16:26:36 +0800155 * blk_get_by_device() - Get the block device descriptor for the given device
156 * @dev: Instance of a storage device
157 *
158 * Return: With block device descriptor on success , NULL if there is no such
159 * block device.
160 */
161struct blk_desc *blk_get_by_device(struct udevice *dev)
162{
Simon Glasscfd72932019-09-25 08:55:56 -0600163 struct udevice *child_dev;
Tien Fong Chee10378522018-07-06 16:26:36 +0800164
Simon Glasscfd72932019-09-25 08:55:56 -0600165 device_foreach_child(child_dev, dev) {
Tien Fong Chee10378522018-07-06 16:26:36 +0800166 if (device_get_uclass_id(child_dev) != UCLASS_BLK)
167 continue;
168
Simon Glass71fa5b42020-12-03 16:55:18 -0700169 return dev_get_uclass_plat(child_dev);
Tien Fong Chee10378522018-07-06 16:26:36 +0800170 }
171
172 debug("%s: No block device found\n", __func__);
173
174 return NULL;
175}
176
177/**
Simon Glass4131ad52016-05-01 11:36:08 -0600178 * get_desc() - Get the block device descriptor for the given device number
179 *
Simon Glassfada3f92022-09-17 09:00:09 -0600180 * @uclass_id: Interface type
Simon Glass4131ad52016-05-01 11:36:08 -0600181 * @devnum: Device number (0 = first)
182 * @descp: Returns block device descriptor on success
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100183 * Return: 0 on success, -ENODEV if there is no such device and no device
Simon Glass4131ad52016-05-01 11:36:08 -0600184 * with a higher device number, -ENOENT if there is no such device but there
185 * is one with a higher number, or other -ve on other error.
186 */
Simon Glassfada3f92022-09-17 09:00:09 -0600187static int get_desc(enum uclass_id uclass_id, int devnum, struct blk_desc **descp)
Simon Glass4131ad52016-05-01 11:36:08 -0600188{
189 bool found_more = false;
190 struct udevice *dev;
191 struct uclass *uc;
192 int ret;
193
194 *descp = NULL;
195 ret = uclass_get(UCLASS_BLK, &uc);
196 if (ret)
197 return ret;
198 uclass_foreach_dev(dev, uc) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700199 struct blk_desc *desc = dev_get_uclass_plat(dev);
Simon Glass4131ad52016-05-01 11:36:08 -0600200
Simon Glassfada3f92022-09-17 09:00:09 -0600201 debug("%s: uclass_id=%d, devnum=%d: %s, %d, %d\n", __func__,
202 uclass_id, devnum, dev->name, desc->uclass_id, desc->devnum);
203 if (desc->uclass_id == uclass_id) {
Simon Glass4131ad52016-05-01 11:36:08 -0600204 if (desc->devnum == devnum) {
205 ret = device_probe(dev);
206 if (ret)
207 return ret;
208
Michal Simekf438b6b2016-11-16 17:37:42 +0100209 *descp = desc;
210 return 0;
Simon Glass4131ad52016-05-01 11:36:08 -0600211 } else if (desc->devnum > devnum) {
212 found_more = true;
213 }
214 }
215 }
216
217 return found_more ? -ENOENT : -ENODEV;
218}
219
Simon Glassfada3f92022-09-17 09:00:09 -0600220int blk_select_hwpart_devnum(enum uclass_id uclass_id, int devnum, int hwpart)
Simon Glass13c2c292016-05-01 13:52:30 -0600221{
222 struct udevice *dev;
223 int ret;
224
Simon Glassfada3f92022-09-17 09:00:09 -0600225 ret = blk_get_device(uclass_id, devnum, &dev);
Simon Glass13c2c292016-05-01 13:52:30 -0600226 if (ret)
227 return ret;
228
developerf50d8d12019-08-27 15:32:18 +0800229 return blk_select_hwpart(dev, hwpart);
Simon Glass13c2c292016-05-01 13:52:30 -0600230}
231
Simon Glassfada3f92022-09-17 09:00:09 -0600232int blk_list_part(enum uclass_id uclass_id)
Simon Glass4131ad52016-05-01 11:36:08 -0600233{
234 struct blk_desc *desc;
235 int devnum, ok;
236 int ret;
237
238 for (ok = 0, devnum = 0;; ++devnum) {
Simon Glassfada3f92022-09-17 09:00:09 -0600239 ret = get_desc(uclass_id, devnum, &desc);
Simon Glass4131ad52016-05-01 11:36:08 -0600240 if (ret == -ENODEV)
241 break;
242 else if (ret)
243 continue;
244 if (desc->part_type != PART_TYPE_UNKNOWN) {
245 ++ok;
246 if (devnum)
247 putc('\n');
248 part_print(desc);
249 }
250 }
251 if (!ok)
252 return -ENODEV;
253
254 return 0;
255}
256
Simon Glassfada3f92022-09-17 09:00:09 -0600257int blk_print_part_devnum(enum uclass_id uclass_id, int devnum)
Simon Glass4131ad52016-05-01 11:36:08 -0600258{
259 struct blk_desc *desc;
260 int ret;
261
Simon Glassfada3f92022-09-17 09:00:09 -0600262 ret = get_desc(uclass_id, devnum, &desc);
Simon Glass4131ad52016-05-01 11:36:08 -0600263 if (ret)
264 return ret;
265 if (desc->type == DEV_TYPE_UNKNOWN)
266 return -ENOENT;
267 part_print(desc);
268
269 return 0;
270}
271
Simon Glassfada3f92022-09-17 09:00:09 -0600272void blk_list_devices(enum uclass_id uclass_id)
Simon Glass4131ad52016-05-01 11:36:08 -0600273{
274 struct blk_desc *desc;
275 int ret;
276 int i;
277
278 for (i = 0;; ++i) {
Simon Glassfada3f92022-09-17 09:00:09 -0600279 ret = get_desc(uclass_id, i, &desc);
Simon Glass4131ad52016-05-01 11:36:08 -0600280 if (ret == -ENODEV)
281 break;
282 else if (ret)
283 continue;
284 if (desc->type == DEV_TYPE_UNKNOWN)
285 continue; /* list only known devices */
286 printf("Device %d: ", i);
287 dev_print(desc);
288 }
289}
290
Simon Glassfada3f92022-09-17 09:00:09 -0600291int blk_print_device_num(enum uclass_id uclass_id, int devnum)
Simon Glass4131ad52016-05-01 11:36:08 -0600292{
293 struct blk_desc *desc;
294 int ret;
295
Simon Glassfada3f92022-09-17 09:00:09 -0600296 ret = get_desc(uclass_id, devnum, &desc);
Simon Glass4131ad52016-05-01 11:36:08 -0600297 if (ret)
298 return ret;
299 printf("\nIDE device %d: ", devnum);
300 dev_print(desc);
301
302 return 0;
303}
304
Simon Glassfada3f92022-09-17 09:00:09 -0600305int blk_show_device(enum uclass_id uclass_id, int devnum)
Simon Glass4131ad52016-05-01 11:36:08 -0600306{
307 struct blk_desc *desc;
308 int ret;
309
310 printf("\nDevice %d: ", devnum);
Simon Glassfada3f92022-09-17 09:00:09 -0600311 ret = get_desc(uclass_id, devnum, &desc);
Simon Glass4131ad52016-05-01 11:36:08 -0600312 if (ret == -ENODEV || ret == -ENOENT) {
313 printf("unknown device\n");
314 return -ENODEV;
315 }
316 if (ret)
317 return ret;
318 dev_print(desc);
319
320 if (desc->type == DEV_TYPE_UNKNOWN)
321 return -ENOENT;
322
323 return 0;
324}
325
Simon Glassfada3f92022-09-17 09:00:09 -0600326ulong blk_read_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
Simon Glass4131ad52016-05-01 11:36:08 -0600327 lbaint_t blkcnt, void *buffer)
328{
329 struct blk_desc *desc;
330 ulong n;
331 int ret;
332
Simon Glassfada3f92022-09-17 09:00:09 -0600333 ret = get_desc(uclass_id, devnum, &desc);
Simon Glass4131ad52016-05-01 11:36:08 -0600334 if (ret)
335 return ret;
336 n = blk_dread(desc, start, blkcnt, buffer);
337 if (IS_ERR_VALUE(n))
338 return n;
339
Simon Glass4131ad52016-05-01 11:36:08 -0600340 return n;
341}
342
Simon Glassfada3f92022-09-17 09:00:09 -0600343ulong blk_write_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
Simon Glass4131ad52016-05-01 11:36:08 -0600344 lbaint_t blkcnt, const void *buffer)
345{
346 struct blk_desc *desc;
347 int ret;
348
Simon Glassfada3f92022-09-17 09:00:09 -0600349 ret = get_desc(uclass_id, devnum, &desc);
Simon Glass4131ad52016-05-01 11:36:08 -0600350 if (ret)
351 return ret;
352 return blk_dwrite(desc, start, blkcnt, buffer);
353}
354
Simon Glass13c2c292016-05-01 13:52:30 -0600355int blk_select_hwpart(struct udevice *dev, int hwpart)
356{
357 const struct blk_ops *ops = blk_get_ops(dev);
358
359 if (!ops)
360 return -ENOSYS;
361 if (!ops->select_hwpart)
362 return 0;
363
364 return ops->select_hwpart(dev, hwpart);
365}
366
367int blk_dselect_hwpart(struct blk_desc *desc, int hwpart)
368{
developerf50d8d12019-08-27 15:32:18 +0800369 return blk_select_hwpart(desc->bdev, hwpart);
Simon Glass13c2c292016-05-01 13:52:30 -0600370}
371
Simon Glassfada3f92022-09-17 09:00:09 -0600372int blk_first_device(int uclass_id, struct udevice **devp)
Simon Glasscceee552016-02-29 15:25:55 -0700373{
374 struct blk_desc *desc;
375 int ret;
376
Stefan Roeseef58a902017-11-29 16:46:42 +0100377 ret = uclass_find_first_device(UCLASS_BLK, devp);
Simon Glasscceee552016-02-29 15:25:55 -0700378 if (ret)
379 return ret;
380 if (!*devp)
381 return -ENODEV;
382 do {
Simon Glass71fa5b42020-12-03 16:55:18 -0700383 desc = dev_get_uclass_plat(*devp);
Simon Glassfada3f92022-09-17 09:00:09 -0600384 if (desc->uclass_id == uclass_id)
Simon Glasscceee552016-02-29 15:25:55 -0700385 return 0;
Stefan Roeseef58a902017-11-29 16:46:42 +0100386 ret = uclass_find_next_device(devp);
Simon Glasscceee552016-02-29 15:25:55 -0700387 if (ret)
388 return ret;
389 } while (*devp);
390
391 return -ENODEV;
392}
393
394int blk_next_device(struct udevice **devp)
395{
396 struct blk_desc *desc;
Simon Glassfada3f92022-09-17 09:00:09 -0600397 int ret, uclass_id;
Simon Glasscceee552016-02-29 15:25:55 -0700398
Simon Glass71fa5b42020-12-03 16:55:18 -0700399 desc = dev_get_uclass_plat(*devp);
Simon Glassfada3f92022-09-17 09:00:09 -0600400 uclass_id = desc->uclass_id;
Simon Glasscceee552016-02-29 15:25:55 -0700401 do {
Stefan Roeseef58a902017-11-29 16:46:42 +0100402 ret = uclass_find_next_device(devp);
Simon Glasscceee552016-02-29 15:25:55 -0700403 if (ret)
404 return ret;
405 if (!*devp)
406 return -ENODEV;
Simon Glass71fa5b42020-12-03 16:55:18 -0700407 desc = dev_get_uclass_plat(*devp);
Simon Glassfada3f92022-09-17 09:00:09 -0600408 if (desc->uclass_id == uclass_id)
Simon Glasscceee552016-02-29 15:25:55 -0700409 return 0;
410 } while (1);
411}
412
Simon Glassfada3f92022-09-17 09:00:09 -0600413int blk_find_device(int uclass_id, int devnum, struct udevice **devp)
Simon Glasscceee552016-02-29 15:25:55 -0700414{
415 struct uclass *uc;
416 struct udevice *dev;
417 int ret;
418
419 ret = uclass_get(UCLASS_BLK, &uc);
420 if (ret)
421 return ret;
422 uclass_foreach_dev(dev, uc) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700423 struct blk_desc *desc = dev_get_uclass_plat(dev);
Simon Glasscceee552016-02-29 15:25:55 -0700424
Simon Glassfada3f92022-09-17 09:00:09 -0600425 debug("%s: uclass_id=%d, devnum=%d: %s, %d, %d\n", __func__,
426 uclass_id, devnum, dev->name, desc->uclass_id, desc->devnum);
427 if (desc->uclass_id == uclass_id && desc->devnum == devnum) {
Simon Glasscceee552016-02-29 15:25:55 -0700428 *devp = dev;
Simon Glassd5d4c102017-04-23 20:02:05 -0600429 return 0;
Simon Glasscceee552016-02-29 15:25:55 -0700430 }
431 }
432
433 return -ENODEV;
434}
435
Simon Glassfada3f92022-09-17 09:00:09 -0600436int blk_get_device(int uclass_id, int devnum, struct udevice **devp)
Simon Glassd5d4c102017-04-23 20:02:05 -0600437{
438 int ret;
439
Simon Glassfada3f92022-09-17 09:00:09 -0600440 ret = blk_find_device(uclass_id, devnum, devp);
Simon Glassd5d4c102017-04-23 20:02:05 -0600441 if (ret)
442 return ret;
443
444 return device_probe(*devp);
445}
446
Simon Glass18861002022-10-20 18:22:54 -0600447long blk_read(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *buf)
Simon Glasscceee552016-02-29 15:25:55 -0700448{
Simon Glass18861002022-10-20 18:22:54 -0600449 struct blk_desc *desc = dev_get_uclass_plat(dev);
Simon Glasscceee552016-02-29 15:25:55 -0700450 const struct blk_ops *ops = blk_get_ops(dev);
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700451 ulong blks_read;
Simon Glasscceee552016-02-29 15:25:55 -0700452
453 if (!ops->read)
454 return -ENOSYS;
455
Simon Glass18861002022-10-20 18:22:54 -0600456 if (blkcache_read(desc->uclass_id, desc->devnum,
457 start, blkcnt, desc->blksz, buf))
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700458 return blkcnt;
Simon Glass18861002022-10-20 18:22:54 -0600459 blks_read = ops->read(dev, start, blkcnt, buf);
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700460 if (blks_read == blkcnt)
Simon Glass18861002022-10-20 18:22:54 -0600461 blkcache_fill(desc->uclass_id, desc->devnum, start, blkcnt,
462 desc->blksz, buf);
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700463
464 return blks_read;
Simon Glasscceee552016-02-29 15:25:55 -0700465}
466
Simon Glass18861002022-10-20 18:22:54 -0600467long blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
468 const void *buf)
Simon Glasscceee552016-02-29 15:25:55 -0700469{
Simon Glass18861002022-10-20 18:22:54 -0600470 struct blk_desc *desc = dev_get_uclass_plat(dev);
Simon Glasscceee552016-02-29 15:25:55 -0700471 const struct blk_ops *ops = blk_get_ops(dev);
472
473 if (!ops->write)
474 return -ENOSYS;
475
Simon Glass18861002022-10-20 18:22:54 -0600476 blkcache_invalidate(desc->uclass_id, desc->devnum);
477
478 return ops->write(dev, start, blkcnt, buf);
Simon Glasscceee552016-02-29 15:25:55 -0700479}
480
Simon Glass18861002022-10-20 18:22:54 -0600481long blk_erase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt)
Simon Glasscceee552016-02-29 15:25:55 -0700482{
Simon Glass18861002022-10-20 18:22:54 -0600483 struct blk_desc *desc = dev_get_uclass_plat(dev);
Simon Glasscceee552016-02-29 15:25:55 -0700484 const struct blk_ops *ops = blk_get_ops(dev);
485
486 if (!ops->erase)
487 return -ENOSYS;
488
Simon Glass18861002022-10-20 18:22:54 -0600489 blkcache_invalidate(desc->uclass_id, desc->devnum);
490
Simon Glasscceee552016-02-29 15:25:55 -0700491 return ops->erase(dev, start, blkcnt);
492}
493
Simon Glass18861002022-10-20 18:22:54 -0600494ulong blk_dread(struct blk_desc *desc, lbaint_t start, lbaint_t blkcnt,
495 void *buffer)
496{
497 return blk_read(desc->bdev, start, blkcnt, buffer);
498}
499
500ulong blk_dwrite(struct blk_desc *desc, lbaint_t start, lbaint_t blkcnt,
501 const void *buffer)
502{
503 return blk_write(desc->bdev, start, blkcnt, buffer);
504}
505
506ulong blk_derase(struct blk_desc *desc, lbaint_t start, lbaint_t blkcnt)
507{
508 return blk_erase(desc->bdev, start, blkcnt);
509}
510
Simon Glass4f269132017-05-27 11:37:17 -0600511int blk_get_from_parent(struct udevice *parent, struct udevice **devp)
512{
513 struct udevice *dev;
514 enum uclass_id id;
515 int ret;
516
517 device_find_first_child(parent, &dev);
518 if (!dev) {
519 debug("%s: No block device found for parent '%s'\n", __func__,
520 parent->name);
521 return -ENODEV;
522 }
523 id = device_get_uclass_id(dev);
524 if (id != UCLASS_BLK) {
525 debug("%s: Incorrect uclass %s for block device '%s'\n",
526 __func__, uclass_get_name(id), dev->name);
527 return -ENOTBLK;
528 }
529 ret = device_probe(dev);
530 if (ret)
531 return ret;
532 *devp = dev;
533
534 return 0;
535}
536
Simon Glassf3086cf2022-04-24 23:31:03 -0600537const char *blk_get_devtype(struct udevice *dev)
538{
539 struct udevice *parent = dev_get_parent(dev);
540
541 return uclass_get_name(device_get_uclass_id(parent));
542};
543
Simon Glassfada3f92022-09-17 09:00:09 -0600544int blk_find_max_devnum(enum uclass_id uclass_id)
Simon Glassd089ba32016-05-01 11:36:28 -0600545{
546 struct udevice *dev;
547 int max_devnum = -ENODEV;
548 struct uclass *uc;
549 int ret;
550
551 ret = uclass_get(UCLASS_BLK, &uc);
552 if (ret)
553 return ret;
554 uclass_foreach_dev(dev, uc) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700555 struct blk_desc *desc = dev_get_uclass_plat(dev);
Simon Glassd089ba32016-05-01 11:36:28 -0600556
Simon Glassfada3f92022-09-17 09:00:09 -0600557 if (desc->uclass_id == uclass_id && desc->devnum > max_devnum)
Simon Glassd089ba32016-05-01 11:36:28 -0600558 max_devnum = desc->devnum;
559 }
560
561 return max_devnum;
562}
563
Simon Glassfada3f92022-09-17 09:00:09 -0600564int blk_next_free_devnum(enum uclass_id uclass_id)
Simon Glassdbc38612017-04-23 20:02:06 -0600565{
566 int ret;
567
Simon Glassfada3f92022-09-17 09:00:09 -0600568 ret = blk_find_max_devnum(uclass_id);
Simon Glassdbc38612017-04-23 20:02:06 -0600569 if (ret == -ENODEV)
570 return 0;
571 if (ret < 0)
572 return ret;
573
574 return ret + 1;
575}
576
Simon Glassfc7a7442021-07-05 16:32:59 -0600577static int blk_flags_check(struct udevice *dev, enum blk_flag_t req_flags)
578{
579 const struct blk_desc *desc = dev_get_uclass_plat(dev);
580 enum blk_flag_t flags;
581
582 flags = desc->removable ? BLKF_REMOVABLE : BLKF_FIXED;
583
584 return flags & req_flags ? 0 : 1;
585}
586
Simon Glass8e61f932022-02-28 12:08:35 -0700587int blk_find_first(enum blk_flag_t flags, struct udevice **devp)
588{
589 int ret;
590
591 for (ret = uclass_find_first_device(UCLASS_BLK, devp);
592 *devp && !blk_flags_check(*devp, flags);
593 ret = uclass_find_next_device(devp))
594 return 0;
595
596 return -ENODEV;
597}
598
599int blk_find_next(enum blk_flag_t flags, struct udevice **devp)
600{
601 int ret;
602
603 for (ret = uclass_find_next_device(devp);
604 *devp && !blk_flags_check(*devp, flags);
605 ret = uclass_find_next_device(devp))
606 return 0;
607
608 return -ENODEV;
609}
610
Simon Glassfc7a7442021-07-05 16:32:59 -0600611int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp)
612{
Michal Suchanekac9e9fc2022-10-12 21:58:01 +0200613 for (uclass_first_device(UCLASS_BLK, devp);
614 *devp;
615 uclass_next_device(devp)) {
Simon Glassfc7a7442021-07-05 16:32:59 -0600616 if (!blk_flags_check(*devp, flags))
617 return 0;
618 }
619
620 return -ENODEV;
621}
622
623int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp)
624{
Michal Suchanekac9e9fc2022-10-12 21:58:01 +0200625 for (uclass_next_device(devp);
626 *devp;
627 uclass_next_device(devp)) {
Simon Glassfc7a7442021-07-05 16:32:59 -0600628 if (!blk_flags_check(*devp, flags))
629 return 0;
630 }
631
632 return -ENODEV;
633}
634
635int blk_count_devices(enum blk_flag_t flag)
636{
637 struct udevice *dev;
638 int count = 0;
639
640 blk_foreach_probe(flag, dev)
641 count++;
642
643 return count;
644}
645
Simon Glassfada3f92022-09-17 09:00:09 -0600646static int blk_claim_devnum(enum uclass_id uclass_id, int devnum)
Simon Glasse4fef742017-04-23 20:02:07 -0600647{
648 struct udevice *dev;
649 struct uclass *uc;
650 int ret;
651
652 ret = uclass_get(UCLASS_BLK, &uc);
653 if (ret)
654 return ret;
655 uclass_foreach_dev(dev, uc) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700656 struct blk_desc *desc = dev_get_uclass_plat(dev);
Simon Glasse4fef742017-04-23 20:02:07 -0600657
Simon Glassfada3f92022-09-17 09:00:09 -0600658 if (desc->uclass_id == uclass_id && desc->devnum == devnum) {
659 int next = blk_next_free_devnum(uclass_id);
Simon Glasse4fef742017-04-23 20:02:07 -0600660
661 if (next < 0)
662 return next;
663 desc->devnum = next;
664 return 0;
665 }
666 }
667
668 return -ENOENT;
669}
670
Simon Glasscceee552016-02-29 15:25:55 -0700671int blk_create_device(struct udevice *parent, const char *drv_name,
Simon Glassfada3f92022-09-17 09:00:09 -0600672 const char *name, int uclass_id, int devnum, int blksz,
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200673 lbaint_t lba, struct udevice **devp)
Simon Glasscceee552016-02-29 15:25:55 -0700674{
675 struct blk_desc *desc;
676 struct udevice *dev;
677 int ret;
678
Simon Glassd089ba32016-05-01 11:36:28 -0600679 if (devnum == -1) {
Simon Glassfada3f92022-09-17 09:00:09 -0600680 devnum = blk_next_free_devnum(uclass_id);
Simon Glasse4fef742017-04-23 20:02:07 -0600681 } else {
Simon Glassfada3f92022-09-17 09:00:09 -0600682 ret = blk_claim_devnum(uclass_id, devnum);
Simon Glasse4fef742017-04-23 20:02:07 -0600683 if (ret < 0 && ret != -ENOENT)
Simon Glassd089ba32016-05-01 11:36:28 -0600684 return ret;
Simon Glassd089ba32016-05-01 11:36:28 -0600685 }
Simon Glasse4fef742017-04-23 20:02:07 -0600686 if (devnum < 0)
687 return devnum;
Simon Glass77f7fb82016-05-01 13:52:22 -0600688 ret = device_bind_driver(parent, drv_name, name, &dev);
689 if (ret)
690 return ret;
Simon Glass71fa5b42020-12-03 16:55:18 -0700691 desc = dev_get_uclass_plat(dev);
Simon Glassfada3f92022-09-17 09:00:09 -0600692 desc->uclass_id = uclass_id;
Simon Glass77f7fb82016-05-01 13:52:22 -0600693 desc->blksz = blksz;
Heinrich Schuchardt09ba4ea2019-10-25 12:15:31 +0200694 desc->log2blksz = LOG2(desc->blksz);
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200695 desc->lba = lba;
Simon Glass77f7fb82016-05-01 13:52:22 -0600696 desc->part_type = PART_TYPE_UNKNOWN;
697 desc->bdev = dev;
Simon Glasscceee552016-02-29 15:25:55 -0700698 desc->devnum = devnum;
699 *devp = dev;
700
701 return 0;
702}
703
Simon Glass966b6952016-05-01 11:36:29 -0600704int blk_create_devicef(struct udevice *parent, const char *drv_name,
Simon Glassfada3f92022-09-17 09:00:09 -0600705 const char *name, int uclass_id, int devnum, int blksz,
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200706 lbaint_t lba, struct udevice **devp)
Simon Glass966b6952016-05-01 11:36:29 -0600707{
708 char dev_name[30], *str;
Simon Glass39e54922016-05-01 13:52:24 -0600709 int ret;
Simon Glass966b6952016-05-01 11:36:29 -0600710
711 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
712 str = strdup(dev_name);
713 if (!str)
714 return -ENOMEM;
715
Simon Glassfada3f92022-09-17 09:00:09 -0600716 ret = blk_create_device(parent, drv_name, str, uclass_id, devnum,
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200717 blksz, lba, devp);
Simon Glass39e54922016-05-01 13:52:24 -0600718 if (ret) {
719 free(str);
720 return ret;
721 }
722 device_set_name_alloced(*devp);
723
Simon Glass44505972017-07-29 11:34:59 -0600724 return 0;
Simon Glass966b6952016-05-01 11:36:29 -0600725}
726
AKASHI Takahiro3e32dbe2021-12-10 15:49:29 +0900727int blk_probe_or_unbind(struct udevice *dev)
728{
729 int ret;
730
731 ret = device_probe(dev);
732 if (ret) {
733 log_debug("probing %s failed\n", dev->name);
734 device_unbind(dev);
735 }
736
737 return ret;
738}
739
Simon Glassfada3f92022-09-17 09:00:09 -0600740int blk_unbind_all(int uclass_id)
Simon Glasscceee552016-02-29 15:25:55 -0700741{
742 struct uclass *uc;
743 struct udevice *dev, *next;
744 int ret;
745
746 ret = uclass_get(UCLASS_BLK, &uc);
747 if (ret)
748 return ret;
749 uclass_foreach_dev_safe(dev, next, uc) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700750 struct blk_desc *desc = dev_get_uclass_plat(dev);
Simon Glasscceee552016-02-29 15:25:55 -0700751
Simon Glassfada3f92022-09-17 09:00:09 -0600752 if (desc->uclass_id == uclass_id) {
Stefan Roese80b5bc92017-03-20 12:51:48 +0100753 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glasscceee552016-02-29 15:25:55 -0700754 if (ret)
755 return ret;
756 ret = device_unbind(dev);
757 if (ret)
758 return ret;
759 }
760 }
761
762 return 0;
763}
764
Bin Mengcbc3da82018-10-15 02:21:07 -0700765static int blk_post_probe(struct udevice *dev)
766{
Simon Glassf5ac3032022-08-11 19:34:45 -0600767 if (CONFIG_IS_ENABLED(PARTITIONS) && blk_enabled()) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700768 struct blk_desc *desc = dev_get_uclass_plat(dev);
Bin Mengcbc3da82018-10-15 02:21:07 -0700769
Ovidiu Panait76160a02020-07-24 14:12:21 +0300770 part_init(desc);
AKASHI Takahiro358e6c72022-04-19 10:05:10 +0900771
772 if (desc->part_type != PART_TYPE_UNKNOWN &&
773 part_create_block_devices(dev))
774 debug("*** creating partitions failed\n");
Ovidiu Panait76160a02020-07-24 14:12:21 +0300775 }
Bin Mengcbc3da82018-10-15 02:21:07 -0700776
777 return 0;
778}
779
Simon Glasscceee552016-02-29 15:25:55 -0700780UCLASS_DRIVER(blk) = {
781 .id = UCLASS_BLK,
782 .name = "blk",
Bin Mengcbc3da82018-10-15 02:21:07 -0700783 .post_probe = blk_post_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -0700784 .per_device_plat_auto = sizeof(struct blk_desc),
Simon Glasscceee552016-02-29 15:25:55 -0700785};