driver-bitmain.c 97 KB

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