driver-gekko.c 133 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680
  1. /*
  2. * Copyright 2017-2021 vh
  3. * Copyright 2021-2022 sidehack
  4. * Copyright 2021-2023 kano
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include "driver-gekko.h"
  12. #include "crc.h"
  13. #include "compat.h"
  14. #include <unistd.h>
  15. #ifdef __GNUC__
  16. #if __GNUC__ >= 7
  17. #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
  18. #endif
  19. #endif
  20. // usleep reliability
  21. #if defined(__APPLE__)
  22. #define USLEEPMIN 2000
  23. #define USLEEPPLUS 200
  24. #elif defined (WIN32)
  25. #define USLEEPMIN 250
  26. #define USLEEPPLUS 100
  27. #else
  28. #define USLEEPMIN 200
  29. #define USLEEPPLUS 50
  30. #endif
  31. static bool compac_prepare(struct thr_info *thr);
  32. static pthread_mutex_t static_lock = PTHREAD_MUTEX_INITIALIZER;
  33. static bool last_widescreen;
  34. static uint8_t dev_init_count[0xffff] = {0};
  35. static uint8_t *init_count;
  36. static uint32_t stat_len;
  37. static uint32_t chip_max;
  38. #define MS2US(_n) ((_n) * 1000)
  39. // report averages and how far they overrun the requested time
  40. // linux would appear to be unable to handle less than 55us
  41. // on the RPi4 it would regularly sleep 3 times as long
  42. // thus code in general ignores sleeping anything less than 200us
  43. #define TUNE_CODE 1
  44. static void gekko_usleep(struct COMPAC_INFO *info, int usec)
  45. {
  46. #if TUNE_CODE
  47. struct timeval stt, fin;
  48. double td, fac;
  49. #endif
  50. // error for usleep()
  51. if (usec >= 1000000)
  52. {
  53. cgsleep_ms(usec / 1000);
  54. #if TUNE_CODE
  55. mutex_lock(&info->slock);
  56. info->inv++;
  57. mutex_unlock(&info->slock);
  58. #endif
  59. return;
  60. }
  61. #if TUNE_CODE
  62. cgtime(&stt);
  63. #endif
  64. usleep(usec);
  65. #if TUNE_CODE
  66. cgtime(&fin);
  67. td = us_tdiff(&fin, &stt);
  68. fac = (td / (double)usec);
  69. mutex_lock(&info->slock);
  70. if (td < usec)
  71. info->num0++;
  72. if (fac >= 1.5)
  73. {
  74. info->req1_5 += usec;
  75. info->fac1_5 += fac;
  76. info->num1_5++;
  77. }
  78. else
  79. {
  80. if (fac >= 1.1)
  81. {
  82. info->req1_1 += usec;
  83. info->fac1_1 += fac;
  84. info->num1_1++;
  85. }
  86. else
  87. {
  88. info->req += usec;
  89. info->fac += fac;
  90. info->num++;
  91. }
  92. }
  93. mutex_unlock(&info->slock);
  94. #endif
  95. }
  96. static float fbound(float value, float lower_bound, float upper_bound)
  97. {
  98. if (value < lower_bound)
  99. return lower_bound;
  100. if (value > upper_bound)
  101. return upper_bound;
  102. return value;
  103. }
  104. uint32_t bmcrc(unsigned char *ptr, uint32_t len)
  105. {
  106. unsigned char c[5] = {1, 1, 1, 1, 1};
  107. uint32_t i, c1, ptr_idx = 0;
  108. for (i = 0; i < len; i++) {
  109. c1 = c[1];
  110. c[1] = c[0];
  111. c[0] = c[4] ^ ((ptr[ptr_idx] & (0x80 >> (i % 8))) ? 1 : 0);
  112. c[4] = c[3];
  113. c[3] = c[2];
  114. c[2] = c1 ^ c[0];
  115. if (((i + 1) % 8) == 0)
  116. ptr_idx++;
  117. }
  118. return (c[4] * 0x10) | (c[3] * 0x08) | (c[2] * 0x04) | (c[1] * 0x02) | (c[0] * 0x01);
  119. }
  120. void dumpbuffer(struct cgpu_info *compac, int LOG_LEVEL, char *note, unsigned char *ptr, uint32_t len)
  121. {
  122. if (opt_log_output || LOG_LEVEL <= opt_log_level) {
  123. char str[2048];
  124. const char * hex = "0123456789ABCDEF";
  125. char * pout = str;
  126. unsigned int i = 0;
  127. for(; i < 768 && i < len - 1; ++i) {
  128. *pout++ = hex[(*ptr>>4)&0xF];
  129. *pout++ = hex[(*ptr++)&0xF];
  130. if (i % 42 == 41) {
  131. *pout = 0;
  132. pout = str;
  133. applog(LOG_LEVEL, "%i: %s %s: %s", compac->cgminer_id, compac->drv->name, note, str);
  134. } else {
  135. *pout++ = ':';
  136. }
  137. }
  138. *pout++ = hex[(*ptr>>4)&0xF];
  139. *pout++ = hex[(*ptr)&0xF];
  140. *pout = 0;
  141. applog(LOG_LEVEL, "%d: %s %d - %s: %s", compac->cgminer_id, compac->drv->name, compac->device_id, note, str);
  142. }
  143. }
  144. static int compac_micro_send(struct cgpu_info *compac, uint8_t cmd, uint8_t channel, uint8_t value)
  145. {
  146. struct COMPAC_INFO *info = compac->device_data;
  147. int bytes = 1;
  148. int read_bytes = 1;
  149. int micro_temp;
  150. uint8_t temp;
  151. unsigned short usb_val;
  152. __maybe_unused char null[255];
  153. // synchronous : safe to run in the listen thread.
  154. if (!info->micro_found) {
  155. return 0;
  156. }
  157. // Baud Rate : 500,000
  158. usb_val = (FTDI_BITMODE_CBUS << 8) | 0xF3; // low byte: bitmask - 1111 0011 - CB1(HI), CB0(HI)
  159. usb_transfer(compac, FTDI_TYPE_OUT, FTDI_REQUEST_BITMODE, usb_val, info->interface, C_SETMODEM);
  160. gekko_usleep(info, MS2US(2));
  161. //usb_transfer(compac, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, 0x06, (FTDI_INDEX_BAUD_BTS & 0xff00) | info->interface, C_SETBAUD);
  162. info->cmd[0] = cmd | channel;
  163. info->cmd[1] = value;
  164. if (value != 0x00 || cmd == M2_SET_VCORE) {
  165. bytes = 2;
  166. }
  167. usb_read_timeout(compac, (char *)info->rx, 255, &read_bytes, 1, C_GETRESULTS);
  168. dumpbuffer(compac, LOG_INFO, "(micro) TX", info->cmd, bytes);
  169. usb_write(compac, (char *)info->cmd, bytes, &read_bytes, C_REQUESTRESULTS);
  170. memset(info->rx, 0, info->rx_len);
  171. usb_read_timeout(compac, (char *)info->rx, 1, &read_bytes, 5, C_GETRESULTS);
  172. if (read_bytes > 0) {
  173. dumpbuffer(compac, LOG_INFO, "(micro) RX", info->rx, read_bytes);
  174. switch (cmd) {
  175. case 0x20:
  176. temp = info->rx[0];
  177. micro_temp = 32 + 1.8 * temp;
  178. if (micro_temp != info->micro_temp) {
  179. info->micro_temp = micro_temp;
  180. applog(LOG_WARNING, "%d: %s %d - micro temp changed to %d°C / %.1f°F",
  181. compac->cgminer_id, compac->drv->name, compac->device_id, temp, info->micro_temp);
  182. }
  183. break;
  184. default:
  185. break;
  186. }
  187. }
  188. // Restore Baud Rate
  189. //usb_transfer(compac, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, (info->bauddiv + 1), (FTDI_INDEX_BAUD_BTS & 0xff00) | info->interface, C_SETBAUD);
  190. usb_val = (FTDI_BITMODE_CBUS << 8) | 0xF2; // low byte: bitmask - 1111 0010 - CB1(HI), CB0(LO)
  191. usb_transfer(compac, FTDI_TYPE_OUT, FTDI_REQUEST_BITMODE, usb_val, info->interface, C_SETMODEM);
  192. gekko_usleep(info, MS2US(2));
  193. return read_bytes;
  194. }
  195. #define compac_send(_c, _r, _b, _crc) compac_send2(_c, _r, _b, _crc, NULL)
  196. static void compac_send2(struct cgpu_info *compac, unsigned char *req_tx, uint32_t bytes, uint32_t crc_bits, __maybe_unused char *msg)
  197. {
  198. struct COMPAC_INFO *info = compac->device_data;
  199. int read_bytes = 1;
  200. unsigned int i, off = 0;
  201. // leave original buffer intact
  202. if (info->asic_type == BM1397)
  203. {
  204. info->cmd[0] = 0x55;
  205. info->cmd[1] = 0xAA;
  206. off = 2;
  207. }
  208. for (i = 0; i < bytes; i++)
  209. info->cmd[i+off] = req_tx[i];
  210. bytes += off;
  211. info->cmd[bytes-1] |= bmcrc(req_tx, crc_bits);
  212. #if 0
  213. if (msg == NULL)
  214. msg = "null";
  215. applog(LOG_ERR, "%s() %d: %s %d - Send len %3u (%s)", __func__,
  216. compac->cgminer_id, compac->drv->name, compac->device_id, bytes, msg);
  217. applog(LOG_ERR, "%s() [%02x %02x %02x %02x %02x %02x %02x %02x]", __func__,
  218. info->cmd[0], info->cmd[1], info->cmd[2], info->cmd[3], info->cmd[4], info->cmd[5], info->cmd[6], info->cmd[7]);
  219. applog(LOG_ERR, "%s() [%02x %02x %02x %02x %02x %02x %02x %02x]", __func__,
  220. info->cmd[8], info->cmd[9], info->cmd[10], info->cmd[11], info->cmd[12], info->cmd[13], info->cmd[14], info->cmd[15]);
  221. #endif
  222. int log_level = (bytes < info->task_len) ? LOG_INFO : LOG_INFO;
  223. dumpbuffer(compac, log_level, "TX", info->cmd, bytes);
  224. usb_write(compac, (char *)(info->cmd), bytes, &read_bytes, C_REQUESTRESULTS);
  225. //let the usb frame propagate
  226. if (info->asic_type == BM1397)
  227. gekko_usleep(info, info->usb_prop);
  228. else
  229. gekko_usleep(info, MS2US(1));
  230. }
  231. static float limit_freq(struct COMPAC_INFO *info, float freq, bool zero)
  232. {
  233. switch(info->ident)
  234. {
  235. case IDENT_BSC:
  236. case IDENT_GSC:
  237. case IDENT_BSD:
  238. case IDENT_GSD:
  239. case IDENT_BSE:
  240. case IDENT_GSE:
  241. freq = fbound(freq, info->freq_base, 500);
  242. break;
  243. case IDENT_GSH:
  244. case IDENT_BAX:
  245. case IDENT_GSI:
  246. freq = fbound(freq, 50, 900);
  247. break;
  248. case IDENT_GSF:
  249. case IDENT_GSFM:
  250. // allow 0 also if zero is true - coded obviously
  251. if (zero && freq == 0)
  252. freq = 0;
  253. else
  254. freq = fbound(freq, 100, 800);
  255. break;
  256. default:
  257. // 'should' never happen ...
  258. freq = fbound(freq, 100, 300);
  259. break;
  260. }
  261. return freq;
  262. }
  263. static void ping_freq(struct cgpu_info *compac, int asic)
  264. {
  265. struct COMPAC_INFO *info = compac->device_data;
  266. bool ping = false;
  267. if (info->asic_type == BM1397)
  268. {
  269. unsigned char pingall[] = {0x52, 0x05, 0x00, BM1397FREQ, 0x00};
  270. compac_send2(compac, pingall, sizeof(pingall), 8 * sizeof(pingall) - 8, "pingfreq");
  271. ping = true;
  272. }
  273. else if (info->asic_type == BM1387)
  274. {
  275. unsigned char buffer[] = {0x44, 0x05, 0x00, 0x0C, 0x00}; // PLL_PARAMETER
  276. buffer[2] = (0x100 / info->chips) * asic;
  277. compac_send(compac, buffer, sizeof(buffer), 8 * sizeof(buffer) - 8);
  278. ping = true;
  279. }
  280. else if (info->asic_type == BM1384)
  281. {
  282. unsigned char buffer[] = {0x04, 0x00, 0x04, 0x00};
  283. buffer[1] = (0x100 / info->chips) * asic;
  284. compac_send(compac, buffer, sizeof(buffer), 8 * sizeof(buffer) - 5);
  285. ping = true;
  286. }
  287. if (ping)
  288. {
  289. cgtime(&info->last_frequency_ping);
  290. cgtime(&(info->asics[asic].last_frequency_ping));
  291. }
  292. }
  293. static void gsf_calc_nb2c(struct cgpu_info *compac)
  294. {
  295. struct COMPAC_INFO *info = compac->device_data;
  296. int c, i, j;
  297. double fac;
  298. if (info->chips == 1)
  299. {
  300. // default all 0 is correct
  301. info->nb2c_setup = true;
  302. }
  303. else if (info->chips == 6)
  304. {
  305. // groups of 4
  306. fac = CHIPPY1397(info, 1) / 4.0;
  307. for (i = 0; i < 256; i += 64)
  308. {
  309. for (j = 0; j < 64; j++)
  310. {
  311. c = (int)((double)j / fac);
  312. if (c >= (int)(info->chips))
  313. c = info->chips - 1;
  314. info->nb2chip[i + j] = c;
  315. }
  316. }
  317. info->nb2c_setup = true;
  318. }
  319. }
  320. static void gc_wipe(struct GEKKOCHIP *gc, struct timeval *now)
  321. {
  322. // clear out everything
  323. gc->zerosec = now->tv_sec;
  324. gc->offset = 0;
  325. memset(gc->noncenum, 0, sizeof(gc->noncenum));
  326. gc->noncesum = 0;
  327. gc->last = 0;
  328. }
  329. static void gc_wipe_all(struct COMPAC_INFO *info, struct timeval *now, bool locked)
  330. {
  331. int i;
  332. if (!locked)
  333. mutex_lock(&info->ghlock);
  334. for (i = 0; i < (int)(info->chips); i++)
  335. gc_wipe(&(info->asics[i].gc), now);
  336. if (!locked)
  337. mutex_unlock(&info->ghlock);
  338. }
  339. // update asic->gc offset as at 'now' and correct values
  340. // info must be locked, wipe creates a new data set
  341. static void gc_offset(struct COMPAC_INFO *info, struct ASIC_INFO *asic, struct timeval *now, bool wipe, bool locked)
  342. {
  343. struct GEKKOCHIP *gc = &(asic->gc);
  344. time_t delta;
  345. if (!locked)
  346. mutex_lock(&info->ghlock);
  347. // wipe or delta != 0
  348. if (wipe || !CHCMP(gc->zerosec, now->tv_sec))
  349. {
  350. // clear some/all delta data
  351. delta = CHBASE(now->tv_sec) - CHBASE(gc->zerosec);
  352. // if time goes back, also reset everything
  353. // a forward jump of CHNUM will reset the whole buffer
  354. if (wipe || delta < 0 || delta >= CHNUM)
  355. gc_wipe(gc, now);
  356. else
  357. {
  358. // delta is > 0, but usually always 1 unless,
  359. // due to asic failure, a 10 minutes had no nonces
  360. // however the loop will total 1 iteration each
  361. // 10 minutes elapsed real time e.g. if not called
  362. // for 30 minutes, it will loop 3 times
  363. // there is also a CHNUM-1 limit on that
  364. gc->zerosec = now->tv_sec;
  365. // clear out the old values
  366. do
  367. {
  368. gc->offset = CHOFF(gc->offset+1);
  369. gc->noncesum -= gc->noncenum[CHOFF(gc->offset)];
  370. gc->noncenum[CHOFF(gc->offset)] = 0;
  371. if (gc->last < (CHNUM-1))
  372. gc->last++;
  373. }
  374. while (--delta > 0);
  375. }
  376. }
  377. // if there's been no nonces up to now, history must already be all zero
  378. // so just remove history
  379. if (gc->noncesum == 0 && gc->last > 0)
  380. gc->last = 0;
  381. if (!locked)
  382. mutex_unlock(&info->ghlock);
  383. }
  384. // update info->gh offset as at 'now' and correct values
  385. // info must be locked, wipe creates a new data set and also wipes all asic->gc
  386. static void gh_offset(struct COMPAC_INFO *info, struct timeval *now, bool wipe, bool locked)
  387. {
  388. struct GEKKOHASH *gh = &(info->gh);
  389. time_t delta;
  390. int i;
  391. if (!locked)
  392. mutex_lock(&info->ghlock);
  393. // first time in, wipe is ignored (it's already all zero)
  394. if (gh->zerosec == 0)
  395. {
  396. gh->zerosec = now->tv_sec;
  397. for (i = 0; i < (int)(info->chips); i++)
  398. info->asics[i].gc.zerosec = now->tv_sec;
  399. }
  400. else
  401. {
  402. if (wipe)
  403. gc_wipe_all(info, now, true);
  404. // wipe or delta != 0
  405. if (wipe || gh->zerosec != now->tv_sec)
  406. {
  407. // clear some/all delta data
  408. delta = now->tv_sec - gh->zerosec;
  409. // if time goes back, also reset everything
  410. // N.B. a forward time jump between 2 and GHLIMsec
  411. // seconds will reduce the hash rate value
  412. // but GHLIMsec or more will reset the whole buffer
  413. if (wipe || delta < 0 || delta >= GHLIMsec)
  414. {
  415. // clear out everything
  416. gh->zerosec = now->tv_sec;
  417. gh->offset = 0;
  418. memset(gh->diff, 0, sizeof(gh->diff));
  419. memset(gh->firstt, 0, sizeof(gh->firstt));
  420. memset(gh->firstd, 0, sizeof(gh->firstd));
  421. memset(gh->lastt, 0, sizeof(gh->lastt));
  422. memset(gh->noncenum, 0, sizeof(gh->noncenum));
  423. gh->diffsum = 0;
  424. gh->noncesum = 0;
  425. gh->last = 0;
  426. }
  427. else
  428. {
  429. // delta is > 0, but usually always 1 unless,
  430. // due to asic failure, a second had no nonces
  431. // however the loop will total 1 iteration each
  432. // second elapsed real time e.g. if not called
  433. // for 3 seconds, it will loop 3 times
  434. // there is also a GHLIMsec-1 limit on that
  435. gh->zerosec = now->tv_sec;
  436. // clear out the old values
  437. do
  438. {
  439. gh->offset = GHOFF(gh->offset+1);
  440. gh->diffsum -= gh->diff[GHOFF(gh->offset)];
  441. gh->diff[GHOFF(gh->offset)] = 0;
  442. gh->noncesum -= gh->noncenum[GHOFF(gh->offset)];
  443. gh->noncenum[GHOFF(gh->offset)] = 0;
  444. gh->firstt[GHOFF(gh->offset)].tv_sec = 0;
  445. gh->firstt[GHOFF(gh->offset)].tv_usec = 0;
  446. gh->firstd[GHOFF(gh->offset)] = 0;
  447. gh->lastt[GHOFF(gh->offset)].tv_sec = 0;
  448. gh->lastt[GHOFF(gh->offset)].tv_usec = 0;
  449. if (gh->last < (GHNUM-1))
  450. gh->last++;
  451. }
  452. while (--delta > 0);
  453. }
  454. }
  455. }
  456. // if there's been no nonces up to now, history must already be all zero
  457. // so just remove history
  458. if (gh->noncesum == 0 && gh->last > 0)
  459. gh->last = 0;
  460. // this also handles the issue of a nonce-less wipe with a high
  461. // now->tv_usec and if the first nonce comes in during the next second.
  462. // without setting 'last=0' the previous empty full second(s) will
  463. // always be included in the elapsed time used to calc the hash rate
  464. if (!locked)
  465. mutex_unlock(&info->ghlock);
  466. }
  467. // update info->gh with a new nonce as at 'now' (diff=info->difficulty)
  468. // info must be locked, wipe creates a new data set with the single nonce
  469. static void add_gekko_nonce(struct COMPAC_INFO *info, struct ASIC_INFO *asic, struct timeval *now)
  470. {
  471. struct GEKKOHASH *gh = &(info->gh);
  472. mutex_lock(&info->ghlock);
  473. gh_offset(info, now, false, true);
  474. if (gh->diff[gh->offset] == 0)
  475. {
  476. gh->firstt[gh->offset].tv_sec = now->tv_sec;
  477. gh->firstt[gh->offset].tv_usec = now->tv_usec;
  478. gh->firstd[gh->offset] = info->difficulty;
  479. }
  480. gh->lastt[gh->offset].tv_sec = now->tv_sec;
  481. gh->lastt[gh->offset].tv_usec = now->tv_usec;
  482. gh->diff[gh->offset] += info->difficulty;
  483. gh->diffsum += info->difficulty;
  484. (gh->noncenum[gh->offset])++;
  485. (gh->noncesum)++;
  486. if (asic != NULL)
  487. {
  488. struct GEKKOCHIP *gc = &(asic->gc);
  489. gc_offset(info, asic, now, false, true);
  490. (gc->noncenum[gc->offset])++;
  491. (gc->noncesum)++;
  492. }
  493. mutex_unlock(&info->ghlock);
  494. }
  495. // calculate MH/s hashrate, info must be locked
  496. // value is 0.0 if there's no useful data
  497. // caller check info->gh.last for history size used and info->gh.noncesum-1
  498. // for the amount of data used (i.e. accuracy of the hash rate)
  499. static double gekko_gh_hashrate(struct COMPAC_INFO *info, struct timeval *now, bool locked)
  500. {
  501. struct GEKKOHASH *gh = &(info->gh);
  502. struct timeval age, end;
  503. int zero, last;
  504. uint64_t delta;
  505. double ghr, old;
  506. ghr = 0.0;
  507. if (!locked)
  508. mutex_lock(&info->ghlock);
  509. // can't be calculated with only one nonce
  510. if (gh->diffsum > 0 && gh->noncesum > 1)
  511. {
  512. gh_offset(info, now, false, true);
  513. if (gh->diffsum > 0 && gh->noncesum > 1)
  514. {
  515. // offset of 'now'
  516. zero = gh->offset;
  517. // offset of oldest nonce
  518. last = GHOFF(zero - gh->last);
  519. if (gh->diff[last] != 0)
  520. {
  521. // from the oldest nonce, excluding it's diff
  522. delta = gh->firstd[last];
  523. age.tv_sec = gh->firstt[last].tv_sec;
  524. age.tv_usec = gh->firstt[last].tv_usec;
  525. }
  526. else
  527. {
  528. // if last is empty, use the start time of last
  529. delta = 0;
  530. age.tv_sec = gh->zerosec - (GHNUM - 1);
  531. age.tv_usec = 0;
  532. }
  533. // up to the time of the newest nonce as long as it
  534. // was curr or prev second, otherwise use now
  535. if (gh->diff[zero] != 0)
  536. {
  537. // time of the newest nonce found this second
  538. end.tv_sec = gh->lastt[zero].tv_sec;
  539. end.tv_usec = gh->lastt[zero].tv_usec;
  540. }
  541. else
  542. {
  543. // unexpected ... no recent nonces ...
  544. if (gh->diff[GHOFF(zero-1)] == 0)
  545. {
  546. end.tv_sec = now->tv_sec;
  547. end.tv_usec = now->tv_usec;
  548. }
  549. else
  550. {
  551. // time of the newest nonce found this second-1
  552. end.tv_sec = gh->lastt[GHOFF(zero-1)].tv_sec;
  553. end.tv_usec = gh->lastt[GHOFF(zero-1)].tv_usec;
  554. }
  555. }
  556. old = tdiff(&end, &age);
  557. if (old > 0.0)
  558. {
  559. ghr = (double)(gh->diffsum - delta)
  560. * (pow(2.0, 32.0) / old) / 1.0e6;
  561. }
  562. }
  563. }
  564. if (!locked)
  565. mutex_unlock(&info->ghlock);
  566. return ghr;
  567. }
  568. static void job_offset(struct COMPAC_INFO *info, struct timeval *now, bool wipe, bool locked)
  569. {
  570. struct GEKKOJOB *job = &(info->job);
  571. time_t delta;
  572. int jobnow;
  573. jobnow = JOBTIME(now->tv_sec);
  574. if (!locked)
  575. mutex_lock(&info->joblock);
  576. // first time in, wipe is ignored (it's already all zero)
  577. if (job->zeromin == 0)
  578. job->zeromin = jobnow;
  579. else
  580. {
  581. // wipe or delta != 0
  582. if (wipe || job->zeromin != jobnow)
  583. {
  584. // clear some/all delta data
  585. delta = jobnow - job->zeromin;
  586. // if time goes back, also reset everything
  587. // N.B. a forward time jump between 2 and JOBLIMn
  588. // seconds will reduce the job rate value
  589. // but JOBLIMn or more will reset the whole buffer
  590. if (wipe || delta < 0 || delta >= JOBLIMn)
  591. {
  592. // clear out everything
  593. job->zeromin = jobnow;
  594. job->lastjob.tv_sec = 0;
  595. job->lastjob.tv_usec = 0;
  596. job->offset = 0;
  597. memset(job->firstj, 0, sizeof(job->firstj));
  598. memset(job->lastj, 0, sizeof(job->lastj));
  599. memset(job->jobnum, 0, sizeof(job->jobnum));
  600. memset(job->avgms, 0, sizeof(job->avgms));
  601. memset(job->minms, 0, sizeof(job->minms));
  602. memset(job->maxms, 0, sizeof(job->maxms));
  603. job->jobsnum = 0;
  604. job->last = 0;
  605. }
  606. else
  607. {
  608. // delta is > 0, but usually always 1 unless,
  609. // due to asic or pool failure, a minute had no jobs
  610. // however the loop will total 1 iteration each
  611. // minute elapsed real time e.g. if not called
  612. // for 2 minutes, it will loop 2 times
  613. // there is also a JOBLIMn-1 limit on that
  614. job->zeromin = jobnow;
  615. // clear out the old values
  616. do
  617. {
  618. job->offset = JOBOFF(job->offset+1);
  619. job->firstj[JOBOFF(job->offset)].tv_sec = 0;
  620. job->firstj[JOBOFF(job->offset)].tv_usec = 0;
  621. job->lastj[JOBOFF(job->offset)].tv_sec = 0;
  622. job->lastj[JOBOFF(job->offset)].tv_usec = 0;
  623. job->jobsnum -= job->jobnum[JOBOFF(job->offset)];
  624. job->jobnum[JOBOFF(job->offset)] = 0;
  625. job->avgms[JOBOFF(job->offset)] = 0;
  626. job->minms[JOBOFF(job->offset)] = 0;
  627. job->maxms[JOBOFF(job->offset)] = 0;
  628. if (job->last < (JOBMIN-1))
  629. job->last++;
  630. }
  631. while (--delta > 0);
  632. }
  633. }
  634. }
  635. // if there's been no jobs up to now, history must already be all zero
  636. // so just remove history
  637. if (job->jobsnum == 0 && job->last > 0)
  638. job->last = 0;
  639. // this also handles the issue of a job-less wipe with a high
  640. // now->tv_usec and if the first job comes in during the next minute.
  641. // without setting 'last=0' the previous empty full minute will
  642. // always be included in the elapsed time used to calc the job rate
  643. if (!locked)
  644. mutex_unlock(&info->joblock);
  645. }
  646. // update info->job with a job as at 'now'
  647. // info must be locked, wipe creates a new empty data set
  648. static void add_gekko_job(struct COMPAC_INFO *info, struct timeval *now, bool wipe)
  649. {
  650. struct GEKKOJOB *job = &(info->job);
  651. bool firstjob;
  652. double avg;
  653. double ms;
  654. mutex_lock(&info->joblock);
  655. job_offset(info, now, wipe, true);
  656. if (!wipe)
  657. {
  658. if (job->jobnum[job->offset] == 0)
  659. {
  660. job->firstj[job->offset].tv_sec = now->tv_sec;
  661. job->firstj[job->offset].tv_usec = now->tv_usec;
  662. firstjob = true;
  663. }
  664. else
  665. firstjob = false;
  666. job->lastj[job->offset].tv_sec = now->tv_sec;
  667. job->lastj[job->offset].tv_usec = now->tv_usec;
  668. // first job time in each offset gets ignored
  669. // this is only necessary for the very first job,
  670. // but easier to do it for every offset group
  671. if (firstjob)
  672. {
  673. // already true
  674. // job->avgms[job->offset] = 0.0;
  675. // job->minms[job->offset] = 0.0;
  676. // job->maxms[job->offset] = 0.0;
  677. }
  678. else
  679. {
  680. avg = job->avgms[job->offset] * (double)(job->jobnum[job->offset] - 1);
  681. ms = (double)(now->tv_sec - job->lastjob.tv_sec) * 1000.0;
  682. ms += (double)(now->tv_usec - job->lastjob.tv_usec) / 1000.0;
  683. // jobnum[] must be > 0
  684. job->avgms[job->offset] = (avg + ms) / (double)(job->jobnum[job->offset]);
  685. if (job->minms[job->offset] == 0.0)
  686. {
  687. job->minms[job->offset] = ms;
  688. job->maxms[job->offset] = ms;
  689. }
  690. else
  691. {
  692. if (ms < job->minms[job->offset])
  693. job->minms[job->offset] = ms;
  694. if (job->maxms[job->offset] < ms)
  695. job->maxms[job->offset] = ms;
  696. }
  697. }
  698. (job->jobnum[job->offset])++;
  699. (job->jobsnum)++;
  700. job->lastjob.tv_sec = now->tv_sec;
  701. job->lastjob.tv_usec = now->tv_usec;
  702. }
  703. mutex_unlock(&info->joblock);
  704. }
  705. // ignore nonces for this many work items after the ticket change
  706. #define TICKET_DELAY 8
  707. // allow this many nonces below the ticket value in case of work swap delays
  708. // N.B. if the chip mask is half the wanted value,
  709. // roughly 85% of shares will be low since CDF 190% = 0.850
  710. // with the lowest nonce_count of 150 below for diff 2,
  711. // TICKET_BLOW_LIM 4 will always be exceeded if incorrectly set to diff 1
  712. #define TICKET_BELOW_LIM 4
  713. struct TICKET_INFO {
  714. uint32_t diff; // work diff value
  715. uint32_t ticket_mask; // ticket mask to ensure work diff
  716. int nonce_count; // CDF[Erl] nonces must have 1 below low_limit
  717. double low_limit; // must be a diff below this or ticket is too hi
  718. double hi_limit; // a diff below this means ticket is too low
  719. // set to .1 below diff to avoid any rounding
  720. uint32_t cclimit; // chips x cores limit i.e. required to go above 16
  721. };
  722. // ticket restart checks allowed before forced to diff=1
  723. #define MAX_TICKET_CHECK 3
  724. // ticket values, diff descending. List values rather than calc them
  725. // end comments are how long at given task/sec (15 ~= 60 1diff nonce/sec = ~260GH/s)
  726. // testing should take and chance of failure
  727. // though it will retry MAX_TICKET_CHECK times so shouldn't give up in the
  728. // exceedingly rare occasion where it fails once due to bad luck
  729. // limit to max diff of 16 unless the chips x cores is a bit better than a GSF/GSFM
  730. // to ensure enough nonces are coming back to identify status changes/issues
  731. // the luck calculation is the chance all nonce diff values will be above low_limit
  732. // after nonce_count nonces i.e. after nonce_count nonces there should be a nonce
  733. // below low_limit, or the ticket mask is actually higher than it was set to
  734. // the gsl function is cdf_gamma_Q(nonces, nonces, low_limit/diff)
  735. static struct TICKET_INFO ticket_1397[] =
  736. {
  737. { 64, 0xfc, 20000, 65.9, 63.9, 2600 }, // 90 59.3m Erlang=1.6x10-5 <- 64+ nonces
  738. { 32, 0xf8, 10000, 33.3, 31.9, 1300 }, // 45 29.6m Erlang=3.0x10-5 <- 32+ nonces
  739. { 16, 0xf0, 5000, 16.9, 15.9, 0 }, // 15 22.2m Erlang=4.6x10-5 <- 16+ nonces
  740. { 8, 0xe0, 1250, 8.9, 7.9, 0 }, // 15 166s Erlang=6.0x10-5
  741. { 4, 0xc0, 450, 4.9, 3.9, 0 }, // 15 30s Erlang=3.9x10-6
  742. { 2, 0x80, 150, 2.9, 1.9, 0 }, // 15 5s Erlang=5.4x10-7
  743. { 1, 0x00, 50, 1.9, 0.0, 0 }, // 15 0.8s Erlang=1.5x10-7 <- all nonces
  744. { 0 }
  745. };
  746. // force=true to allow setting it if it may not have taken before
  747. // force also delays longer after sending the ticket mask
  748. // diff=0.0 mean set the highest valid
  749. static void set_ticket(struct cgpu_info *compac, float diff, bool force, bool locked)
  750. {
  751. struct COMPAC_INFO *info = compac->device_data;
  752. struct timeval now;
  753. bool got = false;
  754. uint32_t udiff, new_diff = 0, new_mask = 0, cc;
  755. int i;
  756. if (diff == 0.0)
  757. {
  758. // above max will get the highest valid for cc
  759. diff = 128;
  760. }
  761. // if (!force && info->last_work_diff == diff)
  762. // return;
  763. // closest uint diff equal or below
  764. udiff = (uint32_t)floor(diff);
  765. cc = info->chips * info->cores;
  766. for (i = 0; ticket_1397[i].diff > 0; i++)
  767. {
  768. if (udiff >= ticket_1397[i].diff && cc > ticket_1397[i].cclimit)
  769. {
  770. // if ticket is already the same
  771. if (!force && info->difficulty == ticket_1397[i].diff)
  772. return;
  773. if (!locked)
  774. mutex_lock(&info->lock);
  775. new_diff = info->difficulty = ticket_1397[i].diff;
  776. new_mask = info->ticket_mask = ticket_1397[i].ticket_mask;
  777. info->last_work_diff = diff;
  778. cgtime(&info->last_ticket_attempt);
  779. info->ticket_number = i;
  780. info->ticket_work = 0;
  781. info->ticket_nonces = 0;
  782. info->below_nonces = 0;
  783. info->ticket_ok = false;
  784. info->ticket_got_low = false;
  785. if (!locked)
  786. mutex_unlock(&info->lock);
  787. got = true;
  788. break;
  789. }
  790. }
  791. // code failure
  792. if (!got)
  793. return;
  794. // set them all the same 0x51 .... 0x00
  795. unsigned char ticket[] = {0x51, 0x09, 0x00, BM1397TICKET, 0x00, 0x00, 0x00, 0xC0, 0x00};
  796. ticket[7] = info->ticket_mask;
  797. compac_send2(compac, ticket, sizeof(ticket), 8 * sizeof(ticket) - 8, "ticket");
  798. if (!force)
  799. gekko_usleep(info, MS2US(10));
  800. else
  801. gekko_usleep(info, MS2US(20));
  802. applog(LOG_ERR, "%d: %s %d - set ticket to 0x%02x/%u work %u/%.1f",
  803. compac->cgminer_id, compac->drv->name, compac->device_id,
  804. new_mask, new_diff, udiff, diff);
  805. // wipe info->gh/asic->gc
  806. cgtime(&now);
  807. gh_offset(info, &now, true, false);
  808. job_offset(info, &now, true, false);
  809. // reset P:
  810. info->frequency_computed = 0;
  811. }
  812. // expected nonces for GEKKOCHIP - MUST already be locked AND gc_offset()
  813. // full 50 mins + current offset in 10 mins - N.B. uses CLOCK_MONOTONIC
  814. // it will grow from 0% to ~100% between 50 & 60 mins if the chip
  815. // is performing at 100% - random variance of course also applies
  816. static double noncepercent(struct COMPAC_INFO *info, int chip, struct timeval *now)
  817. {
  818. double sec, hashpersec, noncepersec, nonceexpect;
  819. if (info->asic_type != BM1397)
  820. return 0.0;
  821. sec = CHTIME * (CHNUM-1) + (now->tv_sec % CHTIME) + ((double)now->tv_usec / 1000000.0);
  822. hashpersec = info->asics[chip].frequency * info->cores * info->hr_scale * 1000000.0;
  823. noncepersec = (hashpersec / (double)0xffffffffull)
  824. / (double)(ticket_1397[info->ticket_number].diff);
  825. nonceexpect = noncepersec * sec;
  826. return 100.0 * (double)(info->asics[chip].gc.noncesum) / nonceexpect;
  827. }
  828. // GSF/GSFM any chip count
  829. static void calc_gsf_freq(struct cgpu_info *compac, float frequency, int chip)
  830. {
  831. struct COMPAC_INFO *info = compac->device_data;
  832. char chipn[8];
  833. bool doall;
  834. if (info->asic_type != BM1397)
  835. return;
  836. if (chip == -1)
  837. doall = true;
  838. else
  839. {
  840. if (chip < 0 || chip >= (int)(info->chips))
  841. {
  842. applog(LOG_ERR, "%d: %s %d - invalid set chip [%d] -> freq %.2fMHz",
  843. compac->cgminer_id, compac->drv->name, compac->device_id, chip, frequency);
  844. return;
  845. }
  846. doall = false;
  847. }
  848. // if attempting the same frequency that previously failed ...
  849. if (frequency != 0 && frequency == info->freq_fail)
  850. return;
  851. unsigned char prefreqall[] = {0x51, 0x09, 0x00, 0x70, 0x0F, 0x0F, 0x0F, 0x00, 0x00};
  852. unsigned char prefreqch[] = {0x41, 0x09, 0x00, 0x70, 0x0F, 0x0F, 0x0F, 0x00, 0x00};
  853. // default 200Mhz if it fails
  854. unsigned char freqbufall[] = {0x51, 0x09, 0x00, BM1397FREQ, 0x40, 0xF0, 0x02, 0x35, 0x00};
  855. unsigned char freqbufch[] = {0x41, 0x09, 0x00, BM1397FREQ, 0x40, 0xF0, 0x02, 0x35, 0x00};
  856. float deffreq = 200.0;
  857. float fa, fb, fc1, fc2, newf;
  858. float f1, basef, famax = 0xf0, famin = 0x10;
  859. uint16_t c;
  860. int i;
  861. // allow a frequency 'power down'
  862. if (frequency == 0)
  863. {
  864. doall = true;
  865. basef = fa = 0;
  866. fb = fc1 = fc2 = 1;
  867. }
  868. else
  869. {
  870. f1 = limit_freq(info, frequency, false);
  871. fb = 2; fc1 = 1; fc2 = 5; // initial multiplier of 10
  872. if (f1 >= 500)
  873. {
  874. // halv down to '250-400'
  875. fb = 1;
  876. }
  877. else if (f1 <= 150)
  878. {
  879. // tiple up to '300-450'
  880. fc1 = 3;
  881. }
  882. else if (f1 <= 250)
  883. {
  884. // double up to '300-500'
  885. fc1 = 2;
  886. }
  887. // else f1 is 250-500
  888. // f1 * fb * fc1 * fc2 is between 2500 and 5000
  889. // - so round up to the next 25 (freq_mult)
  890. basef = info->freq_mult * ceil(f1 * fb * fc1 * fc2 / info->freq_mult);
  891. // fa should be between 100 (0x64) and 200 (0xC8)
  892. fa = basef / info->freq_mult;
  893. }
  894. // code failure ... basef isn't 400 to 6000
  895. if (frequency != 0 && (fa < famin || fa > famax))
  896. {
  897. info->freq_fail = frequency;
  898. newf = deffreq;
  899. }
  900. else
  901. {
  902. if (doall)
  903. {
  904. freqbufall[5] = (int)fa;
  905. freqbufall[6] = (int)fb;
  906. // fc1, fc2 'should' already be 1..15
  907. freqbufall[7] = (((int)fc1 & 0xf) << 4) + ((int)fc2 & 0xf);
  908. }
  909. else
  910. {
  911. freqbufch[5] = (int)fa;
  912. freqbufch[6] = (int)fb;
  913. // fc1, fc2 'should' already be 1..15
  914. freqbufch[7] = (((int)fc1 & 0xf) << 4) + ((int)fc2 & 0xf);
  915. }
  916. newf = basef / ((float)fb * (float)fc1 * (float)fc2);
  917. }
  918. if (doall)
  919. {
  920. // i.e. -1 means no reply since last set
  921. for (c = 0; c < info->chips; c++)
  922. info->asics[c].frequency_reply = -1;
  923. for (i = 0; i < 2; i++)
  924. {
  925. gekko_usleep(info, MS2US(10));
  926. compac_send2(compac, prefreqall, sizeof(prefreqall), 8 * sizeof(prefreqall) - 8, "prefreq");
  927. }
  928. for (i = 0; i < 2; i++)
  929. {
  930. gekko_usleep(info, MS2US(10));
  931. compac_send2(compac, freqbufall, sizeof(freqbufall), 8 * sizeof(freqbufall) - 8, "freq");
  932. }
  933. // the freq wanted, which 'should' be the same
  934. for (c = 0; c < info->chips; c++)
  935. info->asics[c].frequency = frequency;
  936. }
  937. else
  938. {
  939. // just setting 1 chip
  940. prefreqch[2] = freqbufch[2] = CHIPPY1397(info, chip);
  941. // i.e. -1 means no reply since last set
  942. info->asics[chip].frequency_reply = -1;
  943. for (i = 0; i < 2; i++)
  944. {
  945. gekko_usleep(info, MS2US(10));
  946. compac_send2(compac, prefreqch, sizeof(prefreqch), 8 * sizeof(prefreqch) - 8, "prefreq");
  947. }
  948. for (i = 0; i < 2; i++)
  949. {
  950. gekko_usleep(info, MS2US(10));
  951. compac_send2(compac, freqbufch, sizeof(freqbufch), 8 * sizeof(freqbufch) - 8, "freq");
  952. }
  953. // the freq wanted, which 'should' be the same
  954. info->asics[chip].frequency = frequency;
  955. }
  956. if (doall)
  957. info->frequency = frequency;
  958. gekko_usleep(info, MS2US(10));
  959. if (doall)
  960. snprintf(chipn, sizeof(chipn), "all");
  961. else
  962. snprintf(chipn, sizeof(chipn), "%d", chip);
  963. // applog(LOG_INFO, "%d: %s %d - setting frequency to %.2fMHz (%.2f)" " (%.0f/%.0f/%.0f/%.0f)",
  964. applog(LOG_ERR, "%d: %s %d - setting [%s] frequency to %.2fMHz (%.2f)" " (%.0f/%.0f/%.0f/%.0f)",
  965. compac->cgminer_id, compac->drv->name, compac->device_id, chipn, frequency, newf, fa, fb, fc1, fc2);
  966. ping_freq(compac, 0);
  967. }
  968. static void compac_send_chain_inactive(struct cgpu_info *compac)
  969. {
  970. struct COMPAC_INFO *info = compac->device_data;
  971. unsigned int i, j;
  972. applog(LOG_INFO,"%d: %s %d - sending chain inactive for %d chip(s)",
  973. compac->cgminer_id, compac->drv->name, compac->device_id, info->chips);
  974. if (info->asic_type == BM1397)
  975. {
  976. // chain inactive
  977. unsigned char chainin[5] = {0x53, 0x05, 0x00, 0x00, 0x00};
  978. for (i = 0; i < 3; i++)
  979. {
  980. compac_send2(compac, chainin, sizeof(chainin), 8 * sizeof(chainin) - 8, "chin");
  981. gekko_usleep(info, MS2US(100));
  982. }
  983. unsigned char chippy[] = {0x40, 0x05, 0x00, 0x00, 0x00};
  984. for (i = 0; i < info->chips; i++)
  985. {
  986. chippy[2] = CHIPPY1397(info, i);
  987. compac_send2(compac, chippy, sizeof(chippy), 8 * sizeof(chippy) - 8, "chippy");
  988. gekko_usleep(info, MS2US(10));
  989. }
  990. unsigned char init1[] = {0x51, 0x09, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00};
  991. unsigned char init2[] = {0x51, 0x09, 0x00, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00};
  992. unsigned char init3[] = {0x51, 0x09, 0x00, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00};
  993. unsigned char init4[] = {0x51, 0x09, 0x00, 0x3C, 0x80, 0x00, 0x80, 0x74, 0x00};
  994. compac_send2(compac, init1, sizeof(init1), 8 * sizeof(init1) - 8, "init1");
  995. gekko_usleep(info, MS2US(10));
  996. compac_send2(compac, init2, sizeof(init2), 8 * sizeof(init2) - 8, "init2");
  997. gekko_usleep(info, MS2US(100));
  998. compac_send2(compac, init3, sizeof(init3), 8 * sizeof(init3) - 8, "init3");
  999. gekko_usleep(info, MS2US(50));
  1000. compac_send2(compac, init4, sizeof(init4), 8 * sizeof(init4) - 8, "init4");
  1001. gekko_usleep(info, MS2US(100));
  1002. // set ticket based on chips, pool will be above this anyway
  1003. set_ticket(compac, 0.0, true, false);
  1004. unsigned char init5[] = {0x51, 0x09, 0x00, 0x68, 0xC0, 0x70, 0x01, 0x11, 0x00};
  1005. unsigned char init6[] = {0x51, 0x09, 0x00, 0x28, 0x06, 0x00, 0x00, 0x0F, 0x00};
  1006. for (j = 0; j < 2; j++)
  1007. {
  1008. compac_send2(compac, init5, sizeof(init5), 8 * sizeof(init5) - 8, "init5");
  1009. gekko_usleep(info, MS2US(50));
  1010. }
  1011. compac_send2(compac, init6, sizeof(init6), 8 * sizeof(init6) - 8, "init6");
  1012. gekko_usleep(info, MS2US(100));
  1013. unsigned char baudrate[] = { 0x51, 0x09, 0x00, 0x18, 0x00, 0x00, 0x61, 0x31, 0x00 }; // lo 1.51M
  1014. info->bauddiv = 1; // 1.5M
  1015. #ifdef WIN32___fixme_zzz
  1016. if (info->midstates == 4)
  1017. {
  1018. // 4 mid = slow it down on windows 116K
  1019. baudrate[5] = 0x00;
  1020. baudrate[6] = 0x7A;
  1021. // 3.125M/27
  1022. info->bauddiv = 26;
  1023. }
  1024. #endif
  1025. applog(LOG_ERR, "%d: %s %d - setting bauddiv : %02x %02x (ftdi/%d)",
  1026. compac->cgminer_id, compac->drv->name, compac->device_id, baudrate[5], baudrate[6], info->bauddiv + 1);
  1027. compac_send2(compac, baudrate, sizeof(baudrate), 8 * sizeof(baudrate) - 8, "baud");
  1028. gekko_usleep(info, MS2US(10));
  1029. usb_transfer(compac, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, info->bauddiv + 1,
  1030. (FTDI_INDEX_BAUD_BTS & 0xff00) | info->interface, C_SETBAUD);
  1031. gekko_usleep(info, MS2US(10));
  1032. calc_gsf_freq(compac, info->frequency, -1);
  1033. gekko_usleep(info, MS2US(20));
  1034. } else if (info->asic_type == BM1387) {
  1035. unsigned char buffer[5] = {0x55, 0x05, 0x00, 0x00, 0x00};
  1036. compac_send(compac, buffer, sizeof(buffer), 8 * sizeof(buffer) - 8); // chain inactive
  1037. gekko_usleep(info, MS2US(5));
  1038. compac_send(compac, buffer, sizeof(buffer), 8 * sizeof(buffer) - 8); // chain inactive
  1039. gekko_usleep(info, MS2US(5));
  1040. compac_send(compac, buffer, sizeof(buffer), 8 * sizeof(buffer) - 8); // chain inactive
  1041. for (i = 0; i < info->chips; i++) {
  1042. buffer[0] = 0x41;
  1043. buffer[1] = 0x05;
  1044. buffer[2] = (0x100 / info->chips) * i;
  1045. gekko_usleep(info, MS2US(5));
  1046. compac_send(compac, buffer, sizeof(buffer), 8 * sizeof(buffer) - 8);
  1047. }
  1048. gekko_usleep(info, MS2US(10));
  1049. unsigned char baudrate[] = { 0x58, 0x09, 0x00, 0x1C, 0x00, 0x20, 0x07, 0x00, 0x19 };
  1050. if (opt_gekko_bauddiv) {
  1051. info->bauddiv = opt_gekko_bauddiv;
  1052. } else {
  1053. info->bauddiv = 0x01; // 1.5Mbps baud.
  1054. #ifdef WIN32
  1055. if (info->midstates == 4)
  1056. info->bauddiv = 0x0D; // 214Kbps baud.
  1057. #endif
  1058. }
  1059. applog(LOG_INFO, "%d: %s %d - setting bauddiv : %02x",
  1060. compac->cgminer_id, compac->drv->name, compac->device_id, info->bauddiv);
  1061. baudrate[6] = info->bauddiv;
  1062. compac_send(compac, baudrate, sizeof(baudrate), 8 * sizeof(baudrate) - 8);
  1063. gekko_usleep(info, MS2US(10));
  1064. usb_transfer(compac, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, (info->bauddiv + 1),
  1065. (FTDI_INDEX_BAUD_BTS & 0xff00) | info->interface, C_SETBAUD);
  1066. gekko_usleep(info, MS2US(10));
  1067. unsigned char gateblk[9] = {0x58, 0x09, 0x00, 0x1C, 0x40, 0x20, 0x99, 0x80, 0x01};
  1068. gateblk[6] = 0x80 | info->bauddiv;
  1069. compac_send(compac, gateblk, sizeof(gateblk), 8 * sizeof(gateblk) - 8); // chain inactive
  1070. } else if (info->asic_type == BM1384) {
  1071. unsigned char buffer[] = {0x85, 0x00, 0x00, 0x00};
  1072. compac_send(compac, buffer, sizeof(buffer), 8 * sizeof(buffer) - 5); // chain inactive
  1073. for (i = 0; i < info->chips; i++) {
  1074. buffer[0] = 0x01;
  1075. buffer[1] = (0x100 / info->chips) * i;
  1076. compac_send(compac, buffer, sizeof(buffer), 8 * sizeof(buffer) - 5);
  1077. }
  1078. buffer[0] = 0x86; // GATEBLK
  1079. buffer[1] = 0x00;
  1080. buffer[2] = 0x9a; // 0x80 | 0x1a;
  1081. //compac_send(compac, buffer, sizeof(buffer), 8 * sizeof(buffer) - 5);
  1082. }
  1083. if (info->mining_state == MINER_CHIP_COUNT_OK) {
  1084. applog(LOG_INFO, "%d: %s %d - open cores",
  1085. compac->cgminer_id, compac->drv->name, compac->device_id);
  1086. info->zero_check = 0;
  1087. info->task_hcn = 0;
  1088. info->mining_state = MINER_OPEN_CORE;
  1089. }
  1090. }
  1091. static void compac_update_rates(struct cgpu_info *compac)
  1092. {
  1093. struct COMPAC_INFO *info = compac->device_data;
  1094. struct ASIC_INFO *asic;
  1095. float average_frequency = 0, est;
  1096. unsigned int i;
  1097. cgtime(&(info->last_update_rates));
  1098. if (info->chips == 0 || info->frequency == 0)
  1099. return;
  1100. for (i = 0; i < info->chips; i++)
  1101. {
  1102. if (info->asics[i].frequency == 0)
  1103. return;
  1104. }
  1105. info->frequency_asic = 0;
  1106. for (i = 0; i < info->chips; i++) {
  1107. asic = &info->asics[i];
  1108. asic->hashrate = asic->frequency * info->cores * 1000000 * info->hr_scale;
  1109. asic->fullscan_ms = 1000.0 * info->hr_scale * 0xffffffffull / asic->hashrate;
  1110. asic->fullscan_us = 1000.0 * info->hr_scale * 1000.0 * 0xffffffffull / asic->hashrate;
  1111. average_frequency += asic->frequency;
  1112. info->frequency_asic = (asic->frequency > info->frequency_asic ) ? asic->frequency : info->frequency_asic;
  1113. }
  1114. average_frequency = average_frequency / info->chips;
  1115. if (average_frequency != info->frequency) {
  1116. applog(LOG_INFO,"%d: %s %d - frequency updated %.2fMHz -> %.2fMHz",
  1117. compac->cgminer_id, compac->drv->name, compac->device_id, info->frequency, average_frequency);
  1118. info->frequency = average_frequency;
  1119. info->wu_max = 0;
  1120. }
  1121. info->wu = (info->chips * info->frequency * info->cores / 71.6) * info->hr_scale;
  1122. info->hashrate = info->chips * info->frequency * info->cores * 1000000 * info->hr_scale;
  1123. info->fullscan_ms = 1000.0 * info->hr_scale * 0xffffffffull / info->hashrate;
  1124. info->fullscan_us = 1000.0 * info->hr_scale * 1000.0 * 0xffffffffull / info->hashrate;
  1125. if (info->asic_type != BM1397)
  1126. {
  1127. info->ticket_mask = bound(pow(2, ceil(log(info->hashrate / (2.0 * 0xffffffffull)) / log(2))) - 1, 0, 4000);
  1128. info->ticket_mask = (info->asic_type == BM1387) ? 0 : info->ticket_mask;
  1129. info->difficulty = info->ticket_mask + 1;
  1130. }
  1131. info->wait_factor = info->wait_factor0;
  1132. if (!opt_gekko_noboost && info->vmask && (info->asic_type == BM1387 || info->asic_type == BM1397))
  1133. info->wait_factor *= info->midstates;
  1134. est = info->wait_factor * (float)(info->fullscan_us);
  1135. info->max_task_wait = bound((uint64_t)est, 1, 3 * info->fullscan_us);
  1136. if (info->asic_type == BM1387)
  1137. {
  1138. if (opt_gekko_tune_up > 95)
  1139. info->tune_up = 100.0 * ((info->frequency - info->freq_base * (600 / info->frequency)) / info->frequency);
  1140. else
  1141. info->tune_up = opt_gekko_tune_up;
  1142. }
  1143. else if (info->asic_type == BM1397)
  1144. {
  1145. // 90% will always allow at least 2 freq steps
  1146. if (opt_gekko_tune_up > 90)
  1147. opt_gekko_tune_up = 90;
  1148. else
  1149. info->tune_up = opt_gekko_tune_up;
  1150. }
  1151. else
  1152. info->tune_up = 99;
  1153. // shouldn't happen, but next call should fix it
  1154. if (info->difficulty == 0)
  1155. info->nonce_expect = 0;
  1156. else
  1157. {
  1158. // expected ms per nonce for PT_NONONCE
  1159. info->nonce_expect = info->fullscan_ms * info->difficulty;
  1160. // BM1397 check is per miner, not per chip, fullscan_ms is sum of chips
  1161. if (info->asic_type != BM1397 && info->chips > 1)
  1162. info->nonce_expect *= (float)info->chips;
  1163. // CDF >2000% is avg once in 485165205.1 nonces
  1164. info->nonce_limit = info->nonce_expect * 20.0;
  1165. // N.B. this ignores info->ghrequire since
  1166. // that should be an independent test
  1167. }
  1168. applog(LOG_INFO, "%d: %s %d - Rates: ms %.2f tu %.2f td %.2f",
  1169. compac->cgminer_id, compac->drv->name, compac->device_id,
  1170. info->fullscan_ms, info->tune_up, info->tune_down);
  1171. }
  1172. static void compac_set_frequency_single(struct cgpu_info *compac, float frequency, int asic_id)
  1173. {
  1174. struct COMPAC_INFO *info = compac->device_data;
  1175. struct ASIC_INFO *asic = &info->asics[asic_id];
  1176. struct timeval now;
  1177. if (info->asic_type == BM1387) {
  1178. unsigned char buffer[] = {0x48, 0x09, 0x00, 0x0C, 0x00, 0x50, 0x02, 0x41, 0x00}; //250MHz -- osc of 25MHz
  1179. frequency = bound(frequency, 50, 1200);
  1180. frequency = FREQ_BASE(frequency);
  1181. if (frequency < 400) {
  1182. buffer[7] = 0x41;
  1183. buffer[5] = (frequency * 8) / info->freq_mult;
  1184. } else {
  1185. buffer[7] = 0x21;
  1186. buffer[5] = (frequency * 4) / info->freq_mult;
  1187. }
  1188. buffer[2] = (0x100 / info->chips) * asic_id;
  1189. //asic->frequency = frequency;
  1190. asic->frequency_set = frequency;
  1191. asic->frequency_attempt++;
  1192. applog(LOG_INFO, "%d: %s %d - setting chip[%d] frequency (%d) %.2fMHz -> %.2fMHz",
  1193. compac->cgminer_id, compac->drv->name, compac->device_id,
  1194. asic_id, asic->frequency_attempt, asic->frequency, frequency);
  1195. compac_send(compac, buffer, sizeof(buffer), 8 * sizeof(buffer) - 8);
  1196. //unsigned char gateblk[9] = {0x48, 0x09, 0x00, 0x1C, 0x40, 0x20, 0x99, 0x80, 0x01};
  1197. //gateblk[6] = 0x80 | info->bauddiv;
  1198. //gateblk[2] = (0x100 / info->chips) * id;
  1199. //compac_send(compac, gateblk, sizeof(gateblk), 8 * sizeof(gateblk) - 8); // chain inactive
  1200. // wipe info->gh/asic->gc
  1201. cgtime(&now);
  1202. gh_offset(info, &now, true, false);
  1203. // reset P:
  1204. info->frequency_computed = 0;
  1205. }
  1206. }
  1207. static void compac_set_frequency(struct cgpu_info *compac, float frequency)
  1208. {
  1209. struct COMPAC_INFO *info = compac->device_data;
  1210. uint32_t i, r, r1, r2, r3, p1, p2, pll;
  1211. struct timeval now;
  1212. if (info->asic_type == BM1397) {
  1213. calc_gsf_freq(compac, frequency, -1);
  1214. } else if (info->asic_type == BM1387) {
  1215. unsigned char buffer[] = {0x58, 0x09, 0x00, 0x0C, 0x00, 0x50, 0x02, 0x41, 0x00}; //250MHz -- osc of 25MHz
  1216. frequency = bound(frequency, 50, 1200);
  1217. frequency = FREQ_BASE(frequency);
  1218. if (frequency < 400) {
  1219. buffer[7] = 0x41;
  1220. buffer[5] = (frequency * 8) / info->freq_mult;
  1221. } else {
  1222. buffer[7] = 0x21;
  1223. buffer[5] = (frequency * 4) / info->freq_mult;
  1224. }
  1225. /*
  1226. } else {
  1227. buffer[7] = 0x11;
  1228. buffer[5] = (frequency * 2) / info->freq_mult;
  1229. }
  1230. */
  1231. applog(LOG_INFO, "%d: %s %d - setting frequency to %.2fMHz",
  1232. compac->cgminer_id, compac->drv->name, compac->device_id, frequency);
  1233. compac_send(compac, buffer, sizeof(buffer), 8 * sizeof(buffer) - 8);
  1234. info->frequency = frequency;
  1235. for (i = 0; i < info->chips; i++)
  1236. info->asics[i].frequency = frequency;
  1237. } else if (info->asic_type == BM1384) {
  1238. unsigned char buffer[] = {0x82, 0x0b, 0x83, 0x00};
  1239. frequency = bound(frequency, 6, 500);
  1240. frequency = FREQ_BASE(frequency);
  1241. info->frequency = frequency;
  1242. r = floor(log(info->frequency/info->freq_mult) / log(2));
  1243. r1 = 0x0785 - r;
  1244. r2 = 0x200 / pow(2, r);
  1245. r3 = info->freq_mult * pow(2, r);
  1246. p1 = r1 + r2 * (info->frequency - r3) / info->freq_base;
  1247. p2 = p1 * 2 + (0x7f + r);
  1248. pll = (((uint32_t)(info->frequency) % (uint32_t)(info->freq_mult)) == 0 ? p1 : p2);
  1249. if (info->frequency < 100) {
  1250. pll = 0x0783 - 0x80 * (100 - info->frequency) / info->freq_base;
  1251. }
  1252. buffer[1] = (pll >> 8) & 0xff;
  1253. buffer[2] = (pll) & 0xff;
  1254. applog(LOG_INFO, "%d: %s %d - setting frequency to %.2fMHz",
  1255. compac->cgminer_id, compac->drv->name, compac->device_id, frequency);
  1256. compac_send(compac, buffer, sizeof(buffer), 8 * sizeof(buffer) - 5);
  1257. buffer[0] = 0x84;
  1258. buffer[1] = 0x00;
  1259. buffer[2] = 0x00;
  1260. // compac_send(compac, buffer, sizeof(buffer), 8 * sizeof(buffer) - 5);
  1261. buffer[2] = 0x04;
  1262. compac_send(compac, buffer, sizeof(buffer), 8 * sizeof(buffer) - 5);
  1263. for (i = 0; i < info->chips; i++)
  1264. info->asics[i].frequency = frequency;
  1265. }
  1266. compac_update_rates(compac);
  1267. // wipe info->gh/asic->gc
  1268. cgtime(&now);
  1269. gh_offset(info, &now, true, false);
  1270. // reset P:
  1271. info->frequency_computed = 0;
  1272. }
  1273. static void compac_update_work(struct cgpu_info *compac)
  1274. {
  1275. struct COMPAC_INFO *info = compac->device_data;
  1276. int i;
  1277. for (i = 0; i < JOB_MAX; i++) {
  1278. info->active_work[i] = false;
  1279. }
  1280. info->update_work = 1;
  1281. }
  1282. static void compac_flush_buffer(struct cgpu_info *compac)
  1283. {
  1284. int read_bytes = 1;
  1285. unsigned char resp[32];
  1286. while (read_bytes) {
  1287. usb_read_timeout(compac, (char *)resp, 32, &read_bytes, 1, C_REQUESTRESULTS);
  1288. }
  1289. }
  1290. static void compac_flush_work(struct cgpu_info *compac)
  1291. {
  1292. compac_flush_buffer(compac);
  1293. compac_update_work(compac);
  1294. }
  1295. static void compac_toggle_reset(struct cgpu_info *compac)
  1296. {
  1297. struct COMPAC_INFO *info = compac->device_data;
  1298. unsigned short usb_val;
  1299. applog(info->log_wide,"%d: %s %d - Toggling ASIC nRST to reset",
  1300. compac->cgminer_id, compac->drv->name, compac->device_id);
  1301. usb_transfer(compac, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_RESET, info->interface, C_RESET);
  1302. usb_transfer(compac, FTDI_TYPE_OUT, FTDI_REQUEST_DATA, FTDI_VALUE_DATA_BTS, info->interface, C_SETDATA);
  1303. usb_transfer(compac, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD_BTS, (FTDI_INDEX_BAUD_BTS & 0xff00) | info->interface, C_SETBAUD);
  1304. usb_transfer(compac, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW, FTDI_VALUE_FLOW, info->interface, C_SETFLOW);
  1305. usb_transfer(compac, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_PURGE_TX, info->interface, C_PURGETX);
  1306. usb_transfer(compac, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_PURGE_RX, info->interface, C_PURGERX);
  1307. usb_val = (FTDI_BITMODE_CBUS << 8) | 0xF2; // low byte: bitmask - 1111 0010 - CB1(HI)
  1308. usb_transfer(compac, FTDI_TYPE_OUT, FTDI_REQUEST_BITMODE, usb_val, info->interface, C_SETMODEM);
  1309. gekko_usleep(info, MS2US(30));
  1310. usb_val = (FTDI_BITMODE_CBUS << 8) | 0xF0; // low byte: bitmask - 1111 0000 - CB1(LO)
  1311. usb_transfer(compac, FTDI_TYPE_OUT, FTDI_REQUEST_BITMODE, usb_val, info->interface, C_SETMODEM);
  1312. if (info->asic_type == BM1397)
  1313. gekko_usleep(info, MS2US(1000));
  1314. else
  1315. gekko_usleep(info, MS2US(30));
  1316. usb_val = (FTDI_BITMODE_CBUS << 8) | 0xF2; // low byte: bitmask - 1111 0010 - CB1(HI)
  1317. usb_transfer(compac, FTDI_TYPE_OUT, FTDI_REQUEST_BITMODE, usb_val, info->interface, C_SETMODEM);
  1318. gekko_usleep(info, MS2US(200));
  1319. cgtime(&info->last_reset);
  1320. }
  1321. static void compac_gsf_nonce(struct cgpu_info *compac, K_ITEM *item)
  1322. {
  1323. struct COMPAC_INFO *info = compac->device_data;
  1324. unsigned char *rx = DATA_NONCE(item)->rx;
  1325. int hwe = compac->hw_errors;
  1326. struct work *work = NULL;
  1327. bool active_work = false;
  1328. uint32_t job_id = 0;
  1329. uint32_t nonce = 0;
  1330. int domid, midnum = 0;
  1331. double diff = 0.0;
  1332. bool boost, ok;
  1333. int asic_id, i;
  1334. if (info->asic_type != BM1397)
  1335. return;
  1336. job_id = rx[7] & 0xff;
  1337. nonce = (rx[5] << 0) | (rx[4] << 8) | (rx[3] << 16) | (rx[2] << 24);
  1338. // N.B. busy work (0xff) never returns a nonce
  1339. mutex_lock(&info->lock);
  1340. info->nonces++;
  1341. info->nonceless = 0;
  1342. info->noncebyte[rx[3]]++;
  1343. mutex_unlock(&info->lock);
  1344. if (info->nb2c_setup)
  1345. asic_id = info->nb2chip[rx[3]];
  1346. else
  1347. asic_id = floor((double)(rx[4]) / ((double)0x100 / (double)(info->chips)));
  1348. if (asic_id >= (int)(info->chips))
  1349. {
  1350. applog(LOG_ERR, "%d: %s %d - nonce %08x @ %02x rx[4] %02x invalid asic_id (0..%d)",
  1351. compac->cgminer_id, compac->drv->name, compac->device_id, nonce, job_id,
  1352. rx[4], (int)(info->chips)-1);
  1353. asic_id = (info->chips - 1);
  1354. }
  1355. #if 0
  1356. else
  1357. {
  1358. applog(LOG_ERR, "%d: %s %d - gotnonce %08x @ %02x rx[2] %02x id %d(%u)",
  1359. compac->cgminer_id, compac->drv->name, compac->device_id, nonce, job_id,
  1360. rx[2], asic_id, info->chips);
  1361. applog(LOG_ERR, " N:[%02x %02x %02x %02x %02x %02x %02x]",
  1362. rx[0], rx[1], rx[2], rx[3], rx[4], rx[5], rx[6]);
  1363. }
  1364. #endif
  1365. struct ASIC_INFO *asic = &info->asics[asic_id];
  1366. if (nonce == asic->prev_nonce)
  1367. {
  1368. applog(LOG_INFO, "%d: %s %d - Duplicate Nonce : %08x @ %02x [%02x %02x %02x %02x %02x %02x %02x]",
  1369. compac->cgminer_id, compac->drv->name, compac->device_id, nonce, job_id,
  1370. rx[0], rx[1], rx[2], rx[3], rx[4], rx[5], rx[6]);
  1371. mutex_lock(&info->lock);
  1372. info->dups++;
  1373. info->dupsall++;
  1374. info->dupsreset++;
  1375. asic->dups++;
  1376. asic->dupsall++;
  1377. cgtime(&info->last_dup_time);
  1378. if (info->dups == 1)
  1379. info->mining_state = MINER_MINING_DUPS;
  1380. mutex_unlock(&info->lock);
  1381. return;
  1382. }
  1383. mutex_lock(&info->lock);
  1384. info->prev_nonce = nonce;
  1385. asic->prev_nonce = nonce;
  1386. applog(LOG_INFO, "%d: %s %d - Device reported nonce: %08x @ %02x (%d)",
  1387. compac->cgminer_id, compac->drv->name, compac->device_id, nonce, job_id, info->tracker);
  1388. if (!opt_gekko_noboost && info->vmask)
  1389. {
  1390. domid = info->midstates;
  1391. boost = true;
  1392. }
  1393. else
  1394. {
  1395. domid = 1;
  1396. boost = false;
  1397. }
  1398. ok = false;
  1399. // test the exact jobid/midnum
  1400. uint32_t w_job_id = job_id & 0xfc;
  1401. if (w_job_id <= info->max_job_id)
  1402. {
  1403. work = info->work[w_job_id];
  1404. active_work = info->active_work[w_job_id];
  1405. if (work && active_work)
  1406. {
  1407. if (boost)
  1408. {
  1409. midnum = job_id & 0x03;
  1410. work->micro_job_id = pow(2, midnum);
  1411. memcpy(work->data, &(work->pool->vmask_001[work->micro_job_id]), 4);
  1412. }
  1413. if ((diff = test_nonce_value(work, nonce)) != 0.0)
  1414. {
  1415. ok = true;
  1416. if (midnum > 0)
  1417. info->boosted = true;
  1418. }
  1419. }
  1420. }
  1421. if (!ok)
  1422. {
  1423. // not found, try each cur_attempt
  1424. for (i = 0; !ok && i < (int)CUR_ATTEMPT; i++)
  1425. {
  1426. w_job_id = JOB_ID_ROLL(info->job_id, cur_attempt[i], info) & 0xfc;
  1427. work = info->work[w_job_id];
  1428. active_work = info->active_work[w_job_id];
  1429. if (work && active_work)
  1430. {
  1431. for (midnum = 0; !ok && midnum < domid; midnum++)
  1432. {
  1433. // failed original job_id already tested
  1434. if ((w_job_id | midnum) == job_id)
  1435. continue;
  1436. if (boost)
  1437. {
  1438. work->micro_job_id = pow(2, midnum);
  1439. memcpy(work->data, &(work->pool->vmask_001[work->micro_job_id]), 4);
  1440. }
  1441. if ((diff = test_nonce_value(work, nonce)) != 0)
  1442. {
  1443. if (midnum > 0)
  1444. info->boosted = true;
  1445. info->cur_off[i]++;
  1446. ok = true;
  1447. applog(LOG_INFO, "%d: %s %d - Nonce Recovered : %08x @ job[%02x]->fix[%02x] len %u prelen %u",
  1448. compac->cgminer_id, compac->drv->name, compac->device_id,
  1449. nonce, job_id, w_job_id, (uint32_t)(DATA_NONCE(item)->len),
  1450. (uint32_t)(DATA_NONCE(item)->prelen));
  1451. }
  1452. }
  1453. }
  1454. }
  1455. if (!ok)
  1456. {
  1457. applog(LOG_INFO, "%d: %s %d - Nonce Dumped : %08x @ job[%02x] cur[%02x] diff %u",
  1458. compac->cgminer_id, compac->drv->name, compac->device_id,
  1459. nonce, job_id, info->job_id, info->difficulty);
  1460. inc_hw_errors_n(info->thr, info->difficulty);
  1461. cgtime(&info->last_hwerror);
  1462. mutex_unlock(&info->lock);
  1463. return;
  1464. }
  1465. }
  1466. // verify the ticket mask is correct
  1467. if (!info->ticket_ok && diff > 0)
  1468. {
  1469. do // so we can break out
  1470. {
  1471. if (++(info->ticket_work) > TICKET_DELAY)
  1472. {
  1473. int i = info->ticket_number;
  1474. info->ticket_nonces++;
  1475. // nonce below ticket setting - redo ticket - chip must be too low
  1476. // check this for all nonces until ticket_ok
  1477. if (diff < ticket_1397[i].hi_limit)
  1478. {
  1479. if (++(info->below_nonces) < TICKET_BELOW_LIM)
  1480. break;
  1481. // redo ticket to return fewer nonces
  1482. if (info->ticket_failures > MAX_TICKET_CHECK)
  1483. {
  1484. // give up - just set it to max
  1485. applog(LOG_ERR, "%d: %s %d - ticket %u failed too many times setting to max",
  1486. compac->cgminer_id, compac->drv->name, compac->device_id, ticket_1397[i].diff);
  1487. //set_ticket(compac, 1.0, true, true);
  1488. set_ticket(compac, 0.0, true, true);
  1489. info->ticket_ok = true;
  1490. break;
  1491. }
  1492. applog(LOG_ERR, "%d: %s %d - ticket %u failure (%"PRId64") diff %.1f below lim %.1f - retry %d",
  1493. compac->cgminer_id, compac->drv->name, compac->device_id,
  1494. ticket_1397[i].diff, info->below_nonces, diff, ticket_1397[i].hi_limit, info->ticket_failures+1);
  1495. // try again ...
  1496. //set_ticket(compac, ticket_1397[i].diff, true, true);
  1497. set_ticket(compac, 0.0, true, true);
  1498. info->ticket_failures++;
  1499. break;
  1500. }
  1501. // after nonce_count, CDF[Erlang] chance of NOT being below
  1502. if (diff < ticket_1397[i].low_limit)
  1503. info->ticket_got_low = true;
  1504. if (info->ticket_work >= (ticket_1397[i].nonce_count + TICKET_DELAY))
  1505. {
  1506. // we should have got a 'low' by now
  1507. if (info->ticket_got_low)
  1508. {
  1509. info->ticket_ok = true;
  1510. info->ticket_failures = 0;
  1511. applog(LOG_ERR, "%d: %s %d - ticket value confirmed 0x%02x/%u after %"PRId64" nonces",
  1512. compac->cgminer_id, compac->drv->name, compac->device_id,
  1513. info->ticket_mask, info->difficulty, info->ticket_nonces);
  1514. break;
  1515. }
  1516. // chip ticket must be too high means lost shares
  1517. if (info->ticket_failures > MAX_TICKET_CHECK)
  1518. {
  1519. // give up - just set it to 1.0
  1520. applog(LOG_ERR, "%d: %s %d - ticket %u failed too many times setting to max",
  1521. compac->cgminer_id, compac->drv->name, compac->device_id, ticket_1397[i].diff);
  1522. //set_ticket(compac, 1.0, true, true);
  1523. set_ticket(compac, 0.0, true, true);
  1524. info->ticket_ok = true;
  1525. break;
  1526. }
  1527. applog(LOG_ERR, "%d: %s %d - ticket %u failure no low < %.1f after %d - retry %d",
  1528. compac->cgminer_id, compac->drv->name, compac->device_id,
  1529. ticket_1397[i].diff, ticket_1397[i].low_limit, info->ticket_work, info->ticket_failures+1);
  1530. // try again ...
  1531. //set_ticket(compac, ticket_1397[i].diff, true, true);
  1532. set_ticket(compac, 0.0, true, true);
  1533. info->ticket_failures++;
  1534. break;
  1535. }
  1536. }
  1537. }
  1538. while (0);
  1539. }
  1540. if (active_work && work)
  1541. work->device_diff = info->difficulty;
  1542. mutex_unlock(&info->lock);
  1543. if (active_work && work && submit_nonce(info->thr, work, nonce))
  1544. {
  1545. mutex_lock(&info->lock);
  1546. cgtime(&info->last_nonce);
  1547. cgtime(&asic->last_nonce);
  1548. // count of valid nonces
  1549. asic->nonces++; // info only
  1550. if (midnum > 0)
  1551. {
  1552. applog(LOG_INFO, "%d: %s %d - AsicBoost nonce found : midstate %d",
  1553. compac->cgminer_id, compac->drv->name, compac->device_id, midnum);
  1554. }
  1555. // if work diff < info->dificulty, 'accept' hash rate will be low
  1556. info->hashes += info->difficulty * 0xffffffffull;
  1557. info->xhashes += info->difficulty;
  1558. info->accepted++;
  1559. info->failing = false;
  1560. info->dups = 0;
  1561. asic->dups = 0;
  1562. mutex_unlock(&info->lock);
  1563. if (info->nb2c_setup)
  1564. add_gekko_nonce(info, asic, &(DATA_NONCE(item)->when));
  1565. else
  1566. add_gekko_nonce(info, NULL, &(DATA_NONCE(item)->when));
  1567. }
  1568. else
  1569. {
  1570. // shouldn't be possible since diff has already been checked
  1571. if (hwe != compac->hw_errors)
  1572. {
  1573. mutex_lock(&info->lock);
  1574. cgtime(&info->last_hwerror);
  1575. mutex_unlock(&info->lock);
  1576. }
  1577. }
  1578. }
  1579. static uint64_t compac_check_nonce(struct cgpu_info *compac)
  1580. {
  1581. struct COMPAC_INFO *info = compac->device_data;
  1582. uint32_t nonce = 0;
  1583. int hwe = compac->hw_errors;
  1584. uint32_t job_id = 0;
  1585. uint64_t hashes = 0;
  1586. struct timeval now;
  1587. int i;
  1588. if (info->asic_type == BM1387) {
  1589. job_id = info->rx[5] & 0xff;
  1590. nonce = (info->rx[3] << 0) | (info->rx[2] << 8) | (info->rx[1] << 16) | (info->rx[0] << 24);
  1591. } else if (info->asic_type == BM1384) {
  1592. job_id = info->rx[4] ^ 0x80;
  1593. nonce = (info->rx[3] << 0) | (info->rx[2] << 8) | (info->rx[1] << 16) | (info->rx[0] << 24);
  1594. } else if (info->asic_type == BM1397) {
  1595. // should never call here
  1596. return hashes;
  1597. }
  1598. if ((info->rx[0] == 0x72 && info->rx[1] == 0x03 && info->rx[2] == 0xEA && info->rx[3] == 0x83)
  1599. || (info->rx[0] == 0xE1 && info->rx[1] == 0x6B && info->rx[2] == 0xF8 && info->rx[3] == 0x09))
  1600. {
  1601. //busy work nonces
  1602. return hashes;
  1603. }
  1604. if (job_id > info->max_job_id
  1605. || (abs((int)(info->job_id) - (int)job_id) > 3 && abs((int)(info->max_job_id) - (int)job_id + (int)(info->job_id)) > 3))
  1606. {
  1607. return hashes;
  1608. }
  1609. if (!info->active_work[job_id]
  1610. && !(job_id > 0 && info->active_work[job_id - 1])
  1611. && !(job_id > 1 && info->active_work[job_id - 2])
  1612. && !(job_id > 2 && info->active_work[job_id - 3]))
  1613. {
  1614. return hashes;
  1615. }
  1616. cgtime(&now);
  1617. info->nonces++;
  1618. info->nonceless = 0;
  1619. int asic_id = (int)floor((double)(info->rx[0]) / ((double)0x100 / (double)(info->chips)));
  1620. struct ASIC_INFO *asic = &info->asics[asic_id];
  1621. if (nonce == asic->prev_nonce) {
  1622. applog(LOG_INFO, "%d: %s %d - Duplicate Nonce : %08x @ %02x [%02x %02x %02x %02x %02x %02x]",
  1623. compac->cgminer_id, compac->drv->name, compac->device_id, nonce, job_id,
  1624. info->rx[0], info->rx[1], info->rx[2], info->rx[3], info->rx[4], info->rx[5]);
  1625. info->dups++;
  1626. info->dupsall++;
  1627. info->dupsreset++;
  1628. asic->dups++;
  1629. asic->dupsall++;
  1630. cgtime(&info->last_dup_time);
  1631. if (info->dups == 1) {
  1632. info->mining_state = MINER_MINING_DUPS;
  1633. }
  1634. return hashes;
  1635. }
  1636. info->prev_nonce = nonce;
  1637. asic->prev_nonce = nonce;
  1638. applog(LOG_INFO, "%d: %s %d - Device reported nonce: %08x @ %02x (%d)",
  1639. compac->cgminer_id, compac->drv->name, compac->device_id, nonce, job_id, info->tracker);
  1640. struct work *work = info->work[job_id];
  1641. bool active_work = info->active_work[job_id];
  1642. int midnum = 0;
  1643. if (!opt_gekko_noboost && info->vmask)
  1644. {
  1645. // force check last few nonces by [job_id - 1]
  1646. // doesn't handle job_id roll over
  1647. if (info->asic_type == BM1387)
  1648. {
  1649. for (i = 0; i < info->midstates; i++)
  1650. {
  1651. if ((int)job_id >= i)
  1652. {
  1653. if (info->active_work[job_id - i])
  1654. {
  1655. work = info->work[job_id - i];
  1656. active_work = info->active_work[job_id - i];
  1657. if (active_work && work)
  1658. {
  1659. work->micro_job_id = pow(2, i);
  1660. memcpy(work->data, &(work->pool->vmask_001[work->micro_job_id]), 4);
  1661. if (test_nonce(work, nonce))
  1662. {
  1663. midnum = i;
  1664. if (i > 0)
  1665. info->boosted = true;
  1666. break;
  1667. }
  1668. }
  1669. }
  1670. }
  1671. }
  1672. }
  1673. }
  1674. if (!active_work || !work) {
  1675. return hashes;
  1676. }
  1677. work->device_diff = info->difficulty;
  1678. if (submit_nonce(info->thr, work, nonce)) {
  1679. cgtime(&info->last_nonce);
  1680. cgtime(&asic->last_nonce);
  1681. // count of valid nonces
  1682. asic->nonces++; // info only
  1683. if (midnum > 0) {
  1684. applog(LOG_INFO, "%d: %s %d - AsicBoost nonce found : midstate%d",
  1685. compac->cgminer_id, compac->drv->name, compac->device_id, midnum);
  1686. }
  1687. hashes = info->difficulty * 0xffffffffull;
  1688. info->xhashes += info->difficulty;
  1689. info->accepted++;
  1690. info->failing = false;
  1691. info->dups = 0;
  1692. asic->dups = 0;
  1693. add_gekko_nonce(info, asic, &now);
  1694. } else {
  1695. if (hwe != compac->hw_errors) {
  1696. cgtime(&info->last_hwerror);
  1697. }
  1698. }
  1699. return hashes;
  1700. }
  1701. static void busy_work(struct COMPAC_INFO *info)
  1702. {
  1703. memset(info->task, 0, info->task_len);
  1704. if (info->asic_type == BM1387 || info->asic_type == BM1397) {
  1705. info->task[0] = 0x21;
  1706. info->task[1] = info->task_len;
  1707. info->task[2] = info->job_id & 0xff;
  1708. info->task[3] = ((!opt_gekko_noboost && info->vmask) ? 0x04 : 0x01);
  1709. memset(info->task + 8, 0xff, 12);
  1710. unsigned short crc = crc16_false(info->task, info->task_len - 2);
  1711. info->task[info->task_len - 2] = (crc >> 8) & 0xff;
  1712. info->task[info->task_len - 1] = crc & 0xff;
  1713. } else if (info->asic_type == BM1384) {
  1714. if (info->mining_state == MINER_MINING) {
  1715. info->task[39] = info->ticket_mask & 0xff;
  1716. stuff_msb(info->task + 40, info->task_hcn);
  1717. }
  1718. info->task[51] = info->job_id & 0xff;
  1719. }
  1720. }
  1721. static void init_task(struct COMPAC_INFO *info)
  1722. {
  1723. struct work *work = info->work[info->job_id];
  1724. memset(info->task, 0, info->task_len);
  1725. if (info->asic_type == BM1387 || info->asic_type == BM1397) {
  1726. info->task[0] = 0x21;
  1727. info->task[1] = info->task_len;
  1728. info->task[2] = info->job_id & 0xff;
  1729. info->task[3] = ((!opt_gekko_noboost && info->vmask) ? info->midstates : 0x01);
  1730. if (info->mining_state == MINER_MINING) {
  1731. stuff_reverse(info->task + 8, work->data + 64, 12);
  1732. stuff_reverse(info->task + 20, work->midstate, 32);
  1733. if (!opt_gekko_noboost && info->vmask) {
  1734. if (info->midstates > 1)
  1735. stuff_reverse(info->task + 20 + 32, work->midstate1, 32);
  1736. if (info->midstates > 2)
  1737. stuff_reverse(info->task + 20 + 32 + 32, work->midstate2, 32);
  1738. if (info->midstates > 3)
  1739. stuff_reverse(info->task + 20 + 32 + 32 + 32, work->midstate3, 32);
  1740. }
  1741. } else {
  1742. memset(info->task + 8, 0xff, 12);
  1743. }
  1744. unsigned short crc = crc16_false(info->task, info->task_len - 2);
  1745. info->task[info->task_len - 2] = (crc >> 8) & 0xff;
  1746. info->task[info->task_len - 1] = crc & 0xff;
  1747. } else if (info->asic_type == BM1384) {
  1748. if (info->mining_state == MINER_MINING) {
  1749. stuff_reverse(info->task, work->midstate, 32);
  1750. stuff_reverse(info->task + 52, work->data + 64, 12);
  1751. info->task[39] = info->ticket_mask & 0xff;
  1752. stuff_msb(info->task + 40, info->task_hcn);
  1753. }
  1754. info->task[51] = info->job_id & 0xff;
  1755. }
  1756. }
  1757. static void change_freq_any(struct cgpu_info *compac, float new_freq)
  1758. {
  1759. struct COMPAC_INFO *info = compac->device_data;
  1760. unsigned int i;
  1761. float old_freq;
  1762. old_freq = info->frequency;
  1763. for (i = 0; i < info->chips; i++)
  1764. {
  1765. struct ASIC_INFO *asic = &info->asics[i];
  1766. cgtime(&info->last_frequency_adjust);
  1767. cgtime(&asic->last_frequency_adjust);
  1768. cgtime(&info->monitor_time);
  1769. asic->frequency_updated = 1;
  1770. if (info->asic_type == BM1397)
  1771. {
  1772. if (i == 0)
  1773. compac_set_frequency(compac, new_freq);
  1774. }
  1775. else if (info->asic_type == BM1387)
  1776. {
  1777. compac_set_frequency_single(compac, new_freq, i);
  1778. info->frequency = new_freq;
  1779. }
  1780. else if (info->asic_type == BM1384)
  1781. {
  1782. if (i == 0)
  1783. {
  1784. compac_set_frequency(compac, new_freq);
  1785. compac_send_chain_inactive(compac);
  1786. info->frequency = new_freq;
  1787. }
  1788. }
  1789. }
  1790. applog(LOG_WARNING,"%d: %s %d - new frequency %.2fMHz -> %.2fMHz",
  1791. compac->cgminer_id, compac->drv->name, compac->device_id,
  1792. old_freq, new_freq);
  1793. }
  1794. // compac_mine() with long term adjustments
  1795. static void *compac_mine2(void *object)
  1796. {
  1797. struct cgpu_info *compac = (struct cgpu_info *)object;
  1798. struct COMPAC_INFO *info = compac->device_data;
  1799. struct work *work = NULL;
  1800. struct work *old_work = NULL;
  1801. struct timeval now;
  1802. struct timeval last_rolling = (struct timeval){0};
  1803. struct timeval last_movement = (struct timeval){0};
  1804. struct timeval last_plateau_check = (struct timeval){0};
  1805. struct timeval last_frequency_check = (struct timeval){0};
  1806. struct sched_param param;
  1807. int sent_bytes, sleep_us, use_us, policy, ret_nice;
  1808. double diff_us, left_us;
  1809. unsigned int i, j;
  1810. uint32_t err = 0;
  1811. uint64_t hashrate_gs;
  1812. double dev_runtime, wu;
  1813. float frequency_computed;
  1814. bool frequency_updated;
  1815. bool has_freq;
  1816. bool job_added;
  1817. bool last_was_busy = false;
  1818. int plateau_type = 0;
  1819. #ifndef WIN32
  1820. ret_nice = nice(-15);
  1821. #else /* WIN32 */
  1822. pthread_getschedparam(pthread_self(), &policy, &param);
  1823. param.sched_priority = sched_get_priority_max(policy);
  1824. pthread_setschedparam(pthread_self(), policy, &param);
  1825. ret_nice = param.sched_priority;
  1826. #endif /* WIN32 */
  1827. applog(LOG_INFO, "%d: %s %d - work thread niceness (%d)",
  1828. compac->cgminer_id, compac->drv->name, compac->device_id, ret_nice);
  1829. sleep_us = 100;
  1830. cgtime(&last_plateau_check);
  1831. while (info->mining_state != MINER_SHUTDOWN)
  1832. {
  1833. if (old_work)
  1834. {
  1835. mutex_lock(&info->lock);
  1836. work_completed(compac, old_work);
  1837. mutex_unlock(&info->lock);
  1838. old_work = NULL;
  1839. }
  1840. if (info->chips == 0
  1841. || compac->deven == DEV_DISABLED
  1842. || compac->usbinfo.nodev
  1843. || (info->mining_state != MINER_MINING && info->mining_state != MINER_MINING_DUPS))
  1844. {
  1845. gekko_usleep(info, MS2US(10));
  1846. continue;
  1847. }
  1848. cgtime(&now);
  1849. if (!info->update_work)
  1850. {
  1851. diff_us = us_tdiff(&now, &info->last_task);
  1852. left_us = info->max_task_wait - diff_us;
  1853. if (left_us > 0)
  1854. {
  1855. // allow 300us from here to next sleep
  1856. left_us -= 300;
  1857. use_us = sleep_us;
  1858. if (use_us > left_us)
  1859. use_us = left_us;
  1860. // 1ms is more than enough
  1861. if (use_us > 1000)
  1862. use_us = 1000;
  1863. if (use_us >= USLEEPMIN)
  1864. {
  1865. gekko_usleep(info, use_us);
  1866. continue;
  1867. }
  1868. }
  1869. }
  1870. frequency_updated = 0;
  1871. info->update_work = 0;
  1872. sleep_us = bound(ceil(info->max_task_wait / 20), 1, 100 * 1000); // 1us .. 100ms
  1873. dev_runtime = cgpu_runtime(compac);
  1874. wu = compac->diff1 / dev_runtime * 60;
  1875. if (wu > info->wu_max)
  1876. {
  1877. info->wu_max = wu;
  1878. cgtime(&info->last_wu_increase);
  1879. }
  1880. // don't change anything until we have 10s of data since a reset
  1881. if (ms_tdiff(&now, &info->last_reset) > MS_SECOND_10)
  1882. {
  1883. if (ms_tdiff(&now, &last_rolling) >= MS_SECOND_1)
  1884. {
  1885. mutex_lock(&info->ghlock);
  1886. if (info->gh.noncesum > (GHNONCENEEDED+1))
  1887. {
  1888. cgtime(&last_rolling);
  1889. info->rolling = gekko_gh_hashrate(info, &last_rolling, true);
  1890. }
  1891. mutex_unlock(&info->ghlock);
  1892. }
  1893. hashrate_gs = (double)info->rolling * 1000000ull;
  1894. info->eff_gs = 100.0 * (1.0 * hashrate_gs / info->hashrate);
  1895. info->eff_wu = 100.0 * (1.0 * wu / info->wu);
  1896. if (info->eff_gs > 100)
  1897. info->eff_gs = 100;
  1898. if (info->eff_wu > 100)
  1899. info->eff_wu = 100;
  1900. frequency_computed = ((hashrate_gs / 1.0e6) / info->cores) / info->chips;
  1901. frequency_computed = limit_freq(info, frequency_computed, true);
  1902. if (frequency_computed > info->frequency_computed
  1903. && frequency_computed <= info->frequency)
  1904. {
  1905. info->frequency_computed = frequency_computed;
  1906. cgtime(&info->last_computed_increase);
  1907. applog(LOG_INFO, "%d: %s %d - new comp=%.2f (gs=%.2f)",
  1908. compac->cgminer_id, compac->drv->name, compac->device_id, frequency_computed,
  1909. ((double)hashrate_gs)/1.0e6);
  1910. }
  1911. plateau_type = 0;
  1912. // search for plateau
  1913. if (ms_tdiff(&now, &last_plateau_check) > MS_SECOND_5)
  1914. {
  1915. has_freq = false;
  1916. for (i = 0; i < info->chips; i++)
  1917. {
  1918. if (info->asics[i].frequency != 0)
  1919. {
  1920. has_freq = true;
  1921. break;
  1922. }
  1923. }
  1924. cgtime(&last_plateau_check);
  1925. for (i = 0; i < info->chips; i++)
  1926. {
  1927. struct ASIC_INFO *asic = &info->asics[i];
  1928. // missing nonces
  1929. if (info->asic_type == BM1397)
  1930. {
  1931. if (has_freq && i == 0 && info->nonce_limit > 0.0
  1932. && ms_tdiff(&now, &info->last_nonce) > info->nonce_limit)
  1933. {
  1934. plateau_type = PT_NONONCE;
  1935. applog(LOG_ERR, "%d: %s %d - plateau_type PT_NONONCE [%u] %d > %.2f (lock=%d)",
  1936. compac->cgminer_id, compac->drv->name, compac->device_id, i,
  1937. ms_tdiff(&now, &info->last_nonce), info->nonce_limit, info->lock_freq);
  1938. if (info->lock_freq)
  1939. info->lock_freq = false;
  1940. }
  1941. }
  1942. else
  1943. {
  1944. if (has_freq && info->nonce_limit > 0.0
  1945. && ms_tdiff(&now, &asic->last_nonce) > info->nonce_limit)
  1946. {
  1947. plateau_type = PT_NONONCE;
  1948. applog(LOG_ERR, "%d: %s %d - plateau_type PT_NONONCE [%u] %d > %.2f (lock=%d)",
  1949. compac->cgminer_id, compac->drv->name, compac->device_id, i,
  1950. ms_tdiff(&now, &asic->last_nonce), info->nonce_limit, info->lock_freq);
  1951. if (info->lock_freq)
  1952. info->lock_freq = false;
  1953. }
  1954. }
  1955. // asic check-in failed
  1956. if (!info->lock_freq
  1957. && info->asic_type != BM1397)
  1958. {
  1959. if (ms_tdiff(&asic->last_frequency_ping, &asic->last_frequency_reply) > MS_SECOND_30
  1960. && ms_tdiff(&now, &asic->last_frequency_reply) > MS_SECOND_30)
  1961. {
  1962. plateau_type = PT_FREQNR;
  1963. applog(LOG_INFO, "%d: %s %d - plateau_type PT_FREQNR [%u] %d > %d",
  1964. compac->cgminer_id, compac->drv->name, compac->device_id, i,
  1965. ms_tdiff(&now, &asic->last_frequency_reply), MS_SECOND_30);
  1966. }
  1967. }
  1968. // set frequency requests not honored
  1969. if (!info->lock_freq
  1970. && asic->frequency_attempt > 3)
  1971. {
  1972. plateau_type = PT_FREQSET;
  1973. applog(LOG_INFO, "%d: %s %d - plateau_type PT_FREQSET [%u] %u > 3",
  1974. compac->cgminer_id, compac->drv->name, compac->device_id, i, asic->frequency_attempt);
  1975. }
  1976. if (plateau_type)
  1977. {
  1978. float old_frequency, new_frequency;
  1979. new_frequency = info->frequency_requested;
  1980. bool didmsg, doreset;
  1981. char *reason;
  1982. char freq_buf[512];
  1983. char freq_chip_buf[15];
  1984. memset(freq_buf, 0, sizeof(freq_buf));
  1985. memset(freq_chip_buf, 0, sizeof(freq_chip_buf));
  1986. for (j = 0; j < info->chips; j++)
  1987. {
  1988. struct ASIC_INFO *asjc = &info->asics[j];
  1989. sprintf(freq_chip_buf, "[%d:%.2f]", j, asjc->frequency);
  1990. strcat(freq_buf, freq_chip_buf);
  1991. }
  1992. applog(LOG_INFO,"%d: %s %d - %s",
  1993. compac->cgminer_id, compac->drv->name, compac->device_id, freq_buf);
  1994. if (info->plateau_reset < 3)
  1995. {
  1996. // Capture failure high frequency using first three resets
  1997. if ((info->frequency - info->freq_base) > info->frequency_fail_high)
  1998. info->frequency_fail_high = (info->frequency - info->freq_base);
  1999. applog(LOG_WARNING,"%d: %s %d - asic plateau: [%u] (%d/3) %.2fMHz",
  2000. compac->cgminer_id, compac->drv->name, compac->device_id,
  2001. i, info->plateau_reset + 1, info->frequency_fail_high);
  2002. }
  2003. if (info->plateau_reset >= 2) {
  2004. if (ms_tdiff(&now, &info->last_frequency_adjust) > MS_MINUTE_30) {
  2005. // Been running for 30 minutes, possible plateau
  2006. // Overlook the incident
  2007. } else {
  2008. // Step back frequency
  2009. info->frequency_fail_high -= info->freq_base;
  2010. }
  2011. new_frequency = limit_freq(info, FREQ_BASE(info->frequency_fail_high), true);
  2012. }
  2013. info->plateau_reset++;
  2014. asic->last_state = asic->state;
  2015. asic->state = ASIC_HALFDEAD;
  2016. cgtime(&asic->state_change_time);
  2017. cgtime(&info->monitor_time);
  2018. switch (plateau_type)
  2019. {
  2020. case PT_FREQNR:
  2021. applog(info->log_wide,"%d: %s %d - no frequency reply from chip[%u] - %.2fMHz",
  2022. compac->cgminer_id, compac->drv->name, compac->device_id, i, info->frequency);
  2023. asic->frequency_attempt = 0;
  2024. reason = " FREQNR";
  2025. break;
  2026. case PT_FREQSET:
  2027. applog(info->log_wide,"%d: %s %d - frequency set fail to chip[%u] - %.2fMHz",
  2028. compac->cgminer_id, compac->drv->name, compac->device_id, i, info->frequency);
  2029. asic->frequency_attempt = 0;
  2030. reason = " FREQSET";
  2031. break;
  2032. case PT_NONONCE:
  2033. applog(info->log_wide,"%d: %s %d - missing nonces from chip[%u] - %.2fMHz",
  2034. compac->cgminer_id, compac->drv->name, compac->device_id, i, info->frequency);
  2035. reason = " NONONCE";
  2036. break;
  2037. default:
  2038. reason = NULL;
  2039. break;
  2040. }
  2041. if (plateau_type == PT_NONONCE || info->asic_type == BM1387 || info->asic_type == BM1397)
  2042. {
  2043. // BM1384 is less tolerant to sudden drops in frequency.
  2044. // Ignore other indicators except no nonce.
  2045. doreset = true;
  2046. }
  2047. else
  2048. doreset = false;
  2049. didmsg = false;
  2050. old_frequency = info->frequency_requested;
  2051. if (new_frequency != old_frequency)
  2052. {
  2053. info->frequency_requested = new_frequency;
  2054. applog(LOG_WARNING,"%d: %s %d - plateau%s [%u] adjust:%s target frequency %.2fMHz -> %.2fMHz",
  2055. compac->cgminer_id, compac->drv->name, compac->device_id,
  2056. reason ? : "", i, doreset ? " RESET" : "", old_frequency, new_frequency);
  2057. didmsg = true;
  2058. }
  2059. if (doreset)
  2060. {
  2061. if (didmsg == false)
  2062. {
  2063. applog(LOG_WARNING,"%d: %s %d - plateau%s [%u] RESET at %.2fMHz",
  2064. compac->cgminer_id, compac->drv->name, compac->device_id,
  2065. reason ? : "", i, old_frequency);
  2066. }
  2067. info->mining_state = MINER_RESET;
  2068. }
  2069. // any plateau on 1 chip means no need to check the rest
  2070. break;
  2071. }
  2072. }
  2073. }
  2074. if (!info->lock_freq
  2075. && ms_tdiff(&now, &info->last_reset) < info->ramp_time)
  2076. {
  2077. // move running frequency towards target every second
  2078. // initially or for up to ramp_time after a reset
  2079. if ((plateau_type == 0)
  2080. && (ms_tdiff(&now, &last_movement) > MS_SECOND_1)
  2081. && (info->frequency < info->frequency_requested)
  2082. && (info->gh.noncesum > GHNONCENEEDED))
  2083. {
  2084. float new_frequency = info->frequency + info->step_freq;
  2085. if (new_frequency > info->frequency_requested)
  2086. new_frequency = info->frequency_requested;
  2087. frequency_updated = 1;
  2088. change_freq_any(compac, new_frequency);
  2089. cgtime(&last_movement);
  2090. }
  2091. }
  2092. else
  2093. {
  2094. // after ramp_time regularly check the frequency vs hashrate
  2095. // when we have enough nonces or gh is full
  2096. if (!info->lock_freq
  2097. && (info->gh.last == (GHNUM-1) || info->gh.noncesum > GHNONCES)
  2098. && (ms_tdiff(&now, &info->tune_limit) >= MS_MINUTE_2))
  2099. {
  2100. float new_freq, prev_freq;
  2101. double hash_for_freq, curr_hr;
  2102. int nonces, last;
  2103. prev_freq = info->frequency;
  2104. mutex_lock(&info->ghlock);
  2105. curr_hr = gekko_gh_hashrate(info, &now, true);
  2106. nonces = info->gh.noncesum;
  2107. last = info->gh.last;
  2108. mutex_unlock(&info->ghlock);
  2109. // verify with locked values
  2110. if ((last == (GHNUM-1)) || (nonces > GHNONCES))
  2111. {
  2112. hash_for_freq = info->frequency * (double)(info->cores * info->chips);
  2113. // a low hash rate means frequency may be too high
  2114. if (curr_hr < (hash_for_freq * info->ghrequire))
  2115. {
  2116. new_freq = FREQ_BASE(info->frequency - (info->freq_base * 2));
  2117. if (new_freq < info->min_freq)
  2118. new_freq = FREQ_BASE(info->min_freq);
  2119. if (info->frequency_requested > new_freq)
  2120. info->frequency_requested = new_freq;
  2121. if (info->frequency_start > new_freq)
  2122. info->frequency_start = new_freq;
  2123. applog(LOG_WARNING,"%d: %s %d - %.2fGH/s low [%.2f/%.2f/%d] reset limit %.2fMHz -> %.2fMHz",
  2124. compac->cgminer_id, compac->drv->name, compac->device_id,
  2125. curr_hr/1.0e3, hash_for_freq/1.0e3, hash_for_freq*info->ghrequire/1.0e3,
  2126. nonces, prev_freq, new_freq);
  2127. mutex_lock(&info->lock);
  2128. frequency_updated = 1;
  2129. change_freq_any(compac, info->frequency_start);
  2130. // reset from start
  2131. info->mining_state = MINER_RESET;
  2132. mutex_unlock(&info->lock);
  2133. continue;
  2134. }
  2135. cgtime(&info->tune_limit);
  2136. // step the freq up one - environment may have changed to allow it
  2137. if (opt_gekko_tune2 != 0
  2138. && (info->frequency_requested < info->frequency_selected)
  2139. && (tdiff(&now, &info->last_reset) >= ((double)opt_gekko_tune2 * 60.0))
  2140. && (tdiff(&now, &info->last_tune_up) >= ((double)opt_gekko_tune2 * 60.0)))
  2141. {
  2142. new_freq = FREQ_BASE(info->frequency + info->freq_base);
  2143. if (new_freq <= info->frequency_selected)
  2144. {
  2145. if (info->frequency_requested < new_freq)
  2146. info->frequency_requested = new_freq;
  2147. applog(LOG_WARNING,"%d: %s %d - tune up attempt (%.1fGH/s) %.2fMHz -> %.2fMHz",
  2148. compac->cgminer_id, compac->drv->name, compac->device_id,
  2149. curr_hr/1.0e3, prev_freq, new_freq);
  2150. // will reset if it doesn't improve,
  2151. // as soon as it has enough nonces
  2152. frequency_updated = 1;
  2153. change_freq_any(compac, new_freq);
  2154. }
  2155. cgtime(&info->last_tune_up);
  2156. }
  2157. }
  2158. }
  2159. }
  2160. if (!info->lock_freq
  2161. && !frequency_updated
  2162. && ms_tdiff(&now, &last_frequency_check) > 20)
  2163. {
  2164. cgtime(&last_frequency_check);
  2165. for (i = 0; i < info->chips; i++)
  2166. {
  2167. struct ASIC_INFO *asic = &info->asics[i];
  2168. if (asic->frequency_updated)
  2169. {
  2170. asic->frequency_updated = 0;
  2171. info->frequency_of = i;
  2172. if (info->asic_type != BM1397)
  2173. ping_freq(compac, i);
  2174. break;
  2175. }
  2176. }
  2177. }
  2178. }
  2179. has_freq = false;
  2180. for (i = 0; i < info->chips; i++)
  2181. {
  2182. if (info->asics[i].frequency != 0)
  2183. {
  2184. has_freq = true;
  2185. break;
  2186. }
  2187. }
  2188. // don't bother with work if it's not mining
  2189. if (!has_freq)
  2190. {
  2191. gekko_usleep(info, MS2US(10));
  2192. continue;
  2193. }
  2194. struct timeval stt, fin;
  2195. double wd;
  2196. // don't delay work updates when doing busy work
  2197. if (info->work_usec_num > 1 && !last_was_busy)
  2198. {
  2199. // check if we got here early
  2200. // and sleep almost the entire delay required
  2201. cgtime(&now);
  2202. diff_us = us_tdiff(&now, &info->last_task);
  2203. left_us = info->max_task_wait - diff_us;
  2204. if (left_us > 0)
  2205. {
  2206. // allow time for get_queued() + a bit
  2207. left_us -= (info->work_usec_avg + USLEEPPLUS);
  2208. if (left_us >= USLEEPMIN)
  2209. gekko_usleep(info, left_us);
  2210. }
  2211. else
  2212. {
  2213. #if TUNE_CODE
  2214. // ran over by 10us or more
  2215. if (left_us <= -10)
  2216. {
  2217. info->over1num++;
  2218. info->over1amt -= left_us;
  2219. }
  2220. #endif
  2221. }
  2222. }
  2223. cgtime(&stt);
  2224. work = get_queued(compac);
  2225. if (work)
  2226. {
  2227. cgtime(&fin);
  2228. wd = us_tdiff(&fin, &stt);
  2229. if (info->work_usec_num == 0)
  2230. info->work_usec_avg = wd;
  2231. else
  2232. {
  2233. // fast work times should have a higher effect
  2234. if (wd < (info->work_usec_avg / 2.0))
  2235. info->work_usec_avg = (info->work_usec_avg + wd) / 2.0;
  2236. else
  2237. {
  2238. // ignore extra long work times after we get a few
  2239. if (info->work_usec_num > 5
  2240. && (wd / 3.0) < info->work_usec_avg)
  2241. {
  2242. info->work_usec_avg =
  2243. (info->work_usec_avg * 9.0 + wd) / 10.0;
  2244. }
  2245. }
  2246. }
  2247. info->work_usec_num++;
  2248. if (last_was_busy)
  2249. last_was_busy = false;
  2250. #if TUNE_CODE
  2251. diff_us = us_tdiff(&fin, &info->last_task);
  2252. // stats if we got here too fast ...
  2253. left_us = info->max_task_wait - diff_us;
  2254. if (left_us < 0)
  2255. {
  2256. // ran over by 10us or more
  2257. if (left_us <= -10)
  2258. {
  2259. info->over2num++;
  2260. info->over2amt -= left_us;
  2261. }
  2262. }
  2263. #endif
  2264. if (opt_gekko_noboost)
  2265. work->pool->vmask = 0;
  2266. info->job_id += info->add_job_id;
  2267. if (info->job_id > info->max_job_id)
  2268. info->job_id = info->min_job_id;
  2269. old_work = info->work[info->job_id];
  2270. info->work[info->job_id] = work;
  2271. info->active_work[info->job_id] = 1;
  2272. info->vmask = work->pool->vmask;
  2273. if (info->asic_type == BM1387 || info->asic_type == BM1397)
  2274. {
  2275. if (!opt_gekko_noboost && info->vmask)
  2276. info->task_len = 54 + 32 * (info->midstates - 1);
  2277. else
  2278. info->task_len = 54;
  2279. }
  2280. init_task(info);
  2281. }
  2282. else
  2283. {
  2284. struct pool *cp;
  2285. cp = current_pool();
  2286. if (!cp->stratum_active)
  2287. cgtime(&info->last_pool_lost);
  2288. if (cp->stratum_active
  2289. && (info->asic_type == BM1387 || info->asic_type == BM1397))
  2290. {
  2291. // get Dups instead of sending busy work
  2292. // sleep 1ms then fast loop back
  2293. gekko_usleep(info, MS2US(1));
  2294. last_was_busy = true;
  2295. continue;
  2296. }
  2297. busy_work(info);
  2298. info->busy_work++;
  2299. last_was_busy = true;
  2300. cgtime(&info->monitor_time);
  2301. applog(LOG_INFO, "%d: %s %d - Busy",
  2302. compac->cgminer_id, compac->drv->name, compac->device_id);
  2303. }
  2304. int task_len = info->task_len;
  2305. #if 0
  2306. unsigned char jid = info->task[2];
  2307. #endif
  2308. if (info->asic_type == BM1397)
  2309. {
  2310. int k;
  2311. for (k = (info->task_len - 1); k >= 0; k--)
  2312. {
  2313. info->task[k+2] = info->task[k];
  2314. }
  2315. info->task[0] = 0x55;
  2316. info->task[1] = 0xaa;
  2317. task_len += 2;
  2318. }
  2319. #if 0
  2320. applog(LOG_ERR, "%s() %d: %s %d - Task [%02x] len %3u", __func__,
  2321. compac->cgminer_id, compac->drv->name, compac->device_id, jid, task_len);
  2322. applog(LOG_ERR, " [%02x %02x %02x %02x %02x %02x %02x %02x]",
  2323. info->task[0], info->task[1], info->task[2], info->task[3], info->task[4], info->task[5], info->task[6], info->task[7]);
  2324. applog(LOG_ERR, " [%02x %02x %02x %02x %02x %02x %02x %02x]",
  2325. info->task[8], info->task[9], info->task[10], info->task[11], info->task[12], info->task[13], info->task[14], info->task[15]);
  2326. applog(LOG_ERR, " [%02x %02x %02x %02x %02x %02x %02x %02x]",
  2327. info->task[16], info->task[17], info->task[18], info->task[19], info->task[20], info->task[21], info->task[22], info->task[23]);
  2328. applog(LOG_ERR, " [%02x %02x %02x %02x %02x %02x %02x %02x]",
  2329. info->task[24], info->task[25], info->task[26], info->task[27], info->task[28], info->task[29], info->task[30], info->task[31]);
  2330. #endif
  2331. cgtime(&now); // set the time we actually sent it
  2332. err = usb_write(compac, (char *)info->task, task_len, &sent_bytes, C_SENDWORK);
  2333. //dumpbuffer(compac, LOG_WARNING, "TASK.TX", info->task, task_len);
  2334. if (err != LIBUSB_SUCCESS)
  2335. {
  2336. applog(LOG_WARNING,"%d: %s %d - usb failure (%d)", compac->cgminer_id, compac->drv->name, compac->device_id, err);
  2337. info->mining_state = MINER_RESET;
  2338. continue;
  2339. }
  2340. if (sent_bytes != task_len)
  2341. {
  2342. if (ms_tdiff(&now, &info->last_write_error) > (5 * 1000)) {
  2343. applog(LOG_WARNING,"%d: %s %d - usb write error [%d:%d]",
  2344. compac->cgminer_id, compac->drv->name, compac->device_id, sent_bytes, task_len);
  2345. cgtime(&info->last_write_error);
  2346. }
  2347. job_added = false;
  2348. }
  2349. else
  2350. {
  2351. // successfully sent work
  2352. add_gekko_job(info, &now, false);
  2353. job_added = true;
  2354. }
  2355. //let the usb frame propagate
  2356. if (info->asic_type == BM1397 && info->usb_prop != 1000)
  2357. gekko_usleep(info, info->usb_prop);
  2358. else
  2359. gekko_usleep(info, MS2US(1));
  2360. info->task_ms = (info->task_ms * 9 + ms_tdiff(&now, &info->last_task)) / 10;
  2361. info->last_task.tv_sec = now.tv_sec;
  2362. info->last_task.tv_usec = now.tv_usec;
  2363. if (info->first_task.tv_sec == 0L)
  2364. {
  2365. info->first_task.tv_sec = now.tv_sec;
  2366. info->first_task.tv_usec = now.tv_usec;
  2367. }
  2368. info->tasks++;
  2369. // work source changes can affect this e.g. 1 vs 4 midstates
  2370. if (work && job_added
  2371. && ms_tdiff(&now, &info->last_update_rates) > MS_SECOND_5)
  2372. {
  2373. compac_update_rates(compac);
  2374. }
  2375. }
  2376. return NULL;
  2377. }
  2378. // not called by BM1397
  2379. static void *compac_handle_rx(void *object, int read_bytes, int path)
  2380. {
  2381. struct cgpu_info *compac = (struct cgpu_info *)object;
  2382. struct COMPAC_INFO *info = compac->device_data;
  2383. struct ASIC_INFO *asic;
  2384. int cmd_resp;
  2385. unsigned int i;
  2386. struct timeval now;
  2387. cgtime(&now);
  2388. cmd_resp = 0;
  2389. if (info->rx[read_bytes-1] <= 0x1f)
  2390. {
  2391. if (bmcrc(info->rx, 8 * read_bytes - 5) == info->rx[read_bytes-1])
  2392. cmd_resp = 1;
  2393. }
  2394. int log_level = (cmd_resp) ? LOG_INFO : LOG_INFO;
  2395. if (path) {
  2396. dumpbuffer(compac, log_level, "RX1", info->rx, read_bytes);
  2397. } else {
  2398. dumpbuffer(compac, log_level, "RX0", info->rx, read_bytes);
  2399. }
  2400. if (cmd_resp && info->rx[0] == 0x80 && info->frequency_of != (int)(info->chips)) {
  2401. float frequency = 0.0;
  2402. int frequency_of = info->frequency_of;
  2403. info->frequency_of = info->chips;
  2404. cgtime(&info->last_frequency_report);
  2405. if (info->asic_type == BM1387 && (info->rx[2] == 0 || (info->rx[3] >> 4) == 0 || (info->rx[3] & 0x0f) != 1 || (info->rx[4]) != 0 || (info->rx[5]) != 0)) {
  2406. cgtime(&info->last_frequency_invalid);
  2407. applog(LOG_INFO,"%d: %s %d - invalid frequency report", compac->cgminer_id, compac->drv->name, compac->device_id);
  2408. } else {
  2409. if (info->asic_type == BM1387) {
  2410. frequency = info->freq_mult * info->rx[1] / (info->rx[2] * (info->rx[3] >> 4) * (info->rx[3] & 0x0f));
  2411. } else if (info->asic_type == BM1384) {
  2412. frequency = (info->rx[1] + 1) * info->freq_base / ((1 + info->rx[2]) & 0x0f) * pow(2, (3 - info->rx[3])) + ((info->rx[2] >> 4) * info->freq_base);
  2413. }
  2414. if (frequency_of != (int)(info->chips)) {
  2415. asic = &info->asics[frequency_of];
  2416. cgtime(&asic->last_frequency_reply);
  2417. if (frequency != asic->frequency) {
  2418. bool syncd = 1;
  2419. if (frequency < asic->frequency && frequency != info->frequency_requested) {
  2420. applog(LOG_INFO,"%d: %s %d - chip[%d] reported frequency at %.2fMHz", compac->cgminer_id, compac->drv->name, compac->device_id, info->frequency_of, frequency);
  2421. } else {
  2422. applog(LOG_INFO,"%d: %s %d - chip[%d] reported new frequency of %.2fMHz", compac->cgminer_id, compac->drv->name, compac->device_id, info->frequency_of, frequency);
  2423. }
  2424. asic->frequency = frequency;
  2425. if (asic->frequency == asic->frequency_set) {
  2426. asic->frequency_attempt = 0;
  2427. }
  2428. for (i = 1; i < info->chips; i++) {
  2429. if (info->asics[i].frequency != info->asics[i - 1].frequency) {
  2430. syncd = 0;
  2431. }
  2432. }
  2433. if (info->frequency_syncd != syncd) {
  2434. info->frequency_syncd = syncd;
  2435. applog(LOG_INFO,"%d: %s %d - syncd [%d]", compac->cgminer_id, compac->drv->name, compac->device_id, syncd);
  2436. }
  2437. } else {
  2438. applog(LOG_INFO,"%d: %s %d - chip[%d] reported frequency of %.2fMHz", compac->cgminer_id, compac->drv->name, compac->device_id, info->frequency_of, frequency);
  2439. }
  2440. } else {
  2441. //applog(LOG_INFO,"%d: %s %d - [-1] reported frequency of %.2fMHz", compac->cgminer_id, compac->drv->name, compac->device_id, frequency);
  2442. //if (frequency != info->frequency) {
  2443. // info->frequency = frequency;
  2444. //}
  2445. }
  2446. compac_update_rates(compac);
  2447. }
  2448. }
  2449. switch (info->mining_state) {
  2450. case MINER_CHIP_COUNT:
  2451. case MINER_CHIP_COUNT_XX:
  2452. if (cmd_resp && (info->rx[0] == 0x13 ||
  2453. (info->rx[0] == 0xaa && info->rx[1] == 0x55 && info->rx[2] == 0x13))) { // BM1397
  2454. struct ASIC_INFO *asic = &info->asics[info->chips];
  2455. memset(asic, 0, sizeof(struct ASIC_INFO));
  2456. asic->frequency = info->frequency_default;
  2457. asic->frequency_attempt = 0;
  2458. asic->last_frequency_ping = (struct timeval){0};
  2459. asic->last_frequency_reply = (struct timeval){0};
  2460. cgtime(&asic->last_nonce);
  2461. info->chips++;
  2462. info->mining_state = MINER_CHIP_COUNT_XX;
  2463. compac_update_rates(compac);
  2464. }
  2465. break;
  2466. case MINER_OPEN_CORE:
  2467. if ((info->rx[0] == 0x72 && info->rx[1] == 0x03 && info->rx[2] == 0xEA && info->rx[3] == 0x83) ||
  2468. (info->rx[0] == 0xE1 && info->rx[1] == 0x6B && info->rx[2] == 0xF8 && info->rx[3] == 0x09)) {
  2469. //open core nonces = healthy chips.
  2470. info->zero_check++;
  2471. }
  2472. break;
  2473. case MINER_MINING:
  2474. if (!cmd_resp) {
  2475. #ifdef __APPLE__
  2476. if (opt_mac_yield)
  2477. sched_yield();
  2478. #else
  2479. selective_yield();
  2480. #endif
  2481. mutex_lock(&info->lock);
  2482. info->hashes += compac_check_nonce(compac);
  2483. mutex_unlock(&info->lock);
  2484. }
  2485. break;
  2486. default:
  2487. break;
  2488. }
  2489. return NULL;
  2490. }
  2491. static void *compac_gsf_nonce_que(void *object)
  2492. {
  2493. struct cgpu_info *compac = (struct cgpu_info *)object;
  2494. struct COMPAC_INFO *info = compac->device_data;
  2495. struct timespec abstime, addtime;
  2496. K_ITEM *item;
  2497. int rc;
  2498. if (info->asic_type != BM1397)
  2499. return NULL;
  2500. // wait at most 42ms for a nonce
  2501. ms_to_timespec(&addtime, 42);
  2502. while (info->mining_state != MINER_SHUTDOWN)
  2503. {
  2504. K_WLOCK(info->nlist);
  2505. item = k_unlink_head(info->nstore);
  2506. K_WUNLOCK(info->nlist);
  2507. if (item)
  2508. {
  2509. compac_gsf_nonce(compac, item);
  2510. K_WLOCK(info->nlist);
  2511. k_add_head(info->nlist, item);
  2512. K_WUNLOCK(info->nlist);
  2513. }
  2514. else
  2515. {
  2516. cgcond_time(&abstime);
  2517. timeraddspec(&abstime, &addtime);
  2518. mutex_lock(&info->nlock);
  2519. rc = pthread_cond_timedwait(&info->ncond, &info->nlock, &abstime);
  2520. mutex_unlock(&info->nlock);
  2521. if (rc == ETIMEDOUT)
  2522. info->ntimeout++;
  2523. else
  2524. info->ntrigger++;
  2525. }
  2526. }
  2527. return NULL;
  2528. }
  2529. static bool gsf_reply(struct COMPAC_INFO *info, unsigned char *rx, int len, struct timeval *now)
  2530. {
  2531. unsigned char fa, fb, fc1, fc2;
  2532. bool used = false;
  2533. if (len == (int)(info->rx_len) && rx[7] == BM1397FREQ)
  2534. {
  2535. int chip = TOCHIPPY1397(info, rx[6]);
  2536. if (chip >= 0 && chip < (int)(info->chips))
  2537. {
  2538. struct ASIC_INFO *asic = &info->asics[chip];
  2539. fa = rx[3];
  2540. fb = rx[4];
  2541. fc1 = (rx[5] & 0xf0) >> 4;
  2542. fc2 = rx[5] & 0x0f;
  2543. // only allow a valid reply
  2544. if (fa >= 0 && fb > 0 && fc1 > 0 && fc2 > 0)
  2545. {
  2546. asic->frequency_reply = info->freq_mult * fa / fb / fc1 / fc2;
  2547. asic->last_frequency_reply.tv_sec = now->tv_sec;
  2548. asic->last_frequency_reply.tv_usec = now->tv_usec;
  2549. used = true;
  2550. }
  2551. }
  2552. }
  2553. return used;
  2554. }
  2555. static void *compac_listen2(struct cgpu_info *compac, struct COMPAC_INFO *info)
  2556. {
  2557. unsigned char rx[BUFFER_MAX];
  2558. struct timeval now;
  2559. int read_bytes, tmo, pos = 0, len, i, prelen;
  2560. bool okcrc, used, chipped;
  2561. K_ITEM *item;
  2562. memset(rx, 0, sizeof(rx));
  2563. while (info->mining_state != MINER_SHUTDOWN)
  2564. {
  2565. tmo = 20;
  2566. if (info->mining_state == MINER_CHIP_COUNT)
  2567. {
  2568. unsigned char chippy[] = {0x52, 0x05, 0x00, 0x00, 0x0A};
  2569. compac_send2(compac, chippy, sizeof(chippy), 8 * sizeof(chippy) - 8, "CHIPPY");
  2570. info->mining_state = MINER_CHIP_COUNT_XX;
  2571. // initial config reply allow much longer
  2572. tmo = 1000;
  2573. }
  2574. usb_read_timeout(compac, ((char *)rx)+pos, BUFFER_MAX-pos, &read_bytes, tmo, C_GETRESULTS);
  2575. pos += read_bytes;
  2576. cgtime(&now);
  2577. // all replies should be info->rx_len
  2578. while (read_bytes > 0 && pos >= (int)(info->rx_len))
  2579. {
  2580. #if 0
  2581. applog(LOG_ERR, "%d: %s %d - READ %3d pos %3d state %2d first 16: [%02x %02x %02x %02x %02x %02x %02x %02x]",
  2582. compac->cgminer_id, compac->drv->name, compac->device_id, read_bytes, pos, info->mining_state,
  2583. rx[0], rx[1], rx[2], rx[3], rx[4], rx[5], rx[6], rx[7]);
  2584. applog(LOG_ERR, " [%02x %02x %02x %02x %02x %02x %02x %02x]",
  2585. rx[8], rx[9], rx[10], rx[11], rx[12], rx[13], rx[14], rx[15]);
  2586. #endif
  2587. // rubbish - skip over it to next 0xaa
  2588. if (rx[0] != 0xaa || rx[1] != 0x55)
  2589. {
  2590. for (i = 1; i < pos; i++)
  2591. {
  2592. if (rx[i] == 0xaa)
  2593. {
  2594. // next read could be 0x55 or i+1=0x55
  2595. if (i == (pos - 1) || rx[i+1] == 0x55)
  2596. break;
  2597. }
  2598. }
  2599. // no 0xaa dump it and wait for more data
  2600. if (i >= pos)
  2601. {
  2602. #if 0
  2603. applog(LOG_ERR, " %s %d no 0xaa = dump all (%d) [%02x %02x %02x %02x ...]",
  2604. compac->drv->name, compac->device_id, pos, rx[0], rx[1], rx[2], rx[3]);
  2605. #endif
  2606. pos = 0;
  2607. continue;
  2608. }
  2609. #if 0
  2610. applog(LOG_ERR, " %s %d dump before %d=0xaa [%02x %02x %02x %02x ...]",
  2611. compac->drv->name, compac->device_id, i, rx[0], rx[1], rx[2], rx[3]);
  2612. #endif
  2613. // i=0xaa dump up to i-1
  2614. memmove(rx, rx+i, pos-i);
  2615. pos -= i;
  2616. if (pos < (int)(info->rx_len))
  2617. continue;
  2618. }
  2619. // find next 0xaa 0x55
  2620. for (len = info->rx_len; len < pos; len++)
  2621. {
  2622. if (rx[len] == 0xaa
  2623. && (len == (pos-1) || rx[len+1] == 0x55))
  2624. break;
  2625. }
  2626. prelen = len;
  2627. // a reply followed by only 0xaa but no 0x55 yet
  2628. if (len == pos && (len == 8 || len == 10) && rx[pos-1] == 0xaa)
  2629. len--;
  2630. // try it as a nonce
  2631. if (len != (int)(info->rx_len))
  2632. len = info->rx_len;
  2633. #if 0
  2634. if (info->asic_type == BM1397 &&
  2635. bmcrc(&rx[i+2], 8 * (info->rx_len-2) - 5) == (rx[i + info->rx_len - 1] & 0x1f)) {
  2636. // crc checksum is good
  2637. crc_match = true;
  2638. rx_okay = true;
  2639. }
  2640. if (info->asic_type == BM1397 && rx[0] >= 0xaa && rx[1] <= 0x55) {
  2641. // bm1397 response
  2642. rx_okay = true;
  2643. }
  2644. #endif
  2645. if (rx[len-1] <= 0x1f
  2646. && bmcrc(rx+2, 8 * (len-2) - 5) == rx[len-1])
  2647. okcrc = true;
  2648. else
  2649. okcrc = false;
  2650. switch (info->mining_state)
  2651. {
  2652. case MINER_CHIP_COUNT:
  2653. case MINER_CHIP_COUNT_XX:
  2654. // BM1397
  2655. chipped = false;
  2656. if (rx[2] == 0x13 && rx[3] == 0x97)
  2657. {
  2658. struct ASIC_INFO *asic = &info->asics[info->chips];
  2659. memset(asic, 0, sizeof(struct ASIC_INFO));
  2660. asic->frequency = info->frequency_default;
  2661. asic->frequency_attempt = 0;
  2662. asic->last_frequency_ping = (struct timeval){0};
  2663. asic->frequency_reply = -1;
  2664. asic->last_frequency_reply = (struct timeval){0};
  2665. cgtime(&asic->last_nonce);
  2666. info->chips++;
  2667. info->mining_state = MINER_CHIP_COUNT_XX;
  2668. compac_update_rates(compac);
  2669. chipped = true;
  2670. }
  2671. // ignore all data until we get at least 1 chip reply
  2672. if (!chipped && info->mining_state == MINER_CHIP_COUNT_XX)
  2673. {
  2674. // we found some chips then it replied with other data ...
  2675. if (info->chips > 0)
  2676. {
  2677. info->mining_state = MINER_CHIP_COUNT_OK;
  2678. mutex_lock(&static_lock);
  2679. (*init_count) = 0;
  2680. info->init_count = 0;
  2681. mutex_unlock(&static_lock);
  2682. // don't discard the data
  2683. if (len == (int)(info->rx_len) && okcrc)
  2684. gsf_reply(info, rx, info->rx_len, &now);
  2685. }
  2686. else
  2687. info->mining_state = MINER_RESET;
  2688. }
  2689. break;
  2690. case MINER_MINING:
  2691. used = false;
  2692. if (len == (int)(info->rx_len) && okcrc)
  2693. {
  2694. used = gsf_reply(info, rx, info->rx_len, &now);
  2695. #if 0
  2696. if (!used)
  2697. {
  2698. applog(LOG_ERR, "%d: %s %d - ? len %3d state %2d first 12:",
  2699. compac->cgminer_id, compac->drv->name, compac->device_id, len, info->mining_state);
  2700. applog(LOG_ERR, " [%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x]",
  2701. rx[0], rx[1], rx[2], rx[3], rx[4], rx[5], rx[6], rx[7], rx[8], rx[9], rx[10], rx[11]);
  2702. }
  2703. #endif
  2704. }
  2705. // also try unidentifed crc's as a nonce
  2706. if (!used)
  2707. {
  2708. K_WLOCK(info->nlist);
  2709. item = k_unlink_head(info->nlist);
  2710. K_WUNLOCK(info->nlist);
  2711. DATA_NONCE(item)->asic = 0;
  2712. // should never be true ...
  2713. if (len > (int)sizeof(DATA_NONCE(item)->rx))
  2714. len = (int)sizeof(DATA_NONCE(item)->rx);
  2715. memcpy(DATA_NONCE(item)->rx, rx, len);
  2716. DATA_NONCE(item)->len = len;
  2717. DATA_NONCE(item)->prelen = prelen;
  2718. DATA_NONCE(item)->when.tv_sec = now.tv_sec;
  2719. DATA_NONCE(item)->when.tv_usec = now.tv_usec;
  2720. K_WLOCK(info->nlist);
  2721. k_add_tail(info->nstore, item);
  2722. K_WUNLOCK(info->nlist);
  2723. mutex_lock(&info->nlock);
  2724. pthread_cond_signal(&info->ncond);
  2725. mutex_unlock(&info->nlock);
  2726. }
  2727. break;
  2728. default:
  2729. used = false;
  2730. if (len == (int)(info->rx_len) && okcrc)
  2731. used = gsf_reply(info, rx, info->rx_len, &now);
  2732. #if 0
  2733. if (!used)
  2734. {
  2735. applog(LOG_ERR, "%d: %s %d - unhandled, len %3d state %2d first 12:",
  2736. compac->cgminer_id, compac->drv->name, compac->device_id, len, info->mining_state);
  2737. applog(LOG_ERR, " [%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x]",
  2738. rx[0], rx[1], rx[2], rx[3], rx[4], rx[5], rx[6], rx[7], rx[8], rx[9], rx[10], rx[11]);
  2739. }
  2740. #endif
  2741. break;
  2742. }
  2743. // we've used up 0..len-1
  2744. if (pos > len)
  2745. memmove(rx, rx+len, pos-len);
  2746. pos -= len;
  2747. }
  2748. if (read_bytes == 0 || pos < 6)
  2749. {
  2750. if (info->mining_state == MINER_CHIP_COUNT_XX)
  2751. {
  2752. if (info->chips < info->expected_chips)
  2753. info->mining_state = MINER_RESET;
  2754. else
  2755. {
  2756. if (info->chips > 0)
  2757. {
  2758. info->mining_state = MINER_CHIP_COUNT_OK;
  2759. mutex_lock(&static_lock);
  2760. (*init_count) = 0;
  2761. info->init_count = 0;
  2762. mutex_unlock(&static_lock);
  2763. }
  2764. else
  2765. info->mining_state = MINER_RESET;
  2766. }
  2767. }
  2768. }
  2769. }
  2770. return NULL;
  2771. }
  2772. static void *compac_listen(void *object)
  2773. {
  2774. struct cgpu_info *compac = (struct cgpu_info *)object;
  2775. struct COMPAC_INFO *info = compac->device_data;
  2776. if (info->asic_type == BM1397)
  2777. return compac_listen2(compac, info);
  2778. struct timeval now;
  2779. unsigned char rx[BUFFER_MAX];
  2780. unsigned char *prx = rx;
  2781. int read_bytes, cmd_resp, pos;
  2782. unsigned int i, rx_bytes;
  2783. memset(rx, 0, sizeof(rx));
  2784. memset(info->rx, 0, sizeof(info->rx));
  2785. pos = 0;
  2786. rx_bytes = 0;
  2787. while (info->mining_state != MINER_SHUTDOWN) {
  2788. cgtime(&now);
  2789. if (info->mining_state == MINER_CHIP_COUNT) {
  2790. if (info->asic_type == BM1387) {
  2791. unsigned char buffer[] = {0x54, 0x05, 0x00, 0x00, 0x00};
  2792. compac_send(compac, buffer, sizeof(buffer), 8 * sizeof(buffer) - 8);
  2793. } else if (info->asic_type == BM1384) {
  2794. unsigned char buffer[] = {0x84, 0x00, 0x00, 0x00};
  2795. compac_send(compac, buffer, sizeof(buffer), 8 * sizeof(buffer) - 5);
  2796. }
  2797. usb_read_timeout(compac, (char *)rx, BUFFER_MAX, &read_bytes, 1000, C_GETRESULTS);
  2798. dumpbuffer(compac, LOG_INFO, "CMD.RX", rx, read_bytes);
  2799. rx_bytes = (unsigned int)read_bytes;
  2800. info->mining_state = MINER_CHIP_COUNT_XX;
  2801. } else {
  2802. if (rx_bytes >= (BUFFER_MAX - info->rx_len)) {
  2803. applog(LOG_INFO, "%d: %s %d - Buffer not useful, dumping (b) %d bytes", compac->cgminer_id, compac->drv->name, compac->device_id, rx_bytes);
  2804. dumpbuffer(compac, LOG_INFO, "NO-CRC-RX", rx, rx_bytes);
  2805. pos = 0;
  2806. rx_bytes = 0;
  2807. }
  2808. usb_read_timeout(compac, (char *)(&rx[pos]), info->rx_len, &read_bytes, 20, C_GETRESULTS);
  2809. rx_bytes += read_bytes;
  2810. pos = rx_bytes;
  2811. }
  2812. if (read_bytes > 0) {
  2813. if (rx_bytes < info->rx_len) {
  2814. applog(LOG_INFO, "%d: %s %d - Buffered %d bytes", compac->cgminer_id, compac->drv->name, compac->device_id, rx_bytes);
  2815. dumpbuffer(compac, LOG_INFO, "Partial-RX", rx, rx_bytes);
  2816. continue;
  2817. }
  2818. if (rx_bytes >= info->rx_len)
  2819. cmd_resp = (rx[read_bytes - 1] <= 0x1f && bmcrc(prx, 8 * read_bytes - 5) == rx[read_bytes - 1]) ? 1 : 0;
  2820. if (info->mining_state == MINER_CHIP_COUNT_XX || cmd_resp) {
  2821. if (rx_bytes % info->rx_len == 2) {
  2822. // fix up trial 1
  2823. int shift = 0;
  2824. int extra = 0;
  2825. for (i = 0; i < (rx_bytes - shift); i++) {
  2826. if (rx[i] == 0x01) {
  2827. shift = 2;
  2828. extra = rx[i + 1];
  2829. }
  2830. rx[i] = rx[i + shift];
  2831. }
  2832. rx_bytes -= shift;
  2833. pos = rx_bytes;
  2834. applog(LOG_INFO, "%d: %s %d - Extra Data - 0x01 0x%02x", compac->cgminer_id, compac->drv->name, compac->device_id, extra & 0xff);
  2835. }
  2836. }
  2837. if (rx_bytes >= info->rx_len) {
  2838. bool crc_match = false;
  2839. unsigned int new_pos = 0;
  2840. for (i = 0; i <= (rx_bytes - info->rx_len); i++) {
  2841. bool rx_okay = false;
  2842. if (bmcrc(&rx[i], 8 * info->rx_len - 5) == (rx[i + info->rx_len - 1] & 0x1f)) {
  2843. // crc checksum is good
  2844. crc_match = true;
  2845. rx_okay = true;
  2846. }
  2847. if (info->asic_type == BM1384 && rx[i + info->rx_len - 1] >= 0x80 && rx[i + info->rx_len - 1] <= 0x9f) {
  2848. // bm1384 nonce
  2849. rx_okay = true;
  2850. }
  2851. if (rx_okay) {
  2852. memcpy(info->rx, &rx[i], info->rx_len);
  2853. compac_handle_rx(compac, info->rx_len, 0);
  2854. new_pos = i + info->rx_len;
  2855. }
  2856. }
  2857. if (new_pos > rx_bytes) {
  2858. rx_bytes = 0;
  2859. pos = 0;
  2860. } else if (new_pos > 0) {
  2861. for (i = new_pos; i < rx_bytes; i++) {
  2862. rx[i - new_pos] = rx[i];
  2863. }
  2864. rx_bytes -= new_pos;
  2865. pos = rx_bytes;
  2866. }
  2867. }
  2868. /*
  2869. if (bmcrc(&rx[rx_bytes - info->rx_len], 8 * info->rx_len - 5) != info->rx[rx_bytes - 1]) {
  2870. int n_read_bytes;
  2871. pos = rx_bytes;
  2872. usb_read_timeout(compac, &rx[pos], BUFFER_MAX - pos, &n_read_bytes, 5, C_GETRESULTS);
  2873. rx_bytes += n_read_bytes;
  2874. if (rx_bytes % info->rx_len != 0 && rx_bytes >= info->rx_len) {
  2875. int extra_bytes = rx_bytes % info->rx_len;
  2876. for (i = extra_bytes; i < rx_bytes; i++) {
  2877. rx[i - extra_bytes] = rx[i];
  2878. }
  2879. rx_bytes -= extra_bytes;
  2880. applog(LOG_INFO, "%d: %s %d - Fixing buffer alignment, dumping initial %d bytes", compac->cgminer_id, compac->drv->name, compac->device_id, extra_bytes);
  2881. }
  2882. }
  2883. if (rx_bytes % info->rx_len == 0) {
  2884. for (i = 0; i < rx_bytes; i += info->rx_len) {
  2885. memcpy(info->rx, &rx[i], info->rx_len);
  2886. compac_handle_rx(compac, info->rx_len, 1);
  2887. }
  2888. pos = 0;
  2889. rx_bytes = 0;
  2890. }
  2891. */
  2892. } else {
  2893. if (rx_bytes > 0) {
  2894. applog(LOG_INFO, "%d: %s %d - Second read, no data dumping (c) %d bytes", compac->cgminer_id, compac->drv->name, compac->device_id, rx_bytes);
  2895. dumpbuffer(compac, LOG_INFO, "EXTRA-RX", rx, rx_bytes);
  2896. }
  2897. pos = 0;
  2898. rx_bytes = 0;
  2899. // RX line is idle, let's squeeze in a command to the micro if needed.
  2900. if (info->asic_type == BM1387) {
  2901. if (ms_tdiff(&now, &info->last_micro_ping) > MS_SECOND_5 && ms_tdiff(&now, &info->last_task) > 1 && ms_tdiff(&now, &info->last_task) < 3) {
  2902. compac_micro_send(compac, M1_GET_TEMP, 0x00, 0x00);
  2903. cgtime(&info->last_micro_ping);
  2904. }
  2905. }
  2906. switch (info->mining_state) {
  2907. case MINER_CHIP_COUNT_XX:
  2908. if (info->chips < info->expected_chips) {
  2909. applog(LOG_INFO, "%d: %s %d - found %d/%d chip(s)", compac->cgminer_id, compac->drv->name, compac->device_id, info->chips, info->expected_chips);
  2910. info->mining_state = MINER_RESET;
  2911. } else {
  2912. applog(LOG_INFO, "%d: %s %d - found %d chip(s)", compac->cgminer_id, compac->drv->name, compac->device_id, info->chips);
  2913. if (info->chips > 0) {
  2914. info->mining_state = MINER_CHIP_COUNT_OK;
  2915. mutex_lock(&static_lock);
  2916. (*init_count) = 0;
  2917. info->init_count = 0;
  2918. mutex_unlock(&static_lock);
  2919. } else {
  2920. info->mining_state = MINER_RESET;
  2921. }
  2922. }
  2923. break;
  2924. default:
  2925. break;
  2926. }
  2927. }
  2928. }
  2929. return NULL;
  2930. }
  2931. static bool compac_init(struct thr_info *thr)
  2932. {
  2933. int i;
  2934. struct cgpu_info *compac = thr->cgpu;
  2935. struct COMPAC_INFO *info = compac->device_data;
  2936. float step_freq;
  2937. // all are currently this value
  2938. info->freq_mult = 25.0;
  2939. // most are this value (6.25)
  2940. info->freq_base = info->freq_mult / 4.0;
  2941. // correct some options
  2942. if (opt_gekko_tune_down > 100)
  2943. opt_gekko_tune_down = 100;
  2944. if (opt_gekko_tune_up > 99)
  2945. opt_gekko_tune_up = 99;
  2946. opt_gekko_wait_factor = fbound(opt_gekko_wait_factor, 0.01, 2.0);
  2947. info->wait_factor0 = opt_gekko_wait_factor;
  2948. if (opt_gekko_start_freq < 25)
  2949. opt_gekko_start_freq = 25;
  2950. if (opt_gekko_step_freq < 1)
  2951. opt_gekko_step_freq = 1;
  2952. if (opt_gekko_step_delay < 1)
  2953. opt_gekko_step_delay = 1;
  2954. // must limit it to allow tune downs before trying again
  2955. if (opt_gekko_tune2 < 30 && opt_gekko_tune2 != 0)
  2956. opt_gekko_tune2 = 30;
  2957. info->boosted = false;
  2958. info->prev_nonce = 0;
  2959. info->fail_count = 0;
  2960. info->busy_work = 0;
  2961. info->log_wide = (opt_widescreen) ? LOG_WARNING : LOG_INFO;
  2962. info->plateau_reset = 0;
  2963. info->low_eff_resets = 0;
  2964. info->frequency_fail_high = 0;
  2965. info->frequency_fail_low = 999;
  2966. info->frequency_fo = info->chips;
  2967. info->first_task.tv_sec = 0L;
  2968. info->first_task.tv_usec = 0L;
  2969. info->tasks = 0;
  2970. info->tune_limit.tv_sec = 0L;
  2971. info->tune_limit.tv_usec = 0L;
  2972. info->last_tune_up.tv_sec = 0L;
  2973. info->last_tune_up.tv_usec = 0L;
  2974. memset(info->rx, 0, sizeof(info->rx));
  2975. memset(info->tx, 0, sizeof(info->tx));
  2976. memset(info->cmd, 0, sizeof(info->cmd));
  2977. memset(info->end, 0, sizeof(info->end));
  2978. memset(info->task, 0, sizeof(info->task));
  2979. for (i = 0; i < JOB_MAX; i++) {
  2980. info->active_work[i] = false;
  2981. info->work[i] = NULL;
  2982. }
  2983. memset(&(info->gh), 0, sizeof(info->gh));
  2984. cgtime(&info->last_write_error);
  2985. cgtime(&info->last_frequency_adjust);
  2986. cgtime(&info->last_computed_increase);
  2987. cgtime(&info->last_low_eff_reset);
  2988. info->last_frequency_invalid = (struct timeval){0};
  2989. cgtime(&info->last_micro_ping);
  2990. cgtime(&info->last_scanhash);
  2991. cgtime(&info->last_reset);
  2992. cgtime(&info->last_task);
  2993. cgtime(&info->start_time);
  2994. cgtime(&info->monitor_time);
  2995. info->step_freq = FREQ_BASE(opt_gekko_step_freq);
  2996. // if it can't mine at this freq the hardware has failed
  2997. info->min_freq = info->freq_mult;
  2998. // most should only take this long
  2999. info->ramp_time = MS_MINUTE_3;
  3000. info->ghrequire = GHREQUIRE;
  3001. info->freq_fail = 0.0;
  3002. info->hr_scale = 1.0;
  3003. info->usb_prop = 1000;
  3004. switch (info->ident)
  3005. {
  3006. case IDENT_BSC:
  3007. case IDENT_GSC:
  3008. info->frequency_requested = limit_freq(info, opt_gekko_gsc_freq, false);
  3009. info->frequency_start = limit_freq(info, opt_gekko_start_freq, false);
  3010. break;
  3011. case IDENT_BSD:
  3012. case IDENT_GSD:
  3013. info->frequency_requested = limit_freq(info, opt_gekko_gsd_freq, false);
  3014. info->frequency_start = limit_freq(info, opt_gekko_start_freq, false);
  3015. break;
  3016. case IDENT_BSE:
  3017. case IDENT_GSE:
  3018. info->frequency_requested = limit_freq(info, opt_gekko_gse_freq, false);
  3019. info->frequency_start = limit_freq(info, opt_gekko_start_freq, false);
  3020. break;
  3021. case IDENT_GSH:
  3022. info->frequency_requested = limit_freq(info, opt_gekko_gsh_freq, false);
  3023. info->frequency_start = limit_freq(info, opt_gekko_start_freq, false);
  3024. break;
  3025. case IDENT_BAX:
  3026. info->frequency_requested = limit_freq(info, opt_gekko_bax_freq, false);
  3027. info->frequency_start = limit_freq(info, opt_gekko_start_freq, false);
  3028. break;
  3029. case IDENT_GSI:
  3030. info->frequency_requested = limit_freq(info, opt_gekko_gsi_freq, false);
  3031. info->frequency_start = limit_freq(info, opt_gekko_start_freq, false);
  3032. // default to 550
  3033. if (info->frequency_start == 100)
  3034. info->frequency_start = 550;
  3035. if (info->frequency_start < 100)
  3036. info->frequency_start = 100;
  3037. // due to higher freq allow longer
  3038. info->ramp_time = MS_MINUTE_4;
  3039. break;
  3040. case IDENT_GSF:
  3041. case IDENT_GSFM:
  3042. if (info->ident == IDENT_GSF)
  3043. info->frequency_requested = limit_freq(info, opt_gekko_gsf_freq, false);
  3044. else
  3045. info->frequency_requested = limit_freq(info, opt_gekko_r909_freq, false);
  3046. info->frequency_start = limit_freq(info, opt_gekko_start_freq, false);
  3047. if (info->frequency_start < 100)
  3048. info->frequency_start = 100;
  3049. if (info->frequency_start == 100)
  3050. {
  3051. if (info->ident == IDENT_GSF)
  3052. {
  3053. // default to 200
  3054. info->frequency_start = 200;
  3055. }
  3056. else // (info->ident == IDENT_GSFM)
  3057. {
  3058. // default to 400
  3059. info->frequency_start = 400;
  3060. }
  3061. }
  3062. // ensure request is >= start
  3063. if (info->frequency_requested < info->frequency_start)
  3064. info->frequency_requested = info->frequency_start;
  3065. // correct the defaults:
  3066. info->freq_base = info->freq_mult / 5.0;
  3067. step_freq = opt_gekko_step_freq;
  3068. if (step_freq == 6.25)
  3069. step_freq = 5.0;
  3070. info->step_freq = FREQ_BASE(step_freq);
  3071. // IDENT_GSFM runs at the more reliable higher frequencies
  3072. if (info->ident == IDENT_GSF)
  3073. {
  3074. // chips can get lower than the calculated 67.2 at lower freq
  3075. info->hr_scale = 52.5 / 67.2;
  3076. }
  3077. // due to ticket mask allow longer
  3078. info->ramp_time = MS_MINUTE_5;
  3079. break;
  3080. default:
  3081. info->frequency_requested = 200;
  3082. info->frequency_start = info->frequency_requested;
  3083. break;
  3084. }
  3085. if (info->frequency_start > info->frequency_requested) {
  3086. info->frequency_start = info->frequency_requested;
  3087. }
  3088. info->frequency_requested = FREQ_BASE(info->frequency_requested);
  3089. info->frequency_selected = info->frequency_requested;
  3090. info->frequency_start = FREQ_BASE(info->frequency_start);
  3091. info->frequency = info->frequency_start;
  3092. info->frequency_default = info->frequency_start;
  3093. if (!info->rthr.pth) {
  3094. pthread_mutex_init(&info->lock, NULL);
  3095. pthread_mutex_init(&info->wlock, NULL);
  3096. pthread_mutex_init(&info->rlock, NULL);
  3097. pthread_mutex_init(&info->ghlock, NULL);
  3098. pthread_mutex_init(&info->joblock, NULL);
  3099. if (info->ident == IDENT_GSF || info->ident == IDENT_GSFM)
  3100. {
  3101. info->nlist = k_new_list("GekkoNonces", sizeof(struct COMPAC_NONCE),
  3102. ALLOC_NLIST_ITEMS, LIMIT_NLIST_ITEMS, true);
  3103. info->nstore = k_new_store(info->nlist);
  3104. }
  3105. if (thr_info_create(&(info->rthr), NULL, compac_listen, (void *)compac)) {
  3106. applog(LOG_ERR, "%d: %s %d - read thread create failed", compac->cgminer_id, compac->drv->name, compac->device_id);
  3107. return false;
  3108. } else {
  3109. applog(LOG_INFO, "%d: %s %d - read thread created", compac->cgminer_id, compac->drv->name, compac->device_id);
  3110. }
  3111. pthread_detach(info->rthr.pth);
  3112. gekko_usleep(info, MS2US(100));
  3113. if (thr_info_create(&(info->wthr), NULL, compac_mine2, (void *)compac)) {
  3114. applog(LOG_ERR, "%d: %s %d - write thread create failed", compac->cgminer_id, compac->drv->name, compac->device_id);
  3115. return false;
  3116. } else {
  3117. applog(LOG_INFO, "%d: %s %d - write thread created", compac->cgminer_id, compac->drv->name, compac->device_id);
  3118. }
  3119. pthread_detach(info->wthr.pth);
  3120. if (info->ident == IDENT_GSF || info->ident == IDENT_GSFM)
  3121. {
  3122. gekko_usleep(info, MS2US(10));
  3123. if (pthread_mutex_init(&info->nlock, NULL))
  3124. {
  3125. applog(LOG_ERR, "%d: %s %d - nonce mutex create failed",
  3126. compac->cgminer_id, compac->drv->name, compac->device_id);
  3127. return false;
  3128. }
  3129. if (pthread_cond_init(&info->ncond, NULL))
  3130. {
  3131. applog(LOG_ERR, "%d: %s %d - nonce cond create failed",
  3132. compac->cgminer_id, compac->drv->name, compac->device_id);
  3133. return false;
  3134. }
  3135. if (thr_info_create(&(info->nthr), NULL, compac_gsf_nonce_que, (void *)compac))
  3136. {
  3137. applog(LOG_ERR, "%d: %s %d - nonce thread create failed",
  3138. compac->cgminer_id, compac->drv->name, compac->device_id);
  3139. return false;
  3140. }
  3141. else
  3142. {
  3143. applog(LOG_INFO, "%d: %s %d - nonce thread created",
  3144. compac->cgminer_id, compac->drv->name, compac->device_id);
  3145. }
  3146. pthread_detach(info->nthr.pth);
  3147. }
  3148. }
  3149. return true;
  3150. }
  3151. static int64_t compac_scanwork(struct thr_info *thr)
  3152. {
  3153. struct cgpu_info *compac = thr->cgpu;
  3154. struct COMPAC_INFO *info = compac->device_data;
  3155. struct timeval now;
  3156. int read_bytes;
  3157. // uint64_t hashes = 0;
  3158. uint64_t xhashes = 0;
  3159. if (info->chips == 0)
  3160. gekko_usleep(info, MS2US(10));
  3161. if (compac->usbinfo.nodev)
  3162. return -1;
  3163. #ifdef __APPLE__
  3164. if (opt_mac_yield)
  3165. sched_yield();
  3166. #else
  3167. selective_yield();
  3168. #endif
  3169. cgtime(&now);
  3170. switch (info->mining_state) {
  3171. case MINER_INIT:
  3172. gekko_usleep(info, MS2US(50));
  3173. compac_flush_buffer(compac);
  3174. info->chips = 0;
  3175. info->ramping = 0;
  3176. info->frequency_syncd = 1;
  3177. if (info->frequency_start > info->frequency_requested) {
  3178. info->frequency_start = info->frequency_requested;
  3179. }
  3180. info->mining_state = MINER_CHIP_COUNT;
  3181. return 0;
  3182. break;
  3183. case MINER_CHIP_COUNT:
  3184. if (ms_tdiff(&now, &info->last_reset) > MS_SECOND_5) {
  3185. applog(LOG_INFO, "%d: %s %d - found 0 chip(s)", compac->cgminer_id, compac->drv->name, compac->device_id);
  3186. info->mining_state = MINER_RESET;
  3187. return 0;
  3188. }
  3189. gekko_usleep(info, MS2US(10));
  3190. break;
  3191. case MINER_CHIP_COUNT_OK:
  3192. gekko_usleep(info, MS2US(50));
  3193. //compac_set_frequency(compac, info->frequency_start);
  3194. compac_send_chain_inactive(compac);
  3195. if (info->asic_type == BM1397)
  3196. info->mining_state = MINER_OPEN_CORE_OK;
  3197. return 0;
  3198. break;
  3199. case MINER_OPEN_CORE:
  3200. info->job_id = info->ramping % (info->max_job_id + 1);
  3201. //info->task_hcn = (0xffffffff / info->chips) * (1 + info->ramping) / info->cores;
  3202. init_task(info);
  3203. dumpbuffer(compac, LOG_DEBUG, "RAMP", info->task, info->task_len);
  3204. usb_write(compac, (char *)info->task, info->task_len, &read_bytes, C_SENDWORK);
  3205. if (info->ramping > (info->cores * info->add_job_id)) {
  3206. //info->job_id = 0;
  3207. info->mining_state = MINER_OPEN_CORE_OK;
  3208. info->task_hcn = (0xffffffff / info->chips);
  3209. return 0;
  3210. }
  3211. info->ramping += info->add_job_id;
  3212. info->task_ms = (info->task_ms * 9 + ms_tdiff(&now, &info->last_task)) / 10;
  3213. cgtime(&info->last_task);
  3214. gekko_usleep(info, MS2US(10));
  3215. return 0;
  3216. break;
  3217. case MINER_OPEN_CORE_OK:
  3218. applog(LOG_INFO, "%d: %s %d - start work", compac->cgminer_id, compac->drv->name, compac->device_id);
  3219. if (info->asic_type == BM1397)
  3220. gsf_calc_nb2c(compac);
  3221. cgtime(&info->start_time);
  3222. cgtime(&info->monitor_time);
  3223. cgtime(&info->last_frequency_adjust);
  3224. info->last_dup_time = (struct timeval){0};
  3225. cgtime(&info->last_frequency_report);
  3226. cgtime(&info->last_micro_ping);
  3227. cgtime(&info->last_nonce);
  3228. compac_flush_buffer(compac);
  3229. compac_update_rates(compac);
  3230. info->update_work = 1;
  3231. info->mining_state = MINER_MINING;
  3232. return 0;
  3233. break;
  3234. case MINER_MINING:
  3235. break;
  3236. case MINER_RESET:
  3237. compac_flush_work(compac);
  3238. if (info->asic_type == BM1387 || info->asic_type == BM1397) {
  3239. compac_toggle_reset(compac);
  3240. } else if (info->asic_type == BM1384) {
  3241. compac_set_frequency(compac, info->frequency_default);
  3242. //compac_send_chain_inactive(compac);
  3243. }
  3244. compac_prepare(thr);
  3245. info->fail_count++;
  3246. info->dupsreset = 0;
  3247. info->mining_state = MINER_INIT;
  3248. cgtime(&info->last_reset);
  3249. // in case clock jumps back ...
  3250. cgtime(&info->tune_limit);
  3251. // wipe info->gh/asic->gc
  3252. gh_offset(info, &now, true, false);
  3253. // wipe info->job
  3254. job_offset(info, &now, true, false);
  3255. // reset P:
  3256. info->frequency_computed = 0;
  3257. return 0;
  3258. break;
  3259. case MINER_MINING_DUPS:
  3260. info->mining_state = MINER_MINING;
  3261. break;
  3262. default:
  3263. break;
  3264. }
  3265. mutex_lock(&info->lock);
  3266. // hashes = info->hashes;
  3267. xhashes = info->xhashes;
  3268. info->hashes = 0;
  3269. info->xhashes = 0;
  3270. mutex_unlock(&info->lock);
  3271. gekko_usleep(info, MS2US(1));
  3272. return xhashes * 0xffffffffull;
  3273. //return hashes;
  3274. }
  3275. static struct cgpu_info *compac_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  3276. {
  3277. struct cgpu_info *compac;
  3278. struct COMPAC_INFO *info;
  3279. int i;
  3280. bool exclude_me = 0;
  3281. uint32_t baudrate = CP210X_DATA_BAUD;
  3282. unsigned int bits = CP210X_BITS_DATA_8 | CP210X_BITS_PARITY_MARK;
  3283. compac = usb_alloc_cgpu(&gekko_drv, 1);
  3284. if (!usb_init(compac, dev, found))
  3285. {
  3286. applog(LOG_INFO, "failed usb_init");
  3287. compac = usb_free_cgpu(compac);
  3288. return NULL;
  3289. }
  3290. // all zero
  3291. info = cgcalloc(1, sizeof(struct COMPAC_INFO));
  3292. #if TUNE_CODE
  3293. pthread_mutex_init(&info->slock, NULL);
  3294. #endif
  3295. compac->device_data = (void *)info;
  3296. info->ident = usb_ident(compac);
  3297. if (opt_gekko_gsc_detect || opt_gekko_gsd_detect || opt_gekko_gse_detect
  3298. || opt_gekko_gsh_detect || opt_gekko_bax_detect || opt_gekko_gsi_detect
  3299. || opt_gekko_gsf_detect || opt_gekko_r909_detect)
  3300. {
  3301. exclude_me = (info->ident == IDENT_BSC && !opt_gekko_gsc_detect);
  3302. exclude_me |= (info->ident == IDENT_GSC && !opt_gekko_gsc_detect);
  3303. exclude_me |= (info->ident == IDENT_BSD && !opt_gekko_gsd_detect);
  3304. exclude_me |= (info->ident == IDENT_GSD && !opt_gekko_gsd_detect);
  3305. exclude_me |= (info->ident == IDENT_BSE && !opt_gekko_gse_detect);
  3306. exclude_me |= (info->ident == IDENT_GSE && !opt_gekko_gse_detect);
  3307. exclude_me |= (info->ident == IDENT_GSH && !opt_gekko_gsh_detect);
  3308. exclude_me |= (info->ident == IDENT_BAX && !opt_gekko_gsh_detect);
  3309. exclude_me |= (info->ident == IDENT_GSI && !opt_gekko_gsi_detect);
  3310. exclude_me |= (info->ident == IDENT_GSF && !opt_gekko_gsf_detect);
  3311. exclude_me |= (info->ident == IDENT_GSFM && !opt_gekko_r909_detect);
  3312. }
  3313. if (opt_gekko_serial != NULL
  3314. && (strstr(opt_gekko_serial, compac->usbdev->serial_string) == NULL))
  3315. {
  3316. exclude_me = true;
  3317. }
  3318. if (exclude_me)
  3319. {
  3320. usb_uninit(compac);
  3321. free(info);
  3322. compac->device_data = NULL;
  3323. return NULL;
  3324. }
  3325. switch (info->ident)
  3326. {
  3327. case IDENT_BSC:
  3328. case IDENT_GSC:
  3329. case IDENT_BSD:
  3330. case IDENT_GSD:
  3331. case IDENT_BSE:
  3332. case IDENT_GSE:
  3333. info->asic_type = BM1384;
  3334. usb_transfer_data(compac, CP210X_TYPE_OUT, CP210X_REQUEST_IFC_ENABLE,
  3335. CP210X_VALUE_UART_ENABLE, info->interface, NULL, 0, C_ENABLE_UART);
  3336. usb_transfer_data(compac, CP210X_TYPE_OUT, CP210X_REQUEST_DATA,
  3337. CP210X_VALUE_DATA, info->interface, NULL, 0, C_SETDATA);
  3338. usb_transfer_data(compac, CP210X_TYPE_OUT, CP210X_REQUEST_BAUD,
  3339. 0, info->interface, &baudrate, sizeof (baudrate), C_SETBAUD);
  3340. usb_transfer_data(compac, CP210X_TYPE_OUT, CP210X_SET_LINE_CTL,
  3341. bits, info->interface, NULL, 0, C_SETPARITY);
  3342. break;
  3343. case IDENT_GSH:
  3344. info->asic_type = BM1387;
  3345. info->expected_chips = 2;
  3346. break;
  3347. case IDENT_BAX:
  3348. info->asic_type = BM1387;
  3349. info->expected_chips = 2;
  3350. break;
  3351. case IDENT_GSI:
  3352. info->asic_type = BM1387;
  3353. info->expected_chips = 12;
  3354. break;
  3355. case IDENT_GSF:
  3356. case IDENT_GSFM:
  3357. info->asic_type = BM1397;
  3358. // at least 1
  3359. info->expected_chips = 1;
  3360. break;
  3361. default:
  3362. quit(1, "%d: %s compac_detect_one() invalid %s ident=%d",
  3363. compac->cgminer_id, compac->drv->dname, compac->drv->dname, info->ident);
  3364. }
  3365. info->min_job_id = 0x10;
  3366. switch (info->asic_type)
  3367. {
  3368. case BM1384:
  3369. info->rx_len = 5;
  3370. info->task_len = 64;
  3371. info->cores = 55;
  3372. info->add_job_id = 1;
  3373. info->max_job_id = 0x1f;
  3374. info->midstates = 1;
  3375. info->can_boost = false;
  3376. break;
  3377. case BM1387:
  3378. info->rx_len = 7;
  3379. info->task_len = 54;
  3380. info->cores = 114;
  3381. info->add_job_id = 1;
  3382. info->max_job_id = 0x7f;
  3383. info->midstates = (opt_gekko_lowboost) ? 2 : 4;
  3384. info->can_boost = true;
  3385. compac_toggle_reset(compac);
  3386. break;
  3387. case BM1397:
  3388. info->rx_len = 9;
  3389. info->task_len = 54;
  3390. info->cores = 672;
  3391. info->add_job_id = 4;
  3392. info->max_job_id = 0x7f;
  3393. // ignore lowboost
  3394. info->midstates = 4;
  3395. info->can_boost = true;
  3396. compac_toggle_reset(compac);
  3397. break;
  3398. default:
  3399. break;
  3400. }
  3401. info->interface = usb_interface(compac);
  3402. info->mining_state = MINER_INIT;
  3403. applog(LOG_DEBUG, "Using interface %d", info->interface);
  3404. if (!add_cgpu(compac))
  3405. quit(1, "Failed to add_cgpu in compac_detect_one");
  3406. update_usb_stats(compac);
  3407. for (i = 0; i < 8; i++)
  3408. compac->unique_id[i] = compac->unique_id[i+3];
  3409. compac->unique_id[8] = 0;
  3410. info->wait_factor = info->wait_factor0;
  3411. if (!opt_gekko_noboost && info->vmask && (info->asic_type == BM1387 || info->asic_type == BM1397))
  3412. info->wait_factor *= info->midstates;
  3413. return compac;
  3414. }
  3415. static void compac_detect(bool __maybe_unused hotplug)
  3416. {
  3417. usb_detect(&gekko_drv, compac_detect_one);
  3418. }
  3419. static bool compac_prepare(struct thr_info *thr)
  3420. {
  3421. struct cgpu_info *compac = thr->cgpu;
  3422. struct COMPAC_INFO *info = compac->device_data;
  3423. int device = (compac->usbinfo.bus_number * 0xff + compac->usbinfo.device_address) % 0xffff;
  3424. mutex_lock(&static_lock);
  3425. init_count = &dev_init_count[device];
  3426. (*init_count)++;
  3427. info->init_count = (*init_count);
  3428. mutex_unlock(&static_lock);
  3429. if (info->init_count == 1) {
  3430. applog(LOG_WARNING, "%d: %s %d - %s (%s)",
  3431. compac->cgminer_id, compac->drv->name, compac->device_id,
  3432. compac->usbdev->prod_string, compac->unique_id);
  3433. } else {
  3434. applog(LOG_INFO, "%d: %s %d - init_count %d",
  3435. compac->cgminer_id, compac->drv->name, compac->device_id,
  3436. info->init_count);
  3437. }
  3438. info->thr = thr;
  3439. info->bauddiv = 0x19; // 115200
  3440. //info->bauddiv = 0x0D; // 214286
  3441. //info->bauddiv = 0x07; // 375000
  3442. //Sanity check and abort to prevent miner thread from being created.
  3443. if (info->asic_type == BM1387) {
  3444. // Ping Micro
  3445. info->micro_found = 0;
  3446. /*
  3447. if (info->asic_type == BM1387) {
  3448. info->vcore = bound(opt_gekko_gsh_vcore, 300, 810);
  3449. info->micro_found = 1;
  3450. if (!compac_micro_send(compac, M1_GET_TEMP, 0x00, 0x00)) {
  3451. info->micro_found = 0;
  3452. applog(LOG_INFO, "%d: %s %d - micro not found : dummy mode", compac->cgminer_id, compac->drv->name, compac->device_id);
  3453. } else {
  3454. uint8_t vcc = (info->vcore / 1000.0 - 0.3) / 0.002;
  3455. applog(LOG_INFO, "%d: %s %d - requesting vcore of %dmV (%x)", compac->cgminer_id, compac->drv->name, compac->device_id, info->vcore, vcc);
  3456. compac_micro_send(compac, M2_SET_VCORE, 0x00, vcc); // Default 400mV
  3457. }
  3458. }
  3459. */
  3460. }
  3461. if (info->init_count != 0 && info->init_count % 5 == 0) {
  3462. applog(LOG_INFO, "%d: %s %d - forcing usb_nodev()", compac->cgminer_id, compac->drv->name, compac->device_id);
  3463. usb_nodev(compac);
  3464. } else if (info->init_count > 1) {
  3465. if (info->init_count > 10) {
  3466. compac->deven = DEV_DISABLED;
  3467. } else {
  3468. cgsleep_ms(MS_SECOND_5);
  3469. }
  3470. }
  3471. return true;
  3472. }
  3473. static void compac_statline(char *buf, size_t bufsiz, struct cgpu_info *compac)
  3474. {
  3475. struct COMPAC_INFO *info = compac->device_data;
  3476. struct timeval now;
  3477. unsigned int i;
  3478. char ab[2];
  3479. char asic_stat[64];
  3480. char asic_statline[512];
  3481. char ms_stat[64];
  3482. char eff_stat[64];
  3483. uint32_t len = 0;
  3484. memset(asic_statline, 0, sizeof(asic_statline));
  3485. memset(asic_stat, 0, sizeof(asic_stat));
  3486. memset(ms_stat, 0, sizeof(ms_stat));
  3487. memset(eff_stat, 0, sizeof(eff_stat));
  3488. if (info->chips == 0) {
  3489. if (info->init_count > 1) {
  3490. sprintf(asic_statline, "found 0 chip(s)");
  3491. }
  3492. for (i = strlen(asic_statline); i < stat_len + 15; i++)
  3493. asic_statline[i] = ' ';
  3494. tailsprintf(buf, bufsiz, "%s", asic_statline);
  3495. return;
  3496. }
  3497. ab[0] = (info->boosted) ? '+' : 0;
  3498. ab[1] = 0;
  3499. if (info->chips > chip_max)
  3500. chip_max = info->chips;
  3501. cgtime(&now);
  3502. if (opt_widescreen) {
  3503. asic_stat[0] = '[';
  3504. for (i = 1; i <= info->chips; i++) {
  3505. struct ASIC_INFO *asic = &info->asics[i - 1];
  3506. switch (asic->state) {
  3507. case ASIC_HEALTHY:
  3508. asic_stat[i] = 'o';
  3509. break;
  3510. case ASIC_HALFDEAD:
  3511. asic_stat[i] = '-';
  3512. break;
  3513. case ASIC_ALMOST_DEAD:
  3514. asic_stat[i] = '!';
  3515. break;
  3516. case ASIC_DEAD:
  3517. asic_stat[i] = 'x';
  3518. break;
  3519. }
  3520. }
  3521. asic_stat[info->chips + 1] = ']';
  3522. for (i = 1; i <= (chip_max - info->chips) + 1; i++)
  3523. asic_stat[info->chips + 1 + i] = ' ';
  3524. }
  3525. sprintf(ms_stat, "(%.0f:%.0f)", info->task_ms, info->fullscan_ms);
  3526. uint8_t wuc = (ms_tdiff(&now, &info->last_wu_increase) > MS_MINUTE_1) ? 32 : 94;
  3527. if (info->eff_gs >= 99.9 && info->eff_wu >= 98.9) {
  3528. sprintf(eff_stat, "| 100%% WU:100%%");
  3529. } else if (info->eff_gs >= 99.9) {
  3530. sprintf(eff_stat, "| 100%% WU:%c%2.0f%%", wuc, info->eff_wu);
  3531. } else if (info->eff_wu >= 98.9) {
  3532. sprintf(eff_stat, "| %4.1f%% WU:100%%", info->eff_gs);
  3533. } else {
  3534. sprintf(eff_stat, "| %4.1f%% WU:%c%2.0f%%", info->eff_gs, wuc, info->eff_wu);
  3535. }
  3536. if (info->asic_type == BM1387 || info->asic_type == BM1397) {
  3537. char *chipnam = ((info->asic_type == BM1387) ? "BM1387" : "BM1397");
  3538. if (info->micro_found) {
  3539. sprintf(asic_statline, "%s:%02d%-1s %.2fMHz T:%.0f P:%.0f %s %.0fF",
  3540. chipnam, info->chips, ab, info->frequency, info->frequency_requested,
  3541. info->frequency_computed, ms_stat, info->micro_temp);
  3542. } else {
  3543. if (opt_widescreen) {
  3544. sprintf(asic_statline, "%s:%02d%-1s %.0f/%.0f/%3.0f %s %s",
  3545. chipnam, info->chips, ab, info->frequency, info->frequency_requested,
  3546. info->frequency_computed, ms_stat, asic_stat);
  3547. } else {
  3548. sprintf(asic_statline, "%s:%02d%-1s %.2fMHz T:%-3.0f P:%-3.0f %s %s",
  3549. chipnam, info->chips, ab, info->frequency, info->frequency_requested,
  3550. info->frequency_computed, ms_stat, asic_stat);
  3551. }
  3552. }
  3553. } else {
  3554. if (opt_widescreen) {
  3555. sprintf(asic_statline, "BM1384:%02d %.0f/%.0f/%.0f %s %s",
  3556. info->chips, info->frequency, info->frequency_requested,
  3557. info->frequency_computed, ms_stat, asic_stat);
  3558. } else {
  3559. sprintf(asic_statline, "BM1384:%02d %.2fMHz T:%-3.0f P:%-3.0f %s %s",
  3560. info->chips, info->frequency, info->frequency_requested,
  3561. info->frequency_computed, ms_stat, asic_stat);
  3562. }
  3563. }
  3564. len = strlen(asic_statline);
  3565. if (len > stat_len || opt_widescreen != last_widescreen) {
  3566. mutex_lock(&static_lock);
  3567. stat_len = len;
  3568. last_widescreen = opt_widescreen;
  3569. mutex_unlock(&static_lock);
  3570. }
  3571. for (i = len; i < stat_len; i++)
  3572. asic_statline[i] = ' ';
  3573. strcat(asic_statline, eff_stat);
  3574. asic_statline[63] = 0;
  3575. tailsprintf(buf, bufsiz, "%s", asic_statline);
  3576. }
  3577. static struct api_data *compac_api_stats(struct cgpu_info *compac)
  3578. {
  3579. struct COMPAC_INFO *info = compac->device_data;
  3580. struct api_data *root = NULL;
  3581. struct timeval now;
  3582. char nambuf[64], buf256[256];
  3583. double taskdiff, tps, ghs, off;
  3584. time_t secs;
  3585. size_t len;
  3586. int i, j, k;
  3587. cgtime(&now);
  3588. taskdiff = tdiff(&now, &(info->first_task));
  3589. if (taskdiff == 0 || info->tasks < 2)
  3590. tps = 0;
  3591. else
  3592. tps = (double)(info->tasks - 1) / taskdiff;
  3593. root = api_add_string(root, "Serial", compac->usbdev->serial_string, false);
  3594. root = api_add_int(root, "Nonces", &info->nonces, false);
  3595. root = api_add_int(root, "Accepted", &info->accepted, false);
  3596. root = api_add_double(root, "TasksPerSec", &tps, true);
  3597. root = api_add_uint64(root, "Tasks", &info->tasks, false);
  3598. root = api_add_uint64(root, "BusyWork", &info->busy_work, false);
  3599. root = api_add_int(root, "Midstates", &info->midstates, false);
  3600. root = api_add_uint64(root, "MaxTaskWait", &info->max_task_wait, false);
  3601. root = api_add_float(root, "WaitFactor0", &info->wait_factor0, false);
  3602. root = api_add_float(root, "WaitFactor", &info->wait_factor, false);
  3603. root = api_add_float(root, "FreqBase", &info->freq_base, false);
  3604. root = api_add_float(root, "FreqFail", &info->freq_fail, false);
  3605. root = api_add_uint32(root, "TicketDiff", &info->difficulty, false);
  3606. root = api_add_hex32(root, "TicketMask", &info->ticket_mask, false);
  3607. root = api_add_int64(root, "TicketNonces", &info->ticket_nonces, false);
  3608. root = api_add_int64(root, "TicketBelow", &info->below_nonces, false);
  3609. root = api_add_bool(root, "TicketOK", &info->ticket_ok, false);
  3610. root = api_add_float(root, "NonceExpect", &info->nonce_expect, false);
  3611. root = api_add_float(root, "NonceLimit", &info->nonce_limit, false);
  3612. // info->gh access must be under lock
  3613. mutex_lock(&info->ghlock);
  3614. ghs = gekko_gh_hashrate(info, &now, true) / 1.0e3;
  3615. secs = now.tv_sec - info->gh.zerosec;
  3616. root = api_add_time(root, "GHZeroDelta", &secs, true);
  3617. root = api_add_int(root, "GHLast", &info->gh.last, true);
  3618. root = api_add_int(root, "GHNonces", &info->gh.noncesum, true);
  3619. root = api_add_int64(root, "GHDiff", &info->gh.diffsum, true);
  3620. root = api_add_double(root, "GHGHs", &ghs, true);
  3621. mutex_unlock(&info->ghlock);
  3622. root = api_add_float(root, "Require", &info->ghrequire, true);
  3623. ghs = info->frequency * (double)(info->cores * info->chips) * info->ghrequire / 1.0e3;
  3624. root = api_add_double(root, "RequireGH", &ghs, true);
  3625. // info->job access must be under lock
  3626. // N.B. this is as at the last job sent, not 'now'
  3627. mutex_lock(&info->joblock);
  3628. off = tdiff(&now, &(info->job.lastjob));
  3629. root = api_add_double(root, "JobDataAge", &off, true);
  3630. buf256[0] = '\0';
  3631. for (i = 0; i < JOBMIN; i++)
  3632. {
  3633. j = JOBOFF(info->job.offset - i);
  3634. len = strlen(buf256);
  3635. // /, digit, null = 3
  3636. if ((len - sizeof(buf256)) < 3)
  3637. break;
  3638. snprintf(buf256+len, sizeof(buf256)-len, "/%d", info->job.jobnum[j]);
  3639. }
  3640. root = api_add_string(root, "Jobs", buf256+1, true);
  3641. buf256[0] = '\0';
  3642. for (i = 0; i < JOBMIN; i++)
  3643. {
  3644. double elap;
  3645. j = JOBOFF(info->job.offset - i);
  3646. elap = tdiff(&(info->job.lastj[j]), &(info->job.firstj[j]));
  3647. len = strlen(buf256);
  3648. // /, digit, null = 3
  3649. if ((len - sizeof(buf256)) < 3)
  3650. break;
  3651. snprintf(buf256+len, sizeof(buf256)-len, "/%.2f", elap);
  3652. }
  3653. root = api_add_string(root, "JobElapsed", buf256+1, true);
  3654. buf256[0] = '\0';
  3655. for (i = 0; i < JOBMIN; i++)
  3656. {
  3657. double jps, elap;
  3658. j = JOBOFF(info->job.offset - i);
  3659. elap = tdiff(&(info->job.lastj[j]), &(info->job.firstj[j]));
  3660. if (elap == 0)
  3661. jps = 0;
  3662. else
  3663. jps = (double)(info->job.jobnum[j] - 1) / elap;
  3664. len = strlen(buf256);
  3665. // /, digit, null = 3
  3666. if ((len - sizeof(buf256)) < 3)
  3667. break;
  3668. snprintf(buf256+len, sizeof(buf256)-len, "/%.2f", jps);
  3669. }
  3670. root = api_add_string(root, "JobsPerSec", buf256+1, true);
  3671. buf256[0] = '\0';
  3672. for (i = 0; i < JOBMIN; i++)
  3673. {
  3674. j = JOBOFF(info->job.offset - i);
  3675. len = strlen(buf256);
  3676. // /, digit, null = 3
  3677. if ((len - sizeof(buf256)) < 3)
  3678. break;
  3679. snprintf(buf256+len, sizeof(buf256)-len, "/%.2f", info->job.avgms[j]);
  3680. }
  3681. root = api_add_string(root, "JobsAvgms", buf256+1, true);
  3682. buf256[0] = '\0';
  3683. for (i = 0; i < JOBMIN; i++)
  3684. {
  3685. j = JOBOFF(info->job.offset - i);
  3686. len = strlen(buf256);
  3687. // /, digit, null = 3
  3688. if ((len - sizeof(buf256)) < 3)
  3689. break;
  3690. snprintf(buf256+len, sizeof(buf256)-len, "/%.2f:%.2f",
  3691. info->job.minms[j], info->job.maxms[j]);
  3692. }
  3693. root = api_add_string(root, "JobsMinMaxms", buf256+1, true);
  3694. mutex_unlock(&info->joblock);
  3695. for (i = 0; i < (int)CUR_ATTEMPT; i++)
  3696. {
  3697. snprintf(nambuf, sizeof(nambuf), "cur_off_%d_%d", i, cur_attempt[i]);
  3698. root = api_add_uint64(root, nambuf, &info->cur_off[i], true);
  3699. }
  3700. root = api_add_double(root, "Rolling", &info->rolling, false);
  3701. root = api_add_int(root, "Resets", &info->fail_count, false);
  3702. root = api_add_float(root, "Frequency", &info->frequency, false);
  3703. root = api_add_float(root, "FreqComp", &info->frequency_computed, false);
  3704. root = api_add_float(root, "FreqReq", &info->frequency_requested, false);
  3705. root = api_add_float(root, "FreqStart", &info->frequency_start, false);
  3706. root = api_add_float(root, "FreqSel", &info->frequency_selected, false);
  3707. //root = api_add_temp(root, "Temp", &info->micro_temp, false);
  3708. root = api_add_int(root, "Dups", &info->dupsall, true);
  3709. root = api_add_int(root, "DupsReset", &info->dupsreset, true);
  3710. root = api_add_uint(root, "Chips", &info->chips, false);
  3711. root = api_add_bool(root, "FreqLocked", &info->lock_freq, false);
  3712. if (info->asic_type == BM1397)
  3713. root = api_add_int(root, "USBProp", &info->usb_prop, false);
  3714. mutex_lock(&info->ghlock);
  3715. for (i = 0; i < (int)info->chips; i++)
  3716. {
  3717. struct ASIC_INFO *asic = &info->asics[i];
  3718. snprintf(nambuf, sizeof(nambuf), "Chip%dNonces", i);
  3719. root = api_add_int(root, nambuf, &asic->nonces, true);
  3720. snprintf(nambuf, sizeof(nambuf), "Chip%dDups", i);
  3721. root = api_add_uint(root, nambuf, &asic->dupsall, true);
  3722. gc_offset(info, asic, &now, false, true);
  3723. snprintf(nambuf, sizeof(nambuf), "Chip%dRanges", i);
  3724. buf256[0] = '\0';
  3725. for (j = 0; j < CHNUM; j++)
  3726. {
  3727. len = strlen(buf256);
  3728. // slash, digit, null = 3
  3729. if ((len - sizeof(buf256)) < 3)
  3730. break;
  3731. k = CHOFF(asic->gc.offset - j);
  3732. snprintf(buf256+len, sizeof(buf256)-len, "/%d", asic->gc.noncenum[k]);
  3733. }
  3734. len = strlen(buf256);
  3735. if ((len - sizeof(buf256)) >= 3)
  3736. snprintf(buf256+len, sizeof(buf256)-len, "/%d", asic->gc.noncesum);
  3737. len = strlen(buf256);
  3738. if ((len - sizeof(buf256)) >= 3)
  3739. snprintf(buf256+len, sizeof(buf256)-len, "/%.2f%%", noncepercent(info, i, &now));
  3740. root = api_add_string(root, nambuf, buf256+1, true);
  3741. snprintf(nambuf, sizeof(nambuf), "Chip%dFreqSend", i);
  3742. root = api_add_float(root, nambuf, &asic->frequency, true);
  3743. snprintf(nambuf, sizeof(nambuf), "Chip%dFreqReply", i);
  3744. root = api_add_float(root, nambuf, &asic->frequency_reply, true);
  3745. }
  3746. mutex_unlock(&info->ghlock);
  3747. for (i = 0; i < 16; i++)
  3748. {
  3749. snprintf(nambuf, sizeof(nambuf), "NonceByte-%1X0", i);
  3750. buf256[0] = '\0';
  3751. for (j = 0; j < 16; j++)
  3752. {
  3753. len = strlen(buf256);
  3754. // dot, digit, null = 3
  3755. if ((len - sizeof(buf256)) < 3)
  3756. break;
  3757. snprintf(buf256+len, sizeof(buf256)-len, ".%"PRId64, info->noncebyte[i*16+j]);
  3758. }
  3759. root = api_add_string(root, nambuf, buf256+1, true);
  3760. }
  3761. for (i = 0; i < 16; i++)
  3762. {
  3763. snprintf(nambuf, sizeof(nambuf), "nb2c-%1X0", i);
  3764. buf256[0] = '\0';
  3765. for (j = 0; j < 16; j++)
  3766. {
  3767. len = strlen(buf256);
  3768. // dot, digit, null = 3
  3769. if ((len - sizeof(buf256)) < 3)
  3770. break;
  3771. snprintf(buf256+len, sizeof(buf256)-len, ".%u", info->nb2chip[i*16+j]);
  3772. }
  3773. root = api_add_string(root, nambuf, buf256+1, true);
  3774. }
  3775. root = api_add_uint64(root, "NTimeout", &info->ntimeout, false);
  3776. root = api_add_uint64(root, "NTrigger", &info->ntrigger, false);
  3777. #if TUNE_CODE
  3778. mutex_lock(&info->slock);
  3779. uint64_t num = info->num;
  3780. double req = info->req;
  3781. double fac = info->fac;
  3782. uint64_t num1_1 = info->num1_1;
  3783. double req1_1 = info->req1_1;
  3784. double fac1_1 = info->fac1_1;
  3785. uint64_t num1_5 = info->num1_5;
  3786. double req1_5 = info->req1_5;
  3787. double fac1_5 = info->fac1_5;
  3788. uint64_t inv = info->inv;
  3789. mutex_unlock(&info->slock);
  3790. double avg, res, avg1_1, res1_1, avg1_5, res1_5;
  3791. if (num == 0)
  3792. avg = res = 0.0;
  3793. else
  3794. {
  3795. avg = req / (double)num;
  3796. res = fac / (double)num;
  3797. }
  3798. if (num1_1 == 0)
  3799. avg1_1 = res1_1 = 0.0;
  3800. else
  3801. {
  3802. avg1_1 = req1_1 / (double)num1_1;
  3803. res1_1 = fac1_1 / (double)num1_1;
  3804. }
  3805. if (num1_5 == 0)
  3806. avg1_5 = res1_5 = 0.0;
  3807. else
  3808. {
  3809. avg1_5 = req1_5 / (double)num1_5;
  3810. res1_5 = fac1_5 / (double)num1_5;
  3811. }
  3812. root = api_add_uint64(root, "SleepN", &num, true);
  3813. root = api_add_double(root, "SleepAvgReq", &avg, true);
  3814. root = api_add_double(root, "SleepAvgRes", &res, true);
  3815. root = api_add_uint64(root, "SleepN1_1", &num1_1, true);
  3816. root = api_add_double(root, "SleepAvgReq1_1", &avg1_1, true);
  3817. root = api_add_double(root, "SleepAvgRes1_1", &res1_1, true);
  3818. root = api_add_uint64(root, "SleepN1_5", &num1_5, true);
  3819. root = api_add_double(root, "SleepAvgReq1_5", &avg1_5, true);
  3820. root = api_add_double(root, "SleepAvgRes1_5", &res1_5, true);
  3821. root = api_add_uint64(root, "SleepInv", &inv, true);
  3822. root = api_add_uint64(root, "WorkGenNum", &info->work_usec_num, true);
  3823. root = api_add_double(root, "WorkGenAvg", &info->work_usec_avg, true);
  3824. if (info->over1num == 0)
  3825. avg = 0.0;
  3826. else
  3827. avg = info->over1amt / (double)(info->over1num);
  3828. root = api_add_int64(root, "Over1N", &info->over1num, true);
  3829. root = api_add_double(root, "Over1Avg", &avg, true);
  3830. if (info->over2num == 0)
  3831. avg = 0.0;
  3832. else
  3833. avg = info->over2amt / (double)(info->over2num);
  3834. root = api_add_int64(root, "Over2N", &info->over2num, true);
  3835. root = api_add_double(root, "Over2Avg", &avg, true);
  3836. #endif
  3837. #if STRATUM_WORK_TIMING
  3838. cg_rlock(&swt_lock);
  3839. uint64_t swc = stratum_work_count;
  3840. uint64_t swt = stratum_work_time;
  3841. uint64_t swmin = stratum_work_min;
  3842. uint64_t swmax = stratum_work_max;
  3843. uint64_t swt0 = stratum_work_time0;
  3844. uint64_t swt10 = stratum_work_time10;
  3845. uint64_t swt100 = stratum_work_time100;
  3846. cg_runlock(&swt_lock);
  3847. double sw_avg;
  3848. if (swc == 0)
  3849. sw_avg = 0.0;
  3850. else
  3851. sw_avg = (double)swt / (double)swc;
  3852. root = api_add_uint64(root, "SWCount", &swc, true);
  3853. root = api_add_double(root, "SWAvg", &sw_avg, true);
  3854. root = api_add_uint64(root, "SWMin", &swmin, true);
  3855. root = api_add_uint64(root, "SWMax", &swmax, true);
  3856. root = api_add_uint64(root, "SW0Count", &swt0, true);
  3857. root = api_add_uint64(root, "SW10Count", &swt10, true);
  3858. root = api_add_uint64(root, "SW100Count", &swt100, true);
  3859. #endif
  3860. return root;
  3861. }
  3862. static void compac_shutdown(struct thr_info *thr)
  3863. {
  3864. struct cgpu_info *compac = thr->cgpu;
  3865. struct COMPAC_INFO *info = compac->device_data;
  3866. applog(LOG_INFO, "%d: %s %d - shutting down", compac->cgminer_id, compac->drv->name, compac->device_id);
  3867. if (!compac->usbinfo.nodev) {
  3868. if (info->asic_type == BM1387) {
  3869. compac_micro_send(compac, M2_SET_VCORE, 0x00, 0x00); // 300mV
  3870. compac_toggle_reset(compac);
  3871. } else if (info->asic_type == BM1397) {
  3872. calc_gsf_freq(compac, 0, -1);
  3873. compac_toggle_reset(compac);
  3874. } else if (info->asic_type == BM1384 && info->frequency != info->frequency_default) {
  3875. float frequency = info->frequency - info->freq_mult;
  3876. while (frequency > info->frequency_default) {
  3877. compac_set_frequency(compac, frequency);
  3878. frequency -= info->freq_mult;
  3879. cgsleep_ms(100);
  3880. }
  3881. compac_set_frequency(compac, info->frequency_default);
  3882. compac_send_chain_inactive(compac);
  3883. }
  3884. }
  3885. info->mining_state = MINER_SHUTDOWN;
  3886. pthread_join(info->rthr.pth, NULL); // Let thread close.
  3887. pthread_join(info->wthr.pth, NULL); // Let thread close.
  3888. if (info->asic_type == BM1397)
  3889. pthread_join(info->nthr.pth, NULL); // Let thread close.
  3890. PTH(thr) = 0L;
  3891. }
  3892. uint64_t bound(uint64_t value, uint64_t lower_bound, uint64_t upper_bound)
  3893. {
  3894. if (value < lower_bound)
  3895. return lower_bound;
  3896. if (value > upper_bound)
  3897. return upper_bound;
  3898. return value;
  3899. }
  3900. void stuff_reverse(unsigned char *dst, unsigned char *src, uint32_t len)
  3901. {
  3902. uint32_t i;
  3903. for (i = 0; i < len; i++) {
  3904. dst[i] = src[len - i - 1];
  3905. }
  3906. }
  3907. void stuff_lsb(unsigned char *dst, uint32_t x)
  3908. {
  3909. dst[0] = (x >> 0) & 0xff;
  3910. dst[1] = (x >> 8) & 0xff;
  3911. dst[2] = (x >> 16) & 0xff;
  3912. dst[3] = (x >> 24) & 0xff;
  3913. }
  3914. void stuff_msb(unsigned char *dst, uint32_t x)
  3915. {
  3916. dst[0] = (x >> 24) & 0xff;
  3917. dst[1] = (x >> 16) & 0xff;
  3918. dst[2] = (x >> 8) & 0xff;
  3919. dst[3] = (x >> 0) & 0xff;
  3920. }
  3921. static char *compac_api_set(struct cgpu_info *compac, char *option, char *setting, char *replybuf, size_t siz)
  3922. {
  3923. struct COMPAC_INFO *info = compac->device_data;
  3924. float freq;
  3925. if (strcasecmp(option, "help") == 0)
  3926. {
  3927. // freq: all of the drivers automatically fix the value
  3928. // BM1397 0 is a special case, since it 'works'
  3929. if (info->asic_type == BM1397)
  3930. {
  3931. snprintf(replybuf, siz, "reset freq: 0-1200 chip: N:0-800 target: 0-1200"
  3932. " lockfreq unlockfreq waitfactor: 0.01-2.0"
  3933. " usbprop: %d-1000 require: 0.0-0.8", USLEEPMIN);
  3934. }
  3935. else
  3936. {
  3937. snprintf(replybuf, siz, "reset freq: 0-1200 chip: N:0-800 target: 0-1200"
  3938. " lockfreq unlockfreq waitfactor: 0.01-2.0"
  3939. " require: 0.0-0.8");
  3940. }
  3941. return replybuf;
  3942. }
  3943. if (strcasecmp(option, "reset") == 0)
  3944. {
  3945. // will cause various problems ...
  3946. info->mining_state = MINER_RESET;
  3947. return NULL;
  3948. }
  3949. // set all chips to freq
  3950. if (strcasecmp(option, "freq") == 0)
  3951. {
  3952. if (!setting || !*setting)
  3953. {
  3954. snprintf(replybuf, siz, "missing freq");
  3955. return replybuf;
  3956. }
  3957. freq = limit_freq(info, atof(setting), true);
  3958. freq = FREQ_BASE(freq);
  3959. change_freq_any(compac, freq);
  3960. return NULL;
  3961. }
  3962. // set chip:freq
  3963. if (strcasecmp(option, "chip") == 0)
  3964. {
  3965. char *fpos;
  3966. int chip;
  3967. if (!setting || !*setting)
  3968. {
  3969. snprintf(replybuf, siz, "missing chip:freq");
  3970. return replybuf;
  3971. }
  3972. if (info->asic_type != BM1397)
  3973. {
  3974. snprintf(replybuf, siz, "chip:freq only valid for BM1397");
  3975. return replybuf;
  3976. }
  3977. fpos = strchr(setting, ':');
  3978. if (!fpos || fpos == setting || *(fpos+1) == '\0')
  3979. {
  3980. snprintf(replybuf, siz, "not chip:freq");
  3981. return replybuf;
  3982. }
  3983. // atoi will stop at the ':'
  3984. chip = atoi(setting);
  3985. if (chip < 0 || chip >= (int)(info->chips))
  3986. {
  3987. snprintf(replybuf, siz, "invalid chip %d", chip);
  3988. return replybuf;
  3989. }
  3990. freq = limit_freq(info, atof(fpos+1), true);
  3991. freq = FREQ_BASE(freq);
  3992. calc_gsf_freq(compac, freq, chip);
  3993. return NULL;
  3994. }
  3995. if (strcasecmp(option, "target") == 0)
  3996. {
  3997. if (!setting || !*setting)
  3998. {
  3999. snprintf(replybuf, siz, "missing freq");
  4000. return replybuf;
  4001. }
  4002. freq = limit_freq(info, atof(setting), true);
  4003. freq = FREQ_BASE(freq);
  4004. info->frequency_requested = freq;
  4005. return NULL;
  4006. }
  4007. if (strcasecmp(option, "lockfreq") == 0)
  4008. {
  4009. info->lock_freq = true;
  4010. return NULL;
  4011. }
  4012. if (strcasecmp(option, "unlockfreq") == 0)
  4013. {
  4014. info->lock_freq = false;
  4015. return NULL;
  4016. }
  4017. // set wait-factor
  4018. if (strcasecmp(option, "waitfactor") == 0)
  4019. {
  4020. if (!setting || !*setting)
  4021. {
  4022. snprintf(replybuf, siz, "missing value");
  4023. return replybuf;
  4024. }
  4025. info->wait_factor0 = fbound(atof(setting), 0.01, 2.0);
  4026. compac_update_rates(compac);
  4027. return NULL;
  4028. }
  4029. // set work propagation time
  4030. if (strcasecmp(option, "usbprop") == 0)
  4031. {
  4032. if (info->asic_type != BM1397)
  4033. {
  4034. snprintf(replybuf, siz, "usbprop only for BM1397");
  4035. return replybuf;
  4036. }
  4037. if (!setting || !*setting)
  4038. {
  4039. snprintf(replybuf, siz, "missing usec");
  4040. return replybuf;
  4041. }
  4042. info->usb_prop = (int)bound(atoi(setting), USLEEPMIN, 1000);
  4043. return NULL;
  4044. }
  4045. // set ghrequire
  4046. if (strcasecmp(option, "require") == 0)
  4047. {
  4048. if (!setting || !*setting)
  4049. {
  4050. snprintf(replybuf, siz, "missing value");
  4051. return replybuf;
  4052. }
  4053. info->ghrequire = fbound(atof(setting), 0.0, 0.8);
  4054. compac_update_rates(compac);
  4055. return NULL;
  4056. }
  4057. snprintf(replybuf, siz, "Unknown option: %s", option);
  4058. return replybuf;
  4059. }
  4060. struct device_drv gekko_drv = {
  4061. .drv_id = DRIVER_gekko,
  4062. .dname = "GekkoScience",
  4063. .name = "GSX",
  4064. .hash_work = hash_queued_work,
  4065. .get_api_stats = compac_api_stats,
  4066. .get_statline_before = compac_statline,
  4067. .set_device = compac_api_set,
  4068. .drv_detect = compac_detect,
  4069. .scanwork = compac_scanwork,
  4070. .flush_work = compac_flush_work,
  4071. .update_work = compac_update_work,
  4072. .thread_prepare = compac_prepare,
  4073. .thread_init = compac_init,
  4074. .thread_shutdown = compac_shutdown,
  4075. };