miner.h 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  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_USBUTILS
  104. #include "usbutils.h"
  105. #endif
  106. #if (!defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))) \
  107. || (defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)))
  108. #ifndef bswap_16
  109. #define bswap_16 __builtin_bswap16
  110. #define bswap_32 __builtin_bswap32
  111. #define bswap_64 __builtin_bswap64
  112. #endif
  113. #else
  114. #if HAVE_BYTESWAP_H
  115. #include <byteswap.h>
  116. #elif defined(USE_SYS_ENDIAN_H)
  117. #include <sys/endian.h>
  118. #elif defined(__APPLE__)
  119. #include <libkern/OSByteOrder.h>
  120. #define bswap_16 OSSwapInt16
  121. #define bswap_32 OSSwapInt32
  122. #define bswap_64 OSSwapInt64
  123. #else
  124. #define bswap_16(value) \
  125. ((((value) & 0xff) << 8) | ((value) >> 8))
  126. #define bswap_32(value) \
  127. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  128. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  129. #define bswap_64(value) \
  130. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  131. << 32) | \
  132. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  133. #endif
  134. #endif /* !defined(__GLXBYTEORDER_H__) */
  135. /* This assumes htobe32 is a macro in endian.h, and if it doesn't exist, then
  136. * htobe64 also won't exist */
  137. #ifndef htobe32
  138. # if __BYTE_ORDER == __LITTLE_ENDIAN
  139. # define htole16(x) (x)
  140. # define htole32(x) (x)
  141. # define le32toh(x) (x)
  142. # define be32toh(x) bswap_32(x)
  143. # define be64toh(x) bswap_64(x)
  144. # define htobe32(x) bswap_32(x)
  145. # define htobe64(x) bswap_64(x)
  146. # elif __BYTE_ORDER == __BIG_ENDIAN
  147. # define htole16(x) bswap_16(x)
  148. # define htole32(x) bswap_32(x)
  149. # define le32toh(x) bswap_32(x)
  150. # define be32toh(x) (x)
  151. # define be64toh(x) (x)
  152. # define htobe32(x) (x)
  153. # define htobe64(x) (x)
  154. #else
  155. #error UNKNOWN BYTE ORDER
  156. #endif
  157. #endif
  158. #undef unlikely
  159. #undef likely
  160. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  161. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  162. #define likely(expr) (__builtin_expect(!!(expr), 1))
  163. #else
  164. #define unlikely(expr) (expr)
  165. #define likely(expr) (expr)
  166. #endif
  167. #define __maybe_unused __attribute__((unused))
  168. #define uninitialised_var(x) x = x
  169. #if defined(__i386__)
  170. #define WANT_CRYPTOPP_ASM32
  171. #endif
  172. #ifndef ARRAY_SIZE
  173. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  174. #endif
  175. #ifdef MIPSEB
  176. #ifndef roundl
  177. #define roundl(x) (long double)((long long)((x==0)?0.0:((x)+((x)>0)?0.5:-0.5)))
  178. #endif
  179. #endif
  180. #define MIN(x, y) ((x) > (y) ? (y) : (x))
  181. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  182. enum drv_driver {
  183. DRIVER_OPENCL = 0,
  184. DRIVER_ICARUS,
  185. DRIVER_BITFORCE,
  186. DRIVER_MODMINER,
  187. DRIVER_ZTEX,
  188. DRIVER_BFLSC,
  189. DRIVER_AVALON,
  190. DRIVER_MAX
  191. };
  192. enum alive {
  193. LIFE_WELL,
  194. LIFE_SICK,
  195. LIFE_DEAD,
  196. LIFE_NOSTART,
  197. LIFE_INIT,
  198. };
  199. enum pool_strategy {
  200. POOL_FAILOVER,
  201. POOL_ROUNDROBIN,
  202. POOL_ROTATE,
  203. POOL_LOADBALANCE,
  204. POOL_BALANCE,
  205. };
  206. #define TOP_STRATEGY (POOL_BALANCE)
  207. struct strategies {
  208. const char *s;
  209. };
  210. struct cgpu_info;
  211. #ifdef HAVE_ADL
  212. struct gpu_adl {
  213. ADLTemperature lpTemperature;
  214. int iAdapterIndex;
  215. int lpAdapterID;
  216. int iBusNumber;
  217. char strAdapterName[256];
  218. ADLPMActivity lpActivity;
  219. ADLODParameters lpOdParameters;
  220. ADLODPerformanceLevels *DefPerfLev;
  221. ADLFanSpeedInfo lpFanSpeedInfo;
  222. ADLFanSpeedValue lpFanSpeedValue;
  223. ADLFanSpeedValue DefFanSpeedValue;
  224. int iEngineClock;
  225. int iMemoryClock;
  226. int iVddc;
  227. int iPercentage;
  228. bool autofan;
  229. bool autoengine;
  230. bool managed; /* Were the values ever changed on this card */
  231. int lastengine;
  232. int lasttemp;
  233. int targetfan;
  234. int targettemp;
  235. int overtemp;
  236. int minspeed;
  237. int maxspeed;
  238. int gpu;
  239. bool has_fanspeed;
  240. struct gpu_adl *twin;
  241. };
  242. #endif
  243. extern void blank_get_statline_before(char *buf, struct cgpu_info __maybe_unused *cgpu);
  244. struct api_data;
  245. struct thr_info;
  246. struct work;
  247. struct device_drv {
  248. enum drv_driver drv_id;
  249. char *dname;
  250. char *name;
  251. // DRV-global functions
  252. void (*drv_detect)();
  253. // Device-specific functions
  254. void (*reinit_device)(struct cgpu_info *);
  255. void (*get_statline_before)(char *, struct cgpu_info *);
  256. void (*get_statline)(char *, struct cgpu_info *);
  257. struct api_data *(*get_api_stats)(struct cgpu_info *);
  258. bool (*get_stats)(struct cgpu_info *);
  259. void (*identify_device)(struct cgpu_info *); // e.g. to flash a led
  260. char *(*set_device)(struct cgpu_info *, char *option, char *setting, char *replybuf);
  261. // Thread-specific functions
  262. bool (*thread_prepare)(struct thr_info *);
  263. uint64_t (*can_limit_work)(struct thr_info *);
  264. bool (*thread_init)(struct thr_info *);
  265. bool (*prepare_work)(struct thr_info *, struct work *);
  266. /* Which hash work loop this driver uses. */
  267. void (*hash_work)(struct thr_info *);
  268. /* Two variants depending on whether the device divides work up into
  269. * small pieces or works with whole work items and may or may not have
  270. * a queue of its own. */
  271. int64_t (*scanhash)(struct thr_info *, struct work *, int64_t);
  272. int64_t (*scanwork)(struct thr_info *);
  273. /* Used to extract work from the hash table of queued work and tell
  274. * the main loop that it should not add any further work to the table.
  275. */
  276. bool (*queue_full)(struct cgpu_info *);
  277. void (*flush_work)(struct cgpu_info *);
  278. void (*hw_error)(struct thr_info *);
  279. void (*thread_shutdown)(struct thr_info *);
  280. void (*thread_enable)(struct thr_info *);
  281. // Does it need to be free()d?
  282. bool copy;
  283. /* Highest target diff the device supports */
  284. double max_diff;
  285. };
  286. extern struct device_drv *copy_drv(struct device_drv*);
  287. enum dev_enable {
  288. DEV_ENABLED,
  289. DEV_DISABLED,
  290. DEV_RECOVER,
  291. };
  292. enum cl_kernels {
  293. KL_NONE,
  294. KL_POCLBM,
  295. KL_PHATK,
  296. KL_DIAKGCN,
  297. KL_DIABLO,
  298. KL_SCRYPT,
  299. };
  300. enum dev_reason {
  301. REASON_THREAD_FAIL_INIT,
  302. REASON_THREAD_ZERO_HASH,
  303. REASON_THREAD_FAIL_QUEUE,
  304. REASON_DEV_SICK_IDLE_60,
  305. REASON_DEV_DEAD_IDLE_600,
  306. REASON_DEV_NOSTART,
  307. REASON_DEV_OVER_HEAT,
  308. REASON_DEV_THERMAL_CUTOFF,
  309. REASON_DEV_COMMS_ERROR,
  310. REASON_DEV_THROTTLE,
  311. };
  312. #define REASON_NONE "None"
  313. #define REASON_THREAD_FAIL_INIT_STR "Thread failed to init"
  314. #define REASON_THREAD_ZERO_HASH_STR "Thread got zero hashes"
  315. #define REASON_THREAD_FAIL_QUEUE_STR "Thread failed to queue work"
  316. #define REASON_DEV_SICK_IDLE_60_STR "Device idle for 60s"
  317. #define REASON_DEV_DEAD_IDLE_600_STR "Device dead - idle for 600s"
  318. #define REASON_DEV_NOSTART_STR "Device failed to start"
  319. #define REASON_DEV_OVER_HEAT_STR "Device over heated"
  320. #define REASON_DEV_THERMAL_CUTOFF_STR "Device reached thermal cutoff"
  321. #define REASON_DEV_COMMS_ERROR_STR "Device comms error"
  322. #define REASON_DEV_THROTTLE_STR "Device throttle"
  323. #define REASON_UNKNOWN_STR "Unknown reason - code bug"
  324. #define MIN_SEC_UNSET 99999999
  325. struct cgminer_stats {
  326. uint32_t getwork_calls;
  327. struct timeval getwork_wait;
  328. struct timeval getwork_wait_max;
  329. struct timeval getwork_wait_min;
  330. };
  331. // Just the actual network getworks to the pool
  332. struct cgminer_pool_stats {
  333. uint32_t getwork_calls;
  334. uint32_t getwork_attempts;
  335. struct timeval getwork_wait;
  336. struct timeval getwork_wait_max;
  337. struct timeval getwork_wait_min;
  338. double getwork_wait_rolling;
  339. bool hadrolltime;
  340. bool canroll;
  341. bool hadexpire;
  342. uint32_t rolltime;
  343. double min_diff;
  344. double max_diff;
  345. double last_diff;
  346. uint32_t min_diff_count;
  347. uint32_t max_diff_count;
  348. uint64_t times_sent;
  349. uint64_t bytes_sent;
  350. uint64_t net_bytes_sent;
  351. uint64_t times_received;
  352. uint64_t bytes_received;
  353. uint64_t net_bytes_received;
  354. };
  355. struct cgpu_info {
  356. int cgminer_id;
  357. struct device_drv *drv;
  358. int device_id;
  359. char *name;
  360. char *device_path;
  361. FILE *device_file;
  362. union {
  363. #ifdef USE_ZTEX
  364. struct libztex_device *device_ztex;
  365. #endif
  366. #ifdef USE_USBUTILS
  367. struct cg_usb_device *usbdev;
  368. #endif
  369. #if defined(USE_ICARUS) || defined(USE_AVALON)
  370. int device_fd;
  371. #endif
  372. };
  373. #ifdef USE_AVALON
  374. struct work **works;
  375. int work_array;
  376. int queued;
  377. int results;
  378. #endif
  379. #ifdef USE_USBUTILS
  380. struct cg_usb_info usbinfo;
  381. #endif
  382. #ifdef USE_MODMINER
  383. char fpgaid;
  384. unsigned char clock;
  385. pthread_mutex_t *modminer_mutex;
  386. #endif
  387. #ifdef USE_BITFORCE
  388. struct timeval work_start_tv;
  389. unsigned int wait_ms;
  390. unsigned int sleep_ms;
  391. double avg_wait_f;
  392. unsigned int avg_wait_d;
  393. uint32_t nonces;
  394. bool nonce_range;
  395. bool polling;
  396. bool flash_led;
  397. pthread_mutex_t device_mutex;
  398. #endif
  399. enum dev_enable deven;
  400. int accepted;
  401. int rejected;
  402. int hw_errors;
  403. double rolling;
  404. double total_mhashes;
  405. double utility;
  406. enum alive status;
  407. char init[40];
  408. struct timeval last_message_tv;
  409. int threads;
  410. struct thr_info **thr;
  411. int64_t max_hashes;
  412. const char *kname;
  413. #ifdef HAVE_OPENCL
  414. bool mapped;
  415. int virtual_gpu;
  416. int virtual_adl;
  417. int intensity;
  418. bool dynamic;
  419. cl_uint vwidth;
  420. size_t work_size;
  421. enum cl_kernels kernel;
  422. cl_ulong max_alloc;
  423. #ifdef USE_SCRYPT
  424. int opt_lg, lookup_gap;
  425. size_t opt_tc, thread_concurrency;
  426. size_t shaders;
  427. #endif
  428. struct timeval tv_gpustart;
  429. int intervals;
  430. #endif
  431. bool new_work;
  432. float temp;
  433. int cutofftemp;
  434. #ifdef HAVE_ADL
  435. bool has_adl;
  436. struct gpu_adl adl;
  437. int gpu_engine;
  438. int min_engine;
  439. int gpu_fan;
  440. int min_fan;
  441. int gpu_memclock;
  442. int gpu_memdiff;
  443. int gpu_powertune;
  444. float gpu_vddc;
  445. #endif
  446. int diff1;
  447. double diff_accepted;
  448. double diff_rejected;
  449. int last_share_pool;
  450. time_t last_share_pool_time;
  451. double last_share_diff;
  452. time_t last_device_valid_work;
  453. time_t device_last_well;
  454. time_t device_last_not_well;
  455. enum dev_reason device_not_well_reason;
  456. int thread_fail_init_count;
  457. int thread_zero_hash_count;
  458. int thread_fail_queue_count;
  459. int dev_sick_idle_60_count;
  460. int dev_dead_idle_600_count;
  461. int dev_nostart_count;
  462. int dev_over_heat_count; // It's a warning but worth knowing
  463. int dev_thermal_cutoff_count;
  464. int dev_comms_error_count;
  465. int dev_throttle_count;
  466. struct cgminer_stats cgminer_stats;
  467. pthread_rwlock_t qlock;
  468. struct work *queued_work;
  469. unsigned int queued_count;
  470. };
  471. extern bool add_cgpu(struct cgpu_info*);
  472. struct thread_q {
  473. struct list_head q;
  474. bool frozen;
  475. pthread_mutex_t mutex;
  476. pthread_cond_t cond;
  477. };
  478. struct thr_info {
  479. int id;
  480. int device_thread;
  481. bool primary_thread;
  482. pthread_t pth;
  483. struct thread_q *q;
  484. struct cgpu_info *cgpu;
  485. void *cgpu_data;
  486. struct timeval last;
  487. struct timeval sick;
  488. bool pause;
  489. bool getwork;
  490. double rolling;
  491. bool work_restart;
  492. };
  493. struct string_elist {
  494. char *string;
  495. bool free_me;
  496. struct list_head list;
  497. };
  498. static inline void string_elist_add(const char *s, struct list_head *head)
  499. {
  500. struct string_elist *n;
  501. n = calloc(1, sizeof(*n));
  502. n->string = strdup(s);
  503. n->free_me = true;
  504. list_add_tail(&n->list, head);
  505. }
  506. static inline void string_elist_del(struct string_elist *item)
  507. {
  508. if (item->free_me)
  509. free(item->string);
  510. list_del(&item->list);
  511. }
  512. static inline uint32_t swab32(uint32_t v)
  513. {
  514. return bswap_32(v);
  515. }
  516. static inline void swap256(void *dest_p, const void *src_p)
  517. {
  518. uint32_t *dest = dest_p;
  519. const uint32_t *src = src_p;
  520. dest[0] = src[7];
  521. dest[1] = src[6];
  522. dest[2] = src[5];
  523. dest[3] = src[4];
  524. dest[4] = src[3];
  525. dest[5] = src[2];
  526. dest[6] = src[1];
  527. dest[7] = src[0];
  528. }
  529. static inline void swab256(void *dest_p, const void *src_p)
  530. {
  531. uint32_t *dest = dest_p;
  532. const uint32_t *src = src_p;
  533. dest[0] = swab32(src[7]);
  534. dest[1] = swab32(src[6]);
  535. dest[2] = swab32(src[5]);
  536. dest[3] = swab32(src[4]);
  537. dest[4] = swab32(src[3]);
  538. dest[5] = swab32(src[2]);
  539. dest[6] = swab32(src[1]);
  540. dest[7] = swab32(src[0]);
  541. }
  542. static inline void flip32(void *dest_p, const void *src_p)
  543. {
  544. uint32_t *dest = dest_p;
  545. const uint32_t *src = src_p;
  546. int i;
  547. for (i = 0; i < 8; i++)
  548. dest[i] = swab32(src[i]);
  549. }
  550. static inline void flip64(void *dest_p, const void *src_p)
  551. {
  552. uint32_t *dest = dest_p;
  553. const uint32_t *src = src_p;
  554. int i;
  555. for (i = 0; i < 16; i++)
  556. dest[i] = swab32(src[i]);
  557. }
  558. static inline void flip80(void *dest_p, const void *src_p)
  559. {
  560. uint32_t *dest = dest_p;
  561. const uint32_t *src = src_p;
  562. int i;
  563. for (i = 0; i < 20; i++)
  564. dest[i] = swab32(src[i]);
  565. }
  566. static inline void flip128(void *dest_p, const void *src_p)
  567. {
  568. uint32_t *dest = dest_p;
  569. const uint32_t *src = src_p;
  570. int i;
  571. for (i = 0; i < 32; i++)
  572. dest[i] = swab32(src[i]);
  573. }
  574. /* For flipping to the correct endianness if necessary */
  575. #if defined(__BIG_ENDIAN__) || defined(MIPSEB)
  576. static inline void endian_flip32(void *dest_p, const void *src_p)
  577. {
  578. flip32(dest_p, src_p);
  579. }
  580. static inline void endian_flip128(void *dest_p, const void *src_p)
  581. {
  582. flip128(dest_p, src_p);
  583. }
  584. #else
  585. static inline void
  586. endian_flip32(void __maybe_unused *dest_p, const void __maybe_unused *src_p)
  587. {
  588. }
  589. static inline void
  590. endian_flip128(void __maybe_unused *dest_p, const void __maybe_unused *src_p)
  591. {
  592. }
  593. #endif
  594. extern void quit(int status, const char *format, ...);
  595. static inline void mutex_lock(pthread_mutex_t *lock)
  596. {
  597. if (unlikely(pthread_mutex_lock(lock)))
  598. quit(1, "WTF MUTEX ERROR ON LOCK!");
  599. }
  600. static inline void mutex_unlock(pthread_mutex_t *lock)
  601. {
  602. if (unlikely(pthread_mutex_unlock(lock)))
  603. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  604. }
  605. static inline int mutex_trylock(pthread_mutex_t *lock)
  606. {
  607. return pthread_mutex_trylock(lock);
  608. }
  609. static inline void wr_lock(pthread_rwlock_t *lock)
  610. {
  611. if (unlikely(pthread_rwlock_wrlock(lock)))
  612. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  613. }
  614. static inline void rd_lock(pthread_rwlock_t *lock)
  615. {
  616. if (unlikely(pthread_rwlock_rdlock(lock)))
  617. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  618. }
  619. static inline void rw_unlock(pthread_rwlock_t *lock)
  620. {
  621. if (unlikely(pthread_rwlock_unlock(lock)))
  622. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  623. }
  624. static inline void rd_unlock(pthread_rwlock_t *lock)
  625. {
  626. rw_unlock(lock);
  627. }
  628. static inline void wr_unlock(pthread_rwlock_t *lock)
  629. {
  630. rw_unlock(lock);
  631. }
  632. static inline void mutex_init(pthread_mutex_t *lock)
  633. {
  634. if (unlikely(pthread_mutex_init(lock, NULL)))
  635. quit(1, "Failed to pthread_mutex_init");
  636. }
  637. static inline void rwlock_init(pthread_rwlock_t *lock)
  638. {
  639. if (unlikely(pthread_rwlock_init(lock, NULL)))
  640. quit(1, "Failed to pthread_rwlock_init");
  641. }
  642. /* cgminer locks, a write biased variant of rwlocks */
  643. struct cglock {
  644. pthread_mutex_t mutex;
  645. pthread_rwlock_t rwlock;
  646. };
  647. typedef struct cglock cglock_t;
  648. static inline void cglock_init(cglock_t *lock)
  649. {
  650. mutex_init(&lock->mutex);
  651. rwlock_init(&lock->rwlock);
  652. }
  653. /* Read lock variant of cglock */
  654. static inline void cg_rlock(cglock_t *lock)
  655. {
  656. mutex_lock(&lock->mutex);
  657. rd_lock(&lock->rwlock);
  658. mutex_unlock(&lock->mutex);
  659. }
  660. /* Intermediate variant of cglock */
  661. static inline void cg_ilock(cglock_t *lock)
  662. {
  663. mutex_lock(&lock->mutex);
  664. }
  665. /* Upgrade intermediate variant to a write lock */
  666. static inline void cg_ulock(cglock_t *lock)
  667. {
  668. wr_lock(&lock->rwlock);
  669. }
  670. /* Write lock variant of cglock */
  671. static inline void cg_wlock(cglock_t *lock)
  672. {
  673. mutex_lock(&lock->mutex);
  674. wr_lock(&lock->rwlock);
  675. }
  676. /* Downgrade intermediate variant to a read lock */
  677. static inline void cg_dlock(cglock_t *lock)
  678. {
  679. rd_lock(&lock->rwlock);
  680. mutex_unlock(&lock->mutex);
  681. }
  682. static inline void cg_runlock(cglock_t *lock)
  683. {
  684. rd_unlock(&lock->rwlock);
  685. }
  686. static inline void cg_wunlock(cglock_t *lock)
  687. {
  688. wr_unlock(&lock->rwlock);
  689. mutex_unlock(&lock->mutex);
  690. }
  691. struct pool;
  692. extern bool opt_protocol;
  693. extern bool have_longpoll;
  694. extern char *opt_kernel_path;
  695. extern char *opt_socks_proxy;
  696. extern char *cgminer_path;
  697. extern bool opt_fail_only;
  698. extern bool opt_autofan;
  699. extern bool opt_autoengine;
  700. extern bool use_curses;
  701. extern char *opt_api_allow;
  702. extern char *opt_api_groups;
  703. extern char *opt_api_description;
  704. extern int opt_api_port;
  705. extern bool opt_api_listen;
  706. extern bool opt_api_network;
  707. extern bool opt_delaynet;
  708. extern bool opt_restart;
  709. extern char *opt_icarus_options;
  710. extern char *opt_icarus_timing;
  711. extern bool opt_worktime;
  712. #ifdef USE_AVALON
  713. extern char *opt_avalon_options;
  714. #endif
  715. #ifdef USE_USBUTILS
  716. extern char *opt_usb_select;
  717. extern int opt_usbdump;
  718. extern bool opt_usb_list_all;
  719. #endif
  720. #ifdef USE_BITFORCE
  721. extern bool opt_bfl_noncerange;
  722. #endif
  723. extern bool ping;
  724. extern int swork_id;
  725. extern pthread_rwlock_t netacc_lock;
  726. extern const uint32_t sha256_init_state[];
  727. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  728. const char *rpc_req, bool, bool, int *,
  729. struct pool *pool, bool);
  730. extern const char *proxytype(curl_proxytype proxytype);
  731. extern char *get_proxy(char *url, struct pool *pool);
  732. extern char *bin2hex(const unsigned char *p, size_t len);
  733. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  734. typedef bool (*sha256_func)(struct thr_info*, const unsigned char *pmidstate,
  735. unsigned char *pdata,
  736. unsigned char *phash1, unsigned char *phash,
  737. const unsigned char *ptarget,
  738. uint32_t max_nonce,
  739. uint32_t *last_nonce,
  740. uint32_t nonce);
  741. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  742. extern int opt_queue;
  743. extern int opt_scantime;
  744. extern int opt_expiry;
  745. #ifdef USE_USBUTILS
  746. extern pthread_mutex_t cgusb_lock;
  747. #endif
  748. extern cglock_t control_lock;
  749. extern pthread_mutex_t hash_lock;
  750. extern pthread_mutex_t console_lock;
  751. extern cglock_t ch_lock;
  752. extern pthread_rwlock_t mining_thr_lock;
  753. extern pthread_rwlock_t devices_lock;
  754. extern pthread_mutex_t restart_lock;
  755. extern pthread_cond_t restart_cond;
  756. extern void thread_reportin(struct thr_info *thr);
  757. extern void clear_stratum_shares(struct pool *pool);
  758. extern int restart_wait(unsigned int mstime);
  759. extern void kill_work(void);
  760. extern void reinit_device(struct cgpu_info *cgpu);
  761. #ifdef HAVE_ADL
  762. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  763. extern int set_fanspeed(int gpu, int iFanSpeed);
  764. extern int set_vddc(int gpu, float fVddc);
  765. extern int set_engineclock(int gpu, int iEngineClock);
  766. extern int set_memoryclock(int gpu, int iMemoryClock);
  767. #endif
  768. extern void api(int thr_id);
  769. extern struct pool *current_pool(void);
  770. extern int enabled_pools;
  771. extern bool detect_stratum(struct pool *pool, char *url);
  772. extern void print_summary(void);
  773. extern struct pool *add_pool(void);
  774. extern bool add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
  775. #define MAX_GPUDEVICES 16
  776. #define MIN_INTENSITY -10
  777. #define _MIN_INTENSITY_STR "-10"
  778. #ifdef USE_SCRYPT
  779. #define MAX_INTENSITY 20
  780. #define _MAX_INTENSITY_STR "20"
  781. #else
  782. #define MAX_INTENSITY 14
  783. #define _MAX_INTENSITY_STR "14"
  784. #endif
  785. extern bool hotplug_mode;
  786. extern int hotplug_time;
  787. extern struct list_head scan_devices;
  788. extern int nDevs;
  789. extern int num_processors;
  790. extern int hw_errors;
  791. extern bool use_syslog;
  792. extern bool opt_quiet;
  793. extern struct thr_info *control_thr;
  794. extern struct thr_info **mining_thr;
  795. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  796. extern int gpu_threads;
  797. #ifdef USE_SCRYPT
  798. extern bool opt_scrypt;
  799. #else
  800. #define opt_scrypt (0)
  801. #endif
  802. extern double total_secs;
  803. extern int mining_threads;
  804. extern int total_devices;
  805. extern struct cgpu_info **devices;
  806. extern int total_pools;
  807. extern struct pool **pools;
  808. extern struct strategies strategies[];
  809. extern enum pool_strategy pool_strategy;
  810. extern int opt_rotate_period;
  811. extern double total_mhashes_done;
  812. extern unsigned int new_blocks;
  813. extern unsigned int found_blocks;
  814. extern int total_accepted, total_rejected, total_diff1;;
  815. extern int total_getworks, total_stale, total_discarded;
  816. extern double total_diff_accepted, total_diff_rejected, total_diff_stale;
  817. extern unsigned int local_work;
  818. extern unsigned int total_go, total_ro;
  819. extern const int opt_cutofftemp;
  820. extern int opt_log_interval;
  821. extern unsigned long long global_hashrate;
  822. extern char *current_fullhash;
  823. extern double current_diff;
  824. extern uint64_t best_diff;
  825. extern struct timeval block_timeval;
  826. #ifdef HAVE_OPENCL
  827. typedef struct {
  828. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  829. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  830. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  831. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  832. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  833. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  834. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  835. cl_uint W16; cl_uint W17; cl_uint W2;
  836. cl_uint PreVal4; cl_uint T1;
  837. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  838. cl_uint PreVal4addT1; cl_uint T1substate0;
  839. cl_uint PreVal4_2;
  840. cl_uint PreVal0;
  841. cl_uint PreW18;
  842. cl_uint PreW19;
  843. cl_uint PreW31;
  844. cl_uint PreW32;
  845. /* For diakgcn */
  846. cl_uint B1addK6, PreVal0addK7, W16addK16, W17addK17;
  847. cl_uint zeroA, zeroB;
  848. cl_uint oneA, twoA, threeA, fourA, fiveA, sixA, sevenA;
  849. #ifdef USE_SCRYPT
  850. struct work *work;
  851. #endif
  852. } dev_blk_ctx;
  853. #else
  854. typedef struct {
  855. uint32_t nonce;
  856. } dev_blk_ctx;
  857. #endif
  858. struct curl_ent {
  859. CURL *curl;
  860. struct list_head node;
  861. struct timeval tv;
  862. };
  863. /* Disabled needs to be the lowest enum as a freshly calloced value will then
  864. * equal disabled */
  865. enum pool_enable {
  866. POOL_DISABLED,
  867. POOL_ENABLED,
  868. POOL_REJECTING,
  869. };
  870. struct stratum_work {
  871. char *job_id;
  872. char *prev_hash;
  873. char *coinbase1;
  874. char *coinbase2;
  875. char **merkle;
  876. char *bbversion;
  877. char *nbit;
  878. char *ntime;
  879. bool clean;
  880. size_t cb1_len;
  881. size_t cb2_len;
  882. size_t cb_len;
  883. size_t header_len;
  884. int merkles;
  885. double diff;
  886. };
  887. #define RBUFSIZE 8192
  888. #define RECVSIZE (RBUFSIZE - 4)
  889. struct pool {
  890. int pool_no;
  891. int prio;
  892. int accepted, rejected;
  893. int seq_rejects;
  894. int seq_getfails;
  895. int solved;
  896. int diff1;
  897. char diff[8];
  898. double diff_accepted;
  899. double diff_rejected;
  900. double diff_stale;
  901. bool submit_fail;
  902. bool idle;
  903. bool lagging;
  904. bool probed;
  905. enum pool_enable enabled;
  906. bool submit_old;
  907. bool removed;
  908. bool lp_started;
  909. char *hdr_path;
  910. char *lp_url;
  911. unsigned int getwork_requested;
  912. unsigned int stale_shares;
  913. unsigned int discarded_work;
  914. unsigned int getfail_occasions;
  915. unsigned int remotefail_occasions;
  916. struct timeval tv_idle;
  917. double utility;
  918. int last_shares, shares;
  919. char *rpc_req;
  920. char *rpc_url;
  921. char *rpc_userpass;
  922. char *rpc_user, *rpc_pass;
  923. curl_proxytype rpc_proxytype;
  924. char *rpc_proxy;
  925. pthread_mutex_t pool_lock;
  926. cglock_t data_lock;
  927. struct thread_q *submit_q;
  928. struct thread_q *getwork_q;
  929. pthread_t longpoll_thread;
  930. pthread_t test_thread;
  931. bool testing;
  932. int curls;
  933. pthread_cond_t cr_cond;
  934. struct list_head curlring;
  935. time_t last_share_time;
  936. double last_share_diff;
  937. uint64_t best_diff;
  938. struct cgminer_stats cgminer_stats;
  939. struct cgminer_pool_stats cgminer_pool_stats;
  940. /* Stratum variables */
  941. char *stratum_url;
  942. char *stratum_port;
  943. CURL *stratum_curl;
  944. SOCKETTYPE sock;
  945. char *sockbuf;
  946. size_t sockbuf_size;
  947. char *sockaddr_url; /* stripped url used for sockaddr */
  948. char *nonce1;
  949. size_t n1_len;
  950. uint32_t nonce2;
  951. int n2size;
  952. char *sessionid;
  953. bool has_stratum;
  954. bool stratum_active;
  955. bool stratum_init;
  956. bool stratum_notify;
  957. struct stratum_work swork;
  958. pthread_t stratum_thread;
  959. pthread_mutex_t stratum_lock;
  960. int sshares; /* stratum shares submitted waiting on response */
  961. /* GBT variables */
  962. bool has_gbt;
  963. cglock_t gbt_lock;
  964. unsigned char previousblockhash[32];
  965. unsigned char gbt_target[32];
  966. char *coinbasetxn;
  967. char *longpollid;
  968. char *gbt_workid;
  969. int gbt_expires;
  970. uint32_t gbt_version;
  971. uint32_t curtime;
  972. uint32_t gbt_bits;
  973. unsigned char *gbt_coinbase;
  974. unsigned char *txn_hashes;
  975. int gbt_txns;
  976. int coinbase_len;
  977. struct timeval tv_lastwork;
  978. };
  979. #define GETWORK_MODE_TESTPOOL 'T'
  980. #define GETWORK_MODE_POOL 'P'
  981. #define GETWORK_MODE_LP 'L'
  982. #define GETWORK_MODE_BENCHMARK 'B'
  983. #define GETWORK_MODE_STRATUM 'S'
  984. #define GETWORK_MODE_GBT 'G'
  985. struct work {
  986. unsigned char data[128];
  987. unsigned char midstate[32];
  988. unsigned char target[32];
  989. unsigned char hash[32];
  990. double device_diff;
  991. uint64_t share_diff;
  992. int rolls;
  993. dev_blk_ctx blk;
  994. struct thr_info *thr;
  995. int thr_id;
  996. struct pool *pool;
  997. struct timeval tv_staged;
  998. bool mined;
  999. bool clone;
  1000. bool cloned;
  1001. int rolltime;
  1002. bool longpoll;
  1003. bool stale;
  1004. bool mandatory;
  1005. bool block;
  1006. bool queued;
  1007. bool stratum;
  1008. char *job_id;
  1009. char *nonce2;
  1010. char *ntime;
  1011. double sdiff;
  1012. char *nonce1;
  1013. bool gbt;
  1014. char *gbt_coinbase;
  1015. int gbt_txns;
  1016. unsigned int work_block;
  1017. int id;
  1018. UT_hash_handle hh;
  1019. double work_difficulty;
  1020. // Allow devices to identify work if multiple sub-devices
  1021. int subid;
  1022. // Allow devices to flag work for their own purposes
  1023. bool devflag;
  1024. struct timeval tv_getwork;
  1025. struct timeval tv_getwork_reply;
  1026. struct timeval tv_cloned;
  1027. struct timeval tv_work_start;
  1028. struct timeval tv_work_found;
  1029. char getwork_mode;
  1030. };
  1031. #ifdef USE_MODMINER
  1032. struct modminer_fpga_state {
  1033. bool work_running;
  1034. struct work running_work;
  1035. struct timeval tv_workstart;
  1036. uint32_t hashes;
  1037. char next_work_cmd[46];
  1038. char fpgaid;
  1039. bool overheated;
  1040. bool new_work;
  1041. uint32_t shares;
  1042. uint32_t shares_last_hw;
  1043. uint32_t hw_errors;
  1044. uint32_t shares_to_good;
  1045. uint32_t timeout_fail;
  1046. uint32_t success_more;
  1047. struct timeval last_changed;
  1048. struct timeval last_nonce;
  1049. struct timeval first_work;
  1050. bool death_stage_one;
  1051. bool tried_two_byte_temp;
  1052. bool one_byte_temp;
  1053. };
  1054. #endif
  1055. extern void get_datestamp(char *, struct timeval *);
  1056. extern void inc_hw_errors(struct thr_info *thr);
  1057. extern void submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  1058. extern struct work *get_queued(struct cgpu_info *cgpu);
  1059. extern struct work *__find_work_bymidstate(struct work *que, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1060. extern struct work *find_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1061. extern void work_completed(struct cgpu_info *cgpu, struct work *work);
  1062. extern void hash_queued_work(struct thr_info *mythr);
  1063. extern void tailsprintf(char *f, const char *fmt, ...);
  1064. extern void wlogprint(const char *f, ...);
  1065. extern int curses_int(const char *query);
  1066. extern char *curses_input(const char *query);
  1067. extern void kill_work(void);
  1068. extern void switch_pools(struct pool *selected);
  1069. extern void discard_work(struct work *work);
  1070. extern void remove_pool(struct pool *pool);
  1071. extern void write_config(FILE *fcfg);
  1072. extern void zero_bestshare(void);
  1073. extern void zero_stats(void);
  1074. extern void default_save_file(char *filename);
  1075. extern bool log_curses_only(int prio, const char *f, va_list ap);
  1076. extern void clear_logwin(void);
  1077. extern bool pool_tclear(struct pool *pool, bool *var);
  1078. extern struct thread_q *tq_new(void);
  1079. extern void tq_free(struct thread_q *tq);
  1080. extern bool tq_push(struct thread_q *tq, void *data);
  1081. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  1082. extern void tq_freeze(struct thread_q *tq);
  1083. extern void tq_thaw(struct thread_q *tq);
  1084. extern bool successful_connect;
  1085. extern void adl(void);
  1086. extern void app_restart(void);
  1087. extern void clean_work(struct work *work);
  1088. extern void free_work(struct work *work);
  1089. extern void __copy_work(struct work *work, struct work *base_work);
  1090. extern struct work *copy_work(struct work *base_work);
  1091. extern struct thr_info *get_thread(int thr_id);
  1092. extern struct cgpu_info *get_devices(int id);
  1093. enum api_data_type {
  1094. API_ESCAPE,
  1095. API_STRING,
  1096. API_CONST,
  1097. API_INT,
  1098. API_UINT,
  1099. API_UINT32,
  1100. API_UINT64,
  1101. API_DOUBLE,
  1102. API_ELAPSED,
  1103. API_BOOL,
  1104. API_TIMEVAL,
  1105. API_TIME,
  1106. API_MHS,
  1107. API_MHTOTAL,
  1108. API_TEMP,
  1109. API_UTILITY,
  1110. API_FREQ,
  1111. API_VOLTS,
  1112. API_HS,
  1113. API_DIFF
  1114. };
  1115. struct api_data {
  1116. enum api_data_type type;
  1117. char *name;
  1118. void *data;
  1119. bool data_was_malloc;
  1120. struct api_data *prev;
  1121. struct api_data *next;
  1122. };
  1123. extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
  1124. extern struct api_data *api_add_string(struct api_data *root, char *name, char *data, bool copy_data);
  1125. extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
  1126. extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
  1127. extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
  1128. extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  1129. extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
  1130. extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
  1131. extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
  1132. extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
  1133. extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
  1134. extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
  1135. extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
  1136. extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
  1137. extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
  1138. extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
  1139. extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
  1140. extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
  1141. extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
  1142. extern struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data);
  1143. #endif /* __MINER_H__ */