driver-minion.c 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809
  1. /*
  2. * Copyright 2013-2014 Andrew Smith - BlackArrow Ltd
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #include "compat.h"
  11. #include "miner.h"
  12. #include "klist.h"
  13. #include <ctype.h>
  14. #include <math.h>
  15. #ifndef LINUX
  16. static void minion_detect(__maybe_unused bool hotplug)
  17. {
  18. }
  19. #else
  20. #include <unistd.h>
  21. #include <linux/spi/spidev.h>
  22. #include <sys/ioctl.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #include <poll.h>
  26. // Define this to 1 to enable interrupt code and enable no_nonce
  27. #define ENABLE_INT_NONO 0
  28. // Define this to 1 if compiling on RockChip and not on RPi
  29. #define MINION_ROCKCHIP 0
  30. // The code is always in - this just decides if it does it
  31. static bool minreread = true;
  32. #define MINION_SPI_BUS 0
  33. #define MINION_SPI_CHIP 0
  34. //#define MINION_SPI_SPEED 2000000
  35. #define MINION_SPI_SPEED 1000000
  36. #define MINION_SPI_BUFSIZ 1024
  37. #define MINION_CHIPS 32
  38. #define MINION_CORES 99
  39. #define FAKE_CORE MINION_CORES
  40. /*
  41. * TODO: These will need adjusting for final hardware
  42. * Look them up and calculate them?
  43. */
  44. #define MINION_QUE_MAX 64
  45. #define MINION_QUE_HIGH 48
  46. #define MINION_QUE_SEND 16
  47. #define MINION_QUE_LOW 8
  48. #define MINION_FFL " - from %s %s() line %d"
  49. #define MINION_FFL_HERE __FILE__, __func__, __LINE__
  50. #define MINION_FFL_PASS file, func, line
  51. #define MINION_FFL_ARGS __maybe_unused const char *file, \
  52. __maybe_unused const char *func, \
  53. __maybe_unused const int line
  54. #define minion_txrx(_task) _minion_txrx(minioncgpu, minioninfo, _task, MINION_FFL_HERE)
  55. #define MINION_SYS_REGS 0x00
  56. #define MINION_CORE_REGS 0x10
  57. #define MINION_RES_BUF 0x20
  58. #define MINION_CMD_QUE 0x30
  59. #define MINION_NONCE_RANGES 0x70
  60. #define DATA_SIZ (sizeof(uint32_t))
  61. // All SYS data sizes are DATA_SIZ
  62. #define MINION_SYS_CHIP_SIG 0x00
  63. #define MINION_SYS_CHIP_STA 0x01
  64. #define MINION_SYS_TEMP_CTL 0x03
  65. #define MINION_SYS_FREQ_CTL 0x04
  66. #define MINION_SYS_NONCE_LED 0x05
  67. #define MINION_SYS_MISC_CTL 0x06
  68. #define MINION_SYS_RSTN_CTL 0x07
  69. #define MINION_SYS_INT_ENA 0x08
  70. #define MINION_SYS_INT_CLR 0x09
  71. #define MINION_SYS_INT_STA 0x0a
  72. #define MINION_SYS_FIFO_STA 0x0b
  73. #define MINION_SYS_QUE_TRIG 0x0c
  74. #define MINION_SYS_BUF_TRIG 0x0d
  75. #define MINION_SYS_IDLE_CNT 0x0e
  76. // How many 32 bit reports make up all the cores - 99 cores = 4 reps
  77. #define MINION_CORE_REPS (int)((((MINION_CORES-1) >> 5) & 0xff) + 1)
  78. // All SYS data sizes are DATA_SIZ
  79. #define MINION_SYS_SIZ DATA_SIZ
  80. // Header Pin 18 = GPIO5 = BCM 24
  81. #define MINION_GPIO_RESULT_INT_PIN 24
  82. // RockChip is pin 172 ...
  83. #define MINION_GPIO_SYS "/sys/class/gpio"
  84. #define MINION_GPIO_ENA "/export"
  85. #define MINION_GPIO_ENA_VAL "%d"
  86. #define MINION_GPIO_DIS "/unexport"
  87. #define MINION_GPIO_PIN "/gpio%d"
  88. #define MINION_GPIO_DIR "/direction"
  89. #define MINION_GPIO_DIR_READ "in"
  90. #define MINION_GPIO_DIR_WRITE "out"
  91. #define MINION_GPIO_EDGE "/edge"
  92. #define MINION_GPIO_EDGE_NONE "none"
  93. #define MINION_GPIO_EDGE_RISING "rising"
  94. #define MINION_GPIO_EDGE_FALLING "falling"
  95. #define MINION_GPIO_EDGE_BOTH "both"
  96. #define MINION_GPIO_ACT "/active_low"
  97. #define MINION_GPIO_ACT_LO "1"
  98. #define MINION_GPIO_ACT_HI "0"
  99. #define MINION_GPIO_VALUE "/value"
  100. #define MINION_RESULT_INT 0x01
  101. #define MINION_RESULT_FULL_INT 0x02
  102. #define MINION_CMD_INT 0x04
  103. #define MINION_CMD_FULL_INT 0x08
  104. #define MINION_TEMP_LOW_INT 0x10
  105. #define MINION_TEMP_HI_INT 0x20
  106. #define MINION_ALL_INT MINION_RESULT_INT | \
  107. MINION_RESULT_FULL_INT | \
  108. MINION_CMD_INT | \
  109. MINION_CMD_FULL_INT | \
  110. MINION_TEMP_LOW_INT | \
  111. MINION_TEMP_HI_INT
  112. #define RSTN_CTL_RESET_CORES 0x01
  113. #define RSTN_CTL_FLUSH_RESULTS 0x02
  114. #define RSTN_CTL_FLUSH_CMD_QUEUE 0x04
  115. #define RSTN_CTL_SPI_SW_RSTN 0x08
  116. #define RSTN_CTL_SHA_MGR_RESET 0x10
  117. // Init
  118. #define SYS_RSTN_CTL_INIT (RSTN_CTL_RESET_CORES | \
  119. RSTN_CTL_FLUSH_RESULTS | \
  120. RSTN_CTL_FLUSH_CMD_QUEUE | \
  121. RSTN_CTL_SPI_SW_RSTN | \
  122. RSTN_CTL_SHA_MGR_RESET)
  123. // LP
  124. #define SYS_RSTN_CTL_FLUSH (RSTN_CTL_RESET_CORES | \
  125. RSTN_CTL_SPI_SW_RSTN | \
  126. RSTN_CTL_FLUSH_CMD_QUEUE)
  127. #if ENABLE_INT_NONO
  128. // enable 'no nonce' report
  129. #define SYS_MISC_CTL_DEFAULT 0x04
  130. #else
  131. #define SYS_MISC_CTL_DEFAULT 0x00
  132. #endif
  133. // Temperature returned by MINION_SYS_CHIP_STA 0x01 STA_TEMP()
  134. #define MINION_TEMP_40 0
  135. #define MINION_TEMP_60 1
  136. #define MINION_TEMP_80 3
  137. #define MINION_TEMP_100 7
  138. #define MINION_TEMP_OVER 15
  139. static const char *min_temp_40 = "<40";
  140. static const char *min_temp_60 = "40-60";
  141. static const char *min_temp_80 = "60-80";
  142. static const char *min_temp_100 = "80-100";
  143. static const char *min_temp_over = ">100";
  144. static const char *min_temp_invalid = "?";
  145. /*
  146. * Temperature for MINION_SYS_TEMP_CTL 0x03 temp_thres [0:3]
  147. * i.e. it starts at 120 and goes up in steps of 5 to 160
  148. */
  149. #define MINION_TEMP_CTL_MIN 1
  150. #define MINION_TEMP_CTL_MAX 9
  151. #define MINION_TEMP_CTL_BITS 0x0f
  152. #define MINION_TEMP_CTL_DEF 135
  153. #define MINION_TEMP_CTL_STEP 5
  154. #define MINION_TEMP_CTL_MIN_VALUE 120
  155. #define MINION_TEMP_CTL_MAX_VALUE (MINION_TEMP_CTL_MIN_VALUE + \
  156. (MINION_TEMP_CTL_STEP * \
  157. (MINION_TEMP_CTL_MAX - MINION_TEMP_CTL_MIN)))
  158. #define MINION_TEMP_DISABLE "disable"
  159. #define MINION_TEMP_CTL_DISABLE -1
  160. #define MINION_TEMP_CTL_DISABLE_VALUE 0x20
  161. // CORE data size is DATA_SIZ
  162. #define MINION_CORE_ENA0_31 0x10
  163. #define MINION_CORE_ENA32_63 0x11
  164. #define MINION_CORE_ENA64_95 0x12
  165. #define MINION_CORE_ENA96_98 0x13
  166. #define MINION_CORE_ACT0_31 0x14
  167. #define MINION_CORE_ACT32_63 0x15
  168. #define MINION_CORE_ACT64_95 0x16
  169. #define MINION_CORE_ACT96_98 0x17
  170. // All CORE data sizes are DATA_SIZ
  171. #define MINION_CORE_SIZ DATA_SIZ
  172. #define MINION_CORE_ALL "all"
  173. // RES data size is minion_result
  174. #define MINION_RES_DATA 0x20
  175. #define MINION_RES_PEEK 0x21
  176. // QUE data size is minion_que
  177. #define MINION_QUE_0 0x30
  178. #define MINION_QUE_R 0x31
  179. // RANGE data sizes are DATA_SIZ
  180. #define MINION_NONCE_START 0x70
  181. #define MINION_NONCE_RANGE 0x71
  182. // This must be >= max txsiz + max rxsiz
  183. #define MINION_BUFSIZ 1024
  184. #define u8tou32(_c, _off) (((uint8_t *)(_c))[(_off)+0] + \
  185. ((uint8_t *)(_c))[(_off)+1] * 0x100 + \
  186. ((uint8_t *)(_c))[(_off)+2] * 0x10000 + \
  187. ((uint8_t *)(_c))[(_off)+3] * 0x1000000 )
  188. #define MINION_ADDR_WRITE 0x7f
  189. #define MINION_ADDR_READ 0x80
  190. #define READ_ADDR(_reg) ((_reg) | MINION_ADDR_READ)
  191. #define WRITE_ADDR(_reg) ((_reg) & MINION_ADDR_WRITE)
  192. #define IS_ADDR_READ(_reg) (((_reg) & MINION_ADDR_READ) == MINION_ADDR_READ)
  193. #define IS_ADDR_WRITE(_reg) (((_reg) & MINION_ADDR_READ) == 0)
  194. #define SET_HEAD_WRITE(_h, _reg) ((_h)->reg) = WRITE_ADDR(_reg)
  195. #define SET_HEAD_READ(_h, _reg) ((_h)->reg) = READ_ADDR(_reg)
  196. #define SET_HEAD_SIZ(_h, _siz) \
  197. do { \
  198. ((_h)->siz)[0] = (uint8_t)((_siz) & 0xff); \
  199. ((_h)->siz)[1] = (uint8_t)(((_siz) & 0xff00) >> 8); \
  200. } while (0)
  201. struct minion_header {
  202. uint8_t chip;
  203. uint8_t reg;
  204. uint8_t siz[2];
  205. uint8_t data[4]; // placeholder
  206. };
  207. #define HSIZE() (sizeof(struct minion_header) - 4)
  208. #define MINION_NOCHIP_SIG 0x00000000
  209. #define MINION_NOCHIP_SIG2 0xffffffff
  210. #define MINION_CHIP_SIG 0xb1ac8a44
  211. /*
  212. * Number of times to try and get the SIG with each chip,
  213. * if the chip returns neither of the above values
  214. * TODO: maybe need some reset between tries, to handle a shift value?
  215. */
  216. #define MINION_SIG_TRIES 3
  217. /*
  218. * TODO: Finding these means the chip is there - but how to fix it?
  219. * The extra &'s are to ensure there is no sign bit issue since
  220. * the sign bit carry in a C bit-shift is compiler dependent
  221. */
  222. #define MINION_CHIP_SIG_SHIFT1 (((MINION_CHIP_SIG & 0x0000ffff) << 16) & 0xffff0000)
  223. #define MINION_CHIP_SIG_SHIFT2 (((MINION_CHIP_SIG & 0x00ffffff) << 8) & 0xffffff00)
  224. #define MINION_CHIP_SIG_SHIFT3 (((MINION_CHIP_SIG & 0xffffff00) >> 8) & 0x00ffffff)
  225. #define MINION_CHIP_SIG_SHIFT4 (((MINION_CHIP_SIG & 0xffff0000) >> 16) & 0x0000ffff)
  226. #define MINION_FREQ_MIN 100
  227. #define MINION_FREQ_DEF 1000
  228. #define MINION_FREQ_MAX 1400
  229. #define MINION_FREQ_FACTOR 100
  230. #define MINION_FREQ_FACTOR_MIN 1
  231. #define MINION_FREQ_FACTOR_MAX 14
  232. static uint32_t minion_freq[] = {
  233. 0x0,
  234. 0x205032, // 1 = 100Mhz
  235. 0x203042, // 2 = 200Mhz
  236. 0x20204B, // 3 = 300Mhz
  237. 0x201042, // 4 = 400Mhz
  238. 0x201053, // 5 = 500Mhz
  239. 0x200032, // 6 = 600Mhz
  240. 0x20003A, // 7 = 700Mhz
  241. 0x200042, // 8 = 800Mhz
  242. 0x20004B, // 9 = 900Mhz
  243. 0x200053, // 10 = 1000Mhz
  244. 0x21005B, // 11 = 1100Mhz
  245. 0x210064, // 12 = 1200Mhz
  246. 0x21006C, // 13 = 1300Mhz
  247. 0x210074 // 14 = 1400Mhz
  248. };
  249. #define MINION_RESET_PERCENT 50.0
  250. #define STA_TEMP(_sta) ((uint16_t)((_sta)[3] & 0x1f))
  251. #define STA_CORES(_sta) ((uint16_t)((_sta)[2]))
  252. #define STA_FREQ(_sta) ((uint32_t)((_sta)[1]) * 0x100 + (uint32_t)((_sta)[0]))
  253. // Randomly between 1s and 2s per chip
  254. #define MINION_STATS_UPDATE_TIME_mS 1000
  255. #define MINION_STATS_UPDATE_RAND_mS 1000
  256. // Don't report it more than once every ... 5s
  257. #define MINION_IDLE_MESSAGE_ms 5000
  258. struct minion_status {
  259. uint16_t temp;
  260. uint16_t cores;
  261. uint32_t freq;
  262. uint32_t quework;
  263. uint32_t chipwork;
  264. uint32_t realwork; // chipwork, but FIFO_STA can update it
  265. struct timeval last;
  266. bool overheat;
  267. bool islow;
  268. bool tohigh;
  269. int lowcount;
  270. uint32_t freqsent;
  271. uint32_t overheats;
  272. struct timeval lastoverheat;
  273. struct timeval lastrecover;
  274. double overheattime;
  275. uint32_t tempsent;
  276. uint32_t idle;
  277. uint32_t last_rpt_idle;
  278. struct timeval idle_rpt;
  279. };
  280. #define ENABLE_CORE(_core, _n) ((_core[_n >> 3]) |= (1 << (_n % 8)))
  281. #define CORE_IDLE(_core, _n) ((_core[_n >> 3]) & (1 << (_n % 8)))
  282. #define FIFO_RES(_fifo, _off) ((_fifo)[(_off) + 0])
  283. #define FIFO_CMD(_fifo, _off) ((_fifo)[(_off) + 1])
  284. #define RES_GOLD(_res) ((((_res)->status[3]) & 0x80) == 0)
  285. #define RES_CHIP(_res) (((_res)->status[3]) & 0x1f)
  286. #define RES_CORE(_res) ((_res)->status[2])
  287. #define RES_TASK(_res) ((int)((_res)->status[1]) * 0x100 + (int)((_res)->status[0]))
  288. #define RES_NONCE(_res) u8tou32((_res)->nonce, 0)
  289. /*
  290. * This is only valid since we avoid using task_id 0 for work
  291. * However, it isn't really necessary since we only request
  292. * the number of results the result buffer says it has
  293. * However, it is a simple failsafe
  294. */
  295. #define IS_RESULT(_res) ((_res)->status[1] || (_res)->status[0])
  296. struct minion_result {
  297. uint8_t status[DATA_SIZ];
  298. uint8_t nonce[DATA_SIZ];
  299. };
  300. #define MINION_RES_DATA_SIZ sizeof(struct minion_result)
  301. /*
  302. * (MINION_SPI_BUFSIZ - HSIZE()) / MINION_RES_DATA_SIZ
  303. * less a little bit to round it out
  304. */
  305. #define MINION_MAX_RES 120
  306. #define MIDSTATE_BYTES 32
  307. #define MERKLE7_OFFSET 64
  308. #define MERKLE_BYTES 12
  309. #define MINION_MAX_TASK_ID 0xffff
  310. struct minion_que {
  311. uint8_t task_id[2];
  312. uint8_t reserved[2];
  313. uint8_t midstate[MIDSTATE_BYTES];
  314. uint8_t merkle7[DATA_SIZ];
  315. uint8_t ntime[DATA_SIZ];
  316. uint8_t bits[DATA_SIZ];
  317. };
  318. /*
  319. * Max time to wait before checking the task list
  320. * Required, since only urgent tasks trigger an immediate check
  321. * TODO: ? for 2TH/s
  322. */
  323. #define MINION_TASK_mS 8
  324. /*
  325. * Max time to wait before checking the result list for nonces
  326. * This can be long since it's only a failsafe
  327. * cgsem_post is always sent if there are nonces ready to check
  328. */
  329. #define MINION_NONCE_mS 888
  330. // Number of results to make a GPIO interrupt
  331. //#define MINION_RESULT_INT_SIZE 1
  332. #define MINION_RESULT_INT_SIZE 2
  333. /*
  334. * Max time to wait before checking for results
  335. * The interrupt doesn't occur until MINION_RESULT_INT_SIZE results are found
  336. * See comment in minion_spi_reply() at poll()
  337. */
  338. #define MINION_REPLY_mS 88
  339. /*
  340. * Max time to wait before returning the amount of work done
  341. * A result interrupt will send a trigger for this also
  342. * See comment in minion_scanwork()
  343. * This avoids the cgminer master work loop spinning doing nothing
  344. */
  345. #define MINION_SCAN_mS 88
  346. #define ALLOC_WITEMS 4096
  347. #define LIMIT_WITEMS 0
  348. typedef struct witem {
  349. struct work *work;
  350. uint32_t task_id;
  351. struct timeval sent;
  352. int nonces;
  353. bool urgent;
  354. bool stale; // if stale, don't decrement que/chip/realwork when discarded
  355. bool rolled;
  356. int errors; // uncertain since the error could mean task_id is wrong
  357. } WITEM;
  358. #define ALLOC_TITEMS 256
  359. #define LIMIT_TITEMS 0
  360. typedef struct titem {
  361. uint64_t tid;
  362. uint8_t chip;
  363. bool write;
  364. uint8_t address;
  365. uint32_t task_id;
  366. uint32_t wsiz;
  367. uint32_t osiz;
  368. uint32_t rsiz;
  369. uint8_t wbuf[MINION_BUFSIZ];
  370. uint8_t obuf[MINION_BUFSIZ];
  371. uint8_t rbuf[MINION_BUFSIZ];
  372. int reply;
  373. bool urgent;
  374. uint8_t work_state;
  375. struct work *work;
  376. K_ITEM *witem;
  377. } TITEM;
  378. #define ALLOC_RITEMS 256
  379. #define LIMIT_RITEMS 0
  380. typedef struct ritem {
  381. int chip;
  382. int core;
  383. uint32_t task_id;
  384. uint32_t nonce;
  385. struct timeval when;
  386. /*
  387. * Only once per task_id if no nonces were found
  388. * Sent with core = 0
  389. * However, currently it always sends it at the end of every task
  390. * TODO: code assumes it doesn't - change later when we
  391. * see what the final hardware does (minor code performance gain)
  392. */
  393. bool no_nonce;
  394. // If we requested the result twice:
  395. bool another;
  396. uint32_t task_id2;
  397. uint32_t nonce2;
  398. } RITEM;
  399. #define ALLOC_HITEMS 4096
  400. #define LIMIT_HITEMS 0
  401. // How much history to keep (5min)
  402. #define MINION_HISTORY_s 300
  403. // History must be always generated for the reset check
  404. #define MINION_MAX_RESET_CHECK 2
  405. typedef struct hitem {
  406. struct timeval when;
  407. } HITEM;
  408. #define DATAW(_item) ((WITEM *)(_item->data))
  409. #define DATAT(_item) ((TITEM *)(_item->data))
  410. #define DATAR(_item) ((RITEM *)(_item->data))
  411. #define DATAH(_item) ((HITEM *)(_item->data))
  412. // Set this to 1 to enable iostats processing
  413. // N.B. it slows down mining
  414. #define DO_IO_STATS 0
  415. #if DO_IO_STATS
  416. #define IO_STAT_NOW(_tv) cgtime(_tv)
  417. #define IO_STAT_STORE(_sta, _fin, _lsta, _lfin, _tsd, _buf, _siz, _reply, _ioc) \
  418. do { \
  419. double _diff, _ldiff, _lwdiff, _1time; \
  420. int _off; \
  421. _diff = us_tdiff(_fin, _sta); \
  422. _ldiff = us_tdiff(_lfin, _lsta); \
  423. _lwdiff = us_tdiff(_sta, _lsta); \
  424. _1time = us_tdiff(_tsd, _lfin); \
  425. _off = (int)(_buf[1]) + (_reply >= 0 ? 0 : 0x100); \
  426. minioninfo->summary.count++; \
  427. minioninfo->summary.tsd += _1time; \
  428. minioninfo->iostats[_off].count++; \
  429. minioninfo->iostats[_off].tsd += _1time; \
  430. if (_diff <= 0) { \
  431. minioninfo->summary.zero_delay++; \
  432. minioninfo->iostats[_off].zero_delay++; \
  433. } else { \
  434. minioninfo->summary.total_delay += _diff; \
  435. if (minioninfo->summary.max_delay < _diff) \
  436. minioninfo->summary.max_delay = _diff; \
  437. if (minioninfo->summary.min_delay == 0 || \
  438. minioninfo->summary.min_delay > _diff) \
  439. minioninfo->summary.min_delay = _diff; \
  440. minioninfo->iostats[_off].total_delay += _diff; \
  441. if (minioninfo->iostats[_off].max_delay < _diff) \
  442. minioninfo->iostats[_off].max_delay = _diff; \
  443. if (minioninfo->iostats[_off].min_delay == 0 || \
  444. minioninfo->iostats[_off].min_delay > _diff) \
  445. minioninfo->iostats[_off].min_delay = _diff; \
  446. } \
  447. if (_ldiff <= 0) { \
  448. minioninfo->summary.zero_dlock++; \
  449. minioninfo->iostats[_off].zero_dlock++; \
  450. } else { \
  451. minioninfo->summary.total_dlock += _ldiff; \
  452. if (minioninfo->summary.max_dlock < _ldiff) \
  453. minioninfo->summary.max_dlock = _ldiff; \
  454. if (minioninfo->summary.min_dlock == 0 || \
  455. minioninfo->summary.min_dlock > _ldiff) \
  456. minioninfo->summary.min_dlock = _ldiff; \
  457. minioninfo->iostats[_off].total_dlock += _ldiff; \
  458. if (minioninfo->iostats[_off].max_dlock < _ldiff) \
  459. minioninfo->iostats[_off].max_dlock = _ldiff; \
  460. if (minioninfo->iostats[_off].min_dlock == 0 || \
  461. minioninfo->iostats[_off].min_dlock > _ldiff) \
  462. minioninfo->iostats[_off].min_dlock = _ldiff; \
  463. } \
  464. minioninfo->summary.total_dlwait += _lwdiff; \
  465. minioninfo->iostats[_off].total_dlwait += _lwdiff; \
  466. if (_siz == 0) { \
  467. minioninfo->summary.zero_bytes++; \
  468. minioninfo->iostats[_off].zero_bytes++; \
  469. } else { \
  470. minioninfo->summary.total_bytes += _siz; \
  471. if (minioninfo->summary.max_bytes < _siz) \
  472. minioninfo->summary.max_bytes = _siz; \
  473. if (minioninfo->summary.min_bytes == 0 || \
  474. minioninfo->summary.min_bytes > _siz) \
  475. minioninfo->summary.min_bytes = _siz; \
  476. minioninfo->iostats[_off].total_bytes += _siz; \
  477. if (minioninfo->iostats[_off].max_bytes < _siz) \
  478. minioninfo->iostats[_off].max_bytes = _siz; \
  479. if (minioninfo->iostats[_off].min_bytes == 0 || \
  480. minioninfo->iostats[_off].min_bytes > _siz) \
  481. minioninfo->iostats[_off].min_bytes = _siz; \
  482. } \
  483. } while (0);
  484. typedef struct iostat {
  485. uint64_t count; // total ioctl()
  486. double total_delay; // total elapsed ioctl()
  487. double min_delay;
  488. double max_delay;
  489. uint64_t zero_delay; // how many had <= 0 delay
  490. // Above but including locking
  491. double total_dlock;
  492. double min_dlock;
  493. double max_dlock;
  494. uint64_t zero_dlock;
  495. // Total time waiting to get lock
  496. double total_dlwait;
  497. // these 3 fields are ignored for now since all are '1'
  498. uint64_t total_ioc; // SPI_IOC_MESSAGE(x)
  499. uint64_t min_ioc;
  500. uint64_t max_ioc;
  501. uint64_t total_bytes; // ioctl() bytes
  502. uint64_t min_bytes;
  503. uint64_t max_bytes;
  504. uint64_t zero_bytes; // how many had siz == 0
  505. double tsd; // total doing one extra cgtime() each time
  506. } IOSTAT;
  507. #else
  508. #define IO_STAT_NOW(_tv)
  509. #define IO_STAT_STORE(_sta, _fin, _lsta, _lfin, _tsd, _buf, _siz, _reply, _ioc)
  510. #endif
  511. struct minion_info {
  512. struct thr_info spiw_thr;
  513. struct thr_info spir_thr;
  514. struct thr_info res_thr;
  515. pthread_mutex_t spi_lock;
  516. pthread_mutex_t sta_lock;
  517. cgsem_t task_ready;
  518. cgsem_t nonce_ready;
  519. cgsem_t scan_work;
  520. int spifd;
  521. char gpiointvalue[64];
  522. int gpiointfd;
  523. // TODO: need to track disabled chips - done?
  524. int chips;
  525. bool chip[MINION_CHIPS];
  526. int init_freq[MINION_CHIPS];
  527. int init_temp[MINION_CHIPS];
  528. uint8_t init_cores[MINION_CHIPS][DATA_SIZ*MINION_CORE_REPS];
  529. uint32_t next_task_id;
  530. // Stats
  531. uint64_t chip_nonces[MINION_CHIPS];
  532. uint64_t chip_nononces[MINION_CHIPS];
  533. uint64_t chip_good[MINION_CHIPS];
  534. uint64_t chip_bad[MINION_CHIPS];
  535. uint64_t chip_err[MINION_CHIPS];
  536. uint64_t core_good[MINION_CHIPS][MINION_CORES+1];
  537. uint64_t core_bad[MINION_CHIPS][MINION_CORES+1];
  538. uint32_t chip_core_ena[MINION_CORE_REPS][MINION_CHIPS];
  539. uint32_t chip_core_act[MINION_CORE_REPS][MINION_CHIPS];
  540. struct minion_status chip_status[MINION_CHIPS];
  541. uint64_t interrupts;
  542. uint64_t result_interrupts;
  543. uint64_t command_interrupts;
  544. char last_interrupt[64];
  545. pthread_mutex_t nonce_lock;
  546. uint64_t new_nonces;
  547. uint64_t ok_nonces;
  548. uint64_t untested_nonces;
  549. uint64_t tested_nonces;
  550. uint64_t work_unrolled;
  551. uint64_t work_rolled;
  552. uint64_t spi_errors;
  553. uint64_t fifo_spi_errors[MINION_CHIPS];
  554. uint64_t res_spi_errors[MINION_CHIPS];
  555. uint64_t use_res2[MINION_CHIPS];
  556. uint64_t tasks_failed[MINION_CHIPS];
  557. uint64_t tasks_recovered[MINION_CHIPS];
  558. uint64_t nonces_failed[MINION_CHIPS];
  559. uint64_t nonces_recovered[MINION_CHIPS];
  560. struct timeval last_reset[MINION_CHIPS];
  561. double do_reset[MINION_CHIPS];
  562. // Work items
  563. K_LIST *wfree_list;
  564. K_STORE *wwork_list;
  565. K_STORE *wque_list[MINION_CHIPS];
  566. K_STORE *wchip_list[MINION_CHIPS];
  567. // Task list
  568. K_LIST *tfree_list;
  569. K_STORE *task_list;
  570. K_STORE *treply_list;
  571. uint64_t next_tid;
  572. // Nonce replies
  573. K_LIST *rfree_list;
  574. K_STORE *rnonce_list;
  575. struct timeval last_did;
  576. // Nonce history
  577. K_LIST *hfree_list;
  578. K_STORE *hchip_list[MINION_CHIPS];
  579. int history_gen;
  580. struct timeval chip_chk;
  581. struct timeval chip_rpt;
  582. double history_ghs[MINION_CHIPS];
  583. // Gets reset to zero each time it is used in reporting
  584. int res_err_count[MINION_CHIPS];
  585. #if DO_IO_STATS
  586. // Total
  587. IOSTAT summary;
  588. // Two for each command plus wasted extras i.e. direct/fast lookup
  589. // No error uses 0x0 to 0xff, error uses 0x100 to 0x1ff
  590. IOSTAT iostats[0x200];
  591. #endif
  592. bool initialised;
  593. };
  594. static void ready_work(struct cgpu_info *minioncgpu, struct work *work, bool rolled)
  595. {
  596. struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
  597. K_ITEM *item = NULL;
  598. K_WLOCK(minioninfo->wfree_list);
  599. item = k_unlink_head(minioninfo->wfree_list);
  600. DATAW(item)->work = work;
  601. DATAW(item)->task_id = 0;
  602. memset(&(DATAW(item)->sent), 0, sizeof(DATAW(item)->sent));
  603. DATAW(item)->nonces = 0;
  604. DATAW(item)->urgent = false;
  605. DATAW(item)->rolled = rolled;
  606. DATAW(item)->errors = 0;
  607. k_add_head(minioninfo->wwork_list, item);
  608. K_WUNLOCK(minioninfo->wfree_list);
  609. }
  610. static bool oldest_nonce(struct cgpu_info *minioncgpu, int *chip, int *core, uint32_t *task_id,
  611. uint32_t *nonce, bool *no_nonce, struct timeval *when,
  612. bool *another, uint32_t *task_id2, uint32_t *nonce2)
  613. {
  614. struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
  615. K_ITEM *item = NULL;
  616. bool found = false;
  617. K_WLOCK(minioninfo->rnonce_list);
  618. item = k_unlink_tail(minioninfo->rnonce_list);
  619. if (item) {
  620. found = true;
  621. *chip = DATAR(item)->chip;
  622. *core = DATAR(item)->core;
  623. *task_id = DATAR(item)->task_id;
  624. *nonce = DATAR(item)->nonce;
  625. *no_nonce = DATAR(item)->no_nonce;
  626. memcpy(when, &(DATAR(item)->when), sizeof(*when));
  627. *another = DATAR(item)->another;
  628. *task_id2 = DATAR(item)->task_id2;
  629. *nonce2 = DATAR(item)->nonce2;
  630. k_free_head(minioninfo->rfree_list, item);
  631. }
  632. K_WUNLOCK(minioninfo->rnonce_list);
  633. return found;
  634. }
  635. static const char *addr2txt(uint8_t addr)
  636. {
  637. switch (addr) {
  638. case READ_ADDR(MINION_SYS_CHIP_SIG):
  639. return "RChipSig";
  640. case READ_ADDR(MINION_SYS_CHIP_STA):
  641. return "RChipSta";
  642. case WRITE_ADDR(MINION_SYS_MISC_CTL):
  643. return "WMiscCtrl";
  644. case WRITE_ADDR(MINION_SYS_RSTN_CTL):
  645. return "WResetCtrl";
  646. case READ_ADDR(MINION_SYS_FIFO_STA):
  647. return "RFifoSta";
  648. case READ_ADDR(MINION_CORE_ENA0_31):
  649. return "RCoreEna0-31";
  650. case WRITE_ADDR(MINION_CORE_ENA0_31):
  651. return "WCoreEna0-31";
  652. case READ_ADDR(MINION_CORE_ENA32_63):
  653. return "RCoreEna32-63";
  654. case WRITE_ADDR(MINION_CORE_ENA32_63):
  655. return "WCoreEna32-63";
  656. case READ_ADDR(MINION_CORE_ENA64_95):
  657. return "RCoreEna64-95";
  658. case WRITE_ADDR(MINION_CORE_ENA64_95):
  659. return "WCoreEna64-95";
  660. case READ_ADDR(MINION_CORE_ENA96_98):
  661. return "RCoreEna96-98";
  662. case WRITE_ADDR(MINION_CORE_ENA96_98):
  663. return "WCoreEna96-98";
  664. case READ_ADDR(MINION_CORE_ACT0_31):
  665. return "RCoreAct0-31";
  666. case READ_ADDR(MINION_CORE_ACT32_63):
  667. return "RCoreAct32-63";
  668. case READ_ADDR(MINION_CORE_ACT64_95):
  669. return "RCoreAct64-95";
  670. case READ_ADDR(MINION_CORE_ACT96_98):
  671. return "RCoreAct96-98";
  672. case READ_ADDR(MINION_RES_DATA):
  673. return "RResData";
  674. case READ_ADDR(MINION_RES_PEEK):
  675. return "RResPeek";
  676. case WRITE_ADDR(MINION_QUE_0):
  677. return "WQueWork";
  678. case READ_ADDR(MINION_NONCE_START):
  679. return "RNonceStart";
  680. case WRITE_ADDR(MINION_NONCE_START):
  681. return "WNonceStart";
  682. case READ_ADDR(MINION_NONCE_RANGE):
  683. return "RNonceRange";
  684. case WRITE_ADDR(MINION_NONCE_RANGE):
  685. return "WNonceRange";
  686. case READ_ADDR(MINION_SYS_INT_STA):
  687. return "RIntSta";
  688. case WRITE_ADDR(MINION_SYS_INT_ENA):
  689. return "WIntEna";
  690. case WRITE_ADDR(MINION_SYS_INT_CLR):
  691. return "WIntClear";
  692. case WRITE_ADDR(MINION_SYS_BUF_TRIG):
  693. return "WResTrigger";
  694. case WRITE_ADDR(MINION_SYS_QUE_TRIG):
  695. return "WCmdTrigger";
  696. case READ_ADDR(MINION_SYS_TEMP_CTL):
  697. return "RTempCtrl";
  698. case WRITE_ADDR(MINION_SYS_TEMP_CTL):
  699. return "WTempCtrl";
  700. case READ_ADDR(MINION_SYS_FREQ_CTL):
  701. return "RFreqCtrl";
  702. case WRITE_ADDR(MINION_SYS_FREQ_CTL):
  703. return "WFreqCtrl";
  704. case READ_ADDR(MINION_SYS_IDLE_CNT):
  705. return "RIdleCnt";
  706. }
  707. // gcc warning if this is in default:
  708. if (IS_ADDR_READ(addr))
  709. return "RUnhandled";
  710. else
  711. return "WUnhandled";
  712. }
  713. // For display_ioctl()
  714. #define IOCTRL_LOG LOG_WARNING
  715. // For all other debug so it can easily be switched always on
  716. #define MINION_LOG LOG_DEBUG
  717. // For task corruption logging
  718. #define MINTASK_LOG LOG_DEBUG
  719. // Set to 1 for debug
  720. #define MINION_SHOW_IO 0
  721. #define DATA_ALL 2048
  722. #define DATA_OFF 512
  723. #if MINION_SHOW_IO
  724. static void display_ioctl(int reply, uint32_t osiz, uint8_t *obuf, uint32_t rsiz, uint8_t *rbuf)
  725. {
  726. struct minion_result *res;
  727. const char *name, *dir, *ex;
  728. char buf[4096];
  729. int i, rescount;
  730. name = addr2txt(obuf[1]);
  731. if (IS_ADDR_READ(obuf[1]))
  732. dir = "from";
  733. else
  734. dir = "to";
  735. buf[0] = '\0';
  736. ex = "";
  737. switch (obuf[1]) {
  738. case READ_ADDR(MINION_SYS_CHIP_SIG):
  739. case READ_ADDR(MINION_SYS_CHIP_STA):
  740. break;
  741. case WRITE_ADDR(MINION_SYS_MISC_CTL):
  742. case WRITE_ADDR(MINION_SYS_RSTN_CTL):
  743. if (osiz > HSIZE()) {
  744. ex = " wrote ";
  745. __bin2hex(buf, obuf + HSIZE(), osiz - HSIZE());
  746. } else
  747. ex = " wrote nothing";
  748. break;
  749. default:
  750. if (IS_ADDR_WRITE(obuf[1])) {
  751. if (osiz > HSIZE()) {
  752. ex = " wrote ";
  753. __bin2hex(buf, obuf + HSIZE(), osiz - HSIZE());
  754. } else
  755. ex = " wrote nothing";
  756. }
  757. break;
  758. }
  759. if (reply < 0) {
  760. applog(IOCTRL_LOG, "%s %s chip %d osiz %d%s%s",
  761. name, dir, (int)obuf[0], (int)osiz, ex, buf);
  762. applog(IOCTRL_LOG, " reply was error %d", reply);
  763. } else {
  764. if (IS_ADDR_WRITE(obuf[1])) {
  765. applog(IOCTRL_LOG, "%s %s chip %d osiz %d%s%s",
  766. name, dir, (int)obuf[0], (int)osiz, ex, buf);
  767. applog(IOCTRL_LOG, " write ret was %d", reply);
  768. } else {
  769. switch (obuf[1]) {
  770. case READ_ADDR(MINION_RES_DATA):
  771. rescount = (int)((float)rsiz / (float)MINION_RES_DATA_SIZ);
  772. applog(IOCTRL_LOG, "%s %s chip %d osiz %d%s%s",
  773. name, dir, (int)obuf[0], (int)osiz, ex, buf);
  774. for (i = 0; i < rescount; i++) {
  775. res = (struct minion_result *)(rbuf + osiz - rsiz + (i * MINION_RES_DATA_SIZ));
  776. if (!IS_RESULT(res)) {
  777. applog(IOCTRL_LOG, " %s reply %d of %d - none", name, i+1, rescount);
  778. } else {
  779. __bin2hex(buf, res->nonce, DATA_SIZ);
  780. applog(IOCTRL_LOG, " %s reply %d of %d %d(%d) was task 0x%04x"
  781. " chip %d core %d gold %s nonce 0x%s",
  782. name, i+1, rescount, reply, rsiz,
  783. RES_TASK(res),
  784. (int)RES_CHIP(res),
  785. (int)RES_CORE(res),
  786. (int)RES_GOLD(res) ? "Y" : "N",
  787. buf);
  788. }
  789. }
  790. break;
  791. case READ_ADDR(MINION_SYS_CHIP_SIG):
  792. case READ_ADDR(MINION_SYS_CHIP_STA):
  793. default:
  794. applog(IOCTRL_LOG, "%s %s chip %d osiz %d%s%s",
  795. name, dir, (int)obuf[0], (int)osiz, ex, buf);
  796. __bin2hex(buf, rbuf + osiz - rsiz, rsiz);
  797. applog(IOCTRL_LOG, " %s reply %d(%d) was %s", name, reply, rsiz, buf);
  798. break;
  799. }
  800. }
  801. }
  802. }
  803. #endif
  804. #define MINION_UNEXPECTED_TASK -999
  805. #define MINION_OVERSIZE_TASK -998
  806. static int __do_ioctl(struct minion_info *minioninfo, uint8_t *obuf, uint32_t osiz, uint8_t *rbuf, uint32_t rsiz, MINION_FFL_ARGS)
  807. {
  808. struct spi_ioc_transfer tran;
  809. int ret;
  810. #if MINION_SHOW_IO
  811. char dataw[DATA_ALL], datar[DATA_ALL];
  812. #endif
  813. #if DO_IO_STATS
  814. struct timeval sta, fin, lsta, lfin, tsd;
  815. #endif
  816. if ((int)osiz > MINION_BUFSIZ)
  817. quitfrom(1, file, func, line, "%s() invalid osiz %u > %d (chip=%d reg=0x%02x)",
  818. __func__, osiz, MINION_BUFSIZ, (int)(obuf[0]), obuf[1]);
  819. if (rsiz >= osiz)
  820. quitfrom(1, file, func, line, "%s() invalid rsiz %u >= osiz %u (chip=%u reg=0x%02x)",
  821. __func__, rsiz, osiz, (int)(obuf[0]), obuf[1]);
  822. memset(&obuf[0] + osiz - rsiz, 0xff, rsiz);
  823. #if MINION_SHOW_IO
  824. // if the a5/5a outside the data change, it means data overrun or corruption
  825. memset(dataw, 0xa5, sizeof(dataw));
  826. memset(datar, 0x5a, sizeof(datar));
  827. memcpy(&dataw[DATA_OFF], &obuf[0], osiz);
  828. char *buf = bin2hex((unsigned char *)&(dataw[DATA_OFF]), osiz);
  829. applog(IOCTRL_LOG, "*** %s() sending %02x %02x %s %02x %02x",
  830. __func__,
  831. dataw[0], dataw[DATA_OFF-1], buf,
  832. dataw[DATA_OFF+osiz], dataw[DATA_ALL-1]);
  833. free(buf);
  834. #endif
  835. memset((char *)rbuf, 0x00, osiz);
  836. // cgsleep_ms(5); // TODO: a delay ... based on the last command? But subtract elapsed
  837. // i.e. do any commands need a delay after the I/O has completed before the next I/O?
  838. memset(&tran, 0, sizeof(tran));
  839. if (osiz < MINION_SPI_BUFSIZ)
  840. tran.len = osiz;
  841. else
  842. return MINION_OVERSIZE_TASK;
  843. tran.delay_usecs = 0;
  844. tran.speed_hz = MINION_SPI_SPEED;
  845. #if MINION_SHOW_IO
  846. tran.tx_buf = (uintptr_t)&(dataw[DATA_OFF]);
  847. tran.rx_buf = (uintptr_t)&(datar[DATA_OFF]);
  848. #else
  849. tran.tx_buf = (uintptr_t)obuf;
  850. tran.rx_buf = (uintptr_t)rbuf;
  851. #endif
  852. IO_STAT_NOW(&lsta);
  853. mutex_lock(&(minioninfo->spi_lock));
  854. IO_STAT_NOW(&sta);
  855. ret = ioctl(minioninfo->spifd, SPI_IOC_MESSAGE(1), (void *)&tran);
  856. IO_STAT_NOW(&fin);
  857. mutex_unlock(&(minioninfo->spi_lock));
  858. IO_STAT_NOW(&lfin);
  859. IO_STAT_NOW(&tsd);
  860. IO_STAT_STORE(&sta, &fin, &lsta, &lfin, &tsd, obuf, osiz, ret, 1);
  861. #if MINION_SHOW_IO
  862. if (ret > 0) {
  863. buf = bin2hex((unsigned char *)&(datar[DATA_OFF]), ret);
  864. applog(IOCTRL_LOG, "*** %s() reply %d = %02x %02x %s %02x %02x",
  865. __func__, ret,
  866. datar[0], datar[DATA_OFF-1], buf,
  867. datar[DATA_OFF+osiz], datar[DATA_ALL-1]);
  868. free(buf);
  869. } else
  870. applog(LOG_ERR, "*** %s() reply = %d", __func__, ret);
  871. memcpy(&rbuf[0], &datar[DATA_OFF], osiz);
  872. display_ioctl(ret, osiz, (uint8_t *)(&dataw[DATA_OFF]), rsiz, (uint8_t *)(&datar[DATA_OFF]));
  873. #endif
  874. return ret;
  875. }
  876. #if 1
  877. #define do_ioctl(_obuf, _osiz, _rbuf, _rsiz) __do_ioctl(minioninfo, _obuf, _osiz, _rbuf, _rsiz, MINION_FFL_HERE)
  878. #else
  879. #define do_ioctl(_obuf, _osiz, _rbuf, _rsiz) _do_ioctl(minioninfo, _obuf, _osiz, _rbuf, _rsiz, MINION_FFL_HERE)
  880. // This sends an expected to work, SPI command before each SPI command
  881. static int _do_ioctl(struct minion_info *minioninfo, uint8_t *obuf, uint32_t osiz, uint8_t *rbuf, uint32_t rsiz, MINION_FFL_ARGS)
  882. {
  883. struct minion_header *head;
  884. uint8_t buf1[MINION_BUFSIZ];
  885. uint8_t buf2[MINION_BUFSIZ];
  886. uint32_t siz;
  887. head = (struct minion_header *)buf1;
  888. head->chip = 1; // Needs to be set to a valid chip
  889. head->reg = READ_ADDR(MINION_SYS_FIFO_STA);
  890. SET_HEAD_SIZ(head, DATA_SIZ);
  891. siz = HSIZE() + DATA_SIZ;
  892. __do_ioctl(minioninfo, buf1, siz, buf2, MINION_CORE_SIZ, MINION_FFL_PASS);
  893. return __do_ioctl(minioninfo, obuf, osiz, rbuf, rsiz, MINION_FFL_PASS);
  894. }
  895. #endif
  896. static bool _minion_txrx(struct cgpu_info *minioncgpu, struct minion_info *minioninfo, TITEM *task, MINION_FFL_ARGS)
  897. {
  898. struct minion_header *head;
  899. head = (struct minion_header *)(task->obuf);
  900. head->chip = task->chip;
  901. if (task->write)
  902. SET_HEAD_WRITE(head, task->address);
  903. else
  904. SET_HEAD_READ(head, task->address);
  905. SET_HEAD_SIZ(head, task->wsiz + task->rsiz);
  906. if (task->wsiz)
  907. memcpy(&(head->data[0]), task->wbuf, task->wsiz);
  908. task->osiz = HSIZE() + task->wsiz + task->rsiz;
  909. task->reply = do_ioctl(task->obuf, task->osiz, task->rbuf, task->rsiz);
  910. if (task->reply < 0) {
  911. applog(LOG_ERR, "%s%d: chip=%d ioctl failed reply=%d err=%d" MINION_FFL,
  912. minioncgpu->drv->name, minioncgpu->device_id,
  913. task->chip, task->reply, errno, MINION_FFL_PASS);
  914. } else if (task->reply < (int)(task->osiz)) {
  915. applog(LOG_ERR, "%s%d: chip=%d ioctl failed to write %d only wrote %d (err=%d)" MINION_FFL,
  916. minioncgpu->drv->name, minioncgpu->device_id,
  917. task->chip, (int)(task->osiz), task->reply, errno, MINION_FFL_PASS);
  918. }
  919. return (task->reply >= (int)(task->osiz));
  920. }
  921. // Only for DATA_SIZ commands
  922. static int build_cmd(struct cgpu_info *minioncgpu, struct minion_info *minioninfo, int chip, uint8_t reg, uint8_t *rbuf, uint32_t rsiz, uint8_t *data)
  923. {
  924. struct minion_header *head;
  925. uint8_t wbuf[MINION_BUFSIZ];
  926. uint32_t wsiz;
  927. int reply;
  928. head = (struct minion_header *)wbuf;
  929. head->chip = chip;
  930. head->reg = reg;
  931. SET_HEAD_SIZ(head, DATA_SIZ);
  932. head->data[0] = data[0];
  933. head->data[1] = data[1];
  934. head->data[2] = data[2];
  935. head->data[3] = data[3];
  936. wsiz = HSIZE() + DATA_SIZ;
  937. reply = do_ioctl(wbuf, wsiz, rbuf, rsiz);
  938. if (reply != (int)wsiz) {
  939. applog(LOG_ERR, "%s: chip %d %s returned %d (should be %d)",
  940. minioncgpu->drv->dname, chip,
  941. addr2txt(head->reg),
  942. reply, (int)wsiz);
  943. }
  944. return reply;
  945. }
  946. static void init_chip(struct cgpu_info *minioncgpu, struct minion_info *minioninfo, int chip)
  947. {
  948. uint8_t rbuf[MINION_BUFSIZ];
  949. uint8_t data[4];
  950. __maybe_unused int reply;
  951. int choice;
  952. uint32_t freq;
  953. // Complete chip reset
  954. data[0] = 0x00;
  955. data[1] = 0x00;
  956. data[2] = 0xa5;
  957. data[3] = 0xf5;
  958. reply = build_cmd(minioncgpu, minioninfo,
  959. chip, WRITE_ADDR(MINION_SYS_RSTN_CTL),
  960. rbuf, 0, data);
  961. // Default reset
  962. data[0] = SYS_RSTN_CTL_INIT;
  963. data[1] = 0x00;
  964. data[2] = 0x00;
  965. data[3] = 0x00;
  966. reply = build_cmd(minioncgpu, minioninfo,
  967. chip, WRITE_ADDR(MINION_SYS_RSTN_CTL),
  968. rbuf, 0, data);
  969. // Default initialisation
  970. data[0] = SYS_MISC_CTL_DEFAULT;
  971. data[1] = 0x00;
  972. data[2] = 0x00;
  973. data[3] = 0x00;
  974. reply = build_cmd(minioncgpu, minioninfo,
  975. chip, WRITE_ADDR(MINION_SYS_MISC_CTL),
  976. rbuf, 0, data);
  977. // Set chip frequency
  978. choice = minioninfo->init_freq[chip];
  979. if (choice < MINION_FREQ_MIN || choice > MINION_FREQ_MAX)
  980. choice = MINION_FREQ_DEF;
  981. choice /= MINION_FREQ_FACTOR;
  982. if (choice < MINION_FREQ_FACTOR_MIN)
  983. choice = MINION_FREQ_FACTOR_MIN;
  984. if (choice > MINION_FREQ_FACTOR_MAX)
  985. choice = MINION_FREQ_FACTOR_MAX;
  986. freq = minion_freq[choice];
  987. data[0] = (uint8_t)(freq & 0xff);
  988. data[1] = (uint8_t)(((freq & 0xff00) >> 8) & 0xff);
  989. data[2] = (uint8_t)(((freq & 0xff0000) >> 16) & 0xff);
  990. data[3] = (uint8_t)(((freq & 0xff000000) >> 24) & 0xff);
  991. minioninfo->chip_status[chip].freqsent = freq;
  992. reply = build_cmd(minioncgpu, minioninfo,
  993. chip, WRITE_ADDR(MINION_SYS_FREQ_CTL),
  994. rbuf, 0, data);
  995. // Set temp threshold
  996. choice = minioninfo->init_temp[chip];
  997. if (choice == MINION_TEMP_CTL_DISABLE)
  998. choice = MINION_TEMP_CTL_DISABLE_VALUE;
  999. else {
  1000. if (choice < MINION_TEMP_CTL_MIN_VALUE || choice > MINION_TEMP_CTL_MAX_VALUE)
  1001. choice = MINION_TEMP_CTL_DEF;
  1002. choice -= MINION_TEMP_CTL_MIN_VALUE;
  1003. choice /= MINION_TEMP_CTL_STEP;
  1004. choice += MINION_TEMP_CTL_MIN;
  1005. if (choice < MINION_TEMP_CTL_MIN)
  1006. choice = MINION_TEMP_CTL_MIN;
  1007. if (choice > MINION_TEMP_CTL_MAX)
  1008. choice = MINION_TEMP_CTL_MAX;
  1009. }
  1010. data[0] = (uint8_t)choice;
  1011. data[1] = 0;
  1012. data[2] = 0;
  1013. data[3] = 0;
  1014. minioninfo->chip_status[chip].tempsent = choice;
  1015. reply = build_cmd(minioncgpu, minioninfo,
  1016. chip, WRITE_ADDR(MINION_SYS_TEMP_CTL),
  1017. rbuf, 0, data);
  1018. }
  1019. static void enable_chip_cores(struct cgpu_info *minioncgpu, struct minion_info *minioninfo, int chip)
  1020. {
  1021. uint8_t rbuf[MINION_BUFSIZ];
  1022. uint8_t data[4];
  1023. __maybe_unused int reply;
  1024. int rep, i;
  1025. for (i = 0; i < 4; i++)
  1026. data[i] = minioninfo->init_cores[chip][i];
  1027. reply = build_cmd(minioncgpu, minioninfo,
  1028. chip, WRITE_ADDR(MINION_CORE_ENA0_31),
  1029. rbuf, 0, data);
  1030. for (i = 0; i < 4; i++)
  1031. data[i] = minioninfo->init_cores[chip][i+4];
  1032. reply = build_cmd(minioncgpu, minioninfo,
  1033. chip, WRITE_ADDR(MINION_CORE_ENA32_63),
  1034. rbuf, 0, data);
  1035. for (i = 0; i < 4; i++)
  1036. data[i] = minioninfo->init_cores[chip][i+8];
  1037. reply = build_cmd(minioncgpu, minioninfo,
  1038. chip, WRITE_ADDR(MINION_CORE_ENA64_95),
  1039. rbuf, 0, data);
  1040. for (i = 0; i < 4; i++)
  1041. data[i] = minioninfo->init_cores[chip][i+12];
  1042. reply = build_cmd(minioncgpu, minioninfo,
  1043. chip, WRITE_ADDR(MINION_CORE_ENA96_98),
  1044. rbuf, 0, data);
  1045. /* Below is for testing - disabled/use default
  1046. // 1/3 range for each of the 3 cores
  1047. // data[0] = 0x55;
  1048. // data[1] = 0x55;
  1049. // data[2] = 0x55;
  1050. // data[3] = 0x55;
  1051. // quicker replies
  1052. // data[0] = 0x05;
  1053. // data[1] = 0x05;
  1054. // data[2] = 0x05;
  1055. // data[3] = 0x05;
  1056. // 0x00000100 at 20MH/s per core = 336TH/s if 1 nonce per work item
  1057. // 0x00001000 = 21.0TH/s - so well above 2TH/s
  1058. // 0x00002000 = 10.5TH/s - above 2TH/s
  1059. // speed test
  1060. data[0] = 0x00;
  1061. data[1] = 0x01;
  1062. data[2] = 0x00;
  1063. data[3] = 0x00;
  1064. // data[3] = 0x20; // slow it down for other testing
  1065. // 2 cores
  1066. // data[0] = 0xff;
  1067. // data[1] = 0xff;
  1068. // data[2] = 0xff;
  1069. // data[3] = 0x7f;
  1070. reply = build_cmd(minioncgpu, minioninfo,
  1071. chip, WRITE_ADDR(MINION_NONCE_RANGE),
  1072. rbuf, 0, data);
  1073. // find lots more nonces in a short time on my test data
  1074. // i.e. emulate a MUCH higher hash rate on SPI and work
  1075. // generation/testing
  1076. // Current test data (same repeated 10 times) has nonce 0x05e0ed6d
  1077. data[0] = 0x00;
  1078. data[1] = 0xed;
  1079. data[2] = 0xe0;
  1080. data[3] = 0x05;
  1081. reply = build_cmd(minioncgpu, minioninfo,
  1082. chip, WRITE_ADDR(MINION_NONCE_START),
  1083. rbuf, 0, data);
  1084. */
  1085. // store the core ena state
  1086. for (rep = 0; rep < MINION_CORE_REPS; rep++) {
  1087. data[0] = 0x0;
  1088. data[1] = 0x0;
  1089. data[2] = 0x0;
  1090. data[3] = 0x0;
  1091. reply = build_cmd(minioncgpu, minioninfo,
  1092. chip, READ_ADDR(MINION_CORE_ENA0_31 + rep),
  1093. rbuf, MINION_CORE_SIZ, data);
  1094. minioninfo->chip_core_ena[rep][chip] = *((uint32_t *)&(rbuf[HSIZE()]));
  1095. }
  1096. // store the core active state
  1097. for (rep = 0; rep < MINION_CORE_REPS; rep++) {
  1098. data[0] = 0x0;
  1099. data[1] = 0x0;
  1100. data[2] = 0x0;
  1101. data[3] = 0x0;
  1102. reply = build_cmd(minioncgpu, minioninfo,
  1103. chip, READ_ADDR(MINION_CORE_ACT0_31 + rep),
  1104. rbuf, MINION_CORE_SIZ, data);
  1105. minioninfo->chip_core_act[rep][chip] = *((uint32_t *)&(rbuf[HSIZE()]));
  1106. }
  1107. }
  1108. #if ENABLE_INT_NONO
  1109. static void enable_interrupt(struct cgpu_info *minioncgpu, struct minion_info *minioninfo, int chip)
  1110. {
  1111. uint8_t rbuf[MINION_BUFSIZ];
  1112. uint8_t data[4];
  1113. __maybe_unused int reply;
  1114. data[0] = MINION_RESULT_INT_SIZE;
  1115. data[1] = 0x00;
  1116. data[2] = 0x00;
  1117. data[3] = 0x00;
  1118. reply = build_cmd(minioncgpu, minioninfo,
  1119. chip, WRITE_ADDR(MINION_SYS_BUF_TRIG),
  1120. rbuf, 0, data);
  1121. // data[0] = MINION_QUE_MAX; // spaces available ... i.e. empty
  1122. // data[0] = MINION_QUE_LOW; // spaces in use
  1123. data[0] = MINION_QUE_MAX - MINION_QUE_LOW; // spaces available
  1124. data[1] = 0x00;
  1125. data[2] = 0x00;
  1126. data[3] = 0x00;
  1127. reply = build_cmd(minioncgpu, minioninfo,
  1128. chip, WRITE_ADDR(MINION_SYS_QUE_TRIG),
  1129. rbuf, 0, data);
  1130. // data[0] = MINION_RESULT_INT;
  1131. data[0] = MINION_RESULT_INT | MINION_CMD_INT;
  1132. data[1] = 0x00;
  1133. data[2] = 0x00;
  1134. data[3] = 0x00;
  1135. reply = build_cmd(minioncgpu, minioninfo,
  1136. chip, WRITE_ADDR(MINION_SYS_INT_ENA),
  1137. rbuf, 0, data);
  1138. }
  1139. #endif
  1140. // Simple detect - just check each chip for the signature
  1141. static void minion_detect_chips(struct cgpu_info *minioncgpu, struct minion_info *minioninfo)
  1142. {
  1143. struct minion_header *head;
  1144. uint8_t wbuf[MINION_BUFSIZ];
  1145. uint8_t rbuf[MINION_BUFSIZ];
  1146. uint32_t wsiz, rsiz;
  1147. int chip, reply, tries;
  1148. bool ok;
  1149. head = (struct minion_header *)wbuf;
  1150. rsiz = MINION_SYS_SIZ;
  1151. SET_HEAD_READ(head, MINION_SYS_CHIP_SIG);
  1152. SET_HEAD_SIZ(head, rsiz);
  1153. wsiz = HSIZE() + rsiz;
  1154. for (chip = 0; chip < MINION_CHIPS; chip++) {
  1155. head->chip = (uint8_t)chip;
  1156. tries = 0;
  1157. ok = false;
  1158. do {
  1159. reply = do_ioctl(wbuf, wsiz, rbuf, rsiz);
  1160. if (reply == (int)(wsiz)) {
  1161. uint32_t sig = u8tou32(rbuf, wsiz - rsiz);
  1162. if (sig == MINION_CHIP_SIG) {
  1163. minioninfo->chip[chip] = true;
  1164. minioninfo->chips++;
  1165. ok = true;
  1166. } else {
  1167. if (sig == MINION_CHIP_SIG_SHIFT1 ||
  1168. sig == MINION_CHIP_SIG_SHIFT2 ||
  1169. sig == MINION_CHIP_SIG_SHIFT3 ||
  1170. sig == MINION_CHIP_SIG_SHIFT4) {
  1171. applog(LOG_WARNING, "%s: chip %d detect offset got"
  1172. " 0x%08x wanted 0x%08x",
  1173. minioncgpu->drv->dname, chip, sig,
  1174. MINION_CHIP_SIG);
  1175. } else {
  1176. if (sig == MINION_NOCHIP_SIG ||
  1177. sig == MINION_NOCHIP_SIG2) // Assume no chip
  1178. ok = true;
  1179. else {
  1180. applog(LOG_ERR, "%s: chip %d detect failed got"
  1181. " 0x%08x wanted 0x%08x",
  1182. minioncgpu->drv->dname, chip, sig,
  1183. MINION_CHIP_SIG);
  1184. }
  1185. }
  1186. }
  1187. } else {
  1188. applog(LOG_ERR, "%s: chip %d reply %d ignored should be %d",
  1189. minioncgpu->drv->dname, chip, reply, (int)(wsiz));
  1190. }
  1191. } while (!ok && ++tries <= MINION_SIG_TRIES);
  1192. if (!ok) {
  1193. applog(LOG_ERR, "%s: chip %d - detect failure status",
  1194. minioncgpu->drv->dname, chip);
  1195. }
  1196. }
  1197. if (minioninfo->chips) {
  1198. for (chip = 0; chip < MINION_CHIPS; chip++) {
  1199. if (minioninfo->chip[chip]) {
  1200. init_chip(minioncgpu, minioninfo, chip);
  1201. enable_chip_cores(minioncgpu, minioninfo, chip);
  1202. }
  1203. }
  1204. #if ENABLE_INT_NONO
  1205. // After everything is ready
  1206. for (chip = 0; chip < MINION_CHIPS; chip++)
  1207. if (minioninfo->chip[chip])
  1208. enable_interrupt(minioncgpu, minioninfo, chip);
  1209. #endif
  1210. }
  1211. }
  1212. static const char *minion_modules[] = {
  1213. #if MINION_ROCKCHIP == 0
  1214. "i2c-dev",
  1215. "i2c-bcm2708",
  1216. "spidev",
  1217. "spi-bcm2708",
  1218. #endif
  1219. NULL
  1220. };
  1221. static struct {
  1222. int request;
  1223. int value;
  1224. } minion_ioc[] = {
  1225. { SPI_IOC_RD_MODE, 0 },
  1226. { SPI_IOC_WR_MODE, 0 },
  1227. { SPI_IOC_RD_BITS_PER_WORD, 8 },
  1228. { SPI_IOC_WR_BITS_PER_WORD, 8 },
  1229. { SPI_IOC_RD_MAX_SPEED_HZ, MINION_SPI_SPEED },
  1230. { SPI_IOC_WR_MAX_SPEED_HZ, MINION_SPI_SPEED },
  1231. { -1, -1 }
  1232. };
  1233. static bool minion_init_spi(struct cgpu_info *minioncgpu, struct minion_info *minioninfo, int bus, int chip)
  1234. {
  1235. int i, err, data;
  1236. char buf[64];
  1237. for (i = 0; minion_modules[i]; i++) {
  1238. snprintf(buf, sizeof(buf), "modprobe %s", minion_modules[i]);
  1239. err = system(buf);
  1240. if (err) {
  1241. applog(LOG_ERR, "%s: failed to modprobe %s (%d) - you need to be root?",
  1242. minioncgpu->drv->dname,
  1243. minion_modules[i], err);
  1244. goto bad_out;
  1245. }
  1246. }
  1247. snprintf(buf, sizeof(buf), "/dev/spidev%d.%d", bus, chip);
  1248. minioninfo->spifd = open(buf, O_RDWR);
  1249. if (minioninfo->spifd < 0) {
  1250. applog(LOG_ERR, "%s: failed to open spidev (%d)",
  1251. minioncgpu->drv->dname,
  1252. errno);
  1253. goto bad_out;
  1254. }
  1255. minioncgpu->device_path = strdup(buf);
  1256. for (i = 0; minion_ioc[i].value != -1; i++) {
  1257. data = minion_ioc[i].value;
  1258. err = ioctl(minioninfo->spifd, minion_ioc[i].request, (void *)&data);
  1259. if (err < 0) {
  1260. applog(LOG_ERR, "%s: failed ioctl configuration (%d) (%d)",
  1261. minioncgpu->drv->dname,
  1262. i, errno);
  1263. goto close_out;
  1264. }
  1265. }
  1266. return true;
  1267. close_out:
  1268. close(minioninfo->spifd);
  1269. minioninfo->spifd = 0;
  1270. free(minioncgpu->device_path);
  1271. minioncgpu->device_path = NULL;
  1272. bad_out:
  1273. return false;
  1274. }
  1275. #if ENABLE_INT_NONO
  1276. static bool minion_init_gpio_interrupt(struct cgpu_info *minioncgpu, struct minion_info *minioninfo)
  1277. {
  1278. char pindir[64], ena[64], pin[8], dir[64], edge[64], act[64];
  1279. struct stat st;
  1280. int file, err;
  1281. ssize_t ret;
  1282. snprintf(pindir, sizeof(pindir), MINION_GPIO_SYS MINION_GPIO_PIN,
  1283. MINION_GPIO_RESULT_INT_PIN);
  1284. memset(&st, 0, sizeof(st));
  1285. if (stat(pindir, &st) == 0) { // already exists
  1286. if (!S_ISDIR(st.st_mode)) {
  1287. applog(LOG_ERR, "%s: failed1 to enable GPIO pin %d interrupt"
  1288. " - not a directory",
  1289. minioncgpu->drv->dname,
  1290. MINION_GPIO_RESULT_INT_PIN);
  1291. return false;
  1292. }
  1293. } else {
  1294. snprintf(ena, sizeof(ena), MINION_GPIO_SYS MINION_GPIO_ENA);
  1295. file = open(ena, O_WRONLY | O_SYNC);
  1296. if (file == -1) {
  1297. applog(LOG_ERR, "%s: failed2 to enable GPIO pin %d interrupt (%d)"
  1298. " - you need to be root?",
  1299. minioncgpu->drv->dname,
  1300. MINION_GPIO_RESULT_INT_PIN,
  1301. errno);
  1302. return false;
  1303. }
  1304. snprintf(pin, sizeof(pin), MINION_GPIO_ENA_VAL, MINION_GPIO_RESULT_INT_PIN);
  1305. ret = write(file, pin, (size_t)strlen(pin));
  1306. if (ret != (ssize_t)strlen(pin)) {
  1307. if (ret < 0)
  1308. err = errno;
  1309. else
  1310. err = (int)ret;
  1311. close(file);
  1312. applog(LOG_ERR, "%s: failed3 to enable GPIO pin %d interrupt (%d:%d)",
  1313. minioncgpu->drv->dname,
  1314. MINION_GPIO_RESULT_INT_PIN,
  1315. err, (int)strlen(pin));
  1316. return false;
  1317. }
  1318. close(file);
  1319. // Check again if it exists
  1320. memset(&st, 0, sizeof(st));
  1321. if (stat(pindir, &st) != 0) {
  1322. applog(LOG_ERR, "%s: failed4 to enable GPIO pin %d interrupt (%d)",
  1323. minioncgpu->drv->dname,
  1324. MINION_GPIO_RESULT_INT_PIN,
  1325. errno);
  1326. return false;
  1327. }
  1328. }
  1329. // Set the pin attributes
  1330. // Direction
  1331. snprintf(dir, sizeof(dir), MINION_GPIO_SYS MINION_GPIO_PIN MINION_GPIO_DIR,
  1332. MINION_GPIO_RESULT_INT_PIN);
  1333. file = open(dir, O_WRONLY | O_SYNC);
  1334. if (file == -1) {
  1335. applog(LOG_ERR, "%s: failed5 to enable GPIO pin %d interrupt (%d)"
  1336. " - you need to be root?",
  1337. minioncgpu->drv->dname,
  1338. MINION_GPIO_RESULT_INT_PIN,
  1339. errno);
  1340. return false;
  1341. }
  1342. ret = write(file, MINION_GPIO_DIR_READ, (size_t)strlen(MINION_GPIO_DIR_READ));
  1343. if (ret != (ssize_t)strlen(MINION_GPIO_DIR_READ)) {
  1344. if (ret < 0)
  1345. err = errno;
  1346. else
  1347. err = (int)ret;
  1348. close(file);
  1349. applog(LOG_ERR, "%s: failed6 to enable GPIO pin %d interrupt (%d:%d)",
  1350. minioncgpu->drv->dname,
  1351. MINION_GPIO_RESULT_INT_PIN,
  1352. err, (int)strlen(MINION_GPIO_DIR_READ));
  1353. return false;
  1354. }
  1355. close(file);
  1356. // Edge
  1357. snprintf(edge, sizeof(edge), MINION_GPIO_SYS MINION_GPIO_PIN MINION_GPIO_EDGE,
  1358. MINION_GPIO_RESULT_INT_PIN);
  1359. file = open(edge, O_WRONLY | O_SYNC);
  1360. if (file == -1) {
  1361. applog(LOG_ERR, "%s: failed7 to enable GPIO pin %d interrupt (%d)",
  1362. minioncgpu->drv->dname,
  1363. MINION_GPIO_RESULT_INT_PIN,
  1364. errno);
  1365. return false;
  1366. }
  1367. ret = write(file, MINION_GPIO_EDGE_RISING, (size_t)strlen(MINION_GPIO_EDGE_RISING));
  1368. if (ret != (ssize_t)strlen(MINION_GPIO_EDGE_RISING)) {
  1369. if (ret < 0)
  1370. err = errno;
  1371. else
  1372. err = (int)ret;
  1373. close(file);
  1374. applog(LOG_ERR, "%s: failed8 to enable GPIO pin %d interrupt (%d:%d)",
  1375. minioncgpu->drv->dname,
  1376. MINION_GPIO_RESULT_INT_PIN,
  1377. err, (int)strlen(MINION_GPIO_EDGE_RISING));
  1378. return false;
  1379. }
  1380. close(file);
  1381. // Active
  1382. snprintf(act, sizeof(act), MINION_GPIO_SYS MINION_GPIO_PIN MINION_GPIO_ACT,
  1383. MINION_GPIO_RESULT_INT_PIN);
  1384. file = open(act, O_WRONLY | O_SYNC);
  1385. if (file == -1) {
  1386. applog(LOG_ERR, "%s: failed9 to enable GPIO pin %d interrupt (%d)",
  1387. minioncgpu->drv->dname,
  1388. MINION_GPIO_RESULT_INT_PIN,
  1389. errno);
  1390. return false;
  1391. }
  1392. ret = write(file, MINION_GPIO_ACT_HI, (size_t)strlen(MINION_GPIO_ACT_HI));
  1393. if (ret != (ssize_t)strlen(MINION_GPIO_ACT_HI)) {
  1394. if (ret < 0)
  1395. err = errno;
  1396. else
  1397. err = (int)ret;
  1398. close(file);
  1399. applog(LOG_ERR, "%s: failed10 to enable GPIO pin %d interrupt (%d:%d)",
  1400. minioncgpu->drv->dname,
  1401. MINION_GPIO_RESULT_INT_PIN,
  1402. err, (int)strlen(MINION_GPIO_ACT_HI));
  1403. return false;
  1404. }
  1405. close(file);
  1406. // Setup fd access to Value
  1407. snprintf(minioninfo->gpiointvalue, sizeof(minioninfo->gpiointvalue),
  1408. MINION_GPIO_SYS MINION_GPIO_PIN MINION_GPIO_VALUE,
  1409. MINION_GPIO_RESULT_INT_PIN);
  1410. minioninfo->gpiointfd = open(minioninfo->gpiointvalue, O_RDONLY);
  1411. if (minioninfo->gpiointfd == -1) {
  1412. applog(LOG_ERR, "%s: failed11 to enable GPIO pin %d interrupt (%d)",
  1413. minioncgpu->drv->dname,
  1414. MINION_GPIO_RESULT_INT_PIN,
  1415. errno);
  1416. return false;
  1417. }
  1418. return true;
  1419. }
  1420. #endif
  1421. // Default meaning all cores
  1422. static void default_all_cores(uint8_t *cores)
  1423. {
  1424. int i;
  1425. // clear all bits
  1426. for (i = 0; i < (int)(DATA_SIZ * MINION_CORE_REPS); i++)
  1427. cores[i] = 0x00;
  1428. // enable (only) all cores
  1429. for (i = 0; i < MINION_CORES; i++)
  1430. ENABLE_CORE(cores, i);
  1431. }
  1432. static void minion_process_options(struct minion_info *minioninfo)
  1433. {
  1434. int last_freq, last_temp;
  1435. char *freq, *temp, *core, *comma, *buf, *plus, *minus;
  1436. uint8_t last_cores[DATA_SIZ*MINION_CORE_REPS];
  1437. int i, core1, core2;
  1438. bool cleared;
  1439. last_freq = MINION_FREQ_DEF;
  1440. if (opt_minion_freq && *opt_minion_freq) {
  1441. buf = freq = strdup(opt_minion_freq);
  1442. comma = strchr(freq, ',');
  1443. if (comma)
  1444. *(comma++) = '\0';
  1445. for (i = 0; i < MINION_CHIPS; i++) {
  1446. if (freq && isdigit(*freq)) {
  1447. last_freq = (int)(round((double)atoi(freq) / (double)MINION_FREQ_FACTOR)) * MINION_FREQ_FACTOR;
  1448. if (last_freq < MINION_FREQ_MIN)
  1449. last_freq = MINION_FREQ_MIN;
  1450. if (last_freq > MINION_FREQ_MAX)
  1451. last_freq = MINION_FREQ_MAX;
  1452. freq = comma;
  1453. if (comma) {
  1454. comma = strchr(freq, ',');
  1455. if (comma)
  1456. *(comma++) = '\0';
  1457. }
  1458. }
  1459. minioninfo->init_freq[i] = last_freq;
  1460. }
  1461. free(buf);
  1462. }
  1463. last_temp = MINION_TEMP_CTL_DEF;
  1464. if (opt_minion_temp && *opt_minion_temp) {
  1465. buf = temp = strdup(opt_minion_temp);
  1466. comma = strchr(temp, ',');
  1467. if (comma)
  1468. *(comma++) = '\0';
  1469. for (i = 0; i < MINION_CHIPS; i++) {
  1470. if (temp) {
  1471. if (isdigit(*temp)) {
  1472. last_temp = atoi(temp);
  1473. last_temp -= (last_temp % MINION_TEMP_CTL_STEP);
  1474. if (last_temp < MINION_TEMP_CTL_MIN_VALUE)
  1475. last_temp = MINION_TEMP_CTL_MIN_VALUE;
  1476. if (last_temp > MINION_TEMP_CTL_MAX_VALUE)
  1477. last_temp = MINION_TEMP_CTL_MAX_VALUE;
  1478. } else {
  1479. if (strcasecmp(temp, MINION_TEMP_DISABLE) == 0)
  1480. last_temp = MINION_TEMP_CTL_DISABLE;
  1481. }
  1482. temp = comma;
  1483. if (comma) {
  1484. comma = strchr(temp, ',');
  1485. if (comma)
  1486. *(comma++) = '\0';
  1487. }
  1488. }
  1489. minioninfo->init_temp[i] = last_temp;
  1490. }
  1491. free(buf);
  1492. }
  1493. default_all_cores(&(last_cores[0]));
  1494. // default to all cores until we find valid data
  1495. cleared = false;
  1496. if (opt_minion_cores && *opt_minion_cores) {
  1497. buf = core = strdup(opt_minion_cores);
  1498. comma = strchr(core, ',');
  1499. if (comma)
  1500. *(comma++) = '\0';
  1501. for (i = 0; i < MINION_CHIPS; i++) {
  1502. // default to previous until we find valid data
  1503. cleared = false;
  1504. if (core) {
  1505. plus = strchr(core, '+');
  1506. if (plus)
  1507. *(plus++) = '\0';
  1508. while (core) {
  1509. minus = strchr(core, '-');
  1510. if (minus)
  1511. *(minus++) = '\0';
  1512. if (isdigit(*core)) {
  1513. core1 = atoi(core);
  1514. if (core1 >= 0 && core1 < MINION_CORES) {
  1515. if (!minus) {
  1516. if (!cleared) {
  1517. memset(last_cores, 0, sizeof(last_cores));
  1518. cleared = true;
  1519. }
  1520. ENABLE_CORE(last_cores, core1);
  1521. } else {
  1522. core2 = atoi(minus);
  1523. if (core2 >= core1) {
  1524. if (core2 >= MINION_CORES)
  1525. core2 = MINION_CORES - 1;
  1526. while (core1 <= core2) {
  1527. if (!cleared) {
  1528. memset(last_cores, 0,
  1529. sizeof(last_cores));
  1530. cleared = true;
  1531. }
  1532. ENABLE_CORE(last_cores, core1);
  1533. core1++;
  1534. }
  1535. }
  1536. }
  1537. }
  1538. } else {
  1539. if (strcasecmp(core, MINION_CORE_ALL) == 0)
  1540. default_all_cores(&(last_cores[0]));
  1541. }
  1542. core = plus;
  1543. if (plus) {
  1544. plus = strchr(core, '+');
  1545. if (plus)
  1546. *(plus++) = '\0';
  1547. }
  1548. }
  1549. core = comma;
  1550. if (comma) {
  1551. comma = strchr(core, ',');
  1552. if (comma)
  1553. *(comma++) = '\0';
  1554. }
  1555. }
  1556. memcpy(&(minioninfo->init_cores[i][0]), &(last_cores[0]), sizeof(last_cores));
  1557. }
  1558. free(buf);
  1559. }
  1560. }
  1561. static void minion_detect(bool hotplug)
  1562. {
  1563. struct cgpu_info *minioncgpu = NULL;
  1564. struct minion_info *minioninfo = NULL;
  1565. char buf[512];
  1566. size_t off;
  1567. int i;
  1568. if (hotplug)
  1569. return;
  1570. minioncgpu = calloc(1, sizeof(*minioncgpu));
  1571. if (unlikely(!minioncgpu))
  1572. quithere(1, "Failed to calloc minioncgpu");
  1573. minioncgpu->drv = &minion_drv;
  1574. minioncgpu->deven = DEV_ENABLED;
  1575. minioncgpu->threads = 1;
  1576. minioninfo = calloc(1, sizeof(*minioninfo)); // everything '0'
  1577. if (unlikely(!minioninfo))
  1578. quithere(1, "Failed to calloc minioninfo");
  1579. minioncgpu->device_data = (void *)minioninfo;
  1580. if (!minion_init_spi(minioncgpu, minioninfo, MINION_SPI_BUS, MINION_SPI_CHIP))
  1581. goto unalloc;
  1582. #if ENABLE_INT_NONO
  1583. if (!minion_init_gpio_interrupt(minioncgpu, minioninfo))
  1584. goto unalloc;
  1585. #endif
  1586. mutex_init(&(minioninfo->spi_lock));
  1587. mutex_init(&(minioninfo->sta_lock));
  1588. for (i = 0; i < MINION_CHIPS; i++) {
  1589. minioninfo->init_freq[i] = MINION_FREQ_DEF;
  1590. minioninfo->init_temp[i] = MINION_TEMP_CTL_DEF;
  1591. default_all_cores(&(minioninfo->init_cores[i][0]));
  1592. }
  1593. minion_process_options(minioninfo);
  1594. applog(LOG_WARNING, "%s: checking for chips ...", minioncgpu->drv->dname);
  1595. minion_detect_chips(minioncgpu, minioninfo);
  1596. buf[0] = '\0';
  1597. for (i = 0; i < MINION_CHIPS; i++) {
  1598. if (minioninfo->chip[i]) {
  1599. off = strlen(buf);
  1600. snprintf(buf + off, sizeof(buf) - off, " %d", i);
  1601. }
  1602. }
  1603. applog(LOG_WARNING, "%s: found %d chip%s:%s",
  1604. minioncgpu->drv->dname, minioninfo->chips,
  1605. (minioninfo->chips == 1) ? "" : "s", buf);
  1606. if (minioninfo->chips == 0)
  1607. goto cleanup;
  1608. if (!add_cgpu(minioncgpu))
  1609. goto cleanup;
  1610. mutex_init(&(minioninfo->nonce_lock));
  1611. minioninfo->wfree_list = k_new_list("Work", sizeof(WITEM), ALLOC_WITEMS, LIMIT_WITEMS, true);
  1612. minioninfo->wwork_list = k_new_store(minioninfo->wfree_list);
  1613. // Initialise them all in case we later decide to enable chips
  1614. for (i = 0; i < MINION_CHIPS; i++) {
  1615. minioninfo->wque_list[i] = k_new_store(minioninfo->wfree_list);
  1616. minioninfo->wchip_list[i] = k_new_store(minioninfo->wfree_list);
  1617. }
  1618. minioninfo->tfree_list = k_new_list("Task", sizeof(TITEM), ALLOC_TITEMS, LIMIT_TITEMS, true);
  1619. minioninfo->task_list = k_new_store(minioninfo->tfree_list);
  1620. minioninfo->treply_list = k_new_store(minioninfo->tfree_list);
  1621. minioninfo->rfree_list = k_new_list("Reply", sizeof(RITEM), ALLOC_RITEMS, LIMIT_RITEMS, true);
  1622. minioninfo->rnonce_list = k_new_store(minioninfo->rfree_list);
  1623. minioninfo->history_gen = MINION_MAX_RESET_CHECK;
  1624. minioninfo->hfree_list = k_new_list("History", sizeof(HITEM), ALLOC_HITEMS, LIMIT_HITEMS, true);
  1625. for (i = 0; i < MINION_CHIPS; i++)
  1626. minioninfo->hchip_list[i] = k_new_store(minioninfo->hfree_list);
  1627. cgsem_init(&(minioninfo->task_ready));
  1628. cgsem_init(&(minioninfo->nonce_ready));
  1629. cgsem_init(&(minioninfo->scan_work));
  1630. minioninfo->initialised = true;
  1631. return;
  1632. cleanup:
  1633. close(minioninfo->gpiointfd);
  1634. close(minioninfo->spifd);
  1635. mutex_destroy(&(minioninfo->sta_lock));
  1636. mutex_destroy(&(minioninfo->spi_lock));
  1637. unalloc:
  1638. free(minioninfo);
  1639. free(minioncgpu);
  1640. }
  1641. static void minion_identify(__maybe_unused struct cgpu_info *minioncgpu)
  1642. {
  1643. // flash a led
  1644. }
  1645. /*
  1646. * SPI/ioctl write thread
  1647. * Non urgent work is to keep the queue full
  1648. * Urgent work is when an LP occurs (or the queue is empty/low)
  1649. */
  1650. static void *minion_spi_write(void *userdata)
  1651. {
  1652. struct cgpu_info *minioncgpu = (struct cgpu_info *)userdata;
  1653. struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
  1654. K_ITEM *item, *tail, *task, *work;
  1655. TITEM *titem;
  1656. applog(MINION_LOG, "%s%i: SPI writing...",
  1657. minioncgpu->drv->name, minioncgpu->device_id);
  1658. // Wait until we're ready
  1659. while (minioncgpu->shutdown == false) {
  1660. if (minioninfo->initialised) {
  1661. break;
  1662. }
  1663. cgsleep_ms(1); // asap to start mining
  1664. }
  1665. // TODO: combine all urgent into a single I/O?
  1666. // Then combine all state 1 for the same chip into a single I/O ?
  1667. // (then again for state 2?)
  1668. while (minioncgpu->shutdown == false) {
  1669. item = NULL;
  1670. K_WLOCK(minioninfo->task_list);
  1671. tail = minioninfo->task_list->tail;
  1672. if (tail) {
  1673. // Find first urgent item
  1674. item = tail;
  1675. while (item && !(DATAT(item)->urgent))
  1676. item = item->prev;
  1677. // No urgent items, just do the tail
  1678. if (!item)
  1679. item = tail;
  1680. k_unlink_item(minioninfo->task_list, item);
  1681. }
  1682. K_WUNLOCK(minioninfo->task_list);
  1683. if (item) {
  1684. bool do_txrx = true;
  1685. bool store_reply = true;
  1686. titem = DATAT(item);
  1687. switch (titem->address) {
  1688. // TODO: case MINION_SYS_TEMP_CTL:
  1689. // TODO: case MINION_SYS_FREQ_CTL:
  1690. case READ_ADDR(MINION_SYS_CHIP_STA):
  1691. case WRITE_ADDR(MINION_SYS_RSTN_CTL):
  1692. case WRITE_ADDR(MINION_SYS_INT_CLR):
  1693. case READ_ADDR(MINION_SYS_IDLE_CNT):
  1694. case READ_ADDR(MINION_CORE_ENA0_31):
  1695. case READ_ADDR(MINION_CORE_ENA32_63):
  1696. case READ_ADDR(MINION_CORE_ENA64_95):
  1697. case READ_ADDR(MINION_CORE_ENA96_98):
  1698. case READ_ADDR(MINION_CORE_ACT0_31):
  1699. case READ_ADDR(MINION_CORE_ACT32_63):
  1700. case READ_ADDR(MINION_CORE_ACT64_95):
  1701. case READ_ADDR(MINION_CORE_ACT96_98):
  1702. store_reply = false;
  1703. break;
  1704. case WRITE_ADDR(MINION_QUE_0):
  1705. //applog(LOG_ERR, "%s%i: ZZZ send task_id 0x%04x - chip %d", minioncgpu->drv->name, minioncgpu->device_id, titem->task_id, titem->chip);
  1706. store_reply = false;
  1707. break;
  1708. default:
  1709. do_txrx = false;
  1710. titem->reply = MINION_UNEXPECTED_TASK;
  1711. applog(LOG_ERR, "%s%i: Unexpected task address 0x%02x (%s)",
  1712. minioncgpu->drv->name, minioncgpu->device_id,
  1713. (unsigned int)(titem->address),
  1714. addr2txt(titem->address));
  1715. break;
  1716. }
  1717. if (do_txrx) {
  1718. minion_txrx(titem);
  1719. int chip = titem->chip;
  1720. switch (titem->address) {
  1721. case READ_ADDR(MINION_SYS_CHIP_STA):
  1722. if (titem->reply >= (int)(titem->osiz)) {
  1723. uint8_t *rep = &(titem->rbuf[titem->osiz - titem->rsiz]);
  1724. mutex_lock(&(minioninfo->sta_lock));
  1725. minioninfo->chip_status[chip].temp = STA_TEMP(rep);
  1726. minioninfo->chip_status[chip].cores = STA_CORES(rep);
  1727. minioninfo->chip_status[chip].freq = STA_FREQ(rep);
  1728. mutex_unlock(&(minioninfo->sta_lock));
  1729. if (minioninfo->chip_status[chip].overheat) {
  1730. switch (STA_TEMP(rep)) {
  1731. case MINION_TEMP_40:
  1732. case MINION_TEMP_60:
  1733. case MINION_TEMP_80:
  1734. cgtime(&(minioninfo->chip_status[chip].lastrecover));
  1735. minioninfo->chip_status[chip].overheat = false;
  1736. applog(LOG_WARNING, "%s%d: chip %d cooled, restarting",
  1737. minioncgpu->drv->name,
  1738. minioncgpu->device_id,
  1739. chip);
  1740. cgtime(&(minioninfo->chip_status[chip].lastrecover));
  1741. minioninfo->chip_status[chip].overheattime +=
  1742. tdiff(&(minioninfo->chip_status[chip].lastrecover),
  1743. &(minioninfo->chip_status[chip].lastoverheat));
  1744. break;
  1745. default:
  1746. break;
  1747. }
  1748. } else {
  1749. if (opt_minion_overheat && STA_TEMP(rep) == MINION_TEMP_OVER) {
  1750. cgtime(&(minioninfo->chip_status[chip].lastoverheat));
  1751. minioninfo->chip_status[chip].overheat = true;
  1752. applog(LOG_WARNING, "%s%d: chip %d overheated! idling",
  1753. minioncgpu->drv->name,
  1754. minioncgpu->device_id,
  1755. chip);
  1756. K_WLOCK(minioninfo->tfree_list);
  1757. task = k_unlink_head(minioninfo->tfree_list);
  1758. DATAT(task)->tid = ++(minioninfo->next_tid);
  1759. DATAT(task)->chip = chip;
  1760. DATAT(task)->write = true;
  1761. DATAT(task)->address = MINION_SYS_RSTN_CTL;
  1762. DATAT(task)->task_id = 0; // ignored
  1763. DATAT(task)->wsiz = MINION_SYS_SIZ;
  1764. DATAT(task)->rsiz = 0;
  1765. DATAT(task)->wbuf[0] = SYS_RSTN_CTL_FLUSH;
  1766. DATAT(task)->wbuf[1] = 0;
  1767. DATAT(task)->wbuf[2] = 0;
  1768. DATAT(task)->wbuf[3] = 0;
  1769. DATAT(task)->urgent = true;
  1770. k_add_head(minioninfo->task_list, task);
  1771. K_WUNLOCK(minioninfo->tfree_list);
  1772. minioninfo->chip_status[chip].overheats++;
  1773. }
  1774. }
  1775. }
  1776. break;
  1777. case READ_ADDR(MINION_SYS_IDLE_CNT):
  1778. {
  1779. uint32_t *cnt = (uint32_t *)&(titem->rbuf[titem->osiz - titem->rsiz]);
  1780. minioninfo->chip_status[chip].idle = *cnt;
  1781. }
  1782. break;
  1783. case WRITE_ADDR(MINION_SYS_RSTN_CTL):
  1784. // Do this here after it has actually been flushed
  1785. if ((titem->wbuf[0] & SYS_RSTN_CTL_FLUSH) == SYS_RSTN_CTL_FLUSH) {
  1786. K_WLOCK(minioninfo->wwork_list);
  1787. work = minioninfo->wchip_list[chip]->head;
  1788. while (work) {
  1789. DATAW(work)->stale = true;
  1790. minioninfo->chip_status[chip].chipwork--;
  1791. if (minioninfo->chip_status[chip].realwork > 0)
  1792. minioninfo->chip_status[chip].realwork--;
  1793. work = work->next;
  1794. }
  1795. minioninfo->chip_status[chip].chipwork = 0;
  1796. minioninfo->chip_status[chip].realwork = 0;
  1797. K_WUNLOCK(minioninfo->wwork_list);
  1798. }
  1799. break;
  1800. case WRITE_ADDR(MINION_QUE_0):
  1801. K_WLOCK(minioninfo->wchip_list[chip]);
  1802. k_unlink_item(minioninfo->wque_list[chip], titem->witem);
  1803. k_add_head(minioninfo->wchip_list[chip], titem->witem);
  1804. minioninfo->chip_status[chip].quework--;
  1805. minioninfo->chip_status[chip].chipwork++;
  1806. minioninfo->chip_status[chip].realwork++;
  1807. K_WUNLOCK(minioninfo->wchip_list[chip]);
  1808. break;
  1809. case READ_ADDR(MINION_CORE_ENA0_31):
  1810. case READ_ADDR(MINION_CORE_ENA32_63):
  1811. case READ_ADDR(MINION_CORE_ENA64_95):
  1812. case READ_ADDR(MINION_CORE_ENA96_98):
  1813. {
  1814. uint32_t *rep = (uint32_t *)&(titem->rbuf[titem->osiz - titem->rsiz]);
  1815. int off = titem->address - READ_ADDR(MINION_CORE_ENA0_31);
  1816. minioninfo->chip_core_ena[off][chip] = *rep;
  1817. }
  1818. break;
  1819. case READ_ADDR(MINION_CORE_ACT0_31):
  1820. case READ_ADDR(MINION_CORE_ACT32_63):
  1821. case READ_ADDR(MINION_CORE_ACT64_95):
  1822. case READ_ADDR(MINION_CORE_ACT96_98):
  1823. {
  1824. uint32_t *rep = (uint32_t *)&(titem->rbuf[titem->osiz - titem->rsiz]);
  1825. int off = titem->address - READ_ADDR(MINION_CORE_ACT0_31);
  1826. minioninfo->chip_core_act[off][chip] = *rep;
  1827. }
  1828. break;
  1829. case WRITE_ADDR(MINION_SYS_INT_CLR):
  1830. break;
  1831. default:
  1832. break;
  1833. }
  1834. }
  1835. K_WLOCK(minioninfo->treply_list);
  1836. if (store_reply)
  1837. k_add_head(minioninfo->treply_list, item);
  1838. else
  1839. k_free_head(minioninfo->tfree_list, item);
  1840. K_WUNLOCK(minioninfo->treply_list);
  1841. /*
  1842. * Always check for the next task immediately if we just did one
  1843. * i.e. empty the task queue
  1844. */
  1845. continue;
  1846. }
  1847. cgsem_mswait(&(minioninfo->task_ready), MINION_TASK_mS);
  1848. }
  1849. return NULL;
  1850. }
  1851. /*
  1852. * SPI/ioctl reply thread
  1853. * ioctl done every interrupt or MINION_REPLY_mS checking for results
  1854. */
  1855. static void *minion_spi_reply(void *userdata)
  1856. {
  1857. struct cgpu_info *minioncgpu = (struct cgpu_info *)userdata;
  1858. struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
  1859. struct minion_result *result1, *result2, *use1, *use2;
  1860. K_ITEM *item;
  1861. TITEM fifo_task, res1_task, res2_task;
  1862. int chip, resoff;
  1863. int chipwork, gap;
  1864. bool somelow;
  1865. struct timeval now;
  1866. #if ENABLE_INT_NONO
  1867. TITEM clr_task;
  1868. struct pollfd pfd;
  1869. struct minion_header *head;
  1870. uint8_t rbuf[MINION_BUFSIZ];
  1871. uint8_t wbuf[MINION_BUFSIZ];
  1872. uint32_t wsiz, rsiz;
  1873. int ret, reply;
  1874. bool gotreplies = false;
  1875. #endif
  1876. applog(MINION_LOG, "%s%i: SPI replying...",
  1877. minioncgpu->drv->name, minioncgpu->device_id);
  1878. // Wait until we're ready
  1879. while (minioncgpu->shutdown == false) {
  1880. if (minioninfo->initialised) {
  1881. break;
  1882. }
  1883. cgsleep_ms(2);
  1884. }
  1885. fifo_task.chip = 0;
  1886. fifo_task.write = false;
  1887. fifo_task.address = MINION_SYS_FIFO_STA;
  1888. fifo_task.wsiz = 0;
  1889. fifo_task.rsiz = MINION_SYS_SIZ;
  1890. res1_task.chip = 0;
  1891. res1_task.write = false;
  1892. if (minreread)
  1893. res1_task.address = MINION_RES_PEEK;
  1894. else
  1895. res1_task.address = MINION_RES_DATA;
  1896. res1_task.wsiz = 0;
  1897. res1_task.rsiz = MINION_RES_DATA_SIZ;
  1898. res2_task.chip = 0;
  1899. res2_task.write = false;
  1900. res2_task.address = MINION_RES_DATA;
  1901. res2_task.wsiz = 0;
  1902. res2_task.rsiz = MINION_RES_DATA_SIZ;
  1903. #if ENABLE_INT_NONO
  1904. // Clear RESULT_INT after reading all results
  1905. clr_task.chip = 0;
  1906. clr_task.write = true;
  1907. clr_task.address = MINION_SYS_INT_CLR;
  1908. clr_task.wsiz = MINION_SYS_SIZ;
  1909. clr_task.rsiz = 0;
  1910. clr_task.wbuf[0] = MINION_RESULT_INT;
  1911. clr_task.wbuf[1] = 0;
  1912. clr_task.wbuf[2] = 0;
  1913. clr_task.wbuf[3] = 0;
  1914. memset(&pfd, 0, sizeof(pfd));
  1915. pfd.fd = minioninfo->gpiointfd;
  1916. pfd.events = POLLPRI;
  1917. head = (struct minion_header *)wbuf;
  1918. SET_HEAD_SIZ(head, MINION_SYS_SIZ);
  1919. wsiz = HSIZE() + MINION_SYS_SIZ;
  1920. rsiz = MINION_SYS_SIZ; // for READ, use 0 for WRITE
  1921. #endif
  1922. somelow = false;
  1923. while (minioncgpu->shutdown == false) {
  1924. for (chip = 0; chip < MINION_CHIPS; chip++) {
  1925. if (minioninfo->chip[chip]) {
  1926. int tries = 0;
  1927. uint8_t res, cmd;
  1928. while (++tries < 4) {
  1929. res = cmd = 0;
  1930. fifo_task.chip = chip;
  1931. fifo_task.reply = 0;
  1932. minion_txrx(&fifo_task);
  1933. if (fifo_task.reply <= 0)
  1934. break;
  1935. else {
  1936. if (fifo_task.reply < (int)(fifo_task.osiz)) {
  1937. char *buf = bin2hex((unsigned char *)(&(fifo_task.rbuf[fifo_task.osiz - fifo_task.rsiz])),
  1938. (int)(fifo_task.rsiz));
  1939. applog(LOG_ERR, "%s%i: Bad fifo reply (%s) size %d, should be %d",
  1940. minioncgpu->drv->name, minioncgpu->device_id, buf,
  1941. fifo_task.reply, (int)(fifo_task.osiz));
  1942. free(buf);
  1943. minioninfo->spi_errors++;
  1944. minioninfo->fifo_spi_errors[chip]++;
  1945. minioninfo->res_err_count[chip]++;
  1946. } else {
  1947. if (fifo_task.reply > (int)(fifo_task.osiz)) {
  1948. applog(LOG_ERR, "%s%i: Unexpected fifo reply size %d, expected only %d",
  1949. minioncgpu->drv->name, minioncgpu->device_id,
  1950. fifo_task.reply, (int)(fifo_task.osiz));
  1951. }
  1952. res = FIFO_RES(fifo_task.rbuf, fifo_task.osiz - fifo_task.rsiz);
  1953. cmd = FIFO_CMD(fifo_task.rbuf, fifo_task.osiz - fifo_task.rsiz);
  1954. // valid reply?
  1955. if (res <= MINION_QUE_MAX && cmd <= MINION_QUE_HIGH)
  1956. break;
  1957. applog(LOG_ERR, "%s%i: Bad fifo reply res %d (max is %d) cmd %d (max is %d)",
  1958. minioncgpu->drv->name, minioncgpu->device_id,
  1959. (int)res, MINION_QUE_MAX, (int)cmd, MINION_QUE_HIGH);
  1960. minioninfo->spi_errors++;
  1961. minioninfo->fifo_spi_errors[chip]++;
  1962. minioninfo->res_err_count[chip]++;
  1963. }
  1964. }
  1965. }
  1966. // Give up on this chip this round
  1967. if (tries >= 4)
  1968. continue;
  1969. K_WLOCK(minioninfo->wwork_list);
  1970. // it shouldn't go up
  1971. if (cmd < minioninfo->chip_status[chip].realwork)
  1972. minioninfo->chip_status[chip].realwork = (uint32_t)cmd;
  1973. else {
  1974. cmd = (uint8_t)(minioninfo->chip_status[chip].realwork);
  1975. minioninfo->spi_errors++;
  1976. minioninfo->fifo_spi_errors[chip]++;
  1977. minioninfo->res_err_count[chip]++;
  1978. }
  1979. chipwork = (int)(minioninfo->chip_status[chip].chipwork);
  1980. K_WUNLOCK(minioninfo->wwork_list);
  1981. gap = chipwork - (int)cmd;
  1982. if (gap < -1 || gap > 1) {
  1983. // applog(LOG_ERR, "%s%i: fifo cmd difference > 1 for chip %d - work %d cmd %d gap %d",
  1984. // minioncgpu->drv->name, minioncgpu->device_id,
  1985. // chip, chipwork, (int)cmd, gap);
  1986. }
  1987. if (cmd < MINION_QUE_LOW) {
  1988. somelow = true;
  1989. // Flag it in case the count is wrong
  1990. K_WLOCK(minioninfo->wwork_list);
  1991. minioninfo->chip_status[chip].islow = true;
  1992. minioninfo->chip_status[chip].lowcount = (int)cmd;
  1993. K_WUNLOCK(minioninfo->wwork_list);
  1994. }
  1995. /*
  1996. * Chip has results?
  1997. * You can't request results unless it says it has some.
  1998. * We don't ever directly flush the output queue while processing
  1999. * (except at startup) so the answer is always valid
  2000. * i.e. there could be more, but never less ... unless the reply was corrupt
  2001. */
  2002. if (res > MINION_MAX_RES) {
  2003. applog(LOG_ERR, "%s%i: Large work reply chip %d res %d",
  2004. minioncgpu->drv->name, minioncgpu->device_id, chip, res);
  2005. minioninfo->spi_errors++;
  2006. minioninfo->fifo_spi_errors[chip]++;
  2007. minioninfo->res_err_count[chip]++;
  2008. res = 1; // Just read one result
  2009. }
  2010. //else
  2011. //applog(LOG_ERR, "%s%i: work reply res %d", minioncgpu->drv->name, minioncgpu->device_id, res);
  2012. uint8_t left = res;
  2013. int peeks = 0;
  2014. while (left > 0) {
  2015. res = left;
  2016. if (res > MINION_MAX_RES)
  2017. res = MINION_MAX_RES;
  2018. left -= res;
  2019. repeek:
  2020. res1_task.chip = chip;
  2021. res1_task.reply = 0;
  2022. res1_task.rsiz = res * MINION_RES_DATA_SIZ;
  2023. minion_txrx(&res1_task);
  2024. if (res1_task.reply <= 0)
  2025. break;
  2026. else {
  2027. cgtime(&now);
  2028. if (res1_task.reply < (int)MINION_RES_DATA_SIZ) {
  2029. char *buf = bin2hex((unsigned char *)(&(res1_task.rbuf[res1_task.osiz - res1_task.rsiz])), (int)(res1_task.rsiz));
  2030. applog(LOG_ERR, "%s%i: Bad work reply (%s) size %d, should be at least %d",
  2031. minioncgpu->drv->name, minioncgpu->device_id, buf,
  2032. res1_task.reply, (int)MINION_RES_DATA_SIZ);
  2033. free(buf);
  2034. minioninfo->spi_errors++;
  2035. minioninfo->res_spi_errors[chip]++;
  2036. minioninfo->res_err_count[chip]++;
  2037. } else {
  2038. if (res1_task.reply != (int)(res1_task.osiz)) {
  2039. applog(LOG_ERR, "%s%i: Unexpected work reply size %d, expected %d",
  2040. minioncgpu->drv->name, minioncgpu->device_id,
  2041. res1_task.reply, (int)(res1_task.osiz));
  2042. minioninfo->spi_errors++;
  2043. minioninfo->res_spi_errors[chip]++;
  2044. minioninfo->res_err_count[chip]++;
  2045. // Can retry a PEEK without losing data
  2046. if (minreread) {
  2047. if (++peeks < 4)
  2048. goto repeek;
  2049. break;
  2050. }
  2051. }
  2052. if (minreread) {
  2053. res2_task.chip = chip;
  2054. res2_task.reply = 0;
  2055. res2_task.rsiz = res * MINION_RES_DATA_SIZ;
  2056. minion_txrx(&res2_task);
  2057. if (res2_task.reply <= 0) {
  2058. minioninfo->spi_errors++;
  2059. minioninfo->res_spi_errors[chip]++;
  2060. minioninfo->res_err_count[chip]++;
  2061. }
  2062. }
  2063. for (resoff = res1_task.osiz - res1_task.rsiz; resoff < (int)res1_task.osiz; resoff += MINION_RES_DATA_SIZ) {
  2064. result1 = (struct minion_result *)&(res1_task.rbuf[resoff]);
  2065. if (minreread && resoff < (int)res2_task.osiz)
  2066. result2 = (struct minion_result *)&(res2_task.rbuf[resoff]);
  2067. else
  2068. result2 = NULL;
  2069. if (IS_RESULT(result1) || (minreread && result2 && IS_RESULT(result2))) {
  2070. K_WLOCK(minioninfo->rfree_list);
  2071. item = k_unlink_head(minioninfo->rfree_list);
  2072. K_WUNLOCK(minioninfo->rfree_list);
  2073. if (IS_RESULT(result1)) {
  2074. use1 = result1;
  2075. if (minreread && result2 && IS_RESULT(result2))
  2076. use2 = result2;
  2077. else
  2078. use2 = NULL;
  2079. } else {
  2080. use1 = result2;
  2081. use2 = NULL;
  2082. minioninfo->use_res2[chip]++;
  2083. }
  2084. //DATAR(item)->chip = RES_CHIP(use1);
  2085. // We can avoid any SPI transmission error of the chip number
  2086. DATAR(item)->chip = (uint8_t)chip;
  2087. if ((uint8_t)chip != RES_CHIP(use1)) {
  2088. minioninfo->spi_errors++;
  2089. minioninfo->res_spi_errors[chip]++;
  2090. minioninfo->res_err_count[chip]++;
  2091. }
  2092. if (use2 && (uint8_t)chip != RES_CHIP(use2)) {
  2093. minioninfo->spi_errors++;
  2094. minioninfo->res_spi_errors[chip]++;
  2095. minioninfo->res_err_count[chip]++;
  2096. }
  2097. DATAR(item)->core = RES_CORE(use1);
  2098. DATAR(item)->task_id = RES_TASK(use1);
  2099. DATAR(item)->nonce = RES_NONCE(use1);
  2100. DATAR(item)->no_nonce = !RES_GOLD(use1);
  2101. memcpy(&(DATAR(item)->when), &now, sizeof(now));
  2102. if (!use2)
  2103. DATAR(item)->another = false;
  2104. else {
  2105. DATAR(item)->another = true;
  2106. DATAR(item)->task_id2 = RES_TASK(use2);
  2107. DATAR(item)->nonce2 = RES_NONCE(use2);
  2108. }
  2109. //applog(MINTASK_LOG, "%s%i: ZZZ reply task_id 0x%04x - chip %d - gold %d", minioncgpu->drv->name, minioncgpu->device_id, RES_TASK(use1), (int)RES_CHIP(use1), (int)RES_GOLD(use1));
  2110. //if (RES_GOLD(use1))
  2111. //applog(MINTASK_LOG, "%s%i: found a result chip %d core %d task 0x%04x nonce 0x%08x gold=%d", minioncgpu->drv->name, minioncgpu->device_id, DATAR(item)->chip, DATAR(item)->core, DATAR(item)->task_id, DATAR(item)->nonce, (int)RES_GOLD(use1));
  2112. K_WLOCK(minioninfo->rnonce_list);
  2113. k_add_head(minioninfo->rnonce_list, item);
  2114. K_WUNLOCK(minioninfo->rnonce_list);
  2115. cgsem_post(&(minioninfo->nonce_ready));
  2116. } else {
  2117. minioninfo->res_err_count[chip]++;
  2118. applog(MINTASK_LOG, "%s%i: Invalid res0 task_id 0x%04x - chip %d",
  2119. minioncgpu->drv->name, minioncgpu->device_id,
  2120. RES_TASK(result1), chip);
  2121. if (minreread && result2) {
  2122. applog(MINTASK_LOG, "%s%i: Invalid res1 task_id 0x%04x - chip %d",
  2123. minioncgpu->drv->name, minioncgpu->device_id,
  2124. RES_TASK(result2), chip);
  2125. }
  2126. }
  2127. }
  2128. }
  2129. }
  2130. }
  2131. }
  2132. }
  2133. if (somelow)
  2134. cgsem_post(&(minioninfo->scan_work));
  2135. #if ENABLE_INT_NONO
  2136. if (gotreplies)
  2137. minion_txrx(&clr_task);
  2138. #endif
  2139. #if !ENABLE_INT_NONO
  2140. cgsleep_ms(MINION_REPLY_mS);
  2141. #else
  2142. // TODO: this is going to require a bit of tuning with 2TH/s mining:
  2143. // The interrupt size MINION_RESULT_INT_SIZE should be high enough to expect
  2144. // most chips to have some results but low enough to cause negligible latency
  2145. // If all chips don't have some results when an interrupt occurs, then it is a waste
  2146. // since we have to check all chips for results anyway since we don't know which one
  2147. // caused the interrupt
  2148. // MINION_REPLY_mS needs to be low enough in the case of bad luck where no chip
  2149. // finds MINION_RESULT_INT_SIZE results in a short amount of time, so we go check
  2150. // them all anyway - to avoid high latency when there are only a few results due to low luck
  2151. ret = poll(&pfd, 1, MINION_REPLY_mS);
  2152. if (ret > 0) {
  2153. bool gotres;
  2154. int c;
  2155. minioninfo->interrupts++;
  2156. read(minioninfo->gpiointfd, &c, 1);
  2157. // applog(LOG_ERR, "%s%i: Interrupt2",
  2158. // minioncgpu->drv->name,
  2159. // minioncgpu->device_id);
  2160. gotres = false;
  2161. for (chip = 0; chip < MINION_CHIPS; chip++) {
  2162. if (minioninfo->chip[chip]) {
  2163. SET_HEAD_READ(head, MINION_SYS_INT_STA);
  2164. head->chip = chip;
  2165. reply = do_ioctl(wbuf, wsiz, rbuf, rsiz);
  2166. if (reply != (int)wsiz) {
  2167. applog(LOG_ERR, "%s: chip %d int status returned %d"
  2168. " (should be %d)",
  2169. minioncgpu->drv->dname,
  2170. chip, reply, (int)wsiz);
  2171. }
  2172. snprintf(minioninfo->last_interrupt,
  2173. sizeof(minioninfo->last_interrupt),
  2174. "%d %d 0x%02x%02x%02x%02x%02x%02x%02x%02x %d %d 0x%02x %d %d",
  2175. (int)(minioninfo->interrupts), chip,
  2176. rbuf[0], rbuf[1], rbuf[2], rbuf[3],
  2177. rbuf[4], rbuf[5], rbuf[6], rbuf[7],
  2178. (int)wsiz, (int)rsiz, rbuf[wsiz - rsiz],
  2179. rbuf[wsiz - rsiz] & MINION_RESULT_INT,
  2180. rbuf[wsiz - rsiz] & MINION_CMD_INT);
  2181. if ((rbuf[wsiz - rsiz] & MINION_RESULT_INT) != 0) {
  2182. gotres = true;
  2183. (minioninfo->result_interrupts)++;
  2184. // applog(LOG_ERR, "%s%i: chip %d got RES interrupt",
  2185. // minioncgpu->drv->name,
  2186. // minioncgpu->device_id,
  2187. // chip);
  2188. }
  2189. if ((rbuf[wsiz - rsiz] & MINION_CMD_INT) != 0) {
  2190. // Work queue is empty
  2191. (minioninfo->command_interrupts)++;
  2192. // applog(LOG_ERR, "%s%i: chip %d got CMD interrupt",
  2193. // minioncgpu->drv->name,
  2194. // minioncgpu->device_id,
  2195. // chip);
  2196. }
  2197. // char *tmp;
  2198. // tmp = bin2hex(rbuf, wsiz);
  2199. // applog(LOG_ERR, "%s%i: chip %d interrupt: %s",
  2200. // minioncgpu->drv->name,
  2201. // minioncgpu->device_id,
  2202. // chip, tmp);
  2203. // free(tmp);
  2204. // Don't clear either interrupt until after send/recv
  2205. }
  2206. }
  2207. // Doing this last means we can't miss an interrupt
  2208. if (gotres)
  2209. cgsem_post(&(minioninfo->scan_work));
  2210. }
  2211. #endif
  2212. }
  2213. return NULL;
  2214. }
  2215. /*
  2216. * Find the matching work item for this chip
  2217. * Discard any older work items for this chip
  2218. */
  2219. enum nonce_state {
  2220. NONCE_GOOD_NONCE,
  2221. NONCE_NO_NONCE,
  2222. NONCE_BAD_NONCE,
  2223. NONCE_BAD_WORK,
  2224. NONCE_NO_WORK,
  2225. NONCE_SPI_ERR
  2226. };
  2227. static void cleanup_older(struct cgpu_info *minioncgpu, int chip, K_ITEM *item, bool no_nonce)
  2228. {
  2229. struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
  2230. K_ITEM *tail;
  2231. // bool errs;
  2232. /*
  2233. * remove older work items (no_nonce means this 'item' has finished also)
  2234. */
  2235. if (item->next || no_nonce) {
  2236. K_WLOCK(minioninfo->wchip_list[chip]);
  2237. tail = minioninfo->wchip_list[chip]->tail;
  2238. while (tail && tail != item) {
  2239. k_unlink_item(minioninfo->wchip_list[chip], tail);
  2240. if (!(DATAW(tail)->stale)) {
  2241. minioninfo->chip_status[chip].chipwork--;
  2242. if (minioninfo->chip_status[chip].realwork > 0)
  2243. minioninfo->chip_status[chip].realwork--;
  2244. /*
  2245. // If it had no valid work (only errors) then it won't have been cleaned up
  2246. errs = (DATAW(tail)->errors > 0);
  2247. applog(errs ? LOG_DEBUG : LOG_ERR,
  2248. applog(LOG_ERR,
  2249. "%s%i: discarded old task 0x%04x chip %d no reply errs=%d",
  2250. minioncgpu->drv->name, minioncgpu->device_id,
  2251. DATAW(tail)->task_id, chip, DATAW(tail)->errors);
  2252. */
  2253. }
  2254. K_WUNLOCK(minioninfo->wchip_list[chip]);
  2255. applog(MINION_LOG, "%s%i: marking complete - old task 0x%04x chip %d",
  2256. minioncgpu->drv->name, minioncgpu->device_id,
  2257. DATAW(tail)->task_id, chip);
  2258. if (DATAW(tail)->rolled)
  2259. free_work(DATAW(tail)->work);
  2260. else
  2261. work_completed(minioncgpu, DATAW(tail)->work);
  2262. K_WLOCK(minioninfo->wchip_list[chip]);
  2263. k_free_head(minioninfo->wfree_list, tail);
  2264. tail = minioninfo->wchip_list[chip]->tail;
  2265. }
  2266. if (no_nonce) {
  2267. k_unlink_item(minioninfo->wchip_list[chip], item);
  2268. if (!(DATAW(item)->stale)) {
  2269. minioninfo->chip_status[chip].chipwork--;
  2270. if (minioninfo->chip_status[chip].realwork > 0)
  2271. minioninfo->chip_status[chip].realwork--;
  2272. }
  2273. K_WUNLOCK(minioninfo->wchip_list[chip]);
  2274. applog(MINION_LOG, "%s%i: marking complete - no_nonce task 0x%04x chip %d",
  2275. minioncgpu->drv->name, minioncgpu->device_id,
  2276. DATAW(item)->task_id, chip);
  2277. if (DATAW(item)->rolled)
  2278. free_work(DATAW(item)->work);
  2279. else
  2280. work_completed(minioncgpu, DATAW(item)->work);
  2281. K_WLOCK(minioninfo->wchip_list[chip]);
  2282. k_free_head(minioninfo->wfree_list, item);
  2283. }
  2284. K_WUNLOCK(minioninfo->wchip_list[chip]);
  2285. }
  2286. }
  2287. static enum nonce_state oknonce(struct thr_info *thr, struct cgpu_info *minioncgpu, int chip, int core,
  2288. uint32_t task_id, uint32_t nonce, bool no_nonce, struct timeval *when,
  2289. bool another, uint32_t task_id2, uint32_t nonce2)
  2290. {
  2291. struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
  2292. struct timeval now;
  2293. K_ITEM *item, *tail;
  2294. uint32_t min_task_id, max_task_id;
  2295. bool redo;
  2296. // if the chip has been disabled - but we don't do that - so not possible (yet)
  2297. if (!(minioninfo->chip[chip])) {
  2298. minioninfo->spi_errors++;
  2299. applog(MINTASK_LOG, "%s%i: nonce error chip %d not present",
  2300. minioncgpu->drv->name, minioncgpu->device_id, chip);
  2301. return NONCE_NO_WORK;
  2302. }
  2303. if (core < 0 || core >= MINION_CORES) {
  2304. minioninfo->spi_errors++;
  2305. minioninfo->res_spi_errors[chip]++;
  2306. minioninfo->res_err_count[chip]++;
  2307. applog(MINTASK_LOG, "%s%i: SPI nonce error invalid core %d (chip %d)",
  2308. minioncgpu->drv->name, minioncgpu->device_id, core, chip);
  2309. // use the fake core number so we don't discard the result
  2310. core = FAKE_CORE;
  2311. }
  2312. if (no_nonce)
  2313. minioninfo->chip_nononces[chip]++;
  2314. else
  2315. minioninfo->chip_nonces[chip]++;
  2316. redo = false;
  2317. retry:
  2318. K_RLOCK(minioninfo->wchip_list[chip]);
  2319. item = minioninfo->wchip_list[chip]->tail;
  2320. if (!item) {
  2321. K_RUNLOCK(minioninfo->wchip_list[chip]);
  2322. minioninfo->spi_errors++;
  2323. minioninfo->res_spi_errors[chip]++;
  2324. minioninfo->res_err_count[chip]++;
  2325. applog(MINTASK_LOG, "%s%i: chip %d has no tasks (core %d task 0x%04x)",
  2326. minioncgpu->drv->name, minioncgpu->device_id,
  2327. chip, core, (int)task_id);
  2328. if (!no_nonce) {
  2329. minioninfo->untested_nonces++;
  2330. minioninfo->chip_err[chip]++;
  2331. }
  2332. return NONCE_NO_WORK;
  2333. }
  2334. min_task_id = DATAW(item)->task_id;
  2335. while (item) {
  2336. if (DATAW(item)->task_id == task_id)
  2337. break;
  2338. item = item->prev;
  2339. }
  2340. max_task_id = DATAW(minioninfo->wchip_list[chip]->head)->task_id;
  2341. K_RUNLOCK(minioninfo->wchip_list[chip]);
  2342. if (!item) {
  2343. if (another && task_id != task_id2) {
  2344. minioninfo->tasks_failed[chip]++;
  2345. task_id = task_id2;
  2346. redo = true;
  2347. goto retry;
  2348. }
  2349. minioninfo->spi_errors++;
  2350. minioninfo->res_spi_errors[chip]++;
  2351. minioninfo->res_err_count[chip]++;
  2352. applog(MINTASK_LOG, "%s%i: chip %d core %d unknown task 0x%04x (min=0x%04x max=0x%04x no_nonce=%d)",
  2353. minioncgpu->drv->name, minioncgpu->device_id,
  2354. chip, core, (int)task_id, (int)min_task_id,
  2355. (int)max_task_id, no_nonce);
  2356. if (!no_nonce) {
  2357. minioninfo->untested_nonces++;
  2358. minioninfo->chip_err[chip]++;
  2359. }
  2360. return NONCE_BAD_WORK;
  2361. }
  2362. if (redo)
  2363. minioninfo->tasks_recovered[chip]++;
  2364. if (no_nonce) {
  2365. cleanup_older(minioncgpu, chip, item, no_nonce);
  2366. return NONCE_NO_NONCE;
  2367. }
  2368. minioninfo->tested_nonces++;
  2369. redo = false;
  2370. retest:
  2371. if (test_nonce(DATAW(item)->work, nonce)) {
  2372. //applog(MINTASK_LOG, "%s%i: Valid Nonce chip %d core %d task 0x%04x nonce 0x%08x", minioncgpu->drv->name, minioncgpu->device_id, chip, core, task_id, nonce);
  2373. submit_tested_work(thr, DATAW(item)->work);
  2374. if (redo)
  2375. minioninfo->nonces_recovered[chip]++;
  2376. minioninfo->chip_good[chip]++;
  2377. minioninfo->core_good[chip][core]++;
  2378. DATAW(item)->nonces++;
  2379. mutex_lock(&(minioninfo->nonce_lock));
  2380. minioninfo->new_nonces++;
  2381. mutex_unlock(&(minioninfo->nonce_lock));
  2382. minioninfo->ok_nonces++;
  2383. cleanup_older(minioncgpu, chip, item, no_nonce);
  2384. int chip_tmp;
  2385. cgtime(&now);
  2386. K_WLOCK(minioninfo->hfree_list);
  2387. item = k_unlink_head(minioninfo->hfree_list);
  2388. memcpy(&(DATAH(item)->when), when, sizeof(*when));
  2389. k_add_head(minioninfo->hchip_list[chip], item);
  2390. for (chip_tmp = 0; chip_tmp < MINION_CHIPS; chip_tmp++) {
  2391. tail = minioninfo->hchip_list[chip_tmp]->tail;
  2392. while (tail && tdiff(&(DATAH(tail)->when), &now) > MINION_HISTORY_s) {
  2393. tail = k_unlink_tail(minioninfo->hchip_list[chip_tmp]);
  2394. k_add_head(minioninfo->hfree_list, item);
  2395. tail = minioninfo->hchip_list[chip_tmp]->tail;
  2396. }
  2397. }
  2398. K_WUNLOCK(minioninfo->hfree_list);
  2399. return NONCE_GOOD_NONCE;
  2400. }
  2401. if (another && nonce != nonce2) {
  2402. minioninfo->nonces_failed[chip]++;
  2403. nonce = nonce2;
  2404. redo = true;
  2405. goto retest;
  2406. }
  2407. DATAW(item)->errors++;
  2408. minioninfo->chip_bad[chip]++;
  2409. minioninfo->core_bad[chip][core]++;
  2410. inc_hw_errors(thr);
  2411. //applog(MINTASK_LOG, "%s%i: HW ERROR chip %d core %d task 0x%04x nonce 0x%08x", minioncgpu->drv->name, minioncgpu->device_id, chip, core, task_id, nonce);
  2412. return NONCE_BAD_NONCE;
  2413. }
  2414. // Results checking thread
  2415. static void *minion_results(void *userdata)
  2416. {
  2417. struct cgpu_info *minioncgpu = (struct cgpu_info *)userdata;
  2418. struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
  2419. struct thr_info *thr = minioncgpu->thr[0];
  2420. int chip, core;
  2421. uint32_t task_id;
  2422. uint32_t nonce;
  2423. bool no_nonce;
  2424. struct timeval when;
  2425. bool another;
  2426. uint32_t task_id2;
  2427. uint32_t nonce2;
  2428. applog(MINION_LOG, "%s%i: Results...",
  2429. minioncgpu->drv->name, minioncgpu->device_id);
  2430. // Wait until we're ready
  2431. while (minioncgpu->shutdown == false) {
  2432. if (minioninfo->initialised) {
  2433. break;
  2434. }
  2435. cgsleep_ms(3);
  2436. }
  2437. while (minioncgpu->shutdown == false) {
  2438. if (!oldest_nonce(minioncgpu, &chip, &core, &task_id, &nonce,
  2439. &no_nonce, &when, &another, &task_id2, &nonce2)) {
  2440. cgsem_mswait(&(minioninfo->nonce_ready), MINION_NONCE_mS);
  2441. continue;
  2442. }
  2443. oknonce(thr, minioncgpu, chip, core, task_id, nonce, no_nonce, &when,
  2444. another, task_id2, nonce2);
  2445. }
  2446. return NULL;
  2447. }
  2448. static void minion_flush_work(struct cgpu_info *minioncgpu)
  2449. {
  2450. struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
  2451. K_ITEM *stale_unused_work, *prev_unused, *task, *prev_task, *witem;
  2452. int i;
  2453. applog(MINION_LOG, "%s%i: flushing work",
  2454. minioncgpu->drv->name, minioncgpu->device_id);
  2455. // TODO: N.B. scanwork also gets work locks - which master thread calls flush?
  2456. K_WLOCK(minioninfo->wwork_list);
  2457. // Simply remove the whole unused wwork_list
  2458. stale_unused_work = minioninfo->wwork_list->tail;
  2459. if (stale_unused_work) {
  2460. minioninfo->wwork_list->head = NULL;
  2461. minioninfo->wwork_list->tail = NULL;
  2462. minioninfo->wwork_list->count = 0;
  2463. }
  2464. // TODO: flush/work tasks should have a block sequence number so this task removal code
  2465. // might be better implemented in minion_spi_write where each work task would
  2466. // update the block sequence number and any work tasks with an old block sequence
  2467. // number would be discarded rather than sent - minion_spi_write will also need to
  2468. // prioritise flush urgent tasks above work urgent tasks - have 3 urgent states?
  2469. // They should however be 2 seperate variables in minioninfo to reduce locking
  2470. // - flush will increment one and put it in the flush task, (and work will use that)
  2471. // minion_spi_write will check/update the other and thus not need a lock
  2472. // No deadlock since this is the only code to get 2 locks
  2473. K_WLOCK(minioninfo->tfree_list);
  2474. task = minioninfo->task_list->tail;
  2475. while (task) {
  2476. prev_task = task->prev;
  2477. if (DATAT(task)->address == WRITE_ADDR(MINION_QUE_0)) {
  2478. minioninfo->chip_status[DATAT(task)->chip].quework--;
  2479. witem = DATAT(task)->witem;
  2480. k_unlink_item(minioninfo->wque_list[DATAT(task)->chip], witem);
  2481. k_free_head(minioninfo->wfree_list, witem);
  2482. k_unlink_item(minioninfo->task_list, task);
  2483. k_free_head(minioninfo->tfree_list, task);
  2484. }
  2485. task = prev_task;
  2486. }
  2487. for (i = 0; i < MINION_CHIPS; i++) {
  2488. if (minioninfo->chip[i]) {
  2489. // TODO: consider sending it now rather than adding to the task list?
  2490. task = k_unlink_head(minioninfo->tfree_list);
  2491. DATAT(task)->tid = ++(minioninfo->next_tid);
  2492. DATAT(task)->chip = i;
  2493. DATAT(task)->write = true;
  2494. DATAT(task)->address = MINION_SYS_RSTN_CTL;
  2495. DATAT(task)->task_id = 0; // ignored
  2496. DATAT(task)->wsiz = MINION_SYS_SIZ;
  2497. DATAT(task)->rsiz = 0;
  2498. DATAT(task)->wbuf[0] = SYS_RSTN_CTL_FLUSH;
  2499. DATAT(task)->wbuf[1] = 0;
  2500. DATAT(task)->wbuf[2] = 0;
  2501. DATAT(task)->wbuf[3] = 0;
  2502. DATAT(task)->urgent = true;
  2503. k_add_head(minioninfo->task_list, task);
  2504. }
  2505. }
  2506. K_WUNLOCK(minioninfo->tfree_list);
  2507. K_WUNLOCK(minioninfo->wwork_list);
  2508. // TODO: send a signal to force getting and sending new work - needs cgsem_wait in the sending thread
  2509. // TODO: should we use this thread to do the following work?
  2510. if (stale_unused_work) {
  2511. // mark complete all stale unused work (oldest first)
  2512. prev_unused = stale_unused_work;
  2513. while (prev_unused) {
  2514. if (DATAW(prev_unused)->rolled)
  2515. free_work(DATAW(prev_unused)->work);
  2516. else
  2517. work_completed(minioncgpu, DATAW(prev_unused)->work);
  2518. prev_unused = prev_unused->prev;
  2519. }
  2520. // put the items back in the wfree_list (oldest first)
  2521. K_WLOCK(minioninfo->wfree_list);
  2522. while (stale_unused_work) {
  2523. prev_unused = stale_unused_work->prev;
  2524. k_free_head(minioninfo->wfree_list, stale_unused_work);
  2525. stale_unused_work = prev_unused;
  2526. }
  2527. K_WUNLOCK(minioninfo->wfree_list);
  2528. }
  2529. }
  2530. static void sys_chip_sta(struct cgpu_info *minioncgpu, int chip)
  2531. {
  2532. struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
  2533. struct timeval now;
  2534. K_ITEM *item;
  2535. int limit, rep;
  2536. cgtime(&now);
  2537. // No lock required since 'last' is only accessed here
  2538. if (minioninfo->chip_status[chip].last.tv_sec == 0) {
  2539. memcpy(&(minioninfo->chip_status[chip].last), &now, sizeof(now));
  2540. } else {
  2541. limit = MINION_STATS_UPDATE_TIME_mS +
  2542. (int)(random() % MINION_STATS_UPDATE_RAND_mS);
  2543. if (ms_tdiff(&now, &(minioninfo->chip_status[chip].last)) > limit) {
  2544. memcpy(&(minioninfo->chip_status[chip].last), &now, sizeof(now));
  2545. K_WLOCK(minioninfo->tfree_list);
  2546. item = k_unlink_head(minioninfo->tfree_list);
  2547. DATAT(item)->tid = ++(minioninfo->next_tid);
  2548. K_WUNLOCK(minioninfo->tfree_list);
  2549. DATAT(item)->chip = chip;
  2550. DATAT(item)->write = false;
  2551. DATAT(item)->address = READ_ADDR(MINION_SYS_CHIP_STA);
  2552. DATAT(item)->task_id = 0;
  2553. DATAT(item)->wsiz = 0;
  2554. DATAT(item)->rsiz = MINION_SYS_SIZ;
  2555. DATAT(item)->urgent = false;
  2556. K_WLOCK(minioninfo->task_list);
  2557. k_add_head(minioninfo->task_list, item);
  2558. item = k_unlink_head(minioninfo->tfree_list);
  2559. DATAT(item)->tid = ++(minioninfo->next_tid);
  2560. K_WUNLOCK(minioninfo->task_list);
  2561. DATAT(item)->chip = chip;
  2562. DATAT(item)->write = false;
  2563. DATAT(item)->address = READ_ADDR(MINION_SYS_IDLE_CNT);
  2564. DATAT(item)->task_id = 0;
  2565. DATAT(item)->wsiz = 0;
  2566. DATAT(item)->rsiz = MINION_SYS_SIZ;
  2567. DATAT(item)->urgent = false;
  2568. K_WLOCK(minioninfo->task_list);
  2569. k_add_head(minioninfo->task_list, item);
  2570. K_WUNLOCK(minioninfo->task_list);
  2571. // Get the core ena and act state
  2572. for (rep = 0; rep < MINION_CORE_REPS; rep++) {
  2573. // Ena
  2574. K_WLOCK(minioninfo->tfree_list);
  2575. item = k_unlink_head(minioninfo->tfree_list);
  2576. DATAT(item)->tid = ++(minioninfo->next_tid);
  2577. K_WUNLOCK(minioninfo->tfree_list);
  2578. DATAT(item)->chip = chip;
  2579. DATAT(item)->write = false;
  2580. DATAT(item)->address = READ_ADDR(MINION_CORE_ENA0_31 + rep);
  2581. DATAT(item)->task_id = 0;
  2582. DATAT(item)->wsiz = 0;
  2583. DATAT(item)->rsiz = MINION_SYS_SIZ;
  2584. DATAT(item)->urgent = false;
  2585. K_WLOCK(minioninfo->task_list);
  2586. k_add_head(minioninfo->task_list, item);
  2587. // Act
  2588. item = k_unlink_head(minioninfo->tfree_list);
  2589. DATAT(item)->tid = ++(minioninfo->next_tid);
  2590. K_WUNLOCK(minioninfo->task_list);
  2591. DATAT(item)->chip = chip;
  2592. DATAT(item)->write = false;
  2593. DATAT(item)->address = READ_ADDR(MINION_CORE_ACT0_31 + rep);
  2594. DATAT(item)->task_id = 0;
  2595. DATAT(item)->wsiz = 0;
  2596. DATAT(item)->rsiz = MINION_SYS_SIZ;
  2597. DATAT(item)->urgent = false;
  2598. K_WLOCK(minioninfo->task_list);
  2599. k_add_head(minioninfo->task_list, item);
  2600. K_WUNLOCK(minioninfo->task_list);
  2601. }
  2602. }
  2603. }
  2604. }
  2605. static void new_work_task(struct cgpu_info *minioncgpu, K_ITEM *witem, int chip, bool urgent, uint8_t state)
  2606. {
  2607. struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
  2608. struct minion_que *que;
  2609. K_ITEM *item;
  2610. K_WLOCK(minioninfo->tfree_list);
  2611. item = k_unlink_head(minioninfo->tfree_list);
  2612. DATAT(item)->tid = ++(minioninfo->next_tid);
  2613. K_WUNLOCK(minioninfo->tfree_list);
  2614. DATAT(item)->chip = chip;
  2615. DATAT(item)->write = true;
  2616. DATAT(item)->address = MINION_QUE_0;
  2617. // if threaded access to new_work_task() is added, this will need locking
  2618. // Don't use task_id 0 so that we can ignore all '0' work replies
  2619. // ... and report them as errors
  2620. if (minioninfo->next_task_id == 0)
  2621. minioninfo->next_task_id = 1;
  2622. DATAT(item)->task_id = minioninfo->next_task_id;
  2623. DATAW(witem)->task_id = minioninfo->next_task_id;
  2624. minioninfo->next_task_id = (minioninfo->next_task_id + 1) & MINION_MAX_TASK_ID;
  2625. DATAT(item)->urgent = urgent;
  2626. DATAT(item)->work_state = state;
  2627. DATAT(item)->work = DATAW(witem)->work;
  2628. DATAT(item)->witem = witem;
  2629. que = (struct minion_que *)&(DATAT(item)->wbuf[0]);
  2630. que->task_id[0] = DATAT(item)->task_id & 0xff;
  2631. que->task_id[1] = (DATAT(item)->task_id & 0xff00) >> 8;
  2632. memcpy(&(que->midstate[0]), &(DATAW(witem)->work->midstate[0]), MIDSTATE_BYTES);
  2633. memcpy(&(que->merkle7[0]), &(DATAW(witem)->work->data[MERKLE7_OFFSET]), MERKLE_BYTES);
  2634. DATAT(item)->wsiz = (int)sizeof(*que);
  2635. DATAT(item)->rsiz = 0;
  2636. K_WLOCK(minioninfo->wque_list[chip]);
  2637. k_add_head(minioninfo->wque_list[chip], witem);
  2638. minioninfo->chip_status[chip].quework++;
  2639. K_WUNLOCK(minioninfo->wque_list[chip]);
  2640. K_WLOCK(minioninfo->task_list);
  2641. k_add_head(minioninfo->task_list, item);
  2642. K_WUNLOCK(minioninfo->task_list);
  2643. if (urgent)
  2644. cgsem_post(&(minioninfo->task_ready));
  2645. // N.B. this will only update often enough if a chip is > ~2GH/s
  2646. if (!urgent)
  2647. sys_chip_sta(minioncgpu, chip);
  2648. }
  2649. // TODO: stale work ...
  2650. static K_ITEM *next_work(struct minion_info *minioninfo)
  2651. {
  2652. K_ITEM *item;
  2653. K_WLOCK(minioninfo->wwork_list);
  2654. item = k_unlink_tail(minioninfo->wwork_list);
  2655. K_WUNLOCK(minioninfo->wwork_list);
  2656. return item;
  2657. }
  2658. static void minion_do_work(struct cgpu_info *minioncgpu)
  2659. {
  2660. struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
  2661. int count, chip, j, lowcount;
  2662. uint8_t state;
  2663. K_ITEM *item;
  2664. #if ENABLE_INT_NONO
  2665. K_ITEM *task;
  2666. #endif
  2667. bool islow, sentwork;
  2668. // TODO: (remove this) Fake starved of work to test CMD Interrupt
  2669. // if (total_secs > 120) {
  2670. // cgsleep_ms(888);
  2671. // return;
  2672. // }
  2673. /*
  2674. * Fill the queues as follows:
  2675. * 1) put at least 1 in each queue or if islow then add 1
  2676. * 2) push each queue up to LOW or if count is high but islow, then add LOW-1
  2677. * 3) push each LOW queue up to HIGH
  2678. */
  2679. sentwork = false;
  2680. for (state = 0; state < 3; state++) {
  2681. #define CHP 0
  2682. //applog(LOG_ERR, "%s%i: chip %d presta %d: quew %d chw %d", minioncgpu->drv->name, minioncgpu->device_id, CHP, state, minioninfo->chip_status[CHP].quework, minioninfo->chip_status[CHP].chipwork);
  2683. for (chip = 0; chip < MINION_CHIPS; chip++)
  2684. minioninfo->chip_status[chip].tohigh = false;
  2685. for (chip = 0; chip < MINION_CHIPS; chip++) {
  2686. if (minioninfo->chip[chip] && !minioninfo->chip_status[chip].overheat) {
  2687. K_WLOCK(minioninfo->wchip_list[chip]);
  2688. count = minioninfo->chip_status[chip].quework +
  2689. minioninfo->chip_status[chip].realwork;
  2690. islow = minioninfo->chip_status[chip].islow;
  2691. minioninfo->chip_status[chip].islow = false;
  2692. lowcount = minioninfo->chip_status[chip].lowcount;
  2693. K_WUNLOCK(minioninfo->wchip_list[chip]);
  2694. switch (state) {
  2695. case 0:
  2696. if (count == 0 || islow) {
  2697. item = next_work(minioninfo);
  2698. if (item) {
  2699. new_work_task(minioncgpu, item, chip, true, state);
  2700. sentwork = true;
  2701. applog(MINION_LOG, "%s%i: 0 task 0x%04x in chip %d list",
  2702. minioncgpu->drv->name,
  2703. minioncgpu->device_id,
  2704. DATAW(item)->task_id, chip);
  2705. } else {
  2706. applog(LOG_ERR, "%s%i: chip %d urgent empty work list",
  2707. minioncgpu->drv->name,
  2708. minioncgpu->device_id,
  2709. chip);
  2710. }
  2711. }
  2712. break;
  2713. case 1:
  2714. if (count < MINION_QUE_LOW || islow) {
  2715. // do case 2: after we've done other chips
  2716. minioninfo->chip_status[chip].tohigh = true;
  2717. j = count;
  2718. if (count >= MINION_QUE_LOW) {
  2719. // islow means run a full case 1
  2720. j = 1;
  2721. applog(LOG_ERR, "%s%i: chip %d low que (%d) with high count %d",
  2722. minioncgpu->drv->name,
  2723. minioncgpu->device_id,
  2724. chip, lowcount, count);
  2725. }
  2726. for (; j < MINION_QUE_LOW; j++) {
  2727. item = next_work(minioninfo);
  2728. if (item) {
  2729. new_work_task(minioncgpu, item, chip, false, state);
  2730. sentwork = true;
  2731. applog(MINION_LOG, "%s%i: 1 task 0x%04x in chip %d list",
  2732. minioncgpu->drv->name,
  2733. minioncgpu->device_id,
  2734. DATAW(item)->task_id, chip);
  2735. } else {
  2736. applog(LOG_ERR, "%s%i: chip %d non-urgent lo "
  2737. "empty work list (count=%d)",
  2738. minioncgpu->drv->name,
  2739. minioncgpu->device_id,
  2740. chip, j);
  2741. }
  2742. }
  2743. }
  2744. break;
  2745. case 2:
  2746. if (count <= MINION_QUE_LOW || minioninfo->chip_status[chip].tohigh) {
  2747. for (j = count; j < MINION_QUE_HIGH; j++) {
  2748. item = next_work(minioninfo);
  2749. if (item) {
  2750. new_work_task(minioncgpu, item, chip, false, state);
  2751. sentwork = true;
  2752. applog(MINION_LOG, "%s%i: 2 task 0x%04x in chip %d list",
  2753. minioncgpu->drv->name,
  2754. minioncgpu->device_id,
  2755. DATAW(item)->task_id, chip);
  2756. } else {
  2757. applog(LOG_ERR, "%s%i: chip %d non-urgent hi "
  2758. "empty work list (count=%d)",
  2759. minioncgpu->drv->name,
  2760. minioncgpu->device_id,
  2761. chip, j);
  2762. }
  2763. }
  2764. }
  2765. break;
  2766. }
  2767. } else
  2768. if (minioninfo->chip[chip] && minioninfo->chip_status[chip].overheat && state == 2)
  2769. sys_chip_sta(minioncgpu, chip);
  2770. }
  2771. }
  2772. sentwork = sentwork;
  2773. #if ENABLE_INT_NONO
  2774. if (sentwork) {
  2775. // Clear CMD interrupt since we've now sent more
  2776. K_WLOCK(minioninfo->tfree_list);
  2777. task = k_unlink_head(minioninfo->tfree_list);
  2778. DATAT(task)->tid = ++(minioninfo->next_tid);
  2779. DATAT(task)->chip = 0; // ignored
  2780. DATAT(task)->write = true;
  2781. DATAT(task)->address = MINION_SYS_INT_CLR;
  2782. DATAT(task)->task_id = 0; // ignored
  2783. DATAT(task)->wsiz = MINION_SYS_SIZ;
  2784. DATAT(task)->rsiz = 0;
  2785. DATAT(task)->wbuf[0] = MINION_CMD_INT;
  2786. DATAT(task)->wbuf[1] = 0;
  2787. DATAT(task)->wbuf[2] = 0;
  2788. DATAT(task)->wbuf[3] = 0;
  2789. DATAT(task)->urgent = false;
  2790. k_add_head(minioninfo->task_list, task);
  2791. K_WUNLOCK(minioninfo->tfree_list);
  2792. }
  2793. #endif
  2794. //applog(LOG_ERR, "%s%i: chip %d fin: quew %d chw %d", minioncgpu->drv->name, minioncgpu->device_id, CHP, minioninfo->chip_status[CHP].quework, minioninfo->chip_status[CHP].chipwork);
  2795. }
  2796. static bool minion_thread_prepare(struct thr_info *thr)
  2797. {
  2798. struct cgpu_info *minioncgpu = thr->cgpu;
  2799. struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
  2800. /*
  2801. * SPI/ioctl write thread
  2802. */
  2803. if (thr_info_create(&(minioninfo->spiw_thr), NULL, minion_spi_write, (void *)minioncgpu)) {
  2804. applog(LOG_ERR, "%s%i: SPI write thread create failed",
  2805. minioncgpu->drv->name, minioncgpu->device_id);
  2806. return false;
  2807. }
  2808. pthread_detach(minioninfo->spiw_thr.pth);
  2809. /*
  2810. * SPI/ioctl results thread
  2811. */
  2812. if (thr_info_create(&(minioninfo->spir_thr), NULL, minion_spi_reply, (void *)minioncgpu)) {
  2813. applog(LOG_ERR, "%s%i: SPI reply thread create failed",
  2814. minioncgpu->drv->name, minioncgpu->device_id);
  2815. return false;
  2816. }
  2817. pthread_detach(minioninfo->spir_thr.pth);
  2818. /*
  2819. * Seperate results checking thread so ioctl timing can ignore the results checking
  2820. */
  2821. if (thr_info_create(&(minioninfo->res_thr), NULL, minion_results, (void *)minioncgpu)) {
  2822. applog(LOG_ERR, "%s%i: Results thread create failed",
  2823. minioncgpu->drv->name, minioncgpu->device_id);
  2824. return false;
  2825. }
  2826. pthread_detach(minioninfo->res_thr.pth);
  2827. return true;
  2828. }
  2829. static void minion_shutdown(struct thr_info *thr)
  2830. {
  2831. struct cgpu_info *minioncgpu = thr->cgpu;
  2832. struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
  2833. int i;
  2834. applog(MINION_LOG, "%s%i: shutting down",
  2835. minioncgpu->drv->name, minioncgpu->device_id);
  2836. for (i = 0; i < MINION_CHIPS; i++)
  2837. if (minioninfo->chip[i])
  2838. // TODO: minion_shutdown(minioncgpu, minioninfo, i);
  2839. i = i;
  2840. minioncgpu->shutdown = true;
  2841. }
  2842. static bool minion_queue_full(struct cgpu_info *minioncgpu)
  2843. {
  2844. struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
  2845. struct work *work, *usework;
  2846. int count, need, roll, roll_limit;
  2847. bool ret, rolled;
  2848. K_RLOCK(minioninfo->wwork_list);
  2849. count = minioninfo->wwork_list->count;
  2850. K_RUNLOCK(minioninfo->wwork_list);
  2851. if (count >= (MINION_QUE_HIGH * minioninfo->chips))
  2852. ret = true;
  2853. else {
  2854. need = (MINION_QUE_HIGH * minioninfo->chips) - count;
  2855. work = get_queued(minioncgpu);
  2856. if (work) {
  2857. roll_limit = work->drv_rolllimit;
  2858. roll = 0;
  2859. do {
  2860. if (roll == 0) {
  2861. usework = work;
  2862. minioninfo->work_unrolled++;
  2863. rolled = false;
  2864. } else {
  2865. usework = copy_work_noffset(work, roll);
  2866. minioninfo->work_rolled++;
  2867. rolled = true;
  2868. }
  2869. ready_work(minioncgpu, usework, rolled);
  2870. } while (--need > 0 && ++roll <= roll_limit);
  2871. } else {
  2872. // Avoid a hard loop when we can't get work fast enough
  2873. cgsleep_us(42);
  2874. }
  2875. if (need > 0)
  2876. ret = false;
  2877. else
  2878. ret = true;
  2879. }
  2880. return ret;
  2881. }
  2882. static void idle_report(struct cgpu_info *minioncgpu)
  2883. {
  2884. struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
  2885. struct timeval now;
  2886. uint32_t idle;
  2887. int msdiff;
  2888. int chip;
  2889. for (chip = 0; chip < MINION_CHIPS; chip++) {
  2890. if (minioninfo->chip[chip]) {
  2891. idle = minioninfo->chip_status[chip].idle;
  2892. if (idle != minioninfo->chip_status[chip].last_rpt_idle) {
  2893. cgtime(&now);
  2894. msdiff = ms_tdiff(&now, &(minioninfo->chip_status[chip].idle_rpt));
  2895. if (msdiff >= MINION_IDLE_MESSAGE_ms) {
  2896. memcpy(&(minioninfo->chip_status[chip].idle_rpt), &now, sizeof(now));
  2897. applog(LOG_WARNING,
  2898. "%s%d: chip %d internal idle changed %08x",
  2899. minioncgpu->drv->name, minioncgpu->device_id,
  2900. chip, idle);
  2901. minioninfo->chip_status[chip].last_rpt_idle = idle;
  2902. }
  2903. }
  2904. }
  2905. }
  2906. }
  2907. static void chip_report(struct cgpu_info *minioncgpu)
  2908. {
  2909. struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
  2910. struct timeval now;
  2911. char buf[512];
  2912. char res_err_msg[2];
  2913. size_t len;
  2914. double ghs, expect, howlong;
  2915. int msdiff;
  2916. int chip;
  2917. int res_err_count;
  2918. cgtime(&now);
  2919. if (!(minioninfo->chip_chk.tv_sec)) {
  2920. memcpy(&(minioninfo->chip_chk), &now, sizeof(now));
  2921. memcpy(&(minioninfo->chip_rpt), &now, sizeof(now));
  2922. return;
  2923. }
  2924. if (opt_minion_chipreport > 0) {
  2925. msdiff = ms_tdiff(&now, &(minioninfo->chip_rpt));
  2926. if (msdiff >= (opt_minion_chipreport * 1000)) {
  2927. buf[0] = '\0';
  2928. res_err_msg[0] = '\0';
  2929. res_err_msg[1] = '\0';
  2930. K_RLOCK(minioninfo->hfree_list);
  2931. for (chip = 0; chip < MINION_CHIPS; chip++) {
  2932. if (minioninfo->chip[chip]) {
  2933. len = strlen(buf);
  2934. if (minioninfo->hchip_list[chip]->count < 2)
  2935. ghs = 0.0;
  2936. else {
  2937. ghs = 0xffffffffull * (minioninfo->hchip_list[chip]->count - 1);
  2938. ghs /= 1000000000.0;
  2939. ghs /= tdiff(&now, &(DATAH(minioninfo->hchip_list[chip]->tail)->when));
  2940. }
  2941. res_err_count = minioninfo->res_err_count[chip];
  2942. minioninfo->res_err_count[chip] = 0;
  2943. if (res_err_count > 100)
  2944. res_err_msg[0] = '!';
  2945. else if (res_err_count > 50)
  2946. res_err_msg[0] = '*';
  2947. else if (res_err_count > 0)
  2948. res_err_msg[0] = '\'';
  2949. else
  2950. res_err_msg[0] = '\0';
  2951. snprintf(buf + len, sizeof(buf) - len,
  2952. " %d=%s%.2f", chip, res_err_msg, ghs);
  2953. minioninfo->history_ghs[chip] = ghs;
  2954. }
  2955. }
  2956. K_RUNLOCK(minioninfo->hfree_list);
  2957. memcpy(&(minioninfo->chip_chk), &now, sizeof(now));
  2958. applogsiz(LOG_WARNING, 512,
  2959. "%s%d: Chip GHs%s",
  2960. minioncgpu->drv->name, minioncgpu->device_id, buf);
  2961. memcpy(&(minioninfo->chip_rpt), &now, sizeof(now));
  2962. }
  2963. }
  2964. msdiff = ms_tdiff(&now, &(minioninfo->chip_chk));
  2965. if (total_secs >= MINION_HISTORY_s && msdiff >= (minioninfo->history_gen * 1000)) {
  2966. K_RLOCK(minioninfo->hfree_list);
  2967. for (chip = 0; chip < MINION_CHIPS; chip++) {
  2968. if (minioninfo->chip[chip]) {
  2969. if (minioninfo->hchip_list[chip]->count < 2)
  2970. ghs = 0.0;
  2971. else {
  2972. ghs = 0xffffffffull * (minioninfo->hchip_list[chip]->count - 1);
  2973. ghs /= 1000000000.0;
  2974. ghs /= tdiff(&now, &(DATAH(minioninfo->hchip_list[chip]->tail)->when));
  2975. }
  2976. expect = (double)(minioninfo->init_freq[chip]) *
  2977. MINION_RESET_PERCENT / 1000.0;
  2978. howlong = tdiff(&now, &(minioninfo->last_reset[chip]));
  2979. if (ghs <= expect && howlong >= MINION_HISTORY_s)
  2980. minioninfo->do_reset[chip] = expect;
  2981. minioninfo->history_ghs[chip] = ghs;
  2982. }
  2983. }
  2984. K_RUNLOCK(minioninfo->hfree_list);
  2985. for (chip = 0; chip < MINION_CHIPS; chip++) {
  2986. if (minioninfo->chip[chip]) {
  2987. if (minioninfo->do_reset[chip] > 1.0) {
  2988. applog(LOG_WARNING, "%s%d: Chip %d down to threshold %.2fGHs - resetting ...",
  2989. minioncgpu->drv->name, minioncgpu->device_id,
  2990. chip, minioninfo->do_reset[chip]);
  2991. minioninfo->do_reset[chip] = 0.0;
  2992. memcpy(&(minioninfo->last_reset[chip]), &now, sizeof(now));
  2993. init_chip(minioncgpu, minioninfo, chip);
  2994. }
  2995. }
  2996. }
  2997. memcpy(&(minioninfo->chip_chk), &now, sizeof(now));
  2998. }
  2999. }
  3000. static int64_t minion_scanwork(__maybe_unused struct thr_info *thr)
  3001. {
  3002. struct cgpu_info *minioncgpu = thr->cgpu;
  3003. struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
  3004. int64_t hashcount = 0;
  3005. minion_do_work(minioncgpu);
  3006. mutex_lock(&(minioninfo->nonce_lock));
  3007. if (minioninfo->new_nonces) {
  3008. hashcount += 0xffffffffull * minioninfo->new_nonces;
  3009. minioninfo->new_nonces = 0;
  3010. }
  3011. mutex_unlock(&(minioninfo->nonce_lock));
  3012. if (opt_minion_idlecount)
  3013. idle_report(minioncgpu);
  3014. // Must always generate data to check/allow for chip reset
  3015. chip_report(minioncgpu);
  3016. /*
  3017. * To avoid wasting CPU, wait until we get an interrupt
  3018. * before returning back to the main cgminer work loop
  3019. * i.e. we then know we'll need more work
  3020. */
  3021. cgsem_mswait(&(minioninfo->scan_work), MINION_SCAN_mS);
  3022. return hashcount;
  3023. }
  3024. static const char *temp_str(uint16_t temp)
  3025. {
  3026. switch (temp) {
  3027. case MINION_TEMP_40:
  3028. return min_temp_40;
  3029. case MINION_TEMP_60:
  3030. return min_temp_60;
  3031. case MINION_TEMP_80:
  3032. return min_temp_80;
  3033. case MINION_TEMP_100:
  3034. return min_temp_100;
  3035. case MINION_TEMP_OVER:
  3036. return min_temp_over;
  3037. }
  3038. return min_temp_invalid;
  3039. }
  3040. static void minion_get_statline_before(char *buf, size_t bufsiz, struct cgpu_info *minioncgpu)
  3041. {
  3042. struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
  3043. uint16_t max_temp, cores;
  3044. int chip, core;
  3045. max_temp = 0;
  3046. cores = 0;
  3047. mutex_lock(&(minioninfo->sta_lock));
  3048. for (chip = 0; chip < MINION_CHIPS; chip++) {
  3049. if (minioninfo->chip[chip]) {
  3050. if (max_temp < minioninfo->chip_status[chip].temp)
  3051. max_temp = minioninfo->chip_status[chip].temp;
  3052. for (core = 0; core < MINION_CORES; core++) {
  3053. if (minioninfo->chip_core_ena[core >> 5][chip] & (0x1 << (core % 32)))
  3054. cores++;
  3055. }
  3056. }
  3057. }
  3058. mutex_unlock(&(minioninfo->sta_lock));
  3059. tailsprintf(buf, bufsiz, "max%sC Ch:%d Co:%d",
  3060. temp_str(max_temp), minioninfo->chips, (int)cores);
  3061. }
  3062. #define CHIPS_PER_STAT 5
  3063. static struct api_data *minion_api_stats(struct cgpu_info *minioncgpu)
  3064. {
  3065. struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
  3066. struct api_data *root = NULL;
  3067. char cores[MINION_CORES+1];
  3068. char data[2048];
  3069. char buf[32];
  3070. int i, to, j;
  3071. int chip, max_chip, que_work, chip_work, temp;
  3072. if (minioninfo->initialised == false)
  3073. return NULL;
  3074. root = api_add_uint64(root, "OK Nonces", &(minioninfo->ok_nonces), true);
  3075. root = api_add_uint64(root, "New Nonces", &(minioninfo->new_nonces), true);
  3076. root = api_add_uint64(root, "Tested Nonces", &(minioninfo->tested_nonces), true);
  3077. root = api_add_uint64(root, "Untested Nonces", &(minioninfo->untested_nonces), true);
  3078. root = api_add_int(root, "Chips", &(minioninfo->chips), true);
  3079. max_chip = 0;
  3080. for (chip = 0; chip < MINION_CHIPS; chip++)
  3081. if (minioninfo->chip[chip]) {
  3082. max_chip = chip;
  3083. snprintf(buf, sizeof(buf), "Chip %d Temperature", chip);
  3084. root = api_add_const(root, buf, temp_str(minioninfo->chip_status[chip].temp), false);
  3085. snprintf(buf, sizeof(buf), "Chip %d Cores", chip);
  3086. root = api_add_uint16(root, buf, &(minioninfo->chip_status[chip].cores), true);
  3087. snprintf(buf, sizeof(buf), "Chip %d Frequency", chip);
  3088. root = api_add_uint32(root, buf, &(minioninfo->chip_status[chip].freq), true);
  3089. snprintf(buf, sizeof(buf), "Chip %d InitFreq", chip);
  3090. root = api_add_int(root, buf, &(minioninfo->init_freq[chip]), true);
  3091. snprintf(buf, sizeof(buf), "Chip %d FreqSent", chip);
  3092. root = api_add_hex32(root, buf, &(minioninfo->chip_status[chip].freqsent), true);
  3093. snprintf(buf, sizeof(buf), "Chip %d InitTemp", chip);
  3094. temp = minioninfo->init_temp[chip];
  3095. if (temp == MINION_TEMP_CTL_DISABLE)
  3096. root = api_add_string(root, buf, MINION_TEMP_DISABLE, true);
  3097. else {
  3098. snprintf(data, sizeof(data), "%d", temp);
  3099. root = api_add_string(root, buf, data, true);
  3100. }
  3101. snprintf(buf, sizeof(buf), "Chip %d TempSent", chip);
  3102. root = api_add_hex32(root, buf, &(minioninfo->chip_status[chip].tempsent), true);
  3103. __bin2hex(data, (unsigned char *)(&(minioninfo->init_cores[chip][0])),
  3104. sizeof(minioninfo->init_cores[chip]));
  3105. snprintf(buf, sizeof(buf), "Chip %d InitCores", chip);
  3106. root = api_add_string(root, buf, data, true);
  3107. snprintf(buf, sizeof(buf), "Chip %d IdleCount", chip);
  3108. root = api_add_hex32(root, buf, &(minioninfo->chip_status[chip].idle), true);
  3109. snprintf(buf, sizeof(buf), "Chip %d QueWork", chip);
  3110. root = api_add_uint32(root, buf, &(minioninfo->chip_status[chip].quework), true);
  3111. snprintf(buf, sizeof(buf), "Chip %d ChipWork", chip);
  3112. root = api_add_uint32(root, buf, &(minioninfo->chip_status[chip].chipwork), true);
  3113. snprintf(buf, sizeof(buf), "Chip %d RealWork", chip);
  3114. root = api_add_uint32(root, buf, &(minioninfo->chip_status[chip].realwork), true);
  3115. snprintf(buf, sizeof(buf), "Chip %d QueListCount", chip);
  3116. root = api_add_int(root, buf, &(minioninfo->wque_list[chip]->count), true);
  3117. snprintf(buf, sizeof(buf), "Chip %d WorkListCount", chip);
  3118. root = api_add_int(root, buf, &(minioninfo->wchip_list[chip]->count), true);
  3119. snprintf(buf, sizeof(buf), "Chip %d Overheat", chip);
  3120. root = api_add_bool(root, buf, &(minioninfo->chip_status[chip].overheat), true);
  3121. snprintf(buf, sizeof(buf), "Chip %d Overheats", chip);
  3122. root = api_add_uint32(root, buf, &(minioninfo->chip_status[chip].overheats), true);
  3123. snprintf(buf, sizeof(buf), "Chip %d LastOverheat", chip);
  3124. root = api_add_timeval(root, buf, &(minioninfo->chip_status[chip].lastoverheat), true);
  3125. snprintf(buf, sizeof(buf), "Chip %d LastRecover", chip);
  3126. root = api_add_timeval(root, buf, &(minioninfo->chip_status[chip].lastrecover), true);
  3127. snprintf(buf, sizeof(buf), "Chip %d OverheatIdle", chip);
  3128. root = api_add_double(root, buf, &(minioninfo->chip_status[chip].overheattime), true);
  3129. for (i = 0; i < MINION_CORES; i++) {
  3130. if (minioninfo->chip_core_ena[i >> 5][chip] & (0x1 << (i % 32)))
  3131. cores[i] = 'o';
  3132. else
  3133. cores[i] = 'x';
  3134. }
  3135. cores[MINION_CORES] = '\0';
  3136. snprintf(buf, sizeof(buf), "Chip %d CoresEna", chip);
  3137. root = api_add_string(root, buf, cores, true);
  3138. for (i = 0; i < MINION_CORES; i++) {
  3139. if (minioninfo->chip_core_act[i >> 5][chip] & (0x1 << (i % 32)))
  3140. cores[i] = '-';
  3141. else
  3142. cores[i] = 'o';
  3143. }
  3144. cores[MINION_CORES] = '\0';
  3145. snprintf(buf, sizeof(buf), "Chip %d CoresAct", chip);
  3146. root = api_add_string(root, buf, cores, true);
  3147. snprintf(buf, sizeof(buf), "Chip %d History GHs", chip);
  3148. root = api_add_mhs(root, buf, &(minioninfo->history_ghs[chip]), true);
  3149. }
  3150. double his = MINION_HISTORY_s;
  3151. root = api_add_double(root, "History length", &his, true);
  3152. for (i = 0; i <= max_chip; i += CHIPS_PER_STAT) {
  3153. to = i + CHIPS_PER_STAT - 1;
  3154. if (to > max_chip)
  3155. to = max_chip;
  3156. data[0] = '\0';
  3157. for (j = i; j <= to; j++) {
  3158. snprintf(buf, sizeof(buf),
  3159. "%s%d",
  3160. j == i ? "" : " ",
  3161. minioninfo->chip[j] ? 1 : 0);
  3162. strcat(data, buf);
  3163. }
  3164. snprintf(buf, sizeof(buf), "Detected %02d - %02d", i, to);
  3165. root = api_add_string(root, buf, data, true);
  3166. data[0] = '\0';
  3167. for (j = i; j <= to; j++) {
  3168. snprintf(buf, sizeof(buf),
  3169. "%s%8"PRIu64,
  3170. j == i ? "" : " ",
  3171. minioninfo->chip_nonces[j]);
  3172. strcat(data, buf);
  3173. }
  3174. snprintf(buf, sizeof(buf), "Nonces %02d - %02d", i, to);
  3175. root = api_add_string(root, buf, data, true);
  3176. data[0] = '\0';
  3177. for (j = i; j <= to; j++) {
  3178. snprintf(buf, sizeof(buf),
  3179. "%s%8"PRIu64,
  3180. j == i ? "" : " ",
  3181. minioninfo->chip_nononces[j]);
  3182. strcat(data, buf);
  3183. }
  3184. snprintf(buf, sizeof(buf), "NoNonces %02d - %02d", i, to);
  3185. root = api_add_string(root, buf, data, true);
  3186. data[0] = '\0';
  3187. for (j = i; j <= to; j++) {
  3188. snprintf(buf, sizeof(buf),
  3189. "%s%8"PRIu64,
  3190. j == i ? "" : " ",
  3191. minioninfo->chip_good[j]);
  3192. strcat(data, buf);
  3193. }
  3194. snprintf(buf, sizeof(buf), "Good %02d - %02d", i, to);
  3195. root = api_add_string(root, buf, data, true);
  3196. data[0] = '\0';
  3197. for (j = i; j <= to; j++) {
  3198. snprintf(buf, sizeof(buf),
  3199. "%s%8"PRIu64,
  3200. j == i ? "" : " ",
  3201. minioninfo->chip_bad[j]);
  3202. strcat(data, buf);
  3203. }
  3204. snprintf(buf, sizeof(buf), "Bad %02d - %02d", i, to);
  3205. root = api_add_string(root, buf, data, true);
  3206. data[0] = '\0';
  3207. for (j = i; j <= to; j++) {
  3208. snprintf(buf, sizeof(buf),
  3209. "%s%8"PRIu64,
  3210. j == i ? "" : " ",
  3211. minioninfo->chip_err[j]);
  3212. strcat(data, buf);
  3213. }
  3214. snprintf(buf, sizeof(buf), "Err %02d - %02d", i, to);
  3215. root = api_add_string(root, buf, data, true);
  3216. data[0] = '\0';
  3217. for (j = i; j <= to; j++) {
  3218. snprintf(buf, sizeof(buf),
  3219. "%s%8"PRIu64,
  3220. j == i ? "" : " ",
  3221. minioninfo->fifo_spi_errors[j]);
  3222. strcat(data, buf);
  3223. }
  3224. snprintf(buf, sizeof(buf), "FifoSpiErr %02d - %02d", i, to);
  3225. root = api_add_string(root, buf, data, true);
  3226. data[0] = '\0';
  3227. for (j = i; j <= to; j++) {
  3228. snprintf(buf, sizeof(buf),
  3229. "%s%8"PRIu64,
  3230. j == i ? "" : " ",
  3231. minioninfo->res_spi_errors[j]);
  3232. strcat(data, buf);
  3233. }
  3234. snprintf(buf, sizeof(buf), "ResSpiErr %02d - %02d", i, to);
  3235. root = api_add_string(root, buf, data, true);
  3236. data[0] = '\0';
  3237. for (j = i; j <= to; j++) {
  3238. snprintf(buf, sizeof(buf),
  3239. "%s%"PRIu64"/%"PRIu64"/%"PRIu64"/%"PRIu64"/%"PRIu64,
  3240. j == i ? "" : " ",
  3241. minioninfo->use_res2[j],
  3242. minioninfo->tasks_failed[j],
  3243. minioninfo->tasks_recovered[j],
  3244. minioninfo->nonces_failed[j],
  3245. minioninfo->nonces_recovered[j]);
  3246. strcat(data, buf);
  3247. }
  3248. snprintf(buf, sizeof(buf), "Redo %02d - %02d", i, to);
  3249. root = api_add_string(root, buf, data, true);
  3250. }
  3251. que_work = chip_work = 0;
  3252. for (chip = 0; chip <= max_chip; chip++)
  3253. if (minioninfo->chip[chip]) {
  3254. que_work += minioninfo->wchip_list[chip]->count;
  3255. chip_work += minioninfo->wchip_list[chip]->count;
  3256. }
  3257. root = api_add_int(root, "WFree Total", &(minioninfo->wfree_list->total), true);
  3258. root = api_add_int(root, "WFree Count", &(minioninfo->wfree_list->count), true);
  3259. root = api_add_int(root, "WWork Count", &(minioninfo->wwork_list->count), true);
  3260. root = api_add_int(root, "WQue Count", &que_work, true);
  3261. root = api_add_int(root, "WChip Count", &chip_work, true);
  3262. root = api_add_int(root, "TFree Total", &(minioninfo->tfree_list->total), true);
  3263. root = api_add_int(root, "TFree Count", &(minioninfo->tfree_list->count), true);
  3264. root = api_add_int(root, "Task Count", &(minioninfo->task_list->count), true);
  3265. root = api_add_int(root, "Reply Count", &(minioninfo->treply_list->count), true);
  3266. root = api_add_int(root, "RFree Total", &(minioninfo->rfree_list->total), true);
  3267. root = api_add_int(root, "RFree Count", &(minioninfo->rfree_list->count), true);
  3268. root = api_add_int(root, "RNonce Count", &(minioninfo->rnonce_list->count), true);
  3269. #if DO_IO_STATS
  3270. #define sta_api(_name, _iostat) \
  3271. do { \
  3272. if ((_iostat).count) { \
  3273. float _davg = (float)((_iostat).total_delay) / (float)((_iostat).count); \
  3274. float _dlavg = (float)((_iostat).total_dlock) / (float)((_iostat).count); \
  3275. float _dlwavg = (float)((_iostat).total_dlwait) / (float)((_iostat).count); \
  3276. float _bavg = (float)((_iostat).total_bytes) / (float)((_iostat).count); \
  3277. float _tavg = (float)((_iostat).tsd) / (float)((_iostat).count); \
  3278. snprintf(data, sizeof(data), "%s Count=%"PRIu64 \
  3279. " Delay=%.0fus DAvg=%.3f" \
  3280. " DMin=%.0f DMax=%.0f DZ=%"PRIu64 \
  3281. " DLock=%.0fus DLAvg=%.3f" \
  3282. " DLMin=%.0f DLMax=%.0f DZ=%"PRIu64 \
  3283. " DLWait=%.0fus DLWAvg=%.3f" \
  3284. " Bytes=%"PRIu64" BAvg=%.3f" \
  3285. " BMin=%"PRIu64" BMax=%"PRIu64" BZ=%"PRIu64 \
  3286. " TSD=%.0fus TAvg=%.03f", \
  3287. _name, (_iostat).count, \
  3288. (_iostat).total_delay, _davg, (_iostat).min_delay, \
  3289. (_iostat).max_delay, (_iostat).zero_delay, \
  3290. (_iostat).total_dlock, _dlavg, (_iostat).min_dlock, \
  3291. (_iostat).max_dlock, (_iostat).zero_dlock, \
  3292. (_iostat).total_dlwait, _dlwavg, \
  3293. (_iostat).total_bytes, _bavg, (_iostat).min_bytes, \
  3294. (_iostat).max_bytes, (_iostat).zero_bytes, \
  3295. (_iostat).tsd, _tavg); \
  3296. root = api_add_string(root, buf, data, true); \
  3297. } \
  3298. } while(0);
  3299. for (i = 0; i < 0x200; i++) {
  3300. snprintf(buf, sizeof(buf), "Stat-0x%02x", i);
  3301. sta_api(addr2txt((uint8_t)(i & 0xff)), minioninfo->iostats[i]);
  3302. }
  3303. // Test to avoid showing applog
  3304. if (minioninfo->summary.count) {
  3305. snprintf(buf, sizeof(buf), "Stat-S");
  3306. sta_api("Summary", minioninfo->summary);
  3307. applog(LOG_WARNING, "%s %d: (%.0f) %s - %s",
  3308. minioncgpu->drv->name, minioncgpu->device_id,
  3309. total_secs, buf, data);
  3310. }
  3311. #endif
  3312. root = api_add_uint64(root, "Total SPI Errors", &(minioninfo->spi_errors), true);
  3313. root = api_add_uint64(root, "Work Unrolled", &(minioninfo->work_unrolled), true);
  3314. root = api_add_uint64(root, "Work Rolled", &(minioninfo->work_rolled), true);
  3315. root = api_add_uint64(root, "Ints", &(minioninfo->interrupts), true);
  3316. root = api_add_uint64(root, "Res Ints", &(minioninfo->result_interrupts), true);
  3317. root = api_add_uint64(root, "Cmd Ints", &(minioninfo->command_interrupts), true);
  3318. root = api_add_string(root, "Last Int", minioninfo->last_interrupt, true);
  3319. root = api_add_hex32(root, "Next TaskID", &(minioninfo->next_task_id), true);
  3320. root = api_add_elapsed(root, "Elapsed", &(total_secs), true);
  3321. return root;
  3322. }
  3323. #endif
  3324. struct device_drv minion_drv = {
  3325. .drv_id = DRIVER_minion,
  3326. .dname = "Minion BlackArrow",
  3327. .name = "MBA",
  3328. .drv_detect = minion_detect,
  3329. #ifdef LINUX
  3330. .get_api_stats = minion_api_stats,
  3331. .get_statline_before = minion_get_statline_before,
  3332. .identify_device = minion_identify,
  3333. .thread_prepare = minion_thread_prepare,
  3334. .hash_work = hash_queued_work,
  3335. .scanwork = minion_scanwork,
  3336. .queue_full = minion_queue_full,
  3337. .flush_work = minion_flush_work,
  3338. .thread_shutdown = minion_shutdown
  3339. #endif
  3340. };