deviceapi.c 29 KB

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