miner.h 26 KB

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