miner.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. #ifdef HAVE_OPENCL
  13. #ifdef __APPLE_CC__
  14. #include <OpenCL/opencl.h>
  15. #else
  16. #include <CL/cl.h>
  17. #endif
  18. #endif /* HAVE_OPENCL */
  19. #ifdef STDC_HEADERS
  20. # include <stdlib.h>
  21. # include <stddef.h>
  22. #else
  23. # ifdef HAVE_STDLIB_H
  24. # include <stdlib.h>
  25. # endif
  26. #endif
  27. #ifdef HAVE_ALLOCA_H
  28. # include <alloca.h>
  29. #elif defined __GNUC__
  30. # ifndef WIN32
  31. # define alloca __builtin_alloca
  32. # else
  33. # include <malloc.h>
  34. # endif
  35. #elif defined _AIX
  36. # define alloca __alloca
  37. #elif defined _MSC_VER
  38. # include <malloc.h>
  39. # define alloca _alloca
  40. #else
  41. # ifndef HAVE_ALLOCA
  42. # ifdef __cplusplus
  43. extern "C"
  44. # endif
  45. void *alloca (size_t);
  46. # endif
  47. #endif
  48. #if defined (__linux)
  49. #ifndef LINUX
  50. #define LINUX
  51. #endif
  52. #endif
  53. #ifdef HAVE_ADL
  54. #include "ADL_SDK/adl_sdk.h"
  55. #endif
  56. #ifdef __SSE2__
  57. #define WANT_SSE2_4WAY 1
  58. #endif
  59. #if defined(__i386__) && defined(HAS_YASM) && defined(__SSE2__)
  60. #define WANT_X8632_SSE2 1
  61. #endif
  62. #if defined(__i386__) || defined(__x86_64__)
  63. #define WANT_VIA_PADLOCK 1
  64. #endif
  65. #if defined(__x86_64__) && defined(HAS_YASM)
  66. #define WANT_X8664_SSE2 1
  67. #endif
  68. #if defined(__x86_64__) && defined(HAS_YASM)
  69. #define WANT_X8664_SSE4 1
  70. #endif
  71. #if !defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
  72. #define bswap_16 __builtin_bswap16
  73. #define bswap_32 __builtin_bswap32
  74. #define bswap_64 __builtin_bswap64
  75. #else
  76. #if HAVE_BYTESWAP_H
  77. #include <byteswap.h>
  78. #elif defined(USE_SYS_ENDIAN_H)
  79. #include <sys/endian.h>
  80. #elif defined(__APPLE__)
  81. #include <libkern/OSByteOrder.h>
  82. #define bswap_16 OSSwapInt16
  83. #define bswap_32 OSSwapInt32
  84. #define bswap_64 OSSwapInt64
  85. #else
  86. #define bswap_16(value) \
  87. ((((value) & 0xff) << 8) | ((value) >> 8))
  88. #define bswap_32(value) \
  89. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  90. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  91. #define bswap_64(value) \
  92. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  93. << 32) | \
  94. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  95. #endif
  96. #endif /* !defined(__GLXBYTEORDER_H__) */
  97. /* This assumes htobe32 is a macro in endian.h */
  98. #ifndef htobe32
  99. # if __BYTE_ORDER == __LITTLE_ENDIAN
  100. # define be32toh(x) bswap_32(x)
  101. # define htobe32(x) bswap_32(x)
  102. # elif __BYTE_ORDER == __BIG_ENDIAN
  103. # define be32toh(x) (x)
  104. # define htobe32(x) (x)
  105. #else
  106. #error UNKNOWN BYTE ORDER
  107. #endif
  108. #endif
  109. #ifdef HAVE_SYSLOG_H
  110. #include <syslog.h>
  111. #else
  112. enum {
  113. LOG_ERR,
  114. LOG_WARNING,
  115. LOG_NOTICE,
  116. LOG_INFO,
  117. LOG_DEBUG,
  118. };
  119. #endif
  120. #undef unlikely
  121. #undef likely
  122. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  123. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  124. #define likely(expr) (__builtin_expect(!!(expr), 1))
  125. #else
  126. #define unlikely(expr) (expr)
  127. #define likely(expr) (expr)
  128. #endif
  129. #if defined(__i386__)
  130. #define WANT_CRYPTOPP_ASM32
  131. #endif
  132. #ifndef ARRAY_SIZE
  133. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  134. #endif
  135. enum alive {
  136. LIFE_WELL,
  137. LIFE_SICK,
  138. LIFE_DEAD,
  139. LIFE_NOSTART
  140. };
  141. #ifdef HAVE_ADL
  142. struct gpu_adl {
  143. ADLTemperature lpTemperature;
  144. int iAdapterIndex;
  145. int lpAdapterID;
  146. ADLPMActivity lpActivity;
  147. ADLODParameters lpOdParameters;
  148. ADLODPerformanceLevels *DefPerfLev;
  149. ADLFanSpeedInfo lpFanSpeedInfo;
  150. ADLFanSpeedValue lpFanSpeedValue;
  151. ADLFanSpeedValue DefFanSpeedValue;
  152. int iEngineClock;
  153. int iMemoryClock;
  154. int iVddc;
  155. int iPercentage;
  156. bool autofan;
  157. bool autoengine;
  158. bool managed; /* Were the values ever changed on this card */
  159. int lasttemp;
  160. int targetfan;
  161. int targettemp;
  162. int overtemp;
  163. int cutofftemp;
  164. int minspeed;
  165. int maxspeed;
  166. };
  167. #endif
  168. struct cgpu_info {
  169. int is_gpu;
  170. int cpu_gpu;
  171. int accepted;
  172. int rejected;
  173. int hw_errors;
  174. double rolling;
  175. double total_mhashes;
  176. double utility;
  177. enum alive status;
  178. char init[40];
  179. struct timeval last_message_tv;
  180. bool dynamic;
  181. int intensity;
  182. #ifdef HAVE_ADL
  183. bool has_adl;
  184. struct gpu_adl adl;
  185. int gpu_engine;
  186. int min_engine;
  187. int gpu_fan;
  188. int min_fan;
  189. int gpu_memclock;
  190. int gpu_powertune;
  191. float gpu_vddc;
  192. #endif
  193. };
  194. struct thread_q {
  195. struct list_head q;
  196. bool frozen;
  197. pthread_mutex_t mutex;
  198. pthread_cond_t cond;
  199. };
  200. struct thr_info {
  201. int id;
  202. pthread_t pth;
  203. struct thread_q *q;
  204. struct cgpu_info *cgpu;
  205. struct timeval last;
  206. struct timeval sick;
  207. bool pause;
  208. bool getwork;
  209. double rolling;
  210. };
  211. extern int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  212. extern void thr_info_cancel(struct thr_info *thr);
  213. static inline uint32_t swab32(uint32_t v)
  214. {
  215. return bswap_32(v);
  216. }
  217. static inline void swap256(void *dest_p, const void *src_p)
  218. {
  219. uint32_t *dest = dest_p;
  220. const uint32_t *src = src_p;
  221. dest[0] = src[7];
  222. dest[1] = src[6];
  223. dest[2] = src[5];
  224. dest[3] = src[4];
  225. dest[4] = src[3];
  226. dest[5] = src[2];
  227. dest[6] = src[1];
  228. dest[7] = src[0];
  229. }
  230. extern void quit(int status, const char *format, ...);
  231. static inline void mutex_lock(pthread_mutex_t *lock)
  232. {
  233. if (unlikely(pthread_mutex_lock(lock)))
  234. quit(1, "WTF MUTEX ERROR ON LOCK!");
  235. }
  236. static inline void mutex_unlock(pthread_mutex_t *lock)
  237. {
  238. if (unlikely(pthread_mutex_unlock(lock)))
  239. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  240. }
  241. static inline void wr_lock(pthread_rwlock_t *lock)
  242. {
  243. if (unlikely(pthread_rwlock_wrlock(lock)))
  244. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  245. }
  246. static inline void rd_lock(pthread_rwlock_t *lock)
  247. {
  248. if (unlikely(pthread_rwlock_rdlock(lock)))
  249. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  250. }
  251. static inline void rw_unlock(pthread_rwlock_t *lock)
  252. {
  253. if (unlikely(pthread_rwlock_unlock(lock)))
  254. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  255. }
  256. static inline void rd_unlock(pthread_rwlock_t *lock)
  257. {
  258. rw_unlock(lock);
  259. }
  260. static inline void wr_unlock(pthread_rwlock_t *lock)
  261. {
  262. rw_unlock(lock);
  263. }
  264. struct pool;
  265. extern bool opt_debug;
  266. extern bool opt_protocol;
  267. extern bool opt_log_output;
  268. extern char *opt_kernel_path;
  269. extern char *cgminer_path;
  270. extern bool opt_autofan;
  271. extern bool opt_autoengine;
  272. extern bool use_curses;
  273. extern const uint32_t sha256_init_state[];
  274. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  275. const char *rpc_req, bool, bool, bool *,
  276. struct pool *pool);
  277. extern char *bin2hex(const unsigned char *p, size_t len);
  278. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  279. extern unsigned int ScanHash_4WaySSE2(int, const unsigned char *pmidstate,
  280. unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
  281. const unsigned char *ptarget,
  282. uint32_t max_nonce, unsigned long *nHashesDone, uint32_t nonce);
  283. extern unsigned int scanhash_sse2_amd64(int, const unsigned char *pmidstate,
  284. unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
  285. const unsigned char *ptarget,
  286. uint32_t max_nonce, unsigned long *nHashesDone);
  287. extern bool scanhash_via(int, unsigned char *data_inout,
  288. const unsigned char *target,
  289. uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
  290. extern bool scanhash_c(int, const unsigned char *midstate, unsigned char *data,
  291. unsigned char *hash1, unsigned char *hash,
  292. const unsigned char *target,
  293. uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
  294. extern bool scanhash_cryptopp(int, const unsigned char *midstate,unsigned char *data,
  295. unsigned char *hash1, unsigned char *hash,
  296. const unsigned char *target,
  297. uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
  298. extern bool scanhash_asm32(int, const unsigned char *midstate,unsigned char *data,
  299. unsigned char *hash1, unsigned char *hash,
  300. const unsigned char *target,
  301. uint32_t max_nonce, unsigned long *hashes_done, uint32_t nonce);
  302. extern int scanhash_sse2_64(int, const unsigned char *pmidstate, unsigned char *pdata,
  303. unsigned char *phash1, unsigned char *phash,
  304. const unsigned char *ptarget,
  305. uint32_t max_nonce, unsigned long *nHashesDone,
  306. uint32_t nonce);
  307. extern int scanhash_sse4_64(int, const unsigned char *pmidstate, unsigned char *pdata,
  308. unsigned char *phash1, unsigned char *phash,
  309. const unsigned char *ptarget,
  310. uint32_t max_nonce, unsigned long *nHashesDone,
  311. uint32_t nonce);
  312. extern int scanhash_sse2_32(int, const unsigned char *pmidstate, unsigned char *pdata,
  313. unsigned char *phash1, unsigned char *phash,
  314. const unsigned char *ptarget,
  315. uint32_t max_nonce, unsigned long *nHashesDone,
  316. uint32_t nonce);
  317. extern int
  318. timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y);
  319. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  320. extern int opt_scantime;
  321. struct work_restart {
  322. volatile unsigned long restart;
  323. char padding[128 - sizeof(unsigned long)];
  324. };
  325. #define MAX_GPUDEVICES 16
  326. extern int hw_errors;
  327. extern bool use_syslog;
  328. extern struct thr_info *thr_info;
  329. extern int longpoll_thr_id;
  330. extern struct work_restart *work_restart;
  331. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  332. #ifdef HAVE_OPENCL
  333. typedef struct {
  334. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  335. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  336. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  337. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  338. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  339. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  340. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  341. cl_uint W16; cl_uint W17; cl_uint W2;
  342. cl_uint PreVal4; cl_uint T1;
  343. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  344. cl_uint PreVal4addT1; cl_uint T1substate0;
  345. cl_uint PreVal4_2;
  346. cl_uint PreVal0;
  347. cl_uint PreW18;
  348. cl_uint PreW19;
  349. cl_uint PreW31;
  350. cl_uint PreW32;
  351. } dev_blk_ctx;
  352. #else
  353. typedef struct {
  354. uint32_t nonce;
  355. } dev_blk_ctx;
  356. #endif
  357. struct pool {
  358. int pool_no;
  359. int prio;
  360. int accepted, rejected;
  361. bool submit_fail;
  362. bool idle;
  363. bool lagging;
  364. bool probed;
  365. bool enabled;
  366. char *hdr_path;
  367. unsigned int getwork_requested;
  368. unsigned int stale_shares;
  369. unsigned int discarded_work;
  370. unsigned int getfail_occasions;
  371. unsigned int remotefail_occasions;
  372. struct timeval tv_idle;
  373. char *rpc_url;
  374. char *rpc_userpass;
  375. char *rpc_user, *rpc_pass;
  376. pthread_mutex_t pool_lock;
  377. };
  378. struct work {
  379. unsigned char data[128];
  380. unsigned char hash1[64];
  381. unsigned char midstate[32];
  382. unsigned char target[32];
  383. unsigned char hash[32];
  384. int rolls;
  385. uint32_t output[1];
  386. uint32_t valid;
  387. dev_blk_ctx blk;
  388. struct thr_info *thr;
  389. int thr_id;
  390. struct pool *pool;
  391. struct timeval tv_staged;
  392. bool mined;
  393. bool clone;
  394. bool cloned;
  395. bool rolltime;
  396. int id;
  397. UT_hash_handle hh;
  398. };
  399. enum cl_kernel {
  400. KL_NONE,
  401. KL_POCLBM,
  402. KL_PHATK,
  403. };
  404. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  405. extern void wlogprint(const char *f, ...);
  406. extern int curses_int(const char *query);
  407. extern char *curses_input(const char *query);
  408. extern void kill_work(void);
  409. extern void log_curses(int prio, const char *f, va_list ap);
  410. extern void clear_logwin(void);
  411. extern void vapplog(int prio, const char *fmt, va_list ap);
  412. extern void applog(int prio, const char *fmt, ...);
  413. extern struct thread_q *tq_new(void);
  414. extern void tq_free(struct thread_q *tq);
  415. extern bool tq_push(struct thread_q *tq, void *data);
  416. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  417. extern void tq_freeze(struct thread_q *tq);
  418. extern void tq_thaw(struct thread_q *tq);
  419. extern bool successful_connect;
  420. extern enum cl_kernel chosen_kernel;
  421. extern void adl(void);
  422. extern bool get_dondata(char **url, char **userpass);
  423. #endif /* __MINER_H__ */