driver-avalon.c 28 KB

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