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