bitmain-board-test.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310
  1. /*
  2. * Copyright 2016-2017 Fazio Bai <yang.bai@bitmain.com>
  3. * Copyright 2016-2017 Clement Duan <kai.duan@bitmain.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #include <assert.h>
  12. #include <limits.h>
  13. #include <pthread.h>
  14. #include <stdio.h>
  15. #include <sys/time.h>
  16. #include <sys/types.h>
  17. #include <dirent.h>
  18. #include <unistd.h>
  19. #include <sys/mman.h>
  20. #include <math.h>
  21. #ifndef WIN32
  22. #include <sys/select.h>
  23. #include <termios.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #ifndef O_CLOEXEC
  27. #define O_CLOEXEC 0
  28. #endif
  29. #else
  30. #include "compat.h"
  31. #include <windows.h>
  32. #include <io.h>
  33. #endif
  34. #include <sys/ioctl.h>
  35. #include <sys/socket.h>
  36. #include <netinet/in.h>
  37. #include <net/if.h>
  38. #include <netdb.h>
  39. #include <arpa/inet.h>
  40. #include <errno.h>
  41. #include <string.h>
  42. #include <zlib.h>
  43. #include "elist.h"
  44. #include "miner.h"
  45. #include "util.h"
  46. #include "driver-btm-soc.h"
  47. #include "bitmain-board-test.h"
  48. // below are defined in driver-btm-c5.c
  49. extern pthread_mutex_t iic_mutex;
  50. extern bool isChainAllCoresOpened[BITMAIN_MAX_CHAIN_NUM];
  51. extern bool someBoardUpVoltage;
  52. extern int lowest_testOK_temp[BITMAIN_MAX_CHAIN_NUM];
  53. extern int LOWEST_TEMP_DOWN_FAN;
  54. extern int chain_badcore_num[BITMAIN_MAX_CHAIN_NUM][256];
  55. extern pthread_mutex_t opencore_readtemp_mutex;
  56. extern unsigned int *axi_fpga_addr; // defined in driver-btm-c5.c
  57. extern unsigned int *fpga_mem_addr; //defined in driver-btm-c5.c
  58. extern int fd_fpga_mem; // fpga memory
  59. extern unsigned int *nonce2_jobid_address; // the value should be filled in NONCE2_AND_JOBID_STORE_ADDRESS
  60. extern void open_core_one_chain(int chainIndex, bool nullwork_enable);
  61. extern void insert_reg_data(unsigned int *buf);
  62. extern int GetTotalRate();
  63. extern int getVoltageLimitedFromHashrate(int hashrate_GHz);
  64. extern bool isChainEnough();
  65. extern void set_PWM(unsigned char pwm_percent);
  66. extern int get_nonce_number_in_fifo(void);
  67. extern int get_return_nonce(unsigned int *buf);
  68. extern int get_nonce_fifo_interrupt(void);
  69. extern void set_nonce_fifo_interrupt(unsigned int value);
  70. extern void set_TW_write_command(unsigned int *value);
  71. extern void set_TW_write_command_vil(unsigned int *value);
  72. extern int get_buffer_space(void);
  73. extern int get_freqvalue_by_index(int index);
  74. extern int getChainAsicFreqIndex(int chainIndex, int asicIndex);
  75. extern int get_hash_on_plug(void);
  76. ////////// below is only used inside of this file !!! so all static!/////////////
  77. static bool chain_need_opencore[BITMAIN_MAX_CHAIN_NUM]= {false};
  78. static bool StartSendFlag[BITMAIN_MAX_CHAIN_NUM];
  79. static int chain_DataCount[BITMAIN_MAX_CHAIN_NUM];
  80. static int chain_ValidNonce[BITMAIN_MAX_CHAIN_NUM];
  81. static int chain_PassCount[BITMAIN_MAX_CHAIN_NUM];
  82. static int chain_vol_value[BITMAIN_MAX_CHAIN_NUM]; // the searching vol
  83. static int chain_vol_final[BITMAIN_MAX_CHAIN_NUM]; // the final vol, need saved in PIC
  84. static int chain_vol_added[BITMAIN_MAX_CHAIN_NUM]; // how many vol added , recorded in PIC
  85. static int last_result[BITMAIN_MAX_CHAIN_NUM][256];
  86. static int last_result_opencore[BITMAIN_MAX_CHAIN_NUM][256];
  87. static int result = 0;
  88. static bool search_freq_result[BITMAIN_MAX_CHAIN_NUM]; // set true as default
  89. static struct testpatten_cgpu_info cgpu;
  90. static volatile bool gBegin_get_nonce = false;
  91. static unsigned int send_work_num[BITMAIN_MAX_CHAIN_NUM];
  92. static int asic_nonce_num[BITMAIN_MAX_CHAIN_NUM][256];
  93. static int asic_core_nonce_num[BITMAIN_MAX_CHAIN_NUM][256][256]; // 1st: which asic, 2nd: which core
  94. static int last_nonce_num[BITMAIN_MAX_CHAIN_NUM];
  95. static int repeated_nonce_num[BITMAIN_MAX_CHAIN_NUM];
  96. static uint32_t repeated_nonce_id[BITMAIN_MAX_CHAIN_NUM][256];
  97. static int valid_nonce_num[BITMAIN_MAX_CHAIN_NUM]; // all the received nonce in one test
  98. static int err_nonce_num[BITMAIN_MAX_CHAIN_NUM];
  99. static int total_valid_nonce_num=0;
  100. static volatile bool start_receive = false;
  101. static int testModeOKCounter[BITMAIN_MAX_CHAIN_NUM];
  102. static struct configuration Conf; //store information that read from Config.ini
  103. static struct _CONFIG conf; //store the information that handled from Config.ini
  104. static bool ExitFlag=false;
  105. static bool receiveExit;
  106. static bool sendExit[BITMAIN_MAX_CHAIN_NUM];
  107. static void writeLogFile(char *logstr);
  108. static int calculate_asic_number(unsigned int actual_asic_number);
  109. static int calculate_core_number(unsigned int actual_core_number);
  110. #define CONFIG_FILE "/etc/config/Config.ini"
  111. #define FORCE_FREQ_FILE "/etc/config/forcefreq.txt"
  112. #define LAST_FORCE_FREQ_FILE "/etc/config/last_forcefreq.txt"
  113. static bool last_all_pass(int chainIndex)
  114. {
  115. int i = 0;
  116. for(i=0; i<CHAIN_ASIC_NUM; i++)
  117. if (!last_result[chainIndex][i])
  118. return false;
  119. return true;
  120. }
  121. static bool last_all_core_opened(int chainIndex)
  122. {
  123. int i = 0;
  124. for(i=0; i<CHAIN_ASIC_NUM; i++)
  125. if (!last_result_opencore[chainIndex][i])
  126. return false;
  127. return true;
  128. }
  129. static bool isAllChainChipCoreOpened()
  130. {
  131. int i;
  132. FOR_LOOP_CHAIN
  133. {
  134. if(cgpu.chain_exist[i]==0)
  135. continue;
  136. if(!last_all_core_opened(i))
  137. {
  138. return false;
  139. }
  140. }
  141. return true;
  142. }
  143. static int load_testpatten_work(int id, int count)
  144. {
  145. struct testpatten_work * new_work;
  146. int subid = 0;
  147. unsigned long DataLen=MAX_WORK*48; // midstate + data + nonce = 48 bytes
  148. unsigned char *workData;
  149. unsigned char *zipData;
  150. unsigned long zipLen;
  151. workData=(unsigned char *)malloc(DataLen);
  152. fseek(cgpu.fps[id],0,SEEK_END);
  153. zipLen = ftell(cgpu.fps[id]);
  154. fseek(cgpu.fps[id],0,SEEK_SET);
  155. zipData=(unsigned char *)malloc(zipLen);
  156. zipLen=fread(zipData,1,zipLen,cgpu.fps[id]);
  157. uncompress(workData,&DataLen,zipData,zipLen);
  158. free(zipData);
  159. cgpu.works[id] = (struct testpatten_work *)malloc(count * sizeof(struct testpatten_work));
  160. if(NULL == cgpu.works[id])
  161. {
  162. applog(LOG_ERR, "malloc struct testpatten_work err\n");
  163. return 0;
  164. }
  165. while(subid*48<DataLen)
  166. {
  167. if(subid >= count)
  168. break;
  169. new_work = cgpu.works[id] + subid;
  170. memcpy((uint8_t *)(&new_work->nonce) ,workData+subid*48+44, 4);
  171. new_work->nonce = htonl(new_work->nonce);
  172. memcpy(new_work->midstate ,workData+subid*48, 32);
  173. memcpy(new_work->data ,workData+subid*48+32, 12);
  174. new_work->id = subid;
  175. subid++;
  176. }
  177. free(workData);
  178. return subid;
  179. }
  180. static int read_config()
  181. {
  182. FILE * file;
  183. int forceFreq,forceFlag;
  184. struct configuration *m_conf = &Conf;
  185. char str[1024] = {0};
  186. char * temp;
  187. int offset = 0, starttemp = 0;
  188. int i;
  189. file = fopen(CONFIG_FILE, "r");
  190. char logstr[1024];
  191. while(fgets(str, sizeof(str) - 1 , file))
  192. {
  193. if(str[0] == '#' || str[1] == '#')
  194. continue;
  195. if((temp = strstr(str, "TestDir="))!=NULL)
  196. {
  197. temp += 8;
  198. for(i = 0; i < 64; i++)
  199. {
  200. cgpu.workdataPathPrefix[i] = *temp++;
  201. //printf("%c", *temp);
  202. if(*temp == '\n' || *temp == '\r')
  203. break;
  204. }
  205. i++;
  206. cgpu.workdataPathPrefix[i] = '\0';
  207. printf("workdataPathPrefix:%s\n", cgpu.workdataPathPrefix);
  208. }
  209. else if((temp = strstr(str, "DataCount="))!=NULL)
  210. {
  211. temp += 10;
  212. sscanf(temp, "%d", &m_conf->DataCount);
  213. }
  214. else if((temp = strstr(str, "PassCount1="))!=NULL)
  215. {
  216. temp += 11;
  217. sscanf(temp, "%d", &m_conf->PassCount1);
  218. }
  219. else if((temp = strstr(str, "PassCount2="))!=NULL)
  220. {
  221. temp += 11;
  222. sscanf(temp, "%d", &m_conf->PassCount2);
  223. }
  224. else if((temp = strstr(str, "PassCount3="))!=NULL)
  225. {
  226. temp += 11;
  227. sscanf(temp, "%d", &m_conf->PassCount3);
  228. }
  229. else if((temp = strstr(str, "Freq="))!=NULL)
  230. {
  231. temp += 5;
  232. sscanf(temp, "%d", &m_conf->Freq);
  233. m_conf->force_freq=0;
  234. }
  235. else if((temp = strstr(str, "freq_e="))!=NULL)
  236. {
  237. temp += 7;
  238. sscanf(temp, "%d", &m_conf->freq_e);
  239. }
  240. else if((temp = strstr(str, "UseConfigVol="))!=NULL)
  241. {
  242. temp += 13;
  243. sscanf(temp, "%d", &m_conf->UseConfigVol);
  244. }
  245. else if((temp = strstr(str, "freq_m="))!=NULL)
  246. {
  247. temp += 7;
  248. sscanf(temp, "%d", &m_conf->freq_m);
  249. }
  250. else if((temp = strstr(str, "freq_a="))!=NULL)
  251. {
  252. temp += 7;
  253. sscanf(temp, "%d", &m_conf->freq_a);
  254. }
  255. else if((temp = strstr(str, "freq_t="))!=NULL)
  256. {
  257. temp += 7;
  258. sscanf(temp, "%d", &m_conf->freq_t);
  259. }
  260. else if((temp = strstr(str, "force_freq="))!=NULL)
  261. {
  262. temp += 11;
  263. // sscanf(temp, "%d", &m_conf->force_freq);
  264. }
  265. else if((temp = strstr(str, "Timeout="))!=NULL)
  266. {
  267. temp +=8;
  268. sscanf(temp, "%d", &m_conf->Timeout);
  269. }
  270. else if((temp = strstr(str, "UseFreqPIC="))!=NULL)
  271. {
  272. temp += 11;
  273. sscanf(temp , "%d", &m_conf->UseFreqPIC);
  274. }
  275. else if((temp = strstr(str, "TestMode="))!=NULL)
  276. {
  277. temp += 9;
  278. sscanf(temp, "%d", &m_conf->TestMode);
  279. }
  280. else if((temp = strstr(str, "CheckChain="))!=NULL)
  281. {
  282. temp += 11;
  283. sscanf(temp, "%d", &m_conf->CheckChain);
  284. }
  285. else if((temp = strstr(str, "CommandMode="))!=NULL)
  286. {
  287. temp += 12;
  288. sscanf(temp, "%d", &m_conf->CommandMode);
  289. }
  290. else if((temp = strstr(str, "ValidNonce1="))!=NULL)
  291. {
  292. temp += 12;
  293. sscanf(temp, "%d", &m_conf->ValidNonce1);
  294. }
  295. else if((temp = strstr(str, "ValidNonce2="))!=NULL)
  296. {
  297. temp += 12;
  298. sscanf(temp, "%d", &m_conf->ValidNonce2);
  299. }
  300. else if((temp = strstr(str, "ValidNonce3="))!=NULL)
  301. {
  302. temp += 12;
  303. sscanf(temp, "%d", &m_conf->ValidNonce3);
  304. }
  305. else if((temp = strstr(str, "Pic_VOLTAGE="))!=NULL)
  306. {
  307. temp += 12;
  308. sscanf(temp, "%d", &m_conf->Pic);
  309. }
  310. else if((temp = strstr(str, "Voltage1="))!=NULL)
  311. {
  312. temp += 9;
  313. sscanf(temp, "%d", &m_conf->Voltage1);
  314. }
  315. else if((temp = strstr(str, "Voltage2="))!=NULL)
  316. {
  317. temp += 9;
  318. sscanf(temp, "%d", &m_conf->Voltage2);
  319. }
  320. else if((temp = strstr(str, "Voltage3="))!=NULL)
  321. {
  322. temp += 9;
  323. sscanf(temp, "%d", &m_conf->Voltage3);
  324. }
  325. else if((temp = strstr(str, "final_voltage1="))!=NULL)
  326. {
  327. temp += 15;
  328. sscanf(temp, "%ud", &m_conf->final_voltage1);
  329. }
  330. else if((temp = strstr(str, "final_voltage2="))!=NULL)
  331. {
  332. temp += 15;
  333. sscanf(temp, "%ud", &m_conf->final_voltage2);
  334. }
  335. else if((temp = strstr(str, "final_voltage3="))!=NULL)
  336. {
  337. temp += 15;
  338. sscanf(temp, "%ud", &m_conf->final_voltage3);
  339. }
  340. else if((temp = strstr(str, "freq_gap="))!=NULL)
  341. {
  342. temp += 9;
  343. sscanf(temp, "%ud", &m_conf->freq_gap);
  344. }
  345. else if((temp = strstr(str, "OpenCoreGap="))!=NULL)
  346. {
  347. temp += 12;
  348. sscanf(temp, "%d", &m_conf->OpenCoreGap);
  349. }
  350. else if((temp = strstr(str, "CheckTemp="))!=NULL)
  351. {
  352. temp += 10;
  353. sscanf(temp, "%d", &m_conf->checktemp);
  354. }
  355. else if((temp = strstr(str, "IICPic="))!=NULL)
  356. {
  357. temp += 7;
  358. sscanf(temp, "%d", &m_conf->IICPic);
  359. }
  360. else if((temp = strstr(str, "Open_Core_Num1="))!=NULL)
  361. {
  362. temp += 15;
  363. sscanf(temp, "%ud", &m_conf->OpenCoreNum1);
  364. }
  365. else if((temp = strstr(str, "Open_Core_Num2="))!=NULL)
  366. {
  367. temp += 15;
  368. sscanf(temp, "%ud", &m_conf->OpenCoreNum2);
  369. }
  370. else if((temp = strstr(str, "Open_Core_Num3="))!=NULL)
  371. {
  372. temp += 15;
  373. sscanf(temp, "%ud", &m_conf->OpenCoreNum3);
  374. }
  375. else if((temp = strstr(str, "Open_Core_Num4="))!=NULL)
  376. {
  377. temp += 15;
  378. sscanf(temp, "%ud", &m_conf->OpenCoreNum4);
  379. }
  380. else if((temp = strstr(str, "DAC="))!=NULL)
  381. {
  382. temp += 4;
  383. sscanf(temp, "%ud", &m_conf->dac);
  384. }
  385. else if((temp = strstr(str, "GetTempFrom="))!=NULL)
  386. {
  387. temp += 12;
  388. sscanf(temp, "%ud", &m_conf->GetTempFrom);
  389. }
  390. else if((temp = strstr(str, "TempSel="))!=NULL)
  391. {
  392. temp += 8;
  393. sscanf(temp, "%ud", &m_conf->TempSel);
  394. }
  395. else if((temp = strstr(str, "TempSensor1="))!=NULL)
  396. {
  397. temp += 12;
  398. sscanf(temp, "%ud", &m_conf->TempSensor1);
  399. }
  400. else if((temp = strstr(str, "TempSensor2="))!=NULL)
  401. {
  402. temp += 12;
  403. sscanf(temp, "%ud", &m_conf->TempSensor2);
  404. }
  405. else if((temp = strstr(str, "TempSensor3="))!=NULL)
  406. {
  407. temp += 12;
  408. sscanf(temp, "%ud", &m_conf->TempSensor3);
  409. }
  410. else if((temp = strstr(str, "TempSensor4="))!=NULL)
  411. {
  412. temp += 12;
  413. sscanf(temp, "%ud", &m_conf->TempSensor4);
  414. }
  415. else if((temp = strstr(str, "DefaultTempOffset="))!=NULL)
  416. {
  417. temp += 18;
  418. sscanf(temp, "%d", &offset);
  419. if(offset < 0)
  420. {
  421. offset -= 2*offset;
  422. m_conf->DefaultTempOffset = (signed char)offset;
  423. m_conf->DefaultTempOffset -= 2*m_conf->DefaultTempOffset;
  424. //printf("~~~~~~~~~ m_conf->DefaultTempOffset = %d\n", m_conf->DefaultTempOffset);
  425. }
  426. else
  427. {
  428. m_conf->DefaultTempOffset = offset;
  429. //printf("~~~~~~~~~ m_conf->DefaultTempOffset = %d\n", m_conf->DefaultTempOffset);
  430. }
  431. }
  432. else if((temp = strstr(str, "year="))!=NULL)
  433. {
  434. temp += 5;
  435. sscanf(temp, "%d", &m_conf->year);
  436. //printf("year = %d\n", m_conf->year);
  437. }
  438. else if((temp = strstr(str, "month="))!=NULL)
  439. {
  440. temp += 6;
  441. sscanf(temp, "%d", &m_conf->month);
  442. }
  443. else if((temp = strstr(str, "date="))!=NULL)
  444. {
  445. temp += 5;
  446. sscanf(temp, "%d", &m_conf->date);
  447. }
  448. else if((temp = strstr(str, "hour="))!=NULL)
  449. {
  450. temp += 5;
  451. sscanf(temp, "%d", &m_conf->hour);
  452. }
  453. else if((temp = strstr(str, "minute="))!=NULL)
  454. {
  455. temp += 7;
  456. sscanf(temp, "%d", &m_conf->minute);
  457. }
  458. else if((temp = strstr(str, "second="))!=NULL)
  459. {
  460. temp += 7;
  461. sscanf(temp, "%d", &m_conf->second);
  462. }
  463. else if((temp = strstr(str, "StartSensor="))!=NULL)
  464. {
  465. temp += 12;
  466. sscanf(temp, "%d", &m_conf->StartSensor);
  467. }
  468. else if((temp = strstr(str, "StartTemp="))!=NULL)
  469. {
  470. temp += 10;
  471. sscanf(temp, "%d", &m_conf->StartTemp);
  472. sscanf(temp, "%d", &starttemp);
  473. if(starttemp < 0)
  474. {
  475. starttemp -= 2*starttemp;
  476. m_conf->StartTemp = (signed char)starttemp;
  477. m_conf->StartTemp -= 2*m_conf->StartTemp;
  478. //printf("~~~~~~~~~ m_conf->DefaultTempOffset = %d\n", m_conf->DefaultTempOffset);
  479. }
  480. else
  481. {
  482. m_conf->StartTemp = starttemp;
  483. //printf("~~~~~~~~~ m_conf->DefaultTempOffset = %d\n", m_conf->DefaultTempOffset);
  484. }
  485. }
  486. }
  487. m_conf->AsicNum=CHAIN_ASIC_NUM;
  488. m_conf->AsicType=ASIC_TYPE;
  489. m_conf->CoreNum=ASIC_CORE_NUM;
  490. return 0;
  491. }
  492. static int process_config()
  493. {
  494. uint32_t rBaudrate;
  495. int temp_corenum = 0;
  496. conf.CommandMode = Conf.CommandMode;
  497. conf.TempSel = Conf.TempSel;
  498. conf.GetTempFrom = Conf.GetTempFrom;
  499. if(Conf.CommandMode == FIL)
  500. {
  501. if(conf.GetTempFrom == 1) // read temp from asic
  502. {
  503. applog(LOG_ERR, "Can't get temperature from ASIC in FIL mode!\n");
  504. return -1;
  505. }
  506. }
  507. if(Conf.CommandMode == VIL)
  508. {
  509. if(conf.GetTempFrom == 1) // read temp from asic
  510. {
  511. cgpu.temp_sel = Conf.TempSel;
  512. cgpu.rfs = 1;
  513. cgpu.tfs = 3;
  514. //printf("cgpu.temp_sel = %d, cgpu.rfs = %d, cgpu.tfs = %d\n", cgpu.temp_sel, cgpu.rfs, cgpu.tfs);
  515. if(Conf.TempSensor1 + Conf.TempSensor2 + Conf.TempSensor3 + Conf.TempSensor4)
  516. {
  517. conf.TempSensor1 = Conf.TempSensor1;
  518. conf.TempSensor2 = Conf.TempSensor2;
  519. conf.TempSensor3 = Conf.TempSensor3;
  520. conf.TempSensor4 = Conf.TempSensor4;
  521. conf.DefaultTempOffset = Conf.DefaultTempOffset;
  522. cgpu.T1_offset_value = Conf.DefaultTempOffset;
  523. cgpu.T2_offset_value = Conf.DefaultTempOffset;
  524. cgpu.T3_offset_value = Conf.DefaultTempOffset;
  525. cgpu.T4_offset_value = Conf.DefaultTempOffset;
  526. conf.StartSensor = Conf.StartSensor;
  527. conf.StartTemp = Conf.StartTemp;
  528. }
  529. else
  530. {
  531. applog(LOG_ERR, "Must set temperature sensor address!\n");
  532. return -1;
  533. }
  534. }
  535. }
  536. conf.AsicType = ASIC_TYPE;
  537. conf.core = Conf.CoreNum;
  538. conf.freq_e = Conf.freq_e;
  539. conf.freq_m = Conf.freq_m;
  540. conf.freq_a = Conf.freq_a;
  541. conf.freq_t = Conf.freq_t;
  542. conf.force_freq = Conf.force_freq;
  543. conf.UseConfigVol = Conf.UseConfigVol;
  544. conf.OpenCoreNum1 = Conf.OpenCoreNum1;
  545. conf.OpenCoreNum2 = Conf.OpenCoreNum2;
  546. conf.OpenCoreNum3 = Conf.OpenCoreNum3;
  547. conf.OpenCoreNum4 = Conf.OpenCoreNum4;
  548. conf.asicNum = calculate_asic_number(CHAIN_ASIC_NUM);
  549. conf.addrInterval = Conf.AddrInterval = CHIP_ADDR_INTERVAL;
  550. temp_corenum = calculate_core_number(conf.core);
  551. conf.testMode = Conf.TestMode;
  552. conf.ValidNonce1 = Conf.ValidNonce1;
  553. conf.ValidNonce2 = Conf.ValidNonce2;
  554. conf.ValidNonce3 = Conf.ValidNonce3;
  555. conf.Pic = Conf.Pic;
  556. conf.IICPic = Conf.IICPic;
  557. conf.dac= Conf.dac;
  558. conf.Voltage1 = Conf.Voltage1;
  559. conf.Voltage2 = Conf.Voltage2;
  560. conf.Voltage3 = Conf.Voltage3;
  561. conf.OpenCoreGap = Conf.OpenCoreGap;
  562. conf.checktemp = Conf.checktemp;
  563. if(ASIC_TYPE==1385 || ASIC_TYPE == 1387)
  564. {
  565. conf.freq = Conf.Freq;
  566. }
  567. else
  568. {
  569. printf("%s: ASIC_TYPE = %d, but it is not correct!\n", __FUNCTION__, ASIC_TYPE);
  570. }
  571. conf.year = Conf.year;
  572. conf.month = Conf.month;
  573. conf.date = Conf.date;
  574. conf.hour = Conf.hour;
  575. conf.minute = Conf.minute;
  576. conf.second = Conf.second;
  577. if(Conf.Timeout <= 0)
  578. conf.timeout = 0x1000000/temp_corenum*conf.addrInterval/Conf.Freq*95/100;
  579. else
  580. conf.timeout = Conf.Timeout;
  581. rBaudrate = 1000000 * 5/3 / conf.timeout * (64*8);//64*8 need send bit, ratio=2/3
  582. conf.baud = 25000000/rBaudrate/8 - 1;
  583. if(conf.baud > DEFAULT_BAUD_VALUE)
  584. {
  585. conf.baud = DEFAULT_BAUD_VALUE;
  586. }
  587. else if(conf.baud <= 0)
  588. {
  589. applog(LOG_ERR, "$$$$Config argument Baudrate:%d err\n", conf.baud);
  590. return -1;
  591. }
  592. if(Conf.DataCount > MAX_WORK || Conf.DataCount <= 0)
  593. {
  594. applog(LOG_ERR, "$$$$Config argument DataCount:%d err\n", Conf.DataCount);
  595. }
  596. else
  597. conf.dataCount = Conf.DataCount;
  598. if(Conf.PassCount1 > conf.dataCount || Conf.PassCount1 < 0)
  599. {
  600. applog(LOG_ERR, "$$$$Config argument DataCount:%d err\n", Conf.DataCount);
  601. }
  602. else
  603. conf.passCount1 = Conf.PassCount1;
  604. if(Conf.PassCount2 > conf.dataCount || Conf.PassCount2 < 0)
  605. {
  606. applog(LOG_ERR, "$$$$Config argument DataCount:%d err\n", Conf.DataCount);
  607. }
  608. else
  609. conf.passCount2 = Conf.PassCount2;
  610. if(Conf.PassCount3 > conf.dataCount || Conf.PassCount3 < 0)
  611. {
  612. applog(LOG_ERR, "$$$$Config argument DataCount:%d err\n", Conf.DataCount);
  613. }
  614. else
  615. conf.passCount3 = Conf.PassCount3;
  616. return 0;
  617. }
  618. static void print_config()
  619. {
  620. const struct configuration *m_conf = &Conf;
  621. printf("\n\nRead Config.ini\n");
  622. printf("DataCount:%d\n", m_conf->DataCount);
  623. printf("PassCount1:%d\n", m_conf->PassCount1);
  624. printf("PassCount2:%d\n", m_conf->PassCount2);
  625. printf("PassCount3:%d\n", m_conf->PassCount3);
  626. printf("Freq:%d\n", m_conf->Freq);
  627. printf("Timeout:%d\n", m_conf->Timeout);
  628. printf("OpenCoreGap:%d\n", m_conf->OpenCoreGap);
  629. printf("CheckTemp:%d\n", m_conf->checktemp);
  630. printf("CoreNum:%d\n", m_conf->CoreNum);
  631. printf("freq_e:%d\n", m_conf->freq_e);
  632. printf("AsicNum:%d\n", m_conf->AsicNum);
  633. printf("TestMode:%d\n", m_conf->TestMode);
  634. printf("CheckChain:%d\n", m_conf->CheckChain);
  635. printf("CommandMode:%d\n", m_conf->CommandMode);
  636. printf("AsicType:%d\n", m_conf->AsicType);
  637. printf("ValidNonce1:%d\n", m_conf->ValidNonce1);
  638. printf("ValidNonce2:%d\n", m_conf->ValidNonce2);
  639. printf("ValidNonce3:%d\n", m_conf->ValidNonce3);
  640. printf("Pic:%ud\n", m_conf->Pic);
  641. printf("IICPic:%ud\n", m_conf->IICPic);
  642. printf("dac = %ud\n", m_conf->dac);
  643. printf("Voltage1:%ud\n", m_conf->Voltage1);
  644. printf("Voltage2:%ud\n", m_conf->Voltage2);
  645. printf("Voltage3:%ud\n", m_conf->Voltage3);
  646. printf("OpenCoreNum1 = %ud = 0x%x\n", m_conf->OpenCoreNum1, m_conf->OpenCoreNum1);
  647. printf("OpenCoreNum2 = %ud = 0x%x\n", m_conf->OpenCoreNum2, m_conf->OpenCoreNum2);
  648. printf("OpenCoreNum3 = %ud = 0x%x\n", m_conf->OpenCoreNum3, m_conf->OpenCoreNum3);
  649. printf("OpenCoreNum4 = %ud = 0x%x\n", m_conf->OpenCoreNum4, m_conf->OpenCoreNum4);
  650. printf("GetTempFrom:%d\n", m_conf->GetTempFrom);
  651. printf("TempSel:%d\n", m_conf->TempSel);
  652. printf("TempSensor1:%d\n", m_conf->TempSensor1);
  653. printf("TempSensor2:%d\n", m_conf->TempSensor2);
  654. printf("TempSensor3:%d\n", m_conf->TempSensor3);
  655. printf("TempSensor4:%d\n", m_conf->TempSensor4);
  656. printf("DefaultTempOffset:%d\n", m_conf->DefaultTempOffset);
  657. printf("StartSensor:%d\n", m_conf->StartSensor);
  658. printf("StartTemp:%d\n", m_conf->StartTemp);
  659. printf("year:%04d\n", m_conf->year);
  660. printf("month:%02d\n", m_conf->month);
  661. printf("date:%02d\n", m_conf->date);
  662. printf("hour:%02d\n", m_conf->hour);
  663. printf("minute:%02d\n", m_conf->minute);
  664. printf("second:%02d\n", m_conf->second);
  665. printf("\n\n");
  666. }
  667. static void print_CONFIG(void)
  668. {
  669. const struct _CONFIG *m_conf = &conf;
  670. printf("\n\nparameter processed after Reading Config.ini\n");
  671. printf("DataCount:%d\n", m_conf->dataCount);
  672. printf("PassCount1:%d\n", m_conf->passCount1);
  673. printf("PassCount2:%d\n", m_conf->passCount2);
  674. printf("PassCount3:%d\n", m_conf->passCount3);
  675. printf("Freq:%d\n", m_conf->freq);
  676. printf("Timeout:%d\n", m_conf->timeout);
  677. printf("OpenCoreGap:%d\n", m_conf->OpenCoreGap);
  678. printf("CheckTemp:%d\n", m_conf->checktemp);
  679. printf("CoreNum:%d\n", m_conf->core);
  680. printf("AsicNum:%d\n", m_conf->asicNum);
  681. printf("TestMode:%d\n", m_conf->testMode);
  682. printf("CommandMode:%d\n", m_conf->CommandMode);
  683. printf("AsicType:%d\n", m_conf->AsicType);
  684. printf("ValidNonce1:%d\n", m_conf->ValidNonce1);
  685. printf("ValidNonce2:%d\n", m_conf->ValidNonce2);
  686. printf("ValidNonce3:%d\n", m_conf->ValidNonce3);
  687. printf("Pic:%ud\n", m_conf->Pic);
  688. printf("IICPic:%ud\n", m_conf->IICPic);
  689. printf("dac:%ud\n", m_conf->dac);
  690. printf("Voltage1:%ud\n", m_conf->Voltage1);
  691. printf("Voltage2:%ud\n", m_conf->Voltage2);
  692. printf("Voltage3:%ud\n", m_conf->Voltage3);
  693. printf("OpenCoreNum1 = %ud = 0x%x\n", m_conf->OpenCoreNum1, m_conf->OpenCoreNum1);
  694. printf("OpenCoreNum2 = %ud = 0x%x\n", m_conf->OpenCoreNum2, m_conf->OpenCoreNum2);
  695. printf("OpenCoreNum3 = %ud = 0x%x\n", m_conf->OpenCoreNum3, m_conf->OpenCoreNum3);
  696. printf("OpenCoreNum4 = %ud = 0x%x\n", m_conf->OpenCoreNum4, m_conf->OpenCoreNum4);
  697. printf("GetTempFrom:%d\n", m_conf->GetTempFrom);
  698. printf("TempSel:%d\n", m_conf->TempSel);
  699. printf("TempSensor1:%d\n", m_conf->TempSensor1);
  700. printf("TempSensor2:%d\n", m_conf->TempSensor2);
  701. printf("TempSensor3:%d\n", m_conf->TempSensor3);
  702. printf("TempSensor4:%d\n", m_conf->TempSensor4);
  703. printf("DefaultTempOffset:%d\n", m_conf->DefaultTempOffset);
  704. printf("StartSensor:%d\n", m_conf->StartSensor);
  705. printf("StartTemp:%d\n", m_conf->StartTemp);
  706. printf("year:%04d\n", m_conf->year);
  707. printf("month:%02d\n", m_conf->month);
  708. printf("date:%02d\n", m_conf->date);
  709. printf("hour:%02d\n", m_conf->hour);
  710. printf("minute:%02d\n", m_conf->minute);
  711. printf("second:%02d\n", m_conf->second);
  712. printf("\n\n");
  713. }
  714. static int get_works()
  715. {
  716. char strFilePath[64] = {0};
  717. int i, j, record, loop=0;
  718. unsigned int OpenCoreNum1 = conf.OpenCoreNum1;
  719. unsigned int OpenCoreNum2 = conf.OpenCoreNum2;
  720. unsigned int OpenCoreNum3 = conf.OpenCoreNum3;
  721. unsigned int OpenCoreNum4 = conf.OpenCoreNum4;
  722. //getcwd(Path, 128);
  723. //applog(LOG_DEBUG, "Path:%s\n", Path);
  724. //printf("%s: loop = %d\n", __FUNCTION__, loop);
  725. if(CHAIN_ASIC_NUM == 1)
  726. {
  727. for(j=0; j < 32; j++)
  728. {
  729. if(OpenCoreNum1 & 0x00000001)
  730. {
  731. loop++;
  732. }
  733. OpenCoreNum1 = OpenCoreNum1 >> 1;
  734. if(OpenCoreNum2 & 0x00000001)
  735. {
  736. loop++;
  737. }
  738. OpenCoreNum2 = OpenCoreNum2 >> 1;
  739. if(OpenCoreNum3 & 0x00000001)
  740. {
  741. loop++;
  742. }
  743. OpenCoreNum3 = OpenCoreNum3 >> 1;
  744. if(OpenCoreNum4 & 0x00000001)
  745. {
  746. loop++;
  747. }
  748. OpenCoreNum4 = OpenCoreNum4 >> 1;
  749. }
  750. printf("%s: loop = %d\n", __FUNCTION__, loop);
  751. }
  752. else
  753. {
  754. loop = conf.asicNum;
  755. }
  756. j=0;
  757. OpenCoreNum1 = conf.OpenCoreNum1;
  758. OpenCoreNum2 = conf.OpenCoreNum2;
  759. OpenCoreNum3 = conf.OpenCoreNum3;
  760. OpenCoreNum4 = conf.OpenCoreNum4;
  761. for(i = 0; i < loop; i++)
  762. {
  763. if(CHAIN_ASIC_NUM == 1)
  764. {
  765. for(; j < 128; j++)
  766. {
  767. if(j < 32)
  768. {
  769. if(OpenCoreNum1 & 0x00000001)
  770. {
  771. sprintf(strFilePath, "%s%02i.bin", cgpu.workdataPathPrefix, j+1);
  772. printf("dir:%s\n", strFilePath);
  773. OpenCoreNum1 = OpenCoreNum1 >> 1;
  774. j++;
  775. break;
  776. }
  777. else
  778. {
  779. OpenCoreNum1 = OpenCoreNum1 >> 1;
  780. }
  781. }
  782. else if((j >= 32) && (j < 64))
  783. {
  784. if(OpenCoreNum2 & 0x00000001)
  785. {
  786. sprintf(strFilePath, "%s%02i.bin", cgpu.workdataPathPrefix, j+1);
  787. printf("dir:%s\n", strFilePath);
  788. OpenCoreNum2 = OpenCoreNum2 >> 1;
  789. j++;
  790. break;
  791. }
  792. else
  793. {
  794. OpenCoreNum2 = OpenCoreNum2 >> 1;
  795. }
  796. }
  797. else if((j >= 64) && (j < 96))
  798. {
  799. if(OpenCoreNum3 & 0x00000001)
  800. {
  801. sprintf(strFilePath, "%s%02i.bin", cgpu.workdataPathPrefix, j+1);
  802. printf("dir:%s\n", strFilePath);
  803. OpenCoreNum3 = OpenCoreNum3 >> 1;
  804. j++;
  805. break;
  806. }
  807. else
  808. {
  809. OpenCoreNum3 = OpenCoreNum3 >> 1;
  810. }
  811. }
  812. else
  813. {
  814. if(OpenCoreNum4 & 0x00000001)
  815. {
  816. sprintf(strFilePath, "%s%02i.bin", cgpu.workdataPathPrefix, j+1);
  817. printf("dir:%s\n", strFilePath);
  818. OpenCoreNum4 = OpenCoreNum4 >> 1;
  819. j++;
  820. break;
  821. }
  822. else
  823. {
  824. OpenCoreNum4 = OpenCoreNum4 >> 1;
  825. }
  826. }
  827. }
  828. }
  829. else
  830. {
  831. sprintf(strFilePath, "%s%02i.bin", cgpu.workdataPathPrefix, i+1);
  832. //applog(LOG_DEBUG, "dir:%s\n", strFilePath);
  833. }
  834. cgpu.fps[i] = fopen(strFilePath, "rb");
  835. if(NULL == cgpu.fps[i])
  836. {
  837. applog(LOG_ERR, "Open test file %s error\n", strFilePath);
  838. return -1;
  839. }
  840. cgpu.subid[i] = load_testpatten_work(i, MAX_WORK);
  841. //applog(LOG_DEBUG, "asic[%d] get work %d\n", i, cgpu.subid[i]);
  842. fclose(cgpu.fps[i]);
  843. }
  844. cgpu.min_work_subid = cgpu.subid[0];
  845. record = 0;
  846. for(i = 0; i < loop; i++)
  847. {
  848. if(cgpu.min_work_subid > cgpu.subid[i])
  849. {
  850. cgpu.min_work_subid = cgpu.subid[i];
  851. record = i;
  852. }
  853. }
  854. applog(LOG_DEBUG, "min work minertest[%d]:%d\n\n\n", record, cgpu.min_work_subid);
  855. if(conf.dataCount > cgpu.min_work_subid)
  856. {
  857. applog(LOG_ERR, "$$$$dataCount=%d, but min work subid=%d\n",
  858. conf.dataCount, cgpu.min_work_subid);
  859. return -1;
  860. }
  861. return 0;
  862. }
  863. static int configMiner()
  864. {
  865. int ret;
  866. read_config();
  867. print_config();
  868. ret = process_config();
  869. if(ret < 0) return -EFAULT;
  870. print_CONFIG();
  871. ret = get_works();
  872. if(ret < 0) return -EFAULT;
  873. return 0;
  874. }
  875. static int calculate_asic_number(unsigned int actual_asic_number)
  876. {
  877. int i = 0;
  878. if(actual_asic_number == 1)
  879. {
  880. i = 1;
  881. }
  882. else if(actual_asic_number == 2)
  883. {
  884. i = 2;
  885. }
  886. else if((actual_asic_number > 2) && (actual_asic_number <= 4))
  887. {
  888. i = 4;
  889. }
  890. else if((actual_asic_number > 4) && (actual_asic_number <= 8))
  891. {
  892. i = 8;
  893. }
  894. else if((actual_asic_number > 8) && (actual_asic_number <= 16))
  895. {
  896. i = 16;
  897. }
  898. else if((actual_asic_number > 16) && (actual_asic_number <= 32))
  899. {
  900. i = 32;
  901. }
  902. else if((actual_asic_number > 32) && (actual_asic_number <= 64))
  903. {
  904. i = 64;
  905. }
  906. else if((actual_asic_number > 64) && (actual_asic_number <= 128))
  907. {
  908. i = 128;
  909. }
  910. else
  911. {
  912. applog(LOG_DEBUG,"actual_asic_number = %d, but it is error\n", actual_asic_number);
  913. return -1;
  914. }
  915. return i;
  916. }
  917. static int calculate_core_number(unsigned int actual_core_number)
  918. {
  919. int i = 0;
  920. if(actual_core_number == 1)
  921. {
  922. i = 1;
  923. }
  924. else if(actual_core_number == 2)
  925. {
  926. i = 2;
  927. }
  928. else if((actual_core_number > 2) && (actual_core_number <= 4))
  929. {
  930. i = 4;
  931. }
  932. else if((actual_core_number > 4) && (actual_core_number <= 8))
  933. {
  934. i = 8;
  935. }
  936. else if((actual_core_number > 8) && (actual_core_number <= 16))
  937. {
  938. i = 16;
  939. }
  940. else if((actual_core_number > 16) && (actual_core_number <= 32))
  941. {
  942. i = 32;
  943. }
  944. else if((actual_core_number > 32) && (actual_core_number <= 64))
  945. {
  946. i = 64;
  947. }
  948. else if((actual_core_number > 64) && (actual_core_number <= 128))
  949. {
  950. i = 128;
  951. }
  952. else
  953. {
  954. applog(LOG_DEBUG,"actual_core_number = %d, but it is error\n", actual_core_number);
  955. return -1;
  956. }
  957. return i;
  958. }
  959. static int get_result(int chainIndex, int passCount, int validnonce)
  960. {
  961. char logstr[1024];
  962. int ret = 3;
  963. int i, j=0, loop=0, m, n;
  964. unsigned int OpenCoreNum1 = conf.OpenCoreNum1;
  965. unsigned int OpenCoreNum2 = conf.OpenCoreNum2;
  966. unsigned int OpenCoreNum3 = conf.OpenCoreNum3;
  967. unsigned int OpenCoreNum4 = conf.OpenCoreNum4;
  968. printf("\n------------------------------------------------------------------------------------------------------\n");
  969. if(conf.CommandMode)
  970. {
  971. printf("Command mode is FIL\n");
  972. }
  973. else
  974. {
  975. printf("Command mode is VIL\n");
  976. }
  977. if(cgpu.real_asic_num == 1)
  978. {
  979. printf("Open core number : Conf.OpenCoreNum1 = %ud = 0x%x\n", Conf.OpenCoreNum1, Conf.OpenCoreNum1);
  980. printf("Open core number : Conf.OpenCoreNum2 = %ud = 0x%x\n", Conf.OpenCoreNum2, Conf.OpenCoreNum2);
  981. printf("Open core number : Conf.OpenCoreNum3 = %ud = 0x%x\n", Conf.OpenCoreNum3, Conf.OpenCoreNum3);
  982. printf("Open core number : Conf.OpenCoreNum4 = %ud = 0x%x\n", Conf.OpenCoreNum4, Conf.OpenCoreNum4);
  983. loop = Conf.CoreNum;
  984. }
  985. else
  986. {
  987. loop = cgpu.real_asic_num;
  988. }
  989. sprintf(logstr,"require nonce number:%d\n", passCount);
  990. writeLogFile(logstr);
  991. sprintf(logstr,"require validnonce number:%d\n", validnonce);
  992. writeLogFile(logstr);
  993. for(i = 0; i < loop; i++)
  994. {
  995. #ifdef LOG_CHIPS_CORE_DETAIL
  996. if(cgpu.real_asic_num == 1)
  997. {
  998. sprintf(logstr,"core[%02d]=%02d\t", i, asic_nonce_num[chainIndex][i]);
  999. writeLogFile(logstr);
  1000. }
  1001. else
  1002. {
  1003. sprintf(logstr,"asic[%02d]=%02d\t", i, asic_nonce_num[chainIndex][i]);
  1004. writeLogFile(logstr);
  1005. }
  1006. if(i % 8 == 7)
  1007. {
  1008. sprintf(logstr,"\n");
  1009. writeLogFile(logstr);
  1010. }
  1011. #endif
  1012. if(cgpu.real_asic_num == 1)
  1013. {
  1014. for(; j < 128; j++)
  1015. {
  1016. if(j < 32)
  1017. {
  1018. if(OpenCoreNum1 & 0x00000001)
  1019. {
  1020. if(asic_nonce_num[chainIndex][j] < passCount)
  1021. {
  1022. ret = (~0x00000001) & ret;
  1023. }
  1024. OpenCoreNum1 = OpenCoreNum1 >> 1;
  1025. }
  1026. else
  1027. {
  1028. OpenCoreNum1 = OpenCoreNum1 >> 1;
  1029. }
  1030. }
  1031. else if((j >= 32) && (j < 64))
  1032. {
  1033. if(OpenCoreNum2 & 0x00000001)
  1034. {
  1035. if(asic_nonce_num[chainIndex][j] < passCount)
  1036. {
  1037. ret = (~0x00000001) & ret;
  1038. }
  1039. OpenCoreNum2 = OpenCoreNum2 >> 1;
  1040. }
  1041. else
  1042. {
  1043. OpenCoreNum2 = OpenCoreNum2 >> 1;
  1044. }
  1045. }
  1046. else if((j >= 64) && (j < 96))
  1047. {
  1048. if(OpenCoreNum3 & 0x00000001)
  1049. {
  1050. if(asic_nonce_num[chainIndex][j] < passCount)
  1051. {
  1052. ret = (~0x00000001) & ret;
  1053. }
  1054. OpenCoreNum3 = OpenCoreNum3 >> 1;
  1055. }
  1056. else
  1057. {
  1058. OpenCoreNum3 = OpenCoreNum3 >> 1;
  1059. }
  1060. }
  1061. else
  1062. {
  1063. if(OpenCoreNum4 & 0x00000001)
  1064. {
  1065. if(asic_nonce_num[chainIndex][j] < passCount)
  1066. {
  1067. ret = (~0x00000001) & ret;
  1068. }
  1069. OpenCoreNum4 = OpenCoreNum4 >> 1;
  1070. }
  1071. else
  1072. {
  1073. OpenCoreNum4 = OpenCoreNum4 >> 1;
  1074. }
  1075. }
  1076. }
  1077. }
  1078. else
  1079. {
  1080. if(asic_nonce_num[chainIndex][i] < passCount)
  1081. {
  1082. ret = (~0x00000001) & ret;
  1083. }
  1084. }
  1085. }
  1086. if((Conf.StartSensor > 0) && (cgpu.real_asic_num != 1))
  1087. {
  1088. n = passCount/Conf.CoreNum;
  1089. #ifdef LOG_CHIPS_CORE_DETAIL
  1090. sprintf(logstr,"\n\n\nBelow ASIC's core didn't receive all the nonce, they should receive %d nonce each!\n\n", n);
  1091. writeLogFile(logstr);
  1092. #endif
  1093. for(i = 0; i < loop; i++)
  1094. {
  1095. int opened_core_num=0;
  1096. for(m=0; m<Conf.CoreNum; m++)
  1097. {
  1098. if(asic_core_nonce_num[chainIndex][i][m]>0) // we only check core is open
  1099. opened_core_num++;
  1100. }
  1101. if(opened_core_num >= Conf.CoreNum-chain_badcore_num[chainIndex][i])
  1102. last_result_opencore[chainIndex][i]=1;
  1103. else last_result_opencore[chainIndex][i]=0;
  1104. if(asic_nonce_num[chainIndex][i] < passCount-chain_badcore_num[chainIndex][i]*n)
  1105. last_result[chainIndex][i]=0;
  1106. else last_result[chainIndex][i]=1;
  1107. #ifdef LOG_CHIPS_CORE_DETAIL
  1108. if(asic_nonce_num[chainIndex][i] < passCount)
  1109. {
  1110. sprintf(logstr,"asic[%02d]=%02d\n", i, asic_nonce_num[chainIndex][i]);
  1111. writeLogFile(logstr);
  1112. for(m=0; m<Conf.CoreNum; m++)
  1113. {
  1114. if(asic_core_nonce_num[chainIndex][i][m] != n)
  1115. {
  1116. sprintf(logstr,"core[%03d]=%d\t", m, asic_core_nonce_num[chainIndex][i][m]);
  1117. writeLogFile(logstr);
  1118. }
  1119. }
  1120. sprintf(logstr,"\n\n");
  1121. writeLogFile(logstr);
  1122. }
  1123. #endif
  1124. }
  1125. }
  1126. sprintf(logstr,"\n\n");
  1127. writeLogFile(logstr);
  1128. for(i = 0; i < loop; i++)
  1129. {
  1130. sprintf(logstr,"freq[%02d]=%d\t", i, get_freqvalue_by_index(getChainAsicFreqIndex(chainIndex,i)));
  1131. writeLogFile(logstr);
  1132. if(i % 8 == 7)
  1133. {
  1134. sprintf(logstr,"\n");
  1135. writeLogFile(logstr);
  1136. }
  1137. }
  1138. sprintf(logstr,"\n\n");
  1139. writeLogFile(logstr);
  1140. if(valid_nonce_num[chainIndex] < validnonce)
  1141. {
  1142. ret = (~0x00000001) & ret;
  1143. }
  1144. sprintf(logstr,"total valid nonce number:%d\n", valid_nonce_num[chainIndex]);
  1145. writeLogFile(logstr);
  1146. sprintf(logstr,"total send work number:%d\n", send_work_num[chainIndex]);
  1147. writeLogFile(logstr);
  1148. sprintf(logstr,"require valid nonce number:%d\n", validnonce);
  1149. writeLogFile(logstr);
  1150. sprintf(logstr,"repeated_nonce_num:%d\n", repeated_nonce_num[chainIndex]);
  1151. writeLogFile(logstr);
  1152. sprintf(logstr,"err_nonce_num:%d\n", err_nonce_num[chainIndex]);
  1153. writeLogFile(logstr);
  1154. sprintf(logstr,"last_nonce_num:%d\n", last_nonce_num[chainIndex]);
  1155. writeLogFile(logstr);
  1156. return ret;
  1157. }
  1158. ////////////////////////////////////TEST PATTEN MAIN.C /////////////////////////////////
  1159. static int reset_work_data(void)
  1160. {
  1161. int i, j;
  1162. for(i =0 ; i < conf.asicNum; i++)
  1163. {
  1164. for(j = 0; j < conf.dataCount; j++)
  1165. {
  1166. cgpu.results[i][j] = 0;
  1167. }
  1168. cgpu.result_array[i] = 0;
  1169. }
  1170. cgpu.index = 0;
  1171. cgpu.valid_nonce = 0;
  1172. cgpu.err_nonce = 0;
  1173. cgpu.repeated_nonce = 0;
  1174. return 0;
  1175. }
  1176. static int cgpu_init(void)
  1177. {
  1178. int ret = 0;
  1179. memset(&cgpu, 0, sizeof(struct testpatten_cgpu_info));
  1180. return 0;
  1181. }
  1182. static int send_func_all()
  1183. {
  1184. int which_asic[BITMAIN_MAX_CHAIN_NUM];
  1185. int i,j;
  1186. unsigned int work_fifo_ready = 0;
  1187. int index[BITMAIN_MAX_CHAIN_NUM];
  1188. struct testpatten_work * works, *work;
  1189. unsigned char data_fil[TW_WRITE_COMMAND_LEN] = {0xff};
  1190. unsigned char data_vil[TW_WRITE_COMMAND_LEN_VIL] = {0xff};
  1191. struct vil_work_1387 work_vil_1387;
  1192. unsigned int buf[TW_WRITE_COMMAND_LEN/sizeof(unsigned int)]= {0};
  1193. unsigned int buf_vil[TW_WRITE_COMMAND_LEN_VIL/sizeof(unsigned int)]= {0};
  1194. int chainIndex;
  1195. char logstr[1024];
  1196. bool isSendOver=false;
  1197. int wait_counter=0;
  1198. bool sendStartFlag[BITMAIN_MAX_CHAIN_NUM];
  1199. for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  1200. {
  1201. index[i]=0;
  1202. which_asic[i]=0;
  1203. sendStartFlag[i]=StartSendFlag[i];
  1204. }
  1205. while(!isSendOver)
  1206. {
  1207. // send work
  1208. for(chainIndex=0; chainIndex<BITMAIN_MAX_CHAIN_NUM; chainIndex++)
  1209. {
  1210. if(cgpu.chain_exist[chainIndex] == 0 || (!sendStartFlag[chainIndex]))
  1211. continue;
  1212. while(which_asic[chainIndex] < CHAIN_ASIC_NUM)
  1213. {
  1214. work_fifo_ready = get_buffer_space();
  1215. if(work_fifo_ready & (0x1 << chainIndex)) // work fifo is not full, we can send work
  1216. {
  1217. wait_counter=0; // clear wait fifo counter
  1218. if(cgpu.CommandMode) // fil mode
  1219. {
  1220. memset(buf, 0x0, TW_WRITE_COMMAND_LEN/sizeof(unsigned int));
  1221. // get work for sending to asic
  1222. works = cgpu.works[which_asic[chainIndex]]; // which ASIC
  1223. work = works + index[chainIndex]; // which test data for the ASIC
  1224. // parse work data
  1225. memset(data_fil, 0x0, TW_WRITE_COMMAND_LEN);
  1226. data_fil[0] = NORMAL_BLOCK_MARKER;
  1227. data_fil[1] = chainIndex | 0x80; //set chain id and enable it
  1228. for(i=0; i<MIDSTATE_LEN; i++)
  1229. {
  1230. data_fil[i+4] = work->midstate[i];
  1231. }
  1232. for(i=0; i<DATA2_LEN; i++)
  1233. {
  1234. data_fil[i+40] = work->data[i];
  1235. }
  1236. // send work
  1237. //printf("\n");
  1238. for(j=0; j<TW_WRITE_COMMAND_LEN/sizeof(unsigned int); j++)
  1239. {
  1240. buf[j] = (data_fil[4*j + 0] << 24) | (data_fil[4*j + 1] << 16) | (data_fil[4*j + 2] << 8) | data_fil[4*j + 3];
  1241. if(j==9)
  1242. {
  1243. buf[j] = index[chainIndex];
  1244. }
  1245. //applog(LOG_DEBUG,"%s: buf[%d] = 0x%08x\n", __FUNCTION__, j, buf[j]);
  1246. }
  1247. #ifndef DEBUG_XILINX_NONCE_NOTENOUGH
  1248. pthread_mutex_lock(&opencore_readtemp_mutex);
  1249. #endif
  1250. set_TW_write_command(buf);
  1251. #ifndef DEBUG_XILINX_NONCE_NOTENOUGH
  1252. pthread_mutex_unlock(&opencore_readtemp_mutex);
  1253. #endif
  1254. which_asic[chainIndex]++;
  1255. }
  1256. else // vil mode
  1257. {
  1258. if(ASIC_TYPE == 1387)
  1259. {
  1260. //printf("\n--- send work\n");
  1261. memset(buf_vil, 0x0, TW_WRITE_COMMAND_LEN_VIL/sizeof(unsigned int));
  1262. works = cgpu.works[which_asic[chainIndex]]; // which ASIC
  1263. work = works + index[chainIndex]; // which test data for the ASIC
  1264. // parse work data
  1265. memset(&work_vil_1387, 0, sizeof(struct vil_work_1387));
  1266. work_vil_1387.work_type = NORMAL_BLOCK_MARKER;
  1267. work_vil_1387.chain_id = 0x80 | chainIndex;
  1268. work_vil_1387.reserved1[0]= 0;
  1269. work_vil_1387.reserved1[1]= 0;
  1270. work_vil_1387.work_count = index[chainIndex];
  1271. for(i=0; i<DATA2_LEN; i++)
  1272. {
  1273. work_vil_1387.data[i] = work->data[i];
  1274. }
  1275. for(i=0; i<MIDSTATE_LEN; i++)
  1276. {
  1277. work_vil_1387.midstate[i] = work->midstate[i];
  1278. }
  1279. // send work
  1280. buf_vil[0] = (work_vil_1387.work_type << 24) | (work_vil_1387.chain_id << 16) | (work_vil_1387.reserved1[0] << 8) | work_vil_1387.reserved1[1];
  1281. buf_vil[1] = work_vil_1387.work_count;
  1282. for(j=2; j<DATA2_LEN/sizeof(int)+2; j++)
  1283. {
  1284. buf_vil[j] = (work_vil_1387.data[4*(j-2) + 0] << 24) | (work_vil_1387.data[4*(j-2) + 1] << 16) | (work_vil_1387.data[4*(j-2) + 2] << 8) | work_vil_1387.data[4*(j-2) + 3];
  1285. }
  1286. for(j=5; j<MIDSTATE_LEN/sizeof(unsigned int)+5; j++)
  1287. {
  1288. buf_vil[j] = (work_vil_1387.midstate[4*(j-5) + 0] << 24) | (work_vil_1387.midstate[4*(j-5) + 1] << 16) | (work_vil_1387.midstate[4*(j-5) + 2] << 8) | work_vil_1387.midstate[4*(j-5) + 3];;
  1289. }
  1290. #ifndef DEBUG_XILINX_NONCE_NOTENOUGH
  1291. pthread_mutex_lock(&opencore_readtemp_mutex);
  1292. #endif
  1293. set_TW_write_command_vil(buf_vil);
  1294. #ifndef DEBUG_XILINX_NONCE_NOTENOUGH
  1295. pthread_mutex_unlock(&opencore_readtemp_mutex);
  1296. #endif
  1297. which_asic[chainIndex]++;
  1298. }
  1299. else
  1300. {
  1301. //printf("\n--- send work\n");
  1302. memset(buf_vil, 0x0, TW_WRITE_COMMAND_LEN_VIL/sizeof(unsigned int));
  1303. // get work for sending to asic
  1304. //work_fil = (struct testpatten_work *)((void *)cgpu.works[which_asic] + index*sizeof(struct testpatten_work));
  1305. works = cgpu.works[which_asic[chainIndex]]; // which ASIC
  1306. work = works + index[chainIndex]; // which test data for the ASIC
  1307. // parse work data
  1308. memset(data_vil, 0x00, TW_WRITE_COMMAND_LEN_VIL);
  1309. data_vil[0] = NORMAL_BLOCK_MARKER;
  1310. data_vil[1] = chainIndex | 0x80; //set chain id and enable it
  1311. data_vil[4] = 0x01 << 5; // type
  1312. data_vil[5] = sizeof(struct vil_work); // length
  1313. data_vil[6] = index[chainIndex]; // wc_base / work_id
  1314. data_vil[7] = 0x01; // mid_num
  1315. for(i=0; i<MIDSTATE_LEN; i++)
  1316. {
  1317. data_vil[i+8] = work->midstate[i];
  1318. }
  1319. for(i=0; i<DATA2_LEN; i++)
  1320. {
  1321. data_vil[i+40] = work->data[i];
  1322. }
  1323. // send work
  1324. for(j=0; j<TW_WRITE_COMMAND_LEN_VIL/sizeof(unsigned int); j++)
  1325. {
  1326. buf_vil[j] = (data_vil[4*j + 0] << 24) | (data_vil[4*j + 1] << 16) | (data_vil[4*j + 2] << 8) | data_vil[4*j + 3];
  1327. //printf("%s: buf_vil[%d] = 0x%08x\n", __FUNCTION__, j, buf_vil[j]);
  1328. }
  1329. #ifndef DEBUG_XILINX_NONCE_NOTENOUGH
  1330. pthread_mutex_lock(&opencore_readtemp_mutex);
  1331. #endif
  1332. set_TW_write_command_vil(buf_vil);
  1333. #ifndef DEBUG_XILINX_NONCE_NOTENOUGH
  1334. pthread_mutex_unlock(&opencore_readtemp_mutex);
  1335. #endif
  1336. which_asic[chainIndex]++;
  1337. }
  1338. }
  1339. send_work_num[chainIndex]++;
  1340. //printf("%s: send_work_num = %d\n", __FUNCTION__, send_work_num);
  1341. }
  1342. else //work fifo is full, wait for 1ms
  1343. {
  1344. wait_counter++;
  1345. break;
  1346. }
  1347. }
  1348. if(which_asic[chainIndex] >= CHAIN_ASIC_NUM)
  1349. {
  1350. which_asic[chainIndex]=0; // then send from chip[0] ....
  1351. index[chainIndex]++; // switch to next work
  1352. if(index[chainIndex] >= chain_DataCount[chainIndex])
  1353. sendStartFlag[chainIndex]=false;
  1354. }
  1355. if(wait_counter>2000)
  1356. {
  1357. // timeout on wait for fifo ready
  1358. sprintf(logstr,"Fatal Error: send work timeout\n");
  1359. writeLogFile(logstr);
  1360. break;
  1361. }
  1362. }
  1363. usleep(5000);
  1364. isSendOver=true;
  1365. for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  1366. {
  1367. if(cgpu.chain_exist[i] == 0 || (!StartSendFlag[i]))
  1368. continue;
  1369. if(index[i] < chain_DataCount[i])
  1370. {
  1371. isSendOver=false;
  1372. }
  1373. }
  1374. }
  1375. for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  1376. {
  1377. if(cgpu.chain_exist[i] == 0 || (!StartSendFlag[i]))
  1378. continue;
  1379. StartSendFlag[i]=false; // when send over , must set this flag to false!!!
  1380. sprintf(logstr,"get send work num :%d on Chain[%d]\n", send_work_num[i],i);
  1381. writeLogFile(logstr);
  1382. sendExit[i]=true;
  1383. }
  1384. return 0;
  1385. }
  1386. static uint32_t last_nonce[BITMAIN_MAX_CHAIN_NUM], llast_nonce[BITMAIN_MAX_CHAIN_NUM];
  1387. static unsigned int work_id[BITMAIN_MAX_CHAIN_NUM];
  1388. static unsigned int m_nonce[BITMAIN_MAX_CHAIN_NUM];
  1389. static void *receive_func(void *arg)
  1390. {
  1391. unsigned int j=0, n=0, nonce_number = 0, read_loop=0;
  1392. unsigned int buf[2] = {0,0};
  1393. uint8_t which_asic_nonce = 0;
  1394. uint8_t which_core_nonce = 0;
  1395. uint8_t whose_nonce = 0, nonce_index=0;
  1396. unsigned int OpenCoreNum1 = conf.OpenCoreNum1;
  1397. unsigned int OpenCoreNum2 = conf.OpenCoreNum2;
  1398. unsigned int OpenCoreNum3 = conf.OpenCoreNum3;
  1399. unsigned int OpenCoreNum4 = conf.OpenCoreNum4;
  1400. char logstr[1024];
  1401. int chainIndex;
  1402. memset(repeated_nonce_id, 0xff, sizeof(repeated_nonce_id));
  1403. memset(last_nonce,0x00,sizeof(last_nonce));
  1404. memset(llast_nonce,0x00,sizeof(llast_nonce));
  1405. memset(work_id,0x00,sizeof(work_id));
  1406. memset(m_nonce,0x00,sizeof(m_nonce));
  1407. while(!ExitFlag)
  1408. {
  1409. if(!start_receive)
  1410. {
  1411. j=0;
  1412. n=0;
  1413. nonce_number = 0;
  1414. read_loop=0;
  1415. buf[0]=0;
  1416. buf[1]=0;
  1417. which_asic_nonce = 0;
  1418. which_core_nonce = 0;
  1419. whose_nonce = 0;
  1420. nonce_index=0;
  1421. OpenCoreNum1 = conf.OpenCoreNum1;
  1422. OpenCoreNum2 = conf.OpenCoreNum2;
  1423. OpenCoreNum3 = conf.OpenCoreNum3;
  1424. OpenCoreNum4 = conf.OpenCoreNum4;
  1425. memset(repeated_nonce_id, 0xff, sizeof(repeated_nonce_id));
  1426. memset(last_nonce,0x00,sizeof(last_nonce));
  1427. memset(llast_nonce,0x00,sizeof(llast_nonce));
  1428. memset(work_id,0x00,sizeof(work_id));
  1429. memset(m_nonce,0x00,sizeof(m_nonce));
  1430. usleep(100000);
  1431. continue;
  1432. }
  1433. read_loop = 0;
  1434. nonce_number = get_nonce_number_in_fifo() & MAX_NONCE_NUMBER_IN_FIFO;
  1435. //applog(LOG_DEBUG,"%s: --- nonce_number = %d\n", __FUNCTION__, nonce_number);
  1436. if(nonce_number>0)
  1437. {
  1438. read_loop = nonce_number;
  1439. //applog(LOG_DEBUG,"%s: read_loop = %d\n", __FUNCTION__, read_loop);
  1440. for(j=0; j<read_loop; j++)
  1441. {
  1442. get_return_nonce(buf);
  1443. //printf("%s: buf[0] = 0x%08x\n", __FUNCTION__, buf[0]);
  1444. //printf("%s: buf[1] = 0x%08x\n", __FUNCTION__, buf[1]);
  1445. if(buf[0] & WORK_ID_OR_CRC) //nonce
  1446. {
  1447. if(gBegin_get_nonce)
  1448. {
  1449. if(buf[0] & NONCE_INDICATOR)
  1450. {
  1451. chainIndex=CHAIN_NUMBER(buf[0]);
  1452. if(chainIndex<0 || chainIndex>=BITMAIN_MAX_CHAIN_NUM)
  1453. {
  1454. sprintf(logstr,"Error chain index of nonce!!!\n");
  1455. writeLogFile(logstr);
  1456. continue;
  1457. }
  1458. if(cgpu.CommandMode) // fil mode
  1459. {
  1460. work_id[chainIndex] = (buf[0] >> 16) & 0x00007fff;
  1461. }
  1462. else // vil mode
  1463. {
  1464. if(ASIC_TYPE == 1387)
  1465. {
  1466. work_id[chainIndex] = (buf[0] >> 16) & 0x00007fff;
  1467. }
  1468. else
  1469. {
  1470. work_id[chainIndex] = (buf[0] >> 24) & 0x0000007f;
  1471. }
  1472. }
  1473. if((buf[1] == last_nonce[chainIndex]) || (buf[1] == llast_nonce[chainIndex]))
  1474. {
  1475. last_nonce_num[chainIndex]++;
  1476. continue;
  1477. }
  1478. if(cgpu.real_asic_num == 1)
  1479. {
  1480. if(conf.core <= 64)
  1481. {
  1482. which_core_nonce = (buf[1] & 0x0000003f);
  1483. whose_nonce = which_core_nonce;
  1484. }
  1485. else if((conf.core <= 128) && (conf.core > 64))
  1486. {
  1487. which_core_nonce = (buf[1] & 0x0000007f);
  1488. if(which_core_nonce <= 56)
  1489. {
  1490. whose_nonce = which_core_nonce;
  1491. }
  1492. else if((which_core_nonce >= 64) && (which_core_nonce < 128))
  1493. {
  1494. whose_nonce = which_core_nonce - 7;
  1495. }
  1496. }
  1497. else
  1498. {
  1499. printf("%s: conf.core = %d, but it is error\n", __FUNCTION__, conf.core);
  1500. }
  1501. nonce_index = 0;
  1502. OpenCoreNum1 = conf.OpenCoreNum1;
  1503. OpenCoreNum2 = conf.OpenCoreNum2;
  1504. OpenCoreNum3 = conf.OpenCoreNum3;
  1505. OpenCoreNum4 = conf.OpenCoreNum4;
  1506. for(n=0; n<whose_nonce; n++)
  1507. {
  1508. if(n < 32)
  1509. {
  1510. if(OpenCoreNum1 & 0x00000001)
  1511. {
  1512. nonce_index++;
  1513. OpenCoreNum1 = OpenCoreNum1 >> 1;
  1514. }
  1515. else
  1516. {
  1517. OpenCoreNum1 = OpenCoreNum1 >> 1;
  1518. }
  1519. }
  1520. else if((n >= 32) && (n < 64))
  1521. {
  1522. if(OpenCoreNum2 & 0x00000001)
  1523. {
  1524. nonce_index++;
  1525. OpenCoreNum2 = OpenCoreNum2 >> 1;
  1526. }
  1527. else
  1528. {
  1529. OpenCoreNum2 = OpenCoreNum2 >> 1;
  1530. }
  1531. }
  1532. else if((n >= 64) && (n < 96))
  1533. {
  1534. if(OpenCoreNum3 & 0x00000001)
  1535. {
  1536. nonce_index++;
  1537. OpenCoreNum3 = OpenCoreNum3 >> 1;
  1538. }
  1539. else
  1540. {
  1541. OpenCoreNum3 = OpenCoreNum3 >> 1;
  1542. }
  1543. }
  1544. else
  1545. {
  1546. if(OpenCoreNum4 & 0x00000001)
  1547. {
  1548. nonce_index++;
  1549. OpenCoreNum4 = OpenCoreNum4 >> 1;
  1550. }
  1551. else
  1552. {
  1553. OpenCoreNum4 = OpenCoreNum4 >> 1;
  1554. }
  1555. }
  1556. }
  1557. //printf("%s: nonce_index = 0x%08x\n", __FUNCTION__, nonce_index);
  1558. }
  1559. else
  1560. {
  1561. if(CHIP_ADDR_INTERVAL != 0)
  1562. {
  1563. which_asic_nonce = (buf[1] >> 24) / CHIP_ADDR_INTERVAL;
  1564. if(which_asic_nonce >= CHAIN_ASIC_NUM)
  1565. {
  1566. continue;
  1567. }
  1568. //printf("%s: which_asic = %d\n", __FUNCTION__, which_asic);
  1569. }
  1570. else
  1571. {
  1572. //printf("CHIP_ADDR_INTERVAL==0, default=4\n");
  1573. which_asic_nonce = (buf[1] >> 24) / 4;
  1574. if(which_asic_nonce >= conf.asicNum)
  1575. {
  1576. continue;
  1577. }
  1578. }
  1579. whose_nonce = which_asic_nonce;
  1580. nonce_index = which_asic_nonce;
  1581. }
  1582. //printf("%s: whose_nonce = 0x%08x\n", __FUNCTION__, whose_nonce);
  1583. llast_nonce[chainIndex] = last_nonce[chainIndex];
  1584. last_nonce[chainIndex] = buf[1];
  1585. if(work_id[chainIndex]>=MAX_WORK)
  1586. continue;
  1587. m_nonce[chainIndex] = (cgpu.works[nonce_index] + work_id[chainIndex])->nonce;
  1588. //printf("%s: m_nonce = 0x%08x\n", __FUNCTION__, m_nonce);
  1589. if(buf[1] == m_nonce[chainIndex])
  1590. {
  1591. //printf("%s: repeated_nonce_id[which_asic] = 0x%08x\n", __FUNCTION__, repeated_nonce_id[which_asic]);
  1592. if(work_id[chainIndex] != repeated_nonce_id[chainIndex][whose_nonce])
  1593. {
  1594. repeated_nonce_id[chainIndex][whose_nonce] = work_id[chainIndex];
  1595. asic_nonce_num[chainIndex][whose_nonce]++;
  1596. valid_nonce_num[chainIndex]++;
  1597. total_valid_nonce_num++; // used to check and wait all nonce back...
  1598. if(cgpu.real_asic_num != 1)
  1599. {
  1600. if(conf.core <= 64)
  1601. {
  1602. which_core_nonce = (buf[1] & 0x0000003f);
  1603. }
  1604. else if((conf.core <= 128) && (conf.core > 64))
  1605. {
  1606. which_core_nonce = (buf[1] & 0x0000007f);
  1607. if(which_core_nonce <= 56)
  1608. {
  1609. which_core_nonce = which_core_nonce;
  1610. }
  1611. else if((which_core_nonce >= 64) && (which_core_nonce < 128))
  1612. {
  1613. which_core_nonce = which_core_nonce - 7;
  1614. }
  1615. }
  1616. else
  1617. {
  1618. printf("%s: conf.core = %d, but it is error\n", __FUNCTION__, conf.core);
  1619. }
  1620. asic_core_nonce_num[chainIndex][whose_nonce][which_core_nonce]++;
  1621. }
  1622. }
  1623. else
  1624. {
  1625. repeated_nonce_num[chainIndex]++;
  1626. //printf("repeat nonce 0x%08x\n", buf[1]);
  1627. }
  1628. }
  1629. else
  1630. {
  1631. err_nonce_num[chainIndex]++;
  1632. //printf("error nonce 0x%08x\n", buf[1]);
  1633. }
  1634. //printf("\n");
  1635. }
  1636. }
  1637. }
  1638. else //reg value
  1639. {
  1640. insert_reg_data(buf); // insert to driver-btm-c5.c reg buffer
  1641. }
  1642. }
  1643. }
  1644. else usleep(1000);
  1645. }
  1646. receiveExit=true;
  1647. return 0;
  1648. }
  1649. static bool doTestBoard(int test_times)
  1650. {
  1651. int i, freq_index = 0;
  1652. char logstr[1024];
  1653. int wait_count=0;
  1654. int last_send_num;
  1655. int last_recv_num;
  1656. int vol_value, vol_pic, vol_value_limited;
  1657. bool result_flag=true;
  1658. memset(asic_nonce_num, 0, sizeof(asic_nonce_num));
  1659. memset(asic_core_nonce_num, 0, sizeof(asic_core_nonce_num));
  1660. memset(repeated_nonce_id, 0xff, sizeof(repeated_nonce_id));
  1661. memset(err_nonce_num, 0, sizeof(err_nonce_num));
  1662. memset(last_nonce_num, 0, sizeof(last_nonce_num));
  1663. memset(repeated_nonce_num, 0, sizeof(repeated_nonce_num));
  1664. memset(valid_nonce_num, 0, sizeof(valid_nonce_num));
  1665. memset(send_work_num, 0, sizeof(send_work_num));
  1666. total_valid_nonce_num=0;
  1667. start_receive=true;
  1668. FOR_LOOP_CHAIN
  1669. {
  1670. cgpu.chain_exist[i] = getChainExistFlag(i);
  1671. }
  1672. #ifndef T9_18
  1673. sprintf(logstr,"Check voltage total rate=%d\n",GetTotalRate());
  1674. writeLogFile(logstr);
  1675. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++) // here must use i from 0 in for loop, because we use j to get the index as config file's voltage value
  1676. {
  1677. if(cgpu.chain_exist[i]==0)
  1678. continue;
  1679. pthread_mutex_lock(&iic_mutex);
  1680. vol_pic=get_pic_voltage(i);
  1681. pthread_mutex_unlock(&iic_mutex);
  1682. vol_value = getVolValueFromPICvoltage(vol_pic);
  1683. chain_vol_value[i]=(vol_value/10)*10; // must record current voltage!!!
  1684. vol_value_limited=getVoltageLimitedFromHashrate(GetTotalRate());
  1685. sprintf(logstr,"get PIC voltage=%d [%d] on chain[%d], check: must be < %d\n",chain_vol_value[i],vol_pic,i,vol_value_limited);
  1686. writeLogFile(logstr);
  1687. if(chain_vol_value[i] > vol_value_limited) // we will set voltage to the highest voltage for the last chance on test patten
  1688. {
  1689. chain_vol_value[i]=vol_value_limited;
  1690. sprintf(logstr,"will set the voltage limited on chain[%d], change voltage=%d\n",i,chain_vol_value[i]);
  1691. writeLogFile(logstr);
  1692. vol_pic=getPICvoltageFromValue(chain_vol_value[i]);
  1693. sprintf(logstr,"now set pic voltage=%d on chain[%d]\n",vol_pic,i);
  1694. writeLogFile(logstr);
  1695. pthread_mutex_lock(&iic_mutex);
  1696. set_pic_voltage(i, vol_pic);
  1697. pthread_mutex_unlock(&iic_mutex);
  1698. someBoardUpVoltage=true;
  1699. }
  1700. }
  1701. #endif
  1702. reset_work_data();
  1703. cgpu.CommandMode = 0;
  1704. cgpu.AsicType = ASIC_TYPE;
  1705. cgpu.asicNum = conf.asicNum;
  1706. cgpu.real_asic_num = CHAIN_ASIC_NUM;
  1707. cgpu.core_num = conf.core;
  1708. pthread_mutex_lock(&opencore_readtemp_mutex);
  1709. FOR_LOOP_CHAIN
  1710. {
  1711. if(cgpu.chain_exist[i]==0)
  1712. continue;
  1713. cgpu.chain_asic_num[i]=getChainAsicNum(i);
  1714. if(chain_need_opencore[i])
  1715. {
  1716. sprintf(logstr,"do open core on Chain[%d]...\n",i);
  1717. writeLogFile(logstr);
  1718. open_core_one_chain(i,true);
  1719. sprintf(logstr,"Done open core on Chain[%d]!\n",i);
  1720. writeLogFile(logstr);
  1721. }
  1722. }
  1723. pthread_mutex_unlock(&opencore_readtemp_mutex);
  1724. // before the first time for sending work, reset the FPGA's nonce fifo
  1725. if(!gBegin_get_nonce)
  1726. {
  1727. //printf("\n--- clear nonce fifo before send work\n");
  1728. printf("clement2 set_nonce_fifo_interrupt\n");
  1729. set_nonce_fifo_interrupt(get_nonce_fifo_interrupt() | FLUSH_NONCE3_FIFO);
  1730. gBegin_get_nonce = true;
  1731. }
  1732. FOR_LOOP_CHAIN
  1733. {
  1734. if(cgpu.chain_exist[i]==0)
  1735. continue;
  1736. sprintf(logstr,"start send works on chain[%d]\n",i);
  1737. writeLogFile(logstr);
  1738. StartSendFlag[i]=true;
  1739. }
  1740. send_func_all();
  1741. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  1742. {
  1743. if(cgpu.chain_exist[i]==0)
  1744. continue;
  1745. sprintf(logstr,"wait recv nonce on chain[%d]\n",i);
  1746. writeLogFile(logstr);
  1747. last_recv_num=0;
  1748. wait_count=0;
  1749. while(wait_count < RECV_WAIT_TIMEOUT && valid_nonce_num[i]<chain_ValidNonce[i])
  1750. {
  1751. if(last_recv_num!=valid_nonce_num[i])
  1752. {
  1753. wait_count=0;
  1754. last_recv_num=valid_nonce_num[i];
  1755. }
  1756. else wait_count++;
  1757. usleep(100000);
  1758. }
  1759. }
  1760. gBegin_get_nonce=false;
  1761. start_receive=false;
  1762. FOR_LOOP_CHAIN
  1763. {
  1764. if(cgpu.chain_exist[i]==0)
  1765. continue;
  1766. sprintf(logstr,"get nonces on chain[%d]\n",i);
  1767. writeLogFile(logstr);
  1768. result = get_result(i, chain_PassCount[i], chain_ValidNonce[i]);
  1769. }
  1770. result_flag=true;
  1771. FOR_LOOP_CHAIN
  1772. {
  1773. if(cgpu.chain_exist[i]==0)
  1774. continue;
  1775. if(last_all_core_opened(i))
  1776. {
  1777. sprintf(logstr,"chain[%d]: All chip cores are opened OK!\n",i);
  1778. writeLogFile(logstr);
  1779. chain_need_opencore[i]=false;
  1780. isChainAllCoresOpened[i]=true;
  1781. }
  1782. else
  1783. {
  1784. sprintf(logstr,"chain[%d]: some chip cores are not opened FAILED!\n",i);
  1785. writeLogFile(logstr);
  1786. chain_need_opencore[i]=true; // next time , force to re-open core again if open core failed!
  1787. isChainAllCoresOpened[i]=false;
  1788. }
  1789. if(last_all_pass(i))
  1790. {
  1791. sprintf(logstr,"Test Patten on chain[%d]: OK!\n",i);
  1792. writeLogFile(logstr);
  1793. }
  1794. else
  1795. {
  1796. result_flag=false;
  1797. sprintf(logstr,"Test Patten on chain[%d]: FAILED!\n",i);
  1798. writeLogFile(logstr);
  1799. #ifndef T9_18
  1800. #ifdef CHECK_ALLNONCE_ADD_VOLTAGE_USERMODE
  1801. if(readRestartNum()>0 && isChainEnough()) // up voltage is not suitable for T9+
  1802. {
  1803. sprintf(logstr,"Try to add voltage on chain[%d]...\n",i);
  1804. writeLogFile(logstr);
  1805. vol_value=getVoltageLimitedFromHashrate(GetTotalRate());
  1806. if(test_times>=PRE_HEAT_TEST_COUNT-1 && chain_vol_value[i]<vol_value) // we will set voltage to the highest voltage for the last chance on test patten
  1807. {
  1808. chain_vol_value[i]=vol_value;
  1809. sprintf(logstr,"will set the voltage limited on chain[%d], change voltage=%d\n",i,chain_vol_value[i]);
  1810. writeLogFile(logstr);
  1811. vol_pic=getPICvoltageFromValue(chain_vol_value[i]);
  1812. sprintf(logstr,"now set pic voltage=%d on chain[%d]\n",vol_pic,i);
  1813. writeLogFile(logstr);
  1814. pthread_mutex_lock(&iic_mutex);
  1815. set_pic_voltage(i, vol_pic);
  1816. pthread_mutex_unlock(&iic_mutex);
  1817. someBoardUpVoltage=true;
  1818. }
  1819. else if(chain_vol_value[i]+10<=vol_value)
  1820. {
  1821. chain_vol_value[i]+=10;
  1822. sprintf(logstr,"Can add 0.1V on chain[%d], change voltage=%d\n",i,chain_vol_value[i]);
  1823. writeLogFile(logstr);
  1824. vol_pic=getPICvoltageFromValue(chain_vol_value[i]);
  1825. sprintf(logstr,"now set pic voltage=%d on chain[%d]\n",vol_pic,i);
  1826. writeLogFile(logstr);
  1827. pthread_mutex_lock(&iic_mutex);
  1828. set_pic_voltage(i, vol_pic);
  1829. pthread_mutex_unlock(&iic_mutex);
  1830. someBoardUpVoltage=true;
  1831. }
  1832. }
  1833. #endif
  1834. #endif
  1835. search_freq_result[i]=false;
  1836. }
  1837. }
  1838. return result_flag;
  1839. }
  1840. static bool showLogToKernelLog=true;
  1841. static void writeLogFile(char *logstr)
  1842. {
  1843. if(showLogToKernelLog)
  1844. writeInitLogFile(logstr);
  1845. }
  1846. static int init_once=1;
  1847. bool clement_doTestBoard(bool showlog)
  1848. {
  1849. int run_count = 0;
  1850. int ret, i,j,k;
  1851. char logstr[1024];
  1852. bool doOnce=true;
  1853. int wait_count;
  1854. int rebootTestNum; // for searching process, to reboot 3times to check hashrate.
  1855. int restartMinerNum; // the number of chances to reboot miner, for sometime hashrate is low when first startup.
  1856. bool result_flag;
  1857. showLogToKernelLog=showlog;
  1858. if(init_once>0)
  1859. {
  1860. ret = cgpu_init();
  1861. if(ret < 0)
  1862. {
  1863. printf("cgpu_init Error!\n");
  1864. return false;
  1865. }
  1866. ret = configMiner();
  1867. if(ret < 0)
  1868. {
  1869. printf("configMiner Error!\n");
  1870. return false;
  1871. }
  1872. init_once=0;
  1873. printf("single board test start\n");
  1874. Conf.DataCount=conf.dataCount=TESTMODE_PATTEN_NUM; // fixed to 114
  1875. Conf.PassCount1=conf.passCount1=TESTMODE_PATTEN_NUM;
  1876. Conf.PassCount2=conf.passCount2=TESTMODE_PATTEN_NUM;
  1877. Conf.PassCount3=conf.passCount3=TESTMODE_PATTEN_NUM;
  1878. Conf.ValidNonce1=conf.ValidNonce1=TESTMODE_NONCE_NUM;
  1879. Conf.ValidNonce2=conf.ValidNonce2=TESTMODE_NONCE_NUM;
  1880. Conf.ValidNonce3=conf.ValidNonce3=TESTMODE_NONCE_NUM;
  1881. ExitFlag=false;
  1882. receiveExit=false;
  1883. pthread_create(&cgpu.receive_id, NULL, receive_func, &cgpu);
  1884. for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  1885. {
  1886. StartSendFlag[i]=false;
  1887. }
  1888. }
  1889. for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  1890. {
  1891. testModeOKCounter[i]=0;
  1892. for(j=0; j<256; j++)
  1893. {
  1894. last_result[i][j] = 0 ;
  1895. last_result_opencore[i][j] = 0 ;
  1896. }
  1897. // force to test all boards at any time
  1898. chain_vol_value[i]=0;
  1899. chain_vol_final[i]=0;
  1900. chain_vol_added[i]=0;
  1901. search_freq_result[i]=true;
  1902. chain_DataCount[i]=TESTMODE_PATTEN_NUM; // when seaching base freq, we use 8*144 patten on chip
  1903. chain_ValidNonce[i]=TESTMODE_NONCE_NUM;
  1904. chain_PassCount[i]=TESTMODE_PATTEN_NUM;
  1905. chain_need_opencore[i]=false; // init must be false, because chip cores are opened in driver-btm-c5.c
  1906. }
  1907. k=0;
  1908. do
  1909. {
  1910. k++;
  1911. sprintf(logstr,"do heat board 8xPatten for %d times\n",k);
  1912. writeLogFile(logstr);
  1913. for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  1914. {
  1915. for(j=0; j<256; j++)
  1916. {
  1917. last_result[i][j] = 0 ;
  1918. last_result_opencore[i][j] = 0 ;
  1919. }
  1920. // force to test all boards at any time
  1921. search_freq_result[i]=true;
  1922. chain_DataCount[i]=TESTMODE_PATTEN_NUM; // when seaching base freq, we use 8*144 patten on chip
  1923. chain_ValidNonce[i]=TESTMODE_NONCE_NUM;
  1924. chain_PassCount[i]=TESTMODE_PATTEN_NUM;
  1925. }
  1926. result_flag=doTestBoard(k);
  1927. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  1928. {
  1929. if(cgpu.chain_exist[i]==0)
  1930. continue;
  1931. if(search_freq_result[i])
  1932. {
  1933. testModeOKCounter[i]++;
  1934. }
  1935. }
  1936. if(result_flag) // if test paten OK, we stop preheat
  1937. break;
  1938. }
  1939. while(k<PRE_HEAT_TEST_COUNT);
  1940. if(isAllChainChipCoreOpened())
  1941. {
  1942. result_flag=true;
  1943. someBoardUpVoltage=false; // if all chip core opened, then we do not re-init again!!!
  1944. }
  1945. else
  1946. {
  1947. result_flag=false;
  1948. someBoardUpVoltage=true;
  1949. }
  1950. set_PWM(100); // when exit preheat, set full speed of fan
  1951. return result_flag;
  1952. }
  1953. bool clement_doTestBoardOnce(bool showlog)
  1954. {
  1955. int run_count = 0;
  1956. int ret, i,j;
  1957. char logstr[1024];
  1958. bool doOnce=true;
  1959. int wait_count;
  1960. int rebootTestNum; // for searching process, to reboot 3times to check hashrate.
  1961. int restartMinerNum; // the number of chances to reboot miner, for sometime hashrate is low when first startup.
  1962. showLogToKernelLog=showlog;
  1963. if(init_once>0)
  1964. {
  1965. ret = cgpu_init();
  1966. if(ret < 0)
  1967. {
  1968. printf("cgpu_init Error!\n");
  1969. return false;
  1970. }
  1971. ret = configMiner();
  1972. if(ret < 0)
  1973. {
  1974. printf("configMiner Error!\n");
  1975. return false;
  1976. }
  1977. init_once=0;
  1978. printf("single board test start\n");
  1979. Conf.DataCount=conf.dataCount=TESTMODE_PATTEN_NUM; // fixed to 114
  1980. Conf.PassCount1=conf.passCount1=TESTMODE_PATTEN_NUM;
  1981. Conf.PassCount2=conf.passCount2=TESTMODE_PATTEN_NUM;
  1982. Conf.PassCount3=conf.passCount3=TESTMODE_PATTEN_NUM;
  1983. Conf.ValidNonce1=conf.ValidNonce1=TESTMODE_NONCE_NUM;
  1984. Conf.ValidNonce2=conf.ValidNonce2=TESTMODE_NONCE_NUM;
  1985. Conf.ValidNonce3=conf.ValidNonce3=TESTMODE_NONCE_NUM;
  1986. ExitFlag=false;
  1987. receiveExit=false;
  1988. pthread_create(&cgpu.receive_id, NULL, receive_func, &cgpu);
  1989. for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  1990. {
  1991. StartSendFlag[i]=false;
  1992. }
  1993. }
  1994. for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
  1995. {
  1996. testModeOKCounter[i]=0;
  1997. for(j=0; j<256; j++)
  1998. {
  1999. last_result[i][j] = 0 ;
  2000. last_result_opencore[i][j] = 0 ;
  2001. }
  2002. // force to test all boards at any time
  2003. chain_vol_value[i]=0;
  2004. chain_vol_final[i]=0;
  2005. chain_vol_added[i]=0;
  2006. search_freq_result[i]=true;
  2007. chain_DataCount[i]=TESTMODE_PATTEN_NUM; // when seaching base freq, we use 8*144 patten on chip
  2008. chain_ValidNonce[i]=TESTMODE_NONCE_NUM;
  2009. chain_PassCount[i]=TESTMODE_PATTEN_NUM;
  2010. chain_need_opencore[i]=false; // init must be false, because chip cores are opened in driver-btm-c5.c
  2011. }
  2012. doTestBoard(0);
  2013. for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
  2014. {
  2015. if(cgpu.chain_exist[i]==0)
  2016. continue;
  2017. if(search_freq_result[i])
  2018. {
  2019. testModeOKCounter[i]++;
  2020. }
  2021. }
  2022. set_PWM(100); // when exit preheat, set full speed of fan
  2023. return true;
  2024. }