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 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 { 359 return blk_select_hwpart(desc->bdev, hwpart); 360 } 361 362 int blk_first_device(int if_type, struct udevice **devp) 363 { 364 struct blk_desc *desc; 365 int ret; 366 367 ret = uclass_find_first_device(UCLASS_BLK, devp); 368 if (ret) 369 return ret; 370 if (!*devp) 371 return -ENODEV; 372 do { 373 desc = dev_get_uclass_platdata(*devp); 374 if (desc->if_type == if_type) 375 return 0; 376 ret = uclass_find_next_device(devp); 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 389 desc = dev_get_uclass_platdata(*devp); 390 if_type = desc->if_type; 391 do { 392 ret = uclass_find_next_device(devp); 393 if (ret) 394 return ret; 395 if (!*devp) 396 return -ENODEV; 397 desc = dev_get_uclass_platdata(*devp); 398 if (desc->if_type == if_type) 399 return 0; 400 } while (1); 401 } 402 403 int blk_find_device(int if_type, int devnum, struct udevice **devp) 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) { 413 struct blk_desc *desc = dev_get_uclass_platdata(dev); 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; 419 return 0; 420 } 421 } 422 423 return -ENODEV; 424 } 425 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 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); 442 ulong blks_read; 443 444 if (!ops->read) 445 return -ENOSYS; 446 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; 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 467 blkcache_invalidate(block_dev->if_type, block_dev->devnum); 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 480 blkcache_invalidate(block_dev->if_type, block_dev->devnum); 481 return ops->erase(dev, start, blkcnt); 482 } 483 484 int blk_prepare_device(struct udevice *dev) 485 { 486 struct blk_desc *desc = dev_get_uclass_platdata(dev); 487 488 part_init(desc); 489 490 return 0; 491 } 492 493 int blk_get_from_parent(struct udevice *parent, struct udevice **devp) 494 { 495 struct udevice *dev; 496 enum uclass_id id; 497 int ret; 498 499 device_find_first_child(parent, &dev); 500 if (!dev) { 501 debug("%s: No block device found for parent '%s'\n", __func__, 502 parent->name); 503 return -ENODEV; 504 } 505 id = device_get_uclass_id(dev); 506 if (id != UCLASS_BLK) { 507 debug("%s: Incorrect uclass %s for block device '%s'\n", 508 __func__, uclass_get_name(id), dev->name); 509 return -ENOTBLK; 510 } 511 ret = device_probe(dev); 512 if (ret) 513 return ret; 514 *devp = dev; 515 516 return 0; 517 } 518 519 int blk_find_max_devnum(enum if_type if_type) 520 { 521 struct udevice *dev; 522 int max_devnum = -ENODEV; 523 struct uclass *uc; 524 int ret; 525 526 ret = uclass_get(UCLASS_BLK, &uc); 527 if (ret) 528 return ret; 529 uclass_foreach_dev(dev, uc) { 530 struct blk_desc *desc = dev_get_uclass_platdata(dev); 531 532 if (desc->if_type == if_type && desc->devnum > max_devnum) 533 max_devnum = desc->devnum; 534 } 535 536 return max_devnum; 537 } 538 539 static int blk_next_free_devnum(enum if_type if_type) 540 { 541 int ret; 542 543 ret = blk_find_max_devnum(if_type); 544 if (ret == -ENODEV) 545 return 0; 546 if (ret < 0) 547 return ret; 548 549 return ret + 1; 550 } 551 552 static int blk_claim_devnum(enum if_type if_type, int devnum) 553 { 554 struct udevice *dev; 555 struct uclass *uc; 556 int ret; 557 558 ret = uclass_get(UCLASS_BLK, &uc); 559 if (ret) 560 return ret; 561 uclass_foreach_dev(dev, uc) { 562 struct blk_desc *desc = dev_get_uclass_platdata(dev); 563 564 if (desc->if_type == if_type && desc->devnum == devnum) { 565 int next = blk_next_free_devnum(if_type); 566 567 if (next < 0) 568 return next; 569 #ifdef CONFIG_USING_KERNEL_DTB_V2 570 /* 571 * Not allow devnum to be forced distributed. 572 * See commit (e48eeb9ea3 dm: blk: Improve block device claiming). 573 * 574 * fix like: "Device 'dwmmc@fe2b0000': seq 0 is in use by 'sdhci@fe310000'" 575 */ 576 if (!(gd->flags & GD_FLG_KDTB_READY)) 577 #endif 578 desc->devnum = next; 579 580 return 0; 581 } 582 } 583 584 return -ENOENT; 585 } 586 587 int blk_create_device(struct udevice *parent, const char *drv_name, 588 const char *name, int if_type, int devnum, int blksz, 589 lbaint_t size, struct udevice **devp) 590 { 591 struct blk_desc *desc; 592 struct udevice *dev; 593 int ret; 594 595 if (devnum == -1) { 596 devnum = blk_next_free_devnum(if_type); 597 } else { 598 ret = blk_claim_devnum(if_type, devnum); 599 if (ret < 0 && ret != -ENOENT) 600 return ret; 601 } 602 if (devnum < 0) 603 return devnum; 604 ret = device_bind_driver(parent, drv_name, name, &dev); 605 if (ret) 606 return ret; 607 desc = dev_get_uclass_platdata(dev); 608 desc->if_type = if_type; 609 desc->blksz = blksz; 610 desc->lba = size / blksz; 611 desc->part_type = PART_TYPE_UNKNOWN; 612 desc->bdev = dev; 613 desc->devnum = devnum; 614 *devp = dev; 615 616 return 0; 617 } 618 619 int blk_create_devicef(struct udevice *parent, const char *drv_name, 620 const char *name, int if_type, int devnum, int blksz, 621 lbaint_t size, struct udevice **devp) 622 { 623 char dev_name[30], *str; 624 int ret; 625 626 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name); 627 str = strdup(dev_name); 628 if (!str) 629 return -ENOMEM; 630 631 ret = blk_create_device(parent, drv_name, str, if_type, devnum, 632 blksz, size, devp); 633 if (ret) { 634 free(str); 635 return ret; 636 } 637 device_set_name_alloced(*devp); 638 639 return 0; 640 } 641 642 int blk_unbind_all(int if_type) 643 { 644 struct uclass *uc; 645 struct udevice *dev, *next; 646 int ret; 647 648 ret = uclass_get(UCLASS_BLK, &uc); 649 if (ret) 650 return ret; 651 uclass_foreach_dev_safe(dev, next, uc) { 652 struct blk_desc *desc = dev_get_uclass_platdata(dev); 653 654 if (desc->if_type == if_type) { 655 ret = device_remove(dev, DM_REMOVE_NORMAL); 656 if (ret) 657 return ret; 658 ret = device_unbind(dev); 659 if (ret) 660 return ret; 661 } 662 } 663 664 return 0; 665 } 666 667 UCLASS_DRIVER(blk) = { 668 .id = UCLASS_BLK, 669 .name = "blk", 670 .per_device_platdata_auto_alloc_size = sizeof(struct blk_desc), 671 }; 672