miner.h 25 KB

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