driver-avalon2.c.bak 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. /*
  2. * Copyright 2013-2015 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. if (submit_nonce2_nonce(thr, pool, real_pool, nonce2, nonce, 0))
  284. info->failing = false;
  285. break;
  286. case AVA2_P_STATUS:
  287. applog(LOG_DEBUG, "Avalon2: AVA2_P_STATUS");
  288. memcpy(&tmp, ar->data, 4);
  289. tmp = be32toh(tmp);
  290. info->temp[0 + modular_id * 2] = tmp >> 16;
  291. info->temp[1 + modular_id * 2] = tmp & 0xffff;
  292. memcpy(&tmp, ar->data + 4, 4);
  293. tmp = be32toh(tmp);
  294. info->fan[0 + modular_id * 2] = tmp >> 16;
  295. info->fan[1 + modular_id * 2] = tmp & 0xffff;
  296. memcpy(&(info->get_frequency[modular_id]), ar->data + 8, 4);
  297. memcpy(&(info->get_voltage[modular_id]), ar->data + 12, 4);
  298. memcpy(&(info->local_work[modular_id]), ar->data + 16, 4);
  299. memcpy(&(info->hw_work[modular_id]), ar->data + 20, 4);
  300. memcpy(&(info->power_good[modular_id]), ar->data + 24, 4);
  301. info->get_frequency[modular_id] = be32toh(info->get_frequency[modular_id]);
  302. if (info->dev_type[modular_id] == AVA2_ID_AVA3)
  303. info->get_frequency[modular_id] = info->get_frequency[modular_id] * 768 / 65;
  304. info->get_voltage[modular_id] = be32toh(info->get_voltage[modular_id]);
  305. info->local_work[modular_id] = be32toh(info->local_work[modular_id]);
  306. info->hw_work[modular_id] = be32toh(info->hw_work[modular_id]);
  307. info->local_works[modular_id] += info->local_work[modular_id];
  308. info->hw_works[modular_id] += info->hw_work[modular_id];
  309. info->get_voltage[modular_id] = decode_voltage(info->get_voltage[modular_id]);
  310. info->power_good[modular_id] = info->power_good[modular_id] >> 24;
  311. avalon2->temp = get_temp_max(info);
  312. break;
  313. case AVA2_P_ACKDETECT:
  314. applog(LOG_DEBUG, "Avalon2: AVA2_P_ACKDETECT");
  315. break;
  316. case AVA2_P_ACK:
  317. applog(LOG_DEBUG, "Avalon2: AVA2_P_ACK");
  318. break;
  319. case AVA2_P_NAK:
  320. applog(LOG_DEBUG, "Avalon2: AVA2_P_NAK");
  321. break;
  322. default:
  323. applog(LOG_DEBUG, "Avalon2: Unknown response");
  324. type = AVA2_GETS_ERROR;
  325. break;
  326. }
  327. }
  328. out:
  329. return type;
  330. }
  331. static inline int avalon2_gets(struct cgpu_info *avalon2, uint8_t *buf)
  332. {
  333. int read_amount = AVA2_READ_SIZE, ret = 0;
  334. uint8_t *buf_back = buf;
  335. while (true) {
  336. int err;
  337. do {
  338. memset(buf, 0, read_amount);
  339. err = usb_read(avalon2, (char *)buf, read_amount, &ret, C_AVA2_READ);
  340. if (unlikely(err && err != LIBUSB_ERROR_TIMEOUT)) {
  341. applog(LOG_ERR, "Avalon2: Error %d on read in avalon_gets got %d", err, ret);
  342. return AVA2_GETS_ERROR;
  343. }
  344. if (likely(ret >= read_amount)) {
  345. if (unlikely(buf_back[0] != AVA2_H1 || buf_back[1] != AVA2_H2))
  346. return AVA2_GETS_ERROR;
  347. return AVA2_GETS_OK;
  348. }
  349. buf += ret;
  350. read_amount -= ret;
  351. } while (ret > 0);
  352. return AVA2_GETS_TIMEOUT;
  353. }
  354. }
  355. static int avalon2_send_pkg(struct cgpu_info *avalon2, const struct avalon2_pkg *pkg)
  356. {
  357. int err, amount;
  358. uint8_t buf[AVA2_WRITE_SIZE];
  359. int nr_len = AVA2_WRITE_SIZE;
  360. if (unlikely(avalon2->usbinfo.nodev))
  361. return AVA2_SEND_ERROR;
  362. memcpy(buf, pkg, AVA2_WRITE_SIZE);
  363. err = usb_write(avalon2, (char *)buf, nr_len, &amount, C_AVA2_WRITE);
  364. if (err || amount != nr_len) {
  365. applog(LOG_DEBUG, "Avalon2: Send(%d)!", amount);
  366. usb_nodev(avalon2);
  367. return AVA2_SEND_ERROR;
  368. }
  369. return AVA2_SEND_OK;
  370. }
  371. static void avalon2_stratum_pkgs(struct cgpu_info *avalon2, struct pool *pool)
  372. {
  373. const int merkle_offset = 36;
  374. struct avalon2_pkg pkg;
  375. int i, a, b, tmp;
  376. unsigned char target[32];
  377. int job_id_len, n2size;
  378. unsigned short crc;
  379. int diff;
  380. /* Cap maximum diff in order to still get shares */
  381. diff = pool->swork.diff;
  382. if (diff > 64)
  383. diff = 64;
  384. else if (unlikely(diff < 1))
  385. diff = 1;
  386. /* Send out the first stratum message STATIC */
  387. applog(LOG_DEBUG, "Avalon2: Pool stratum message STATIC: %d, %d, %d, %d, %d",
  388. pool->coinbase_len,
  389. pool->nonce2_offset,
  390. pool->n2size,
  391. merkle_offset,
  392. pool->merkles);
  393. memset(pkg.data, 0, AVA2_P_DATA_LEN);
  394. tmp = be32toh(pool->coinbase_len);
  395. memcpy(pkg.data, &tmp, 4);
  396. tmp = be32toh(pool->nonce2_offset);
  397. memcpy(pkg.data + 4, &tmp, 4);
  398. n2size = pool->n2size >= 4 ? 4 : pool->n2size;
  399. tmp = be32toh(n2size);
  400. memcpy(pkg.data + 8, &tmp, 4);
  401. tmp = be32toh(merkle_offset);
  402. memcpy(pkg.data + 12, &tmp, 4);
  403. tmp = be32toh(pool->merkles);
  404. memcpy(pkg.data + 16, &tmp, 4);
  405. tmp = be32toh(diff);
  406. memcpy(pkg.data + 20, &tmp, 4);
  407. tmp = be32toh((int)pool->pool_no);
  408. memcpy(pkg.data + 24, &tmp, 4);
  409. avalon2_init_pkg(&pkg, AVA2_P_STATIC, 1, 1);
  410. if (avalon2_send_pkg(avalon2, &pkg))
  411. return;
  412. set_target(target, pool->sdiff);
  413. memcpy(pkg.data, target, 32);
  414. if (opt_debug) {
  415. char *target_str;
  416. target_str = bin2hex(target, 32);
  417. applog(LOG_DEBUG, "Avalon2: Pool stratum target: %s", target_str);
  418. free(target_str);
  419. }
  420. avalon2_init_pkg(&pkg, AVA2_P_TARGET, 1, 1);
  421. if (avalon2_send_pkg(avalon2, &pkg))
  422. return;
  423. applog(LOG_DEBUG, "Avalon2: Pool stratum message JOBS_ID: %s",
  424. pool->swork.job_id);
  425. memset(pkg.data, 0, AVA2_P_DATA_LEN);
  426. job_id_len = strlen(pool->swork.job_id);
  427. crc = crc16((unsigned char *)pool->swork.job_id, job_id_len);
  428. pkg.data[0] = (crc & 0xff00) >> 8;
  429. pkg.data[1] = crc & 0x00ff;
  430. avalon2_init_pkg(&pkg, AVA2_P_JOB_ID, 1, 1);
  431. if (avalon2_send_pkg(avalon2, &pkg))
  432. return;
  433. if (pool->coinbase_len > AVA2_P_COINBASE_SIZE) {
  434. int coinbase_len_posthash, coinbase_len_prehash;
  435. uint8_t coinbase_prehash[32];
  436. coinbase_len_prehash = pool->nonce2_offset - (pool->nonce2_offset % SHA256_BLOCK_SIZE);
  437. coinbase_len_posthash = pool->coinbase_len - coinbase_len_prehash;
  438. sha256_prehash(pool->coinbase, coinbase_len_prehash, coinbase_prehash);
  439. a = (coinbase_len_posthash / AVA2_P_DATA_LEN) + 1;
  440. b = coinbase_len_posthash % AVA2_P_DATA_LEN;
  441. memcpy(pkg.data, coinbase_prehash, 32);
  442. avalon2_init_pkg(&pkg, AVA2_P_COINBASE, 1, a + (b ? 1 : 0));
  443. if (avalon2_send_pkg(avalon2, &pkg))
  444. return;
  445. applog(LOG_DEBUG, "Avalon2: Pool stratum message modified COINBASE: %d %d", a, b);
  446. for (i = 1; i < a; i++) {
  447. memcpy(pkg.data, pool->coinbase + coinbase_len_prehash + i * 32 - 32, 32);
  448. avalon2_init_pkg(&pkg, AVA2_P_COINBASE, i + 1, a + (b ? 1 : 0));
  449. if (avalon2_send_pkg(avalon2, &pkg))
  450. return;
  451. }
  452. if (b) {
  453. memset(pkg.data, 0, AVA2_P_DATA_LEN);
  454. memcpy(pkg.data, pool->coinbase + coinbase_len_prehash + i * 32 - 32, b);
  455. avalon2_init_pkg(&pkg, AVA2_P_COINBASE, i + 1, i + 1);
  456. if (avalon2_send_pkg(avalon2, &pkg))
  457. return;
  458. }
  459. } else {
  460. a = pool->coinbase_len / AVA2_P_DATA_LEN;
  461. b = pool->coinbase_len % AVA2_P_DATA_LEN;
  462. applog(LOG_DEBUG, "Avalon2: Pool stratum message COINBASE: %d %d", a, b);
  463. for (i = 0; i < a; i++) {
  464. memcpy(pkg.data, pool->coinbase + i * 32, 32);
  465. avalon2_init_pkg(&pkg, AVA2_P_COINBASE, i + 1, a + (b ? 1 : 0));
  466. if (avalon2_send_pkg(avalon2, &pkg))
  467. return;
  468. }
  469. if (b) {
  470. memset(pkg.data, 0, AVA2_P_DATA_LEN);
  471. memcpy(pkg.data, pool->coinbase + i * 32, b);
  472. avalon2_init_pkg(&pkg, AVA2_P_COINBASE, i + 1, i + 1);
  473. if (avalon2_send_pkg(avalon2, &pkg))
  474. return;
  475. }
  476. }
  477. b = pool->merkles;
  478. applog(LOG_DEBUG, "Avalon2: Pool stratum message MERKLES: %d", b);
  479. for (i = 0; i < b; i++) {
  480. memset(pkg.data, 0, AVA2_P_DATA_LEN);
  481. memcpy(pkg.data, pool->swork.merkle_bin[i], 32);
  482. avalon2_init_pkg(&pkg, AVA2_P_MERKLES, i + 1, b);
  483. if (avalon2_send_pkg(avalon2, &pkg))
  484. return;
  485. }
  486. applog(LOG_DEBUG, "Avalon2: Pool stratum message HEADER: 4");
  487. for (i = 0; i < 4; i++) {
  488. memset(pkg.data, 0, AVA2_P_HEADER);
  489. memcpy(pkg.data, pool->header_bin + i * 32, 32);
  490. avalon2_init_pkg(&pkg, AVA2_P_HEADER, i + 1, 4);
  491. if (avalon2_send_pkg(avalon2, &pkg))
  492. return;
  493. }
  494. }
  495. static void avalon2_initialise(struct cgpu_info *avalon2)
  496. {
  497. uint32_t ava2_data[2] = { PL2303_VALUE_LINE0, PL2303_VALUE_LINE1 };
  498. int interface;
  499. if (avalon2->usbinfo.nodev)
  500. return;
  501. interface = usb_interface(avalon2);
  502. // Set Data Control
  503. usb_transfer(avalon2, PL2303_VENDOR_OUT, PL2303_REQUEST_VENDOR, 8,
  504. interface, C_VENDOR);
  505. if (avalon2->usbinfo.nodev)
  506. return;
  507. usb_transfer(avalon2, PL2303_VENDOR_OUT, PL2303_REQUEST_VENDOR, 9,
  508. interface, C_VENDOR);
  509. if (avalon2->usbinfo.nodev)
  510. return;
  511. // Set Line Control
  512. usb_transfer_data(avalon2, PL2303_CTRL_OUT, PL2303_REQUEST_LINE, PL2303_VALUE_LINE,
  513. interface, ava2_data, PL2303_VALUE_LINE_SIZE, C_SETLINE);
  514. if (avalon2->usbinfo.nodev)
  515. return;
  516. // Vendor
  517. usb_transfer(avalon2, PL2303_VENDOR_OUT, PL2303_REQUEST_VENDOR, PL2303_VALUE_VENDOR,
  518. interface, C_VENDOR);
  519. if (avalon2->usbinfo.nodev)
  520. return;
  521. // Set More Line Control ?
  522. usb_transfer(avalon2, PL2303_CTRL_OUT, PL2303_REQUEST_CTRL, 3, interface, C_SETLINE);
  523. }
  524. static struct cgpu_info *avalon2_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  525. {
  526. struct avalon2_info *info;
  527. int ackdetect;
  528. int err, amount;
  529. int tmp, i, j, modular[AVA2_DEFAULT_MODULARS] = {};
  530. char mm_version[AVA2_DEFAULT_MODULARS][16];
  531. struct cgpu_info *avalon2 = usb_alloc_cgpu(&avalon2_drv, 1);
  532. struct avalon2_pkg detect_pkg;
  533. struct avalon2_ret ret_pkg;
  534. if (!usb_init(avalon2, dev, found)) {
  535. applog(LOG_ERR, "Avalon2 failed usb_init");
  536. avalon2 = usb_free_cgpu(avalon2);
  537. return NULL;
  538. }
  539. avalon2_initialise(avalon2);
  540. for (j = 0; j < 2; j++) {
  541. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  542. strcpy(mm_version[i], AVA2_MM_VERNULL);
  543. /* Send out detect pkg */
  544. memset(detect_pkg.data, 0, AVA2_P_DATA_LEN);
  545. tmp = be32toh(i);
  546. memcpy(detect_pkg.data + 28, &tmp, 4);
  547. avalon2_init_pkg(&detect_pkg, AVA2_P_DETECT, 1, 1);
  548. avalon2_send_pkg(avalon2, &detect_pkg);
  549. err = usb_read(avalon2, (char *)&ret_pkg, AVA2_READ_SIZE, &amount, C_AVA2_READ);
  550. if (err < 0 || amount != AVA2_READ_SIZE) {
  551. applog(LOG_DEBUG, "%s %d: Avalon2 failed usb_read with err %d amount %d",
  552. avalon2->drv->name, avalon2->device_id, err, amount);
  553. continue;
  554. }
  555. ackdetect = ret_pkg.type;
  556. applog(LOG_DEBUG, "Avalon2 Detect ID[%d]: %d", i, ackdetect);
  557. if (ackdetect != AVA2_P_ACKDETECT && modular[i] == 0)
  558. continue;
  559. modular[i] = 1;
  560. memcpy(mm_version[i], ret_pkg.data, 15);
  561. mm_version[i][15] = '\0';
  562. }
  563. }
  564. if (!modular[0] && !modular[1] && !modular[2] && !modular[3]) {
  565. applog(LOG_DEBUG, "Not an Avalon2 device");
  566. usb_uninit(avalon2);
  567. usb_free_cgpu(avalon2);
  568. return NULL;
  569. }
  570. /* We have a real Avalon! */
  571. avalon2->threads = AVA2_MINER_THREADS;
  572. add_cgpu(avalon2);
  573. update_usb_stats(avalon2);
  574. applog(LOG_INFO, "%s %d: Found at %s", avalon2->drv->name, avalon2->device_id,
  575. avalon2->device_path);
  576. avalon2->device_data = cgcalloc(sizeof(struct avalon2_info), 1);
  577. info = avalon2->device_data;
  578. info->fan_pwm = get_fan_pwm(AVA2_DEFAULT_FAN_PWM);
  579. info->temp_max = 0;
  580. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  581. strcpy(info->mm_version[i], mm_version[i]);
  582. info->modulars[i] = modular[i]; /* Enable modular */
  583. info->enable[i] = modular[i];
  584. info->dev_type[i] = AVA2_ID_AVAX;
  585. if (!strncmp((char *)&(info->mm_version[i]), AVA2_FW2_PREFIXSTR, 2)) {
  586. info->dev_type[i] = AVA2_ID_AVA2;
  587. info->set_voltage = AVA2_DEFAULT_VOLTAGE_MIN;
  588. info->set_frequency = AVA2_DEFAULT_FREQUENCY;
  589. }
  590. if (!strncmp((char *)&(info->mm_version[i]), AVA2_FW3_PREFIXSTR, 2)) {
  591. info->dev_type[i] = AVA2_ID_AVA3;
  592. info->set_voltage = AVA2_AVA3_VOLTAGE;
  593. info->set_frequency = AVA2_AVA3_FREQUENCY;
  594. }
  595. }
  596. if (!opt_avalon2_voltage_min)
  597. opt_avalon2_voltage_min = opt_avalon2_voltage_max = info->set_voltage;
  598. if (!opt_avalon2_freq_min)
  599. opt_avalon2_freq_min = opt_avalon2_freq_max = info->set_frequency;
  600. return avalon2;
  601. }
  602. static inline void avalon2_detect(bool __maybe_unused hotplug)
  603. {
  604. usb_detect(&avalon2_drv, avalon2_detect_one);
  605. }
  606. static bool avalon2_prepare(struct thr_info *thr)
  607. {
  608. struct cgpu_info *avalon2 = thr->cgpu;
  609. struct avalon2_info *info = avalon2->device_data;
  610. cglock_init(&info->pool.data_lock);
  611. return true;
  612. }
  613. static int polling(struct thr_info *thr, struct cgpu_info *avalon2, struct avalon2_info *info)
  614. {
  615. struct avalon2_pkg send_pkg;
  616. struct avalon2_ret ar;
  617. int i, tmp;
  618. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  619. if (info->modulars[i] && info->enable[i]) {
  620. uint8_t result[AVA2_READ_SIZE];
  621. int ret;
  622. cgsleep_ms(opt_avalon2_polling_delay);
  623. memset(send_pkg.data, 0, AVA2_P_DATA_LEN);
  624. tmp = be32toh(info->led_red[i]); /* RED LED */
  625. memcpy(send_pkg.data + 12, &tmp, 4);
  626. tmp = be32toh(i); /* ID */
  627. memcpy(send_pkg.data + 28, &tmp, 4);
  628. if (info->led_red[i] && mm_cmp_1404(info, i)) {
  629. avalon2_init_pkg(&send_pkg, AVA2_P_TEST, 1, 1);
  630. avalon2_send_pkg(avalon2, &send_pkg);
  631. info->enable[i] = 0;
  632. continue;
  633. } else
  634. avalon2_init_pkg(&send_pkg, AVA2_P_POLLING, 1, 1);
  635. avalon2_send_pkg(avalon2, &send_pkg);
  636. ret = avalon2_gets(avalon2, result);
  637. if (ret == AVA2_GETS_OK)
  638. decode_pkg(thr, &ar, result);
  639. }
  640. }
  641. return 0;
  642. }
  643. static void copy_pool_stratum(struct avalon2_info *info, struct pool *pool)
  644. {
  645. int i;
  646. int merkles = pool->merkles;
  647. size_t coinbase_len = pool->coinbase_len;
  648. struct pool *pool_stratum = &info->pool;
  649. if (!job_idcmp((unsigned char *)pool->swork.job_id, pool_stratum->swork.job_id))
  650. return;
  651. cg_wlock(&pool_stratum->data_lock);
  652. free(pool_stratum->swork.job_id);
  653. free(pool_stratum->nonce1);
  654. free(pool_stratum->coinbase);
  655. pool_stratum->coinbase = cgcalloc(coinbase_len, 1);
  656. memcpy(pool_stratum->coinbase, pool->coinbase, coinbase_len);
  657. for (i = 0; i < pool_stratum->merkles; i++)
  658. free(pool_stratum->swork.merkle_bin[i]);
  659. if (merkles) {
  660. pool_stratum->swork.merkle_bin = cgrealloc(pool_stratum->swork.merkle_bin,
  661. sizeof(char *) * merkles + 1);
  662. for (i = 0; i < merkles; i++) {
  663. pool_stratum->swork.merkle_bin[i] = cgmalloc(32);
  664. memcpy(pool_stratum->swork.merkle_bin[i], pool->swork.merkle_bin[i], 32);
  665. }
  666. }
  667. pool_stratum->sdiff = pool->sdiff;
  668. pool_stratum->coinbase_len = pool->coinbase_len;
  669. pool_stratum->nonce2_offset = pool->nonce2_offset;
  670. pool_stratum->n2size = pool->n2size;
  671. pool_stratum->merkles = pool->merkles;
  672. pool_stratum->swork.job_id = strdup(pool->swork.job_id);
  673. pool_stratum->nonce1 = strdup(pool->nonce1);
  674. memcpy(pool_stratum->ntime, pool->ntime, sizeof(pool_stratum->ntime));
  675. memcpy(pool_stratum->header_bin, pool->header_bin, sizeof(pool_stratum->header_bin));
  676. cg_wunlock(&pool_stratum->data_lock);
  677. }
  678. static void avalon2_update(struct cgpu_info *avalon2)
  679. {
  680. struct avalon2_info *info = avalon2->device_data;
  681. struct thr_info *thr = avalon2->thr[0];
  682. struct avalon2_pkg send_pkg;
  683. uint32_t tmp, range, start;
  684. struct work *work;
  685. struct pool *pool;
  686. applog(LOG_DEBUG, "Avalon2: New stratum: restart: %d, update: %d",
  687. thr->work_restart, thr->work_update);
  688. thr->work_update = false;
  689. thr->work_restart = false;
  690. work = get_work(thr, thr->id); /* Make sure pool is ready */
  691. discard_work(work); /* Don't leak memory */
  692. pool = current_pool();
  693. if (!pool->has_stratum)
  694. quit(1, "Avalon2: MM have to use stratum pool");
  695. if (pool->coinbase_len > AVA2_P_COINBASE_SIZE) {
  696. applog(LOG_INFO, "Avalon2: MM pool coinbase length(%d) is more than %d",
  697. pool->coinbase_len, AVA2_P_COINBASE_SIZE);
  698. if (mm_cmp_1406(info)) {
  699. applog(LOG_ERR, "Avalon2: MM version less then 1406");
  700. return;
  701. }
  702. if ((pool->coinbase_len - pool->nonce2_offset + 64) > AVA2_P_COINBASE_SIZE) {
  703. applog(LOG_ERR, "Avalon2: MM pool modified coinbase length(%d) is more than %d",
  704. pool->coinbase_len - pool->nonce2_offset + 64, AVA2_P_COINBASE_SIZE);
  705. return;
  706. }
  707. }
  708. if (pool->merkles > AVA2_P_MERKLES_COUNT) {
  709. applog(LOG_ERR, "Avalon2: MM merkles have to less then %d", AVA2_P_MERKLES_COUNT);
  710. return;
  711. }
  712. if (pool->n2size < 3) {
  713. applog(LOG_ERR, "Avalon2: MM nonce2 size have to >= 3 (%d)", pool->n2size);
  714. return;
  715. }
  716. cgtime(&info->last_stratum);
  717. cg_rlock(&pool->data_lock);
  718. info->pool_no = pool->pool_no;
  719. copy_pool_stratum(info, pool);
  720. avalon2_stratum_pkgs(avalon2, pool);
  721. cg_runlock(&pool->data_lock);
  722. /* Configuer the parameter from outside */
  723. adjust_fan(info);
  724. info->set_voltage = opt_avalon2_voltage_min;
  725. info->set_frequency = opt_avalon2_freq_min;
  726. /* Set the Fan, Voltage and Frequency */
  727. memset(send_pkg.data, 0, AVA2_P_DATA_LEN);
  728. tmp = be32toh(info->fan_pwm);
  729. memcpy(send_pkg.data, &tmp, 4);
  730. applog(LOG_INFO, "Avalon2: Temp max: %d, Cut off temp: %d",
  731. get_current_temp_max(info), opt_avalon2_overheat);
  732. if (get_current_temp_max(info) >= opt_avalon2_overheat)
  733. tmp = encode_voltage(0);
  734. else
  735. tmp = encode_voltage(info->set_voltage);
  736. tmp = be32toh(tmp);
  737. memcpy(send_pkg.data + 4, &tmp, 4);
  738. tmp = be32toh(info->set_frequency);
  739. memcpy(send_pkg.data + 8, &tmp, 4);
  740. /* Configure the nonce2 offset and range */
  741. if (pool->n2size == 3)
  742. range = 0xffffff / (total_devices + 1);
  743. else
  744. range = 0xffffffff / (total_devices + 1);
  745. start = range * (avalon2->device_id + 1);
  746. tmp = be32toh(start);
  747. memcpy(send_pkg.data + 12, &tmp, 4);
  748. tmp = be32toh(range);
  749. memcpy(send_pkg.data + 16, &tmp, 4);
  750. /* Package the data */
  751. avalon2_init_pkg(&send_pkg, AVA2_P_SET, 1, 1);
  752. avalon2_send_pkg(avalon2, &send_pkg);
  753. }
  754. static int64_t avalon2_scanhash(struct thr_info *thr)
  755. {
  756. struct timeval current_stratum;
  757. struct cgpu_info *avalon2 = thr->cgpu;
  758. struct avalon2_info *info = avalon2->device_data;
  759. int stdiff;
  760. int64_t h;
  761. int i;
  762. if (unlikely(avalon2->usbinfo.nodev)) {
  763. applog(LOG_ERR, "%s %d: Device disappeared, shutting down thread",
  764. avalon2->drv->name, avalon2->device_id);
  765. return -1;
  766. }
  767. /* Stop polling the device if there is no stratum in 3 minutes, network is down */
  768. cgtime(&current_stratum);
  769. if (tdiff(&current_stratum, &(info->last_stratum)) > (double)(3.0 * 60.0))
  770. return 0;
  771. polling(thr, avalon2, info);
  772. stdiff = share_work_tdiff(avalon2);
  773. if (unlikely(info->failing)) {
  774. if (stdiff > 120) {
  775. applog(LOG_ERR, "%s %d: No valid shares for over 2 minutes, shutting down thread",
  776. avalon2->drv->name, avalon2->device_id);
  777. return -1;
  778. }
  779. } else if (stdiff > 60) {
  780. applog(LOG_ERR, "%s %d: No valid shares for over 1 minute, issuing a USB reset",
  781. avalon2->drv->name, avalon2->device_id);
  782. usb_reset(avalon2);
  783. info->failing = true;
  784. }
  785. h = 0;
  786. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  787. h += info->enable[i] ? (info->local_work[i] - info->hw_work[i]) : 0;
  788. }
  789. return h * 0xffffffff;
  790. }
  791. static struct api_data *avalon2_api_stats(struct cgpu_info *cgpu)
  792. {
  793. struct api_data *root = NULL;
  794. struct avalon2_info *info = cgpu->device_data;
  795. int i, j, a, b;
  796. char buf[24];
  797. double hwp;
  798. int minerindex, minercount;
  799. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  800. if(info->dev_type[i] == AVA2_ID_AVAX)
  801. continue;
  802. sprintf(buf, "ID%d MM Version", i + 1);
  803. root = api_add_string(root, buf, (char *)&(info->mm_version[i]), false);
  804. }
  805. minerindex = 0;
  806. minercount = 0;
  807. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  808. if (info->dev_type[i] == AVA2_ID_AVAX) {
  809. minerindex += AVA2_DEFAULT_MINERS;
  810. continue;
  811. }
  812. if (info->dev_type[i] == AVA2_ID_AVA2)
  813. minercount = AVA2_DEFAULT_MINERS;
  814. if (info->dev_type[i] == AVA2_ID_AVA3)
  815. minercount = AVA2_AVA3_MINERS;
  816. for (j = minerindex; j < (minerindex + minercount); j++) {
  817. sprintf(buf, "Match work count%02d", j+1);
  818. root = api_add_int(root, buf, &(info->matching_work[j]), false);
  819. }
  820. minerindex += AVA2_DEFAULT_MINERS;
  821. }
  822. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  823. if(info->dev_type[i] == AVA2_ID_AVAX)
  824. continue;
  825. sprintf(buf, "Local works%d", i + 1);
  826. root = api_add_int(root, buf, &(info->local_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. sprintf(buf, "Hardware error works%d", i + 1);
  832. root = api_add_int(root, buf, &(info->hw_works[i]), false);
  833. }
  834. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  835. if(info->dev_type[i] == AVA2_ID_AVAX)
  836. continue;
  837. a = info->hw_works[i];
  838. b = info->local_works[i];
  839. hwp = b ? ((double)a / (double)b) : 0;
  840. sprintf(buf, "Device hardware error%d%%", i + 1);
  841. root = api_add_percent(root, buf, &hwp, true);
  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, "Temperature%d", i + 1);
  847. root = api_add_int(root, buf, &(info->temp[i]), false);
  848. }
  849. for (i = 0; i < 2 * AVA2_DEFAULT_MODULARS; i++) {
  850. if(info->dev_type[i/2] == AVA2_ID_AVAX)
  851. continue;
  852. sprintf(buf, "Fan%d", i + 1);
  853. root = api_add_int(root, buf, &(info->fan[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, "Voltage%d", i + 1);
  859. root = api_add_int(root, buf, &(info->get_voltage[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, "Frequency%d", i + 1);
  865. root = api_add_int(root, buf, &(info->get_frequency[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, "Power good %02x", i + 1);
  871. root = api_add_int(root, buf, &(info->power_good[i]), false);
  872. }
  873. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  874. if(info->dev_type[i] == AVA2_ID_AVAX)
  875. continue;
  876. sprintf(buf, "Led %02x", i + 1);
  877. root = api_add_int(root, buf, &(info->led_red[i]), false);
  878. }
  879. return root;
  880. }
  881. static void avalon2_statline_before(char *buf, size_t bufsiz, struct cgpu_info *avalon2)
  882. {
  883. struct avalon2_info *info = avalon2->device_data;
  884. int temp = get_current_temp_max(info);
  885. float volts = (float)info->set_voltage / 10000;
  886. tailsprintf(buf, bufsiz, "%4dMhz %2dC %3d%% %.3fV", info->set_frequency,
  887. temp, info->fan_pct, volts);
  888. }
  889. static void avalon2_shutdown(struct thr_info *thr)
  890. {
  891. struct cgpu_info *avalon2 = thr->cgpu;
  892. int interface = usb_interface(avalon2);
  893. usb_transfer(avalon2, PL2303_CTRL_OUT, PL2303_REQUEST_CTRL, 0, interface, C_SETLINE);
  894. }
  895. struct device_drv avalon2_drv = {
  896. .drv_id = DRIVER_avalon2,
  897. .dname = "avalon2",
  898. .name = "AV2",
  899. .get_api_stats = avalon2_api_stats,
  900. .get_statline_before = avalon2_statline_before,
  901. .drv_detect = avalon2_detect,
  902. .thread_prepare = avalon2_prepare,
  903. .hash_work = hash_driver_work,
  904. .flush_work = avalon2_update,
  905. .update_work = avalon2_update,
  906. .scanwork = avalon2_scanhash,
  907. .thread_shutdown = avalon2_shutdown,
  908. };