miner.h 21 KB

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