driver-avalon.c 29 KB

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