driver-titan.c 20 KB

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