api_storage.c 8.2 KB

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