/ driver-bflsc.c
driver-bflsc.c
1 /* 2 * Copyright 2013 Andrew Smith 3 * Copyright 2013-2015 Con Kolivas 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License as published by the Free 7 * Software Foundation; either version 3 of the License, or (at your option) 8 * any later version. See COPYING for more details. 9 */ 10 11 #include "config.h" 12 13 #include <float.h> 14 #include <limits.h> 15 #include <pthread.h> 16 #include <stdint.h> 17 #include <stdio.h> 18 #include <string.h> 19 #include <strings.h> 20 #include <sys/time.h> 21 #include <unistd.h> 22 23 #include "compat.h" 24 #include "miner.h" 25 #include "usbutils.h" 26 #include "driver-bflsc.h" 27 28 int opt_bflsc_overheat = BFLSC_TEMP_OVERHEAT; 29 30 static const char *blank = ""; 31 32 static enum driver_version drv_ver(struct cgpu_info *bflsc, const char *ver) 33 { 34 char *tmp; 35 36 if (strstr(ver, "1.0.0")) 37 return BFLSC_DRV1; 38 39 if (strstr(ver, "1.0.") || strstr(ver, "1.1.")) { 40 applog(LOG_WARNING, "%s detect (%s) Warning assuming firmware '%s' is Ver1", 41 bflsc->drv->dname, bflsc->device_path, ver); 42 return BFLSC_DRV1; 43 } 44 45 if (strstr(ver, "1.2.")) 46 return BFLSC_DRV2; 47 48 tmp = str_text((char *)ver); 49 applog(LOG_INFO, "%s detect (%s) Warning unknown firmware '%s' using Ver2", 50 bflsc->drv->dname, bflsc->device_path, tmp); 51 free(tmp); 52 return BFLSC_DRV2; 53 } 54 55 static void xlinkstr(char *xlink, size_t siz, int dev, struct bflsc_info *sc_info) 56 { 57 if (dev > 0) 58 snprintf(xlink, siz, " x-%d", dev); 59 else { 60 if (sc_info->sc_count > 1) 61 strcpy(xlink, " master"); 62 else 63 *xlink = '\0'; 64 } 65 } 66 67 static void bflsc_applog(struct cgpu_info *bflsc, int dev, enum usb_cmds cmd, int amount, int err) 68 { 69 struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data); 70 char xlink[17]; 71 72 xlinkstr(xlink, sizeof(xlink), dev, sc_info); 73 74 usb_applog(bflsc, cmd, xlink, amount, err); 75 } 76 77 // Break an input up into lines with LFs removed 78 // false means an error, but if *lines > 0 then data was also found 79 // error would be no data or missing LF at the end 80 static bool tolines(struct cgpu_info *bflsc, int dev, char *buf, int *lines, char ***items, enum usb_cmds cmd) 81 { 82 bool ok = false; 83 char *ptr; 84 85 #define p_lines (*lines) 86 #define p_items (*items) 87 88 p_lines = 0; 89 p_items = NULL; 90 91 if (!buf || !(*buf)) { 92 applog(LOG_DEBUG, "USB: %s%i: (%d) empty %s", 93 bflsc->drv->name, bflsc->device_id, dev, usb_cmdname(cmd)); 94 return ok; 95 } 96 97 ptr = strdup(buf); 98 while (ptr && *ptr) { 99 p_items = cgrealloc(p_items, ++p_lines * sizeof(*p_items)); 100 p_items[p_lines-1] = ptr; 101 ptr = strchr(ptr, '\n'); 102 if (ptr) 103 *(ptr++) = '\0'; 104 else { 105 applog(LOG_DEBUG, "USB: %s%i: (%d) missing lf(s) in %s", 106 bflsc->drv->name, bflsc->device_id, dev, usb_cmdname(cmd)); 107 return ok; 108 } 109 } 110 ok = true; 111 112 return ok; 113 } 114 115 static void freetolines(int *lines, char ***items) 116 { 117 if (*lines > 0) { 118 free(**items); 119 free(*items); 120 } 121 *lines = 0; 122 *items = NULL; 123 } 124 125 enum breakmode { 126 NOCOLON, 127 ONECOLON, 128 ALLCOLON // Temperature uses this 129 }; 130 131 // Break down a single line into 'fields' 132 // 'lf' will be a pointer to the final LF if it is there (or NULL) 133 // firstname will be the allocated buf copy pointer which is also 134 // the string before ':' for ONECOLON and ALLCOLON 135 // If any string is missing the ':' when it was expected, false is returned 136 static bool breakdown(enum breakmode mode, char *buf, int *count, char **firstname, char ***fields, char **lf) 137 { 138 char *ptr, *colon, *comma; 139 bool ok = false; 140 141 #define p_count (*count) 142 #define p_firstname (*firstname) 143 #define p_fields (*fields) 144 #define p_lf (*lf) 145 146 p_count = 0; 147 p_firstname = NULL; 148 p_fields = NULL; 149 p_lf = NULL; 150 151 if (!buf || !(*buf)) 152 return ok; 153 154 ptr = p_firstname = strdup(buf); 155 p_lf = strchr(p_firstname, '\n'); 156 if (mode == ONECOLON) { 157 colon = strchr(ptr, ':'); 158 if (colon) { 159 ptr = colon; 160 *(ptr++) = '\0'; 161 } else 162 return ok; 163 } 164 165 while (ptr && *ptr) { 166 if (mode == ALLCOLON) { 167 colon = strchr(ptr, ':'); 168 if (colon) 169 ptr = colon + 1; 170 else 171 return ok; 172 } 173 comma = strchr(ptr, ','); 174 if (comma) 175 *(comma++) = '\0'; 176 p_fields = cgrealloc(p_fields, ++p_count * sizeof(*p_fields)); 177 p_fields[p_count-1] = ptr; 178 ptr = comma; 179 } 180 181 ok = true; 182 return ok; 183 } 184 185 static void freebreakdown(int *count, char **firstname, char ***fields) 186 { 187 if (*firstname) 188 free(*firstname); 189 if (*count > 0) 190 free(*fields); 191 *count = 0; 192 *firstname = NULL; 193 *fields = NULL; 194 } 195 196 static bool isokerr(int err, char *buf, int amount) 197 { 198 if (err < 0 || amount < (int)BFLSC_OK_LEN) 199 return false; 200 else { 201 if (strstr(buf, BFLSC_ANERR)) { 202 applog(LOG_INFO, "BFLSC not ok err: %s", buf); 203 return false; 204 } else 205 return true; 206 } 207 } 208 209 // send+receive dual stage - always single line replies 210 static int send_recv_ds(struct cgpu_info *bflsc, int dev, int *stage, bool *sent, int *amount, char *send1, int send1_len, enum usb_cmds send1_cmd, enum usb_cmds recv1_cmd, char *send2, int send2_len, enum usb_cmds send2_cmd, enum usb_cmds recv2_cmd, char *recv, int recv_siz) 211 { 212 struct DataForwardToChain data; 213 int len, err, tried; 214 215 if (dev == 0) { 216 usb_buffer_clear(bflsc); 217 218 *stage = 1; 219 *sent = false; 220 err = usb_write(bflsc, send1, send1_len, amount, send1_cmd); 221 if (err < 0 || *amount < send1_len) 222 return err; 223 224 *sent = true; 225 err = usb_read_nl(bflsc, recv, recv_siz, amount, recv1_cmd); 226 if (!isokerr(err, recv, *amount)) 227 return err; 228 229 usb_buffer_clear(bflsc); 230 231 *stage = 2; 232 *sent = false; 233 err = usb_write(bflsc, send2, send2_len, amount, send2_cmd); 234 if (err < 0 || *amount < send2_len) 235 return err; 236 237 *sent = true; 238 err = usb_read_nl(bflsc, recv, recv_siz, amount, recv2_cmd); 239 240 return err; 241 } 242 243 data.header = BFLSC_XLINKHDR; 244 data.deviceAddress = (uint8_t)dev; 245 tried = 0; 246 while (tried++ < 3) { 247 data.payloadSize = send1_len; 248 memcpy(data.payloadData, send1, send1_len); 249 len = DATAFORWARDSIZE(data); 250 251 usb_buffer_clear(bflsc); 252 253 *stage = 1; 254 *sent = false; 255 err = usb_write(bflsc, (char *)&data, len, amount, send1_cmd); 256 if (err < 0 || *amount < send1_len) 257 return err; 258 259 *sent = true; 260 err = usb_read_nl(bflsc, recv, recv_siz, amount, recv1_cmd); 261 262 if (err != LIBUSB_SUCCESS) 263 return err; 264 265 // x-link timeout? - try again? 266 if (strstr(recv, BFLSC_XTIMEOUT)) 267 continue; 268 269 if (!isokerr(err, recv, *amount)) 270 return err; 271 272 data.payloadSize = send2_len; 273 memcpy(data.payloadData, send2, send2_len); 274 len = DATAFORWARDSIZE(data); 275 276 usb_buffer_clear(bflsc); 277 278 *stage = 2; 279 *sent = false; 280 err = usb_write(bflsc, (char *)&data, len, amount, send2_cmd); 281 if (err < 0 || *amount < send2_len) 282 return err; 283 284 *sent = true; 285 err = usb_read_nl(bflsc, recv, recv_siz, amount, recv2_cmd); 286 287 if (err != LIBUSB_SUCCESS) 288 return err; 289 290 // x-link timeout? - try again? 291 if (strstr(recv, BFLSC_XTIMEOUT)) 292 continue; 293 294 // SUCCESS - return it 295 break; 296 } 297 return err; 298 } 299 300 #define READ_OK true 301 #define READ_NL false 302 303 // send+receive single stage 304 static int send_recv_ss(struct cgpu_info *bflsc, int dev, bool *sent, int *amount, char *send, int send_len, enum usb_cmds send_cmd, char *recv, int recv_siz, enum usb_cmds recv_cmd, bool read_ok) 305 { 306 struct DataForwardToChain data; 307 int len, err, tried; 308 309 if (dev == 0) { 310 usb_buffer_clear(bflsc); 311 312 *sent = false; 313 err = usb_write(bflsc, send, send_len, amount, send_cmd); 314 if (err < 0 || *amount < send_len) { 315 // N.B. thus !(*sent) directly implies err < 0 or *amount < send_len 316 return err; 317 } 318 319 *sent = true; 320 if (read_ok == READ_OK) 321 err = usb_read_ok(bflsc, recv, recv_siz, amount, recv_cmd); 322 else 323 err = usb_read_nl(bflsc, recv, recv_siz, amount, recv_cmd); 324 325 return err; 326 } 327 328 data.header = BFLSC_XLINKHDR; 329 data.deviceAddress = (uint8_t)dev; 330 data.payloadSize = send_len; 331 memcpy(data.payloadData, send, send_len); 332 len = DATAFORWARDSIZE(data); 333 334 tried = 0; 335 while (tried++ < 3) { 336 usb_buffer_clear(bflsc); 337 338 *sent = false; 339 err = usb_write(bflsc, (char *)&data, len, amount, recv_cmd); 340 if (err < 0 || *amount < send_len) 341 return err; 342 343 *sent = true; 344 if (read_ok == READ_OK) 345 err = usb_read_ok(bflsc, recv, recv_siz, amount, recv_cmd); 346 else 347 err = usb_read_nl(bflsc, recv, recv_siz, amount, recv_cmd); 348 349 if (err != LIBUSB_SUCCESS && err != LIBUSB_ERROR_TIMEOUT) 350 return err; 351 352 // read_ok can err timeout if it's looking for OK<LF> 353 // TODO: add a usb_read() option to spot the ERR: and convert end=OK<LF> to just <LF> 354 // x-link timeout? - try again? 355 if ((err == LIBUSB_SUCCESS || (read_ok == READ_OK && err == LIBUSB_ERROR_TIMEOUT)) && 356 strstr(recv, BFLSC_XTIMEOUT)) 357 continue; 358 359 // SUCCESS or TIMEOUT - return it 360 break; 361 } 362 return err; 363 } 364 365 static int write_to_dev(struct cgpu_info *bflsc, int dev, char *buf, int buflen, int *amount, enum usb_cmds cmd) 366 { 367 struct DataForwardToChain data; 368 int len; 369 370 /* 371 * The protocol is syncronous so any previous excess can be 372 * discarded and assumed corrupt data or failed USB transfers 373 */ 374 usb_buffer_clear(bflsc); 375 376 if (dev == 0) 377 return usb_write(bflsc, buf, buflen, amount, cmd); 378 379 data.header = BFLSC_XLINKHDR; 380 data.deviceAddress = (uint8_t)dev; 381 data.payloadSize = buflen; 382 memcpy(data.payloadData, buf, buflen); 383 len = DATAFORWARDSIZE(data); 384 385 return usb_write(bflsc, (char *)&data, len, amount, cmd); 386 } 387 388 static void bflsc_send_flush_work(struct cgpu_info *bflsc, int dev) 389 { 390 char buf[BFLSC_BUFSIZ+1]; 391 int err, amount; 392 bool sent; 393 394 // Device is gone 395 if (bflsc->usbinfo.nodev) 396 return; 397 398 mutex_lock(&bflsc->device_mutex); 399 err = send_recv_ss(bflsc, dev, &sent, &amount, 400 BFLSC_QFLUSH, BFLSC_QFLUSH_LEN, C_QUEFLUSH, 401 buf, sizeof(buf)-1, C_QUEFLUSHREPLY, READ_NL); 402 mutex_unlock(&bflsc->device_mutex); 403 404 if (!sent) 405 bflsc_applog(bflsc, dev, C_QUEFLUSH, amount, err); 406 else { 407 // TODO: do we care if we don't get 'OK'? (always will in normal processing) 408 } 409 } 410 411 /* return True = attempted usb_read_ok() 412 * set ignore to true means no applog/ignore errors */ 413 static bool bflsc_qres(struct cgpu_info *bflsc, char *buf, size_t bufsiz, int dev, int *err, int *amount, bool ignore) 414 { 415 bool readok = false; 416 417 mutex_lock(&(bflsc->device_mutex)); 418 *err = send_recv_ss(bflsc, dev, &readok, amount, 419 BFLSC_QRES, BFLSC_QRES_LEN, C_REQUESTRESULTS, 420 buf, bufsiz-1, C_GETRESULTS, READ_OK); 421 mutex_unlock(&(bflsc->device_mutex)); 422 423 if (!readok) { 424 if (!ignore) 425 bflsc_applog(bflsc, dev, C_REQUESTRESULTS, *amount, *err); 426 427 // TODO: do what? flag as dead device? 428 // count how many times it has happened and reset/fail it 429 // or even make sure it is all x-link and that means device 430 // has failed after some limit of this? 431 // of course all other I/O must also be failing ... 432 } else { 433 if (*err < 0 || *amount < 1) { 434 if (!ignore) 435 bflsc_applog(bflsc, dev, C_GETRESULTS, *amount, *err); 436 437 // TODO: do what? ... see above 438 } 439 } 440 441 return readok; 442 } 443 444 static void __bflsc_initialise(struct cgpu_info *bflsc) 445 { 446 int err, interface; 447 448 // TODO: does x-link bypass the other device FTDI? (I think it does) 449 // So no initialisation required except for the master device? 450 451 if (bflsc->usbinfo.nodev) 452 return; 453 454 interface = usb_interface(bflsc); 455 // Reset 456 err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, 457 FTDI_VALUE_RESET, interface, C_RESET); 458 459 applog(LOG_DEBUG, "%s%i: reset got err %d", 460 bflsc->drv->name, bflsc->device_id, err); 461 462 if (bflsc->usbinfo.nodev) 463 return; 464 465 usb_ftdi_set_latency(bflsc); 466 467 if (bflsc->usbinfo.nodev) 468 return; 469 470 // Set data control 471 err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_DATA, 472 FTDI_VALUE_DATA_BAS, interface, C_SETDATA); 473 474 applog(LOG_DEBUG, "%s%i: setdata got err %d", 475 bflsc->drv->name, bflsc->device_id, err); 476 477 if (bflsc->usbinfo.nodev) 478 return; 479 480 // Set the baud 481 err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD_BAS, 482 (FTDI_INDEX_BAUD_BAS & 0xff00) | interface, 483 C_SETBAUD); 484 485 applog(LOG_DEBUG, "%s%i: setbaud got err %d", 486 bflsc->drv->name, bflsc->device_id, err); 487 488 if (bflsc->usbinfo.nodev) 489 return; 490 491 // Set Flow Control 492 err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW, 493 FTDI_VALUE_FLOW, interface, C_SETFLOW); 494 495 applog(LOG_DEBUG, "%s%i: setflowctrl got err %d", 496 bflsc->drv->name, bflsc->device_id, err); 497 498 if (bflsc->usbinfo.nodev) 499 return; 500 501 // Set Modem Control 502 err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM, 503 FTDI_VALUE_MODEM, interface, C_SETMODEM); 504 505 applog(LOG_DEBUG, "%s%i: setmodemctrl got err %d", 506 bflsc->drv->name, bflsc->device_id, err); 507 508 if (bflsc->usbinfo.nodev) 509 return; 510 511 // Clear any sent data 512 err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, 513 FTDI_VALUE_PURGE_TX, interface, C_PURGETX); 514 515 applog(LOG_DEBUG, "%s%i: purgetx got err %d", 516 bflsc->drv->name, bflsc->device_id, err); 517 518 if (bflsc->usbinfo.nodev) 519 return; 520 521 // Clear any received data 522 err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, 523 FTDI_VALUE_PURGE_RX, interface, C_PURGERX); 524 525 applog(LOG_DEBUG, "%s%i: purgerx got err %d", 526 bflsc->drv->name, bflsc->device_id, err); 527 528 if (!bflsc->cutofftemp) 529 bflsc->cutofftemp = opt_bflsc_overheat; 530 } 531 532 static void bflsc_initialise(struct cgpu_info *bflsc) 533 { 534 struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data); 535 char buf[BFLSC_BUFSIZ+1]; 536 int err, amount; 537 int dev; 538 539 mutex_lock(&(bflsc->device_mutex)); 540 __bflsc_initialise(bflsc); 541 mutex_unlock(&(bflsc->device_mutex)); 542 543 for (dev = 0; dev < sc_info->sc_count; dev++) { 544 bflsc_send_flush_work(bflsc, dev); 545 bflsc_qres(bflsc, buf, sizeof(buf), dev, &err, &amount, true); 546 } 547 } 548 549 static bool getinfo(struct cgpu_info *bflsc, int dev) 550 { 551 struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data); 552 struct bflsc_dev sc_dev; 553 char buf[BFLSC_BUFSIZ+1]; 554 int err, amount; 555 char **items, *firstname, **fields, *lf; 556 bool res, ok = false; 557 int i, lines, count; 558 char *tmp; 559 560 /* 561 * Kano's first dev Jalapeno output: 562 * DEVICE: BitFORCE SC<LF> 563 * FIRMWARE: 1.0.0<LF> 564 * ENGINES: 30<LF> 565 * FREQUENCY: [UNKNOWN]<LF> 566 * XLINK MODE: MASTER<LF> 567 * XLINK PRESENT: YES<LF> 568 * --DEVICES IN CHAIN: 0<LF> 569 * --CHAIN PRESENCE MASK: 00000000<LF> 570 * OK<LF> 571 */ 572 573 /* 574 * Don't use send_recv_ss() since we have a different receive timeout 575 * Also getinfo() is called multiple times if it fails anyway 576 */ 577 err = write_to_dev(bflsc, dev, BFLSC_DETAILS, BFLSC_DETAILS_LEN, &amount, C_REQUESTDETAILS); 578 if (err < 0 || amount != BFLSC_DETAILS_LEN) { 579 applog(LOG_ERR, "%s detect (%s) send details request failed (%d:%d)", 580 bflsc->drv->dname, bflsc->device_path, amount, err); 581 return ok; 582 } 583 584 err = usb_read_ok_timeout(bflsc, buf, sizeof(buf)-1, &amount, 585 BFLSC_INFO_TIMEOUT, C_GETDETAILS); 586 if (err < 0 || amount < 1) { 587 if (err < 0) { 588 applog(LOG_ERR, "%s detect (%s) get details return invalid/timed out (%d:%d)", 589 bflsc->drv->dname, bflsc->device_path, amount, err); 590 } else { 591 applog(LOG_ERR, "%s detect (%s) get details returned nothing (%d:%d)", 592 bflsc->drv->dname, bflsc->device_path, amount, err); 593 } 594 return ok; 595 } 596 597 memset(&sc_dev, 0, sizeof(struct bflsc_dev)); 598 sc_info->sc_count = 1; 599 res = tolines(bflsc, dev, &(buf[0]), &lines, &items, C_GETDETAILS); 600 if (!res) 601 return ok; 602 603 tmp = str_text(buf); 604 strncpy(sc_dev.getinfo, tmp, sizeof(sc_dev.getinfo)); 605 sc_dev.getinfo[sizeof(sc_dev.getinfo)-1] = '\0'; 606 free(tmp); 607 608 for (i = 0; i < lines-2; i++) { 609 res = breakdown(ONECOLON, items[i], &count, &firstname, &fields, &lf); 610 if (lf) 611 *lf = '\0'; 612 if (!res || count != 1) { 613 tmp = str_text(items[i]); 614 applogsiz(LOG_WARNING, BFLSC_APPLOGSIZ, 615 "%s detect (%s) invalid details line: '%s' %d", 616 bflsc->drv->dname, bflsc->device_path, tmp, count); 617 free(tmp); 618 dev_error(bflsc, REASON_DEV_COMMS_ERROR); 619 goto mata; 620 } 621 if (strstr(firstname, BFLSC_DI_FIRMWARE)) { 622 sc_dev.firmware = strdup(fields[0]); 623 sc_info->driver_version = drv_ver(bflsc, sc_dev.firmware); 624 } 625 else if (Strcasestr(firstname, BFLSC_DI_ENGINES)) { 626 sc_dev.engines = atoi(fields[0]); 627 if (sc_dev.engines < 1) { 628 tmp = str_text(items[i]); 629 applogsiz(LOG_WARNING, BFLSC_APPLOGSIZ, 630 "%s detect (%s) invalid engine count: '%s'", 631 bflsc->drv->dname, bflsc->device_path, tmp); 632 free(tmp); 633 goto mata; 634 } 635 } 636 else if (strstr(firstname, BFLSC_DI_XLINKMODE)) 637 sc_dev.xlink_mode = strdup(fields[0]); 638 else if (strstr(firstname, BFLSC_DI_XLINKPRESENT)) 639 sc_dev.xlink_present = strdup(fields[0]); 640 else if (strstr(firstname, BFLSC_DI_DEVICESINCHAIN)) { 641 if (fields[0][0] == '0' || 642 (fields[0][0] == ' ' && fields[0][1] == '0')) 643 sc_info->sc_count = 1; 644 else 645 sc_info->sc_count = atoi(fields[0]); 646 if (sc_info->sc_count < 1 || sc_info->sc_count > 30) { 647 tmp = str_text(items[i]); 648 applogsiz(LOG_WARNING, BFLSC_APPLOGSIZ, 649 "%s detect (%s) invalid x-link count: '%s'", 650 bflsc->drv->dname, bflsc->device_path, tmp); 651 free(tmp); 652 goto mata; 653 } 654 } 655 else if (strstr(firstname, BFLSC_DI_CHIPS)) 656 sc_dev.chips = strdup(fields[0]); 657 else if (strstr(firstname, BFLSC28_DI_ASICS)) 658 sc_dev.chips = strdup(fields[0]); 659 660 freebreakdown(&count, &firstname, &fields); 661 } 662 663 if (sc_info->driver_version == BFLSC_DRVUNDEF) { 664 applog(LOG_WARNING, "%s detect (%s) missing %s", 665 bflsc->drv->dname, bflsc->device_path, BFLSC_DI_FIRMWARE); 666 goto ne; 667 } 668 669 sc_info->sc_devs = cgcalloc(sc_info->sc_count, sizeof(struct bflsc_dev)); 670 memcpy(&(sc_info->sc_devs[0]), &sc_dev, sizeof(sc_dev)); 671 // TODO: do we care about getting this info for the rest if > 0 x-link 672 673 ok = true; 674 goto ne; 675 676 mata: 677 freebreakdown(&count, &firstname, &fields); 678 ok = false; 679 ne: 680 freetolines(&lines, &items); 681 return ok; 682 } 683 684 static bool bflsc28_queue_full(struct cgpu_info *bflsc); 685 686 static struct cgpu_info *bflsc_detect_one(struct libusb_device *dev, struct usb_find_devices *found) 687 { 688 struct bflsc_info *sc_info = NULL; 689 char buf[BFLSC_BUFSIZ+1]; 690 int i, err, amount; 691 struct timeval init_start, init_now; 692 int init_sleep, init_count; 693 bool ident_first, sent; 694 char *newname; 695 uint16_t latency; 696 697 struct cgpu_info *bflsc = usb_alloc_cgpu(&bflsc_drv, 1); 698 699 sc_info = cgcalloc(1, sizeof(*sc_info)); 700 // TODO: fix ... everywhere ... 701 bflsc->device_data = (FILE *)sc_info; 702 703 if (!usb_init(bflsc, dev, found)) 704 goto shin; 705 706 // Allow 2 complete attempts if the 1st time returns an unrecognised reply 707 ident_first = true; 708 retry: 709 init_count = 0; 710 init_sleep = REINIT_TIME_FIRST_MS; 711 cgtime(&init_start); 712 reinit: 713 __bflsc_initialise(bflsc); 714 715 err = send_recv_ss(bflsc, 0, &sent, &amount, 716 BFLSC_IDENTIFY, BFLSC_IDENTIFY_LEN, C_REQUESTIDENTIFY, 717 buf, sizeof(buf)-1, C_GETIDENTIFY, READ_NL); 718 719 if (!sent) { 720 applog(LOG_ERR, "%s detect (%s) send identify request failed (%d:%d)", 721 bflsc->drv->dname, bflsc->device_path, amount, err); 722 goto unshin; 723 } 724 725 if (err < 0 || amount < 1) { 726 init_count++; 727 cgtime(&init_now); 728 if (us_tdiff(&init_now, &init_start) <= REINIT_TIME_MAX) { 729 if (init_count == 2) { 730 applog(LOG_WARNING, "%s detect (%s) 2nd init failed (%d:%d) - retrying", 731 bflsc->drv->dname, bflsc->device_path, amount, err); 732 } 733 cgsleep_ms(init_sleep); 734 if ((init_sleep * 2) <= REINIT_TIME_MAX_MS) 735 init_sleep *= 2; 736 goto reinit; 737 } 738 739 if (init_count > 0) 740 applog(LOG_WARNING, "%s detect (%s) init failed %d times %.2fs", 741 bflsc->drv->dname, bflsc->device_path, init_count, tdiff(&init_now, &init_start)); 742 743 if (err < 0) { 744 applog(LOG_ERR, "%s detect (%s) error identify reply (%d:%d)", 745 bflsc->drv->dname, bflsc->device_path, amount, err); 746 } else { 747 applog(LOG_ERR, "%s detect (%s) empty identify reply (%d)", 748 bflsc->drv->dname, bflsc->device_path, amount); 749 } 750 751 goto unshin; 752 } 753 buf[amount] = '\0'; 754 755 if (unlikely(!strstr(buf, BFLSC_BFLSC) && !strstr(buf, BFLSC_BFLSC28))) { 756 applog(LOG_DEBUG, "%s detect (%s) found an FPGA '%s' ignoring", 757 bflsc->drv->dname, bflsc->device_path, buf); 758 goto unshin; 759 } 760 761 if (unlikely(strstr(buf, BFLSC_IDENTITY))) { 762 if (ident_first) { 763 applog(LOG_DEBUG, "%s detect (%s) didn't recognise '%s' trying again ...", 764 bflsc->drv->dname, bflsc->device_path, buf); 765 ident_first = false; 766 goto retry; 767 } 768 applog(LOG_DEBUG, "%s detect (%s) didn't recognise '%s' on 2nd attempt", 769 bflsc->drv->dname, bflsc->device_path, buf); 770 goto unshin; 771 } 772 773 int tries = 0; 774 while (7734) { 775 if (getinfo(bflsc, 0)) 776 break; 777 778 // N.B. we will get displayed errors each time it fails 779 if (++tries > 2) 780 goto unshin; 781 782 cgsleep_ms(40); 783 } 784 785 switch (sc_info->driver_version) { 786 case BFLSC_DRV1: 787 sc_info->que_size = BFLSC_QUE_SIZE_V1; 788 sc_info->que_full_enough = BFLSC_QUE_FULL_ENOUGH_V1; 789 sc_info->que_watermark = BFLSC_QUE_WATERMARK_V1; 790 sc_info->que_low = BFLSC_QUE_LOW_V1; 791 sc_info->que_noncecount = QUE_NONCECOUNT_V1; 792 sc_info->que_fld_min = QUE_FLD_MIN_V1; 793 sc_info->que_fld_max = QUE_FLD_MAX_V1; 794 // Only Jalapeno uses 1.0.0 795 sc_info->flush_size = 1; 796 break; 797 case BFLSC_DRV2: 798 case BFLSC_DRVUNDEF: 799 default: 800 sc_info->driver_version = BFLSC_DRV2; 801 802 sc_info->que_size = BFLSC_QUE_SIZE_V2; 803 sc_info->que_full_enough = BFLSC_QUE_FULL_ENOUGH_V2; 804 sc_info->que_watermark = BFLSC_QUE_WATERMARK_V2; 805 sc_info->que_low = BFLSC_QUE_LOW_V2; 806 sc_info->que_noncecount = QUE_NONCECOUNT_V2; 807 sc_info->que_fld_min = QUE_FLD_MIN_V2; 808 sc_info->que_fld_max = QUE_FLD_MAX_V2; 809 // TODO: this can be reduced to total chip count 810 sc_info->flush_size = 16 * sc_info->sc_count; 811 break; 812 } 813 814 // Set parallelization based on the getinfo() response if it is present 815 if (sc_info->sc_devs[0].chips && strlen(sc_info->sc_devs[0].chips)) { 816 if (strstr(sc_info->sc_devs[0].chips, BFLSC_DI_CHIPS_PARALLEL)) { 817 sc_info->que_noncecount = QUE_NONCECOUNT_V2; 818 sc_info->que_fld_min = QUE_FLD_MIN_V2; 819 sc_info->que_fld_max = QUE_FLD_MAX_V2; 820 } else { 821 sc_info->que_noncecount = QUE_NONCECOUNT_V1; 822 sc_info->que_fld_min = QUE_FLD_MIN_V1; 823 sc_info->que_fld_max = QUE_FLD_MAX_V1; 824 } 825 } 826 827 sc_info->scan_sleep_time = BAS_SCAN_TIME; 828 sc_info->results_sleep_time = BFLSC_RES_TIME; 829 sc_info->default_ms_work = (unsigned int)BAS_WORK_TIME; 830 latency = BAS_LATENCY; 831 832 /* When getinfo() "FREQUENCY: [UNKNOWN]" is fixed - 833 * use 'freq * engines' to estimate. 834 * Otherwise for now: */ 835 newname = NULL; 836 if (sc_info->sc_count > 1) { 837 newname = BFLSC_MINIRIG; 838 sc_info->scan_sleep_time = BAM_SCAN_TIME; 839 sc_info->default_ms_work = (unsigned int)BAM_WORK_TIME; 840 bflsc->usbdev->ident = IDENT_BAM; 841 latency = BAM_LATENCY; 842 } else { 843 if (sc_info->sc_devs[0].engines < 34) { // 16 * 2 + 2 844 newname = BFLSC_JALAPENO; 845 sc_info->scan_sleep_time = BAJ_SCAN_TIME; 846 sc_info->default_ms_work = (unsigned int)BAJ_WORK_TIME; 847 bflsc->usbdev->ident = IDENT_BAJ; 848 latency = BAJ_LATENCY; 849 } else if (sc_info->sc_devs[0].engines < 130) { // 16 * 8 + 2 850 newname = BFLSC_LITTLESINGLE; 851 sc_info->scan_sleep_time = BAL_SCAN_TIME; 852 sc_info->default_ms_work = (unsigned int)BAL_WORK_TIME; 853 bflsc->usbdev->ident = IDENT_BAL; 854 latency = BAL_LATENCY; 855 } 856 } 857 858 sc_info->ident = usb_ident(bflsc); 859 if (sc_info->ident == IDENT_BMA) { 860 bflsc->drv->queue_full = &bflsc28_queue_full; 861 sc_info->scan_sleep_time = BMA_SCAN_TIME; 862 sc_info->default_ms_work = (unsigned int)BMA_WORK_TIME; 863 sc_info->results_sleep_time = BMA_RES_TIME; 864 } 865 866 if (latency != bflsc->usbdev->found->latency) { 867 bflsc->usbdev->found->latency = latency; 868 usb_ftdi_set_latency(bflsc); 869 } 870 871 for (i = 0; i < sc_info->sc_count; i++) 872 sc_info->sc_devs[i].ms_work = sc_info->default_ms_work; 873 874 if (newname) { 875 if (!bflsc->drv->copy) 876 bflsc->drv = copy_drv(bflsc->drv); 877 bflsc->drv->name = newname; 878 } 879 880 // We have a real BFLSC! 881 applog(LOG_DEBUG, "%s (%s) identified as: '%s'", 882 bflsc->drv->dname, bflsc->device_path, bflsc->drv->name); 883 884 if (!add_cgpu(bflsc)) 885 goto unshin; 886 887 update_usb_stats(bflsc); 888 889 mutex_init(&bflsc->device_mutex); 890 rwlock_init(&sc_info->stat_lock); 891 892 return bflsc; 893 894 unshin: 895 896 usb_uninit(bflsc); 897 898 shin: 899 900 free(bflsc->device_data); 901 bflsc->device_data = NULL; 902 903 if (bflsc->name != blank) { 904 free(bflsc->name); 905 bflsc->name = NULL; 906 } 907 908 bflsc = usb_free_cgpu(bflsc); 909 910 return NULL; 911 } 912 913 static void bflsc_detect(bool __maybe_unused hotplug) 914 { 915 usb_detect(&bflsc_drv, bflsc_detect_one); 916 } 917 918 static void get_bflsc_statline_before(char *buf, size_t bufsiz, struct cgpu_info *bflsc) 919 { 920 struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data); 921 float temp = 0; 922 float vcc2 = 0; 923 int i; 924 925 rd_lock(&(sc_info->stat_lock)); 926 for (i = 0; i < sc_info->sc_count; i++) { 927 if (sc_info->sc_devs[i].temp1 > temp) 928 temp = sc_info->sc_devs[i].temp1; 929 if (sc_info->sc_devs[i].temp2 > temp) 930 temp = sc_info->sc_devs[i].temp2; 931 if (sc_info->sc_devs[i].vcc2 > vcc2) 932 vcc2 = sc_info->sc_devs[i].vcc2; 933 } 934 rd_unlock(&(sc_info->stat_lock)); 935 936 tailsprintf(buf, bufsiz, "max%3.0fC %4.2fV", temp, vcc2); 937 } 938 939 static void flush_one_dev(struct cgpu_info *bflsc, int dev) 940 { 941 struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data); 942 struct work *work, *tmp; 943 bool did = false; 944 945 bflsc_send_flush_work(bflsc, dev); 946 947 rd_lock(&bflsc->qlock); 948 949 HASH_ITER(hh, bflsc->queued_work, work, tmp) { 950 if (work->subid == dev) { 951 // devflag is used to flag stale work 952 work->devflag = true; 953 did = true; 954 } 955 } 956 957 rd_unlock(&bflsc->qlock); 958 959 if (did) { 960 wr_lock(&(sc_info->stat_lock)); 961 sc_info->sc_devs[dev].flushed = true; 962 sc_info->sc_devs[dev].flush_id = sc_info->sc_devs[dev].result_id; 963 sc_info->sc_devs[dev].work_queued = 0; 964 wr_unlock(&(sc_info->stat_lock)); 965 } 966 } 967 968 static void bflsc_flush_work(struct cgpu_info *bflsc) 969 { 970 struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data); 971 int dev; 972 973 for (dev = 0; dev < sc_info->sc_count; dev++) 974 flush_one_dev(bflsc, dev); 975 } 976 977 static void bflsc_set_volt(struct cgpu_info *bflsc, int dev) 978 { 979 struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data); 980 char buf[BFLSC_BUFSIZ+1]; 981 char msg[16]; 982 int err, amount; 983 bool sent; 984 985 // Device is gone 986 if (bflsc->usbinfo.nodev) 987 return; 988 989 snprintf(msg, sizeof(msg), "V%dX", sc_info->volt_next); 990 991 mutex_lock(&bflsc->device_mutex); 992 993 err = send_recv_ss(bflsc, dev, &sent, &amount, 994 msg, strlen(msg), C_SETVOLT, 995 buf, sizeof(buf)-1, C_REPLYSETVOLT, READ_NL); 996 mutex_unlock(&(bflsc->device_mutex)); 997 998 if (!sent) 999 bflsc_applog(bflsc, dev, C_SETVOLT, amount, err); 1000 else { 1001 // Don't care 1002 } 1003 1004 sc_info->volt_next_stat = false; 1005 1006 return; 1007 } 1008 1009 static void bflsc_set_clock(struct cgpu_info *bflsc, int dev) 1010 { 1011 struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data); 1012 char buf[BFLSC_BUFSIZ+1]; 1013 char msg[16]; 1014 int err, amount; 1015 bool sent; 1016 1017 // Device is gone 1018 if (bflsc->usbinfo.nodev) 1019 return; 1020 1021 snprintf(msg, sizeof(msg), "F%XX", sc_info->clock_next); 1022 1023 mutex_lock(&bflsc->device_mutex); 1024 1025 err = send_recv_ss(bflsc, dev, &sent, &amount, 1026 msg, strlen(msg), C_SETCLOCK, 1027 buf, sizeof(buf)-1, C_REPLYSETCLOCK, READ_NL); 1028 mutex_unlock(&(bflsc->device_mutex)); 1029 1030 if (!sent) 1031 bflsc_applog(bflsc, dev, C_SETCLOCK, amount, err); 1032 else { 1033 // Don't care 1034 } 1035 1036 sc_info->clock_next_stat = false; 1037 1038 return; 1039 } 1040 1041 static void bflsc_flash_led(struct cgpu_info *bflsc, int dev) 1042 { 1043 struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data); 1044 char buf[BFLSC_BUFSIZ+1]; 1045 int err, amount; 1046 bool sent; 1047 1048 // Device is gone 1049 if (bflsc->usbinfo.nodev) 1050 return; 1051 1052 // It is not critical flashing the led so don't get stuck if we 1053 // can't grab the mutex now 1054 if (mutex_trylock(&bflsc->device_mutex)) 1055 return; 1056 1057 err = send_recv_ss(bflsc, dev, &sent, &amount, 1058 BFLSC_FLASH, BFLSC_FLASH_LEN, C_REQUESTFLASH, 1059 buf, sizeof(buf)-1, C_FLASHREPLY, READ_NL); 1060 mutex_unlock(&(bflsc->device_mutex)); 1061 1062 if (!sent) 1063 bflsc_applog(bflsc, dev, C_REQUESTFLASH, amount, err); 1064 else { 1065 // Don't care 1066 } 1067 1068 // Once we've tried - don't do it until told to again 1069 // - even if it failed 1070 sc_info->flash_led = false; 1071 1072 return; 1073 } 1074 1075 /* Flush and stop all work if the device reaches the thermal cutoff temp, or 1076 * temporarily stop queueing work if it's in the throttling range. */ 1077 static void bflsc_manage_temp(struct cgpu_info *bflsc, struct bflsc_dev *sc_dev, 1078 int dev, float temp) 1079 { 1080 bflsc->temp = temp; 1081 if (bflsc->cutofftemp > 0) { 1082 int cutoff = bflsc->cutofftemp; 1083 int throttle = cutoff - BFLSC_TEMP_THROTTLE; 1084 int recover = cutoff - BFLSC_TEMP_RECOVER; 1085 1086 if (sc_dev->overheat) { 1087 if (temp < recover) 1088 sc_dev->overheat = false; 1089 } else if (temp > throttle) { 1090 sc_dev->overheat = true; 1091 if (temp > cutoff) { 1092 applog(LOG_WARNING, "%s%i: temp (%.1f) hit thermal cutoff limit %d, stopping work!", 1093 bflsc->drv->name, bflsc->device_id, temp, cutoff); 1094 dev_error(bflsc, REASON_DEV_THERMAL_CUTOFF); 1095 flush_one_dev(bflsc, dev); 1096 1097 } else { 1098 applog(LOG_NOTICE, "%s%i: temp (%.1f) hit thermal throttle limit %d, throttling", 1099 bflsc->drv->name, bflsc->device_id, temp, throttle); 1100 } 1101 } 1102 } 1103 } 1104 1105 static bool bflsc_get_temp(struct cgpu_info *bflsc, int dev) 1106 { 1107 struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data); 1108 struct bflsc_dev *sc_dev; 1109 char temp_buf[BFLSC_BUFSIZ+1]; 1110 char volt_buf[BFLSC_BUFSIZ+1]; 1111 char *tmp; 1112 int err, amount; 1113 char *firstname, **fields, *lf; 1114 char xlink[17]; 1115 int count; 1116 bool res, sent; 1117 float temp, temp1, temp2; 1118 float vcc1, vcc2, vmain; 1119 1120 // Device is gone 1121 if (bflsc->usbinfo.nodev) 1122 return false; 1123 1124 if (dev >= sc_info->sc_count) { 1125 applog(LOG_ERR, "%s%i: temp invalid xlink device %d - limit %d", 1126 bflsc->drv->name, bflsc->device_id, dev, sc_info->sc_count - 1); 1127 return false; 1128 } 1129 1130 if (sc_info->volt_next_stat || sc_info->clock_next_stat) { 1131 if (sc_info->volt_next_stat) 1132 bflsc_set_volt(bflsc, dev); 1133 if (sc_info->clock_next_stat) 1134 bflsc_set_clock(bflsc, dev); 1135 return true; 1136 } 1137 1138 // Flash instead of Temp 1139 if (sc_info->flash_led) { 1140 bflsc_flash_led(bflsc, dev); 1141 return true; 1142 } 1143 1144 xlinkstr(xlink, sizeof(xlink), dev, sc_info); 1145 1146 /* It is not very critical getting temp so don't get stuck if we 1147 * can't grab the mutex here */ 1148 if (mutex_trylock(&bflsc->device_mutex)) 1149 return false; 1150 1151 err = send_recv_ss(bflsc, dev, &sent, &amount, 1152 BFLSC_TEMPERATURE, BFLSC_TEMPERATURE_LEN, C_REQUESTTEMPERATURE, 1153 temp_buf, sizeof(temp_buf)-1, C_GETTEMPERATURE, READ_NL); 1154 mutex_unlock(&(bflsc->device_mutex)); 1155 1156 if (!sent) { 1157 applog(LOG_ERR, "%s%i: Error: Request%s temp invalid/timed out (%d:%d)", 1158 bflsc->drv->name, bflsc->device_id, xlink, amount, err); 1159 return false; 1160 } else { 1161 if (err < 0 || amount < 1) { 1162 if (err < 0) { 1163 applog(LOG_ERR, "%s%i: Error: Get%s temp return invalid/timed out (%d:%d)", 1164 bflsc->drv->name, bflsc->device_id, xlink, amount, err); 1165 } else { 1166 applog(LOG_ERR, "%s%i: Error: Get%s temp returned nothing (%d:%d)", 1167 bflsc->drv->name, bflsc->device_id, xlink, amount, err); 1168 } 1169 return false; 1170 } 1171 } 1172 1173 // Ignore it if we can't get the V 1174 if (mutex_trylock(&bflsc->device_mutex)) 1175 return false; 1176 1177 err = send_recv_ss(bflsc, dev, &sent, &amount, 1178 BFLSC_VOLTAGE, BFLSC_VOLTAGE_LEN, C_REQUESTVOLTS, 1179 volt_buf, sizeof(volt_buf)-1, C_GETVOLTS, READ_NL); 1180 mutex_unlock(&(bflsc->device_mutex)); 1181 1182 if (!sent) { 1183 applog(LOG_ERR, "%s%i: Error: Request%s volts invalid/timed out (%d:%d)", 1184 bflsc->drv->name, bflsc->device_id, xlink, amount, err); 1185 return false; 1186 } else { 1187 if (err < 0 || amount < 1) { 1188 if (err < 0) { 1189 applog(LOG_ERR, "%s%i: Error: Get%s volt return invalid/timed out (%d:%d)", 1190 bflsc->drv->name, bflsc->device_id, xlink, amount, err); 1191 } else { 1192 applog(LOG_ERR, "%s%i: Error: Get%s volt returned nothing (%d:%d)", 1193 bflsc->drv->name, bflsc->device_id, xlink, amount, err); 1194 } 1195 return false; 1196 } 1197 } 1198 1199 res = breakdown(ALLCOLON, temp_buf, &count, &firstname, &fields, &lf); 1200 if (lf) 1201 *lf = '\0'; 1202 if (!res || count < 2 || !lf) { 1203 tmp = str_text(temp_buf); 1204 applog(LOG_WARNING, "%s%i: Invalid%s temp reply: '%s'", 1205 bflsc->drv->name, bflsc->device_id, xlink, tmp); 1206 free(tmp); 1207 freebreakdown(&count, &firstname, &fields); 1208 dev_error(bflsc, REASON_DEV_COMMS_ERROR); 1209 return false; 1210 } 1211 1212 temp = temp1 = (float)atoi(fields[0]); 1213 temp2 = (float)atoi(fields[1]); 1214 1215 freebreakdown(&count, &firstname, &fields); 1216 1217 res = breakdown(NOCOLON, volt_buf, &count, &firstname, &fields, &lf); 1218 if (lf) 1219 *lf = '\0'; 1220 if (!res || count != 3 || !lf) { 1221 tmp = str_text(volt_buf); 1222 applog(LOG_WARNING, "%s%i: Invalid%s volt reply: '%s'", 1223 bflsc->drv->name, bflsc->device_id, xlink, tmp); 1224 free(tmp); 1225 freebreakdown(&count, &firstname, &fields); 1226 dev_error(bflsc, REASON_DEV_COMMS_ERROR); 1227 return false; 1228 } 1229 1230 sc_dev = &sc_info->sc_devs[dev]; 1231 vcc1 = (float)atoi(fields[0]) / 1000.0; 1232 vcc2 = (float)atoi(fields[1]) / 1000.0; 1233 vmain = (float)atoi(fields[2]) / 1000.0; 1234 1235 freebreakdown(&count, &firstname, &fields); 1236 1237 if (vcc1 > 0 || vcc2 > 0 || vmain > 0) { 1238 wr_lock(&(sc_info->stat_lock)); 1239 if (vcc1 > 0) { 1240 if (unlikely(sc_dev->vcc1 == 0)) 1241 sc_dev->vcc1 = vcc1; 1242 else { 1243 sc_dev->vcc1 += vcc1 * 0.63; 1244 sc_dev->vcc1 /= 1.63; 1245 } 1246 } 1247 if (vcc2 > 0) { 1248 if (unlikely(sc_dev->vcc2 == 0)) 1249 sc_dev->vcc2 = vcc2; 1250 else { 1251 sc_dev->vcc2 += vcc2 * 0.63; 1252 sc_dev->vcc2 /= 1.63; 1253 } 1254 } 1255 if (vmain > 0) { 1256 if (unlikely(sc_dev->vmain == 0)) 1257 sc_dev->vmain = vmain; 1258 else { 1259 sc_dev->vmain += vmain * 0.63; 1260 sc_dev->vmain /= 1.63; 1261 } 1262 } 1263 wr_unlock(&(sc_info->stat_lock)); 1264 } 1265 1266 if (temp1 > 0 || temp2 > 0) { 1267 wr_lock(&(sc_info->stat_lock)); 1268 if (unlikely(!sc_dev->temp1)) 1269 sc_dev->temp1 = temp1; 1270 else { 1271 sc_dev->temp1 += temp1 * 0.63; 1272 sc_dev->temp1 /= 1.63; 1273 } 1274 if (unlikely(!sc_dev->temp2)) 1275 sc_dev->temp2 = temp2; 1276 else { 1277 sc_dev->temp2 += temp2 * 0.63; 1278 sc_dev->temp2 /= 1.63; 1279 } 1280 if (temp1 > sc_dev->temp1_max) { 1281 sc_dev->temp1_max = temp1; 1282 sc_dev->temp1_max_time = time(NULL); 1283 } 1284 if (temp2 > sc_dev->temp2_max) { 1285 sc_dev->temp2_max = temp2; 1286 sc_dev->temp2_max_time = time(NULL); 1287 } 1288 1289 if (unlikely(sc_dev->temp1_5min_av == 0)) 1290 sc_dev->temp1_5min_av = temp1; 1291 else { 1292 sc_dev->temp1_5min_av += temp1 * .0042; 1293 sc_dev->temp1_5min_av /= 1.0042; 1294 } 1295 if (unlikely(sc_dev->temp2_5min_av == 0)) 1296 sc_dev->temp2_5min_av = temp2; 1297 else { 1298 sc_dev->temp2_5min_av += temp2 * .0042; 1299 sc_dev->temp2_5min_av /= 1.0042; 1300 } 1301 wr_unlock(&(sc_info->stat_lock)); 1302 1303 if (temp < temp2) 1304 temp = temp2; 1305 1306 bflsc_manage_temp(bflsc, sc_dev, dev, temp); 1307 } 1308 1309 return true; 1310 } 1311 1312 static void inc_core_errors(struct bflsc_info *info, int8_t core) 1313 { 1314 if (info->ident == IDENT_BMA) { 1315 if (core >= 0) 1316 info->cortex_hw[core]++; 1317 } else { 1318 if (core >= 0 && core < 16) 1319 info->core_hw[core]++; 1320 } 1321 } 1322 1323 static void inc_bflsc_errors(struct thr_info *thr, struct bflsc_info *info, int8_t core) 1324 { 1325 inc_hw_errors(thr); 1326 inc_core_errors(info, core); 1327 } 1328 1329 static void inc_bflsc_nonces(struct bflsc_info *info, int8_t core) 1330 { 1331 if (info->ident == IDENT_BMA) { 1332 if (core >= 0) 1333 info->cortex_nonces[core]++; 1334 } else { 1335 if (core >= 0 && core < 16) 1336 info->core_nonces[core]++; 1337 } 1338 } 1339 1340 struct work *bflsc_work_by_uid(struct cgpu_info *bflsc, struct bflsc_info *sc_info, int id) 1341 { 1342 struct bflsc_work *bwork; 1343 struct work *work = NULL; 1344 1345 wr_lock(&bflsc->qlock); 1346 HASH_FIND_INT(sc_info->bworks, &id, bwork); 1347 if (likely(bwork)) { 1348 HASH_DEL(sc_info->bworks, bwork); 1349 work = bwork->work; 1350 free(bwork); 1351 } 1352 wr_unlock(&bflsc->qlock); 1353 1354 return work; 1355 } 1356 1357 static void process_nonces(struct cgpu_info *bflsc, int dev, char *xlink, char *data, int count, char **fields, int *nonces) 1358 { 1359 struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data); 1360 struct thr_info *thr = bflsc->thr[0]; 1361 struct work *work = NULL; 1362 int8_t core = -1; 1363 uint32_t nonce; 1364 int i, num, x; 1365 char *tmp; 1366 bool res; 1367 1368 if (count < sc_info->que_fld_min) { 1369 tmp = str_text(data); 1370 applogsiz(LOG_INFO, BFLSC_APPLOGSIZ, 1371 "%s%i:%s work returned too small (%d,%s)", 1372 bflsc->drv->name, bflsc->device_id, xlink, count, tmp); 1373 free(tmp); 1374 inc_bflsc_errors(thr, sc_info, core); 1375 return; 1376 } 1377 1378 if (sc_info->ident == IDENT_BMA) { 1379 unsigned int ucore; 1380 1381 if (sscanf(fields[QUE_CC], "%x", &ucore) == 1) 1382 core = ucore; 1383 } else if (sc_info->que_noncecount != QUE_NONCECOUNT_V1) { 1384 unsigned int ucore; 1385 1386 if (sscanf(fields[QUE_CHIP_V2], "%x", &ucore) == 1) 1387 core = ucore; 1388 } 1389 1390 if (count > sc_info->que_fld_max) { 1391 applog(LOG_INFO, "%s%i:%s work returned too large (%d) processing %d anyway", 1392 bflsc->drv->name, bflsc->device_id, xlink, count, sc_info->que_fld_max); 1393 count = sc_info->que_fld_max; 1394 inc_bflsc_errors(thr, sc_info, core); 1395 } 1396 1397 num = atoi(fields[sc_info->que_noncecount]); 1398 if (num != count - sc_info->que_fld_min) { 1399 tmp = str_text(data); 1400 applogsiz(LOG_INFO, BFLSC_APPLOGSIZ, 1401 "%s%i:%s incorrect data count (%d) will use %d instead from (%s)", 1402 bflsc->drv->name, bflsc->device_id, xlink, num, 1403 count - sc_info->que_fld_max, tmp); 1404 free(tmp); 1405 inc_bflsc_errors(thr, sc_info, core); 1406 } 1407 1408 if (sc_info->ident == IDENT_BMA) { 1409 int uid; 1410 1411 if (sscanf(fields[QUE_UID], "%04x", &uid) == 1) 1412 work = bflsc_work_by_uid(bflsc, sc_info, uid); 1413 } else { 1414 char midstate[MIDSTATE_BYTES] = {}, blockdata[MERKLE_BYTES] = {}; 1415 1416 if (!hex2bin((unsigned char *)midstate, fields[QUE_MIDSTATE], MIDSTATE_BYTES) || 1417 !hex2bin((unsigned char *)blockdata, fields[QUE_BLOCKDATA], MERKLE_BYTES)) { 1418 applog(LOG_INFO, "%s%i:%s Failed to convert binary data to hex result - ignored", 1419 bflsc->drv->name, bflsc->device_id, xlink); 1420 inc_bflsc_errors(thr, sc_info, core); 1421 return; 1422 } 1423 1424 work = take_queued_work_bymidstate(bflsc, midstate, MIDSTATE_BYTES, 1425 blockdata, MERKLE_OFFSET, MERKLE_BYTES); 1426 } 1427 if (!work) { 1428 if (sc_info->not_first_work) { 1429 applog(LOG_INFO, "%s%i:%s failed to find nonce work - can't be processed - ignored", 1430 bflsc->drv->name, bflsc->device_id, xlink); 1431 inc_bflsc_errors(thr, sc_info, core); 1432 } 1433 return; 1434 } 1435 1436 res = false; 1437 x = 0; 1438 for (i = sc_info->que_fld_min; i < count; i++) { 1439 if (strlen(fields[i]) != 8) { 1440 tmp = str_text(data); 1441 applogsiz(LOG_INFO, BFLSC_APPLOGSIZ, 1442 "%s%i:%s invalid nonce (%s) will try to process anyway", 1443 bflsc->drv->name, bflsc->device_id, xlink, tmp); 1444 free(tmp); 1445 } 1446 1447 hex2bin((void*)&nonce, fields[i], 4); 1448 nonce = htobe32(nonce); 1449 res = submit_nonce(thr, work, nonce); 1450 if (res) { 1451 wr_lock(&(sc_info->stat_lock)); 1452 sc_info->sc_devs[dev].nonces_found++; 1453 wr_unlock(&(sc_info->stat_lock)); 1454 1455 (*nonces)++; 1456 x++; 1457 inc_bflsc_nonces(sc_info, core); 1458 } else 1459 inc_core_errors(sc_info, core); 1460 } 1461 1462 wr_lock(&(sc_info->stat_lock)); 1463 if (res) 1464 sc_info->sc_devs[dev].result_id++; 1465 if (x > QUE_MAX_RESULTS) 1466 x = QUE_MAX_RESULTS + 1; 1467 (sc_info->result_size[x])++; 1468 sc_info->sc_devs[dev].work_complete++; 1469 sc_info->sc_devs[dev].hashes_unsent += FULLNONCE; 1470 // If not flushed (stale) 1471 if (!(work->devflag)) 1472 sc_info->sc_devs[dev].work_queued -= 1; 1473 wr_unlock(&(sc_info->stat_lock)); 1474 1475 free_work(work); 1476 } 1477 1478 static int process_results(struct cgpu_info *bflsc, int dev, char *pbuf, int *nonces, int *in_process) 1479 { 1480 struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data); 1481 char **items, *firstname, **fields, *lf; 1482 int que = 0, i, lines, count; 1483 char *tmp, *tmp2, *buf; 1484 char xlink[17]; 1485 bool res; 1486 1487 *nonces = 0; 1488 *in_process = 0; 1489 1490 xlinkstr(xlink, sizeof(xlink), dev, sc_info); 1491 1492 buf = strdup(pbuf); 1493 if (!strncmp(buf, "INPROCESS", 9)) 1494 sscanf(buf, "INPROCESS:%d\n%s", in_process, pbuf); 1495 res = tolines(bflsc, dev, buf, &lines, &items, C_GETRESULTS); 1496 if (!res || lines < 1) { 1497 tmp = str_text(pbuf); 1498 applogsiz(LOG_ERR, BFLSC_APPLOGSIZ, 1499 "%s%i:%s empty result (%s) ignored", 1500 bflsc->drv->name, bflsc->device_id, xlink, tmp); 1501 free(tmp); 1502 goto arigatou; 1503 } 1504 1505 if (lines < QUE_RES_LINES_MIN) { 1506 tmp = str_text(pbuf); 1507 applogsiz(LOG_ERR, BFLSC_APPLOGSIZ, 1508 "%s%i:%s result of %d too small (%s) ignored", 1509 bflsc->drv->name, bflsc->device_id, xlink, lines, tmp); 1510 free(tmp); 1511 goto arigatou; 1512 } 1513 1514 breakdown(ONECOLON, items[1], &count, &firstname, &fields, &lf); 1515 if (count < 1) { 1516 tmp = str_text(pbuf); 1517 tmp2 = str_text(items[1]); 1518 applogsiz(LOG_ERR, BFLSC_APPLOGSIZ, 1519 "%s%i:%s empty result count (%s) in (%s) ignoring", 1520 bflsc->drv->name, bflsc->device_id, xlink, tmp2, tmp); 1521 free(tmp2); 1522 free(tmp); 1523 goto arigatou; 1524 } else if (count != 1) { 1525 tmp = str_text(pbuf); 1526 tmp2 = str_text(items[1]); 1527 applogsiz(LOG_ERR, BFLSC_APPLOGSIZ, 1528 "%s%i:%s incorrect result count %d (%s) in (%s) will try anyway", 1529 bflsc->drv->name, bflsc->device_id, xlink, count, tmp2, tmp); 1530 free(tmp2); 1531 free(tmp); 1532 } 1533 1534 que = atoi(fields[0]); 1535 if (que != (lines - QUE_RES_LINES_MIN)) { 1536 i = que; 1537 // 1+ In case the last line isn't 'OK' - try to process it 1538 que = 1 + lines - QUE_RES_LINES_MIN; 1539 1540 tmp = str_text(pbuf); 1541 tmp2 = str_text(items[0]); 1542 applogsiz(LOG_ERR, BFLSC_APPLOGSIZ, 1543 "%s%i:%s incorrect result count %d (%s) will try %d (%s)", 1544 bflsc->drv->name, bflsc->device_id, xlink, i, tmp2, que, tmp); 1545 free(tmp2); 1546 free(tmp); 1547 1548 } 1549 1550 freebreakdown(&count, &firstname, &fields); 1551 1552 for (i = 0; i < que; i++) { 1553 res = breakdown(NOCOLON, items[i + QUE_RES_LINES_MIN - 1], &count, &firstname, &fields, &lf); 1554 if (likely(res)) 1555 process_nonces(bflsc, dev, &(xlink[0]), items[i], count, fields, nonces); 1556 else 1557 applogsiz(LOG_ERR, BFLSC_APPLOGSIZ, 1558 "%s%i:%s failed to process nonce %s", 1559 bflsc->drv->name, bflsc->device_id, xlink, items[i]); 1560 freebreakdown(&count, &firstname, &fields); 1561 sc_info->not_first_work = true; 1562 } 1563 1564 arigatou: 1565 freetolines(&lines, &items); 1566 free(buf); 1567 1568 return que; 1569 } 1570 1571 #define TVF(tv) ((float)((tv)->tv_sec) + ((float)((tv)->tv_usec) / 1000000.0)) 1572 #define TVFMS(tv) (TVF(tv) * 1000.0) 1573 1574 // Thread to simply keep looking for results 1575 static void *bflsc_get_results(void *userdata) 1576 { 1577 struct cgpu_info *bflsc = (struct cgpu_info *)userdata; 1578 struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data); 1579 struct timeval elapsed, now; 1580 float oldest, f; 1581 char buf[BFLSC_BUFSIZ+1]; 1582 int err, amount; 1583 int i, que, dev, nonces; 1584 bool readok; 1585 1586 cgtime(&now); 1587 for (i = 0; i < sc_info->sc_count; i++) { 1588 copy_time(&(sc_info->sc_devs[i].last_check_result), &now); 1589 copy_time(&(sc_info->sc_devs[i].last_dev_result), &now); 1590 copy_time(&(sc_info->sc_devs[i].last_nonce_result), &now); 1591 } 1592 1593 while (sc_info->shutdown == false) { 1594 cgtimer_t ts_start; 1595 int in_process; 1596 1597 if (bflsc->usbinfo.nodev) 1598 return NULL; 1599 1600 dev = -1; 1601 oldest = FLT_MAX; 1602 cgtime(&now); 1603 1604 // Find the first oldest ... that also needs checking 1605 for (i = 0; i < sc_info->sc_count; i++) { 1606 timersub(&now, &(sc_info->sc_devs[i].last_check_result), &elapsed); 1607 f = TVFMS(&elapsed); 1608 if (f < oldest && f >= sc_info->sc_devs[i].ms_work) { 1609 f = oldest; 1610 dev = i; 1611 } 1612 } 1613 1614 if (bflsc->usbinfo.nodev) 1615 return NULL; 1616 1617 cgsleep_prepare_r(&ts_start); 1618 if (dev == -1) 1619 goto utsura; 1620 1621 cgtime(&(sc_info->sc_devs[dev].last_check_result)); 1622 1623 readok = bflsc_qres(bflsc, buf, sizeof(buf), dev, &err, &amount, false); 1624 if (err < 0 || (!readok && amount != BFLSC_QRES_LEN) || (readok && amount < 1)) { 1625 // TODO: do what else? 1626 } else { 1627 que = process_results(bflsc, dev, buf, &nonces, &in_process); 1628 sc_info->not_first_work = true; // in case it failed processing it 1629 if (que > 0) 1630 cgtime(&(sc_info->sc_devs[dev].last_dev_result)); 1631 if (nonces > 0) 1632 cgtime(&(sc_info->sc_devs[dev].last_nonce_result)); 1633 1634 /* There are more results queued so do not sleep */ 1635 if (in_process) 1636 continue; 1637 // TODO: if not getting results ... reinit? 1638 } 1639 1640 utsura: 1641 cgsleep_ms_r(&ts_start, sc_info->results_sleep_time); 1642 } 1643 1644 return NULL; 1645 } 1646 1647 static bool bflsc_thread_prepare(struct thr_info *thr) 1648 { 1649 struct cgpu_info *bflsc = thr->cgpu; 1650 struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data); 1651 1652 if (thr_info_create(&(sc_info->results_thr), NULL, bflsc_get_results, (void *)bflsc)) { 1653 applog(LOG_ERR, "%s%i: thread create failed", bflsc->drv->name, bflsc->device_id); 1654 return false; 1655 } 1656 pthread_detach(sc_info->results_thr.pth); 1657 1658 return true; 1659 } 1660 1661 static void bflsc_shutdown(struct thr_info *thr) 1662 { 1663 struct cgpu_info *bflsc = thr->cgpu; 1664 struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data); 1665 1666 bflsc_flush_work(bflsc); 1667 sc_info->shutdown = true; 1668 } 1669 1670 static void bflsc_thread_enable(struct thr_info *thr) 1671 { 1672 struct cgpu_info *bflsc = thr->cgpu; 1673 1674 if (bflsc->usbinfo.nodev) 1675 return; 1676 1677 bflsc_initialise(bflsc); 1678 } 1679 1680 static bool bflsc_send_work(struct cgpu_info *bflsc, int dev, bool mandatory) 1681 { 1682 struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data); 1683 struct FullNonceRangeJob data; 1684 char buf[BFLSC_BUFSIZ+1]; 1685 bool sent, ret = false; 1686 struct work *work; 1687 int err, amount; 1688 int len, try; 1689 int stage; 1690 1691 // Device is gone 1692 if (bflsc->usbinfo.nodev) 1693 return false; 1694 1695 // TODO: handle this everywhere 1696 if (sc_info->sc_devs[dev].overheat == true) 1697 return false; 1698 1699 // Initially code only deals with sending one work item 1700 data.payloadSize = BFLSC_JOBSIZ; 1701 data.endOfBlock = BFLSC_EOB; 1702 1703 len = sizeof(struct FullNonceRangeJob); 1704 1705 /* On faster devices we have a lot of lock contention so only 1706 * mandatorily grab the lock and send work if the queue is empty since 1707 * we have a submit queue. */ 1708 if (mandatory) 1709 mutex_lock(&(bflsc->device_mutex)); 1710 else { 1711 if (mutex_trylock(&bflsc->device_mutex)) 1712 return ret; 1713 } 1714 1715 work = get_queued(bflsc); 1716 if (unlikely(!work)) { 1717 mutex_unlock(&bflsc->device_mutex); 1718 return ret; 1719 } 1720 memcpy(data.midState, work->midstate, MIDSTATE_BYTES); 1721 memcpy(data.blockData, work->data + MERKLE_OFFSET, MERKLE_BYTES); 1722 try = 0; 1723 re_send: 1724 err = send_recv_ds(bflsc, dev, &stage, &sent, &amount, 1725 BFLSC_QJOB, BFLSC_QJOB_LEN, C_REQUESTQUEJOB, C_REQUESTQUEJOBSTATUS, 1726 (char *)&data, len, C_QUEJOB, C_QUEJOBSTATUS, 1727 buf, sizeof(buf)-1); 1728 mutex_unlock(&(bflsc->device_mutex)); 1729 1730 switch (stage) { 1731 case 1: 1732 if (!sent) { 1733 bflsc_applog(bflsc, dev, C_REQUESTQUEJOB, amount, err); 1734 goto out; 1735 } else { 1736 // TODO: handle other errors ... 1737 1738 // Try twice 1739 if (try++ < 1 && amount > 1 && 1740 strstr(buf, BFLSC_TIMEOUT)) 1741 goto re_send; 1742 1743 bflsc_applog(bflsc, dev, C_REQUESTQUEJOBSTATUS, amount, err); 1744 goto out; 1745 } 1746 break; 1747 case 2: 1748 if (!sent) { 1749 bflsc_applog(bflsc, dev, C_QUEJOB, amount, err); 1750 goto out; 1751 } else { 1752 if (!isokerr(err, buf, amount)) { 1753 // TODO: check for QUEUE FULL and set work_queued to sc_info->que_size 1754 // and report a code bug LOG_ERR - coz it should never happen 1755 // TODO: handle other errors ... 1756 1757 // Try twice 1758 if (try++ < 1 && amount > 1 && 1759 strstr(buf, BFLSC_TIMEOUT)) 1760 goto re_send; 1761 1762 bflsc_applog(bflsc, dev, C_QUEJOBSTATUS, amount, err); 1763 goto out; 1764 } 1765 } 1766 break; 1767 } 1768 1769 wr_lock(&(sc_info->stat_lock)); 1770 sc_info->sc_devs[dev].work_queued++; 1771 wr_unlock(&(sc_info->stat_lock)); 1772 1773 work->subid = dev; 1774 ret = true; 1775 out: 1776 if (unlikely(!ret)) 1777 work_completed(bflsc, work); 1778 return ret; 1779 } 1780 1781 #define JP_COMMAND 0 1782 #define JP_STREAMLENGTH 2 1783 #define JP_SIGNATURE 4 1784 #define JP_JOBSINARRY 5 1785 #define JP_JOBSARRY 6 1786 #define JP_ARRAYSIZE 45 1787 1788 static bool bflsc28_queue_full(struct cgpu_info *bflsc) 1789 { 1790 struct bflsc_info *sc_info = bflsc->device_data; 1791 int created, queued = 0, create, i, offset; 1792 struct work *base_work, *work, *works[10]; 1793 char *buf, *field, *ptr; 1794 bool sent, ret = false; 1795 uint16_t *streamlen; 1796 uint8_t *job_pack; 1797 int err, amount; 1798 1799 job_pack = alloca(2 + // Command 1800 2 + // StreamLength 1801 1 + // Signature 1802 1 + // JobsInArray 1803 JP_ARRAYSIZE * 10 +// Array of up to 10 Job Structs 1804 1 // EndOfWrapper 1805 ); 1806 1807 if (bflsc->usbinfo.nodev) 1808 return true; 1809 1810 /* Don't send any work if this device is overheating */ 1811 if (sc_info->sc_devs[0].overheat == true) 1812 return true; 1813 1814 wr_lock(&bflsc->qlock); 1815 base_work = __get_queued(bflsc); 1816 if (likely(base_work)) 1817 __work_completed(bflsc, base_work); 1818 wr_unlock(&bflsc->qlock); 1819 1820 if (unlikely(!base_work)) 1821 return ret; 1822 created = 1; 1823 1824 create = 9; 1825 if (base_work->drv_rolllimit < create) 1826 create = base_work->drv_rolllimit; 1827 1828 works[0] = base_work; 1829 for (i = 1; i <= create ; i++) { 1830 created++; 1831 work = make_clone(base_work); 1832 roll_work(base_work); 1833 works[i] = work; 1834 } 1835 1836 memcpy(job_pack, "WX", 2); 1837 streamlen = (uint16_t *)&job_pack[JP_STREAMLENGTH]; 1838 *streamlen = created * JP_ARRAYSIZE + 7; 1839 job_pack[JP_SIGNATURE] = 0xc1; 1840 job_pack[JP_JOBSINARRY] = created; 1841 offset = JP_JOBSARRY; 1842 1843 /* Create the maximum number of work items we can queue by nrolling one */ 1844 for (i = 0; i < created; i++) { 1845 work = works[i]; 1846 memcpy(job_pack + offset, work->midstate, MIDSTATE_BYTES); 1847 offset += MIDSTATE_BYTES; 1848 memcpy(job_pack + offset, work->data + MERKLE_OFFSET, MERKLE_BYTES); 1849 offset += MERKLE_BYTES; 1850 job_pack[offset] = 0xaa; // EndOfBlock signature 1851 offset++; 1852 } 1853 job_pack[offset++] = 0xfe; // EndOfWrapper 1854 1855 buf = alloca(BFLSC_BUFSIZ + 1); 1856 mutex_lock(&bflsc->device_mutex); 1857 err = send_recv_ss(bflsc, 0, &sent, &amount, (char *)job_pack, offset, 1858 C_REQUESTQUEJOB, buf, BFLSC_BUFSIZ, C_REQUESTQUEJOBSTATUS, READ_NL); 1859 mutex_unlock(&bflsc->device_mutex); 1860 1861 if (!isokerr(err, buf, amount)) { 1862 if (!strncasecmp(buf, "ERR:QUEUE FULL", 14)) { 1863 applog(LOG_DEBUG, "%s%d: Queue full", 1864 bflsc->drv->name, bflsc->device_id); 1865 ret = true; 1866 } else { 1867 applog(LOG_WARNING, "%s%d: Queue response not ok %s", 1868 bflsc->drv->name, bflsc->device_id, buf); 1869 } 1870 goto out; 1871 } 1872 1873 ptr = alloca(strlen(buf)); 1874 if (sscanf(buf, "OK:QUEUED %d:%s", &queued, ptr) != 2) { 1875 applog(LOG_WARNING, "%s%d: Failed to parse queue response %s", 1876 bflsc->drv->name, bflsc->device_id, buf); 1877 goto out; 1878 } 1879 if (queued < 1 || queued > 10) { 1880 applog(LOG_WARNING, "%s%d: Invalid queued count %d", 1881 bflsc->drv->name, bflsc->device_id, queued); 1882 queued = 0; 1883 goto out; 1884 } 1885 for (i = 0; i < queued; i++) { 1886 struct bflsc_work *bwork, *oldbwork; 1887 unsigned int uid; 1888 1889 work = works[i]; 1890 field = Strsep(&ptr, ","); 1891 if (!field) { 1892 applog(LOG_WARNING, "%s%d: Ran out of queued IDs after %d of %d", 1893 bflsc->drv->name, bflsc->device_id, i, queued); 1894 queued = i; 1895 goto out; 1896 } 1897 sscanf(field, "%04x", &uid); 1898 bwork = cgcalloc(sizeof(struct bflsc_work), 1); 1899 bwork->id = uid; 1900 bwork->work = work; 1901 1902 wr_lock(&bflsc->qlock); 1903 HASH_REPLACE_INT(sc_info->bworks, id, bwork, oldbwork); 1904 if (oldbwork) { 1905 free_work(oldbwork->work); 1906 free(oldbwork); 1907 } 1908 wr_unlock(&bflsc->qlock); 1909 sc_info->sc_devs[0].work_queued++; 1910 } 1911 if (queued < created) 1912 ret = true; 1913 out: 1914 for (i = queued; i < created; i++) { 1915 work = works[i]; 1916 discard_work(work); 1917 } 1918 return ret; 1919 } 1920 1921 static bool bflsc_queue_full(struct cgpu_info *bflsc) 1922 { 1923 struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data); 1924 int i, dev, tried, que; 1925 bool ret = false; 1926 int tries = 0; 1927 1928 tried = -1; 1929 // if something is wrong with a device try the next one available 1930 // TODO: try them all? Add an unavailable flag to sc_devs[i] init to 0 here first 1931 while (++tries < 3) { 1932 bool mandatory = false; 1933 1934 // Device is gone - shouldn't normally get here 1935 if (bflsc->usbinfo.nodev) { 1936 ret = true; 1937 break; 1938 } 1939 1940 dev = -1; 1941 rd_lock(&(sc_info->stat_lock)); 1942 // Anything waiting - gets the work first 1943 for (i = 0; i < sc_info->sc_count; i++) { 1944 // TODO: and ignore x-link dead - once I work out how to decide it is dead 1945 if (i != tried && sc_info->sc_devs[i].work_queued == 0 && 1946 !sc_info->sc_devs[i].overheat) { 1947 dev = i; 1948 break; 1949 } 1950 } 1951 1952 if (dev == -1) { 1953 que = sc_info->que_size * 10; // 10x is certainly above the MAX it could be 1954 // The first device with the smallest amount queued 1955 for (i = 0; i < sc_info->sc_count; i++) { 1956 if (i != tried && sc_info->sc_devs[i].work_queued < que && 1957 !sc_info->sc_devs[i].overheat) { 1958 dev = i; 1959 que = sc_info->sc_devs[i].work_queued; 1960 } 1961 } 1962 if (que > sc_info->que_full_enough) 1963 dev = -1; 1964 else if (que < sc_info->que_low) 1965 mandatory = true; 1966 } 1967 rd_unlock(&(sc_info->stat_lock)); 1968 1969 // nothing needs work yet 1970 if (dev == -1) { 1971 ret = true; 1972 break; 1973 } 1974 1975 if (bflsc_send_work(bflsc, dev, mandatory)) 1976 break; 1977 else 1978 tried = dev; 1979 } 1980 1981 return ret; 1982 } 1983 1984 static int64_t bflsc_scanwork(struct thr_info *thr) 1985 { 1986 struct cgpu_info *bflsc = thr->cgpu; 1987 struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data); 1988 int64_t ret, unsent; 1989 bool flushed, cleanup; 1990 struct work *work, *tmp; 1991 int dev, waited, i; 1992 1993 // Device is gone 1994 if (bflsc->usbinfo.nodev) 1995 return -1; 1996 1997 flushed = false; 1998 // Single lock check if any are flagged as flushed 1999 rd_lock(&(sc_info->stat_lock)); 2000 for (dev = 0; dev < sc_info->sc_count; dev++) 2001 flushed |= sc_info->sc_devs[dev].flushed; 2002 rd_unlock(&(sc_info->stat_lock)); 2003 2004 // > 0 flagged as flushed 2005 if (flushed) { 2006 // TODO: something like this ...... 2007 for (dev = 0; dev < sc_info->sc_count; dev++) { 2008 cleanup = false; 2009 2010 // Is there any flushed work that can be removed? 2011 rd_lock(&(sc_info->stat_lock)); 2012 if (sc_info->sc_devs[dev].flushed) { 2013 if (sc_info->sc_devs[dev].result_id > (sc_info->sc_devs[dev].flush_id + sc_info->flush_size)) 2014 cleanup = true; 2015 } 2016 rd_unlock(&(sc_info->stat_lock)); 2017 2018 // yes remove the flushed work that can be removed 2019 if (cleanup) { 2020 wr_lock(&bflsc->qlock); 2021 HASH_ITER(hh, bflsc->queued_work, work, tmp) { 2022 if (work->devflag && work->subid == dev) { 2023 bflsc->queued_count--; 2024 HASH_DEL(bflsc->queued_work, work); 2025 discard_work(work); 2026 } 2027 } 2028 wr_unlock(&bflsc->qlock); 2029 2030 wr_lock(&(sc_info->stat_lock)); 2031 sc_info->sc_devs[dev].flushed = false; 2032 wr_unlock(&(sc_info->stat_lock)); 2033 } 2034 } 2035 } 2036 2037 waited = restart_wait(thr, sc_info->scan_sleep_time); 2038 if (waited == ETIMEDOUT && sc_info->ident != IDENT_BMA) { 2039 unsigned int old_sleep_time, new_sleep_time = 0; 2040 int min_queued = sc_info->que_size; 2041 /* Only adjust the scan_sleep_time if we did not receive a 2042 * restart message while waiting. Try to adjust sleep time 2043 * so we drop to sc_info->que_watermark before getting more work. 2044 */ 2045 2046 rd_lock(&sc_info->stat_lock); 2047 old_sleep_time = sc_info->scan_sleep_time; 2048 for (i = 0; i < sc_info->sc_count; i++) { 2049 if (sc_info->sc_devs[i].work_queued < min_queued) 2050 min_queued = sc_info->sc_devs[i].work_queued; 2051 } 2052 rd_unlock(&sc_info->stat_lock); 2053 new_sleep_time = old_sleep_time; 2054 2055 /* Increase slowly but decrease quickly */ 2056 if (min_queued > sc_info->que_full_enough && old_sleep_time < BFLSC_MAX_SLEEP) 2057 new_sleep_time = old_sleep_time * 21 / 20; 2058 else if (min_queued < sc_info->que_low) 2059 new_sleep_time = old_sleep_time * 2 / 3; 2060 2061 /* Do not sleep more than BFLSC_MAX_SLEEP so we can always 2062 * report in at least 2 results per 5s log interval. */ 2063 if (new_sleep_time != old_sleep_time) { 2064 if (new_sleep_time > BFLSC_MAX_SLEEP) 2065 new_sleep_time = BFLSC_MAX_SLEEP; 2066 else if (new_sleep_time == 0) 2067 new_sleep_time = 1; 2068 applog(LOG_DEBUG, "%s%i: Changed scan sleep time to %d", 2069 bflsc->drv->name, bflsc->device_id, new_sleep_time); 2070 2071 wr_lock(&sc_info->stat_lock); 2072 sc_info->scan_sleep_time = new_sleep_time; 2073 wr_unlock(&sc_info->stat_lock); 2074 } 2075 } 2076 2077 // Count up the work done since we last were here 2078 ret = 0; 2079 wr_lock(&(sc_info->stat_lock)); 2080 for (dev = 0; dev < sc_info->sc_count; dev++) { 2081 unsent = sc_info->sc_devs[dev].hashes_unsent; 2082 sc_info->sc_devs[dev].hashes_unsent = 0; 2083 sc_info->sc_devs[dev].hashes_sent += unsent; 2084 sc_info->hashes_sent += unsent; 2085 ret += unsent; 2086 } 2087 wr_unlock(&(sc_info->stat_lock)); 2088 2089 return ret; 2090 } 2091 2092 #define BFLSC_OVER_TEMP 75 2093 2094 /* Set the fanspeed to auto for any valid value <= BFLSC_OVER_TEMP, 2095 * or max for any value > BFLSC_OVER_TEMP or if we don't know the temperature. */ 2096 static void bflsc_set_fanspeed(struct cgpu_info *bflsc) 2097 { 2098 struct bflsc_info *sc_info = (struct bflsc_info *)bflsc->device_data; 2099 char buf[BFLSC_BUFSIZ+1]; 2100 char data[16+1]; 2101 int amount; 2102 bool sent; 2103 2104 if ((bflsc->temp <= BFLSC_OVER_TEMP && bflsc->temp > 0 && sc_info->fanauto) || 2105 ((bflsc->temp > BFLSC_OVER_TEMP || !bflsc->temp) && !sc_info->fanauto)) 2106 return; 2107 2108 if (bflsc->temp > BFLSC_OVER_TEMP || !bflsc->temp) { 2109 strcpy(data, BFLSC_FAN4); 2110 sc_info->fanauto = false; 2111 } else { 2112 strcpy(data, BFLSC_FANAUTO); 2113 sc_info->fanauto = true; 2114 } 2115 2116 applog(LOG_DEBUG, "%s%i: temp=%.0f over=%d set fan to %s", 2117 bflsc->drv->name, bflsc->device_id, bflsc->temp, 2118 BFLSC_OVER_TEMP, data); 2119 2120 mutex_lock(&bflsc->device_mutex); 2121 send_recv_ss(bflsc, 0, &sent, &amount, 2122 data, strlen(data), C_SETFAN, 2123 buf, sizeof(buf)-1, C_FANREPLY, READ_NL); 2124 mutex_unlock(&bflsc->device_mutex); 2125 } 2126 2127 static bool bflsc_get_stats(struct cgpu_info *bflsc) 2128 { 2129 struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data); 2130 bool allok = true; 2131 int i; 2132 2133 // Device is gone 2134 if (bflsc->usbinfo.nodev) 2135 return false; 2136 2137 for (i = 0; i < sc_info->sc_count; i++) { 2138 if (!bflsc_get_temp(bflsc, i)) 2139 allok = false; 2140 2141 // Device is gone 2142 if (bflsc->usbinfo.nodev) 2143 return false; 2144 2145 if (i < (sc_info->sc_count - 1)) 2146 cgsleep_ms(BFLSC_TEMP_SLEEPMS); 2147 } 2148 2149 bflsc_set_fanspeed(bflsc); 2150 2151 return allok; 2152 } 2153 2154 static char *bflsc_set(struct cgpu_info *bflsc, char *option, char *setting, char *replybuf) 2155 { 2156 struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data); 2157 int val; 2158 2159 if (sc_info->ident != IDENT_BMA) { 2160 strcpy(replybuf, "no set options available"); 2161 return replybuf; 2162 } 2163 2164 if (strcasecmp(option, "help") == 0) { 2165 sprintf(replybuf, "volt: range 0-9 clock: range 0-15"); 2166 return replybuf; 2167 } 2168 2169 if (strcasecmp(option, "volt") == 0) { 2170 if (!setting || !*setting) { 2171 sprintf(replybuf, "missing volt setting"); 2172 return replybuf; 2173 } 2174 2175 val = atoi(setting); 2176 if (val < 0 || val > 9) { 2177 sprintf(replybuf, "invalid volt: '%s' valid range 0-9", 2178 setting); 2179 } 2180 2181 sc_info->volt_next = val; 2182 sc_info->volt_next_stat = true; 2183 2184 return NULL; 2185 } 2186 2187 if (strcasecmp(option, "clock") == 0) { 2188 if (!setting || !*setting) { 2189 sprintf(replybuf, "missing clock setting"); 2190 return replybuf; 2191 } 2192 2193 val = atoi(setting); 2194 if (val < 0 || val > 15) { 2195 sprintf(replybuf, "invalid clock: '%s' valid range 0-15", 2196 setting); 2197 } 2198 2199 sc_info->clock_next = val; 2200 sc_info->clock_next_stat = true; 2201 2202 return NULL; 2203 } 2204 2205 sprintf(replybuf, "Unknown option: %s", option); 2206 return replybuf; 2207 } 2208 2209 static void bflsc_identify(struct cgpu_info *bflsc) 2210 { 2211 struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data); 2212 2213 // TODO: handle x-link 2214 sc_info->flash_led = true; 2215 } 2216 2217 static bool bflsc_thread_init(struct thr_info *thr) 2218 { 2219 struct cgpu_info *bflsc = thr->cgpu; 2220 2221 if (bflsc->usbinfo.nodev) 2222 return false; 2223 2224 bflsc_initialise(bflsc); 2225 2226 return true; 2227 } 2228 2229 // there should be a new API function to return device info that isn't the standard stuff 2230 // instead of bflsc_api_stats - since the stats should really just be internal code info 2231 // and the new one should be UNusual device stats/extra details - like the stuff below 2232 2233 static struct api_data *bflsc_api_stats(struct cgpu_info *bflsc) 2234 { 2235 struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data); 2236 struct api_data *root = NULL; 2237 char data[4096]; 2238 char buf[256]; 2239 int i, j, off; 2240 size_t len; 2241 2242 //if no x-link ... etc 2243 rd_lock(&(sc_info->stat_lock)); 2244 root = api_add_temp(root, "Temp1", &(sc_info->sc_devs[0].temp1), true); 2245 root = api_add_temp(root, "Temp2", &(sc_info->sc_devs[0].temp2), true); 2246 root = api_add_volts(root, "Vcc1", &(sc_info->sc_devs[0].vcc1), true); 2247 root = api_add_volts(root, "Vcc2", &(sc_info->sc_devs[0].vcc2), true); 2248 root = api_add_volts(root, "Vmain", &(sc_info->sc_devs[0].vmain), true); 2249 root = api_add_temp(root, "Temp1 Max", &(sc_info->sc_devs[0].temp1_max), true); 2250 root = api_add_temp(root, "Temp2 Max", &(sc_info->sc_devs[0].temp2_max), true); 2251 root = api_add_time(root, "Temp1 Max Time", &(sc_info->sc_devs[0].temp1_max_time), true); 2252 root = api_add_time(root, "Temp2 Max Time", &(sc_info->sc_devs[0].temp2_max_time), true); 2253 root = api_add_int(root, "Work Queued", &(sc_info->sc_devs[0].work_queued), true); 2254 root = api_add_int(root, "Work Complete", &(sc_info->sc_devs[0].work_complete), true); 2255 root = api_add_bool(root, "Overheat", &(sc_info->sc_devs[0].overheat), true); 2256 root = api_add_uint64(root, "Flush ID", &(sc_info->sc_devs[0].flush_id), true); 2257 root = api_add_uint64(root, "Result ID", &(sc_info->sc_devs[0].result_id), true); 2258 root = api_add_bool(root, "Flushed", &(sc_info->sc_devs[0].flushed), true); 2259 root = api_add_uint(root, "Scan Sleep", &(sc_info->scan_sleep_time), true); 2260 root = api_add_uint(root, "Results Sleep", &(sc_info->results_sleep_time), true); 2261 root = api_add_uint(root, "Work ms", &(sc_info->default_ms_work), true); 2262 2263 buf[0] = '\0'; 2264 for (i = 0; i <= QUE_MAX_RESULTS + 1; i++) 2265 tailsprintf(buf, sizeof(buf), "%s%"PRIu64, (i > 0) ? "/" : "", sc_info->result_size[i]); 2266 root = api_add_string(root, "Result Size", buf, true); 2267 2268 rd_unlock(&(sc_info->stat_lock)); 2269 2270 i = (int)(sc_info->driver_version); 2271 root = api_add_int(root, "Driver", &i, true); 2272 root = api_add_string(root, "Firmware", sc_info->sc_devs[0].firmware, false); 2273 root = api_add_string(root, "Chips", sc_info->sc_devs[0].chips, false); 2274 root = api_add_int(root, "Que Size", &(sc_info->que_size), false); 2275 root = api_add_int(root, "Que Full", &(sc_info->que_full_enough), false); 2276 root = api_add_int(root, "Que Watermark", &(sc_info->que_watermark), false); 2277 root = api_add_int(root, "Que Low", &(sc_info->que_low), false); 2278 root = api_add_escape(root, "GetInfo", sc_info->sc_devs[0].getinfo, false); 2279 2280 /* 2281 else a whole lot of something like these ... etc 2282 root = api_add_temp(root, "X-%d-Temp1", &(sc_info->temp1), false); 2283 root = api_add_temp(root, "X-%d-Temp2", &(sc_info->temp2), false); 2284 root = api_add_volts(root, "X-%d-Vcc1", &(sc_info->vcc1), false); 2285 root = api_add_volts(root, "X-%d-Vcc2", &(sc_info->vcc2), false); 2286 root = api_add_volts(root, "X-%d-Vmain", &(sc_info->vmain), false); 2287 */ 2288 if (sc_info->ident == IDENT_BMA) { 2289 for (i = 0; i < 128; i += 16) { 2290 data[0] = '\0'; 2291 off = 0; 2292 for (j = 0; j < 16; j++) { 2293 len = snprintf(data+off, sizeof(data)-off, 2294 "%s%"PRIu64, 2295 j > 0 ? " " : "", 2296 sc_info->cortex_nonces[i+j]); 2297 if (len >= (sizeof(data)-off)) 2298 off = sizeof(data)-1; 2299 else { 2300 if (len > 0) 2301 off += len; 2302 } 2303 } 2304 sprintf(buf, "Cortex %02x-%02x Nonces", i, i+15); 2305 root = api_add_string(root, buf, data, true); 2306 } 2307 for (i = 0; i < 128; i += 16) { 2308 data[0] = '\0'; 2309 off = 0; 2310 for (j = 0; j < 16; j++) { 2311 len = snprintf(data+off, sizeof(data)-off, 2312 "%s%"PRIu64, 2313 j > 0 ? " " : "", 2314 sc_info->cortex_hw[i+j]); 2315 if (len >= (sizeof(data)-off)) 2316 off = sizeof(data)-1; 2317 else { 2318 if (len > 0) 2319 off += len; 2320 } 2321 } 2322 sprintf(buf, "Cortex %02x-%02x HW Errors", i, i+15); 2323 root = api_add_string(root, buf, data, true); 2324 } 2325 } else if (sc_info->que_noncecount != QUE_NONCECOUNT_V1) { 2326 for (i = 0; i < 16; i++) { 2327 sprintf(buf, "Core%d Nonces", i); 2328 root = api_add_uint64(root, buf, &sc_info->core_nonces[i], false); 2329 } 2330 for (i = 0; i < 16; i++) { 2331 sprintf(buf, "Core%d HW Errors", i); 2332 root = api_add_uint64(root, buf, &sc_info->core_hw[i], false); 2333 } 2334 } 2335 2336 return root; 2337 } 2338 2339 struct device_drv bflsc_drv = { 2340 .drv_id = DRIVER_bflsc, 2341 .dname = "BitForceSC", 2342 .name = BFLSC_SINGLE, 2343 .drv_detect = bflsc_detect, 2344 .get_api_stats = bflsc_api_stats, 2345 .get_statline_before = get_bflsc_statline_before, 2346 .get_stats = bflsc_get_stats, 2347 .set_device = bflsc_set, 2348 .identify_device = bflsc_identify, 2349 .thread_prepare = bflsc_thread_prepare, 2350 .thread_init = bflsc_thread_init, 2351 .hash_work = hash_queued_work, 2352 .scanwork = bflsc_scanwork, 2353 .queue_full = bflsc_queue_full, 2354 .flush_work = bflsc_flush_work, 2355 .thread_shutdown = bflsc_shutdown, 2356 .thread_enable = bflsc_thread_enable 2357 };