driver-avalon.c 29 KB

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