static bool hfa_get_header(struct cgpu_info *hashfast, struct hf_header *h, uint8_t *computed_crc) { int amount, ret, orig_len, len, ofs = 0; cgtimer_t ts_start; char buf[512]; char *header; orig_len = len = sizeof(*h); /* Read for up to 200ms till we find the first occurrence of HF_PREAMBLE * though it should be the first byte unless we get woefully out of * sync. */ cgtimer_time(&ts_start); do { cgtimer_t ts_now, ts_diff; cgtimer_time(&ts_now); cgtimer_sub(&ts_now, &ts_start, &ts_diff); if (cgtimer_to_ms(&ts_diff) > 200) return false; ret = usb_read(hashfast, buf + ofs, len, &amount, C_HF_GETHEADER); if (unlikely(ret && ret != LIBUSB_ERROR_TIMEOUT)) return false; ofs += amount; header = memchr(buf, HF_PREAMBLE, ofs); if (header) len -= ofs - (header - buf); } while (len); memcpy(h, header, orig_len); *computed_crc = hfa_crc8((uint8_t *)h); return true; }
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 bool hfa_send_frame(struct cgpu_info *hashfast, uint8_t opcode, uint16_t hdata, uint8_t *data, int len) { int tx_length, ret, amount, id = hashfast->device_id; uint8_t packet[256]; struct hf_header *p = (struct hf_header *)packet; p->preamble = HF_PREAMBLE; p->operation_code = hfa_cmds[opcode].cmd; p->chip_address = HF_GWQ_ADDRESS; p->core_address = 0; p->hdata = htole16(hdata); p->data_length = len / 4; p->crc8 = hfa_crc8(packet); if (len) memcpy(&packet[sizeof(struct hf_header)], data, len); tx_length = sizeof(struct hf_header) + len; applog(LOG_DEBUG, "HFA %d: Sending %s frame", hashfast->device_id, hfa_cmds[opcode].cmd_name); ret = usb_write(hashfast, (char *)packet, tx_length, &amount, hfa_cmds[opcode].usb_cmd); if (unlikely(ret < 0 || amount != tx_length)) { applog(LOG_ERR, "HFA %d: hfa_send_frame: USB Send error, ret %d amount %d vs. tx_length %d", id, ret, amount, tx_length); return false; } return true; }
int main(int argc, char *argv[]) { struct rw_usb_state s; struct hf_header a; int rslt; int sent; /* Just needs to run once. */ hfa_init_crc8(); set_up_rw_usb(&s); memset(&a, 0, sizeof(a)); a.preamble = HF_PREAMBLE; a.operation_code = OP_DFU; a.chip_address = 0x00; a.core_address = 0; a.hdata = 0; a.data_length = 0; a.crc8 = hfa_crc8((unsigned char *) &a); rslt = libusb_bulk_transfer(s.handle, s.send_endpoint->bEndpointAddress, (unsigned char *) &a, 8, &sent, TIMEOUT); if(rslt != 0) { fprintf(stderr, "libusb_bulk_transfer() failed while sending: %s\n", libusb_strerror(rslt)); exit(1); } /* Fix: libusb_bulk_transfer() is not required to send everything at once. We expect it to * here because of the short size, but it is not guaranteed. This should be fine for * the moment, be we should replace it with a generic sending routine that sends * everything once it is written. */ if(sent != 8) { fprintf(stderr, "libusb_bulk_transfer() was asked to send %ld bytes and only sent %d.\n", (long) sizeof(a), sent); exit(1); } shut_down_rw_usb(&s); return 0; }
static bool hfa_send_frame(struct cgpu_info *hashfast, uint8_t opcode, uint16_t hdata, uint8_t *data, int len) { uint8_t packet[256]; struct hf_header *p = (struct hf_header *)packet; int tx_length; p->preamble = HF_PREAMBLE; p->operation_code = hfa_cmds[opcode].cmd; p->chip_address = HF_GWQ_ADDRESS; p->core_address = 0; p->hdata = htole16(hdata); p->data_length = len / 4; p->crc8 = hfa_crc8(packet); if (len) memcpy(&packet[sizeof(struct hf_header)], data, len); tx_length = sizeof(struct hf_header) + len; return (__hfa_send_frame(hashfast, opcode, tx_length, packet)); }
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; }
int main(int argc, char *argv[]) { struct rw_usb_state s; unsigned char a[64]; struct hf_header *a_ptr; int rslt; int sent; int received; unsigned char b[64]; hfparseOptsT parseOpts; hfparseT *parse; hfparseStatsT stats, oldStats; memset(&parseOpts, 0, sizeof(parseOpts)); parseOpts.includeDataCRC = 0; parseOpts.packet = packetRx; parse = hfparseCreate(&parseOpts); if (parse == NULL) { fprintf(stderr, "failed to create parser\n"); exit(1); } memset(&oldStats, 0, sizeof(oldStats)); /* Just needs to run once. */ hfa_init_crc8(); set_up_rw_usb(&s); memset(&a, 0, sizeof(a)); a_ptr = (struct hf_header *) a; a_ptr->preamble = HF_PREAMBLE; a_ptr->operation_code = OP_USB_INIT; a_ptr->chip_address = 0x00; a_ptr->core_address = 0x10 | 0x01; a_ptr->hdata = 0; a_ptr->data_length = 0; a_ptr->crc8 = hfa_crc8((unsigned char *) &a); rslt = libusb_bulk_transfer(s.handle, s.send_endpoint->bEndpointAddress, a, 8, &sent, 100); if(rslt != 0) { fprintf(stderr, "libusb_bulk_transfer() failed while sending: %s\n", libusb_strerror(rslt)); exit(1); } /* Fix: libusb_bulk_transfer() is not required to send everything at once. We expect it to * here because of the short size, but it is not guaranteed. This should be fine for * the moment, be we should replace it with a generic sending routine that sends * everything once it is written. */ if(sent != 8) { fprintf(stderr, "libusb_bulk_transfer() was asked to send %ld bytes and only sent %d.\n", (long) sizeof(a), sent); exit(1); } while (1) { rslt = libusb_bulk_transfer(s.handle, s.receive_endpoint->bEndpointAddress, b, 64, &received, 100); if (rslt == 0) { hfparseRun(parse, b, received); hfparseStats(parse, &stats); if (stats.syncLoss != oldStats.syncLoss) printf("sync loss %lu\n", stats.syncLoss); if (stats.bytesDiscarded != oldStats.bytesDiscarded) printf("bytes discarded %lu\n", stats.bytesDiscarded); memcpy(&oldStats, &stats, sizeof(oldStats)); } else if (rslt != LIBUSB_ERROR_TIMEOUT) { fprintf(stderr, "libusb_bulk_transfer returned %d\n", rslt); break; } } shut_down_rw_usb(&s); hfparseDestroy(parse); return 0; }
void report_version_info(const char *prefix, uint8_t board, uint8_t type) { struct rw_usb_state s; struct hf_header a; unsigned char a_send[64]; int rslt; int sent; int received; int i; unsigned char b[64]; struct hf_header *b_ptr; unsigned char output[BUFSIZE]; uint8_t len; set_up_rw_usb(&s); memset(&a, 0, sizeof(a)); a.preamble = 0xaa; /* Always this value. */ a.operation_code = OP_VERSION; a.chip_address = board; a.core_address = type; a.hdata = 0; a.data_length = 0; a.crc8 = hfa_crc8((unsigned char *) &a); memset(a_send, 0, sizeof(a_send)); memcpy(a_send, &a, 8); rslt = libusb_bulk_transfer(s.handle, s.send_endpoint->bEndpointAddress, a_send, (int) 8, &sent, 1000); if(rslt != 0) { fprintf(stderr, "libusb_bulk_transfer() failed while sending: %s\n", libusb_strerror(rslt)); exit(1); } /* Fix: libusb_bulk_transfer() is not required to send everything at once. We expect it to * here because of the short size, but it is not guaranteed. This should be fine for * the moment, be we should replace it with a generic sending routine that sends * everything once it is written. */ if(sent != 8) { fprintf(stderr, "libusb_bulk_transfer() was asked to send %ld bytes and only sent %d.\n", (long) sizeof(a), sent); exit(1); } /* Fix: libusb_bulk_transfer() seems to have to be called four times * no matter how much delay we introduce. This suggests that * the EVK board has some bug where needs four asks before it * will reply. */ for(received=0, i=0; received == 0 && i < 100; i++) { memset(b, 0, sizeof(b)); /* Fix: We may receive partial reads as libusb_bulk_transfer() is * not required to send everything at once. We expect it to * here because of the short size, but it is not guaranteed. * This should be fine for the moment, be we should replace * it with a generic sending routine that sends everything * once it is written. */ rslt = libusb_bulk_transfer(s.handle, s.receive_endpoint->bEndpointAddress, b, (int) sizeof(b), &received, 1000); if(rslt != 0) { fprintf(stderr, "libusb_bulk_transfer() failed while receiving: %s\n", libusb_strerror(rslt)); exit(1); } } if(received == 0) { fprintf(stderr, "Failed to receive a version reply.\n"); exit(1); } b_ptr = (struct hf_header *) b; if(b_ptr->crc8 != hfa_crc8(b)) { fprintf(stderr, "Bad CRC8 checksum on header: "); dump_header_and_data(b, received); fprintf(stderr, "\n"); exit(1); } if(b_ptr->core_address != 4) { if(b_ptr->preamble != 0xaa || b_ptr->operation_code != 142 || /* Fix: OP_VERSION */ b_ptr->chip_address != board || b_ptr->core_address != type || b_ptr->hdata != 0) { fprintf(stderr, "Unexpected header: "); dump_header_and_data(b, received); fprintf(stderr, "\n"); exit(1); } if(b_ptr->data_length*4 + 8 != received) { fprintf(stderr, "Got back packet with mismatched length. "); fprintf(stderr, "Received: %ld Expected: %ld\n", (long) received, (long) b_ptr->data_length*4 + 8); exit(1); } if(b_ptr->data_length > 0) { len = *(b + 8); /* Length byte of data. */ if(len > (4*b_ptr->data_length-1)) { fprintf(stderr, "Length byte exceeds available data space. "); fprintf(stderr, "Length byte: %ld Data space: %ld\n", (long) len, (long) 4*b_ptr->data_length-1); exit(1); } for(i=len + 1; i < b_ptr->data_length*4; i++) if(*(b + 8 + i) != '\0') { fprintf(stderr, "Extra data is not NUL.\n"); exit(1); } memset(output, 0, sizeof(output)); memcpy(output, b + 8 + 1, len); *(output + 8 + 1 + len) = '\0'; printf("%s: %s\n", prefix, output); } else { printf("%s, not defined\n", prefix); } } else { /* Fail return. */ if(b_ptr->crc8 != hfa_crc8(b)) { fprintf(stderr, "Bad CRC8 checksum on header: "); dump_header_and_data(b, received); fprintf(stderr, "\n"); exit(1); } if(b_ptr->preamble != 0xaa || b_ptr->operation_code != 142 || /* Fix: OP_VERSION */ b_ptr->chip_address != board || b_ptr->core_address != 4) { fprintf(stderr, "Unexpected fail header: "); dump_header_and_data(b, received); fprintf(stderr, "\n"); exit(1); } printf("%s, fail value %d\n", prefix, (int) b_ptr->hdata); } shut_down_rw_usb(&s); }
int main() { struct rw_usb_state s; struct hf_header a; int rslt; int sent; int received; int i; unsigned char b[64]; struct hf_header *b_ptr; int total_cores; int total_bits; /* Just needs to run once. */ hfa_init_crc8(); set_up_rw_usb(&s); memset(&a, 0, sizeof(a)); a.preamble = 0xaa; /* Always this value. */ a.operation_code = 141; /* OP_CORE_MAP */ /* Fix: Get from hf_protocol.h. */ a.chip_address = 0x00; a.core_address = 0; a.hdata = 0; a.data_length = 0; a.crc8 = hfa_crc8((unsigned char *) &a); rslt = libusb_bulk_transfer(s.handle, s.send_endpoint->bEndpointAddress, (unsigned char *) &a, (int) sizeof(a), &sent, 100); if(rslt != 0) { fprintf(stderr, "libusb_bulk_transfer() failed while sending: %s\n", libusb_strerror(rslt)); exit(1); } /* Fix: libusb_bulk_transfer() is not required to send everything at once. We expect it to * here because of the short size, but it is not guaranteed. This should be fine for * the moment, be we should replace it with a generic sending routine that sends * everything once it is written. */ if(sent != sizeof(a)) { fprintf(stderr, "libusb_bulk_transfer() was asked to send %ld bytes and only sent %d.\n", (long) sizeof(a), sent); exit(1); } /* Fix: libusb_bulk_transfer() seems to have to be called four times * no matter how much delay we introduce. This suggests that * the EVK board has some bug where needs four asks before it * will reply. */ for(received=0, i=0; received == 0 && i < 100; i++) { struct timespec aa; memset(b, 0, sizeof(b)); /* Fix: We may receive partial reads as libusb_bulk_transfer() is * not required to send everything at once. We expect it to * here because of the short size, but it is not guaranteed. * This should be fine for the moment, be we should replace * it with a generic sending routine that sends everything * once it is written. */ rslt = libusb_bulk_transfer(s.handle, s.receive_endpoint->bEndpointAddress, b, (int) sizeof(b), &received, 100); if(rslt != 0) { fprintf(stderr, "libusb_bulk_transfer() failed while receiving: %s\n", libusb_strerror(rslt)); exit(1); } aa.tv_sec = 0; aa.tv_nsec = 50 * 1000 * 1000; /* 50 ms */ nanosleep(&aa, NULL); } if(received == 0) { fprintf(stderr, "Failed to receive a core map reply.\n"); exit(1); } b_ptr = (struct hf_header *) b; if(b_ptr->preamble != 0xaa || b_ptr->operation_code != 141 || /* OP_CORE_MAP */ b_ptr->chip_address != 0x00 || b_ptr->core_address != 0) { fprintf(stderr, "Unexpected header: "); dump_header_and_data(b, received); fprintf(stderr, "\n"); exit(1); } if(b_ptr->crc8 != hfa_crc8(b)) { fprintf(stderr, "Bad CRC8 checksum on header: "); dump_header_and_data(b, received); fprintf(stderr, "\n"); exit(1); } if(4*b_ptr->data_length + 8 != received) { fprintf(stderr, "Received %d bytes, but packet claims to be 8 + %d bytes: ", received, (int) b_ptr->data_length); dump_header_and_data(b, received); fprintf(stderr, "\n"); } total_cores = b_ptr->hdata; total_bits = 4*b_ptr->data_length * 8; if(total_cores <= total_bits - 32 || total_cores > total_bits) { fprintf(stderr, "Core data of %d bits is not consistent with total cores of %d: ", total_bits, total_cores); dump_header_and_data(b, received); fprintf(stderr, "\n"); exit(1); } printf("%d cores: ", total_cores); for(i=0; i < b_ptr->data_length * 4; i++) printf("%02x", b[8 + i]); printf("\n"); shut_down_rw_usb(&s); return 0; }