miner.h 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. #ifndef __MINER_H__
  2. #define __MINER_H__
  3. #include "config.h"
  4. #include <stdbool.h>
  5. #include <stdint.h>
  6. #include <sys/time.h>
  7. #include <pthread.h>
  8. #include <jansson.h>
  9. #include <curl/curl.h>
  10. #include "elist.h"
  11. #include "uthash.h"
  12. #include "logging.h"
  13. #include "util.h"
  14. #ifdef HAVE_OPENCL
  15. #ifdef __APPLE_CC__
  16. #include <OpenCL/opencl.h>
  17. #else
  18. #include <CL/cl.h>
  19. #endif
  20. #endif /* HAVE_OPENCL */
  21. #ifdef STDC_HEADERS
  22. # include <stdlib.h>
  23. # include <stddef.h>
  24. #else
  25. # ifdef HAVE_STDLIB_H
  26. # include <stdlib.h>
  27. # endif
  28. #endif
  29. #ifdef HAVE_ALLOCA_H
  30. # include <alloca.h>
  31. #elif defined __GNUC__
  32. # ifndef WIN32
  33. # define alloca __builtin_alloca
  34. # else
  35. # include <malloc.h>
  36. # endif
  37. #elif defined _AIX
  38. # define alloca __alloca
  39. #elif defined _MSC_VER
  40. # include <malloc.h>
  41. # define alloca _alloca
  42. #else
  43. # ifndef HAVE_ALLOCA
  44. # ifdef __cplusplus
  45. extern "C"
  46. # endif
  47. void *alloca (size_t);
  48. # endif
  49. #endif
  50. #ifdef __MINGW32__
  51. #include <windows.h>
  52. #include <io.h>
  53. static inline int fsync (int fd)
  54. {
  55. return (FlushFileBuffers ((HANDLE) _get_osfhandle (fd))) ? 0 : -1;
  56. }
  57. #ifndef MSG_DONTWAIT
  58. # define MSG_DONTWAIT 0x1000000
  59. #endif
  60. #endif /* __MINGW32__ */
  61. #if defined (__linux)
  62. #ifndef LINUX
  63. #define LINUX
  64. #endif
  65. #endif
  66. #ifdef WIN32
  67. #ifndef timersub
  68. #define timersub(a, b, result) \
  69. do { \
  70. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  71. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  72. if ((result)->tv_usec < 0) { \
  73. --(result)->tv_sec; \
  74. (result)->tv_usec += 1000000; \
  75. } \
  76. } while (0)
  77. #endif
  78. #ifndef timeradd
  79. # define timeradd(a, b, result) \
  80. do { \
  81. (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
  82. (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
  83. if ((result)->tv_usec >= 1000000) \
  84. { \
  85. ++(result)->tv_sec; \
  86. (result)->tv_usec -= 1000000; \
  87. } \
  88. } while (0)
  89. #endif
  90. #endif
  91. #ifdef HAVE_ADL
  92. #include "ADL_SDK/adl_sdk.h"
  93. #endif
  94. #ifdef HAVE_LIBUSB
  95. #include <libusb.h>
  96. #endif
  97. #ifdef USE_ZTEX
  98. #include "libztex.h"
  99. #endif
  100. #ifdef USE_MODMINER
  101. #include "usbutils.h"
  102. #endif
  103. #if !defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
  104. #define bswap_16 __builtin_bswap16
  105. #define bswap_32 __builtin_bswap32
  106. #define bswap_64 __builtin_bswap64
  107. #else
  108. #if HAVE_BYTESWAP_H
  109. #include <byteswap.h>
  110. #elif defined(USE_SYS_ENDIAN_H)
  111. #include <sys/endian.h>
  112. #elif defined(__APPLE__)
  113. #include <libkern/OSByteOrder.h>
  114. #define bswap_16 OSSwapInt16
  115. #define bswap_32 OSSwapInt32
  116. #define bswap_64 OSSwapInt64
  117. #else
  118. #define bswap_16(value) \
  119. ((((value) & 0xff) << 8) | ((value) >> 8))
  120. #define bswap_32(value) \
  121. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  122. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  123. #define bswap_64(value) \
  124. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  125. << 32) | \
  126. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  127. #endif
  128. #endif /* !defined(__GLXBYTEORDER_H__) */
  129. /* This assumes htobe32 is a macro in endian.h, and if it doesn't exist, then
  130. * htobe64 also won't exist */
  131. #ifndef htobe32
  132. # if __BYTE_ORDER == __LITTLE_ENDIAN
  133. # define htole16(x) (x)
  134. # define htole32(x) (x)
  135. # define be32toh(x) bswap_32(x)
  136. # define be64toh(x) bswap_64(x)
  137. # define htobe32(x) bswap_32(x)
  138. # define htobe64(x) bswap_64(x)
  139. # elif __BYTE_ORDER == __BIG_ENDIAN
  140. # define htole16(x) bswap_16(x)
  141. # define htole32(x) bswap_32(x)
  142. # define be32toh(x) (x)
  143. # define be64toh(x) (x)
  144. # define htobe32(x) (x)
  145. # define htobe64(x) (x)
  146. #else
  147. #error UNKNOWN BYTE ORDER
  148. #endif
  149. #endif
  150. #undef unlikely
  151. #undef likely
  152. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  153. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  154. #define likely(expr) (__builtin_expect(!!(expr), 1))
  155. #else
  156. #define unlikely(expr) (expr)
  157. #define likely(expr) (expr)
  158. #endif
  159. #define __maybe_unused __attribute__((unused))
  160. #define uninitialised_var(x) x = x
  161. #if defined(__i386__)
  162. #define WANT_CRYPTOPP_ASM32
  163. #endif
  164. #ifndef ARRAY_SIZE
  165. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  166. #endif
  167. #ifdef MIPSEB
  168. #ifndef roundl
  169. #define roundl(x) (long double)((long long)((x==0)?0.0:((x)+((x)>0)?0.5:-0.5)))
  170. #endif
  171. #endif
  172. enum alive {
  173. LIFE_WELL,
  174. LIFE_SICK,
  175. LIFE_DEAD,
  176. LIFE_NOSTART,
  177. LIFE_INIT,
  178. };
  179. enum pool_strategy {
  180. POOL_FAILOVER,
  181. POOL_ROUNDROBIN,
  182. POOL_ROTATE,
  183. POOL_LOADBALANCE,
  184. POOL_BALANCE,
  185. };
  186. #define TOP_STRATEGY (POOL_BALANCE)
  187. struct strategies {
  188. const char *s;
  189. };
  190. struct cgpu_info;
  191. #ifdef HAVE_ADL
  192. struct gpu_adl {
  193. ADLTemperature lpTemperature;
  194. int iAdapterIndex;
  195. int lpAdapterID;
  196. int iBusNumber;
  197. char strAdapterName[256];
  198. ADLPMActivity lpActivity;
  199. ADLODParameters lpOdParameters;
  200. ADLODPerformanceLevels *DefPerfLev;
  201. ADLFanSpeedInfo lpFanSpeedInfo;
  202. ADLFanSpeedValue lpFanSpeedValue;
  203. ADLFanSpeedValue DefFanSpeedValue;
  204. int iEngineClock;
  205. int iMemoryClock;
  206. int iVddc;
  207. int iPercentage;
  208. bool autofan;
  209. bool autoengine;
  210. bool managed; /* Were the values ever changed on this card */
  211. int lastengine;
  212. int lasttemp;
  213. int targetfan;
  214. int targettemp;
  215. int overtemp;
  216. int minspeed;
  217. int maxspeed;
  218. int gpu;
  219. bool has_fanspeed;
  220. struct gpu_adl *twin;
  221. };
  222. #endif
  223. struct api_data;
  224. struct thr_info;
  225. struct work;
  226. struct device_api {
  227. char *dname;
  228. char *name;
  229. // API-global functions
  230. void (*api_detect)();
  231. // Device-specific functions
  232. void (*reinit_device)(struct cgpu_info *);
  233. void (*get_statline_before)(char *, struct cgpu_info *);
  234. void (*get_statline)(char *, struct cgpu_info *);
  235. struct api_data *(*get_api_stats)(struct cgpu_info *);
  236. bool (*get_stats)(struct cgpu_info *);
  237. void (*identify_device)(struct cgpu_info *); // e.g. to flash a led
  238. // Thread-specific functions
  239. bool (*thread_prepare)(struct thr_info *);
  240. uint64_t (*can_limit_work)(struct thr_info *);
  241. bool (*thread_init)(struct thr_info *);
  242. bool (*prepare_work)(struct thr_info *, struct work *);
  243. int64_t (*scanhash)(struct thr_info *, struct work *, int64_t);
  244. void (*hw_error)(struct thr_info *);
  245. void (*thread_shutdown)(struct thr_info *);
  246. void (*thread_enable)(struct thr_info *);
  247. };
  248. enum dev_enable {
  249. DEV_ENABLED,
  250. DEV_DISABLED,
  251. DEV_RECOVER,
  252. };
  253. enum cl_kernels {
  254. KL_NONE,
  255. KL_POCLBM,
  256. KL_PHATK,
  257. KL_DIAKGCN,
  258. KL_DIABLO,
  259. KL_SCRYPT,
  260. };
  261. enum dev_reason {
  262. REASON_THREAD_FAIL_INIT,
  263. REASON_THREAD_ZERO_HASH,
  264. REASON_THREAD_FAIL_QUEUE,
  265. REASON_DEV_SICK_IDLE_60,
  266. REASON_DEV_DEAD_IDLE_600,
  267. REASON_DEV_NOSTART,
  268. REASON_DEV_OVER_HEAT,
  269. REASON_DEV_THERMAL_CUTOFF,
  270. REASON_DEV_COMMS_ERROR,
  271. REASON_DEV_THROTTLE,
  272. };
  273. #define REASON_NONE "None"
  274. #define REASON_THREAD_FAIL_INIT_STR "Thread failed to init"
  275. #define REASON_THREAD_ZERO_HASH_STR "Thread got zero hashes"
  276. #define REASON_THREAD_FAIL_QUEUE_STR "Thread failed to queue work"
  277. #define REASON_DEV_SICK_IDLE_60_STR "Device idle for 60s"
  278. #define REASON_DEV_DEAD_IDLE_600_STR "Device dead - idle for 600s"
  279. #define REASON_DEV_NOSTART_STR "Device failed to start"
  280. #define REASON_DEV_OVER_HEAT_STR "Device over heated"
  281. #define REASON_DEV_THERMAL_CUTOFF_STR "Device reached thermal cutoff"
  282. #define REASON_DEV_COMMS_ERROR_STR "Device comms error"
  283. #define REASON_DEV_THROTTLE_STR "Device throttle"
  284. #define REASON_UNKNOWN_STR "Unknown reason - code bug"
  285. #define MIN_SEC_UNSET 99999999
  286. struct cgminer_stats {
  287. uint32_t getwork_calls;
  288. struct timeval getwork_wait;
  289. struct timeval getwork_wait_max;
  290. struct timeval getwork_wait_min;
  291. };
  292. // Just the actual network getworks to the pool
  293. struct cgminer_pool_stats {
  294. uint32_t getwork_calls;
  295. uint32_t getwork_attempts;
  296. struct timeval getwork_wait;
  297. struct timeval getwork_wait_max;
  298. struct timeval getwork_wait_min;
  299. double getwork_wait_rolling;
  300. bool hadrolltime;
  301. bool canroll;
  302. bool hadexpire;
  303. uint32_t rolltime;
  304. double min_diff;
  305. double max_diff;
  306. double last_diff;
  307. uint32_t min_diff_count;
  308. uint32_t max_diff_count;
  309. };
  310. struct cgpu_info {
  311. int cgminer_id;
  312. struct device_api *api;
  313. int device_id;
  314. char *name;
  315. char *device_path;
  316. FILE *device_file;
  317. union {
  318. #ifdef USE_ZTEX
  319. struct libztex_device *device_ztex;
  320. #endif
  321. #ifdef USE_MODMINER
  322. struct cg_usb_device *usbdev;
  323. #endif
  324. int device_fd;
  325. };
  326. #ifdef USE_MODMINER
  327. int usbstat;
  328. char fpgaid;
  329. unsigned char clock;
  330. pthread_mutex_t *modminer_mutex;
  331. #endif
  332. #ifdef USE_BITFORCE
  333. struct timeval work_start_tv;
  334. unsigned int wait_ms;
  335. unsigned int sleep_ms;
  336. double avg_wait_f;
  337. unsigned int avg_wait_d;
  338. uint32_t nonces;
  339. bool nonce_range;
  340. bool polling;
  341. bool flash_led;
  342. pthread_mutex_t device_mutex;
  343. #endif
  344. enum dev_enable deven;
  345. int accepted;
  346. int rejected;
  347. int hw_errors;
  348. double rolling;
  349. double total_mhashes;
  350. double utility;
  351. enum alive status;
  352. char init[40];
  353. struct timeval last_message_tv;
  354. int threads;
  355. struct thr_info **thr;
  356. int64_t max_hashes;
  357. const char *kname;
  358. #ifdef HAVE_OPENCL
  359. bool mapped;
  360. int virtual_gpu;
  361. int virtual_adl;
  362. int intensity;
  363. bool dynamic;
  364. cl_uint vwidth;
  365. size_t work_size;
  366. enum cl_kernels kernel;
  367. cl_ulong max_alloc;
  368. #ifdef USE_SCRYPT
  369. int opt_lg, lookup_gap;
  370. size_t opt_tc, thread_concurrency;
  371. size_t shaders;
  372. #endif
  373. struct timeval tv_gpustart;
  374. int intervals;
  375. #endif
  376. bool new_work;
  377. float temp;
  378. int cutofftemp;
  379. #ifdef HAVE_ADL
  380. bool has_adl;
  381. struct gpu_adl adl;
  382. int gpu_engine;
  383. int min_engine;
  384. int gpu_fan;
  385. int min_fan;
  386. int gpu_memclock;
  387. int gpu_memdiff;
  388. int gpu_powertune;
  389. float gpu_vddc;
  390. #endif
  391. int diff1;
  392. double diff_accepted;
  393. double diff_rejected;
  394. int last_share_pool;
  395. time_t last_share_pool_time;
  396. double last_share_diff;
  397. time_t device_last_well;
  398. time_t device_last_not_well;
  399. enum dev_reason device_not_well_reason;
  400. int thread_fail_init_count;
  401. int thread_zero_hash_count;
  402. int thread_fail_queue_count;
  403. int dev_sick_idle_60_count;
  404. int dev_dead_idle_600_count;
  405. int dev_nostart_count;
  406. int dev_over_heat_count; // It's a warning but worth knowing
  407. int dev_thermal_cutoff_count;
  408. int dev_comms_error_count;
  409. int dev_throttle_count;
  410. struct cgminer_stats cgminer_stats;
  411. };
  412. extern bool add_cgpu(struct cgpu_info*);
  413. struct thread_q {
  414. struct list_head q;
  415. bool frozen;
  416. pthread_mutex_t mutex;
  417. pthread_cond_t cond;
  418. };
  419. struct thr_info {
  420. int id;
  421. int device_thread;
  422. bool primary_thread;
  423. pthread_t pth;
  424. struct thread_q *q;
  425. struct cgpu_info *cgpu;
  426. void *cgpu_data;
  427. struct timeval last;
  428. struct timeval sick;
  429. bool pause;
  430. bool getwork;
  431. double rolling;
  432. bool work_restart;
  433. };
  434. extern int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  435. extern void thr_info_cancel(struct thr_info *thr);
  436. extern void thr_info_freeze(struct thr_info *thr);
  437. extern void nmsleep(unsigned int msecs);
  438. extern double us_tdiff(struct timeval *end, struct timeval *start);
  439. extern double tdiff(struct timeval *end, struct timeval *start);
  440. struct string_elist {
  441. char *string;
  442. bool free_me;
  443. struct list_head list;
  444. };
  445. static inline void string_elist_add(const char *s, struct list_head *head)
  446. {
  447. struct string_elist *n;
  448. n = calloc(1, sizeof(*n));
  449. n->string = strdup(s);
  450. n->free_me = true;
  451. list_add_tail(&n->list, head);
  452. }
  453. static inline void string_elist_del(struct string_elist *item)
  454. {
  455. if (item->free_me)
  456. free(item->string);
  457. list_del(&item->list);
  458. }
  459. static inline uint32_t swab32(uint32_t v)
  460. {
  461. return bswap_32(v);
  462. }
  463. static inline void swap256(void *dest_p, const void *src_p)
  464. {
  465. uint32_t *dest = dest_p;
  466. const uint32_t *src = src_p;
  467. dest[0] = src[7];
  468. dest[1] = src[6];
  469. dest[2] = src[5];
  470. dest[3] = src[4];
  471. dest[4] = src[3];
  472. dest[5] = src[2];
  473. dest[6] = src[1];
  474. dest[7] = src[0];
  475. }
  476. static inline void swab256(void *dest_p, const void *src_p)
  477. {
  478. uint32_t *dest = dest_p;
  479. const uint32_t *src = src_p;
  480. dest[0] = swab32(src[7]);
  481. dest[1] = swab32(src[6]);
  482. dest[2] = swab32(src[5]);
  483. dest[3] = swab32(src[4]);
  484. dest[4] = swab32(src[3]);
  485. dest[5] = swab32(src[2]);
  486. dest[6] = swab32(src[1]);
  487. dest[7] = swab32(src[0]);
  488. }
  489. static inline void flip32(void *dest_p, const void *src_p)
  490. {
  491. uint32_t *dest = dest_p;
  492. const uint32_t *src = src_p;
  493. int i;
  494. for (i = 0; i < 8; i++)
  495. dest[i] = swab32(src[i]);
  496. }
  497. static inline void flip80(void *dest_p, const void *src_p)
  498. {
  499. uint32_t *dest = dest_p;
  500. const uint32_t *src = src_p;
  501. int i;
  502. for (i = 0; i < 20; i++)
  503. dest[i] = swab32(src[i]);
  504. }
  505. extern void quit(int status, const char *format, ...);
  506. static inline void mutex_lock(pthread_mutex_t *lock)
  507. {
  508. if (unlikely(pthread_mutex_lock(lock)))
  509. quit(1, "WTF MUTEX ERROR ON LOCK!");
  510. }
  511. static inline void mutex_unlock(pthread_mutex_t *lock)
  512. {
  513. if (unlikely(pthread_mutex_unlock(lock)))
  514. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  515. }
  516. static inline int mutex_trylock(pthread_mutex_t *lock)
  517. {
  518. return pthread_mutex_trylock(lock);
  519. }
  520. static inline void wr_lock(pthread_rwlock_t *lock)
  521. {
  522. if (unlikely(pthread_rwlock_wrlock(lock)))
  523. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  524. }
  525. static inline void rd_lock(pthread_rwlock_t *lock)
  526. {
  527. if (unlikely(pthread_rwlock_rdlock(lock)))
  528. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  529. }
  530. static inline void rw_unlock(pthread_rwlock_t *lock)
  531. {
  532. if (unlikely(pthread_rwlock_unlock(lock)))
  533. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  534. }
  535. static inline void rd_unlock(pthread_rwlock_t *lock)
  536. {
  537. rw_unlock(lock);
  538. }
  539. static inline void wr_unlock(pthread_rwlock_t *lock)
  540. {
  541. rw_unlock(lock);
  542. }
  543. static inline void mutex_init(pthread_mutex_t *lock)
  544. {
  545. if (unlikely(pthread_mutex_init(lock, NULL)))
  546. quit(1, "Failed to pthread_mutex_init");
  547. }
  548. static inline void rwlock_init(pthread_rwlock_t *lock)
  549. {
  550. if (unlikely(pthread_rwlock_init(lock, NULL)))
  551. quit(1, "Failed to pthread_rwlock_init");
  552. }
  553. struct pool;
  554. extern bool opt_protocol;
  555. extern bool have_longpoll;
  556. extern char *opt_kernel_path;
  557. extern char *opt_socks_proxy;
  558. extern char *cgminer_path;
  559. extern bool opt_fail_only;
  560. extern bool opt_autofan;
  561. extern bool opt_autoengine;
  562. extern bool use_curses;
  563. extern char *opt_api_allow;
  564. extern char *opt_api_groups;
  565. extern char *opt_api_description;
  566. extern int opt_api_port;
  567. extern bool opt_api_listen;
  568. extern bool opt_api_network;
  569. extern bool opt_delaynet;
  570. extern bool opt_restart;
  571. extern char *opt_icarus_options;
  572. extern char *opt_icarus_timing;
  573. extern bool opt_worktime;
  574. #ifdef HAVE_LIBUSB
  575. extern int opt_usbdump;
  576. #endif
  577. #ifdef USE_BITFORCE
  578. extern bool opt_bfl_noncerange;
  579. #endif
  580. extern int swork_id;
  581. extern pthread_rwlock_t netacc_lock;
  582. extern const uint32_t sha256_init_state[];
  583. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  584. const char *rpc_req, bool, bool, int *,
  585. struct pool *pool, bool);
  586. extern const char *proxytype(curl_proxytype proxytype);
  587. extern char *get_proxy(char *url, struct pool *pool);
  588. extern char *bin2hex(const unsigned char *p, size_t len);
  589. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  590. typedef bool (*sha256_func)(struct thr_info*, const unsigned char *pmidstate,
  591. unsigned char *pdata,
  592. unsigned char *phash1, unsigned char *phash,
  593. const unsigned char *ptarget,
  594. uint32_t max_nonce,
  595. uint32_t *last_nonce,
  596. uint32_t nonce);
  597. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  598. extern int opt_queue;
  599. extern int opt_scantime;
  600. extern int opt_expiry;
  601. #ifdef HAVE_LIBUSB
  602. extern pthread_mutex_t cgusb_lock;
  603. #endif
  604. extern pthread_mutex_t hash_lock;
  605. extern pthread_mutex_t console_lock;
  606. extern pthread_mutex_t ch_lock;
  607. extern pthread_mutex_t restart_lock;
  608. extern pthread_cond_t restart_cond;
  609. extern void thread_reportin(struct thr_info *thr);
  610. extern int restart_wait(unsigned int mstime);
  611. extern void kill_work(void);
  612. extern void reinit_device(struct cgpu_info *cgpu);
  613. #ifdef HAVE_ADL
  614. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  615. extern int set_fanspeed(int gpu, int iFanSpeed);
  616. extern int set_vddc(int gpu, float fVddc);
  617. extern int set_engineclock(int gpu, int iEngineClock);
  618. extern int set_memoryclock(int gpu, int iMemoryClock);
  619. #endif
  620. extern void api(int thr_id);
  621. extern struct pool *current_pool(void);
  622. extern int enabled_pools;
  623. extern bool detect_stratum(struct pool *pool, char *url);
  624. extern struct pool *add_pool(void);
  625. extern void add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
  626. #define MAX_GPUDEVICES 16
  627. #define MIN_INTENSITY -10
  628. #define _MIN_INTENSITY_STR "-10"
  629. #ifdef USE_SCRYPT
  630. #define MAX_INTENSITY 20
  631. #define _MAX_INTENSITY_STR "20"
  632. #else
  633. #define MAX_INTENSITY 14
  634. #define _MAX_INTENSITY_STR "14"
  635. #endif
  636. extern struct list_head scan_devices;
  637. extern int nDevs;
  638. extern int opt_n_threads;
  639. extern int num_processors;
  640. extern int hw_errors;
  641. extern bool use_syslog;
  642. extern bool opt_quiet;
  643. extern struct thr_info *thr_info;
  644. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  645. extern int gpu_threads;
  646. #ifdef USE_SCRYPT
  647. extern bool opt_scrypt;
  648. #else
  649. #define opt_scrypt (0)
  650. #endif
  651. extern double total_secs;
  652. extern int mining_threads;
  653. extern struct cgpu_info *cpus;
  654. extern int total_devices;
  655. extern struct cgpu_info **devices;
  656. extern int total_pools;
  657. extern struct pool **pools;
  658. extern const char *algo_names[];
  659. extern enum sha256_algos opt_algo;
  660. extern struct strategies strategies[];
  661. extern enum pool_strategy pool_strategy;
  662. extern int opt_rotate_period;
  663. extern double total_mhashes_done;
  664. extern unsigned int new_blocks;
  665. extern unsigned int found_blocks;
  666. extern int total_accepted, total_rejected, total_diff1;;
  667. extern int total_getworks, total_stale, total_discarded;
  668. extern double total_diff_accepted, total_diff_rejected, total_diff_stale;
  669. extern unsigned int local_work;
  670. extern unsigned int total_go, total_ro;
  671. extern const int opt_cutofftemp;
  672. extern int opt_log_interval;
  673. extern unsigned long long global_hashrate;
  674. extern char *current_fullhash;
  675. extern uint64_t best_diff;
  676. extern struct timeval block_timeval;
  677. #ifdef HAVE_OPENCL
  678. typedef struct {
  679. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  680. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  681. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  682. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  683. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  684. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  685. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  686. cl_uint W16; cl_uint W17; cl_uint W2;
  687. cl_uint PreVal4; cl_uint T1;
  688. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  689. cl_uint PreVal4addT1; cl_uint T1substate0;
  690. cl_uint PreVal4_2;
  691. cl_uint PreVal0;
  692. cl_uint PreW18;
  693. cl_uint PreW19;
  694. cl_uint PreW31;
  695. cl_uint PreW32;
  696. /* For diakgcn */
  697. cl_uint B1addK6, PreVal0addK7, W16addK16, W17addK17;
  698. cl_uint zeroA, zeroB;
  699. cl_uint oneA, twoA, threeA, fourA, fiveA, sixA, sevenA;
  700. #ifdef USE_SCRYPT
  701. struct work *work;
  702. #endif
  703. } dev_blk_ctx;
  704. #else
  705. typedef struct {
  706. uint32_t nonce;
  707. } dev_blk_ctx;
  708. #endif
  709. struct curl_ent {
  710. CURL *curl;
  711. struct list_head node;
  712. struct timeval tv;
  713. };
  714. /* Disabled needs to be the lowest enum as a freshly calloced value will then
  715. * equal disabled */
  716. enum pool_enable {
  717. POOL_DISABLED,
  718. POOL_ENABLED,
  719. POOL_REJECTING,
  720. };
  721. struct stratum_work {
  722. char *job_id;
  723. char *prev_hash;
  724. char *coinbase1;
  725. char *coinbase2;
  726. char **merkle;
  727. char *bbversion;
  728. char *nbit;
  729. char *ntime;
  730. bool clean;
  731. int merkles;
  732. double diff;
  733. };
  734. #define RECVSIZE 8192
  735. #define RBUFSIZE (RECVSIZE + 4)
  736. struct pool {
  737. int pool_no;
  738. int prio;
  739. int accepted, rejected;
  740. int seq_rejects;
  741. int seq_getfails;
  742. int solved;
  743. int diff1;
  744. double diff_accepted;
  745. double diff_rejected;
  746. double diff_stale;
  747. bool submit_fail;
  748. bool idle;
  749. bool lagging;
  750. bool probed;
  751. enum pool_enable enabled;
  752. bool submit_old;
  753. bool removed;
  754. bool lp_started;
  755. char *hdr_path;
  756. char *lp_url;
  757. unsigned int getwork_requested;
  758. unsigned int stale_shares;
  759. unsigned int discarded_work;
  760. unsigned int getfail_occasions;
  761. unsigned int remotefail_occasions;
  762. struct timeval tv_idle;
  763. double utility;
  764. int last_shares, shares;
  765. char *rpc_req;
  766. char *rpc_url;
  767. char *rpc_userpass;
  768. char *rpc_user, *rpc_pass;
  769. curl_proxytype rpc_proxytype;
  770. char *rpc_proxy;
  771. pthread_mutex_t pool_lock;
  772. struct thread_q *submit_q;
  773. struct thread_q *getwork_q;
  774. pthread_t longpoll_thread;
  775. pthread_t submit_thread;
  776. pthread_t getwork_thread;
  777. int curls;
  778. pthread_cond_t cr_cond;
  779. struct list_head curlring;
  780. time_t last_share_time;
  781. double last_share_diff;
  782. struct cgminer_stats cgminer_stats;
  783. struct cgminer_pool_stats cgminer_pool_stats;
  784. /* Stratum variables */
  785. char *stratum_url;
  786. char *stratum_port;
  787. CURL *stratum_curl;
  788. SOCKETTYPE sock;
  789. char sockbuf[RBUFSIZE];
  790. char *sockaddr_url; /* stripped url used for sockaddr */
  791. char *nonce1;
  792. uint32_t nonce2;
  793. int n2size;
  794. bool has_stratum;
  795. bool stratum_active;
  796. bool stratum_auth;
  797. struct stratum_work swork;
  798. pthread_t stratum_thread;
  799. pthread_mutex_t stratum_lock;
  800. /* GBT variables */
  801. bool has_gbt;
  802. pthread_mutex_t gbt_lock;
  803. unsigned char previousblockhash[32];
  804. unsigned char gbt_target[32];
  805. char *coinbasetxn;
  806. char *longpollid;
  807. char *gbt_workid;
  808. int gbt_expires;
  809. uint32_t gbt_version;
  810. uint32_t curtime;
  811. uint32_t gbt_bits;
  812. unsigned char *gbt_coinbase;
  813. unsigned char *txn_hashes;
  814. int gbt_txns;
  815. int coinbase_len;
  816. struct timeval tv_lastwork;
  817. };
  818. #define GETWORK_MODE_TESTPOOL 'T'
  819. #define GETWORK_MODE_POOL 'P'
  820. #define GETWORK_MODE_LP 'L'
  821. #define GETWORK_MODE_BENCHMARK 'B'
  822. #define GETWORK_MODE_STRATUM 'S'
  823. #define GETWORK_MODE_GBT 'G'
  824. struct work {
  825. unsigned char data[128];
  826. unsigned char midstate[32];
  827. unsigned char target[32];
  828. unsigned char hash[32];
  829. uint64_t outputhash;
  830. int rolls;
  831. dev_blk_ctx blk;
  832. struct thr_info *thr;
  833. int thr_id;
  834. struct pool *pool;
  835. struct timeval tv_staged;
  836. bool mined;
  837. bool clone;
  838. bool cloned;
  839. int rolltime;
  840. bool longpoll;
  841. bool stale;
  842. bool mandatory;
  843. bool block;
  844. bool queued;
  845. bool stratum;
  846. char *job_id;
  847. char *nonce2;
  848. char *ntime;
  849. double sdiff;
  850. bool gbt;
  851. char *gbt_coinbase;
  852. int gbt_txns;
  853. unsigned int work_block;
  854. int id;
  855. UT_hash_handle hh;
  856. double work_difficulty;
  857. struct timeval tv_getwork;
  858. struct timeval tv_getwork_reply;
  859. struct timeval tv_cloned;
  860. struct timeval tv_work_start;
  861. struct timeval tv_work_found;
  862. char getwork_mode;
  863. };
  864. #ifdef USE_MODMINER
  865. struct modminer_fpga_state {
  866. bool work_running;
  867. struct work running_work;
  868. struct timeval tv_workstart;
  869. uint32_t hashes;
  870. char next_work_cmd[46];
  871. char fpgaid;
  872. bool overheated;
  873. bool new_work;
  874. uint32_t shares;
  875. uint32_t shares_last_hw;
  876. uint32_t hw_errors;
  877. uint32_t shares_to_good;
  878. struct timeval last_changed;
  879. struct timeval last_nonce;
  880. struct timeval first_work;
  881. bool death_stage_one;
  882. bool tried_two_byte_temp;
  883. bool one_byte_temp;
  884. };
  885. #endif
  886. extern void get_datestamp(char *, struct timeval *);
  887. extern void submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  888. extern void tailsprintf(char *f, const char *fmt, ...);
  889. extern void wlogprint(const char *f, ...);
  890. extern int curses_int(const char *query);
  891. extern char *curses_input(const char *query);
  892. extern void kill_work(void);
  893. extern void switch_pools(struct pool *selected);
  894. extern void remove_pool(struct pool *pool);
  895. extern void write_config(FILE *fcfg);
  896. extern void default_save_file(char *filename);
  897. extern bool log_curses_only(int prio, const char *f, va_list ap);
  898. extern void clear_logwin(void);
  899. extern bool pool_tclear(struct pool *pool, bool *var);
  900. extern struct thread_q *tq_new(void);
  901. extern void tq_free(struct thread_q *tq);
  902. extern bool tq_push(struct thread_q *tq, void *data);
  903. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  904. extern void tq_freeze(struct thread_q *tq);
  905. extern void tq_thaw(struct thread_q *tq);
  906. extern bool successful_connect;
  907. extern void adl(void);
  908. extern void app_restart(void);
  909. extern void clean_work(struct work *work);
  910. extern void free_work(struct work *work);
  911. extern void __copy_work(struct work *work, struct work *base_work);
  912. extern struct work *copy_work(struct work *base_work);
  913. enum api_data_type {
  914. API_ESCAPE,
  915. API_STRING,
  916. API_CONST,
  917. API_INT,
  918. API_UINT,
  919. API_UINT32,
  920. API_UINT64,
  921. API_DOUBLE,
  922. API_ELAPSED,
  923. API_BOOL,
  924. API_TIMEVAL,
  925. API_TIME,
  926. API_MHS,
  927. API_MHTOTAL,
  928. API_TEMP,
  929. API_UTILITY,
  930. API_FREQ,
  931. API_VOLTS,
  932. API_HS,
  933. API_DIFF
  934. };
  935. struct api_data {
  936. enum api_data_type type;
  937. char *name;
  938. void *data;
  939. bool data_was_malloc;
  940. struct api_data *prev;
  941. struct api_data *next;
  942. };
  943. extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
  944. extern struct api_data *api_add_string(struct api_data *root, char *name, char *data, bool copy_data);
  945. extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
  946. extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
  947. extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
  948. extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  949. extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
  950. extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
  951. extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
  952. extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
  953. extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
  954. extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
  955. extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
  956. extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
  957. extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
  958. extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
  959. extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
  960. extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
  961. extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
  962. extern struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data);
  963. #endif /* __MINER_H__ */