driver-avalon2.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /*
  2. * Copyright 2013 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 <sys/select.h>
  19. #include <dirent.h>
  20. #include <unistd.h>
  21. #ifndef WIN32
  22. #include <termios.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #ifndef O_CLOEXEC
  26. #define O_CLOEXEC 0
  27. #endif
  28. #else
  29. #include <windows.h>
  30. #include <io.h>
  31. #endif
  32. #include <utlist.h>
  33. #include "miner.h"
  34. #include "driver-avalon2.h"
  35. #include "lowl-vcom.h"
  36. #include "util.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. BFG_REGISTER_DRIVER(avalon2_drv)
  40. int opt_avalon2_freq_min = AVA2_DEFAULT_FREQUENCY;
  41. int opt_avalon2_freq_max = AVA2_DEFAULT_FREQUENCY_MAX;
  42. int opt_avalon2_fan_min = AVA2_DEFAULT_FAN_PWM;
  43. int opt_avalon2_fan_max = AVA2_DEFAULT_FAN_MAX;
  44. int opt_avalon2_voltage_min = AVA2_DEFAULT_VOLTAGE;
  45. int opt_avalon2_voltage_max = AVA2_DEFAULT_VOLTAGE_MAX;
  46. static inline uint8_t rev8(uint8_t d)
  47. {
  48. int i;
  49. uint8_t out = 0;
  50. /* (from left to right) */
  51. for (i = 0; i < 8; i++)
  52. if (d & (1 << i))
  53. out |= (1 << (7 - i));
  54. return out;
  55. }
  56. char *set_avalon2_fan(char *arg)
  57. {
  58. int val1, val2, ret;
  59. ret = sscanf(arg, "%d-%d", &val1, &val2);
  60. if (ret < 1)
  61. return "No values passed to avalon2-fan";
  62. if (ret == 1)
  63. val2 = val1;
  64. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
  65. return "Invalid value passed to avalon2-fan";
  66. opt_avalon2_fan_min = AVA2_PWM_MAX - val1 * AVA2_PWM_MAX / 100;
  67. opt_avalon2_fan_max = AVA2_PWM_MAX - val2 * AVA2_PWM_MAX / 100;
  68. return NULL;
  69. }
  70. char *set_avalon2_freq(char *arg)
  71. {
  72. int val1, val2, ret;
  73. ret = sscanf(arg, "%d-%d", &val1, &val2);
  74. if (ret < 1)
  75. return "No values passed to avalon2-freq";
  76. if (ret == 1)
  77. val2 = val1;
  78. if (val1 < AVA2_DEFAULT_FREQUENCY_MIN || val1 > AVA2_DEFAULT_FREQUENCY_MAX ||
  79. val2 < AVA2_DEFAULT_FREQUENCY_MIN || val2 > AVA2_DEFAULT_FREQUENCY_MAX ||
  80. val2 < val1)
  81. return "Invalid value passed to avalon2-freq";
  82. opt_avalon2_freq_min = val1;
  83. opt_avalon2_freq_max = val2;
  84. return NULL;
  85. }
  86. char *set_avalon2_voltage(char *arg)
  87. {
  88. int val1, val2, ret;
  89. ret = sscanf(arg, "%d-%d", &val1, &val2);
  90. if (ret < 1)
  91. return "No values passed to avalon2-voltage";
  92. if (ret == 1)
  93. val2 = val1;
  94. if (val1 < AVA2_DEFAULT_VOLTAGE_MIN || val1 > AVA2_DEFAULT_VOLTAGE_MAX ||
  95. val2 < AVA2_DEFAULT_VOLTAGE_MIN || val2 > AVA2_DEFAULT_VOLTAGE_MAX ||
  96. val2 < val1)
  97. return "Invalid value passed to avalon2-voltage";
  98. opt_avalon2_voltage_min = val1;
  99. opt_avalon2_voltage_max = val2;
  100. return NULL;
  101. }
  102. static int avalon2_init_pkg(struct avalon2_pkg *pkg, uint8_t type, uint8_t idx, uint8_t cnt)
  103. {
  104. unsigned short crc;
  105. pkg->head[0] = AVA2_H1;
  106. pkg->head[1] = AVA2_H2;
  107. pkg->type = type;
  108. pkg->idx = idx;
  109. pkg->cnt = cnt;
  110. crc = crc16xmodem(pkg->data, AVA2_P_DATA_LEN);
  111. pkg->crc[0] = (crc & 0xff00) >> 8;
  112. pkg->crc[1] = crc & 0x00ff;
  113. return 0;
  114. }
  115. static int job_idcmp(uint8_t *job_id, char *pool_job_id)
  116. {
  117. int i = 0;
  118. for (i = 0; i < 4; i++) {
  119. if (job_id[i] != *(pool_job_id + strlen(pool_job_id) - 4 + i))
  120. return 1;
  121. }
  122. return 0;
  123. }
  124. extern void submit_nonce2_nonce(struct thr_info *thr, uint32_t pool_no, uint32_t nonce2, uint32_t nonce);
  125. static int decode_pkg(struct thr_info *thr, struct avalon2_ret *ar, uint8_t *pkg)
  126. {
  127. struct cgpu_info *avalon2 = NULL;
  128. struct avalon2_info *info = NULL;
  129. struct pool *pool;
  130. unsigned int expected_crc;
  131. unsigned int actual_crc;
  132. uint32_t nonce, nonce2, miner, modular_id;
  133. int pool_no;
  134. uint8_t job_id[5];
  135. int tmp;
  136. int type = AVA2_GETS_ERROR;
  137. if (thr) {
  138. avalon2 = thr->cgpu;
  139. info = avalon2->device_data;
  140. }
  141. memcpy((uint8_t *)ar, pkg, AVA2_READ_SIZE);
  142. if (ar->head[0] == AVA2_H1 && ar->head[1] == AVA2_H2) {
  143. expected_crc = crc16xmodem(ar->data, AVA2_P_DATA_LEN);
  144. actual_crc = (ar->crc[0] & 0xff) |
  145. ((ar->crc[1] & 0xff) << 8);
  146. type = ar->type;
  147. applog(LOG_DEBUG, "Avalon2: %d: expected crc(%04x), actural_crc(%04x)", type, expected_crc, actual_crc);
  148. if (expected_crc != actual_crc)
  149. goto out;
  150. memcpy(&modular_id, ar->data + 28, 4);
  151. modular_id = be32toh(modular_id);
  152. if (modular_id == 3)
  153. modular_id = 0;
  154. switch(type) {
  155. case AVA2_P_NONCE:
  156. memcpy(&miner, ar->data + 0, 4);
  157. memcpy(&pool_no, ar->data + 4, 4);
  158. memcpy(&nonce2, ar->data + 8, 4);
  159. /* Calc time ar->data + 12 */
  160. memcpy(&nonce, ar->data + 16, 4);
  161. memset(job_id, 0, 5);
  162. memcpy(job_id, ar->data + 20, 4);
  163. miner = be32toh(miner);
  164. pool_no = be32toh(pool_no);
  165. if (miner >= AVA2_DEFAULT_MINERS ||
  166. modular_id >= AVA2_DEFAULT_MINERS ||
  167. pool_no >= total_pools ||
  168. pool_no < 0) {
  169. applog(LOG_DEBUG, "Avalon2: Wrong miner/pool/id no %d,%d,%d", miner, pool_no, modular_id);
  170. break;
  171. } else
  172. if (thr)
  173. info->matching_work[modular_id * AVA2_DEFAULT_MINERS + miner]++;
  174. nonce2 = bswap_32(nonce2);
  175. nonce = be32toh(nonce);
  176. nonce -= 0x180;
  177. applog(LOG_DEBUG, "Avalon2: Found! [%s] %d:(%08x) (%08x)",
  178. job_id, pool_no, nonce2, nonce);
  179. /* FIXME:
  180. * We need remember the pre_pool. then submit the stale work */
  181. pool = pools[pool_no];
  182. if (job_idcmp(job_id, pool->swork.job_id))
  183. break;
  184. if (thr && !info->new_stratum)
  185. submit_nonce2_nonce(thr, pool_no, nonce2, nonce);
  186. break;
  187. case AVA2_P_STATUS:
  188. if (thr)
  189. {
  190. memcpy(&tmp, ar->data, 4);
  191. tmp = be32toh(tmp);
  192. info->temp[0 + modular_id * 2] = tmp >> 16;
  193. info->temp[1 + modular_id * 2] = tmp & 0xffff;
  194. memcpy(&tmp, ar->data + 4, 4);
  195. tmp = be32toh(tmp);
  196. info->fan[0 + modular_id * 2] = tmp >> 16;
  197. info->fan[1 + modular_id * 2] = tmp & 0xffff;
  198. memcpy(&(info->get_frequency[modular_id]), ar->data + 8, 4);
  199. memcpy(&(info->get_voltage[modular_id]), ar->data + 12, 4);
  200. memcpy(&(info->local_work[modular_id]), ar->data + 16, 4);
  201. memcpy(&(info->hw_work[modular_id]), ar->data + 20, 4);
  202. info->get_frequency[modular_id] = be32toh(info->get_frequency[modular_id]);
  203. info->get_voltage[modular_id] = be32toh(info->get_voltage[modular_id]);
  204. info->local_work[modular_id] = be32toh(info->local_work[modular_id]);
  205. info->hw_work[modular_id] = be32toh(info->hw_work[modular_id]);
  206. info->local_works[modular_id] += info->local_work[modular_id];
  207. info->hw_works[modular_id] += info->hw_work[modular_id];
  208. avalon2->temp = info->temp[0]; /* FIXME: */
  209. }
  210. break;
  211. case AVA2_P_ACKDETECT:
  212. break;
  213. case AVA2_P_ACK:
  214. break;
  215. case AVA2_P_NAK:
  216. break;
  217. default:
  218. type = AVA2_GETS_ERROR;
  219. break;
  220. }
  221. }
  222. out:
  223. return type;
  224. }
  225. static inline int avalon2_gets(int fd, uint8_t *buf)
  226. {
  227. int i;
  228. int read_amount = AVA2_READ_SIZE;
  229. uint8_t buf_tmp[AVA2_READ_SIZE];
  230. uint8_t buf_copy[2 * AVA2_READ_SIZE];
  231. uint8_t *buf_back = buf;
  232. ssize_t ret = 0;
  233. while (true) {
  234. struct timeval timeout;
  235. fd_set rd;
  236. timeout.tv_sec = 0;
  237. timeout.tv_usec = 100000;
  238. FD_ZERO(&rd);
  239. FD_SET(fd, &rd);
  240. ret = select(fd + 1, &rd, NULL, NULL, &timeout);
  241. if (unlikely(ret < 0)) {
  242. applog(LOG_ERR, "Avalon2: Error %d on select in avalon_gets", errno);
  243. return AVA2_GETS_ERROR;
  244. }
  245. if (ret) {
  246. memset(buf, 0, read_amount);
  247. ret = read(fd, buf, read_amount);
  248. if (unlikely(ret < 0)) {
  249. applog(LOG_ERR, "Avalon2: Error %d on read in avalon_gets", errno);
  250. return AVA2_GETS_ERROR;
  251. }
  252. if (likely(ret >= read_amount)) {
  253. for (i = 1; i < read_amount; i++) {
  254. if (buf_back[i - 1] == AVA2_H1 && buf_back[i] == AVA2_H2)
  255. break;
  256. }
  257. i -= 1;
  258. if (i) {
  259. ret = read(fd, buf_tmp, i);
  260. if (unlikely(ret != i)) {
  261. applog(LOG_ERR, "Avalon2: Error %d on read in avalon_gets", errno);
  262. return AVA2_GETS_ERROR;
  263. }
  264. memcpy(buf_copy, buf_back + i, AVA2_READ_SIZE - i);
  265. memcpy(buf_copy + AVA2_READ_SIZE - i, buf_tmp, i);
  266. memcpy(buf_back, buf_copy, AVA2_READ_SIZE);
  267. }
  268. return AVA2_GETS_OK;
  269. }
  270. buf += ret;
  271. read_amount -= ret;
  272. continue;
  273. }
  274. return AVA2_GETS_TIMEOUT;
  275. }
  276. }
  277. static int avalon2_send_pkg(int fd, const struct avalon2_pkg *pkg,
  278. struct thr_info __maybe_unused *thr)
  279. {
  280. int ret;
  281. uint8_t buf[AVA2_WRITE_SIZE];
  282. size_t nr_len = AVA2_WRITE_SIZE;
  283. memcpy(buf, pkg, AVA2_WRITE_SIZE);
  284. if (opt_debug) {
  285. applog(LOG_DEBUG, "Avalon2: Sent(%ld):", (long)nr_len);
  286. hexdump((uint8_t *)buf, nr_len);
  287. }
  288. ret = write(fd, buf, nr_len);
  289. if (unlikely(ret != nr_len)) {
  290. applog(LOG_DEBUG, "Avalon2: Send(%d)!", (int)ret);
  291. return AVA2_SEND_ERROR;
  292. }
  293. cgsleep_ms(20);
  294. #if 0
  295. ret = avalon2_gets(fd, result);
  296. if (ret != AVA2_GETS_OK) {
  297. applog(LOG_DEBUG, "Avalon2: Get(%d)!", ret);
  298. return AVA2_SEND_ERROR;
  299. }
  300. ret = decode_pkg(thr, &ar, result);
  301. if (ret != AVA2_P_ACK) {
  302. applog(LOG_DEBUG, "Avalon2: PKG(%d)!", ret);
  303. hexdump((uint8_t *)result, AVA2_READ_SIZE);
  304. return AVA2_SEND_ERROR;
  305. }
  306. #endif
  307. return AVA2_SEND_OK;
  308. }
  309. static int avalon2_stratum_pkgs(int fd, struct pool *pool, struct thr_info *thr)
  310. {
  311. /* FIXME: what if new stratum arrive when writing */
  312. struct avalon2_pkg pkg;
  313. int i, a, b, tmp;
  314. unsigned char target[32];
  315. int job_id_len;
  316. /* Send out the first stratum message STATIC */
  317. applog(LOG_DEBUG, "Avalon2: Pool stratum message STATIC: %ld, %d, %d, %d, %d",
  318. (long)bytes_len(&pool->swork.coinbase),
  319. pool->swork.nonce2_offset,
  320. pool->swork.n2size,
  321. 36,
  322. pool->swork.merkles);
  323. memset(pkg.data, 0, AVA2_P_DATA_LEN);
  324. tmp = be32toh(bytes_len(&pool->swork.coinbase));
  325. memcpy(pkg.data, &tmp, 4);
  326. tmp = be32toh(pool->swork.nonce2_offset);
  327. memcpy(pkg.data + 4, &tmp, 4);
  328. tmp = be32toh(pool->swork.n2size);
  329. memcpy(pkg.data + 8, &tmp, 4);
  330. tmp = be32toh(36);
  331. memcpy(pkg.data + 12, &tmp, 4);
  332. tmp = be32toh(pool->swork.merkles);
  333. memcpy(pkg.data + 16, &tmp, 4);
  334. tmp = be32toh((int)pdiff_to_bdiff(target_diff(pool->swork.target)));
  335. memcpy(pkg.data + 20, &tmp, 4);
  336. tmp = be32toh((int)pool->pool_no);
  337. memcpy(pkg.data + 24, &tmp, 4);
  338. avalon2_init_pkg(&pkg, AVA2_P_STATIC, 1, 1);
  339. while (avalon2_send_pkg(fd, &pkg, thr) != AVA2_SEND_OK)
  340. ;
  341. memcpy(target, pool->swork.target, sizeof(target));
  342. memcpy(pkg.data, target, 32);
  343. if (opt_debug) {
  344. char target_str[(32 * 2) + 1];
  345. bin2hex(target_str, target, 32);
  346. applog(LOG_DEBUG, "Avalon2: Pool stratum target: %s", target_str);
  347. }
  348. avalon2_init_pkg(&pkg, AVA2_P_TARGET, 1, 1);
  349. while (avalon2_send_pkg(fd, &pkg, thr) != AVA2_SEND_OK)
  350. ;
  351. applog(LOG_DEBUG, "Avalon2: Pool stratum message JOBS_ID: %s",
  352. pool->swork.job_id);
  353. memset(pkg.data, 0, AVA2_P_DATA_LEN);
  354. job_id_len = strlen(pool->swork.job_id);
  355. job_id_len = job_id_len >= 4 ? 4 : job_id_len;
  356. for (i = 0; i < job_id_len; i++) {
  357. pkg.data[i] = *(pool->swork.job_id + strlen(pool->swork.job_id) - 4 + i);
  358. }
  359. avalon2_init_pkg(&pkg, AVA2_P_JOB_ID, 1, 1);
  360. while (avalon2_send_pkg(fd, &pkg, thr) != AVA2_SEND_OK)
  361. ;
  362. a = bytes_len(&pool->swork.coinbase) / AVA2_P_DATA_LEN;
  363. b = bytes_len(&pool->swork.coinbase) % AVA2_P_DATA_LEN;
  364. applog(LOG_DEBUG, "Avalon2: Pool stratum message COINBASE: %d %d", a, b);
  365. for (i = 0; i < a; i++) {
  366. memcpy(pkg.data, bytes_buf(&pool->swork.coinbase) + i * 32, 32);
  367. avalon2_init_pkg(&pkg, AVA2_P_COINBASE, i + 1, a + (b ? 1 : 0));
  368. while (avalon2_send_pkg(fd, &pkg, thr) != AVA2_SEND_OK)
  369. ;
  370. }
  371. if (b) {
  372. memset(pkg.data, 0, AVA2_P_DATA_LEN);
  373. memcpy(pkg.data, bytes_buf(&pool->swork.coinbase) + i * 32, b);
  374. avalon2_init_pkg(&pkg, AVA2_P_COINBASE, i + 1, i + 1);
  375. while (avalon2_send_pkg(fd, &pkg, thr) != AVA2_SEND_OK)
  376. ;
  377. }
  378. b = pool->swork.merkles;
  379. applog(LOG_DEBUG, "Avalon2: Pool stratum message MERKLES: %d", b);
  380. for (i = 0; i < b; i++) {
  381. memset(pkg.data, 0, AVA2_P_DATA_LEN);
  382. memcpy(pkg.data, &bytes_buf(&pool->swork.merkle_bin)[0x20 * i], 32);
  383. avalon2_init_pkg(&pkg, AVA2_P_MERKLES, i + 1, b);
  384. while (avalon2_send_pkg(fd, &pkg, thr) != AVA2_SEND_OK)
  385. ;
  386. }
  387. applog(LOG_DEBUG, "Avalon2: Pool stratum message HEADER: 4");
  388. uint8_t header_bin[0x80];
  389. memcpy(&header_bin[0], pool->swork.header1, 36);
  390. *((uint32_t*)&header_bin[68]) = htobe32(pool->swork.ntime);
  391. memcpy(&header_bin[72], pool->swork.diffbits, 4);
  392. memset(&header_bin[76], 0, 4); // nonce
  393. memcpy(&header_bin[80], bfg_workpadding_bin, 48);
  394. for (i = 0; i < 4; i++) {
  395. memset(pkg.data, 0, AVA2_P_HEADER);
  396. memcpy(pkg.data, header_bin + i * 32, 32);
  397. avalon2_init_pkg(&pkg, AVA2_P_HEADER, i + 1, 4);
  398. while (avalon2_send_pkg(fd, &pkg, thr) != AVA2_SEND_OK)
  399. ;
  400. }
  401. return 0;
  402. }
  403. static int avalon2_get_result(struct thr_info *thr, int fd_detect, struct avalon2_ret *ar)
  404. {
  405. struct cgpu_info *avalon2;
  406. struct avalon2_info *info;
  407. int fd;
  408. fd = fd_detect;
  409. if (thr) {
  410. avalon2 = thr->cgpu;
  411. info = avalon2->device_data;
  412. fd = info->fd;
  413. }
  414. uint8_t result[AVA2_READ_SIZE];
  415. int ret;
  416. memset(result, 0, AVA2_READ_SIZE);
  417. ret = avalon2_gets(fd, result);
  418. if (ret != AVA2_GETS_OK)
  419. return ret;
  420. if (opt_debug) {
  421. applog(LOG_DEBUG, "Avalon2: Get(ret = %d):", ret);
  422. hexdump((uint8_t *)result, AVA2_READ_SIZE);
  423. }
  424. return decode_pkg(thr, ar, result);
  425. }
  426. static bool avalon2_detect_one(const char *devpath)
  427. {
  428. struct avalon2_info *info;
  429. int ackdetect;
  430. int fd;
  431. int tmp, i, modular[3];
  432. char mm_version[AVA2_DEFAULT_MODULARS][16];
  433. struct cgpu_info *avalon2;
  434. struct avalon2_pkg detect_pkg;
  435. struct avalon2_ret ret_pkg;
  436. applog(LOG_DEBUG, "Avalon2 Detect: Attempting to open %s", devpath);
  437. fd = avalon2_open(devpath, AVA2_IO_SPEED, true);
  438. if (unlikely(fd == -1)) {
  439. applog(LOG_ERR, "Avalon2 Detect: Failed to open %s", devpath);
  440. return false;
  441. }
  442. tcflush(fd, TCIOFLUSH);
  443. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  444. modular[i] = 0;
  445. strcpy(mm_version[i], "NONE");
  446. /* Send out detect pkg */
  447. memset(detect_pkg.data, 0, AVA2_P_DATA_LEN);
  448. tmp = be32toh(i);
  449. memcpy(detect_pkg.data + 28, &tmp, 4);
  450. avalon2_init_pkg(&detect_pkg, AVA2_P_DETECT, 1, 1);
  451. avalon2_send_pkg(fd, &detect_pkg, NULL);
  452. ackdetect = avalon2_get_result(NULL, fd, &ret_pkg);
  453. applog(LOG_DEBUG, "Avalon2 Detect ID[%d]: %d", i, ackdetect);
  454. if (ackdetect != AVA2_P_ACKDETECT)
  455. continue;
  456. modular[i] = 1;
  457. memcpy(mm_version[i], ret_pkg.data, 15);
  458. mm_version[i][15] = '\0';
  459. }
  460. if (!modular[0] && !modular[1] && !modular[2])
  461. return false;
  462. /* We have a real Avalon! */
  463. avalon2 = calloc(1, sizeof(struct cgpu_info));
  464. avalon2->drv = &avalon2_drv;
  465. avalon2->device_path = strdup(devpath);
  466. avalon2->threads = AVA2_MINER_THREADS;
  467. add_cgpu(avalon2);
  468. applog(LOG_INFO, "Avalon2 Detect: Found at %s, mark as %d",
  469. devpath, avalon2->device_id);
  470. avalon2->device_data = calloc(sizeof(struct avalon2_info), 1);
  471. if (unlikely(!(avalon2->device_data)))
  472. quit(1, "Failed to malloc avalon2_info");
  473. info = avalon2->device_data;
  474. strcpy(info->mm_version[0], mm_version[0]);
  475. strcpy(info->mm_version[1], mm_version[1]);
  476. strcpy(info->mm_version[2], mm_version[2]);
  477. info->baud = AVA2_IO_SPEED;
  478. info->fan_pwm = AVA2_DEFAULT_FAN_PWM;
  479. info->set_voltage = AVA2_DEFAULT_VOLTAGE_MIN;
  480. info->set_frequency = AVA2_DEFAULT_FREQUENCY;
  481. info->temp_max = 0;
  482. info->temp_history_index = 0;
  483. info->temp_sum = 0;
  484. info->temp_old = 0;
  485. info->modulars[0] = modular[0];
  486. info->modulars[1] = modular[1];
  487. info->modulars[2] = modular[2]; /* Enable modular */
  488. info->fd = -1;
  489. /* Set asic to idle mode after detect */
  490. avalon2_close(fd);
  491. return true;
  492. }
  493. static inline void avalon2_detect()
  494. {
  495. generic_detect(&avalon2_drv, avalon2_detect_one, NULL, 0);
  496. }
  497. static void avalon2_init(struct cgpu_info *avalon2)
  498. {
  499. int fd;
  500. struct avalon2_info *info = avalon2->device_data;
  501. fd = avalon2_open(avalon2->device_path, info->baud, true);
  502. if (unlikely(fd == -1)) {
  503. applog(LOG_ERR, "Avalon2: Failed to open on %s", avalon2->device_path);
  504. return;
  505. }
  506. applog(LOG_DEBUG, "Avalon2: Opened on %s", avalon2->device_path);
  507. info->fd = fd;
  508. }
  509. static bool avalon2_prepare(struct thr_info *thr)
  510. {
  511. struct cgpu_info *avalon2 = thr->cgpu;
  512. struct avalon2_info *info = avalon2->device_data;
  513. free(avalon2->works);
  514. avalon2->works = calloc(sizeof(struct work *), 2);
  515. if (!avalon2->works)
  516. quit(1, "Failed to calloc avalon2 works in avalon2_prepare");
  517. if (info->fd == -1)
  518. avalon2_init(avalon2);
  519. info->first = true;
  520. return true;
  521. }
  522. static int polling(struct thr_info *thr)
  523. {
  524. int i, tmp;
  525. struct avalon2_pkg send_pkg;
  526. struct avalon2_ret ar;
  527. struct cgpu_info *avalon2 = thr->cgpu;
  528. struct avalon2_info *info = avalon2->device_data;
  529. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  530. if (info->modulars[i]) {
  531. memset(send_pkg.data, 0, AVA2_P_DATA_LEN);
  532. tmp = be32toh(i);
  533. memcpy(send_pkg.data + 28, &tmp, 4);
  534. avalon2_init_pkg(&send_pkg, AVA2_P_POLLING, 1, 1);
  535. while (avalon2_send_pkg(info->fd, &send_pkg, thr) != AVA2_SEND_OK)
  536. ;
  537. avalon2_get_result(thr, info->fd, &ar);
  538. }
  539. }
  540. return 0;
  541. }
  542. static int64_t avalon2_scanhash(struct thr_info *thr)
  543. {
  544. struct avalon2_pkg send_pkg;
  545. struct pool *pool;
  546. struct cgpu_info *avalon2 = thr->cgpu;
  547. struct avalon2_info *info = avalon2->device_data;
  548. int64_t h;
  549. uint32_t tmp, range, start;
  550. int i;
  551. if (thr->work_restart || thr->work_restart ||
  552. info->first) {
  553. info->new_stratum = true;
  554. applog(LOG_DEBUG, "Avalon2: New stratum: restart: %d, update: %d, first: %d",
  555. thr->work_restart, thr->work_restart, info->first);
  556. thr->work_restart = false;
  557. thr->work_restart = false;
  558. if (unlikely(info->first))
  559. info->first = false;
  560. get_work(thr); /* Make sure pool is ready */
  561. pool = current_pool();
  562. if (!pool->has_stratum)
  563. quit(1, "Avalon2: Miner Manager have to use stratum pool");
  564. if (bytes_len(&pool->swork.coinbase) > AVA2_P_COINBASE_SIZE)
  565. quit(1, "Avalon2: Miner Manager pool coinbase length have to less then %d", AVA2_P_COINBASE_SIZE);
  566. if (pool->swork.merkles > AVA2_P_MERKLES_COUNT)
  567. quit(1, "Avalon2: Miner Manager merkles have to less then %d", AVA2_P_MERKLES_COUNT);
  568. info->diff = (int)pdiff_to_bdiff(target_diff(pool->swork.target)) - 1;
  569. info->pool_no = pool->pool_no;
  570. cg_wlock(&pool->data_lock);
  571. avalon2_stratum_pkgs(info->fd, pool, thr);
  572. cg_wunlock(&pool->data_lock);
  573. /* Configuer the parameter from outside */
  574. info->fan_pwm = opt_avalon2_fan_min;
  575. info->set_voltage = opt_avalon2_voltage_min;
  576. info->set_frequency = opt_avalon2_freq_min;
  577. /* Set the Fan, Voltage and Frequency */
  578. memset(send_pkg.data, 0, AVA2_P_DATA_LEN);
  579. tmp = be32toh(info->fan_pwm);
  580. memcpy(send_pkg.data, &tmp, 4);
  581. /* http://www.onsemi.com/pub_link/Collateral/ADP3208D.PDF */
  582. tmp = rev8((0x78 - info->set_voltage / 125) << 1 | 1) << 8;
  583. tmp = be32toh(tmp);
  584. memcpy(send_pkg.data + 4, &tmp, 4);
  585. tmp = be32toh(info->set_frequency);
  586. memcpy(send_pkg.data + 8, &tmp, 4);
  587. /* Configure the nonce2 offset and range */
  588. range = 0xffffffff / total_devices;
  589. start = range * avalon2->device_id;
  590. tmp = be32toh(start);
  591. memcpy(send_pkg.data + 12, &tmp, 4);
  592. tmp = be32toh(range);
  593. memcpy(send_pkg.data + 16, &tmp, 4);
  594. /* Package the data */
  595. avalon2_init_pkg(&send_pkg, AVA2_P_SET, 1, 1);
  596. while (avalon2_send_pkg(info->fd, &send_pkg, thr) != AVA2_SEND_OK)
  597. ;
  598. info->new_stratum = false;
  599. }
  600. polling(thr);
  601. h = 0;
  602. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  603. h += info->local_work[i];
  604. }
  605. return h * 0xffffffff;
  606. }
  607. static struct api_data *avalon2_api_stats(struct cgpu_info *cgpu)
  608. {
  609. struct api_data *root = NULL;
  610. struct avalon2_info *info = cgpu->device_data;
  611. int i, a, b;
  612. char buf[24];
  613. double hwp;
  614. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  615. sprintf(buf, "ID%d MM Version", i + 1);
  616. const char * const mmv = info->mm_version[i];
  617. root = api_add_string(root, buf, mmv, false);
  618. }
  619. for (i = 0; i < AVA2_DEFAULT_MINERS * AVA2_DEFAULT_MODULARS; i++) {
  620. sprintf(buf, "Match work count%02d", i + 1);
  621. root = api_add_int(root, buf, &(info->matching_work[i]), false);
  622. }
  623. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  624. sprintf(buf, "Local works%d", i + 1);
  625. root = api_add_int(root, buf, &(info->local_works[i]), false);
  626. }
  627. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  628. sprintf(buf, "Hardware error works%d", i + 1);
  629. root = api_add_int(root, buf, &(info->hw_works[i]), false);
  630. }
  631. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  632. a = info->hw_works[i];
  633. b = info->local_works[i];
  634. hwp = b ? ((double)a / (double)b) : 0;
  635. sprintf(buf, "Device hardware error%d%%", i + 1);
  636. root = api_add_percent(root, buf, &hwp, true);
  637. }
  638. for (i = 0; i < 2 * AVA2_DEFAULT_MODULARS; i++) {
  639. sprintf(buf, "Temperature%d", i + 1);
  640. root = api_add_int(root, buf, &(info->temp[i]), false);
  641. }
  642. for (i = 0; i < 2 * AVA2_DEFAULT_MODULARS; i++) {
  643. sprintf(buf, "Fan%d", i + 1);
  644. root = api_add_int(root, buf, &(info->fan[i]), false);
  645. }
  646. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  647. sprintf(buf, "Voltage%d", i + 1);
  648. root = api_add_int(root, buf, &(info->get_voltage[i]), false);
  649. }
  650. for (i = 0; i < AVA2_DEFAULT_MODULARS; i++) {
  651. sprintf(buf, "Frequency%d", i + 1);
  652. root = api_add_int(root, buf, &(info->get_frequency[i]), false);
  653. }
  654. return root;
  655. }
  656. static void avalon2_shutdown(struct thr_info *thr)
  657. {
  658. struct cgpu_info *avalon = thr->cgpu;
  659. free(avalon->works);
  660. avalon->works = NULL;
  661. }
  662. struct device_drv avalon2_drv = {
  663. .dname = "avalon2",
  664. .name = "AV2",
  665. .get_api_stats = avalon2_api_stats,
  666. .drv_detect = avalon2_detect,
  667. .reinit_device = avalon2_init,
  668. .thread_prepare = avalon2_prepare,
  669. .minerloop = hash_driver_work,
  670. .scanwork = avalon2_scanhash,
  671. .thread_shutdown = avalon2_shutdown,
  672. };