miner.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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. #endif
  342. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  343. bool flash_led;
  344. #endif
  345. void *cgpu_data;
  346. pthread_mutex_t device_mutex;
  347. enum dev_enable deven;
  348. int accepted;
  349. double accepted_weighed;
  350. int rejected;
  351. int hw_errors;
  352. double rolling;
  353. double total_mhashes;
  354. double utility;
  355. double utility_diff1;
  356. enum alive status;
  357. char init[40];
  358. struct timeval last_message_tv;
  359. int threads;
  360. struct thr_info **thr;
  361. int64_t max_hashes;
  362. const char *kname;
  363. #ifdef HAVE_OPENCL
  364. bool mapped;
  365. int virtual_gpu;
  366. int virtual_adl;
  367. int intensity;
  368. bool dynamic;
  369. cl_uint vwidth;
  370. size_t work_size;
  371. enum cl_kernels kernel;
  372. cl_ulong max_alloc;
  373. #ifdef USE_SCRYPT
  374. int opt_lg, lookup_gap;
  375. size_t opt_tc, thread_concurrency;
  376. size_t shaders;
  377. #endif
  378. struct timeval tv_gpustart;;
  379. struct timeval tv_gpuend;
  380. double gpu_us_average;
  381. #endif
  382. bool new_work;
  383. float temp;
  384. int cutofftemp;
  385. int targettemp;
  386. #ifdef HAVE_ADL
  387. bool has_adl;
  388. struct gpu_adl adl;
  389. int gpu_engine;
  390. int min_engine;
  391. int gpu_fan;
  392. int min_fan;
  393. int gpu_memclock;
  394. int gpu_memdiff;
  395. int gpu_powertune;
  396. float gpu_vddc;
  397. #endif
  398. int diff1;
  399. double diff_accepted;
  400. double diff_rejected;
  401. int last_share_pool;
  402. time_t last_share_pool_time;
  403. double last_share_diff;
  404. time_t device_last_well;
  405. time_t device_last_not_well;
  406. enum dev_reason device_not_well_reason;
  407. float reinit_backoff;
  408. int thread_fail_init_count;
  409. int thread_zero_hash_count;
  410. int thread_fail_queue_count;
  411. int dev_sick_idle_60_count;
  412. int dev_dead_idle_600_count;
  413. int dev_nostart_count;
  414. int dev_over_heat_count; // It's a warning but worth knowing
  415. int dev_thermal_cutoff_count;
  416. int dev_comms_error_count;
  417. int dev_throttle_count;
  418. struct cgminer_stats cgminer_stats;
  419. };
  420. extern void renumber_cgpu(struct cgpu_info *);
  421. extern bool add_cgpu(struct cgpu_info*);
  422. struct thread_q {
  423. struct list_head q;
  424. bool frozen;
  425. pthread_mutex_t mutex;
  426. pthread_cond_t cond;
  427. };
  428. struct thr_info {
  429. int id;
  430. int device_thread;
  431. bool primary_thread;
  432. pthread_t pth;
  433. struct thread_q *q;
  434. struct cgpu_info *cgpu;
  435. void *cgpu_data;
  436. struct timeval last;
  437. struct timeval sick;
  438. bool pause;
  439. time_t getwork;
  440. double rolling;
  441. bool work_restart;
  442. int work_restart_fd;
  443. int _work_restart_fd_w;
  444. };
  445. extern int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  446. extern void thr_info_cancel(struct thr_info *thr);
  447. extern void thr_info_freeze(struct thr_info *thr);
  448. extern void nmsleep(unsigned int msecs);
  449. extern void rename_thr(const char* name);
  450. extern double tdiff(struct timeval *end, struct timeval *start);
  451. struct string_elist {
  452. char *string;
  453. bool free_me;
  454. struct list_head list;
  455. };
  456. static inline void string_elist_add(const char *s, struct list_head *head)
  457. {
  458. struct string_elist *n;
  459. n = calloc(1, sizeof(*n));
  460. n->string = strdup(s);
  461. n->free_me = true;
  462. list_add_tail(&n->list, head);
  463. }
  464. static inline void string_elist_del(struct string_elist *item)
  465. {
  466. if (item->free_me)
  467. free(item->string);
  468. list_del(&item->list);
  469. free(item);
  470. }
  471. static inline uint32_t swab32(uint32_t v)
  472. {
  473. return bswap_32(v);
  474. }
  475. static inline void swap256(void *dest_p, const void *src_p)
  476. {
  477. uint32_t *dest = dest_p;
  478. const uint32_t *src = src_p;
  479. dest[0] = src[7];
  480. dest[1] = src[6];
  481. dest[2] = src[5];
  482. dest[3] = src[4];
  483. dest[4] = src[3];
  484. dest[5] = src[2];
  485. dest[6] = src[1];
  486. dest[7] = src[0];
  487. }
  488. static inline void swap32yes(void*out, const void*in, size_t sz) {
  489. size_t swapcounter = 0;
  490. for (swapcounter = 0; swapcounter < sz; ++swapcounter)
  491. (((uint32_t*)out)[swapcounter]) = swab32(((uint32_t*)in)[swapcounter]);
  492. }
  493. #ifdef __BIG_ENDIAN__
  494. # define swap32tobe(out, in, sz) (void)0
  495. # define swap32tole(out, in, sz) swap32yes(out, in, sz)
  496. #else
  497. # define swap32tobe(out, in, sz) swap32yes(out, in, sz)
  498. # define swap32tole(out, in, sz) (void)0
  499. #endif
  500. extern void quit(int status, const char *format, ...);
  501. static inline void mutex_lock(pthread_mutex_t *lock)
  502. {
  503. if (unlikely(pthread_mutex_lock(lock)))
  504. quit(1, "WTF MUTEX ERROR ON LOCK!");
  505. }
  506. static inline void mutex_unlock(pthread_mutex_t *lock)
  507. {
  508. if (unlikely(pthread_mutex_unlock(lock)))
  509. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  510. }
  511. static inline int mutex_trylock(pthread_mutex_t *lock)
  512. {
  513. return pthread_mutex_trylock(lock);
  514. }
  515. static inline void wr_lock(pthread_rwlock_t *lock)
  516. {
  517. if (unlikely(pthread_rwlock_wrlock(lock)))
  518. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  519. }
  520. static inline void rd_lock(pthread_rwlock_t *lock)
  521. {
  522. if (unlikely(pthread_rwlock_rdlock(lock)))
  523. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  524. }
  525. static inline void rw_unlock(pthread_rwlock_t *lock)
  526. {
  527. if (unlikely(pthread_rwlock_unlock(lock)))
  528. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  529. }
  530. static inline void rd_unlock(pthread_rwlock_t *lock)
  531. {
  532. rw_unlock(lock);
  533. }
  534. static inline void wr_unlock(pthread_rwlock_t *lock)
  535. {
  536. rw_unlock(lock);
  537. }
  538. static inline void mutex_init(pthread_mutex_t *lock)
  539. {
  540. if (unlikely(pthread_mutex_init(lock, NULL)))
  541. quit(1, "Failed to pthread_mutex_init");
  542. }
  543. static inline void rwlock_init(pthread_rwlock_t *lock)
  544. {
  545. if (unlikely(pthread_rwlock_init(lock, NULL)))
  546. quit(1, "Failed to pthread_rwlock_init");
  547. }
  548. struct pool;
  549. extern bool opt_protocol;
  550. extern char *opt_coinbase_sig;
  551. extern bool have_longpoll;
  552. extern char *opt_kernel_path;
  553. extern char *opt_socks_proxy;
  554. extern char *cgminer_path;
  555. extern bool opt_fail_only;
  556. extern bool opt_autofan;
  557. extern bool opt_autoengine;
  558. extern bool use_curses;
  559. extern char *opt_api_allow;
  560. extern char *opt_api_groups;
  561. extern char *opt_api_description;
  562. extern int opt_api_port;
  563. extern bool opt_api_listen;
  564. extern bool opt_api_network;
  565. extern bool opt_delaynet;
  566. extern bool opt_restart;
  567. extern char *opt_icarus_options;
  568. extern char *opt_icarus_timing;
  569. extern bool opt_worktime;
  570. #ifdef USE_BITFORCE
  571. extern bool opt_bfl_noncerange;
  572. #endif
  573. extern pthread_rwlock_t netacc_lock;
  574. extern const uint32_t sha256_init_state[];
  575. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  576. const char *rpc_req, bool, bool, int *,
  577. struct pool *pool, bool);
  578. extern bool our_curl_supports_proxy_uris();
  579. extern char *bin2hex(const unsigned char *p, size_t len);
  580. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  581. typedef bool (*sha256_func)(struct thr_info*, const unsigned char *pmidstate,
  582. unsigned char *pdata,
  583. unsigned char *phash1, unsigned char *phash,
  584. const unsigned char *ptarget,
  585. uint32_t max_nonce,
  586. uint32_t *last_nonce,
  587. uint32_t nonce);
  588. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  589. extern int opt_queue;
  590. extern int opt_scantime;
  591. extern int opt_expiry;
  592. extern pthread_mutex_t console_lock;
  593. extern pthread_mutex_t ch_lock;
  594. extern pthread_mutex_t restart_lock;
  595. extern pthread_cond_t restart_cond;
  596. extern void thread_reportin(struct thr_info *thr);
  597. extern int restart_wait(unsigned int mstime);
  598. extern int stale_wait(unsigned int mstime, struct work*, bool checkend);
  599. extern void kill_work(void);
  600. extern void app_restart(void);
  601. extern void reinit_device(struct cgpu_info *cgpu);
  602. #ifdef HAVE_ADL
  603. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  604. extern int set_fanspeed(int gpu, int iFanSpeed);
  605. extern int set_vddc(int gpu, float fVddc);
  606. extern int set_engineclock(int gpu, int iEngineClock);
  607. extern int set_memoryclock(int gpu, int iMemoryClock);
  608. #endif
  609. extern void api(int thr_id);
  610. extern struct pool *current_pool(void);
  611. extern int enabled_pools;
  612. extern void add_pool_details5(bool live, char *url, char *user, char *pass, char *proxy);
  613. extern void add_pool_details(bool live, char *url, char *user, char *pass);
  614. #define MAX_GPUDEVICES 16
  615. #define MIN_INTENSITY -10
  616. #define _MIN_INTENSITY_STR "-10"
  617. #ifdef USE_SCRYPT
  618. #define MAX_INTENSITY 20
  619. #define _MAX_INTENSITY_STR "20"
  620. #else
  621. #define MAX_INTENSITY 14
  622. #define _MAX_INTENSITY_STR "14"
  623. #endif
  624. extern struct list_head scan_devices;
  625. extern int nDevs;
  626. extern int opt_n_threads;
  627. extern int num_processors;
  628. extern int hw_errors;
  629. extern bool use_syslog;
  630. extern bool opt_quiet;
  631. extern struct thr_info *thr_info;
  632. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  633. extern int gpu_threads;
  634. #ifdef USE_SCRYPT
  635. extern bool opt_scrypt;
  636. #else
  637. #define opt_scrypt (0)
  638. #endif
  639. extern double total_secs;
  640. extern int mining_threads;
  641. extern struct cgpu_info *cpus;
  642. extern int total_devices;
  643. extern struct cgpu_info **devices;
  644. extern int total_pools;
  645. extern struct pool **pools;
  646. extern const char *algo_names[];
  647. extern enum sha256_algos opt_algo;
  648. extern struct strategies strategies[];
  649. extern enum pool_strategy pool_strategy;
  650. extern int opt_rotate_period;
  651. extern double total_mhashes_done;
  652. extern unsigned int new_blocks;
  653. extern unsigned int found_blocks;
  654. extern int total_accepted, total_rejected, total_diff1;;
  655. extern int total_getworks, total_stale, total_discarded;
  656. extern double total_diff_accepted, total_diff_rejected, total_diff_stale;
  657. extern unsigned int local_work;
  658. extern unsigned int total_go, total_ro;
  659. extern const int opt_cutofftemp;
  660. extern int opt_hysteresis;
  661. extern int opt_fail_pause;
  662. extern int opt_log_interval;
  663. extern unsigned long long global_hashrate;
  664. extern char *current_fullhash;
  665. extern struct timeval block_timeval;
  666. #ifdef HAVE_OPENCL
  667. typedef struct {
  668. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  669. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  670. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  671. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  672. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  673. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  674. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  675. cl_uint W16; cl_uint W17; cl_uint W2;
  676. cl_uint PreVal4; cl_uint T1;
  677. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  678. cl_uint PreVal4addT1; cl_uint T1substate0;
  679. cl_uint PreVal4_2;
  680. cl_uint PreVal0;
  681. cl_uint PreW18;
  682. cl_uint PreW19;
  683. cl_uint PreW31;
  684. cl_uint PreW32;
  685. /* For diakgcn */
  686. cl_uint B1addK6, PreVal0addK7, W16addK16, W17addK17;
  687. cl_uint zeroA, zeroB;
  688. cl_uint oneA, twoA, threeA, fourA, fiveA, sixA, sevenA;
  689. #ifdef USE_SCRYPT
  690. struct work *work;
  691. #endif
  692. } dev_blk_ctx;
  693. #else
  694. typedef struct {
  695. uint32_t nonce;
  696. } dev_blk_ctx;
  697. #endif
  698. struct curl_ent {
  699. CURL *curl;
  700. struct list_head node;
  701. struct timeval tv;
  702. };
  703. /* Disabled needs to be the lowest enum as a freshly calloced value will then
  704. * equal disabled */
  705. enum pool_enable {
  706. POOL_DISABLED,
  707. POOL_ENABLED,
  708. POOL_REJECTING,
  709. };
  710. enum pool_protocol {
  711. PLP_NONE,
  712. PLP_GETWORK,
  713. PLP_GETBLOCKTEMPLATE,
  714. };
  715. struct pool {
  716. int pool_no;
  717. int prio;
  718. int accepted, rejected;
  719. int seq_rejects;
  720. int seq_getfails;
  721. int solved;
  722. int diff1;
  723. double diff_accepted;
  724. double diff_rejected;
  725. double diff_stale;
  726. int queued;
  727. int staged;
  728. bool submit_fail;
  729. bool idle;
  730. bool lagging;
  731. bool probed;
  732. enum pool_enable enabled;
  733. bool submit_old;
  734. bool removed;
  735. bool lp_started;
  736. unsigned char work_restart_id;
  737. uint32_t block_id;
  738. enum pool_protocol proto;
  739. char *hdr_path;
  740. char *lp_url;
  741. char *lp_id;
  742. enum pool_protocol lp_proto;
  743. curl_socket_t lp_socket;
  744. unsigned int getwork_requested;
  745. unsigned int stale_shares;
  746. unsigned int discarded_work;
  747. unsigned int getfail_occasions;
  748. unsigned int remotefail_occasions;
  749. struct timeval tv_idle;
  750. double utility;
  751. int last_shares, shares;
  752. char *rpc_url;
  753. char *rpc_userpass;
  754. char *rpc_user, *rpc_pass;
  755. char *rpc_proxy;
  756. pthread_mutex_t pool_lock;
  757. struct thread_q *submit_q;
  758. struct thread_q *getwork_q;
  759. pthread_t longpoll_thread;
  760. pthread_t submit_thread;
  761. pthread_t getwork_thread;
  762. int curls;
  763. pthread_cond_t cr_cond;
  764. struct list_head curlring;
  765. time_t last_share_time;
  766. double last_share_diff;
  767. struct cgminer_stats cgminer_stats;
  768. struct cgminer_pool_stats cgminer_pool_stats;
  769. };
  770. #define GETWORK_MODE_TESTPOOL 'T'
  771. #define GETWORK_MODE_POOL 'P'
  772. #define GETWORK_MODE_LP 'L'
  773. #define GETWORK_MODE_BENCHMARK 'B'
  774. struct work {
  775. unsigned char data[128];
  776. unsigned char hash1[64];
  777. unsigned char midstate[32];
  778. unsigned char target[32];
  779. unsigned char hash[32];
  780. int rolls;
  781. uint32_t output[1];
  782. uint32_t valid;
  783. dev_blk_ctx blk;
  784. struct thr_info *thr;
  785. int thr_id;
  786. struct pool *pool;
  787. struct timeval tv_staged;
  788. bool mined;
  789. bool clone;
  790. bool cloned;
  791. int rolltime;
  792. bool longpoll;
  793. bool stale;
  794. bool mandatory;
  795. bool block;
  796. bool queued;
  797. unsigned char work_restart_id;
  798. int id;
  799. UT_hash_handle hh;
  800. double work_difficulty;
  801. blktemplate_t *tmpl;
  802. int *tmpl_refcount;
  803. unsigned int dataid;
  804. struct timeval tv_getwork;
  805. struct timeval tv_getwork_reply;
  806. struct timeval tv_cloned;
  807. struct timeval tv_work_start;
  808. struct timeval tv_work_found;
  809. char getwork_mode;
  810. };
  811. extern void get_datestamp(char *, struct timeval *);
  812. enum test_nonce2_result {
  813. TNR_GOOD,
  814. TNR_HIGH,
  815. TNR_BAD,
  816. };
  817. extern enum test_nonce2_result _test_nonce2(struct work *, uint32_t nonce, bool checktarget);
  818. #define test_nonce(work, nonce, checktarget) (_test_nonce2(work, nonce, checktarget) == TNR_GOOD)
  819. #define test_nonce2(work, nonce) (_test_nonce2(work, nonce, true))
  820. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  821. extern void tailsprintf(char *f, const char *fmt, ...);
  822. extern void wlogprint(const char *f, ...);
  823. extern int curses_int(const char *query);
  824. extern char *curses_input(const char *query);
  825. extern void kill_work(void);
  826. extern void switch_pools(struct pool *selected);
  827. extern void remove_pool(struct pool *pool);
  828. extern void write_config(FILE *fcfg);
  829. extern void default_save_file(char *filename);
  830. extern bool log_curses_only(int prio, const char *f, va_list ap);
  831. extern void clear_logwin(void);
  832. extern bool pool_tclear(struct pool *pool, bool *var);
  833. extern struct thread_q *tq_new(void);
  834. extern void tq_free(struct thread_q *tq);
  835. extern bool tq_push(struct thread_q *tq, void *data);
  836. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  837. extern void tq_freeze(struct thread_q *tq);
  838. extern void tq_thaw(struct thread_q *tq);
  839. extern bool successful_connect;
  840. extern void adl(void);
  841. enum api_data_type {
  842. API_ESCAPE,
  843. API_STRING,
  844. API_CONST,
  845. API_INT,
  846. API_UINT,
  847. API_UINT32,
  848. API_UINT64,
  849. API_DOUBLE,
  850. API_ELAPSED,
  851. API_BOOL,
  852. API_TIMEVAL,
  853. API_TIME,
  854. API_MHS,
  855. API_MHTOTAL,
  856. API_TEMP,
  857. API_UTILITY,
  858. API_FREQ,
  859. API_VOLTS,
  860. API_HS,
  861. API_DIFF,
  862. API_JSON,
  863. };
  864. struct api_data {
  865. enum api_data_type type;
  866. char *name;
  867. void *data;
  868. bool data_was_malloc;
  869. struct api_data *prev;
  870. struct api_data *next;
  871. };
  872. extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
  873. extern struct api_data *api_add_string(struct api_data *root, char *name, const char *data, bool copy_data);
  874. extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
  875. extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
  876. extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
  877. extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  878. extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
  879. extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
  880. extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
  881. extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
  882. extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
  883. extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
  884. extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
  885. extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
  886. extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
  887. extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
  888. extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
  889. extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
  890. extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
  891. extern struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data);
  892. extern struct api_data *api_add_json(struct api_data *root, char *name, json_t *data, bool copy_data);
  893. #endif /* __MINER_H__ */