miner.h 37 KB

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