driver-bitfury16.c 157 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677
  1. #include "config.h"
  2. #include "math.h"
  3. #include "miner.h"
  4. #include "bf16-communication.h"
  5. #include "bf16-ctrldevice.h"
  6. #include "bf16-mspcontrol.h"
  7. #include "bf16-spidevice.h"
  8. #include "bf16-uartdevice.h"
  9. #include "driver-bitfury16.h"
  10. #ifdef FILELOG
  11. #define LOGFILE "/var/log/cgminer.log"
  12. #endif
  13. #define DISABLE_SEND_CMD_ERROR
  14. #define POWER_WAIT_INTERVAL 100000
  15. #define RENONCE_SEND 5
  16. #define RENONCE_STAGE2_LIMIT 80
  17. #define RENONCE_STAGE3_LIMIT 60
  18. #define RENONCE_QUEUE_LEN 100
  19. #define RENONCE_COUNT 29
  20. /* chip nonce queue len */
  21. #define NONCE_CHIP_QUEUE_LEN 7
  22. #define RENONCE_CHIP_QUEUE_LEN 40
  23. #define WORK_TIMEOUT 2.0
  24. /* statistics intervals */
  25. #define AVG_TIME_DELTA 5.0
  26. #define AVG_TIME_INTERVAL 5.0
  27. /* power chain alarm interval */
  28. #define ICHAIN_ALARM_INTERVAL 60
  29. #define U_LOSS 0.2
  30. /* power chain reenable timeout */
  31. #define CHAIN_REENABLE_INTERVAL 10
  32. #define CHAIN_WORK_INTERVAL 1200
  33. /* disable chip if no good nonces received during CHIP_FAILING_INTERVAL */
  34. #define RENONCE_CHIP_FAILING_INTERVAL 15.0
  35. #define CHIP_FAILING_INTERVAL 30.0
  36. #define CHIP_RECOVERY_INTERVAL 5.0
  37. /* chip is considered to be failed after CHIP_ERROR_FAIL_LIMIT recovery attempts */
  38. #define CHIP_ERROR_FAIL_LIMIT 10
  39. /* this value should be less than CHIP_ERROR_FAIL_LIMIT */
  40. #define RENONCE_CHIP_ERROR_FAIL_LIMIT 8
  41. #define CHIP_ERROR_LIMIT 5
  42. #define CHIP_TASK_STATUS_INTERVAL 7000
  43. #define CHIP_TASK_SWITCH_INTERVAL 5000000
  44. #define CHIP_RESTART_LIMIT 120
  45. /* alarm intervals */
  46. #define LED_GREEN_INTERVAL 1000000
  47. #define LED_RED_INTERVAL 1000000
  48. #define LED_RED_NET_INTERVAL 500000
  49. #define BUZZER_INTERVAL 1000000
  50. /* threads delay */
  51. #define CHIPWORKER_DELAY 1000000
  52. #define NONCEWORKER_DELAY 30000
  53. #define RENONCEWORKER_DELAY 30000
  54. #define HWMONITOR_DELAY 1000000
  55. #define STATISTICS_DELAY 400000
  56. #define ALARM_DELAY 500000
  57. /* hold 3 works for each chip */
  58. #define WORK_QUEUE_LEN 3 * CHIPS_NUM
  59. /* set clock to all chips and exit */
  60. bool opt_bf16_set_clock = false;
  61. /* enable board mining statistics output */
  62. bool opt_bf16_stats_enabled = false;
  63. /* disable automatic power management */
  64. bool opt_bf16_power_management_disabled = false;
  65. /* renonce configuration */
  66. int opt_bf16_renonce = RENONCE_ONE_CHIP;
  67. /* chip clock value */
  68. char* opt_bf16_clock = NULL;
  69. uint8_t bf16_chip_clock = 0x32;
  70. /* renonce chip clock value */
  71. char* opt_bf16_renonce_clock = NULL;
  72. uint8_t bf16_renonce_chip_clock = 0x2d;
  73. /* fan speed */
  74. int opt_bf16_fan_speed = -1;
  75. /* target temp */
  76. int opt_bf16_target_temp = -1;
  77. /* alarm temp */
  78. int opt_bf16_alarm_temp = -1;
  79. /* test chip communication */
  80. char* opt_bf16_test_chip = NULL;
  81. /* number of bits to fixate */
  82. static uint32_t mask_bits = 10;
  83. /* default chip mask */
  84. static uint32_t mask = 0x00000000;
  85. /* initial pid state */
  86. static bf_pid_t pid = {
  87. .i_state = 0,
  88. .i_max = 300,
  89. .i_min = -10,
  90. };
  91. #ifdef MINER_X5
  92. bool opt_bf16_manual_pid_enabled = false;
  93. bool manual_pid_enabled = false;
  94. static bf_bcm250_map_t bcm250_map[CHIPBOARD_NUM][BCM250_NUM] = {
  95. {
  96. {
  97. .channel_path = { BF250_LOCAL, BF250_NONE },
  98. .first_good_chip = 0,
  99. .last_good_chip = BF16_NUM,
  100. .chips_num = BF16_NUM
  101. },
  102. {
  103. .channel_path = { BF250_CHAN1, BF250_LOCAL },
  104. .first_good_chip = 7,
  105. .last_good_chip = BF16_NUM,
  106. .chips_num = 4
  107. },
  108. {
  109. .channel_path = { BF250_CHAN2, BF250_LOCAL },
  110. .first_good_chip = 0,
  111. .last_good_chip = BF16_NUM,
  112. .chips_num = BF16_NUM
  113. }
  114. },
  115. {
  116. {
  117. .channel_path = { BF250_LOCAL, BF250_NONE },
  118. .first_good_chip = 0,
  119. .last_good_chip = BF16_NUM,
  120. .chips_num = BF16_NUM
  121. },
  122. {
  123. .channel_path = { BF250_CHAN1, BF250_LOCAL },
  124. .first_good_chip = 7,
  125. .last_good_chip = BF16_NUM,
  126. .chips_num = 4
  127. },
  128. {
  129. .channel_path = { BF250_CHAN2, BF250_LOCAL },
  130. .first_good_chip = 0,
  131. .last_good_chip = BF16_NUM,
  132. .chips_num = BF16_NUM
  133. }
  134. }
  135. };
  136. #endif
  137. #ifdef MINER_X6
  138. bool opt_bf16_manual_pid_disabled = false;
  139. bool manual_pid_enabled = false;
  140. static bf_bcm250_map_t bcm250_map[CHIPBOARD_NUM][BCM250_NUM] = {
  141. {
  142. {
  143. .channel_path = { BF250_LOCAL, BF250_NONE, BF250_NONE, BF250_NONE },
  144. .first_good_chip = 0,
  145. .last_good_chip = BF16_NUM,
  146. .chips_num = BF16_NUM
  147. },
  148. {
  149. .channel_path = { BF250_CHAN1, BF250_LOCAL, BF250_NONE, BF250_NONE },
  150. .first_good_chip = 0,
  151. .last_good_chip = BF16_NUM,
  152. .chips_num = BF16_NUM
  153. },
  154. {
  155. .channel_path = { BF250_CHAN2, BF250_LOCAL, BF250_NONE, BF250_NONE },
  156. .first_good_chip = 0,
  157. .last_good_chip = 4,
  158. .chips_num = 4
  159. },
  160. {
  161. .channel_path = { BF250_CHAN2, BF250_CHAN2, BF250_LOCAL, BF250_NONE },
  162. .first_good_chip = 0,
  163. .last_good_chip = BF16_NUM,
  164. .chips_num = BF16_NUM
  165. },
  166. {
  167. .channel_path = { BF250_CHAN2, BF250_CHAN2, BF250_CHAN1, BF250_LOCAL },
  168. .first_good_chip = 0,
  169. .last_good_chip = BF16_NUM,
  170. .chips_num = BF16_NUM
  171. },
  172. {
  173. .channel_path = { BF250_CHAN2, BF250_CHAN2, BF250_CHAN2, BF250_LOCAL },
  174. .first_good_chip = 0,
  175. .last_good_chip = 4,
  176. .chips_num = 4
  177. }
  178. },
  179. {
  180. {
  181. .channel_path = { BF250_LOCAL, BF250_NONE, BF250_NONE, BF250_NONE },
  182. .first_good_chip = 0,
  183. .last_good_chip = BF16_NUM,
  184. .chips_num = BF16_NUM
  185. },
  186. {
  187. .channel_path = { BF250_CHAN1, BF250_LOCAL, BF250_NONE, BF250_NONE },
  188. .first_good_chip = 0,
  189. .last_good_chip = BF16_NUM,
  190. .chips_num = BF16_NUM
  191. },
  192. {
  193. .channel_path = { BF250_CHAN2, BF250_LOCAL, BF250_NONE, BF250_NONE },
  194. .first_good_chip = 0,
  195. .last_good_chip = 4,
  196. .chips_num = 4
  197. },
  198. {
  199. .channel_path = { BF250_CHAN2, BF250_CHAN2, BF250_LOCAL, BF250_NONE },
  200. .first_good_chip = 0,
  201. .last_good_chip = BF16_NUM,
  202. .chips_num = BF16_NUM
  203. },
  204. {
  205. .channel_path = { BF250_CHAN2, BF250_CHAN2, BF250_CHAN1, BF250_LOCAL },
  206. .first_good_chip = 0,
  207. .last_good_chip = BF16_NUM,
  208. .chips_num = BF16_NUM
  209. },
  210. {
  211. .channel_path = { BF250_CHAN2, BF250_CHAN2, BF250_CHAN2, BF250_LOCAL },
  212. .first_good_chip = 0,
  213. .last_good_chip = 4,
  214. .chips_num = 4
  215. }
  216. }
  217. };
  218. #endif
  219. /* each array element contains chip address assosiated to corresponting chipboard *
  220. * e.g. first array element - renonce chip address for the first chipboard and so *
  221. * on */
  222. static bf_chip_address_t renonce_chip_address[CHIPBOARD_NUM] = {
  223. {
  224. .board_id = 0,
  225. .bcm250_id = 0,
  226. .chip_id = 0
  227. },
  228. {
  229. .board_id = 1,
  230. .bcm250_id = 0,
  231. .chip_id = 0
  232. }
  233. };
  234. #ifdef FILELOG
  235. static int filelog(struct bitfury16_info *info, const char* format, ...)
  236. {
  237. char fmt[1024];
  238. char datetime[64];
  239. struct timeval tv = {0, 0};
  240. struct tm *tm;
  241. if (info->logfile == NULL)
  242. return -1;
  243. gettimeofday(&tv, NULL);
  244. const time_t tmp_time = tv.tv_sec;
  245. int ms = (int)(tv.tv_usec / 1000);
  246. tm = localtime(&tmp_time);
  247. snprintf(datetime, sizeof(datetime), " [%d-%02d-%02d %02d:%02d:%02d.%03d] ",
  248. tm->tm_year + 1900,
  249. tm->tm_mon + 1,
  250. tm->tm_mday,
  251. tm->tm_hour,
  252. tm->tm_min,
  253. tm->tm_sec, ms);
  254. memset(fmt, 0, sizeof(fmt));
  255. sprintf(fmt, "%s%s\n", datetime, format);
  256. va_list args;
  257. va_start(args, format);
  258. mutex_lock(&info->logfile_mutex);
  259. vfprintf(info->logfile, fmt, args);
  260. fflush(info->logfile);
  261. mutex_unlock(&info->logfile_mutex);
  262. va_end(args);
  263. return 0;
  264. }
  265. #endif
  266. static double timediff(struct timeval time1, struct timeval time2)
  267. {
  268. double time1_val = 1000000 * time1.tv_sec + time1.tv_usec;
  269. double time2_val = 1000000 * time2.tv_sec + time2.tv_usec;
  270. return (double)(time2_val - time1_val) / 1000000.0;
  271. }
  272. static uint32_t timediff_us(struct timeval time1, struct timeval time2)
  273. {
  274. uint32_t time1_val = 1000000 * time1.tv_sec + time1.tv_usec;
  275. uint32_t time2_val = 1000000 * time2.tv_sec + time2.tv_usec;
  276. return (time2_val - time1_val);
  277. }
  278. static void get_average(float* average, float delta, float time_diff, float interval)
  279. {
  280. float ftotal, fprop;
  281. fprop = 1.0 - 1 / (exp((float)time_diff/(float)interval));
  282. ftotal = 1.0 + fprop;
  283. *average += (delta / time_diff * fprop);
  284. *average /= ftotal;
  285. }
  286. static uint8_t renonce_chip(bf_chip_address_t chip_address)
  287. {
  288. uint8_t board_id = chip_address.board_id;
  289. if ((renonce_chip_address[board_id].bcm250_id == chip_address.bcm250_id) &&
  290. (renonce_chip_address[board_id].chip_id == chip_address.chip_id))
  291. return 1;
  292. return 0;
  293. }
  294. static void get_next_chip_address(struct bitfury16_info *info, bf_chip_address_t* chip_address)
  295. {
  296. uint8_t board_id = chip_address->board_id;
  297. uint8_t bcm250_id = chip_address->bcm250_id;
  298. uint8_t chip_id = chip_address->chip_id;
  299. uint8_t last_good_chip = info->chipboard[board_id].bcm250[bcm250_id].last_good_chip - 1;
  300. if (last_good_chip == chip_id) {
  301. #ifdef MINER_X5
  302. bcm250_id = (bcm250_id + 1) % BCM250_NUM;
  303. chip_id = info->chipboard[board_id].bcm250[bcm250_id].first_good_chip;
  304. #endif
  305. #ifdef MINER_X6
  306. if (opt_bf16_power_management_disabled == false) {
  307. bcm250_id = (bcm250_id + 1) % BCM250_NUM;
  308. /* do not set bcm250_id to disabled chain */
  309. /* second power chain disabled */
  310. if ((info->chipboard[board_id].p_chain2_enabled == 0) &&
  311. (info->chipboard[board_id].power2_disabled == true) &&
  312. (bcm250_id >= BCM250_NUM / 2) && (bcm250_id < BCM250_NUM)) {
  313. bcm250_id = 0;
  314. }
  315. chip_id = info->chipboard[board_id].bcm250[bcm250_id].first_good_chip;
  316. } else {
  317. chip_id = info->chipboard[board_id].bcm250[bcm250_id].first_good_chip;
  318. }
  319. #endif
  320. } else
  321. chip_id++;
  322. chip_address->bcm250_id = bcm250_id;
  323. chip_address->chip_id = chip_id;
  324. }
  325. static int8_t change_renonce_chip_address(struct cgpu_info *bitfury, bf_chip_address_t chip_address)
  326. {
  327. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  328. uint8_t board_id = chip_address.board_id;
  329. uint8_t bcm250_id = chip_address.bcm250_id;
  330. uint8_t chip_id = chip_address.chip_id;
  331. bf_chip_address_t new_chip_address = { board_id, bcm250_id, chip_id };
  332. bool found = false;
  333. uint8_t chip_count = 0;
  334. while (true) {
  335. get_next_chip_address(info, &new_chip_address);
  336. chip_count++;
  337. uint8_t new_board_id = new_chip_address.board_id;
  338. uint8_t new_bcm250_id = new_chip_address.bcm250_id;
  339. uint8_t new_chip_id = new_chip_address.chip_id;
  340. /* enabled chip found */
  341. if (info->chipboard[new_board_id].bcm250[new_bcm250_id].chips[new_chip_id].status != DISABLED) {
  342. found = true;
  343. break;
  344. }
  345. /* we have run full loop chipboard */
  346. #ifdef MINER_X5
  347. if (chip_count == info->chipboard[board_id].chips_num)
  348. break;
  349. #endif
  350. #ifdef MINER_X6
  351. if (opt_bf16_power_management_disabled == false) {
  352. if ((info->chipboard[board_id].p_chain2_enabled == 0) &&
  353. (info->chipboard[board_id].power2_disabled == true)) {
  354. if (chip_count == info->chipboard[board_id].chips_num / 2)
  355. break;
  356. } else {
  357. if (chip_count == info->chipboard[board_id].chips_num)
  358. break;
  359. }
  360. } else {
  361. if (chip_count == info->chipboard[board_id].chips_num)
  362. break;
  363. }
  364. #endif
  365. }
  366. if ((found == true) &&
  367. (memcmp(&new_chip_address, &renonce_chip_address[board_id], sizeof(bf_chip_address_t)) != 0)) {
  368. board_id = new_chip_address.board_id;
  369. bcm250_id = new_chip_address.bcm250_id;
  370. chip_id = new_chip_address.chip_id;
  371. renonce_chip_address[board_id].bcm250_id = bcm250_id;
  372. renonce_chip_address[board_id].chip_id = chip_id;
  373. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status = UNINITIALIZED;
  374. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_nonce_time = time(NULL);
  375. gettimeofday(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status_time, NULL);
  376. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count = 0;
  377. applog(LOG_NOTICE, "%s: changed renonce chip address to: [%d:%d:%2d]",
  378. bitfury->drv->name,
  379. board_id, bcm250_id, chip_id);
  380. #ifdef FILELOG
  381. filelog(info, "%s: changed renonce chip address to: [%d:%d:%2d]",
  382. bitfury->drv->name,
  383. board_id, bcm250_id, chip_id);
  384. #endif
  385. return 0;
  386. } else {
  387. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status = DISABLED;
  388. applog(LOG_NOTICE, "%s: failed to find working renonce chip. disabling...",
  389. bitfury->drv->name);
  390. #ifdef FILELOG
  391. filelog(info, "%s: failed to find working renonce chip. disabling...",
  392. bitfury->drv->name);
  393. #endif
  394. return -1;
  395. }
  396. }
  397. static void increase_good_nonces(struct bitfury16_info *info, bf_chip_address_t chip_address)
  398. {
  399. uint8_t board_id = chip_address.board_id;
  400. uint8_t bcm250_id = chip_address.bcm250_id;
  401. uint8_t chip_id = chip_address.chip_id;
  402. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces_good_dx++;
  403. info->chipboard[board_id].bcm250[bcm250_id].nonces_good_dx++;
  404. info->chipboard[board_id].nonces_good_dx++;
  405. info->nonces_good_dx++;
  406. }
  407. static void increase_bad_nonces(struct bitfury16_info *info, bf_chip_address_t chip_address)
  408. {
  409. uint8_t board_id = chip_address.board_id;
  410. uint8_t bcm250_id = chip_address.bcm250_id;
  411. uint8_t chip_id = chip_address.chip_id;
  412. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces_bad_dx++;
  413. info->chipboard[board_id].bcm250[bcm250_id].nonces_bad_dx++;
  414. info->chipboard[board_id].nonces_bad_dx++;
  415. info->nonces_bad_dx++;
  416. }
  417. static void increase_re_nonces(struct bitfury16_info *info, bf_chip_address_t chip_address)
  418. {
  419. uint8_t board_id = chip_address.board_id;
  420. uint8_t bcm250_id = chip_address.bcm250_id;
  421. uint8_t chip_id = chip_address.chip_id;
  422. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces_re_dx++;
  423. info->chipboard[board_id].bcm250[bcm250_id].nonces_re_dx++;
  424. info->chipboard[board_id].nonces_re_dx++;
  425. info->nonces_re_dx++;
  426. }
  427. static void increase_re_good_nonces(struct bitfury16_info *info, bf_chip_address_t chip_address)
  428. {
  429. uint8_t board_id = chip_address.board_id;
  430. uint8_t bcm250_id = chip_address.bcm250_id;
  431. uint8_t chip_id = chip_address.chip_id;
  432. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces_re_good_dx++;
  433. if (renonce_chip(chip_address) == 0) {
  434. info->chipboard[board_id].bcm250[bcm250_id].nonces_re_good_dx++;
  435. info->chipboard[board_id].nonces_re_good_dx++;
  436. info->nonces_re_good_dx++;
  437. }
  438. }
  439. static void increase_re_bad_nonces(struct bitfury16_info *info, bf_chip_address_t chip_address)
  440. {
  441. uint8_t board_id = chip_address.board_id;
  442. uint8_t bcm250_id = chip_address.bcm250_id;
  443. uint8_t chip_id = chip_address.chip_id;
  444. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces_re_bad_dx++;
  445. if (renonce_chip(chip_address) == 0) {
  446. info->chipboard[board_id].bcm250[bcm250_id].nonces_re_bad_dx++;
  447. info->chipboard[board_id].nonces_re_bad_dx++;
  448. info->nonces_re_bad_dx++;
  449. }
  450. }
  451. static void increase_total_nonces(struct bitfury16_info *info, bf_chip_address_t chip_address)
  452. {
  453. uint8_t board_id = chip_address.board_id;
  454. uint8_t bcm250_id = chip_address.bcm250_id;
  455. uint8_t chip_id = chip_address.chip_id;
  456. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces_dx++;
  457. if ((renonce_chip(chip_address) == 0) ||
  458. (opt_bf16_renonce == RENONCE_DISABLED)) {
  459. info->chipboard[board_id].bcm250[bcm250_id].nonces_dx++;
  460. info->chipboard[board_id].nonces_dx++;
  461. info->nonces_dx++;
  462. }
  463. }
  464. static void increase_task_switch(struct bitfury16_info *info, bf_chip_address_t chip_address)
  465. {
  466. uint8_t board_id = chip_address.board_id;
  467. uint8_t bcm250_id = chip_address.bcm250_id;
  468. uint8_t chip_id = chip_address.chip_id;
  469. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].task_switch_dx++;
  470. info->chipboard[board_id].bcm250[bcm250_id].task_switch_dx++;
  471. info->chipboard[board_id].task_switch_dx++;
  472. info->task_switch_dx++;
  473. }
  474. static void increase_errors(struct bitfury16_info *info, bf_chip_address_t chip_address)
  475. {
  476. uint8_t board_id = chip_address.board_id;
  477. uint8_t bcm250_id = chip_address.bcm250_id;
  478. uint8_t chip_id = chip_address.chip_id;
  479. /* update timing interval */
  480. time_t curr_time = time(NULL);
  481. if (curr_time - info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_error_time <= 1)
  482. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].error_rate++;
  483. else
  484. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].error_rate = 0;
  485. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_error_time = curr_time;
  486. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].errors++;
  487. if ((renonce_chip(chip_address) == 0) ||
  488. (opt_bf16_renonce == RENONCE_DISABLED))
  489. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status = UNINITIALIZED;
  490. /* mark chip as FAILING if error rate too high*/
  491. if (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].error_rate >= CHIP_ERROR_LIMIT) {
  492. #ifdef FILELOG
  493. filelog(info, "BF16: chip [%d:%d:%2d] error rate too high: [%d], "
  494. "marked as failing, recovery_count: [%d]",
  495. board_id, bcm250_id, chip_id,
  496. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].error_rate,
  497. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count);
  498. #endif
  499. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status = FAILING;
  500. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].error_rate = 0;
  501. }
  502. }
  503. static uint8_t* init_channel_path(uint8_t board_id, uint8_t btc250_num, uint8_t* channel_depth, uint8_t channel_length)
  504. {
  505. uint8_t i, j;
  506. uint8_t* channel_path = cgcalloc(channel_length, sizeof(uint8_t));
  507. for (i = 0, j = 0; i < CHANNEL_DEPTH; i++) {
  508. if (bcm250_map[board_id][btc250_num].channel_path[i] != BF250_NONE)
  509. (*channel_depth)++;
  510. int8_t shift = 8*(j + 1) - 3*(i + 1);
  511. if (shift < 0) {
  512. channel_path[j] |= bcm250_map[board_id][btc250_num].channel_path[i] >> abs(shift);
  513. channel_path[j + 1] |= bcm250_map[board_id][btc250_num].channel_path[i] << (8 - abs(shift));
  514. j++;
  515. } else
  516. channel_path[j] |= bcm250_map[board_id][btc250_num].channel_path[i] << shift;
  517. }
  518. return channel_path;
  519. }
  520. static int8_t parse_chip_address(struct cgpu_info *bitfury, char* address, bf_chip_address_t* chip_address)
  521. {
  522. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  523. int8_t board_id = 0;
  524. int8_t bcm250_id = 0;
  525. int8_t chip_id = 0;
  526. char buff[16];
  527. if (address == NULL)
  528. return -1;
  529. /* board_id */
  530. char* start = strchr(address, '[');
  531. char* end = strchr(address, ':');
  532. if ((start == NULL) || (end == NULL))
  533. return -1;
  534. uint8_t len = end - start;
  535. memset(buff, 0, sizeof(buff));
  536. cg_memcpy(buff, start + 1, len);
  537. board_id = atoi(buff);
  538. /* bcm250_id */
  539. start = end;
  540. end = strchr(start + 1, ':');
  541. if (end == NULL)
  542. return -1;
  543. len = end - start;
  544. memset(buff, 0, sizeof(buff));
  545. cg_memcpy(buff, start + 1, len);
  546. bcm250_id = atoi(buff);
  547. /* chip_id */
  548. start = end;
  549. end = strchr(start + 1, ']');
  550. if (end == NULL)
  551. return -1;
  552. len = end - start;
  553. memset(buff, 0, sizeof(buff));
  554. cg_memcpy(buff, start + 1, len);
  555. chip_id = atoi(buff);
  556. if ((board_id < 0) || (board_id >= CHIPBOARD_NUM)) {
  557. applog(LOG_ERR, "%s: invalid board_id %d: [0 - %d] specified",
  558. bitfury->drv->name,
  559. board_id, CHIPBOARD_NUM);
  560. return -1;
  561. }
  562. if ((bcm250_id < 0) || (bcm250_id >= BCM250_NUM)) {
  563. applog(LOG_ERR, "%s: invalid bcm250_id %d: [0 - %d] specified",
  564. bitfury->drv->name,
  565. bcm250_id, BCM250_NUM);
  566. return -1;
  567. }
  568. uint8_t first_good_chip = info->chipboard[board_id].bcm250[bcm250_id].first_good_chip;
  569. uint8_t last_good_chip = info->chipboard[board_id].bcm250[bcm250_id].last_good_chip;
  570. if ((chip_id >= first_good_chip) && (chip_id < last_good_chip)) {
  571. chip_address->board_id = board_id;
  572. chip_address->bcm250_id = bcm250_id;
  573. chip_address->chip_id = chip_id;
  574. } else {
  575. applog(LOG_ERR, "%s: invalid chip_id %d: [%d - %d] specified",
  576. bitfury->drv->name,
  577. chip_id, first_good_chip, last_good_chip);
  578. return -1;
  579. }
  580. applog(LOG_NOTICE, "%s: parsed chip address: [%d:%d:%2d]",
  581. bitfury->drv->name,
  582. chip_address->board_id,
  583. chip_address->bcm250_id,
  584. chip_address->chip_id);
  585. return 0;
  586. }
  587. static void update_bcm250_map(struct cgpu_info *bitfury, uint8_t board_id)
  588. {
  589. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  590. #ifdef MINER_X5
  591. info->chipboard[board_id].board_type = CHIPBOARD_X5;
  592. switch (info->chipboard[board_id].board_ver) {
  593. /* 23 chip board version */
  594. case 5:
  595. info->chipboard[board_id].board_rev = CHIPBOARD_REV2;
  596. bcm250_map[board_id][0].last_good_chip = BF16_NUM - 2;
  597. bcm250_map[board_id][0].chips_num = BF16_NUM - 2;
  598. bcm250_map[board_id][1].first_good_chip = 6;
  599. bcm250_map[board_id][1].chips_num = 5;
  600. bcm250_map[board_id][2].last_good_chip = BF16_NUM - 2;
  601. bcm250_map[board_id][2].chips_num = BF16_NUM - 2;
  602. break;
  603. /* 24 chip board version */
  604. case 7:
  605. info->chipboard[board_id].board_rev = CHIPBOARD_REV2;
  606. bcm250_map[board_id][0].last_good_chip = BF16_NUM - 2;
  607. bcm250_map[board_id][0].chips_num = BF16_NUM - 2;
  608. bcm250_map[board_id][1].first_good_chip = 6;
  609. bcm250_map[board_id][1].chips_num = 5;
  610. bcm250_map[board_id][2].last_good_chip = BF16_NUM - 1;
  611. bcm250_map[board_id][2].chips_num = BF16_NUM - 1;
  612. break;
  613. /* 25 chip board version */
  614. case 9:
  615. info->chipboard[board_id].board_rev = CHIPBOARD_REV2;
  616. bcm250_map[board_id][0].last_good_chip = BF16_NUM - 1;
  617. bcm250_map[board_id][0].chips_num = BF16_NUM - 1;
  618. bcm250_map[board_id][1].first_good_chip = 6;
  619. bcm250_map[board_id][1].chips_num = 5;
  620. bcm250_map[board_id][2].last_good_chip = BF16_NUM - 1;
  621. bcm250_map[board_id][2].chips_num = BF16_NUM - 1;
  622. break;
  623. /* 26 chip board version */
  624. case 11:
  625. info->chipboard[board_id].board_rev = CHIPBOARD_REV2;
  626. bcm250_map[board_id][0].last_good_chip = BF16_NUM - 1;
  627. bcm250_map[board_id][0].chips_num = BF16_NUM - 1;
  628. bcm250_map[board_id][1].first_good_chip = 6;
  629. bcm250_map[board_id][1].chips_num = 5;
  630. break;
  631. /* 27 chip board version */
  632. case 13:
  633. info->chipboard[board_id].board_rev = CHIPBOARD_REV2;
  634. bcm250_map[board_id][1].first_good_chip = 6;
  635. bcm250_map[board_id][1].chips_num = 5;
  636. break;
  637. /* 26 chip board version - default */
  638. case 1:
  639. case 2:
  640. info->chipboard[board_id].board_rev = CHIPBOARD_REV1;
  641. default:
  642. break;
  643. }
  644. #endif
  645. #ifdef MINER_X6
  646. info->chipboard[board_id].board_type = CHIPBOARD_X6;
  647. switch (info->chipboard[board_id].board_ver) {
  648. /* 46 chip board version */
  649. case 4:
  650. info->chipboard[board_id].board_rev = CHIPBOARD_REV2;
  651. bcm250_map[board_id][0].last_good_chip = BF16_NUM - 2;
  652. bcm250_map[board_id][0].chips_num = BF16_NUM - 2;
  653. bcm250_map[board_id][1].first_good_chip = 1;
  654. bcm250_map[board_id][1].last_good_chip = BF16_NUM - 1;
  655. bcm250_map[board_id][1].chips_num = BF16_NUM - 2;
  656. bcm250_map[board_id][2].last_good_chip = 5;
  657. bcm250_map[board_id][2].chips_num = 5;
  658. bcm250_map[board_id][3].last_good_chip = BF16_NUM - 2;
  659. bcm250_map[board_id][3].chips_num = BF16_NUM - 2;
  660. bcm250_map[board_id][4].first_good_chip = 1;
  661. bcm250_map[board_id][4].last_good_chip = BF16_NUM - 1;
  662. bcm250_map[board_id][4].chips_num = BF16_NUM - 1;
  663. bcm250_map[board_id][5].last_good_chip = 5;
  664. bcm250_map[board_id][5].chips_num = 5;
  665. break;
  666. /* 48 chip board version */
  667. case 6:
  668. info->chipboard[board_id].board_rev = CHIPBOARD_REV2;
  669. bcm250_map[board_id][0].last_good_chip = BF16_NUM - 1;
  670. bcm250_map[board_id][0].chips_num = BF16_NUM - 1;
  671. bcm250_map[board_id][1].first_good_chip = 1;
  672. bcm250_map[board_id][1].last_good_chip = BF16_NUM - 1;
  673. bcm250_map[board_id][1].chips_num = BF16_NUM - 2;
  674. bcm250_map[board_id][2].last_good_chip = 5;
  675. bcm250_map[board_id][2].chips_num = 5;
  676. bcm250_map[board_id][3].last_good_chip = BF16_NUM - 1;
  677. bcm250_map[board_id][3].chips_num = BF16_NUM - 1;
  678. bcm250_map[board_id][4].first_good_chip = 1;
  679. bcm250_map[board_id][4].last_good_chip = BF16_NUM - 1;
  680. bcm250_map[board_id][4].chips_num = BF16_NUM - 1;
  681. bcm250_map[board_id][5].last_good_chip = 5;
  682. bcm250_map[board_id][5].chips_num = 5;
  683. break;
  684. /* 50 chip board version */
  685. case 8:
  686. info->chipboard[board_id].board_rev = CHIPBOARD_REV2;
  687. bcm250_map[board_id][0].last_good_chip = BF16_NUM - 1;
  688. bcm250_map[board_id][0].chips_num = BF16_NUM - 1;
  689. bcm250_map[board_id][1].last_good_chip = BF16_NUM - 1;
  690. bcm250_map[board_id][1].chips_num = BF16_NUM - 1;
  691. bcm250_map[board_id][2].last_good_chip = 5;
  692. bcm250_map[board_id][2].chips_num = 5;
  693. bcm250_map[board_id][3].last_good_chip = BF16_NUM - 1;
  694. bcm250_map[board_id][3].chips_num = BF16_NUM - 1;
  695. bcm250_map[board_id][4].last_good_chip = BF16_NUM - 1;
  696. bcm250_map[board_id][4].chips_num = BF16_NUM - 1;
  697. bcm250_map[board_id][5].last_good_chip = 5;
  698. bcm250_map[board_id][5].chips_num = 5;
  699. break;
  700. /* 52 chip board version */
  701. case 10:
  702. info->chipboard[board_id].board_rev = CHIPBOARD_REV2;
  703. bcm250_map[board_id][1].last_good_chip = BF16_NUM - 1;
  704. bcm250_map[board_id][1].chips_num = BF16_NUM - 1;
  705. bcm250_map[board_id][2].last_good_chip = 5;
  706. bcm250_map[board_id][2].chips_num = 5;
  707. bcm250_map[board_id][4].last_good_chip = BF16_NUM - 1;
  708. bcm250_map[board_id][4].chips_num = BF16_NUM - 1;
  709. bcm250_map[board_id][5].last_good_chip = 5;
  710. bcm250_map[board_id][5].chips_num = 5;
  711. break;
  712. /* 54 chip board version */
  713. case 12:
  714. info->chipboard[board_id].board_rev = CHIPBOARD_REV2;
  715. bcm250_map[board_id][2].last_good_chip = 5;
  716. bcm250_map[board_id][2].chips_num = 5;
  717. bcm250_map[board_id][5].last_good_chip = 5;
  718. bcm250_map[board_id][5].chips_num = 5;
  719. break;
  720. /* 46 chip board version */
  721. case 14:;
  722. info->chipboard[board_id].board_rev = CHIPBOARD_REV3;
  723. uint8_t bcm250_channel_path[3][CHANNEL_DEPTH] = {
  724. { BF250_CHAN1, BF250_CHAN1, BF250_LOCAL, BF250_NONE },
  725. { BF250_CHAN1, BF250_CHAN1, BF250_CHAN1, BF250_LOCAL },
  726. { BF250_CHAN1, BF250_CHAN1, BF250_CHAN2, BF250_LOCAL },
  727. };
  728. bcm250_map[board_id][0].first_good_chip = 0;
  729. bcm250_map[board_id][0].last_good_chip = 1;
  730. bcm250_map[board_id][0].chips_num = 1;
  731. bcm250_map[board_id][2].first_good_chip = 0;
  732. bcm250_map[board_id][2].last_good_chip = BF16_NUM;
  733. bcm250_map[board_id][2].chips_num = BF16_NUM;
  734. bcm250_map[board_id][3].first_good_chip = 0;
  735. bcm250_map[board_id][3].last_good_chip = 1;
  736. bcm250_map[board_id][3].chips_num = 1;
  737. cg_memcpy(bcm250_map[board_id][3].channel_path, bcm250_channel_path[0], sizeof(bcm250_map[board_id][3].channel_path));
  738. cg_memcpy(bcm250_map[board_id][4].channel_path, bcm250_channel_path[1], sizeof(bcm250_map[board_id][4].channel_path));
  739. bcm250_map[board_id][5].first_good_chip = 0;
  740. bcm250_map[board_id][5].last_good_chip = BF16_NUM;
  741. bcm250_map[board_id][5].chips_num = BF16_NUM;
  742. cg_memcpy(bcm250_map[board_id][5].channel_path, bcm250_channel_path[2], sizeof(bcm250_map[board_id][5].channel_path));
  743. break;
  744. /* 52 chip board version - default */
  745. case 3:
  746. info->chipboard[board_id].board_rev = CHIPBOARD_REV1;
  747. default:
  748. break;
  749. }
  750. #endif
  751. }
  752. static void reinit_x5(struct bitfury16_info *info, bool chip_reinit)
  753. {
  754. uint8_t board_id, bcm250_id, chip_id;
  755. /* reinit board chips */
  756. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  757. for (bcm250_id = 0; bcm250_id < BCM250_NUM; bcm250_id++) {
  758. uint8_t first_good_chip = info->chipboard[board_id].bcm250[bcm250_id].first_good_chip;
  759. uint8_t last_good_chip = info->chipboard[board_id].bcm250[bcm250_id].last_good_chip;
  760. for (chip_id = first_good_chip; chip_id < last_good_chip; chip_id++) {
  761. if (chip_reinit == true) {
  762. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count = 0;
  763. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].error_rate = 0;
  764. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status = UNINITIALIZED;
  765. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_nonce_time = time(NULL);
  766. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_error_time = time(NULL);
  767. gettimeofday(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status_time, NULL);
  768. gettimeofday(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].switch_time, NULL);
  769. } else {
  770. if (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count < CHIP_ERROR_FAIL_LIMIT)
  771. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status = UNINITIALIZED;
  772. }
  773. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_nonce_time = time(NULL);
  774. gettimeofday(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status_time, NULL);
  775. gettimeofday(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].switch_time, NULL);
  776. }
  777. }
  778. }
  779. /* send reset to all boards */
  780. spi_emit_reset(SPI_CHANNEL1);
  781. spi_emit_reset(SPI_CHANNEL2);
  782. }
  783. static void init_x5(struct cgpu_info *bitfury)
  784. {
  785. uint8_t board_id, bcm250_id, chip_id;
  786. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  787. info->chipboard = cgcalloc(CHIPBOARD_NUM, sizeof(bf_chipboard_t));
  788. /* channel size in bytes */
  789. info->channel_length = (CHANNEL_DEPTH * 3) / 8 + 1;
  790. info->ialarm_count = 1;
  791. info->work_list = workd_list_init();
  792. info->stale_work_list = workd_list_init();
  793. info->noncework_list = noncework_list_init();
  794. if (opt_bf16_renonce != RENONCE_DISABLED) {
  795. info->renoncework_list = renoncework_list_init();
  796. info->renonce_id = 1;
  797. info->renonce_list = renonce_list_init();
  798. }
  799. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  800. /* detect board */
  801. char buff[256];
  802. memset(buff, 0, sizeof(buff));
  803. device_ctrl_txrx(board_id + 1, 0, F_BDET, buff);
  804. parse_board_detect(bitfury, board_id, buff);
  805. if (info->chipboard[board_id].detected == true) {
  806. applog(LOG_NOTICE, "%s: BOARD%d detected", bitfury->drv->name, board_id + 1);
  807. info->chipboard[board_id].bcm250 = cgcalloc(BCM250_NUM, sizeof(bf_bcm250_t));
  808. cmd_buffer_init(&info->chipboard[board_id].cmd_buffer);
  809. get_board_info(bitfury, board_id);
  810. update_bcm250_map(bitfury, board_id);
  811. info->chipboard_num++;
  812. info->chipboard[board_id].bcm250_num = BCM250_NUM;
  813. cg_memcpy(&info->chipboard[board_id].pid, &pid, sizeof(bf_pid_t));
  814. uint8_t chips_num = 0;
  815. for (bcm250_id = 0; bcm250_id < BCM250_NUM; bcm250_id++) {
  816. info->chipboard[board_id].bcm250[bcm250_id].channel_path = init_channel_path(board_id, bcm250_id,
  817. &info->chipboard[board_id].bcm250[bcm250_id].channel_depth, info->channel_length);
  818. info->chipboard[board_id].bcm250[bcm250_id].first_good_chip = bcm250_map[board_id][bcm250_id].first_good_chip;
  819. info->chipboard[board_id].bcm250[bcm250_id].last_good_chip = bcm250_map[board_id][bcm250_id].last_good_chip;
  820. info->chipboard[board_id].bcm250[bcm250_id].chips_num = bcm250_map[board_id][bcm250_id].chips_num;
  821. chips_num += bcm250_map[board_id][bcm250_id].chips_num;
  822. uint8_t first_good_chip = info->chipboard[board_id].bcm250[bcm250_id].first_good_chip;
  823. uint8_t last_good_chip = info->chipboard[board_id].bcm250[bcm250_id].last_good_chip;
  824. for (chip_id = first_good_chip; chip_id < last_good_chip; chip_id++) {
  825. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status = UNINITIALIZED;
  826. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_nonce_time = time(NULL);
  827. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonce_list = nonce_list_init();
  828. gettimeofday(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status_time, NULL);
  829. gettimeofday(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].switch_time, NULL);
  830. }
  831. }
  832. info->chipboard[board_id].chips_num = chips_num;
  833. info->chips_num += info->chipboard[board_id].chips_num;
  834. } else
  835. applog(LOG_NOTICE, "%s: BOARD%d not found", bitfury->drv->name, board_id + 1);
  836. applog(LOG_INFO, "%s: initialized board X5.%d", bitfury->drv->name, board_id);
  837. }
  838. applog(LOG_INFO, "%s: initialized X5", bitfury->drv->name);
  839. }
  840. static void deinit_x5(struct cgpu_info *bitfury)
  841. {
  842. uint8_t board_id, bcm250_id, chip_id;
  843. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  844. workd_list_deinit(info->work_list, bitfury);
  845. workd_list_deinit(info->stale_work_list, bitfury);
  846. noncework_list_deinit(info->noncework_list);
  847. if (opt_bf16_renonce != RENONCE_DISABLED) {
  848. renoncework_list_deinit(info->renoncework_list);
  849. info->renonce_id = 1;
  850. renonce_list_deinit(info->renonce_list);
  851. }
  852. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  853. for (bcm250_id = 0; bcm250_id < BCM250_NUM; bcm250_id++) {
  854. free(info->chipboard[board_id].bcm250[bcm250_id].channel_path);
  855. uint8_t first_good_chip = info->chipboard[board_id].bcm250[bcm250_id].first_good_chip;
  856. uint8_t last_good_chip = info->chipboard[board_id].bcm250[bcm250_id].last_good_chip;
  857. for (chip_id = first_good_chip; chip_id < last_good_chip; chip_id++)
  858. nonce_list_deinit(info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonce_list);
  859. }
  860. free(info->chipboard[board_id].bcm250);
  861. cmd_buffer_deinit(&info->chipboard[board_id].cmd_buffer);
  862. }
  863. free(info->chipboard);
  864. }
  865. static void bitfury16_set_clock(struct cgpu_info *bitfury)
  866. {
  867. uint8_t board_id, bcm250_id, chip_id;
  868. struct bitfury16_info *info = (struct bitfury16_info *)bitfury->device_data;
  869. /* send reset to all boards */
  870. spi_emit_reset(SPI_CHANNEL1);
  871. spi_emit_reset(SPI_CHANNEL2);
  872. /* board loop */
  873. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  874. applog(LOG_NOTICE, "%s: CHIPBOARD [%d]:", bitfury->drv->name, board_id);
  875. /* concentrator loop */
  876. for (bcm250_id = 0; bcm250_id < BCM250_NUM; bcm250_id++) {
  877. applog(LOG_NOTICE, "%s: BCM250 [%d]:", bitfury->drv->name, bcm250_id);
  878. spi_emit_reset(board_id + 1);
  879. /* build channel */
  880. create_channel(board_id + 1, info->chipboard[board_id].bcm250[bcm250_id].channel_path, info->channel_length);
  881. uint8_t channel_depth = info->chipboard[board_id].bcm250[bcm250_id].channel_depth;
  882. uint8_t first_good_chip = info->chipboard[board_id].bcm250[bcm250_id].first_good_chip;
  883. uint8_t last_good_chip = info->chipboard[board_id].bcm250[bcm250_id].last_good_chip;
  884. uint8_t result;
  885. bool fail;
  886. /* chips loop */
  887. for (chip_id = first_good_chip; chip_id < last_good_chip; chip_id++) {
  888. fail = false;
  889. bf_chip_address_t chip_address = { board_id, bcm250_id, chip_id };
  890. result = send_toggle(board_id + 1, channel_depth, chip_address);
  891. if (result != 0)
  892. fail = true;
  893. result = set_clock(board_id + 1, channel_depth, chip_address, bf16_chip_clock);
  894. if ((result != 0) && (fail != true))
  895. fail = true;
  896. if (fail == false)
  897. applog(LOG_NOTICE, "%s: CHIP [%2d]: OK", bitfury->drv->name, chip_id);
  898. else
  899. applog(LOG_NOTICE, "%s: CHIP [%2d]: FAIL", bitfury->drv->name, chip_id);
  900. }
  901. /* destroy channel */
  902. destroy_channel(board_id + 1, info->chipboard[board_id].bcm250[bcm250_id].channel_depth);
  903. }
  904. }
  905. deinit_x5(bitfury);
  906. }
  907. static void bitfury16_test_chip(struct cgpu_info *bitfury, bf_chip_address_t chip_address)
  908. {
  909. struct bitfury16_info *info = (struct bitfury16_info *)bitfury->device_data;
  910. uint8_t board_id = chip_address.board_id;
  911. uint8_t bcm250_id = chip_address.bcm250_id;
  912. uint8_t chip_id = chip_address.chip_id;
  913. /* send reset to board */
  914. spi_emit_reset(board_id + 1);
  915. /* build channel */
  916. create_channel(board_id + 1, info->chipboard[board_id].bcm250[bcm250_id].channel_path, info->channel_length);
  917. uint8_t channel_depth = info->chipboard[board_id].bcm250[bcm250_id].channel_depth;
  918. uint8_t result;
  919. bool fail = false;
  920. result = send_toggle(board_id + 1, channel_depth, chip_address);
  921. if (result != 0)
  922. fail = true;
  923. result = set_clock(board_id + 1, channel_depth, chip_address, bf16_chip_clock);
  924. if ((result != 0) && (fail != true))
  925. fail = true;
  926. if (fail == false)
  927. applog(LOG_NOTICE, "%s: CHIP [%2d]: OK", bitfury->drv->name, chip_id);
  928. else
  929. applog(LOG_NOTICE, "%s: CHIP [%2d]: FAIL", bitfury->drv->name, chip_id);
  930. /* destroy channel */
  931. destroy_channel(board_id + 1, info->chipboard[board_id].bcm250[bcm250_id].channel_depth);
  932. deinit_x5(bitfury);
  933. }
  934. static void bitfury16_identify(__maybe_unused struct cgpu_info *bitfury)
  935. {
  936. }
  937. static void set_fan_speed(struct cgpu_info *bitfury)
  938. {
  939. struct bitfury16_info *info = (struct bitfury16_info *)bitfury->device_data;
  940. uint8_t board_id;
  941. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  942. if (info->chipboard[board_id].detected == true) {
  943. if (opt_bf16_fan_speed == -1) {
  944. if (device_uart_transfer(board_id + 1, "F") < 0)
  945. quit(1, "%s: %s() failed to set BOARD%d fan speed",
  946. bitfury->drv->name, __func__, board_id + 1);
  947. applog(LOG_INFO, "%s: set BOARD%d fan speed to auto mode",
  948. bitfury->drv->name, board_id + 1);
  949. } else {
  950. char uart_cmd[8];
  951. sprintf(uart_cmd, "F:%d", opt_bf16_fan_speed);
  952. if (device_uart_transfer(board_id + 1, uart_cmd) < 0)
  953. quit(1, "%s: %s() failed to set BOARD%d fan speed",
  954. bitfury->drv->name, __func__, board_id + 1);
  955. applog(LOG_INFO, "%s: set BOARD%d fan speed to [%d]",
  956. bitfury->drv->name, board_id + 1, opt_bf16_fan_speed);
  957. }
  958. }
  959. }
  960. }
  961. static void bitfury16_detect(bool hotplug)
  962. {
  963. struct cgpu_info *bitfury = NULL;
  964. struct bitfury16_info *info = NULL;
  965. uint8_t board_id, bcm250_id, chip_id;
  966. if (hotplug)
  967. return;
  968. bitfury = cgmalloc(sizeof(struct cgpu_info));
  969. if (unlikely(!bitfury))
  970. quit(1, "%s: %s() failed to malloc bitfury",
  971. bitfury->drv->name, __func__);
  972. bitfury->drv = &bitfury16_drv;
  973. bitfury->deven = DEV_ENABLED;
  974. bitfury->threads = 1;
  975. info = cgmalloc(sizeof(struct bitfury16_info));
  976. if (unlikely(!info))
  977. quit(1, "%s: %s() failed to malloc info",
  978. bitfury->drv->name, __func__);
  979. bitfury->device_data = info;
  980. /* manual PID option */
  981. #ifdef MINER_X5
  982. manual_pid_enabled = opt_bf16_manual_pid_enabled;
  983. #endif
  984. #ifdef MINER_X6
  985. manual_pid_enabled = !opt_bf16_manual_pid_disabled;
  986. #endif
  987. /* renonce chip address and renonce chip clock */
  988. if (opt_bf16_renonce != RENONCE_DISABLED) {
  989. if (opt_bf16_renonce_clock != NULL)
  990. bf16_renonce_chip_clock = strtol(opt_bf16_renonce_clock, NULL, 16);
  991. } else
  992. /* default chip clock if renonce is disabled */
  993. bf16_chip_clock = 0x2d;
  994. /* general chip clock */
  995. if (opt_bf16_clock != NULL)
  996. bf16_chip_clock = strtol(opt_bf16_clock, NULL, 16);
  997. else if ((opt_bf16_set_clock == true) || (opt_bf16_test_chip != NULL))
  998. /* default chip clock if set_clock option is set */
  999. bf16_chip_clock = 0x20;
  1000. /* open devices */
  1001. if (open_spi_device(SPI_CHANNEL1) < 0)
  1002. quit(1, "%s: %s() failed to open [%s] device",
  1003. bitfury->drv->name, __func__, spi0_device_name);
  1004. applog(LOG_INFO, "%s: opened [%s] device", bitfury->drv->name, spi0_device_name);
  1005. if (open_spi_device(SPI_CHANNEL2) < 0)
  1006. quit(1, "%s: %s() failed to open [%s] device",
  1007. bitfury->drv->name, __func__, spi1_device_name);
  1008. applog(LOG_INFO, "%s: opened [%s] device", bitfury->drv->name, spi1_device_name);
  1009. if (open_ctrl_device() < 0)
  1010. quit(1, "%s: %s() failed to open [%s] device",
  1011. bitfury->drv->name, __func__, ctrl_device_name);
  1012. applog(LOG_INFO, "%s: opened [%s] device", bitfury->drv->name, ctrl_device_name);
  1013. if (open_uart_device(UART_CHANNEL1) < 0)
  1014. quit(1, "%s: %s() failed to open [%s] device",
  1015. bitfury->drv->name, __func__, uart1_device_name);
  1016. applog(LOG_INFO, "%s: opened [%s] device", bitfury->drv->name, uart1_device_name);
  1017. if (open_uart_device(UART_CHANNEL2) < 0)
  1018. quit(1, "%s: %s() failed to open [%s] device",
  1019. bitfury->drv->name, __func__, uart2_device_name);
  1020. applog(LOG_INFO, "%s: opened [%s] device", bitfury->drv->name, uart2_device_name);
  1021. init_x5(bitfury);
  1022. /* send reset to boards */
  1023. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  1024. if (info->chipboard[board_id].detected == true) {
  1025. device_ctrl_transfer(board_id + 1, 1, F_BRST);
  1026. cgsleep_us(POWER_WAIT_INTERVAL);
  1027. device_ctrl_transfer(board_id + 1, 0, F_BRST);
  1028. cgsleep_us(POWER_WAIT_INTERVAL);
  1029. applog(LOG_INFO, "%s: sent reset to BOARD%d",
  1030. bitfury->drv->name, board_id + 1);
  1031. }
  1032. }
  1033. /* check if board power is present */
  1034. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  1035. /* read hw sensor data */
  1036. char buff[256];
  1037. if (info->chipboard[board_id].detected == true) {
  1038. memset(buff, 0, sizeof(buff));
  1039. if (device_uart_txrx(board_id + 1, "S", buff) < 0)
  1040. quit(1, "%s: %s() failed to get BOARD%d status",
  1041. bitfury->drv->name, __func__, board_id + 1);
  1042. if (parse_hwstats(info, board_id, buff) < 0)
  1043. applog(LOG_ERR, "%s: failed to parse hw stats",
  1044. bitfury->drv->name);
  1045. /* disable board if power voltage is incorrect */
  1046. if ((info->chipboard[board_id].u_board < 10.0) ||
  1047. (info->chipboard[board_id].u_board > 15.0)) {
  1048. /* concentrator loop */
  1049. for (bcm250_id = 0; bcm250_id < BCM250_NUM; bcm250_id++) {
  1050. uint8_t first_good_chip = info->chipboard[board_id].bcm250[bcm250_id].first_good_chip;
  1051. uint8_t last_good_chip = info->chipboard[board_id].bcm250[bcm250_id].last_good_chip;
  1052. /* chips loop */
  1053. for (chip_id = first_good_chip; chip_id < last_good_chip; chip_id++)
  1054. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status = DISABLED;
  1055. }
  1056. applog(LOG_ERR, "%s: incorrect U board detected [%.1f] on BOARD%d, "
  1057. "disabling board...",
  1058. bitfury->drv->name,
  1059. info->chipboard[board_id].u_board,
  1060. board_id + 1);
  1061. } else {
  1062. info->chipboard[board_id].active = true;
  1063. info->active_chipboard_num++;
  1064. }
  1065. }
  1066. }
  1067. /* enable power chain */
  1068. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  1069. if (enable_power_chain(bitfury, board_id, 0) < 0)
  1070. info->chipboard[board_id].detected = false;
  1071. else {
  1072. #ifdef MINER_X5
  1073. info->chipboard[board_id].power_enable_time = time(NULL);
  1074. info->chipboard[board_id].power_disable_count = 1;
  1075. #endif
  1076. #ifdef MINER_X6
  1077. info->chipboard[board_id].power1_enable_time = time(NULL);
  1078. info->chipboard[board_id].power2_enable_time = time(NULL);
  1079. info->chipboard[board_id].power1_disable_count = 1;
  1080. info->chipboard[board_id].power2_disable_count = 1;
  1081. #endif
  1082. }
  1083. }
  1084. /* wait for power chain to enable */
  1085. cgsleep_us(POWER_WAIT_INTERVAL);
  1086. if (opt_bf16_set_clock == true) {
  1087. applog(LOG_INFO, "%s: setting clock [%02x] to all chips",
  1088. bitfury->drv->name, bf16_chip_clock);
  1089. bitfury16_set_clock(bitfury);
  1090. quit(0, "Done.");
  1091. }
  1092. if (opt_bf16_test_chip != NULL) {
  1093. bf_chip_address_t chip_address = { 0, 0, 0 };
  1094. if (parse_chip_address(bitfury, opt_bf16_test_chip, &chip_address) < 0) {
  1095. quit(1, "%s: %s() error parsing chip address...",
  1096. bitfury->drv->name, __func__);
  1097. }
  1098. applog(LOG_INFO, "%s: testing communicaton with chip [%d:%d:%2d]",
  1099. bitfury->drv->name,
  1100. chip_address.board_id,
  1101. chip_address.bcm250_id,
  1102. chip_address.chip_id);
  1103. bitfury16_test_chip(bitfury, chip_address);
  1104. quit(0, "Done.");
  1105. }
  1106. /* fan speed */
  1107. if ((opt_bf16_fan_speed != -1) &&
  1108. (manual_pid_enabled == true)) {
  1109. manual_pid_enabled = false;
  1110. }
  1111. set_fan_speed(bitfury);
  1112. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  1113. if (info->chipboard[board_id].detected == true) {
  1114. /* target temp */
  1115. if (opt_bf16_target_temp == -1) {
  1116. if (device_uart_transfer(board_id + 1, "T") < 0)
  1117. quit(1, "%s: %s() failed to set BOARD%d target temp",
  1118. bitfury->drv->name, __func__, board_id + 1);
  1119. applog(LOG_INFO, "%s: set BOARD%d target temp to default value",
  1120. bitfury->drv->name, board_id + 1);
  1121. } else {
  1122. char uart_cmd[8];
  1123. sprintf(uart_cmd, "T:%d", opt_bf16_target_temp);
  1124. if (device_uart_transfer(board_id + 1, uart_cmd) < 0)
  1125. quit(1, "%s: %s() failed to set BOARD%d target temp",
  1126. bitfury->drv->name, __func__, board_id + 1);
  1127. applog(LOG_INFO, "%s: set BOARD%d target temp to [%d]",
  1128. bitfury->drv->name, board_id + 1, opt_bf16_target_temp);
  1129. }
  1130. /* alarm temp */
  1131. if (opt_bf16_alarm_temp == -1) {
  1132. if (device_uart_transfer(board_id + 1, "C") < 0)
  1133. quit(1, "%s: %s() failed to set BOARD%d alarm temp",
  1134. bitfury->drv->name, __func__, board_id + 1);
  1135. applog(LOG_INFO, "%s: set BOARD%d alarm temp to default value",
  1136. bitfury->drv->name, board_id + 1);
  1137. } else {
  1138. char uart_cmd[8];
  1139. sprintf(uart_cmd, "C:%d", opt_bf16_alarm_temp);
  1140. if (device_uart_transfer(board_id + 1, uart_cmd) < 0)
  1141. quit(1, "%s: %s() failed to set BOARD%d alarm temp",
  1142. bitfury->drv->name, __func__, board_id + 1);
  1143. applog(LOG_INFO, "%s: set BOARD%d alarm temp to [%d]",
  1144. bitfury->drv->name, board_id + 1, opt_bf16_alarm_temp);
  1145. }
  1146. }
  1147. }
  1148. /* count number of renonce chips */
  1149. if (opt_bf16_renonce != RENONCE_DISABLED) {
  1150. info->renonce_chips = opt_bf16_renonce;
  1151. uint8_t renonce_chips = 0;
  1152. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  1153. if ((info->chipboard[board_id].detected == true) &&
  1154. (info->chipboard[board_id].active == true)) {
  1155. if (opt_bf16_renonce == RENONCE_ONE_CHIP) {
  1156. if (renonce_chips != info->renonce_chips)
  1157. renonce_chips++;
  1158. else {
  1159. renonce_chip_address[board_id].board_id = -1;
  1160. renonce_chip_address[board_id].bcm250_id = -1;
  1161. renonce_chip_address[board_id].chip_id = -1;
  1162. }
  1163. } else
  1164. renonce_chips++;
  1165. } else {
  1166. renonce_chip_address[board_id].board_id = -1;
  1167. renonce_chip_address[board_id].bcm250_id = -1;
  1168. renonce_chip_address[board_id].chip_id = -1;
  1169. }
  1170. }
  1171. if (renonce_chips != info->renonce_chips) {
  1172. applog(LOG_ERR, "%s: expected to find [%d] renonce chips, but found only [%d]",
  1173. bitfury->drv->name, opt_bf16_renonce, renonce_chips);
  1174. info->renonce_chips = renonce_chips;
  1175. }
  1176. } else {
  1177. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  1178. renonce_chip_address[board_id].board_id = -1;
  1179. renonce_chip_address[board_id].bcm250_id = -1;
  1180. renonce_chip_address[board_id].chip_id = -1;
  1181. }
  1182. }
  1183. /* correct renonce chip address */
  1184. if (opt_bf16_renonce != RENONCE_DISABLED) {
  1185. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  1186. if ((info->chipboard[board_id].detected == true) &&
  1187. (info->chipboard[board_id].active == true) &&
  1188. (renonce_chip_address[board_id].board_id != -1)) {
  1189. bcm250_id = renonce_chip_address[board_id].bcm250_id;
  1190. uint8_t first_good_chip = info->chipboard[board_id].bcm250[bcm250_id].first_good_chip;
  1191. uint8_t last_good_chip = info->chipboard[board_id].bcm250[bcm250_id].last_good_chip;
  1192. if (renonce_chip_address[board_id].chip_id >= last_good_chip)
  1193. renonce_chip_address[board_id].chip_id = last_good_chip - 1;
  1194. else if (renonce_chip_address[board_id].chip_id < first_good_chip)
  1195. renonce_chip_address[board_id].chip_id = first_good_chip;
  1196. }
  1197. }
  1198. }
  1199. #ifdef FILELOG
  1200. info->logfile = fopen(LOGFILE, "a");
  1201. if (info->logfile == NULL)
  1202. applog(LOG_ERR, "%s: failed to open logfile [%s]: %s",
  1203. bitfury->drv->name, LOGFILE, strerror(errno));
  1204. else
  1205. mutex_init(&info->logfile_mutex);
  1206. #endif
  1207. /* exit if no boards present */
  1208. if ((info->chipboard_num == 0) ||
  1209. (info->active_chipboard_num == 0)) {
  1210. deinit_x5(bitfury);
  1211. /* close devices */
  1212. close_spi_device(SPI_CHANNEL1);
  1213. close_spi_device(SPI_CHANNEL2);
  1214. close_ctrl_device();
  1215. close_uart_device(UART_CHANNEL1);
  1216. close_uart_device(UART_CHANNEL2);
  1217. #ifdef FILELOG
  1218. fclose(info->logfile);
  1219. #endif
  1220. applog(LOG_ERR, "%s: no boards present. exiting...",
  1221. bitfury->drv->name);
  1222. free(info);
  1223. free(bitfury);
  1224. return;
  1225. }
  1226. mutex_init(&info->nonces_good_lock);
  1227. if (!add_cgpu(bitfury))
  1228. quit(1, "%s: %s() failed to add_cgpu",
  1229. bitfury->drv->name, __func__);
  1230. info->initialised = true;
  1231. applog(LOG_INFO, "%s: chip driver initialized", bitfury->drv->name);
  1232. #ifdef FILELOG
  1233. filelog(info, "%s: cgminer started", bitfury->drv->name);
  1234. #endif
  1235. }
  1236. static uint8_t chip_task_update(struct cgpu_info *bitfury, bf_chip_address_t chip_address)
  1237. {
  1238. uint8_t i;
  1239. int8_t ret = 0;
  1240. bf_works_t work;
  1241. time_t curr_time_t;
  1242. struct timeval curr_time;
  1243. uint8_t board_id = chip_address.board_id;
  1244. uint8_t bcm250_id = chip_address.bcm250_id;
  1245. uint8_t chip_id = chip_address.chip_id;
  1246. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  1247. bf_cmd_buffer_t* cmd_buffer = &info->chipboard[board_id].cmd_buffer;
  1248. if (cmd_buffer->status != EMPTY)
  1249. return -1;
  1250. switch (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status) {
  1251. /* fill chip buffer with toggle task */
  1252. case UNINITIALIZED:
  1253. applog(LOG_DEBUG, "%s: chipworker_thr: chip [%d:%d:%2d], prepare toggle_cmd",
  1254. bitfury->drv->name,
  1255. board_id, bcm250_id, chip_id);
  1256. uint8_t toggle[4] = { 0xa5, 0x00, 0x00, 0x02 };
  1257. ret = cmd_buffer_push(cmd_buffer,
  1258. info->chipboard[board_id].bcm250[bcm250_id].channel_depth, chip_address, chip_address,
  1259. work, 0, CHIP_CMD_TOGGLE, 3, toggle);
  1260. if (ret < 0)
  1261. applog(LOG_ERR, "%s: chipworker_thr: chip [%d:%d:%2d], error prepare toggle_cmd",
  1262. bitfury->drv->name,
  1263. board_id, bcm250_id, chip_id);
  1264. break;
  1265. /* fill chip buffer with set clock task */
  1266. case TOGGLE_SET:
  1267. applog(LOG_DEBUG, "%s: chipworker_thr: chip [%d:%d:%2d], prepare set_clock_cmd",
  1268. bitfury->drv->name,
  1269. board_id, bcm250_id, chip_id);
  1270. uint8_t clock_buf[4];
  1271. memset(clock_buf, 0, sizeof(clock_buf));
  1272. /* init renonce chip with lower clock */
  1273. if ((renonce_chip(chip_address) == 1) &&
  1274. (opt_bf16_renonce != RENONCE_DISABLED)) {
  1275. gen_clock_data(bf16_renonce_chip_clock, 1, clock_buf);
  1276. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].clock = bf16_renonce_chip_clock;
  1277. } else {
  1278. gen_clock_data(bf16_chip_clock, 1, clock_buf);
  1279. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].clock = bf16_chip_clock;
  1280. }
  1281. ret = cmd_buffer_push(cmd_buffer,
  1282. info->chipboard[board_id].bcm250[bcm250_id].channel_depth, chip_address, chip_address,
  1283. work, 0, CHIP_CMD_SET_CLOCK, 3, clock_buf);
  1284. if (ret < 0)
  1285. applog(LOG_ERR, "%s: chipworker_thr: chip [%d:%d:%2d], error prepare set_clock_cmd",
  1286. bitfury->drv->name,
  1287. board_id, bcm250_id, chip_id);
  1288. break;
  1289. /* fill chip buffer with chip mask */
  1290. case CLOCK_SET:
  1291. applog(LOG_DEBUG, "%s: chipworker_thr: chip [%d:%d:%2d], prepare set_mask_cmd",
  1292. bitfury->drv->name,
  1293. board_id, bcm250_id, chip_id);
  1294. uint8_t noncemask[4];
  1295. memset(noncemask, 0, sizeof(noncemask));
  1296. for (i = 0; i < 4; i++)
  1297. noncemask[i] = (mask >> (8*(4 - i - 1))) & 0xff;
  1298. ret = cmd_buffer_push(cmd_buffer,
  1299. info->chipboard[board_id].bcm250[bcm250_id].channel_depth, chip_address, chip_address,
  1300. work, 0, CHIP_CMD_SET_MASK, 3, noncemask);
  1301. if (ret < 0)
  1302. applog(LOG_ERR, "%s: chipworker_thr: chip [%d:%d:%2d], error prepare set_mask_cmd",
  1303. bitfury->drv->name,
  1304. board_id, bcm250_id, chip_id);
  1305. break;
  1306. /* fill chip buffer with new task */
  1307. case MASK_SET:
  1308. if ((renonce_chip(chip_address) == 0) ||
  1309. (opt_bf16_renonce == RENONCE_DISABLED)) {
  1310. applog(LOG_DEBUG, "%s: chipworker_thr: chip [%d:%d:%2d], prepare send_task_cmd",
  1311. bitfury->drv->name,
  1312. board_id, bcm250_id, chip_id);
  1313. L_LOCK(info->work_list);
  1314. L_LOCK(info->stale_work_list);
  1315. if (info->work_list->count > 0) {
  1316. memset(info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].cwork.task, 0,
  1317. sizeof(info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].cwork.task));
  1318. bf_data_t* wdata = info->work_list->head;
  1319. workd_list_push(info->stale_work_list, WORKD(wdata));
  1320. workd_list_remove(info->work_list, &info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].cwork);
  1321. gen_task_data(info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].cwork.payload.midstate,
  1322. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].cwork.payload.m7,
  1323. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].cwork.payload.ntime,
  1324. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].cwork.payload.nbits, mask,
  1325. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].cwork.task);
  1326. ret = cmd_buffer_push(cmd_buffer,
  1327. info->chipboard[board_id].bcm250[bcm250_id].channel_depth, chip_address, chip_address,
  1328. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].cwork, 0,
  1329. CHIP_CMD_TASK_WRITE, 79,
  1330. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].cwork.task);
  1331. if (ret < 0)
  1332. applog(LOG_ERR, "%s: chipworker_thr: chip [%d:%d:%2d], error prepare send_task_cmd",
  1333. bitfury->drv->name,
  1334. board_id, bcm250_id, chip_id);
  1335. }
  1336. #if 0
  1337. else
  1338. applog(LOG_ERR, "%s: chipworker_thr: chip [%d:%d:%2d], error prepare send_task_cmd: no works available",
  1339. bitfury->drv->name,
  1340. board_id, bcm250_id, chip_id);
  1341. #endif
  1342. L_UNLOCK(info->stale_work_list);
  1343. L_UNLOCK(info->work_list);
  1344. }
  1345. break;
  1346. /* fill chip buffer with check status task */
  1347. case TASK_SENT:
  1348. gettimeofday(&curr_time, NULL);
  1349. if (((renonce_chip(chip_address) == 0) ||
  1350. (opt_bf16_renonce == RENONCE_DISABLED)) &&
  1351. (timediff_us(info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status_time, curr_time) > CHIP_TASK_STATUS_INTERVAL)) {
  1352. applog(LOG_DEBUG, "%s: chipworker_thr: chip [%d:%d:%2d], prepare task_status_cmd",
  1353. bitfury->drv->name,
  1354. board_id, bcm250_id, chip_id);
  1355. ret = cmd_buffer_push(cmd_buffer,
  1356. info->chipboard[board_id].bcm250[bcm250_id].channel_depth, chip_address, chip_address,
  1357. work, 0, CHIP_CMD_TASK_STATUS, 0, NULL);
  1358. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status_time.tv_sec = curr_time.tv_sec;
  1359. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status_time.tv_usec = curr_time.tv_usec;
  1360. if (ret < 0)
  1361. applog(LOG_ERR, "%s: chipworker_thr: chip [%d:%d:%2d], error prepare task_status_cmd",
  1362. bitfury->drv->name,
  1363. board_id, bcm250_id, chip_id);
  1364. }
  1365. break;
  1366. /* fill chip buffer with read nonces task */
  1367. case TASK_SWITCHED:
  1368. if ((renonce_chip(chip_address) == 0) ||
  1369. (opt_bf16_renonce == RENONCE_DISABLED)) {
  1370. applog(LOG_DEBUG, "%s: chipworker_thr: chip [%d:%d:%2d], prepare read_nonce_cmd",
  1371. bitfury->drv->name,
  1372. board_id, bcm250_id, chip_id);
  1373. ret = cmd_buffer_push(cmd_buffer,
  1374. info->chipboard[board_id].bcm250[bcm250_id].channel_depth, chip_address, chip_address,
  1375. work, 0, CHIP_CMD_READ_NONCE, 0, NULL);
  1376. if (ret < 0)
  1377. applog(LOG_ERR, "%s: chipworker_thr: chip [%d:%d:%2d], error prepare read_nonce_cmd",
  1378. bitfury->drv->name,
  1379. board_id, bcm250_id, chip_id);
  1380. }
  1381. break;
  1382. /* mark chip as UNINITIALIZED and start all over again */
  1383. case FAILING:
  1384. curr_time_t = time(NULL);
  1385. time_t time_diff = curr_time_t - info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_error_time;
  1386. if (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count < CHIP_ERROR_FAIL_LIMIT) {
  1387. if (time_diff >= info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count * CHIP_RECOVERY_INTERVAL) {
  1388. /* change renonce chip address if RENONCE_CHIP_ERROR_FAIL_LIMIT reached */
  1389. if ((renonce_chip(chip_address) == 1) &&
  1390. (opt_bf16_renonce != RENONCE_DISABLED)) {
  1391. if (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count == RENONCE_CHIP_ERROR_FAIL_LIMIT) {
  1392. applog(LOG_ERR, "%s: chipworker_thr: renonce chip [%d:%d:%2d] failed. trying to switch to another one...",
  1393. bitfury->drv->name,
  1394. chip_address.board_id,
  1395. chip_address.bcm250_id,
  1396. chip_address.chip_id);
  1397. if (change_renonce_chip_address(bitfury, chip_address) == 0) {
  1398. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status = UNINITIALIZED;
  1399. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_nonce_time = curr_time_t;
  1400. gettimeofday(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status_time, NULL);
  1401. }
  1402. } else {
  1403. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status = UNINITIALIZED;
  1404. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_nonce_time = curr_time_t;
  1405. gettimeofday(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status_time, NULL);
  1406. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count++;
  1407. }
  1408. } else {
  1409. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status = UNINITIALIZED;
  1410. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_nonce_time = curr_time_t;
  1411. gettimeofday(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status_time, NULL);
  1412. gettimeofday(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].switch_time, NULL);
  1413. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count++;
  1414. #ifdef FILELOG
  1415. filelog(info, "BF16: chip [%d:%d:%2d] recovered time_diff: [%d], "
  1416. "recovery_count: [%d], error_rate: [%d]",
  1417. board_id, bcm250_id, chip_id,
  1418. time_diff,
  1419. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count,
  1420. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].error_rate);
  1421. #endif
  1422. }
  1423. }
  1424. }
  1425. /* mark chip as DISABLED and never communicate to it again */
  1426. else if (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status != DISABLED) {
  1427. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status = DISABLED;
  1428. #ifdef FILELOG
  1429. filelog(info, "BF16: disabling chip [%d:%d:%2d] recovery_count: [%d], error_rate: [%d]",
  1430. board_id, bcm250_id, chip_id,
  1431. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count,
  1432. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].error_rate);
  1433. #endif
  1434. }
  1435. break;
  1436. case DISABLED:
  1437. default:
  1438. break;
  1439. }
  1440. return ret;
  1441. }
  1442. static uint8_t renonce_task_update_loop(struct cgpu_info *bitfury, uint8_t board_id,
  1443. bf_renonce_stage_t stage, uint8_t renonce_count)
  1444. {
  1445. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  1446. bf_cmd_buffer_t* cmd_buffer = &info->chipboard[board_id].cmd_buffer;
  1447. uint8_t bcm250_id = renonce_chip_address[board_id].bcm250_id;
  1448. uint8_t chip_id = renonce_chip_address[board_id].chip_id;
  1449. uint8_t nonces = 0;
  1450. if (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status < FAILING) {
  1451. L_LOCK(info->renonce_list);
  1452. bf_data_t* rdata = info->renonce_list->head;
  1453. while ((rdata != NULL) && (nonces < renonce_count)) {
  1454. uint8_t ret = 1;
  1455. if ((RENONCE(rdata)->sent == false) &&
  1456. (RENONCE(rdata)->stage == stage)) {
  1457. /* generate work mask */
  1458. uint32_t nonce_mask = gen_mask(RENONCE(rdata)->nonce, mask_bits);
  1459. nonce_mask = ntohl(nonce_mask);
  1460. switch (RENONCE(rdata)->stage) {
  1461. case RENONCE_STAGE0:
  1462. case RENONCE_STAGE2:
  1463. /* generate chip work with new mask */
  1464. cg_memcpy(RENONCE(rdata)->owork.task + 19*4,
  1465. &nonce_mask, sizeof(nonce_mask));
  1466. /* send task and read nonces at the same time */
  1467. ret = cmd_buffer_push(cmd_buffer,
  1468. info->chipboard[board_id].bcm250[bcm250_id].channel_depth,
  1469. renonce_chip_address[board_id], RENONCE(rdata)->src_address,
  1470. RENONCE(rdata)->owork, RENONCE(rdata)->id,
  1471. (CHIP_CMD_TASK_WRITE | CHIP_CMD_TASK_SWITCH | CHIP_CMD_READ_NONCE), 79,
  1472. RENONCE(rdata)->owork.task);
  1473. break;
  1474. case RENONCE_STAGE1:
  1475. case RENONCE_STAGE3:
  1476. /* generate chip work with new mask */
  1477. cg_memcpy(RENONCE(rdata)->cwork.task + 19*4,
  1478. &nonce_mask, sizeof(nonce_mask));
  1479. /* send task and read nonces at the same time */
  1480. ret = cmd_buffer_push(cmd_buffer,
  1481. info->chipboard[board_id].bcm250[bcm250_id].channel_depth,
  1482. renonce_chip_address[board_id], RENONCE(rdata)->src_address,
  1483. RENONCE(rdata)->cwork, RENONCE(rdata)->id,
  1484. (CHIP_CMD_TASK_WRITE | CHIP_CMD_TASK_SWITCH | CHIP_CMD_READ_NONCE), 79,
  1485. RENONCE(rdata)->cwork.task);
  1486. break;
  1487. case RENONCE_STAGE_FINISHED:
  1488. default:
  1489. break;
  1490. }
  1491. }
  1492. if (ret == 0) {
  1493. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status = TASK_SWITCHED;
  1494. RENONCE(rdata)->sent = true;
  1495. RENONCE(rdata)->received = false;
  1496. nonces++;
  1497. }
  1498. rdata = rdata->next;
  1499. }
  1500. L_UNLOCK(info->renonce_list);
  1501. }
  1502. return nonces;
  1503. }
  1504. static void renonce_task_update(struct cgpu_info *bitfury, uint8_t board_id, uint8_t renonce_count)
  1505. {
  1506. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  1507. bf_cmd_buffer_t* cmd_buffer = &info->chipboard[board_id].cmd_buffer;
  1508. bf_works_t work;
  1509. uint8_t i;
  1510. uint8_t bcm250_id = renonce_chip_address[board_id].bcm250_id;
  1511. uint8_t chip_id = renonce_chip_address[board_id].chip_id;
  1512. cmd_buffer_push_create_channel(cmd_buffer,
  1513. info->chipboard[board_id].bcm250[bcm250_id].channel_path,
  1514. info->channel_length);
  1515. if (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status < FAILING) {
  1516. uint8_t toggle[4] = { 0xa5, 0x00, 0x00, 0x02 };
  1517. cmd_buffer_push(cmd_buffer,
  1518. info->chipboard[board_id].bcm250[bcm250_id].channel_depth,
  1519. renonce_chip_address[board_id], renonce_chip_address[board_id],
  1520. work, 0, CHIP_CMD_TOGGLE, 3, toggle);
  1521. uint8_t clock_buf[4];
  1522. memset(clock_buf, 0, sizeof(clock_buf));
  1523. gen_clock_data(bf16_renonce_chip_clock, 1, clock_buf);
  1524. cmd_buffer_push(cmd_buffer,
  1525. info->chipboard[board_id].bcm250[bcm250_id].channel_depth,
  1526. renonce_chip_address[board_id], renonce_chip_address[board_id],
  1527. work, 0, CHIP_CMD_SET_CLOCK, 3, clock_buf);
  1528. uint8_t noncemask[4];
  1529. memset(noncemask, 0, sizeof(noncemask));
  1530. for (i = 0; i < 4; i++)
  1531. noncemask[i] = (mask >> (8*(4 - i - 1))) & 0xff;
  1532. cmd_buffer_push(cmd_buffer,
  1533. info->chipboard[board_id].bcm250[bcm250_id].channel_depth,
  1534. renonce_chip_address[board_id], renonce_chip_address[board_id],
  1535. work, 0, CHIP_CMD_SET_MASK, 3, noncemask);
  1536. bf_renonce_stage_t stage = RENONCE_STAGE0;
  1537. while ((renonce_count > 0) && (stage != RENONCE_STAGE_FINISHED)) {
  1538. renonce_count -= renonce_task_update_loop(bitfury, board_id, stage++, renonce_count);
  1539. }
  1540. }
  1541. cmd_buffer_push_destroy_channel(cmd_buffer,
  1542. info->chipboard[board_id].
  1543. bcm250[renonce_chip_address[board_id].bcm250_id].channel_depth);
  1544. }
  1545. static void fill_cmd_buffer_loop(struct cgpu_info *bitfury, uint8_t board_id, bool do_renonce, uint16_t renonce_count)
  1546. {
  1547. uint8_t bcm250_id, chip_id;
  1548. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  1549. bf_cmd_buffer_t* cmd_buffer = &info->chipboard[board_id].cmd_buffer;
  1550. /* concentrator loop */
  1551. for (bcm250_id = 0; bcm250_id < BCM250_NUM; bcm250_id++) {
  1552. uint8_t first_good_chip = info->chipboard[board_id].bcm250[bcm250_id].first_good_chip;
  1553. uint8_t last_good_chip = info->chipboard[board_id].bcm250[bcm250_id].last_good_chip;
  1554. cmd_buffer_push_create_channel(cmd_buffer,
  1555. info->chipboard[board_id].bcm250[bcm250_id].channel_path,
  1556. info->channel_length);
  1557. /* chips loop */
  1558. for (chip_id = first_good_chip; chip_id < last_good_chip; chip_id++) {
  1559. bf_chip_address_t chip_address = { board_id, bcm250_id, chip_id };
  1560. chip_task_update(bitfury, chip_address);
  1561. }
  1562. cmd_buffer_push_destroy_channel(cmd_buffer,
  1563. info->chipboard[board_id].bcm250[bcm250_id].channel_depth);
  1564. }
  1565. if (do_renonce == true) {
  1566. uint8_t count = (cmd_buffer->free_bytes - (2 + 11 + 11 + 11 + 8)) / 136;
  1567. if (count < renonce_count) {
  1568. if (count > 0)
  1569. renonce_task_update(bitfury, board_id, count);
  1570. } else if (renonce_count > 0)
  1571. renonce_task_update(bitfury, board_id, renonce_count);
  1572. }
  1573. cmd_buffer->status = TX_READY;
  1574. }
  1575. static void fill_cmd_buffer(struct cgpu_info *bitfury, uint8_t board_id)
  1576. {
  1577. static uint8_t do_renonce[CHIPBOARD_NUM];
  1578. uint16_t renonce_count = 0;
  1579. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  1580. bf_cmd_buffer_t* cmd_buffer = &info->chipboard[board_id].cmd_buffer;
  1581. if (cmd_buffer->status == EMPTY) {
  1582. if ((opt_bf16_renonce == RENONCE_DISABLED) ||
  1583. (renonce_chip_address[board_id].board_id == -1)) {
  1584. fill_cmd_buffer_loop(bitfury, board_id, false, 0);
  1585. } else {
  1586. L_LOCK(info->renonce_list);
  1587. bf_data_t* rdata = info->renonce_list->head;
  1588. while (rdata != NULL) {
  1589. if (RENONCE(rdata)->sent == false)
  1590. renonce_count++;
  1591. rdata = rdata->next;
  1592. }
  1593. L_UNLOCK(info->renonce_list);
  1594. if (do_renonce[board_id] < RENONCE_SEND) {
  1595. fill_cmd_buffer_loop(bitfury, board_id, true, renonce_count);
  1596. do_renonce[board_id]++;
  1597. } else {
  1598. if (renonce_count >= RENONCE_COUNT) {
  1599. renonce_task_update(bitfury, board_id, RENONCE_COUNT);
  1600. do_renonce[board_id] = 0;
  1601. cmd_buffer->status = TX_READY;
  1602. } else {
  1603. fill_cmd_buffer_loop(bitfury, board_id, true, renonce_count);
  1604. do_renonce[board_id]++;
  1605. }
  1606. }
  1607. }
  1608. }
  1609. }
  1610. static uint8_t update_chip_status(struct cgpu_info *bitfury, bf_cmd_status_t cmd_status)
  1611. {
  1612. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  1613. uint8_t board_id = cmd_status.chip_address.board_id;
  1614. uint8_t bcm250_id = cmd_status.chip_address.bcm250_id;
  1615. uint8_t chip_id = cmd_status.chip_address.chip_id;
  1616. switch (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status) {
  1617. case UNINITIALIZED:
  1618. case TOGGLE_SET:
  1619. case CLOCK_SET:
  1620. case MASK_SET:
  1621. applog(LOG_DEBUG, "%s: chipworker_thr: chip [%d:%d:%2d], send_cmd [%s]",
  1622. bitfury->drv->name,
  1623. board_id, bcm250_id, chip_id,
  1624. get_cmd_description(cmd_status.cmd_code));
  1625. /* update chip status */
  1626. if (cmd_status.checksum_error == false)
  1627. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status++;
  1628. else {
  1629. #ifndef DISABLE_SEND_CMD_ERROR
  1630. applog(LOG_ERR, "%s: chipworker_thr: chip [%d:%d:%2d]: error send_cmd [%s]. "
  1631. "checksum: expected: [%02x]; received: [%02x]",
  1632. bitfury->drv->name,
  1633. board_id, bcm250_id, chip_id,
  1634. get_cmd_description(cmd_status.cmd_code),
  1635. cmd_status.checksum_expected,
  1636. cmd_status.checksum_received);
  1637. #endif
  1638. /* increase error counters */
  1639. increase_errors(info, cmd_status.chip_address);
  1640. }
  1641. break;
  1642. case TASK_SENT:
  1643. applog(LOG_DEBUG, "%s: chipworker_thr: chip [%d:%d:%2d], send_cmd [%s]",
  1644. bitfury->drv->name,
  1645. board_id, bcm250_id, chip_id,
  1646. get_cmd_description(cmd_status.cmd_code));
  1647. /* read task status from chip*/
  1648. if (cmd_status.checksum_error == false) {
  1649. uint8_t new_buff = ((cmd_status.status & 0x0f) == 0x0f) ? 1 : 0;
  1650. /* status cmd counter */
  1651. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status_cmd_dx++;
  1652. info->chipboard[board_id].bcm250[bcm250_id].status_cmd_dx++;
  1653. info->chipboard[board_id].status_cmd_dx++;
  1654. info->status_cmd_dx++;
  1655. /* check if chip task has switched */
  1656. if (new_buff != info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].curr_buff) {
  1657. gettimeofday(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].switch_time, NULL);
  1658. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].curr_buff = new_buff;
  1659. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status++;
  1660. /* task switch counter */
  1661. increase_task_switch(info, cmd_status.chip_address);
  1662. } else {
  1663. /* task not switched */
  1664. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status_cmd_none_dx++;
  1665. info->chipboard[board_id].bcm250[bcm250_id].status_cmd_none_dx++;
  1666. info->chipboard[board_id].status_cmd_none_dx++;
  1667. info->status_cmd_none_dx++;
  1668. /* check if chip hang */
  1669. struct timeval curr_time;
  1670. gettimeofday(&curr_time, NULL);
  1671. if ((timediff_us(info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].switch_time, curr_time) > CHIP_TASK_SWITCH_INTERVAL) &&
  1672. (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status < FAILING)) {
  1673. increase_errors(info, cmd_status.chip_address);
  1674. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status = FAILING;
  1675. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_error_time = time(NULL);
  1676. applog(LOG_ERR, "%s: nonceworker_thr: chip [%d:%d:%2d], "
  1677. "failed: no task switch during last [%.3f] seconds",
  1678. bitfury->drv->name,
  1679. board_id, bcm250_id, chip_id, CHIP_TASK_SWITCH_INTERVAL / 1000000.0);
  1680. #ifdef FILELOG
  1681. filelog(info, "BF16: no task switch for chip [%d:%d:%2d], "
  1682. "error count: [%d], recovery_count: [%d], error_rate: [%d]",
  1683. board_id, bcm250_id, chip_id,
  1684. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].errors,
  1685. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count,
  1686. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].error_rate);
  1687. #endif
  1688. }
  1689. }
  1690. } else {
  1691. #ifndef DISABLE_SEND_CMD_ERROR
  1692. applog(LOG_ERR, "%s: chipworker_thr: chip [%d:%d:%2d]: error send_cmd [%s]. "
  1693. "checksum: expected: [%02x]; received: [%02x]",
  1694. bitfury->drv->name,
  1695. board_id, bcm250_id, chip_id,
  1696. get_cmd_description(cmd_status.cmd_code),
  1697. cmd_status.checksum_expected,
  1698. cmd_status.checksum_received);
  1699. #endif
  1700. /* increase error counters */
  1701. increase_errors(info, cmd_status.chip_address);
  1702. }
  1703. break;
  1704. case TASK_SWITCHED:
  1705. applog(LOG_DEBUG, "%s: chipworker_thr: chip [%d:%d:%2d], send_cmd [%s]",
  1706. bitfury->drv->name,
  1707. board_id, bcm250_id, chip_id,
  1708. get_cmd_description(cmd_status.cmd_code));
  1709. if (cmd_status.checksum_error == false) {
  1710. if ((renonce_chip(cmd_status.chip_address) == 1) &&
  1711. (opt_bf16_renonce != RENONCE_DISABLED)) {
  1712. /* task switch counter */
  1713. if (cmd_status.cmd_code & CHIP_CMD_READ_NONCE)
  1714. increase_task_switch(info, cmd_status.chip_address);
  1715. } else if (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status < FAILING) {
  1716. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].task_processed++;
  1717. if (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].task_processed >= CHIP_RESTART_LIMIT) {
  1718. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status = UNINITIALIZED;
  1719. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].task_processed = 0;
  1720. } else
  1721. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status = MASK_SET;
  1722. }
  1723. return 1;
  1724. } else {
  1725. #ifndef DISABLE_SEND_CMD_ERROR
  1726. applog(LOG_ERR, "%s: chipworker_thr: chip [%d:%d:%2d]: error send_cmd [%s]. "
  1727. "checksum: expected: [%02x]; received: [%02x]",
  1728. bitfury->drv->name,
  1729. board_id, bcm250_id, chip_id,
  1730. get_cmd_description(cmd_status.cmd_code),
  1731. cmd_status.checksum_expected,
  1732. cmd_status.checksum_received);
  1733. #endif
  1734. /* increase error counters */
  1735. increase_errors(info, cmd_status.chip_address);
  1736. }
  1737. break;
  1738. case FAILING:
  1739. case DISABLED:
  1740. default:
  1741. break;
  1742. }
  1743. return 0;
  1744. }
  1745. static uint8_t process_nonces(struct cgpu_info *bitfury, bf_cmd_status_t cmd_status, uint32_t* nonces)
  1746. {
  1747. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  1748. uint8_t i;
  1749. uint32_t found_nonces[12];
  1750. uint8_t board_id = cmd_status.chip_address.board_id;
  1751. uint8_t bcm250_id = cmd_status.chip_address.bcm250_id;
  1752. uint8_t chip_id = cmd_status.chip_address.chip_id;
  1753. if ((cmd_status.checksum_error == false) &&
  1754. (cmd_status.nonce_checksum_error == false)) {
  1755. cg_memcpy(info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].rx, nonces, sizeof(found_nonces));
  1756. uint8_t found = find_nonces(info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].rx,
  1757. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].rx_prev,
  1758. found_nonces);
  1759. /* check if chip is still mining */
  1760. if (found == 0) {
  1761. time_t curr_time = time(NULL);
  1762. time_t time_diff = curr_time - info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_nonce_time;
  1763. if ((renonce_chip(cmd_status.chip_address) == 1) &&
  1764. (opt_bf16_renonce != RENONCE_DISABLED)) {
  1765. if ((time_diff >= RENONCE_CHIP_FAILING_INTERVAL) &&
  1766. (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status < FAILING)) {
  1767. increase_errors(info, cmd_status.chip_address);
  1768. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status = FAILING;
  1769. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_error_time = curr_time;
  1770. applog(LOG_ERR, "%s: nonceworker_thr: renonce chip [%d:%d:%2d] "
  1771. "failed: no good nonces during last [%.1f] seconds",
  1772. bitfury->drv->name,
  1773. board_id, bcm250_id, chip_id, RENONCE_CHIP_FAILING_INTERVAL);
  1774. #ifdef FILELOG
  1775. filelog(info, "BF16: no good nonces from renonce chip [%d:%d:%2d], "
  1776. "error count: [%d] recovery_count: [%d], error_rate: [%d]",
  1777. board_id, bcm250_id, chip_id,
  1778. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].errors,
  1779. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count,
  1780. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].error_rate);
  1781. #endif
  1782. }
  1783. } else {
  1784. if ((time_diff >= CHIP_FAILING_INTERVAL) &&
  1785. (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status < FAILING)) {
  1786. increase_errors(info, cmd_status.chip_address);
  1787. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status = FAILING;
  1788. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_error_time = curr_time;
  1789. applog(LOG_ERR, "%s: nonceworker_thr: chip [%d:%d:%2d], "
  1790. "failed: no good nonces during last [%.1f] seconds",
  1791. bitfury->drv->name,
  1792. board_id, bcm250_id, chip_id, CHIP_FAILING_INTERVAL);
  1793. #ifdef FILELOG
  1794. filelog(info, "BF16: no good nonces from chip [%d:%d:%2d], "
  1795. "error count: [%d], recovery_count: [%d], error_rate: [%d]",
  1796. board_id, bcm250_id, chip_id,
  1797. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].errors,
  1798. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count,
  1799. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].error_rate);
  1800. #endif
  1801. }
  1802. }
  1803. }
  1804. /* increase stage if no nonces received */
  1805. if ((renonce_chip(cmd_status.chip_address) == 1) &&
  1806. (opt_bf16_renonce != RENONCE_DISABLED)) {
  1807. L_LOCK(info->renonce_list);
  1808. bf_data_t* rdata = info->renonce_list->head;
  1809. while (rdata != NULL) {
  1810. if (RENONCE(rdata)->id == cmd_status.id) {
  1811. if (found == 0) {
  1812. RENONCE(rdata)->stage++;
  1813. RENONCE(rdata)->sent = false;
  1814. /* clear renonces list if we are running too slow */
  1815. if ((RENONCE(rdata)->stage >= RENONCE_STAGE2) &&
  1816. (info->renonce_list->count > RENONCE_STAGE2_LIMIT)) {
  1817. info->unmatched++;
  1818. increase_re_bad_nonces(info, RENONCE(rdata)->src_address);
  1819. bf_data_t* rndata = rdata->next;
  1820. renonce_list_remove(info->renonce_list, rdata);
  1821. rdata = rndata;
  1822. continue;
  1823. }
  1824. if ((RENONCE(rdata)->stage >= RENONCE_STAGE3) &&
  1825. (info->renonce_list->count > RENONCE_STAGE3_LIMIT)) {
  1826. info->unmatched++;
  1827. increase_re_bad_nonces(info, RENONCE(rdata)->src_address);
  1828. bf_data_t* rndata = rdata->next;
  1829. renonce_list_remove(info->renonce_list, rdata);
  1830. rdata = rndata;
  1831. continue;
  1832. }
  1833. /* remove expired renonce */
  1834. if (RENONCE(rdata)->stage == RENONCE_STAGE_FINISHED) {
  1835. info->unmatched++;
  1836. increase_re_bad_nonces(info, RENONCE(rdata)->src_address);
  1837. bf_data_t* rndata = rdata->next;
  1838. renonce_list_remove(info->renonce_list, rdata);
  1839. rdata = rndata;
  1840. continue;
  1841. }
  1842. }
  1843. RENONCE(rdata)->received = true;
  1844. break;
  1845. }
  1846. rdata = rdata->next;
  1847. }
  1848. L_UNLOCK(info->renonce_list);
  1849. }
  1850. bf_list_t* nonce_list = info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonce_list;
  1851. for (i = 0; i < found; i++) {
  1852. L_LOCK(nonce_list);
  1853. int8_t res = nonce_list_push(nonce_list, found_nonces[i]);
  1854. L_UNLOCK(nonce_list);
  1855. if (res < 0)
  1856. continue;
  1857. increase_total_nonces(info, cmd_status.chip_address);
  1858. /* check if nonce has errors and add it to renonce list */
  1859. if ((opt_bf16_renonce != RENONCE_DISABLED) &&
  1860. (renonce_chip(cmd_status.chip_address) == 0) &&
  1861. (found_nonces[i] & 0xfff00000) == 0xaaa00000) {
  1862. increase_re_nonces(info, cmd_status.src_address);
  1863. L_LOCK(info->renonce_list);
  1864. if (info->renonce_list->count < RENONCE_QUEUE_LEN) {
  1865. renonce_list_push(info->renonce_list,
  1866. info->renonce_id++,
  1867. found_nonces[i],
  1868. cmd_status.chip_address,
  1869. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].cwork,
  1870. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].owork);
  1871. } else
  1872. increase_re_bad_nonces(info, cmd_status.chip_address);
  1873. L_UNLOCK(info->renonce_list);
  1874. applog(LOG_DEBUG, "%s: chipworker_thr: pushing renonce task: nonce: [%08x]",
  1875. bitfury->drv->name,
  1876. found_nonces[i]);
  1877. continue;
  1878. }
  1879. /* add nonces to noncework list */
  1880. if ((renonce_chip(cmd_status.chip_address) == 1) &&
  1881. (opt_bf16_renonce != RENONCE_DISABLED)) {
  1882. L_LOCK(info->renoncework_list);
  1883. renoncework_list_push(info->renoncework_list, cmd_status.chip_address, found_nonces[i]);
  1884. L_UNLOCK(info->renoncework_list);
  1885. } else {
  1886. L_LOCK(info->noncework_list);
  1887. noncework_list_push(info->noncework_list,
  1888. cmd_status.chip_address, cmd_status.src_address,
  1889. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].cwork,
  1890. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].owork,
  1891. found_nonces[i]);
  1892. L_UNLOCK(info->noncework_list);
  1893. }
  1894. applog(LOG_DEBUG, "%s: chipworker_thr: pushing nonce task: nonce: [%08x]",
  1895. bitfury->drv->name,
  1896. found_nonces[i]);
  1897. }
  1898. /* rotate works and buffers */
  1899. cg_memcpy(info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].rx_prev,
  1900. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].rx, sizeof(found_nonces));
  1901. cg_memcpy(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].owork,
  1902. &info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].cwork, sizeof(bf_works_t));
  1903. /* remove old nonces from nonce list */
  1904. L_LOCK(nonce_list);
  1905. if ((renonce_chip(cmd_status.chip_address) == 1) &&
  1906. (opt_bf16_renonce != RENONCE_DISABLED)) {
  1907. while (nonce_list->count > RENONCE_CHIP_QUEUE_LEN)
  1908. nonce_list_pop(nonce_list);
  1909. } else {
  1910. while (nonce_list->count > NONCE_CHIP_QUEUE_LEN)
  1911. nonce_list_pop(nonce_list);
  1912. }
  1913. L_UNLOCK(nonce_list);
  1914. } else {
  1915. if (cmd_status.checksum_error != 0) {
  1916. #ifndef DISABLE_SEND_CMD_ERROR
  1917. applog(LOG_ERR, "%s: chipworker_thr: chip [%d:%d:%2d]: error send_cmd [%s]. "
  1918. "checksum: expected: [%02x]; received: [%02x]",
  1919. bitfury->drv->name,
  1920. board_id, bcm250_id, chip_id,
  1921. get_cmd_description(cmd_status.cmd_code),
  1922. cmd_status.checksum_expected,
  1923. cmd_status.checksum_received);
  1924. #endif
  1925. /* increase error counters */
  1926. increase_errors(info, cmd_status.chip_address);
  1927. }
  1928. if (cmd_status.nonce_checksum_error != 0) {
  1929. if ((renonce_chip(cmd_status.chip_address) == 0) ||
  1930. (opt_bf16_renonce == RENONCE_DISABLED)) {
  1931. applog(LOG_ERR, "%s: chipworker_thr: chip [%d:%d:%2d]: error receiving data. "
  1932. "nonce checksum: expected: [%02x]; received: [%02x]",
  1933. bitfury->drv->name,
  1934. board_id, bcm250_id, chip_id,
  1935. cmd_status.checksum_expected,
  1936. cmd_status.checksum_received);
  1937. /* increase error counters */
  1938. increase_errors(info, cmd_status.chip_address);
  1939. }
  1940. }
  1941. }
  1942. return 0;
  1943. }
  1944. /* routine sending-receiving data to chipboard */
  1945. static void process_cmd_buffer(struct cgpu_info *bitfury, uint8_t board_id)
  1946. {
  1947. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  1948. uint8_t i;
  1949. bf_cmd_status_t cmd_status;
  1950. bf_cmd_buffer_t* cmd_buffer = &info->chipboard[board_id].cmd_buffer;
  1951. if (cmd_buffer->status == EXECUTED) {
  1952. /* process extracted data */
  1953. uint16_t cmd_number = cmd_buffer->cmd_list->count;
  1954. for (i = 0; i < cmd_number; i++) {
  1955. uint32_t nonces[12];
  1956. cmd_buffer_pop(cmd_buffer, &cmd_status, nonces);
  1957. if ((cmd_status.cmd_code == CHIP_CMD_CREATE_CHANNEL) ||
  1958. (cmd_status.cmd_code == CHIP_CMD_TASK_SWITCH))
  1959. continue;
  1960. uint8_t task_switch = update_chip_status(bitfury, cmd_status);
  1961. /* analyze nonces */
  1962. if ((cmd_status.cmd_code & CHIP_CMD_READ_NONCE) &&
  1963. (task_switch == 1)) {
  1964. process_nonces(bitfury, cmd_status, nonces);
  1965. }
  1966. }
  1967. cmd_buffer_clear(cmd_buffer);
  1968. }
  1969. }
  1970. static void *bitfury_chipworker(void *userdata)
  1971. {
  1972. struct cgpu_info *bitfury = (struct cgpu_info *)userdata;
  1973. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  1974. uint8_t board_id;
  1975. applog(LOG_INFO, "%s: started chipworker thread", bitfury->drv->name);
  1976. while (bitfury->shutdown == false) {
  1977. if (info->initialised) {
  1978. break;
  1979. }
  1980. cgsleep_us(30);
  1981. }
  1982. /* send reset sequence to boards */
  1983. spi_emit_reset(SPI_CHANNEL1);
  1984. spi_emit_reset(SPI_CHANNEL2);
  1985. while (bitfury->shutdown == false) {
  1986. if ((info->a_temp == false) &&
  1987. (info->a_ichain == false)) {
  1988. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  1989. if (info->chipboard[board_id].detected == true) {
  1990. /* prepare send buffer */
  1991. struct timeval start_time, stop_time;
  1992. gettimeofday(&start_time, NULL);
  1993. if (info->chipboard[board_id].cmd_buffer.status == EMPTY)
  1994. fill_cmd_buffer(bitfury, board_id);
  1995. gettimeofday(&stop_time, NULL);
  1996. #if 0
  1997. applog(LOG_ERR, "%s: chipworker_thr: board %d buffer prepare: time elapsed: [%.6f]",
  1998. bitfury->drv->name, board_id, timediff(start_time, stop_time));
  1999. #endif
  2000. gettimeofday(&start_time, NULL);
  2001. /* send buffer to chipboard */
  2002. if (info->chipboard[board_id].cmd_buffer.status == TX_READY) {
  2003. spi_emit_reset(board_id + 1);
  2004. cmd_buffer_exec(board_id + 1, &info->chipboard[board_id].cmd_buffer);
  2005. info->chipboard[board_id].bytes_transmitted_dx += info->chipboard[board_id].cmd_buffer.tx_offset;
  2006. info->chipboard[board_id].bytes_transmitted += info->chipboard[board_id].cmd_buffer.tx_offset;
  2007. }
  2008. gettimeofday(&stop_time, NULL);
  2009. #if 0
  2010. applog(LOG_ERR, "%s: chipworker_thr: board %d TX/RX time: [%.6f]",
  2011. bitfury->drv->name, board_id, timediff(start_time, stop_time));
  2012. #endif
  2013. gettimeofday(&start_time, NULL);
  2014. /* analyze received data */
  2015. if (info->chipboard[board_id].cmd_buffer.status == EXECUTED) {
  2016. process_cmd_buffer(bitfury, board_id);
  2017. }
  2018. gettimeofday(&stop_time, NULL);
  2019. #if 0
  2020. applog(LOG_ERR, "%s: chipworker_thr: board %d buffer processing: time elapsed: [%.6f]",
  2021. bitfury->drv->name, board_id, timediff(start_time, stop_time));
  2022. #endif
  2023. }
  2024. }
  2025. } else
  2026. cgsleep_us(CHIPWORKER_DELAY);
  2027. }
  2028. applog(LOG_INFO, "%s: chipworker_thr: exiting...", bitfury->drv->name);
  2029. return NULL;
  2030. }
  2031. static int16_t cleanup_older(struct cgpu_info *bitfury)
  2032. {
  2033. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  2034. time_t curr_time = time(NULL);
  2035. uint16_t released = 0;
  2036. /* clear stale work list */
  2037. L_LOCK(info->stale_work_list);
  2038. bf_data_t* wdata = info->stale_work_list->head;
  2039. while (wdata != NULL) {
  2040. if (curr_time - WORKD(wdata)->generated >= WORK_TIMEOUT) {
  2041. workd_list_pop(info->stale_work_list, bitfury);
  2042. released++;
  2043. } else
  2044. break;
  2045. wdata = info->stale_work_list->head;
  2046. }
  2047. L_UNLOCK(info->stale_work_list);
  2048. applog(LOG_INFO, "%s: released %d works", bitfury->drv->name, released);
  2049. return released;
  2050. }
  2051. static void *bitfury_nonceworker(void *userdata)
  2052. {
  2053. struct cgpu_info *bitfury = (struct cgpu_info *)userdata;
  2054. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  2055. applog(LOG_INFO, "%s: started nonceworker thread", bitfury->drv->name);
  2056. while (bitfury->shutdown == false) {
  2057. if (info->initialised) {
  2058. break;
  2059. }
  2060. cgsleep_us(30);
  2061. }
  2062. applog(LOG_INFO, "%s: nonceworker loop started", bitfury->drv->name);
  2063. while (bitfury->shutdown == false) {
  2064. struct timeval start_time, stop_time;
  2065. gettimeofday(&start_time, NULL);
  2066. uint32_t nonce_cnt = 0;
  2067. /* general nonces processing */
  2068. L_LOCK(info->noncework_list);
  2069. bf_data_t* nwdata = info->noncework_list->head;
  2070. while (nwdata != NULL) {
  2071. uint8_t board_id = NONCEWORK(nwdata)->chip_address.board_id;
  2072. uint8_t bcm250_id = NONCEWORK(nwdata)->chip_address.bcm250_id;
  2073. uint8_t chip_id = NONCEWORK(nwdata)->chip_address.chip_id;
  2074. nonce_cnt++;
  2075. /* general chip results processing */
  2076. if (test_nonce(&NONCEWORK(nwdata)->owork.work, NONCEWORK(nwdata)->nonce)) {
  2077. applog(LOG_DEBUG, "%s: nonceworker_thr: chip [%d:%d:%2d], valid nonce [%08x]",
  2078. bitfury->drv->name,
  2079. board_id, bcm250_id, chip_id, NONCEWORK(nwdata)->nonce);
  2080. submit_tested_work(info->thr, &NONCEWORK(nwdata)->owork.work);
  2081. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_nonce_time = time(NULL);
  2082. if (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count > 0) {
  2083. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count = 0;
  2084. #ifdef FILELOG
  2085. filelog(info, "BF16: good nonce for chip [%d:%d:%2d] "
  2086. "setting recovery_count to [0], error_rate: [%d]",
  2087. board_id, bcm250_id, chip_id,
  2088. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].error_rate);
  2089. #endif
  2090. }
  2091. increase_good_nonces(info, NONCEWORK(nwdata)->chip_address);
  2092. mutex_lock(&info->nonces_good_lock);
  2093. info->nonces_good_cg++;
  2094. mutex_unlock(&info->nonces_good_lock);
  2095. } else if (test_nonce(&NONCEWORK(nwdata)->cwork.work, NONCEWORK(nwdata)->nonce)) {
  2096. applog(LOG_DEBUG, "%s: nonceworker_thr: chip [%d:%d:%2d], valid nonce [%08x]",
  2097. bitfury->drv->name,
  2098. board_id, bcm250_id, chip_id, NONCEWORK(nwdata)->nonce);
  2099. submit_tested_work(info->thr, &NONCEWORK(nwdata)->cwork.work);
  2100. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_nonce_time = time(NULL);
  2101. if (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count > 0) {
  2102. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count = 0;
  2103. #ifdef FILELOG
  2104. filelog(info, "BF16: good nonce for chip [%d:%d:%2d] "
  2105. "setting recovery_count to 0, error_rate: [%d]",
  2106. board_id, bcm250_id, chip_id,
  2107. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].error_rate);
  2108. #endif
  2109. }
  2110. increase_good_nonces(info, NONCEWORK(nwdata)->chip_address);
  2111. mutex_lock(&info->nonces_good_lock);
  2112. info->nonces_good_cg++;
  2113. mutex_unlock(&info->nonces_good_lock);
  2114. } else {
  2115. time_t curr_time = time(NULL);
  2116. time_t time_diff = curr_time - info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_nonce_time;
  2117. if ((time_diff >= CHIP_FAILING_INTERVAL) &&
  2118. (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status < FAILING)) {
  2119. increase_errors(info, NONCEWORK(nwdata)->chip_address);
  2120. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status = FAILING;
  2121. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_error_time = curr_time;
  2122. applog(LOG_ERR, "%s: nonceworker_thr: chip [%d:%d:%2d], "
  2123. "failed: no good nonces during last [%.1f] seconds",
  2124. bitfury->drv->name,
  2125. board_id, bcm250_id, chip_id, CHIP_FAILING_INTERVAL);
  2126. #ifdef FILELOG
  2127. filelog(info, "BF16: no good nonces from chip [%d:%d:%2d], "
  2128. "error count: [%d], recovery_count: [%d], error_rate: [%d]",
  2129. board_id, bcm250_id, chip_id,
  2130. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].errors,
  2131. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count,
  2132. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].error_rate);
  2133. #endif
  2134. }
  2135. if (opt_bf16_renonce != RENONCE_DISABLED) {
  2136. /* add failed nonce to renonce list */
  2137. increase_bad_nonces(info, NONCEWORK(nwdata)->chip_address);
  2138. L_LOCK(info->renonce_list);
  2139. if (info->renonce_list->count < RENONCE_QUEUE_LEN) {
  2140. renonce_list_push(info->renonce_list,
  2141. info->renonce_id++,
  2142. NONCEWORK(nwdata)->nonce,
  2143. NONCEWORK(nwdata)->chip_address,
  2144. NONCEWORK(nwdata)->cwork,
  2145. NONCEWORK(nwdata)->owork);
  2146. } else
  2147. increase_re_bad_nonces(info, NONCEWORK(nwdata)->chip_address);
  2148. L_UNLOCK(info->renonce_list);
  2149. applog(LOG_DEBUG, "%s: nonceworker_thr: pushing renonce task: nonce: [%08x]",
  2150. bitfury->drv->name,
  2151. NONCEWORK(nwdata)->nonce);
  2152. } else
  2153. increase_bad_nonces(info, NONCEWORK(nwdata)->chip_address);
  2154. }
  2155. /* remove nonce from list */
  2156. noncework_list_pop(info->noncework_list);
  2157. nwdata = info->noncework_list->head;
  2158. }
  2159. L_UNLOCK(info->noncework_list);
  2160. /* cleanup older works */
  2161. cleanup_older(bitfury);
  2162. gettimeofday(&stop_time, NULL);
  2163. #if 0
  2164. applog(LOG_DEBUG, "%s: nonceworker_thr: nonces processed [%d]: time elapsed: [%.6f]",
  2165. bitfury->drv->name,
  2166. nonce_cnt, timediff(start_time, stop_time));
  2167. #endif
  2168. cgsleep_us(NONCEWORKER_DELAY);
  2169. }
  2170. applog(LOG_INFO, "%s: nonceworker_thr: exiting...", bitfury->drv->name);
  2171. return NULL;
  2172. }
  2173. static bool test_renonce(struct cgpu_info *bitfury, bf_data_t* rdata, bf_data_t* rnwdata, bool owork)
  2174. {
  2175. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  2176. uint8_t board_id = RENONCEWORK(rnwdata)->src_address.board_id;
  2177. uint8_t bcm250_id = renonce_chip_address[board_id].bcm250_id;
  2178. uint8_t chip_id = renonce_chip_address[board_id].chip_id;
  2179. if (owork == true) {
  2180. if (test_nonce(&RENONCE(rdata)->owork.work, RENONCEWORK(rnwdata)->nonce)) {
  2181. applog(LOG_DEBUG, "%s: renonceworker_thr: restored renonce: nonce: [%08x]",
  2182. bitfury->drv->name,
  2183. RENONCEWORK(rnwdata)->nonce);
  2184. submit_tested_work(info->thr, &RENONCE(rdata)->owork.work);
  2185. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_nonce_time = time(NULL);
  2186. if (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count > 0) {
  2187. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count = 0;
  2188. #ifdef FILELOG
  2189. filelog(info, "BF16: good nonce for renonce chip [%d:%d:%2d] "
  2190. "setting recovery_count to 0, error_rate: [%d]",
  2191. board_id, bcm250_id, chip_id,
  2192. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].error_rate);
  2193. #endif
  2194. }
  2195. increase_re_good_nonces(info, RENONCE(rdata)->src_address);
  2196. increase_re_good_nonces(info, renonce_chip_address[board_id]);
  2197. mutex_lock(&info->nonces_good_lock);
  2198. info->nonces_good_cg++;
  2199. mutex_unlock(&info->nonces_good_lock);
  2200. RENONCE(rdata)->match = true;
  2201. } else {
  2202. time_t curr_time = time(NULL);
  2203. time_t time_diff = curr_time - info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_nonce_time;
  2204. if ((time_diff >= RENONCE_CHIP_FAILING_INTERVAL) &&
  2205. (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status < FAILING)) {
  2206. increase_errors(info, renonce_chip_address[board_id]);
  2207. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status = FAILING;
  2208. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_error_time = curr_time;
  2209. applog(LOG_ERR, "%s: nonceworker_thr: renonce chip [%d:%d:%2d] "
  2210. "failed: no good nonces during last [%.1f] seconds",
  2211. bitfury->drv->name,
  2212. board_id, bcm250_id, chip_id, RENONCE_CHIP_FAILING_INTERVAL);
  2213. #ifdef FILELOG
  2214. filelog(info, "BF16: no good nonces from renonce chip [%d:%d:%2d], "
  2215. "error count: [%d] recovery_count: [%d], error_rate: [%d]",
  2216. board_id, bcm250_id, chip_id,
  2217. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].errors,
  2218. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count,
  2219. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].error_rate);
  2220. #endif
  2221. }
  2222. }
  2223. } else {
  2224. if (test_nonce(&RENONCE(rdata)->cwork.work, RENONCEWORK(rnwdata)->nonce)) {
  2225. applog(LOG_DEBUG, "%s: renonceworker_thr: restored renonce: nonce: [%08x]",
  2226. bitfury->drv->name,
  2227. RENONCEWORK(rnwdata)->nonce);
  2228. submit_tested_work(info->thr, &RENONCE(rdata)->cwork.work);
  2229. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_nonce_time = time(NULL);
  2230. if (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count > 0) {
  2231. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count = 0;
  2232. #ifdef FILELOG
  2233. filelog(info, "BF16: good nonce for renonce chip [%d:%d:%2d] "
  2234. "setting recovery_count to 0, error_rate: [%d]",
  2235. board_id, bcm250_id, chip_id,
  2236. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].error_rate);
  2237. #endif
  2238. }
  2239. increase_re_good_nonces(info, RENONCE(rdata)->src_address);
  2240. increase_re_good_nonces(info, renonce_chip_address[board_id]);
  2241. mutex_lock(&info->nonces_good_lock);
  2242. info->nonces_good_cg++;
  2243. mutex_unlock(&info->nonces_good_lock);
  2244. RENONCE(rdata)->match = true;
  2245. } else {
  2246. time_t curr_time = time(NULL);
  2247. time_t time_diff = curr_time - info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_nonce_time;
  2248. if ((time_diff >= RENONCE_CHIP_FAILING_INTERVAL) &&
  2249. (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status < FAILING)) {
  2250. increase_errors(info, renonce_chip_address[board_id]);
  2251. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status = FAILING;
  2252. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].last_error_time = curr_time;
  2253. applog(LOG_ERR, "%s: nonceworker_thr: renonce chip [%d:%d:%2d] "
  2254. "failed: no good nonces during last [%.1f] seconds",
  2255. bitfury->drv->name,
  2256. board_id, bcm250_id, chip_id, RENONCE_CHIP_FAILING_INTERVAL);
  2257. #ifdef FILELOG
  2258. filelog(info, "BF16: no good nonces from renonce chip [%d:%d:%2d], "
  2259. "error count: [%d] recovery_count: [%d], error_rate: [%d]",
  2260. board_id, bcm250_id, chip_id,
  2261. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].errors,
  2262. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].recovery_count,
  2263. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].error_rate);
  2264. #endif
  2265. }
  2266. }
  2267. }
  2268. return RENONCE(rdata)->match;
  2269. }
  2270. static void *bitfury_renonceworker(void *userdata)
  2271. {
  2272. struct cgpu_info *bitfury = (struct cgpu_info *)userdata;
  2273. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  2274. bf_list_t* id_list = nonce_list_init();
  2275. applog(LOG_INFO, "%s: started renonceworker thread", bitfury->drv->name);
  2276. while (bitfury->shutdown == false) {
  2277. if (info->initialised) {
  2278. break;
  2279. }
  2280. cgsleep_us(30);
  2281. }
  2282. applog(LOG_INFO, "%s: renonceworker loop started", bitfury->drv->name);
  2283. while (bitfury->shutdown == false) {
  2284. struct timeval start_time, stop_time;
  2285. gettimeofday(&start_time, NULL);
  2286. uint32_t nonce_cnt = 0;
  2287. /* renonce chip results processing */
  2288. L_LOCK(info->renoncework_list);
  2289. bf_data_t* rnwdata = info->renoncework_list->head;
  2290. while (rnwdata != NULL) {
  2291. nonce_cnt++;
  2292. /* find nonce by id */
  2293. L_LOCK(info->renonce_list);
  2294. bf_data_t* rdata = info->renonce_list->head;
  2295. while (rdata != NULL) {
  2296. if (match_nonce(RENONCEWORK(rnwdata)->nonce, RENONCE(rdata)->nonce, mask_bits)) {
  2297. if (RENONCE(rdata)->match == false) {
  2298. switch (RENONCE(rdata)->stage) {
  2299. case RENONCE_STAGE0:
  2300. if (test_renonce(bitfury, rdata, rnwdata, true) == true)
  2301. info->stage0_match++;
  2302. else
  2303. info->stage0_mismatch++;
  2304. break;
  2305. case RENONCE_STAGE1:
  2306. case RENONCE_STAGE2:
  2307. case RENONCE_STAGE3:
  2308. /* test old work first */
  2309. if (test_renonce(bitfury, rdata, rnwdata, true) == true) {
  2310. if (RENONCE(rdata)->stage == RENONCE_STAGE1)
  2311. info->stage1_mismatch++;
  2312. if (RENONCE(rdata)->stage == RENONCE_STAGE2)
  2313. info->stage2_match++;
  2314. if (RENONCE(rdata)->stage == RENONCE_STAGE3)
  2315. info->stage3_mismatch++;
  2316. } else {
  2317. if (test_renonce(bitfury, rdata, rnwdata, false) == true) {
  2318. if (RENONCE(rdata)->stage == RENONCE_STAGE1)
  2319. info->stage1_match++;
  2320. if (RENONCE(rdata)->stage == RENONCE_STAGE2)
  2321. info->stage2_mismatch++;
  2322. if (RENONCE(rdata)->stage == RENONCE_STAGE3)
  2323. info->stage3_match++;
  2324. }
  2325. }
  2326. break;
  2327. default:
  2328. applog(LOG_ERR, "%s: renonceworker_thr: invalid renonce stage arrived: [%d]",
  2329. bitfury->drv->name,
  2330. RENONCE(rdata)->stage);
  2331. }
  2332. }
  2333. nonce_list_push(id_list, RENONCE(rdata)->id);
  2334. if (RENONCE(rdata)->match == true)
  2335. break;
  2336. }
  2337. rdata = rdata->next;
  2338. }
  2339. L_UNLOCK(info->renonce_list);
  2340. /* remove nonce from list */
  2341. renoncework_list_pop(info->renoncework_list);
  2342. rnwdata = info->renoncework_list->head;
  2343. }
  2344. L_UNLOCK(info->renoncework_list);
  2345. L_LOCK(info->renonce_list);
  2346. bf_data_t* rdata = info->renonce_list->head;
  2347. while (rdata != NULL) {
  2348. if (RENONCE(rdata)->match == true) {
  2349. bf_data_t* rndata = rdata->next;
  2350. renonce_list_remove(info->renonce_list, rdata);
  2351. rdata = rndata;
  2352. continue;
  2353. }
  2354. /* update stage */
  2355. bf_data_t* ndata = id_list->head;
  2356. while (ndata != NULL) {
  2357. if (NONCE(ndata)->nonce == RENONCE(rdata)->id) {
  2358. RENONCE(rdata)->stage++;
  2359. RENONCE(rdata)->sent = false;
  2360. RENONCE(rdata)->received = false;
  2361. break;
  2362. }
  2363. ndata = ndata->next;
  2364. }
  2365. /* update tasks with wrong or dup nonces recieved */
  2366. if ((RENONCE(rdata)->stage != RENONCE_STAGE_FINISHED) &&
  2367. (RENONCE(rdata)->received == true)) {
  2368. RENONCE(rdata)->stage++;
  2369. RENONCE(rdata)->sent = false;
  2370. RENONCE(rdata)->received = false;
  2371. }
  2372. /* clear renonces list if we are running too slow */
  2373. if ((RENONCE(rdata)->stage >= RENONCE_STAGE2) &&
  2374. (info->renonce_list->count > RENONCE_STAGE2_LIMIT)) {
  2375. info->unmatched++;
  2376. increase_re_bad_nonces(info, RENONCE(rdata)->src_address);
  2377. bf_data_t* rndata = rdata->next;
  2378. renonce_list_remove(info->renonce_list, rdata);
  2379. rdata = rndata;
  2380. continue;
  2381. }
  2382. if ((RENONCE(rdata)->stage >= RENONCE_STAGE3) &&
  2383. (info->renonce_list->count > RENONCE_STAGE3_LIMIT)) {
  2384. info->unmatched++;
  2385. increase_re_bad_nonces(info, RENONCE(rdata)->src_address);
  2386. bf_data_t* rndata = rdata->next;
  2387. renonce_list_remove(info->renonce_list, rdata);
  2388. rdata = rndata;
  2389. continue;
  2390. }
  2391. if ((RENONCE(rdata)->stage == RENONCE_STAGE_FINISHED) &&
  2392. (RENONCE(rdata)->sent == false)) {
  2393. info->unmatched++;
  2394. increase_re_bad_nonces(info, RENONCE(rdata)->src_address);
  2395. bf_data_t* rndata = rdata->next;
  2396. renonce_list_remove(info->renonce_list, rdata);
  2397. rdata = rndata;
  2398. continue;
  2399. }
  2400. rdata = rdata->next;
  2401. }
  2402. L_UNLOCK(info->renonce_list);
  2403. /* clear id list */
  2404. bf_data_t* ndata = id_list->head;
  2405. while (ndata != NULL) {
  2406. nonce_list_pop(id_list);
  2407. ndata = id_list->head;
  2408. }
  2409. gettimeofday(&stop_time, NULL);
  2410. #if 0
  2411. applog(LOG_DEBUG, "%s: renonceworker_thr: nonces processed [%d]: time elapsed: [%.6f]",
  2412. bitfury->drv->name,
  2413. nonce_cnt, timediff(start_time, stop_time));
  2414. #endif
  2415. cgsleep_us(RENONCEWORKER_DELAY);
  2416. }
  2417. applog(LOG_INFO, "%s: renonceworker_thr: exiting...", bitfury->drv->name);
  2418. return NULL;
  2419. }
  2420. int16_t update_pid(bf_pid_t *pid, int16_t error)
  2421. {
  2422. int16_t pTerm, iTerm;
  2423. pTerm = 2 * error;
  2424. pid->i_state += error;
  2425. if(pid->i_state > pid->i_max)
  2426. pid->i_state = pid->i_max;
  2427. else if(pid->i_state < pid->i_min)
  2428. pid->i_state = pid->i_min;
  2429. iTerm = pid->i_state;
  2430. return (pTerm + iTerm);
  2431. }
  2432. static void *bitfury_hwmonitor(void *userdata)
  2433. {
  2434. struct cgpu_info *bitfury = (struct cgpu_info *)userdata;
  2435. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  2436. uint8_t board_id, bcm250_id, chip_id;
  2437. time_t curr_time;
  2438. applog(LOG_INFO, "%s: started hwmonitor thread", bitfury->drv->name);
  2439. while (bitfury->shutdown == false) {
  2440. if (info->initialised) {
  2441. break;
  2442. }
  2443. cgsleep_us(30);
  2444. }
  2445. applog(LOG_INFO, "%s: hwmonitor loop started", bitfury->drv->name);
  2446. while (bitfury->shutdown == false) {
  2447. float max_temp = 0.0;
  2448. float t_alarm = 100.0;
  2449. bool a_temp = false;
  2450. bool a_ichain = false;
  2451. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  2452. /* read hw sensor data */
  2453. char buff[256];
  2454. if (info->chipboard[board_id].detected == true) {
  2455. memset(buff, 0, sizeof(buff));
  2456. if (device_uart_txrx(board_id + 1, "S", buff) < 0)
  2457. quit(1, "%s: %s() failed to get BOARD%d status",
  2458. bitfury->drv->name, __func__, board_id + 1);
  2459. if (parse_hwstats(info, board_id, buff) < 0)
  2460. applog(LOG_ERR, "%s: failed to parse hw stats",
  2461. bitfury->drv->name);
  2462. info->chipboard[board_id].p_chain1 = info->chipboard[board_id].u_chain1 * info->chipboard[board_id].i_chain1;
  2463. info->chipboard[board_id].p_chain2 = info->chipboard[board_id].u_chain2 * info->chipboard[board_id].i_chain2;
  2464. /* fan power calculation */
  2465. if (info->chipboard[board_id].rpm != 0) {
  2466. if (info->chipboard[board_id].fan_speed > 40)
  2467. info->chipboard[board_id].p_fan = (0.1 + 0.0236 * (info->chipboard[board_id].fan_speed - 40)) *
  2468. (info->chipboard[board_id].u_board + U_LOSS);
  2469. else
  2470. info->chipboard[board_id].p_fan = 1.23;
  2471. } else {
  2472. info->chipboard[board_id].p_fan = 0.0;
  2473. }
  2474. /* board power calculation */
  2475. #ifdef MINER_X5
  2476. float i_board = info->chipboard[board_id].i_chain1;
  2477. #endif
  2478. #ifdef MINER_X6
  2479. float i_board = info->chipboard[board_id].i_chain1 + info->chipboard[board_id].i_chain2;
  2480. #endif
  2481. info->chipboard[board_id].p_board = ((info->chipboard[board_id].u_board + U_LOSS) * i_board + 2.0 +
  2482. info->chipboard[board_id].p_fan);
  2483. if (info->chipboard[board_id].a_temp == 1)
  2484. a_temp = true;
  2485. if ((info->chipboard[board_id].a_ichain1 == 1) ||
  2486. (info->chipboard[board_id].a_ichain2 == 1))
  2487. a_ichain = true;
  2488. if (max_temp < info->chipboard[board_id].temp)
  2489. max_temp = info->chipboard[board_id].temp;
  2490. if (t_alarm > info->chipboard[board_id].t_alarm)
  2491. t_alarm = info->chipboard[board_id].t_alarm;
  2492. }
  2493. }
  2494. /* enable temp alarm */
  2495. if ((a_temp == true) && (info->a_temp == false)) {
  2496. applog(LOG_ERR, "%s: temperature alarm enabled: [%5.1f]!!!",
  2497. bitfury->drv->name, max_temp);
  2498. /* enable fans to full speed*/
  2499. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  2500. if (info->chipboard[board_id].detected == true) {
  2501. if (device_uart_transfer(board_id + 1, "F:100") < 0)
  2502. quit(1, "%s: %s() failed to set BOARD%d fan speed",
  2503. bitfury->drv->name, __func__, board_id + 1);
  2504. applog(LOG_INFO, "%s: set BOARD%d fan speed to max speed",
  2505. bitfury->drv->name, board_id + 1);
  2506. }
  2507. }
  2508. }
  2509. /* temp alarm recovery */
  2510. if ((a_temp == false) && (info->a_temp == true)) {
  2511. applog(LOG_ERR, "%s: temperature alarm recovery: [%5.1f]",
  2512. bitfury->drv->name, max_temp);
  2513. /* restore fans speed */
  2514. set_fan_speed(bitfury);
  2515. reinit_x5(info, false);
  2516. }
  2517. /* enable power chain alarm */
  2518. if ((a_ichain == true) && (info->a_ichain == false)) {
  2519. applog(LOG_ERR, "%s: power chain alarm enabled!!!",
  2520. bitfury->drv->name);
  2521. info->ialarm_start = time(NULL);
  2522. }
  2523. /* power chain alarm recovery */
  2524. if ((a_ichain == false) && (info->a_ichain == true)) {
  2525. if (info->ialarm_count > 1) {
  2526. applog(LOG_ERR, "%s: power chain alarm recovery",
  2527. bitfury->drv->name);
  2528. info->ialarm_count = 1;
  2529. info->ialarm_buzzer = false;
  2530. info->ialarm_start = time(NULL);
  2531. reinit_x5(info, false);
  2532. }
  2533. }
  2534. info->a_temp = a_temp;
  2535. info->a_ichain = a_ichain;
  2536. if (info->a_temp == true)
  2537. applog(LOG_ERR, "%s: ALARM: board temp: [%5.1f] alarm temp: [%5.1f]",
  2538. bitfury->drv->name, max_temp, t_alarm);
  2539. if (info->a_ichain == true) {
  2540. curr_time = time(NULL);
  2541. if (curr_time - info->ialarm_start >= info->ialarm_count * ICHAIN_ALARM_INTERVAL) {
  2542. info->ialarm_count *= 2;
  2543. info->ialarm_start = curr_time;
  2544. /* enable power chain */
  2545. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  2546. enable_power_chain(bitfury, board_id, 0);
  2547. #ifdef MINER_X5
  2548. info->chipboard[board_id].power_enable_time = curr_time;
  2549. #endif
  2550. #ifdef MINER_X6
  2551. info->chipboard[board_id].power1_enable_time = curr_time;
  2552. info->chipboard[board_id].power2_enable_time = curr_time;
  2553. #endif
  2554. }
  2555. }
  2556. } else if (opt_bf16_power_management_disabled == false) {
  2557. if (info->a_net == true) {
  2558. /* disable power chain if no internet available */
  2559. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  2560. if (info->chipboard[board_id].detected == true) {
  2561. #ifdef MINER_X5
  2562. if (info->chipboard[board_id].p_chain1_enabled == 1) {
  2563. #endif
  2564. #ifdef MINER_X6
  2565. if ((info->chipboard[board_id].p_chain1_enabled == 1) ||
  2566. (info->chipboard[board_id].p_chain2_enabled == 1)) {
  2567. #endif
  2568. disable_power_chain(bitfury, board_id, 0);
  2569. }
  2570. }
  2571. }
  2572. } else {
  2573. /* enable power chain - internet recovery */
  2574. bool recovery = false;
  2575. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  2576. if (info->chipboard[board_id].detected == true) {
  2577. #ifdef MINER_X5
  2578. if ((info->chipboard[board_id].p_chain1_enabled == 0) &&
  2579. (info->chipboard[board_id].power_disabled == false)) {
  2580. enable_power_chain(bitfury, board_id, 0);
  2581. info->chipboard[board_id].power_enable_time = time(NULL);
  2582. /* wait for power chain to enable */
  2583. cgsleep_us(POWER_WAIT_INTERVAL);
  2584. reinit_x5(info, false);
  2585. }
  2586. #endif
  2587. #ifdef MINER_X6
  2588. if ((info->chipboard[board_id].p_chain1_enabled == 0) &&
  2589. (info->chipboard[board_id].power1_disabled == false)){
  2590. enable_power_chain(bitfury, board_id, 1);
  2591. info->chipboard[board_id].power1_enable_time = time(NULL);
  2592. recovery = true;
  2593. }
  2594. if ((info->chipboard[board_id].p_chain2_enabled == 0) &&
  2595. (info->chipboard[board_id].power2_disabled == false)) {
  2596. enable_power_chain(bitfury, board_id, 2);
  2597. info->chipboard[board_id].power2_enable_time = time(NULL);
  2598. recovery = true;
  2599. }
  2600. #endif
  2601. }
  2602. }
  2603. /* reinit chips on alarm recovery */
  2604. if (recovery == true) {
  2605. /* wait for power chain to enable */
  2606. cgsleep_us(POWER_WAIT_INTERVAL);
  2607. reinit_x5(info, false);
  2608. }
  2609. }
  2610. }
  2611. /* board temperature regulation */
  2612. char uart_cmd[8];
  2613. if (manual_pid_enabled == false) {
  2614. sprintf(uart_cmd, "N:%d", (int)max_temp);
  2615. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  2616. if (info->chipboard[board_id].detected == true) {
  2617. if (device_uart_transfer(board_id + 1, uart_cmd) < 0)
  2618. quit(1, "%s: %s() failed to set BOARD%d next temp",
  2619. bitfury->drv->name, __func__, board_id + 1);
  2620. applog(LOG_INFO, "%s: set BOARD%d next temp to [%5.1f]",
  2621. bitfury->drv->name, board_id + 1, max_temp);
  2622. }
  2623. }
  2624. } else {
  2625. uint8_t max_fan_speed = 0;
  2626. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  2627. if (info->chipboard[board_id].detected == true) {
  2628. uint16_t pid_temp = 10 * info->chipboard[board_id].temp;
  2629. if (10 * max_temp > pid_temp)
  2630. pid_temp = 10 * max_temp;
  2631. int16_t fan_speed = update_pid(&info->chipboard[board_id].pid,
  2632. (pid_temp - 10 * info->chipboard[board_id].target_temp) / 10);
  2633. if (fan_speed > 100)
  2634. fan_speed = 100;
  2635. else if (fan_speed < 0)
  2636. fan_speed = 0;
  2637. if (max_fan_speed < fan_speed)
  2638. max_fan_speed = fan_speed;
  2639. }
  2640. }
  2641. sprintf(uart_cmd, "F:%d", max_fan_speed);
  2642. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  2643. if (info->chipboard[board_id].detected == true) {
  2644. if (device_uart_transfer(board_id + 1, uart_cmd) < 0)
  2645. quit(1, "%s: %s() failed to set BOARD%d fan speed",
  2646. bitfury->drv->name, __func__, board_id + 1);
  2647. applog(LOG_INFO, "%s: set BOARD%d fan speed to [%d]",
  2648. bitfury->drv->name, board_id + 1, max_fan_speed);
  2649. }
  2650. }
  2651. }
  2652. /* board power management */
  2653. if (opt_bf16_power_management_disabled == false) {
  2654. curr_time = time(NULL);
  2655. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  2656. if (info->chipboard[board_id].detected == true) {
  2657. #ifdef MINER_X5
  2658. uint8_t disabled_chips = 0;
  2659. uint8_t total_chips = 0;
  2660. #endif
  2661. #ifdef MINER_X6
  2662. uint8_t disabled_chips_chain1 = 0;
  2663. uint8_t total_chips_chain1 = 0;
  2664. uint8_t disabled_chips_chain2 = 0;
  2665. uint8_t total_chips_chain2 = 0;
  2666. #endif
  2667. for (bcm250_id = 0; bcm250_id < BCM250_NUM; bcm250_id++) {
  2668. uint8_t first_good_chip = info->chipboard[board_id].bcm250[bcm250_id].first_good_chip;
  2669. uint8_t last_good_chip = info->chipboard[board_id].bcm250[bcm250_id].last_good_chip;
  2670. /* chips loop */
  2671. for (chip_id = first_good_chip; chip_id < last_good_chip; chip_id++) {
  2672. #ifdef MINER_X5
  2673. total_chips++;
  2674. if (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status == DISABLED)
  2675. disabled_chips++;
  2676. #endif
  2677. #ifdef MINER_X6
  2678. if (bcm250_id < BCM250_NUM / 2) {
  2679. total_chips_chain1++;
  2680. if (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status == DISABLED)
  2681. disabled_chips_chain1++;
  2682. }
  2683. if ((bcm250_id >= BCM250_NUM / 2) && (bcm250_id < BCM250_NUM)) {
  2684. total_chips_chain2++;
  2685. if (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status == DISABLED)
  2686. disabled_chips_chain2++;
  2687. }
  2688. #endif
  2689. }
  2690. }
  2691. #ifdef MINER_X5
  2692. /* disable chain power if all chips failed */
  2693. if ((total_chips == disabled_chips) &&
  2694. (info->chipboard[board_id].p_chain1_enabled == 1)) {
  2695. disable_power_chain(bitfury, board_id, 0);
  2696. /* increase disable counter until we reach long enough work time */
  2697. time_t work_interval = curr_time - info->chipboard[board_id].power_enable_time;
  2698. if (work_interval < CHAIN_WORK_INTERVAL)
  2699. info->chipboard[board_id].power_disable_count *= 2;
  2700. info->chipboard[board_id].power_disabled = true;
  2701. info->chipboard[board_id].power_disable_time = curr_time;
  2702. info->chipboard[board_id].chips_disabled = info->chipboard[board_id].chips_num;
  2703. }
  2704. /* enable disabled chain */
  2705. if ((info->chipboard[board_id].p_chain1_enabled == 0) &&
  2706. (info->chipboard[board_id].power_disabled == true) &&
  2707. (curr_time - info->chipboard[board_id].power_disable_time >= info->chipboard[board_id].power_disable_count * CHAIN_REENABLE_INTERVAL)) {
  2708. time_t disable_interval = curr_time - info->chipboard[board_id].power_disable_time;
  2709. info->chipboard[board_id].power_disable_time = curr_time;
  2710. info->chipboard[board_id].chips_disabled = 0;
  2711. enable_power_chain(bitfury, board_id, 0);
  2712. info->chipboard[board_id].power_enable_time = curr_time;
  2713. info->chipboard[board_id].power_disabled = false;
  2714. /* wait for power chain to enable */
  2715. cgsleep_us(POWER_WAIT_INTERVAL);
  2716. reinit_x5(info, true);
  2717. applog(LOG_NOTICE, "%s: reenabled power on BOARD%d: time interval [%d]",
  2718. bitfury->drv->name,
  2719. board_id + 1, (int)disable_interval);
  2720. #ifdef FILELOG
  2721. filelog(info, "%s: reenabled power on BOARD%d: time interval [%d]",
  2722. bitfury->drv->name,
  2723. board_id + 1, (int)disable_interval);
  2724. #endif
  2725. }
  2726. #endif
  2727. #ifdef MINER_X6
  2728. /* disable power on chain 1 only if chips on both chains failed
  2729. * as chain 2 data channel depends on chain 1 */
  2730. if ((total_chips_chain1 == disabled_chips_chain1) &&
  2731. (info->chipboard[board_id].p_chain1_enabled == 1) &&
  2732. (info->chipboard[board_id].power2_disabled == true)) {
  2733. disable_power_chain(bitfury, board_id, 0);
  2734. /* increase disable counter until we reach long enough work time */
  2735. time_t work_interval = curr_time - info->chipboard[board_id].power1_enable_time;
  2736. if (work_interval < CHAIN_WORK_INTERVAL)
  2737. info->chipboard[board_id].power1_disable_count *= 2;
  2738. info->chipboard[board_id].power1_disabled = true;
  2739. info->chipboard[board_id].power2_disabled = true;
  2740. info->chipboard[board_id].power1_disable_time = curr_time;
  2741. info->chipboard[board_id].power2_disable_time = curr_time;
  2742. info->chipboard[board_id].chips_disabled = info->chipboard[board_id].chips_num;
  2743. } else
  2744. if ((total_chips_chain2 == disabled_chips_chain2) &&
  2745. (info->chipboard[board_id].p_chain2_enabled == 1)) {
  2746. disable_power_chain(bitfury, board_id, 2);
  2747. /* increase disable counter until we reach long enough work time */
  2748. time_t work_interval = curr_time - info->chipboard[board_id].power2_enable_time;
  2749. if (work_interval < CHAIN_WORK_INTERVAL)
  2750. info->chipboard[board_id].power2_disable_count *= 2;
  2751. info->chipboard[board_id].power2_disabled = true;
  2752. info->chipboard[board_id].power2_disable_time = curr_time;
  2753. info->chipboard[board_id].chips_disabled = info->chipboard[board_id].chips_num / 2;
  2754. }
  2755. /* HW FIX: try to reenable disabled chain */
  2756. if ((info->chipboard[board_id].p_chain1_enabled == 0) &&
  2757. (info->chipboard[board_id].power1_disabled == true) &&
  2758. (curr_time - info->chipboard[board_id].power1_disable_time >= info->chipboard[board_id].power1_disable_count * CHAIN_REENABLE_INTERVAL)) {
  2759. time_t disable_interval = curr_time - info->chipboard[board_id].power1_disable_time;
  2760. info->chipboard[board_id].power1_disable_time = curr_time;
  2761. info->chipboard[board_id].power2_disable_time = curr_time;
  2762. info->chipboard[board_id].chips_disabled = 0;
  2763. enable_power_chain(bitfury, board_id, 0);
  2764. info->chipboard[board_id].power1_enable_time = curr_time;
  2765. info->chipboard[board_id].power2_enable_time = curr_time;
  2766. info->chipboard[board_id].power1_disabled = false;
  2767. info->chipboard[board_id].power2_disabled = false;
  2768. /* wait for power chain to enable */
  2769. cgsleep_us(POWER_WAIT_INTERVAL);
  2770. reinit_x5(info, true);
  2771. applog(LOG_NOTICE, "%s: reenabled power on BOARD%d: time interval [%d]",
  2772. bitfury->drv->name,
  2773. board_id + 1, (int)disable_interval);
  2774. #ifdef FILELOG
  2775. filelog(info, "%s: reenabled power on BOARD%d: time interval [%d]",
  2776. bitfury->drv->name,
  2777. board_id + 1, (int)disable_interval);
  2778. #endif
  2779. } else
  2780. if ((info->chipboard[board_id].p_chain2_enabled == 0) &&
  2781. (info->chipboard[board_id].power2_disabled == true) &&
  2782. (curr_time - info->chipboard[board_id].power2_disable_time >= info->chipboard[board_id].power2_disable_count * CHAIN_REENABLE_INTERVAL)) {
  2783. time_t disable_interval = curr_time - info->chipboard[board_id].power2_disable_time;
  2784. info->chipboard[board_id].power2_disable_time = curr_time;
  2785. info->chipboard[board_id].chips_disabled = 0;
  2786. enable_power_chain(bitfury, board_id, 2);
  2787. info->chipboard[board_id].power2_enable_time = curr_time;
  2788. info->chipboard[board_id].power2_disabled = false;
  2789. /* wait for power chain to enable */
  2790. cgsleep_us(POWER_WAIT_INTERVAL);
  2791. reinit_x5(info, true);
  2792. applog(LOG_NOTICE, "%s: reenabled chainboard 2 on BOARD%d: time interval [%d]",
  2793. bitfury->drv->name,
  2794. board_id + 1, (int)disable_interval);
  2795. #ifdef FILELOG
  2796. filelog(info, "%s: reenabled chainboard 2 on BOARD%d: time interval [%d]",
  2797. bitfury->drv->name,
  2798. board_id + 1, (int)disable_interval);
  2799. #endif
  2800. }
  2801. #endif
  2802. /* switch renonce chip to next board */
  2803. if ((info->renonce_chips == 1) &&
  2804. (renonce_chip_address[board_id].board_id == board_id) &&
  2805. #ifdef MINER_X5
  2806. (info->chipboard[board_id].power_disabled == true)) {
  2807. #endif
  2808. #ifdef MINER_X6
  2809. (info->chipboard[board_id].power1_disabled == true) &&
  2810. (info->chipboard[board_id].power2_disabled == true)) {
  2811. #endif
  2812. uint8_t next_board_id = (board_id + 1) % CHIPBOARD_NUM;
  2813. uint8_t next_bcm250_id = renonce_chip_address[board_id].bcm250_id;
  2814. uint8_t next_chip_id = renonce_chip_address[board_id].chip_id;
  2815. /* board id should be set last */
  2816. renonce_chip_address[next_board_id].bcm250_id = next_bcm250_id;
  2817. renonce_chip_address[next_board_id].chip_id = next_chip_id;
  2818. renonce_chip_address[next_board_id].board_id = next_board_id;
  2819. info->chipboard[next_board_id].bcm250[next_bcm250_id].chips[next_chip_id].status = UNINITIALIZED;
  2820. applog(LOG_NOTICE, "%s: changed renonce chip address to: [%d:%d:%2d]",
  2821. bitfury->drv->name,
  2822. next_board_id, next_bcm250_id, next_chip_id);
  2823. renonce_chip_address[board_id].board_id = -1;
  2824. renonce_chip_address[board_id].bcm250_id = -1;
  2825. renonce_chip_address[board_id].chip_id = -1;
  2826. }
  2827. }
  2828. }
  2829. }
  2830. cgsleep_us(HWMONITOR_DELAY);
  2831. }
  2832. applog(LOG_INFO, "%s: hwmonitor_thr: exiting...", bitfury->drv->name);
  2833. return NULL;
  2834. }
  2835. static void *bitfury_alarm(void *userdata)
  2836. {
  2837. struct cgpu_info *bitfury = (struct cgpu_info *)userdata;
  2838. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  2839. struct pool *pool;
  2840. int i;
  2841. time_t curr_time_t;
  2842. struct timeval curr_time;
  2843. applog(LOG_INFO, "%s: started alarm thread", bitfury->drv->name);
  2844. /* blink lamps once */
  2845. led_red_enable(info);
  2846. led_green_enable(info);
  2847. cgsleep_ms(1000);
  2848. led_red_disable(info);
  2849. led_green_disable(info);
  2850. buzzer_disable(info);
  2851. applog(LOG_INFO, "%s: alarm loop started", bitfury->drv->name);
  2852. while (bitfury->shutdown == false) {
  2853. curr_time_t = time(NULL);
  2854. gettimeofday(&curr_time, NULL);
  2855. /* check if there are enabled pools present */
  2856. bool idle = true;
  2857. for (i = 0; i < total_pools; i++) {
  2858. pool = pools[i];
  2859. if (pool->idle == false)
  2860. idle = false;
  2861. }
  2862. if (idle == true)
  2863. info->a_net = true;
  2864. else
  2865. info->a_net = false;
  2866. /* temperature alarm processing */
  2867. if ((info->a_temp == true) && (info->a_ichain == false)) {
  2868. if (timediff_us(info->led_red_switch, curr_time) >= LED_RED_INTERVAL) {
  2869. if (info->led_red_enabled == true)
  2870. led_red_disable(info);
  2871. else
  2872. led_red_enable(info);
  2873. }
  2874. /* enable buzzer */
  2875. if (timediff_us(info->buzzer_switch, curr_time) >= BUZZER_INTERVAL) {
  2876. if (info->buzzer_enabled == true)
  2877. buzzer_disable(info);
  2878. else
  2879. buzzer_enable(info);
  2880. }
  2881. } else if ((info->a_ichain == false) && (info->a_net == false)) {
  2882. if (info->led_red_enabled == true)
  2883. led_red_disable(info);
  2884. if (info->buzzer_enabled == true)
  2885. buzzer_disable(info);
  2886. }
  2887. /* power chain alarm processing */
  2888. if ((info->a_ichain == true) && (info->a_temp == false)) {
  2889. if (timediff_us(info->led_red_switch, curr_time) >= LED_RED_INTERVAL) {
  2890. if (info->led_red_enabled == true)
  2891. led_red_disable(info);
  2892. else
  2893. led_red_enable(info);
  2894. }
  2895. if (info->ialarm_buzzer == false) {
  2896. buzzer_enable(info);
  2897. cgsleep_ms(1000);
  2898. buzzer_disable(info);
  2899. info->ialarm_buzzer = true;
  2900. }
  2901. /* enable buzzer */
  2902. if (curr_time_t - info->ialarm_start >= info->ialarm_count * ICHAIN_ALARM_INTERVAL) {
  2903. buzzer_enable(info);
  2904. cgsleep_ms(1000);
  2905. buzzer_disable(info);
  2906. }
  2907. } else if ((info->a_temp == false) && (info->a_net == false)) {
  2908. if (info->led_red_enabled == true)
  2909. led_red_disable(info);
  2910. if (info->buzzer_enabled == true)
  2911. buzzer_disable(info);
  2912. }
  2913. /* blink green lamp if there are enabled pools present and no alarms active */
  2914. if ((info->a_net == false) && (info->a_temp == false) && (info->a_ichain == false)) {
  2915. if (info->led_red_enabled == true)
  2916. led_red_disable(info);
  2917. if (timediff_us(info->led_green_switch, curr_time) >= LED_GREEN_INTERVAL) {
  2918. if (info->led_green_enabled == true)
  2919. led_green_disable(info);
  2920. else
  2921. led_green_enable(info);
  2922. }
  2923. } else {
  2924. if (info->led_green_enabled == true)
  2925. led_green_disable(info);
  2926. if ((info->a_temp == false) && (info->a_ichain == false)) {
  2927. if (timediff_us(info->led_red_switch, curr_time) >= LED_RED_NET_INTERVAL) {
  2928. if (info->led_red_enabled == true)
  2929. led_red_disable(info);
  2930. else
  2931. led_red_enable(info);
  2932. }
  2933. }
  2934. }
  2935. cgsleep_us(ALARM_DELAY);
  2936. }
  2937. led_red_disable(info);
  2938. led_green_disable(info);
  2939. buzzer_disable(info);
  2940. applog(LOG_INFO, "%s: alarm_thr: exiting...", bitfury->drv->name);
  2941. return NULL;
  2942. }
  2943. static void *bitfury_statistics(void *userdata)
  2944. {
  2945. struct cgpu_info *bitfury = (struct cgpu_info *)userdata;
  2946. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  2947. struct timeval start_time, stop_time;
  2948. uint8_t board_id, bcm250_id, chip_id;
  2949. float u_board;
  2950. float i_total;
  2951. float p_total;
  2952. float u_chip;
  2953. float p_chip;
  2954. time_t time0 = time(NULL);
  2955. time_t time1 = time(NULL);
  2956. time_t time2;
  2957. /* statistics calculation loop */
  2958. while (bitfury->shutdown == false) {
  2959. time2 = time(NULL);
  2960. if (time2 - time1 >= AVG_TIME_DELTA) {
  2961. gettimeofday(&start_time, NULL);
  2962. u_board = 0.0;
  2963. i_total = 0.0;
  2964. p_total = 0.0;
  2965. u_chip = 0.0;
  2966. p_chip = 0.0;
  2967. info->chips_failed = 0;
  2968. info->chips_disabled = 0;
  2969. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  2970. if (info->chipboard[board_id].detected == true) {
  2971. info->chipboard[board_id].chips_failed = 0;
  2972. info->chips_disabled += info->chipboard[board_id].chips_disabled;
  2973. /* concentrator loop */
  2974. for (bcm250_id = 0; bcm250_id < BCM250_NUM; bcm250_id++) {
  2975. uint8_t first_good_chip = info->chipboard[board_id].bcm250[bcm250_id].first_good_chip;
  2976. uint8_t last_good_chip = info->chipboard[board_id].bcm250[bcm250_id].last_good_chip;
  2977. info->chipboard[board_id].bcm250[bcm250_id].chips_failed = 0;
  2978. /* chips loop */
  2979. for (chip_id = first_good_chip; chip_id < last_good_chip; chip_id++) {
  2980. if (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status >= FAILING) {
  2981. info->chipboard[board_id].bcm250[bcm250_id].chips_failed++;
  2982. info->chipboard[board_id].chips_failed++;
  2983. info->chips_failed++;
  2984. }
  2985. if (opt_bf16_stats_enabled) {
  2986. get_average(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].task_switch,
  2987. (float)info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].task_switch_dx,
  2988. (float)(time2 - time1), AVG_TIME_INTERVAL);
  2989. get_average(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status_cmd,
  2990. (float)info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status_cmd_dx,
  2991. (float)(time2 - time1), AVG_TIME_INTERVAL);
  2992. get_average(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status_cmd_none,
  2993. (float)info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status_cmd_none_dx,
  2994. (float)(time2 - time1), AVG_TIME_INTERVAL);
  2995. get_average(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces,
  2996. (float)info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces_dx,
  2997. (float)(time2 - time1), AVG_TIME_INTERVAL);
  2998. get_average(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].hashrate,
  2999. (float)info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces_dx,
  3000. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3001. get_average(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].hashrate_good,
  3002. (float)info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces_good_dx,
  3003. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3004. get_average(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].hashrate_diff,
  3005. (float)info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces_diff_dx,
  3006. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3007. get_average(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].hashrate_bad,
  3008. (float)info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces_bad_dx,
  3009. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3010. if (opt_bf16_renonce != RENONCE_DISABLED) {
  3011. get_average(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].hashrate_re,
  3012. (float)info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces_re_dx,
  3013. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3014. get_average(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].hashrate_re_good,
  3015. (float)info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces_re_good_dx,
  3016. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3017. get_average(&info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].hashrate_re_bad,
  3018. (float)info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces_re_bad_dx,
  3019. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3020. }
  3021. }
  3022. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].task_switch_dx = 0;
  3023. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status_cmd_dx = 0;
  3024. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status_cmd_none_dx = 0;
  3025. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces_dx = 0;
  3026. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces_good_dx = 0;
  3027. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces_diff_dx = 0;
  3028. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces_bad_dx = 0;
  3029. if (opt_bf16_renonce != RENONCE_DISABLED) {
  3030. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces_re_dx = 0;
  3031. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces_re_good_dx = 0;
  3032. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces_re_bad_dx = 0;
  3033. }
  3034. if (opt_bf16_stats_enabled) {
  3035. if (opt_bf16_renonce != RENONCE_DISABLED)
  3036. applog(LOG_NOTICE, "STATS: chp [%d:%d:%2d], tsk/s [%4.0f], "
  3037. "ncs/s [%4.0f], sts/s [%3.0f], none/s [%3.0f], hr/s [%8.3f] hr+/s [%8.3f] rhr/s [%8.3f]",
  3038. board_id, bcm250_id, chip_id,
  3039. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].task_switch,
  3040. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces,
  3041. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status_cmd,
  3042. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status_cmd_none,
  3043. CHIP_COEFF * info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].hashrate,
  3044. CHIP_COEFF * info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].hashrate_good,
  3045. CHIP_COEFF * (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].hashrate_good +
  3046. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].hashrate_re_good));
  3047. else
  3048. applog(LOG_NOTICE, "STATS: chp [%d:%d:%2d], tsk/s [%4.0f], "
  3049. "ncs/s [%3.0f], sts/s [%3.0f], none/s [%3.0f], hr/s [%8.3f] hr+/s [%8.3f]",
  3050. board_id, bcm250_id, chip_id,
  3051. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].task_switch,
  3052. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].nonces,
  3053. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status_cmd,
  3054. info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status_cmd_none,
  3055. CHIP_COEFF * info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].hashrate,
  3056. CHIP_COEFF * info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].hashrate_good);
  3057. }
  3058. }
  3059. if (opt_bf16_stats_enabled) {
  3060. get_average(&info->chipboard[board_id].bcm250[bcm250_id].task_switch,
  3061. (float)info->chipboard[board_id].bcm250[bcm250_id].task_switch_dx,
  3062. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3063. get_average(&info->chipboard[board_id].bcm250[bcm250_id].status_cmd,
  3064. (float)info->chipboard[board_id].bcm250[bcm250_id].status_cmd_dx,
  3065. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3066. get_average(&info->chipboard[board_id].bcm250[bcm250_id].status_cmd_none,
  3067. (float)info->chipboard[board_id].bcm250[bcm250_id].status_cmd_none_dx,
  3068. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3069. get_average(&info->chipboard[board_id].bcm250[bcm250_id].nonces,
  3070. (float)info->chipboard[board_id].bcm250[bcm250_id].nonces_dx,
  3071. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3072. get_average(&info->chipboard[board_id].bcm250[bcm250_id].hashrate,
  3073. (float)info->chipboard[board_id].bcm250[bcm250_id].nonces_dx,
  3074. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3075. get_average(&info->chipboard[board_id].bcm250[bcm250_id].hashrate_good,
  3076. (float)info->chipboard[board_id].bcm250[bcm250_id].nonces_good_dx,
  3077. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3078. get_average(&info->chipboard[board_id].bcm250[bcm250_id].hashrate_diff,
  3079. (float)info->chipboard[board_id].bcm250[bcm250_id].nonces_diff_dx,
  3080. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3081. get_average(&info->chipboard[board_id].bcm250[bcm250_id].hashrate_bad,
  3082. (float)info->chipboard[board_id].bcm250[bcm250_id].nonces_bad_dx,
  3083. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3084. if (opt_bf16_renonce != RENONCE_DISABLED) {
  3085. get_average(&info->chipboard[board_id].bcm250[bcm250_id].hashrate_re,
  3086. (float)info->chipboard[board_id].bcm250[bcm250_id].nonces_re_dx,
  3087. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3088. get_average(&info->chipboard[board_id].bcm250[bcm250_id].hashrate_re_good,
  3089. (float)info->chipboard[board_id].bcm250[bcm250_id].nonces_re_good_dx,
  3090. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3091. get_average(&info->chipboard[board_id].bcm250[bcm250_id].hashrate_re_bad,
  3092. (float)info->chipboard[board_id].bcm250[bcm250_id].nonces_re_bad_dx,
  3093. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3094. }
  3095. }
  3096. info->chipboard[board_id].bcm250[bcm250_id].task_switch_dx = 0;
  3097. info->chipboard[board_id].bcm250[bcm250_id].status_cmd_dx = 0;
  3098. info->chipboard[board_id].bcm250[bcm250_id].status_cmd_none_dx = 0;
  3099. info->chipboard[board_id].bcm250[bcm250_id].nonces_dx = 0;
  3100. info->chipboard[board_id].bcm250[bcm250_id].nonces_good_dx = 0;
  3101. info->chipboard[board_id].bcm250[bcm250_id].nonces_diff_dx = 0;
  3102. info->chipboard[board_id].bcm250[bcm250_id].nonces_bad_dx = 0;
  3103. if (opt_bf16_renonce != RENONCE_DISABLED) {
  3104. info->chipboard[board_id].bcm250[bcm250_id].nonces_re_dx = 0;
  3105. info->chipboard[board_id].bcm250[bcm250_id].nonces_re_good_dx = 0;
  3106. info->chipboard[board_id].bcm250[bcm250_id].nonces_re_bad_dx = 0;
  3107. }
  3108. }
  3109. if (opt_bf16_stats_enabled) {
  3110. get_average(&info->chipboard[board_id].task_switch,
  3111. (float)info->chipboard[board_id].task_switch_dx,
  3112. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3113. get_average(&info->chipboard[board_id].status_cmd,
  3114. (float)info->chipboard[board_id].status_cmd_dx,
  3115. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3116. get_average(&info->chipboard[board_id].status_cmd_none,
  3117. (float)info->chipboard[board_id].status_cmd_none_dx,
  3118. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3119. get_average(&info->chipboard[board_id].nonces,
  3120. (float)info->chipboard[board_id].nonces_dx,
  3121. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3122. }
  3123. get_average(&info->chipboard[board_id].hashrate,
  3124. (float)info->chipboard[board_id].nonces_dx,
  3125. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3126. get_average(&info->chipboard[board_id].hashrate_good,
  3127. (float)info->chipboard[board_id].nonces_good_dx,
  3128. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3129. get_average(&info->chipboard[board_id].hashrate_diff,
  3130. (float)info->chipboard[board_id].nonces_diff_dx,
  3131. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3132. get_average(&info->chipboard[board_id].hashrate_bad,
  3133. (float)info->chipboard[board_id].nonces_bad_dx,
  3134. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3135. info->chipboard[board_id].task_switch_dx = 0;
  3136. info->chipboard[board_id].status_cmd_dx = 0;
  3137. info->chipboard[board_id].status_cmd_none_dx = 0;
  3138. info->chipboard[board_id].nonces_dx = 0;
  3139. info->chipboard[board_id].nonces_good_dx = 0;
  3140. info->chipboard[board_id].nonces_diff_dx = 0;
  3141. info->chipboard[board_id].nonces_bad_dx = 0;
  3142. u_board += (info->chipboard[board_id].u_board + U_LOSS);
  3143. #ifdef MINER_X5
  3144. i_total += (info->chipboard[board_id].i_chain1);
  3145. u_chip += (info->chipboard[board_id].u_chain1);
  3146. p_chip += (info->chipboard[board_id].p_chain1);
  3147. #endif
  3148. #ifdef MINER_X6
  3149. i_total += (info->chipboard[board_id].i_chain1 + info->chipboard[board_id].i_chain2);
  3150. u_chip += (info->chipboard[board_id].u_chain1 + info->chipboard[board_id].u_chain2);
  3151. p_chip += (info->chipboard[board_id].p_chain1 + info->chipboard[board_id].p_chain2);
  3152. #endif
  3153. p_total += info->chipboard[board_id].p_board;
  3154. if (opt_bf16_renonce != RENONCE_DISABLED) {
  3155. get_average(&info->chipboard[board_id].hashrate_re,
  3156. (float)info->chipboard[board_id].nonces_re_dx,
  3157. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3158. get_average(&info->chipboard[board_id].hashrate_re_good,
  3159. (float)info->chipboard[board_id].nonces_re_good_dx,
  3160. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3161. get_average(&info->chipboard[board_id].hashrate_re_bad,
  3162. (float)info->chipboard[board_id].nonces_re_bad_dx,
  3163. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3164. info->chipboard[board_id].nonces_re_dx = 0;
  3165. info->chipboard[board_id].nonces_re_good_dx = 0;
  3166. info->chipboard[board_id].nonces_re_bad_dx = 0;
  3167. }
  3168. get_average(&info->chipboard[board_id].txrx_speed,
  3169. (float)info->chipboard[board_id].bytes_transmitted_dx,
  3170. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3171. info->chipboard[board_id].bytes_transmitted_dx = 0;
  3172. if (opt_bf16_stats_enabled) {
  3173. if (opt_bf16_renonce != RENONCE_DISABLED) {
  3174. applog(LOG_NOTICE, "STATS: board [%d] hrate: good: [%8.3f] re_good: [%8.3f] => "
  3175. "[%8.3f] toren: [%8.3f] bad: [%8.3f] total: [%8.3f]",
  3176. board_id,
  3177. CHIP_COEFF * (info->chipboard[board_id].hashrate_good),
  3178. CHIP_COEFF * (info->chipboard[board_id].hashrate_re_good),
  3179. CHIP_COEFF * (info->chipboard[board_id].hashrate_good + info->chipboard[board_id].hashrate_re_good),
  3180. CHIP_COEFF * (info->chipboard[board_id].hashrate - info->chipboard[board_id].hashrate_good),
  3181. CHIP_COEFF * (info->chipboard[board_id].hashrate_bad),
  3182. CHIP_COEFF * (info->chipboard[board_id].hashrate));
  3183. } else {
  3184. applog(LOG_NOTICE, "STATS: board [%d] hrate: good: [%8.3f] "
  3185. "bad: [%8.3f] total: [%8.3f]",
  3186. board_id,
  3187. CHIP_COEFF * (info->chipboard[board_id].hashrate_good),
  3188. CHIP_COEFF * (info->chipboard[board_id].hashrate_bad),
  3189. CHIP_COEFF * (info->chipboard[board_id].hashrate));
  3190. }
  3191. #ifdef MINER_X5
  3192. applog(LOG_NOTICE, "STATS: TX/RX: [%5.3f] Mbit/s UB: [%3.1f] "
  3193. "U1: [%3.1f] I1: [%3.1f] T: [%3.1f] RPM: [%4d] FAN: [%3d]",
  3194. 8.0 * info->chipboard[board_id].txrx_speed / 1000000,
  3195. info->chipboard[board_id].u_board + U_LOSS,
  3196. info->chipboard[board_id].u_chain1,
  3197. info->chipboard[board_id].i_chain1,
  3198. info->chipboard[board_id].temp,
  3199. info->chipboard[board_id].rpm,
  3200. info->chipboard[board_id].fan_speed);
  3201. #endif
  3202. #ifdef MINER_X6
  3203. applog(LOG_NOTICE, "STATS: TX/RX: [%5.3f] Mbit/s UB: [%3.1f] "
  3204. "U1: [%3.1f] U2: [%3.1f] I1: [%3.1f] I2: [%3.1f] T: [%3.1f] RPM: [%4d] FAN: [%3d]",
  3205. 8.0 * info->chipboard[board_id].txrx_speed / 1000000,
  3206. info->chipboard[board_id].u_board + U_LOSS,
  3207. info->chipboard[board_id].u_chain1,
  3208. info->chipboard[board_id].u_chain2,
  3209. info->chipboard[board_id].i_chain1,
  3210. info->chipboard[board_id].i_chain2,
  3211. info->chipboard[board_id].temp,
  3212. info->chipboard[board_id].rpm,
  3213. info->chipboard[board_id].fan_speed);
  3214. #endif
  3215. applog(LOG_NOTICE, "STATS: ver: [%d] fw: [%d] hwid: [%s]",
  3216. info->chipboard[board_id].board_ver,
  3217. info->chipboard[board_id].board_fwver,
  3218. info->chipboard[board_id].board_hwid);
  3219. }
  3220. }
  3221. }
  3222. if (opt_bf16_stats_enabled) {
  3223. get_average(&info->task_switch,
  3224. (float)info->task_switch_dx,
  3225. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3226. get_average(&info->status_cmd,
  3227. (float)info->status_cmd_dx,
  3228. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3229. get_average(&info->status_cmd_none,
  3230. (float)info->status_cmd_none_dx,
  3231. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3232. get_average(&info->chipboard[board_id].nonces,
  3233. (float)info->chipboard[board_id].nonces_dx,
  3234. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3235. }
  3236. get_average(&info->hashrate,
  3237. (float)info->nonces_dx,
  3238. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3239. get_average(&info->hashrate_good,
  3240. (float)info->nonces_good_dx,
  3241. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3242. get_average(&info->hashrate_diff,
  3243. (float)info->nonces_diff_dx,
  3244. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3245. get_average(&info->hashrate_bad,
  3246. (float)info->nonces_bad_dx,
  3247. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3248. info->task_switch_dx = 0;
  3249. info->status_cmd_dx = 0;
  3250. info->status_cmd_none_dx = 0;
  3251. info->nonces_dx = 0;
  3252. info->nonces_good_dx = 0;
  3253. info->nonces_diff_dx = 0;
  3254. info->nonces_bad_dx = 0;
  3255. if (opt_bf16_renonce != RENONCE_DISABLED) {
  3256. get_average(&info->hashrate_re,
  3257. (float)info->nonces_re_dx,
  3258. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3259. get_average(&info->hashrate_re_good,
  3260. (float)info->nonces_re_good_dx,
  3261. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3262. get_average(&info->hashrate_re_bad,
  3263. (float)info->nonces_re_bad_dx,
  3264. (float)(time2 - time1), AVG_TIME_INTERVAL);
  3265. info->nonces_re_dx = 0;
  3266. info->nonces_re_good_dx = 0;
  3267. info->nonces_re_bad_dx = 0;
  3268. }
  3269. time1 = time2;
  3270. gettimeofday(&stop_time, NULL);
  3271. uint32_t uptime = time2 - time0;
  3272. uint32_t hours = uptime / 3600;
  3273. uint8_t minutes = (uptime - hours * 3600) / 60;
  3274. uint8_t seconds = uptime - hours * 3600 - minutes * 60;
  3275. info->u_avg = u_board / info->active_chipboard_num;
  3276. info->i_total = i_total;
  3277. info->p_total = p_total;
  3278. info->u_chip = u_chip;
  3279. info->p_chip = p_chip;
  3280. if (opt_bf16_stats_enabled) {
  3281. if (opt_bf16_renonce != RENONCE_DISABLED) {
  3282. applog(LOG_NOTICE, "STATS: rencs: [%d] stale: [%d] nws: [%d] rnws: [%d] failed: [%d]",
  3283. info->renonce_list->count,
  3284. info->stale_work_list->count,
  3285. info->noncework_list->count,
  3286. info->renoncework_list->count,
  3287. info->chips_failed);
  3288. applog(LOG_NOTICE, "STATS: %4.0fGH/s osc: 0x%02x re_osc: 0x%02x "
  3289. "%3.1fV %3.1fA %4.1fW %.3fW/GH",
  3290. CHIP_COEFF * (info->hashrate_good + info->hashrate_re_good),
  3291. bf16_chip_clock,
  3292. bf16_renonce_chip_clock,
  3293. info->u_avg,
  3294. info->i_total,
  3295. info->p_total,
  3296. (info->a_net == true) ?
  3297. 0.0 :
  3298. info->p_total / (CHIP_COEFF * (info->hashrate_good + info->hashrate_re_good)));
  3299. applog(LOG_NOTICE, "STATS: TOTAL: %5.1fGH/s %5.3fV %3.1fA %5.2fW "
  3300. "CHIP: %5.1fGH/s GOOD: %4.1fGH/s RENONCE: %4.1fGH/s BAD: %4.1fGH/s",
  3301. CHIP_COEFF * info->hashrate / (info->chips_num - info->renonce_chips - info->chips_failed),
  3302. info->u_chip / (info->chips_num - info->chips_disabled),
  3303. (info->a_net == true) ? 0.0 : info->p_chip / info->u_chip,
  3304. info->p_chip / (info->chips_num - info->chips_disabled),
  3305. CHIP_COEFF * (info->hashrate_good + info->hashrate_re_good) / (info->chips_num - info->renonce_chips - info->chips_failed),
  3306. CHIP_COEFF * info->hashrate_good / (info->chips_num - info->renonce_chips - info->chips_failed),
  3307. CHIP_COEFF * info->hashrate_re_good / (info->chips_num - info->renonce_chips - info->chips_failed),
  3308. CHIP_COEFF * info->hashrate_re_bad / (info->chips_num - info->renonce_chips - info->chips_failed));
  3309. #if 1
  3310. applog(LOG_NOTICE, "STATS: m0: [%lu] mm0: [%lu] m1: [%lu] mm1: [%lu] m2: [%lu] mm2: [%lu] "
  3311. "m3: [%lu] mm3: [%lu] um: [%lu]",
  3312. info->stage0_match / (time2 - time0),
  3313. info->stage0_mismatch / (time2 - time0),
  3314. info->stage1_match / (time2 - time0),
  3315. info->stage1_mismatch / (time2 - time0),
  3316. info->stage2_match / (time2 - time0),
  3317. info->stage2_mismatch / (time2 - time0),
  3318. info->stage3_match / (time2 - time0),
  3319. info->stage3_mismatch / (time2 - time0),
  3320. info->unmatched / (time2 - time0));
  3321. #endif
  3322. } else {
  3323. applog(LOG_NOTICE, "STATS: stale: [%d] nws: [%d]",
  3324. info->stale_work_list->count, info->noncework_list->count);
  3325. applog(LOG_NOTICE, "STATS: %4.0fGH/s osc: 0x%02x"
  3326. "%3.1fV %3.1fA %4.1fW %.3fW/GH",
  3327. CHIP_COEFF * info->hashrate_good,
  3328. bf16_chip_clock,
  3329. info->u_avg,
  3330. info->i_total,
  3331. info->p_total,
  3332. (info->a_net == true) ?
  3333. 0.0 :
  3334. info->p_total / (CHIP_COEFF * info->hashrate_good));
  3335. applog(LOG_NOTICE, "STATS: TOTAL: %5.1fGH/s %5.3fV %3.1fA %5.2fW "
  3336. "CHIP: %5.1fGH/s GOOD: %4.1fGH/s BAD: %4.1fGH/s",
  3337. CHIP_COEFF * info->hashrate / (info->chips_num - info->chips_failed),
  3338. info->u_chip / (info->chips_num - info->chips_disabled),
  3339. (info->a_net == true) ? 0.0 : info->p_chip / info->u_chip,
  3340. info->p_chip / (info->chips_num - info->chips_disabled),
  3341. CHIP_COEFF * info->hashrate_good / (info->chips_num - info->chips_failed),
  3342. CHIP_COEFF * info->hashrate_good / (info->chips_num - info->chips_failed),
  3343. CHIP_COEFF * info->hashrate_bad / (info->chips_num - info->chips_failed));
  3344. }
  3345. applog(LOG_NOTICE, "STATS: uptime: [%4d:%02d:%02d] time elapsed: [%.6f] ",
  3346. hours, minutes, seconds, timediff(start_time, stop_time));
  3347. }
  3348. }
  3349. cgsleep_us(STATISTICS_DELAY);
  3350. }
  3351. applog(LOG_INFO, "%s: statistics_thr: exiting...", bitfury->drv->name);
  3352. return NULL;
  3353. }
  3354. static bool bitfury16_thread_prepare(struct thr_info *thr)
  3355. {
  3356. struct cgpu_info *bitfury = thr->cgpu;
  3357. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  3358. info->thr = thr;
  3359. if (thr_info_create(&(info->chipworker_thr), NULL, bitfury_chipworker, (void *)bitfury)) {
  3360. applog(LOG_ERR, "%s: %s() chipworker thread create failed",
  3361. bitfury->drv->name, __func__);
  3362. return false;
  3363. }
  3364. pthread_detach(info->chipworker_thr.pth);
  3365. applog(LOG_INFO, "%s: thread prepare: starting chipworker thread", bitfury->drv->name);
  3366. if (thr_info_create(&(info->nonceworker_thr), NULL, bitfury_nonceworker, (void *)bitfury)) {
  3367. applog(LOG_ERR, "%s: %s() nonceworker thread create failed",
  3368. bitfury->drv->name, __func__);
  3369. return false;
  3370. }
  3371. pthread_detach(info->nonceworker_thr.pth);
  3372. applog(LOG_INFO, "%s: thread prepare: starting nonceworker thread", bitfury->drv->name);
  3373. if (opt_bf16_renonce != RENONCE_DISABLED) {
  3374. if (thr_info_create(&(info->renonceworker_thr), NULL, bitfury_renonceworker, (void *)bitfury)) {
  3375. applog(LOG_ERR, "%s: %s() renonceworker thread create failed",
  3376. bitfury->drv->name, __func__);
  3377. return false;
  3378. }
  3379. pthread_detach(info->renonceworker_thr.pth);
  3380. applog(LOG_INFO, "%s: thread prepare: starting renonceworker thread", bitfury->drv->name);
  3381. }
  3382. if (thr_info_create(&(info->hwmonitor_thr), NULL, bitfury_hwmonitor, (void *)bitfury)) {
  3383. applog(LOG_ERR, "%s: %s() hwmonitor thread create failed",
  3384. bitfury->drv->name, __func__);
  3385. return false;
  3386. }
  3387. pthread_detach(info->hwmonitor_thr.pth);
  3388. applog(LOG_INFO, "%s: thread prepare: starting hwmonitor thread", bitfury->drv->name);
  3389. if (thr_info_create(&(info->alarm_thr), NULL, bitfury_alarm, (void *)bitfury)) {
  3390. applog(LOG_ERR, "%s: %s() alarm thread create failed",
  3391. bitfury->drv->name, __func__);
  3392. return false;
  3393. }
  3394. pthread_detach(info->alarm_thr.pth);
  3395. applog(LOG_INFO, "%s: thread prepare: starting alarm thread", bitfury->drv->name);
  3396. if (thr_info_create(&(info->statistics_thr), NULL, bitfury_statistics, (void *)bitfury)) {
  3397. applog(LOG_ERR, "%s: %s() statistics thread create failed",
  3398. bitfury->drv->name, __func__);
  3399. return false;
  3400. }
  3401. pthread_detach(info->statistics_thr.pth);
  3402. applog(LOG_INFO, "%s: thread prepare: starting statistics thread", bitfury->drv->name);
  3403. return true;
  3404. }
  3405. static int64_t bitfury16_scanwork(struct thr_info *thr)
  3406. {
  3407. struct cgpu_info *bitfury = thr->cgpu;
  3408. struct bitfury16_info *info = bitfury->device_data;
  3409. int64_t hashcount = 0;
  3410. applog(LOG_INFO, "%s: scan work", bitfury->drv->name);
  3411. mutex_lock(&info->nonces_good_lock);
  3412. if (info->nonces_good_cg) {
  3413. hashcount += 0xffffffffull * info->nonces_good_cg;
  3414. info->nonces_good_cg = 0;
  3415. }
  3416. mutex_unlock(&info->nonces_good_lock);
  3417. return hashcount;
  3418. }
  3419. static void prepare_work(struct cgpu_info *bitfury, struct work *work, bool rolled)
  3420. {
  3421. struct bitfury16_info *info = (struct bitfury16_info *)bitfury->device_data;
  3422. bf_workd_t* wdata = cgmalloc(sizeof(bf_workd_t));
  3423. wdata->work = work;
  3424. wdata->rolled = rolled;
  3425. wdata->generated = time(NULL);
  3426. /* generate task payload */
  3427. cg_memcpy(wdata->payload.midstate, work->midstate, 32);
  3428. wdata->payload.m7 = *(uint32_t *)(work->data + 64);
  3429. wdata->payload.ntime = *(uint32_t *)(work->data + 68);
  3430. wdata->payload.nbits = *(uint32_t *)(work->data + 72);
  3431. workd_list_push(info->work_list, wdata);
  3432. }
  3433. static bool bitfury16_queue_full(struct cgpu_info *bitfury)
  3434. {
  3435. struct bitfury16_info *info = (struct bitfury16_info *)bitfury->device_data;
  3436. struct work *work, *usework;
  3437. uint16_t need = 0;
  3438. uint16_t generated = 0;
  3439. uint16_t roll, roll_limit;
  3440. bool rolled;
  3441. if (info->initialised == false) {
  3442. cgsleep_us(30);
  3443. return true;
  3444. }
  3445. L_LOCK(info->work_list);
  3446. uint16_t work_count = info->work_list->count;
  3447. L_UNLOCK(info->work_list);
  3448. if (work_count < WORK_QUEUE_LEN)
  3449. need = WORK_QUEUE_LEN - work_count;
  3450. else
  3451. return true;
  3452. /* Ensure we do enough rolling to reduce CPU
  3453. but dont roll too much to have them end up stale */
  3454. work = get_queued(bitfury);
  3455. if (work) {
  3456. roll_limit = work->drv_rolllimit;
  3457. roll = 0;
  3458. L_LOCK(info->work_list);
  3459. do {
  3460. if (roll == 0) {
  3461. usework = work;
  3462. rolled = false;
  3463. } else {
  3464. usework = copy_work_noffset(work, roll);
  3465. rolled = true;
  3466. }
  3467. prepare_work(bitfury, usework, rolled);
  3468. generated++;
  3469. } while ((--need > 0) && (++roll <= roll_limit));
  3470. L_UNLOCK(info->work_list);
  3471. applog(LOG_INFO, "%s: queue full: generated %d works", bitfury->drv->name, generated);
  3472. } else
  3473. cgsleep_us(30);
  3474. if (need > 0)
  3475. return false;
  3476. else
  3477. return true;
  3478. }
  3479. static void bitfury16_flush_work(struct cgpu_info *bitfury)
  3480. {
  3481. struct bitfury16_info *info = (struct bitfury16_info *)bitfury->device_data;
  3482. uint16_t flushed = 0;
  3483. if (info->initialised == false)
  3484. return;
  3485. bf_works_t works;
  3486. /* flush work list */
  3487. L_LOCK(info->work_list);
  3488. L_LOCK(info->stale_work_list);
  3489. bf_data_t* wdata = info->work_list->head;
  3490. while (wdata != NULL) {
  3491. workd_list_push(info->stale_work_list, WORKD(wdata));
  3492. workd_list_remove(info->work_list, &works);
  3493. wdata = info->work_list->head;
  3494. flushed++;
  3495. }
  3496. L_UNLOCK(info->stale_work_list);
  3497. L_UNLOCK(info->work_list);
  3498. /* flush nonces list */
  3499. L_LOCK(info->noncework_list);
  3500. bf_data_t* nwdata = info->noncework_list->head;
  3501. while (nwdata != NULL) {
  3502. noncework_list_pop(info->noncework_list);
  3503. nwdata = info->noncework_list->head;
  3504. }
  3505. L_UNLOCK(info->noncework_list);
  3506. if (opt_bf16_renonce != RENONCE_DISABLED) {
  3507. /* flush renonces list */
  3508. L_LOCK(info->renonce_list);
  3509. bf_data_t* rdata = info->renonce_list->head;
  3510. while (rdata != NULL) {
  3511. renonce_list_pop(info->renonce_list);
  3512. rdata = info->renonce_list->head;
  3513. }
  3514. L_UNLOCK(info->renonce_list);
  3515. /* flush renoncework list */
  3516. L_LOCK(info->renoncework_list);
  3517. bf_data_t* rnwdata = info->renoncework_list->head;
  3518. while (rnwdata != NULL) {
  3519. renoncework_list_pop(info->renoncework_list);
  3520. rnwdata = info->renoncework_list->head;
  3521. }
  3522. L_UNLOCK(info->renoncework_list);
  3523. }
  3524. applog(LOG_INFO, "%s: flushed %d works", bitfury->drv->name, flushed);
  3525. }
  3526. static struct api_data *bitfury16_api_stats(struct cgpu_info *bitfury)
  3527. {
  3528. uint8_t board_id, bcm250_id, chip_id;
  3529. struct bitfury16_info *info = bitfury->device_data;
  3530. char data[128];
  3531. char value[128];
  3532. struct api_data *root = NULL;
  3533. applog(LOG_INFO, "%s: API stats", bitfury->drv->name);
  3534. if (info->initialised == false)
  3535. return NULL;
  3536. #ifdef MINER_X5
  3537. root = api_add_string(root, "Device name", "Bitfury X5", true);
  3538. #endif
  3539. #ifdef MINER_X6
  3540. root = api_add_string(root, "Device name", "Bitfury X6", true);
  3541. #endif
  3542. root = api_add_uint8(root, "Boards number", &info->chipboard_num, false);
  3543. root = api_add_uint8(root, "Boards detected", &info->chipboard_num, false);
  3544. root = api_add_uint8(root, "Chips number", &info->chips_num, false);
  3545. /* software revision chages according to comments in CHANGELOG */
  3546. root = api_add_string(root, "hwv1", "1", true);
  3547. root = api_add_string(root, "hwv2", "2", true);
  3548. root = api_add_string(root, "hwv3", "11", true);
  3549. root = api_add_string(root, "hwv4", "0", true);
  3550. root = api_add_string(root, "hwv5", "0", true);
  3551. /* U avg */
  3552. sprintf(value, "%.1f", info->u_avg);
  3553. root = api_add_string(root, "U avg", value, true);
  3554. /* I total */
  3555. sprintf(value, "%.1f", info->i_total);
  3556. root = api_add_string(root, "I total", value, true);
  3557. /* P total */
  3558. sprintf(value, "%.1f", info->p_total);
  3559. root = api_add_string(root, "P total", value, true);
  3560. if (opt_bf16_renonce != RENONCE_DISABLED) {
  3561. /* Efficiency */
  3562. sprintf(value, "%.3f",
  3563. (info->a_net == true) ?
  3564. 0.0 :
  3565. info->p_total / (CHIP_COEFF * (info->hashrate_good + info->hashrate_re_good)));
  3566. root = api_add_string(root, "Efficiency", value, true);
  3567. /* Chip GHS total avg */
  3568. sprintf(value, "%.1f", CHIP_COEFF * info->hashrate / (info->chips_num - info->renonce_chips - info->chips_failed));
  3569. root = api_add_string(root, "Chip GHS total avg", value, true);
  3570. /* Chip GHS avg */
  3571. sprintf(value, "%.1f", CHIP_COEFF * (info->hashrate_good + info->hashrate_re_good) / (info->chips_num - info->renonce_chips - info->chips_failed));
  3572. root = api_add_string(root, "Chip GHS avg", value, true);
  3573. /* Chip GHS good avg */
  3574. sprintf(value, "%.1f", CHIP_COEFF * info->hashrate_good / (info->chips_num - info->renonce_chips - info->chips_failed));
  3575. root = api_add_string(root, "Chip GHS good avg", value, true);
  3576. /* Chip GHS re avg */
  3577. sprintf(value, "%.1f", CHIP_COEFF * info->hashrate_re_good / (info->chips_num - info->renonce_chips - info->chips_failed));
  3578. root = api_add_string(root, "Chip GHS re avg", value, true);
  3579. /* Chip GHS bad avg */
  3580. sprintf(value, "%.1f", CHIP_COEFF * info->hashrate_re_bad / (info->chips_num - info->renonce_chips - info->chips_failed));
  3581. root = api_add_string(root, "Chip GHS bad avg", value, true);
  3582. } else {
  3583. /* Efficiency */
  3584. sprintf(value, "%.3f",
  3585. (info->a_net == true) ?
  3586. 0.0 :
  3587. info->p_total / (CHIP_COEFF * info->hashrate_good));
  3588. root = api_add_string(root, "Efficiency", value, true);
  3589. /* Chip GHS total avg */
  3590. sprintf(value, "%.1f", CHIP_COEFF * info->hashrate / (info->chips_num - info->chips_failed));
  3591. root = api_add_string(root, "Chip GHS total avg", value, true);
  3592. /* Chip GHS avg */
  3593. sprintf(value, "%.1f", CHIP_COEFF * info->hashrate_good / (info->chips_num - info->chips_failed));
  3594. root = api_add_string(root, "Chip GHS avg", value, true);
  3595. /* Chip GHS good avg */
  3596. sprintf(value, "%.1f", CHIP_COEFF * info->hashrate_good / (info->chips_num - info->chips_failed));
  3597. root = api_add_string(root, "Chip GHS good avg", value, true);
  3598. /* Chip GHS re avg */
  3599. sprintf(value, "%.1f", 0.0);
  3600. root = api_add_string(root, "Chip GHS re avg", value, true);
  3601. /* Chip GHS bad avg */
  3602. sprintf(value, "%.1f", CHIP_COEFF * info->hashrate_bad / (info->chips_num - info->chips_failed));
  3603. root = api_add_string(root, "Chip GHS bad avg", value, true);
  3604. }
  3605. /* U Chip avg */
  3606. sprintf(value, "%.3f", info->u_chip / (info->chips_num - info->chips_disabled));
  3607. root = api_add_string(root, "U Chip avg", value, true);
  3608. /* I Chip avg */
  3609. sprintf(value, "%.1f", (info->a_net == true) ? 0.0 : info->p_chip / info->u_chip);
  3610. root = api_add_string(root, "I Chip avg", value, true);
  3611. /* P Chip avg */
  3612. sprintf(value, "%.2f", info->p_chip / (info->chips_num - info->chips_disabled));
  3613. root = api_add_string(root, "P Chip avg", value, true);
  3614. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  3615. /* board status */
  3616. sprintf(data, "Board%d detected", board_id);
  3617. if (info->chipboard[board_id].detected == true)
  3618. root = api_add_string(root, data, "1", true);
  3619. else
  3620. root = api_add_string(root, data, "0", true);
  3621. /* number of concentratord on board */
  3622. sprintf(data, "Board%d BTC250", board_id);
  3623. root = api_add_uint8(root, data, &info->chipboard[board_id].bcm250_num, false);
  3624. /* number of chips on board */
  3625. sprintf(data, "Board%d BTC16", board_id);
  3626. root = api_add_uint8(root, data, &info->chipboard[board_id].chips_num, false);
  3627. /* number of good chips on board */
  3628. sprintf(data, "Board%d BTC16 good", board_id);
  3629. uint8_t chips_good = info->chipboard[board_id].chips_num - info->chipboard[board_id].chips_failed;
  3630. root = api_add_uint8(root, data, &chips_good, true);
  3631. sprintf(data, "Board%d Clock", board_id);
  3632. if (info->chipboard[board_id].detected == true)
  3633. sprintf(value, "%d (0x%02x)", bf16_chip_clock, bf16_chip_clock);
  3634. else
  3635. sprintf(value, "%d (0x%02x)", 0, 0);
  3636. root = api_add_string(root, data, value, true);
  3637. sprintf(data, "Board%d GHS av", board_id);
  3638. if (opt_bf16_renonce != RENONCE_DISABLED) {
  3639. sprintf(value, "%.2f",
  3640. CHIP_COEFF * (info->chipboard[board_id].hashrate_good + info->chipboard[board_id].hashrate_re_good));
  3641. } else {
  3642. sprintf(value, "%.2f", CHIP_COEFF * (info->chipboard[board_id].hashrate_good));
  3643. }
  3644. root = api_add_string(root, data, value, true);
  3645. /* number of chips per concentrator */
  3646. uint8_t i = 0;
  3647. memset(value, 0, sizeof(value));
  3648. for (bcm250_id = 0; bcm250_id < BCM250_NUM; bcm250_id++) {
  3649. sprintf(data, "Board%d BTC250_%d BTC16", board_id, bcm250_id);
  3650. root = api_add_uint8(root, data, &info->chipboard[board_id].bcm250[bcm250_id].chips_num, false);
  3651. uint8_t first_good_chip = info->chipboard[board_id].bcm250[bcm250_id].first_good_chip;
  3652. uint8_t last_good_chip = info->chipboard[board_id].bcm250[bcm250_id].last_good_chip;
  3653. /* chips loop */
  3654. for (chip_id = first_good_chip; chip_id < last_good_chip; chip_id++, i++) {
  3655. bf_chip_address_t chip_address;
  3656. chip_address.board_id = board_id;
  3657. chip_address.bcm250_id = bcm250_id;
  3658. chip_address.chip_id = chip_id;
  3659. if (info->chipboard[board_id].bcm250[bcm250_id].chips[chip_id].status < FAILING) {
  3660. if ((opt_bf16_renonce != RENONCE_DISABLED) &&
  3661. (renonce_chip(chip_address) == 1))
  3662. value[i] = 'O';
  3663. else
  3664. value[i] = 'o';
  3665. } else {
  3666. if ((opt_bf16_renonce != RENONCE_DISABLED) &&
  3667. (renonce_chip(chip_address) == 1))
  3668. value[i] = 'X';
  3669. else
  3670. value[i] = 'x';
  3671. }
  3672. }
  3673. if (bcm250_id != BCM250_NUM - 1)
  3674. value[i++] = ' ';
  3675. }
  3676. sprintf(data, "Board%d BF16 Status", board_id);
  3677. root = api_add_string(root, data, value, true);
  3678. /* MSP version data */
  3679. /* board version */
  3680. sprintf(data, "Board%d Ver", board_id);
  3681. root = api_add_uint32(root, data, &info->chipboard[board_id].board_ver, false);
  3682. /* board firmware version */
  3683. sprintf(data, "Board%d FW", board_id);
  3684. root = api_add_uint32(root, data, &info->chipboard[board_id].board_fwver, false);
  3685. /* board hardware id */
  3686. sprintf(data, "Board%d HWID", board_id);
  3687. root = api_add_string(root, data, info->chipboard[board_id].board_hwid, true);
  3688. /* MSP hw data */
  3689. /* T */
  3690. sprintf(data, "Board%d T", board_id);
  3691. sprintf(value, "%.1f", info->chipboard[board_id].temp);
  3692. root = api_add_string(root, data, value, true);
  3693. /* UB */
  3694. sprintf(data, "Board%d UB", board_id);
  3695. sprintf(value, "%.1f", info->chipboard[board_id].u_board);
  3696. root = api_add_string(root, data, value, true);
  3697. /* P1 */
  3698. sprintf(data, "Board%d P1", board_id);
  3699. root = api_add_uint8(root, data, &info->chipboard[board_id].p_chain1_enabled, false);
  3700. /* P2 */
  3701. sprintf(data, "Board%d P2", board_id);
  3702. root = api_add_uint8(root, data, &info->chipboard[board_id].p_chain2_enabled, false);
  3703. /* U1 */
  3704. sprintf(data, "Board%d U1", board_id);
  3705. sprintf(value, "%.1f", info->chipboard[board_id].u_chain1);
  3706. root = api_add_string(root, data, value, true);
  3707. /* U2 */
  3708. sprintf(data, "Board%d U2", board_id);
  3709. sprintf(value, "%.1f", info->chipboard[board_id].u_chain2);
  3710. root = api_add_string(root, data, value, true);
  3711. /* I1 */
  3712. sprintf(data, "Board%d I1", board_id);
  3713. sprintf(value, "%.1f", info->chipboard[board_id].i_chain1);
  3714. root = api_add_string(root, data, value, true);
  3715. /* I2 */
  3716. sprintf(data, "Board%d I2", board_id);
  3717. sprintf(value, "%.1f", info->chipboard[board_id].i_chain2);
  3718. root = api_add_string(root, data, value, true);
  3719. /* RPM */
  3720. sprintf(data, "Board%d PRM", board_id);
  3721. root = api_add_uint32(root, data, &info->chipboard[board_id].rpm, true);
  3722. /* A */
  3723. sprintf(data, "Board%d T_Alarm", board_id);
  3724. root = api_add_uint8(root, data, &info->chipboard[board_id].a_temp, true);
  3725. sprintf(data, "Board%d I1_Alarm", board_id);
  3726. root = api_add_uint8(root, data, &info->chipboard[board_id].a_ichain1, true);
  3727. sprintf(data, "Board%d I2_Alarm", board_id);
  3728. root = api_add_uint8(root, data, &info->chipboard[board_id].a_ichain2, true);
  3729. /* F */
  3730. sprintf(data, "Board%d F", board_id);
  3731. root = api_add_uint8(root, data, &info->chipboard[board_id].fan_speed, false);
  3732. /* AI */
  3733. sprintf(data, "Board%d AI", board_id);
  3734. sprintf(value, "%.1f", info->chipboard[board_id].i_alarm);
  3735. root = api_add_string(root, data, value, true);
  3736. /* AT */
  3737. sprintf(data, "Board%d AT", board_id);
  3738. sprintf(value, "%.0f", info->chipboard[board_id].t_alarm);
  3739. root = api_add_string(root, data, value, true);
  3740. /* AG */
  3741. sprintf(data, "Board%d AG", board_id);
  3742. sprintf(value, "%.0f", info->chipboard[board_id].t_gisteresis);
  3743. root = api_add_string(root, data, value, true);
  3744. /* FM */
  3745. sprintf(data, "Board%d FM", board_id);
  3746. #ifdef MINER_X5
  3747. sprintf(value, "%c", info->chipboard[board_id].fan_mode);
  3748. #endif
  3749. #ifdef MINER_X6
  3750. if (manual_pid_enabled == true)
  3751. sprintf(value, "A");
  3752. else
  3753. sprintf(value, "%c", info->chipboard[board_id].fan_mode);
  3754. #endif
  3755. root = api_add_string(root, data, value, true);
  3756. /* TT */
  3757. sprintf(data, "Board%d TT", board_id);
  3758. sprintf(value, "%.0f", info->chipboard[board_id].target_temp);
  3759. root = api_add_string(root, data, value, true);
  3760. }
  3761. return root;
  3762. }
  3763. static void bitfury16_shutdown(struct thr_info *thr)
  3764. {
  3765. struct cgpu_info *bitfury = thr->cgpu;
  3766. struct bitfury16_info *info = (struct bitfury16_info *)bitfury->device_data;
  3767. uint8_t board_id;
  3768. bitfury->shutdown = true;
  3769. cgsleep_ms(300);
  3770. /* flush next board temp to default value */
  3771. if (manual_pid_enabled == false) {
  3772. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  3773. if (info->chipboard[board_id].detected == true) {
  3774. if (device_uart_transfer(board_id + 1, "N") < 0)
  3775. quit(1, "%s: %s() failed to set BOARD%d next temp",
  3776. bitfury->drv->name, __func__, board_id + 1);
  3777. applog(LOG_INFO, "%s: set BOARD%d next temp to default value",
  3778. bitfury->drv->name, board_id + 1);
  3779. }
  3780. }
  3781. } else {
  3782. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  3783. if (info->chipboard[board_id].detected == true) {
  3784. if (device_uart_transfer(board_id + 1, "F") < 0)
  3785. quit(1, "%s: %s() failed to set BOARD%d fan speed to auto mode",
  3786. bitfury->drv->name, __func__, board_id + 1);
  3787. applog(LOG_INFO, "%s: set BOARD%d fan speed to auto mode",
  3788. bitfury->drv->name, board_id + 1);
  3789. }
  3790. }
  3791. }
  3792. /* disable power chain */
  3793. for (board_id = 0; board_id < CHIPBOARD_NUM; board_id++) {
  3794. disable_power_chain(bitfury, board_id, 0);
  3795. }
  3796. mutex_destroy(&info->nonces_good_lock);
  3797. /* close devices */
  3798. close_spi_device(SPI_CHANNEL1);
  3799. close_spi_device(SPI_CHANNEL2);
  3800. close_ctrl_device();
  3801. close_uart_device(UART_CHANNEL1);
  3802. close_uart_device(UART_CHANNEL2);
  3803. #ifdef FILELOG
  3804. filelog(info, "%s: cgminer stopped", bitfury->drv->name);
  3805. fclose(info->logfile);
  3806. mutex_destroy(&info->logfile_mutex);
  3807. #endif
  3808. applog(LOG_INFO, "%s: driver shutdown", bitfury->drv->name);
  3809. }
  3810. /* Currently hardcoded to BF1 devices */
  3811. struct device_drv bitfury16_drv = {
  3812. .drv_id = DRIVER_bitfury16,
  3813. .dname = "bitfury16",
  3814. .name = "BF16",
  3815. .drv_detect = bitfury16_detect,
  3816. .get_api_stats = bitfury16_api_stats,
  3817. .identify_device = bitfury16_identify,
  3818. .thread_prepare = bitfury16_thread_prepare,
  3819. .hash_work = hash_queued_work,
  3820. .scanwork = bitfury16_scanwork,
  3821. .queue_full = bitfury16_queue_full,
  3822. .flush_work = bitfury16_flush_work,
  3823. .thread_shutdown = bitfury16_shutdown
  3824. };