Rework avalon reset sequence to include idling of chips and waiting for them to go idle followed by 2nd reset and then checking result.
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
diff --git a/driver-avalon.c b/driver-avalon.c
index 1ab2efc..6accf45 100644
--- a/driver-avalon.c
+++ b/driver-avalon.c
@@ -299,68 +299,139 @@ static bool avalon_decode_nonce(struct thr_info *thr, struct avalon_result *ar,
return true;
}
-static void avalon_get_reset(int fd, struct avalon_result *ar)
+static int avalon_write(int fd, char *buf, ssize_t len)
{
- int read_amount = AVALON_READ_SIZE;
- uint8_t result[AVALON_READ_SIZE];
- struct timeval timeout = {1, 0};
- ssize_t ret = 0, offset = 0;
- fd_set rd;
+ ssize_t wrote = 0;
- memset(result, 0, AVALON_READ_SIZE);
- memset(ar, 0, AVALON_READ_SIZE);
- FD_ZERO(&rd);
- FD_SET((SOCKETTYPE)fd, &rd);
- ret = select(fd + 1, &rd, NULL, NULL, &timeout);
- if (unlikely(ret < 0)) {
- applog(LOG_WARNING, "Avalon: Error %d on select in avalon_get_reset", errno);
- return;
+ while (len > 0) {
+ struct timeval timeout;
+ ssize_t ret;
+ fd_set wd;
+
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 1000;
+ FD_ZERO(&wd);
+ FD_SET((SOCKETTYPE)fd, &wd);
+ ret = select(fd + 1, NULL, &wd, NULL, &timeout);
+ if (unlikely(ret < 1)) {
+ applog(LOG_WARNING, "Select error on avalon_write");
+ return AVA_SEND_ERROR;
+ }
+ ret = write(fd, buf + wrote, len);
+ if (unlikely(ret < 1)) {
+ applog(LOG_WARNING, "Write error on avalon_write");
+ return AVA_SEND_ERROR;
+ }
+ wrote += ret;
+ len -= ret;
}
- if (!ret) {
- applog(LOG_WARNING, "Avalon: Timeout on select in avalon_get_reset");
- return;
+
+ return 0;
+}
+
+static int avalon_read(int fd, char *buf, ssize_t len)
+{
+ ssize_t aread = 0;
+
+ while (len > 0) {
+ struct timeval timeout;
+ ssize_t ret;
+ fd_set rd;
+
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 1000;
+ FD_ZERO(&rd);
+ FD_SET((SOCKETTYPE)fd, &rd);
+ ret = select(fd + 1, &rd, NULL, NULL, &timeout);
+ if (unlikely(ret < 1)) {
+ applog(LOG_WARNING, "Select error on avalon_read");
+ return AVA_GETS_ERROR;
+ }
+ ret = read(fd, buf + aread, len);
+ if (unlikely(ret < 1)) {
+ applog(LOG_WARNING, "Read error on avalon_read");
+ return AVA_GETS_ERROR;
+ }
+ aread += ret;
+ len -= ret;
}
+
+ return 0;
+}
+
+/* Non blocking clearing of anything in the buffer */
+static void avalon_clear_readbuf(int fd)
+{
+ ssize_t ret;
+
do {
- ret = read(fd, result + offset, read_amount);
- if (unlikely(ret < 0)) {
- applog(LOG_WARNING, "Avalon: Error %d on read in avalon_get_reset", errno);
- return;
+ struct timeval timeout;
+ char buf[AVALON_FTDI_READSIZE];
+ fd_set rd;
+
+ timeout.tv_sec = timeout.tv_usec = 0;
+ FD_ZERO(&rd);
+ FD_SET((SOCKETTYPE)fd, &rd);
+ ret = select(fd + 1, &rd, NULL, NULL, &timeout);
+ if (ret > 0)
+ ret = read(fd, buf, AVALON_FTDI_READSIZE);
+ } while (ret > 0);
+}
+
+static void avalon_idle(struct cgpu_info *avalon)
+{
+ struct avalon_info *info = avalon->device_data;
+ int i, fd = avalon->device_fd;
+
+ for (i = 0; i < info->miner_count; i++) {
+ struct avalon_task at;
+ int ret;
+
+ if (unlikely(avalon_buffer_full(fd))) {
+ applog(LOG_WARNING, "Avalon buffer full in avalon_idle");
+ break;
}
- read_amount -= ret;
- offset += ret;
- } while (read_amount > 0);
- if (opt_debug) {
- applog(LOG_DEBUG, "Avalon: get:");
- hexdump((uint8_t *)result, AVALON_READ_SIZE);
+ avalon_init_task(&at, 0, 0, info->fan_pwm,
+ info->timeout, info->asic_count,
+ info->miner_count, 1, 1, info->frequency);
+ ret = avalon_write(fd, (char *)&at, AVALON_WRITE_SIZE);
+ if (unlikely(ret == AVA_SEND_ERROR))
+ break;
}
- memcpy((uint8_t *)ar, result, AVALON_READ_SIZE);
+ applog(LOG_ERR, "Avalon: Going to idle mode");
+ sleep(2);
+ avalon_clear_readbuf(fd);
+ applog(LOG_ERR, "Avalon: Idle");
}
-static int avalon_reset(int fd, struct avalon_result *ar)
+static int avalon_reset(struct cgpu_info *avalon, int fd)
{
- struct avalon_task at;
+ struct avalon_result ar;
uint8_t *buf;
int ret, i = 0;
struct timespec p;
- avalon_init_task(&at, 1, 0,
- AVALON_DEFAULT_FAN_MAX_PWM,
- AVALON_DEFAULT_TIMEOUT,
- AVALON_DEFAULT_ASIC_NUM,
- AVALON_DEFAULT_MINER_NUM,
- 0, 0,
- AVALON_DEFAULT_FREQUENCY);
- ret = avalon_send_task(fd, &at, NULL);
- if (ret == AVA_SEND_ERROR)
- return 1;
-
- avalon_get_reset(fd, ar);
-
- buf = (uint8_t *)ar;
- /* Sometimes there is one extra 0 byte for some reason in the buffer,
- * so work around it. */
- if (buf[0] == 0)
- buf = (uint8_t *)(ar + 1);
+ /* Reset once, then send command to go idle */
+ ret = avalon_write(fd, "ad", 2);
+ if (unlikely(ret == AVA_SEND_ERROR))
+ return -1;
+ p.tv_sec = 0;
+ p.tv_nsec = AVALON_RESET_PITCH;
+ nanosleep(&p, NULL);
+ avalon_clear_readbuf(fd);
+ avalon_idle(avalon);
+ /* Reset again, then check result */
+ ret = avalon_write(fd, "ad", 2);
+ if (unlikely(ret == AVA_SEND_ERROR))
+ return -1;
+
+ ret = avalon_read(fd, (char *)&ar, AVALON_READ_SIZE);
+ if (unlikely(ret == AVA_GETS_ERROR))
+ return -1;
+
+ nanosleep(&p, NULL);
+
+ buf = (uint8_t *)&ar;
if (buf[0] == 0xAA && buf[1] == 0x55 &&
buf[2] == 0xAA && buf[3] == 0x55) {
for (i = 4; i < 11; i++)
@@ -368,10 +439,6 @@ static int avalon_reset(int fd, struct avalon_result *ar)
break;
}
- p.tv_sec = 0;
- p.tv_nsec = AVALON_RESET_PITCH;
- nanosleep(&p, NULL);
-
if (i != 11) {
applog(LOG_ERR, "Avalon: Reset failed! not an Avalon?"
" (%d: %02x %02x %02x %02x)",
@@ -382,38 +449,6 @@ static int avalon_reset(int fd, struct avalon_result *ar)
return 0;
}
-static void avalon_idle(struct cgpu_info *avalon)
-{
- int i, ret;
- struct avalon_task at;
-
- int fd = avalon->device_fd;
- struct avalon_info *info = avalon->device_data;
- int avalon_get_work_count = info->miner_count;
-
- i = 0;
- while (true) {
- avalon_init_task(&at, 0, 0, info->fan_pwm,
- info->timeout, info->asic_count,
- info->miner_count, 1, 1, info->frequency);
- ret = avalon_send_task(fd, &at, avalon);
- if (unlikely(ret == AVA_SEND_ERROR ||
- (ret == AVA_SEND_BUFFER_EMPTY &&
- (i + 1 == avalon_get_work_count * 2)))) {
- applog(LOG_ERR, "AVA%i: Comms error", avalon->device_id);
- return;
- }
- if (i + 1 == avalon_get_work_count * 2)
- break;
-
- if (ret == AVA_SEND_BUFFER_FULL)
- break;
-
- i++;
- }
- applog(LOG_ERR, "Avalon: Goto idle mode");
-}
-
static void get_options(int this_option_offset, int *baud, int *miner_count,
int *asic_count, int *timeout, int *frequency)
{
@@ -550,29 +585,9 @@ static void get_options(int this_option_offset, int *baud, int *miner_count,
}
}
-/* Non blocking clearing of anything in the buffer */
-static void avalon_clear_readbuf(int fd)
-{
- ssize_t ret;
-
- do {
- struct timeval timeout;
- char buf[AVALON_FTDI_READSIZE];
- fd_set rd;
-
- timeout.tv_sec = timeout.tv_usec = 0;
- FD_ZERO(&rd);
- FD_SET((SOCKETTYPE)fd, &rd);
- ret = select(fd + 1, &rd, NULL, NULL, &timeout);
- if (ret > 0)
- ret = read(fd, buf, AVALON_FTDI_READSIZE);
- } while (ret > 0);
-}
-
static bool avalon_detect_one(const char *devpath)
{
struct avalon_info *info;
- struct avalon_result ar;
int fd, ret;
int baud, miner_count, asic_count, timeout, frequency = 0;
struct cgpu_info *avalon;
@@ -600,7 +615,7 @@ static bool avalon_detect_one(const char *devpath)
avalon->threads = AVALON_MINER_THREADS;
add_cgpu(avalon);
- ret = avalon_reset(fd, &ar);
+ ret = avalon_reset(avalon, fd);
if (ret) {
; /* FIXME: I think IT IS avalon and wait on reset;
* avalon_close(fd);
@@ -640,8 +655,6 @@ static bool avalon_detect_one(const char *devpath)
info->temp_old = 0;
info->frequency = frequency;
- /* Set asic to idle mode after detect */
- avalon_idle(avalon);
avalon->device_fd = -1;
avalon_close(fd);
@@ -661,7 +674,6 @@ static void __avalon_init(struct cgpu_info *avalon)
static void avalon_init(struct cgpu_info *avalon)
{
struct avalon_info *info = avalon->device_data;
- struct avalon_result ar;
int fd, ret;
avalon->device_fd = -1;
@@ -672,7 +684,7 @@ static void avalon_init(struct cgpu_info *avalon)
return;
}
- ret = avalon_reset(fd, &ar);
+ ret = avalon_reset(avalon, fd);
if (ret) {
avalon_close(fd);
return;
@@ -727,14 +739,12 @@ static void avalon_free_work(struct thr_info *thr)
static void do_avalon_close(struct thr_info *thr)
{
- struct avalon_result ar;
struct cgpu_info *avalon = thr->cgpu;
struct avalon_info *info = avalon->device_data;
avalon_free_work(thr);
sleep(1);
- avalon_reset(avalon->device_fd, &ar);
- avalon_idle(avalon);
+ avalon_reset(avalon, avalon->device_fd);
avalon_close(avalon->device_fd);
avalon->device_fd = -1;