driver-avalon.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. /*
  2. * Copyright 2012-2013 Xiangfu
  3. * Copyright 2012 Luke Dashjr
  4. * Copyright 2012 Andrew Smith
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include "config.h"
  12. #include <limits.h>
  13. #include <pthread.h>
  14. #include <stdbool.h>
  15. #include <stdint.h>
  16. #include <stdio.h>
  17. #include <sys/time.h>
  18. #include <sys/types.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 "deviceapi.h"
  33. #include "elist.h"
  34. #include "miner.h"
  35. #include "fpgautils.h"
  36. #include "driver-avalon.h"
  37. #include "logging.h"
  38. static int option_offset = -1;
  39. struct avalon_info **avalon_info;
  40. struct device_api avalon_api;
  41. static inline uint8_t rev8(uint8_t d)
  42. {
  43. int i;
  44. uint8_t out = 0;
  45. /* from left to right */
  46. for (i = 0; i < 8; i++)
  47. if (d & (1 << i))
  48. out |= (1 << (7 - i));
  49. return out;
  50. }
  51. static int avalon_init_task(struct avalon_task *at,
  52. uint8_t reset, uint8_t ff, uint8_t fan,
  53. uint8_t timeout, uint8_t asic_num,
  54. uint8_t miner_num, uint8_t nonce_elf,
  55. uint8_t gate_miner, int frequency)
  56. {
  57. uint8_t *buf;
  58. static bool first = true;
  59. if (unlikely(!at))
  60. return -1;
  61. if (unlikely(timeout <= 0 || asic_num <= 0 || miner_num <= 0))
  62. return -1;
  63. memset(at, 0, sizeof(struct avalon_task));
  64. if (unlikely(reset)) {
  65. at->reset = 1;
  66. at->fan_eft = 1;
  67. at->timer_eft = 1;
  68. first = true;
  69. }
  70. at->flush_fifo = (ff ? 1 : 0);
  71. at->fan_eft = (fan ? 1 : 0);
  72. if (unlikely(first && !at->reset)) {
  73. at->fan_eft = 1;
  74. at->timer_eft = 1;
  75. first = false;
  76. }
  77. at->fan_pwm_data = (fan ? fan : AVALON_DEFAULT_FAN_MAX_PWM);
  78. at->timeout_data = timeout;
  79. at->asic_num = asic_num;
  80. at->miner_num = miner_num;
  81. at->nonce_elf = nonce_elf;
  82. at->gate_miner_elf = 1;
  83. at->asic_pll = 1;
  84. if (unlikely(gate_miner)) {
  85. at-> gate_miner = 1;
  86. at->asic_pll = 0;
  87. }
  88. buf = (uint8_t *)at;
  89. buf[5] = 0x00;
  90. buf[8] = 0x74;
  91. buf[9] = 0x01;
  92. buf[10] = 0x00;
  93. buf[11] = 0x00;
  94. if (frequency == 256) {
  95. buf[6] = 0x03;
  96. buf[7] = 0x08;
  97. } else if (frequency == 270) {
  98. buf[6] = 0x73;
  99. buf[7] = 0x08;
  100. } else if (frequency == 282) {
  101. buf[6] = 0xd3;
  102. buf[7] = 0x08;
  103. } else if (frequency == 300) {
  104. buf[6] = 0x63;
  105. buf[7] = 0x09;
  106. }
  107. return 0;
  108. }
  109. static inline void avalon_create_task(struct avalon_task *at,
  110. struct work *work)
  111. {
  112. memcpy(at->midstate, work->midstate, 32);
  113. memcpy(at->data, work->data + 64, 12);
  114. }
  115. static int avalon_send_task(int fd, const struct avalon_task *at,
  116. struct cgpu_info *avalon)
  117. {
  118. size_t ret;
  119. int full;
  120. struct timespec p;
  121. uint8_t buf[AVALON_WRITE_SIZE + 4 * AVALON_DEFAULT_ASIC_NUM];
  122. size_t nr_len;
  123. struct avalon_info *info;
  124. uint64_t delay = 32000000; /* Default 32ms for B19200 */
  125. uint32_t nonce_range;
  126. int i;
  127. if (at->nonce_elf)
  128. nr_len = AVALON_WRITE_SIZE + 4 * at->asic_num;
  129. else
  130. nr_len = AVALON_WRITE_SIZE;
  131. memcpy(buf, at, AVALON_WRITE_SIZE);
  132. if (at->nonce_elf) {
  133. nonce_range = (uint32_t)0xffffffff / at->asic_num;
  134. for (i = 0; i < at->asic_num; i++) {
  135. buf[AVALON_WRITE_SIZE + (i * 4) + 3] =
  136. (i * nonce_range & 0xff000000) >> 24;
  137. buf[AVALON_WRITE_SIZE + (i * 4) + 2] =
  138. (i * nonce_range & 0x00ff0000) >> 16;
  139. buf[AVALON_WRITE_SIZE + (i * 4) + 1] =
  140. (i * nonce_range & 0x0000ff00) >> 8;
  141. buf[AVALON_WRITE_SIZE + (i * 4) + 0] =
  142. (i * nonce_range & 0x000000ff) >> 0;
  143. }
  144. }
  145. #if defined(__BIG_ENDIAN__) || defined(MIPSEB)
  146. uint8_t tt = 0;
  147. tt = (buf[0] & 0x0f) << 4;
  148. tt |= ((buf[0] & 0x10) ? (1 << 3) : 0);
  149. tt |= ((buf[0] & 0x20) ? (1 << 2) : 0);
  150. tt |= ((buf[0] & 0x40) ? (1 << 1) : 0);
  151. tt |= ((buf[0] & 0x80) ? (1 << 0) : 0);
  152. buf[0] = tt;
  153. tt = (buf[4] & 0x0f) << 4;
  154. tt |= ((buf[4] & 0x10) ? (1 << 3) : 0);
  155. tt |= ((buf[4] & 0x20) ? (1 << 2) : 0);
  156. tt |= ((buf[4] & 0x40) ? (1 << 1) : 0);
  157. tt |= ((buf[4] & 0x80) ? (1 << 0) : 0);
  158. buf[4] = tt;
  159. #endif
  160. if (likely(avalon)) {
  161. info = avalon_info[avalon->device_id];
  162. delay = nr_len * 10 * 1000000000ULL;
  163. delay = delay / info->baud;
  164. }
  165. if (at->reset)
  166. nr_len = 1;
  167. if (opt_debug) {
  168. applog(LOG_DEBUG, "Avalon: Sent(%lu):", (unsigned long)nr_len);
  169. hexdump((uint8_t *)buf, nr_len);
  170. }
  171. ret = write(fd, buf, nr_len);
  172. if (unlikely(ret != nr_len))
  173. return AVA_SEND_ERROR;
  174. p.tv_sec = 0;
  175. p.tv_nsec = (long)delay + 4000000;
  176. nanosleep(&p, NULL);
  177. applog(LOG_DEBUG, "Avalon: Sent: Buffer delay: %ld", p.tv_nsec);
  178. full = avalon_buffer_full(fd);
  179. applog(LOG_DEBUG, "Avalon: Sent: Buffer full: %s",
  180. ((full == AVA_BUFFER_FULL) ? "Yes" : "No"));
  181. if (unlikely(full == AVA_BUFFER_FULL))
  182. return AVA_SEND_BUFFER_FULL;
  183. return AVA_SEND_BUFFER_EMPTY;
  184. }
  185. static int avalon_gets(int fd, uint8_t *buf, int read_count,
  186. struct thr_info *thr, struct timeval *tv_finish)
  187. {
  188. ssize_t ret = 0;
  189. int rc = 0;
  190. int read_amount = AVALON_READ_SIZE;
  191. bool first = true;
  192. /* Read reply 1 byte at a time to get earliest tv_finish */
  193. while (true) {
  194. ret = read(fd, buf, 1);
  195. if (ret < 0)
  196. return AVA_GETS_ERROR;
  197. if (first && tv_finish != NULL)
  198. gettimeofday(tv_finish, NULL);
  199. if (ret >= read_amount)
  200. return AVA_GETS_OK;
  201. if (ret > 0) {
  202. buf += ret;
  203. read_amount -= ret;
  204. first = false;
  205. continue;
  206. }
  207. rc++;
  208. if (rc >= read_count) {
  209. if (opt_debug) {
  210. applog(LOG_WARNING,
  211. "Avalon: No data in %.2f seconds",
  212. (float)rc/(float)AVALON_TIME_FACTOR);
  213. }
  214. return AVA_GETS_TIMEOUT;
  215. }
  216. if (thr && thr->work_restart) {
  217. if (opt_debug) {
  218. applog(LOG_WARNING,
  219. "Avalon: Work restart at %.2f seconds",
  220. (float)(rc)/(float)AVALON_TIME_FACTOR);
  221. }
  222. return AVA_GETS_RESTART;
  223. }
  224. }
  225. }
  226. static int avalon_get_result(int fd, struct avalon_result *ar,
  227. struct thr_info *thr, struct timeval *tv_finish)
  228. {
  229. struct cgpu_info *avalon;
  230. struct avalon_info *info;
  231. uint8_t result[AVALON_READ_SIZE];
  232. int ret, read_count = AVALON_RESET_FAULT_DECISECONDS * AVALON_TIME_FACTOR;
  233. if (likely(thr)) {
  234. avalon = thr->cgpu;
  235. info = avalon_info[avalon->device_id];
  236. read_count = info->read_count;
  237. }
  238. memset(result, 0, AVALON_READ_SIZE);
  239. ret = avalon_gets(fd, result, read_count, thr, tv_finish);
  240. if (ret == AVA_GETS_OK) {
  241. if (opt_debug) {
  242. applog(LOG_DEBUG, "Avalon: get:");
  243. hexdump((uint8_t *)result, AVALON_READ_SIZE);
  244. }
  245. memcpy((uint8_t *)ar, result, AVALON_READ_SIZE);
  246. }
  247. return ret;
  248. }
  249. static int avalon_decode_nonce(struct thr_info *thr, struct work **work,
  250. struct avalon_result *ar, uint32_t *nonce)
  251. {
  252. struct cgpu_info *avalon;
  253. struct avalon_info *info;
  254. int avalon_get_work_count, i;
  255. if (unlikely(!work))
  256. return -1;
  257. avalon = thr->cgpu;
  258. info = avalon_info[avalon->device_id];
  259. avalon_get_work_count = info->miner_count;
  260. for (i = 0; i < avalon_get_work_count; i++) {
  261. if (work[i] &&
  262. !memcmp(ar->data, work[i]->data + 64, 12) &&
  263. !memcmp(ar->midstate, work[i]->midstate, 32))
  264. break;
  265. }
  266. if (i == avalon_get_work_count)
  267. return -1;
  268. info->matching_work[i]++;
  269. *nonce = ar->nonce;
  270. #if defined (__BIG_ENDIAN__) || defined(MIPSEB)
  271. *nonce = swab32(*nonce);
  272. #endif
  273. applog(LOG_DEBUG, "Avalon: match to work[%d](%p): %d",i, work[i],
  274. info->matching_work[i]);
  275. return i;
  276. }
  277. static int avalon_reset(int fd, struct avalon_result *ar)
  278. {
  279. struct avalon_task at;
  280. uint8_t *buf;
  281. int ret, i = 0;
  282. struct timespec p;
  283. avalon_init_task(&at, 1, 0,
  284. AVALON_DEFAULT_FAN_MAX_PWM,
  285. AVALON_DEFAULT_TIMEOUT,
  286. AVALON_DEFAULT_ASIC_NUM,
  287. AVALON_DEFAULT_MINER_NUM,
  288. 0, 0,
  289. AVALON_DEFAULT_FREQUENCY);
  290. ret = avalon_send_task(fd, &at, NULL);
  291. if (ret == AVA_SEND_ERROR)
  292. return 1;
  293. avalon_get_result(fd, ar, NULL, NULL);
  294. buf = (uint8_t *)ar;
  295. if (buf[0] == 0xAA && buf[1] == 0x55 &&
  296. buf[2] == 0xAA && buf[3] == 0x55) {
  297. for (i = 4; i < 11; i++)
  298. if (buf[i] != 0)
  299. break;
  300. }
  301. if (i != 11) {
  302. applog(LOG_ERR, "Avalon: Reset failed! not an Avalon?"
  303. " (%d: %02x %02x %02x %02x)",
  304. i, buf[0], buf[1], buf[2], buf[3]);
  305. /* FIXME: return 1; */
  306. }
  307. p.tv_sec = 0;
  308. p.tv_nsec = AVALON_RESET_PITCH;
  309. nanosleep(&p, NULL);
  310. applog(LOG_WARNING, "Avalon: Reset succeeded");
  311. return 0;
  312. }
  313. static void avalon_idle(struct cgpu_info *avalon)
  314. {
  315. int i, ret;
  316. struct avalon_task at;
  317. int fd = avalon->device_fd;
  318. struct avalon_info *info = avalon_info[avalon->device_id];
  319. int avalon_get_work_count = info->miner_count;
  320. i = 0;
  321. while (true) {
  322. avalon_init_task(&at, 0, 0, info->fan_pwm,
  323. info->timeout, info->asic_count,
  324. info->miner_count, 1, 1, info->frequency);
  325. ret = avalon_send_task(fd, &at, avalon);
  326. if (unlikely(ret == AVA_SEND_ERROR ||
  327. (ret == AVA_SEND_BUFFER_EMPTY &&
  328. (i + 1 == avalon_get_work_count * 2)))) {
  329. applog(LOG_ERR, "AVA%i: Comms error", avalon->device_id);
  330. return;
  331. }
  332. if (i + 1 == avalon_get_work_count * 2)
  333. break;
  334. if (ret == AVA_SEND_BUFFER_FULL)
  335. break;
  336. i++;
  337. }
  338. applog(LOG_ERR, "Avalon: Goto idle mode");
  339. }
  340. static void get_options(int this_option_offset, int *baud, int *miner_count,
  341. int *asic_count, int *timeout, int *frequency)
  342. {
  343. char buf[BUFSIZ+1];
  344. char *ptr, *comma, *colon, *colon2, *colon3, *colon4;
  345. size_t max;
  346. int i, tmp;
  347. if (opt_avalon_options == NULL)
  348. buf[0] = '\0';
  349. else {
  350. ptr = opt_avalon_options;
  351. for (i = 0; i < this_option_offset; i++) {
  352. comma = strchr(ptr, ',');
  353. if (comma == NULL)
  354. break;
  355. ptr = comma + 1;
  356. }
  357. comma = strchr(ptr, ',');
  358. if (comma == NULL)
  359. max = strlen(ptr);
  360. else
  361. max = comma - ptr;
  362. if (max > BUFSIZ)
  363. max = BUFSIZ;
  364. strncpy(buf, ptr, max);
  365. buf[max] = '\0';
  366. }
  367. *baud = AVALON_IO_SPEED;
  368. *miner_count = AVALON_DEFAULT_MINER_NUM - 8;
  369. *asic_count = AVALON_DEFAULT_ASIC_NUM;
  370. *timeout = AVALON_DEFAULT_TIMEOUT;
  371. *frequency = AVALON_DEFAULT_FREQUENCY;
  372. if (!(*buf))
  373. return;
  374. colon = strchr(buf, ':');
  375. if (colon)
  376. *(colon++) = '\0';
  377. tmp = atoi(buf);
  378. switch (tmp) {
  379. case 115200:
  380. *baud = 115200;
  381. break;
  382. case 57600:
  383. *baud = 57600;
  384. break;
  385. case 38400:
  386. *baud = 38400;
  387. break;
  388. case 19200:
  389. *baud = 19200;
  390. break;
  391. default:
  392. quit(1,
  393. "Invalid avalon-options for baud (%s) "
  394. "must be 115200, 57600, 38400 or 19200", buf);
  395. }
  396. if (colon && *colon) {
  397. colon2 = strchr(colon, ':');
  398. if (colon2)
  399. *(colon2++) = '\0';
  400. if (*colon) {
  401. tmp = atoi(colon);
  402. if (tmp > 0 && tmp <= AVALON_DEFAULT_MINER_NUM) {
  403. *miner_count = tmp;
  404. } else {
  405. quit(1,
  406. "Invalid avalon-options for "
  407. "miner_count (%s) must be 1 ~ %d",
  408. colon, AVALON_DEFAULT_MINER_NUM);
  409. }
  410. }
  411. if (colon2 && *colon2) {
  412. colon3 = strchr(colon2, ':');
  413. if (colon3)
  414. *(colon3++) = '\0';
  415. tmp = atoi(colon2);
  416. if (tmp > 0 && tmp <= AVALON_DEFAULT_ASIC_NUM)
  417. *asic_count = tmp;
  418. else {
  419. quit(1,
  420. "Invalid avalon-options for "
  421. "asic_count (%s) must be 1 ~ %d",
  422. colon2, AVALON_DEFAULT_ASIC_NUM);
  423. }
  424. if (colon3 && *colon3) {
  425. colon4 = strchr(colon3, ':');
  426. if (colon4)
  427. *(colon4++) = '\0';
  428. tmp = atoi(colon3);
  429. if (tmp > 0 && tmp <= 0xff)
  430. *timeout = tmp;
  431. else {
  432. quit(1,
  433. "Invalid avalon-options for "
  434. "timeout (%s) must be 1 ~ %d",
  435. colon3, 0xff);
  436. }
  437. if (colon4 && *colon4) {
  438. tmp = atoi(colon4);
  439. switch (tmp) {
  440. case 256:
  441. case 270:
  442. case 282:
  443. case 300:
  444. *frequency = tmp;
  445. break;
  446. default:
  447. quit(1,
  448. "Invalid avalon-options for "
  449. "frequency must be 256/270/282/300");
  450. }
  451. }
  452. }
  453. }
  454. }
  455. }
  456. static bool avalon_detect_one(const char *devpath)
  457. {
  458. struct avalon_info *info;
  459. struct avalon_result ar;
  460. int fd, ret;
  461. int baud, miner_count, asic_count, timeout, frequency = 0;
  462. int this_option_offset = ++option_offset;
  463. get_options(this_option_offset, &baud, &miner_count, &asic_count,
  464. &timeout, &frequency);
  465. applog(LOG_DEBUG, "Avalon Detect: Attempting to open %s "
  466. "(baud=%d miner_count=%d asic_count=%d timeout=%d frequency=%d)",
  467. devpath, baud, miner_count, asic_count, timeout, frequency);
  468. fd = avalon_open2(devpath, baud, true);
  469. if (unlikely(fd == -1)) {
  470. applog(LOG_ERR, "Avalon Detect: Failed to open %s", devpath);
  471. return false;
  472. }
  473. ret = avalon_reset(fd, &ar);
  474. if (ret) {
  475. ; /* FIXME: I think IT IS avalon and wait on reset;
  476. * avalon_close(fd);
  477. * return false; */
  478. }
  479. /* We have a real Avalon! */
  480. struct cgpu_info *avalon;
  481. avalon = calloc(1, sizeof(struct cgpu_info));
  482. avalon->api = &avalon_api;
  483. avalon->device_path = strdup(devpath);
  484. avalon->device_fd = fd;
  485. avalon->threads = AVALON_MINER_THREADS;
  486. add_cgpu(avalon);
  487. avalon_info = realloc(avalon_info,
  488. sizeof(struct avalon_info *) *
  489. (total_devices + 1));
  490. applog(LOG_INFO, "Avalon Detect: Found at %s, mark as %d",
  491. devpath, avalon->device_id);
  492. avalon_info[avalon->device_id] = (struct avalon_info *)
  493. malloc(sizeof(struct avalon_info));
  494. if (unlikely(!(avalon_info[avalon->device_id])))
  495. quit(1, "Failed to malloc avalon_info");
  496. info = avalon_info[avalon->device_id];
  497. memset(info, 0, sizeof(struct avalon_info));
  498. info->baud = baud;
  499. info->miner_count = miner_count;
  500. info->asic_count = asic_count;
  501. info->timeout = timeout;
  502. info->read_count = ((float)info->timeout * AVALON_HASH_TIME_FACTOR *
  503. AVALON_TIME_FACTOR) / (float)info->miner_count;
  504. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  505. info->temp_max = 0;
  506. /* This is for check the temp/fan every 3~4s */
  507. info->temp_history_count = (4 / (float)((float)info->timeout * ((float)1.67/0x32))) + 1;
  508. if (info->temp_history_count <= 0)
  509. info->temp_history_count = 1;
  510. info->temp_history_index = 0;
  511. info->temp_sum = 0;
  512. info->temp_old = 0;
  513. info->frequency = frequency;
  514. /* Set asic to idle mode after detect */
  515. avalon_idle(avalon);
  516. avalon->device_fd = -1;
  517. avalon_close(fd);
  518. return true;
  519. }
  520. static inline void avalon_detect()
  521. {
  522. serial_detect(&avalon_api, avalon_detect_one);
  523. }
  524. static void avalon_init(struct cgpu_info *avalon)
  525. {
  526. struct avalon_result ar;
  527. int fd, ret;
  528. avalon->device_fd = -1;
  529. fd = avalon_open(avalon->device_path,
  530. avalon_info[avalon->device_id]->baud);
  531. if (unlikely(fd == -1)) {
  532. applog(LOG_ERR, "Avalon: Failed to open on %s",
  533. avalon->device_path);
  534. return;
  535. }
  536. ret = avalon_reset(fd, &ar);
  537. if (ret) {
  538. avalon_close(fd);
  539. return;
  540. }
  541. avalon->device_fd = fd;
  542. applog(LOG_INFO, "Avalon: Opened on %s", avalon->device_path);
  543. return;
  544. }
  545. static bool avalon_prepare(struct thr_info *thr)
  546. {
  547. struct cgpu_info *avalon = thr->cgpu;
  548. struct timeval now;
  549. avalon_init(avalon);
  550. if (avalon->device_fd == -1)
  551. return false;
  552. gettimeofday(&now, NULL);
  553. get_datestamp(avalon->init, &now);
  554. avalon->status = LIFE_INIT2;
  555. return true;
  556. }
  557. static void avalon_free_work(struct thr_info *thr, struct work **work)
  558. {
  559. struct cgpu_info *avalon;
  560. struct avalon_info *info;
  561. int i;
  562. if (unlikely(!work))
  563. return;
  564. avalon = thr->cgpu;
  565. info = avalon_info[avalon->device_id];
  566. for (i = 0; i < info->miner_count; i++)
  567. if (likely(work[i])) {
  568. free_work(work[i]);
  569. work[i] = NULL;
  570. }
  571. }
  572. static void do_avalon_close(struct thr_info *thr)
  573. {
  574. struct avalon_result ar;
  575. struct cgpu_info *avalon = thr->cgpu;
  576. struct avalon_info *info = avalon_info[avalon->device_id];
  577. nmsleep(1000);
  578. avalon_reset(avalon->device_fd, &ar);
  579. avalon_idle(avalon);
  580. avalon_close(avalon->device_fd);
  581. avalon->device_fd = -1;
  582. info->no_matching_work = 0;
  583. avalon_free_work(thr, info->bulk0);
  584. avalon_free_work(thr, info->bulk1);
  585. avalon_free_work(thr, info->bulk2);
  586. avalon_free_work(thr, info->bulk3);
  587. }
  588. static inline void record_temp_fan(struct avalon_info *info, struct avalon_result *ar, float *temp_avg)
  589. {
  590. int max;
  591. info->fan0 = ar->fan0 * AVALON_FAN_FACTOR;
  592. info->fan1 = ar->fan1 * AVALON_FAN_FACTOR;
  593. info->fan2 = ar->fan2 * AVALON_FAN_FACTOR;
  594. info->temp0 = ar->temp0;
  595. info->temp1 = ar->temp1;
  596. info->temp2 = ar->temp2;
  597. if (ar->temp0 & 0x80) {
  598. ar->temp0 &= 0x7f;
  599. info->temp0 = 0 - ((~ar->temp0 & 0x7f) + 1);
  600. }
  601. if (ar->temp1 & 0x80) {
  602. ar->temp1 &= 0x7f;
  603. info->temp1 = 0 - ((~ar->temp1 & 0x7f) + 1);
  604. }
  605. if (ar->temp2 & 0x80) {
  606. ar->temp2 &= 0x7f;
  607. info->temp2 = 0 - ((~ar->temp2 & 0x7f) + 1);
  608. }
  609. *temp_avg = info->temp2;
  610. max = info->temp_max;
  611. if (info->temp0 > max)
  612. max = info->temp0;
  613. if (info->temp1 > max)
  614. max = info->temp1;
  615. if (info->temp2 > max)
  616. max = info->temp2;
  617. if (max >= 100) { /* FIXME: fix the root cause on fpga controller firmware */
  618. if (opt_debug) {
  619. applog(LOG_DEBUG, "Avalon: temp_max: %d", max);
  620. hexdump((uint8_t *)ar, AVALON_READ_SIZE);
  621. }
  622. return;
  623. }
  624. info->temp_max = max;
  625. }
  626. static inline void adjust_fan(struct avalon_info *info)
  627. {
  628. int temp_new;
  629. temp_new = info->temp_sum / info->temp_history_count;
  630. if (temp_new < 35) {
  631. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  632. info->temp_old = temp_new;
  633. } else if (temp_new > 55) {
  634. info->fan_pwm = AVALON_DEFAULT_FAN_MAX_PWM;
  635. info->temp_old = temp_new;
  636. } else if (abs(temp_new - info->temp_old) >= 2) {
  637. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM + (temp_new - 35) * 6.4;
  638. info->temp_old = temp_new;
  639. }
  640. }
  641. static int64_t avalon_scanhash(struct thr_info *thr, struct work **work,
  642. __maybe_unused int64_t max_nonce)
  643. {
  644. struct cgpu_info *avalon;
  645. int fd, ret, full;
  646. struct avalon_info *info;
  647. struct avalon_task at;
  648. struct avalon_result ar;
  649. int i, work_i0, work_i1, work_i2, work_i3;
  650. int avalon_get_work_count;
  651. struct timeval tv_start, tv_finish, elapsed;
  652. uint32_t nonce;
  653. int64_t hash_count;
  654. static int first_try = 0;
  655. int result_count, result_wrong;
  656. avalon = thr->cgpu;
  657. info = avalon_info[avalon->device_id];
  658. avalon_get_work_count = info->miner_count;
  659. if (unlikely(avalon->device_fd == -1))
  660. if (!avalon_prepare(thr)) {
  661. applog(LOG_ERR, "AVA%i: Comms error(open)",
  662. avalon->device_id);
  663. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  664. /* fail the device if the reopen attempt fails */
  665. return -1;
  666. }
  667. fd = avalon->device_fd;
  668. #ifndef WIN32
  669. tcflush(fd, TCOFLUSH);
  670. #endif
  671. for (i = 0; i < avalon_get_work_count; i++) {
  672. info->bulk0[i] = info->bulk1[i];
  673. info->bulk1[i] = info->bulk2[i];
  674. info->bulk2[i] = info->bulk3[i];
  675. info->bulk3[i] = work[i];
  676. applog(LOG_DEBUG, "Avalon: bulk0/1/2 buffer [%d]: %p, %p, %p, %p",
  677. i, info->bulk0[i], info->bulk1[i], info->bulk2[i], info->bulk3[i]);
  678. }
  679. i = 0;
  680. while (true) {
  681. avalon_init_task(&at, 0, 0, info->fan_pwm,
  682. info->timeout, info->asic_count,
  683. info->miner_count, 1, 0, info->frequency);
  684. avalon_create_task(&at, work[i]);
  685. ret = avalon_send_task(fd, &at, avalon);
  686. if (unlikely(ret == AVA_SEND_ERROR ||
  687. (ret == AVA_SEND_BUFFER_EMPTY &&
  688. (i + 1 == avalon_get_work_count) &&
  689. first_try))) {
  690. avalon_free_work(thr, info->bulk0);
  691. avalon_free_work(thr, info->bulk1);
  692. avalon_free_work(thr, info->bulk2);
  693. avalon_free_work(thr, info->bulk3);
  694. do_avalon_close(thr);
  695. applog(LOG_ERR, "AVA%i: Comms error(buffer)",
  696. avalon->device_id);
  697. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  698. first_try = 0;
  699. nmsleep(1000);
  700. return 0; /* This should never happen */
  701. }
  702. if (ret == AVA_SEND_BUFFER_EMPTY && (i + 1 == avalon_get_work_count)) {
  703. first_try = 1;
  704. return 0xffffffff;
  705. }
  706. work[i]->blk.nonce = 0xffffffff;
  707. if (ret == AVA_SEND_BUFFER_FULL)
  708. break;
  709. i++;
  710. }
  711. if (unlikely(first_try))
  712. first_try = 0;
  713. elapsed.tv_sec = elapsed.tv_usec = 0;
  714. gettimeofday(&tv_start, NULL);
  715. result_count = 0;
  716. result_wrong = 0;
  717. hash_count = 0;
  718. while (true) {
  719. work_i0 = work_i1 = work_i2 = work_i3 = -1;
  720. full = avalon_buffer_full(fd);
  721. applog(LOG_DEBUG, "Avalon: Buffer full: %s",
  722. ((full == AVA_BUFFER_FULL) ? "Yes" : "No"));
  723. if (unlikely(full == AVA_BUFFER_EMPTY))
  724. break;
  725. ret = avalon_get_result(fd, &ar, thr, &tv_finish);
  726. if (unlikely(ret == AVA_GETS_ERROR)) {
  727. avalon_free_work(thr, info->bulk0);
  728. avalon_free_work(thr, info->bulk1);
  729. avalon_free_work(thr, info->bulk2);
  730. avalon_free_work(thr, info->bulk3);
  731. do_avalon_close(thr);
  732. applog(LOG_ERR,
  733. "AVA%i: Comms error(read)", avalon->device_id);
  734. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  735. return 0;
  736. }
  737. if (unlikely(ret == AVA_GETS_TIMEOUT)) {
  738. timersub(&tv_finish, &tv_start, &elapsed);
  739. applog(LOG_DEBUG, "Avalon: no nonce in (%ld.%06lds)",
  740. (long)elapsed.tv_sec, (long)elapsed.tv_usec);
  741. continue;
  742. }
  743. if (unlikely(ret == AVA_GETS_RESTART)) {
  744. avalon_free_work(thr, info->bulk0);
  745. avalon_free_work(thr, info->bulk1);
  746. avalon_free_work(thr, info->bulk2);
  747. avalon_free_work(thr, info->bulk3);
  748. continue;
  749. }
  750. result_count++;
  751. work_i0 = avalon_decode_nonce(thr, info->bulk0, &ar, &nonce);
  752. work_i1 = avalon_decode_nonce(thr, info->bulk1, &ar, &nonce);
  753. work_i2 = avalon_decode_nonce(thr, info->bulk2, &ar, &nonce);
  754. work_i3 = avalon_decode_nonce(thr, info->bulk3, &ar, &nonce);
  755. if ((work_i0 < 0) && (work_i1 < 0) && (work_i2 < 0) && (work_i3 < 0)) {
  756. info->no_matching_work++;
  757. result_wrong++;
  758. if (opt_debug) {
  759. timersub(&tv_finish, &tv_start, &elapsed);
  760. applog(LOG_DEBUG,"Avalon: no matching work: %d"
  761. " (%ld.%06lds)", info->no_matching_work,
  762. (long)elapsed.tv_sec, (long)elapsed.tv_usec);
  763. }
  764. continue;
  765. }
  766. if (work_i0 >= 0)
  767. submit_nonce(thr, info->bulk0[work_i0], nonce);
  768. if (work_i1 >= 0)
  769. submit_nonce(thr, info->bulk1[work_i1], nonce);
  770. if (work_i2 >= 0)
  771. submit_nonce(thr, info->bulk2[work_i2], nonce);
  772. if (work_i3 >= 0)
  773. submit_nonce(thr, info->bulk3[work_i3], nonce);
  774. hash_count += nonce;
  775. if (opt_debug) {
  776. timersub(&tv_finish, &tv_start, &elapsed);
  777. applog(LOG_DEBUG,
  778. "Avalon: nonce = 0x%08"PRIx32" = 0x%08"PRIx64" hashes "
  779. "(%ld.%06lds)", nonce, (uint64_t)hash_count,
  780. (long)elapsed.tv_sec, (long)elapsed.tv_usec);
  781. }
  782. }
  783. if (result_count == result_wrong) {
  784. /* This mean FPGA controller give all wrong result
  785. * try to reset the Avalon */
  786. avalon_free_work(thr, info->bulk0);
  787. avalon_free_work(thr, info->bulk1);
  788. avalon_free_work(thr, info->bulk2);
  789. avalon_free_work(thr, info->bulk3);
  790. do_avalon_close(thr);
  791. applog(LOG_ERR,
  792. "AVA%i: FPGA controller mess up", avalon->device_id);
  793. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  794. return 0;
  795. }
  796. avalon_free_work(thr, info->bulk0);
  797. record_temp_fan(info, &ar, &(avalon->temp));
  798. applog(LOG_INFO,
  799. "Avalon: Fan1: %d/m, Fan2: %d/m, Fan3: %d/m\t"
  800. "Temp1: %dC, Temp2: %dC, Temp3: %dC, TempMAX: %dC",
  801. info->fan0, info->fan1, info->fan2,
  802. info->temp0, info->temp1, info->temp2, info->temp_max);
  803. info->temp_history_index++;
  804. info->temp_sum += info->temp2;
  805. applog(LOG_DEBUG, "Avalon: temp_index: %d, temp_count: %d, temp_old: %d",
  806. info->temp_history_index, info->temp_history_count, info->temp_old);
  807. if (info->temp_history_index == info->temp_history_count) {
  808. adjust_fan(info);
  809. info->temp_history_index = 0;
  810. info->temp_sum = 0;
  811. }
  812. /*
  813. * FIXME: Each work split to 10 pieces, each piece send to a
  814. * asic(256MHs). one work can be mulit-nonce back. it is not
  815. * easy calculate correct hash on such situation. so I simplely
  816. * add each nonce to hash_count. base on Utility/m hash_count*2
  817. * give a very good result.
  818. *
  819. * Any patch will be great.
  820. */
  821. return (hash_count * 2);
  822. }
  823. // minerloop_scanhash hacked to handle Avalon's many processors
  824. static
  825. void minerloop_avalon(struct thr_info *mythr)
  826. {
  827. const int thr_id = mythr->id;
  828. struct cgpu_info *cgpu = mythr->cgpu;
  829. const struct device_api *api = cgpu->api;
  830. struct timeval tv_start, tv_end;
  831. struct timeval tv_hashes;
  832. uint32_t max_nonce = api->can_limit_work ? api->can_limit_work(mythr) : 0xffffffff;
  833. int64_t hashes;
  834. struct avalon_info *info = avalon_info[cgpu->device_id];
  835. int i;
  836. int avalon_get_work_count = info->miner_count;
  837. struct work **work = calloc(1,
  838. avalon_get_work_count * sizeof(struct work *));
  839. if (!work)
  840. quit(1, "Faile on Avalon calloc");
  841. const bool primary = (!mythr->device_thread) || mythr->primary_thread;
  842. while (1) {
  843. mythr->work_restart = false;
  844. for (i = 0; i < avalon_get_work_count; i++)
  845. request_work(mythr);
  846. for (i = 0; i < avalon_get_work_count; i++) {
  847. work[i] = get_work(mythr);
  848. work[i]->blk.nonce = 0;
  849. }
  850. for (i = 0; i < avalon_get_work_count; i++) {
  851. if (api->prepare_work && !api->prepare_work(mythr, work[i])) {
  852. applog(LOG_ERR, "work prepare failed, exiting "
  853. "mining thread %d", thr_id);
  854. break;
  855. }
  856. gettimeofday(&(work[i]->tv_work_start), NULL);
  857. }
  858. do {
  859. thread_reportin(mythr);
  860. gettimeofday(&tv_start, NULL);
  861. hashes = api->scanhash_queue(mythr, work, max_nonce);
  862. gettimeofday(&tv_end, NULL);
  863. thread_reportin(mythr);
  864. timersub(&tv_end, &tv_start, &tv_hashes);
  865. if (!hashes_done(mythr, hashes, &tv_hashes, api->can_limit_work ? &max_nonce : NULL))
  866. goto disabled;
  867. if (unlikely(mythr->work_restart)) {
  868. /* Apart from device_thread 0, we stagger the
  869. * starting of every next thread to try and get
  870. * all devices busy before worrying about
  871. * getting work for their extra threads */
  872. if (!primary) {
  873. struct timespec rgtp;
  874. rgtp.tv_sec = 0;
  875. rgtp.tv_nsec = 250 * mythr->device_thread * 1000000;
  876. nanosleep(&rgtp, NULL);
  877. }
  878. break;
  879. }
  880. if (unlikely(mythr->pause || cgpu->deven != DEV_ENABLED))
  881. disabled:
  882. mt_disable(mythr);
  883. } while (false);
  884. }
  885. free(work);
  886. }
  887. static struct api_data *avalon_api_stats(struct cgpu_info *cgpu)
  888. {
  889. struct api_data *root = NULL;
  890. struct avalon_info *info = avalon_info[cgpu->device_id];
  891. root = api_add_int(root, "baud", &(info->baud), false);
  892. root = api_add_int(root, "miner_count", &(info->miner_count),false);
  893. root = api_add_int(root, "asic_count", &(info->asic_count), false);
  894. root = api_add_int(root, "read_count", &(info->read_count), false);
  895. root = api_add_int(root, "timeout", &(info->timeout), false);
  896. root = api_add_int(root, "frequency", &(info->frequency), false);
  897. root = api_add_int(root, "fan1", &(info->fan0), false);
  898. root = api_add_int(root, "fan2", &(info->fan1), false);
  899. root = api_add_int(root, "fan3", &(info->fan2), false);
  900. root = api_add_int(root, "temp1", &(info->temp0), false);
  901. root = api_add_int(root, "temp2", &(info->temp1), false);
  902. root = api_add_int(root, "temp3", &(info->temp2), false);
  903. root = api_add_int(root, "temp_max", &(info->temp_max), false);
  904. root = api_add_int(root, "no_matching_work", &(info->no_matching_work), false);
  905. root = api_add_int(root, "matching_work_count1", &(info->matching_work[0]), false);
  906. root = api_add_int(root, "matching_work_count2", &(info->matching_work[1]), false);
  907. root = api_add_int(root, "matching_work_count3", &(info->matching_work[2]), false);
  908. root = api_add_int(root, "matching_work_count4", &(info->matching_work[3]), false);
  909. root = api_add_int(root, "matching_work_count5", &(info->matching_work[4]), false);
  910. root = api_add_int(root, "matching_work_count6", &(info->matching_work[5]), false);
  911. root = api_add_int(root, "matching_work_count7", &(info->matching_work[6]), false);
  912. root = api_add_int(root, "matching_work_count8", &(info->matching_work[7]), false);
  913. root = api_add_int(root, "matching_work_count9", &(info->matching_work[8]), false);
  914. root = api_add_int(root, "matching_work_count10", &(info->matching_work[9]), false);
  915. root = api_add_int(root, "matching_work_count11", &(info->matching_work[10]), false);
  916. root = api_add_int(root, "matching_work_count12", &(info->matching_work[11]), false);
  917. root = api_add_int(root, "matching_work_count13", &(info->matching_work[12]), false);
  918. root = api_add_int(root, "matching_work_count14", &(info->matching_work[13]), false);
  919. root = api_add_int(root, "matching_work_count15", &(info->matching_work[14]), false);
  920. root = api_add_int(root, "matching_work_count16", &(info->matching_work[15]), false);
  921. root = api_add_int(root, "matching_work_count17", &(info->matching_work[16]), false);
  922. root = api_add_int(root, "matching_work_count18", &(info->matching_work[17]), false);
  923. root = api_add_int(root, "matching_work_count19", &(info->matching_work[18]), false);
  924. root = api_add_int(root, "matching_work_count20", &(info->matching_work[19]), false);
  925. root = api_add_int(root, "matching_work_count21", &(info->matching_work[20]), false);
  926. root = api_add_int(root, "matching_work_count22", &(info->matching_work[21]), false);
  927. root = api_add_int(root, "matching_work_count23", &(info->matching_work[22]), false);
  928. root = api_add_int(root, "matching_work_count24", &(info->matching_work[23]), false);
  929. return root;
  930. }
  931. static void avalon_shutdown(struct thr_info *thr)
  932. {
  933. do_avalon_close(thr);
  934. }
  935. struct device_api avalon_api = {
  936. .dname = "avalon",
  937. .name = "AVA",
  938. .api_detect = avalon_detect,
  939. .thread_prepare = avalon_prepare,
  940. .minerloop = minerloop_avalon,
  941. .scanhash_queue = avalon_scanhash,
  942. .get_api_stats = avalon_api_stats,
  943. .reinit_device = avalon_init,
  944. .thread_shutdown = avalon_shutdown,
  945. };