driver-avalon.c 29 KB

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