api_storage.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2007-2008 Semihalf
  4. *
  5. * Written by: Rafal Jaworowski <raj@semihalf.com>
  6. */
  7. #include <config.h>
  8. #include <common.h>
  9. #include <api_public.h>
  10. #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
  11. #include <usb.h>
  12. #endif
  13. #define DEBUG
  14. #undef DEBUG
  15. #ifdef DEBUG
  16. #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt, ##args); } while (0)
  17. #else
  18. #define debugf(fmt, args...)
  19. #endif
  20. #define errf(fmt, args...) do { printf("ERROR @ %s(): ", __func__); printf(fmt, ##args); } while (0)
  21. #define ENUM_IDE 0
  22. #define ENUM_USB 1
  23. #define ENUM_SCSI 2
  24. #define ENUM_MMC 3
  25. #define ENUM_SATA 4
  26. #define ENUM_MAX 5
  27. struct stor_spec {
  28. int max_dev;
  29. int enum_started;
  30. int enum_ended;
  31. int type; /* "external" type: DT_STOR_{IDE,USB,etc} */
  32. char *name;
  33. };
  34. static struct stor_spec specs[ENUM_MAX] = { { 0, 0, 0, 0, NULL }, };
  35. #ifndef CONFIG_SYS_MMC_MAX_DEVICE
  36. #define CONFIG_SYS_MMC_MAX_DEVICE 1
  37. #endif
  38. void dev_stor_init(void)
  39. {
  40. #if defined(CONFIG_IDE)
  41. specs[ENUM_IDE].max_dev = CONFIG_SYS_IDE_MAXDEVICE;
  42. specs[ENUM_IDE].enum_started = 0;
  43. specs[ENUM_IDE].enum_ended = 0;
  44. specs[ENUM_IDE].type = DEV_TYP_STOR | DT_STOR_IDE;
  45. specs[ENUM_IDE].name = "ide";
  46. #endif
  47. #if defined(CONFIG_CMD_MMC)
  48. specs[ENUM_MMC].max_dev = CONFIG_SYS_MMC_MAX_DEVICE;
  49. specs[ENUM_MMC].enum_started = 0;
  50. specs[ENUM_MMC].enum_ended = 0;
  51. specs[ENUM_MMC].type = DEV_TYP_STOR | DT_STOR_MMC;
  52. specs[ENUM_MMC].name = "mmc";
  53. #endif
  54. #if defined(CONFIG_SATA)
  55. specs[ENUM_SATA].max_dev = CONFIG_SYS_SATA_MAX_DEVICE;
  56. specs[ENUM_SATA].enum_started = 0;
  57. specs[ENUM_SATA].enum_ended = 0;
  58. specs[ENUM_SATA].type = DEV_TYP_STOR | DT_STOR_SATA;
  59. specs[ENUM_SATA].name = "sata";
  60. #endif
  61. #if defined(CONFIG_SCSI)
  62. specs[ENUM_SCSI].max_dev = CONFIG_SYS_SCSI_MAX_DEVICE;
  63. specs[ENUM_SCSI].enum_started = 0;
  64. specs[ENUM_SCSI].enum_ended = 0;
  65. specs[ENUM_SCSI].type = DEV_TYP_STOR | DT_STOR_SCSI;
  66. specs[ENUM_SCSI].name = "scsi";
  67. #endif
  68. #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
  69. specs[ENUM_USB].max_dev = USB_MAX_STOR_DEV;
  70. specs[ENUM_USB].enum_started = 0;
  71. specs[ENUM_USB].enum_ended = 0;
  72. specs[ENUM_USB].type = DEV_TYP_STOR | DT_STOR_USB;
  73. specs[ENUM_USB].name = "usb";
  74. #endif
  75. }
  76. /*
  77. * Finds next available device in the storage group
  78. *
  79. * type: storage group type - ENUM_IDE, ENUM_SCSI etc.
  80. *
  81. * more: returns 0/1 depending if there are more devices in this group
  82. * available (for future iterations)
  83. *
  84. * returns: 0/1 depending if device found in this iteration
  85. */
  86. static int dev_stor_get(int type, int *more, struct device_info *di)
  87. {
  88. struct blk_desc *dd;
  89. int found = 0;
  90. int i = 0;
  91. /* Wasn't configured for this type, return 0 directly */
  92. if (specs[type].name == NULL)
  93. return 0;
  94. if (di->cookie != NULL) {
  95. /* Find the last device we've returned */
  96. for (i = 0; i < specs[type].max_dev; i++) {
  97. if (di->cookie ==
  98. (void *)blk_get_dev(specs[type].name, i)) {
  99. i += 1;
  100. break;
  101. }
  102. }
  103. }
  104. for (; i < specs[type].max_dev; i++) {
  105. di->cookie = (void *)blk_get_dev(specs[type].name, i);
  106. if (di->cookie != NULL) {
  107. found = 1;
  108. break;
  109. }
  110. }
  111. if (i == specs[type].max_dev)
  112. *more = 0;
  113. else
  114. *more = 1;
  115. if (found) {
  116. di->type = specs[type].type;
  117. dd = (struct blk_desc *)di->cookie;
  118. if (dd->type == DEV_TYPE_UNKNOWN) {
  119. debugf("device instance exists, but is not active..");
  120. found = 0;
  121. } else {
  122. di->di_stor.block_count = dd->lba;
  123. di->di_stor.block_size = dd->blksz;
  124. }
  125. } else {
  126. di->cookie = NULL;
  127. }
  128. return found;
  129. }
  130. /* returns: ENUM_IDE, ENUM_USB etc. based on struct blk_desc */
  131. static int dev_stor_type(struct blk_desc *dd)
  132. {
  133. int i, j;
  134. for (i = ENUM_IDE; i < ENUM_MAX; i++)
  135. for (j = 0; j < specs[i].max_dev; j++)
  136. if (dd == blk_get_dev(specs[i].name, j))
  137. return i;
  138. return ENUM_MAX;
  139. }
  140. /* returns: 0/1 whether cookie points to some device in this group */
  141. static int dev_is_stor(int type, struct device_info *di)
  142. {
  143. return (dev_stor_type(di->cookie) == type) ? 1 : 0;
  144. }
  145. static int dev_enum_stor(int type, struct device_info *di)
  146. {
  147. int found = 0, more = 0;
  148. debugf("called, type %d\n", type);
  149. /*
  150. * Formulae for enumerating storage devices:
  151. * 1. if cookie (hint from previous enum call) is NULL we start again
  152. * with enumeration, so return the first available device, done.
  153. *
  154. * 2. if cookie is not NULL, check if it identifies some device in
  155. * this group:
  156. *
  157. * 2a. if cookie is a storage device from our group (IDE, USB etc.),
  158. * return next available (if exists) in this group
  159. *
  160. * 2b. if it isn't device from our group, check if such devices were
  161. * ever enumerated before:
  162. * - if not, return the first available device from this group
  163. * - else return 0
  164. */
  165. if (di->cookie == NULL) {
  166. debugf("group%d - enum restart\n", type);
  167. /*
  168. * 1. Enumeration (re-)started: take the first available
  169. * device, if exists
  170. */
  171. found = dev_stor_get(type, &more, di);
  172. specs[type].enum_started = 1;
  173. } else if (dev_is_stor(type, di)) {
  174. debugf("group%d - enum continued for the next device\n", type);
  175. if (specs[type].enum_ended) {
  176. debugf("group%d - nothing more to enum!\n", type);
  177. return 0;
  178. }
  179. /* 2a. Attempt to take a next available device in the group */
  180. found = dev_stor_get(type, &more, di);
  181. } else {
  182. if (specs[type].enum_ended) {
  183. debugf("group %d - already enumerated, skipping\n", type);
  184. return 0;
  185. }
  186. debugf("group%d - first time enum\n", type);
  187. if (specs[type].enum_started == 0) {
  188. /*
  189. * 2b. If enumerating devices in this group did not
  190. * happen before, it means the cookie pointed to a
  191. * device from some other group (another storage
  192. * group, or network); in this case try to take the
  193. * first available device from our group
  194. */
  195. specs[type].enum_started = 1;
  196. /*
  197. * Attempt to take the first device in this group:
  198. *'first element' flag is set
  199. */
  200. found = dev_stor_get(type, &more, di);
  201. } else {
  202. errf("group%d - out of order iteration\n", type);
  203. found = 0;
  204. more = 0;
  205. }
  206. }
  207. /*
  208. * If there are no more devices in this group, consider its
  209. * enumeration finished
  210. */
  211. specs[type].enum_ended = (!more) ? 1 : 0;
  212. if (found)
  213. debugf("device found, returning cookie 0x%08x\n",
  214. (u_int32_t)di->cookie);
  215. else
  216. debugf("no device found\n");
  217. return found;
  218. }
  219. void dev_enum_reset(void)
  220. {
  221. int i;
  222. for (i = 0; i < ENUM_MAX; i ++) {
  223. specs[i].enum_started = 0;
  224. specs[i].enum_ended = 0;
  225. }
  226. }
  227. int dev_enum_storage(struct device_info *di)
  228. {
  229. int i;
  230. /* check: ide, usb, scsi, mmc */
  231. for (i = ENUM_IDE; i < ENUM_MAX; i ++) {
  232. if (dev_enum_stor(i, di))
  233. return 1;
  234. }
  235. return 0;
  236. }
  237. static int dev_stor_is_valid(int type, struct blk_desc *dd)
  238. {
  239. int i;
  240. for (i = 0; i < specs[type].max_dev; i++)
  241. if (dd == blk_get_dev(specs[type].name, i))
  242. if (dd->type != DEV_TYPE_UNKNOWN)
  243. return 1;
  244. return 0;
  245. }
  246. int dev_open_stor(void *cookie)
  247. {
  248. int type = dev_stor_type(cookie);
  249. if (type == ENUM_MAX)
  250. return API_ENODEV;
  251. if (dev_stor_is_valid(type, (struct blk_desc *)cookie))
  252. return 0;
  253. return API_ENODEV;
  254. }
  255. int dev_close_stor(void *cookie)
  256. {
  257. /*
  258. * Not much to do as we actually do not alter storage devices upon
  259. * close
  260. */
  261. return 0;
  262. }
  263. lbasize_t dev_read_stor(void *cookie, void *buf, lbasize_t len, lbastart_t start)
  264. {
  265. int type;
  266. struct blk_desc *dd = (struct blk_desc *)cookie;
  267. if ((type = dev_stor_type(dd)) == ENUM_MAX)
  268. return 0;
  269. if (!dev_stor_is_valid(type, dd))
  270. return 0;
  271. #ifdef CONFIG_BLK
  272. return blk_dread(dd, start, len, buf);
  273. #else
  274. if ((dd->block_read) == NULL) {
  275. debugf("no block_read() for device 0x%08x\n", cookie);
  276. return 0;
  277. }
  278. return dd->block_read(dd, start, len, buf);
  279. #endif /* defined(CONFIG_BLK) */
  280. }