titan-asic.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. uint8_t *requests = malloc(REQUEST_BUFSIZE * num);
  52. if (NULL == requests)
  53. goto exit_err;
  54. int RESPONSE_BUFSIZE = 1 + 1 + (1 + 4) * 5;
  55. uint8_t *responses = malloc(RESPONSE_BUFSIZE * num);
  56. if (NULL == responses)
  57. goto exit_err;
  58. int *request_lengths = malloc(num * sizeof(int));
  59. if (NULL == request_lengths)
  60. goto exit_err;
  61. int *response_lengths = malloc(num * sizeof(int));
  62. if (NULL == response_lengths)
  63. goto exit_err;
  64. int *statuses = malloc(num * sizeof(int));
  65. if (NULL == statuses)
  66. goto exit_err;
  67. int i, core;
  68. for (i = 0, core = core_start; i < num; ++i, ++core) {
  69. request_lengths[i] = knc_prepare_titan_setwork(&requests[REQUEST_BUFSIZE * i], die, core, slot, work, urgent);
  70. response_lengths[i] = RESPONSE_BUFSIZE;
  71. statuses[i] = KNC_ERR_UNAVAIL;
  72. }
  73. knc_syncronous_transfer_multi(ctx, channel, request_lengths, REQUEST_BUFSIZE, requests, response_lengths, RESPONSE_BUFSIZE, responses, statuses, num);
  74. for (i = 0, core = core_start; i < num; ++i, ++core) {
  75. uint8_t *response = &responses[RESPONSE_BUFSIZE * i];
  76. if (statuses[i] == KNC_ACCEPTED) {
  77. work_accepted[i] = true;
  78. } else {
  79. work_accepted[i] = false;
  80. if (response[0] == 0x7f) {
  81. applog(LOG_DEBUG, "%s[%d:%d:%d]: Core disabled", repr, channel, die, core);
  82. continue;
  83. }
  84. if (statuses[i] & KNC_ERR_MASK) {
  85. applog(LOG_INFO, "%s[%d:%d:%d]: Failed to set work state (%x)", repr, channel, die, core, statuses[i]);
  86. continue;
  87. }
  88. if (!(statuses[i] & KNC_ERR_MASK)) {
  89. applog(LOG_DEBUG, "%s[%d:%d:%d]: Core busy (%x)", repr, channel, die, core, statuses[i]);
  90. }
  91. }
  92. knc_decode_report(response, &reports[i], KNC_VERSION_TITAN);
  93. }
  94. free(response_lengths);
  95. free(request_lengths);
  96. free(statuses);
  97. free(responses);
  98. free(requests);
  99. return true;
  100. exit_err:
  101. if (NULL != response_lengths)
  102. free(response_lengths);
  103. if (NULL != request_lengths)
  104. free(request_lengths);
  105. if (NULL != statuses)
  106. free(statuses);
  107. if (NULL != responses)
  108. free(responses);
  109. if (NULL != requests)
  110. free(requests);
  111. return false;
  112. }
  113. bool knc_titan_get_report(const char *repr, void * const ctx, int channel, int die, int core, struct knc_report *report)
  114. {
  115. uint8_t request[4];
  116. int request_length;
  117. int response_length = 1 + 1 + (1 + 4) * 5;
  118. uint8_t response[response_length];
  119. int status;
  120. request_length = knc_prepare_report(request, die, core);
  121. status = knc_syncronous_transfer(ctx, channel, request_length, request, response_length, response);
  122. if (status) {
  123. applog(LOG_INFO, "%s[%d:%d:%d]: get_report failed (%x)", repr, channel, die, core, status);
  124. return false;
  125. }
  126. knc_decode_report(response, report, KNC_VERSION_TITAN);
  127. return true;
  128. }
  129. /* Use bare function without extra checks */
  130. extern bool knc_titan_setup_core_(void * const ctx, int channel, int die, int core, struct titan_setup_core_params *params);
  131. /* This fails if core is hashing!
  132. * Stop it before setting up.
  133. */
  134. bool knc_titan_setup_core_local(const char *repr, void * const ctx, int channel, int die, int core, struct titan_setup_core_params *params)
  135. {
  136. return knc_titan_setup_core_(ctx, channel, die, core, params);
  137. }