miner.h 50 KB

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