driver-bitfury.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656
  1. /*
  2. * Copyright 2013-2014 Con Kolivas
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #include "miner.h"
  11. #include "driver-bitfury.h"
  12. #include "sha2.h"
  13. #include "mcp2210.h"
  14. #include "libbitfury.h"
  15. int opt_bxf_temp_target = BXF_TEMP_TARGET / 10;
  16. int opt_nfu_bits = 50;
  17. int opt_bxm_bits = 54;
  18. int opt_bxf_bits = 54;
  19. int opt_bxf_debug;
  20. int opt_osm_led_mode = 4;
  21. /* Wait longer 1/3 longer than it would take for a full nonce range */
  22. #define BF1WAIT 1600
  23. #define BF1MSGSIZE 7
  24. #define BF1INFOSIZE 14
  25. #define TWELVE_MHZ 12000000
  26. //Low port pins
  27. #define SK 1
  28. #define DO 2
  29. #define DI 4
  30. #define CS 8
  31. #define GPIO0 16
  32. #define GPIO1 32
  33. #define GPIO2 64
  34. #define GPIO3 128
  35. //GPIO pins
  36. #define GPIOL0 0
  37. #define GPIOL1 1
  38. #define GPIOL2 2
  39. #define GPIOL3 3
  40. #define GPIOH 4
  41. #define GPIOH1 5
  42. #define GPIOH2 6
  43. #define GPIOH3 7
  44. #define GPIOH4 8
  45. #define GPIOH5 9
  46. #define GPIOH6 10
  47. #define GPIOH7 11
  48. #define DEFAULT_DIR (SK | DO | CS | GPIO0 | GPIO1 | GPIO2 | GPIO3) /* Setup default input or output state per FTDI for SPI */
  49. #define DEFAULT_STATE (CS) /* CS idles high, CLK idles LOW for SPI0 */
  50. //MPSSE commands from FTDI AN_108
  51. #define INVALID_COMMAND 0xAB
  52. #define ENABLE_ADAPTIVE_CLOCK 0x96
  53. #define DISABLE_ADAPTIVE_CLOCK 0x97
  54. #define ENABLE_3_PHASE_CLOCK 0x8C
  55. #define DISABLE_3_PHASE_CLOCK 0x8D
  56. #define TCK_X5 0x8A
  57. #define TCK_D5 0x8B
  58. #define CLOCK_N_CYCLES 0x8E
  59. #define CLOCK_N8_CYCLES 0x8F
  60. #define PULSE_CLOCK_IO_HIGH 0x94
  61. #define PULSE_CLOCK_IO_LOW 0x95
  62. #define CLOCK_N8_CYCLES_IO_HIGH 0x9C
  63. #define CLOCK_N8_CYCLES_IO_LOW 0x9D
  64. #define TRISTATE_IO 0x9E
  65. #define TCK_DIVISOR 0x86
  66. #define LOOPBACK_END 0x85
  67. #define SET_OUT_ADBUS 0x80
  68. #define SET_OUT_ACBUS 0x82
  69. #define WRITE_BYTES_SPI0 0x11
  70. #define READ_WRITE_BYTES_SPI0 0x31
  71. static void bf1_empty_buffer(struct cgpu_info *bitfury)
  72. {
  73. char buf[512];
  74. int amount;
  75. do {
  76. usb_read_once(bitfury, buf, 512, &amount, C_BF1_FLUSH);
  77. } while (amount);
  78. }
  79. static bool bf1_open(struct cgpu_info *bitfury)
  80. {
  81. uint32_t buf[2];
  82. int err;
  83. bf1_empty_buffer(bitfury);
  84. /* Magic sequence to reset device only really needed for windows but
  85. * harmless on linux. */
  86. buf[0] = 0x80250000;
  87. buf[1] = 0x00000800;
  88. err = usb_transfer(bitfury, 0, 9, 1, 0, C_ATMEL_RESET);
  89. if (!err)
  90. err = usb_transfer(bitfury, 0x21, 0x22, 0, 0, C_ATMEL_OPEN);
  91. if (!err) {
  92. err = usb_transfer_data(bitfury, 0x21, 0x20, 0x0000, 0, buf,
  93. BF1MSGSIZE, C_ATMEL_INIT);
  94. }
  95. if (err < 0) {
  96. applog(LOG_INFO, "%s %d: Failed to open with error %s", bitfury->drv->name,
  97. bitfury->device_id, libusb_error_name(err));
  98. }
  99. return (err == BF1MSGSIZE);
  100. }
  101. static void bf1_close(struct cgpu_info *bitfury)
  102. {
  103. bf1_empty_buffer(bitfury);
  104. }
  105. static void bf1_identify(struct cgpu_info *bitfury)
  106. {
  107. int amount;
  108. usb_write(bitfury, "L", 1, &amount, C_BF1_IDENTIFY);
  109. }
  110. static void bitfury_identify(struct cgpu_info *bitfury)
  111. {
  112. struct bitfury_info *info = bitfury->device_data;
  113. switch(info->ident) {
  114. case IDENT_BF1:
  115. bf1_identify(bitfury);
  116. break;
  117. case IDENT_BXF:
  118. case IDENT_OSM:
  119. default:
  120. break;
  121. }
  122. }
  123. static bool bf1_getinfo(struct cgpu_info *bitfury, struct bitfury_info *info)
  124. {
  125. int amount, err;
  126. char buf[16];
  127. err = usb_write(bitfury, "I", 1, &amount, C_BF1_REQINFO);
  128. if (err) {
  129. applog(LOG_INFO, "%s %d: Failed to write REQINFO",
  130. bitfury->drv->name, bitfury->device_id);
  131. return false;
  132. }
  133. err = usb_read(bitfury, buf, BF1INFOSIZE, &amount, C_BF1_GETINFO);
  134. if (err) {
  135. applog(LOG_INFO, "%s %d: Failed to read GETINFO",
  136. bitfury->drv->name, bitfury->device_id);
  137. return false;
  138. }
  139. if (amount != BF1INFOSIZE) {
  140. applog(LOG_INFO, "%s %d: Getinfo received %d bytes instead of %d",
  141. bitfury->drv->name, bitfury->device_id, amount, BF1INFOSIZE);
  142. return false;
  143. }
  144. info->version = buf[1];
  145. cg_memcpy(&info->product, buf + 2, 8);
  146. cg_memcpy(&info->serial, buf + 10, 4);
  147. bitfury->unique_id = bin2hex((unsigned char *)buf + 10, 4);
  148. applog(LOG_INFO, "%s %d: Getinfo returned version %d, product %s serial %s", bitfury->drv->name,
  149. bitfury->device_id, info->version, info->product, bitfury->unique_id);
  150. bf1_empty_buffer(bitfury);
  151. return true;
  152. }
  153. static bool bf1_reset(struct cgpu_info *bitfury)
  154. {
  155. int amount, err;
  156. char buf[16];
  157. err = usb_write(bitfury, "R", 1, &amount, C_BF1_REQRESET);
  158. if (err) {
  159. applog(LOG_INFO, "%s %d: Failed to write REQRESET",
  160. bitfury->drv->name, bitfury->device_id);
  161. return false;
  162. }
  163. err = usb_read_timeout(bitfury, buf, BF1MSGSIZE, &amount, BF1WAIT,
  164. C_BF1_GETRESET);
  165. if (err) {
  166. applog(LOG_INFO, "%s %d: Failed to read GETRESET",
  167. bitfury->drv->name, bitfury->device_id);
  168. return false;
  169. }
  170. if (amount != BF1MSGSIZE) {
  171. applog(LOG_INFO, "%s %d: Getreset received %d bytes instead of %d",
  172. bitfury->drv->name, bitfury->device_id, amount, BF1MSGSIZE);
  173. return false;
  174. }
  175. applog(LOG_DEBUG, "%s %d: Getreset returned %s", bitfury->drv->name,
  176. bitfury->device_id, buf);
  177. bf1_empty_buffer(bitfury);
  178. return true;
  179. }
  180. static bool bxf_send_msg(struct cgpu_info *bitfury, char *buf, enum usb_cmds cmd)
  181. {
  182. int err, amount, len;
  183. if (unlikely(bitfury->usbinfo.nodev))
  184. return false;
  185. if (opt_bxf_debug) {
  186. char *strbuf = str_text(buf);
  187. applog(LOG_ERR, "%s %d: >BXF [%s]", bitfury->drv->name, bitfury->device_id, strbuf);
  188. free(strbuf);
  189. }
  190. len = strlen(buf);
  191. applog(LOG_DEBUG, "%s %d: Sending %s", bitfury->drv->name, bitfury->device_id, buf);
  192. err = usb_write(bitfury, buf, len, &amount, cmd);
  193. if (err || amount != len) {
  194. applog(LOG_WARNING, "%s %d: Error %d sending %s sent %d of %d", bitfury->drv->name,
  195. bitfury->device_id, err, usb_cmdname(cmd), amount, len);
  196. return false;
  197. }
  198. return true;
  199. }
  200. static bool bxf_send_debugmode(struct cgpu_info *bitfury)
  201. {
  202. char buf[16];
  203. sprintf(buf, "debug-mode %d\n", opt_bxf_debug);
  204. return bxf_send_msg(bitfury, buf, C_BXF_DEBUGMODE);
  205. }
  206. static bool bxf_send_ledmode(struct cgpu_info *bitfury)
  207. {
  208. char buf[16];
  209. sprintf(buf, "led-mode %d\n", opt_osm_led_mode);
  210. return bxf_send_msg(bitfury, buf, C_BXF_LEDMODE);
  211. }
  212. /* Returns the amount received only if we receive a full message, otherwise
  213. * it returns the err value. */
  214. static int bxf_recv_msg(struct cgpu_info *bitfury, char *buf)
  215. {
  216. int err, amount;
  217. err = usb_read_nl(bitfury, buf, 512, &amount, C_BXF_READ);
  218. if (amount)
  219. applog(LOG_DEBUG, "%s %d: Received %s", bitfury->drv->name, bitfury->device_id, buf);
  220. if (!err)
  221. return amount;
  222. return err;
  223. }
  224. /* Keep reading till the first timeout or error */
  225. static void bxf_clear_buffer(struct cgpu_info *bitfury)
  226. {
  227. int err, retries = 0;
  228. char buf[512];
  229. do {
  230. err = bxf_recv_msg(bitfury, buf);
  231. usb_buffer_clear(bitfury);
  232. if (err < 0)
  233. break;
  234. } while (retries++ < 10);
  235. }
  236. static bool bxf_send_flush(struct cgpu_info *bitfury)
  237. {
  238. char buf[8];
  239. sprintf(buf, "flush\n");
  240. return bxf_send_msg(bitfury, buf, C_BXF_FLUSH);
  241. }
  242. static bool bxf_detect_one(struct cgpu_info *bitfury, struct bitfury_info *info)
  243. {
  244. int err, retries = 0;
  245. char buf[512];
  246. if (!bxf_send_flush(bitfury))
  247. return false;
  248. bxf_clear_buffer(bitfury);
  249. sprintf(buf, "version\n");
  250. if (!bxf_send_msg(bitfury, buf, C_BXF_VERSION))
  251. return false;
  252. do {
  253. err = bxf_recv_msg(bitfury, buf);
  254. if (err < 0 && err != LIBUSB_ERROR_TIMEOUT)
  255. return false;
  256. if (err > 0 && !strncmp(buf, "version", 7)) {
  257. sscanf(&buf[8], "%d.%d rev %d chips %d", &info->ver_major,
  258. &info->ver_minor, &info->hw_rev, &info->chips);
  259. applog(LOG_INFO, "%s %d: Version %d.%d rev %d chips %d",
  260. bitfury->drv->name, bitfury->device_id, info->ver_major,
  261. info->ver_minor, info->hw_rev, info->chips);
  262. break;
  263. }
  264. /* Keep parsing if the buffer is full without counting it as
  265. * a retry. */
  266. if (usb_buffer_size(bitfury))
  267. continue;
  268. } while (retries++ < 10);
  269. if (!add_cgpu(bitfury))
  270. quit(1, "Failed to add_cgpu in bxf_detect_one");
  271. update_usb_stats(bitfury);
  272. applog(LOG_INFO, "%s %d: Successfully initialised %s",
  273. bitfury->drv->name, bitfury->device_id, bitfury->device_path);
  274. /* Sanity check and recognise variations */
  275. if (info->chips <= 2 || info->chips > 999)
  276. info->chips = 2;
  277. else if (info->chips <= 6 && info->ident == IDENT_BXF)
  278. bitfury->drv->name = "HXF";
  279. else if (info->chips > 6 && info->ident == IDENT_BXF)
  280. bitfury->drv->name = "MXF";
  281. info->filtered_hw = cgcalloc(sizeof(int), info->chips);
  282. info->job = cgcalloc(sizeof(int), info->chips);
  283. info->submits = cgcalloc(sizeof(int), info->chips);
  284. info->total_nonces = 1;
  285. info->temp_target = opt_bxf_temp_target * 10;
  286. /* This unsets it to make sure it gets set on the first pass */
  287. info->maxroll = -1;
  288. return true;
  289. }
  290. static bool bf1_detect_one(struct cgpu_info *bitfury, struct bitfury_info *info)
  291. {
  292. if (!bf1_open(bitfury))
  293. goto out_close;
  294. /* Send getinfo request */
  295. if (!bf1_getinfo(bitfury, info))
  296. goto out_close;
  297. /* Send reset request */
  298. if (!bf1_reset(bitfury))
  299. goto out_close;
  300. bf1_identify(bitfury);
  301. bf1_empty_buffer(bitfury);
  302. if (!add_cgpu(bitfury))
  303. quit(1, "Failed to add_cgpu in bf1_detect_one");
  304. update_usb_stats(bitfury);
  305. applog(LOG_INFO, "%s %d: Successfully initialised %s",
  306. bitfury->drv->name, bitfury->device_id, bitfury->device_path);
  307. /* This does not artificially raise hashrate, it simply allows the
  308. * hashrate to adapt quickly on starting. */
  309. info->total_nonces = 1;
  310. return true;
  311. out_close:
  312. bf1_close(bitfury);
  313. return false;
  314. }
  315. static void nfu_close(struct cgpu_info *bitfury)
  316. {
  317. struct bitfury_info *info = bitfury->device_data;
  318. struct mcp_settings *mcp = &info->mcp;
  319. int i;
  320. mcp2210_spi_cancel(bitfury);
  321. /* Set all pins to input mode, ignoring return code */
  322. for (i = 0; i < 9; i++) {
  323. mcp->direction.pin[i] = MCP2210_GPIO_INPUT;
  324. mcp->value.pin[i] = MCP2210_GPIO_PIN_LOW;
  325. }
  326. mcp2210_set_gpio_settings(bitfury, mcp);
  327. }
  328. static bool nfu_reinit(struct cgpu_info *bitfury, struct bitfury_info *info)
  329. {
  330. bool ret = true;
  331. int i;
  332. for (i = 0; i < info->chips; i++) {
  333. spi_clear_buf(info);
  334. spi_add_break(info);
  335. spi_add_fasync(info, i);
  336. spi_set_freq(info);
  337. spi_send_conf(info);
  338. spi_send_init(info);
  339. spi_reset(bitfury, info);
  340. ret = info->spi_txrx(bitfury, info);
  341. if (!ret)
  342. break;
  343. }
  344. return ret;
  345. }
  346. static bool nfu_set_spi_settings(struct cgpu_info *bitfury, struct bitfury_info *info)
  347. {
  348. struct mcp_settings *mcp = &info->mcp;
  349. return mcp2210_set_spi_transfer_settings(bitfury, mcp->bitrate, mcp->icsv,
  350. mcp->acsv, mcp->cstdd, mcp->ldbtcsd, mcp->sdbd, mcp->bpst, mcp->spimode);
  351. }
  352. static void nfu_alloc_arrays(struct bitfury_info *info)
  353. {
  354. info->payload = cgcalloc(sizeof(struct bitfury_payload), info->chips);
  355. info->oldbuf = cgcalloc(sizeof(unsigned int) * 17, info->chips);
  356. info->job_switched = cgcalloc(sizeof(bool), info->chips);
  357. info->second_run = cgcalloc(sizeof(bool), info->chips);
  358. info->work = cgcalloc(sizeof(struct work *), info->chips);
  359. info->owork = cgcalloc(sizeof(struct work *), info->chips);
  360. info->submits = cgcalloc(sizeof(int *), info->chips);
  361. }
  362. static bool nfu_detect_one(struct cgpu_info *bitfury, struct bitfury_info *info)
  363. {
  364. struct mcp_settings *mcp = &info->mcp;
  365. char buf[MCP2210_BUFFER_LENGTH];
  366. unsigned int length;
  367. bool ret = false;
  368. int i, val;
  369. /* Identify number of chips, and use it in device name if it can fit
  370. * into 3 chars, otherwise use generic NFU name. */
  371. val = sscanf(bitfury->usbdev->prod_string, "NanoFury NF%u ", &info->chips);
  372. if (val < 1)
  373. info->chips = 1;
  374. else if (info->chips < 10) {
  375. sprintf(info->product, "NF%u", info->chips);
  376. bitfury->drv->name = info->product;
  377. }
  378. nfu_alloc_arrays(info);
  379. info->spi_txrx = &mcp_spi_txrx;
  380. mcp2210_get_gpio_settings(bitfury, mcp);
  381. for (i = 0; i < 9; i++) {
  382. /* Set all pins to GPIO mode */
  383. mcp->designation.pin[i] = MCP2210_PIN_GPIO;
  384. /* Set all pins to input mode */
  385. mcp->direction.pin[i] = MCP2210_GPIO_INPUT;
  386. mcp->value.pin[i] = MCP2210_GPIO_PIN_LOW;
  387. }
  388. /* Set LED and PWR pins to output and high */
  389. mcp->direction.pin[NFU_PIN_LED] = mcp->direction.pin[NFU_PIN_PWR_EN] = MCP2210_GPIO_OUTPUT;
  390. mcp->value.pin[NFU_PIN_LED] = mcp->value.pin[NFU_PIN_PWR_EN] = MCP2210_GPIO_PIN_HIGH;
  391. mcp->direction.pin[NFU_PIN_PWR_EN0] = MCP2210_GPIO_OUTPUT;
  392. mcp->value.pin[NFU_PIN_PWR_EN0] = MCP2210_GPIO_PIN_LOW;
  393. mcp->direction.pin[4] = MCP2210_GPIO_OUTPUT;
  394. mcp->designation.pin[4] = MCP2210_PIN_CS;
  395. if (!mcp2210_set_gpio_settings(bitfury, mcp))
  396. goto out;
  397. if (opt_debug) {
  398. struct gpio_pin gp;
  399. mcp2210_get_gpio_pindirs(bitfury, &gp);
  400. for (i = 0; i < 9; i++) {
  401. applog(LOG_DEBUG, "%s %d: Pin dir %d %d", bitfury->drv->name,
  402. bitfury->device_id, i, gp.pin[i]);
  403. }
  404. mcp2210_get_gpio_pinvals(bitfury, &gp);
  405. for (i = 0; i < 9; i++) {
  406. applog(LOG_DEBUG, "%s %d: Pin val %d %d", bitfury->drv->name,
  407. bitfury->device_id, i, gp.pin[i]);
  408. }
  409. mcp2210_get_gpio_pindes(bitfury, &gp);
  410. for (i = 0; i < 9; i++) {
  411. applog(LOG_DEBUG, "%s %d: Pin des %d %d", bitfury->drv->name,
  412. bitfury->device_id, i, gp.pin[i]);
  413. }
  414. }
  415. /* Cancel any transfers in progress */
  416. if (!mcp2210_spi_cancel(bitfury))
  417. goto out;
  418. if (!mcp2210_get_spi_transfer_settings(bitfury, &mcp->bitrate, &mcp->icsv,
  419. &mcp->acsv, &mcp->cstdd, &mcp->ldbtcsd, &mcp->sdbd, &mcp->bpst, &mcp->spimode))
  420. goto out;
  421. mcp->bitrate = 200000; // default to 200kHz
  422. mcp->icsv = 0xffff;
  423. mcp->acsv = 0xffef;
  424. mcp->cstdd = mcp->ldbtcsd = mcp->sdbd = mcp->spimode = 0;
  425. mcp->bpst = 1;
  426. if (!nfu_set_spi_settings(bitfury, info))
  427. goto out;
  428. buf[0] = 0;
  429. length = 1;
  430. if (!mcp2210_spi_transfer(bitfury, mcp, buf, &length))
  431. goto out;
  432. /* after this command SCK_OVRRIDE should read the same as current SCK
  433. * value (which for mode 0 should be 0) */
  434. if (!mcp2210_get_gpio_pinval(bitfury, NFU_PIN_SCK_OVR, &val))
  435. goto out;
  436. if (val != MCP2210_GPIO_PIN_LOW)
  437. goto out;
  438. /* switch SCK to polarity (default SCK=1 in mode 2) */
  439. mcp->spimode = 2;
  440. if (!nfu_set_spi_settings(bitfury, info))
  441. goto out;
  442. buf[0] = 0;
  443. length = 1;
  444. if (!mcp2210_spi_transfer(bitfury, mcp, buf, &length))
  445. goto out;
  446. /* after this command SCK_OVRRIDE should read the same as current SCK
  447. * value (which for mode 2 should be 1) */
  448. if (!mcp2210_get_gpio_pinval(bitfury, NFU_PIN_SCK_OVR, &val))
  449. goto out;
  450. if (val != MCP2210_GPIO_PIN_HIGH)
  451. goto out;
  452. /* switch SCK to polarity (default SCK=0 in mode 0) */
  453. mcp->spimode = 0;
  454. if (!nfu_set_spi_settings(bitfury, info))
  455. goto out;
  456. buf[0] = 0;
  457. length = 1;
  458. if (!mcp2210_spi_transfer(bitfury, mcp, buf, &length))
  459. goto out;
  460. if (!mcp2210_get_gpio_pinval(bitfury, NFU_PIN_SCK_OVR, &val))
  461. goto out;
  462. if (val != MCP2210_GPIO_PIN_LOW)
  463. goto out;
  464. info->osc6_bits = opt_nfu_bits;
  465. if (!nfu_reinit(bitfury, info))
  466. goto out;
  467. ret = true;
  468. if (!add_cgpu(bitfury))
  469. quit(1, "Failed to add_cgpu in nfu_detect_one");
  470. update_usb_stats(bitfury);
  471. applog(LOG_INFO, "%s %d: Successfully initialised %s",
  472. bitfury->drv->name, bitfury->device_id, bitfury->device_path);
  473. spi_clear_buf(info);
  474. info->total_nonces = info->chips;
  475. out:
  476. if (!ret)
  477. nfu_close(bitfury);
  478. return ret;
  479. }
  480. static bool bxm_purge_buffers(struct cgpu_info *bitfury)
  481. {
  482. int err;
  483. err = usb_transfer(bitfury, FTDI_TYPE_OUT, SIO_RESET_REQUEST, SIO_RESET_PURGE_RX, 1, C_BXM_PURGERX);
  484. if (err)
  485. return false;
  486. err = usb_transfer(bitfury, FTDI_TYPE_OUT, SIO_RESET_REQUEST, SIO_RESET_PURGE_TX, 1, C_BXM_PURGETX);
  487. if (err)
  488. return false;
  489. return true;
  490. }
  491. /* Calculate required divisor for desired frequency see FTDI AN_108 page 19*/
  492. static uint16_t calc_divisor(uint32_t system_clock, uint32_t freq)
  493. {
  494. uint16_t divisor = system_clock / freq;
  495. divisor /= 2;
  496. divisor -= 1;
  497. return divisor;
  498. }
  499. static void bxm_shutdown(struct cgpu_info *bitfury, struct bitfury_info *info)
  500. {
  501. int chip_n;
  502. for (chip_n = 0; chip_n < 2; chip_n++) {
  503. spi_clear_buf(info);
  504. spi_add_break(info);
  505. spi_add_fasync(info, chip_n);
  506. spi_config_reg(info, 4, 0);
  507. info->spi_txrx(bitfury, info);
  508. }
  509. }
  510. static void bxm_close(struct cgpu_info *bitfury, struct bitfury_info *info)
  511. {
  512. unsigned char bitmask = 0;
  513. unsigned char mode = BITMODE_RESET;
  514. unsigned short usb_val = bitmask;
  515. bxm_shutdown(bitfury, info);
  516. //Need to do BITMODE_RESET before usb close per FTDI
  517. usb_val |= (mode << 8);
  518. usb_transfer(bitfury, FTDI_TYPE_OUT, SIO_SET_BITMODE_REQUEST, usb_val, 1, C_BXM_SETBITMODE);
  519. }
  520. static bool bxm_open(struct cgpu_info *bitfury)
  521. {
  522. unsigned char mode = BITMODE_RESET;
  523. unsigned char bitmask = 0;
  524. unsigned short usb_val = bitmask;
  525. uint32_t system_clock = TWELVE_MHZ;
  526. uint32_t freq = 200000;
  527. uint16_t divisor = calc_divisor(system_clock,freq);
  528. int amount, err;
  529. char buf[4];
  530. /* Enable the transaction translator emulator for these devices
  531. * otherwise we may write to them too quickly. */
  532. bitfury->usbdev->tt = true;
  533. err = usb_transfer(bitfury, FTDI_TYPE_OUT, SIO_RESET_REQUEST, SIO_RESET_SIO, 1, C_BXM_SRESET);
  534. if (err)
  535. return false;
  536. err = usb_transfer(bitfury, FTDI_TYPE_OUT, SIO_SET_LATENCY_TIMER_REQUEST, BXM_LATENCY_MS, 1, C_BXM_SETLATENCY);
  537. if (err)
  538. return false;
  539. err = usb_transfer(bitfury, FTDI_TYPE_OUT, SIO_SET_EVENT_CHAR_REQUEST, 0x00, 1, C_BXM_SECR);
  540. if (err)
  541. return false;
  542. //Do a BITMODE_RESET
  543. usb_val |= (mode << 8);
  544. err = usb_transfer(bitfury, FTDI_TYPE_OUT, SIO_SET_BITMODE_REQUEST, usb_val, 1, C_BXM_SETBITMODE);
  545. if (err)
  546. return false;
  547. //Now set to MPSSE mode
  548. bitmask = 0;
  549. mode = BITMODE_MPSSE;
  550. usb_val = bitmask;
  551. usb_val |= (mode << 8);
  552. err = usb_transfer(bitfury, FTDI_TYPE_OUT, SIO_SET_BITMODE_REQUEST, usb_val, 1, C_BXM_SETBITMODE);
  553. if (err)
  554. return false;
  555. //Now set the clock divisor
  556. //First send just the 0x8B command to set the system clock to 12MHz
  557. memset(buf, 0, 4);
  558. buf[0] = TCK_D5;
  559. err = usb_write(bitfury, buf, 1, &amount, C_BXM_CLOCK);
  560. if (err || amount != 1)
  561. return false;
  562. buf[0] = TCK_DIVISOR;
  563. buf[1] = (divisor & 0xFF);
  564. buf[2] = ((divisor >> 8) & 0xFF);
  565. err = usb_write(bitfury, buf, 3, &amount, C_BXM_CLOCKDIV);
  566. if (err || amount != 3)
  567. return false;
  568. //Disable internal loopback
  569. buf[0] = LOOPBACK_END;
  570. err = usb_write(bitfury, buf, 1, &amount, C_BXM_LOOP);
  571. if (err || amount != 1)
  572. return false;
  573. //Now set direction and idle (initial) states for the pins
  574. buf[0] = SET_OUT_ADBUS;
  575. buf[1] = DEFAULT_STATE; //Bitmask for LOW_PORT
  576. buf[2] = DEFAULT_DIR;
  577. err = usb_write(bitfury, buf, 3, &amount, C_BXM_ADBUS);
  578. if (err || amount != 3)
  579. return false;
  580. //Set the pin states for the HIGH_BITS port as all outputs, all low
  581. buf[0] = SET_OUT_ACBUS;
  582. buf[1] = 0x00; //Bitmask for HIGH_PORT
  583. buf[2] = 0xFF;
  584. err = usb_write(bitfury, buf, 3, &amount, C_BXM_ACBUS);
  585. if (err || amount != 3)
  586. return false;
  587. return true;
  588. }
  589. static bool bxm_set_CS_low(struct cgpu_info *bitfury)
  590. {
  591. char buf[4] = { 0 };
  592. int err, amount;
  593. buf[0] = SET_OUT_ADBUS;
  594. buf[1] &= ~DEFAULT_STATE; //Bitmask for LOW_PORT
  595. buf[2] = DEFAULT_DIR;
  596. err = usb_write(bitfury, buf, 3, &amount, C_BXM_CSLOW);
  597. if (err || amount != 3)
  598. return false;
  599. return true;
  600. }
  601. static bool bxm_set_CS_high(struct cgpu_info *bitfury)
  602. {
  603. char buf[4] = { 0 };
  604. int err, amount;
  605. buf[0] = SET_OUT_ADBUS;
  606. buf[1] = DEFAULT_STATE; //Bitmask for LOW_PORT
  607. buf[2] = DEFAULT_DIR;
  608. err = usb_write(bitfury, buf, 3, &amount, C_BXM_CSHIGH);
  609. if (err || amount != 3)
  610. return false;
  611. return true;
  612. }
  613. static bool bxm_reset_bitfury(struct cgpu_info *bitfury)
  614. {
  615. char buf[20] = { 0 };
  616. char rst_buf[8] = {0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00};
  617. int err, amount;
  618. //Set the FTDI CS pin HIGH. This will gate the clock to the Bitfury chips so we can send the reset sequence.
  619. if (!bxm_set_CS_high(bitfury))
  620. return false;
  621. buf[0] = WRITE_BYTES_SPI0;
  622. buf[1] = (uint8_t)16 - (uint8_t)1;
  623. buf[2] = 0;
  624. cg_memcpy(&buf[3], rst_buf, 8);
  625. cg_memcpy(&buf[11], rst_buf, 8);
  626. err = usb_write(bitfury, buf, 19, &amount, C_BXM_RESET);
  627. if (err || amount != 19)
  628. return false;
  629. if (!bxm_set_CS_low(bitfury))
  630. return false;
  631. return true;
  632. }
  633. static bool bxm_reinit(struct cgpu_info *bitfury, struct bitfury_info *info)
  634. {
  635. bool ret;
  636. int i;
  637. for (i = 0; i < 2; i++) {
  638. spi_clear_buf(info);
  639. spi_add_break(info);
  640. spi_add_fasync(info, i);
  641. spi_set_freq(info);
  642. spi_send_conf(info);
  643. spi_send_init(info);
  644. ret = info->spi_txrx(bitfury, info);
  645. if (!ret)
  646. break;
  647. }
  648. return ret;
  649. }
  650. static bool bxm_detect_one(struct cgpu_info *bitfury, struct bitfury_info *info)
  651. {
  652. bool ret;
  653. info->spi_txrx = &ftdi_spi_txrx;
  654. ret = bxm_open(bitfury);
  655. if (!ret)
  656. goto out;
  657. ret = bxm_purge_buffers(bitfury);
  658. if (!ret)
  659. goto out;
  660. ret = bxm_reset_bitfury(bitfury);
  661. if (!ret)
  662. goto out;
  663. ret = bxm_purge_buffers(bitfury);
  664. if (!ret)
  665. goto out;
  666. /* Do a dummy read */
  667. memset(info->spibuf, 0, 80);
  668. info->spibufsz = 80;
  669. ret = info->spi_txrx(bitfury, info);
  670. if (!ret)
  671. goto out;
  672. info->osc6_bits = opt_bxm_bits;
  673. /* Only have 2 chip devices for now */
  674. info->chips = 2;
  675. nfu_alloc_arrays(info);
  676. ret = bxm_reinit(bitfury, info);
  677. if (!ret)
  678. goto out;
  679. if (!add_cgpu(bitfury))
  680. quit(1, "Failed to add_cgpu in bxm_detect_one");
  681. update_usb_stats(bitfury);
  682. applog(LOG_INFO, "%s %d: Successfully initialised %s",
  683. bitfury->drv->name, bitfury->device_id, bitfury->device_path);
  684. spi_clear_buf(info);
  685. info->total_nonces = 1;
  686. out:
  687. if (!ret)
  688. bxm_close(bitfury, info);
  689. return ret;
  690. }
  691. static struct cgpu_info *bitfury_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  692. {
  693. struct cgpu_info *bitfury;
  694. struct bitfury_info *info;
  695. enum sub_ident ident;
  696. bool ret = false;
  697. bitfury = usb_alloc_cgpu(&bitfury_drv, 1);
  698. if (!usb_init(bitfury, dev, found))
  699. goto out;
  700. applog(LOG_INFO, "%s %d: Found at %s", bitfury->drv->name,
  701. bitfury->device_id, bitfury->device_path);
  702. info = cgcalloc(sizeof(struct bitfury_info), 1);
  703. bitfury->device_data = info;
  704. info->ident = ident = usb_ident(bitfury);
  705. switch (ident) {
  706. case IDENT_BF1:
  707. ret = bf1_detect_one(bitfury, info);
  708. break;
  709. case IDENT_BXF:
  710. case IDENT_OSM:
  711. ret = bxf_detect_one(bitfury, info);
  712. break;
  713. case IDENT_NFU:
  714. ret = nfu_detect_one(bitfury, info);
  715. break;
  716. case IDENT_BXM:
  717. ret = bxm_detect_one(bitfury, info);
  718. break;
  719. default:
  720. applog(LOG_INFO, "%s %d: Unrecognised bitfury device",
  721. bitfury->drv->name, bitfury->device_id);
  722. break;
  723. }
  724. if (!ret) {
  725. free(info);
  726. usb_uninit(bitfury);
  727. out:
  728. bitfury = usb_free_cgpu(bitfury);
  729. }
  730. return bitfury;
  731. }
  732. static void bitfury_detect(bool __maybe_unused hotplug)
  733. {
  734. usb_detect(&bitfury_drv, bitfury_detect_one);
  735. }
  736. static void adjust_bxf_chips(struct cgpu_info *bitfury, struct bitfury_info *info, int chip)
  737. {
  738. int chips = chip + 1;
  739. size_t old, new;
  740. if (likely(chips <= info->chips))
  741. return;
  742. if (chips > 999)
  743. return;
  744. old = sizeof(int) * info->chips;
  745. new = sizeof(int) * chips;
  746. applog(LOG_INFO, "%s %d: Adjust chip size to %d", bitfury->drv->name, bitfury->device_id,
  747. chips);
  748. recalloc(info->filtered_hw, old, new);
  749. recalloc(info->job, old, new);
  750. recalloc(info->submits, old, new);
  751. if (info->chips == 2 && chips <= 6 && info->ident == IDENT_BXF)
  752. bitfury->drv->name = "HXF";
  753. else if (info->chips <= 6 && chips > 6 && info->ident == IDENT_BXF)
  754. bitfury->drv->name = "MXF";
  755. info->chips = chips;
  756. }
  757. static void parse_bxf_submit(struct cgpu_info *bitfury, struct bitfury_info *info, char *buf)
  758. {
  759. struct work *match_work, *tmp, *work = NULL;
  760. struct thr_info *thr = info->thr;
  761. uint32_t nonce, timestamp;
  762. int workid, chip = -1;
  763. if (!sscanf(&buf[7], "%x %x %x %d", &nonce, &workid, &timestamp, &chip)) {
  764. applog(LOG_WARNING, "%s %d: Failed to parse submit response",
  765. bitfury->drv->name, bitfury->device_id);
  766. return;
  767. }
  768. adjust_bxf_chips(bitfury, info, chip);
  769. if (unlikely(chip >= info->chips || chip < 0)) {
  770. applog(LOG_INFO, "%s %d: Invalid submit chip number %d",
  771. bitfury->drv->name, bitfury->device_id, chip);
  772. } else
  773. info->submits[chip]++;
  774. applog(LOG_DEBUG, "%s %d: Parsed nonce %u workid %d timestamp %u",
  775. bitfury->drv->name, bitfury->device_id, nonce, workid, timestamp);
  776. rd_lock(&bitfury->qlock);
  777. HASH_ITER(hh, bitfury->queued_work, match_work, tmp) {
  778. if (match_work->subid == workid) {
  779. work = copy_work(match_work);
  780. break;
  781. }
  782. }
  783. rd_unlock(&bitfury->qlock);
  784. if (!work) {
  785. /* Discard first results from any previous run */
  786. if (unlikely(!info->valid))
  787. return;
  788. applog(LOG_INFO, "%s %d: No matching work", bitfury->drv->name, bitfury->device_id);
  789. mutex_lock(&info->lock);
  790. info->no_matching_work++;
  791. mutex_unlock(&info->lock);
  792. inc_hw_errors(thr);
  793. return;
  794. }
  795. /* Set the device start time from when we first get valid results */
  796. if (unlikely(!info->valid)) {
  797. info->valid = true;
  798. cgtime(&bitfury->dev_start_tv);
  799. }
  800. set_work_ntime(work, timestamp);
  801. if (submit_nonce(thr, work, nonce)) {
  802. mutex_lock(&info->lock);
  803. info->nonces++;
  804. mutex_unlock(&info->lock);
  805. }
  806. free_work(work);
  807. }
  808. static bool bxf_send_clock(struct cgpu_info *bitfury, struct bitfury_info *info,
  809. uint8_t clockspeed)
  810. {
  811. char buf[64];
  812. info->clocks = clockspeed;
  813. sprintf(buf, "clock %d %d\n", clockspeed, clockspeed);
  814. return bxf_send_msg(bitfury, buf, C_BXF_CLOCK);
  815. }
  816. static void parse_bxf_temp(struct cgpu_info *bitfury, struct bitfury_info *info, char *buf)
  817. {
  818. uint8_t clockspeed = info->clocks;
  819. int decitemp;
  820. if (!sscanf(&buf[5], "%d", &decitemp)) {
  821. applog(LOG_INFO, "%s %d: Failed to parse temperature",
  822. bitfury->drv->name, bitfury->device_id);
  823. return;
  824. }
  825. mutex_lock(&info->lock);
  826. bitfury->temp = (double)decitemp / 10;
  827. if (decitemp > info->max_decitemp) {
  828. info->max_decitemp = decitemp;
  829. applog(LOG_DEBUG, "%s %d: New max decitemp %d", bitfury->drv->name,
  830. bitfury->device_id, decitemp);
  831. }
  832. mutex_unlock(&info->lock);
  833. if (decitemp > info->temp_target + BXF_TEMP_HYSTERESIS) {
  834. if (info->clocks <= BXF_CLOCK_MIN)
  835. goto out;
  836. applog(LOG_WARNING, "%s %d: Hit overheat temperature of %d, throttling!",
  837. bitfury->drv->name, bitfury->device_id, decitemp);
  838. clockspeed = BXF_CLOCK_MIN;
  839. goto out;
  840. }
  841. if (decitemp > info->temp_target) {
  842. if (info->clocks <= BXF_CLOCK_MIN)
  843. goto out;
  844. if (decitemp < info->last_decitemp)
  845. goto out;
  846. applog(LOG_INFO, "%s %d: Temp %d over target and not falling, decreasing clock",
  847. bitfury->drv->name, bitfury->device_id, decitemp);
  848. clockspeed = info->clocks - 1;
  849. goto out;
  850. }
  851. if (decitemp <= info->temp_target && decitemp >= info->temp_target - BXF_TEMP_HYSTERESIS) {
  852. if (decitemp == info->last_decitemp)
  853. goto out;
  854. if (decitemp > info->last_decitemp) {
  855. if (info->clocks <= BXF_CLOCK_MIN)
  856. goto out;
  857. applog(LOG_DEBUG, "%s %d: Temp %d in target and rising, decreasing clock",
  858. bitfury->drv->name, bitfury->device_id, decitemp);
  859. clockspeed = info->clocks - 1;
  860. goto out;
  861. }
  862. /* implies: decitemp < info->last_decitemp */
  863. if (info->clocks >= opt_bxf_bits)
  864. goto out;
  865. applog(LOG_DEBUG, "%s %d: Temp %d in target and falling, increasing clock",
  866. bitfury->drv->name, bitfury->device_id, decitemp);
  867. clockspeed = info->clocks + 1;
  868. goto out;
  869. }
  870. /* implies: decitemp < info->temp_target - BXF_TEMP_HYSTERESIS */
  871. if (info->clocks >= opt_bxf_bits)
  872. goto out;
  873. applog(LOG_DEBUG, "%s %d: Temp %d below target, increasing clock",
  874. bitfury->drv->name, bitfury->device_id, decitemp);
  875. clockspeed = info->clocks + 1;
  876. out:
  877. bxf_send_clock(bitfury, info, clockspeed);
  878. info->last_decitemp = decitemp;
  879. }
  880. static void bxf_update_work(struct cgpu_info *bitfury, struct bitfury_info *info);
  881. static void parse_bxf_needwork(struct cgpu_info *bitfury, struct bitfury_info *info,
  882. char *buf)
  883. {
  884. int needed;
  885. if (!sscanf(&buf[9], "%d", &needed)) {
  886. applog(LOG_INFO, "%s %d: Failed to parse needwork",
  887. bitfury->drv->name, bitfury->device_id);
  888. return;
  889. }
  890. while (needed-- > 0)
  891. bxf_update_work(bitfury, info);
  892. }
  893. static void parse_bxf_job(struct cgpu_info *bitfury, struct bitfury_info *info, char *buf)
  894. {
  895. int job_id, timestamp, chip;
  896. if (sscanf(&buf[4], "%x %x %x", &job_id, &timestamp, &chip) != 3) {
  897. applog(LOG_INFO, "%s %d: Failed to parse job",
  898. bitfury->drv->name, bitfury->device_id);
  899. return;
  900. }
  901. adjust_bxf_chips(bitfury, info, chip);
  902. if (chip >= info->chips || chip < 0) {
  903. applog(LOG_INFO, "%s %d: Invalid job chip number %d",
  904. bitfury->drv->name, bitfury->device_id, chip);
  905. return;
  906. }
  907. ++info->job[chip];
  908. }
  909. static void parse_bxf_hwerror(struct cgpu_info *bitfury, struct bitfury_info *info, char *buf)
  910. {
  911. int chip;
  912. if (!sscanf(&buf[8], "%d", &chip)) {
  913. applog(LOG_INFO, "%s %d: Failed to parse hwerror",
  914. bitfury->drv->name, bitfury->device_id);
  915. return;
  916. }
  917. adjust_bxf_chips(bitfury, info, chip);
  918. if (chip >= info->chips || chip < 0) {
  919. applog(LOG_INFO, "%s %d: Invalid hwerror chip number %d",
  920. bitfury->drv->name, bitfury->device_id, chip);
  921. return;
  922. }
  923. ++info->filtered_hw[chip];
  924. }
  925. #define PARSE_BXF_MSG(MSG) \
  926. msg = strstr(buf, #MSG); \
  927. if (msg) { \
  928. parse_bxf_##MSG(bitfury, info, msg); \
  929. continue; \
  930. }
  931. static void *bxf_get_results(void *userdata)
  932. {
  933. struct cgpu_info *bitfury = userdata;
  934. struct bitfury_info *info = bitfury->device_data;
  935. char threadname[24], buf[512];
  936. snprintf(threadname, 24, "bxf_recv/%d", bitfury->device_id);
  937. /* We operate the device at lowest diff since it's not a lot of results
  938. * to process and gives us a better indicator of the nonce return rate
  939. * and hardware errors. */
  940. sprintf(buf, "target ffffffff\n");
  941. if (!bxf_send_msg(bitfury, buf, C_BXF_TARGET))
  942. goto out;
  943. /* Read thread sends the first work item to get the device started
  944. * since it will roll ntime and make work itself from there on. */
  945. bxf_update_work(bitfury, info);
  946. bxf_update_work(bitfury, info);
  947. while (likely(!bitfury->shutdown)) {
  948. char *msg, *strbuf;
  949. int err;
  950. if (unlikely(bitfury->usbinfo.nodev))
  951. break;
  952. err = bxf_recv_msg(bitfury, buf);
  953. if (err < 0) {
  954. if (err != LIBUSB_ERROR_TIMEOUT)
  955. break;
  956. continue;
  957. }
  958. if (!err)
  959. continue;
  960. if (opt_bxf_debug) {
  961. strbuf = str_text(buf);
  962. applog(LOG_ERR, "%s %d: < [%s]",
  963. bitfury->drv->name, bitfury->device_id, strbuf);
  964. free(strbuf);
  965. }
  966. PARSE_BXF_MSG(submit);
  967. PARSE_BXF_MSG(temp);
  968. PARSE_BXF_MSG(needwork);
  969. PARSE_BXF_MSG(job);
  970. PARSE_BXF_MSG(hwerror);
  971. if (buf[0] != '#') {
  972. strbuf = str_text(buf);
  973. applog(LOG_DEBUG, "%s %d: Unrecognised string %s",
  974. bitfury->drv->name, bitfury->device_id, strbuf);
  975. free(strbuf);
  976. }
  977. }
  978. out:
  979. return NULL;
  980. }
  981. static bool bxf_prepare(struct cgpu_info *bitfury, struct bitfury_info *info)
  982. {
  983. bxf_send_ledmode(bitfury);
  984. bxf_send_debugmode(bitfury);
  985. mutex_init(&info->lock);
  986. if (pthread_create(&info->read_thr, NULL, bxf_get_results, (void *)bitfury))
  987. quit(1, "Failed to create bxf read_thr");
  988. return bxf_send_clock(bitfury, info, opt_bxf_bits);
  989. }
  990. static bool bitfury_prepare(struct thr_info *thr)
  991. {
  992. struct cgpu_info *bitfury = thr->cgpu;
  993. struct bitfury_info *info = bitfury->device_data;
  994. info->thr = thr;
  995. switch(info->ident) {
  996. case IDENT_BXF:
  997. case IDENT_OSM:
  998. return bxf_prepare(bitfury, info);
  999. break;
  1000. case IDENT_BF1:
  1001. default:
  1002. return true;
  1003. }
  1004. }
  1005. static int64_t bitfury_rate(struct bitfury_info *info)
  1006. {
  1007. double nonce_rate;
  1008. int64_t ret = 0;
  1009. info->cycles++;
  1010. info->total_nonces += info->nonces;
  1011. info->saved_nonces += info->nonces;
  1012. info->nonces = 0;
  1013. nonce_rate = (double)info->total_nonces / (double)info->cycles;
  1014. if (info->saved_nonces >= nonce_rate) {
  1015. info->saved_nonces -= nonce_rate;
  1016. ret = (double)0xffffffff * nonce_rate;
  1017. }
  1018. return ret;
  1019. }
  1020. static int64_t bf1_scan(struct thr_info *thr, struct cgpu_info *bitfury,
  1021. struct bitfury_info *info)
  1022. {
  1023. int amount, i, aged, total = 0, ms_diff;
  1024. char readbuf[512], buf[45];
  1025. struct work *work, *tmp;
  1026. struct timeval tv_now;
  1027. int64_t ret = 0;
  1028. work = get_queue_work(thr, bitfury, thr->id);
  1029. if (unlikely(thr->work_restart)) {
  1030. work_completed(bitfury, work);
  1031. goto out;
  1032. }
  1033. buf[0] = 'W';
  1034. cg_memcpy(buf + 1, work->midstate, 32);
  1035. cg_memcpy(buf + 33, work->data + 64, 12);
  1036. /* New results may spill out from the latest work, making us drop out
  1037. * too early so read whatever we get for the first half nonce and then
  1038. * look for the results to prev work. */
  1039. cgtime(&tv_now);
  1040. ms_diff = 600 - ms_tdiff(&tv_now, &info->tv_start);
  1041. if (ms_diff > 0) {
  1042. usb_read_timeout_cancellable(bitfury, readbuf, 512, &amount, ms_diff,
  1043. C_BF1_GETRES);
  1044. total += amount;
  1045. }
  1046. /* Now look for the bulk of the previous work results, they will come
  1047. * in a batch following the first data. */
  1048. cgtime(&tv_now);
  1049. ms_diff = BF1WAIT - ms_tdiff(&tv_now, &info->tv_start);
  1050. /* If a work restart was sent, just empty the buffer. */
  1051. if (unlikely(ms_diff < 10 || thr->work_restart))
  1052. ms_diff = 10;
  1053. usb_read_once_timeout_cancellable(bitfury, readbuf + total, BF1MSGSIZE,
  1054. &amount, ms_diff, C_BF1_GETRES);
  1055. total += amount;
  1056. while (amount) {
  1057. usb_read_once_timeout(bitfury, readbuf + total, 512 - total, &amount, 10,
  1058. C_BF1_GETRES);
  1059. total += amount;
  1060. };
  1061. /* Don't send whatever work we've stored if we got a restart */
  1062. if (unlikely(thr->work_restart))
  1063. goto out;
  1064. /* Send work */
  1065. cgtime(&work->tv_work_start);
  1066. usb_write(bitfury, buf, 45, &amount, C_BF1_REQWORK);
  1067. cgtime(&info->tv_start);
  1068. /* Get response acknowledging work */
  1069. usb_read(bitfury, buf, BF1MSGSIZE, &amount, C_BF1_GETWORK);
  1070. out:
  1071. /* Search for what work the nonce matches in order of likelihood. Last
  1072. * entry is end of result marker. */
  1073. for (i = 0; i < total - BF1MSGSIZE; i += BF1MSGSIZE) {
  1074. bool found = false;
  1075. uint32_t nonce;
  1076. /* Ignore state & switched data in results for now. */
  1077. cg_memcpy(&nonce, readbuf + i + 3, 4);
  1078. nonce = decnonce(nonce);
  1079. rd_lock(&bitfury->qlock);
  1080. HASH_ITER(hh, bitfury->queued_work, work, tmp) {
  1081. if (bitfury_checkresults(thr, work, nonce)) {
  1082. info->nonces++;
  1083. found = true;
  1084. break;
  1085. }
  1086. }
  1087. rd_unlock(&bitfury->qlock);
  1088. if (!found) {
  1089. if (likely(info->valid))
  1090. inc_hw_errors(thr);
  1091. } else if (unlikely(!info->valid)) {
  1092. info->valid = true;
  1093. cgtime(&bitfury->dev_start_tv);
  1094. }
  1095. }
  1096. cgtime(&tv_now);
  1097. /* This iterates over the hashlist finding work started more than 6
  1098. * seconds ago. */
  1099. aged = age_queued_work(bitfury, 6.0);
  1100. if (aged) {
  1101. applog(LOG_DEBUG, "%s %d: Aged %d work items", bitfury->drv->name,
  1102. bitfury->device_id, aged);
  1103. }
  1104. ret = bitfury_rate(info);
  1105. if (unlikely(bitfury->usbinfo.nodev)) {
  1106. applog(LOG_WARNING, "%s %d: Device disappeared, disabling thread",
  1107. bitfury->drv->name, bitfury->device_id);
  1108. ret = -1;
  1109. }
  1110. return ret;
  1111. }
  1112. static int64_t bxf_scan(struct cgpu_info *bitfury, struct bitfury_info *info)
  1113. {
  1114. int ms, aged;
  1115. int64_t ret;
  1116. bxf_update_work(bitfury, info);
  1117. ms = 1200 / info->chips;
  1118. if (ms < 100)
  1119. ms = 100;
  1120. cgsleep_ms(ms);
  1121. mutex_lock(&info->lock);
  1122. ret = bitfury_rate(info);
  1123. mutex_unlock(&info->lock);
  1124. /* Keep no more than the last 90 seconds worth of work items in the
  1125. * hashlist */
  1126. aged = age_queued_work(bitfury, 90.0);
  1127. if (aged) {
  1128. applog(LOG_DEBUG, "%s %d: Aged %d work items", bitfury->drv->name,
  1129. bitfury->device_id, aged);
  1130. }
  1131. if (unlikely(bitfury->usbinfo.nodev)) {
  1132. applog(LOG_WARNING, "%s %d: Device disappeared, disabling thread",
  1133. bitfury->drv->name, bitfury->device_id);
  1134. ret = -1;
  1135. }
  1136. return ret;
  1137. }
  1138. static void bitfury_check_work(struct thr_info *thr, struct cgpu_info *bitfury,
  1139. struct bitfury_info *info, int chip_n)
  1140. {
  1141. if (!info->work[chip_n]) {
  1142. info->work[chip_n] = get_work(thr, thr->id);
  1143. if (unlikely(thr->work_restart)) {
  1144. free_work(info->work[chip_n]);
  1145. info->work[chip_n] = NULL;
  1146. return;
  1147. }
  1148. bitfury_work_to_payload(&info->payload[chip_n], info->work[chip_n]);
  1149. }
  1150. if (unlikely(bitfury->usbinfo.nodev))
  1151. return;
  1152. if (!libbitfury_sendHashData(thr, bitfury, info, chip_n))
  1153. usb_nodev(bitfury);
  1154. if (info->job_switched[chip_n]) {
  1155. if (likely(info->owork[chip_n]))
  1156. free_work(info->owork[chip_n]);
  1157. info->owork[chip_n] = info->work[chip_n];
  1158. info->work[chip_n] = NULL;
  1159. }
  1160. }
  1161. static int64_t nfu_scan(struct thr_info *thr, struct cgpu_info *bitfury,
  1162. struct bitfury_info *info)
  1163. {
  1164. int64_t ret = 0;
  1165. int i;
  1166. for (i = 0; i < info->chips; i++)
  1167. bitfury_check_work(thr, bitfury, info, i);
  1168. ret = bitfury_rate(info);
  1169. if (unlikely(bitfury->usbinfo.nodev)) {
  1170. applog(LOG_WARNING, "%s %d: Device disappeared, disabling thread",
  1171. bitfury->drv->name, bitfury->device_id);
  1172. ret = -1;
  1173. }
  1174. return ret;
  1175. }
  1176. static int64_t bitfury_scanwork(struct thr_info *thr)
  1177. {
  1178. struct cgpu_info *bitfury = thr->cgpu;
  1179. struct bitfury_info *info = bitfury->device_data;
  1180. int64_t ret = -1;
  1181. if (unlikely(share_work_tdiff(bitfury) > 60)) {
  1182. if (info->failing) {
  1183. if (share_work_tdiff(bitfury) > 120) {
  1184. applog(LOG_ERR, "%s %d: Device failed to respond to restart",
  1185. bitfury->drv->name, bitfury->device_id);
  1186. return ret;
  1187. }
  1188. } else {
  1189. applog(LOG_WARNING, "%s %d: No valid hashes for over 1 minute, attempting to reset",
  1190. bitfury->drv->name, bitfury->device_id);
  1191. usb_reset(bitfury);
  1192. info->failing = true;
  1193. }
  1194. }
  1195. if (unlikely(bitfury->usbinfo.nodev))
  1196. return ret;
  1197. switch(info->ident) {
  1198. case IDENT_BF1:
  1199. ret = bf1_scan(thr, bitfury, info);
  1200. break;
  1201. case IDENT_BXF:
  1202. case IDENT_OSM:
  1203. ret = bxf_scan(bitfury, info);
  1204. break;
  1205. case IDENT_NFU:
  1206. case IDENT_BXM:
  1207. ret = nfu_scan(thr, bitfury, info);
  1208. break;
  1209. default:
  1210. ret = 0;
  1211. break;
  1212. }
  1213. if (ret > 0)
  1214. info->failing = false;
  1215. return ret;
  1216. }
  1217. static void bxf_send_maxroll(struct cgpu_info *bitfury, int maxroll)
  1218. {
  1219. char buf[20];
  1220. sprintf(buf, "maxroll %d\n", maxroll);
  1221. bxf_send_msg(bitfury, buf, C_BXF_MAXROLL);
  1222. }
  1223. static bool bxf_send_work(struct cgpu_info *bitfury, struct work *work)
  1224. {
  1225. char buf[512], hexwork[156];
  1226. __bin2hex(hexwork, work->data, 76);
  1227. sprintf(buf, "work %s %x\n", hexwork, work->subid);
  1228. return bxf_send_msg(bitfury, buf, C_BXF_WORK);
  1229. }
  1230. static void bxf_update_work(struct cgpu_info *bitfury, struct bitfury_info *info)
  1231. {
  1232. struct thr_info *thr = info->thr;
  1233. struct work *work;
  1234. if (unlikely(bitfury->usbinfo.nodev))
  1235. return;
  1236. work = get_queue_work(thr, bitfury, thr->id);
  1237. if (work->drv_rolllimit != info->maxroll) {
  1238. info->maxroll = work->drv_rolllimit;
  1239. bxf_send_maxroll(bitfury, info->maxroll);
  1240. }
  1241. mutex_lock(&info->lock);
  1242. work->subid = ++info->work_id;
  1243. mutex_unlock(&info->lock);
  1244. cgtime(&work->tv_work_start);
  1245. bxf_send_work(bitfury, work);
  1246. }
  1247. static void bitfury_flush_work(struct cgpu_info *bitfury)
  1248. {
  1249. struct bitfury_info *info = bitfury->device_data;
  1250. switch(info->ident) {
  1251. case IDENT_BXF:
  1252. case IDENT_OSM:
  1253. bxf_send_flush(bitfury);
  1254. bxf_update_work(bitfury, info);
  1255. bxf_update_work(bitfury, info);
  1256. case IDENT_BF1:
  1257. default:
  1258. break;
  1259. }
  1260. }
  1261. static void bitfury_update_work(struct cgpu_info *bitfury)
  1262. {
  1263. struct bitfury_info *info = bitfury->device_data;
  1264. switch(info->ident) {
  1265. case IDENT_BXF:
  1266. case IDENT_OSM:
  1267. bxf_update_work(bitfury, info);
  1268. case IDENT_BF1:
  1269. default:
  1270. break;
  1271. }
  1272. }
  1273. static struct api_data *bf1_api_stats(struct bitfury_info *info)
  1274. {
  1275. struct api_data *root = NULL;
  1276. double nonce_rate;
  1277. char serial[16];
  1278. int version;
  1279. version = info->version;
  1280. root = api_add_int(root, "Version", &version, true);
  1281. root = api_add_string(root, "Product", info->product, false);
  1282. sprintf(serial, "%08x", info->serial);
  1283. root = api_add_string(root, "Serial", serial, true);
  1284. nonce_rate = (double)info->total_nonces / (double)info->cycles;
  1285. root = api_add_double(root, "NonceRate", &nonce_rate, true);
  1286. return root;
  1287. }
  1288. static struct api_data *bxf_api_stats(struct cgpu_info *bitfury, struct bitfury_info *info)
  1289. {
  1290. struct api_data *root = NULL;
  1291. double nonce_rate;
  1292. char buf[32];
  1293. int i;
  1294. sprintf(buf, "%d.%d", info->ver_major, info->ver_minor);
  1295. root = api_add_string(root, "Version", buf, true);
  1296. root = api_add_int(root, "Revision", &info->hw_rev, false);
  1297. root = api_add_int(root, "Chips", &info->chips, false);
  1298. nonce_rate = (double)info->total_nonces / (double)info->cycles;
  1299. root = api_add_double(root, "NonceRate", &nonce_rate, true);
  1300. root = api_add_int(root, "NoMatchingWork", &info->no_matching_work, false);
  1301. root = api_add_double(root, "Temperature", &bitfury->temp, false);
  1302. root = api_add_int(root, "Max DeciTemp", &info->max_decitemp, false);
  1303. root = api_add_uint8(root, "Clock", &info->clocks, false);
  1304. for (i = 0; i < info->chips; i++) {
  1305. sprintf(buf, "Core%d hwerror", i);
  1306. root = api_add_int(root, buf, &info->filtered_hw[i], false);
  1307. sprintf(buf, "Core%d jobs", i);
  1308. root = api_add_int(root, buf, &info->job[i], false);
  1309. sprintf(buf, "Core%d submits", i);
  1310. root = api_add_int(root, buf, &info->submits[i], false);
  1311. }
  1312. return root;
  1313. }
  1314. static struct api_data *nfu_api_stats(struct bitfury_info *info)
  1315. {
  1316. struct api_data *root = NULL;
  1317. char buf[32];
  1318. int i;
  1319. root = api_add_int(root, "Chips", &info->chips, false);
  1320. for (i = 0; i < info->chips; i++) {
  1321. sprintf(buf, "Core%d submits", i);
  1322. root = api_add_int(root, buf, &info->submits[i], false);
  1323. }
  1324. return root;
  1325. }
  1326. static struct api_data *bitfury_api_stats(struct cgpu_info *cgpu)
  1327. {
  1328. struct bitfury_info *info = cgpu->device_data;
  1329. switch(info->ident) {
  1330. case IDENT_BF1:
  1331. return bf1_api_stats(info);
  1332. break;
  1333. case IDENT_BXF:
  1334. case IDENT_OSM:
  1335. return bxf_api_stats(cgpu, info);
  1336. break;
  1337. case IDENT_NFU:
  1338. case IDENT_BXM:
  1339. return nfu_api_stats(info);
  1340. break;
  1341. default:
  1342. break;
  1343. }
  1344. return NULL;
  1345. }
  1346. static void bitfury_get_statline_before(char *buf, size_t bufsiz, struct cgpu_info *cgpu)
  1347. {
  1348. struct bitfury_info *info = cgpu->device_data;
  1349. switch(info->ident) {
  1350. case IDENT_BXF:
  1351. case IDENT_OSM:
  1352. tailsprintf(buf, bufsiz, "%5.1fC", cgpu->temp);
  1353. break;
  1354. default:
  1355. break;
  1356. }
  1357. }
  1358. static void bf1_init(struct cgpu_info *bitfury)
  1359. {
  1360. bf1_close(bitfury);
  1361. bf1_open(bitfury);
  1362. bf1_reset(bitfury);
  1363. }
  1364. static void bitfury_init(struct cgpu_info *bitfury)
  1365. {
  1366. struct bitfury_info *info = bitfury->device_data;
  1367. switch(info->ident) {
  1368. case IDENT_BF1:
  1369. bf1_init(bitfury);
  1370. break;
  1371. default:
  1372. break;
  1373. }
  1374. }
  1375. static void bxf_close(struct bitfury_info *info)
  1376. {
  1377. pthread_join(info->read_thr, NULL);
  1378. mutex_destroy(&info->lock);
  1379. }
  1380. static void bitfury_shutdown(struct thr_info *thr)
  1381. {
  1382. struct cgpu_info *bitfury = thr->cgpu;
  1383. struct bitfury_info *info = bitfury->device_data;
  1384. switch(info->ident) {
  1385. case IDENT_BF1:
  1386. bf1_close(bitfury);
  1387. break;
  1388. case IDENT_BXF:
  1389. case IDENT_OSM:
  1390. bxf_close(info);
  1391. break;
  1392. case IDENT_NFU:
  1393. nfu_close(bitfury);
  1394. break;
  1395. case IDENT_BXM:
  1396. bxm_close(bitfury, info);
  1397. break;
  1398. default:
  1399. break;
  1400. }
  1401. usb_nodev(bitfury);
  1402. }
  1403. /* Currently hardcoded to BF1 devices */
  1404. struct device_drv bitfury_drv = {
  1405. .drv_id = DRIVER_bitfury,
  1406. .dname = "bitfury",
  1407. .name = "BF1",
  1408. .drv_detect = bitfury_detect,
  1409. .thread_prepare = bitfury_prepare,
  1410. .hash_work = &hash_driver_work,
  1411. .scanwork = bitfury_scanwork,
  1412. .flush_work = bitfury_flush_work,
  1413. .update_work = bitfury_update_work,
  1414. .get_api_stats = bitfury_api_stats,
  1415. .get_statline_before = bitfury_get_statline_before,
  1416. .reinit_device = bitfury_init,
  1417. .thread_shutdown = bitfury_shutdown,
  1418. .identify_device = bitfury_identify
  1419. };