miner.h 28 KB

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