deviceapi.c 20 KB

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