deviceapi.c 20 KB

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