driver-titan.c 22 KB

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