Browse Source

Merge branch 'bitfury_cleanup' into bfgminer

Luke Dashjr 12 years ago
parent
commit
5e6543e67b
8 changed files with 93 additions and 571 deletions
  1. 3 3
      driver-bfsb.c
  2. 31 242
      driver-bitfury.c
  3. 0 2
      driver-bitfury.h
  4. 2 2
      driver-littlefury.c
  5. 1 6
      driver-metabank.c
  6. 1 1
      driver-nanofury.c
  7. 48 282
      libbitfury.c
  8. 7 33
      libbitfury.h

+ 3 - 3
driver-bfsb.c

@@ -146,7 +146,7 @@ bool bfsb_init(struct thr_info *thr)
 		bitfury->spi->cgpu = proc;
 		bitfury_init_chip(proc);
 		bitfury->osc6_bits = 53;
-		send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
+		bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
 		bitfury_init_freq_stat(&bitfury->chip_stat, 52, 56);
 		
 		if (proc->proc_id == proc->procs - 1)
@@ -165,7 +165,7 @@ void bfsb_disable(struct thr_info * const thr)
 	struct bitfury_device * const bitfury = proc->device_data;
 	
 	applog(LOG_DEBUG, "%"PRIpreprv": Shutting down chip (disable)", proc->proc_repr);
-	send_shutdown(bitfury->spi, bitfury->slot, bitfury->fasync);
+	bitfury_send_shutdown(bitfury->spi, bitfury->slot, bitfury->fasync);
 }
 
 static
@@ -175,7 +175,7 @@ void bfsb_enable(struct thr_info * const thr)
 	struct bitfury_device * const bitfury = proc->device_data;
 	
 	applog(LOG_DEBUG, "%"PRIpreprv": Reinitialising chip (enable)", proc->proc_repr);
-	send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
+	bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
 	bitfury_init_chip(proc);
 }
 

+ 31 - 242
driver-bitfury.c

@@ -37,14 +37,8 @@
 #include "util.h"
 #include "spidevc.h"
 
-#define GOLDEN_BACKLOG 5
-#define LINE_LEN 2048
-
 struct device_drv bitfury_drv;
 
-int calc_stat(time_t * stat_ts, time_t stat, struct timeval now);
-double shares_to_ghashes(int shares, int seconds);
-
 static
 int bitfury_autodetect()
 {
@@ -179,7 +173,7 @@ bool bitfury_init_chip(struct cgpu_info * const proc)
 		.ntime = 0xb6c24563,
 		.nbits = 0x6dfa4352,
 	};
-	payload_to_atrvec(bitfury->atrvec, &payload);
+	bitfury_payload_to_atrvec(bitfury->atrvec, &payload);
 	return bitfury_init_oldbuf(proc, NULL);
 }
 
@@ -197,234 +191,15 @@ bool bitfury_init(struct thr_info *thr)
 			.fasync = proc->proc_id,
 		};
 		bitfury_init_chip(proc);
+		bitfury->osc6_bits = 50;
+		bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
 	}
 	
-	return true;
-}
-
-static
-bool bitfury_queue_full(struct cgpu_info *cgpu)
-{
-	struct cgpu_info *proc;
-	struct bitfury_device *bitfury;
-	
-	for (proc = cgpu; proc; proc = proc->next_proc)
-	{
-		bitfury = proc->device_data;
-		
-		if (bitfury->work)
-			continue;
-		
-		bitfury->work = get_queued(cgpu);
-		if (!bitfury->work)
-			return false;
-		
-		work_to_payload(&bitfury->payload, bitfury->work);
-	}
+	timer_set_now(&thr->tv_poll);
 	
 	return true;
 }
 
-int64_t bitfury_scanHash(struct thr_info *thr)
-{
-	struct cgpu_info * const cgpu = thr->cgpu;
-	struct bitfury_device * const sds = cgpu->device_data;
-	struct cgpu_info *proc;
-	struct thr_info *pthr;
-	struct bitfury_device *bitfury;
-	struct timeval now;
-	char line[LINE_LEN];
-	int short_stat = 10;
-	int long_stat = 1800;
-	int i;
-
-	if (!bitfury_queue_full(cgpu))
-		return 0;
-	
-	for (proc = cgpu; proc; proc = proc->next_proc)
-	{
-		const int chip = proc->proc_id;
-		pthr = proc->thr[0];
-		bitfury = proc->device_data;
-		
-		bitfury->job_switched = 0;
-		payload_to_atrvec(bitfury->atrvec, &bitfury->payload);
-		libbitfury_sendHashData1(chip, bitfury, pthr);
-	}
-
-	cgsleep_ms(5);
-
-	cgtime(&now);
-	for (proc = cgpu; proc; proc = proc->next_proc)
-	{
-		pthr = proc->thr[0];
-		bitfury = proc->device_data;
-		
-		if (bitfury->job_switched) {
-			int i,j;
-			unsigned int * const res = bitfury->results;
-			struct work * const work = bitfury->work;
-			struct work * const owork = bitfury->owork;
-			struct work * const o2work = bitfury->o2work;
-			i = bitfury->results_n;
-			for (j = i - 1; j >= 0; j--) {
-				if (owork) {
-					submit_nonce(pthr, owork, bswap_32(res[j]));
-					bitfury->stat_ts[bitfury->stat_counter++] =
-						now.tv_sec;
-					if (bitfury->stat_counter == BITFURY_STAT_N) {
-						bitfury->stat_counter = 0;
-					}
-				}
-				if (o2work) {
-					// TEST
-					//submit_nonce(pthr, owork, bswap_32(res[j]));
-				}
-			}
-			bitfury->results_n = 0;
-			bitfury->job_switched = 0;
-			if (bitfury->old_nonce && o2work) {
-					submit_nonce(pthr, o2work, bswap_32(bitfury->old_nonce));
-					i++;
-			}
-			if (bitfury->future_nonce) {
-					submit_nonce(pthr, work, bswap_32(bitfury->future_nonce));
-					i++;
-			}
-
-			if (o2work)
-				work_completed(cgpu, o2work);
-
-			bitfury->o2work = bitfury->owork;
-			bitfury->owork = bitfury->work;
-			bitfury->work = NULL;
-			hashes_done2(pthr, 0xbd000000, NULL);
-		}
-	}
-
-	if (now.tv_sec - sds->short_out_t > short_stat) {
-		int shares_first = 0, shares_last = 0, shares_total = 0;
-		char stat_lines[32][LINE_LEN] = {{0}};
-		int len, k;
-		double gh[32][8] = {{0}};
-		double ghsum = 0, gh1h = 0, gh2h = 0;
-		unsigned strange_counter = 0;
-
-		for (proc = cgpu; proc; proc = proc->next_proc)
-		{
-			const int chip = proc->proc_id;
-			bitfury = proc->device_data;
-			
-			int shares_found = calc_stat(bitfury->stat_ts, short_stat, now);
-			double ghash;
-			len = strlen(stat_lines[bitfury->slot]);
-			ghash = shares_to_ghashes(shares_found, short_stat);
-			gh[bitfury->slot][chip & 0x07] = ghash;
-			snprintf(stat_lines[bitfury->slot] + len, LINE_LEN - len, "%.1f-%3.0f ", ghash, bitfury->mhz);
-
-			if(sds->short_out_t && ghash < 0.5) {
-				applog(LOG_WARNING, "Chip_id %d FREQ CHANGE", chip);
-				send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits - 1);
-				cgsleep_ms(1);
-				send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
-			}
-			shares_total += shares_found;
-			shares_first += chip < 4 ? shares_found : 0;
-			shares_last += chip > 3 ? shares_found : 0;
-			strange_counter += bitfury->strange_counter;
-			bitfury->strange_counter = 0;
-		}
-		sprintf(line, "vvvvwww SHORT stat %ds: wwwvvvv", short_stat);
-		applog(LOG_WARNING, "%s", line);
-		sprintf(line, "stranges: %u", strange_counter);
-		applog(LOG_WARNING, "%s", line);
-		for(i = 0; i < 32; i++)
-			if(strlen(stat_lines[i])) {
-				len = strlen(stat_lines[i]);
-				ghsum = 0;
-				gh1h = 0;
-				gh2h = 0;
-				for(k = 0; k < 4; k++) {
-					gh1h += gh[i][k];
-					gh2h += gh[i][k+4];
-					ghsum += gh[i][k] + gh[i][k+4];
-				}
-				snprintf(stat_lines[i] + len, LINE_LEN - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
-				applog(LOG_WARNING, "%s", stat_lines[i]);
-			}
-		sds->short_out_t = now.tv_sec;
-	}
-
-	if (now.tv_sec - sds->long_out_t > long_stat) {
-		int shares_first = 0, shares_last = 0, shares_total = 0;
-		char stat_lines[32][LINE_LEN] = {{0}};
-		int len, k;
-		double gh[32][8] = {{0}};
-		double ghsum = 0, gh1h = 0, gh2h = 0;
-
-		for (proc = cgpu; proc; proc = proc->next_proc)
-		{
-			const int chip = proc->proc_id;
-			bitfury = proc->device_data;
-			
-			int shares_found = calc_stat(bitfury->stat_ts, long_stat, now);
-			double ghash;
-			len = strlen(stat_lines[bitfury->slot]);
-			ghash = shares_to_ghashes(shares_found, long_stat);
-			gh[bitfury->slot][chip & 0x07] = ghash;
-			snprintf(stat_lines[bitfury->slot] + len, LINE_LEN - len, "%.1f-%3.0f ", ghash, bitfury->mhz);
-
-			shares_total += shares_found;
-			shares_first += chip < 4 ? shares_found : 0;
-			shares_last += chip > 3 ? shares_found : 0;
-		}
-		sprintf(line, "!!!_________ LONG stat %ds: ___________!!!", long_stat);
-		applog(LOG_WARNING, "%s", line);
-		for(i = 0; i < 32; i++)
-			if(strlen(stat_lines[i])) {
-				len = strlen(stat_lines[i]);
-				ghsum = 0;
-				gh1h = 0;
-				gh2h = 0;
-				for(k = 0; k < 4; k++) {
-					gh1h += gh[i][k];
-					gh2h += gh[i][k+4];
-					ghsum += gh[i][k] + gh[i][k+4];
-				}
-				snprintf(stat_lines[i] + len, LINE_LEN - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
-				applog(LOG_WARNING, "%s", stat_lines[i]);
-			}
-		sds->long_out_t = now.tv_sec;
-	}
-
-	return 0;
-}
-
-double shares_to_ghashes(int shares, int seconds) {
-	return (double)shares / (double)seconds * 4.84387;  //orig: 4.77628
-}
-
-int calc_stat(time_t * stat_ts, time_t stat, struct timeval now) {
-	int j;
-	int shares_found = 0;
-	for(j = 0; j < BITFURY_STAT_N; j++) {
-		if (now.tv_sec - stat_ts[j] < stat) {
-			shares_found++;
-		}
-	}
-	return shares_found;
-}
-
-bool bitfury_prepare(struct thr_info *thr)
-{
-	struct cgpu_info *cgpu = thr->cgpu;
-
-	get_now_datestamp(cgpu->init, sizeof(cgpu->init));
-
-	applog(LOG_INFO, "INFO bitfury_prepare");
-	return true;
-}
-
 void bitfury_shutdown(struct thr_info *thr) {
 	struct cgpu_info *cgpu = thr->cgpu, *proc;
 	struct bitfury_device *bitfury;
@@ -433,7 +208,7 @@ void bitfury_shutdown(struct thr_info *thr) {
 	for (proc = cgpu; proc; proc = proc->next_proc)
 	{
 		bitfury = proc->device_data;
-		send_shutdown(bitfury->spi, bitfury->slot, bitfury->fasync);
+		bitfury_send_shutdown(bitfury->spi, bitfury->slot, bitfury->fasync);
 	}
 }
 
@@ -449,8 +224,8 @@ bool bitfury_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused
 		applog(LOG_DEBUG, "%"PRIpreprv": Preparing work %s",
 		       proc->proc_repr, hex);
 	}
-	work_to_payload(&bitfury->payload, work);
-	payload_to_atrvec(bitfury->atrvec, &bitfury->payload);
+	work_to_bitfury_payload(&bitfury->payload, work);
+	bitfury_payload_to_atrvec(bitfury->atrvec, &bitfury->payload);
 	
 	work->blk.nonce = 0xffffffff;
 	return true;
@@ -502,7 +277,8 @@ void bitfury_clean_freq_stat(struct freq_stat * const c)
 
 typedef uint32_t bitfury_inp_t[0x11];
 
-int select_freq(struct bitfury_device *bitfury, struct cgpu_info *proc) {
+static
+int bitfury_select_freq(struct bitfury_device *bitfury, struct cgpu_info *proc) {
 	int freq;
 	int random;
 	int i;
@@ -544,7 +320,7 @@ int select_freq(struct bitfury_device *bitfury, struct cgpu_info *proc) {
 	applog(LOG_DEBUG, "%"PRIpreprv": Changing osc6_bits to %d",
 	       proc->proc_repr, freq);
 	bitfury->osc6_bits = freq;
-	send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
+	bitfury_send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
 	return 0;
 }
 
@@ -671,7 +447,7 @@ void bitfury_do_io(struct thr_info * const master_thr)
 				inc_hw_errors2(thr, NULL, NULL);
 				applog(LOG_DEBUG, "%"PRIpreprv": Full result match, reinitialising",
 				       proc->proc_repr);
-				send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
+				bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
 				bitfury->desync_counter = 99;
 				goto out;
 			}
@@ -736,7 +512,7 @@ void bitfury_do_io(struct thr_info * const master_thr)
 				
 				// Change freq;
 				if (!c->best_done) {
-					select_freq(bitfury, proc);
+					bitfury_select_freq(bitfury, proc);
 				} else {
 					applog(LOG_DEBUG, "%"PRIpreprv": Stable freq, osc6_bits: %d",
 					       proc->proc_repr, bitfury->osc6_bits);
@@ -776,7 +552,7 @@ void bitfury_do_io(struct thr_info * const master_thr)
 					{
 						applog(LOG_WARNING, "%"PRIpreprv": %d of the last %d results were bad, reinitialising",
 						       proc->proc_repr, bitfury->sample_hwe, bitfury->sample_tot);
-						send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
+						bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
 						bitfury->desync_counter = 99;
 					}
 					bitfury->sample_tot = bitfury->sample_hwe = 0;
@@ -794,7 +570,7 @@ out:
 		{
 			applog(LOG_DEBUG, "%"PRIpreprv": Forcing reinitialisation",
 			       proc->proc_repr);
-			send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
+			bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
 			bitfury->desync_counter = 99;
 			bitfury->force_reinit = false;
 		}
@@ -940,11 +716,24 @@ struct device_drv bitfury_drv = {
 	.dname = "bitfury_gpio",
 	.name = "BFY",
 	.drv_detect = bitfury_detect,
-	.thread_prepare = bitfury_prepare,
+	
 	.thread_init = bitfury_init,
-	.queue_full = bitfury_queue_full,
-	.scanwork = bitfury_scanHash,
 	.thread_shutdown = bitfury_shutdown,
-	.minerloop = hash_queued_work,
+	
+	.minerloop = minerloop_async,
+	.job_prepare = bitfury_job_prepare,
+	.job_start = bitfury_noop_job_start,
+	.poll = bitfury_do_io,
+	.job_process_results = bitfury_job_process_results,
+	
+	.get_api_extra_device_detail = bitfury_api_device_detail,
+	.get_api_extra_device_status = bitfury_api_device_status,
+	.set_device = bitfury_set_device,
+	
+#ifdef HAVE_CURSES
+	.proc_wlogprint_status = bitfury_wlogprint_status,
+	.proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
+	.proc_tui_handle_choice = bitfury_tui_handle_choice,
+#endif
 };
 

+ 0 - 2
driver-bitfury.h

@@ -13,8 +13,6 @@ extern bool bitfury_init_chip(struct cgpu_info *);
 extern void bitfury_init_freq_stat(struct freq_stat *, int osc6_min, int osc6_max);
 extern void bitfury_clean_freq_stat(struct freq_stat *);
 
-extern int64_t bitfury_scanHash(struct thr_info *);
-
 extern bool bitfury_job_prepare(struct thr_info *, struct work *, uint64_t max_nonce);
 extern void bitfury_noop_job_start(struct thr_info *);
 extern void bitfury_do_io(struct thr_info *);

+ 2 - 2
driver-littlefury.c

@@ -361,7 +361,7 @@ void littlefury_disable(struct thr_info * const thr)
 	struct bitfury_device * const bitfury = proc->device_data;
 	struct cgpu_info * const dev = proc->device;
 	
-	send_shutdown(bitfury->spi, 0, bitfury->fasync);
+	bitfury_send_shutdown(bitfury->spi, 0, bitfury->fasync);
 	
 	// If all chips disabled, kill power and close device
 	bool any_running = false;
@@ -438,7 +438,7 @@ void littlefury_poll(struct thr_info * const master_thr)
 		for (proc = dev; proc; proc = proc->next_proc)
 		{
 			struct bitfury_device * const bitfury = proc->device_data;
-			send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
+			bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
 			bitfury_init_chip(proc);
 		}
 	}

+ 1 - 6
driver-metabank.c

@@ -160,7 +160,7 @@ bool metabank_init(struct thr_info *thr)
 		bitfury->spi->cgpu = proc;
 		bitfury_init_chip(proc);
 		bitfury->osc6_bits = 53;
-		send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
+		bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
 		bitfury_init_freq_stat(&bitfury->chip_stat, 52, 56);
 		
 		if (proc->proc_id == proc->procs - 1)
@@ -228,11 +228,6 @@ struct device_drv metabank_drv = {
 	.drv_detect = metabank_detect,
 	.thread_init = metabank_init,
 	
-#if 0
-	.minerloop = hash_queued_work,
-	.thread_prepare = bitfury_prepare,
-	.scanwork = bitfury_scanHash,
-#endif
 	.minerloop = minerloop_async,
 	.job_prepare = bitfury_job_prepare,
 	.job_start = bitfury_noop_job_start,

+ 1 - 1
driver-nanofury.c

@@ -274,7 +274,7 @@ bool nanofury_init(struct thr_info * const thr)
 	};
 	cgpu->device_data = bitfury;
 	bitfury->osc6_bits = 50;
-	send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
+	bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
 	bitfury_init_chip(cgpu);
 	
 	timer_set_now(&thr->tv_poll);

+ 48 - 282
libbitfury.c

@@ -45,7 +45,8 @@
 unsigned bitfury_decnonce(unsigned in);
 
 /* Configuration registers - control oscillators and such stuff. PROGRAMMED when magic number is matches, UNPROGRAMMED (default) otherwise */
-void config_reg(struct spi_port *port, int cfgreg, int ena)
+static
+void bitfury_config_reg(struct spi_port *port, int cfgreg, int ena)
 {
 	static const uint8_t enaconf[4] = { 0xc1, 0x6a, 0x59, 0xe3 };
 	static const uint8_t disconf[4] = { 0, 0, 0, 0 };
@@ -56,14 +57,11 @@ void config_reg(struct spi_port *port, int cfgreg, int ena)
 
 #define FIRST_BASE 61
 #define SECOND_BASE 4
-const int8_t counters[16] = { 64, 64,
+static
+const int8_t bitfury_counters[16] = { 64, 64,
 	SECOND_BASE, SECOND_BASE+4, SECOND_BASE+2, SECOND_BASE+2+16, SECOND_BASE, SECOND_BASE+1,
 	(FIRST_BASE)%65,  (FIRST_BASE+1)%65,  (FIRST_BASE+3)%65, (FIRST_BASE+3+16)%65, (FIRST_BASE+4)%65, (FIRST_BASE+4+4)%65, (FIRST_BASE+3+3)%65, (FIRST_BASE+3+1+3)%65};
 
-//char counters[16] = { 64, 64,
-//	SECOND_BASE, SECOND_BASE+4, SECOND_BASE+2, SECOND_BASE+2+16, SECOND_BASE, SECOND_BASE+1,
-//	(FIRST_BASE)%65,  (FIRST_BASE+1)%65,  (FIRST_BASE+3)%65, (FIRST_BASE+3+16)%65, (FIRST_BASE+4)%65, (FIRST_BASE+4+4)%65, (FIRST_BASE+3+3)%65, (FIRST_BASE+3+1+3)%65};
-
 /* Oscillator setup variants (maybe more), values inside of chip ANDed to not allow by programming errors work it at higher speeds  */
 /* WARNING! no chip temperature control limits, etc. It may self-fry and make fried chips with great ease :-) So if trying to overclock */
 /* Do not place chip near flammable objects, provide adequate power protection and better wear eye protection ! */
@@ -93,7 +91,8 @@ static const unsigned SHA_K[64] = {
 
 
 
-void ms3_compute(unsigned *p)
+static
+void libbitfury_ms3_compute(unsigned *p)
 {
 	unsigned a,b,c,d,e,f,g,h, ne, na,  i;
 
@@ -109,18 +108,20 @@ void ms3_compute(unsigned *p)
 	p[15] = a; p[14] = b; p[13] = c; p[12] = d; p[11] = e; p[10] = f; p[9] = g; p[8] = h;
 }
 
-void send_conf(struct spi_port *port) {
+static
+void bitfury_send_conf(struct spi_port *port) {
 	int i;
 	for (i = 7; i <= 11; ++i)
-		config_reg(port, i, 0);
-	config_reg(port, 6, 0); /* disable OUTSLK */
-	config_reg(port, 4, 1); /* Enable slow oscillator */
+		bitfury_config_reg(port, i, 0);
+	bitfury_config_reg(port, 6, 0); /* disable OUTSLK */
+	bitfury_config_reg(port, 4, 1); /* Enable slow oscillator */
 	for (i = 1; i <= 3; ++i)
-		config_reg(port, i, 0);
-	spi_emit_data(port, 0x0100, counters, 16); /* Program counters correctly for rounds processing, here baby should start consuming power */
+		bitfury_config_reg(port, i, 0);
+	spi_emit_data(port, 0x0100, bitfury_counters, 16); /* Program counters correctly for rounds processing, here baby should start consuming power */
 }
 
-void send_init(struct spi_port *port) {
+static
+void bitfury_send_init(struct spi_port *port) {
 	/* Prepare internal buffers */
 	/* PREPARE BUFFERS (INITIAL PROGRAMMING) */
 	unsigned w[16];
@@ -130,7 +131,7 @@ void send_init(struct spi_port *port) {
 		0x8a0bb7b7, 0x33af304f, 0x0b290c1a, 0xf0c4e61f, /* WDATA: hashMerleRoot[7], nTime, nBits, nNonce */
 	};
 
-	ms3_compute(&atrvec[0]);
+	libbitfury_ms3_compute(&atrvec[0]);
 	memset(&w, 0, sizeof(w)); w[3] = 0xffffffff; w[4] = 0x80000000; w[15] = 0x00000280;
 	spi_emit_data(port, 0x1000, w, 16*4);
 	spi_emit_data(port, 0x1400, w,  8*4);
@@ -139,47 +140,50 @@ void send_init(struct spi_port *port) {
 	spi_emit_data(port, 0x3000, &atrvec[0], 19*4);
 }
 
-void set_freq(struct spi_port *port, int bits) {
+static
+void bitfury_set_freq(struct spi_port *port, int bits) {
 	uint64_t freq;
 	const uint8_t *
 	osc6 = (unsigned char *)&freq;
 	freq = (1ULL << bits) - 1ULL;
 
 	spi_emit_data(port, 0x6000, osc6, 8); /* Program internal on-die slow oscillator frequency */
-	config_reg(port, 4, 1); /* Enable slow oscillator */
+	bitfury_config_reg(port, 4, 1); /* Enable slow oscillator */
 }
 
-void send_reinit(struct spi_port *port, int slot, int chip_n, int n) {
+void bitfury_send_reinit(struct spi_port *port, int slot, int chip_n, int n) {
 	spi_clear_buf(port);
 	spi_emit_break(port);
 	spi_emit_fasync(port, chip_n);
-	set_freq(port, n);
-	send_conf(port);
-	send_init(port);
+	bitfury_set_freq(port, n);
+	bitfury_send_conf(port);
+	bitfury_send_init(port);
 	spi_txrx(port);
 }
 
-void send_shutdown(struct spi_port *port, int slot, int chip_n) {
+void bitfury_send_shutdown(struct spi_port *port, int slot, int chip_n) {
 	spi_clear_buf(port);
 	spi_emit_break(port);
 	spi_emit_fasync(port, chip_n);
-	config_reg(port, 4, 0); /* Disable slow oscillator */
+	bitfury_config_reg(port, 4, 0); /* Disable slow oscillator */
 	spi_txrx(port);
 }
 
-void send_freq(struct spi_port *port, int slot, int chip_n, int bits) {
+void bitfury_send_freq(struct spi_port *port, int slot, int chip_n, int bits) {
 	spi_clear_buf(port);
 	spi_emit_break(port);
 	spi_emit_fasync(port, chip_n);
-	set_freq(port, bits);
+	bitfury_set_freq(port, bits);
 	spi_txrx(port);
 }
 
-unsigned int c_diff(unsigned ocounter, unsigned counter) {
+static
+unsigned int libbitfury_c_diff(unsigned ocounter, unsigned counter) {
 	return counter >  ocounter ? counter - ocounter : (0x003FFFFF - ocounter) + counter;
 }
 
-int get_counter(unsigned int *newbuf, unsigned int *oldbuf) {
+static
+int libbitfury_get_counter(unsigned int *newbuf, unsigned int *oldbuf) {
 	int j;
 	for(j = 0; j < 16; j++) {
 		if (newbuf[j] != oldbuf[j]) {
@@ -193,18 +197,8 @@ int get_counter(unsigned int *newbuf, unsigned int *oldbuf) {
 	return 0;
 }
 
-int get_diff(unsigned int *newbuf, unsigned int *oldbuf) {
-		int j;
-		unsigned counter = 0;
-		for(j = 0; j < 16; j++) {
-				if (newbuf[j] != oldbuf[j]) {
-						counter++;
-				}
-		}
-		return counter;
-}
-
-int detect_chip(struct spi_port *port, int chip_n) {
+static
+int libbitfury_detect_chip(struct spi_port *port, int chip_n) {
 	/* Test vectors to calculate (using address-translated loads) */
 	unsigned atrvec[] = {
 		0xb0e72d8e, 0x1dc5b862, 0xe9e7c4a6, 0x3050f1f5, 0x8a1a6b7e, 0x7ec384e8, 0x42c1c3fc, 0x8ed158a1, /* MIDSTATE */
@@ -235,17 +229,17 @@ int detect_chip(struct spi_port *port, int chip_n) {
 	memset(newbuf, 0, 17 * 4);
 	memset(oldbuf, 0, 17 * 4);
 
-	ms3_compute(&atrvec[0]);
-	ms3_compute(&atrvec[20]);
-	ms3_compute(&atrvec[40]);
+	libbitfury_ms3_compute(&atrvec[0]);
+	libbitfury_ms3_compute(&atrvec[20]);
+	libbitfury_ms3_compute(&atrvec[40]);
 
 
 	spi_clear_buf(port);
 	spi_emit_break(port); /* First we want to break chain! Otherwise we'll get all of traffic bounced to output */
 	spi_emit_fasync(port, chip_n);
-	set_freq(port, 52);  //54 - 3F, 53 - 1F
-	send_conf(port);
-	send_init(port);
+	bitfury_set_freq(port, 52);  //54 - 3F, 53 - 1F
+	bitfury_send_conf(port);
+	bitfury_send_init(port);
 	spi_txrx(port);
 
 	ocounter = 0;
@@ -259,9 +253,9 @@ int detect_chip(struct spi_port *port, int chip_n) {
 		spi_txrx(port);
 		memcpy(newbuf, spi_getrxbuf(port) + 4 + chip_n, 17*4);
 
-		counter = get_counter(newbuf, oldbuf);
+		counter = libbitfury_get_counter(newbuf, oldbuf);
 		if (ocounter) {
-			unsigned int cdiff = c_diff(ocounter, counter);
+			unsigned int cdiff = libbitfury_c_diff(ocounter, counter);
 
 			if (cdiff > 5000 && cdiff < 100000 && odiff > 5000 && odiff < 100000)
 				return 1;
@@ -279,7 +273,7 @@ int detect_chip(struct spi_port *port, int chip_n) {
 
 int libbitfury_detectChips1(struct spi_port *port) {
 	int n;
-	for (n = 0; detect_chip(port, n); ++n)
+	for (n = 0; libbitfury_detect_chip(port, n); ++n)
 	{}
 	return n;
 }
@@ -308,7 +302,8 @@ unsigned bitfury_decnonce(unsigned in)
 	return out;
 }
 
-int rehash(const void *midstate, const uint32_t m7, const uint32_t ntime, const uint32_t nbits, uint32_t nnonce) {
+static
+int libbitfury_rehash(const void *midstate, const uint32_t m7, const uint32_t ntime, const uint32_t nbits, uint32_t nnonce) {
 	unsigned char in[16];
 	unsigned int *in32 = (unsigned int *)in;
 	unsigned int *mid32 = (unsigned int *)midstate;
@@ -358,7 +353,7 @@ bool bitfury_fudge_nonce(const void *midstate, const uint32_t m7, const uint32_t
 	for (i = 0; i < 6; ++i)
 	{
 		nonce = *nonce_p + offsets[i];
-		if (rehash(midstate, m7, ntime, nbits, nonce))
+		if (libbitfury_rehash(midstate, m7, ntime, nbits, nonce))
 		{
 			*nonce_p = nonce;
 			return true;
@@ -367,7 +362,7 @@ bool bitfury_fudge_nonce(const void *midstate, const uint32_t m7, const uint32_t
 	return false;
 }
 
-void work_to_payload(struct bitfury_payload *p, struct work *w) {
+void work_to_bitfury_payload(struct bitfury_payload *p, struct work *w) {
 	unsigned char flipped_data[80];
 
 	memset(p, 0, sizeof(struct bitfury_payload));
@@ -379,238 +374,9 @@ void work_to_payload(struct bitfury_payload *p, struct work *w) {
 	p->nbits = bswap_32(*(unsigned *)(flipped_data + 72));
 }
 
-void payload_to_atrvec(uint32_t *atrvec, struct bitfury_payload *p)
+void bitfury_payload_to_atrvec(uint32_t *atrvec, struct bitfury_payload *p)
 {
 	/* Programming next value */
 	memcpy(atrvec, p, 20*4);
-	ms3_compute(atrvec);
-}
-
-void libbitfury_sendHashData1(int chip_id, struct bitfury_device *d, struct thr_info *thr)
-{
-	struct spi_port *port = d->spi;
-	unsigned *newbuf = d->newbuf;
-	unsigned *oldbuf = d->oldbuf;
-	struct bitfury_payload *p = &(d->payload);
-	struct bitfury_payload *op = &(d->opayload);
-	struct bitfury_payload *o2p = &(d->o2payload);
-	struct timeval d_time;
-	struct timeval time;
-	int smart = 0;
-	int chip = d->fasync;
-	int buf_diff;
-
-	timer_set_now(&time);
-
-	if (!d->second_run) {
-		d->predict2 = d->predict1 = time;
-		d->counter1 = d->counter2 = 0;
-		d->req2_done = 0;
-	};
-
-	timersub(&time, &d->predict1, &d_time);
-	if (d_time.tv_sec < 0 && (d->req2_done || !smart)) {
-		d->otimer1 = d->timer1;
-		d->timer1 = time;
-		d->ocounter1 = d->counter1;
-		/* Programming next value */
-		spi_clear_buf(port);
-		spi_emit_break(port);
-		spi_emit_fasync(port, chip);
-		spi_emit_data(port, 0x3000, &d->atrvec[0], 19*4);
-		if (smart) {
-			config_reg(port, 3, 0);
-		}
-		timer_set_now(&time);
-		timersub(&time, &d->predict1, &d_time);
-		spi_txrx(port);
-		memcpy(newbuf, spi_getrxbuf(port)+4 + chip, 17*4);
-		d->counter1 = get_counter(newbuf, oldbuf);
-		buf_diff = get_diff(newbuf, oldbuf);
-		if (buf_diff > 4 || (d->counter1 > 0 && d->counter1 < 0x00400000 / 2)) {
-			if (buf_diff > 4) {
-#ifdef BITFURY_SENDHASHDATA_DEBUG
-				applog(LOG_DEBUG, "AAA        chip_id: %d, buf_diff: %d, counter: %08x", chip_id, buf_diff, d->counter1);
-#endif
-				payload_to_atrvec(&d->atrvec[0], p);
-				spi_clear_buf(port);
-				spi_emit_break(port);
-				spi_emit_fasync(port, chip);
-				spi_emit_data(port, 0x3000, &d->atrvec[0], 19*4);
-				timer_set_now(&time);
-				timersub(&time, &d->predict1, &d_time);
-				spi_txrx(port);
-				memcpy(newbuf, spi_getrxbuf(port)+4 + chip, 17*4);
-				buf_diff = get_diff(newbuf, oldbuf);
-				d->counter1 = get_counter(newbuf, oldbuf);
-#ifdef BITFURY_SENDHASHDATA_DEBUG
-				applog(LOG_DEBUG, "AAA _222__ chip_id: %d, buf_diff: %d, counter: %08x", chip_id, buf_diff, d->counter1);
-#endif
-			}
-		}
-
-		d->job_switched = newbuf[16] != oldbuf[16];
-
-		int i;
-		int results_num = 0;
-		int found = 0;
-		unsigned * results = d->results;
-
-		d->old_nonce = 0;
-		d->future_nonce = 0;
-		for (i = 0; i < 16; i++) {
-			if (oldbuf[i] != newbuf[i] && op && o2p) {
-				uint32_t pn;  // possible nonce
-				if ((newbuf[i] & 0xFF) == 0xE0)
-					continue;
-				pn = bitfury_decnonce(newbuf[i]);
-				if (bitfury_fudge_nonce(op->midstate, op->m7, op->ntime, op->nbits, &pn))
-				{
-					int k;
-					int dup = 0;
-					for (k = 0; k < results_num; k++) {
-						if (results[k] == bswap_32(pn))
-							dup = 1;
-					}
-					if (!dup) {
-						results[results_num++] = bswap_32(pn);
-						found++;
-					}
-				}
-				else
-				if (bitfury_fudge_nonce(o2p->midstate, o2p->m7, o2p->ntime, o2p->nbits, &pn))
-				{
-					d->old_nonce = bswap_32(pn);
-					found++;
-				}
-				else
-				if (bitfury_fudge_nonce(p->midstate, p->m7, p->ntime, p->nbits, &pn))
-				{
-					d->future_nonce = bswap_32(pn);
-					found++;
-				}
-				if (!found) {
-					inc_hw_errors2(thr, NULL, &pn);
-					d->strange_counter++;
-				}
-			}
-		}
-		d->results_n = results_num;
-
-		if (smart) {
-			timersub(&d->timer2, &d->timer1, &d_time);
-		} else {
-			timersub(&d->otimer1, &d->timer1, &d_time);
-		}
-		d->counter1 = get_counter(newbuf, oldbuf);
-		if (d->counter2 || !smart) {
-			int shift;
-			int cycles;
-			int req1_cycles;
-			long long unsigned int period;
-			double ns;
-			unsigned full_cycles, half_cycles;
-			double full_delay, half_delay;
-			long long unsigned delta;
-			struct timeval t_delta;
-			double mhz;
-#ifdef BITFURY_SENDHASHDATA_DEBUG
-			int ccase;
-#endif
-
-			shift = 800000;
-			if (smart) {
-				cycles = d->counter1 < d->counter2 ? 0x00400000 - d->counter2 + d->counter1 : d->counter1 - d->counter2; // + 0x003FFFFF;
-			} else {
-				if (d->counter1 > (0x00400000 - shift * 2) && d->ocounter1 > (0x00400000 - shift)) {
-					cycles = 0x00400000 - d->ocounter1 + d->counter1; // + 0x003FFFFF;
-#ifdef BITFURY_SENDHASHDATA_DEBUG
-					ccase = 1;
-#endif
-				} else {
-					cycles = d->counter1 > d->ocounter1 ? d->counter1 - d->ocounter1 : 0x00400000 - d->ocounter1 + d->counter1;
-#ifdef BITFURY_SENDHASHDATA_DEBUG
-					ccase = 2;
-#endif
-				}
-			}
-			req1_cycles = 0x003FFFFF - d->counter1;
-			period = timeval_to_us(&d_time) * 1000ULL;
-			ns = (double)period / (double)(cycles);
-			mhz = 1.0 / ns * 65.0 * 1000.0;
-
-#ifdef BITFURY_SENDHASHDATA_DEBUG
-			if (d->counter1 > 0 && d->counter1 < 0x001FFFFF) {
-				applog(LOG_DEBUG, "//AAA chip_id %2d: %llu ms, req1_cycles: %08u,  counter1: %08d, ocounter1: %08d, counter2: %08d, cycles: %08d, ns: %.2f, mhz: %.2f ", chip_id, period / 1000000ULL, req1_cycles, d->counter1, d->ocounter1, d->counter2, cycles, ns, mhz);
-			}
-#endif
-			if (ns > 2000.0 || ns < 20) {
-#ifdef BITFURY_SENDHASHDATA_DEBUG
-				applog(LOG_DEBUG, "AAA %d!Stupid ns chip_id %2d: %llu ms, req1_cycles: %08u,  counter1: %08d, ocounter1: %08d, counter2: %08d, cycles: %08d, ns: %.2f, mhz: %.2f ", ccase, chip_id, period / 1000000ULL, req1_cycles, d->counter1, d->ocounter1, d->counter2, cycles, ns, mhz);
-#endif
-				ns = 200.0;
-			} else {
-				d->ns = ns;
-				d->mhz = mhz;
-			}
-
-			if (smart) {
-				half_cycles = req1_cycles + shift;
-				full_cycles = 0x003FFFFF - 2 * shift;
-			} else {
-				half_cycles = 0;
-				full_cycles = req1_cycles > shift ? req1_cycles - shift : req1_cycles + 0x00400000 - shift;
-			}
-			half_delay = (double)half_cycles * ns * (1 +0.92);
-			full_delay = (double)full_cycles * ns;
-			delta = (long long unsigned)(full_delay + half_delay);
-			t_delta = TIMEVAL_USECS(delta / 1000ULL);
-			timeradd(&time, &t_delta, &d->predict1);
-
-			if (smart) {
-				half_cycles = req1_cycles + shift;
-				full_cycles = 0;
-			} else {
-				full_cycles = req1_cycles + shift;
-			}
-			half_delay = (double)half_cycles * ns * (1 + 0.92);
-			full_delay = (double)full_cycles * ns;
-			delta = (long long unsigned)(full_delay + half_delay);
-
-			t_delta = TIMEVAL_USECS(delta / 1000ULL);
-			timeradd(&time, &t_delta, &d->predict2);
-			d->req2_done = 0; d->req1_done = 0;
-		}
-
-		if (d->job_switched) {
-			memcpy(o2p, op, sizeof(struct bitfury_payload));
-			memcpy(op, p, sizeof(struct bitfury_payload));
-			memcpy(oldbuf, newbuf, 17 * 4);
-		}
-	}
-
-	timer_set_now(&time);
-	timersub(&time, &d->predict2, &d_time);
-	if (d_time.tv_sec < 0 && !d->req2_done) {
-		if(smart) {
-			d->otimer2 = d->timer2;
-			d->timer2 = time;
-			spi_clear_buf(port);
-			spi_emit_break(port);
-			spi_emit_fasync(port, chip);
-			spi_emit_data(port, 0x3000, &d->atrvec[0], 19*4);
-			if (smart) {
-				config_reg(port, 3, 1);
-			}
-			spi_txrx(port);
-			memcpy(newbuf, spi_getrxbuf(port)+4 + chip, 17*4);
-			d->counter2 = get_counter(newbuf, oldbuf);
-
-			d->req2_done = 1;
-		} else {
-			d->req2_done = 1;
-		}
-	}
-	
-	d->second_run = true;
+	libbitfury_ms3_compute(atrvec);
 }

+ 7 - 33
libbitfury.h

@@ -7,6 +7,8 @@
 #include "miner.h"
 #include "spidevc.h"
 
+struct work;
+
 #define BITFURY_STAT_N 1024
 
 struct bitfury_payload {
@@ -36,55 +38,27 @@ struct bitfury_device {
 	unsigned oldbuf[17];
 	bool oldjob;
 	int active;
-	struct work * work;
-	struct work * owork;
-	struct work * o2work;
-	int job_switched;
 	uint32_t atrvec[20];
 	struct bitfury_payload payload;
-	struct bitfury_payload opayload;
-	struct bitfury_payload o2payload;
 	struct freq_stat chip_stat;
-	unsigned int results[16];
-	int results_n;
-	time_t stat_ts[BITFURY_STAT_N];
-	unsigned int stat_counter;
-	unsigned int future_nonce;
-	unsigned int old_nonce;
 	struct timeval timer1;
-	struct timeval timer2;
-	struct timeval otimer1;
-	struct timeval otimer2;
-	struct timeval predict1;
-	struct timeval predict2;
 	struct timeval tv_stat;
 	unsigned int counter1, counter2;
-	unsigned int ocounter1, ocounter2;
-	int rate; //per msec
-	int osc_slow;
-	int osc_fast;
-	int req1_done, req2_done;
 	double mhz;
-	double ns;
 	unsigned slot;
 	unsigned fasync;
 	unsigned strange_counter;
-	bool second_run;
 	bool force_reinit;
 	int desync_counter;
 	int sample_hwe;
 	int sample_tot;
-	
-	time_t short_out_t;
-	time_t long_out_t;
 };
 
-extern void libbitfury_sendHashData1(int chip_id, struct bitfury_device *, struct thr_info *);
-void work_to_payload(struct bitfury_payload *p, struct work *w);
-extern void payload_to_atrvec(uint32_t *atrvec, struct bitfury_payload *);
-extern void send_reinit(struct spi_port *, int slot, int chip_n, int n);
-extern void send_shutdown(struct spi_port *, int slot, int chip_n);
-extern void send_freq(struct spi_port *, int slot, int chip_n, int bits);
+extern void work_to_bitfury_payload(struct bitfury_payload *, struct work *);
+extern void bitfury_payload_to_atrvec(uint32_t *atrvec, struct bitfury_payload *);
+extern void bitfury_send_reinit(struct spi_port *, int slot, int chip_n, int n);
+extern void bitfury_send_shutdown(struct spi_port *, int slot, int chip_n);
+extern void bitfury_send_freq(struct spi_port *, int slot, int chip_n, int bits);
 extern int libbitfury_detectChips1(struct spi_port *);
 extern unsigned bitfury_decnonce(unsigned);
 extern bool bitfury_fudge_nonce(const void *midstate, const uint32_t m7, const uint32_t ntime, const uint32_t nbits, uint32_t *nonce_p);