driver-avalon.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. /*
  2. * Copyright 2013 Con Kolivas <kernel@kolivas.org>
  3. * Copyright 2012-2013 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 <sys/select.h>
  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 "compat.h"
  30. #include <windows.h>
  31. #include <io.h>
  32. #endif
  33. #include "elist.h"
  34. #include "miner.h"
  35. #include "fpgautils.h"
  36. #include "driver-avalon.h"
  37. #include "hexdump.c"
  38. #include "util.h"
  39. static int option_offset = -1;
  40. struct avalon_info **avalon_infos;
  41. struct device_drv avalon_drv;
  42. static int avalon_init_task(struct avalon_task *at,
  43. uint8_t reset, uint8_t ff, uint8_t fan,
  44. uint8_t timeout, uint8_t asic_num,
  45. uint8_t miner_num, uint8_t nonce_elf,
  46. uint8_t gate_miner, int frequency)
  47. {
  48. uint8_t *buf;
  49. static bool first = true;
  50. if (unlikely(!at))
  51. return -1;
  52. if (unlikely(timeout <= 0 || asic_num <= 0 || miner_num <= 0))
  53. return -1;
  54. memset(at, 0, sizeof(struct avalon_task));
  55. if (unlikely(reset)) {
  56. at->reset = 1;
  57. at->fan_eft = 1;
  58. at->timer_eft = 1;
  59. first = true;
  60. }
  61. at->flush_fifo = (ff ? 1 : 0);
  62. at->fan_eft = (fan ? 1 : 0);
  63. if (unlikely(first && !at->reset)) {
  64. at->fan_eft = 1;
  65. at->timer_eft = 1;
  66. first = false;
  67. }
  68. at->fan_pwm_data = (fan ? fan : AVALON_DEFAULT_FAN_MAX_PWM);
  69. at->timeout_data = timeout;
  70. at->asic_num = asic_num;
  71. at->miner_num = miner_num;
  72. at->nonce_elf = nonce_elf;
  73. at->gate_miner_elf = 1;
  74. at->asic_pll = 1;
  75. if (unlikely(gate_miner)) {
  76. at-> gate_miner = 1;
  77. at->asic_pll = 0;
  78. }
  79. buf = (uint8_t *)at;
  80. buf[5] = 0x00;
  81. buf[8] = 0x74;
  82. buf[9] = 0x01;
  83. buf[10] = 0x00;
  84. buf[11] = 0x00;
  85. if (frequency == 256) {
  86. buf[6] = 0x03;
  87. buf[7] = 0x08;
  88. } else if (frequency == 270) {
  89. buf[6] = 0x73;
  90. buf[7] = 0x08;
  91. } else if (frequency == 282) {
  92. buf[6] = 0xd3;
  93. buf[7] = 0x08;
  94. } else if (frequency == 300) {
  95. buf[6] = 0x63;
  96. buf[7] = 0x09;
  97. }
  98. return 0;
  99. }
  100. static inline void avalon_create_task(struct avalon_task *at,
  101. struct work *work)
  102. {
  103. memcpy(at->midstate, work->midstate, 32);
  104. memcpy(at->data, work->data + 64, 12);
  105. }
  106. static int avalon_write(int fd, char *buf, ssize_t len)
  107. {
  108. ssize_t wrote = 0;
  109. while (len > 0) {
  110. struct timeval timeout;
  111. ssize_t ret;
  112. fd_set wd;
  113. timeout.tv_sec = 0;
  114. timeout.tv_usec = 100000;
  115. FD_ZERO(&wd);
  116. FD_SET((SOCKETTYPE)fd, &wd);
  117. ret = select(fd + 1, NULL, &wd, NULL, &timeout);
  118. if (unlikely(ret < 1)) {
  119. applog(LOG_WARNING, "Select error on avalon_write");
  120. return AVA_SEND_ERROR;
  121. }
  122. ret = write(fd, buf + wrote, len);
  123. if (unlikely(ret < 1)) {
  124. applog(LOG_WARNING, "Write error on avalon_write");
  125. return AVA_SEND_ERROR;
  126. }
  127. wrote += ret;
  128. len -= ret;
  129. }
  130. return AVA_SEND_OK;
  131. }
  132. static int avalon_send_task(int fd, const struct avalon_task *at,
  133. struct cgpu_info *avalon)
  134. {
  135. struct timespec p;
  136. uint8_t buf[AVALON_WRITE_SIZE + 4 * AVALON_DEFAULT_ASIC_NUM];
  137. size_t nr_len;
  138. struct avalon_info *info;
  139. uint64_t delay = 32000000; /* Default 32ms for B19200 */
  140. uint32_t nonce_range;
  141. int ret, i;
  142. if (at->nonce_elf)
  143. nr_len = AVALON_WRITE_SIZE + 4 * at->asic_num;
  144. else
  145. nr_len = AVALON_WRITE_SIZE;
  146. memcpy(buf, at, AVALON_WRITE_SIZE);
  147. if (at->nonce_elf) {
  148. nonce_range = (uint32_t)0xffffffff / at->asic_num;
  149. for (i = 0; i < at->asic_num; i++) {
  150. buf[AVALON_WRITE_SIZE + (i * 4) + 3] =
  151. (i * nonce_range & 0xff000000) >> 24;
  152. buf[AVALON_WRITE_SIZE + (i * 4) + 2] =
  153. (i * nonce_range & 0x00ff0000) >> 16;
  154. buf[AVALON_WRITE_SIZE + (i * 4) + 1] =
  155. (i * nonce_range & 0x0000ff00) >> 8;
  156. buf[AVALON_WRITE_SIZE + (i * 4) + 0] =
  157. (i * nonce_range & 0x000000ff) >> 0;
  158. }
  159. }
  160. #if defined(__BIG_ENDIAN__) || defined(MIPSEB)
  161. uint8_t tt = 0;
  162. tt = (buf[0] & 0x0f) << 4;
  163. tt |= ((buf[0] & 0x10) ? (1 << 3) : 0);
  164. tt |= ((buf[0] & 0x20) ? (1 << 2) : 0);
  165. tt |= ((buf[0] & 0x40) ? (1 << 1) : 0);
  166. tt |= ((buf[0] & 0x80) ? (1 << 0) : 0);
  167. buf[0] = tt;
  168. tt = (buf[4] & 0x0f) << 4;
  169. tt |= ((buf[4] & 0x10) ? (1 << 3) : 0);
  170. tt |= ((buf[4] & 0x20) ? (1 << 2) : 0);
  171. tt |= ((buf[4] & 0x40) ? (1 << 1) : 0);
  172. tt |= ((buf[4] & 0x80) ? (1 << 0) : 0);
  173. buf[4] = tt;
  174. #endif
  175. if (likely(avalon)) {
  176. info = avalon->device_data;
  177. delay = nr_len * 10 * 1000000000ULL;
  178. delay = delay / info->baud;
  179. }
  180. if (at->reset)
  181. nr_len = 1;
  182. if (opt_debug) {
  183. applog(LOG_DEBUG, "Avalon: Sent(%u):", (unsigned int)nr_len);
  184. hexdump(buf, nr_len);
  185. }
  186. ret = avalon_write(fd, (char *)buf, nr_len);
  187. p.tv_sec = 0;
  188. p.tv_nsec = (long)delay + 4000000;
  189. nanosleep(&p, NULL);
  190. applog(LOG_DEBUG, "Avalon: Sent: Buffer delay: %ld", p.tv_nsec);
  191. return ret;
  192. }
  193. static bool avalon_decode_nonce(struct thr_info *thr, struct cgpu_info *avalon,
  194. struct avalon_info *info, struct avalon_result *ar,
  195. struct work *work)
  196. {
  197. uint32_t nonce;
  198. info = avalon->device_data;
  199. info->matching_work[work->subid]++;
  200. nonce = htole32(ar->nonce);
  201. applog(LOG_DEBUG, "Avalon: nonce = %0x08x", nonce);
  202. return submit_nonce(thr, work, nonce);
  203. }
  204. static int avalon_read(int fd, char *buf, ssize_t len)
  205. {
  206. ssize_t aread = 0;
  207. while (len > 0) {
  208. struct timeval timeout;
  209. ssize_t ret;
  210. fd_set rd;
  211. timeout.tv_sec = 0;
  212. timeout.tv_usec = 100000;
  213. FD_ZERO(&rd);
  214. FD_SET((SOCKETTYPE)fd, &rd);
  215. ret = select(fd + 1, &rd, NULL, NULL, &timeout);
  216. if (unlikely(ret < 1)) {
  217. applog(LOG_WARNING, "Select error on avalon_read");
  218. return AVA_GETS_ERROR;
  219. }
  220. ret = read(fd, buf + aread, len);
  221. if (unlikely(ret < 1)) {
  222. applog(LOG_WARNING, "Read error on avalon_read");
  223. return AVA_GETS_ERROR;
  224. }
  225. aread += ret;
  226. len -= ret;
  227. }
  228. return AVA_GETS_OK;
  229. }
  230. /* Wait until the ftdi chip returns a CTS saying we can send more data. The
  231. * status is updated every 40ms. */
  232. static void wait_avalon_ready(int fd)
  233. {
  234. while (avalon_buffer_full(fd) == AVA_BUFFER_FULL) {
  235. nmsleep(40);
  236. }
  237. }
  238. static int avalon_reset(struct cgpu_info *avalon, int fd, bool initial)
  239. {
  240. struct avalon_result ar;
  241. struct avalon_task at;
  242. uint8_t *buf, *tmp;
  243. int ret, i, spare;
  244. struct timespec p;
  245. /* Send reset, then check for result */
  246. avalon_init_task(&at, 1, 0,
  247. AVALON_DEFAULT_FAN_MAX_PWM,
  248. AVALON_DEFAULT_TIMEOUT,
  249. AVALON_DEFAULT_ASIC_NUM,
  250. AVALON_DEFAULT_MINER_NUM,
  251. 0, 0,
  252. AVALON_DEFAULT_FREQUENCY);
  253. wait_avalon_ready(fd);
  254. ret = avalon_send_task(fd, &at, NULL);
  255. if (unlikely(ret == AVA_SEND_ERROR))
  256. return -1;
  257. if (!initial) {
  258. applog(LOG_ERR, "AVA%d reset sequence sent", avalon->device_id);
  259. return 0;
  260. }
  261. ret = avalon_read(fd, (char *)&ar, AVALON_READ_SIZE);
  262. if (unlikely(ret == AVA_GETS_ERROR))
  263. return -1;
  264. /* What do these sleeps do?? */
  265. p.tv_sec = 0;
  266. p.tv_nsec = AVALON_RESET_PITCH;
  267. nanosleep(&p, NULL);
  268. /* Look for the first occurrence of 0xAA, the reset response should be:
  269. * AA 55 AA 55 00 00 00 00 00 00 */
  270. spare = AVALON_READ_SIZE - 10;
  271. tmp = (uint8_t *)&ar;
  272. if (opt_debug) {
  273. applog(LOG_DEBUG, "AVA%d reset: get:", avalon->device_id);
  274. hexdump(tmp, AVALON_READ_SIZE);
  275. }
  276. for (i = 0; i <= spare; i++) {
  277. buf = &tmp[i];
  278. if (buf[0] == 0xAA)
  279. break;
  280. }
  281. i = 0;
  282. if (buf[0] == 0xAA && buf[1] == 0x55 &&
  283. buf[2] == 0xAA && buf[3] == 0x55) {
  284. for (i = 4; i < 11; i++)
  285. if (buf[i] != 0)
  286. break;
  287. }
  288. if (i != 11) {
  289. applog(LOG_ERR, "AVA%d: Reset failed! not an Avalon?"
  290. " (%d: %02x %02x %02x %02x)", avalon->device_id,
  291. i, buf[0], buf[1], buf[2], buf[3]);
  292. /* FIXME: return 1; */
  293. } else
  294. applog(LOG_WARNING, "AVA%d: Reset succeeded",
  295. avalon->device_id);
  296. return 0;
  297. }
  298. static void get_options(int this_option_offset, int *baud, int *miner_count,
  299. int *asic_count, int *timeout, int *frequency)
  300. {
  301. char err_buf[BUFSIZ+1];
  302. char buf[BUFSIZ+1];
  303. char *ptr, *comma, *colon, *colon2, *colon3, *colon4;
  304. size_t max;
  305. int i, tmp;
  306. if (opt_avalon_options == NULL)
  307. buf[0] = '\0';
  308. else {
  309. ptr = opt_avalon_options;
  310. for (i = 0; i < this_option_offset; i++) {
  311. comma = strchr(ptr, ',');
  312. if (comma == NULL)
  313. break;
  314. ptr = comma + 1;
  315. }
  316. comma = strchr(ptr, ',');
  317. if (comma == NULL)
  318. max = strlen(ptr);
  319. else
  320. max = comma - ptr;
  321. if (max > BUFSIZ)
  322. max = BUFSIZ;
  323. strncpy(buf, ptr, max);
  324. buf[max] = '\0';
  325. }
  326. *baud = AVALON_IO_SPEED;
  327. *miner_count = AVALON_DEFAULT_MINER_NUM - 8;
  328. *asic_count = AVALON_DEFAULT_ASIC_NUM;
  329. *timeout = AVALON_DEFAULT_TIMEOUT;
  330. *frequency = AVALON_DEFAULT_FREQUENCY;
  331. if (!(*buf))
  332. return;
  333. colon = strchr(buf, ':');
  334. if (colon)
  335. *(colon++) = '\0';
  336. tmp = atoi(buf);
  337. switch (tmp) {
  338. case 115200:
  339. *baud = 115200;
  340. break;
  341. case 57600:
  342. *baud = 57600;
  343. break;
  344. case 38400:
  345. *baud = 38400;
  346. break;
  347. case 19200:
  348. *baud = 19200;
  349. break;
  350. default:
  351. sprintf(err_buf,
  352. "Invalid avalon-options for baud (%s) "
  353. "must be 115200, 57600, 38400 or 19200", buf);
  354. quit(1, err_buf);
  355. }
  356. if (colon && *colon) {
  357. colon2 = strchr(colon, ':');
  358. if (colon2)
  359. *(colon2++) = '\0';
  360. if (*colon) {
  361. tmp = atoi(colon);
  362. if (tmp > 0 && tmp <= AVALON_DEFAULT_MINER_NUM) {
  363. *miner_count = tmp;
  364. } else {
  365. sprintf(err_buf,
  366. "Invalid avalon-options for "
  367. "miner_count (%s) must be 1 ~ %d",
  368. colon, AVALON_DEFAULT_MINER_NUM);
  369. quit(1, err_buf);
  370. }
  371. }
  372. if (colon2 && *colon2) {
  373. colon3 = strchr(colon2, ':');
  374. if (colon3)
  375. *(colon3++) = '\0';
  376. tmp = atoi(colon2);
  377. if (tmp > 0 && tmp <= AVALON_DEFAULT_ASIC_NUM)
  378. *asic_count = tmp;
  379. else {
  380. sprintf(err_buf,
  381. "Invalid avalon-options for "
  382. "asic_count (%s) must be 1 ~ %d",
  383. colon2, AVALON_DEFAULT_ASIC_NUM);
  384. quit(1, err_buf);
  385. }
  386. if (colon3 && *colon3) {
  387. colon4 = strchr(colon3, ':');
  388. if (colon4)
  389. *(colon4++) = '\0';
  390. tmp = atoi(colon3);
  391. if (tmp > 0 && tmp <= 0xff)
  392. *timeout = tmp;
  393. else {
  394. sprintf(err_buf,
  395. "Invalid avalon-options for "
  396. "timeout (%s) must be 1 ~ %d",
  397. colon3, 0xff);
  398. quit(1, err_buf);
  399. }
  400. if (colon4 && *colon4) {
  401. tmp = atoi(colon4);
  402. switch (tmp) {
  403. case 256:
  404. case 270:
  405. case 282:
  406. case 300:
  407. *frequency = tmp;
  408. break;
  409. default:
  410. sprintf(err_buf,
  411. "Invalid avalon-options for "
  412. "frequency must be 256/270/282/300");
  413. quit(1, err_buf);
  414. }
  415. }
  416. }
  417. }
  418. }
  419. }
  420. static void avalon_idle(struct cgpu_info *avalon, struct avalon_info *info,
  421. int fd)
  422. {
  423. int i;
  424. info->idle = true;
  425. wait_avalon_ready(fd);
  426. applog(LOG_WARNING, "AVA%i: Idling %d miners", avalon->device_id,
  427. info->miner_count);
  428. /* Send idle to all miners */
  429. for (i = 0; i < info->miner_count; i++) {
  430. struct avalon_task at;
  431. avalon_init_task(&at, 0, 0, info->fan_pwm, info->timeout,
  432. info->asic_count, info->miner_count, 1, 1,
  433. info->frequency);
  434. avalon_send_task(fd, &at, avalon);
  435. }
  436. wait_avalon_ready(fd);
  437. }
  438. static bool avalon_detect_one(const char *devpath)
  439. {
  440. struct avalon_info *info;
  441. int fd, ret;
  442. int baud, miner_count, asic_count, timeout, frequency = 0;
  443. struct cgpu_info *avalon;
  444. int this_option_offset = ++option_offset;
  445. get_options(this_option_offset, &baud, &miner_count, &asic_count,
  446. &timeout, &frequency);
  447. applog(LOG_DEBUG, "Avalon Detect: Attempting to open %s "
  448. "(baud=%d miner_count=%d asic_count=%d timeout=%d frequency=%d)",
  449. devpath, baud, miner_count, asic_count, timeout, frequency);
  450. fd = avalon_open2(devpath, baud, true);
  451. if (unlikely(fd == -1)) {
  452. applog(LOG_ERR, "Avalon Detect: Failed to open %s", devpath);
  453. return false;
  454. }
  455. /* We have a real Avalon! */
  456. avalon = calloc(1, sizeof(struct cgpu_info));
  457. avalon->drv = &avalon_drv;
  458. avalon->device_path = strdup(devpath);
  459. avalon->device_fd = fd;
  460. avalon->threads = AVALON_MINER_THREADS;
  461. add_cgpu(avalon);
  462. avalon_infos = realloc(avalon_infos,
  463. sizeof(struct avalon_info *) *
  464. (total_devices + 1));
  465. applog(LOG_INFO, "Avalon Detect: Found at %s, mark as %d",
  466. devpath, avalon->device_id);
  467. avalon_infos[avalon->device_id] = calloc(sizeof(struct avalon_info), 1);
  468. if (unlikely(!(avalon_infos[avalon->device_id])))
  469. quit(1, "Failed to calloc avalon_infos");
  470. avalon->device_data = avalon_infos[avalon->device_id];
  471. info = avalon->device_data;
  472. info->baud = baud;
  473. info->miner_count = miner_count;
  474. info->asic_count = asic_count;
  475. info->timeout = timeout;
  476. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  477. info->temp_max = 0;
  478. /* This is for check the temp/fan every 3~4s */
  479. info->temp_history_count = (4 / (float)((float)info->timeout * ((float)1.67/0x32))) + 1;
  480. if (info->temp_history_count <= 0)
  481. info->temp_history_count = 1;
  482. info->temp_history_index = 0;
  483. info->temp_sum = 0;
  484. info->temp_old = 0;
  485. info->frequency = frequency;
  486. ret = avalon_reset(avalon, fd, true);
  487. if (ret) {
  488. ; /* FIXME: I think IT IS avalon and wait on reset;
  489. * avalon_close(fd);
  490. * return false; */
  491. }
  492. avalon_idle(avalon, info, fd);
  493. return true;
  494. }
  495. static inline void avalon_detect()
  496. {
  497. serial_detect(&avalon_drv, avalon_detect_one);
  498. }
  499. static void avalon_init(struct cgpu_info *avalon)
  500. {
  501. applog(LOG_INFO, "Avalon: Opened on %s", avalon->device_path);
  502. }
  503. static struct work *avalon_valid_result(struct cgpu_info *avalon, struct avalon_result *ar)
  504. {
  505. return find_queued_work_bymidstate(avalon, (char *)ar->midstate, 32,
  506. (char *)ar->data, 64, 12);
  507. }
  508. static void avalon_update_temps(struct cgpu_info *avalon, struct avalon_info *info,
  509. struct avalon_result *ar);
  510. static void avalon_inc_nvw(struct cgpu_info *avalon, struct avalon_info *info,
  511. struct thr_info *thr)
  512. {
  513. if (unlikely(info->idle))
  514. return;
  515. applog(LOG_WARNING, "%s%d: No valid work - HW error",
  516. thr->cgpu->drv->name, thr->cgpu->device_id);
  517. inc_hw_errors(thr);
  518. mutex_lock(&info->lock);
  519. info->no_matching_work++;
  520. avalon->results--;
  521. mutex_unlock(&info->lock);
  522. }
  523. static void avalon_parse_results(struct cgpu_info *avalon, struct avalon_info *info,
  524. struct thr_info *thr, char *buf, int *offset)
  525. {
  526. int i, spare = *offset - AVALON_READ_SIZE;
  527. bool found = false;
  528. for (i = 0; i <= spare; i++) {
  529. struct avalon_result *ar;
  530. struct work *work;
  531. ar = (struct avalon_result *)&buf[i];
  532. work = avalon_valid_result(avalon, ar);
  533. if (work) {
  534. bool gettemp = false;
  535. found = true;
  536. if (avalon_decode_nonce(thr, avalon, info, ar, work)) {
  537. mutex_lock(&info->lock);
  538. if (++avalon->results > 0 &&
  539. !(avalon->results % info->miner_count)) {
  540. gettemp = true;
  541. avalon->results = 0;
  542. }
  543. info->nonces++;
  544. mutex_unlock(&info->lock);
  545. } else {
  546. mutex_lock(&info->lock);
  547. avalon->results--;
  548. mutex_unlock(&info->lock);
  549. }
  550. if (gettemp)
  551. avalon_update_temps(avalon, info, ar);
  552. break;
  553. }
  554. }
  555. if (!found) {
  556. spare = *offset - AVALON_READ_SIZE;
  557. /* We are buffering and haven't accumulated one more corrupt
  558. * work result. */
  559. if (spare < (int)AVALON_READ_SIZE)
  560. return;
  561. avalon_inc_nvw(avalon, info, thr);
  562. } else {
  563. spare = AVALON_READ_SIZE + i;
  564. if (i) {
  565. if (i >= (int)AVALON_READ_SIZE)
  566. avalon_inc_nvw(avalon, info, thr);
  567. else
  568. applog(LOG_WARNING, "Avalon: Discarding %d bytes from buffer", i);
  569. }
  570. }
  571. *offset -= spare;
  572. memmove(buf, buf + spare, *offset);
  573. }
  574. static void __avalon_running_reset(struct cgpu_info *avalon,
  575. struct avalon_info *info, int fd)
  576. {
  577. info->reset = true;
  578. avalon_reset(avalon, fd, false);
  579. avalon_idle(avalon, info, fd);
  580. avalon->results = 0;
  581. info->reset = false;
  582. }
  583. static void avalon_running_reset(struct cgpu_info *avalon,
  584. struct avalon_info *info, int fd)
  585. {
  586. /* Lock to prevent more work being sent during reset */
  587. mutex_lock(&info->qlock);
  588. __avalon_running_reset(avalon, info, fd);
  589. mutex_unlock(&info->qlock);
  590. }
  591. static void *avalon_get_results(void *userdata)
  592. {
  593. struct cgpu_info *avalon = (struct cgpu_info *)userdata;
  594. struct avalon_info *info = avalon->device_data;
  595. const int rsize = AVALON_FTDI_READSIZE;
  596. char readbuf[AVALON_READBUF_SIZE];
  597. struct thr_info *thr = info->thr;
  598. int fd = avalon->device_fd;
  599. char threadname[24];
  600. int offset = 0;
  601. pthread_detach(pthread_self());
  602. snprintf(threadname, 24, "ava_recv/%d", avalon->device_id);
  603. RenameThread(threadname);
  604. while (42) {
  605. struct timeval timeout;
  606. char buf[rsize];
  607. ssize_t ret;
  608. fd_set rd;
  609. if (offset >= (int)AVALON_READ_SIZE)
  610. avalon_parse_results(avalon, info, thr, readbuf, &offset);
  611. /* Check for nothing but consecutive bad results and reset the
  612. * FPGA if necessary */
  613. if (unlikely(avalon->results <= -info->miner_count)) {
  614. applog(LOG_ERR, "AVA%d: %d invalid consecutive results, resetting",
  615. avalon->device_id, -avalon->results);
  616. avalon_running_reset(avalon, info, fd);
  617. }
  618. if (unlikely(offset + rsize >= AVALON_READBUF_SIZE)) {
  619. /* This should never happen */
  620. applog(LOG_ERR, "Avalon readbuf overflow, resetting buffer");
  621. offset = 0;
  622. }
  623. timeout.tv_sec = 0;
  624. timeout.tv_usec = AVALON_READ_TIMEOUT * 1000;
  625. FD_ZERO(&rd);
  626. FD_SET((SOCKETTYPE)fd, &rd);
  627. ret = select(fd + 1, &rd, NULL, NULL, &timeout);
  628. if (ret < 1) {
  629. if (unlikely(ret < 0))
  630. applog(LOG_WARNING, "Select error in avalon_get_results");
  631. continue;
  632. }
  633. ret = read(fd, buf, AVALON_FTDI_READSIZE);
  634. if (unlikely(ret < 1)) {
  635. if (unlikely(ret < 0))
  636. applog(LOG_WARNING, "Read error in avalon_get_results");
  637. continue;
  638. }
  639. if (opt_debug) {
  640. applog(LOG_DEBUG, "Avalon: get:");
  641. hexdump((uint8_t *)buf, ret);
  642. }
  643. /* During a reset, goes on reading but discards anything */
  644. if (unlikely(info->reset)) {
  645. offset = 0;
  646. continue;
  647. }
  648. memcpy(&readbuf[offset], buf, ret);
  649. offset += ret;
  650. }
  651. return NULL;
  652. }
  653. static void avalon_rotate_array(struct cgpu_info *avalon)
  654. {
  655. avalon->queued = 0;
  656. if (++avalon->work_array >= AVALON_ARRAY_SIZE)
  657. avalon->work_array = 0;
  658. }
  659. static void *avalon_send_tasks(void *userdata)
  660. {
  661. struct cgpu_info *avalon = (struct cgpu_info *)userdata;
  662. struct avalon_info *info = avalon->device_data;
  663. const int avalon_get_work_count = info->miner_count;
  664. int fd = avalon->device_fd;
  665. char threadname[24];
  666. pthread_detach(pthread_self());
  667. snprintf(threadname, 24, "ava_send/%d", avalon->device_id);
  668. RenameThread(threadname);
  669. while (42) {
  670. int start_count, end_count, i, j, ret;
  671. struct avalon_task at;
  672. int idled = 0;
  673. wait_avalon_ready(fd);
  674. mutex_lock(&info->qlock);
  675. start_count = avalon->work_array * avalon_get_work_count;
  676. end_count = start_count + avalon_get_work_count;
  677. for (i = start_count, j = 0; i < end_count; i++, j++) {
  678. if (unlikely(avalon_buffer_full(fd) == AVA_BUFFER_FULL)) {
  679. applog(LOG_WARNING,
  680. "AVA%i: Buffer full before all work queued",
  681. avalon->device_id);
  682. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  683. __avalon_running_reset(avalon, info, fd);
  684. break;
  685. }
  686. if (likely(j < avalon->queued)) {
  687. info->idle = false;
  688. avalon_init_task(&at, 0, 0, info->fan_pwm,
  689. info->timeout, info->asic_count,
  690. info->miner_count, 1, 0, info->frequency);
  691. avalon_create_task(&at, avalon->works[i]);
  692. } else {
  693. idled++;
  694. avalon_init_task(&at, 0, 0, info->fan_pwm,
  695. info->timeout, info->asic_count,
  696. info->miner_count, 1, 1, info->frequency);
  697. }
  698. ret = avalon_send_task(fd, &at, avalon);
  699. if (unlikely(ret == AVA_SEND_ERROR)) {
  700. applog(LOG_ERR, "AVA%i: Comms error(buffer)",
  701. avalon->device_id);
  702. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  703. __avalon_running_reset(avalon, info, fd);
  704. break;
  705. }
  706. }
  707. avalon_rotate_array(avalon);
  708. pthread_cond_signal(&info->qcond);
  709. mutex_unlock(&info->qlock);
  710. if (unlikely(idled && !info->idle)) {
  711. info->idle = true;
  712. applog(LOG_WARNING, "AVA%i: Idled %d miners",
  713. avalon->device_id, idled);
  714. }
  715. }
  716. return NULL;
  717. }
  718. static bool avalon_prepare(struct thr_info *thr)
  719. {
  720. struct cgpu_info *avalon = thr->cgpu;
  721. struct avalon_info *info = avalon->device_data;
  722. struct timeval now;
  723. free(avalon->works);
  724. avalon->works = calloc(info->miner_count * sizeof(struct work *),
  725. AVALON_ARRAY_SIZE);
  726. if (!avalon->works)
  727. quit(1, "Failed to calloc avalon works in avalon_prepare");
  728. info->thr = thr;
  729. mutex_init(&info->lock);
  730. mutex_init(&info->qlock);
  731. if (unlikely(pthread_cond_init(&info->qcond, NULL)))
  732. quit(1, "Failed to pthread_cond_init avalon qcond");
  733. if (pthread_create(&info->write_thr, NULL, avalon_send_tasks, (void *)avalon))
  734. quit(1, "Failed to create avalon write_thr");
  735. mutex_lock(&info->qlock);
  736. pthread_cond_wait(&info->qcond, &info->qlock);
  737. mutex_unlock(&info->qlock);
  738. if (pthread_create(&info->read_thr, NULL, avalon_get_results, (void *)avalon))
  739. quit(1, "Failed to create avalon read_thr");
  740. avalon_init(avalon);
  741. cgtime(&now);
  742. get_datestamp(avalon->init, &now);
  743. return true;
  744. }
  745. static void avalon_free_work(struct thr_info *thr)
  746. {
  747. struct cgpu_info *avalon;
  748. struct avalon_info *info;
  749. struct work **works;
  750. int i;
  751. avalon = thr->cgpu;
  752. avalon->queued = 0;
  753. if (unlikely(!avalon->works))
  754. return;
  755. works = avalon->works;
  756. info = avalon->device_data;
  757. for (i = 0; i < info->miner_count * 4; i++) {
  758. if (works[i]) {
  759. work_completed(avalon, works[i]);
  760. works[i] = NULL;
  761. }
  762. }
  763. }
  764. static void do_avalon_close(struct thr_info *thr)
  765. {
  766. struct cgpu_info *avalon = thr->cgpu;
  767. struct avalon_info *info = avalon->device_data;
  768. int fd = avalon->device_fd;
  769. pthread_cancel(info->read_thr);
  770. pthread_cancel(info->write_thr);
  771. __avalon_running_reset(avalon, info, fd);
  772. avalon_idle(avalon, info, fd);
  773. avalon_free_work(thr);
  774. avalon_close(fd);
  775. avalon->device_fd = -1;
  776. info->no_matching_work = 0;
  777. }
  778. static inline void record_temp_fan(struct avalon_info *info, struct avalon_result *ar, float *temp_avg)
  779. {
  780. info->fan0 = ar->fan0 * AVALON_FAN_FACTOR;
  781. info->fan1 = ar->fan1 * AVALON_FAN_FACTOR;
  782. info->fan2 = ar->fan2 * AVALON_FAN_FACTOR;
  783. info->temp0 = ar->temp0;
  784. info->temp1 = ar->temp1;
  785. info->temp2 = ar->temp2;
  786. if (ar->temp0 & 0x80) {
  787. ar->temp0 &= 0x7f;
  788. info->temp0 = 0 - ((~ar->temp0 & 0x7f) + 1);
  789. }
  790. if (ar->temp1 & 0x80) {
  791. ar->temp1 &= 0x7f;
  792. info->temp1 = 0 - ((~ar->temp1 & 0x7f) + 1);
  793. }
  794. if (ar->temp2 & 0x80) {
  795. ar->temp2 &= 0x7f;
  796. info->temp2 = 0 - ((~ar->temp2 & 0x7f) + 1);
  797. }
  798. *temp_avg = info->temp2 > info->temp1 ? info->temp2 : info->temp1;
  799. if (info->temp0 > info->temp_max)
  800. info->temp_max = info->temp0;
  801. if (info->temp1 > info->temp_max)
  802. info->temp_max = info->temp1;
  803. if (info->temp2 > info->temp_max)
  804. info->temp_max = info->temp2;
  805. }
  806. static inline void adjust_fan(struct avalon_info *info)
  807. {
  808. int temp_new;
  809. temp_new = info->temp_sum / info->temp_history_count;
  810. if (temp_new < 35) {
  811. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  812. info->temp_old = temp_new;
  813. } else if (temp_new > 55) {
  814. info->fan_pwm = AVALON_DEFAULT_FAN_MAX_PWM;
  815. info->temp_old = temp_new;
  816. } else if (abs(temp_new - info->temp_old) >= 2) {
  817. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM + (temp_new - 35) * 6.4;
  818. info->temp_old = temp_new;
  819. }
  820. }
  821. static void avalon_update_temps(struct cgpu_info *avalon, struct avalon_info *info,
  822. struct avalon_result *ar)
  823. {
  824. record_temp_fan(info, ar, &(avalon->temp));
  825. applog(LOG_INFO,
  826. "Avalon: Fan1: %d/m, Fan2: %d/m, Fan3: %d/m\t"
  827. "Temp1: %dC, Temp2: %dC, Temp3: %dC, TempMAX: %dC",
  828. info->fan0, info->fan1, info->fan2,
  829. info->temp0, info->temp1, info->temp2, info->temp_max);
  830. info->temp_history_index++;
  831. info->temp_sum += avalon->temp;
  832. applog(LOG_DEBUG, "Avalon: temp_index: %d, temp_count: %d, temp_old: %d",
  833. info->temp_history_index, info->temp_history_count, info->temp_old);
  834. if (info->temp_history_index == info->temp_history_count) {
  835. adjust_fan(info);
  836. info->temp_history_index = 0;
  837. info->temp_sum = 0;
  838. }
  839. }
  840. /* We use a replacement algorithm to only remove references to work done from
  841. * the buffer when we need the extra space for new work. */
  842. static bool avalon_fill(struct cgpu_info *avalon)
  843. {
  844. int subid, slot, mc = avalon_infos[avalon->device_id]->miner_count;
  845. struct avalon_info *info = avalon->device_data;
  846. struct work *work;
  847. bool ret = true;
  848. mutex_lock(&info->qlock);
  849. if (avalon->queued >= mc)
  850. goto out_unlock;
  851. work = get_queued(avalon);
  852. if (unlikely(!work)) {
  853. ret = false;
  854. goto out_unlock;
  855. }
  856. subid = avalon->queued++;
  857. work->subid = subid;
  858. slot = avalon->work_array * mc + subid;
  859. if (likely(avalon->works[slot]))
  860. work_completed(avalon, avalon->works[slot]);
  861. avalon->works[slot] = work;
  862. if (avalon->queued < mc)
  863. ret = false;
  864. out_unlock:
  865. mutex_unlock(&info->qlock);
  866. return ret;
  867. }
  868. static int64_t avalon_scanhash(struct thr_info *thr)
  869. {
  870. struct cgpu_info *avalon = thr->cgpu;
  871. struct avalon_info *info = avalon->device_data;
  872. struct timeval now, then, tdiff;
  873. int64_t hash_count, us_timeout;
  874. struct timespec abstime;
  875. /* Full nonce range */
  876. us_timeout = 0x100000000ll / info->asic_count / info->frequency;
  877. tdiff.tv_sec = us_timeout / 1000000;
  878. tdiff.tv_usec = us_timeout - (tdiff.tv_sec * 1000000);
  879. cgtime(&now);
  880. timeradd(&now, &tdiff, &then);
  881. abstime.tv_sec = then.tv_sec;
  882. abstime.tv_nsec = then.tv_usec * 1000;
  883. /* Wait until avalon_send_tasks signals us that it has completed
  884. * sending its work or a full nonce range timeout has occurred */
  885. mutex_lock(&info->qlock);
  886. pthread_cond_timedwait(&info->qcond, &info->qlock, &abstime);
  887. mutex_unlock(&info->qlock);
  888. mutex_lock(&info->lock);
  889. hash_count = 0xffffffffull * (uint64_t)info->nonces;
  890. info->nonces = 0;
  891. mutex_unlock(&info->lock);
  892. /* This hashmeter is just a utility counter based on returned shares */
  893. return hash_count;
  894. }
  895. static void avalon_flush_work(struct cgpu_info *avalon)
  896. {
  897. struct avalon_info *info = avalon->device_data;
  898. struct thr_info *thr = info->thr;
  899. thr->work_restart = false;
  900. mutex_lock(&info->qlock);
  901. /* Will overwrite any work queued */
  902. avalon->queued = 0;
  903. pthread_cond_signal(&info->qcond);
  904. mutex_unlock(&info->qlock);
  905. }
  906. static struct api_data *avalon_api_stats(struct cgpu_info *cgpu)
  907. {
  908. struct api_data *root = NULL;
  909. struct avalon_info *info = cgpu->device_data;
  910. int i;
  911. root = api_add_int(root, "baud", &(info->baud), false);
  912. root = api_add_int(root, "miner_count", &(info->miner_count),false);
  913. root = api_add_int(root, "asic_count", &(info->asic_count), false);
  914. root = api_add_int(root, "timeout", &(info->timeout), false);
  915. root = api_add_int(root, "frequency", &(info->frequency), false);
  916. root = api_add_int(root, "fan1", &(info->fan0), false);
  917. root = api_add_int(root, "fan2", &(info->fan1), false);
  918. root = api_add_int(root, "fan3", &(info->fan2), false);
  919. root = api_add_int(root, "temp1", &(info->temp0), false);
  920. root = api_add_int(root, "temp2", &(info->temp1), false);
  921. root = api_add_int(root, "temp3", &(info->temp2), false);
  922. root = api_add_int(root, "temp_max", &(info->temp_max), false);
  923. root = api_add_int(root, "no_matching_work", &(info->no_matching_work), false);
  924. for (i = 0; i < info->miner_count; i++) {
  925. char mcw[24];
  926. sprintf(mcw, "match_work_count%d", i + 1);
  927. root = api_add_int(root, mcw, &(info->matching_work[i]), false);
  928. }
  929. return root;
  930. }
  931. static void avalon_shutdown(struct thr_info *thr)
  932. {
  933. do_avalon_close(thr);
  934. }
  935. struct device_drv avalon_drv = {
  936. .drv_id = DRIVER_AVALON,
  937. .dname = "avalon",
  938. .name = "AVA",
  939. .drv_detect = avalon_detect,
  940. .thread_prepare = avalon_prepare,
  941. .hash_work = hash_queued_work,
  942. .queue_full = avalon_fill,
  943. .scanwork = avalon_scanhash,
  944. .flush_work = avalon_flush_work,
  945. .get_api_stats = avalon_api_stats,
  946. .reinit_device = avalon_init,
  947. .thread_shutdown = avalon_shutdown,
  948. };