blob: 7d12d5413f18b99f2df1cf96150c1cb9fa70c7e8 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass09d71aa2016-02-29 15:25:55 -07002/*
3 * Copyright (C) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass09d71aa2016-02-29 15:25:55 -07005 */
6
Patrick Delaunayb953ec22021-04-27 11:02:19 +02007#define LOG_CATEGORY UCLASS_BLK
8
Simon Glass09d71aa2016-02-29 15:25:55 -07009#include <common.h>
10#include <blk.h>
11#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070013#include <malloc.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060014#include <part.h>
Simon Glass09d71aa2016-02-29 15:25:55 -070015#include <dm/device-internal.h>
16#include <dm/lists.h>
Stefan Roese8a5cbc02017-11-29 16:46:42 +010017#include <dm/uclass-internal.h>
Simon Glass61b29b82020-02-03 07:36:15 -070018#include <linux/err.h>
Simon Glass09d71aa2016-02-29 15:25:55 -070019
Simon Glassbb2c9a32022-08-11 19:34:57 -060020static struct {
21 enum uclass_id id;
22 const char *name;
Simon Glass8149b152022-09-17 09:00:09 -060023} uclass_idname_str[] = {
Simon Glasse33a5c62022-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 Glassd508c822016-05-01 11:36:08 -060035};
36
Simon Glass8149b152022-09-17 09:00:09 -060037static enum uclass_id uclass_name_to_iftype(const char *uclass_idname)
Simon Glassd508c822016-05-01 11:36:08 -060038{
39 int i;
40
Simon Glass8149b152022-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 Glassd508c822016-05-01 11:36:08 -060044 }
45
Simon Glasse33a5c62022-08-11 19:34:59 -060046 return UCLASS_INVALID;
Simon Glassd508c822016-05-01 11:36:08 -060047}
48
Simon Glass8149b152022-09-17 09:00:09 -060049static enum uclass_id conv_uclass_id(enum uclass_id uclass_id)
Simon Glassd508c822016-05-01 11:36:08 -060050{
Simon Glasse33a5c62022-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 Glass8149b152022-09-17 09:00:09 -060068 if (uclass_id == UCLASS_USB)
Simon Glasse33a5c62022-08-11 19:34:59 -060069 return UCLASS_MASS_STORAGE;
Simon Glass8149b152022-09-17 09:00:09 -060070 return uclass_id;
Simon Glassd508c822016-05-01 11:36:08 -060071}
72
Simon Glass8149b152022-09-17 09:00:09 -060073const char *blk_get_uclass_name(enum uclass_id uclass_id)
Simon Glass6faa4ed2017-07-29 11:34:53 -060074{
Simon Glassbb2c9a32022-08-11 19:34:57 -060075 int i;
76
Simon Glass8149b152022-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 Glassbb2c9a32022-08-11 19:34:57 -060080 }
81
82 return "(none)";
Simon Glass6faa4ed2017-07-29 11:34:53 -060083}
84
Simon Glass8149b152022-09-17 09:00:09 -060085struct blk_desc *blk_get_devnum_by_uclass_id(enum uclass_id uclass_id, int devnum)
Simon Glassd508c822016-05-01 11:36:08 -060086{
87 struct blk_desc *desc;
88 struct udevice *dev;
89 int ret;
90
Simon Glass8149b152022-09-17 09:00:09 -060091 ret = blk_get_device(uclass_id, devnum, &dev);
Simon Glassd508c822016-05-01 11:36:08 -060092 if (ret)
93 return NULL;
Simon Glasscaa4daa2020-12-03 16:55:18 -070094 desc = dev_get_uclass_plat(dev);
Simon Glassd508c822016-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 Glass8149b152022-09-17 09:00:09 -0600104struct blk_desc *blk_get_devnum_by_uclass_idname(const char *uclass_idname, int devnum)
Simon Glassd508c822016-05-01 11:36:08 -0600105{
106 enum uclass_id uclass_id;
Simon Glassec8bdc92022-08-11 19:35:01 -0600107 enum uclass_id type;
Simon Glassd508c822016-05-01 11:36:08 -0600108 struct udevice *dev;
109 struct uclass *uc;
110 int ret;
111
Simon Glass8149b152022-09-17 09:00:09 -0600112 type = uclass_name_to_iftype(uclass_idname);
Simon Glasse33a5c62022-08-11 19:34:59 -0600113 if (type == UCLASS_INVALID) {
Simon Glassd508c822016-05-01 11:36:08 -0600114 debug("%s: Unknown interface type '%s'\n", __func__,
Simon Glass8149b152022-09-17 09:00:09 -0600115 uclass_idname);
Simon Glassd508c822016-05-01 11:36:08 -0600116 return NULL;
117 }
Simon Glass8149b152022-09-17 09:00:09 -0600118 uclass_id = conv_uclass_id(type);
Simon Glassd508c822016-05-01 11:36:08 -0600119 if (uclass_id == UCLASS_INVALID) {
120 debug("%s: Unknown uclass for interface type'\n",
Simon Glass8149b152022-09-17 09:00:09 -0600121 blk_get_uclass_name(type));
Simon Glassd508c822016-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 Glasscaa4daa2020-12-03 16:55:18 -0700129 struct blk_desc *desc = dev_get_uclass_plat(dev);
Simon Glassd508c822016-05-01 11:36:08 -0600130
Simon Glass8149b152022-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 Glassd508c822016-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 Cheebc53d262018-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 Glasse5f73902019-09-25 08:55:56 -0600163 struct udevice *child_dev;
Tien Fong Cheebc53d262018-07-06 16:26:36 +0800164
Simon Glasse5f73902019-09-25 08:55:56 -0600165 device_foreach_child(child_dev, dev) {
Tien Fong Cheebc53d262018-07-06 16:26:36 +0800166 if (device_get_uclass_id(child_dev) != UCLASS_BLK)
167 continue;
168
Simon Glasscaa4daa2020-12-03 16:55:18 -0700169 return dev_get_uclass_plat(child_dev);
Tien Fong Cheebc53d262018-07-06 16:26:36 +0800170 }
171
172 debug("%s: No block device found\n", __func__);
173
174 return NULL;
175}
176
177/**
Simon Glassd508c822016-05-01 11:36:08 -0600178 * get_desc() - Get the block device descriptor for the given device number
179 *
Simon Glass8149b152022-09-17 09:00:09 -0600180 * @uclass_id: Interface type
Simon Glassd508c822016-05-01 11:36:08 -0600181 * @devnum: Device number (0 = first)
182 * @descp: Returns block device descriptor on success
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100183 * Return: 0 on success, -ENODEV if there is no such device and no device
Simon Glassd508c822016-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 Glass8149b152022-09-17 09:00:09 -0600187static int get_desc(enum uclass_id uclass_id, int devnum, struct blk_desc **descp)
Simon Glassd508c822016-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 Glasscaa4daa2020-12-03 16:55:18 -0700199 struct blk_desc *desc = dev_get_uclass_plat(dev);
Simon Glassd508c822016-05-01 11:36:08 -0600200
Simon Glass8149b152022-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 Glassd508c822016-05-01 11:36:08 -0600204 if (desc->devnum == devnum) {
205 ret = device_probe(dev);
206 if (ret)
207 return ret;
208
Michal Simek4408f6f2016-11-16 17:37:42 +0100209 *descp = desc;
210 return 0;
Simon Glassd508c822016-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 Glass8149b152022-09-17 09:00:09 -0600220int blk_select_hwpart_devnum(enum uclass_id uclass_id, int devnum, int hwpart)
Simon Glasscd0fb552016-05-01 13:52:30 -0600221{
222 struct udevice *dev;
223 int ret;
224
Simon Glass8149b152022-09-17 09:00:09 -0600225 ret = blk_get_device(uclass_id, devnum, &dev);
Simon Glasscd0fb552016-05-01 13:52:30 -0600226 if (ret)
227 return ret;
228
Weijie Gao1ce88472019-08-27 15:32:18 +0800229 return blk_select_hwpart(dev, hwpart);
Simon Glasscd0fb552016-05-01 13:52:30 -0600230}
231
Simon Glass8149b152022-09-17 09:00:09 -0600232int blk_list_part(enum uclass_id uclass_id)
Simon Glassd508c822016-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 Glass8149b152022-09-17 09:00:09 -0600239 ret = get_desc(uclass_id, devnum, &desc);
Simon Glassd508c822016-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 Glass8149b152022-09-17 09:00:09 -0600257int blk_print_part_devnum(enum uclass_id uclass_id, int devnum)
Simon Glassd508c822016-05-01 11:36:08 -0600258{
259 struct blk_desc *desc;
260 int ret;
261
Simon Glass8149b152022-09-17 09:00:09 -0600262 ret = get_desc(uclass_id, devnum, &desc);
Simon Glassd508c822016-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 Glass8149b152022-09-17 09:00:09 -0600272void blk_list_devices(enum uclass_id uclass_id)
Simon Glassd508c822016-05-01 11:36:08 -0600273{
274 struct blk_desc *desc;
275 int ret;
276 int i;
277
278 for (i = 0;; ++i) {
Simon Glass8149b152022-09-17 09:00:09 -0600279 ret = get_desc(uclass_id, i, &desc);
Simon Glassd508c822016-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 Glass8149b152022-09-17 09:00:09 -0600291int blk_print_device_num(enum uclass_id uclass_id, int devnum)
Simon Glassd508c822016-05-01 11:36:08 -0600292{
293 struct blk_desc *desc;
294 int ret;
295
Simon Glass8149b152022-09-17 09:00:09 -0600296 ret = get_desc(uclass_id, devnum, &desc);
Simon Glassd508c822016-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 Glass8149b152022-09-17 09:00:09 -0600305int blk_show_device(enum uclass_id uclass_id, int devnum)
Simon Glassd508c822016-05-01 11:36:08 -0600306{
307 struct blk_desc *desc;
308 int ret;
309
310 printf("\nDevice %d: ", devnum);
Simon Glass8149b152022-09-17 09:00:09 -0600311 ret = get_desc(uclass_id, devnum, &desc);
Simon Glassd508c822016-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 Glass8149b152022-09-17 09:00:09 -0600326ulong blk_read_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
Simon Glassd508c822016-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 Glass8149b152022-09-17 09:00:09 -0600333 ret = get_desc(uclass_id, devnum, &desc);
Simon Glassd508c822016-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 Glassd508c822016-05-01 11:36:08 -0600340 return n;
341}
342
Simon Glass8149b152022-09-17 09:00:09 -0600343ulong blk_write_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
Simon Glassd508c822016-05-01 11:36:08 -0600344 lbaint_t blkcnt, const void *buffer)
345{
346 struct blk_desc *desc;
347 int ret;
348
Simon Glass8149b152022-09-17 09:00:09 -0600349 ret = get_desc(uclass_id, devnum, &desc);
Simon Glassd508c822016-05-01 11:36:08 -0600350 if (ret)
351 return ret;
352 return blk_dwrite(desc, start, blkcnt, buffer);
353}
354
Simon Glasscd0fb552016-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{
Weijie Gao1ce88472019-08-27 15:32:18 +0800369 return blk_select_hwpart(desc->bdev, hwpart);
Simon Glasscd0fb552016-05-01 13:52:30 -0600370}
371
Simon Glass8149b152022-09-17 09:00:09 -0600372int blk_first_device(int uclass_id, struct udevice **devp)
Simon Glass09d71aa2016-02-29 15:25:55 -0700373{
374 struct blk_desc *desc;
375 int ret;
376
Stefan Roese8a5cbc02017-11-29 16:46:42 +0100377 ret = uclass_find_first_device(UCLASS_BLK, devp);
Simon Glass09d71aa2016-02-29 15:25:55 -0700378 if (ret)
379 return ret;
380 if (!*devp)
381 return -ENODEV;
382 do {
Simon Glasscaa4daa2020-12-03 16:55:18 -0700383 desc = dev_get_uclass_plat(*devp);
Simon Glass8149b152022-09-17 09:00:09 -0600384 if (desc->uclass_id == uclass_id)
Simon Glass09d71aa2016-02-29 15:25:55 -0700385 return 0;
Stefan Roese8a5cbc02017-11-29 16:46:42 +0100386 ret = uclass_find_next_device(devp);
Simon Glass09d71aa2016-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 Glass8149b152022-09-17 09:00:09 -0600397 int ret, uclass_id;
Simon Glass09d71aa2016-02-29 15:25:55 -0700398
Simon Glasscaa4daa2020-12-03 16:55:18 -0700399 desc = dev_get_uclass_plat(*devp);
Simon Glass8149b152022-09-17 09:00:09 -0600400 uclass_id = desc->uclass_id;
Simon Glass09d71aa2016-02-29 15:25:55 -0700401 do {
Stefan Roese8a5cbc02017-11-29 16:46:42 +0100402 ret = uclass_find_next_device(devp);
Simon Glass09d71aa2016-02-29 15:25:55 -0700403 if (ret)
404 return ret;
405 if (!*devp)
406 return -ENODEV;
Simon Glasscaa4daa2020-12-03 16:55:18 -0700407 desc = dev_get_uclass_plat(*devp);
Simon Glass8149b152022-09-17 09:00:09 -0600408 if (desc->uclass_id == uclass_id)
Simon Glass09d71aa2016-02-29 15:25:55 -0700409 return 0;
410 } while (1);
411}
412
Simon Glass8149b152022-09-17 09:00:09 -0600413int blk_find_device(int uclass_id, int devnum, struct udevice **devp)
Simon Glass09d71aa2016-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 Glasscaa4daa2020-12-03 16:55:18 -0700423 struct blk_desc *desc = dev_get_uclass_plat(dev);
Simon Glass09d71aa2016-02-29 15:25:55 -0700424
Simon Glass8149b152022-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 Glass09d71aa2016-02-29 15:25:55 -0700428 *devp = dev;
Simon Glass61392812017-04-23 20:02:05 -0600429 return 0;
Simon Glass09d71aa2016-02-29 15:25:55 -0700430 }
431 }
432
433 return -ENODEV;
434}
435
Simon Glass8149b152022-09-17 09:00:09 -0600436int blk_get_device(int uclass_id, int devnum, struct udevice **devp)
Simon Glass61392812017-04-23 20:02:05 -0600437{
438 int ret;
439
Simon Glass8149b152022-09-17 09:00:09 -0600440 ret = blk_find_device(uclass_id, devnum, devp);
Simon Glass61392812017-04-23 20:02:05 -0600441 if (ret)
442 return ret;
443
444 return device_probe(*devp);
445}
446
Simon Glass09d71aa2016-02-29 15:25:55 -0700447unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
448 lbaint_t blkcnt, void *buffer)
449{
450 struct udevice *dev = block_dev->bdev;
451 const struct blk_ops *ops = blk_get_ops(dev);
Eric Nelsone40cf342016-03-28 10:05:44 -0700452 ulong blks_read;
Simon Glass09d71aa2016-02-29 15:25:55 -0700453
454 if (!ops->read)
455 return -ENOSYS;
456
Simon Glass8149b152022-09-17 09:00:09 -0600457 if (blkcache_read(block_dev->uclass_id, block_dev->devnum,
Eric Nelsone40cf342016-03-28 10:05:44 -0700458 start, blkcnt, block_dev->blksz, buffer))
459 return blkcnt;
460 blks_read = ops->read(dev, start, blkcnt, buffer);
461 if (blks_read == blkcnt)
Simon Glass8149b152022-09-17 09:00:09 -0600462 blkcache_fill(block_dev->uclass_id, block_dev->devnum,
Eric Nelsone40cf342016-03-28 10:05:44 -0700463 start, blkcnt, block_dev->blksz, buffer);
464
465 return blks_read;
Simon Glass09d71aa2016-02-29 15:25:55 -0700466}
467
468unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
469 lbaint_t blkcnt, const void *buffer)
470{
471 struct udevice *dev = block_dev->bdev;
472 const struct blk_ops *ops = blk_get_ops(dev);
473
474 if (!ops->write)
475 return -ENOSYS;
476
Simon Glass8149b152022-09-17 09:00:09 -0600477 blkcache_invalidate(block_dev->uclass_id, block_dev->devnum);
Simon Glass09d71aa2016-02-29 15:25:55 -0700478 return ops->write(dev, start, blkcnt, buffer);
479}
480
481unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
482 lbaint_t blkcnt)
483{
484 struct udevice *dev = block_dev->bdev;
485 const struct blk_ops *ops = blk_get_ops(dev);
486
487 if (!ops->erase)
488 return -ENOSYS;
489
Simon Glass8149b152022-09-17 09:00:09 -0600490 blkcache_invalidate(block_dev->uclass_id, block_dev->devnum);
Simon Glass09d71aa2016-02-29 15:25:55 -0700491 return ops->erase(dev, start, blkcnt);
492}
493
Simon Glass9f103b92017-05-27 11:37:17 -0600494int blk_get_from_parent(struct udevice *parent, struct udevice **devp)
495{
496 struct udevice *dev;
497 enum uclass_id id;
498 int ret;
499
500 device_find_first_child(parent, &dev);
501 if (!dev) {
502 debug("%s: No block device found for parent '%s'\n", __func__,
503 parent->name);
504 return -ENODEV;
505 }
506 id = device_get_uclass_id(dev);
507 if (id != UCLASS_BLK) {
508 debug("%s: Incorrect uclass %s for block device '%s'\n",
509 __func__, uclass_get_name(id), dev->name);
510 return -ENOTBLK;
511 }
512 ret = device_probe(dev);
513 if (ret)
514 return ret;
515 *devp = dev;
516
517 return 0;
518}
519
Simon Glass87571b72022-04-24 23:31:03 -0600520const char *blk_get_devtype(struct udevice *dev)
521{
522 struct udevice *parent = dev_get_parent(dev);
523
524 return uclass_get_name(device_get_uclass_id(parent));
525};
526
Simon Glass8149b152022-09-17 09:00:09 -0600527int blk_find_max_devnum(enum uclass_id uclass_id)
Simon Glass52138fd2016-05-01 11:36:28 -0600528{
529 struct udevice *dev;
530 int max_devnum = -ENODEV;
531 struct uclass *uc;
532 int ret;
533
534 ret = uclass_get(UCLASS_BLK, &uc);
535 if (ret)
536 return ret;
537 uclass_foreach_dev(dev, uc) {
Simon Glasscaa4daa2020-12-03 16:55:18 -0700538 struct blk_desc *desc = dev_get_uclass_plat(dev);
Simon Glass52138fd2016-05-01 11:36:28 -0600539
Simon Glass8149b152022-09-17 09:00:09 -0600540 if (desc->uclass_id == uclass_id && desc->devnum > max_devnum)
Simon Glass52138fd2016-05-01 11:36:28 -0600541 max_devnum = desc->devnum;
542 }
543
544 return max_devnum;
545}
546
Simon Glass8149b152022-09-17 09:00:09 -0600547int blk_next_free_devnum(enum uclass_id uclass_id)
Simon Glasse8abbb52017-04-23 20:02:06 -0600548{
549 int ret;
550
Simon Glass8149b152022-09-17 09:00:09 -0600551 ret = blk_find_max_devnum(uclass_id);
Simon Glasse8abbb52017-04-23 20:02:06 -0600552 if (ret == -ENODEV)
553 return 0;
554 if (ret < 0)
555 return ret;
556
557 return ret + 1;
558}
559
Simon Glass96f37b02021-07-05 16:32:59 -0600560static int blk_flags_check(struct udevice *dev, enum blk_flag_t req_flags)
561{
562 const struct blk_desc *desc = dev_get_uclass_plat(dev);
563 enum blk_flag_t flags;
564
565 flags = desc->removable ? BLKF_REMOVABLE : BLKF_FIXED;
566
567 return flags & req_flags ? 0 : 1;
568}
569
Simon Glass49e86682022-02-28 12:08:35 -0700570int blk_find_first(enum blk_flag_t flags, struct udevice **devp)
571{
572 int ret;
573
574 for (ret = uclass_find_first_device(UCLASS_BLK, devp);
575 *devp && !blk_flags_check(*devp, flags);
576 ret = uclass_find_next_device(devp))
577 return 0;
578
579 return -ENODEV;
580}
581
582int blk_find_next(enum blk_flag_t flags, struct udevice **devp)
583{
584 int ret;
585
586 for (ret = uclass_find_next_device(devp);
587 *devp && !blk_flags_check(*devp, flags);
588 ret = uclass_find_next_device(devp))
589 return 0;
590
591 return -ENODEV;
592}
593
Simon Glass96f37b02021-07-05 16:32:59 -0600594int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp)
595{
596 int ret;
597
598 for (ret = uclass_first_device_err(UCLASS_BLK, 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_next_device_err(enum blk_flag_t flags, struct udevice **devp)
609{
610 int ret;
611
612 for (ret = uclass_next_device_err(devp);
613 !ret;
614 ret = uclass_next_device_err(devp)) {
615 if (!blk_flags_check(*devp, flags))
616 return 0;
617 }
618
619 return -ENODEV;
620}
621
622int blk_count_devices(enum blk_flag_t flag)
623{
624 struct udevice *dev;
625 int count = 0;
626
627 blk_foreach_probe(flag, dev)
628 count++;
629
630 return count;
631}
632
Simon Glass8149b152022-09-17 09:00:09 -0600633static int blk_claim_devnum(enum uclass_id uclass_id, int devnum)
Simon Glasse48eeb92017-04-23 20:02:07 -0600634{
635 struct udevice *dev;
636 struct uclass *uc;
637 int ret;
638
639 ret = uclass_get(UCLASS_BLK, &uc);
640 if (ret)
641 return ret;
642 uclass_foreach_dev(dev, uc) {
Simon Glasscaa4daa2020-12-03 16:55:18 -0700643 struct blk_desc *desc = dev_get_uclass_plat(dev);
Simon Glasse48eeb92017-04-23 20:02:07 -0600644
Simon Glass8149b152022-09-17 09:00:09 -0600645 if (desc->uclass_id == uclass_id && desc->devnum == devnum) {
646 int next = blk_next_free_devnum(uclass_id);
Simon Glasse48eeb92017-04-23 20:02:07 -0600647
648 if (next < 0)
649 return next;
650 desc->devnum = next;
651 return 0;
652 }
653 }
654
655 return -ENOENT;
656}
657
Simon Glass09d71aa2016-02-29 15:25:55 -0700658int blk_create_device(struct udevice *parent, const char *drv_name,
Simon Glass8149b152022-09-17 09:00:09 -0600659 const char *name, int uclass_id, int devnum, int blksz,
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200660 lbaint_t lba, struct udevice **devp)
Simon Glass09d71aa2016-02-29 15:25:55 -0700661{
662 struct blk_desc *desc;
663 struct udevice *dev;
664 int ret;
665
Simon Glass52138fd2016-05-01 11:36:28 -0600666 if (devnum == -1) {
Simon Glass8149b152022-09-17 09:00:09 -0600667 devnum = blk_next_free_devnum(uclass_id);
Simon Glasse48eeb92017-04-23 20:02:07 -0600668 } else {
Simon Glass8149b152022-09-17 09:00:09 -0600669 ret = blk_claim_devnum(uclass_id, devnum);
Simon Glasse48eeb92017-04-23 20:02:07 -0600670 if (ret < 0 && ret != -ENOENT)
Simon Glass52138fd2016-05-01 11:36:28 -0600671 return ret;
Simon Glass52138fd2016-05-01 11:36:28 -0600672 }
Simon Glasse48eeb92017-04-23 20:02:07 -0600673 if (devnum < 0)
674 return devnum;
Simon Glass72a85c02016-05-01 13:52:22 -0600675 ret = device_bind_driver(parent, drv_name, name, &dev);
676 if (ret)
677 return ret;
Simon Glasscaa4daa2020-12-03 16:55:18 -0700678 desc = dev_get_uclass_plat(dev);
Simon Glass8149b152022-09-17 09:00:09 -0600679 desc->uclass_id = uclass_id;
Simon Glass72a85c02016-05-01 13:52:22 -0600680 desc->blksz = blksz;
Heinrich Schuchardtee504142019-10-25 12:15:31 +0200681 desc->log2blksz = LOG2(desc->blksz);
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200682 desc->lba = lba;
Simon Glass72a85c02016-05-01 13:52:22 -0600683 desc->part_type = PART_TYPE_UNKNOWN;
684 desc->bdev = dev;
Simon Glass09d71aa2016-02-29 15:25:55 -0700685 desc->devnum = devnum;
686 *devp = dev;
687
688 return 0;
689}
690
Simon Glass9107c972016-05-01 11:36:29 -0600691int blk_create_devicef(struct udevice *parent, const char *drv_name,
Simon Glass8149b152022-09-17 09:00:09 -0600692 const char *name, int uclass_id, int devnum, int blksz,
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200693 lbaint_t lba, struct udevice **devp)
Simon Glass9107c972016-05-01 11:36:29 -0600694{
695 char dev_name[30], *str;
Simon Glassd0773522016-05-01 13:52:24 -0600696 int ret;
Simon Glass9107c972016-05-01 11:36:29 -0600697
698 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
699 str = strdup(dev_name);
700 if (!str)
701 return -ENOMEM;
702
Simon Glass8149b152022-09-17 09:00:09 -0600703 ret = blk_create_device(parent, drv_name, str, uclass_id, devnum,
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200704 blksz, lba, devp);
Simon Glassd0773522016-05-01 13:52:24 -0600705 if (ret) {
706 free(str);
707 return ret;
708 }
709 device_set_name_alloced(*devp);
710
Simon Glass7074b2a2017-07-29 11:34:59 -0600711 return 0;
Simon Glass9107c972016-05-01 11:36:29 -0600712}
713
AKASHI Takahiro19b241c2021-12-10 15:49:29 +0900714int blk_probe_or_unbind(struct udevice *dev)
715{
716 int ret;
717
718 ret = device_probe(dev);
719 if (ret) {
720 log_debug("probing %s failed\n", dev->name);
721 device_unbind(dev);
722 }
723
724 return ret;
725}
726
Simon Glass8149b152022-09-17 09:00:09 -0600727int blk_unbind_all(int uclass_id)
Simon Glass09d71aa2016-02-29 15:25:55 -0700728{
729 struct uclass *uc;
730 struct udevice *dev, *next;
731 int ret;
732
733 ret = uclass_get(UCLASS_BLK, &uc);
734 if (ret)
735 return ret;
736 uclass_foreach_dev_safe(dev, next, uc) {
Simon Glasscaa4daa2020-12-03 16:55:18 -0700737 struct blk_desc *desc = dev_get_uclass_plat(dev);
Simon Glass09d71aa2016-02-29 15:25:55 -0700738
Simon Glass8149b152022-09-17 09:00:09 -0600739 if (desc->uclass_id == uclass_id) {
Stefan Roese706865a2017-03-20 12:51:48 +0100740 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glass09d71aa2016-02-29 15:25:55 -0700741 if (ret)
742 return ret;
743 ret = device_unbind(dev);
744 if (ret)
745 return ret;
746 }
747 }
748
749 return 0;
750}
751
Bin Mengd0851c82018-10-15 02:21:07 -0700752static int blk_post_probe(struct udevice *dev)
753{
Simon Glassa51eb8d2022-08-11 19:34:45 -0600754 if (CONFIG_IS_ENABLED(PARTITIONS) && blk_enabled()) {
Simon Glasscaa4daa2020-12-03 16:55:18 -0700755 struct blk_desc *desc = dev_get_uclass_plat(dev);
Bin Mengd0851c82018-10-15 02:21:07 -0700756
Ovidiu Panait3a4b52a2020-07-24 14:12:21 +0300757 part_init(desc);
AKASHI Takahirobf760312022-04-19 10:05:10 +0900758
759 if (desc->part_type != PART_TYPE_UNKNOWN &&
760 part_create_block_devices(dev))
761 debug("*** creating partitions failed\n");
Ovidiu Panait3a4b52a2020-07-24 14:12:21 +0300762 }
Bin Mengd0851c82018-10-15 02:21:07 -0700763
764 return 0;
765}
766
Simon Glass09d71aa2016-02-29 15:25:55 -0700767UCLASS_DRIVER(blk) = {
768 .id = UCLASS_BLK,
769 .name = "blk",
Bin Mengd0851c82018-10-15 02:21:07 -0700770 .post_probe = blk_post_probe,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700771 .per_device_plat_auto = sizeof(struct blk_desc),
Simon Glass09d71aa2016-02-29 15:25:55 -0700772};