deviceapi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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, bfg_condattr);
  410. }
  411. void cgpu_request_control(struct cgpu_info * const cgpu)
  412. {
  413. struct thr_info * const thr = cgpu->thr[0];
  414. if (pthread_equal(pthread_self(), thr->pth))
  415. return;
  416. mutex_lock(&cgpu->device_mutex);
  417. notifier_wake(thr->mutex_request);
  418. pthread_cond_wait(&cgpu->device_cond, &cgpu->device_mutex);
  419. }
  420. void cgpu_release_control(struct cgpu_info * const cgpu)
  421. {
  422. struct thr_info * const thr = cgpu->thr[0];
  423. if (pthread_equal(pthread_self(), thr->pth))
  424. return;
  425. pthread_cond_signal(&cgpu->device_cond);
  426. mutex_unlock(&cgpu->device_mutex);
  427. }
  428. static
  429. void _minerloop_setup(struct thr_info *mythr)
  430. {
  431. struct cgpu_info * const cgpu = mythr->cgpu, *proc;
  432. if (mythr->work_restart_notifier[1] == -1)
  433. notifier_init(mythr->work_restart_notifier);
  434. for (proc = cgpu; proc; proc = proc->next_proc)
  435. {
  436. mythr = proc->thr[0];
  437. timer_set_now(&mythr->tv_watchdog);
  438. proc->disable_watchdog = true;
  439. }
  440. }
  441. void minerloop_async(struct thr_info *mythr)
  442. {
  443. struct thr_info *thr = mythr;
  444. struct cgpu_info *cgpu = mythr->cgpu;
  445. struct device_drv *api = cgpu->drv;
  446. struct timeval tv_now;
  447. struct timeval tv_timeout;
  448. struct cgpu_info *proc;
  449. bool is_running, should_be_running;
  450. _minerloop_setup(mythr);
  451. while (likely(!cgpu->shutdown)) {
  452. tv_timeout.tv_sec = -1;
  453. timer_set_now(&tv_now);
  454. for (proc = cgpu; proc; proc = proc->next_proc)
  455. {
  456. mythr = proc->thr[0];
  457. // Nothing should happen while we're starting a job
  458. if (unlikely(mythr->busy_state == TBS_STARTING_JOB))
  459. goto defer_events;
  460. is_running = mythr->work;
  461. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  462. if (should_be_running)
  463. {
  464. if (unlikely(!(is_running || mythr->_job_transition_in_progress)))
  465. {
  466. mt_disable_finish(mythr);
  467. goto djp;
  468. }
  469. if (unlikely(mythr->work_restart))
  470. goto djp;
  471. }
  472. else // ! should_be_running
  473. {
  474. if (unlikely((is_running || !mythr->_mt_disable_called) && !mythr->_job_transition_in_progress))
  475. {
  476. disabled: ;
  477. timer_unset(&mythr->tv_morework);
  478. if (is_running)
  479. {
  480. if (mythr->busy_state != TBS_GETTING_RESULTS)
  481. do_get_results(mythr, false);
  482. else
  483. // Avoid starting job when pending result fetch completes
  484. mythr->_proceed_with_new_job = false;
  485. }
  486. else // !mythr->_mt_disable_called
  487. mt_disable_start(mythr);
  488. }
  489. }
  490. if (timer_passed(&mythr->tv_morework, &tv_now))
  491. {
  492. djp: ;
  493. if (!do_job_prepare(mythr, &tv_now))
  494. goto disabled;
  495. }
  496. defer_events:
  497. if (timer_passed(&mythr->tv_poll, &tv_now))
  498. api->poll(mythr);
  499. if (timer_passed(&mythr->tv_watchdog, &tv_now))
  500. {
  501. timer_set_delay(&mythr->tv_watchdog, &tv_now, WATCHDOG_INTERVAL * 1000000);
  502. bfg_watchdog(proc, &tv_now);
  503. }
  504. reduce_timeout_to(&tv_timeout, &mythr->tv_morework);
  505. reduce_timeout_to(&tv_timeout, &mythr->tv_poll);
  506. reduce_timeout_to(&tv_timeout, &mythr->tv_watchdog);
  507. }
  508. do_notifier_select(thr, &tv_timeout);
  509. }
  510. }
  511. static
  512. void do_queue_flush(struct thr_info *mythr)
  513. {
  514. struct cgpu_info *proc = mythr->cgpu;
  515. struct device_drv *api = proc->drv;
  516. api->queue_flush(mythr);
  517. if (mythr->next_work)
  518. {
  519. free_work(mythr->next_work);
  520. mythr->next_work = NULL;
  521. }
  522. }
  523. void minerloop_queue(struct thr_info *thr)
  524. {
  525. struct thr_info *mythr;
  526. struct cgpu_info *cgpu = thr->cgpu;
  527. struct device_drv *api = cgpu->drv;
  528. struct timeval tv_now;
  529. struct timeval tv_timeout;
  530. struct cgpu_info *proc;
  531. bool should_be_running;
  532. struct work *work;
  533. _minerloop_setup(thr);
  534. while (likely(!cgpu->shutdown)) {
  535. tv_timeout.tv_sec = -1;
  536. timer_set_now(&tv_now);
  537. for (proc = cgpu; proc; proc = proc->next_proc)
  538. {
  539. mythr = proc->thr[0];
  540. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  541. redo:
  542. if (should_be_running)
  543. {
  544. if (unlikely(mythr->_mt_disable_called))
  545. mt_disable_finish(mythr);
  546. if (unlikely(mythr->work_restart))
  547. {
  548. mythr->work_restart = false;
  549. do_queue_flush(mythr);
  550. }
  551. while (!mythr->queue_full)
  552. {
  553. if (mythr->next_work)
  554. {
  555. work = mythr->next_work;
  556. mythr->next_work = NULL;
  557. }
  558. else
  559. {
  560. request_work(mythr);
  561. // FIXME: Allow get_work to return NULL to retry on notification
  562. work = get_and_prepare_work(mythr);
  563. }
  564. if (!work)
  565. break;
  566. if (!api->queue_append(mythr, work))
  567. mythr->next_work = work;
  568. }
  569. }
  570. else
  571. if (unlikely(!mythr->_mt_disable_called))
  572. {
  573. do_queue_flush(mythr);
  574. mt_disable_start(mythr);
  575. }
  576. if (timer_passed(&mythr->tv_poll, &tv_now))
  577. api->poll(mythr);
  578. if (timer_passed(&mythr->tv_watchdog, &tv_now))
  579. {
  580. timer_set_delay(&mythr->tv_watchdog, &tv_now, WATCHDOG_INTERVAL * 1000000);
  581. bfg_watchdog(proc, &tv_now);
  582. }
  583. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  584. if (should_be_running && !mythr->queue_full)
  585. goto redo;
  586. reduce_timeout_to(&tv_timeout, &mythr->tv_poll);
  587. reduce_timeout_to(&tv_timeout, &mythr->tv_watchdog);
  588. }
  589. do_notifier_select(thr, &tv_timeout);
  590. }
  591. }
  592. void *miner_thread(void *userdata)
  593. {
  594. struct thr_info *mythr = userdata;
  595. struct cgpu_info *cgpu = mythr->cgpu;
  596. struct device_drv *drv = cgpu->drv;
  597. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  598. char threadname[20];
  599. snprintf(threadname, 20, "miner_%s", cgpu->proc_repr_ns);
  600. RenameThread(threadname);
  601. if (drv->thread_init && !drv->thread_init(mythr)) {
  602. dev_error(cgpu, REASON_THREAD_FAIL_INIT);
  603. for (struct cgpu_info *slave = cgpu->next_proc; slave && !slave->threads; slave = slave->next_proc)
  604. dev_error(slave, REASON_THREAD_FAIL_INIT);
  605. __thr_being_msg(LOG_ERR, mythr, "failure, exiting");
  606. goto out;
  607. }
  608. if (drv_ready(cgpu))
  609. cgpu_set_defaults(cgpu);
  610. thread_reportout(mythr);
  611. applog(LOG_DEBUG, "Popping ping in miner thread");
  612. notifier_read(mythr->notifier); // Wait for a notification to start
  613. cgtime(&cgpu->cgminer_stats.start_tv);
  614. if (drv->minerloop)
  615. drv->minerloop(mythr);
  616. else
  617. minerloop_scanhash(mythr);
  618. __thr_being_msg(LOG_NOTICE, mythr, "shutting down");
  619. out: ;
  620. struct cgpu_info *proc = cgpu;
  621. do
  622. {
  623. proc->deven = DEV_DISABLED;
  624. proc->status = LIFE_DEAD2;
  625. }
  626. while ( (proc = proc->next_proc) && !proc->threads);
  627. mythr->getwork = 0;
  628. mythr->has_pth = false;
  629. cgsleep_ms(1);
  630. if (drv->thread_shutdown)
  631. drv->thread_shutdown(mythr);
  632. notifier_destroy(mythr->notifier);
  633. return NULL;
  634. }
  635. static pthread_mutex_t _add_cgpu_mutex = PTHREAD_MUTEX_INITIALIZER;
  636. static
  637. bool _add_cgpu(struct cgpu_info *cgpu)
  638. {
  639. int lpcount;
  640. renumber_cgpu(cgpu);
  641. if (!cgpu->procs)
  642. cgpu->procs = 1;
  643. lpcount = cgpu->procs;
  644. cgpu->device = cgpu;
  645. cgpu->dev_repr = malloc(6);
  646. sprintf(cgpu->dev_repr, "%s%2u", cgpu->drv->name, cgpu->device_id % 100);
  647. cgpu->dev_repr_ns = malloc(6);
  648. sprintf(cgpu->dev_repr_ns, "%s%u", cgpu->drv->name, cgpu->device_id % 100);
  649. strcpy(cgpu->proc_repr, cgpu->dev_repr);
  650. sprintf(cgpu->proc_repr_ns, "%s%u", cgpu->drv->name, cgpu->device_id);
  651. #ifdef NEED_BFG_LOWL_VCOM
  652. maybe_strdup_if_null(&cgpu->dev_manufacturer, detectone_meta_info.manufacturer);
  653. maybe_strdup_if_null(&cgpu->dev_product, detectone_meta_info.product);
  654. maybe_strdup_if_null(&cgpu->dev_serial, detectone_meta_info.serial);
  655. #endif
  656. devices_new = realloc(devices_new, sizeof(struct cgpu_info *) * (total_devices_new + lpcount + 1));
  657. devices_new[total_devices_new++] = cgpu;
  658. if (lpcount > 1)
  659. {
  660. int ns;
  661. int tpp = cgpu->threads / lpcount;
  662. struct cgpu_info **nlp_p, *slave;
  663. const bool manylp = (lpcount > 26);
  664. const char *as = (manylp ? "aa" : "a");
  665. // Note, strcpy instead of assigning a byte to get the \0 too
  666. strcpy(&cgpu->proc_repr[5], as);
  667. ns = strlen(cgpu->proc_repr_ns);
  668. strcpy(&cgpu->proc_repr_ns[ns], as);
  669. nlp_p = &cgpu->next_proc;
  670. for (int i = 1; i < lpcount; ++i)
  671. {
  672. slave = malloc(sizeof(*slave));
  673. *slave = *cgpu;
  674. slave->proc_id = i;
  675. if (manylp)
  676. {
  677. slave->proc_repr[5] += i / 26;
  678. slave->proc_repr[6] += i % 26;
  679. slave->proc_repr_ns[ns ] += i / 26;
  680. slave->proc_repr_ns[ns + 1] += i % 26;
  681. }
  682. else
  683. {
  684. slave->proc_repr[5] += i;
  685. slave->proc_repr_ns[ns] += i;
  686. }
  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. cgpu->last_device_valid_work = time(NULL);
  697. return true;
  698. }
  699. bool add_cgpu(struct cgpu_info *cgpu)
  700. {
  701. mutex_lock(&_add_cgpu_mutex);
  702. const bool rv = _add_cgpu(cgpu);
  703. mutex_unlock(&_add_cgpu_mutex);
  704. return rv;
  705. }
  706. void add_cgpu_live(void *p)
  707. {
  708. add_cgpu(p);
  709. }
  710. bool add_cgpu_slave(struct cgpu_info *cgpu, struct cgpu_info *prev_cgpu)
  711. {
  712. if (!prev_cgpu)
  713. return add_cgpu(cgpu);
  714. while (prev_cgpu->next_proc)
  715. prev_cgpu = prev_cgpu->next_proc;
  716. mutex_lock(&_add_cgpu_mutex);
  717. int old_total_devices = total_devices_new;
  718. if (!_add_cgpu(cgpu))
  719. {
  720. mutex_unlock(&_add_cgpu_mutex);
  721. return false;
  722. }
  723. prev_cgpu->next_proc = devices_new[old_total_devices];
  724. mutex_unlock(&_add_cgpu_mutex);
  725. return true;
  726. }
  727. #ifdef NEED_BFG_LOWL_VCOM
  728. bool _serial_detect_all(struct lowlevel_device_info * const info, void * const userp)
  729. {
  730. detectone_func_t detectone = userp;
  731. if (serial_claim(info->path, NULL))
  732. applogr(false, LOG_DEBUG, "%s is already claimed... skipping probes", info->path);
  733. return detectone(info->path);
  734. }
  735. #endif
  736. int _serial_detect(struct device_drv *api, detectone_func_t detectone, autoscan_func_t autoscan, int flags)
  737. {
  738. struct string_elist *iter, *tmp;
  739. const char *dev, *colon;
  740. bool inhibitauto = flags & 4;
  741. char found = 0;
  742. bool forceauto = flags & 1;
  743. bool hasname;
  744. bool doall = false;
  745. size_t namel = strlen(api->name);
  746. size_t dnamel = strlen(api->dname);
  747. #ifdef NEED_BFG_LOWL_VCOM
  748. clear_detectone_meta_info();
  749. #endif
  750. DL_FOREACH_SAFE(scan_devices, iter, tmp) {
  751. dev = iter->string;
  752. if ((colon = strchr(dev, ':')) && colon[1] != '\0') {
  753. size_t idlen = colon - dev;
  754. // allow either name:device or dname:device
  755. if ((idlen != namel || strncasecmp(dev, api->name, idlen))
  756. && (idlen != dnamel || strncasecmp(dev, api->dname, idlen)))
  757. continue;
  758. dev = colon + 1;
  759. hasname = true;
  760. }
  761. else
  762. hasname = false;
  763. if (!strcmp(dev, "auto"))
  764. forceauto = true;
  765. else if (!strcmp(dev, "noauto"))
  766. inhibitauto = true;
  767. else
  768. if ((flags & 2) && !hasname)
  769. continue;
  770. else
  771. if (!detectone)
  772. {} // do nothing
  773. else
  774. if (!strcmp(dev, "all"))
  775. doall = true;
  776. #ifdef NEED_BFG_LOWL_VCOM
  777. else
  778. if (serial_claim(dev, NULL))
  779. {
  780. applog(LOG_DEBUG, "%s is already claimed... skipping probes", dev);
  781. string_elist_del(&scan_devices, iter);
  782. }
  783. #endif
  784. else if (detectone(dev)) {
  785. string_elist_del(&scan_devices, iter);
  786. ++found;
  787. }
  788. }
  789. #ifdef NEED_BFG_LOWL_VCOM
  790. if (doall && detectone)
  791. found += lowlevel_detect_id(_serial_detect_all, detectone, &lowl_vcom, 0, 0);
  792. #endif
  793. if ((forceauto || !(inhibitauto || found)) && autoscan)
  794. found += autoscan();
  795. return found;
  796. }
  797. static
  798. FILE *_open_bitstream(const char *path, const char *subdir, const char *sub2, const char *filename)
  799. {
  800. char fullpath[PATH_MAX];
  801. strcpy(fullpath, path);
  802. strcat(fullpath, "/");
  803. if (subdir) {
  804. strcat(fullpath, subdir);
  805. strcat(fullpath, "/");
  806. }
  807. if (sub2) {
  808. strcat(fullpath, sub2);
  809. strcat(fullpath, "/");
  810. }
  811. strcat(fullpath, filename);
  812. return fopen(fullpath, "rb");
  813. }
  814. #define _open_bitstream(path, subdir, sub2) do { \
  815. f = _open_bitstream(path, subdir, sub2, filename); \
  816. if (f) \
  817. return f; \
  818. } while(0)
  819. #define _open_bitstream2(path, path3) do { \
  820. _open_bitstream(path, NULL, path3); \
  821. _open_bitstream(path, "../share/" PACKAGE, path3); \
  822. _open_bitstream(path, "../" PACKAGE, path3); \
  823. } while(0)
  824. #define _open_bitstream3(path) do { \
  825. _open_bitstream2(path, dname); \
  826. _open_bitstream2(path, "bitstreams"); \
  827. _open_bitstream2(path, NULL); \
  828. } while(0)
  829. FILE *open_bitstream(const char *dname, const char *filename)
  830. {
  831. FILE *f;
  832. _open_bitstream3(opt_kernel_path);
  833. _open_bitstream3(cgminer_path);
  834. _open_bitstream3(".");
  835. return NULL;
  836. }
  837. void close_device_fd(struct thr_info * const thr)
  838. {
  839. struct cgpu_info * const proc = thr->cgpu;
  840. const int fd = proc->device_fd;
  841. if (fd == -1)
  842. return;
  843. if (close(fd))
  844. applog(LOG_WARNING, "%"PRIpreprv": Error closing device fd", proc->proc_repr);
  845. else
  846. {
  847. proc->device_fd = -1;
  848. applog(LOG_DEBUG, "%"PRIpreprv": Closed device fd", proc->proc_repr);
  849. }
  850. }