driver-bflsc.c.bak 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362
  1. /*
  2. * Copyright 2013 Andrew Smith
  3. * Copyright 2013-2015 Con Kolivas
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #include <float.h>
  12. #include <limits.h>
  13. #include <pthread.h>
  14. #include <stdint.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <strings.h>
  18. #include <sys/time.h>
  19. #include <unistd.h>
  20. #ifdef WIN32
  21. #include <windows.h>
  22. #endif
  23. #include "compat.h"
  24. #include "miner.h"
  25. #include "usbutils.h"
  26. #include "uthash.h"
  27. #include "driver-bflsc.h"
  28. int opt_bflsc_overheat = BFLSC_TEMP_OVERHEAT;
  29. static const char *blank = "";
  30. static enum driver_version drv_ver(struct cgpu_info *bflsc, const char *ver)
  31. {
  32. char *tmp;
  33. if (strstr(ver, "1.0.0"))
  34. return BFLSC_DRV1;
  35. if (strstr(ver, "1.0.") || strstr(ver, "1.1.")) {
  36. applog(LOG_WARNING, "%s detect (%s) Warning assuming firmware '%s' is Ver1",
  37. bflsc->drv->dname, bflsc->device_path, ver);
  38. return BFLSC_DRV1;
  39. }
  40. if (strstr(ver, "1.2."))
  41. return BFLSC_DRV2;
  42. tmp = str_text((char *)ver);
  43. applog(LOG_INFO, "%s detect (%s) Warning unknown firmware '%s' using Ver2",
  44. bflsc->drv->dname, bflsc->device_path, tmp);
  45. free(tmp);
  46. return BFLSC_DRV2;
  47. }
  48. static void xlinkstr(char *xlink, size_t siz, int dev, struct bflsc_info *sc_info)
  49. {
  50. if (dev > 0)
  51. snprintf(xlink, siz, " x-%d", dev);
  52. else {
  53. if (sc_info->sc_count > 1)
  54. strcpy(xlink, " master");
  55. else
  56. *xlink = '\0';
  57. }
  58. }
  59. static void bflsc_applog(struct cgpu_info *bflsc, int dev, enum usb_cmds cmd, int amount, int err)
  60. {
  61. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  62. char xlink[17];
  63. xlinkstr(xlink, sizeof(xlink), dev, sc_info);
  64. usb_applog(bflsc, cmd, xlink, amount, err);
  65. }
  66. // Break an input up into lines with LFs removed
  67. // false means an error, but if *lines > 0 then data was also found
  68. // error would be no data or missing LF at the end
  69. static bool tolines(struct cgpu_info *bflsc, int dev, char *buf, int *lines, char ***items, enum usb_cmds cmd)
  70. {
  71. bool ok = false;
  72. char *ptr;
  73. #define p_lines (*lines)
  74. #define p_items (*items)
  75. p_lines = 0;
  76. p_items = NULL;
  77. if (!buf || !(*buf)) {
  78. applog(LOG_DEBUG, "USB: %s%i: (%d) empty %s",
  79. bflsc->drv->name, bflsc->device_id, dev, usb_cmdname(cmd));
  80. return ok;
  81. }
  82. ptr = strdup(buf);
  83. while (ptr && *ptr) {
  84. p_items = cgrealloc(p_items, ++p_lines * sizeof(*p_items));
  85. p_items[p_lines-1] = ptr;
  86. ptr = strchr(ptr, '\n');
  87. if (ptr)
  88. *(ptr++) = '\0';
  89. else {
  90. applog(LOG_DEBUG, "USB: %s%i: (%d) missing lf(s) in %s",
  91. bflsc->drv->name, bflsc->device_id, dev, usb_cmdname(cmd));
  92. return ok;
  93. }
  94. }
  95. ok = true;
  96. return ok;
  97. }
  98. static void freetolines(int *lines, char ***items)
  99. {
  100. if (*lines > 0) {
  101. free(**items);
  102. free(*items);
  103. }
  104. *lines = 0;
  105. *items = NULL;
  106. }
  107. enum breakmode {
  108. NOCOLON,
  109. ONECOLON,
  110. ALLCOLON // Temperature uses this
  111. };
  112. // Break down a single line into 'fields'
  113. // 'lf' will be a pointer to the final LF if it is there (or NULL)
  114. // firstname will be the allocated buf copy pointer which is also
  115. // the string before ':' for ONECOLON and ALLCOLON
  116. // If any string is missing the ':' when it was expected, false is returned
  117. static bool breakdown(enum breakmode mode, char *buf, int *count, char **firstname, char ***fields, char **lf)
  118. {
  119. char *ptr, *colon, *comma;
  120. bool ok = false;
  121. #define p_count (*count)
  122. #define p_firstname (*firstname)
  123. #define p_fields (*fields)
  124. #define p_lf (*lf)
  125. p_count = 0;
  126. p_firstname = NULL;
  127. p_fields = NULL;
  128. p_lf = NULL;
  129. if (!buf || !(*buf))
  130. return ok;
  131. ptr = p_firstname = strdup(buf);
  132. p_lf = strchr(p_firstname, '\n');
  133. if (mode == ONECOLON) {
  134. colon = strchr(ptr, ':');
  135. if (colon) {
  136. ptr = colon;
  137. *(ptr++) = '\0';
  138. } else
  139. return ok;
  140. }
  141. while (ptr && *ptr) {
  142. if (mode == ALLCOLON) {
  143. colon = strchr(ptr, ':');
  144. if (colon)
  145. ptr = colon + 1;
  146. else
  147. return ok;
  148. }
  149. comma = strchr(ptr, ',');
  150. if (comma)
  151. *(comma++) = '\0';
  152. p_fields = cgrealloc(p_fields, ++p_count * sizeof(*p_fields));
  153. p_fields[p_count-1] = ptr;
  154. ptr = comma;
  155. }
  156. ok = true;
  157. return ok;
  158. }
  159. static void freebreakdown(int *count, char **firstname, char ***fields)
  160. {
  161. if (*firstname)
  162. free(*firstname);
  163. if (*count > 0)
  164. free(*fields);
  165. *count = 0;
  166. *firstname = NULL;
  167. *fields = NULL;
  168. }
  169. static bool isokerr(int err, char *buf, int amount)
  170. {
  171. if (err < 0 || amount < (int)BFLSC_OK_LEN)
  172. return false;
  173. else {
  174. if (strstr(buf, BFLSC_ANERR)) {
  175. applog(LOG_INFO, "BFLSC not ok err: %s", buf);
  176. return false;
  177. } else
  178. return true;
  179. }
  180. }
  181. // send+receive dual stage - always single line replies
  182. static int send_recv_ds(struct cgpu_info *bflsc, int dev, int *stage, bool *sent, int *amount, char *send1, int send1_len, enum usb_cmds send1_cmd, enum usb_cmds recv1_cmd, char *send2, int send2_len, enum usb_cmds send2_cmd, enum usb_cmds recv2_cmd, char *recv, int recv_siz)
  183. {
  184. struct DataForwardToChain data;
  185. int len, err, tried;
  186. if (dev == 0) {
  187. usb_buffer_clear(bflsc);
  188. *stage = 1;
  189. *sent = false;
  190. err = usb_write(bflsc, send1, send1_len, amount, send1_cmd);
  191. if (err < 0 || *amount < send1_len)
  192. return err;
  193. *sent = true;
  194. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv1_cmd);
  195. if (!isokerr(err, recv, *amount))
  196. return err;
  197. usb_buffer_clear(bflsc);
  198. *stage = 2;
  199. *sent = false;
  200. err = usb_write(bflsc, send2, send2_len, amount, send2_cmd);
  201. if (err < 0 || *amount < send2_len)
  202. return err;
  203. *sent = true;
  204. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv2_cmd);
  205. return err;
  206. }
  207. data.header = BFLSC_XLINKHDR;
  208. data.deviceAddress = (uint8_t)dev;
  209. tried = 0;
  210. while (tried++ < 3) {
  211. data.payloadSize = send1_len;
  212. memcpy(data.payloadData, send1, send1_len);
  213. len = DATAFORWARDSIZE(data);
  214. usb_buffer_clear(bflsc);
  215. *stage = 1;
  216. *sent = false;
  217. err = usb_write(bflsc, (char *)&data, len, amount, send1_cmd);
  218. if (err < 0 || *amount < send1_len)
  219. return err;
  220. *sent = true;
  221. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv1_cmd);
  222. if (err != LIBUSB_SUCCESS)
  223. return err;
  224. // x-link timeout? - try again?
  225. if (strstr(recv, BFLSC_XTIMEOUT))
  226. continue;
  227. if (!isokerr(err, recv, *amount))
  228. return err;
  229. data.payloadSize = send2_len;
  230. memcpy(data.payloadData, send2, send2_len);
  231. len = DATAFORWARDSIZE(data);
  232. usb_buffer_clear(bflsc);
  233. *stage = 2;
  234. *sent = false;
  235. err = usb_write(bflsc, (char *)&data, len, amount, send2_cmd);
  236. if (err < 0 || *amount < send2_len)
  237. return err;
  238. *sent = true;
  239. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv2_cmd);
  240. if (err != LIBUSB_SUCCESS)
  241. return err;
  242. // x-link timeout? - try again?
  243. if (strstr(recv, BFLSC_XTIMEOUT))
  244. continue;
  245. // SUCCESS - return it
  246. break;
  247. }
  248. return err;
  249. }
  250. #define READ_OK true
  251. #define READ_NL false
  252. // send+receive single stage
  253. static int send_recv_ss(struct cgpu_info *bflsc, int dev, bool *sent, int *amount, char *send, int send_len, enum usb_cmds send_cmd, char *recv, int recv_siz, enum usb_cmds recv_cmd, bool read_ok)
  254. {
  255. struct DataForwardToChain data;
  256. int len, err, tried;
  257. if (dev == 0) {
  258. usb_buffer_clear(bflsc);
  259. *sent = false;
  260. err = usb_write(bflsc, send, send_len, amount, send_cmd);
  261. if (err < 0 || *amount < send_len) {
  262. // N.B. thus !(*sent) directly implies err < 0 or *amount < send_len
  263. return err;
  264. }
  265. *sent = true;
  266. if (read_ok == READ_OK)
  267. err = usb_read_ok(bflsc, recv, recv_siz, amount, recv_cmd);
  268. else
  269. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv_cmd);
  270. return err;
  271. }
  272. data.header = BFLSC_XLINKHDR;
  273. data.deviceAddress = (uint8_t)dev;
  274. data.payloadSize = send_len;
  275. memcpy(data.payloadData, send, send_len);
  276. len = DATAFORWARDSIZE(data);
  277. tried = 0;
  278. while (tried++ < 3) {
  279. usb_buffer_clear(bflsc);
  280. *sent = false;
  281. err = usb_write(bflsc, (char *)&data, len, amount, recv_cmd);
  282. if (err < 0 || *amount < send_len)
  283. return err;
  284. *sent = true;
  285. if (read_ok == READ_OK)
  286. err = usb_read_ok(bflsc, recv, recv_siz, amount, recv_cmd);
  287. else
  288. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv_cmd);
  289. if (err != LIBUSB_SUCCESS && err != LIBUSB_ERROR_TIMEOUT)
  290. return err;
  291. // read_ok can err timeout if it's looking for OK<LF>
  292. // TODO: add a usb_read() option to spot the ERR: and convert end=OK<LF> to just <LF>
  293. // x-link timeout? - try again?
  294. if ((err == LIBUSB_SUCCESS || (read_ok == READ_OK && err == LIBUSB_ERROR_TIMEOUT)) &&
  295. strstr(recv, BFLSC_XTIMEOUT))
  296. continue;
  297. // SUCCESS or TIMEOUT - return it
  298. break;
  299. }
  300. return err;
  301. }
  302. static int write_to_dev(struct cgpu_info *bflsc, int dev, char *buf, int buflen, int *amount, enum usb_cmds cmd)
  303. {
  304. struct DataForwardToChain data;
  305. int len;
  306. /*
  307. * The protocol is syncronous so any previous excess can be
  308. * discarded and assumed corrupt data or failed USB transfers
  309. */
  310. usb_buffer_clear(bflsc);
  311. if (dev == 0)
  312. return usb_write(bflsc, buf, buflen, amount, cmd);
  313. data.header = BFLSC_XLINKHDR;
  314. data.deviceAddress = (uint8_t)dev;
  315. data.payloadSize = buflen;
  316. memcpy(data.payloadData, buf, buflen);
  317. len = DATAFORWARDSIZE(data);
  318. return usb_write(bflsc, (char *)&data, len, amount, cmd);
  319. }
  320. static void bflsc_send_flush_work(struct cgpu_info *bflsc, int dev)
  321. {
  322. char buf[BFLSC_BUFSIZ+1];
  323. int err, amount;
  324. bool sent;
  325. // Device is gone
  326. if (bflsc->usbinfo.nodev)
  327. return;
  328. mutex_lock(&bflsc->device_mutex);
  329. err = send_recv_ss(bflsc, dev, &sent, &amount,
  330. BFLSC_QFLUSH, BFLSC_QFLUSH_LEN, C_QUEFLUSH,
  331. buf, sizeof(buf)-1, C_QUEFLUSHREPLY, READ_NL);
  332. mutex_unlock(&bflsc->device_mutex);
  333. if (!sent)
  334. bflsc_applog(bflsc, dev, C_QUEFLUSH, amount, err);
  335. else {
  336. // TODO: do we care if we don't get 'OK'? (always will in normal processing)
  337. }
  338. }
  339. /* return True = attempted usb_read_ok()
  340. * set ignore to true means no applog/ignore errors */
  341. static bool bflsc_qres(struct cgpu_info *bflsc, char *buf, size_t bufsiz, int dev, int *err, int *amount, bool ignore)
  342. {
  343. bool readok = false;
  344. mutex_lock(&(bflsc->device_mutex));
  345. *err = send_recv_ss(bflsc, dev, &readok, amount,
  346. BFLSC_QRES, BFLSC_QRES_LEN, C_REQUESTRESULTS,
  347. buf, bufsiz-1, C_GETRESULTS, READ_OK);
  348. mutex_unlock(&(bflsc->device_mutex));
  349. if (!readok) {
  350. if (!ignore)
  351. bflsc_applog(bflsc, dev, C_REQUESTRESULTS, *amount, *err);
  352. // TODO: do what? flag as dead device?
  353. // count how many times it has happened and reset/fail it
  354. // or even make sure it is all x-link and that means device
  355. // has failed after some limit of this?
  356. // of course all other I/O must also be failing ...
  357. } else {
  358. if (*err < 0 || *amount < 1) {
  359. if (!ignore)
  360. bflsc_applog(bflsc, dev, C_GETRESULTS, *amount, *err);
  361. // TODO: do what? ... see above
  362. }
  363. }
  364. return readok;
  365. }
  366. static void __bflsc_initialise(struct cgpu_info *bflsc)
  367. {
  368. int err, interface;
  369. // TODO: does x-link bypass the other device FTDI? (I think it does)
  370. // So no initialisation required except for the master device?
  371. if (bflsc->usbinfo.nodev)
  372. return;
  373. interface = usb_interface(bflsc);
  374. // Reset
  375. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  376. FTDI_VALUE_RESET, interface, C_RESET);
  377. applog(LOG_DEBUG, "%s%i: reset got err %d",
  378. bflsc->drv->name, bflsc->device_id, err);
  379. if (bflsc->usbinfo.nodev)
  380. return;
  381. usb_ftdi_set_latency(bflsc);
  382. if (bflsc->usbinfo.nodev)
  383. return;
  384. // Set data control
  385. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_DATA,
  386. FTDI_VALUE_DATA_BAS, interface, C_SETDATA);
  387. applog(LOG_DEBUG, "%s%i: setdata got err %d",
  388. bflsc->drv->name, bflsc->device_id, err);
  389. if (bflsc->usbinfo.nodev)
  390. return;
  391. // Set the baud
  392. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD_BAS,
  393. (FTDI_INDEX_BAUD_BAS & 0xff00) | interface,
  394. C_SETBAUD);
  395. applog(LOG_DEBUG, "%s%i: setbaud got err %d",
  396. bflsc->drv->name, bflsc->device_id, err);
  397. if (bflsc->usbinfo.nodev)
  398. return;
  399. // Set Flow Control
  400. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
  401. FTDI_VALUE_FLOW, interface, C_SETFLOW);
  402. applog(LOG_DEBUG, "%s%i: setflowctrl got err %d",
  403. bflsc->drv->name, bflsc->device_id, err);
  404. if (bflsc->usbinfo.nodev)
  405. return;
  406. // Set Modem Control
  407. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
  408. FTDI_VALUE_MODEM, interface, C_SETMODEM);
  409. applog(LOG_DEBUG, "%s%i: setmodemctrl got err %d",
  410. bflsc->drv->name, bflsc->device_id, err);
  411. if (bflsc->usbinfo.nodev)
  412. return;
  413. // Clear any sent data
  414. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  415. FTDI_VALUE_PURGE_TX, interface, C_PURGETX);
  416. applog(LOG_DEBUG, "%s%i: purgetx got err %d",
  417. bflsc->drv->name, bflsc->device_id, err);
  418. if (bflsc->usbinfo.nodev)
  419. return;
  420. // Clear any received data
  421. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  422. FTDI_VALUE_PURGE_RX, interface, C_PURGERX);
  423. applog(LOG_DEBUG, "%s%i: purgerx got err %d",
  424. bflsc->drv->name, bflsc->device_id, err);
  425. if (!bflsc->cutofftemp)
  426. bflsc->cutofftemp = opt_bflsc_overheat;
  427. }
  428. static void bflsc_initialise(struct cgpu_info *bflsc)
  429. {
  430. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  431. char buf[BFLSC_BUFSIZ+1];
  432. int err, amount;
  433. int dev;
  434. mutex_lock(&(bflsc->device_mutex));
  435. __bflsc_initialise(bflsc);
  436. mutex_unlock(&(bflsc->device_mutex));
  437. for (dev = 0; dev < sc_info->sc_count; dev++) {
  438. bflsc_send_flush_work(bflsc, dev);
  439. bflsc_qres(bflsc, buf, sizeof(buf), dev, &err, &amount, true);
  440. }
  441. }
  442. static bool getinfo(struct cgpu_info *bflsc, int dev)
  443. {
  444. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  445. struct bflsc_dev sc_dev;
  446. char buf[BFLSC_BUFSIZ+1];
  447. int err, amount;
  448. char **items, *firstname, **fields, *lf;
  449. bool res, ok = false;
  450. int i, lines, count;
  451. char *tmp;
  452. /*
  453. * Kano's first dev Jalapeno output:
  454. * DEVICE: BitFORCE SC<LF>
  455. * FIRMWARE: 1.0.0<LF>
  456. * ENGINES: 30<LF>
  457. * FREQUENCY: [UNKNOWN]<LF>
  458. * XLINK MODE: MASTER<LF>
  459. * XLINK PRESENT: YES<LF>
  460. * --DEVICES IN CHAIN: 0<LF>
  461. * --CHAIN PRESENCE MASK: 00000000<LF>
  462. * OK<LF>
  463. */
  464. /*
  465. * Don't use send_recv_ss() since we have a different receive timeout
  466. * Also getinfo() is called multiple times if it fails anyway
  467. */
  468. err = write_to_dev(bflsc, dev, BFLSC_DETAILS, BFLSC_DETAILS_LEN, &amount, C_REQUESTDETAILS);
  469. if (err < 0 || amount != BFLSC_DETAILS_LEN) {
  470. applog(LOG_ERR, "%s detect (%s) send details request failed (%d:%d)",
  471. bflsc->drv->dname, bflsc->device_path, amount, err);
  472. return ok;
  473. }
  474. err = usb_read_ok_timeout(bflsc, buf, sizeof(buf)-1, &amount,
  475. BFLSC_INFO_TIMEOUT, C_GETDETAILS);
  476. if (err < 0 || amount < 1) {
  477. if (err < 0) {
  478. applog(LOG_ERR, "%s detect (%s) get details return invalid/timed out (%d:%d)",
  479. bflsc->drv->dname, bflsc->device_path, amount, err);
  480. } else {
  481. applog(LOG_ERR, "%s detect (%s) get details returned nothing (%d:%d)",
  482. bflsc->drv->dname, bflsc->device_path, amount, err);
  483. }
  484. return ok;
  485. }
  486. memset(&sc_dev, 0, sizeof(struct bflsc_dev));
  487. sc_info->sc_count = 1;
  488. res = tolines(bflsc, dev, &(buf[0]), &lines, &items, C_GETDETAILS);
  489. if (!res)
  490. return ok;
  491. tmp = str_text(buf);
  492. strncpy(sc_dev.getinfo, tmp, sizeof(sc_dev.getinfo));
  493. sc_dev.getinfo[sizeof(sc_dev.getinfo)-1] = '\0';
  494. free(tmp);
  495. for (i = 0; i < lines-2; i++) {
  496. res = breakdown(ONECOLON, items[i], &count, &firstname, &fields, &lf);
  497. if (lf)
  498. *lf = '\0';
  499. if (!res || count != 1) {
  500. tmp = str_text(items[i]);
  501. applogsiz(LOG_WARNING, BFLSC_APPLOGSIZ,
  502. "%s detect (%s) invalid details line: '%s' %d",
  503. bflsc->drv->dname, bflsc->device_path, tmp, count);
  504. free(tmp);
  505. dev_error(bflsc, REASON_DEV_COMMS_ERROR);
  506. goto mata;
  507. }
  508. if (strstr(firstname, BFLSC_DI_FIRMWARE)) {
  509. sc_dev.firmware = strdup(fields[0]);
  510. sc_info->driver_version = drv_ver(bflsc, sc_dev.firmware);
  511. }
  512. else if (Strcasestr(firstname, BFLSC_DI_ENGINES)) {
  513. sc_dev.engines = atoi(fields[0]);
  514. if (sc_dev.engines < 1) {
  515. tmp = str_text(items[i]);
  516. applogsiz(LOG_WARNING, BFLSC_APPLOGSIZ,
  517. "%s detect (%s) invalid engine count: '%s'",
  518. bflsc->drv->dname, bflsc->device_path, tmp);
  519. free(tmp);
  520. goto mata;
  521. }
  522. }
  523. else if (strstr(firstname, BFLSC_DI_XLINKMODE))
  524. sc_dev.xlink_mode = strdup(fields[0]);
  525. else if (strstr(firstname, BFLSC_DI_XLINKPRESENT))
  526. sc_dev.xlink_present = strdup(fields[0]);
  527. else if (strstr(firstname, BFLSC_DI_DEVICESINCHAIN)) {
  528. if (fields[0][0] == '0' ||
  529. (fields[0][0] == ' ' && fields[0][1] == '0'))
  530. sc_info->sc_count = 1;
  531. else
  532. sc_info->sc_count = atoi(fields[0]);
  533. if (sc_info->sc_count < 1 || sc_info->sc_count > 30) {
  534. tmp = str_text(items[i]);
  535. applogsiz(LOG_WARNING, BFLSC_APPLOGSIZ,
  536. "%s detect (%s) invalid x-link count: '%s'",
  537. bflsc->drv->dname, bflsc->device_path, tmp);
  538. free(tmp);
  539. goto mata;
  540. }
  541. }
  542. else if (strstr(firstname, BFLSC_DI_CHIPS))
  543. sc_dev.chips = strdup(fields[0]);
  544. else if (strstr(firstname, BFLSC28_DI_ASICS))
  545. sc_dev.chips = strdup(fields[0]);
  546. freebreakdown(&count, &firstname, &fields);
  547. }
  548. if (sc_info->driver_version == BFLSC_DRVUNDEF) {
  549. applog(LOG_WARNING, "%s detect (%s) missing %s",
  550. bflsc->drv->dname, bflsc->device_path, BFLSC_DI_FIRMWARE);
  551. goto ne;
  552. }
  553. sc_info->sc_devs = cgcalloc(sc_info->sc_count, sizeof(struct bflsc_dev));
  554. memcpy(&(sc_info->sc_devs[0]), &sc_dev, sizeof(sc_dev));
  555. // TODO: do we care about getting this info for the rest if > 0 x-link
  556. ok = true;
  557. goto ne;
  558. mata:
  559. freebreakdown(&count, &firstname, &fields);
  560. ok = false;
  561. ne:
  562. freetolines(&lines, &items);
  563. return ok;
  564. }
  565. static bool bflsc28_queue_full(struct cgpu_info *bflsc);
  566. static struct cgpu_info *bflsc_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  567. {
  568. struct bflsc_info *sc_info = NULL;
  569. char buf[BFLSC_BUFSIZ+1];
  570. int i, err, amount;
  571. struct timeval init_start, init_now;
  572. int init_sleep, init_count;
  573. bool ident_first, sent;
  574. char *newname;
  575. uint16_t latency;
  576. struct cgpu_info *bflsc = usb_alloc_cgpu(&bflsc_drv, 1);
  577. sc_info = cgcalloc(1, sizeof(*sc_info));
  578. // TODO: fix ... everywhere ...
  579. bflsc->device_data = (FILE *)sc_info;
  580. if (!usb_init(bflsc, dev, found))
  581. goto shin;
  582. // Allow 2 complete attempts if the 1st time returns an unrecognised reply
  583. ident_first = true;
  584. retry:
  585. init_count = 0;
  586. init_sleep = REINIT_TIME_FIRST_MS;
  587. cgtime(&init_start);
  588. reinit:
  589. __bflsc_initialise(bflsc);
  590. err = send_recv_ss(bflsc, 0, &sent, &amount,
  591. BFLSC_IDENTIFY, BFLSC_IDENTIFY_LEN, C_REQUESTIDENTIFY,
  592. buf, sizeof(buf)-1, C_GETIDENTIFY, READ_NL);
  593. if (!sent) {
  594. applog(LOG_ERR, "%s detect (%s) send identify request failed (%d:%d)",
  595. bflsc->drv->dname, bflsc->device_path, amount, err);
  596. goto unshin;
  597. }
  598. if (err < 0 || amount < 1) {
  599. init_count++;
  600. cgtime(&init_now);
  601. if (us_tdiff(&init_now, &init_start) <= REINIT_TIME_MAX) {
  602. if (init_count == 2) {
  603. applog(LOG_WARNING, "%s detect (%s) 2nd init failed (%d:%d) - retrying",
  604. bflsc->drv->dname, bflsc->device_path, amount, err);
  605. }
  606. cgsleep_ms(init_sleep);
  607. if ((init_sleep * 2) <= REINIT_TIME_MAX_MS)
  608. init_sleep *= 2;
  609. goto reinit;
  610. }
  611. if (init_count > 0)
  612. applog(LOG_WARNING, "%s detect (%s) init failed %d times %.2fs",
  613. bflsc->drv->dname, bflsc->device_path, init_count, tdiff(&init_now, &init_start));
  614. if (err < 0) {
  615. applog(LOG_ERR, "%s detect (%s) error identify reply (%d:%d)",
  616. bflsc->drv->dname, bflsc->device_path, amount, err);
  617. } else {
  618. applog(LOG_ERR, "%s detect (%s) empty identify reply (%d)",
  619. bflsc->drv->dname, bflsc->device_path, amount);
  620. }
  621. goto unshin;
  622. }
  623. buf[amount] = '\0';
  624. if (unlikely(!strstr(buf, BFLSC_BFLSC) && !strstr(buf, BFLSC_BFLSC28))) {
  625. applog(LOG_DEBUG, "%s detect (%s) found an FPGA '%s' ignoring",
  626. bflsc->drv->dname, bflsc->device_path, buf);
  627. goto unshin;
  628. }
  629. if (unlikely(strstr(buf, BFLSC_IDENTITY))) {
  630. if (ident_first) {
  631. applog(LOG_DEBUG, "%s detect (%s) didn't recognise '%s' trying again ...",
  632. bflsc->drv->dname, bflsc->device_path, buf);
  633. ident_first = false;
  634. goto retry;
  635. }
  636. applog(LOG_DEBUG, "%s detect (%s) didn't recognise '%s' on 2nd attempt",
  637. bflsc->drv->dname, bflsc->device_path, buf);
  638. goto unshin;
  639. }
  640. int tries = 0;
  641. while (7734) {
  642. if (getinfo(bflsc, 0))
  643. break;
  644. // N.B. we will get displayed errors each time it fails
  645. if (++tries > 2)
  646. goto unshin;
  647. cgsleep_ms(40);
  648. }
  649. switch (sc_info->driver_version) {
  650. case BFLSC_DRV1:
  651. sc_info->que_size = BFLSC_QUE_SIZE_V1;
  652. sc_info->que_full_enough = BFLSC_QUE_FULL_ENOUGH_V1;
  653. sc_info->que_watermark = BFLSC_QUE_WATERMARK_V1;
  654. sc_info->que_low = BFLSC_QUE_LOW_V1;
  655. sc_info->que_noncecount = QUE_NONCECOUNT_V1;
  656. sc_info->que_fld_min = QUE_FLD_MIN_V1;
  657. sc_info->que_fld_max = QUE_FLD_MAX_V1;
  658. // Only Jalapeno uses 1.0.0
  659. sc_info->flush_size = 1;
  660. break;
  661. case BFLSC_DRV2:
  662. case BFLSC_DRVUNDEF:
  663. default:
  664. sc_info->driver_version = BFLSC_DRV2;
  665. sc_info->que_size = BFLSC_QUE_SIZE_V2;
  666. sc_info->que_full_enough = BFLSC_QUE_FULL_ENOUGH_V2;
  667. sc_info->que_watermark = BFLSC_QUE_WATERMARK_V2;
  668. sc_info->que_low = BFLSC_QUE_LOW_V2;
  669. sc_info->que_noncecount = QUE_NONCECOUNT_V2;
  670. sc_info->que_fld_min = QUE_FLD_MIN_V2;
  671. sc_info->que_fld_max = QUE_FLD_MAX_V2;
  672. // TODO: this can be reduced to total chip count
  673. sc_info->flush_size = 16 * sc_info->sc_count;
  674. break;
  675. }
  676. // Set parallelization based on the getinfo() response if it is present
  677. if (sc_info->sc_devs[0].chips && strlen(sc_info->sc_devs[0].chips)) {
  678. if (strstr(sc_info->sc_devs[0].chips, BFLSC_DI_CHIPS_PARALLEL)) {
  679. sc_info->que_noncecount = QUE_NONCECOUNT_V2;
  680. sc_info->que_fld_min = QUE_FLD_MIN_V2;
  681. sc_info->que_fld_max = QUE_FLD_MAX_V2;
  682. } else {
  683. sc_info->que_noncecount = QUE_NONCECOUNT_V1;
  684. sc_info->que_fld_min = QUE_FLD_MIN_V1;
  685. sc_info->que_fld_max = QUE_FLD_MAX_V1;
  686. }
  687. }
  688. sc_info->scan_sleep_time = BAS_SCAN_TIME;
  689. sc_info->results_sleep_time = BFLSC_RES_TIME;
  690. sc_info->default_ms_work = (unsigned int)BAS_WORK_TIME;
  691. latency = BAS_LATENCY;
  692. /* When getinfo() "FREQUENCY: [UNKNOWN]" is fixed -
  693. * use 'freq * engines' to estimate.
  694. * Otherwise for now: */
  695. newname = NULL;
  696. if (sc_info->sc_count > 1) {
  697. newname = BFLSC_MINIRIG;
  698. sc_info->scan_sleep_time = BAM_SCAN_TIME;
  699. sc_info->default_ms_work = (unsigned int)BAM_WORK_TIME;
  700. bflsc->usbdev->ident = IDENT_BAM;
  701. latency = BAM_LATENCY;
  702. } else {
  703. if (sc_info->sc_devs[0].engines < 34) { // 16 * 2 + 2
  704. newname = BFLSC_JALAPENO;
  705. sc_info->scan_sleep_time = BAJ_SCAN_TIME;
  706. sc_info->default_ms_work = (unsigned int)BAJ_WORK_TIME;
  707. bflsc->usbdev->ident = IDENT_BAJ;
  708. latency = BAJ_LATENCY;
  709. } else if (sc_info->sc_devs[0].engines < 130) { // 16 * 8 + 2
  710. newname = BFLSC_LITTLESINGLE;
  711. sc_info->scan_sleep_time = BAL_SCAN_TIME;
  712. sc_info->default_ms_work = (unsigned int)BAL_WORK_TIME;
  713. bflsc->usbdev->ident = IDENT_BAL;
  714. latency = BAL_LATENCY;
  715. }
  716. }
  717. sc_info->ident = usb_ident(bflsc);
  718. if (sc_info->ident == IDENT_BMA) {
  719. bflsc->drv->queue_full = &bflsc28_queue_full;
  720. sc_info->scan_sleep_time = BMA_SCAN_TIME;
  721. sc_info->default_ms_work = (unsigned int)BMA_WORK_TIME;
  722. sc_info->results_sleep_time = BMA_RES_TIME;
  723. }
  724. if (latency != bflsc->usbdev->found->latency) {
  725. bflsc->usbdev->found->latency = latency;
  726. usb_ftdi_set_latency(bflsc);
  727. }
  728. for (i = 0; i < sc_info->sc_count; i++)
  729. sc_info->sc_devs[i].ms_work = sc_info->default_ms_work;
  730. if (newname) {
  731. if (!bflsc->drv->copy)
  732. bflsc->drv = copy_drv(bflsc->drv);
  733. bflsc->drv->name = newname;
  734. }
  735. // We have a real BFLSC!
  736. applog(LOG_DEBUG, "%s (%s) identified as: '%s'",
  737. bflsc->drv->dname, bflsc->device_path, bflsc->drv->name);
  738. if (!add_cgpu(bflsc))
  739. goto unshin;
  740. update_usb_stats(bflsc);
  741. mutex_init(&bflsc->device_mutex);
  742. rwlock_init(&sc_info->stat_lock);
  743. return bflsc;
  744. unshin:
  745. usb_uninit(bflsc);
  746. shin:
  747. free(bflsc->device_data);
  748. bflsc->device_data = NULL;
  749. if (bflsc->name != blank) {
  750. free(bflsc->name);
  751. bflsc->name = NULL;
  752. }
  753. bflsc = usb_free_cgpu(bflsc);
  754. return NULL;
  755. }
  756. static void bflsc_detect(bool __maybe_unused hotplug)
  757. {
  758. usb_detect(&bflsc_drv, bflsc_detect_one);
  759. }
  760. static void get_bflsc_statline_before(char *buf, size_t bufsiz, struct cgpu_info *bflsc)
  761. {
  762. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  763. float temp = 0;
  764. float vcc2 = 0;
  765. int i;
  766. rd_lock(&(sc_info->stat_lock));
  767. for (i = 0; i < sc_info->sc_count; i++) {
  768. if (sc_info->sc_devs[i].temp1 > temp)
  769. temp = sc_info->sc_devs[i].temp1;
  770. if (sc_info->sc_devs[i].temp2 > temp)
  771. temp = sc_info->sc_devs[i].temp2;
  772. if (sc_info->sc_devs[i].vcc2 > vcc2)
  773. vcc2 = sc_info->sc_devs[i].vcc2;
  774. }
  775. rd_unlock(&(sc_info->stat_lock));
  776. tailsprintf(buf, bufsiz, "max%3.0fC %4.2fV", temp, vcc2);
  777. }
  778. static void flush_one_dev(struct cgpu_info *bflsc, int dev)
  779. {
  780. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  781. struct work *work, *tmp;
  782. bool did = false;
  783. bflsc_send_flush_work(bflsc, dev);
  784. rd_lock(&bflsc->qlock);
  785. HASH_ITER(hh, bflsc->queued_work, work, tmp) {
  786. if (work->subid == dev) {
  787. // devflag is used to flag stale work
  788. work->devflag = true;
  789. did = true;
  790. }
  791. }
  792. rd_unlock(&bflsc->qlock);
  793. if (did) {
  794. wr_lock(&(sc_info->stat_lock));
  795. sc_info->sc_devs[dev].flushed = true;
  796. sc_info->sc_devs[dev].flush_id = sc_info->sc_devs[dev].result_id;
  797. sc_info->sc_devs[dev].work_queued = 0;
  798. wr_unlock(&(sc_info->stat_lock));
  799. }
  800. }
  801. static void bflsc_flush_work(struct cgpu_info *bflsc)
  802. {
  803. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  804. int dev;
  805. for (dev = 0; dev < sc_info->sc_count; dev++)
  806. flush_one_dev(bflsc, dev);
  807. }
  808. static void bflsc_set_volt(struct cgpu_info *bflsc, int dev)
  809. {
  810. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  811. char buf[BFLSC_BUFSIZ+1];
  812. char msg[16];
  813. int err, amount;
  814. bool sent;
  815. // Device is gone
  816. if (bflsc->usbinfo.nodev)
  817. return;
  818. snprintf(msg, sizeof(msg), "V%dX", sc_info->volt_next);
  819. mutex_lock(&bflsc->device_mutex);
  820. err = send_recv_ss(bflsc, dev, &sent, &amount,
  821. msg, strlen(msg), C_SETVOLT,
  822. buf, sizeof(buf)-1, C_REPLYSETVOLT, READ_NL);
  823. mutex_unlock(&(bflsc->device_mutex));
  824. if (!sent)
  825. bflsc_applog(bflsc, dev, C_SETVOLT, amount, err);
  826. else {
  827. // Don't care
  828. }
  829. sc_info->volt_next_stat = false;
  830. return;
  831. }
  832. static void bflsc_set_clock(struct cgpu_info *bflsc, int dev)
  833. {
  834. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  835. char buf[BFLSC_BUFSIZ+1];
  836. char msg[16];
  837. int err, amount;
  838. bool sent;
  839. // Device is gone
  840. if (bflsc->usbinfo.nodev)
  841. return;
  842. snprintf(msg, sizeof(msg), "F%XX", sc_info->clock_next);
  843. mutex_lock(&bflsc->device_mutex);
  844. err = send_recv_ss(bflsc, dev, &sent, &amount,
  845. msg, strlen(msg), C_SETCLOCK,
  846. buf, sizeof(buf)-1, C_REPLYSETCLOCK, READ_NL);
  847. mutex_unlock(&(bflsc->device_mutex));
  848. if (!sent)
  849. bflsc_applog(bflsc, dev, C_SETCLOCK, amount, err);
  850. else {
  851. // Don't care
  852. }
  853. sc_info->clock_next_stat = false;
  854. return;
  855. }
  856. static void bflsc_flash_led(struct cgpu_info *bflsc, int dev)
  857. {
  858. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  859. char buf[BFLSC_BUFSIZ+1];
  860. int err, amount;
  861. bool sent;
  862. // Device is gone
  863. if (bflsc->usbinfo.nodev)
  864. return;
  865. // It is not critical flashing the led so don't get stuck if we
  866. // can't grab the mutex now
  867. if (mutex_trylock(&bflsc->device_mutex))
  868. return;
  869. err = send_recv_ss(bflsc, dev, &sent, &amount,
  870. BFLSC_FLASH, BFLSC_FLASH_LEN, C_REQUESTFLASH,
  871. buf, sizeof(buf)-1, C_FLASHREPLY, READ_NL);
  872. mutex_unlock(&(bflsc->device_mutex));
  873. if (!sent)
  874. bflsc_applog(bflsc, dev, C_REQUESTFLASH, amount, err);
  875. else {
  876. // Don't care
  877. }
  878. // Once we've tried - don't do it until told to again
  879. // - even if it failed
  880. sc_info->flash_led = false;
  881. return;
  882. }
  883. /* Flush and stop all work if the device reaches the thermal cutoff temp, or
  884. * temporarily stop queueing work if it's in the throttling range. */
  885. static void bflsc_manage_temp(struct cgpu_info *bflsc, struct bflsc_dev *sc_dev,
  886. int dev, float temp)
  887. {
  888. bflsc->temp = temp;
  889. if (bflsc->cutofftemp > 0) {
  890. int cutoff = bflsc->cutofftemp;
  891. int throttle = cutoff - BFLSC_TEMP_THROTTLE;
  892. int recover = cutoff - BFLSC_TEMP_RECOVER;
  893. if (sc_dev->overheat) {
  894. if (temp < recover)
  895. sc_dev->overheat = false;
  896. } else if (temp > throttle) {
  897. sc_dev->overheat = true;
  898. if (temp > cutoff) {
  899. applog(LOG_WARNING, "%s%i: temp (%.1f) hit thermal cutoff limit %d, stopping work!",
  900. bflsc->drv->name, bflsc->device_id, temp, cutoff);
  901. dev_error(bflsc, REASON_DEV_THERMAL_CUTOFF);
  902. flush_one_dev(bflsc, dev);
  903. } else {
  904. applog(LOG_NOTICE, "%s%i: temp (%.1f) hit thermal throttle limit %d, throttling",
  905. bflsc->drv->name, bflsc->device_id, temp, throttle);
  906. }
  907. }
  908. }
  909. }
  910. static bool bflsc_get_temp(struct cgpu_info *bflsc, int dev)
  911. {
  912. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  913. struct bflsc_dev *sc_dev;
  914. char temp_buf[BFLSC_BUFSIZ+1];
  915. char volt_buf[BFLSC_BUFSIZ+1];
  916. char *tmp;
  917. int err, amount;
  918. char *firstname, **fields, *lf;
  919. char xlink[17];
  920. int count;
  921. bool res, sent;
  922. float temp, temp1, temp2;
  923. float vcc1, vcc2, vmain;
  924. // Device is gone
  925. if (bflsc->usbinfo.nodev)
  926. return false;
  927. if (dev >= sc_info->sc_count) {
  928. applog(LOG_ERR, "%s%i: temp invalid xlink device %d - limit %d",
  929. bflsc->drv->name, bflsc->device_id, dev, sc_info->sc_count - 1);
  930. return false;
  931. }
  932. if (sc_info->volt_next_stat || sc_info->clock_next_stat) {
  933. if (sc_info->volt_next_stat)
  934. bflsc_set_volt(bflsc, dev);
  935. if (sc_info->clock_next_stat)
  936. bflsc_set_clock(bflsc, dev);
  937. return true;
  938. }
  939. // Flash instead of Temp
  940. if (sc_info->flash_led) {
  941. bflsc_flash_led(bflsc, dev);
  942. return true;
  943. }
  944. xlinkstr(xlink, sizeof(xlink), dev, sc_info);
  945. /* It is not very critical getting temp so don't get stuck if we
  946. * can't grab the mutex here */
  947. if (mutex_trylock(&bflsc->device_mutex))
  948. return false;
  949. err = send_recv_ss(bflsc, dev, &sent, &amount,
  950. BFLSC_TEMPERATURE, BFLSC_TEMPERATURE_LEN, C_REQUESTTEMPERATURE,
  951. temp_buf, sizeof(temp_buf)-1, C_GETTEMPERATURE, READ_NL);
  952. mutex_unlock(&(bflsc->device_mutex));
  953. if (!sent) {
  954. applog(LOG_ERR, "%s%i: Error: Request%s temp invalid/timed out (%d:%d)",
  955. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  956. return false;
  957. } else {
  958. if (err < 0 || amount < 1) {
  959. if (err < 0) {
  960. applog(LOG_ERR, "%s%i: Error: Get%s temp return invalid/timed out (%d:%d)",
  961. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  962. } else {
  963. applog(LOG_ERR, "%s%i: Error: Get%s temp returned nothing (%d:%d)",
  964. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  965. }
  966. return false;
  967. }
  968. }
  969. // Ignore it if we can't get the V
  970. if (mutex_trylock(&bflsc->device_mutex))
  971. return false;
  972. err = send_recv_ss(bflsc, dev, &sent, &amount,
  973. BFLSC_VOLTAGE, BFLSC_VOLTAGE_LEN, C_REQUESTVOLTS,
  974. volt_buf, sizeof(volt_buf)-1, C_GETVOLTS, READ_NL);
  975. mutex_unlock(&(bflsc->device_mutex));
  976. if (!sent) {
  977. applog(LOG_ERR, "%s%i: Error: Request%s volts invalid/timed out (%d:%d)",
  978. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  979. return false;
  980. } else {
  981. if (err < 0 || amount < 1) {
  982. if (err < 0) {
  983. applog(LOG_ERR, "%s%i: Error: Get%s volt return invalid/timed out (%d:%d)",
  984. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  985. } else {
  986. applog(LOG_ERR, "%s%i: Error: Get%s volt returned nothing (%d:%d)",
  987. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  988. }
  989. return false;
  990. }
  991. }
  992. res = breakdown(ALLCOLON, temp_buf, &count, &firstname, &fields, &lf);
  993. if (lf)
  994. *lf = '\0';
  995. if (!res || count < 2 || !lf) {
  996. tmp = str_text(temp_buf);
  997. applog(LOG_WARNING, "%s%i: Invalid%s temp reply: '%s'",
  998. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  999. free(tmp);
  1000. freebreakdown(&count, &firstname, &fields);
  1001. dev_error(bflsc, REASON_DEV_COMMS_ERROR);
  1002. return false;
  1003. }
  1004. temp = temp1 = (float)atoi(fields[0]);
  1005. temp2 = (float)atoi(fields[1]);
  1006. freebreakdown(&count, &firstname, &fields);
  1007. res = breakdown(NOCOLON, volt_buf, &count, &firstname, &fields, &lf);
  1008. if (lf)
  1009. *lf = '\0';
  1010. if (!res || count != 3 || !lf) {
  1011. tmp = str_text(volt_buf);
  1012. applog(LOG_WARNING, "%s%i: Invalid%s volt reply: '%s'",
  1013. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1014. free(tmp);
  1015. freebreakdown(&count, &firstname, &fields);
  1016. dev_error(bflsc, REASON_DEV_COMMS_ERROR);
  1017. return false;
  1018. }
  1019. sc_dev = &sc_info->sc_devs[dev];
  1020. vcc1 = (float)atoi(fields[0]) / 1000.0;
  1021. vcc2 = (float)atoi(fields[1]) / 1000.0;
  1022. vmain = (float)atoi(fields[2]) / 1000.0;
  1023. freebreakdown(&count, &firstname, &fields);
  1024. if (vcc1 > 0 || vcc2 > 0 || vmain > 0) {
  1025. wr_lock(&(sc_info->stat_lock));
  1026. if (vcc1 > 0) {
  1027. if (unlikely(sc_dev->vcc1 == 0))
  1028. sc_dev->vcc1 = vcc1;
  1029. else {
  1030. sc_dev->vcc1 += vcc1 * 0.63;
  1031. sc_dev->vcc1 /= 1.63;
  1032. }
  1033. }
  1034. if (vcc2 > 0) {
  1035. if (unlikely(sc_dev->vcc2 == 0))
  1036. sc_dev->vcc2 = vcc2;
  1037. else {
  1038. sc_dev->vcc2 += vcc2 * 0.63;
  1039. sc_dev->vcc2 /= 1.63;
  1040. }
  1041. }
  1042. if (vmain > 0) {
  1043. if (unlikely(sc_dev->vmain == 0))
  1044. sc_dev->vmain = vmain;
  1045. else {
  1046. sc_dev->vmain += vmain * 0.63;
  1047. sc_dev->vmain /= 1.63;
  1048. }
  1049. }
  1050. wr_unlock(&(sc_info->stat_lock));
  1051. }
  1052. if (temp1 > 0 || temp2 > 0) {
  1053. wr_lock(&(sc_info->stat_lock));
  1054. if (unlikely(!sc_dev->temp1))
  1055. sc_dev->temp1 = temp1;
  1056. else {
  1057. sc_dev->temp1 += temp1 * 0.63;
  1058. sc_dev->temp1 /= 1.63;
  1059. }
  1060. if (unlikely(!sc_dev->temp2))
  1061. sc_dev->temp2 = temp2;
  1062. else {
  1063. sc_dev->temp2 += temp2 * 0.63;
  1064. sc_dev->temp2 /= 1.63;
  1065. }
  1066. if (temp1 > sc_dev->temp1_max) {
  1067. sc_dev->temp1_max = temp1;
  1068. sc_dev->temp1_max_time = time(NULL);
  1069. }
  1070. if (temp2 > sc_dev->temp2_max) {
  1071. sc_dev->temp2_max = temp2;
  1072. sc_dev->temp2_max_time = time(NULL);
  1073. }
  1074. if (unlikely(sc_dev->temp1_5min_av == 0))
  1075. sc_dev->temp1_5min_av = temp1;
  1076. else {
  1077. sc_dev->temp1_5min_av += temp1 * .0042;
  1078. sc_dev->temp1_5min_av /= 1.0042;
  1079. }
  1080. if (unlikely(sc_dev->temp2_5min_av == 0))
  1081. sc_dev->temp2_5min_av = temp2;
  1082. else {
  1083. sc_dev->temp2_5min_av += temp2 * .0042;
  1084. sc_dev->temp2_5min_av /= 1.0042;
  1085. }
  1086. wr_unlock(&(sc_info->stat_lock));
  1087. if (temp < temp2)
  1088. temp = temp2;
  1089. bflsc_manage_temp(bflsc, sc_dev, dev, temp);
  1090. }
  1091. return true;
  1092. }
  1093. static void inc_core_errors(struct bflsc_info *info, int8_t core)
  1094. {
  1095. if (info->ident == IDENT_BMA) {
  1096. if (core >= 0)
  1097. info->cortex_hw[core]++;
  1098. } else {
  1099. if (core >= 0 && core < 16)
  1100. info->core_hw[core]++;
  1101. }
  1102. }
  1103. static void inc_bflsc_errors(struct thr_info *thr, struct bflsc_info *info, int8_t core)
  1104. {
  1105. inc_hw_errors(thr);
  1106. inc_core_errors(info, core);
  1107. }
  1108. static void inc_bflsc_nonces(struct bflsc_info *info, int8_t core)
  1109. {
  1110. if (info->ident == IDENT_BMA) {
  1111. if (core >= 0)
  1112. info->cortex_nonces[core]++;
  1113. } else {
  1114. if (core >= 0 && core < 16)
  1115. info->core_nonces[core]++;
  1116. }
  1117. }
  1118. struct work *bflsc_work_by_uid(struct cgpu_info *bflsc, struct bflsc_info *sc_info, int id)
  1119. {
  1120. struct bflsc_work *bwork;
  1121. struct work *work = NULL;
  1122. wr_lock(&bflsc->qlock);
  1123. HASH_FIND_INT(sc_info->bworks, &id, bwork);
  1124. if (likely(bwork)) {
  1125. HASH_DEL(sc_info->bworks, bwork);
  1126. work = bwork->work;
  1127. free(bwork);
  1128. }
  1129. wr_unlock(&bflsc->qlock);
  1130. return work;
  1131. }
  1132. static void process_nonces(struct cgpu_info *bflsc, int dev, char *xlink, char *data, int count, char **fields, int *nonces)
  1133. {
  1134. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1135. struct thr_info *thr = bflsc->thr[0];
  1136. struct work *work = NULL;
  1137. int8_t core = -1;
  1138. uint32_t nonce;
  1139. int i, num, x;
  1140. char *tmp;
  1141. bool res;
  1142. if (count < sc_info->que_fld_min) {
  1143. tmp = str_text(data);
  1144. applogsiz(LOG_INFO, BFLSC_APPLOGSIZ,
  1145. "%s%i:%s work returned too small (%d,%s)",
  1146. bflsc->drv->name, bflsc->device_id, xlink, count, tmp);
  1147. free(tmp);
  1148. inc_bflsc_errors(thr, sc_info, core);
  1149. return;
  1150. }
  1151. if (sc_info->ident == IDENT_BMA) {
  1152. unsigned int ucore;
  1153. if (sscanf(fields[QUE_CC], "%x", &ucore) == 1)
  1154. core = ucore;
  1155. } else if (sc_info->que_noncecount != QUE_NONCECOUNT_V1) {
  1156. unsigned int ucore;
  1157. if (sscanf(fields[QUE_CHIP_V2], "%x", &ucore) == 1)
  1158. core = ucore;
  1159. }
  1160. if (count > sc_info->que_fld_max) {
  1161. applog(LOG_INFO, "%s%i:%s work returned too large (%d) processing %d anyway",
  1162. bflsc->drv->name, bflsc->device_id, xlink, count, sc_info->que_fld_max);
  1163. count = sc_info->que_fld_max;
  1164. inc_bflsc_errors(thr, sc_info, core);
  1165. }
  1166. num = atoi(fields[sc_info->que_noncecount]);
  1167. if (num != count - sc_info->que_fld_min) {
  1168. tmp = str_text(data);
  1169. applogsiz(LOG_INFO, BFLSC_APPLOGSIZ,
  1170. "%s%i:%s incorrect data count (%d) will use %d instead from (%s)",
  1171. bflsc->drv->name, bflsc->device_id, xlink, num,
  1172. count - sc_info->que_fld_max, tmp);
  1173. free(tmp);
  1174. inc_bflsc_errors(thr, sc_info, core);
  1175. }
  1176. if (sc_info->ident == IDENT_BMA) {
  1177. int uid;
  1178. if (sscanf(fields[QUE_UID], "%04x", &uid) == 1)
  1179. work = bflsc_work_by_uid(bflsc, sc_info, uid);
  1180. } else {
  1181. char midstate[MIDSTATE_BYTES] = {}, blockdata[MERKLE_BYTES] = {};
  1182. if (!hex2bin((unsigned char *)midstate, fields[QUE_MIDSTATE], MIDSTATE_BYTES) ||
  1183. !hex2bin((unsigned char *)blockdata, fields[QUE_BLOCKDATA], MERKLE_BYTES)) {
  1184. applog(LOG_INFO, "%s%i:%s Failed to convert binary data to hex result - ignored",
  1185. bflsc->drv->name, bflsc->device_id, xlink);
  1186. inc_bflsc_errors(thr, sc_info, core);
  1187. return;
  1188. }
  1189. work = take_queued_work_bymidstate(bflsc, midstate, MIDSTATE_BYTES,
  1190. blockdata, MERKLE_OFFSET, MERKLE_BYTES);
  1191. }
  1192. if (!work) {
  1193. if (sc_info->not_first_work) {
  1194. applog(LOG_INFO, "%s%i:%s failed to find nonce work - can't be processed - ignored",
  1195. bflsc->drv->name, bflsc->device_id, xlink);
  1196. inc_bflsc_errors(thr, sc_info, core);
  1197. }
  1198. return;
  1199. }
  1200. res = false;
  1201. x = 0;
  1202. for (i = sc_info->que_fld_min; i < count; i++) {
  1203. if (strlen(fields[i]) != 8) {
  1204. tmp = str_text(data);
  1205. applogsiz(LOG_INFO, BFLSC_APPLOGSIZ,
  1206. "%s%i:%s invalid nonce (%s) will try to process anyway",
  1207. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1208. free(tmp);
  1209. }
  1210. hex2bin((void*)&nonce, fields[i], 4);
  1211. nonce = htobe32(nonce);
  1212. res = submit_nonce(thr, work, nonce);
  1213. if (res) {
  1214. wr_lock(&(sc_info->stat_lock));
  1215. sc_info->sc_devs[dev].nonces_found++;
  1216. wr_unlock(&(sc_info->stat_lock));
  1217. (*nonces)++;
  1218. x++;
  1219. inc_bflsc_nonces(sc_info, core);
  1220. } else
  1221. inc_core_errors(sc_info, core);
  1222. }
  1223. wr_lock(&(sc_info->stat_lock));
  1224. if (res)
  1225. sc_info->sc_devs[dev].result_id++;
  1226. if (x > QUE_MAX_RESULTS)
  1227. x = QUE_MAX_RESULTS + 1;
  1228. (sc_info->result_size[x])++;
  1229. sc_info->sc_devs[dev].work_complete++;
  1230. sc_info->sc_devs[dev].hashes_unsent += FULLNONCE;
  1231. // If not flushed (stale)
  1232. if (!(work->devflag))
  1233. sc_info->sc_devs[dev].work_queued -= 1;
  1234. wr_unlock(&(sc_info->stat_lock));
  1235. free_work(work);
  1236. }
  1237. static int process_results(struct cgpu_info *bflsc, int dev, char *pbuf, int *nonces, int *in_process)
  1238. {
  1239. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1240. char **items, *firstname, **fields, *lf;
  1241. int que = 0, i, lines, count;
  1242. char *tmp, *tmp2, *buf;
  1243. char xlink[17];
  1244. bool res;
  1245. *nonces = 0;
  1246. *in_process = 0;
  1247. xlinkstr(xlink, sizeof(xlink), dev, sc_info);
  1248. buf = strdup(pbuf);
  1249. if (!strncmp(buf, "INPROCESS", 9))
  1250. sscanf(buf, "INPROCESS:%d\n%s", in_process, pbuf);
  1251. res = tolines(bflsc, dev, buf, &lines, &items, C_GETRESULTS);
  1252. if (!res || lines < 1) {
  1253. tmp = str_text(pbuf);
  1254. applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
  1255. "%s%i:%s empty result (%s) ignored",
  1256. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1257. free(tmp);
  1258. goto arigatou;
  1259. }
  1260. if (lines < QUE_RES_LINES_MIN) {
  1261. tmp = str_text(pbuf);
  1262. applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
  1263. "%s%i:%s result of %d too small (%s) ignored",
  1264. bflsc->drv->name, bflsc->device_id, xlink, lines, tmp);
  1265. free(tmp);
  1266. goto arigatou;
  1267. }
  1268. breakdown(ONECOLON, items[1], &count, &firstname, &fields, &lf);
  1269. if (count < 1) {
  1270. tmp = str_text(pbuf);
  1271. tmp2 = str_text(items[1]);
  1272. applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
  1273. "%s%i:%s empty result count (%s) in (%s) ignoring",
  1274. bflsc->drv->name, bflsc->device_id, xlink, tmp2, tmp);
  1275. free(tmp2);
  1276. free(tmp);
  1277. goto arigatou;
  1278. } else if (count != 1) {
  1279. tmp = str_text(pbuf);
  1280. tmp2 = str_text(items[1]);
  1281. applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
  1282. "%s%i:%s incorrect result count %d (%s) in (%s) will try anyway",
  1283. bflsc->drv->name, bflsc->device_id, xlink, count, tmp2, tmp);
  1284. free(tmp2);
  1285. free(tmp);
  1286. }
  1287. que = atoi(fields[0]);
  1288. if (que != (lines - QUE_RES_LINES_MIN)) {
  1289. i = que;
  1290. // 1+ In case the last line isn't 'OK' - try to process it
  1291. que = 1 + lines - QUE_RES_LINES_MIN;
  1292. tmp = str_text(pbuf);
  1293. tmp2 = str_text(items[0]);
  1294. applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
  1295. "%s%i:%s incorrect result count %d (%s) will try %d (%s)",
  1296. bflsc->drv->name, bflsc->device_id, xlink, i, tmp2, que, tmp);
  1297. free(tmp2);
  1298. free(tmp);
  1299. }
  1300. freebreakdown(&count, &firstname, &fields);
  1301. for (i = 0; i < que; i++) {
  1302. res = breakdown(NOCOLON, items[i + QUE_RES_LINES_MIN - 1], &count, &firstname, &fields, &lf);
  1303. if (likely(res))
  1304. process_nonces(bflsc, dev, &(xlink[0]), items[i], count, fields, nonces);
  1305. else
  1306. applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
  1307. "%s%i:%s failed to process nonce %s",
  1308. bflsc->drv->name, bflsc->device_id, xlink, items[i]);
  1309. freebreakdown(&count, &firstname, &fields);
  1310. sc_info->not_first_work = true;
  1311. }
  1312. arigatou:
  1313. freetolines(&lines, &items);
  1314. free(buf);
  1315. return que;
  1316. }
  1317. #define TVF(tv) ((float)((tv)->tv_sec) + ((float)((tv)->tv_usec) / 1000000.0))
  1318. #define TVFMS(tv) (TVF(tv) * 1000.0)
  1319. // Thread to simply keep looking for results
  1320. static void *bflsc_get_results(void *userdata)
  1321. {
  1322. struct cgpu_info *bflsc = (struct cgpu_info *)userdata;
  1323. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1324. struct timeval elapsed, now;
  1325. float oldest, f;
  1326. char buf[BFLSC_BUFSIZ+1];
  1327. int err, amount;
  1328. int i, que, dev, nonces;
  1329. bool readok;
  1330. cgtime(&now);
  1331. for (i = 0; i < sc_info->sc_count; i++) {
  1332. copy_time(&(sc_info->sc_devs[i].last_check_result), &now);
  1333. copy_time(&(sc_info->sc_devs[i].last_dev_result), &now);
  1334. copy_time(&(sc_info->sc_devs[i].last_nonce_result), &now);
  1335. }
  1336. while (sc_info->shutdown == false) {
  1337. cgtimer_t ts_start;
  1338. int in_process;
  1339. if (bflsc->usbinfo.nodev)
  1340. return NULL;
  1341. dev = -1;
  1342. oldest = FLT_MAX;
  1343. cgtime(&now);
  1344. // Find the first oldest ... that also needs checking
  1345. for (i = 0; i < sc_info->sc_count; i++) {
  1346. timersub(&now, &(sc_info->sc_devs[i].last_check_result), &elapsed);
  1347. f = TVFMS(&elapsed);
  1348. if (f < oldest && f >= sc_info->sc_devs[i].ms_work) {
  1349. f = oldest;
  1350. dev = i;
  1351. }
  1352. }
  1353. if (bflsc->usbinfo.nodev)
  1354. return NULL;
  1355. cgsleep_prepare_r(&ts_start);
  1356. if (dev == -1)
  1357. goto utsura;
  1358. cgtime(&(sc_info->sc_devs[dev].last_check_result));
  1359. readok = bflsc_qres(bflsc, buf, sizeof(buf), dev, &err, &amount, false);
  1360. if (err < 0 || (!readok && amount != BFLSC_QRES_LEN) || (readok && amount < 1)) {
  1361. // TODO: do what else?
  1362. } else {
  1363. que = process_results(bflsc, dev, buf, &nonces, &in_process);
  1364. sc_info->not_first_work = true; // in case it failed processing it
  1365. if (que > 0)
  1366. cgtime(&(sc_info->sc_devs[dev].last_dev_result));
  1367. if (nonces > 0)
  1368. cgtime(&(sc_info->sc_devs[dev].last_nonce_result));
  1369. /* There are more results queued so do not sleep */
  1370. if (in_process)
  1371. continue;
  1372. // TODO: if not getting results ... reinit?
  1373. }
  1374. utsura:
  1375. cgsleep_ms_r(&ts_start, sc_info->results_sleep_time);
  1376. }
  1377. return NULL;
  1378. }
  1379. static bool bflsc_thread_prepare(struct thr_info *thr)
  1380. {
  1381. struct cgpu_info *bflsc = thr->cgpu;
  1382. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1383. if (thr_info_create(&(sc_info->results_thr), NULL, bflsc_get_results, (void *)bflsc)) {
  1384. applog(LOG_ERR, "%s%i: thread create failed", bflsc->drv->name, bflsc->device_id);
  1385. return false;
  1386. }
  1387. pthread_detach(sc_info->results_thr.pth);
  1388. return true;
  1389. }
  1390. static void bflsc_shutdown(struct thr_info *thr)
  1391. {
  1392. struct cgpu_info *bflsc = thr->cgpu;
  1393. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1394. bflsc_flush_work(bflsc);
  1395. sc_info->shutdown = true;
  1396. }
  1397. static void bflsc_thread_enable(struct thr_info *thr)
  1398. {
  1399. struct cgpu_info *bflsc = thr->cgpu;
  1400. if (bflsc->usbinfo.nodev)
  1401. return;
  1402. bflsc_initialise(bflsc);
  1403. }
  1404. static bool bflsc_send_work(struct cgpu_info *bflsc, int dev, bool mandatory)
  1405. {
  1406. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1407. struct FullNonceRangeJob data;
  1408. char buf[BFLSC_BUFSIZ+1];
  1409. bool sent, ret = false;
  1410. struct work *work;
  1411. int err, amount;
  1412. int len, try;
  1413. int stage;
  1414. // Device is gone
  1415. if (bflsc->usbinfo.nodev)
  1416. return false;
  1417. // TODO: handle this everywhere
  1418. if (sc_info->sc_devs[dev].overheat == true)
  1419. return false;
  1420. // Initially code only deals with sending one work item
  1421. data.payloadSize = BFLSC_JOBSIZ;
  1422. data.endOfBlock = BFLSC_EOB;
  1423. len = sizeof(struct FullNonceRangeJob);
  1424. /* On faster devices we have a lot of lock contention so only
  1425. * mandatorily grab the lock and send work if the queue is empty since
  1426. * we have a submit queue. */
  1427. if (mandatory)
  1428. mutex_lock(&(bflsc->device_mutex));
  1429. else {
  1430. if (mutex_trylock(&bflsc->device_mutex))
  1431. return ret;
  1432. }
  1433. work = get_queued(bflsc);
  1434. if (unlikely(!work)) {
  1435. mutex_unlock(&bflsc->device_mutex);
  1436. return ret;
  1437. }
  1438. memcpy(data.midState, work->midstate, MIDSTATE_BYTES);
  1439. memcpy(data.blockData, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  1440. try = 0;
  1441. re_send:
  1442. err = send_recv_ds(bflsc, dev, &stage, &sent, &amount,
  1443. BFLSC_QJOB, BFLSC_QJOB_LEN, C_REQUESTQUEJOB, C_REQUESTQUEJOBSTATUS,
  1444. (char *)&data, len, C_QUEJOB, C_QUEJOBSTATUS,
  1445. buf, sizeof(buf)-1);
  1446. mutex_unlock(&(bflsc->device_mutex));
  1447. switch (stage) {
  1448. case 1:
  1449. if (!sent) {
  1450. bflsc_applog(bflsc, dev, C_REQUESTQUEJOB, amount, err);
  1451. goto out;
  1452. } else {
  1453. // TODO: handle other errors ...
  1454. // Try twice
  1455. if (try++ < 1 && amount > 1 &&
  1456. strstr(buf, BFLSC_TIMEOUT))
  1457. goto re_send;
  1458. bflsc_applog(bflsc, dev, C_REQUESTQUEJOBSTATUS, amount, err);
  1459. goto out;
  1460. }
  1461. break;
  1462. case 2:
  1463. if (!sent) {
  1464. bflsc_applog(bflsc, dev, C_QUEJOB, amount, err);
  1465. goto out;
  1466. } else {
  1467. if (!isokerr(err, buf, amount)) {
  1468. // TODO: check for QUEUE FULL and set work_queued to sc_info->que_size
  1469. // and report a code bug LOG_ERR - coz it should never happen
  1470. // TODO: handle other errors ...
  1471. // Try twice
  1472. if (try++ < 1 && amount > 1 &&
  1473. strstr(buf, BFLSC_TIMEOUT))
  1474. goto re_send;
  1475. bflsc_applog(bflsc, dev, C_QUEJOBSTATUS, amount, err);
  1476. goto out;
  1477. }
  1478. }
  1479. break;
  1480. }
  1481. wr_lock(&(sc_info->stat_lock));
  1482. sc_info->sc_devs[dev].work_queued++;
  1483. wr_unlock(&(sc_info->stat_lock));
  1484. work->subid = dev;
  1485. ret = true;
  1486. out:
  1487. if (unlikely(!ret))
  1488. work_completed(bflsc, work);
  1489. return ret;
  1490. }
  1491. #define JP_COMMAND 0
  1492. #define JP_STREAMLENGTH 2
  1493. #define JP_SIGNATURE 4
  1494. #define JP_JOBSINARRY 5
  1495. #define JP_JOBSARRY 6
  1496. #define JP_ARRAYSIZE 45
  1497. static bool bflsc28_queue_full(struct cgpu_info *bflsc)
  1498. {
  1499. struct bflsc_info *sc_info = bflsc->device_data;
  1500. int created, queued = 0, create, i, offset;
  1501. struct work *base_work, *work, *works[10];
  1502. char *buf, *field, *ptr;
  1503. bool sent, ret = false;
  1504. uint16_t *streamlen;
  1505. uint8_t *job_pack;
  1506. int err, amount;
  1507. job_pack = alloca(2 + // Command
  1508. 2 + // StreamLength
  1509. 1 + // Signature
  1510. 1 + // JobsInArray
  1511. JP_ARRAYSIZE * 10 +// Array of up to 10 Job Structs
  1512. 1 // EndOfWrapper
  1513. );
  1514. if (bflsc->usbinfo.nodev)
  1515. return true;
  1516. /* Don't send any work if this device is overheating */
  1517. if (sc_info->sc_devs[0].overheat == true)
  1518. return true;
  1519. wr_lock(&bflsc->qlock);
  1520. base_work = __get_queued(bflsc);
  1521. if (likely(base_work))
  1522. __work_completed(bflsc, base_work);
  1523. wr_unlock(&bflsc->qlock);
  1524. if (unlikely(!base_work))
  1525. return ret;
  1526. created = 1;
  1527. create = 9;
  1528. if (base_work->drv_rolllimit < create)
  1529. create = base_work->drv_rolllimit;
  1530. works[0] = base_work;
  1531. for (i = 1; i <= create ; i++) {
  1532. created++;
  1533. work = make_clone(base_work);
  1534. roll_work(base_work);
  1535. works[i] = work;
  1536. }
  1537. memcpy(job_pack, "WX", 2);
  1538. streamlen = (uint16_t *)&job_pack[JP_STREAMLENGTH];
  1539. *streamlen = created * JP_ARRAYSIZE + 7;
  1540. job_pack[JP_SIGNATURE] = 0xc1;
  1541. job_pack[JP_JOBSINARRY] = created;
  1542. offset = JP_JOBSARRY;
  1543. /* Create the maximum number of work items we can queue by nrolling one */
  1544. for (i = 0; i < created; i++) {
  1545. work = works[i];
  1546. memcpy(job_pack + offset, work->midstate, MIDSTATE_BYTES);
  1547. offset += MIDSTATE_BYTES;
  1548. memcpy(job_pack + offset, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  1549. offset += MERKLE_BYTES;
  1550. job_pack[offset] = 0xaa; // EndOfBlock signature
  1551. offset++;
  1552. }
  1553. job_pack[offset++] = 0xfe; // EndOfWrapper
  1554. buf = alloca(BFLSC_BUFSIZ + 1);
  1555. mutex_lock(&bflsc->device_mutex);
  1556. err = send_recv_ss(bflsc, 0, &sent, &amount, (char *)job_pack, offset,
  1557. C_REQUESTQUEJOB, buf, BFLSC_BUFSIZ, C_REQUESTQUEJOBSTATUS, READ_NL);
  1558. mutex_unlock(&bflsc->device_mutex);
  1559. if (!isokerr(err, buf, amount)) {
  1560. if (!strncasecmp(buf, "ERR:QUEUE FULL", 14)) {
  1561. applog(LOG_DEBUG, "%s%d: Queue full",
  1562. bflsc->drv->name, bflsc->device_id);
  1563. ret = true;
  1564. } else {
  1565. applog(LOG_WARNING, "%s%d: Queue response not ok %s",
  1566. bflsc->drv->name, bflsc->device_id, buf);
  1567. }
  1568. goto out;
  1569. }
  1570. ptr = alloca(strlen(buf));
  1571. if (sscanf(buf, "OK:QUEUED %d:%s", &queued, ptr) != 2) {
  1572. applog(LOG_WARNING, "%s%d: Failed to parse queue response %s",
  1573. bflsc->drv->name, bflsc->device_id, buf);
  1574. goto out;
  1575. }
  1576. if (queued < 1 || queued > 10) {
  1577. applog(LOG_WARNING, "%s%d: Invalid queued count %d",
  1578. bflsc->drv->name, bflsc->device_id, queued);
  1579. queued = 0;
  1580. goto out;
  1581. }
  1582. for (i = 0; i < queued; i++) {
  1583. struct bflsc_work *bwork, *oldbwork;
  1584. unsigned int uid;
  1585. work = works[i];
  1586. field = Strsep(&ptr, ",");
  1587. if (!field) {
  1588. applog(LOG_WARNING, "%s%d: Ran out of queued IDs after %d of %d",
  1589. bflsc->drv->name, bflsc->device_id, i, queued);
  1590. queued = i;
  1591. goto out;
  1592. }
  1593. sscanf(field, "%04x", &uid);
  1594. bwork = cgcalloc(sizeof(struct bflsc_work), 1);
  1595. bwork->id = uid;
  1596. bwork->work = work;
  1597. wr_lock(&bflsc->qlock);
  1598. HASH_REPLACE_INT(sc_info->bworks, id, bwork, oldbwork);
  1599. if (oldbwork) {
  1600. free_work(oldbwork->work);
  1601. free(oldbwork);
  1602. }
  1603. wr_unlock(&bflsc->qlock);
  1604. sc_info->sc_devs[0].work_queued++;
  1605. }
  1606. if (queued < created)
  1607. ret = true;
  1608. out:
  1609. for (i = queued; i < created; i++) {
  1610. work = works[i];
  1611. discard_work(work);
  1612. }
  1613. return ret;
  1614. }
  1615. static bool bflsc_queue_full(struct cgpu_info *bflsc)
  1616. {
  1617. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1618. int i, dev, tried, que;
  1619. bool ret = false;
  1620. int tries = 0;
  1621. tried = -1;
  1622. // if something is wrong with a device try the next one available
  1623. // TODO: try them all? Add an unavailable flag to sc_devs[i] init to 0 here first
  1624. while (++tries < 3) {
  1625. bool mandatory = false;
  1626. // Device is gone - shouldn't normally get here
  1627. if (bflsc->usbinfo.nodev) {
  1628. ret = true;
  1629. break;
  1630. }
  1631. dev = -1;
  1632. rd_lock(&(sc_info->stat_lock));
  1633. // Anything waiting - gets the work first
  1634. for (i = 0; i < sc_info->sc_count; i++) {
  1635. // TODO: and ignore x-link dead - once I work out how to decide it is dead
  1636. if (i != tried && sc_info->sc_devs[i].work_queued == 0 &&
  1637. !sc_info->sc_devs[i].overheat) {
  1638. dev = i;
  1639. break;
  1640. }
  1641. }
  1642. if (dev == -1) {
  1643. que = sc_info->que_size * 10; // 10x is certainly above the MAX it could be
  1644. // The first device with the smallest amount queued
  1645. for (i = 0; i < sc_info->sc_count; i++) {
  1646. if (i != tried && sc_info->sc_devs[i].work_queued < que &&
  1647. !sc_info->sc_devs[i].overheat) {
  1648. dev = i;
  1649. que = sc_info->sc_devs[i].work_queued;
  1650. }
  1651. }
  1652. if (que > sc_info->que_full_enough)
  1653. dev = -1;
  1654. else if (que < sc_info->que_low)
  1655. mandatory = true;
  1656. }
  1657. rd_unlock(&(sc_info->stat_lock));
  1658. // nothing needs work yet
  1659. if (dev == -1) {
  1660. ret = true;
  1661. break;
  1662. }
  1663. if (bflsc_send_work(bflsc, dev, mandatory))
  1664. break;
  1665. else
  1666. tried = dev;
  1667. }
  1668. return ret;
  1669. }
  1670. static int64_t bflsc_scanwork(struct thr_info *thr)
  1671. {
  1672. struct cgpu_info *bflsc = thr->cgpu;
  1673. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1674. int64_t ret, unsent;
  1675. bool flushed, cleanup;
  1676. struct work *work, *tmp;
  1677. int dev, waited, i;
  1678. // Device is gone
  1679. if (bflsc->usbinfo.nodev)
  1680. return -1;
  1681. flushed = false;
  1682. // Single lock check if any are flagged as flushed
  1683. rd_lock(&(sc_info->stat_lock));
  1684. for (dev = 0; dev < sc_info->sc_count; dev++)
  1685. flushed |= sc_info->sc_devs[dev].flushed;
  1686. rd_unlock(&(sc_info->stat_lock));
  1687. // > 0 flagged as flushed
  1688. if (flushed) {
  1689. // TODO: something like this ......
  1690. for (dev = 0; dev < sc_info->sc_count; dev++) {
  1691. cleanup = false;
  1692. // Is there any flushed work that can be removed?
  1693. rd_lock(&(sc_info->stat_lock));
  1694. if (sc_info->sc_devs[dev].flushed) {
  1695. if (sc_info->sc_devs[dev].result_id > (sc_info->sc_devs[dev].flush_id + sc_info->flush_size))
  1696. cleanup = true;
  1697. }
  1698. rd_unlock(&(sc_info->stat_lock));
  1699. // yes remove the flushed work that can be removed
  1700. if (cleanup) {
  1701. wr_lock(&bflsc->qlock);
  1702. HASH_ITER(hh, bflsc->queued_work, work, tmp) {
  1703. if (work->devflag && work->subid == dev) {
  1704. bflsc->queued_count--;
  1705. HASH_DEL(bflsc->queued_work, work);
  1706. discard_work(work);
  1707. }
  1708. }
  1709. wr_unlock(&bflsc->qlock);
  1710. wr_lock(&(sc_info->stat_lock));
  1711. sc_info->sc_devs[dev].flushed = false;
  1712. wr_unlock(&(sc_info->stat_lock));
  1713. }
  1714. }
  1715. }
  1716. waited = restart_wait(thr, sc_info->scan_sleep_time);
  1717. if (waited == ETIMEDOUT && sc_info->ident != IDENT_BMA) {
  1718. unsigned int old_sleep_time, new_sleep_time = 0;
  1719. int min_queued = sc_info->que_size;
  1720. /* Only adjust the scan_sleep_time if we did not receive a
  1721. * restart message while waiting. Try to adjust sleep time
  1722. * so we drop to sc_info->que_watermark before getting more work.
  1723. */
  1724. rd_lock(&sc_info->stat_lock);
  1725. old_sleep_time = sc_info->scan_sleep_time;
  1726. for (i = 0; i < sc_info->sc_count; i++) {
  1727. if (sc_info->sc_devs[i].work_queued < min_queued)
  1728. min_queued = sc_info->sc_devs[i].work_queued;
  1729. }
  1730. rd_unlock(&sc_info->stat_lock);
  1731. new_sleep_time = old_sleep_time;
  1732. /* Increase slowly but decrease quickly */
  1733. if (min_queued > sc_info->que_full_enough && old_sleep_time < BFLSC_MAX_SLEEP)
  1734. new_sleep_time = old_sleep_time * 21 / 20;
  1735. else if (min_queued < sc_info->que_low)
  1736. new_sleep_time = old_sleep_time * 2 / 3;
  1737. /* Do not sleep more than BFLSC_MAX_SLEEP so we can always
  1738. * report in at least 2 results per 5s log interval. */
  1739. if (new_sleep_time != old_sleep_time) {
  1740. if (new_sleep_time > BFLSC_MAX_SLEEP)
  1741. new_sleep_time = BFLSC_MAX_SLEEP;
  1742. else if (new_sleep_time == 0)
  1743. new_sleep_time = 1;
  1744. applog(LOG_DEBUG, "%s%i: Changed scan sleep time to %d",
  1745. bflsc->drv->name, bflsc->device_id, new_sleep_time);
  1746. wr_lock(&sc_info->stat_lock);
  1747. sc_info->scan_sleep_time = new_sleep_time;
  1748. wr_unlock(&sc_info->stat_lock);
  1749. }
  1750. }
  1751. // Count up the work done since we last were here
  1752. ret = 0;
  1753. wr_lock(&(sc_info->stat_lock));
  1754. for (dev = 0; dev < sc_info->sc_count; dev++) {
  1755. unsent = sc_info->sc_devs[dev].hashes_unsent;
  1756. sc_info->sc_devs[dev].hashes_unsent = 0;
  1757. sc_info->sc_devs[dev].hashes_sent += unsent;
  1758. sc_info->hashes_sent += unsent;
  1759. ret += unsent;
  1760. }
  1761. wr_unlock(&(sc_info->stat_lock));
  1762. return ret;
  1763. }
  1764. #define BFLSC_OVER_TEMP 75
  1765. /* Set the fanspeed to auto for any valid value <= BFLSC_OVER_TEMP,
  1766. * or max for any value > BFLSC_OVER_TEMP or if we don't know the temperature. */
  1767. static void bflsc_set_fanspeed(struct cgpu_info *bflsc)
  1768. {
  1769. struct bflsc_info *sc_info = (struct bflsc_info *)bflsc->device_data;
  1770. char buf[BFLSC_BUFSIZ+1];
  1771. char data[16+1];
  1772. int amount;
  1773. bool sent;
  1774. if ((bflsc->temp <= BFLSC_OVER_TEMP && bflsc->temp > 0 && sc_info->fanauto) ||
  1775. ((bflsc->temp > BFLSC_OVER_TEMP || !bflsc->temp) && !sc_info->fanauto))
  1776. return;
  1777. if (bflsc->temp > BFLSC_OVER_TEMP || !bflsc->temp) {
  1778. strcpy(data, BFLSC_FAN4);
  1779. sc_info->fanauto = false;
  1780. } else {
  1781. strcpy(data, BFLSC_FANAUTO);
  1782. sc_info->fanauto = true;
  1783. }
  1784. applog(LOG_DEBUG, "%s%i: temp=%.0f over=%d set fan to %s",
  1785. bflsc->drv->name, bflsc->device_id, bflsc->temp,
  1786. BFLSC_OVER_TEMP, data);
  1787. mutex_lock(&bflsc->device_mutex);
  1788. send_recv_ss(bflsc, 0, &sent, &amount,
  1789. data, strlen(data), C_SETFAN,
  1790. buf, sizeof(buf)-1, C_FANREPLY, READ_NL);
  1791. mutex_unlock(&bflsc->device_mutex);
  1792. }
  1793. static bool bflsc_get_stats(struct cgpu_info *bflsc)
  1794. {
  1795. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1796. bool allok = true;
  1797. int i;
  1798. // Device is gone
  1799. if (bflsc->usbinfo.nodev)
  1800. return false;
  1801. for (i = 0; i < sc_info->sc_count; i++) {
  1802. if (!bflsc_get_temp(bflsc, i))
  1803. allok = false;
  1804. // Device is gone
  1805. if (bflsc->usbinfo.nodev)
  1806. return false;
  1807. if (i < (sc_info->sc_count - 1))
  1808. cgsleep_ms(BFLSC_TEMP_SLEEPMS);
  1809. }
  1810. bflsc_set_fanspeed(bflsc);
  1811. return allok;
  1812. }
  1813. static char *bflsc_set(struct cgpu_info *bflsc, char *option, char *setting, char *replybuf)
  1814. {
  1815. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1816. int val;
  1817. if (sc_info->ident != IDENT_BMA) {
  1818. strcpy(replybuf, "no set options available");
  1819. return replybuf;
  1820. }
  1821. if (strcasecmp(option, "help") == 0) {
  1822. sprintf(replybuf, "volt: range 0-9 clock: range 0-15");
  1823. return replybuf;
  1824. }
  1825. if (strcasecmp(option, "volt") == 0) {
  1826. if (!setting || !*setting) {
  1827. sprintf(replybuf, "missing volt setting");
  1828. return replybuf;
  1829. }
  1830. val = atoi(setting);
  1831. if (val < 0 || val > 9) {
  1832. sprintf(replybuf, "invalid volt: '%s' valid range 0-9",
  1833. setting);
  1834. }
  1835. sc_info->volt_next = val;
  1836. sc_info->volt_next_stat = true;
  1837. return NULL;
  1838. }
  1839. if (strcasecmp(option, "clock") == 0) {
  1840. if (!setting || !*setting) {
  1841. sprintf(replybuf, "missing clock setting");
  1842. return replybuf;
  1843. }
  1844. val = atoi(setting);
  1845. if (val < 0 || val > 15) {
  1846. sprintf(replybuf, "invalid clock: '%s' valid range 0-15",
  1847. setting);
  1848. }
  1849. sc_info->clock_next = val;
  1850. sc_info->clock_next_stat = true;
  1851. return NULL;
  1852. }
  1853. sprintf(replybuf, "Unknown option: %s", option);
  1854. return replybuf;
  1855. }
  1856. static void bflsc_identify(struct cgpu_info *bflsc)
  1857. {
  1858. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1859. // TODO: handle x-link
  1860. sc_info->flash_led = true;
  1861. }
  1862. static bool bflsc_thread_init(struct thr_info *thr)
  1863. {
  1864. struct cgpu_info *bflsc = thr->cgpu;
  1865. if (bflsc->usbinfo.nodev)
  1866. return false;
  1867. bflsc_initialise(bflsc);
  1868. return true;
  1869. }
  1870. // there should be a new API function to return device info that isn't the standard stuff
  1871. // instead of bflsc_api_stats - since the stats should really just be internal code info
  1872. // and the new one should be UNusual device stats/extra details - like the stuff below
  1873. static struct api_data *bflsc_api_stats(struct cgpu_info *bflsc)
  1874. {
  1875. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1876. struct api_data *root = NULL;
  1877. char data[4096];
  1878. char buf[256];
  1879. int i, j, off;
  1880. size_t len;
  1881. //if no x-link ... etc
  1882. rd_lock(&(sc_info->stat_lock));
  1883. root = api_add_temp(root, "Temp1", &(sc_info->sc_devs[0].temp1), true);
  1884. root = api_add_temp(root, "Temp2", &(sc_info->sc_devs[0].temp2), true);
  1885. root = api_add_volts(root, "Vcc1", &(sc_info->sc_devs[0].vcc1), true);
  1886. root = api_add_volts(root, "Vcc2", &(sc_info->sc_devs[0].vcc2), true);
  1887. root = api_add_volts(root, "Vmain", &(sc_info->sc_devs[0].vmain), true);
  1888. root = api_add_temp(root, "Temp1 Max", &(sc_info->sc_devs[0].temp1_max), true);
  1889. root = api_add_temp(root, "Temp2 Max", &(sc_info->sc_devs[0].temp2_max), true);
  1890. root = api_add_time(root, "Temp1 Max Time", &(sc_info->sc_devs[0].temp1_max_time), true);
  1891. root = api_add_time(root, "Temp2 Max Time", &(sc_info->sc_devs[0].temp2_max_time), true);
  1892. root = api_add_int(root, "Work Queued", &(sc_info->sc_devs[0].work_queued), true);
  1893. root = api_add_int(root, "Work Complete", &(sc_info->sc_devs[0].work_complete), true);
  1894. root = api_add_bool(root, "Overheat", &(sc_info->sc_devs[0].overheat), true);
  1895. root = api_add_uint64(root, "Flush ID", &(sc_info->sc_devs[0].flush_id), true);
  1896. root = api_add_uint64(root, "Result ID", &(sc_info->sc_devs[0].result_id), true);
  1897. root = api_add_bool(root, "Flushed", &(sc_info->sc_devs[0].flushed), true);
  1898. root = api_add_uint(root, "Scan Sleep", &(sc_info->scan_sleep_time), true);
  1899. root = api_add_uint(root, "Results Sleep", &(sc_info->results_sleep_time), true);
  1900. root = api_add_uint(root, "Work ms", &(sc_info->default_ms_work), true);
  1901. buf[0] = '\0';
  1902. for (i = 0; i <= QUE_MAX_RESULTS + 1; i++)
  1903. tailsprintf(buf, sizeof(buf), "%s%"PRIu64, (i > 0) ? "/" : "", sc_info->result_size[i]);
  1904. root = api_add_string(root, "Result Size", buf, true);
  1905. rd_unlock(&(sc_info->stat_lock));
  1906. i = (int)(sc_info->driver_version);
  1907. root = api_add_int(root, "Driver", &i, true);
  1908. root = api_add_string(root, "Firmware", sc_info->sc_devs[0].firmware, false);
  1909. root = api_add_string(root, "Chips", sc_info->sc_devs[0].chips, false);
  1910. root = api_add_int(root, "Que Size", &(sc_info->que_size), false);
  1911. root = api_add_int(root, "Que Full", &(sc_info->que_full_enough), false);
  1912. root = api_add_int(root, "Que Watermark", &(sc_info->que_watermark), false);
  1913. root = api_add_int(root, "Que Low", &(sc_info->que_low), false);
  1914. root = api_add_escape(root, "GetInfo", sc_info->sc_devs[0].getinfo, false);
  1915. /*
  1916. else a whole lot of something like these ... etc
  1917. root = api_add_temp(root, "X-%d-Temp1", &(sc_info->temp1), false);
  1918. root = api_add_temp(root, "X-%d-Temp2", &(sc_info->temp2), false);
  1919. root = api_add_volts(root, "X-%d-Vcc1", &(sc_info->vcc1), false);
  1920. root = api_add_volts(root, "X-%d-Vcc2", &(sc_info->vcc2), false);
  1921. root = api_add_volts(root, "X-%d-Vmain", &(sc_info->vmain), false);
  1922. */
  1923. if (sc_info->ident == IDENT_BMA) {
  1924. for (i = 0; i < 128; i += 16) {
  1925. data[0] = '\0';
  1926. off = 0;
  1927. for (j = 0; j < 16; j++) {
  1928. len = snprintf(data+off, sizeof(data)-off,
  1929. "%s%"PRIu64,
  1930. j > 0 ? " " : "",
  1931. sc_info->cortex_nonces[i+j]);
  1932. if (len >= (sizeof(data)-off))
  1933. off = sizeof(data)-1;
  1934. else {
  1935. if (len > 0)
  1936. off += len;
  1937. }
  1938. }
  1939. sprintf(buf, "Cortex %02x-%02x Nonces", i, i+15);
  1940. root = api_add_string(root, buf, data, true);
  1941. }
  1942. for (i = 0; i < 128; i += 16) {
  1943. data[0] = '\0';
  1944. off = 0;
  1945. for (j = 0; j < 16; j++) {
  1946. len = snprintf(data+off, sizeof(data)-off,
  1947. "%s%"PRIu64,
  1948. j > 0 ? " " : "",
  1949. sc_info->cortex_hw[i+j]);
  1950. if (len >= (sizeof(data)-off))
  1951. off = sizeof(data)-1;
  1952. else {
  1953. if (len > 0)
  1954. off += len;
  1955. }
  1956. }
  1957. sprintf(buf, "Cortex %02x-%02x HW Errors", i, i+15);
  1958. root = api_add_string(root, buf, data, true);
  1959. }
  1960. } else if (sc_info->que_noncecount != QUE_NONCECOUNT_V1) {
  1961. for (i = 0; i < 16; i++) {
  1962. sprintf(buf, "Core%d Nonces", i);
  1963. root = api_add_uint64(root, buf, &sc_info->core_nonces[i], false);
  1964. }
  1965. for (i = 0; i < 16; i++) {
  1966. sprintf(buf, "Core%d HW Errors", i);
  1967. root = api_add_uint64(root, buf, &sc_info->core_hw[i], false);
  1968. }
  1969. }
  1970. return root;
  1971. }
  1972. struct device_drv bflsc_drv = {
  1973. .drv_id = DRIVER_bflsc,
  1974. .dname = "BitForceSC",
  1975. .name = BFLSC_SINGLE,
  1976. .drv_detect = bflsc_detect,
  1977. .get_api_stats = bflsc_api_stats,
  1978. .get_statline_before = get_bflsc_statline_before,
  1979. .get_stats = bflsc_get_stats,
  1980. .set_device = bflsc_set,
  1981. .identify_device = bflsc_identify,
  1982. .thread_prepare = bflsc_thread_prepare,
  1983. .thread_init = bflsc_thread_init,
  1984. .hash_work = hash_queued_work,
  1985. .scanwork = bflsc_scanwork,
  1986. .queue_full = bflsc_queue_full,
  1987. .flush_work = bflsc_flush_work,
  1988. .thread_shutdown = bflsc_shutdown,
  1989. .thread_enable = bflsc_thread_enable
  1990. };