Simplify avalon scanhash code using the new find_queued_work_bymidstate function. Partially works only.
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
diff --git a/driver-avalon.c b/driver-avalon.c
index 8af3c4c..0197062 100644
--- a/driver-avalon.c
+++ b/driver-avalon.c
@@ -272,6 +272,7 @@ static int avalon_get_result(int fd, struct avalon_result *ar,
memset(result, 0, AVALON_READ_SIZE);
ret = avalon_gets(fd, result, read_count, thr, tv_finish);
+ memset(ar, 0, sizeof(struct avalon_result));
if (ret == AVA_GETS_OK) {
if (opt_debug) {
applog(LOG_DEBUG, "Avalon: get:");
@@ -283,35 +284,28 @@ static int avalon_get_result(int fd, struct avalon_result *ar,
return ret;
}
-static int avalon_decode_nonce(struct thr_info *thr, struct work **work,
- struct avalon_result *ar, uint32_t *nonce)
+static bool avalon_decode_nonce(struct thr_info *thr, struct avalon_result *ar,
+ uint32_t *nonce)
{
struct cgpu_info *avalon;
struct avalon_info *info;
int avalon_get_work_count, i;
-
- if (unlikely(!work))
- return -1;
+ struct work *work;
avalon = thr->cgpu;
- info = avalon_info[avalon->device_id];
- avalon_get_work_count = info->miner_count;
+ if (unlikely(!avalon->works))
+ return false;
- for (i = 0; i < avalon_get_work_count; i++) {
- if (work[i] &&
- !memcmp(ar->data, work[i]->data + 64, 12) &&
- !memcmp(ar->midstate, work[i]->midstate, 32))
- break;
- }
- if (i == avalon_get_work_count)
- return -1;
+ work = find_queued_work_bymidstate(avalon, ar->midstate, 32, ar->data, 64, 12);
+ if (!work)
+ return false;
+ info = avalon_info[avalon->device_id];
info->matching_work[i]++;
*nonce = htole32(ar->nonce);
+ submit_nonce(thr, work, *nonce);
- applog(LOG_DEBUG, "Avalon: match to work[%d](%p): %d",i, work[i],
- info->matching_work[i]);
- return i;
+ return true;
}
static int avalon_reset(int fd, struct avalon_result *ar)
@@ -661,16 +655,18 @@ static bool avalon_prepare(struct thr_info *thr)
return true;
}
-static void avalon_free_work(struct thr_info *thr, struct work **works)
+static void avalon_free_work(struct thr_info *thr)
{
struct cgpu_info *avalon;
struct avalon_info *info;
+ struct work **works;
int i;
- if (unlikely(!works))
- return;
-
avalon = thr->cgpu;
+ avalon->queued = 0;
+ if (unlikely(!avalon->works))
+ return;
+ works = avalon->works;
info = avalon_info[avalon->device_id];
for (i = 0; i < info->miner_count; i++) {
@@ -687,6 +683,7 @@ static void do_avalon_close(struct thr_info *thr)
struct cgpu_info *avalon = thr->cgpu;
struct avalon_info *info = avalon_info[avalon->device_id];
+ avalon_free_work(thr);
sleep(1);
avalon_reset(avalon->device_fd, &ar);
avalon_idle(avalon);
@@ -694,10 +691,6 @@ static void do_avalon_close(struct thr_info *thr)
avalon->device_fd = -1;
info->no_matching_work = 0;
- avalon_free_work(thr, info->bulk0);
- avalon_free_work(thr, info->bulk1);
- avalon_free_work(thr, info->bulk2);
- avalon_free_work(thr, info->bulk3);
}
static inline void record_temp_fan(struct avalon_info *info, struct avalon_result *ar, float *temp_avg)
@@ -781,12 +774,11 @@ static int64_t avalon_scanhash(struct thr_info *thr)
struct cgpu_info *avalon;
struct work **works;
int fd, ret, full;
- int64_t scanret = 0;
struct avalon_info *info;
struct avalon_task at;
struct avalon_result ar;
- int i, work_i0, work_i1, work_i2, work_i3;
+ int i;
int avalon_get_work_count;
struct timeval tv_start, tv_finish, elapsed;
@@ -806,8 +798,7 @@ static int64_t avalon_scanhash(struct thr_info *thr)
avalon->device_id);
dev_error(avalon, REASON_DEV_COMMS_ERROR);
/* fail the device if the reopen attempt fails */
- scanret = -1;
- goto out;
+ return -1;
}
}
fd = avalon->device_fd;
@@ -815,15 +806,6 @@ static int64_t avalon_scanhash(struct thr_info *thr)
tcflush(fd, TCOFLUSH);
#endif
- for (i = 0; i < avalon_get_work_count; i++) {
- info->bulk0[i] = info->bulk1[i];
- info->bulk1[i] = info->bulk2[i];
- info->bulk2[i] = info->bulk3[i];
- info->bulk3[i] = works[i];
- applog(LOG_DEBUG, "Avalon: bulk0/1/2 buffer [%d]: %p, %p, %p, %p",
- i, info->bulk0[i], info->bulk1[i], info->bulk2[i], info->bulk3[i]);
- }
-
i = 0;
while (true) {
avalon_init_task(&at, 0, 0, info->fan_pwm,
@@ -835,10 +817,6 @@ static int64_t avalon_scanhash(struct thr_info *thr)
(ret == AVA_SEND_BUFFER_EMPTY &&
(i + 1 == avalon_get_work_count) &&
first_try))) {
- avalon_free_work(thr, info->bulk0);
- avalon_free_work(thr, info->bulk1);
- avalon_free_work(thr, info->bulk2);
- avalon_free_work(thr, info->bulk3);
do_avalon_close(thr);
applog(LOG_ERR, "AVA%i: Comms error(buffer)",
avalon->device_id);
@@ -846,12 +824,11 @@ static int64_t avalon_scanhash(struct thr_info *thr)
first_try = 0;
sleep(1);
avalon_init(avalon);
- goto out; /* This should never happen */
+ return 0; /* This should never happen */
}
if (ret == AVA_SEND_BUFFER_EMPTY && (i + 1 == avalon_get_work_count)) {
first_try = 1;
- ret = 0xffffffff;
- goto out;
+ return 0xffffffff;
}
works[i]->blk.nonce = 0xffffffff;
@@ -871,8 +848,6 @@ static int64_t avalon_scanhash(struct thr_info *thr)
result_wrong = 0;
hash_count = 0;
while (true) {
- work_i0 = work_i1 = work_i2 = work_i3 = -1;
-
full = avalon_buffer_full(fd);
applog(LOG_DEBUG, "Avalon: Buffer full: %s",
((full == AVA_BUFFER_FULL) ? "Yes" : "No"));
@@ -881,15 +856,11 @@ static int64_t avalon_scanhash(struct thr_info *thr)
ret = avalon_get_result(fd, &ar, thr, &tv_finish);
if (unlikely(ret == AVA_GETS_ERROR)) {
- avalon_free_work(thr, info->bulk0);
- avalon_free_work(thr, info->bulk1);
- avalon_free_work(thr, info->bulk2);
- avalon_free_work(thr, info->bulk3);
do_avalon_close(thr);
applog(LOG_ERR,
"AVA%i: Comms error(read)", avalon->device_id);
dev_error(avalon, REASON_DEV_COMMS_ERROR);
- goto out;
+ return 0;
}
if (unlikely(ret == AVA_GETS_TIMEOUT)) {
timersub(&tv_finish, &tv_start, &elapsed);
@@ -898,40 +869,22 @@ static int64_t avalon_scanhash(struct thr_info *thr)
continue;
}
if (unlikely(ret == AVA_GETS_RESTART)) {
- avalon_free_work(thr, info->bulk0);
- avalon_free_work(thr, info->bulk1);
- avalon_free_work(thr, info->bulk2);
- avalon_free_work(thr, info->bulk3);
break;
}
result_count++;
- work_i0 = avalon_decode_nonce(thr, info->bulk0, &ar, &nonce);
- if (work_i0 < 0) {
- work_i1 = avalon_decode_nonce(thr, info->bulk1, &ar, &nonce);
- if (work_i1 < 0) {
- work_i2 = avalon_decode_nonce(thr, info->bulk2, &ar, &nonce);
- if (work_i2 < 0) {
- work_i3 = avalon_decode_nonce(thr, info->bulk3, &ar, &nonce);
- if (work_i3 < 0) {
- info->no_matching_work++;
- result_wrong++;
-
- if (opt_debug) {
- timersub(&tv_finish, &tv_start, &elapsed);
- applog(LOG_DEBUG,"Avalon: no matching work: %d"
- " (%ld.%06lds)", info->no_matching_work,
- elapsed.tv_sec, elapsed.tv_usec);
- }
- continue;
- } else
- submit_nonce(thr, info->bulk3[work_i3], nonce);
- } else
- submit_nonce(thr, info->bulk2[work_i2], nonce);
- } else
- submit_nonce(thr, info->bulk1[work_i1], nonce);
- } else
- submit_nonce(thr, info->bulk0[work_i0], nonce);
+ if (!avalon_decode_nonce(thr, &ar, &nonce)) {
+ info->no_matching_work++;
+ result_wrong++;
+
+ if (opt_debug) {
+ timersub(&tv_finish, &tv_start, &elapsed);
+ applog(LOG_DEBUG,"Avalon: no matching work: %d"
+ " (%ld.%06lds)", info->no_matching_work,
+ elapsed.tv_sec, elapsed.tv_usec);
+ }
+ continue;
+ }
hash_count += nonce;
if (opt_debug) {
@@ -945,10 +898,6 @@ static int64_t avalon_scanhash(struct thr_info *thr)
if (result_wrong && result_count == result_wrong) {
/* This mean FPGA controller give all wrong result
* try to reset the Avalon */
- avalon_free_work(thr, info->bulk0);
- avalon_free_work(thr, info->bulk1);
- avalon_free_work(thr, info->bulk2);
- avalon_free_work(thr, info->bulk3);
do_avalon_close(thr);
applog(LOG_ERR,
"AVA%i: FPGA controller mess up", avalon->device_id);
@@ -956,10 +905,10 @@ static int64_t avalon_scanhash(struct thr_info *thr)
do_avalon_close(thr);
sleep(1);
avalon_init(avalon);
- goto out;
+ return 0;
}
- avalon_free_work(thr, info->bulk0);
+ avalon_free_work(thr);
record_temp_fan(info, &ar, &(avalon->temp));
applog(LOG_INFO,
@@ -986,11 +935,7 @@ static int64_t avalon_scanhash(struct thr_info *thr)
*
* Any patch will be great.
*/
- scanret = hash_count * 2;
-out:
- avalon_free_work(thr, avalon->works);
- avalon->queued = 0;
- return scanret;
+ return hash_count * 2;
}
static struct api_data *avalon_api_stats(struct cgpu_info *cgpu)
diff --git a/driver-avalon.h b/driver-avalon.h
index d4fb9d9..f860740 100644
--- a/driver-avalon.h
+++ b/driver-avalon.h
@@ -96,10 +96,6 @@ struct avalon_info {
int no_matching_work;
int matching_work[AVALON_DEFAULT_MINER_NUM];
- struct work *bulk0[AVALON_DEFAULT_MINER_NUM];
- struct work *bulk1[AVALON_DEFAULT_MINER_NUM];
- struct work *bulk2[AVALON_DEFAULT_MINER_NUM];
- struct work *bulk3[AVALON_DEFAULT_MINER_NUM];
int frequency;
};