deviceapi.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. /*
  2. * Copyright 2011-2014 Luke Dashjr
  3. * Copyright 2011-2012 Con Kolivas
  4. * Copyright 2012-2013 Andrew Smith
  5. * Copyright 2010 Jeff Garzik
  6. * Copyright 2014 Nate Woolls
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 3 of the License, or (at your option)
  11. * any later version. See COPYING for more details.
  12. */
  13. #include "config.h"
  14. #include <ctype.h>
  15. #ifdef WIN32
  16. #include <winsock2.h>
  17. #else
  18. #include <sys/select.h>
  19. #endif
  20. #include <stdbool.h>
  21. #include <stdint.h>
  22. #include <sys/time.h>
  23. #include <sys/types.h>
  24. #include <time.h>
  25. #include <unistd.h>
  26. #include "compat.h"
  27. #include "deviceapi.h"
  28. #include "logging.h"
  29. #include "lowlevel.h"
  30. #ifdef NEED_BFG_LOWL_VCOM
  31. #include "lowl-vcom.h"
  32. #endif
  33. #include "miner.h"
  34. #include "util.h"
  35. struct driver_registration *_bfg_drvreg1;
  36. struct driver_registration *_bfg_drvreg2;
  37. void _bfg_register_driver(const struct device_drv *drv)
  38. {
  39. static struct driver_registration *initlist;
  40. struct driver_registration *ndr;
  41. if (!drv)
  42. {
  43. // Move initlist to hashtables
  44. LL_FOREACH(initlist, ndr)
  45. {
  46. drv = ndr->drv;
  47. if (drv->drv_init)
  48. drv->drv_init();
  49. HASH_ADD_KEYPTR(hh , _bfg_drvreg1, drv->dname, strlen(drv->dname), ndr);
  50. HASH_ADD_KEYPTR(hh2, _bfg_drvreg2, drv->name , strlen(drv->name ), ndr);
  51. }
  52. initlist = NULL;
  53. return;
  54. }
  55. ndr = malloc(sizeof(*ndr));
  56. *ndr = (struct driver_registration){
  57. .drv = drv,
  58. };
  59. LL_PREPEND(initlist, ndr);
  60. }
  61. static
  62. int sort_drv_by_dname(struct driver_registration * const a, struct driver_registration * const b)
  63. {
  64. return strcmp(a->drv->dname, b->drv->dname);
  65. };
  66. static
  67. int sort_drv_by_priority(struct driver_registration * const a, struct driver_registration * const b)
  68. {
  69. return a->drv->probe_priority - b->drv->probe_priority;
  70. };
  71. void bfg_devapi_init()
  72. {
  73. _bfg_register_driver(NULL);
  74. HASH_SRT(hh , _bfg_drvreg1, sort_drv_by_dname );
  75. HASH_SRT(hh2, _bfg_drvreg2, sort_drv_by_priority);
  76. }
  77. float common_sha256d_and_scrypt_min_nonce_diff(struct cgpu_info * const proc, const struct mining_algorithm * const malgo)
  78. {
  79. switch (malgo->algo)
  80. {
  81. #ifdef USE_SCRYPT
  82. case POW_SCRYPT:
  83. return 1./0x10000;
  84. #endif
  85. #ifdef USE_SHA256D
  86. case POW_SHA256D:
  87. return 1.;
  88. #endif
  89. default:
  90. return -1.;
  91. }
  92. }
  93. #ifdef USE_SCRYPT
  94. float common_scrypt_min_nonce_diff(struct cgpu_info * const proc, const struct mining_algorithm * const malgo)
  95. {
  96. return (malgo->algo == POW_SCRYPT) ? (1./0x10000) : -1.;
  97. }
  98. #endif
  99. bool hashes_done(struct thr_info *thr, int64_t hashes, struct timeval *tvp_hashes, uint32_t *max_nonce)
  100. {
  101. struct cgpu_info *cgpu = thr->cgpu;
  102. const long cycle = opt_log_interval / 5 ? : 1;
  103. if (unlikely(hashes == -1)) {
  104. if (timer_elapsed(&cgpu->tv_device_last_not_well, NULL) > 0)
  105. dev_error(cgpu, REASON_THREAD_ZERO_HASH);
  106. if (thr->scanhash_working && opt_restart) {
  107. applog(LOG_ERR, "%"PRIpreprv" failure, attempting to reinitialize", cgpu->proc_repr);
  108. thr->scanhash_working = false;
  109. cgpu->reinit_backoff = 5.2734375;
  110. hashes = 0;
  111. } else {
  112. applog(LOG_ERR, "%"PRIpreprv" failure, disabling!", cgpu->proc_repr);
  113. cgpu->deven = DEV_RECOVER_ERR;
  114. run_cmd(cmd_idle);
  115. return false;
  116. }
  117. }
  118. else
  119. thr->scanhash_working = true;
  120. thr->hashes_done += hashes;
  121. if (hashes > cgpu->max_hashes)
  122. cgpu->max_hashes = hashes;
  123. timeradd(&thr->tv_hashes_done, tvp_hashes, &thr->tv_hashes_done);
  124. // max_nonce management (optional)
  125. if (max_nonce)
  126. {
  127. uint64_t new_max_nonce = *max_nonce;
  128. new_max_nonce *= cycle;
  129. new_max_nonce *= 1000000;
  130. new_max_nonce /= ((uint64_t)thr->tv_hashes_done.tv_sec * 1000000) + thr->tv_hashes_done.tv_usec;
  131. if (new_max_nonce > 0xffffffff)
  132. new_max_nonce = 0xffffffff;
  133. *max_nonce = new_max_nonce;
  134. }
  135. hashmeter2(thr);
  136. return true;
  137. }
  138. bool hashes_done2(struct thr_info *thr, int64_t hashes, uint32_t *max_nonce)
  139. {
  140. struct timeval tv_now, tv_delta;
  141. timer_set_now(&tv_now);
  142. timersub(&tv_now, &thr->_tv_last_hashes_done_call, &tv_delta);
  143. thr->_tv_last_hashes_done_call = tv_now;
  144. return hashes_done(thr, hashes, &tv_delta, max_nonce);
  145. }
  146. /* A generic wait function for threads that poll that will wait a specified
  147. * time tdiff waiting on a work restart request. Returns zero if the condition
  148. * was met (work restart requested) or ETIMEDOUT if not.
  149. */
  150. int restart_wait(struct thr_info *thr, unsigned int mstime)
  151. {
  152. struct timeval tv_timer, tv_now, tv_timeout;
  153. fd_set rfds;
  154. SOCKETTYPE wrn = thr->work_restart_notifier[0];
  155. int rv;
  156. if (unlikely(thr->work_restart_notifier[1] == INVSOCK))
  157. {
  158. // This is a bug!
  159. applog(LOG_ERR, "%"PRIpreprv": restart_wait called without a work_restart_notifier", thr->cgpu->proc_repr);
  160. cgsleep_ms(mstime);
  161. return (thr->work_restart ? 0 : ETIMEDOUT);
  162. }
  163. timer_set_now(&tv_now);
  164. timer_set_delay(&tv_timer, &tv_now, mstime * 1000);
  165. while (true)
  166. {
  167. FD_ZERO(&rfds);
  168. FD_SET(wrn, &rfds);
  169. tv_timeout = tv_timer;
  170. rv = select(wrn + 1, &rfds, NULL, NULL, select_timeout(&tv_timeout, &tv_now));
  171. if (rv == 0)
  172. return ETIMEDOUT;
  173. if (rv > 0)
  174. {
  175. if (thr->work_restart)
  176. return 0;
  177. notifier_read(thr->work_restart_notifier);
  178. }
  179. timer_set_now(&tv_now);
  180. }
  181. }
  182. static
  183. struct work *get_and_prepare_work(struct thr_info *thr)
  184. {
  185. struct cgpu_info *proc = thr->cgpu;
  186. struct device_drv *api = proc->drv;
  187. struct work *work;
  188. work = get_work(thr);
  189. if (!work)
  190. return NULL;
  191. if (api->prepare_work && !api->prepare_work(thr, work)) {
  192. free_work(work);
  193. applog(LOG_ERR, "%"PRIpreprv": Work prepare failed, disabling!", proc->proc_repr);
  194. proc->deven = DEV_RECOVER_ERR;
  195. run_cmd(cmd_idle);
  196. return NULL;
  197. }
  198. return work;
  199. }
  200. // Miner loop to manage a single processor (with possibly multiple threads per processor)
  201. void minerloop_scanhash(struct thr_info *mythr)
  202. {
  203. struct cgpu_info *cgpu = mythr->cgpu;
  204. struct device_drv *api = cgpu->drv;
  205. struct timeval tv_start, tv_end;
  206. struct timeval tv_hashes, tv_worktime;
  207. uint32_t max_nonce = api->can_limit_work ? api->can_limit_work(mythr) : 0xffffffff;
  208. int64_t hashes;
  209. struct work *work;
  210. const bool primary = (!mythr->device_thread) || mythr->primary_thread;
  211. #ifdef HAVE_PTHREAD_CANCEL
  212. pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
  213. #endif
  214. if (cgpu->deven != DEV_ENABLED)
  215. mt_disable(mythr);
  216. while (likely(!cgpu->shutdown)) {
  217. mythr->work_restart = false;
  218. request_work(mythr);
  219. work = get_and_prepare_work(mythr);
  220. if (!work)
  221. break;
  222. timer_set_now(&work->tv_work_start);
  223. do {
  224. thread_reportin(mythr);
  225. /* Only allow the mining thread to be cancelled when
  226. * it is not in the driver code. */
  227. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  228. timer_set_now(&tv_start);
  229. /* api->scanhash should scan the work for valid nonces
  230. * until max_nonce is reached or thr_info->work_restart */
  231. hashes = api->scanhash(mythr, work, work->blk.nonce + max_nonce);
  232. timer_set_now(&tv_end);
  233. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  234. pthread_testcancel();
  235. thread_reportin(mythr);
  236. timersub(&tv_end, &tv_start, &tv_hashes);
  237. if (!hashes_done(mythr, hashes, &tv_hashes, api->can_limit_work ? &max_nonce : NULL))
  238. goto disabled;
  239. if (unlikely(mythr->work_restart)) {
  240. /* Apart from device_thread 0, we stagger the
  241. * starting of every next thread to try and get
  242. * all devices busy before worrying about
  243. * getting work for their extra threads */
  244. if (!primary) {
  245. struct timespec rgtp;
  246. rgtp.tv_sec = 0;
  247. rgtp.tv_nsec = 250 * mythr->device_thread * 1000000;
  248. nanosleep(&rgtp, NULL);
  249. }
  250. break;
  251. }
  252. if (unlikely(mythr->pause || cgpu->deven != DEV_ENABLED))
  253. disabled:
  254. mt_disable(mythr);
  255. timersub(&tv_end, &work->tv_work_start, &tv_worktime);
  256. /* The inner do-while loop will exit unless the device is capable of
  257. * scanning a specific nonce range (currently CPU and GPU drivers)
  258. * See abandon_work comments for more details */
  259. } while (!abandon_work(work, &tv_worktime, cgpu->max_hashes));
  260. free_work(work);
  261. }
  262. }
  263. void mt_disable_start__async(struct thr_info * const mythr)
  264. {
  265. mt_disable_start(mythr);
  266. if (mythr->prev_work)
  267. free_work(mythr->prev_work);
  268. mythr->prev_work = mythr->work;
  269. mythr->work = NULL;
  270. mythr->_job_transition_in_progress = false;
  271. }
  272. bool do_job_prepare(struct thr_info *mythr, struct timeval *tvp_now)
  273. {
  274. struct cgpu_info *proc = mythr->cgpu;
  275. struct device_drv *api = proc->drv;
  276. struct timeval tv_worktime;
  277. mythr->tv_morework.tv_sec = -1;
  278. mythr->_job_transition_in_progress = true;
  279. if (mythr->work)
  280. timersub(tvp_now, &mythr->work->tv_work_start, &tv_worktime);
  281. if ((!mythr->work) || abandon_work(mythr->work, &tv_worktime, proc->max_hashes))
  282. {
  283. mythr->work_restart = false;
  284. request_work(mythr);
  285. // FIXME: Allow get_work to return NULL to retry on notification
  286. if (mythr->next_work)
  287. free_work(mythr->next_work);
  288. mythr->next_work = get_and_prepare_work(mythr);
  289. if (!mythr->next_work)
  290. return false;
  291. mythr->starting_next_work = true;
  292. api->job_prepare(mythr, mythr->next_work, mythr->_max_nonce);
  293. }
  294. else
  295. {
  296. mythr->starting_next_work = false;
  297. api->job_prepare(mythr, mythr->work, mythr->_max_nonce);
  298. }
  299. job_prepare_complete(mythr);
  300. return true;
  301. }
  302. void job_prepare_complete(struct thr_info *mythr)
  303. {
  304. if (unlikely(mythr->busy_state == TBS_GETTING_RESULTS))
  305. return;
  306. if (mythr->work)
  307. {
  308. if (true /* TODO: job is near complete */ || unlikely(mythr->work_restart))
  309. do_get_results(mythr, true);
  310. else
  311. {} // TODO: Set a timer to call do_get_results when job is near complete
  312. }
  313. else // no job currently running
  314. do_job_start(mythr);
  315. }
  316. void do_get_results(struct thr_info *mythr, bool proceed_with_new_job)
  317. {
  318. struct cgpu_info *proc = mythr->cgpu;
  319. struct device_drv *api = proc->drv;
  320. struct work *work = mythr->work;
  321. mythr->_job_transition_in_progress = true;
  322. mythr->tv_results_jobstart = mythr->tv_jobstart;
  323. mythr->_proceed_with_new_job = proceed_with_new_job;
  324. if (api->job_get_results)
  325. api->job_get_results(mythr, work);
  326. else
  327. job_results_fetched(mythr);
  328. }
  329. void job_results_fetched(struct thr_info *mythr)
  330. {
  331. if (mythr->_proceed_with_new_job)
  332. do_job_start(mythr);
  333. else
  334. {
  335. if (likely(mythr->prev_work))
  336. {
  337. struct timeval tv_now;
  338. timer_set_now(&tv_now);
  339. do_process_results(mythr, &tv_now, mythr->prev_work, true);
  340. }
  341. mt_disable_start__async(mythr);
  342. }
  343. }
  344. void do_job_start(struct thr_info *mythr)
  345. {
  346. struct cgpu_info *proc = mythr->cgpu;
  347. struct device_drv *api = proc->drv;
  348. thread_reportin(mythr);
  349. api->job_start(mythr);
  350. }
  351. void mt_job_transition(struct thr_info *mythr)
  352. {
  353. struct timeval tv_now;
  354. timer_set_now(&tv_now);
  355. if (mythr->starting_next_work)
  356. {
  357. mythr->next_work->tv_work_start = tv_now;
  358. if (mythr->prev_work)
  359. free_work(mythr->prev_work);
  360. mythr->prev_work = mythr->work;
  361. mythr->work = mythr->next_work;
  362. mythr->next_work = NULL;
  363. }
  364. mythr->tv_jobstart = tv_now;
  365. mythr->_job_transition_in_progress = false;
  366. }
  367. void job_start_complete(struct thr_info *mythr)
  368. {
  369. struct timeval tv_now;
  370. if (unlikely(!mythr->prev_work))
  371. return;
  372. timer_set_now(&tv_now);
  373. do_process_results(mythr, &tv_now, mythr->prev_work, false);
  374. }
  375. void job_start_abort(struct thr_info *mythr, bool failure)
  376. {
  377. struct cgpu_info *proc = mythr->cgpu;
  378. if (failure)
  379. {
  380. proc->deven = DEV_RECOVER_ERR;
  381. run_cmd(cmd_idle);
  382. }
  383. mythr->work = NULL;
  384. mythr->_job_transition_in_progress = false;
  385. }
  386. bool do_process_results(struct thr_info *mythr, struct timeval *tvp_now, struct work *work, bool stopping)
  387. {
  388. struct cgpu_info *proc = mythr->cgpu;
  389. struct device_drv *api = proc->drv;
  390. struct timeval tv_hashes;
  391. int64_t hashes = 0;
  392. if (api->job_process_results)
  393. hashes = api->job_process_results(mythr, work, stopping);
  394. thread_reportin(mythr);
  395. if (hashes)
  396. {
  397. timersub(tvp_now, &mythr->tv_results_jobstart, &tv_hashes);
  398. if (!hashes_done(mythr, hashes, &tv_hashes, api->can_limit_work ? &mythr->_max_nonce : NULL))
  399. return false;
  400. }
  401. return true;
  402. }
  403. static
  404. void do_notifier_select(struct thr_info *thr, struct timeval *tvp_timeout)
  405. {
  406. struct cgpu_info *cgpu = thr->cgpu;
  407. struct timeval tv_now;
  408. int maxfd;
  409. fd_set rfds;
  410. timer_set_now(&tv_now);
  411. FD_ZERO(&rfds);
  412. FD_SET(thr->notifier[0], &rfds);
  413. maxfd = thr->notifier[0];
  414. FD_SET(thr->work_restart_notifier[0], &rfds);
  415. set_maxfd(&maxfd, thr->work_restart_notifier[0]);
  416. if (thr->mutex_request[1] != INVSOCK)
  417. {
  418. FD_SET(thr->mutex_request[0], &rfds);
  419. set_maxfd(&maxfd, thr->mutex_request[0]);
  420. }
  421. if (select(maxfd + 1, &rfds, NULL, NULL, select_timeout(tvp_timeout, &tv_now)) < 0)
  422. return;
  423. if (thr->mutex_request[1] != INVSOCK && FD_ISSET(thr->mutex_request[0], &rfds))
  424. {
  425. // FIXME: This can only handle one request at a time!
  426. pthread_mutex_t *mutexp = &cgpu->device_mutex;
  427. notifier_read(thr->mutex_request);
  428. mutex_lock(mutexp);
  429. pthread_cond_signal(&cgpu->device_cond);
  430. pthread_cond_wait(&cgpu->device_cond, mutexp);
  431. mutex_unlock(mutexp);
  432. }
  433. if (FD_ISSET(thr->notifier[0], &rfds)) {
  434. notifier_read(thr->notifier);
  435. }
  436. if (FD_ISSET(thr->work_restart_notifier[0], &rfds))
  437. notifier_read(thr->work_restart_notifier);
  438. }
  439. void cgpu_setup_control_requests(struct cgpu_info * const cgpu)
  440. {
  441. mutex_init(&cgpu->device_mutex);
  442. notifier_init(cgpu->thr[0]->mutex_request);
  443. pthread_cond_init(&cgpu->device_cond, bfg_condattr);
  444. }
  445. void cgpu_request_control(struct cgpu_info * const cgpu)
  446. {
  447. struct thr_info * const thr = cgpu->thr[0];
  448. if (pthread_equal(pthread_self(), thr->pth))
  449. return;
  450. mutex_lock(&cgpu->device_mutex);
  451. notifier_wake(thr->mutex_request);
  452. pthread_cond_wait(&cgpu->device_cond, &cgpu->device_mutex);
  453. }
  454. void cgpu_release_control(struct cgpu_info * const cgpu)
  455. {
  456. struct thr_info * const thr = cgpu->thr[0];
  457. if (pthread_equal(pthread_self(), thr->pth))
  458. return;
  459. pthread_cond_signal(&cgpu->device_cond);
  460. mutex_unlock(&cgpu->device_mutex);
  461. }
  462. static
  463. void _minerloop_setup(struct thr_info *mythr)
  464. {
  465. struct cgpu_info * const cgpu = mythr->cgpu, *proc;
  466. if (mythr->work_restart_notifier[1] == -1)
  467. notifier_init(mythr->work_restart_notifier);
  468. for (proc = cgpu; proc; proc = proc->next_proc)
  469. {
  470. mythr = proc->thr[0];
  471. timer_set_now(&mythr->tv_watchdog);
  472. proc->disable_watchdog = true;
  473. }
  474. }
  475. void minerloop_async(struct thr_info *mythr)
  476. {
  477. struct thr_info *thr = mythr;
  478. struct cgpu_info *cgpu = mythr->cgpu;
  479. struct device_drv *api = cgpu->drv;
  480. struct timeval tv_now;
  481. struct timeval tv_timeout;
  482. struct cgpu_info *proc;
  483. bool is_running, should_be_running;
  484. _minerloop_setup(mythr);
  485. while (likely(!cgpu->shutdown)) {
  486. tv_timeout.tv_sec = -1;
  487. timer_set_now(&tv_now);
  488. for (proc = cgpu; proc; proc = proc->next_proc)
  489. {
  490. mythr = proc->thr[0];
  491. // Nothing should happen while we're starting a job
  492. if (unlikely(mythr->busy_state == TBS_STARTING_JOB))
  493. goto defer_events;
  494. is_running = mythr->work;
  495. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  496. if (should_be_running)
  497. {
  498. if (unlikely(!(is_running || mythr->_job_transition_in_progress)))
  499. {
  500. mt_disable_finish(mythr);
  501. goto djp;
  502. }
  503. if (unlikely(mythr->work_restart))
  504. goto djp;
  505. }
  506. else // ! should_be_running
  507. {
  508. if (unlikely(mythr->_job_transition_in_progress && timer_isset(&mythr->tv_morework)))
  509. {
  510. // Really only happens at startup
  511. applog(LOG_DEBUG, "%"PRIpreprv": Job transition in progress, with morework timer enabled: unsetting in-progress flag", proc->proc_repr);
  512. mythr->_job_transition_in_progress = false;
  513. }
  514. if (unlikely((is_running || !mythr->_mt_disable_called) && !mythr->_job_transition_in_progress))
  515. {
  516. disabled: ;
  517. if (is_running)
  518. {
  519. if (mythr->busy_state != TBS_GETTING_RESULTS)
  520. do_get_results(mythr, false);
  521. else
  522. // Avoid starting job when pending result fetch completes
  523. mythr->_proceed_with_new_job = false;
  524. }
  525. else // !mythr->_mt_disable_called
  526. mt_disable_start__async(mythr);
  527. }
  528. timer_unset(&mythr->tv_morework);
  529. }
  530. if (timer_passed(&mythr->tv_morework, &tv_now))
  531. {
  532. djp: ;
  533. if (!do_job_prepare(mythr, &tv_now))
  534. goto disabled;
  535. }
  536. defer_events:
  537. if (timer_passed(&mythr->tv_poll, &tv_now))
  538. api->poll(mythr);
  539. if (timer_passed(&mythr->tv_watchdog, &tv_now))
  540. {
  541. timer_set_delay(&mythr->tv_watchdog, &tv_now, WATCHDOG_INTERVAL * 1000000);
  542. bfg_watchdog(proc, &tv_now);
  543. }
  544. reduce_timeout_to(&tv_timeout, &mythr->tv_morework);
  545. reduce_timeout_to(&tv_timeout, &mythr->tv_poll);
  546. reduce_timeout_to(&tv_timeout, &mythr->tv_watchdog);
  547. }
  548. do_notifier_select(thr, &tv_timeout);
  549. }
  550. }
  551. static
  552. void do_queue_flush(struct thr_info *mythr)
  553. {
  554. struct cgpu_info *proc = mythr->cgpu;
  555. struct device_drv *api = proc->drv;
  556. api->queue_flush(mythr);
  557. if (mythr->next_work)
  558. {
  559. free_work(mythr->next_work);
  560. mythr->next_work = NULL;
  561. }
  562. }
  563. void minerloop_queue(struct thr_info *thr)
  564. {
  565. struct thr_info *mythr;
  566. struct cgpu_info *cgpu = thr->cgpu;
  567. struct device_drv *api = cgpu->drv;
  568. struct timeval tv_now;
  569. struct timeval tv_timeout;
  570. struct cgpu_info *proc;
  571. bool should_be_running;
  572. struct work *work;
  573. _minerloop_setup(thr);
  574. while (likely(!cgpu->shutdown)) {
  575. tv_timeout.tv_sec = -1;
  576. timer_set_now(&tv_now);
  577. for (proc = cgpu; proc; proc = proc->next_proc)
  578. {
  579. mythr = proc->thr[0];
  580. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  581. redo:
  582. if (should_be_running)
  583. {
  584. if (unlikely(mythr->_mt_disable_called))
  585. mt_disable_finish(mythr);
  586. if (unlikely(mythr->work_restart))
  587. {
  588. mythr->work_restart = false;
  589. do_queue_flush(mythr);
  590. }
  591. while (!mythr->queue_full)
  592. {
  593. if (mythr->next_work)
  594. {
  595. work = mythr->next_work;
  596. mythr->next_work = NULL;
  597. }
  598. else
  599. {
  600. request_work(mythr);
  601. // FIXME: Allow get_work to return NULL to retry on notification
  602. work = get_and_prepare_work(mythr);
  603. }
  604. if (!work)
  605. break;
  606. if (!api->queue_append(mythr, work))
  607. mythr->next_work = work;
  608. }
  609. }
  610. else
  611. if (unlikely(!mythr->_mt_disable_called))
  612. {
  613. do_queue_flush(mythr);
  614. mt_disable_start(mythr);
  615. }
  616. if (timer_passed(&mythr->tv_poll, &tv_now))
  617. api->poll(mythr);
  618. if (timer_passed(&mythr->tv_watchdog, &tv_now))
  619. {
  620. timer_set_delay(&mythr->tv_watchdog, &tv_now, WATCHDOG_INTERVAL * 1000000);
  621. bfg_watchdog(proc, &tv_now);
  622. }
  623. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  624. if (should_be_running && !mythr->queue_full)
  625. goto redo;
  626. reduce_timeout_to(&tv_timeout, &mythr->tv_poll);
  627. reduce_timeout_to(&tv_timeout, &mythr->tv_watchdog);
  628. }
  629. // HACK: Some designs set the main thr tv_poll from secondary thrs
  630. reduce_timeout_to(&tv_timeout, &cgpu->thr[0]->tv_poll);
  631. do_notifier_select(thr, &tv_timeout);
  632. }
  633. }
  634. void *miner_thread(void *userdata)
  635. {
  636. struct thr_info *mythr = userdata;
  637. struct cgpu_info *cgpu = mythr->cgpu;
  638. struct device_drv *drv = cgpu->drv;
  639. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  640. char threadname[20];
  641. snprintf(threadname, 20, "miner_%s", cgpu->proc_repr_ns);
  642. RenameThread(threadname);
  643. if (drv->thread_init && !drv->thread_init(mythr)) {
  644. dev_error(cgpu, REASON_THREAD_FAIL_INIT);
  645. for (struct cgpu_info *slave = cgpu->next_proc; slave && !slave->threads; slave = slave->next_proc)
  646. dev_error(slave, REASON_THREAD_FAIL_INIT);
  647. __thr_being_msg(LOG_ERR, mythr, "failure, exiting");
  648. goto out;
  649. }
  650. if (drv_ready(cgpu) && !cgpu->already_set_defaults)
  651. cgpu_set_defaults(cgpu);
  652. thread_reportout(mythr);
  653. applog(LOG_DEBUG, "Popping ping in miner thread");
  654. notifier_read(mythr->notifier); // Wait for a notification to start
  655. cgtime(&cgpu->cgminer_stats.start_tv);
  656. if (drv->minerloop)
  657. drv->minerloop(mythr);
  658. else
  659. minerloop_scanhash(mythr);
  660. __thr_being_msg(LOG_NOTICE, mythr, "shutting down");
  661. out: ;
  662. struct cgpu_info *proc = cgpu;
  663. do
  664. {
  665. proc->deven = DEV_DISABLED;
  666. proc->status = LIFE_DEAD2;
  667. }
  668. while ( (proc = proc->next_proc) && !proc->threads);
  669. mythr->getwork = 0;
  670. mythr->has_pth = false;
  671. cgsleep_ms(1);
  672. if (drv->thread_shutdown)
  673. drv->thread_shutdown(mythr);
  674. notifier_destroy(mythr->notifier);
  675. return NULL;
  676. }
  677. static pthread_mutex_t _add_cgpu_mutex = PTHREAD_MUTEX_INITIALIZER;
  678. static
  679. bool _add_cgpu(struct cgpu_info *cgpu)
  680. {
  681. int lpcount;
  682. if (!cgpu->procs)
  683. cgpu->procs = 1;
  684. lpcount = cgpu->procs;
  685. cgpu->device = cgpu;
  686. cgpu->dev_repr = malloc(6);
  687. cgpu->dev_repr_ns = malloc(6);
  688. #ifdef NEED_BFG_LOWL_VCOM
  689. maybe_strdup_if_null(&cgpu->dev_manufacturer, detectone_meta_info.manufacturer);
  690. maybe_strdup_if_null(&cgpu->dev_product, detectone_meta_info.product);
  691. maybe_strdup_if_null(&cgpu->dev_serial, detectone_meta_info.serial);
  692. #endif
  693. devices_new = realloc(devices_new, sizeof(struct cgpu_info *) * (total_devices_new + lpcount + 1));
  694. devices_new[total_devices_new++] = cgpu;
  695. if (lpcount > 1)
  696. {
  697. int tpp = cgpu->threads / lpcount;
  698. struct cgpu_info **nlp_p, *slave;
  699. nlp_p = &cgpu->next_proc;
  700. for (int i = 1; i < lpcount; ++i)
  701. {
  702. slave = malloc(sizeof(*slave));
  703. *slave = *cgpu;
  704. slave->proc_id = i;
  705. slave->threads = tpp;
  706. devices_new[total_devices_new++] = slave;
  707. *nlp_p = slave;
  708. nlp_p = &slave->next_proc;
  709. }
  710. *nlp_p = NULL;
  711. cgpu->proc_id = 0;
  712. cgpu->threads -= (tpp * (lpcount - 1));
  713. }
  714. renumber_cgpu(cgpu);
  715. cgpu->last_device_valid_work = time(NULL);
  716. return true;
  717. }
  718. bool add_cgpu(struct cgpu_info *cgpu)
  719. {
  720. mutex_lock(&_add_cgpu_mutex);
  721. const bool rv = _add_cgpu(cgpu);
  722. mutex_unlock(&_add_cgpu_mutex);
  723. return rv;
  724. }
  725. void add_cgpu_live(void *p)
  726. {
  727. add_cgpu(p);
  728. }
  729. bool add_cgpu_slave(struct cgpu_info *cgpu, struct cgpu_info *prev_cgpu)
  730. {
  731. if (!prev_cgpu)
  732. return add_cgpu(cgpu);
  733. while (prev_cgpu->next_proc)
  734. prev_cgpu = prev_cgpu->next_proc;
  735. mutex_lock(&_add_cgpu_mutex);
  736. int old_total_devices = total_devices_new;
  737. if (!_add_cgpu(cgpu))
  738. {
  739. mutex_unlock(&_add_cgpu_mutex);
  740. return false;
  741. }
  742. prev_cgpu->next_proc = devices_new[old_total_devices];
  743. mutex_unlock(&_add_cgpu_mutex);
  744. return true;
  745. }
  746. const char *proc_set_device_help(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  747. {
  748. const struct bfg_set_device_definition *sdf;
  749. char *p = replybuf;
  750. bool first = true;
  751. *out_success = SDR_HELP;
  752. sdf = proc->set_device_funcs;
  753. if (!sdf)
  754. nohelp:
  755. return "No help available";
  756. size_t matchlen = 0;
  757. if (newvalue)
  758. while (!isspace(newvalue[0]))
  759. ++matchlen;
  760. for ( ; sdf->optname; ++sdf)
  761. {
  762. if (!sdf->description)
  763. continue;
  764. if (matchlen && (strncasecmp(optname, sdf->optname, matchlen) || optname[matchlen]))
  765. continue;
  766. if (first)
  767. first = false;
  768. else
  769. p++[0] = '\n';
  770. p += sprintf(p, "%s: %s", sdf->optname, sdf->description);
  771. }
  772. if (replybuf == p)
  773. goto nohelp;
  774. return replybuf;
  775. }
  776. const char *proc_set_device_temp_cutoff(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  777. {
  778. int target_diff = proc->cutofftemp - proc->targettemp;
  779. proc->cutofftemp = atoi(newvalue);
  780. if (!proc->targettemp_user)
  781. proc->targettemp = proc->cutofftemp - target_diff;
  782. return NULL;
  783. }
  784. const char *proc_set_device_temp_target(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  785. {
  786. proc->targettemp = atoi(newvalue);
  787. proc->targettemp_user = true;
  788. return NULL;
  789. }
  790. static inline
  791. void _set_auto_sdr(enum bfg_set_device_replytype * const out_success, const char * const rv, const char * const optname)
  792. {
  793. if (!rv)
  794. *out_success = SDR_OK;
  795. else
  796. if (!strcasecmp(optname, "help"))
  797. *out_success = SDR_HELP;
  798. else
  799. *out_success = SDR_ERR;
  800. }
  801. const char *_proc_set_device(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  802. {
  803. const struct bfg_set_device_definition *sdf;
  804. sdf = proc->set_device_funcs;
  805. if (!sdf)
  806. {
  807. *out_success = SDR_NOSUPP;
  808. return "Device does not support setting parameters.";
  809. }
  810. for ( ; sdf->optname; ++sdf)
  811. if (!strcasecmp(optname, sdf->optname))
  812. {
  813. *out_success = SDR_AUTO;
  814. const char * const rv = sdf->func(proc, optname, newvalue, replybuf, out_success);
  815. if (SDR_AUTO == *out_success)
  816. _set_auto_sdr(out_success, rv, optname);
  817. return rv;
  818. }
  819. if (!strcasecmp(optname, "help"))
  820. return proc_set_device_help(proc, optname, newvalue, replybuf, out_success);
  821. *out_success = SDR_UNKNOWN;
  822. sprintf(replybuf, "Unknown option: %s", optname);
  823. return replybuf;
  824. }
  825. const char *__proc_set_device(struct cgpu_info * const proc, char * const optname, char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  826. {
  827. if (proc->drv->set_device)
  828. {
  829. const char * const rv = proc->drv->set_device(proc, optname, newvalue, replybuf);
  830. _set_auto_sdr(out_success, rv, optname);
  831. return rv;
  832. }
  833. return _proc_set_device(proc, optname, newvalue, replybuf, out_success);
  834. }
  835. const char *proc_set_device(struct cgpu_info * const proc, char * const optname, char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  836. {
  837. const char * const rv = __proc_set_device(proc, optname, newvalue, replybuf, out_success);
  838. switch (*out_success)
  839. {
  840. case SDR_NOSUPP:
  841. case SDR_UNKNOWN:
  842. if (!strcasecmp(optname, "temp-cutoff") || !strcasecmp(optname, "temp_cutoff"))
  843. return proc_set_device_temp_cutoff(proc, optname, newvalue, replybuf, out_success);
  844. else
  845. if (!strcasecmp(optname, "temp-target") || !strcasecmp(optname, "temp_target"))
  846. return proc_set_device_temp_target(proc, optname, newvalue, replybuf, out_success);
  847. default:
  848. break;
  849. }
  850. return rv;
  851. }
  852. #ifdef HAVE_CURSES
  853. const char *proc_set_device_tui_wrapper(struct cgpu_info * const proc, char * const optname, const bfg_set_device_func_t func, const char * const prompt, const char * const success_msg)
  854. {
  855. static char replybuf[0x2001];
  856. char * const cvar = curses_input(prompt);
  857. if (!cvar)
  858. return "Cancelled\n";
  859. enum bfg_set_device_replytype success;
  860. const char * const reply = func(proc, optname, cvar, replybuf, &success);
  861. free(cvar);
  862. if (reply)
  863. {
  864. if (reply != replybuf)
  865. snprintf(replybuf, sizeof(replybuf), "%s\n", reply);
  866. else
  867. tailsprintf(replybuf, sizeof(replybuf), "\n");
  868. return replybuf;
  869. }
  870. return success_msg ?: "Successful\n";
  871. }
  872. #endif
  873. #ifdef NEED_BFG_LOWL_VCOM
  874. bool _serial_detect_all(struct lowlevel_device_info * const info, void * const userp)
  875. {
  876. detectone_func_t detectone = userp;
  877. if (serial_claim(info->path, NULL))
  878. applogr(false, LOG_DEBUG, "%s is already claimed... skipping probes", info->path);
  879. return detectone(info->path);
  880. }
  881. #endif
  882. // NOTE: This is never used for any actual VCOM devices, which should use the new lowlevel interface
  883. int _serial_detect(struct device_drv *api, detectone_func_t detectone, autoscan_func_t autoscan, int flags)
  884. {
  885. struct string_elist *iter, *tmp;
  886. const char *dev, *colon;
  887. bool inhibitauto = flags & 4;
  888. char found = 0;
  889. bool forceauto = flags & 1;
  890. bool hasname;
  891. size_t namel = strlen(api->name);
  892. size_t dnamel = strlen(api->dname);
  893. #ifdef NEED_BFG_LOWL_VCOM
  894. clear_detectone_meta_info();
  895. #endif
  896. DL_FOREACH_SAFE(scan_devices, iter, tmp) {
  897. dev = iter->string;
  898. if ((colon = strchr(dev, ':')) && colon[1] != '\0') {
  899. size_t idlen = colon - dev;
  900. // allow either name:device or dname:device
  901. if ((idlen != namel || strncasecmp(dev, api->name, idlen))
  902. && (idlen != dnamel || strncasecmp(dev, api->dname, idlen)))
  903. continue;
  904. dev = colon + 1;
  905. hasname = true;
  906. }
  907. else
  908. hasname = false;
  909. if (!strcmp(dev, "auto"))
  910. forceauto = true;
  911. else if (!strcmp(dev, "noauto"))
  912. inhibitauto = true;
  913. else
  914. if ((flags & 2) && !hasname)
  915. continue;
  916. else
  917. if (!detectone)
  918. {} // do nothing
  919. else
  920. if (!strcmp(dev, "all"))
  921. {} // n/a
  922. else if (detectone(dev)) {
  923. string_elist_del(&scan_devices, iter);
  924. ++found;
  925. }
  926. }
  927. if ((forceauto || !(inhibitauto || found)) && autoscan)
  928. found += autoscan();
  929. return found;
  930. }
  931. static
  932. FILE *_open_bitstream(const char *path, const char *subdir, const char *sub2, const char *filename)
  933. {
  934. char fullpath[PATH_MAX];
  935. strcpy(fullpath, path);
  936. strcat(fullpath, "/");
  937. if (subdir) {
  938. strcat(fullpath, subdir);
  939. strcat(fullpath, "/");
  940. }
  941. if (sub2) {
  942. strcat(fullpath, sub2);
  943. strcat(fullpath, "/");
  944. }
  945. strcat(fullpath, filename);
  946. return fopen(fullpath, "rb");
  947. }
  948. #define _open_bitstream(path, subdir, sub2) do { \
  949. f = _open_bitstream(path, subdir, sub2, filename); \
  950. if (f) \
  951. return f; \
  952. } while(0)
  953. #define _open_bitstream2(path, path3) do { \
  954. _open_bitstream(path, NULL, path3); \
  955. _open_bitstream(path, "../share/" PACKAGE, path3); \
  956. _open_bitstream(path, "../" PACKAGE, path3); \
  957. } while(0)
  958. #define _open_bitstream3(path) do { \
  959. _open_bitstream2(path, dname); \
  960. _open_bitstream2(path, "bitstreams"); \
  961. _open_bitstream2(path, NULL); \
  962. } while(0)
  963. FILE *open_bitstream(const char *dname, const char *filename)
  964. {
  965. FILE *f;
  966. _open_bitstream3(opt_kernel_path);
  967. _open_bitstream3(cgminer_path);
  968. _open_bitstream3(".");
  969. return NULL;
  970. }
  971. void close_device_fd(struct thr_info * const thr)
  972. {
  973. struct cgpu_info * const proc = thr->cgpu;
  974. const int fd = proc->device_fd;
  975. if (fd == -1)
  976. return;
  977. if (close(fd))
  978. applog(LOG_WARNING, "%"PRIpreprv": Error closing device fd", proc->proc_repr);
  979. else
  980. {
  981. proc->device_fd = -1;
  982. applog(LOG_DEBUG, "%"PRIpreprv": Closed device fd", proc->proc_repr);
  983. }
  984. }
  985. struct cgpu_info *device_proc_by_id(const struct cgpu_info * const dev, const int procid)
  986. {
  987. struct cgpu_info *proc = (void*)dev;
  988. for (int i = 0; i < procid; ++i)
  989. {
  990. proc = proc->next_proc;
  991. if (unlikely((!proc) || proc->device != dev))
  992. return NULL;
  993. }
  994. return proc;
  995. }