deviceapi.c 23 KB

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