miner.h 25 KB

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