miner.h 27 KB

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