miner.h 25 KB

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