miner.h 31 KB

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