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