deviceapi.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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. bool do_job_prepare(struct thr_info *mythr, struct timeval *tvp_now)
  237. {
  238. struct cgpu_info *proc = mythr->cgpu;
  239. struct device_drv *api = proc->drv;
  240. struct timeval tv_worktime;
  241. mythr->tv_morework.tv_sec = -1;
  242. mythr->_job_transition_in_progress = true;
  243. if (mythr->work)
  244. timersub(tvp_now, &mythr->work->tv_work_start, &tv_worktime);
  245. if ((!mythr->work) || abandon_work(mythr->work, &tv_worktime, proc->max_hashes))
  246. {
  247. mythr->work_restart = false;
  248. request_work(mythr);
  249. // FIXME: Allow get_work to return NULL to retry on notification
  250. if (mythr->next_work)
  251. free_work(mythr->next_work);
  252. mythr->next_work = get_and_prepare_work(mythr);
  253. if (!mythr->next_work)
  254. return false;
  255. mythr->starting_next_work = true;
  256. api->job_prepare(mythr, mythr->next_work, mythr->_max_nonce);
  257. }
  258. else
  259. {
  260. mythr->starting_next_work = false;
  261. api->job_prepare(mythr, mythr->work, mythr->_max_nonce);
  262. }
  263. job_prepare_complete(mythr);
  264. return true;
  265. }
  266. void job_prepare_complete(struct thr_info *mythr)
  267. {
  268. if (unlikely(mythr->busy_state == TBS_GETTING_RESULTS))
  269. return;
  270. if (mythr->work)
  271. {
  272. if (true /* TODO: job is near complete */ || unlikely(mythr->work_restart))
  273. do_get_results(mythr, true);
  274. else
  275. {} // TODO: Set a timer to call do_get_results when job is near complete
  276. }
  277. else // no job currently running
  278. do_job_start(mythr);
  279. }
  280. void do_get_results(struct thr_info *mythr, bool proceed_with_new_job)
  281. {
  282. struct cgpu_info *proc = mythr->cgpu;
  283. struct device_drv *api = proc->drv;
  284. struct work *work = mythr->work;
  285. mythr->_job_transition_in_progress = true;
  286. mythr->tv_results_jobstart = mythr->tv_jobstart;
  287. mythr->_proceed_with_new_job = proceed_with_new_job;
  288. if (api->job_get_results)
  289. api->job_get_results(mythr, work);
  290. else
  291. job_results_fetched(mythr);
  292. }
  293. void job_results_fetched(struct thr_info *mythr)
  294. {
  295. if (mythr->_proceed_with_new_job)
  296. do_job_start(mythr);
  297. else
  298. {
  299. if (likely(mythr->prev_work))
  300. {
  301. struct timeval tv_now;
  302. timer_set_now(&tv_now);
  303. do_process_results(mythr, &tv_now, mythr->prev_work, true);
  304. }
  305. mt_disable_start(mythr);
  306. }
  307. }
  308. void do_job_start(struct thr_info *mythr)
  309. {
  310. struct cgpu_info *proc = mythr->cgpu;
  311. struct device_drv *api = proc->drv;
  312. thread_reportin(mythr);
  313. api->job_start(mythr);
  314. }
  315. void mt_job_transition(struct thr_info *mythr)
  316. {
  317. struct timeval tv_now;
  318. timer_set_now(&tv_now);
  319. if (mythr->starting_next_work)
  320. {
  321. mythr->next_work->tv_work_start = tv_now;
  322. if (mythr->prev_work)
  323. free_work(mythr->prev_work);
  324. mythr->prev_work = mythr->work;
  325. mythr->work = mythr->next_work;
  326. mythr->next_work = NULL;
  327. }
  328. mythr->tv_jobstart = tv_now;
  329. mythr->_job_transition_in_progress = false;
  330. }
  331. void job_start_complete(struct thr_info *mythr)
  332. {
  333. struct timeval tv_now;
  334. if (unlikely(!mythr->prev_work))
  335. return;
  336. timer_set_now(&tv_now);
  337. do_process_results(mythr, &tv_now, mythr->prev_work, false);
  338. }
  339. void job_start_abort(struct thr_info *mythr, bool failure)
  340. {
  341. struct cgpu_info *proc = mythr->cgpu;
  342. if (failure)
  343. {
  344. proc->deven = DEV_RECOVER_ERR;
  345. run_cmd(cmd_idle);
  346. }
  347. mythr->work = NULL;
  348. mythr->_job_transition_in_progress = false;
  349. }
  350. bool do_process_results(struct thr_info *mythr, struct timeval *tvp_now, struct work *work, bool stopping)
  351. {
  352. struct cgpu_info *proc = mythr->cgpu;
  353. struct device_drv *api = proc->drv;
  354. struct timeval tv_hashes;
  355. int64_t hashes = 0;
  356. if (api->job_process_results)
  357. hashes = api->job_process_results(mythr, work, stopping);
  358. thread_reportin(mythr);
  359. if (hashes)
  360. {
  361. timersub(tvp_now, &mythr->tv_results_jobstart, &tv_hashes);
  362. if (!hashes_done(mythr, hashes, &tv_hashes, api->can_limit_work ? &mythr->_max_nonce : NULL))
  363. return false;
  364. }
  365. return true;
  366. }
  367. static
  368. void do_notifier_select(struct thr_info *thr, struct timeval *tvp_timeout)
  369. {
  370. struct cgpu_info *cgpu = thr->cgpu;
  371. struct timeval tv_now;
  372. int maxfd;
  373. fd_set rfds;
  374. timer_set_now(&tv_now);
  375. FD_ZERO(&rfds);
  376. FD_SET(thr->notifier[0], &rfds);
  377. maxfd = thr->notifier[0];
  378. FD_SET(thr->work_restart_notifier[0], &rfds);
  379. set_maxfd(&maxfd, thr->work_restart_notifier[0]);
  380. if (thr->mutex_request[1] != INVSOCK)
  381. {
  382. FD_SET(thr->mutex_request[0], &rfds);
  383. set_maxfd(&maxfd, thr->mutex_request[0]);
  384. }
  385. if (select(maxfd + 1, &rfds, NULL, NULL, select_timeout(tvp_timeout, &tv_now)) < 0)
  386. return;
  387. if (thr->mutex_request[1] != INVSOCK && FD_ISSET(thr->mutex_request[0], &rfds))
  388. {
  389. // FIXME: This can only handle one request at a time!
  390. pthread_mutex_t *mutexp = &cgpu->device_mutex;
  391. notifier_read(thr->mutex_request);
  392. mutex_lock(mutexp);
  393. pthread_cond_signal(&cgpu->device_cond);
  394. pthread_cond_wait(&cgpu->device_cond, mutexp);
  395. mutex_unlock(mutexp);
  396. }
  397. if (FD_ISSET(thr->notifier[0], &rfds)) {
  398. notifier_read(thr->notifier);
  399. }
  400. if (FD_ISSET(thr->work_restart_notifier[0], &rfds))
  401. notifier_read(thr->work_restart_notifier);
  402. }
  403. static
  404. void _minerloop_setup(struct thr_info *mythr)
  405. {
  406. struct cgpu_info * const cgpu = mythr->cgpu, *proc;
  407. if (mythr->work_restart_notifier[1] == -1)
  408. notifier_init(mythr->work_restart_notifier);
  409. for (proc = cgpu; proc; proc = proc->next_proc)
  410. {
  411. mythr = proc->thr[0];
  412. timer_set_now(&mythr->tv_watchdog);
  413. proc->disable_watchdog = true;
  414. }
  415. }
  416. void minerloop_async(struct thr_info *mythr)
  417. {
  418. struct thr_info *thr = mythr;
  419. struct cgpu_info *cgpu = mythr->cgpu;
  420. struct device_drv *api = cgpu->drv;
  421. struct timeval tv_now;
  422. struct timeval tv_timeout;
  423. struct cgpu_info *proc;
  424. bool is_running, should_be_running;
  425. _minerloop_setup(mythr);
  426. while (likely(!cgpu->shutdown)) {
  427. tv_timeout.tv_sec = -1;
  428. timer_set_now(&tv_now);
  429. for (proc = cgpu; proc; proc = proc->next_proc)
  430. {
  431. mythr = proc->thr[0];
  432. // Nothing should happen while we're starting a job
  433. if (unlikely(mythr->busy_state == TBS_STARTING_JOB))
  434. goto defer_events;
  435. is_running = mythr->work;
  436. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  437. if (should_be_running)
  438. {
  439. if (unlikely(!(is_running || mythr->_job_transition_in_progress)))
  440. {
  441. mt_disable_finish(mythr);
  442. goto djp;
  443. }
  444. if (unlikely(mythr->work_restart))
  445. goto djp;
  446. }
  447. else // ! should_be_running
  448. {
  449. if (unlikely((is_running || !mythr->_mt_disable_called) && !mythr->_job_transition_in_progress))
  450. {
  451. disabled: ;
  452. timer_unset(&mythr->tv_morework);
  453. if (is_running)
  454. {
  455. if (mythr->busy_state != TBS_GETTING_RESULTS)
  456. do_get_results(mythr, false);
  457. else
  458. // Avoid starting job when pending result fetch completes
  459. mythr->_proceed_with_new_job = false;
  460. }
  461. else // !mythr->_mt_disable_called
  462. mt_disable_start(mythr);
  463. }
  464. }
  465. if (timer_passed(&mythr->tv_morework, &tv_now))
  466. {
  467. djp: ;
  468. if (!do_job_prepare(mythr, &tv_now))
  469. goto disabled;
  470. }
  471. defer_events:
  472. if (timer_passed(&mythr->tv_poll, &tv_now))
  473. api->poll(mythr);
  474. if (timer_passed(&mythr->tv_watchdog, &tv_now))
  475. {
  476. timer_set_delay(&mythr->tv_watchdog, &tv_now, WATCHDOG_INTERVAL * 1000000);
  477. bfg_watchdog(proc, &tv_now);
  478. }
  479. reduce_timeout_to(&tv_timeout, &mythr->tv_morework);
  480. reduce_timeout_to(&tv_timeout, &mythr->tv_poll);
  481. reduce_timeout_to(&tv_timeout, &mythr->tv_watchdog);
  482. }
  483. do_notifier_select(thr, &tv_timeout);
  484. }
  485. }
  486. static
  487. void do_queue_flush(struct thr_info *mythr)
  488. {
  489. struct cgpu_info *proc = mythr->cgpu;
  490. struct device_drv *api = proc->drv;
  491. api->queue_flush(mythr);
  492. if (mythr->next_work)
  493. {
  494. free_work(mythr->next_work);
  495. mythr->next_work = NULL;
  496. }
  497. }
  498. void minerloop_queue(struct thr_info *thr)
  499. {
  500. struct thr_info *mythr;
  501. struct cgpu_info *cgpu = thr->cgpu;
  502. struct device_drv *api = cgpu->drv;
  503. struct timeval tv_now;
  504. struct timeval tv_timeout;
  505. struct cgpu_info *proc;
  506. bool should_be_running;
  507. struct work *work;
  508. _minerloop_setup(thr);
  509. while (likely(!cgpu->shutdown)) {
  510. tv_timeout.tv_sec = -1;
  511. timer_set_now(&tv_now);
  512. for (proc = cgpu; proc; proc = proc->next_proc)
  513. {
  514. mythr = proc->thr[0];
  515. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  516. redo:
  517. if (should_be_running)
  518. {
  519. if (unlikely(mythr->_mt_disable_called))
  520. mt_disable_finish(mythr);
  521. if (unlikely(mythr->work_restart))
  522. {
  523. mythr->work_restart = false;
  524. do_queue_flush(mythr);
  525. }
  526. while (!mythr->queue_full)
  527. {
  528. if (mythr->next_work)
  529. {
  530. work = mythr->next_work;
  531. mythr->next_work = NULL;
  532. }
  533. else
  534. {
  535. request_work(mythr);
  536. // FIXME: Allow get_work to return NULL to retry on notification
  537. work = get_and_prepare_work(mythr);
  538. }
  539. if (!work)
  540. break;
  541. if (!api->queue_append(mythr, work))
  542. mythr->next_work = work;
  543. }
  544. }
  545. else
  546. if (unlikely(!mythr->_mt_disable_called))
  547. {
  548. do_queue_flush(mythr);
  549. mt_disable_start(mythr);
  550. }
  551. if (timer_passed(&mythr->tv_poll, &tv_now))
  552. api->poll(mythr);
  553. if (timer_passed(&mythr->tv_watchdog, &tv_now))
  554. {
  555. timer_set_delay(&mythr->tv_watchdog, &tv_now, WATCHDOG_INTERVAL * 1000000);
  556. bfg_watchdog(proc, &tv_now);
  557. }
  558. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  559. if (should_be_running && !mythr->queue_full)
  560. goto redo;
  561. reduce_timeout_to(&tv_timeout, &mythr->tv_poll);
  562. reduce_timeout_to(&tv_timeout, &mythr->tv_watchdog);
  563. }
  564. do_notifier_select(thr, &tv_timeout);
  565. }
  566. }
  567. void *miner_thread(void *userdata)
  568. {
  569. struct thr_info *mythr = userdata;
  570. struct cgpu_info *cgpu = mythr->cgpu;
  571. struct device_drv *drv = cgpu->drv;
  572. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  573. char threadname[20];
  574. snprintf(threadname, 20, "miner_%s", cgpu->proc_repr_ns);
  575. RenameThread(threadname);
  576. if (drv->thread_init && !drv->thread_init(mythr)) {
  577. dev_error(cgpu, REASON_THREAD_FAIL_INIT);
  578. for (struct cgpu_info *slave = cgpu->next_proc; slave && !slave->threads; slave = slave->next_proc)
  579. dev_error(slave, REASON_THREAD_FAIL_INIT);
  580. __thr_being_msg(LOG_ERR, mythr, "failure, exiting");
  581. goto out;
  582. }
  583. if (drv_ready(cgpu))
  584. cgpu_set_defaults(cgpu);
  585. thread_reportout(mythr);
  586. applog(LOG_DEBUG, "Popping ping in miner thread");
  587. notifier_read(mythr->notifier); // Wait for a notification to start
  588. cgtime(&cgpu->cgminer_stats.start_tv);
  589. if (drv->minerloop)
  590. drv->minerloop(mythr);
  591. else
  592. minerloop_scanhash(mythr);
  593. __thr_being_msg(LOG_NOTICE, mythr, "shutting down");
  594. out: ;
  595. struct cgpu_info *proc = cgpu;
  596. do
  597. {
  598. proc->deven = DEV_DISABLED;
  599. proc->status = LIFE_DEAD2;
  600. }
  601. while ( (proc = proc->next_proc) && !proc->threads);
  602. mythr->getwork = 0;
  603. mythr->has_pth = false;
  604. cgsleep_ms(1);
  605. if (drv->thread_shutdown)
  606. drv->thread_shutdown(mythr);
  607. notifier_destroy(mythr->notifier);
  608. return NULL;
  609. }
  610. static pthread_mutex_t _add_cgpu_mutex = PTHREAD_MUTEX_INITIALIZER;
  611. static
  612. bool _add_cgpu(struct cgpu_info *cgpu)
  613. {
  614. int lpcount;
  615. renumber_cgpu(cgpu);
  616. if (!cgpu->procs)
  617. cgpu->procs = 1;
  618. lpcount = cgpu->procs;
  619. cgpu->device = cgpu;
  620. cgpu->dev_repr = malloc(6);
  621. sprintf(cgpu->dev_repr, "%s%2u", cgpu->drv->name, cgpu->device_id % 100);
  622. cgpu->dev_repr_ns = malloc(6);
  623. sprintf(cgpu->dev_repr_ns, "%s%u", cgpu->drv->name, cgpu->device_id % 100);
  624. strcpy(cgpu->proc_repr, cgpu->dev_repr);
  625. sprintf(cgpu->proc_repr_ns, "%s%u", cgpu->drv->name, cgpu->device_id);
  626. #ifdef HAVE_FPGAUTILS
  627. maybe_strdup_if_null(&cgpu->dev_manufacturer, detectone_meta_info.manufacturer);
  628. maybe_strdup_if_null(&cgpu->dev_product, detectone_meta_info.product);
  629. maybe_strdup_if_null(&cgpu->dev_serial, detectone_meta_info.serial);
  630. #endif
  631. devices_new = realloc(devices_new, sizeof(struct cgpu_info *) * (total_devices_new + lpcount + 1));
  632. devices_new[total_devices_new++] = cgpu;
  633. if (lpcount > 1)
  634. {
  635. int ns;
  636. int tpp = cgpu->threads / lpcount;
  637. struct cgpu_info **nlp_p, *slave;
  638. const bool manylp = (lpcount > 26);
  639. const char *as = (manylp ? "aa" : "a");
  640. // Note, strcpy instead of assigning a byte to get the \0 too
  641. strcpy(&cgpu->proc_repr[5], as);
  642. ns = strlen(cgpu->proc_repr_ns);
  643. strcpy(&cgpu->proc_repr_ns[ns], as);
  644. nlp_p = &cgpu->next_proc;
  645. for (int i = 1; i < lpcount; ++i)
  646. {
  647. slave = malloc(sizeof(*slave));
  648. *slave = *cgpu;
  649. slave->proc_id = i;
  650. if (manylp)
  651. {
  652. slave->proc_repr[5] += i / 26;
  653. slave->proc_repr[6] += i % 26;
  654. slave->proc_repr_ns[ns ] += i / 26;
  655. slave->proc_repr_ns[ns + 1] += i % 26;
  656. }
  657. else
  658. {
  659. slave->proc_repr[5] += i;
  660. slave->proc_repr_ns[ns] += i;
  661. }
  662. slave->threads = tpp;
  663. devices_new[total_devices_new++] = slave;
  664. *nlp_p = slave;
  665. nlp_p = &slave->next_proc;
  666. }
  667. *nlp_p = NULL;
  668. cgpu->proc_id = 0;
  669. cgpu->threads -= (tpp * (lpcount - 1));
  670. }
  671. cgpu->last_device_valid_work = time(NULL);
  672. return true;
  673. }
  674. bool add_cgpu(struct cgpu_info *cgpu)
  675. {
  676. mutex_lock(&_add_cgpu_mutex);
  677. const bool rv = _add_cgpu(cgpu);
  678. mutex_unlock(&_add_cgpu_mutex);
  679. return rv;
  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. if (!prev_cgpu)
  688. return add_cgpu(cgpu);
  689. while (prev_cgpu->next_proc)
  690. prev_cgpu = prev_cgpu->next_proc;
  691. mutex_lock(&_add_cgpu_mutex);
  692. int old_total_devices = total_devices_new;
  693. if (!_add_cgpu(cgpu))
  694. {
  695. mutex_unlock(&_add_cgpu_mutex);
  696. return false;
  697. }
  698. prev_cgpu->next_proc = devices_new[old_total_devices];
  699. mutex_unlock(&_add_cgpu_mutex);
  700. return true;
  701. }
  702. #ifdef HAVE_FPGAUTILS
  703. bool _serial_detect_all(struct lowlevel_device_info * const info, void * const userp)
  704. {
  705. detectone_func_t detectone = userp;
  706. if (serial_claim(info->path, NULL))
  707. applogr(false, LOG_DEBUG, "%s is already claimed... skipping probes", info->path);
  708. return detectone(info->path);
  709. }
  710. #endif
  711. int _serial_detect(struct device_drv *api, detectone_func_t detectone, autoscan_func_t autoscan, int flags)
  712. {
  713. struct string_elist *iter, *tmp;
  714. const char *dev, *colon;
  715. bool inhibitauto = flags & 4;
  716. char found = 0;
  717. bool forceauto = flags & 1;
  718. bool hasname;
  719. bool doall = false;
  720. size_t namel = strlen(api->name);
  721. size_t dnamel = strlen(api->dname);
  722. #ifdef HAVE_FPGAUTILS
  723. clear_detectone_meta_info();
  724. #endif
  725. DL_FOREACH_SAFE(scan_devices, iter, tmp) {
  726. dev = iter->string;
  727. if ((colon = strchr(dev, ':')) && colon[1] != '\0') {
  728. size_t idlen = colon - dev;
  729. // allow either name:device or dname:device
  730. if ((idlen != namel || strncasecmp(dev, api->name, idlen))
  731. && (idlen != dnamel || strncasecmp(dev, api->dname, idlen)))
  732. continue;
  733. dev = colon + 1;
  734. hasname = true;
  735. }
  736. else
  737. hasname = false;
  738. if (!strcmp(dev, "auto"))
  739. forceauto = true;
  740. else if (!strcmp(dev, "noauto"))
  741. inhibitauto = true;
  742. else
  743. if ((flags & 2) && !hasname)
  744. continue;
  745. else
  746. if (!detectone)
  747. {} // do nothing
  748. else
  749. if (!strcmp(dev, "all"))
  750. doall = true;
  751. #ifdef HAVE_FPGAUTILS
  752. else
  753. if (serial_claim(dev, NULL))
  754. {
  755. applog(LOG_DEBUG, "%s is already claimed... skipping probes", dev);
  756. string_elist_del(&scan_devices, iter);
  757. }
  758. #endif
  759. else if (detectone(dev)) {
  760. string_elist_del(&scan_devices, iter);
  761. ++found;
  762. }
  763. }
  764. #ifdef HAVE_FPGAUTILS
  765. if (doall && detectone)
  766. found += lowlevel_detect_id(_serial_detect_all, detectone, &lowl_vcom, 0, 0);
  767. #endif
  768. if ((forceauto || !(inhibitauto || found)) && autoscan)
  769. found += autoscan();
  770. return found;
  771. }
  772. static
  773. FILE *_open_bitstream(const char *path, const char *subdir, const char *sub2, const char *filename)
  774. {
  775. char fullpath[PATH_MAX];
  776. strcpy(fullpath, path);
  777. strcat(fullpath, "/");
  778. if (subdir) {
  779. strcat(fullpath, subdir);
  780. strcat(fullpath, "/");
  781. }
  782. if (sub2) {
  783. strcat(fullpath, sub2);
  784. strcat(fullpath, "/");
  785. }
  786. strcat(fullpath, filename);
  787. return fopen(fullpath, "rb");
  788. }
  789. #define _open_bitstream(path, subdir, sub2) do { \
  790. f = _open_bitstream(path, subdir, sub2, filename); \
  791. if (f) \
  792. return f; \
  793. } while(0)
  794. #define _open_bitstream2(path, path3) do { \
  795. _open_bitstream(path, NULL, path3); \
  796. _open_bitstream(path, "../share/" PACKAGE, path3); \
  797. _open_bitstream(path, "../" PACKAGE, path3); \
  798. } while(0)
  799. #define _open_bitstream3(path) do { \
  800. _open_bitstream2(path, dname); \
  801. _open_bitstream2(path, "bitstreams"); \
  802. _open_bitstream2(path, NULL); \
  803. } while(0)
  804. FILE *open_bitstream(const char *dname, const char *filename)
  805. {
  806. FILE *f;
  807. _open_bitstream3(opt_kernel_path);
  808. _open_bitstream3(cgminer_path);
  809. _open_bitstream3(".");
  810. return NULL;
  811. }
  812. void close_device_fd(struct thr_info * const thr)
  813. {
  814. struct cgpu_info * const proc = thr->cgpu;
  815. const int fd = proc->device_fd;
  816. if (fd == -1)
  817. return;
  818. if (close(fd))
  819. applog(LOG_WARNING, "%"PRIpreprv": Error closing device fd", proc->proc_repr);
  820. else
  821. {
  822. proc->device_fd = -1;
  823. applog(LOG_DEBUG, "%"PRIpreprv": Closed device fd", proc->proc_repr);
  824. }
  825. }