driver-avalon.c 29 KB

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