miner.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  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. #endif
  419. #ifdef USE_MODMINER
  420. char fpgaid;
  421. unsigned char clock;
  422. pthread_mutex_t *modminer_mutex;
  423. #endif
  424. #ifdef USE_BITFORCE
  425. struct timeval work_start_tv;
  426. unsigned int wait_ms;
  427. unsigned int sleep_ms;
  428. double avg_wait_f;
  429. unsigned int avg_wait_d;
  430. uint32_t nonces;
  431. bool nonce_range;
  432. bool polling;
  433. bool flash_led;
  434. #endif /* USE_BITFORCE */
  435. #if defined(USE_BITFORCE) || defined(USE_BFLSC)
  436. pthread_mutex_t device_mutex;
  437. #endif /* USE_BITFORCE || USE_BFLSC */
  438. enum dev_enable deven;
  439. int accepted;
  440. int rejected;
  441. int hw_errors;
  442. double rolling;
  443. double total_mhashes;
  444. double utility;
  445. enum alive status;
  446. char init[40];
  447. struct timeval last_message_tv;
  448. int threads;
  449. struct thr_info **thr;
  450. int64_t max_hashes;
  451. const char *kname;
  452. #ifdef HAVE_OPENCL
  453. bool mapped;
  454. int virtual_gpu;
  455. int virtual_adl;
  456. int intensity;
  457. bool dynamic;
  458. cl_uint vwidth;
  459. size_t work_size;
  460. enum cl_kernels kernel;
  461. cl_ulong max_alloc;
  462. #ifdef USE_SCRYPT
  463. int opt_lg, lookup_gap;
  464. size_t opt_tc, thread_concurrency;
  465. size_t shaders;
  466. #endif
  467. struct timeval tv_gpustart;
  468. int intervals;
  469. #endif
  470. bool new_work;
  471. float temp;
  472. int cutofftemp;
  473. #ifdef HAVE_ADL
  474. bool has_adl;
  475. struct gpu_adl adl;
  476. int gpu_engine;
  477. int min_engine;
  478. int gpu_fan;
  479. int min_fan;
  480. int gpu_memclock;
  481. int gpu_memdiff;
  482. int gpu_powertune;
  483. float gpu_vddc;
  484. #endif
  485. int diff1;
  486. double diff_accepted;
  487. double diff_rejected;
  488. int last_share_pool;
  489. time_t last_share_pool_time;
  490. double last_share_diff;
  491. time_t last_device_valid_work;
  492. time_t device_last_well;
  493. time_t device_last_not_well;
  494. enum dev_reason device_not_well_reason;
  495. int thread_fail_init_count;
  496. int thread_zero_hash_count;
  497. int thread_fail_queue_count;
  498. int dev_sick_idle_60_count;
  499. int dev_dead_idle_600_count;
  500. int dev_nostart_count;
  501. int dev_over_heat_count; // It's a warning but worth knowing
  502. int dev_thermal_cutoff_count;
  503. int dev_comms_error_count;
  504. int dev_throttle_count;
  505. struct cgminer_stats cgminer_stats;
  506. pthread_rwlock_t qlock;
  507. struct work *queued_work;
  508. unsigned int queued_count;
  509. bool shutdown;
  510. struct timeval dev_start_tv;
  511. };
  512. extern bool add_cgpu(struct cgpu_info*);
  513. struct thread_q {
  514. struct list_head q;
  515. bool frozen;
  516. pthread_mutex_t mutex;
  517. pthread_cond_t cond;
  518. };
  519. struct thr_info {
  520. int id;
  521. int device_thread;
  522. bool primary_thread;
  523. pthread_t pth;
  524. cgsem_t sem;
  525. struct thread_q *q;
  526. struct cgpu_info *cgpu;
  527. void *cgpu_data;
  528. struct timeval last;
  529. struct timeval sick;
  530. bool pause;
  531. bool getwork;
  532. double rolling;
  533. bool work_restart;
  534. };
  535. struct string_elist {
  536. char *string;
  537. bool free_me;
  538. struct list_head list;
  539. };
  540. static inline void string_elist_add(const char *s, struct list_head *head)
  541. {
  542. struct string_elist *n;
  543. n = calloc(1, sizeof(*n));
  544. n->string = strdup(s);
  545. n->free_me = true;
  546. list_add_tail(&n->list, head);
  547. }
  548. static inline void string_elist_del(struct string_elist *item)
  549. {
  550. if (item->free_me)
  551. free(item->string);
  552. list_del(&item->list);
  553. }
  554. static inline uint32_t swab32(uint32_t v)
  555. {
  556. return bswap_32(v);
  557. }
  558. static inline void swap256(void *dest_p, const void *src_p)
  559. {
  560. uint32_t *dest = dest_p;
  561. const uint32_t *src = src_p;
  562. dest[0] = src[7];
  563. dest[1] = src[6];
  564. dest[2] = src[5];
  565. dest[3] = src[4];
  566. dest[4] = src[3];
  567. dest[5] = src[2];
  568. dest[6] = src[1];
  569. dest[7] = src[0];
  570. }
  571. static inline void swab256(void *dest_p, const void *src_p)
  572. {
  573. uint32_t *dest = dest_p;
  574. const uint32_t *src = src_p;
  575. dest[0] = swab32(src[7]);
  576. dest[1] = swab32(src[6]);
  577. dest[2] = swab32(src[5]);
  578. dest[3] = swab32(src[4]);
  579. dest[4] = swab32(src[3]);
  580. dest[5] = swab32(src[2]);
  581. dest[6] = swab32(src[1]);
  582. dest[7] = swab32(src[0]);
  583. }
  584. static inline void flip32(void *dest_p, const void *src_p)
  585. {
  586. uint32_t *dest = dest_p;
  587. const uint32_t *src = src_p;
  588. int i;
  589. for (i = 0; i < 8; i++)
  590. dest[i] = swab32(src[i]);
  591. }
  592. static inline void flip64(void *dest_p, const void *src_p)
  593. {
  594. uint32_t *dest = dest_p;
  595. const uint32_t *src = src_p;
  596. int i;
  597. for (i = 0; i < 16; i++)
  598. dest[i] = swab32(src[i]);
  599. }
  600. static inline void flip80(void *dest_p, const void *src_p)
  601. {
  602. uint32_t *dest = dest_p;
  603. const uint32_t *src = src_p;
  604. int i;
  605. for (i = 0; i < 20; i++)
  606. dest[i] = swab32(src[i]);
  607. }
  608. static inline void flip128(void *dest_p, const void *src_p)
  609. {
  610. uint32_t *dest = dest_p;
  611. const uint32_t *src = src_p;
  612. int i;
  613. for (i = 0; i < 32; i++)
  614. dest[i] = swab32(src[i]);
  615. }
  616. /* For flipping to the correct endianness if necessary */
  617. #if defined(__BIG_ENDIAN__) || defined(MIPSEB)
  618. static inline void endian_flip32(void *dest_p, const void *src_p)
  619. {
  620. flip32(dest_p, src_p);
  621. }
  622. static inline void endian_flip128(void *dest_p, const void *src_p)
  623. {
  624. flip128(dest_p, src_p);
  625. }
  626. #else
  627. static inline void
  628. endian_flip32(void __maybe_unused *dest_p, const void __maybe_unused *src_p)
  629. {
  630. }
  631. static inline void
  632. endian_flip128(void __maybe_unused *dest_p, const void __maybe_unused *src_p)
  633. {
  634. }
  635. #endif
  636. extern void _quit(int status);
  637. #define mutex_lock(_lock) _mutex_lock(_lock, __FILE__, __func__, __LINE__)
  638. #define mutex_unlock_noyield(_lock) _mutex_unlock_noyield(_lock, __FILE__, __func__, __LINE__)
  639. #define wr_lock(_lock) _wr_lock(_lock, __FILE__, __func__, __LINE__)
  640. #define rd_lock(_lock) _rd_lock(_lock, __FILE__, __func__, __LINE__)
  641. #define rw_unlock(_lock) _rw_unlock(_lock, __FILE__, __func__, __LINE__)
  642. #define mutex_init(_lock) _mutex_init(_lock, __FILE__, __func__, __LINE__)
  643. #define rwlock_init(_lock) _rwlock_init(_lock, __FILE__, __func__, __LINE__)
  644. static inline void _mutex_lock(pthread_mutex_t *lock, const char *file, const char *func, const int line)
  645. {
  646. if (unlikely(pthread_mutex_lock(lock)))
  647. quitfrom(1, file, func, line, "WTF MUTEX ERROR ON LOCK! errno=%d", errno);
  648. }
  649. static inline void _mutex_unlock_noyield(pthread_mutex_t *lock, const char *file, const char *func, const int line)
  650. {
  651. if (unlikely(pthread_mutex_unlock(lock)))
  652. quitfrom(1, file, func, line, "WTF MUTEX ERROR ON UNLOCK! errno=%d", errno);
  653. }
  654. static inline void mutex_unlock(pthread_mutex_t *lock)
  655. {
  656. mutex_unlock_noyield(lock);
  657. sched_yield();
  658. }
  659. static inline int mutex_trylock(pthread_mutex_t *lock)
  660. {
  661. return pthread_mutex_trylock(lock);
  662. }
  663. static inline void _wr_lock(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  664. {
  665. if (unlikely(pthread_rwlock_wrlock(lock)))
  666. quitfrom(1, file, func, line, "WTF WRLOCK ERROR ON LOCK! errno=%d", errno);
  667. }
  668. static inline void _rd_lock(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  669. {
  670. if (unlikely(pthread_rwlock_rdlock(lock)))
  671. quitfrom(1, file, func, line, "WTF RDLOCK ERROR ON LOCK! errno=%d", errno);
  672. }
  673. static inline void _rw_unlock(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  674. {
  675. if (unlikely(pthread_rwlock_unlock(lock)))
  676. quitfrom(1, file, func, line, "WTF RWLOCK ERROR ON UNLOCK! errno=%d", errno);
  677. }
  678. static inline void rd_unlock_noyield(pthread_rwlock_t *lock)
  679. {
  680. rw_unlock(lock);
  681. }
  682. static inline void wr_unlock_noyield(pthread_rwlock_t *lock)
  683. {
  684. rw_unlock(lock);
  685. }
  686. static inline void rd_unlock(pthread_rwlock_t *lock)
  687. {
  688. rw_unlock(lock);
  689. sched_yield();
  690. }
  691. static inline void wr_unlock(pthread_rwlock_t *lock)
  692. {
  693. rw_unlock(lock);
  694. sched_yield();
  695. }
  696. static inline void _mutex_init(pthread_mutex_t *lock, const char *file, const char *func, const int line)
  697. {
  698. if (unlikely(pthread_mutex_init(lock, NULL)))
  699. quitfrom(1, file, func, line, "Failed to pthread_mutex_init errno=%d", errno);
  700. }
  701. static inline void _rwlock_init(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  702. {
  703. if (unlikely(pthread_rwlock_init(lock, NULL)))
  704. quitfrom(1, file, func, line, "Failed to pthread_rwlock_init errno=%d", errno);
  705. }
  706. /* cgminer locks, a write biased variant of rwlocks */
  707. struct cglock {
  708. pthread_mutex_t mutex;
  709. pthread_rwlock_t rwlock;
  710. };
  711. typedef struct cglock cglock_t;
  712. static inline void cglock_init(cglock_t *lock)
  713. {
  714. mutex_init(&lock->mutex);
  715. rwlock_init(&lock->rwlock);
  716. }
  717. /* Read lock variant of cglock. Cannot be promoted. */
  718. static inline void cg_rlock(cglock_t *lock)
  719. {
  720. mutex_lock(&lock->mutex);
  721. rd_lock(&lock->rwlock);
  722. mutex_unlock_noyield(&lock->mutex);
  723. }
  724. /* Intermediate variant of cglock - behaves as a read lock but can be promoted
  725. * to a write lock or demoted to read lock. */
  726. static inline void cg_ilock(cglock_t *lock)
  727. {
  728. mutex_lock(&lock->mutex);
  729. }
  730. /* Upgrade intermediate variant to a write lock */
  731. static inline void cg_ulock(cglock_t *lock)
  732. {
  733. wr_lock(&lock->rwlock);
  734. }
  735. /* Write lock variant of cglock */
  736. static inline void cg_wlock(cglock_t *lock)
  737. {
  738. mutex_lock(&lock->mutex);
  739. wr_lock(&lock->rwlock);
  740. }
  741. /* Downgrade write variant to a read lock */
  742. static inline void cg_dwlock(cglock_t *lock)
  743. {
  744. wr_unlock_noyield(&lock->rwlock);
  745. rd_lock(&lock->rwlock);
  746. mutex_unlock_noyield(&lock->mutex);
  747. }
  748. /* Demote a write variant to an intermediate variant */
  749. static inline void cg_dwilock(cglock_t *lock)
  750. {
  751. wr_unlock(&lock->rwlock);
  752. }
  753. /* Downgrade intermediate variant to a read lock */
  754. static inline void cg_dlock(cglock_t *lock)
  755. {
  756. rd_lock(&lock->rwlock);
  757. mutex_unlock_noyield(&lock->mutex);
  758. }
  759. static inline void cg_runlock(cglock_t *lock)
  760. {
  761. rd_unlock(&lock->rwlock);
  762. }
  763. static inline void cg_wunlock(cglock_t *lock)
  764. {
  765. wr_unlock_noyield(&lock->rwlock);
  766. mutex_unlock(&lock->mutex);
  767. }
  768. struct pool;
  769. #define API_MCAST_CODE "FTW"
  770. #define API_MCAST_ADDR "224.0.0.75"
  771. extern bool opt_protocol;
  772. extern bool have_longpoll;
  773. extern char *opt_kernel_path;
  774. extern char *opt_socks_proxy;
  775. extern char *cgminer_path;
  776. extern bool opt_fail_only;
  777. extern bool opt_autofan;
  778. extern bool opt_autoengine;
  779. extern bool use_curses;
  780. extern char *opt_api_allow;
  781. extern bool opt_api_mcast;
  782. extern char *opt_api_mcast_addr;
  783. extern char *opt_api_mcast_code;
  784. extern char *opt_api_mcast_des;
  785. extern int opt_api_mcast_port;
  786. extern char *opt_api_groups;
  787. extern char *opt_api_description;
  788. extern int opt_api_port;
  789. extern bool opt_api_listen;
  790. extern bool opt_api_network;
  791. extern bool opt_delaynet;
  792. extern bool opt_restart;
  793. extern bool opt_nogpu;
  794. extern char *opt_icarus_options;
  795. extern char *opt_icarus_timing;
  796. extern bool opt_worktime;
  797. #ifdef USE_AVALON
  798. extern char *opt_avalon_options;
  799. #endif
  800. #ifdef USE_USBUTILS
  801. extern char *opt_usb_select;
  802. extern int opt_usbdump;
  803. extern bool opt_usb_list_all;
  804. extern cgsem_t usb_resource_sem;
  805. #endif
  806. #ifdef USE_BITFORCE
  807. extern bool opt_bfl_noncerange;
  808. #endif
  809. extern int swork_id;
  810. extern pthread_rwlock_t netacc_lock;
  811. extern const uint32_t sha256_init_state[];
  812. #ifdef HAVE_LIBCURL
  813. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  814. const char *rpc_req, bool, bool, int *,
  815. struct pool *pool, bool);
  816. #endif
  817. extern const char *proxytype(proxytypes_t proxytype);
  818. extern char *get_proxy(char *url, struct pool *pool);
  819. extern char *bin2hex(const unsigned char *p, size_t len);
  820. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  821. typedef bool (*sha256_func)(struct thr_info*, const unsigned char *pmidstate,
  822. unsigned char *pdata,
  823. unsigned char *phash1, unsigned char *phash,
  824. const unsigned char *ptarget,
  825. uint32_t max_nonce,
  826. uint32_t *last_nonce,
  827. uint32_t nonce);
  828. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  829. extern int opt_queue;
  830. extern int opt_scantime;
  831. extern int opt_expiry;
  832. #ifdef USE_USBUTILS
  833. extern pthread_mutex_t cgusb_lock;
  834. extern pthread_mutex_t cgusbres_lock;
  835. extern cglock_t cgusb_fd_lock;
  836. #endif
  837. extern cglock_t control_lock;
  838. extern pthread_mutex_t hash_lock;
  839. extern pthread_mutex_t console_lock;
  840. extern cglock_t ch_lock;
  841. extern pthread_rwlock_t mining_thr_lock;
  842. extern pthread_rwlock_t devices_lock;
  843. extern pthread_mutex_t restart_lock;
  844. extern pthread_cond_t restart_cond;
  845. extern void clear_stratum_shares(struct pool *pool);
  846. extern void set_target(unsigned char *dest_target, double diff);
  847. extern int restart_wait(struct thr_info *thr, unsigned int mstime);
  848. extern void kill_work(void);
  849. extern void reinit_device(struct cgpu_info *cgpu);
  850. #ifdef HAVE_ADL
  851. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  852. extern int set_fanspeed(int gpu, int iFanSpeed);
  853. extern int set_vddc(int gpu, float fVddc);
  854. extern int set_engineclock(int gpu, int iEngineClock);
  855. extern int set_memoryclock(int gpu, int iMemoryClock);
  856. #endif
  857. extern void api(int thr_id);
  858. extern struct pool *current_pool(void);
  859. extern int enabled_pools;
  860. extern void get_intrange(char *arg, int *val1, int *val2);
  861. extern bool detect_stratum(struct pool *pool, char *url);
  862. extern void print_summary(void);
  863. extern void adjust_quota_gcd(void);
  864. extern struct pool *add_pool(void);
  865. extern bool add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
  866. #define MAX_GPUDEVICES 16
  867. #define MAX_DEVICES 4096
  868. #define MIN_SHA_INTENSITY -10
  869. #define MIN_SHA_INTENSITY_STR "-10"
  870. #define MAX_SHA_INTENSITY 14
  871. #define MAX_SHA_INTENSITY_STR "14"
  872. #define MIN_SCRYPT_INTENSITY 8
  873. #define MIN_SCRYPT_INTENSITY_STR "8"
  874. #define MAX_SCRYPT_INTENSITY 20
  875. #define MAX_SCRYPT_INTENSITY_STR "20"
  876. #ifdef USE_SCRYPT
  877. #define MIN_INTENSITY (opt_scrypt ? MIN_SCRYPT_INTENSITY : MIN_SHA_INTENSITY)
  878. #define MIN_INTENSITY_STR (opt_scrypt ? MIN_SCRYPT_INTENSITY_STR : MIN_SHA_INTENSITY_STR)
  879. #define MAX_INTENSITY (opt_scrypt ? MAX_SCRYPT_INTENSITY : MAX_SHA_INTENSITY)
  880. #define MAX_INTENSITY_STR (opt_scrypt ? MAX_SCRYPT_INTENSITY_STR : MAX_SHA_INTENSITY_STR)
  881. #define MAX_GPU_INTENSITY MAX_SCRYPT_INTENSITY
  882. #else
  883. #define MIN_INTENSITY MIN_SHA_INTENSITY
  884. #define MIN_INTENSITY_STR MIN_SHA_INTENSITY_STR
  885. #define MAX_INTENSITY MAX_SHA_INTENSITY
  886. #define MAX_INTENSITY_STR MAX_SHA_INTENSITY_STR
  887. #define MAX_GPU_INTENSITY MAX_SHA_INTENSITY
  888. #endif
  889. extern bool hotplug_mode;
  890. extern int hotplug_time;
  891. extern struct list_head scan_devices;
  892. extern int nDevs;
  893. extern int num_processors;
  894. extern int hw_errors;
  895. extern bool use_syslog;
  896. extern bool opt_quiet;
  897. extern struct thr_info *control_thr;
  898. extern struct thr_info **mining_thr;
  899. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  900. extern int gpu_threads;
  901. #ifdef USE_SCRYPT
  902. extern bool opt_scrypt;
  903. #else
  904. #define opt_scrypt (0)
  905. #endif
  906. extern double total_secs;
  907. extern int mining_threads;
  908. extern int total_devices;
  909. extern int zombie_devs;
  910. extern struct cgpu_info **devices;
  911. extern int total_pools;
  912. extern struct pool **pools;
  913. extern struct strategies strategies[];
  914. extern enum pool_strategy pool_strategy;
  915. extern int opt_rotate_period;
  916. extern double total_mhashes_done;
  917. extern unsigned int new_blocks;
  918. extern unsigned int found_blocks;
  919. extern int total_accepted, total_rejected, total_diff1;;
  920. extern int total_getworks, total_stale, total_discarded;
  921. extern double total_diff_accepted, total_diff_rejected, total_diff_stale;
  922. extern unsigned int local_work;
  923. extern unsigned int total_go, total_ro;
  924. extern const int opt_cutofftemp;
  925. extern int opt_log_interval;
  926. extern unsigned long long global_hashrate;
  927. extern char *current_fullhash;
  928. extern double current_diff;
  929. extern uint64_t best_diff;
  930. extern struct timeval block_timeval;
  931. extern char *workpadding;
  932. #ifdef HAVE_OPENCL
  933. typedef struct {
  934. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  935. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  936. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  937. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  938. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  939. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  940. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  941. cl_uint W16; cl_uint W17; cl_uint W2;
  942. cl_uint PreVal4; cl_uint T1;
  943. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  944. cl_uint PreVal4addT1; cl_uint T1substate0;
  945. cl_uint PreVal4_2;
  946. cl_uint PreVal0;
  947. cl_uint PreW18;
  948. cl_uint PreW19;
  949. cl_uint PreW31;
  950. cl_uint PreW32;
  951. /* For diakgcn */
  952. cl_uint B1addK6, PreVal0addK7, W16addK16, W17addK17;
  953. cl_uint zeroA, zeroB;
  954. cl_uint oneA, twoA, threeA, fourA, fiveA, sixA, sevenA;
  955. #ifdef USE_SCRYPT
  956. struct work *work;
  957. #endif
  958. } dev_blk_ctx;
  959. #else
  960. typedef struct {
  961. uint32_t nonce;
  962. } dev_blk_ctx;
  963. #endif
  964. struct curl_ent {
  965. CURL *curl;
  966. struct list_head node;
  967. struct timeval tv;
  968. };
  969. /* Disabled needs to be the lowest enum as a freshly calloced value will then
  970. * equal disabled */
  971. enum pool_enable {
  972. POOL_DISABLED,
  973. POOL_ENABLED,
  974. POOL_REJECTING,
  975. };
  976. struct stratum_work {
  977. char *job_id;
  978. char *prev_hash;
  979. unsigned char **merkle_bin;
  980. char *bbversion;
  981. char *nbit;
  982. char *ntime;
  983. bool clean;
  984. size_t cb_len;
  985. size_t header_len;
  986. int merkles;
  987. double diff;
  988. };
  989. #define RBUFSIZE 8192
  990. #define RECVSIZE (RBUFSIZE - 4)
  991. struct pool {
  992. int pool_no;
  993. int prio;
  994. int accepted, rejected;
  995. int seq_rejects;
  996. int seq_getfails;
  997. int solved;
  998. int diff1;
  999. char diff[8];
  1000. int quota;
  1001. int quota_gcd;
  1002. int quota_used;
  1003. double diff_accepted;
  1004. double diff_rejected;
  1005. double diff_stale;
  1006. bool submit_fail;
  1007. bool idle;
  1008. bool lagging;
  1009. bool probed;
  1010. enum pool_enable enabled;
  1011. bool submit_old;
  1012. bool removed;
  1013. bool lp_started;
  1014. char *hdr_path;
  1015. char *lp_url;
  1016. unsigned int getwork_requested;
  1017. unsigned int stale_shares;
  1018. unsigned int discarded_work;
  1019. unsigned int getfail_occasions;
  1020. unsigned int remotefail_occasions;
  1021. struct timeval tv_idle;
  1022. double utility;
  1023. int last_shares, shares;
  1024. char *rpc_req;
  1025. char *rpc_url;
  1026. char *rpc_userpass;
  1027. char *rpc_user, *rpc_pass;
  1028. proxytypes_t rpc_proxytype;
  1029. char *rpc_proxy;
  1030. pthread_mutex_t pool_lock;
  1031. cglock_t data_lock;
  1032. struct thread_q *submit_q;
  1033. struct thread_q *getwork_q;
  1034. pthread_t longpoll_thread;
  1035. pthread_t test_thread;
  1036. bool testing;
  1037. int curls;
  1038. pthread_cond_t cr_cond;
  1039. struct list_head curlring;
  1040. time_t last_share_time;
  1041. double last_share_diff;
  1042. uint64_t best_diff;
  1043. struct cgminer_stats cgminer_stats;
  1044. struct cgminer_pool_stats cgminer_pool_stats;
  1045. /* Stratum variables */
  1046. char *stratum_url;
  1047. char *stratum_port;
  1048. struct addrinfo stratum_hints;
  1049. SOCKETTYPE sock;
  1050. char *sockbuf;
  1051. size_t sockbuf_size;
  1052. char *sockaddr_url; /* stripped url used for sockaddr */
  1053. char *sockaddr_proxy_url;
  1054. char *sockaddr_proxy_port;
  1055. char *nonce1;
  1056. unsigned char *nonce1bin;
  1057. size_t n1_len;
  1058. uint32_t nonce2;
  1059. int n2size;
  1060. char *sessionid;
  1061. bool has_stratum;
  1062. bool stratum_active;
  1063. bool stratum_init;
  1064. bool stratum_notify;
  1065. struct stratum_work swork;
  1066. pthread_t stratum_sthread;
  1067. pthread_t stratum_rthread;
  1068. pthread_mutex_t stratum_lock;
  1069. struct thread_q *stratum_q;
  1070. int sshares; /* stratum shares submitted waiting on response */
  1071. /* GBT variables */
  1072. bool has_gbt;
  1073. cglock_t gbt_lock;
  1074. unsigned char previousblockhash[32];
  1075. unsigned char gbt_target[32];
  1076. char *coinbasetxn;
  1077. char *longpollid;
  1078. char *gbt_workid;
  1079. int gbt_expires;
  1080. uint32_t gbt_version;
  1081. uint32_t curtime;
  1082. uint32_t gbt_bits;
  1083. unsigned char *txn_hashes;
  1084. int gbt_txns;
  1085. int coinbase_len;
  1086. /* Shared by both stratum & GBT */
  1087. unsigned char *coinbase;
  1088. int nonce2_offset;
  1089. unsigned char header_bin[128];
  1090. int merkle_offset;
  1091. struct timeval tv_lastwork;
  1092. };
  1093. #define GETWORK_MODE_TESTPOOL 'T'
  1094. #define GETWORK_MODE_POOL 'P'
  1095. #define GETWORK_MODE_LP 'L'
  1096. #define GETWORK_MODE_BENCHMARK 'B'
  1097. #define GETWORK_MODE_STRATUM 'S'
  1098. #define GETWORK_MODE_GBT 'G'
  1099. struct work {
  1100. unsigned char data[128];
  1101. unsigned char midstate[32];
  1102. unsigned char target[32];
  1103. unsigned char hash[32];
  1104. #ifdef USE_SCRYPT
  1105. unsigned char device_target[32];
  1106. #endif
  1107. double device_diff;
  1108. uint64_t share_diff;
  1109. unsigned char hash2[32];
  1110. int rolls;
  1111. dev_blk_ctx blk;
  1112. struct thr_info *thr;
  1113. int thr_id;
  1114. struct pool *pool;
  1115. struct timeval tv_staged;
  1116. bool mined;
  1117. bool clone;
  1118. bool cloned;
  1119. int rolltime;
  1120. bool longpoll;
  1121. bool stale;
  1122. bool mandatory;
  1123. bool block;
  1124. bool queued;
  1125. bool stratum;
  1126. char *job_id;
  1127. uint32_t nonce2;
  1128. size_t nonce2_len;
  1129. char *ntime;
  1130. double sdiff;
  1131. char *nonce1;
  1132. bool gbt;
  1133. char *coinbase;
  1134. int gbt_txns;
  1135. unsigned int work_block;
  1136. int id;
  1137. UT_hash_handle hh;
  1138. double work_difficulty;
  1139. // Allow devices to identify work if multiple sub-devices
  1140. int subid;
  1141. // Allow devices to flag work for their own purposes
  1142. bool devflag;
  1143. struct timeval tv_getwork;
  1144. struct timeval tv_getwork_reply;
  1145. struct timeval tv_cloned;
  1146. struct timeval tv_work_start;
  1147. struct timeval tv_work_found;
  1148. char getwork_mode;
  1149. };
  1150. #ifdef USE_MODMINER
  1151. struct modminer_fpga_state {
  1152. bool work_running;
  1153. struct work running_work;
  1154. struct timeval tv_workstart;
  1155. uint32_t hashes;
  1156. char next_work_cmd[46];
  1157. char fpgaid;
  1158. bool overheated;
  1159. bool new_work;
  1160. uint32_t shares;
  1161. uint32_t shares_last_hw;
  1162. uint32_t hw_errors;
  1163. uint32_t shares_to_good;
  1164. uint32_t timeout_fail;
  1165. uint32_t success_more;
  1166. struct timeval last_changed;
  1167. struct timeval last_nonce;
  1168. struct timeval first_work;
  1169. bool death_stage_one;
  1170. bool tried_two_byte_temp;
  1171. bool one_byte_temp;
  1172. };
  1173. #endif
  1174. #define TAILBUFSIZ 64
  1175. #define tailsprintf(buf, bufsiz, fmt, ...) do { \
  1176. char tmp13[TAILBUFSIZ]; \
  1177. size_t len13, buflen = strlen(buf); \
  1178. snprintf(tmp13, sizeof(tmp13), fmt, ##__VA_ARGS__); \
  1179. len13 = strlen(tmp13); \
  1180. if ((buflen + len13) >= bufsiz) \
  1181. quit(1, "tailsprintf buffer overflow in %s %s line %d", __FILE__, __func__, __LINE__); \
  1182. strcat(buf, tmp13); \
  1183. } while (0)
  1184. extern void get_datestamp(char *, size_t, struct timeval *);
  1185. extern void inc_hw_errors(struct thr_info *thr);
  1186. extern bool test_nonce(struct work *work, uint32_t nonce);
  1187. extern void submit_tested_work(struct thr_info *thr, struct work *work);
  1188. extern bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  1189. extern struct work *get_queued(struct cgpu_info *cgpu);
  1190. extern struct work *__find_work_bymidstate(struct work *que, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1191. extern struct work *find_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1192. extern struct work *clone_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1193. extern void work_completed(struct cgpu_info *cgpu, struct work *work);
  1194. extern struct work *take_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1195. extern void hash_queued_work(struct thr_info *mythr);
  1196. extern void _wlog(const char *str);
  1197. extern void _wlogprint(const char *str);
  1198. extern int curses_int(const char *query);
  1199. extern char *curses_input(const char *query);
  1200. extern void kill_work(void);
  1201. extern void switch_pools(struct pool *selected);
  1202. extern void discard_work(struct work *work);
  1203. extern void remove_pool(struct pool *pool);
  1204. extern void write_config(FILE *fcfg);
  1205. extern void zero_bestshare(void);
  1206. extern void zero_stats(void);
  1207. extern void default_save_file(char *filename);
  1208. extern bool log_curses_only(int prio, const char *datetime, const char *str);
  1209. extern void clear_logwin(void);
  1210. extern void logwin_update(void);
  1211. extern bool pool_tclear(struct pool *pool, bool *var);
  1212. extern struct thread_q *tq_new(void);
  1213. extern void tq_free(struct thread_q *tq);
  1214. extern bool tq_push(struct thread_q *tq, void *data);
  1215. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  1216. extern void tq_freeze(struct thread_q *tq);
  1217. extern void tq_thaw(struct thread_q *tq);
  1218. extern bool successful_connect;
  1219. extern void adl(void);
  1220. extern void app_restart(void);
  1221. extern void clean_work(struct work *work);
  1222. extern void free_work(struct work *work);
  1223. extern void __copy_work(struct work *work, struct work *base_work);
  1224. extern struct work *copy_work(struct work *base_work);
  1225. extern struct thr_info *get_thread(int thr_id);
  1226. extern struct cgpu_info *get_devices(int id);
  1227. enum api_data_type {
  1228. API_ESCAPE,
  1229. API_STRING,
  1230. API_CONST,
  1231. API_INT,
  1232. API_UINT,
  1233. API_UINT32,
  1234. API_UINT64,
  1235. API_DOUBLE,
  1236. API_ELAPSED,
  1237. API_BOOL,
  1238. API_TIMEVAL,
  1239. API_TIME,
  1240. API_MHS,
  1241. API_MHTOTAL,
  1242. API_TEMP,
  1243. API_UTILITY,
  1244. API_FREQ,
  1245. API_VOLTS,
  1246. API_HS,
  1247. API_DIFF,
  1248. API_PERCENT
  1249. };
  1250. struct api_data {
  1251. enum api_data_type type;
  1252. char *name;
  1253. void *data;
  1254. bool data_was_malloc;
  1255. struct api_data *prev;
  1256. struct api_data *next;
  1257. };
  1258. extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
  1259. extern struct api_data *api_add_string(struct api_data *root, char *name, char *data, bool copy_data);
  1260. extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
  1261. extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
  1262. extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
  1263. extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  1264. extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
  1265. extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
  1266. extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
  1267. extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
  1268. extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
  1269. extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
  1270. extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
  1271. extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
  1272. extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
  1273. extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
  1274. extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
  1275. extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
  1276. extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
  1277. extern struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data);
  1278. extern struct api_data *api_add_percent(struct api_data *root, char *name, double *data, bool copy_data);
  1279. #endif /* __MINER_H__ */