driver-avalon.c 28 KB

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