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