driver-avalon8.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769
  1. /*
  2. * Copyright 2017 xuzhenxing <xuzhenxing@canaan-creative.com>
  3. * Copyright 2016-2017 Mikeqin <Fengling.Qin@gmail.com>
  4. * Copyright 2016 Con Kolivas <kernel@kolivas.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include <math.h>
  12. #include "config.h"
  13. #include "miner.h"
  14. #include "driver-avalon8.h"
  15. #include "crc.h"
  16. #include "sha2.h"
  17. #include "hexdump.c"
  18. #define get_fan_pwm(v) (AVA8_PWM_MAX - (v) * AVA8_PWM_MAX / 100)
  19. int opt_avalon8_temp_target = AVA8_DEFAULT_TEMP_TARGET;
  20. int opt_avalon8_fan_min = AVA8_DEFAULT_FAN_MIN;
  21. int opt_avalon8_fan_max = AVA8_DEFAULT_FAN_MAX;
  22. int opt_avalon8_voltage_level = AVA8_INVALID_VOLTAGE_LEVEL;
  23. int opt_avalon8_voltage_level_offset = AVA8_DEFAULT_VOLTAGE_LEVEL_OFFSET;
  24. int opt_avalon8_freq[AVA8_DEFAULT_PLL_CNT] =
  25. {
  26. AVA8_DEFAULT_FREQUENCY_IGNORE,
  27. AVA8_DEFAULT_FREQUENCY_IGNORE,
  28. AVA8_DEFAULT_FREQUENCY_IGNORE,
  29. AVA8_DEFAULT_FREQUENCY_IGNORE,
  30. AVA8_DEFAULT_FREQUENCY_IGNORE,
  31. AVA8_DEFAULT_FREQUENCY_IGNORE,
  32. AVA8_DEFAULT_FREQUENCY_IGNORE
  33. };
  34. int opt_avalon8_freq_sel = AVA8_DEFAULT_FREQUENCY_SEL;
  35. int opt_avalon8_polling_delay = AVA8_DEFAULT_POLLING_DELAY;
  36. int opt_avalon8_aucspeed = AVA8_AUC_SPEED;
  37. int opt_avalon8_aucxdelay = AVA8_AUC_XDELAY;
  38. int opt_avalon8_smart_speed = AVA8_DEFAULT_SMART_SPEED;
  39. /*
  40. * smart speed have 2 modes
  41. * 1. auto speed by A3210 chips
  42. * 2. option 1 + adjust by average frequency
  43. */
  44. bool opt_avalon8_iic_detect = AVA8_DEFAULT_IIC_DETECT;
  45. uint32_t opt_avalon8_th_pass = AVA8_DEFAULT_TH_PASS;
  46. uint32_t opt_avalon8_th_fail = AVA8_DEFAULT_TH_FAIL;
  47. uint32_t opt_avalon8_th_init = AVA8_DEFAULT_TH_INIT;
  48. uint32_t opt_avalon8_th_ms = AVA8_DEFAULT_TH_MS;
  49. uint32_t opt_avalon8_th_timeout = AVA8_DEFAULT_TH_TIMEOUT;
  50. uint32_t opt_avalon8_th_add = AVA8_DEFAULT_TH_ADD;
  51. uint32_t opt_avalon8_th_mssel = AVA8_DEFAULT_TH_MSSEL;
  52. uint32_t opt_avalon8_nonce_mask = AVA8_DEFAULT_NONCE_MASK;
  53. uint32_t opt_avalon8_nonce_check = AVA8_DEFAULT_NONCE_CHECK;
  54. uint32_t opt_avalon8_mux_l2h = AVA8_DEFAULT_MUX_L2H;
  55. uint32_t opt_avalon8_mux_h2l = AVA8_DEFAULT_MUX_H2L;
  56. uint32_t opt_avalon8_h2ltime0_spd = AVA8_DEFAULT_H2LTIME0_SPD;
  57. uint32_t opt_avalon8_roll_enable = AVA8_DEFAULT_ROLL_ENABLE;
  58. uint32_t opt_avalon8_spdlow = AVA8_DEFAULT_SPDLOW;
  59. uint32_t opt_avalon8_spdhigh = AVA8_DEFAULT_SPDHIGH;
  60. uint32_t opt_avalon8_lv2_th_ms = AVA8_DEFAULT_LV2_TH_MS;
  61. uint32_t opt_avalon8_lv3_th_ms = AVA8_DEFAULT_LV3_TH_MS;
  62. uint32_t opt_avalon8_lv4_th_ms = AVA8_DEFAULT_LV4_TH_MS;
  63. uint32_t opt_avalon8_lv5_th_ms = AVA8_DEFAULT_LV5_TH_MS;
  64. uint32_t opt_avalon8_lv6_th_ms = AVA8_DEFAULT_LV6_TH_MS;
  65. uint32_t opt_avalon8_lv7_th_ms = AVA8_DEFAULT_LV7_TH_MS;
  66. uint32_t opt_avalon8_lv2_th_add = AVA8_DEFAULT_LV2_TH_ADD;
  67. uint32_t opt_avalon8_lv3_th_add = AVA8_DEFAULT_LV3_TH_ADD;
  68. uint32_t opt_avalon8_lv4_th_add = AVA8_DEFAULT_LV4_TH_ADD;
  69. uint32_t opt_avalon8_lv5_th_add = AVA8_DEFAULT_LV5_TH_ADD;
  70. uint32_t opt_avalon8_lv6_th_add = AVA8_DEFAULT_LV6_TH_ADD;
  71. uint32_t opt_avalon8_lv7_th_add = AVA8_DEFAULT_LV7_TH_ADD;
  72. uint32_t cpm_table[] =
  73. {
  74. 0x00000000,
  75. 0x0c041205,
  76. 0x0c041203,
  77. 0x0c031103,
  78. 0x0c041103,
  79. 0x0c079183,
  80. 0x0c079503,
  81. 0x0c07ed83,
  82. 0x0c040603,
  83. 0x0c06c703,
  84. 0x0c078703,
  85. 0x0c042583,
  86. 0x0c078683,
  87. 0x0c068603,
  88. 0x0c070603,
  89. 0x0c078603,
  90. 0x0c040503,
  91. 0x0c044503,
  92. 0x0c048503,
  93. 0x0c04c503,
  94. 0x0c050503,
  95. 0x0c054503,
  96. 0x0c058503,
  97. 0x0c05c503,
  98. 0x0c060503,
  99. 0x0c064503,
  100. 0x0c068503,
  101. 0x0c06c503,
  102. 0x0c070503,
  103. 0x0c074503,
  104. 0x0c078503,
  105. 0x0c07c503,
  106. 0x0c040483,
  107. 0x0c042483,
  108. 0x0c044483,
  109. 0x0c046483,
  110. 0x0c048483,
  111. 0x0c04a483,
  112. 0x0c04c483,
  113. 0x0c04e483,
  114. 0x0c050483,
  115. 0x0c052483,
  116. 0x0c054483,
  117. 0x0c056483,
  118. 0x0c058483,
  119. 0x0c05a483,
  120. 0x0c05c483,
  121. 0x0c05e483,
  122. 0x0c060483,
  123. 0x0c062483,
  124. 0x0c064483,
  125. 0x0c066483,
  126. 0x0c068483,
  127. 0x0c06a483,
  128. 0x0c06c483,
  129. 0x0c06e483
  130. };
  131. struct avalon8_dev_description avalon8_dev_table[] = {
  132. {
  133. "921",
  134. 921,
  135. 4,
  136. 26,
  137. AVA8_MM851_VIN_ADC_RATIO,
  138. AVA8_MM851_VOUT_ADC_RATIO,
  139. -2,
  140. {
  141. AVA8_DEFAULT_FREQUENCY_0M,
  142. AVA8_DEFAULT_FREQUENCY_0M,
  143. AVA8_DEFAULT_FREQUENCY_0M,
  144. AVA8_DEFAULT_FREQUENCY_0M,
  145. AVA8_DEFAULT_FREQUENCY_750M,
  146. AVA8_DEFAULT_FREQUENCY_775M,
  147. AVA8_DEFAULT_FREQUENCY_800M
  148. }
  149. }
  150. };
  151. static uint32_t api_get_cpm(uint32_t freq)
  152. {
  153. return cpm_table[freq / 25];
  154. }
  155. static uint32_t encode_voltage(int volt_level)
  156. {
  157. if (volt_level > AVA8_DEFAULT_VOLTAGE_LEVEL_MAX)
  158. volt_level = AVA8_DEFAULT_VOLTAGE_LEVEL_MAX;
  159. else if (volt_level < AVA8_DEFAULT_VOLTAGE_LEVEL_MIN)
  160. volt_level = AVA8_DEFAULT_VOLTAGE_LEVEL_MIN;
  161. if (volt_level < 0)
  162. return 0x8080 | (-volt_level);
  163. return 0x8000 | volt_level;
  164. }
  165. static uint32_t decode_voltage(struct avalon8_info *info, int modular_id, uint32_t volt)
  166. {
  167. return (volt * info->vout_adc_ratio[modular_id] / info->asic_count[modular_id] / 100);
  168. }
  169. static uint16_t decode_vin(struct avalon8_info *info, int modular_id, uint16_t volt)
  170. {
  171. return (volt * info->vin_adc_ratio[modular_id] / 1000);
  172. }
  173. static double decode_pvt_temp(uint16_t pvt_code)
  174. {
  175. double g = 60.0;
  176. double h = 200.0;
  177. double cal5 = 4094.0;
  178. double j = -0.1;
  179. double fclkm = 6.25;
  180. /* Mode2 temperature equation */
  181. return g + h * (pvt_code / cal5 - 0.5) + j * fclkm;
  182. }
  183. static uint32_t decode_pvt_volt(uint16_t volt)
  184. {
  185. double vref = 1.20;
  186. double r = 16384.0; /* 2 ** 14 */
  187. double c;
  188. c = vref / 5.0 * (6 * (volt - 0.5) / r - 1.0);
  189. if (c < 0)
  190. c = 0;
  191. return c * 1000;
  192. }
  193. #define SERIESRESISTOR 10000
  194. #define THERMISTORNOMINAL 10000
  195. #define BCOEFFICIENT 3500
  196. #define TEMPERATURENOMINAL 25
  197. float decode_auc_temp(int value)
  198. {
  199. float ret, resistance;
  200. if (!((value > 0) && (value < 33000)))
  201. return -273;
  202. resistance = (3.3 * 10000 / value) - 1;
  203. resistance = SERIESRESISTOR / resistance;
  204. ret = resistance / THERMISTORNOMINAL;
  205. ret = logf(ret);
  206. ret /= BCOEFFICIENT;
  207. ret += 1.0 / (TEMPERATURENOMINAL + 273.15);
  208. ret = 1.0 / ret;
  209. ret -= 273.15;
  210. return ret;
  211. }
  212. #define UNPACK32(x, str) \
  213. { \
  214. *((str) + 3) = (uint8_t) ((x) ); \
  215. *((str) + 2) = (uint8_t) ((x) >> 8); \
  216. *((str) + 1) = (uint8_t) ((x) >> 16); \
  217. *((str) + 0) = (uint8_t) ((x) >> 24); \
  218. }
  219. static inline void sha256_prehash(const unsigned char *message, unsigned int len, unsigned char *digest)
  220. {
  221. int i;
  222. sha256_ctx ctx;
  223. sha256_init(&ctx);
  224. sha256_update(&ctx, message, len);
  225. for (i = 0; i < 8; i++)
  226. UNPACK32(ctx.h[i], &digest[i << 2]);
  227. }
  228. char *set_avalon8_fan(char *arg)
  229. {
  230. int val1, val2, ret;
  231. ret = sscanf(arg, "%d-%d", &val1, &val2);
  232. if (ret < 1)
  233. return "No value passed to avalon8-fan";
  234. if (ret == 1)
  235. val2 = val1;
  236. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
  237. return "Invalid value passed to avalon8-fan";
  238. opt_avalon8_fan_min = val1;
  239. opt_avalon8_fan_max = val2;
  240. return NULL;
  241. }
  242. char *set_avalon8_freq(char *arg)
  243. {
  244. int val[AVA8_DEFAULT_PLL_CNT];
  245. char *colon, *data;
  246. int i;
  247. if (!(*arg))
  248. return NULL;
  249. data = arg;
  250. memset(val, 0, sizeof(val));
  251. for (i = 0; i < AVA8_DEFAULT_PLL_CNT; i++) {
  252. colon = strchr(data, ':');
  253. if (colon)
  254. *(colon++) = '\0';
  255. else {
  256. /* last value */
  257. if (*data) {
  258. val[i] = atoi(data);
  259. if (val[i] > AVA8_DEFAULT_FREQUENCY_MAX)
  260. return "Invalid value passed to avalon8-freq";
  261. }
  262. break;
  263. }
  264. if (*data) {
  265. val[i] = atoi(data);
  266. if (val[i] > AVA8_DEFAULT_FREQUENCY_MAX)
  267. return "Invalid value passed to avalon8-freq";
  268. }
  269. data = colon;
  270. }
  271. for (i = 0; i < AVA8_DEFAULT_PLL_CNT; i++)
  272. opt_avalon8_freq[i] = val[i];
  273. return NULL;
  274. }
  275. char *set_avalon8_voltage_level(char *arg)
  276. {
  277. int val, ret;
  278. ret = sscanf(arg, "%d", &val);
  279. if (ret < 1)
  280. return "No value passed to avalon8-voltage-level";
  281. if (val < AVA8_DEFAULT_VOLTAGE_LEVEL_MIN || val > AVA8_DEFAULT_VOLTAGE_LEVEL_MAX)
  282. return "Invalid value passed to avalon8-voltage-level";
  283. opt_avalon8_voltage_level = val;
  284. return NULL;
  285. }
  286. char *set_avalon8_voltage_level_offset(char *arg)
  287. {
  288. int val, ret;
  289. ret = sscanf(arg, "%d", &val);
  290. if (ret < 1)
  291. return "No value passed to avalon8-voltage-level-offset";
  292. if (val < AVA8_DEFAULT_VOLTAGE_LEVEL_OFFSET_MIN || val > AVA8_DEFAULT_VOLTAGE_LEVEL_OFFSET_MAX)
  293. return "Invalid value passed to avalon8-voltage-level-offset";
  294. opt_avalon8_voltage_level_offset = val;
  295. return NULL;
  296. }
  297. static int avalon8_init_pkg(struct avalon8_pkg *pkg, uint8_t type, uint8_t idx, uint8_t cnt)
  298. {
  299. unsigned short crc;
  300. pkg->head[0] = AVA8_H1;
  301. pkg->head[1] = AVA8_H2;
  302. pkg->type = type;
  303. pkg->opt = 0;
  304. pkg->idx = idx;
  305. pkg->cnt = cnt;
  306. crc = crc16(pkg->data, AVA8_P_DATA_LEN);
  307. pkg->crc[0] = (crc & 0xff00) >> 8;
  308. pkg->crc[1] = crc & 0xff;
  309. return 0;
  310. }
  311. static int job_idcmp(uint8_t *job_id, char *pool_job_id)
  312. {
  313. int job_id_len;
  314. unsigned short crc, crc_expect;
  315. if (!pool_job_id)
  316. return 1;
  317. job_id_len = strlen(pool_job_id);
  318. crc_expect = crc16((unsigned char *)pool_job_id, job_id_len);
  319. crc = job_id[0] << 8 | job_id[1];
  320. if (crc_expect == crc)
  321. return 0;
  322. applog(LOG_DEBUG, "avalon8: job_id doesn't match! [%04x:%04x (%s)]",
  323. crc, crc_expect, pool_job_id);
  324. return 1;
  325. }
  326. static inline int get_temp_max(struct avalon8_info *info, int addr)
  327. {
  328. int i, j;
  329. int max = -273;
  330. for (i = 0; i < info->miner_count[addr]; i++) {
  331. for (j = 0; j < info->asic_count[addr]; j++) {
  332. if (info->temp[addr][i][j] > max)
  333. max = info->temp[addr][i][j];
  334. }
  335. }
  336. if (max < info->temp_mm[addr])
  337. max = info->temp_mm[addr];
  338. return max;
  339. }
  340. /* Use a PID-like feedback mechanism for optimal temperature and fan speed */
  341. static inline uint32_t adjust_fan(struct avalon8_info *info, int id)
  342. {
  343. int t, tdiff, delta;
  344. uint32_t pwm;
  345. time_t now_t;
  346. now_t = time(NULL);
  347. t = get_temp_max(info, id);
  348. tdiff = t - info->temp_last_max[id];
  349. if (!tdiff && now_t < info->last_temp_time[id] + AVA8_DEFAULT_FAN_INTERVAL)
  350. goto out;
  351. info->last_temp_time[id] = now_t;
  352. delta = t - info->temp_target[id];
  353. /* Check for init value and ignore it */
  354. if (unlikely(info->temp_last_max[id] == -273))
  355. tdiff = 0;
  356. info->temp_last_max[id] = t;
  357. if (t >= info->temp_overheat[id]) {
  358. /* Hit the overheat temperature limit */
  359. if (info->fan_pct[id] < opt_avalon8_fan_max) {
  360. applog(LOG_WARNING, "Overheat detected on AV8-%d, increasing fan to max", id);
  361. info->fan_pct[id] = opt_avalon8_fan_max;
  362. }
  363. } else if (delta > 0) {
  364. /* Over target temperature. */
  365. /* Is the temp already coming down */
  366. if (tdiff < 0)
  367. goto out;
  368. /* Adjust fanspeed by temperature over and any further rise */
  369. info->fan_pct[id] += opt_avalon8_fan_max; /* info->fan_pct[id] += delta + tdiff; */
  370. } else {
  371. /* Below target temperature */
  372. int diff = delta; /* int diff = tdiff; Sth. wrong with tdiff? It will keep lower than target */
  373. if (tdiff > 0) {
  374. int divisor = -delta / AVA8_DEFAULT_TEMP_HYSTERESIS + 1;
  375. /* Adjust fanspeed by temperature change proportional to
  376. * diff from optimal. */
  377. diff /= divisor;
  378. } else {
  379. /* Is the temp below optimal and unchanging, gently lower speed */
  380. if (t < info->temp_target[id] - AVA8_DEFAULT_TEMP_HYSTERESIS && !tdiff)
  381. diff -= 1;
  382. diff = (delta == 0) ? 0 : diff;
  383. }
  384. info->fan_pct[id] += diff;
  385. }
  386. if (info->fan_pct[id] > opt_avalon8_fan_max)
  387. info->fan_pct[id] = opt_avalon8_fan_max;
  388. else if (info->fan_pct[id] < opt_avalon8_fan_min)
  389. info->fan_pct[id] = opt_avalon8_fan_min;
  390. out:
  391. pwm = get_fan_pwm(info->fan_pct[id]);
  392. if (info->cutoff[id])
  393. pwm = get_fan_pwm(opt_avalon8_fan_max);
  394. applog(LOG_DEBUG, "[%d], Adjust_fan: %dC-%d%%(%03x)", id, t, info->fan_pct[id], pwm);
  395. return pwm;
  396. }
  397. static int decode_pkg(struct cgpu_info *avalon8, struct avalon8_ret *ar, int modular_id)
  398. {
  399. struct avalon8_info *info = avalon8->device_data;
  400. struct pool *pool, *real_pool;
  401. struct pool *pool_stratum0 = &info->pool0;
  402. struct pool *pool_stratum1 = &info->pool1;
  403. struct pool *pool_stratum2 = &info->pool2;
  404. struct thr_info *thr = NULL;
  405. unsigned short expected_crc;
  406. unsigned short actual_crc;
  407. uint32_t nonce, nonce2, ntime, miner, chip_id, tmp;
  408. uint8_t job_id[2];
  409. int pool_no;
  410. uint32_t i, j;
  411. int64_t last_diff1;
  412. uint16_t vin;
  413. if (likely(avalon8->thr))
  414. thr = avalon8->thr[0];
  415. if (ar->head[0] != AVA8_H1 && ar->head[1] != AVA8_H2) {
  416. applog(LOG_DEBUG, "%s-%d-%d: H1 %02x, H2 %02x",
  417. avalon8->drv->name, avalon8->device_id, modular_id,
  418. ar->head[0], ar->head[1]);
  419. hexdump(ar->data, 32);
  420. return 1;
  421. }
  422. expected_crc = crc16(ar->data, AVA8_P_DATA_LEN);
  423. actual_crc = ((ar->crc[0] & 0xff) << 8) | (ar->crc[1] & 0xff);
  424. if (expected_crc != actual_crc) {
  425. applog(LOG_DEBUG, "%s-%d-%d: %02x: expected crc(%04x), actual_crc(%04x)",
  426. avalon8->drv->name, avalon8->device_id, modular_id,
  427. ar->type, expected_crc, actual_crc);
  428. return 1;
  429. }
  430. switch(ar->type) {
  431. case AVA8_P_NONCE:
  432. applog(LOG_DEBUG, "%s-%d-%d: AVA8_P_NONCE", avalon8->drv->name, avalon8->device_id, modular_id);
  433. memcpy(&miner, ar->data + 0, 4);
  434. memcpy(&nonce2, ar->data + 4, 4);
  435. memcpy(&ntime, ar->data + 8, 4);
  436. memcpy(&nonce, ar->data + 12, 4);
  437. job_id[0] = ar->data[16];
  438. job_id[1] = ar->data[17];
  439. pool_no = (ar->data[18] | (ar->data[19] << 8));
  440. miner = be32toh(miner);
  441. chip_id = (miner >> 16) & 0xffff;
  442. miner &= 0xffff;
  443. ntime = be32toh(ntime);
  444. if (miner >= info->miner_count[modular_id] ||
  445. pool_no >= total_pools || pool_no < 0) {
  446. applog(LOG_DEBUG, "%s-%d-%d: Wrong miner/pool_no %d/%d",
  447. avalon8->drv->name, avalon8->device_id, modular_id,
  448. miner, pool_no);
  449. break;
  450. }
  451. nonce2 = be32toh(nonce2);
  452. nonce = be32toh(nonce);
  453. if (ntime > info->max_ntime)
  454. info->max_ntime = ntime;
  455. 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")]",
  456. avalon8->drv->name, avalon8->device_id, modular_id,
  457. pool_no, nonce2, nonce, ntime, info->max_ntime,
  458. miner, chip_id, nonce & 0x7f,
  459. info->chip_matching_work[modular_id][miner][0],
  460. info->chip_matching_work[modular_id][miner][1],
  461. info->chip_matching_work[modular_id][miner][2],
  462. info->chip_matching_work[modular_id][miner][3]);
  463. real_pool = pool = pools[pool_no];
  464. if (job_idcmp(job_id, pool->swork.job_id)) {
  465. if (!job_idcmp(job_id, pool_stratum0->swork.job_id)) {
  466. applog(LOG_DEBUG, "%s-%d-%d: Match to previous stratum0! (%s)",
  467. avalon8->drv->name, avalon8->device_id, modular_id,
  468. pool_stratum0->swork.job_id);
  469. pool = pool_stratum0;
  470. } else if (!job_idcmp(job_id, pool_stratum1->swork.job_id)) {
  471. applog(LOG_DEBUG, "%s-%d-%d: Match to previous stratum1! (%s)",
  472. avalon8->drv->name, avalon8->device_id, modular_id,
  473. pool_stratum1->swork.job_id);
  474. pool = pool_stratum1;
  475. } else if (!job_idcmp(job_id, pool_stratum2->swork.job_id)) {
  476. applog(LOG_DEBUG, "%s-%d-%d: Match to previous stratum2! (%s)",
  477. avalon8->drv->name, avalon8->device_id, modular_id,
  478. pool_stratum2->swork.job_id);
  479. pool = pool_stratum2;
  480. } else {
  481. applog(LOG_ERR, "%s-%d-%d: Cannot match to any stratum! (%s)",
  482. avalon8->drv->name, avalon8->device_id, modular_id,
  483. pool->swork.job_id);
  484. if (likely(thr))
  485. inc_hw_errors(thr);
  486. info->hw_works_i[modular_id][miner]++;
  487. break;
  488. }
  489. }
  490. /* Can happen during init sequence before add_cgpu */
  491. if (unlikely(!thr))
  492. break;
  493. last_diff1 = avalon8->diff1;
  494. if (!submit_nonce2_nonce(thr, pool, real_pool, nonce2, nonce, ntime))
  495. info->hw_works_i[modular_id][miner]++;
  496. else {
  497. info->diff1[modular_id] += (avalon8->diff1 - last_diff1);
  498. info->chip_matching_work[modular_id][miner][chip_id]++;
  499. }
  500. break;
  501. case AVA8_P_STATUS:
  502. applog(LOG_DEBUG, "%s-%d-%d: AVA8_P_STATUS", avalon8->drv->name, avalon8->device_id, modular_id);
  503. hexdump(ar->data, 32);
  504. memcpy(&tmp, ar->data, 4);
  505. tmp = be32toh(tmp);
  506. info->temp_mm[modular_id] = tmp;
  507. avalon8->temp = decode_auc_temp(info->auc_sensor);
  508. memcpy(&tmp, ar->data + 4, 4);
  509. tmp = be32toh(tmp);
  510. info->fan_cpm[modular_id] = tmp;
  511. memcpy(&tmp, ar->data + 8, 4);
  512. info->local_works_i[modular_id][ar->idx] += be32toh(tmp);
  513. memcpy(&tmp, ar->data + 12, 4);
  514. info->hw_works_i[modular_id][ar->idx] += be32toh(tmp);
  515. memcpy(&tmp, ar->data + 16, 4);
  516. info->error_code[modular_id][ar->idx] = be32toh(tmp);
  517. memcpy(&tmp, ar->data + 20, 4);
  518. info->error_code[modular_id][ar->cnt] = be32toh(tmp);
  519. memcpy(&tmp, ar->data + 24, 4);
  520. info->error_crc[modular_id][ar->idx] += be32toh(tmp);
  521. break;
  522. case AVA8_P_STATUS_PMU:
  523. /* TODO: decode ntc led from PMU */
  524. applog(LOG_DEBUG, "%s-%d-%d: AVA8_P_STATUS_PMU", avalon8->drv->name, avalon8->device_id, modular_id);
  525. info->power_good[modular_id] = ar->data[16];
  526. for (i = 0; i < AVA8_DEFAULT_PMU_CNT; i++) {
  527. memcpy(&info->pmu_version[modular_id][i], ar->data + 24 + (i * 4), 4);
  528. info->pmu_version[modular_id][i][4] = '\0';
  529. }
  530. for (i = 0; i < info->miner_count[modular_id]; i++) {
  531. memcpy(&vin, ar->data + 8 + i * 2, 2);
  532. info->get_vin[modular_id][i] = decode_vin(info, modular_id, be16toh(vin));
  533. }
  534. break;
  535. case AVA8_P_STATUS_VOLT:
  536. applog(LOG_DEBUG, "%s-%d-%d: AVA8_P_STATUS_VOLT", avalon8->drv->name, avalon8->device_id, modular_id);
  537. for (i = 0; i < info->miner_count[modular_id]; i++) {
  538. memcpy(&tmp, ar->data + i * 4, 4);
  539. info->get_voltage[modular_id][i] = decode_voltage(info, modular_id, be32toh(tmp));
  540. }
  541. break;
  542. case AVA8_P_STATUS_PLL:
  543. applog(LOG_DEBUG, "%s-%d-%d: AVA8_P_STATUS_PLL", avalon8->drv->name, avalon8->device_id, modular_id);
  544. if (ar->opt) {
  545. for (i = 0; i < AVA8_DEFAULT_PLL_CNT; i++) {
  546. memcpy(&tmp, ar->data + i * 4, 4);
  547. info->get_pll[modular_id][ar->idx][i] = be32toh(tmp);
  548. }
  549. } else {
  550. for (i = 0; i < AVA8_DEFAULT_PLL_CNT; i++) {
  551. memcpy(&tmp, ar->data + i * 4, 4);
  552. info->get_frequency[modular_id][ar->idx][i] = be32toh(tmp);
  553. }
  554. }
  555. break;
  556. case AVA8_P_STATUS_PVT:
  557. applog(LOG_DEBUG, "%s-%d-%d: AVA8_P_STATUS_PVT", avalon8->drv->name, avalon8->device_id, modular_id);
  558. if (!info->asic_count[modular_id])
  559. break;
  560. if (ar->idx < info->asic_count[modular_id]) {
  561. for (i = 0; i < info->miner_count[modular_id]; i++) {
  562. memcpy(&tmp, ar->data + i * 4, 2);
  563. tmp = be16toh(tmp);
  564. info->temp[modular_id][i][ar->idx] = decode_pvt_temp(tmp);
  565. memcpy(&tmp, ar->data + i * 4 + 2, 2);
  566. tmp = be16toh(tmp);
  567. info->core_volt[modular_id][i][ar->idx] = decode_pvt_volt(tmp);
  568. }
  569. }
  570. break;
  571. case AVA8_P_STATUS_ASIC:
  572. {
  573. int miner_id;
  574. int asic_id;
  575. uint16_t freq;
  576. if (!info->asic_count[modular_id])
  577. break;
  578. miner_id = ar->idx / info->asic_count[modular_id];
  579. asic_id = ar->idx % info->asic_count[modular_id];
  580. applog(LOG_DEBUG, "%s-%d-%d: AVA8_P_STATUS_ASIC %d-%d",
  581. avalon8->drv->name, avalon8->device_id, modular_id,
  582. miner_id, asic_id);
  583. memcpy(&tmp, ar->data + 0, 4);
  584. if (tmp)
  585. info->get_asic[modular_id][miner_id][asic_id][0] = be32toh(tmp);
  586. memcpy(&tmp, ar->data + 4, 4);
  587. if (tmp)
  588. info->get_asic[modular_id][miner_id][asic_id][1] = be32toh(tmp);
  589. for (i = 0; i < AVA8_DEFAULT_PLL_CNT; i++) {
  590. memcpy(&tmp, ar->data + 8 + i * 2, 2);
  591. info->get_asic[modular_id][miner_id][asic_id][2 + i] = be16toh(tmp);
  592. }
  593. }
  594. break;
  595. case AVA8_P_STATUS_FAC:
  596. applog(LOG_DEBUG, "%s-%d-%d: AVA8_P_STATUS_FAC", avalon8->drv->name, avalon8->device_id, modular_id);
  597. info->factory_info[0] = ar->data[0];
  598. break;
  599. case AVA8_P_STATUS_OC:
  600. applog(LOG_DEBUG, "%s-%d-%d: AVA8_P_STATUS_OC", avalon8->drv->name, avalon8->device_id, modular_id);
  601. info->overclocking_info[0] = ar->data[0];
  602. break;
  603. default:
  604. applog(LOG_DEBUG, "%s-%d-%d: Unknown response %x", avalon8->drv->name, avalon8->device_id, modular_id, ar->type);
  605. break;
  606. }
  607. return 0;
  608. }
  609. /*
  610. # IIC packet format: length[1]+transId[1]+sesId[1]+req[1]+data[60]
  611. # length: 4+len(data)
  612. # transId: 0
  613. # sesId: 0
  614. # req: checkout the header file
  615. # data:
  616. # INIT: clock_rate[4] + reserved[4] + payload[52]
  617. # XFER: txSz[1]+rxSz[1]+options[1]+slaveAddr[1] + payload[56]
  618. */
  619. static int avalon8_auc_init_pkg(uint8_t *iic_pkg, struct avalon8_iic_info *iic_info, uint8_t *buf, int wlen, int rlen)
  620. {
  621. memset(iic_pkg, 0, AVA8_AUC_P_SIZE);
  622. switch (iic_info->iic_op) {
  623. case AVA8_IIC_INIT:
  624. iic_pkg[0] = 12; /* 4 bytes IIC header + 4 bytes speed + 4 bytes xfer delay */
  625. iic_pkg[3] = AVA8_IIC_INIT;
  626. iic_pkg[4] = iic_info->iic_param.aucParam[0] & 0xff;
  627. iic_pkg[5] = (iic_info->iic_param.aucParam[0] >> 8) & 0xff;
  628. iic_pkg[6] = (iic_info->iic_param.aucParam[0] >> 16) & 0xff;
  629. iic_pkg[7] = iic_info->iic_param.aucParam[0] >> 24;
  630. iic_pkg[8] = iic_info->iic_param.aucParam[1] & 0xff;
  631. iic_pkg[9] = (iic_info->iic_param.aucParam[1] >> 8) & 0xff;
  632. iic_pkg[10] = (iic_info->iic_param.aucParam[1] >> 16) & 0xff;
  633. iic_pkg[11] = iic_info->iic_param.aucParam[1] >> 24;
  634. break;
  635. case AVA8_IIC_XFER:
  636. iic_pkg[0] = 8 + wlen;
  637. iic_pkg[3] = AVA8_IIC_XFER;
  638. iic_pkg[4] = wlen;
  639. iic_pkg[5] = rlen;
  640. iic_pkg[7] = iic_info->iic_param.slave_addr;
  641. if (buf && wlen)
  642. memcpy(iic_pkg + 8, buf, wlen);
  643. break;
  644. case AVA8_IIC_RESET:
  645. case AVA8_IIC_DEINIT:
  646. case AVA8_IIC_INFO:
  647. iic_pkg[0] = 4;
  648. iic_pkg[3] = iic_info->iic_op;
  649. break;
  650. default:
  651. break;
  652. }
  653. return 0;
  654. }
  655. static int avalon8_iic_xfer(struct cgpu_info *avalon8, uint8_t slave_addr,
  656. uint8_t *wbuf, int wlen,
  657. uint8_t *rbuf, int rlen)
  658. {
  659. struct avalon8_info *info = avalon8->device_data;
  660. struct i2c_ctx *pctx = NULL;
  661. int err = 1;
  662. bool ret = false;
  663. pctx = info->i2c_slaves[slave_addr];
  664. if (!pctx) {
  665. applog(LOG_ERR, "%s-%d: IIC xfer i2c slaves null!", avalon8->drv->name, avalon8->device_id);
  666. goto out;
  667. }
  668. if (wbuf) {
  669. ret = pctx->write_raw(pctx, wbuf, wlen);
  670. if (!ret) {
  671. applog(LOG_DEBUG, "%s-%d: IIC xfer write raw failed!", avalon8->drv->name, avalon8->device_id);
  672. goto out;
  673. }
  674. }
  675. cgsleep_ms(5);
  676. if (rbuf) {
  677. ret = pctx->read_raw(pctx, rbuf, rlen);
  678. if (!ret) {
  679. applog(LOG_DEBUG, "%s-%d: IIC xfer read raw failed!", avalon8->drv->name, avalon8->device_id);
  680. hexdump(rbuf, rlen);
  681. goto out;
  682. }
  683. }
  684. return 0;
  685. out:
  686. return err;
  687. }
  688. static int avalon8_auc_xfer(struct cgpu_info *avalon8,
  689. uint8_t *wbuf, int wlen, int *write,
  690. uint8_t *rbuf, int rlen, int *read)
  691. {
  692. int err = -1;
  693. if (unlikely(avalon8->usbinfo.nodev))
  694. goto out;
  695. usb_buffer_clear(avalon8);
  696. err = usb_write(avalon8, (char *)wbuf, wlen, write, C_AVA8_WRITE);
  697. if (err || *write != wlen) {
  698. applog(LOG_DEBUG, "%s-%d: AUC xfer %d, w(%d-%d)!", avalon8->drv->name, avalon8->device_id, err, wlen, *write);
  699. usb_nodev(avalon8);
  700. goto out;
  701. }
  702. cgsleep_ms(opt_avalon8_aucxdelay / 4800 + 1);
  703. rlen += 4; /* Add 4 bytes IIC header */
  704. err = usb_read(avalon8, (char *)rbuf, rlen, read, C_AVA8_READ);
  705. if (err || *read != rlen || *read != rbuf[0]) {
  706. applog(LOG_DEBUG, "%s-%d: AUC xfer %d, r(%d-%d-%d)!", avalon8->drv->name, avalon8->device_id, err, rlen - 4, *read, rbuf[0]);
  707. hexdump(rbuf, rlen);
  708. return -1;
  709. }
  710. *read = rbuf[0] - 4; /* Remove 4 bytes IIC header */
  711. out:
  712. return err;
  713. }
  714. static int avalon8_auc_init(struct cgpu_info *avalon8, char *ver)
  715. {
  716. struct avalon8_iic_info iic_info;
  717. int err, wlen, rlen;
  718. uint8_t wbuf[AVA8_AUC_P_SIZE];
  719. uint8_t rbuf[AVA8_AUC_P_SIZE];
  720. if (unlikely(avalon8->usbinfo.nodev))
  721. return 1;
  722. /* Try to clean the AUC buffer */
  723. usb_buffer_clear(avalon8);
  724. err = usb_read(avalon8, (char *)rbuf, AVA8_AUC_P_SIZE, &rlen, C_AVA8_READ);
  725. applog(LOG_DEBUG, "%s-%d: AUC usb_read %d, %d!", avalon8->drv->name, avalon8->device_id, err, rlen);
  726. hexdump(rbuf, AVA8_AUC_P_SIZE);
  727. /* Reset */
  728. iic_info.iic_op = AVA8_IIC_RESET;
  729. rlen = 0;
  730. avalon8_auc_init_pkg(wbuf, &iic_info, NULL, 0, rlen);
  731. memset(rbuf, 0, AVA8_AUC_P_SIZE);
  732. err = avalon8_auc_xfer(avalon8, wbuf, AVA8_AUC_P_SIZE, &wlen, rbuf, rlen, &rlen);
  733. if (err) {
  734. applog(LOG_ERR, "%s-%d: Failed to reset Avalon USB2IIC Converter", avalon8->drv->name, avalon8->device_id);
  735. return 1;
  736. }
  737. /* Deinit */
  738. iic_info.iic_op = AVA8_IIC_DEINIT;
  739. rlen = 0;
  740. avalon8_auc_init_pkg(wbuf, &iic_info, NULL, 0, rlen);
  741. memset(rbuf, 0, AVA8_AUC_P_SIZE);
  742. err = avalon8_auc_xfer(avalon8, wbuf, AVA8_AUC_P_SIZE, &wlen, rbuf, rlen, &rlen);
  743. if (err) {
  744. applog(LOG_ERR, "%s-%d: Failed to deinit Avalon USB2IIC Converter", avalon8->drv->name, avalon8->device_id);
  745. return 1;
  746. }
  747. /* Init */
  748. iic_info.iic_op = AVA8_IIC_INIT;
  749. iic_info.iic_param.aucParam[0] = opt_avalon8_aucspeed;
  750. iic_info.iic_param.aucParam[1] = opt_avalon8_aucxdelay;
  751. rlen = AVA8_AUC_VER_LEN;
  752. avalon8_auc_init_pkg(wbuf, &iic_info, NULL, 0, rlen);
  753. memset(rbuf, 0, AVA8_AUC_P_SIZE);
  754. err = avalon8_auc_xfer(avalon8, wbuf, AVA8_AUC_P_SIZE, &wlen, rbuf, rlen, &rlen);
  755. if (err) {
  756. applog(LOG_ERR, "%s-%d: Failed to init Avalon USB2IIC Converter", avalon8->drv->name, avalon8->device_id);
  757. return 1;
  758. }
  759. hexdump(rbuf, AVA8_AUC_P_SIZE);
  760. memcpy(ver, rbuf + 4, AVA8_AUC_VER_LEN);
  761. ver[AVA8_AUC_VER_LEN] = '\0';
  762. applog(LOG_DEBUG, "%s-%d: USB2IIC Converter version: %s!", avalon8->drv->name, avalon8->device_id, ver);
  763. return 0;
  764. }
  765. static int avalon8_auc_getinfo(struct cgpu_info *avalon8)
  766. {
  767. struct avalon8_iic_info iic_info;
  768. int err, wlen, rlen;
  769. uint8_t wbuf[AVA8_AUC_P_SIZE];
  770. uint8_t rbuf[AVA8_AUC_P_SIZE];
  771. uint8_t *pdata = rbuf + 4;
  772. uint16_t adc_val;
  773. struct avalon8_info *info = avalon8->device_data;
  774. iic_info.iic_op = AVA8_IIC_INFO;
  775. /*
  776. * Device info: (9 bytes)
  777. * tempadc(2), reqRdIndex, reqWrIndex,
  778. * respRdIndex, respWrIndex, tx_flags, state
  779. */
  780. rlen = 7;
  781. avalon8_auc_init_pkg(wbuf, &iic_info, NULL, 0, rlen);
  782. memset(rbuf, 0, AVA8_AUC_P_SIZE);
  783. err = avalon8_auc_xfer(avalon8, wbuf, AVA8_AUC_P_SIZE, &wlen, rbuf, rlen, &rlen);
  784. if (err) {
  785. applog(LOG_ERR, "%s-%d: AUC Failed to get info ", avalon8->drv->name, avalon8->device_id);
  786. return 1;
  787. }
  788. applog(LOG_DEBUG, "%s-%d: AUC tempADC(%03d), reqcnt(%d), respcnt(%d), txflag(%d), state(%d)",
  789. avalon8->drv->name, avalon8->device_id,
  790. pdata[1] << 8 | pdata[0],
  791. pdata[2],
  792. pdata[3],
  793. pdata[5] << 8 | pdata[4],
  794. pdata[6]);
  795. adc_val = pdata[1] << 8 | pdata[0];
  796. info->auc_sensor = 3.3 * adc_val * 10000 / 1023;
  797. return 0;
  798. }
  799. static int avalon8_iic_xfer_pkg(struct cgpu_info *avalon8, uint8_t slave_addr,
  800. const struct avalon8_pkg *pkg, struct avalon8_ret *ret)
  801. {
  802. struct avalon8_iic_info iic_info;
  803. int err, wcnt, rcnt, rlen = 0;
  804. uint8_t wbuf[AVA8_AUC_P_SIZE];
  805. uint8_t rbuf[AVA8_AUC_P_SIZE];
  806. struct avalon8_info *info = avalon8->device_data;
  807. if (ret)
  808. rlen = AVA8_READ_SIZE;
  809. if (info->connecter == AVA8_CONNECTER_AUC) {
  810. if (unlikely(avalon8->usbinfo.nodev))
  811. return AVA8_SEND_ERROR;
  812. iic_info.iic_op = AVA8_IIC_XFER;
  813. iic_info.iic_param.slave_addr = slave_addr;
  814. avalon8_auc_init_pkg(wbuf, &iic_info, (uint8_t *)pkg, AVA8_WRITE_SIZE, rlen);
  815. err = avalon8_auc_xfer(avalon8, wbuf, wbuf[0], &wcnt, rbuf, rlen, &rcnt);
  816. if ((pkg->type != AVA8_P_DETECT) && err == -7 && !rcnt && rlen) {
  817. avalon8_auc_init_pkg(wbuf, &iic_info, NULL, 0, rlen);
  818. err = avalon8_auc_xfer(avalon8, wbuf, wbuf[0], &wcnt, rbuf, rlen, &rcnt);
  819. applog(LOG_DEBUG, "%s-%d-%d: AUC read again!(type:0x%x, err:%d)", avalon8->drv->name, avalon8->device_id, slave_addr, pkg->type, err);
  820. }
  821. if (err || rcnt != rlen) {
  822. if (info->xfer_err_cnt++ == 100) {
  823. applog(LOG_DEBUG, "%s-%d-%d: AUC xfer_err_cnt reach err = %d, rcnt = %d, rlen = %d",
  824. avalon8->drv->name, avalon8->device_id, slave_addr,
  825. err, rcnt, rlen);
  826. cgsleep_ms(5 * 1000); /* Wait MM reset */
  827. if (avalon8_auc_init(avalon8, info->auc_version)) {
  828. applog(LOG_WARNING, "%s-%d: Failed to re-init auc, unplugging for new hotplug",
  829. avalon8->drv->name, avalon8->device_id);
  830. usb_nodev(avalon8);
  831. }
  832. }
  833. return AVA8_SEND_ERROR;
  834. }
  835. if (ret)
  836. memcpy((char *)ret, rbuf + 4, AVA8_READ_SIZE);
  837. info->xfer_err_cnt = 0;
  838. }
  839. if (info->connecter == AVA8_CONNECTER_IIC) {
  840. err = avalon8_iic_xfer(avalon8, slave_addr, (uint8_t *)pkg, AVA8_WRITE_SIZE, (uint8_t *)ret, AVA8_READ_SIZE);
  841. if ((pkg->type != AVA8_P_DETECT) && err) {
  842. err = avalon8_iic_xfer(avalon8, slave_addr, (uint8_t *)pkg, AVA8_WRITE_SIZE, (uint8_t *)ret, AVA8_READ_SIZE);
  843. applog(LOG_DEBUG, "%s-%d-%d: IIC read again!(type:0x%x, err:%d)", avalon8->drv->name, avalon8->device_id, slave_addr, pkg->type, err);
  844. }
  845. if (err) {
  846. /* FIXME: Don't care broadcast message with no reply, or it will block other thread when called by avalon8_send_bc_pkgs */
  847. if ((pkg->type != AVA8_P_DETECT) && (slave_addr == AVA8_MODULE_BROADCAST))
  848. return AVA8_SEND_OK;
  849. if (info->xfer_err_cnt++ == 100) {
  850. info->xfer_err_cnt = 0;
  851. applog(LOG_DEBUG, "%s-%d-%d: IIC xfer_err_cnt reach err = %d, rcnt = %d, rlen = %d",
  852. avalon8->drv->name, avalon8->device_id, slave_addr,
  853. err, rcnt, rlen);
  854. cgsleep_ms(5 * 1000); /* Wait MM reset */
  855. }
  856. return AVA8_SEND_ERROR;
  857. }
  858. info->xfer_err_cnt = 0;
  859. }
  860. return AVA8_SEND_OK;
  861. }
  862. static int avalon8_send_bc_pkgs(struct cgpu_info *avalon8, const struct avalon8_pkg *pkg)
  863. {
  864. int ret;
  865. do {
  866. ret = avalon8_iic_xfer_pkg(avalon8, AVA8_MODULE_BROADCAST, pkg, NULL);
  867. } while (ret != AVA8_SEND_OK);
  868. return 0;
  869. }
  870. static void avalon8_stratum_pkgs(struct cgpu_info *avalon8, struct pool *pool)
  871. {
  872. struct avalon8_info *info = avalon8->device_data;
  873. const int merkle_offset = 36;
  874. struct avalon8_pkg pkg;
  875. int i, a, b;
  876. uint32_t tmp;
  877. unsigned char target[32];
  878. int job_id_len, n2size;
  879. unsigned short crc;
  880. int coinbase_len_posthash, coinbase_len_prehash;
  881. uint8_t coinbase_prehash[32];
  882. uint32_t range, start;
  883. /* Send out the first stratum message STATIC */
  884. applog(LOG_DEBUG, "%s-%d: Pool stratum message STATIC: %d, %d, %d, %d, %d",
  885. avalon8->drv->name, avalon8->device_id,
  886. pool->coinbase_len,
  887. pool->nonce2_offset,
  888. pool->n2size,
  889. merkle_offset,
  890. pool->merkles);
  891. memset(pkg.data, 0, AVA8_P_DATA_LEN);
  892. tmp = be32toh(pool->coinbase_len);
  893. memcpy(pkg.data, &tmp, 4);
  894. tmp = be32toh(pool->nonce2_offset);
  895. memcpy(pkg.data + 4, &tmp, 4);
  896. n2size = pool->n2size >= 4 ? 4 : pool->n2size;
  897. tmp = be32toh(n2size);
  898. memcpy(pkg.data + 8, &tmp, 4);
  899. tmp = be32toh(merkle_offset);
  900. memcpy(pkg.data + 12, &tmp, 4);
  901. tmp = be32toh(pool->merkles);
  902. memcpy(pkg.data + 16, &tmp, 4);
  903. if (pool->n2size == 3)
  904. range = 0xffffff / (total_devices ? total_devices : 1);
  905. else
  906. range = 0xffffffff / (total_devices ? total_devices : 1);
  907. start = range * avalon8->device_id;
  908. tmp = be32toh(start);
  909. memcpy(pkg.data + 20, &tmp, 4);
  910. tmp = be32toh(range);
  911. memcpy(pkg.data + 24, &tmp, 4);
  912. if (info->work_restart) {
  913. info->work_restart = false;
  914. tmp = be32toh(0x1);
  915. memcpy(pkg.data + 28, &tmp, 4);
  916. }
  917. avalon8_init_pkg(&pkg, AVA8_P_STATIC, 1, 1);
  918. if (avalon8_send_bc_pkgs(avalon8, &pkg))
  919. return;
  920. if (pool->sdiff <= AVA8_DRV_DIFFMAX)
  921. set_target(target, pool->sdiff);
  922. else
  923. set_target(target, AVA8_DRV_DIFFMAX);
  924. memcpy(pkg.data, target, 32);
  925. if (opt_debug) {
  926. char *target_str;
  927. target_str = bin2hex(target, 32);
  928. applog(LOG_DEBUG, "%s-%d: Pool stratum target: %s", avalon8->drv->name, avalon8->device_id, target_str);
  929. free(target_str);
  930. }
  931. avalon8_init_pkg(&pkg, AVA8_P_TARGET, 1, 1);
  932. if (avalon8_send_bc_pkgs(avalon8, &pkg))
  933. return;
  934. memset(pkg.data, 0, AVA8_P_DATA_LEN);
  935. job_id_len = strlen(pool->swork.job_id);
  936. crc = crc16((unsigned char *)pool->swork.job_id, job_id_len);
  937. applog(LOG_DEBUG, "%s-%d: Pool stratum message JOBS_ID[%04x]: %s",
  938. avalon8->drv->name, avalon8->device_id,
  939. crc, pool->swork.job_id);
  940. tmp = ((crc << 16) | pool->pool_no);
  941. if (info->last_jobid != tmp) {
  942. info->last_jobid = tmp;
  943. pkg.data[0] = (crc & 0xff00) >> 8;
  944. pkg.data[1] = crc & 0xff;
  945. pkg.data[2] = pool->pool_no & 0xff;
  946. pkg.data[3] = (pool->pool_no & 0xff00) >> 8;
  947. avalon8_init_pkg(&pkg, AVA8_P_JOB_ID, 1, 1);
  948. if (avalon8_send_bc_pkgs(avalon8, &pkg))
  949. return;
  950. }
  951. coinbase_len_prehash = pool->nonce2_offset - (pool->nonce2_offset % SHA256_BLOCK_SIZE);
  952. coinbase_len_posthash = pool->coinbase_len - coinbase_len_prehash;
  953. sha256_prehash(pool->coinbase, coinbase_len_prehash, coinbase_prehash);
  954. a = (coinbase_len_posthash / AVA8_P_DATA_LEN) + 1;
  955. b = coinbase_len_posthash % AVA8_P_DATA_LEN;
  956. memcpy(pkg.data, coinbase_prehash, 32);
  957. avalon8_init_pkg(&pkg, AVA8_P_COINBASE, 1, a + (b ? 1 : 0));
  958. if (avalon8_send_bc_pkgs(avalon8, &pkg))
  959. return;
  960. applog(LOG_DEBUG, "%s-%d: Pool stratum message modified COINBASE: %d %d",
  961. avalon8->drv->name, avalon8->device_id,
  962. a, b);
  963. for (i = 1; i < a; i++) {
  964. memcpy(pkg.data, pool->coinbase + coinbase_len_prehash + i * 32 - 32, 32);
  965. avalon8_init_pkg(&pkg, AVA8_P_COINBASE, i + 1, a + (b ? 1 : 0));
  966. if (avalon8_send_bc_pkgs(avalon8, &pkg))
  967. return;
  968. }
  969. if (b) {
  970. memset(pkg.data, 0, AVA8_P_DATA_LEN);
  971. memcpy(pkg.data, pool->coinbase + coinbase_len_prehash + i * 32 - 32, b);
  972. avalon8_init_pkg(&pkg, AVA8_P_COINBASE, i + 1, i + 1);
  973. if (avalon8_send_bc_pkgs(avalon8, &pkg))
  974. return;
  975. }
  976. b = pool->merkles;
  977. applog(LOG_DEBUG, "%s-%d: Pool stratum message MERKLES: %d", avalon8->drv->name, avalon8->device_id, b);
  978. for (i = 0; i < b; i++) {
  979. memset(pkg.data, 0, AVA8_P_DATA_LEN);
  980. memcpy(pkg.data, pool->swork.merkle_bin[i], 32);
  981. avalon8_init_pkg(&pkg, AVA8_P_MERKLES, i + 1, b);
  982. if (avalon8_send_bc_pkgs(avalon8, &pkg))
  983. return;
  984. }
  985. applog(LOG_DEBUG, "%s-%d: Pool stratum message HEADER: 4", avalon8->drv->name, avalon8->device_id);
  986. for (i = 0; i < 4; i++) {
  987. memset(pkg.data, 0, AVA8_P_DATA_LEN);
  988. memcpy(pkg.data, pool->header_bin + i * 32, 32);
  989. avalon8_init_pkg(&pkg, AVA8_P_HEADER, i + 1, 4);
  990. if (avalon8_send_bc_pkgs(avalon8, &pkg))
  991. return;
  992. }
  993. if (info->connecter == AVA8_CONNECTER_AUC)
  994. avalon8_auc_getinfo(avalon8);
  995. }
  996. static struct cgpu_info *avalon8_iic_detect(void)
  997. {
  998. int i;
  999. struct avalon8_info *info;
  1000. struct cgpu_info *avalon8 = NULL;
  1001. struct i2c_ctx *i2c_slave = NULL;
  1002. i2c_slave = i2c_slave_open(I2C_BUS, 0);
  1003. if (!i2c_slave) {
  1004. applog(LOG_ERR, "avalon8 init iic failed\n");
  1005. return NULL;
  1006. }
  1007. i2c_slave->exit(i2c_slave);
  1008. i2c_slave = NULL;
  1009. avalon8 = cgcalloc(1, sizeof(*avalon8));
  1010. avalon8->drv = &avalon8_drv;
  1011. avalon8->deven = DEV_ENABLED;
  1012. avalon8->threads = 1;
  1013. add_cgpu(avalon8);
  1014. applog(LOG_INFO, "%s-%d: Found at %s", avalon8->drv->name, avalon8->device_id,
  1015. I2C_BUS);
  1016. avalon8->device_data = cgcalloc(sizeof(struct avalon8_info), 1);
  1017. memset(avalon8->device_data, 0, sizeof(struct avalon8_info));
  1018. info = avalon8->device_data;
  1019. for (i = 0; i < AVA8_DEFAULT_MODULARS; i++) {
  1020. info->enable[i] = false;
  1021. info->reboot[i] = false;
  1022. info->i2c_slaves[i] = i2c_slave_open(I2C_BUS, i);
  1023. if (!info->i2c_slaves[i]) {
  1024. applog(LOG_ERR, "avalon8 init i2c slaves failed\n");
  1025. free(avalon8->device_data);
  1026. avalon8->device_data = NULL;
  1027. free(avalon8);
  1028. avalon8 = NULL;
  1029. return NULL;
  1030. }
  1031. }
  1032. info->connecter = AVA8_CONNECTER_IIC;
  1033. return avalon8;
  1034. }
  1035. static void detect_modules(struct cgpu_info *avalon8);
  1036. static struct cgpu_info *avalon8_auc_detect(struct libusb_device *dev, struct usb_find_devices *found)
  1037. {
  1038. int i, modules = 0;
  1039. struct avalon8_info *info;
  1040. struct cgpu_info *avalon8 = usb_alloc_cgpu(&avalon8_drv, 1);
  1041. char auc_ver[AVA8_AUC_VER_LEN];
  1042. if (!usb_init(avalon8, dev, found)) {
  1043. applog(LOG_ERR, "avalon8 failed usb_init");
  1044. avalon8 = usb_free_cgpu(avalon8);
  1045. return NULL;
  1046. }
  1047. /* avalon8 prefers not to use zero length packets */
  1048. avalon8->nozlp = true;
  1049. /* We try twice on AUC init */
  1050. if (avalon8_auc_init(avalon8, auc_ver) && avalon8_auc_init(avalon8, auc_ver))
  1051. return NULL;
  1052. applog(LOG_INFO, "%s-%d: Found at %s", avalon8->drv->name, avalon8->device_id,
  1053. avalon8->device_path);
  1054. avalon8->device_data = cgcalloc(sizeof(struct avalon8_info), 1);
  1055. memset(avalon8->device_data, 0, sizeof(struct avalon8_info));
  1056. info = avalon8->device_data;
  1057. memcpy(info->auc_version, auc_ver, AVA8_AUC_VER_LEN);
  1058. info->auc_version[AVA8_AUC_VER_LEN] = '\0';
  1059. info->auc_speed = opt_avalon8_aucspeed;
  1060. info->auc_xdelay = opt_avalon8_aucxdelay;
  1061. for (i = 0; i < AVA8_DEFAULT_MODULARS; i++)
  1062. info->enable[i] = 0;
  1063. info->connecter = AVA8_CONNECTER_AUC;
  1064. detect_modules(avalon8);
  1065. for (i = 0; i < AVA8_DEFAULT_MODULARS; i++)
  1066. modules += info->enable[i];
  1067. if (!modules) {
  1068. applog(LOG_INFO, "avalon8 found but no modules initialised");
  1069. free(info);
  1070. avalon8 = usb_free_cgpu(avalon8);
  1071. return NULL;
  1072. }
  1073. /* We have an avalon8 AUC connected */
  1074. avalon8->threads = 1;
  1075. add_cgpu(avalon8);
  1076. update_usb_stats(avalon8);
  1077. return avalon8;
  1078. }
  1079. static inline void avalon8_detect(bool __maybe_unused hotplug)
  1080. {
  1081. usb_detect(&avalon8_drv, avalon8_auc_detect);
  1082. if (!hotplug && opt_avalon8_iic_detect)
  1083. avalon8_iic_detect();
  1084. }
  1085. static bool avalon8_prepare(struct thr_info *thr)
  1086. {
  1087. struct cgpu_info *avalon8 = thr->cgpu;
  1088. struct avalon8_info *info = avalon8->device_data;
  1089. info->last_diff1 = 0;
  1090. info->pending_diff1 = 0;
  1091. info->last_rej = 0;
  1092. info->mm_count = 0;
  1093. info->xfer_err_cnt = 0;
  1094. info->pool_no = 0;
  1095. memset(&(info->firsthash), 0, sizeof(info->firsthash));
  1096. cgtime(&(info->last_fan_adj));
  1097. cgtime(&info->last_stratum);
  1098. cgtime(&info->last_detect);
  1099. cglock_init(&info->update_lock);
  1100. cglock_init(&info->pool0.data_lock);
  1101. cglock_init(&info->pool1.data_lock);
  1102. cglock_init(&info->pool2.data_lock);
  1103. return true;
  1104. }
  1105. static int check_module_exist(struct cgpu_info *avalon8, uint8_t mm_dna[AVA8_MM_DNA_LEN])
  1106. {
  1107. struct avalon8_info *info = avalon8->device_data;
  1108. int i;
  1109. for (i = 0; i < AVA8_DEFAULT_MODULARS; i++) {
  1110. /* last byte is \0 */
  1111. if (info->enable[i] && !memcmp(info->mm_dna[i], mm_dna, AVA8_MM_DNA_LEN))
  1112. return 1;
  1113. }
  1114. return 0;
  1115. }
  1116. static void detect_modules(struct cgpu_info *avalon8)
  1117. {
  1118. struct avalon8_info *info = avalon8->device_data;
  1119. struct avalon8_pkg send_pkg;
  1120. struct avalon8_ret ret_pkg;
  1121. uint32_t tmp;
  1122. int i, j, k, err, rlen;
  1123. uint8_t dev_index;
  1124. uint8_t rbuf[AVA8_AUC_P_SIZE];
  1125. /* Detect new modules here */
  1126. for (i = 1; i < AVA8_DEFAULT_MODULARS + 1; i++) {
  1127. if (info->enable[i])
  1128. continue;
  1129. /* Send out detect pkg */
  1130. applog(LOG_DEBUG, "%s-%d: AVA8_P_DETECT ID[%d]",
  1131. avalon8->drv->name, avalon8->device_id, i);
  1132. memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
  1133. tmp = be32toh(i); /* ID */
  1134. memcpy(send_pkg.data + 28, &tmp, 4);
  1135. avalon8_init_pkg(&send_pkg, AVA8_P_DETECT, 1, 1);
  1136. err = avalon8_iic_xfer_pkg(avalon8, AVA8_MODULE_BROADCAST, &send_pkg, &ret_pkg);
  1137. if (err == AVA8_SEND_OK) {
  1138. if (decode_pkg(avalon8, &ret_pkg, AVA8_MODULE_BROADCAST)) {
  1139. applog(LOG_DEBUG, "%s-%d: Should be AVA8_P_ACKDETECT(%d), but %d",
  1140. avalon8->drv->name, avalon8->device_id, AVA8_P_ACKDETECT, ret_pkg.type);
  1141. continue;
  1142. }
  1143. }
  1144. if (err != AVA8_SEND_OK) {
  1145. applog(LOG_DEBUG, "%s-%d: AVA8_P_DETECT: Failed AUC xfer data with err %d",
  1146. avalon8->drv->name, avalon8->device_id, err);
  1147. break;
  1148. }
  1149. applog(LOG_DEBUG, "%s-%d: Module detect ID[%d]: %d",
  1150. avalon8->drv->name, avalon8->device_id, i, ret_pkg.type);
  1151. if (ret_pkg.type != AVA8_P_ACKDETECT)
  1152. break;
  1153. if (check_module_exist(avalon8, ret_pkg.data))
  1154. continue;
  1155. /* Check count of modulars */
  1156. if (i == AVA8_DEFAULT_MODULARS) {
  1157. applog(LOG_NOTICE, "You have connected more than %d machines. This is discouraged.", (AVA8_DEFAULT_MODULARS - 1));
  1158. info->conn_overloaded = true;
  1159. break;
  1160. } else
  1161. info->conn_overloaded = false;
  1162. memcpy(info->mm_version[i], ret_pkg.data + AVA8_MM_DNA_LEN, AVA8_MM_VER_LEN);
  1163. info->mm_version[i][AVA8_MM_VER_LEN] = '\0';
  1164. for (dev_index = 0; dev_index < (sizeof(avalon8_dev_table) / sizeof(avalon8_dev_table[0])); dev_index++) {
  1165. if (!strncmp((char *)&(info->mm_version[i]), (char *)(avalon8_dev_table[dev_index].dev_id_str), 3)) {
  1166. info->mod_type[i] = avalon8_dev_table[dev_index].mod_type;
  1167. info->miner_count[i] = avalon8_dev_table[dev_index].miner_count;
  1168. info->asic_count[i] = avalon8_dev_table[dev_index].asic_count;
  1169. info->vin_adc_ratio[i] = avalon8_dev_table[dev_index].vin_adc_ratio;
  1170. info->vout_adc_ratio[i] = avalon8_dev_table[dev_index].vout_adc_ratio;
  1171. break;
  1172. }
  1173. }
  1174. if (dev_index == (sizeof(avalon8_dev_table) / sizeof(avalon8_dev_table[0]))) {
  1175. applog(LOG_NOTICE, "%s-%d: The modular version %s cann't be support",
  1176. avalon8->drv->name, avalon8->device_id, info->mm_version[i]);
  1177. break;
  1178. }
  1179. info->enable[i] = 1;
  1180. cgtime(&info->elapsed[i]);
  1181. memcpy(info->mm_dna[i], ret_pkg.data, AVA8_MM_DNA_LEN);
  1182. memcpy(&tmp, ret_pkg.data + AVA8_MM_DNA_LEN + AVA8_MM_VER_LEN, 4);
  1183. tmp = be32toh(tmp);
  1184. info->total_asics[i] = tmp;
  1185. info->temp_overheat[i] = AVA8_DEFAULT_TEMP_OVERHEAT;
  1186. info->temp_target[i] = opt_avalon8_temp_target;
  1187. info->fan_pct[i] = opt_avalon8_fan_min;
  1188. for (j = 0; j < info->miner_count[i]; j++) {
  1189. if (opt_avalon8_voltage_level == AVA8_INVALID_VOLTAGE_LEVEL)
  1190. info->set_voltage_level[i][j] = avalon8_dev_table[dev_index].set_voltage_level;
  1191. else
  1192. info->set_voltage_level[i][j] = opt_avalon8_voltage_level;
  1193. info->get_voltage[i][j] = 0;
  1194. info->get_vin[i][j] = 0;
  1195. for (k = 0; k < info->asic_count[i]; k++)
  1196. info->temp[i][j][k] = -273;
  1197. for (k = 0; k < AVA8_DEFAULT_PLL_CNT; k++)
  1198. info->set_frequency[i][j][k] = avalon8_dev_table[dev_index].set_freq[k];
  1199. }
  1200. info->freq_mode[i] = AVA8_FREQ_INIT_MODE;
  1201. memset(info->get_pll[i], 0, sizeof(uint32_t) * info->miner_count[i] * AVA8_DEFAULT_PLL_CNT);
  1202. info->led_indicator[i] = 0;
  1203. info->cutoff[i] = 0;
  1204. info->fan_cpm[i] = 0;
  1205. info->temp_mm[i] = -273;
  1206. info->temp_last_max[i] = -273;
  1207. info->local_works[i] = 0;
  1208. info->hw_works[i] = 0;
  1209. for (j = 0; j < info->miner_count[i]; j++) {
  1210. memset(info->chip_matching_work[i][j], 0, sizeof(uint64_t) * info->asic_count[i]);
  1211. info->local_works_i[i][j] = 0;
  1212. info->hw_works_i[i][j] = 0;
  1213. info->error_code[i][j] = 0;
  1214. info->error_crc[i][j] = 0;
  1215. }
  1216. info->error_code[i][j] = 0;
  1217. info->error_polling_cnt[i] = 0;
  1218. info->power_good[i] = 0;
  1219. memset(info->pmu_version[i], 0, sizeof(char) * 5 * AVA8_DEFAULT_PMU_CNT);
  1220. info->diff1[i] = 0;
  1221. applog(LOG_NOTICE, "%s-%d: New module detected! ID[%d-%x]",
  1222. avalon8->drv->name, avalon8->device_id, i, info->mm_dna[i][AVA8_MM_DNA_LEN - 1]);
  1223. /* Tell MM, it has been detected */
  1224. memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
  1225. memcpy(send_pkg.data, info->mm_dna[i], AVA8_MM_DNA_LEN);
  1226. avalon8_init_pkg(&send_pkg, AVA8_P_SYNC, 1, 1);
  1227. avalon8_iic_xfer_pkg(avalon8, i, &send_pkg, &ret_pkg);
  1228. /* Keep the usb buffer is empty */
  1229. usb_buffer_clear(avalon8);
  1230. usb_read(avalon8, (char *)rbuf, AVA8_AUC_P_SIZE, &rlen, C_AVA8_READ);
  1231. }
  1232. }
  1233. static void detach_module(struct cgpu_info *avalon8, int addr)
  1234. {
  1235. struct avalon8_info *info = avalon8->device_data;
  1236. info->enable[addr] = 0;
  1237. applog(LOG_NOTICE, "%s-%d: Module detached! ID[%d]",
  1238. avalon8->drv->name, avalon8->device_id, addr);
  1239. }
  1240. static int polling(struct cgpu_info *avalon8)
  1241. {
  1242. struct avalon8_info *info = avalon8->device_data;
  1243. struct avalon8_pkg send_pkg;
  1244. struct avalon8_ret ar;
  1245. int i, tmp, ret, decode_err = 0;
  1246. struct timeval current_fan;
  1247. int do_adjust_fan = 0;
  1248. uint32_t fan_pwm;
  1249. double device_tdiff;
  1250. cgtime(&current_fan);
  1251. device_tdiff = tdiff(&current_fan, &(info->last_fan_adj));
  1252. if (device_tdiff > 2.0 || device_tdiff < 0) {
  1253. cgtime(&info->last_fan_adj);
  1254. do_adjust_fan = 1;
  1255. }
  1256. for (i = 1; i < AVA8_DEFAULT_MODULARS; i++) {
  1257. if (!info->enable[i])
  1258. continue;
  1259. cgsleep_ms(opt_avalon8_polling_delay);
  1260. memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
  1261. /* Red LED */
  1262. tmp = be32toh(info->led_indicator[i]);
  1263. memcpy(send_pkg.data, &tmp, 4);
  1264. /* Adjust fan every 2 seconds*/
  1265. if (do_adjust_fan) {
  1266. fan_pwm = adjust_fan(info, i);
  1267. fan_pwm |= 0x80000000;
  1268. tmp = be32toh(fan_pwm);
  1269. memcpy(send_pkg.data + 4, &tmp, 4);
  1270. }
  1271. if (info->reboot[i]) {
  1272. info->reboot[i] = false;
  1273. send_pkg.data[8] = 0x1;
  1274. }
  1275. avalon8_init_pkg(&send_pkg, AVA8_P_POLLING, 1, 1);
  1276. ret = avalon8_iic_xfer_pkg(avalon8, i, &send_pkg, &ar);
  1277. if (ret == AVA8_SEND_OK)
  1278. decode_err = decode_pkg(avalon8, &ar, i);
  1279. if (ret != AVA8_SEND_OK || decode_err) {
  1280. info->error_polling_cnt[i]++;
  1281. memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
  1282. avalon8_init_pkg(&send_pkg, AVA8_P_RSTMMTX, 1, 1);
  1283. avalon8_iic_xfer_pkg(avalon8, i, &send_pkg, NULL);
  1284. if (info->error_polling_cnt[i] >= 10)
  1285. detach_module(avalon8, i);
  1286. }
  1287. if (ret == AVA8_SEND_OK && !decode_err) {
  1288. info->error_polling_cnt[i] = 0;
  1289. if ((ar.opt == AVA8_P_STATUS) &&
  1290. (info->mm_dna[i][AVA8_MM_DNA_LEN - 1] != ar.opt)) {
  1291. applog(LOG_ERR, "%s-%d-%d: Dup address found %d-%d",
  1292. avalon8->drv->name, avalon8->device_id, i,
  1293. info->mm_dna[i][AVA8_MM_DNA_LEN - 1], ar.opt);
  1294. hexdump((uint8_t *)&ar, sizeof(ar));
  1295. detach_module(avalon8, i);
  1296. }
  1297. }
  1298. }
  1299. return 0;
  1300. }
  1301. static void copy_pool_stratum(struct pool *pool_stratum, struct pool *pool)
  1302. {
  1303. int i;
  1304. int merkles = pool->merkles, job_id_len;
  1305. size_t coinbase_len = pool->coinbase_len;
  1306. unsigned short crc;
  1307. if (!pool->swork.job_id)
  1308. return;
  1309. if (pool_stratum->swork.job_id) {
  1310. job_id_len = strlen(pool->swork.job_id);
  1311. crc = crc16((unsigned char *)pool->swork.job_id, job_id_len);
  1312. job_id_len = strlen(pool_stratum->swork.job_id);
  1313. if (crc16((unsigned char *)pool_stratum->swork.job_id, job_id_len) == crc)
  1314. return;
  1315. }
  1316. cg_wlock(&pool_stratum->data_lock);
  1317. free(pool_stratum->swork.job_id);
  1318. free(pool_stratum->nonce1);
  1319. free(pool_stratum->coinbase);
  1320. pool_stratum->coinbase = cgcalloc(coinbase_len, 1);
  1321. memcpy(pool_stratum->coinbase, pool->coinbase, coinbase_len);
  1322. for (i = 0; i < pool_stratum->merkles; i++)
  1323. free(pool_stratum->swork.merkle_bin[i]);
  1324. if (merkles) {
  1325. pool_stratum->swork.merkle_bin = cgrealloc(pool_stratum->swork.merkle_bin,
  1326. sizeof(char *) * merkles + 1);
  1327. for (i = 0; i < merkles; i++) {
  1328. pool_stratum->swork.merkle_bin[i] = cgmalloc(32);
  1329. memcpy(pool_stratum->swork.merkle_bin[i], pool->swork.merkle_bin[i], 32);
  1330. }
  1331. }
  1332. pool_stratum->sdiff = pool->sdiff;
  1333. pool_stratum->coinbase_len = pool->coinbase_len;
  1334. pool_stratum->nonce2_offset = pool->nonce2_offset;
  1335. pool_stratum->n2size = pool->n2size;
  1336. pool_stratum->merkles = pool->merkles;
  1337. pool_stratum->swork.job_id = strdup(pool->swork.job_id);
  1338. pool_stratum->nonce1 = strdup(pool->nonce1);
  1339. memcpy(pool_stratum->ntime, pool->ntime, sizeof(pool_stratum->ntime));
  1340. memcpy(pool_stratum->header_bin, pool->header_bin, sizeof(pool_stratum->header_bin));
  1341. cg_wunlock(&pool_stratum->data_lock);
  1342. }
  1343. static void avalon8_init_setting(struct cgpu_info *avalon8, int addr)
  1344. {
  1345. struct avalon8_pkg send_pkg;
  1346. uint32_t tmp;
  1347. memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
  1348. tmp = be32toh(opt_avalon8_freq_sel);
  1349. memcpy(send_pkg.data + 4, &tmp, 4);
  1350. /*
  1351. * set flags:
  1352. * 0: ss switch
  1353. * 1: nonce check
  1354. * 2: roll enable
  1355. */
  1356. tmp = 1;
  1357. if (!opt_avalon8_smart_speed)
  1358. tmp = 0;
  1359. tmp |= (opt_avalon8_nonce_check << 1);
  1360. tmp |= (opt_avalon8_roll_enable << 2);
  1361. send_pkg.data[8] = tmp & 0xff;
  1362. send_pkg.data[9] = opt_avalon8_nonce_mask & 0xff;
  1363. tmp = be32toh(opt_avalon8_mux_l2h);
  1364. memcpy(send_pkg.data + 10, &tmp, 4);
  1365. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set mux l2h %u",
  1366. avalon8->drv->name, avalon8->device_id, addr,
  1367. opt_avalon8_mux_l2h);
  1368. tmp = be32toh(opt_avalon8_mux_h2l);
  1369. memcpy(send_pkg.data + 14, &tmp, 4);
  1370. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set mux h2l %u",
  1371. avalon8->drv->name, avalon8->device_id, addr,
  1372. opt_avalon8_mux_h2l);
  1373. tmp = be32toh(opt_avalon8_h2ltime0_spd);
  1374. memcpy(send_pkg.data + 18, &tmp, 4);
  1375. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set h2ltime0 spd %u",
  1376. avalon8->drv->name, avalon8->device_id, addr,
  1377. opt_avalon8_h2ltime0_spd);
  1378. tmp = be32toh(opt_avalon8_spdlow);
  1379. memcpy(send_pkg.data + 22, &tmp, 4);
  1380. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set spdlow %u",
  1381. avalon8->drv->name, avalon8->device_id, addr,
  1382. opt_avalon8_spdlow);
  1383. tmp = be32toh(opt_avalon8_spdhigh);
  1384. memcpy(send_pkg.data + 26, &tmp, 4);
  1385. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set spdhigh %u",
  1386. avalon8->drv->name, avalon8->device_id, addr,
  1387. opt_avalon8_spdhigh);
  1388. /* Package the data */
  1389. avalon8_init_pkg(&send_pkg, AVA8_P_SET, 1, 1);
  1390. if (addr == AVA8_MODULE_BROADCAST)
  1391. avalon8_send_bc_pkgs(avalon8, &send_pkg);
  1392. else
  1393. avalon8_iic_xfer_pkg(avalon8, addr, &send_pkg, NULL);
  1394. }
  1395. static void avalon8_set_voltage_level(struct cgpu_info *avalon8, int addr, unsigned int voltage[])
  1396. {
  1397. struct avalon8_info *info = avalon8->device_data;
  1398. struct avalon8_pkg send_pkg;
  1399. uint32_t tmp;
  1400. uint8_t i;
  1401. memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
  1402. /* NOTE: miner_count should <= 8 */
  1403. for (i = 0; i < info->miner_count[addr]; i++) {
  1404. tmp = be32toh(encode_voltage(voltage[i] +
  1405. opt_avalon8_voltage_level_offset));
  1406. memcpy(send_pkg.data + i * 4, &tmp, 4);
  1407. }
  1408. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set voltage miner %d, (%d-%d)",
  1409. avalon8->drv->name, avalon8->device_id, addr,
  1410. i, voltage[0], voltage[info->miner_count[addr] - 1]);
  1411. /* Package the data */
  1412. avalon8_init_pkg(&send_pkg, AVA8_P_SET_VOLT, 1, 1);
  1413. if (addr == AVA8_MODULE_BROADCAST)
  1414. avalon8_send_bc_pkgs(avalon8, &send_pkg);
  1415. else
  1416. avalon8_iic_xfer_pkg(avalon8, addr, &send_pkg, NULL);
  1417. }
  1418. static void avalon8_set_freq(struct cgpu_info *avalon8, int addr, int miner_id, unsigned int freq[])
  1419. {
  1420. struct avalon8_info *info = avalon8->device_data;
  1421. struct avalon8_pkg send_pkg;
  1422. uint32_t tmp, f;
  1423. uint8_t i;
  1424. memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
  1425. for (i = 0; i < AVA8_DEFAULT_PLL_CNT; i++) {
  1426. tmp = be32toh(api_get_cpm(freq[i]));
  1427. memcpy(send_pkg.data + i * 4, &tmp, 4);
  1428. }
  1429. f = freq[0];
  1430. for (i = 1; i < AVA8_DEFAULT_PLL_CNT; i++)
  1431. f = f > freq[i] ? f : freq[i];
  1432. f = f ? f : 1;
  1433. tmp = AVA8_ASIC_TIMEOUT_CONST / f * 83 / 100;
  1434. tmp = be32toh(tmp);
  1435. memcpy(send_pkg.data + AVA8_DEFAULT_PLL_CNT * 4, &tmp, 4);
  1436. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set freq miner %x-%x",
  1437. avalon8->drv->name, avalon8->device_id, addr,
  1438. miner_id, be32toh(tmp));
  1439. /* Package the data */
  1440. avalon8_init_pkg(&send_pkg, AVA8_P_SET_PLL, miner_id + 1, info->miner_count[addr]);
  1441. if (addr == AVA8_MODULE_BROADCAST)
  1442. avalon8_send_bc_pkgs(avalon8, &send_pkg);
  1443. else
  1444. avalon8_iic_xfer_pkg(avalon8, addr, &send_pkg, NULL);
  1445. }
  1446. static void avalon8_set_factory_info(struct cgpu_info *avalon8, int addr, uint8_t value[])
  1447. {
  1448. struct avalon8_pkg send_pkg;
  1449. uint8_t i;
  1450. memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
  1451. for (i = 0; i < AVA8_DEFAULT_FACTORY_INFO_CNT; i++)
  1452. send_pkg.data[i] = value[i];
  1453. /* Package the data */
  1454. avalon8_init_pkg(&send_pkg, AVA8_P_SET_FAC, 1, 1);
  1455. if (addr == AVA8_MODULE_BROADCAST)
  1456. avalon8_send_bc_pkgs(avalon8, &send_pkg);
  1457. else
  1458. avalon8_iic_xfer_pkg(avalon8, addr, &send_pkg, NULL);
  1459. }
  1460. static void avalon8_set_overclocking_info(struct cgpu_info *avalon8, int addr, uint8_t value[])
  1461. {
  1462. struct avalon8_pkg send_pkg;
  1463. uint8_t i;
  1464. memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
  1465. for (i = 0; i < AVA8_DEFAULT_OVERCLOCKING_CNT; i++)
  1466. send_pkg.data[i] = value[i];
  1467. /* Package the data */
  1468. avalon8_init_pkg(&send_pkg, AVA8_P_SET_OC, 1, 1);
  1469. if (addr == AVA8_MODULE_BROADCAST)
  1470. avalon8_send_bc_pkgs(avalon8, &send_pkg);
  1471. else
  1472. avalon8_iic_xfer_pkg(avalon8, addr, &send_pkg, NULL);
  1473. }
  1474. static void avalon8_set_ss_param(struct cgpu_info *avalon8, int addr)
  1475. {
  1476. struct avalon8_pkg send_pkg;
  1477. uint32_t tmp;
  1478. if (!opt_avalon8_smart_speed)
  1479. return;
  1480. memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
  1481. tmp = (opt_avalon8_th_pass << 16) | opt_avalon8_th_fail;
  1482. tmp = be32toh(tmp);
  1483. memcpy(send_pkg.data, &tmp, 4);
  1484. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set th pass %u",
  1485. avalon8->drv->name, avalon8->device_id, addr,
  1486. opt_avalon8_th_pass);
  1487. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set th fail %u",
  1488. avalon8->drv->name, avalon8->device_id, addr,
  1489. opt_avalon8_th_fail);
  1490. tmp = ((opt_avalon8_th_add & 0x1) << 31) | ((opt_avalon8_th_mssel & 0x1) << 30)
  1491. | ((opt_avalon8_th_ms & 0x3fff) << 16)
  1492. | (opt_avalon8_th_init & 0xffff);
  1493. tmp = be32toh(tmp);
  1494. memcpy(send_pkg.data + 4, &tmp, 4);
  1495. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set th add %u",
  1496. avalon8->drv->name, avalon8->device_id, addr,
  1497. (opt_avalon8_th_add & 0x1));
  1498. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set th mssel %u",
  1499. avalon8->drv->name, avalon8->device_id, addr,
  1500. (opt_avalon8_th_mssel & 0x1));
  1501. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set th ms %u",
  1502. avalon8->drv->name, avalon8->device_id, addr,
  1503. (opt_avalon8_th_ms & 0x3fff));
  1504. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set th init %u",
  1505. avalon8->drv->name, avalon8->device_id, addr,
  1506. (opt_avalon8_th_init & 0xffff));
  1507. tmp = be32toh(opt_avalon8_th_timeout);
  1508. memcpy(send_pkg.data + 8, &tmp, 4);
  1509. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set th timeout %u",
  1510. avalon8->drv->name, avalon8->device_id, addr,
  1511. opt_avalon8_th_timeout);
  1512. tmp = ((opt_avalon8_lv3_th_add & 0x1) << 31) | ((opt_avalon8_lv2_th_add & 0x1) << 15)
  1513. | ((opt_avalon8_lv3_th_ms & 0x7fff) << 16)
  1514. | (opt_avalon8_lv2_th_ms & 0x7fff);
  1515. tmp = be32toh(tmp);
  1516. memcpy(send_pkg.data + 12, &tmp, 4);
  1517. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv3 th add %u",
  1518. avalon8->drv->name, avalon8->device_id, addr,
  1519. (opt_avalon8_lv3_th_add & 0x1));
  1520. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv3 th ms %u",
  1521. avalon8->drv->name, avalon8->device_id, addr,
  1522. (opt_avalon8_lv3_th_ms & 0x7fff));
  1523. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv2 th add %u",
  1524. avalon8->drv->name, avalon8->device_id, addr,
  1525. (opt_avalon8_lv2_th_add & 0x1));
  1526. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv2 th ms %u",
  1527. avalon8->drv->name, avalon8->device_id, addr,
  1528. (opt_avalon8_lv2_th_ms & 0x7fff));
  1529. tmp = ((opt_avalon8_lv5_th_add & 0x1) << 31) | ((opt_avalon8_lv4_th_add & 0x1) << 15)
  1530. | ((opt_avalon8_lv5_th_ms & 0x7fff) << 16)
  1531. | (opt_avalon8_lv4_th_ms & 0x7fff);
  1532. tmp = be32toh(tmp);
  1533. memcpy(send_pkg.data + 16, &tmp, 4);
  1534. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv5 th add %u",
  1535. avalon8->drv->name, avalon8->device_id, addr,
  1536. (opt_avalon8_lv5_th_add & 0x1));
  1537. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv5 th ms %u",
  1538. avalon8->drv->name, avalon8->device_id, addr,
  1539. (opt_avalon8_lv5_th_ms & 0x7fff));
  1540. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv4 th add %u",
  1541. avalon8->drv->name, avalon8->device_id, addr,
  1542. (opt_avalon8_lv4_th_add & 0x1));
  1543. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv4 th ms %u",
  1544. avalon8->drv->name, avalon8->device_id, addr,
  1545. (opt_avalon8_lv4_th_ms & 0x7fff));
  1546. tmp = ((opt_avalon8_lv7_th_add & 0x1) << 31) | ((opt_avalon8_lv6_th_add & 0x1) << 15)
  1547. | ((opt_avalon8_lv7_th_ms & 0x7fff) << 16)
  1548. | (opt_avalon8_lv6_th_ms & 0x7fff);
  1549. tmp = be32toh(tmp);
  1550. memcpy(send_pkg.data + 20, &tmp, 4);
  1551. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv7 th add %u",
  1552. avalon8->drv->name, avalon8->device_id, addr,
  1553. (opt_avalon8_lv7_th_add & 0x1));
  1554. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv7 th ms %u",
  1555. avalon8->drv->name, avalon8->device_id, addr,
  1556. (opt_avalon8_lv7_th_ms & 0x7fff));
  1557. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv6 th add %u",
  1558. avalon8->drv->name, avalon8->device_id, addr,
  1559. (opt_avalon8_lv6_th_add & 0x1));
  1560. applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv6 th ms %u",
  1561. avalon8->drv->name, avalon8->device_id, addr,
  1562. (opt_avalon8_lv6_th_ms & 0x7fff));
  1563. /* Package the data */
  1564. avalon8_init_pkg(&send_pkg, AVA8_P_SET_SS, 1, 1);
  1565. if (addr == AVA8_MODULE_BROADCAST)
  1566. avalon8_send_bc_pkgs(avalon8, &send_pkg);
  1567. else
  1568. avalon8_iic_xfer_pkg(avalon8, addr, &send_pkg, NULL);
  1569. }
  1570. static void avalon8_stratum_finish(struct cgpu_info *avalon8)
  1571. {
  1572. struct avalon8_pkg send_pkg;
  1573. memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
  1574. avalon8_init_pkg(&send_pkg, AVA8_P_JOB_FIN, 1, 1);
  1575. avalon8_send_bc_pkgs(avalon8, &send_pkg);
  1576. }
  1577. static void avalon8_set_finish(struct cgpu_info *avalon8, int addr)
  1578. {
  1579. struct avalon8_pkg send_pkg;
  1580. memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
  1581. avalon8_init_pkg(&send_pkg, AVA8_P_SET_FIN, 1, 1);
  1582. avalon8_iic_xfer_pkg(avalon8, addr, &send_pkg, NULL);
  1583. }
  1584. static void avalon8_sswork_update(struct cgpu_info *avalon8)
  1585. {
  1586. struct avalon8_info *info = avalon8->device_data;
  1587. struct thr_info *thr = avalon8->thr[0];
  1588. struct pool *pool;
  1589. int coinbase_len_posthash, coinbase_len_prehash;
  1590. cgtime(&info->last_stratum);
  1591. /*
  1592. * NOTE: We need mark work_restart to private information,
  1593. * So that it cann't reset by hash_driver_work
  1594. */
  1595. if (thr->work_restart)
  1596. info->work_restart = thr->work_restart;
  1597. applog(LOG_NOTICE, "%s-%d: New stratum: restart: %d, update: %d, clean: %d",
  1598. avalon8->drv->name, avalon8->device_id,
  1599. thr->work_restart, thr->work_update, thr->clean_jobs);
  1600. /* Step 1: MM protocol check */
  1601. pool = current_pool();
  1602. if (!pool->has_stratum)
  1603. quit(1, "%s-%d: MM has to use stratum pools", avalon8->drv->name, avalon8->device_id);
  1604. coinbase_len_prehash = pool->nonce2_offset - (pool->nonce2_offset % SHA256_BLOCK_SIZE);
  1605. coinbase_len_posthash = pool->coinbase_len - coinbase_len_prehash;
  1606. if (coinbase_len_posthash + SHA256_BLOCK_SIZE > AVA8_P_COINBASE_SIZE) {
  1607. applog(LOG_ERR, "%s-%d: MM pool modified coinbase length(%d) is more than %d",
  1608. avalon8->drv->name, avalon8->device_id,
  1609. coinbase_len_posthash + SHA256_BLOCK_SIZE, AVA8_P_COINBASE_SIZE);
  1610. return;
  1611. }
  1612. if (pool->merkles > AVA8_P_MERKLES_COUNT) {
  1613. applog(LOG_ERR, "%s-%d: MM merkles has to be less then %d", avalon8->drv->name, avalon8->device_id, AVA8_P_MERKLES_COUNT);
  1614. return;
  1615. }
  1616. if (pool->n2size < 3) {
  1617. applog(LOG_ERR, "%s-%d: MM nonce2 size has to be >= 3 (%d)", avalon8->drv->name, avalon8->device_id, pool->n2size);
  1618. return;
  1619. }
  1620. cg_wlock(&info->update_lock);
  1621. /* Step 2: Send out stratum pkgs */
  1622. cg_rlock(&pool->data_lock);
  1623. info->pool_no = pool->pool_no;
  1624. copy_pool_stratum(&info->pool2, &info->pool1);
  1625. copy_pool_stratum(&info->pool1, &info->pool0);
  1626. copy_pool_stratum(&info->pool0, pool);
  1627. avalon8_stratum_pkgs(avalon8, pool);
  1628. cg_runlock(&pool->data_lock);
  1629. /* Step 3: Send out finish pkg */
  1630. avalon8_stratum_finish(avalon8);
  1631. cg_wunlock(&info->update_lock);
  1632. }
  1633. static int64_t avalon8_scanhash(struct thr_info *thr)
  1634. {
  1635. struct cgpu_info *avalon8 = thr->cgpu;
  1636. struct avalon8_info *info = avalon8->device_data;
  1637. struct timeval current;
  1638. int i, j, k, count = 0;
  1639. int temp_max;
  1640. int64_t ret;
  1641. bool update_settings = false;
  1642. if ((info->connecter == AVA8_CONNECTER_AUC) &&
  1643. (unlikely(avalon8->usbinfo.nodev))) {
  1644. applog(LOG_ERR, "%s-%d: Device disappeared, shutting down thread",
  1645. avalon8->drv->name, avalon8->device_id);
  1646. return -1;
  1647. }
  1648. /* Step 1: Stop polling and detach the device if there is no stratum in 3 minutes, network is down */
  1649. cgtime(&current);
  1650. if (tdiff(&current, &(info->last_stratum)) > 180.0) {
  1651. for (i = 1; i < AVA8_DEFAULT_MODULARS; i++) {
  1652. if (!info->enable[i])
  1653. continue;
  1654. detach_module(avalon8, i);
  1655. }
  1656. info->mm_count = 0;
  1657. return 0;
  1658. }
  1659. /* Step 2: Try to detect new modules */
  1660. if ((tdiff(&current, &(info->last_detect)) > AVA8_MODULE_DETECT_INTERVAL) ||
  1661. !info->mm_count) {
  1662. cgtime(&info->last_detect);
  1663. detect_modules(avalon8);
  1664. }
  1665. /* Step 3: ASIC configrations (voltage and frequency) */
  1666. for (i = 1; i < AVA8_DEFAULT_MODULARS; i++) {
  1667. if (!info->enable[i])
  1668. continue;
  1669. update_settings = false;
  1670. /* Check temperautre */
  1671. temp_max = get_temp_max(info, i);
  1672. /* Enter too hot */
  1673. if (temp_max >= info->temp_overheat[i])
  1674. info->cutoff[i] = 1;
  1675. /* Exit too hot */
  1676. if (info->cutoff[i] && (temp_max <= (info->temp_overheat[i] - 10)))
  1677. info->cutoff[i] = 0;
  1678. switch (info->freq_mode[i]) {
  1679. case AVA8_FREQ_INIT_MODE:
  1680. update_settings = true;
  1681. for (j = 0; j < info->miner_count[i]; j++) {
  1682. for (k = 0; k < AVA8_DEFAULT_PLL_CNT; k++) {
  1683. if (opt_avalon8_freq[k] != AVA8_DEFAULT_FREQUENCY_IGNORE)
  1684. info->set_frequency[i][j][k] = opt_avalon8_freq[k];
  1685. }
  1686. }
  1687. avalon8_init_setting(avalon8, i);
  1688. info->freq_mode[i] = AVA8_FREQ_PLLADJ_MODE;
  1689. break;
  1690. case AVA8_FREQ_PLLADJ_MODE:
  1691. if (opt_avalon8_smart_speed == AVA8_DEFAULT_SMARTSPEED_OFF)
  1692. break;
  1693. /* AVA8_DEFAULT_SMARTSPEED_MODE1: auto speed by A3210 chips */
  1694. break;
  1695. default:
  1696. applog(LOG_ERR, "%s-%d-%d: Invalid frequency mode %d",
  1697. avalon8->drv->name, avalon8->device_id, i, info->freq_mode[i]);
  1698. break;
  1699. }
  1700. if (update_settings) {
  1701. cg_wlock(&info->update_lock);
  1702. avalon8_set_voltage_level(avalon8, i, info->set_voltage_level[i]);
  1703. for (j = 0; j < info->miner_count[i]; j++)
  1704. avalon8_set_freq(avalon8, i, j, info->set_frequency[i][j]);
  1705. if (opt_avalon8_smart_speed)
  1706. avalon8_set_ss_param(avalon8, i);
  1707. avalon8_set_finish(avalon8, i);
  1708. cg_wunlock(&info->update_lock);
  1709. }
  1710. }
  1711. /* Step 4: Polling */
  1712. cg_rlock(&info->update_lock);
  1713. polling(avalon8);
  1714. cg_runlock(&info->update_lock);
  1715. /* Step 5: Calculate mm count */
  1716. for (i = 1; i < AVA8_DEFAULT_MODULARS; i++) {
  1717. if (info->enable[i])
  1718. count++;
  1719. }
  1720. info->mm_count = count;
  1721. /* Step 6: Calculate hashes. Use the diff1 value which is scaled by
  1722. * device diff and is usually lower than pool diff which will give a
  1723. * more stable result, but remove diff rejected shares to more closely
  1724. * approximate diff accepted values. */
  1725. info->pending_diff1 += avalon8->diff1 - info->last_diff1;
  1726. info->last_diff1 = avalon8->diff1;
  1727. info->pending_diff1 -= avalon8->diff_rejected - info->last_rej;
  1728. info->last_rej = avalon8->diff_rejected;
  1729. if (info->pending_diff1 && !info->firsthash.tv_sec) {
  1730. cgtime(&info->firsthash);
  1731. copy_time(&(avalon8->dev_start_tv), &(info->firsthash));
  1732. }
  1733. if (info->pending_diff1 <= 0)
  1734. ret = 0;
  1735. else {
  1736. ret = info->pending_diff1;
  1737. info->pending_diff1 = 0;
  1738. }
  1739. return ret * 0xffffffffull;
  1740. }
  1741. static float avalon8_hash_cal(struct cgpu_info *avalon8, int modular_id)
  1742. {
  1743. struct avalon8_info *info = avalon8->device_data;
  1744. uint32_t tmp_freq[AVA8_DEFAULT_PLL_CNT];
  1745. unsigned int i, j, k;
  1746. float mhsmm;
  1747. mhsmm = 0;
  1748. for (i = 0; i < info->miner_count[modular_id]; i++) {
  1749. for (j = 0; j < info->asic_count[modular_id]; j++) {
  1750. for (k = 0; k < AVA8_DEFAULT_PLL_CNT; k++)
  1751. mhsmm += (info->get_asic[modular_id][i][j][2 + k] * info->get_frequency[modular_id][i][k]);
  1752. }
  1753. }
  1754. return mhsmm;
  1755. }
  1756. #define STATBUFLEN_WITHOUT_DBG (6 * 1024)
  1757. #define STATBUFLEN_WITH_DBG (6 * 7 * 1024)
  1758. static struct api_data *avalon8_api_stats(struct cgpu_info *avalon8)
  1759. {
  1760. struct api_data *root = NULL;
  1761. struct avalon8_info *info = avalon8->device_data;
  1762. int i, j, k, m;
  1763. double a, b, dh;
  1764. char buf[256];
  1765. char *statbuf = NULL;
  1766. struct timeval current;
  1767. float mhsmm, auc_temp = 0.0;
  1768. double sum;
  1769. int avg, cnt, max_vl, max_id, min_vl, min_id;
  1770. cgtime(&current);
  1771. if (opt_debug)
  1772. statbuf = cgcalloc(STATBUFLEN_WITH_DBG, 1);
  1773. else
  1774. statbuf = cgcalloc(STATBUFLEN_WITHOUT_DBG, 1);
  1775. for (i = 1; i < AVA8_DEFAULT_MODULARS; i++) {
  1776. if (!info->enable[i])
  1777. continue;
  1778. sprintf(buf, "Ver[%s]", info->mm_version[i]);
  1779. strcpy(statbuf, buf);
  1780. sprintf(buf, " DNA[%02x%02x%02x%02x%02x%02x%02x%02x]",
  1781. info->mm_dna[i][0],
  1782. info->mm_dna[i][1],
  1783. info->mm_dna[i][2],
  1784. info->mm_dna[i][3],
  1785. info->mm_dna[i][4],
  1786. info->mm_dna[i][5],
  1787. info->mm_dna[i][6],
  1788. info->mm_dna[i][7]);
  1789. strcat(statbuf, buf);
  1790. sprintf(buf, " Elapsed[%.0f]", tdiff(&current, &(info->elapsed[i])));
  1791. strcat(statbuf, buf);
  1792. strcat(statbuf, " MW[");
  1793. info->local_works[i] = 0;
  1794. for (j = 0; j < info->miner_count[i]; j++) {
  1795. info->local_works[i] += info->local_works_i[i][j];
  1796. sprintf(buf, "%"PRIu64" ", info->local_works_i[i][j]);
  1797. strcat(statbuf, buf);
  1798. }
  1799. statbuf[strlen(statbuf) - 1] = ']';
  1800. sprintf(buf, " LW[%"PRIu64"]", info->local_works[i]);
  1801. strcat(statbuf, buf);
  1802. strcat(statbuf, " MH[");
  1803. info->hw_works[i] = 0;
  1804. for (j = 0; j < info->miner_count[i]; j++) {
  1805. info->hw_works[i] += info->hw_works_i[i][j];
  1806. sprintf(buf, "%"PRIu64" ", info->hw_works_i[i][j]);
  1807. strcat(statbuf, buf);
  1808. }
  1809. statbuf[strlen(statbuf) - 1] = ']';
  1810. sprintf(buf, " HW[%"PRIu64"]", info->hw_works[i]);
  1811. strcat(statbuf, buf);
  1812. {
  1813. double a, b, dh;
  1814. a = 0;
  1815. b = 0;
  1816. for (j = 0; j < info->miner_count[i]; j++) {
  1817. for (k = 0; k < info->asic_count[i]; k++) {
  1818. a += info->get_asic[i][j][k][0];
  1819. b += info->get_asic[i][j][k][1];
  1820. }
  1821. }
  1822. dh = b ? (b / (a + b)) * 100 : 0;
  1823. sprintf(buf, " DH[%.3f%%]", dh);
  1824. strcat(statbuf, buf);
  1825. }
  1826. sprintf(buf, " Temp[%d]", info->temp_mm[i]);
  1827. strcat(statbuf, buf);
  1828. sprintf(buf, " TMax[%d]", get_temp_max(info, i));
  1829. strcat(statbuf, buf);
  1830. sprintf(buf, " Fan[%d]", info->fan_cpm[i]);
  1831. strcat(statbuf, buf);
  1832. sprintf(buf, " FanR[%d%%]", info->fan_pct[i]);
  1833. strcat(statbuf, buf);
  1834. sprintf(buf, " Vi[");
  1835. strcat(statbuf, buf);
  1836. for (j = 0; j < info->miner_count[i]; j++) {
  1837. sprintf(buf, "%d ", info->get_vin[i][j]);
  1838. strcat(statbuf, buf);
  1839. }
  1840. statbuf[strlen(statbuf) - 1] = ']';
  1841. sprintf(buf, " Vo[");
  1842. strcat(statbuf, buf);
  1843. for (j = 0; j < info->miner_count[i]; j++) {
  1844. sprintf(buf, "%d ", info->get_voltage[i][j]);
  1845. strcat(statbuf, buf);
  1846. }
  1847. statbuf[strlen(statbuf) - 1] = ']';
  1848. if (opt_debug) {
  1849. for (j = 0; j < info->miner_count[i]; j++) {
  1850. sprintf(buf, " PLL%d[", j);
  1851. strcat(statbuf, buf);
  1852. for (k = 0; k < AVA8_DEFAULT_PLL_CNT; k++) {
  1853. sprintf(buf, "%d ", info->get_pll[i][j][k]);
  1854. strcat(statbuf, buf);
  1855. }
  1856. statbuf[strlen(statbuf) - 1] = ']';
  1857. }
  1858. }
  1859. mhsmm = avalon8_hash_cal(avalon8, i);
  1860. sprintf(buf, " GHSmm[%.2f] WU[%.2f] Freq[%.2f]", (float)mhsmm / 1000,
  1861. info->diff1[i] / tdiff(&current, &(info->elapsed[i])) * 60.0,
  1862. (float)mhsmm / (info->asic_count[i] * info->miner_count[i] * 256));
  1863. strcat(statbuf, buf);
  1864. sprintf(buf, " PG[%d]", info->power_good[i]);
  1865. strcat(statbuf, buf);
  1866. sprintf(buf, " Led[%d]", info->led_indicator[i]);
  1867. strcat(statbuf, buf);
  1868. for (j = 0; j < info->miner_count[i]; j++) {
  1869. sprintf(buf, " MW%d[", j);
  1870. strcat(statbuf, buf);
  1871. for (k = 0; k < info->asic_count[i]; k++) {
  1872. sprintf(buf, "%"PRIu64" ", info->chip_matching_work[i][j][k]);
  1873. strcat(statbuf, buf);
  1874. }
  1875. statbuf[strlen(statbuf) - 1] = ']';
  1876. }
  1877. sprintf(buf, " TA[%d]", info->total_asics[i]);
  1878. strcat(statbuf, buf);
  1879. strcat(statbuf, " ECHU[");
  1880. for (j = 0; j < info->miner_count[i]; j++) {
  1881. sprintf(buf, "%d ", info->error_code[i][j]);
  1882. strcat(statbuf, buf);
  1883. }
  1884. statbuf[strlen(statbuf) - 1] = ']';
  1885. sprintf(buf, " ECMM[%d]", info->error_code[i][j]);
  1886. strcat(statbuf, buf);
  1887. if (opt_debug) {
  1888. sprintf(buf, " FAC0[%d]", info->factory_info[0]);
  1889. strcat(statbuf, buf);
  1890. sprintf(buf, " OC[%d]", info->overclocking_info[0]);
  1891. strcat(statbuf, buf);
  1892. for (j = 0; j < info->miner_count[i]; j++) {
  1893. sprintf(buf, " SF%d[", j);
  1894. strcat(statbuf, buf);
  1895. for (k = 0; k < AVA8_DEFAULT_PLL_CNT; k++) {
  1896. sprintf(buf, "%d ", info->get_frequency[i][j][k]);
  1897. strcat(statbuf, buf);
  1898. }
  1899. statbuf[strlen(statbuf) - 1] = ']';
  1900. }
  1901. strcat(statbuf, " PMUV[");
  1902. for (j = 0; j < AVA8_DEFAULT_PMU_CNT; j++) {
  1903. sprintf(buf, "%s ", info->pmu_version[i][j]);
  1904. strcat(statbuf, buf);
  1905. }
  1906. statbuf[strlen(statbuf) - 1] = ']';
  1907. for (j = 0; j < info->miner_count[i]; j++) {
  1908. sprintf(buf, " PVT_T%d[", j);
  1909. strcat(statbuf, buf);
  1910. for (k = 0; k < info->asic_count[i]; k++) {
  1911. sprintf(buf, "%3d ", info->temp[i][j][k]);
  1912. strcat(statbuf, buf);
  1913. }
  1914. statbuf[strlen(statbuf) - 1] = ']';
  1915. statbuf[strlen(statbuf)] = '\0';
  1916. }
  1917. for (j = 0; j < info->miner_count[i]; j++) {
  1918. sprintf(buf, " PVT_V%d[", j);
  1919. strcat(statbuf, buf);
  1920. for (k = 0; k < info->asic_count[i]; k++) {
  1921. sprintf(buf, "%d ", info->core_volt[i][j][k]);
  1922. strcat(statbuf, buf);
  1923. }
  1924. statbuf[strlen(statbuf) - 1] = ']';
  1925. statbuf[strlen(statbuf)] = '\0';
  1926. }
  1927. for (j = 0; j < info->miner_count[i]; j++) {
  1928. sprintf(buf, " ERATIO%d[", j);
  1929. strcat(statbuf, buf);
  1930. for (k = 0; k < info->asic_count[i]; k++) {
  1931. if (info->get_asic[i][j][k][0])
  1932. 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])));
  1933. else
  1934. sprintf(buf, "%6.2f%% ", 0.0);
  1935. strcat(statbuf, buf);
  1936. }
  1937. statbuf[strlen(statbuf) - 1] = ']';
  1938. }
  1939. int l;
  1940. /* i: modular, j: miner, k:asic, l:value */
  1941. for (l = 0; l < 2; l++) {
  1942. for (j = 0; j < info->miner_count[i]; j++) {
  1943. sprintf(buf, " C_%02d_%02d[", j, l);
  1944. strcat(statbuf, buf);
  1945. for (k = 0; k < info->asic_count[i]; k++) {
  1946. sprintf(buf, "%7d ", info->get_asic[i][j][k][l]);
  1947. strcat(statbuf, buf);
  1948. }
  1949. statbuf[strlen(statbuf) - 1] = ']';
  1950. }
  1951. }
  1952. for (j = 0; j < info->miner_count[i]; j++) {
  1953. sprintf(buf, " GHSmm%02d[", j);
  1954. strcat(statbuf, buf);
  1955. for (k = 0; k < info->asic_count[i]; k++) {
  1956. mhsmm = 0;
  1957. for (l = 2; l < (2 + AVA8_DEFAULT_PLL_CNT); l++) {
  1958. mhsmm += (info->get_asic[i][j][k][l] * info->get_frequency[i][j][l - 2]);
  1959. }
  1960. sprintf(buf, "%7.2f ", mhsmm / 1000);
  1961. strcat(statbuf, buf);
  1962. }
  1963. statbuf[strlen(statbuf) - 1] = ']';
  1964. }
  1965. }
  1966. sprintf(buf, " FM[%d]", info->freq_mode[i]);
  1967. strcat(statbuf, buf);
  1968. strcat(statbuf, " CRC[");
  1969. for (j = 0; j < info->miner_count[i]; j++) {
  1970. sprintf(buf, "%d ", info->error_crc[i][j]);
  1971. strcat(statbuf, buf);
  1972. }
  1973. statbuf[strlen(statbuf) - 1] = ']';
  1974. sprintf(buf, "MM ID%d", i);
  1975. root = api_add_string(root, buf, statbuf, true);
  1976. }
  1977. free(statbuf);
  1978. root = api_add_int(root, "MM Count", &(info->mm_count), true);
  1979. root = api_add_int(root, "Smart Speed", &opt_avalon8_smart_speed, true);
  1980. if (info->connecter == AVA8_CONNECTER_IIC)
  1981. root = api_add_string(root, "Connecter", "IIC", true);
  1982. if (info->connecter == AVA8_CONNECTER_AUC) {
  1983. root = api_add_string(root, "Connecter", "AUC", true);
  1984. root = api_add_string(root, "AUC VER", info->auc_version, false);
  1985. root = api_add_int(root, "AUC I2C Speed", &(info->auc_speed), true);
  1986. root = api_add_int(root, "AUC I2C XDelay", &(info->auc_xdelay), true);
  1987. root = api_add_int(root, "AUC Sensor", &(info->auc_sensor), true);
  1988. auc_temp = decode_auc_temp(info->auc_sensor);
  1989. root = api_add_temp(root, "AUC Temperature", &auc_temp, true);
  1990. }
  1991. root = api_add_bool(root, "Connection Overloaded", &info->conn_overloaded, true);
  1992. root = api_add_int(root, "Voltage Level Offset", &opt_avalon8_voltage_level_offset, true);
  1993. root = api_add_uint32(root, "Nonce Mask", &opt_avalon8_nonce_mask, true);
  1994. return root;
  1995. }
  1996. /* format: voltage[-addr[-miner]]
  1997. * addr[0, AVA8_DEFAULT_MODULARS - 1], 0 means all modulars
  1998. * miner[0, miner_count], 0 means all miners
  1999. */
  2000. char *set_avalon8_device_voltage_level(struct cgpu_info *avalon8, char *arg)
  2001. {
  2002. struct avalon8_info *info = avalon8->device_data;
  2003. int val;
  2004. unsigned int addr = 0, i, j;
  2005. uint32_t miner_id = 0;
  2006. if (!(*arg))
  2007. return NULL;
  2008. sscanf(arg, "%d-%d-%d", &val, &addr, &miner_id);
  2009. if (val < AVA8_DEFAULT_VOLTAGE_LEVEL_MIN || val > AVA8_DEFAULT_VOLTAGE_LEVEL_MAX)
  2010. return "Invalid value passed to set_avalon8_device_voltage_level";
  2011. if (addr >= AVA8_DEFAULT_MODULARS) {
  2012. applog(LOG_ERR, "invalid modular index: %d, valid range 0-%d", addr, (AVA8_DEFAULT_MODULARS - 1));
  2013. return "Invalid modular index to set_avalon8_device_voltage_level";
  2014. }
  2015. if (!addr) {
  2016. for (i = 1; i < AVA8_DEFAULT_MODULARS; i++) {
  2017. if (!info->enable[i])
  2018. continue;
  2019. if (miner_id > info->miner_count[i]) {
  2020. applog(LOG_ERR, "invalid miner index: %d, valid range 0-%d", miner_id, info->miner_count[i]);
  2021. return "Invalid miner index to set_avalon8_device_voltage_level";
  2022. }
  2023. if (miner_id)
  2024. info->set_voltage_level[i][miner_id - 1] = val;
  2025. else {
  2026. for (j = 0; j < info->miner_count[i]; j++)
  2027. info->set_voltage_level[i][j] = val;
  2028. }
  2029. avalon8_set_voltage_level(avalon8, i, info->set_voltage_level[i]);
  2030. }
  2031. } else {
  2032. if (!info->enable[addr]) {
  2033. applog(LOG_ERR, "Disabled modular:%d", addr);
  2034. return "Disabled modular to set_avalon8_device_voltage_level";
  2035. }
  2036. if (miner_id > info->miner_count[addr]) {
  2037. applog(LOG_ERR, "invalid miner index: %d, valid range 0-%d", miner_id, info->miner_count[addr]);
  2038. return "Invalid miner index to set_avalon8_device_voltage_level";
  2039. }
  2040. if (miner_id)
  2041. info->set_voltage_level[addr][miner_id - 1] = val;
  2042. else {
  2043. for (j = 0; j < info->miner_count[addr]; j++)
  2044. info->set_voltage_level[addr][j] = val;
  2045. }
  2046. avalon8_set_voltage_level(avalon8, addr, info->set_voltage_level[addr]);
  2047. }
  2048. applog(LOG_NOTICE, "%s-%d: Update voltage-level to %d", avalon8->drv->name, avalon8->device_id, val);
  2049. return NULL;
  2050. }
  2051. /*
  2052. * format: freq[-addr[-miner]]
  2053. * addr[0, AVA8_DEFAULT_MODULARS - 1], 0 means all modulars
  2054. * miner[0, miner_count], 0 means all miners
  2055. */
  2056. char *set_avalon8_device_freq(struct cgpu_info *avalon8, char *arg)
  2057. {
  2058. struct avalon8_info *info = avalon8->device_data;
  2059. unsigned int val, addr = 0, i, j, k;
  2060. uint32_t miner_id = 0;
  2061. if (!(*arg))
  2062. return NULL;
  2063. sscanf(arg, "%d-%d-%d", &val, &addr, &miner_id);
  2064. if (val > AVA8_DEFAULT_FREQUENCY_MAX)
  2065. return "Invalid value passed to set_avalon8_device_freq";
  2066. if (addr >= AVA8_DEFAULT_MODULARS) {
  2067. applog(LOG_ERR, "invalid modular index: %d, valid range 0-%d", addr, (AVA8_DEFAULT_MODULARS - 1));
  2068. return "Invalid modular index to set_avalon8_device_freq";
  2069. }
  2070. if (!addr) {
  2071. for (i = 1; i < AVA8_DEFAULT_MODULARS; i++) {
  2072. if (!info->enable[i])
  2073. continue;
  2074. if (miner_id > info->miner_count[i]) {
  2075. applog(LOG_ERR, "invalid miner index: %d, valid range 0-%d", miner_id, info->miner_count[i]);
  2076. return "Invalid miner index to set_avalon8_device_freq";
  2077. }
  2078. if (miner_id) {
  2079. for (k = 0; k < AVA8_DEFAULT_PLL_CNT; k++)
  2080. info->set_frequency[i][miner_id - 1][k] = val;
  2081. avalon8_set_freq(avalon8, i, miner_id - 1, info->set_frequency[i][miner_id - 1]);
  2082. } else {
  2083. for (j = 0; j < info->miner_count[i]; j++) {
  2084. for (k = 0; k < AVA8_DEFAULT_PLL_CNT; k++)
  2085. info->set_frequency[i][j][k] = val;
  2086. avalon8_set_freq(avalon8, i, j, info->set_frequency[i][j]);
  2087. }
  2088. }
  2089. }
  2090. } else {
  2091. if (!info->enable[addr]) {
  2092. applog(LOG_ERR, "Disabled modular:%d", addr);
  2093. return "Disabled modular to set_avalon8_device_freq";
  2094. }
  2095. if (miner_id > info->miner_count[addr]) {
  2096. applog(LOG_ERR, "invalid miner index: %d, valid range 0-%d", miner_id, info->miner_count[addr]);
  2097. return "Invalid miner index to set_avalon8_device_freq";
  2098. }
  2099. if (miner_id) {
  2100. for (k = 0; k < AVA8_DEFAULT_PLL_CNT; k++)
  2101. info->set_frequency[addr][miner_id - 1][k] = val;
  2102. avalon8_set_freq(avalon8, addr, miner_id - 1, info->set_frequency[addr][miner_id - 1]);
  2103. } else {
  2104. for (j = 0; j < info->miner_count[addr]; j++) {
  2105. for (k = 0; k < AVA8_DEFAULT_PLL_CNT; k++)
  2106. info->set_frequency[addr][j][k] = val;
  2107. avalon8_set_freq(avalon8, addr, j, info->set_frequency[addr][j]);
  2108. }
  2109. }
  2110. }
  2111. applog(LOG_NOTICE, "%s-%d: Update frequency to %d",
  2112. avalon8->drv->name, avalon8->device_id, val);
  2113. return NULL;
  2114. }
  2115. char *set_avalon8_factory_info(struct cgpu_info *avalon8, char *arg)
  2116. {
  2117. struct avalon8_info *info = avalon8->device_data;
  2118. char type[AVA8_DEFAULT_FACTORY_INFO_1_CNT];
  2119. int val;
  2120. if (!(*arg))
  2121. return NULL;
  2122. memset(type, 0, AVA8_DEFAULT_FACTORY_INFO_1_CNT);
  2123. sscanf(arg, "%d-%s", &val, type);
  2124. if ((val != AVA8_DEFAULT_FACTORY_INFO_0_IGNORE) &&
  2125. (val < AVA8_DEFAULT_FACTORY_INFO_0_MIN || val > AVA8_DEFAULT_FACTORY_INFO_0_MAX))
  2126. return "Invalid value passed to set_avalon8_factory_info";
  2127. info->factory_info[0] = val;
  2128. memcpy(&info->factory_info[1], type, AVA8_DEFAULT_FACTORY_INFO_1_CNT);
  2129. avalon8_set_factory_info(avalon8, 0, (uint8_t *)info->factory_info);
  2130. applog(LOG_NOTICE, "%s-%d: Update factory info %d",
  2131. avalon8->drv->name, avalon8->device_id, val);
  2132. return NULL;
  2133. }
  2134. char *set_avalon8_overclocking_info(struct cgpu_info *avalon8, char *arg)
  2135. {
  2136. struct avalon8_info *info = avalon8->device_data;
  2137. int val;
  2138. if (!(*arg))
  2139. return NULL;
  2140. sscanf(arg, "%d", &val);
  2141. if (val != AVA8_DEFAULT_OVERCLOCKING_OFF && val != AVA8_DEFAULT_OVERCLOCKING_ON)
  2142. return "Invalid value passed to set_avalon8_overclocking_info";
  2143. info->overclocking_info[0] = val;
  2144. avalon8_set_overclocking_info(avalon8, 0, (uint8_t *)info->overclocking_info);
  2145. applog(LOG_NOTICE, "%s-%d: Update Overclocking info %d",
  2146. avalon8->drv->name, avalon8->device_id, val);
  2147. return NULL;
  2148. }
  2149. static char *avalon8_set_device(struct cgpu_info *avalon8, char *option, char *setting, char *replybuf, size_t siz)
  2150. {
  2151. unsigned int val;
  2152. struct avalon8_info *info = avalon8->device_data;
  2153. if (strcasecmp(option, "help") == 0) {
  2154. snprintf(replybuf, siz, "pdelay|fan|frequency|led|voltage");
  2155. return replybuf;
  2156. }
  2157. if (strcasecmp(option, "pdelay") == 0) {
  2158. if (!setting || !*setting) {
  2159. snprintf(replybuf, siz, "missing polling delay setting");
  2160. return replybuf;
  2161. }
  2162. val = (unsigned int)atoi(setting);
  2163. if (val < 1 || val > 65535) {
  2164. snprintf(replybuf, siz, "invalid polling delay: %d, valid range 1-65535", val);
  2165. return replybuf;
  2166. }
  2167. opt_avalon8_polling_delay = val;
  2168. applog(LOG_NOTICE, "%s-%d: Update polling delay to: %d",
  2169. avalon8->drv->name, avalon8->device_id, val);
  2170. return NULL;
  2171. }
  2172. if (strcasecmp(option, "fan") == 0) {
  2173. if (!setting || !*setting) {
  2174. snprintf(replybuf, siz, "missing fan value");
  2175. return replybuf;
  2176. }
  2177. if (set_avalon8_fan(setting)) {
  2178. snprintf(replybuf, siz, "invalid fan value, valid range 0-100");
  2179. return replybuf;
  2180. }
  2181. applog(LOG_NOTICE, "%s-%d: Update fan to %d-%d",
  2182. avalon8->drv->name, avalon8->device_id,
  2183. opt_avalon8_fan_min, opt_avalon8_fan_max);
  2184. return NULL;
  2185. }
  2186. if (strcasecmp(option, "frequency") == 0) {
  2187. if (!setting || !*setting) {
  2188. snprintf(replybuf, siz, "missing frequency value");
  2189. return replybuf;
  2190. }
  2191. return set_avalon8_device_freq(avalon8, setting);
  2192. }
  2193. if (strcasecmp(option, "led") == 0) {
  2194. int val_led = -1;
  2195. if (!setting || !*setting) {
  2196. snprintf(replybuf, siz, "missing module_id setting");
  2197. return replybuf;
  2198. }
  2199. sscanf(setting, "%d-%d", &val, &val_led);
  2200. if (val < 1 || val >= AVA8_DEFAULT_MODULARS) {
  2201. snprintf(replybuf, siz, "invalid module_id: %d, valid range 1-%d", val, AVA8_DEFAULT_MODULARS);
  2202. return replybuf;
  2203. }
  2204. if (!info->enable[val]) {
  2205. snprintf(replybuf, siz, "the current module was disabled %d", val);
  2206. return replybuf;
  2207. }
  2208. if (val_led == -1)
  2209. info->led_indicator[val] = !info->led_indicator[val];
  2210. else {
  2211. if (val_led < 0 || val_led > 1) {
  2212. snprintf(replybuf, siz, "invalid LED status: %d, valid value 0|1", val_led);
  2213. return replybuf;
  2214. }
  2215. if (val_led != info->led_indicator[val])
  2216. info->led_indicator[val] = val_led;
  2217. }
  2218. applog(LOG_NOTICE, "%s-%d: Module:%d, LED: %s",
  2219. avalon8->drv->name, avalon8->device_id,
  2220. val, info->led_indicator[val] ? "on" : "off");
  2221. return NULL;
  2222. }
  2223. if (strcasecmp(option, "voltage-level") == 0) {
  2224. if (!setting || !*setting) {
  2225. snprintf(replybuf, siz, "missing voltage-level value");
  2226. return replybuf;
  2227. }
  2228. return set_avalon8_device_voltage_level(avalon8, setting);
  2229. }
  2230. if (strcasecmp(option, "factory") == 0) {
  2231. if (!setting || !*setting) {
  2232. snprintf(replybuf, siz, "missing factory info");
  2233. return replybuf;
  2234. }
  2235. return set_avalon8_factory_info(avalon8, setting);
  2236. }
  2237. if (strcasecmp(option, "reboot") == 0) {
  2238. if (!setting || !*setting) {
  2239. snprintf(replybuf, siz, "missing reboot value");
  2240. return replybuf;
  2241. }
  2242. sscanf(setting, "%d", &val);
  2243. if (val < 1 || val >= AVA8_DEFAULT_MODULARS) {
  2244. snprintf(replybuf, siz, "invalid module_id: %d, valid range 1-%d", val, AVA8_DEFAULT_MODULARS);
  2245. return replybuf;
  2246. }
  2247. info->reboot[val] = true;
  2248. return NULL;
  2249. }
  2250. if (strcasecmp(option, "overclocking") == 0) {
  2251. if (!setting || !*setting) {
  2252. snprintf(replybuf, siz, "missing overclocking info");
  2253. return replybuf;
  2254. }
  2255. return set_avalon8_overclocking_info(avalon8, setting);
  2256. }
  2257. snprintf(replybuf, siz, "Unknown option: %s", option);
  2258. return replybuf;
  2259. }
  2260. static void avalon8_statline_before(char *buf, size_t bufsiz, struct cgpu_info *avalon8)
  2261. {
  2262. struct avalon8_info *info = avalon8->device_data;
  2263. int temp = -273;
  2264. int fanmin = AVA8_DEFAULT_FAN_MAX;
  2265. int i, j, k;
  2266. uint32_t frequency = 0;
  2267. float ghs_sum = 0, mhsmm = 0;
  2268. double pass_num = 0.0, fail_num = 0.0;
  2269. for (i = 1; i < AVA8_DEFAULT_MODULARS; i++) {
  2270. if (!info->enable[i])
  2271. continue;
  2272. if (fanmin >= info->fan_pct[i])
  2273. fanmin = info->fan_pct[i];
  2274. if (temp < get_temp_max(info, i))
  2275. temp = get_temp_max(info, i);
  2276. mhsmm = avalon8_hash_cal(avalon8, i);
  2277. frequency += (mhsmm / (info->asic_count[i] * info->miner_count[i] * 256));
  2278. ghs_sum += (mhsmm / 1000);
  2279. for (j = 0; j < info->miner_count[i]; j++) {
  2280. for (k = 0; k < info->asic_count[i]; k++) {
  2281. pass_num += info->get_asic[i][j][k][0];
  2282. fail_num += info->get_asic[i][j][k][1];
  2283. }
  2284. }
  2285. }
  2286. if (info->mm_count)
  2287. frequency /= info->mm_count;
  2288. tailsprintf(buf, bufsiz, "%4dMhz %.2fGHS %2dC %.2f%% %3d%%", frequency, ghs_sum, temp,
  2289. (fail_num + pass_num) ? fail_num * 100.0 / (fail_num + pass_num) : 0, fanmin);
  2290. }
  2291. struct device_drv avalon8_drv = {
  2292. .drv_id = DRIVER_avalon8,
  2293. .dname = "avalon8",
  2294. .name = "AV8",
  2295. .set_device = avalon8_set_device,
  2296. .get_api_stats = avalon8_api_stats,
  2297. .get_statline_before = avalon8_statline_before,
  2298. .drv_detect = avalon8_detect,
  2299. .thread_prepare = avalon8_prepare,
  2300. .hash_work = hash_driver_work,
  2301. .flush_work = avalon8_sswork_update,
  2302. .update_work = avalon8_sswork_update,
  2303. .scanwork = avalon8_scanhash,
  2304. .max_diff = AVA8_DRV_DIFFMAX,
  2305. .genwork = true,
  2306. };