miner.h 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  1. #ifndef __MINER_H__
  2. #define __MINER_H__
  3. #include "config.h"
  4. #include <stdbool.h>
  5. #include <stdint.h>
  6. #include <sys/time.h>
  7. #include <pthread.h>
  8. #include <jansson.h>
  9. #include <inttypes.h>
  10. #include <sched.h>
  11. #include "elist.h"
  12. #if HAVE_UTHASH_H
  13. # include <uthash.h>
  14. #else
  15. # include "uthash.h"
  16. #endif
  17. #include "logging.h"
  18. #include "util.h"
  19. #include <sys/types.h>
  20. #ifndef WIN32
  21. # include <sys/socket.h>
  22. # include <netdb.h>
  23. #endif
  24. #ifdef USE_USBUTILS
  25. #include <semaphore.h>
  26. #endif
  27. #ifdef STDC_HEADERS
  28. # include <stdlib.h>
  29. # include <stddef.h>
  30. #else
  31. # ifdef HAVE_STDLIB_H
  32. # include <stdlib.h>
  33. # endif
  34. #endif
  35. #ifdef HAVE_ALLOCA_H
  36. # include <alloca.h>
  37. #elif defined __GNUC__
  38. # ifndef __FreeBSD__ /* FreeBSD has below #define in stdlib.h */
  39. # ifndef WIN32
  40. # define alloca __builtin_alloca
  41. # else
  42. # include <malloc.h>
  43. # endif
  44. # endif
  45. #elif defined _AIX
  46. # define alloca __alloca
  47. #elif defined _MSC_VER
  48. # include <malloc.h>
  49. # define alloca _alloca
  50. #else
  51. # ifndef HAVE_ALLOCA
  52. # ifdef __cplusplus
  53. extern "C"
  54. # endif
  55. void *alloca (size_t);
  56. # endif
  57. #endif
  58. #ifdef HAVE_LIBCURL
  59. #include <curl/curl.h>
  60. #else
  61. typedef char CURL;
  62. extern char *curly;
  63. #define curl_easy_init(curl) (curly)
  64. #define curl_easy_cleanup(curl) {}
  65. #define curl_global_cleanup() {}
  66. #define CURL_GLOBAL_ALL 0
  67. #define curl_global_init(X) (0)
  68. #endif
  69. #ifdef __MINGW32__
  70. #include <io.h>
  71. static inline int fsync (int fd)
  72. {
  73. return (FlushFileBuffers ((HANDLE) _get_osfhandle (fd))) ? 0 : -1;
  74. }
  75. #ifndef EWOULDBLOCK
  76. # define EWOULDBLOCK EAGAIN
  77. #endif
  78. #ifndef MSG_DONTWAIT
  79. # define MSG_DONTWAIT 0x1000000
  80. #endif
  81. #endif /* __MINGW32__ */
  82. #if defined (__linux)
  83. #ifndef LINUX
  84. #define LINUX
  85. #endif
  86. #endif
  87. #ifdef WIN32
  88. #ifndef timersub
  89. #define timersub(a, b, result) \
  90. do { \
  91. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  92. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  93. if ((result)->tv_usec < 0) { \
  94. --(result)->tv_sec; \
  95. (result)->tv_usec += 1000000; \
  96. } \
  97. } while (0)
  98. #endif
  99. #ifndef timeradd
  100. # define timeradd(a, b, result) \
  101. do { \
  102. (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
  103. (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
  104. if ((result)->tv_usec >= 1000000) \
  105. { \
  106. ++(result)->tv_sec; \
  107. (result)->tv_usec -= 1000000; \
  108. } \
  109. } while (0)
  110. #endif
  111. #endif
  112. #ifdef USE_USBUTILS
  113. #include <libusb.h>
  114. #endif
  115. #ifdef USE_USBUTILS
  116. #include "usbutils.h"
  117. #endif
  118. #if (!defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))) \
  119. || (defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)))
  120. #ifndef bswap_16
  121. #define bswap_16 __builtin_bswap16
  122. #define bswap_32 __builtin_bswap32
  123. #define bswap_64 __builtin_bswap64
  124. #endif
  125. #else
  126. #if HAVE_BYTESWAP_H
  127. #include <byteswap.h>
  128. #elif defined(USE_SYS_ENDIAN_H)
  129. #include <sys/endian.h>
  130. #elif defined(__APPLE__)
  131. #include <libkern/OSByteOrder.h>
  132. #define bswap_16 OSSwapInt16
  133. #define bswap_32 OSSwapInt32
  134. #define bswap_64 OSSwapInt64
  135. #else
  136. #define bswap_16(value) \
  137. ((((value) & 0xff) << 8) | ((value) >> 8))
  138. #define bswap_32(value) \
  139. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  140. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  141. #define bswap_64(value) \
  142. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  143. << 32) | \
  144. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  145. #endif
  146. #endif /* !defined(__GLXBYTEORDER_H__) */
  147. /* This assumes htobe32 is a macro in endian.h, and if it doesn't exist, then
  148. * htobe64 also won't exist */
  149. #ifndef htobe32
  150. # if __BYTE_ORDER == __LITTLE_ENDIAN
  151. # define htole16(x) (x)
  152. # define le16toh(x) (x)
  153. # define htole32(x) (x)
  154. # define htole64(x) (x)
  155. # define le32toh(x) (x)
  156. # define le64toh(x) (x)
  157. # define be32toh(x) bswap_32(x)
  158. # define be64toh(x) bswap_64(x)
  159. # define htobe16(x) bswap_16(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 le16toh(x) bswap_16(x)
  165. # define htole32(x) bswap_32(x)
  166. # define le32toh(x) bswap_32(x)
  167. # define le64toh(x) bswap_64(x)
  168. # define htole64(x) bswap_64(x)
  169. # define be32toh(x) (x)
  170. # define be64toh(x) (x)
  171. # define htobe16(x) (x)
  172. # define htobe32(x) (x)
  173. # define htobe64(x) (x)
  174. #else
  175. #error UNKNOWN BYTE ORDER
  176. #endif
  177. #endif
  178. #undef unlikely
  179. #undef likely
  180. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  181. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  182. #define likely(expr) (__builtin_expect(!!(expr), 1))
  183. #else
  184. #define unlikely(expr) (expr)
  185. #define likely(expr) (expr)
  186. #endif
  187. #define __maybe_unused __attribute__((unused))
  188. #define uninitialised_var(x) x = x
  189. #if defined(__i386__)
  190. #define WANT_CRYPTOPP_ASM32
  191. #endif
  192. #ifndef ARRAY_SIZE
  193. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  194. #endif
  195. /* No semtimedop on apple so ignore timeout till we implement one */
  196. #ifdef __APPLE__
  197. #define semtimedop(SEM, SOPS, VAL, TIMEOUT) semop(SEM, SOPS, VAL)
  198. #endif
  199. #ifndef MIN
  200. #define MIN(x, y) ((x) > (y) ? (y) : (x))
  201. #endif
  202. #ifndef MAX
  203. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  204. #endif
  205. #define MACSTR(_num) MACSTR2(_num)
  206. #define MACSTR2(__num) #__num
  207. /* Put avalon last to make it the last device it tries to detect to prevent it
  208. * trying to claim same chip but different devices. Adding a device here will
  209. * update all macros in the code that use the *_PARSE_COMMANDS macros for each
  210. * listed driver. */
  211. #define FPGA_PARSE_COMMANDS(DRIVER_ADD_COMMAND) \
  212. DRIVER_ADD_COMMAND(bitforce) \
  213. DRIVER_ADD_COMMAND(modminer)
  214. #define ASIC_PARSE_COMMANDS(DRIVER_ADD_COMMAND) \
  215. DRIVER_ADD_COMMAND(ants1) \
  216. DRIVER_ADD_COMMAND(ants2) \
  217. DRIVER_ADD_COMMAND(ants3) \
  218. DRIVER_ADD_COMMAND(avalon) \
  219. DRIVER_ADD_COMMAND(avalon2) \
  220. DRIVER_ADD_COMMAND(avalon4) \
  221. DRIVER_ADD_COMMAND(avalon7) \
  222. DRIVER_ADD_COMMAND(avalon8) \
  223. DRIVER_ADD_COMMAND(avalonm) \
  224. DRIVER_ADD_COMMAND(bab) \
  225. DRIVER_ADD_COMMAND(bflsc) \
  226. DRIVER_ADD_COMMAND(bitfury) \
  227. DRIVER_ADD_COMMAND(bitfury16) \
  228. DRIVER_ADD_COMMAND(bitmineA1) \
  229. DRIVER_ADD_COMMAND(blockerupter) \
  230. DRIVER_ADD_COMMAND(cointerra) \
  231. DRIVER_ADD_COMMAND(gekko) \
  232. DRIVER_ADD_COMMAND(dragonmintT1) \
  233. DRIVER_ADD_COMMAND(hashfast) \
  234. DRIVER_ADD_COMMAND(drillbit) \
  235. DRIVER_ADD_COMMAND(hashratio) \
  236. DRIVER_ADD_COMMAND(icarus) \
  237. DRIVER_ADD_COMMAND(klondike) \
  238. DRIVER_ADD_COMMAND(knc) \
  239. DRIVER_ADD_COMMAND(minion) \
  240. DRIVER_ADD_COMMAND(sp10) \
  241. DRIVER_ADD_COMMAND(sp30) \
  242. DRIVER_ADD_COMMAND(bitmain_soc)
  243. #define DRIVER_PARSE_COMMANDS(DRIVER_ADD_COMMAND) \
  244. FPGA_PARSE_COMMANDS(DRIVER_ADD_COMMAND) \
  245. ASIC_PARSE_COMMANDS(DRIVER_ADD_COMMAND)
  246. #define DRIVER_ENUM(X) DRIVER_##X,
  247. #define DRIVER_PROTOTYPE(X) struct device_drv X##_drv;
  248. /* Create drv_driver enum from DRIVER_PARSE_COMMANDS macro */
  249. enum drv_driver {
  250. DRIVER_PARSE_COMMANDS(DRIVER_ENUM)
  251. DRIVER_MAX
  252. };
  253. /* Use DRIVER_PARSE_COMMANDS to generate extern device_drv prototypes */
  254. DRIVER_PARSE_COMMANDS(DRIVER_PROTOTYPE)
  255. enum alive {
  256. LIFE_WELL,
  257. LIFE_SICK,
  258. LIFE_DEAD,
  259. LIFE_NOSTART,
  260. LIFE_INIT,
  261. };
  262. enum pool_strategy {
  263. POOL_FAILOVER,
  264. POOL_ROUNDROBIN,
  265. POOL_ROTATE,
  266. POOL_LOADBALANCE,
  267. POOL_BALANCE,
  268. };
  269. #define TOP_STRATEGY (POOL_BALANCE)
  270. struct strategies {
  271. const char *s;
  272. };
  273. struct cgpu_info;
  274. extern void blank_get_statline_before(char *buf, size_t bufsiz, struct cgpu_info __maybe_unused *cgpu);
  275. struct api_data;
  276. struct thr_info;
  277. struct work;
  278. struct device_drv {
  279. enum drv_driver drv_id;
  280. char *dname;
  281. char *name;
  282. // DRV-global functions
  283. void (*drv_detect)(bool);
  284. // Device-specific functions
  285. void (*reinit_device)(struct cgpu_info *);
  286. void (*get_statline_before)(char *, size_t, struct cgpu_info *);
  287. void (*get_statline)(char *, size_t, struct cgpu_info *);
  288. struct api_data *(*get_api_stats)(struct cgpu_info *);
  289. struct api_data *(*get_api_debug)(struct cgpu_info *);
  290. bool (*get_stats)(struct cgpu_info *);
  291. void (*identify_device)(struct cgpu_info *); // e.g. to flash a led
  292. char *(*set_device)(struct cgpu_info *, char *option, char *setting, char *replybuf);
  293. // Thread-specific functions
  294. bool (*thread_prepare)(struct thr_info *);
  295. uint64_t (*can_limit_work)(struct thr_info *);
  296. bool (*thread_init)(struct thr_info *);
  297. bool (*prepare_work)(struct thr_info *, struct work *);
  298. /* Which hash work loop this driver uses. */
  299. void (*hash_work)(struct thr_info *);
  300. /* Two variants depending on whether the device divides work up into
  301. * small pieces or works with whole work items and may or may not have
  302. * a queue of its own. */
  303. int64_t (*scanhash)(struct thr_info *, struct work *, int64_t);
  304. int64_t (*scanwork)(struct thr_info *);
  305. /* Used to extract work from the hash table of queued work and tell
  306. * the main loop that it should not add any further work to the table.
  307. */
  308. bool (*queue_full)(struct cgpu_info *);
  309. /* Tell the driver of a block change */
  310. void (*flush_work)(struct cgpu_info *);
  311. /* Tell the driver of an updated work template for eg. stratum */
  312. void (*update_work)(struct cgpu_info *);
  313. void (*hw_error)(struct thr_info *);
  314. void (*thread_shutdown)(struct thr_info *);
  315. void (*thread_enable)(struct thr_info *);
  316. /* What should be zeroed in this device when global zero stats is sent */
  317. void (*zero_stats)(struct cgpu_info *);
  318. // Does it need to be free()d?
  319. bool copy;
  320. /* Highest target diff the device supports */
  321. double max_diff;
  322. /* Lowest diff the controller can safely run at */
  323. double min_diff;
  324. /* Does this device generate work itself and not require stratum work generation? */
  325. bool genwork;
  326. };
  327. extern struct device_drv *copy_drv(struct device_drv*);
  328. enum dev_enable {
  329. DEV_ENABLED,
  330. DEV_DISABLED,
  331. DEV_RECOVER,
  332. };
  333. enum dev_reason {
  334. REASON_THREAD_FAIL_INIT,
  335. REASON_THREAD_ZERO_HASH,
  336. REASON_THREAD_FAIL_QUEUE,
  337. REASON_DEV_SICK_IDLE_60,
  338. REASON_DEV_DEAD_IDLE_600,
  339. REASON_DEV_NOSTART,
  340. REASON_DEV_OVER_HEAT,
  341. REASON_DEV_THERMAL_CUTOFF,
  342. REASON_DEV_COMMS_ERROR,
  343. REASON_DEV_THROTTLE,
  344. };
  345. #define REASON_NONE "None"
  346. #define REASON_THREAD_FAIL_INIT_STR "Thread failed to init"
  347. #define REASON_THREAD_ZERO_HASH_STR "Thread got zero hashes"
  348. #define REASON_THREAD_FAIL_QUEUE_STR "Thread failed to queue work"
  349. #define REASON_DEV_SICK_IDLE_60_STR "Device idle for 60s"
  350. #define REASON_DEV_DEAD_IDLE_600_STR "Device dead - idle for 600s"
  351. #define REASON_DEV_NOSTART_STR "Device failed to start"
  352. #define REASON_DEV_OVER_HEAT_STR "Device over heated"
  353. #define REASON_DEV_THERMAL_CUTOFF_STR "Device reached thermal cutoff"
  354. #define REASON_DEV_COMMS_ERROR_STR "Device comms error"
  355. #define REASON_DEV_THROTTLE_STR "Device throttle"
  356. #define REASON_UNKNOWN_STR "Unknown reason - code bug"
  357. #define MIN_SEC_UNSET 99999999
  358. struct cgminer_stats {
  359. uint32_t getwork_calls;
  360. struct timeval getwork_wait;
  361. struct timeval getwork_wait_max;
  362. struct timeval getwork_wait_min;
  363. };
  364. // Just the actual network getworks to the pool
  365. struct cgminer_pool_stats {
  366. uint32_t getwork_calls;
  367. uint32_t getwork_attempts;
  368. struct timeval getwork_wait;
  369. struct timeval getwork_wait_max;
  370. struct timeval getwork_wait_min;
  371. double getwork_wait_rolling;
  372. bool hadrolltime;
  373. bool canroll;
  374. bool hadexpire;
  375. uint32_t rolltime;
  376. double min_diff;
  377. double max_diff;
  378. double last_diff;
  379. uint32_t min_diff_count;
  380. uint32_t max_diff_count;
  381. uint64_t times_sent;
  382. uint64_t bytes_sent;
  383. uint64_t net_bytes_sent;
  384. uint64_t times_received;
  385. uint64_t bytes_received;
  386. uint64_t net_bytes_received;
  387. };
  388. struct cgpu_info {
  389. int cgminer_id;
  390. struct device_drv *drv;
  391. int device_id;
  392. char *name;
  393. char *device_path;
  394. void *device_data;
  395. void *dup_data;
  396. char *unique_id;
  397. #ifdef USE_USBUTILS
  398. struct cg_usb_device *usbdev;
  399. struct cg_usb_info usbinfo;
  400. bool blacklisted;
  401. bool nozlp; // Device prefers no zero length packet
  402. #endif
  403. #if defined(USE_AVALON) || defined(USE_AVALON2) || defined (USE_AVALON_MINER)
  404. struct work **works;
  405. int work_array;
  406. int queued;
  407. int results;
  408. #endif
  409. #ifdef USE_MODMINER
  410. char fpgaid;
  411. unsigned char clock;
  412. pthread_mutex_t *modminer_mutex;
  413. #endif
  414. #ifdef USE_BITFORCE
  415. struct timeval work_start_tv;
  416. unsigned int wait_ms;
  417. unsigned int sleep_ms;
  418. double avg_wait_f;
  419. unsigned int avg_wait_d;
  420. uint32_t nonces;
  421. bool nonce_range;
  422. bool polling;
  423. bool flash_led;
  424. #endif /* USE_BITFORCE */
  425. #if defined(USE_BITFORCE) || defined(USE_BFLSC)
  426. pthread_mutex_t device_mutex;
  427. #endif /* USE_BITFORCE || USE_BFLSC */
  428. enum dev_enable deven;
  429. int accepted;
  430. int rejected;
  431. int hw_errors;
  432. double rolling;
  433. double rolling1;
  434. double rolling5;
  435. double rolling15;
  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. bool new_work;
  446. double temp;
  447. #ifdef USE_DRAGONMINT_T1
  448. double temp_max;
  449. double temp_min;
  450. int fan_duty;
  451. int chainNum;
  452. double mhs_av;
  453. #endif
  454. int cutofftemp;
  455. int64_t diff1;
  456. double diff_accepted;
  457. double diff_rejected;
  458. int last_share_pool;
  459. time_t last_share_pool_time;
  460. double last_share_diff;
  461. time_t last_device_valid_work;
  462. uint32_t last_nonce;
  463. time_t device_last_well;
  464. time_t device_last_not_well;
  465. enum dev_reason device_not_well_reason;
  466. int thread_fail_init_count;
  467. int thread_zero_hash_count;
  468. int thread_fail_queue_count;
  469. int dev_sick_idle_60_count;
  470. int dev_dead_idle_600_count;
  471. int dev_nostart_count;
  472. int dev_over_heat_count; // It's a warning but worth knowing
  473. int dev_thermal_cutoff_count;
  474. int dev_comms_error_count;
  475. int dev_throttle_count;
  476. struct cgminer_stats cgminer_stats;
  477. pthread_rwlock_t qlock;
  478. struct work *queued_work;
  479. struct work *unqueued_work;
  480. unsigned int queued_count;
  481. bool shutdown;
  482. struct timeval dev_start_tv;
  483. /* For benchmarking only */
  484. int hidiff;
  485. int lodiff;
  486. int direction;
  487. };
  488. extern bool add_cgpu(struct cgpu_info*);
  489. struct thread_q {
  490. struct list_head q;
  491. bool frozen;
  492. pthread_mutex_t mutex;
  493. pthread_cond_t cond;
  494. };
  495. struct thr_info {
  496. int id;
  497. int device_thread;
  498. bool primary_thread;
  499. pthread_t pth;
  500. cgsem_t sem;
  501. struct thread_q *q;
  502. struct cgpu_info *cgpu;
  503. void *cgpu_data;
  504. struct timeval last;
  505. struct timeval sick;
  506. bool pause;
  507. bool getwork;
  508. bool work_restart;
  509. bool work_update;
  510. };
  511. struct string_elist {
  512. char *string;
  513. bool free_me;
  514. struct list_head list;
  515. };
  516. static inline void string_elist_add(const char *s, struct list_head *head)
  517. {
  518. struct string_elist *n;
  519. n = cgcalloc(1, sizeof(*n));
  520. n->string = strdup(s);
  521. n->free_me = true;
  522. list_add_tail(&n->list, head);
  523. }
  524. static inline void string_elist_del(struct string_elist *item)
  525. {
  526. if (item->free_me)
  527. free(item->string);
  528. list_del(&item->list);
  529. }
  530. static inline uint32_t swab32(uint32_t v)
  531. {
  532. return bswap_32(v);
  533. }
  534. static inline void swap256(void *dest_p, const void *src_p)
  535. {
  536. uint32_t *dest = dest_p;
  537. const uint32_t *src = src_p;
  538. dest[0] = src[7];
  539. dest[1] = src[6];
  540. dest[2] = src[5];
  541. dest[3] = src[4];
  542. dest[4] = src[3];
  543. dest[5] = src[2];
  544. dest[6] = src[1];
  545. dest[7] = src[0];
  546. }
  547. static inline void swab256(void *dest_p, const void *src_p)
  548. {
  549. uint32_t *dest = dest_p;
  550. const uint32_t *src = src_p;
  551. dest[0] = swab32(src[7]);
  552. dest[1] = swab32(src[6]);
  553. dest[2] = swab32(src[5]);
  554. dest[3] = swab32(src[4]);
  555. dest[4] = swab32(src[3]);
  556. dest[5] = swab32(src[2]);
  557. dest[6] = swab32(src[1]);
  558. dest[7] = swab32(src[0]);
  559. }
  560. static inline void flip12(void *dest_p, const void *src_p)
  561. {
  562. uint32_t *dest = dest_p;
  563. const uint32_t *src = src_p;
  564. int i;
  565. for (i = 0; i < 3; i++)
  566. dest[i] = swab32(src[i]);
  567. }
  568. static inline void flip32(void *dest_p, const void *src_p)
  569. {
  570. uint32_t *dest = dest_p;
  571. const uint32_t *src = src_p;
  572. int i;
  573. for (i = 0; i < 8; i++)
  574. dest[i] = swab32(src[i]);
  575. }
  576. static inline void flip64(void *dest_p, const void *src_p)
  577. {
  578. uint32_t *dest = dest_p;
  579. const uint32_t *src = src_p;
  580. int i;
  581. for (i = 0; i < 16; i++)
  582. dest[i] = swab32(src[i]);
  583. }
  584. static inline void flip80(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 < 20; i++)
  590. dest[i] = swab32(src[i]);
  591. }
  592. static inline void flip128(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 < 32; i++)
  598. dest[i] = swab32(src[i]);
  599. }
  600. /* For flipping to the correct endianness if necessary */
  601. #if defined(__BIG_ENDIAN__) || defined(MIPSEB)
  602. static inline void endian_flip32(void *dest_p, const void *src_p)
  603. {
  604. flip32(dest_p, src_p);
  605. }
  606. static inline void endian_flip128(void *dest_p, const void *src_p)
  607. {
  608. flip128(dest_p, src_p);
  609. }
  610. #else
  611. static inline void
  612. endian_flip32(void __maybe_unused *dest_p, const void __maybe_unused *src_p)
  613. {
  614. }
  615. static inline void
  616. endian_flip128(void __maybe_unused *dest_p, const void __maybe_unused *src_p)
  617. {
  618. }
  619. #endif
  620. extern double cgpu_runtime(struct cgpu_info *cgpu);
  621. extern double tsince_restart(void);
  622. extern double tsince_update(void);
  623. extern void __quit(int status, bool clean);
  624. extern void _quit(int status);
  625. /*
  626. * Set this to non-zero to enable lock tracking
  627. * Use the API lockstats command to see the locking status on stderr
  628. * i.e. in your log file if you 2> log.log - but not on the screen
  629. * API lockstats is privilidged but will always exist and will return
  630. * success if LOCK_TRACKING is enabled and warning if disabled
  631. * In production code, this should never be enabled since it will slow down all locking
  632. * So, e.g. use it to track down a deadlock - after a reproducable deadlock occurs
  633. * ... Of course if the API code itself deadlocks, it wont help :)
  634. */
  635. #define LOCK_TRACKING 0
  636. #if LOCK_TRACKING
  637. enum cglock_typ {
  638. CGLOCK_MUTEX,
  639. CGLOCK_RW,
  640. CGLOCK_UNKNOWN
  641. };
  642. extern uint64_t api_getlock(void *lock, const char *file, const char *func, const int line);
  643. extern void api_gotlock(uint64_t id, void *lock, const char *file, const char *func, const int line);
  644. extern uint64_t api_trylock(void *lock, const char *file, const char *func, const int line);
  645. extern void api_didlock(uint64_t id, int ret, void *lock, const char *file, const char *func, const int line);
  646. extern void api_gunlock(void *lock, const char *file, const char *func, const int line);
  647. extern void api_initlock(void *lock, enum cglock_typ typ, const char *file, const char *func, const int line);
  648. #define GETLOCK(_lock, _file, _func, _line) uint64_t _id1 = api_getlock((void *)(_lock), _file, _func, _line)
  649. #define GOTLOCK(_lock, _file, _func, _line) api_gotlock(_id1, (void *)(_lock), _file, _func, _line)
  650. #define TRYLOCK(_lock, _file, _func, _line) uint64_t _id2 = api_trylock((void *)(_lock), _file, _func, _line)
  651. #define DIDLOCK(_ret, _lock, _file, _func, _line) api_didlock(_id2, _ret, (void *)(_lock), _file, _func, _line)
  652. #define GUNLOCK(_lock, _file, _func, _line) api_gunlock((void *)(_lock), _file, _func, _line)
  653. #define INITLOCK(_lock, _typ, _file, _func, _line) api_initlock((void *)(_lock), _typ, _file, _func, _line)
  654. #else
  655. #define GETLOCK(_lock, _file, _func, _line)
  656. #define GOTLOCK(_lock, _file, _func, _line)
  657. #define TRYLOCK(_lock, _file, _func, _line)
  658. #define DIDLOCK(_ret, _lock, _file, _func, _line)
  659. #define GUNLOCK(_lock, _file, _func, _line)
  660. #define INITLOCK(_typ, _lock, _file, _func, _line)
  661. #endif
  662. #define mutex_lock(_lock) _mutex_lock(_lock, __FILE__, __func__, __LINE__)
  663. #define mutex_unlock_noyield(_lock) _mutex_unlock_noyield(_lock, __FILE__, __func__, __LINE__)
  664. #define mutex_unlock(_lock) _mutex_unlock(_lock, __FILE__, __func__, __LINE__)
  665. #define mutex_trylock(_lock) _mutex_trylock(_lock, __FILE__, __func__, __LINE__)
  666. #define wr_lock(_lock) _wr_lock(_lock, __FILE__, __func__, __LINE__)
  667. #define wr_trylock(_lock) _wr_trylock(_lock, __FILE__, __func__, __LINE__)
  668. #define rd_lock(_lock) _rd_lock(_lock, __FILE__, __func__, __LINE__)
  669. #define rw_unlock(_lock) _rw_unlock(_lock, __FILE__, __func__, __LINE__)
  670. #define rd_unlock_noyield(_lock) _rd_unlock_noyield(_lock, __FILE__, __func__, __LINE__)
  671. #define wr_unlock_noyield(_lock) _wr_unlock_noyield(_lock, __FILE__, __func__, __LINE__)
  672. #define rd_unlock(_lock) _rd_unlock(_lock, __FILE__, __func__, __LINE__)
  673. #define wr_unlock(_lock) _wr_unlock(_lock, __FILE__, __func__, __LINE__)
  674. #define mutex_init(_lock) _mutex_init(_lock, __FILE__, __func__, __LINE__)
  675. #define rwlock_init(_lock) _rwlock_init(_lock, __FILE__, __func__, __LINE__)
  676. #define cglock_init(_lock) _cglock_init(_lock, __FILE__, __func__, __LINE__)
  677. #define cg_rlock(_lock) _cg_rlock(_lock, __FILE__, __func__, __LINE__)
  678. #define cg_ilock(_lock) _cg_ilock(_lock, __FILE__, __func__, __LINE__)
  679. #define cg_uilock(_lock) _cg_uilock(_lock, __FILE__, __func__, __LINE__)
  680. #define cg_ulock(_lock) _cg_ulock(_lock, __FILE__, __func__, __LINE__)
  681. #define cg_wlock(_lock) _cg_wlock(_lock, __FILE__, __func__, __LINE__)
  682. #define cg_dwlock(_lock) _cg_dwlock(_lock, __FILE__, __func__, __LINE__)
  683. #define cg_dwilock(_lock) _cg_dwilock(_lock, __FILE__, __func__, __LINE__)
  684. #define cg_dlock(_lock) _cg_dlock(_lock, __FILE__, __func__, __LINE__)
  685. #define cg_runlock(_lock) _cg_runlock(_lock, __FILE__, __func__, __LINE__)
  686. #define cg_ruwlock(_lock) _cg_ruwlock(_lock, __FILE__, __func__, __LINE__)
  687. #define cg_wunlock(_lock) _cg_wunlock(_lock, __FILE__, __func__, __LINE__)
  688. static inline void _mutex_lock(pthread_mutex_t *lock, const char *file, const char *func, const int line)
  689. {
  690. GETLOCK(lock, file, func, line);
  691. if (unlikely(pthread_mutex_lock(lock)))
  692. quitfrom(1, file, func, line, "WTF MUTEX ERROR ON LOCK! errno=%d", errno);
  693. GOTLOCK(lock, file, func, line);
  694. }
  695. static inline void _mutex_unlock_noyield(pthread_mutex_t *lock, const char *file, const char *func, const int line)
  696. {
  697. if (unlikely(pthread_mutex_unlock(lock)))
  698. quitfrom(1, file, func, line, "WTF MUTEX ERROR ON UNLOCK! errno=%d", errno);
  699. GUNLOCK(lock, file, func, line);
  700. }
  701. static inline void _mutex_unlock(pthread_mutex_t *lock, const char *file, const char *func, const int line)
  702. {
  703. _mutex_unlock_noyield(lock, file, func, line);
  704. selective_yield();
  705. }
  706. static inline int _mutex_trylock(pthread_mutex_t *lock, __maybe_unused const char *file, __maybe_unused const char *func, __maybe_unused const int line)
  707. {
  708. TRYLOCK(lock, file, func, line);
  709. int ret = pthread_mutex_trylock(lock);
  710. DIDLOCK(ret, lock, file, func, line);
  711. return ret;
  712. }
  713. static inline void _wr_lock(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  714. {
  715. GETLOCK(lock, file, func, line);
  716. if (unlikely(pthread_rwlock_wrlock(lock)))
  717. quitfrom(1, file, func, line, "WTF WRLOCK ERROR ON LOCK! errno=%d", errno);
  718. GOTLOCK(lock, file, func, line);
  719. }
  720. static inline int _wr_trylock(pthread_rwlock_t *lock, __maybe_unused const char *file, __maybe_unused const char *func, __maybe_unused const int line)
  721. {
  722. TRYLOCK(lock, file, func, line);
  723. int ret = pthread_rwlock_trywrlock(lock);
  724. DIDLOCK(ret, lock, file, func, line);
  725. return ret;
  726. }
  727. static inline void _rd_lock(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  728. {
  729. GETLOCK(lock, file, func, line);
  730. if (unlikely(pthread_rwlock_rdlock(lock)))
  731. quitfrom(1, file, func, line, "WTF RDLOCK ERROR ON LOCK! errno=%d", errno);
  732. GOTLOCK(lock, file, func, line);
  733. }
  734. static inline void _rw_unlock(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  735. {
  736. if (unlikely(pthread_rwlock_unlock(lock)))
  737. quitfrom(1, file, func, line, "WTF RWLOCK ERROR ON UNLOCK! errno=%d", errno);
  738. GUNLOCK(lock, file, func, line);
  739. }
  740. static inline void _rd_unlock_noyield(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  741. {
  742. _rw_unlock(lock, file, func, line);
  743. }
  744. static inline void _wr_unlock_noyield(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  745. {
  746. _rw_unlock(lock, file, func, line);
  747. }
  748. static inline void _rd_unlock(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  749. {
  750. _rw_unlock(lock, file, func, line);
  751. selective_yield();
  752. }
  753. static inline void _wr_unlock(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  754. {
  755. _rw_unlock(lock, file, func, line);
  756. selective_yield();
  757. }
  758. static inline void _mutex_init(pthread_mutex_t *lock, const char *file, const char *func, const int line)
  759. {
  760. if (unlikely(pthread_mutex_init(lock, NULL)))
  761. quitfrom(1, file, func, line, "Failed to pthread_mutex_init errno=%d", errno);
  762. INITLOCK(lock, CGLOCK_MUTEX, file, func, line);
  763. }
  764. static inline void mutex_destroy(pthread_mutex_t *lock)
  765. {
  766. /* Ignore return code. This only invalidates the mutex on linux but
  767. * releases resources on windows. */
  768. pthread_mutex_destroy(lock);
  769. }
  770. static inline void _rwlock_init(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  771. {
  772. if (unlikely(pthread_rwlock_init(lock, NULL)))
  773. quitfrom(1, file, func, line, "Failed to pthread_rwlock_init errno=%d", errno);
  774. INITLOCK(lock, CGLOCK_RW, file, func, line);
  775. }
  776. static inline void rwlock_destroy(pthread_rwlock_t *lock)
  777. {
  778. pthread_rwlock_destroy(lock);
  779. }
  780. static inline void _cglock_init(cglock_t *lock, const char *file, const char *func, const int line)
  781. {
  782. _mutex_init(&lock->mutex, file, func, line);
  783. _rwlock_init(&lock->rwlock, file, func, line);
  784. }
  785. static inline void cglock_destroy(cglock_t *lock)
  786. {
  787. rwlock_destroy(&lock->rwlock);
  788. mutex_destroy(&lock->mutex);
  789. }
  790. /* Read lock variant of cglock. Cannot be promoted. */
  791. static inline void _cg_rlock(cglock_t *lock, const char *file, const char *func, const int line)
  792. {
  793. _mutex_lock(&lock->mutex, file, func, line);
  794. _rd_lock(&lock->rwlock, file, func, line);
  795. _mutex_unlock_noyield(&lock->mutex, file, func, line);
  796. }
  797. /* Intermediate variant of cglock - behaves as a read lock but can be promoted
  798. * to a write lock or demoted to read lock. */
  799. static inline void _cg_ilock(cglock_t *lock, const char *file, const char *func, const int line)
  800. {
  801. _mutex_lock(&lock->mutex, file, func, line);
  802. }
  803. /* Unlock intermediate variant without changing to read or write version */
  804. static inline void _cg_uilock(cglock_t *lock, const char *file, const char *func, const int line)
  805. {
  806. _mutex_unlock(&lock->mutex, file, func, line);
  807. }
  808. /* Upgrade intermediate variant to a write lock */
  809. static inline void _cg_ulock(cglock_t *lock, const char *file, const char *func, const int line)
  810. {
  811. _wr_lock(&lock->rwlock, file, func, line);
  812. }
  813. /* Write lock variant of cglock */
  814. static inline void _cg_wlock(cglock_t *lock, const char *file, const char *func, const int line)
  815. {
  816. _mutex_lock(&lock->mutex, file, func, line);
  817. _wr_lock(&lock->rwlock, file, func, line);
  818. }
  819. /* Downgrade write variant to a read lock */
  820. static inline void _cg_dwlock(cglock_t *lock, const char *file, const char *func, const int line)
  821. {
  822. _wr_unlock_noyield(&lock->rwlock, file, func, line);
  823. _rd_lock(&lock->rwlock, file, func, line);
  824. _mutex_unlock_noyield(&lock->mutex, file, func, line);
  825. }
  826. /* Demote a write variant to an intermediate variant */
  827. static inline void _cg_dwilock(cglock_t *lock, const char *file, const char *func, const int line)
  828. {
  829. _wr_unlock(&lock->rwlock, file, func, line);
  830. }
  831. /* Downgrade intermediate variant to a read lock */
  832. static inline void _cg_dlock(cglock_t *lock, const char *file, const char *func, const int line)
  833. {
  834. _rd_lock(&lock->rwlock, file, func, line);
  835. _mutex_unlock_noyield(&lock->mutex, file, func, line);
  836. }
  837. static inline void _cg_runlock(cglock_t *lock, const char *file, const char *func, const int line)
  838. {
  839. _rd_unlock(&lock->rwlock, file, func, line);
  840. }
  841. /* This drops the read lock and grabs a write lock. It does NOT protect data
  842. * between the two locks! */
  843. static inline void _cg_ruwlock(cglock_t *lock, const char *file, const char *func, const int line)
  844. {
  845. _rd_unlock_noyield(&lock->rwlock, file, func, line);
  846. _cg_wlock(lock, file, func, line);
  847. }
  848. static inline void _cg_wunlock(cglock_t *lock, const char *file, const char *func, const int line)
  849. {
  850. _wr_unlock_noyield(&lock->rwlock, file, func, line);
  851. _mutex_unlock(&lock->mutex, file, func, line);
  852. }
  853. struct pool;
  854. #define API_LISTEN_ADDR "0.0.0.0"
  855. #define API_MCAST_CODE "FTW"
  856. #define API_MCAST_ADDR "224.0.0.75"
  857. extern bool opt_work_update;
  858. extern bool opt_protocol;
  859. extern bool have_longpoll;
  860. extern char *opt_kernel_path;
  861. extern char *opt_socks_proxy;
  862. extern int opt_suggest_diff;
  863. extern char *cgminer_path;
  864. extern bool opt_lowmem;
  865. extern bool opt_autofan;
  866. extern bool opt_autoengine;
  867. extern bool use_curses;
  868. extern char *opt_api_allow;
  869. extern bool opt_api_mcast;
  870. extern char *opt_api_mcast_addr;
  871. extern char *opt_api_mcast_code;
  872. extern char *opt_api_mcast_des;
  873. extern int opt_api_mcast_port;
  874. extern char *opt_api_groups;
  875. extern char *opt_api_description;
  876. extern int opt_api_port;
  877. extern char *opt_api_host;
  878. extern bool opt_api_listen;
  879. extern bool opt_api_network;
  880. extern bool opt_delaynet;
  881. extern time_t last_getwork;
  882. extern bool opt_restart;
  883. #ifdef USE_ICARUS
  884. extern char *opt_icarus_options;
  885. extern char *opt_icarus_timing;
  886. extern float opt_anu_freq;
  887. extern float opt_au3_freq;
  888. extern int opt_au3_volt;
  889. extern float opt_rock_freq;
  890. #endif
  891. extern bool opt_worktime;
  892. #ifdef USE_AVALON
  893. extern char *opt_avalon_options;
  894. extern char *opt_bitburner_fury_options;
  895. #endif
  896. #ifdef USE_GEKKO
  897. extern char *opt_gekko_serial;
  898. extern bool opt_gekko_boost;
  899. extern bool opt_gekko_gsc_detect;
  900. extern bool opt_gekko_gsd_detect;
  901. extern bool opt_gekko_gse_detect;
  902. extern bool opt_gekko_gsh_detect;
  903. extern float opt_gekko_gsc_freq;
  904. extern float opt_gekko_gsd_freq;
  905. extern float opt_gekko_gse_freq;
  906. extern int opt_gekko_gsh_freq;
  907. extern int opt_gekko_gsh_vcore;
  908. extern int opt_gekko_start_freq;
  909. extern int opt_gekko_step_freq;
  910. extern int opt_gekko_step_delay;
  911. #endif
  912. #ifdef USE_KLONDIKE
  913. extern char *opt_klondike_options;
  914. #endif
  915. #ifdef USE_DRILLBIT
  916. extern char *opt_drillbit_options;
  917. extern char *opt_drillbit_auto;
  918. #endif
  919. #ifdef USE_BAB
  920. extern char *opt_bab_options;
  921. #endif
  922. #ifdef USE_BITMINE_A1
  923. extern char *opt_bitmine_a1_options;
  924. #endif
  925. #ifdef USE_DRAGONMINT_T1
  926. extern char *opt_dragonmint_t1_options;
  927. extern int opt_T1Pll[];
  928. extern int opt_T1Vol[];
  929. extern int opt_T1VID[];
  930. extern bool opt_T1auto;
  931. extern bool opt_T1_efficient;
  932. extern bool opt_T1_performance;
  933. extern int opt_T1_target;
  934. #endif
  935. #ifdef USE_ANT_S1
  936. extern char *opt_bitmain_options;
  937. extern char *opt_bitmain_freq;
  938. extern bool opt_bitmain_hwerror;
  939. #endif
  940. #if (defined(USE_ANT_S2) || defined(USE_ANT_S3))
  941. #ifndef USE_ANT_S3
  942. extern char *opt_bitmain_dev;
  943. #endif
  944. extern char *opt_bitmain_options;
  945. extern char *opt_bitmain_freq;
  946. extern bool opt_bitmain_hwerror;
  947. extern bool opt_bitmain_checkall;
  948. extern bool opt_bitmain_checkn2diff;
  949. extern bool opt_bitmain_beeper;
  950. extern bool opt_bitmain_tempoverctrl;
  951. extern char *opt_bitmain_voltage;
  952. #endif
  953. #ifdef USE_MINION
  954. extern int opt_minion_chipreport;
  955. extern char *opt_minion_cores;
  956. extern bool opt_minion_extra;
  957. extern char *opt_minion_freq;
  958. extern int opt_minion_freqchange;
  959. extern int opt_minion_freqpercent;
  960. extern bool opt_minion_idlecount;
  961. extern int opt_minion_ledcount;
  962. extern int opt_minion_ledlimit;
  963. extern bool opt_minion_noautofreq;
  964. extern bool opt_minion_overheat;
  965. extern int opt_minion_spidelay;
  966. extern char *opt_minion_spireset;
  967. extern int opt_minion_spisleep;
  968. extern int opt_minion_spiusec;
  969. extern char *opt_minion_temp;
  970. #endif
  971. #ifdef USE_USBUTILS
  972. extern char *opt_usb_select;
  973. extern int opt_usbdump;
  974. extern bool opt_usb_list_all;
  975. extern cgsem_t usb_resource_sem;
  976. #endif
  977. #ifdef USE_BITFORCE
  978. extern bool opt_bfl_noncerange;
  979. #endif
  980. extern int swork_id;
  981. #if LOCK_TRACKING
  982. extern pthread_mutex_t lockstat_lock;
  983. #endif
  984. extern pthread_rwlock_t netacc_lock;
  985. extern const uint32_t sha256_init_state[];
  986. #ifdef HAVE_LIBCURL
  987. extern json_t *json_web_config(const char *url);
  988. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  989. const char *rpc_req, bool, bool, int *,
  990. struct pool *pool, bool);
  991. struct pool;
  992. extern struct pool *opt_btcd;
  993. #endif
  994. extern const char *proxytype(proxytypes_t proxytype);
  995. extern char *get_proxy(char *url, struct pool *pool);
  996. extern void __bin2hex(char *s, const unsigned char *p, size_t len);
  997. extern char *bin2hex(const unsigned char *p, size_t len);
  998. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  999. typedef bool (*sha256_func)(struct thr_info*, const unsigned char *pmidstate,
  1000. unsigned char *pdata,
  1001. unsigned char *phash1, unsigned char *phash,
  1002. const unsigned char *ptarget,
  1003. uint32_t max_nonce,
  1004. uint32_t *last_nonce,
  1005. uint32_t nonce);
  1006. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  1007. extern const int max_scantime;
  1008. extern cglock_t control_lock;
  1009. extern pthread_mutex_t hash_lock;
  1010. extern pthread_mutex_t console_lock;
  1011. extern cglock_t ch_lock;
  1012. extern pthread_rwlock_t mining_thr_lock;
  1013. extern pthread_rwlock_t devices_lock;
  1014. extern pthread_mutex_t restart_lock;
  1015. extern pthread_cond_t restart_cond;
  1016. extern void clear_stratum_shares(struct pool *pool);
  1017. extern void clear_pool_work(struct pool *pool);
  1018. extern void set_target(unsigned char *dest_target, double diff);
  1019. #if defined (USE_AVALON2) || defined (USE_AVALON4) || defined (USE_AVALON7) || defined (USE_AVALON8) || defined (USE_AVALON_MINER) || defined (USE_HASHRATIO)
  1020. bool submit_nonce2_nonce(struct thr_info *thr, struct pool *pool, struct pool *real_pool,
  1021. uint32_t nonce2, uint32_t nonce, uint32_t ntime);
  1022. #endif
  1023. #ifdef USE_BITMAIN_SOC
  1024. void get_work_by_nonce2(struct thr_info *thr,
  1025. struct work **work,
  1026. struct pool *pool,
  1027. struct pool *real_pool,
  1028. uint64_t nonce2,
  1029. uint32_t version);
  1030. #endif
  1031. extern int restart_wait(struct thr_info *thr, unsigned int mstime);
  1032. extern void raise_cgminer(void);
  1033. extern void kill_work(void);
  1034. extern void reinit_device(struct cgpu_info *cgpu);
  1035. extern void api(int thr_id);
  1036. extern struct pool *current_pool(void);
  1037. extern int enabled_pools;
  1038. extern void get_intrange(char *arg, int *val1, int *val2);
  1039. extern bool detect_stratum(struct pool *pool, char *url);
  1040. extern void print_summary(void);
  1041. extern void adjust_quota_gcd(void);
  1042. extern struct pool *add_pool(void);
  1043. extern bool add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
  1044. #define MAX_DEVICES 4096
  1045. extern bool hotplug_mode;
  1046. extern int hotplug_time;
  1047. extern struct list_head scan_devices;
  1048. extern int nDevs;
  1049. extern int num_processors;
  1050. extern int hw_errors;
  1051. extern bool use_syslog;
  1052. extern bool opt_quiet;
  1053. extern struct thr_info *control_thr;
  1054. extern struct thr_info **mining_thr;
  1055. extern double total_secs;
  1056. extern int mining_threads;
  1057. extern int total_devices;
  1058. extern int zombie_devs;
  1059. extern struct cgpu_info **devices;
  1060. extern int total_pools;
  1061. extern struct pool **pools;
  1062. extern struct strategies strategies[];
  1063. extern enum pool_strategy pool_strategy;
  1064. extern int opt_rotate_period;
  1065. extern double rolling1, rolling5, rolling15;
  1066. extern double total_rolling;
  1067. extern double total_mhashes_done;
  1068. extern unsigned int new_blocks;
  1069. extern unsigned int found_blocks;
  1070. extern int64_t total_accepted, total_rejected, total_diff1;
  1071. extern int64_t total_getworks, total_stale, total_discarded;
  1072. extern double total_diff_accepted, total_diff_rejected, total_diff_stale;
  1073. extern unsigned int local_work;
  1074. extern unsigned int total_go, total_ro;
  1075. extern const int opt_cutofftemp;
  1076. extern int opt_log_interval;
  1077. extern uint64_t global_hashrate;
  1078. extern char current_hash[68];
  1079. extern double current_diff;
  1080. extern uint64_t best_diff;
  1081. extern struct timeval block_timeval;
  1082. extern char *workpadding;
  1083. #ifdef USE_BITMAIN_SOC
  1084. extern char displayed_hash_rate[16];
  1085. #define NONCE_BUFF 4096
  1086. extern char nonce_num10_string[NONCE_BUFF];
  1087. extern char nonce_num30_string[NONCE_BUFF];
  1088. extern char nonce_num60_string[NONCE_BUFF];
  1089. extern char g_miner_version[256];
  1090. extern char g_miner_compiletime[256];
  1091. extern char g_miner_type[256];
  1092. extern double new_total_mhashes_done;
  1093. extern double new_total_secs;
  1094. extern time_t total_tv_start_sys;
  1095. extern time_t total_tv_end_sys;
  1096. extern void writeInitLogFile(char *logstr);
  1097. #endif
  1098. struct curl_ent {
  1099. CURL *curl;
  1100. struct list_head node;
  1101. struct timeval tv;
  1102. };
  1103. /* Disabled needs to be the lowest enum as a freshly calloced value will then
  1104. * equal disabled */
  1105. enum pool_enable {
  1106. POOL_DISABLED,
  1107. POOL_ENABLED,
  1108. POOL_REJECTING,
  1109. };
  1110. struct stratum_work {
  1111. char *job_id;
  1112. unsigned char **merkle_bin;
  1113. bool clean;
  1114. double diff;
  1115. };
  1116. #define RBUFSIZE 8192
  1117. #define RECVSIZE (RBUFSIZE - 4)
  1118. struct pool {
  1119. int pool_no;
  1120. int prio;
  1121. int64_t accepted, rejected;
  1122. int seq_rejects;
  1123. int seq_getfails;
  1124. int solved;
  1125. int64_t diff1;
  1126. char diff[8];
  1127. int quota;
  1128. int quota_gcd;
  1129. int quota_used;
  1130. int works;
  1131. double diff_accepted;
  1132. double diff_rejected;
  1133. double diff_stale;
  1134. /* Vmask data */
  1135. bool vmask; /* Supports vmask */
  1136. uint32_t vmask_001[16];
  1137. char vmask_002[16][9];
  1138. int vmask_003[4];
  1139. bool submit_fail;
  1140. bool idle;
  1141. bool probed;
  1142. enum pool_enable enabled;
  1143. bool submit_old;
  1144. bool removed;
  1145. bool lp_started;
  1146. bool blocking;
  1147. char *hdr_path;
  1148. char *lp_url;
  1149. unsigned int getwork_requested;
  1150. unsigned int stale_shares;
  1151. unsigned int discarded_work;
  1152. unsigned int getfail_occasions;
  1153. unsigned int remotefail_occasions;
  1154. struct timeval tv_idle;
  1155. double utility;
  1156. int last_shares, shares;
  1157. char *rpc_req;
  1158. char *rpc_url;
  1159. char *rpc_userpass;
  1160. char *rpc_user, *rpc_pass;
  1161. proxytypes_t rpc_proxytype;
  1162. char *rpc_proxy;
  1163. pthread_mutex_t pool_lock;
  1164. cglock_t data_lock;
  1165. struct thread_q *submit_q;
  1166. struct thread_q *getwork_q;
  1167. pthread_t longpoll_thread;
  1168. pthread_t test_thread;
  1169. bool testing;
  1170. int curls;
  1171. pthread_cond_t cr_cond;
  1172. struct list_head curlring;
  1173. time_t last_share_time;
  1174. double last_share_diff;
  1175. uint64_t best_diff;
  1176. uint64_t bad_work;
  1177. struct cgminer_stats cgminer_stats;
  1178. struct cgminer_pool_stats cgminer_pool_stats;
  1179. /* The last block this particular pool knows about */
  1180. char prev_block[32];
  1181. /* Stratum variables */
  1182. char *stratum_url;
  1183. bool extranonce_subscribe;
  1184. char *stratum_port;
  1185. SOCKETTYPE sock;
  1186. char *sockbuf;
  1187. size_t sockbuf_size;
  1188. char *sockaddr_url; /* stripped url used for sockaddr */
  1189. char *sockaddr_proxy_url;
  1190. char *sockaddr_proxy_port;
  1191. char *nonce1;
  1192. unsigned char *nonce1bin;
  1193. uint64_t nonce2;
  1194. int n2size;
  1195. char *sessionid;
  1196. bool has_stratum;
  1197. bool stratum_active;
  1198. bool stratum_init;
  1199. bool stratum_notify;
  1200. struct stratum_work swork;
  1201. pthread_t stratum_sthread;
  1202. pthread_t stratum_rthread;
  1203. pthread_mutex_t stratum_lock;
  1204. struct thread_q *stratum_q;
  1205. int sshares; /* stratum shares submitted waiting on response */
  1206. /* GBT variables */
  1207. bool has_gbt;
  1208. cglock_t gbt_lock;
  1209. unsigned char previousblockhash[32];
  1210. unsigned char gbt_target[32];
  1211. char *coinbasetxn;
  1212. char *longpollid;
  1213. char *gbt_workid;
  1214. int gbt_expires;
  1215. uint32_t gbt_version;
  1216. uint32_t curtime;
  1217. uint32_t gbt_bits;
  1218. unsigned char *txn_hashes;
  1219. int gbt_txns;
  1220. int height;
  1221. bool gbt_solo;
  1222. unsigned char merklebin[16 * 32];
  1223. int transactions;
  1224. char *txn_data;
  1225. unsigned char scriptsig_base[100];
  1226. unsigned char script_pubkey[25 + 3];
  1227. int nValue;
  1228. CURL *gbt_curl;
  1229. bool gbt_curl_inuse;
  1230. /* Shared by both stratum & GBT */
  1231. size_t n1_len;
  1232. unsigned char *coinbase;
  1233. int coinbase_len;
  1234. int nonce2_offset;
  1235. unsigned char header_bin[128];
  1236. int merkles;
  1237. char prev_hash[68];
  1238. char bbversion[12];
  1239. char nbit[12];
  1240. char ntime[12];
  1241. double next_diff;
  1242. double diff_after;
  1243. double sdiff;
  1244. uint32_t current_height;
  1245. struct timeval tv_lastwork;
  1246. #ifdef USE_BITMAIN_SOC
  1247. bool support_vil;
  1248. int version_num;
  1249. int version[4];
  1250. #endif
  1251. };
  1252. #define GETWORK_MODE_TESTPOOL 'T'
  1253. #define GETWORK_MODE_POOL 'P'
  1254. #define GETWORK_MODE_LP 'L'
  1255. #define GETWORK_MODE_BENCHMARK 'B'
  1256. #define GETWORK_MODE_STRATUM 'S'
  1257. #define GETWORK_MODE_GBT 'G'
  1258. #define GETWORK_MODE_SOLO 'C'
  1259. struct work {
  1260. unsigned char data[128];
  1261. unsigned char midstate[32];
  1262. unsigned char midstate1[32];
  1263. unsigned char midstate2[32];
  1264. unsigned char midstate3[32];
  1265. unsigned char target[32];
  1266. unsigned char hash[32];
  1267. uint16_t micro_job_id;
  1268. /* This is the diff the device is currently aiming for and must be
  1269. * the minimum of work_difficulty & drv->max_diff */
  1270. double device_diff;
  1271. uint64_t share_diff;
  1272. int rolls;
  1273. int drv_rolllimit; /* How much the driver can roll ntime */
  1274. uint32_t nonce; /* For devices that hash sole work */
  1275. struct thr_info *thr;
  1276. int thr_id;
  1277. struct pool *pool;
  1278. struct timeval tv_staged;
  1279. bool mined;
  1280. bool clone;
  1281. bool cloned;
  1282. int rolltime;
  1283. bool longpoll;
  1284. bool stale;
  1285. bool mandatory;
  1286. bool block;
  1287. bool stratum;
  1288. char *job_id;
  1289. uint64_t nonce2;
  1290. size_t nonce2_len;
  1291. char *ntime;
  1292. double sdiff;
  1293. char *nonce1;
  1294. bool gbt;
  1295. char *coinbase;
  1296. int gbt_txns;
  1297. unsigned int work_block;
  1298. uint32_t id;
  1299. UT_hash_handle hh;
  1300. /* This is the diff work we're aiming to submit and should match the
  1301. * work->target binary */
  1302. double work_difficulty;
  1303. // Allow devices to identify work if multiple sub-devices
  1304. int subid;
  1305. // Allow devices to flag work for their own purposes
  1306. bool devflag;
  1307. // Allow devices to timestamp work for their own purposes
  1308. struct timeval tv_stamp;
  1309. struct timeval tv_getwork;
  1310. struct timeval tv_getwork_reply;
  1311. struct timeval tv_cloned;
  1312. struct timeval tv_work_start;
  1313. struct timeval tv_work_found;
  1314. char getwork_mode;
  1315. #ifdef USE_BITMAIN_SOC
  1316. int version;
  1317. #endif
  1318. };
  1319. #ifdef USE_MODMINER
  1320. struct modminer_fpga_state {
  1321. bool work_running;
  1322. struct work running_work;
  1323. struct timeval tv_workstart;
  1324. uint32_t hashes;
  1325. char next_work_cmd[46];
  1326. char fpgaid;
  1327. bool overheated;
  1328. bool new_work;
  1329. uint32_t shares;
  1330. uint32_t shares_last_hw;
  1331. uint32_t hw_errors;
  1332. uint32_t shares_to_good;
  1333. uint32_t timeout_fail;
  1334. uint32_t success_more;
  1335. struct timeval last_changed;
  1336. struct timeval last_nonce;
  1337. struct timeval first_work;
  1338. bool death_stage_one;
  1339. bool tried_two_byte_temp;
  1340. bool one_byte_temp;
  1341. };
  1342. #endif
  1343. #define TAILBUFSIZ 64
  1344. #define tailsprintf(buf, bufsiz, fmt, ...) do { \
  1345. char tmp13[TAILBUFSIZ]; \
  1346. size_t len13, buflen = strlen(buf); \
  1347. snprintf(tmp13, sizeof(tmp13), fmt, ##__VA_ARGS__); \
  1348. len13 = strlen(tmp13); \
  1349. if ((buflen + len13) >= bufsiz) \
  1350. quit(1, "tailsprintf buffer overflow in %s %s line %d", __FILE__, __func__, __LINE__); \
  1351. strcat(buf, tmp13); \
  1352. } while (0)
  1353. extern void get_datestamp(char *, size_t, struct timeval *);
  1354. extern void inc_hw_errors(struct thr_info *thr);
  1355. extern bool test_nonce(struct work *work, uint32_t nonce);
  1356. extern bool test_nonce_diff(struct work *work, uint32_t nonce, double diff);
  1357. extern bool submit_tested_work(struct thr_info *thr, struct work *work);
  1358. extern bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  1359. extern bool submit_noffset_nonce(struct thr_info *thr, struct work *work, uint32_t nonce,
  1360. int noffset);
  1361. extern int share_work_tdiff(struct cgpu_info *cgpu);
  1362. extern struct work *get_work(struct thr_info *thr, const int thr_id);
  1363. extern void __add_queued(struct cgpu_info *cgpu, struct work *work);
  1364. extern struct work *get_queued(struct cgpu_info *cgpu);
  1365. extern struct work *__get_queued(struct cgpu_info *cgpu);
  1366. extern void add_queued(struct cgpu_info *cgpu, struct work *work);
  1367. extern struct work *get_queue_work(struct thr_info *thr, struct cgpu_info *cgpu, int thr_id);
  1368. extern struct work *__find_work_bymidstate(struct work *que, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1369. extern struct work *find_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1370. extern struct work *clone_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1371. extern struct work *__find_work_byid(struct work *que, uint32_t id);
  1372. extern struct work *find_queued_work_byid(struct cgpu_info *cgpu, uint32_t id);
  1373. extern struct work *clone_queued_work_byid(struct cgpu_info *cgpu, uint32_t id);
  1374. extern void __work_completed(struct cgpu_info *cgpu, struct work *work);
  1375. extern int age_queued_work(struct cgpu_info *cgpu, double secs);
  1376. extern void work_completed(struct cgpu_info *cgpu, struct work *work);
  1377. extern struct work *take_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1378. extern void flush_queue(struct cgpu_info *cgpu);
  1379. extern void hash_driver_work(struct thr_info *mythr);
  1380. extern void hash_queued_work(struct thr_info *mythr);
  1381. extern void _wlog(const char *str);
  1382. extern void _wlogprint(const char *str);
  1383. extern int curses_int(const char *query);
  1384. extern char *curses_input(const char *query);
  1385. extern void kill_work(void);
  1386. extern void switch_pools(struct pool *selected);
  1387. extern void _discard_work(struct work **workptr, const char *file, const char *func, const int line);
  1388. #define discard_work(WORK) _discard_work(&(WORK), __FILE__, __func__, __LINE__)
  1389. extern void remove_pool(struct pool *pool);
  1390. extern void write_config(FILE *fcfg);
  1391. extern void zero_bestshare(void);
  1392. extern void zero_stats(void);
  1393. extern void default_save_file(char *filename);
  1394. extern bool log_curses_only(int prio, const char *datetime, const char *str);
  1395. extern void clear_logwin(void);
  1396. extern void logwin_update(void);
  1397. extern bool pool_tclear(struct pool *pool, bool *var);
  1398. extern void stratum_resumed(struct pool *pool);
  1399. extern void pool_died(struct pool *pool);
  1400. extern struct thread_q *tq_new(void);
  1401. extern void tq_free(struct thread_q *tq);
  1402. extern bool tq_push(struct thread_q *tq, void *data);
  1403. extern void *tq_pop(struct thread_q *tq);
  1404. extern void tq_freeze(struct thread_q *tq);
  1405. extern void tq_thaw(struct thread_q *tq);
  1406. extern bool successful_connect;
  1407. extern void adl(void);
  1408. extern void app_restart(void);
  1409. extern void roll_work(struct work *work);
  1410. extern void roll_work_ntime(struct work *work, int noffset);
  1411. extern struct work *make_clone(struct work *work);
  1412. extern void clean_work(struct work *work);
  1413. extern void _free_work(struct work **workptr, const char *file, const char *func, const int line);
  1414. #define free_work(WORK) _free_work(&(WORK), __FILE__, __func__, __LINE__)
  1415. extern void set_work_ntime(struct work *work, int ntime);
  1416. extern struct work *copy_work_noffset(struct work *base_work, int noffset);
  1417. #define copy_work(work_in) copy_work_noffset(work_in, 0)
  1418. extern uint64_t share_diff(const struct work *work);
  1419. extern struct thr_info *get_thread(int thr_id);
  1420. extern struct cgpu_info *get_devices(int id);
  1421. enum api_data_type {
  1422. API_ESCAPE,
  1423. API_STRING,
  1424. API_CONST,
  1425. API_UINT8,
  1426. API_INT16,
  1427. API_UINT16,
  1428. API_INT,
  1429. API_UINT,
  1430. API_UINT32,
  1431. API_HEX32,
  1432. API_UINT64,
  1433. API_INT64,
  1434. API_DOUBLE,
  1435. API_ELAPSED,
  1436. API_BOOL,
  1437. API_TIMEVAL,
  1438. API_TIME,
  1439. API_MHS,
  1440. API_MHTOTAL,
  1441. API_TEMP,
  1442. API_UTILITY,
  1443. API_FREQ,
  1444. API_VOLTS,
  1445. API_HS,
  1446. API_DIFF,
  1447. API_PERCENT,
  1448. API_AVG
  1449. };
  1450. struct api_data {
  1451. enum api_data_type type;
  1452. char *name;
  1453. void *data;
  1454. bool data_was_malloc;
  1455. struct api_data *prev;
  1456. struct api_data *next;
  1457. };
  1458. extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
  1459. extern struct api_data *api_add_string(struct api_data *root, char *name, char *data, bool copy_data);
  1460. extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
  1461. extern struct api_data *api_add_uint8(struct api_data *root, char *name, uint8_t *data, bool copy_data);
  1462. extern struct api_data *api_add_int16(struct api_data *root, char *name, uint16_t *data, bool copy_data);
  1463. extern struct api_data *api_add_uint16(struct api_data *root, char *name, uint16_t *data, bool copy_data);
  1464. extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
  1465. extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
  1466. extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  1467. extern struct api_data *api_add_hex32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  1468. extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
  1469. extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
  1470. extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
  1471. extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
  1472. extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
  1473. extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
  1474. extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
  1475. extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
  1476. extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
  1477. extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
  1478. extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
  1479. extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
  1480. extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
  1481. extern struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data);
  1482. extern struct api_data *api_add_percent(struct api_data *root, char *name, double *data, bool copy_data);
  1483. extern struct api_data *api_add_avg(struct api_data *root, char *name, float *data, bool copy_data);
  1484. #define ROOT_ADD_API(FUNC, NAME, VAR, BOOL) root = api_add_##FUNC(root, (NAME), &(VAR), (BOOL))
  1485. extern void dupalloc(struct cgpu_info *cgpu, int timelimit);
  1486. extern void dupcounters(struct cgpu_info *cgpu, uint64_t *checked, uint64_t *dups);
  1487. extern bool isdupnonce(struct cgpu_info *cgpu, struct work *work, uint32_t nonce);
  1488. #endif /* __MINER_H__ */