deviceapi.c 29 KB

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