/ driver-hashfast.c
driver-hashfast.c
1 /* 2 * Copyright 2013-2014 Con Kolivas <kernel@kolivas.org> 3 * Copyright 2013 Hashfast Inc. 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 <stdbool.h> 14 #include <math.h> 15 16 #include "miner.h" 17 #include "usbutils.h" 18 19 #include "driver-hashfast.h" 20 21 int opt_hfa_ntime_roll = 1; 22 int opt_hfa_hash_clock = HFA_CLOCK_DEFAULT; 23 int opt_hfa_overheat = HFA_TEMP_OVERHEAT; 24 int opt_hfa_target = HFA_TEMP_TARGET; 25 bool opt_hfa_pll_bypass; 26 bool opt_hfa_dfu_boot; 27 int opt_hfa_fan_default = HFA_FAN_DEFAULT; 28 int opt_hfa_fan_max = HFA_FAN_MAX; 29 int opt_hfa_fan_min = HFA_FAN_MIN; 30 int opt_hfa_fail_drop = 10; 31 bool opt_hfa_noshed; 32 33 char *opt_hfa_name; 34 char *opt_hfa_options; 35 36 //////////////////////////////////////////////////////////////////////////////// 37 // Support for the CRC's used in header (CRC-8) and packet body (CRC-32) 38 //////////////////////////////////////////////////////////////////////////////// 39 40 #define GP8 0x107 /* x^8 + x^2 + x + 1 */ 41 #define DI8 0x07 42 43 static bool hfa_crc8_set; 44 45 char *set_hfa_fan(char *arg) 46 { 47 int val1, val2, ret; 48 49 ret = sscanf(arg, "%d-%d", &val1, &val2); 50 if (ret < 1) 51 return "No values passed to hfa-fan"; 52 if (ret == 1) 53 val2 = val1; 54 55 if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1) 56 return "Invalid value passed to hfa-fan"; 57 58 opt_hfa_fan_min = val1; 59 opt_hfa_fan_max = val2; 60 if (opt_hfa_fan_min > opt_hfa_fan_default) 61 opt_hfa_fan_default = opt_hfa_fan_min; 62 if (opt_hfa_fan_max < opt_hfa_fan_default) 63 opt_hfa_fan_default = opt_hfa_fan_max; 64 65 return NULL; 66 } 67 68 static unsigned char crc8_table[256]; /* CRC-8 table */ 69 70 static void hfa_init_crc8(void) 71 { 72 int i,j; 73 unsigned char crc; 74 75 hfa_crc8_set = true; 76 for (i = 0; i < 256; i++) { 77 crc = i; 78 for (j = 0; j < 8; j++) 79 crc = (crc << 1) ^ ((crc & 0x80) ? DI8 : 0); 80 crc8_table[i] = crc & 0xFF; 81 } 82 } 83 84 static unsigned char hfa_crc8(unsigned char *h) 85 { 86 int i; 87 unsigned char crc; 88 89 h++; // Preamble not included 90 for (i = 1, crc = 0xff; i < 7; i++) 91 crc = crc8_table[crc ^ *h++]; 92 93 return crc; 94 } 95 96 struct hfa_cmd { 97 uint8_t cmd; 98 char *cmd_name; 99 enum usb_cmds usb_cmd; 100 }; 101 102 /* Entries in this array need to align with the actual op values specified 103 * in hf_protocol.h */ 104 #define C_NULL C_MAX 105 static const struct hfa_cmd hfa_cmds[] = { 106 {OP_NULL, "OP_NULL", C_NULL}, // 0 107 {OP_ROOT, "OP_ROOT", C_NULL}, 108 {OP_RESET, "OP_RESET", C_HF_RESET}, 109 {OP_PLL_CONFIG, "OP_PLL_CONFIG", C_HF_PLL_CONFIG}, 110 {OP_ADDRESS, "OP_ADDRESS", C_HF_ADDRESS}, 111 {OP_READDRESS, "OP_READDRESS", C_NULL}, 112 {OP_HIGHEST, "OP_HIGHEST", C_NULL}, 113 {OP_BAUD, "OP_BAUD", C_HF_BAUD}, 114 {OP_UNROOT, "OP_UNROOT", C_NULL}, // 8 115 {OP_HASH, "OP_HASH", C_HF_HASH}, 116 {OP_NONCE, "OP_NONCE", C_HF_NONCE}, 117 {OP_ABORT, "OP_ABORT", C_HF_ABORT}, 118 {OP_STATUS, "OP_STATUS", C_HF_STATUS}, 119 {OP_GPIO, "OP_GPIO", C_NULL}, 120 {OP_CONFIG, "OP_CONFIG", C_HF_CONFIG}, 121 {OP_STATISTICS, "OP_STATISTICS", C_HF_STATISTICS}, 122 {OP_GROUP, "OP_GROUP", C_NULL}, // 16 123 {OP_CLOCKGATE, "OP_CLOCKGATE", C_HF_CLOCKGATE}, 124 125 {OP_USB_INIT, "OP_USB_INIT", C_HF_USB_INIT}, // 18 126 {OP_GET_TRACE, "OP_GET_TRACE", C_NULL}, 127 {OP_LOOPBACK_USB, "OP_LOOPBACK_USB", C_NULL}, 128 {OP_LOOPBACK_UART, "OP_LOOPBACK_UART", C_NULL}, 129 {OP_DFU, "OP_DFU", C_HF_DFU}, 130 {OP_USB_SHUTDOWN, "OP_USB_SHUTDOWN", C_NULL}, 131 {OP_DIE_STATUS, "OP_DIE_STATUS", C_HF_DIE_STATUS}, // 24 132 {OP_GWQ_STATUS, "OP_GWQ_STATUS", C_HF_GWQ_STATUS}, 133 {OP_WORK_RESTART, "OP_WORK_RESTART", C_HF_WORK_RESTART}, 134 {OP_USB_STATS1, "OP_USB_STATS1", C_NULL}, 135 {OP_USB_GWQSTATS, "OP_USB_GWQSTATS", C_HF_GWQSTATS}, 136 {OP_USB_NOTICE, "OP_USB_NOTICE", C_HF_NOTICE}, 137 {OP_PING, "OP_PING", C_HF_PING}, 138 {OP_CORE_MAP, "OP_CORE_MAP", C_NULL}, 139 {OP_VERSION, "OP_VERSION", C_NULL}, // 32 140 {OP_FAN, "OP_FAN", C_HF_FAN}, 141 {OP_NAME, "OP_NAME", C_OP_NAME} 142 }; 143 144 #define HF_USB_CMD_OFFSET (128 - 18) 145 #define HF_USB_CMD(X) (X - HF_USB_CMD_OFFSET) 146 147 /* Send an arbitrary frame, consisting of an 8 byte header and an optional 148 * packet body. */ 149 static bool __hfa_send_frame(struct cgpu_info *hashfast, uint8_t opcode, int tx_length, 150 uint8_t *packet) 151 { 152 struct hashfast_info *info = hashfast->device_data; 153 int ret, amount; 154 bool retried = false; 155 156 if (unlikely(hashfast->usbinfo.nodev)) 157 return false; 158 159 info->last_send = time(NULL); 160 applog(LOG_DEBUG, "%s %s: Sending %s frame", hashfast->drv->name, hashfast->unique_id, hfa_cmds[opcode].cmd_name); 161 retry: 162 ret = usb_write(hashfast, (char *)packet, tx_length, &amount, 163 hfa_cmds[opcode].usb_cmd); 164 if (unlikely(ret < 0 || amount != tx_length)) { 165 if (hashfast->usbinfo.nodev) 166 return false; 167 if (!retried) { 168 applog(LOG_ERR, "%s %s: hfa_send_frame: USB Send error, ret %d amount %d vs. tx_length %d, retrying", 169 hashfast->drv->name, hashfast->unique_id, ret, amount, tx_length); 170 retried = true; 171 goto retry; 172 } 173 applog(LOG_ERR, "%s %s: hfa_send_frame: USB Send error, ret %d amount %d vs. tx_length %d", 174 hashfast->drv->name, hashfast->unique_id, ret, amount, tx_length); 175 return false; 176 } 177 178 if (retried) 179 applog(LOG_WARNING, "%s %s: hfa_send_frame: recovered OK", hashfast->drv->name, hashfast->unique_id); 180 181 return true; 182 } 183 184 static bool hfa_send_generic_frame(struct cgpu_info *hashfast, uint8_t opcode, uint8_t chip_address, 185 uint8_t core_address, uint16_t hdata, uint8_t *data, int len) 186 { 187 uint8_t packet[256]; 188 struct hf_header *p = (struct hf_header *)packet; 189 int tx_length, ret, amount; 190 191 p->preamble = HF_PREAMBLE; 192 p->operation_code = opcode; 193 p->chip_address = chip_address; 194 p->core_address = core_address; 195 p->hdata = htole16(hdata); 196 p->data_length = len / 4; 197 p->crc8 = hfa_crc8(packet); 198 199 if (len) 200 memcpy(&packet[sizeof(struct hf_header)], data, len); 201 tx_length = sizeof(struct hf_header) + len; 202 203 ret = usb_write(hashfast, (char *)packet, tx_length, &amount, C_NULL); 204 205 return ((ret >= 0) && (amount == tx_length)); 206 } 207 208 static bool hfa_send_frame(struct cgpu_info *hashfast, uint8_t opcode, uint16_t hdata, 209 uint8_t *data, int len) 210 { 211 uint8_t packet[256]; 212 struct hf_header *p = (struct hf_header *)packet; 213 int tx_length; 214 215 p->preamble = HF_PREAMBLE; 216 p->operation_code = hfa_cmds[opcode].cmd; 217 p->chip_address = HF_GWQ_ADDRESS; 218 p->core_address = 0; 219 p->hdata = htole16(hdata); 220 p->data_length = len / 4; 221 p->crc8 = hfa_crc8(packet); 222 223 if (len) 224 memcpy(&packet[sizeof(struct hf_header)], data, len); 225 tx_length = sizeof(struct hf_header) + len; 226 227 return (__hfa_send_frame(hashfast, opcode, tx_length, packet)); 228 } 229 230 /* Send an already assembled packet, consisting of an 8 byte header which may 231 * or may not be followed by a packet body. */ 232 233 static bool hfa_send_packet(struct cgpu_info *hashfast, struct hf_header *h, int cmd) 234 { 235 int amount, ret, len; 236 237 if (unlikely(hashfast->usbinfo.nodev)) 238 return false; 239 240 len = sizeof(*h) + h->data_length * 4; 241 ret = usb_write(hashfast, (char *)h, len, &amount, hfa_cmds[cmd].usb_cmd); 242 if (ret < 0 || amount != len) { 243 applog(LOG_WARNING, "%s %s: send_packet: %s USB Send error, ret %d amount %d vs. length %d", 244 hashfast->drv->name, hashfast->unique_id, hfa_cmds[cmd].cmd_name, ret, amount, len); 245 return false; 246 } 247 return true; 248 } 249 250 #define HFA_GET_HEADER_BUFSIZE 512 251 252 static bool hfa_get_header(struct cgpu_info *hashfast, struct hf_header *h, uint8_t *computed_crc) 253 { 254 int amount, ret, orig_len, len, ofs = 0; 255 cgtimer_t ts_start; 256 char buf[HFA_GET_HEADER_BUFSIZE]; 257 char *header; 258 259 if (unlikely(hashfast->usbinfo.nodev)) 260 return false; 261 262 orig_len = len = sizeof(*h); 263 264 /* Read for up to 500ms till we find the first occurrence of HF_PREAMBLE 265 * though it should be the first byte unless we get woefully out of 266 * sync. */ 267 cgtimer_time(&ts_start); 268 do { 269 cgtimer_t ts_now, ts_diff; 270 271 cgtimer_time(&ts_now); 272 cgtimer_sub(&ts_now, &ts_start, &ts_diff); 273 if (cgtimer_to_ms(&ts_diff) > 500) 274 return false; 275 276 if (unlikely(hashfast->usbinfo.nodev)) 277 return false; 278 if(ofs + len > HFA_GET_HEADER_BUFSIZE) { 279 // Not expected to happen. 280 applog(LOG_WARNING, "hfa_get_header() tried to overflow buf[]."); 281 return false; 282 } 283 ret = usb_read(hashfast, buf + ofs, len, &amount, C_HF_GETHEADER); 284 285 if (unlikely(ret && ret != LIBUSB_ERROR_TIMEOUT)) 286 return false; 287 ofs += amount; 288 header = memchr(buf, HF_PREAMBLE, ofs); 289 if (header) { 290 /* Toss any leading data we can't use */ 291 if (header != buf) { 292 memmove(buf, header, ofs); 293 ofs -= header - buf; 294 } 295 len -= ofs; 296 } 297 else { 298 /* HF_PREAMBLE not found, toss all the useless leading data. */ 299 ofs = 0; 300 len = sizeof(*h); 301 } 302 } while (len > 0); 303 304 memcpy(h, header, orig_len); 305 *computed_crc = hfa_crc8((uint8_t *)h); 306 307 return true; 308 } 309 310 static bool hfa_get_data(struct cgpu_info *hashfast, char *buf, int len4) 311 { 312 int amount, ret, len = len4 * 4; 313 314 if (unlikely(hashfast->usbinfo.nodev)) 315 return false; 316 ret = usb_read(hashfast, buf, len, &amount, C_HF_GETDATA); 317 if (ret) 318 return false; 319 if (amount != len) { 320 applog(LOG_WARNING, "%s %s: get_data: Strange amount returned %d vs. expected %d", 321 hashfast->drv->name, hashfast->unique_id, amount, len); 322 return false; 323 } 324 return true; 325 } 326 327 static const char *hf_usb_init_errors[] = { 328 "Success", 329 "Reset timeout", 330 "Address cycle timeout", 331 "Clockgate operation timeout", 332 "Configuration operation timeout", 333 "Excessive core failures", 334 "All cores failed diagnostics", 335 "Too many groups configured - increase ntime roll amount", 336 "Chaining connections detected but secondary board(s) did not respond", 337 "Secondary board communication error", 338 "Main board 12V power is bad", 339 "Secondary board(s) 12V power is bad", 340 "Main board FPGA programming error", 341 "Main board FPGA SPI read timeout", 342 "Main board FPGA Bad magic number", 343 "Main board FPGA SPI write timeout", 344 "Main board FPGA register read/write test failed", 345 "ASIC core power fault", 346 "Dynamic baud rate change timeout", 347 "Address failure", 348 "Regulator programming error", 349 "Address range inconsistent after mixed reconfiguration", 350 "Timeout after mixed reconfiguration" 351 }; 352 353 static bool hfa_clear_readbuf(struct cgpu_info *hashfast); 354 355 struct op_nameframe { 356 struct hf_header h; 357 char name[32]; 358 } __attribute__((packed)); 359 360 static void hfa_write_opname(struct cgpu_info *hashfast, struct hashfast_info *info) 361 { 362 const uint8_t opcode = HF_USB_CMD(OP_NAME); 363 struct op_nameframe nameframe; 364 struct hf_header *h = (struct hf_header *)&nameframe; 365 const int tx_length = sizeof(struct op_nameframe); 366 367 memset(&nameframe, 0, sizeof(nameframe)); 368 strncpy(nameframe.name, info->op_name, 30); 369 h->preamble = HF_PREAMBLE; 370 h->operation_code = hfa_cmds[opcode].cmd; 371 h->core_address = 1; 372 h->data_length = 32 / 4; 373 h->crc8 = hfa_crc8((unsigned char *)h); 374 applog(LOG_DEBUG, "%s %d: Opname being set to %s", hashfast->drv->name, 375 hashfast->device_id, info->op_name); 376 __hfa_send_frame(hashfast, opcode, tx_length, (uint8_t *)&nameframe); 377 } 378 379 /* If no opname or an invalid opname is set, change it to the serial number if 380 * it exists, or a random name based on timestamp if not. */ 381 static void hfa_choose_opname(struct cgpu_info *hashfast, struct hashfast_info *info) 382 { 383 uint64_t usecs; 384 385 if (info->serial_number) 386 sprintf(info->op_name, "%08x", info->serial_number); 387 else { 388 struct timeval tv_now; 389 390 cgtime(&tv_now); 391 usecs = (uint64_t)(tv_now.tv_sec) * (uint64_t)1000000 + (uint64_t)tv_now.tv_usec; 392 sprintf(info->op_name, "%lx", (long unsigned int)usecs); 393 } 394 hfa_write_opname(hashfast, info); 395 } 396 397 // Generic setting header 398 struct hf_settings_data { 399 uint8_t revision; 400 uint8_t ref_frequency; 401 uint16_t magic; 402 uint16_t frequency0; 403 uint16_t voltage0; 404 uint16_t frequency1; 405 uint16_t voltage1; 406 uint16_t frequency2; 407 uint16_t voltage2; 408 uint16_t frequency3; 409 uint16_t voltage3; 410 } __attribute__((packed,aligned(4))); 411 412 static bool hfa_set_voltages(struct cgpu_info *hashfast, struct hashfast_info *info) 413 { 414 struct hf_settings_data op_settings_data; 415 416 op_settings_data.revision = 1; 417 op_settings_data.ref_frequency = 25; 418 op_settings_data.magic = HFA_MAGIC_SETTINGS_VALUE; 419 420 op_settings_data.frequency0 = info->hash_clock_rate; 421 op_settings_data.voltage0 = info->hash_voltage; 422 op_settings_data.frequency1 = info->hash_clock_rate; 423 op_settings_data.voltage1 = info->hash_voltage; 424 op_settings_data.frequency2 = info->hash_clock_rate; 425 op_settings_data.voltage2 = info->hash_voltage; 426 op_settings_data.frequency3 = info->hash_clock_rate; 427 op_settings_data.voltage3 = info->hash_voltage; 428 429 hfa_send_generic_frame(hashfast, OP_SETTINGS, 0x00, 0x01, HFA_MAGIC_SETTINGS_VALUE, 430 (uint8_t *)&op_settings_data, sizeof(op_settings_data)); 431 // reset the board once to switch to new voltage settings 432 hfa_send_generic_frame(hashfast, OP_POWER, 0xff, 0x00, 0x1, NULL, 0); 433 hfa_send_generic_frame(hashfast, OP_POWER, 0xff, 0x00, 0x2, NULL, 0); 434 435 return true; 436 } 437 438 static bool hfa_send_shutdown(struct cgpu_info *hashfast); 439 440 static bool hfa_reset(struct cgpu_info *hashfast, struct hashfast_info *info) 441 { 442 struct hf_usb_init_header usb_init[2], *hu = usb_init; 443 struct hf_usb_init_base *db; 444 struct hf_usb_init_options *ho; 445 int retries = 0, i; 446 bool ret = false; 447 char buf[1024]; 448 struct hf_header *h = (struct hf_header *)buf; 449 uint8_t hcrc; 450 451 /* Hash clock rate in Mhz. Set to opt_hfa_hash_clock if it has not 452 * been inherited across a restart. */ 453 if (!info->hash_clock_rate) 454 info->hash_clock_rate = opt_hfa_hash_clock; 455 info->group_ntime_roll = opt_hfa_ntime_roll; 456 info->core_ntime_roll = 1; 457 458 // Assemble the USB_INIT request 459 memset(hu, 0, sizeof(*hu)); 460 hu->preamble = HF_PREAMBLE; 461 hu->operation_code = OP_USB_INIT; 462 hu->protocol = PROTOCOL_GLOBAL_WORK_QUEUE; // Protocol to use 463 if (!opt_hfa_noshed) 464 hu->shed_supported = true; 465 // Force PLL bypass 466 hu->pll_bypass = opt_hfa_pll_bypass; 467 hu->hash_clock = info->hash_clock_rate; // Hash clock rate in Mhz 468 if (info->group_ntime_roll > 1 && info->core_ntime_roll) { 469 ho = (struct hf_usb_init_options *)(hu + 1); 470 memset(ho, 0, sizeof(*ho)); 471 ho->group_ntime_roll = info->group_ntime_roll; 472 ho->core_ntime_roll = info->core_ntime_roll; 473 hu->data_length = sizeof(*ho) / 4; 474 } 475 hu->crc8 = hfa_crc8((uint8_t *)hu); 476 applog(LOG_INFO, "%s %s: Sending OP_USB_INIT with GWQ protocol specified", 477 hashfast->drv->name, hashfast->unique_id); 478 resend: 479 if (unlikely(hashfast->usbinfo.nodev)) 480 goto out; 481 482 if (!hfa_clear_readbuf(hashfast)) 483 goto out; 484 485 if (!hfa_send_packet(hashfast, (struct hf_header *)hu, HF_USB_CMD(OP_USB_INIT))) 486 goto out; 487 488 // Check for the correct response. 489 // We extend the normal timeout - a complete device initialization, including 490 // bringing power supplies up from standby, etc., can take over a second. 491 tryagain: 492 for (i = 0; i < 10; i++) { 493 ret = hfa_get_header(hashfast, h, &hcrc); 494 if (unlikely(hashfast->usbinfo.nodev)) 495 goto out; 496 if (ret) 497 break; 498 } 499 if (!ret) { 500 if (retries++ < 3) 501 goto resend; 502 applog(LOG_WARNING, "%s %s: OP_USB_INIT failed!", hashfast->drv->name, hashfast->unique_id); 503 goto out; 504 } 505 if (h->crc8 != hcrc) { 506 applog(LOG_WARNING, "%s %s: OP_USB_INIT failed! CRC mismatch", hashfast->drv->name, hashfast->unique_id); 507 ret = false; 508 goto out; 509 } 510 if (h->operation_code != OP_USB_INIT) { 511 // This can happen if valid packet(s) were in transit *before* the OP_USB_INIT arrived 512 // at the device, so we just toss the packets and keep looking for the response. 513 applog(LOG_WARNING, "%s %s: OP_USB_INIT: Tossing packet, valid but unexpected type %d", 514 hashfast->drv->name, hashfast->unique_id, h->operation_code); 515 hfa_get_data(hashfast, buf, h->data_length); 516 if (retries++ < 3) 517 goto tryagain; 518 ret = false; 519 goto out; 520 } 521 522 applog(LOG_DEBUG, "%s %s: Good reply to OP_USB_INIT", hashfast->drv->name, hashfast->unique_id); 523 applog(LOG_DEBUG, "%s %s: OP_USB_INIT: %d die in chain, %d cores, device_type %d, refclk %d Mhz", 524 hashfast->drv->name, hashfast->unique_id, h->chip_address, h->core_address, h->hdata & 0xff, (h->hdata >> 8) & 0xff); 525 526 // Save device configuration 527 info->asic_count = h->chip_address; 528 info->core_count = h->core_address; 529 info->device_type = (uint8_t)h->hdata; 530 info->ref_frequency = (uint8_t)(h->hdata >> 8); 531 info->hash_sequence_head = 0; 532 info->hash_sequence_tail = 0; 533 info->device_sequence_tail = 0; 534 535 if (info->asic_count == 12) 536 hashfast->drv->name = "HFS"; 537 else if (info->asic_count == 4) 538 hashfast->drv->name = "HFB"; 539 540 // Size in bytes of the core bitmap in bytes 541 info->core_bitmap_size = (((info->asic_count * info->core_count) + 31) / 32) * 4; 542 543 // Get the usb_init_base structure 544 if (!hfa_get_data(hashfast, (char *)&info->usb_init_base, U32SIZE(info->usb_init_base))) { 545 applog(LOG_WARNING, "%s %s: OP_USB_INIT failed! Failure to get usb_init_base data", 546 hashfast->drv->name, hashfast->unique_id); 547 ret = false; 548 goto out; 549 } 550 db = &info->usb_init_base; 551 info->firmware_version = ((db->firmware_rev >> 8) & 0xff) + (double)(db->firmware_rev & 0xff) / 10.0; 552 info->hardware_version = ((db->hardware_rev >> 8) & 0xff) + (double)(db->hardware_rev & 0xff) / 10.0; 553 applog(LOG_INFO, "%s %s: firmware_rev: %.1f", hashfast->drv->name, hashfast->unique_id, 554 info->firmware_version); 555 applog(LOG_INFO, "%s %s: hardware_rev: %.1f", hashfast->drv->name, hashfast->unique_id, 556 info->hardware_version); 557 applog(LOG_INFO, "%s %s: serial number: %08x", hashfast->drv->name, hashfast->unique_id, 558 db->serial_number); 559 applog(LOG_INFO, "%s %s: hash clockrate: %d Mhz", hashfast->drv->name, hashfast->unique_id, 560 db->hash_clockrate); 561 applog(LOG_INFO, "%s %s: inflight_target: %d", hashfast->drv->name, hashfast->unique_id, 562 db->inflight_target); 563 applog(LOG_INFO, "%s %s: sequence_modulus: %d", hashfast->drv->name, hashfast->unique_id, 564 db->sequence_modulus); 565 566 // Now a copy of the config data used 567 if (!hfa_get_data(hashfast, (char *)&info->config_data, U32SIZE(info->config_data))) { 568 applog(LOG_WARNING, "%s %s: OP_USB_INIT failed! Failure to get config_data", 569 hashfast->drv->name, hashfast->unique_id); 570 ret = false; 571 goto out; 572 } 573 574 // Now the core bitmap 575 info->core_bitmap = malloc(info->core_bitmap_size); 576 if (!info->core_bitmap) 577 quit(1, "Failed to malloc info core bitmap in hfa_reset"); 578 if (!hfa_get_data(hashfast, (char *)info->core_bitmap, info->core_bitmap_size / 4)) { 579 applog(LOG_WARNING, "%s %s: OP_USB_INIT failed! Failure to get core_bitmap", hashfast->drv->name, hashfast->unique_id); 580 ret = false; 581 goto out; 582 } 583 584 // See if the initialization suceeded 585 if (db->operation_status) { 586 applog(LOG_ERR, "%s %s: OP_USB_INIT failed! Operation status %d (%s)", 587 hashfast->drv->name, hashfast->unique_id, db->operation_status, 588 (db->operation_status < sizeof(hf_usb_init_errors)/sizeof(hf_usb_init_errors[0])) ? 589 hf_usb_init_errors[db->operation_status] : "Unknown error code"); 590 ret = false; 591 switch (db->operation_status) { 592 case E_CORE_POWER_FAULT: 593 for (i = 0; i < 4; i++) { 594 if (((db->extra_status_1 >> i) & 0x11) == 0x1) { 595 applog(LOG_ERR, "%s %s: OP_USB_INIT: Quadrant %d (of 4) regulator failure", 596 hashfast->drv->name, hashfast->unique_id, i + 1); 597 } 598 } 599 break; 600 default: 601 break; 602 } 603 goto out; 604 } 605 606 if (!db->hash_clockrate) { 607 applog(LOG_INFO, "%s %s: OP_USB_INIT failed! Clockrate reported as zero", 608 hashfast->drv->name, hashfast->unique_id); 609 ret = false; 610 goto out; 611 } 612 info->num_sequence = db->sequence_modulus; 613 info->serial_number = db->serial_number; 614 info->base_clock = db->hash_clockrate; 615 616 ret = hfa_clear_readbuf(hashfast); 617 out: 618 if (!ret) { 619 hfa_send_shutdown(hashfast); 620 usb_nodev(hashfast); 621 } 622 return ret; 623 } 624 625 static bool hfa_clear_readbuf(struct cgpu_info *hashfast) 626 { 627 int amount, ret = 0; 628 char buf[512]; 629 630 do { 631 if (hashfast->usbinfo.nodev) { 632 ret = LIBUSB_ERROR_NO_DEVICE; 633 break; 634 } 635 ret = usb_read(hashfast, buf, 512, &amount, C_HF_CLEAR_READ); 636 } while (!ret && amount); 637 638 if (ret && ret != LIBUSB_ERROR_TIMEOUT) 639 return false; 640 return true; 641 } 642 643 static bool hfa_send_shutdown(struct cgpu_info *hashfast) 644 { 645 bool ret = false; 646 647 if (hashfast->usbinfo.nodev) 648 return ret; 649 /* Send a restart before the shutdown frame to tell the device to 650 * discard any work it thinks is in flight for a cleaner restart. */ 651 if (!hfa_send_frame(hashfast, HF_USB_CMD(OP_WORK_RESTART), 0, (uint8_t *)NULL, 0)) 652 return ret; 653 if (hfa_send_frame(hashfast, HF_USB_CMD(OP_USB_SHUTDOWN), 0, NULL, 0)) { 654 /* Wait to allow device to properly shut down. */ 655 cgsleep_ms(1000); 656 ret = true; 657 } 658 return ret; 659 } 660 661 static struct cgpu_info *hfa_old_device(struct cgpu_info *hashfast, struct hashfast_info *info) 662 { 663 struct cgpu_info *cgpu, *found = NULL; 664 struct hashfast_info *cinfo = NULL; 665 int i; 666 667 /* See if we can find a zombie instance of the same device */ 668 for (i = 0; i < mining_threads; i++) { 669 cgpu = mining_thr[i]->cgpu; 670 if (!cgpu) 671 continue; 672 if (cgpu == hashfast) 673 continue; 674 if (cgpu->drv->drv_id != DRIVER_hashfast) 675 continue; 676 if (!cgpu->usbinfo.nodev) 677 continue; 678 cinfo = cgpu->device_data; 679 if (!cinfo) 680 continue; 681 if (info->op_name[0] != '\0' && !strncmp(info->op_name, cinfo->op_name, 32)) { 682 found = cgpu; 683 break; 684 } 685 if (info->serial_number && info->serial_number == cinfo->serial_number) { 686 found = cgpu; 687 break; 688 } 689 } 690 return found; 691 } 692 693 static void hfa_set_clock(struct cgpu_info *hashfast, struct hashfast_info *info) 694 { 695 uint16_t hdata; 696 int i; 697 698 hdata = (WR_CLOCK_VALUE << WR_COMMAND_SHIFT) | info->hash_clock_rate; 699 700 hfa_send_frame(hashfast, HF_USB_CMD(OP_WORK_RESTART), hdata, (uint8_t *)NULL, 0); 701 /* We won't know what the real clock is in this case without a 702 * usb_init_base message so we have to assume it's what we asked. */ 703 info->base_clock = info->hash_clock_rate; 704 for (i = 0; i < info->asic_count; i++) 705 info->die_data[i].hash_clock = info->base_clock; 706 } 707 708 /* Look for an op name match and apply any options to its first attempted 709 * init sequence. This function allows any arbitrary number of extra parameters 710 * to be added in the future. */ 711 static void hfa_check_options(struct hashfast_info *info) 712 { 713 char *p, *options, *found = NULL, *marker; 714 int maxlen, option = 0; 715 716 if (!opt_hfa_options) 717 return; 718 719 if (!info->op_name) 720 return; 721 722 maxlen = strlen(info->op_name); 723 724 options = strdup(opt_hfa_options); 725 for (p = strtok(options, ","); p; p = strtok(NULL, ",")) { 726 int cmplen = strlen(p); 727 728 if (maxlen < cmplen) 729 cmplen = maxlen; 730 if (cmplen < maxlen) 731 continue; 732 if (!strncmp(info->op_name, p, cmplen)) { 733 found = strdup(p); 734 break; 735 } 736 } 737 free(options); 738 if (!found) 739 return; 740 741 for (p = strtok(found, ":"); p; p = strtok(NULL, ":")) { 742 long lval; 743 744 /* Parse each option in order, leaving room to add more */ 745 switch(option++) { 746 default: 747 break; 748 case 1: 749 lval = strtol(p, NULL, 10); 750 if (lval < HFA_CLOCK_MIN || lval > HFA_CLOCK_MAX) { 751 applog(LOG_ERR, "Invalid clock speed %ld set with hashfast option for %s", 752 lval, info->op_name); 753 break; 754 } 755 info->hash_clock_rate = lval; 756 marker = strchr(p,'@'); 757 if (marker != NULL) { 758 lval = strtol(marker+1, NULL, 10); 759 if (lval < HFA_VOLTAGE_MIN || lval > HFA_VOLTAGE_MAX) { 760 applog(LOG_ERR, "Invalid core voltage %ld set with hashfast option for %s", 761 lval, info->op_name); 762 break; 763 } 764 info->hash_voltage = lval; 765 } 766 break; 767 } 768 } 769 free(found); 770 } 771 772 static bool hfa_detect_common(struct cgpu_info *hashfast) 773 { 774 struct hashfast_info *info; 775 char buf[1024]; 776 struct hf_header *h = (struct hf_header *)buf; 777 uint8_t hcrc; 778 bool ret; 779 int i; 780 781 info = calloc(sizeof(struct hashfast_info), 1); 782 if (!info) 783 quit(1, "Failed to calloc hashfast_info in hfa_detect_common"); 784 hashfast->device_data = info; 785 786 /* Try sending and receiving an OP_NAME */ 787 ret = hfa_send_frame(hashfast, HF_USB_CMD(OP_NAME), 0, (uint8_t *)NULL, 0); 788 if (hashfast->usbinfo.nodev) { 789 ret = false; 790 goto out; 791 } 792 if (!ret) { 793 applog(LOG_WARNING, "%s %d: Failed to send OP_NAME!", hashfast->drv->name, 794 hashfast->device_id); 795 goto out; 796 } 797 ret = hfa_get_header(hashfast, h, &hcrc); 798 if (hashfast->usbinfo.nodev) { 799 ret = false; 800 goto out; 801 } 802 if (!ret) { 803 /* We should receive a valid header even if OP_NAME isn't 804 * supported by the firmware. */ 805 applog(LOG_NOTICE, "%s %d: No response to name query - failed init or firmware upgrade required.", 806 hashfast->drv->name, hashfast->device_id); 807 ret = true; 808 } else { 809 /* Only try to parse the name if the firmware supports OP_NAME */ 810 if (h->operation_code == OP_NAME) { 811 if (!hfa_get_data(hashfast, info->op_name, 32 / 4)) { 812 applog(LOG_WARNING, "%s %d: OP_NAME failed! Failure to get op_name data", 813 hashfast->drv->name, hashfast->device_id); 814 goto out; 815 } 816 info->has_opname = info->opname_valid = true; 817 applog(LOG_DEBUG, "%s: Returned an OP_NAME", hashfast->drv->name); 818 for (i = 0; i < 32; i++) { 819 if (i > 0 && info->op_name[i] == '\0') 820 break; 821 /* Make sure the op_name is valid ascii only */ 822 if (info->op_name[i] < 32 || info->op_name[i] > 126) { 823 info->opname_valid = false; 824 break; 825 } 826 } 827 } 828 } 829 830 info->cgpu = hashfast; 831 /* Look for a matching zombie instance and inherit values from it if it 832 * exists. */ 833 if (info->has_opname && info->opname_valid) { 834 info->old_cgpu = hfa_old_device(hashfast, info); 835 if (info->old_cgpu) { 836 struct hashfast_info *cinfo = info->old_cgpu->device_data; 837 838 applog(LOG_NOTICE, "%s: Found old instance by op name %s at device %d", 839 hashfast->drv->name, info->op_name, info->old_cgpu->device_id); 840 info->resets = ++cinfo->resets; 841 info->hash_clock_rate = cinfo->hash_clock_rate; 842 } else { 843 applog(LOG_NOTICE, "%s: Found device with name %s", hashfast->drv->name, 844 info->op_name); 845 hfa_check_options(info); 846 } 847 } 848 849 out: 850 if (!ret) { 851 if (!hashfast->usbinfo.nodev) 852 hfa_clear_readbuf(hashfast); 853 hashfast->device_data = NULL; 854 free(info); 855 } 856 return ret; 857 } 858 859 static bool hfa_initialise(struct cgpu_info *hashfast) 860 { 861 int err = 7; 862 863 if (hashfast->usbinfo.nodev) 864 return false; 865 866 if (!hfa_clear_readbuf(hashfast)) 867 return false; 868 #ifdef WIN32 869 err = usb_transfer(hashfast, 0, 9, 1, 0, C_ATMEL_RESET); 870 if (!err) 871 err = usb_transfer(hashfast, 0x21, 0x22, 0, 0, C_ATMEL_OPEN); 872 if (!err) { 873 uint32_t buf[2]; 874 875 /* Magic sequence to reset device only really needed for windows 876 * but harmless on linux. */ 877 buf[0] = 0x80250000; 878 buf[1] = 0x00000800; 879 err = usb_transfer_data(hashfast, 0x21, 0x20, 0x0000, 0, buf, 880 7, C_ATMEL_INIT); 881 } 882 if (err < 0) { 883 applog(LOG_INFO, "%s %s: Failed to open with error %s", 884 hashfast->drv->name, hashfast->unique_id, libusb_error_name(err)); 885 } 886 #endif 887 /* Must have transmitted init sequence sized buffer */ 888 return (err == 7); 889 } 890 891 static void hfa_dfu_boot(struct cgpu_info *hashfast) 892 { 893 bool ret; 894 895 if (unlikely(hashfast->usbinfo.nodev)) 896 return; 897 898 ret = hfa_send_frame(hashfast, HF_USB_CMD(OP_DFU), 0, NULL, 0); 899 applog(LOG_WARNING, "%s %s: %03d:%03d DFU Boot %s", hashfast->drv->name, hashfast->unique_id, 900 hashfast->usbinfo.bus_number, hashfast->usbinfo.device_address, 901 ret ? "Succeeded" : "Failed"); 902 } 903 904 static struct cgpu_info *hfa_detect_one(libusb_device *dev, struct usb_find_devices *found) 905 { 906 struct cgpu_info *hashfast; 907 908 hashfast = usb_alloc_cgpu(&hashfast_drv, HASHFAST_MINER_THREADS); 909 if (!hashfast) 910 quit(1, "Failed to usb_alloc_cgpu hashfast"); 911 hashfast->unique_id = ""; 912 913 if (!usb_init(hashfast, dev, found)) { 914 hashfast = usb_free_cgpu(hashfast); 915 return NULL; 916 } 917 918 hashfast->usbdev->usb_type = USB_TYPE_STD; 919 920 if (!hfa_initialise(hashfast)) { 921 hashfast = usb_free_cgpu(hashfast); 922 return NULL; 923 } 924 if (opt_hfa_dfu_boot) { 925 hfa_dfu_boot(hashfast); 926 hashfast = usb_free_cgpu(hashfast); 927 opt_hfa_dfu_boot = false; 928 return NULL; 929 } 930 if (!hfa_detect_common(hashfast)) { 931 usb_uninit(hashfast); 932 hashfast = usb_free_cgpu(hashfast); 933 return NULL; 934 } 935 if (!add_cgpu(hashfast)) 936 return NULL; 937 938 if (opt_hfa_name) { 939 struct hashfast_info *info = hashfast->device_data; 940 941 strncpy(info->op_name, opt_hfa_name, 30); 942 applog(LOG_NOTICE, "%s %d %03d:%03d: Writing name %s", hashfast->drv->name, 943 hashfast->device_id, hashfast->usbinfo.bus_number, hashfast->usbinfo.device_address, 944 info->op_name); 945 hfa_write_opname(hashfast, info); 946 opt_hfa_name = NULL; 947 } 948 949 return hashfast; 950 } 951 952 static void hfa_detect(bool __maybe_unused hotplug) 953 { 954 /* Set up the CRC tables only once. */ 955 if (!hfa_crc8_set) 956 hfa_init_crc8(); 957 usb_detect(&hashfast_drv, hfa_detect_one); 958 } 959 960 static bool hfa_get_packet(struct cgpu_info *hashfast, struct hf_header *h) 961 { 962 uint8_t hcrc; 963 bool ret; 964 965 if (unlikely(hashfast->usbinfo.nodev)) 966 return false; 967 968 ret = hfa_get_header(hashfast, h, &hcrc); 969 if (unlikely(!ret)) 970 goto out; 971 if (unlikely(h->crc8 != hcrc)) { 972 applog(LOG_WARNING, "%s %s: Bad CRC %d vs %d, discarding packet", 973 hashfast->drv->name, hashfast->unique_id, h->crc8, hcrc); 974 ret = false; 975 goto out; 976 } 977 if (h->data_length > 0) 978 ret = hfa_get_data(hashfast, (char *)(h + 1), h->data_length); 979 if (unlikely(!ret)) { 980 applog(LOG_WARNING, "%s %s: Failed to get data associated with header", 981 hashfast->drv->name, hashfast->unique_id); 982 } 983 984 out: 985 return ret; 986 } 987 988 static void hfa_running_shutdown(struct cgpu_info *hashfast, struct hashfast_info *info); 989 990 static void hfa_parse_gwq_status(struct cgpu_info *hashfast, struct hashfast_info *info, 991 struct hf_header *h) 992 { 993 struct hf_gwq_data *g = (struct hf_gwq_data *)(h + 1); 994 struct work *work; 995 996 applog(LOG_DEBUG, "%s %s: OP_GWQ_STATUS, device_head %4d tail %4d my tail %4d shed %3d inflight %4d", 997 hashfast->drv->name, hashfast->unique_id, g->sequence_head, g->sequence_tail, info->hash_sequence_tail, 998 g->shed_count, HF_SEQUENCE_DISTANCE(info->hash_sequence_head,g->sequence_tail)); 999 1000 /* This is a special flag that the thermal overload has been tripped */ 1001 if (unlikely(h->core_address & 0x80)) { 1002 applog(LOG_ERR, "%s %s: Thermal overload tripped! Shutting down device", 1003 hashfast->drv->name, hashfast->unique_id); 1004 hfa_running_shutdown(hashfast, info); 1005 usb_nodev(hashfast); 1006 return; 1007 } 1008 1009 mutex_lock(&info->lock); 1010 info->raw_hashes += g->hash_count; 1011 info->device_sequence_head = g->sequence_head; 1012 info->device_sequence_tail = g->sequence_tail; 1013 info->shed_count = g->shed_count; 1014 /* Free any work that is no longer required */ 1015 while (info->device_sequence_tail != info->hash_sequence_tail) { 1016 if (++info->hash_sequence_tail >= info->num_sequence) 1017 info->hash_sequence_tail = 0; 1018 if (unlikely(!(work = info->works[info->hash_sequence_tail]))) { 1019 applog(LOG_ERR, "%s %s: Bad work sequence tail %d head %d devhead %d devtail %d sequence %d", 1020 hashfast->drv->name, hashfast->unique_id, info->hash_sequence_tail, 1021 info->hash_sequence_head, info->device_sequence_head, 1022 info->device_sequence_tail, info->num_sequence); 1023 hashfast->shutdown = true; 1024 usb_nodev(hashfast); 1025 break; 1026 } 1027 applog(LOG_DEBUG, "%s %s: Completing work on hash_sequence_tail %d", 1028 hashfast->drv->name, hashfast->unique_id, info->hash_sequence_tail); 1029 free_work(work); 1030 info->works[info->hash_sequence_tail] = NULL; 1031 } 1032 mutex_unlock(&info->lock); 1033 } 1034 1035 /* Board temperature conversion */ 1036 static float board_temperature(uint16_t adc) 1037 { 1038 float t, r, f, b; 1039 1040 if (adc < 40 || adc > 650) 1041 return((float) 0.0); // Bad count 1042 1043 b = 3590.0; 1044 f = (float)adc / 1023.0; 1045 r = 1.0 / (1.0 / f - 1.0); 1046 t = log(r) / b; 1047 t += 1.0 / (25.0 + 273.15); 1048 t = 1.0 / t - 273.15; 1049 1050 return t; 1051 } 1052 1053 static void hfa_update_die_status(struct cgpu_info *hashfast, struct hashfast_info *info, 1054 struct hf_header *h) 1055 { 1056 struct hf_g1_die_data *d = (struct hf_g1_die_data *)(h + 1), *ds; 1057 int num_included = (h->data_length * 4) / sizeof(struct hf_g1_die_data); 1058 int i, j, die = h->chip_address; 1059 1060 float die_temperature, board_temp; 1061 float core_voltage[6]; 1062 1063 // Copy in the data. They're numbered sequentially from the starting point 1064 ds = info->die_status + h->chip_address; 1065 for (i = 0; i < num_included; i++) 1066 memcpy(ds++, d++, sizeof(struct hf_g1_die_data)); 1067 1068 for (i = 0, d = &info->die_status[h->chip_address]; i < num_included; i++, d++) { 1069 die += i; 1070 die_temperature = GN_DIE_TEMPERATURE(d->die.die_temperature); 1071 /* Sanity checking */ 1072 if (unlikely(die_temperature > 255)) 1073 die_temperature = info->die_data[die].temp; 1074 else 1075 info->die_data[die].temp = die_temperature; 1076 board_temp = board_temperature(d->temperature); 1077 if (unlikely(board_temp > 255)) 1078 board_temp = info->die_data[die].board_temp; 1079 else 1080 info->die_data[die].board_temp = board_temp; 1081 for (j = 0; j < 6; j++) 1082 core_voltage[j] = GN_CORE_VOLTAGE(d->die.core_voltage[j]); 1083 1084 applog(LOG_DEBUG, "%s %s: die %2d: OP_DIE_STATUS Temps die %.1fC board %.1fC vdd's %.2f %.2f %.2f %.2f %.2f %.2f", 1085 hashfast->drv->name, hashfast->unique_id, die, die_temperature, board_temp, 1086 core_voltage[0], core_voltage[1], core_voltage[2], 1087 core_voltage[3], core_voltage[4], core_voltage[5]); 1088 // XXX Convert board phase currents, voltage, temperature 1089 } 1090 if (die == info->asic_count - 1) { 1091 /* We have a full set of die temperatures, find the highest 1092 * current temperature. */ 1093 float max_temp = 0; 1094 1095 info->temp_updates++; 1096 1097 for (die = 0; die < info->asic_count; die++) { 1098 if (info->die_data[die].temp > max_temp) 1099 max_temp = info->die_data[die].temp; 1100 if (info->die_data[die].board_temp > max_temp) 1101 max_temp = info->die_data[die].board_temp; 1102 } 1103 /* Exponentially change the max_temp to smooth out troughs. */ 1104 hashfast->temp = hashfast->temp * 0.63 + max_temp * 0.37; 1105 } 1106 1107 if (unlikely(hashfast->temp >= opt_hfa_overheat)) { 1108 /* -1 means new overheat condition */ 1109 if (!info->overheat) 1110 info->overheat = -1; 1111 } else if (unlikely(info->overheat && hashfast->temp < opt_hfa_overheat - HFA_TEMP_HYSTERESIS)) 1112 info->overheat = 0; 1113 } 1114 1115 static void hfa_parse_nonce(struct thr_info *thr, struct cgpu_info *hashfast, 1116 struct hashfast_info *info, struct hf_header *h) 1117 { 1118 struct hf_candidate_nonce *n = (struct hf_candidate_nonce *)(h + 1); 1119 int i, num_nonces = h->data_length / U32SIZE(sizeof(struct hf_candidate_nonce)); 1120 1121 applog(LOG_DEBUG, "%s %s: OP_NONCE: %2d/%2d:, num_nonces %d hdata 0x%04x", 1122 hashfast->drv->name, hashfast->unique_id, h->chip_address, h->core_address, num_nonces, h->hdata); 1123 for (i = 0; i < num_nonces; i++, n++) { 1124 struct work *work = NULL; 1125 1126 applog(LOG_DEBUG, "%s %s: OP_NONCE: %2d: %2d: ntime %2d sequence %4d nonce 0x%08x", 1127 hashfast->drv->name, hashfast->unique_id, h->chip_address, i, n->ntime & HF_NTIME_MASK, n->sequence, n->nonce); 1128 1129 if (n->sequence < info->usb_init_base.sequence_modulus) { 1130 // Find the job from the sequence number 1131 mutex_lock(&info->lock); 1132 work = info->works[n->sequence]; 1133 mutex_unlock(&info->lock); 1134 } else { 1135 applog(LOG_INFO, "%s %s: OP_NONCE: Sequence out of range %4d max %4d", 1136 hashfast->drv->name, hashfast->unique_id, n->sequence, info->usb_init_base.sequence_modulus); 1137 } 1138 1139 if (unlikely(!work)) { 1140 info->no_matching_work++; 1141 applog(LOG_INFO, "%s %s: No matching work!", hashfast->drv->name, hashfast->unique_id); 1142 } else { 1143 applog(LOG_DEBUG, "%s %s: OP_NONCE: sequence %d: submitting nonce 0x%08x ntime %d", 1144 hashfast->drv->name, hashfast->unique_id, n->sequence, n->nonce, n->ntime & HF_NTIME_MASK); 1145 if (submit_noffset_nonce(thr, work, n->nonce, n->ntime & HF_NTIME_MASK)) { 1146 mutex_lock(&info->lock); 1147 info->hash_count += 0xffffffffull * work->device_diff; 1148 mutex_unlock(&info->lock); 1149 } 1150 #if 0 /* Not used */ 1151 if (unlikely(n->ntime & HF_NONCE_SEARCH)) { 1152 /* This tells us there is another share in the 1153 * next 128 nonces */ 1154 applog(LOG_DEBUG, "%s %s: OP_NONCE: SEARCH PROXIMITY EVENT FOUND", 1155 hashfast->drv->name, hashfast->unique_id); 1156 } 1157 #endif 1158 } 1159 } 1160 } 1161 1162 static void hfa_update_die_statistics(struct hashfast_info *info, struct hf_header *h) 1163 { 1164 struct hf_statistics *s = (struct hf_statistics *)(h + 1); 1165 struct hf_long_statistics *l; 1166 1167 // Accumulate the data 1168 l = info->die_statistics + h->chip_address; 1169 1170 l->rx_header_crc += s->rx_header_crc; 1171 l->rx_body_crc += s->rx_body_crc; 1172 l->rx_header_timeouts += s->rx_header_timeouts; 1173 l->rx_body_timeouts += s->rx_body_timeouts; 1174 l->core_nonce_fifo_full += s->core_nonce_fifo_full; 1175 l->array_nonce_fifo_full += s->array_nonce_fifo_full; 1176 l->stats_overrun += s->stats_overrun; 1177 } 1178 1179 static void hfa_update_stats1(struct cgpu_info *hashfast, struct hashfast_info *info, 1180 struct hf_header *h) 1181 { 1182 struct hf_long_usb_stats1 *s1 = &info->stats1; 1183 struct hf_usb_stats1 *sd = (struct hf_usb_stats1 *)(h + 1); 1184 1185 s1->usb_rx_preambles += sd->usb_rx_preambles; 1186 s1->usb_rx_receive_byte_errors += sd->usb_rx_receive_byte_errors; 1187 s1->usb_rx_bad_hcrc += sd->usb_rx_bad_hcrc; 1188 1189 s1->usb_tx_attempts += sd->usb_tx_attempts; 1190 s1->usb_tx_packets += sd->usb_tx_packets; 1191 s1->usb_tx_timeouts += sd->usb_tx_timeouts; 1192 s1->usb_tx_incompletes += sd->usb_tx_incompletes; 1193 s1->usb_tx_endpointstalled += sd->usb_tx_endpointstalled; 1194 s1->usb_tx_disconnected += sd->usb_tx_disconnected; 1195 s1->usb_tx_suspended += sd->usb_tx_suspended; 1196 #if 0 1197 /* We don't care about UART stats so they're not in our struct */ 1198 s1->uart_tx_queue_dma += sd->uart_tx_queue_dma; 1199 s1->uart_tx_interrupts += sd->uart_tx_interrupts; 1200 1201 s1->uart_rx_preamble_ints += sd->uart_rx_preamble_ints; 1202 s1->uart_rx_missed_preamble_ints += sd->uart_rx_missed_preamble_ints; 1203 s1->uart_rx_header_done += sd->uart_rx_header_done; 1204 s1->uart_rx_data_done += sd->uart_rx_data_done; 1205 s1->uart_rx_bad_hcrc += sd->uart_rx_bad_hcrc; 1206 s1->uart_rx_bad_dma += sd->uart_rx_bad_dma; 1207 s1->uart_rx_short_dma += sd->uart_rx_short_dma; 1208 s1->uart_rx_buffers_full += sd->uart_rx_buffers_full; 1209 #endif 1210 if (sd->max_tx_buffers > s1->max_tx_buffers) 1211 s1->max_tx_buffers = sd->max_tx_buffers; 1212 if (sd->max_rx_buffers > s1->max_rx_buffers) 1213 s1->max_rx_buffers = sd->max_rx_buffers; 1214 1215 applog(LOG_DEBUG, "%s %s: OP_USB_STATS1:", hashfast->drv->name, hashfast->unique_id); 1216 applog(LOG_DEBUG, " usb_rx_preambles: %6d", sd->usb_rx_preambles); 1217 applog(LOG_DEBUG, " usb_rx_receive_byte_errors: %6d", sd->usb_rx_receive_byte_errors); 1218 applog(LOG_DEBUG, " usb_rx_bad_hcrc: %6d", sd->usb_rx_bad_hcrc); 1219 1220 applog(LOG_DEBUG, " usb_tx_attempts: %6d", sd->usb_tx_attempts); 1221 applog(LOG_DEBUG, " usb_tx_packets: %6d", sd->usb_tx_packets); 1222 applog(LOG_DEBUG, " usb_tx_timeouts: %6d", sd->usb_tx_timeouts); 1223 applog(LOG_DEBUG, " usb_tx_incompletes: %6d", sd->usb_tx_incompletes); 1224 applog(LOG_DEBUG, " usb_tx_endpointstalled: %6d", sd->usb_tx_endpointstalled); 1225 applog(LOG_DEBUG, " usb_tx_disconnected: %6d", sd->usb_tx_disconnected); 1226 applog(LOG_DEBUG, " usb_tx_suspended: %6d", sd->usb_tx_suspended); 1227 #if 0 1228 applog(LOG_DEBUG, " uart_tx_queue_dma: %6d", sd->uart_tx_queue_dma); 1229 applog(LOG_DEBUG, " uart_tx_interrupts: %6d", sd->uart_tx_interrupts); 1230 1231 applog(LOG_DEBUG, " uart_rx_preamble_ints: %6d", sd->uart_rx_preamble_ints); 1232 applog(LOG_DEBUG, " uart_rx_missed_preamble_ints: %6d", sd->uart_rx_missed_preamble_ints); 1233 applog(LOG_DEBUG, " uart_rx_header_done: %6d", sd->uart_rx_header_done); 1234 applog(LOG_DEBUG, " uart_rx_data_done: %6d", sd->uart_rx_data_done); 1235 applog(LOG_DEBUG, " uart_rx_bad_hcrc: %6d", sd->uart_rx_bad_hcrc); 1236 applog(LOG_DEBUG, " uart_rx_bad_dma: %6d", sd->uart_rx_bad_dma); 1237 applog(LOG_DEBUG, " uart_rx_short_dma: %6d", sd->uart_rx_short_dma); 1238 applog(LOG_DEBUG, " uart_rx_buffers_full: %6d", sd->uart_rx_buffers_full); 1239 #endif 1240 applog(LOG_DEBUG, " max_tx_buffers: %6d", sd->max_tx_buffers); 1241 applog(LOG_DEBUG, " max_rx_buffers: %6d", sd->max_rx_buffers); 1242 } 1243 1244 static void hfa_parse_notice(struct cgpu_info *hashfast, struct hf_header *h) 1245 { 1246 struct hf_usb_notice_data *d; 1247 1248 if (h->data_length == 0) { 1249 applog(LOG_DEBUG, "%s %s: Received OP_USB_NOTICE with zero data length", 1250 hashfast->drv->name, hashfast->unique_id); 1251 return; 1252 } 1253 d = (struct hf_usb_notice_data *)(h + 1); 1254 /* FIXME Do something with the notification code d->extra_data here */ 1255 applog(LOG_NOTICE, "%s %s NOTICE: %s", hashfast->drv->name, hashfast->unique_id, d->message); 1256 } 1257 1258 static void hfa_parse_settings(struct cgpu_info *hashfast, struct hf_header *h) 1259 { 1260 struct hashfast_info *info = hashfast->device_data; 1261 struct hf_settings_data *op_settings_data = (struct hf_settings_data *)(h + 1); 1262 1263 // Check if packet size, revision and magic are matching 1264 if ((h->data_length * 4 == sizeof(struct hf_settings_data)) && 1265 (h->core_address == 0) && 1266 (op_settings_data->revision == 1) && 1267 (op_settings_data->magic == HFA_MAGIC_SETTINGS_VALUE)) 1268 { 1269 applog(LOG_NOTICE, "%s: Device settings (%dMHz@%dmV,%dMHz@%dmV,%dMHz@%dmV,%dMHz@%dmV)", hashfast->drv->name, 1270 op_settings_data->frequency0, op_settings_data->voltage0, 1271 op_settings_data->frequency1, op_settings_data->voltage1, 1272 op_settings_data->frequency2, op_settings_data->voltage2, 1273 op_settings_data->frequency3, op_settings_data->voltage3); 1274 // Set voltage only when current voltage values are different 1275 if ((info->hash_voltage != 0) && 1276 ((op_settings_data->voltage0 != info->hash_voltage) || 1277 (op_settings_data->voltage1 != info->hash_voltage) || 1278 (op_settings_data->voltage2 != info->hash_voltage) || 1279 (op_settings_data->voltage3 != info->hash_voltage))) { 1280 applog(LOG_NOTICE, "%s: Setting default clock and voltage to %dMHz@%dmV", 1281 hashfast->drv->name, info->hash_clock_rate, info->hash_voltage); 1282 hfa_set_voltages(hashfast, info); 1283 } 1284 } 1285 } 1286 1287 static void *hfa_read(void *arg) 1288 { 1289 struct thr_info *thr = (struct thr_info *)arg; 1290 struct cgpu_info *hashfast = thr->cgpu; 1291 struct hashfast_info *info = hashfast->device_data; 1292 char threadname[16]; 1293 1294 snprintf(threadname, sizeof(threadname), "%d/%sRead", hashfast->device_id, hashfast->drv->name); 1295 RenameThread(threadname); 1296 1297 while (likely(!hashfast->shutdown)) { 1298 char buf[512]; 1299 struct hf_header *h = (struct hf_header *)buf; 1300 bool ret; 1301 1302 mutex_lock(&info->rlock); 1303 ret = hfa_get_packet(hashfast, h); 1304 mutex_unlock(&info->rlock); 1305 1306 if (unlikely(hashfast->usbinfo.nodev)) 1307 break; 1308 1309 if (unlikely(!ret)) 1310 continue; 1311 1312 switch (h->operation_code) { 1313 case OP_GWQ_STATUS: 1314 hfa_parse_gwq_status(hashfast, info, h); 1315 break; 1316 case OP_DIE_STATUS: 1317 hfa_update_die_status(hashfast, info, h); 1318 break; 1319 case OP_NONCE: 1320 hfa_parse_nonce(thr, hashfast, info, h); 1321 break; 1322 case OP_STATISTICS: 1323 hfa_update_die_statistics(info, h); 1324 break; 1325 case OP_USB_STATS1: 1326 hfa_update_stats1(hashfast, info, h); 1327 break; 1328 case OP_USB_NOTICE: 1329 hfa_parse_notice(hashfast, h); 1330 break; 1331 case OP_SETTINGS: 1332 hfa_parse_settings(hashfast, h); 1333 break; 1334 case OP_POWER: 1335 case OP_PING: 1336 /* Do nothing */ 1337 break; 1338 default: 1339 if (h->operation_code == OP_FAN) { 1340 applog(LOG_NOTICE, "%s %s: Firmware upgrade required to support fan control", 1341 hashfast->drv->name, hashfast->unique_id); 1342 opt_hfa_target = 0; 1343 break; 1344 } 1345 applog(LOG_WARNING, "%s %s: Unhandled operation code %d", 1346 hashfast->drv->name, hashfast->unique_id, h->operation_code); 1347 break; 1348 } 1349 /* Make sure we send something to the device at least every 5 1350 * seconds so it knows the driver is still alive for when we 1351 * run out of work. The read thread never blocks so is the 1352 * best place to do this. */ 1353 if (time(NULL) - info->last_send > 5) 1354 hfa_send_frame(hashfast, HF_USB_CMD(OP_PING), 0, NULL, 0); 1355 } 1356 applog(LOG_DEBUG, "%s %s: Shutting down read thread", hashfast->drv->name, hashfast->unique_id); 1357 1358 return NULL; 1359 } 1360 1361 static void hfa_set_fanspeed(struct cgpu_info *hashfast, struct hashfast_info *info, 1362 int fanspeed); 1363 1364 static bool hfa_init(struct thr_info *thr) 1365 { 1366 struct cgpu_info *hashfast = thr->cgpu; 1367 struct hashfast_info *info = hashfast->device_data; 1368 struct timeval now; 1369 bool ret; 1370 int i; 1371 1372 if (hashfast->usbinfo.nodev) 1373 return false; 1374 1375 /* hashfast_reset should fill in details for info */ 1376 ret = hfa_reset(hashfast, info); 1377 1378 // The per-die status array 1379 info->die_status = calloc(info->asic_count, sizeof(struct hf_g1_die_data)); 1380 if (unlikely(!(info->die_status))) 1381 quit(1, "Failed to calloc die_status"); 1382 1383 info->die_data = calloc(info->asic_count, sizeof(struct hf_die_data)); 1384 if (unlikely(!(info->die_data))) 1385 quit(1, "Failed to calloc die_data"); 1386 for (i = 0; i < info->asic_count; i++) 1387 info->die_data[i].hash_clock = info->base_clock; 1388 1389 // The per-die statistics array 1390 info->die_statistics = calloc(info->asic_count, sizeof(struct hf_long_statistics)); 1391 if (unlikely(!(info->die_statistics))) 1392 quit(1, "Failed to calloc die_statistics"); 1393 1394 info->works = calloc(sizeof(struct work *), info->num_sequence); 1395 if (!info->works) 1396 quit(1, "Failed to calloc info works in hfa_detect_common"); 1397 if (!ret) 1398 goto out; 1399 1400 /* We will have extracted the serial number by now */ 1401 if (info->has_opname && !info->opname_valid) 1402 hfa_choose_opname(hashfast, info); 1403 1404 /* Use the opname as the displayed unique identifier */ 1405 hashfast->unique_id = info->op_name; 1406 1407 /* Inherit the old device id */ 1408 if (info->old_cgpu) 1409 hashfast->device_id = info->old_cgpu->device_id; 1410 1411 /* If we haven't found a matching old instance, we might not have 1412 * a valid op_name yet or lack support so try to match based on 1413 * serial number. */ 1414 if (!info->old_cgpu) 1415 info->old_cgpu = hfa_old_device(hashfast, info); 1416 1417 if (!info->has_opname && info->old_cgpu) { 1418 struct hashfast_info *cinfo = info->old_cgpu->device_data; 1419 1420 applog(LOG_NOTICE, "%s: Found old instance by serial number %08x at device %d", 1421 hashfast->drv->name, info->serial_number, info->old_cgpu->device_id); 1422 info->resets = ++cinfo->resets; 1423 /* Set the device with the last hash_clock_rate if it's 1424 * different. */ 1425 if (info->hash_clock_rate != cinfo->hash_clock_rate) { 1426 info->hash_clock_rate = cinfo->hash_clock_rate; 1427 hfa_set_clock(hashfast, info); 1428 } 1429 } 1430 1431 // Read current device settings if voltage was set in options 1432 if (info->hash_voltage != 0) 1433 hfa_send_generic_frame(hashfast, OP_SETTINGS, 0x00, 0x00, HFA_MAGIC_SETTINGS_VALUE, NULL, 0); 1434 1435 mutex_init(&info->lock); 1436 mutex_init(&info->rlock); 1437 if (pthread_create(&info->read_thr, NULL, hfa_read, (void *)thr)) 1438 quit(1, "Failed to pthread_create read thr in hfa_prepare"); 1439 1440 cgtime(&now); 1441 get_datestamp(hashfast->init, sizeof(hashfast->init), &now); 1442 hashfast->last_device_valid_work = time(NULL); 1443 hfa_set_fanspeed(hashfast, info, opt_hfa_fan_default); 1444 out: 1445 if (hashfast->usbinfo.nodev) 1446 ret = false; 1447 1448 if (!ret) { 1449 hfa_clear_readbuf(hashfast); 1450 free(info); 1451 hashfast->device_data = NULL; 1452 usb_nodev(hashfast); 1453 } 1454 1455 return ret; 1456 } 1457 1458 /* If this ever returns 0 it means we have shed all the cores which will lead 1459 * to no work being done which will trigger the watchdog. */ 1460 static inline int hfa_basejobs(struct hashfast_info *info) 1461 { 1462 return info->usb_init_base.inflight_target - info->shed_count; 1463 } 1464 1465 /* Figure out how many jobs to send. */ 1466 static int hfa_jobs(struct cgpu_info *hashfast, struct hashfast_info *info) 1467 { 1468 int ret = 0; 1469 1470 if (unlikely(info->overheat)) { 1471 /* Acknowledge and notify of new condition.*/ 1472 if (info->overheat < 0) { 1473 applog(LOG_WARNING, "%s %s: Hit overheat temp %.1f, throttling!", 1474 hashfast->drv->name, hashfast->unique_id, hashfast->temp); 1475 /* Value of 1 means acknowledged overheat */ 1476 info->overheat = 1; 1477 } 1478 goto out; 1479 } 1480 1481 mutex_lock(&info->lock); 1482 ret = hfa_basejobs(info) - HF_SEQUENCE_DISTANCE(info->hash_sequence_head, info->device_sequence_tail); 1483 /* Place an upper limit on how many jobs to queue to prevent sending 1484 * more work than the device can use after a period of outage. */ 1485 if (ret > hfa_basejobs(info)) 1486 ret = hfa_basejobs(info); 1487 mutex_unlock(&info->lock); 1488 1489 if (unlikely(ret < 0)) 1490 ret = 0; 1491 1492 out: 1493 return ret; 1494 } 1495 1496 static void hfa_set_fanspeed(struct cgpu_info *hashfast, struct hashfast_info *info, 1497 int fandiff) 1498 { 1499 const uint8_t opcode = HF_USB_CMD(OP_FAN); 1500 uint8_t packet[256]; 1501 struct hf_header *p = (struct hf_header *)packet; 1502 const int tx_length = sizeof(struct hf_header); 1503 uint16_t hdata; 1504 int fandata; 1505 1506 info->fanspeed += fandiff; 1507 if (info->fanspeed > opt_hfa_fan_max) 1508 info->fanspeed = opt_hfa_fan_max; 1509 else if (info->fanspeed < opt_hfa_fan_min) 1510 info->fanspeed = opt_hfa_fan_min; 1511 fandata = info->fanspeed * 255 / 100; // Fanspeed is in percent, hdata 0-255 1512 hdata = fandata; // Use an int first to avoid overflowing uint16_t 1513 p->preamble = HF_PREAMBLE; 1514 p->operation_code = hfa_cmds[opcode].cmd; 1515 p->chip_address = 0xff; 1516 p->core_address = 1; 1517 p->hdata = htole16(hdata); 1518 p->data_length = 0; 1519 p->crc8 = hfa_crc8(packet); 1520 1521 __hfa_send_frame(hashfast, opcode, tx_length, packet); 1522 } 1523 1524 static void hfa_increase_clock(struct cgpu_info *hashfast, struct hashfast_info *info, 1525 int die) 1526 { 1527 int i, high_clock = 0, low_clock = info->hash_clock_rate; 1528 struct hf_die_data *hdd = &info->die_data[die]; 1529 uint32_t diebit = 0x00000001ul << die; 1530 uint16_t hdata, increase = 10; 1531 1532 if (hdd->hash_clock + increase > info->hash_clock_rate) 1533 increase = info->hash_clock_rate - hdd->hash_clock; 1534 hdd->hash_clock += increase; 1535 hdata = (WR_MHZ_INCREASE << 12) | increase; 1536 if (info->clock_offset) { 1537 for (i = 0; i < info->asic_count; i++) { 1538 if (info->die_data[i].hash_clock > high_clock) 1539 high_clock = info->die_data[i].hash_clock; 1540 if (info->die_data[i].hash_clock < low_clock) 1541 low_clock = info->die_data[i].hash_clock; 1542 } 1543 if (info->firmware_version < 0.5 && low_clock + HFA_CLOCK_MAXDIFF > high_clock) { 1544 /* We can increase all clocks again */ 1545 for (i = 0; i < info->asic_count; i++) { 1546 if (i == die) /* We've already added to this die */ 1547 continue; 1548 info->die_data[i].hash_clock += increase; 1549 } 1550 applog(LOG_INFO, "%s %s: Die %d temp below range %.1f, increasing ALL dies by %d", 1551 hashfast->drv->name, hashfast->unique_id, die, info->die_data[die].temp, increase); 1552 hfa_send_frame(hashfast, HF_USB_CMD(OP_WORK_RESTART), hdata, (uint8_t *)NULL, 0); 1553 info->clock_offset -= increase; 1554 return; 1555 } 1556 } 1557 applog(LOG_INFO, "%s %s: Die temp below range %.1f, increasing die %d clock to %d", 1558 hashfast->drv->name, hashfast->unique_id, info->die_data[die].temp, die, hdd->hash_clock); 1559 hfa_send_frame(hashfast, HF_USB_CMD(OP_WORK_RESTART), hdata, (uint8_t *)&diebit, 4); 1560 } 1561 1562 static void hfa_decrease_clock(struct cgpu_info *hashfast, struct hashfast_info *info, 1563 int die) 1564 { 1565 struct hf_die_data *hdd = &info->die_data[die]; 1566 uint32_t diebit = 0x00000001ul << die; 1567 uint16_t hdata, decrease = 20; 1568 int i, high_clock = 0; 1569 1570 /* Find the fastest die for comparison */ 1571 for (i = 0; i < info->asic_count; i++) { 1572 if (info->die_data[i].hash_clock > high_clock) 1573 high_clock = info->die_data[i].hash_clock; 1574 } 1575 if (hdd->hash_clock - decrease < HFA_CLOCK_MIN) 1576 decrease = hdd->hash_clock - HFA_CLOCK_MIN; 1577 hdata = (WR_MHZ_DECREASE << 12) | decrease; 1578 if (info->firmware_version < 0.5 && high_clock >= hdd->hash_clock + HFA_CLOCK_MAXDIFF) { 1579 /* We can't have huge differences in clocks as it will lead to 1580 * starvation of the faster cores so we have no choice but to 1581 * slow down all dies to tame this one. */ 1582 for (i = 0; i < info->asic_count; i++) 1583 info->die_data[i].hash_clock -= decrease; 1584 applog(LOG_INFO, "%s %s: Die %d temp above range %.1f, decreasing ALL die clocks by %d", 1585 hashfast->drv->name, hashfast->unique_id, die, info->die_data[die].temp, decrease); 1586 hfa_send_frame(hashfast, HF_USB_CMD(OP_WORK_RESTART), hdata, (uint8_t *)NULL, 0); 1587 info->clock_offset += decrease; 1588 return; 1589 1590 } 1591 hdd->hash_clock -= decrease; 1592 applog(LOG_INFO, "%s %s: Die temp above range %.1f, decreasing die %d clock to %d", 1593 hashfast->drv->name, hashfast->unique_id, info->die_data[die].temp, die, hdd->hash_clock); 1594 hfa_send_frame(hashfast, HF_USB_CMD(OP_WORK_RESTART), hdata, (uint8_t *)&diebit, 4); 1595 } 1596 1597 /* Adjust clock according to temperature if need be by changing the clock 1598 * setting and issuing a work restart with the new clock speed. */ 1599 static void hfa_temp_clock(struct cgpu_info *hashfast, struct hashfast_info *info) 1600 { 1601 int temp_change, i, low_clock; 1602 time_t now_t = time(NULL); 1603 bool throttled = false; 1604 1605 if (!opt_hfa_target) 1606 return; 1607 1608 /* First find out if any dies are throttled before trying to optimise 1609 * fanspeed, and find the slowest clock. */ 1610 low_clock = info->hash_clock_rate; 1611 for (i = 0; i < info->asic_count ; i++) { 1612 struct hf_die_data *hdd = &info->die_data[i]; 1613 1614 if (hdd->hash_clock < info->hash_clock_rate) 1615 throttled = true; 1616 if (hdd->hash_clock < low_clock) 1617 low_clock = hdd->hash_clock; 1618 } 1619 1620 /* Find the direction of temperature change since we last checked */ 1621 if (info->temp_updates < 5) 1622 goto dies_only; 1623 info->temp_updates = 0; 1624 temp_change = hashfast->temp - info->last_max_temp; 1625 info->last_max_temp = hashfast->temp; 1626 1627 /* Adjust fanspeeds first if possible before die speeds, increasing 1628 * speed quickly and lowering speed slowly */ 1629 if (hashfast->temp > opt_hfa_target || 1630 (throttled && hashfast->temp >= opt_hfa_target - HFA_TEMP_HYSTERESIS)) { 1631 /* We should be trying to decrease temperature, if it's not on 1632 * its way down. */ 1633 if (info->fanspeed < opt_hfa_fan_max) { 1634 if (!temp_change) 1635 hfa_set_fanspeed(hashfast, info, 5); 1636 else if (temp_change > 0) 1637 hfa_set_fanspeed(hashfast, info, 10); 1638 } 1639 } else if (hashfast->temp >= opt_hfa_target - HFA_TEMP_HYSTERESIS) { 1640 /* In optimal range, try and maintain the same temp */ 1641 if (temp_change > 0) { 1642 /* Temp rising, tweak fanspeed up */ 1643 if (info->fanspeed < opt_hfa_fan_max) 1644 hfa_set_fanspeed(hashfast, info, 2); 1645 } else if (temp_change < 0) { 1646 /* Temp falling, tweak fanspeed down */ 1647 if (info->fanspeed > opt_hfa_fan_min) 1648 hfa_set_fanspeed(hashfast, info, -1); 1649 } 1650 } else { 1651 /* Below optimal range, try and increase temp */ 1652 if (temp_change <= 0 && !throttled) { 1653 if (info->fanspeed > opt_hfa_fan_min) 1654 hfa_set_fanspeed(hashfast, info, -1); 1655 } 1656 } 1657 1658 dies_only: 1659 /* Do no restarts at all if there has been one less than 15 seconds 1660 * ago */ 1661 if (now_t - info->last_restart < 15) 1662 return; 1663 1664 for (i = 1; i <= info->asic_count ; i++) { 1665 int die = (info->last_die_adjusted + i) % info->asic_count; 1666 struct hf_die_data *hdd = &info->die_data[die]; 1667 1668 /* Sanity check */ 1669 if (unlikely(hdd->temp == 0.0 || hdd->temp > 255)) 1670 continue; 1671 1672 /* In target temperature */ 1673 if (hdd->temp >= opt_hfa_target - HFA_TEMP_HYSTERESIS && hdd->temp <= opt_hfa_target) 1674 continue; 1675 1676 if (hdd->temp > opt_hfa_target) { 1677 /* Temp above target range */ 1678 1679 /* Already at min speed */ 1680 if (hdd->hash_clock == HFA_CLOCK_MIN) 1681 continue; 1682 /* Have some leeway before throttling speed */ 1683 if (hdd->temp < opt_hfa_target + HFA_TEMP_HYSTERESIS) 1684 break; 1685 hfa_decrease_clock(hashfast, info, die); 1686 } else { 1687 /* Temp below target range. Only send a restart to 1688 * increase speed no more than every 60 seconds. */ 1689 if (now_t - hdd->last_restart < 60) 1690 continue; 1691 1692 /* Already at max speed */ 1693 if (hdd->hash_clock == info->hash_clock_rate) 1694 continue; 1695 /* Do not increase the clocks on any dies if we have 1696 * a forced offset due to wild differences in clocks, 1697 * unless this is the slowest one. */ 1698 if (info->clock_offset && hdd->hash_clock > low_clock) 1699 continue; 1700 hfa_increase_clock(hashfast, info, die); 1701 } 1702 /* Keep track of the last die adjusted since we only adjust 1703 * one at a time to ensure we end up iterating over all of 1704 * them. */ 1705 info->last_restart = hdd->last_restart = now_t; 1706 info->last_die_adjusted = die; 1707 break; 1708 } 1709 } 1710 1711 static void hfa_running_shutdown(struct cgpu_info *hashfast, struct hashfast_info *info) 1712 { 1713 int iruntime = cgpu_runtime(hashfast); 1714 1715 /* If the device has already disapperaed, don't drop the clock in case 1716 * it was just unplugged as opposed to a failure. */ 1717 if (hashfast->usbinfo.nodev) 1718 return; 1719 1720 /* Only decrease the clock speed if the device has run at this speed 1721 * for less than an hour before failing, otherwise the hashrate gains 1722 * are worth the occasional restart which takes at most a minute. */ 1723 if (iruntime < 3600 && info->hash_clock_rate > HFA_CLOCK_DEFAULT && opt_hfa_fail_drop) { 1724 info->hash_clock_rate -= opt_hfa_fail_drop; 1725 if (info->hash_clock_rate < HFA_CLOCK_DEFAULT) 1726 info->hash_clock_rate = HFA_CLOCK_DEFAULT; 1727 if (info->old_cgpu && info->old_cgpu->device_data) { 1728 struct hashfast_info *cinfo = info->old_cgpu->device_data; 1729 1730 /* Set the master device's clock speed if this is a copy */ 1731 cinfo->hash_clock_rate = info->hash_clock_rate; 1732 } 1733 applog(LOG_WARNING, "%s %s: Decreasing clock speed to %d with reset", 1734 hashfast->drv->name, hashfast->unique_id, info->hash_clock_rate); 1735 } 1736 1737 if (!hfa_send_shutdown(hashfast)) 1738 return; 1739 1740 if (hashfast->usbinfo.nodev) 1741 return; 1742 1743 mutex_lock(&info->rlock); 1744 hfa_clear_readbuf(hashfast); 1745 mutex_unlock(&info->rlock); 1746 1747 usb_nodev(hashfast); 1748 } 1749 1750 static int64_t hfa_scanwork(struct thr_info *thr) 1751 { 1752 struct cgpu_info *hashfast = thr->cgpu; 1753 struct hashfast_info *info = hashfast->device_data; 1754 struct work *base_work = NULL; 1755 int jobs, ret, cycles = 0; 1756 double fail_time; 1757 int64_t hashes; 1758 1759 if (unlikely(hashfast->usbinfo.nodev)) { 1760 applog(LOG_WARNING, "%s %s: device disappeared, disabling", 1761 hashfast->drv->name, hashfast->unique_id); 1762 return -1; 1763 } 1764 1765 /* Base the fail time on no valid nonces for 25 full nonce ranges at 1766 * the current expected hashrate. */ 1767 fail_time = 25.0 * (double)hashfast->drv->max_diff * 0xffffffffull / 1768 (double)(info->base_clock * 1000000) / hfa_basejobs(info); 1769 if (unlikely(share_work_tdiff(hashfast) > fail_time)) { 1770 applog(LOG_WARNING, "%s %s: No valid hashes for over %.0f seconds, shutting down thread", 1771 hashfast->drv->name, hashfast->unique_id, fail_time); 1772 hfa_running_shutdown(hashfast, info); 1773 return -1; 1774 } 1775 1776 if (unlikely(thr->work_restart)) { 1777 restart: 1778 info->last_restart = time(NULL); 1779 thr->work_restart = false; 1780 ret = hfa_send_frame(hashfast, HF_USB_CMD(OP_WORK_RESTART), 0, (uint8_t *)NULL, 0); 1781 if (unlikely(!ret)) { 1782 hfa_running_shutdown(hashfast, info); 1783 return -1; 1784 } 1785 /* Give a full allotment of jobs after a restart, not waiting 1786 * for the status update telling us how much to give. */ 1787 jobs = hfa_basejobs(info); 1788 } else { 1789 /* Only adjust die clocks if there's no restart since two 1790 * restarts back to back get ignored. */ 1791 hfa_temp_clock(hashfast, info); 1792 jobs = hfa_jobs(hashfast, info); 1793 } 1794 1795 /* Wait on restart_wait for up to 0.5 seconds or submit jobs as soon as 1796 * they're required. */ 1797 while (!jobs && ++cycles < 5) { 1798 ret = restart_wait(thr, 100); 1799 if (unlikely(!ret)) 1800 goto restart; 1801 jobs = hfa_jobs(hashfast, info); 1802 } 1803 1804 if (jobs) { 1805 applog(LOG_DEBUG, "%s %s: Sending %d new jobs", hashfast->drv->name, hashfast->unique_id, 1806 jobs); 1807 } 1808 1809 while (jobs-- > 0) { 1810 struct hf_hash_usb op_hash_data; 1811 struct work *work; 1812 uint64_t intdiff; 1813 int i, sequence; 1814 uint32_t *p; 1815 1816 /* This is a blocking function if there's no work */ 1817 if (!base_work) 1818 base_work = get_work(thr, thr->id); 1819 1820 /* HFA hardware actually had ntime rolling disabled so we 1821 * can roll the work ourselves here to minimise the amount of 1822 * work we need to generate. */ 1823 if (base_work->drv_rolllimit > jobs) { 1824 base_work->drv_rolllimit--; 1825 roll_work(base_work); 1826 work = make_clone(base_work); 1827 } else { 1828 work = base_work; 1829 base_work = NULL; 1830 } 1831 1832 /* Assemble the data frame and send the OP_HASH packet */ 1833 memcpy(op_hash_data.midstate, work->midstate, sizeof(op_hash_data.midstate)); 1834 memcpy(op_hash_data.merkle_residual, work->data + 64, 4); 1835 p = (uint32_t *)(work->data + 64 + 4); 1836 op_hash_data.timestamp = *p++; 1837 op_hash_data.bits = *p++; 1838 op_hash_data.starting_nonce = 0; 1839 op_hash_data.nonce_loops = 0; 1840 op_hash_data.ntime_loops = 0; 1841 1842 /* Set the number of leading zeroes to look for based on diff. 1843 * Diff 1 = 32, Diff 2 = 33, Diff 4 = 34 etc. */ 1844 intdiff = (uint64_t)work->device_diff; 1845 for (i = 31; intdiff; i++, intdiff >>= 1); 1846 op_hash_data.search_difficulty = i; 1847 op_hash_data.group = 0; 1848 if ((sequence = info->hash_sequence_head + 1) >= info->num_sequence) 1849 sequence = 0; 1850 ret = hfa_send_frame(hashfast, OP_HASH, sequence, (uint8_t *)&op_hash_data, sizeof(op_hash_data)); 1851 if (unlikely(!ret)) { 1852 free_work(work); 1853 if (base_work) 1854 free_work(base_work); 1855 hfa_running_shutdown(hashfast, info); 1856 return -1; 1857 } 1858 1859 mutex_lock(&info->lock); 1860 info->hash_sequence_head = sequence; 1861 info->works[info->hash_sequence_head] = work; 1862 mutex_unlock(&info->lock); 1863 1864 applog(LOG_DEBUG, "%s %s: OP_HASH sequence %d search_difficulty %d work_difficulty %g", 1865 hashfast->drv->name, hashfast->unique_id, info->hash_sequence_head, 1866 op_hash_data.search_difficulty, work->work_difficulty); 1867 } 1868 1869 if (base_work) 1870 free_work(base_work); 1871 1872 /* Only count 2/3 of the hashes to smooth out the hashrate for cycles 1873 * that have no hashes added. */ 1874 mutex_lock(&info->lock); 1875 hashes = info->hash_count / 3 * 2; 1876 info->calc_hashes += hashes; 1877 info->hash_count -= hashes; 1878 mutex_unlock(&info->lock); 1879 1880 return hashes; 1881 } 1882 1883 static struct api_data *hfa_api_stats(struct cgpu_info *cgpu) 1884 { 1885 struct hashfast_info *info; 1886 struct hf_long_usb_stats1 *s1; 1887 struct api_data *root = NULL; 1888 struct hf_usb_init_base *db; 1889 int varint, i; 1890 char buf[64]; 1891 1892 info = cgpu->device_data; 1893 if (!info) 1894 return NULL; 1895 1896 root = api_add_int(root, "asic count", &info->asic_count, false); 1897 root = api_add_int(root, "core count", &info->core_count, false); 1898 1899 root = api_add_double(root, "firmware rev", &info->firmware_version, false); 1900 root = api_add_double(root, "hardware rev", &info->hardware_version, false); 1901 db = &info->usb_init_base; 1902 root = api_add_hex32(root, "serial number", &db->serial_number, true); 1903 varint = db->hash_clockrate; 1904 root = api_add_int(root, "base clockrate", &varint, true); 1905 varint = db->inflight_target; 1906 root = api_add_int(root, "inflight target", &varint, true); 1907 varint = db->sequence_modulus; 1908 root = api_add_int(root, "sequence modulus", &varint, true); 1909 root = api_add_int(root, "fan percent", &info->fanspeed, false); 1910 if (info->op_name[0] != '\0') 1911 root = api_add_string(root, "op name", info->op_name, false); 1912 1913 s1 = &info->stats1; 1914 root = api_add_uint64(root, "rx preambles", &s1->usb_rx_preambles, false); 1915 root = api_add_uint64(root, "rx rcv byte err", &s1->usb_rx_receive_byte_errors, false); 1916 root = api_add_uint64(root, "rx bad hcrc", &s1->usb_rx_bad_hcrc, false); 1917 root = api_add_uint64(root, "tx attempts", &s1->usb_tx_attempts, false); 1918 root = api_add_uint64(root, "tx packets", &s1->usb_tx_packets, false); 1919 root = api_add_uint64(root, "tx incompletes", &s1->usb_tx_incompletes, false); 1920 root = api_add_uint64(root, "tx ep stalled", &s1->usb_tx_endpointstalled, false); 1921 root = api_add_uint64(root, "tx disconnect", &s1->usb_tx_disconnected, false); 1922 root = api_add_uint64(root, "tx suspend", &s1->usb_tx_suspended, false); 1923 varint = s1->max_tx_buffers; 1924 root = api_add_int(root, "max tx buf", &varint, true); 1925 varint = s1->max_rx_buffers; 1926 root = api_add_int(root, "max rx buf", &varint, true); 1927 1928 for (i = 0; i < info->asic_count; i++) { 1929 struct hf_long_statistics *l; 1930 struct hf_g1_die_data *d; 1931 char which[16]; 1932 double val; 1933 int j; 1934 1935 if (!info->die_statistics || !info->die_status) 1936 continue; 1937 l = &info->die_statistics[i]; 1938 if (!l) 1939 continue; 1940 d = &info->die_status[i]; 1941 if (!d) 1942 continue; 1943 snprintf(which, sizeof(which), "Asic%d", i); 1944 1945 snprintf(buf, sizeof(buf), "%s hash clockrate", which); 1946 root = api_add_int(root, buf, &(info->die_data[i].hash_clock), false); 1947 snprintf(buf, sizeof(buf), "%s die temperature", which); 1948 val = GN_DIE_TEMPERATURE(d->die.die_temperature); 1949 root = api_add_double(root, buf, &val, true); 1950 snprintf(buf, sizeof(buf), "%s board temperature", which); 1951 val = board_temperature(d->temperature); 1952 root = api_add_double(root, buf, &val, true); 1953 for (j = 0; j < 6; j++) { 1954 snprintf(buf, sizeof(buf), "%s voltage %d", which, j); 1955 val = GN_CORE_VOLTAGE(d->die.core_voltage[j]); 1956 root = api_add_utility(root, buf, &val, true); 1957 } 1958 snprintf(buf, sizeof(buf), "%s rx header crc", which); 1959 root = api_add_uint64(root, buf, &l->rx_header_crc, false); 1960 snprintf(buf, sizeof(buf), "%s rx body crc", which); 1961 root = api_add_uint64(root, buf, &l->rx_body_crc, false); 1962 snprintf(buf, sizeof(buf), "%s rx header to", which); 1963 root = api_add_uint64(root, buf, &l->rx_header_timeouts, false); 1964 snprintf(buf, sizeof(buf), "%s rx body to", which); 1965 root = api_add_uint64(root, buf, &l->rx_body_timeouts, false); 1966 snprintf(buf, sizeof(buf), "%s cn fifo full", which); 1967 root = api_add_uint64(root, buf, &l->core_nonce_fifo_full, false); 1968 snprintf(buf, sizeof(buf), "%s an fifo full", which); 1969 root = api_add_uint64(root, buf, &l->array_nonce_fifo_full, false); 1970 snprintf(buf, sizeof(buf), "%s stats overrun", which); 1971 root = api_add_uint64(root, buf, &l->stats_overrun, false); 1972 } 1973 1974 root = api_add_uint64(root, "raw hashcount", &info->raw_hashes, false); 1975 root = api_add_uint64(root, "calc hashcount", &info->calc_hashes, false); 1976 root = api_add_int(root, "no matching work", &info->no_matching_work, false); 1977 root = api_add_uint16(root, "shed count", &info->shed_count, false); 1978 root = api_add_int(root, "resets", &info->resets, false); 1979 1980 return root; 1981 } 1982 1983 static void hfa_statline_before(char *buf, size_t bufsiz, struct cgpu_info *hashfast) 1984 { 1985 struct hashfast_info *info; 1986 struct hf_g1_die_data *d; 1987 double max_volt; 1988 int i; 1989 1990 if (!hashfast->device_data) 1991 return; 1992 info = hashfast->device_data; 1993 /* Can happen during init sequence */ 1994 if (!info->die_status) 1995 return; 1996 max_volt = 0.0; 1997 1998 for (i = 0; i < info->asic_count; i++) { 1999 int j; 2000 2001 d = &info->die_status[i]; 2002 for (j = 0; j < 6; j++) { 2003 double volt = GN_CORE_VOLTAGE(d->die.core_voltage[j]); 2004 2005 if (volt > max_volt) 2006 max_volt = volt; 2007 } 2008 } 2009 2010 tailsprintf(buf, bufsiz, "%3dMHz %3.0fC %3d%% %3.2fV", info->base_clock, 2011 hashfast->temp, info->fanspeed, max_volt); 2012 } 2013 2014 /* We cannot re-initialise so just shut down the device for it to hotplug 2015 * again. */ 2016 static void hfa_reinit(struct cgpu_info *hashfast) 2017 { 2018 if (hashfast && hashfast->device_data) 2019 hfa_running_shutdown(hashfast, hashfast->device_data); 2020 } 2021 2022 static void hfa_free_all_work(struct hashfast_info *info) 2023 { 2024 while (info->device_sequence_tail != info->hash_sequence_head) { 2025 struct work *work; 2026 2027 if (++info->hash_sequence_tail >= info->num_sequence) 2028 info->hash_sequence_tail = 0; 2029 if (unlikely(!(work = info->works[info->hash_sequence_tail]))) 2030 break; 2031 free_work(work); 2032 info->works[info->hash_sequence_tail] = NULL; 2033 } 2034 } 2035 2036 static void hfa_shutdown(struct thr_info *thr) 2037 { 2038 struct cgpu_info *hashfast = thr->cgpu; 2039 struct hashfast_info *info = hashfast->device_data; 2040 2041 hfa_send_shutdown(hashfast); 2042 pthread_join(info->read_thr, NULL); 2043 hfa_free_all_work(info); 2044 hfa_clear_readbuf(hashfast); 2045 free(info->works); 2046 free(info->die_statistics); 2047 free(info->die_status); 2048 free(info->die_data); 2049 /* Keep the device data intact to allow new instances to match old 2050 * ones. */ 2051 } 2052 2053 struct device_drv hashfast_drv = { 2054 .drv_id = DRIVER_hashfast, 2055 .dname = "Hashfast", 2056 .name = "HFA", 2057 .max_diff = 32.0, // Limit max diff to get some nonces back regardless 2058 .drv_detect = hfa_detect, 2059 .thread_init = hfa_init, 2060 .hash_work = &hash_driver_work, 2061 .scanwork = hfa_scanwork, 2062 .get_api_stats = hfa_api_stats, 2063 .get_statline_before = hfa_statline_before, 2064 .reinit_device = hfa_reinit, 2065 .thread_shutdown = hfa_shutdown, 2066 };