driver-titan.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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 <fcntl.h>
  11. #include <sys/ioctl.h>
  12. #include "deviceapi.h"
  13. #include "logging.h"
  14. #include "miner.h"
  15. #include "util.h"
  16. #include "titan-asic.h"
  17. #define KNC_TITAN_DEFAULT_FREQUENCY 275
  18. #define KNC_TITAN_HWERR_DISABLE_SECS 10
  19. #define KNC_POLL_INTERVAL_US 10000
  20. #define DIE_HEALTH_INTERVAL_SEC 20
  21. /* Broadcast address to all cores in a die */
  22. #define ALL_CORES 0xFFFF
  23. /* Work queue pre-fill level.
  24. * Must be high enough to supply all ASICs with works after a flush */
  25. #define WORK_QUEUE_PREFILL 20
  26. #define MANUAL_CHECK_CORES_PER_POLL 100
  27. /* Specify here minimum number of leading zeroes in hash */
  28. #define DEFAULT_DIFF_FILTERING_ZEROES 24
  29. #define DEFAULT_DIFF_FILTERING_FLOAT (1. / ((double)(0x00000000FFFFFFFF >> DEFAULT_DIFF_FILTERING_ZEROES)))
  30. #define DEFAULT_DIFF_HASHES_PER_NONCE (1 << DEFAULT_DIFF_FILTERING_ZEROES)
  31. BFG_REGISTER_DRIVER(knc_titan_drv)
  32. /* 3 - default number of threads per core */
  33. static int opt_knc_threads_per_core = 3;
  34. static const struct bfg_set_device_definition knc_titan_set_device_funcs[];
  35. struct knc_titan_core {
  36. int asicno;
  37. int dieno; /* inside asic */
  38. int coreno; /* inside die */
  39. struct knc_titan_die *die;
  40. struct knc_titan_core *next_core;
  41. int hwerr_in_row;
  42. int hwerr_disable_time;
  43. struct timeval enable_at;
  44. struct timeval first_hwerr;
  45. struct nonce_report last_nonce;
  46. bool need_manual_check;
  47. };
  48. struct knc_titan_die {
  49. int asicno;
  50. int dieno; /* inside asic */
  51. int cores;
  52. struct cgpu_info *proc;
  53. struct knc_titan_core *first_core;
  54. bool need_flush;
  55. int next_slot;
  56. /* First slot after flush. If next_slot reaches this, then
  57. * we need to re-flush all the cores to avoid duplicating slot numbers
  58. * for different works */
  59. int first_slot;
  60. struct timeval last_share;
  61. /* Don't use this! DC/DCs don't like broadcast urgent setworks */
  62. bool broadcast_flushes;
  63. uint64_t hashes_done;
  64. /* We store history of hash counters for the last minute (12*5 = 60 s) */
  65. #define HASHES_BUF_ONE_ENTRY_TIME 5
  66. #define HASHES_BUF_ENTRIES 12
  67. uint64_t hashes_buf[HASHES_BUF_ENTRIES];
  68. int hashes_buf_idx; /* next write position === the oldest data stored */
  69. int freq;
  70. int manual_check_count;
  71. };
  72. struct knc_titan_info {
  73. void *ctx;
  74. struct cgpu_info *cgpu;
  75. int cores;
  76. struct knc_titan_die dies[KNC_TITAN_MAX_ASICS][KNC_TITAN_DIES_PER_ASIC];
  77. bool asic_served_by_fpga[KNC_TITAN_MAX_ASICS];
  78. struct timeval tv_prev;
  79. struct work *workqueue;
  80. int workqueue_size;
  81. int workqueue_max;
  82. int next_id;
  83. struct work *devicework;
  84. };
  85. static void knc_titan_zero_stats(struct cgpu_info *cgpu)
  86. {
  87. if (cgpu->device != cgpu)
  88. return;
  89. int asic = ((struct knc_titan_die *)cgpu->thr[0]->cgpu_data)->asicno;
  90. struct knc_titan_info *knc = cgpu->device_data;
  91. struct knc_titan_die *die;
  92. int dieno;
  93. for (dieno = 0; dieno < KNC_TITAN_DIES_PER_ASIC; ++dieno) {
  94. die = &(knc->dies[asic][dieno]);
  95. die->hashes_done = 0;
  96. memset(die->hashes_buf, 0, sizeof(die->hashes_buf));
  97. }
  98. }
  99. static double knc_titan_get_device_rolling_hashrate(struct cgpu_info *device)
  100. {
  101. int asic = ((struct knc_titan_die *)device->thr[0]->cgpu_data)->asicno;
  102. struct knc_titan_info *knc = device->device_data;
  103. double hashrate = 0.0;
  104. int dieno, i;
  105. for (dieno = 0; dieno < KNC_TITAN_DIES_PER_ASIC; ++dieno) {
  106. for (i = 0; i < HASHES_BUF_ENTRIES; ++i) {
  107. hashrate += (double)knc->dies[asic][dieno].hashes_buf[i] / 1.0e6;
  108. }
  109. }
  110. return hashrate / ((double)(HASHES_BUF_ENTRIES * HASHES_BUF_ONE_ENTRY_TIME));
  111. }
  112. static void knc_titan_die_hashmeter(struct knc_titan_die *die, uint64_t hashes_done)
  113. {
  114. struct timeval tv_now;
  115. cgtime(&tv_now);
  116. int cur_idx = (tv_now.tv_sec / HASHES_BUF_ONE_ENTRY_TIME) % HASHES_BUF_ENTRIES;
  117. if (die->hashes_buf_idx == cur_idx) {
  118. die->hashes_done += hashes_done;
  119. return;
  120. }
  121. die->hashes_buf[die->hashes_buf_idx] = die->hashes_done;
  122. die->hashes_done = hashes_done;
  123. die->hashes_buf_idx = (die->hashes_buf_idx + 1) % HASHES_BUF_ENTRIES;
  124. while (die->hashes_buf_idx != cur_idx) {
  125. die->hashes_buf[die->hashes_buf_idx] = 0;
  126. die->hashes_buf_idx = (die->hashes_buf_idx + 1) % HASHES_BUF_ENTRIES;
  127. }
  128. }
  129. static bool knc_titan_detect_one(const char *devpath)
  130. {
  131. static struct cgpu_info *prev_cgpu = NULL;
  132. struct cgpu_info *cgpu;
  133. void *ctx;
  134. struct knc_titan_info *knc;
  135. int cores = 0, asic, die, dies = 0;
  136. struct knc_die_info die_info;
  137. char repr[6];
  138. cgpu = malloc(sizeof(*cgpu));
  139. if (unlikely(!cgpu))
  140. quit(1, "Failed to alloc cgpu_info");
  141. if (!prev_cgpu) {
  142. if (NULL == (ctx = knc_trnsp_new(NULL))) {
  143. free(cgpu);
  144. return false;
  145. }
  146. knc = calloc(1, sizeof(*knc));
  147. if (unlikely(!knc))
  148. quit(1, "Failed to alloc knc_titan_info");
  149. knc->ctx = ctx;
  150. knc->cgpu = cgpu;
  151. knc->workqueue_max = WORK_QUEUE_PREFILL;
  152. } else {
  153. knc = prev_cgpu->device_data;
  154. ctx = knc->ctx;
  155. }
  156. snprintf(repr, sizeof(repr), "%s %s", knc_titan_drv.name, devpath);
  157. asic = atoi(devpath);
  158. for (die = 0; die < KNC_TITAN_DIES_PER_ASIC; ++die) {
  159. die_info.cores = KNC_TITAN_CORES_PER_DIE; /* core hint */
  160. die_info.version = KNC_VERSION_TITAN;
  161. if (!knc_titan_get_info(repr, ctx, asic, die, &die_info))
  162. die_info.cores = -1;
  163. if (0 < die_info.cores) {
  164. knc->dies[asic][die] = (struct knc_titan_die) {
  165. .asicno = asic,
  166. .dieno = die,
  167. .cores = die_info.cores,
  168. .proc = cgpu,
  169. .first_core = NULL,
  170. .freq = KNC_TITAN_DEFAULT_FREQUENCY,
  171. };
  172. cores += die_info.cores;
  173. dies++;
  174. } else {
  175. knc->dies[asic][die] = (struct knc_titan_die) {
  176. .asicno = -INT_MAX,
  177. .dieno = -INT_MAX,
  178. .cores = 0,
  179. .proc = NULL,
  180. .first_core = NULL,
  181. };
  182. }
  183. }
  184. if (0 == cores) {
  185. free(cgpu);
  186. if (!prev_cgpu) {
  187. free(knc);
  188. knc_trnsp_free(ctx);
  189. }
  190. return false;
  191. }
  192. applog(LOG_NOTICE, "%s: Found ASIC with %d dies and %d cores", repr, dies, cores);
  193. *cgpu = (struct cgpu_info) {
  194. .drv = &knc_titan_drv,
  195. .device_path = strdup(devpath),
  196. .set_device_funcs = knc_titan_set_device_funcs,
  197. .deven = DEV_ENABLED,
  198. .procs = dies,
  199. .threads = prev_cgpu ? 0 : 1,
  200. .extra_work_queue = -1,
  201. .device_data = knc,
  202. };
  203. const bool rv = add_cgpu_slave(cgpu, prev_cgpu);
  204. if (!prev_cgpu) {
  205. cgpu->extra_work_queue += WORK_QUEUE_PREFILL - opt_queue;
  206. if (0 > cgpu->extra_work_queue)
  207. cgpu->extra_work_queue = 0;
  208. }
  209. prev_cgpu = cgpu;
  210. return rv;
  211. }
  212. static int knc_titan_detect_auto(void)
  213. {
  214. const int first = 0, last = KNC_TITAN_MAX_ASICS - 1;
  215. char devpath[256];
  216. int found = 0, i;
  217. for (i = first; i <= last; ++i) {
  218. sprintf(devpath, "%d", i);
  219. if (knc_titan_detect_one(devpath))
  220. ++found;
  221. }
  222. return found;
  223. }
  224. static void knc_titan_detect(void)
  225. {
  226. generic_detect(&knc_titan_drv, knc_titan_detect_one, knc_titan_detect_auto, GDF_REQUIRE_DNAME | GDF_DEFAULT_NOAUTO);
  227. }
  228. static void knc_titan_clean_flush(const char *repr, void * const ctx, struct knc_titan_core *knccore)
  229. {
  230. struct knc_report report;
  231. bool unused;
  232. if (knc_titan_set_work(repr, ctx, knccore->asicno, knccore->dieno, knccore->coreno, 0, NULL, true, &unused, &report)) {
  233. knccore->last_nonce.slot = report.nonce[0].slot;
  234. knccore->last_nonce.nonce = report.nonce[0].nonce;
  235. }
  236. }
  237. static uint32_t nonce_tops[KNC_TITAN_CORES_PER_DIE];
  238. static bool nonce_tops_inited = false;
  239. static void get_nonce_range(int dieno, int coreno, uint32_t *nonce_bottom, uint32_t *nonce_top)
  240. {
  241. if (!nonce_tops_inited) {
  242. uint32_t top;
  243. double nonce_f, nonce_step;
  244. int core;
  245. nonce_f = 0.0;
  246. nonce_step = 4294967296.0 / KNC_TITAN_CORES_PER_DIE;
  247. for (core = 0; core < KNC_TITAN_CORES_PER_DIE; ++core) {
  248. nonce_f += nonce_step;
  249. if (core < (KNC_TITAN_CORES_PER_DIE - 1))
  250. top = nonce_f;
  251. else
  252. top = 0xFFFFFFFF;
  253. nonce_tops[core] = top;
  254. }
  255. nonce_tops_inited = true;
  256. }
  257. *nonce_top = nonce_tops[coreno];
  258. if (coreno > 0) {
  259. *nonce_bottom = nonce_tops[coreno - 1] + 1;
  260. return;
  261. }
  262. *nonce_bottom = 0;
  263. }
  264. static bool configure_one_die(struct knc_titan_info *knc, int asic, int die)
  265. {
  266. struct cgpu_info *proc, *first_proc;
  267. struct thr_info *mythr;
  268. struct knc_titan_core *knccore;
  269. char *repr;
  270. struct knc_titan_die *die_p;
  271. if ((0 > asic) || (KNC_TITAN_MAX_ASICS <= asic) || (0 > die) || (KNC_TITAN_DIES_PER_ASIC <= die))
  272. return false;
  273. die_p = &(knc->dies[asic][die]);
  274. if (0 >= die_p->cores)
  275. return false;
  276. /* Init nonce ranges for cores */
  277. struct titan_setup_core_params setup_params = {
  278. .bad_address_mask = {0, 0},
  279. .bad_address_match = {0x3FF, 0x3FF},
  280. .difficulty = DEFAULT_DIFF_FILTERING_ZEROES - 1,
  281. .thread_enable = 0xFF,
  282. .thread_base_address = {0, 1, 2, 3, 4, 5, 6, 7},
  283. .lookup_gap_mask = {0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7},
  284. .N_mask = {0, 0, 0, 0, 0, 0, 0, 0},
  285. .N_shift = {0, 0, 0, 0, 0, 0, 0, 0},
  286. .nonce_bottom = 0,
  287. .nonce_top = 0xFFFFFFFF,
  288. };
  289. fill_in_thread_params(opt_knc_threads_per_core, &setup_params);
  290. repr = die_p->proc->device->dev_repr;
  291. bool success = true;
  292. for (knccore = die_p->first_core ; knccore ; knccore = knccore->next_core) {
  293. knc_titan_clean_flush(repr, knc->ctx, knccore);
  294. get_nonce_range(knccore->dieno, knccore->coreno, &setup_params.nonce_bottom, &setup_params.nonce_top);
  295. applog(LOG_DEBUG, "%s[%d:%d:%d]: Setup core, nonces 0x%08X - 0x%08X", repr, knccore->asicno, knccore->dieno, knccore->coreno, setup_params.nonce_bottom, setup_params.nonce_top);
  296. if (!knc_titan_setup_core_local(repr, knc->ctx, knccore->asicno, knccore->dieno, knccore->coreno, &setup_params))
  297. success = false;
  298. }
  299. applog(LOG_NOTICE, "%s[%d-%d] Die configur%s", repr, asic, die, success ? "ed successfully" : "ation failed");
  300. die_p->need_flush = true;
  301. timer_set_now(&(die_p->last_share));
  302. die_p->broadcast_flushes = false;
  303. die_p->manual_check_count = 0;
  304. return true;
  305. }
  306. static
  307. float titan_min_nonce_diff(struct cgpu_info * const proc, const struct mining_algorithm * const malgo)
  308. {
  309. return (malgo->algo == POW_SCRYPT) ? DEFAULT_DIFF_FILTERING_FLOAT : -1.;
  310. }
  311. static bool knc_titan_init(struct thr_info * const thr)
  312. {
  313. const int max_cores = KNC_TITAN_CORES_PER_ASIC;
  314. struct thr_info *mythr;
  315. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  316. struct knc_titan_core *knccore;
  317. struct knc_titan_die *kncdie;
  318. struct knc_titan_info *knc;
  319. int i, asic, die, core_base;
  320. int total_cores = 0;
  321. int asic_cores[KNC_TITAN_MAX_ASICS] = {0};
  322. knc = cgpu->device_data;
  323. for (proc = cgpu ; proc ; proc = proc->next_proc) {
  324. proc->device_data = knc;
  325. if (proc->device == proc) {
  326. asic = atoi(proc->device_path);
  327. die = 0;
  328. knc->asic_served_by_fpga[asic] = true;
  329. } else {
  330. die++;
  331. }
  332. kncdie = ((struct thr_info *)proc->thr[0])->cgpu_data = &knc->dies[asic][die];
  333. for (i = 0 ; i < KNC_TITAN_CORES_PER_DIE ; i++) {
  334. if (i == 0) {
  335. kncdie->first_core = malloc(sizeof(*knccore));
  336. knccore = kncdie->first_core;
  337. } else {
  338. knccore->next_core = malloc(sizeof(*knccore));
  339. knccore = knccore->next_core;
  340. }
  341. if (unlikely(!knccore))
  342. quit(1, "Failed to alloc knc_titan_core");
  343. *knccore = (struct knc_titan_core) {
  344. .asicno = asic,
  345. .dieno = die,
  346. .coreno = i,
  347. .die = &(knc->dies[asic][die]),
  348. .hwerr_in_row = 0,
  349. .hwerr_disable_time = KNC_TITAN_HWERR_DISABLE_SECS,
  350. .need_manual_check = false,
  351. };
  352. timer_set_now(&knccore->enable_at);
  353. ++total_cores;
  354. ++(asic_cores[asic]);
  355. }
  356. knccore->next_core = NULL;
  357. }
  358. knc->cores = total_cores;
  359. cgpu_set_defaults(cgpu);
  360. cgpu_setup_control_requests(cgpu);
  361. if (0 >= total_cores)
  362. return false;
  363. knc = cgpu->device_data;
  364. for (asic = 0; asic < KNC_TITAN_MAX_ASICS; ++asic) {
  365. knc_titan_setup_spi("ASIC", knc->ctx, asic, KNC_TITAN_FPGA_SPI_DIVIDER,
  366. KNC_TITAN_FPGA_SPI_PRECLK, KNC_TITAN_FPGA_SPI_DECLK,
  367. KNC_TITAN_FPGA_SPI_SSLOWMIN);
  368. for (die = 0; die < KNC_TITAN_DIES_PER_ASIC; ++die) {
  369. configure_one_die(knc, asic, die);
  370. knc->dies[asic][die].next_slot = KNC_TITAN_MIN_WORK_SLOT_NUM;
  371. knc->dies[asic][die].first_slot = KNC_TITAN_MIN_WORK_SLOT_NUM;
  372. }
  373. }
  374. timer_set_now(&thr->tv_poll);
  375. return true;
  376. }
  377. static bool die_test_and_add(struct knc_titan_info * const knc, int asic, int die, char * const errbuf)
  378. {
  379. struct knc_die_info die_info;
  380. char repr[6];
  381. snprintf(repr, sizeof(repr), "%s %d", knc_titan_drv.name, asic);
  382. die_info.cores = KNC_TITAN_CORES_PER_DIE; /* core hint */
  383. die_info.version = KNC_VERSION_TITAN;
  384. if (!knc_titan_get_info(repr, knc->ctx, asic, die, &die_info))
  385. die_info.cores = -1;
  386. if (0 < die_info.cores) {
  387. sprintf(errbuf, "Die[%d:%d] not detected", asic, die);
  388. return false;
  389. }
  390. /* TODO: add procs */
  391. sprintf(errbuf, "Die[%d:%d] has %d cores; was not added (addition not implemented)", asic, die, die_info.cores);
  392. return false;
  393. }
  394. static bool die_enable(struct knc_titan_info * const knc, int asic, int die, char * const errbuf)
  395. {
  396. bool res = true;
  397. cgpu_request_control(knc->cgpu);
  398. if (0 >= knc->dies[asic][die].cores)
  399. res = die_test_and_add(knc, asic, die, errbuf);
  400. if (res) {
  401. res = configure_one_die(knc, asic, die);
  402. }
  403. cgpu_release_control(knc->cgpu);
  404. return res;
  405. }
  406. static bool die_disable(struct knc_titan_info * const knc, int asic, int die, char * const errbuf)
  407. {
  408. cgpu_request_control(knc->cgpu);
  409. /* TODO: delete procs */
  410. cgpu_release_control(knc->cgpu);
  411. sprintf(errbuf, "die_disable[%d:%d] not imnplemented", asic, die);
  412. return false;
  413. }
  414. static bool die_reconfigure(struct knc_titan_info * const knc, int asic, int die, char * const errbuf)
  415. {
  416. return die_enable(knc, asic, die, errbuf);
  417. }
  418. static bool knc_titan_prepare_work(struct thr_info *thr, struct work *work)
  419. {
  420. work->nonce_diff = DEFAULT_DIFF_FILTERING_FLOAT;
  421. return true;
  422. }
  423. static void knc_titan_set_queue_full(struct knc_titan_info * const knc)
  424. {
  425. const bool full = (knc->workqueue_size >= knc->workqueue_max);
  426. struct cgpu_info *proc;
  427. for (proc = knc->cgpu; proc; proc = proc->next_proc) {
  428. struct thr_info * const thr = proc->thr[0];
  429. thr->queue_full = full;
  430. }
  431. }
  432. static void knc_titan_remove_local_queue(struct knc_titan_info * const knc, struct work * const work)
  433. {
  434. DL_DELETE(knc->workqueue, work);
  435. free_work(work);
  436. --knc->workqueue_size;
  437. }
  438. static void knc_titan_prune_local_queue(struct thr_info *thr)
  439. {
  440. struct cgpu_info * const cgpu = thr->cgpu;
  441. struct knc_titan_info * const knc = cgpu->device_data;
  442. struct work *work, *tmp;
  443. DL_FOREACH_SAFE(knc->workqueue, work, tmp) {
  444. if (stale_work(work, false))
  445. knc_titan_remove_local_queue(knc, work);
  446. }
  447. knc_titan_set_queue_full(knc);
  448. }
  449. static bool knc_titan_queue_append(struct thr_info * const thr, struct work * const work)
  450. {
  451. struct cgpu_info * const cgpu = thr->cgpu;
  452. struct knc_titan_info * const knc = cgpu->device_data;
  453. if (knc->workqueue_size >= knc->workqueue_max) {
  454. knc_titan_prune_local_queue(thr);
  455. if (thr->queue_full)
  456. return false;
  457. }
  458. DL_APPEND(knc->workqueue, work);
  459. ++knc->workqueue_size;
  460. knc_titan_set_queue_full(knc);
  461. if (thr->queue_full)
  462. knc_titan_prune_local_queue(thr);
  463. return true;
  464. }
  465. #define HASH_LAST_ADDED(head, out) \
  466. (out = (head) ? (ELMT_FROM_HH((head)->hh.tbl, (head)->hh.tbl->tail)) : NULL)
  467. static void knc_titan_queue_flush(struct thr_info * const thr)
  468. {
  469. struct cgpu_info * const cgpu = thr->cgpu;
  470. struct knc_titan_info * const knc = cgpu->device_data;
  471. struct work *work, *tmp;
  472. if (knc->cgpu != cgpu)
  473. return;
  474. DL_FOREACH_SAFE(knc->workqueue, work, tmp){
  475. knc_titan_remove_local_queue(knc, work);
  476. }
  477. knc_titan_set_queue_full(knc);
  478. HASH_LAST_ADDED(knc->devicework, work);
  479. if (work && stale_work(work, true)) {
  480. int asic, die;
  481. for (asic = 0; asic < KNC_TITAN_MAX_ASICS; ++asic) {
  482. for (die = 0; die < KNC_TITAN_DIES_PER_ASIC; ++die) {
  483. knc->dies[asic][die].need_flush = true;
  484. }
  485. knc->asic_served_by_fpga[asic] = true;
  486. }
  487. timer_set_now(&thr->tv_poll);
  488. }
  489. }
  490. #define MAKE_WORKID(asic, die, slot) ((((uint32_t)(asic)) << 16) | ((uint32_t)(die) << 8) | ((uint32_t)(slot)))
  491. #define ASIC_FROM_WORKID(workid) ((((uint32_t)(workid)) >> 16) & 0xFF)
  492. #define DIE_FROM_WORKID(workid) ((((uint32_t)(workid)) >> 8) & 0xFF)
  493. #define SLOT_FROM_WORKID(workid) (((uint32_t)(workid)) & 0xFF)
  494. static bool knc_titan_process_report(struct knc_titan_info * const knc, struct knc_titan_core * const knccore, struct knc_report * const report)
  495. {
  496. int i, tmp_int;
  497. struct work *work;
  498. struct cgpu_info * const proc = knccore->die->proc;
  499. bool ret = false;
  500. for (i = 0; i < KNC_TITAN_NONCES_PER_REPORT; ++i) {
  501. if ((report->nonce[i].slot == knccore->last_nonce.slot) &&
  502. (report->nonce[i].nonce == knccore->last_nonce.nonce))
  503. break;
  504. ret = true;
  505. tmp_int = MAKE_WORKID(knccore->asicno, knccore->dieno, report->nonce[i].slot);
  506. HASH_FIND_INT(knc->devicework, &tmp_int, work);
  507. if (!work) {
  508. applog(LOG_WARNING, "%"PRIpreprv"[%d:%d:%d]: Got nonce for unknown work in slot %u", proc->proc_repr, knccore->asicno, knccore->dieno, knccore->coreno, (unsigned)report->nonce[i].slot);
  509. continue;
  510. }
  511. if (submit_nonce(proc->thr[0], work, report->nonce[i].nonce)) {
  512. hashes_done2(proc->thr[0], DEFAULT_DIFF_HASHES_PER_NONCE, NULL);
  513. knc_titan_die_hashmeter(knccore->die, DEFAULT_DIFF_HASHES_PER_NONCE);
  514. knccore->hwerr_in_row = 0;
  515. }
  516. }
  517. knccore->last_nonce.slot = report->nonce[0].slot;
  518. knccore->last_nonce.nonce = report->nonce[0].nonce;
  519. knccore->need_manual_check = false;
  520. return ret;
  521. }
  522. static void knc_titan_poll(struct thr_info * const thr)
  523. {
  524. struct thr_info *mythr;
  525. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  526. struct knc_titan_info * const knc = cgpu->device_data;
  527. struct knc_titan_core *knccore;
  528. struct work *work, *tmp;
  529. int workaccept = 0;
  530. unsigned long delay_usecs = KNC_POLL_INTERVAL_US;
  531. struct knc_report report;
  532. struct knc_die_info die_info;
  533. int asic;
  534. int die;
  535. struct knc_titan_die *die_p;
  536. struct timeval tv_now;
  537. int num_request_busy;
  538. int num_status_byte_error[4];
  539. bool fpga_status_checked;
  540. knc_titan_prune_local_queue(thr);
  541. for (asic = 0; asic < KNC_TITAN_MAX_ASICS; ++asic) {
  542. fpga_status_checked = false;
  543. num_request_busy = KNC_TITAN_DIES_PER_ASIC;
  544. for (die = 0; die < KNC_TITAN_DIES_PER_ASIC; ++die) {
  545. die_p = &(knc->dies[asic][die]);
  546. /* make sure the running average buffers are updated even in the absence of found nonces */
  547. knc_titan_die_hashmeter(die_p, 0);
  548. if (0 >= die_p->cores)
  549. continue;
  550. struct cgpu_info *die_proc = die_p->proc;
  551. DL_FOREACH_SAFE(knc->workqueue, work, tmp) {
  552. bool work_accepted = false;
  553. bool need_replace;
  554. if (die_p->first_slot > KNC_TITAN_MIN_WORK_SLOT_NUM)
  555. need_replace = ((die_p->next_slot + 1) == die_p->first_slot);
  556. else
  557. need_replace = (die_p->next_slot == KNC_TITAN_MAX_WORK_SLOT_NUM);
  558. if (die_p->need_flush || need_replace) {
  559. bool unused;
  560. if (die_p->broadcast_flushes) {
  561. /* Use broadcast */
  562. if (knc_titan_set_work(die_proc->device->dev_repr, knc->ctx, asic, die, ALL_CORES, die_p->next_slot, work, true, &unused, &report)) {
  563. work_accepted = true;
  564. }
  565. } else {
  566. /* Use FPGA accelerated unicasts */
  567. if (!fpga_status_checked) {
  568. timer_set_now(&knc->tv_prev);
  569. knc_titan_get_work_status(die_proc->device->dev_repr, knc->ctx, asic, &num_request_busy, num_status_byte_error);
  570. fpga_status_checked = true;
  571. }
  572. if (num_request_busy == 0) {
  573. if (knc_titan_set_work_parallel(die_proc->device->dev_repr, knc->ctx, asic, 1 << die, 0, die_p->next_slot, work, true, die_p->cores, KNC_TITAN_FPGA_RETRIES)) {
  574. work_accepted = true;
  575. }
  576. }
  577. }
  578. } else {
  579. if (knc->asic_served_by_fpga[asic]) {
  580. knc_titan_get_work_status(die_proc->device->dev_repr, knc->ctx, asic, &num_request_busy, num_status_byte_error);
  581. if (num_request_busy == 0) {
  582. timer_set_now(&tv_now);
  583. double diff = ((tv_now.tv_sec - knc->tv_prev.tv_sec) * 1000000.0 + (tv_now.tv_usec - knc->tv_prev.tv_usec)) / 1000000.0;
  584. applog(LOG_INFO, "%s: Flush took %f secs for ASIC %d", knc_titan_drv.dname, diff, asic);
  585. applog(LOG_DEBUG, "FPGA CRC error counters: %d %d %d %d", num_status_byte_error[0], num_status_byte_error[1], num_status_byte_error[2], num_status_byte_error[3]);
  586. knc->asic_served_by_fpga[asic] = false;
  587. for (int die2 = 0; die2 < KNC_TITAN_DIES_PER_ASIC; ++die2) {
  588. knc->dies[asic][die2].manual_check_count = KNC_TITAN_CORES_PER_DIE - MANUAL_CHECK_CORES_PER_POLL;
  589. for (knccore = knc->dies[asic][die2].first_core ; knccore ; knccore = knccore->next_core)
  590. knccore->need_manual_check = true;
  591. }
  592. }
  593. }
  594. if (knc->asic_served_by_fpga[asic] || !knc_titan_set_work(die_proc->dev_repr, knc->ctx, asic, die, ALL_CORES, die_p->next_slot, work, false, &work_accepted, &report))
  595. work_accepted = false;
  596. }
  597. knccore = die_proc->thr[0]->cgpu_data;
  598. if ((!work_accepted) || (NULL == knccore))
  599. break;
  600. bool was_flushed = false;
  601. if (die_p->need_flush || need_replace) {
  602. applog(LOG_NOTICE, "%s[%d-%d] Flushing stale works (%s)", die_proc->dev_repr, asic, die,
  603. die_p->need_flush ? "New work" : "Slot collision");
  604. die_p->need_flush = false;
  605. die_p->first_slot = die_p->next_slot;
  606. delay_usecs = 0;
  607. was_flushed = true;
  608. }
  609. --knc->workqueue_size;
  610. DL_DELETE(knc->workqueue, work);
  611. work->device_id = MAKE_WORKID(asic, die, die_p->next_slot);
  612. struct work *work1, *tmp1;
  613. HASH_ITER(hh, knc->devicework, work1, tmp1) {
  614. if (work->device_id == work1->device_id) {
  615. HASH_DEL(knc->devicework, work1);
  616. free_work(work1);
  617. }
  618. }
  619. HASH_ADD(hh, knc->devicework, device_id, sizeof(work->device_id), work);
  620. if (++(die_p->next_slot) > KNC_TITAN_MAX_WORK_SLOT_NUM)
  621. die_p->next_slot = KNC_TITAN_MIN_WORK_SLOT_NUM;
  622. ++workaccept;
  623. /* If we know for sure that this work was urgent, then we don't need to hurry up
  624. * with filling next slot, we have plenty of time until current work completes.
  625. * So, better to proceed with other ASICs/dies. */
  626. if (was_flushed)
  627. break;
  628. }
  629. }
  630. }
  631. applog(LOG_DEBUG, "%s: %d jobs accepted to queue (max=%d)", knc_titan_drv.dname, workaccept, knc->workqueue_max);
  632. timer_set_now(&tv_now);
  633. for (asic = 0; asic < KNC_TITAN_MAX_ASICS; ++asic) {
  634. for (die = 0; die < KNC_TITAN_DIES_PER_ASIC; ++die) {
  635. die_p = &(knc->dies[asic][die]);
  636. if (0 >= die_p->cores)
  637. continue;
  638. die_info.cores = die_p->cores; /* core hint */
  639. die_info.version = KNC_VERSION_TITAN;
  640. if (knc->asic_served_by_fpga[asic] || !knc_titan_get_info(cgpu->dev_repr, knc->ctx, asic, die, &die_info))
  641. continue;
  642. thread_reportin(die_p->proc->thr[0]);
  643. for (knccore = die_p->first_core ; knccore ; knccore = knccore->next_core) {
  644. if (!die_info.has_report[knccore->coreno])
  645. continue;
  646. if (!knc_titan_get_report(die_p->proc->proc_repr, knc->ctx, asic, die, knccore->coreno, &report))
  647. continue;
  648. if (knc_titan_process_report(knc, knccore, &report))
  649. timer_set_now(&(die_p->last_share));
  650. }
  651. }
  652. /* Check die health */
  653. for (die = 0; die < KNC_TITAN_DIES_PER_ASIC; ++die) {
  654. die_p = &(knc->dies[asic][die]);
  655. if (0 >= die_p->cores)
  656. continue;
  657. if (timer_elapsed(&(die_p->last_share), &tv_now) < DIE_HEALTH_INTERVAL_SEC)
  658. continue;
  659. /* Reconfigure die */
  660. configure_one_die(knc, asic, die);
  661. }
  662. }
  663. for (asic = 0; asic < KNC_TITAN_MAX_ASICS; ++asic) {
  664. for (die = 0; die < KNC_TITAN_DIES_PER_ASIC; ++die) {
  665. die_p = &(knc->dies[asic][die]);
  666. if (0 >= die_p->cores || die_p->manual_check_count < 0)
  667. continue;
  668. for (knccore = die_p->first_core ; knccore ; knccore = knccore->next_core) {
  669. int core = knccore->coreno;
  670. if (core < die_p->manual_check_count)
  671. continue;
  672. if (core >= die_p->manual_check_count + MANUAL_CHECK_CORES_PER_POLL)
  673. break;
  674. if (!knccore->need_manual_check)
  675. continue;
  676. if (!knc_titan_get_report(die_p->proc->proc_repr, knc->ctx, asic, die, knccore->coreno, &report))
  677. continue;
  678. if (knc_titan_process_report(knc, knccore, &report))
  679. timer_set_now(&(die_p->last_share));
  680. }
  681. if (die_p->manual_check_count == 0) {
  682. die_p->manual_check_count = -1;
  683. } else {
  684. die_p->manual_check_count -= MANUAL_CHECK_CORES_PER_POLL;
  685. if (die_p->manual_check_count < 0)
  686. die_p->manual_check_count = 0;
  687. }
  688. }
  689. }
  690. if (workaccept) {
  691. if (workaccept >= knc->workqueue_max) {
  692. knc->workqueue_max = workaccept;
  693. delay_usecs = 0;
  694. }
  695. knc_titan_set_queue_full(knc);
  696. }
  697. timer_set_delay_from_now(&thr->tv_poll, delay_usecs);
  698. }
  699. /*
  700. * specify settings / options via RPC or command line
  701. */
  702. /* support for --set-device
  703. * must be set before probing the device
  704. */
  705. static void knc_titan_set_clock_freq(struct cgpu_info * const device, int const val)
  706. {
  707. }
  708. static const char *knc_titan_set_clock(struct cgpu_info * const device, const char * const option, const char * const setting, char * const replybuf, enum bfg_set_device_replytype * const success)
  709. {
  710. knc_titan_set_clock_freq(device, atoi(setting));
  711. return NULL;
  712. }
  713. static const char *knc_titan_die_ena(struct cgpu_info * const device, const char * const option, const char * const setting, char * const replybuf, enum bfg_set_device_replytype * const success)
  714. {
  715. int asic, die;
  716. char str[256];
  717. /* command format: ASIC:N;DIE:N;MODE:ENABLE|DISABLE|RECONFIGURE */
  718. if (3 != sscanf(setting, "ASIC:%d;DIE:%d;MODE:%255s", &asic, &die, str)) {
  719. error_bad_params:
  720. sprintf(replybuf, "Die setup failed, bad parameters");
  721. return replybuf;
  722. }
  723. if (0 == strncasecmp(str, "enable", sizeof(str) - 1)) {
  724. if (!die_enable(device->device_data, asic, die, replybuf))
  725. return replybuf;
  726. } else if (0 == strncasecmp(str, "disable", sizeof(str) - 1)) {
  727. if (!die_disable(device->device_data, asic, die, replybuf))
  728. return replybuf;
  729. } else if (0 == strncasecmp(str, "reconfigure", sizeof(str) - 1)) {
  730. if (!die_reconfigure(device->device_data, asic, die, replybuf)) {
  731. /* Do not return error on reconfigure command!
  732. * (or the whole bfgminer will be restarted) */
  733. *success = SDR_OK;
  734. return replybuf;
  735. }
  736. } else
  737. goto error_bad_params;
  738. sprintf(replybuf, "Die setup Ok; asic %d die %d cmd %s", asic, die, str);
  739. *success = SDR_OK;
  740. return replybuf;
  741. }
  742. static const struct bfg_set_device_definition knc_titan_set_device_funcs[] = {
  743. { "clock", knc_titan_set_clock, NULL },
  744. { "die", knc_titan_die_ena, NULL },
  745. { NULL },
  746. };
  747. /*
  748. * specify settings / options via TUI
  749. */
  750. #ifdef HAVE_CURSES
  751. static void knc_titan_tui_wlogprint_choices(struct cgpu_info * const proc)
  752. {
  753. wlogprint("[C]lock speed ");
  754. }
  755. static const char *knc_titan_tui_handle_choice(struct cgpu_info * const proc, const int input)
  756. {
  757. static char buf[0x100]; /* Static for replies */
  758. switch (input)
  759. {
  760. case 'c': case 'C':
  761. {
  762. sprintf(buf, "Set clock speed");
  763. char * const setting = curses_input(buf);
  764. knc_titan_set_clock_freq(proc->device, atoi(setting));
  765. return "Clock speed changed\n";
  766. }
  767. }
  768. return NULL;
  769. }
  770. static void knc_titan_wlogprint_status(struct cgpu_info * const proc)
  771. {
  772. wlogprint("Clock speed: N/A\n");
  773. }
  774. #endif
  775. struct device_drv knc_titan_drv =
  776. {
  777. /* metadata */
  778. .dname = "titan",
  779. .name = "KNC",
  780. .drv_min_nonce_diff = titan_min_nonce_diff,
  781. .drv_detect = knc_titan_detect,
  782. .thread_init = knc_titan_init,
  783. /* specify mining type - queue */
  784. .minerloop = minerloop_queue,
  785. .queue_append = knc_titan_queue_append,
  786. .queue_flush = knc_titan_queue_flush,
  787. .poll = knc_titan_poll,
  788. .prepare_work = knc_titan_prepare_work,
  789. /* additional statistics */
  790. .get_master_rolling_hashrate = knc_titan_get_device_rolling_hashrate,
  791. .zero_stats = knc_titan_zero_stats,
  792. /* TUI support - e.g. setting clock via UI */
  793. #ifdef HAVE_CURSES
  794. .proc_wlogprint_status = knc_titan_wlogprint_status,
  795. .proc_tui_wlogprint_choices = knc_titan_tui_wlogprint_choices,
  796. .proc_tui_handle_choice = knc_titan_tui_handle_choice,
  797. #endif
  798. };