driver-bitmain.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847
  1. /*
  2. * Copyright 2012-2013 Lingchao Xu <lingchao.xu@bitmaintech.com>
  3. * Copyright 2014 Andrew Smith
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #include "compat.h"
  12. #include "miner.h"
  13. #ifndef LINUX
  14. #ifdef USE_ANT_S1
  15. static void ants1_detect(__maybe_unused bool hotplug)
  16. {
  17. }
  18. #endif
  19. #ifdef USE_ANT_S2
  20. static void ants2_detect(__maybe_unused bool hotplug)
  21. {
  22. }
  23. #endif
  24. #else // LINUX
  25. #include "elist.h"
  26. #include "usbutils.h"
  27. #include "driver-bitmain.h"
  28. #include "hexdump.c"
  29. #include "util.h"
  30. #include <fcntl.h>
  31. #include <unistd.h>
  32. #include <math.h>
  33. #ifdef USE_ANT_S1
  34. #define ANTDRV ants1_drv
  35. #else
  36. #define ANTDRV ants2_drv
  37. #endif
  38. #define BITMAIN_CALC_DIFF1 1
  39. bool opt_bitmain_hwerror = false;
  40. #ifdef USE_ANT_S2
  41. bool opt_bitmain_checkall = false;
  42. bool opt_bitmain_checkn2diff = false;
  43. bool opt_bitmain_beeper = false;
  44. bool opt_bitmain_tempoverctrl = true;
  45. #endif
  46. int opt_bitmain_temp = BITMAIN_TEMP_TARGET;
  47. int opt_bitmain_overheat = BITMAIN_TEMP_OVERHEAT;
  48. int opt_bitmain_fan_min = BITMAIN_DEFAULT_FAN_MIN_PWM;
  49. int opt_bitmain_fan_max = BITMAIN_DEFAULT_FAN_MAX_PWM;
  50. int opt_bitmain_freq_min = BITMAIN_MIN_FREQUENCY;
  51. int opt_bitmain_freq_max = BITMAIN_MAX_FREQUENCY;
  52. bool opt_bitmain_auto;
  53. static bool is_usb;
  54. static int option_offset = -1;
  55. #ifdef USE_ANT_S1
  56. static unsigned char bit_swap_table[256] =
  57. {
  58. 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
  59. 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
  60. 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
  61. 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
  62. 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
  63. 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
  64. 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
  65. 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
  66. 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
  67. 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
  68. 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
  69. 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
  70. 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
  71. 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
  72. 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
  73. 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
  74. 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
  75. 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
  76. 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
  77. 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
  78. 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
  79. 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
  80. 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
  81. 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
  82. 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
  83. 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
  84. 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
  85. 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
  86. 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
  87. 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
  88. 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
  89. 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
  90. };
  91. #define bitswap(x) (bit_swap_table[x])
  92. #else
  93. #define bitswap(x) (x)
  94. #endif
  95. // --------------------------------------------------------------
  96. // CRC16 check table
  97. // --------------------------------------------------------------
  98. const uint8_t chCRCHTalbe[] = // CRC high byte table
  99. {
  100. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  101. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  102. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  103. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  104. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  105. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  106. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  107. 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  108. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  109. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  110. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  111. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  112. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  113. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  114. 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  115. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  116. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  117. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  118. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  119. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  120. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  121. 0x00, 0xC1, 0x81, 0x40
  122. };
  123. const uint8_t chCRCLTalbe[] = // CRC low byte table
  124. {
  125. 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
  126. 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
  127. 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9,
  128. 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
  129. 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
  130. 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32,
  131. 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D,
  132. 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38,
  133. 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF,
  134. 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
  135. 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1,
  136. 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4,
  137. 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB,
  138. 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA,
  139. 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
  140. 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
  141. 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97,
  142. 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E,
  143. 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89,
  144. 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
  145. 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
  146. 0x41, 0x81, 0x80, 0x40
  147. };
  148. static uint16_t CRC16(const uint8_t* p_data, uint16_t w_len)
  149. {
  150. uint8_t chCRCHi = 0xFF; // CRC high byte initialize
  151. uint8_t chCRCLo = 0xFF; // CRC low byte initialize
  152. uint16_t wIndex = 0; // CRC cycling index
  153. while (w_len--) {
  154. wIndex = chCRCLo ^ *p_data++;
  155. chCRCLo = chCRCHi ^ chCRCHTalbe[wIndex];
  156. chCRCHi = chCRCLTalbe[wIndex];
  157. }
  158. return ((chCRCHi << 8) | chCRCLo);
  159. }
  160. static uint32_t num2bit(int num)
  161. {
  162. if (num < 0 || num > 31)
  163. return 0;
  164. else
  165. return (((uint32_t)1) << (31 - num));
  166. }
  167. static bool get_options(int this_option_offset, int *baud, int *chain_num,
  168. int *asic_num, int *timeout, int *frequency, uint8_t * reg_data)
  169. {
  170. char buf[BUFSIZ+1];
  171. char *ptr, *comma, *colon, *colon2, *colon3, *colon4, *colon5;
  172. size_t max;
  173. int i, tmp;
  174. if (opt_bitmain_options == NULL)
  175. buf[0] = '\0';
  176. else {
  177. ptr = opt_bitmain_options;
  178. for (i = 0; i < this_option_offset; i++) {
  179. comma = strchr(ptr, ',');
  180. if (comma == NULL)
  181. break;
  182. ptr = comma + 1;
  183. }
  184. comma = strchr(ptr, ',');
  185. if (comma == NULL)
  186. max = strlen(ptr);
  187. else
  188. max = comma - ptr;
  189. if (max > BUFSIZ)
  190. max = BUFSIZ;
  191. strncpy(buf, ptr, max);
  192. buf[max] = '\0';
  193. }
  194. if (!(*buf))
  195. return false;
  196. colon = strchr(buf, ':');
  197. if (colon)
  198. *(colon++) = '\0';
  199. tmp = atoi(buf);
  200. switch (tmp) {
  201. case 115200:
  202. *baud = 115200;
  203. break;
  204. case 57600:
  205. *baud = 57600;
  206. break;
  207. case 38400:
  208. *baud = 38400;
  209. break;
  210. case 19200:
  211. *baud = 19200;
  212. break;
  213. default:
  214. quit(1, "Invalid bitmain-options for baud (%s) "
  215. "must be 115200, 57600, 38400 or 19200", buf);
  216. }
  217. if (colon && *colon) {
  218. colon2 = strchr(colon, ':');
  219. if (colon2)
  220. *(colon2++) = '\0';
  221. if (*colon) {
  222. tmp = atoi(colon);
  223. if (tmp > 0)
  224. *chain_num = tmp;
  225. else {
  226. quit(1, "Invalid bitmain-options for "
  227. "chain_num (%s) must be 1 ~ %d",
  228. colon, BITMAIN_DEFAULT_CHAIN_NUM);
  229. }
  230. }
  231. if (colon2 && *colon2) {
  232. colon3 = strchr(colon2, ':');
  233. if (colon3)
  234. *(colon3++) = '\0';
  235. tmp = atoi(colon2);
  236. if (tmp > 0 && tmp <= BITMAIN_DEFAULT_ASIC_NUM)
  237. *asic_num = tmp;
  238. else {
  239. quit(1, "Invalid bitmain-options for "
  240. "asic_num (%s) must be 1 ~ %d",
  241. colon2, BITMAIN_DEFAULT_ASIC_NUM);
  242. }
  243. if (colon3 && *colon3) {
  244. colon4 = strchr(colon3, ':');
  245. if (colon4)
  246. *(colon4++) = '\0';
  247. tmp = atoi(colon3);
  248. if (tmp > 0 && tmp <= 0xff)
  249. *timeout = tmp;
  250. else {
  251. quit(1, "Invalid bitmain-options for "
  252. "timeout (%s) must be 1 ~ %d",
  253. colon3, 0xff);
  254. }
  255. if (colon4 && *colon4) {
  256. colon5 = strchr(colon4, ':');
  257. if (colon5)
  258. *(colon5++) = '\0';
  259. tmp = atoi(colon4);
  260. if (tmp < BITMAIN_MIN_FREQUENCY ||
  261. tmp > BITMAIN_MAX_FREQUENCY) {
  262. quit(1, "Invalid bitmain-options for frequency,"
  263. " must be %d <= frequency <= %d",
  264. BITMAIN_MIN_FREQUENCY,
  265. BITMAIN_MAX_FREQUENCY);
  266. } else
  267. *frequency = tmp;
  268. if (colon5 && *colon5) {
  269. if (strlen(colon5) > 8 ||
  270. strlen(colon5)%2 != 0 ||
  271. strlen(colon5)/2 == 0) {
  272. quit(1, "Invalid bitmain-options for"
  273. " reg data, must be hex now: %s",
  274. colon5);
  275. }
  276. memset(reg_data, 0, 4);
  277. if (!hex2bin(reg_data, colon5, strlen(colon5)/2)) {
  278. quit(1, "Invalid bitmain-options for reg"
  279. " data, hex2bin error now: %s",
  280. colon5);
  281. }
  282. }
  283. }
  284. }
  285. }
  286. }
  287. return true;
  288. }
  289. #ifdef USE_ANT_S1
  290. static int bitmain_set_txconfig(struct bitmain_txconfig_token *bm,
  291. uint8_t reset, uint8_t fan_eft, uint8_t timeout_eft, uint8_t frequency_eft,
  292. uint8_t voltage_eft, uint8_t chain_check_time_eft, uint8_t chip_config_eft,
  293. uint8_t hw_error_eft, uint8_t chain_num, uint8_t asic_num,
  294. uint8_t fan_pwm_data, uint8_t timeout_data,
  295. uint16_t frequency, uint8_t voltage, uint8_t chain_check_time,
  296. uint8_t chip_address, uint8_t reg_address, uint8_t * reg_data)
  297. #else
  298. static int bitmain_set_txconfig(struct bitmain_txconfig_token *bm,
  299. uint8_t reset, uint8_t fan_eft, uint8_t timeout_eft, uint8_t frequency_eft,
  300. uint8_t voltage_eft, uint8_t chain_check_time_eft, uint8_t chip_config_eft,
  301. uint8_t hw_error_eft, uint8_t beeper_ctrl, uint8_t temp_over_ctrl,
  302. uint8_t chain_num, uint8_t asic_num,
  303. uint8_t fan_pwm_data, uint8_t timeout_data,
  304. uint16_t frequency, uint8_t voltage, uint8_t chain_check_time,
  305. uint8_t chip_address, uint8_t reg_address, uint8_t * reg_data)
  306. #endif
  307. {
  308. uint16_t crc = 0;
  309. int datalen = 0;
  310. #ifdef USE_ANT_S2
  311. uint8_t version = 0;
  312. #endif
  313. uint8_t *sendbuf = (uint8_t *)bm;
  314. if (unlikely(!bm)) {
  315. applog(LOG_WARNING, "%s: %s() bm is null", ANTDRV.dname, __func__);
  316. return -1;
  317. }
  318. if (unlikely(timeout_data <= 0 || asic_num <= 0 || chain_num <= 0)) {
  319. applog(LOG_WARNING, "%s: %s() parameter invalid"
  320. " timeout_data(%d) asic_num(%d) chain_num(%d)",
  321. ANTDRV.dname, __func__,
  322. (int)timeout_data, (int)asic_num, (int)chain_num);
  323. return -1;
  324. }
  325. datalen = sizeof(struct bitmain_txconfig_token);
  326. memset(bm, 0, datalen);
  327. bm->token_type = BITMAIN_TOKEN_TYPE_TXCONFIG;
  328. #ifdef USE_ANT_S1
  329. bm->length = datalen-2;
  330. #else
  331. bm->version = version;
  332. bm->length = datalen-4;
  333. bm->length = htole16(bm->length);
  334. #endif
  335. bm->reset = reset;
  336. bm->fan_eft = fan_eft;
  337. bm->timeout_eft = timeout_eft;
  338. bm->frequency_eft = frequency_eft;
  339. bm->voltage_eft = voltage_eft;
  340. bm->chain_check_time_eft = chain_check_time_eft;
  341. bm->chip_config_eft = chip_config_eft;
  342. bm->hw_error_eft = hw_error_eft;
  343. #ifdef USE_ANT_S1
  344. sendbuf[2] = bitswap(sendbuf[2]);
  345. #else
  346. bm->beeper_ctrl = beeper_ctrl;
  347. bm->temp_over_ctrl = temp_over_ctrl;
  348. sendbuf[4] = bitswap(sendbuf[4]);
  349. sendbuf[5] = bitswap(sendbuf[5]);
  350. #endif
  351. bm->chain_num = chain_num;
  352. bm->asic_num = asic_num;
  353. bm->fan_pwm_data = fan_pwm_data;
  354. bm->timeout_data = timeout_data;
  355. bm->frequency = htole16(frequency);
  356. bm->voltage = voltage;
  357. bm->chain_check_time = chain_check_time;
  358. memcpy(bm->reg_data, reg_data, 4);
  359. bm->chip_address = chip_address;
  360. bm->reg_address = reg_address;
  361. crc = CRC16((uint8_t *)bm, datalen-2);
  362. bm->crc = htole16(crc);
  363. #ifdef USE_ANT_S1
  364. applogsiz(LOG_DEBUG, 512, "%s: %s() reset(%d) faneft(%d) touteft(%d) freqeft(%d)"
  365. " volteft(%d) chainceft(%d) chipceft(%d) hweft(%d) mnum(%d)"
  366. " anum(%d) fanpwmdata(%d) toutdata(%d) freq(%d) volt(%d)"
  367. " chainctime(%d) regdata(%02x%02x%02x%02x) chipaddr(%02x)"
  368. " regaddr(%02x) crc(%04x)",
  369. ANTDRV.dname, __func__,
  370. (int)reset, (int)fan_eft, (int)timeout_eft, (int)frequency_eft,
  371. (int)voltage_eft, (int)chain_check_time_eft, (int)chip_config_eft,
  372. (int)hw_error_eft, (int)chain_num, (int)asic_num, (int)fan_pwm_data,
  373. (int)timeout_data, (int)frequency, (int)voltage, (int)chain_check_time,
  374. (int)reg_data[0], (int)reg_data[1], (int)reg_data[2], (int)reg_data[3],
  375. (int)chip_address, (int)reg_address, (int)crc);
  376. #else
  377. applogsiz(LOG_DEBUG, 512, "%s: %s() v(%d) reset(%d) faneft(%d) touteft(%d) freqeft(%d)"
  378. " volteft(%d) chainceft(%d) chipceft(%d) hweft(%d)"
  379. " beepctrl(%d) toverctl(%d) mnum(%d)"
  380. " anum(%d) fanpwmdata(%d) toutdata(%d) freq(%d) volt(%d)"
  381. " chainctime(%d) regdata(%02x%02x%02x%02x) chipaddr(%02x)"
  382. " regaddr(%02x) crc(%04x)",
  383. ANTDRV.dname, __func__,
  384. (int)version, (int)reset, (int)fan_eft, (int)timeout_eft,
  385. (int)frequency_eft, (int)voltage_eft, (int)chain_check_time_eft,
  386. (int)chip_config_eft, (int)hw_error_eft, (int)beeper_ctrl,
  387. (int)temp_over_ctrl, (int)chain_num, (int)asic_num, (int)fan_pwm_data,
  388. (int)timeout_data, (int)frequency, (int)voltage, (int)chain_check_time,
  389. (int)reg_data[0], (int)reg_data[1], (int)reg_data[2], (int)reg_data[3],
  390. (int)chip_address, (int)reg_address, (int)crc);
  391. #endif
  392. return datalen;
  393. }
  394. static int bitmain_set_txtask(struct bitmain_info *info, uint8_t *sendbuf,
  395. unsigned int *last_work_block, int *sentcount)
  396. {
  397. uint16_t crc = 0;
  398. uint32_t work_id = 0;
  399. int datalen = 0;
  400. uint8_t new_block = 0;
  401. //char *ob_hex = NULL;
  402. struct bitmain_txtask_token *bm = (struct bitmain_txtask_token *)sendbuf;
  403. int cursentcount = 0;
  404. #ifdef USE_ANT_S2
  405. uint8_t version = 0;
  406. int diffbits, lowestdiffbits = -1;
  407. double workdiff;
  408. #endif
  409. K_ITEM *witem;
  410. *sentcount = 0;
  411. if (unlikely(!bm)) {
  412. applog(LOG_WARNING, "%s: %s() bm is null", ANTDRV.dname, __func__);
  413. return -1;
  414. }
  415. memset(bm, 0, sizeof(struct bitmain_txtask_token));
  416. bm->token_type = BITMAIN_TOKEN_TYPE_TXTASK;
  417. #ifdef USE_ANT_S2
  418. bm->version = version;
  419. if (info->wbuild->head)
  420. quithere(1, "%s: %s() wbuild wasn't empty", ANTDRV.dname, __func__);
  421. #endif
  422. datalen = 10;
  423. applog(LOG_DEBUG, "%s: send work count %d", ANTDRV.dname, info->work_ready->count);
  424. while (info->work_ready->count) {
  425. witem = k_unlink_tail(info->work_ready);
  426. if (DATAW(witem)->work->work_block > *last_work_block) {
  427. applog(LOG_ERR, "%s: send task new block %d old(%d)",
  428. ANTDRV.dname,
  429. DATAW(witem)->work->work_block, *last_work_block);
  430. new_block = 1;
  431. *last_work_block = DATAW(witem)->work->work_block;
  432. }
  433. work_id = DATAW(witem)->wid;
  434. bm->works[cursentcount].work_id = htole32(work_id);
  435. applog(LOG_DEBUG, "%s: send task work id:%"PRIu32" %"PRIu32,
  436. ANTDRV.dname,
  437. bm->works[cursentcount].work_id, work_id);
  438. memcpy(bm->works[cursentcount].midstate, DATAW(witem)->work->midstate, 32);
  439. memcpy(bm->works[cursentcount].data2, DATAW(witem)->work->data + 64, 12);
  440. cursentcount++;
  441. #ifdef USE_ANT_S1
  442. k_add_head(info->work_list, witem);
  443. #else
  444. k_add_head(info->wbuild, witem);
  445. diffbits = (int)floor(log2(DATAW(witem)->work->sdiff));
  446. if (diffbits < 0)
  447. diffbits = 0;
  448. // Limit to 4096 so solo mining has reasonable mining stats
  449. if (diffbits > 12)
  450. diffbits = 12;
  451. // Must use diffbits <= all work being sent
  452. if (lowestdiffbits == -1 || lowestdiffbits > diffbits)
  453. lowestdiffbits = diffbits;
  454. #endif
  455. }
  456. if (cursentcount <= 0) {
  457. applog(LOG_ERR, "%s: send work count %d", ANTDRV.dname, cursentcount);
  458. return 0;
  459. }
  460. #ifdef USE_ANT_S2
  461. workdiff = pow(2.0, (double)lowestdiffbits);
  462. witem = info->wbuild->head;
  463. while (witem) {
  464. DATAW(witem)->work->device_diff = workdiff;
  465. witem = witem->next;
  466. }
  467. k_list_transfer_to_head(info->wbuild, info->work_list);
  468. #endif
  469. datalen += 48*cursentcount;
  470. bm->length = datalen-4;
  471. bm->length = htole16(bm->length);
  472. //len = datalen-3;
  473. //len = htole16(len);
  474. //memcpy(sendbuf+1, &len, 2);
  475. bm->new_block = new_block;
  476. #ifdef USE_ANT_S2
  477. bm->diff = lowestdiffbits;
  478. #endif
  479. sendbuf[4] = bitswap(sendbuf[4]);
  480. applog(LOG_DEBUG, "%s: TxTask Token: %d %d %02x%02x%02x%02x%02x%02x",
  481. ANTDRV.dname,
  482. datalen, bm->length,
  483. sendbuf[0], sendbuf[1], sendbuf[2],
  484. sendbuf[3], sendbuf[4], sendbuf[5]);
  485. *sentcount = cursentcount;
  486. crc = CRC16(sendbuf, datalen-2);
  487. crc = htole16(crc);
  488. memcpy(sendbuf+datalen-2, &crc, 2);
  489. #ifdef USE_ANT_S1
  490. applog(LOG_DEBUG, "%s: TxTask Token: new_block(%d) work_num(%d)"
  491. " crc(%04x)",
  492. ANTDRV.dname,
  493. (int)new_block, cursentcount, (int)crc);
  494. #else
  495. applog(LOG_DEBUG, "%s: TxTask Token: v(%d) new_block(%d)"
  496. " diff(%d work:%f) work_num(%d) crc(%04x)",
  497. ANTDRV.dname,
  498. (int)version, (int)new_block, lowestdiffbits, workdiff,
  499. cursentcount, (int)crc);
  500. #endif
  501. applog(LOG_DEBUG, "%s: TxTask Token: %d %d %02x%02x%02x%02x%02x%02x",
  502. ANTDRV.dname,
  503. datalen, bm->length,
  504. sendbuf[0], sendbuf[1], sendbuf[2],
  505. sendbuf[3], sendbuf[4], sendbuf[5]);
  506. return datalen;
  507. }
  508. static int bitmain_set_rxstatus(struct bitmain_rxstatus_token *bm,
  509. uint8_t chip_status_eft, uint8_t detect_get, uint8_t chip_address, uint8_t reg_address)
  510. {
  511. uint16_t crc = 0;
  512. int datalen = 0;
  513. uint8_t *sendbuf = (uint8_t *)bm;
  514. #ifdef USE_ANT_S2
  515. uint8_t version = 0;
  516. #endif
  517. if (unlikely(!bm)) {
  518. applog(LOG_WARNING, "%s: %s() bm is null", ANTDRV.dname, __func__);
  519. return -1;
  520. }
  521. datalen = sizeof(struct bitmain_rxstatus_token);
  522. memset(bm, 0, datalen);
  523. bm->token_type = BITMAIN_TOKEN_TYPE_RXSTATUS;
  524. #ifdef USE_ANT_S1
  525. bm->length = datalen-2;
  526. #else
  527. bm->version = version;
  528. bm->length = datalen-4;
  529. bm->length = htole16(bm->length);
  530. #endif
  531. bm->chip_status_eft = chip_status_eft;
  532. bm->detect_get = detect_get;
  533. #ifdef USE_ANT_S1
  534. sendbuf[2] = bitswap(sendbuf[2]);
  535. #else
  536. sendbuf[4] = bitswap(sendbuf[4]);
  537. #endif
  538. bm->chip_address = chip_address;
  539. bm->reg_address = reg_address;
  540. crc = CRC16((uint8_t *)bm, datalen-2);
  541. bm->crc = htole16(crc);
  542. #ifdef USE_ANT_S1
  543. applog(LOG_DEBUG, "%s: RxStatus Token: chip_status_eft(%d) detect_get(%d)"
  544. " chip_address(%02x) reg_address(%02x) crc(%04x)",
  545. ANTDRV.dname,
  546. (int)chip_status_eft, (int)detect_get, chip_address, reg_address, crc);
  547. #else
  548. applog(LOG_DEBUG, "%s: RxStatus Token: v(%d) chip_status_eft(%d) detect_get(%d)"
  549. " chip_address(%02x) reg_address(%02x) crc(%04x)",
  550. ANTDRV.dname,
  551. (int)version, (int)chip_status_eft, (int)detect_get,
  552. chip_address, reg_address, crc);
  553. #endif
  554. return datalen;
  555. }
  556. static int bitmain_parse_rxstatus(const uint8_t * data, int datalen, struct bitmain_rxstatus_data *bm)
  557. {
  558. uint16_t crc = 0;
  559. int i = 0;
  560. #ifdef USE_ANT_S2
  561. uint8_t version = 0;
  562. int j = 0;
  563. int asic_num = 0;
  564. int dataindex = 0;
  565. #endif
  566. if (unlikely(!bm)) {
  567. applog(LOG_ERR, "%s: %s() bm is null", ANTDRV.dname, __func__);
  568. return -1;
  569. }
  570. if (unlikely(!data || datalen <= 0)) {
  571. applog(LOG_ERR, "%s: %s() parameter invalid data is null"
  572. " or datalen(%d) error",
  573. ANTDRV.dname, __func__, datalen);
  574. return -1;
  575. }
  576. #ifdef USE_ANT_S1
  577. memcpy(bm, data, sizeof(struct bitmain_rxstatus_data));
  578. if (bm->data_type != BITMAIN_DATA_TYPE_RXSTATUS) {
  579. applog(LOG_ERR, "%s: %s() datatype(%02x) error",
  580. ANTDRV.dname, __func__,
  581. bm->data_type);
  582. return -1;
  583. }
  584. if (bm->length+2 != datalen) {
  585. applog(LOG_ERR, "%s: %s() length(%d) datalen(%d) error",
  586. ANTDRV.dname, __func__,
  587. bm->length, datalen);
  588. return -1;
  589. }
  590. crc = CRC16(data, datalen-2);
  591. memcpy(&(bm->crc), data+datalen-2, 2);
  592. bm->crc = htole16(bm->crc);
  593. if (crc != bm->crc) {
  594. applog(LOG_ERR, "%s: %s() check crc(%d)"
  595. " != bm crc(%d) datalen(%d)",
  596. ANTDRV.dname, __func__,
  597. crc, bm->crc, datalen);
  598. return -1;
  599. }
  600. bm->fifo_space = htole32(bm->fifo_space);
  601. bm->nonce_error = htole32(bm->nonce_error);
  602. if (bm->chain_num*5 + bm->temp_num + bm->fan_num + 22 != datalen) {
  603. applog(LOG_ERR, "%s: %s() chain_num(%d) temp_num(%d)"
  604. " fan_num(%d) not match datalen(%d)",
  605. ANTDRV.dname, __func__,
  606. bm->chain_num, bm->temp_num, bm->fan_num, datalen);
  607. return -1;
  608. }
  609. if (bm->chain_num > BITMAIN_MAX_CHAIN_NUM) {
  610. applog(LOG_ERR, "%s: %s() chain_num=%d error",
  611. ANTDRV.dname, __func__,
  612. bm->chain_num);
  613. return -1;
  614. }
  615. if (bm->chain_num > 0) {
  616. memcpy(bm->chain_asic_status, data+20, bm->chain_num*4);
  617. memcpy(bm->chain_asic_num, data+20+bm->chain_num*4, bm->chain_num);
  618. }
  619. for (i = 0; i < bm->chain_num; i++)
  620. bm->chain_asic_status[i] = htole32(bm->chain_asic_status[i]);
  621. if (bm->temp_num > 0)
  622. memcpy(bm->temp, data+20+bm->chain_num*5, bm->temp_num);
  623. if (bm->fan_num > 0)
  624. memcpy(bm->fan, data+20+bm->chain_num*5+bm->temp_num, bm->fan_num);
  625. applog(LOG_DEBUG, "%s: RxStatus Data chipvalueeft(%d) version(%d) fifospace(%d)"
  626. " regvalue(%d) chainnum(%d) tempnum(%d) fannum(%d) crc(%04x)",
  627. ANTDRV.dname,
  628. bm->chip_value_eft, bm->version, bm->fifo_space, bm->reg_value,
  629. bm->chain_num, bm->temp_num, bm->fan_num, bm->crc);
  630. applog(LOG_DEBUG, "%s: RxStatus Data chain info:", ANTDRV.dname);
  631. for (i = 0; i < bm->chain_num; i++) {
  632. applog(LOG_DEBUG, "%s: RxStatus Data chain(%d) asic num=%d asic_status=%08x",
  633. ANTDRV.dname,
  634. i+1, bm->chain_asic_num[i], bm->chain_asic_status[i]);
  635. }
  636. #else // USE_ANT_S2
  637. memset(bm, 0, sizeof(struct bitmain_rxstatus_data));
  638. memcpy(bm, data, 28);
  639. if (bm->data_type != BITMAIN_DATA_TYPE_RXSTATUS) {
  640. applog(LOG_ERR, "%s: %s() datatype(%02x) error",
  641. ANTDRV.dname, __func__,
  642. bm->data_type);
  643. return -1;
  644. }
  645. if (bm->version != version) {
  646. applog(LOG_ERR, "%s: %s() version(%02x) error",
  647. ANTDRV.dname, __func__,
  648. bm->version);
  649. return -1;
  650. }
  651. bm->length = htole16(bm->length);
  652. if (bm->length+4 != datalen) {
  653. applog(LOG_ERR, "%s: %s() length(%d) datalen(%d) error",
  654. ANTDRV.dname, __func__,
  655. bm->length, datalen);
  656. return -1;
  657. }
  658. crc = CRC16(data, datalen-2);
  659. memcpy(&(bm->crc), data+datalen-2, 2);
  660. bm->crc = htole16(bm->crc);
  661. if (crc != bm->crc) {
  662. applog(LOG_ERR, "%s: %s() check crc(%d)"
  663. " != bm crc(%d) datalen(%d)",
  664. ANTDRV.dname, __func__,
  665. crc, bm->crc, datalen);
  666. return -1;
  667. }
  668. bm->fifo_space = htole16(bm->fifo_space);
  669. bm->fan_exist = htole16(bm->fan_exist);
  670. bm->temp_exist = htole32(bm->temp_exist);
  671. bm->nonce_error = htole32(bm->nonce_error);
  672. if (bm->chain_num > BITMAIN_MAX_CHAIN_NUM) {
  673. applog(LOG_ERR, "%s: %s() chain_num=%d error",
  674. ANTDRV.dname, __func__,
  675. bm->chain_num);
  676. return -1;
  677. }
  678. dataindex = 28;
  679. if (bm->chain_num > 0) {
  680. memcpy(bm->chain_asic_num,
  681. data+datalen-2-bm->chain_num-bm->temp_num-bm->fan_num,
  682. bm->chain_num);
  683. }
  684. for (i = 0; i < bm->chain_num; i++) {
  685. asic_num = bm->chain_asic_num[i];
  686. if (asic_num < 0)
  687. asic_num = 0;
  688. else {
  689. if (asic_num % 32 == 0)
  690. asic_num = asic_num / 32;
  691. else
  692. asic_num = asic_num / 32 + 1;
  693. }
  694. memcpy((uint8_t *)bm->chain_asic_exist+i*32, data+dataindex, asic_num*4);
  695. dataindex += asic_num*4;
  696. }
  697. for(i = 0; i < bm->chain_num; i++) {
  698. asic_num = bm->chain_asic_num[i];
  699. if (asic_num < 0)
  700. asic_num = 0;
  701. else {
  702. if (asic_num % 32 == 0)
  703. asic_num = asic_num / 32;
  704. else
  705. asic_num = asic_num / 32 + 1;
  706. }
  707. memcpy((uint8_t *)bm->chain_asic_status+i*32, data+dataindex, asic_num*4);
  708. dataindex += asic_num*4;
  709. }
  710. dataindex += bm->chain_num;
  711. if ((dataindex + bm->temp_num + bm->fan_num + 2) != datalen) {
  712. applog(LOG_ERR, "%s: %s() dataindex(%d) chain_num(%d) temp_num(%d)"
  713. " fan_num(%d) not match datalen(%d)",
  714. ANTDRV.dname, __func__,
  715. dataindex, bm->chain_num, bm->temp_num, bm->fan_num, datalen);
  716. return -1;
  717. }
  718. for (i = 0; i < bm->chain_num; i++) {
  719. for (j = 0; j < 8; j++) {
  720. bm->chain_asic_exist[i*8+j] = htole32(bm->chain_asic_exist[i*8+j]);
  721. bm->chain_asic_status[i*8+j] = htole32(bm->chain_asic_status[i*8+j]);
  722. }
  723. }
  724. if (bm->temp_num > 0) {
  725. memcpy(bm->temp, data+dataindex, bm->temp_num);
  726. dataindex += bm->temp_num;
  727. }
  728. if (bm->fan_num > 0) {
  729. memcpy(bm->fan, data+dataindex, bm->fan_num);
  730. dataindex += bm->fan_num;
  731. }
  732. applog(LOG_DEBUG, "%s: RxStatus Data chipv_e(%d) chainnum(%d) fifos(%d)"
  733. " v1(%d) v2(%d) v3(%d) v4(%d) fann(%d) tempn(%d) fanet(%04x)"
  734. " tempet(%08x) ne(%d) regvalue(%d) crc(%04x)",
  735. ANTDRV.dname,
  736. bm->chip_value_eft, bm->chain_num, bm->fifo_space,
  737. bm->hw_version[0], bm->hw_version[1], bm->hw_version[2],
  738. bm->hw_version[3], bm->fan_num, bm->temp_num, bm->fan_exist,
  739. bm->temp_exist, bm->nonce_error, bm->reg_value, bm->crc);
  740. applog(LOG_DEBUG, "%s: RxStatus Data chain info:", ANTDRV.dname);
  741. for (i = 0; i < bm->chain_num; i++) {
  742. applog(LOG_DEBUG, "%s: RxStatus Data chain(%d) asic num=%d asic_exists=%08x"
  743. " asic_status=%08x",
  744. ANTDRV.dname,
  745. i+1, bm->chain_asic_num[i], bm->chain_asic_exist[i*8],
  746. bm->chain_asic_status[i*8]);
  747. }
  748. #endif
  749. applog(LOG_DEBUG, "%s: RxStatus Data temp info:", ANTDRV.dname);
  750. for (i = 0; i < bm->temp_num; i++) {
  751. applog(LOG_DEBUG, "%s: RxStatus Data temp(%d) temp=%d",
  752. ANTDRV.dname,
  753. i+1, bm->temp[i]);
  754. }
  755. applog(LOG_DEBUG, "%s: RxStatus Data fan info:", ANTDRV.dname);
  756. for (i = 0; i < bm->fan_num; i++) {
  757. applog(LOG_DEBUG, "%s: RxStatus Data fan(%d) fan=%d",
  758. ANTDRV.dname,
  759. i+1, bm->fan[i]);
  760. }
  761. return 0;
  762. }
  763. static int bitmain_parse_rxnonce(const uint8_t * data, int datalen, struct bitmain_rxnonce_data *bm, int * nonce_num)
  764. {
  765. int i = 0;
  766. uint16_t crc = 0;
  767. #ifdef USE_ANT_S2
  768. uint8_t version = 0;
  769. #endif
  770. int curnoncenum = 0;
  771. if (unlikely(!bm)) {
  772. applog(LOG_ERR, "%s: %s() bm is null", ANTDRV.dname, __func__);
  773. return -1;
  774. }
  775. if (unlikely(!data || datalen <= 0)) {
  776. applog(LOG_ERR, "%s: %s() parameter invalid data is null"
  777. " or datalen(%d) error",
  778. ANTDRV.dname, __func__, datalen);
  779. return -1;
  780. }
  781. memcpy(bm, data, sizeof(struct bitmain_rxnonce_data));
  782. if (bm->data_type != BITMAIN_DATA_TYPE_RXNONCE) {
  783. applog(LOG_ERR, "%s: %s() datatype(%02x) error",
  784. ANTDRV.dname, __func__,
  785. bm->data_type);
  786. return -1;
  787. }
  788. #ifdef USE_ANT_S1
  789. if (bm->length+2 != datalen) {
  790. applog(LOG_ERR, "%s: %s() length(%d) error",
  791. ANTDRV.dname, __func__,
  792. bm->length);
  793. return -1;
  794. }
  795. #else
  796. if (bm->version != version) {
  797. applog(LOG_ERR, "%s: %s() version(%02x) error",
  798. ANTDRV.dname, __func__,
  799. bm->version);
  800. return -1;
  801. }
  802. bm->length = htole16(bm->length);
  803. if (bm->length+4 != datalen) {
  804. applog(LOG_ERR, "%s: %s() length(%d) datalen(%d) error",
  805. ANTDRV.dname, __func__,
  806. bm->length, datalen);
  807. return -1;
  808. }
  809. #endif
  810. crc = CRC16(data, datalen-2);
  811. memcpy(&(bm->crc), data+datalen-2, 2);
  812. bm->crc = htole16(bm->crc);
  813. if (crc != bm->crc) {
  814. applog(LOG_ERR, "%s: %s() check crc(%d)"
  815. " != bm crc(%d) datalen(%d)",
  816. ANTDRV.dname, __func__,
  817. crc, bm->crc, datalen);
  818. return -1;
  819. }
  820. #ifdef USE_ANT_S1
  821. curnoncenum = (datalen-4)/8;
  822. #else
  823. bm->fifo_space = htole16(bm->fifo_space);
  824. bm->diff = htole16(bm->diff);
  825. bm->total_nonce_num = htole64(bm->total_nonce_num);
  826. curnoncenum = (datalen-14)/8;
  827. #endif
  828. applog(LOG_DEBUG, "%s: RxNonce Data: nonce_num(%d) fifo_space(%d)",
  829. ANTDRV.dname, curnoncenum, bm->fifo_space);
  830. for (i = 0; i < curnoncenum; i++) {
  831. bm->nonces[i].work_id = htole32(bm->nonces[i].work_id);
  832. bm->nonces[i].nonce = htole32(bm->nonces[i].nonce);
  833. applog(LOG_DEBUG, "%s: RxNonce Data %d: work_id(%"PRIu32") nonce(%08x)(%d)",
  834. ANTDRV.dname,
  835. i, bm->nonces[i].work_id,
  836. bm->nonces[i].nonce, bm->nonces[i].nonce);
  837. }
  838. *nonce_num = curnoncenum;
  839. return 0;
  840. }
  841. static int bitmain_read(struct cgpu_info *bitmain, unsigned char *buf,
  842. size_t bufsize, int timeout, int ep)
  843. {
  844. struct bitmain_info *info = bitmain->device_data;
  845. int readlen = 0, err = 0;
  846. if (bitmain == NULL || buf == NULL || bufsize <= 0) {
  847. applog(LOG_WARNING, "%s%d: %s() parameter error bufsize(%d)",
  848. bitmain->drv->name, bitmain->device_id,
  849. __func__, (int)bufsize);
  850. return -1;
  851. }
  852. if (is_usb) {
  853. err = usb_read_once_timeout(bitmain, (char *)buf, bufsize, &readlen, timeout, ep);
  854. applog(LOG_DEBUG, "%s%i: Get %s() got readlen %d err %d",
  855. bitmain->drv->name, bitmain->device_id,
  856. __func__, readlen, err);
  857. } else
  858. readlen = read(info->device_fd, buf, bufsize);
  859. return readlen;
  860. }
  861. static int bitmain_write(struct cgpu_info *bitmain, char *buf, ssize_t len, int ep)
  862. {
  863. struct bitmain_info *info = bitmain->device_data;
  864. int err, amount, sent;
  865. if (is_usb) {
  866. err = usb_write(bitmain, buf, len, &amount, ep);
  867. applog(LOG_DEBUG, "%s%d: usb_write got err %d",
  868. bitmain->drv->name, bitmain->device_id, err);
  869. if (unlikely(err != 0)) {
  870. applog(LOG_ERR, "%s%d: usb_write error on %s() err=%d",
  871. bitmain->drv->name, bitmain->device_id, __func__, err);
  872. return BTM_SEND_ERROR;
  873. }
  874. if (amount != len) {
  875. applog(LOG_ERR, "%s%d: usb_write length mismatch on %s() "
  876. "amount=%d len=%d",
  877. bitmain->drv->name, bitmain->device_id, __func__,
  878. amount, (int)len);
  879. return BTM_SEND_ERROR;
  880. }
  881. } else {
  882. sent = 0;
  883. while (sent < len) {
  884. amount = write(info->device_fd, buf+sent, len-sent);
  885. if (amount < 0) {
  886. applog(LOG_WARNING, "%s%d: ser_write got err %d",
  887. bitmain->drv->name, bitmain->device_id, amount);
  888. return BTM_SEND_ERROR;
  889. }
  890. sent += amount;
  891. }
  892. }
  893. return BTM_SEND_OK;
  894. }
  895. static int bitmain_send_data(const uint8_t *data, int datalen, __maybe_unused struct cgpu_info *bitmain)
  896. {
  897. int ret, ep = C_BITMAIN_SEND;
  898. //int delay;
  899. //struct bitmain_info *info = NULL;
  900. //cgtimer_t ts_start;
  901. if (datalen <= 0) {
  902. return 0;
  903. }
  904. if (data[0] == BITMAIN_TOKEN_TYPE_TXCONFIG) {
  905. ep = C_BITMAIN_TOKEN_TXCONFIG;
  906. } else if (data[0] == BITMAIN_TOKEN_TYPE_TXTASK) {
  907. ep = C_BITMAIN_TOKEN_TXTASK;
  908. } else if (data[0] == BITMAIN_TOKEN_TYPE_RXSTATUS) {
  909. ep = C_BITMAIN_TOKEN_RXSTATUS;
  910. }
  911. //info = bitmain->device_data;
  912. //delay = datalen * 10 * 1000000;
  913. //delay = delay / info->baud;
  914. //delay += 4000;
  915. if (opt_debug) {
  916. applog(LOG_DEBUG, "%s: Sent(%d):", ANTDRV.dname, datalen);
  917. hexdump(data, datalen);
  918. }
  919. //cgsleep_prepare_r(&ts_start);
  920. applog(LOG_DEBUG, "%s: %s() start", ANTDRV.dname, __func__);
  921. ret = bitmain_write(bitmain, (char *)data, datalen, ep);
  922. applog(LOG_DEBUG, "%s: %s() stop ret=%d datalen=%d",
  923. ANTDRV.dname, __func__, ret, datalen);
  924. //cgsleep_us_r(&ts_start, delay);
  925. //applog(LOG_DEBUG, "BitMain: Sent: Buffer delay: %dus", delay);
  926. return ret;
  927. }
  928. static void bitmain_inc_nvw(struct bitmain_info *info, struct thr_info *thr)
  929. {
  930. applog(LOG_INFO, "%s%d: No matching work - HW error",
  931. thr->cgpu->drv->name, thr->cgpu->device_id);
  932. inc_hw_errors(thr);
  933. info->no_matching_work++;
  934. }
  935. static inline void record_temp_fan(struct bitmain_info *info, struct bitmain_rxstatus_data *bm, double *temp_avg)
  936. {
  937. int i = 0;
  938. *temp_avg = 0.0;
  939. info->fan_num = bm->fan_num;
  940. for (i = 0; i < bm->fan_num; i++)
  941. info->fan[i] = bm->fan[i] * BITMAIN_FAN_FACTOR;
  942. info->temp_num = bm->temp_num;
  943. info->temp_hi = 0;
  944. for (i = 0; i < bm->temp_num; i++) {
  945. info->temp[i] = bm->temp[i];
  946. /* if (bm->temp[i] & 0x80) {
  947. bm->temp[i] &= 0x7f;
  948. info->temp[i] = 0 - ((~bm->temp[i] & 0x7f) + 1);
  949. }*/
  950. *temp_avg += info->temp[i];
  951. if (info->temp[i] > info->temp_max)
  952. info->temp_max = info->temp[i];
  953. if (info->temp[i] > info->temp_hi)
  954. info->temp_hi = info->temp[i];
  955. }
  956. if (bm->temp_num > 0) {
  957. *temp_avg = *temp_avg / bm->temp_num;
  958. info->temp_avg = *temp_avg;
  959. }
  960. }
  961. static void bitmain_update_temps(struct cgpu_info *bitmain, struct bitmain_info *info,
  962. struct bitmain_rxstatus_data *bm)
  963. {
  964. char tmp[64] = {0};
  965. char msg[10240] = {0};
  966. int i = 0;
  967. record_temp_fan(info, bm, &(bitmain->temp));
  968. sprintf(msg, "%s%d: ", bitmain->drv->name, bitmain->device_id);
  969. for (i = 0; i < bm->fan_num; i++) {
  970. if (i != 0) {
  971. strcat(msg, ", ");
  972. }
  973. sprintf(tmp, "Fan%d: %d/m", i+1, info->fan[i]);
  974. strcat(msg, tmp);
  975. }
  976. strcat(msg, " ");
  977. for (i = 0; i < bm->temp_num; i++) {
  978. if (i != 0) {
  979. strcat(msg, ", ");
  980. }
  981. sprintf(tmp, "Temp%d: %dC", i+1, info->temp[i]);
  982. strcat(msg, tmp);
  983. }
  984. sprintf(tmp, ", TempMAX: %dC", info->temp_max);
  985. strcat(msg, tmp);
  986. applog(LOG_INFO, "%s", msg);
  987. info->temp_history_index++;
  988. info->temp_sum += bitmain->temp;
  989. applog(LOG_DEBUG, "%s%d: temp_index: %d, temp_count: %d, temp_max: %d",
  990. bitmain->drv->name, bitmain->device_id,
  991. info->temp_history_index, info->temp_history_count, info->temp_max);
  992. if (info->temp_history_index == info->temp_history_count) {
  993. info->temp_history_index = 0;
  994. info->temp_sum = 0;
  995. }
  996. #ifdef USE_ANT_S1
  997. if (unlikely(info->temp_hi >= opt_bitmain_overheat)) {
  998. if (!info->overheat) {
  999. applog(LOG_WARNING, "%s%d: overheat! hi %dC limit %dC idling",
  1000. bitmain->drv->name, bitmain->device_id,
  1001. info->temp_hi, opt_bitmain_overheat);
  1002. info->overheat = true;
  1003. info->overheat_temp = info->temp_hi;
  1004. info->overheat_count++;
  1005. info->overheat_slept = 0;
  1006. }
  1007. } else if (info->overheat && info->temp_hi <= opt_bitmain_temp) {
  1008. applog(LOG_WARNING, "%s%d: cooled, restarting",
  1009. bitmain->drv->name, bitmain->device_id);
  1010. info->overheat = false;
  1011. info->overheat_recovers++;
  1012. }
  1013. #endif
  1014. }
  1015. static void bitmain_parse_results(struct cgpu_info *bitmain, struct bitmain_info *info,
  1016. struct thr_info *thr, uint8_t *buf, int *offset)
  1017. {
  1018. #ifdef USE_ANT_S1
  1019. int i, j, n, m, errordiff, spare = BITMAIN_READ_SIZE;
  1020. uint32_t checkbit = 0x00000000;
  1021. bool found = false;
  1022. struct work *work = NULL;
  1023. //char *ob_hex = NULL;
  1024. uint64_t searches;
  1025. K_ITEM *witem;
  1026. for (i = 0; i <= spare; i++) {
  1027. if (buf[i] == 0xa1) {
  1028. struct bitmain_rxstatus_data rxstatusdata;
  1029. applog(LOG_DEBUG, "%s%d: %s() RxStatus Data",
  1030. bitmain->drv->name, bitmain->device_id,
  1031. __func__);
  1032. if (*offset < 2) {
  1033. return;
  1034. }
  1035. if (buf[i+1] > 124) {
  1036. applog(LOG_ERR, "%s%d: %s() RxStatus Data datalen=%d error",
  1037. bitmain->drv->name, bitmain->device_id,
  1038. __func__, buf[i+1]+2);
  1039. continue;
  1040. }
  1041. if (*offset < buf[i+1] + 2) {
  1042. return;
  1043. }
  1044. if (bitmain_parse_rxstatus(buf+i, buf[i+1]+2, &rxstatusdata) != 0) {
  1045. applog(LOG_ERR, "%s%d: %s() RxStatus Data error len=%d",
  1046. bitmain->drv->name, bitmain->device_id,
  1047. __func__, buf[i+1]+2);
  1048. } else {
  1049. mutex_lock(&info->qlock);
  1050. info->chain_num = rxstatusdata.chain_num;
  1051. info->fifo_space = rxstatusdata.fifo_space;
  1052. info->nonce_error = rxstatusdata.nonce_error;
  1053. errordiff = info->nonce_error-info->last_nonce_error;
  1054. applog(LOG_DEBUG, "%s%d: %s() RxStatus Data"
  1055. " version=%d chainnum=%d fifospace=%d"
  1056. " nonceerror=%d-%d freq=%d chain info:",
  1057. bitmain->drv->name, bitmain->device_id, __func__,
  1058. rxstatusdata.version, info->chain_num,
  1059. info->fifo_space, info->last_nonce_error,
  1060. info->nonce_error, info->frequency);
  1061. for (n = 0; n < rxstatusdata.chain_num; n++) {
  1062. info->chain_asic_num[n] = rxstatusdata.chain_asic_num[n];
  1063. info->chain_asic_status[n] = rxstatusdata.chain_asic_status[n];
  1064. memset(info->chain_asic_status_t[n], 0, 40);
  1065. j = 0;
  1066. for (m = 0; m < 32; m++) {
  1067. if (m%8 == 0 && m != 0) {
  1068. info->chain_asic_status_t[n][j] = ' ';
  1069. j++;
  1070. }
  1071. checkbit = num2bit(m);
  1072. if (rxstatusdata.chain_asic_status[n] & checkbit)
  1073. info->chain_asic_status_t[n][j] = 'o';
  1074. else
  1075. info->chain_asic_status_t[n][j] = 'x';
  1076. j++;
  1077. }
  1078. applog(LOG_DEBUG, "%s%d: %s() RxStatus Data chain(%d)"
  1079. " asic_num=%d asic_status=%08x-%s",
  1080. bitmain->drv->name, bitmain->device_id,
  1081. __func__,
  1082. n, info->chain_asic_num[n],
  1083. info->chain_asic_status[n],
  1084. info->chain_asic_status_t[n]);
  1085. }
  1086. mutex_unlock(&info->qlock);
  1087. if (errordiff > 0) {
  1088. for (j = 0; j < errordiff; j++) {
  1089. bitmain_inc_nvw(info, thr);
  1090. }
  1091. mutex_lock(&info->qlock);
  1092. info->last_nonce_error += errordiff;
  1093. mutex_unlock(&info->qlock);
  1094. }
  1095. bitmain_update_temps(bitmain, info, &rxstatusdata);
  1096. }
  1097. found = true;
  1098. spare = buf[i+1] + 2 + i;
  1099. if (spare > *offset) {
  1100. applog(LOG_ERR, "%s%d: %s() spare(%d) > offset(%d)",
  1101. bitmain->drv->name, bitmain->device_id,
  1102. __func__, spare, *offset);
  1103. spare = *offset;
  1104. }
  1105. break;
  1106. } else if (buf[i] == 0xa2) {
  1107. struct bitmain_rxnonce_data rxnoncedata;
  1108. int nonce_num = 0;
  1109. applog(LOG_DEBUG, "%s%d: %s() RxNonce Data",
  1110. bitmain->drv->name, bitmain->device_id,
  1111. __func__);
  1112. if (*offset < 2) {
  1113. return;
  1114. }
  1115. if (buf[i+1] > 70) {
  1116. applog(LOG_ERR, "%s%d: %s() RxNonce Data datalen=%d error",
  1117. bitmain->drv->name, bitmain->device_id,
  1118. __func__, buf[i+1]+2);
  1119. continue;
  1120. }
  1121. if (*offset < buf[i+1] + 2) {
  1122. return;
  1123. }
  1124. if (bitmain_parse_rxnonce(buf+i, buf[i+1]+2, &rxnoncedata, &nonce_num) != 0) {
  1125. applog(LOG_ERR, "%s%d: %s() RxNonce Data error len=%d",
  1126. bitmain->drv->name, bitmain->device_id,
  1127. __func__, buf[i+1]+2);
  1128. } else {
  1129. for (j = 0; j < nonce_num; j++) {
  1130. searches = 0;
  1131. mutex_lock(&info->qlock);
  1132. witem = info->work_list->head;
  1133. while (witem) {
  1134. searches++;
  1135. if (DATAW(witem)->work->id == rxnoncedata.nonces[j].work_id)
  1136. break;
  1137. witem = witem->next;
  1138. }
  1139. mutex_unlock(&info->qlock);
  1140. if (witem) {
  1141. if (info->work_search == 0) {
  1142. info->min_search = searches;
  1143. info->max_search = searches;
  1144. } else {
  1145. if (info->min_search > searches)
  1146. info->min_search = searches;
  1147. if (info->max_search < searches)
  1148. info->max_search = searches;
  1149. }
  1150. info->work_search++;
  1151. info->tot_search += searches;
  1152. work = DATAW(witem)->work;
  1153. applog(LOG_DEBUG, "%s%d: %s() RxNonce Data find "
  1154. "work(%"PRIu32"-%"PRIu32")(%08x)",
  1155. bitmain->drv->name, bitmain->device_id,
  1156. __func__, work->id,
  1157. rxnoncedata.nonces[j].work_id,
  1158. rxnoncedata.nonces[j].nonce);
  1159. applog(LOG_DEBUG, "%s%d: %s() nonce = %08x",
  1160. bitmain->drv->name, bitmain->device_id,
  1161. __func__, rxnoncedata.nonces[j].nonce);
  1162. if (isdupnonce(bitmain, work, rxnoncedata.nonces[j].nonce)) {
  1163. // ignore it
  1164. } else {
  1165. if (submit_nonce(thr, work, rxnoncedata.nonces[j].nonce)) {
  1166. applog(LOG_DEBUG, "%s%d: %s() RxNonce Data ok",
  1167. bitmain->drv->name,
  1168. bitmain->device_id,
  1169. __func__);
  1170. mutex_lock(&info->qlock);
  1171. info->nonces++;
  1172. mutex_unlock(&info->qlock);
  1173. } else {
  1174. applog(LOG_ERR, "%s%d: %s() RxNonce Data "
  1175. "error work(%"PRIu32")",
  1176. bitmain->drv->name,
  1177. bitmain->device_id,
  1178. __func__,
  1179. rxnoncedata.nonces[j].work_id);
  1180. }
  1181. }
  1182. } else {
  1183. if (info->failed_search == 0) {
  1184. info->min_failed = searches;
  1185. info->max_failed = searches;
  1186. } else {
  1187. if (info->min_failed > searches)
  1188. info->min_failed = searches;
  1189. if (info->max_failed < searches)
  1190. info->max_failed = searches;
  1191. }
  1192. info->failed_search++;
  1193. info->tot_failed += searches;
  1194. mutex_lock(&info->qlock);
  1195. uint32_t min = 0, max = 0;
  1196. int count = 0;
  1197. if (info->work_list->tail) {
  1198. min = DATAW(info->work_list->tail)->wid;
  1199. max = DATAW(info->work_list->head)->wid;
  1200. count = info->work_list->count;
  1201. }
  1202. mutex_unlock(&info->qlock);
  1203. applog(LOG_ERR, "%s%d: %s() Work not found for id (%"PRIu32")"
  1204. " (min=%"PRIu32" max=%"PRIu32" count=%d)",
  1205. bitmain->drv->name, bitmain->device_id,
  1206. __func__, rxnoncedata.nonces[j].work_id.
  1207. min, max);
  1208. }
  1209. }
  1210. mutex_lock(&info->qlock);
  1211. info->fifo_space = rxnoncedata.fifo_space;
  1212. mutex_unlock(&info->qlock);
  1213. applog(LOG_DEBUG, "%s%d: %s() RxNonce Data fifo space=%d",
  1214. bitmain->drv->name, bitmain->device_id,
  1215. __func__, rxnoncedata.fifo_space);
  1216. }
  1217. found = true;
  1218. spare = buf[i+1] + 2 + i;
  1219. if (spare > *offset) {
  1220. applog(LOG_ERR, "%s%d: %s() RxNonce Data space(%d) > offset(%d)",
  1221. bitmain->drv->name, bitmain->device_id, __func__,
  1222. spare, *offset);
  1223. spare = *offset;
  1224. }
  1225. break;
  1226. } else {
  1227. applog(LOG_ERR, "%s%d: %s() data type error=%02x",
  1228. bitmain->drv->name, bitmain->device_id,
  1229. __func__, buf[i]);
  1230. }
  1231. }
  1232. if (!found) {
  1233. spare = *offset - BITMAIN_READ_SIZE;
  1234. /* We are buffering and haven't accumulated one more corrupt
  1235. * work result. */
  1236. if (spare < (int)BITMAIN_READ_SIZE)
  1237. return;
  1238. bitmain_inc_nvw(info, thr);
  1239. }
  1240. *offset -= spare;
  1241. memmove(buf, buf + spare, *offset);
  1242. #else // S2
  1243. int i, j, n, m, r, errordiff, spare = BITMAIN_READ_SIZE;
  1244. uint32_t checkbit = 0x00000000;
  1245. bool found = false;
  1246. struct work *work = NULL;
  1247. struct bitmain_packet_head packethead;
  1248. int asicnum = 0;
  1249. uint64_t searches;
  1250. K_ITEM *witem;
  1251. for (i = 0; i <= spare; i++) {
  1252. if (buf[i] == 0xa1) {
  1253. struct bitmain_rxstatus_data rxstatusdata;
  1254. applog(LOG_DEBUG, "%s%d: %s() RxStatus Data",
  1255. bitmain->drv->name, bitmain->device_id,
  1256. __func__);
  1257. if (*offset < 4) {
  1258. return;
  1259. }
  1260. memcpy(&packethead, buf+i, sizeof(struct bitmain_packet_head));
  1261. packethead.length = htole16(packethead.length);
  1262. if (packethead.length > 1130) {
  1263. applog(LOG_ERR, "%s%d: %s() RxStatus Data datalen=%d error",
  1264. bitmain->drv->name, bitmain->device_id,
  1265. __func__, packethead.length+4);
  1266. continue;
  1267. }
  1268. if (*offset < packethead.length + 4)
  1269. return;
  1270. if (bitmain_parse_rxstatus(buf+i, packethead.length+4, &rxstatusdata) != 0) {
  1271. applog(LOG_ERR, "%s%d: %s() RxStatus Data error len=%d",
  1272. bitmain->drv->name, bitmain->device_id,
  1273. __func__, packethead.length+4);
  1274. } else {
  1275. mutex_lock(&info->qlock);
  1276. info->chain_num = rxstatusdata.chain_num;
  1277. info->fifo_space = rxstatusdata.fifo_space;
  1278. info->hw_version[0] = rxstatusdata.hw_version[0];
  1279. info->hw_version[1] = rxstatusdata.hw_version[1];
  1280. info->hw_version[2] = rxstatusdata.hw_version[2];
  1281. info->hw_version[3] = rxstatusdata.hw_version[3];
  1282. info->nonce_error = rxstatusdata.nonce_error;
  1283. errordiff = info->nonce_error-info->last_nonce_error;
  1284. applog(LOG_DEBUG, "%s%d: %s() RxStatus Data"
  1285. " version=%d chainnum=%d fifospace=%d"
  1286. " hwv1=%d hwv2=%d hwv3=%d hwv4=%d"
  1287. " nonceerror=%d-%d freq=%d chain info:",
  1288. bitmain->drv->name, bitmain->device_id, __func__,
  1289. rxstatusdata.version, info->chain_num, info->fifo_space,
  1290. info->hw_version[0], info->hw_version[1],
  1291. info->hw_version[2], info->hw_version[3],
  1292. info->last_nonce_error,
  1293. info->nonce_error, info->frequency);
  1294. memcpy(info->chain_asic_exist, rxstatusdata.chain_asic_exist, BITMAIN_MAX_CHAIN_NUM*32);
  1295. memcpy(info->chain_asic_status, rxstatusdata.chain_asic_status, BITMAIN_MAX_CHAIN_NUM*32);
  1296. for (n = 0; n < rxstatusdata.chain_num; n++) {
  1297. info->chain_asic_num[n] = rxstatusdata.chain_asic_num[n];
  1298. memset(info->chain_asic_status_t[n], 0, 320);
  1299. j = 0;
  1300. if (info->chain_asic_num[n] <= 0)
  1301. asicnum = 0;
  1302. else {
  1303. if (info->chain_asic_num[n] % 32 == 0)
  1304. asicnum = info->chain_asic_num[n] / 32;
  1305. else
  1306. asicnum = info->chain_asic_num[n] / 32 + 1;
  1307. }
  1308. if (asicnum > 0) {
  1309. for (m = asicnum-1; m >= 0; m--) {
  1310. for (r = 0; r < 32; r++) {
  1311. if ((r % 8) == 0 && r != 0) {
  1312. info->chain_asic_status_t[n][j] = ' ';
  1313. j++;
  1314. }
  1315. checkbit = num2bit(r);
  1316. if (rxstatusdata.chain_asic_exist[n*8+m] & checkbit) {
  1317. if (rxstatusdata.chain_asic_status[n*8+m] & checkbit)
  1318. info->chain_asic_status_t[n][j] = 'o';
  1319. else
  1320. info->chain_asic_status_t[n][j] = 'x';
  1321. } else
  1322. info->chain_asic_status_t[n][j] = '-';
  1323. j++;
  1324. }
  1325. info->chain_asic_status_t[n][j] = ' ';
  1326. j++;
  1327. }
  1328. }
  1329. applog(LOG_DEBUG, "%s%d: %s() RxStatis Data chain(%d) asic_num=%d "
  1330. "asic_exist=%08x%08x%08x%08x%08x%08x%08x%08x "
  1331. "asic_status=%08x%08x%08x%08x%08x%08x%08x%08x",
  1332. bitmain->drv->name, bitmain->device_id,
  1333. __func__, n, info->chain_asic_num[n],
  1334. info->chain_asic_exist[n*8+0],
  1335. info->chain_asic_exist[n*8+1],
  1336. info->chain_asic_exist[n*8+2],
  1337. info->chain_asic_exist[n*8+3],
  1338. info->chain_asic_exist[n*8+4],
  1339. info->chain_asic_exist[n*8+5],
  1340. info->chain_asic_exist[n*8+6],
  1341. info->chain_asic_exist[n*8+7],
  1342. info->chain_asic_status[n*8+0],
  1343. info->chain_asic_status[n*8+1],
  1344. info->chain_asic_status[n*8+2],
  1345. info->chain_asic_status[n*8+3],
  1346. info->chain_asic_status[n*8+4],
  1347. info->chain_asic_status[n*8+5],
  1348. info->chain_asic_status[n*8+6],
  1349. info->chain_asic_status[n*8+7]);
  1350. applog(LOG_ERR, "%s%d: %s() RxStatis Data chain(%d) asic_num=%d"
  1351. " asic_status=%s",
  1352. bitmain->drv->name, bitmain->device_id,
  1353. __func__, n, info->chain_asic_num[n],
  1354. info->chain_asic_status_t[n]);
  1355. }
  1356. mutex_unlock(&info->qlock);
  1357. if (errordiff > 0) {
  1358. for (j = 0; j < errordiff; j++)
  1359. bitmain_inc_nvw(info, thr);
  1360. mutex_lock(&info->qlock);
  1361. info->last_nonce_error += errordiff;
  1362. mutex_unlock(&info->qlock);
  1363. }
  1364. bitmain_update_temps(bitmain, info, &rxstatusdata);
  1365. }
  1366. found = true;
  1367. spare = packethead.length + 4 + i;
  1368. if (spare > *offset) {
  1369. applog(LOG_ERR, "%s%d: %s() spare(%d) > offset(%d)",
  1370. bitmain->drv->name, bitmain->device_id,
  1371. __func__, spare, *offset);
  1372. spare = *offset;
  1373. }
  1374. break;
  1375. } else if (buf[i] == 0xa2) {
  1376. struct bitmain_rxnonce_data rxnoncedata;
  1377. int nonce_num = 0;
  1378. applog(LOG_DEBUG, "%s%d: %s() RxNonce Data",
  1379. bitmain->drv->name, bitmain->device_id,
  1380. __func__);
  1381. if (*offset < 4)
  1382. return;
  1383. memcpy(&packethead, buf+i, sizeof(struct bitmain_packet_head));
  1384. packethead.length = htole16(packethead.length);
  1385. if (packethead.length > 1030) {
  1386. applog(LOG_ERR, "%s%d: %s() RxNonce Data datalen=%d error",
  1387. bitmain->drv->name, bitmain->device_id,
  1388. __func__, packethead.length+4);
  1389. continue;
  1390. }
  1391. if (*offset < packethead.length + 4)
  1392. return;
  1393. if (bitmain_parse_rxnonce(buf+i, packethead.length+4, &rxnoncedata, &nonce_num) != 0) {
  1394. applog(LOG_ERR, "%s%d: %s() RxNonce Data error len=%d",
  1395. bitmain->drv->name, bitmain->device_id,
  1396. __func__, packethead.length+4);
  1397. } else {
  1398. for (j = 0; j < nonce_num; j++) {
  1399. searches = 0;
  1400. mutex_lock(&info->qlock);
  1401. witem = info->work_list->head;
  1402. while (witem && DATAW(witem)->work) {
  1403. searches++;
  1404. if (DATAW(witem)->wid == rxnoncedata.nonces[j].work_id)
  1405. break;
  1406. witem = witem->next;
  1407. }
  1408. if (witem && DATAW(witem)->work) {
  1409. work = DATAW(witem)->work;
  1410. mutex_unlock(&info->qlock);
  1411. if (info->work_search == 0) {
  1412. info->min_search = searches;
  1413. info->max_search = searches;
  1414. } else {
  1415. if (info->min_search > searches)
  1416. info->min_search = searches;
  1417. if (info->max_search < searches)
  1418. info->max_search = searches;
  1419. }
  1420. info->work_search++;
  1421. info->tot_search += searches;
  1422. applog(LOG_DEBUG, "%s%d: %s() RxNonce Data find "
  1423. "work(%"PRIu32"-%"PRIu32")(%08x)",
  1424. bitmain->drv->name, bitmain->device_id,
  1425. __func__, work->id,
  1426. rxnoncedata.nonces[j].work_id,
  1427. rxnoncedata.nonces[j].nonce);
  1428. applog(LOG_DEBUG, "%s%d: %s() nonce = %08x",
  1429. bitmain->drv->name, bitmain->device_id,
  1430. __func__, rxnoncedata.nonces[j].nonce);
  1431. if (isdupnonce(bitmain, work, rxnoncedata.nonces[j].nonce)) {
  1432. // ignore it
  1433. } else {
  1434. if (submit_nonce(thr, work, rxnoncedata.nonces[j].nonce)) {
  1435. applog(LOG_DEBUG, "%s%d: %s() RxNonce Data ok",
  1436. bitmain->drv->name,
  1437. bitmain->device_id,
  1438. __func__);
  1439. mutex_lock(&info->qlock);
  1440. info->nonces += work->device_diff;
  1441. mutex_unlock(&info->qlock);
  1442. } else {
  1443. applog(LOG_ERR, "%s%d: %s() RxNonce Data "
  1444. "error work(%"PRIu32")",
  1445. bitmain->drv->name,
  1446. bitmain->device_id,
  1447. __func__,
  1448. rxnoncedata.nonces[j].work_id);
  1449. }
  1450. }
  1451. } else {
  1452. mutex_unlock(&info->qlock);
  1453. if (info->failed_search == 0) {
  1454. info->min_failed = searches;
  1455. info->max_failed = searches;
  1456. } else {
  1457. if (info->min_failed > searches)
  1458. info->min_failed = searches;
  1459. if (info->max_failed < searches)
  1460. info->max_failed = searches;
  1461. }
  1462. info->failed_search++;
  1463. info->tot_failed += searches;
  1464. applog(LOG_ERR, "%s%d: %s() Work not found for id (%"PRIu32")",
  1465. bitmain->drv->name, bitmain->device_id,
  1466. __func__, rxnoncedata.nonces[j].work_id);
  1467. }
  1468. }
  1469. mutex_lock(&info->qlock);
  1470. info->fifo_space = rxnoncedata.fifo_space;
  1471. mutex_unlock(&info->qlock);
  1472. applog(LOG_DEBUG, "%s%d: %s() RxNonce Data fifo space=%d",
  1473. bitmain->drv->name, bitmain->device_id,
  1474. __func__, rxnoncedata.fifo_space);
  1475. if (nonce_num < BITMAIN_MAX_NONCE_NUM)
  1476. cgsleep_ms(5);
  1477. }
  1478. found = true;
  1479. spare = packethead.length + 4 + i;
  1480. if (spare > *offset) {
  1481. applog(LOG_ERR, "%s%d: %s() RxNonce Data space(%d) > offset(%d)",
  1482. bitmain->drv->name, bitmain->device_id, __func__,
  1483. spare, *offset);
  1484. spare = *offset;
  1485. }
  1486. break;
  1487. } else {
  1488. applog(LOG_ERR, "%s%d: %s() data type error=%02x",
  1489. bitmain->drv->name, bitmain->device_id,
  1490. __func__, buf[i]);
  1491. }
  1492. }
  1493. if (!found) {
  1494. spare = *offset - BITMAIN_READ_SIZE;
  1495. /* We are buffering and haven't accumulated one more corrupt
  1496. * work result. */
  1497. if (spare < (int)BITMAIN_READ_SIZE)
  1498. return;
  1499. bitmain_inc_nvw(info, thr);
  1500. }
  1501. *offset -= spare;
  1502. memmove(buf, buf + spare, *offset);
  1503. #endif
  1504. }
  1505. static void bitmain_running_reset(struct bitmain_info *info)
  1506. {
  1507. info->results = 0;
  1508. info->reset = false;
  1509. }
  1510. static void *bitmain_get_results(void *userdata)
  1511. {
  1512. struct cgpu_info *bitmain = (struct cgpu_info *)userdata;
  1513. struct bitmain_info *info = bitmain->device_data;
  1514. int offset = 0, ret = 0;
  1515. const int rsize = BITMAIN_FTDI_READSIZE;
  1516. char readbuf[BITMAIN_READBUF_SIZE];
  1517. struct thr_info *thr = info->thr;
  1518. char threadname[24];
  1519. int errorcount = 0;
  1520. snprintf(threadname, 24, "btm_recv/%d", bitmain->device_id);
  1521. RenameThread(threadname);
  1522. while (likely(!bitmain->shutdown)) {
  1523. unsigned char buf[rsize];
  1524. applog(LOG_DEBUG, "%s%d: %s() offset=%d",
  1525. bitmain->drv->name, bitmain->device_id, __func__, offset);
  1526. if (offset >= (int)BITMAIN_READ_SIZE) {
  1527. applog(LOG_DEBUG, "%s%d: %s() start",
  1528. bitmain->drv->name, bitmain->device_id, __func__);
  1529. bitmain_parse_results(bitmain, info, thr, (uint8_t *)readbuf, &offset);
  1530. applog(LOG_DEBUG, "%s%d: %s() stop",
  1531. bitmain->drv->name, bitmain->device_id, __func__);
  1532. }
  1533. if (unlikely(offset + rsize >= BITMAIN_READBUF_SIZE)) {
  1534. /* This should never happen */
  1535. applog(LOG_DEBUG, "%s%d: readbuf overflow, resetting buffer",
  1536. bitmain->drv->name, bitmain->device_id);
  1537. offset = 0;
  1538. }
  1539. if (unlikely(info->reset)) {
  1540. bitmain_running_reset(info);
  1541. /* Discard anything in the buffer */
  1542. offset = 0;
  1543. }
  1544. #ifdef USE_ANT_S1
  1545. // 2ms shouldn't be too much
  1546. cgsleep_ms(2);
  1547. #endif
  1548. applog(LOG_DEBUG, "%s%d: %s() read",
  1549. bitmain->drv->name, bitmain->device_id, __func__);
  1550. ret = bitmain_read(bitmain, buf, rsize, BITMAIN_READ_TIMEOUT, C_BITMAIN_READ);
  1551. applog(LOG_DEBUG, "%s%d: %s() read=%d",
  1552. bitmain->drv->name, bitmain->device_id, __func__, ret);
  1553. if (ret < 1) {
  1554. errorcount++;
  1555. #ifdef USE_ANT_S1
  1556. if (errorcount > 100) {
  1557. #else
  1558. if (errorcount > 3) {
  1559. #endif
  1560. // applog(LOG_ERR, "%s%d: read errorcount>100 ret=%d",
  1561. // bitmain->drv->name, bitmain->device_id, ret);
  1562. cgsleep_ms(20);
  1563. errorcount = 0;
  1564. }
  1565. continue;
  1566. }
  1567. if (opt_debug) {
  1568. applog(LOG_DEBUG, "%s%d: get:",
  1569. bitmain->drv->name, bitmain->device_id);
  1570. hexdump((uint8_t *)buf, ret);
  1571. }
  1572. memcpy(readbuf+offset, buf, ret);
  1573. offset += ret;
  1574. }
  1575. return NULL;
  1576. }
  1577. /*
  1578. static void bitmain_set_timeout(struct bitmain_info *info)
  1579. {
  1580. info->timeout = BITMAIN_TIMEOUT_FACTOR / info->frequency;
  1581. }
  1582. */
  1583. static void bitmain_init(struct cgpu_info *bitmain)
  1584. {
  1585. applog(LOG_INFO, "%s%d: opened on %s",
  1586. bitmain->drv->name, bitmain->device_id,
  1587. bitmain->device_path);
  1588. }
  1589. static bool bitmain_prepare(struct thr_info *thr)
  1590. {
  1591. struct cgpu_info *bitmain = thr->cgpu;
  1592. struct bitmain_info *info = bitmain->device_data;
  1593. info->thr = thr;
  1594. mutex_init(&info->lock);
  1595. mutex_init(&info->qlock);
  1596. if (unlikely(pthread_cond_init(&info->qcond, NULL)))
  1597. quit(1, "Failed to pthread_cond_init bitmain qcond");
  1598. cgsem_init(&info->write_sem);
  1599. if (pthread_create(&info->read_thr, NULL, bitmain_get_results, (void *)bitmain))
  1600. quit(1, "Failed to create bitmain read_thr");
  1601. bitmain_init(bitmain);
  1602. return true;
  1603. }
  1604. static int bitmain_initialize(struct cgpu_info *bitmain)
  1605. {
  1606. uint8_t data[BITMAIN_READBUF_SIZE];
  1607. struct bitmain_info *info = NULL;
  1608. int ret = 0;
  1609. uint8_t sendbuf[BITMAIN_SENDBUF_SIZE];
  1610. int readlen = 0;
  1611. int sendlen = 0;
  1612. int trycount = 3;
  1613. struct timespec p;
  1614. struct bitmain_rxstatus_data rxstatusdata;
  1615. int i = 0, j = 0, m = 0, statusok = 0;
  1616. uint32_t checkbit = 0x00000000;
  1617. #ifdef USE_ANT_S1
  1618. int eft = 0;
  1619. #else
  1620. int r = 0;
  1621. int hwerror_eft = 0;
  1622. int beeper_ctrl = 1;
  1623. int tempover_ctrl = 1;
  1624. struct bitmain_packet_head packethead;
  1625. int asicnum = 0;
  1626. int mathtest = (int)floor(log2(42));
  1627. if (mathtest != 5) {
  1628. applog(LOG_ERR, "%s%d: %s() floating point math library is deficient",
  1629. bitmain->drv->name, bitmain->device_id, __func__);
  1630. return -1;
  1631. }
  1632. #endif
  1633. /* Send reset, then check for result */
  1634. if (!bitmain) {
  1635. applog(LOG_WARNING, "%s%d: %s() cgpu_info is null",
  1636. bitmain->drv->name, bitmain->device_id, __func__);
  1637. return -1;
  1638. }
  1639. info = bitmain->device_data;
  1640. /* clear read buf */
  1641. ret = bitmain_read(bitmain, data, BITMAIN_READBUF_SIZE,
  1642. BITMAIN_RESET_TIMEOUT, C_BITMAIN_READ);
  1643. if (ret > 0) {
  1644. if (opt_debug) {
  1645. applog(LOG_DEBUG, "%s%d: clear read(%d):",
  1646. bitmain->drv->name, bitmain->device_id, ret);
  1647. hexdump(data, ret);
  1648. }
  1649. }
  1650. sendlen = bitmain_set_rxstatus((struct bitmain_rxstatus_token *)sendbuf, 0, 1, 0, 0);
  1651. if (sendlen <= 0) {
  1652. applog(LOG_ERR, "%s%d: %s() set_rx error(%d)",
  1653. bitmain->drv->name, bitmain->device_id, __func__, sendlen);
  1654. return -1;
  1655. }
  1656. ret = bitmain_send_data(sendbuf, sendlen, bitmain);
  1657. if (unlikely(ret == BTM_SEND_ERROR)) {
  1658. applog(LOG_ERR, "%s%d: %s() send_data error",
  1659. bitmain->drv->name, bitmain->device_id, __func__);
  1660. return -1;
  1661. }
  1662. while (trycount >= 0) {
  1663. ret = bitmain_read(bitmain, data+readlen, BITMAIN_READBUF_SIZE,
  1664. BITMAIN_RESET_TIMEOUT, C_BITMAIN_DATA_RXSTATUS);
  1665. if (ret > 0) {
  1666. readlen += ret;
  1667. if (readlen > BITMAIN_READ_SIZE) {
  1668. for (i = 0; i < readlen; i++) {
  1669. if (data[i] == 0xa1) {
  1670. if (opt_debug) {
  1671. applog(LOG_DEBUG, "%s%d: initset get:",
  1672. bitmain->drv->name,
  1673. bitmain->device_id);
  1674. hexdump(data, readlen);
  1675. }
  1676. #ifdef USE_ANT_S1
  1677. if (data[i+1] > 124) {
  1678. applog(LOG_ERR, "%s%d: %s() rxstatus datalen=%d error",
  1679. bitmain->drv->name, bitmain->device_id,
  1680. __func__, data[i+1]+2);
  1681. continue;
  1682. }
  1683. if (readlen-i < data[i+1]+2) {
  1684. applog(LOG_ERR, "%s%d: %s() rxstatus datalen=%d low",
  1685. bitmain->drv->name, bitmain->device_id,
  1686. __func__, data[i+1]+2);
  1687. continue;
  1688. }
  1689. if (bitmain_parse_rxstatus(data+i, data[i+1]+2, &rxstatusdata) != 0) {
  1690. applog(LOG_ERR, "%s%d: %s() parse_rxstatus error",
  1691. bitmain->drv->name, bitmain->device_id,
  1692. __func__);
  1693. continue;
  1694. }
  1695. info->chain_num = rxstatusdata.chain_num;
  1696. info->fifo_space = rxstatusdata.fifo_space;
  1697. info->nonce_error = 0;
  1698. info->last_nonce_error = 0;
  1699. applog(LOG_ERR, "%s%d: %s() parse_rxstatus "
  1700. "version(%d) chain_num(%d) fifo_space(%d) "
  1701. "nonce_error(%d) freq=%d",
  1702. bitmain->drv->name, bitmain->device_id,
  1703. __func__,
  1704. rxstatusdata.version,
  1705. info->chain_num,
  1706. info->fifo_space,
  1707. rxstatusdata.nonce_error,
  1708. info->frequency);
  1709. for (i = 0; i < rxstatusdata.chain_num; i++) {
  1710. info->chain_asic_num[i] = rxstatusdata.chain_asic_num[i];
  1711. info->chain_asic_status[i] = rxstatusdata.chain_asic_status[i];
  1712. memset(info->chain_asic_status_t[i], 0, 40);
  1713. j = 0;
  1714. for (m = 0; m < 32; m++) {
  1715. if (m%8 == 0 && m != 0) {
  1716. info->chain_asic_status_t[i][j] = ' ';
  1717. j++;
  1718. }
  1719. checkbit = num2bit(m);
  1720. if (rxstatusdata.chain_asic_status[i] & checkbit)
  1721. info->chain_asic_status_t[i][j] = 'o';
  1722. else
  1723. info->chain_asic_status_t[i][j] = 'x';
  1724. j++;
  1725. }
  1726. applog(LOG_ERR, "%s%d: %s() parse_rxstatus chain(%d) "
  1727. "asic_num=%d asic_status=%08x-%s",
  1728. bitmain->drv->name, bitmain->device_id,
  1729. __func__, i, info->chain_asic_num[i],
  1730. info->chain_asic_status[i],
  1731. info->chain_asic_status_t[i]);
  1732. }
  1733. #else // S2
  1734. memcpy(&packethead, data+i, sizeof(struct bitmain_packet_head));
  1735. packethead.length = htole16(packethead.length);
  1736. if (packethead.length > 1130) {
  1737. applog(LOG_ERR, "%s%d: %s() rxstatus datalen=%d error",
  1738. bitmain->drv->name, bitmain->device_id,
  1739. __func__, packethead.length+4);
  1740. continue;
  1741. }
  1742. if (readlen-i < packethead.length+4) {
  1743. applog(LOG_ERR, "%s%d: %s() rxstatus datalen=%d<%d low",
  1744. bitmain->drv->name, bitmain->device_id,
  1745. __func__, readlen-i, packethead.length+4);
  1746. continue;
  1747. }
  1748. if (bitmain_parse_rxstatus(data+i, packethead.length+4, &rxstatusdata) != 0) {
  1749. applog(LOG_ERR, "%s%d: %s() parse_rxstatus error",
  1750. bitmain->drv->name, bitmain->device_id,
  1751. __func__);
  1752. continue;
  1753. }
  1754. info->chain_num = rxstatusdata.chain_num;
  1755. info->fifo_space = rxstatusdata.fifo_space;
  1756. info->hw_version[0] = rxstatusdata.hw_version[0];
  1757. info->hw_version[1] = rxstatusdata.hw_version[1];
  1758. info->hw_version[2] = rxstatusdata.hw_version[2];
  1759. info->hw_version[3] = rxstatusdata.hw_version[3];
  1760. info->nonce_error = 0;
  1761. info->last_nonce_error = 0;
  1762. applog(LOG_ERR, "%s%d: %s() parse_rxstatus "
  1763. "version(%d) chain_num(%d) fifo_space(%d) "
  1764. "hwv1(%d) hwv2(%d) hwv3(%d) hwv4(%d) "
  1765. "nonce_error(%d) freq=%d",
  1766. bitmain->drv->name, bitmain->device_id,
  1767. __func__, rxstatusdata.version,
  1768. info->chain_num, info->fifo_space,
  1769. info->hw_version[0], info->hw_version[1],
  1770. info->hw_version[2], info->hw_version[3],
  1771. rxstatusdata.nonce_error,
  1772. info->frequency);
  1773. memcpy(info->chain_asic_exist,
  1774. rxstatusdata.chain_asic_exist,
  1775. BITMAIN_MAX_CHAIN_NUM*32);
  1776. memcpy(info->chain_asic_status,
  1777. rxstatusdata.chain_asic_status,
  1778. BITMAIN_MAX_CHAIN_NUM*32);
  1779. for (i = 0; i < rxstatusdata.chain_num; i++) {
  1780. info->chain_asic_num[i] = rxstatusdata.chain_asic_num[i];
  1781. memset(info->chain_asic_status_t[i], 0, 320);
  1782. j = 0;
  1783. if (info->chain_asic_num[i] <= 0)
  1784. asicnum = 0;
  1785. else {
  1786. if (info->chain_asic_num[i] % 32 == 0)
  1787. asicnum = info->chain_asic_num[i] / 32;
  1788. else
  1789. asicnum = info->chain_asic_num[i] / 32 + 1;
  1790. }
  1791. if (asicnum > 0) {
  1792. for (m = asicnum-1; m >= 0; m--) {
  1793. for (r = 0; r < 32; r++) {
  1794. if (r%8 == 0 && r != 0) {
  1795. info->chain_asic_status_t[i][j] = ' ';
  1796. j++;
  1797. }
  1798. checkbit = num2bit(r);
  1799. if (rxstatusdata.chain_asic_exist[i*8+m] & checkbit) {
  1800. if (rxstatusdata.chain_asic_status[i*8+m] & checkbit)
  1801. info->chain_asic_status_t[i][j] = 'o';
  1802. else
  1803. info->chain_asic_status_t[i][j] = 'x';
  1804. } else
  1805. info->chain_asic_status_t[i][j] = '-';
  1806. j++;
  1807. }
  1808. info->chain_asic_status_t[i][j] = ' ';
  1809. j++;
  1810. }
  1811. }
  1812. applog(LOG_DEBUG, "%s%d: %s() chain(%d) asic_num=%d "
  1813. "asic_exist=%08x%08x%08x%08x%08x%08x%08x%08x "
  1814. "asic_status=%08x%08x%08x%08x%08x%08x%08x%08x",
  1815. bitmain->drv->name, bitmain->device_id,
  1816. __func__, i, info->chain_asic_num[i],
  1817. info->chain_asic_exist[i*8+0],
  1818. info->chain_asic_exist[i*8+1],
  1819. info->chain_asic_exist[i*8+2],
  1820. info->chain_asic_exist[i*8+3],
  1821. info->chain_asic_exist[i*8+4],
  1822. info->chain_asic_exist[i*8+5],
  1823. info->chain_asic_exist[i*8+6],
  1824. info->chain_asic_exist[i*8+7],
  1825. info->chain_asic_status[i*8+0],
  1826. info->chain_asic_status[i*8+1],
  1827. info->chain_asic_status[i*8+2],
  1828. info->chain_asic_status[i*8+3],
  1829. info->chain_asic_status[i*8+4],
  1830. info->chain_asic_status[i*8+5],
  1831. info->chain_asic_status[i*8+6],
  1832. info->chain_asic_status[i*8+7]);
  1833. applog(LOG_ERR, "%s%d: %s() chain(%d) "
  1834. "asic_num=%d asic_status=%s",
  1835. bitmain->drv->name, bitmain->device_id,
  1836. __func__, i, info->chain_asic_num[i],
  1837. info->chain_asic_status_t[i]);
  1838. }
  1839. #endif
  1840. bitmain_update_temps(bitmain, info, &rxstatusdata);
  1841. statusok = 1;
  1842. break;
  1843. }
  1844. }
  1845. if (statusok)
  1846. break;
  1847. }
  1848. }
  1849. trycount--;
  1850. p.tv_sec = 0;
  1851. p.tv_nsec = BITMAIN_RESET_PITCH;
  1852. nanosleep(&p, NULL);
  1853. }
  1854. p.tv_sec = 0;
  1855. p.tv_nsec = BITMAIN_RESET_PITCH;
  1856. nanosleep(&p, NULL);
  1857. cgtime(&info->last_status_time);
  1858. if (statusok) {
  1859. applog(LOG_ERR, "%s%d: %s() set_txconfig",
  1860. bitmain->drv->name, bitmain->device_id, __func__);
  1861. #ifdef USE_ANT_S1
  1862. if (opt_bitmain_hwerror)
  1863. eft = 1;
  1864. else
  1865. eft = 0;
  1866. sendlen = bitmain_set_txconfig((struct bitmain_txconfig_token *)sendbuf,
  1867. 1, 1, 1, 1, 1, 0, 1, eft,
  1868. info->chain_num, info->asic_num,
  1869. BITMAIN_DEFAULT_FAN_MAX_PWM, info->timeout,
  1870. info->frequency, BITMAIN_DEFAULT_VOLTAGE,
  1871. 0, 0, 0x04, info->reg_data);
  1872. #else // S2
  1873. if (opt_bitmain_hwerror)
  1874. hwerror_eft = 1;
  1875. else
  1876. hwerror_eft = 0;
  1877. if (opt_bitmain_beeper)
  1878. beeper_ctrl = 1;
  1879. else
  1880. beeper_ctrl = 0;
  1881. if (opt_bitmain_tempoverctrl)
  1882. tempover_ctrl = 1;
  1883. else
  1884. tempover_ctrl = 0;
  1885. sendlen = bitmain_set_txconfig((struct bitmain_txconfig_token *)sendbuf,
  1886. 1, 1, 1, 1, 1, 0, 1, hwerror_eft,
  1887. beeper_ctrl, tempover_ctrl,
  1888. info->chain_num, info->asic_num,
  1889. BITMAIN_DEFAULT_FAN_MAX_PWM, info->timeout,
  1890. info->frequency, BITMAIN_DEFAULT_VOLTAGE,
  1891. 0, 0, 0x04, info->reg_data);
  1892. #endif
  1893. if (sendlen <= 0) {
  1894. applog(LOG_ERR, "%s%d: %s() set_txconfig error(%d)",
  1895. bitmain->drv->name, bitmain->device_id, __func__, sendlen);
  1896. return -1;
  1897. }
  1898. ret = bitmain_send_data(sendbuf, sendlen, bitmain);
  1899. if (unlikely(ret == BTM_SEND_ERROR)) {
  1900. applog(LOG_ERR, "%s%d: %s() send_data error",
  1901. bitmain->drv->name, bitmain->device_id, __func__);
  1902. return -1;
  1903. }
  1904. applog(LOG_WARNING, "%s%d: %s() succeeded",
  1905. bitmain->drv->name, bitmain->device_id, __func__);
  1906. } else {
  1907. applog(LOG_WARNING, "%s%d: %s() failed",
  1908. bitmain->drv->name, bitmain->device_id, __func__);
  1909. return -1;
  1910. }
  1911. return 0;
  1912. }
  1913. static void ant_info(struct bitmain_info *info, int baud, int chain_num, int asic_num, int timeout, int frequency, uint8_t *reg_data)
  1914. {
  1915. info->baud = baud;
  1916. info->chain_num = chain_num;
  1917. info->asic_num = asic_num;
  1918. info->timeout = timeout;
  1919. info->frequency = frequency;
  1920. memcpy(info->reg_data, reg_data, 4);
  1921. info->voltage = BITMAIN_DEFAULT_VOLTAGE;
  1922. info->fan_pwm = BITMAIN_DEFAULT_FAN_MIN_PWM;
  1923. info->temp_max = 0;
  1924. /* This is for check the temp/fan every 3~4s */
  1925. info->temp_history_count = (4 / (float)((float)info->timeout * ((float)1.67/0x32))) + 1;
  1926. if (info->temp_history_count <= 0)
  1927. info->temp_history_count = 1;
  1928. info->temp_history_index = 0;
  1929. info->temp_sum = 0;
  1930. }
  1931. static struct cgpu_info *bitmain_detect_one(libusb_device *dev, struct usb_find_devices *found)
  1932. {
  1933. int baud, chain_num, asic_num, timeout, frequency = 0;
  1934. uint8_t reg_data[4] = {0};
  1935. struct bitmain_info *info;
  1936. struct cgpu_info *bitmain;
  1937. bool configured;
  1938. int ret;
  1939. if (opt_bitmain_options == NULL)
  1940. return NULL;
  1941. bitmain = usb_alloc_cgpu(&ANTDRV, BITMAIN_MINER_THREADS);
  1942. baud = BITMAIN_IO_SPEED;
  1943. chain_num = BITMAIN_DEFAULT_CHAIN_NUM;
  1944. asic_num = BITMAIN_DEFAULT_ASIC_NUM;
  1945. timeout = BITMAIN_DEFAULT_TIMEOUT;
  1946. frequency = BITMAIN_DEFAULT_FREQUENCY;
  1947. if (!usb_init(bitmain, dev, found))
  1948. goto shin;
  1949. configured = get_options(++option_offset, &baud, &chain_num,
  1950. &asic_num, &timeout, &frequency, reg_data);
  1951. /* Even though this is an FTDI type chip, we want to do the parsing
  1952. * all ourselves so set it to std usb type */
  1953. bitmain->usbdev->usb_type = USB_TYPE_STD;
  1954. bitmain->device_data = calloc(sizeof(struct bitmain_info), 1);
  1955. if (unlikely(!(bitmain->device_data)))
  1956. quit(1, "Failed to calloc bitmain_info data");
  1957. info = bitmain->device_data;
  1958. if (configured)
  1959. ant_info(info, baud, chain_num, asic_num, timeout, frequency, reg_data);
  1960. else
  1961. ant_info(info, BITMAIN_IO_SPEED, BITMAIN_DEFAULT_CHAIN_NUM,
  1962. BITMAIN_DEFAULT_ASIC_NUM, BITMAIN_DEFAULT_TIMEOUT,
  1963. BITMAIN_DEFAULT_FREQUENCY, reg_data);
  1964. if (!add_cgpu(bitmain))
  1965. goto unshin;
  1966. applog(LOG_ERR, "%s: detected %s%d",
  1967. ANTDRV.dname, bitmain->drv->name, bitmain->device_id);
  1968. ret = bitmain_initialize(bitmain);
  1969. if (ret && !configured)
  1970. goto unshin;
  1971. update_usb_stats(bitmain);
  1972. info->errorcount = 0;
  1973. info->work_list = k_new_list("Work", sizeof(WITEM), ALLOC_WITEMS, LIMIT_WITEMS, true);
  1974. info->work_ready = k_new_store(info->work_list);
  1975. #ifdef USE_ANT_S2
  1976. info->wbuild = k_new_store(info->work_list);
  1977. #endif
  1978. applog(LOG_DEBUG, "%s%d: detected %s "
  1979. "chain_num=%d asic_num=%d timeout=%d frequency=%d",
  1980. bitmain->drv->name, bitmain->device_id, bitmain->device_path,
  1981. info->chain_num, info->asic_num, info->timeout,
  1982. info->frequency);
  1983. return bitmain;
  1984. unshin:
  1985. usb_uninit(bitmain);
  1986. shin:
  1987. free(bitmain->device_data);
  1988. bitmain->device_data = NULL;
  1989. bitmain = usb_free_cgpu(bitmain);
  1990. return NULL;
  1991. }
  1992. #ifdef USE_ANT_S2
  1993. static void ser_detect()
  1994. {
  1995. int baud, chain_num, asic_num, timeout, frequency = 0;
  1996. uint8_t reg_data[4] = {0};
  1997. struct cgpu_info *bitmain;
  1998. struct bitmain_info *info;
  1999. bool configured;
  2000. int ret;
  2001. applog(LOG_WARNING, "%s: checking for %s", ANTDRV.dname, opt_bitmain_dev);
  2002. if (!opt_bitmain_options || !(*opt_bitmain_options)) {
  2003. applog(LOG_ERR, "%s: no bitmain-options specified", ANTDRV.dname);
  2004. return;
  2005. }
  2006. bitmain = calloc(1, sizeof(*bitmain));
  2007. if (unlikely(!bitmain))
  2008. quithere(1, "Failed to calloc bitmain");
  2009. bitmain->drv = &ANTDRV;
  2010. bitmain->deven = DEV_ENABLED;
  2011. bitmain->threads = 1;
  2012. bitmain->usbinfo.nodev = true;
  2013. configured = get_options(++option_offset, &baud, &chain_num,
  2014. &asic_num, &timeout, &frequency, reg_data);
  2015. info = calloc(1, sizeof(*info));
  2016. if (unlikely(!info))
  2017. quit(1, "Failed to calloc bitmain_info");
  2018. bitmain->device_data = (void *)info;
  2019. info->device_fd = open(opt_bitmain_dev, O_RDWR|O_EXCL|O_NONBLOCK);
  2020. if (info->device_fd == -1) {
  2021. applog(LOG_DEBUG, "%s open %s error %d",
  2022. bitmain->drv->dname, opt_bitmain_dev, errno);
  2023. goto giveup;
  2024. }
  2025. bitmain->device_path = strdup(opt_bitmain_dev);
  2026. bitmain->usbinfo.nodev = false;
  2027. if (configured)
  2028. ant_info(info, baud, chain_num, asic_num, timeout, frequency, reg_data);
  2029. else
  2030. ant_info(info, BITMAIN_IO_SPEED, BITMAIN_DEFAULT_CHAIN_NUM,
  2031. BITMAIN_DEFAULT_ASIC_NUM, BITMAIN_DEFAULT_TIMEOUT,
  2032. BITMAIN_DEFAULT_FREQUENCY, reg_data);
  2033. if (!add_cgpu(bitmain))
  2034. goto cleen;
  2035. ret = bitmain_initialize(bitmain);
  2036. if (ret && !configured)
  2037. goto cleen;
  2038. info->errorcount = 0;
  2039. info->work_list = k_new_list("Work", sizeof(WITEM), ALLOC_WITEMS, LIMIT_WITEMS, true);
  2040. info->work_ready = k_new_store(info->work_list);
  2041. info->wbuild = k_new_store(info->work_list);
  2042. applog(LOG_DEBUG, "%s%d: detected %s "
  2043. "chain_num=%d asic_num=%d timeout=%d frequency=%d",
  2044. bitmain->drv->name, bitmain->device_id, bitmain->device_path,
  2045. info->chain_num, info->asic_num, info->timeout,
  2046. info->frequency);
  2047. dupalloc(bitmain, 10);
  2048. return;
  2049. cleen:
  2050. if (info->device_fd != -1)
  2051. close(info->device_fd);
  2052. free(bitmain->device_path);
  2053. giveup:
  2054. free(info);
  2055. free(bitmain);
  2056. }
  2057. #endif
  2058. #ifdef USE_ANT_S1
  2059. static void ants1_detect(bool __maybe_unused hotplug)
  2060. {
  2061. is_usb = true;
  2062. usb_detect(&ANTDRV, bitmain_detect_one);
  2063. }
  2064. #endif
  2065. #ifdef USE_ANT_S2
  2066. static bool first_ant = true;
  2067. static void ants2_detect(bool __maybe_unused hotplug)
  2068. {
  2069. // Only ever do this once
  2070. if (!first_ant)
  2071. return;
  2072. first_ant = false;
  2073. if (opt_bitmain_dev && *opt_bitmain_dev)
  2074. is_usb = false;
  2075. else
  2076. is_usb = true;
  2077. if (is_usb)
  2078. usb_detect(&ANTDRV, bitmain_detect_one);
  2079. else
  2080. ser_detect();
  2081. }
  2082. #endif
  2083. static void do_bitmain_close(struct thr_info *thr)
  2084. {
  2085. struct cgpu_info *bitmain = thr->cgpu;
  2086. struct bitmain_info *info = bitmain->device_data;
  2087. pthread_join(info->read_thr, NULL);
  2088. bitmain_running_reset(info);
  2089. info->no_matching_work = 0;
  2090. cgsem_destroy(&info->write_sem);
  2091. }
  2092. static void get_bitmain_statline_before(char *buf, size_t bufsiz, struct cgpu_info *bitmain)
  2093. {
  2094. struct bitmain_info *info = bitmain->device_data;
  2095. int lowfan = 10000;
  2096. int i = 0;
  2097. /* Find the lowest fan speed of the ASIC cooling fans. */
  2098. for (i = 0; i < info->fan_num; i++) {
  2099. if (info->fan[i] >= 0 && info->fan[i] < lowfan)
  2100. lowfan = info->fan[i];
  2101. }
  2102. tailsprintf(buf, bufsiz, "%2d/%3dC %04dR", info->temp_avg, info->temp_max, lowfan);
  2103. }
  2104. /* We use a replacement algorithm to only remove references to work done from
  2105. * the buffer when we need the extra space for new work. */
  2106. static bool bitmain_fill(struct cgpu_info *bitmain)
  2107. {
  2108. struct bitmain_info *info = bitmain->device_data;
  2109. struct work *work, *usework;
  2110. bool ret = true;
  2111. int sendret = 0, sentcount = 0, neednum = 0, queuednum = 0, sendnum = 0, sendlen = 0;
  2112. int roll, roll_limit;
  2113. uint8_t sendbuf[BITMAIN_SENDBUF_SIZE];
  2114. cgtimer_t ts_start;
  2115. int senderror = 0;
  2116. struct timeval now;
  2117. int timediff = 0;
  2118. K_ITEM *witem;
  2119. #ifdef USE_ANT_S1
  2120. /*
  2121. * Overheat just means delay the next work
  2122. * since the temperature reply is only found with a work reply,
  2123. * we can only sleep and hope it will cool down
  2124. * TODO: of course it may be possible to read the temperature
  2125. * without sending work ...
  2126. */
  2127. if (info->overheat == true) {
  2128. if (info->overheat_sleep_ms == 0)
  2129. info->overheat_sleep_ms = BITMAIN_OVERHEAT_SLEEP_MS_DEF;
  2130. /*
  2131. * If we slept and we are still here, and the temp didn't drop,
  2132. * increment the sleep time to find a sleep time that causes a
  2133. * temperature drop
  2134. */
  2135. if (info->overheat_slept) {
  2136. if (info->overheat_temp > info->temp_hi)
  2137. info->overheat_temp = info->temp_hi;
  2138. else {
  2139. if (info->overheat_sleep_ms < BITMAIN_OVERHEAT_SLEEP_MS_MAX)
  2140. info->overheat_sleep_ms += BITMAIN_OVERHEAT_SLEEP_MS_STEP;
  2141. }
  2142. }
  2143. applog(LOG_DEBUG, "%s%d: %s() sleeping %"PRIu32" - overheated",
  2144. bitmain->drv->name, bitmain->device_id,
  2145. __func__, info->overheat_sleep_ms);
  2146. cgsleep_ms(info->overheat_sleep_ms);
  2147. info->overheat_sleeps++;
  2148. info->overheat_slept = info->overheat_sleep_ms;
  2149. info->overheat_total_sleep += info->overheat_sleep_ms;
  2150. } else {
  2151. // If we slept and it cooled then try less next time
  2152. if (info->overheat_slept) {
  2153. if (info->overheat_sleep_ms > BITMAIN_OVERHEAT_SLEEP_MS_MIN)
  2154. info->overheat_sleep_ms -= BITMAIN_OVERHEAT_SLEEP_MS_STEP;
  2155. info->overheat_slept = 0;
  2156. }
  2157. }
  2158. #endif
  2159. applog(LOG_DEBUG, "%s%d: %s() start",
  2160. bitmain->drv->name, bitmain->device_id,
  2161. __func__);
  2162. mutex_lock(&info->qlock);
  2163. if (info->fifo_space <= 0) {
  2164. applog(LOG_DEBUG, "%s%d: %s() fifo space empty",
  2165. bitmain->drv->name, bitmain->device_id,
  2166. __func__);
  2167. ret = true;
  2168. goto out_unlock;
  2169. }
  2170. if (info->queued >= BITMAIN_MAX_WORK_QUEUE_NUM)
  2171. ret = true;
  2172. else
  2173. ret = false;
  2174. while (info->fifo_space > 0) {
  2175. #ifdef USE_ANT_S1
  2176. neednum = info->fifo_space<8?info->fifo_space:8;
  2177. #else
  2178. neednum = info->fifo_space<BITMAIN_MAX_WORK_NUM?info->fifo_space:BITMAIN_MAX_WORK_NUM;
  2179. #endif
  2180. queuednum = info->queued;
  2181. applog(LOG_DEBUG, "%s%d: Work task queued(%d) fifo space(%d) needsend(%d)",
  2182. bitmain->drv->name, bitmain->device_id,
  2183. queuednum, info->fifo_space, neednum);
  2184. while (queuednum < neednum) {
  2185. work = get_queued(bitmain);
  2186. if (!work)
  2187. break;
  2188. else {
  2189. roll_limit = work->drv_rolllimit;
  2190. roll = 0;
  2191. while (queuednum < neednum && roll <= roll_limit) {
  2192. applog(LOG_DEBUG, "%s%d: get work queued number:%d"
  2193. " neednum:%d",
  2194. bitmain->drv->name,
  2195. bitmain->device_id,
  2196. queuednum, neednum);
  2197. // Using devflag to say if it was rolled
  2198. if (roll == 0) {
  2199. usework = work;
  2200. usework->devflag = false;
  2201. } else {
  2202. usework = copy_work_noffset(work, roll);
  2203. usework->devflag = true;
  2204. }
  2205. witem = k_unlink_tail(info->work_list);
  2206. if (DATAW(witem)->work) {
  2207. // Was it rolled?
  2208. if (DATAW(witem)->work->devflag)
  2209. free_work(DATAW(witem)->work);
  2210. else
  2211. work_completed(bitmain, DATAW(witem)->work);
  2212. }
  2213. DATAW(witem)->work = usework;
  2214. DATAW(witem)->wid = ++info->last_wid;
  2215. info->queued++;
  2216. k_add_head(info->work_ready, witem);
  2217. queuednum++;
  2218. roll++;
  2219. }
  2220. }
  2221. }
  2222. if (queuednum < BITMAIN_MAX_DEAL_QUEUE_NUM) {
  2223. if (queuednum < neednum) {
  2224. applog(LOG_DEBUG, "%s%d: Not enough work to send, queue num=%d",
  2225. bitmain->drv->name, bitmain->device_id,
  2226. queuednum);
  2227. break;
  2228. }
  2229. }
  2230. sendnum = queuednum < neednum ? queuednum : neednum;
  2231. sendlen = bitmain_set_txtask(info, sendbuf, &(info->last_work_block), &sentcount);
  2232. info->queued -= sendnum;
  2233. info->send_full_space += sendnum;
  2234. if (info->queued < 0)
  2235. info->queued = 0;
  2236. applog(LOG_DEBUG, "%s%d: Send work %d",
  2237. bitmain->drv->name, bitmain->device_id,
  2238. sentcount);
  2239. if (sendlen > 0) {
  2240. info->fifo_space -= sentcount;
  2241. if (info->fifo_space < 0)
  2242. info->fifo_space = 0;
  2243. sendret = bitmain_send_data(sendbuf, sendlen, bitmain);
  2244. if (unlikely(sendret == BTM_SEND_ERROR)) {
  2245. applog(LOG_ERR, "%s%d: send work comms error",
  2246. bitmain->drv->name, bitmain->device_id);
  2247. //dev_error(bitmain, REASON_DEV_COMMS_ERROR);
  2248. info->reset = true;
  2249. info->errorcount++;
  2250. senderror = 1;
  2251. if (info->errorcount > 1000) {
  2252. info->errorcount = 0;
  2253. applog(LOG_ERR, "%s%d: Device disappeared,"
  2254. " shutting down thread",
  2255. bitmain->drv->name, bitmain->device_id);
  2256. bitmain->shutdown = true;
  2257. }
  2258. break;
  2259. } else {
  2260. applog(LOG_DEBUG, "%s%d: send_data ret=%d",
  2261. bitmain->drv->name, bitmain->device_id,
  2262. sendret);
  2263. info->errorcount = 0;
  2264. }
  2265. } else {
  2266. applog(LOG_DEBUG, "%s%d: Send work set_txtask error: %d",
  2267. bitmain->drv->name, bitmain->device_id,
  2268. sendlen);
  2269. break;
  2270. }
  2271. }
  2272. out_unlock:
  2273. cgtime(&now);
  2274. timediff = now.tv_sec - info->last_status_time.tv_sec;
  2275. if (timediff < 0) timediff = -timediff;
  2276. if (now.tv_sec - info->last_status_time.tv_sec > BITMAIN_SEND_STATUS_TIME) {
  2277. applog(LOG_DEBUG, "%s%d: Send RX Status Token fifo_space(%d) timediff(%d)",
  2278. bitmain->drv->name, bitmain->device_id,
  2279. info->fifo_space, timediff);
  2280. copy_time(&(info->last_status_time), &now);
  2281. sendlen = bitmain_set_rxstatus((struct bitmain_rxstatus_token *) sendbuf, 0, 0, 0, 0);
  2282. if (sendlen > 0) {
  2283. sendret = bitmain_send_data(sendbuf, sendlen, bitmain);
  2284. if (unlikely(sendret == BTM_SEND_ERROR)) {
  2285. applog(LOG_ERR, "%s%d: send status comms error",
  2286. bitmain->drv->name, bitmain->device_id);
  2287. //dev_error(bitmain, REASON_DEV_COMMS_ERROR);
  2288. info->reset = true;
  2289. info->errorcount++;
  2290. senderror = 1;
  2291. if (info->errorcount > 1000) {
  2292. info->errorcount = 0;
  2293. applog(LOG_ERR, "%s%d: Device disappeared,"
  2294. " shutting down thread",
  2295. bitmain->drv->name, bitmain->device_id);
  2296. bitmain->shutdown = true;
  2297. }
  2298. } else {
  2299. info->errorcount = 0;
  2300. if (info->fifo_space <= 0) {
  2301. senderror = 1;
  2302. }
  2303. }
  2304. }
  2305. }
  2306. if (info->send_full_space > BITMAIN_SEND_FULL_SPACE) {
  2307. info->send_full_space = 0;
  2308. ret = true;
  2309. }
  2310. mutex_unlock(&info->qlock);
  2311. if (senderror) {
  2312. ret = true;
  2313. cgsleep_prepare_r(&ts_start);
  2314. cgsleep_ms_r(&ts_start, 50);
  2315. }
  2316. return ret;
  2317. }
  2318. static int64_t bitmain_scanhash(struct thr_info *thr)
  2319. {
  2320. struct cgpu_info *bitmain = thr->cgpu;
  2321. struct bitmain_info *info = bitmain->device_data;
  2322. const int chain_num = info->chain_num;
  2323. struct timeval now, then, tdiff;
  2324. int64_t hash_count, us_timeout;
  2325. /* Half nonce range */
  2326. us_timeout = 0x80000000ll / info->asic_num / info->frequency;
  2327. tdiff.tv_sec = us_timeout / 1000000;
  2328. tdiff.tv_usec = us_timeout - (tdiff.tv_sec * 1000000);
  2329. cgtime(&now);
  2330. timeradd(&now, &tdiff, &then);
  2331. mutex_lock(&info->qlock);
  2332. hash_count = 0xffffffffull * (uint64_t)info->nonces;
  2333. info->results += info->nonces;
  2334. if (info->results > chain_num)
  2335. info->results = chain_num;
  2336. if (!info->reset)
  2337. info->results--;
  2338. info->nonces = 0;
  2339. mutex_unlock(&info->qlock);
  2340. /* Check for nothing but consecutive bad results or consistently less
  2341. * results than we should be getting and reset the FPGA if necessary */
  2342. //if (info->results < -chain_num && !info->reset) {
  2343. // applog(LOG_ERR, "%s%d: Result return rate low, resetting!",
  2344. // bitmain->drv->name, bitmain->device_id);
  2345. // info->reset = true;
  2346. //}
  2347. if (unlikely(bitmain->usbinfo.nodev)) {
  2348. applog(LOG_ERR, "%s%d: Device disappeared, shutting down thread",
  2349. bitmain->drv->name, bitmain->device_id);
  2350. bitmain->shutdown = true;
  2351. }
  2352. /* This hashmeter is just a utility counter based on returned shares */
  2353. return hash_count;
  2354. }
  2355. static void bitmain_flush_work(struct cgpu_info *bitmain)
  2356. {
  2357. struct bitmain_info *info = bitmain->device_data;
  2358. //int i = 0;
  2359. applog(LOG_ERR, "%s%d: %s() queued=%d",
  2360. bitmain->drv->name, bitmain->device_id,
  2361. __func__, info->queued);
  2362. mutex_lock(&info->qlock);
  2363. /* Will overwrite any work queued */
  2364. info->queued = 0;
  2365. k_list_transfer_to_tail(info->work_ready, info->work_list);
  2366. //pthread_cond_signal(&info->qcond);
  2367. mutex_unlock(&info->qlock);
  2368. }
  2369. static struct api_data *bitmain_api_stats(struct cgpu_info *cgpu)
  2370. {
  2371. struct api_data *root = NULL;
  2372. struct bitmain_info *info = cgpu->device_data;
  2373. //int i = 0;
  2374. double hwp = (cgpu->hw_errors + cgpu->diff1) ?
  2375. (double)(cgpu->hw_errors) / (double)(cgpu->hw_errors + cgpu->diff1) : 0;
  2376. root = api_add_int(root, "baud", &(info->baud), false);
  2377. root = api_add_int(root, "miner_count", &(info->chain_num), false);
  2378. root = api_add_int(root, "asic_count", &(info->asic_num), false);
  2379. root = api_add_int(root, "timeout", &(info->timeout), false);
  2380. root = api_add_int(root, "frequency", &(info->frequency), false);
  2381. root = api_add_int(root, "voltage", &(info->voltage), false);
  2382. #ifdef USE_ANT_S2
  2383. root = api_add_int(root, "hwv1", &(info->hw_version[0]), false);
  2384. root = api_add_int(root, "hwv2", &(info->hw_version[1]), false);
  2385. root = api_add_int(root, "hwv3", &(info->hw_version[2]), false);
  2386. root = api_add_int(root, "hwv4", &(info->hw_version[3]), false);
  2387. #endif
  2388. root = api_add_int(root, "fan_num", &(info->fan_num), false);
  2389. root = api_add_int(root, "fan1", &(info->fan[0]), false);
  2390. root = api_add_int(root, "fan2", &(info->fan[1]), false);
  2391. root = api_add_int(root, "fan3", &(info->fan[2]), false);
  2392. root = api_add_int(root, "fan4", &(info->fan[3]), false);
  2393. #ifdef USE_ANT_S2
  2394. root = api_add_int(root, "fan5", &(info->fan[4]), false);
  2395. root = api_add_int(root, "fan6", &(info->fan[5]), false);
  2396. root = api_add_int(root, "fan7", &(info->fan[6]), false);
  2397. root = api_add_int(root, "fan8", &(info->fan[7]), false);
  2398. root = api_add_int(root, "fan9", &(info->fan[8]), false);
  2399. root = api_add_int(root, "fan10", &(info->fan[9]), false);
  2400. root = api_add_int(root, "fan11", &(info->fan[10]), false);
  2401. root = api_add_int(root, "fan12", &(info->fan[11]), false);
  2402. root = api_add_int(root, "fan13", &(info->fan[12]), false);
  2403. root = api_add_int(root, "fan14", &(info->fan[13]), false);
  2404. root = api_add_int(root, "fan15", &(info->fan[14]), false);
  2405. root = api_add_int(root, "fan16", &(info->fan[15]), false);
  2406. #endif
  2407. root = api_add_int(root, "temp_num", &(info->temp_num), false);
  2408. root = api_add_int(root, "temp1", &(info->temp[0]), false);
  2409. root = api_add_int(root, "temp2", &(info->temp[1]), false);
  2410. root = api_add_int(root, "temp3", &(info->temp[2]), false);
  2411. root = api_add_int(root, "temp4", &(info->temp[3]), false);
  2412. #ifdef USE_ANT_S2
  2413. root = api_add_int(root, "temp5", &(info->temp[4]), false);
  2414. root = api_add_int(root, "temp6", &(info->temp[5]), false);
  2415. root = api_add_int(root, "temp7", &(info->temp[6]), false);
  2416. root = api_add_int(root, "temp8", &(info->temp[7]), false);
  2417. root = api_add_int(root, "temp9", &(info->temp[8]), false);
  2418. root = api_add_int(root, "temp10", &(info->temp[9]), false);
  2419. root = api_add_int(root, "temp11", &(info->temp[10]), false);
  2420. root = api_add_int(root, "temp12", &(info->temp[11]), false);
  2421. root = api_add_int(root, "temp13", &(info->temp[12]), false);
  2422. root = api_add_int(root, "temp14", &(info->temp[13]), false);
  2423. root = api_add_int(root, "temp15", &(info->temp[14]), false);
  2424. root = api_add_int(root, "temp16", &(info->temp[15]), false);
  2425. #endif
  2426. root = api_add_int(root, "temp_avg", &(info->temp_avg), false);
  2427. root = api_add_int(root, "temp_max", &(info->temp_max), false);
  2428. root = api_add_percent(root, "Device Hardware%", &hwp, true);
  2429. root = api_add_int(root, "no_matching_work", &(info->no_matching_work), false);
  2430. /*
  2431. for (i = 0; i < info->chain_num; i++) {
  2432. char mcw[24];
  2433. sprintf(mcw, "match_work_count%d", i + 1);
  2434. root = api_add_int(root, mcw, &(info->matching_work[i]), false);
  2435. }*/
  2436. root = api_add_int(root, "chain_acn1", &(info->chain_asic_num[0]), false);
  2437. root = api_add_int(root, "chain_acn2", &(info->chain_asic_num[1]), false);
  2438. root = api_add_int(root, "chain_acn3", &(info->chain_asic_num[2]), false);
  2439. root = api_add_int(root, "chain_acn4", &(info->chain_asic_num[3]), false);
  2440. #ifdef USE_ANT_S2
  2441. root = api_add_int(root, "chain_acn5", &(info->chain_asic_num[4]), false);
  2442. root = api_add_int(root, "chain_acn6", &(info->chain_asic_num[5]), false);
  2443. root = api_add_int(root, "chain_acn7", &(info->chain_asic_num[6]), false);
  2444. root = api_add_int(root, "chain_acn8", &(info->chain_asic_num[7]), false);
  2445. root = api_add_int(root, "chain_acn9", &(info->chain_asic_num[8]), false);
  2446. root = api_add_int(root, "chain_acn10", &(info->chain_asic_num[9]), false);
  2447. root = api_add_int(root, "chain_acn11", &(info->chain_asic_num[10]), false);
  2448. root = api_add_int(root, "chain_acn12", &(info->chain_asic_num[11]), false);
  2449. root = api_add_int(root, "chain_acn13", &(info->chain_asic_num[12]), false);
  2450. root = api_add_int(root, "chain_acn14", &(info->chain_asic_num[13]), false);
  2451. root = api_add_int(root, "chain_acn15", &(info->chain_asic_num[14]), false);
  2452. root = api_add_int(root, "chain_acn16", &(info->chain_asic_num[15]), false);
  2453. #endif
  2454. root = api_add_string(root, "chain_acs1", info->chain_asic_status_t[0], false);
  2455. root = api_add_string(root, "chain_acs2", info->chain_asic_status_t[1], false);
  2456. root = api_add_string(root, "chain_acs3", info->chain_asic_status_t[2], false);
  2457. root = api_add_string(root, "chain_acs4", info->chain_asic_status_t[3], false);
  2458. #ifdef USE_ANT_S2
  2459. root = api_add_string(root, "chain_acs5", info->chain_asic_status_t[4], false);
  2460. root = api_add_string(root, "chain_acs6", info->chain_asic_status_t[5], false);
  2461. root = api_add_string(root, "chain_acs7", info->chain_asic_status_t[6], false);
  2462. root = api_add_string(root, "chain_acs8", info->chain_asic_status_t[7], false);
  2463. root = api_add_string(root, "chain_acs9", info->chain_asic_status_t[8], false);
  2464. root = api_add_string(root, "chain_acs10", info->chain_asic_status_t[9], false);
  2465. root = api_add_string(root, "chain_acs11", info->chain_asic_status_t[10], false);
  2466. root = api_add_string(root, "chain_acs12", info->chain_asic_status_t[11], false);
  2467. root = api_add_string(root, "chain_acs13", info->chain_asic_status_t[12], false);
  2468. root = api_add_string(root, "chain_acs14", info->chain_asic_status_t[13], false);
  2469. root = api_add_string(root, "chain_acs15", info->chain_asic_status_t[14], false);
  2470. root = api_add_string(root, "chain_acs16", info->chain_asic_status_t[15], false);
  2471. #endif
  2472. root = api_add_int(root, "work_list_total", &(info->work_list->total), true);
  2473. root = api_add_int(root, "work_list_count", &(info->work_list->count), true);
  2474. root = api_add_int(root, "work_ready_count", &(info->work_ready->count), true);
  2475. root = api_add_uint64(root, "work_search", &(info->work_search), true);
  2476. root = api_add_uint64(root, "min_search", &(info->min_search), true);
  2477. root = api_add_uint64(root, "max_search", &(info->max_search), true);
  2478. float avg = info->work_search ? (float)(info->tot_search) /
  2479. (float)(info->work_search) : 0;
  2480. root = api_add_avg(root, "avg_search", &avg, true);
  2481. root = api_add_uint64(root, "failed_search", &(info->failed_search), true);
  2482. root = api_add_uint64(root, "min_failed", &(info->min_failed), true);
  2483. root = api_add_uint64(root, "max_failed", &(info->max_failed), true);
  2484. avg = info->failed_search ? (float)(info->tot_failed) /
  2485. (float)(info->failed_search) : 0;
  2486. root = api_add_avg(root, "avg_failed", &avg, true);
  2487. root = api_add_int(root, "temp_hi", &(info->temp_hi), false);
  2488. #ifdef USE_ANT_S1
  2489. root = api_add_bool(root, "overheat", &(info->overheat), true);
  2490. root = api_add_int(root, "overheat_temp", &(info->overheat_temp), true);
  2491. root = api_add_uint32(root, "overheat_count", &(info->overheat_count), true);
  2492. root = api_add_uint32(root, "overheat_sleep_ms", &(info->overheat_sleep_ms), true);
  2493. root = api_add_uint32(root, "overheat_sleeps", &(info->overheat_sleeps), true);
  2494. root = api_add_uint32(root, "overheat_slept", &(info->overheat_slept), true);
  2495. root = api_add_uint64(root, "overheat_total_sleep", &(info->overheat_total_sleep), true);
  2496. root = api_add_uint32(root, "overheat_recovers", &(info->overheat_recovers), true);
  2497. #endif
  2498. #ifdef USE_ANT_S2
  2499. root = api_add_bool(root, "opt_bitmain_beeper", &opt_bitmain_beeper, false);
  2500. root = api_add_bool(root, "opt_bitmain_tempoverctrl", &opt_bitmain_tempoverctrl, false);
  2501. #endif
  2502. root = api_add_int(root, "opt_bitmain_temp", &opt_bitmain_temp, false);
  2503. root = api_add_int(root, "opt_bitmain_overheat", &opt_bitmain_overheat, false);
  2504. root = api_add_int(root, "opt_bitmain_fan_min", &opt_bitmain_fan_min, false);
  2505. root = api_add_int(root, "opt_bitmain_fan_max", &opt_bitmain_fan_max, false);
  2506. root = api_add_int(root, "opt_bitmain_freq_min", &opt_bitmain_freq_min, false);
  2507. root = api_add_int(root, "opt_bitmain_freq_max", &opt_bitmain_freq_max, false);
  2508. root = api_add_bool(root, "opt_bitmain_auto", &opt_bitmain_auto, false);
  2509. return root;
  2510. }
  2511. static void bitmain_shutdown(struct thr_info *thr)
  2512. {
  2513. do_bitmain_close(thr);
  2514. }
  2515. char *set_bitmain_fan(char *arg)
  2516. {
  2517. int val1, val2, ret;
  2518. ret = sscanf(arg, "%d-%d", &val1, &val2);
  2519. if (ret < 1)
  2520. return "No values passed to bitmain-fan";
  2521. if (ret == 1)
  2522. val2 = val1;
  2523. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
  2524. return "Invalid value passed to bitmain-fan";
  2525. opt_bitmain_fan_min = val1 * BITMAIN_PWM_MAX / 100;
  2526. opt_bitmain_fan_max = val2 * BITMAIN_PWM_MAX / 100;
  2527. return NULL;
  2528. }
  2529. char *set_bitmain_freq(char *arg)
  2530. {
  2531. int val1, val2, ret;
  2532. ret = sscanf(arg, "%d-%d", &val1, &val2);
  2533. if (ret < 1)
  2534. return "No values passed to bitmain-freq";
  2535. if (ret == 1)
  2536. val2 = val1;
  2537. if (val1 < BITMAIN_MIN_FREQUENCY || val1 > BITMAIN_MAX_FREQUENCY ||
  2538. val2 < BITMAIN_MIN_FREQUENCY || val2 > BITMAIN_MAX_FREQUENCY ||
  2539. val2 < val1)
  2540. return "Invalid value passed to bitmain-freq";
  2541. opt_bitmain_freq_min = val1;
  2542. opt_bitmain_freq_max = val2;
  2543. return NULL;
  2544. }
  2545. #endif // LINUX
  2546. #ifdef USE_ANT_S1
  2547. struct device_drv ants1_drv = {
  2548. .drv_id = DRIVER_ants1,
  2549. .dname = "BitmainAntS1",
  2550. .name = "ANT",
  2551. .drv_detect = ants1_detect,
  2552. #ifdef LINUX
  2553. .thread_prepare = bitmain_prepare,
  2554. .hash_work = hash_queued_work,
  2555. .queue_full = bitmain_fill,
  2556. .scanwork = bitmain_scanhash,
  2557. .flush_work = bitmain_flush_work,
  2558. .get_api_stats = bitmain_api_stats,
  2559. .get_statline_before = get_bitmain_statline_before,
  2560. .reinit_device = bitmain_init,
  2561. .thread_shutdown = bitmain_shutdown,
  2562. #endif
  2563. };
  2564. #endif
  2565. #ifdef USE_ANT_S2
  2566. struct device_drv ants2_drv = {
  2567. .drv_id = DRIVER_ants2,
  2568. .dname = "BitmainAntS2",
  2569. .name = "AS2",
  2570. .drv_detect = ants2_detect,
  2571. #ifdef LINUX
  2572. .thread_prepare = bitmain_prepare,
  2573. .hash_work = hash_queued_work,
  2574. .queue_full = bitmain_fill,
  2575. .scanwork = bitmain_scanhash,
  2576. .flush_work = bitmain_flush_work,
  2577. .get_api_stats = bitmain_api_stats,
  2578. .get_statline_before = get_bitmain_statline_before,
  2579. .reinit_device = bitmain_init,
  2580. .thread_shutdown = bitmain_shutdown,
  2581. #endif
  2582. };
  2583. #endif