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 <common.h> |
| 10 | #include <blk.h> |
| 11 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 14 | #include <part.h> |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 15 | #include <dm/device-internal.h> |
| 16 | #include <dm/lists.h> |
Stefan Roese | ef58a90 | 2017-11-29 16:46:42 +0100 | [diff] [blame] | 17 | #include <dm/uclass-internal.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 18 | #include <linux/err.h> |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 19 | |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 20 | static const char *if_typename_str[IF_TYPE_COUNT] = { |
| 21 | [IF_TYPE_IDE] = "ide", |
| 22 | [IF_TYPE_SCSI] = "scsi", |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 23 | [IF_TYPE_USB] = "usb", |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 24 | [IF_TYPE_MMC] = "mmc", |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 25 | [IF_TYPE_SATA] = "sata", |
| 26 | [IF_TYPE_HOST] = "host", |
Zhikang Zhang | 182fccd | 2017-08-03 02:30:56 -0700 | [diff] [blame] | 27 | [IF_TYPE_NVME] = "nvme", |
Simon Glass | 507ab96 | 2021-12-04 08:56:31 -0700 | [diff] [blame] | 28 | [IF_TYPE_EFI_MEDIA] = "efi", |
Simon Glass | 15c4d67 | 2021-12-04 08:56:30 -0700 | [diff] [blame] | 29 | [IF_TYPE_EFI_LOADER] = "efiloader", |
Tuomas Tynkkynen | d458006 | 2018-10-15 02:21:10 -0700 | [diff] [blame] | 30 | [IF_TYPE_VIRTIO] = "virtio", |
Anastasiia Lukianenko | 4fec7f8 | 2020-08-06 12:42:55 +0300 | [diff] [blame] | 31 | [IF_TYPE_PVBLOCK] = "pvblock", |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = { |
Bin Meng | b650afa | 2017-09-10 05:12:51 -0700 | [diff] [blame] | 35 | [IF_TYPE_IDE] = UCLASS_IDE, |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 36 | [IF_TYPE_SCSI] = UCLASS_SCSI, |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 37 | [IF_TYPE_USB] = UCLASS_MASS_STORAGE, |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 38 | [IF_TYPE_MMC] = UCLASS_MMC, |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 39 | [IF_TYPE_SATA] = UCLASS_AHCI, |
| 40 | [IF_TYPE_HOST] = UCLASS_ROOT, |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 41 | [IF_TYPE_NVME] = UCLASS_NVME, |
Simon Glass | 507ab96 | 2021-12-04 08:56:31 -0700 | [diff] [blame] | 42 | [IF_TYPE_EFI_MEDIA] = UCLASS_EFI_MEDIA, |
Simon Glass | 15c4d67 | 2021-12-04 08:56:30 -0700 | [diff] [blame] | 43 | [IF_TYPE_EFI_LOADER] = UCLASS_EFI_LOADER, |
Tuomas Tynkkynen | d458006 | 2018-10-15 02:21:10 -0700 | [diff] [blame] | 44 | [IF_TYPE_VIRTIO] = UCLASS_VIRTIO, |
Anastasiia Lukianenko | 4fec7f8 | 2020-08-06 12:42:55 +0300 | [diff] [blame] | 45 | [IF_TYPE_PVBLOCK] = UCLASS_PVBLOCK, |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | static enum if_type if_typename_to_iftype(const char *if_typename) |
| 49 | { |
| 50 | int i; |
| 51 | |
| 52 | for (i = 0; i < IF_TYPE_COUNT; i++) { |
| 53 | if (if_typename_str[i] && |
| 54 | !strcmp(if_typename, if_typename_str[i])) |
| 55 | return i; |
| 56 | } |
| 57 | |
| 58 | return IF_TYPE_UNKNOWN; |
| 59 | } |
| 60 | |
| 61 | static enum uclass_id if_type_to_uclass_id(enum if_type if_type) |
| 62 | { |
| 63 | return if_type_uclass_id[if_type]; |
| 64 | } |
| 65 | |
Simon Glass | 85af5a4 | 2017-07-29 11:34:53 -0600 | [diff] [blame] | 66 | const char *blk_get_if_type_name(enum if_type if_type) |
| 67 | { |
| 68 | return if_typename_str[if_type]; |
| 69 | } |
| 70 | |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 71 | struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum) |
| 72 | { |
| 73 | struct blk_desc *desc; |
| 74 | struct udevice *dev; |
| 75 | int ret; |
| 76 | |
| 77 | ret = blk_get_device(if_type, devnum, &dev); |
| 78 | if (ret) |
| 79 | return NULL; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 80 | desc = dev_get_uclass_plat(dev); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 81 | |
| 82 | return desc; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * This function is complicated with driver model. We look up the interface |
| 87 | * name in a local table. This gives us an interface type which we can match |
| 88 | * against the uclass of the block device's parent. |
| 89 | */ |
| 90 | struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum) |
| 91 | { |
| 92 | enum uclass_id uclass_id; |
Simon Glass | b00361a | 2022-08-11 19:34:56 -0600 | [diff] [blame^] | 93 | enum if_type type; |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 94 | struct udevice *dev; |
| 95 | struct uclass *uc; |
| 96 | int ret; |
| 97 | |
Simon Glass | b00361a | 2022-08-11 19:34:56 -0600 | [diff] [blame^] | 98 | type = if_typename_to_iftype(if_typename); |
| 99 | if (type == IF_TYPE_UNKNOWN) { |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 100 | debug("%s: Unknown interface type '%s'\n", __func__, |
| 101 | if_typename); |
| 102 | return NULL; |
| 103 | } |
Simon Glass | b00361a | 2022-08-11 19:34:56 -0600 | [diff] [blame^] | 104 | uclass_id = if_type_to_uclass_id(type); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 105 | if (uclass_id == UCLASS_INVALID) { |
| 106 | debug("%s: Unknown uclass for interface type'\n", |
Simon Glass | b00361a | 2022-08-11 19:34:56 -0600 | [diff] [blame^] | 107 | if_typename_str[type]); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 108 | return NULL; |
| 109 | } |
| 110 | |
| 111 | ret = uclass_get(UCLASS_BLK, &uc); |
| 112 | if (ret) |
| 113 | return NULL; |
| 114 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 115 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 116 | |
| 117 | debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__, |
Simon Glass | b00361a | 2022-08-11 19:34:56 -0600 | [diff] [blame^] | 118 | type, devnum, dev->name, desc->if_type, desc->devnum); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 119 | if (desc->devnum != devnum) |
| 120 | continue; |
| 121 | |
| 122 | /* Find out the parent device uclass */ |
| 123 | if (device_get_uclass_id(dev->parent) != uclass_id) { |
| 124 | debug("%s: parent uclass %d, this dev %d\n", __func__, |
| 125 | device_get_uclass_id(dev->parent), uclass_id); |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | if (device_probe(dev)) |
| 130 | return NULL; |
| 131 | |
| 132 | debug("%s: Device desc %p\n", __func__, desc); |
| 133 | return desc; |
| 134 | } |
| 135 | debug("%s: No device found\n", __func__); |
| 136 | |
| 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | /** |
Tien Fong Chee | 1037852 | 2018-07-06 16:26:36 +0800 | [diff] [blame] | 141 | * blk_get_by_device() - Get the block device descriptor for the given device |
| 142 | * @dev: Instance of a storage device |
| 143 | * |
| 144 | * Return: With block device descriptor on success , NULL if there is no such |
| 145 | * block device. |
| 146 | */ |
| 147 | struct blk_desc *blk_get_by_device(struct udevice *dev) |
| 148 | { |
Simon Glass | cfd7293 | 2019-09-25 08:55:56 -0600 | [diff] [blame] | 149 | struct udevice *child_dev; |
Tien Fong Chee | 1037852 | 2018-07-06 16:26:36 +0800 | [diff] [blame] | 150 | |
Simon Glass | cfd7293 | 2019-09-25 08:55:56 -0600 | [diff] [blame] | 151 | device_foreach_child(child_dev, dev) { |
Tien Fong Chee | 1037852 | 2018-07-06 16:26:36 +0800 | [diff] [blame] | 152 | if (device_get_uclass_id(child_dev) != UCLASS_BLK) |
| 153 | continue; |
| 154 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 155 | return dev_get_uclass_plat(child_dev); |
Tien Fong Chee | 1037852 | 2018-07-06 16:26:36 +0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | debug("%s: No block device found\n", __func__); |
| 159 | |
| 160 | return NULL; |
| 161 | } |
| 162 | |
| 163 | /** |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 164 | * get_desc() - Get the block device descriptor for the given device number |
| 165 | * |
| 166 | * @if_type: Interface type |
| 167 | * @devnum: Device number (0 = first) |
| 168 | * @descp: Returns block device descriptor on success |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 169 | * Return: 0 on success, -ENODEV if there is no such device and no device |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 170 | * with a higher device number, -ENOENT if there is no such device but there |
| 171 | * is one with a higher number, or other -ve on other error. |
| 172 | */ |
| 173 | static int get_desc(enum if_type if_type, int devnum, struct blk_desc **descp) |
| 174 | { |
| 175 | bool found_more = false; |
| 176 | struct udevice *dev; |
| 177 | struct uclass *uc; |
| 178 | int ret; |
| 179 | |
| 180 | *descp = NULL; |
| 181 | ret = uclass_get(UCLASS_BLK, &uc); |
| 182 | if (ret) |
| 183 | return ret; |
| 184 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 185 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 186 | |
| 187 | debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__, |
| 188 | if_type, devnum, dev->name, desc->if_type, desc->devnum); |
| 189 | if (desc->if_type == if_type) { |
| 190 | if (desc->devnum == devnum) { |
| 191 | ret = device_probe(dev); |
| 192 | if (ret) |
| 193 | return ret; |
| 194 | |
Michal Simek | f438b6b | 2016-11-16 17:37:42 +0100 | [diff] [blame] | 195 | *descp = desc; |
| 196 | return 0; |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 197 | } else if (desc->devnum > devnum) { |
| 198 | found_more = true; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | return found_more ? -ENOENT : -ENODEV; |
| 204 | } |
| 205 | |
Simon Glass | 13c2c29 | 2016-05-01 13:52:30 -0600 | [diff] [blame] | 206 | int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart) |
| 207 | { |
| 208 | struct udevice *dev; |
| 209 | int ret; |
| 210 | |
| 211 | ret = blk_get_device(if_type, devnum, &dev); |
| 212 | if (ret) |
| 213 | return ret; |
| 214 | |
developer | f50d8d1 | 2019-08-27 15:32:18 +0800 | [diff] [blame] | 215 | return blk_select_hwpart(dev, hwpart); |
Simon Glass | 13c2c29 | 2016-05-01 13:52:30 -0600 | [diff] [blame] | 216 | } |
| 217 | |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 218 | int blk_list_part(enum if_type if_type) |
| 219 | { |
| 220 | struct blk_desc *desc; |
| 221 | int devnum, ok; |
| 222 | int ret; |
| 223 | |
| 224 | for (ok = 0, devnum = 0;; ++devnum) { |
| 225 | ret = get_desc(if_type, devnum, &desc); |
| 226 | if (ret == -ENODEV) |
| 227 | break; |
| 228 | else if (ret) |
| 229 | continue; |
| 230 | if (desc->part_type != PART_TYPE_UNKNOWN) { |
| 231 | ++ok; |
| 232 | if (devnum) |
| 233 | putc('\n'); |
| 234 | part_print(desc); |
| 235 | } |
| 236 | } |
| 237 | if (!ok) |
| 238 | return -ENODEV; |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | int blk_print_part_devnum(enum if_type if_type, int devnum) |
| 244 | { |
| 245 | struct blk_desc *desc; |
| 246 | int ret; |
| 247 | |
| 248 | ret = get_desc(if_type, devnum, &desc); |
| 249 | if (ret) |
| 250 | return ret; |
| 251 | if (desc->type == DEV_TYPE_UNKNOWN) |
| 252 | return -ENOENT; |
| 253 | part_print(desc); |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | void blk_list_devices(enum if_type if_type) |
| 259 | { |
| 260 | struct blk_desc *desc; |
| 261 | int ret; |
| 262 | int i; |
| 263 | |
| 264 | for (i = 0;; ++i) { |
| 265 | ret = get_desc(if_type, i, &desc); |
| 266 | if (ret == -ENODEV) |
| 267 | break; |
| 268 | else if (ret) |
| 269 | continue; |
| 270 | if (desc->type == DEV_TYPE_UNKNOWN) |
| 271 | continue; /* list only known devices */ |
| 272 | printf("Device %d: ", i); |
| 273 | dev_print(desc); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | int blk_print_device_num(enum if_type if_type, int devnum) |
| 278 | { |
| 279 | struct blk_desc *desc; |
| 280 | int ret; |
| 281 | |
| 282 | ret = get_desc(if_type, devnum, &desc); |
| 283 | if (ret) |
| 284 | return ret; |
| 285 | printf("\nIDE device %d: ", devnum); |
| 286 | dev_print(desc); |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | int blk_show_device(enum if_type if_type, int devnum) |
| 292 | { |
| 293 | struct blk_desc *desc; |
| 294 | int ret; |
| 295 | |
| 296 | printf("\nDevice %d: ", devnum); |
| 297 | ret = get_desc(if_type, devnum, &desc); |
| 298 | if (ret == -ENODEV || ret == -ENOENT) { |
| 299 | printf("unknown device\n"); |
| 300 | return -ENODEV; |
| 301 | } |
| 302 | if (ret) |
| 303 | return ret; |
| 304 | dev_print(desc); |
| 305 | |
| 306 | if (desc->type == DEV_TYPE_UNKNOWN) |
| 307 | return -ENOENT; |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start, |
| 313 | lbaint_t blkcnt, void *buffer) |
| 314 | { |
| 315 | struct blk_desc *desc; |
| 316 | ulong n; |
| 317 | int ret; |
| 318 | |
| 319 | ret = get_desc(if_type, devnum, &desc); |
| 320 | if (ret) |
| 321 | return ret; |
| 322 | n = blk_dread(desc, start, blkcnt, buffer); |
| 323 | if (IS_ERR_VALUE(n)) |
| 324 | return n; |
| 325 | |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 326 | return n; |
| 327 | } |
| 328 | |
| 329 | ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start, |
| 330 | lbaint_t blkcnt, const void *buffer) |
| 331 | { |
| 332 | struct blk_desc *desc; |
| 333 | int ret; |
| 334 | |
| 335 | ret = get_desc(if_type, devnum, &desc); |
| 336 | if (ret) |
| 337 | return ret; |
| 338 | return blk_dwrite(desc, start, blkcnt, buffer); |
| 339 | } |
| 340 | |
Simon Glass | 13c2c29 | 2016-05-01 13:52:30 -0600 | [diff] [blame] | 341 | int blk_select_hwpart(struct udevice *dev, int hwpart) |
| 342 | { |
| 343 | const struct blk_ops *ops = blk_get_ops(dev); |
| 344 | |
| 345 | if (!ops) |
| 346 | return -ENOSYS; |
| 347 | if (!ops->select_hwpart) |
| 348 | return 0; |
| 349 | |
| 350 | return ops->select_hwpart(dev, hwpart); |
| 351 | } |
| 352 | |
| 353 | int blk_dselect_hwpart(struct blk_desc *desc, int hwpart) |
| 354 | { |
developer | f50d8d1 | 2019-08-27 15:32:18 +0800 | [diff] [blame] | 355 | return blk_select_hwpart(desc->bdev, hwpart); |
Simon Glass | 13c2c29 | 2016-05-01 13:52:30 -0600 | [diff] [blame] | 356 | } |
| 357 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 358 | int blk_first_device(int if_type, struct udevice **devp) |
| 359 | { |
| 360 | struct blk_desc *desc; |
| 361 | int ret; |
| 362 | |
Stefan Roese | ef58a90 | 2017-11-29 16:46:42 +0100 | [diff] [blame] | 363 | ret = uclass_find_first_device(UCLASS_BLK, devp); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 364 | if (ret) |
| 365 | return ret; |
| 366 | if (!*devp) |
| 367 | return -ENODEV; |
| 368 | do { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 369 | desc = dev_get_uclass_plat(*devp); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 370 | if (desc->if_type == if_type) |
| 371 | return 0; |
Stefan Roese | ef58a90 | 2017-11-29 16:46:42 +0100 | [diff] [blame] | 372 | ret = uclass_find_next_device(devp); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 373 | if (ret) |
| 374 | return ret; |
| 375 | } while (*devp); |
| 376 | |
| 377 | return -ENODEV; |
| 378 | } |
| 379 | |
| 380 | int blk_next_device(struct udevice **devp) |
| 381 | { |
| 382 | struct blk_desc *desc; |
| 383 | int ret, if_type; |
| 384 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 385 | desc = dev_get_uclass_plat(*devp); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 386 | if_type = desc->if_type; |
| 387 | do { |
Stefan Roese | ef58a90 | 2017-11-29 16:46:42 +0100 | [diff] [blame] | 388 | ret = uclass_find_next_device(devp); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 389 | if (ret) |
| 390 | return ret; |
| 391 | if (!*devp) |
| 392 | return -ENODEV; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 393 | desc = dev_get_uclass_plat(*devp); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 394 | if (desc->if_type == if_type) |
| 395 | return 0; |
| 396 | } while (1); |
| 397 | } |
| 398 | |
Simon Glass | d5d4c10 | 2017-04-23 20:02:05 -0600 | [diff] [blame] | 399 | int blk_find_device(int if_type, int devnum, struct udevice **devp) |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 400 | { |
| 401 | struct uclass *uc; |
| 402 | struct udevice *dev; |
| 403 | int ret; |
| 404 | |
| 405 | ret = uclass_get(UCLASS_BLK, &uc); |
| 406 | if (ret) |
| 407 | return ret; |
| 408 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 409 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 410 | |
| 411 | debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__, |
| 412 | if_type, devnum, dev->name, desc->if_type, desc->devnum); |
| 413 | if (desc->if_type == if_type && desc->devnum == devnum) { |
| 414 | *devp = dev; |
Simon Glass | d5d4c10 | 2017-04-23 20:02:05 -0600 | [diff] [blame] | 415 | return 0; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
| 419 | return -ENODEV; |
| 420 | } |
| 421 | |
Simon Glass | d5d4c10 | 2017-04-23 20:02:05 -0600 | [diff] [blame] | 422 | int blk_get_device(int if_type, int devnum, struct udevice **devp) |
| 423 | { |
| 424 | int ret; |
| 425 | |
| 426 | ret = blk_find_device(if_type, devnum, devp); |
| 427 | if (ret) |
| 428 | return ret; |
| 429 | |
| 430 | return device_probe(*devp); |
| 431 | } |
| 432 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 433 | unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start, |
| 434 | lbaint_t blkcnt, void *buffer) |
| 435 | { |
| 436 | struct udevice *dev = block_dev->bdev; |
| 437 | const struct blk_ops *ops = blk_get_ops(dev); |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 438 | ulong blks_read; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 439 | |
| 440 | if (!ops->read) |
| 441 | return -ENOSYS; |
| 442 | |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 443 | if (blkcache_read(block_dev->if_type, block_dev->devnum, |
| 444 | start, blkcnt, block_dev->blksz, buffer)) |
| 445 | return blkcnt; |
| 446 | blks_read = ops->read(dev, start, blkcnt, buffer); |
| 447 | if (blks_read == blkcnt) |
| 448 | blkcache_fill(block_dev->if_type, block_dev->devnum, |
| 449 | start, blkcnt, block_dev->blksz, buffer); |
| 450 | |
| 451 | return blks_read; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start, |
| 455 | lbaint_t blkcnt, const void *buffer) |
| 456 | { |
| 457 | struct udevice *dev = block_dev->bdev; |
| 458 | const struct blk_ops *ops = blk_get_ops(dev); |
| 459 | |
| 460 | if (!ops->write) |
| 461 | return -ENOSYS; |
| 462 | |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 463 | blkcache_invalidate(block_dev->if_type, block_dev->devnum); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 464 | return ops->write(dev, start, blkcnt, buffer); |
| 465 | } |
| 466 | |
| 467 | unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start, |
| 468 | lbaint_t blkcnt) |
| 469 | { |
| 470 | struct udevice *dev = block_dev->bdev; |
| 471 | const struct blk_ops *ops = blk_get_ops(dev); |
| 472 | |
| 473 | if (!ops->erase) |
| 474 | return -ENOSYS; |
| 475 | |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 476 | blkcache_invalidate(block_dev->if_type, block_dev->devnum); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 477 | return ops->erase(dev, start, blkcnt); |
| 478 | } |
| 479 | |
Simon Glass | 4f26913 | 2017-05-27 11:37:17 -0600 | [diff] [blame] | 480 | int blk_get_from_parent(struct udevice *parent, struct udevice **devp) |
| 481 | { |
| 482 | struct udevice *dev; |
| 483 | enum uclass_id id; |
| 484 | int ret; |
| 485 | |
| 486 | device_find_first_child(parent, &dev); |
| 487 | if (!dev) { |
| 488 | debug("%s: No block device found for parent '%s'\n", __func__, |
| 489 | parent->name); |
| 490 | return -ENODEV; |
| 491 | } |
| 492 | id = device_get_uclass_id(dev); |
| 493 | if (id != UCLASS_BLK) { |
| 494 | debug("%s: Incorrect uclass %s for block device '%s'\n", |
| 495 | __func__, uclass_get_name(id), dev->name); |
| 496 | return -ENOTBLK; |
| 497 | } |
| 498 | ret = device_probe(dev); |
| 499 | if (ret) |
| 500 | return ret; |
| 501 | *devp = dev; |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
Simon Glass | f3086cf | 2022-04-24 23:31:03 -0600 | [diff] [blame] | 506 | const char *blk_get_devtype(struct udevice *dev) |
| 507 | { |
| 508 | struct udevice *parent = dev_get_parent(dev); |
| 509 | |
| 510 | return uclass_get_name(device_get_uclass_id(parent)); |
| 511 | }; |
| 512 | |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 513 | int blk_find_max_devnum(enum if_type if_type) |
| 514 | { |
| 515 | struct udevice *dev; |
| 516 | int max_devnum = -ENODEV; |
| 517 | struct uclass *uc; |
| 518 | int ret; |
| 519 | |
| 520 | ret = uclass_get(UCLASS_BLK, &uc); |
| 521 | if (ret) |
| 522 | return ret; |
| 523 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 524 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 525 | |
| 526 | if (desc->if_type == if_type && desc->devnum > max_devnum) |
| 527 | max_devnum = desc->devnum; |
| 528 | } |
| 529 | |
| 530 | return max_devnum; |
| 531 | } |
| 532 | |
Bin Meng | fd5eda7 | 2018-10-15 02:21:09 -0700 | [diff] [blame] | 533 | int blk_next_free_devnum(enum if_type if_type) |
Simon Glass | dbc3861 | 2017-04-23 20:02:06 -0600 | [diff] [blame] | 534 | { |
| 535 | int ret; |
| 536 | |
| 537 | ret = blk_find_max_devnum(if_type); |
| 538 | if (ret == -ENODEV) |
| 539 | return 0; |
| 540 | if (ret < 0) |
| 541 | return ret; |
| 542 | |
| 543 | return ret + 1; |
| 544 | } |
| 545 | |
Simon Glass | fc7a744 | 2021-07-05 16:32:59 -0600 | [diff] [blame] | 546 | static int blk_flags_check(struct udevice *dev, enum blk_flag_t req_flags) |
| 547 | { |
| 548 | const struct blk_desc *desc = dev_get_uclass_plat(dev); |
| 549 | enum blk_flag_t flags; |
| 550 | |
| 551 | flags = desc->removable ? BLKF_REMOVABLE : BLKF_FIXED; |
| 552 | |
| 553 | return flags & req_flags ? 0 : 1; |
| 554 | } |
| 555 | |
Simon Glass | 8e61f93 | 2022-02-28 12:08:35 -0700 | [diff] [blame] | 556 | int blk_find_first(enum blk_flag_t flags, struct udevice **devp) |
| 557 | { |
| 558 | int ret; |
| 559 | |
| 560 | for (ret = uclass_find_first_device(UCLASS_BLK, devp); |
| 561 | *devp && !blk_flags_check(*devp, flags); |
| 562 | ret = uclass_find_next_device(devp)) |
| 563 | return 0; |
| 564 | |
| 565 | return -ENODEV; |
| 566 | } |
| 567 | |
| 568 | int blk_find_next(enum blk_flag_t flags, struct udevice **devp) |
| 569 | { |
| 570 | int ret; |
| 571 | |
| 572 | for (ret = uclass_find_next_device(devp); |
| 573 | *devp && !blk_flags_check(*devp, flags); |
| 574 | ret = uclass_find_next_device(devp)) |
| 575 | return 0; |
| 576 | |
| 577 | return -ENODEV; |
| 578 | } |
| 579 | |
Simon Glass | fc7a744 | 2021-07-05 16:32:59 -0600 | [diff] [blame] | 580 | int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp) |
| 581 | { |
| 582 | int ret; |
| 583 | |
| 584 | for (ret = uclass_first_device_err(UCLASS_BLK, devp); |
| 585 | !ret; |
| 586 | ret = uclass_next_device_err(devp)) { |
| 587 | if (!blk_flags_check(*devp, flags)) |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | return -ENODEV; |
| 592 | } |
| 593 | |
| 594 | int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp) |
| 595 | { |
| 596 | int ret; |
| 597 | |
| 598 | for (ret = uclass_next_device_err(devp); |
| 599 | !ret; |
| 600 | ret = uclass_next_device_err(devp)) { |
| 601 | if (!blk_flags_check(*devp, flags)) |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | return -ENODEV; |
| 606 | } |
| 607 | |
| 608 | int blk_count_devices(enum blk_flag_t flag) |
| 609 | { |
| 610 | struct udevice *dev; |
| 611 | int count = 0; |
| 612 | |
| 613 | blk_foreach_probe(flag, dev) |
| 614 | count++; |
| 615 | |
| 616 | return count; |
| 617 | } |
| 618 | |
Simon Glass | e4fef74 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 619 | static int blk_claim_devnum(enum if_type if_type, int devnum) |
| 620 | { |
| 621 | struct udevice *dev; |
| 622 | struct uclass *uc; |
| 623 | int ret; |
| 624 | |
| 625 | ret = uclass_get(UCLASS_BLK, &uc); |
| 626 | if (ret) |
| 627 | return ret; |
| 628 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 629 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | e4fef74 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 630 | |
| 631 | if (desc->if_type == if_type && desc->devnum == devnum) { |
| 632 | int next = blk_next_free_devnum(if_type); |
| 633 | |
| 634 | if (next < 0) |
| 635 | return next; |
| 636 | desc->devnum = next; |
| 637 | return 0; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | return -ENOENT; |
| 642 | } |
| 643 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 644 | int blk_create_device(struct udevice *parent, const char *drv_name, |
| 645 | const char *name, int if_type, int devnum, int blksz, |
Jean-Jacques Hiblot | 99b324a | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 646 | lbaint_t lba, struct udevice **devp) |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 647 | { |
| 648 | struct blk_desc *desc; |
| 649 | struct udevice *dev; |
| 650 | int ret; |
| 651 | |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 652 | if (devnum == -1) { |
Simon Glass | e4fef74 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 653 | devnum = blk_next_free_devnum(if_type); |
| 654 | } else { |
| 655 | ret = blk_claim_devnum(if_type, devnum); |
| 656 | if (ret < 0 && ret != -ENOENT) |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 657 | return ret; |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 658 | } |
Simon Glass | e4fef74 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 659 | if (devnum < 0) |
| 660 | return devnum; |
Simon Glass | 77f7fb8 | 2016-05-01 13:52:22 -0600 | [diff] [blame] | 661 | ret = device_bind_driver(parent, drv_name, name, &dev); |
| 662 | if (ret) |
| 663 | return ret; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 664 | desc = dev_get_uclass_plat(dev); |
Simon Glass | 77f7fb8 | 2016-05-01 13:52:22 -0600 | [diff] [blame] | 665 | desc->if_type = if_type; |
| 666 | desc->blksz = blksz; |
Heinrich Schuchardt | 09ba4ea | 2019-10-25 12:15:31 +0200 | [diff] [blame] | 667 | desc->log2blksz = LOG2(desc->blksz); |
Jean-Jacques Hiblot | 99b324a | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 668 | desc->lba = lba; |
Simon Glass | 77f7fb8 | 2016-05-01 13:52:22 -0600 | [diff] [blame] | 669 | desc->part_type = PART_TYPE_UNKNOWN; |
| 670 | desc->bdev = dev; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 671 | desc->devnum = devnum; |
| 672 | *devp = dev; |
| 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
Simon Glass | 966b695 | 2016-05-01 11:36:29 -0600 | [diff] [blame] | 677 | int blk_create_devicef(struct udevice *parent, const char *drv_name, |
| 678 | const char *name, int if_type, int devnum, int blksz, |
Jean-Jacques Hiblot | 99b324a | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 679 | lbaint_t lba, struct udevice **devp) |
Simon Glass | 966b695 | 2016-05-01 11:36:29 -0600 | [diff] [blame] | 680 | { |
| 681 | char dev_name[30], *str; |
Simon Glass | 39e5492 | 2016-05-01 13:52:24 -0600 | [diff] [blame] | 682 | int ret; |
Simon Glass | 966b695 | 2016-05-01 11:36:29 -0600 | [diff] [blame] | 683 | |
| 684 | snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name); |
| 685 | str = strdup(dev_name); |
| 686 | if (!str) |
| 687 | return -ENOMEM; |
| 688 | |
Simon Glass | 39e5492 | 2016-05-01 13:52:24 -0600 | [diff] [blame] | 689 | ret = blk_create_device(parent, drv_name, str, if_type, devnum, |
Jean-Jacques Hiblot | 99b324a | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 690 | blksz, lba, devp); |
Simon Glass | 39e5492 | 2016-05-01 13:52:24 -0600 | [diff] [blame] | 691 | if (ret) { |
| 692 | free(str); |
| 693 | return ret; |
| 694 | } |
| 695 | device_set_name_alloced(*devp); |
| 696 | |
Simon Glass | 4450597 | 2017-07-29 11:34:59 -0600 | [diff] [blame] | 697 | return 0; |
Simon Glass | 966b695 | 2016-05-01 11:36:29 -0600 | [diff] [blame] | 698 | } |
| 699 | |
AKASHI Takahiro | 3e32dbe | 2021-12-10 15:49:29 +0900 | [diff] [blame] | 700 | int blk_probe_or_unbind(struct udevice *dev) |
| 701 | { |
| 702 | int ret; |
| 703 | |
| 704 | ret = device_probe(dev); |
| 705 | if (ret) { |
| 706 | log_debug("probing %s failed\n", dev->name); |
| 707 | device_unbind(dev); |
| 708 | } |
| 709 | |
| 710 | return ret; |
| 711 | } |
| 712 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 713 | int blk_unbind_all(int if_type) |
| 714 | { |
| 715 | struct uclass *uc; |
| 716 | struct udevice *dev, *next; |
| 717 | int ret; |
| 718 | |
| 719 | ret = uclass_get(UCLASS_BLK, &uc); |
| 720 | if (ret) |
| 721 | return ret; |
| 722 | uclass_foreach_dev_safe(dev, next, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 723 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 724 | |
| 725 | if (desc->if_type == if_type) { |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 726 | ret = device_remove(dev, DM_REMOVE_NORMAL); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 727 | if (ret) |
| 728 | return ret; |
| 729 | ret = device_unbind(dev); |
| 730 | if (ret) |
| 731 | return ret; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | return 0; |
| 736 | } |
| 737 | |
Bin Meng | cbc3da8 | 2018-10-15 02:21:07 -0700 | [diff] [blame] | 738 | static int blk_post_probe(struct udevice *dev) |
| 739 | { |
Simon Glass | f5ac303 | 2022-08-11 19:34:45 -0600 | [diff] [blame] | 740 | if (CONFIG_IS_ENABLED(PARTITIONS) && blk_enabled()) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 741 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Bin Meng | cbc3da8 | 2018-10-15 02:21:07 -0700 | [diff] [blame] | 742 | |
Ovidiu Panait | 76160a0 | 2020-07-24 14:12:21 +0300 | [diff] [blame] | 743 | part_init(desc); |
AKASHI Takahiro | 358e6c7 | 2022-04-19 10:05:10 +0900 | [diff] [blame] | 744 | |
| 745 | if (desc->part_type != PART_TYPE_UNKNOWN && |
| 746 | part_create_block_devices(dev)) |
| 747 | debug("*** creating partitions failed\n"); |
Ovidiu Panait | 76160a0 | 2020-07-24 14:12:21 +0300 | [diff] [blame] | 748 | } |
Bin Meng | cbc3da8 | 2018-10-15 02:21:07 -0700 | [diff] [blame] | 749 | |
| 750 | return 0; |
| 751 | } |
| 752 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 753 | UCLASS_DRIVER(blk) = { |
| 754 | .id = UCLASS_BLK, |
| 755 | .name = "blk", |
Bin Meng | cbc3da8 | 2018-10-15 02:21:07 -0700 | [diff] [blame] | 756 | .post_probe = blk_post_probe, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 757 | .per_device_plat_auto = sizeof(struct blk_desc), |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 758 | }; |