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