driver-bitfury.c 43 KB

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