miner.h 37 KB

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