driver-avalon.c 29 KB

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