driver-avalon.c 29 KB

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