driver-avalon2.c 30 KB

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