Commit 9d37d33df1863436b066e8515de6e8f2ba2555ef

Con Kolivas 2012-07-03T10:32:33

Merge branch 'master' of https://github.com/pshep/cgminer into bfl

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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
diff --git a/cgminer.c b/cgminer.c
index 64f1794..6086d57 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -3373,10 +3373,6 @@ static void hashmeter(int thr_id, struct timeval *diff,
 		thr_info[thr_id].cgpu->device_last_well = time(NULL);
 	}
 
-	/* Don't bother calculating anything if we're not displaying it */
-	if (opt_realquiet || !opt_log_interval)
-		return;
-
 	secs = (double)diff->tv_sec + ((double)diff->tv_usec / 1000000.0);
 
 	/* So we can call hashmeter from a non worker thread */
@@ -4170,6 +4166,7 @@ disabled:
 				tq_pop(mythr->q, NULL); /* Ignore ping that's popped */
 				thread_reportin(mythr);
 				applog(LOG_WARNING, "Thread %d being re-enabled", thr_id);
+				if (api->thread_enable) api->thread_enable(mythr);
 			}
 
 			sdiff.tv_sec = sdiff.tv_usec = 0;
@@ -4455,9 +4452,16 @@ static void age_work(void)
 /* Makes sure the hashmeter keeps going even if mining threads stall, updates
  * the screen at regular intervals, and restarts threads if they appear to have
  * died. */
+#define WATCHDOG_INTERVAL		3
+#define WATCHDOG_SICK_TIME		60
+#define WATCHDOG_DEAD_TIME		600
+#define WATCHDOG_SICK_COUNT		(WATCHDOG_SICK_TIME/WATCHDOG_INTERVAL)
+#define WATCHDOG_DEAD_COUNT		(WATCHDOG_DEAD_TIME/WATCHDOG_INTERVAL)
+#define WATCHDOG_LOW_HASH		1.0 /* consider < 1MH too low for any device */
+
 static void *watchdog_thread(void __maybe_unused *userdata)
 {
-	const unsigned int interval = 3;
+	const unsigned int interval = WATCHDOG_INTERVAL;
 	struct timeval zero_tv;
 
 	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
@@ -4530,24 +4534,24 @@ static void *watchdog_thread(void __maybe_unused *userdata)
 			}
 		}
 
-#ifdef HAVE_OPENCL
 		for (i = 0; i < total_devices; ++i) {
 			struct cgpu_info *cgpu = devices[i];
 			struct thr_info *thr = cgpu->thr[0];
 			enum dev_enable *denable;
 			int gpu;
+			char dev_str[8];
 
-			if (cgpu->api != &opencl_api)
-				continue;
-			/* Use only one thread per device to determine if the GPU is healthy */
-			if (i >= nDevs)
-				break;
-			gpu = thr->cgpu->device_id;
+			if (cgpu->api->get_stats)
+			  cgpu->api->get_stats(cgpu);
+
+			gpu = cgpu->device_id;
 			denable = &cgpu->deven;
+			sprintf(dev_str, "%s%d", cgpu->api->name, gpu);
+
 #ifdef HAVE_ADL
-			if (adl_active && gpus[gpu].has_adl)
+			if (adl_active && cgpu->has_adl)
 				gpu_autotune(gpu, denable);
-			if (opt_debug && gpus[gpu].has_adl) {
+			if (opt_debug && cgpu->has_adl) {
 				int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
 				float temp = 0, vddc = 0;
 
@@ -4556,55 +4560,64 @@ static void *watchdog_thread(void __maybe_unused *userdata)
 					temp, fanpercent, fanspeed, engineclock, memclock, vddc, activity, powertune);
 			}
 #endif
+			
 			/* Thread is waiting on getwork or disabled */
 			if (thr->getwork || *denable == DEV_DISABLED)
 				continue;
 
-			if (gpus[gpu].status != LIFE_WELL && now.tv_sec - thr->last.tv_sec < 60) {
-				applog(LOG_ERR, "Device %d recovered, GPU %d declared WELL!", i, gpu);
-				gpus[gpu].status = LIFE_WELL;
-				gpus[gpu].device_last_well = time(NULL);
-			} else if (now.tv_sec - thr->last.tv_sec > 60 && gpus[gpu].status == LIFE_WELL) {
-				thr->rolling = thr->cgpu->rolling = 0;
-				gpus[gpu].status = LIFE_SICK;
-				applog(LOG_ERR, "Device %d idle for more than 60 seconds, GPU %d declared SICK!", i, gpu);
+			if (cgpu->rolling < WATCHDOG_LOW_HASH)
+				cgpu->low_count++;
+			else
+				cgpu->low_count = 0;
+
+			bool dev_count_well = (cgpu->low_count < WATCHDOG_SICK_COUNT);
+			bool dev_count_sick = (cgpu->low_count > WATCHDOG_SICK_COUNT);
+			bool dev_count_dead = (cgpu->low_count > WATCHDOG_DEAD_COUNT);
+
+			if (gpus[gpu].status != LIFE_WELL && (now.tv_sec - thr->last.tv_sec < WATCHDOG_SICK_TIME) && dev_count_well) {
+				applog(LOG_ERR, "%s: Recovered, declaring WELL!", dev_str);
+				cgpu->status = LIFE_WELL;
+				cgpu->device_last_well = time(NULL);
+			} else if (cgpu->status == LIFE_WELL && ((now.tv_sec - thr->last.tv_sec > WATCHDOG_SICK_TIME) || dev_count_sick)) {
+				thr->rolling = cgpu->rolling = 0;
+				cgpu->status = LIFE_SICK;
+				applog(LOG_ERR, "%s: Idle for more than 60 seconds, declaring SICK!", dev_str);
 				gettimeofday(&thr->sick, NULL);
 
-				gpus[gpu].device_last_not_well = time(NULL);
-				gpus[gpu].device_not_well_reason = REASON_DEV_SICK_IDLE_60;
-				gpus[gpu].dev_sick_idle_60_count++;
+				cgpu->device_last_not_well = time(NULL);
+				cgpu->device_not_well_reason = REASON_DEV_SICK_IDLE_60;
+				cgpu->dev_sick_idle_60_count++;
 #ifdef HAVE_ADL
-				if (adl_active && gpus[gpu].has_adl && gpu_activity(gpu) > 50) {
+				if (adl_active && cgpu->has_adl && gpu_activity(gpu) > 50) {
 					applog(LOG_ERR, "GPU still showing activity suggesting a hard hang.");
 					applog(LOG_ERR, "Will not attempt to auto-restart it.");
 				} else
 #endif
 				if (opt_restart) {
-					applog(LOG_ERR, "Attempting to restart GPU");
-					reinit_device(thr->cgpu);
+					applog(LOG_ERR, "%s: Attempting to restart", dev_str);
+					reinit_device(cgpu);
 				}
-			} else if (now.tv_sec - thr->last.tv_sec > 600 && gpus[i].status == LIFE_SICK) {
-				gpus[gpu].status = LIFE_DEAD;
-				applog(LOG_ERR, "Device %d not responding for more than 10 minutes, GPU %d declared DEAD!", i, gpu);
+			} else if (cgpu->status == LIFE_SICK && ((now.tv_sec - thr->last.tv_sec > WATCHDOG_DEAD_TIME) || dev_count_dead)) {
+				cgpu->status = LIFE_DEAD;
+				applog(LOG_ERR, "%s: Not responded for more than 10 minutes, declaring DEAD!", dev_str);
 				gettimeofday(&thr->sick, NULL);
 
-				gpus[gpu].device_last_not_well = time(NULL);
-				gpus[gpu].device_not_well_reason = REASON_DEV_DEAD_IDLE_600;
-				gpus[gpu].dev_dead_idle_600_count++;
+				cgpu->device_last_not_well = time(NULL);
+				cgpu->device_not_well_reason = REASON_DEV_DEAD_IDLE_600;
+				cgpu->dev_dead_idle_600_count++;
 			} else if (now.tv_sec - thr->sick.tv_sec > 60 &&
-				   (gpus[i].status == LIFE_SICK || gpus[i].status == LIFE_DEAD)) {
+				   (cgpu->status == LIFE_SICK || cgpu->status == LIFE_DEAD)) {
 				/* Attempt to restart a GPU that's sick or dead once every minute */
 				gettimeofday(&thr->sick, NULL);
 #ifdef HAVE_ADL
-				if (adl_active && gpus[gpu].has_adl && gpu_activity(gpu) > 50) {
+				if (adl_active && cgpu->has_adl && gpu_activity(gpu) > 50) {
 					/* Again do not attempt to restart a device that may have hard hung */
 				} else
 #endif
 				if (opt_restart)
-					reinit_device(thr->cgpu);
+					reinit_device(cgpu);
 			}
 		}
-#endif
 	}
 
 	return NULL;
@@ -5482,7 +5495,7 @@ begin_bench:
 		quit(1, "tq_new failed for gpur_thr_id");
 	if (thr_info_create(thr, NULL, reinit_gpu, thr))
 		quit(1, "reinit_gpu thread create failed");
-#endif
+#endif	
 
 	/* Create API socket thread */
 	api_thr_id = mining_threads + 5;
@@ -5491,6 +5504,7 @@ begin_bench:
 		quit(1, "API thread create failed");
 	pthread_detach(thr->pth);
 
+
 #ifdef HAVE_CURSES
 	/* Create curses input thread for keyboard input. Create this last so
 	 * that we know all threads are created since this can call kill_work
@@ -5529,3 +5543,4 @@ begin_bench:
 
 	return 0;
 }
+
diff --git a/driver-bitforce.c b/driver-bitforce.c
index 23dc39b..d0c99c2 100644
--- a/driver-bitforce.c
+++ b/driver-bitforce.c
@@ -20,6 +20,11 @@
 #include "fpgautils.h"
 #include "miner.h"
 
+#define BITFORCE_SLEEP_MS 3000
+#define BITFORCE_TIMEOUT_MS 10000
+#define BITFORCE_CHECK_INTERVAL_MS 10
+#define WORK_CHECK_INTERVAL_MS 50
+#define MAX_START_DELAY_US 100000
 
 struct device_api bitforce_api;
 
@@ -29,23 +34,19 @@ static void BFgets(char *buf, size_t bufLen, int fd)
 {
 	do
 		--bufLen;
-	while (likely(bufLen && read(fd, buf, 1) && (buf++)[0] != '\n'))
-		;
+	while (likely(bufLen && read(fd, buf, 1) && (buf++)[0] != '\n'));
 	buf[0] = '\0';
 }
 
-static ssize_t BFwrite2(int fd, const void *buf, ssize_t bufLen)
+static ssize_t BFwrite(int fd, const void *buf, ssize_t bufLen)
 {
-	return write(fd, buf, bufLen);
+	if ((bufLen) != write(fd, buf, bufLen)) {
+		applog(LOG_ERR, "BFL: Error writing: %s", buf); 
+		return 0;
+	} else
+		return bufLen;
 }
 
-#define BFwrite(fd, buf, bufLen)  do {  \
-	if ((bufLen) != BFwrite2(fd, buf, bufLen)) {  \
-		applog(LOG_ERR, "Error writing to BitForce (" #buf ")");  \
-		return 0;  \
-	}  \
-} while(0)
-
 #define BFclose(fd) close(fd)
 
 static bool bitforce_detect_one(const char *devpath)
@@ -53,22 +54,22 @@ static bool bitforce_detect_one(const char *devpath)
 	char *s;
 	char pdevbuf[0x100];
 
-	applog(LOG_DEBUG, "BitForce Detect: Attempting to open %s", devpath);
+	applog(LOG_DEBUG, "BFL: Attempting to open %s", devpath);
 
 	int fdDev = BFopen(devpath);
 	if (unlikely(fdDev == -1)) {
-		applog(LOG_ERR, "BitForce Detect: Failed to open %s", devpath);
+		applog(LOG_ERR, "BFL: Failed to open %s", devpath);
 		return false;
 	}
 	BFwrite(fdDev, "ZGX", 3);
 	BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
 	if (unlikely(!pdevbuf[0])) {
-		applog(LOG_ERR, "Error reading from BitForce (ZGX)");
+		applog(LOG_ERR, "BFL: Error reading (ZGX)");
 		return 0;
 	}
 	BFclose(fdDev);
 	if (unlikely(!strstr(pdevbuf, "SHA256"))) {
-		applog(LOG_DEBUG, "BitForce Detect: Didn't recognise BitForce on %s", devpath);
+		applog(LOG_ERR, "BFL: Didn't recognise BitForce on %s", devpath);
 		return false;
 	}
 
@@ -79,11 +80,14 @@ static bool bitforce_detect_one(const char *devpath)
 	bitforce->device_path = strdup(devpath);
 	bitforce->deven = DEV_ENABLED;
 	bitforce->threads = 1;
+	bitforce->sleep_ms = BITFORCE_SLEEP_MS;
 	if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>"))))
 	{
 		s[0] = '\0';
 		bitforce->name = strdup(pdevbuf + 7);
 	}
+	
+	mutex_init(&bitforce->device_mutex);
 
 	return add_cgpu(bitforce);
 }
@@ -119,74 +123,107 @@ static bool bitforce_thread_prepare(struct thr_info *thr)
 
 	int fdDev = BFopen(bitforce->device_path);
 	if (unlikely(-1 == fdDev)) {
-		applog(LOG_ERR, "Failed to open BitForce on %s", bitforce->device_path);
+		applog(LOG_ERR, "BFL%i: Failed to open %s", bitforce->device_id, bitforce->device_path);
 		return false;
 	}
 
 	bitforce->device_fd = fdDev;
 
-	applog(LOG_INFO, "Opened BitForce on %s", bitforce->device_path);
+	applog(LOG_INFO, "BFL%i: Opened %s", bitforce->device_id, bitforce->device_path);
 	gettimeofday(&now, NULL);
 	get_datestamp(bitforce->init, &now);
 
 	return true;
 }
 
-static uint64_t bitforce_scanhash(struct thr_info *thr, struct work *work, uint64_t __maybe_unused max_nonce)
+static void biforce_clear_buffer(struct cgpu_info *bitforce)
 {
-	struct cgpu_info *bitforce = thr->cgpu;
 	int fdDev = bitforce->device_fd;
+	char pdevbuf[0x100];
+
+	applog(LOG_DEBUG, "BFL%i: Clearing read buffer", bitforce->device_id);
+
+	mutex_lock(&bitforce->device_mutex);
+	do {
+		pdevbuf[0] = '\0';
+		BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
+	} while (pdevbuf[0]);
+	mutex_unlock(&bitforce->device_mutex);
+}
 
-	int i, thr_id = thr->id;
+void bitforce_init(struct cgpu_info *bitforce)
+{
+	int fdDev = bitforce->device_fd;
+	char *devpath = bitforce->device_path;
 	char pdevbuf[0x100];
-	unsigned char ob[61] = ">>>>>>>>12345678901234567890123456789012123456789012>>>>>>>>";
-	struct timeval tdiff;
-	char *pnoncebuf;
 	char *s;
-	uint32_t nonce;
 
-	BFwrite(fdDev, "ZDX", 3);
-	BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
-	if (unlikely(!pdevbuf[0])) {
-		applog(LOG_ERR, "Error reading from BitForce (ZDX)");
-		return 0;
-	}
-	if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
-		applog(LOG_ERR, "BitForce ZDX reports: %s", pdevbuf);
-		return 0;
-	}
+	applog(LOG_WARNING, "BFL%i: Re-initalizing", bitforce->device_id);
 
-	memcpy(ob + 8, work->midstate, 32);
-	memcpy(ob + 8 + 32, work->data + 64, 12);
-	BFwrite(fdDev, ob, 60);
-	if (opt_debug) {
-		s = bin2hex(ob + 8, 44);
-		applog(LOG_DEBUG, "BitForce block data: %s", s);
-		free(s);
+	biforce_clear_buffer(bitforce);
+
+	mutex_lock(&bitforce->device_mutex);
+	if (fdDev)
+		BFclose(fdDev);
+	bitforce->device_fd = 0;
+
+	fdDev = BFopen(devpath);
+	if (unlikely(fdDev == -1)) {
+		applog(LOG_ERR, "BFL%i: Failed to open %s", bitforce->device_id, devpath);
+		mutex_unlock(&bitforce->device_mutex);
+		return;
 	}
 
+	BFwrite(fdDev, "ZGX", 3);
 	BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
+	
 	if (unlikely(!pdevbuf[0])) {
-		applog(LOG_ERR, "Error reading from BitForce (block data)");
-		return 0;
+		applog(LOG_ERR, "BFL%i: Error reading (ZGX)", bitforce->device_id);
+		mutex_unlock(&bitforce->device_mutex);
+		return;
 	}
-	if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
-		applog(LOG_ERR, "BitForce block data reports: %s", pdevbuf);
-		return 0;
+
+	if (unlikely(!strstr(pdevbuf, "SHA256"))) {
+		applog(LOG_ERR, "BFL%i: Didn't recognise BitForce on %s returned: %s", bitforce->device_id, devpath, pdevbuf);
+		mutex_unlock(&bitforce->device_mutex);
+		return;
 	}
+	
+	if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>"))))
+	{
+		s[0] = '\0';
+		bitforce->name = strdup(pdevbuf + 7);
+	}
+
+	bitforce->device_fd = fdDev;
+	mutex_unlock(&bitforce->device_mutex);
+}
 
+static bool bitforce_get_temp(struct cgpu_info *bitforce)
+{
+	int fdDev = bitforce->device_fd;
+	char pdevbuf[0x100];
+	char *s;
+
+	if (!fdDev)
+		return false;
+
+	mutex_lock(&bitforce->device_mutex);
 	BFwrite(fdDev, "ZLX", 3);
 	BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
+	mutex_unlock(&bitforce->device_mutex);
+	
 	if (unlikely(!pdevbuf[0])) {
-		applog(LOG_ERR, "Error reading from BitForce (ZKX)");
-		return 0;
+		applog(LOG_ERR, "BFL%i: Error: Get temp returned empty string", bitforce->device_id);
+		bitforce->temp = 0;
+		return false;
 	}
 	if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
 		float temp = strtof(s + 1, NULL);
 		if (temp > 0) {
 			bitforce->temp = temp;
 			if (temp > bitforce->cutofftemp) {
-				applog(LOG_WARNING, "Hit thermal cutoff limit on %s %d, disabling!", bitforce->api->name, bitforce->device_id);
+				applog(LOG_WARNING, "BFL%i: Hit thermal cutoff limit, disabling!", bitforce->device_id);
 				bitforce->deven = DEV_RECOVER;
 
 				bitforce->device_last_not_well = time(NULL);
@@ -195,46 +232,108 @@ static uint64_t bitforce_scanhash(struct thr_info *thr, struct work *work, uint6
 			}
 		}
 	}
+	return true;
+}
 
-	/* Initially wait 2/3 of the average cycle time so we can request more
-	 * work before full scan is up ~ 3.4 seconds */
-	tdiff.tv_sec = 3;
-	tdiff.tv_usec = 4000000;
-	if (!restart_wait(&tdiff))
-		return 0;
-	queue_request(thr, false);
-	i = 3400;
+static bool bitforce_send_work(struct thr_info *thr, struct work *work)
+{
+	struct cgpu_info *bitforce = thr->cgpu;
+	int fdDev = bitforce->device_fd;
+	char pdevbuf[0x100];
+	unsigned char ob[61] = ">>>>>>>>12345678901234567890123456789012123456789012>>>>>>>>";
+	char *s;
 
-	/* Now wait another second; no bistream should be finished by now */
-	tdiff.tv_sec = 1;
-	tdiff.tv_usec = 0;
-	if (!restart_wait(&tdiff))
-		return 0;
-	i += 1000;
+	if (!fdDev)
+		return false;
+re_send:
+	mutex_lock(&bitforce->device_mutex);
+	BFwrite(fdDev, "ZDX", 3);
+	BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
+	if (!pdevbuf[0] || (pdevbuf[0] == 'B')) {
+		mutex_unlock(&bitforce->device_mutex);
+		bitforce->wait_ms += WORK_CHECK_INTERVAL_MS;
+		usleep(WORK_CHECK_INTERVAL_MS*1000);
+		goto re_send;
+	} else if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
+		applog(LOG_ERR, "BFL%i: Error: Send work reports: %s", bitforce->device_id, pdevbuf);
+		mutex_unlock(&bitforce->device_mutex);
+		return false;
+	}
+	memcpy(ob + 8, work->midstate, 32);
+	memcpy(ob + 8 + 32, work->data + 64, 12);
 
-	/* Now start looking for results. Stupid polling every 10ms... */
-	while (42) {
-		if (unlikely(work_restart[thr_id].restart))
-			return 0;
-		usleep(10000);
-		i += 10;
+	BFwrite(fdDev, ob, 60);
+	if (opt_debug) {
+		s = bin2hex(ob + 8, 44);
+		applog(LOG_DEBUG, "BFL%i: block data: %s", bitforce->device_id, s);
+		free(s);
+	}
+	BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
+	mutex_unlock(&bitforce->device_mutex);
+	if (unlikely(!pdevbuf[0])) {
+		applog(LOG_ERR, "BFL%i: Error: Send block data returned empty string", bitforce->device_id);
+		return false;
+	}
+	if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
+		applog(LOG_ERR, "BFL%i: Error: Send block data reports: %s", bitforce->device_id, pdevbuf);
+		return false;
+	}
+	return true;
+}
+
+static uint64_t bitforce_get_result(struct thr_info *thr, struct work *work)
+{
+	struct cgpu_info *bitforce = thr->cgpu;
+	int fdDev = bitforce->device_fd;
+
+	char pdevbuf[0x100];
+	char *pnoncebuf;
+	uint32_t nonce;
+	unsigned int delay_time_ms = BITFORCE_CHECK_INTERVAL_MS;
 
+	if (!fdDev)
+		return 0;
+
+	while (bitforce->wait_ms < BITFORCE_TIMEOUT_MS) {
+		if (unlikely(work_restart[thr->id].restart))
+			return 1;
+		mutex_lock(&bitforce->device_mutex);
 		BFwrite(fdDev, "ZFX", 3);
 		BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
-		if (unlikely(!pdevbuf[0])) {
-			applog(LOG_ERR, "Error reading from BitForce (ZFX)");
-			return 0;
-		}
-		if (pdevbuf[0] != 'B')
-		    break;
+		mutex_unlock(&bitforce->device_mutex);
+		if (pdevbuf[0] && pdevbuf[0] != 'B') /* BFL does not respond during throttling */
+			break;
+		/* if BFL is throttling, no point checking so quickly */
+		delay_time_ms = (pdevbuf[0] ? BITFORCE_CHECK_INTERVAL_MS : 2*WORK_CHECK_INTERVAL_MS);
+		usleep(delay_time_ms*1000);
+		bitforce->wait_ms += delay_time_ms;
 	}
-	applog(LOG_DEBUG, "BitForce waited %dms until %s\n", i, pdevbuf);
+	if (bitforce->wait_ms >= BITFORCE_TIMEOUT_MS) {
+		applog(LOG_ERR, "BFL%i: took longer than 10s", bitforce->device_id);
+		bitforce->device_last_not_well = time(NULL);
+		bitforce->device_not_well_reason = REASON_DEV_OVER_HEAT;
+		bitforce->dev_over_heat_count++;
+		return 1;
+	} else if (pdevbuf[0] == 'N') {/* Hashing complete (NONCE-FOUND or NO-NONCE) */
+	    /* Simple timing adjustment */
+        delay_time_ms = bitforce->sleep_ms;
+		if (bitforce->wait_ms > (bitforce->sleep_ms + BITFORCE_CHECK_INTERVAL_MS))
+			bitforce->sleep_ms += (unsigned int) ((double) (bitforce->wait_ms - bitforce->sleep_ms) / 1.6);
+		else if (bitforce->wait_ms == bitforce->sleep_ms)
+			bitforce->sleep_ms -= BITFORCE_CHECK_INTERVAL_MS;
+        if (delay_time_ms != bitforce->sleep_ms)
+		  applog(LOG_DEBUG, "BFL%i: Wait time changed to: %d. Waited: %d", bitforce->device_id, bitforce->sleep_ms, bitforce->wait_ms);
+	}
+
+	applog(LOG_DEBUG, "BFL%i: waited %dms until %s", bitforce->device_id, bitforce->wait_ms, pdevbuf);
 	work->blk.nonce = 0xffffffff;
-	if (pdevbuf[2] == '-')
-		return 0xffffffff;
+	if (pdevbuf[2] == '-') 
+		return 0xffffffff;   /* No valid nonce found */
+	else if (pdevbuf[0] == 'I') 
+		return 1;          /* Device idle */
 	else if (strncasecmp(pdevbuf, "NONCE-FOUND", 11)) {
-		applog(LOG_ERR, "BitForce result reports: %s", pdevbuf);
-		return 0;
+		applog(LOG_WARNING, "BFL%i: Error: Get result reports: %s", bitforce->device_id, pdevbuf);
+		return 1;
 	}
 
 	pnoncebuf = &pdevbuf[12];
@@ -254,11 +353,95 @@ static uint64_t bitforce_scanhash(struct thr_info *thr, struct work *work, uint6
 	return 0xffffffff;
 }
 
+static void bitforce_shutdown(struct thr_info *thr)
+{
+	struct cgpu_info *bitforce = thr->cgpu;
+
+	BFclose(bitforce->device_fd);
+	bitforce->device_fd = 0;
+}
+
+static void biforce_thread_enable(struct thr_info *thr)
+{
+	struct cgpu_info *bitforce = thr->cgpu;
+
+	bitforce_init(bitforce);
+}
+
+static uint64_t bitforce_scanhash(struct thr_info *thr, struct work *work, uint64_t __maybe_unused max_nonce)
+{
+	struct cgpu_info *bitforce = thr->cgpu;
+	uint64_t ret;
+	struct timeval tdiff;
+	unsigned int sleep_time;
+
+	bitforce->wait_ms = 0;
+	ret = bitforce_send_work(thr, work);
+
+	/* Initially wait 2/3 of the average cycle time so we can request more
+	work before full scan is up */
+	sleep_time = (2*bitforce->sleep_ms) / 3;
+	tdiff.tv_sec = sleep_time/1000;
+	tdiff.tv_usec = sleep_time*1000 - (tdiff.tv_sec * 1000000);
+	if (!restart_wait(&tdiff))
+		return 1;
+	bitforce->wait_ms += sleep_time;
+	queue_request(thr, false);
+
+	/* Now wait athe final 1/3rd; no bitforce should be finished by now */
+	sleep_time = bitforce->sleep_ms - sleep_time;
+	tdiff.tv_sec = sleep_time/1000;
+	tdiff.tv_usec = sleep_time*1000 - (tdiff.tv_sec * 1000000);
+	if (!restart_wait(&tdiff))
+		return 1;
+	bitforce->wait_ms += sleep_time;
+
+	if (ret)
+		ret = bitforce_get_result(thr, work);
+
+	if (!ret) {
+		ret = 1;
+		applog(LOG_ERR, "BFL%i: Comms error", bitforce->device_id);
+		bitforce->device_last_not_well = time(NULL);
+		bitforce->device_not_well_reason = REASON_DEV_NOSTART;
+		bitforce->dev_nostart_count++;
+		/* empty read buffer */
+		biforce_clear_buffer(bitforce);
+	}
+	return ret;
+}
+
+static bool bitforce_get_stats(struct cgpu_info *bitforce)
+{
+	return bitforce_get_temp(bitforce);
+}
+
+static bool bitforce_thread_init(struct thr_info *thr)
+{
+	struct cgpu_info *bitforce = thr->cgpu;
+	unsigned int wait;
+
+	/* Pause each new thread a random time between 0-100ms 
+	so the devices aren't making calls all at the same time. */
+	wait = (rand() * MAX_START_DELAY_US)/RAND_MAX;
+	applog(LOG_DEBUG, "BFL%i: Delaying start by %dms", bitforce->device_id, wait/1000);
+	usleep(wait);
+
+	return true;
+}
+
 struct device_api bitforce_api = {
 	.dname = "bitforce",
 	.name = "BFL",
 	.api_detect = bitforce_detect,
+	.reinit_device = bitforce_init,
 	.get_statline_before = get_bitforce_statline_before,
+	.get_stats = bitforce_get_stats,
 	.thread_prepare = bitforce_thread_prepare,
+	.thread_init = bitforce_thread_init,
 	.scanhash = bitforce_scanhash,
+	.thread_shutdown = bitforce_shutdown,
+	.thread_enable = biforce_thread_enable
 };
+
+
diff --git a/miner.h b/miner.h
index 9965a98..88c0392 100644
--- a/miner.h
+++ b/miner.h
@@ -159,6 +159,10 @@ void *alloca (size_t);
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
 #endif
 
+#ifndef roundl
+#define roundl(x)   (long double)((long long)((x==0)?0.0:((x)+((x)>0)?0.5:-0.5)))
+#endif
+
 enum alive {
 	LIFE_WELL,
 	LIFE_SICK,
@@ -235,6 +239,7 @@ struct device_api {
 	void (*get_statline_before)(char*, struct cgpu_info*);
 	void (*get_statline)(char*, struct cgpu_info*);
 	void (*get_api_stats)(char*, struct cgpu_info*, bool);
+	bool (*get_stats)(struct cgpu_info*);
 
 	// Thread-specific functions
 	bool (*thread_prepare)(struct thr_info*);
@@ -244,6 +249,7 @@ struct device_api {
 	bool (*prepare_work)(struct thr_info*, struct work*);
 	uint64_t (*scanhash)(struct thr_info*, struct work*, uint64_t);
 	void (*thread_shutdown)(struct thr_info*);
+	void (*thread_enable)(struct thr_info*);
 };
 
 enum dev_enable {
@@ -314,12 +320,17 @@ struct cgpu_info {
 #endif
 		int device_fd;
 	};
+#ifdef USE_BITFORCE
+	unsigned int wait_ms;
+	unsigned int sleep_ms;
+#endif
 	pthread_mutex_t		device_mutex;
 
 	enum dev_enable deven;
 	int accepted;
 	int rejected;
 	int hw_errors;
+	unsigned int low_count;
 	double rolling;
 	double total_mhashes;
 	double utility;
@@ -794,3 +805,4 @@ extern void adl(void);
 extern void app_restart(void);
 
 #endif /* __MINER_H__ */
+