static bool hfa_detect_common(struct cgpu_info *hashfast) { struct hashfast_info *info; bool ret; info = calloc(sizeof(struct hashfast_info), 1); if (!info) quit(1, "Failed to calloc hashfast_info in hfa_detect_common"); hashfast->device_data = info; /* hashfast_reset should fill in details for info */ ret = hfa_reset(hashfast, info); if (!ret) { hfa_send_shutdown(hashfast); hfa_clear_readbuf(hashfast); free(info); hashfast->device_data = NULL; return false; } // The per-die status array info->die_status = calloc(info->asic_count, sizeof(struct hf_g1_die_data)); if (unlikely(!(info->die_status))) quit(1, "Failed to calloc die_status"); // The per-die statistics array info->die_statistics = calloc(info->asic_count, sizeof(struct hf_long_statistics)); if (unlikely(!(info->die_statistics))) quit(1, "Failed to calloc die_statistics"); info->works = calloc(sizeof(struct work *), info->num_sequence); if (!info->works) quit(1, "Failed to calloc info works in hfa_detect_common"); return true; }
static void hfa_parse_gwq_status(struct cgpu_info *hashfast, struct hashfast_info *info, struct hf_header *h) { struct hf_gwq_data *g = (struct hf_gwq_data *)(h + 1); struct work *work; applog(LOG_DEBUG, "HFA %d: OP_GWQ_STATUS, device_head %4d tail %4d my tail %4d shed %3d inflight %4d", hashfast->device_id, g->sequence_head, g->sequence_tail, info->hash_sequence_tail, g->shed_count, HF_SEQUENCE_DISTANCE(info->hash_sequence_head,g->sequence_tail)); /* This is a special flag that the thermal overload has been tripped */ if (unlikely(h->core_address & 0x80)) { applog(LOG_WARNING, "HFA %d Thermal overload tripped! Resetting device", hashfast->device_id); hfa_send_shutdown(hashfast); if (hfa_reset(hashfast, info)) { applog(LOG_NOTICE, "HFA %d: Succesfully reset, continuing operation", hashfast->device_id); return; } applog(LOG_WARNING, "HFA %d Failed to reset device, killing off thread to allow re-hotplug", hashfast->device_id); usb_nodev(hashfast); return; } mutex_lock(&info->lock); info->hash_count += g->hash_count; info->device_sequence_head = g->sequence_head; info->device_sequence_tail = g->sequence_tail; info->shed_count = g->shed_count; /* Free any work that is no longer required */ while (info->device_sequence_tail != info->hash_sequence_tail) { if (++info->hash_sequence_tail >= info->num_sequence) info->hash_sequence_tail = 0; if (unlikely(!(work = info->works[info->hash_sequence_tail]))) { applog(LOG_ERR, "HFA %d: Bad work sequence tail", hashfast->device_id); hashfast->shutdown = true; break; } applog(LOG_DEBUG, "HFA %d: Completing work on hash_sequence_tail %d", hashfast->device_id, info->hash_sequence_tail); free_work(work); info->works[info->hash_sequence_tail] = NULL; } mutex_unlock(&info->lock); }
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 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; }