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