driver-titan.c 26 KB

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