driver-bflsc.c 55 KB

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