miner.h 29 KB

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