bf16-bitfury16.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. #ifndef BF16_BITFURY16_H
  2. #define BF16_BITFURY16_H
  3. #include <stdint.h>
  4. #include "bf16-spidevice.h"
  5. /******************************************************
  6. * Macros
  7. ******************************************************/
  8. #define LIST_PUSH_HEAD(_list, _item) { \
  9. if (_list->tail == NULL) { \
  10. _list->tail = _item; \
  11. _list->head = _item; \
  12. } else { \
  13. _list->head->prev = _item; \
  14. _item->next = _list->head; \
  15. _list->head = _item; \
  16. } \
  17. }
  18. #define LIST_PUSH_TAIL(_list, _item) { \
  19. if (_list->head == NULL) { \
  20. _list->head = _item; \
  21. _list->tail = _item; \
  22. } else { \
  23. _list->tail->next = _item; \
  24. _item->prev = _list->tail; \
  25. _list->tail = _item; \
  26. } \
  27. }
  28. #define LIST_POP_HEAD(_list) { \
  29. if (_list->head != _list->tail) { \
  30. _list->head = _list->head->next; \
  31. _list->head->prev = NULL; \
  32. } else { \
  33. _list->head = NULL; \
  34. _list->tail = NULL; \
  35. } \
  36. }
  37. #define LIST_POP_TAIL(_list) { \
  38. if (_list->head != _list->tail) { \
  39. _list->tail = _list->tail->prev; \
  40. _list->tail->next = NULL; \
  41. } else { \
  42. _list->head = NULL; \
  43. _list->tail = NULL; \
  44. } \
  45. }
  46. #define LIST_REMOVE(_list, _item) { \
  47. if (_list->head != _list->tail) { \
  48. if (_list->head == _item) { \
  49. _list->head = _list->head->next; \
  50. _list->head->prev = NULL; \
  51. } else if (_list->tail == _item) { \
  52. _list->tail = _list->tail->prev; \
  53. _list->tail->next = NULL; \
  54. } else { \
  55. bf_data_t* prev; \
  56. bf_data_t* next; \
  57. prev = _item->prev; \
  58. next = _item->next; \
  59. prev->next = next; \
  60. next->prev = prev; \
  61. } \
  62. } else { \
  63. _list->head = NULL; \
  64. _list->tail = NULL; \
  65. } \
  66. }
  67. #define L_LOCK(_list) pthread_mutex_lock (&_list->lock);
  68. #define L_UNLOCK(_list) pthread_mutex_unlock(&_list->lock);
  69. /******************************************************
  70. * Constants
  71. ******************************************************/
  72. #define CHIP_COEFF 4.295
  73. #define CHIP_CMD_NUM 8
  74. #define CMD_BUFFER_LEN 4096
  75. /******************************************************
  76. * Enumerations
  77. ******************************************************/
  78. typedef enum {
  79. CHIP_CMD_TASK_STATUS,
  80. CHIP_CMD_TASK_WRITE,
  81. CHIP_CMD_TASK_SWITCH,
  82. CHIP_CMD_READ_NONCE = 0x04,
  83. CHIP_CMD_SET_CLOCK = 0x08,
  84. CHIP_CMD_TOGGLE = 0x10,
  85. CHIP_CMD_SET_MASK = 0x20,
  86. CHIP_CMD_CREATE_CHANNEL
  87. } bf_cmd_code_t;
  88. /* chip state enumeration */
  89. typedef enum {
  90. UNINITIALIZED,
  91. TOGGLE_SET,
  92. CLOCK_SET,
  93. MASK_SET,
  94. TASK_SENT,
  95. TASK_SWITCHED,
  96. FAILING,
  97. DISABLED
  98. } bf_chip_status_t;
  99. enum bf_channel_id {
  100. BF250_NONE = 0x00,
  101. BF250_LOCAL = 0x04,
  102. BF250_CHAN1 = 0x06,
  103. BF250_CHAN2 = 0x07
  104. };
  105. typedef enum {
  106. RENONCE_STAGE0,
  107. RENONCE_STAGE1,
  108. RENONCE_STAGE2,
  109. RENONCE_STAGE3,
  110. RENONCE_STAGE_FINISHED
  111. } bf_renonce_stage_t;
  112. /******************************************************
  113. * Type Definitions
  114. ******************************************************/
  115. /* list definition */
  116. struct bf_data {
  117. struct bf_data* next;
  118. struct bf_data* prev;
  119. void* data;
  120. };
  121. typedef struct bf_data bf_data_t;
  122. typedef struct {
  123. bf_data_t* head;
  124. bf_data_t* tail;
  125. uint32_t count;
  126. pthread_mutex_t lock;
  127. } bf_list_t;
  128. /* general chip command staff */
  129. typedef struct {
  130. bf_cmd_code_t cmd_code;
  131. char cmd_description[32];
  132. } bf_cmd_description_t;
  133. typedef struct {
  134. int8_t board_id;
  135. int8_t bcm250_id;
  136. int8_t chip_id;
  137. } bf_chip_address_t;
  138. typedef struct {
  139. bf_chip_address_t chip_address;
  140. uint8_t depth;
  141. bf_cmd_code_t cmd_code;
  142. uint8_t data_length;
  143. uint8_t tx[128];
  144. uint8_t rx[64];
  145. uint8_t status;
  146. uint8_t checksum;
  147. bool checksum_error;
  148. uint8_t nonce_checksum;
  149. bool nonce_checksum_error;
  150. } bf_command_t;
  151. /* work stuff */
  152. typedef struct {
  153. uint32_t midstate[8];
  154. uint32_t m7;
  155. uint32_t ntime;
  156. uint32_t nbits;
  157. } bf_payload_t;
  158. typedef struct {
  159. struct work* work;
  160. bf_payload_t payload;
  161. bool rolled;
  162. time_t generated;
  163. } bf_workd_t;
  164. typedef struct {
  165. struct work work;
  166. bf_payload_t payload;
  167. uint8_t task[80];
  168. } bf_works_t;
  169. /* nonceworker stuff */
  170. typedef struct {
  171. bf_chip_address_t chip_address;
  172. bf_chip_address_t src_address;
  173. bf_works_t cwork;
  174. bf_works_t owork;
  175. uint32_t nonce;
  176. } bf_noncework_t;
  177. /* renonceworker stuff */
  178. typedef struct {
  179. bf_chip_address_t src_address;
  180. uint32_t nonce;
  181. } bf_renoncework_t;
  182. /* nonce recalculation staff */
  183. /* nonce list */
  184. typedef struct {
  185. uint32_t nonce;
  186. } bf_nonce_t;
  187. /* task + nonces list */
  188. typedef struct {
  189. uint32_t id;
  190. uint32_t nonce;
  191. bf_works_t cwork;
  192. bf_works_t owork;
  193. bf_chip_address_t src_address;
  194. bf_renonce_stage_t stage;
  195. bool sent;
  196. bool received;
  197. bool match;
  198. } bf_renonce_t;
  199. /* command buffer staff */
  200. typedef struct bf_cmd {
  201. bf_chip_address_t chip_address; /* address of chip calculating result */
  202. bf_chip_address_t src_address; /* track chip address during nonce recalculation */
  203. bf_works_t work;
  204. uint32_t id; /* renonce id */
  205. uint8_t depth;
  206. bf_cmd_code_t cmd_code;
  207. uint8_t data_length;
  208. uint8_t checksum;
  209. } bf_cmd_t;
  210. #define CMD(_item) ((bf_cmd_t *) (_item->data))
  211. #define NONCE(_item) ((bf_nonce_t *) (_item->data))
  212. #define RENONCE(_item) ((bf_renonce_t *) (_item->data))
  213. #define NONCEWORK(_item) ((bf_noncework_t *) (_item->data))
  214. #define RENONCEWORK(_item) ((bf_renoncework_t *) (_item->data))
  215. #define WORKD(_item) ((bf_workd_t *) (_item->data))
  216. #define WORKS(_item) ((bf_works_t *) (_item->data))
  217. typedef struct {
  218. bf_chip_address_t chip_address;
  219. bf_chip_address_t src_address;
  220. bf_works_t work;
  221. uint32_t id;
  222. bf_cmd_code_t cmd_code;
  223. uint8_t status;
  224. uint8_t checksum_expected;
  225. uint8_t checksum_received;
  226. bool checksum_error;
  227. uint8_t nonce_checksum_expected;
  228. uint8_t nonce_checksum_received;
  229. bool nonce_checksum_error;
  230. } bf_cmd_status_t;
  231. typedef enum {
  232. EMPTY,
  233. TX_READY,
  234. EXECUTED
  235. } bf_cmd_buffer_status_t;
  236. typedef struct {
  237. bf_list_t* cmd_list;
  238. uint8_t* tx_buffer;
  239. uint8_t* rx_buffer;
  240. uint32_t free_bytes; /* TX buffer bytes free */
  241. uint32_t tx_offset;
  242. uint32_t rx_offset;
  243. bf_cmd_buffer_status_t status;
  244. } bf_cmd_buffer_t;
  245. /******************************************************
  246. * Structures
  247. ******************************************************/
  248. /******************************************************
  249. * Static Function Declarations
  250. ******************************************************/
  251. /******************************************************
  252. * Variables Definitions
  253. ******************************************************/
  254. extern bf_cmd_description_t cmd_description[CHIP_CMD_NUM];
  255. /******************************************************
  256. * Function Definitions
  257. ******************************************************/
  258. /* BF16 command primitives */
  259. uint8_t spi_command_init(bf_command_t* command, const uint8_t depth,
  260. const bf_chip_address_t chip_address, const bf_cmd_code_t cmd_code,
  261. const uint8_t data_length, const uint8_t* tx);
  262. uint8_t spi_command_exec(spi_channel_id_t spi_channel, bf_command_t* command, uint32_t* nonces);
  263. /* data preparation routines */
  264. uint8_t gen_clock_data(uint8_t clock, uint8_t prescaler, uint8_t data[4]);
  265. uint8_t gen_task_data(uint32_t* midstate, uint32_t merkle, uint32_t ntime,
  266. uint32_t nbits, uint32_t mask, uint8_t* task);
  267. uint32_t gen_mask(uint32_t nonce, uint8_t nbits);
  268. /* SPI BCM250 primitives */
  269. uint8_t create_channel(spi_channel_id_t spi_channel, uint8_t* channel_path, uint8_t channel_length);
  270. uint8_t destroy_channel(spi_channel_id_t spi_channel, uint8_t depth);
  271. /* SPI BTC16 primitives */
  272. void spi_emit_reset(spi_channel_id_t spi_channel);
  273. uint8_t send_toggle(spi_channel_id_t spi_channel, uint8_t depth,
  274. bf_chip_address_t chip_address);
  275. uint8_t set_clock(spi_channel_id_t spi_channel, uint8_t depth,
  276. bf_chip_address_t chip_address, uint8_t clock);
  277. /* cmd buffer primitives */
  278. int8_t cmd_buffer_init(bf_cmd_buffer_t* cmd_buffer);
  279. int8_t cmd_buffer_deinit(bf_cmd_buffer_t* cmd_buffer);
  280. int8_t cmd_buffer_clear(bf_cmd_buffer_t* cmd_buffer);
  281. int8_t cmd_buffer_push(bf_cmd_buffer_t* cmd_buffer, const uint8_t depth,
  282. const bf_chip_address_t chip_address, const bf_chip_address_t src_address,
  283. const bf_works_t work, const uint32_t id,
  284. const bf_cmd_code_t cmd_code, const uint8_t data_length, const uint8_t* tx);
  285. int8_t cmd_buffer_pop(bf_cmd_buffer_t* cmd_buffer, bf_cmd_status_t* cmd_status, uint32_t* nonces);
  286. int8_t cmd_buffer_exec(spi_channel_id_t spi_channel, bf_cmd_buffer_t* cmd_buffer);
  287. int8_t cmd_buffer_push_create_channel(bf_cmd_buffer_t* cmd_buffer, uint8_t* channel_path,
  288. uint8_t channel_length);
  289. int8_t cmd_buffer_push_destroy_channel(bf_cmd_buffer_t* cmd_buffer, const uint8_t depth);
  290. int8_t cmd_buffer_push_send_toggle(bf_cmd_buffer_t* cmd_buffer, const uint8_t depth,
  291. const bf_chip_address_t chip_address);
  292. int8_t cmd_buffer_push_set_clock(bf_cmd_buffer_t* cmd_buffer, const uint8_t depth,
  293. const bf_chip_address_t chip_address, uint8_t clock);
  294. int8_t cmd_buffer_push_set_mask(bf_cmd_buffer_t* cmd_buffer, const uint8_t depth,
  295. const bf_chip_address_t chip_address, uint8_t mask);
  296. char* get_cmd_description(bf_cmd_code_t cmd_code);
  297. /* dynamic work list primitives */
  298. bf_list_t* workd_list_init(void);
  299. int8_t workd_list_deinit(bf_list_t* list, struct cgpu_info *bitfury);
  300. int8_t workd_list_push(bf_list_t* list, bf_workd_t* work);
  301. int8_t workd_list_pop(bf_list_t* list, struct cgpu_info *bitfury);
  302. int8_t workd_list_remove(bf_list_t* list, bf_works_t* work);
  303. /* nonce list primitives */
  304. bf_list_t* nonce_list_init(void);
  305. int8_t nonce_list_deinit(bf_list_t* list);
  306. int8_t nonce_list_push(bf_list_t* list, uint32_t nonce);
  307. uint32_t nonce_list_pop(bf_list_t* list);
  308. /* noncework list primitives */
  309. bf_list_t* noncework_list_init(void);
  310. int8_t noncework_list_deinit(bf_list_t* list);
  311. int8_t noncework_list_push(bf_list_t* list, bf_chip_address_t chip_address,
  312. bf_chip_address_t src_address, bf_works_t cwork, bf_works_t owork, uint32_t nonce);
  313. int8_t noncework_list_pop(bf_list_t* list);
  314. bf_list_t* renoncework_list_init(void);
  315. int8_t renoncework_list_deinit(bf_list_t* list);
  316. int8_t renoncework_list_push(bf_list_t* list, bf_chip_address_t src_address, uint32_t nonce);
  317. int8_t renoncework_list_pop(bf_list_t* list);
  318. /* renonce list primitives */
  319. bf_list_t* renonce_list_init(void);
  320. int8_t renonce_list_deinit(bf_list_t* list);
  321. int8_t renonce_list_push(bf_list_t* list, uint32_t id, uint32_t nonce, bf_chip_address_t src_address,
  322. bf_works_t cwork, bf_works_t owork);
  323. int8_t renonce_list_pop(bf_list_t* list);
  324. int8_t renonce_list_remove(bf_list_t* list, bf_data_t* rdata);
  325. uint8_t find_nonces(uint32_t* curr_nonces, uint32_t* prev_nonces, uint32_t* valid_nonces);
  326. bool match_nonce(uint32_t nonce, uint32_t mask, uint8_t nbits);
  327. #endif /* BF16_BITFURY16_H */