driver-bitmain.c 103 KB

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