miner.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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. #include "CL/cl.h"
  15. #endif /* HAVE_OPENCL */
  16. #ifdef STDC_HEADERS
  17. # include <stdlib.h>
  18. # include <stddef.h>
  19. #else
  20. # ifdef HAVE_STDLIB_H
  21. # include <stdlib.h>
  22. # endif
  23. #endif
  24. #ifdef HAVE_ALLOCA_H
  25. # include <alloca.h>
  26. #elif defined __GNUC__
  27. # ifndef WIN32
  28. # define alloca __builtin_alloca
  29. # else
  30. # include <malloc.h>
  31. # endif
  32. #elif defined _AIX
  33. # define alloca __alloca
  34. #elif defined _MSC_VER
  35. # include <malloc.h>
  36. # define alloca _alloca
  37. #else
  38. # ifndef HAVE_ALLOCA
  39. # ifdef __cplusplus
  40. extern "C"
  41. # endif
  42. void *alloca (size_t);
  43. # endif
  44. #endif
  45. #if defined (__linux)
  46. #ifndef LINUX
  47. #define LINUX
  48. #endif
  49. #endif
  50. #ifdef HAVE_ADL
  51. #include "ADL_SDK/adl_sdk.h"
  52. #endif
  53. #ifdef HAVE_LIBUSB
  54. #include <libusb-1.0/libusb.h>
  55. #endif
  56. #ifdef USE_ZTEX
  57. #include "libztex.h"
  58. #endif
  59. #if !defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
  60. #define bswap_16 __builtin_bswap16
  61. #define bswap_32 __builtin_bswap32
  62. #define bswap_64 __builtin_bswap64
  63. #else
  64. #if HAVE_BYTESWAP_H
  65. #include <byteswap.h>
  66. #elif defined(USE_SYS_ENDIAN_H)
  67. #include <sys/endian.h>
  68. #elif defined(__APPLE__)
  69. #include <libkern/OSByteOrder.h>
  70. #define bswap_16 OSSwapInt16
  71. #define bswap_32 OSSwapInt32
  72. #define bswap_64 OSSwapInt64
  73. #else
  74. #define bswap_16(value) \
  75. ((((value) & 0xff) << 8) | ((value) >> 8))
  76. #define bswap_32(value) \
  77. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  78. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  79. #define bswap_64(value) \
  80. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  81. << 32) | \
  82. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  83. #endif
  84. #endif /* !defined(__GLXBYTEORDER_H__) */
  85. /* This assumes htobe32 is a macro in endian.h */
  86. #ifndef htobe32
  87. # if __BYTE_ORDER == __LITTLE_ENDIAN
  88. # define be32toh(x) bswap_32(x)
  89. # define htobe32(x) bswap_32(x)
  90. # elif __BYTE_ORDER == __BIG_ENDIAN
  91. # define be32toh(x) (x)
  92. # define htobe32(x) (x)
  93. #else
  94. #error UNKNOWN BYTE ORDER
  95. #endif
  96. #endif
  97. #undef unlikely
  98. #undef likely
  99. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  100. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  101. #define likely(expr) (__builtin_expect(!!(expr), 1))
  102. #else
  103. #define unlikely(expr) (expr)
  104. #define likely(expr) (expr)
  105. #endif
  106. #define __maybe_unused __attribute__((unused))
  107. #if defined(__i386__)
  108. #define WANT_CRYPTOPP_ASM32
  109. #endif
  110. #ifndef ARRAY_SIZE
  111. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  112. #endif
  113. enum alive {
  114. LIFE_WELL,
  115. LIFE_SICK,
  116. LIFE_DEAD,
  117. LIFE_NOSTART
  118. };
  119. enum pool_strategy {
  120. POOL_FAILOVER,
  121. POOL_ROUNDROBIN,
  122. POOL_ROTATE,
  123. POOL_LOADBALANCE,
  124. };
  125. #define TOP_STRATEGY (POOL_LOADBALANCE)
  126. struct strategies {
  127. const char *s;
  128. };
  129. struct cgpu_info;
  130. #ifdef HAVE_ADL
  131. struct gpu_adl {
  132. ADLTemperature lpTemperature;
  133. int iAdapterIndex;
  134. int lpAdapterID;
  135. int iBusNumber;
  136. char strAdapterName[256];
  137. ADLPMActivity lpActivity;
  138. ADLODParameters lpOdParameters;
  139. ADLODPerformanceLevels *DefPerfLev;
  140. ADLFanSpeedInfo lpFanSpeedInfo;
  141. ADLFanSpeedValue lpFanSpeedValue;
  142. ADLFanSpeedValue DefFanSpeedValue;
  143. int iEngineClock;
  144. int iMemoryClock;
  145. int iVddc;
  146. int iPercentage;
  147. bool autofan;
  148. bool autoengine;
  149. bool managed; /* Were the values ever changed on this card */
  150. int lastengine;
  151. int lasttemp;
  152. int targetfan;
  153. int targettemp;
  154. int overtemp;
  155. int minspeed;
  156. int maxspeed;
  157. int gpu;
  158. bool has_fanspeed;
  159. struct gpu_adl *twin;
  160. };
  161. #endif
  162. struct thr_info;
  163. struct work;
  164. struct device_api {
  165. char*dname;
  166. char*name;
  167. // API-global functions
  168. void (*api_detect)();
  169. // Device-specific functions
  170. void (*reinit_device)(struct cgpu_info*);
  171. void (*get_statline_before)(char*, struct cgpu_info*);
  172. void (*get_statline)(char*, struct cgpu_info*);
  173. // Thread-specific functions
  174. bool (*thread_prepare)(struct thr_info*);
  175. uint64_t (*can_limit_work)(struct thr_info*);
  176. bool (*thread_init)(struct thr_info*);
  177. void (*free_work)(struct thr_info*, struct work*);
  178. bool (*prepare_work)(struct thr_info*, struct work*);
  179. uint64_t (*scanhash)(struct thr_info*, struct work*, uint64_t);
  180. void (*thread_shutdown)(struct thr_info*);
  181. };
  182. enum dev_enable {
  183. DEV_ENABLED,
  184. DEV_DISABLED,
  185. DEV_RECOVER,
  186. };
  187. enum cl_kernels {
  188. KL_NONE,
  189. KL_POCLBM,
  190. KL_PHATK,
  191. KL_DIAKGCN,
  192. KL_DIABLO,
  193. };
  194. enum dev_reason {
  195. REASON_THREAD_FAIL_INIT,
  196. REASON_THREAD_ZERO_HASH,
  197. REASON_THREAD_FAIL_QUEUE,
  198. REASON_DEV_SICK_IDLE_60,
  199. REASON_DEV_DEAD_IDLE_600,
  200. REASON_DEV_NOSTART,
  201. REASON_DEV_OVER_HEAT,
  202. REASON_DEV_THERMAL_CUTOFF,
  203. };
  204. #define REASON_NONE "None"
  205. #define REASON_THREAD_FAIL_INIT_STR "Thread failed to init"
  206. #define REASON_THREAD_ZERO_HASH_STR "Thread got zero hashes"
  207. #define REASON_THREAD_FAIL_QUEUE_STR "Thread failed to queue work"
  208. #define REASON_DEV_SICK_IDLE_60_STR "Device idle for 60s"
  209. #define REASON_DEV_DEAD_IDLE_600_STR "Device dead - idle for 600s"
  210. #define REASON_DEV_NOSTART_STR "Device failed to start"
  211. #define REASON_DEV_OVER_HEAT_STR "Device over heated"
  212. #define REASON_DEV_THERMAL_CUTOFF_STR "Device reached thermal cutoff"
  213. #define REASON_UNKNOWN_STR "Unknown reason - code bug"
  214. struct cgpu_info {
  215. int cgminer_id;
  216. struct device_api *api;
  217. int device_id;
  218. char *name;
  219. char *device_path;
  220. FILE *device_file;
  221. union {
  222. #ifdef USE_ZTEX
  223. struct libztex_device *device_ztex;
  224. #endif
  225. int device_fd;
  226. };
  227. enum dev_enable deven;
  228. int accepted;
  229. int rejected;
  230. int hw_errors;
  231. double rolling;
  232. double total_mhashes;
  233. double utility;
  234. enum alive status;
  235. char init[40];
  236. struct timeval last_message_tv;
  237. int threads;
  238. struct thr_info *thread;
  239. unsigned int max_hashes;
  240. bool mapped;
  241. int virtual_gpu;
  242. int virtual_adl;
  243. int intensity;
  244. bool dynamic;
  245. char *kname;
  246. #ifdef HAVE_OPENCL
  247. cl_uint vwidth;
  248. size_t work_size;
  249. enum cl_kernels kernel;
  250. #endif
  251. float temp;
  252. int cutofftemp;
  253. #ifdef HAVE_ADL
  254. bool has_adl;
  255. struct gpu_adl adl;
  256. int gpu_engine;
  257. int min_engine;
  258. int gpu_fan;
  259. int min_fan;
  260. int gpu_memclock;
  261. int gpu_memdiff;
  262. int gpu_powertune;
  263. float gpu_vddc;
  264. #endif
  265. int last_share_pool;
  266. time_t last_share_pool_time;
  267. time_t device_last_well;
  268. time_t device_last_not_well;
  269. enum dev_reason device_not_well_reason;
  270. int thread_fail_init_count;
  271. int thread_zero_hash_count;
  272. int thread_fail_queue_count;
  273. int dev_sick_idle_60_count;
  274. int dev_dead_idle_600_count;
  275. int dev_nostart_count;
  276. int dev_over_heat_count; // It's a warning but worth knowing
  277. int dev_thermal_cutoff_count;
  278. };
  279. extern bool add_cgpu(struct cgpu_info*);
  280. struct thread_q {
  281. struct list_head q;
  282. bool frozen;
  283. pthread_mutex_t mutex;
  284. pthread_cond_t cond;
  285. };
  286. struct thr_info {
  287. int id;
  288. int device_thread;
  289. pthread_t pth;
  290. struct thread_q *q;
  291. struct cgpu_info *cgpu;
  292. void *cgpu_data;
  293. struct timeval last;
  294. struct timeval sick;
  295. bool pause;
  296. bool getwork;
  297. double rolling;
  298. };
  299. extern int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  300. extern void thr_info_cancel(struct thr_info *thr);
  301. extern void thr_info_freeze(struct thr_info *thr);
  302. struct string_elist {
  303. char *string;
  304. bool free_me;
  305. struct list_head list;
  306. };
  307. static inline void string_elist_add(const char *s, struct list_head *head)
  308. {
  309. struct string_elist *n;
  310. n = calloc(1, sizeof(*n));
  311. n->string = strdup(s);
  312. n->free_me = true;
  313. list_add_tail(&n->list, head);
  314. }
  315. static inline void string_elist_del(struct string_elist *item)
  316. {
  317. if (item->free_me)
  318. free(item->string);
  319. list_del(&item->list);
  320. }
  321. static inline uint32_t swab32(uint32_t v)
  322. {
  323. return bswap_32(v);
  324. }
  325. static inline void swap256(void *dest_p, const void *src_p)
  326. {
  327. uint32_t *dest = dest_p;
  328. const uint32_t *src = src_p;
  329. dest[0] = src[7];
  330. dest[1] = src[6];
  331. dest[2] = src[5];
  332. dest[3] = src[4];
  333. dest[4] = src[3];
  334. dest[5] = src[2];
  335. dest[6] = src[1];
  336. dest[7] = src[0];
  337. }
  338. extern void quit(int status, const char *format, ...);
  339. static inline void mutex_lock(pthread_mutex_t *lock)
  340. {
  341. if (unlikely(pthread_mutex_lock(lock)))
  342. quit(1, "WTF MUTEX ERROR ON LOCK!");
  343. }
  344. static inline void mutex_unlock(pthread_mutex_t *lock)
  345. {
  346. if (unlikely(pthread_mutex_unlock(lock)))
  347. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  348. }
  349. static inline void wr_lock(pthread_rwlock_t *lock)
  350. {
  351. if (unlikely(pthread_rwlock_wrlock(lock)))
  352. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  353. }
  354. static inline void rd_lock(pthread_rwlock_t *lock)
  355. {
  356. if (unlikely(pthread_rwlock_rdlock(lock)))
  357. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  358. }
  359. static inline void rw_unlock(pthread_rwlock_t *lock)
  360. {
  361. if (unlikely(pthread_rwlock_unlock(lock)))
  362. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  363. }
  364. static inline void rd_unlock(pthread_rwlock_t *lock)
  365. {
  366. rw_unlock(lock);
  367. }
  368. static inline void wr_unlock(pthread_rwlock_t *lock)
  369. {
  370. rw_unlock(lock);
  371. }
  372. static inline void mutex_init(pthread_mutex_t *lock)
  373. {
  374. if (unlikely(pthread_mutex_init(lock, NULL)))
  375. quit(1, "Failed to pthread_mutex_init");
  376. }
  377. static inline void rwlock_init(pthread_rwlock_t *lock)
  378. {
  379. if (unlikely(pthread_rwlock_init(lock, NULL)))
  380. quit(1, "Failed to pthread_rwlock_init");
  381. }
  382. struct pool;
  383. extern bool opt_protocol;
  384. extern char *opt_kernel_path;
  385. extern char *opt_socks_proxy;
  386. extern char *cgminer_path;
  387. extern bool opt_autofan;
  388. extern bool opt_autoengine;
  389. extern bool use_curses;
  390. extern char *opt_api_allow;
  391. extern char *opt_api_description;
  392. extern int opt_api_port;
  393. extern bool opt_api_listen;
  394. extern bool opt_api_network;
  395. extern bool opt_delaynet;
  396. extern bool opt_restart;
  397. extern pthread_rwlock_t netacc_lock;
  398. extern const uint32_t sha256_init_state[];
  399. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  400. const char *rpc_req, bool, bool, bool *,
  401. struct pool *pool, bool);
  402. extern char *bin2hex(const unsigned char *p, size_t len);
  403. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  404. typedef bool (*sha256_func)(int thr_id, const unsigned char *pmidstate,
  405. unsigned char *pdata,
  406. unsigned char *phash1, unsigned char *phash,
  407. const unsigned char *ptarget,
  408. uint32_t max_nonce,
  409. uint32_t *last_nonce,
  410. uint32_t nonce);
  411. extern int
  412. timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y);
  413. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  414. extern int opt_scantime;
  415. struct work_restart {
  416. volatile unsigned long restart;
  417. char padding[128 - sizeof(unsigned long)];
  418. };
  419. extern void thread_reportin(struct thr_info *thr);
  420. extern void kill_work(void);
  421. extern void reinit_device(struct cgpu_info *cgpu);
  422. #ifdef HAVE_ADL
  423. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  424. extern int set_fanspeed(int gpu, int iFanSpeed);
  425. extern int set_vddc(int gpu, float fVddc);
  426. extern int set_engineclock(int gpu, int iEngineClock);
  427. extern int set_memoryclock(int gpu, int iMemoryClock);
  428. #endif
  429. extern void api(int thr_id);
  430. extern struct pool *current_pool(void);
  431. extern int active_pools(void);
  432. extern int add_pool_details(bool live, char *url, char *user, char *pass);
  433. #define ADD_POOL_MAXIMUM 1
  434. #define ADD_POOL_OK 0
  435. #define MAX_GPUDEVICES 16
  436. #define MAX_DEVICES 64
  437. #define MAX_POOLS (32)
  438. #define MIN_INTENSITY -10
  439. #define _MIN_INTENSITY_STR "-10"
  440. #define MAX_INTENSITY 14
  441. #define _MAX_INTENSITY_STR "14"
  442. extern struct list_head scan_devices;
  443. extern int nDevs;
  444. extern int opt_n_threads;
  445. extern int num_processors;
  446. extern int hw_errors;
  447. extern bool use_syslog;
  448. extern struct thr_info *thr_info;
  449. extern int longpoll_thr_id;
  450. extern struct work_restart *work_restart;
  451. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  452. extern int gpu_threads;
  453. extern double total_secs;
  454. extern int mining_threads;
  455. extern struct cgpu_info *cpus;
  456. extern int total_devices;
  457. extern struct cgpu_info *devices[];
  458. extern int total_pools;
  459. extern struct pool *pools[MAX_POOLS];
  460. extern const char *algo_names[];
  461. extern enum sha256_algos opt_algo;
  462. extern struct strategies strategies[];
  463. extern enum pool_strategy pool_strategy;
  464. extern int opt_rotate_period;
  465. extern double total_mhashes_done;
  466. extern unsigned int new_blocks;
  467. extern unsigned int found_blocks;
  468. extern int total_accepted, total_rejected;
  469. extern int total_getworks, total_stale, total_discarded;
  470. extern unsigned int local_work;
  471. extern unsigned int total_go, total_ro;
  472. extern const int opt_cutofftemp;
  473. extern int opt_log_interval;
  474. #ifdef HAVE_OPENCL
  475. typedef struct {
  476. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  477. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  478. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  479. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  480. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  481. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  482. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  483. cl_uint W16; cl_uint W17; cl_uint W2;
  484. cl_uint PreVal4; cl_uint T1;
  485. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  486. cl_uint PreVal4addT1; cl_uint T1substate0;
  487. cl_uint PreVal4_2;
  488. cl_uint PreVal0;
  489. cl_uint PreW18;
  490. cl_uint PreW19;
  491. cl_uint PreW31;
  492. cl_uint PreW32;
  493. /* For diakgcn */
  494. cl_uint B1addK6, PreVal0addK7, W16addK16, W17addK17;
  495. cl_uint zeroA, zeroB;
  496. cl_uint oneA, twoA, threeA, fourA, fiveA, sixA, sevenA;
  497. } dev_blk_ctx;
  498. #else
  499. typedef struct {
  500. uint32_t nonce;
  501. } dev_blk_ctx;
  502. #endif
  503. struct pool {
  504. int pool_no;
  505. int prio;
  506. int accepted, rejected;
  507. bool submit_fail;
  508. bool idle;
  509. bool lagging;
  510. bool probed;
  511. bool enabled;
  512. bool submit_old;
  513. char *hdr_path;
  514. char *lp_url;
  515. bool lp_sent;
  516. bool is_lp;
  517. unsigned int getwork_requested;
  518. unsigned int stale_shares;
  519. unsigned int discarded_work;
  520. unsigned int getfail_occasions;
  521. unsigned int remotefail_occasions;
  522. struct timeval tv_idle;
  523. char *rpc_url;
  524. char *rpc_userpass;
  525. char *rpc_user, *rpc_pass;
  526. pthread_mutex_t pool_lock;
  527. };
  528. struct work {
  529. unsigned char data[128];
  530. unsigned char hash1[64];
  531. unsigned char midstate[32];
  532. unsigned char target[32];
  533. unsigned char hash[32];
  534. int rolls;
  535. uint32_t output[1];
  536. uint32_t valid;
  537. dev_blk_ctx blk;
  538. struct thr_info *thr;
  539. int thr_id;
  540. struct pool *pool;
  541. struct timeval tv_staged;
  542. bool mined;
  543. bool clone;
  544. bool cloned;
  545. bool rolltime;
  546. bool longpoll;
  547. unsigned int work_block;
  548. int id;
  549. UT_hash_handle hh;
  550. time_t share_found_time;
  551. };
  552. extern void get_datestamp(char *, struct timeval *);
  553. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  554. extern void tailsprintf(char *f, const char *fmt, ...);
  555. extern void wlogprint(const char *f, ...);
  556. extern int curses_int(const char *query);
  557. extern char *curses_input(const char *query);
  558. extern void kill_work(void);
  559. extern void switch_pools(struct pool *selected);
  560. extern void remove_pool(struct pool *pool);
  561. extern void write_config(FILE *fcfg);
  562. extern void log_curses(int prio, const char *f, va_list ap);
  563. extern void clear_logwin(void);
  564. extern bool pool_tclear(struct pool *pool, bool *var);
  565. extern struct thread_q *tq_new(void);
  566. extern void tq_free(struct thread_q *tq);
  567. extern bool tq_push(struct thread_q *tq, void *data);
  568. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  569. extern void tq_freeze(struct thread_q *tq);
  570. extern void tq_thaw(struct thread_q *tq);
  571. extern bool successful_connect;
  572. extern void adl(void);
  573. extern void app_restart(void);
  574. #endif /* __MINER_H__ */