miner.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  1. #ifndef __MINER_H__
  2. #define __MINER_H__
  3. #include "config.h"
  4. #ifdef WIN32
  5. #include <winsock2.h>
  6. #endif
  7. #include <stdbool.h>
  8. #include <stdint.h>
  9. #include <sys/time.h>
  10. #include <pthread.h>
  11. #include <jansson.h>
  12. #include <curl/curl.h>
  13. #include <blkmaker.h>
  14. #include <blktemplate.h>
  15. #if defined(WORDS_BIGENDIAN) && !defined(__BIG_ENDIAN__)
  16. /* uthash.h depends on __BIG_ENDIAN__ on BE platforms */
  17. #define __BIG_ENDIAN__ 1
  18. #endif
  19. #include "elist.h"
  20. #include "uthash.h"
  21. #include "logging.h"
  22. #include "util.h"
  23. #ifdef HAVE_OPENCL
  24. #include "CL/cl.h"
  25. #endif /* HAVE_OPENCL */
  26. #ifdef STDC_HEADERS
  27. # include <stdlib.h>
  28. # include <stddef.h>
  29. #else
  30. # ifdef HAVE_STDLIB_H
  31. # include <stdlib.h>
  32. # endif
  33. #endif
  34. #ifdef HAVE_ALLOCA_H
  35. # include <alloca.h>
  36. #elif defined __GNUC__
  37. # ifndef WIN32
  38. # define alloca __builtin_alloca
  39. # else
  40. # include <malloc.h>
  41. # endif
  42. #elif defined _AIX
  43. # define alloca __alloca
  44. #elif defined _MSC_VER
  45. # include <malloc.h>
  46. # define alloca _alloca
  47. #else
  48. # ifndef HAVE_ALLOCA
  49. # ifdef __cplusplus
  50. extern "C"
  51. # endif
  52. void *alloca (size_t);
  53. # endif
  54. #endif
  55. #ifdef __MINGW32__
  56. #include <windows.h>
  57. #include <io.h>
  58. static inline int fsync (int fd)
  59. {
  60. return (FlushFileBuffers ((HANDLE) _get_osfhandle (fd))) ? 0 : -1;
  61. }
  62. #ifndef EWOULDBLOCK
  63. # define EWOULDBLOCK EAGAIN
  64. #endif
  65. #ifndef MSG_DONTWAIT
  66. # define MSG_DONTWAIT 0x1000000
  67. #endif
  68. #endif /* __MINGW32__ */
  69. #if defined (__linux)
  70. #ifndef LINUX
  71. #define LINUX
  72. #endif
  73. #endif
  74. #ifdef HAVE_ADL
  75. #include "ADL/adl_sdk.h"
  76. #endif
  77. #ifdef HAVE_LIBUSB
  78. #include <libusb.h>
  79. #endif
  80. #ifdef USE_ZTEX
  81. #include "libztex.h"
  82. #endif
  83. #ifdef HAVE_BYTESWAP_H
  84. #include <byteswap.h>
  85. #endif
  86. #ifdef HAVE_ENDIAN_H
  87. #include <endian.h>
  88. #endif
  89. #ifdef HAVE_SYS_ENDIAN_H
  90. #include <sys/endian.h>
  91. #endif
  92. #ifdef HAVE_LIBKERN_OSBYTEORDER_H
  93. #include <libkern/OSByteOrder.h>
  94. #endif
  95. #ifndef bswap_16
  96. #define bswap_16(value) \
  97. ((((value) & 0xff) << 8) | ((value) >> 8))
  98. #define bswap_32(value) \
  99. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  100. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  101. #define bswap_64(value) \
  102. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  103. << 32) | \
  104. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  105. #endif
  106. /* This assumes htobe32 is a macro and that if it doesn't exist, then the
  107. * also won't exist */
  108. #ifndef htobe32
  109. # ifndef WORDS_BIGENDIAN
  110. # define htole16(x) (x)
  111. # define htole32(x) (x)
  112. # define htole64(x) (x)
  113. # define htobe32(x) bswap_32(x)
  114. # define htobe64(x) bswap_64(x)
  115. # else
  116. # define htole16(x) bswap_16(x)
  117. # define htole32(x) bswap_32(x)
  118. # define htole64(x) bswap_64(x)
  119. # define htobe32(x) (x)
  120. # define htobe64(x) (x)
  121. # endif
  122. #endif
  123. #ifndef be32toh
  124. # define le32toh(x) htole32(x)
  125. # define le64toh(x) htole64(x)
  126. # define be32toh(x) htobe32(x)
  127. # define be64toh(x) htobe64(x)
  128. #endif
  129. #ifndef max
  130. # define max(a, b) ((a) > (b) ? (a) : (b))
  131. #endif
  132. #undef unlikely
  133. #undef likely
  134. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  135. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  136. #define likely(expr) (__builtin_expect(!!(expr), 1))
  137. #else
  138. #define unlikely(expr) (expr)
  139. #define likely(expr) (expr)
  140. #endif
  141. #ifndef __maybe_unused
  142. #define __maybe_unused __attribute__((unused))
  143. #endif
  144. #define uninitialised_var(x) x = x
  145. #if defined(__i386__)
  146. #define WANT_CRYPTOPP_ASM32
  147. #endif
  148. #ifndef ARRAY_SIZE
  149. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  150. #endif
  151. #ifdef HAVE_CURSES
  152. extern int my_cancellable_getch(void);
  153. # ifdef getch
  154. // getch() is a macro
  155. static int __maybe_unused __real_getch(void) {
  156. return getch();
  157. }
  158. # undef getch
  159. # define getch() my_cancellable_getch()
  160. # else
  161. // getch() is a real function
  162. # define __real_getch getch
  163. # define getch() my_cancellable_getch()
  164. # endif
  165. #endif
  166. #ifndef roundl
  167. #define roundl(x) (long double)((long long)((x==0)?0.0:((x)+((x)>0)?0.5:-0.5)))
  168. #endif
  169. enum alive {
  170. LIFE_WELL,
  171. LIFE_SICK,
  172. LIFE_DEAD,
  173. LIFE_NOSTART,
  174. LIFE_INIT,
  175. LIFE_WAIT,
  176. };
  177. enum pool_strategy {
  178. POOL_FAILOVER,
  179. POOL_ROUNDROBIN,
  180. POOL_ROTATE,
  181. POOL_LOADBALANCE,
  182. POOL_BALANCE,
  183. };
  184. #define TOP_STRATEGY (POOL_BALANCE)
  185. struct strategies {
  186. const char *s;
  187. };
  188. struct cgpu_info;
  189. #ifdef HAVE_ADL
  190. struct gpu_adl {
  191. ADLTemperature lpTemperature;
  192. int iAdapterIndex;
  193. int lpAdapterID;
  194. int iBusNumber;
  195. char strAdapterName[256];
  196. ADLPMActivity lpActivity;
  197. ADLODParameters lpOdParameters;
  198. ADLODPerformanceLevels *DefPerfLev;
  199. ADLFanSpeedInfo lpFanSpeedInfo;
  200. ADLFanSpeedValue lpFanSpeedValue;
  201. ADLFanSpeedValue DefFanSpeedValue;
  202. int iEngineClock;
  203. int iMemoryClock;
  204. int iVddc;
  205. int iPercentage;
  206. bool autofan;
  207. bool autoengine;
  208. bool managed; /* Were the values ever changed on this card */
  209. int lastengine;
  210. int lasttemp;
  211. int targetfan;
  212. int overtemp;
  213. int minspeed;
  214. int maxspeed;
  215. int gpu;
  216. bool has_fanspeed;
  217. struct gpu_adl *twin;
  218. };
  219. #endif
  220. struct api_data;
  221. struct thr_info;
  222. struct work;
  223. struct device_api {
  224. const char *dname;
  225. const char *name;
  226. // API-global functions
  227. void (*api_detect)();
  228. // Device-specific functions
  229. void (*get_dev_statline_before)(char *, struct cgpu_info *);
  230. void (*get_dev_statline_after)(char *, struct cgpu_info *);
  231. // Processor-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_extra_device_detail)(struct cgpu_info *);
  236. struct api_data* (*get_api_extra_device_status)(struct cgpu_info *);
  237. struct api_data *(*get_api_stats)(struct cgpu_info *);
  238. bool (*get_stats)(struct cgpu_info *);
  239. bool (*identify_device)(struct cgpu_info *); // e.g. to flash a led
  240. char *(*set_device)(struct cgpu_info *, char *option, char *setting, char *replybuf);
  241. // Thread-specific functions
  242. bool (*thread_prepare)(struct thr_info *);
  243. void (*minerloop)(struct thr_info *);
  244. uint64_t (*can_limit_work)(struct thr_info *);
  245. bool (*thread_init)(struct thr_info *);
  246. bool (*prepare_work)(struct thr_info *, struct work *);
  247. int64_t (*scanhash)(struct thr_info *, struct work *, int64_t);
  248. void (*hw_error)(struct thr_info *);
  249. void (*thread_shutdown)(struct thr_info *);
  250. void (*thread_enable)(struct thr_info *);
  251. // === Implemented by minerloop_async ===
  252. // Can be used per-thread or per-processor
  253. void (*poll)(struct thr_info *);
  254. // Job-specific functions (only with minerloop_async!)
  255. bool (*job_prepare)(struct thr_info*, struct work*, uint64_t);
  256. void (*job_start)(struct thr_info*);
  257. void (*job_get_results)(struct thr_info*, struct work*);
  258. int64_t (*job_process_results)(struct thr_info*, struct work*, bool stopping);
  259. };
  260. enum dev_enable {
  261. DEV_ENABLED,
  262. DEV_DISABLED,
  263. DEV_RECOVER,
  264. DEV_RECOVER_ERR,
  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. enum {
  300. MSG_NOPOOL = 8,
  301. MSG_MISPID = 25,
  302. MSG_INVPID = 26,
  303. MSG_DUPPID = 74,
  304. MSG_POOLPRIO = 73,
  305. };
  306. struct cgminer_stats {
  307. uint32_t getwork_calls;
  308. struct timeval getwork_wait;
  309. struct timeval getwork_wait_max;
  310. struct timeval getwork_wait_min;
  311. struct timeval _get_start;
  312. };
  313. // Just the actual network getworks to the pool
  314. struct cgminer_pool_stats {
  315. uint32_t getwork_calls;
  316. uint32_t getwork_attempts;
  317. struct timeval getwork_wait;
  318. struct timeval getwork_wait_max;
  319. struct timeval getwork_wait_min;
  320. double getwork_wait_rolling;
  321. bool hadrolltime;
  322. bool canroll;
  323. bool hadexpire;
  324. uint32_t rolltime;
  325. double min_diff;
  326. double max_diff;
  327. double last_diff;
  328. uint32_t min_diff_count;
  329. uint32_t max_diff_count;
  330. uint64_t times_sent;
  331. uint64_t bytes_sent;
  332. uint64_t net_bytes_sent;
  333. uint64_t times_received;
  334. uint64_t bytes_received;
  335. uint64_t net_bytes_received;
  336. };
  337. #define PRIprepr "-6s"
  338. #define PRIpreprv "s"
  339. struct cgpu_info {
  340. int cgminer_id;
  341. int device_line_id;
  342. const struct device_api *api;
  343. const char *devtype;
  344. int device_id;
  345. char *dev_repr;
  346. char *dev_repr_ns;
  347. const char *name;
  348. int procs;
  349. int proc_id;
  350. char proc_repr[8];
  351. char proc_repr_ns[8];
  352. struct cgpu_info *device;
  353. struct cgpu_info *next_proc;
  354. const char *device_path;
  355. FILE *device_file;
  356. union {
  357. #ifdef USE_ZTEX
  358. struct libztex_device *device_ztex;
  359. #endif
  360. int device_fd;
  361. #ifdef USE_X6500
  362. struct ft232r_device_handle *device_ft232r;
  363. #endif
  364. };
  365. #ifdef USE_BITFORCE
  366. struct timeval work_start_tv;
  367. unsigned int wait_ms;
  368. unsigned int sleep_ms;
  369. double avg_wait_f;
  370. unsigned int avg_wait_d;
  371. uint32_t nonces;
  372. bool polling;
  373. #endif
  374. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  375. bool flash_led;
  376. #endif
  377. void *cgpu_data;
  378. pthread_mutex_t device_mutex;
  379. pthread_cond_t device_cond;
  380. enum dev_enable deven;
  381. int accepted;
  382. int rejected;
  383. int hw_errors;
  384. double rolling;
  385. double total_mhashes;
  386. double utility;
  387. double utility_diff1;
  388. enum alive status;
  389. char init[40];
  390. struct timeval last_message_tv;
  391. int threads;
  392. struct thr_info **thr;
  393. int64_t max_hashes;
  394. const char *kname;
  395. #ifdef HAVE_OPENCL
  396. bool mapped;
  397. int virtual_gpu;
  398. int virtual_adl;
  399. int intensity;
  400. bool dynamic;
  401. cl_uint vwidth;
  402. size_t work_size;
  403. enum cl_kernels kernel;
  404. cl_ulong max_alloc;
  405. #ifdef USE_SCRYPT
  406. int opt_lg, lookup_gap;
  407. size_t opt_tc, thread_concurrency;
  408. size_t shaders;
  409. #endif
  410. struct timeval tv_gpustart;
  411. int intervals;
  412. #endif
  413. float temp;
  414. int cutofftemp;
  415. int targettemp;
  416. #ifdef HAVE_ADL
  417. bool has_adl;
  418. struct gpu_adl adl;
  419. int gpu_engine;
  420. int min_engine;
  421. int gpu_fan;
  422. int min_fan;
  423. int gpu_memclock;
  424. int gpu_memdiff;
  425. int gpu_powertune;
  426. float gpu_vddc;
  427. #endif
  428. int diff1;
  429. double diff_accepted;
  430. double diff_rejected;
  431. int last_share_pool;
  432. time_t last_share_pool_time;
  433. double last_share_diff;
  434. time_t device_last_well;
  435. time_t device_last_not_well;
  436. enum dev_reason device_not_well_reason;
  437. float reinit_backoff;
  438. int thread_fail_init_count;
  439. int thread_zero_hash_count;
  440. int thread_fail_queue_count;
  441. int dev_sick_idle_60_count;
  442. int dev_dead_idle_600_count;
  443. int dev_nostart_count;
  444. int dev_over_heat_count; // It's a warning but worth knowing
  445. int dev_thermal_cutoff_count;
  446. int dev_comms_error_count;
  447. int dev_throttle_count;
  448. struct cgminer_stats cgminer_stats;
  449. };
  450. extern void renumber_cgpu(struct cgpu_info *);
  451. extern bool add_cgpu(struct cgpu_info*);
  452. struct thread_q {
  453. struct list_head q;
  454. bool frozen;
  455. pthread_mutex_t mutex;
  456. pthread_cond_t cond;
  457. };
  458. enum thr_busy_state {
  459. TBS_IDLE,
  460. TBS_GETTING_RESULTS,
  461. TBS_STARTING_JOB,
  462. };
  463. struct thr_info {
  464. int id;
  465. int device_thread;
  466. bool primary_thread;
  467. pthread_t pth;
  468. struct thread_q *q;
  469. struct cgpu_info *cgpu;
  470. void *cgpu_data;
  471. struct timeval last;
  472. struct timeval sick;
  473. bool scanhash_working;
  474. uint64_t hashes_done;
  475. struct timeval tv_hashes_done;
  476. struct timeval tv_lastupdate;
  477. bool pause;
  478. time_t getwork;
  479. double rolling;
  480. // Used by minerloop_async
  481. struct work *prev_work;
  482. struct work *work;
  483. struct work *next_work;
  484. enum thr_busy_state busy_state;
  485. struct timeval tv_morework;
  486. struct work *results_work;
  487. bool _job_transition_in_progress;
  488. bool _proceed_with_new_job;
  489. struct timeval tv_results_jobstart;
  490. struct timeval tv_jobstart;
  491. struct timeval tv_poll;
  492. notifier_t notifier;
  493. bool starting_next_work;
  494. uint32_t _max_nonce;
  495. notifier_t mutex_request;
  496. bool work_restart;
  497. notifier_t work_restart_notifier;
  498. };
  499. extern int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  500. extern void thr_info_cancel(struct thr_info *thr);
  501. extern void thr_info_freeze(struct thr_info *thr);
  502. extern void nmsleep(unsigned int msecs);
  503. extern double us_tdiff(struct timeval *end, struct timeval *start);
  504. extern double tdiff(struct timeval *end, struct timeval *start);
  505. struct string_elist {
  506. char *string;
  507. bool free_me;
  508. struct list_head list;
  509. };
  510. static inline void string_elist_add(const char *s, struct list_head *head)
  511. {
  512. struct string_elist *n;
  513. n = calloc(1, sizeof(*n));
  514. n->string = strdup(s);
  515. n->free_me = true;
  516. list_add_tail(&n->list, head);
  517. }
  518. static inline void string_elist_del(struct string_elist *item)
  519. {
  520. if (item->free_me)
  521. free(item->string);
  522. list_del(&item->list);
  523. free(item);
  524. }
  525. static inline uint32_t swab32(uint32_t v)
  526. {
  527. return bswap_32(v);
  528. }
  529. static inline void swap256(void *dest_p, const void *src_p)
  530. {
  531. uint32_t *dest = dest_p;
  532. const uint32_t *src = src_p;
  533. dest[0] = src[7];
  534. dest[1] = src[6];
  535. dest[2] = src[5];
  536. dest[3] = src[4];
  537. dest[4] = src[3];
  538. dest[5] = src[2];
  539. dest[6] = src[1];
  540. dest[7] = src[0];
  541. }
  542. static inline void swap32yes(void*out, const void*in, size_t sz) {
  543. size_t swapcounter = 0;
  544. for (swapcounter = 0; swapcounter < sz; ++swapcounter)
  545. (((uint32_t*)out)[swapcounter]) = swab32(((uint32_t*)in)[swapcounter]);
  546. }
  547. #define LOCAL_swap32(type, var, sz) \
  548. type __swapped_ ## var[sz * 4 / sizeof(type)]; \
  549. swap32yes(__swapped_ ## var, var, sz); \
  550. var = __swapped_ ## var; \
  551. // end
  552. #ifdef WORDS_BIGENDIAN
  553. # define swap32tobe(out, in, sz) ((out == in) ? (void)0 : memmove(out, in, sz))
  554. # define LOCAL_swap32be(type, var, sz) ;
  555. # define swap32tole(out, in, sz) swap32yes(out, in, sz)
  556. # define LOCAL_swap32le(type, var, sz) LOCAL_swap32(type, var, sz)
  557. #else
  558. # define swap32tobe(out, in, sz) swap32yes(out, in, sz)
  559. # define LOCAL_swap32be(type, var, sz) LOCAL_swap32(type, var, sz)
  560. # define swap32tole(out, in, sz) ((out == in) ? (void)0 : memmove(out, in, sz))
  561. # define LOCAL_swap32le(type, var, sz) ;
  562. #endif
  563. static inline void swab256(void *dest_p, const void *src_p)
  564. {
  565. uint32_t *dest = dest_p;
  566. const uint32_t *src = src_p;
  567. dest[0] = swab32(src[7]);
  568. dest[1] = swab32(src[6]);
  569. dest[2] = swab32(src[5]);
  570. dest[3] = swab32(src[4]);
  571. dest[4] = swab32(src[3]);
  572. dest[5] = swab32(src[2]);
  573. dest[6] = swab32(src[1]);
  574. dest[7] = swab32(src[0]);
  575. }
  576. #define flip32(dest_p, src_p) swap32yes(dest_p, src_p, 32 / 4)
  577. extern void quit(int status, const char *format, ...);
  578. static inline void mutex_lock(pthread_mutex_t *lock)
  579. {
  580. if (unlikely(pthread_mutex_lock(lock)))
  581. quit(1, "WTF MUTEX ERROR ON LOCK!");
  582. }
  583. static inline void mutex_unlock(pthread_mutex_t *lock)
  584. {
  585. if (unlikely(pthread_mutex_unlock(lock)))
  586. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  587. }
  588. static inline int mutex_trylock(pthread_mutex_t *lock)
  589. {
  590. return pthread_mutex_trylock(lock);
  591. }
  592. static inline void wr_lock(pthread_rwlock_t *lock)
  593. {
  594. if (unlikely(pthread_rwlock_wrlock(lock)))
  595. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  596. }
  597. static inline void rd_lock(pthread_rwlock_t *lock)
  598. {
  599. if (unlikely(pthread_rwlock_rdlock(lock)))
  600. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  601. }
  602. static inline void rw_unlock(pthread_rwlock_t *lock)
  603. {
  604. if (unlikely(pthread_rwlock_unlock(lock)))
  605. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  606. }
  607. static inline void rd_unlock(pthread_rwlock_t *lock)
  608. {
  609. rw_unlock(lock);
  610. }
  611. static inline void wr_unlock(pthread_rwlock_t *lock)
  612. {
  613. rw_unlock(lock);
  614. }
  615. static inline void mutex_init(pthread_mutex_t *lock)
  616. {
  617. if (unlikely(pthread_mutex_init(lock, NULL)))
  618. quit(1, "Failed to pthread_mutex_init");
  619. }
  620. static inline void rwlock_init(pthread_rwlock_t *lock)
  621. {
  622. if (unlikely(pthread_rwlock_init(lock, NULL)))
  623. quit(1, "Failed to pthread_rwlock_init");
  624. }
  625. struct pool;
  626. extern bool opt_protocol;
  627. extern char *opt_coinbase_sig;
  628. extern bool have_longpoll;
  629. extern int opt_skip_checks;
  630. extern char *opt_kernel_path;
  631. extern char *opt_socks_proxy;
  632. extern char *cgminer_path;
  633. extern bool opt_fail_only;
  634. extern bool opt_autofan;
  635. extern bool opt_autoengine;
  636. extern bool use_curses;
  637. extern char *opt_api_allow;
  638. extern char *opt_api_groups;
  639. extern char *opt_api_description;
  640. extern int opt_api_port;
  641. extern bool opt_api_listen;
  642. extern bool opt_api_network;
  643. extern bool opt_delaynet;
  644. extern bool opt_restart;
  645. extern char *opt_icarus_options;
  646. extern char *opt_icarus_timing;
  647. extern bool opt_worktime;
  648. #ifdef USE_BITFORCE
  649. extern bool opt_bfl_noncerange;
  650. #endif
  651. extern int swork_id;
  652. extern pthread_rwlock_t netacc_lock;
  653. extern const uint32_t sha256_init_state[];
  654. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  655. const char *rpc_req, bool, bool, int *,
  656. struct pool *pool, bool);
  657. extern bool our_curl_supports_proxy_uris();
  658. extern char *bin2hex(const unsigned char *p, size_t len);
  659. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  660. typedef bool (*sha256_func)(struct thr_info*, const unsigned char *pmidstate,
  661. unsigned char *pdata,
  662. unsigned char *phash1, unsigned char *phash,
  663. const unsigned char *ptarget,
  664. uint32_t max_nonce,
  665. uint32_t *last_nonce,
  666. uint32_t nonce);
  667. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  668. extern int opt_queue;
  669. extern int opt_scantime;
  670. extern int opt_expiry;
  671. extern pthread_mutex_t hash_lock;
  672. extern pthread_mutex_t console_lock;
  673. extern pthread_mutex_t ch_lock;
  674. extern void thread_reportin(struct thr_info *thr);
  675. extern void thread_reportout(struct thr_info *);
  676. extern void clear_stratum_shares(struct pool *pool);
  677. extern void hashmeter2(struct thr_info *);
  678. extern bool stale_work(struct work *, bool share);
  679. extern bool stale_work_future(struct work *, bool share, unsigned long ustime);
  680. extern void kill_work(void);
  681. extern void app_restart(void);
  682. extern void mt_enable(struct thr_info *thr);
  683. extern void proc_enable(struct cgpu_info *);
  684. extern void reinit_device(struct cgpu_info *cgpu);
  685. #ifdef HAVE_ADL
  686. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  687. extern int set_fanspeed(int gpu, int iFanSpeed);
  688. extern int set_vddc(int gpu, float fVddc);
  689. extern int set_engineclock(int gpu, int iEngineClock);
  690. extern int set_memoryclock(int gpu, int iMemoryClock);
  691. #endif
  692. extern void api(int thr_id);
  693. extern struct pool *current_pool(void);
  694. extern int enabled_pools;
  695. extern bool detect_stratum(struct pool *pool, char *url);
  696. extern void print_summary(void);
  697. extern struct pool *add_pool(void);
  698. extern void add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
  699. #define MAX_GPUDEVICES 16
  700. #define MIN_INTENSITY -10
  701. #define _MIN_INTENSITY_STR "-10"
  702. #ifdef USE_SCRYPT
  703. #define MAX_INTENSITY 20
  704. #define _MAX_INTENSITY_STR "20"
  705. #else
  706. #define MAX_INTENSITY 14
  707. #define _MAX_INTENSITY_STR "14"
  708. #endif
  709. extern struct list_head scan_devices;
  710. extern bool opt_force_dev_init;
  711. extern int nDevs;
  712. extern int opt_n_threads;
  713. extern int num_processors;
  714. extern int hw_errors;
  715. extern bool use_syslog;
  716. extern bool opt_quiet;
  717. extern struct thr_info *thr_info;
  718. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  719. extern int gpu_threads;
  720. #ifdef USE_SCRYPT
  721. extern bool opt_scrypt;
  722. #else
  723. #define opt_scrypt (0)
  724. #endif
  725. extern double total_secs;
  726. extern int mining_threads;
  727. extern struct cgpu_info *cpus;
  728. extern int total_devices;
  729. extern struct cgpu_info **devices;
  730. extern int total_pools;
  731. extern struct pool **pools;
  732. extern const char *algo_names[];
  733. extern enum sha256_algos opt_algo;
  734. extern struct strategies strategies[];
  735. extern enum pool_strategy pool_strategy;
  736. extern int opt_rotate_period;
  737. extern double total_mhashes_done;
  738. extern unsigned int new_blocks;
  739. extern unsigned int found_blocks;
  740. extern int total_accepted, total_rejected, total_diff1;;
  741. extern int total_getworks, total_stale, total_discarded;
  742. extern uint64_t total_bytes_xfer;
  743. extern double total_diff_accepted, total_diff_rejected, total_diff_stale;
  744. extern unsigned int local_work;
  745. extern unsigned int total_go, total_ro;
  746. extern const int opt_cutofftemp;
  747. extern int opt_hysteresis;
  748. extern int opt_fail_pause;
  749. extern int opt_log_interval;
  750. extern unsigned long long global_hashrate;
  751. extern char *current_fullhash;
  752. extern uint64_t best_diff;
  753. extern struct timeval block_timeval;
  754. #ifdef HAVE_OPENCL
  755. typedef struct {
  756. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  757. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  758. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  759. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  760. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  761. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  762. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  763. cl_uint W16; cl_uint W17; cl_uint W2;
  764. cl_uint PreVal4; cl_uint T1;
  765. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  766. cl_uint PreVal4addT1; cl_uint T1substate0;
  767. cl_uint PreVal4_2;
  768. cl_uint PreVal0;
  769. cl_uint PreW18;
  770. cl_uint PreW19;
  771. cl_uint PreW31;
  772. cl_uint PreW32;
  773. /* For diakgcn */
  774. cl_uint B1addK6, PreVal0addK7, W16addK16, W17addK17;
  775. cl_uint zeroA, zeroB;
  776. cl_uint oneA, twoA, threeA, fourA, fiveA, sixA, sevenA;
  777. #ifdef USE_SCRYPT
  778. struct work *work;
  779. #endif
  780. } dev_blk_ctx;
  781. #else
  782. typedef struct {
  783. uint32_t nonce;
  784. } dev_blk_ctx;
  785. #endif
  786. struct curl_ent {
  787. CURL *curl;
  788. struct list_head node;
  789. struct timeval tv;
  790. };
  791. /* Disabled needs to be the lowest enum as a freshly calloced value will then
  792. * equal disabled */
  793. enum pool_enable {
  794. POOL_DISABLED,
  795. POOL_ENABLED,
  796. POOL_REJECTING,
  797. };
  798. enum pool_protocol {
  799. PLP_NONE,
  800. PLP_GETWORK,
  801. PLP_GETBLOCKTEMPLATE,
  802. };
  803. struct stratum_work {
  804. char *job_id;
  805. char *prev_hash;
  806. char *coinbase1;
  807. char *coinbase2;
  808. char **merkle;
  809. char *bbversion;
  810. char *nbit;
  811. char *ntime;
  812. bool clean;
  813. size_t cb1_len;
  814. size_t cb2_len;
  815. size_t cb_len;
  816. size_t header_len;
  817. int merkles;
  818. double diff;
  819. bool transparency_probed;
  820. time_t transparency_time;
  821. bool opaque;
  822. };
  823. #define RBUFSIZE 8192
  824. #define RECVSIZE (RBUFSIZE - 4)
  825. struct pool {
  826. int pool_no;
  827. int prio;
  828. int accepted, rejected;
  829. int seq_rejects;
  830. int seq_getfails;
  831. int solved;
  832. int diff1;
  833. char diff[8];
  834. double diff_accepted;
  835. double diff_rejected;
  836. double diff_stale;
  837. bool submit_fail;
  838. bool idle;
  839. bool lagging;
  840. bool probed;
  841. int force_rollntime;
  842. enum pool_enable enabled;
  843. bool submit_old;
  844. bool removed;
  845. bool lp_started;
  846. unsigned char work_restart_id;
  847. uint32_t block_id;
  848. enum pool_protocol proto;
  849. char *hdr_path;
  850. char *lp_url;
  851. char *lp_id;
  852. enum pool_protocol lp_proto;
  853. curl_socket_t lp_socket;
  854. unsigned int getwork_requested;
  855. unsigned int stale_shares;
  856. unsigned int discarded_work;
  857. unsigned int getfail_occasions;
  858. unsigned int remotefail_occasions;
  859. struct timeval tv_idle;
  860. double utility;
  861. int last_shares, shares;
  862. char *rpc_url;
  863. char *rpc_userpass;
  864. char *rpc_user, *rpc_pass;
  865. char *rpc_proxy;
  866. pthread_mutex_t pool_lock;
  867. struct thread_q *submit_q;
  868. struct thread_q *getwork_q;
  869. pthread_t longpoll_thread;
  870. pthread_t submit_thread;
  871. pthread_t getwork_thread;
  872. int curls;
  873. pthread_cond_t cr_cond;
  874. struct list_head curlring;
  875. struct submit_work_state *sws_waiting_on_curl;
  876. time_t last_share_time;
  877. double last_share_diff;
  878. uint64_t best_diff;
  879. struct cgminer_stats cgminer_stats;
  880. struct cgminer_pool_stats cgminer_pool_stats;
  881. /* Stratum variables */
  882. char *stratum_url;
  883. char *stratum_port;
  884. CURL *stratum_curl;
  885. SOCKETTYPE sock;
  886. char *sockbuf;
  887. size_t sockbuf_size;
  888. char *sockaddr_url; /* stripped url used for sockaddr */
  889. char *nonce1;
  890. size_t n1_len;
  891. uint32_t nonce2;
  892. int n2size;
  893. char *sessionid;
  894. bool has_stratum;
  895. bool stratum_active;
  896. time_t last_work_time; /* only set for Stratum right now */
  897. bool stratum_auth;
  898. bool stratum_notify;
  899. struct stratum_work swork;
  900. pthread_t stratum_thread;
  901. pthread_mutex_t stratum_lock;
  902. char *admin_msg;
  903. pthread_mutex_t last_work_lock;
  904. struct work *last_work_copy;
  905. };
  906. #define GETWORK_MODE_TESTPOOL 'T'
  907. #define GETWORK_MODE_POOL 'P'
  908. #define GETWORK_MODE_LP 'L'
  909. #define GETWORK_MODE_BENCHMARK 'B'
  910. #define GETWORK_MODE_STRATUM 'S'
  911. #define GETWORK_MODE_GBT 'G'
  912. struct work {
  913. unsigned char data[128];
  914. unsigned char midstate[32];
  915. unsigned char target[32];
  916. unsigned char hash[32];
  917. int rolls;
  918. dev_blk_ctx blk;
  919. struct thr_info *thr;
  920. int thr_id;
  921. struct pool *pool;
  922. struct timeval tv_staged;
  923. bool mined;
  924. bool clone;
  925. bool cloned;
  926. int rolltime;
  927. bool longpoll;
  928. bool stale;
  929. bool mandatory;
  930. bool block;
  931. bool queued;
  932. bool stratum;
  933. char *job_id;
  934. char *nonce2;
  935. char *ntime;
  936. double sdiff;
  937. char *nonce1;
  938. unsigned char work_restart_id;
  939. int id;
  940. UT_hash_handle hh;
  941. double work_difficulty;
  942. blktemplate_t *tmpl;
  943. int *tmpl_refcount;
  944. unsigned int dataid;
  945. struct timeval tv_getwork;
  946. struct timeval tv_getwork_reply;
  947. struct timeval tv_cloned;
  948. struct timeval tv_work_start;
  949. struct timeval tv_work_found;
  950. char getwork_mode;
  951. /* Used to queue shares in submit_waiting */
  952. struct list_head list;
  953. };
  954. extern void get_datestamp(char *, struct timeval *);
  955. enum test_nonce2_result {
  956. TNR_GOOD,
  957. TNR_HIGH,
  958. TNR_BAD,
  959. };
  960. extern enum test_nonce2_result _test_nonce2(struct work *, uint32_t nonce, bool checktarget);
  961. #define test_nonce(work, nonce, checktarget) (_test_nonce2(work, nonce, checktarget) == TNR_GOOD)
  962. #define test_nonce2(work, nonce) (_test_nonce2(work, nonce, true))
  963. extern void submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  964. extern bool abandon_work(struct work *, struct timeval *work_runtime, uint64_t hashes);
  965. extern void tailsprintf(char *f, const char *fmt, ...);
  966. extern void wlogprint(const char *f, ...);
  967. extern int curses_int(const char *query);
  968. extern char *curses_input(const char *query);
  969. extern void kill_work(void);
  970. extern int prioritize_pools(char *param, int *pid);
  971. extern void validate_pool_priorities(void);
  972. extern void switch_pools(struct pool *selected);
  973. extern void remove_pool(struct pool *pool);
  974. extern void write_config(FILE *fcfg);
  975. extern void zero_bestshare(void);
  976. extern void zero_stats(void);
  977. extern void default_save_file(char *filename);
  978. extern bool log_curses_only(int prio, const char *f, va_list ap) FORMAT_SYNTAX_CHECK(printf, 2, 0);
  979. extern void clear_logwin(void);
  980. extern bool pool_tclear(struct pool *pool, bool *var);
  981. extern struct thread_q *tq_new(void);
  982. extern void tq_free(struct thread_q *tq);
  983. extern bool tq_push(struct thread_q *tq, void *data);
  984. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  985. extern void tq_freeze(struct thread_q *tq);
  986. extern void tq_thaw(struct thread_q *tq);
  987. extern bool successful_connect;
  988. extern void adl(void);
  989. extern void clean_work(struct work *work);
  990. extern void free_work(struct work *work);
  991. extern void __copy_work(struct work *work, struct work *base_work);
  992. extern struct work *copy_work(struct work *base_work);
  993. enum api_data_type {
  994. API_ESCAPE,
  995. API_STRING,
  996. API_CONST,
  997. API_INT,
  998. API_UINT,
  999. API_UINT32,
  1000. API_UINT64,
  1001. API_DOUBLE,
  1002. API_ELAPSED,
  1003. API_BOOL,
  1004. API_TIMEVAL,
  1005. API_TIME,
  1006. API_MHS,
  1007. API_MHTOTAL,
  1008. API_TEMP,
  1009. API_UTILITY,
  1010. API_FREQ,
  1011. API_VOLTS,
  1012. API_HS,
  1013. API_DIFF,
  1014. API_JSON,
  1015. };
  1016. struct api_data {
  1017. enum api_data_type type;
  1018. char *name;
  1019. void *data;
  1020. bool data_was_malloc;
  1021. struct api_data *prev;
  1022. struct api_data *next;
  1023. };
  1024. extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
  1025. extern struct api_data *api_add_string(struct api_data *root, char *name, const char *data, bool copy_data);
  1026. extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
  1027. extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
  1028. extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
  1029. extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  1030. extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
  1031. extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
  1032. extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
  1033. extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
  1034. extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
  1035. extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
  1036. extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
  1037. extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
  1038. extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
  1039. extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
  1040. extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
  1041. extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
  1042. extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
  1043. extern struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data);
  1044. extern struct api_data *api_add_json(struct api_data *root, char *name, json_t *data, bool copy_data);
  1045. #endif /* __MINER_H__ */