driver-avalon7.c 78 KB

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