deviceapi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. {
  254. struct timeval tv_now;
  255. timer_set_now(&tv_now);
  256. do_process_results(mythr, &tv_now, mythr->prev_work, true);
  257. }
  258. }
  259. void do_job_start(struct thr_info *mythr)
  260. {
  261. struct cgpu_info *proc = mythr->cgpu;
  262. struct device_drv *api = proc->drv;
  263. thread_reportin(mythr);
  264. api->job_start(mythr);
  265. }
  266. void mt_job_transition(struct thr_info *mythr)
  267. {
  268. struct timeval tv_now;
  269. timer_set_now(&tv_now);
  270. if (mythr->starting_next_work)
  271. {
  272. mythr->next_work->tv_work_start = tv_now;
  273. if (mythr->prev_work)
  274. free_work(mythr->prev_work);
  275. mythr->prev_work = mythr->work;
  276. mythr->work = mythr->next_work;
  277. mythr->next_work = NULL;
  278. }
  279. mythr->tv_jobstart = tv_now;
  280. mythr->_job_transition_in_progress = false;
  281. }
  282. void job_start_complete(struct thr_info *mythr)
  283. {
  284. struct timeval tv_now;
  285. timer_set_now(&tv_now);
  286. do_process_results(mythr, &tv_now, mythr->prev_work, false);
  287. }
  288. void job_start_abort(struct thr_info *mythr, bool failure)
  289. {
  290. struct cgpu_info *proc = mythr->cgpu;
  291. if (failure)
  292. {
  293. proc->deven = DEV_RECOVER_ERR;
  294. run_cmd(cmd_idle);
  295. }
  296. mythr->work = NULL;
  297. mythr->_job_transition_in_progress = false;
  298. }
  299. bool do_process_results(struct thr_info *mythr, struct timeval *tvp_now, struct work *work, bool stopping)
  300. {
  301. struct cgpu_info *proc = mythr->cgpu;
  302. struct device_drv *api = proc->drv;
  303. struct timeval tv_hashes;
  304. int64_t hashes = 0;
  305. if (api->job_process_results)
  306. hashes = api->job_process_results(mythr, work, stopping);
  307. thread_reportin(mythr);
  308. if (hashes)
  309. {
  310. timersub(tvp_now, &mythr->tv_results_jobstart, &tv_hashes);
  311. if (!hashes_done(mythr, hashes, &tv_hashes, api->can_limit_work ? &mythr->_max_nonce : NULL))
  312. return false;
  313. }
  314. return true;
  315. }
  316. static
  317. void do_notifier_select(struct thr_info *thr, struct timeval *tvp_timeout)
  318. {
  319. struct cgpu_info *cgpu = thr->cgpu;
  320. struct timeval tv_now;
  321. int maxfd;
  322. fd_set rfds;
  323. timer_set_now(&tv_now);
  324. FD_ZERO(&rfds);
  325. FD_SET(thr->notifier[0], &rfds);
  326. maxfd = thr->notifier[0];
  327. FD_SET(thr->work_restart_notifier[0], &rfds);
  328. set_maxfd(&maxfd, thr->work_restart_notifier[0]);
  329. if (thr->mutex_request[1] != INVSOCK)
  330. {
  331. FD_SET(thr->mutex_request[0], &rfds);
  332. set_maxfd(&maxfd, thr->mutex_request[0]);
  333. }
  334. if (select(maxfd + 1, &rfds, NULL, NULL, select_timeout(tvp_timeout, &tv_now)) < 0)
  335. return;
  336. if (thr->mutex_request[1] != INVSOCK && FD_ISSET(thr->mutex_request[0], &rfds))
  337. {
  338. // FIXME: This can only handle one request at a time!
  339. pthread_mutex_t *mutexp = &cgpu->device_mutex;
  340. notifier_read(thr->mutex_request);
  341. mutex_lock(mutexp);
  342. pthread_cond_signal(&cgpu->device_cond);
  343. pthread_cond_wait(&cgpu->device_cond, mutexp);
  344. mutex_unlock(mutexp);
  345. }
  346. if (FD_ISSET(thr->notifier[0], &rfds)) {
  347. notifier_read(thr->notifier);
  348. }
  349. if (FD_ISSET(thr->work_restart_notifier[0], &rfds))
  350. notifier_read(thr->work_restart_notifier);
  351. }
  352. void minerloop_async(struct thr_info *mythr)
  353. {
  354. struct thr_info *thr = mythr;
  355. struct cgpu_info *cgpu = mythr->cgpu;
  356. struct device_drv *api = cgpu->drv;
  357. struct timeval tv_now;
  358. struct timeval tv_timeout;
  359. struct cgpu_info *proc;
  360. bool is_running, should_be_running;
  361. if (mythr->work_restart_notifier[1] == -1)
  362. notifier_init(mythr->work_restart_notifier);
  363. while (likely(!cgpu->shutdown)) {
  364. tv_timeout.tv_sec = -1;
  365. timer_set_now(&tv_now);
  366. for (proc = cgpu; proc; proc = proc->next_proc)
  367. {
  368. mythr = proc->thr[0];
  369. // Nothing should happen while we're starting a job
  370. if (unlikely(mythr->busy_state == TBS_STARTING_JOB))
  371. goto defer_events;
  372. is_running = mythr->work;
  373. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  374. if (should_be_running)
  375. {
  376. if (unlikely(!(is_running || mythr->_job_transition_in_progress)))
  377. {
  378. mt_disable_finish(mythr);
  379. goto djp;
  380. }
  381. if (unlikely(mythr->work_restart))
  382. goto djp;
  383. }
  384. else // ! should_be_running
  385. {
  386. if (unlikely(is_running && !mythr->_job_transition_in_progress))
  387. {
  388. disabled: ;
  389. mythr->tv_morework.tv_sec = -1;
  390. if (mythr->busy_state != TBS_GETTING_RESULTS)
  391. do_get_results(mythr, false);
  392. else
  393. // Avoid starting job when pending result fetch completes
  394. mythr->_proceed_with_new_job = false;
  395. }
  396. }
  397. if (timer_passed(&mythr->tv_morework, &tv_now))
  398. {
  399. djp: ;
  400. if (!do_job_prepare(mythr, &tv_now))
  401. goto disabled;
  402. }
  403. defer_events:
  404. if (timer_passed(&mythr->tv_poll, &tv_now))
  405. api->poll(mythr);
  406. reduce_timeout_to(&tv_timeout, &mythr->tv_morework);
  407. reduce_timeout_to(&tv_timeout, &mythr->tv_poll);
  408. }
  409. do_notifier_select(thr, &tv_timeout);
  410. }
  411. }
  412. static
  413. void do_queue_flush(struct thr_info *mythr)
  414. {
  415. struct cgpu_info *proc = mythr->cgpu;
  416. struct device_drv *api = proc->drv;
  417. api->queue_flush(mythr);
  418. if (mythr->next_work)
  419. {
  420. free_work(mythr->next_work);
  421. mythr->next_work = NULL;
  422. }
  423. }
  424. void minerloop_queue(struct thr_info *thr)
  425. {
  426. struct thr_info *mythr;
  427. struct cgpu_info *cgpu = thr->cgpu;
  428. struct device_drv *api = cgpu->drv;
  429. struct timeval tv_now;
  430. struct timeval tv_timeout;
  431. struct cgpu_info *proc;
  432. bool should_be_running;
  433. struct work *work;
  434. if (thr->work_restart_notifier[1] == -1)
  435. notifier_init(thr->work_restart_notifier);
  436. while (likely(!cgpu->shutdown)) {
  437. tv_timeout.tv_sec = -1;
  438. timer_set_now(&tv_now);
  439. for (proc = cgpu; proc; proc = proc->next_proc)
  440. {
  441. mythr = proc->thr[0];
  442. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  443. redo:
  444. if (should_be_running)
  445. {
  446. if (unlikely(!mythr->_last_sbr_state))
  447. {
  448. mt_disable_finish(mythr);
  449. mythr->_last_sbr_state = should_be_running;
  450. }
  451. if (unlikely(mythr->work_restart))
  452. {
  453. mythr->work_restart = false;
  454. do_queue_flush(mythr);
  455. }
  456. while (!mythr->queue_full)
  457. {
  458. if (mythr->next_work)
  459. {
  460. work = mythr->next_work;
  461. mythr->next_work = NULL;
  462. }
  463. else
  464. {
  465. request_work(mythr);
  466. // FIXME: Allow get_work to return NULL to retry on notification
  467. work = get_and_prepare_work(mythr);
  468. }
  469. if (!work)
  470. break;
  471. if (!api->queue_append(mythr, work))
  472. mythr->next_work = work;
  473. }
  474. }
  475. else
  476. if (unlikely(mythr->_last_sbr_state))
  477. {
  478. mythr->_last_sbr_state = should_be_running;
  479. do_queue_flush(mythr);
  480. }
  481. if (timer_passed(&mythr->tv_poll, &tv_now))
  482. api->poll(mythr);
  483. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  484. if (should_be_running && !mythr->queue_full)
  485. goto redo;
  486. reduce_timeout_to(&tv_timeout, &mythr->tv_poll);
  487. }
  488. do_notifier_select(thr, &tv_timeout);
  489. }
  490. }
  491. void *miner_thread(void *userdata)
  492. {
  493. struct thr_info *mythr = userdata;
  494. struct cgpu_info *cgpu = mythr->cgpu;
  495. struct device_drv *drv = cgpu->drv;
  496. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  497. char threadname[20];
  498. snprintf(threadname, 20, "miner_%s", cgpu->proc_repr_ns);
  499. RenameThread(threadname);
  500. if (drv->thread_init && !drv->thread_init(mythr)) {
  501. dev_error(cgpu, REASON_THREAD_FAIL_INIT);
  502. for (struct cgpu_info *slave = cgpu->next_proc; slave && !slave->threads; slave = slave->next_proc)
  503. dev_error(slave, REASON_THREAD_FAIL_INIT);
  504. __thr_being_msg(LOG_ERR, mythr, "failure, exiting");
  505. goto out;
  506. }
  507. thread_reportout(mythr);
  508. applog(LOG_DEBUG, "Popping ping in miner thread");
  509. notifier_read(mythr->notifier); // Wait for a notification to start
  510. cgtime(&cgpu->cgminer_stats.start_tv);
  511. if (drv->minerloop)
  512. drv->minerloop(mythr);
  513. else
  514. minerloop_scanhash(mythr);
  515. __thr_being_msg(LOG_NOTICE, mythr, "shutting down");
  516. out: ;
  517. struct cgpu_info *proc = cgpu;
  518. do
  519. {
  520. proc->deven = DEV_DISABLED;
  521. proc->status = LIFE_DEAD2;
  522. }
  523. while ( (proc = proc->next_proc) && !proc->threads);
  524. mythr->getwork = 0;
  525. mythr->has_pth = false;
  526. cgsleep_ms(1000);
  527. if (drv->thread_shutdown)
  528. drv->thread_shutdown(mythr);
  529. notifier_destroy(mythr->notifier);
  530. return NULL;
  531. }
  532. bool add_cgpu(struct cgpu_info *cgpu)
  533. {
  534. int lpcount;
  535. renumber_cgpu(cgpu);
  536. if (!cgpu->procs)
  537. cgpu->procs = 1;
  538. lpcount = cgpu->procs;
  539. cgpu->device = cgpu;
  540. cgpu->dev_repr = malloc(6);
  541. sprintf(cgpu->dev_repr, "%s%2u", cgpu->drv->name, cgpu->device_id % 100);
  542. cgpu->dev_repr_ns = malloc(6);
  543. sprintf(cgpu->dev_repr_ns, "%s%u", cgpu->drv->name, cgpu->device_id % 100);
  544. strcpy(cgpu->proc_repr, cgpu->dev_repr);
  545. sprintf(cgpu->proc_repr_ns, "%s%u", cgpu->drv->name, cgpu->device_id);
  546. #ifdef HAVE_FPGAUTILS
  547. maybe_strdup_if_null(&cgpu->dev_manufacturer, detectone_meta_info.manufacturer);
  548. maybe_strdup_if_null(&cgpu->dev_product, detectone_meta_info.product);
  549. maybe_strdup_if_null(&cgpu->dev_serial, detectone_meta_info.serial);
  550. #endif
  551. devices_new = realloc(devices_new, sizeof(struct cgpu_info *) * (total_devices_new + lpcount + 1));
  552. devices_new[total_devices_new++] = cgpu;
  553. if (lpcount > 1)
  554. {
  555. int ns;
  556. int tpp = cgpu->threads / lpcount;
  557. struct cgpu_info **nlp_p, *slave;
  558. const bool manylp = (lpcount > 26);
  559. const char *as = (manylp ? "aa" : "a");
  560. // Note, strcpy instead of assigning a byte to get the \0 too
  561. strcpy(&cgpu->proc_repr[5], as);
  562. ns = strlen(cgpu->proc_repr_ns);
  563. strcpy(&cgpu->proc_repr_ns[ns], as);
  564. nlp_p = &cgpu->next_proc;
  565. for (int i = 1; i < lpcount; ++i)
  566. {
  567. slave = malloc(sizeof(*slave));
  568. *slave = *cgpu;
  569. slave->proc_id = i;
  570. if (manylp)
  571. {
  572. slave->proc_repr[5] += i / 26;
  573. slave->proc_repr[6] += i % 26;
  574. slave->proc_repr_ns[ns ] += i / 26;
  575. slave->proc_repr_ns[ns + 1] += i % 26;
  576. }
  577. else
  578. {
  579. slave->proc_repr[5] += i;
  580. slave->proc_repr_ns[ns] += i;
  581. }
  582. slave->threads = tpp;
  583. devices_new[total_devices_new++] = slave;
  584. *nlp_p = slave;
  585. nlp_p = &slave->next_proc;
  586. }
  587. *nlp_p = NULL;
  588. cgpu->proc_id = 0;
  589. cgpu->threads -= (tpp * (lpcount - 1));
  590. }
  591. cgpu->last_device_valid_work = time(NULL);
  592. return true;
  593. }
  594. void add_cgpu_live(void *p)
  595. {
  596. add_cgpu(p);
  597. }
  598. int _serial_detect(struct device_drv *api, detectone_func_t detectone, autoscan_func_t autoscan, int flags)
  599. {
  600. struct string_elist *iter, *tmp;
  601. const char *dev, *colon;
  602. bool inhibitauto = flags & 4;
  603. char found = 0;
  604. bool forceauto = flags & 1;
  605. bool hasname;
  606. size_t namel = strlen(api->name);
  607. size_t dnamel = strlen(api->dname);
  608. #ifdef HAVE_FPGAUTILS
  609. clear_detectone_meta_info();
  610. #endif
  611. DL_FOREACH_SAFE(scan_devices, iter, tmp) {
  612. dev = iter->string;
  613. if ((colon = strchr(dev, ':')) && colon[1] != '\0') {
  614. size_t idlen = colon - dev;
  615. // allow either name:device or dname:device
  616. if ((idlen != namel || strncasecmp(dev, api->name, idlen))
  617. && (idlen != dnamel || strncasecmp(dev, api->dname, idlen)))
  618. continue;
  619. dev = colon + 1;
  620. hasname = true;
  621. }
  622. else
  623. hasname = false;
  624. if (!strcmp(dev, "auto"))
  625. forceauto = true;
  626. else if (!strcmp(dev, "noauto"))
  627. inhibitauto = true;
  628. else
  629. if ((flags & 2) && !hasname)
  630. continue;
  631. else
  632. if (!detectone)
  633. {} // do nothing
  634. #ifdef HAVE_FPGAUTILS
  635. else
  636. if (serial_claim(dev, NULL))
  637. {
  638. applog(LOG_DEBUG, "%s is already claimed... skipping probes", dev);
  639. string_elist_del(&scan_devices, iter);
  640. }
  641. #endif
  642. else if (detectone(dev)) {
  643. string_elist_del(&scan_devices, iter);
  644. inhibitauto = true;
  645. ++found;
  646. }
  647. }
  648. if ((forceauto || !inhibitauto) && autoscan)
  649. found += autoscan();
  650. return found;
  651. }
  652. static
  653. FILE *_open_bitstream(const char *path, const char *subdir, const char *sub2, const char *filename)
  654. {
  655. char fullpath[PATH_MAX];
  656. strcpy(fullpath, path);
  657. strcat(fullpath, "/");
  658. if (subdir) {
  659. strcat(fullpath, subdir);
  660. strcat(fullpath, "/");
  661. }
  662. if (sub2) {
  663. strcat(fullpath, sub2);
  664. strcat(fullpath, "/");
  665. }
  666. strcat(fullpath, filename);
  667. return fopen(fullpath, "rb");
  668. }
  669. #define _open_bitstream(path, subdir, sub2) do { \
  670. f = _open_bitstream(path, subdir, sub2, filename); \
  671. if (f) \
  672. return f; \
  673. } while(0)
  674. #define _open_bitstream2(path, path3) do { \
  675. _open_bitstream(path, NULL, path3); \
  676. _open_bitstream(path, "../share/" PACKAGE, path3); \
  677. _open_bitstream(path, "../" PACKAGE, path3); \
  678. } while(0)
  679. #define _open_bitstream3(path) do { \
  680. _open_bitstream2(path, dname); \
  681. _open_bitstream2(path, "bitstreams"); \
  682. _open_bitstream2(path, NULL); \
  683. } while(0)
  684. FILE *open_bitstream(const char *dname, const char *filename)
  685. {
  686. FILE *f;
  687. _open_bitstream3(opt_kernel_path);
  688. _open_bitstream3(cgminer_path);
  689. _open_bitstream3(".");
  690. return NULL;
  691. }