driver-titan.c 21 KB

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