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