driver-bflsc.c 61 KB

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