driver-avalon.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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. int opt_avalon_temp = AVALON_TEMP_TARGET;
  40. int opt_avalon_overheat = AVALON_TEMP_OVERHEAT;
  41. bool opt_avalon_auto;
  42. static int option_offset = -1;
  43. struct device_drv avalon_drv;
  44. static int avalon_init_task(struct avalon_task *at,
  45. uint8_t reset, uint8_t ff, uint8_t fan,
  46. uint8_t timeout, uint8_t asic_num,
  47. uint8_t miner_num, uint8_t nonce_elf,
  48. uint8_t gate_miner, int frequency)
  49. {
  50. uint16_t *lefreq16;
  51. uint8_t *buf;
  52. static bool first = true;
  53. if (unlikely(!at))
  54. return -1;
  55. if (unlikely(timeout <= 0 || asic_num <= 0 || miner_num <= 0))
  56. return -1;
  57. memset(at, 0, sizeof(struct avalon_task));
  58. if (unlikely(reset)) {
  59. at->reset = 1;
  60. at->fan_eft = 1;
  61. at->timer_eft = 1;
  62. first = true;
  63. }
  64. at->flush_fifo = (ff ? 1 : 0);
  65. at->fan_eft = (fan ? 1 : 0);
  66. if (unlikely(first && !at->reset)) {
  67. at->fan_eft = 1;
  68. at->timer_eft = 1;
  69. first = false;
  70. }
  71. at->fan_pwm_data = (fan ? fan : AVALON_DEFAULT_FAN_MAX_PWM);
  72. at->timeout_data = timeout;
  73. at->asic_num = asic_num;
  74. at->miner_num = miner_num;
  75. at->nonce_elf = nonce_elf;
  76. at->gate_miner_elf = 1;
  77. at->asic_pll = 1;
  78. if (unlikely(gate_miner)) {
  79. at-> gate_miner = 1;
  80. at->asic_pll = 0;
  81. }
  82. buf = (uint8_t *)at;
  83. buf[5] = 0x00;
  84. buf[8] = 0x74;
  85. buf[9] = 0x01;
  86. buf[10] = 0x00;
  87. buf[11] = 0x00;
  88. lefreq16 = (uint16_t *)&buf[6];
  89. *lefreq16 = htole16(frequency * 8);
  90. return 0;
  91. }
  92. static inline void avalon_create_task(struct avalon_task *at,
  93. struct work *work)
  94. {
  95. memcpy(at->midstate, work->midstate, 32);
  96. memcpy(at->data, work->data + 64, 12);
  97. }
  98. static int avalon_write(struct cgpu_info *avalon, char *buf, ssize_t len, int ep)
  99. {
  100. int err, amount;
  101. err = usb_write(avalon, buf, len, &amount, ep);
  102. applog(LOG_DEBUG, "%s%i: usb_write got err %d", avalon->drv->name,
  103. avalon->device_id, err);
  104. if (unlikely(err != 0)) {
  105. applog(LOG_WARNING, "usb_write error on avalon_write");
  106. return AVA_SEND_ERROR;
  107. }
  108. if (amount != len) {
  109. applog(LOG_WARNING, "usb_write length mismatch on avalon_write");
  110. return AVA_SEND_ERROR;
  111. }
  112. return AVA_SEND_OK;
  113. }
  114. static int avalon_send_task(const struct avalon_task *at, struct cgpu_info *avalon)
  115. {
  116. uint8_t buf[AVALON_WRITE_SIZE + 4 * AVALON_DEFAULT_ASIC_NUM];
  117. int delay, ret, i, ep = C_AVALON_TASK;
  118. struct avalon_info *info;
  119. uint32_t nonce_range;
  120. size_t nr_len;
  121. if (at->nonce_elf)
  122. nr_len = AVALON_WRITE_SIZE + 4 * at->asic_num;
  123. else
  124. nr_len = AVALON_WRITE_SIZE;
  125. memcpy(buf, at, AVALON_WRITE_SIZE);
  126. if (at->nonce_elf) {
  127. nonce_range = (uint32_t)0xffffffff / at->asic_num;
  128. for (i = 0; i < at->asic_num; i++) {
  129. buf[AVALON_WRITE_SIZE + (i * 4) + 3] =
  130. (i * nonce_range & 0xff000000) >> 24;
  131. buf[AVALON_WRITE_SIZE + (i * 4) + 2] =
  132. (i * nonce_range & 0x00ff0000) >> 16;
  133. buf[AVALON_WRITE_SIZE + (i * 4) + 1] =
  134. (i * nonce_range & 0x0000ff00) >> 8;
  135. buf[AVALON_WRITE_SIZE + (i * 4) + 0] =
  136. (i * nonce_range & 0x000000ff) >> 0;
  137. }
  138. }
  139. #if defined(__BIG_ENDIAN__) || defined(MIPSEB)
  140. uint8_t tt = 0;
  141. tt = (buf[0] & 0x0f) << 4;
  142. tt |= ((buf[0] & 0x10) ? (1 << 3) : 0);
  143. tt |= ((buf[0] & 0x20) ? (1 << 2) : 0);
  144. tt |= ((buf[0] & 0x40) ? (1 << 1) : 0);
  145. tt |= ((buf[0] & 0x80) ? (1 << 0) : 0);
  146. buf[0] = tt;
  147. tt = (buf[4] & 0x0f) << 4;
  148. tt |= ((buf[4] & 0x10) ? (1 << 3) : 0);
  149. tt |= ((buf[4] & 0x20) ? (1 << 2) : 0);
  150. tt |= ((buf[4] & 0x40) ? (1 << 1) : 0);
  151. tt |= ((buf[4] & 0x80) ? (1 << 0) : 0);
  152. buf[4] = tt;
  153. #endif
  154. info = avalon->device_data;
  155. delay = nr_len * 10 * 1000000;
  156. delay = delay / info->baud;
  157. if (at->reset) {
  158. ep = C_AVALON_RESET;
  159. nr_len = 1;
  160. }
  161. if (opt_debug) {
  162. applog(LOG_DEBUG, "Avalon: Sent(%u):", (unsigned int)nr_len);
  163. hexdump(buf, nr_len);
  164. }
  165. ret = avalon_write(avalon, (char *)buf, nr_len, ep);
  166. delay += 4000;
  167. nusleep(delay);
  168. applog(LOG_DEBUG, "Avalon: Sent: Buffer delay: %dus", delay);
  169. return ret;
  170. }
  171. static bool avalon_decode_nonce(struct thr_info *thr, struct cgpu_info *avalon,
  172. struct avalon_info *info, struct avalon_result *ar,
  173. struct work *work)
  174. {
  175. uint32_t nonce;
  176. info = avalon->device_data;
  177. info->matching_work[work->subid]++;
  178. nonce = htole32(ar->nonce);
  179. applog(LOG_DEBUG, "Avalon: nonce = %0x08x", nonce);
  180. return submit_nonce(thr, work, nonce);
  181. }
  182. /* Wait until the ftdi chip returns a CTS saying we can send more data. */
  183. static void wait_avalon_ready(struct cgpu_info *avalon)
  184. {
  185. while (avalon_buffer_full(avalon)) {
  186. nmsleep(40);
  187. }
  188. }
  189. #define AVALON_CTS (1 << 4)
  190. static inline bool avalon_cts(char c)
  191. {
  192. return (c & AVALON_CTS);
  193. }
  194. static int avalon_read(struct cgpu_info *avalon, unsigned char *buf,
  195. size_t bufsize, int timeout, int ep)
  196. {
  197. struct avalon_info *info = avalon->device_data;
  198. size_t total = 0, readsize = bufsize + 2;
  199. char readbuf[AVALON_READBUF_SIZE];
  200. int err, amount, ofs = 2, cp;
  201. err = usb_read_once_timeout(avalon, readbuf, readsize, &amount, timeout, ep);
  202. applog(LOG_DEBUG, "%s%i: Get avalon read got err %d",
  203. avalon->drv->name, avalon->device_id, err);
  204. if (amount < 2)
  205. goto out;
  206. /* Use the fact that we're reading the status with the buffer to tell
  207. * the write thread it should send more work without needing to call
  208. * avalon_buffer_full directly. */
  209. if (avalon_cts(buf[0]))
  210. cgsem_post(&info->write_sem);
  211. /* The first 2 of every 64 bytes are status on FTDIRL */
  212. while (amount > 2) {
  213. cp = amount - 2;
  214. if (cp > 62)
  215. cp = 62;
  216. memcpy(&buf[total], &readbuf[ofs], cp);
  217. total += cp;
  218. amount -= cp + 2;
  219. ofs += 64;
  220. }
  221. out:
  222. return total;
  223. }
  224. static int avalon_reset(struct cgpu_info *avalon, bool initial)
  225. {
  226. struct avalon_result ar;
  227. int ret, i, spare;
  228. struct avalon_task at;
  229. uint8_t *buf, *tmp;
  230. struct timespec p;
  231. /* Send reset, then check for result */
  232. avalon_init_task(&at, 1, 0,
  233. AVALON_DEFAULT_FAN_MAX_PWM,
  234. AVALON_DEFAULT_TIMEOUT,
  235. AVALON_DEFAULT_ASIC_NUM,
  236. AVALON_DEFAULT_MINER_NUM,
  237. 0, 0,
  238. AVALON_DEFAULT_FREQUENCY);
  239. wait_avalon_ready(avalon);
  240. ret = avalon_send_task(&at, avalon);
  241. if (unlikely(ret == AVA_SEND_ERROR))
  242. return -1;
  243. if (!initial) {
  244. applog(LOG_ERR, "AVA%d reset sequence sent", avalon->device_id);
  245. return 0;
  246. }
  247. ret = avalon_read(avalon, (unsigned char *)&ar, AVALON_READ_SIZE,
  248. AVALON_RESET_TIMEOUT, C_GET_AVALON_RESET);
  249. /* What do these sleeps do?? */
  250. p.tv_sec = 0;
  251. p.tv_nsec = AVALON_RESET_PITCH;
  252. nanosleep(&p, NULL);
  253. /* Look for the first occurrence of 0xAA, the reset response should be:
  254. * AA 55 AA 55 00 00 00 00 00 00 */
  255. spare = ret - 10;
  256. buf = tmp = (uint8_t *)&ar;
  257. if (opt_debug) {
  258. applog(LOG_DEBUG, "AVA%d reset: get:", avalon->device_id);
  259. hexdump(tmp, AVALON_READ_SIZE);
  260. }
  261. for (i = 0; i <= spare; i++) {
  262. buf = &tmp[i];
  263. if (buf[0] == 0xAA)
  264. break;
  265. }
  266. i = 0;
  267. if (buf[0] == 0xAA && buf[1] == 0x55 &&
  268. buf[2] == 0xAA && buf[3] == 0x55) {
  269. for (i = 4; i < 11; i++)
  270. if (buf[i] != 0)
  271. break;
  272. }
  273. if (i != 11) {
  274. applog(LOG_ERR, "AVA%d: Reset failed! not an Avalon?"
  275. " (%d: %02x %02x %02x %02x)", avalon->device_id,
  276. i, buf[0], buf[1], buf[2], buf[3]);
  277. /* FIXME: return 1; */
  278. } else
  279. applog(LOG_WARNING, "AVA%d: Reset succeeded",
  280. avalon->device_id);
  281. return 0;
  282. }
  283. static bool get_options(int this_option_offset, int *baud, int *miner_count,
  284. int *asic_count, int *timeout, int *frequency)
  285. {
  286. char buf[BUFSIZ+1];
  287. char *ptr, *comma, *colon, *colon2, *colon3, *colon4;
  288. size_t max;
  289. int i, tmp;
  290. if (opt_avalon_options == NULL)
  291. buf[0] = '\0';
  292. else {
  293. ptr = opt_avalon_options;
  294. for (i = 0; i < this_option_offset; i++) {
  295. comma = strchr(ptr, ',');
  296. if (comma == NULL)
  297. break;
  298. ptr = comma + 1;
  299. }
  300. comma = strchr(ptr, ',');
  301. if (comma == NULL)
  302. max = strlen(ptr);
  303. else
  304. max = comma - ptr;
  305. if (max > BUFSIZ)
  306. max = BUFSIZ;
  307. strncpy(buf, ptr, max);
  308. buf[max] = '\0';
  309. }
  310. if (!(*buf))
  311. return false;
  312. colon = strchr(buf, ':');
  313. if (colon)
  314. *(colon++) = '\0';
  315. tmp = atoi(buf);
  316. switch (tmp) {
  317. case 115200:
  318. *baud = 115200;
  319. break;
  320. case 57600:
  321. *baud = 57600;
  322. break;
  323. case 38400:
  324. *baud = 38400;
  325. break;
  326. case 19200:
  327. *baud = 19200;
  328. break;
  329. default:
  330. quit(1, "Invalid avalon-options for baud (%s) "
  331. "must be 115200, 57600, 38400 or 19200", buf);
  332. }
  333. if (colon && *colon) {
  334. colon2 = strchr(colon, ':');
  335. if (colon2)
  336. *(colon2++) = '\0';
  337. if (*colon) {
  338. tmp = atoi(colon);
  339. if (tmp > 0 && tmp <= AVALON_DEFAULT_MINER_NUM) {
  340. *miner_count = tmp;
  341. } else {
  342. quit(1, "Invalid avalon-options for "
  343. "miner_count (%s) must be 1 ~ %d",
  344. colon, AVALON_DEFAULT_MINER_NUM);
  345. }
  346. }
  347. if (colon2 && *colon2) {
  348. colon3 = strchr(colon2, ':');
  349. if (colon3)
  350. *(colon3++) = '\0';
  351. tmp = atoi(colon2);
  352. if (tmp > 0 && tmp <= AVALON_DEFAULT_ASIC_NUM)
  353. *asic_count = tmp;
  354. else {
  355. quit(1, "Invalid avalon-options for "
  356. "asic_count (%s) must be 1 ~ %d",
  357. colon2, AVALON_DEFAULT_ASIC_NUM);
  358. }
  359. if (colon3 && *colon3) {
  360. colon4 = strchr(colon3, ':');
  361. if (colon4)
  362. *(colon4++) = '\0';
  363. tmp = atoi(colon3);
  364. if (tmp > 0 && tmp <= 0xff)
  365. *timeout = tmp;
  366. else {
  367. quit(1, "Invalid avalon-options for "
  368. "timeout (%s) must be 1 ~ %d",
  369. colon3, 0xff);
  370. }
  371. if (colon4 && *colon4) {
  372. tmp = atoi(colon4);
  373. switch (tmp) {
  374. case 256:
  375. case 270:
  376. case 282:
  377. case 300:
  378. case 325:
  379. case 350:
  380. case 375:
  381. *frequency = tmp;
  382. break;
  383. default:
  384. quit(1, "Invalid avalon-options for "
  385. "frequency must be 256/270/282/300/325/350/375");
  386. }
  387. }
  388. }
  389. }
  390. }
  391. return true;
  392. }
  393. static void avalon_idle(struct cgpu_info *avalon, struct avalon_info *info)
  394. {
  395. int i;
  396. info->idle = true;
  397. wait_avalon_ready(avalon);
  398. /* Send idle to all miners */
  399. for (i = 0; i < info->miner_count; i++) {
  400. struct avalon_task at;
  401. if (unlikely(avalon_buffer_full(avalon)))
  402. break;
  403. avalon_init_task(&at, 0, 0, info->fan_pwm, info->timeout,
  404. info->asic_count, info->miner_count, 1, 1,
  405. info->frequency);
  406. avalon_send_task(&at, avalon);
  407. }
  408. applog(LOG_WARNING, "AVA%i: Idling %d miners", avalon->device_id, i);
  409. wait_avalon_ready(avalon);
  410. }
  411. static void avalon_initialise(struct cgpu_info *avalon)
  412. {
  413. int err, interface;
  414. if (avalon->usbinfo.nodev)
  415. return;
  416. interface = avalon->usbdev->found->interface;
  417. // Reset
  418. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  419. FTDI_VALUE_RESET, interface, C_RESET);
  420. applog(LOG_DEBUG, "%s%i: reset got err %d",
  421. avalon->drv->name, avalon->device_id, err);
  422. if (avalon->usbinfo.nodev)
  423. return;
  424. // Set latency
  425. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_LATENCY,
  426. AVALON_LATENCY, interface, C_LATENCY);
  427. applog(LOG_DEBUG, "%s%i: latency got err %d",
  428. avalon->drv->name, avalon->device_id, err);
  429. if (avalon->usbinfo.nodev)
  430. return;
  431. // Set data
  432. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_DATA,
  433. FTDI_VALUE_DATA_AVA, interface, C_SETDATA);
  434. applog(LOG_DEBUG, "%s%i: data got err %d",
  435. avalon->drv->name, avalon->device_id, err);
  436. if (avalon->usbinfo.nodev)
  437. return;
  438. // Set the baud
  439. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD_AVA,
  440. (FTDI_INDEX_BAUD_AVA & 0xff00) | interface,
  441. C_SETBAUD);
  442. applog(LOG_DEBUG, "%s%i: setbaud got err %d",
  443. avalon->drv->name, avalon->device_id, err);
  444. if (avalon->usbinfo.nodev)
  445. return;
  446. // Set Modem Control
  447. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
  448. FTDI_VALUE_MODEM, interface, C_SETMODEM);
  449. applog(LOG_DEBUG, "%s%i: setmodemctrl got err %d",
  450. avalon->drv->name, avalon->device_id, err);
  451. if (avalon->usbinfo.nodev)
  452. return;
  453. // Set Flow Control
  454. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
  455. FTDI_VALUE_FLOW, interface, C_SETFLOW);
  456. applog(LOG_DEBUG, "%s%i: setflowctrl got err %d",
  457. avalon->drv->name, avalon->device_id, err);
  458. if (avalon->usbinfo.nodev)
  459. return;
  460. /* Avalon repeats the following */
  461. // Set Modem Control
  462. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
  463. FTDI_VALUE_MODEM, interface, C_SETMODEM);
  464. applog(LOG_DEBUG, "%s%i: setmodemctrl 2 got err %d",
  465. avalon->drv->name, avalon->device_id, err);
  466. if (avalon->usbinfo.nodev)
  467. return;
  468. // Set Flow Control
  469. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
  470. FTDI_VALUE_FLOW, interface, C_SETFLOW);
  471. applog(LOG_DEBUG, "%s%i: setflowctrl 2 got err %d",
  472. avalon->drv->name, avalon->device_id, err);
  473. }
  474. static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found)
  475. {
  476. int baud, miner_count, asic_count, timeout, frequency = 0;
  477. int this_option_offset = ++option_offset;
  478. struct avalon_info *info;
  479. struct cgpu_info *avalon;
  480. bool configured;
  481. int ret;
  482. avalon = usb_alloc_cgpu(&avalon_drv, AVALON_MINER_THREADS);
  483. configured = get_options(this_option_offset, &baud, &miner_count,
  484. &asic_count, &timeout, &frequency);
  485. if (!usb_init(avalon, dev, found))
  486. goto shin;
  487. /* Even though this is an FTDI type chip, we want to do the parsing
  488. * all ourselves so set it to std usb type */
  489. avalon->usbdev->usb_type = USB_TYPE_STD;
  490. avalon->usbdev->PrefPacketSize = AVALON_USB_PACKETSIZE;
  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_INFO, "%s%d: No matching 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. info->auto_nonces++;
  581. mutex_unlock(&info->lock);
  582. } else if (opt_avalon_auto) {
  583. mutex_lock(&info->lock);
  584. info->auto_hw++;
  585. mutex_unlock(&info->lock);
  586. }
  587. if (gettemp)
  588. avalon_update_temps(avalon, info, ar);
  589. break;
  590. }
  591. }
  592. if (!found) {
  593. spare = *offset - AVALON_READ_SIZE;
  594. /* We are buffering and haven't accumulated one more corrupt
  595. * work result. */
  596. if (spare < (int)AVALON_READ_SIZE)
  597. return;
  598. avalon_inc_nvw(info, thr);
  599. } else {
  600. spare = AVALON_READ_SIZE + i;
  601. if (i) {
  602. if (i >= (int)AVALON_READ_SIZE)
  603. avalon_inc_nvw(info, thr);
  604. else
  605. applog(LOG_WARNING, "Avalon: Discarding %d bytes from buffer", i);
  606. }
  607. }
  608. *offset -= spare;
  609. memmove(buf, buf + spare, *offset);
  610. }
  611. static void avalon_running_reset(struct cgpu_info *avalon,
  612. struct avalon_info *info)
  613. {
  614. avalon_reset(avalon, false);
  615. avalon_idle(avalon, info);
  616. avalon->results = 0;
  617. info->reset = false;
  618. }
  619. static void *avalon_get_results(void *userdata)
  620. {
  621. struct cgpu_info *avalon = (struct cgpu_info *)userdata;
  622. struct avalon_info *info = avalon->device_data;
  623. int offset = 0, read_delay = 0, ret = 0;
  624. const int rsize = AVALON_FTDI_READSIZE;
  625. char readbuf[AVALON_READBUF_SIZE];
  626. struct thr_info *thr = info->thr;
  627. struct timeval tv_start, tv_end;
  628. char threadname[24];
  629. snprintf(threadname, 24, "ava_recv/%d", avalon->device_id);
  630. RenameThread(threadname);
  631. while (likely(!avalon->shutdown)) {
  632. unsigned char buf[rsize];
  633. struct timeval tv_diff;
  634. int us_diff;
  635. if (offset >= (int)AVALON_READ_SIZE)
  636. avalon_parse_results(avalon, info, thr, readbuf, &offset);
  637. if (unlikely(offset + rsize >= AVALON_READBUF_SIZE)) {
  638. /* This should never happen */
  639. applog(LOG_ERR, "Avalon readbuf overflow, resetting buffer");
  640. offset = 0;
  641. }
  642. if (unlikely(info->reset)) {
  643. avalon_running_reset(avalon, info);
  644. /* Discard anything in the buffer */
  645. offset = 0;
  646. }
  647. /* As the usb read returns after just 1ms, sleep long enough
  648. * to leave the interface idle for writes to occur, but do not
  649. * sleep if we have been receiving data as more may be coming. */
  650. if (ret < 1) {
  651. cgtime(&tv_end);
  652. timersub(&tv_end, &tv_start, &tv_diff);
  653. /* Assume it has not been > 1 second so ignore tv_sec */
  654. us_diff = tv_diff.tv_usec;
  655. read_delay = AVALON_READ_TIMEOUT * 1000 - us_diff;
  656. if (likely(read_delay >= 1000))
  657. nusleep(read_delay);
  658. }
  659. cgtime(&tv_start);
  660. ret = avalon_read(avalon, buf, rsize, AVALON_READ_TIMEOUT,
  661. C_AVALON_READ);
  662. if (ret < 1)
  663. continue;
  664. if (opt_debug) {
  665. applog(LOG_DEBUG, "Avalon: get:");
  666. hexdump((uint8_t *)buf, ret);
  667. }
  668. memcpy(&readbuf[offset], &buf, ret);
  669. offset += ret;
  670. }
  671. return NULL;
  672. }
  673. static void avalon_rotate_array(struct cgpu_info *avalon)
  674. {
  675. avalon->queued = 0;
  676. if (++avalon->work_array >= AVALON_ARRAY_SIZE)
  677. avalon->work_array = 0;
  678. }
  679. static void avalon_set_timeout(struct avalon_info *info)
  680. {
  681. info->timeout = AVALON_TIMEOUT_FACTOR / info->frequency;
  682. }
  683. static void avalon_inc_freq(struct avalon_info *info)
  684. {
  685. info->frequency += 2;
  686. if (info->frequency > AVALON_MAX_FREQUENCY)
  687. info->frequency = AVALON_MAX_FREQUENCY;
  688. avalon_set_timeout(info);
  689. applog(LOG_NOTICE, "Avalon increasing frequency to %d, timeout %d",
  690. info->frequency, info->timeout);
  691. }
  692. static void avalon_dec_freq(struct avalon_info *info)
  693. {
  694. info->frequency -= 1;
  695. if (info->frequency < AVALON_MIN_FREQUENCY)
  696. info->frequency = AVALON_MIN_FREQUENCY;
  697. avalon_set_timeout(info);
  698. applog(LOG_NOTICE, "Avalon decreasing frequency to %d, timeout %d",
  699. info->frequency, info->timeout);
  700. }
  701. static void *avalon_send_tasks(void *userdata)
  702. {
  703. struct cgpu_info *avalon = (struct cgpu_info *)userdata;
  704. struct avalon_info *info = avalon->device_data;
  705. const int avalon_get_work_count = info->miner_count;
  706. char threadname[24];
  707. snprintf(threadname, 24, "ava_send/%d", avalon->device_id);
  708. RenameThread(threadname);
  709. while (likely(!avalon->shutdown)) {
  710. int start_count, end_count, i, j, ret;
  711. struct avalon_task at;
  712. int idled = 0;
  713. while (avalon_buffer_full(avalon))
  714. cgsem_wait(&info->write_sem);
  715. if (opt_avalon_auto && info->auto_queued >= AVALON_AUTO_CYCLE) {
  716. mutex_lock(&info->lock);
  717. if (info->auto_nonces >= (AVALON_AUTO_CYCLE * 19 / 20) &&
  718. info->auto_nonces <= (AVALON_AUTO_CYCLE * 21 / 20)) {
  719. int total = info->auto_nonces + info->auto_hw;
  720. /* Try to keep hw errors ~1-1.5% */
  721. if (info->auto_hw * 100 < total)
  722. avalon_inc_freq(info);
  723. else if (info->auto_hw * 66 > total)
  724. avalon_dec_freq(info);
  725. }
  726. info->auto_queued =
  727. info->auto_nonces =
  728. info->auto_hw = 0;
  729. mutex_unlock(&info->lock);
  730. }
  731. mutex_lock(&info->qlock);
  732. start_count = avalon->work_array * avalon_get_work_count;
  733. end_count = start_count + avalon_get_work_count;
  734. for (i = start_count, j = 0; i < end_count; i++, j++) {
  735. if (avalon_buffer_full(avalon)) {
  736. applog(LOG_INFO,
  737. "AVA%i: Buffer full after only %d of %d work queued",
  738. avalon->device_id, j, avalon_get_work_count);
  739. break;
  740. }
  741. if (likely(j < avalon->queued && !info->overheat)) {
  742. info->idle = false;
  743. avalon_init_task(&at, 0, 0, info->fan_pwm,
  744. info->timeout, info->asic_count,
  745. info->miner_count, 1, 0, info->frequency);
  746. avalon_create_task(&at, avalon->works[i]);
  747. info->auto_queued++;
  748. } else {
  749. idled++;
  750. avalon_init_task(&at, 0, 0, info->fan_pwm,
  751. info->timeout, info->asic_count,
  752. info->miner_count, 1, 1, info->frequency);
  753. /* Reset the auto_queued count if we end up
  754. * idling any miners. */
  755. info->auto_queued = 0;
  756. }
  757. ret = avalon_send_task(&at, avalon);
  758. if (unlikely(ret == AVA_SEND_ERROR)) {
  759. applog(LOG_ERR, "AVA%i: Comms error(buffer)",
  760. avalon->device_id);
  761. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  762. info->reset = true;
  763. break;
  764. }
  765. }
  766. avalon_rotate_array(avalon);
  767. pthread_cond_signal(&info->qcond);
  768. mutex_unlock(&info->qlock);
  769. if (unlikely(idled && !info->idle)) {
  770. info->idle = true;
  771. applog(LOG_WARNING, "AVA%i: Idled %d miners",
  772. avalon->device_id, idled);
  773. }
  774. }
  775. return NULL;
  776. }
  777. static bool avalon_prepare(struct thr_info *thr)
  778. {
  779. struct cgpu_info *avalon = thr->cgpu;
  780. struct avalon_info *info = avalon->device_data;
  781. struct timeval now;
  782. free(avalon->works);
  783. avalon->works = calloc(info->miner_count * sizeof(struct work *),
  784. AVALON_ARRAY_SIZE);
  785. if (!avalon->works)
  786. quit(1, "Failed to calloc avalon works in avalon_prepare");
  787. info->thr = thr;
  788. mutex_init(&info->lock);
  789. mutex_init(&info->qlock);
  790. if (unlikely(pthread_cond_init(&info->qcond, NULL)))
  791. quit(1, "Failed to pthread_cond_init avalon qcond");
  792. cgsem_init(&info->write_sem);
  793. if (pthread_create(&info->read_thr, NULL, avalon_get_results, (void *)avalon))
  794. quit(1, "Failed to create avalon read_thr");
  795. if (pthread_create(&info->write_thr, NULL, avalon_send_tasks, (void *)avalon))
  796. quit(1, "Failed to create avalon write_thr");
  797. avalon_init(avalon);
  798. cgtime(&now);
  799. get_datestamp(avalon->init, &now);
  800. return true;
  801. }
  802. static void do_avalon_close(struct thr_info *thr)
  803. {
  804. struct cgpu_info *avalon = thr->cgpu;
  805. struct avalon_info *info = avalon->device_data;
  806. pthread_join(info->read_thr, NULL);
  807. pthread_join(info->write_thr, NULL);
  808. avalon_running_reset(avalon, info);
  809. info->no_matching_work = 0;
  810. cgsem_destroy(&info->write_sem);
  811. }
  812. static inline void record_temp_fan(struct avalon_info *info, struct avalon_result *ar, float *temp_avg)
  813. {
  814. info->fan0 = ar->fan0 * AVALON_FAN_FACTOR;
  815. info->fan1 = ar->fan1 * AVALON_FAN_FACTOR;
  816. info->fan2 = ar->fan2 * AVALON_FAN_FACTOR;
  817. info->temp0 = ar->temp0;
  818. info->temp1 = ar->temp1;
  819. info->temp2 = ar->temp2;
  820. if (ar->temp0 & 0x80) {
  821. ar->temp0 &= 0x7f;
  822. info->temp0 = 0 - ((~ar->temp0 & 0x7f) + 1);
  823. }
  824. if (ar->temp1 & 0x80) {
  825. ar->temp1 &= 0x7f;
  826. info->temp1 = 0 - ((~ar->temp1 & 0x7f) + 1);
  827. }
  828. if (ar->temp2 & 0x80) {
  829. ar->temp2 &= 0x7f;
  830. info->temp2 = 0 - ((~ar->temp2 & 0x7f) + 1);
  831. }
  832. *temp_avg = info->temp2 > info->temp1 ? info->temp2 : info->temp1;
  833. if (info->temp0 > info->temp_max)
  834. info->temp_max = info->temp0;
  835. if (info->temp1 > info->temp_max)
  836. info->temp_max = info->temp1;
  837. if (info->temp2 > info->temp_max)
  838. info->temp_max = info->temp2;
  839. }
  840. static void temp_rise(struct avalon_info *info, int temp)
  841. {
  842. if (temp >= opt_avalon_temp + AVALON_TEMP_HYSTERESIS * 3) {
  843. info->fan_pwm = AVALON_DEFAULT_FAN_MAX_PWM;
  844. return;
  845. }
  846. if (temp >= opt_avalon_temp + AVALON_TEMP_HYSTERESIS * 2)
  847. info->fan_pwm += 10;
  848. else if (temp > opt_avalon_temp)
  849. info->fan_pwm += 5;
  850. else if (temp >= opt_avalon_temp - AVALON_TEMP_HYSTERESIS)
  851. info->fan_pwm += 1;
  852. else
  853. return;
  854. if (info->fan_pwm > AVALON_DEFAULT_FAN_MAX_PWM)
  855. info->fan_pwm = AVALON_DEFAULT_FAN_MAX_PWM;
  856. }
  857. static void temp_drop(struct avalon_info *info, int temp)
  858. {
  859. if (temp <= opt_avalon_temp - AVALON_TEMP_HYSTERESIS * 3) {
  860. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  861. return;
  862. }
  863. if (temp <= opt_avalon_temp - AVALON_TEMP_HYSTERESIS * 2)
  864. info->fan_pwm -= 10;
  865. else if (temp <= opt_avalon_temp - AVALON_TEMP_HYSTERESIS)
  866. info->fan_pwm -= 5;
  867. else if (temp < opt_avalon_temp)
  868. info->fan_pwm -= 1;
  869. if (info->fan_pwm < AVALON_DEFAULT_FAN_MIN_PWM)
  870. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  871. }
  872. static inline void adjust_fan(struct avalon_info *info)
  873. {
  874. int temp_new;
  875. temp_new = info->temp_sum / info->temp_history_count;
  876. if (temp_new > info->temp_old)
  877. temp_rise(info, temp_new);
  878. else if (temp_new < info->temp_old)
  879. temp_drop(info, temp_new);
  880. else {
  881. /* temp_new == info->temp_old */
  882. if (temp_new > opt_avalon_temp)
  883. temp_rise(info, temp_new);
  884. else if (temp_new < opt_avalon_temp - AVALON_TEMP_HYSTERESIS)
  885. temp_drop(info, temp_new);
  886. }
  887. info->temp_old = temp_new;
  888. }
  889. static void avalon_update_temps(struct cgpu_info *avalon, struct avalon_info *info,
  890. struct avalon_result *ar)
  891. {
  892. record_temp_fan(info, ar, &(avalon->temp));
  893. applog(LOG_INFO,
  894. "Avalon: Fan1: %d/m, Fan2: %d/m, Fan3: %d/m\t"
  895. "Temp1: %dC, Temp2: %dC, Temp3: %dC, TempMAX: %dC",
  896. info->fan0, info->fan1, info->fan2,
  897. info->temp0, info->temp1, info->temp2, info->temp_max);
  898. info->temp_history_index++;
  899. info->temp_sum += avalon->temp;
  900. applog(LOG_DEBUG, "Avalon: temp_index: %d, temp_count: %d, temp_old: %d",
  901. info->temp_history_index, info->temp_history_count, info->temp_old);
  902. if (info->temp_history_index == info->temp_history_count) {
  903. adjust_fan(info);
  904. info->temp_history_index = 0;
  905. info->temp_sum = 0;
  906. }
  907. if (unlikely(info->temp_old >= opt_avalon_overheat)) {
  908. applog(LOG_WARNING, "AVA%d overheat! Idling", avalon->device_id);
  909. info->overheat = true;
  910. } else if (info->overheat && info->temp_old <= opt_avalon_temp) {
  911. applog(LOG_WARNING, "AVA%d cooled, restarting", avalon->device_id);
  912. info->overheat = false;
  913. }
  914. }
  915. static void get_avalon_statline_before(char *buf, struct cgpu_info *avalon)
  916. {
  917. struct avalon_info *info = avalon->device_data;
  918. int lowfan = 10000;
  919. /* Find the lowest fan speed of the ASIC cooling fans. */
  920. if (info->fan1 >= 0 && info->fan1 < lowfan)
  921. lowfan = info->fan1;
  922. if (info->fan2 >= 0 && info->fan2 < lowfan)
  923. lowfan = info->fan2;
  924. tailsprintf(buf, "%2d/%3dC %04dR | ", info->temp0, info->temp2, lowfan);
  925. }
  926. /* We use a replacement algorithm to only remove references to work done from
  927. * the buffer when we need the extra space for new work. */
  928. static bool avalon_fill(struct cgpu_info *avalon)
  929. {
  930. struct avalon_info *info = avalon->device_data;
  931. int subid, slot, mc;
  932. struct work *work;
  933. bool ret = true;
  934. mc = info->miner_count;
  935. mutex_lock(&info->qlock);
  936. if (avalon->queued >= mc)
  937. goto out_unlock;
  938. work = get_queued(avalon);
  939. if (unlikely(!work)) {
  940. ret = false;
  941. goto out_unlock;
  942. }
  943. subid = avalon->queued++;
  944. work->subid = subid;
  945. slot = avalon->work_array * mc + subid;
  946. if (likely(avalon->works[slot]))
  947. work_completed(avalon, avalon->works[slot]);
  948. avalon->works[slot] = work;
  949. if (avalon->queued < mc)
  950. ret = false;
  951. out_unlock:
  952. mutex_unlock(&info->qlock);
  953. return ret;
  954. }
  955. static int64_t avalon_scanhash(struct thr_info *thr)
  956. {
  957. struct cgpu_info *avalon = thr->cgpu;
  958. struct avalon_info *info = avalon->device_data;
  959. const int miner_count = info->miner_count;
  960. struct timeval now, then, tdiff;
  961. int64_t hash_count, us_timeout;
  962. struct timespec abstime;
  963. /* Half nonce range */
  964. us_timeout = 0x80000000ll / info->asic_count / info->frequency;
  965. tdiff.tv_sec = us_timeout / 1000000;
  966. tdiff.tv_usec = us_timeout - (tdiff.tv_sec * 1000000);
  967. cgtime(&now);
  968. timeradd(&now, &tdiff, &then);
  969. abstime.tv_sec = then.tv_sec;
  970. abstime.tv_nsec = then.tv_usec * 1000;
  971. /* Wait until avalon_send_tasks signals us that it has completed
  972. * sending its work or a full nonce range timeout has occurred */
  973. mutex_lock(&info->qlock);
  974. pthread_cond_timedwait(&info->qcond, &info->qlock, &abstime);
  975. mutex_unlock(&info->qlock);
  976. mutex_lock(&info->lock);
  977. hash_count = 0xffffffffull * (uint64_t)info->nonces;
  978. avalon->results += info->nonces;
  979. if (avalon->results > miner_count)
  980. avalon->results = miner_count;
  981. if (!info->idle && !info->reset)
  982. avalon->results -= miner_count / 3;
  983. else
  984. avalon->results = miner_count;
  985. info->nonces = 0;
  986. mutex_unlock(&info->lock);
  987. /* Check for nothing but consecutive bad results or consistently less
  988. * results than we should be getting and reset the FPGA if necessary */
  989. if (avalon->results < -miner_count && !info->reset) {
  990. applog(LOG_ERR, "AVA%d: Result return rate low, resetting!",
  991. avalon->device_id);
  992. info->reset = true;
  993. }
  994. if (unlikely(avalon->usbinfo.nodev)) {
  995. applog(LOG_ERR, "AVA%d: Device disappeared, shutting down thread",
  996. avalon->device_id);
  997. avalon->shutdown = true;
  998. }
  999. /* This hashmeter is just a utility counter based on returned shares */
  1000. return hash_count;
  1001. }
  1002. static void avalon_flush_work(struct cgpu_info *avalon)
  1003. {
  1004. struct avalon_info *info = avalon->device_data;
  1005. mutex_lock(&info->qlock);
  1006. /* Will overwrite any work queued */
  1007. avalon->queued = 0;
  1008. pthread_cond_signal(&info->qcond);
  1009. mutex_unlock(&info->qlock);
  1010. }
  1011. static struct api_data *avalon_api_stats(struct cgpu_info *cgpu)
  1012. {
  1013. struct api_data *root = NULL;
  1014. struct avalon_info *info = cgpu->device_data;
  1015. int i;
  1016. root = api_add_int(root, "baud", &(info->baud), false);
  1017. root = api_add_int(root, "miner_count", &(info->miner_count),false);
  1018. root = api_add_int(root, "asic_count", &(info->asic_count), false);
  1019. root = api_add_int(root, "timeout", &(info->timeout), false);
  1020. root = api_add_int(root, "frequency", &(info->frequency), false);
  1021. root = api_add_int(root, "fan1", &(info->fan0), false);
  1022. root = api_add_int(root, "fan2", &(info->fan1), false);
  1023. root = api_add_int(root, "fan3", &(info->fan2), false);
  1024. root = api_add_int(root, "temp1", &(info->temp0), false);
  1025. root = api_add_int(root, "temp2", &(info->temp1), false);
  1026. root = api_add_int(root, "temp3", &(info->temp2), false);
  1027. root = api_add_int(root, "temp_max", &(info->temp_max), false);
  1028. root = api_add_int(root, "no_matching_work", &(info->no_matching_work), false);
  1029. for (i = 0; i < info->miner_count; i++) {
  1030. char mcw[24];
  1031. sprintf(mcw, "match_work_count%d", i + 1);
  1032. root = api_add_int(root, mcw, &(info->matching_work[i]), false);
  1033. }
  1034. return root;
  1035. }
  1036. static void avalon_shutdown(struct thr_info *thr)
  1037. {
  1038. do_avalon_close(thr);
  1039. }
  1040. struct device_drv avalon_drv = {
  1041. .drv_id = DRIVER_AVALON,
  1042. .dname = "avalon",
  1043. .name = "AVA",
  1044. .drv_detect = avalon_detect,
  1045. .thread_prepare = avalon_prepare,
  1046. .hash_work = hash_queued_work,
  1047. .queue_full = avalon_fill,
  1048. .scanwork = avalon_scanhash,
  1049. .flush_work = avalon_flush_work,
  1050. .get_api_stats = avalon_api_stats,
  1051. .get_statline_before = get_avalon_statline_before,
  1052. .reinit_device = avalon_init,
  1053. .thread_shutdown = avalon_shutdown,
  1054. };