api_storage.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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, "" }, };
  36. void dev_stor_init(void)
  37. {
  38. #if defined(CONFIG_CMD_IDE)
  39. specs[ENUM_IDE].max_dev = CONFIG_SYS_IDE_MAXDEVICE;
  40. specs[ENUM_IDE].enum_started = 0;
  41. specs[ENUM_IDE].enum_ended = 0;
  42. specs[ENUM_IDE].type = DEV_TYP_STOR | DT_STOR_IDE;
  43. specs[ENUM_IDE].name = "ide";
  44. #endif
  45. #if defined(CONFIG_CMD_MMC)
  46. specs[ENUM_MMC].max_dev = CONFIG_SYS_MMC_MAX_DEVICE;
  47. specs[ENUM_MMC].enum_started = 0;
  48. specs[ENUM_MMC].enum_ended = 0;
  49. specs[ENUM_MMC].type = DEV_TYP_STOR | DT_STOR_MMC;
  50. specs[ENUM_MMC].name = "mmc";
  51. #endif
  52. #if defined(CONFIG_CMD_SATA)
  53. specs[ENUM_SATA].max_dev = CONFIG_SYS_SATA_MAX_DEVICE;
  54. specs[ENUM_SATA].enum_started = 0;
  55. specs[ENUM_SATA].enum_ended = 0;
  56. specs[ENUM_SATA].type = DEV_TYP_STOR | DT_STOR_SATA;
  57. specs[ENUM_SATA].name = "sata";
  58. #endif
  59. #if defined(CONFIG_CMD_SCSI)
  60. specs[ENUM_SCSI].max_dev = CONFIG_SYS_SCSI_MAX_DEVICE;
  61. specs[ENUM_SCSI].enum_started = 0;
  62. specs[ENUM_SCSI].enum_ended = 0;
  63. specs[ENUM_SCSI].type = DEV_TYP_STOR | DT_STOR_SCSI;
  64. specs[ENUM_SCSI].name = "scsi";
  65. #endif
  66. #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
  67. specs[ENUM_USB].max_dev = USB_MAX_STOR_DEV;
  68. specs[ENUM_USB].enum_started = 0;
  69. specs[ENUM_USB].enum_ended = 0;
  70. specs[ENUM_USB].type = DEV_TYP_STOR | DT_STOR_USB;
  71. specs[ENUM_USB].name = "usb";
  72. #endif
  73. }
  74. /*
  75. * Finds next available device in the storage group
  76. *
  77. * type: storage group type - ENUM_IDE, ENUM_SCSI etc.
  78. *
  79. * first: if 1 the first device in the storage group is returned (if
  80. * exists), if 0 the next available device is searched
  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 first, int *more, struct device_info *di)
  88. {
  89. int found = 0;
  90. *more = 0;
  91. int i;
  92. block_dev_desc_t *dd;
  93. if (first) {
  94. di->cookie = (void *)get_dev(specs[type].name, 0);
  95. if (di->cookie == NULL)
  96. return 0;
  97. else
  98. found = 1;
  99. } else {
  100. for (i = 0; i < specs[type].max_dev; i++)
  101. if (di->cookie == (void *)get_dev(specs[type].name, i)) {
  102. /* previous cookie found -- advance to the
  103. * next device, if possible */
  104. if (++i >= specs[type].max_dev) {
  105. /* out of range, no more to enum */
  106. di->cookie = NULL;
  107. break;
  108. }
  109. di->cookie = (void *)get_dev(specs[type].name, i);
  110. if (di->cookie == NULL)
  111. return 0;
  112. else
  113. found = 1;
  114. /* provide hint if there are more devices in
  115. * this group to enumerate */
  116. if ((i + 1) < specs[type].max_dev)
  117. *more = 1;
  118. break;
  119. }
  120. }
  121. if (found) {
  122. di->type = specs[type].type;
  123. if (di->cookie != NULL) {
  124. dd = (block_dev_desc_t *)di->cookie;
  125. if (dd->type == DEV_TYPE_UNKNOWN) {
  126. debugf("device instance exists, but is not active..");
  127. found = 0;
  128. } else {
  129. di->di_stor.block_count = dd->lba;
  130. di->di_stor.block_size = dd->blksz;
  131. }
  132. }
  133. } else
  134. di->cookie = NULL;
  135. return found;
  136. }
  137. /*
  138. * returns: ENUM_IDE, ENUM_USB etc. based on block_dev_desc_t
  139. */
  140. static int dev_stor_type(block_dev_desc_t *dd)
  141. {
  142. int i, j;
  143. for (i = ENUM_IDE; i < ENUM_MAX; i++)
  144. for (j = 0; j < specs[i].max_dev; j++)
  145. if (dd == get_dev(specs[i].name, j))
  146. return i;
  147. return ENUM_MAX;
  148. }
  149. /*
  150. * returns: 0/1 whether cookie points to some device in this group
  151. */
  152. static int dev_is_stor(int type, struct device_info *di)
  153. {
  154. return (dev_stor_type(di->cookie) == type) ? 1 : 0;
  155. }
  156. static int dev_enum_stor(int type, struct device_info *di)
  157. {
  158. int found = 0, more = 0;
  159. debugf("called, type %d\n", type);
  160. /*
  161. * Formulae for enumerating storage devices:
  162. * 1. if cookie (hint from previous enum call) is NULL we start again
  163. * with enumeration, so return the first available device, done.
  164. *
  165. * 2. if cookie is not NULL, check if it identifies some device in
  166. * this group:
  167. *
  168. * 2a. if cookie is a storage device from our group (IDE, USB etc.),
  169. * return next available (if exists) in this group
  170. *
  171. * 2b. if it isn't device from our group, check if such devices were
  172. * ever enumerated before:
  173. * - if not, return the first available device from this group
  174. * - else return 0
  175. */
  176. if (di->cookie == NULL) {
  177. debugf("group%d - enum restart\n", type);
  178. /*
  179. * 1. Enumeration (re-)started: take the first available
  180. * device, if exists
  181. */
  182. found = dev_stor_get(type, 1, &more, di);
  183. specs[type].enum_started = 1;
  184. } else if (dev_is_stor(type, di)) {
  185. debugf("group%d - enum continued for the next device\n", type);
  186. if (specs[type].enum_ended) {
  187. debugf("group%d - nothing more to enum!\n", type);
  188. return 0;
  189. }
  190. /* 2a. Attempt to take a next available device in the group */
  191. found = dev_stor_get(type, 0, &more, di);
  192. } else {
  193. if (specs[type].enum_ended) {
  194. debugf("group %d - already enumerated, skipping\n", type);
  195. return 0;
  196. }
  197. debugf("group%d - first time enum\n", type);
  198. if (specs[type].enum_started == 0) {
  199. /*
  200. * 2b. If enumerating devices in this group did not
  201. * happen before, it means the cookie pointed to a
  202. * device frome some other group (another storage
  203. * group, or network); in this case try to take the
  204. * first available device from our group
  205. */
  206. specs[type].enum_started = 1;
  207. /*
  208. * Attempt to take the first device in this group:
  209. *'first element' flag is set
  210. */
  211. found = dev_stor_get(type, 1, &more, di);
  212. } else {
  213. errf("group%d - out of order iteration\n", type);
  214. found = 0;
  215. more = 0;
  216. }
  217. }
  218. /*
  219. * If there are no more devices in this group, consider its
  220. * enumeration finished
  221. */
  222. specs[type].enum_ended = (!more) ? 1 : 0;
  223. if (found)
  224. debugf("device found, returning cookie 0x%08x\n",
  225. (u_int32_t)di->cookie);
  226. else
  227. debugf("no device found\n");
  228. return found;
  229. }
  230. void dev_enum_reset(void)
  231. {
  232. int i;
  233. for (i = 0; i < ENUM_MAX; i ++) {
  234. specs[i].enum_started = 0;
  235. specs[i].enum_ended = 0;
  236. }
  237. }
  238. int dev_enum_storage(struct device_info *di)
  239. {
  240. int i;
  241. /*
  242. * check: ide, usb, scsi, mmc
  243. */
  244. for (i = ENUM_IDE; i < ENUM_MAX; i ++) {
  245. if (dev_enum_stor(i, di))
  246. return 1;
  247. }
  248. return 0;
  249. }
  250. static int dev_stor_is_valid(int type, block_dev_desc_t *dd)
  251. {
  252. int i;
  253. for (i = 0; i < specs[type].max_dev; i++)
  254. if (dd == get_dev(specs[type].name, i))
  255. if (dd->type != DEV_TYPE_UNKNOWN)
  256. return 1;
  257. return 0;
  258. }
  259. int dev_open_stor(void *cookie)
  260. {
  261. int type = dev_stor_type(cookie);
  262. if (type == ENUM_MAX)
  263. return API_ENODEV;
  264. if (dev_stor_is_valid(type, (block_dev_desc_t *)cookie))
  265. return 0;
  266. return API_ENODEV;
  267. }
  268. int dev_close_stor(void *cookie)
  269. {
  270. /*
  271. * Not much to do as we actually do not alter storage devices upon
  272. * close
  273. */
  274. return 0;
  275. }
  276. static int dev_stor_index(block_dev_desc_t *dd)
  277. {
  278. int i, type;
  279. type = dev_stor_type(dd);
  280. for (i = 0; i < specs[type].max_dev; i++)
  281. if (dd == get_dev(specs[type].name, i))
  282. return i;
  283. return (specs[type].max_dev);
  284. }
  285. lbasize_t dev_read_stor(void *cookie, void *buf, lbasize_t len, lbastart_t start)
  286. {
  287. int type;
  288. block_dev_desc_t *dd = (block_dev_desc_t *)cookie;
  289. if ((type = dev_stor_type(dd)) == ENUM_MAX)
  290. return 0;
  291. if (!dev_stor_is_valid(type, dd))
  292. return 0;
  293. if ((dd->block_read) == NULL) {
  294. debugf("no block_read() for device 0x%08x\n", cookie);
  295. return 0;
  296. }
  297. return (dd->block_read(dev_stor_index(dd), start, len, buf));
  298. }