driver-titan.c 28 KB

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