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