driver-titan.c 26 KB

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