miner.h 26 KB

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