123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769 |
- /*
- * Copyright 2017 xuzhenxing <xuzhenxing@canaan-creative.com>
- * Copyright 2016-2017 Mikeqin <Fengling.Qin@gmail.com>
- * Copyright 2016 Con Kolivas <kernel@kolivas.org>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 3 of the License, or (at your option)
- * any later version. See COPYING for more details.
- */
- #include <math.h>
- #include "config.h"
- #include "miner.h"
- #include "driver-avalon8.h"
- #include "crc.h"
- #include "sha2.h"
- #include "hexdump.c"
- #define get_fan_pwm(v) (AVA8_PWM_MAX - (v) * AVA8_PWM_MAX / 100)
- int opt_avalon8_temp_target = AVA8_DEFAULT_TEMP_TARGET;
- int opt_avalon8_fan_min = AVA8_DEFAULT_FAN_MIN;
- int opt_avalon8_fan_max = AVA8_DEFAULT_FAN_MAX;
- int opt_avalon8_voltage_level = AVA8_INVALID_VOLTAGE_LEVEL;
- int opt_avalon8_voltage_level_offset = AVA8_DEFAULT_VOLTAGE_LEVEL_OFFSET;
- int opt_avalon8_freq[AVA8_DEFAULT_PLL_CNT] =
- {
- AVA8_DEFAULT_FREQUENCY_IGNORE,
- AVA8_DEFAULT_FREQUENCY_IGNORE,
- AVA8_DEFAULT_FREQUENCY_IGNORE,
- AVA8_DEFAULT_FREQUENCY_IGNORE,
- AVA8_DEFAULT_FREQUENCY_IGNORE,
- AVA8_DEFAULT_FREQUENCY_IGNORE,
- AVA8_DEFAULT_FREQUENCY_IGNORE
- };
- int opt_avalon8_freq_sel = AVA8_DEFAULT_FREQUENCY_SEL;
- int opt_avalon8_polling_delay = AVA8_DEFAULT_POLLING_DELAY;
- int opt_avalon8_aucspeed = AVA8_AUC_SPEED;
- int opt_avalon8_aucxdelay = AVA8_AUC_XDELAY;
- int opt_avalon8_smart_speed = AVA8_DEFAULT_SMART_SPEED;
- /*
- * smart speed have 2 modes
- * 1. auto speed by A3210 chips
- * 2. option 1 + adjust by average frequency
- */
- bool opt_avalon8_iic_detect = AVA8_DEFAULT_IIC_DETECT;
- uint32_t opt_avalon8_th_pass = AVA8_DEFAULT_TH_PASS;
- uint32_t opt_avalon8_th_fail = AVA8_DEFAULT_TH_FAIL;
- uint32_t opt_avalon8_th_init = AVA8_DEFAULT_TH_INIT;
- uint32_t opt_avalon8_th_ms = AVA8_DEFAULT_TH_MS;
- uint32_t opt_avalon8_th_timeout = AVA8_DEFAULT_TH_TIMEOUT;
- uint32_t opt_avalon8_th_add = AVA8_DEFAULT_TH_ADD;
- uint32_t opt_avalon8_th_mssel = AVA8_DEFAULT_TH_MSSEL;
- uint32_t opt_avalon8_nonce_mask = AVA8_DEFAULT_NONCE_MASK;
- uint32_t opt_avalon8_nonce_check = AVA8_DEFAULT_NONCE_CHECK;
- uint32_t opt_avalon8_mux_l2h = AVA8_DEFAULT_MUX_L2H;
- uint32_t opt_avalon8_mux_h2l = AVA8_DEFAULT_MUX_H2L;
- uint32_t opt_avalon8_h2ltime0_spd = AVA8_DEFAULT_H2LTIME0_SPD;
- uint32_t opt_avalon8_roll_enable = AVA8_DEFAULT_ROLL_ENABLE;
- uint32_t opt_avalon8_spdlow = AVA8_DEFAULT_SPDLOW;
- uint32_t opt_avalon8_spdhigh = AVA8_DEFAULT_SPDHIGH;
- uint32_t opt_avalon8_lv2_th_ms = AVA8_DEFAULT_LV2_TH_MS;
- uint32_t opt_avalon8_lv3_th_ms = AVA8_DEFAULT_LV3_TH_MS;
- uint32_t opt_avalon8_lv4_th_ms = AVA8_DEFAULT_LV4_TH_MS;
- uint32_t opt_avalon8_lv5_th_ms = AVA8_DEFAULT_LV5_TH_MS;
- uint32_t opt_avalon8_lv6_th_ms = AVA8_DEFAULT_LV6_TH_MS;
- uint32_t opt_avalon8_lv7_th_ms = AVA8_DEFAULT_LV7_TH_MS;
- uint32_t opt_avalon8_lv2_th_add = AVA8_DEFAULT_LV2_TH_ADD;
- uint32_t opt_avalon8_lv3_th_add = AVA8_DEFAULT_LV3_TH_ADD;
- uint32_t opt_avalon8_lv4_th_add = AVA8_DEFAULT_LV4_TH_ADD;
- uint32_t opt_avalon8_lv5_th_add = AVA8_DEFAULT_LV5_TH_ADD;
- uint32_t opt_avalon8_lv6_th_add = AVA8_DEFAULT_LV6_TH_ADD;
- uint32_t opt_avalon8_lv7_th_add = AVA8_DEFAULT_LV7_TH_ADD;
- uint32_t cpm_table[] =
- {
- 0x00000000,
- 0x0c041205,
- 0x0c041203,
- 0x0c031103,
- 0x0c041103,
- 0x0c079183,
- 0x0c079503,
- 0x0c07ed83,
- 0x0c040603,
- 0x0c06c703,
- 0x0c078703,
- 0x0c042583,
- 0x0c078683,
- 0x0c068603,
- 0x0c070603,
- 0x0c078603,
- 0x0c040503,
- 0x0c044503,
- 0x0c048503,
- 0x0c04c503,
- 0x0c050503,
- 0x0c054503,
- 0x0c058503,
- 0x0c05c503,
- 0x0c060503,
- 0x0c064503,
- 0x0c068503,
- 0x0c06c503,
- 0x0c070503,
- 0x0c074503,
- 0x0c078503,
- 0x0c07c503,
- 0x0c040483,
- 0x0c042483,
- 0x0c044483,
- 0x0c046483,
- 0x0c048483,
- 0x0c04a483,
- 0x0c04c483,
- 0x0c04e483,
- 0x0c050483,
- 0x0c052483,
- 0x0c054483,
- 0x0c056483,
- 0x0c058483,
- 0x0c05a483,
- 0x0c05c483,
- 0x0c05e483,
- 0x0c060483,
- 0x0c062483,
- 0x0c064483,
- 0x0c066483,
- 0x0c068483,
- 0x0c06a483,
- 0x0c06c483,
- 0x0c06e483
- };
- struct avalon8_dev_description avalon8_dev_table[] = {
- {
- "921",
- 921,
- 4,
- 26,
- AVA8_MM851_VIN_ADC_RATIO,
- AVA8_MM851_VOUT_ADC_RATIO,
- -2,
- {
- AVA8_DEFAULT_FREQUENCY_0M,
- AVA8_DEFAULT_FREQUENCY_0M,
- AVA8_DEFAULT_FREQUENCY_0M,
- AVA8_DEFAULT_FREQUENCY_0M,
- AVA8_DEFAULT_FREQUENCY_750M,
- AVA8_DEFAULT_FREQUENCY_775M,
- AVA8_DEFAULT_FREQUENCY_800M
- }
- }
- };
- static uint32_t api_get_cpm(uint32_t freq)
- {
- return cpm_table[freq / 25];
- }
- static uint32_t encode_voltage(int volt_level)
- {
- if (volt_level > AVA8_DEFAULT_VOLTAGE_LEVEL_MAX)
- volt_level = AVA8_DEFAULT_VOLTAGE_LEVEL_MAX;
- else if (volt_level < AVA8_DEFAULT_VOLTAGE_LEVEL_MIN)
- volt_level = AVA8_DEFAULT_VOLTAGE_LEVEL_MIN;
- if (volt_level < 0)
- return 0x8080 | (-volt_level);
- return 0x8000 | volt_level;
- }
- static uint32_t decode_voltage(struct avalon8_info *info, int modular_id, uint32_t volt)
- {
- return (volt * info->vout_adc_ratio[modular_id] / info->asic_count[modular_id] / 100);
- }
- static uint16_t decode_vin(struct avalon8_info *info, int modular_id, uint16_t volt)
- {
- return (volt * info->vin_adc_ratio[modular_id] / 1000);
- }
- static double decode_pvt_temp(uint16_t pvt_code)
- {
- double g = 60.0;
- double h = 200.0;
- double cal5 = 4094.0;
- double j = -0.1;
- double fclkm = 6.25;
- /* Mode2 temperature equation */
- return g + h * (pvt_code / cal5 - 0.5) + j * fclkm;
- }
- static uint32_t decode_pvt_volt(uint16_t volt)
- {
- double vref = 1.20;
- double r = 16384.0; /* 2 ** 14 */
- double c;
- c = vref / 5.0 * (6 * (volt - 0.5) / r - 1.0);
- if (c < 0)
- c = 0;
- return c * 1000;
- }
- #define SERIESRESISTOR 10000
- #define THERMISTORNOMINAL 10000
- #define BCOEFFICIENT 3500
- #define TEMPERATURENOMINAL 25
- float decode_auc_temp(int value)
- {
- float ret, resistance;
- if (!((value > 0) && (value < 33000)))
- return -273;
- resistance = (3.3 * 10000 / value) - 1;
- resistance = SERIESRESISTOR / resistance;
- ret = resistance / THERMISTORNOMINAL;
- ret = logf(ret);
- ret /= BCOEFFICIENT;
- ret += 1.0 / (TEMPERATURENOMINAL + 273.15);
- ret = 1.0 / ret;
- ret -= 273.15;
- return ret;
- }
- #define UNPACK32(x, str) \
- { \
- *((str) + 3) = (uint8_t) ((x) ); \
- *((str) + 2) = (uint8_t) ((x) >> 8); \
- *((str) + 1) = (uint8_t) ((x) >> 16); \
- *((str) + 0) = (uint8_t) ((x) >> 24); \
- }
- static inline void sha256_prehash(const unsigned char *message, unsigned int len, unsigned char *digest)
- {
- int i;
- sha256_ctx ctx;
- sha256_init(&ctx);
- sha256_update(&ctx, message, len);
- for (i = 0; i < 8; i++)
- UNPACK32(ctx.h[i], &digest[i << 2]);
- }
- char *set_avalon8_fan(char *arg)
- {
- int val1, val2, ret;
- ret = sscanf(arg, "%d-%d", &val1, &val2);
- if (ret < 1)
- return "No value passed to avalon8-fan";
- if (ret == 1)
- val2 = val1;
- if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
- return "Invalid value passed to avalon8-fan";
- opt_avalon8_fan_min = val1;
- opt_avalon8_fan_max = val2;
- return NULL;
- }
- char *set_avalon8_freq(char *arg)
- {
- int val[AVA8_DEFAULT_PLL_CNT];
- char *colon, *data;
- int i;
- if (!(*arg))
- return NULL;
- data = arg;
- memset(val, 0, sizeof(val));
- for (i = 0; i < AVA8_DEFAULT_PLL_CNT; i++) {
- colon = strchr(data, ':');
- if (colon)
- *(colon++) = '\0';
- else {
- /* last value */
- if (*data) {
- val[i] = atoi(data);
- if (val[i] > AVA8_DEFAULT_FREQUENCY_MAX)
- return "Invalid value passed to avalon8-freq";
- }
- break;
- }
- if (*data) {
- val[i] = atoi(data);
- if (val[i] > AVA8_DEFAULT_FREQUENCY_MAX)
- return "Invalid value passed to avalon8-freq";
- }
- data = colon;
- }
- for (i = 0; i < AVA8_DEFAULT_PLL_CNT; i++)
- opt_avalon8_freq[i] = val[i];
- return NULL;
- }
- char *set_avalon8_voltage_level(char *arg)
- {
- int val, ret;
- ret = sscanf(arg, "%d", &val);
- if (ret < 1)
- return "No value passed to avalon8-voltage-level";
- if (val < AVA8_DEFAULT_VOLTAGE_LEVEL_MIN || val > AVA8_DEFAULT_VOLTAGE_LEVEL_MAX)
- return "Invalid value passed to avalon8-voltage-level";
- opt_avalon8_voltage_level = val;
- return NULL;
- }
- char *set_avalon8_voltage_level_offset(char *arg)
- {
- int val, ret;
- ret = sscanf(arg, "%d", &val);
- if (ret < 1)
- return "No value passed to avalon8-voltage-level-offset";
- if (val < AVA8_DEFAULT_VOLTAGE_LEVEL_OFFSET_MIN || val > AVA8_DEFAULT_VOLTAGE_LEVEL_OFFSET_MAX)
- return "Invalid value passed to avalon8-voltage-level-offset";
- opt_avalon8_voltage_level_offset = val;
- return NULL;
- }
- static int avalon8_init_pkg(struct avalon8_pkg *pkg, uint8_t type, uint8_t idx, uint8_t cnt)
- {
- unsigned short crc;
- pkg->head[0] = AVA8_H1;
- pkg->head[1] = AVA8_H2;
- pkg->type = type;
- pkg->opt = 0;
- pkg->idx = idx;
- pkg->cnt = cnt;
- crc = crc16(pkg->data, AVA8_P_DATA_LEN);
- pkg->crc[0] = (crc & 0xff00) >> 8;
- pkg->crc[1] = crc & 0xff;
- return 0;
- }
- static int job_idcmp(uint8_t *job_id, char *pool_job_id)
- {
- int job_id_len;
- unsigned short crc, crc_expect;
- if (!pool_job_id)
- return 1;
- job_id_len = strlen(pool_job_id);
- crc_expect = crc16((unsigned char *)pool_job_id, job_id_len);
- crc = job_id[0] << 8 | job_id[1];
- if (crc_expect == crc)
- return 0;
- applog(LOG_DEBUG, "avalon8: job_id doesn't match! [%04x:%04x (%s)]",
- crc, crc_expect, pool_job_id);
- return 1;
- }
- static inline int get_temp_max(struct avalon8_info *info, int addr)
- {
- int i, j;
- int max = -273;
- for (i = 0; i < info->miner_count[addr]; i++) {
- for (j = 0; j < info->asic_count[addr]; j++) {
- if (info->temp[addr][i][j] > max)
- max = info->temp[addr][i][j];
- }
- }
- if (max < info->temp_mm[addr])
- max = info->temp_mm[addr];
- return max;
- }
- /* Use a PID-like feedback mechanism for optimal temperature and fan speed */
- static inline uint32_t adjust_fan(struct avalon8_info *info, int id)
- {
- int t, tdiff, delta;
- uint32_t pwm;
- time_t now_t;
- now_t = time(NULL);
- t = get_temp_max(info, id);
- tdiff = t - info->temp_last_max[id];
- if (!tdiff && now_t < info->last_temp_time[id] + AVA8_DEFAULT_FAN_INTERVAL)
- goto out;
- info->last_temp_time[id] = now_t;
- delta = t - info->temp_target[id];
- /* Check for init value and ignore it */
- if (unlikely(info->temp_last_max[id] == -273))
- tdiff = 0;
- info->temp_last_max[id] = t;
- if (t >= info->temp_overheat[id]) {
- /* Hit the overheat temperature limit */
- if (info->fan_pct[id] < opt_avalon8_fan_max) {
- applog(LOG_WARNING, "Overheat detected on AV8-%d, increasing fan to max", id);
- info->fan_pct[id] = opt_avalon8_fan_max;
- }
- } else if (delta > 0) {
- /* Over target temperature. */
- /* Is the temp already coming down */
- if (tdiff < 0)
- goto out;
- /* Adjust fanspeed by temperature over and any further rise */
- info->fan_pct[id] += opt_avalon8_fan_max; /* info->fan_pct[id] += delta + tdiff; */
- } else {
- /* Below target temperature */
- int diff = delta; /* int diff = tdiff; Sth. wrong with tdiff? It will keep lower than target */
- if (tdiff > 0) {
- int divisor = -delta / AVA8_DEFAULT_TEMP_HYSTERESIS + 1;
- /* Adjust fanspeed by temperature change proportional to
- * diff from optimal. */
- diff /= divisor;
- } else {
- /* Is the temp below optimal and unchanging, gently lower speed */
- if (t < info->temp_target[id] - AVA8_DEFAULT_TEMP_HYSTERESIS && !tdiff)
- diff -= 1;
- diff = (delta == 0) ? 0 : diff;
- }
- info->fan_pct[id] += diff;
- }
- if (info->fan_pct[id] > opt_avalon8_fan_max)
- info->fan_pct[id] = opt_avalon8_fan_max;
- else if (info->fan_pct[id] < opt_avalon8_fan_min)
- info->fan_pct[id] = opt_avalon8_fan_min;
- out:
- pwm = get_fan_pwm(info->fan_pct[id]);
- if (info->cutoff[id])
- pwm = get_fan_pwm(opt_avalon8_fan_max);
- applog(LOG_DEBUG, "[%d], Adjust_fan: %dC-%d%%(%03x)", id, t, info->fan_pct[id], pwm);
- return pwm;
- }
- static int decode_pkg(struct cgpu_info *avalon8, struct avalon8_ret *ar, int modular_id)
- {
- struct avalon8_info *info = avalon8->device_data;
- struct pool *pool, *real_pool;
- struct pool *pool_stratum0 = &info->pool0;
- struct pool *pool_stratum1 = &info->pool1;
- struct pool *pool_stratum2 = &info->pool2;
- struct thr_info *thr = NULL;
- unsigned short expected_crc;
- unsigned short actual_crc;
- uint32_t nonce, nonce2, ntime, miner, chip_id, tmp;
- uint8_t job_id[2];
- int pool_no;
- uint32_t i, j;
- int64_t last_diff1;
- uint16_t vin;
- if (likely(avalon8->thr))
- thr = avalon8->thr[0];
- if (ar->head[0] != AVA8_H1 && ar->head[1] != AVA8_H2) {
- applog(LOG_DEBUG, "%s-%d-%d: H1 %02x, H2 %02x",
- avalon8->drv->name, avalon8->device_id, modular_id,
- ar->head[0], ar->head[1]);
- hexdump(ar->data, 32);
- return 1;
- }
- expected_crc = crc16(ar->data, AVA8_P_DATA_LEN);
- actual_crc = ((ar->crc[0] & 0xff) << 8) | (ar->crc[1] & 0xff);
- if (expected_crc != actual_crc) {
- applog(LOG_DEBUG, "%s-%d-%d: %02x: expected crc(%04x), actual_crc(%04x)",
- avalon8->drv->name, avalon8->device_id, modular_id,
- ar->type, expected_crc, actual_crc);
- return 1;
- }
- switch(ar->type) {
- case AVA8_P_NONCE:
- applog(LOG_DEBUG, "%s-%d-%d: AVA8_P_NONCE", avalon8->drv->name, avalon8->device_id, modular_id);
- memcpy(&miner, ar->data + 0, 4);
- memcpy(&nonce2, ar->data + 4, 4);
- memcpy(&ntime, ar->data + 8, 4);
- memcpy(&nonce, ar->data + 12, 4);
- job_id[0] = ar->data[16];
- job_id[1] = ar->data[17];
- pool_no = (ar->data[18] | (ar->data[19] << 8));
- miner = be32toh(miner);
- chip_id = (miner >> 16) & 0xffff;
- miner &= 0xffff;
- ntime = be32toh(ntime);
- if (miner >= info->miner_count[modular_id] ||
- pool_no >= total_pools || pool_no < 0) {
- applog(LOG_DEBUG, "%s-%d-%d: Wrong miner/pool_no %d/%d",
- avalon8->drv->name, avalon8->device_id, modular_id,
- miner, pool_no);
- break;
- }
- nonce2 = be32toh(nonce2);
- nonce = be32toh(nonce);
- if (ntime > info->max_ntime)
- info->max_ntime = ntime;
- 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")]",
- avalon8->drv->name, avalon8->device_id, modular_id,
- pool_no, nonce2, nonce, ntime, info->max_ntime,
- miner, chip_id, nonce & 0x7f,
- info->chip_matching_work[modular_id][miner][0],
- info->chip_matching_work[modular_id][miner][1],
- info->chip_matching_work[modular_id][miner][2],
- info->chip_matching_work[modular_id][miner][3]);
- real_pool = pool = pools[pool_no];
- if (job_idcmp(job_id, pool->swork.job_id)) {
- if (!job_idcmp(job_id, pool_stratum0->swork.job_id)) {
- applog(LOG_DEBUG, "%s-%d-%d: Match to previous stratum0! (%s)",
- avalon8->drv->name, avalon8->device_id, modular_id,
- pool_stratum0->swork.job_id);
- pool = pool_stratum0;
- } else if (!job_idcmp(job_id, pool_stratum1->swork.job_id)) {
- applog(LOG_DEBUG, "%s-%d-%d: Match to previous stratum1! (%s)",
- avalon8->drv->name, avalon8->device_id, modular_id,
- pool_stratum1->swork.job_id);
- pool = pool_stratum1;
- } else if (!job_idcmp(job_id, pool_stratum2->swork.job_id)) {
- applog(LOG_DEBUG, "%s-%d-%d: Match to previous stratum2! (%s)",
- avalon8->drv->name, avalon8->device_id, modular_id,
- pool_stratum2->swork.job_id);
- pool = pool_stratum2;
- } else {
- applog(LOG_ERR, "%s-%d-%d: Cannot match to any stratum! (%s)",
- avalon8->drv->name, avalon8->device_id, modular_id,
- pool->swork.job_id);
- if (likely(thr))
- inc_hw_errors(thr);
- info->hw_works_i[modular_id][miner]++;
- break;
- }
- }
- /* Can happen during init sequence before add_cgpu */
- if (unlikely(!thr))
- break;
- last_diff1 = avalon8->diff1;
- if (!submit_nonce2_nonce(thr, pool, real_pool, nonce2, nonce, ntime))
- info->hw_works_i[modular_id][miner]++;
- else {
- info->diff1[modular_id] += (avalon8->diff1 - last_diff1);
- info->chip_matching_work[modular_id][miner][chip_id]++;
- }
- break;
- case AVA8_P_STATUS:
- applog(LOG_DEBUG, "%s-%d-%d: AVA8_P_STATUS", avalon8->drv->name, avalon8->device_id, modular_id);
- hexdump(ar->data, 32);
- memcpy(&tmp, ar->data, 4);
- tmp = be32toh(tmp);
- info->temp_mm[modular_id] = tmp;
- avalon8->temp = decode_auc_temp(info->auc_sensor);
- memcpy(&tmp, ar->data + 4, 4);
- tmp = be32toh(tmp);
- info->fan_cpm[modular_id] = tmp;
- memcpy(&tmp, ar->data + 8, 4);
- info->local_works_i[modular_id][ar->idx] += be32toh(tmp);
- memcpy(&tmp, ar->data + 12, 4);
- info->hw_works_i[modular_id][ar->idx] += be32toh(tmp);
- memcpy(&tmp, ar->data + 16, 4);
- info->error_code[modular_id][ar->idx] = be32toh(tmp);
- memcpy(&tmp, ar->data + 20, 4);
- info->error_code[modular_id][ar->cnt] = be32toh(tmp);
- memcpy(&tmp, ar->data + 24, 4);
- info->error_crc[modular_id][ar->idx] += be32toh(tmp);
- break;
- case AVA8_P_STATUS_PMU:
- /* TODO: decode ntc led from PMU */
- applog(LOG_DEBUG, "%s-%d-%d: AVA8_P_STATUS_PMU", avalon8->drv->name, avalon8->device_id, modular_id);
- info->power_good[modular_id] = ar->data[16];
- for (i = 0; i < AVA8_DEFAULT_PMU_CNT; i++) {
- memcpy(&info->pmu_version[modular_id][i], ar->data + 24 + (i * 4), 4);
- info->pmu_version[modular_id][i][4] = '\0';
- }
- for (i = 0; i < info->miner_count[modular_id]; i++) {
- memcpy(&vin, ar->data + 8 + i * 2, 2);
- info->get_vin[modular_id][i] = decode_vin(info, modular_id, be16toh(vin));
- }
- break;
- case AVA8_P_STATUS_VOLT:
- applog(LOG_DEBUG, "%s-%d-%d: AVA8_P_STATUS_VOLT", avalon8->drv->name, avalon8->device_id, modular_id);
- for (i = 0; i < info->miner_count[modular_id]; i++) {
- memcpy(&tmp, ar->data + i * 4, 4);
- info->get_voltage[modular_id][i] = decode_voltage(info, modular_id, be32toh(tmp));
- }
- break;
- case AVA8_P_STATUS_PLL:
- applog(LOG_DEBUG, "%s-%d-%d: AVA8_P_STATUS_PLL", avalon8->drv->name, avalon8->device_id, modular_id);
- if (ar->opt) {
- for (i = 0; i < AVA8_DEFAULT_PLL_CNT; i++) {
- memcpy(&tmp, ar->data + i * 4, 4);
- info->get_pll[modular_id][ar->idx][i] = be32toh(tmp);
- }
- } else {
- for (i = 0; i < AVA8_DEFAULT_PLL_CNT; i++) {
- memcpy(&tmp, ar->data + i * 4, 4);
- info->get_frequency[modular_id][ar->idx][i] = be32toh(tmp);
- }
- }
- break;
- case AVA8_P_STATUS_PVT:
- applog(LOG_DEBUG, "%s-%d-%d: AVA8_P_STATUS_PVT", avalon8->drv->name, avalon8->device_id, modular_id);
- if (!info->asic_count[modular_id])
- break;
- if (ar->idx < info->asic_count[modular_id]) {
- for (i = 0; i < info->miner_count[modular_id]; i++) {
- memcpy(&tmp, ar->data + i * 4, 2);
- tmp = be16toh(tmp);
- info->temp[modular_id][i][ar->idx] = decode_pvt_temp(tmp);
- memcpy(&tmp, ar->data + i * 4 + 2, 2);
- tmp = be16toh(tmp);
- info->core_volt[modular_id][i][ar->idx] = decode_pvt_volt(tmp);
- }
- }
- break;
- case AVA8_P_STATUS_ASIC:
- {
- int miner_id;
- int asic_id;
- uint16_t freq;
- if (!info->asic_count[modular_id])
- break;
- miner_id = ar->idx / info->asic_count[modular_id];
- asic_id = ar->idx % info->asic_count[modular_id];
- applog(LOG_DEBUG, "%s-%d-%d: AVA8_P_STATUS_ASIC %d-%d",
- avalon8->drv->name, avalon8->device_id, modular_id,
- miner_id, asic_id);
- memcpy(&tmp, ar->data + 0, 4);
- if (tmp)
- info->get_asic[modular_id][miner_id][asic_id][0] = be32toh(tmp);
- memcpy(&tmp, ar->data + 4, 4);
- if (tmp)
- info->get_asic[modular_id][miner_id][asic_id][1] = be32toh(tmp);
- for (i = 0; i < AVA8_DEFAULT_PLL_CNT; i++) {
- memcpy(&tmp, ar->data + 8 + i * 2, 2);
- info->get_asic[modular_id][miner_id][asic_id][2 + i] = be16toh(tmp);
- }
- }
- break;
- case AVA8_P_STATUS_FAC:
- applog(LOG_DEBUG, "%s-%d-%d: AVA8_P_STATUS_FAC", avalon8->drv->name, avalon8->device_id, modular_id);
- info->factory_info[0] = ar->data[0];
- break;
- case AVA8_P_STATUS_OC:
- applog(LOG_DEBUG, "%s-%d-%d: AVA8_P_STATUS_OC", avalon8->drv->name, avalon8->device_id, modular_id);
- info->overclocking_info[0] = ar->data[0];
- break;
- default:
- applog(LOG_DEBUG, "%s-%d-%d: Unknown response %x", avalon8->drv->name, avalon8->device_id, modular_id, ar->type);
- break;
- }
- return 0;
- }
- /*
- # IIC packet format: length[1]+transId[1]+sesId[1]+req[1]+data[60]
- # length: 4+len(data)
- # transId: 0
- # sesId: 0
- # req: checkout the header file
- # data:
- # INIT: clock_rate[4] + reserved[4] + payload[52]
- # XFER: txSz[1]+rxSz[1]+options[1]+slaveAddr[1] + payload[56]
- */
- static int avalon8_auc_init_pkg(uint8_t *iic_pkg, struct avalon8_iic_info *iic_info, uint8_t *buf, int wlen, int rlen)
- {
- memset(iic_pkg, 0, AVA8_AUC_P_SIZE);
- switch (iic_info->iic_op) {
- case AVA8_IIC_INIT:
- iic_pkg[0] = 12; /* 4 bytes IIC header + 4 bytes speed + 4 bytes xfer delay */
- iic_pkg[3] = AVA8_IIC_INIT;
- iic_pkg[4] = iic_info->iic_param.aucParam[0] & 0xff;
- iic_pkg[5] = (iic_info->iic_param.aucParam[0] >> 8) & 0xff;
- iic_pkg[6] = (iic_info->iic_param.aucParam[0] >> 16) & 0xff;
- iic_pkg[7] = iic_info->iic_param.aucParam[0] >> 24;
- iic_pkg[8] = iic_info->iic_param.aucParam[1] & 0xff;
- iic_pkg[9] = (iic_info->iic_param.aucParam[1] >> 8) & 0xff;
- iic_pkg[10] = (iic_info->iic_param.aucParam[1] >> 16) & 0xff;
- iic_pkg[11] = iic_info->iic_param.aucParam[1] >> 24;
- break;
- case AVA8_IIC_XFER:
- iic_pkg[0] = 8 + wlen;
- iic_pkg[3] = AVA8_IIC_XFER;
- iic_pkg[4] = wlen;
- iic_pkg[5] = rlen;
- iic_pkg[7] = iic_info->iic_param.slave_addr;
- if (buf && wlen)
- memcpy(iic_pkg + 8, buf, wlen);
- break;
- case AVA8_IIC_RESET:
- case AVA8_IIC_DEINIT:
- case AVA8_IIC_INFO:
- iic_pkg[0] = 4;
- iic_pkg[3] = iic_info->iic_op;
- break;
- default:
- break;
- }
- return 0;
- }
- static int avalon8_iic_xfer(struct cgpu_info *avalon8, uint8_t slave_addr,
- uint8_t *wbuf, int wlen,
- uint8_t *rbuf, int rlen)
- {
- struct avalon8_info *info = avalon8->device_data;
- struct i2c_ctx *pctx = NULL;
- int err = 1;
- bool ret = false;
- pctx = info->i2c_slaves[slave_addr];
- if (!pctx) {
- applog(LOG_ERR, "%s-%d: IIC xfer i2c slaves null!", avalon8->drv->name, avalon8->device_id);
- goto out;
- }
- if (wbuf) {
- ret = pctx->write_raw(pctx, wbuf, wlen);
- if (!ret) {
- applog(LOG_DEBUG, "%s-%d: IIC xfer write raw failed!", avalon8->drv->name, avalon8->device_id);
- goto out;
- }
- }
- cgsleep_ms(5);
- if (rbuf) {
- ret = pctx->read_raw(pctx, rbuf, rlen);
- if (!ret) {
- applog(LOG_DEBUG, "%s-%d: IIC xfer read raw failed!", avalon8->drv->name, avalon8->device_id);
- hexdump(rbuf, rlen);
- goto out;
- }
- }
- return 0;
- out:
- return err;
- }
- static int avalon8_auc_xfer(struct cgpu_info *avalon8,
- uint8_t *wbuf, int wlen, int *write,
- uint8_t *rbuf, int rlen, int *read)
- {
- int err = -1;
- if (unlikely(avalon8->usbinfo.nodev))
- goto out;
- usb_buffer_clear(avalon8);
- err = usb_write(avalon8, (char *)wbuf, wlen, write, C_AVA8_WRITE);
- if (err || *write != wlen) {
- applog(LOG_DEBUG, "%s-%d: AUC xfer %d, w(%d-%d)!", avalon8->drv->name, avalon8->device_id, err, wlen, *write);
- usb_nodev(avalon8);
- goto out;
- }
- cgsleep_ms(opt_avalon8_aucxdelay / 4800 + 1);
- rlen += 4; /* Add 4 bytes IIC header */
- err = usb_read(avalon8, (char *)rbuf, rlen, read, C_AVA8_READ);
- if (err || *read != rlen || *read != rbuf[0]) {
- applog(LOG_DEBUG, "%s-%d: AUC xfer %d, r(%d-%d-%d)!", avalon8->drv->name, avalon8->device_id, err, rlen - 4, *read, rbuf[0]);
- hexdump(rbuf, rlen);
- return -1;
- }
- *read = rbuf[0] - 4; /* Remove 4 bytes IIC header */
- out:
- return err;
- }
- static int avalon8_auc_init(struct cgpu_info *avalon8, char *ver)
- {
- struct avalon8_iic_info iic_info;
- int err, wlen, rlen;
- uint8_t wbuf[AVA8_AUC_P_SIZE];
- uint8_t rbuf[AVA8_AUC_P_SIZE];
- if (unlikely(avalon8->usbinfo.nodev))
- return 1;
- /* Try to clean the AUC buffer */
- usb_buffer_clear(avalon8);
- err = usb_read(avalon8, (char *)rbuf, AVA8_AUC_P_SIZE, &rlen, C_AVA8_READ);
- applog(LOG_DEBUG, "%s-%d: AUC usb_read %d, %d!", avalon8->drv->name, avalon8->device_id, err, rlen);
- hexdump(rbuf, AVA8_AUC_P_SIZE);
- /* Reset */
- iic_info.iic_op = AVA8_IIC_RESET;
- rlen = 0;
- avalon8_auc_init_pkg(wbuf, &iic_info, NULL, 0, rlen);
- memset(rbuf, 0, AVA8_AUC_P_SIZE);
- err = avalon8_auc_xfer(avalon8, wbuf, AVA8_AUC_P_SIZE, &wlen, rbuf, rlen, &rlen);
- if (err) {
- applog(LOG_ERR, "%s-%d: Failed to reset Avalon USB2IIC Converter", avalon8->drv->name, avalon8->device_id);
- return 1;
- }
- /* Deinit */
- iic_info.iic_op = AVA8_IIC_DEINIT;
- rlen = 0;
- avalon8_auc_init_pkg(wbuf, &iic_info, NULL, 0, rlen);
- memset(rbuf, 0, AVA8_AUC_P_SIZE);
- err = avalon8_auc_xfer(avalon8, wbuf, AVA8_AUC_P_SIZE, &wlen, rbuf, rlen, &rlen);
- if (err) {
- applog(LOG_ERR, "%s-%d: Failed to deinit Avalon USB2IIC Converter", avalon8->drv->name, avalon8->device_id);
- return 1;
- }
- /* Init */
- iic_info.iic_op = AVA8_IIC_INIT;
- iic_info.iic_param.aucParam[0] = opt_avalon8_aucspeed;
- iic_info.iic_param.aucParam[1] = opt_avalon8_aucxdelay;
- rlen = AVA8_AUC_VER_LEN;
- avalon8_auc_init_pkg(wbuf, &iic_info, NULL, 0, rlen);
- memset(rbuf, 0, AVA8_AUC_P_SIZE);
- err = avalon8_auc_xfer(avalon8, wbuf, AVA8_AUC_P_SIZE, &wlen, rbuf, rlen, &rlen);
- if (err) {
- applog(LOG_ERR, "%s-%d: Failed to init Avalon USB2IIC Converter", avalon8->drv->name, avalon8->device_id);
- return 1;
- }
- hexdump(rbuf, AVA8_AUC_P_SIZE);
- memcpy(ver, rbuf + 4, AVA8_AUC_VER_LEN);
- ver[AVA8_AUC_VER_LEN] = '\0';
- applog(LOG_DEBUG, "%s-%d: USB2IIC Converter version: %s!", avalon8->drv->name, avalon8->device_id, ver);
- return 0;
- }
- static int avalon8_auc_getinfo(struct cgpu_info *avalon8)
- {
- struct avalon8_iic_info iic_info;
- int err, wlen, rlen;
- uint8_t wbuf[AVA8_AUC_P_SIZE];
- uint8_t rbuf[AVA8_AUC_P_SIZE];
- uint8_t *pdata = rbuf + 4;
- uint16_t adc_val;
- struct avalon8_info *info = avalon8->device_data;
- iic_info.iic_op = AVA8_IIC_INFO;
- /*
- * Device info: (9 bytes)
- * tempadc(2), reqRdIndex, reqWrIndex,
- * respRdIndex, respWrIndex, tx_flags, state
- */
- rlen = 7;
- avalon8_auc_init_pkg(wbuf, &iic_info, NULL, 0, rlen);
- memset(rbuf, 0, AVA8_AUC_P_SIZE);
- err = avalon8_auc_xfer(avalon8, wbuf, AVA8_AUC_P_SIZE, &wlen, rbuf, rlen, &rlen);
- if (err) {
- applog(LOG_ERR, "%s-%d: AUC Failed to get info ", avalon8->drv->name, avalon8->device_id);
- return 1;
- }
- applog(LOG_DEBUG, "%s-%d: AUC tempADC(%03d), reqcnt(%d), respcnt(%d), txflag(%d), state(%d)",
- avalon8->drv->name, avalon8->device_id,
- pdata[1] << 8 | pdata[0],
- pdata[2],
- pdata[3],
- pdata[5] << 8 | pdata[4],
- pdata[6]);
- adc_val = pdata[1] << 8 | pdata[0];
- info->auc_sensor = 3.3 * adc_val * 10000 / 1023;
- return 0;
- }
- static int avalon8_iic_xfer_pkg(struct cgpu_info *avalon8, uint8_t slave_addr,
- const struct avalon8_pkg *pkg, struct avalon8_ret *ret)
- {
- struct avalon8_iic_info iic_info;
- int err, wcnt, rcnt, rlen = 0;
- uint8_t wbuf[AVA8_AUC_P_SIZE];
- uint8_t rbuf[AVA8_AUC_P_SIZE];
- struct avalon8_info *info = avalon8->device_data;
- if (ret)
- rlen = AVA8_READ_SIZE;
- if (info->connecter == AVA8_CONNECTER_AUC) {
- if (unlikely(avalon8->usbinfo.nodev))
- return AVA8_SEND_ERROR;
- iic_info.iic_op = AVA8_IIC_XFER;
- iic_info.iic_param.slave_addr = slave_addr;
- avalon8_auc_init_pkg(wbuf, &iic_info, (uint8_t *)pkg, AVA8_WRITE_SIZE, rlen);
- err = avalon8_auc_xfer(avalon8, wbuf, wbuf[0], &wcnt, rbuf, rlen, &rcnt);
- if ((pkg->type != AVA8_P_DETECT) && err == -7 && !rcnt && rlen) {
- avalon8_auc_init_pkg(wbuf, &iic_info, NULL, 0, rlen);
- err = avalon8_auc_xfer(avalon8, wbuf, wbuf[0], &wcnt, rbuf, rlen, &rcnt);
- applog(LOG_DEBUG, "%s-%d-%d: AUC read again!(type:0x%x, err:%d)", avalon8->drv->name, avalon8->device_id, slave_addr, pkg->type, err);
- }
- if (err || rcnt != rlen) {
- if (info->xfer_err_cnt++ == 100) {
- applog(LOG_DEBUG, "%s-%d-%d: AUC xfer_err_cnt reach err = %d, rcnt = %d, rlen = %d",
- avalon8->drv->name, avalon8->device_id, slave_addr,
- err, rcnt, rlen);
- cgsleep_ms(5 * 1000); /* Wait MM reset */
- if (avalon8_auc_init(avalon8, info->auc_version)) {
- applog(LOG_WARNING, "%s-%d: Failed to re-init auc, unplugging for new hotplug",
- avalon8->drv->name, avalon8->device_id);
- usb_nodev(avalon8);
- }
- }
- return AVA8_SEND_ERROR;
- }
- if (ret)
- memcpy((char *)ret, rbuf + 4, AVA8_READ_SIZE);
- info->xfer_err_cnt = 0;
- }
- if (info->connecter == AVA8_CONNECTER_IIC) {
- err = avalon8_iic_xfer(avalon8, slave_addr, (uint8_t *)pkg, AVA8_WRITE_SIZE, (uint8_t *)ret, AVA8_READ_SIZE);
- if ((pkg->type != AVA8_P_DETECT) && err) {
- err = avalon8_iic_xfer(avalon8, slave_addr, (uint8_t *)pkg, AVA8_WRITE_SIZE, (uint8_t *)ret, AVA8_READ_SIZE);
- applog(LOG_DEBUG, "%s-%d-%d: IIC read again!(type:0x%x, err:%d)", avalon8->drv->name, avalon8->device_id, slave_addr, pkg->type, err);
- }
- if (err) {
- /* FIXME: Don't care broadcast message with no reply, or it will block other thread when called by avalon8_send_bc_pkgs */
- if ((pkg->type != AVA8_P_DETECT) && (slave_addr == AVA8_MODULE_BROADCAST))
- return AVA8_SEND_OK;
- if (info->xfer_err_cnt++ == 100) {
- info->xfer_err_cnt = 0;
- applog(LOG_DEBUG, "%s-%d-%d: IIC xfer_err_cnt reach err = %d, rcnt = %d, rlen = %d",
- avalon8->drv->name, avalon8->device_id, slave_addr,
- err, rcnt, rlen);
- cgsleep_ms(5 * 1000); /* Wait MM reset */
- }
- return AVA8_SEND_ERROR;
- }
- info->xfer_err_cnt = 0;
- }
- return AVA8_SEND_OK;
- }
- static int avalon8_send_bc_pkgs(struct cgpu_info *avalon8, const struct avalon8_pkg *pkg)
- {
- int ret;
- do {
- ret = avalon8_iic_xfer_pkg(avalon8, AVA8_MODULE_BROADCAST, pkg, NULL);
- } while (ret != AVA8_SEND_OK);
- return 0;
- }
- static void avalon8_stratum_pkgs(struct cgpu_info *avalon8, struct pool *pool)
- {
- struct avalon8_info *info = avalon8->device_data;
- const int merkle_offset = 36;
- struct avalon8_pkg pkg;
- int i, a, b;
- uint32_t tmp;
- unsigned char target[32];
- int job_id_len, n2size;
- unsigned short crc;
- int coinbase_len_posthash, coinbase_len_prehash;
- uint8_t coinbase_prehash[32];
- uint32_t range, start;
- /* Send out the first stratum message STATIC */
- applog(LOG_DEBUG, "%s-%d: Pool stratum message STATIC: %d, %d, %d, %d, %d",
- avalon8->drv->name, avalon8->device_id,
- pool->coinbase_len,
- pool->nonce2_offset,
- pool->n2size,
- merkle_offset,
- pool->merkles);
- memset(pkg.data, 0, AVA8_P_DATA_LEN);
- tmp = be32toh(pool->coinbase_len);
- memcpy(pkg.data, &tmp, 4);
- tmp = be32toh(pool->nonce2_offset);
- memcpy(pkg.data + 4, &tmp, 4);
- n2size = pool->n2size >= 4 ? 4 : pool->n2size;
- tmp = be32toh(n2size);
- memcpy(pkg.data + 8, &tmp, 4);
- tmp = be32toh(merkle_offset);
- memcpy(pkg.data + 12, &tmp, 4);
- tmp = be32toh(pool->merkles);
- memcpy(pkg.data + 16, &tmp, 4);
- if (pool->n2size == 3)
- range = 0xffffff / (total_devices ? total_devices : 1);
- else
- range = 0xffffffff / (total_devices ? total_devices : 1);
- start = range * avalon8->device_id;
- tmp = be32toh(start);
- memcpy(pkg.data + 20, &tmp, 4);
- tmp = be32toh(range);
- memcpy(pkg.data + 24, &tmp, 4);
- if (info->work_restart) {
- info->work_restart = false;
- tmp = be32toh(0x1);
- memcpy(pkg.data + 28, &tmp, 4);
- }
- avalon8_init_pkg(&pkg, AVA8_P_STATIC, 1, 1);
- if (avalon8_send_bc_pkgs(avalon8, &pkg))
- return;
- if (pool->sdiff <= AVA8_DRV_DIFFMAX)
- set_target(target, pool->sdiff);
- else
- set_target(target, AVA8_DRV_DIFFMAX);
- memcpy(pkg.data, target, 32);
- if (opt_debug) {
- char *target_str;
- target_str = bin2hex(target, 32);
- applog(LOG_DEBUG, "%s-%d: Pool stratum target: %s", avalon8->drv->name, avalon8->device_id, target_str);
- free(target_str);
- }
- avalon8_init_pkg(&pkg, AVA8_P_TARGET, 1, 1);
- if (avalon8_send_bc_pkgs(avalon8, &pkg))
- return;
- memset(pkg.data, 0, AVA8_P_DATA_LEN);
- job_id_len = strlen(pool->swork.job_id);
- crc = crc16((unsigned char *)pool->swork.job_id, job_id_len);
- applog(LOG_DEBUG, "%s-%d: Pool stratum message JOBS_ID[%04x]: %s",
- avalon8->drv->name, avalon8->device_id,
- crc, pool->swork.job_id);
- tmp = ((crc << 16) | pool->pool_no);
- if (info->last_jobid != tmp) {
- info->last_jobid = tmp;
- pkg.data[0] = (crc & 0xff00) >> 8;
- pkg.data[1] = crc & 0xff;
- pkg.data[2] = pool->pool_no & 0xff;
- pkg.data[3] = (pool->pool_no & 0xff00) >> 8;
- avalon8_init_pkg(&pkg, AVA8_P_JOB_ID, 1, 1);
- if (avalon8_send_bc_pkgs(avalon8, &pkg))
- return;
- }
- coinbase_len_prehash = pool->nonce2_offset - (pool->nonce2_offset % SHA256_BLOCK_SIZE);
- coinbase_len_posthash = pool->coinbase_len - coinbase_len_prehash;
- sha256_prehash(pool->coinbase, coinbase_len_prehash, coinbase_prehash);
- a = (coinbase_len_posthash / AVA8_P_DATA_LEN) + 1;
- b = coinbase_len_posthash % AVA8_P_DATA_LEN;
- memcpy(pkg.data, coinbase_prehash, 32);
- avalon8_init_pkg(&pkg, AVA8_P_COINBASE, 1, a + (b ? 1 : 0));
- if (avalon8_send_bc_pkgs(avalon8, &pkg))
- return;
- applog(LOG_DEBUG, "%s-%d: Pool stratum message modified COINBASE: %d %d",
- avalon8->drv->name, avalon8->device_id,
- a, b);
- for (i = 1; i < a; i++) {
- memcpy(pkg.data, pool->coinbase + coinbase_len_prehash + i * 32 - 32, 32);
- avalon8_init_pkg(&pkg, AVA8_P_COINBASE, i + 1, a + (b ? 1 : 0));
- if (avalon8_send_bc_pkgs(avalon8, &pkg))
- return;
- }
- if (b) {
- memset(pkg.data, 0, AVA8_P_DATA_LEN);
- memcpy(pkg.data, pool->coinbase + coinbase_len_prehash + i * 32 - 32, b);
- avalon8_init_pkg(&pkg, AVA8_P_COINBASE, i + 1, i + 1);
- if (avalon8_send_bc_pkgs(avalon8, &pkg))
- return;
- }
- b = pool->merkles;
- applog(LOG_DEBUG, "%s-%d: Pool stratum message MERKLES: %d", avalon8->drv->name, avalon8->device_id, b);
- for (i = 0; i < b; i++) {
- memset(pkg.data, 0, AVA8_P_DATA_LEN);
- memcpy(pkg.data, pool->swork.merkle_bin[i], 32);
- avalon8_init_pkg(&pkg, AVA8_P_MERKLES, i + 1, b);
- if (avalon8_send_bc_pkgs(avalon8, &pkg))
- return;
- }
- applog(LOG_DEBUG, "%s-%d: Pool stratum message HEADER: 4", avalon8->drv->name, avalon8->device_id);
- for (i = 0; i < 4; i++) {
- memset(pkg.data, 0, AVA8_P_DATA_LEN);
- memcpy(pkg.data, pool->header_bin + i * 32, 32);
- avalon8_init_pkg(&pkg, AVA8_P_HEADER, i + 1, 4);
- if (avalon8_send_bc_pkgs(avalon8, &pkg))
- return;
- }
- if (info->connecter == AVA8_CONNECTER_AUC)
- avalon8_auc_getinfo(avalon8);
- }
- static struct cgpu_info *avalon8_iic_detect(void)
- {
- int i;
- struct avalon8_info *info;
- struct cgpu_info *avalon8 = NULL;
- struct i2c_ctx *i2c_slave = NULL;
- i2c_slave = i2c_slave_open(I2C_BUS, 0);
- if (!i2c_slave) {
- applog(LOG_ERR, "avalon8 init iic failed\n");
- return NULL;
- }
- i2c_slave->exit(i2c_slave);
- i2c_slave = NULL;
- avalon8 = cgcalloc(1, sizeof(*avalon8));
- avalon8->drv = &avalon8_drv;
- avalon8->deven = DEV_ENABLED;
- avalon8->threads = 1;
- add_cgpu(avalon8);
- applog(LOG_INFO, "%s-%d: Found at %s", avalon8->drv->name, avalon8->device_id,
- I2C_BUS);
- avalon8->device_data = cgcalloc(sizeof(struct avalon8_info), 1);
- memset(avalon8->device_data, 0, sizeof(struct avalon8_info));
- info = avalon8->device_data;
- for (i = 0; i < AVA8_DEFAULT_MODULARS; i++) {
- info->enable[i] = false;
- info->reboot[i] = false;
- info->i2c_slaves[i] = i2c_slave_open(I2C_BUS, i);
- if (!info->i2c_slaves[i]) {
- applog(LOG_ERR, "avalon8 init i2c slaves failed\n");
- free(avalon8->device_data);
- avalon8->device_data = NULL;
- free(avalon8);
- avalon8 = NULL;
- return NULL;
- }
- }
- info->connecter = AVA8_CONNECTER_IIC;
- return avalon8;
- }
- static void detect_modules(struct cgpu_info *avalon8);
- static struct cgpu_info *avalon8_auc_detect(struct libusb_device *dev, struct usb_find_devices *found)
- {
- int i, modules = 0;
- struct avalon8_info *info;
- struct cgpu_info *avalon8 = usb_alloc_cgpu(&avalon8_drv, 1);
- char auc_ver[AVA8_AUC_VER_LEN];
- if (!usb_init(avalon8, dev, found)) {
- applog(LOG_ERR, "avalon8 failed usb_init");
- avalon8 = usb_free_cgpu(avalon8);
- return NULL;
- }
- /* avalon8 prefers not to use zero length packets */
- avalon8->nozlp = true;
- /* We try twice on AUC init */
- if (avalon8_auc_init(avalon8, auc_ver) && avalon8_auc_init(avalon8, auc_ver))
- return NULL;
- applog(LOG_INFO, "%s-%d: Found at %s", avalon8->drv->name, avalon8->device_id,
- avalon8->device_path);
- avalon8->device_data = cgcalloc(sizeof(struct avalon8_info), 1);
- memset(avalon8->device_data, 0, sizeof(struct avalon8_info));
- info = avalon8->device_data;
- memcpy(info->auc_version, auc_ver, AVA8_AUC_VER_LEN);
- info->auc_version[AVA8_AUC_VER_LEN] = '\0';
- info->auc_speed = opt_avalon8_aucspeed;
- info->auc_xdelay = opt_avalon8_aucxdelay;
- for (i = 0; i < AVA8_DEFAULT_MODULARS; i++)
- info->enable[i] = 0;
- info->connecter = AVA8_CONNECTER_AUC;
- detect_modules(avalon8);
- for (i = 0; i < AVA8_DEFAULT_MODULARS; i++)
- modules += info->enable[i];
- if (!modules) {
- applog(LOG_INFO, "avalon8 found but no modules initialised");
- free(info);
- avalon8 = usb_free_cgpu(avalon8);
- return NULL;
- }
- /* We have an avalon8 AUC connected */
- avalon8->threads = 1;
- add_cgpu(avalon8);
- update_usb_stats(avalon8);
- return avalon8;
- }
- static inline void avalon8_detect(bool __maybe_unused hotplug)
- {
- usb_detect(&avalon8_drv, avalon8_auc_detect);
- if (!hotplug && opt_avalon8_iic_detect)
- avalon8_iic_detect();
- }
- static bool avalon8_prepare(struct thr_info *thr)
- {
- struct cgpu_info *avalon8 = thr->cgpu;
- struct avalon8_info *info = avalon8->device_data;
- info->last_diff1 = 0;
- info->pending_diff1 = 0;
- info->last_rej = 0;
- info->mm_count = 0;
- info->xfer_err_cnt = 0;
- info->pool_no = 0;
- memset(&(info->firsthash), 0, sizeof(info->firsthash));
- cgtime(&(info->last_fan_adj));
- cgtime(&info->last_stratum);
- cgtime(&info->last_detect);
- cglock_init(&info->update_lock);
- cglock_init(&info->pool0.data_lock);
- cglock_init(&info->pool1.data_lock);
- cglock_init(&info->pool2.data_lock);
- return true;
- }
- static int check_module_exist(struct cgpu_info *avalon8, uint8_t mm_dna[AVA8_MM_DNA_LEN])
- {
- struct avalon8_info *info = avalon8->device_data;
- int i;
- for (i = 0; i < AVA8_DEFAULT_MODULARS; i++) {
- /* last byte is \0 */
- if (info->enable[i] && !memcmp(info->mm_dna[i], mm_dna, AVA8_MM_DNA_LEN))
- return 1;
- }
- return 0;
- }
- static void detect_modules(struct cgpu_info *avalon8)
- {
- struct avalon8_info *info = avalon8->device_data;
- struct avalon8_pkg send_pkg;
- struct avalon8_ret ret_pkg;
- uint32_t tmp;
- int i, j, k, err, rlen;
- uint8_t dev_index;
- uint8_t rbuf[AVA8_AUC_P_SIZE];
- /* Detect new modules here */
- for (i = 1; i < AVA8_DEFAULT_MODULARS + 1; i++) {
- if (info->enable[i])
- continue;
- /* Send out detect pkg */
- applog(LOG_DEBUG, "%s-%d: AVA8_P_DETECT ID[%d]",
- avalon8->drv->name, avalon8->device_id, i);
- memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
- tmp = be32toh(i); /* ID */
- memcpy(send_pkg.data + 28, &tmp, 4);
- avalon8_init_pkg(&send_pkg, AVA8_P_DETECT, 1, 1);
- err = avalon8_iic_xfer_pkg(avalon8, AVA8_MODULE_BROADCAST, &send_pkg, &ret_pkg);
- if (err == AVA8_SEND_OK) {
- if (decode_pkg(avalon8, &ret_pkg, AVA8_MODULE_BROADCAST)) {
- applog(LOG_DEBUG, "%s-%d: Should be AVA8_P_ACKDETECT(%d), but %d",
- avalon8->drv->name, avalon8->device_id, AVA8_P_ACKDETECT, ret_pkg.type);
- continue;
- }
- }
- if (err != AVA8_SEND_OK) {
- applog(LOG_DEBUG, "%s-%d: AVA8_P_DETECT: Failed AUC xfer data with err %d",
- avalon8->drv->name, avalon8->device_id, err);
- break;
- }
- applog(LOG_DEBUG, "%s-%d: Module detect ID[%d]: %d",
- avalon8->drv->name, avalon8->device_id, i, ret_pkg.type);
- if (ret_pkg.type != AVA8_P_ACKDETECT)
- break;
- if (check_module_exist(avalon8, ret_pkg.data))
- continue;
- /* Check count of modulars */
- if (i == AVA8_DEFAULT_MODULARS) {
- applog(LOG_NOTICE, "You have connected more than %d machines. This is discouraged.", (AVA8_DEFAULT_MODULARS - 1));
- info->conn_overloaded = true;
- break;
- } else
- info->conn_overloaded = false;
- memcpy(info->mm_version[i], ret_pkg.data + AVA8_MM_DNA_LEN, AVA8_MM_VER_LEN);
- info->mm_version[i][AVA8_MM_VER_LEN] = '\0';
- for (dev_index = 0; dev_index < (sizeof(avalon8_dev_table) / sizeof(avalon8_dev_table[0])); dev_index++) {
- if (!strncmp((char *)&(info->mm_version[i]), (char *)(avalon8_dev_table[dev_index].dev_id_str), 3)) {
- info->mod_type[i] = avalon8_dev_table[dev_index].mod_type;
- info->miner_count[i] = avalon8_dev_table[dev_index].miner_count;
- info->asic_count[i] = avalon8_dev_table[dev_index].asic_count;
- info->vin_adc_ratio[i] = avalon8_dev_table[dev_index].vin_adc_ratio;
- info->vout_adc_ratio[i] = avalon8_dev_table[dev_index].vout_adc_ratio;
- break;
- }
- }
- if (dev_index == (sizeof(avalon8_dev_table) / sizeof(avalon8_dev_table[0]))) {
- applog(LOG_NOTICE, "%s-%d: The modular version %s cann't be support",
- avalon8->drv->name, avalon8->device_id, info->mm_version[i]);
- break;
- }
- info->enable[i] = 1;
- cgtime(&info->elapsed[i]);
- memcpy(info->mm_dna[i], ret_pkg.data, AVA8_MM_DNA_LEN);
- memcpy(&tmp, ret_pkg.data + AVA8_MM_DNA_LEN + AVA8_MM_VER_LEN, 4);
- tmp = be32toh(tmp);
- info->total_asics[i] = tmp;
- info->temp_overheat[i] = AVA8_DEFAULT_TEMP_OVERHEAT;
- info->temp_target[i] = opt_avalon8_temp_target;
- info->fan_pct[i] = opt_avalon8_fan_min;
- for (j = 0; j < info->miner_count[i]; j++) {
- if (opt_avalon8_voltage_level == AVA8_INVALID_VOLTAGE_LEVEL)
- info->set_voltage_level[i][j] = avalon8_dev_table[dev_index].set_voltage_level;
- else
- info->set_voltage_level[i][j] = opt_avalon8_voltage_level;
- info->get_voltage[i][j] = 0;
- info->get_vin[i][j] = 0;
- for (k = 0; k < info->asic_count[i]; k++)
- info->temp[i][j][k] = -273;
- for (k = 0; k < AVA8_DEFAULT_PLL_CNT; k++)
- info->set_frequency[i][j][k] = avalon8_dev_table[dev_index].set_freq[k];
- }
- info->freq_mode[i] = AVA8_FREQ_INIT_MODE;
- memset(info->get_pll[i], 0, sizeof(uint32_t) * info->miner_count[i] * AVA8_DEFAULT_PLL_CNT);
- info->led_indicator[i] = 0;
- info->cutoff[i] = 0;
- info->fan_cpm[i] = 0;
- info->temp_mm[i] = -273;
- info->temp_last_max[i] = -273;
- info->local_works[i] = 0;
- info->hw_works[i] = 0;
- for (j = 0; j < info->miner_count[i]; j++) {
- memset(info->chip_matching_work[i][j], 0, sizeof(uint64_t) * info->asic_count[i]);
- info->local_works_i[i][j] = 0;
- info->hw_works_i[i][j] = 0;
- info->error_code[i][j] = 0;
- info->error_crc[i][j] = 0;
- }
- info->error_code[i][j] = 0;
- info->error_polling_cnt[i] = 0;
- info->power_good[i] = 0;
- memset(info->pmu_version[i], 0, sizeof(char) * 5 * AVA8_DEFAULT_PMU_CNT);
- info->diff1[i] = 0;
- applog(LOG_NOTICE, "%s-%d: New module detected! ID[%d-%x]",
- avalon8->drv->name, avalon8->device_id, i, info->mm_dna[i][AVA8_MM_DNA_LEN - 1]);
- /* Tell MM, it has been detected */
- memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
- memcpy(send_pkg.data, info->mm_dna[i], AVA8_MM_DNA_LEN);
- avalon8_init_pkg(&send_pkg, AVA8_P_SYNC, 1, 1);
- avalon8_iic_xfer_pkg(avalon8, i, &send_pkg, &ret_pkg);
- /* Keep the usb buffer is empty */
- usb_buffer_clear(avalon8);
- usb_read(avalon8, (char *)rbuf, AVA8_AUC_P_SIZE, &rlen, C_AVA8_READ);
- }
- }
- static void detach_module(struct cgpu_info *avalon8, int addr)
- {
- struct avalon8_info *info = avalon8->device_data;
- info->enable[addr] = 0;
- applog(LOG_NOTICE, "%s-%d: Module detached! ID[%d]",
- avalon8->drv->name, avalon8->device_id, addr);
- }
- static int polling(struct cgpu_info *avalon8)
- {
- struct avalon8_info *info = avalon8->device_data;
- struct avalon8_pkg send_pkg;
- struct avalon8_ret ar;
- int i, tmp, ret, decode_err = 0;
- struct timeval current_fan;
- int do_adjust_fan = 0;
- uint32_t fan_pwm;
- double device_tdiff;
- cgtime(¤t_fan);
- device_tdiff = tdiff(¤t_fan, &(info->last_fan_adj));
- if (device_tdiff > 2.0 || device_tdiff < 0) {
- cgtime(&info->last_fan_adj);
- do_adjust_fan = 1;
- }
- for (i = 1; i < AVA8_DEFAULT_MODULARS; i++) {
- if (!info->enable[i])
- continue;
- cgsleep_ms(opt_avalon8_polling_delay);
- memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
- /* Red LED */
- tmp = be32toh(info->led_indicator[i]);
- memcpy(send_pkg.data, &tmp, 4);
- /* Adjust fan every 2 seconds*/
- if (do_adjust_fan) {
- fan_pwm = adjust_fan(info, i);
- fan_pwm |= 0x80000000;
- tmp = be32toh(fan_pwm);
- memcpy(send_pkg.data + 4, &tmp, 4);
- }
- if (info->reboot[i]) {
- info->reboot[i] = false;
- send_pkg.data[8] = 0x1;
- }
- avalon8_init_pkg(&send_pkg, AVA8_P_POLLING, 1, 1);
- ret = avalon8_iic_xfer_pkg(avalon8, i, &send_pkg, &ar);
- if (ret == AVA8_SEND_OK)
- decode_err = decode_pkg(avalon8, &ar, i);
- if (ret != AVA8_SEND_OK || decode_err) {
- info->error_polling_cnt[i]++;
- memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
- avalon8_init_pkg(&send_pkg, AVA8_P_RSTMMTX, 1, 1);
- avalon8_iic_xfer_pkg(avalon8, i, &send_pkg, NULL);
- if (info->error_polling_cnt[i] >= 10)
- detach_module(avalon8, i);
- }
- if (ret == AVA8_SEND_OK && !decode_err) {
- info->error_polling_cnt[i] = 0;
- if ((ar.opt == AVA8_P_STATUS) &&
- (info->mm_dna[i][AVA8_MM_DNA_LEN - 1] != ar.opt)) {
- applog(LOG_ERR, "%s-%d-%d: Dup address found %d-%d",
- avalon8->drv->name, avalon8->device_id, i,
- info->mm_dna[i][AVA8_MM_DNA_LEN - 1], ar.opt);
- hexdump((uint8_t *)&ar, sizeof(ar));
- detach_module(avalon8, i);
- }
- }
- }
- return 0;
- }
- static void copy_pool_stratum(struct pool *pool_stratum, struct pool *pool)
- {
- int i;
- int merkles = pool->merkles, job_id_len;
- size_t coinbase_len = pool->coinbase_len;
- unsigned short crc;
- if (!pool->swork.job_id)
- return;
- if (pool_stratum->swork.job_id) {
- job_id_len = strlen(pool->swork.job_id);
- crc = crc16((unsigned char *)pool->swork.job_id, job_id_len);
- job_id_len = strlen(pool_stratum->swork.job_id);
- if (crc16((unsigned char *)pool_stratum->swork.job_id, job_id_len) == crc)
- return;
- }
- cg_wlock(&pool_stratum->data_lock);
- free(pool_stratum->swork.job_id);
- free(pool_stratum->nonce1);
- free(pool_stratum->coinbase);
- pool_stratum->coinbase = cgcalloc(coinbase_len, 1);
- memcpy(pool_stratum->coinbase, pool->coinbase, coinbase_len);
- for (i = 0; i < pool_stratum->merkles; i++)
- free(pool_stratum->swork.merkle_bin[i]);
- if (merkles) {
- pool_stratum->swork.merkle_bin = cgrealloc(pool_stratum->swork.merkle_bin,
- sizeof(char *) * merkles + 1);
- for (i = 0; i < merkles; i++) {
- pool_stratum->swork.merkle_bin[i] = cgmalloc(32);
- memcpy(pool_stratum->swork.merkle_bin[i], pool->swork.merkle_bin[i], 32);
- }
- }
- pool_stratum->sdiff = pool->sdiff;
- pool_stratum->coinbase_len = pool->coinbase_len;
- pool_stratum->nonce2_offset = pool->nonce2_offset;
- pool_stratum->n2size = pool->n2size;
- pool_stratum->merkles = pool->merkles;
- pool_stratum->swork.job_id = strdup(pool->swork.job_id);
- pool_stratum->nonce1 = strdup(pool->nonce1);
- memcpy(pool_stratum->ntime, pool->ntime, sizeof(pool_stratum->ntime));
- memcpy(pool_stratum->header_bin, pool->header_bin, sizeof(pool_stratum->header_bin));
- cg_wunlock(&pool_stratum->data_lock);
- }
- static void avalon8_init_setting(struct cgpu_info *avalon8, int addr)
- {
- struct avalon8_pkg send_pkg;
- uint32_t tmp;
- memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
- tmp = be32toh(opt_avalon8_freq_sel);
- memcpy(send_pkg.data + 4, &tmp, 4);
- /*
- * set flags:
- * 0: ss switch
- * 1: nonce check
- * 2: roll enable
- */
- tmp = 1;
- if (!opt_avalon8_smart_speed)
- tmp = 0;
- tmp |= (opt_avalon8_nonce_check << 1);
- tmp |= (opt_avalon8_roll_enable << 2);
- send_pkg.data[8] = tmp & 0xff;
- send_pkg.data[9] = opt_avalon8_nonce_mask & 0xff;
- tmp = be32toh(opt_avalon8_mux_l2h);
- memcpy(send_pkg.data + 10, &tmp, 4);
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set mux l2h %u",
- avalon8->drv->name, avalon8->device_id, addr,
- opt_avalon8_mux_l2h);
- tmp = be32toh(opt_avalon8_mux_h2l);
- memcpy(send_pkg.data + 14, &tmp, 4);
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set mux h2l %u",
- avalon8->drv->name, avalon8->device_id, addr,
- opt_avalon8_mux_h2l);
- tmp = be32toh(opt_avalon8_h2ltime0_spd);
- memcpy(send_pkg.data + 18, &tmp, 4);
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set h2ltime0 spd %u",
- avalon8->drv->name, avalon8->device_id, addr,
- opt_avalon8_h2ltime0_spd);
- tmp = be32toh(opt_avalon8_spdlow);
- memcpy(send_pkg.data + 22, &tmp, 4);
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set spdlow %u",
- avalon8->drv->name, avalon8->device_id, addr,
- opt_avalon8_spdlow);
- tmp = be32toh(opt_avalon8_spdhigh);
- memcpy(send_pkg.data + 26, &tmp, 4);
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set spdhigh %u",
- avalon8->drv->name, avalon8->device_id, addr,
- opt_avalon8_spdhigh);
- /* Package the data */
- avalon8_init_pkg(&send_pkg, AVA8_P_SET, 1, 1);
- if (addr == AVA8_MODULE_BROADCAST)
- avalon8_send_bc_pkgs(avalon8, &send_pkg);
- else
- avalon8_iic_xfer_pkg(avalon8, addr, &send_pkg, NULL);
- }
- static void avalon8_set_voltage_level(struct cgpu_info *avalon8, int addr, unsigned int voltage[])
- {
- struct avalon8_info *info = avalon8->device_data;
- struct avalon8_pkg send_pkg;
- uint32_t tmp;
- uint8_t i;
- memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
- /* NOTE: miner_count should <= 8 */
- for (i = 0; i < info->miner_count[addr]; i++) {
- tmp = be32toh(encode_voltage(voltage[i] +
- opt_avalon8_voltage_level_offset));
- memcpy(send_pkg.data + i * 4, &tmp, 4);
- }
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set voltage miner %d, (%d-%d)",
- avalon8->drv->name, avalon8->device_id, addr,
- i, voltage[0], voltage[info->miner_count[addr] - 1]);
- /* Package the data */
- avalon8_init_pkg(&send_pkg, AVA8_P_SET_VOLT, 1, 1);
- if (addr == AVA8_MODULE_BROADCAST)
- avalon8_send_bc_pkgs(avalon8, &send_pkg);
- else
- avalon8_iic_xfer_pkg(avalon8, addr, &send_pkg, NULL);
- }
- static void avalon8_set_freq(struct cgpu_info *avalon8, int addr, int miner_id, unsigned int freq[])
- {
- struct avalon8_info *info = avalon8->device_data;
- struct avalon8_pkg send_pkg;
- uint32_t tmp, f;
- uint8_t i;
- memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
- for (i = 0; i < AVA8_DEFAULT_PLL_CNT; i++) {
- tmp = be32toh(api_get_cpm(freq[i]));
- memcpy(send_pkg.data + i * 4, &tmp, 4);
- }
- f = freq[0];
- for (i = 1; i < AVA8_DEFAULT_PLL_CNT; i++)
- f = f > freq[i] ? f : freq[i];
- f = f ? f : 1;
- tmp = AVA8_ASIC_TIMEOUT_CONST / f * 83 / 100;
- tmp = be32toh(tmp);
- memcpy(send_pkg.data + AVA8_DEFAULT_PLL_CNT * 4, &tmp, 4);
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set freq miner %x-%x",
- avalon8->drv->name, avalon8->device_id, addr,
- miner_id, be32toh(tmp));
- /* Package the data */
- avalon8_init_pkg(&send_pkg, AVA8_P_SET_PLL, miner_id + 1, info->miner_count[addr]);
- if (addr == AVA8_MODULE_BROADCAST)
- avalon8_send_bc_pkgs(avalon8, &send_pkg);
- else
- avalon8_iic_xfer_pkg(avalon8, addr, &send_pkg, NULL);
- }
- static void avalon8_set_factory_info(struct cgpu_info *avalon8, int addr, uint8_t value[])
- {
- struct avalon8_pkg send_pkg;
- uint8_t i;
- memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
- for (i = 0; i < AVA8_DEFAULT_FACTORY_INFO_CNT; i++)
- send_pkg.data[i] = value[i];
- /* Package the data */
- avalon8_init_pkg(&send_pkg, AVA8_P_SET_FAC, 1, 1);
- if (addr == AVA8_MODULE_BROADCAST)
- avalon8_send_bc_pkgs(avalon8, &send_pkg);
- else
- avalon8_iic_xfer_pkg(avalon8, addr, &send_pkg, NULL);
- }
- static void avalon8_set_overclocking_info(struct cgpu_info *avalon8, int addr, uint8_t value[])
- {
- struct avalon8_pkg send_pkg;
- uint8_t i;
- memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
- for (i = 0; i < AVA8_DEFAULT_OVERCLOCKING_CNT; i++)
- send_pkg.data[i] = value[i];
- /* Package the data */
- avalon8_init_pkg(&send_pkg, AVA8_P_SET_OC, 1, 1);
- if (addr == AVA8_MODULE_BROADCAST)
- avalon8_send_bc_pkgs(avalon8, &send_pkg);
- else
- avalon8_iic_xfer_pkg(avalon8, addr, &send_pkg, NULL);
- }
- static void avalon8_set_ss_param(struct cgpu_info *avalon8, int addr)
- {
- struct avalon8_pkg send_pkg;
- uint32_t tmp;
- if (!opt_avalon8_smart_speed)
- return;
- memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
- tmp = (opt_avalon8_th_pass << 16) | opt_avalon8_th_fail;
- tmp = be32toh(tmp);
- memcpy(send_pkg.data, &tmp, 4);
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set th pass %u",
- avalon8->drv->name, avalon8->device_id, addr,
- opt_avalon8_th_pass);
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set th fail %u",
- avalon8->drv->name, avalon8->device_id, addr,
- opt_avalon8_th_fail);
- tmp = ((opt_avalon8_th_add & 0x1) << 31) | ((opt_avalon8_th_mssel & 0x1) << 30)
- | ((opt_avalon8_th_ms & 0x3fff) << 16)
- | (opt_avalon8_th_init & 0xffff);
- tmp = be32toh(tmp);
- memcpy(send_pkg.data + 4, &tmp, 4);
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set th add %u",
- avalon8->drv->name, avalon8->device_id, addr,
- (opt_avalon8_th_add & 0x1));
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set th mssel %u",
- avalon8->drv->name, avalon8->device_id, addr,
- (opt_avalon8_th_mssel & 0x1));
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set th ms %u",
- avalon8->drv->name, avalon8->device_id, addr,
- (opt_avalon8_th_ms & 0x3fff));
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set th init %u",
- avalon8->drv->name, avalon8->device_id, addr,
- (opt_avalon8_th_init & 0xffff));
- tmp = be32toh(opt_avalon8_th_timeout);
- memcpy(send_pkg.data + 8, &tmp, 4);
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set th timeout %u",
- avalon8->drv->name, avalon8->device_id, addr,
- opt_avalon8_th_timeout);
- tmp = ((opt_avalon8_lv3_th_add & 0x1) << 31) | ((opt_avalon8_lv2_th_add & 0x1) << 15)
- | ((opt_avalon8_lv3_th_ms & 0x7fff) << 16)
- | (opt_avalon8_lv2_th_ms & 0x7fff);
- tmp = be32toh(tmp);
- memcpy(send_pkg.data + 12, &tmp, 4);
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv3 th add %u",
- avalon8->drv->name, avalon8->device_id, addr,
- (opt_avalon8_lv3_th_add & 0x1));
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv3 th ms %u",
- avalon8->drv->name, avalon8->device_id, addr,
- (opt_avalon8_lv3_th_ms & 0x7fff));
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv2 th add %u",
- avalon8->drv->name, avalon8->device_id, addr,
- (opt_avalon8_lv2_th_add & 0x1));
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv2 th ms %u",
- avalon8->drv->name, avalon8->device_id, addr,
- (opt_avalon8_lv2_th_ms & 0x7fff));
- tmp = ((opt_avalon8_lv5_th_add & 0x1) << 31) | ((opt_avalon8_lv4_th_add & 0x1) << 15)
- | ((opt_avalon8_lv5_th_ms & 0x7fff) << 16)
- | (opt_avalon8_lv4_th_ms & 0x7fff);
- tmp = be32toh(tmp);
- memcpy(send_pkg.data + 16, &tmp, 4);
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv5 th add %u",
- avalon8->drv->name, avalon8->device_id, addr,
- (opt_avalon8_lv5_th_add & 0x1));
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv5 th ms %u",
- avalon8->drv->name, avalon8->device_id, addr,
- (opt_avalon8_lv5_th_ms & 0x7fff));
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv4 th add %u",
- avalon8->drv->name, avalon8->device_id, addr,
- (opt_avalon8_lv4_th_add & 0x1));
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv4 th ms %u",
- avalon8->drv->name, avalon8->device_id, addr,
- (opt_avalon8_lv4_th_ms & 0x7fff));
- tmp = ((opt_avalon8_lv7_th_add & 0x1) << 31) | ((opt_avalon8_lv6_th_add & 0x1) << 15)
- | ((opt_avalon8_lv7_th_ms & 0x7fff) << 16)
- | (opt_avalon8_lv6_th_ms & 0x7fff);
- tmp = be32toh(tmp);
- memcpy(send_pkg.data + 20, &tmp, 4);
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv7 th add %u",
- avalon8->drv->name, avalon8->device_id, addr,
- (opt_avalon8_lv7_th_add & 0x1));
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv7 th ms %u",
- avalon8->drv->name, avalon8->device_id, addr,
- (opt_avalon8_lv7_th_ms & 0x7fff));
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv6 th add %u",
- avalon8->drv->name, avalon8->device_id, addr,
- (opt_avalon8_lv6_th_add & 0x1));
- applog(LOG_DEBUG, "%s-%d-%d: avalon8 set lv6 th ms %u",
- avalon8->drv->name, avalon8->device_id, addr,
- (opt_avalon8_lv6_th_ms & 0x7fff));
- /* Package the data */
- avalon8_init_pkg(&send_pkg, AVA8_P_SET_SS, 1, 1);
- if (addr == AVA8_MODULE_BROADCAST)
- avalon8_send_bc_pkgs(avalon8, &send_pkg);
- else
- avalon8_iic_xfer_pkg(avalon8, addr, &send_pkg, NULL);
- }
- static void avalon8_stratum_finish(struct cgpu_info *avalon8)
- {
- struct avalon8_pkg send_pkg;
- memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
- avalon8_init_pkg(&send_pkg, AVA8_P_JOB_FIN, 1, 1);
- avalon8_send_bc_pkgs(avalon8, &send_pkg);
- }
- static void avalon8_set_finish(struct cgpu_info *avalon8, int addr)
- {
- struct avalon8_pkg send_pkg;
- memset(send_pkg.data, 0, AVA8_P_DATA_LEN);
- avalon8_init_pkg(&send_pkg, AVA8_P_SET_FIN, 1, 1);
- avalon8_iic_xfer_pkg(avalon8, addr, &send_pkg, NULL);
- }
- static void avalon8_sswork_update(struct cgpu_info *avalon8)
- {
- struct avalon8_info *info = avalon8->device_data;
- struct thr_info *thr = avalon8->thr[0];
- struct pool *pool;
- int coinbase_len_posthash, coinbase_len_prehash;
- cgtime(&info->last_stratum);
- /*
- * NOTE: We need mark work_restart to private information,
- * So that it cann't reset by hash_driver_work
- */
- if (thr->work_restart)
- info->work_restart = thr->work_restart;
- applog(LOG_NOTICE, "%s-%d: New stratum: restart: %d, update: %d, clean: %d",
- avalon8->drv->name, avalon8->device_id,
- thr->work_restart, thr->work_update, thr->clean_jobs);
- /* Step 1: MM protocol check */
- pool = current_pool();
- if (!pool->has_stratum)
- quit(1, "%s-%d: MM has to use stratum pools", avalon8->drv->name, avalon8->device_id);
- coinbase_len_prehash = pool->nonce2_offset - (pool->nonce2_offset % SHA256_BLOCK_SIZE);
- coinbase_len_posthash = pool->coinbase_len - coinbase_len_prehash;
- if (coinbase_len_posthash + SHA256_BLOCK_SIZE > AVA8_P_COINBASE_SIZE) {
- applog(LOG_ERR, "%s-%d: MM pool modified coinbase length(%d) is more than %d",
- avalon8->drv->name, avalon8->device_id,
- coinbase_len_posthash + SHA256_BLOCK_SIZE, AVA8_P_COINBASE_SIZE);
- return;
- }
- if (pool->merkles > AVA8_P_MERKLES_COUNT) {
- applog(LOG_ERR, "%s-%d: MM merkles has to be less then %d", avalon8->drv->name, avalon8->device_id, AVA8_P_MERKLES_COUNT);
- return;
- }
- if (pool->n2size < 3) {
- applog(LOG_ERR, "%s-%d: MM nonce2 size has to be >= 3 (%d)", avalon8->drv->name, avalon8->device_id, pool->n2size);
- return;
- }
- cg_wlock(&info->update_lock);
- /* Step 2: Send out stratum pkgs */
- cg_rlock(&pool->data_lock);
- info->pool_no = pool->pool_no;
- copy_pool_stratum(&info->pool2, &info->pool1);
- copy_pool_stratum(&info->pool1, &info->pool0);
- copy_pool_stratum(&info->pool0, pool);
- avalon8_stratum_pkgs(avalon8, pool);
- cg_runlock(&pool->data_lock);
- /* Step 3: Send out finish pkg */
- avalon8_stratum_finish(avalon8);
- cg_wunlock(&info->update_lock);
- }
- static int64_t avalon8_scanhash(struct thr_info *thr)
- {
- struct cgpu_info *avalon8 = thr->cgpu;
- struct avalon8_info *info = avalon8->device_data;
- struct timeval current;
- int i, j, k, count = 0;
- int temp_max;
- int64_t ret;
- bool update_settings = false;
- if ((info->connecter == AVA8_CONNECTER_AUC) &&
- (unlikely(avalon8->usbinfo.nodev))) {
- applog(LOG_ERR, "%s-%d: Device disappeared, shutting down thread",
- avalon8->drv->name, avalon8->device_id);
- return -1;
- }
- /* Step 1: Stop polling and detach the device if there is no stratum in 3 minutes, network is down */
- cgtime(¤t);
- if (tdiff(¤t, &(info->last_stratum)) > 180.0) {
- for (i = 1; i < AVA8_DEFAULT_MODULARS; i++) {
- if (!info->enable[i])
- continue;
- detach_module(avalon8, i);
- }
- info->mm_count = 0;
- return 0;
- }
- /* Step 2: Try to detect new modules */
- if ((tdiff(¤t, &(info->last_detect)) > AVA8_MODULE_DETECT_INTERVAL) ||
- !info->mm_count) {
- cgtime(&info->last_detect);
- detect_modules(avalon8);
- }
- /* Step 3: ASIC configrations (voltage and frequency) */
- for (i = 1; i < AVA8_DEFAULT_MODULARS; i++) {
- if (!info->enable[i])
- continue;
- update_settings = false;
- /* Check temperautre */
- temp_max = get_temp_max(info, i);
- /* Enter too hot */
- if (temp_max >= info->temp_overheat[i])
- info->cutoff[i] = 1;
- /* Exit too hot */
- if (info->cutoff[i] && (temp_max <= (info->temp_overheat[i] - 10)))
- info->cutoff[i] = 0;
- switch (info->freq_mode[i]) {
- case AVA8_FREQ_INIT_MODE:
- update_settings = true;
- for (j = 0; j < info->miner_count[i]; j++) {
- for (k = 0; k < AVA8_DEFAULT_PLL_CNT; k++) {
- if (opt_avalon8_freq[k] != AVA8_DEFAULT_FREQUENCY_IGNORE)
- info->set_frequency[i][j][k] = opt_avalon8_freq[k];
- }
- }
- avalon8_init_setting(avalon8, i);
- info->freq_mode[i] = AVA8_FREQ_PLLADJ_MODE;
- break;
- case AVA8_FREQ_PLLADJ_MODE:
- if (opt_avalon8_smart_speed == AVA8_DEFAULT_SMARTSPEED_OFF)
- break;
- /* AVA8_DEFAULT_SMARTSPEED_MODE1: auto speed by A3210 chips */
- break;
- default:
- applog(LOG_ERR, "%s-%d-%d: Invalid frequency mode %d",
- avalon8->drv->name, avalon8->device_id, i, info->freq_mode[i]);
- break;
- }
- if (update_settings) {
- cg_wlock(&info->update_lock);
- avalon8_set_voltage_level(avalon8, i, info->set_voltage_level[i]);
- for (j = 0; j < info->miner_count[i]; j++)
- avalon8_set_freq(avalon8, i, j, info->set_frequency[i][j]);
- if (opt_avalon8_smart_speed)
- avalon8_set_ss_param(avalon8, i);
- avalon8_set_finish(avalon8, i);
- cg_wunlock(&info->update_lock);
- }
- }
- /* Step 4: Polling */
- cg_rlock(&info->update_lock);
- polling(avalon8);
- cg_runlock(&info->update_lock);
- /* Step 5: Calculate mm count */
- for (i = 1; i < AVA8_DEFAULT_MODULARS; i++) {
- if (info->enable[i])
- count++;
- }
- info->mm_count = count;
- /* Step 6: Calculate hashes. Use the diff1 value which is scaled by
- * device diff and is usually lower than pool diff which will give a
- * more stable result, but remove diff rejected shares to more closely
- * approximate diff accepted values. */
- info->pending_diff1 += avalon8->diff1 - info->last_diff1;
- info->last_diff1 = avalon8->diff1;
- info->pending_diff1 -= avalon8->diff_rejected - info->last_rej;
- info->last_rej = avalon8->diff_rejected;
- if (info->pending_diff1 && !info->firsthash.tv_sec) {
- cgtime(&info->firsthash);
- copy_time(&(avalon8->dev_start_tv), &(info->firsthash));
- }
- if (info->pending_diff1 <= 0)
- ret = 0;
- else {
- ret = info->pending_diff1;
- info->pending_diff1 = 0;
- }
- return ret * 0xffffffffull;
- }
- static float avalon8_hash_cal(struct cgpu_info *avalon8, int modular_id)
- {
- struct avalon8_info *info = avalon8->device_data;
- uint32_t tmp_freq[AVA8_DEFAULT_PLL_CNT];
- unsigned int i, j, k;
- float mhsmm;
- mhsmm = 0;
- for (i = 0; i < info->miner_count[modular_id]; i++) {
- for (j = 0; j < info->asic_count[modular_id]; j++) {
- for (k = 0; k < AVA8_DEFAULT_PLL_CNT; k++)
- mhsmm += (info->get_asic[modular_id][i][j][2 + k] * info->get_frequency[modular_id][i][k]);
- }
- }
- return mhsmm;
- }
- #define STATBUFLEN_WITHOUT_DBG (6 * 1024)
- #define STATBUFLEN_WITH_DBG (6 * 7 * 1024)
- static struct api_data *avalon8_api_stats(struct cgpu_info *avalon8)
- {
- struct api_data *root = NULL;
- struct avalon8_info *info = avalon8->device_data;
- int i, j, k, m;
- double a, b, dh;
- char buf[256];
- char *statbuf = NULL;
- struct timeval current;
- float mhsmm, auc_temp = 0.0;
- double sum;
- int avg, cnt, max_vl, max_id, min_vl, min_id;
- cgtime(¤t);
- if (opt_debug)
- statbuf = cgcalloc(STATBUFLEN_WITH_DBG, 1);
- else
- statbuf = cgcalloc(STATBUFLEN_WITHOUT_DBG, 1);
- for (i = 1; i < AVA8_DEFAULT_MODULARS; i++) {
- if (!info->enable[i])
- continue;
- sprintf(buf, "Ver[%s]", info->mm_version[i]);
- strcpy(statbuf, buf);
- sprintf(buf, " DNA[%02x%02x%02x%02x%02x%02x%02x%02x]",
- info->mm_dna[i][0],
- info->mm_dna[i][1],
- info->mm_dna[i][2],
- info->mm_dna[i][3],
- info->mm_dna[i][4],
- info->mm_dna[i][5],
- info->mm_dna[i][6],
- info->mm_dna[i][7]);
- strcat(statbuf, buf);
- sprintf(buf, " Elapsed[%.0f]", tdiff(¤t, &(info->elapsed[i])));
- strcat(statbuf, buf);
- strcat(statbuf, " MW[");
- info->local_works[i] = 0;
- for (j = 0; j < info->miner_count[i]; j++) {
- info->local_works[i] += info->local_works_i[i][j];
- sprintf(buf, "%"PRIu64" ", info->local_works_i[i][j]);
- strcat(statbuf, buf);
- }
- statbuf[strlen(statbuf) - 1] = ']';
- sprintf(buf, " LW[%"PRIu64"]", info->local_works[i]);
- strcat(statbuf, buf);
- strcat(statbuf, " MH[");
- info->hw_works[i] = 0;
- for (j = 0; j < info->miner_count[i]; j++) {
- info->hw_works[i] += info->hw_works_i[i][j];
- sprintf(buf, "%"PRIu64" ", info->hw_works_i[i][j]);
- strcat(statbuf, buf);
- }
- statbuf[strlen(statbuf) - 1] = ']';
- sprintf(buf, " HW[%"PRIu64"]", info->hw_works[i]);
- strcat(statbuf, buf);
- {
- double a, b, dh;
- a = 0;
- b = 0;
- for (j = 0; j < info->miner_count[i]; j++) {
- for (k = 0; k < info->asic_count[i]; k++) {
- a += info->get_asic[i][j][k][0];
- b += info->get_asic[i][j][k][1];
- }
- }
- dh = b ? (b / (a + b)) * 100 : 0;
- sprintf(buf, " DH[%.3f%%]", dh);
- strcat(statbuf, buf);
- }
- sprintf(buf, " Temp[%d]", info->temp_mm[i]);
- strcat(statbuf, buf);
- sprintf(buf, " TMax[%d]", get_temp_max(info, i));
- strcat(statbuf, buf);
- sprintf(buf, " Fan[%d]", info->fan_cpm[i]);
- strcat(statbuf, buf);
- sprintf(buf, " FanR[%d%%]", info->fan_pct[i]);
- strcat(statbuf, buf);
- sprintf(buf, " Vi[");
- strcat(statbuf, buf);
- for (j = 0; j < info->miner_count[i]; j++) {
- sprintf(buf, "%d ", info->get_vin[i][j]);
- strcat(statbuf, buf);
- }
- statbuf[strlen(statbuf) - 1] = ']';
- sprintf(buf, " Vo[");
- strcat(statbuf, buf);
- for (j = 0; j < info->miner_count[i]; j++) {
- sprintf(buf, "%d ", info->get_voltage[i][j]);
- strcat(statbuf, buf);
- }
- statbuf[strlen(statbuf) - 1] = ']';
- if (opt_debug) {
- for (j = 0; j < info->miner_count[i]; j++) {
- sprintf(buf, " PLL%d[", j);
- strcat(statbuf, buf);
- for (k = 0; k < AVA8_DEFAULT_PLL_CNT; k++) {
- sprintf(buf, "%d ", info->get_pll[i][j][k]);
- strcat(statbuf, buf);
- }
- statbuf[strlen(statbuf) - 1] = ']';
- }
- }
- mhsmm = avalon8_hash_cal(avalon8, i);
- sprintf(buf, " GHSmm[%.2f] WU[%.2f] Freq[%.2f]", (float)mhsmm / 1000,
- info->diff1[i] / tdiff(¤t, &(info->elapsed[i])) * 60.0,
- (float)mhsmm / (info->asic_count[i] * info->miner_count[i] * 256));
- strcat(statbuf, buf);
- sprintf(buf, " PG[%d]", info->power_good[i]);
- strcat(statbuf, buf);
- sprintf(buf, " Led[%d]", info->led_indicator[i]);
- strcat(statbuf, buf);
- for (j = 0; j < info->miner_count[i]; j++) {
- sprintf(buf, " MW%d[", j);
- strcat(statbuf, buf);
- for (k = 0; k < info->asic_count[i]; k++) {
- sprintf(buf, "%"PRIu64" ", info->chip_matching_work[i][j][k]);
- strcat(statbuf, buf);
- }
- statbuf[strlen(statbuf) - 1] = ']';
- }
- sprintf(buf, " TA[%d]", info->total_asics[i]);
- strcat(statbuf, buf);
- strcat(statbuf, " ECHU[");
- for (j = 0; j < info->miner_count[i]; j++) {
- sprintf(buf, "%d ", info->error_code[i][j]);
- strcat(statbuf, buf);
- }
- statbuf[strlen(statbuf) - 1] = ']';
- sprintf(buf, " ECMM[%d]", info->error_code[i][j]);
- strcat(statbuf, buf);
- if (opt_debug) {
- sprintf(buf, " FAC0[%d]", info->factory_info[0]);
- strcat(statbuf, buf);
- sprintf(buf, " OC[%d]", info->overclocking_info[0]);
- strcat(statbuf, buf);
- for (j = 0; j < info->miner_count[i]; j++) {
- sprintf(buf, " SF%d[", j);
- strcat(statbuf, buf);
- for (k = 0; k < AVA8_DEFAULT_PLL_CNT; k++) {
- sprintf(buf, "%d ", info->get_frequency[i][j][k]);
- strcat(statbuf, buf);
- }
- statbuf[strlen(statbuf) - 1] = ']';
- }
- strcat(statbuf, " PMUV[");
- for (j = 0; j < AVA8_DEFAULT_PMU_CNT; j++) {
- sprintf(buf, "%s ", info->pmu_version[i][j]);
- strcat(statbuf, buf);
- }
- statbuf[strlen(statbuf) - 1] = ']';
- for (j = 0; j < info->miner_count[i]; j++) {
- sprintf(buf, " PVT_T%d[", j);
- strcat(statbuf, buf);
- for (k = 0; k < info->asic_count[i]; k++) {
- sprintf(buf, "%3d ", info->temp[i][j][k]);
- strcat(statbuf, buf);
- }
- statbuf[strlen(statbuf) - 1] = ']';
- statbuf[strlen(statbuf)] = '\0';
- }
- for (j = 0; j < info->miner_count[i]; j++) {
- sprintf(buf, " PVT_V%d[", j);
- strcat(statbuf, buf);
- for (k = 0; k < info->asic_count[i]; k++) {
- sprintf(buf, "%d ", info->core_volt[i][j][k]);
- strcat(statbuf, buf);
- }
- statbuf[strlen(statbuf) - 1] = ']';
- statbuf[strlen(statbuf)] = '\0';
- }
- for (j = 0; j < info->miner_count[i]; j++) {
- sprintf(buf, " ERATIO%d[", j);
- strcat(statbuf, buf);
- for (k = 0; k < info->asic_count[i]; k++) {
- if (info->get_asic[i][j][k][0])
- sprintf(buf, "%6.2f%% ", (double)(info->get_asic[i][j][k][1] * 100.0 / (info->get_asic[i][j][k][0] + info->get_asic[i][j][k][1])));
- else
- sprintf(buf, "%6.2f%% ", 0.0);
- strcat(statbuf, buf);
- }
- statbuf[strlen(statbuf) - 1] = ']';
- }
- int l;
- /* i: modular, j: miner, k:asic, l:value */
- for (l = 0; l < 2; l++) {
- for (j = 0; j < info->miner_count[i]; j++) {
- sprintf(buf, " C_%02d_%02d[", j, l);
- strcat(statbuf, buf);
- for (k = 0; k < info->asic_count[i]; k++) {
- sprintf(buf, "%7d ", info->get_asic[i][j][k][l]);
- strcat(statbuf, buf);
- }
- statbuf[strlen(statbuf) - 1] = ']';
- }
- }
- for (j = 0; j < info->miner_count[i]; j++) {
- sprintf(buf, " GHSmm%02d[", j);
- strcat(statbuf, buf);
- for (k = 0; k < info->asic_count[i]; k++) {
- mhsmm = 0;
- for (l = 2; l < (2 + AVA8_DEFAULT_PLL_CNT); l++) {
- mhsmm += (info->get_asic[i][j][k][l] * info->get_frequency[i][j][l - 2]);
- }
- sprintf(buf, "%7.2f ", mhsmm / 1000);
- strcat(statbuf, buf);
- }
- statbuf[strlen(statbuf) - 1] = ']';
- }
- }
- sprintf(buf, " FM[%d]", info->freq_mode[i]);
- strcat(statbuf, buf);
- strcat(statbuf, " CRC[");
- for (j = 0; j < info->miner_count[i]; j++) {
- sprintf(buf, "%d ", info->error_crc[i][j]);
- strcat(statbuf, buf);
- }
- statbuf[strlen(statbuf) - 1] = ']';
- sprintf(buf, "MM ID%d", i);
- root = api_add_string(root, buf, statbuf, true);
- }
- free(statbuf);
- root = api_add_int(root, "MM Count", &(info->mm_count), true);
- root = api_add_int(root, "Smart Speed", &opt_avalon8_smart_speed, true);
- if (info->connecter == AVA8_CONNECTER_IIC)
- root = api_add_string(root, "Connecter", "IIC", true);
- if (info->connecter == AVA8_CONNECTER_AUC) {
- root = api_add_string(root, "Connecter", "AUC", true);
- root = api_add_string(root, "AUC VER", info->auc_version, false);
- root = api_add_int(root, "AUC I2C Speed", &(info->auc_speed), true);
- root = api_add_int(root, "AUC I2C XDelay", &(info->auc_xdelay), true);
- root = api_add_int(root, "AUC Sensor", &(info->auc_sensor), true);
- auc_temp = decode_auc_temp(info->auc_sensor);
- root = api_add_temp(root, "AUC Temperature", &auc_temp, true);
- }
- root = api_add_bool(root, "Connection Overloaded", &info->conn_overloaded, true);
- root = api_add_int(root, "Voltage Level Offset", &opt_avalon8_voltage_level_offset, true);
- root = api_add_uint32(root, "Nonce Mask", &opt_avalon8_nonce_mask, true);
- return root;
- }
- /* format: voltage[-addr[-miner]]
- * addr[0, AVA8_DEFAULT_MODULARS - 1], 0 means all modulars
- * miner[0, miner_count], 0 means all miners
- */
- char *set_avalon8_device_voltage_level(struct cgpu_info *avalon8, char *arg)
- {
- struct avalon8_info *info = avalon8->device_data;
- int val;
- unsigned int addr = 0, i, j;
- uint32_t miner_id = 0;
- if (!(*arg))
- return NULL;
- sscanf(arg, "%d-%d-%d", &val, &addr, &miner_id);
- if (val < AVA8_DEFAULT_VOLTAGE_LEVEL_MIN || val > AVA8_DEFAULT_VOLTAGE_LEVEL_MAX)
- return "Invalid value passed to set_avalon8_device_voltage_level";
- if (addr >= AVA8_DEFAULT_MODULARS) {
- applog(LOG_ERR, "invalid modular index: %d, valid range 0-%d", addr, (AVA8_DEFAULT_MODULARS - 1));
- return "Invalid modular index to set_avalon8_device_voltage_level";
- }
- if (!addr) {
- for (i = 1; i < AVA8_DEFAULT_MODULARS; i++) {
- if (!info->enable[i])
- continue;
- if (miner_id > info->miner_count[i]) {
- applog(LOG_ERR, "invalid miner index: %d, valid range 0-%d", miner_id, info->miner_count[i]);
- return "Invalid miner index to set_avalon8_device_voltage_level";
- }
- if (miner_id)
- info->set_voltage_level[i][miner_id - 1] = val;
- else {
- for (j = 0; j < info->miner_count[i]; j++)
- info->set_voltage_level[i][j] = val;
- }
- avalon8_set_voltage_level(avalon8, i, info->set_voltage_level[i]);
- }
- } else {
- if (!info->enable[addr]) {
- applog(LOG_ERR, "Disabled modular:%d", addr);
- return "Disabled modular to set_avalon8_device_voltage_level";
- }
- if (miner_id > info->miner_count[addr]) {
- applog(LOG_ERR, "invalid miner index: %d, valid range 0-%d", miner_id, info->miner_count[addr]);
- return "Invalid miner index to set_avalon8_device_voltage_level";
- }
- if (miner_id)
- info->set_voltage_level[addr][miner_id - 1] = val;
- else {
- for (j = 0; j < info->miner_count[addr]; j++)
- info->set_voltage_level[addr][j] = val;
- }
- avalon8_set_voltage_level(avalon8, addr, info->set_voltage_level[addr]);
- }
- applog(LOG_NOTICE, "%s-%d: Update voltage-level to %d", avalon8->drv->name, avalon8->device_id, val);
- return NULL;
- }
- /*
- * format: freq[-addr[-miner]]
- * addr[0, AVA8_DEFAULT_MODULARS - 1], 0 means all modulars
- * miner[0, miner_count], 0 means all miners
- */
- char *set_avalon8_device_freq(struct cgpu_info *avalon8, char *arg)
- {
- struct avalon8_info *info = avalon8->device_data;
- unsigned int val, addr = 0, i, j, k;
- uint32_t miner_id = 0;
- if (!(*arg))
- return NULL;
- sscanf(arg, "%d-%d-%d", &val, &addr, &miner_id);
- if (val > AVA8_DEFAULT_FREQUENCY_MAX)
- return "Invalid value passed to set_avalon8_device_freq";
- if (addr >= AVA8_DEFAULT_MODULARS) {
- applog(LOG_ERR, "invalid modular index: %d, valid range 0-%d", addr, (AVA8_DEFAULT_MODULARS - 1));
- return "Invalid modular index to set_avalon8_device_freq";
- }
- if (!addr) {
- for (i = 1; i < AVA8_DEFAULT_MODULARS; i++) {
- if (!info->enable[i])
- continue;
- if (miner_id > info->miner_count[i]) {
- applog(LOG_ERR, "invalid miner index: %d, valid range 0-%d", miner_id, info->miner_count[i]);
- return "Invalid miner index to set_avalon8_device_freq";
- }
- if (miner_id) {
- for (k = 0; k < AVA8_DEFAULT_PLL_CNT; k++)
- info->set_frequency[i][miner_id - 1][k] = val;
- avalon8_set_freq(avalon8, i, miner_id - 1, info->set_frequency[i][miner_id - 1]);
- } else {
- for (j = 0; j < info->miner_count[i]; j++) {
- for (k = 0; k < AVA8_DEFAULT_PLL_CNT; k++)
- info->set_frequency[i][j][k] = val;
- avalon8_set_freq(avalon8, i, j, info->set_frequency[i][j]);
- }
- }
- }
- } else {
- if (!info->enable[addr]) {
- applog(LOG_ERR, "Disabled modular:%d", addr);
- return "Disabled modular to set_avalon8_device_freq";
- }
- if (miner_id > info->miner_count[addr]) {
- applog(LOG_ERR, "invalid miner index: %d, valid range 0-%d", miner_id, info->miner_count[addr]);
- return "Invalid miner index to set_avalon8_device_freq";
- }
- if (miner_id) {
- for (k = 0; k < AVA8_DEFAULT_PLL_CNT; k++)
- info->set_frequency[addr][miner_id - 1][k] = val;
- avalon8_set_freq(avalon8, addr, miner_id - 1, info->set_frequency[addr][miner_id - 1]);
- } else {
- for (j = 0; j < info->miner_count[addr]; j++) {
- for (k = 0; k < AVA8_DEFAULT_PLL_CNT; k++)
- info->set_frequency[addr][j][k] = val;
- avalon8_set_freq(avalon8, addr, j, info->set_frequency[addr][j]);
- }
- }
- }
- applog(LOG_NOTICE, "%s-%d: Update frequency to %d",
- avalon8->drv->name, avalon8->device_id, val);
- return NULL;
- }
- char *set_avalon8_factory_info(struct cgpu_info *avalon8, char *arg)
- {
- struct avalon8_info *info = avalon8->device_data;
- char type[AVA8_DEFAULT_FACTORY_INFO_1_CNT];
- int val;
- if (!(*arg))
- return NULL;
- memset(type, 0, AVA8_DEFAULT_FACTORY_INFO_1_CNT);
- sscanf(arg, "%d-%s", &val, type);
- if ((val != AVA8_DEFAULT_FACTORY_INFO_0_IGNORE) &&
- (val < AVA8_DEFAULT_FACTORY_INFO_0_MIN || val > AVA8_DEFAULT_FACTORY_INFO_0_MAX))
- return "Invalid value passed to set_avalon8_factory_info";
- info->factory_info[0] = val;
- memcpy(&info->factory_info[1], type, AVA8_DEFAULT_FACTORY_INFO_1_CNT);
- avalon8_set_factory_info(avalon8, 0, (uint8_t *)info->factory_info);
- applog(LOG_NOTICE, "%s-%d: Update factory info %d",
- avalon8->drv->name, avalon8->device_id, val);
- return NULL;
- }
- char *set_avalon8_overclocking_info(struct cgpu_info *avalon8, char *arg)
- {
- struct avalon8_info *info = avalon8->device_data;
- int val;
- if (!(*arg))
- return NULL;
- sscanf(arg, "%d", &val);
- if (val != AVA8_DEFAULT_OVERCLOCKING_OFF && val != AVA8_DEFAULT_OVERCLOCKING_ON)
- return "Invalid value passed to set_avalon8_overclocking_info";
- info->overclocking_info[0] = val;
- avalon8_set_overclocking_info(avalon8, 0, (uint8_t *)info->overclocking_info);
- applog(LOG_NOTICE, "%s-%d: Update Overclocking info %d",
- avalon8->drv->name, avalon8->device_id, val);
- return NULL;
- }
- static char *avalon8_set_device(struct cgpu_info *avalon8, char *option, char *setting, char *replybuf, size_t siz)
- {
- unsigned int val;
- struct avalon8_info *info = avalon8->device_data;
- if (strcasecmp(option, "help") == 0) {
- snprintf(replybuf, siz, "pdelay|fan|frequency|led|voltage");
- return replybuf;
- }
- if (strcasecmp(option, "pdelay") == 0) {
- if (!setting || !*setting) {
- snprintf(replybuf, siz, "missing polling delay setting");
- return replybuf;
- }
- val = (unsigned int)atoi(setting);
- if (val < 1 || val > 65535) {
- snprintf(replybuf, siz, "invalid polling delay: %d, valid range 1-65535", val);
- return replybuf;
- }
- opt_avalon8_polling_delay = val;
- applog(LOG_NOTICE, "%s-%d: Update polling delay to: %d",
- avalon8->drv->name, avalon8->device_id, val);
- return NULL;
- }
- if (strcasecmp(option, "fan") == 0) {
- if (!setting || !*setting) {
- snprintf(replybuf, siz, "missing fan value");
- return replybuf;
- }
- if (set_avalon8_fan(setting)) {
- snprintf(replybuf, siz, "invalid fan value, valid range 0-100");
- return replybuf;
- }
- applog(LOG_NOTICE, "%s-%d: Update fan to %d-%d",
- avalon8->drv->name, avalon8->device_id,
- opt_avalon8_fan_min, opt_avalon8_fan_max);
- return NULL;
- }
- if (strcasecmp(option, "frequency") == 0) {
- if (!setting || !*setting) {
- snprintf(replybuf, siz, "missing frequency value");
- return replybuf;
- }
- return set_avalon8_device_freq(avalon8, setting);
- }
- if (strcasecmp(option, "led") == 0) {
- int val_led = -1;
- if (!setting || !*setting) {
- snprintf(replybuf, siz, "missing module_id setting");
- return replybuf;
- }
- sscanf(setting, "%d-%d", &val, &val_led);
- if (val < 1 || val >= AVA8_DEFAULT_MODULARS) {
- snprintf(replybuf, siz, "invalid module_id: %d, valid range 1-%d", val, AVA8_DEFAULT_MODULARS);
- return replybuf;
- }
- if (!info->enable[val]) {
- snprintf(replybuf, siz, "the current module was disabled %d", val);
- return replybuf;
- }
- if (val_led == -1)
- info->led_indicator[val] = !info->led_indicator[val];
- else {
- if (val_led < 0 || val_led > 1) {
- snprintf(replybuf, siz, "invalid LED status: %d, valid value 0|1", val_led);
- return replybuf;
- }
- if (val_led != info->led_indicator[val])
- info->led_indicator[val] = val_led;
- }
- applog(LOG_NOTICE, "%s-%d: Module:%d, LED: %s",
- avalon8->drv->name, avalon8->device_id,
- val, info->led_indicator[val] ? "on" : "off");
- return NULL;
- }
- if (strcasecmp(option, "voltage-level") == 0) {
- if (!setting || !*setting) {
- snprintf(replybuf, siz, "missing voltage-level value");
- return replybuf;
- }
- return set_avalon8_device_voltage_level(avalon8, setting);
- }
- if (strcasecmp(option, "factory") == 0) {
- if (!setting || !*setting) {
- snprintf(replybuf, siz, "missing factory info");
- return replybuf;
- }
- return set_avalon8_factory_info(avalon8, setting);
- }
- if (strcasecmp(option, "reboot") == 0) {
- if (!setting || !*setting) {
- snprintf(replybuf, siz, "missing reboot value");
- return replybuf;
- }
- sscanf(setting, "%d", &val);
- if (val < 1 || val >= AVA8_DEFAULT_MODULARS) {
- snprintf(replybuf, siz, "invalid module_id: %d, valid range 1-%d", val, AVA8_DEFAULT_MODULARS);
- return replybuf;
- }
- info->reboot[val] = true;
- return NULL;
- }
- if (strcasecmp(option, "overclocking") == 0) {
- if (!setting || !*setting) {
- snprintf(replybuf, siz, "missing overclocking info");
- return replybuf;
- }
- return set_avalon8_overclocking_info(avalon8, setting);
- }
- snprintf(replybuf, siz, "Unknown option: %s", option);
- return replybuf;
- }
- static void avalon8_statline_before(char *buf, size_t bufsiz, struct cgpu_info *avalon8)
- {
- struct avalon8_info *info = avalon8->device_data;
- int temp = -273;
- int fanmin = AVA8_DEFAULT_FAN_MAX;
- int i, j, k;
- uint32_t frequency = 0;
- float ghs_sum = 0, mhsmm = 0;
- double pass_num = 0.0, fail_num = 0.0;
- for (i = 1; i < AVA8_DEFAULT_MODULARS; i++) {
- if (!info->enable[i])
- continue;
- if (fanmin >= info->fan_pct[i])
- fanmin = info->fan_pct[i];
- if (temp < get_temp_max(info, i))
- temp = get_temp_max(info, i);
- mhsmm = avalon8_hash_cal(avalon8, i);
- frequency += (mhsmm / (info->asic_count[i] * info->miner_count[i] * 256));
- ghs_sum += (mhsmm / 1000);
- for (j = 0; j < info->miner_count[i]; j++) {
- for (k = 0; k < info->asic_count[i]; k++) {
- pass_num += info->get_asic[i][j][k][0];
- fail_num += info->get_asic[i][j][k][1];
- }
- }
- }
- if (info->mm_count)
- frequency /= info->mm_count;
- tailsprintf(buf, bufsiz, "%4dMhz %.2fGHS %2dC %.2f%% %3d%%", frequency, ghs_sum, temp,
- (fail_num + pass_num) ? fail_num * 100.0 / (fail_num + pass_num) : 0, fanmin);
- }
- struct device_drv avalon8_drv = {
- .drv_id = DRIVER_avalon8,
- .dname = "avalon8",
- .name = "AV8",
- .set_device = avalon8_set_device,
- .get_api_stats = avalon8_api_stats,
- .get_statline_before = avalon8_statline_before,
- .drv_detect = avalon8_detect,
- .thread_prepare = avalon8_prepare,
- .hash_work = hash_driver_work,
- .flush_work = avalon8_sswork_update,
- .update_work = avalon8_sswork_update,
- .scanwork = avalon8_scanhash,
- .max_diff = AVA8_DRV_DIFFMAX,
- .genwork = true,
- };
|