driver-avalon.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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. static int avalon_reset(struct cgpu_info *avalon, int fd)
  231. {
  232. struct avalon_result ar;
  233. struct avalon_task at;
  234. uint8_t *buf;
  235. int ret, i = 0;
  236. struct timespec p;
  237. /* Send reset, then check for result */
  238. avalon_init_task(&at, 1, 0,
  239. AVALON_DEFAULT_FAN_MAX_PWM,
  240. AVALON_DEFAULT_TIMEOUT,
  241. AVALON_DEFAULT_ASIC_NUM,
  242. AVALON_DEFAULT_MINER_NUM,
  243. 0, 0,
  244. AVALON_DEFAULT_FREQUENCY);
  245. ret = avalon_send_task(fd, &at, NULL);
  246. if (unlikely(ret == AVA_SEND_ERROR))
  247. return -1;
  248. ret = avalon_read(fd, (char *)&ar, AVALON_READ_SIZE);
  249. if (unlikely(ret == AVA_GETS_ERROR))
  250. return -1;
  251. /* What do these sleeps do?? */
  252. p.tv_sec = 0;
  253. p.tv_nsec = AVALON_RESET_PITCH;
  254. nanosleep(&p, NULL);
  255. buf = (uint8_t *)&ar;
  256. /* We may also get 0x00 and 0x18 first */
  257. if (buf[0] != 0xAA)
  258. buf = &buf[1];
  259. if (buf[0] != 0xAA)
  260. buf = &buf[1];
  261. if (buf[0] == 0xAA && buf[1] == 0x55 &&
  262. buf[2] == 0xAA && buf[3] == 0x55) {
  263. for (i = 4; i < 11; i++)
  264. if (buf[i] != 0)
  265. break;
  266. }
  267. if (i != 11) {
  268. applog(LOG_ERR, "AVA%d: Reset failed! not an Avalon?"
  269. " (%d: %02x %02x %02x %02x)", avalon->device_id,
  270. i, buf[0], buf[1], buf[2], buf[3]);
  271. /* FIXME: return 1; */
  272. } else
  273. applog(LOG_WARNING, "AVA%d: Reset succeeded",
  274. avalon->device_id);
  275. return 0;
  276. }
  277. static void get_options(int this_option_offset, int *baud, int *miner_count,
  278. int *asic_count, int *timeout, int *frequency)
  279. {
  280. char err_buf[BUFSIZ+1];
  281. char buf[BUFSIZ+1];
  282. char *ptr, *comma, *colon, *colon2, *colon3, *colon4;
  283. size_t max;
  284. int i, tmp;
  285. if (opt_avalon_options == NULL)
  286. buf[0] = '\0';
  287. else {
  288. ptr = opt_avalon_options;
  289. for (i = 0; i < this_option_offset; i++) {
  290. comma = strchr(ptr, ',');
  291. if (comma == NULL)
  292. break;
  293. ptr = comma + 1;
  294. }
  295. comma = strchr(ptr, ',');
  296. if (comma == NULL)
  297. max = strlen(ptr);
  298. else
  299. max = comma - ptr;
  300. if (max > BUFSIZ)
  301. max = BUFSIZ;
  302. strncpy(buf, ptr, max);
  303. buf[max] = '\0';
  304. }
  305. *baud = AVALON_IO_SPEED;
  306. *miner_count = AVALON_DEFAULT_MINER_NUM - 8;
  307. *asic_count = AVALON_DEFAULT_ASIC_NUM;
  308. *timeout = AVALON_DEFAULT_TIMEOUT;
  309. *frequency = AVALON_DEFAULT_FREQUENCY;
  310. if (!(*buf))
  311. return;
  312. colon = strchr(buf, ':');
  313. if (colon)
  314. *(colon++) = '\0';
  315. tmp = atoi(buf);
  316. switch (tmp) {
  317. case 115200:
  318. *baud = 115200;
  319. break;
  320. case 57600:
  321. *baud = 57600;
  322. break;
  323. case 38400:
  324. *baud = 38400;
  325. break;
  326. case 19200:
  327. *baud = 19200;
  328. break;
  329. default:
  330. sprintf(err_buf,
  331. "Invalid avalon-options for baud (%s) "
  332. "must be 115200, 57600, 38400 or 19200", buf);
  333. quit(1, err_buf);
  334. }
  335. if (colon && *colon) {
  336. colon2 = strchr(colon, ':');
  337. if (colon2)
  338. *(colon2++) = '\0';
  339. if (*colon) {
  340. tmp = atoi(colon);
  341. if (tmp > 0 && tmp <= AVALON_DEFAULT_MINER_NUM) {
  342. *miner_count = tmp;
  343. } else {
  344. sprintf(err_buf,
  345. "Invalid avalon-options for "
  346. "miner_count (%s) must be 1 ~ %d",
  347. colon, AVALON_DEFAULT_MINER_NUM);
  348. quit(1, err_buf);
  349. }
  350. }
  351. if (colon2 && *colon2) {
  352. colon3 = strchr(colon2, ':');
  353. if (colon3)
  354. *(colon3++) = '\0';
  355. tmp = atoi(colon2);
  356. if (tmp > 0 && tmp <= AVALON_DEFAULT_ASIC_NUM)
  357. *asic_count = tmp;
  358. else {
  359. sprintf(err_buf,
  360. "Invalid avalon-options for "
  361. "asic_count (%s) must be 1 ~ %d",
  362. colon2, AVALON_DEFAULT_ASIC_NUM);
  363. quit(1, err_buf);
  364. }
  365. if (colon3 && *colon3) {
  366. colon4 = strchr(colon3, ':');
  367. if (colon4)
  368. *(colon4++) = '\0';
  369. tmp = atoi(colon3);
  370. if (tmp > 0 && tmp <= 0xff)
  371. *timeout = tmp;
  372. else {
  373. sprintf(err_buf,
  374. "Invalid avalon-options for "
  375. "timeout (%s) must be 1 ~ %d",
  376. colon3, 0xff);
  377. quit(1, err_buf);
  378. }
  379. if (colon4 && *colon4) {
  380. tmp = atoi(colon4);
  381. switch (tmp) {
  382. case 256:
  383. case 270:
  384. case 282:
  385. case 300:
  386. *frequency = tmp;
  387. break;
  388. default:
  389. sprintf(err_buf,
  390. "Invalid avalon-options for "
  391. "frequency must be 256/270/282/300");
  392. quit(1, err_buf);
  393. }
  394. }
  395. }
  396. }
  397. }
  398. }
  399. static bool avalon_detect_one(const char *devpath)
  400. {
  401. struct avalon_info *info;
  402. int fd, ret;
  403. int baud, miner_count, asic_count, timeout, frequency = 0;
  404. struct cgpu_info *avalon;
  405. int this_option_offset = ++option_offset;
  406. get_options(this_option_offset, &baud, &miner_count, &asic_count,
  407. &timeout, &frequency);
  408. applog(LOG_DEBUG, "Avalon Detect: Attempting to open %s "
  409. "(baud=%d miner_count=%d asic_count=%d timeout=%d frequency=%d)",
  410. devpath, baud, miner_count, asic_count, timeout, frequency);
  411. fd = avalon_open2(devpath, baud, true);
  412. if (unlikely(fd == -1)) {
  413. applog(LOG_ERR, "Avalon Detect: Failed to open %s", devpath);
  414. return false;
  415. }
  416. /* We have a real Avalon! */
  417. avalon = calloc(1, sizeof(struct cgpu_info));
  418. avalon->drv = &avalon_drv;
  419. avalon->device_path = strdup(devpath);
  420. avalon->device_fd = fd;
  421. avalon->threads = AVALON_MINER_THREADS;
  422. add_cgpu(avalon);
  423. avalon_infos = realloc(avalon_infos,
  424. sizeof(struct avalon_info *) *
  425. (total_devices + 1));
  426. applog(LOG_INFO, "Avalon Detect: Found at %s, mark as %d",
  427. devpath, avalon->device_id);
  428. avalon_infos[avalon->device_id] = calloc(sizeof(struct avalon_info), 1);
  429. if (unlikely(!(avalon_infos[avalon->device_id])))
  430. quit(1, "Failed to calloc avalon_infos");
  431. avalon->device_data = avalon_infos[avalon->device_id];
  432. info = avalon->device_data;
  433. info->baud = baud;
  434. info->miner_count = miner_count;
  435. info->asic_count = asic_count;
  436. info->timeout = timeout;
  437. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  438. info->temp_max = 0;
  439. /* This is for check the temp/fan every 3~4s */
  440. info->temp_history_count = (4 / (float)((float)info->timeout * ((float)1.67/0x32))) + 1;
  441. if (info->temp_history_count <= 0)
  442. info->temp_history_count = 1;
  443. info->temp_history_index = 0;
  444. info->temp_sum = 0;
  445. info->temp_old = 0;
  446. info->frequency = frequency;
  447. ret = avalon_reset(avalon, fd);
  448. if (ret) {
  449. ; /* FIXME: I think IT IS avalon and wait on reset;
  450. * avalon_close(fd);
  451. * return false; */
  452. }
  453. return true;
  454. }
  455. static inline void avalon_detect()
  456. {
  457. serial_detect(&avalon_drv, avalon_detect_one);
  458. }
  459. static void avalon_init(struct cgpu_info *avalon)
  460. {
  461. applog(LOG_INFO, "Avalon: Opened on %s", avalon->device_path);
  462. }
  463. static struct work *avalon_valid_result(struct cgpu_info *avalon, struct avalon_result *ar)
  464. {
  465. return find_queued_work_bymidstate(avalon, (char *)ar->midstate, 32,
  466. (char *)ar->data, 64, 12);
  467. }
  468. static void avalon_update_temps(struct cgpu_info *avalon, struct avalon_info *info,
  469. struct avalon_result *ar);
  470. static void avalon_inc_nvw(struct cgpu_info *avalon, struct avalon_info *info,
  471. struct thr_info *thr)
  472. {
  473. if (unlikely(info->idle))
  474. return;
  475. applog(LOG_WARNING, "%s%d: No valid work - HW error",
  476. thr->cgpu->drv->name, thr->cgpu->device_id);
  477. inc_hw_errors(thr);
  478. mutex_lock(&info->lock);
  479. info->no_matching_work++;
  480. avalon->results--;
  481. mutex_unlock(&info->lock);
  482. }
  483. static void avalon_parse_results(struct cgpu_info *avalon, struct avalon_info *info,
  484. struct thr_info *thr, char *buf, int *offset)
  485. {
  486. int i, spare = *offset - AVALON_READ_SIZE;
  487. bool found = false;
  488. for (i = 0; i <= spare; i++) {
  489. struct avalon_result *ar;
  490. struct work *work;
  491. ar = (struct avalon_result *)&buf[i];
  492. work = avalon_valid_result(avalon, ar);
  493. if (work) {
  494. bool gettemp = false;
  495. found = true;
  496. if (avalon_decode_nonce(thr, avalon, info, ar, work)) {
  497. mutex_lock(&info->lock);
  498. if (avalon->results < 0)
  499. avalon->results = 0;
  500. if (!(++avalon->results % info->miner_count)) {
  501. gettemp = true;
  502. avalon->results = 0;
  503. }
  504. info->nonces++;
  505. mutex_unlock(&info->lock);
  506. } else {
  507. mutex_lock(&info->lock);
  508. avalon->results--;
  509. mutex_unlock(&info->lock);
  510. }
  511. if (gettemp)
  512. avalon_update_temps(avalon, info, ar);
  513. break;
  514. }
  515. }
  516. if (!found) {
  517. spare = *offset - AVALON_READ_SIZE;
  518. /* We are buffering and haven't accumulated one more corrupt
  519. * work result. */
  520. if (spare < (int)AVALON_READ_SIZE)
  521. return;
  522. avalon_inc_nvw(avalon, info, thr);
  523. } else {
  524. spare = AVALON_READ_SIZE + i;
  525. if (i) {
  526. if (i >= (int)AVALON_READ_SIZE)
  527. avalon_inc_nvw(avalon, info, thr);
  528. else
  529. applog(LOG_WARNING, "Avalon: Discarding %d bytes from buffer", i);
  530. }
  531. }
  532. *offset -= spare;
  533. memmove(buf, buf + spare, *offset);
  534. }
  535. static void *avalon_get_results(void *userdata)
  536. {
  537. struct cgpu_info *avalon = (struct cgpu_info *)userdata;
  538. struct avalon_info *info = avalon->device_data;
  539. const int rsize = AVALON_FTDI_READSIZE;
  540. char readbuf[AVALON_READBUF_SIZE];
  541. struct thr_info *thr = info->thr;
  542. int fd = avalon->device_fd;
  543. char threadname[24];
  544. int offset = 0;
  545. pthread_detach(pthread_self());
  546. snprintf(threadname, 24, "ava_recv/%d", avalon->device_id);
  547. RenameThread(threadname);
  548. while (42) {
  549. struct timeval timeout;
  550. char buf[rsize];
  551. ssize_t ret;
  552. fd_set rd;
  553. if (offset >= (int)AVALON_READ_SIZE)
  554. avalon_parse_results(avalon, info, thr, readbuf, &offset);
  555. /* Check for nothing but consecutive bad results and reset the
  556. * FPGA if necessary */
  557. if (unlikely(avalon->results < -info->miner_count)) {
  558. applog(LOG_ERR, "AVA%d: %d invalid consecutive results, resetting",
  559. avalon->device_id, -avalon->results);
  560. avalon_reset(avalon, fd);
  561. avalon->results = 0;
  562. }
  563. if (unlikely(offset + rsize >= AVALON_READBUF_SIZE)) {
  564. /* This should never happen */
  565. applog(LOG_ERR, "Avalon readbuf overflow, resetting buffer");
  566. offset = 0;
  567. }
  568. timeout.tv_sec = 0;
  569. timeout.tv_usec = AVALON_READ_TIMEOUT * 1000;
  570. FD_ZERO(&rd);
  571. FD_SET((SOCKETTYPE)fd, &rd);
  572. ret = select(fd + 1, &rd, NULL, NULL, &timeout);
  573. if (ret < 1) {
  574. if (unlikely(ret < 0))
  575. applog(LOG_WARNING, "Select error in avalon_get_results");
  576. continue;
  577. }
  578. ret = read(fd, buf, AVALON_FTDI_READSIZE);
  579. if (unlikely(ret < 1)) {
  580. if (unlikely(ret < 0))
  581. applog(LOG_WARNING, "Read error in avalon_get_results");
  582. continue;
  583. }
  584. if (opt_debug) {
  585. applog(LOG_DEBUG, "Avalon: get:");
  586. hexdump((uint8_t *)buf, ret);
  587. }
  588. memcpy(&readbuf[offset], buf, ret);
  589. offset += ret;
  590. }
  591. return NULL;
  592. }
  593. static void avalon_rotate_array(struct cgpu_info *avalon)
  594. {
  595. avalon->queued = 0;
  596. if (++avalon->work_array >= AVALON_ARRAY_SIZE)
  597. avalon->work_array = 0;
  598. }
  599. static void wait_avalon_ready(int fd)
  600. {
  601. while (avalon_buffer_full(fd) == AVA_BUFFER_FULL) {
  602. nmsleep(40);
  603. }
  604. }
  605. static void *avalon_send_tasks(void *userdata)
  606. {
  607. struct cgpu_info *avalon = (struct cgpu_info *)userdata;
  608. struct avalon_info *info = avalon->device_data;
  609. const int avalon_get_work_count = info->miner_count;
  610. int fd = avalon->device_fd;
  611. char threadname[24];
  612. pthread_detach(pthread_self());
  613. snprintf(threadname, 24, "ava_send/%d", avalon->device_id);
  614. RenameThread(threadname);
  615. while (42) {
  616. int start_count, end_count, i, j, ret;
  617. struct avalon_task at;
  618. int idled = 0;
  619. wait_avalon_ready(fd);
  620. mutex_lock(&info->qlock);
  621. start_count = avalon->work_array * avalon_get_work_count;
  622. end_count = start_count + avalon_get_work_count;
  623. for (i = start_count, j = 0; i < end_count; i++, j++) {
  624. if (unlikely(avalon_buffer_full(fd) == AVA_BUFFER_FULL)) {
  625. applog(LOG_WARNING,
  626. "AVA%i: Buffer full before all work queued",
  627. avalon->device_id);
  628. break;
  629. }
  630. if (likely(j < avalon->queued)) {
  631. info->idle = false;
  632. avalon_init_task(&at, 0, 0, info->fan_pwm,
  633. info->timeout, info->asic_count,
  634. info->miner_count, 1, 0, info->frequency);
  635. avalon_create_task(&at, avalon->works[i]);
  636. } else {
  637. idled++;
  638. avalon_init_task(&at, 0, 0, info->fan_pwm,
  639. info->timeout, info->asic_count,
  640. info->miner_count, 1, 1, info->frequency);
  641. }
  642. ret = avalon_send_task(fd, &at, avalon);
  643. if (unlikely(ret == AVA_SEND_ERROR)) {
  644. applog(LOG_ERR, "AVA%i: Comms error(buffer)",
  645. avalon->device_id);
  646. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  647. avalon_reset(avalon, fd);
  648. }
  649. }
  650. avalon_rotate_array(avalon);
  651. pthread_cond_signal(&info->qcond);
  652. mutex_unlock(&info->qlock);
  653. if (unlikely(idled && !info->idle)) {
  654. info->idle = true;
  655. applog(LOG_WARNING, "AVA%i: Idled %d miners",
  656. avalon->device_id, idled);
  657. }
  658. }
  659. return NULL;
  660. }
  661. static bool avalon_prepare(struct thr_info *thr)
  662. {
  663. struct cgpu_info *avalon = thr->cgpu;
  664. struct avalon_info *info = avalon->device_data;
  665. struct timeval now;
  666. free(avalon->works);
  667. avalon->works = calloc(info->miner_count * sizeof(struct work *),
  668. AVALON_ARRAY_SIZE);
  669. if (!avalon->works)
  670. quit(1, "Failed to calloc avalon works in avalon_prepare");
  671. info->idle = true;
  672. info->thr = thr;
  673. mutex_init(&info->lock);
  674. mutex_init(&info->qlock);
  675. if (unlikely(pthread_cond_init(&info->qcond, NULL)))
  676. quit(1, "Failed to pthread_cond_init avalon qcond");
  677. if (pthread_create(&info->write_thr, NULL, avalon_send_tasks, (void *)avalon))
  678. quit(1, "Failed to create avalon write_thr");
  679. mutex_lock(&info->qlock);
  680. pthread_cond_wait(&info->qcond, &info->qlock);
  681. mutex_unlock(&info->qlock);
  682. if (pthread_create(&info->read_thr, NULL, avalon_get_results, (void *)avalon))
  683. quit(1, "Failed to create avalon read_thr");
  684. avalon_init(avalon);
  685. cgtime(&now);
  686. get_datestamp(avalon->init, &now);
  687. return true;
  688. }
  689. static void avalon_free_work(struct thr_info *thr)
  690. {
  691. struct cgpu_info *avalon;
  692. struct avalon_info *info;
  693. struct work **works;
  694. int i;
  695. avalon = thr->cgpu;
  696. avalon->queued = 0;
  697. if (unlikely(!avalon->works))
  698. return;
  699. works = avalon->works;
  700. info = avalon->device_data;
  701. for (i = 0; i < info->miner_count * 4; i++) {
  702. if (works[i]) {
  703. work_completed(avalon, works[i]);
  704. works[i] = NULL;
  705. }
  706. }
  707. }
  708. static void do_avalon_close(struct thr_info *thr)
  709. {
  710. struct cgpu_info *avalon = thr->cgpu;
  711. struct avalon_info *info = avalon->device_data;
  712. int i, fd = avalon->device_fd;
  713. pthread_cancel(info->read_thr);
  714. pthread_cancel(info->write_thr);
  715. avalon_reset(avalon, fd);
  716. wait_avalon_ready(fd);
  717. applog(LOG_WARNING, "AVA%i: Idling %d miners", avalon->device_id,
  718. info->miner_count);
  719. /* Send idle to all miners */
  720. for (i = 0; i < info->miner_count; i++) {
  721. struct avalon_task at;
  722. avalon_init_task(&at, 0, 0, info->fan_pwm, info->timeout,
  723. info->asic_count, info->miner_count, 1, 1,
  724. info->frequency);
  725. avalon_send_task(fd, &at, avalon);
  726. }
  727. avalon_free_work(thr);
  728. avalon_close(fd);
  729. avalon->device_fd = -1;
  730. info->no_matching_work = 0;
  731. }
  732. static inline void record_temp_fan(struct avalon_info *info, struct avalon_result *ar, float *temp_avg)
  733. {
  734. info->fan0 = ar->fan0 * AVALON_FAN_FACTOR;
  735. info->fan1 = ar->fan1 * AVALON_FAN_FACTOR;
  736. info->fan2 = ar->fan2 * AVALON_FAN_FACTOR;
  737. info->temp0 = ar->temp0;
  738. info->temp1 = ar->temp1;
  739. info->temp2 = ar->temp2;
  740. if (ar->temp0 & 0x80) {
  741. ar->temp0 &= 0x7f;
  742. info->temp0 = 0 - ((~ar->temp0 & 0x7f) + 1);
  743. }
  744. if (ar->temp1 & 0x80) {
  745. ar->temp1 &= 0x7f;
  746. info->temp1 = 0 - ((~ar->temp1 & 0x7f) + 1);
  747. }
  748. if (ar->temp2 & 0x80) {
  749. ar->temp2 &= 0x7f;
  750. info->temp2 = 0 - ((~ar->temp2 & 0x7f) + 1);
  751. }
  752. *temp_avg = info->temp2 > info->temp1 ? info->temp2 : info->temp1;
  753. if (info->temp0 > info->temp_max)
  754. info->temp_max = info->temp0;
  755. if (info->temp1 > info->temp_max)
  756. info->temp_max = info->temp1;
  757. if (info->temp2 > info->temp_max)
  758. info->temp_max = info->temp2;
  759. }
  760. static inline void adjust_fan(struct avalon_info *info)
  761. {
  762. int temp_new;
  763. temp_new = info->temp_sum / info->temp_history_count;
  764. if (temp_new < 35) {
  765. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  766. info->temp_old = temp_new;
  767. } else if (temp_new > 55) {
  768. info->fan_pwm = AVALON_DEFAULT_FAN_MAX_PWM;
  769. info->temp_old = temp_new;
  770. } else if (abs(temp_new - info->temp_old) >= 2) {
  771. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM + (temp_new - 35) * 6.4;
  772. info->temp_old = temp_new;
  773. }
  774. }
  775. static void avalon_update_temps(struct cgpu_info *avalon, struct avalon_info *info,
  776. struct avalon_result *ar)
  777. {
  778. record_temp_fan(info, ar, &(avalon->temp));
  779. applog(LOG_INFO,
  780. "Avalon: Fan1: %d/m, Fan2: %d/m, Fan3: %d/m\t"
  781. "Temp1: %dC, Temp2: %dC, Temp3: %dC, TempMAX: %dC",
  782. info->fan0, info->fan1, info->fan2,
  783. info->temp0, info->temp1, info->temp2, info->temp_max);
  784. info->temp_history_index++;
  785. info->temp_sum += avalon->temp;
  786. applog(LOG_DEBUG, "Avalon: temp_index: %d, temp_count: %d, temp_old: %d",
  787. info->temp_history_index, info->temp_history_count, info->temp_old);
  788. if (info->temp_history_index == info->temp_history_count) {
  789. adjust_fan(info);
  790. info->temp_history_index = 0;
  791. info->temp_sum = 0;
  792. }
  793. }
  794. /* We use a replacement algorithm to only remove references to work done from
  795. * the buffer when we need the extra space for new work. */
  796. static bool avalon_fill(struct cgpu_info *avalon)
  797. {
  798. int subid, slot, mc = avalon_infos[avalon->device_id]->miner_count;
  799. struct avalon_info *info = avalon->device_data;
  800. struct work *work;
  801. bool ret = true;
  802. mutex_lock(&info->qlock);
  803. if (avalon->queued >= mc)
  804. goto out_unlock;
  805. work = get_queued(avalon);
  806. if (unlikely(!work)) {
  807. ret = false;
  808. goto out_unlock;
  809. }
  810. subid = avalon->queued++;
  811. work->subid = subid;
  812. slot = avalon->work_array * mc + subid;
  813. if (likely(avalon->works[slot]))
  814. work_completed(avalon, avalon->works[slot]);
  815. avalon->works[slot] = work;
  816. if (avalon->queued < mc)
  817. ret = false;
  818. out_unlock:
  819. mutex_unlock(&info->qlock);
  820. return ret;
  821. }
  822. static int64_t avalon_scanhash(struct thr_info *thr)
  823. {
  824. struct cgpu_info *avalon = thr->cgpu;
  825. struct avalon_info *info = avalon->device_data;
  826. struct timeval now, then, tdiff;
  827. int64_t hash_count, us_timeout;
  828. struct timespec abstime;
  829. /* Full nonce range */
  830. us_timeout = 0x100000000ll / info->asic_count / info->frequency;
  831. tdiff.tv_sec = us_timeout / 1000000;
  832. tdiff.tv_usec = us_timeout - (tdiff.tv_sec * 1000000);
  833. cgtime(&now);
  834. timeradd(&now, &tdiff, &then);
  835. abstime.tv_sec = then.tv_sec;
  836. abstime.tv_nsec = then.tv_usec * 1000;
  837. /* Wait until avalon_send_tasks signals us that it has completed
  838. * sending its work or a full nonce range timeout has occurred */
  839. mutex_lock(&info->qlock);
  840. pthread_cond_timedwait(&info->qcond, &info->qlock, &abstime);
  841. mutex_unlock(&info->qlock);
  842. mutex_lock(&info->lock);
  843. hash_count = 0xffffffffull * (uint64_t)info->nonces;
  844. info->nonces = 0;
  845. mutex_unlock(&info->lock);
  846. /* This hashmeter is just a utility counter based on returned shares */
  847. return hash_count;
  848. }
  849. static void avalon_flush_work(struct cgpu_info *avalon)
  850. {
  851. struct avalon_info *info = avalon->device_data;
  852. struct thr_info *thr = info->thr;
  853. thr->work_restart = false;
  854. mutex_lock(&info->qlock);
  855. /* Will overwrite any work queued */
  856. avalon->queued = 0;
  857. pthread_cond_signal(&info->qcond);
  858. mutex_unlock(&info->qlock);
  859. }
  860. static struct api_data *avalon_api_stats(struct cgpu_info *cgpu)
  861. {
  862. struct api_data *root = NULL;
  863. struct avalon_info *info = cgpu->device_data;
  864. int i;
  865. root = api_add_int(root, "baud", &(info->baud), false);
  866. root = api_add_int(root, "miner_count", &(info->miner_count),false);
  867. root = api_add_int(root, "asic_count", &(info->asic_count), false);
  868. root = api_add_int(root, "timeout", &(info->timeout), false);
  869. root = api_add_int(root, "frequency", &(info->frequency), false);
  870. root = api_add_int(root, "fan1", &(info->fan0), false);
  871. root = api_add_int(root, "fan2", &(info->fan1), false);
  872. root = api_add_int(root, "fan3", &(info->fan2), false);
  873. root = api_add_int(root, "temp1", &(info->temp0), false);
  874. root = api_add_int(root, "temp2", &(info->temp1), false);
  875. root = api_add_int(root, "temp3", &(info->temp2), false);
  876. root = api_add_int(root, "temp_max", &(info->temp_max), false);
  877. root = api_add_int(root, "no_matching_work", &(info->no_matching_work), false);
  878. for (i = 0; i < info->miner_count; i++) {
  879. char mcw[24];
  880. sprintf(mcw, "match_work_count%d", i + 1);
  881. root = api_add_int(root, mcw, &(info->matching_work[i]), false);
  882. }
  883. return root;
  884. }
  885. static void avalon_shutdown(struct thr_info *thr)
  886. {
  887. do_avalon_close(thr);
  888. }
  889. struct device_drv avalon_drv = {
  890. .drv_id = DRIVER_AVALON,
  891. .dname = "avalon",
  892. .name = "AVA",
  893. .drv_detect = avalon_detect,
  894. .thread_prepare = avalon_prepare,
  895. .hash_work = hash_queued_work,
  896. .queue_full = avalon_fill,
  897. .scanwork = avalon_scanhash,
  898. .flush_work = avalon_flush_work,
  899. .get_api_stats = avalon_api_stats,
  900. .reinit_device = avalon_init,
  901. .thread_shutdown = avalon_shutdown,
  902. };