Commit a8bcc8f7f3fc3a8060230f249f4b0eab687a8dbf

kanoi 2014-06-25T09:42:43

Merge pull request #614 from kanoi/master Minion - resolve duplicates

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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
diff --git a/driver-minion.c b/driver-minion.c
index 960940a..85e9d45 100644
--- a/driver-minion.c
+++ b/driver-minion.c
@@ -35,14 +35,14 @@ static void minion_detect(__maybe_unused bool hotplug)
 #define MINION_ROCKCHIP 0
 
 // The code is always in - this just decides if it does it
-static bool minreread = true;
+static bool minreread = false;
 
 /*
  * Use pins for board selection
  * If disabled, it will test chips just as 'pin 0'
  * but never do any gpio - the equivalent of the previous 'no pins' code
  */
-static bool usepins = true;
+static bool usepins = false;
 
 #define MINION_PAGE_SIZE 4096
 
@@ -386,7 +386,7 @@ struct minion_status {
 	uint32_t freq;
 	uint32_t quework;
 	uint32_t chipwork;
-	uint32_t realwork; // chipwork, but FIFO_STA can update it
+	uint32_t realwork; // FIFO_STA
 	struct timeval last;
 	bool overheat;
 	bool islow;
@@ -493,7 +493,7 @@ typedef struct witem {
 	struct timeval sent;
 	int nonces;
 	bool urgent;
-	bool stale; // if stale, don't decrement que/chip/realwork when discarded
+	bool stale; // if stale, don't decrement que/chipwork when discarded
 	bool rolled;
 	int errors; // uncertain since the error could mean task_id is wrong
 } WITEM;
@@ -1048,10 +1048,18 @@ static void init_pins(struct minion_info *minioninfo)
 	}
 }
 
+#define EXTRA_LOG_IO 0
+#if EXTRA_LOG_IO
+static int ioc = 0;
+#endif
+
 static int __do_ioctl(struct minion_info *minioninfo, int pin, uint8_t *obuf, uint32_t osiz, uint8_t *rbuf, uint32_t rsiz, MINION_FFL_ARGS)
 {
 	struct spi_ioc_transfer tran;
 	int ret;
+#if EXTRA_LOG_IO
+	int myioc;
+#endif
 #if MINION_SHOW_IO
 	char dataw[DATA_ALL], datar[DATA_ALL];
 #endif
@@ -1116,6 +1124,9 @@ static int __do_ioctl(struct minion_info *minioninfo, int pin, uint8_t *obuf, ui
 	}
 	IO_STAT_NOW(&sta);
 	ret = ioctl(minioninfo->spifd, SPI_IOC_MESSAGE(1), (void *)&tran);
+#if EXTRA_LOG_IO
+	myioc = ioc++;
+#endif
 	IO_STAT_NOW(&fin);
 	if (usepins) {
 		MINION_PIN_AFTER;
@@ -1143,7 +1154,37 @@ static int __do_ioctl(struct minion_info *minioninfo, int pin, uint8_t *obuf, ui
 
 	display_ioctl(ret, osiz, (uint8_t *)(&dataw[DATA_OFF]), rsiz, (uint8_t *)(&datar[DATA_OFF]));
 #endif
-
+#if EXTRA_LOG_IO
+	if (obuf[1] == READ_ADDR(MINION_RES_PEEK) ||
+	    obuf[1] == READ_ADDR(MINION_RES_DATA) ||
+	    obuf[1] == READ_ADDR(MINION_SYS_FIFO_STA)) {
+		char *uf1, *uf2, c;
+		uf1 = bin2hex(obuf, DATA_SIZ);
+		uf2 = bin2hex(rbuf, (size_t)ret);
+		switch (obuf[1]) {
+			case READ_ADDR(MINION_RES_PEEK):
+				c = 'P';
+				break;
+			case READ_ADDR(MINION_RES_DATA):
+				c = 'D';
+				break;
+			case READ_ADDR(MINION_SYS_FIFO_STA):
+				c = 'F';
+				break;
+		}
+		applog(LOG_WARNING, "*** seq %d cmd %c %s rep %.8s %s",
+				    myioc, c, uf1, uf2, uf2+8);
+		free(uf2);
+		free(uf1);
+	}
+	if (obuf[1] == WRITE_ADDR(MINION_QUE_0)) {
+		char *uf;
+		uf = bin2hex(obuf, osiz);
+		applog(LOG_WARNING, "*** seq %d work %s",
+				    myioc, uf);
+		free(uf);
+	}
+#endif
 	return ret;
 }
 
@@ -2102,6 +2143,8 @@ static void minion_detect(bool hotplug)
 
 	minioninfo->initialised = true;
 
+	dupalloc(minioncgpu, 10);
+
 	return;
 
 cleanup:
@@ -2272,17 +2315,23 @@ static void *minion_spi_write(void *userdata)
 					case WRITE_ADDR(MINION_SYS_RSTN_CTL):
 						// Do this here after it has actually been flushed
 						if ((titem->wbuf[0] & SYS_RSTN_CTL_FLUSH) == SYS_RSTN_CTL_FLUSH) {
+							int cnt = 0;
 							K_WLOCK(minioninfo->wwork_list);
 							work = minioninfo->wchip_list[chip]->head;
 							while (work) {
+								cnt++;
 								DATAW(work)->stale = true;
-								minioninfo->chip_status[chip].chipwork--;
-								if (minioninfo->chip_status[chip].realwork > 0)
-									minioninfo->chip_status[chip].realwork--;
 								work = work->next;
 							}
 							minioninfo->chip_status[chip].chipwork = 0;
 							minioninfo->chip_status[chip].realwork = 0;
+#if MINION_SHOW_IO
+							applog(IOCTRL_LOG, "RSTN chip %d (cnt=%d) cw0=%u rw0=%u qw=%u",
+									   chip, cnt,
+									   minioninfo->chip_status[chip].chipwork,
+									   minioninfo->chip_status[chip].realwork,
+									   minioninfo->chip_status[chip].quework);
+#endif
 							K_WUNLOCK(minioninfo->wwork_list);
 						}
 						break;
@@ -2292,7 +2341,13 @@ static void *minion_spi_write(void *userdata)
 						k_add_head(minioninfo->wchip_list[chip], titem->witem);
 						minioninfo->chip_status[chip].quework--;
 						minioninfo->chip_status[chip].chipwork++;
-						minioninfo->chip_status[chip].realwork++;
+#if MINION_SHOW_IO
+						applog(IOCTRL_LOG, "QUE_0 chip %d cw+1=%u rw=%u qw-1=%u",
+								   chip,
+								   minioninfo->chip_status[chip].chipwork,
+								   minioninfo->chip_status[chip].realwork,
+								   minioninfo->chip_status[chip].quework);
+#endif
 						K_WUNLOCK(minioninfo->wchip_list[chip]);
 						break;
 					case READ_ADDR(MINION_CORE_ENA0_31):
@@ -2352,7 +2407,6 @@ static void *minion_spi_reply(void *userdata)
 	K_ITEM *item;
 	TITEM fifo_task, res1_task, res2_task;
 	int chip, resoff;
-	int chipwork, gap;
 	bool somelow;
 	struct timeval now;
 
@@ -2432,14 +2486,18 @@ static void *minion_spi_reply(void *userdata)
 					fifo_task.chip = chip;
 					fifo_task.reply = 0;
 					minion_txrx(&fifo_task);
-					if (fifo_task.reply <= 0)
+					if (fifo_task.reply <= 0) {
+						minioninfo->spi_errors++;
+						minioninfo->fifo_spi_errors[chip]++;
+						minioninfo->res_err_count[chip]++;
 						break;
-					else {
+					} else {
 						if (fifo_task.reply < (int)(fifo_task.osiz)) {
 							char *buf = bin2hex((unsigned char *)(&(fifo_task.rbuf[fifo_task.osiz - fifo_task.rsiz])),
 										(int)(fifo_task.rsiz));
-							applog(LOG_ERR, "%s%i: Bad fifo reply (%s) size %d, should be %d",
-									minioncgpu->drv->name, minioncgpu->device_id, buf,
+							applog(LOG_ERR, "%s%i: Chip %d Bad fifo reply (%s) size %d, should be %d",
+									minioncgpu->drv->name, minioncgpu->device_id,
+									chip, buf,
 									fifo_task.reply, (int)(fifo_task.osiz));
 							free(buf);
 							minioninfo->spi_errors++;
@@ -2447,19 +2505,19 @@ static void *minion_spi_reply(void *userdata)
 							minioninfo->res_err_count[chip]++;
 						} else {
 							if (fifo_task.reply > (int)(fifo_task.osiz)) {
-								applog(LOG_ERR, "%s%i: Unexpected fifo reply size %d, expected only %d",
+								applog(LOG_ERR, "%s%i: Chip %d Unexpected fifo reply size %d, expected only %d",
 										minioncgpu->drv->name, minioncgpu->device_id,
-										fifo_task.reply, (int)(fifo_task.osiz));
+										chip, fifo_task.reply, (int)(fifo_task.osiz));
 							}
 							res = FIFO_RES(fifo_task.rbuf, fifo_task.osiz - fifo_task.rsiz);
 							cmd = FIFO_CMD(fifo_task.rbuf, fifo_task.osiz - fifo_task.rsiz);
 							// valid reply?
-							if (res <= MINION_QUE_MAX && cmd <= MINION_QUE_HIGH)
+							if (res <= MINION_QUE_MAX && cmd <= MINION_QUE_MAX)
 								break;
 
-							applog(LOG_ERR, "%s%i: Bad fifo reply res %d (max is %d) cmd %d (max is %d)",
+							applog(LOG_ERR, "%s%i: Chip %d Bad fifo reply res %d (max is %d) cmd %d (max is %d)",
 									minioncgpu->drv->name, minioncgpu->device_id,
-									(int)res, MINION_QUE_MAX, (int)cmd, MINION_QUE_HIGH);
+									chip, (int)res, MINION_QUE_MAX, (int)cmd, MINION_QUE_MAX);
 							minioninfo->spi_errors++;
 							minioninfo->fifo_spi_errors[chip]++;
 							minioninfo->res_err_count[chip]++;
@@ -2472,23 +2530,16 @@ static void *minion_spi_reply(void *userdata)
 					continue;
 
 				K_WLOCK(minioninfo->wwork_list);
-				// it shouldn't go up
-				if (cmd < minioninfo->chip_status[chip].realwork)
-					minioninfo->chip_status[chip].realwork = (uint32_t)cmd;
-				else {
-					cmd = (uint8_t)(minioninfo->chip_status[chip].realwork);
-					minioninfo->spi_errors++;
-					minioninfo->fifo_spi_errors[chip]++;
-					minioninfo->res_err_count[chip]++;
-				}
-				chipwork = (int)(minioninfo->chip_status[chip].chipwork);
+				// have to just assume it's always correct since we can't verify it
+				minioninfo->chip_status[chip].realwork = (uint32_t)cmd;
+#if MINION_SHOW_IO
+				applog(IOCTRL_LOG, "SetReal chip %d cw=%u rw==%u qw=%u",
+						   chip,
+						   minioninfo->chip_status[chip].chipwork,
+						   minioninfo->chip_status[chip].realwork,
+						   minioninfo->chip_status[chip].quework);
+#endif
 				K_WUNLOCK(minioninfo->wwork_list);
-				gap = chipwork - (int)cmd;
-				if (gap < -1 || gap > 1) {
-//					applog(LOG_ERR, "%s%i: fifo cmd difference > 1 for chip %d - work %d cmd %d gap %d",
-//							minioncgpu->drv->name, minioncgpu->device_id,
-//							chip, chipwork, (int)cmd, gap);
-				}
 
 				if (cmd < MINION_QUE_LOW) {
 					somelow = true;
@@ -2534,8 +2585,9 @@ repeek:
 						cgtime(&now);
 						if (res1_task.reply < (int)MINION_RES_DATA_SIZ) {
 							char *buf = bin2hex((unsigned char *)(&(res1_task.rbuf[res1_task.osiz - res1_task.rsiz])), (int)(res1_task.rsiz));
-							applog(LOG_ERR, "%s%i: Bad work reply (%s) size %d, should be at least %d",
-									minioncgpu->drv->name, minioncgpu->device_id, buf,
+							applog(LOG_ERR, "%s%i: Chip %d Bad work reply (%s) size %d, should be at least %d",
+									minioncgpu->drv->name, minioncgpu->device_id,
+									chip, buf,
 									res1_task.reply, (int)MINION_RES_DATA_SIZ);
 							free(buf);
 							minioninfo->spi_errors++;
@@ -2543,9 +2595,9 @@ repeek:
 							minioninfo->res_err_count[chip]++;
 						} else {
 							if (res1_task.reply != (int)(res1_task.osiz)) {
-								applog(LOG_ERR, "%s%i: Unexpected work reply size %d, expected %d",
+								applog(LOG_ERR, "%s%i: Chip %d Unexpected work reply size %d, expected %d",
 										minioncgpu->drv->name, minioncgpu->device_id,
-										res1_task.reply, (int)(res1_task.osiz));
+										chip, res1_task.reply, (int)(res1_task.osiz));
 								minioninfo->spi_errors++;
 								minioninfo->res_spi_errors[chip]++;
 								minioninfo->res_err_count[chip]++;
@@ -2749,6 +2801,7 @@ repeek:
 enum nonce_state {
 	NONCE_GOOD_NONCE,
 	NONCE_NO_NONCE,
+	NONCE_DUP_NONCE,
 	NONCE_BAD_NONCE,
 	NONCE_BAD_WORK,
 	NONCE_NO_WORK,
@@ -2771,8 +2824,13 @@ static void cleanup_older(struct cgpu_info *minioncgpu, int chip, K_ITEM *item, 
 			k_unlink_item(minioninfo->wchip_list[chip], tail);
 			if (!(DATAW(tail)->stale)) {
 				minioninfo->chip_status[chip].chipwork--;
-				if (minioninfo->chip_status[chip].realwork > 0)
-					minioninfo->chip_status[chip].realwork--;
+#if MINION_SHOW_IO
+				applog(IOCTRL_LOG, "COld chip %d cw-1=%u rw=%u qw=%u",
+						   chip,
+						   minioninfo->chip_status[chip].chipwork,
+						   minioninfo->chip_status[chip].realwork,
+						   minioninfo->chip_status[chip].quework);
+#endif
 /*
 				// If it had no valid work (only errors) then it won't have been cleaned up
 				errs = (DATAW(tail)->errors > 0);
@@ -2799,8 +2857,13 @@ static void cleanup_older(struct cgpu_info *minioncgpu, int chip, K_ITEM *item, 
 			k_unlink_item(minioninfo->wchip_list[chip], item);
 			if (!(DATAW(item)->stale)) {
 				minioninfo->chip_status[chip].chipwork--;
-				if (minioninfo->chip_status[chip].realwork > 0)
-					minioninfo->chip_status[chip].realwork--;
+#if MINION_SHOW_IO
+			applog(IOCTRL_LOG, "CONoN chip %d cw-1=%u rw=%u qw=%u",
+					   chip,
+					   minioninfo->chip_status[chip].chipwork,
+					   minioninfo->chip_status[chip].realwork,
+					   minioninfo->chip_status[chip].quework);
+#endif
 			}
 			K_WUNLOCK(minioninfo->wchip_list[chip]);
 			applog(MINION_LOG, "%s%i: marking complete - no_nonce task 0x%04x chip %d",
@@ -2915,6 +2978,14 @@ retry:
 
 	redo = false;
 retest:
+	if (isdupnonce(minioncgpu, DATAW(item)->work, nonce)) {
+		applog(LOG_WARNING, " ... nonce %02x%02x%02x%02x chip %d core %d task 0x%04x",
+				    (nonce & 0xff), ((nonce >> 8) & 0xff),
+				    ((nonce >> 16) & 0xff), ((nonce >> 24) & 0xff),
+				    chip, core, task_id);
+		return NONCE_DUP_NONCE;
+	}
+
 	if (test_nonce(DATAW(item)->work, nonce)) {
 //applog(MINTASK_LOG, "%s%i: Valid Nonce chip %d core %d task 0x%04x nonce 0x%08x", minioncgpu->drv->name, minioncgpu->device_id, chip, core, task_id, nonce);
 		submit_tested_work(thr, DATAW(item)->work);
@@ -3047,6 +3118,13 @@ static void minion_flush_work(struct cgpu_info *minioncgpu)
 		prev_task = task->prev;
 		if (DATAT(task)->address == WRITE_ADDR(MINION_QUE_0)) {
 			minioninfo->chip_status[DATAT(task)->chip].quework--;
+#if MINION_SHOW_IO
+			applog(IOCTRL_LOG, "QueFlush chip %d cw=%u rw=%u qw-1=%u",
+					   (int)DATAT(task)->chip,
+					   minioninfo->chip_status[DATAT(task)->chip].chipwork,
+					   minioninfo->chip_status[DATAT(task)->chip].realwork,
+					   minioninfo->chip_status[DATAT(task)->chip].quework);
+#endif
 			witem = DATAT(task)->witem;
 			k_unlink_item(minioninfo->wque_list[DATAT(task)->chip], witem);
 			k_free_head(minioninfo->wfree_list, witem);
@@ -3232,6 +3310,13 @@ static void new_work_task(struct cgpu_info *minioncgpu, K_ITEM *witem, int chip,
 	K_WLOCK(minioninfo->wque_list[chip]);
 	k_add_head(minioninfo->wque_list[chip], witem);
 	minioninfo->chip_status[chip].quework++;
+#if MINION_SHOW_IO
+	applog(IOCTRL_LOG, "Que chip %d cw=%u rw=%u qw+1=%u",
+			   chip,
+			   minioninfo->chip_status[chip].chipwork,
+			   minioninfo->chip_status[chip].realwork,
+			   minioninfo->chip_status[chip].quework);
+#endif
 	K_WUNLOCK(minioninfo->wque_list[chip]);
 
 	K_WLOCK(minioninfo->task_list);
@@ -3262,13 +3347,20 @@ static void minion_do_work(struct cgpu_info *minioncgpu)
 {
 	struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
 	int count, chip, j, lowcount;
-	uint8_t state;
+	TITEM fifo_task;
+	uint8_t state, cmd;
 	K_ITEM *item;
 #if ENABLE_INT_NONO
 	K_ITEM *task;
 #endif
 	bool islow, sentwork;
 
+	fifo_task.chip = 0;
+	fifo_task.write = false;
+	fifo_task.address = MINION_SYS_FIFO_STA;
+	fifo_task.wsiz = 0;
+	fifo_task.rsiz = MINION_SYS_SIZ;
+
 	// TODO: (remove this) Fake starved of work to test CMD Interrupt
 //	if (total_secs > 120) {
 //		cgsleep_ms(888);
@@ -3291,6 +3383,55 @@ static void minion_do_work(struct cgpu_info *minioncgpu)
 
 		for (chip = 0; chip < (int)MINION_CHIPS; chip++) {
 			if (minioninfo->has_chip[chip] && !minioninfo->chip_status[chip].overheat) {
+				int tries = 0;
+				while (tries++ < 4) {
+					cmd = 0;
+					fifo_task.chip = chip;
+					fifo_task.reply = 0;
+					minion_txrx(&fifo_task);
+					if (fifo_task.reply <= 0) {
+						if (fifo_task.reply < (int)(fifo_task.osiz)) {
+							char *buf = bin2hex((unsigned char *)(&(fifo_task.rbuf[fifo_task.osiz - fifo_task.rsiz])),
+										(int)(fifo_task.rsiz));
+							applog(LOG_ERR, "%s%i: Chip %d Bad fifo reply (%s) size %d, should be %d",
+									minioncgpu->drv->name, minioncgpu->device_id,
+									chip, buf,
+									fifo_task.reply, (int)(fifo_task.osiz));
+							free(buf);
+							minioninfo->spi_errors++;
+							minioninfo->fifo_spi_errors[chip]++;
+							minioninfo->res_err_count[chip]++;
+						} else {
+							if (fifo_task.reply > (int)(fifo_task.osiz)) {
+								applog(LOG_ERR, "%s%i: Chip %d Unexpected fifo reply size %d, expected only %d",
+										minioncgpu->drv->name, minioncgpu->device_id,
+										chip, fifo_task.reply, (int)(fifo_task.osiz));
+							}
+							cmd = FIFO_CMD(fifo_task.rbuf, fifo_task.osiz - fifo_task.rsiz);
+							// valid reply?
+							if (cmd < MINION_QUE_MAX) {
+								K_WLOCK(minioninfo->wchip_list[chip]);
+								minioninfo->chip_status[chip].realwork = cmd;
+								K_WUNLOCK(minioninfo->wchip_list[chip]);
+								if (cmd < 10 || cmd >= MINION_QUE_HIGH) {
+									applog(LOG_DEBUG, "%s%i: Chip %d fifo cmd %d",
+											  minioncgpu->drv->name,
+											  minioncgpu->device_id,
+											  chip, (int)cmd);
+								}
+								break;
+							}
+
+							applog(LOG_ERR, "%s%i: Chip %d Bad fifo reply cmd %d (max is %d)",
+									minioncgpu->drv->name, minioncgpu->device_id,
+									chip, (int)cmd, MINION_QUE_MAX);
+							minioninfo->spi_errors++;
+							minioninfo->fifo_spi_errors[chip]++;
+							minioninfo->res_err_count[chip]++;
+						}
+					}
+				}
+
 				K_WLOCK(minioninfo->wchip_list[chip]);
 				count = minioninfo->chip_status[chip].quework +
 					minioninfo->chip_status[chip].realwork;