driver-avalon8.c.bak 84 KB

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