driver-bab.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062
  1. /*
  2. * Copyright 2013-2014 Andrew Smith
  3. * Copyright 2013 bitfury
  4. *
  5. * BitFury GPIO code originally based on chainminer code:
  6. * https://github.com/bfsb/chainminer
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 3 of the License, or (at your option)
  11. * any later version. See COPYING for more details.
  12. */
  13. #include "config.h"
  14. #include "compat.h"
  15. #include "miner.h"
  16. #include "sha2.h"
  17. #include "klist.h"
  18. #include <ctype.h>
  19. /*
  20. * Tested on RPi running both Raspbian and Arch
  21. * with BlackArrow BitFury V1 & V2 GPIO Controller
  22. * with 16 chip BlackArrow BitFury boards
  23. */
  24. #ifndef LINUX
  25. static void bab_detect(__maybe_unused bool hotplug)
  26. {
  27. }
  28. #else
  29. #include <unistd.h>
  30. #include <linux/spi/spidev.h>
  31. #include <sys/mman.h>
  32. #include <sys/ioctl.h>
  33. #include <fcntl.h>
  34. #define BAB_SPI_BUS 0
  35. #define BAB_SPI_CHIP 0
  36. #define BAB_SPI_SPEED 96000
  37. #define BAB_SPI_BUFSIZ 1024
  38. #define BAB_DELAY_USECS 0
  39. #define BAB_TRF_DELAY 0
  40. #define BAB_ADDR(_n) (*((babinfo->gpio) + (_n)))
  41. #define BAB_INP_GPIO(_n) BAB_ADDR((_n) / 10) &= (~(7 << (((_n) % 10) * 3)))
  42. #define BAB_OUT_GPIO(_n) BAB_ADDR((_n) / 10) |= (1 << (((_n) % 10) * 3))
  43. #define BAB_OUT_GPIO_V(_n, _v) BAB_ADDR((_n) / 10) |= (((_v) <= 3 ? (_v) + 4 : \
  44. ((_v) == 4 ? 3 : 2)) << (((_n) % 10) * 3))
  45. #define BAB_GPIO_SET BAB_ADDR(7)
  46. #define BAB_GPIO_CLR BAB_ADDR(10)
  47. #define BAB_GPIO_LEVEL BAB_ADDR(13)
  48. // If the V1 test of this many chips finds no chips it will try V2
  49. #define BAB_V1_CHIP_TEST 32
  50. //maximum number of chips per board
  51. #define BAB_BOARDCHIPS 16
  52. #define BAB_MAXBUF (BAB_MAXCHIPS * 512)
  53. #define BAB_V1_BANK 0
  54. //maximum number of alternative banks
  55. #define BAB_MAXBANKS 4
  56. //maximum number of boards in a bank
  57. #define BAB_BANKBOARDS 6
  58. //maximum number of chips on alternative bank
  59. #define BAB_BANKCHIPS (BAB_BOARDCHIPS * BAB_BANKBOARDS)
  60. //maximum number of chips
  61. #define BAB_MAXCHIPS (BAB_MAXBANKS * BAB_BANKCHIPS)
  62. #define BAB_CORES 16
  63. #define BAB_X_COORD 21
  64. #define BAB_Y_COORD 36
  65. #define BAB_NOOP 0
  66. #define BAB_BREAK ((uint8_t *)"\04")
  67. #define BAB_ASYNC ((uint8_t *)"\05")
  68. #define BAB_SYNC ((uint8_t *)"\06")
  69. #define BAB_FFL " - from %s %s() line %d"
  70. #define BAB_FFL_HERE __FILE__, __func__, __LINE__
  71. #define BAB_FFL_PASS file, func, line
  72. #define bab_reset(_bank, _times) _bab_reset(babcgpu, babinfo, _bank, _times)
  73. #define bab_txrx(_item, _det) _bab_txrx(babcgpu, babinfo, _item, _det, BAB_FFL_HERE)
  74. #define bab_add_buf(_item, _data) _bab_add_buf(_item, _data, sizeof(_data)-1, BAB_FFL_HERE)
  75. #define BAB_ADD_BREAK(_item) _bab_add_buf(_item, BAB_BREAK, 1, BAB_FFL_HERE)
  76. #define BAB_ADD_ASYNC(_item) _bab_add_buf(_item, BAB_ASYNC, 1, BAB_FFL_HERE)
  77. #define bab_config_reg(_item, _reg, _ena) _bab_config_reg(_item, _reg, _ena, BAB_FFL_HERE)
  78. #define bab_add_data(_item, _addr, _data, _siz) _bab_add_data(_item, _addr, (const uint8_t *)(_data), _siz, BAB_FFL_HERE)
  79. #define BAB_ADD_NOOPs(_item, _count) _bab_add_noops(_item, _count, BAB_FFL_HERE)
  80. #define BAB_ADD_MIN 4
  81. #define BAB_ADD_MAX 128
  82. #define BAB_BASEA 4
  83. #define BAB_BASEB 61
  84. #define BAB_COUNTERS 16
  85. static const uint8_t bab_counters[BAB_COUNTERS] = {
  86. 64, 64,
  87. BAB_BASEA, BAB_BASEA+4,
  88. BAB_BASEA+2, BAB_BASEA+2+16,
  89. BAB_BASEA, BAB_BASEA+1,
  90. (BAB_BASEB)%65, (BAB_BASEB+1)%65,
  91. (BAB_BASEB+3)%65, (BAB_BASEB+3+16)%65,
  92. (BAB_BASEB+4)%65, (BAB_BASEB+4+4)%65,
  93. (BAB_BASEB+3+3)%65, (BAB_BASEB+3+1+3)%65
  94. };
  95. #define BAB_W1 16
  96. static const uint32_t bab_w1[BAB_W1] = {
  97. 0, 0, 0, 0xffffffff,
  98. 0x80000000, 0, 0, 0,
  99. 0, 0, 0, 0,
  100. 0, 0, 0, 0x00000280
  101. };
  102. #define BAB_W2 8
  103. static const uint32_t bab_w2[BAB_W2] = {
  104. 0x80000000, 0, 0, 0,
  105. 0, 0, 0, 0x00000100
  106. };
  107. #define BAB_TEST_DATA 19
  108. static const uint32_t bab_test_data[BAB_TEST_DATA] = {
  109. 0xb0e72d8e, 0x1dc5b862, 0xe9e7c4a6, 0x3050f1f5,
  110. 0x8a1a6b7e, 0x7ec384e8, 0x42c1c3fc, 0x8ed158a1,
  111. 0x8a1a6b7e, 0x6f484872, 0x4ff0bb9b, 0x12c97f07,
  112. 0xb0e72d8e, 0x55d979bc, 0x39403296, 0x40f09e84,
  113. 0x8a0bb7b7, 0x33af304f, 0x0b290c1a //, 0xf0c4e61f
  114. };
  115. /*
  116. * maximum chip speed available for auto tuner
  117. * speed/nrate/hrate/watt
  118. * 53/ 97/ 100/ 84
  119. * 54/ 98/ 107/ 88
  120. * 55/ 99/ 115/ 93
  121. * 56/ 101/ 125/ 99
  122. */
  123. #define BAB_MAXSPEED 57
  124. #define BAB_DEFMAXSPEED 55
  125. #define BAB_DEFSPEED 53
  126. #define BAB_MINSPEED 52
  127. #define BAB_ABSMINSPEED 32
  128. /*
  129. * % of errors to tune the speed up or down
  130. * 1.0 to 10.0 should average around 5.5% errors
  131. */
  132. #define BAB_TUNEUP 1.0
  133. #define BAB_TUNEDOWN 10.0
  134. #define MIDSTATE_BYTES 32
  135. #define MERKLE_OFFSET 64
  136. #define MERKLE_BYTES 12
  137. #define BLOCK_HEADER_BYTES 80
  138. #define MIDSTATE_UINTS (MIDSTATE_BYTES / sizeof(uint32_t))
  139. #define DATA_UINTS ((BLOCK_HEADER_BYTES / sizeof(uint32_t)) - 1)
  140. // Auto adjust
  141. #define BAB_AUTO_REG 0
  142. #define BAB_AUTO_VAL 0x01
  143. // iclk
  144. #define BAB_ICLK_REG 1
  145. #define BAB_ICLK_VAL 0x02
  146. // No fast clock
  147. #define BAB_FAST_REG 2
  148. #define BAB_FAST_VAL 0x04
  149. // Divide by 2
  150. #define BAB_DIV2_REG 3
  151. #define BAB_DIV2_VAL 0x08
  152. // Slow Clock
  153. #define BAB_SLOW_REG 4
  154. #define BAB_SLOW_VAL 0x10
  155. // No oclk
  156. #define BAB_OCLK_REG 6
  157. #define BAB_OCLK_VAL 0x20
  158. // Has configured
  159. #define BAB_CFGD_VAL 0x40
  160. #define BAB_DEFCONF (BAB_AUTO_VAL | \
  161. BAB_ICLK_VAL | \
  162. BAB_DIV2_VAL | \
  163. BAB_SLOW_VAL)
  164. #define BAB_REG_CLR_FROM 7
  165. #define BAB_REG_CLR_TO 11
  166. #define BAB_AUTO_SET(_c) ((_c) & BAB_AUTO_VAL)
  167. #define BAB_ICLK_SET(_c) ((_c) & BAB_ICLK_VAL)
  168. #define BAB_FAST_SET(_c) ((_c) & BAB_FAST_VAL)
  169. #define BAB_DIV2_SET(_c) ((_c) & BAB_DIV2_VAL)
  170. #define BAB_SLOW_SET(_c) ((_c) & BAB_SLOW_VAL)
  171. #define BAB_OCLK_SET(_c) ((_c) & BAB_OCLK_VAL)
  172. #define BAB_CFGD_SET(_c) ((_c) & BAB_CFGD_VAL)
  173. #define BAB_AUTO_BIT(_c) (BAB_AUTO_SET(_c) ? true : false)
  174. #define BAB_ICLK_BIT(_c) (BAB_ICLK_SET(_c) ? false : true)
  175. #define BAB_FAST_BIT(_c) (BAB_FAST_SET(_c) ? true : false)
  176. #define BAB_DIV2_BIT(_c) (BAB_DIV2_SET(_c) ? false : true)
  177. #define BAB_SLOW_BIT(_c) (BAB_SLOW_SET(_c) ? true : false)
  178. #define BAB_OCLK_BIT(_c) (BAB_OCLK_SET(_c) ? true : false)
  179. #define BAB_COUNT_ADDR 0x0100
  180. #define BAB_W1A_ADDR 0x1000
  181. #define BAB_W1B_ADDR 0x1400
  182. #define BAB_W2_ADDR 0x1900
  183. #define BAB_INP_ADDR 0x3000
  184. #define BAB_OSC_ADDR 0x6000
  185. #define BAB_REG_ADDR 0x7000
  186. /*
  187. * valid: 0x01 0x03 0x07 0x0F 0x1F 0x3F 0x7F 0xFF
  188. * max { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x00 }
  189. * max { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x00 }
  190. * avg { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00 }
  191. * slo { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x00 }
  192. * min { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  193. * good: 0x1F (97) 0x3F (104) 0x7F (109) 0xFF (104)
  194. */
  195. #define BAB_OSC 8
  196. static const uint8_t bab_osc_bits[BAB_OSC] =
  197. { 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF };
  198. static const uint8_t bab_reg_ena[4] = { 0xc1, 0x6a, 0x59, 0xe3 };
  199. static const uint8_t bab_reg_dis[4] = { 0x00, 0x00, 0x00, 0x00 };
  200. #define BAB_NONCE_OFFSETS 3
  201. #define BAB_OFF_0x1C_STA 2
  202. #define BAB_OFF_0x1C_FIN 2
  203. #define BAB_OFF_OTHER_STA 0
  204. #define BAB_OFF_OTHER_FIN 1
  205. #define BAB_EVIL_NONCE 0xe0
  206. #define BAB_EVIL_MASK 0xff
  207. static const uint32_t bab_nonce_offsets[] = {-0x800000, 0, -0x400000};
  208. struct bab_work_send {
  209. uint32_t midstate[MIDSTATE_UINTS];
  210. uint32_t ms3steps[MIDSTATE_UINTS];
  211. uint32_t merkle7;
  212. uint32_t ntime;
  213. uint32_t bits;
  214. };
  215. #define BAB_REPLY_NONCES 16
  216. struct bab_work_reply {
  217. uint32_t nonce[BAB_REPLY_NONCES];
  218. uint32_t jobsel;
  219. uint32_t spichk;
  220. };
  221. #define BAB_CHIP_MIN (sizeof(struct bab_work_reply)+16)
  222. #define ALLOC_WITEMS 1024
  223. #define LIMIT_WITEMS 0
  224. // Work
  225. typedef struct witem {
  226. struct work *work;
  227. struct bab_work_send chip_input;
  228. bool ci_setup;
  229. bool rolled;
  230. int nonces;
  231. struct timeval work_start;
  232. } WITEM;
  233. #define ALLOC_SITEMS 8
  234. #define LIMIT_SITEMS 0
  235. // SPI I/O
  236. typedef struct sitem {
  237. uint32_t siz;
  238. uint8_t wbuf[BAB_MAXBUF];
  239. uint8_t rbuf[BAB_MAXBUF];
  240. uint32_t chip_off[BAB_MAXCHIPS+1];
  241. uint32_t bank_off[BAB_MAXBANKS+2];
  242. // WITEMs used to build the work
  243. K_ITEM *witems[BAB_MAXCHIPS];
  244. struct timeval work_start;
  245. } SITEM;
  246. #define ALLOC_RITEMS 256
  247. #define LIMIT_RITEMS 0
  248. // Results
  249. typedef struct ritem {
  250. int chip;
  251. int nonces;
  252. uint32_t nonce[BAB_REPLY_NONCES];
  253. bool not_first_reply;
  254. struct timeval when;
  255. } RITEM;
  256. #define ALLOC_NITEMS 102400
  257. #define LIMIT_NITEMS 0
  258. // Nonce History
  259. typedef struct nitem {
  260. struct timeval found;
  261. } NITEM;
  262. #define DATAW(_item) ((WITEM *)(_item->data))
  263. #define DATAS(_item) ((SITEM *)(_item->data))
  264. #define DATAR(_item) ((RITEM *)(_item->data))
  265. #define DATAN(_item) ((NITEM *)(_item->data))
  266. // Record the number of each band between work sends
  267. #define BAB_DELAY_BANDS 10
  268. #define BAB_DELAY_BASE 0.5
  269. #define BAB_DELAY_STEP 0.2
  270. #define BAB_CHIP_SPEEDS 6
  271. // less than or equal GH/s
  272. static double chip_speed_ranges[BAB_CHIP_SPEEDS - 1] =
  273. { 0.0, 0.8, 1.6, 2.2, 2.8 };
  274. // Greater than the last one above means it's the last speed
  275. static char *chip_speed_names[BAB_CHIP_SPEEDS] =
  276. { "Bad", "V.Slow", "Slow", "OK", "Good", "Fast" };
  277. /*
  278. * This is required to do chip tuning
  279. * If disabled, it will simply run the chips at default speed
  280. * unless they never return valid results
  281. */
  282. #define UPDATE_HISTORY 1
  283. struct bab_info {
  284. struct thr_info spi_thr;
  285. struct thr_info res_thr;
  286. pthread_mutex_t did_lock;
  287. pthread_mutex_t nonce_lock;
  288. // All GPIO goes through this
  289. volatile unsigned *gpio;
  290. int version;
  291. int spifd;
  292. int chips;
  293. int chips_per_bank[BAB_MAXBANKS+1];
  294. int missing_chips_per_bank[BAB_MAXBANKS+1];
  295. int bank_first_chip[BAB_MAXBANKS+1];
  296. int bank_last_chip[BAB_MAXBANKS+1];
  297. int boards;
  298. int banks;
  299. uint32_t chip_spis[BAB_MAXCHIPS+1];
  300. int reply_wait;
  301. uint64_t reply_waits;
  302. cgsem_t scan_work;
  303. cgsem_t spi_work;
  304. cgsem_t spi_reply;
  305. cgsem_t process_reply;
  306. bool disabled[BAB_MAXCHIPS];
  307. int total_disabled;
  308. struct bab_work_reply chip_results[BAB_MAXCHIPS];
  309. struct bab_work_reply chip_prev[BAB_MAXCHIPS];
  310. uint8_t chip_fast[BAB_MAXCHIPS];
  311. uint8_t chip_conf[BAB_MAXCHIPS];
  312. uint8_t old_fast[BAB_MAXCHIPS];
  313. uint8_t old_conf[BAB_MAXCHIPS];
  314. uint8_t chip_bank[BAB_MAXCHIPS+1];
  315. uint8_t osc[BAB_OSC];
  316. /*
  317. * Ignore errors in the first work reply since
  318. * they may be from a previous run or random junk
  319. * There can be >100 with just one 16 chip board
  320. */
  321. uint32_t initial_ignored;
  322. bool not_first_reply[BAB_MAXCHIPS];
  323. // Stats
  324. uint64_t core_good[BAB_MAXCHIPS][BAB_CORES];
  325. uint64_t core_bad[BAB_MAXCHIPS][BAB_CORES];
  326. uint64_t chip_spie[BAB_MAXCHIPS]; // spi errors
  327. uint64_t chip_miso[BAB_MAXCHIPS]; // msio errors
  328. uint64_t chip_nonces[BAB_MAXCHIPS];
  329. uint64_t chip_good[BAB_MAXCHIPS];
  330. uint64_t chip_bad[BAB_MAXCHIPS];
  331. uint64_t chip_ncore[BAB_MAXCHIPS][BAB_X_COORD][BAB_Y_COORD];
  332. uint64_t chip_cont_bad[BAB_MAXCHIPS];
  333. uint64_t chip_max_bad[BAB_MAXCHIPS];
  334. uint64_t discarded_e0s;
  335. uint64_t untested_nonces;
  336. uint64_t tested_nonces;
  337. uint64_t new_nonces;
  338. uint64_t ok_nonces;
  339. uint64_t nonce_offset_count[BAB_NONCE_OFFSETS];
  340. uint64_t total_tests;
  341. uint64_t max_tests_per_nonce;
  342. uint64_t total_links;
  343. uint64_t total_proc_links;
  344. uint64_t max_links;
  345. uint64_t max_proc_links;
  346. uint64_t total_work_links;
  347. uint64_t fail;
  348. uint64_t fail_total_tests;
  349. uint64_t fail_total_links;
  350. uint64_t fail_total_work_links;
  351. uint64_t ign_total_tests;
  352. uint64_t ign_total_links;
  353. uint64_t ign_total_work_links;
  354. struct timeval last_sent_work;
  355. uint64_t delay_count;
  356. double delay_min;
  357. double delay_max;
  358. /*
  359. * 0 is below band ranges
  360. * BAB_DELAY_BANDS+1 is above band ranges
  361. */
  362. uint64_t delay_bands[BAB_DELAY_BANDS+2];
  363. uint64_t send_count;
  364. double send_total;
  365. double send_min;
  366. double send_max;
  367. // Work
  368. K_LIST *wfree_list;
  369. K_STORE *available_work;
  370. K_STORE *chip_work[BAB_MAXCHIPS];
  371. // SPI I/O
  372. K_LIST *sfree_list;
  373. // Waiting to send
  374. K_STORE *spi_list;
  375. // Sent
  376. K_STORE *spi_sent;
  377. // Results
  378. K_LIST *rfree_list;
  379. K_STORE *res_list;
  380. // Nonce History
  381. K_LIST *nfree_list;
  382. K_STORE *good_nonces[BAB_MAXCHIPS];
  383. K_STORE *bad_nonces[BAB_MAXCHIPS];
  384. struct timeval first_work[BAB_MAXCHIPS];
  385. #if UPDATE_HISTORY
  386. uint32_t work_count[BAB_MAXCHIPS];
  387. struct timeval last_tune[BAB_MAXCHIPS];
  388. uint8_t bad_fast[BAB_MAXCHIPS];
  389. bool bad_msg[BAB_MAXCHIPS];
  390. #endif
  391. uint64_t work_unrolled;
  392. uint64_t work_rolled;
  393. // bab-options (in order)
  394. uint8_t max_speed;
  395. uint8_t def_speed;
  396. uint8_t min_speed;
  397. double tune_up;
  398. double tune_down;
  399. uint32_t speed_hz;
  400. uint16_t delay_usecs;
  401. uint64_t trf_delay;
  402. struct timeval last_did;
  403. bool initialised;
  404. };
  405. /*
  406. * Amount of time for history
  407. * Older items in nonce_history are discarded
  408. * 300s / 5 minutes
  409. */
  410. #define HISTORY_TIME_S 300
  411. /*
  412. * If the SPI I/O thread waits longer than this long for work
  413. * it will report an error saying how long it's waiting
  414. * and again every BAB_STD_WAIT_mS after that
  415. */
  416. #define BAB_LONG_uS 1200000
  417. /*
  418. * If work wasn't available early enough,
  419. * report every BAB_LONG_WAIT_mS until it is
  420. */
  421. #define BAB_LONG_WAIT_mS 888
  422. /*
  423. * Some amount of time to wait for work
  424. * before checking how long we've waited
  425. */
  426. #define BAB_STD_WAIT_mS 888
  427. /*
  428. * How long to wait for the ioctl() to complete (per BANK)
  429. * This is a failsafe in case the ioctl() fails
  430. * since bab_txrx() will already post a wakeup when it completes
  431. * V1 is set to this x 2
  432. * V2 is set to this x active banks
  433. */
  434. #define BAB_REPLY_WAIT_mS 160
  435. /*
  436. * Work items older than this should not expect results
  437. * It has to allow for the result buffer returned with the next result
  438. * 0.75GH/s takes 5.727s to do a full nonce range
  439. * If HW is too high, consider increasing this to see if work is being
  440. * expired too early (due to slow chips)
  441. */
  442. #define BAB_WORK_EXPIRE_mS 7800
  443. // Don't send work more often than this
  444. #define BAB_EXPECTED_WORK_DELAY_mS 899
  445. /*
  446. * If a chip only has bad results after this time limit in seconds,
  447. * then switch it down to min_speed
  448. */
  449. #define BAB_BAD_TO_MIN (HISTORY_TIME_S + 10)
  450. /*
  451. * Also, just to be sure it's actually mining, it must have got this
  452. * many bad results before considering disabling it
  453. */
  454. #define BAB_BAD_COUNT 100
  455. /*
  456. * If a chip only has bad results after this time limit in seconds,
  457. * then disable it
  458. * A chip only returning bad results will use a lot more CPU than
  459. * an ok chip since all results will be tested against all unexpired
  460. * work that's been sent to the chip
  461. */
  462. #define BAB_BAD_DEAD (BAB_BAD_TO_MIN * 2)
  463. /*
  464. * Maximum bab_queue_full() will roll work if it is allowed to
  465. * Since work can somtimes (rarely) queue up with many chips,
  466. * limit it to avoid it getting too much range in the pending work
  467. */
  468. #define BAB_MAX_ROLLTIME 42
  469. static void bab_ms3steps(uint32_t *p)
  470. {
  471. uint32_t a, b, c, d, e, f, g, h, new_e, new_a;
  472. int i;
  473. a = p[0];
  474. b = p[1];
  475. c = p[2];
  476. d = p[3];
  477. e = p[4];
  478. f = p[5];
  479. g = p[6];
  480. h = p[7];
  481. for (i = 0; i < 3; i++) {
  482. new_e = p[i+16] + sha256_k[i] + h + CH(e,f,g) + SHA256_F2(e) + d;
  483. new_a = p[i+16] + sha256_k[i] + h + CH(e,f,g) + SHA256_F2(e) +
  484. SHA256_F1(a) + MAJ(a,b,c);
  485. d = c;
  486. c = b;
  487. b = a;
  488. a = new_a;
  489. h = g;
  490. g = f;
  491. f = e;
  492. e = new_e;
  493. }
  494. p[15] = a;
  495. p[14] = b;
  496. p[13] = c;
  497. p[12] = d;
  498. p[11] = e;
  499. p[10] = f;
  500. p[9] = g;
  501. p[8] = h;
  502. }
  503. static uint32_t bab_decnonce(uint32_t in)
  504. {
  505. uint32_t out;
  506. /* First part load */
  507. out = (in & 0xFF) << 24;
  508. in >>= 8;
  509. /* Byte reversal */
  510. in = (((in & 0xaaaaaaaa) >> 1) | ((in & 0x55555555) << 1));
  511. in = (((in & 0xcccccccc) >> 2) | ((in & 0x33333333) << 2));
  512. in = (((in & 0xf0f0f0f0) >> 4) | ((in & 0x0f0f0f0f) << 4));
  513. out |= (in >> 2) & 0x3FFFFF;
  514. /* Extraction */
  515. if (in & 1)
  516. out |= (1 << 23);
  517. if (in & 2)
  518. out |= (1 << 22);
  519. out -= 0x800004;
  520. return out;
  521. }
  522. static void cleanup_older(struct cgpu_info *babcgpu, int chip, K_ITEM *witem)
  523. {
  524. struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
  525. struct timeval now;
  526. bool expired_item;
  527. K_ITEM *tail;
  528. cgtime(&now);
  529. K_WLOCK(babinfo->chip_work[chip]);
  530. tail = babinfo->chip_work[chip]->tail;
  531. expired_item = false;
  532. // Discard expired work
  533. while (tail) {
  534. if (ms_tdiff(&now, &(DATAW(tail)->work_start)) < BAB_WORK_EXPIRE_mS)
  535. break;
  536. if (tail == witem)
  537. expired_item = true;
  538. k_unlink_item(babinfo->chip_work[chip], tail);
  539. K_WUNLOCK(babinfo->chip_work[chip]);
  540. if (DATAW(tail)->rolled)
  541. free_work(DATAW(tail)->work);
  542. else
  543. work_completed(babcgpu, DATAW(tail)->work);
  544. K_WLOCK(babinfo->chip_work[chip]);
  545. k_add_head(babinfo->wfree_list, tail);
  546. tail = babinfo->chip_work[chip]->tail;
  547. }
  548. // If we didn't expire witem, then remove all older than it
  549. if (!expired_item && witem && witem->next) {
  550. tail = babinfo->chip_work[chip]->tail;
  551. while (tail && tail != witem) {
  552. k_unlink_item(babinfo->chip_work[chip], tail);
  553. K_WUNLOCK(babinfo->chip_work[chip]);
  554. if (DATAW(tail)->rolled)
  555. free_work(DATAW(tail)->work);
  556. else
  557. work_completed(babcgpu, DATAW(tail)->work);
  558. K_WLOCK(babinfo->chip_work[chip]);
  559. k_add_head(babinfo->wfree_list, tail);
  560. tail = babinfo->chip_work[chip]->tail;
  561. }
  562. }
  563. K_WUNLOCK(babinfo->chip_work[chip]);
  564. }
  565. static void _bab_reset(__maybe_unused struct cgpu_info *babcgpu, struct bab_info *babinfo, int bank, int times)
  566. {
  567. const int banks[BAB_MAXBANKS] = { 18, 23, 24, 25 };
  568. int i;
  569. BAB_INP_GPIO(10);
  570. BAB_OUT_GPIO(10);
  571. BAB_INP_GPIO(11);
  572. BAB_OUT_GPIO(11);
  573. if (bank) {
  574. for (i = 0; i < BAB_MAXBANKS; i++) {
  575. BAB_INP_GPIO(banks[i]);
  576. BAB_OUT_GPIO(banks[i]);
  577. if (bank == i+1)
  578. BAB_GPIO_SET = 1 << banks[i];
  579. else
  580. BAB_GPIO_CLR = 1 << banks[i];
  581. }
  582. cgsleep_us(4096);
  583. } else {
  584. for (i = 0; i < BAB_MAXBANKS; i++)
  585. BAB_INP_GPIO(banks[i]);
  586. }
  587. BAB_GPIO_SET = 1 << 11;
  588. for (i = 0; i < times; i++) { // 1us = 1MHz
  589. BAB_GPIO_SET = 1 << 10;
  590. cgsleep_us(1);
  591. BAB_GPIO_CLR = 1 << 10;
  592. cgsleep_us(1);
  593. }
  594. BAB_GPIO_CLR = 1 << 11;
  595. BAB_INP_GPIO(11);
  596. BAB_INP_GPIO(10);
  597. BAB_INP_GPIO(9);
  598. BAB_OUT_GPIO_V(11, 0);
  599. BAB_OUT_GPIO_V(10, 0);
  600. BAB_OUT_GPIO_V(9, 0);
  601. }
  602. // TODO: handle a false return where this is called?
  603. static bool _bab_txrx(struct cgpu_info *babcgpu, struct bab_info *babinfo, K_ITEM *item, bool detect_ignore, const char *file, const char *func, const int line)
  604. {
  605. int bank, i, count, chip1, chip2;
  606. uint32_t siz, pos;
  607. struct spi_ioc_transfer tran;
  608. uintptr_t rbuf, wbuf;
  609. wbuf = (uintptr_t)(DATAS(item)->wbuf);
  610. rbuf = (uintptr_t)(DATAS(item)->rbuf);
  611. siz = (uint32_t)(DATAS(item)->siz);
  612. memset(&tran, 0, sizeof(tran));
  613. tran.speed_hz = babinfo->speed_hz;
  614. tran.delay_usecs = babinfo->delay_usecs;
  615. i = 0;
  616. pos = 0;
  617. for (bank = 0; bank <= BAB_MAXBANKS; bank++) {
  618. if (DATAS(item)->bank_off[bank]) {
  619. bab_reset(bank, 64);
  620. break;
  621. }
  622. }
  623. if (unlikely(bank > BAB_MAXBANKS)) {
  624. applog(LOG_ERR, "%s%d: %s() failed to find a bank" BAB_FFL,
  625. babcgpu->drv->name, babcgpu->device_id,
  626. __func__, BAB_FFL_PASS);
  627. return false;
  628. }
  629. count = 0;
  630. while (siz > 0) {
  631. tran.tx_buf = wbuf;
  632. tran.rx_buf = rbuf;
  633. tran.speed_hz = BAB_SPI_SPEED;
  634. if (pos == DATAS(item)->bank_off[bank]) {
  635. for (; ++bank <= BAB_MAXBANKS; ) {
  636. if (DATAS(item)->bank_off[bank] > pos) {
  637. bab_reset(bank, 64);
  638. break;
  639. }
  640. }
  641. }
  642. if (siz < BAB_SPI_BUFSIZ)
  643. tran.len = siz;
  644. else
  645. tran.len = BAB_SPI_BUFSIZ;
  646. if (pos < DATAS(item)->bank_off[bank] &&
  647. DATAS(item)->bank_off[bank] < (pos + tran.len))
  648. tran.len = DATAS(item)->bank_off[bank] - pos;
  649. for (; i < babinfo->chips; i++) {
  650. if (!DATAS(item)->chip_off[i])
  651. continue;
  652. if (DATAS(item)->chip_off[i] >= pos + tran.len) {
  653. tran.speed_hz = babinfo->chip_spis[i];
  654. break;
  655. }
  656. }
  657. if (unlikely(i > babinfo->chips)) {
  658. applog(LOG_ERR, "%s%d: %s() failed to find chip" BAB_FFL,
  659. babcgpu->drv->name, babcgpu->device_id,
  660. __func__, BAB_FFL_PASS);
  661. return false;
  662. }
  663. if (unlikely(babinfo->chip_spis[i] == BAB_SPI_SPEED)) {
  664. applog(LOG_DEBUG, "%s%d: %s() chip[%d] speed %d shouldn't be %d" BAB_FFL,
  665. babcgpu->drv->name, babcgpu->device_id,
  666. __func__, i, (int)babinfo->chip_spis[i],
  667. BAB_SPI_SPEED, BAB_FFL_PASS);
  668. }
  669. if (unlikely(tran.speed_hz == BAB_SPI_SPEED)) {
  670. applog(LOG_DEBUG, "%s%d: %s() transfer speed %d shouldn't be %d" BAB_FFL,
  671. babcgpu->drv->name, babcgpu->device_id,
  672. __func__, (int)tran.speed_hz,
  673. BAB_SPI_SPEED, BAB_FFL_PASS);
  674. }
  675. count++;
  676. if (ioctl(babinfo->spifd, SPI_IOC_MESSAGE(1), (void *)&tran) < 0) {
  677. if (!detect_ignore || errno != 110) {
  678. for (bank = BAB_MAXBANKS; bank >= 0; bank--) {
  679. if (DATAS(item)->bank_off[bank] &&
  680. pos >= DATAS(item)->bank_off[bank]) {
  681. break;
  682. }
  683. }
  684. for (chip1 = babinfo->chips-1; chip1 >= 0; chip1--) {
  685. if (DATAS(item)->chip_off[chip1] &&
  686. pos >= DATAS(item)->chip_off[chip1]) {
  687. break;
  688. }
  689. }
  690. for (chip2 = babinfo->chips-1; chip2 >= 0; chip2--) {
  691. if (DATAS(item)->chip_off[chip2] &&
  692. (pos + tran.len) >= DATAS(item)->chip_off[chip2]) {
  693. break;
  694. }
  695. }
  696. applog(LOG_ERR, "%s%d: ioctl (%d) siz=%d bank=%d chip=%d-%d"
  697. " failed err=%d" BAB_FFL,
  698. babcgpu->drv->name,
  699. babcgpu->device_id,
  700. count, (int)(tran.len),
  701. bank, chip1, chip2,
  702. errno, BAB_FFL_PASS);
  703. }
  704. return false;
  705. }
  706. siz -= tran.len;
  707. wbuf += tran.len;
  708. rbuf += tran.len;
  709. pos += tran.len;
  710. if (siz > 0 && babinfo->trf_delay > 0)
  711. cgsleep_us(babinfo->trf_delay);
  712. }
  713. cgtime(&(DATAS(item)->work_start));
  714. mutex_lock(&(babinfo->did_lock));
  715. cgtime(&(babinfo->last_did));
  716. mutex_unlock(&(babinfo->did_lock));
  717. return true;
  718. }
  719. static void _bab_add_buf_rev(K_ITEM *item, const uint8_t *data, uint32_t siz, const char *file, const char *func, const int line)
  720. {
  721. uint32_t now_used, i;
  722. uint8_t tmp;
  723. now_used = DATAS(item)->siz;
  724. if (now_used + siz >= BAB_MAXBUF) {
  725. quitfrom(1, file, func, line,
  726. "%s() buffer limit of %d exceeded=%d siz=%d",
  727. __func__, BAB_MAXBUF, (int)(now_used + siz), (int)siz);
  728. }
  729. for (i = 0; i < siz; i++) {
  730. tmp = data[i];
  731. tmp = ((tmp & 0xaa)>>1) | ((tmp & 0x55) << 1);
  732. tmp = ((tmp & 0xcc)>>2) | ((tmp & 0x33) << 2);
  733. tmp = ((tmp & 0xf0)>>4) | ((tmp & 0x0f) << 4);
  734. DATAS(item)->wbuf[now_used + i] = tmp;
  735. }
  736. DATAS(item)->siz += siz;
  737. }
  738. static void _bab_add_buf(K_ITEM *item, const uint8_t *data, size_t siz, const char *file, const char *func, const int line)
  739. {
  740. uint32_t now_used;
  741. now_used = DATAS(item)->siz;
  742. if (now_used + siz >= BAB_MAXBUF) {
  743. quitfrom(1, file, func, line,
  744. "%s() DATAS buffer limit of %d exceeded=%d siz=%d",
  745. __func__, BAB_MAXBUF, (int)(now_used + siz), (int)siz);
  746. }
  747. memcpy(&(DATAS(item)->wbuf[now_used]), data, siz);
  748. DATAS(item)->siz += siz;
  749. }
  750. static void _bab_add_noops(K_ITEM *item, size_t siz, const char *file, const char *func, const int line)
  751. {
  752. uint32_t now_used;
  753. now_used = DATAS(item)->siz;
  754. if (now_used + siz >= BAB_MAXBUF) {
  755. quitfrom(1, file, func, line,
  756. "%s() DATAS buffer limit of %d exceeded=%d siz=%d",
  757. __func__, BAB_MAXBUF, (int)(now_used + siz), (int)siz);
  758. }
  759. memset(&(DATAS(item)->wbuf[now_used]), BAB_NOOP, siz);
  760. DATAS(item)->siz += siz;
  761. }
  762. static void _bab_add_data(K_ITEM *item, uint32_t addr, const uint8_t *data, size_t siz, const char *file, const char *func, const int line)
  763. {
  764. uint8_t tmp[3];
  765. int trf_siz;
  766. if (siz < BAB_ADD_MIN || siz > BAB_ADD_MAX) {
  767. quitfrom(1, file, func, line,
  768. "%s() called with invalid siz=%d (min=%d max=%d)",
  769. __func__, (int)siz, BAB_ADD_MIN, BAB_ADD_MAX);
  770. }
  771. trf_siz = siz / 4;
  772. tmp[0] = (trf_siz - 1) | 0xE0;
  773. tmp[1] = (addr >> 8) & 0xff;
  774. tmp[2] = addr & 0xff;
  775. _bab_add_buf(item, tmp, sizeof(tmp), BAB_FFL_PASS);
  776. _bab_add_buf_rev(item, data, siz, BAB_FFL_PASS);
  777. }
  778. static void _bab_config_reg(K_ITEM *item, uint32_t reg, bool enable, const char *file, const char *func, const int line)
  779. {
  780. if (enable) {
  781. _bab_add_data(item, BAB_REG_ADDR + reg*32,
  782. bab_reg_ena, sizeof(bab_reg_ena), BAB_FFL_PASS);
  783. } else {
  784. _bab_add_data(item, BAB_REG_ADDR + reg*32,
  785. bab_reg_dis, sizeof(bab_reg_dis), BAB_FFL_PASS);
  786. }
  787. }
  788. static void bab_set_osc(struct bab_info *babinfo, int chip)
  789. {
  790. int fast, i;
  791. fast = babinfo->chip_fast[chip];
  792. for (i = 0; i < BAB_OSC && fast > BAB_OSC; i++, fast -= BAB_OSC) {
  793. babinfo->osc[i] = 0xff;
  794. }
  795. if (i < BAB_OSC && fast > 0 && fast <= BAB_OSC)
  796. babinfo->osc[i++] = bab_osc_bits[fast - 1];
  797. for (; i < BAB_OSC; i++)
  798. babinfo->osc[i] = 0x00;
  799. applog(LOG_DEBUG, "@osc(chip=%d) fast=%d 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x", chip, fast, babinfo->osc[0], babinfo->osc[1], babinfo->osc[2], babinfo->osc[3], babinfo->osc[4], babinfo->osc[5], babinfo->osc[6], babinfo->osc[7]);
  800. }
  801. static void bab_put(struct bab_info *babinfo, K_ITEM *sitem)
  802. {
  803. struct bab_work_send *chip_input;
  804. int i, reg, bank = 0;
  805. size_t chip_siz;
  806. BAB_ADD_BREAK(sitem);
  807. for (i = 0; i < babinfo->chips; i++) {
  808. if (babinfo->chip_bank[i] != bank) {
  809. DATAS(sitem)->bank_off[bank] = DATAS(sitem)->siz;
  810. bank = babinfo->chip_bank[i];
  811. BAB_ADD_BREAK(sitem);
  812. }
  813. if (!(babinfo->disabled[i])) {
  814. if (BAB_CFGD_SET(babinfo->chip_conf[i]) || !babinfo->chip_conf[i]) {
  815. bab_set_osc(babinfo, i);
  816. bab_add_data(sitem, BAB_OSC_ADDR, babinfo->osc, sizeof(babinfo->osc));
  817. bab_config_reg(sitem, BAB_ICLK_REG, BAB_ICLK_BIT(babinfo->chip_conf[i]));
  818. bab_config_reg(sitem, BAB_FAST_REG, BAB_FAST_BIT(babinfo->chip_conf[i]));
  819. bab_config_reg(sitem, BAB_DIV2_REG, BAB_DIV2_BIT(babinfo->chip_conf[i]));
  820. bab_config_reg(sitem, BAB_SLOW_REG, BAB_SLOW_BIT(babinfo->chip_conf[i]));
  821. bab_config_reg(sitem, BAB_OCLK_REG, BAB_OCLK_BIT(babinfo->chip_conf[i]));
  822. for (reg = BAB_REG_CLR_FROM; reg <= BAB_REG_CLR_TO; reg++)
  823. bab_config_reg(sitem, reg, false);
  824. if (babinfo->chip_conf[i]) {
  825. bab_add_data(sitem, BAB_COUNT_ADDR, bab_counters, sizeof(bab_counters));
  826. bab_add_data(sitem, BAB_W1A_ADDR, bab_w1, sizeof(bab_w1));
  827. bab_add_data(sitem, BAB_W1B_ADDR, bab_w1, sizeof(bab_w1)/2);
  828. bab_add_data(sitem, BAB_W2_ADDR, bab_w2, sizeof(bab_w2));
  829. babinfo->chip_conf[i] ^= BAB_CFGD_VAL;
  830. }
  831. babinfo->old_fast[i] = babinfo->chip_fast[i];
  832. babinfo->old_conf[i] = babinfo->chip_conf[i];
  833. } else {
  834. if (babinfo->old_fast[i] != babinfo->chip_fast[i]) {
  835. bab_set_osc(babinfo, i);
  836. bab_add_data(sitem, BAB_OSC_ADDR, babinfo->osc, sizeof(babinfo->osc));
  837. babinfo->old_fast[i] = babinfo->chip_fast[i];
  838. }
  839. if (babinfo->old_conf[i] != babinfo->chip_conf[i]) {
  840. if (BAB_ICLK_SET(babinfo->old_conf[i]) !=
  841. BAB_ICLK_SET(babinfo->chip_conf[i]))
  842. bab_config_reg(sitem, BAB_ICLK_REG,
  843. BAB_ICLK_BIT(babinfo->chip_conf[i]));
  844. if (BAB_FAST_SET(babinfo->old_conf[i]) !=
  845. BAB_FAST_SET(babinfo->chip_conf[i]))
  846. bab_config_reg(sitem, BAB_FAST_REG,
  847. BAB_FAST_BIT(babinfo->chip_conf[i]));
  848. if (BAB_DIV2_SET(babinfo->old_conf[i]) !=
  849. BAB_DIV2_SET(babinfo->chip_conf[i]))
  850. bab_config_reg(sitem, BAB_DIV2_REG,
  851. BAB_DIV2_BIT(babinfo->chip_conf[i]));
  852. if (BAB_SLOW_SET(babinfo->old_conf[i]) !=
  853. BAB_SLOW_SET(babinfo->chip_conf[i]))
  854. bab_config_reg(sitem, BAB_SLOW_REG,
  855. BAB_SLOW_BIT(babinfo->chip_conf[i]));
  856. if (BAB_OCLK_SET(babinfo->old_conf[i]) !=
  857. BAB_OCLK_SET(babinfo->chip_conf[i]))
  858. bab_config_reg(sitem, BAB_OCLK_REG,
  859. BAB_OCLK_BIT(babinfo->chip_conf[i]));
  860. babinfo->old_conf[i] = babinfo->chip_conf[i];
  861. }
  862. }
  863. DATAS(sitem)->chip_off[i] = DATAS(sitem)->siz + 3;
  864. chip_input = &(DATAW(DATAS(sitem)->witems[i])->chip_input);
  865. if (babinfo->chip_conf[i])
  866. bab_add_data(sitem, BAB_INP_ADDR, (uint8_t *)chip_input, sizeof(*chip_input));
  867. chip_siz = DATAS(sitem)->siz - babinfo->chip_conf[i];
  868. if (chip_siz < BAB_CHIP_MIN)
  869. BAB_ADD_NOOPs(sitem, BAB_CHIP_MIN - chip_siz);
  870. }
  871. BAB_ADD_ASYNC(sitem);
  872. }
  873. DATAS(sitem)->chip_off[i] = DATAS(sitem)->siz;
  874. DATAS(sitem)->bank_off[bank] = DATAS(sitem)->siz;
  875. K_WLOCK(babinfo->spi_list);
  876. k_add_head(babinfo->spi_list, sitem);
  877. K_WUNLOCK(babinfo->spi_list);
  878. cgsem_post(&(babinfo->spi_work));
  879. }
  880. static bool bab_get(__maybe_unused struct cgpu_info *babcgpu, struct bab_info *babinfo, struct timeval *when)
  881. {
  882. K_ITEM *item;
  883. bool delayed;
  884. int i;
  885. item = NULL;
  886. delayed = false;
  887. while (item == NULL) {
  888. cgsem_mswait(&(babinfo->spi_reply), babinfo->reply_wait);
  889. K_WLOCK(babinfo->spi_sent);
  890. item = k_unlink_tail(babinfo->spi_sent);
  891. K_WUNLOCK(babinfo->spi_sent);
  892. if (!item) {
  893. if (!delayed) {
  894. applog(LOG_WARNING, "%s%d: Delay getting work reply ...",
  895. babcgpu->drv->name,
  896. babcgpu->device_id);
  897. delayed = true;
  898. babinfo->reply_waits++;
  899. }
  900. }
  901. }
  902. for (i = 0; i < babinfo->chips; i++) {
  903. if (babinfo->chip_conf[i] & 0x7f) {
  904. memcpy((void *)&(babinfo->chip_results[i]),
  905. (void *)(DATAS(item)->rbuf + DATAS(item)->chip_off[i]),
  906. sizeof(babinfo->chip_results[0]));
  907. }
  908. }
  909. // work_start is also the time the results were read
  910. memcpy(when, &(DATAS(item)->work_start), sizeof(*when));
  911. K_WLOCK(babinfo->sfree_list);
  912. k_add_head(babinfo->sfree_list, item);
  913. K_WUNLOCK(babinfo->sfree_list);
  914. return true;
  915. }
  916. void bab_detect_chips(struct cgpu_info *babcgpu, struct bab_info *babinfo, int bank, int first, int last)
  917. {
  918. int i, reg, j;
  919. K_ITEM *item;
  920. if (sizeof(struct bab_work_send) != sizeof(bab_test_data)) {
  921. quithere(1, "struct bab_work_send (%d) and bab_test_data (%d)"
  922. " must be the same size",
  923. (int)sizeof(struct bab_work_send),
  924. (int)sizeof(bab_test_data));
  925. }
  926. K_WLOCK(babinfo->sfree_list);
  927. item = k_unlink_head_zero(babinfo->sfree_list);
  928. K_WUNLOCK(babinfo->sfree_list);
  929. BAB_ADD_BREAK(item);
  930. for (i = first; i < last && i < BAB_MAXCHIPS; i++) {
  931. bab_set_osc(babinfo, i);
  932. bab_add_data(item, BAB_OSC_ADDR, babinfo->osc, sizeof(babinfo->osc));
  933. bab_config_reg(item, BAB_ICLK_REG, BAB_ICLK_BIT(babinfo->chip_conf[i]));
  934. bab_config_reg(item, BAB_FAST_REG, BAB_FAST_BIT(babinfo->chip_conf[i]));
  935. bab_config_reg(item, BAB_DIV2_REG, BAB_DIV2_BIT(babinfo->chip_conf[i]));
  936. bab_config_reg(item, BAB_SLOW_REG, BAB_SLOW_BIT(babinfo->chip_conf[i]));
  937. bab_config_reg(item, BAB_OCLK_REG, BAB_OCLK_BIT(babinfo->chip_conf[i]));
  938. for (reg = BAB_REG_CLR_FROM; reg <= BAB_REG_CLR_TO; reg++)
  939. bab_config_reg(item, reg, false);
  940. bab_add_data(item, BAB_COUNT_ADDR, bab_counters, sizeof(bab_counters));
  941. bab_add_data(item, BAB_W1A_ADDR, bab_w1, sizeof(bab_w1));
  942. bab_add_data(item, BAB_W1B_ADDR, bab_w1, sizeof(bab_w1)/2);
  943. bab_add_data(item, BAB_W2_ADDR, bab_w2, sizeof(bab_w2));
  944. DATAS(item)->chip_off[i] = DATAS(item)->siz + 3;
  945. bab_add_data(item, BAB_INP_ADDR, bab_test_data, sizeof(bab_test_data));
  946. DATAS(item)->chip_off[i+1] = DATAS(item)->siz;
  947. DATAS(item)->bank_off[bank] = DATAS(item)->siz;
  948. babinfo->chips = i + 1;
  949. bab_txrx(item, false);
  950. DATAS(item)->siz = 0;
  951. BAB_ADD_BREAK(item);
  952. for (j = first; j <= i; j++) {
  953. DATAS(item)->chip_off[j] = DATAS(item)->siz + 3;
  954. BAB_ADD_ASYNC(item);
  955. }
  956. }
  957. memset(item->data, 0, babinfo->sfree_list->siz);
  958. BAB_ADD_BREAK(item);
  959. for (i = first; i < last && i < BAB_MAXCHIPS; i++) {
  960. DATAS(item)->chip_off[i] = DATAS(item)->siz + 3;
  961. bab_add_data(item, BAB_INP_ADDR, bab_test_data, sizeof(bab_test_data));
  962. BAB_ADD_ASYNC(item);
  963. }
  964. DATAS(item)->chip_off[i] = DATAS(item)->siz;
  965. DATAS(item)->bank_off[bank] = DATAS(item)->siz;
  966. babinfo->chips = i;
  967. bab_txrx(item, true);
  968. DATAS(item)->siz = 0;
  969. babinfo->chips = first;
  970. for (i = first; i < last && i < BAB_MAXCHIPS; i++) {
  971. uint32_t tmp[DATA_UINTS-1];
  972. memcpy(tmp, DATAS(item)->rbuf + DATAS(item)->chip_off[i], sizeof(tmp));
  973. DATAS(item)->chip_off[i] = 0;
  974. for (j = 0; j < BAB_REPLY_NONCES; j++) {
  975. if (tmp[j] != 0xffffffff && tmp[j] != 0x00000000) {
  976. babinfo->chip_bank[i] = bank;
  977. babinfo->chips = i + 1;
  978. break;
  979. }
  980. }
  981. }
  982. for (i = first ; i < babinfo->chips; i++)
  983. babinfo->chip_bank[i] = bank;
  984. K_WLOCK(babinfo->sfree_list);
  985. k_add_head(babinfo->sfree_list, item);
  986. K_WUNLOCK(babinfo->sfree_list);
  987. }
  988. static const char *bab_modules[] = {
  989. "i2c-dev",
  990. "i2c-bcm2708",
  991. "spidev",
  992. "spi-bcm2708",
  993. NULL
  994. };
  995. static const char *bab_memory = "/dev/mem";
  996. static int bab_memory_addr = 0x20200000;
  997. // TODO: add --bab-options for SPEED_HZ, tran.delay_usecs and an inter transfer delay (default 0)
  998. static struct {
  999. int request;
  1000. int value;
  1001. } bab_ioc[] = {
  1002. { SPI_IOC_RD_MODE, 0 },
  1003. { SPI_IOC_WR_MODE, 0 },
  1004. { SPI_IOC_RD_BITS_PER_WORD, 8 },
  1005. { SPI_IOC_WR_BITS_PER_WORD, 8 },
  1006. { SPI_IOC_RD_MAX_SPEED_HZ, 1000000 },
  1007. { SPI_IOC_WR_MAX_SPEED_HZ, 1000000 },
  1008. { -1, -1 }
  1009. };
  1010. static bool bab_init_gpio(struct cgpu_info *babcgpu, struct bab_info *babinfo, int bus, int chip)
  1011. {
  1012. int i, err, memfd, data;
  1013. char buf[64];
  1014. bab_ioc[4].value = (int)(babinfo->speed_hz);
  1015. bab_ioc[5].value = (int)(babinfo->speed_hz);
  1016. for (i = 0; bab_modules[i]; i++) {
  1017. snprintf(buf, sizeof(buf), "modprobe %s", bab_modules[i]);
  1018. err = system(buf);
  1019. if (err) {
  1020. applog(LOG_ERR, "%s failed to modprobe %s (%d) - you need to be root?",
  1021. babcgpu->drv->dname,
  1022. bab_modules[i], err);
  1023. goto bad_out;
  1024. }
  1025. }
  1026. memfd = open(bab_memory, O_RDWR | O_SYNC);
  1027. if (memfd < 0) {
  1028. applog(LOG_ERR, "%s failed open %s (%d)",
  1029. babcgpu->drv->dname,
  1030. bab_memory, errno);
  1031. goto bad_out;
  1032. }
  1033. babinfo->gpio = (volatile unsigned *)mmap(NULL, BAB_SPI_BUFSIZ,
  1034. PROT_READ | PROT_WRITE,
  1035. MAP_SHARED, memfd,
  1036. bab_memory_addr);
  1037. if (babinfo->gpio == MAP_FAILED) {
  1038. close(memfd);
  1039. applog(LOG_ERR, "%s failed mmap gpio (%d)",
  1040. babcgpu->drv->dname,
  1041. errno);
  1042. goto bad_out;
  1043. }
  1044. close(memfd);
  1045. snprintf(buf, sizeof(buf), "/dev/spidev%d.%d", bus, chip);
  1046. babinfo->spifd = open(buf, O_RDWR);
  1047. if (babinfo->spifd < 0) {
  1048. applog(LOG_ERR, "%s failed to open spidev (%d)",
  1049. babcgpu->drv->dname,
  1050. errno);
  1051. goto map_out;
  1052. }
  1053. babcgpu->device_path = strdup(buf);
  1054. for (i = 0; bab_ioc[i].value != -1; i++) {
  1055. data = bab_ioc[i].value;
  1056. err = ioctl(babinfo->spifd, bab_ioc[i].request, (void *)&data);
  1057. if (err < 0) {
  1058. applog(LOG_ERR, "%s failed ioctl (%d) (%d)",
  1059. babcgpu->drv->dname,
  1060. i, errno);
  1061. goto close_out;
  1062. }
  1063. }
  1064. for (i = 0; i < BAB_MAXCHIPS; i++)
  1065. babinfo->chip_spis[i] = (int)((1000000.0 / (100.0 + 31.0 * (i + 1))) * 1000);
  1066. return true;
  1067. close_out:
  1068. close(babinfo->spifd);
  1069. babinfo->spifd = 0;
  1070. free(babcgpu->device_path);
  1071. babcgpu->device_path = NULL;
  1072. map_out:
  1073. munmap((void *)(babinfo->gpio), BAB_SPI_BUFSIZ);
  1074. babinfo->gpio = NULL;
  1075. bad_out:
  1076. return false;
  1077. }
  1078. static void bab_init_chips(struct cgpu_info *babcgpu, struct bab_info *babinfo)
  1079. {
  1080. int chip, chipoff, bank, chips, new_chips, boards, mis;
  1081. applog(LOG_WARNING, "%s V1 first test for %d chips ...",
  1082. babcgpu->drv->dname, BAB_V1_CHIP_TEST);
  1083. bab_detect_chips(babcgpu, babinfo, 0, 0, BAB_V1_CHIP_TEST);
  1084. if (babinfo->chips > 0) {
  1085. babinfo->version = 1;
  1086. babinfo->banks = 0;
  1087. if (babinfo->chips == BAB_V1_CHIP_TEST) {
  1088. applog(LOG_WARNING, "%s V1 test for %d more chips ...",
  1089. babcgpu->drv->dname, BAB_MAXCHIPS - BAB_V1_CHIP_TEST);
  1090. bab_detect_chips(babcgpu, babinfo, 0, BAB_V1_CHIP_TEST, BAB_MAXCHIPS);
  1091. }
  1092. babinfo->chips_per_bank[BAB_V1_BANK] = babinfo->chips;
  1093. babinfo->bank_first_chip[BAB_V1_BANK] = 0;
  1094. babinfo->bank_last_chip[BAB_V1_BANK] = babinfo->chips - 1;
  1095. babinfo->boards = (int)((float)(babinfo->chips - 1) / BAB_BOARDCHIPS) + 1;
  1096. babinfo->reply_wait = BAB_REPLY_WAIT_mS * 2;
  1097. if ((chip = (babinfo->chips_per_bank[BAB_V1_BANK] % BAB_BOARDCHIPS))) {
  1098. mis = BAB_BOARDCHIPS - chip;
  1099. babinfo->missing_chips_per_bank[BAB_V1_BANK] = mis;
  1100. applog(LOG_WARNING, "%s V1: missing %d chip%s",
  1101. babcgpu->drv->dname, mis,
  1102. (mis == 1) ? "" : "s");
  1103. }
  1104. } else {
  1105. applog(LOG_WARNING, "%s no chips found with V1", babcgpu->drv->dname);
  1106. applog(LOG_WARNING, "%s V2 test %d banks %d chips ...",
  1107. babcgpu->drv->dname, BAB_MAXBANKS, BAB_MAXCHIPS);
  1108. chips = 0;
  1109. babinfo->version = 2;
  1110. babinfo->banks = 0;
  1111. for (bank = 1; bank <= BAB_MAXBANKS; bank++) {
  1112. for (chipoff = 0; chipoff < BAB_BANKCHIPS; chipoff++) {
  1113. chip = babinfo->chips + chipoff;
  1114. babinfo->chip_spis[chip] = 625000;
  1115. }
  1116. bab_reset(bank, 64);
  1117. bab_detect_chips(babcgpu, babinfo, bank, babinfo->chips, babinfo->chips + BAB_BANKCHIPS);
  1118. new_chips = babinfo->chips - chips;
  1119. babinfo->chips_per_bank[bank] = new_chips;
  1120. if (new_chips > 0) {
  1121. babinfo->bank_first_chip[bank] = babinfo->chips - new_chips;
  1122. babinfo->bank_last_chip[bank] = babinfo->chips - 1;
  1123. }
  1124. chips = babinfo->chips;
  1125. if (new_chips == 0)
  1126. boards = 0;
  1127. else {
  1128. boards = (int)((float)(new_chips - 1) / BAB_BOARDCHIPS) + 1;
  1129. babinfo->banks++;
  1130. }
  1131. applog(LOG_WARNING, "%s V2 bank %d: %d chips %d board%s",
  1132. babcgpu->drv->dname, bank, new_chips,
  1133. boards, (boards == 1) ? "" : "s");
  1134. babinfo->boards += boards;
  1135. if ((chip = (babinfo->chips_per_bank[bank] % BAB_BOARDCHIPS))) {
  1136. mis = BAB_BOARDCHIPS - chip;
  1137. babinfo->missing_chips_per_bank[bank] = mis;
  1138. applog(LOG_WARNING, "%s V2: bank %d missing %d chip%s",
  1139. babcgpu->drv->dname, bank,
  1140. mis, (mis == 1) ? "" : "s");
  1141. }
  1142. }
  1143. babinfo->reply_wait = BAB_REPLY_WAIT_mS * babinfo->banks;
  1144. bab_reset(0, 8);
  1145. }
  1146. memcpy(babinfo->old_conf, babinfo->chip_conf, sizeof(babinfo->old_conf));
  1147. memcpy(babinfo->old_fast, babinfo->chip_fast, sizeof(babinfo->old_fast));
  1148. }
  1149. static char *bab_options[] = {
  1150. "MaxSpeed",
  1151. "DefaultSpeed",
  1152. "MinSpeed",
  1153. "TuneUp",
  1154. "TuneDown",
  1155. "SPISpeed",
  1156. "SPIDelayuS",
  1157. "TransferDelayuS"
  1158. };
  1159. #define INVOP " Invalid Option "
  1160. static void bab_get_options(struct cgpu_info *babcgpu, struct bab_info *babinfo)
  1161. {
  1162. char *ptr, *colon;
  1163. int which, val;
  1164. double fval;
  1165. long lval;
  1166. if (opt_bab_options == NULL)
  1167. return;
  1168. which = 0;
  1169. ptr = opt_bab_options;
  1170. while (ptr && *ptr) {
  1171. colon = strchr(ptr, ':');
  1172. if (colon)
  1173. *(colon++) = '\0';
  1174. switch (which) {
  1175. case 0:
  1176. if (*ptr && tolower(*ptr) != 'd') {
  1177. val = atoi(ptr);
  1178. if (!isdigit(*ptr) || val < BAB_ABSMINSPEED || val > BAB_MAXSPEED) {
  1179. quit(1, "%s"INVOP"%s '%s' must be %d <= %s <= %d",
  1180. babcgpu->drv->dname,
  1181. bab_options[which],
  1182. ptr, BAB_ABSMINSPEED,
  1183. bab_options[which],
  1184. BAB_MAXSPEED);
  1185. }
  1186. babinfo->max_speed = (uint8_t)val;
  1187. // Adjust def,min down if they are above max specified
  1188. if (babinfo->def_speed > babinfo->max_speed)
  1189. babinfo->def_speed = babinfo->max_speed;
  1190. if (babinfo->min_speed > babinfo->max_speed)
  1191. babinfo->min_speed = babinfo->max_speed;
  1192. }
  1193. break;
  1194. case 1:
  1195. if (*ptr && tolower(*ptr) != 'd') {
  1196. val = atoi(ptr);
  1197. if (!isdigit(*ptr) || val < BAB_ABSMINSPEED || val > babinfo->max_speed) {
  1198. quit(1, "%s"INVOP"%s '%s' must be %d <= %s <= %d",
  1199. babcgpu->drv->dname,
  1200. bab_options[which],
  1201. ptr, BAB_ABSMINSPEED,
  1202. bab_options[which],
  1203. babinfo->max_speed);
  1204. }
  1205. babinfo->def_speed = (uint8_t)val;
  1206. // Adjust min down if is is above def specified
  1207. if (babinfo->min_speed > babinfo->def_speed)
  1208. babinfo->min_speed = babinfo->def_speed;
  1209. }
  1210. break;
  1211. case 2:
  1212. if (*ptr && tolower(*ptr) != 'd') {
  1213. val = atoi(ptr);
  1214. if (!isdigit(*ptr) || val < BAB_ABSMINSPEED || val > babinfo->def_speed) {
  1215. quit(1, "%s"INVOP"%s '%s' must be %d <= %s <= %d",
  1216. babcgpu->drv->dname,
  1217. bab_options[which],
  1218. ptr, BAB_ABSMINSPEED,
  1219. bab_options[which],
  1220. babinfo->def_speed);
  1221. }
  1222. babinfo->min_speed = (uint8_t)val;
  1223. }
  1224. break;
  1225. case 3:
  1226. if (*ptr && tolower(*ptr) != 'd') {
  1227. fval = atof(ptr);
  1228. if (!isdigit(*ptr) || fval < 0.0 || fval > 100.0) {
  1229. quit(1, "%s"INVOP"%s '%s' must be 0.0 <= %s <= 100.0",
  1230. babcgpu->drv->dname,
  1231. bab_options[which], ptr,
  1232. bab_options[which]);
  1233. }
  1234. babinfo->tune_up = fval;
  1235. }
  1236. break;
  1237. case 4:
  1238. if (*ptr && tolower(*ptr) != 'd') {
  1239. fval = atof(ptr);
  1240. if (!isdigit(*ptr) || fval < 0.0 || fval > 100.0) {
  1241. quit(1, "%s"INVOP"%s '%s' must be %f <= %s <= 100.0",
  1242. babcgpu->drv->dname,
  1243. bab_options[which],
  1244. ptr, babinfo->tune_up,
  1245. bab_options[which]);
  1246. }
  1247. babinfo->tune_down = fval;
  1248. }
  1249. break;
  1250. case 5:
  1251. if (*ptr && tolower(*ptr) != 'd') {
  1252. val = atoi(ptr);
  1253. if (!isdigit(*ptr) || val < 10000 || val > 10000000) {
  1254. quit(1, "%s"INVOP"%s '%s' must be 10,000 <= %s <= 10,000,000",
  1255. babcgpu->drv->dname,
  1256. bab_options[which], ptr,
  1257. bab_options[which]);
  1258. }
  1259. babinfo->speed_hz = (uint32_t)val;
  1260. }
  1261. break;
  1262. case 6:
  1263. if (*ptr && tolower(*ptr) != 'd') {
  1264. val = atoi(ptr);
  1265. if (!isdigit(*ptr) || val < 0 || val > 65535) {
  1266. quit(1, "%s"INVOP"%s '%s' must be 0 <= %s <= 65535",
  1267. babcgpu->drv->dname,
  1268. bab_options[which], ptr,
  1269. bab_options[which]);
  1270. }
  1271. babinfo->delay_usecs = (uint16_t)val;
  1272. }
  1273. break;
  1274. case 7:
  1275. if (*ptr && tolower(*ptr) != 'd') {
  1276. lval = atol(ptr);
  1277. if (!isdigit(*ptr) || lval < 0) {
  1278. quit(1, "%s"INVOP"%s '%s' must be %s >= 0",
  1279. babcgpu->drv->dname,
  1280. bab_options[which], ptr,
  1281. bab_options[which]);
  1282. }
  1283. babinfo->trf_delay = (uint64_t)lval;
  1284. }
  1285. break;
  1286. default:
  1287. break;
  1288. }
  1289. ptr = colon;
  1290. which++;
  1291. }
  1292. }
  1293. static void bab_detect(bool hotplug)
  1294. {
  1295. struct cgpu_info *babcgpu = NULL;
  1296. struct bab_info *babinfo = NULL;
  1297. int i;
  1298. if (hotplug)
  1299. return;
  1300. babcgpu = calloc(1, sizeof(*babcgpu));
  1301. if (unlikely(!babcgpu))
  1302. quithere(1, "Failed to calloc babcgpu");
  1303. babcgpu->drv = &bab_drv;
  1304. babcgpu->deven = DEV_ENABLED;
  1305. babcgpu->threads = 1;
  1306. babinfo = calloc(1, sizeof(*babinfo));
  1307. if (unlikely(!babinfo))
  1308. quithere(1, "Failed to calloc babinfo");
  1309. babcgpu->device_data = (void *)babinfo;
  1310. babinfo->max_speed = BAB_DEFMAXSPEED;
  1311. babinfo->def_speed = BAB_DEFSPEED;
  1312. babinfo->min_speed = BAB_ABSMINSPEED;
  1313. babinfo->tune_up = BAB_TUNEUP;
  1314. babinfo->tune_down = BAB_TUNEDOWN;
  1315. babinfo->speed_hz = BAB_SPI_SPEED;
  1316. babinfo->delay_usecs = BAB_DELAY_USECS;
  1317. babinfo->trf_delay = BAB_TRF_DELAY;
  1318. bab_get_options(babcgpu, babinfo);
  1319. for (i = 0; i < BAB_MAXCHIPS; i++) {
  1320. babinfo->chip_conf[i] = BAB_DEFCONF;
  1321. babinfo->chip_fast[i] = babinfo->def_speed;
  1322. #if UPDATE_HISTORY
  1323. babinfo->bad_fast[i] = babinfo->max_speed + 1;
  1324. #endif
  1325. }
  1326. if (!bab_init_gpio(babcgpu, babinfo, BAB_SPI_BUS, BAB_SPI_CHIP))
  1327. goto unalloc;
  1328. babinfo->sfree_list = k_new_list("SPI I/O", sizeof(SITEM),
  1329. ALLOC_SITEMS, LIMIT_SITEMS, true);
  1330. babinfo->spi_list = k_new_store(babinfo->sfree_list);
  1331. babinfo->spi_sent = k_new_store(babinfo->sfree_list);
  1332. for (i = 0; i <= BAB_MAXBANKS; i++) {
  1333. babinfo->bank_first_chip[i] = -1;
  1334. babinfo->bank_last_chip[i] = -1;
  1335. }
  1336. bab_init_chips(babcgpu, babinfo);
  1337. if (babinfo->boards) {
  1338. applog(LOG_WARNING, "%s found %d chips %d board%s",
  1339. babcgpu->drv->dname, babinfo->chips,
  1340. babinfo->boards,
  1341. (babinfo->boards == 1) ? "" : "s");
  1342. } else {
  1343. applog(LOG_WARNING, "%s found %d chips",
  1344. babcgpu->drv->dname, babinfo->chips);
  1345. }
  1346. if (babinfo->chips == 0)
  1347. goto cleanup;
  1348. if (!add_cgpu(babcgpu))
  1349. goto cleanup;
  1350. cgsem_init(&(babinfo->scan_work));
  1351. cgsem_init(&(babinfo->spi_work));
  1352. cgsem_init(&(babinfo->spi_reply));
  1353. cgsem_init(&(babinfo->process_reply));
  1354. mutex_init(&babinfo->did_lock);
  1355. mutex_init(&babinfo->nonce_lock);
  1356. babinfo->rfree_list = k_new_list("Results", sizeof(RITEM),
  1357. ALLOC_RITEMS, LIMIT_RITEMS, true);
  1358. babinfo->res_list = k_new_store(babinfo->rfree_list);
  1359. babinfo->wfree_list = k_new_list("Work", sizeof(WITEM),
  1360. ALLOC_WITEMS, LIMIT_WITEMS, true);
  1361. babinfo->available_work = k_new_store(babinfo->wfree_list);
  1362. for (i = 0; i < BAB_MAXCHIPS; i++)
  1363. babinfo->chip_work[i] = k_new_store(babinfo->wfree_list);
  1364. babinfo->nfree_list = k_new_list("Nonce History", sizeof(WITEM),
  1365. ALLOC_NITEMS, LIMIT_NITEMS, true);
  1366. for (i = 0; i < BAB_MAXCHIPS; i++) {
  1367. babinfo->good_nonces[i] = k_new_store(babinfo->nfree_list);
  1368. babinfo->bad_nonces[i] = k_new_store(babinfo->nfree_list);
  1369. }
  1370. // Exclude detection
  1371. cgtime(&(babcgpu->dev_start_tv));
  1372. // Ignore detection tests
  1373. babinfo->last_did.tv_sec = 0;
  1374. babinfo->initialised = true;
  1375. return;
  1376. cleanup:
  1377. close(babinfo->spifd);
  1378. munmap((void *)(babinfo->gpio), BAB_SPI_BUFSIZ);
  1379. unalloc:
  1380. free(babinfo);
  1381. free(babcgpu);
  1382. }
  1383. static void bab_identify(__maybe_unused struct cgpu_info *babcgpu)
  1384. {
  1385. }
  1386. // thread to do spi txrx
  1387. static void *bab_spi(void *userdata)
  1388. {
  1389. struct cgpu_info *babcgpu = (struct cgpu_info *)userdata;
  1390. struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
  1391. struct timeval start, stop, send, now;
  1392. K_ITEM *sitem, *witem;
  1393. double wait, delay;
  1394. int chip, band;
  1395. applog(LOG_DEBUG, "%s%i: SPIing...",
  1396. babcgpu->drv->name, babcgpu->device_id);
  1397. // Wait until we're ready
  1398. while (babcgpu->shutdown == false) {
  1399. if (babinfo->initialised) {
  1400. break;
  1401. }
  1402. cgsleep_ms(3);
  1403. }
  1404. cgtime(&start);
  1405. while (babcgpu->shutdown == false) {
  1406. K_WLOCK(babinfo->spi_list);
  1407. sitem = k_unlink_tail(babinfo->spi_list);
  1408. K_WUNLOCK(babinfo->spi_list);
  1409. if (!sitem) {
  1410. cgtime(&stop);
  1411. wait = us_tdiff(&stop, &start);
  1412. if (wait > BAB_LONG_uS) {
  1413. applog(LOG_WARNING, "%s%i: SPI waiting %fs ...",
  1414. babcgpu->drv->name,
  1415. babcgpu->device_id,
  1416. (float)wait / 1000000.0);
  1417. cgsem_mswait(&(babinfo->spi_work), BAB_LONG_WAIT_mS);
  1418. } else
  1419. cgsem_mswait(&(babinfo->spi_work), (int)((BAB_LONG_uS - wait) / 1000));
  1420. continue;
  1421. }
  1422. // TODO: need an LP/urgent flag to skip this possible cgsem_mswait()
  1423. // maybe zero last_sent_work.tv_sec ?
  1424. while (babinfo->last_sent_work.tv_sec) {
  1425. cgtime(&now);
  1426. delay = tdiff(&now, &(babinfo->last_sent_work)) * 1000.0;
  1427. if (delay < BAB_EXPECTED_WORK_DELAY_mS)
  1428. cgsem_mswait(&(babinfo->spi_work), BAB_EXPECTED_WORK_DELAY_mS - delay);
  1429. else
  1430. break;
  1431. }
  1432. /*
  1433. * TODO: handle if an LP happened after bab_do_work() started
  1434. * i.e. we don't want to send the work
  1435. * Have an LP counter that at this point would show the work
  1436. * is stale - so don't send it
  1437. */
  1438. cgtime(&send);
  1439. bab_txrx(sitem, false);
  1440. cgtime(&start);
  1441. // The work isn't added to the chip until it has been sent
  1442. K_WLOCK(babinfo->wfree_list);
  1443. for (chip = 0; chip < babinfo->chips; chip++) {
  1444. witem = DATAS(sitem)->witems[chip];
  1445. if (witem) {
  1446. memcpy(&(DATAW(witem)->work_start), &(DATAS(sitem)->work_start),
  1447. sizeof(DATAW(witem)->work_start));
  1448. k_add_head(babinfo->chip_work[chip], witem);
  1449. #if UPDATE_HISTORY
  1450. babinfo->work_count[chip]++;
  1451. #endif
  1452. if (babinfo->first_work[chip].tv_sec == 0)
  1453. memcpy(&(babinfo->first_work[chip]), &send, sizeof(send));
  1454. }
  1455. }
  1456. K_WUNLOCK(babinfo->wfree_list);
  1457. K_WLOCK(babinfo->spi_sent);
  1458. k_add_head(babinfo->spi_sent, sitem);
  1459. K_WUNLOCK(babinfo->spi_sent);
  1460. cgsem_post(&(babinfo->spi_reply));
  1461. // Store stats
  1462. if (babinfo->last_sent_work.tv_sec) {
  1463. delay = tdiff(&send, &(babinfo->last_sent_work));
  1464. babinfo->delay_count++;
  1465. if (babinfo->delay_min == 0 || babinfo->delay_min > delay)
  1466. babinfo->delay_min = delay;
  1467. if (babinfo->delay_max < delay)
  1468. babinfo->delay_max = delay;
  1469. if (delay < BAB_DELAY_BASE)
  1470. band = 0;
  1471. else if (delay >= (BAB_DELAY_BASE+BAB_DELAY_STEP*(BAB_DELAY_BANDS+1)))
  1472. band = BAB_DELAY_BANDS+1;
  1473. else
  1474. band = (int)(((double)delay - BAB_DELAY_BASE) / BAB_DELAY_STEP) + 1;
  1475. babinfo->delay_bands[band]++;
  1476. }
  1477. memcpy(&(babinfo->last_sent_work), &send, sizeof(start));
  1478. delay = tdiff(&start, &send);
  1479. babinfo->send_count++;
  1480. babinfo->send_total += delay;
  1481. if (babinfo->send_min == 0 || babinfo->send_min > delay)
  1482. babinfo->send_min = delay;
  1483. if (babinfo->send_max < delay)
  1484. babinfo->send_max = delay;
  1485. cgsem_mswait(&(babinfo->spi_work), BAB_STD_WAIT_mS);
  1486. }
  1487. return NULL;
  1488. }
  1489. static void bab_flush_work(struct cgpu_info *babcgpu)
  1490. {
  1491. struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
  1492. applog(LOG_DEBUG, "%s%i: flushing work",
  1493. babcgpu->drv->name, babcgpu->device_id);
  1494. mutex_lock(&(babinfo->did_lock));
  1495. babinfo->last_did.tv_sec = 0;
  1496. mutex_unlock(&(babinfo->did_lock));
  1497. cgsem_post(&(babinfo->scan_work));
  1498. }
  1499. #define DATA_MERKLE7 16
  1500. #define DATA_NTIME 17
  1501. #define DATA_BITS 18
  1502. #define DATA_NONCE 19
  1503. #define WORK_MERKLE7 (16*4)
  1504. #define WORK_NTIME (17*4)
  1505. #define WORK_BITS (18*4)
  1506. #define WORK_NONCE (19*4)
  1507. #if UPDATE_HISTORY
  1508. static void process_history(struct cgpu_info *babcgpu, int chip, struct timeval *when, bool good, struct timeval *now)
  1509. {
  1510. struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
  1511. uint64_t good_nonces, bad_nonces;
  1512. uint8_t chip_fast;
  1513. double tune;
  1514. K_ITEM *item;
  1515. int i;
  1516. K_WLOCK(babinfo->nfree_list);
  1517. item = k_unlink_head(babinfo->nfree_list);
  1518. memcpy(&(DATAN(item)->found), when, sizeof(*when));
  1519. if (good)
  1520. k_add_head(babinfo->good_nonces[chip], item);
  1521. else
  1522. k_add_head(babinfo->bad_nonces[chip], item);
  1523. // Remove all expired history
  1524. for (i = 0; i < babinfo->chips; i++) {
  1525. item = babinfo->good_nonces[i]->tail;
  1526. while (item) {
  1527. if (tdiff(now, &(DATAN(item)->found)) < HISTORY_TIME_S)
  1528. break;
  1529. k_unlink_item(babinfo->good_nonces[i], item);
  1530. k_add_head(babinfo->nfree_list, item);
  1531. item = babinfo->good_nonces[i]->tail;
  1532. }
  1533. item = babinfo->bad_nonces[i]->tail;
  1534. while (item) {
  1535. if (tdiff(now, &(DATAN(item)->found)) < HISTORY_TIME_S)
  1536. break;
  1537. k_unlink_item(babinfo->bad_nonces[i], item);
  1538. k_add_head(babinfo->nfree_list, item);
  1539. item = babinfo->bad_nonces[i]->tail;
  1540. }
  1541. }
  1542. good_nonces = babinfo->good_nonces[chip]->count;
  1543. bad_nonces = babinfo->bad_nonces[chip]->count;
  1544. K_WUNLOCK(babinfo->nfree_list);
  1545. // Tuning ...
  1546. if (tdiff(now, &(babinfo->first_work[chip])) >= HISTORY_TIME_S &&
  1547. tdiff(now, &(babinfo->last_tune[chip])) >= HISTORY_TIME_S &&
  1548. (good_nonces + bad_nonces) > 0) {
  1549. chip_fast = babinfo->chip_fast[chip];
  1550. /*
  1551. * If bad then step it down and remember the speed
  1552. * TODO: does a speed change reset the chip? Or is there a reset?
  1553. */
  1554. if (good_nonces == 0) {
  1555. if (chip_fast > babinfo->min_speed) {
  1556. if (babinfo->bad_fast[chip] > chip_fast)
  1557. babinfo->bad_fast[chip] = chip_fast;
  1558. babinfo->chip_fast[chip]--;
  1559. applog(LOG_WARNING, "%s%d: Chip %d BAD - speed down from %d to %d",
  1560. babcgpu->drv->name, babcgpu->device_id,
  1561. chip, (int)chip_fast, (int)chip_fast - 1);
  1562. } else {
  1563. /*
  1564. * Permanently BAD since we're already at the minumum speed
  1565. * but only getting bad nonces
  1566. */
  1567. if (babinfo->bad_msg[chip] == false) {
  1568. applog(LOG_WARNING, "%s%d: Chip %d BAD - at min speed %d",
  1569. babcgpu->drv->name, babcgpu->device_id,
  1570. chip, (int)chip_fast);
  1571. babinfo->bad_msg[chip] = true;
  1572. }
  1573. }
  1574. goto tune_over;
  1575. }
  1576. /*
  1577. * It 'was' permanently BAD but a good nonce came back!
  1578. */
  1579. if (babinfo->bad_msg[chip]) {
  1580. applog(LOG_WARNING, "%s%d: Chip %d REVIVED - at speed %d",
  1581. babcgpu->drv->name, babcgpu->device_id,
  1582. chip, (int)chip_fast);
  1583. babinfo->bad_msg[chip] = false;
  1584. }
  1585. /*
  1586. * Since we have found 'some' nonces -
  1587. * make sure bad_fast is higher than current chip_fast
  1588. */
  1589. if (babinfo->bad_fast[chip] <= chip_fast)
  1590. babinfo->bad_fast[chip] = chip_fast + 1;
  1591. tune = (double)bad_nonces / (double)(good_nonces + bad_nonces) * 100.0;
  1592. /*
  1593. * TODO: it appears some chips just get a % bad at low speeds
  1594. * so we should handle them by weighting the speed reduction vs
  1595. * the HW% gained from the reduction (i.e. GH/s)
  1596. * Maybe handle that when they hit min_speed, then do a gradual speed
  1597. * up verifying if it is really making GH/s worse or better
  1598. */
  1599. // Tune it down if error rate is too high (and it's above min)
  1600. if (tune >= babinfo->tune_down && chip_fast > babinfo->min_speed) {
  1601. babinfo->chip_fast[chip]--;
  1602. applog(LOG_WARNING, "%s%d: Chip %d High errors %.2f%% - speed down %d to %d",
  1603. babcgpu->drv->name, babcgpu->device_id,
  1604. chip, tune, (int)chip_fast, (int)chip_fast - 1);
  1605. goto tune_over;
  1606. }
  1607. /*
  1608. * TODO: if we are at bad_fast-1 and tune_up
  1609. * and bad_fast was set more than some time limit ago
  1610. * then consider increasing bad_fast by 1?
  1611. */
  1612. // Tune it up if error rate is low enough
  1613. if (tune <= babinfo->tune_up &&
  1614. chip_fast < babinfo->max_speed &&
  1615. chip_fast < (babinfo->bad_fast[chip] - 1)) {
  1616. babinfo->chip_fast[chip]++;
  1617. applog(LOG_WARNING, "%s%d: Chip %d Low errors %.2f%% - speed up %d to %d",
  1618. babcgpu->drv->name, babcgpu->device_id,
  1619. chip, tune, (int)chip_fast, (int)chip_fast + 1);
  1620. goto tune_over;
  1621. }
  1622. tune_over:
  1623. cgtime(&(babinfo->last_tune[chip]));
  1624. }
  1625. }
  1626. #endif
  1627. /*
  1628. * Find the matching work item by checking each nonce against
  1629. * work items for the nonces chip
  1630. */
  1631. static K_ITEM *process_nonce(struct thr_info *thr, struct cgpu_info *babcgpu, K_ITEM *ritem, uint32_t raw_nonce, K_ITEM *newest_witem)
  1632. {
  1633. struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
  1634. unsigned int links, proc_links, work_links, tests;
  1635. int try_sta, try_fin, offset;
  1636. K_ITEM *witem, *wtail;
  1637. struct timeval now;
  1638. bool not_first_reply;
  1639. uint32_t nonce;
  1640. int chip;
  1641. chip = DATAR(ritem)->chip;
  1642. not_first_reply = DATAR(ritem)->not_first_reply;
  1643. babinfo->chip_nonces[chip]++;
  1644. /*
  1645. * We can grab the head of the chip work queue and then release
  1646. * the lock and follow it to the end and back, since the other
  1647. * thread will only add items above the head - it wont touch
  1648. * any of the prev/next pointers from the head to the end -
  1649. * except the head->prev pointer may get changed
  1650. */
  1651. K_RLOCK(babinfo->chip_work[chip]);
  1652. witem = babinfo->chip_work[chip]->head;
  1653. K_RUNLOCK(babinfo->chip_work[chip]);
  1654. if (!witem) {
  1655. applog(LOG_ERR, "%s%i: chip %d has no work, 1 nonce discarded!",
  1656. babcgpu->drv->name, babcgpu->device_id, chip);
  1657. babinfo->untested_nonces++;
  1658. return newest_witem;
  1659. }
  1660. babinfo->tested_nonces++;
  1661. if ((raw_nonce & 0xff) < 0x1c) {
  1662. // Will only be this offset
  1663. try_sta = BAB_OFF_0x1C_STA;
  1664. try_fin = BAB_OFF_0x1C_FIN;
  1665. } else {
  1666. // Will only be one of the other offsets
  1667. try_sta = BAB_OFF_OTHER_STA;
  1668. try_fin = BAB_OFF_OTHER_FIN;
  1669. }
  1670. nonce = bab_decnonce(raw_nonce);
  1671. cgtime(&now);
  1672. tests = links = proc_links = work_links = 0;
  1673. wtail = witem;
  1674. while (wtail && wtail->next) {
  1675. work_links++;
  1676. wtail = wtail->next;
  1677. }
  1678. while (wtail) {
  1679. if (!(DATAW(wtail)->work)) {
  1680. applog(LOG_ERR, "%s%i: chip %d witem links %d has no work!",
  1681. babcgpu->drv->name,
  1682. babcgpu->device_id,
  1683. chip, links);
  1684. } else {
  1685. if (ms_tdiff(&now, &(DATAW(wtail)->work_start)) >= BAB_WORK_EXPIRE_mS)
  1686. proc_links--;
  1687. else {
  1688. for (offset = try_sta; offset <= try_fin; offset++) {
  1689. tests++;
  1690. if (test_nonce(DATAW(wtail)->work, nonce + bab_nonce_offsets[offset])) {
  1691. submit_tested_work(thr, DATAW(wtail)->work);
  1692. babinfo->nonce_offset_count[offset]++;
  1693. babinfo->chip_good[chip]++;
  1694. DATAW(wtail)->nonces++;
  1695. mutex_lock(&(babinfo->nonce_lock));
  1696. babinfo->new_nonces++;
  1697. mutex_unlock(&(babinfo->nonce_lock));
  1698. babinfo->ok_nonces++;
  1699. babinfo->total_tests += tests;
  1700. if (babinfo->max_tests_per_nonce < tests)
  1701. babinfo->max_tests_per_nonce = tests;
  1702. babinfo->total_links += links;
  1703. babinfo->total_proc_links += proc_links;
  1704. if (babinfo->max_links < links)
  1705. babinfo->max_links = links;
  1706. if (babinfo->max_proc_links < proc_links)
  1707. babinfo->max_proc_links = proc_links;
  1708. babinfo->total_work_links += work_links;
  1709. babinfo->chip_cont_bad[chip] = 0;
  1710. #if UPDATE_HISTORY
  1711. process_history(babcgpu, chip,
  1712. &(DATAR(ritem)->when),
  1713. true, &now);
  1714. #endif
  1715. if (newest_witem == NULL ||
  1716. ms_tdiff(&(DATAW(wtail)->work_start),
  1717. &(DATAW(newest_witem)->work_start)) < 0)
  1718. return wtail;
  1719. return newest_witem;
  1720. }
  1721. }
  1722. }
  1723. }
  1724. if (wtail == witem)
  1725. break;
  1726. wtail = wtail->prev;
  1727. links++;
  1728. proc_links++;
  1729. }
  1730. if (not_first_reply) {
  1731. babinfo->chip_bad[chip]++;
  1732. inc_hw_errors(thr);
  1733. babinfo->fail++;
  1734. babinfo->fail_total_tests += tests;
  1735. babinfo->fail_total_links += links;
  1736. babinfo->fail_total_work_links += work_links;
  1737. babinfo->chip_cont_bad[chip]++;
  1738. if (babinfo->chip_max_bad[chip] < babinfo->chip_cont_bad[chip])
  1739. babinfo->chip_max_bad[chip] = babinfo->chip_cont_bad[chip];
  1740. // Handle chips with only bad results
  1741. if (babinfo->disabled[chip] == false &&
  1742. babinfo->chip_good[chip] == 0 &&
  1743. babinfo->chip_bad[chip] >= BAB_BAD_COUNT &&
  1744. tdiff(&now, &(babinfo->first_work[chip])) >= BAB_BAD_TO_MIN) {
  1745. if (babinfo->chip_fast[chip] > babinfo->min_speed)
  1746. babinfo->chip_fast[chip] = babinfo->min_speed;
  1747. else if (tdiff(&now, &(babinfo->first_work[chip])) > BAB_BAD_DEAD) {
  1748. babinfo->disabled[chip] = true;
  1749. babinfo->total_disabled++;
  1750. applog(LOG_ERR, "%s%i: chip %d disabled!",
  1751. babcgpu->drv->name,
  1752. babcgpu->device_id,
  1753. chip);
  1754. }
  1755. }
  1756. #if UPDATE_HISTORY
  1757. process_history(babcgpu, chip, &(DATAR(ritem)->when), false, &now);
  1758. #endif
  1759. } else {
  1760. babinfo->initial_ignored++;
  1761. babinfo->ign_total_tests += tests;
  1762. babinfo->ign_total_links += links;
  1763. babinfo->ign_total_work_links += work_links;
  1764. }
  1765. return newest_witem;
  1766. }
  1767. /*
  1768. * On completion discard any work items older than BAB_WORK_EXPIRE_mS
  1769. * and any work items of the chip older than the work of the newest nonce work item
  1770. */
  1771. static void oknonces(struct thr_info *thr, struct cgpu_info *babcgpu, K_ITEM *ritem)
  1772. {
  1773. uint32_t raw_nonce;
  1774. K_ITEM *witem;
  1775. int nonces;
  1776. witem = NULL;
  1777. for (nonces = 0; nonces < DATAR(ritem)->nonces; nonces++) {
  1778. raw_nonce = DATAR(ritem)->nonce[nonces];
  1779. witem = process_nonce(thr, babcgpu, ritem, raw_nonce, witem);
  1780. }
  1781. cleanup_older(babcgpu, DATAR(ritem)->chip, witem);
  1782. }
  1783. // Check at least every ...
  1784. #define BAB_RESULT_DELAY_mS 999
  1785. // Results checking thread
  1786. static void *bab_res(void *userdata)
  1787. {
  1788. struct cgpu_info *babcgpu = (struct cgpu_info *)userdata;
  1789. struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
  1790. struct thr_info *thr = babcgpu->thr[0];
  1791. K_ITEM *ritem;
  1792. applog(LOG_DEBUG, "%s%i: Results...",
  1793. babcgpu->drv->name, babcgpu->device_id);
  1794. // Wait until we're ready
  1795. while (babcgpu->shutdown == false) {
  1796. if (babinfo->initialised) {
  1797. break;
  1798. }
  1799. cgsleep_ms(3);
  1800. }
  1801. ritem = NULL;
  1802. while (babcgpu->shutdown == false) {
  1803. K_WLOCK(babinfo->res_list);
  1804. if (ritem) {
  1805. // Release the old one
  1806. k_add_head(babinfo->rfree_list, ritem);
  1807. ritem = NULL;
  1808. }
  1809. // Check for a new one
  1810. ritem = k_unlink_tail(babinfo->res_list);
  1811. K_WUNLOCK(babinfo->res_list);
  1812. if (!ritem) {
  1813. cgsem_mswait(&(babinfo->process_reply), BAB_RESULT_DELAY_mS);
  1814. continue;
  1815. }
  1816. oknonces(thr, babcgpu, ritem);
  1817. }
  1818. return NULL;
  1819. }
  1820. /*
  1821. * 1.0s per nonce = 4.2GH/s
  1822. * 0.9s per nonce = 4.8GH/s
  1823. * On a slow machine, reducing this may resolve:
  1824. * BaB0: SPI waiting 1.2...s
  1825. */
  1826. #define BAB_STD_WORK_DELAY_uS 900000
  1827. static bool bab_do_work(struct cgpu_info *babcgpu)
  1828. {
  1829. struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
  1830. int work_items = 0;
  1831. K_ITEM *witem, *sitem, *ritem;
  1832. struct timeval when, now;
  1833. double delay;
  1834. int chip, rep, j, nonces, spie = 0, miso = 0;
  1835. uint32_t nonce, spichk;
  1836. bool res;
  1837. cgtime(&now);
  1838. mutex_lock(&(babinfo->did_lock));
  1839. delay = us_tdiff(&now, &(babinfo->last_did));
  1840. mutex_unlock(&(babinfo->did_lock));
  1841. if (delay < BAB_STD_WORK_DELAY_uS)
  1842. return false;
  1843. K_WLOCK(babinfo->sfree_list);
  1844. sitem = k_unlink_head_zero(babinfo->sfree_list);
  1845. K_WUNLOCK(babinfo->sfree_list);
  1846. for (chip = 0; chip < babinfo->chips; chip++) {
  1847. if (!(babinfo->disabled[chip])) {
  1848. // TODO: ignore stale work
  1849. K_WLOCK(babinfo->available_work);
  1850. witem = k_unlink_tail(babinfo->available_work);
  1851. K_WUNLOCK(babinfo->available_work);
  1852. if (!witem) {
  1853. applog(LOG_ERR, "%s%i: short work list (%d) %d expected %d - reset",
  1854. babcgpu->drv->name, babcgpu->device_id,
  1855. chip, work_items,
  1856. babinfo->chips - babinfo->total_disabled);
  1857. // Put them back in the order they were taken
  1858. K_WLOCK(babinfo->available_work);
  1859. for (j = chip-1; j >= 0; j--) {
  1860. witem = DATAS(sitem)->witems[j];
  1861. if (witem)
  1862. k_add_tail(babinfo->available_work, witem);
  1863. }
  1864. K_WUNLOCK(babinfo->available_work);
  1865. K_WLOCK(babinfo->sfree_list);
  1866. k_add_head(babinfo->sfree_list, sitem);
  1867. K_WUNLOCK(babinfo->sfree_list);
  1868. return false;
  1869. }
  1870. /*
  1871. * TODO: do this when we get work except on LP?
  1872. * (not LP so we only do ms3steps for work required)
  1873. * Though that may more likely trigger the applog(short work list) above?
  1874. */
  1875. if (DATAW(witem)->ci_setup == false) {
  1876. memcpy((void *)&(DATAW(witem)->chip_input.midstate[0]),
  1877. DATAW(witem)->work->midstate,
  1878. sizeof(DATAW(witem)->work->midstate));
  1879. memcpy((void *)&(DATAW(witem)->chip_input.merkle7),
  1880. (void *)&(DATAW(witem)->work->data[WORK_MERKLE7]),
  1881. MERKLE_BYTES);
  1882. bab_ms3steps((void *)&(DATAW(witem)->chip_input));
  1883. DATAW(witem)->ci_setup = true;
  1884. }
  1885. DATAS(sitem)->witems[chip] = witem;
  1886. work_items++;
  1887. }
  1888. }
  1889. // Send
  1890. bab_put(babinfo, sitem);
  1891. // Receive
  1892. res = bab_get(babcgpu, babinfo, &when);
  1893. if (!res) {
  1894. applog(LOG_DEBUG, "%s%i: didn't get work reply ...",
  1895. babcgpu->drv->name, babcgpu->device_id);
  1896. return false;
  1897. }
  1898. applog(LOG_DEBUG, "%s%i: Did get work reply ...",
  1899. babcgpu->drv->name, babcgpu->device_id);
  1900. for (chip = 0; chip < babinfo->chips; chip++) {
  1901. if (!(babinfo->disabled[chip])) {
  1902. K_WLOCK(babinfo->rfree_list);
  1903. ritem = k_unlink_head(babinfo->rfree_list);
  1904. K_WUNLOCK(babinfo->rfree_list);
  1905. DATAR(ritem)->chip = chip;
  1906. DATAR(ritem)->not_first_reply = babinfo->not_first_reply[chip];
  1907. memcpy(&(DATAR(ritem)->when), &when, sizeof(when));
  1908. spichk = babinfo->chip_results[chip].spichk;
  1909. if (spichk != 0 && spichk != 0xffffffff) {
  1910. babinfo->chip_spie[chip]++;
  1911. spie++;
  1912. // Test the results anyway
  1913. }
  1914. nonces = 0;
  1915. for (rep = 0; rep < BAB_REPLY_NONCES; rep++) {
  1916. nonce = babinfo->chip_results[chip].nonce[rep];
  1917. if (nonce != babinfo->chip_prev[chip].nonce[rep]) {
  1918. if ((nonce & BAB_EVIL_MASK) == BAB_EVIL_NONCE)
  1919. babinfo->discarded_e0s++;
  1920. else
  1921. DATAR(ritem)->nonce[nonces++] = nonce;
  1922. }
  1923. }
  1924. if (nonces == BAB_REPLY_NONCES) {
  1925. babinfo->chip_miso[chip]++;
  1926. miso++;
  1927. // Test the results anyway
  1928. }
  1929. /*
  1930. * Send even with zero nonces
  1931. * so cleanup_older() is called for the chip
  1932. */
  1933. DATAR(ritem)->nonces = nonces;
  1934. K_WLOCK(babinfo->res_list);
  1935. k_add_head(babinfo->res_list, ritem);
  1936. K_WUNLOCK(babinfo->res_list);
  1937. cgsem_post(&(babinfo->process_reply));
  1938. babinfo->not_first_reply[chip] = true;
  1939. memcpy((void *)(&(babinfo->chip_prev[chip])),
  1940. (void *)(&(babinfo->chip_results[chip])),
  1941. sizeof(struct bab_work_reply));
  1942. }
  1943. }
  1944. applog(LOG_DEBUG, "Work: items:%d spie:%d miso:%d", work_items, spie, miso);
  1945. return true;
  1946. }
  1947. static bool bab_thread_prepare(struct thr_info *thr)
  1948. {
  1949. struct cgpu_info *babcgpu = thr->cgpu;
  1950. struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
  1951. if (thr_info_create(&(babinfo->spi_thr), NULL, bab_spi, (void *)babcgpu)) {
  1952. applog(LOG_ERR, "%s%i: SPI thread create failed",
  1953. babcgpu->drv->name, babcgpu->device_id);
  1954. return false;
  1955. }
  1956. pthread_detach(babinfo->spi_thr.pth);
  1957. /*
  1958. * We require a seperate results checking thread since there is a lot
  1959. * of work done checking the results multiple times - thus we don't
  1960. * want that delay affecting sending/receiving work to/from the device
  1961. */
  1962. if (thr_info_create(&(babinfo->res_thr), NULL, bab_res, (void *)babcgpu)) {
  1963. applog(LOG_ERR, "%s%i: Results thread create failed",
  1964. babcgpu->drv->name, babcgpu->device_id);
  1965. return false;
  1966. }
  1967. pthread_detach(babinfo->res_thr.pth);
  1968. return true;
  1969. }
  1970. static void bab_shutdown(struct thr_info *thr)
  1971. {
  1972. struct cgpu_info *babcgpu = thr->cgpu;
  1973. struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
  1974. int i;
  1975. applog(LOG_DEBUG, "%s%i: shutting down",
  1976. babcgpu->drv->name, babcgpu->device_id);
  1977. for (i = 0; i < babinfo->chips; i++)
  1978. // TODO: bab_shutdown(babcgpu, babinfo, i);
  1979. ;
  1980. babcgpu->shutdown = true;
  1981. }
  1982. static bool bab_queue_full(struct cgpu_info *babcgpu)
  1983. {
  1984. struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
  1985. int roll, roll_limit = BAB_MAX_ROLLTIME;
  1986. struct work *work, *usework;
  1987. K_ITEM *item;
  1988. int count, need;
  1989. bool ret, rolled;
  1990. K_RLOCK(babinfo->available_work);
  1991. count = babinfo->available_work->count;
  1992. K_RUNLOCK(babinfo->available_work);
  1993. if (count >= (babinfo->chips - babinfo->total_disabled))
  1994. ret = true;
  1995. else {
  1996. need = (babinfo->chips - babinfo->total_disabled) - count;
  1997. work = get_queued(babcgpu);
  1998. if (work) {
  1999. if (roll_limit > work->drv_rolllimit)
  2000. roll_limit = work->drv_rolllimit;
  2001. roll = 0;
  2002. do {
  2003. if (roll == 0) {
  2004. usework = work;
  2005. babinfo->work_unrolled++;
  2006. rolled = false;
  2007. } else {
  2008. usework = copy_work_noffset(work, roll);
  2009. babinfo->work_rolled++;
  2010. rolled = true;
  2011. }
  2012. K_WLOCK(babinfo->wfree_list);
  2013. item = k_unlink_head_zero(babinfo->wfree_list);
  2014. DATAW(item)->work = usework;
  2015. DATAW(item)->rolled = rolled;
  2016. k_add_head(babinfo->available_work, item);
  2017. K_WUNLOCK(babinfo->wfree_list);
  2018. } while (--need > 0 && ++roll <= roll_limit);
  2019. } else {
  2020. // Avoid a hard loop when we can't get work fast enough
  2021. cgsleep_us(42);
  2022. }
  2023. if (need > 0)
  2024. ret = false;
  2025. else
  2026. ret = true;
  2027. }
  2028. return ret;
  2029. }
  2030. #define BAB_STD_DELAY_mS 100
  2031. /*
  2032. * TODO: allow this to run through more than once - the second+
  2033. * time not sending any new work unless a flush occurs since:
  2034. * at the moment we have BAB_STD_WORK_mS latency added to earliest replies
  2035. */
  2036. static int64_t bab_scanwork(__maybe_unused struct thr_info *thr)
  2037. {
  2038. struct cgpu_info *babcgpu = thr->cgpu;
  2039. struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
  2040. int64_t hashcount = 0;
  2041. int count;
  2042. bab_do_work(babcgpu);
  2043. K_RLOCK(babinfo->available_work);
  2044. count = babinfo->available_work->count;
  2045. K_RUNLOCK(babinfo->available_work);
  2046. if (count >= babinfo->chips)
  2047. cgsem_mswait(&(babinfo->scan_work), BAB_STD_DELAY_mS);
  2048. mutex_lock(&(babinfo->nonce_lock));
  2049. if (babinfo->new_nonces) {
  2050. hashcount += 0xffffffffull * babinfo->new_nonces;
  2051. babinfo->new_nonces = 0;
  2052. }
  2053. mutex_unlock(&(babinfo->nonce_lock));
  2054. return hashcount;
  2055. }
  2056. #define CHIPS_PER_STAT 16
  2057. #define FMT_RANGE "%d-%d"
  2058. static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
  2059. {
  2060. struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
  2061. uint64_t history_good[BAB_MAXCHIPS], history_bad[BAB_MAXCHIPS];
  2062. uint64_t his_good_tot, his_bad_tot;
  2063. double history_elapsed[BAB_MAXCHIPS], diff;
  2064. bool elapsed_is_good[BAB_MAXCHIPS];
  2065. int speeds[BAB_CHIP_SPEEDS];
  2066. struct api_data *root = NULL;
  2067. char data[2048];
  2068. char buf[32];
  2069. int spi_work, chip_work, sp, chip, bank, chip_off, board, last_board;
  2070. int i, to, j, k;
  2071. bool bad;
  2072. struct timeval now;
  2073. double elapsed, ghs;
  2074. float ghs_sum, his_ghs_tot;
  2075. float tot, hw;
  2076. K_ITEM *item;
  2077. if (babinfo->initialised == false)
  2078. return NULL;
  2079. memset(&speeds, 0, sizeof(speeds));
  2080. root = api_add_int(root, "Version", &(babinfo->version), true);
  2081. root = api_add_int(root, "Chips", &(babinfo->chips), true);
  2082. root = api_add_int(root, "Boards", &(babinfo->boards), true);
  2083. root = api_add_int(root, "Banks", &(babinfo->banks), true);
  2084. data[0] = '\0';
  2085. for (i = 0; i <= BAB_MAXBANKS; i++) {
  2086. snprintf(buf, sizeof(buf), "%s%d",
  2087. (i == 0) ? "" : " ",
  2088. babinfo->chips_per_bank[i]);
  2089. strcat(data, buf);
  2090. }
  2091. root = api_add_string(root, "Chips Per Bank", data, true);
  2092. data[0] = '\0';
  2093. for (i = 0; i <= BAB_MAXBANKS; i++) {
  2094. snprintf(buf, sizeof(buf), "%s%d",
  2095. (i == 0) ? "" : " ",
  2096. babinfo->missing_chips_per_bank[i]);
  2097. strcat(data, buf);
  2098. }
  2099. root = api_add_string(root, "Missing Chips Per Bank", data, true);
  2100. cgtime(&now);
  2101. elapsed = tdiff(&now, &(babcgpu->dev_start_tv));
  2102. root = api_add_elapsed(root, "Device Elapsed", &elapsed, true);
  2103. root = api_add_string(root, "History Enabled",
  2104. #if UPDATE_HISTORY
  2105. "true",
  2106. #else
  2107. "false",
  2108. #endif
  2109. true);
  2110. int chs = HISTORY_TIME_S;
  2111. root = api_add_int(root, "Chip History Limit", &chs, true);
  2112. K_RLOCK(babinfo->nfree_list);
  2113. for (i = 0; i < babinfo->chips; i++) {
  2114. item = babinfo->good_nonces[i]->tail;
  2115. elapsed_is_good[i] = true;
  2116. if (!item)
  2117. history_elapsed[i] = 0;
  2118. else
  2119. history_elapsed[i] = tdiff(&now, &(DATAN(item)->found));
  2120. item = babinfo->bad_nonces[i]->tail;
  2121. if (item) {
  2122. diff = tdiff(&now, &(DATAN(item)->found));
  2123. if (history_elapsed[i] < diff) {
  2124. history_elapsed[i] = diff;
  2125. elapsed_is_good[i] = false;
  2126. }
  2127. }
  2128. history_good[i] = babinfo->good_nonces[i]->count;
  2129. history_bad[i] = babinfo->bad_nonces[i]->count;
  2130. }
  2131. K_RUNLOCK(babinfo->nfree_list);
  2132. his_ghs_tot = 0;
  2133. for (i = 0; i < babinfo->chips; i += CHIPS_PER_STAT) {
  2134. to = i + CHIPS_PER_STAT - 1;
  2135. if (to >= babinfo->chips)
  2136. to = babinfo->chips - 1;
  2137. data[0] = '\0';
  2138. for (j = i; j <= to; j++) {
  2139. snprintf(buf, sizeof(buf),
  2140. "%s%"PRIu64,
  2141. j == i ? "" : " ",
  2142. babinfo->chip_nonces[j]);
  2143. strcat(data, buf);
  2144. }
  2145. snprintf(buf, sizeof(buf), "Nonces "FMT_RANGE, i, to);
  2146. root = api_add_string(root, buf, data, true);
  2147. data[0] = '\0';
  2148. for (j = i; j <= to; j++) {
  2149. snprintf(buf, sizeof(buf),
  2150. "%s%"PRIu64,
  2151. j == i ? "" : " ",
  2152. babinfo->chip_good[j]);
  2153. strcat(data, buf);
  2154. }
  2155. snprintf(buf, sizeof(buf), "Good "FMT_RANGE, i, to);
  2156. root = api_add_string(root, buf, data, true);
  2157. data[0] = '\0';
  2158. for (j = i; j <= to; j++) {
  2159. snprintf(buf, sizeof(buf),
  2160. "%s%"PRIu64,
  2161. j == i ? "" : " ",
  2162. babinfo->chip_bad[j]);
  2163. strcat(data, buf);
  2164. }
  2165. snprintf(buf, sizeof(buf), "Bad "FMT_RANGE, i, to);
  2166. root = api_add_string(root, buf, data, true);
  2167. data[0] = '\0';
  2168. for (j = i; j <= to; j++) {
  2169. snprintf(buf, sizeof(buf),
  2170. "%s0x%02x",
  2171. j == i ? "" : " ",
  2172. (int)(babinfo->chip_conf[j]));
  2173. strcat(data, buf);
  2174. }
  2175. snprintf(buf, sizeof(buf), "Conf "FMT_RANGE, i, to);
  2176. root = api_add_string(root, buf, data, true);
  2177. data[0] = '\0';
  2178. for (j = i; j <= to; j++) {
  2179. snprintf(buf, sizeof(buf),
  2180. "%s%d",
  2181. j == i ? "" : " ",
  2182. (int)(babinfo->chip_fast[j]));
  2183. strcat(data, buf);
  2184. }
  2185. snprintf(buf, sizeof(buf), "Fast "FMT_RANGE, i, to);
  2186. root = api_add_string(root, buf, data, true);
  2187. data[0] = '\0';
  2188. for (j = i; j <= to; j++) {
  2189. snprintf(buf, sizeof(buf),
  2190. "%s%d",
  2191. j == i ? "" : " ",
  2192. (int)(babinfo->chip_spie[j]));
  2193. strcat(data, buf);
  2194. }
  2195. snprintf(buf, sizeof(buf), "Spie "FMT_RANGE, i, to);
  2196. root = api_add_string(root, buf, data, true);
  2197. data[0] = '\0';
  2198. for (j = i; j <= to; j++) {
  2199. snprintf(buf, sizeof(buf),
  2200. "%s%d",
  2201. j == i ? "" : " ",
  2202. (int)(babinfo->chip_miso[j]));
  2203. strcat(data, buf);
  2204. }
  2205. snprintf(buf, sizeof(buf), "Miso "FMT_RANGE, i, to);
  2206. root = api_add_string(root, buf, data, true);
  2207. data[0] = '\0';
  2208. for (j = i; j <= to; j++) {
  2209. tot = (float)(babinfo->chip_good[j] + babinfo->chip_bad[j]);
  2210. if (tot != 0)
  2211. hw = 100.0 * (float)(babinfo->chip_bad[j]) / tot;
  2212. else
  2213. hw = 0;
  2214. snprintf(buf, sizeof(buf),
  2215. "%s%.3f",
  2216. j == i ? "" : " ", hw);
  2217. strcat(data, buf);
  2218. }
  2219. snprintf(buf, sizeof(buf), "HW%% "FMT_RANGE, i, to);
  2220. root = api_add_string(root, buf, data, true);
  2221. ghs_sum = 0;
  2222. data[0] = '\0';
  2223. for (j = i; j <= to; j++) {
  2224. if (elapsed > 0) {
  2225. ghs = (double)(babinfo->chip_good[j]) * 0xffffffffull /
  2226. elapsed / 1000000000.0;
  2227. } else
  2228. ghs = 0;
  2229. snprintf(buf, sizeof(buf),
  2230. "%s%.3f",
  2231. j == i ? "" : " ", ghs);
  2232. strcat(data, buf);
  2233. ghs_sum += (float)ghs;
  2234. }
  2235. snprintf(buf, sizeof(buf), "GHs "FMT_RANGE, i, to);
  2236. root = api_add_string(root, buf, data, true);
  2237. snprintf(buf, sizeof(buf), "Sum GHs "FMT_RANGE, i, to);
  2238. root = api_add_avg(root, buf, &ghs_sum, true);
  2239. data[0] = '\0';
  2240. for (j = i; j <= to; j++) {
  2241. snprintf(buf, sizeof(buf),
  2242. "%s%"PRIu64,
  2243. j == i ? "" : " ",
  2244. babinfo->chip_cont_bad[j]);
  2245. strcat(data, buf);
  2246. }
  2247. snprintf(buf, sizeof(buf), "Cont-Bad "FMT_RANGE, i, to);
  2248. root = api_add_string(root, buf, data, true);
  2249. data[0] = '\0';
  2250. for (j = i; j <= to; j++) {
  2251. snprintf(buf, sizeof(buf),
  2252. "%s%"PRIu64,
  2253. j == i ? "" : " ",
  2254. babinfo->chip_max_bad[j]);
  2255. strcat(data, buf);
  2256. }
  2257. snprintf(buf, sizeof(buf), "Max-Bad "FMT_RANGE, i, to);
  2258. root = api_add_string(root, buf, data, true);
  2259. data[0] = '\0';
  2260. for (j = i; j <= to; j++) {
  2261. snprintf(buf, sizeof(buf),
  2262. "%s%"PRIu64,
  2263. j == i ? "" : " ",
  2264. history_good[j]);
  2265. strcat(data, buf);
  2266. }
  2267. snprintf(buf, sizeof(buf), "History Good "FMT_RANGE, i, to);
  2268. root = api_add_string(root, buf, data, true);
  2269. data[0] = '\0';
  2270. for (j = i; j <= to; j++) {
  2271. snprintf(buf, sizeof(buf),
  2272. "%s%"PRIu64,
  2273. j == i ? "" : " ",
  2274. history_bad[j]);
  2275. strcat(data, buf);
  2276. }
  2277. snprintf(buf, sizeof(buf), "History Bad "FMT_RANGE, i, to);
  2278. root = api_add_string(root, buf, data, true);
  2279. data[0] = '\0';
  2280. for (j = i; j <= to; j++) {
  2281. tot = (float)(history_good[j] + history_bad[j]);
  2282. if (tot != 0)
  2283. hw = 100.0 * (float)(history_bad[j]) / tot;
  2284. else
  2285. hw = 0;
  2286. snprintf(buf, sizeof(buf),
  2287. "%s%.3f",
  2288. j == i ? "" : " ", hw);
  2289. strcat(data, buf);
  2290. }
  2291. snprintf(buf, sizeof(buf), "History HW%% "FMT_RANGE, i, to);
  2292. root = api_add_string(root, buf, data, true);
  2293. ghs_sum = 0;
  2294. data[0] = '\0';
  2295. for (j = i; j <= to; j++) {
  2296. if (history_elapsed[j] > 0) {
  2297. double num = history_good[j];
  2298. // exclude the first nonce?
  2299. if (elapsed_is_good[j])
  2300. num--;
  2301. ghs = num * 0xffffffffull /
  2302. history_elapsed[j] / 1000000000.0;
  2303. } else
  2304. ghs = 0;
  2305. snprintf(buf, sizeof(buf),
  2306. "%s%.3f",
  2307. j == i ? "" : " ", ghs);
  2308. strcat(data, buf);
  2309. ghs_sum += (float)ghs;
  2310. // Setup speed range data
  2311. for (sp = 0; sp < BAB_CHIP_SPEEDS - 1; sp++) {
  2312. if (ghs <= chip_speed_ranges[sp]) {
  2313. speeds[sp]++;
  2314. break;
  2315. }
  2316. }
  2317. if (sp >= (BAB_CHIP_SPEEDS - 1))
  2318. speeds[BAB_CHIP_SPEEDS - 1]++;
  2319. }
  2320. snprintf(buf, sizeof(buf), "History GHs "FMT_RANGE, i, to);
  2321. root = api_add_string(root, buf, data, true);
  2322. snprintf(buf, sizeof(buf), "Sum History GHs "FMT_RANGE, i, to);
  2323. root = api_add_avg(root, buf, &ghs_sum, true);
  2324. his_ghs_tot += ghs_sum;
  2325. }
  2326. root = api_add_avg(root, "Total History GHs", &his_ghs_tot, true);
  2327. his_good_tot = his_bad_tot = 0;
  2328. for (i = 0; i < babinfo->chips; i++) {
  2329. his_good_tot += history_good[i];
  2330. his_bad_tot += history_bad[i];
  2331. }
  2332. if (his_good_tot + his_bad_tot)
  2333. tot = 100.0 * (float)his_bad_tot / (float)(his_good_tot + his_bad_tot);
  2334. else
  2335. tot = 0.0;
  2336. root = api_add_avg(root, "Total History HW%", &tot, true);
  2337. for (sp = 0; sp < BAB_CHIP_SPEEDS; sp++) {
  2338. if (sp < (BAB_CHIP_SPEEDS - 1))
  2339. ghs = chip_speed_ranges[sp];
  2340. else
  2341. ghs = chip_speed_ranges[BAB_CHIP_SPEEDS - 2];
  2342. snprintf(buf, sizeof(buf), "History Speed %s%.1f %s",
  2343. (sp < (BAB_CHIP_SPEEDS - 1)) ? "" : ">",
  2344. ghs, chip_speed_names[sp]);
  2345. root = api_add_int(root, buf, &(speeds[sp]), true);
  2346. }
  2347. int len, str, siz = 1024;
  2348. char *tmp = malloc(siz);
  2349. if (!tmp)
  2350. quithere(1, "OOM tmp1");
  2351. for (sp = 0; sp < 2; sp++) {
  2352. tmp[0] = '\0';
  2353. len = 0;
  2354. for (i = 0; i < babinfo->chips; i++) {
  2355. if (history_elapsed[i] > 0) {
  2356. double num = history_good[i];
  2357. // exclude the first nonce?
  2358. if (elapsed_is_good[i])
  2359. num--;
  2360. ghs = num * 0xffffffffull /
  2361. history_elapsed[i] / 1000000000.0;
  2362. } else
  2363. ghs = 0;
  2364. if ((sp == 0 || ghs > chip_speed_ranges[sp-1]) &&
  2365. (ghs <= chip_speed_ranges[sp])) {
  2366. bank = babinfo->chip_bank[i];
  2367. chip_off = i;
  2368. for (j = 0; j < babinfo->chip_bank[i]; j++)
  2369. chip_off -= babinfo->chips_per_bank[j];
  2370. /*
  2371. * Bank/Board/Chip are all 1 based
  2372. * except V1 Bank = BAB_V1_BANK (0)
  2373. * If the bank has any missing chips then a "?"
  2374. * is placed after the board number
  2375. */
  2376. snprintf(buf, sizeof(buf), "%s%d/%d%s/%d",
  2377. len ? " " : "", bank,
  2378. (int)(chip_off / BAB_BOARDCHIPS)+1,
  2379. babinfo->missing_chips_per_bank[bank] ?
  2380. "?" : "",
  2381. (chip_off % BAB_BOARDCHIPS)+1);
  2382. str = strlen(buf);
  2383. while ((len + str + 1) > siz) {
  2384. siz += 1024;
  2385. tmp = realloc(tmp, siz);
  2386. if (!tmp)
  2387. quithere(1, "OOM tmp2");
  2388. }
  2389. strcpy(tmp + len, buf);
  2390. len += str;
  2391. }
  2392. }
  2393. snprintf(buf, sizeof(buf), "History %s", chip_speed_names[sp]);
  2394. root = api_add_string(root, buf, len ? tmp : "None", true);
  2395. }
  2396. free(tmp);
  2397. tmp = NULL;
  2398. switch (babinfo->version) {
  2399. case 1:
  2400. i = j = BAB_V1_BANK;
  2401. break;
  2402. case 2:
  2403. i = 1;
  2404. j = BAB_MAXBANKS;
  2405. break;
  2406. }
  2407. data[0] = '\0';
  2408. for (bank = i; bank <= j; bank++) {
  2409. if (babinfo->bank_first_chip[bank] >= 0) {
  2410. chip = babinfo->bank_first_chip[bank];
  2411. to = babinfo->bank_last_chip[bank];
  2412. for (; chip <= to; chip += BAB_BOARDCHIPS) {
  2413. bad = true;
  2414. for (k = chip; (k <= to) && (k < (chip+BAB_BOARDCHIPS)); k++) {
  2415. if (history_elapsed[k] > 0) {
  2416. double num = history_good[k];
  2417. // exclude the first nonce?
  2418. if (elapsed_is_good[k])
  2419. num--;
  2420. ghs = num * 0xffffffffull /
  2421. history_elapsed[k] / 1000000000.0;
  2422. } else
  2423. ghs = 0;
  2424. if (ghs > 0.0) {
  2425. bad = false;
  2426. break;
  2427. }
  2428. }
  2429. if (bad) {
  2430. board = (int)((float)(chip - babinfo->bank_first_chip[bank]) /
  2431. BAB_BOARDCHIPS) + 1;
  2432. snprintf(buf, sizeof(buf),
  2433. "%s%d/%d%s",
  2434. data[0] ? " " : "",
  2435. bank, board,
  2436. babinfo->missing_chips_per_bank[bank] ?
  2437. "?" : "");
  2438. strcat(data, buf);
  2439. }
  2440. }
  2441. }
  2442. }
  2443. root = api_add_string(root, "History Bad Boards", data[0] ? data : "None", true);
  2444. data[0] = '\0';
  2445. for (bank = i; bank <= j; bank++) {
  2446. if (babinfo->bank_first_chip[bank] >= 0) {
  2447. to = babinfo->bank_first_chip[bank];
  2448. chip = babinfo->bank_last_chip[bank];
  2449. for (; chip >= to; chip--) {
  2450. bad = true;
  2451. if (history_elapsed[chip] > 0) {
  2452. double num = history_good[chip];
  2453. // exclude the first nonce?
  2454. if (elapsed_is_good[chip])
  2455. num--;
  2456. ghs = num * 0xffffffffull /
  2457. history_elapsed[chip] / 1000000000.0;
  2458. } else
  2459. ghs = 0;
  2460. if (ghs > 0.0)
  2461. break;
  2462. }
  2463. /*
  2464. * The output here is: a/b+c/d
  2465. * a/b is the SPI/board that starts the Bad Chain
  2466. * c is the number of boards after a
  2467. * d is the total number of chips in the Bad Chain
  2468. * A Bad Chain is a continous set of bad chips that
  2469. * finish at the end of an SPI chain of boards
  2470. * This might be caused by the first board, or the cables attached
  2471. * to the first board, in the Bad Chain i.e. a/b
  2472. * If c is zero, it's just the last board, so it's the same as any
  2473. * other board having bad chips
  2474. */
  2475. if (chip < babinfo->bank_last_chip[bank]) {
  2476. board = (int)((float)(chip - babinfo->bank_first_chip[bank]) /
  2477. BAB_BOARDCHIPS) + 1;
  2478. last_board = (int)((float)(babinfo->bank_last_chip[bank] -
  2479. babinfo->bank_first_chip[bank]) /
  2480. BAB_BOARDCHIPS) + 1;
  2481. snprintf(buf, sizeof(buf),
  2482. "%s%d/%d%s+%d/%d",
  2483. data[0] ? " " : "",
  2484. bank, board,
  2485. babinfo->missing_chips_per_bank[bank] ?
  2486. "?" : "",
  2487. last_board - board,
  2488. babinfo->bank_last_chip[bank] - chip);
  2489. strcat(data, buf);
  2490. }
  2491. }
  2492. }
  2493. root = api_add_string(root, "History Bad Chains", data[0] ? data : "None", true);
  2494. root = api_add_int(root, "Disabled Chips", &(babinfo->total_disabled), true);
  2495. for (i = 0; i < BAB_NONCE_OFFSETS; i++) {
  2496. snprintf(buf, sizeof(buf), "Nonce Offset 0x%08x", bab_nonce_offsets[i]);
  2497. root = api_add_uint64(root, buf, &(babinfo->nonce_offset_count[i]), true);
  2498. }
  2499. root = api_add_uint64(root, "Discarded E0s", &(babinfo->discarded_e0s), true);
  2500. root = api_add_uint64(root, "Tested", &(babinfo->tested_nonces), true);
  2501. root = api_add_uint64(root, "OK", &(babinfo->ok_nonces), true);
  2502. root = api_add_uint64(root, "Total Tests", &(babinfo->total_tests), true);
  2503. root = api_add_uint64(root, "Max Tests", &(babinfo->max_tests_per_nonce), true);
  2504. float avg = babinfo->ok_nonces ? (float)(babinfo->total_tests) /
  2505. (float)(babinfo->ok_nonces) : 0;
  2506. root = api_add_avg(root, "Avg Tests", &avg, true);
  2507. root = api_add_uint64(root, "Untested", &(babinfo->untested_nonces), true);
  2508. root = api_add_uint64(root, "Work Links", &(babinfo->total_links), true);
  2509. root = api_add_uint64(root, "Work Processed Links", &(babinfo->total_proc_links), true);
  2510. root = api_add_uint64(root, "Max Links", &(babinfo->max_links), true);
  2511. root = api_add_uint64(root, "Max Processed Links", &(babinfo->max_proc_links), true);
  2512. root = api_add_uint64(root, "Total Work Links", &(babinfo->total_work_links), true);
  2513. avg = babinfo->ok_nonces ? (float)(babinfo->total_links) /
  2514. (float)(babinfo->ok_nonces) : 0;
  2515. root = api_add_avg(root, "Avg Links", &avg, true);
  2516. avg = babinfo->ok_nonces ? (float)(babinfo->total_proc_links) /
  2517. (float)(babinfo->ok_nonces) : 0;
  2518. root = api_add_avg(root, "Avg Proc Links", &avg, true);
  2519. avg = babinfo->ok_nonces ? (float)(babinfo->total_work_links) /
  2520. (float)(babinfo->ok_nonces) : 0;
  2521. root = api_add_avg(root, "Avg Work Links", &avg, true);
  2522. root = api_add_uint64(root, "Fail", &(babinfo->fail), true);
  2523. root = api_add_uint64(root, "Fail Total Tests", &(babinfo->fail_total_tests), true);
  2524. avg = babinfo->fail ? (float)(babinfo->fail_total_tests) /
  2525. (float)(babinfo->fail) : 0;
  2526. root = api_add_avg(root, "Fail Avg Tests", &avg, true);
  2527. root = api_add_uint64(root, "Fail Work Links", &(babinfo->fail_total_links), true);
  2528. root = api_add_uint64(root, "Fail Total Work Links", &(babinfo->fail_total_work_links), true);
  2529. root = api_add_uint32(root, "Initial Ignored", &(babinfo->initial_ignored), true);
  2530. root = api_add_uint64(root, "Ign Total Tests", &(babinfo->ign_total_tests), true);
  2531. root = api_add_uint64(root, "Ign Work Links", &(babinfo->ign_total_links), true);
  2532. root = api_add_uint64(root, "Ign Total Work Links", &(babinfo->ign_total_work_links), true);
  2533. chip_work = 0;
  2534. for (i = 0; i < babinfo->chips; i++)
  2535. chip_work += babinfo->chip_work[i]->count;
  2536. spi_work = babinfo->spi_list->count * babinfo->chips;
  2537. root = api_add_int(root, "WFree Total", &(babinfo->wfree_list->total), true);
  2538. root = api_add_int(root, "WFree Count", &(babinfo->wfree_list->count), true);
  2539. root = api_add_int(root, "Available Work", &(babinfo->available_work->count), true);
  2540. root = api_add_int(root, "SPI Work", &spi_work, true);
  2541. root = api_add_int(root, "Chip Work", &chip_work, true);
  2542. root = api_add_int(root, "SFree Total", &(babinfo->sfree_list->total), true);
  2543. root = api_add_int(root, "SFree Count", &(babinfo->sfree_list->count), true);
  2544. root = api_add_int(root, "SPI Waiting", &(babinfo->spi_list->count), true);
  2545. root = api_add_int(root, "SPI Sent", &(babinfo->spi_sent->count), true);
  2546. root = api_add_int(root, "RFree Total", &(babinfo->rfree_list->total), true);
  2547. root = api_add_int(root, "RFree Count", &(babinfo->rfree_list->count), true);
  2548. root = api_add_int(root, "Result Count", &(babinfo->res_list->count), true);
  2549. int used = babinfo->nfree_list->total - babinfo->nfree_list->count;
  2550. root = api_add_int(root, "NFree Total", &(babinfo->nfree_list->total), true);
  2551. root = api_add_int(root, "NFree Used", &used, true);
  2552. root = api_add_uint64(root, "Delay Count", &(babinfo->delay_count), true);
  2553. root = api_add_double(root, "Delay Min", &(babinfo->delay_min), true);
  2554. root = api_add_double(root, "Delay Max", &(babinfo->delay_max), true);
  2555. data[0] = '\0';
  2556. for (i = 0; i <= BAB_DELAY_BANDS; i++) {
  2557. snprintf(buf, sizeof(buf),
  2558. "%s<%.1f=%"PRIu64,
  2559. i == 0 ? "" : " ",
  2560. BAB_DELAY_BASE+(BAB_DELAY_STEP*i),
  2561. babinfo->delay_bands[i]);
  2562. strcat(data, buf);
  2563. }
  2564. snprintf(buf, sizeof(buf),
  2565. " >=%.1f=%"PRIu64,
  2566. BAB_DELAY_BASE+BAB_DELAY_STEP*(BAB_DELAY_BANDS+1),
  2567. babinfo->delay_bands[BAB_DELAY_BANDS+1]);
  2568. strcat(data, buf);
  2569. root = api_add_string(root, "Delay Bands", data, true);
  2570. root = api_add_uint64(root, "Send Count", &(babinfo->send_count), true);
  2571. root = api_add_double(root, "Send Total", &(babinfo->send_total), true);
  2572. avg = babinfo->send_count ? (float)(babinfo->send_total) /
  2573. (float)(babinfo->send_count) : 0;
  2574. root = api_add_avg(root, "Send Avg", &avg, true);
  2575. root = api_add_double(root, "Send Min", &(babinfo->send_min), true);
  2576. root = api_add_double(root, "Send Max", &(babinfo->send_max), true);
  2577. root = api_add_int(root, "Reply Wait", &(babinfo->reply_wait), true);
  2578. root = api_add_uint64(root, "Reply Waits", &(babinfo->reply_waits), true);
  2579. root = api_add_uint64(root, "Work Unrolled", &(babinfo->work_unrolled), true);
  2580. root = api_add_uint64(root, "Work Rolled", &(babinfo->work_rolled), true);
  2581. i = (int)(babinfo->max_speed);
  2582. root = api_add_int(root, bab_options[0], &i, true);
  2583. i = (int)(babinfo->def_speed);
  2584. root = api_add_int(root, bab_options[1], &i, true);
  2585. i = (int)(babinfo->min_speed);
  2586. root = api_add_int(root, bab_options[2], &i, true);
  2587. root = api_add_double(root, bab_options[3], &(babinfo->tune_up), true);
  2588. root = api_add_double(root, bab_options[4], &(babinfo->tune_down), true);
  2589. i = (int)(babinfo->speed_hz);
  2590. root = api_add_int(root, bab_options[5], &i, true);
  2591. i = (int)(babinfo->delay_usecs);
  2592. root = api_add_int(root, bab_options[6], &i, true);
  2593. root = api_add_uint64(root, bab_options[7], &(babinfo->trf_delay), true);
  2594. return root;
  2595. }
  2596. static void bab_get_statline_before(char *buf, size_t bufsiz, struct cgpu_info *babcgpu)
  2597. {
  2598. struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
  2599. #if UPDATE_HISTORY
  2600. struct timeval now;
  2601. double elapsed;
  2602. int i, bad = 0;
  2603. cgtime(&now);
  2604. elapsed = tdiff(&now, &(babcgpu->dev_start_tv));
  2605. // At least get 15s of nonces before saying anything is bad
  2606. if (elapsed > 15.0) {
  2607. K_RLOCK(babinfo->nfree_list);
  2608. for (i = 0; i < babinfo->chips; i++) {
  2609. if (babinfo->good_nonces[i]->count == 0 &&
  2610. babinfo->bad_nonces[i]->count > 1)
  2611. bad++;
  2612. }
  2613. K_RUNLOCK(babinfo->nfree_list);
  2614. }
  2615. tailsprintf(buf, bufsiz, "%d.%02d.%03d B:%03d D:%03d",
  2616. babinfo->banks,
  2617. babinfo->boards,
  2618. babinfo->chips,
  2619. bad,
  2620. babinfo->total_disabled);
  2621. #else
  2622. tailsprintf(buf, bufsiz, "%d.%02d.%03d D:%03d",
  2623. babinfo->banks,
  2624. babinfo->boards,
  2625. babinfo->chips,
  2626. babinfo->total_disabled);
  2627. #endif
  2628. }
  2629. #endif
  2630. struct device_drv bab_drv = {
  2631. .drv_id = DRIVER_bab,
  2632. .dname = "BlackArrowBitFuryGPIO",
  2633. .name = "BaB",
  2634. .drv_detect = bab_detect,
  2635. #ifdef LINUX
  2636. .get_api_stats = bab_api_stats,
  2637. .get_statline_before = bab_get_statline_before,
  2638. .identify_device = bab_identify,
  2639. .thread_prepare = bab_thread_prepare,
  2640. .hash_work = hash_queued_work,
  2641. .scanwork = bab_scanwork,
  2642. .queue_full = bab_queue_full,
  2643. .flush_work = bab_flush_work,
  2644. .thread_shutdown = bab_shutdown
  2645. #endif
  2646. };