Browse Source

titan: New commands set_work & get_report

Vitalii Demianets 11 years ago
parent
commit
26e2e783c1
2 changed files with 115 additions and 12 deletions
  1. 97 12
      titan-asic.c
  2. 18 0
      titan-asic.h

+ 97 - 12
titan-asic.c

@@ -10,11 +10,18 @@
 
 #include <zlib.h>
 
+#include "miner.h"
 #include "logging.h"
 #include "lowl-spi.h"
 
 #include "titan-asic.h"
 
+/* ASIC Command codes */
+#define	KNC_ASIC_CMD_GETINFO		0x80
+#define	KNC_ASIC_CMD_REPORT		0x82
+#define	KNC_ASIC_CMD_SETWORK		0x81
+#define	KNC_ASIC_CMD_SETWORK_URGENT	0x83
+
 /* Error bits */
 #define	ERR_SEND_CRC_FAIL	(1 << 0)
 #define	ERR_RCV_CRC_FAIL	(1 << 1)
@@ -32,12 +39,16 @@
 /* send_size - size of send_buf, without crc
  * transfer_size - total size of transfer
  */
-static uint8_t * spi_transfer(struct spi_port * const spi, uint8_t *send_buf, int send_size, int transfer_size, bool check_rcv_crc, uint32_t *errors)
+static uint8_t * spi_transfer(struct spi_port * const spi, uint8_t *send_buf, int send_size, int transfer_size, int rcv_crc_data_len, uint32_t *errors)
 {
 	uint8_t *rxbuf, crcbuf[CRC32_SIZE];
 	uint32_t crc;
 	uint8_t rcv_status;
-	int min_transfer_size = check_rcv_crc ? (send_size + SPI_RESPONSE_TRAILER_SIZE + CRC32_SIZE) : (send_size + CRC32_SIZE);
+	int min_transfer_size = send_size + CRC32_SIZE + SPI_RESPONSE_TRAILER_SIZE;
+	if (0 < rcv_crc_data_len) {
+		if (min_transfer_size < (4 + rcv_crc_data_len + CRC32_SIZE + SPI_RESPONSE_TRAILER_SIZE))
+			min_transfer_size = 4 + rcv_crc_data_len + CRC32_SIZE + SPI_RESPONSE_TRAILER_SIZE;
+	}
 
 	*errors = 0;
 	if (transfer_size < min_transfer_size) {
@@ -49,7 +60,7 @@ exit_other_error:
 	spi_emit_buf(spi, send_buf, send_size);
 	crc = crc32(0, Z_NULL, 0);
 	crc = crc32(crc, send_buf, send_size);
-	*((uint32_t *)crcbuf) = htonl(crc);
+	*((uint32_t *)crcbuf) = htobe32(crc);
 	spi_emit_buf(spi, crcbuf, CRC32_SIZE);
 	spi_emit_nop(spi, transfer_size - spi_getbufsz(spi));
 	if (!spi_txrx(spi))
@@ -61,11 +72,11 @@ exit_other_error:
 		*errors |= ERR_BAD_RESPONSE;
 	if (rcv_status & RCV_STATUS_SEND_CRC_BAD)
 		*errors |= ERR_SEND_CRC_FAIL;
-	if (check_rcv_crc) {
+	if (0 < rcv_crc_data_len) {
 		crc = crc32(0, Z_NULL, 0);
-		crc = crc32(crc, rxbuf + send_size, transfer_size - send_size - SPI_RESPONSE_TRAILER_SIZE - CRC32_SIZE);
-		memcpy(crcbuf, &rxbuf[transfer_size - SPI_RESPONSE_TRAILER_SIZE - CRC32_SIZE], CRC32_SIZE);
-		if (crc != ntohl(*((uint32_t *)crcbuf)))
+		crc = crc32(crc, rxbuf + 4, rcv_crc_data_len);
+		memcpy(crcbuf, &rxbuf[4 + rcv_crc_data_len], CRC32_SIZE);
+		if (crc != be32toh(*((uint32_t *)crcbuf)))
 			*errors |= ERR_RCV_CRC_FAIL;
 	}
 
@@ -80,7 +91,7 @@ exit_other_error:
  */
 bool knc_titan_spi_get_info(const char *repr, struct spi_port * const spi, struct titan_info_response *resp, int die, int core_hint)
 {
-	uint8_t get_info_cmd[] = {0x80, die, 0x00, 0x00};
+	uint8_t get_info_cmd[] = {KNC_ASIC_CMD_GETINFO, die, 0x00, 0x00};
 	uint8_t *rxbuf;
 	uint32_t errors;
 	uint16_t revision;
@@ -88,7 +99,7 @@ bool knc_titan_spi_get_info(const char *repr, struct spi_port * const spi, struc
 	int i, core;
 
 	for (i = 0; i < 3; ++i) {
-		rxbuf = spi_transfer(spi, get_info_cmd, sizeof(get_info_cmd), transfer_size, true, &errors);
+		rxbuf = spi_transfer(spi, get_info_cmd, sizeof(get_info_cmd), transfer_size, transfer_size - 4 - CRC32_SIZE - SPI_RESPONSE_TRAILER_SIZE, &errors);
 		if (NULL == rxbuf) {
 exit_unrec_error:	applog(LOG_ERR, "%s[%d] knc_titan_spi_get_info: Unrecognized error", repr, die);
 			return false;
@@ -109,7 +120,7 @@ exit_unrec_error:	applog(LOG_ERR, "%s[%d] knc_titan_spi_get_info: Unrecognized e
 		 * from partial response.
 		 */
 		revision = (rxbuf[6] << 8) | rxbuf[7];
-		if (0xA102 != revision) {
+		if (KNC_TITAN_ASIC_REVISION != revision) {
 exit_bad_revision:	applog(LOG_ERR, "%s[%d] knc_titan_spi_get_info: Bad revision 0x%04hX", repr, die, revision);
 			return false;
 		}
@@ -118,7 +129,7 @@ exit_bad_revision:	applog(LOG_ERR, "%s[%d] knc_titan_spi_get_info: Bad revision
 			applog(LOG_NOTICE, "%s[%d] core hint %d might be wrong, new guess is %d", repr, die, core_hint, resp->cores);
 			transfer_size = 24 + ((resp->cores + 3) / 4);
 			for (i = 0; i < 3; ++i) {
-				rxbuf = spi_transfer(spi, get_info_cmd, sizeof(get_info_cmd), transfer_size, true, &errors);
+				rxbuf = spi_transfer(spi, get_info_cmd, sizeof(get_info_cmd), transfer_size, transfer_size - 4 - CRC32_SIZE - SPI_RESPONSE_TRAILER_SIZE, &errors);
 				if (NULL == rxbuf)
 					goto exit_unrec_error;
 				if (errors != ERR_SEND_CRC_FAIL)
@@ -136,7 +147,7 @@ exit_bad_revision:	applog(LOG_ERR, "%s[%d] knc_titan_spi_get_info: Bad revision
 	}
 
 	revision = (rxbuf[6] << 8) | rxbuf[7];
-	if (0xA102 != revision)
+	if (KNC_TITAN_ASIC_REVISION != revision)
 		goto exit_bad_revision;
 	resp->cores = (rxbuf[4] << 8) | rxbuf[5];
 	resp->pll_state = *((uint64_t *)(&rxbuf[8]));
@@ -162,3 +173,77 @@ exit_bad_revision:	applog(LOG_ERR, "%s[%d] knc_titan_spi_get_info: Bad revision
 
 	return true;
 }
+
+static void knc_titan_parse_get_report(uint8_t *data, struct titan_report *report)
+{
+	int i;
+
+	report->flags = data[0];
+	report->core_counter = data[1];
+	report->slot_core = (data[2] >> 4) & 0x0F;
+	for (i = 0; i < KNC_TITAN_NONCES_PER_REPORT; ++i) {
+		report->nonces[i].slot = data[2 + i * 5] & 0x0F;
+		report->nonces[i].nonce = ((uint32_t)data[2 + i * 5 + 1] << 24) |
+					  ((uint32_t)data[2 + i * 5 + 2] << 16) |
+					  ((uint32_t)data[2 + i * 5 + 3] << 8) |
+					  ((uint32_t)data[2 + i * 5 + 4]);
+	}
+}
+
+bool knc_titan_set_work(const char *repr, struct spi_port * const spi, struct titan_report *report, int die, int core, int slot, struct work *work, bool urgent)
+{
+#define	SETWORK_CMD_SIZE	(5 + BLOCK_HEADER_BYTES_WITHOUT_NONCE)
+	uint8_t set_work_cmd_aligned[3 + SETWORK_CMD_SIZE] = {
+		0, 0, 0, /* three extra bytes for alignment */
+		urgent ? KNC_ASIC_CMD_SETWORK_URGENT : KNC_ASIC_CMD_SETWORK,
+		die,
+		(core >> 8) & 0xFF,
+		core & 0xFF,
+		0xF0 | (slot & 0x0F),
+		/* next follows data. Thanks to the first three extra bytes it is 64bit-aligned */
+	};
+	const int send_size = sizeof(set_work_cmd_aligned) - 3;
+	const int transfer_size = send_size + CRC32_SIZE + SPI_RESPONSE_TRAILER_SIZE;
+	uint8_t *rxbuf;
+	int i;
+	uint32_t *src, *dst;
+	uint32_t errors;
+
+	src = (uint32_t *)work->data;
+	dst = (uint32_t *)(&set_work_cmd_aligned[3 + 5]);
+	for (i = 0; i < (BLOCK_HEADER_BYTES_WITHOUT_NONCE / 4); ++i)
+		dst[i] = htobe32(src[i]);
+
+	rxbuf = spi_transfer(spi, &set_work_cmd_aligned[3], send_size, transfer_size, 2 + KNC_TITAN_NONCES_PER_REPORT * 5, &errors);
+	if (NULL == rxbuf) {
+		applog(LOG_ERR, "%s[%d:%d] knc_titan_set_work: Unrecognized error", repr, die, core);
+		return false;
+	}
+	if (0 != errors) {
+		applog(LOG_ERR, "%s[%d:%d] knc_titan_set_work: Communication failed, errors = 0x%X", repr, die, core, errors);
+		return false;
+	}
+	knc_titan_parse_get_report(&rxbuf[4], report);
+	return true;
+}
+
+bool knc_titan_get_report(const char *repr, struct spi_port * const spi, struct titan_report *report, int die, int core)
+{
+	uint8_t get_report_cmd[] = {KNC_ASIC_CMD_REPORT, die, (core >> 8) & 0xFF, core & 0xFF};
+	const int send_size = sizeof(get_report_cmd);
+	const int transfer_size = send_size + 2 + KNC_TITAN_NONCES_PER_REPORT * 5 + CRC32_SIZE + SPI_RESPONSE_TRAILER_SIZE;
+	uint8_t *rxbuf;
+	uint32_t errors;
+
+	rxbuf = spi_transfer(spi, get_report_cmd, send_size, transfer_size, 2 + KNC_TITAN_NONCES_PER_REPORT * 5, &errors);
+	if (NULL == rxbuf) {
+		applog(LOG_ERR, "%s[%d:%d] knc_titan_get_report: Unrecognized error", repr, die, core);
+		return false;
+	}
+	if (0 != errors) {
+		applog(LOG_ERR, "%s[%d:%d] knc_titan_get_report: Communication failed, errors = 0x%X", repr, die, core, errors);
+		return false;
+	}
+	knc_titan_parse_get_report(&rxbuf[4], report);
+	return true;
+}

+ 18 - 0
titan-asic.h

@@ -1,10 +1,16 @@
 #ifndef __TITAN_ASIC_H
 #define __TITAN_ASIC_H
 
+#define	BLOCK_HEADER_BYTES			80
+#define	BLOCK_HEADER_BYTES_WITHOUT_NONCE	(BLOCK_HEADER_BYTES - 4)
+
 #define	KNC_TITAN_MAX_ASICS		6
 #define	KNC_TITAN_DIES_PER_ASIC		4
 #define	KNC_TITAN_CORES_PER_DIE		48
 #define	KNC_TITAN_CORES_PER_ASIC	(KNC_TITAN_CORES_PER_DIE * KNC_TITAN_DIES_PER_ASIC)
+#define	KNC_TITAN_NONCES_PER_REPORT	5
+
+#define	KNC_TITAN_ASIC_REVISION		0xA102
 
 struct titan_info_response {
 	uint64_t pll_state;
@@ -13,6 +19,18 @@ struct titan_info_response {
 	bool have_report[KNC_TITAN_CORES_PER_DIE];
 };
 
+struct titan_report {
+	uint8_t flags;
+	uint8_t core_counter;
+	uint8_t slot_core;
+	struct nonce_report {
+		uint32_t nonce;
+		uint8_t slot;
+	} nonces[KNC_TITAN_NONCES_PER_REPORT];
+};
+
 bool knc_titan_spi_get_info(const char *repr, struct spi_port * const spi, struct titan_info_response *resp, int die, int core_hint);
+bool knc_titan_get_report(const char *repr, struct spi_port * const spi, struct titan_report *report, int die, int core);
+bool knc_titan_set_work(const char *repr, struct spi_port * const spi, struct titan_report *report, int die, int core, int slot, struct work *work, bool urgent);
 
 #endif /* __TITAN_ASIC_H */