deviceapi.c 24 KB

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