driver-bflsc.c 62 KB

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