1 /* 2 * Copyright (C) 2016 Google, Inc 3 * Written by Simon Glass <sjg@chromium.org> 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 8 #include <common.h> 9 #include <blk.h> 10 #include <dm.h> 11 #include <dm/device-internal.h> 12 #include <dm/lists.h> 13 #include <dm/uclass-internal.h> 14 15 static const char *if_typename_str[IF_TYPE_COUNT] = { 16 [IF_TYPE_IDE] = "ide", 17 [IF_TYPE_SCSI] = "scsi", 18 [IF_TYPE_ATAPI] = "atapi", 19 [IF_TYPE_USB] = "usb", 20 [IF_TYPE_DOC] = "doc", 21 [IF_TYPE_MMC] = "mmc", 22 [IF_TYPE_SD] = "sd", 23 [IF_TYPE_SATA] = "sata", 24 [IF_TYPE_HOST] = "host", 25 [IF_TYPE_SYSTEMACE] = "ace", 26 [IF_TYPE_NVME] = "nvme", 27 [IF_TYPE_RKNAND] = "rknand", 28 [IF_TYPE_SPINAND] = "spinand", 29 [IF_TYPE_SPINOR] = "spinor", 30 [IF_TYPE_RAMDISK] = "ramdisk", 31 [IF_TYPE_MTD] = "mtd", 32 }; 33 34 static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = { 35 [IF_TYPE_IDE] = UCLASS_IDE, 36 [IF_TYPE_SCSI] = UCLASS_SCSI, 37 [IF_TYPE_ATAPI] = UCLASS_INVALID, 38 [IF_TYPE_USB] = UCLASS_MASS_STORAGE, 39 [IF_TYPE_DOC] = UCLASS_INVALID, 40 [IF_TYPE_MMC] = UCLASS_MMC, 41 [IF_TYPE_SD] = UCLASS_INVALID, 42 [IF_TYPE_SATA] = UCLASS_AHCI, 43 [IF_TYPE_HOST] = UCLASS_ROOT, 44 [IF_TYPE_NVME] = UCLASS_NVME, 45 [IF_TYPE_RKNAND] = UCLASS_RKNAND, 46 [IF_TYPE_SPINAND] = UCLASS_SPI_FLASH, 47 [IF_TYPE_SPINOR] = UCLASS_SPI_FLASH, 48 [IF_TYPE_RAMDISK] = UCLASS_RAMDISK, 49 [IF_TYPE_MTD] = UCLASS_MTD, 50 [IF_TYPE_SYSTEMACE] = UCLASS_INVALID, 51 }; 52 53 enum if_type if_typename_to_iftype(const char *if_typename) 54 { 55 int i; 56 57 for (i = 0; i < IF_TYPE_COUNT; i++) { 58 if (if_typename_str[i] && 59 !strcmp(if_typename, if_typename_str[i])) 60 return i; 61 } 62 63 return IF_TYPE_UNKNOWN; 64 } 65 66 static enum uclass_id if_type_to_uclass_id(enum if_type if_type) 67 { 68 return if_type_uclass_id[if_type]; 69 } 70 71 const char *blk_get_if_type_name(enum if_type if_type) 72 { 73 return if_typename_str[if_type]; 74 } 75 76 struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum) 77 { 78 struct blk_desc *desc; 79 struct udevice *dev; 80 int ret; 81 82 ret = blk_get_device(if_type, devnum, &dev); 83 if (ret) 84 return NULL; 85 desc = dev_get_uclass_platdata(dev); 86 87 return desc; 88 } 89 90 /* 91 * This function is complicated with driver model. We look up the interface 92 * name in a local table. This gives us an interface type which we can match 93 * against the uclass of the block device's parent. 94 */ 95 struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum) 96 { 97 enum uclass_id uclass_id; 98 enum if_type if_type; 99 struct udevice *dev; 100 struct uclass *uc; 101 int ret; 102 103 if_type = if_typename_to_iftype(if_typename); 104 if (if_type == IF_TYPE_UNKNOWN) { 105 debug("%s: Unknown interface type '%s'\n", __func__, 106 if_typename); 107 return NULL; 108 } 109 uclass_id = if_type_to_uclass_id(if_type); 110 if (uclass_id == UCLASS_INVALID) { 111 debug("%s: Unknown uclass for interface type'\n", 112 if_typename_str[if_type]); 113 return NULL; 114 } 115 116 ret = uclass_get(UCLASS_BLK, &uc); 117 if (ret) 118 return NULL; 119 uclass_foreach_dev(dev, uc) { 120 struct blk_desc *desc = dev_get_uclass_platdata(dev); 121 122 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__, 123 if_type, devnum, dev->name, desc->if_type, desc->devnum); 124 if (desc->devnum != devnum) 125 continue; 126 127 /* Find out the parent device uclass */ 128 if (device_get_uclass_id(dev->parent) != uclass_id) { 129 #ifdef CONFIG_MTD_BLK 130 /* 131 * The normal mtd block attachment steps are 132 * UCLASS_BLK -> UCLASS_MTD -> UCLASS_(SPI or NAND). 133 * Since the spi flash frame is attached to 134 * UCLASS_SPI_FLASH, this make mistake to find 135 * the UCLASS_MTD when find the mtd block device. 136 * Fix it here when enable CONFIG_MTD_BLK. 137 */ 138 if (device_get_uclass_id(dev->parent) == UCLASS_SPI_FLASH && 139 if_type == IF_TYPE_MTD && 140 devnum == BLK_MTD_SPI_NOR) { 141 debug("Fix the spi flash uclass different\n"); 142 } else { 143 debug("%s: parent uclass %d, this dev %d\n", 144 __func__, 145 device_get_uclass_id(dev->parent), 146 uclass_id); 147 continue; 148 } 149 #else 150 debug("%s: parent uclass %d, this dev %d\n", __func__, 151 device_get_uclass_id(dev->parent), uclass_id); 152 continue; 153 #endif 154 } 155 156 if (device_probe(dev)) 157 return NULL; 158 159 debug("%s: Device desc %p\n", __func__, desc); 160 return desc; 161 } 162 debug("%s: No device found\n", __func__); 163 164 return NULL; 165 } 166 167 /** 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) { 189 struct blk_desc *desc = dev_get_uclass_platdata(dev); 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 199 *descp = desc; 200 return 0; 201 } else if (desc->devnum > devnum) { 202 found_more = true; 203 } 204 } 205 } 206 207 return found_more ? -ENOENT : -ENODEV; 208 } 209 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 219 return blk_select_hwpart(dev, hwpart); 220 } 221 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 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 345 ulong blk_erase_devnum(enum if_type if_type, int devnum, lbaint_t start, 346 lbaint_t blkcnt) 347 { 348 struct blk_desc *desc; 349 int ret; 350 351 ret = get_desc(if_type, devnum, &desc); 352 if (ret) 353 return ret; 354 return blk_derase(desc, start, blkcnt); 355 } 356 357 int blk_select_hwpart(struct udevice *dev, int hwpart) 358 { 359 const struct blk_ops *ops = blk_get_ops(dev); 360 361 if (!ops) 362 return -ENOSYS; 363 if (!ops->select_hwpart) 364 return 0; 365 366 return ops->select_hwpart(dev, hwpart); 367 } 368 369 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart) 370 { 371 return blk_select_hwpart(desc->bdev, hwpart); 372 } 373 374 int blk_first_device(int if_type, struct udevice **devp) 375 { 376 struct blk_desc *desc; 377 int ret; 378 379 ret = uclass_find_first_device(UCLASS_BLK, devp); 380 if (ret) 381 return ret; 382 if (!*devp) 383 return -ENODEV; 384 do { 385 desc = dev_get_uclass_platdata(*devp); 386 if (desc->if_type == if_type) 387 return 0; 388 ret = uclass_find_next_device(devp); 389 if (ret) 390 return ret; 391 } while (*devp); 392 393 return -ENODEV; 394 } 395 396 int blk_next_device(struct udevice **devp) 397 { 398 struct blk_desc *desc; 399 int ret, if_type; 400 401 desc = dev_get_uclass_platdata(*devp); 402 if_type = desc->if_type; 403 do { 404 ret = uclass_find_next_device(devp); 405 if (ret) 406 return ret; 407 if (!*devp) 408 return -ENODEV; 409 desc = dev_get_uclass_platdata(*devp); 410 if (desc->if_type == if_type) 411 return 0; 412 } while (1); 413 } 414 415 int blk_find_device(int if_type, int devnum, struct udevice **devp) 416 { 417 struct uclass *uc; 418 struct udevice *dev; 419 int ret; 420 421 ret = uclass_get(UCLASS_BLK, &uc); 422 if (ret) 423 return ret; 424 uclass_foreach_dev(dev, uc) { 425 struct blk_desc *desc = dev_get_uclass_platdata(dev); 426 427 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__, 428 if_type, devnum, dev->name, desc->if_type, desc->devnum); 429 if (desc->if_type == if_type && desc->devnum == devnum) { 430 *devp = dev; 431 return 0; 432 } 433 } 434 435 return -ENODEV; 436 } 437 438 int blk_get_device(int if_type, int devnum, struct udevice **devp) 439 { 440 int ret; 441 442 ret = blk_find_device(if_type, devnum, devp); 443 if (ret) 444 return ret; 445 446 return device_probe(*devp); 447 } 448 449 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start, 450 lbaint_t blkcnt, void *buffer) 451 { 452 struct udevice *dev = block_dev->bdev; 453 const struct blk_ops *ops = blk_get_ops(dev); 454 ulong blks_read; 455 456 if (!ops->read) 457 return -ENOSYS; 458 459 if (blkcache_read(block_dev->if_type, block_dev->devnum, 460 start, blkcnt, block_dev->blksz, buffer)) 461 return blkcnt; 462 blks_read = ops->read(dev, start, blkcnt, buffer); 463 if (blks_read == blkcnt) 464 blkcache_fill(block_dev->if_type, block_dev->devnum, 465 start, blkcnt, block_dev->blksz, buffer); 466 467 return blks_read; 468 } 469 470 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start, 471 lbaint_t blkcnt, const void *buffer) 472 { 473 struct udevice *dev = block_dev->bdev; 474 const struct blk_ops *ops = blk_get_ops(dev); 475 476 if (!ops->write) 477 return -ENOSYS; 478 479 blkcache_invalidate(block_dev->if_type, block_dev->devnum); 480 return ops->write(dev, start, blkcnt, buffer); 481 } 482 483 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start, 484 lbaint_t blkcnt) 485 { 486 struct udevice *dev = block_dev->bdev; 487 const struct blk_ops *ops = blk_get_ops(dev); 488 489 if (!ops->erase) 490 return -ENOSYS; 491 492 blkcache_invalidate(block_dev->if_type, block_dev->devnum); 493 return ops->erase(dev, start, blkcnt); 494 } 495 496 int blk_prepare_device(struct udevice *dev) 497 { 498 struct blk_desc *desc = dev_get_uclass_platdata(dev); 499 500 part_init(desc); 501 502 return 0; 503 } 504 505 int blk_get_from_parent(struct udevice *parent, struct udevice **devp) 506 { 507 struct udevice *dev; 508 enum uclass_id id; 509 int ret; 510 511 device_find_first_child(parent, &dev); 512 if (!dev) { 513 debug("%s: No block device found for parent '%s'\n", __func__, 514 parent->name); 515 return -ENODEV; 516 } 517 id = device_get_uclass_id(dev); 518 if (id != UCLASS_BLK) { 519 debug("%s: Incorrect uclass %s for block device '%s'\n", 520 __func__, uclass_get_name(id), dev->name); 521 return -ENOTBLK; 522 } 523 ret = device_probe(dev); 524 if (ret) 525 return ret; 526 *devp = dev; 527 528 return 0; 529 } 530 531 int blk_find_max_devnum(enum if_type if_type) 532 { 533 struct udevice *dev; 534 int max_devnum = -ENODEV; 535 struct uclass *uc; 536 int ret; 537 538 ret = uclass_get(UCLASS_BLK, &uc); 539 if (ret) 540 return ret; 541 uclass_foreach_dev(dev, uc) { 542 struct blk_desc *desc = dev_get_uclass_platdata(dev); 543 544 if (desc->if_type == if_type && desc->devnum > max_devnum) 545 max_devnum = desc->devnum; 546 } 547 548 return max_devnum; 549 } 550 551 static int blk_next_free_devnum(enum if_type if_type) 552 { 553 int ret; 554 555 ret = blk_find_max_devnum(if_type); 556 if (ret == -ENODEV) 557 return 0; 558 if (ret < 0) 559 return ret; 560 561 return ret + 1; 562 } 563 564 static int blk_claim_devnum(enum if_type if_type, int devnum) 565 { 566 struct udevice *dev; 567 struct uclass *uc; 568 int ret; 569 570 ret = uclass_get(UCLASS_BLK, &uc); 571 if (ret) 572 return ret; 573 uclass_foreach_dev(dev, uc) { 574 struct blk_desc *desc = dev_get_uclass_platdata(dev); 575 576 if (desc->if_type == if_type && desc->devnum == devnum) { 577 int next = blk_next_free_devnum(if_type); 578 579 if (next < 0) 580 return next; 581 #ifdef CONFIG_USING_KERNEL_DTB_V2 582 /* 583 * Not allow devnum to be forced distributed. 584 * See commit (e48eeb9ea3 dm: blk: Improve block device claiming). 585 * 586 * fix like: "Device 'dwmmc@fe2b0000': seq 0 is in use by 'sdhci@fe310000'" 587 */ 588 if (!(gd->flags & GD_FLG_KDTB_READY)) 589 #endif 590 desc->devnum = next; 591 592 return 0; 593 } 594 } 595 596 return -ENOENT; 597 } 598 599 int blk_create_device(struct udevice *parent, const char *drv_name, 600 const char *name, int if_type, int devnum, int blksz, 601 lbaint_t size, struct udevice **devp) 602 { 603 struct blk_desc *desc; 604 struct udevice *dev; 605 int ret; 606 607 if (devnum == -1) { 608 devnum = blk_next_free_devnum(if_type); 609 } else { 610 ret = blk_claim_devnum(if_type, devnum); 611 if (ret < 0 && ret != -ENOENT) 612 return ret; 613 } 614 if (devnum < 0) 615 return devnum; 616 ret = device_bind_driver(parent, drv_name, name, &dev); 617 if (ret) 618 return ret; 619 desc = dev_get_uclass_platdata(dev); 620 desc->if_type = if_type; 621 desc->blksz = blksz; 622 desc->lba = size / blksz; 623 desc->part_type = PART_TYPE_UNKNOWN; 624 desc->bdev = dev; 625 desc->devnum = devnum; 626 *devp = dev; 627 628 return 0; 629 } 630 631 int blk_create_devicef(struct udevice *parent, const char *drv_name, 632 const char *name, int if_type, int devnum, int blksz, 633 lbaint_t size, struct udevice **devp) 634 { 635 char dev_name[30], *str; 636 int ret; 637 638 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name); 639 str = strdup(dev_name); 640 if (!str) 641 return -ENOMEM; 642 643 ret = blk_create_device(parent, drv_name, str, if_type, devnum, 644 blksz, size, devp); 645 if (ret) { 646 free(str); 647 return ret; 648 } 649 device_set_name_alloced(*devp); 650 651 return 0; 652 } 653 654 int blk_unbind_all(int if_type) 655 { 656 struct uclass *uc; 657 struct udevice *dev, *next; 658 int ret; 659 660 ret = uclass_get(UCLASS_BLK, &uc); 661 if (ret) 662 return ret; 663 uclass_foreach_dev_safe(dev, next, uc) { 664 struct blk_desc *desc = dev_get_uclass_platdata(dev); 665 666 if (desc->if_type == if_type) { 667 ret = device_remove(dev, DM_REMOVE_NORMAL); 668 if (ret) 669 return ret; 670 ret = device_unbind(dev); 671 if (ret) 672 return ret; 673 } 674 } 675 676 return 0; 677 } 678 679 UCLASS_DRIVER(blk) = { 680 .id = UCLASS_BLK, 681 .name = "blk", 682 .per_device_platdata_auto_alloc_size = sizeof(struct blk_desc), 683 }; 684