blk-uclass.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2016 Google, Inc
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #define LOG_CATEGORY UCLASS_BLK
  7. #include <common.h>
  8. #include <blk.h>
  9. #include <dm.h>
  10. #include <log.h>
  11. #include <malloc.h>
  12. #include <part.h>
  13. #include <dm/device-internal.h>
  14. #include <dm/lists.h>
  15. #include <dm/uclass-internal.h>
  16. #include <linux/err.h>
  17. static const char *if_typename_str[IF_TYPE_COUNT] = {
  18. [IF_TYPE_IDE] = "ide",
  19. [IF_TYPE_SCSI] = "scsi",
  20. [IF_TYPE_ATAPI] = "atapi",
  21. [IF_TYPE_USB] = "usb",
  22. [IF_TYPE_DOC] = "doc",
  23. [IF_TYPE_MMC] = "mmc",
  24. [IF_TYPE_SD] = "sd",
  25. [IF_TYPE_SATA] = "sata",
  26. [IF_TYPE_HOST] = "host",
  27. [IF_TYPE_NVME] = "nvme",
  28. [IF_TYPE_EFI] = "efi",
  29. [IF_TYPE_VIRTIO] = "virtio",
  30. [IF_TYPE_PVBLOCK] = "pvblock",
  31. };
  32. static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
  33. [IF_TYPE_IDE] = UCLASS_IDE,
  34. [IF_TYPE_SCSI] = UCLASS_SCSI,
  35. [IF_TYPE_ATAPI] = UCLASS_INVALID,
  36. [IF_TYPE_USB] = UCLASS_MASS_STORAGE,
  37. [IF_TYPE_DOC] = UCLASS_INVALID,
  38. [IF_TYPE_MMC] = UCLASS_MMC,
  39. [IF_TYPE_SD] = UCLASS_INVALID,
  40. [IF_TYPE_SATA] = UCLASS_AHCI,
  41. [IF_TYPE_HOST] = UCLASS_ROOT,
  42. [IF_TYPE_NVME] = UCLASS_NVME,
  43. [IF_TYPE_EFI] = UCLASS_EFI,
  44. [IF_TYPE_VIRTIO] = UCLASS_VIRTIO,
  45. [IF_TYPE_PVBLOCK] = UCLASS_PVBLOCK,
  46. };
  47. static enum if_type if_typename_to_iftype(const char *if_typename)
  48. {
  49. int i;
  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. return IF_TYPE_UNKNOWN;
  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. const char *blk_get_if_type_name(enum if_type if_type)
  62. {
  63. return if_typename_str[if_type];
  64. }
  65. struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum)
  66. {
  67. struct blk_desc *desc;
  68. struct udevice *dev;
  69. int ret;
  70. ret = blk_get_device(if_type, devnum, &dev);
  71. if (ret)
  72. return NULL;
  73. desc = dev_get_uclass_plat(dev);
  74. return desc;
  75. }
  76. /*
  77. * This function is complicated with driver model. We look up the interface
  78. * name in a local table. This gives us an interface type which we can match
  79. * against the uclass of the block device's parent.
  80. */
  81. struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum)
  82. {
  83. enum uclass_id uclass_id;
  84. enum if_type if_type;
  85. struct udevice *dev;
  86. struct uclass *uc;
  87. int ret;
  88. if_type = if_typename_to_iftype(if_typename);
  89. if (if_type == IF_TYPE_UNKNOWN) {
  90. debug("%s: Unknown interface type '%s'\n", __func__,
  91. if_typename);
  92. return NULL;
  93. }
  94. uclass_id = if_type_to_uclass_id(if_type);
  95. if (uclass_id == UCLASS_INVALID) {
  96. debug("%s: Unknown uclass for interface type'\n",
  97. if_typename_str[if_type]);
  98. return NULL;
  99. }
  100. ret = uclass_get(UCLASS_BLK, &uc);
  101. if (ret)
  102. return NULL;
  103. uclass_foreach_dev(dev, uc) {
  104. struct blk_desc *desc = dev_get_uclass_plat(dev);
  105. debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
  106. if_type, devnum, dev->name, desc->if_type, desc->devnum);
  107. if (desc->devnum != devnum)
  108. continue;
  109. /* Find out the parent device uclass */
  110. if (device_get_uclass_id(dev->parent) != uclass_id) {
  111. debug("%s: parent uclass %d, this dev %d\n", __func__,
  112. device_get_uclass_id(dev->parent), uclass_id);
  113. continue;
  114. }
  115. if (device_probe(dev))
  116. return NULL;
  117. debug("%s: Device desc %p\n", __func__, desc);
  118. return desc;
  119. }
  120. debug("%s: No device found\n", __func__);
  121. return NULL;
  122. }
  123. /**
  124. * blk_get_by_device() - Get the block device descriptor for the given device
  125. * @dev: Instance of a storage device
  126. *
  127. * Return: With block device descriptor on success , NULL if there is no such
  128. * block device.
  129. */
  130. struct blk_desc *blk_get_by_device(struct udevice *dev)
  131. {
  132. struct udevice *child_dev;
  133. device_foreach_child(child_dev, dev) {
  134. if (device_get_uclass_id(child_dev) != UCLASS_BLK)
  135. continue;
  136. return dev_get_uclass_plat(child_dev);
  137. }
  138. debug("%s: No block device found\n", __func__);
  139. return NULL;
  140. }
  141. /**
  142. * get_desc() - Get the block device descriptor for the given device number
  143. *
  144. * @if_type: Interface type
  145. * @devnum: Device number (0 = first)
  146. * @descp: Returns block device descriptor on success
  147. * @return 0 on success, -ENODEV if there is no such device and no device
  148. * with a higher device number, -ENOENT if there is no such device but there
  149. * is one with a higher number, or other -ve on other error.
  150. */
  151. static int get_desc(enum if_type if_type, int devnum, struct blk_desc **descp)
  152. {
  153. bool found_more = false;
  154. struct udevice *dev;
  155. struct uclass *uc;
  156. int ret;
  157. *descp = NULL;
  158. ret = uclass_get(UCLASS_BLK, &uc);
  159. if (ret)
  160. return ret;
  161. uclass_foreach_dev(dev, uc) {
  162. struct blk_desc *desc = dev_get_uclass_plat(dev);
  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. *descp = desc;
  171. return 0;
  172. } else if (desc->devnum > devnum) {
  173. found_more = true;
  174. }
  175. }
  176. }
  177. return found_more ? -ENOENT : -ENODEV;
  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. ret = blk_get_device(if_type, devnum, &dev);
  184. if (ret)
  185. return ret;
  186. return blk_select_hwpart(dev, hwpart);
  187. }
  188. int blk_list_part(enum if_type if_type)
  189. {
  190. struct blk_desc *desc;
  191. int devnum, ok;
  192. int ret;
  193. for (ok = 0, devnum = 0;; ++devnum) {
  194. ret = get_desc(if_type, devnum, &desc);
  195. if (ret == -ENODEV)
  196. break;
  197. else if (ret)
  198. continue;
  199. if (desc->part_type != PART_TYPE_UNKNOWN) {
  200. ++ok;
  201. if (devnum)
  202. putc('\n');
  203. part_print(desc);
  204. }
  205. }
  206. if (!ok)
  207. return -ENODEV;
  208. return 0;
  209. }
  210. int blk_print_part_devnum(enum if_type if_type, int devnum)
  211. {
  212. struct blk_desc *desc;
  213. int ret;
  214. ret = get_desc(if_type, devnum, &desc);
  215. if (ret)
  216. return ret;
  217. if (desc->type == DEV_TYPE_UNKNOWN)
  218. return -ENOENT;
  219. part_print(desc);
  220. return 0;
  221. }
  222. void blk_list_devices(enum if_type if_type)
  223. {
  224. struct blk_desc *desc;
  225. int ret;
  226. int i;
  227. for (i = 0;; ++i) {
  228. ret = get_desc(if_type, i, &desc);
  229. if (ret == -ENODEV)
  230. break;
  231. else if (ret)
  232. continue;
  233. if (desc->type == DEV_TYPE_UNKNOWN)
  234. continue; /* list only known devices */
  235. printf("Device %d: ", i);
  236. dev_print(desc);
  237. }
  238. }
  239. int blk_print_device_num(enum if_type if_type, int devnum)
  240. {
  241. struct blk_desc *desc;
  242. int ret;
  243. ret = get_desc(if_type, devnum, &desc);
  244. if (ret)
  245. return ret;
  246. printf("\nIDE device %d: ", devnum);
  247. dev_print(desc);
  248. return 0;
  249. }
  250. int blk_show_device(enum if_type if_type, int devnum)
  251. {
  252. struct blk_desc *desc;
  253. int ret;
  254. printf("\nDevice %d: ", devnum);
  255. ret = get_desc(if_type, devnum, &desc);
  256. if (ret == -ENODEV || ret == -ENOENT) {
  257. printf("unknown device\n");
  258. return -ENODEV;
  259. }
  260. if (ret)
  261. return ret;
  262. dev_print(desc);
  263. if (desc->type == DEV_TYPE_UNKNOWN)
  264. return -ENOENT;
  265. return 0;
  266. }
  267. ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
  268. lbaint_t blkcnt, void *buffer)
  269. {
  270. struct blk_desc *desc;
  271. ulong n;
  272. int ret;
  273. ret = get_desc(if_type, devnum, &desc);
  274. if (ret)
  275. return ret;
  276. n = blk_dread(desc, start, blkcnt, buffer);
  277. if (IS_ERR_VALUE(n))
  278. return n;
  279. return n;
  280. }
  281. ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
  282. lbaint_t blkcnt, const void *buffer)
  283. {
  284. struct blk_desc *desc;
  285. int ret;
  286. ret = get_desc(if_type, devnum, &desc);
  287. if (ret)
  288. return ret;
  289. return blk_dwrite(desc, start, blkcnt, buffer);
  290. }
  291. int blk_select_hwpart(struct udevice *dev, int hwpart)
  292. {
  293. const struct blk_ops *ops = blk_get_ops(dev);
  294. if (!ops)
  295. return -ENOSYS;
  296. if (!ops->select_hwpart)
  297. return 0;
  298. return ops->select_hwpart(dev, hwpart);
  299. }
  300. int blk_dselect_hwpart(struct blk_desc *desc, int hwpart)
  301. {
  302. return blk_select_hwpart(desc->bdev, hwpart);
  303. }
  304. int blk_first_device(int if_type, struct udevice **devp)
  305. {
  306. struct blk_desc *desc;
  307. int ret;
  308. ret = uclass_find_first_device(UCLASS_BLK, devp);
  309. if (ret)
  310. return ret;
  311. if (!*devp)
  312. return -ENODEV;
  313. do {
  314. desc = dev_get_uclass_plat(*devp);
  315. if (desc->if_type == if_type)
  316. return 0;
  317. ret = uclass_find_next_device(devp);
  318. if (ret)
  319. return ret;
  320. } while (*devp);
  321. return -ENODEV;
  322. }
  323. int blk_next_device(struct udevice **devp)
  324. {
  325. struct blk_desc *desc;
  326. int ret, if_type;
  327. desc = dev_get_uclass_plat(*devp);
  328. if_type = desc->if_type;
  329. do {
  330. ret = uclass_find_next_device(devp);
  331. if (ret)
  332. return ret;
  333. if (!*devp)
  334. return -ENODEV;
  335. desc = dev_get_uclass_plat(*devp);
  336. if (desc->if_type == if_type)
  337. return 0;
  338. } while (1);
  339. }
  340. int blk_find_device(int if_type, int devnum, struct udevice **devp)
  341. {
  342. struct uclass *uc;
  343. struct udevice *dev;
  344. int ret;
  345. ret = uclass_get(UCLASS_BLK, &uc);
  346. if (ret)
  347. return ret;
  348. uclass_foreach_dev(dev, uc) {
  349. struct blk_desc *desc = dev_get_uclass_plat(dev);
  350. debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
  351. if_type, devnum, dev->name, desc->if_type, desc->devnum);
  352. if (desc->if_type == if_type && desc->devnum == devnum) {
  353. *devp = dev;
  354. return 0;
  355. }
  356. }
  357. return -ENODEV;
  358. }
  359. int blk_get_device(int if_type, int devnum, struct udevice **devp)
  360. {
  361. int ret;
  362. ret = blk_find_device(if_type, devnum, devp);
  363. if (ret)
  364. return ret;
  365. return device_probe(*devp);
  366. }
  367. unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
  368. lbaint_t blkcnt, void *buffer)
  369. {
  370. struct udevice *dev = block_dev->bdev;
  371. const struct blk_ops *ops = blk_get_ops(dev);
  372. ulong blks_read;
  373. if (!ops->read)
  374. return -ENOSYS;
  375. if (blkcache_read(block_dev->if_type, block_dev->devnum,
  376. start, blkcnt, block_dev->blksz, buffer))
  377. return blkcnt;
  378. blks_read = ops->read(dev, start, blkcnt, buffer);
  379. if (blks_read == blkcnt)
  380. blkcache_fill(block_dev->if_type, block_dev->devnum,
  381. start, blkcnt, block_dev->blksz, buffer);
  382. return blks_read;
  383. }
  384. unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
  385. lbaint_t blkcnt, const void *buffer)
  386. {
  387. struct udevice *dev = block_dev->bdev;
  388. const struct blk_ops *ops = blk_get_ops(dev);
  389. if (!ops->write)
  390. return -ENOSYS;
  391. blkcache_invalidate(block_dev->if_type, block_dev->devnum);
  392. return ops->write(dev, start, blkcnt, buffer);
  393. }
  394. unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
  395. lbaint_t blkcnt)
  396. {
  397. struct udevice *dev = block_dev->bdev;
  398. const struct blk_ops *ops = blk_get_ops(dev);
  399. if (!ops->erase)
  400. return -ENOSYS;
  401. blkcache_invalidate(block_dev->if_type, block_dev->devnum);
  402. return ops->erase(dev, start, blkcnt);
  403. }
  404. int blk_get_from_parent(struct udevice *parent, struct udevice **devp)
  405. {
  406. struct udevice *dev;
  407. enum uclass_id id;
  408. int ret;
  409. device_find_first_child(parent, &dev);
  410. if (!dev) {
  411. debug("%s: No block device found for parent '%s'\n", __func__,
  412. parent->name);
  413. return -ENODEV;
  414. }
  415. id = device_get_uclass_id(dev);
  416. if (id != UCLASS_BLK) {
  417. debug("%s: Incorrect uclass %s for block device '%s'\n",
  418. __func__, uclass_get_name(id), dev->name);
  419. return -ENOTBLK;
  420. }
  421. ret = device_probe(dev);
  422. if (ret)
  423. return ret;
  424. *devp = dev;
  425. return 0;
  426. }
  427. int blk_find_max_devnum(enum if_type if_type)
  428. {
  429. struct udevice *dev;
  430. int max_devnum = -ENODEV;
  431. struct uclass *uc;
  432. int ret;
  433. ret = uclass_get(UCLASS_BLK, &uc);
  434. if (ret)
  435. return ret;
  436. uclass_foreach_dev(dev, uc) {
  437. struct blk_desc *desc = dev_get_uclass_plat(dev);
  438. if (desc->if_type == if_type && desc->devnum > max_devnum)
  439. max_devnum = desc->devnum;
  440. }
  441. return max_devnum;
  442. }
  443. int blk_next_free_devnum(enum if_type if_type)
  444. {
  445. int ret;
  446. ret = blk_find_max_devnum(if_type);
  447. if (ret == -ENODEV)
  448. return 0;
  449. if (ret < 0)
  450. return ret;
  451. return ret + 1;
  452. }
  453. static int blk_flags_check(struct udevice *dev, enum blk_flag_t req_flags)
  454. {
  455. const struct blk_desc *desc = dev_get_uclass_plat(dev);
  456. enum blk_flag_t flags;
  457. flags = desc->removable ? BLKF_REMOVABLE : BLKF_FIXED;
  458. return flags & req_flags ? 0 : 1;
  459. }
  460. int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp)
  461. {
  462. int ret;
  463. for (ret = uclass_first_device_err(UCLASS_BLK, devp);
  464. !ret;
  465. ret = uclass_next_device_err(devp)) {
  466. if (!blk_flags_check(*devp, flags))
  467. return 0;
  468. }
  469. return -ENODEV;
  470. }
  471. int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp)
  472. {
  473. int ret;
  474. for (ret = uclass_next_device_err(devp);
  475. !ret;
  476. ret = uclass_next_device_err(devp)) {
  477. if (!blk_flags_check(*devp, flags))
  478. return 0;
  479. }
  480. return -ENODEV;
  481. }
  482. int blk_count_devices(enum blk_flag_t flag)
  483. {
  484. struct udevice *dev;
  485. int count = 0;
  486. blk_foreach_probe(flag, dev)
  487. count++;
  488. return count;
  489. }
  490. static int blk_claim_devnum(enum if_type if_type, int devnum)
  491. {
  492. struct udevice *dev;
  493. struct uclass *uc;
  494. int ret;
  495. ret = uclass_get(UCLASS_BLK, &uc);
  496. if (ret)
  497. return ret;
  498. uclass_foreach_dev(dev, uc) {
  499. struct blk_desc *desc = dev_get_uclass_plat(dev);
  500. if (desc->if_type == if_type && desc->devnum == devnum) {
  501. int next = blk_next_free_devnum(if_type);
  502. if (next < 0)
  503. return next;
  504. desc->devnum = next;
  505. return 0;
  506. }
  507. }
  508. return -ENOENT;
  509. }
  510. int blk_create_device(struct udevice *parent, const char *drv_name,
  511. const char *name, int if_type, int devnum, int blksz,
  512. lbaint_t lba, struct udevice **devp)
  513. {
  514. struct blk_desc *desc;
  515. struct udevice *dev;
  516. int ret;
  517. if (devnum == -1) {
  518. devnum = blk_next_free_devnum(if_type);
  519. } else {
  520. ret = blk_claim_devnum(if_type, devnum);
  521. if (ret < 0 && ret != -ENOENT)
  522. return ret;
  523. }
  524. if (devnum < 0)
  525. return devnum;
  526. ret = device_bind_driver(parent, drv_name, name, &dev);
  527. if (ret)
  528. return ret;
  529. desc = dev_get_uclass_plat(dev);
  530. desc->if_type = if_type;
  531. desc->blksz = blksz;
  532. desc->log2blksz = LOG2(desc->blksz);
  533. desc->lba = lba;
  534. desc->part_type = PART_TYPE_UNKNOWN;
  535. desc->bdev = dev;
  536. desc->devnum = devnum;
  537. *devp = dev;
  538. return 0;
  539. }
  540. int blk_create_devicef(struct udevice *parent, const char *drv_name,
  541. const char *name, int if_type, int devnum, int blksz,
  542. lbaint_t lba, struct udevice **devp)
  543. {
  544. char dev_name[30], *str;
  545. int ret;
  546. snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
  547. str = strdup(dev_name);
  548. if (!str)
  549. return -ENOMEM;
  550. ret = blk_create_device(parent, drv_name, str, if_type, devnum,
  551. blksz, lba, devp);
  552. if (ret) {
  553. free(str);
  554. return ret;
  555. }
  556. device_set_name_alloced(*devp);
  557. return 0;
  558. }
  559. int blk_unbind_all(int if_type)
  560. {
  561. struct uclass *uc;
  562. struct udevice *dev, *next;
  563. int ret;
  564. ret = uclass_get(UCLASS_BLK, &uc);
  565. if (ret)
  566. return ret;
  567. uclass_foreach_dev_safe(dev, next, uc) {
  568. struct blk_desc *desc = dev_get_uclass_plat(dev);
  569. if (desc->if_type == if_type) {
  570. ret = device_remove(dev, DM_REMOVE_NORMAL);
  571. if (ret)
  572. return ret;
  573. ret = device_unbind(dev);
  574. if (ret)
  575. return ret;
  576. }
  577. }
  578. return 0;
  579. }
  580. static int blk_post_probe(struct udevice *dev)
  581. {
  582. if (IS_ENABLED(CONFIG_PARTITIONS) &&
  583. IS_ENABLED(CONFIG_HAVE_BLOCK_DEVICE)) {
  584. struct blk_desc *desc = dev_get_uclass_plat(dev);
  585. part_init(desc);
  586. }
  587. return 0;
  588. }
  589. UCLASS_DRIVER(blk) = {
  590. .id = UCLASS_BLK,
  591. .name = "blk",
  592. .post_probe = blk_post_probe,
  593. .per_device_plat_auto = sizeof(struct blk_desc),
  594. };