driver-avalon8.c 84 KB

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