miner.h 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. #ifndef __MINER_H__
  2. #define __MINER_H__
  3. #include "config.h"
  4. #include <stdbool.h>
  5. #include <stdint.h>
  6. #include <sys/time.h>
  7. #include <pthread.h>
  8. #include <jansson.h>
  9. #include <curl/curl.h>
  10. #include "elist.h"
  11. #include "uthash.h"
  12. #include "logging.h"
  13. #include "util.h"
  14. #ifdef HAVE_OPENCL
  15. #ifdef __APPLE_CC__
  16. #include <OpenCL/opencl.h>
  17. #else
  18. #include <CL/cl.h>
  19. #endif
  20. #endif /* HAVE_OPENCL */
  21. #ifdef STDC_HEADERS
  22. # include <stdlib.h>
  23. # include <stddef.h>
  24. #else
  25. # ifdef HAVE_STDLIB_H
  26. # include <stdlib.h>
  27. # endif
  28. #endif
  29. #ifdef HAVE_ALLOCA_H
  30. # include <alloca.h>
  31. #elif defined __GNUC__
  32. # ifndef WIN32
  33. # define alloca __builtin_alloca
  34. # else
  35. # include <malloc.h>
  36. # endif
  37. #elif defined _AIX
  38. # define alloca __alloca
  39. #elif defined _MSC_VER
  40. # include <malloc.h>
  41. # define alloca _alloca
  42. #else
  43. # ifndef HAVE_ALLOCA
  44. # ifdef __cplusplus
  45. extern "C"
  46. # endif
  47. void *alloca (size_t);
  48. # endif
  49. #endif
  50. #ifdef __MINGW32__
  51. #include <windows.h>
  52. #include <io.h>
  53. static inline int fsync (int fd)
  54. {
  55. return (FlushFileBuffers ((HANDLE) _get_osfhandle (fd))) ? 0 : -1;
  56. }
  57. #ifndef MSG_DONTWAIT
  58. # define MSG_DONTWAIT 0x1000000
  59. #endif
  60. #endif /* __MINGW32__ */
  61. #if defined (__linux)
  62. #ifndef LINUX
  63. #define LINUX
  64. #endif
  65. #endif
  66. #ifdef WIN32
  67. #ifndef timersub
  68. #define timersub(a, b, result) \
  69. do { \
  70. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  71. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  72. if ((result)->tv_usec < 0) { \
  73. --(result)->tv_sec; \
  74. (result)->tv_usec += 1000000; \
  75. } \
  76. } while (0)
  77. #endif
  78. #ifndef timeradd
  79. # define timeradd(a, b, result) \
  80. do { \
  81. (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
  82. (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
  83. if ((result)->tv_usec >= 1000000) \
  84. { \
  85. ++(result)->tv_sec; \
  86. (result)->tv_usec -= 1000000; \
  87. } \
  88. } while (0)
  89. #endif
  90. #endif
  91. #ifdef HAVE_ADL
  92. #include "ADL_SDK/adl_sdk.h"
  93. #endif
  94. #ifdef HAVE_LIBUSB
  95. #include <libusb.h>
  96. #endif
  97. #ifdef USE_ZTEX
  98. #include "libztex.h"
  99. #endif
  100. #ifdef USE_MODMINER
  101. #include "usbutils.h"
  102. #endif
  103. #if !defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
  104. #define bswap_16 __builtin_bswap16
  105. #define bswap_32 __builtin_bswap32
  106. #define bswap_64 __builtin_bswap64
  107. #else
  108. #if HAVE_BYTESWAP_H
  109. #include <byteswap.h>
  110. #elif defined(USE_SYS_ENDIAN_H)
  111. #include <sys/endian.h>
  112. #elif defined(__APPLE__)
  113. #include <libkern/OSByteOrder.h>
  114. #define bswap_16 OSSwapInt16
  115. #define bswap_32 OSSwapInt32
  116. #define bswap_64 OSSwapInt64
  117. #else
  118. #define bswap_16(value) \
  119. ((((value) & 0xff) << 8) | ((value) >> 8))
  120. #define bswap_32(value) \
  121. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  122. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  123. #define bswap_64(value) \
  124. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  125. << 32) | \
  126. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  127. #endif
  128. #endif /* !defined(__GLXBYTEORDER_H__) */
  129. /* This assumes htobe32 is a macro in endian.h, and if it doesn't exist, then
  130. * htobe64 also won't exist */
  131. #ifndef htobe32
  132. # if __BYTE_ORDER == __LITTLE_ENDIAN
  133. # define htole16(x) (x)
  134. # define htole32(x) (x)
  135. # define be32toh(x) bswap_32(x)
  136. # define be64toh(x) bswap_64(x)
  137. # define htobe32(x) bswap_32(x)
  138. # define htobe64(x) bswap_64(x)
  139. # elif __BYTE_ORDER == __BIG_ENDIAN
  140. # define htole16(x) bswap_16(x)
  141. # define htole32(x) bswap_32(x)
  142. # define be32toh(x) (x)
  143. # define be64toh(x) (x)
  144. # define htobe32(x) (x)
  145. # define htobe64(x) (x)
  146. #else
  147. #error UNKNOWN BYTE ORDER
  148. #endif
  149. #endif
  150. #undef unlikely
  151. #undef likely
  152. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  153. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  154. #define likely(expr) (__builtin_expect(!!(expr), 1))
  155. #else
  156. #define unlikely(expr) (expr)
  157. #define likely(expr) (expr)
  158. #endif
  159. #define __maybe_unused __attribute__((unused))
  160. #define uninitialised_var(x) x = x
  161. #if defined(__i386__)
  162. #define WANT_CRYPTOPP_ASM32
  163. #endif
  164. #ifndef ARRAY_SIZE
  165. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  166. #endif
  167. #ifdef MIPSEB
  168. #ifndef roundl
  169. #define roundl(x) (long double)((long long)((x==0)?0.0:((x)+((x)>0)?0.5:-0.5)))
  170. #endif
  171. #endif
  172. enum alive {
  173. LIFE_WELL,
  174. LIFE_SICK,
  175. LIFE_DEAD,
  176. LIFE_NOSTART,
  177. LIFE_INIT,
  178. };
  179. enum pool_strategy {
  180. POOL_FAILOVER,
  181. POOL_ROUNDROBIN,
  182. POOL_ROTATE,
  183. POOL_LOADBALANCE,
  184. POOL_BALANCE,
  185. };
  186. #define TOP_STRATEGY (POOL_BALANCE)
  187. struct strategies {
  188. const char *s;
  189. };
  190. struct cgpu_info;
  191. #ifdef HAVE_ADL
  192. struct gpu_adl {
  193. ADLTemperature lpTemperature;
  194. int iAdapterIndex;
  195. int lpAdapterID;
  196. int iBusNumber;
  197. char strAdapterName[256];
  198. ADLPMActivity lpActivity;
  199. ADLODParameters lpOdParameters;
  200. ADLODPerformanceLevels *DefPerfLev;
  201. ADLFanSpeedInfo lpFanSpeedInfo;
  202. ADLFanSpeedValue lpFanSpeedValue;
  203. ADLFanSpeedValue DefFanSpeedValue;
  204. int iEngineClock;
  205. int iMemoryClock;
  206. int iVddc;
  207. int iPercentage;
  208. bool autofan;
  209. bool autoengine;
  210. bool managed; /* Were the values ever changed on this card */
  211. int lastengine;
  212. int lasttemp;
  213. int targetfan;
  214. int targettemp;
  215. int overtemp;
  216. int minspeed;
  217. int maxspeed;
  218. int gpu;
  219. bool has_fanspeed;
  220. struct gpu_adl *twin;
  221. };
  222. #endif
  223. struct api_data;
  224. struct thr_info;
  225. struct work;
  226. struct device_api {
  227. char *dname;
  228. char *name;
  229. // API-global functions
  230. void (*api_detect)();
  231. // Device-specific functions
  232. void (*reinit_device)(struct cgpu_info *);
  233. void (*get_statline_before)(char *, struct cgpu_info *);
  234. void (*get_statline)(char *, struct cgpu_info *);
  235. struct api_data *(*get_api_stats)(struct cgpu_info *);
  236. bool (*get_stats)(struct cgpu_info *);
  237. void (*identify_device)(struct cgpu_info *); // e.g. to flash a led
  238. // Thread-specific functions
  239. bool (*thread_prepare)(struct thr_info *);
  240. uint64_t (*can_limit_work)(struct thr_info *);
  241. bool (*thread_init)(struct thr_info *);
  242. bool (*prepare_work)(struct thr_info *, struct work *);
  243. int64_t (*scanhash)(struct thr_info *, struct work *, int64_t);
  244. void (*hw_error)(struct thr_info *);
  245. void (*thread_shutdown)(struct thr_info *);
  246. void (*thread_enable)(struct thr_info *);
  247. };
  248. enum dev_enable {
  249. DEV_ENABLED,
  250. DEV_DISABLED,
  251. DEV_RECOVER,
  252. };
  253. enum cl_kernels {
  254. KL_NONE,
  255. KL_POCLBM,
  256. KL_PHATK,
  257. KL_DIAKGCN,
  258. KL_DIABLO,
  259. KL_SCRYPT,
  260. };
  261. enum dev_reason {
  262. REASON_THREAD_FAIL_INIT,
  263. REASON_THREAD_ZERO_HASH,
  264. REASON_THREAD_FAIL_QUEUE,
  265. REASON_DEV_SICK_IDLE_60,
  266. REASON_DEV_DEAD_IDLE_600,
  267. REASON_DEV_NOSTART,
  268. REASON_DEV_OVER_HEAT,
  269. REASON_DEV_THERMAL_CUTOFF,
  270. REASON_DEV_COMMS_ERROR,
  271. REASON_DEV_THROTTLE,
  272. };
  273. #define REASON_NONE "None"
  274. #define REASON_THREAD_FAIL_INIT_STR "Thread failed to init"
  275. #define REASON_THREAD_ZERO_HASH_STR "Thread got zero hashes"
  276. #define REASON_THREAD_FAIL_QUEUE_STR "Thread failed to queue work"
  277. #define REASON_DEV_SICK_IDLE_60_STR "Device idle for 60s"
  278. #define REASON_DEV_DEAD_IDLE_600_STR "Device dead - idle for 600s"
  279. #define REASON_DEV_NOSTART_STR "Device failed to start"
  280. #define REASON_DEV_OVER_HEAT_STR "Device over heated"
  281. #define REASON_DEV_THERMAL_CUTOFF_STR "Device reached thermal cutoff"
  282. #define REASON_DEV_COMMS_ERROR_STR "Device comms error"
  283. #define REASON_DEV_THROTTLE_STR "Device throttle"
  284. #define REASON_UNKNOWN_STR "Unknown reason - code bug"
  285. #define MIN_SEC_UNSET 99999999
  286. struct cgminer_stats {
  287. uint32_t getwork_calls;
  288. struct timeval getwork_wait;
  289. struct timeval getwork_wait_max;
  290. struct timeval getwork_wait_min;
  291. };
  292. // Just the actual network getworks to the pool
  293. struct cgminer_pool_stats {
  294. uint32_t getwork_calls;
  295. uint32_t getwork_attempts;
  296. struct timeval getwork_wait;
  297. struct timeval getwork_wait_max;
  298. struct timeval getwork_wait_min;
  299. double getwork_wait_rolling;
  300. bool hadrolltime;
  301. bool canroll;
  302. bool hadexpire;
  303. uint32_t rolltime;
  304. double min_diff;
  305. double max_diff;
  306. double last_diff;
  307. uint32_t min_diff_count;
  308. uint32_t max_diff_count;
  309. uint64_t times_sent;
  310. uint64_t bytes_sent;
  311. uint64_t times_received;
  312. uint64_t bytes_received;
  313. };
  314. struct cgpu_info {
  315. int cgminer_id;
  316. struct device_api *api;
  317. int device_id;
  318. char *name;
  319. char *device_path;
  320. FILE *device_file;
  321. union {
  322. #ifdef USE_ZTEX
  323. struct libztex_device *device_ztex;
  324. #endif
  325. #ifdef USE_MODMINER
  326. struct cg_usb_device *usbdev;
  327. #endif
  328. int device_fd;
  329. };
  330. #ifdef USE_MODMINER
  331. int usbstat;
  332. char fpgaid;
  333. unsigned char clock;
  334. pthread_mutex_t *modminer_mutex;
  335. bool tried_two_byte_temp;
  336. bool one_byte_temp;
  337. #endif
  338. #ifdef USE_BITFORCE
  339. struct timeval work_start_tv;
  340. unsigned int wait_ms;
  341. unsigned int sleep_ms;
  342. double avg_wait_f;
  343. unsigned int avg_wait_d;
  344. uint32_t nonces;
  345. bool nonce_range;
  346. bool polling;
  347. bool flash_led;
  348. pthread_mutex_t device_mutex;
  349. #endif
  350. enum dev_enable deven;
  351. int accepted;
  352. int rejected;
  353. int hw_errors;
  354. double rolling;
  355. double total_mhashes;
  356. double utility;
  357. enum alive status;
  358. char init[40];
  359. struct timeval last_message_tv;
  360. int threads;
  361. struct thr_info **thr;
  362. int64_t max_hashes;
  363. const char *kname;
  364. #ifdef HAVE_OPENCL
  365. bool mapped;
  366. int virtual_gpu;
  367. int virtual_adl;
  368. int intensity;
  369. bool dynamic;
  370. cl_uint vwidth;
  371. size_t work_size;
  372. enum cl_kernels kernel;
  373. cl_ulong max_alloc;
  374. #ifdef USE_SCRYPT
  375. int opt_lg, lookup_gap;
  376. size_t opt_tc, thread_concurrency;
  377. size_t shaders;
  378. #endif
  379. struct timeval tv_gpustart;
  380. int intervals;
  381. #endif
  382. bool new_work;
  383. float temp;
  384. int cutofftemp;
  385. #ifdef HAVE_ADL
  386. bool has_adl;
  387. struct gpu_adl adl;
  388. int gpu_engine;
  389. int min_engine;
  390. int gpu_fan;
  391. int min_fan;
  392. int gpu_memclock;
  393. int gpu_memdiff;
  394. int gpu_powertune;
  395. float gpu_vddc;
  396. #endif
  397. int diff1;
  398. double diff_accepted;
  399. double diff_rejected;
  400. int last_share_pool;
  401. time_t last_share_pool_time;
  402. double last_share_diff;
  403. time_t device_last_well;
  404. time_t device_last_not_well;
  405. enum dev_reason device_not_well_reason;
  406. int thread_fail_init_count;
  407. int thread_zero_hash_count;
  408. int thread_fail_queue_count;
  409. int dev_sick_idle_60_count;
  410. int dev_dead_idle_600_count;
  411. int dev_nostart_count;
  412. int dev_over_heat_count; // It's a warning but worth knowing
  413. int dev_thermal_cutoff_count;
  414. int dev_comms_error_count;
  415. int dev_throttle_count;
  416. struct cgminer_stats cgminer_stats;
  417. };
  418. extern bool add_cgpu(struct cgpu_info*);
  419. struct thread_q {
  420. struct list_head q;
  421. bool frozen;
  422. pthread_mutex_t mutex;
  423. pthread_cond_t cond;
  424. };
  425. struct thr_info {
  426. int id;
  427. int device_thread;
  428. bool primary_thread;
  429. pthread_t pth;
  430. struct thread_q *q;
  431. struct cgpu_info *cgpu;
  432. void *cgpu_data;
  433. struct timeval last;
  434. struct timeval sick;
  435. bool pause;
  436. bool getwork;
  437. double rolling;
  438. bool work_restart;
  439. };
  440. extern int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  441. extern void thr_info_cancel(struct thr_info *thr);
  442. extern void thr_info_freeze(struct thr_info *thr);
  443. extern void nmsleep(unsigned int msecs);
  444. extern double us_tdiff(struct timeval *end, struct timeval *start);
  445. extern double tdiff(struct timeval *end, struct timeval *start);
  446. struct string_elist {
  447. char *string;
  448. bool free_me;
  449. struct list_head list;
  450. };
  451. static inline void string_elist_add(const char *s, struct list_head *head)
  452. {
  453. struct string_elist *n;
  454. n = calloc(1, sizeof(*n));
  455. n->string = strdup(s);
  456. n->free_me = true;
  457. list_add_tail(&n->list, head);
  458. }
  459. static inline void string_elist_del(struct string_elist *item)
  460. {
  461. if (item->free_me)
  462. free(item->string);
  463. list_del(&item->list);
  464. }
  465. static inline uint32_t swab32(uint32_t v)
  466. {
  467. return bswap_32(v);
  468. }
  469. static inline void swap256(void *dest_p, const void *src_p)
  470. {
  471. uint32_t *dest = dest_p;
  472. const uint32_t *src = src_p;
  473. dest[0] = src[7];
  474. dest[1] = src[6];
  475. dest[2] = src[5];
  476. dest[3] = src[4];
  477. dest[4] = src[3];
  478. dest[5] = src[2];
  479. dest[6] = src[1];
  480. dest[7] = src[0];
  481. }
  482. static inline void swab256(void *dest_p, const void *src_p)
  483. {
  484. uint32_t *dest = dest_p;
  485. const uint32_t *src = src_p;
  486. dest[0] = swab32(src[7]);
  487. dest[1] = swab32(src[6]);
  488. dest[2] = swab32(src[5]);
  489. dest[3] = swab32(src[4]);
  490. dest[4] = swab32(src[3]);
  491. dest[5] = swab32(src[2]);
  492. dest[6] = swab32(src[1]);
  493. dest[7] = swab32(src[0]);
  494. }
  495. static inline void flip32(void *dest_p, const void *src_p)
  496. {
  497. uint32_t *dest = dest_p;
  498. const uint32_t *src = src_p;
  499. int i;
  500. for (i = 0; i < 8; i++)
  501. dest[i] = swab32(src[i]);
  502. }
  503. static inline void flip80(void *dest_p, const void *src_p)
  504. {
  505. uint32_t *dest = dest_p;
  506. const uint32_t *src = src_p;
  507. int i;
  508. for (i = 0; i < 20; i++)
  509. dest[i] = swab32(src[i]);
  510. }
  511. extern void quit(int status, const char *format, ...);
  512. static inline void mutex_lock(pthread_mutex_t *lock)
  513. {
  514. if (unlikely(pthread_mutex_lock(lock)))
  515. quit(1, "WTF MUTEX ERROR ON LOCK!");
  516. }
  517. static inline void mutex_unlock(pthread_mutex_t *lock)
  518. {
  519. if (unlikely(pthread_mutex_unlock(lock)))
  520. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  521. }
  522. static inline int mutex_trylock(pthread_mutex_t *lock)
  523. {
  524. return pthread_mutex_trylock(lock);
  525. }
  526. static inline void wr_lock(pthread_rwlock_t *lock)
  527. {
  528. if (unlikely(pthread_rwlock_wrlock(lock)))
  529. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  530. }
  531. static inline void rd_lock(pthread_rwlock_t *lock)
  532. {
  533. if (unlikely(pthread_rwlock_rdlock(lock)))
  534. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  535. }
  536. static inline void rw_unlock(pthread_rwlock_t *lock)
  537. {
  538. if (unlikely(pthread_rwlock_unlock(lock)))
  539. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  540. }
  541. static inline void rd_unlock(pthread_rwlock_t *lock)
  542. {
  543. rw_unlock(lock);
  544. }
  545. static inline void wr_unlock(pthread_rwlock_t *lock)
  546. {
  547. rw_unlock(lock);
  548. }
  549. static inline void mutex_init(pthread_mutex_t *lock)
  550. {
  551. if (unlikely(pthread_mutex_init(lock, NULL)))
  552. quit(1, "Failed to pthread_mutex_init");
  553. }
  554. static inline void rwlock_init(pthread_rwlock_t *lock)
  555. {
  556. if (unlikely(pthread_rwlock_init(lock, NULL)))
  557. quit(1, "Failed to pthread_rwlock_init");
  558. }
  559. struct pool;
  560. extern bool opt_protocol;
  561. extern bool have_longpoll;
  562. extern char *opt_kernel_path;
  563. extern char *opt_socks_proxy;
  564. extern char *cgminer_path;
  565. extern bool opt_fail_only;
  566. extern bool opt_autofan;
  567. extern bool opt_autoengine;
  568. extern bool use_curses;
  569. extern char *opt_api_allow;
  570. extern char *opt_api_groups;
  571. extern char *opt_api_description;
  572. extern int opt_api_port;
  573. extern bool opt_api_listen;
  574. extern bool opt_api_network;
  575. extern bool opt_delaynet;
  576. extern bool opt_restart;
  577. extern char *opt_icarus_options;
  578. extern char *opt_icarus_timing;
  579. extern bool opt_worktime;
  580. #ifdef HAVE_LIBUSB
  581. extern int opt_usbdump;
  582. #endif
  583. #ifdef USE_BITFORCE
  584. extern bool opt_bfl_noncerange;
  585. #endif
  586. extern int swork_id;
  587. extern pthread_rwlock_t netacc_lock;
  588. extern const uint32_t sha256_init_state[];
  589. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  590. const char *rpc_req, bool, bool, int *,
  591. struct pool *pool, bool);
  592. extern const char *proxytype(curl_proxytype proxytype);
  593. extern char *get_proxy(char *url, struct pool *pool);
  594. extern char *bin2hex(const unsigned char *p, size_t len);
  595. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  596. typedef bool (*sha256_func)(struct thr_info*, const unsigned char *pmidstate,
  597. unsigned char *pdata,
  598. unsigned char *phash1, unsigned char *phash,
  599. const unsigned char *ptarget,
  600. uint32_t max_nonce,
  601. uint32_t *last_nonce,
  602. uint32_t nonce);
  603. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  604. extern int opt_queue;
  605. extern int opt_scantime;
  606. extern int opt_expiry;
  607. #ifdef HAVE_LIBUSB
  608. extern pthread_mutex_t cgusb_lock;
  609. #endif
  610. extern pthread_mutex_t hash_lock;
  611. extern pthread_mutex_t console_lock;
  612. extern pthread_mutex_t ch_lock;
  613. extern pthread_mutex_t restart_lock;
  614. extern pthread_cond_t restart_cond;
  615. extern void thread_reportin(struct thr_info *thr);
  616. extern int restart_wait(unsigned int mstime);
  617. extern void kill_work(void);
  618. extern void reinit_device(struct cgpu_info *cgpu);
  619. #ifdef HAVE_ADL
  620. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  621. extern int set_fanspeed(int gpu, int iFanSpeed);
  622. extern int set_vddc(int gpu, float fVddc);
  623. extern int set_engineclock(int gpu, int iEngineClock);
  624. extern int set_memoryclock(int gpu, int iMemoryClock);
  625. #endif
  626. extern void api(int thr_id);
  627. extern struct pool *current_pool(void);
  628. extern int enabled_pools;
  629. extern bool detect_stratum(struct pool *pool, char *url);
  630. extern struct pool *add_pool(void);
  631. extern void add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
  632. #define MAX_GPUDEVICES 16
  633. #define MIN_INTENSITY -10
  634. #define _MIN_INTENSITY_STR "-10"
  635. #ifdef USE_SCRYPT
  636. #define MAX_INTENSITY 20
  637. #define _MAX_INTENSITY_STR "20"
  638. #else
  639. #define MAX_INTENSITY 14
  640. #define _MAX_INTENSITY_STR "14"
  641. #endif
  642. extern struct list_head scan_devices;
  643. extern int nDevs;
  644. extern int opt_n_threads;
  645. extern int num_processors;
  646. extern int hw_errors;
  647. extern bool use_syslog;
  648. extern bool opt_quiet;
  649. extern struct thr_info *thr_info;
  650. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  651. extern int gpu_threads;
  652. #ifdef USE_SCRYPT
  653. extern bool opt_scrypt;
  654. #else
  655. #define opt_scrypt (0)
  656. #endif
  657. extern double total_secs;
  658. extern int mining_threads;
  659. extern struct cgpu_info *cpus;
  660. extern int total_devices;
  661. extern struct cgpu_info **devices;
  662. extern int total_pools;
  663. extern struct pool **pools;
  664. extern const char *algo_names[];
  665. extern enum sha256_algos opt_algo;
  666. extern struct strategies strategies[];
  667. extern enum pool_strategy pool_strategy;
  668. extern int opt_rotate_period;
  669. extern double total_mhashes_done;
  670. extern unsigned int new_blocks;
  671. extern unsigned int found_blocks;
  672. extern int total_accepted, total_rejected, total_diff1;;
  673. extern int total_getworks, total_stale, total_discarded;
  674. extern double total_diff_accepted, total_diff_rejected, total_diff_stale;
  675. extern unsigned int local_work;
  676. extern unsigned int total_go, total_ro;
  677. extern const int opt_cutofftemp;
  678. extern int opt_log_interval;
  679. extern unsigned long long global_hashrate;
  680. extern char *current_fullhash;
  681. extern uint64_t best_diff;
  682. extern struct timeval block_timeval;
  683. #ifdef HAVE_OPENCL
  684. typedef struct {
  685. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  686. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  687. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  688. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  689. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  690. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  691. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  692. cl_uint W16; cl_uint W17; cl_uint W2;
  693. cl_uint PreVal4; cl_uint T1;
  694. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  695. cl_uint PreVal4addT1; cl_uint T1substate0;
  696. cl_uint PreVal4_2;
  697. cl_uint PreVal0;
  698. cl_uint PreW18;
  699. cl_uint PreW19;
  700. cl_uint PreW31;
  701. cl_uint PreW32;
  702. /* For diakgcn */
  703. cl_uint B1addK6, PreVal0addK7, W16addK16, W17addK17;
  704. cl_uint zeroA, zeroB;
  705. cl_uint oneA, twoA, threeA, fourA, fiveA, sixA, sevenA;
  706. #ifdef USE_SCRYPT
  707. struct work *work;
  708. #endif
  709. } dev_blk_ctx;
  710. #else
  711. typedef struct {
  712. uint32_t nonce;
  713. } dev_blk_ctx;
  714. #endif
  715. struct curl_ent {
  716. CURL *curl;
  717. struct list_head node;
  718. struct timeval tv;
  719. };
  720. /* Disabled needs to be the lowest enum as a freshly calloced value will then
  721. * equal disabled */
  722. enum pool_enable {
  723. POOL_DISABLED,
  724. POOL_ENABLED,
  725. POOL_REJECTING,
  726. };
  727. struct stratum_work {
  728. char *job_id;
  729. char *prev_hash;
  730. char *coinbase1;
  731. char *coinbase2;
  732. char **merkle;
  733. char *bbversion;
  734. char *nbit;
  735. char *ntime;
  736. bool clean;
  737. int merkles;
  738. double diff;
  739. };
  740. #define RECVSIZE 8192
  741. #define RBUFSIZE (RECVSIZE + 4)
  742. struct pool {
  743. int pool_no;
  744. int prio;
  745. int accepted, rejected;
  746. int seq_rejects;
  747. int seq_getfails;
  748. int solved;
  749. int diff1;
  750. double diff_accepted;
  751. double diff_rejected;
  752. double diff_stale;
  753. bool submit_fail;
  754. bool idle;
  755. bool lagging;
  756. bool probed;
  757. enum pool_enable enabled;
  758. bool submit_old;
  759. bool removed;
  760. bool lp_started;
  761. char *hdr_path;
  762. char *lp_url;
  763. unsigned int getwork_requested;
  764. unsigned int stale_shares;
  765. unsigned int discarded_work;
  766. unsigned int getfail_occasions;
  767. unsigned int remotefail_occasions;
  768. struct timeval tv_idle;
  769. double utility;
  770. int last_shares, shares;
  771. char *rpc_req;
  772. char *rpc_url;
  773. char *rpc_userpass;
  774. char *rpc_user, *rpc_pass;
  775. curl_proxytype rpc_proxytype;
  776. char *rpc_proxy;
  777. pthread_mutex_t pool_lock;
  778. struct thread_q *submit_q;
  779. struct thread_q *getwork_q;
  780. pthread_t longpoll_thread;
  781. pthread_t submit_thread;
  782. pthread_t getwork_thread;
  783. int curls;
  784. pthread_cond_t cr_cond;
  785. struct list_head curlring;
  786. time_t last_share_time;
  787. double last_share_diff;
  788. struct cgminer_stats cgminer_stats;
  789. struct cgminer_pool_stats cgminer_pool_stats;
  790. /* Stratum variables */
  791. char *stratum_url;
  792. char *stratum_port;
  793. CURL *stratum_curl;
  794. SOCKETTYPE sock;
  795. char sockbuf[RBUFSIZE];
  796. char *sockaddr_url; /* stripped url used for sockaddr */
  797. char *nonce1;
  798. uint32_t nonce2;
  799. int n2size;
  800. bool has_stratum;
  801. bool stratum_active;
  802. bool stratum_auth;
  803. struct stratum_work swork;
  804. pthread_t stratum_thread;
  805. pthread_mutex_t stratum_lock;
  806. /* GBT variables */
  807. bool has_gbt;
  808. pthread_mutex_t gbt_lock;
  809. unsigned char previousblockhash[32];
  810. unsigned char gbt_target[32];
  811. char *coinbasetxn;
  812. char *longpollid;
  813. char *gbt_workid;
  814. int gbt_expires;
  815. uint32_t gbt_version;
  816. uint32_t curtime;
  817. uint32_t gbt_bits;
  818. unsigned char *gbt_coinbase;
  819. unsigned char *txn_hashes;
  820. int gbt_txns;
  821. int coinbase_len;
  822. struct timeval tv_lastwork;
  823. };
  824. #define GETWORK_MODE_TESTPOOL 'T'
  825. #define GETWORK_MODE_POOL 'P'
  826. #define GETWORK_MODE_LP 'L'
  827. #define GETWORK_MODE_BENCHMARK 'B'
  828. #define GETWORK_MODE_STRATUM 'S'
  829. #define GETWORK_MODE_GBT 'G'
  830. struct work {
  831. unsigned char data[128];
  832. unsigned char midstate[32];
  833. unsigned char target[32];
  834. unsigned char hash[32];
  835. uint64_t outputhash;
  836. int rolls;
  837. dev_blk_ctx blk;
  838. struct thr_info *thr;
  839. int thr_id;
  840. struct pool *pool;
  841. struct timeval tv_staged;
  842. bool mined;
  843. bool clone;
  844. bool cloned;
  845. int rolltime;
  846. bool longpoll;
  847. bool stale;
  848. bool mandatory;
  849. bool block;
  850. bool queued;
  851. bool stratum;
  852. char *job_id;
  853. char *nonce2;
  854. char *ntime;
  855. double sdiff;
  856. bool gbt;
  857. char *gbt_coinbase;
  858. int gbt_txns;
  859. unsigned int work_block;
  860. int id;
  861. UT_hash_handle hh;
  862. double work_difficulty;
  863. struct timeval tv_getwork;
  864. struct timeval tv_getwork_reply;
  865. struct timeval tv_cloned;
  866. struct timeval tv_work_start;
  867. struct timeval tv_work_found;
  868. char getwork_mode;
  869. };
  870. #ifdef USE_MODMINER
  871. struct modminer_fpga_state {
  872. bool work_running;
  873. struct work running_work;
  874. struct timeval tv_workstart;
  875. uint32_t hashes;
  876. char next_work_cmd[46];
  877. char fpgaid;
  878. bool overheated;
  879. bool new_work;
  880. uint32_t shares;
  881. uint32_t shares_last_hw;
  882. uint32_t hw_errors;
  883. uint32_t shares_to_good;
  884. struct timeval last_changed;
  885. uint32_t no_nonce_counter;
  886. };
  887. #endif
  888. extern void get_datestamp(char *, struct timeval *);
  889. extern void submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  890. extern void tailsprintf(char *f, const char *fmt, ...);
  891. extern void wlogprint(const char *f, ...);
  892. extern int curses_int(const char *query);
  893. extern char *curses_input(const char *query);
  894. extern void kill_work(void);
  895. extern void switch_pools(struct pool *selected);
  896. extern void remove_pool(struct pool *pool);
  897. extern void write_config(FILE *fcfg);
  898. extern void default_save_file(char *filename);
  899. extern bool log_curses_only(int prio, const char *f, va_list ap);
  900. extern void clear_logwin(void);
  901. extern bool pool_tclear(struct pool *pool, bool *var);
  902. extern struct thread_q *tq_new(void);
  903. extern void tq_free(struct thread_q *tq);
  904. extern bool tq_push(struct thread_q *tq, void *data);
  905. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  906. extern void tq_freeze(struct thread_q *tq);
  907. extern void tq_thaw(struct thread_q *tq);
  908. extern bool successful_connect;
  909. extern void adl(void);
  910. extern void app_restart(void);
  911. extern void clean_work(struct work *work);
  912. extern void free_work(struct work *work);
  913. extern void __copy_work(struct work *work, struct work *base_work);
  914. extern struct work *copy_work(struct work *base_work);
  915. enum api_data_type {
  916. API_ESCAPE,
  917. API_STRING,
  918. API_CONST,
  919. API_INT,
  920. API_UINT,
  921. API_UINT32,
  922. API_UINT64,
  923. API_DOUBLE,
  924. API_ELAPSED,
  925. API_BOOL,
  926. API_TIMEVAL,
  927. API_TIME,
  928. API_MHS,
  929. API_MHTOTAL,
  930. API_TEMP,
  931. API_UTILITY,
  932. API_FREQ,
  933. API_VOLTS,
  934. API_HS,
  935. API_DIFF
  936. };
  937. struct api_data {
  938. enum api_data_type type;
  939. char *name;
  940. void *data;
  941. bool data_was_malloc;
  942. struct api_data *prev;
  943. struct api_data *next;
  944. };
  945. extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
  946. extern struct api_data *api_add_string(struct api_data *root, char *name, char *data, bool copy_data);
  947. extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
  948. extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
  949. extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
  950. extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  951. extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
  952. extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
  953. extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
  954. extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
  955. extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
  956. extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
  957. extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
  958. extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
  959. extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
  960. extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
  961. extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
  962. extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
  963. extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
  964. extern struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data);
  965. #endif /* __MINER_H__ */