Commit 2d4a1db57e21d399eac8bb3b901e3dff614be061

Con Kolivas 2013-05-27T14:37:41

Merge branch 'icarus-dev' into avalon-usbdev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
diff --git a/usbutils.c b/usbutils.c
index 3b14805..c8323e6 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -336,8 +336,28 @@ struct usb_in_use_list {
 // List of in use devices
 static struct usb_in_use_list *in_use_head = NULL;
 
+// Set this to 0 to remove stats processing
+#define DO_USB_STATS 1
+
 static bool stats_initialised = false;
 
+#if DO_USB_STATS
+
+// NONE must be 0 - calloced
+#define MODE_NONE 0
+#define MODE_CTRL_READ (1 << 0)
+#define MODE_CTRL_WRITE (1 << 1)
+#define MODE_BULK_READ (1 << 2)
+#define MODE_BULK_WRITE (1 << 3)
+
+#define MODE_SEP_STR "+"
+#define MODE_NONE_STR "X"
+#define MODE_CTRL_READ_STR "cr"
+#define MODE_CTRL_WRITE_STR "cw"
+#define MODE_BULK_READ_STR "br"
+#define MODE_BULK_WRITE_STR "bw"
+
+// One for each CMD, TIMEOUT, ERROR
 struct cg_usb_stats_item {
 	uint64_t count;
 	double total_delay;
@@ -351,11 +371,14 @@ struct cg_usb_stats_item {
 #define CMD_TIMEOUT 1
 #define CMD_ERROR 2
 
+// One for each C_CMD
 struct cg_usb_stats_details {
 	int seq;
+	uint32_t modes;
 	struct cg_usb_stats_item item[CMD_ERROR+1];
 };
 
+// One for each device
 struct cg_usb_stats {
 	char *name;
 	int device_id;
@@ -368,6 +391,17 @@ struct cg_usb_stats {
 static struct cg_usb_stats *usb_stats = NULL;
 static int next_stat = 0;
 
+#define USB_STATS(sgpu, sta, fin, err, mode, cmd, seq) \
+		stats(cgpu, sta, fin, err, mode, cmd, seq)
+#define STATS_TIMEVAL(tv) cgtime(tv)
+#define USB_REJECT(sgpu, mode) rejected_inc(sgpu, mode)
+#else
+#define USB_STATS(sgpu, sta, fin, err, mode, cmd, seq)
+#define STATS_TIMEVAL(tv)
+#define USB_REJECT(sgpu, mode)
+
+#endif // DO_USB_STATS
+
 static const char **usb_commands;
 
 static const char *C_REJECTED_S = "RejectedNoDevice";
@@ -1783,15 +1817,45 @@ void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_devi
 	libusb_free_device_list(list, 1);
 }
 
-// Set this to 0 to remove stats processing
-#define DO_USB_STATS 1
-
 #if DO_USB_STATS
-#define USB_STATS(sgpu, sta, fin, err, cmd, seq) stats(cgpu, sta, fin, err, cmd, seq)
-#define STATS_TIMEVAL(tv) cgtime(tv)
-#else
-#define USB_STATS(sgpu, sta, fin, err, cmd, seq)
-#define STATS_TIMEVAL(tv)
+static void modes_str(char *buf, uint32_t modes)
+{
+	bool first;
+
+	*buf = '\0';
+
+	if (modes == MODE_NONE)
+		strcpy(buf, MODE_NONE_STR);
+	else {
+		first = true;
+
+		if (modes & MODE_CTRL_READ) {
+			strcpy(buf, MODE_CTRL_READ_STR);
+			first = false;
+		}
+
+		if (modes & MODE_CTRL_WRITE) {
+			if (!first)
+				strcat(buf, MODE_SEP_STR);
+			strcat(buf, MODE_CTRL_WRITE_STR);
+			first = false;
+		}
+
+		if (modes & MODE_BULK_READ) {
+			if (!first)
+				strcat(buf, MODE_SEP_STR);
+			strcat(buf, MODE_BULK_READ_STR);
+			first = false;
+		}
+
+		if (modes & MODE_BULK_WRITE) {
+			if (!first)
+				strcat(buf, MODE_SEP_STR);
+			strcat(buf, MODE_BULK_WRITE_STR);
+			first = false;
+		}
+	}
+}
 #endif
 
 // The stat data can be spurious due to not locking it before copying it -
@@ -1806,6 +1870,7 @@ struct api_data *api_usb_stats(__maybe_unused int *count)
 	struct api_data *root = NULL;
 	int device;
 	int cmdseq;
+	char modes_s[32];
 
 	if (next_stat == 0)
 		return NULL;
@@ -1829,6 +1894,8 @@ struct api_data *api_usb_stats(__maybe_unused int *count)
 		root = api_add_int(root, "ID", &(sta->device_id), false);
 		root = api_add_const(root, "Stat", usb_commands[cmdseq/2], false);
 		root = api_add_int(root, "Seq", &(details->seq), true);
+		modes_str(modes_s, details->modes);
+		root = api_add_string(root, "Modes", modes_s, true);
 		root = api_add_uint64(root, "Count",
 					&(details->item[CMD_CMD].count), true);
 		root = api_add_double(root, "Total Delay",
@@ -1882,10 +1949,15 @@ static void newstats(struct cgpu_info *cgpu)
 	mutex_unlock(&cgusb_lock);
 
 	usb_stats = realloc(usb_stats, sizeof(*usb_stats) * next_stat);
+	if (!usb_stats)
+		quit(1, "USB failed to realloc usb_stats %d", next_stat);
 
 	usb_stats[next_stat-1].name = cgpu->drv->name;
 	usb_stats[next_stat-1].device_id = -1;
 	usb_stats[next_stat-1].details = calloc(1, sizeof(struct cg_usb_stats_details) * C_MAX * 2);
+	if (!usb_stats[next_stat-1].details)
+		quit(1, "USB failed to calloc details for %d", next_stat);
+
 	for (i = 1; i < C_MAX * 2; i += 2)
 		usb_stats[next_stat-1].details[i].seq = 1;
 }
@@ -1903,7 +1975,7 @@ void update_usb_stats(__maybe_unused struct cgpu_info *cgpu)
 }
 
 #if DO_USB_STATS
-static void stats(struct cgpu_info *cgpu, struct timeval *tv_start, struct timeval *tv_finish, int err, enum usb_cmds cmd, int seq)
+static void stats(struct cgpu_info *cgpu, struct timeval *tv_start, struct timeval *tv_finish, int err, int mode, enum usb_cmds cmd, int seq)
 {
 	struct cg_usb_stats_details *details;
 	double diff;
@@ -1913,6 +1985,7 @@ static void stats(struct cgpu_info *cgpu, struct timeval *tv_start, struct timev
 		newstats(cgpu);
 
 	details = &(usb_stats[cgpu->usbinfo.usbstat - 1].details[cmd * 2 + seq]);
+	details->modes |= mode;
 
 	diff = tdiff(tv_finish, tv_start);
 
@@ -1942,7 +2015,7 @@ static void stats(struct cgpu_info *cgpu, struct timeval *tv_start, struct timev
 	details->item[item].count++;
 }
 
-static void rejected_inc(struct cgpu_info *cgpu)
+static void rejected_inc(struct cgpu_info *cgpu, uint32_t mode)
 {
 	struct cg_usb_stats_details *details;
 	int item = CMD_ERROR;
@@ -1951,14 +2024,14 @@ static void rejected_inc(struct cgpu_info *cgpu)
 		newstats(cgpu);
 
 	details = &(usb_stats[cgpu->usbinfo.usbstat - 1].details[C_REJECTED * 2 + 0]);
-
+	details->modes |= mode;
 	details->item[item].count++;
 }
 #endif
 
 #define USB_MAX_READ 8192
 
-int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool readonce)
+int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, __maybe_unused enum usb_cmds cmd, bool readonce)
 {
 	struct cg_usb_device *usbdev = cgpu->usbdev;
 	bool ftdi = (usbdev->usb_type == USB_TYPE_FTDI);
@@ -1969,7 +2042,7 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
 	unsigned int initial_timeout;
 	double max, done;
 	int bufleft, err, got, tot;
-	bool first = true;
+	__maybe_unused bool first = true;
 	unsigned char *search;
 	int endlen;
 
@@ -1985,9 +2058,8 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
 	if (cgpu->usbinfo.nodev) {
 		*buf = '\0';
 		*processed = 0;
-#if DO_USB_STATS
-		rejected_inc(cgpu);
-#endif
+		USB_REJECT(cgpu, MODE_BULK_READ);
+
 		return LIBUSB_ERROR_NO_DEVICE;
 	}
 
@@ -2012,8 +2084,9 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
 			err = libusb_bulk_transfer(usbdev->handle,
 					usbdev->found->eps[ep].ep,
 					ptr, usbbufread, &got, timeout);
-			STATS_TIMEVAL(&tv_finish);
-			USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, first ? SEQ0 : SEQ1);
+			cgtime(&tv_finish);
+			USB_STATS(cgpu, &tv_start, &tv_finish, err,
+					MODE_BULK_READ, cmd, first ? SEQ0 : SEQ1);
 			ptr[got] = '\0';
 
 			USBDEBUG("USB debug: @_usb_read(%s (nodev=%s)) first=%s err=%d%s got=%d ptr='%s' usbbufread=%zu", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), bool_str(first), err, isnodev(err), got, (char *)str_text((char *)ptr), usbbufread);
@@ -2040,7 +2113,7 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
 			first = false;
 
 			done = tdiff(&tv_finish, &read_start);
-			// N.B. this is return LIBUSB_SUCCESS with whatever size has already been read
+			// N.B. this is: return LIBUSB_SUCCESS with whatever size has already been read
 			if (unlikely(done >= max))
 				break;
 
@@ -2075,7 +2148,8 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
 				usbdev->found->eps[ep].ep,
 				ptr, usbbufread, &got, timeout);
 		cgtime(&tv_finish);
-		USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, first ? SEQ0 : SEQ1);
+		USB_STATS(cgpu, &tv_start, &tv_finish, err,
+				MODE_BULK_READ, cmd, first ? SEQ0 : SEQ1);
 		ptr[got] = '\0';
 
 		USBDEBUG("USB debug: @_usb_read(%s (nodev=%s)) first=%s err=%d%s got=%d ptr='%s' usbbufread=%zu", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), bool_str(first), err, isnodev(err), got, (char *)str_text((char *)ptr), usbbufread);
@@ -2120,7 +2194,7 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
 		first = false;
 
 		done = tdiff(&tv_finish, &read_start);
-		// N.B. this is return LIBUSB_SUCCESS with whatever size has already been read
+		// N.B. this is: return LIBUSB_SUCCESS with whatever size has already been read
 		if (unlikely(done >= max))
 			break;
 
@@ -2136,37 +2210,68 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
 	return err;
 }
 
-int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds cmd)
+int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, __maybe_unused enum usb_cmds cmd)
 {
 	struct cg_usb_device *usbdev = cgpu->usbdev;
 #if DO_USB_STATS
-	struct timeval tv_start, tv_finish;
+	struct timeval tv_start;
 #endif
-	int err, sent;
+	struct timeval read_start, tv_finish;
+	unsigned int initial_timeout;
+	double max, done;
+	__maybe_unused bool first = true;
+	int err, sent, tot;
 
 	USBDEBUG("USB debug: _usb_write(%s (nodev=%s),ep=%d,buf='%s',bufsiz=%zu,proc=%p,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), ep, (char *)str_text(buf), bufsiz, processed, timeout, usb_cmdname(cmd));
 
+	*processed = 0;
+
 	if (cgpu->usbinfo.nodev) {
-		*processed = 0;
-#if DO_USB_STATS
-		rejected_inc(cgpu);
-#endif
+		USB_REJECT(cgpu, MODE_BULK_WRITE);
+
 		return LIBUSB_ERROR_NO_DEVICE;
 	}
 
-	sent = 0;
-	STATS_TIMEVAL(&tv_start);
-	err = libusb_bulk_transfer(usbdev->handle,
-			usbdev->found->eps[ep].ep,
-			(unsigned char *)buf,
-			bufsiz, &sent,
-			timeout == DEVTIMEOUT ? usbdev->found->timeout : timeout);
-	STATS_TIMEVAL(&tv_finish);
-	USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, SEQ0);
+	if (timeout == DEVTIMEOUT)
+		timeout = usbdev->found->timeout;
+
+	tot = 0;
+	err = LIBUSB_SUCCESS;
+	initial_timeout = timeout;
+	max = ((double)timeout) / 1000.0;
+	cgtime(&read_start);
+	while (bufsiz > 0) {
+		sent = 0;
+		STATS_TIMEVAL(&tv_start);
+		err = libusb_bulk_transfer(usbdev->handle,
+				usbdev->found->eps[ep].ep,
+				(unsigned char *)buf,
+				bufsiz, &sent, timeout);
+		cgtime(&tv_finish);
+		USB_STATS(cgpu, &tv_start, &tv_finish, err,
+				MODE_BULK_WRITE, cmd, first ? SEQ0 : SEQ1);
 
-	USBDEBUG("USB debug: @_usb_write(%s (nodev=%s)) err=%d%s sent=%d", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), err, isnodev(err), sent);
+		USBDEBUG("USB debug: @_usb_write(%s (nodev=%s)) err=%d%s sent=%d", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), err, isnodev(err), sent);
 
-	*processed = sent;
+		tot += sent;
+
+		if (err)
+			break;
+
+		buf += sent;
+		bufsiz -= sent;
+
+		first = false;
+
+		done = tdiff(&tv_finish, &read_start);
+		// N.B. this is: return LIBUSB_SUCCESS with whatever size was written
+		if (unlikely(done >= max))
+			break;
+
+		timeout = initial_timeout - (done * 1000);
+	}
+
+	*processed = tot;
 
 	if (NODEV(err))
 		release_cgpu(cgpu);
@@ -2174,7 +2279,7 @@ int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pr
 	return err;
 }
 
-int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint32_t *data, int siz, unsigned int timeout, enum usb_cmds cmd)
+int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint32_t *data, int siz, unsigned int timeout, __maybe_unused enum usb_cmds cmd)
 {
 	struct cg_usb_device *usbdev = cgpu->usbdev;
 #if DO_USB_STATS
@@ -2186,9 +2291,8 @@ int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest
 	USBDEBUG("USB debug: _usb_transfer(%s (nodev=%s),type=%"PRIu8",req=%"PRIu8",value=%"PRIu16",index=%"PRIu16",siz=%d,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), request_type, bRequest, wValue, wIndex, siz, timeout, usb_cmdname(cmd));
 
 	if (cgpu->usbinfo.nodev) {
-#if DO_USB_STATS
-		rejected_inc(cgpu);
-#endif
+		USB_REJECT(cgpu, MODE_CTRL_WRITE);
+
 		return LIBUSB_ERROR_NO_DEVICE;
 	}
 
@@ -2213,7 +2317,7 @@ int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest
 		(unsigned char *)buf, (uint16_t)siz,
 		timeout == DEVTIMEOUT ? usbdev->found->timeout : timeout);
 	STATS_TIMEVAL(&tv_finish);
-	USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, SEQ0);
+	USB_STATS(cgpu, &tv_start, &tv_finish, err, MODE_CTRL_WRITE, cmd, SEQ0);
 
 	USBDEBUG("USB debug: @_usb_transfer(%s (nodev=%s)) err=%d%s", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), err, isnodev(err));
 
@@ -2226,7 +2330,7 @@ int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest
 	return err;
 }
 
-int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, char *buf, int bufsiz, int *amount, unsigned int timeout, enum usb_cmds cmd)
+int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, char *buf, int bufsiz, int *amount, unsigned int timeout, __maybe_unused enum usb_cmds cmd)
 {
 	struct cg_usb_device *usbdev = cgpu->usbdev;
 #if DO_USB_STATS
@@ -2237,9 +2341,8 @@ int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRe
 	USBDEBUG("USB debug: _usb_transfer_read(%s (nodev=%s),type=%"PRIu8",req=%"PRIu8",value=%"PRIu16",index=%"PRIu16",bufsiz=%d,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), request_type, bRequest, wValue, wIndex, bufsiz, timeout, usb_cmdname(cmd));
 
 	if (cgpu->usbinfo.nodev) {
-#if DO_USB_STATS
-		rejected_inc(cgpu);
-#endif
+		USB_REJECT(cgpu, MODE_CTRL_READ);
+
 		return LIBUSB_ERROR_NO_DEVICE;
 	}
 
@@ -2251,7 +2354,7 @@ int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRe
 		(unsigned char *)buf, (uint16_t)bufsiz,
 		timeout == DEVTIMEOUT ? usbdev->found->timeout : timeout);
 	STATS_TIMEVAL(&tv_finish);
-	USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, SEQ0);
+	USB_STATS(cgpu, &tv_start, &tv_finish, err, MODE_CTRL_READ, cmd, SEQ0);
 
 	USBDEBUG("USB debug: @_usb_transfer_read(%s (nodev=%s)) amt/err=%d%s%s%s", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), err, isnodev(err), err > 0 ? " = " : BLANK, err > 0 ? bin2hex((unsigned char *)buf, (size_t)err) : BLANK);