miner.h 26 KB

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