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. #include <sys/types.h>
  15. #include <sys/socket.h>
  16. #include <netdb.h>
  17. #ifdef HAVE_OPENCL
  18. #ifdef __APPLE_CC__
  19. #include <OpenCL/opencl.h>
  20. #else
  21. #include <CL/cl.h>
  22. #endif
  23. #endif /* HAVE_OPENCL */
  24. #ifdef STDC_HEADERS
  25. # include <stdlib.h>
  26. # include <stddef.h>
  27. #else
  28. # ifdef HAVE_STDLIB_H
  29. # include <stdlib.h>
  30. # endif
  31. #endif
  32. #ifdef HAVE_ALLOCA_H
  33. # include <alloca.h>
  34. #elif defined __GNUC__
  35. # ifndef WIN32
  36. # define alloca __builtin_alloca
  37. # else
  38. # include <malloc.h>
  39. # endif
  40. #elif defined _AIX
  41. # define alloca __alloca
  42. #elif defined _MSC_VER
  43. # include <malloc.h>
  44. # define alloca _alloca
  45. #else
  46. # ifndef HAVE_ALLOCA
  47. # ifdef __cplusplus
  48. extern "C"
  49. # endif
  50. void *alloca (size_t);
  51. # endif
  52. #endif
  53. #ifdef __MINGW32__
  54. #include <windows.h>
  55. #include <io.h>
  56. static inline int fsync (int fd)
  57. {
  58. return (FlushFileBuffers ((HANDLE) _get_osfhandle (fd))) ? 0 : -1;
  59. }
  60. #ifndef EWOULDBLOCK
  61. # define EWOULDBLOCK EAGAIN
  62. #endif
  63. #ifndef MSG_DONTWAIT
  64. # define MSG_DONTWAIT 0x1000000
  65. #endif
  66. #endif /* __MINGW32__ */
  67. #if defined (__linux)
  68. #ifndef LINUX
  69. #define LINUX
  70. #endif
  71. #endif
  72. #ifdef WIN32
  73. #ifndef timersub
  74. #define timersub(a, b, result) \
  75. do { \
  76. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  77. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  78. if ((result)->tv_usec < 0) { \
  79. --(result)->tv_sec; \
  80. (result)->tv_usec += 1000000; \
  81. } \
  82. } while (0)
  83. #endif
  84. #ifndef timeradd
  85. # define timeradd(a, b, result) \
  86. do { \
  87. (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
  88. (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
  89. if ((result)->tv_usec >= 1000000) \
  90. { \
  91. ++(result)->tv_sec; \
  92. (result)->tv_usec -= 1000000; \
  93. } \
  94. } while (0)
  95. #endif
  96. #endif
  97. #ifdef HAVE_ADL
  98. #include "ADL_SDK/adl_sdk.h"
  99. #endif
  100. #ifdef HAVE_LIBUSB
  101. #include <libusb.h>
  102. #endif
  103. #ifdef USE_ZTEX
  104. #include "libztex.h"
  105. #endif
  106. #ifdef USE_USBUTILS
  107. #include "usbutils.h"
  108. #endif
  109. #if (!defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))) \
  110. || (defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)))
  111. #ifndef bswap_16
  112. #define bswap_16 __builtin_bswap16
  113. #define bswap_32 __builtin_bswap32
  114. #define bswap_64 __builtin_bswap64
  115. #endif
  116. #else
  117. #if HAVE_BYTESWAP_H
  118. #include <byteswap.h>
  119. #elif defined(USE_SYS_ENDIAN_H)
  120. #include <sys/endian.h>
  121. #elif defined(__APPLE__)
  122. #include <libkern/OSByteOrder.h>
  123. #define bswap_16 OSSwapInt16
  124. #define bswap_32 OSSwapInt32
  125. #define bswap_64 OSSwapInt64
  126. #else
  127. #define bswap_16(value) \
  128. ((((value) & 0xff) << 8) | ((value) >> 8))
  129. #define bswap_32(value) \
  130. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  131. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  132. #define bswap_64(value) \
  133. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  134. << 32) | \
  135. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  136. #endif
  137. #endif /* !defined(__GLXBYTEORDER_H__) */
  138. /* This assumes htobe32 is a macro in endian.h, and if it doesn't exist, then
  139. * htobe64 also won't exist */
  140. #ifndef htobe32
  141. # if __BYTE_ORDER == __LITTLE_ENDIAN
  142. # define htole16(x) (x)
  143. # define htole32(x) (x)
  144. # define le32toh(x) (x)
  145. # define be32toh(x) bswap_32(x)
  146. # define be64toh(x) bswap_64(x)
  147. # define htobe32(x) bswap_32(x)
  148. # define htobe64(x) bswap_64(x)
  149. # elif __BYTE_ORDER == __BIG_ENDIAN
  150. # define htole16(x) bswap_16(x)
  151. # define htole32(x) bswap_32(x)
  152. # define le32toh(x) bswap_32(x)
  153. # define be32toh(x) (x)
  154. # define be64toh(x) (x)
  155. # define htobe32(x) (x)
  156. # define htobe64(x) (x)
  157. #else
  158. #error UNKNOWN BYTE ORDER
  159. #endif
  160. #endif
  161. #undef unlikely
  162. #undef likely
  163. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  164. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  165. #define likely(expr) (__builtin_expect(!!(expr), 1))
  166. #else
  167. #define unlikely(expr) (expr)
  168. #define likely(expr) (expr)
  169. #endif
  170. #define __maybe_unused __attribute__((unused))
  171. #define uninitialised_var(x) x = x
  172. #if defined(__i386__)
  173. #define WANT_CRYPTOPP_ASM32
  174. #endif
  175. #ifndef ARRAY_SIZE
  176. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  177. #endif
  178. #ifdef MIPSEB
  179. #ifndef roundl
  180. #define roundl(x) (long double)((long long)((x==0)?0.0:((x)+((x)>0)?0.5:-0.5)))
  181. #endif
  182. #endif
  183. #define MIN(x, y) ((x) > (y) ? (y) : (x))
  184. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  185. enum drv_driver {
  186. DRIVER_OPENCL = 0,
  187. DRIVER_ICARUS,
  188. DRIVER_BITFORCE,
  189. DRIVER_MODMINER,
  190. DRIVER_ZTEX,
  191. DRIVER_BFLSC,
  192. DRIVER_AVALON,
  193. DRIVER_MAX
  194. };
  195. enum alive {
  196. LIFE_WELL,
  197. LIFE_SICK,
  198. LIFE_DEAD,
  199. LIFE_NOSTART,
  200. LIFE_INIT,
  201. };
  202. enum pool_strategy {
  203. POOL_FAILOVER,
  204. POOL_ROUNDROBIN,
  205. POOL_ROTATE,
  206. POOL_LOADBALANCE,
  207. POOL_BALANCE,
  208. };
  209. #define TOP_STRATEGY (POOL_BALANCE)
  210. struct strategies {
  211. const char *s;
  212. };
  213. struct cgpu_info;
  214. #ifdef HAVE_ADL
  215. struct gpu_adl {
  216. ADLTemperature lpTemperature;
  217. int iAdapterIndex;
  218. int lpAdapterID;
  219. int iBusNumber;
  220. char strAdapterName[256];
  221. ADLPMActivity lpActivity;
  222. ADLODParameters lpOdParameters;
  223. ADLODPerformanceLevels *DefPerfLev;
  224. ADLFanSpeedInfo lpFanSpeedInfo;
  225. ADLFanSpeedValue lpFanSpeedValue;
  226. ADLFanSpeedValue DefFanSpeedValue;
  227. int iEngineClock;
  228. int iMemoryClock;
  229. int iVddc;
  230. int iPercentage;
  231. bool autofan;
  232. bool autoengine;
  233. bool managed; /* Were the values ever changed on this card */
  234. int lastengine;
  235. int lasttemp;
  236. int targetfan;
  237. int targettemp;
  238. int overtemp;
  239. int minspeed;
  240. int maxspeed;
  241. int gpu;
  242. bool has_fanspeed;
  243. struct gpu_adl *twin;
  244. };
  245. #endif
  246. extern void blank_get_statline_before(char *buf, struct cgpu_info __maybe_unused *cgpu);
  247. struct api_data;
  248. struct thr_info;
  249. struct work;
  250. struct device_drv {
  251. enum drv_driver drv_id;
  252. char *dname;
  253. char *name;
  254. // DRV-global functions
  255. void (*drv_detect)();
  256. // Device-specific functions
  257. void (*reinit_device)(struct cgpu_info *);
  258. void (*get_statline_before)(char *, struct cgpu_info *);
  259. void (*get_statline)(char *, struct cgpu_info *);
  260. struct api_data *(*get_api_stats)(struct cgpu_info *);
  261. bool (*get_stats)(struct cgpu_info *);
  262. void (*identify_device)(struct cgpu_info *); // e.g. to flash a led
  263. char *(*set_device)(struct cgpu_info *, char *option, char *setting, char *replybuf);
  264. // Thread-specific functions
  265. bool (*thread_prepare)(struct thr_info *);
  266. uint64_t (*can_limit_work)(struct thr_info *);
  267. bool (*thread_init)(struct thr_info *);
  268. bool (*prepare_work)(struct thr_info *, struct work *);
  269. /* Which hash work loop this driver uses. */
  270. void (*hash_work)(struct thr_info *);
  271. /* Two variants depending on whether the device divides work up into
  272. * small pieces or works with whole work items and may or may not have
  273. * a queue of its own. */
  274. int64_t (*scanhash)(struct thr_info *, struct work *, int64_t);
  275. int64_t (*scanwork)(struct thr_info *);
  276. /* Used to extract work from the hash table of queued work and tell
  277. * the main loop that it should not add any further work to the table.
  278. */
  279. bool (*queue_full)(struct cgpu_info *);
  280. void (*flush_work)(struct cgpu_info *);
  281. void (*hw_error)(struct thr_info *);
  282. void (*thread_shutdown)(struct thr_info *);
  283. void (*thread_enable)(struct thr_info *);
  284. // Does it need to be free()d?
  285. bool copy;
  286. /* Highest target diff the device supports */
  287. double max_diff;
  288. };
  289. extern struct device_drv *copy_drv(struct device_drv*);
  290. enum dev_enable {
  291. DEV_ENABLED,
  292. DEV_DISABLED,
  293. DEV_RECOVER,
  294. };
  295. enum cl_kernels {
  296. KL_NONE,
  297. KL_POCLBM,
  298. KL_PHATK,
  299. KL_DIAKGCN,
  300. KL_DIABLO,
  301. KL_SCRYPT,
  302. };
  303. enum dev_reason {
  304. REASON_THREAD_FAIL_INIT,
  305. REASON_THREAD_ZERO_HASH,
  306. REASON_THREAD_FAIL_QUEUE,
  307. REASON_DEV_SICK_IDLE_60,
  308. REASON_DEV_DEAD_IDLE_600,
  309. REASON_DEV_NOSTART,
  310. REASON_DEV_OVER_HEAT,
  311. REASON_DEV_THERMAL_CUTOFF,
  312. REASON_DEV_COMMS_ERROR,
  313. REASON_DEV_THROTTLE,
  314. };
  315. #define REASON_NONE "None"
  316. #define REASON_THREAD_FAIL_INIT_STR "Thread failed to init"
  317. #define REASON_THREAD_ZERO_HASH_STR "Thread got zero hashes"
  318. #define REASON_THREAD_FAIL_QUEUE_STR "Thread failed to queue work"
  319. #define REASON_DEV_SICK_IDLE_60_STR "Device idle for 60s"
  320. #define REASON_DEV_DEAD_IDLE_600_STR "Device dead - idle for 600s"
  321. #define REASON_DEV_NOSTART_STR "Device failed to start"
  322. #define REASON_DEV_OVER_HEAT_STR "Device over heated"
  323. #define REASON_DEV_THERMAL_CUTOFF_STR "Device reached thermal cutoff"
  324. #define REASON_DEV_COMMS_ERROR_STR "Device comms error"
  325. #define REASON_DEV_THROTTLE_STR "Device throttle"
  326. #define REASON_UNKNOWN_STR "Unknown reason - code bug"
  327. #define MIN_SEC_UNSET 99999999
  328. struct cgminer_stats {
  329. uint32_t getwork_calls;
  330. struct timeval getwork_wait;
  331. struct timeval getwork_wait_max;
  332. struct timeval getwork_wait_min;
  333. };
  334. // Just the actual network getworks to the pool
  335. struct cgminer_pool_stats {
  336. uint32_t getwork_calls;
  337. uint32_t getwork_attempts;
  338. struct timeval getwork_wait;
  339. struct timeval getwork_wait_max;
  340. struct timeval getwork_wait_min;
  341. double getwork_wait_rolling;
  342. bool hadrolltime;
  343. bool canroll;
  344. bool hadexpire;
  345. uint32_t rolltime;
  346. double min_diff;
  347. double max_diff;
  348. double last_diff;
  349. uint32_t min_diff_count;
  350. uint32_t max_diff_count;
  351. uint64_t times_sent;
  352. uint64_t bytes_sent;
  353. uint64_t net_bytes_sent;
  354. uint64_t times_received;
  355. uint64_t bytes_received;
  356. uint64_t net_bytes_received;
  357. };
  358. struct cgpu_info {
  359. int cgminer_id;
  360. struct device_drv *drv;
  361. int device_id;
  362. char *name;
  363. char *device_path;
  364. FILE *device_file;
  365. union {
  366. #ifdef USE_ZTEX
  367. struct libztex_device *device_ztex;
  368. #endif
  369. #ifdef USE_USBUTILS
  370. struct cg_usb_device *usbdev;
  371. #endif
  372. #if defined(USE_ICARUS) || defined(USE_AVALON)
  373. int device_fd;
  374. #endif
  375. };
  376. #ifdef USE_AVALON
  377. struct work **works;
  378. int work_array;
  379. int queued;
  380. int results;
  381. #endif
  382. #ifdef USE_USBUTILS
  383. struct cg_usb_info usbinfo;
  384. #endif
  385. #ifdef USE_MODMINER
  386. char fpgaid;
  387. unsigned char clock;
  388. pthread_mutex_t *modminer_mutex;
  389. #endif
  390. #ifdef USE_BITFORCE
  391. struct timeval work_start_tv;
  392. unsigned int wait_ms;
  393. unsigned int sleep_ms;
  394. double avg_wait_f;
  395. unsigned int avg_wait_d;
  396. uint32_t nonces;
  397. bool nonce_range;
  398. bool polling;
  399. bool flash_led;
  400. #endif /* USE_BITFORCE */
  401. #if defined(USE_BITFORCE) || defined(USE_BFLSC)
  402. pthread_mutex_t device_mutex;
  403. #endif /* USE_BITFORCE || USE_BFLSC */
  404. enum dev_enable deven;
  405. int accepted;
  406. int rejected;
  407. int hw_errors;
  408. double rolling;
  409. double total_mhashes;
  410. double utility;
  411. enum alive status;
  412. char init[40];
  413. struct timeval last_message_tv;
  414. int threads;
  415. struct thr_info **thr;
  416. int64_t max_hashes;
  417. const char *kname;
  418. #ifdef HAVE_OPENCL
  419. bool mapped;
  420. int virtual_gpu;
  421. int virtual_adl;
  422. int intensity;
  423. bool dynamic;
  424. cl_uint vwidth;
  425. size_t work_size;
  426. enum cl_kernels kernel;
  427. cl_ulong max_alloc;
  428. #ifdef USE_SCRYPT
  429. int opt_lg, lookup_gap;
  430. size_t opt_tc, thread_concurrency;
  431. size_t shaders;
  432. #endif
  433. struct timeval tv_gpustart;
  434. int intervals;
  435. #endif
  436. bool new_work;
  437. float temp;
  438. int cutofftemp;
  439. #ifdef HAVE_ADL
  440. bool has_adl;
  441. struct gpu_adl adl;
  442. int gpu_engine;
  443. int min_engine;
  444. int gpu_fan;
  445. int min_fan;
  446. int gpu_memclock;
  447. int gpu_memdiff;
  448. int gpu_powertune;
  449. float gpu_vddc;
  450. #endif
  451. int diff1;
  452. double diff_accepted;
  453. double diff_rejected;
  454. int last_share_pool;
  455. time_t last_share_pool_time;
  456. double last_share_diff;
  457. time_t last_device_valid_work;
  458. time_t device_last_well;
  459. time_t device_last_not_well;
  460. enum dev_reason device_not_well_reason;
  461. int thread_fail_init_count;
  462. int thread_zero_hash_count;
  463. int thread_fail_queue_count;
  464. int dev_sick_idle_60_count;
  465. int dev_dead_idle_600_count;
  466. int dev_nostart_count;
  467. int dev_over_heat_count; // It's a warning but worth knowing
  468. int dev_thermal_cutoff_count;
  469. int dev_comms_error_count;
  470. int dev_throttle_count;
  471. struct cgminer_stats cgminer_stats;
  472. pthread_rwlock_t qlock;
  473. struct work *queued_work;
  474. unsigned int queued_count;
  475. };
  476. extern bool add_cgpu(struct cgpu_info*);
  477. struct thread_q {
  478. struct list_head q;
  479. bool frozen;
  480. pthread_mutex_t mutex;
  481. pthread_cond_t cond;
  482. };
  483. struct thr_info {
  484. int id;
  485. int device_thread;
  486. bool primary_thread;
  487. pthread_t pth;
  488. struct thread_q *q;
  489. struct cgpu_info *cgpu;
  490. void *cgpu_data;
  491. struct timeval last;
  492. struct timeval sick;
  493. bool pause;
  494. bool getwork;
  495. double rolling;
  496. bool work_restart;
  497. };
  498. struct string_elist {
  499. char *string;
  500. bool free_me;
  501. struct list_head list;
  502. };
  503. static inline void string_elist_add(const char *s, struct list_head *head)
  504. {
  505. struct string_elist *n;
  506. n = calloc(1, sizeof(*n));
  507. n->string = strdup(s);
  508. n->free_me = true;
  509. list_add_tail(&n->list, head);
  510. }
  511. static inline void string_elist_del(struct string_elist *item)
  512. {
  513. if (item->free_me)
  514. free(item->string);
  515. list_del(&item->list);
  516. }
  517. static inline uint32_t swab32(uint32_t v)
  518. {
  519. return bswap_32(v);
  520. }
  521. static inline void swap256(void *dest_p, const void *src_p)
  522. {
  523. uint32_t *dest = dest_p;
  524. const uint32_t *src = src_p;
  525. dest[0] = src[7];
  526. dest[1] = src[6];
  527. dest[2] = src[5];
  528. dest[3] = src[4];
  529. dest[4] = src[3];
  530. dest[5] = src[2];
  531. dest[6] = src[1];
  532. dest[7] = src[0];
  533. }
  534. static inline void swab256(void *dest_p, const void *src_p)
  535. {
  536. uint32_t *dest = dest_p;
  537. const uint32_t *src = src_p;
  538. dest[0] = swab32(src[7]);
  539. dest[1] = swab32(src[6]);
  540. dest[2] = swab32(src[5]);
  541. dest[3] = swab32(src[4]);
  542. dest[4] = swab32(src[3]);
  543. dest[5] = swab32(src[2]);
  544. dest[6] = swab32(src[1]);
  545. dest[7] = swab32(src[0]);
  546. }
  547. static inline void flip32(void *dest_p, const void *src_p)
  548. {
  549. uint32_t *dest = dest_p;
  550. const uint32_t *src = src_p;
  551. int i;
  552. for (i = 0; i < 8; i++)
  553. dest[i] = swab32(src[i]);
  554. }
  555. static inline void flip64(void *dest_p, const void *src_p)
  556. {
  557. uint32_t *dest = dest_p;
  558. const uint32_t *src = src_p;
  559. int i;
  560. for (i = 0; i < 16; i++)
  561. dest[i] = swab32(src[i]);
  562. }
  563. static inline void flip80(void *dest_p, const void *src_p)
  564. {
  565. uint32_t *dest = dest_p;
  566. const uint32_t *src = src_p;
  567. int i;
  568. for (i = 0; i < 20; i++)
  569. dest[i] = swab32(src[i]);
  570. }
  571. static inline void flip128(void *dest_p, const void *src_p)
  572. {
  573. uint32_t *dest = dest_p;
  574. const uint32_t *src = src_p;
  575. int i;
  576. for (i = 0; i < 32; i++)
  577. dest[i] = swab32(src[i]);
  578. }
  579. /* For flipping to the correct endianness if necessary */
  580. #if defined(__BIG_ENDIAN__) || defined(MIPSEB)
  581. static inline void endian_flip32(void *dest_p, const void *src_p)
  582. {
  583. flip32(dest_p, src_p);
  584. }
  585. static inline void endian_flip128(void *dest_p, const void *src_p)
  586. {
  587. flip128(dest_p, src_p);
  588. }
  589. #else
  590. static inline void
  591. endian_flip32(void __maybe_unused *dest_p, const void __maybe_unused *src_p)
  592. {
  593. }
  594. static inline void
  595. endian_flip128(void __maybe_unused *dest_p, const void __maybe_unused *src_p)
  596. {
  597. }
  598. #endif
  599. extern void quit(int status, const char *format, ...);
  600. static inline void mutex_lock(pthread_mutex_t *lock)
  601. {
  602. if (unlikely(pthread_mutex_lock(lock)))
  603. quit(1, "WTF MUTEX ERROR ON LOCK!");
  604. }
  605. static inline void mutex_unlock(pthread_mutex_t *lock)
  606. {
  607. if (unlikely(pthread_mutex_unlock(lock)))
  608. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  609. }
  610. static inline int mutex_trylock(pthread_mutex_t *lock)
  611. {
  612. return pthread_mutex_trylock(lock);
  613. }
  614. static inline void wr_lock(pthread_rwlock_t *lock)
  615. {
  616. if (unlikely(pthread_rwlock_wrlock(lock)))
  617. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  618. }
  619. static inline void rd_lock(pthread_rwlock_t *lock)
  620. {
  621. if (unlikely(pthread_rwlock_rdlock(lock)))
  622. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  623. }
  624. static inline void rw_unlock(pthread_rwlock_t *lock)
  625. {
  626. if (unlikely(pthread_rwlock_unlock(lock)))
  627. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  628. }
  629. static inline void rd_unlock(pthread_rwlock_t *lock)
  630. {
  631. rw_unlock(lock);
  632. }
  633. static inline void wr_unlock(pthread_rwlock_t *lock)
  634. {
  635. rw_unlock(lock);
  636. }
  637. static inline void mutex_init(pthread_mutex_t *lock)
  638. {
  639. if (unlikely(pthread_mutex_init(lock, NULL)))
  640. quit(1, "Failed to pthread_mutex_init");
  641. }
  642. static inline void rwlock_init(pthread_rwlock_t *lock)
  643. {
  644. if (unlikely(pthread_rwlock_init(lock, NULL)))
  645. quit(1, "Failed to pthread_rwlock_init");
  646. }
  647. /* cgminer locks, a write biased variant of rwlocks */
  648. struct cglock {
  649. pthread_mutex_t mutex;
  650. pthread_rwlock_t rwlock;
  651. };
  652. typedef struct cglock cglock_t;
  653. static inline void cglock_init(cglock_t *lock)
  654. {
  655. mutex_init(&lock->mutex);
  656. rwlock_init(&lock->rwlock);
  657. }
  658. /* Read lock variant of cglock */
  659. static inline void cg_rlock(cglock_t *lock)
  660. {
  661. mutex_lock(&lock->mutex);
  662. rd_lock(&lock->rwlock);
  663. mutex_unlock(&lock->mutex);
  664. }
  665. /* Intermediate variant of cglock */
  666. static inline void cg_ilock(cglock_t *lock)
  667. {
  668. mutex_lock(&lock->mutex);
  669. }
  670. /* Upgrade intermediate variant to a write lock */
  671. static inline void cg_ulock(cglock_t *lock)
  672. {
  673. wr_lock(&lock->rwlock);
  674. }
  675. /* Write lock variant of cglock */
  676. static inline void cg_wlock(cglock_t *lock)
  677. {
  678. mutex_lock(&lock->mutex);
  679. wr_lock(&lock->rwlock);
  680. }
  681. /* Downgrade intermediate variant to a read lock */
  682. static inline void cg_dlock(cglock_t *lock)
  683. {
  684. rd_lock(&lock->rwlock);
  685. mutex_unlock(&lock->mutex);
  686. }
  687. static inline void cg_runlock(cglock_t *lock)
  688. {
  689. rd_unlock(&lock->rwlock);
  690. }
  691. static inline void cg_wunlock(cglock_t *lock)
  692. {
  693. wr_unlock(&lock->rwlock);
  694. mutex_unlock(&lock->mutex);
  695. }
  696. struct pool;
  697. extern bool opt_protocol;
  698. extern bool have_longpoll;
  699. extern char *opt_kernel_path;
  700. extern char *opt_socks_proxy;
  701. extern char *cgminer_path;
  702. extern bool opt_fail_only;
  703. extern bool opt_autofan;
  704. extern bool opt_autoengine;
  705. extern bool use_curses;
  706. extern char *opt_api_allow;
  707. extern char *opt_api_groups;
  708. extern char *opt_api_description;
  709. extern int opt_api_port;
  710. extern bool opt_api_listen;
  711. extern bool opt_api_network;
  712. extern bool opt_delaynet;
  713. extern bool opt_restart;
  714. extern char *opt_icarus_options;
  715. extern char *opt_icarus_timing;
  716. extern bool opt_worktime;
  717. #ifdef USE_AVALON
  718. extern char *opt_avalon_options;
  719. #endif
  720. #ifdef USE_USBUTILS
  721. extern char *opt_usb_select;
  722. extern int opt_usbdump;
  723. extern bool opt_usb_list_all;
  724. #endif
  725. #ifdef USE_BITFORCE
  726. extern bool opt_bfl_noncerange;
  727. #endif
  728. extern bool ping;
  729. extern int swork_id;
  730. extern pthread_rwlock_t netacc_lock;
  731. extern const uint32_t sha256_init_state[];
  732. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  733. const char *rpc_req, bool, bool, int *,
  734. struct pool *pool, bool);
  735. extern const char *proxytype(curl_proxytype proxytype);
  736. extern char *get_proxy(char *url, struct pool *pool);
  737. extern char *bin2hex(const unsigned char *p, size_t len);
  738. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  739. typedef bool (*sha256_func)(struct thr_info*, const unsigned char *pmidstate,
  740. unsigned char *pdata,
  741. unsigned char *phash1, unsigned char *phash,
  742. const unsigned char *ptarget,
  743. uint32_t max_nonce,
  744. uint32_t *last_nonce,
  745. uint32_t nonce);
  746. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  747. extern int opt_queue;
  748. extern int opt_scantime;
  749. extern int opt_expiry;
  750. #ifdef USE_USBUTILS
  751. extern pthread_mutex_t cgusb_lock;
  752. #endif
  753. extern cglock_t control_lock;
  754. extern pthread_mutex_t hash_lock;
  755. extern pthread_mutex_t console_lock;
  756. extern cglock_t ch_lock;
  757. extern pthread_rwlock_t mining_thr_lock;
  758. extern pthread_rwlock_t devices_lock;
  759. extern pthread_mutex_t restart_lock;
  760. extern pthread_cond_t restart_cond;
  761. extern void thread_reportin(struct thr_info *thr);
  762. extern void clear_stratum_shares(struct pool *pool);
  763. extern int restart_wait(unsigned int mstime);
  764. extern void kill_work(void);
  765. extern void reinit_device(struct cgpu_info *cgpu);
  766. #ifdef HAVE_ADL
  767. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  768. extern int set_fanspeed(int gpu, int iFanSpeed);
  769. extern int set_vddc(int gpu, float fVddc);
  770. extern int set_engineclock(int gpu, int iEngineClock);
  771. extern int set_memoryclock(int gpu, int iMemoryClock);
  772. #endif
  773. extern void api(int thr_id);
  774. extern struct pool *current_pool(void);
  775. extern int enabled_pools;
  776. extern bool detect_stratum(struct pool *pool, char *url);
  777. extern void print_summary(void);
  778. extern struct pool *add_pool(void);
  779. extern bool add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
  780. #define MAX_GPUDEVICES 16
  781. #define MIN_INTENSITY -10
  782. #define _MIN_INTENSITY_STR "-10"
  783. #ifdef USE_SCRYPT
  784. #define MAX_INTENSITY 20
  785. #define _MAX_INTENSITY_STR "20"
  786. #else
  787. #define MAX_INTENSITY 14
  788. #define _MAX_INTENSITY_STR "14"
  789. #endif
  790. extern bool hotplug_mode;
  791. extern int hotplug_time;
  792. extern struct list_head scan_devices;
  793. extern int nDevs;
  794. extern int num_processors;
  795. extern int hw_errors;
  796. extern bool use_syslog;
  797. extern bool opt_quiet;
  798. extern struct thr_info *control_thr;
  799. extern struct thr_info **mining_thr;
  800. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  801. extern int gpu_threads;
  802. #ifdef USE_SCRYPT
  803. extern bool opt_scrypt;
  804. #else
  805. #define opt_scrypt (0)
  806. #endif
  807. extern double total_secs;
  808. extern int mining_threads;
  809. extern int total_devices;
  810. extern struct cgpu_info **devices;
  811. extern int total_pools;
  812. extern struct pool **pools;
  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. struct addrinfo stratum_hints;
  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__ */