miner.h 25 KB

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