driver-bitmain.c 88 KB

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