miner.h 37 KB

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