miner.h 31 KB

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