Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_BLK |
| 8 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 9 | #include <blk.h> |
| 10 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 13 | #include <part.h> |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 14 | #include <dm/device-internal.h> |
| 15 | #include <dm/lists.h> |
Stefan Roese | ef58a90 | 2017-11-29 16:46:42 +0100 | [diff] [blame] | 16 | #include <dm/uclass-internal.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 17 | #include <linux/err.h> |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 18 | |
Marek Vasut | ddfa088 | 2023-08-14 01:46:48 +0200 | [diff] [blame] | 19 | #define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops) |
| 20 | |
Simon Glass | 95e6e03 | 2022-08-11 19:34:57 -0600 | [diff] [blame] | 21 | static struct { |
| 22 | enum uclass_id id; |
| 23 | const char *name; |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 24 | } uclass_idname_str[] = { |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 25 | { UCLASS_IDE, "ide" }, |
| 26 | { UCLASS_SCSI, "scsi" }, |
| 27 | { UCLASS_USB, "usb" }, |
| 28 | { UCLASS_MMC, "mmc" }, |
| 29 | { UCLASS_AHCI, "sata" }, |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 30 | { UCLASS_HOST, "host" }, |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 31 | { UCLASS_NVME, "nvme" }, |
Abdellatif El Khlifi | 857360c | 2023-04-17 10:11:52 +0100 | [diff] [blame] | 32 | { UCLASS_NVMXIP, "nvmxip" }, |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 33 | { UCLASS_EFI_MEDIA, "efi" }, |
| 34 | { UCLASS_EFI_LOADER, "efiloader" }, |
| 35 | { UCLASS_VIRTIO, "virtio" }, |
| 36 | { UCLASS_PVBLOCK, "pvblock" }, |
Tobias Waldekranz | 4f76dd3 | 2023-02-16 16:33:49 +0100 | [diff] [blame] | 37 | { UCLASS_BLKMAP, "blkmap" }, |
Johan Jonker | 7a657e0 | 2023-10-18 16:00:40 +0200 | [diff] [blame] | 38 | { UCLASS_RKMTD, "rkmtd" }, |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 39 | }; |
| 40 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 41 | static enum uclass_id uclass_name_to_iftype(const char *uclass_idname) |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 42 | { |
| 43 | int i; |
| 44 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 45 | for (i = 0; i < ARRAY_SIZE(uclass_idname_str); i++) { |
| 46 | if (!strcmp(uclass_idname, uclass_idname_str[i].name)) |
| 47 | return uclass_idname_str[i].id; |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 48 | } |
| 49 | |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 50 | return UCLASS_INVALID; |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 51 | } |
| 52 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 53 | static enum uclass_id conv_uclass_id(enum uclass_id uclass_id) |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 54 | { |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 55 | /* |
| 56 | * This strange adjustment is used because we use UCLASS_MASS_STORAGE |
| 57 | * for USB storage devices, so need to return this as the uclass to |
| 58 | * use for USB. In fact USB_UCLASS is for USB controllers, not |
| 59 | * peripherals. |
| 60 | * |
| 61 | * The name of the UCLASS_MASS_STORAGE uclass driver is |
| 62 | * "usb_mass_storage", but we want to use "usb" in things like the |
| 63 | * 'part list' command and when showing interfaces. |
| 64 | * |
| 65 | * So for now we have this one-way conversion. |
| 66 | * |
| 67 | * The fix for this is possibly to: |
| 68 | * - rename UCLASS_MASS_STORAGE name to "usb" |
| 69 | * - rename UCLASS_USB name to "usb_ctlr" |
| 70 | * - use UCLASS_MASS_STORAGE instead of UCLASS_USB in if_typename_str |
| 71 | */ |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 72 | if (uclass_id == UCLASS_USB) |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 73 | return UCLASS_MASS_STORAGE; |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 74 | return uclass_id; |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 75 | } |
| 76 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 77 | const char *blk_get_uclass_name(enum uclass_id uclass_id) |
Simon Glass | 85af5a4 | 2017-07-29 11:34:53 -0600 | [diff] [blame] | 78 | { |
Simon Glass | 95e6e03 | 2022-08-11 19:34:57 -0600 | [diff] [blame] | 79 | int i; |
| 80 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 81 | for (i = 0; i < ARRAY_SIZE(uclass_idname_str); i++) { |
| 82 | if (uclass_idname_str[i].id == uclass_id) |
| 83 | return uclass_idname_str[i].name; |
Simon Glass | 95e6e03 | 2022-08-11 19:34:57 -0600 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | return "(none)"; |
Simon Glass | 85af5a4 | 2017-07-29 11:34:53 -0600 | [diff] [blame] | 87 | } |
| 88 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 89 | struct blk_desc *blk_get_devnum_by_uclass_id(enum uclass_id uclass_id, int devnum) |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 90 | { |
| 91 | struct blk_desc *desc; |
| 92 | struct udevice *dev; |
| 93 | int ret; |
| 94 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 95 | ret = blk_get_device(uclass_id, devnum, &dev); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 96 | if (ret) |
| 97 | return NULL; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 98 | desc = dev_get_uclass_plat(dev); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 99 | |
| 100 | return desc; |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * This function is complicated with driver model. We look up the interface |
| 105 | * name in a local table. This gives us an interface type which we can match |
| 106 | * against the uclass of the block device's parent. |
| 107 | */ |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 108 | struct blk_desc *blk_get_devnum_by_uclass_idname(const char *uclass_idname, int devnum) |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 109 | { |
| 110 | enum uclass_id uclass_id; |
Simon Glass | 57b3d2e | 2022-08-11 19:35:01 -0600 | [diff] [blame] | 111 | enum uclass_id type; |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 112 | struct udevice *dev; |
| 113 | struct uclass *uc; |
| 114 | int ret; |
| 115 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 116 | type = uclass_name_to_iftype(uclass_idname); |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 117 | if (type == UCLASS_INVALID) { |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 118 | debug("%s: Unknown interface type '%s'\n", __func__, |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 119 | uclass_idname); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 120 | return NULL; |
| 121 | } |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 122 | uclass_id = conv_uclass_id(type); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 123 | if (uclass_id == UCLASS_INVALID) { |
| 124 | debug("%s: Unknown uclass for interface type'\n", |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 125 | blk_get_uclass_name(type)); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | ret = uclass_get(UCLASS_BLK, &uc); |
| 130 | if (ret) |
| 131 | return NULL; |
| 132 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 133 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 134 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 135 | debug("%s: uclass_id=%d, devnum=%d: %s, %d, %d\n", __func__, |
| 136 | type, devnum, dev->name, desc->uclass_id, desc->devnum); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 137 | if (desc->devnum != devnum) |
| 138 | continue; |
| 139 | |
| 140 | /* Find out the parent device uclass */ |
| 141 | if (device_get_uclass_id(dev->parent) != uclass_id) { |
| 142 | debug("%s: parent uclass %d, this dev %d\n", __func__, |
| 143 | device_get_uclass_id(dev->parent), uclass_id); |
| 144 | continue; |
| 145 | } |
| 146 | |
| 147 | if (device_probe(dev)) |
| 148 | return NULL; |
| 149 | |
| 150 | debug("%s: Device desc %p\n", __func__, desc); |
| 151 | return desc; |
| 152 | } |
| 153 | debug("%s: No device found\n", __func__); |
| 154 | |
| 155 | return NULL; |
| 156 | } |
| 157 | |
| 158 | /** |
Tien Fong Chee | 1037852 | 2018-07-06 16:26:36 +0800 | [diff] [blame] | 159 | * blk_get_by_device() - Get the block device descriptor for the given device |
| 160 | * @dev: Instance of a storage device |
| 161 | * |
| 162 | * Return: With block device descriptor on success , NULL if there is no such |
| 163 | * block device. |
| 164 | */ |
| 165 | struct blk_desc *blk_get_by_device(struct udevice *dev) |
| 166 | { |
Simon Glass | cfd7293 | 2019-09-25 08:55:56 -0600 | [diff] [blame] | 167 | struct udevice *child_dev; |
Tien Fong Chee | 1037852 | 2018-07-06 16:26:36 +0800 | [diff] [blame] | 168 | |
Simon Glass | cfd7293 | 2019-09-25 08:55:56 -0600 | [diff] [blame] | 169 | device_foreach_child(child_dev, dev) { |
Tien Fong Chee | 1037852 | 2018-07-06 16:26:36 +0800 | [diff] [blame] | 170 | if (device_get_uclass_id(child_dev) != UCLASS_BLK) |
| 171 | continue; |
| 172 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 173 | return dev_get_uclass_plat(child_dev); |
Tien Fong Chee | 1037852 | 2018-07-06 16:26:36 +0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | debug("%s: No block device found\n", __func__); |
| 177 | |
| 178 | return NULL; |
| 179 | } |
| 180 | |
Bin Meng | 9efc245 | 2023-09-26 16:43:41 +0800 | [diff] [blame] | 181 | int blk_get_desc(enum uclass_id uclass_id, int devnum, struct blk_desc **descp) |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 182 | { |
| 183 | bool found_more = false; |
| 184 | struct udevice *dev; |
| 185 | struct uclass *uc; |
| 186 | int ret; |
| 187 | |
| 188 | *descp = NULL; |
| 189 | ret = uclass_get(UCLASS_BLK, &uc); |
| 190 | if (ret) |
| 191 | return ret; |
| 192 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 193 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 194 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 195 | debug("%s: uclass_id=%d, devnum=%d: %s, %d, %d\n", __func__, |
| 196 | uclass_id, devnum, dev->name, desc->uclass_id, desc->devnum); |
| 197 | if (desc->uclass_id == uclass_id) { |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 198 | if (desc->devnum == devnum) { |
| 199 | ret = device_probe(dev); |
| 200 | if (ret) |
| 201 | return ret; |
| 202 | |
Michal Simek | f438b6b | 2016-11-16 17:37:42 +0100 | [diff] [blame] | 203 | *descp = desc; |
| 204 | return 0; |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 205 | } else if (desc->devnum > devnum) { |
| 206 | found_more = true; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | return found_more ? -ENOENT : -ENODEV; |
| 212 | } |
| 213 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 214 | int blk_select_hwpart_devnum(enum uclass_id uclass_id, int devnum, int hwpart) |
Simon Glass | 13c2c29 | 2016-05-01 13:52:30 -0600 | [diff] [blame] | 215 | { |
| 216 | struct udevice *dev; |
| 217 | int ret; |
| 218 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 219 | ret = blk_get_device(uclass_id, devnum, &dev); |
Simon Glass | 13c2c29 | 2016-05-01 13:52:30 -0600 | [diff] [blame] | 220 | if (ret) |
| 221 | return ret; |
| 222 | |
developer | f50d8d1 | 2019-08-27 15:32:18 +0800 | [diff] [blame] | 223 | return blk_select_hwpart(dev, hwpart); |
Simon Glass | 13c2c29 | 2016-05-01 13:52:30 -0600 | [diff] [blame] | 224 | } |
| 225 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 226 | int blk_list_part(enum uclass_id uclass_id) |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 227 | { |
| 228 | struct blk_desc *desc; |
| 229 | int devnum, ok; |
| 230 | int ret; |
| 231 | |
| 232 | for (ok = 0, devnum = 0;; ++devnum) { |
Bin Meng | 9efc245 | 2023-09-26 16:43:41 +0800 | [diff] [blame] | 233 | ret = blk_get_desc(uclass_id, devnum, &desc); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 234 | if (ret == -ENODEV) |
| 235 | break; |
| 236 | else if (ret) |
| 237 | continue; |
| 238 | if (desc->part_type != PART_TYPE_UNKNOWN) { |
| 239 | ++ok; |
| 240 | if (devnum) |
| 241 | putc('\n'); |
| 242 | part_print(desc); |
| 243 | } |
| 244 | } |
| 245 | if (!ok) |
| 246 | return -ENODEV; |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 251 | int blk_print_part_devnum(enum uclass_id uclass_id, int devnum) |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 252 | { |
| 253 | struct blk_desc *desc; |
| 254 | int ret; |
| 255 | |
Bin Meng | 9efc245 | 2023-09-26 16:43:41 +0800 | [diff] [blame] | 256 | ret = blk_get_desc(uclass_id, devnum, &desc); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 257 | if (ret) |
| 258 | return ret; |
| 259 | if (desc->type == DEV_TYPE_UNKNOWN) |
| 260 | return -ENOENT; |
| 261 | part_print(desc); |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 266 | void blk_list_devices(enum uclass_id uclass_id) |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 267 | { |
| 268 | struct blk_desc *desc; |
| 269 | int ret; |
| 270 | int i; |
| 271 | |
| 272 | for (i = 0;; ++i) { |
Bin Meng | 9efc245 | 2023-09-26 16:43:41 +0800 | [diff] [blame] | 273 | ret = blk_get_desc(uclass_id, i, &desc); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 274 | if (ret == -ENODEV) |
| 275 | break; |
| 276 | else if (ret) |
| 277 | continue; |
| 278 | if (desc->type == DEV_TYPE_UNKNOWN) |
| 279 | continue; /* list only known devices */ |
| 280 | printf("Device %d: ", i); |
| 281 | dev_print(desc); |
| 282 | } |
| 283 | } |
| 284 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 285 | int blk_print_device_num(enum uclass_id uclass_id, int devnum) |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 286 | { |
| 287 | struct blk_desc *desc; |
| 288 | int ret; |
| 289 | |
Bin Meng | 9efc245 | 2023-09-26 16:43:41 +0800 | [diff] [blame] | 290 | ret = blk_get_desc(uclass_id, devnum, &desc); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 291 | if (ret) |
| 292 | return ret; |
| 293 | printf("\nIDE device %d: ", devnum); |
| 294 | dev_print(desc); |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 299 | int blk_show_device(enum uclass_id uclass_id, int devnum) |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 300 | { |
| 301 | struct blk_desc *desc; |
| 302 | int ret; |
| 303 | |
| 304 | printf("\nDevice %d: ", devnum); |
Bin Meng | 9efc245 | 2023-09-26 16:43:41 +0800 | [diff] [blame] | 305 | ret = blk_get_desc(uclass_id, devnum, &desc); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 306 | if (ret == -ENODEV || ret == -ENOENT) { |
| 307 | printf("unknown device\n"); |
| 308 | return -ENODEV; |
| 309 | } |
| 310 | if (ret) |
| 311 | return ret; |
| 312 | dev_print(desc); |
| 313 | |
| 314 | if (desc->type == DEV_TYPE_UNKNOWN) |
| 315 | return -ENOENT; |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
Simon Glass | 13c2c29 | 2016-05-01 13:52:30 -0600 | [diff] [blame] | 320 | int blk_select_hwpart(struct udevice *dev, int hwpart) |
| 321 | { |
| 322 | const struct blk_ops *ops = blk_get_ops(dev); |
| 323 | |
| 324 | if (!ops) |
| 325 | return -ENOSYS; |
| 326 | if (!ops->select_hwpart) |
| 327 | return 0; |
| 328 | |
| 329 | return ops->select_hwpart(dev, hwpart); |
| 330 | } |
| 331 | |
| 332 | int blk_dselect_hwpart(struct blk_desc *desc, int hwpart) |
| 333 | { |
developer | f50d8d1 | 2019-08-27 15:32:18 +0800 | [diff] [blame] | 334 | return blk_select_hwpart(desc->bdev, hwpart); |
Simon Glass | 13c2c29 | 2016-05-01 13:52:30 -0600 | [diff] [blame] | 335 | } |
| 336 | |
Michal Suchanek | bad8ab6 | 2022-09-27 23:23:53 +0200 | [diff] [blame] | 337 | static int _blk_next_device(int uclass_id, struct udevice **devp) |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 338 | { |
| 339 | struct blk_desc *desc; |
Michal Suchanek | bad8ab6 | 2022-09-27 23:23:53 +0200 | [diff] [blame] | 340 | int ret = 0; |
| 341 | |
| 342 | for (; *devp; uclass_find_next_device(devp)) { |
| 343 | desc = dev_get_uclass_plat(*devp); |
| 344 | if (desc->uclass_id == uclass_id) { |
| 345 | ret = device_probe(*devp); |
| 346 | if (!ret) |
| 347 | return 0; |
| 348 | } |
| 349 | } |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 350 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 351 | if (ret) |
| 352 | return ret; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 353 | |
| 354 | return -ENODEV; |
| 355 | } |
| 356 | |
Michal Suchanek | bad8ab6 | 2022-09-27 23:23:53 +0200 | [diff] [blame] | 357 | int blk_first_device(int uclass_id, struct udevice **devp) |
| 358 | { |
| 359 | uclass_find_first_device(UCLASS_BLK, devp); |
| 360 | |
| 361 | return _blk_next_device(uclass_id, devp); |
| 362 | } |
| 363 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 364 | int blk_next_device(struct udevice **devp) |
| 365 | { |
| 366 | struct blk_desc *desc; |
Michal Suchanek | bad8ab6 | 2022-09-27 23:23:53 +0200 | [diff] [blame] | 367 | int uclass_id; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 368 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 369 | desc = dev_get_uclass_plat(*devp); |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 370 | uclass_id = desc->uclass_id; |
Michal Suchanek | bad8ab6 | 2022-09-27 23:23:53 +0200 | [diff] [blame] | 371 | uclass_find_next_device(devp); |
| 372 | |
| 373 | return _blk_next_device(uclass_id, devp); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 376 | int blk_find_device(int uclass_id, int devnum, struct udevice **devp) |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 377 | { |
| 378 | struct uclass *uc; |
| 379 | struct udevice *dev; |
| 380 | int ret; |
| 381 | |
| 382 | ret = uclass_get(UCLASS_BLK, &uc); |
| 383 | if (ret) |
| 384 | return ret; |
| 385 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 386 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 387 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 388 | debug("%s: uclass_id=%d, devnum=%d: %s, %d, %d\n", __func__, |
| 389 | uclass_id, devnum, dev->name, desc->uclass_id, desc->devnum); |
| 390 | if (desc->uclass_id == uclass_id && desc->devnum == devnum) { |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 391 | *devp = dev; |
Simon Glass | d5d4c10 | 2017-04-23 20:02:05 -0600 | [diff] [blame] | 392 | return 0; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
| 396 | return -ENODEV; |
| 397 | } |
| 398 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 399 | int blk_get_device(int uclass_id, int devnum, struct udevice **devp) |
Simon Glass | d5d4c10 | 2017-04-23 20:02:05 -0600 | [diff] [blame] | 400 | { |
| 401 | int ret; |
| 402 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 403 | ret = blk_find_device(uclass_id, devnum, devp); |
Simon Glass | d5d4c10 | 2017-04-23 20:02:05 -0600 | [diff] [blame] | 404 | if (ret) |
| 405 | return ret; |
| 406 | |
| 407 | return device_probe(*devp); |
| 408 | } |
| 409 | |
Marek Vasut | 847e24f | 2023-08-14 01:49:59 +0200 | [diff] [blame] | 410 | struct blk_bounce_buffer { |
| 411 | struct udevice *dev; |
| 412 | struct bounce_buffer state; |
| 413 | }; |
| 414 | |
| 415 | static int blk_buffer_aligned(struct bounce_buffer *state) |
| 416 | { |
| 417 | #if IS_ENABLED(CONFIG_BOUNCE_BUFFER) |
| 418 | struct blk_bounce_buffer *bbstate = |
| 419 | container_of(state, struct blk_bounce_buffer, state); |
| 420 | struct udevice *dev = bbstate->dev; |
| 421 | const struct blk_ops *ops = blk_get_ops(dev); |
| 422 | |
| 423 | if (ops->buffer_aligned) |
| 424 | return ops->buffer_aligned(dev, state); |
| 425 | #endif /* CONFIG_BOUNCE_BUFFER */ |
| 426 | |
| 427 | return 1; /* Default, any buffer is OK */ |
| 428 | } |
| 429 | |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 430 | long blk_read(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *buf) |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 431 | { |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 432 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 433 | const struct blk_ops *ops = blk_get_ops(dev); |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 434 | ulong blks_read; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 435 | |
| 436 | if (!ops->read) |
| 437 | return -ENOSYS; |
| 438 | |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 439 | if (blkcache_read(desc->uclass_id, desc->devnum, |
| 440 | start, blkcnt, desc->blksz, buf)) |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 441 | return blkcnt; |
Marek Vasut | 847e24f | 2023-08-14 01:49:59 +0200 | [diff] [blame] | 442 | |
Johan Jonker | 10a986c | 2023-10-18 16:01:10 +0200 | [diff] [blame] | 443 | if (IS_ENABLED(CONFIG_BOUNCE_BUFFER) && desc->bb) { |
Marek Vasut | 847e24f | 2023-08-14 01:49:59 +0200 | [diff] [blame] | 444 | struct blk_bounce_buffer bbstate = { .dev = dev }; |
| 445 | int ret; |
| 446 | |
| 447 | ret = bounce_buffer_start_extalign(&bbstate.state, buf, |
| 448 | blkcnt * desc->blksz, |
| 449 | GEN_BB_WRITE, desc->blksz, |
| 450 | blk_buffer_aligned); |
| 451 | if (ret) |
| 452 | return ret; |
| 453 | |
| 454 | blks_read = ops->read(dev, start, blkcnt, bbstate.state.bounce_buffer); |
| 455 | |
| 456 | bounce_buffer_stop(&bbstate.state); |
| 457 | } else { |
| 458 | blks_read = ops->read(dev, start, blkcnt, buf); |
| 459 | } |
| 460 | |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 461 | if (blks_read == blkcnt) |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 462 | blkcache_fill(desc->uclass_id, desc->devnum, start, blkcnt, |
| 463 | desc->blksz, buf); |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 464 | |
| 465 | return blks_read; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 468 | long blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, |
| 469 | const void *buf) |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 470 | { |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 471 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 472 | const struct blk_ops *ops = blk_get_ops(dev); |
Marek Vasut | 847e24f | 2023-08-14 01:49:59 +0200 | [diff] [blame] | 473 | long blks_written; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 474 | |
| 475 | if (!ops->write) |
| 476 | return -ENOSYS; |
| 477 | |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 478 | blkcache_invalidate(desc->uclass_id, desc->devnum); |
| 479 | |
Johan Jonker | 10a986c | 2023-10-18 16:01:10 +0200 | [diff] [blame] | 480 | if (IS_ENABLED(CONFIG_BOUNCE_BUFFER) && desc->bb) { |
Marek Vasut | 847e24f | 2023-08-14 01:49:59 +0200 | [diff] [blame] | 481 | struct blk_bounce_buffer bbstate = { .dev = dev }; |
| 482 | int ret; |
| 483 | |
| 484 | ret = bounce_buffer_start_extalign(&bbstate.state, (void *)buf, |
| 485 | blkcnt * desc->blksz, |
| 486 | GEN_BB_READ, desc->blksz, |
| 487 | blk_buffer_aligned); |
| 488 | if (ret) |
| 489 | return ret; |
| 490 | |
| 491 | blks_written = ops->write(dev, start, blkcnt, |
| 492 | bbstate.state.bounce_buffer); |
| 493 | |
| 494 | bounce_buffer_stop(&bbstate.state); |
| 495 | } else { |
| 496 | blks_written = ops->write(dev, start, blkcnt, buf); |
| 497 | } |
| 498 | |
| 499 | return blks_written; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 502 | long blk_erase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt) |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 503 | { |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 504 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 505 | const struct blk_ops *ops = blk_get_ops(dev); |
| 506 | |
| 507 | if (!ops->erase) |
| 508 | return -ENOSYS; |
| 509 | |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 510 | blkcache_invalidate(desc->uclass_id, desc->devnum); |
| 511 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 512 | return ops->erase(dev, start, blkcnt); |
| 513 | } |
| 514 | |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 515 | ulong blk_dread(struct blk_desc *desc, lbaint_t start, lbaint_t blkcnt, |
| 516 | void *buffer) |
| 517 | { |
| 518 | return blk_read(desc->bdev, start, blkcnt, buffer); |
| 519 | } |
| 520 | |
| 521 | ulong blk_dwrite(struct blk_desc *desc, lbaint_t start, lbaint_t blkcnt, |
| 522 | const void *buffer) |
| 523 | { |
| 524 | return blk_write(desc->bdev, start, blkcnt, buffer); |
| 525 | } |
| 526 | |
| 527 | ulong blk_derase(struct blk_desc *desc, lbaint_t start, lbaint_t blkcnt) |
| 528 | { |
| 529 | return blk_erase(desc->bdev, start, blkcnt); |
| 530 | } |
| 531 | |
Simon Glass | c0bcaaf | 2022-10-29 19:47:14 -0600 | [diff] [blame] | 532 | int blk_find_from_parent(struct udevice *parent, struct udevice **devp) |
Simon Glass | 4f26913 | 2017-05-27 11:37:17 -0600 | [diff] [blame] | 533 | { |
| 534 | struct udevice *dev; |
Simon Glass | 4f26913 | 2017-05-27 11:37:17 -0600 | [diff] [blame] | 535 | |
Simon Glass | c0bcaaf | 2022-10-29 19:47:14 -0600 | [diff] [blame] | 536 | if (device_find_first_child_by_uclass(parent, UCLASS_BLK, &dev)) { |
Simon Glass | 4f26913 | 2017-05-27 11:37:17 -0600 | [diff] [blame] | 537 | debug("%s: No block device found for parent '%s'\n", __func__, |
| 538 | parent->name); |
| 539 | return -ENODEV; |
| 540 | } |
Simon Glass | c0bcaaf | 2022-10-29 19:47:14 -0600 | [diff] [blame] | 541 | *devp = dev; |
| 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | int blk_get_from_parent(struct udevice *parent, struct udevice **devp) |
| 547 | { |
| 548 | struct udevice *dev; |
| 549 | int ret; |
| 550 | |
| 551 | ret = blk_find_from_parent(parent, &dev); |
| 552 | if (ret) |
| 553 | return ret; |
Simon Glass | 4f26913 | 2017-05-27 11:37:17 -0600 | [diff] [blame] | 554 | ret = device_probe(dev); |
| 555 | if (ret) |
| 556 | return ret; |
| 557 | *devp = dev; |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
Simon Glass | f3086cf | 2022-04-24 23:31:03 -0600 | [diff] [blame] | 562 | const char *blk_get_devtype(struct udevice *dev) |
| 563 | { |
| 564 | struct udevice *parent = dev_get_parent(dev); |
| 565 | |
| 566 | return uclass_get_name(device_get_uclass_id(parent)); |
| 567 | }; |
| 568 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 569 | int blk_find_max_devnum(enum uclass_id uclass_id) |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 570 | { |
| 571 | struct udevice *dev; |
| 572 | int max_devnum = -ENODEV; |
| 573 | struct uclass *uc; |
| 574 | int ret; |
| 575 | |
| 576 | ret = uclass_get(UCLASS_BLK, &uc); |
| 577 | if (ret) |
| 578 | return ret; |
| 579 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 580 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 581 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 582 | if (desc->uclass_id == uclass_id && desc->devnum > max_devnum) |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 583 | max_devnum = desc->devnum; |
| 584 | } |
| 585 | |
| 586 | return max_devnum; |
| 587 | } |
| 588 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 589 | int blk_next_free_devnum(enum uclass_id uclass_id) |
Simon Glass | dbc3861 | 2017-04-23 20:02:06 -0600 | [diff] [blame] | 590 | { |
| 591 | int ret; |
| 592 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 593 | ret = blk_find_max_devnum(uclass_id); |
Simon Glass | dbc3861 | 2017-04-23 20:02:06 -0600 | [diff] [blame] | 594 | if (ret == -ENODEV) |
| 595 | return 0; |
| 596 | if (ret < 0) |
| 597 | return ret; |
| 598 | |
| 599 | return ret + 1; |
| 600 | } |
| 601 | |
Simon Glass | fc7a744 | 2021-07-05 16:32:59 -0600 | [diff] [blame] | 602 | static int blk_flags_check(struct udevice *dev, enum blk_flag_t req_flags) |
| 603 | { |
| 604 | const struct blk_desc *desc = dev_get_uclass_plat(dev); |
| 605 | enum blk_flag_t flags; |
| 606 | |
| 607 | flags = desc->removable ? BLKF_REMOVABLE : BLKF_FIXED; |
| 608 | |
| 609 | return flags & req_flags ? 0 : 1; |
| 610 | } |
| 611 | |
Simon Glass | 8e61f93 | 2022-02-28 12:08:35 -0700 | [diff] [blame] | 612 | int blk_find_first(enum blk_flag_t flags, struct udevice **devp) |
| 613 | { |
| 614 | int ret; |
| 615 | |
| 616 | for (ret = uclass_find_first_device(UCLASS_BLK, devp); |
| 617 | *devp && !blk_flags_check(*devp, flags); |
| 618 | ret = uclass_find_next_device(devp)) |
| 619 | return 0; |
| 620 | |
| 621 | return -ENODEV; |
| 622 | } |
| 623 | |
| 624 | int blk_find_next(enum blk_flag_t flags, struct udevice **devp) |
| 625 | { |
| 626 | int ret; |
| 627 | |
| 628 | for (ret = uclass_find_next_device(devp); |
| 629 | *devp && !blk_flags_check(*devp, flags); |
| 630 | ret = uclass_find_next_device(devp)) |
| 631 | return 0; |
| 632 | |
| 633 | return -ENODEV; |
| 634 | } |
| 635 | |
Simon Glass | fc7a744 | 2021-07-05 16:32:59 -0600 | [diff] [blame] | 636 | int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp) |
| 637 | { |
Michal Suchanek | ac9e9fc | 2022-10-12 21:58:01 +0200 | [diff] [blame] | 638 | for (uclass_first_device(UCLASS_BLK, devp); |
| 639 | *devp; |
| 640 | uclass_next_device(devp)) { |
Simon Glass | fc7a744 | 2021-07-05 16:32:59 -0600 | [diff] [blame] | 641 | if (!blk_flags_check(*devp, flags)) |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | return -ENODEV; |
| 646 | } |
| 647 | |
| 648 | int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp) |
| 649 | { |
Michal Suchanek | ac9e9fc | 2022-10-12 21:58:01 +0200 | [diff] [blame] | 650 | for (uclass_next_device(devp); |
| 651 | *devp; |
| 652 | uclass_next_device(devp)) { |
Simon Glass | fc7a744 | 2021-07-05 16:32:59 -0600 | [diff] [blame] | 653 | if (!blk_flags_check(*devp, flags)) |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | return -ENODEV; |
| 658 | } |
| 659 | |
| 660 | int blk_count_devices(enum blk_flag_t flag) |
| 661 | { |
| 662 | struct udevice *dev; |
| 663 | int count = 0; |
| 664 | |
| 665 | blk_foreach_probe(flag, dev) |
| 666 | count++; |
| 667 | |
| 668 | return count; |
| 669 | } |
| 670 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 671 | static int blk_claim_devnum(enum uclass_id uclass_id, int devnum) |
Simon Glass | e4fef74 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 672 | { |
| 673 | struct udevice *dev; |
| 674 | struct uclass *uc; |
| 675 | int ret; |
| 676 | |
| 677 | ret = uclass_get(UCLASS_BLK, &uc); |
| 678 | if (ret) |
| 679 | return ret; |
| 680 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 681 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | e4fef74 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 682 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 683 | if (desc->uclass_id == uclass_id && desc->devnum == devnum) { |
| 684 | int next = blk_next_free_devnum(uclass_id); |
Simon Glass | e4fef74 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 685 | |
| 686 | if (next < 0) |
| 687 | return next; |
| 688 | desc->devnum = next; |
| 689 | return 0; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | return -ENOENT; |
| 694 | } |
| 695 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 696 | int blk_create_device(struct udevice *parent, const char *drv_name, |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 697 | const char *name, int uclass_id, int devnum, int blksz, |
Jean-Jacques Hiblot | 99b324a | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 698 | lbaint_t lba, struct udevice **devp) |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 699 | { |
| 700 | struct blk_desc *desc; |
| 701 | struct udevice *dev; |
| 702 | int ret; |
| 703 | |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 704 | if (devnum == -1) { |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 705 | devnum = blk_next_free_devnum(uclass_id); |
Simon Glass | e4fef74 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 706 | } else { |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 707 | ret = blk_claim_devnum(uclass_id, devnum); |
Simon Glass | e4fef74 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 708 | if (ret < 0 && ret != -ENOENT) |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 709 | return ret; |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 710 | } |
Simon Glass | e4fef74 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 711 | if (devnum < 0) |
| 712 | return devnum; |
Simon Glass | 77f7fb8 | 2016-05-01 13:52:22 -0600 | [diff] [blame] | 713 | ret = device_bind_driver(parent, drv_name, name, &dev); |
| 714 | if (ret) |
| 715 | return ret; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 716 | desc = dev_get_uclass_plat(dev); |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 717 | desc->uclass_id = uclass_id; |
Simon Glass | 77f7fb8 | 2016-05-01 13:52:22 -0600 | [diff] [blame] | 718 | desc->blksz = blksz; |
Heinrich Schuchardt | 09ba4ea | 2019-10-25 12:15:31 +0200 | [diff] [blame] | 719 | desc->log2blksz = LOG2(desc->blksz); |
Jean-Jacques Hiblot | 99b324a | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 720 | desc->lba = lba; |
Simon Glass | 77f7fb8 | 2016-05-01 13:52:22 -0600 | [diff] [blame] | 721 | desc->part_type = PART_TYPE_UNKNOWN; |
| 722 | desc->bdev = dev; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 723 | desc->devnum = devnum; |
| 724 | *devp = dev; |
| 725 | |
| 726 | return 0; |
| 727 | } |
| 728 | |
Simon Glass | 966b695 | 2016-05-01 11:36:29 -0600 | [diff] [blame] | 729 | int blk_create_devicef(struct udevice *parent, const char *drv_name, |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 730 | const char *name, int uclass_id, int devnum, int blksz, |
Jean-Jacques Hiblot | 99b324a | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 731 | lbaint_t lba, struct udevice **devp) |
Simon Glass | 966b695 | 2016-05-01 11:36:29 -0600 | [diff] [blame] | 732 | { |
| 733 | char dev_name[30], *str; |
Simon Glass | 39e5492 | 2016-05-01 13:52:24 -0600 | [diff] [blame] | 734 | int ret; |
Simon Glass | 966b695 | 2016-05-01 11:36:29 -0600 | [diff] [blame] | 735 | |
| 736 | snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name); |
| 737 | str = strdup(dev_name); |
| 738 | if (!str) |
| 739 | return -ENOMEM; |
| 740 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 741 | ret = blk_create_device(parent, drv_name, str, uclass_id, devnum, |
Jean-Jacques Hiblot | 99b324a | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 742 | blksz, lba, devp); |
Simon Glass | 39e5492 | 2016-05-01 13:52:24 -0600 | [diff] [blame] | 743 | if (ret) { |
| 744 | free(str); |
| 745 | return ret; |
| 746 | } |
| 747 | device_set_name_alloced(*devp); |
| 748 | |
Simon Glass | 4450597 | 2017-07-29 11:34:59 -0600 | [diff] [blame] | 749 | return 0; |
Simon Glass | 966b695 | 2016-05-01 11:36:29 -0600 | [diff] [blame] | 750 | } |
| 751 | |
AKASHI Takahiro | 3e32dbe | 2021-12-10 15:49:29 +0900 | [diff] [blame] | 752 | int blk_probe_or_unbind(struct udevice *dev) |
| 753 | { |
| 754 | int ret; |
| 755 | |
| 756 | ret = device_probe(dev); |
| 757 | if (ret) { |
| 758 | log_debug("probing %s failed\n", dev->name); |
| 759 | device_unbind(dev); |
| 760 | } |
| 761 | |
| 762 | return ret; |
| 763 | } |
| 764 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 765 | int blk_unbind_all(int uclass_id) |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 766 | { |
| 767 | struct uclass *uc; |
| 768 | struct udevice *dev, *next; |
| 769 | int ret; |
| 770 | |
| 771 | ret = uclass_get(UCLASS_BLK, &uc); |
| 772 | if (ret) |
| 773 | return ret; |
| 774 | uclass_foreach_dev_safe(dev, next, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 775 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 776 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 777 | if (desc->uclass_id == uclass_id) { |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 778 | ret = device_remove(dev, DM_REMOVE_NORMAL); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 779 | if (ret) |
| 780 | return ret; |
| 781 | ret = device_unbind(dev); |
| 782 | if (ret) |
| 783 | return ret; |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | return 0; |
| 788 | } |
| 789 | |
Marek Vasut | 14fafef | 2023-08-14 01:46:47 +0200 | [diff] [blame] | 790 | static int part_create_block_devices(struct udevice *blk_dev) |
| 791 | { |
| 792 | int part, count; |
| 793 | struct blk_desc *desc = dev_get_uclass_plat(blk_dev); |
| 794 | struct disk_partition info; |
| 795 | struct disk_part *part_data; |
| 796 | char devname[32]; |
| 797 | struct udevice *dev; |
| 798 | int ret; |
| 799 | |
| 800 | if (!CONFIG_IS_ENABLED(PARTITIONS) || !blk_enabled()) |
| 801 | return 0; |
| 802 | |
| 803 | if (device_get_uclass_id(blk_dev) != UCLASS_BLK) |
| 804 | return 0; |
| 805 | |
| 806 | /* Add devices for each partition */ |
| 807 | for (count = 0, part = 1; part <= MAX_SEARCH_PARTITIONS; part++) { |
| 808 | if (part_get_info(desc, part, &info)) |
| 809 | continue; |
| 810 | snprintf(devname, sizeof(devname), "%s:%d", blk_dev->name, |
| 811 | part); |
| 812 | |
| 813 | ret = device_bind_driver(blk_dev, "blk_partition", |
| 814 | strdup(devname), &dev); |
| 815 | if (ret) |
| 816 | return ret; |
| 817 | |
| 818 | part_data = dev_get_uclass_plat(dev); |
| 819 | part_data->partnum = part; |
| 820 | part_data->gpt_part_info = info; |
| 821 | count++; |
| 822 | |
| 823 | ret = device_probe(dev); |
| 824 | if (ret) { |
| 825 | debug("Can't probe\n"); |
| 826 | count--; |
| 827 | device_unbind(dev); |
| 828 | |
| 829 | continue; |
| 830 | } |
| 831 | } |
| 832 | debug("%s: %d partitions found in %s\n", __func__, count, |
| 833 | blk_dev->name); |
| 834 | |
| 835 | return 0; |
| 836 | } |
| 837 | |
Bin Meng | cbc3da8 | 2018-10-15 02:21:07 -0700 | [diff] [blame] | 838 | static int blk_post_probe(struct udevice *dev) |
| 839 | { |
Simon Glass | f5ac303 | 2022-08-11 19:34:45 -0600 | [diff] [blame] | 840 | if (CONFIG_IS_ENABLED(PARTITIONS) && blk_enabled()) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 841 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Bin Meng | cbc3da8 | 2018-10-15 02:21:07 -0700 | [diff] [blame] | 842 | |
Ovidiu Panait | 76160a0 | 2020-07-24 14:12:21 +0300 | [diff] [blame] | 843 | part_init(desc); |
AKASHI Takahiro | 358e6c7 | 2022-04-19 10:05:10 +0900 | [diff] [blame] | 844 | |
| 845 | if (desc->part_type != PART_TYPE_UNKNOWN && |
| 846 | part_create_block_devices(dev)) |
| 847 | debug("*** creating partitions failed\n"); |
Ovidiu Panait | 76160a0 | 2020-07-24 14:12:21 +0300 | [diff] [blame] | 848 | } |
Bin Meng | cbc3da8 | 2018-10-15 02:21:07 -0700 | [diff] [blame] | 849 | |
| 850 | return 0; |
| 851 | } |
| 852 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 853 | UCLASS_DRIVER(blk) = { |
| 854 | .id = UCLASS_BLK, |
| 855 | .name = "blk", |
Bin Meng | cbc3da8 | 2018-10-15 02:21:07 -0700 | [diff] [blame] | 856 | .post_probe = blk_post_probe, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 857 | .per_device_plat_auto = sizeof(struct blk_desc), |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 858 | }; |