static void hfa_set_fanspeed(struct cgpu_info *hashfast, struct hashfast_info *info, int fandiff) { const uint8_t opcode = HF_USB_CMD(OP_FAN); uint8_t packet[256]; struct hf_header *p = (struct hf_header *)packet; const int tx_length = sizeof(struct hf_header); uint16_t hdata; int fandata; info->fanspeed += fandiff; if (info->fanspeed > opt_hfa_fan_max) info->fanspeed = opt_hfa_fan_max; else if (info->fanspeed < opt_hfa_fan_min) info->fanspeed = opt_hfa_fan_min; fandata = info->fanspeed * 255 / 100; // Fanspeed is in percent, hdata 0-255 hdata = fandata; // Use an int first to avoid overflowing uint16_t p->preamble = HF_PREAMBLE; p->operation_code = hfa_cmds[opcode].cmd; p->chip_address = 0xff; p->core_address = 1; p->hdata = htole16(hdata); p->data_length = 0; p->crc8 = hfa_crc8(packet); __hfa_send_frame(hashfast, opcode, tx_length, packet); }
static void *hfa_read(void *arg) { struct thr_info *thr = (struct thr_info *)arg; struct cgpu_info *hashfast = thr->cgpu; struct hashfast_info *info = hashfast->device_data; char threadname[16]; snprintf(threadname, sizeof(threadname), "%d/%sRead", hashfast->device_id, hashfast->drv->name); RenameThread(threadname); while (likely(!hashfast->shutdown)) { char buf[512]; struct hf_header *h = (struct hf_header *)buf; bool ret = hfa_get_packet(hashfast, h); if (unlikely(hashfast->usbinfo.nodev)) break; if (unlikely(!ret)) continue; switch (h->operation_code) { case OP_GWQ_STATUS: hfa_parse_gwq_status(hashfast, info, h); break; case OP_DIE_STATUS: hfa_update_die_status(hashfast, info, h); break; case OP_NONCE: hfa_parse_nonce(thr, hashfast, info, h); break; case OP_STATISTICS: hfa_update_die_statistics(info, h); break; case OP_USB_STATS1: hfa_update_stats1(hashfast, info, h); break; case OP_USB_NOTICE: hfa_parse_notice(hashfast, h); break; case OP_PING: /* Do nothing */ break; default: applog(LOG_WARNING, "%s %d: Unhandled operation code %d", hashfast->drv->name, hashfast->device_id, h->operation_code); break; } /* Make sure we send something to the device at least every 5 * seconds so it knows the driver is still alive for when we * run out of work. The read thread never blocks so is the * best place to do this. */ if (time(NULL) - info->last_send > 5) hfa_send_frame(hashfast, HF_USB_CMD(OP_PING), 0, NULL, 0); } applog(LOG_DEBUG, "%s %d: Shutting down read thread", hashfast->drv->name, hashfast->device_id); return NULL; }
static void hfa_dfu_boot(struct cgpu_info *hashfast) { bool ret; ret = hfa_send_frame(hashfast, HF_USB_CMD(OP_DFU), 0, NULL, 0); applog(LOG_WARNING, "HFA %d %03d:%03d DFU Boot %s", hashfast->device_id, hashfast->usbinfo.bus_number, hashfast->usbinfo.device_address, ret ? "Succeeded" : "Failed"); }
static void hfa_decrease_clock(struct cgpu_info *hashfast, struct hashfast_info *info, int die) { struct hf_die_data *hdd = &info->die_data[die]; uint32_t diebit = 0x00000001ul << die; uint16_t hdata, decrease = 10; if (hdd->hash_clock - decrease < HFA_CLOCK_MIN) decrease = hdd->hash_clock - HFA_CLOCK_MIN; hdd->hash_clock -= decrease; applog(LOG_INFO, "%s %d: Die temp above range %.1f, decreasing die %d clock to %d", hashfast->drv->name, hashfast->device_id, info->die_data[die].temp, die, hdd->hash_clock); hdata = (WR_MHZ_DECREASE << 12) | decrease; hfa_send_frame(hashfast, HF_USB_CMD(OP_WORK_RESTART), hdata, (uint8_t *)&diebit, 4); }
static void hfa_increase_clock(struct cgpu_info *hashfast, struct hashfast_info *info, int die) { struct hf_die_data *hdd = &info->die_data[die]; uint32_t diebit = 0x00000001ul << die; uint16_t hdata, increase = 10; if (hdd->hash_clock + increase > info->hash_clock_rate) increase = info->hash_clock_rate - hdd->hash_clock; hdd->hash_clock += increase; applog(LOG_INFO, "%s %d: Die temp below range %.1f, increasing die %d clock to %d", hashfast->drv->name, hashfast->device_id, info->die_data[die].temp, die, hdd->hash_clock); hdata = (WR_MHZ_INCREASE << 12) | increase; hfa_send_frame(hashfast, HF_USB_CMD(OP_WORK_RESTART), hdata, (uint8_t *)&diebit, 4); }
static int64_t hfa_scanwork(struct thr_info *thr) { struct cgpu_info *hashfast = thr->cgpu; struct hashfast_info *info = hashfast->device_data; int64_t hashes; int jobs, ret; if (unlikely(hashfast->usbinfo.nodev)) { applog(LOG_WARNING, "HFA %d: device disappeared, disabling", hashfast->device_id); return -1; } if (unlikely(thr->work_restart)) { restart: thr->work_restart = false; ret = hfa_send_frame(hashfast, HF_USB_CMD(OP_WORK_RESTART), 0, (uint8_t *)NULL, 0); if (unlikely(!ret)) { ret = hfa_reset(hashfast, info); if (unlikely(!ret)) { applog(LOG_ERR, "HFA %d: Failed to reset after write failure, disabling", hashfast->device_id); return -1; } } } jobs = hfa_jobs(info); if (!jobs) { ret = restart_wait(thr, 100); if (unlikely(!ret)) goto restart; jobs = hfa_jobs(info); } if (jobs) { applog(LOG_DEBUG, "HFA %d: Sending %d new jobs", hashfast->device_id, jobs); } while (jobs-- > 0) { struct hf_hash_usb op_hash_data; struct work *work; uint64_t intdiff; int i, sequence; uint32_t *p; /* This is a blocking function if there's no work */ work = get_work(thr, thr->id); /* Assemble the data frame and send the OP_HASH packet */ memcpy(op_hash_data.midstate, work->midstate, sizeof(op_hash_data.midstate)); memcpy(op_hash_data.merkle_residual, work->data + 64, 4); p = (uint32_t *)(work->data + 64 + 4); op_hash_data.timestamp = *p++; op_hash_data.bits = *p++; op_hash_data.starting_nonce = 0; op_hash_data.nonce_loops = 0; op_hash_data.ntime_loops = 0; /* Set the number of leading zeroes to look for based on diff. * Diff 1 = 32, Diff 2 = 33, Diff 4 = 34 etc. */ intdiff = (uint64_t)work->device_diff; for (i = 31; intdiff; i++, intdiff >>= 1); op_hash_data.search_difficulty = i; op_hash_data.group = 0; if ((sequence = info->hash_sequence_head + 1) >= info->num_sequence) sequence = 0; ret = hfa_send_frame(hashfast, OP_HASH, sequence, (uint8_t *)&op_hash_data, sizeof(op_hash_data)); if (unlikely(!ret)) { ret = hfa_reset(hashfast, info); if (unlikely(!ret)) { applog(LOG_ERR, "HFA %d: Failed to reset after write failure, disabling", hashfast->device_id); return -1; } } mutex_lock(&info->lock); info->hash_sequence_head = sequence; info->works[info->hash_sequence_head] = work; mutex_unlock(&info->lock); applog(LOG_DEBUG, "HFA %d: OP_HASH sequence %d search_difficulty %d work_difficulty %g", hashfast->device_id, info->hash_sequence_head, op_hash_data.search_difficulty, work->work_difficulty); } mutex_lock(&info->lock); hashes = info->hash_count; info->hash_count = 0; mutex_unlock(&info->lock); return hashes; }
static void hfa_send_shutdown(struct cgpu_info *hashfast) { hfa_send_frame(hashfast, HF_USB_CMD(OP_USB_SHUTDOWN), 0, NULL, 0); }
static bool hfa_reset(struct cgpu_info *hashfast, struct hashfast_info *info) { struct hf_usb_init_header usb_init[2], *hu = usb_init; struct hf_usb_init_base *db; struct hf_usb_init_options *ho; int retries = 0, i; char buf[1024]; struct hf_header *h = (struct hf_header *)buf; uint8_t hcrc; bool ret; /* Hash clock rate in Mhz */ info->hash_clock_rate = opt_hfa_hash_clock ? opt_hfa_hash_clock : 550; info->group_ntime_roll = opt_hfa_ntime_roll ? opt_hfa_ntime_roll : 1; info->core_ntime_roll = 1; // Assemble the USB_INIT request memset(hu, 0, sizeof(*hu)); hu->preamble = HF_PREAMBLE; hu->operation_code = OP_USB_INIT; hu->protocol = PROTOCOL_GLOBAL_WORK_QUEUE; // Protocol to use // Force PLL bypass hu->pll_bypass = opt_hfa_pll_bypass; hu->hash_clock = info->hash_clock_rate; // Hash clock rate in Mhz if (info->group_ntime_roll > 1 && info->core_ntime_roll) { ho = (struct hf_usb_init_options *)(hu + 1); memset(ho, 0, sizeof(*ho)); ho->group_ntime_roll = info->group_ntime_roll; ho->core_ntime_roll = info->core_ntime_roll; hu->data_length = sizeof(*ho) / 4; } hu->crc8 = hfa_crc8((uint8_t *)hu); applog(LOG_INFO, "HFA%d: Sending OP_USB_INIT with GWQ protocol specified", hashfast->device_id); if (!hfa_send_packet(hashfast, (struct hf_header *)hu, HF_USB_CMD(OP_USB_INIT))) return false; // Check for the correct response. // We extend the normal timeout - a complete device initialization, including // bringing power supplies up from standby, etc., can take over a second. tryagain: for (i = 0; i < 30; i++) { ret = hfa_get_header(hashfast, h, &hcrc); if (ret) break; } if (!ret) { applog(LOG_WARNING, "HFA %d: OP_USB_INIT failed!", hashfast->device_id); return false; } if (h->crc8 != hcrc) { applog(LOG_WARNING, "HFA %d: OP_USB_INIT failed! CRC mismatch", hashfast->device_id); return false; } if (h->operation_code != OP_USB_INIT) { // This can happen if valid packet(s) were in transit *before* the OP_USB_INIT arrived // at the device, so we just toss the packets and keep looking for the response. applog(LOG_WARNING, "HFA %d: OP_USB_INIT: Tossing packet, valid but unexpected type %d", hashfast->device_id, h->operation_code); hfa_get_data(hashfast, buf, h->data_length); if (retries++ < 3) goto tryagain; return false; } applog(LOG_DEBUG, "HFA %d: Good reply to OP_USB_INIT", hashfast->device_id); applog(LOG_DEBUG, "HFA %d: OP_USB_INIT: %d die in chain, %d cores, device_type %d, refclk %d Mhz", hashfast->device_id, h->chip_address, h->core_address, h->hdata & 0xff, (h->hdata >> 8) & 0xff); // Save device configuration info->asic_count = h->chip_address; info->core_count = h->core_address; info->device_type = (uint8_t)h->hdata; info->ref_frequency = (uint8_t)(h->hdata >> 8); info->hash_sequence_head = 0; info->hash_sequence_tail = 0; info->device_sequence_tail = 0; // Size in bytes of the core bitmap in bytes info->core_bitmap_size = (((info->asic_count * info->core_count) + 31) / 32) * 4; // Get the usb_init_base structure if (!hfa_get_data(hashfast, (char *)&info->usb_init_base, U32SIZE(info->usb_init_base))) { applog(LOG_WARNING, "HFA %d: OP_USB_INIT failed! Failure to get usb_init_base data", hashfast->device_id); return false; } db = &info->usb_init_base; applog(LOG_INFO, "HFA %d: firmware_rev: %d.%d", hashfast->device_id, (db->firmware_rev >> 8) & 0xff, db->firmware_rev & 0xff); applog(LOG_INFO, "HFA %d: hardware_rev: %d.%d", hashfast->device_id, (db->hardware_rev >> 8) & 0xff, db->hardware_rev & 0xff); applog(LOG_INFO, "HFA %d: serial number: %d", hashfast->device_id, db->serial_number); applog(LOG_INFO, "HFA %d: hash clockrate: %d Mhz", hashfast->device_id, db->hash_clockrate); applog(LOG_INFO, "HFA %d: inflight_target: %d", hashfast->device_id, db->inflight_target); applog(LOG_INFO, "HFA %d: sequence_modulus: %d", hashfast->device_id, db->sequence_modulus); info->num_sequence = db->sequence_modulus; // Now a copy of the config data used if (!hfa_get_data(hashfast, (char *)&info->config_data, U32SIZE(info->config_data))) { applog(LOG_WARNING, "HFA %d: OP_USB_INIT failed! Failure to get config_data", hashfast->device_id); return false; } // Now the core bitmap info->core_bitmap = malloc(info->core_bitmap_size); if (!info->core_bitmap) quit(1, "Failed to malloc info core bitmap in hfa_reset"); if (!hfa_get_data(hashfast, (char *)info->core_bitmap, info->core_bitmap_size / 4)) { applog(LOG_WARNING, "HFA %d: OP_USB_INIT failed! Failure to get core_bitmap", hashfast->device_id); return false; } // See if the initialization suceeded if (db->operation_status) { applog(LOG_WARNING, "HFA %d: OP_USB_INIT failed! Operation status %d (%s)", hashfast->device_id, db->operation_status, (db->operation_status < sizeof(hf_usb_init_errors)/sizeof(hf_usb_init_errors[0])) ? hf_usb_init_errors[db->operation_status] : "Unknown error code"); return false; } return true; }
static void hfa_send_shutdown(struct cgpu_info *hashfast) { if (hashfast->usbinfo.nodev) return; hfa_send_frame(hashfast, HF_USB_CMD(OP_USB_SHUTDOWN), 0, NULL, 0); }
static int64_t hfa_scanwork(struct thr_info *thr) { struct cgpu_info *hashfast = thr->cgpu; struct hashfast_info *info = hashfast->device_data; int jobs, ret, cycles = 0; int64_t hashes; if (unlikely(hashfast->usbinfo.nodev)) { applog(LOG_WARNING, "%s %d: device disappeared, disabling", hashfast->drv->name, hashfast->device_id); return -1; } if (unlikely(last_getwork - hashfast->last_device_valid_work > 60)) { applog(LOG_WARNING, "%s %d: No valid hashes for over 1 minute, attempting to reset", hashfast->drv->name, hashfast->device_id); if (info->hash_clock_rate > HFA_CLOCK_DEFAULT) { info->hash_clock_rate -= 5; if (info->hash_clock_rate < opt_hfa_hash_clock) opt_hfa_hash_clock = info->hash_clock_rate; applog(LOG_WARNING, "%s %d: Decreasing clock speed to %d with reset", hashfast->drv->name, hashfast->device_id, info->hash_clock_rate); } ret = hfa_reset(hashfast, info); if (!ret) { applog(LOG_ERR, "%s %d: Failed to reset after hash failure, disabling", hashfast->drv->name, hashfast->device_id); return -1; } applog(LOG_NOTICE, "%s %d: Reset successful", hashfast->drv->name, hashfast->device_id); } if (unlikely(thr->work_restart)) { restart: info->last_restart = time(NULL); thr->work_restart = false; ret = hfa_send_frame(hashfast, HF_USB_CMD(OP_WORK_RESTART), 0, (uint8_t *)NULL, 0); if (unlikely(!ret)) { ret = hfa_reset(hashfast, info); if (unlikely(!ret)) { applog(LOG_ERR, "%s %d: Failed to reset after write failure, disabling", hashfast->drv->name, hashfast->device_id); return -1; } } /* Give a full allotment of jobs after a restart, not waiting * for the status update telling us how much to give. */ jobs = info->usb_init_base.inflight_target; } else { /* Only adjust die clocks if there's no restart since two * restarts back to back get ignored. */ hfa_temp_clock(hashfast, info); jobs = hfa_jobs(hashfast, info); } /* Wait on restart_wait for up to 0.5 seconds or submit jobs as soon as * they're required. */ while (!jobs && ++cycles < 5) { ret = restart_wait(thr, 100); if (unlikely(!ret)) goto restart; jobs = hfa_jobs(hashfast, info); } if (jobs) { applog(LOG_DEBUG, "%s %d: Sending %d new jobs", hashfast->drv->name, hashfast->device_id, jobs); } while (jobs-- > 0) { struct hf_hash_usb op_hash_data; struct work *work; uint64_t intdiff; int i, sequence; uint32_t *p; /* This is a blocking function if there's no work */ work = get_work(thr, thr->id); /* Assemble the data frame and send the OP_HASH packet */ memcpy(op_hash_data.midstate, work->midstate, sizeof(op_hash_data.midstate)); memcpy(op_hash_data.merkle_residual, work->data + 64, 4); p = (uint32_t *)(work->data + 64 + 4); op_hash_data.timestamp = *p++; op_hash_data.bits = *p++; op_hash_data.starting_nonce = 0; op_hash_data.nonce_loops = 0; op_hash_data.ntime_loops = 0; /* Set the number of leading zeroes to look for based on diff. * Diff 1 = 32, Diff 2 = 33, Diff 4 = 34 etc. */ intdiff = (uint64_t)work->device_diff; for (i = 31; intdiff; i++, intdiff >>= 1); op_hash_data.search_difficulty = i; op_hash_data.group = 0; if ((sequence = info->hash_sequence_head + 1) >= info->num_sequence) sequence = 0; ret = hfa_send_frame(hashfast, OP_HASH, sequence, (uint8_t *)&op_hash_data, sizeof(op_hash_data)); if (unlikely(!ret)) { ret = hfa_reset(hashfast, info); if (unlikely(!ret)) { applog(LOG_ERR, "%s %d: Failed to reset after write failure, disabling", hashfast->drv->name, hashfast->device_id); return -1; } } mutex_lock(&info->lock); info->hash_sequence_head = sequence; info->works[info->hash_sequence_head] = work; mutex_unlock(&info->lock); applog(LOG_DEBUG, "%s %d: OP_HASH sequence %d search_difficulty %d work_difficulty %g", hashfast->drv->name, hashfast->device_id, info->hash_sequence_head, op_hash_data.search_difficulty, work->work_difficulty); } /* Only count 2/3 of the hashes to smooth out the hashrate for cycles * that have no hashes added. */ mutex_lock(&info->lock); hashes = info->hash_count / 3 * 2; info->calc_hashes += hashes; info->hash_count -= hashes; mutex_unlock(&info->lock); return hashes; }