api_storage.c 7.6 KB

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