titan-asic.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright 2014 Vitalii Demianets
  3. * Copyright 2014 KnCMiner
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "miner.h"
  11. #include "logging.h"
  12. #include "titan-asic.h"
  13. bool knc_titan_get_info(const char *repr, void * const ctx, int channel, int die, struct knc_die_info *die_info)
  14. {
  15. int rc;
  16. rc = knc_detect_die(ctx, channel, die, die_info);
  17. return (0 == rc);
  18. }
  19. bool knc_titan_set_work(const char *repr, void * const ctx, int channel, int die, int core, int slot, struct work *work, bool urgent, bool *work_accepted, struct knc_report *report)
  20. {
  21. int request_length = 4 + 1 + BLOCK_HEADER_BYTES_WITHOUT_NONCE;
  22. uint8_t request[request_length];
  23. int response_length = 1 + 1 + (1 + 4) * 5;
  24. uint8_t response[response_length];
  25. int status;
  26. request_length = knc_prepare_titan_setwork(request, die, core, slot, work, urgent);
  27. status = knc_syncronous_transfer(ctx, channel, request_length, request, response_length, response);
  28. if (status == KNC_ACCEPTED) {
  29. *work_accepted = true;
  30. } else {
  31. *work_accepted = false;
  32. if (response[0] == 0x7f) {
  33. applog(LOG_DEBUG, "%s[%d:%d:%d]: Core disabled", repr, channel, die, core);
  34. return false;
  35. }
  36. if (status & KNC_ERR_MASK) {
  37. applog(LOG_INFO, "%s[%d:%d:%d]: Failed to set work state (%x)", repr, channel, die, core, status);
  38. return false;
  39. }
  40. if (!(status & KNC_ERR_MASK)) {
  41. /* !KNC_ERRMASK */
  42. applog(LOG_DEBUG, "%s[%d:%d:%d]: Core busy (%x)", repr, channel, die, core, status);
  43. }
  44. }
  45. knc_decode_report(response, report, KNC_VERSION_TITAN);
  46. return true;
  47. }
  48. bool knc_titan_set_work_multi(const char *repr, void * const ctx, int channel, int die, int core_start, int slot, struct work *work, bool urgent, bool *work_accepted, struct knc_report *reports, int num)
  49. {
  50. int REQUEST_BUFSIZE = 4 + 1 + BLOCK_HEADER_BYTES_WITHOUT_NONCE;
  51. int RESPONSE_BUFSIZE = 1 + 1 + (1 + 4) * 5;
  52. int i, core;
  53. uint8_t *requests = NULL;
  54. uint8_t *responses = NULL;
  55. int *request_lengths = NULL;
  56. int *response_lengths = NULL;
  57. int *statuses = NULL;
  58. requests = malloc(REQUEST_BUFSIZE * num);
  59. if (NULL == requests)
  60. goto exit_err;
  61. responses = malloc(RESPONSE_BUFSIZE * num);
  62. if (NULL == responses)
  63. goto exit_err;
  64. request_lengths = malloc(num * sizeof(int));
  65. if (NULL == request_lengths)
  66. goto exit_err;
  67. response_lengths = malloc(num * sizeof(int));
  68. if (NULL == response_lengths)
  69. goto exit_err;
  70. statuses = malloc(num * sizeof(int));
  71. if (NULL == statuses)
  72. goto exit_err;
  73. for (i = 0, core = core_start; i < num; ++i, ++core) {
  74. request_lengths[i] = knc_prepare_titan_setwork(&requests[REQUEST_BUFSIZE * i], die, core, slot, work, urgent);
  75. response_lengths[i] = RESPONSE_BUFSIZE;
  76. statuses[i] = KNC_ERR_UNAVAIL;
  77. }
  78. knc_syncronous_transfer_multi(ctx, channel, request_lengths, REQUEST_BUFSIZE, requests, response_lengths, RESPONSE_BUFSIZE, responses, statuses, num);
  79. for (i = 0, core = core_start; i < num; ++i, ++core) {
  80. uint8_t *response = &responses[RESPONSE_BUFSIZE * i];
  81. if (statuses[i] == KNC_ACCEPTED) {
  82. work_accepted[i] = true;
  83. } else {
  84. work_accepted[i] = false;
  85. if (response[0] == 0x7f) {
  86. applog(LOG_DEBUG, "%s[%d:%d:%d]: Core disabled", repr, channel, die, core);
  87. continue;
  88. }
  89. if (statuses[i] & KNC_ERR_MASK) {
  90. applog(LOG_INFO, "%s[%d:%d:%d]: Failed to set work state (%x)", repr, channel, die, core, statuses[i]);
  91. continue;
  92. }
  93. if (!(statuses[i] & KNC_ERR_MASK)) {
  94. applog(LOG_DEBUG, "%s[%d:%d:%d]: Core busy (%x)", repr, channel, die, core, statuses[i]);
  95. }
  96. }
  97. knc_decode_report(response, &reports[i], KNC_VERSION_TITAN);
  98. }
  99. free(response_lengths);
  100. free(request_lengths);
  101. free(statuses);
  102. free(responses);
  103. free(requests);
  104. return true;
  105. exit_err:
  106. if (NULL != response_lengths)
  107. free(response_lengths);
  108. if (NULL != request_lengths)
  109. free(request_lengths);
  110. if (NULL != statuses)
  111. free(statuses);
  112. if (NULL != responses)
  113. free(responses);
  114. if (NULL != requests)
  115. free(requests);
  116. return false;
  117. }
  118. bool knc_titan_get_report(const char *repr, void * const ctx, int channel, int die, int core, struct knc_report *report)
  119. {
  120. uint8_t request[4];
  121. int request_length;
  122. int response_length = 1 + 1 + (1 + 4) * 5;
  123. uint8_t response[response_length];
  124. int status;
  125. request_length = knc_prepare_report(request, die, core);
  126. status = knc_syncronous_transfer(ctx, channel, request_length, request, response_length, response);
  127. if (status) {
  128. applog(LOG_INFO, "%s[%d:%d:%d]: get_report failed (%x)", repr, channel, die, core, status);
  129. return false;
  130. }
  131. knc_decode_report(response, report, KNC_VERSION_TITAN);
  132. return true;
  133. }
  134. /* This fails if core is hashing!
  135. * Stop it before setting up.
  136. */
  137. bool knc_titan_setup_core_local(const char *repr, void * const ctx, int channel, int die, int core, struct titan_setup_core_params *params)
  138. {
  139. return knc_titan_setup_core_(LOG_INFO, ctx, channel, die, core, params);
  140. }
  141. bool knc_titan_setup_spi(const char *repr, void * const ctx, int asic, int divider, int preclk, int declk, int sslowmin)
  142. {
  143. uint8_t request[7];
  144. int request_length;
  145. int status;
  146. request_length = knc_prepare_titan_setup(request, asic, divider, preclk, declk, sslowmin);
  147. status = knc_syncronous_transfer_fpga(ctx, request_length, request, 0, NULL);
  148. if (status) {
  149. applog(LOG_INFO, "%s[%d]: setup_spi failed (%x)", repr, asic, status);
  150. return false;
  151. }
  152. return true;
  153. }
  154. bool knc_titan_set_work_parallel(const char *repr, void * const ctx, int asic, int die, int core_start, int slot, struct work *work, bool urgent, int num, int resend)
  155. {
  156. uint8_t request[9 + BLOCK_HEADER_BYTES_WITHOUT_NONCE];
  157. int request_length;
  158. int status;
  159. request_length = knc_prepare_titan_work_request(request, asic, die, slot, core_start, core_start + num - 1, resend, work);
  160. status = knc_syncronous_transfer_fpga(ctx, request_length, request, 0, NULL);
  161. if (status) {
  162. applog(LOG_INFO, "%s[%d]: set_work_parallel failed (%x)", repr, asic, status);
  163. return false;
  164. }
  165. return true;
  166. }
  167. bool knc_titan_get_work_status(const char *repr, void * const ctx, int asic, int *num_request_busy, int *num_status_byte_error)
  168. {
  169. uint8_t request[2];
  170. int request_length;
  171. int response_length = 2 + 2 * KNC_STATUS_BYTE_ERROR_COUNTERS;
  172. uint8_t response[response_length];
  173. int status;
  174. uint8_t num_request_busy_byte;
  175. uint16_t num_status_byte_error_counters[KNC_STATUS_BYTE_ERROR_COUNTERS];
  176. request_length = knc_prepare_titan_work_status(request, asic);
  177. status = knc_syncronous_transfer_fpga(ctx, request_length, request, response_length, response);
  178. if (status) {
  179. applog(LOG_INFO, "%s[%d]: get_work_status failed (%x)", repr, asic, status);
  180. return false;
  181. }
  182. status = knc_decode_work_status(response, &num_request_busy_byte, num_status_byte_error_counters);
  183. if (status) {
  184. applog(LOG_INFO, "%s[%d]: get_work_status got undefined response", repr, asic);
  185. return false;
  186. }
  187. *num_request_busy = num_request_busy_byte;
  188. for (int i = 0 ; i < KNC_STATUS_BYTE_ERROR_COUNTERS ; i++)
  189. num_status_byte_error[i] = num_status_byte_error_counters[i];
  190. return true;
  191. }