deviceapi.c 24 KB

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