driver-kncasic.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. /*
  2. * Copyright 2014 KnCminer
  3. * Copyright 2014 Luke Dashjr
  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 <stdlib.h>
  11. #include <assert.h>
  12. #include <fcntl.h>
  13. #include <limits.h>
  14. #include <unistd.h>
  15. #include <sys/ioctl.h>
  16. #include <sys/time.h>
  17. #include <linux/types.h>
  18. #include <linux/spi/spidev.h>
  19. #include <zlib.h>
  20. #include "deviceapi.h"
  21. #include "logging.h"
  22. #include "miner.h"
  23. #include "knc-asic/knc-transport.h"
  24. #include "knc-asic/knc-asic.h"
  25. #define WORKS_PER_CORE 3
  26. #define CORE_ERROR_LIMIT 30
  27. #define CORE_ERROR_INTERVAL 30
  28. #define CORE_ERROR_DISABLE_TIME 5*60
  29. #define CORE_SUBMIT_MIN_TIME 2
  30. #define CORE_TIMEOUT 20
  31. #define SCAN_ADJUST_RANGE 32
  32. BFG_REGISTER_DRIVER(kncasic_drv)
  33. #define KNC_FREE_WORK(WORK) do { \
  34. free_work(WORK); \
  35. WORK = NULL; \
  36. } while (0)
  37. static struct timeval now;
  38. static const struct timeval core_check_interval = {
  39. CORE_ERROR_INTERVAL, 0
  40. };
  41. static const struct timeval core_disable_interval = {
  42. CORE_ERROR_DISABLE_TIME, 0
  43. };
  44. static const struct timeval core_submit_interval = {
  45. CORE_SUBMIT_MIN_TIME, 0
  46. };
  47. static const struct timeval core_timeout_interval = {
  48. CORE_TIMEOUT, 0
  49. };
  50. struct knc_die;
  51. struct knc_core_state {
  52. int generation;
  53. int core;
  54. struct knc_die *die;
  55. struct {
  56. int slot;
  57. struct work *work;
  58. } workslot[WORKS_PER_CORE]; /* active, next */
  59. int transfer_stamp;
  60. struct knc_report report;
  61. struct {
  62. int slot;
  63. uint32_t nonce;
  64. } last_nonce;
  65. uint32_t works;
  66. uint32_t shares;
  67. uint32_t errors;
  68. uint32_t completed;
  69. int last_slot;
  70. uint32_t errors_now;
  71. struct timeval disabled_until;
  72. struct timeval hold_work_until;
  73. struct timeval timeout;
  74. bool inuse;
  75. };
  76. struct knc_state;
  77. struct knc_die {
  78. int channel;
  79. int die;
  80. int version;
  81. int cores;
  82. struct cgpu_info *proc;
  83. struct knc_state *knc;
  84. struct knc_core_state *core;
  85. };
  86. #define MAX_SPI_SIZE (4096)
  87. #define MAX_SPI_RESPONSES (MAX_SPI_SIZE / (2 + 4 + 1 + 1 + 1 + 4))
  88. #define MAX_SPI_MESSAGE (128)
  89. #define KNC_SPI_BUFFERS (3)
  90. struct knc_state {
  91. void *ctx;
  92. int generation; /* work/block generation, incremented on each flush invalidating older works */
  93. int dies;
  94. struct knc_die die[KNC_MAX_ASICS * KNC_MAX_DIES_PER_ASIC];
  95. int cores;
  96. int scan_adjust;
  97. int startup;
  98. /* Statistics */
  99. uint64_t shares; /* diff1 shares reported by hardware */
  100. uint64_t works; /* Work units submitted */
  101. uint64_t completed; /* Work units completed */
  102. uint64_t errors; /* Hardware & communication errors */
  103. struct timeval next_error_interval;
  104. /* End of statistics */
  105. /* SPI communications thread */
  106. pthread_mutex_t spi_qlock; /* SPI queue status lock */
  107. struct thr_info spi_thr; /* SPI I/O thread */
  108. pthread_cond_t spi_qcond; /* SPI queue change wakeup */
  109. struct knc_spi_buffer {
  110. enum {
  111. KNC_SPI_IDLE=0,
  112. KNC_SPI_PENDING,
  113. KNC_SPI_DONE
  114. } state;
  115. int size;
  116. uint8_t txbuf[MAX_SPI_SIZE];
  117. uint8_t rxbuf[MAX_SPI_SIZE];
  118. int responses;
  119. struct knc_spi_response {
  120. int request_length;
  121. int response_length;
  122. enum {
  123. KNC_UNKNOWN = 0,
  124. KNC_NO_RESPONSE,
  125. KNC_SETWORK,
  126. KNC_REPORT,
  127. KNC_INFO
  128. } type;
  129. struct knc_core_state *core;
  130. uint32_t data;
  131. int offset;
  132. } response_info[MAX_SPI_RESPONSES];
  133. } spi_buffer[KNC_SPI_BUFFERS];
  134. int send_buffer;
  135. int read_buffer;
  136. int send_buffer_count;
  137. int read_buffer_count;
  138. /* end SPI thread */
  139. /* lock to protect resources between different threads */
  140. pthread_mutex_t state_lock;
  141. struct knc_core_state core[KNC_MAX_ASICS * KNC_MAX_DIES_PER_ASIC * KNC_MAX_CORES_PER_DIE];
  142. };
  143. int opt_knc_device_bus = -1;
  144. char *knc_log_file = NULL;
  145. static void *knc_spi(void *thr_data)
  146. {
  147. struct cgpu_info *cgpu = thr_data;
  148. struct knc_state *knc = cgpu->device_data;
  149. int buffer = 0;
  150. pthread_mutex_lock(&knc->spi_qlock);
  151. while (!cgpu->shutdown) {
  152. int this_buffer = buffer;
  153. while (knc->spi_buffer[buffer].state != KNC_SPI_PENDING && !cgpu->shutdown)
  154. pthread_cond_wait(&knc->spi_qcond, &knc->spi_qlock);
  155. pthread_mutex_unlock(&knc->spi_qlock);
  156. if (cgpu->shutdown)
  157. return NULL;
  158. knc_trnsp_transfer(knc->ctx, knc->spi_buffer[buffer].txbuf, knc->spi_buffer[buffer].rxbuf, knc->spi_buffer[buffer].size);
  159. buffer += 1;
  160. if (buffer >= KNC_SPI_BUFFERS)
  161. buffer = 0;
  162. pthread_mutex_lock(&knc->spi_qlock);
  163. knc->spi_buffer[this_buffer].state = KNC_SPI_DONE;
  164. pthread_cond_signal(&knc->spi_qcond);
  165. }
  166. pthread_mutex_unlock(&knc->spi_qlock);
  167. return NULL;
  168. }
  169. static void knc_process_responses(struct thr_info *thr);
  170. static void knc_flush(struct thr_info *thr)
  171. {
  172. struct cgpu_info *cgpu = thr->cgpu;
  173. struct knc_state *knc = cgpu->device_data;
  174. struct knc_spi_buffer *buffer = &knc->spi_buffer[knc->send_buffer];
  175. if (buffer->state == KNC_SPI_IDLE && buffer->size > 0) {
  176. pthread_mutex_lock(&knc->spi_qlock);
  177. buffer->state = KNC_SPI_PENDING;
  178. pthread_cond_signal(&knc->spi_qcond);
  179. knc->send_buffer += 1;
  180. knc->send_buffer_count += 1;
  181. if (knc->send_buffer >= KNC_SPI_BUFFERS)
  182. knc->send_buffer = 0;
  183. buffer = &knc->spi_buffer[knc->send_buffer];
  184. /* Block for SPI to finish a transfer if all buffers are busy */
  185. while (buffer->state == KNC_SPI_PENDING) {
  186. applog(LOG_DEBUG, "KnC: SPI buffer full (%d), waiting for SPI thread", buffer->responses);
  187. pthread_cond_wait(&knc->spi_qcond, &knc->spi_qlock);
  188. }
  189. pthread_mutex_unlock(&knc->spi_qlock);
  190. }
  191. knc_process_responses(thr);
  192. }
  193. static void knc_transfer(struct thr_info *thr, struct knc_core_state *core, int request_length, uint8_t *request, int response_length, int response_type, uint32_t data)
  194. {
  195. struct cgpu_info *cgpu = thr->cgpu;
  196. struct knc_state *knc = cgpu->device_data;
  197. struct knc_spi_buffer *buffer = &knc->spi_buffer[knc->send_buffer];
  198. /* FPGA control, request header, request body/response, CRC(4), ACK(1), EXTRA(3) */
  199. int msglen = 2 + max(request_length, 4 + response_length) + 4 + 1 + 3;
  200. if (buffer->size + msglen > MAX_SPI_SIZE || buffer->responses >= MAX_SPI_RESPONSES) {
  201. applog(LOG_INFO, "KnC: SPI buffer sent, %d messages %d bytes", buffer->responses, buffer->size);
  202. knc_flush(thr);
  203. buffer = &knc->spi_buffer[knc->send_buffer];
  204. }
  205. struct knc_spi_response *response_info = &buffer->response_info[buffer->responses];
  206. buffer->responses++;
  207. response_info->offset = buffer->size;
  208. response_info->type = response_type;
  209. response_info->request_length = request_length;
  210. response_info->response_length = response_length;
  211. response_info->core = core;
  212. response_info->data = data;
  213. buffer->size = knc_prepare_transfer(buffer->txbuf, buffer->size, MAX_SPI_SIZE, core->die->channel, request_length, request, response_length);
  214. }
  215. static int knc_transfer_stamp(struct knc_state *knc)
  216. {
  217. return knc->send_buffer_count;
  218. }
  219. static int knc_transfer_completed(struct knc_state *knc, int stamp)
  220. {
  221. /* signed delta math, counter wrap OK */
  222. return (int)(knc->read_buffer_count - stamp) >= 1;
  223. }
  224. static struct cgpu_info * all_cgpus[KNC_MAX_ASICS][KNC_MAX_DIES_PER_ASIC] = {{NULL}};
  225. /* Note: content of knc might be not initialized yet */
  226. static bool prealloc_all_cgpus(struct knc_state *knc)
  227. {
  228. int channel, die;
  229. struct cgpu_info *cgpu, *prev_cgpu;
  230. prev_cgpu = NULL;
  231. for (channel = 0; channel < KNC_MAX_ASICS; ++channel) {
  232. cgpu = all_cgpus[channel][0];
  233. if (NULL != cgpu)
  234. continue;
  235. cgpu = malloc(sizeof(*cgpu));
  236. if (NULL == cgpu)
  237. return false;
  238. *cgpu = (struct cgpu_info){
  239. .drv = &kncasic_drv,
  240. .name = "KnCminer",
  241. .procs = KNC_MAX_DIES_PER_ASIC,
  242. .threads = prev_cgpu ? 0 : 1,
  243. .device_data = knc,
  244. };
  245. if (!add_cgpu_slave(cgpu, prev_cgpu)) {
  246. free(cgpu);
  247. return false;
  248. }
  249. prev_cgpu = cgpu;
  250. die = 0;
  251. for_each_managed_proc(proc, cgpu) {
  252. proc->deven = DEV_DISABLED;
  253. all_cgpus[channel][die++] = proc;
  254. }
  255. }
  256. return true;
  257. }
  258. static struct cgpu_info * get_cgpu(int channel, int die)
  259. {
  260. if ((channel < 0) || (channel >= KNC_MAX_ASICS) || (die < 0) || (die >= KNC_MAX_DIES_PER_ASIC))
  261. return NULL;
  262. return all_cgpus[channel][die];
  263. }
  264. int knc_change_die_state(void* device_data, int asic_id, int die_id, bool enable)
  265. {
  266. int ret = 0;
  267. struct knc_state *knc = device_data;
  268. struct knc_die_info die_info = {};
  269. int die, next_die, core;
  270. applog(LOG_NOTICE, "KnC: %s die, ASIC id=%d, DIE id=%d", enable ? "enable" : "disable", asic_id, die_id);
  271. mutex_lock(&knc->state_lock);
  272. if (asic_id < 0 || asic_id >= KNC_MAX_ASICS || die_id < 0 || die_id >= KNC_MAX_DIES_PER_ASIC) {
  273. ret = EINVAL;
  274. goto out_unlock;
  275. }
  276. struct cgpu_info *proc = get_cgpu(asic_id, die_id);
  277. for (die = 0; die < knc->dies; ++die) {
  278. if (knc->die[die].channel != asic_id || knc->die[die].die != die_id)
  279. continue;
  280. if (!enable) {
  281. int slot, buffer, resp;
  282. int deleted_cores = knc->die[die].cores;
  283. knc->cores -= deleted_cores;
  284. --knc->dies;
  285. /* cgpu[0][0] must be always enabled */
  286. if ((asic_id != 0) || (die_id != 0))
  287. proc->deven = DEV_DISABLED;
  288. struct knc_core_state *pcore_to = knc->die[die].core;
  289. struct knc_core_state *pcore_from = pcore_to + knc->die[die].cores;
  290. struct knc_core_state *pcore;
  291. for (pcore = pcore_to; pcore < pcore_from; ++pcore) {
  292. for (slot = 0; slot < WORKS_PER_CORE; ++slot) {
  293. if (pcore->workslot[slot].work)
  294. KNC_FREE_WORK(pcore->workslot[slot].work);
  295. }
  296. }
  297. int core_move_count = &(knc->core[knc->cores]) - pcore_to;
  298. assert(core_move_count >= 0);
  299. memmove(pcore_to, pcore_from, core_move_count * sizeof(struct knc_core_state));
  300. struct knc_die *pdie_to = &(knc->die[die]);
  301. struct knc_die *pdie_from = pdie_to + 1;
  302. int die_move_count = knc->dies - die;
  303. assert(die_move_count >= 0);
  304. memmove(pdie_to, pdie_from, die_move_count * sizeof(struct knc_die));
  305. /* Now fix pointers */
  306. for (next_die = 0; next_die < knc->dies; ++next_die) {
  307. assert(knc->die[next_die].core != pcore_to);
  308. if (knc->die[next_die].core > pcore_to)
  309. knc->die[next_die].core -= deleted_cores;
  310. }
  311. for (core = 0; core < knc->cores; ++core) {
  312. assert(knc->core[core].die != pdie_to);
  313. if (knc->core[core].die > pdie_to)
  314. --(knc->core[core].die);
  315. }
  316. for (buffer = 0; buffer < KNC_SPI_BUFFERS; ++buffer) {
  317. for (resp = 0; resp < MAX_SPI_RESPONSES; ++resp) {
  318. if (knc->spi_buffer[buffer].response_info[resp].core < pcore_to)
  319. continue;
  320. if (knc->spi_buffer[buffer].response_info[resp].core < pcore_from) {
  321. knc->spi_buffer[buffer].response_info[resp].core = NULL;
  322. continue;
  323. }
  324. knc->spi_buffer[buffer].response_info[resp].core -= deleted_cores;
  325. }
  326. }
  327. }
  328. /* die was found */
  329. ret = 0;
  330. goto out_unlock;
  331. }
  332. /* die was not found */
  333. if (enable) {
  334. /* Send GETINFO to a die to detect if it is usable */
  335. if (knc_trnsp_asic_detect(knc->ctx, asic_id)) {
  336. if (knc_detect_die(knc->ctx, asic_id, die_id, &die_info) != 0) {
  337. ret = ENODEV;
  338. goto out_unlock;
  339. }
  340. } else {
  341. ret = ENODEV;
  342. goto out_unlock;
  343. }
  344. memset(&(knc->core[knc->cores]), 0, die_info.cores * sizeof(struct knc_core_state));
  345. int next_die = knc->dies;
  346. knc->die[next_die].channel = asic_id;
  347. knc->die[next_die].die = die_id;
  348. knc->die[next_die].version = die_info.version;
  349. knc->die[next_die].cores = die_info.cores;
  350. knc->die[next_die].core = &(knc->core[knc->cores]);
  351. knc->die[next_die].knc = knc;
  352. knc->die[next_die].proc = proc;
  353. for (core = 0; core < knc->die[next_die].cores; ++core) {
  354. knc->die[next_die].core[core].die = &knc->die[next_die];
  355. knc->die[next_die].core[core].core = core;
  356. }
  357. ++knc->dies;
  358. knc->cores += die_info.cores;
  359. proc_enable(proc);
  360. }
  361. out_unlock:
  362. mutex_unlock(&knc->state_lock);
  363. return ret;
  364. }
  365. static bool knc_detect_one(void *ctx)
  366. {
  367. /* Scan device for ASICs */
  368. int channel, die, cores = 0, core;
  369. struct knc_state *knc;
  370. struct knc_die_info die_info[KNC_MAX_ASICS][KNC_MAX_DIES_PER_ASIC];
  371. memset(die_info, 0, sizeof(die_info));
  372. /* Send GETINFO to each die to detect if it is usable */
  373. for (channel = 0; channel < KNC_MAX_ASICS; channel++) {
  374. if (!knc_trnsp_asic_detect(ctx, channel))
  375. continue;
  376. for (die = 0; die < KNC_MAX_DIES_PER_ASIC; die++) {
  377. if (knc_detect_die(ctx, channel, die, &die_info[channel][die]) == 0)
  378. cores += die_info[channel][die].cores;
  379. }
  380. }
  381. if (!cores) {
  382. applog(LOG_NOTICE, "no KnCminer cores found");
  383. return false;
  384. }
  385. applog(LOG_ERR, "Found a KnC miner with %d cores", cores);
  386. knc = calloc(1, sizeof(*knc));
  387. if (!knc) {
  388. err_nomem:
  389. applog(LOG_ERR, "KnC miner detected, but failed to allocate memory");
  390. return false;
  391. }
  392. if (!prealloc_all_cgpus(knc)) {
  393. free(knc);
  394. goto err_nomem;
  395. }
  396. knc->ctx = ctx;
  397. knc->generation = 1;
  398. /* Index all cores */
  399. struct cgpu_info *first_cgpu = NULL;
  400. int dies = 0;
  401. cores = 0;
  402. struct knc_core_state *pcore = knc->core;
  403. for (channel = 0; channel < KNC_MAX_ASICS; channel++) {
  404. for (die = 0; die < KNC_MAX_DIES_PER_ASIC; die++) {
  405. if (die_info[channel][die].cores) {
  406. knc->die[dies].channel = channel;
  407. knc->die[dies].die = die;
  408. knc->die[dies].version = die_info[channel][die].version;
  409. knc->die[dies].cores = die_info[channel][die].cores;
  410. knc->die[dies].core = pcore;
  411. knc->die[dies].knc = knc;
  412. knc->die[dies].proc = get_cgpu(channel, die);
  413. knc->die[dies].proc->deven = DEV_ENABLED;
  414. if (NULL == first_cgpu)
  415. first_cgpu = knc->die[dies].proc;
  416. for (core = 0; core < knc->die[dies].cores; core++) {
  417. knc->die[dies].core[core].die = &knc->die[dies];
  418. knc->die[dies].core[core].core = core;
  419. }
  420. cores += knc->die[dies].cores;
  421. pcore += knc->die[dies].cores;
  422. dies++;
  423. }
  424. }
  425. }
  426. knc->dies = dies;
  427. knc->cores = cores;
  428. knc->startup = 2;
  429. pthread_mutex_init(&knc->spi_qlock, NULL);
  430. pthread_cond_init(&knc->spi_qcond, NULL);
  431. pthread_mutex_init(&knc->state_lock, NULL);
  432. if (thr_info_create(&knc->spi_thr, NULL, knc_spi, first_cgpu)) {
  433. applog(LOG_ERR, "%s: SPI thread create failed", first_cgpu->dev_repr);
  434. free(knc);
  435. /* TODO: free all cgpus. We can not do it at the moment as there is no good
  436. * way to free all cgpu-related resources. */
  437. return false;
  438. }
  439. return true;
  440. }
  441. /* Probe devices and register with add_cgpu */
  442. static
  443. bool kncasic_detect_one(const char * const devpath)
  444. {
  445. void *ctx = knc_trnsp_new(devpath);
  446. if (ctx != NULL) {
  447. if (!knc_detect_one(ctx))
  448. knc_trnsp_free(ctx);
  449. else
  450. return true;
  451. }
  452. return false;
  453. }
  454. static
  455. int kncasic_detect_auto(void)
  456. {
  457. return kncasic_detect_one(NULL) ? 1 : 0;
  458. }
  459. static
  460. void kncasic_detect(void)
  461. {
  462. generic_detect(&kncasic_drv, kncasic_detect_one, kncasic_detect_auto, GDF_REQUIRE_DNAME | GDF_DEFAULT_NOAUTO);
  463. }
  464. static
  465. bool knc_init(struct thr_info * const thr)
  466. {
  467. int channel, die;
  468. struct cgpu_info *cgpu = thr->cgpu, *proc;
  469. struct knc_state *knc = cgpu->device_data;
  470. /* Set initial enable/disable state */
  471. bool die_detected[KNC_MAX_ASICS][KNC_MAX_DIES_PER_ASIC];
  472. memset(die_detected, 0, sizeof(die_detected));
  473. for (die = 0; die < knc->dies; ++die) {
  474. if (0 < knc->die[die].cores) {
  475. die_detected[knc->die[die].channel][knc->die[die].die] = true;
  476. }
  477. }
  478. /* cgpu[0][0] must be always enabled */
  479. die_detected[0][0] = true;
  480. for (channel = 0; channel < KNC_MAX_ASICS; ++channel) {
  481. for (die = 0; die < KNC_MAX_DIES_PER_ASIC; ++die) {
  482. proc = get_cgpu(channel, die);
  483. if (NULL != proc) {
  484. proc->deven = die_detected[channel][die] ? DEV_ENABLED : DEV_DISABLED;
  485. }
  486. }
  487. }
  488. return true;
  489. }
  490. /* Core helper functions */
  491. static int knc_core_hold_work(struct knc_core_state *core)
  492. {
  493. return timercmp(&core->hold_work_until, &now, >);
  494. }
  495. static int knc_core_has_work(struct knc_core_state *core)
  496. {
  497. int i;
  498. for (i = 0; i < WORKS_PER_CORE; i++) {
  499. if (core->workslot[i].slot > 0)
  500. return true;
  501. }
  502. return false;
  503. }
  504. static int knc_core_need_work(struct knc_core_state *core)
  505. {
  506. return !knc_core_hold_work(core) && !core->workslot[1].work && !core->workslot[2].work;
  507. }
  508. static int knc_core_disabled(struct knc_core_state *core)
  509. {
  510. return timercmp(&core->disabled_until, &now, >);
  511. }
  512. static int _knc_core_next_slot(struct knc_core_state *core)
  513. {
  514. /* Avoid slot #0 and #15. #0 is "no work assigned" and #15 is seen on bad cores */
  515. int slot = core->last_slot + 1;
  516. if (slot >= 15)
  517. slot = 1;
  518. core->last_slot = slot;
  519. return slot;
  520. }
  521. static bool knc_core_slot_busy(struct knc_core_state *core, int slot)
  522. {
  523. if (slot == core->report.active_slot)
  524. return true;
  525. if (slot == core->report.next_slot)
  526. return true;
  527. int i;
  528. for (i = 0; i < WORKS_PER_CORE; i++) {
  529. if (slot == core->workslot[i].slot)
  530. return true;
  531. }
  532. return false;
  533. }
  534. static int knc_core_next_slot(struct knc_core_state *core)
  535. {
  536. int slot;
  537. do slot = _knc_core_next_slot(core);
  538. while (knc_core_slot_busy(core, slot));
  539. return slot;
  540. }
  541. static void knc_core_failure(struct knc_core_state *core)
  542. {
  543. core->errors++;
  544. core->errors_now++;
  545. core->die->knc->errors++;
  546. if (knc_core_disabled(core))
  547. return;
  548. if (core->errors_now > CORE_ERROR_LIMIT) {
  549. struct cgpu_info * const proc = core->die->proc;
  550. applog(LOG_ERR, "%"PRIpreprv"[%d] disabled for %ld seconds due to repeated hardware errors",
  551. proc->proc_repr, core->core, (long)core_disable_interval.tv_sec);
  552. timeradd(&now, &core_disable_interval, &core->disabled_until);
  553. }
  554. }
  555. static
  556. void knc_core_handle_nonce(struct thr_info *thr, struct knc_core_state *core, int slot, uint32_t nonce)
  557. {
  558. int i;
  559. if (!slot)
  560. return;
  561. core->last_nonce.slot = slot;
  562. core->last_nonce.nonce = nonce;
  563. if (core->die->knc->startup)
  564. return;
  565. for (i = 0; i < WORKS_PER_CORE; i++) {
  566. if (slot == core->workslot[i].slot && core->workslot[i].work) {
  567. struct cgpu_info * const proc = core->die->proc;
  568. struct thr_info * const corethr = proc->thr[0];
  569. applog(LOG_INFO, "%"PRIpreprv"[%d] found nonce %08x", proc->proc_repr, core->core, nonce);
  570. if (submit_nonce(corethr, core->workslot[i].work, nonce)) {
  571. /* Good share */
  572. core->shares++;
  573. core->die->knc->shares++;
  574. hashes_done2(corethr, 0x100000000, NULL);
  575. /* This core is useful. Ignore any errors */
  576. core->errors_now = 0;
  577. } else {
  578. applog(LOG_INFO, "%"PRIpreprv"[%d] hwerror nonce %08x", proc->proc_repr, core->core, nonce);
  579. /* Bad share */
  580. knc_core_failure(core);
  581. }
  582. }
  583. }
  584. }
  585. static int knc_core_process_report(struct thr_info *thr, struct knc_core_state *core, uint8_t *response)
  586. {
  587. struct cgpu_info * const proc = core->die->proc;
  588. struct knc_report *report = &core->report;
  589. knc_decode_report(response, report, core->die->version);
  590. bool had_event = false;
  591. applog(LOG_DEBUG, "%"PRIpreprv"[%d]: Process report %d %d(%d) / %d %d %d", proc->proc_repr, core->core, report->active_slot, report->next_slot, report->next_state, core->workslot[0].slot, core->workslot[1].slot, core->workslot[2].slot);
  592. int n;
  593. for (n = 0; n < KNC_NONCES_PER_REPORT; n++) {
  594. if (report->nonce[n].slot < 0)
  595. break;
  596. if (core->last_nonce.slot == report->nonce[n].slot && core->last_nonce.nonce == report->nonce[n].nonce)
  597. break;
  598. }
  599. while(n-- > 0) {
  600. knc_core_handle_nonce(thr, core, report->nonce[n].slot, report->nonce[n].nonce);
  601. }
  602. if (report->active_slot && core->workslot[0].slot != report->active_slot) {
  603. had_event = true;
  604. applog(LOG_INFO, "%"PRIpreprv"[%d]: New work %d %d / %d %d %d", proc->proc_repr, core->core, report->active_slot, report->next_slot, core->workslot[0].slot, core->workslot[1].slot, core->workslot[2].slot);
  605. /* Core switched to next work */
  606. if (core->workslot[0].work) {
  607. core->die->knc->completed++;
  608. core->completed++;
  609. applog(LOG_INFO, "%"PRIpreprv"[%d]: Work completed!", proc->proc_repr, core->core);
  610. KNC_FREE_WORK(core->workslot[0].work);
  611. }
  612. core->workslot[0] = core->workslot[1];
  613. core->workslot[1].work = NULL;
  614. core->workslot[1].slot = -1;
  615. /* or did it switch directly to pending work? */
  616. if (report->active_slot == core->workslot[2].slot) {
  617. applog(LOG_INFO, "%"PRIpreprv"[%d]: New work %d %d %d %d (pending)", proc->proc_repr, core->core, report->active_slot, core->workslot[0].slot, core->workslot[1].slot, core->workslot[2].slot);
  618. if (core->workslot[0].work)
  619. KNC_FREE_WORK(core->workslot[0].work);
  620. core->workslot[0] = core->workslot[2];
  621. core->workslot[2].work = NULL;
  622. core->workslot[2].slot = -1;
  623. }
  624. }
  625. if (report->next_state && core->workslot[2].slot > 0 && (core->workslot[2].slot == report->next_slot || report->next_slot == -1)) {
  626. had_event = true;
  627. applog(LOG_INFO, "%"PRIpreprv"[%d]: Accepted work %d %d %d %d (pending)", proc->proc_repr, core->core, report->active_slot, core->workslot[0].slot, core->workslot[1].slot, core->workslot[2].slot);
  628. /* core accepted next work */
  629. if (core->workslot[1].work)
  630. KNC_FREE_WORK(core->workslot[1].work);
  631. core->workslot[1] = core->workslot[2];
  632. core->workslot[2].work = NULL;
  633. core->workslot[2].slot = -1;
  634. }
  635. if (core->workslot[2].work && knc_transfer_completed(core->die->knc, core->transfer_stamp)) {
  636. had_event = true;
  637. applog(LOG_INFO, "%"PRIpreprv"[%d]: Setwork failed?", proc->proc_repr, core->core);
  638. KNC_FREE_WORK(core->workslot[2].work);
  639. core->workslot[2].slot = -1;
  640. }
  641. if (had_event)
  642. applog(LOG_INFO, "%"PRIpreprv"[%d]: Exit report %d %d / %d %d %d", proc->proc_repr, core->core, report->active_slot, report->next_slot, core->workslot[0].slot, core->workslot[1].slot, core->workslot[2].slot);
  643. return 0;
  644. }
  645. static void knc_process_responses(struct thr_info *thr)
  646. {
  647. struct cgpu_info *cgpu = thr->cgpu;
  648. struct knc_state *knc = cgpu->device_data;
  649. struct knc_spi_buffer *buffer = &knc->spi_buffer[knc->read_buffer];
  650. while (buffer->state == KNC_SPI_DONE) {
  651. int i;
  652. for (i = 0; i < buffer->responses; i++) {
  653. struct knc_spi_response *response_info = &buffer->response_info[i];
  654. uint8_t *rxbuf = &buffer->rxbuf[response_info->offset];
  655. struct knc_core_state *core = response_info->core;
  656. if (NULL == core) /* core was deleted, e.g. by API call */
  657. continue;
  658. struct cgpu_info * const proc = core->die->proc;
  659. int status = knc_decode_response(rxbuf, response_info->request_length, &rxbuf, response_info->response_length);
  660. /* Invert KNC_ACCEPTED to simplify logics below */
  661. if (response_info->type == KNC_SETWORK && !KNC_IS_ERROR(status))
  662. status ^= KNC_ACCEPTED;
  663. if (core->die->version != KNC_VERSION_JUPITER && status != 0) {
  664. applog(LOG_ERR, "%"PRIpreprv"[%d]: Communication error (%x / %d)", proc->proc_repr, core->core, status, i);
  665. if (status == KNC_ACCEPTED) {
  666. /* Core refused our work vector. Likely out of sync. Reset it */
  667. core->inuse = false;
  668. }
  669. knc_core_failure(core);
  670. }
  671. switch(response_info->type) {
  672. case KNC_REPORT:
  673. case KNC_SETWORK:
  674. /* Should we care about failed SETWORK explicit? Or simply handle it by next state not loaded indication in reports? */
  675. knc_core_process_report(thr, core, rxbuf);
  676. break;
  677. default:
  678. break;
  679. }
  680. }
  681. buffer->state = KNC_SPI_IDLE;
  682. buffer->responses = 0;
  683. buffer->size = 0;
  684. knc->read_buffer += 1;
  685. knc->read_buffer_count += 1;
  686. if (knc->read_buffer >= KNC_SPI_BUFFERS)
  687. knc->read_buffer = 0;
  688. buffer = &knc->spi_buffer[knc->read_buffer];
  689. }
  690. }
  691. static int knc_core_send_work(struct thr_info *thr, struct knc_core_state *core, struct work *work, bool clean)
  692. {
  693. struct knc_state *knc = core->die->knc;
  694. struct cgpu_info * const proc = core->die->proc;
  695. int request_length = 4 + 1 + 6*4 + 3*4 + 8*4;
  696. uint8_t request[request_length];
  697. int response_length = 1 + 1 + (1 + 4) * 5;
  698. int slot = knc_core_next_slot(core);
  699. if (slot < 0)
  700. goto error;
  701. applog(LOG_INFO, "%"PRIpreprv"[%d] setwork%s = %d, %d %d / %d %d %d", proc->proc_repr, core->core, clean ? " CLEAN" : "", slot, core->report.active_slot, core->report.next_slot, core->workslot[0].slot, core->workslot[1].slot, core->workslot[2].slot);
  702. if (!clean && !knc_core_need_work(core))
  703. goto error;
  704. switch(core->die->version) {
  705. case KNC_VERSION_JUPITER:
  706. if (clean) {
  707. /* Double halt to get rid of any previous queued work */
  708. request_length = knc_prepare_jupiter_halt(request, core->die->die, core->core);
  709. knc_transfer(thr, core, request_length, request, 0, KNC_NO_RESPONSE, 0);
  710. knc_transfer(thr, core, request_length, request, 0, KNC_NO_RESPONSE, 0);
  711. }
  712. request_length = knc_prepare_jupiter_setwork(request, core->die->die, core->core, slot, work);
  713. knc_transfer(thr, core, request_length, request, 0, KNC_NO_RESPONSE, 0);
  714. break;
  715. case KNC_VERSION_NEPTUNE:
  716. request_length = knc_prepare_neptune_setwork(request, core->die->die, core->core, slot, work, clean);
  717. knc_transfer(thr, core, request_length, request, response_length, KNC_SETWORK, slot);
  718. break;
  719. default:
  720. goto error;
  721. }
  722. if (core->workslot[2].work)
  723. KNC_FREE_WORK(core->workslot[2].work);
  724. core->workslot[2].work = work;
  725. core->workslot[2].slot = slot;
  726. core->works++;
  727. core->die->knc->works++;
  728. core->transfer_stamp = knc_transfer_stamp(knc);
  729. core->inuse = true;
  730. timeradd(&now, &core_submit_interval, &core->hold_work_until);
  731. timeradd(&now, &core_timeout_interval, &core->timeout);
  732. return 0;
  733. error:
  734. applog(LOG_INFO, "%"PRIpreprv"[%d]: Failed to setwork (%d)", proc->proc_repr, core->core, core->errors_now);
  735. knc_core_failure(core);
  736. KNC_FREE_WORK(work);
  737. return -1;
  738. }
  739. static int knc_core_request_report(struct thr_info *thr, struct knc_core_state *core)
  740. {
  741. struct cgpu_info * const proc = core->die->proc;
  742. int request_length = 4;
  743. uint8_t request[request_length];
  744. int response_length = 1 + 1 + (1 + 4) * 5;
  745. applog(LOG_DEBUG, "%"PRIpreprv"[%d]: Request report", proc->proc_repr, core->core);
  746. request_length = knc_prepare_report(request, core->die->die, core->core);
  747. switch(core->die->version) {
  748. case KNC_VERSION_JUPITER:
  749. response_length = 1 + 1 + (1 + 4);
  750. knc_transfer(thr, core, request_length, request, response_length, KNC_REPORT, 0);
  751. return 0;
  752. case KNC_VERSION_NEPTUNE:
  753. knc_transfer(thr, core, request_length, request, response_length, KNC_REPORT, 0);
  754. return 0;
  755. }
  756. applog(LOG_INFO, "%"PRIpreprv"[%d]: Failed to scan work report", proc->proc_repr, core->core);
  757. knc_core_failure(core);
  758. return -1;
  759. }
  760. /* return value is number of nonces that have been checked since
  761. * previous call
  762. */
  763. static int64_t knc_scanwork(struct thr_info *thr)
  764. {
  765. struct cgpu_info *cgpu = thr->cgpu;
  766. struct knc_state *knc = cgpu->device_data;
  767. applog(LOG_DEBUG, "KnC running scanwork");
  768. mutex_lock(&knc->state_lock);
  769. gettimeofday(&now, NULL);
  770. knc_trnsp_periodic_check(knc->ctx);
  771. int i;
  772. knc_process_responses(thr);
  773. if (timercmp(&knc->next_error_interval, &now, >)) {
  774. /* Reset hw error limiter every check interval */
  775. timeradd(&now, &core_check_interval, &knc->next_error_interval);
  776. for (i = 0; i < knc->cores; i++) {
  777. struct knc_core_state *core = &knc->core[i];
  778. core->errors_now = 0;
  779. }
  780. }
  781. for (i = 0; i < knc->cores; i++) {
  782. struct knc_core_state *core = &knc->core[i];
  783. struct cgpu_info * const proc = core->die->proc;
  784. bool clean = !core->inuse;
  785. if (knc_core_disabled(core))
  786. continue;
  787. if (core->generation != knc->generation) {
  788. applog(LOG_INFO, "%"PRIpreprv"[%d] flush gen=%d/%d", proc->proc_repr, core->core, core->generation, knc->generation);
  789. /* clean set state, forget everything */
  790. int slot;
  791. for (slot = 0; slot < WORKS_PER_CORE; slot ++) {
  792. if (core->workslot[slot].work)
  793. KNC_FREE_WORK(core->workslot[slot].work);
  794. core->workslot[slot].slot = -1;
  795. }
  796. core->hold_work_until = now;
  797. core->generation = knc->generation;
  798. } else if (timercmp(&core->timeout, &now, <=) && (core->workslot[0].slot > 0 || core->workslot[1].slot > 0 || core->workslot[2].slot > 0)) {
  799. applog(LOG_ERR, "%"PRIpreprv"[%d] timeout gen=%d/%d", proc->proc_repr, core->core, core->generation, knc->generation);
  800. clean = true;
  801. }
  802. if (!knc_core_has_work(core))
  803. clean = true;
  804. if (core->workslot[0].slot < 0 && core->workslot[1].slot < 0 && core->workslot[2].slot < 0)
  805. clean = true;
  806. if (i % SCAN_ADJUST_RANGE == knc->scan_adjust)
  807. clean = true;
  808. if ((knc_core_need_work(core) || clean) && !knc->startup) {
  809. struct work *work = get_work(thr);
  810. knc_core_send_work(thr, core, work, clean);
  811. } else {
  812. knc_core_request_report(thr, core);
  813. }
  814. }
  815. /* knc->startup delays initial work submission until we have had chance to query all cores on their current status, to avoid slot number collisions with earlier run */
  816. if (knc->startup)
  817. knc->startup--;
  818. else if (knc->scan_adjust < SCAN_ADJUST_RANGE)
  819. knc->scan_adjust++;
  820. knc_flush(thr);
  821. mutex_unlock(&knc->state_lock);
  822. return 0;
  823. }
  824. static void knc_flush_work(struct cgpu_info *cgpu)
  825. {
  826. struct knc_state *knc = cgpu->device_data;
  827. applog(LOG_INFO, "KnC running flushwork");
  828. mutex_lock(&knc->state_lock);
  829. knc->generation++;
  830. knc->scan_adjust=0;
  831. if (!knc->generation)
  832. knc->generation++;
  833. mutex_unlock(&knc->state_lock);
  834. }
  835. static void knc_zero_stats(struct cgpu_info *cgpu)
  836. {
  837. int core;
  838. struct knc_state *knc = cgpu->device_data;
  839. mutex_lock(&knc->state_lock);
  840. for (core = 0; core < knc->cores; core++) {
  841. knc->shares = 0;
  842. knc->completed = 0;
  843. knc->works = 0;
  844. knc->errors = 0;
  845. knc->core[core].works = 0;
  846. knc->core[core].errors = 0;
  847. knc->core[core].shares = 0;
  848. knc->core[core].completed = 0;
  849. }
  850. mutex_unlock(&knc->state_lock);
  851. }
  852. static struct api_data *knc_api_stats(struct cgpu_info *cgpu)
  853. {
  854. struct knc_state *knc = cgpu->device_data;
  855. struct knc_core_state * const proccore = &knc->core[cgpu->proc_id];
  856. struct knc_die * const die = proccore->die;
  857. struct api_data *root = NULL;
  858. int core;
  859. char label[256];
  860. mutex_lock(&knc->state_lock);
  861. root = api_add_int(root, "dies", &knc->dies, 1);
  862. root = api_add_int(root, "cores", &knc->cores, 1);
  863. root = api_add_uint64(root, "shares", &knc->shares, 1);
  864. root = api_add_uint64(root, "works", &knc->works, 1);
  865. root = api_add_uint64(root, "completed", &knc->completed, 1);
  866. root = api_add_uint64(root, "errors", &knc->errors, 1);
  867. /* Active cores */
  868. int active = knc->cores;
  869. for (core = 0; core < knc->cores; core++) {
  870. if (knc_core_disabled(&knc->core[core]))
  871. active -= 1;
  872. }
  873. root = api_add_int(root, "active", &active, 1);
  874. /* Per ASIC/die data */
  875. {
  876. #define knc_api_die_string(name, value) do { \
  877. snprintf(label, sizeof(label), "%d.%d.%s", die->channel, die->die, name); \
  878. root = api_add_string(root, label, value, 1); \
  879. } while(0)
  880. #define knc_api_die_int(name, value) do { \
  881. snprintf(label, sizeof(label), "%d.%d.%s", die->channel, die->die, name); \
  882. uint64_t v = value; \
  883. root = api_add_uint64(root, label, &v, 1); \
  884. } while(0)
  885. /* Model */
  886. {
  887. char *model = "?";
  888. switch(die->version) {
  889. case KNC_VERSION_JUPITER:
  890. model = "Jupiter";
  891. break;
  892. case KNC_VERSION_NEPTUNE:
  893. model = "Neptune";
  894. break;
  895. }
  896. knc_api_die_string("model", model);
  897. knc_api_die_int("cores", die->cores);
  898. }
  899. /* Core based stats */
  900. {
  901. uint64_t errors = 0;
  902. uint64_t shares = 0;
  903. uint64_t works = 0;
  904. uint64_t completed = 0;
  905. char coremap[die->cores+1];
  906. for (core = 0; core < die->cores; core++) {
  907. coremap[core] = knc_core_disabled(&die->core[core]) ? '0' : '1';
  908. works += die->core[core].works;
  909. shares += die->core[core].shares;
  910. errors += die->core[core].errors;
  911. completed += die->core[core].completed;
  912. }
  913. coremap[die->cores] = '\0';
  914. knc_api_die_int("errors", errors);
  915. knc_api_die_int("shares", shares);
  916. knc_api_die_int("works", works);
  917. knc_api_die_int("completed", completed);
  918. knc_api_die_string("coremap", coremap);
  919. }
  920. }
  921. mutex_unlock(&knc->state_lock);
  922. return root;
  923. }
  924. static
  925. void hash_driver_work(struct thr_info * const thr)
  926. {
  927. struct cgpu_info * const cgpu = thr->cgpu;
  928. struct device_drv * const drv = cgpu->drv;
  929. while (likely(!cgpu->shutdown))
  930. {
  931. drv->scanwork(thr);
  932. if (unlikely(thr->pause || cgpu->deven != DEV_ENABLED))
  933. mt_disable(thr);
  934. }
  935. }
  936. struct device_drv kncasic_drv = {
  937. .dname = "kncasic",
  938. .name = "KNC",
  939. .drv_detect = kncasic_detect,
  940. .thread_init = knc_init,
  941. .minerloop = hash_driver_work,
  942. .flush_work = knc_flush_work,
  943. .scanwork = knc_scanwork,
  944. .zero_stats = knc_zero_stats,
  945. .get_api_stats = knc_api_stats,
  946. };