miner.h 25 KB

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