Commit 5dfc8b694f95159857aaabff60922c8755b9c08b

Luke Dashjr 2012-01-08T19:56:15

BitForce FPGA support cgminer will scan for and mine BitForce FPGAs on USB ports by providing the new --scan-serial <device> option, or autodetect them by searching /dev/serial/by-id for *BitFORCE_SHA256*

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
diff --git a/Makefile.am b/Makefile.am
index da4a365..85ff715 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -60,3 +60,7 @@ AM_CFLAGS	= -DHAS_YASM
 endif
 endif
 endif
+
+if USE_BITFORCE
+cgminer_SOURCES += bitforce.c
+endif
diff --git a/bitforce.c b/bitforce.c
new file mode 100644
index 0000000..f548b07
--- /dev/null
+++ b/bitforce.c
@@ -0,0 +1,219 @@
+/*
+ * Copyright 2012 Luke Dashjr
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.  See COPYING for more details.
+ */
+
+#include <limits.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <termios.h>
+#include <unistd.h>
+
+#include "elist.h"
+#include "miner.h"
+
+
+struct device_api bitforce_api;
+
+static bool bitforce_detect_one(const char *devpath)
+{
+	char pdevbuf[0x100];
+	int i = 0;
+
+	if (total_devices == MAX_DEVICES)
+		return false;
+
+	FILE *fileDev = fopen(devpath, "r+b");
+	if (unlikely(!fileDev))
+	{
+		applog(LOG_DEBUG, "BitForce Detect: Failed to open %s", devpath);
+		return false;
+	}
+	setbuf(fileDev, NULL);
+	fprintf(fileDev, "ZGX");
+	if (!fgets(pdevbuf, sizeof(pdevbuf), fileDev))
+	{
+		applog(LOG_ERR, "Error reading from BitForce (ZGX)");
+		return 0;
+	}
+	fclose(fileDev);
+	if (unlikely(!strstr(pdevbuf, "SHA256")))
+	{
+		applog(LOG_DEBUG, "BitForce Detect: Didn't recognize BitForce on %s", devpath);
+		return false;
+	}
+
+	// We have a real BitForce!
+	struct cgpu_info *bitforce;
+	bitforce = calloc(1, sizeof(*bitforce));
+	devices[total_devices++] = bitforce;
+	bitforce->api = &bitforce_api;
+	bitforce->device_id = i++;
+	bitforce->device_path = strdup(devpath);
+	bitforce->enabled = true;
+	bitforce->threads = 1;
+
+	return true;
+}
+
+static void bitforce_detect_auto()
+{
+	DIR *D;
+	struct dirent *de;
+	const char udevdir[] = "/dev/serial/by-id";
+	char devpath[sizeof(udevdir) + 1 + NAME_MAX];
+	char *devfile = devpath + sizeof(udevdir);
+
+	D = opendir(udevdir);
+	if (!D)
+		return;
+	memcpy(devpath, udevdir, sizeof(udevdir) - 1);
+	devpath[sizeof(udevdir) - 1] = '/';
+	while ( (de = readdir(D)) ) {
+		if (!strstr(de->d_name, "BitFORCE_SHA256"))
+			continue;
+		strcpy(devfile, de->d_name);
+		bitforce_detect_one(devpath);
+	}
+	closedir(D);
+}
+
+static void bitforce_detect()
+{
+	struct string_elist *iter, *tmp;
+
+	list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
+		if (bitforce_detect_one(iter->string))
+			string_elist_del(iter);
+	}
+
+	bitforce_detect_auto();
+}
+
+static bool bitforce_thread_prepare(struct thr_info *thr)
+{
+	struct cgpu_info *bitforce = thr->cgpu;
+
+	struct timeval now;
+
+	FILE *fileDev = fopen(bitforce->device_path, "r+b");
+	if (unlikely(!fileDev))
+	{
+		applog(LOG_ERR, "Failed to open BitForce on %s", bitforce->device_path);
+		return false;
+	}
+
+	{
+		int nDevFD = fileno(fileDev);
+		struct termios pattr;
+		tcgetattr(nDevFD, &pattr);
+		pattr.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
+		pattr.c_oflag &= ~OPOST;
+		pattr.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
+		pattr.c_cflag &= ~(CSIZE | PARENB);
+		pattr.c_cflag |= CS8;
+		tcsetattr(nDevFD, TCSANOW, &pattr);
+	}
+	setbuf(fileDev, NULL);
+	bitforce->device_file = fileDev;
+
+	applog(LOG_INFO, "Opened BitForce on %s", 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 max_nonce)
+{
+	struct cgpu_info *bitforce = thr->cgpu;
+	FILE *fileDev = bitforce->device_file;
+
+	char pdevbuf[0x100];
+	unsigned char ob[61] = ">>>>>>>>12345678901234567890123456789012123456789012>>>>>>>>";
+	int i;
+	char *pnoncebuf;
+	uint32_t nonce;
+
+	fprintf(fileDev, "ZDX");
+	if (!fgets(pdevbuf, sizeof(pdevbuf), fileDev)) {
+		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;
+	}
+
+	memcpy(ob + 8, work->midstate, 32);
+	memcpy(ob + 8 + 32, work->data + 64, 12);
+	fwrite(ob, 60, 1, fileDev);
+	applog(LOG_DEBUG, "BitForce block data: %s", bin2hex(ob + 8, 44));
+
+	if (!fgets(pdevbuf, sizeof(pdevbuf), fileDev))
+	{
+		applog(LOG_ERR, "Error reading from BitForce (block data)");
+		return 0;
+	}
+	if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K'))
+	{
+		applog(LOG_ERR, "BitForce block data reports: %s", pdevbuf);
+		return 0;
+	}
+
+	usleep(4500000);
+	i = 4500;
+	while (1) {
+		fprintf(fileDev, "ZFX");
+		if (!fgets(pdevbuf, sizeof(pdevbuf), fileDev))
+		{
+			applog(LOG_ERR, "Error reading from BitForce (ZFX)");
+			return 0;
+		}
+		if (pdevbuf[0] != 'B')
+		    break;
+		usleep(10000);
+		i += 10;
+	}
+	applog(LOG_DEBUG, "BitForce waited %dms until %s\n", i, pdevbuf);
+	work->blk.nonce = 0xffffffff;
+	if (pdevbuf[2] == '-')
+		return 0xffffffff;
+	else
+	if (strncasecmp(pdevbuf, "NONCE-FOUND", 11)) {
+		applog(LOG_ERR, "BitForce result reports: %s", pdevbuf);
+		return 0;
+	}
+
+	pnoncebuf = &pdevbuf[12];
+
+	while (1) {
+		hex2bin((void*)&nonce, pnoncebuf, 4);
+#ifndef __BIG_ENDIAN__
+		nonce = swab32(nonce);
+#endif
+
+		submit_nonce(thr, work, nonce);
+		if (pnoncebuf[8] != ',')
+			break;
+		pnoncebuf += 9;
+	}
+
+	return 0xffffffff;
+}
+
+struct device_api bitforce_api = {
+	.name = "BFL",
+	.api_detect = bitforce_detect,
+	// .reinit_device = TODO
+	.thread_prepare = bitforce_thread_prepare,
+	.scanhash = bitforce_scanhash,
+};
diff --git a/configure.ac b/configure.ac
index f5077c2..d3b55a4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -181,6 +181,15 @@ else
 	DLOPEN_FLAGS=""
 fi
 
+bitforce=yes
+AC_ARG_ENABLE([bitforce],
+	[AC_HELP_STRING([--disable-bitforce],[Don't compile support for BitForce FPGAs])],
+	[bitforce=$enableval]
+)
+if test "x$bitforce" != xno; then
+	AC_DEFINE([USE_BITFORCE], [1], [Defined to 1 if BitForce support is wanted.])
+fi
+
 AC_SEARCH_LIBS(addstr, ncurses pdcurses, ,
         AC_MSG_ERROR([Could not find curses library - please install libncurses-dev or pdcurses-dev]))
 
@@ -190,6 +199,7 @@ AC_CHECK_LIB(pdcurses, addstr, PDCURSES_LIBS=-lpdcurses)
 AM_CONDITIONAL([WANT_JANSSON], [test x$request_jansson = xtrue])
 AM_CONDITIONAL([HAVE_WINDOWS], [test x$have_win32 = xtrue])
 AM_CONDITIONAL([HAVE_x86_64], [test x$have_x86_64 = xtrue])
+AM_CONDITIONAL([USE_BITFORCE], [test x$bitforce != xno])
 
 if test x$request_jansson = xtrue
 then
@@ -312,18 +322,20 @@ echo
 echo "Configuration Options Summary:"
 echo
 
+echo "  BitForce.FPGAs.......: $bitforce"
+
 if test "x$opencl" != xno; then
 	if test $found_opencl = 1; then
 		echo "  OpenCL...............: FOUND. GPU mining support enabled"
 	else
 		echo "  OpenCL...............: NOT FOUND. GPU mining support DISABLED"
-		if test "x$cpumining" != xyes; then
+		if test "x$cpumining$bitforce" = xnono; then
 			AC_MSG_ERROR([No mining configured in])
 		fi
 	fi
 else
 	echo "  OpenCL...............: Detection overrided. GPU mining support DISABLED"
-	if test "x$cpumining" != xyes; then
+	if test "x$cpumining$bitforce" = xnono; then
 		AC_MSG_ERROR([No mining configured in])
 	fi
 fi
diff --git a/main.c b/main.c
index 5571ec1..1f5a8c4 100644
--- a/main.c
+++ b/main.c
@@ -216,6 +216,7 @@ static bool opt_restart = true;
 static bool opt_nogpu;
 #endif
 
+struct list_head scan_devices;
 int nDevs;
 static int opt_g_threads = 2;
 static signed int devices_enabled = 0;
@@ -1003,6 +1004,12 @@ static char *set_float_0_to_99(const char *arg, float *f)
 	return NULL;
 }
 
+static char *add_serial(char *arg)
+{
+	string_elist_add(arg, &scan_devices);
+	return NULL;
+}
+
 static char *set_devices(char *arg)
 {
 	int i = strtol(arg, &arg, 0);
@@ -1670,6 +1677,11 @@ static struct opt_table opt_config_table[] = {
 	OPT_WITHOUT_ARG("--round-robin",
 		     set_rr, &pool_strategy,
 		     "Change multipool strategy from failover to round robin on failure"),
+#ifdef USE_BITFORCE
+	OPT_WITH_ARG("--scan-serial|-S",
+		     add_serial, NULL, NULL,
+		     "Serial port to probe for BitForce device"),
+#endif
 	OPT_WITH_ARG("--scan-time|-s",
 		     set_int_0_to_9999, opt_show_intval, &opt_scantime,
 		     "Upper bound on time spent scanning current work, in seconds"),
@@ -4340,6 +4352,7 @@ static inline bool abandon_work(int thr_id, struct work *work, struct timeval *w
 {
 	if (wdiff->tv_sec > opt_scantime ||
 	    work->blk.nonce >= MAXTHREADS - hashes ||
+	    hashes >= 0xfffffffe ||
 	    stale_work(work, false))
 		return true;
 	return false;
@@ -4432,7 +4445,7 @@ static void *miner_thread(void *userdata)
 				}
 			}
 
-			if (sdiff.tv_sec < cycle) {
+			if (unlikely(sdiff.tv_sec < cycle)) {
 				if (likely(!api->can_limit_work || max_nonce == 0xffffffff))
 					continue;
 
@@ -5386,7 +5399,7 @@ static void cpu_detect()
 	#endif /* !WIN32 */
 
 	if (opt_n_threads < 0 || !forced_n_threads) {
-		if (nDevs && !opt_usecpu)
+		if (total_devices && !opt_usecpu)
 			opt_n_threads = 0;
 		else
 			opt_n_threads = num_processors;
@@ -5803,6 +5816,12 @@ struct device_api opencl_api = {
 };
 #endif
 
+
+#ifdef USE_BITFORCE
+extern struct device_api bitforce_api;
+#endif
+
+
 static int cgminer_id_count = 0;
 
 void enable_device(struct cgpu_info *cgpu)
@@ -5877,6 +5896,8 @@ int main (int argc, char *argv[])
 	HASH_ADD_STR(blocks, hash, block);
 	strcpy(current_block, block->hash);
 
+	INIT_LIST_HEAD(&scan_devices);
+
 	memset(gpus, 0, sizeof(gpus));
 	for (i = 0; i < MAX_GPUDEVICES; i++)
 		gpus[i].dynamic = true;
@@ -5946,6 +5967,10 @@ int main (int argc, char *argv[])
 		opencl_api.api_detect();
 #endif
 
+#ifdef USE_BITFORCE
+	bitforce_api.api_detect();
+#endif
+
 #ifdef WANT_CPUMINE
 	cpu_api.api_detect();
 #endif
diff --git a/miner.h b/miner.h
index 86352a3..eb29058 100644
--- a/miner.h
+++ b/miner.h
@@ -236,6 +236,9 @@ struct cgpu_info {
 	int cgminer_id;
 	struct device_api *api;
 	int device_id;
+	char *device_path;
+	FILE *device_file;
+
 	bool enabled;
 	int accepted;
 	int rejected;
@@ -297,6 +300,32 @@ struct thr_info {
 extern int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
 extern void thr_info_cancel(struct thr_info *thr);
 
+
+struct string_elist {
+	char *string;
+	bool free_me;
+
+	struct list_head list;
+};
+
+static inline void string_elist_add(const char *s, struct list_head *head)
+{
+	struct string_elist *n;
+
+	n = calloc(1, sizeof(*n));
+	n->string = strdup(s);
+	n->free_me = true;
+	list_add_tail(&n->list, head);
+}
+
+static inline void string_elist_del(struct string_elist *item)
+{
+	if (item->free_me)
+		free(item->string);
+	list_del(&item->list);
+}
+
+
 static inline uint32_t swab32(uint32_t v)
 {
 	return bswap_32(v);
@@ -468,6 +497,7 @@ extern void api(void);
 #define MAX_DEVICES 32
 #define MAX_POOLS (32)
 
+extern struct list_head scan_devices;
 extern int nDevs;
 extern int opt_n_threads;
 extern int num_processors;
@@ -581,6 +611,7 @@ enum cl_kernel {
 	KL_PHATK,
 };
 
+extern void get_datestamp(char *, struct timeval *);
 bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
 extern void wlogprint(const char *f, ...);
 extern int curses_int(const char *query);