blk-uclass.c 14 KB

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