deviceapi.c 25 KB

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