driver-avalon.c 29 KB

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