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 14 static const char *if_typename_str[IF_TYPE_COUNT] = { 15 [IF_TYPE_IDE] = "ide", 16 [IF_TYPE_SCSI] = "scsi", 17 [IF_TYPE_ATAPI] = "atapi", 18 [IF_TYPE_USB] = "usb", 19 [IF_TYPE_DOC] = "doc", 20 [IF_TYPE_MMC] = "mmc", 21 [IF_TYPE_SD] = "sd", 22 [IF_TYPE_SATA] = "sata", 23 [IF_TYPE_HOST] = "host", 24 [IF_TYPE_SYSTEMACE] = "ace", 25 [IF_TYPE_NVME] = "nvme", 26 [IF_TYPE_RKNAND] = "rknand", 27 }; 28 29 static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = { 30 [IF_TYPE_IDE] = UCLASS_IDE, 31 [IF_TYPE_SCSI] = UCLASS_SCSI, 32 [IF_TYPE_ATAPI] = UCLASS_INVALID, 33 [IF_TYPE_USB] = UCLASS_MASS_STORAGE, 34 [IF_TYPE_DOC] = UCLASS_INVALID, 35 [IF_TYPE_MMC] = UCLASS_MMC, 36 [IF_TYPE_SD] = UCLASS_INVALID, 37 [IF_TYPE_SATA] = UCLASS_AHCI, 38 [IF_TYPE_HOST] = UCLASS_ROOT, 39 [IF_TYPE_NVME] = UCLASS_NVME, 40 [IF_TYPE_RKNAND] = UCLASS_RKNAND, 41 [IF_TYPE_SYSTEMACE] = UCLASS_INVALID, 42 }; 43 44 static enum if_type if_typename_to_iftype(const char *if_typename) 45 { 46 int i; 47 48 for (i = 0; i < IF_TYPE_COUNT; i++) { 49 if (if_typename_str[i] && 50 !strcmp(if_typename, if_typename_str[i])) 51 return i; 52 } 53 54 return IF_TYPE_UNKNOWN; 55 } 56 57 static enum uclass_id if_type_to_uclass_id(enum if_type if_type) 58 { 59 return if_type_uclass_id[if_type]; 60 } 61 62 const char *blk_get_if_type_name(enum if_type if_type) 63 { 64 return if_typename_str[if_type]; 65 } 66 67 struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum) 68 { 69 struct blk_desc *desc; 70 struct udevice *dev; 71 int ret; 72 73 ret = blk_get_device(if_type, devnum, &dev); 74 if (ret) 75 return NULL; 76 desc = dev_get_uclass_platdata(dev); 77 78 return desc; 79 } 80 81 /* 82 * This function is complicated with driver model. We look up the interface 83 * name in a local table. This gives us an interface type which we can match 84 * against the uclass of the block device's parent. 85 */ 86 struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum) 87 { 88 enum uclass_id uclass_id; 89 enum if_type if_type; 90 struct udevice *dev; 91 struct uclass *uc; 92 int ret; 93 94 if_type = if_typename_to_iftype(if_typename); 95 if (if_type == IF_TYPE_UNKNOWN) { 96 debug("%s: Unknown interface type '%s'\n", __func__, 97 if_typename); 98 return NULL; 99 } 100 uclass_id = if_type_to_uclass_id(if_type); 101 if (uclass_id == UCLASS_INVALID) { 102 debug("%s: Unknown uclass for interface type'\n", 103 if_typename_str[if_type]); 104 return NULL; 105 } 106 107 ret = uclass_get(UCLASS_BLK, &uc); 108 if (ret) 109 return NULL; 110 uclass_foreach_dev(dev, uc) { 111 struct blk_desc *desc = dev_get_uclass_platdata(dev); 112 113 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__, 114 if_type, devnum, dev->name, desc->if_type, desc->devnum); 115 if (desc->devnum != devnum) 116 continue; 117 118 /* Find out the parent device uclass */ 119 if (device_get_uclass_id(dev->parent) != uclass_id) { 120 debug("%s: parent uclass %d, this dev %d\n", __func__, 121 device_get_uclass_id(dev->parent), uclass_id); 122 continue; 123 } 124 125 if (device_probe(dev)) 126 return NULL; 127 128 debug("%s: Device desc %p\n", __func__, desc); 129 return desc; 130 } 131 debug("%s: No device found\n", __func__); 132 133 return NULL; 134 } 135 136 /** 137 * get_desc() - Get the block device descriptor for the given device number 138 * 139 * @if_type: Interface type 140 * @devnum: Device number (0 = first) 141 * @descp: Returns block device descriptor on success 142 * @return 0 on success, -ENODEV if there is no such device and no device 143 * with a higher device number, -ENOENT if there is no such device but there 144 * is one with a higher number, or other -ve on other error. 145 */ 146 static int get_desc(enum if_type if_type, int devnum, struct blk_desc **descp) 147 { 148 bool found_more = false; 149 struct udevice *dev; 150 struct uclass *uc; 151 int ret; 152 153 *descp = NULL; 154 ret = uclass_get(UCLASS_BLK, &uc); 155 if (ret) 156 return ret; 157 uclass_foreach_dev(dev, uc) { 158 struct blk_desc *desc = dev_get_uclass_platdata(dev); 159 160 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__, 161 if_type, devnum, dev->name, desc->if_type, desc->devnum); 162 if (desc->if_type == if_type) { 163 if (desc->devnum == devnum) { 164 ret = device_probe(dev); 165 if (ret) 166 return ret; 167 168 *descp = desc; 169 return 0; 170 } else if (desc->devnum > devnum) { 171 found_more = true; 172 } 173 } 174 } 175 176 return found_more ? -ENOENT : -ENODEV; 177 } 178 179 int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart) 180 { 181 struct udevice *dev; 182 int ret; 183 184 ret = blk_get_device(if_type, devnum, &dev); 185 if (ret) 186 return ret; 187 188 return blk_select_hwpart(dev, hwpart); 189 } 190 191 int blk_list_part(enum if_type if_type) 192 { 193 struct blk_desc *desc; 194 int devnum, ok; 195 int ret; 196 197 for (ok = 0, devnum = 0;; ++devnum) { 198 ret = get_desc(if_type, devnum, &desc); 199 if (ret == -ENODEV) 200 break; 201 else if (ret) 202 continue; 203 if (desc->part_type != PART_TYPE_UNKNOWN) { 204 ++ok; 205 if (devnum) 206 putc('\n'); 207 part_print(desc); 208 } 209 } 210 if (!ok) 211 return -ENODEV; 212 213 return 0; 214 } 215 216 int blk_print_part_devnum(enum if_type if_type, int devnum) 217 { 218 struct blk_desc *desc; 219 int ret; 220 221 ret = get_desc(if_type, devnum, &desc); 222 if (ret) 223 return ret; 224 if (desc->type == DEV_TYPE_UNKNOWN) 225 return -ENOENT; 226 part_print(desc); 227 228 return 0; 229 } 230 231 void blk_list_devices(enum if_type if_type) 232 { 233 struct blk_desc *desc; 234 int ret; 235 int i; 236 237 for (i = 0;; ++i) { 238 ret = get_desc(if_type, i, &desc); 239 if (ret == -ENODEV) 240 break; 241 else if (ret) 242 continue; 243 if (desc->type == DEV_TYPE_UNKNOWN) 244 continue; /* list only known devices */ 245 printf("Device %d: ", i); 246 dev_print(desc); 247 } 248 } 249 250 int blk_print_device_num(enum if_type if_type, int devnum) 251 { 252 struct blk_desc *desc; 253 int ret; 254 255 ret = get_desc(if_type, devnum, &desc); 256 if (ret) 257 return ret; 258 printf("\nIDE device %d: ", devnum); 259 dev_print(desc); 260 261 return 0; 262 } 263 264 int blk_show_device(enum if_type if_type, int devnum) 265 { 266 struct blk_desc *desc; 267 int ret; 268 269 printf("\nDevice %d: ", devnum); 270 ret = get_desc(if_type, devnum, &desc); 271 if (ret == -ENODEV || ret == -ENOENT) { 272 printf("unknown device\n"); 273 return -ENODEV; 274 } 275 if (ret) 276 return ret; 277 dev_print(desc); 278 279 if (desc->type == DEV_TYPE_UNKNOWN) 280 return -ENOENT; 281 282 return 0; 283 } 284 285 ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start, 286 lbaint_t blkcnt, void *buffer) 287 { 288 struct blk_desc *desc; 289 ulong n; 290 int ret; 291 292 ret = get_desc(if_type, devnum, &desc); 293 if (ret) 294 return ret; 295 n = blk_dread(desc, start, blkcnt, buffer); 296 if (IS_ERR_VALUE(n)) 297 return n; 298 299 /* flush cache after read */ 300 flush_cache((ulong)buffer, blkcnt * desc->blksz); 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_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_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_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