driver-avalon.c 28 KB

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