miner.h 29 KB

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