driver-avalon2.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. /*
  2. * Copyright 2013-2014 Con Kolivas <kernel@kolivas.org>
  3. * Copyright 2012-2014 Xiangfu <xiangfu@openmobilefree.com>
  4. * Copyright 2012 Luke Dashjr
  5. * Copyright 2012 Andrew Smith
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 3 of the License, or (at your option)
  10. * any later version. See COPYING for more details.
  11. */
  12. #include "config.h"
  13. #include <limits.h>
  14. #include <pthread.h>
  15. #include <stdio.h>
  16. #include <sys/time.h>
  17. #include <sys/types.h>
  18. #include <dirent.h>
  19. #include <unistd.h>
  20. #ifndef WIN32
  21. #include <termios.h>
  22. #include <sys/stat.h>
  23. #include <fcntl.h>
  24. #ifndef O_CLOEXEC
  25. #define O_CLOEXEC 0
  26. #endif
  27. #else
  28. #include <windows.h>
  29. #include <io.h>
  30. #endif
  31. #include "elist.h"
  32. #include "miner.h"
  33. #include "fpgautils.h"
  34. #include "driver-avalon2.h"
  35. #include "crc.h"
  36. #include "sha2.h"
  37. #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]
  38. ASSERT1(sizeof(uint32_t) == 4);
  39. #define get_fan_pwm(v) (AVA2_PWM_MAX - (v) * AVA2_PWM_MAX / 100)
  40. int opt_avalon2_freq_min;
  41. int opt_avalon2_freq_max;
  42. int opt_avalon2_fan_min = AVA2_DEFAULT_FAN_MIN;
  43. int opt_avalon2_fan_max = AVA2_DEFAULT_FAN_MAX;
  44. static int avalon2_fan_min = get_fan_pwm(AVA2_DEFAULT_FAN_MIN);
  45. static int avalon2_fan_max = get_fan_pwm(AVA2_DEFAULT_FAN_MAX);
  46. int opt_avalon2_voltage_min;
  47. int opt_avalon2_voltage_max;
  48. int opt_avalon2_overheat = AVALON2_TEMP_OVERHEAT;
  49. int opt_avalon2_polling_delay = AVALON2_DEFAULT_POLLING_DELAY;
  50. enum avalon2_fan_fixed opt_avalon2_fan_fixed = FAN_AUTO;
  51. #define UNPACK32(x, str) \
  52. { \
  53. *((str) + 3) = (uint8_t) ((x) ); \
  54. *((str) + 2) = (uint8_t) ((x) >> 8); \
  55. *((str) + 1) = (uint8_t) ((x) >> 16); \
  56. *((str) + 0) = (uint8_t) ((x) >> 24); \
  57. }
  58. static void sha256_prehash(const unsigned char *message, unsigned int len, unsigned char *digest)
  59. {
  60. sha256_ctx ctx;
  61. int i;
  62. sha256_init(&ctx);
  63. sha256_update(&ctx, message, len);
  64. for (i = 0; i < 8; i++) {
  65. UNPACK32(ctx.h[i], &digest[i << 2]);
  66. }
  67. }
  68. static inline uint8_t rev8(uint8_t d)
  69. {
  70. int i;
  71. uint8_t out = 0;
  72. /* (from left to right) */
  73. for (i = 0; i < 8; i++)
  74. if (d & (1 << i))
  75. out |= (1 << (7 - i));
  76. return out;
  77. }
  78. char *set_avalon2_fan(char *arg)
  79. {
  80. int val1, val2, ret;
  81. ret = sscanf(arg, "%d-%d", &val1, &val2);
  82. if (ret < 1)
  83. return "No values passed to avalon2-fan";
  84. if (ret == 1)
  85. val2 = val1;
  86. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
  87. return "Invalid value passed to avalon2-fan";
  88. opt_avalon2_fan_min = val1;
  89. opt_avalon2_fan_max = val2;
  90. avalon2_fan_min = get_fan_pwm(val1);
  91. avalon2_fan_max = get_fan_pwm(val2);
  92. return NULL;
  93. }
  94. char *set_avalon2_fixed_speed(enum avalon2_fan_fixed *f)
  95. {
  96. *f = FAN_FIXED;
  97. return NULL;
  98. }
  99. char *set_avalon2_freq(char *arg)
  100. {
  101. int val1, val2, ret;
  102. ret = sscanf(arg, "%d-%d", &val1, &val2);
  103. if (ret < 1)
  104. return "No values passed to avalon2-freq";
  105. if (ret == 1)
  106. val2 = val1;
  107. if (val1 < AVA2_DEFAULT_FREQUENCY_MIN || val1 > AVA2_DEFAULT_FREQUENCY_MAX ||
  108. val2 < AVA2_DEFAULT_FREQUENCY_MIN || val2 > AVA2_DEFAULT_FREQUENCY_MAX ||
  109. val2 < val1)
  110. return "Invalid value passed to avalon2-freq";
  111. opt_avalon2_freq_min = val1;
  112. opt_avalon2_freq_max = val2;
  113. return NULL;
  114. }
  115. char *set_avalon2_voltage(char *arg)
  116. {
  117. int val1, val2, ret;
  118. ret = sscanf(arg, "%d-%d", &val1, &val2);
  119. if (ret < 1)
  120. return "No values passed to avalon2-voltage";
  121. if (ret == 1)
  122. val2 = val1;
  123. if (val1 < AVA2_DEFAULT_VOLTAGE_MIN || val1 > AVA2_DEFAULT_VOLTAGE_MAX ||
  124. val2 < AVA2_DEFAULT_VOLTAGE_MIN || val2 > AVA2_DEFAULT_VOLTAGE_MAX ||
  125. val2 < val1)
  126. return "Invalid value passed to avalon2-voltage";
  127. opt_avalon2_voltage_min = val1;
  128. opt_avalon2_voltage_max = val2;
  129. return NULL;
  130. }
  131. static int avalon2_init_pkg(struct avalon2_pkg *pkg, uint8_t type, uint8_t idx, uint8_t cnt)
  132. {
  133. unsigned short crc;
  134. pkg->head[0] = AVA2_H1;
  135. pkg->head[1] = AVA2_H2;
  136. pkg->type = type;
  137. pkg->idx = idx;
  138. pkg->cnt = cnt;
  139. crc = crc16(pkg->data, AVA2_P_DATA_LEN);
  140. pkg->crc[0] = (crc & 0xff00) >> 8;
  141. pkg->crc[1] = crc & 0x00ff;
  142. return 0;
  143. }
  144. static int job_idcmp(uint8_t *job_id, char *pool_job_id)
  145. {
  146. int job_id_len;
  147. unsigned short crc, crc_expect;
  148. if (!pool_job_id)
  149. return 1;
  150. job_id_len = strlen(pool_job_id);
  151. crc_expect = crc16((unsigned char *)pool_job_id, job_id_len);
  152. crc = job_id[0] << 8 | job_id[1];
  153. if (crc_expect == crc)
  154. return 0;
  155. applog(LOG_DEBUG, "Avalon2: job_id not match! [%04x:%04x (%s)]",
  156. crc, crc_expect, pool_job_id);
  157. return 1;
  158. }
  159. static inline int get_temp_max(struct avalon2_info *info)
  160. {
  161. int i;
  162. for (i = 0; i < 2 * AVA2_DEFAULT_MODULARS; i++) {
  163. if (info->temp_max <= info->temp[i])
  164. info->temp_max = info->temp[i];
  165. }
  166. return info->temp_max;
  167. }
  168. static inline int get_current_temp_max(struct avalon2_info *info)
  169. {
  170. int i;
  171. int t = info->temp[0];
  172. for (i = 1; i < 2 * AVA2_DEFAULT_MODULARS; i++) {
  173. if (info->temp[i] > t)
  174. t = info->temp[i];
  175. }
  176. return t;
  177. }
  178. /* http://www.onsemi.com/pub_link/Collateral/ADP3208D.PDF */
  179. static inline uint32_t encode_voltage(uint32_t v)
  180. {
  181. return rev8((0x78 - v / 125) << 1 | 1) << 8;
  182. }
  183. static inline uint32_t decode_voltage(uint32_t v)
  184. {
  185. return (0x78 - (rev8(v >> 8) >> 1)) * 125;
  186. }
  187. static void adjust_fan(struct avalon2_info *info)
  188. {
  189. int t;
  190. if (opt_avalon2_fan_fixed == FAN_FIXED) {
  191. info->fan_pct = opt_avalon2_fan_min;
  192. info->fan_pwm = get_fan_pwm(info->fan_pct);
  193. return;
  194. }
  195. t = get_current_temp_max(info);
  196. /* TODO: Add options for temperature range and fan adjust function */
  197. if (t < 60)
  198. info->fan_pct = opt_avalon2_fan_min;
  199. else if (t > 80)
  200. info->fan_pct = opt_avalon2_fan_max;
  201. else
  202. info->fan_pct = (t - 60) * (opt_avalon2_fan_max - opt_avalon2_fan_min) / 20 + opt_avalon2_fan_min;
  203. info->fan_pwm = get_fan_pwm(info->fan_pct);
  204. }
  205. static inline int mm_cmp_1404(struct avalon2_info *info, int modular)
  206. {
  207. /* <= 1404 return 1 */
  208. char *mm_1404 = "1404";
  209. return strncmp(info->mm_version[modular] + 2, mm_1404, 4) > 0 ? 0 : 1;
  210. }
  211. static inline int mm_cmp_1406(struct avalon2_info *info)
  212. {
  213. /* <= 1406 return 1 */
  214. char *mm_1406 = "1406";
  215. int i;
  216. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  217. if (info->enable[i] &&
  218. strncmp(info->mm_version[i] + 2, mm_1406, 4) <= 0)
  219. return 1;
  220. }
  221. return 0;
  222. }
  223. static int decode_pkg(struct thr_info *thr, struct avalon2_ret *ar, uint8_t *pkg)
  224. {
  225. struct cgpu_info *avalon2 = thr->cgpu;
  226. struct avalon2_info *info = avalon2->device_data;
  227. struct pool *pool, *real_pool, *pool_stratum = &info->pool;
  228. unsigned int expected_crc;
  229. unsigned int actual_crc;
  230. uint32_t nonce, nonce2, miner, modular_id;
  231. int pool_no;
  232. uint8_t job_id[4];
  233. int tmp;
  234. int type = AVA2_GETS_ERROR;
  235. memcpy((uint8_t *)ar, pkg, AVA2_READ_SIZE);
  236. if (ar->head[0] == AVA2_H1 && ar->head[1] == AVA2_H2) {
  237. expected_crc = crc16(ar->data, AVA2_P_DATA_LEN);
  238. actual_crc = (ar->crc[0] & 0xff) |
  239. ((ar->crc[1] & 0xff) << 8);
  240. type = ar->type;
  241. applog(LOG_DEBUG, "Avalon2: %d: expected crc(%04x), actual_crc(%04x)",
  242. type, expected_crc, actual_crc);
  243. if (expected_crc != actual_crc)
  244. goto out;
  245. memcpy(&modular_id, ar->data + 28, 4);
  246. modular_id = be32toh(modular_id);
  247. if (modular_id > 3)
  248. modular_id = 0;
  249. switch(type) {
  250. case AVA2_P_NONCE:
  251. applog(LOG_DEBUG, "Avalon2: AVA2_P_NONCE");
  252. memcpy(&miner, ar->data + 0, 4);
  253. memcpy(&pool_no, ar->data + 4, 4);
  254. memcpy(&nonce2, ar->data + 8, 4);
  255. /* Calc time ar->data + 12 */
  256. memcpy(&nonce, ar->data + 16, 4);
  257. memcpy(job_id, ar->data + 20, 4);
  258. miner = be32toh(miner);
  259. pool_no = be32toh(pool_no);
  260. if (miner >= AVA2_DEFAULT_MINERS ||
  261. modular_id >= AVA2_DEFAULT_MINERS ||
  262. pool_no >= total_pools ||
  263. pool_no < 0) {
  264. applog(LOG_DEBUG, "Avalon2: Wrong miner/pool/id no %d,%d,%d", miner, pool_no, modular_id);
  265. break;
  266. } else
  267. info->matching_work[modular_id * AVA2_DEFAULT_MINERS + miner]++;
  268. nonce2 = be32toh(nonce2);
  269. nonce = be32toh(nonce);
  270. nonce -= 0x180;
  271. applog(LOG_DEBUG, "Avalon2: Found! %d: (%08x) (%08x)",
  272. pool_no, nonce2, nonce);
  273. real_pool = pool = pools[pool_no];
  274. if (job_idcmp(job_id, pool->swork.job_id)) {
  275. if (!job_idcmp(job_id, pool_stratum->swork.job_id)) {
  276. applog(LOG_DEBUG, "Avalon2: Match to previous stratum! (%s)", pool_stratum->swork.job_id);
  277. pool = pool_stratum;
  278. } else {
  279. applog(LOG_ERR, "Avalon2: Cannot match to any stratum! (%s)", pool->swork.job_id);
  280. break;
  281. }
  282. }
  283. submit_nonce2_nonce(thr, pool, real_pool, nonce2, nonce);
  284. break;
  285. case AVA2_P_STATUS:
  286. applog(LOG_DEBUG, "Avalon2: AVA2_P_STATUS");
  287. memcpy(&tmp, ar->data, 4);
  288. tmp = be32toh(tmp);
  289. info->temp[0 + modular_id * 2] = tmp >> 16;
  290. info->temp[1 + modular_id * 2] = tmp & 0xffff;
  291. memcpy(&tmp, ar->data + 4, 4);
  292. tmp = be32toh(tmp);
  293. info->fan[0 + modular_id * 2] = tmp >> 16;
  294. info->fan[1 + modular_id * 2] = tmp & 0xffff;
  295. memcpy(&(info->get_frequency[modular_id]), ar->data + 8, 4);
  296. memcpy(&(info->get_voltage[modular_id]), ar->data + 12, 4);
  297. memcpy(&(info->local_work[modular_id]), ar->data + 16, 4);
  298. memcpy(&(info->hw_work[modular_id]), ar->data + 20, 4);
  299. memcpy(&(info->power_good[modular_id]), ar->data + 24, 4);
  300. info->get_frequency[modular_id] = be32toh(info->get_frequency[modular_id]);
  301. if (info->dev_type[modular_id] == AVA2_ID_AVA3)
  302. info->get_frequency[modular_id] = info->get_frequency[modular_id] * 768 / 65;
  303. info->get_voltage[modular_id] = be32toh(info->get_voltage[modular_id]);
  304. info->local_work[modular_id] = be32toh(info->local_work[modular_id]);
  305. info->hw_work[modular_id] = be32toh(info->hw_work[modular_id]);
  306. info->local_works[modular_id] += info->local_work[modular_id];
  307. info->hw_works[modular_id] += info->hw_work[modular_id];
  308. info->get_voltage[modular_id] = decode_voltage(info->get_voltage[modular_id]);
  309. info->power_good[modular_id] = info->power_good[modular_id] >> 24;
  310. avalon2->temp = get_temp_max(info);
  311. break;
  312. case AVA2_P_ACKDETECT:
  313. applog(LOG_DEBUG, "Avalon2: AVA2_P_ACKDETECT");
  314. break;
  315. case AVA2_P_ACK:
  316. applog(LOG_DEBUG, "Avalon2: AVA2_P_ACK");
  317. break;
  318. case AVA2_P_NAK:
  319. applog(LOG_DEBUG, "Avalon2: AVA2_P_NAK");
  320. break;
  321. default:
  322. applog(LOG_DEBUG, "Avalon2: Unknown response");
  323. type = AVA2_GETS_ERROR;
  324. break;
  325. }
  326. }
  327. out:
  328. return type;
  329. }
  330. static inline int avalon2_gets(struct cgpu_info *avalon2, uint8_t *buf)
  331. {
  332. int read_amount = AVA2_READ_SIZE, ret = 0;
  333. uint8_t *buf_back = buf;
  334. while (true) {
  335. int err;
  336. do {
  337. memset(buf, 0, read_amount);
  338. err = usb_read(avalon2, (char *)buf, read_amount, &ret, C_AVA2_READ);
  339. if (unlikely(err && err != LIBUSB_ERROR_TIMEOUT)) {
  340. applog(LOG_ERR, "Avalon2: Error %d on read in avalon_gets got %d", err, ret);
  341. return AVA2_GETS_ERROR;
  342. }
  343. if (likely(ret >= read_amount)) {
  344. if (unlikely(buf_back[0] != AVA2_H1 || buf_back[1] != AVA2_H2))
  345. return AVA2_GETS_ERROR;
  346. return AVA2_GETS_OK;
  347. }
  348. buf += ret;
  349. read_amount -= ret;
  350. } while (ret > 0);
  351. return AVA2_GETS_TIMEOUT;
  352. }
  353. }
  354. static int avalon2_send_pkg(struct cgpu_info *avalon2, const struct avalon2_pkg *pkg)
  355. {
  356. int err, amount;
  357. uint8_t buf[AVA2_WRITE_SIZE];
  358. int nr_len = AVA2_WRITE_SIZE;
  359. if (unlikely(avalon2->usbinfo.nodev))
  360. return AVA2_SEND_ERROR;
  361. memcpy(buf, pkg, AVA2_WRITE_SIZE);
  362. err = usb_write(avalon2, (char *)buf, nr_len, &amount, C_AVA2_WRITE);
  363. if (err || amount != nr_len) {
  364. applog(LOG_DEBUG, "Avalon2: Send(%d)!", amount);
  365. return AVA2_SEND_ERROR;
  366. }
  367. return AVA2_SEND_OK;
  368. }
  369. static int avalon2_send_pkgs(struct cgpu_info *avalon2, const struct avalon2_pkg *pkg)
  370. {
  371. int ret;
  372. do {
  373. if (unlikely(avalon2->usbinfo.nodev))
  374. return -1;
  375. ret = avalon2_send_pkg(avalon2, pkg);
  376. } while (ret != AVA2_SEND_OK);
  377. return 0;
  378. }
  379. static void avalon2_stratum_pkgs(struct cgpu_info *avalon2, struct pool *pool)
  380. {
  381. const int merkle_offset = 36;
  382. struct avalon2_pkg pkg;
  383. int i, a, b, tmp;
  384. unsigned char target[32];
  385. int job_id_len, n2size;
  386. unsigned short crc;
  387. /* Send out the first stratum message STATIC */
  388. applog(LOG_DEBUG, "Avalon2: Pool stratum message STATIC: %d, %d, %d, %d, %d",
  389. pool->coinbase_len,
  390. pool->nonce2_offset,
  391. pool->n2size,
  392. merkle_offset,
  393. pool->merkles);
  394. memset(pkg.data, 0, AVA2_P_DATA_LEN);
  395. tmp = be32toh(pool->coinbase_len);
  396. memcpy(pkg.data, &tmp, 4);
  397. tmp = be32toh(pool->nonce2_offset);
  398. memcpy(pkg.data + 4, &tmp, 4);
  399. n2size = pool->n2size >= 4 ? 4 : pool->n2size;
  400. tmp = be32toh(n2size);
  401. memcpy(pkg.data + 8, &tmp, 4);
  402. tmp = be32toh(merkle_offset);
  403. memcpy(pkg.data + 12, &tmp, 4);
  404. tmp = be32toh(pool->merkles);
  405. memcpy(pkg.data + 16, &tmp, 4);
  406. tmp = be32toh((int)pool->swork.diff);
  407. memcpy(pkg.data + 20, &tmp, 4);
  408. tmp = be32toh((int)pool->pool_no);
  409. memcpy(pkg.data + 24, &tmp, 4);
  410. avalon2_init_pkg(&pkg, AVA2_P_STATIC, 1, 1);
  411. if (avalon2_send_pkgs(avalon2, &pkg))
  412. return;
  413. set_target(target, pool->sdiff);
  414. memcpy(pkg.data, target, 32);
  415. if (opt_debug) {
  416. char *target_str;
  417. target_str = bin2hex(target, 32);
  418. applog(LOG_DEBUG, "Avalon2: Pool stratum target: %s", target_str);
  419. free(target_str);
  420. }
  421. avalon2_init_pkg(&pkg, AVA2_P_TARGET, 1, 1);
  422. if (avalon2_send_pkgs(avalon2, &pkg))
  423. return;
  424. applog(LOG_DEBUG, "Avalon2: Pool stratum message JOBS_ID: %s",
  425. pool->swork.job_id);
  426. memset(pkg.data, 0, AVA2_P_DATA_LEN);
  427. job_id_len = strlen(pool->swork.job_id);
  428. crc = crc16((unsigned char *)pool->swork.job_id, job_id_len);
  429. pkg.data[0] = (crc & 0xff00) >> 8;
  430. pkg.data[1] = crc & 0x00ff;
  431. avalon2_init_pkg(&pkg, AVA2_P_JOB_ID, 1, 1);
  432. if (avalon2_send_pkgs(avalon2, &pkg))
  433. return;
  434. if (pool->coinbase_len > AVA2_P_COINBASE_SIZE) {
  435. int coinbase_len_posthash, coinbase_len_prehash;
  436. uint8_t coinbase_prehash[32];
  437. coinbase_len_prehash = pool->nonce2_offset - (pool->nonce2_offset % SHA256_BLOCK_SIZE);
  438. coinbase_len_posthash = pool->coinbase_len - coinbase_len_prehash;
  439. sha256_prehash(pool->coinbase, coinbase_len_prehash, coinbase_prehash);
  440. a = (coinbase_len_posthash / AVA2_P_DATA_LEN) + 1;
  441. b = coinbase_len_posthash % AVA2_P_DATA_LEN;
  442. memcpy(pkg.data, coinbase_prehash, 32);
  443. avalon2_init_pkg(&pkg, AVA2_P_COINBASE, 1, a + (b ? 1 : 0));
  444. if (avalon2_send_pkg(avalon2, &pkg))
  445. return;
  446. applog(LOG_DEBUG, "Avalon2: Pool stratum message modified COINBASE: %d %d", a, b);
  447. for (i = 1; i < a; i++) {
  448. memcpy(pkg.data, pool->coinbase + coinbase_len_prehash + i * 32 - 32, 32);
  449. avalon2_init_pkg(&pkg, AVA2_P_COINBASE, i + 1, a + (b ? 1 : 0));
  450. if (avalon2_send_pkg(avalon2, &pkg))
  451. return;
  452. }
  453. if (b) {
  454. memset(pkg.data, 0, AVA2_P_DATA_LEN);
  455. memcpy(pkg.data, pool->coinbase + coinbase_len_prehash + i * 32 - 32, b);
  456. avalon2_init_pkg(&pkg, AVA2_P_COINBASE, i + 1, i + 1);
  457. if (avalon2_send_pkg(avalon2, &pkg))
  458. return;
  459. }
  460. } else {
  461. a = pool->coinbase_len / AVA2_P_DATA_LEN;
  462. b = pool->coinbase_len % AVA2_P_DATA_LEN;
  463. applog(LOG_DEBUG, "Avalon2: Pool stratum message COINBASE: %d %d", a, b);
  464. for (i = 0; i < a; i++) {
  465. memcpy(pkg.data, pool->coinbase + i * 32, 32);
  466. avalon2_init_pkg(&pkg, AVA2_P_COINBASE, i + 1, a + (b ? 1 : 0));
  467. if (avalon2_send_pkg(avalon2, &pkg))
  468. return;
  469. }
  470. if (b) {
  471. memset(pkg.data, 0, AVA2_P_DATA_LEN);
  472. memcpy(pkg.data, pool->coinbase + i * 32, b);
  473. avalon2_init_pkg(&pkg, AVA2_P_COINBASE, i + 1, i + 1);
  474. if (avalon2_send_pkg(avalon2, &pkg))
  475. return;
  476. }
  477. }
  478. b = pool->merkles;
  479. applog(LOG_DEBUG, "Avalon2: Pool stratum message MERKLES: %d", b);
  480. for (i = 0; i < b; i++) {
  481. memset(pkg.data, 0, AVA2_P_DATA_LEN);
  482. memcpy(pkg.data, pool->swork.merkle_bin[i], 32);
  483. avalon2_init_pkg(&pkg, AVA2_P_MERKLES, i + 1, b);
  484. if (avalon2_send_pkgs(avalon2, &pkg))
  485. return;
  486. }
  487. applog(LOG_DEBUG, "Avalon2: Pool stratum message HEADER: 4");
  488. for (i = 0; i < 4; i++) {
  489. memset(pkg.data, 0, AVA2_P_HEADER);
  490. memcpy(pkg.data, pool->header_bin + i * 32, 32);
  491. avalon2_init_pkg(&pkg, AVA2_P_HEADER, i + 1, 4);
  492. if (avalon2_send_pkgs(avalon2, &pkg))
  493. return;
  494. }
  495. }
  496. static void avalon2_initialise(struct cgpu_info *avalon2)
  497. {
  498. uint32_t ava2_data[2] = { PL2303_VALUE_LINE0, PL2303_VALUE_LINE1 };
  499. int interface;
  500. if (avalon2->usbinfo.nodev)
  501. return;
  502. interface = usb_interface(avalon2);
  503. // Set Data Control
  504. usb_transfer(avalon2, PL2303_VENDOR_OUT, PL2303_REQUEST_VENDOR, 8,
  505. interface, C_VENDOR);
  506. if (avalon2->usbinfo.nodev)
  507. return;
  508. usb_transfer(avalon2, PL2303_VENDOR_OUT, PL2303_REQUEST_VENDOR, 9,
  509. interface, C_VENDOR);
  510. if (avalon2->usbinfo.nodev)
  511. return;
  512. // Set Line Control
  513. usb_transfer_data(avalon2, PL2303_CTRL_OUT, PL2303_REQUEST_LINE, PL2303_VALUE_LINE,
  514. interface, ava2_data, PL2303_VALUE_LINE_SIZE, C_SETLINE);
  515. if (avalon2->usbinfo.nodev)
  516. return;
  517. // Vendor
  518. usb_transfer(avalon2, PL2303_VENDOR_OUT, PL2303_REQUEST_VENDOR, PL2303_VALUE_VENDOR,
  519. interface, C_VENDOR);
  520. if (avalon2->usbinfo.nodev)
  521. return;
  522. // Set More Line Control ?
  523. usb_transfer(avalon2, PL2303_CTRL_OUT, PL2303_REQUEST_CTRL, 3, interface, C_SETLINE);
  524. }
  525. static struct cgpu_info *avalon2_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  526. {
  527. struct avalon2_info *info;
  528. int ackdetect;
  529. int err, amount;
  530. int tmp, i, j, modular[AVA2_DEFAULT_MODULARS] = {};
  531. char mm_version[AVA2_DEFAULT_MODULARS][16];
  532. struct cgpu_info *avalon2 = usb_alloc_cgpu(&avalon2_drv, 1);
  533. struct avalon2_pkg detect_pkg;
  534. struct avalon2_ret ret_pkg;
  535. if (!usb_init(avalon2, dev, found)) {
  536. applog(LOG_ERR, "Avalon2 failed usb_init");
  537. avalon2 = usb_free_cgpu(avalon2);
  538. return NULL;
  539. }
  540. avalon2_initialise(avalon2);
  541. for (j = 0; j < 2; j++) {
  542. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  543. strcpy(mm_version[i], AVA2_MM_VERNULL);
  544. /* Send out detect pkg */
  545. memset(detect_pkg.data, 0, AVA2_P_DATA_LEN);
  546. tmp = be32toh(i);
  547. memcpy(detect_pkg.data + 28, &tmp, 4);
  548. avalon2_init_pkg(&detect_pkg, AVA2_P_DETECT, 1, 1);
  549. avalon2_send_pkg(avalon2, &detect_pkg);
  550. err = usb_read(avalon2, (char *)&ret_pkg, AVA2_READ_SIZE, &amount, C_AVA2_READ);
  551. if (err < 0 || amount != AVA2_READ_SIZE) {
  552. applog(LOG_DEBUG, "%s %d: Avalon2 failed usb_read with err %d amount %d",
  553. avalon2->drv->name, avalon2->device_id, err, amount);
  554. continue;
  555. }
  556. ackdetect = ret_pkg.type;
  557. applog(LOG_DEBUG, "Avalon2 Detect ID[%d]: %d", i, ackdetect);
  558. if (ackdetect != AVA2_P_ACKDETECT && modular[i] == 0)
  559. continue;
  560. modular[i] = 1;
  561. memcpy(mm_version[i], ret_pkg.data, 15);
  562. mm_version[i][15] = '\0';
  563. }
  564. }
  565. if (!modular[0] && !modular[1] && !modular[2] && !modular[3]) {
  566. applog(LOG_DEBUG, "Not an Avalon2 device");
  567. usb_uninit(avalon2);
  568. usb_free_cgpu(avalon2);
  569. return NULL;
  570. }
  571. /* We have a real Avalon! */
  572. avalon2->threads = AVA2_MINER_THREADS;
  573. add_cgpu(avalon2);
  574. update_usb_stats(avalon2);
  575. applog(LOG_INFO, "%s%d: Found at %s", avalon2->drv->name, avalon2->device_id,
  576. avalon2->device_path);
  577. avalon2->device_data = calloc(sizeof(struct avalon2_info), 1);
  578. if (unlikely(!(avalon2->device_data)))
  579. quit(1, "Failed to calloc avalon2_info");
  580. info = avalon2->device_data;
  581. info->fan_pwm = get_fan_pwm(AVA2_DEFAULT_FAN_PWM);
  582. info->temp_max = 0;
  583. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  584. strcpy(info->mm_version[i], mm_version[i]);
  585. info->modulars[i] = modular[i]; /* Enable modular */
  586. info->enable[i] = modular[i];
  587. info->dev_type[i] = AVA2_ID_AVAX;
  588. if (!strncmp((char *)&(info->mm_version[i]), AVA2_FW2_PREFIXSTR, 2)) {
  589. info->dev_type[i] = AVA2_ID_AVA2;
  590. info->set_voltage = AVA2_DEFAULT_VOLTAGE_MIN;
  591. info->set_frequency = AVA2_DEFAULT_FREQUENCY;
  592. }
  593. if (!strncmp((char *)&(info->mm_version[i]), AVA2_FW3_PREFIXSTR, 2)) {
  594. info->dev_type[i] = AVA2_ID_AVA3;
  595. info->set_voltage = AVA2_AVA3_VOLTAGE;
  596. info->set_frequency = AVA2_AVA3_FREQUENCY;
  597. }
  598. }
  599. if (!opt_avalon2_voltage_min)
  600. opt_avalon2_voltage_min = opt_avalon2_voltage_max = info->set_voltage;
  601. if (!opt_avalon2_freq_min)
  602. opt_avalon2_freq_min = opt_avalon2_freq_max = info->set_frequency;
  603. return avalon2;
  604. }
  605. static inline void avalon2_detect(bool __maybe_unused hotplug)
  606. {
  607. usb_detect(&avalon2_drv, avalon2_detect_one);
  608. }
  609. static bool avalon2_prepare(struct thr_info *thr)
  610. {
  611. struct cgpu_info *avalon2 = thr->cgpu;
  612. struct avalon2_info *info = avalon2->device_data;
  613. cglock_init(&info->pool.data_lock);
  614. return true;
  615. }
  616. static int polling(struct thr_info *thr, struct cgpu_info *avalon2, struct avalon2_info *info)
  617. {
  618. struct avalon2_pkg send_pkg;
  619. struct avalon2_ret ar;
  620. int i, tmp;
  621. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  622. if (info->modulars[i] && info->enable[i]) {
  623. uint8_t result[AVA2_READ_SIZE];
  624. int ret;
  625. cgsleep_ms(opt_avalon2_polling_delay);
  626. memset(send_pkg.data, 0, AVA2_P_DATA_LEN);
  627. tmp = be32toh(info->led_red[i]); /* RED LED */
  628. memcpy(send_pkg.data + 12, &tmp, 4);
  629. tmp = be32toh(i); /* ID */
  630. memcpy(send_pkg.data + 28, &tmp, 4);
  631. if (info->led_red[i] && mm_cmp_1404(info, i)) {
  632. avalon2_init_pkg(&send_pkg, AVA2_P_TEST, 1, 1);
  633. avalon2_send_pkgs(avalon2, &send_pkg);
  634. info->enable[i] = 0;
  635. continue;
  636. } else
  637. avalon2_init_pkg(&send_pkg, AVA2_P_POLLING, 1, 1);
  638. avalon2_send_pkgs(avalon2, &send_pkg);
  639. ret = avalon2_gets(avalon2, result);
  640. if (ret == AVA2_GETS_OK)
  641. decode_pkg(thr, &ar, result);
  642. }
  643. }
  644. return 0;
  645. }
  646. static void copy_pool_stratum(struct avalon2_info *info, struct pool *pool)
  647. {
  648. int i;
  649. int merkles = pool->merkles;
  650. size_t coinbase_len = pool->coinbase_len;
  651. struct pool *pool_stratum = &info->pool;
  652. if (!job_idcmp((unsigned char *)pool->swork.job_id, pool_stratum->swork.job_id))
  653. return;
  654. cg_wlock(&pool_stratum->data_lock);
  655. free(pool_stratum->swork.job_id);
  656. free(pool_stratum->nonce1);
  657. free(pool_stratum->coinbase);
  658. align_len(&coinbase_len);
  659. pool_stratum->coinbase = calloc(coinbase_len, 1);
  660. if (unlikely(!pool_stratum->coinbase))
  661. quit(1, "Failed to calloc pool_stratum coinbase in avalon2");
  662. memcpy(pool_stratum->coinbase, pool->coinbase, coinbase_len);
  663. for (i = 0; i < pool_stratum->merkles; i++)
  664. free(pool_stratum->swork.merkle_bin[i]);
  665. if (merkles) {
  666. pool_stratum->swork.merkle_bin = realloc(pool_stratum->swork.merkle_bin,
  667. sizeof(char *) * merkles + 1);
  668. for (i = 0; i < merkles; i++) {
  669. pool_stratum->swork.merkle_bin[i] = malloc(32);
  670. if (unlikely(!pool_stratum->swork.merkle_bin[i]))
  671. quit(1, "Failed to malloc pool_stratum swork merkle_bin");
  672. memcpy(pool_stratum->swork.merkle_bin[i], pool->swork.merkle_bin[i], 32);
  673. }
  674. }
  675. pool_stratum->sdiff = pool->sdiff;
  676. pool_stratum->coinbase_len = pool->coinbase_len;
  677. pool_stratum->nonce2_offset = pool->nonce2_offset;
  678. pool_stratum->n2size = pool->n2size;
  679. pool_stratum->merkles = pool->merkles;
  680. pool_stratum->swork.job_id = strdup(pool->swork.job_id);
  681. pool_stratum->nonce1 = strdup(pool->nonce1);
  682. memcpy(pool_stratum->ntime, pool->ntime, sizeof(pool_stratum->ntime));
  683. memcpy(pool_stratum->header_bin, pool->header_bin, sizeof(pool_stratum->header_bin));
  684. cg_wunlock(&pool_stratum->data_lock);
  685. }
  686. static void avalon2_update(struct cgpu_info *avalon2)
  687. {
  688. struct avalon2_info *info = avalon2->device_data;
  689. struct thr_info *thr = avalon2->thr[0];
  690. struct avalon2_pkg send_pkg;
  691. uint32_t tmp, range, start;
  692. struct work *work;
  693. struct pool *pool;
  694. applog(LOG_DEBUG, "Avalon2: New stratum: restart: %d, update: %d",
  695. thr->work_restart, thr->work_update);
  696. thr->work_update = false;
  697. thr->work_restart = false;
  698. work = get_work(thr, thr->id); /* Make sure pool is ready */
  699. discard_work(work); /* Don't leak memory */
  700. pool = current_pool();
  701. if (!pool->has_stratum)
  702. quit(1, "Avalon2: MM have to use stratum pool");
  703. if (pool->coinbase_len > AVA2_P_COINBASE_SIZE) {
  704. applog(LOG_INFO, "Avalon2: MM pool coinbase length(%d) is more than %d",
  705. pool->coinbase_len, AVA2_P_COINBASE_SIZE);
  706. if (mm_cmp_1406(info)) {
  707. applog(LOG_ERR, "Avalon2: MM version less then 1406");
  708. return;
  709. }
  710. if ((pool->coinbase_len - pool->nonce2_offset + 64) > AVA2_P_COINBASE_SIZE) {
  711. applog(LOG_ERR, "Avalon2: MM pool modified coinbase length(%d) is more than %d",
  712. pool->coinbase_len - pool->nonce2_offset + 64, AVA2_P_COINBASE_SIZE);
  713. return;
  714. }
  715. }
  716. if (pool->merkles > AVA2_P_MERKLES_COUNT) {
  717. applog(LOG_ERR, "Avalon2: MM merkles have to less then %d", AVA2_P_MERKLES_COUNT);
  718. return;
  719. }
  720. if (pool->n2size < 3) {
  721. applog(LOG_ERR, "Avalon2: MM nonce2 size have to >= 3 (%d)", pool->n2size);
  722. return;
  723. }
  724. cgtime(&info->last_stratum);
  725. cg_rlock(&pool->data_lock);
  726. info->pool_no = pool->pool_no;
  727. copy_pool_stratum(info, pool);
  728. avalon2_stratum_pkgs(avalon2, pool);
  729. cg_runlock(&pool->data_lock);
  730. /* Configuer the parameter from outside */
  731. adjust_fan(info);
  732. info->set_voltage = opt_avalon2_voltage_min;
  733. info->set_frequency = opt_avalon2_freq_min;
  734. /* Set the Fan, Voltage and Frequency */
  735. memset(send_pkg.data, 0, AVA2_P_DATA_LEN);
  736. tmp = be32toh(info->fan_pwm);
  737. memcpy(send_pkg.data, &tmp, 4);
  738. applog(LOG_INFO, "Avalon2: Temp max: %d, Cut off temp: %d",
  739. get_current_temp_max(info), opt_avalon2_overheat);
  740. if (get_current_temp_max(info) >= opt_avalon2_overheat)
  741. tmp = encode_voltage(0);
  742. else
  743. tmp = encode_voltage(info->set_voltage);
  744. tmp = be32toh(tmp);
  745. memcpy(send_pkg.data + 4, &tmp, 4);
  746. tmp = be32toh(info->set_frequency);
  747. memcpy(send_pkg.data + 8, &tmp, 4);
  748. /* Configure the nonce2 offset and range */
  749. if (pool->n2size == 3)
  750. range = 0xffffff / (total_devices + 1);
  751. else
  752. range = 0xffffffff / (total_devices + 1);
  753. start = range * (avalon2->device_id + 1);
  754. tmp = be32toh(start);
  755. memcpy(send_pkg.data + 12, &tmp, 4);
  756. tmp = be32toh(range);
  757. memcpy(send_pkg.data + 16, &tmp, 4);
  758. /* Package the data */
  759. avalon2_init_pkg(&send_pkg, AVA2_P_SET, 1, 1);
  760. avalon2_send_pkgs(avalon2, &send_pkg);
  761. }
  762. static int64_t avalon2_scanhash(struct thr_info *thr)
  763. {
  764. struct timeval current_stratum;
  765. struct cgpu_info *avalon2 = thr->cgpu;
  766. struct avalon2_info *info = avalon2->device_data;
  767. int64_t h;
  768. int i;
  769. if (unlikely(avalon2->usbinfo.nodev)) {
  770. applog(LOG_ERR, "%s%d: Device disappeared, shutting down thread",
  771. avalon2->drv->name, avalon2->device_id);
  772. return -1;
  773. }
  774. /* Stop polling the device if there is no stratum in 3 minutes, network is down */
  775. cgtime(&current_stratum);
  776. if (tdiff(&current_stratum, &(info->last_stratum)) > (double)(3.0 * 60.0))
  777. return 0;
  778. polling(thr, avalon2, info);
  779. h = 0;
  780. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  781. h += info->enable[i] ? (info->local_work[i] - info->hw_work[i]) : 0;
  782. }
  783. return h * 0xffffffff;
  784. }
  785. static struct api_data *avalon2_api_stats(struct cgpu_info *cgpu)
  786. {
  787. struct api_data *root = NULL;
  788. struct avalon2_info *info = cgpu->device_data;
  789. int i, j, a, b;
  790. char buf[24];
  791. double hwp;
  792. int minerindex, minercount;
  793. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  794. if(info->dev_type[i] == AVA2_ID_AVAX)
  795. continue;
  796. sprintf(buf, "ID%d MM Version", i + 1);
  797. root = api_add_string(root, buf, (char *)&(info->mm_version[i]), false);
  798. }
  799. minerindex = 0;
  800. minercount = 0;
  801. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  802. if (info->dev_type[i] == AVA2_ID_AVAX) {
  803. minerindex += AVA2_DEFAULT_MINERS;
  804. continue;
  805. }
  806. if (info->dev_type[i] == AVA2_ID_AVA2)
  807. minercount = AVA2_DEFAULT_MINERS;
  808. if (info->dev_type[i] == AVA2_ID_AVA3)
  809. minercount = AVA2_AVA3_MINERS;
  810. for (j = minerindex; j < (minerindex + minercount); j++) {
  811. sprintf(buf, "Match work count%02d", j+1);
  812. root = api_add_int(root, buf, &(info->matching_work[j]), false);
  813. }
  814. minerindex += AVA2_DEFAULT_MINERS;
  815. }
  816. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  817. if(info->dev_type[i] == AVA2_ID_AVAX)
  818. continue;
  819. sprintf(buf, "Local works%d", i + 1);
  820. root = api_add_int(root, buf, &(info->local_works[i]), false);
  821. }
  822. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  823. if(info->dev_type[i] == AVA2_ID_AVAX)
  824. continue;
  825. sprintf(buf, "Hardware error works%d", i + 1);
  826. root = api_add_int(root, buf, &(info->hw_works[i]), false);
  827. }
  828. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  829. if(info->dev_type[i] == AVA2_ID_AVAX)
  830. continue;
  831. a = info->hw_works[i];
  832. b = info->local_works[i];
  833. hwp = b ? ((double)a / (double)b) : 0;
  834. sprintf(buf, "Device hardware error%d%%", i + 1);
  835. root = api_add_percent(root, buf, &hwp, true);
  836. }
  837. for (i = 0; i < 2 * AVA2_DEFAULT_MODULARS; i++) {
  838. if(info->dev_type[i/2] == AVA2_ID_AVAX)
  839. continue;
  840. sprintf(buf, "Temperature%d", i + 1);
  841. root = api_add_int(root, buf, &(info->temp[i]), false);
  842. }
  843. for (i = 0; i < 2 * AVA2_DEFAULT_MODULARS; i++) {
  844. if(info->dev_type[i/2] == AVA2_ID_AVAX)
  845. continue;
  846. sprintf(buf, "Fan%d", i + 1);
  847. root = api_add_int(root, buf, &(info->fan[i]), false);
  848. }
  849. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  850. if(info->dev_type[i] == AVA2_ID_AVAX)
  851. continue;
  852. sprintf(buf, "Voltage%d", i + 1);
  853. root = api_add_int(root, buf, &(info->get_voltage[i]), false);
  854. }
  855. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  856. if(info->dev_type[i] == AVA2_ID_AVAX)
  857. continue;
  858. sprintf(buf, "Frequency%d", i + 1);
  859. root = api_add_int(root, buf, &(info->get_frequency[i]), false);
  860. }
  861. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  862. if(info->dev_type[i] == AVA2_ID_AVAX)
  863. continue;
  864. sprintf(buf, "Power good %02x", i + 1);
  865. root = api_add_int(root, buf, &(info->power_good[i]), false);
  866. }
  867. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  868. if(info->dev_type[i] == AVA2_ID_AVAX)
  869. continue;
  870. sprintf(buf, "Led %02x", i + 1);
  871. root = api_add_int(root, buf, &(info->led_red[i]), false);
  872. }
  873. return root;
  874. }
  875. static void avalon2_statline_before(char *buf, size_t bufsiz, struct cgpu_info *avalon2)
  876. {
  877. struct avalon2_info *info = avalon2->device_data;
  878. int temp = get_current_temp_max(info);
  879. float volts = (float)info->set_voltage / 10000;
  880. tailsprintf(buf, bufsiz, "%4dMhz %2dC %3d%% %.3fV", info->set_frequency,
  881. temp, info->fan_pct, volts);
  882. }
  883. static void avalon2_shutdown(struct thr_info *thr)
  884. {
  885. struct cgpu_info *avalon2 = thr->cgpu;
  886. int interface = usb_interface(avalon2);
  887. usb_transfer(avalon2, PL2303_CTRL_OUT, PL2303_REQUEST_CTRL, 0, interface, C_SETLINE);
  888. }
  889. struct device_drv avalon2_drv = {
  890. .drv_id = DRIVER_avalon2,
  891. .dname = "avalon2",
  892. .name = "AV2",
  893. .get_api_stats = avalon2_api_stats,
  894. .get_statline_before = avalon2_statline_before,
  895. .drv_detect = avalon2_detect,
  896. .thread_prepare = avalon2_prepare,
  897. .hash_work = hash_driver_work,
  898. .flush_work = avalon2_update,
  899. .update_work = avalon2_update,
  900. .scanwork = avalon2_scanhash,
  901. .thread_shutdown = avalon2_shutdown,
  902. };