driver-gekko.c 133 KB

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