miner.h 37 KB

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