driver-avalon9.c 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107
  1. /*
  2. * Copyright 2016-2017 Mikeqin <Fengling.Qin@gmail.com>
  3. * Copyright 2016 Con Kolivas <kernel@kolivas.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include <math.h>
  11. #include "config.h"
  12. #include "miner.h"
  13. #include "driver-avalon9.h"
  14. #include "crc.h"
  15. #include "sha2.h"
  16. #include "hexdump.c"
  17. #define get_fan_pwm(v) (AVA9_PWM_MAX - (v) * AVA9_PWM_MAX / 100)
  18. int opt_avalon9_temp_target = AVA9_DEFAULT_TEMP_TARGET;
  19. int opt_avalon9_fan_min = AVA9_DEFAULT_FAN_MIN;
  20. int opt_avalon9_fan_max = AVA9_DEFAULT_FAN_MAX;
  21. int opt_avalon9_voltage_level = AVA9_INVALID_VOLTAGE_LEVEL;
  22. int opt_avalon9_voltage_level_offset = AVA9_DEFAULT_VOLTAGE_LEVEL_OFFSET;
  23. int opt_avalon9_freq[AVA9_DEFAULT_PLL_CNT] =
  24. {
  25. AVA9_DEFAULT_FREQUENCY_IGNORE,
  26. AVA9_DEFAULT_FREQUENCY_IGNORE,
  27. AVA9_DEFAULT_FREQUENCY_IGNORE,
  28. AVA9_DEFAULT_FREQUENCY_IGNORE,
  29. AVA9_DEFAULT_FREQUENCY_IGNORE,
  30. AVA9_DEFAULT_FREQUENCY_IGNORE,
  31. AVA9_DEFAULT_FREQUENCY_IGNORE
  32. };
  33. int32_t opt_avalon9_adjust_volt_up_init = AVA9_DEFAULT_AJUST_VOLT_UP_INIT;
  34. uint32_t opt_avalon9_adjust_volt_up_factor = AVA9_DEFAULT_AJUST_VOLT_UP_FACTOR;
  35. uint32_t opt_avalon9_adjust_volt_up_threshold = AVA9_DEFAULT_AJUST_VOLT_UP_THRESHOLD;
  36. int32_t opt_avalon9_adjust_volt_down_init = AVA9_DEFAULT_AJUST_VOLT_DOWN_INIT;
  37. uint32_t opt_avalon9_adjust_volt_down_factor = AVA9_DEFAULT_AJUST_VOLT_DOWN_FACTOR;
  38. uint32_t opt_avalon9_adjust_volt_down_threshold = AVA9_DEFAULT_AJUST_VOLT_DOWN_THRESHOLD;
  39. uint32_t opt_avalon9_adjust_volt_time = AVA9_DEFAULT_AJUST_VOLT_TIME;
  40. uint32_t opt_avalon9_adjust_volt_enable = AVA9_DEFAULT_AJUST_VOLT_ENABLE;
  41. int opt_avalon9_freq_sel = AVA9_DEFAULT_FREQUENCY_SEL;
  42. int opt_avalon9_polling_delay = AVA9_DEFAULT_POLLING_DELAY;
  43. int opt_avalon9_aucspeed = AVA9_AUC_SPEED;
  44. int opt_avalon9_aucxdelay = AVA9_AUC_XDELAY;
  45. int opt_avalon9_smart_speed = AVA9_DEFAULT_SMART_SPEED;
  46. /*
  47. * smart speed have 2 modes
  48. * 1. auto speed by A3206 chips
  49. * 2. option 1 + adjust by average frequency
  50. */
  51. bool opt_avalon9_iic_detect = AVA9_DEFAULT_IIC_DETECT;
  52. uint32_t opt_avalon9_th_pass = AVA9_DEFAULT_TH_PASS;
  53. uint32_t opt_avalon9_th_fail = AVA9_DEFAULT_TH_FAIL;
  54. uint32_t opt_avalon9_th_init = AVA9_DEFAULT_TH_INIT;
  55. uint32_t opt_avalon9_th_ms = AVA9_DEFAULT_TH_MS;
  56. uint32_t opt_avalon9_th_timeout = AVA9_DEFAULT_TH_TIMEOUT;
  57. uint32_t opt_avalon9_th_add = AVA9_DEFAULT_TH_ADD;
  58. uint32_t opt_avalon9_th_mssel = AVA9_DEFAULT_TH_MSSEL;
  59. uint32_t opt_avalon9_nonce_mask = AVA9_DEFAULT_NONCE_MASK;
  60. uint32_t opt_avalon9_nonce_check = AVA9_DEFAULT_NONCE_CHECK;
  61. uint32_t opt_avalon9_mux_l2h = AVA9_DEFAULT_MUX_L2H;
  62. uint32_t opt_avalon9_mux_h2l = AVA9_DEFAULT_MUX_H2L;
  63. uint32_t opt_avalon9_h2ltime0_spd = AVA9_DEFAULT_H2LTIME0_SPD;
  64. uint32_t opt_avalon9_roll_enable = AVA9_DEFAULT_ROLL_ENABLE;
  65. uint32_t opt_avalon9_spdlow = AVA9_INVALID_SPDLOW;
  66. uint32_t opt_avalon9_spdhigh = AVA9_DEFAULT_SPDHIGH;
  67. uint32_t opt_avalon9_tbase = AVA9_DEFAULT_TBASE;
  68. uint32_t opt_avalon9_pid_p = AVA9_DEFAULT_PID_P;
  69. uint32_t opt_avalon9_pid_i = AVA9_DEFAULT_PID_I;
  70. uint32_t opt_avalon9_pid_d = AVA9_DEFAULT_PID_D;
  71. uint32_t opt_avalon9_lv2_th_ms = AVA9_DEFAULT_LV2_TH_MS;
  72. uint32_t opt_avalon9_lv3_th_ms = AVA9_DEFAULT_LV3_TH_MS;
  73. uint32_t opt_avalon9_lv4_th_ms = AVA9_DEFAULT_LV4_TH_MS;
  74. uint32_t opt_avalon9_lv5_th_ms = AVA9_DEFAULT_LV5_TH_MS;
  75. uint32_t opt_avalon9_lv6_th_ms = AVA9_DEFAULT_LV6_TH_MS;
  76. uint32_t opt_avalon9_lv7_th_ms = AVA9_DEFAULT_LV7_TH_MS;
  77. uint32_t opt_avalon9_lv2_th_add = AVA9_DEFAULT_LV2_TH_ADD;
  78. uint32_t opt_avalon9_lv3_th_add = AVA9_DEFAULT_LV3_TH_ADD;
  79. uint32_t opt_avalon9_lv4_th_add = AVA9_DEFAULT_LV4_TH_ADD;
  80. uint32_t opt_avalon9_lv5_th_add = AVA9_DEFAULT_LV5_TH_ADD;
  81. uint32_t opt_avalon9_lv6_th_add = AVA9_DEFAULT_LV6_TH_ADD;
  82. uint32_t opt_avalon9_lv7_th_add = AVA9_DEFAULT_LV7_TH_ADD;
  83. uint32_t cpm_table[] =
  84. {
  85. 0x00000000,
  86. 0x0c041205,
  87. 0x0c041203,
  88. 0x0c031103,
  89. 0x0c041103,
  90. 0x0c079183,
  91. 0x0c079503,
  92. 0x0c07ed83,
  93. 0x0c040603,
  94. 0x0c06c703,
  95. 0x0c078703,
  96. 0x0c042583,
  97. 0x0c078683,
  98. 0x0c068603,
  99. 0x0c070603,
  100. 0x0c078603,
  101. 0x0c040503,
  102. 0x0c044503,
  103. 0x0c048503,
  104. 0x0c04c503,
  105. 0x0c050503,
  106. 0x0c054503,
  107. 0x0c058503,
  108. 0x0c05c503,
  109. 0x0c060503,
  110. 0x0c064503,
  111. 0x0c068503,
  112. 0x0c06c503,
  113. 0x0c070503,
  114. 0x0c074503,
  115. 0x0c078503,
  116. 0x0c07c503,
  117. 0x0c040483,
  118. 0x0c042483,
  119. 0x0c044483,
  120. 0x0c046483,
  121. 0x0c048483,
  122. 0x0c04a483,
  123. 0x0c04c483,
  124. 0x0c04e483,
  125. 0x0c050483,
  126. 0x0c052483,
  127. 0x0c054483,
  128. 0x0c056483,
  129. 0x0c058483,
  130. 0x0c05a483,
  131. 0x0c05c483,
  132. 0x0c05e483,
  133. 0x0c060483,
  134. 0x0c062483,
  135. 0x0c064483,
  136. 0x0c066483,
  137. 0x0c068483,
  138. 0x0c06a483,
  139. 0x0c06c483,
  140. 0x0c06e483
  141. };
  142. /*700-900*/
  143. uint32_t cpm_table2[] =
  144. {
  145. 0x0c072503, //712.5
  146. 0x0c076503, //737.5
  147. 0x0c07a503, //765.5
  148. 0x0c07e503, //787.5
  149. 0x0c082503, //812.5
  150. 0x0c086503, //837.5
  151. 0x0c08a503, //865.5
  152. 0x0c08e503, //887.5
  153. };
  154. struct avalon9_dev_description avalon9_dev_table[] = {
  155. {
  156. "921",
  157. 921,
  158. 4,
  159. 26,
  160. AVA9_MM921_VIN_ADC_RATIO,
  161. AVA9_MM921_VOUT_ADC_RATIO,
  162. 8,
  163. {
  164. AVA9_DEFAULT_FREQUENCY_0M,
  165. AVA9_DEFAULT_FREQUENCY_0M,
  166. AVA9_DEFAULT_FREQUENCY_0M,
  167. AVA9_DEFAULT_FREQUENCY_0M,
  168. AVA9_DEFAULT_FREQUENCY_0M,
  169. AVA9_DEFAULT_FREQUENCY_775M,
  170. AVA9_DEFAULT_FREQUENCY_787M
  171. }
  172. },
  173. {
  174. "920P",
  175. 920,
  176. 4,
  177. 26,
  178. AVA9_MM921_VIN_ADC_RATIO,
  179. AVA9_MM921_VOUT_ADC_RATIO,
  180. 8,
  181. {
  182. AVA9_DEFAULT_FREQUENCY_0M,
  183. AVA9_DEFAULT_FREQUENCY_0M,
  184. AVA9_DEFAULT_FREQUENCY_0M,
  185. AVA9_DEFAULT_FREQUENCY_0M,
  186. AVA9_DEFAULT_FREQUENCY_0M,
  187. AVA9_DEFAULT_FREQUENCY_775M,
  188. AVA9_DEFAULT_FREQUENCY_787M
  189. }
  190. },
  191. {
  192. "920",
  193. 920,
  194. 4,
  195. 26,
  196. AVA9_MM920_VIN_ADC_RATIO,
  197. AVA9_MM920_VOUT_ADC_RATIO,
  198. 5,
  199. {
  200. AVA9_DEFAULT_FREQUENCY_0M,
  201. AVA9_DEFAULT_FREQUENCY_0M,
  202. AVA9_DEFAULT_FREQUENCY_0M,
  203. AVA9_DEFAULT_FREQUENCY_0M,
  204. AVA9_DEFAULT_FREQUENCY_0M,
  205. AVA9_DEFAULT_FREQUENCY_700M,
  206. AVA9_DEFAULT_FREQUENCY_750M
  207. }
  208. }
  209. };
  210. static uint32_t api_get_cpm(uint32_t freq)
  211. {
  212. if (freq % 25 == 0)
  213. return cpm_table[freq / 25];
  214. else
  215. return cpm_table2[(freq - 712) / 25];
  216. }
  217. static uint32_t encode_voltage(int volt_level)
  218. {
  219. if (volt_level > AVA9_DEFAULT_VOLTAGE_LEVEL_MAX)
  220. volt_level = AVA9_DEFAULT_VOLTAGE_LEVEL_MAX;
  221. else if (volt_level < AVA9_DEFAULT_VOLTAGE_LEVEL_MIN)
  222. volt_level = AVA9_DEFAULT_VOLTAGE_LEVEL_MIN;
  223. if (volt_level < 0)
  224. return 0x8080 | (-volt_level);
  225. return 0x8000 | volt_level;
  226. }
  227. static uint32_t decode_voltage(struct avalon9_info *info, int modular_id, uint32_t volt)
  228. {
  229. return (volt * info->vout_adc_ratio[modular_id] / info->asic_count[modular_id] / 100);
  230. }
  231. static uint16_t decode_vin(struct avalon9_info *info, int modular_id, uint16_t volt)
  232. {
  233. return (volt * info->vin_adc_ratio[modular_id] / 1000);
  234. }
  235. static double decode_pvt_temp(uint16_t pvt_code)
  236. {
  237. double g = 60.0;
  238. double h = 200.0;
  239. double cal5 = 4094.0;
  240. double j = -0.1;
  241. double fclkm = 6.25;
  242. /* Mode2 temperature equation */
  243. return g + h * (pvt_code / cal5 - 0.5) + j * fclkm;
  244. }
  245. static uint32_t decode_pvt_volt(uint16_t volt)
  246. {
  247. double vref = 1.20;
  248. double r = 16384.0; /* 2 ** 14 */
  249. double c;
  250. c = vref / 5.0 * (6 * (volt - 0.5) / r - 1.0);
  251. if (c < 0)
  252. c = 0;
  253. return c * 1000;
  254. }
  255. #define SERIESRESISTOR 10000
  256. #define THERMISTORNOMINAL 10000
  257. #define BCOEFFICIENT 3500
  258. #define TEMPERATURENOMINAL 25
  259. float decode_auc_temp(int value)
  260. {
  261. float ret, resistance;
  262. if (!((value > 0) && (value < 33000)))
  263. return -273;
  264. resistance = (3.3 * 10000 / value) - 1;
  265. resistance = SERIESRESISTOR / resistance;
  266. ret = resistance / THERMISTORNOMINAL;
  267. ret = logf(ret);
  268. ret /= BCOEFFICIENT;
  269. ret += 1.0 / (TEMPERATURENOMINAL + 273.15);
  270. ret = 1.0 / ret;
  271. ret -= 273.15;
  272. return ret;
  273. }
  274. #define UNPACK32(x, str) \
  275. { \
  276. *((str) + 3) = (uint8_t) ((x) ); \
  277. *((str) + 2) = (uint8_t) ((x) >> 8); \
  278. *((str) + 1) = (uint8_t) ((x) >> 16); \
  279. *((str) + 0) = (uint8_t) ((x) >> 24); \
  280. }
  281. static inline void sha256_prehash(const unsigned char *message, unsigned int len, unsigned char *digest)
  282. {
  283. int i;
  284. sha256_ctx ctx;
  285. sha256_init(&ctx);
  286. sha256_update(&ctx, message, len);
  287. for (i = 0; i < 8; i++)
  288. UNPACK32(ctx.h[i], &digest[i << 2]);
  289. }
  290. char *set_avalon9_fan(char *arg)
  291. {
  292. int val1, val2, ret;
  293. ret = sscanf(arg, "%d-%d", &val1, &val2);
  294. if (ret < 1)
  295. return "No value passed to avalon9-fan";
  296. if (ret == 1)
  297. val2 = val1;
  298. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
  299. return "Invalid value passed to avalon9-fan";
  300. opt_avalon9_fan_min = val1;
  301. opt_avalon9_fan_max = val2;
  302. return NULL;
  303. }
  304. char *set_avalon9_freq(char *arg)
  305. {
  306. int val[AVA9_DEFAULT_PLL_CNT];
  307. char *colon, *data;
  308. int i;
  309. if (!(*arg))
  310. return NULL;
  311. data = arg;
  312. memset(val, 0, sizeof(val));
  313. for (i = 0; i < AVA9_DEFAULT_PLL_CNT; i++) {
  314. colon = strchr(data, ':');
  315. if (colon)
  316. *(colon++) = '\0';
  317. else {
  318. /* last value */
  319. if (*data) {
  320. val[i] = atoi(data);
  321. if (val[i] > AVA9_DEFAULT_FREQUENCY_MAX)
  322. return "Invalid value passed to avalon9-freq";
  323. }
  324. break;
  325. }
  326. if (*data) {
  327. val[i] = atoi(data);
  328. if (val[i] > AVA9_DEFAULT_FREQUENCY_MAX)
  329. return "Invalid value passed to avalon9-freq";
  330. }
  331. data = colon;
  332. }
  333. for (i = 0; i < AVA9_DEFAULT_PLL_CNT; i++)
  334. opt_avalon9_freq[i] = val[i];
  335. return NULL;
  336. }
  337. char *set_avalon9_adjust_volt_info(char *arg)
  338. {
  339. int ret;
  340. ret = sscanf(arg, "%d-%d-%d-%d-%d-%d-%d-%d", &opt_avalon9_adjust_volt_up_init,
  341. &opt_avalon9_adjust_volt_up_factor,
  342. &opt_avalon9_adjust_volt_up_threshold,
  343. &opt_avalon9_adjust_volt_down_init,
  344. &opt_avalon9_adjust_volt_down_factor,
  345. &opt_avalon9_adjust_volt_down_threshold,
  346. &opt_avalon9_adjust_volt_time,
  347. &opt_avalon9_adjust_volt_enable);
  348. if (ret < 1)
  349. return "Invalid value for adjust volt info";
  350. return NULL;
  351. }
  352. char *set_avalon9_voltage_level(char *arg)
  353. {
  354. int val, ret;
  355. ret = sscanf(arg, "%d", &val);
  356. if (ret < 1)
  357. return "No value passed to avalon9-voltage-level";
  358. if (val < AVA9_DEFAULT_VOLTAGE_LEVEL_MIN || val > AVA9_DEFAULT_VOLTAGE_LEVEL_MAX)
  359. return "Invalid value passed to avalon9-voltage-level";
  360. opt_avalon9_voltage_level = val;
  361. return NULL;
  362. }
  363. char *set_avalon9_voltage_level_offset(char *arg)
  364. {
  365. int val, ret;
  366. ret = sscanf(arg, "%d", &val);
  367. if (ret < 1)
  368. return "No value passed to avalon9-voltage-level-offset";
  369. if (val < AVA9_DEFAULT_VOLTAGE_LEVEL_OFFSET_MIN || val > AVA9_DEFAULT_VOLTAGE_LEVEL_OFFSET_MAX)
  370. return "Invalid value passed to avalon9-voltage-level-offset";
  371. opt_avalon9_voltage_level_offset = val;
  372. return NULL;
  373. }
  374. static int avalon9_init_pkg(struct avalon9_pkg *pkg, uint8_t type, uint8_t idx, uint8_t cnt)
  375. {
  376. unsigned short crc;
  377. pkg->head[0] = AVA9_H1;
  378. pkg->head[1] = AVA9_H2;
  379. pkg->type = type;
  380. pkg->opt = 0;
  381. pkg->idx = idx;
  382. pkg->cnt = cnt;
  383. crc = crc16(pkg->data, AVA9_P_DATA_LEN);
  384. pkg->crc[0] = (crc & 0xff00) >> 8;
  385. pkg->crc[1] = crc & 0xff;
  386. return 0;
  387. }
  388. static int job_idcmp(uint8_t *job_id, char *pool_job_id)
  389. {
  390. int job_id_len;
  391. unsigned short crc, crc_expect;
  392. if (!pool_job_id)
  393. return 1;
  394. job_id_len = strlen(pool_job_id);
  395. crc_expect = crc16((unsigned char *)pool_job_id, job_id_len);
  396. crc = job_id[0] << 8 | job_id[1];
  397. if (crc_expect == crc)
  398. return 0;
  399. applog(LOG_DEBUG, "avalon9: job_id doesn't match! [%04x:%04x (%s)]",
  400. crc, crc_expect, pool_job_id);
  401. return 1;
  402. }
  403. static inline int get_temp_max(struct avalon9_info *info, int addr)
  404. {
  405. int i, j;
  406. int max = -273;
  407. for (i = 0; i < info->miner_count[addr]; i++) {
  408. for (j = 0; j < info->asic_count[addr]; j++) {
  409. if (info->temp[addr][i][j] > max)
  410. max = info->temp[addr][i][j];
  411. }
  412. }
  413. if (max < info->temp_mm[addr])
  414. max = info->temp_mm[addr];
  415. return max;
  416. }
  417. /*
  418. * Incremental PID controller
  419. *
  420. * controller input: u, output: t
  421. *
  422. * delta_u = P * [e(k) - e(k-1)] + I * e(k) + D * [e(k) - 2*e(k-1) + e(k-2)];
  423. * e(k) = t(k) - t[target];
  424. * u(k) = u(k-1) + delta_u;
  425. *
  426. * when temp_target = 95
  427. * Tenv Tmm Fan(PWM)
  428. * -30 -13 26
  429. * -20 -5 29
  430. * -10 1 33
  431. * 0 8 39
  432. * 10 15 54
  433. * 20 24 69
  434. * 30 32 93
  435. *
  436. * Fan = 0.0327 * Tmm * Tmm + 0.84 * Tmm + 31
  437. */
  438. static inline uint32_t adjust_fan(struct avalon9_info *info, int id)
  439. {
  440. int t;
  441. double delta_u;
  442. double delta_p, delta_i, delta_d;
  443. uint32_t pwm;
  444. if(info->ss_para_en[id])
  445. info->temp_target[id] = info->ss_para_target_temp[id];
  446. t = get_temp_max(info, id);
  447. /* update target error */
  448. info->pid_e[id][2] = info->pid_e[id][1];
  449. info->pid_e[id][1] = info->pid_e[id][0];
  450. info->pid_e[id][0] = t - info->temp_target[id];
  451. if (t > AVA9_DEFAULT_PID_TEMP_MAX) {
  452. info->pid_u[id] = opt_avalon9_fan_max;
  453. } else if (t < info->temp_target[id] - AVA9_DEFAULT_PID_TEMP_MIN_DIFF && info->pid_0[id] == 0) {
  454. info->pid_u[id] = opt_avalon9_fan_min;
  455. } else if (!info->pid_0[id]) {
  456. /* first, init u use temp_mm */
  457. info->pid_0[id] = 1;
  458. info->pid_u[id] = 0.0327 * info->temp_mm[id] * info->temp_mm[id] + 0.84 * info->temp_mm[id] + 31;
  459. } else {
  460. delta_p = info->pid_p[id] * (info->pid_e[id][0] - info->pid_e[id][1]);
  461. delta_i = info->pid_i[id] * info->pid_e[id][0];
  462. delta_d = info->pid_d[id] * (info->pid_e[id][0] - 2 * info->pid_e[id][1] + info->pid_e[id][2]);
  463. /*Parameter I is int type(1, 2, 3...), but should be used as a smaller value (such as 0.1, 0.01...)*/
  464. delta_u = delta_p + delta_i / 100 + delta_d;
  465. info->pid_u[id] += delta_u;
  466. }
  467. if(info->pid_u[id] > opt_avalon9_fan_max)
  468. info->pid_u[id] = opt_avalon9_fan_max;
  469. if (info->pid_u[id] < opt_avalon9_fan_min)
  470. info->pid_u[id] = opt_avalon9_fan_min;
  471. /* Round from float to int */
  472. info->fan_pct[id] = (int)(info->pid_u[id] + 0.5);
  473. pwm = get_fan_pwm(info->fan_pct[id]);
  474. return pwm;
  475. }
  476. static int decode_pkg(struct cgpu_info *avalon9, struct avalon9_ret *ar, int modular_id)
  477. {
  478. struct avalon9_info *info = avalon9->device_data;
  479. struct pool *pool, *real_pool;
  480. struct pool *pool_stratum0 = &info->pool0;
  481. struct pool *pool_stratum1 = &info->pool1;
  482. struct pool *pool_stratum2 = &info->pool2;
  483. struct thr_info *thr = NULL;
  484. unsigned short expected_crc;
  485. unsigned short actual_crc;
  486. uint32_t nonce, nonce2, ntime, miner, chip_id, tmp;
  487. uint8_t job_id[2];
  488. int pool_no;
  489. uint32_t i, j;
  490. int64_t last_diff1;
  491. uint16_t vin;
  492. if (likely(avalon9->thr))
  493. thr = avalon9->thr[0];
  494. if (ar->head[0] != AVA9_H1 && ar->head[1] != AVA9_H2) {
  495. applog(LOG_DEBUG, "%s-%d-%d: H1 %02x, H2 %02x",
  496. avalon9->drv->name, avalon9->device_id, modular_id,
  497. ar->head[0], ar->head[1]);
  498. hexdump(ar->data, 32);
  499. return 1;
  500. }
  501. expected_crc = crc16(ar->data, AVA9_P_DATA_LEN);
  502. actual_crc = ((ar->crc[0] & 0xff) << 8) | (ar->crc[1] & 0xff);
  503. if (expected_crc != actual_crc) {
  504. applog(LOG_DEBUG, "%s-%d-%d: %02x: expected crc(%04x), actual_crc(%04x)",
  505. avalon9->drv->name, avalon9->device_id, modular_id,
  506. ar->type, expected_crc, actual_crc);
  507. return 1;
  508. }
  509. switch(ar->type) {
  510. case AVA9_P_NONCE:
  511. applog(LOG_DEBUG, "%s-%d-%d: AVA9_P_NONCE", avalon9->drv->name, avalon9->device_id, modular_id);
  512. memcpy(&miner, ar->data + 0, 4);
  513. memcpy(&nonce2, ar->data + 4, 4);
  514. memcpy(&ntime, ar->data + 8, 4);
  515. memcpy(&nonce, ar->data + 12, 4);
  516. job_id[0] = ar->data[16];
  517. job_id[1] = ar->data[17];
  518. pool_no = (ar->data[18] | (ar->data[19] << 8));
  519. miner = be32toh(miner);
  520. chip_id = (miner >> 16) & 0xffff;
  521. miner &= 0xffff;
  522. ntime = be32toh(ntime);
  523. if (miner >= info->miner_count[modular_id] ||
  524. pool_no >= total_pools || pool_no < 0) {
  525. applog(LOG_DEBUG, "%s-%d-%d: Wrong miner/pool_no %d/%d",
  526. avalon9->drv->name, avalon9->device_id, modular_id,
  527. miner, pool_no);
  528. break;
  529. }
  530. nonce2 = be32toh(nonce2);
  531. nonce = be32toh(nonce);
  532. if (ntime > info->max_ntime)
  533. info->max_ntime = ntime;
  534. applog(LOG_NOTICE, "%s-%d-%d: Found! P:%d - N2:%08x N:%08x NR:%d/%d [M:%d, A:%d, C:%d - MW: (%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64")]",
  535. avalon9->drv->name, avalon9->device_id, modular_id,
  536. pool_no, nonce2, nonce, ntime, info->max_ntime,
  537. miner, chip_id, nonce & 0x7f,
  538. info->chip_matching_work[modular_id][miner][0],
  539. info->chip_matching_work[modular_id][miner][1],
  540. info->chip_matching_work[modular_id][miner][2],
  541. info->chip_matching_work[modular_id][miner][3]);
  542. real_pool = pool = pools[pool_no];
  543. if (job_idcmp(job_id, pool->swork.job_id)) {
  544. if (!job_idcmp(job_id, pool_stratum0->swork.job_id)) {
  545. applog(LOG_DEBUG, "%s-%d-%d: Match to previous stratum0! (%s)",
  546. avalon9->drv->name, avalon9->device_id, modular_id,
  547. pool_stratum0->swork.job_id);
  548. pool = pool_stratum0;
  549. } else if (!job_idcmp(job_id, pool_stratum1->swork.job_id)) {
  550. applog(LOG_DEBUG, "%s-%d-%d: Match to previous stratum1! (%s)",
  551. avalon9->drv->name, avalon9->device_id, modular_id,
  552. pool_stratum1->swork.job_id);
  553. pool = pool_stratum1;
  554. } else if (!job_idcmp(job_id, pool_stratum2->swork.job_id)) {
  555. applog(LOG_DEBUG, "%s-%d-%d: Match to previous stratum2! (%s)",
  556. avalon9->drv->name, avalon9->device_id, modular_id,
  557. pool_stratum2->swork.job_id);
  558. pool = pool_stratum2;
  559. } else {
  560. applog(LOG_ERR, "%s-%d-%d: Cannot match to any stratum! (%s)",
  561. avalon9->drv->name, avalon9->device_id, modular_id,
  562. pool->swork.job_id);
  563. if (likely(thr))
  564. inc_hw_errors(thr);
  565. info->hw_works_i[modular_id][miner]++;
  566. break;
  567. }
  568. }
  569. /* Can happen during init sequence before add_cgpu */
  570. if (unlikely(!thr))
  571. break;
  572. last_diff1 = avalon9->diff1;
  573. if (!submit_nonce2_nonce(thr, pool, real_pool, nonce2, nonce, ntime))
  574. info->hw_works_i[modular_id][miner]++;
  575. else {
  576. info->diff1[modular_id] += (avalon9->diff1 - last_diff1);
  577. info->chip_matching_work[modular_id][miner][chip_id]++;
  578. }
  579. break;
  580. case AVA9_P_STATUS:
  581. applog(LOG_DEBUG, "%s-%d-%d: AVA9_P_STATUS", avalon9->drv->name, avalon9->device_id, modular_id);
  582. hexdump(ar->data, 32);
  583. memcpy(&tmp, ar->data, 4);
  584. tmp = be32toh(tmp);
  585. info->temp_mm[modular_id] = tmp;
  586. avalon9->temp = decode_auc_temp(info->auc_sensor);
  587. memcpy(&tmp, ar->data + 4, 4);
  588. tmp = be32toh(tmp);
  589. info->fan_cpm[modular_id] = tmp;
  590. memcpy(&tmp, ar->data + 8, 4);
  591. info->local_works_i[modular_id][ar->idx] += be32toh(tmp);
  592. memcpy(&tmp, ar->data + 12, 4);
  593. info->hw_works_i[modular_id][ar->idx] += be32toh(tmp);
  594. memcpy(&tmp, ar->data + 16, 4);
  595. info->error_code[modular_id][ar->idx] = be32toh(tmp);
  596. memcpy(&tmp, ar->data + 20, 4);
  597. info->error_code[modular_id][ar->cnt] = be32toh(tmp);
  598. memcpy(&tmp, ar->data + 24, 4);
  599. info->error_crc[modular_id][ar->idx] += be32toh(tmp);
  600. break;
  601. case AVA9_P_STATUS_PMU:
  602. /* TODO: decode ntc led from PMU */
  603. applog(LOG_DEBUG, "%s-%d-%d: AVA9_P_STATUS_PMU", avalon9->drv->name, avalon9->device_id, modular_id);
  604. info->power_good[modular_id] = ar->data[16];
  605. for (i = 0; i < AVA9_DEFAULT_PMU_CNT; i++) {
  606. memcpy(&info->pmu_version[modular_id][i], ar->data + 24 + (i * 4), 4);
  607. info->pmu_version[modular_id][i][4] = '\0';
  608. }
  609. for (i = 0; i < info->miner_count[modular_id]; i++) {
  610. memcpy(&vin, ar->data + 8 + i * 2, 2);
  611. info->get_vin[modular_id][i] = decode_vin(info, modular_id, be16toh(vin));
  612. }
  613. break;
  614. case AVA9_P_STATUS_VOLT:
  615. applog(LOG_DEBUG, "%s-%d-%d: AVA9_P_STATUS_VOLT", avalon9->drv->name, avalon9->device_id, modular_id);
  616. for (i = 0; i < info->miner_count[modular_id]; i++) {
  617. memcpy(&tmp, ar->data + i * 4, 4);
  618. info->get_voltage[modular_id][i] = decode_voltage(info, modular_id, be32toh(tmp));
  619. }
  620. break;
  621. case AVA9_P_STATUS_PLL:
  622. applog(LOG_DEBUG, "%s-%d-%d: AVA9_P_STATUS_PLL", avalon9->drv->name, avalon9->device_id, modular_id);
  623. if (ar->opt) {
  624. for (i = 0; i < AVA9_DEFAULT_PLL_CNT; i++) {
  625. memcpy(&tmp, ar->data + i * 4, 4);
  626. info->get_pll[modular_id][ar->idx][i] = be32toh(tmp);
  627. }
  628. } else {
  629. for (i = 0; i < AVA9_DEFAULT_PLL_CNT; i++) {
  630. memcpy(&tmp, ar->data + i * 4, 4);
  631. info->set_frequency[modular_id][ar->idx][i] = be32toh(tmp);
  632. }
  633. }
  634. break;
  635. case AVA9_P_STATUS_PVT:
  636. applog(LOG_DEBUG, "%s-%d-%d: AVA9_P_STATUS_PVT", avalon9->drv->name, avalon9->device_id, modular_id);
  637. if (!info->asic_count[modular_id])
  638. break;
  639. if (ar->idx < info->asic_count[modular_id]) {
  640. for (i = 0; i < info->miner_count[modular_id]; i++) {
  641. memcpy(&tmp, ar->data + i * 4, 2);
  642. tmp = be16toh(tmp);
  643. info->temp[modular_id][i][ar->idx] = decode_pvt_temp(tmp);
  644. memcpy(&tmp, ar->data + i * 4 + 2, 2);
  645. tmp = be16toh(tmp);
  646. info->core_volt[modular_id][i][ar->idx] = decode_pvt_volt(tmp);
  647. }
  648. }
  649. break;
  650. case AVA9_P_STATUS_ASIC:
  651. {
  652. int miner_id;
  653. int asic_id;
  654. uint16_t freq;
  655. if (!info->asic_count[modular_id])
  656. break;
  657. miner_id = ar->idx / info->asic_count[modular_id];
  658. asic_id = ar->idx % info->asic_count[modular_id];
  659. applog(LOG_DEBUG, "%s-%d-%d: AVA9_P_STATUS_ASIC %d-%d",
  660. avalon9->drv->name, avalon9->device_id, modular_id,
  661. miner_id, asic_id);
  662. memcpy(&tmp, ar->data + 0, 4);
  663. if (tmp)
  664. info->get_asic[modular_id][miner_id][asic_id][0] = be32toh(tmp);
  665. memcpy(&tmp, ar->data + 4, 4);
  666. if (tmp)
  667. info->get_asic[modular_id][miner_id][asic_id][1] = be32toh(tmp);
  668. for (i = 0; i < AVA9_DEFAULT_PLL_CNT; i++) {
  669. memcpy(&tmp, ar->data + 8 + i * 2, 2);
  670. info->get_asic[modular_id][miner_id][asic_id][2 + i] = be16toh(tmp);
  671. }
  672. }
  673. break;
  674. case AVA9_P_STATUS_ASIC_PLL:
  675. {
  676. int miner_id;
  677. int asic_id;
  678. uint16_t freq;
  679. if (!info->asic_count[modular_id])
  680. break;
  681. miner_id = ar->idx / info->asic_count[modular_id];
  682. asic_id = ar->idx % info->asic_count[modular_id];
  683. applog(LOG_DEBUG, "%s-%d-%d: AVA9_P_STATUS_ASIC %d-%d",
  684. avalon9->drv->name, avalon9->device_id, modular_id,
  685. miner_id, asic_id);
  686. for (i = 0; i < AVA9_DEFAULT_PLL_CNT; i++) {
  687. memcpy(&freq, ar->data + i * 2, 2);
  688. info->get_frequency[modular_id][miner_id][asic_id][i] = be16toh(freq);
  689. }
  690. }
  691. break;
  692. case AVA9_P_STATUS_PVT_RO:
  693. applog(LOG_DEBUG, "%s-%d-%d: AVA9_P_STATUS_PVT_RO", avalon9->drv->name, avalon9->device_id, modular_id);
  694. if (ar->data[7]) {
  695. memcpy(&tmp, ar->data, 4);
  696. info->pvt_ro[modular_id][ar->data[4]][ar->data[5]][ar->data[6]] = be32toh(tmp);
  697. }
  698. break;
  699. case AVA9_P_STATUS_FAC:
  700. applog(LOG_DEBUG, "%s-%d-%d: AVA9_P_STATUS_FAC", avalon9->drv->name, avalon9->device_id, modular_id);
  701. memcpy(&info->factory_info[modular_id][0], ar->data, info->miner_count[modular_id]);
  702. break;
  703. case AVA9_P_STATUS_OC:
  704. applog(LOG_DEBUG, "%s-%d-%d: AVA9_P_STATUS_OC", avalon9->drv->name, avalon9->device_id, modular_id);
  705. info->overclocking_info[0] = ar->data[0];
  706. break;
  707. case AVA9_P_STATUS_SS_PARA:
  708. applog(LOG_DEBUG, "%s-%d-%d: AVA9_P_STATUS_SS_PARA", avalon9->drv->name, avalon9->device_id, modular_id);
  709. info->ss_para_en[modular_id] = ar->data[0];
  710. if(info->ss_para_en[modular_id])
  711. info->ss_para_target_temp[modular_id] = ar->data[1];
  712. break;
  713. default:
  714. applog(LOG_DEBUG, "%s-%d-%d: Unknown response %x", avalon9->drv->name, avalon9->device_id, modular_id, ar->type);
  715. break;
  716. }
  717. return 0;
  718. }
  719. /*
  720. # IIC packet format: length[1]+transId[1]+sesId[1]+req[1]+data[60]
  721. # length: 4+len(data)
  722. # transId: 0
  723. # sesId: 0
  724. # req: checkout the header file
  725. # data:
  726. # INIT: clock_rate[4] + reserved[4] + payload[52]
  727. # XFER: txSz[1]+rxSz[1]+options[1]+slaveAddr[1] + payload[56]
  728. */
  729. static int avalon9_auc_init_pkg(uint8_t *iic_pkg, struct avalon9_iic_info *iic_info, uint8_t *buf, int wlen, int rlen)
  730. {
  731. memset(iic_pkg, 0, AVA9_AUC_P_SIZE);
  732. switch (iic_info->iic_op) {
  733. case AVA9_IIC_INIT:
  734. iic_pkg[0] = 12; /* 4 bytes IIC header + 4 bytes speed + 4 bytes xfer delay */
  735. iic_pkg[3] = AVA9_IIC_INIT;
  736. iic_pkg[4] = iic_info->iic_param.aucParam[0] & 0xff;
  737. iic_pkg[5] = (iic_info->iic_param.aucParam[0] >> 8) & 0xff;
  738. iic_pkg[6] = (iic_info->iic_param.aucParam[0] >> 16) & 0xff;
  739. iic_pkg[7] = iic_info->iic_param.aucParam[0] >> 24;
  740. iic_pkg[8] = iic_info->iic_param.aucParam[1] & 0xff;
  741. iic_pkg[9] = (iic_info->iic_param.aucParam[1] >> 8) & 0xff;
  742. iic_pkg[10] = (iic_info->iic_param.aucParam[1] >> 16) & 0xff;
  743. iic_pkg[11] = iic_info->iic_param.aucParam[1] >> 24;
  744. break;
  745. case AVA9_IIC_XFER:
  746. iic_pkg[0] = 8 + wlen;
  747. iic_pkg[3] = AVA9_IIC_XFER;
  748. iic_pkg[4] = wlen;
  749. iic_pkg[5] = rlen;
  750. iic_pkg[7] = iic_info->iic_param.slave_addr;
  751. if (buf && wlen)
  752. memcpy(iic_pkg + 8, buf, wlen);
  753. break;
  754. case AVA9_IIC_RESET:
  755. case AVA9_IIC_DEINIT:
  756. case AVA9_IIC_INFO:
  757. iic_pkg[0] = 4;
  758. iic_pkg[3] = iic_info->iic_op;
  759. break;
  760. default:
  761. break;
  762. }
  763. return 0;
  764. }
  765. static int avalon9_iic_xfer(struct cgpu_info *avalon9, uint8_t slave_addr,
  766. uint8_t *wbuf, int wlen,
  767. uint8_t *rbuf, int rlen)
  768. {
  769. struct avalon9_info *info = avalon9->device_data;
  770. struct i2c_ctx *pctx = NULL;
  771. int err = 1;
  772. bool ret = false;
  773. pctx = info->i2c_slaves[slave_addr];
  774. if (!pctx) {
  775. applog(LOG_ERR, "%s-%d: IIC xfer i2c slaves null!", avalon9->drv->name, avalon9->device_id);
  776. goto out;
  777. }
  778. if (wbuf) {
  779. ret = pctx->write_raw(pctx, wbuf, wlen);
  780. if (!ret) {
  781. applog(LOG_DEBUG, "%s-%d: IIC xfer write raw failed!", avalon9->drv->name, avalon9->device_id);
  782. goto out;
  783. }
  784. }
  785. cgsleep_ms(5);
  786. if (rbuf) {
  787. ret = pctx->read_raw(pctx, rbuf, rlen);
  788. if (!ret) {
  789. applog(LOG_DEBUG, "%s-%d: IIC xfer read raw failed!", avalon9->drv->name, avalon9->device_id);
  790. hexdump(rbuf, rlen);
  791. goto out;
  792. }
  793. }
  794. return 0;
  795. out:
  796. return err;
  797. }
  798. static int avalon9_auc_xfer(struct cgpu_info *avalon9,
  799. uint8_t *wbuf, int wlen, int *write,
  800. uint8_t *rbuf, int rlen, int *read)
  801. {
  802. int err = -1;
  803. if (unlikely(avalon9->usbinfo.nodev))
  804. goto out;
  805. usb_buffer_clear(avalon9);
  806. err = usb_write(avalon9, (char *)wbuf, wlen, write, C_AVA9_WRITE);
  807. if (err || *write != wlen) {
  808. applog(LOG_DEBUG, "%s-%d: AUC xfer %d, w(%d-%d)!", avalon9->drv->name, avalon9->device_id, err, wlen, *write);
  809. usb_nodev(avalon9);
  810. goto out;
  811. }
  812. cgsleep_ms(opt_avalon9_aucxdelay / 4800 + 1);
  813. rlen += 4; /* Add 4 bytes IIC header */
  814. err = usb_read(avalon9, (char *)rbuf, rlen, read, C_AVA9_READ);
  815. if (err || *read != rlen || *read != rbuf[0]) {
  816. applog(LOG_DEBUG, "%s-%d: AUC xfer %d, r(%d-%d-%d)!", avalon9->drv->name, avalon9->device_id, err, rlen - 4, *read, rbuf[0]);
  817. hexdump(rbuf, rlen);
  818. return -1;
  819. }
  820. *read = rbuf[0] - 4; /* Remove 4 bytes IIC header */
  821. out:
  822. return err;
  823. }
  824. static int avalon9_auc_init(struct cgpu_info *avalon9, char *ver)
  825. {
  826. struct avalon9_iic_info iic_info;
  827. int err, wlen, rlen;
  828. uint8_t wbuf[AVA9_AUC_P_SIZE];
  829. uint8_t rbuf[AVA9_AUC_P_SIZE];
  830. if (unlikely(avalon9->usbinfo.nodev))
  831. return 1;
  832. /* Try to clean the AUC buffer */
  833. usb_buffer_clear(avalon9);
  834. err = usb_read(avalon9, (char *)rbuf, AVA9_AUC_P_SIZE, &rlen, C_AVA9_READ);
  835. applog(LOG_DEBUG, "%s-%d: AUC usb_read %d, %d!", avalon9->drv->name, avalon9->device_id, err, rlen);
  836. hexdump(rbuf, AVA9_AUC_P_SIZE);
  837. /* Reset */
  838. iic_info.iic_op = AVA9_IIC_RESET;
  839. rlen = 0;
  840. avalon9_auc_init_pkg(wbuf, &iic_info, NULL, 0, rlen);
  841. memset(rbuf, 0, AVA9_AUC_P_SIZE);
  842. err = avalon9_auc_xfer(avalon9, wbuf, AVA9_AUC_P_SIZE, &wlen, rbuf, rlen, &rlen);
  843. if (err) {
  844. applog(LOG_ERR, "%s-%d: Failed to reset Avalon USB2IIC Converter", avalon9->drv->name, avalon9->device_id);
  845. return 1;
  846. }
  847. /* Deinit */
  848. iic_info.iic_op = AVA9_IIC_DEINIT;
  849. rlen = 0;
  850. avalon9_auc_init_pkg(wbuf, &iic_info, NULL, 0, rlen);
  851. memset(rbuf, 0, AVA9_AUC_P_SIZE);
  852. err = avalon9_auc_xfer(avalon9, wbuf, AVA9_AUC_P_SIZE, &wlen, rbuf, rlen, &rlen);
  853. if (err) {
  854. applog(LOG_ERR, "%s-%d: Failed to deinit Avalon USB2IIC Converter", avalon9->drv->name, avalon9->device_id);
  855. return 1;
  856. }
  857. /* Init */
  858. iic_info.iic_op = AVA9_IIC_INIT;
  859. iic_info.iic_param.aucParam[0] = opt_avalon9_aucspeed;
  860. iic_info.iic_param.aucParam[1] = opt_avalon9_aucxdelay;
  861. rlen = AVA9_AUC_VER_LEN;
  862. avalon9_auc_init_pkg(wbuf, &iic_info, NULL, 0, rlen);
  863. memset(rbuf, 0, AVA9_AUC_P_SIZE);
  864. err = avalon9_auc_xfer(avalon9, wbuf, AVA9_AUC_P_SIZE, &wlen, rbuf, rlen, &rlen);
  865. if (err) {
  866. applog(LOG_ERR, "%s-%d: Failed to init Avalon USB2IIC Converter", avalon9->drv->name, avalon9->device_id);
  867. return 1;
  868. }
  869. hexdump(rbuf, AVA9_AUC_P_SIZE);
  870. memcpy(ver, rbuf + 4, AVA9_AUC_VER_LEN);
  871. ver[AVA9_AUC_VER_LEN] = '\0';
  872. applog(LOG_DEBUG, "%s-%d: USB2IIC Converter version: %s!", avalon9->drv->name, avalon9->device_id, ver);
  873. return 0;
  874. }
  875. static int avalon9_auc_getinfo(struct cgpu_info *avalon9)
  876. {
  877. struct avalon9_iic_info iic_info;
  878. int err, wlen, rlen;
  879. uint8_t wbuf[AVA9_AUC_P_SIZE];
  880. uint8_t rbuf[AVA9_AUC_P_SIZE];
  881. uint8_t *pdata = rbuf + 4;
  882. uint16_t adc_val;
  883. struct avalon9_info *info = avalon9->device_data;
  884. iic_info.iic_op = AVA9_IIC_INFO;
  885. /*
  886. * Device info: (9 bytes)
  887. * tempadc(2), reqRdIndex, reqWrIndex,
  888. * respRdIndex, respWrIndex, tx_flags, state
  889. */
  890. rlen = 7;
  891. avalon9_auc_init_pkg(wbuf, &iic_info, NULL, 0, rlen);
  892. memset(rbuf, 0, AVA9_AUC_P_SIZE);
  893. err = avalon9_auc_xfer(avalon9, wbuf, AVA9_AUC_P_SIZE, &wlen, rbuf, rlen, &rlen);
  894. if (err) {
  895. applog(LOG_ERR, "%s-%d: AUC Failed to get info ", avalon9->drv->name, avalon9->device_id);
  896. return 1;
  897. }
  898. applog(LOG_DEBUG, "%s-%d: AUC tempADC(%03d), reqcnt(%d), respcnt(%d), txflag(%d), state(%d)",
  899. avalon9->drv->name, avalon9->device_id,
  900. pdata[1] << 8 | pdata[0],
  901. pdata[2],
  902. pdata[3],
  903. pdata[5] << 8 | pdata[4],
  904. pdata[6]);
  905. adc_val = pdata[1] << 8 | pdata[0];
  906. info->auc_sensor = 3.3 * adc_val * 10000 / 1023;
  907. return 0;
  908. }
  909. static int avalon9_iic_xfer_pkg(struct cgpu_info *avalon9, uint8_t slave_addr,
  910. const struct avalon9_pkg *pkg, struct avalon9_ret *ret)
  911. {
  912. struct avalon9_iic_info iic_info;
  913. int err, wcnt, rcnt, rlen = 0;
  914. uint8_t wbuf[AVA9_AUC_P_SIZE];
  915. uint8_t rbuf[AVA9_AUC_P_SIZE];
  916. struct avalon9_info *info = avalon9->device_data;
  917. if (ret)
  918. rlen = AVA9_READ_SIZE;
  919. if (info->connecter == AVA9_CONNECTER_AUC) {
  920. if (unlikely(avalon9->usbinfo.nodev))
  921. return AVA9_SEND_ERROR;
  922. iic_info.iic_op = AVA9_IIC_XFER;
  923. iic_info.iic_param.slave_addr = slave_addr;
  924. avalon9_auc_init_pkg(wbuf, &iic_info, (uint8_t *)pkg, AVA9_WRITE_SIZE, rlen);
  925. err = avalon9_auc_xfer(avalon9, wbuf, wbuf[0], &wcnt, rbuf, rlen, &rcnt);
  926. if ((pkg->type != AVA9_P_DETECT) && err == -7 && !rcnt && rlen) {
  927. avalon9_auc_init_pkg(wbuf, &iic_info, NULL, 0, rlen);
  928. err = avalon9_auc_xfer(avalon9, wbuf, wbuf[0], &wcnt, rbuf, rlen, &rcnt);
  929. applog(LOG_DEBUG, "%s-%d-%d: AUC read again!(type:0x%x, err:%d)", avalon9->drv->name, avalon9->device_id, slave_addr, pkg->type, err);
  930. }
  931. if (err || rcnt != rlen) {
  932. if (info->xfer_err_cnt++ == 100) {
  933. applog(LOG_DEBUG, "%s-%d-%d: AUC xfer_err_cnt reach err = %d, rcnt = %d, rlen = %d",
  934. avalon9->drv->name, avalon9->device_id, slave_addr,
  935. err, rcnt, rlen);
  936. cgsleep_ms(5 * 1000); /* Wait MM reset */
  937. if (avalon9_auc_init(avalon9, info->auc_version)) {
  938. applog(LOG_WARNING, "%s-%d: Failed to re-init auc, unplugging for new hotplug",
  939. avalon9->drv->name, avalon9->device_id);
  940. usb_nodev(avalon9);
  941. }
  942. }
  943. return AVA9_SEND_ERROR;
  944. }
  945. if (ret)
  946. memcpy((char *)ret, rbuf + 4, AVA9_READ_SIZE);
  947. info->xfer_err_cnt = 0;
  948. }
  949. if (info->connecter == AVA9_CONNECTER_IIC) {
  950. err = avalon9_iic_xfer(avalon9, slave_addr, (uint8_t *)pkg, AVA9_WRITE_SIZE, (uint8_t *)ret, AVA9_READ_SIZE);
  951. if ((pkg->type != AVA9_P_DETECT) && err) {
  952. err = avalon9_iic_xfer(avalon9, slave_addr, (uint8_t *)pkg, AVA9_WRITE_SIZE, (uint8_t *)ret, AVA9_READ_SIZE);
  953. applog(LOG_DEBUG, "%s-%d-%d: IIC read again!(type:0x%x, err:%d)", avalon9->drv->name, avalon9->device_id, slave_addr, pkg->type, err);
  954. }
  955. if (err) {
  956. /* FIXME: Don't care broadcast message with no reply, or it will block other thread when called by avalon9_send_bc_pkgs */
  957. if ((pkg->type != AVA9_P_DETECT) && (slave_addr == AVA9_MODULE_BROADCAST))
  958. return AVA9_SEND_OK;
  959. if (info->xfer_err_cnt++ == 100) {
  960. info->xfer_err_cnt = 0;
  961. applog(LOG_DEBUG, "%s-%d-%d: IIC xfer_err_cnt reach err = %d, rcnt = %d, rlen = %d",
  962. avalon9->drv->name, avalon9->device_id, slave_addr,
  963. err, rcnt, rlen);
  964. cgsleep_ms(5 * 1000); /* Wait MM reset */
  965. }
  966. return AVA9_SEND_ERROR;
  967. }
  968. info->xfer_err_cnt = 0;
  969. }
  970. return AVA9_SEND_OK;
  971. }
  972. static int avalon9_send_bc_pkgs(struct cgpu_info *avalon9, const struct avalon9_pkg *pkg)
  973. {
  974. int ret;
  975. do {
  976. ret = avalon9_iic_xfer_pkg(avalon9, AVA9_MODULE_BROADCAST, pkg, NULL);
  977. } while (ret != AVA9_SEND_OK);
  978. return 0;
  979. }
  980. static void avalon9_stratum_pkgs(struct cgpu_info *avalon9, struct pool *pool)
  981. {
  982. struct avalon9_info *info = avalon9->device_data;
  983. const int merkle_offset = 36;
  984. struct avalon9_pkg pkg;
  985. int i, a, b;
  986. uint32_t tmp;
  987. unsigned char target[32];
  988. int job_id_len, n2size;
  989. unsigned short crc;
  990. int coinbase_len_posthash, coinbase_len_prehash;
  991. uint8_t coinbase_prehash[32];
  992. uint32_t range, start;
  993. /* Send out the first stratum message STATIC */
  994. applog(LOG_DEBUG, "%s-%d: Pool stratum message STATIC: %d, %d, %d, %d, %d",
  995. avalon9->drv->name, avalon9->device_id,
  996. pool->coinbase_len,
  997. pool->nonce2_offset,
  998. pool->n2size,
  999. merkle_offset,
  1000. pool->merkles);
  1001. memset(pkg.data, 0, AVA9_P_DATA_LEN);
  1002. tmp = be32toh(pool->coinbase_len);
  1003. memcpy(pkg.data, &tmp, 4);
  1004. tmp = be32toh(pool->nonce2_offset);
  1005. memcpy(pkg.data + 4, &tmp, 4);
  1006. n2size = pool->n2size >= 4 ? 4 : pool->n2size;
  1007. tmp = be32toh(n2size);
  1008. memcpy(pkg.data + 8, &tmp, 4);
  1009. tmp = be32toh(merkle_offset);
  1010. memcpy(pkg.data + 12, &tmp, 4);
  1011. tmp = be32toh(pool->merkles);
  1012. memcpy(pkg.data + 16, &tmp, 4);
  1013. if (pool->n2size == 3)
  1014. range = 0xffffff / (total_devices ? total_devices : 1);
  1015. else
  1016. range = 0xffffffff / (total_devices ? total_devices : 1);
  1017. start = range * avalon9->device_id;
  1018. tmp = be32toh(start);
  1019. memcpy(pkg.data + 20, &tmp, 4);
  1020. tmp = be32toh(range);
  1021. memcpy(pkg.data + 24, &tmp, 4);
  1022. if (info->work_restart) {
  1023. info->work_restart = false;
  1024. tmp = be32toh(0x1);
  1025. memcpy(pkg.data + 28, &tmp, 4);
  1026. }
  1027. avalon9_init_pkg(&pkg, AVA9_P_STATIC, 1, 1);
  1028. if (avalon9_send_bc_pkgs(avalon9, &pkg))
  1029. return;
  1030. if (pool->sdiff <= AVA9_DRV_DIFFMAX)
  1031. set_target(target, pool->sdiff);
  1032. else
  1033. set_target(target, AVA9_DRV_DIFFMAX);
  1034. memcpy(pkg.data, target, 32);
  1035. if (opt_debug) {
  1036. char *target_str;
  1037. target_str = bin2hex(target, 32);
  1038. applog(LOG_DEBUG, "%s-%d: Pool stratum target: %s", avalon9->drv->name, avalon9->device_id, target_str);
  1039. free(target_str);
  1040. }
  1041. avalon9_init_pkg(&pkg, AVA9_P_TARGET, 1, 1);
  1042. if (avalon9_send_bc_pkgs(avalon9, &pkg))
  1043. return;
  1044. memset(pkg.data, 0, AVA9_P_DATA_LEN);
  1045. job_id_len = strlen(pool->swork.job_id);
  1046. crc = crc16((unsigned char *)pool->swork.job_id, job_id_len);
  1047. applog(LOG_DEBUG, "%s-%d: Pool stratum message JOBS_ID[%04x]: %s",
  1048. avalon9->drv->name, avalon9->device_id,
  1049. crc, pool->swork.job_id);
  1050. tmp = ((crc << 16) | pool->pool_no);
  1051. if (info->last_jobid != tmp) {
  1052. info->last_jobid = tmp;
  1053. pkg.data[0] = (crc & 0xff00) >> 8;
  1054. pkg.data[1] = crc & 0xff;
  1055. pkg.data[2] = pool->pool_no & 0xff;
  1056. pkg.data[3] = (pool->pool_no & 0xff00) >> 8;
  1057. avalon9_init_pkg(&pkg, AVA9_P_JOB_ID, 1, 1);
  1058. if (avalon9_send_bc_pkgs(avalon9, &pkg))
  1059. return;
  1060. }
  1061. coinbase_len_prehash = pool->nonce2_offset - (pool->nonce2_offset % SHA256_BLOCK_SIZE);
  1062. coinbase_len_posthash = pool->coinbase_len - coinbase_len_prehash;
  1063. sha256_prehash(pool->coinbase, coinbase_len_prehash, coinbase_prehash);
  1064. a = (coinbase_len_posthash / AVA9_P_DATA_LEN) + 1;
  1065. b = coinbase_len_posthash % AVA9_P_DATA_LEN;
  1066. memcpy(pkg.data, coinbase_prehash, 32);
  1067. avalon9_init_pkg(&pkg, AVA9_P_COINBASE, 1, a + (b ? 1 : 0));
  1068. if (avalon9_send_bc_pkgs(avalon9, &pkg))
  1069. return;
  1070. applog(LOG_DEBUG, "%s-%d: Pool stratum message modified COINBASE: %d %d",
  1071. avalon9->drv->name, avalon9->device_id,
  1072. a, b);
  1073. for (i = 1; i < a; i++) {
  1074. memcpy(pkg.data, pool->coinbase + coinbase_len_prehash + i * 32 - 32, 32);
  1075. avalon9_init_pkg(&pkg, AVA9_P_COINBASE, i + 1, a + (b ? 1 : 0));
  1076. if (avalon9_send_bc_pkgs(avalon9, &pkg))
  1077. return;
  1078. }
  1079. if (b) {
  1080. memset(pkg.data, 0, AVA9_P_DATA_LEN);
  1081. memcpy(pkg.data, pool->coinbase + coinbase_len_prehash + i * 32 - 32, b);
  1082. avalon9_init_pkg(&pkg, AVA9_P_COINBASE, i + 1, i + 1);
  1083. if (avalon9_send_bc_pkgs(avalon9, &pkg))
  1084. return;
  1085. }
  1086. b = pool->merkles;
  1087. applog(LOG_DEBUG, "%s-%d: Pool stratum message MERKLES: %d", avalon9->drv->name, avalon9->device_id, b);
  1088. for (i = 0; i < b; i++) {
  1089. memset(pkg.data, 0, AVA9_P_DATA_LEN);
  1090. memcpy(pkg.data, pool->swork.merkle_bin[i], 32);
  1091. avalon9_init_pkg(&pkg, AVA9_P_MERKLES, i + 1, b);
  1092. if (avalon9_send_bc_pkgs(avalon9, &pkg))
  1093. return;
  1094. }
  1095. applog(LOG_DEBUG, "%s-%d: Pool stratum message HEADER: 4", avalon9->drv->name, avalon9->device_id);
  1096. for (i = 0; i < 4; i++) {
  1097. memset(pkg.data, 0, AVA9_P_DATA_LEN);
  1098. memcpy(pkg.data, pool->header_bin + i * 32, 32);
  1099. avalon9_init_pkg(&pkg, AVA9_P_HEADER, i + 1, 4);
  1100. if (avalon9_send_bc_pkgs(avalon9, &pkg))
  1101. return;
  1102. }
  1103. if (info->connecter == AVA9_CONNECTER_AUC)
  1104. avalon9_auc_getinfo(avalon9);
  1105. }
  1106. static struct cgpu_info *avalon9_iic_detect(void)
  1107. {
  1108. int i;
  1109. struct avalon9_info *info;
  1110. struct cgpu_info *avalon9 = NULL;
  1111. struct i2c_ctx *i2c_slave = NULL;
  1112. i2c_slave = i2c_slave_open(I2C_BUS, 0);
  1113. if (!i2c_slave) {
  1114. applog(LOG_ERR, "avalon9 init iic failed\n");
  1115. return NULL;
  1116. }
  1117. i2c_slave->exit(i2c_slave);
  1118. i2c_slave = NULL;
  1119. avalon9 = cgcalloc(1, sizeof(*avalon9));
  1120. avalon9->drv = &avalon9_drv;
  1121. avalon9->deven = DEV_ENABLED;
  1122. avalon9->threads = 1;
  1123. add_cgpu(avalon9);
  1124. applog(LOG_INFO, "%s-%d: Found at %s", avalon9->drv->name, avalon9->device_id,
  1125. I2C_BUS);
  1126. avalon9->device_data = cgcalloc(sizeof(struct avalon9_info), 1);
  1127. memset(avalon9->device_data, 0, sizeof(struct avalon9_info));
  1128. info = avalon9->device_data;
  1129. for (i = 0; i < AVA9_DEFAULT_MODULARS; i++) {
  1130. info->enable[i] = false;
  1131. info->reboot[i] = false;
  1132. info->i2c_slaves[i] = i2c_slave_open(I2C_BUS, i);
  1133. if (!info->i2c_slaves[i]) {
  1134. applog(LOG_ERR, "avalon9 init i2c slaves failed\n");
  1135. free(avalon9->device_data);
  1136. avalon9->device_data = NULL;
  1137. free(avalon9);
  1138. avalon9 = NULL;
  1139. return NULL;
  1140. }
  1141. }
  1142. info->connecter = AVA9_CONNECTER_IIC;
  1143. return avalon9;
  1144. }
  1145. static void detect_modules(struct cgpu_info *avalon9);
  1146. static struct cgpu_info *avalon9_auc_detect(struct libusb_device *dev, struct usb_find_devices *found)
  1147. {
  1148. int i, modules = 0;
  1149. struct avalon9_info *info;
  1150. struct cgpu_info *avalon9 = usb_alloc_cgpu(&avalon9_drv, 1);
  1151. char auc_ver[AVA9_AUC_VER_LEN];
  1152. if (!usb_init(avalon9, dev, found)) {
  1153. applog(LOG_ERR, "avalon9 failed usb_init");
  1154. avalon9 = usb_free_cgpu(avalon9);
  1155. return NULL;
  1156. }
  1157. /* avalon9 prefers not to use zero length packets */
  1158. avalon9->nozlp = true;
  1159. /* We try twice on AUC init */
  1160. if (avalon9_auc_init(avalon9, auc_ver) && avalon9_auc_init(avalon9, auc_ver))
  1161. return NULL;
  1162. applog(LOG_INFO, "%s-%d: Found at %s", avalon9->drv->name, avalon9->device_id,
  1163. avalon9->device_path);
  1164. avalon9->device_data = cgcalloc(sizeof(struct avalon9_info), 1);
  1165. memset(avalon9->device_data, 0, sizeof(struct avalon9_info));
  1166. info = avalon9->device_data;
  1167. memcpy(info->auc_version, auc_ver, AVA9_AUC_VER_LEN);
  1168. info->auc_version[AVA9_AUC_VER_LEN] = '\0';
  1169. info->auc_speed = opt_avalon9_aucspeed;
  1170. info->auc_xdelay = opt_avalon9_aucxdelay;
  1171. for (i = 0; i < AVA9_DEFAULT_MODULARS; i++)
  1172. info->enable[i] = 0;
  1173. info->connecter = AVA9_CONNECTER_AUC;
  1174. detect_modules(avalon9);
  1175. for (i = 0; i < AVA9_DEFAULT_MODULARS; i++)
  1176. modules += info->enable[i];
  1177. if (!modules) {
  1178. applog(LOG_INFO, "avalon9 found but no modules initialised");
  1179. free(info);
  1180. avalon9 = usb_free_cgpu(avalon9);
  1181. return NULL;
  1182. }
  1183. /* We have an avalon9 AUC connected */
  1184. avalon9->threads = 1;
  1185. add_cgpu(avalon9);
  1186. update_usb_stats(avalon9);
  1187. return avalon9;
  1188. }
  1189. static inline void avalon9_detect(bool __maybe_unused hotplug)
  1190. {
  1191. usb_detect(&avalon9_drv, avalon9_auc_detect);
  1192. if (!hotplug && opt_avalon9_iic_detect)
  1193. avalon9_iic_detect();
  1194. }
  1195. static bool avalon9_prepare(struct thr_info *thr)
  1196. {
  1197. struct cgpu_info *avalon9 = thr->cgpu;
  1198. struct avalon9_info *info = avalon9->device_data;
  1199. info->last_diff1 = 0;
  1200. info->pending_diff1 = 0;
  1201. info->last_rej = 0;
  1202. info->mm_count = 0;
  1203. info->xfer_err_cnt = 0;
  1204. info->pool_no = 0;
  1205. memset(&(info->firsthash), 0, sizeof(info->firsthash));
  1206. cgtime(&(info->last_fan_adj));
  1207. cgtime(&info->last_stratum);
  1208. cgtime(&info->last_detect);
  1209. cglock_init(&info->update_lock);
  1210. cglock_init(&info->pool0.data_lock);
  1211. cglock_init(&info->pool1.data_lock);
  1212. cglock_init(&info->pool2.data_lock);
  1213. return true;
  1214. }
  1215. static int check_module_exist(struct cgpu_info *avalon9, uint8_t mm_dna[AVA9_MM_DNA_LEN])
  1216. {
  1217. struct avalon9_info *info = avalon9->device_data;
  1218. int i;
  1219. for (i = 0; i < AVA9_DEFAULT_MODULARS; i++) {
  1220. /* last byte is \0 */
  1221. if (info->enable[i] && !memcmp(info->mm_dna[i], mm_dna, AVA9_MM_DNA_LEN))
  1222. return 1;
  1223. }
  1224. return 0;
  1225. }
  1226. static void detect_modules(struct cgpu_info *avalon9)
  1227. {
  1228. struct avalon9_info *info = avalon9->device_data;
  1229. struct avalon9_pkg send_pkg;
  1230. struct avalon9_ret ret_pkg;
  1231. uint32_t tmp;
  1232. int i, j, k, err, rlen;
  1233. uint8_t dev_index;
  1234. uint8_t rbuf[AVA9_AUC_P_SIZE];
  1235. /* Detect new modules here */
  1236. for (i = 1; i < AVA9_DEFAULT_MODULARS + 1; i++) {
  1237. if (info->enable[i])
  1238. continue;
  1239. /* Send out detect pkg */
  1240. applog(LOG_DEBUG, "%s-%d: AVA9_P_DETECT ID[%d]",
  1241. avalon9->drv->name, avalon9->device_id, i);
  1242. memset(send_pkg.data, 0, AVA9_P_DATA_LEN);
  1243. tmp = be32toh(i); /* ID */
  1244. memcpy(send_pkg.data + 28, &tmp, 4);
  1245. avalon9_init_pkg(&send_pkg, AVA9_P_DETECT, 1, 1);
  1246. err = avalon9_iic_xfer_pkg(avalon9, AVA9_MODULE_BROADCAST, &send_pkg, &ret_pkg);
  1247. if (err == AVA9_SEND_OK) {
  1248. if (decode_pkg(avalon9, &ret_pkg, AVA9_MODULE_BROADCAST)) {
  1249. applog(LOG_DEBUG, "%s-%d: Should be AVA9_P_ACKDETECT(%d), but %d",
  1250. avalon9->drv->name, avalon9->device_id, AVA9_P_ACKDETECT, ret_pkg.type);
  1251. continue;
  1252. }
  1253. }
  1254. if (err != AVA9_SEND_OK) {
  1255. applog(LOG_DEBUG, "%s-%d: AVA9_P_DETECT: Failed AUC xfer data with err %d",
  1256. avalon9->drv->name, avalon9->device_id, err);
  1257. break;
  1258. }
  1259. applog(LOG_DEBUG, "%s-%d: Module detect ID[%d]: %d",
  1260. avalon9->drv->name, avalon9->device_id, i, ret_pkg.type);
  1261. if (ret_pkg.type != AVA9_P_ACKDETECT)
  1262. break;
  1263. if (check_module_exist(avalon9, ret_pkg.data))
  1264. continue;
  1265. /* Check count of modulars */
  1266. if (i == AVA9_DEFAULT_MODULARS) {
  1267. applog(LOG_NOTICE, "You have connected more than %d machines. This is discouraged.", (AVA9_DEFAULT_MODULARS - 1));
  1268. info->conn_overloaded = true;
  1269. break;
  1270. } else
  1271. info->conn_overloaded = false;
  1272. memcpy(info->mm_version[i], ret_pkg.data + AVA9_MM_DNA_LEN, AVA9_MM_VER_LEN);
  1273. info->mm_version[i][AVA9_MM_VER_LEN] = '\0';
  1274. for (dev_index = 0; dev_index < (sizeof(avalon9_dev_table) / sizeof(avalon9_dev_table[0])); dev_index++) {
  1275. if (!strncmp((char *)&(info->mm_version[i]), (char *)(avalon9_dev_table[dev_index].dev_id_str), 3) ||
  1276. !strncmp((char *)&(info->mm_version[i]), (char *)(avalon9_dev_table[dev_index].dev_id_str), 4)) {
  1277. info->mod_type[i] = avalon9_dev_table[dev_index].mod_type;
  1278. info->miner_count[i] = avalon9_dev_table[dev_index].miner_count;
  1279. info->asic_count[i] = avalon9_dev_table[dev_index].asic_count;
  1280. info->vin_adc_ratio[i] = avalon9_dev_table[dev_index].vin_adc_ratio;
  1281. info->vout_adc_ratio[i] = avalon9_dev_table[dev_index].vout_adc_ratio;
  1282. break;
  1283. }
  1284. }
  1285. if (dev_index == (sizeof(avalon9_dev_table) / sizeof(avalon9_dev_table[0]))) {
  1286. applog(LOG_NOTICE, "%s-%d: The modular version %s cann't be support",
  1287. avalon9->drv->name, avalon9->device_id, info->mm_version[i]);
  1288. break;
  1289. }
  1290. info->enable[i] = 1;
  1291. cgtime(&info->elapsed[i]);
  1292. memcpy(info->mm_dna[i], ret_pkg.data, AVA9_MM_DNA_LEN);
  1293. memcpy(&tmp, ret_pkg.data + AVA9_MM_DNA_LEN + AVA9_MM_VER_LEN, 4);
  1294. tmp = be32toh(tmp);
  1295. info->total_asics[i] = tmp;
  1296. info->temp_overheat[i] = AVA9_DEFAULT_TEMP_OVERHEAT;
  1297. info->temp_target[i] = opt_avalon9_temp_target;
  1298. info->fan_pct[i] = opt_avalon9_fan_min;
  1299. for (j = 0; j < info->miner_count[i]; j++) {
  1300. if (opt_avalon9_voltage_level == AVA9_INVALID_VOLTAGE_LEVEL)
  1301. info->set_voltage_level[i][j] = avalon9_dev_table[dev_index].set_voltage_level;
  1302. else
  1303. info->set_voltage_level[i][j] = opt_avalon9_voltage_level;
  1304. info->get_voltage[i][j] = 0;
  1305. info->get_vin[i][j] = 0;
  1306. for (k = 0; k < info->asic_count[i]; k++)
  1307. info->temp[i][j][k] = -273;
  1308. for (k = 0; k < AVA9_DEFAULT_PLL_CNT; k++)
  1309. info->set_frequency[i][j][k] = avalon9_dev_table[dev_index].set_freq[k];
  1310. }
  1311. info->freq_mode[i] = AVA9_FREQ_INIT_MODE;
  1312. memset(info->get_pll[i], 0, sizeof(uint32_t) * info->miner_count[i] * AVA9_DEFAULT_PLL_CNT);
  1313. info->led_indicator[i] = 0;
  1314. info->cutoff[i] = 0;
  1315. info->fan_cpm[i] = 0;
  1316. info->temp_mm[i] = -273;
  1317. info->local_works[i] = 0;
  1318. info->hw_works[i] = 0;
  1319. /*PID controller*/
  1320. info->pid_u[i] = opt_avalon9_fan_min;
  1321. info->pid_p[i] = opt_avalon9_pid_p;
  1322. info->pid_i[i] = opt_avalon9_pid_i;
  1323. info->pid_d[i] = opt_avalon9_pid_d;
  1324. info->pid_e[i][0] = 0;
  1325. info->pid_e[i][1] = 0;
  1326. info->pid_e[i][2] = 0;
  1327. info->pid_0[i] = 0;
  1328. for (j = 0; j < info->miner_count[i]; j++) {
  1329. memset(info->chip_matching_work[i][j], 0, sizeof(uint64_t) * info->asic_count[i]);
  1330. info->local_works_i[i][j] = 0;
  1331. info->hw_works_i[i][j] = 0;
  1332. info->error_code[i][j] = 0;
  1333. info->error_crc[i][j] = 0;
  1334. }
  1335. info->error_code[i][j] = 0;
  1336. info->error_polling_cnt[i] = 0;
  1337. info->power_good[i] = 0;
  1338. memset(info->pmu_version[i], 0, sizeof(char) * 5 * AVA9_DEFAULT_PMU_CNT);
  1339. info->diff1[i] = 0;
  1340. applog(LOG_NOTICE, "%s-%d: New module detected! ID[%d-%x]",
  1341. avalon9->drv->name, avalon9->device_id, i, info->mm_dna[i][AVA9_MM_DNA_LEN - 1]);
  1342. /* Tell MM, it has been detected */
  1343. memset(send_pkg.data, 0, AVA9_P_DATA_LEN);
  1344. memcpy(send_pkg.data, info->mm_dna[i], AVA9_MM_DNA_LEN);
  1345. avalon9_init_pkg(&send_pkg, AVA9_P_SYNC, 1, 1);
  1346. avalon9_iic_xfer_pkg(avalon9, i, &send_pkg, &ret_pkg);
  1347. /* Keep the usb buffer is empty */
  1348. usb_buffer_clear(avalon9);
  1349. usb_read(avalon9, (char *)rbuf, AVA9_AUC_P_SIZE, &rlen, C_AVA9_READ);
  1350. }
  1351. }
  1352. static void detach_module(struct cgpu_info *avalon9, int addr)
  1353. {
  1354. struct avalon9_info *info = avalon9->device_data;
  1355. info->enable[addr] = 0;
  1356. applog(LOG_NOTICE, "%s-%d: Module detached! ID[%d]",
  1357. avalon9->drv->name, avalon9->device_id, addr);
  1358. }
  1359. static int polling(struct cgpu_info *avalon9)
  1360. {
  1361. struct avalon9_info *info = avalon9->device_data;
  1362. struct avalon9_pkg send_pkg;
  1363. struct avalon9_ret ar;
  1364. int i, tmp, ret, decode_err = 0;
  1365. struct timeval current_fan;
  1366. int do_adjust_fan = 0;
  1367. uint32_t fan_pwm;
  1368. double device_tdiff;
  1369. cgtime(&current_fan);
  1370. device_tdiff = tdiff(&current_fan, &(info->last_fan_adj));
  1371. if (device_tdiff > 2.0 || device_tdiff < 0) {
  1372. cgtime(&info->last_fan_adj);
  1373. do_adjust_fan = 1;
  1374. }
  1375. for (i = 1; i < AVA9_DEFAULT_MODULARS; i++) {
  1376. if (!info->enable[i])
  1377. continue;
  1378. cgsleep_ms(opt_avalon9_polling_delay);
  1379. memset(send_pkg.data, 0, AVA9_P_DATA_LEN);
  1380. /* Red LED */
  1381. tmp = be32toh(info->led_indicator[i]);
  1382. memcpy(send_pkg.data, &tmp, 4);
  1383. /* Adjust fan every 2 seconds*/
  1384. if (do_adjust_fan) {
  1385. fan_pwm = adjust_fan(info, i);
  1386. fan_pwm |= 0x80000000;
  1387. tmp = be32toh(fan_pwm);
  1388. memcpy(send_pkg.data + 4, &tmp, 4);
  1389. }
  1390. if (info->reboot[i]) {
  1391. info->reboot[i] = false;
  1392. send_pkg.data[8] = 0x1;
  1393. }
  1394. avalon9_init_pkg(&send_pkg, AVA9_P_POLLING, 1, 1);
  1395. ret = avalon9_iic_xfer_pkg(avalon9, i, &send_pkg, &ar);
  1396. if (ret == AVA9_SEND_OK)
  1397. decode_err = decode_pkg(avalon9, &ar, i);
  1398. if (ret != AVA9_SEND_OK || decode_err) {
  1399. info->error_polling_cnt[i]++;
  1400. memset(send_pkg.data, 0, AVA9_P_DATA_LEN);
  1401. /* NOTE: fix duplicate iic address */
  1402. memcpy(send_pkg.data, info->mm_dna[i], AVA9_MM_DNA_LEN);
  1403. avalon9_init_pkg(&send_pkg, AVA9_P_RSTMMTX, 1, 1);
  1404. avalon9_iic_xfer_pkg(avalon9, i, &send_pkg, NULL);
  1405. if (info->error_polling_cnt[i] >= 10)
  1406. detach_module(avalon9, i);
  1407. }
  1408. if (ret == AVA9_SEND_OK && !decode_err)
  1409. info->error_polling_cnt[i] = 0;
  1410. }
  1411. return 0;
  1412. }
  1413. static int copy_pool_stratum(struct pool *pool_stratum, struct pool *pool)
  1414. {
  1415. int i;
  1416. int merkles = pool->merkles, job_id_len;
  1417. size_t coinbase_len = pool->coinbase_len;
  1418. unsigned short crc;
  1419. if (!pool->swork.job_id)
  1420. return 1;
  1421. if (pool_stratum->swork.job_id) {
  1422. job_id_len = strlen(pool->swork.job_id);
  1423. crc = crc16((unsigned char *)pool->swork.job_id, job_id_len);
  1424. job_id_len = strlen(pool_stratum->swork.job_id);
  1425. if (crc16((unsigned char *)pool_stratum->swork.job_id, job_id_len) == crc)
  1426. return 1;
  1427. }
  1428. cg_wlock(&pool_stratum->data_lock);
  1429. free(pool_stratum->swork.job_id);
  1430. free(pool_stratum->nonce1);
  1431. free(pool_stratum->coinbase);
  1432. pool_stratum->coinbase = cgcalloc(coinbase_len, 1);
  1433. memcpy(pool_stratum->coinbase, pool->coinbase, coinbase_len);
  1434. for (i = 0; i < pool_stratum->merkles; i++)
  1435. free(pool_stratum->swork.merkle_bin[i]);
  1436. if (merkles) {
  1437. pool_stratum->swork.merkle_bin = cgrealloc(pool_stratum->swork.merkle_bin,
  1438. sizeof(char *) * merkles + 1);
  1439. for (i = 0; i < merkles; i++) {
  1440. pool_stratum->swork.merkle_bin[i] = cgmalloc(32);
  1441. memcpy(pool_stratum->swork.merkle_bin[i], pool->swork.merkle_bin[i], 32);
  1442. }
  1443. }
  1444. pool_stratum->sdiff = pool->sdiff;
  1445. pool_stratum->coinbase_len = pool->coinbase_len;
  1446. pool_stratum->nonce2_offset = pool->nonce2_offset;
  1447. pool_stratum->n2size = pool->n2size;
  1448. pool_stratum->merkles = pool->merkles;
  1449. pool_stratum->swork.job_id = strdup(pool->swork.job_id);
  1450. pool_stratum->nonce1 = strdup(pool->nonce1);
  1451. memcpy(pool_stratum->ntime, pool->ntime, sizeof(pool_stratum->ntime));
  1452. memcpy(pool_stratum->header_bin, pool->header_bin, sizeof(pool_stratum->header_bin));
  1453. cg_wunlock(&pool_stratum->data_lock);
  1454. return 0;
  1455. }
  1456. static void avalon9_init_setting(struct cgpu_info *avalon9, int addr)
  1457. {
  1458. struct avalon9_pkg send_pkg;
  1459. uint32_t tmp;
  1460. memset(send_pkg.data, 0, AVA9_P_DATA_LEN);
  1461. tmp = be32toh(opt_avalon9_freq_sel);
  1462. memcpy(send_pkg.data + 4, &tmp, 4);
  1463. /*
  1464. * set flags:
  1465. * 0: ss switch
  1466. * 1: nonce check
  1467. * 2: roll enable
  1468. */
  1469. tmp = 1;
  1470. if (!opt_avalon9_smart_speed)
  1471. tmp = 0;
  1472. tmp |= (opt_avalon9_nonce_check << 1);
  1473. tmp |= (opt_avalon9_roll_enable << 2);
  1474. send_pkg.data[8] = tmp & 0xff;
  1475. send_pkg.data[9] = opt_avalon9_nonce_mask & 0xff;
  1476. tmp = be32toh(opt_avalon9_mux_l2h);
  1477. memcpy(send_pkg.data + 10, &tmp, 4);
  1478. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set mux l2h %u",
  1479. avalon9->drv->name, avalon9->device_id, addr,
  1480. opt_avalon9_mux_l2h);
  1481. tmp = be32toh(opt_avalon9_mux_h2l);
  1482. memcpy(send_pkg.data + 14, &tmp, 4);
  1483. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set mux h2l %u",
  1484. avalon9->drv->name, avalon9->device_id, addr,
  1485. opt_avalon9_mux_h2l);
  1486. tmp = be32toh(opt_avalon9_h2ltime0_spd);
  1487. memcpy(send_pkg.data + 18, &tmp, 4);
  1488. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set h2ltime0 spd %u",
  1489. avalon9->drv->name, avalon9->device_id, addr,
  1490. opt_avalon9_h2ltime0_spd);
  1491. tmp = be32toh(opt_avalon9_spdlow);
  1492. memcpy(send_pkg.data + 22, &tmp, 4);
  1493. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set spdlow %u",
  1494. avalon9->drv->name, avalon9->device_id, addr,
  1495. opt_avalon9_spdlow);
  1496. tmp = be32toh(opt_avalon9_spdhigh);
  1497. memcpy(send_pkg.data + 26, &tmp, 4);
  1498. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set spdhigh %u",
  1499. avalon9->drv->name, avalon9->device_id, addr,
  1500. opt_avalon9_spdhigh);
  1501. send_pkg.data[30] = opt_avalon9_tbase & 0xff;
  1502. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set tbase %u",
  1503. avalon9->drv->name, avalon9->device_id, addr,
  1504. opt_avalon9_tbase);
  1505. /* Package the data */
  1506. avalon9_init_pkg(&send_pkg, AVA9_P_SET, 1, 1);
  1507. if (addr == AVA9_MODULE_BROADCAST)
  1508. avalon9_send_bc_pkgs(avalon9, &send_pkg);
  1509. else
  1510. avalon9_iic_xfer_pkg(avalon9, addr, &send_pkg, NULL);
  1511. }
  1512. static void avalon9_set_adjust_voltage_option(struct cgpu_info *avalon9, int addr,
  1513. int32_t up_init, uint32_t up_factor, uint32_t up_threshold,
  1514. int32_t down_init, uint32_t down_factor, uint32_t down_threshold,
  1515. uint32_t time, uint32_t enable)
  1516. {
  1517. struct avalon9_info *info = avalon9->device_data;
  1518. struct avalon9_pkg send_pkg;
  1519. int32_t tmp;
  1520. memset(send_pkg.data, 0, AVA9_P_DATA_LEN);
  1521. tmp = be32toh(up_init);
  1522. memcpy(send_pkg.data + 0, &tmp, 4);
  1523. applog(LOG_DEBUG, "%s-%d-%d: avalon9 adjust volt set up init %d",
  1524. avalon9->drv->name, avalon9->device_id, addr, up_init);
  1525. tmp = be32toh(up_factor);
  1526. memcpy(send_pkg.data + 4, &tmp, 4);
  1527. applog(LOG_DEBUG, "%s-%d-%d: avalon9 adjust volt set up factor %d",
  1528. avalon9->drv->name, avalon9->device_id, addr, up_factor);
  1529. tmp = be32toh(up_threshold);
  1530. memcpy(send_pkg.data + 8, &tmp, 4);
  1531. applog(LOG_DEBUG, "%s-%d-%d: avalon9 adjust volt set up threshold %d",
  1532. avalon9->drv->name, avalon9->device_id, addr, up_threshold);
  1533. tmp = be32toh(down_init);
  1534. memcpy(send_pkg.data + 12, &tmp, 4);
  1535. applog(LOG_DEBUG, "%s-%d-%d: avalon9 adjust volt set down init %d",
  1536. avalon9->drv->name, avalon9->device_id, addr, down_init);
  1537. tmp = be32toh(down_factor);
  1538. memcpy(send_pkg.data + 16, &tmp, 4);
  1539. applog(LOG_DEBUG, "%s-%d-%d: avalon9 adjust volt set down factor %d",
  1540. avalon9->drv->name, avalon9->device_id, addr, down_factor);
  1541. tmp = be32toh(down_threshold);
  1542. memcpy(send_pkg.data + 20, &tmp, 4);
  1543. applog(LOG_DEBUG, "%s-%d-%d: avalon9 adjust volt set down threshold %d",
  1544. avalon9->drv->name, avalon9->device_id, addr, down_threshold);
  1545. tmp = be32toh(time);
  1546. memcpy(send_pkg.data + 24, &tmp, 4);
  1547. applog(LOG_DEBUG, "%s-%d-%d: avalon9 adjust volt set time %d",
  1548. avalon9->drv->name, avalon9->device_id, addr, time);
  1549. tmp = be32toh(enable);
  1550. memcpy(send_pkg.data + 28, &tmp, 4);
  1551. applog(LOG_DEBUG, "%s-%d-%d: avalon9 adjust volt set enable %d",
  1552. avalon9->drv->name, avalon9->device_id, addr, time);
  1553. /* Package the data */
  1554. avalon9_init_pkg(&send_pkg, AVA9_P_SET_ADJUST_VOLT, 1, 1);
  1555. if (addr == AVA9_MODULE_BROADCAST)
  1556. avalon9_send_bc_pkgs(avalon9, &send_pkg);
  1557. else
  1558. avalon9_iic_xfer_pkg(avalon9, addr, &send_pkg, NULL);
  1559. return;
  1560. }
  1561. static void avalon9_set_voltage_level(struct cgpu_info *avalon9, int addr, unsigned int voltage[])
  1562. {
  1563. struct avalon9_info *info = avalon9->device_data;
  1564. struct avalon9_pkg send_pkg;
  1565. uint32_t tmp;
  1566. uint8_t i;
  1567. memset(send_pkg.data, 0, AVA9_P_DATA_LEN);
  1568. /* NOTE: miner_count should <= 8 */
  1569. for (i = 0; i < info->miner_count[addr]; i++) {
  1570. tmp = be32toh(encode_voltage(voltage[i] +
  1571. opt_avalon9_voltage_level_offset));
  1572. memcpy(send_pkg.data + i * 4, &tmp, 4);
  1573. }
  1574. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set voltage miner %d, (%d-%d)",
  1575. avalon9->drv->name, avalon9->device_id, addr,
  1576. i, voltage[0], voltage[info->miner_count[addr] - 1]);
  1577. /* Package the data */
  1578. avalon9_init_pkg(&send_pkg, AVA9_P_SET_VOLT, 1, 1);
  1579. if (addr == AVA9_MODULE_BROADCAST)
  1580. avalon9_send_bc_pkgs(avalon9, &send_pkg);
  1581. else
  1582. avalon9_iic_xfer_pkg(avalon9, addr, &send_pkg, NULL);
  1583. }
  1584. static void avalon9_set_freq(struct cgpu_info *avalon9, int addr, int miner_id, int asic_id, unsigned int freq[])
  1585. {
  1586. struct avalon9_info *info = avalon9->device_data;
  1587. struct avalon9_pkg send_pkg;
  1588. uint32_t tmp, f;
  1589. uint8_t i;
  1590. uint8_t miner_asic;
  1591. memset(send_pkg.data, 0, AVA9_P_DATA_LEN);
  1592. for (i = 0; i < AVA9_DEFAULT_PLL_CNT; i++) {
  1593. tmp = be32toh(api_get_cpm(freq[i]));
  1594. memcpy(send_pkg.data + i * 4, &tmp, 4);
  1595. }
  1596. f = freq[0];
  1597. for (i = 1; i < AVA9_DEFAULT_PLL_CNT; i++)
  1598. f = f > freq[i] ? f : freq[i];
  1599. f = f ? f : 1;
  1600. tmp = AVA9_ASIC_TIMEOUT_CONST / f * 83 / 100;
  1601. tmp = be32toh(tmp);
  1602. memcpy(send_pkg.data + AVA9_DEFAULT_PLL_CNT * 4, &tmp, 4);
  1603. miner_asic = ((miner_id & 0x07) << 5) | (asic_id & 0x1f);
  1604. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set freq miner %x-%x",
  1605. avalon9->drv->name, avalon9->device_id, addr,
  1606. miner_id, be32toh(tmp));
  1607. /* Package the data */
  1608. avalon9_init_pkg(&send_pkg, AVA9_P_SET_PLL, miner_asic, info->miner_count[addr]);
  1609. if (addr == AVA9_MODULE_BROADCAST)
  1610. avalon9_send_bc_pkgs(avalon9, &send_pkg);
  1611. else
  1612. avalon9_iic_xfer_pkg(avalon9, addr, &send_pkg, NULL);
  1613. }
  1614. static void avalon9_set_factory_info(struct cgpu_info *avalon9, int addr, uint8_t value[])
  1615. {
  1616. struct avalon9_pkg send_pkg;
  1617. uint8_t i;
  1618. memset(send_pkg.data, 0, AVA9_P_DATA_LEN);
  1619. for (i = 0; i < AVA9_DEFAULT_FACTORY_INFO_CNT; i++)
  1620. send_pkg.data[i] = value[i];
  1621. /* Package the data */
  1622. avalon9_init_pkg(&send_pkg, AVA9_P_SET_FAC, 1, 1);
  1623. if (addr == AVA9_MODULE_BROADCAST)
  1624. avalon9_send_bc_pkgs(avalon9, &send_pkg);
  1625. else
  1626. avalon9_iic_xfer_pkg(avalon9, addr, &send_pkg, NULL);
  1627. }
  1628. static void avalon9_set_overclocking_info(struct cgpu_info *avalon9, int addr, uint8_t value[])
  1629. {
  1630. struct avalon9_pkg send_pkg;
  1631. uint8_t i;
  1632. memset(send_pkg.data, 0, AVA9_P_DATA_LEN);
  1633. for (i = 0; i < AVA9_DEFAULT_OVERCLOCKING_CNT; i++)
  1634. send_pkg.data[i] = value[i];
  1635. /* Package the data */
  1636. avalon9_init_pkg(&send_pkg, AVA9_P_SET_OC, 1, 1);
  1637. if (addr == AVA9_MODULE_BROADCAST)
  1638. avalon9_send_bc_pkgs(avalon9, &send_pkg);
  1639. else
  1640. avalon9_iic_xfer_pkg(avalon9, addr, &send_pkg, NULL);
  1641. }
  1642. static void avalon9_set_ss_param(struct cgpu_info *avalon9, int addr)
  1643. {
  1644. struct avalon9_pkg send_pkg;
  1645. uint32_t tmp;
  1646. if (!opt_avalon9_smart_speed)
  1647. return;
  1648. memset(send_pkg.data, 0, AVA9_P_DATA_LEN);
  1649. tmp = (opt_avalon9_th_pass << 16) | opt_avalon9_th_fail;
  1650. tmp = be32toh(tmp);
  1651. memcpy(send_pkg.data, &tmp, 4);
  1652. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set th pass %u",
  1653. avalon9->drv->name, avalon9->device_id, addr,
  1654. opt_avalon9_th_pass);
  1655. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set th fail %u",
  1656. avalon9->drv->name, avalon9->device_id, addr,
  1657. opt_avalon9_th_fail);
  1658. tmp = ((opt_avalon9_th_add & 0x1) << 31) | ((opt_avalon9_th_mssel & 0x1) << 30)
  1659. | ((opt_avalon9_th_ms & 0x3fff) << 16)
  1660. | (opt_avalon9_th_init & 0xffff);
  1661. tmp = be32toh(tmp);
  1662. memcpy(send_pkg.data + 4, &tmp, 4);
  1663. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set th add %u",
  1664. avalon9->drv->name, avalon9->device_id, addr,
  1665. (opt_avalon9_th_add & 0x1));
  1666. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set th mssel %u",
  1667. avalon9->drv->name, avalon9->device_id, addr,
  1668. (opt_avalon9_th_mssel & 0x1));
  1669. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set th ms %u",
  1670. avalon9->drv->name, avalon9->device_id, addr,
  1671. (opt_avalon9_th_ms & 0x3fff));
  1672. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set th init %u",
  1673. avalon9->drv->name, avalon9->device_id, addr,
  1674. (opt_avalon9_th_init & 0xffff));
  1675. tmp = be32toh(opt_avalon9_th_timeout);
  1676. memcpy(send_pkg.data + 8, &tmp, 4);
  1677. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set th timeout %u",
  1678. avalon9->drv->name, avalon9->device_id, addr,
  1679. opt_avalon9_th_timeout);
  1680. tmp = ((opt_avalon9_lv3_th_add & 0x1) << 31) | ((opt_avalon9_lv2_th_add & 0x1) << 15)
  1681. | ((opt_avalon9_lv3_th_ms & 0x7fff) << 16)
  1682. | (opt_avalon9_lv2_th_ms & 0x7fff);
  1683. tmp = be32toh(tmp);
  1684. memcpy(send_pkg.data + 12, &tmp, 4);
  1685. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set lv3 th add %u",
  1686. avalon9->drv->name, avalon9->device_id, addr,
  1687. (opt_avalon9_lv3_th_add & 0x1));
  1688. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set lv3 th ms %u",
  1689. avalon9->drv->name, avalon9->device_id, addr,
  1690. (opt_avalon9_lv3_th_ms & 0x7fff));
  1691. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set lv2 th add %u",
  1692. avalon9->drv->name, avalon9->device_id, addr,
  1693. (opt_avalon9_lv2_th_add & 0x1));
  1694. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set lv2 th ms %u",
  1695. avalon9->drv->name, avalon9->device_id, addr,
  1696. (opt_avalon9_lv2_th_ms & 0x7fff));
  1697. tmp = ((opt_avalon9_lv5_th_add & 0x1) << 31) | ((opt_avalon9_lv4_th_add & 0x1) << 15)
  1698. | ((opt_avalon9_lv5_th_ms & 0x7fff) << 16)
  1699. | (opt_avalon9_lv4_th_ms & 0x7fff);
  1700. tmp = be32toh(tmp);
  1701. memcpy(send_pkg.data + 16, &tmp, 4);
  1702. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set lv5 th add %u",
  1703. avalon9->drv->name, avalon9->device_id, addr,
  1704. (opt_avalon9_lv5_th_add & 0x1));
  1705. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set lv5 th ms %u",
  1706. avalon9->drv->name, avalon9->device_id, addr,
  1707. (opt_avalon9_lv5_th_ms & 0x7fff));
  1708. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set lv4 th add %u",
  1709. avalon9->drv->name, avalon9->device_id, addr,
  1710. (opt_avalon9_lv4_th_add & 0x1));
  1711. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set lv4 th ms %u",
  1712. avalon9->drv->name, avalon9->device_id, addr,
  1713. (opt_avalon9_lv4_th_ms & 0x7fff));
  1714. tmp = ((opt_avalon9_lv7_th_add & 0x1) << 31) | ((opt_avalon9_lv6_th_add & 0x1) << 15)
  1715. | ((opt_avalon9_lv7_th_ms & 0x7fff) << 16)
  1716. | (opt_avalon9_lv6_th_ms & 0x7fff);
  1717. tmp = be32toh(tmp);
  1718. memcpy(send_pkg.data + 20, &tmp, 4);
  1719. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set lv7 th add %u",
  1720. avalon9->drv->name, avalon9->device_id, addr,
  1721. (opt_avalon9_lv7_th_add & 0x1));
  1722. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set lv7 th ms %u",
  1723. avalon9->drv->name, avalon9->device_id, addr,
  1724. (opt_avalon9_lv7_th_ms & 0x7fff));
  1725. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set lv6 th add %u",
  1726. avalon9->drv->name, avalon9->device_id, addr,
  1727. (opt_avalon9_lv6_th_add & 0x1));
  1728. applog(LOG_DEBUG, "%s-%d-%d: avalon9 set lv6 th ms %u",
  1729. avalon9->drv->name, avalon9->device_id, addr,
  1730. (opt_avalon9_lv6_th_ms & 0x7fff));
  1731. /* Package the data */
  1732. avalon9_init_pkg(&send_pkg, AVA9_P_SET_SS, 1, 1);
  1733. if (addr == AVA9_MODULE_BROADCAST)
  1734. avalon9_send_bc_pkgs(avalon9, &send_pkg);
  1735. else
  1736. avalon9_iic_xfer_pkg(avalon9, addr, &send_pkg, NULL);
  1737. }
  1738. static void avalon9_set_ss_param_en(struct cgpu_info *avalon9, int addr, uint8_t en)
  1739. {
  1740. struct avalon9_pkg send_pkg;
  1741. memset(send_pkg.data, 0, AVA9_P_DATA_LEN);
  1742. send_pkg.data[0] = en;
  1743. /* Package the data */
  1744. avalon9_init_pkg(&send_pkg, AVA9_P_SET_SS_PARA_EN, 1, 1);
  1745. if (addr == AVA9_MODULE_BROADCAST)
  1746. avalon9_send_bc_pkgs(avalon9, &send_pkg);
  1747. else
  1748. avalon9_iic_xfer_pkg(avalon9, addr, &send_pkg, NULL);
  1749. }
  1750. static void avalon9_stratum_finish(struct cgpu_info *avalon9)
  1751. {
  1752. struct avalon9_pkg send_pkg;
  1753. memset(send_pkg.data, 0, AVA9_P_DATA_LEN);
  1754. avalon9_init_pkg(&send_pkg, AVA9_P_JOB_FIN, 1, 1);
  1755. avalon9_send_bc_pkgs(avalon9, &send_pkg);
  1756. }
  1757. static void avalon9_set_finish(struct cgpu_info *avalon9, int addr)
  1758. {
  1759. struct avalon9_pkg send_pkg;
  1760. memset(send_pkg.data, 0, AVA9_P_DATA_LEN);
  1761. avalon9_init_pkg(&send_pkg, AVA9_P_SET_FIN, 1, 1);
  1762. avalon9_iic_xfer_pkg(avalon9, addr, &send_pkg, NULL);
  1763. }
  1764. static void avalon9_sswork_update(struct cgpu_info *avalon9)
  1765. {
  1766. struct avalon9_info *info = avalon9->device_data;
  1767. struct thr_info *thr = avalon9->thr[0];
  1768. struct pool *pool;
  1769. int coinbase_len_posthash, coinbase_len_prehash;
  1770. cgtime(&info->last_stratum);
  1771. applog(LOG_NOTICE, "%s-%d: New stratum: restart: %d, update: %d, clean: %d",
  1772. avalon9->drv->name, avalon9->device_id,
  1773. thr->work_restart, thr->work_update, thr->clean_jobs);
  1774. /*
  1775. * NOTE: We need mark work_restart to private information,
  1776. * So that it cann't reset by hash_driver_work
  1777. */
  1778. if (thr->work_restart) {
  1779. info->work_restart = thr->work_restart;
  1780. thr->work_restart = false;
  1781. }
  1782. thr->work_update = false;
  1783. /* Step 1: MM protocol check */
  1784. pool = current_pool();
  1785. if (!pool->has_stratum)
  1786. quit(1, "%s-%d: MM has to use stratum pools", avalon9->drv->name, avalon9->device_id);
  1787. coinbase_len_prehash = pool->nonce2_offset - (pool->nonce2_offset % SHA256_BLOCK_SIZE);
  1788. coinbase_len_posthash = pool->coinbase_len - coinbase_len_prehash;
  1789. if (coinbase_len_posthash + SHA256_BLOCK_SIZE > AVA9_P_COINBASE_SIZE) {
  1790. applog(LOG_ERR, "%s-%d: MM pool modified coinbase length(%d) is more than %d",
  1791. avalon9->drv->name, avalon9->device_id,
  1792. coinbase_len_posthash + SHA256_BLOCK_SIZE, AVA9_P_COINBASE_SIZE);
  1793. return;
  1794. }
  1795. if (pool->merkles > AVA9_P_MERKLES_COUNT) {
  1796. applog(LOG_ERR, "%s-%d: MM merkles has to be less then %d", avalon9->drv->name, avalon9->device_id, AVA9_P_MERKLES_COUNT);
  1797. return;
  1798. }
  1799. if (pool->n2size < 3) {
  1800. applog(LOG_ERR, "%s-%d: MM nonce2 size has to be >= 3 (%d)", avalon9->drv->name, avalon9->device_id, pool->n2size);
  1801. return;
  1802. }
  1803. cg_wlock(&info->update_lock);
  1804. /* Step 2: Send out stratum pkgs */
  1805. cg_rlock(&pool->data_lock);
  1806. copy_pool_stratum(&info->pool2, &info->pool1);
  1807. copy_pool_stratum(&info->pool1, &info->pool0);
  1808. if (copy_pool_stratum(&info->pool0, pool)) {
  1809. cg_runlock(&pool->data_lock);
  1810. cg_wunlock(&info->update_lock);
  1811. } else {
  1812. info->pool_no = pool->pool_no;
  1813. avalon9_stratum_pkgs(avalon9, pool);
  1814. cg_runlock(&pool->data_lock);
  1815. /* Step 3: Send out finish pkg */
  1816. avalon9_stratum_finish(avalon9);
  1817. cg_wunlock(&info->update_lock);
  1818. }
  1819. }
  1820. static int64_t avalon9_scanhash(struct thr_info *thr)
  1821. {
  1822. struct cgpu_info *avalon9 = thr->cgpu;
  1823. struct avalon9_info *info = avalon9->device_data;
  1824. struct timeval current;
  1825. int i, j, k, count = 0;
  1826. int temp_max;
  1827. int64_t ret;
  1828. bool update_settings = false;
  1829. if ((info->connecter == AVA9_CONNECTER_AUC) &&
  1830. (unlikely(avalon9->usbinfo.nodev))) {
  1831. applog(LOG_ERR, "%s-%d: Device disappeared, shutting down thread",
  1832. avalon9->drv->name, avalon9->device_id);
  1833. return -1;
  1834. }
  1835. /* Step 1: Stop polling and detach the device if there is no stratum in 3 minutes, network is down */
  1836. cgtime(&current);
  1837. if (tdiff(&current, &(info->last_stratum)) > 180.0) {
  1838. for (i = 1; i < AVA9_DEFAULT_MODULARS; i++) {
  1839. if (!info->enable[i])
  1840. continue;
  1841. detach_module(avalon9, i);
  1842. }
  1843. info->mm_count = 0;
  1844. return 0;
  1845. }
  1846. /* Step 2: Try to detect new modules */
  1847. if ((tdiff(&current, &(info->last_detect)) > AVA9_MODULE_DETECT_INTERVAL) ||
  1848. !info->mm_count) {
  1849. cgtime(&info->last_detect);
  1850. detect_modules(avalon9);
  1851. }
  1852. /* Step 3: ASIC configrations (voltage and frequency) */
  1853. for (i = 1; i < AVA9_DEFAULT_MODULARS; i++) {
  1854. if (!info->enable[i])
  1855. continue;
  1856. update_settings = false;
  1857. /* Check temperautre */
  1858. temp_max = get_temp_max(info, i);
  1859. /* Enter too hot */
  1860. if (temp_max >= info->temp_overheat[i])
  1861. info->cutoff[i] = 1;
  1862. /* Exit too hot */
  1863. if (info->cutoff[i] && (temp_max <= (info->temp_overheat[i] - 10)))
  1864. info->cutoff[i] = 0;
  1865. switch (info->freq_mode[i]) {
  1866. case AVA9_FREQ_INIT_MODE:
  1867. update_settings = true;
  1868. for (j = 0; j < info->miner_count[i]; j++) {
  1869. for (k = 0; k < AVA9_DEFAULT_PLL_CNT; k++) {
  1870. if (opt_avalon9_freq[k] != AVA9_DEFAULT_FREQUENCY_IGNORE)
  1871. info->set_frequency[i][j][k] = opt_avalon9_freq[k];
  1872. }
  1873. }
  1874. if (!strncmp((char *)&(info->mm_version[i]), "921", 3) ||
  1875. (!strncmp((char *)&(info->mm_version[i]), "920", 3) &&
  1876. (info->mm_version[i][3] == 'P'))) {
  1877. if (opt_avalon9_spdlow == AVA9_INVALID_SPDLOW)
  1878. opt_avalon9_spdlow = AVA9_DEFAULT_MM921_SPDLOW;
  1879. } else if (!strncmp((char *)&(info->mm_version[i]), "920", 3)) {
  1880. if (opt_avalon9_spdlow == AVA9_INVALID_SPDLOW)
  1881. opt_avalon9_spdlow = AVA9_DEFAULT_MM920_SPDLOW;
  1882. } else {
  1883. if (opt_avalon9_spdlow == AVA9_INVALID_SPDLOW)
  1884. opt_avalon9_spdlow = AVA9_DEFAULT_SPDLOW;
  1885. }
  1886. avalon9_init_setting(avalon9, i);
  1887. info->freq_mode[i] = AVA9_FREQ_PLLADJ_MODE;
  1888. break;
  1889. case AVA9_FREQ_PLLADJ_MODE:
  1890. if (opt_avalon9_smart_speed == AVA9_DEFAULT_SMARTSPEED_OFF)
  1891. break;
  1892. /* AVA9_DEFAULT_SMARTSPEED_MODE1: auto speed by A3206 chips */
  1893. break;
  1894. default:
  1895. applog(LOG_ERR, "%s-%d-%d: Invalid frequency mode %d",
  1896. avalon9->drv->name, avalon9->device_id, i, info->freq_mode[i]);
  1897. break;
  1898. }
  1899. if (update_settings) {
  1900. cg_wlock(&info->update_lock);
  1901. avalon9_set_voltage_level(avalon9, i, info->set_voltage_level[i]);
  1902. for (j = 0; j < info->miner_count[i]; j++)
  1903. avalon9_set_freq(avalon9, i, j, 0, info->set_frequency[i][j]);
  1904. avalon9_set_adjust_voltage_option(avalon9, i,
  1905. opt_avalon9_adjust_volt_up_init,
  1906. opt_avalon9_adjust_volt_up_factor,
  1907. opt_avalon9_adjust_volt_up_threshold,
  1908. opt_avalon9_adjust_volt_down_init,
  1909. opt_avalon9_adjust_volt_down_factor,
  1910. opt_avalon9_adjust_volt_down_threshold,
  1911. opt_avalon9_adjust_volt_time,
  1912. opt_avalon9_adjust_volt_enable);
  1913. if (opt_avalon9_smart_speed)
  1914. avalon9_set_ss_param(avalon9, i);
  1915. avalon9_set_finish(avalon9, i);
  1916. cg_wunlock(&info->update_lock);
  1917. }
  1918. }
  1919. /* Step 4: Polling */
  1920. cg_rlock(&info->update_lock);
  1921. polling(avalon9);
  1922. cg_runlock(&info->update_lock);
  1923. /* Step 5: Calculate mm count */
  1924. for (i = 1; i < AVA9_DEFAULT_MODULARS; i++) {
  1925. if (info->enable[i])
  1926. count++;
  1927. }
  1928. info->mm_count = count;
  1929. /* Step 6: Calculate hashes. Use the diff1 value which is scaled by
  1930. * device diff and is usually lower than pool diff which will give a
  1931. * more stable result, but remove diff rejected shares to more closely
  1932. * approximate diff accepted values. */
  1933. info->pending_diff1 += avalon9->diff1 - info->last_diff1;
  1934. info->last_diff1 = avalon9->diff1;
  1935. info->pending_diff1 -= avalon9->diff_rejected - info->last_rej;
  1936. info->last_rej = avalon9->diff_rejected;
  1937. if (info->pending_diff1 && !info->firsthash.tv_sec) {
  1938. cgtime(&info->firsthash);
  1939. copy_time(&(avalon9->dev_start_tv), &(info->firsthash));
  1940. }
  1941. if (info->pending_diff1 <= 0)
  1942. ret = 0;
  1943. else {
  1944. ret = info->pending_diff1;
  1945. info->pending_diff1 = 0;
  1946. }
  1947. return ret * 0xffffffffull;
  1948. }
  1949. static float avalon9_hash_cal(struct cgpu_info *avalon9, int modular_id)
  1950. {
  1951. struct avalon9_info *info = avalon9->device_data;
  1952. uint32_t tmp_freq[AVA9_DEFAULT_PLL_CNT];
  1953. unsigned int i, j, k;
  1954. float mhsmm;
  1955. mhsmm = 0;
  1956. for (i = 0; i < info->miner_count[modular_id]; i++) {
  1957. for (j = 0; j < info->asic_count[modular_id]; j++) {
  1958. for (k = 0; k < AVA9_DEFAULT_PLL_CNT; k++)
  1959. mhsmm += (info->get_asic[modular_id][i][j][2 + k] * info->get_frequency[modular_id][i][j][k]);
  1960. }
  1961. }
  1962. return mhsmm;
  1963. }
  1964. #define STATBUFLEN_WITHOUT_DBG (6 * 1024)
  1965. #define STATBUFLEN_WITH_DBG (6 * 7 * 1024)
  1966. static struct api_data *avalon9_api_stats(struct cgpu_info *avalon9)
  1967. {
  1968. struct api_data *root = NULL;
  1969. struct avalon9_info *info = avalon9->device_data;
  1970. int i, j, k, m;
  1971. double a, b, dh;
  1972. char buf[256];
  1973. char *statbuf = NULL;
  1974. struct timeval current;
  1975. float mhsmm, auc_temp = 0.0;
  1976. double sum;
  1977. int avg, cnt, max_vl, max_id, min_vl, min_id;
  1978. cgtime(&current);
  1979. if (opt_debug)
  1980. statbuf = cgcalloc(STATBUFLEN_WITH_DBG, 1);
  1981. else
  1982. statbuf = cgcalloc(STATBUFLEN_WITHOUT_DBG, 1);
  1983. for (i = 1; i < AVA9_DEFAULT_MODULARS; i++) {
  1984. if (!info->enable[i])
  1985. continue;
  1986. sprintf(buf, "Ver[%s]", info->mm_version[i]);
  1987. strcpy(statbuf, buf);
  1988. sprintf(buf, " DNA[%02x%02x%02x%02x%02x%02x%02x%02x]",
  1989. info->mm_dna[i][0],
  1990. info->mm_dna[i][1],
  1991. info->mm_dna[i][2],
  1992. info->mm_dna[i][3],
  1993. info->mm_dna[i][4],
  1994. info->mm_dna[i][5],
  1995. info->mm_dna[i][6],
  1996. info->mm_dna[i][7]);
  1997. strcat(statbuf, buf);
  1998. sprintf(buf, " Elapsed[%.0f]", tdiff(&current, &(info->elapsed[i])));
  1999. strcat(statbuf, buf);
  2000. strcat(statbuf, " MW[");
  2001. info->local_works[i] = 0;
  2002. for (j = 0; j < info->miner_count[i]; j++) {
  2003. info->local_works[i] += info->local_works_i[i][j];
  2004. sprintf(buf, "%"PRIu64" ", info->local_works_i[i][j]);
  2005. strcat(statbuf, buf);
  2006. }
  2007. statbuf[strlen(statbuf) - 1] = ']';
  2008. sprintf(buf, " LW[%"PRIu64"]", info->local_works[i]);
  2009. strcat(statbuf, buf);
  2010. strcat(statbuf, " MH[");
  2011. info->hw_works[i] = 0;
  2012. for (j = 0; j < info->miner_count[i]; j++) {
  2013. info->hw_works[i] += info->hw_works_i[i][j];
  2014. sprintf(buf, "%"PRIu64" ", info->hw_works_i[i][j]);
  2015. strcat(statbuf, buf);
  2016. }
  2017. statbuf[strlen(statbuf) - 1] = ']';
  2018. sprintf(buf, " HW[%"PRIu64"]", info->hw_works[i]);
  2019. strcat(statbuf, buf);
  2020. {
  2021. double a, b, dh;
  2022. a = 0;
  2023. b = 0;
  2024. for (j = 0; j < info->miner_count[i]; j++) {
  2025. for (k = 0; k < info->asic_count[i]; k++) {
  2026. a += info->get_asic[i][j][k][0];
  2027. b += info->get_asic[i][j][k][1];
  2028. }
  2029. }
  2030. dh = b ? (b / (a + b)) * 100 : 0;
  2031. sprintf(buf, " DH[%.3f%%]", dh);
  2032. strcat(statbuf, buf);
  2033. }
  2034. sprintf(buf, " Temp[%d]", info->temp_mm[i]);
  2035. strcat(statbuf, buf);
  2036. sprintf(buf, " TMax[%d]", get_temp_max(info, i));
  2037. strcat(statbuf, buf);
  2038. sprintf(buf, " Fan[%d]", info->fan_cpm[i]);
  2039. strcat(statbuf, buf);
  2040. sprintf(buf, " FanR[%d%%]", info->fan_pct[i]);
  2041. strcat(statbuf, buf);
  2042. sprintf(buf, " Vi[");
  2043. strcat(statbuf, buf);
  2044. for (j = 0; j < info->miner_count[i]; j++) {
  2045. sprintf(buf, "%d ", info->get_vin[i][j]);
  2046. strcat(statbuf, buf);
  2047. }
  2048. statbuf[strlen(statbuf) - 1] = ']';
  2049. sprintf(buf, " Vo[");
  2050. strcat(statbuf, buf);
  2051. for (j = 0; j < info->miner_count[i]; j++) {
  2052. sprintf(buf, "%d ", info->get_voltage[i][j]);
  2053. strcat(statbuf, buf);
  2054. }
  2055. statbuf[strlen(statbuf) - 1] = ']';
  2056. if (opt_debug) {
  2057. for (j = 0; j < info->miner_count[i]; j++) {
  2058. sprintf(buf, " PLL%d[", j);
  2059. strcat(statbuf, buf);
  2060. for (k = 0; k < AVA9_DEFAULT_PLL_CNT; k++) {
  2061. sprintf(buf, "%d ", info->get_pll[i][j][k]);
  2062. strcat(statbuf, buf);
  2063. }
  2064. statbuf[strlen(statbuf) - 1] = ']';
  2065. }
  2066. }
  2067. mhsmm = avalon9_hash_cal(avalon9, i);
  2068. sprintf(buf, " GHSmm[%.2f] WU[%.2f] Freq[%.2f]", (float)mhsmm / 1000,
  2069. info->diff1[i] / tdiff(&current, &(info->elapsed[i])) * 60.0,
  2070. (float)mhsmm / (info->asic_count[i] * info->miner_count[i] * 256));
  2071. strcat(statbuf, buf);
  2072. sprintf(buf, " PG[%d]", info->power_good[i]);
  2073. strcat(statbuf, buf);
  2074. sprintf(buf, " Led[%d]", info->led_indicator[i]);
  2075. strcat(statbuf, buf);
  2076. for (j = 0; j < info->miner_count[i]; j++) {
  2077. sprintf(buf, " MW%d[", j);
  2078. strcat(statbuf, buf);
  2079. for (k = 0; k < info->asic_count[i]; k++) {
  2080. sprintf(buf, "%"PRIu64" ", info->chip_matching_work[i][j][k]);
  2081. strcat(statbuf, buf);
  2082. }
  2083. statbuf[strlen(statbuf) - 1] = ']';
  2084. }
  2085. sprintf(buf, " TA[%d]", info->total_asics[i]);
  2086. strcat(statbuf, buf);
  2087. strcat(statbuf, " ECHU[");
  2088. for (j = 0; j < info->miner_count[i]; j++) {
  2089. sprintf(buf, "%d ", info->error_code[i][j]);
  2090. strcat(statbuf, buf);
  2091. }
  2092. statbuf[strlen(statbuf) - 1] = ']';
  2093. sprintf(buf, " ECMM[%d]", info->error_code[i][j]);
  2094. strcat(statbuf, buf);
  2095. if (opt_debug) {
  2096. sprintf(buf, " FAC0[");
  2097. strcat(statbuf, buf);
  2098. for (j = 0; j < info->miner_count[i]; j++) {
  2099. sprintf(buf, "%d ", info->factory_info[i][j]);
  2100. strcat(statbuf, buf);
  2101. }
  2102. statbuf[strlen(statbuf) - 1] = ']';
  2103. sprintf(buf, " OC[%d]", info->overclocking_info[0]);
  2104. strcat(statbuf, buf);
  2105. sprintf(buf, " SSPE[%d]", info->ss_para_en[i]);
  2106. strcat(statbuf, buf);
  2107. for (j = 0; j < info->miner_count[i]; j++) {
  2108. sprintf(buf, " SF%d[", j);
  2109. strcat(statbuf, buf);
  2110. for (k = 0; k < AVA9_DEFAULT_PLL_CNT; k++) {
  2111. sprintf(buf, "%d ", info->set_frequency[i][j][k]);
  2112. strcat(statbuf, buf);
  2113. }
  2114. statbuf[strlen(statbuf) - 1] = ']';
  2115. }
  2116. strcat(statbuf, " PMUV[");
  2117. for (j = 0; j < AVA9_DEFAULT_PMU_CNT; j++) {
  2118. sprintf(buf, "%s ", info->pmu_version[i][j]);
  2119. strcat(statbuf, buf);
  2120. }
  2121. statbuf[strlen(statbuf) - 1] = ']';
  2122. for (j = 0; j < info->miner_count[i]; j++) {
  2123. sprintf(buf, " PVT_T%d[", j);
  2124. strcat(statbuf, buf);
  2125. for (k = 0; k < info->asic_count[i]; k++) {
  2126. sprintf(buf, "%3d ", info->temp[i][j][k]);
  2127. strcat(statbuf, buf);
  2128. }
  2129. statbuf[strlen(statbuf) - 1] = ']';
  2130. statbuf[strlen(statbuf)] = '\0';
  2131. }
  2132. for (j = 0; j < info->miner_count[i]; j++) {
  2133. sprintf(buf, " PVT_V%d[", j);
  2134. strcat(statbuf, buf);
  2135. for (k = 0; k < info->asic_count[i]; k++) {
  2136. sprintf(buf, "%d ", info->core_volt[i][j][k]);
  2137. strcat(statbuf, buf);
  2138. }
  2139. statbuf[strlen(statbuf) - 1] = ']';
  2140. statbuf[strlen(statbuf)] = '\0';
  2141. }
  2142. for (m = 0; m < AVA9_DEFAULT_RO_CHANNEL_CNT; m++) {
  2143. for (j = 0; j < info->miner_count[i]; j++) {
  2144. sprintf(buf, " PVT_P%d_%02d[", j, m);
  2145. strcat(statbuf, buf);
  2146. for (k = 0; k < info->asic_count[i]; k++) {
  2147. sprintf(buf, "%08x ", info->pvt_ro[i][j][k][m]);
  2148. strcat(statbuf, buf);
  2149. }
  2150. statbuf[strlen(statbuf) - 1] = ']';
  2151. statbuf[strlen(statbuf)] = '\0';
  2152. }
  2153. }
  2154. for (j = 0; j < info->miner_count[i]; j++) {
  2155. sprintf(buf, " ERATIO%d[", j);
  2156. strcat(statbuf, buf);
  2157. for (k = 0; k < info->asic_count[i]; k++) {
  2158. if (info->get_asic[i][j][k][0])
  2159. sprintf(buf, "%6.2f%% ", (double)(info->get_asic[i][j][k][1] * 100.0 / (info->get_asic[i][j][k][0] + info->get_asic[i][j][k][1])));
  2160. else
  2161. sprintf(buf, "%6.2f%% ", 0.0);
  2162. strcat(statbuf, buf);
  2163. }
  2164. statbuf[strlen(statbuf) - 1] = ']';
  2165. }
  2166. int l;
  2167. /* i: modular, j: miner, k:asic, l:value */
  2168. for (j = 0; j < info->miner_count[i]; j++) {
  2169. for (l = 0; l < AVA9_DEFAULT_PLL_CNT; l++) {
  2170. sprintf(buf, " GF%d_%d[", j, l);
  2171. strcat(statbuf, buf);
  2172. for (k = 0; k < info->asic_count[i]; k++) {
  2173. sprintf(buf, "%3d ", info->get_frequency[i][j][k][l]);
  2174. strcat(statbuf, buf);
  2175. }
  2176. statbuf[strlen(statbuf) - 1] = ']';
  2177. statbuf[strlen(statbuf)] = '\0';
  2178. }
  2179. }
  2180. for (j = 0; j < info->miner_count[i]; j++) {
  2181. for (l = 0; l < AVA9_DEFAULT_PLL_CNT; l++) {
  2182. sprintf(buf, " PLL%d_%d[", j, l);
  2183. strcat(statbuf, buf);
  2184. for (k = 0; k < info->asic_count[i]; k++) {
  2185. sprintf(buf, "%3d ", info->get_asic[i][j][k][2 + l]);
  2186. strcat(statbuf, buf);
  2187. }
  2188. statbuf[strlen(statbuf) - 1] = ']';
  2189. statbuf[strlen(statbuf)] = '\0';
  2190. }
  2191. }
  2192. for (l = 0; l < 2; l++) {
  2193. for (j = 0; j < info->miner_count[i]; j++) {
  2194. sprintf(buf, " C_%02d_%02d[", j, l);
  2195. strcat(statbuf, buf);
  2196. for (k = 0; k < info->asic_count[i]; k++) {
  2197. sprintf(buf, "%7d ", info->get_asic[i][j][k][l]);
  2198. strcat(statbuf, buf);
  2199. }
  2200. statbuf[strlen(statbuf) - 1] = ']';
  2201. }
  2202. }
  2203. for (j = 0; j < info->miner_count[i]; j++) {
  2204. sprintf(buf, " GHSmm%02d[", j);
  2205. strcat(statbuf, buf);
  2206. for (k = 0; k < info->asic_count[i]; k++) {
  2207. mhsmm = 0;
  2208. for (l = 2; l < (2 + AVA9_DEFAULT_PLL_CNT); l++) {
  2209. mhsmm += (info->get_asic[i][j][k][l] * info->get_frequency[i][j][k][l - 2]);
  2210. }
  2211. sprintf(buf, "%7.2f ", mhsmm / 1000);
  2212. strcat(statbuf, buf);
  2213. }
  2214. statbuf[strlen(statbuf) - 1] = ']';
  2215. }
  2216. }
  2217. sprintf(buf, " FM[%d]", info->freq_mode[i]);
  2218. strcat(statbuf, buf);
  2219. strcat(statbuf, " CRC[");
  2220. for (j = 0; j < info->miner_count[i]; j++) {
  2221. sprintf(buf, "%d ", info->error_crc[i][j]);
  2222. strcat(statbuf, buf);
  2223. }
  2224. statbuf[strlen(statbuf) - 1] = ']';
  2225. sprintf(buf, "MM ID%d", i);
  2226. root = api_add_string(root, buf, statbuf, true);
  2227. }
  2228. free(statbuf);
  2229. root = api_add_int(root, "MM Count", &(info->mm_count), true);
  2230. root = api_add_int(root, "Smart Speed", &opt_avalon9_smart_speed, true);
  2231. if (info->connecter == AVA9_CONNECTER_IIC)
  2232. root = api_add_string(root, "Connecter", "IIC", true);
  2233. if (info->connecter == AVA9_CONNECTER_AUC) {
  2234. root = api_add_string(root, "Connecter", "AUC", true);
  2235. root = api_add_string(root, "AUC VER", info->auc_version, false);
  2236. root = api_add_int(root, "AUC I2C Speed", &(info->auc_speed), true);
  2237. root = api_add_int(root, "AUC I2C XDelay", &(info->auc_xdelay), true);
  2238. root = api_add_int(root, "AUC Sensor", &(info->auc_sensor), true);
  2239. auc_temp = decode_auc_temp(info->auc_sensor);
  2240. root = api_add_temp(root, "AUC Temperature", &auc_temp, true);
  2241. }
  2242. root = api_add_bool(root, "Connection Overloaded", &info->conn_overloaded, true);
  2243. root = api_add_int(root, "Voltage Level Offset", &opt_avalon9_voltage_level_offset, true);
  2244. root = api_add_uint32(root, "Nonce Mask", &opt_avalon9_nonce_mask, true);
  2245. return root;
  2246. }
  2247. /* format: voltage[-addr[-miner]]
  2248. * addr[0, AVA9_DEFAULT_MODULARS - 1], 0 means all modulars
  2249. * miner[0, miner_count], 0 means all miners
  2250. */
  2251. char *set_avalon9_device_voltage_level(struct cgpu_info *avalon9, char *arg)
  2252. {
  2253. struct avalon9_info *info = avalon9->device_data;
  2254. int val;
  2255. unsigned int addr = 0, i, j;
  2256. uint32_t miner_id = 0;
  2257. if (!(*arg))
  2258. return NULL;
  2259. sscanf(arg, "%d-%d-%d", &val, &addr, &miner_id);
  2260. if (val < AVA9_DEFAULT_VOLTAGE_LEVEL_MIN || val > AVA9_DEFAULT_VOLTAGE_LEVEL_MAX)
  2261. return "Invalid value passed to set_avalon9_device_voltage_level";
  2262. if (addr >= AVA9_DEFAULT_MODULARS) {
  2263. applog(LOG_ERR, "invalid modular index: %d, valid range 0-%d", addr, (AVA9_DEFAULT_MODULARS - 1));
  2264. return "Invalid modular index to set_avalon9_device_voltage_level";
  2265. }
  2266. if (!addr) {
  2267. for (i = 1; i < AVA9_DEFAULT_MODULARS; i++) {
  2268. if (!info->enable[i])
  2269. continue;
  2270. if (miner_id > info->miner_count[i]) {
  2271. applog(LOG_ERR, "invalid miner index: %d, valid range 0-%d", miner_id, info->miner_count[i]);
  2272. return "Invalid miner index to set_avalon9_device_voltage_level";
  2273. }
  2274. if (miner_id)
  2275. info->set_voltage_level[i][miner_id - 1] = val;
  2276. else {
  2277. for (j = 0; j < info->miner_count[i]; j++)
  2278. info->set_voltage_level[i][j] = val;
  2279. }
  2280. avalon9_set_voltage_level(avalon9, i, info->set_voltage_level[i]);
  2281. }
  2282. } else {
  2283. if (!info->enable[addr]) {
  2284. applog(LOG_ERR, "Disabled modular:%d", addr);
  2285. return "Disabled modular to set_avalon9_device_voltage_level";
  2286. }
  2287. if (miner_id > info->miner_count[addr]) {
  2288. applog(LOG_ERR, "invalid miner index: %d, valid range 0-%d", miner_id, info->miner_count[addr]);
  2289. return "Invalid miner index to set_avalon9_device_voltage_level";
  2290. }
  2291. if (miner_id)
  2292. info->set_voltage_level[addr][miner_id - 1] = val;
  2293. else {
  2294. for (j = 0; j < info->miner_count[addr]; j++)
  2295. info->set_voltage_level[addr][j] = val;
  2296. }
  2297. avalon9_set_voltage_level(avalon9, addr, info->set_voltage_level[addr]);
  2298. }
  2299. applog(LOG_NOTICE, "%s-%d: Update voltage-level to %d", avalon9->drv->name, avalon9->device_id, val);
  2300. return NULL;
  2301. }
  2302. /*
  2303. * format: freq[-addr[-miner]]
  2304. * addr[0, AVA9_DEFAULT_MODULARS - 1], 0 means all modulars
  2305. * miner[0, miner_count], 0 means all miners
  2306. */
  2307. char *set_avalon9_device_freq(struct cgpu_info *avalon9, char *arg)
  2308. {
  2309. struct avalon9_info *info = avalon9->device_data;
  2310. unsigned int val[AVA9_DEFAULT_PLL_CNT], addr = 0, i, j, k;
  2311. uint32_t miner_id = 0;
  2312. uint32_t asic_id = 0;
  2313. if (!(*arg))
  2314. return NULL;
  2315. sscanf(arg, "%d:%d:%d:%d:%d:%d:%d-%d-%d-%d", &val[0], &val[1], &val[2], &val[3], &val[4], &val[5], &val[6], &addr, &miner_id, &asic_id);
  2316. if (val[AVA9_DEFAULT_PLL_CNT - 1] > AVA9_DEFAULT_FREQUENCY_MAX)
  2317. return "Invalid value passed to set_avalon9_device_freq";
  2318. if (addr >= AVA9_DEFAULT_MODULARS) {
  2319. applog(LOG_ERR, "invalid modular index: %d, valid range 0-%d", addr, (AVA9_DEFAULT_MODULARS - 1));
  2320. return "Invalid modular index to set_avalon9_device_freq";
  2321. }
  2322. if (!addr) {
  2323. for (i = 1; i < AVA9_DEFAULT_MODULARS; i++) {
  2324. if (!info->enable[i])
  2325. continue;
  2326. if (miner_id > info->miner_count[i]) {
  2327. applog(LOG_ERR, "invalid miner index: %d, valid range 0-%d", miner_id, info->miner_count[i]);
  2328. return "Invalid miner index to set_avalon9_device_freq";
  2329. }
  2330. if (miner_id) {
  2331. for (k = 0; k < AVA9_DEFAULT_PLL_CNT; k++)
  2332. info->set_frequency[i][miner_id - 1][k] = val[k];
  2333. avalon9_set_freq(avalon9, i, miner_id - 1, asic_id, info->set_frequency[i][miner_id - 1]);
  2334. } else {
  2335. for (j = 0; j < info->miner_count[i]; j++) {
  2336. for (k = 0; k < AVA9_DEFAULT_PLL_CNT; k++)
  2337. info->set_frequency[i][j][k] = val[k];
  2338. avalon9_set_freq(avalon9, i, j, asic_id, info->set_frequency[i][j]);
  2339. }
  2340. }
  2341. }
  2342. } else {
  2343. if (!info->enable[addr]) {
  2344. applog(LOG_ERR, "Disabled modular:%d", addr);
  2345. return "Disabled modular to set_avalon9_device_freq";
  2346. }
  2347. if (miner_id > info->miner_count[addr]) {
  2348. applog(LOG_ERR, "invalid miner index: %d, valid range 0-%d", miner_id, info->miner_count[addr]);
  2349. return "Invalid miner index to set_avalon9_device_freq";
  2350. }
  2351. if (miner_id) {
  2352. for (k = 0; k < AVA9_DEFAULT_PLL_CNT; k++)
  2353. info->set_frequency[addr][miner_id - 1][k] = val[k];
  2354. avalon9_set_freq(avalon9, addr, miner_id - 1, asic_id, info->set_frequency[addr][miner_id - 1]);
  2355. } else {
  2356. for (j = 0; j < info->miner_count[addr]; j++) {
  2357. for (k = 0; k < AVA9_DEFAULT_PLL_CNT; k++)
  2358. info->set_frequency[addr][j][k] = val[k];
  2359. avalon9_set_freq(avalon9, addr, j, asic_id, info->set_frequency[addr][j]);
  2360. }
  2361. }
  2362. }
  2363. applog(LOG_NOTICE, "%s-%d: Update frequency to %d",
  2364. avalon9->drv->name, avalon9->device_id, val[AVA9_DEFAULT_PLL_CNT - 1]);
  2365. return NULL;
  2366. }
  2367. char *set_avalon9_factory_info(struct cgpu_info *avalon9, char *arg)
  2368. {
  2369. struct avalon9_info *info = avalon9->device_data;
  2370. char type[AVA9_DEFAULT_FACTORY_INFO_1_CNT] = {0};
  2371. char type_plus[AVA9_DEFAULT_FACTORY_INFO_2_CNT] = {0};
  2372. char type_all[AVA9_DEFAULT_FACTORY_INFO_1_CNT + AVA9_DEFAULT_FACTORY_INFO_2_CNT + 1] = {0};
  2373. int val, i;
  2374. if (!(*arg))
  2375. return NULL;
  2376. sscanf(arg, "%d-%s", &val, type_all);
  2377. memcpy(type, &type_all[0], AVA9_DEFAULT_FACTORY_INFO_1_CNT);
  2378. memcpy(type_plus, &type_all[AVA9_DEFAULT_FACTORY_INFO_1_CNT + 1], AVA9_DEFAULT_FACTORY_INFO_2_CNT);
  2379. if ((val != AVA9_DEFAULT_FACTORY_INFO_0_IGNORE) &&
  2380. (val < AVA9_DEFAULT_FACTORY_INFO_0_MIN || val > AVA9_DEFAULT_FACTORY_INFO_0_MAX))
  2381. return "Invalid value passed to set_avalon9_factory_info";
  2382. for (i = 1; i < AVA9_DEFAULT_MODULARS; i++) {
  2383. if (!info->enable[i])
  2384. continue;
  2385. info->factory_info[i][0] = val;
  2386. memcpy(&info->factory_info[i][1], type, AVA9_DEFAULT_FACTORY_INFO_1_CNT);
  2387. memcpy(&info->factory_info[i][4], type_plus, AVA9_DEFAULT_FACTORY_INFO_2_CNT);
  2388. avalon9_set_factory_info(avalon9, i, (uint8_t *)info->factory_info[i]);
  2389. }
  2390. applog(LOG_NOTICE, "%s-%d: Update factory info %d",
  2391. avalon9->drv->name, avalon9->device_id, val);
  2392. return NULL;
  2393. }
  2394. char *set_avalon9_overclocking_info(struct cgpu_info *avalon9, char *arg)
  2395. {
  2396. struct avalon9_info *info = avalon9->device_data;
  2397. int val;
  2398. if (!(*arg))
  2399. return NULL;
  2400. sscanf(arg, "%d", &val);
  2401. if (val != AVA9_DEFAULT_OVERCLOCKING_OFF && val != AVA9_DEFAULT_OVERCLOCKING_ON)
  2402. return "Invalid value passed to set_avalon9_overclocking_info";
  2403. info->overclocking_info[0] = val;
  2404. avalon9_set_overclocking_info(avalon9, 0, (uint8_t *)info->overclocking_info);
  2405. applog(LOG_NOTICE, "%s-%d: Update Overclocking info %d",
  2406. avalon9->drv->name, avalon9->device_id, val);
  2407. return NULL;
  2408. }
  2409. char *set_avalon9_ss_param_en(struct cgpu_info *avalon9, char *arg)
  2410. {
  2411. struct avalon9_info *info = avalon9->device_data;
  2412. int val, i;
  2413. if (!(*arg))
  2414. return NULL;
  2415. sscanf(arg, "%d", &val);
  2416. if ((val != 0) && (val != 1))
  2417. return "Invalid value passed to set_avalon9_ss_param_en";
  2418. for (i = 1; i < AVA9_DEFAULT_MODULARS; i++) {
  2419. if (!info->enable[i])
  2420. continue;
  2421. avalon9_set_ss_param_en(avalon9, i, val);
  2422. }
  2423. applog(LOG_NOTICE, "%s-%d: Update ss param enable %d",
  2424. avalon9->drv->name, avalon9->device_id, val);
  2425. return NULL;
  2426. }
  2427. static char *avalon9_set_device(struct cgpu_info *avalon9, char *option, char *setting, char *replybuf, size_t siz)
  2428. {
  2429. unsigned int val;
  2430. struct avalon9_info *info = avalon9->device_data;
  2431. if (strcasecmp(option, "help") == 0) {
  2432. snprintf(replybuf, siz, "pdelay|fan|frequency|led|voltage");
  2433. return replybuf;
  2434. }
  2435. if (strcasecmp(option, "pdelay") == 0) {
  2436. if (!setting || !*setting) {
  2437. snprintf(replybuf, siz, "missing polling delay setting");
  2438. return replybuf;
  2439. }
  2440. val = (unsigned int)atoi(setting);
  2441. if (val < 1 || val > 65535) {
  2442. snprintf(replybuf, siz, "invalid polling delay: %d, valid range 1-65535", val);
  2443. return replybuf;
  2444. }
  2445. opt_avalon9_polling_delay = val;
  2446. applog(LOG_NOTICE, "%s-%d: Update polling delay to: %d",
  2447. avalon9->drv->name, avalon9->device_id, val);
  2448. return NULL;
  2449. }
  2450. if (strcasecmp(option, "fan") == 0) {
  2451. if (!setting || !*setting) {
  2452. snprintf(replybuf, siz, "missing fan value");
  2453. return replybuf;
  2454. }
  2455. if (set_avalon9_fan(setting)) {
  2456. snprintf(replybuf, siz, "invalid fan value, valid range 0-100");
  2457. return replybuf;
  2458. }
  2459. applog(LOG_NOTICE, "%s-%d: Update fan to %d-%d",
  2460. avalon9->drv->name, avalon9->device_id,
  2461. opt_avalon9_fan_min, opt_avalon9_fan_max);
  2462. return NULL;
  2463. }
  2464. if (strcasecmp(option, "frequency") == 0) {
  2465. if (!setting || !*setting) {
  2466. snprintf(replybuf, siz, "missing frequency value");
  2467. return replybuf;
  2468. }
  2469. return set_avalon9_device_freq(avalon9, setting);
  2470. }
  2471. if (strcasecmp(option, "led") == 0) {
  2472. int val_led = -1;
  2473. if (!setting || !*setting) {
  2474. snprintf(replybuf, siz,"missing module_id setting");
  2475. return replybuf;
  2476. }
  2477. sscanf(setting, "%d-%d", &val, &val_led);
  2478. if (val < 1 || val >= AVA9_DEFAULT_MODULARS) {
  2479. snprintf(replybuf, siz, "invalid module_id: %d, valid range 1-%d", val, AVA9_DEFAULT_MODULARS);
  2480. return replybuf;
  2481. }
  2482. if (!info->enable[val]) {
  2483. snprintf(replybuf,siz, "the current module was disabled %d", val);
  2484. return replybuf;
  2485. }
  2486. if (val_led == -1)
  2487. info->led_indicator[val] = !info->led_indicator[val];
  2488. else {
  2489. if (val_led < 0 || val_led > 1) {
  2490. snprintf(replybuf, siz,"invalid LED status: %d, valid value 0|1", val_led);
  2491. return replybuf;
  2492. }
  2493. if (val_led != info->led_indicator[val])
  2494. info->led_indicator[val] = val_led;
  2495. }
  2496. applog(LOG_NOTICE, "%s-%d: Module:%d, LED: %s",
  2497. avalon9->drv->name, avalon9->device_id,
  2498. val, info->led_indicator[val] ? "on" : "off");
  2499. return NULL;
  2500. }
  2501. if (strcasecmp(option, "voltage-level") == 0) {
  2502. if (!setting || !*setting) {
  2503. snprintf(replybuf, siz,"missing voltage-level value");
  2504. return replybuf;
  2505. }
  2506. return set_avalon9_device_voltage_level(avalon9, setting);
  2507. }
  2508. if (strcasecmp(option, "factory") == 0) {
  2509. if (!setting || !*setting) {
  2510. snprintf(replybuf, siz,"missing factory info");
  2511. return replybuf;
  2512. }
  2513. return set_avalon9_factory_info(avalon9, setting);
  2514. }
  2515. if (strcasecmp(option, "reboot") == 0) {
  2516. if (!setting || !*setting) {
  2517. snprintf(replybuf, siz,"missing reboot value");
  2518. return replybuf;
  2519. }
  2520. sscanf(setting, "%d", &val);
  2521. if (val < 1 || val >= AVA9_DEFAULT_MODULARS) {
  2522. snprintf(replybuf, siz,"invalid module_id: %d, valid range 1-%d", val, AVA9_DEFAULT_MODULARS);
  2523. return replybuf;
  2524. }
  2525. info->reboot[val] = true;
  2526. return NULL;
  2527. }
  2528. if (strcasecmp(option, "overclocking") == 0) {
  2529. if (!setting || !*setting) {
  2530. snprintf(replybuf, siz,"missing overclocking info");
  2531. return replybuf;
  2532. }
  2533. return set_avalon9_overclocking_info(avalon9, setting);
  2534. }
  2535. if (strcasecmp(option, "ss-param-en") == 0) {
  2536. if (!setting || !*setting) {
  2537. snprintf(replybuf, siz,"missing ss-param-en value");
  2538. return replybuf;
  2539. }
  2540. return set_avalon9_ss_param_en(avalon9, setting);
  2541. }
  2542. snprintf(replybuf, siz,"Unknown option: %s", option);
  2543. return replybuf;
  2544. }
  2545. static void avalon9_statline_before(char *buf, size_t bufsiz, struct cgpu_info *avalon9)
  2546. {
  2547. struct avalon9_info *info = avalon9->device_data;
  2548. int temp = -273;
  2549. int fanmin = AVA9_DEFAULT_FAN_MAX;
  2550. int i, j, k;
  2551. uint32_t frequency = 0;
  2552. float ghs_sum = 0, mhsmm = 0;
  2553. double pass_num = 0.0, fail_num = 0.0;
  2554. for (i = 1; i < AVA9_DEFAULT_MODULARS; i++) {
  2555. if (!info->enable[i])
  2556. continue;
  2557. if (fanmin >= info->fan_pct[i])
  2558. fanmin = info->fan_pct[i];
  2559. if (temp < get_temp_max(info, i))
  2560. temp = get_temp_max(info, i);
  2561. mhsmm = avalon9_hash_cal(avalon9, i);
  2562. frequency += (mhsmm / (info->asic_count[i] * info->miner_count[i] * 256));
  2563. ghs_sum += (mhsmm / 1000);
  2564. for (j = 0; j < info->miner_count[i]; j++) {
  2565. for (k = 0; k < info->asic_count[i]; k++) {
  2566. pass_num += info->get_asic[i][j][k][0];
  2567. fail_num += info->get_asic[i][j][k][1];
  2568. }
  2569. }
  2570. }
  2571. if (info->mm_count)
  2572. frequency /= info->mm_count;
  2573. tailsprintf(buf, bufsiz, "%4dMhz %.2fGHS %2dC %.2f%% %3d%%", frequency, ghs_sum, temp,
  2574. (fail_num + pass_num) ? fail_num * 100.0 / (fail_num + pass_num) : 0, fanmin);
  2575. }
  2576. struct device_drv avalon9_drv = {
  2577. .drv_id = DRIVER_avalon9,
  2578. .dname = "avalon9",
  2579. .name = "AV9",
  2580. .set_device = avalon9_set_device,
  2581. .get_api_stats = avalon9_api_stats,
  2582. .get_statline_before = avalon9_statline_before,
  2583. .drv_detect = avalon9_detect,
  2584. .thread_prepare = avalon9_prepare,
  2585. .hash_work = hash_driver_work,
  2586. .flush_work = avalon9_sswork_update,
  2587. .update_work = avalon9_sswork_update,
  2588. .scanwork = avalon9_scanhash,
  2589. .max_diff = AVA9_DRV_DIFFMAX,
  2590. .genwork = true,
  2591. };