miner.h 26 KB

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