driver-avalon.c 28 KB

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