miner.h 25 KB

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