/ driver-bitmain.c
driver-bitmain.c
1 /* 2 * Copyright 2012-2013 Lingchao Xu <lingchao.xu@bitmaintech.com> 3 * Copyright 2014-2015 Andrew Smith 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 11 #include "config.h" 12 #include "compat.h" 13 #include "miner.h" 14 15 #ifndef LINUX 16 17 #ifdef USE_ANT_S1 18 static void ants1_detect(__maybe_unused bool hotplug) 19 { 20 } 21 #endif 22 #ifdef USE_ANT_S2 23 #ifdef USE_ANT_S3 24 static void ants3_detect(__maybe_unused bool hotplug) 25 { 26 } 27 #else 28 static void ants2_detect(__maybe_unused bool hotplug) 29 { 30 } 31 #endif 32 #endif 33 34 #else // LINUX 35 36 #include "elist.h" 37 #if (defined(USE_ANT_S1) || defined(USE_ANT_S3)) 38 #include "usbutils.h" 39 #else 40 #define C_BITMAIN_READ 0 41 #define C_BITMAIN_DATA_RXSTATUS 0 42 #endif 43 #include "driver-bitmain.h" 44 #include "hexdump.c" 45 #include "util.h" 46 #include <fcntl.h> 47 #include <unistd.h> 48 #include <math.h> 49 50 #ifdef USE_ANT_S1 51 #define ANTDRV ants1_drv 52 #else 53 #ifdef USE_ANT_S3 54 #define ANTDRV ants3_drv 55 #else 56 #define ANTDRV ants2_drv 57 #endif 58 #endif 59 60 #define BITMAIN_CALC_DIFF1 1 61 62 bool opt_bitmain_hwerror = false; 63 #ifdef USE_ANT_S2 64 bool opt_bitmain_checkall = false; 65 bool opt_bitmain_checkn2diff = false; 66 bool opt_bitmain_beeper = false; 67 bool opt_bitmain_tempoverctrl = false; 68 #endif 69 int opt_bitmain_temp = BITMAIN_TEMP_TARGET; 70 int opt_bitmain_workdelay = BITMAIN_WORK_DELAY; 71 int opt_bitmain_overheat = BITMAIN_TEMP_OVERHEAT; 72 int opt_bitmain_fan_min = BITMAIN_DEFAULT_FAN_MIN_PWM; 73 int opt_bitmain_fan_max = BITMAIN_DEFAULT_FAN_MAX_PWM; 74 bool opt_bitmain_auto; 75 76 static int option_offset = -1; 77 78 #if (defined(USE_ANT_S1) || defined(USE_ANT_S3)) 79 static unsigned char bit_swap_table[256] = 80 { 81 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 82 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0, 83 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, 84 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, 85 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, 86 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, 87 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, 88 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc, 89 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, 90 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, 91 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea, 92 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, 93 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, 94 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6, 95 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, 96 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, 97 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1, 98 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1, 99 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, 100 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9, 101 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, 102 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, 103 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, 104 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, 105 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 106 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3, 107 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, 108 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, 109 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, 110 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, 111 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 112 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff, 113 }; 114 #define bitswap(x) (bit_swap_table[x]) 115 #else 116 #define bitswap(x) (x) 117 #endif 118 119 // -------------------------------------------------------------- 120 // CRC16 check table 121 // -------------------------------------------------------------- 122 const uint8_t chCRCHTalbe[] = // CRC high byte table 123 { 124 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 125 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 126 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 127 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 128 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 129 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 130 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 131 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 132 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 133 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 134 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 135 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 136 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 137 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 138 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 139 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 140 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 141 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 142 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 143 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 144 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 145 0x00, 0xC1, 0x81, 0x40 146 }; 147 148 const uint8_t chCRCLTalbe[] = // CRC low byte table 149 { 150 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 151 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 152 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 153 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 154 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, 155 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 156 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 157 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 158 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 159 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, 160 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 161 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 162 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 163 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 164 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, 165 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 166 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 167 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 168 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 169 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, 170 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 171 0x41, 0x81, 0x80, 0x40 172 }; 173 174 static uint16_t CRC16(const uint8_t* p_data, uint16_t w_len) 175 { 176 uint8_t chCRCHi = 0xFF; // CRC high byte initialize 177 uint8_t chCRCLo = 0xFF; // CRC low byte initialize 178 uint16_t wIndex = 0; // CRC cycling index 179 180 while (w_len--) { 181 wIndex = chCRCLo ^ *p_data++; 182 chCRCLo = chCRCHi ^ chCRCHTalbe[wIndex]; 183 chCRCHi = chCRCLTalbe[wIndex]; 184 } 185 return ((chCRCHi << 8) | chCRCLo); 186 } 187 188 static uint32_t num2bit(int num) 189 { 190 if (num < 0 || num > 31) 191 return 0; 192 else 193 return (((uint32_t)1) << (31 - num)); 194 } 195 196 #ifdef USE_ANT_S1 197 static bool get_options(int this_option_offset, int *baud, int *chain_num, 198 int *asic_num, int *timeout, int *frequency, 199 uint8_t *reg_data) 200 { 201 char buf[BUFSIZ+1]; 202 char *ptr, *comma, *colon, *colon2, *colon3, *colon4, *colon5; 203 size_t max; 204 int tmp; 205 206 if (opt_bitmain_options == NULL) 207 buf[0] = '\0'; 208 else { 209 // Always use the first set if more than one 210 ptr = opt_bitmain_options; 211 comma = strchr(ptr, ','); 212 if (comma) 213 *comma = '\0'; 214 max = strlen(ptr); 215 if (max > BUFSIZ) 216 max = BUFSIZ; 217 strncpy(buf, ptr, max); 218 buf[max] = '\0'; 219 } 220 221 if (!(*buf)) 222 return false; 223 224 colon = strchr(buf, ':'); 225 if (colon) 226 *(colon++) = '\0'; 227 228 tmp = atoi(buf); 229 switch (tmp) { 230 case 115200: 231 *baud = 115200; 232 break; 233 case 57600: 234 *baud = 57600; 235 break; 236 case 38400: 237 *baud = 38400; 238 break; 239 case 19200: 240 *baud = 19200; 241 break; 242 default: 243 quit(1, "Invalid bitmain-options for baud (%s) " 244 "must be 115200, 57600, 38400 or 19200", buf); 245 } 246 247 if (colon && *colon) { 248 colon2 = strchr(colon, ':'); 249 if (colon2) 250 *(colon2++) = '\0'; 251 252 if (*colon) { 253 tmp = atoi(colon); 254 if (tmp > 0) 255 *chain_num = tmp; 256 else { 257 quit(1, "Invalid bitmain-options for " 258 "chain_num (%s) must be 1 ~ %d", 259 colon, BITMAIN_DEFAULT_CHAIN_NUM); 260 } 261 } 262 263 if (colon2 && *colon2) { 264 colon3 = strchr(colon2, ':'); 265 if (colon3) 266 *(colon3++) = '\0'; 267 268 tmp = atoi(colon2); 269 if (tmp > 0 && tmp <= BITMAIN_DEFAULT_ASIC_NUM) 270 *asic_num = tmp; 271 else { 272 quit(1, "Invalid bitmain-options for " 273 "asic_num (%s) must be 1 ~ %d", 274 colon2, BITMAIN_DEFAULT_ASIC_NUM); 275 } 276 277 if (colon3 && *colon3) { 278 colon4 = strchr(colon3, ':'); 279 if (colon4) 280 *(colon4++) = '\0'; 281 282 tmp = atoi(colon3); 283 if (tmp > 0 && tmp <= 0xff) 284 *timeout = tmp; 285 else { 286 quit(1, "Invalid bitmain-options for " 287 "timeout (%s) must be 1 ~ %d", 288 colon3, 0xff); 289 } 290 if (colon4 && *colon4) { 291 colon5 = strchr(colon4, ':'); 292 if (colon5) 293 *(colon5++) = '\0'; 294 295 tmp = atoi(colon4); 296 if (tmp < BITMAIN_MIN_FREQUENCY || 297 tmp > BITMAIN_MAX_FREQUENCY) { 298 quit(1, "Invalid bitmain-options for frequency," 299 " must be %d <= frequency <= %d", 300 BITMAIN_MIN_FREQUENCY, 301 BITMAIN_MAX_FREQUENCY); 302 } else 303 *frequency = tmp; 304 305 if (colon5 && *colon5) { 306 if (strlen(colon5) > 8 || 307 strlen(colon5)%2 != 0 || 308 strlen(colon5)/2 == 0) { 309 quit(1, "Invalid bitmain-options for" 310 " reg data, must be hex now: %s", 311 colon5); 312 } 313 memset(reg_data, 0, 4); 314 if (!hex2bin(reg_data, colon5, strlen(colon5)/2)) { 315 quit(1, "Invalid bitmain-options for reg" 316 " data, hex2bin error now: %s", 317 colon5); 318 } 319 } 320 } 321 } 322 } 323 } 324 return true; 325 } 326 #else 327 static bool get_options(__maybe_unused int this_option_offset, int *baud, 328 int *chain_num, int *asic_num) 329 { 330 char buf[BUFSIZ+1]; 331 char *ptr, *comma, *colon, *colon2, *colon3; 332 size_t max; 333 int tmp; 334 335 if (opt_bitmain_options == NULL) 336 buf[0] = '\0'; 337 else { 338 // Always use the first set if more than one 339 ptr = opt_bitmain_options; 340 comma = strchr(ptr, ','); 341 if (comma) 342 *comma = '\0'; 343 max = strlen(ptr); 344 if (max > BUFSIZ) 345 max = BUFSIZ; 346 strncpy(buf, ptr, max); 347 buf[max] = '\0'; 348 } 349 350 if (!(*buf)) 351 return false; 352 353 colon = strchr(buf, ':'); 354 if (colon) 355 *(colon++) = '\0'; 356 357 tmp = atoi(buf); 358 switch (tmp) { 359 case 115200: 360 *baud = 115200; 361 break; 362 case 57600: 363 *baud = 57600; 364 break; 365 case 38400: 366 *baud = 38400; 367 break; 368 case 19200: 369 *baud = 19200; 370 break; 371 default: 372 quit(1, "Invalid bitmain-options for baud (%s) " 373 "must be 115200, 57600, 38400 or 19200", buf); 374 } 375 376 if (colon && *colon) { 377 colon2 = strchr(colon, ':'); 378 if (colon2) 379 *(colon2++) = '\0'; 380 381 if (*colon) { 382 tmp = atoi(colon); 383 if (tmp > 0) 384 *chain_num = tmp; 385 else { 386 quit(1, "Invalid bitmain-options for " 387 "chain_num (%s) must be 1 ~ %d", 388 colon, BITMAIN_DEFAULT_CHAIN_NUM); 389 } 390 } 391 392 if (colon2 && *colon2) { 393 colon3 = strchr(colon2, ':'); 394 if (colon3) 395 *(colon3++) = '\0'; 396 397 tmp = atoi(colon2); 398 if (tmp > 0 && tmp <= BITMAIN_DEFAULT_ASIC_NUM) 399 *asic_num = tmp; 400 else { 401 quit(1, "Invalid bitmain-options for " 402 "asic_num (%s) must be 1 ~ %d", 403 colon2, BITMAIN_DEFAULT_ASIC_NUM); 404 } 405 } 406 } 407 return true; 408 } 409 #endif 410 411 #ifdef USE_ANT_S1 412 static int bitmain_set_txconfig(struct bitmain_txconfig_token *bm, 413 uint8_t reset, uint8_t fan_eft, uint8_t timeout_eft, uint8_t frequency_eft, 414 uint8_t voltage_eft, uint8_t chain_check_time_eft, uint8_t chip_config_eft, 415 uint8_t hw_error_eft, uint8_t chain_num, uint8_t asic_num, 416 uint8_t fan_pwm_data, uint8_t timeout_data, 417 uint16_t frequency, uint8_t voltage, uint8_t chain_check_time, 418 uint8_t chip_address, uint8_t reg_address, uint8_t * reg_data) 419 #else 420 static int bitmain_set_txconfig(struct bitmain_txconfig_token *bm, 421 uint8_t reset, uint8_t fan_eft, uint8_t timeout_eft, uint8_t frequency_eft, 422 uint8_t voltage_eft, uint8_t chain_check_time_eft, uint8_t chip_config_eft, 423 uint8_t hw_error_eft, uint8_t beeper_ctrl, uint8_t temp_over_ctrl, 424 uint8_t chain_num, uint8_t asic_num, 425 uint8_t fan_pwm_data, uint8_t timeout_data, 426 uint16_t frequency, uint8_t *voltage, uint8_t chain_check_time, 427 uint8_t chip_address, uint8_t reg_address, uint8_t * reg_data) 428 #endif 429 { 430 uint16_t crc = 0; 431 int datalen = 0; 432 #ifdef USE_ANT_S2 433 uint8_t version = 0; 434 #endif 435 uint8_t *sendbuf = (uint8_t *)bm; 436 if (unlikely(!bm)) { 437 applog(LOG_WARNING, "%s: %s() bm is null", ANTDRV.dname, __func__); 438 return -1; 439 } 440 441 if (unlikely(timeout_data <= 0 || asic_num <= 0 || chain_num <= 0)) { 442 applog(LOG_WARNING, "%s: %s() parameter invalid" 443 " timeout_data(%d) asic_num(%d) chain_num(%d)", 444 ANTDRV.dname, __func__, 445 (int)timeout_data, (int)asic_num, (int)chain_num); 446 return -1; 447 } 448 449 datalen = sizeof(struct bitmain_txconfig_token); 450 memset(bm, 0, datalen); 451 452 bm->token_type = BITMAIN_TOKEN_TYPE_TXCONFIG; 453 #ifdef USE_ANT_S1 454 bm->length = datalen-2; 455 #else 456 bm->version = version; 457 bm->length = datalen-4; 458 bm->length = htole16(bm->length); 459 #endif 460 bm->reset = reset; 461 bm->fan_eft = fan_eft; 462 bm->timeout_eft = timeout_eft; 463 bm->frequency_eft = frequency_eft; 464 bm->voltage_eft = voltage_eft; 465 bm->chain_check_time_eft = chain_check_time_eft; 466 bm->chip_config_eft = chip_config_eft; 467 bm->hw_error_eft = hw_error_eft; 468 469 #ifdef USE_ANT_S1 470 sendbuf[2] = bitswap(sendbuf[2]); 471 #else 472 bm->beeper_ctrl = beeper_ctrl; 473 bm->temp_over_ctrl = temp_over_ctrl; 474 475 sendbuf[4] = bitswap(sendbuf[4]); 476 sendbuf[5] = bitswap(sendbuf[5]); 477 #endif 478 479 bm->chain_num = chain_num; 480 bm->asic_num = asic_num; 481 bm->fan_pwm_data = fan_pwm_data; 482 bm->timeout_data = timeout_data; 483 484 bm->frequency = htole16(frequency); 485 #ifdef USE_ANT_S1 486 bm->voltage = voltage; 487 #else 488 bm->voltage[0] = voltage[0]; 489 bm->voltage[1] = voltage[1]; 490 #endif 491 bm->chain_check_time = chain_check_time; 492 493 memcpy(bm->reg_data, reg_data, 4); 494 bm->chip_address = chip_address; 495 bm->reg_address = reg_address; 496 497 crc = CRC16((uint8_t *)bm, datalen-2); 498 bm->crc = htole16(crc); 499 500 #ifdef USE_ANT_S1 501 applogsiz(LOG_DEBUG, 512, "%s: %s() reset(%d) faneft(%d) touteft(%d) freqeft(%d)" 502 " volteft(%d) chainceft(%d) chipceft(%d) hweft(%d) mnum(%d)" 503 " anum(%d) fanpwmdata(%d) toutdata(%d) freq(%d) volt(%d)" 504 " chainctime(%d) regdata(%02x%02x%02x%02x) chipaddr(%02x)" 505 " regaddr(%02x) crc(%04x)", 506 ANTDRV.dname, __func__, 507 (int)reset, (int)fan_eft, (int)timeout_eft, (int)frequency_eft, 508 (int)voltage_eft, (int)chain_check_time_eft, (int)chip_config_eft, 509 (int)hw_error_eft, (int)chain_num, (int)asic_num, (int)fan_pwm_data, 510 (int)timeout_data, (int)frequency, (int)voltage, (int)chain_check_time, 511 (int)reg_data[0], (int)reg_data[1], (int)reg_data[2], (int)reg_data[3], 512 (int)chip_address, (int)reg_address, (int)crc); 513 #else 514 applogsiz(LOG_DEBUG, 512, "%s: %s() v(%d) reset(%d) faneft(%d) touteft(%d) freqeft(%d)" 515 " volteft(%d) chainceft(%d) chipceft(%d) hweft(%d)" 516 " beepctrl(%d) toverctl(%d) mnum(%d)" 517 " anum(%d) fanpwmdata(%d) toutdata(%d) freq(%d) volt(%02x%02x)" 518 " chainctime(%d) regdata(%02x%02x%02x%02x) chipaddr(%02x)" 519 " regaddr(%02x) crc(%04x)", 520 ANTDRV.dname, __func__, 521 (int)version, (int)reset, (int)fan_eft, (int)timeout_eft, 522 (int)frequency_eft, (int)voltage_eft, (int)chain_check_time_eft, 523 (int)chip_config_eft, (int)hw_error_eft, (int)beeper_ctrl, 524 (int)temp_over_ctrl, (int)chain_num, (int)asic_num, (int)fan_pwm_data, 525 (int)timeout_data, (int)frequency, (int)voltage[0], (int)voltage[1], 526 (int)chain_check_time, (int)reg_data[0], (int)reg_data[1], 527 (int)reg_data[2], (int)reg_data[3], (int)chip_address, 528 (int)reg_address, (int)crc); 529 #endif 530 531 return datalen; 532 } 533 534 static int bitmain_set_txtask(struct bitmain_info *info, uint8_t *sendbuf, 535 unsigned int *last_work_block, int *sentcount) 536 { 537 uint16_t crc = 0; 538 uint32_t wid = 0; 539 int datalen = 0; 540 uint8_t new_block = 0; 541 //char *ob_hex = NULL; 542 struct bitmain_txtask_token *bm = (struct bitmain_txtask_token *)sendbuf; 543 int cursentcount = 0; 544 #ifdef USE_ANT_S2 545 uint8_t version = 0; 546 int diffbits, lowestdiffbits = -1; 547 double workdiff; 548 #endif 549 K_ITEM *witem; 550 551 *sentcount = 0; 552 553 if (unlikely(!bm)) { 554 applog(LOG_WARNING, "%s: %s() bm is null", ANTDRV.dname, __func__); 555 return -1; 556 } 557 memset(bm, 0, sizeof(struct bitmain_txtask_token)); 558 559 bm->token_type = BITMAIN_TOKEN_TYPE_TXTASK; 560 #ifdef USE_ANT_S2 561 bm->version = version; 562 if (info->wbuild->head) 563 quithere(1, "%s: %s() wbuild wasn't empty", ANTDRV.dname, __func__); 564 #endif 565 566 datalen = 10; 567 applog(LOG_DEBUG, "%s: send work count %d", ANTDRV.dname, info->work_ready->count); 568 while (info->work_ready->count) { 569 witem = k_unlink_tail(info->work_ready); 570 if (DATAW(witem)->work->work_block > *last_work_block) { 571 applog(LOG_ERR, "%s: send task new block %d old(%d)", 572 ANTDRV.dname, 573 DATAW(witem)->work->work_block, *last_work_block); 574 new_block = 1; 575 *last_work_block = DATAW(witem)->work->work_block; 576 } 577 wid = DATAW(witem)->wid; 578 bm->works[cursentcount].work_id = htole32(wid); 579 applog(LOG_DEBUG, "%s: send task work id:%"PRIu32" %"PRIu32, 580 ANTDRV.dname, 581 wid, bm->works[cursentcount].work_id); 582 memcpy(bm->works[cursentcount].midstate, DATAW(witem)->work->midstate, 32); 583 memcpy(bm->works[cursentcount].data2, DATAW(witem)->work->data + 64, 12); 584 585 cursentcount++; 586 587 #ifdef USE_ANT_S1 588 k_add_head(info->work_list, witem); 589 #else 590 k_add_head(info->wbuild, witem); 591 592 diffbits = (int)floor(log2(DATAW(witem)->work->sdiff)); 593 if (diffbits < 0) 594 diffbits = 0; 595 // Limit to 4096 so solo mining has reasonable mining stats 596 if (diffbits > 12) 597 diffbits = 12; 598 599 // Must use diffbits <= all work being sent 600 if (lowestdiffbits == -1 || lowestdiffbits > diffbits) 601 lowestdiffbits = diffbits; 602 #endif 603 604 } 605 if (cursentcount <= 0) { 606 applog(LOG_ERR, "%s: send work count %d", ANTDRV.dname, cursentcount); 607 return 0; 608 } 609 610 #ifdef USE_ANT_S2 611 workdiff = pow(2.0, (double)lowestdiffbits); 612 witem = info->wbuild->head; 613 while (witem) { 614 DATAW(witem)->work->device_diff = workdiff; 615 witem = witem->next; 616 } 617 k_list_transfer_to_head(info->wbuild, info->work_list); 618 #endif 619 620 datalen += 48*cursentcount; 621 622 bm->length = datalen-4; 623 bm->length = htole16(bm->length); 624 //len = datalen-3; 625 //len = htole16(len); 626 //memcpy(sendbuf+1, &len, 2); 627 bm->new_block = new_block; 628 #ifdef USE_ANT_S2 629 bm->diff = lowestdiffbits; 630 #endif 631 632 sendbuf[4] = bitswap(sendbuf[4]); 633 634 applog(LOG_DEBUG, "%s: TxTask Token: %d %d %02x%02x%02x%02x%02x%02x", 635 ANTDRV.dname, 636 datalen, bm->length, 637 sendbuf[0], sendbuf[1], sendbuf[2], 638 sendbuf[3], sendbuf[4], sendbuf[5]); 639 640 *sentcount = cursentcount; 641 642 crc = CRC16(sendbuf, datalen-2); 643 crc = htole16(crc); 644 memcpy(sendbuf+datalen-2, &crc, 2); 645 646 #ifdef USE_ANT_S1 647 applog(LOG_DEBUG, "%s: TxTask Token: new_block(%d) work_num(%d)" 648 " crc(%04x)", 649 ANTDRV.dname, 650 (int)new_block, cursentcount, (int)crc); 651 #else 652 applog(LOG_DEBUG, "%s: TxTask Token: v(%d) new_block(%d)" 653 " diff(%d work:%f) work_num(%d) crc(%04x)", 654 ANTDRV.dname, 655 (int)version, (int)new_block, lowestdiffbits, workdiff, 656 cursentcount, (int)crc); 657 #endif 658 applog(LOG_DEBUG, "%s: TxTask Token: %d %d %02x%02x%02x%02x%02x%02x", 659 ANTDRV.dname, 660 datalen, bm->length, 661 sendbuf[0], sendbuf[1], sendbuf[2], 662 sendbuf[3], sendbuf[4], sendbuf[5]); 663 664 return datalen; 665 } 666 667 static int bitmain_set_rxstatus(struct bitmain_rxstatus_token *bm, 668 uint8_t chip_status_eft, uint8_t detect_get, uint8_t chip_address, uint8_t reg_address) 669 { 670 uint16_t crc = 0; 671 int datalen = 0; 672 uint8_t *sendbuf = (uint8_t *)bm; 673 #ifdef USE_ANT_S2 674 uint8_t version = 0; 675 #endif 676 677 if (unlikely(!bm)) { 678 applog(LOG_WARNING, "%s: %s() bm is null", ANTDRV.dname, __func__); 679 return -1; 680 } 681 682 datalen = sizeof(struct bitmain_rxstatus_token); 683 memset(bm, 0, datalen); 684 685 bm->token_type = BITMAIN_TOKEN_TYPE_RXSTATUS; 686 #ifdef USE_ANT_S1 687 bm->length = datalen-2; 688 #else 689 bm->version = version; 690 bm->length = datalen-4; 691 bm->length = htole16(bm->length); 692 #endif 693 694 bm->chip_status_eft = chip_status_eft; 695 bm->detect_get = detect_get; 696 697 #ifdef USE_ANT_S1 698 sendbuf[2] = bitswap(sendbuf[2]); 699 #else 700 sendbuf[4] = bitswap(sendbuf[4]); 701 #endif 702 703 bm->chip_address = chip_address; 704 bm->reg_address = reg_address; 705 706 crc = CRC16((uint8_t *)bm, datalen-2); 707 bm->crc = htole16(crc); 708 709 #ifdef USE_ANT_S1 710 applog(LOG_DEBUG, "%s: RxStatus Token: chip_status_eft(%d) detect_get(%d)" 711 " chip_address(%02x) reg_address(%02x) crc(%04x)", 712 ANTDRV.dname, 713 (int)chip_status_eft, (int)detect_get, chip_address, reg_address, crc); 714 #else 715 applog(LOG_DEBUG, "%s: RxStatus Token: v(%d) chip_status_eft(%d) detect_get(%d)" 716 " chip_address(%02x) reg_address(%02x) crc(%04x)", 717 ANTDRV.dname, 718 (int)version, (int)chip_status_eft, (int)detect_get, 719 chip_address, reg_address, crc); 720 #endif 721 722 return datalen; 723 } 724 725 static int bitmain_parse_rxstatus(const uint8_t * data, int datalen, struct bitmain_rxstatus_data *bm) 726 { 727 uint16_t crc = 0; 728 int i = 0; 729 #ifdef USE_ANT_S2 730 uint8_t version = 0; 731 int j = 0; 732 int asic_num = 0; 733 int dataindex = 0; 734 #endif 735 736 if (unlikely(!bm)) { 737 applog(LOG_ERR, "%s: %s() bm is null", ANTDRV.dname, __func__); 738 return -1; 739 } 740 if (unlikely(!data || datalen <= 0)) { 741 applog(LOG_ERR, "%s: %s() parameter invalid data is null" 742 " or datalen(%d) error", 743 ANTDRV.dname, __func__, datalen); 744 return -1; 745 } 746 747 #ifdef USE_ANT_S1 748 memcpy(bm, data, sizeof(struct bitmain_rxstatus_data)); 749 if (bm->data_type != BITMAIN_DATA_TYPE_RXSTATUS) { 750 applog(LOG_ERR, "%s: %s() datatype(%02x) error", 751 ANTDRV.dname, __func__, 752 bm->data_type); 753 return -1; 754 } 755 if (bm->length+2 != datalen) { 756 applog(LOG_ERR, "%s: %s() length(%d) datalen(%d) error", 757 ANTDRV.dname, __func__, 758 bm->length, datalen); 759 return -1; 760 } 761 crc = CRC16(data, datalen-2); 762 memcpy(&(bm->crc), data+datalen-2, 2); 763 bm->crc = htole16(bm->crc); 764 if (crc != bm->crc) { 765 applog(LOG_ERR, "%s: %s() check crc(%d)" 766 " != bm crc(%d) datalen(%d)", 767 ANTDRV.dname, __func__, 768 crc, bm->crc, datalen); 769 return -1; 770 } 771 bm->fifo_space = htole32(bm->fifo_space); 772 bm->nonce_error = htole32(bm->nonce_error); 773 if (bm->chain_num*5 + bm->temp_num + bm->fan_num + 22 != datalen) { 774 applog(LOG_ERR, "%s: %s() chain_num(%d) temp_num(%d)" 775 " fan_num(%d) not match datalen(%d)", 776 ANTDRV.dname, __func__, 777 bm->chain_num, bm->temp_num, bm->fan_num, datalen); 778 return -1; 779 } 780 if (bm->chain_num > BITMAIN_MAX_CHAIN_NUM) { 781 applog(LOG_ERR, "%s: %s() chain_num=%d error", 782 ANTDRV.dname, __func__, 783 bm->chain_num); 784 return -1; 785 } 786 if (bm->chain_num > 0) { 787 memcpy(bm->chain_asic_status, data+20, bm->chain_num*4); 788 memcpy(bm->chain_asic_num, data+20+bm->chain_num*4, bm->chain_num); 789 } 790 for (i = 0; i < bm->chain_num; i++) 791 bm->chain_asic_status[i] = htole32(bm->chain_asic_status[i]); 792 if (bm->temp_num > 0) 793 memcpy(bm->temp, data+20+bm->chain_num*5, bm->temp_num); 794 if (bm->fan_num > 0) 795 memcpy(bm->fan, data+20+bm->chain_num*5+bm->temp_num, bm->fan_num); 796 applog(LOG_DEBUG, "%s: RxStatus Data chipvalueeft(%d) version(%d) fifospace(%d)" 797 " regvalue(%d) chainnum(%d) tempnum(%d) fannum(%d) crc(%04x)", 798 ANTDRV.dname, 799 bm->chip_value_eft, bm->version, bm->fifo_space, bm->reg_value, 800 bm->chain_num, bm->temp_num, bm->fan_num, bm->crc); 801 applog(LOG_DEBUG, "%s: RxStatus Data chain info:", ANTDRV.dname); 802 for (i = 0; i < bm->chain_num; i++) { 803 applog(LOG_DEBUG, "%s: RxStatus Data chain(%d) asic num=%d asic_status=%08x", 804 ANTDRV.dname, 805 i+1, bm->chain_asic_num[i], bm->chain_asic_status[i]); 806 } 807 #else // USE_ANT_S2 808 memset(bm, 0, sizeof(struct bitmain_rxstatus_data)); 809 memcpy(bm, data, 28); 810 if (bm->data_type != BITMAIN_DATA_TYPE_RXSTATUS) { 811 applog(LOG_ERR, "%s: %s() datatype(%02x) error", 812 ANTDRV.dname, __func__, 813 bm->data_type); 814 return -1; 815 } 816 if (bm->version != version) { 817 applog(LOG_ERR, "%s: %s() version(%02x) error", 818 ANTDRV.dname, __func__, 819 bm->version); 820 return -1; 821 } 822 bm->length = htole16(bm->length); 823 if (bm->length+4 != datalen) { 824 applog(LOG_ERR, "%s: %s() length(%d) datalen(%d) error", 825 ANTDRV.dname, __func__, 826 bm->length, datalen); 827 return -1; 828 } 829 crc = CRC16(data, datalen-2); 830 memcpy(&(bm->crc), data+datalen-2, 2); 831 bm->crc = htole16(bm->crc); 832 if (crc != bm->crc) { 833 applog(LOG_ERR, "%s: %s() check crc(%d)" 834 " != bm crc(%d) datalen(%d)", 835 ANTDRV.dname, __func__, 836 crc, bm->crc, datalen); 837 return -1; 838 } 839 bm->fifo_space = htole16(bm->fifo_space); 840 bm->fan_exist = htole16(bm->fan_exist); 841 bm->temp_exist = htole32(bm->temp_exist); 842 bm->nonce_error = htole32(bm->nonce_error); 843 if (bm->chain_num > BITMAIN_MAX_CHAIN_NUM) { 844 applog(LOG_ERR, "%s: %s() chain_num=%d error", 845 ANTDRV.dname, __func__, 846 bm->chain_num); 847 return -1; 848 } 849 dataindex = 28; 850 if (bm->chain_num > 0) { 851 memcpy(bm->chain_asic_num, 852 data+datalen-2-bm->chain_num-bm->temp_num-bm->fan_num, 853 bm->chain_num); 854 } 855 for (i = 0; i < bm->chain_num; i++) { 856 asic_num = bm->chain_asic_num[i]; 857 if (asic_num < 0) 858 asic_num = 1; 859 else { 860 if (asic_num % 32 == 0) 861 asic_num = asic_num / 32; 862 else 863 asic_num = asic_num / 32 + 1; 864 } 865 memcpy((uint8_t *)bm->chain_asic_exist+i*32, data+dataindex, asic_num*4); 866 dataindex += asic_num*4; 867 } 868 for(i = 0; i < bm->chain_num; i++) { 869 asic_num = bm->chain_asic_num[i]; 870 if (asic_num < 0) 871 asic_num = 1; 872 else { 873 if (asic_num % 32 == 0) 874 asic_num = asic_num / 32; 875 else 876 asic_num = asic_num / 32 + 1; 877 } 878 memcpy((uint8_t *)bm->chain_asic_status+i*32, data+dataindex, asic_num*4); 879 dataindex += asic_num*4; 880 } 881 dataindex += bm->chain_num; 882 if ((dataindex + bm->temp_num + bm->fan_num + 2) != datalen) { 883 applog(LOG_ERR, "%s: %s() dataindex(%d) chain_num(%d) temp_num(%d)" 884 " fan_num(%d) not match datalen(%d)", 885 ANTDRV.dname, __func__, 886 dataindex, bm->chain_num, bm->temp_num, bm->fan_num, datalen); 887 return -1; 888 } 889 for (i = 0; i < bm->chain_num; i++) { 890 for (j = 0; j < 8; j++) { 891 bm->chain_asic_exist[i*8+j] = htole32(bm->chain_asic_exist[i*8+j]); 892 bm->chain_asic_status[i*8+j] = htole32(bm->chain_asic_status[i*8+j]); 893 } 894 } 895 if (bm->temp_num > 0) { 896 memcpy(bm->temp, data+dataindex, bm->temp_num); 897 dataindex += bm->temp_num; 898 } 899 if (bm->fan_num > 0) { 900 memcpy(bm->fan, data+dataindex, bm->fan_num); 901 dataindex += bm->fan_num; 902 } 903 applog(LOG_DEBUG, "%s: RxStatus Data chipv_e(%d) chainnum(%d) fifos(%d)" 904 " v1(%d) v2(%d) v3(%d) v4(%d) fann(%d) tempn(%d) fanet(%04x)" 905 " tempet(%08x) ne(%d) regvalue(%d) crc(%04x)", 906 ANTDRV.dname, 907 bm->chip_value_eft, bm->chain_num, bm->fifo_space, 908 bm->hw_version[0], bm->hw_version[1], bm->hw_version[2], 909 bm->hw_version[3], bm->fan_num, bm->temp_num, bm->fan_exist, 910 bm->temp_exist, bm->nonce_error, bm->reg_value, bm->crc); 911 applog(LOG_DEBUG, "%s: RxStatus Data chain info:", ANTDRV.dname); 912 for (i = 0; i < bm->chain_num; i++) { 913 applog(LOG_DEBUG, "%s: RxStatus Data chain(%d) asic num=%d asic_exists=%08x" 914 " asic_status=%08x", 915 ANTDRV.dname, 916 i+1, bm->chain_asic_num[i], bm->chain_asic_exist[i*8], 917 bm->chain_asic_status[i*8]); 918 } 919 #endif 920 921 applog(LOG_DEBUG, "%s: RxStatus Data temp info:", ANTDRV.dname); 922 for (i = 0; i < bm->temp_num; i++) { 923 applog(LOG_DEBUG, "%s: RxStatus Data temp(%d) temp=%d", 924 ANTDRV.dname, 925 i+1, bm->temp[i]); 926 } 927 applog(LOG_DEBUG, "%s: RxStatus Data fan info:", ANTDRV.dname); 928 for (i = 0; i < bm->fan_num; i++) { 929 applog(LOG_DEBUG, "%s: RxStatus Data fan(%d) fan=%d", 930 ANTDRV.dname, 931 i+1, bm->fan[i]); 932 } 933 return 0; 934 } 935 936 static int bitmain_parse_rxnonce(const uint8_t * data, int datalen, struct bitmain_rxnonce_data *bm, int * nonce_num) 937 { 938 int i = 0; 939 uint16_t crc = 0; 940 #ifdef USE_ANT_S2 941 uint8_t version = 0; 942 #endif 943 int curnoncenum = 0; 944 if (unlikely(!bm)) { 945 applog(LOG_ERR, "%s: %s() bm is null", ANTDRV.dname, __func__); 946 return -1; 947 } 948 if (unlikely(!data || datalen <= 0)) { 949 applog(LOG_ERR, "%s: %s() parameter invalid data is null" 950 " or datalen(%d) error", 951 ANTDRV.dname, __func__, datalen); 952 return -1; 953 } 954 memcpy(bm, data, sizeof(struct bitmain_rxnonce_data)); 955 956 if (bm->data_type != BITMAIN_DATA_TYPE_RXNONCE) { 957 applog(LOG_ERR, "%s: %s() datatype(%02x) error", 958 ANTDRV.dname, __func__, 959 bm->data_type); 960 return -1; 961 } 962 #ifdef USE_ANT_S1 963 if (bm->length+2 != datalen) { 964 applog(LOG_ERR, "%s: %s() length(%d) error", 965 ANTDRV.dname, __func__, 966 bm->length); 967 return -1; 968 } 969 #else 970 if (bm->version != version) { 971 applog(LOG_ERR, "%s: %s() version(%02x) error", 972 ANTDRV.dname, __func__, 973 bm->version); 974 return -1; 975 } 976 bm->length = htole16(bm->length); 977 if (bm->length+4 != datalen) { 978 applog(LOG_ERR, "%s: %s() length(%d) datalen(%d) error", 979 ANTDRV.dname, __func__, 980 bm->length, datalen); 981 return -1; 982 } 983 #endif 984 crc = CRC16(data, datalen-2); 985 memcpy(&(bm->crc), data+datalen-2, 2); 986 bm->crc = htole16(bm->crc); 987 if (crc != bm->crc) { 988 applog(LOG_ERR, "%s: %s() check crc(%d)" 989 " != bm crc(%d) datalen(%d)", 990 ANTDRV.dname, __func__, 991 crc, bm->crc, datalen); 992 return -1; 993 } 994 #ifdef USE_ANT_S1 995 curnoncenum = (datalen-4)/8; 996 #else 997 bm->fifo_space = htole16(bm->fifo_space); 998 bm->diff = htole16(bm->diff); 999 bm->total_nonce_num = htole64(bm->total_nonce_num); 1000 curnoncenum = (datalen-14)/8; 1001 #endif 1002 applog(LOG_DEBUG, "%s: RxNonce Data: nonce_num(%d) fifo_space(%d)", 1003 ANTDRV.dname, curnoncenum, bm->fifo_space); 1004 for (i = 0; i < curnoncenum; i++) { 1005 bm->nonces[i].work_id = htole32(bm->nonces[i].work_id); 1006 bm->nonces[i].nonce = htole32(bm->nonces[i].nonce); 1007 applog(LOG_DEBUG, "%s: RxNonce Data %d: work_id(%"PRIu32") nonce(%08x)(%d)", 1008 ANTDRV.dname, 1009 i, bm->nonces[i].work_id, 1010 bm->nonces[i].nonce, bm->nonces[i].nonce); 1011 } 1012 *nonce_num = curnoncenum; 1013 return 0; 1014 } 1015 1016 static int bitmain_read(struct cgpu_info *bitmain, unsigned char *buf, 1017 size_t bufsize, __maybe_unused int timeout, 1018 __maybe_unused int ep) 1019 { 1020 __maybe_unused struct bitmain_info *info = bitmain->device_data; 1021 int readlen = 0; 1022 1023 if (bitmain == NULL || buf == NULL || bufsize <= 0) { 1024 applog(LOG_WARNING, "%s%d: %s() parameter error bufsize(%d)", 1025 bitmain->drv->name, bitmain->device_id, 1026 __func__, (int)bufsize); 1027 return -1; 1028 } 1029 1030 #if (defined(USE_ANT_S1) || defined(USE_ANT_S3)) 1031 int err = usb_read_once_timeout(bitmain, (char *)buf, bufsize, &readlen, 1032 timeout, ep); 1033 applog(LOG_DEBUG, "%s%i: Get %s() got readlen %d err %d", 1034 bitmain->drv->name, bitmain->device_id, 1035 __func__, readlen, err); 1036 #else 1037 readlen = read(info->device_fd, buf, bufsize); 1038 #endif 1039 return readlen; 1040 } 1041 1042 static int bitmain_write(struct cgpu_info *bitmain, char *buf, ssize_t len, 1043 __maybe_unused int ep) 1044 { 1045 __maybe_unused struct bitmain_info *info = bitmain->device_data; 1046 int amount, __maybe_unused sent; 1047 1048 #if (defined(USE_ANT_S1) || defined(USE_ANT_S3)) 1049 int err = usb_write(bitmain, buf, len, &amount, ep); 1050 applog(LOG_DEBUG, "%s%d: usb_write got err %d", 1051 bitmain->drv->name, bitmain->device_id, err); 1052 1053 if (unlikely(err != 0)) { 1054 applog(LOG_ERR, "%s%d: usb_write error on %s() err=%d", 1055 bitmain->drv->name, bitmain->device_id, __func__, err); 1056 return BTM_SEND_ERROR; 1057 } 1058 if (amount != len) { 1059 applog(LOG_ERR, "%s%d: usb_write length mismatch on %s() " 1060 "amount=%d len=%d", 1061 bitmain->drv->name, bitmain->device_id, __func__, 1062 amount, (int)len); 1063 return BTM_SEND_ERROR; 1064 } 1065 #else 1066 sent = 0; 1067 1068 while (sent < len) { 1069 amount = write(info->device_fd, buf+sent, len-sent); 1070 if (amount < 0) { 1071 applog(LOG_WARNING, "%s%d: ser_write got err %d", 1072 bitmain->drv->name, bitmain->device_id, amount); 1073 return BTM_SEND_ERROR; 1074 } 1075 sent += amount; 1076 } 1077 #endif 1078 return BTM_SEND_OK; 1079 } 1080 1081 static int bitmain_send_data(const uint8_t *data, int datalen, __maybe_unused struct cgpu_info *bitmain) 1082 { 1083 int ret, ep = 0; 1084 //int delay; 1085 //struct bitmain_info *info = NULL; 1086 //cgtimer_t ts_start; 1087 1088 if (datalen <= 0) { 1089 return 0; 1090 } 1091 1092 #if (defined(USE_ANT_S1) || defined(USE_ANT_S3)) 1093 ep = C_BITMAIN_SEND; 1094 if (data[0] == BITMAIN_TOKEN_TYPE_TXCONFIG) { 1095 ep = C_BITMAIN_TOKEN_TXCONFIG; 1096 } else if (data[0] == BITMAIN_TOKEN_TYPE_TXTASK) { 1097 ep = C_BITMAIN_TOKEN_TXTASK; 1098 } else if (data[0] == BITMAIN_TOKEN_TYPE_RXSTATUS) { 1099 ep = C_BITMAIN_TOKEN_RXSTATUS; 1100 } 1101 #endif 1102 1103 //info = bitmain->device_data; 1104 //delay = datalen * 10 * 1000000; 1105 //delay = delay / info->baud; 1106 //delay += 4000; 1107 1108 if (opt_debug) { 1109 applog(LOG_DEBUG, "%s: Sent(%d):", ANTDRV.dname, datalen); 1110 hexdump(data, datalen); 1111 } 1112 1113 //cgsleep_prepare_r(&ts_start); 1114 applog(LOG_DEBUG, "%s: %s() start", ANTDRV.dname, __func__); 1115 ret = bitmain_write(bitmain, (char *)data, datalen, ep); 1116 applog(LOG_DEBUG, "%s: %s() stop ret=%d datalen=%d", 1117 ANTDRV.dname, __func__, ret, datalen); 1118 //cgsleep_us_r(&ts_start, delay); 1119 1120 //applog(LOG_DEBUG, "BitMain: Sent: Buffer delay: %dus", delay); 1121 1122 return ret; 1123 } 1124 1125 static void bitmain_inc_nvw(struct bitmain_info *info, struct thr_info *thr) 1126 { 1127 applog(LOG_INFO, "%s%d: No matching work - HW error", 1128 thr->cgpu->drv->name, thr->cgpu->device_id); 1129 1130 inc_hw_errors(thr); 1131 info->no_matching_work++; 1132 } 1133 1134 static inline void record_temp_fan(struct bitmain_info *info, struct bitmain_rxstatus_data *bm, double *temp_avg) 1135 { 1136 int i = 0; 1137 1138 *temp_avg = 0.0; 1139 1140 info->fan_num = bm->fan_num; 1141 for (i = 0; i < bm->fan_num; i++) 1142 info->fan[i] = bm->fan[i] * BITMAIN_FAN_FACTOR; 1143 info->temp_num = bm->temp_num; 1144 info->temp_hi = 0; 1145 for (i = 0; i < bm->temp_num; i++) { 1146 info->temp[i] = bm->temp[i]; 1147 /* if (bm->temp[i] & 0x80) { 1148 bm->temp[i] &= 0x7f; 1149 info->temp[i] = 0 - ((~bm->temp[i] & 0x7f) + 1); 1150 }*/ 1151 *temp_avg += info->temp[i]; 1152 1153 if (info->temp[i] > info->temp_max) 1154 info->temp_max = info->temp[i]; 1155 if (info->temp[i] > info->temp_hi) 1156 info->temp_hi = info->temp[i]; 1157 } 1158 1159 if (bm->temp_num > 0) { 1160 *temp_avg = *temp_avg / bm->temp_num; 1161 info->temp_avg = *temp_avg; 1162 } 1163 } 1164 1165 static void bitmain_update_temps(struct cgpu_info *bitmain, struct bitmain_info *info, 1166 struct bitmain_rxstatus_data *bm) 1167 { 1168 char tmp[64] = {0}; 1169 char msg[10240] = {0}; 1170 int i = 0; 1171 record_temp_fan(info, bm, &(bitmain->temp)); 1172 1173 sprintf(msg, "%s%d: ", bitmain->drv->name, bitmain->device_id); 1174 for (i = 0; i < bm->fan_num; i++) { 1175 if (i != 0) { 1176 strcat(msg, ", "); 1177 } 1178 sprintf(tmp, "Fan%d: %d/m", i+1, info->fan[i]); 1179 strcat(msg, tmp); 1180 } 1181 strcat(msg, " "); 1182 for (i = 0; i < bm->temp_num; i++) { 1183 if (i != 0) { 1184 strcat(msg, ", "); 1185 } 1186 sprintf(tmp, "Temp%d: %dC", i+1, info->temp[i]); 1187 strcat(msg, tmp); 1188 } 1189 sprintf(tmp, ", TempMAX: %dC", info->temp_max); 1190 strcat(msg, tmp); 1191 applog(LOG_INFO, "%s", msg); 1192 info->temp_history_index++; 1193 info->temp_sum += bitmain->temp; 1194 applog(LOG_DEBUG, "%s%d: temp_index: %d, temp_count: %d, temp_max: %d", 1195 bitmain->drv->name, bitmain->device_id, 1196 info->temp_history_index, info->temp_history_count, info->temp_max); 1197 if (info->temp_history_index == info->temp_history_count) { 1198 info->temp_history_index = 0; 1199 info->temp_sum = 0; 1200 } 1201 #ifdef USE_ANT_S1 1202 if (unlikely(info->temp_hi >= opt_bitmain_overheat)) { 1203 if (!info->overheat) { 1204 applog(LOG_WARNING, "%s%d: overheat! hi %dC limit %dC idling", 1205 bitmain->drv->name, bitmain->device_id, 1206 info->temp_hi, opt_bitmain_overheat); 1207 info->overheat = true; 1208 info->overheat_temp = info->temp_hi; 1209 info->overheat_count++; 1210 info->overheat_slept = 0; 1211 } 1212 } else if (info->overheat && info->temp_hi <= opt_bitmain_temp) { 1213 applog(LOG_WARNING, "%s%d: cooled, restarting", 1214 bitmain->drv->name, bitmain->device_id); 1215 info->overheat = false; 1216 info->overheat_recovers++; 1217 } 1218 #endif 1219 } 1220 1221 static void bitmain_parse_results(struct cgpu_info *bitmain, struct bitmain_info *info, 1222 struct thr_info *thr, uint8_t *buf, int *offset) 1223 { 1224 #ifdef USE_ANT_S1 1225 int i, j, n, m, errordiff, spare = BITMAIN_READ_SIZE; 1226 uint32_t checkbit = 0x00000000; 1227 bool found = false; 1228 struct work *work = NULL; 1229 //char *ob_hex = NULL; 1230 uint64_t searches; 1231 K_ITEM *witem; 1232 1233 for (i = 0; i <= spare; i++) { 1234 if (buf[i] == 0xa1) { 1235 struct bitmain_rxstatus_data rxstatusdata; 1236 applog(LOG_DEBUG, "%s%d: %s() RxStatus Data", 1237 bitmain->drv->name, bitmain->device_id, 1238 __func__); 1239 if (*offset < 2) { 1240 return; 1241 } 1242 if (buf[i+1] > 124) { 1243 applog(LOG_ERR, "%s%d: %s() RxStatus Data datalen=%d error", 1244 bitmain->drv->name, bitmain->device_id, 1245 __func__, buf[i+1]+2); 1246 continue; 1247 } 1248 if (*offset < buf[i+1] + 2) { 1249 return; 1250 } 1251 if (bitmain_parse_rxstatus(buf+i, buf[i+1]+2, &rxstatusdata) != 0) { 1252 applog(LOG_ERR, "%s%d: %s() RxStatus Data error len=%d", 1253 bitmain->drv->name, bitmain->device_id, 1254 __func__, buf[i+1]+2); 1255 } else { 1256 mutex_lock(&info->qlock); 1257 info->chain_num = rxstatusdata.chain_num; 1258 info->fifo_space = rxstatusdata.fifo_space; 1259 info->nonce_error = rxstatusdata.nonce_error; 1260 errordiff = info->nonce_error-info->last_nonce_error; 1261 applog(LOG_DEBUG, "%s%d: %s() RxStatus Data" 1262 " version=%d chainnum=%d fifospace=%d" 1263 " nonceerror=%d-%d freq=%d chain info:", 1264 bitmain->drv->name, bitmain->device_id, __func__, 1265 rxstatusdata.version, info->chain_num, 1266 info->fifo_space, info->last_nonce_error, 1267 info->nonce_error, info->frequency); 1268 for (n = 0; n < rxstatusdata.chain_num; n++) { 1269 info->chain_asic_num[n] = rxstatusdata.chain_asic_num[n]; 1270 info->chain_asic_status[n] = rxstatusdata.chain_asic_status[n]; 1271 memset(info->chain_asic_status_t[n], 0, 40); 1272 j = 0; 1273 for (m = 0; m < 32; m++) { 1274 if (m%8 == 0 && m != 0) { 1275 info->chain_asic_status_t[n][j] = ' '; 1276 j++; 1277 } 1278 checkbit = num2bit(m); 1279 if (rxstatusdata.chain_asic_status[n] & checkbit) 1280 info->chain_asic_status_t[n][j] = 'o'; 1281 else 1282 info->chain_asic_status_t[n][j] = 'x'; 1283 1284 j++; 1285 } 1286 applog(LOG_DEBUG, "%s%d: %s() RxStatus Data chain(%d)" 1287 " asic_num=%d asic_status=%08x-%s", 1288 bitmain->drv->name, bitmain->device_id, 1289 __func__, 1290 n, info->chain_asic_num[n], 1291 info->chain_asic_status[n], 1292 info->chain_asic_status_t[n]); 1293 } 1294 mutex_unlock(&info->qlock); 1295 1296 if (errordiff > 0) { 1297 for (j = 0; j < errordiff; j++) { 1298 bitmain_inc_nvw(info, thr); 1299 } 1300 mutex_lock(&info->qlock); 1301 info->last_nonce_error += errordiff; 1302 mutex_unlock(&info->qlock); 1303 } 1304 bitmain_update_temps(bitmain, info, &rxstatusdata); 1305 } 1306 1307 found = true; 1308 spare = buf[i+1] + 2 + i; 1309 if (spare > *offset) { 1310 applog(LOG_ERR, "%s%d: %s() spare(%d) > offset(%d)", 1311 bitmain->drv->name, bitmain->device_id, 1312 __func__, spare, *offset); 1313 spare = *offset; 1314 } 1315 break; 1316 } else if (buf[i] == 0xa2) { 1317 struct bitmain_rxnonce_data rxnoncedata; 1318 int nonce_num = 0; 1319 applog(LOG_DEBUG, "%s%d: %s() RxNonce Data", 1320 bitmain->drv->name, bitmain->device_id, 1321 __func__); 1322 if (*offset < 2) { 1323 return; 1324 } 1325 if (buf[i+1] > 70) { 1326 applog(LOG_ERR, "%s%d: %s() RxNonce Data datalen=%d error", 1327 bitmain->drv->name, bitmain->device_id, 1328 __func__, buf[i+1]+2); 1329 continue; 1330 } 1331 if (*offset < buf[i+1] + 2) { 1332 return; 1333 } 1334 if (bitmain_parse_rxnonce(buf+i, buf[i+1]+2, &rxnoncedata, &nonce_num) != 0) { 1335 applog(LOG_ERR, "%s%d: %s() RxNonce Data error len=%d", 1336 bitmain->drv->name, bitmain->device_id, 1337 __func__, buf[i+1]+2); 1338 } else { 1339 for (j = 0; j < nonce_num; j++) { 1340 uint32_t wid = rxnoncedata.nonces[j].work_id; 1341 searches = 0; 1342 mutex_lock(&info->qlock); 1343 witem = info->work_list->head; 1344 while (witem) { 1345 searches++; 1346 if (DATAW(witem)->wid == wid) 1347 break; 1348 witem = witem->next; 1349 } 1350 mutex_unlock(&info->qlock); 1351 if (witem) { 1352 if (info->work_search == 0) { 1353 info->min_search = searches; 1354 info->max_search = searches; 1355 } else { 1356 if (info->min_search > searches) 1357 info->min_search = searches; 1358 if (info->max_search < searches) 1359 info->max_search = searches; 1360 } 1361 info->work_search++; 1362 info->tot_search += searches; 1363 1364 work = DATAW(witem)->work; 1365 applog(LOG_DEBUG, "%s%d: %s() RxNonce Data find " 1366 "work(%"PRIu32")(%08x)", 1367 bitmain->drv->name, 1368 bitmain->device_id, 1369 __func__, wid, 1370 rxnoncedata.nonces[j].nonce); 1371 1372 applog(LOG_DEBUG, "%s%d: %s() nonce = %08x", 1373 bitmain->drv->name, bitmain->device_id, 1374 __func__, rxnoncedata.nonces[j].nonce); 1375 if (isdupnonce(bitmain, work, rxnoncedata.nonces[j].nonce)) { 1376 // ignore it 1377 } else { 1378 if (submit_nonce(thr, work, rxnoncedata.nonces[j].nonce)) { 1379 applog(LOG_DEBUG, "%s%d: %s() RxNonce Data ok", 1380 bitmain->drv->name, 1381 bitmain->device_id, 1382 __func__); 1383 mutex_lock(&info->qlock); 1384 info->nonces++; 1385 mutex_unlock(&info->qlock); 1386 } else { 1387 applog(LOG_ERR, "%s%d: %s() RxNonce Data " 1388 "error work(%"PRIu32")", 1389 bitmain->drv->name, 1390 bitmain->device_id, 1391 __func__, 1392 rxnoncedata.nonces[j].work_id); 1393 } 1394 } 1395 } else { 1396 if (info->failed_search == 0) { 1397 info->min_failed = searches; 1398 info->max_failed = searches; 1399 } else { 1400 if (info->min_failed > searches) 1401 info->min_failed = searches; 1402 if (info->max_failed < searches) 1403 info->max_failed = searches; 1404 } 1405 info->failed_search++; 1406 info->tot_failed += searches; 1407 1408 mutex_lock(&info->qlock); 1409 uint32_t min = 0, max = 0; 1410 int count = 0; 1411 if (info->work_list->tail) { 1412 min = DATAW(info->work_list->tail)->wid; 1413 max = DATAW(info->work_list->head)->wid; 1414 count = info->work_list->count; 1415 } 1416 mutex_unlock(&info->qlock); 1417 applog(LOG_ERR, "%s%d: %s() Work not found" 1418 " for id (%"PRIu32") (min=%" 1419 PRIu32" max=%"PRIu32" count=%d)", 1420 bitmain->drv->name, 1421 bitmain->device_id, 1422 __func__, wid, 1423 min, max, count); 1424 } 1425 } 1426 mutex_lock(&info->qlock); 1427 info->fifo_space = rxnoncedata.fifo_space; 1428 mutex_unlock(&info->qlock); 1429 applog(LOG_DEBUG, "%s%d: %s() RxNonce Data fifo space=%d", 1430 bitmain->drv->name, bitmain->device_id, 1431 __func__, rxnoncedata.fifo_space); 1432 } 1433 1434 found = true; 1435 spare = buf[i+1] + 2 + i; 1436 if (spare > *offset) { 1437 applog(LOG_ERR, "%s%d: %s() RxNonce Data space(%d) > offset(%d)", 1438 bitmain->drv->name, bitmain->device_id, __func__, 1439 spare, *offset); 1440 spare = *offset; 1441 } 1442 break; 1443 } else { 1444 applog(LOG_ERR, "%s%d: %s() data type error=%02x", 1445 bitmain->drv->name, bitmain->device_id, 1446 __func__, buf[i]); 1447 } 1448 } 1449 if (!found) { 1450 spare = *offset - BITMAIN_READ_SIZE; 1451 /* We are buffering and haven't accumulated one more corrupt 1452 * work result. */ 1453 if (spare < (int)BITMAIN_READ_SIZE) 1454 return; 1455 bitmain_inc_nvw(info, thr); 1456 } 1457 1458 *offset -= spare; 1459 memmove(buf, buf + spare, *offset); 1460 #else // S2 1461 int i, j, n, m, r, errordiff, spare = BITMAIN_READ_SIZE; 1462 uint32_t checkbit = 0x00000000; 1463 bool found = false; 1464 struct work *work = NULL; 1465 struct bitmain_packet_head packethead; 1466 int asicnum = 0, mod = 0, tmp = 0; 1467 uint64_t searches; 1468 K_ITEM *witem; 1469 1470 for (i = 0; i <= spare; i++) { 1471 if (buf[i] == 0xa1) { 1472 struct bitmain_rxstatus_data rxstatusdata; 1473 applog(LOG_DEBUG, "%s%d: %s() RxStatus Data", 1474 bitmain->drv->name, bitmain->device_id, 1475 __func__); 1476 if (*offset < 4) { 1477 return; 1478 } 1479 memcpy(&packethead, buf+i, sizeof(struct bitmain_packet_head)); 1480 packethead.length = htole16(packethead.length); 1481 if (packethead.length > 1130) { 1482 applog(LOG_ERR, "%s%d: %s() RxStatus Data datalen=%d error", 1483 bitmain->drv->name, bitmain->device_id, 1484 __func__, packethead.length+4); 1485 continue; 1486 } 1487 if (*offset < packethead.length + 4) 1488 return; 1489 if (bitmain_parse_rxstatus(buf+i, packethead.length+4, &rxstatusdata) != 0) { 1490 applog(LOG_ERR, "%s%d: %s() RxStatus Data error len=%d", 1491 bitmain->drv->name, bitmain->device_id, 1492 __func__, packethead.length+4); 1493 } else { 1494 mutex_lock(&info->qlock); 1495 info->chain_num = rxstatusdata.chain_num; 1496 info->fifo_space = rxstatusdata.fifo_space; 1497 info->hw_version[0] = rxstatusdata.hw_version[0]; 1498 info->hw_version[1] = rxstatusdata.hw_version[1]; 1499 info->hw_version[2] = rxstatusdata.hw_version[2]; 1500 info->hw_version[3] = rxstatusdata.hw_version[3]; 1501 info->nonce_error = rxstatusdata.nonce_error; 1502 errordiff = info->nonce_error-info->last_nonce_error; 1503 applog(LOG_DEBUG, "%s%d: %s() RxStatus Data" 1504 " version=%d chainnum=%d fifospace=%d" 1505 " hwv1=%d hwv2=%d hwv3=%d hwv4=%d" 1506 " nonceerror=%d-%d freq=%d chain info:", 1507 bitmain->drv->name, bitmain->device_id, __func__, 1508 rxstatusdata.version, info->chain_num, info->fifo_space, 1509 info->hw_version[0], info->hw_version[1], 1510 info->hw_version[2], info->hw_version[3], 1511 info->last_nonce_error, 1512 info->nonce_error, info->frequency); 1513 memcpy(info->chain_asic_exist, rxstatusdata.chain_asic_exist, BITMAIN_MAX_CHAIN_NUM*32); 1514 memcpy(info->chain_asic_status, rxstatusdata.chain_asic_status, BITMAIN_MAX_CHAIN_NUM*32); 1515 for (n = 0; n < rxstatusdata.chain_num; n++) { 1516 info->chain_asic_num[n] = rxstatusdata.chain_asic_num[n]; 1517 memset(info->chain_asic_status_t[n], 0, 320); 1518 j = 0; 1519 mod = 0; 1520 if (info->chain_asic_num[n] <= 0) 1521 asicnum = 0; 1522 else { 1523 mod = info->chain_asic_num[n] % 32; 1524 if (mod == 0) 1525 asicnum = info->chain_asic_num[n] / 32; 1526 else 1527 asicnum = info->chain_asic_num[n] / 32 + 1; 1528 } 1529 if (asicnum > 0) { 1530 for (m = asicnum-1; m >= 0; m--) { 1531 tmp = (mod ? (32 - mod) : 0); 1532 for (r = tmp; r < 32; r++) { 1533 if (((r-tmp) % 8) == 0 && (r-tmp) != 0) { 1534 info->chain_asic_status_t[n][j] = ' '; 1535 j++; 1536 } 1537 checkbit = num2bit(r); 1538 if (rxstatusdata.chain_asic_exist[n*8+m] & checkbit) { 1539 if (rxstatusdata.chain_asic_status[n*8+m] & checkbit) 1540 info->chain_asic_status_t[n][j] = 'o'; 1541 else 1542 info->chain_asic_status_t[n][j] = 'x'; 1543 } else 1544 info->chain_asic_status_t[n][j] = '-'; 1545 j++; 1546 } 1547 info->chain_asic_status_t[n][j] = ' '; 1548 j++; 1549 mod = 0; 1550 } 1551 } 1552 applog(LOG_DEBUG, "%s%d: %s() RxStatis Data chain(%d) asic_num=%d " 1553 "asic_exist=%08x%08x%08x%08x%08x%08x%08x%08x " 1554 "asic_status=%08x%08x%08x%08x%08x%08x%08x%08x", 1555 bitmain->drv->name, bitmain->device_id, 1556 __func__, n, info->chain_asic_num[n], 1557 info->chain_asic_exist[n*8+0], 1558 info->chain_asic_exist[n*8+1], 1559 info->chain_asic_exist[n*8+2], 1560 info->chain_asic_exist[n*8+3], 1561 info->chain_asic_exist[n*8+4], 1562 info->chain_asic_exist[n*8+5], 1563 info->chain_asic_exist[n*8+6], 1564 info->chain_asic_exist[n*8+7], 1565 info->chain_asic_status[n*8+0], 1566 info->chain_asic_status[n*8+1], 1567 info->chain_asic_status[n*8+2], 1568 info->chain_asic_status[n*8+3], 1569 info->chain_asic_status[n*8+4], 1570 info->chain_asic_status[n*8+5], 1571 info->chain_asic_status[n*8+6], 1572 info->chain_asic_status[n*8+7]); 1573 applog(LOG_ERR, "%s%d: %s() RxStatis Data chain(%d) asic_num=%d" 1574 " asic_status=%s", 1575 bitmain->drv->name, bitmain->device_id, 1576 __func__, n, info->chain_asic_num[n], 1577 info->chain_asic_status_t[n]); 1578 } 1579 mutex_unlock(&info->qlock); 1580 1581 if (errordiff > 0) { 1582 for (j = 0; j < errordiff; j++) 1583 bitmain_inc_nvw(info, thr); 1584 mutex_lock(&info->qlock); 1585 info->last_nonce_error += errordiff; 1586 mutex_unlock(&info->qlock); 1587 } 1588 bitmain_update_temps(bitmain, info, &rxstatusdata); 1589 } 1590 1591 found = true; 1592 spare = packethead.length + 4 + i; 1593 if (spare > *offset) { 1594 applog(LOG_ERR, "%s%d: %s() spare(%d) > offset(%d)", 1595 bitmain->drv->name, bitmain->device_id, 1596 __func__, spare, *offset); 1597 spare = *offset; 1598 } 1599 break; 1600 } else if (buf[i] == 0xa2) { 1601 struct bitmain_rxnonce_data rxnoncedata; 1602 int nonce_num = 0; 1603 applog(LOG_DEBUG, "%s%d: %s() RxNonce Data", 1604 bitmain->drv->name, bitmain->device_id, 1605 __func__); 1606 if (*offset < 4) 1607 return; 1608 memcpy(&packethead, buf+i, sizeof(struct bitmain_packet_head)); 1609 packethead.length = htole16(packethead.length); 1610 if (packethead.length > 1030) { 1611 applog(LOG_ERR, "%s%d: %s() RxNonce Data datalen=%d error", 1612 bitmain->drv->name, bitmain->device_id, 1613 __func__, packethead.length+4); 1614 continue; 1615 } 1616 if (*offset < packethead.length + 4) 1617 return; 1618 if (bitmain_parse_rxnonce(buf+i, packethead.length+4, &rxnoncedata, &nonce_num) != 0) { 1619 applog(LOG_ERR, "%s%d: %s() RxNonce Data error len=%d", 1620 bitmain->drv->name, bitmain->device_id, 1621 __func__, packethead.length+4); 1622 } else { 1623 for (j = 0; j < nonce_num; j++) { 1624 uint32_t wid = rxnoncedata.nonces[j].work_id; 1625 searches = 0; 1626 mutex_lock(&info->qlock); 1627 witem = info->work_list->head; 1628 while (witem && DATAW(witem)->work) { 1629 searches++; 1630 if (DATAW(witem)->wid == wid) 1631 break; 1632 witem = witem->next; 1633 } 1634 if (witem && DATAW(witem)->work) { 1635 work = DATAW(witem)->work; 1636 mutex_unlock(&info->qlock); 1637 if (info->work_search == 0) { 1638 info->min_search = searches; 1639 info->max_search = searches; 1640 } else { 1641 if (info->min_search > searches) 1642 info->min_search = searches; 1643 if (info->max_search < searches) 1644 info->max_search = searches; 1645 } 1646 info->work_search++; 1647 info->tot_search += searches; 1648 1649 applog(LOG_DEBUG, "%s%d: %s() RxNonce Data find " 1650 "work(%"PRIu32")(%08x)", 1651 bitmain->drv->name, bitmain->device_id, 1652 __func__, wid, 1653 rxnoncedata.nonces[j].nonce); 1654 1655 applog(LOG_DEBUG, "%s%d: %s() nonce = %08x", 1656 bitmain->drv->name, bitmain->device_id, 1657 __func__, rxnoncedata.nonces[j].nonce); 1658 if (isdupnonce(bitmain, work, rxnoncedata.nonces[j].nonce)) { 1659 // ignore it 1660 } else { 1661 if (submit_nonce(thr, work, rxnoncedata.nonces[j].nonce)) { 1662 applog(LOG_DEBUG, "%s%d: %s() RxNonce Data ok", 1663 bitmain->drv->name, 1664 bitmain->device_id, 1665 __func__); 1666 mutex_lock(&info->qlock); 1667 info->nonces += work->device_diff; 1668 mutex_unlock(&info->qlock); 1669 } else { 1670 applog(LOG_ERR, "%s%d: %s() RxNonce Data " 1671 "error work(%"PRIu32")", 1672 bitmain->drv->name, 1673 bitmain->device_id, 1674 __func__, 1675 rxnoncedata.nonces[j].work_id); 1676 } 1677 } 1678 } else { 1679 mutex_unlock(&info->qlock); 1680 if (info->failed_search == 0) { 1681 info->min_failed = searches; 1682 info->max_failed = searches; 1683 } else { 1684 if (info->min_failed > searches) 1685 info->min_failed = searches; 1686 if (info->max_failed < searches) 1687 info->max_failed = searches; 1688 } 1689 info->failed_search++; 1690 info->tot_failed += searches; 1691 1692 applog(LOG_ERR, "%s%d: %s() Work not found " 1693 "for id (%"PRIu32")", 1694 bitmain->drv->name, 1695 bitmain->device_id, 1696 __func__, wid); 1697 } 1698 } 1699 mutex_lock(&info->qlock); 1700 info->fifo_space = rxnoncedata.fifo_space; 1701 mutex_unlock(&info->qlock); 1702 applog(LOG_DEBUG, "%s%d: %s() RxNonce Data fifo space=%d", 1703 bitmain->drv->name, bitmain->device_id, 1704 __func__, rxnoncedata.fifo_space); 1705 1706 if (nonce_num < BITMAIN_MAX_NONCE_NUM) 1707 cgsleep_ms(5); 1708 } 1709 1710 found = true; 1711 spare = packethead.length + 4 + i; 1712 if (spare > *offset) { 1713 applog(LOG_ERR, "%s%d: %s() RxNonce Data space(%d) > offset(%d)", 1714 bitmain->drv->name, bitmain->device_id, __func__, 1715 spare, *offset); 1716 spare = *offset; 1717 } 1718 break; 1719 } else { 1720 applog(LOG_ERR, "%s%d: %s() data type error=%02x", 1721 bitmain->drv->name, bitmain->device_id, 1722 __func__, buf[i]); 1723 } 1724 } 1725 if (!found) { 1726 spare = *offset - BITMAIN_READ_SIZE; 1727 /* We are buffering and haven't accumulated one more corrupt 1728 * work result. */ 1729 if (spare < (int)BITMAIN_READ_SIZE) 1730 return; 1731 bitmain_inc_nvw(info, thr); 1732 } 1733 1734 *offset -= spare; 1735 memmove(buf, buf + spare, *offset); 1736 #endif 1737 } 1738 1739 static void bitmain_running_reset(struct bitmain_info *info) 1740 { 1741 info->results = 0; 1742 info->reset = false; 1743 } 1744 1745 static void *bitmain_get_results(void *userdata) 1746 { 1747 struct cgpu_info *bitmain = (struct cgpu_info *)userdata; 1748 struct bitmain_info *info = bitmain->device_data; 1749 int offset = 0, ret = 0; 1750 const int rsize = BITMAIN_FTDI_READSIZE; 1751 char readbuf[BITMAIN_READBUF_SIZE]; 1752 struct thr_info *thr = info->thr; 1753 char threadname[24]; 1754 int errorcount = 0; 1755 1756 snprintf(threadname, 24, "btm_recv/%d", bitmain->device_id); 1757 RenameThread(threadname); 1758 1759 while (likely(!bitmain->shutdown)) { 1760 unsigned char buf[rsize]; 1761 1762 applog(LOG_DEBUG, "%s%d: %s() offset=%d", 1763 bitmain->drv->name, bitmain->device_id, __func__, offset); 1764 1765 if (offset >= (int)BITMAIN_READ_SIZE) { 1766 applog(LOG_DEBUG, "%s%d: %s() start", 1767 bitmain->drv->name, bitmain->device_id, __func__); 1768 bitmain_parse_results(bitmain, info, thr, (uint8_t *)readbuf, &offset); 1769 applog(LOG_DEBUG, "%s%d: %s() stop", 1770 bitmain->drv->name, bitmain->device_id, __func__); 1771 } 1772 1773 if (unlikely(offset + rsize >= BITMAIN_READBUF_SIZE)) { 1774 info->readbuf_over++; 1775 /* This should never happen */ 1776 applog(LOG_DEBUG, "%s%d: readbuf overflow, resetting buffer", 1777 bitmain->drv->name, bitmain->device_id); 1778 offset = 0; 1779 } 1780 1781 if (unlikely(info->reset)) { 1782 bitmain_running_reset(info); 1783 /* Discard anything in the buffer */ 1784 offset = 0; 1785 } 1786 1787 #ifdef USE_ANT_S1 1788 // 2ms shouldn't be too much 1789 cgsleep_ms(2); 1790 #endif 1791 1792 applog(LOG_DEBUG, "%s%d: %s() read", 1793 bitmain->drv->name, bitmain->device_id, __func__); 1794 ret = bitmain_read(bitmain, buf, rsize, BITMAIN_READ_TIMEOUT, C_BITMAIN_READ); 1795 applog(LOG_DEBUG, "%s%d: %s() read=%d", 1796 bitmain->drv->name, bitmain->device_id, __func__, ret); 1797 1798 if (ret < 1) { 1799 errorcount++; 1800 info->read_bad++; 1801 #ifdef USE_ANT_S1 1802 if (errorcount > 100) { 1803 #else 1804 if (errorcount > 3) { 1805 #endif 1806 // applog(LOG_ERR, "%s%d: read errorcount>100 ret=%d", 1807 // bitmain->drv->name, bitmain->device_id, ret); 1808 cgsleep_ms(20); 1809 errorcount = 0; 1810 } 1811 #ifndef USE_ANT_S1 1812 if (errorcount > 0) 1813 cgsleep_ms(1); 1814 #endif 1815 continue; 1816 } 1817 1818 if (opt_debug) { 1819 applog(LOG_DEBUG, "%s%d: get:", 1820 bitmain->drv->name, bitmain->device_id); 1821 hexdump((uint8_t *)buf, ret); 1822 } 1823 1824 info->read_size += ret; 1825 if (info->read_good == 0) 1826 info->read_sizemin = info->read_sizemax = ret; 1827 else { 1828 if (info->read_sizemin > ret) 1829 info->read_sizemin = ret; 1830 if (info->read_sizemax < ret) 1831 info->read_sizemax = ret; 1832 } 1833 info->read_good++; 1834 if (ret == 18) 1835 info->read_18s++; 1836 memcpy(readbuf+offset, buf, ret); 1837 offset += ret; 1838 } 1839 return NULL; 1840 } 1841 1842 /* 1843 static void bitmain_set_timeout(struct bitmain_info *info) 1844 { 1845 info->timeout = BITMAIN_TIMEOUT_FACTOR / info->frequency; 1846 } 1847 */ 1848 1849 static void bitmain_init(struct cgpu_info *bitmain) 1850 { 1851 applog(LOG_INFO, "%s%d: opened on %s", 1852 bitmain->drv->name, bitmain->device_id, 1853 bitmain->device_path); 1854 } 1855 1856 static bool bitmain_prepare(struct thr_info *thr) 1857 { 1858 struct cgpu_info *bitmain = thr->cgpu; 1859 struct bitmain_info *info = bitmain->device_data; 1860 1861 info->thr = thr; 1862 mutex_init(&info->lock); 1863 mutex_init(&info->qlock); 1864 if (unlikely(pthread_cond_init(&info->qcond, NULL))) 1865 quit(1, "Failed to pthread_cond_init bitmain qcond"); 1866 cgsem_init(&info->write_sem); 1867 1868 if (pthread_create(&info->read_thr, NULL, bitmain_get_results, (void *)bitmain)) 1869 quit(1, "Failed to create bitmain read_thr"); 1870 1871 bitmain_init(bitmain); 1872 1873 return true; 1874 } 1875 1876 static int bitmain_initialize(struct cgpu_info *bitmain) 1877 { 1878 uint8_t data[BITMAIN_READBUF_SIZE]; 1879 struct bitmain_info *info = NULL; 1880 int ret = 0; 1881 uint8_t sendbuf[BITMAIN_SENDBUF_SIZE]; 1882 int readlen = 0; 1883 int sendlen = 0; 1884 int trycount = 3; 1885 struct timespec p; 1886 struct bitmain_rxstatus_data rxstatusdata; 1887 int i = 0, j = 0, m = 0, statusok = 0; 1888 uint32_t checkbit = 0x00000000; 1889 #ifdef USE_ANT_S1 1890 int eft = 0; 1891 #else 1892 int r = 0; 1893 int hwerror_eft = 0; 1894 int beeper_ctrl = 1; 1895 int tempover_ctrl = 1; 1896 struct bitmain_packet_head packethead; 1897 int asicnum = 0; 1898 1899 int mathtest = (int)floor(log2(42)); 1900 if (mathtest != 5) { 1901 applog(LOG_ERR, "%s%d: %s() floating point math library is deficient", 1902 bitmain->drv->name, bitmain->device_id, __func__); 1903 return -1; 1904 } 1905 #endif 1906 1907 /* Send reset, then check for result */ 1908 if (!bitmain) { 1909 applog(LOG_WARNING, "%s%d: %s() cgpu_info is null", 1910 bitmain->drv->name, bitmain->device_id, __func__); 1911 return -1; 1912 } 1913 info = bitmain->device_data; 1914 1915 /* clear read buf */ 1916 ret = bitmain_read(bitmain, data, BITMAIN_READBUF_SIZE, 1917 BITMAIN_RESET_TIMEOUT, C_BITMAIN_READ); 1918 if (ret > 0) { 1919 if (opt_debug) { 1920 applog(LOG_DEBUG, "%s%d: clear read(%d):", 1921 bitmain->drv->name, bitmain->device_id, ret); 1922 hexdump(data, ret); 1923 } 1924 } 1925 1926 sendlen = bitmain_set_rxstatus((struct bitmain_rxstatus_token *)sendbuf, 0, 1, 0, 0); 1927 if (sendlen <= 0) { 1928 applog(LOG_ERR, "%s%d: %s() set_rx error(%d)", 1929 bitmain->drv->name, bitmain->device_id, __func__, sendlen); 1930 return -1; 1931 } 1932 1933 ret = bitmain_send_data(sendbuf, sendlen, bitmain); 1934 if (unlikely(ret == BTM_SEND_ERROR)) { 1935 applog(LOG_ERR, "%s%d: %s() send_data error", 1936 bitmain->drv->name, bitmain->device_id, __func__); 1937 return -1; 1938 } 1939 while (trycount >= 0) { 1940 ret = bitmain_read(bitmain, data+readlen, BITMAIN_READBUF_SIZE, 1941 BITMAIN_RESET_TIMEOUT, C_BITMAIN_DATA_RXSTATUS); 1942 if (ret > 0) { 1943 readlen += ret; 1944 if (readlen > BITMAIN_READ_SIZE) { 1945 for (i = 0; i < readlen; i++) { 1946 if (data[i] == 0xa1) { 1947 if (opt_debug) { 1948 applog(LOG_DEBUG, "%s%d: initset get:", 1949 bitmain->drv->name, 1950 bitmain->device_id); 1951 hexdump(data, readlen); 1952 } 1953 #ifdef USE_ANT_S1 1954 if (data[i+1] > 124) { 1955 applog(LOG_ERR, "%s%d: %s() rxstatus datalen=%d error", 1956 bitmain->drv->name, bitmain->device_id, 1957 __func__, data[i+1]+2); 1958 continue; 1959 } 1960 if (readlen-i < data[i+1]+2) { 1961 applog(LOG_ERR, "%s%d: %s() rxstatus datalen=%d low", 1962 bitmain->drv->name, bitmain->device_id, 1963 __func__, data[i+1]+2); 1964 continue; 1965 } 1966 if (bitmain_parse_rxstatus(data+i, data[i+1]+2, &rxstatusdata) != 0) { 1967 applog(LOG_ERR, "%s%d: %s() parse_rxstatus error", 1968 bitmain->drv->name, bitmain->device_id, 1969 __func__); 1970 continue; 1971 } 1972 info->chain_num = rxstatusdata.chain_num; 1973 info->fifo_space = rxstatusdata.fifo_space; 1974 info->nonce_error = 0; 1975 info->last_nonce_error = 0; 1976 applog(LOG_ERR, "%s%d: %s() parse_rxstatus " 1977 "version(%d) chain_num(%d) fifo_space(%d) " 1978 "nonce_error(%d) freq=%d", 1979 bitmain->drv->name, bitmain->device_id, 1980 __func__, 1981 rxstatusdata.version, 1982 info->chain_num, 1983 info->fifo_space, 1984 rxstatusdata.nonce_error, 1985 info->frequency); 1986 for (i = 0; i < rxstatusdata.chain_num; i++) { 1987 info->chain_asic_num[i] = rxstatusdata.chain_asic_num[i]; 1988 info->chain_asic_status[i] = rxstatusdata.chain_asic_status[i]; 1989 memset(info->chain_asic_status_t[i], 0, 40); 1990 j = 0; 1991 for (m = 0; m < 32; m++) { 1992 if (m%8 == 0 && m != 0) { 1993 info->chain_asic_status_t[i][j] = ' '; 1994 j++; 1995 } 1996 checkbit = num2bit(m); 1997 if (rxstatusdata.chain_asic_status[i] & checkbit) 1998 info->chain_asic_status_t[i][j] = 'o'; 1999 else 2000 info->chain_asic_status_t[i][j] = 'x'; 2001 2002 j++; 2003 } 2004 applog(LOG_ERR, "%s%d: %s() parse_rxstatus chain(%d) " 2005 "asic_num=%d asic_status=%08x-%s", 2006 bitmain->drv->name, bitmain->device_id, 2007 __func__, i, info->chain_asic_num[i], 2008 info->chain_asic_status[i], 2009 info->chain_asic_status_t[i]); 2010 } 2011 #else // S2 2012 memcpy(&packethead, data+i, sizeof(struct bitmain_packet_head)); 2013 packethead.length = htole16(packethead.length); 2014 2015 if (packethead.length > 1130) { 2016 applog(LOG_ERR, "%s%d: %s() rxstatus datalen=%d error", 2017 bitmain->drv->name, bitmain->device_id, 2018 __func__, packethead.length+4); 2019 continue; 2020 } 2021 if (readlen-i < packethead.length+4) { 2022 applog(LOG_ERR, "%s%d: %s() rxstatus datalen=%d<%d low", 2023 bitmain->drv->name, bitmain->device_id, 2024 __func__, readlen-i, packethead.length+4); 2025 continue; 2026 } 2027 if (bitmain_parse_rxstatus(data+i, packethead.length+4, &rxstatusdata) != 0) { 2028 applog(LOG_ERR, "%s%d: %s() parse_rxstatus error", 2029 bitmain->drv->name, bitmain->device_id, 2030 __func__); 2031 continue; 2032 } 2033 info->chain_num = rxstatusdata.chain_num; 2034 info->fifo_space = rxstatusdata.fifo_space; 2035 info->hw_version[0] = rxstatusdata.hw_version[0]; 2036 info->hw_version[1] = rxstatusdata.hw_version[1]; 2037 info->hw_version[2] = rxstatusdata.hw_version[2]; 2038 info->hw_version[3] = rxstatusdata.hw_version[3]; 2039 info->nonce_error = 0; 2040 info->last_nonce_error = 0; 2041 applog(LOG_ERR, "%s%d: %s() parse_rxstatus " 2042 "version(%d) chain_num(%d) fifo_space(%d) " 2043 "hwv1(%d) hwv2(%d) hwv3(%d) hwv4(%d) " 2044 "nonce_error(%d) freq=%d", 2045 bitmain->drv->name, bitmain->device_id, 2046 __func__, rxstatusdata.version, 2047 info->chain_num, info->fifo_space, 2048 info->hw_version[0], info->hw_version[1], 2049 info->hw_version[2], info->hw_version[3], 2050 rxstatusdata.nonce_error, 2051 info->frequency); 2052 2053 memcpy(info->chain_asic_exist, 2054 rxstatusdata.chain_asic_exist, 2055 BITMAIN_MAX_CHAIN_NUM*32); 2056 memcpy(info->chain_asic_status, 2057 rxstatusdata.chain_asic_status, 2058 BITMAIN_MAX_CHAIN_NUM*32); 2059 for (i = 0; i < rxstatusdata.chain_num; i++) { 2060 info->chain_asic_num[i] = rxstatusdata.chain_asic_num[i]; 2061 memset(info->chain_asic_status_t[i], 0, 320); 2062 j = 0; 2063 if (info->chain_asic_num[i] <= 0) 2064 asicnum = 0; 2065 else { 2066 if (info->chain_asic_num[i] % 32 == 0) 2067 asicnum = info->chain_asic_num[i] / 32; 2068 else 2069 asicnum = info->chain_asic_num[i] / 32 + 1; 2070 } 2071 if (asicnum > 0) { 2072 for (m = asicnum-1; m >= 0; m--) { 2073 for (r = 0; r < 32; r++) { 2074 if (r%8 == 0 && r != 0) { 2075 info->chain_asic_status_t[i][j] = ' '; 2076 j++; 2077 } 2078 checkbit = num2bit(r); 2079 if (rxstatusdata.chain_asic_exist[i*8+m] & checkbit) { 2080 if (rxstatusdata.chain_asic_status[i*8+m] & checkbit) 2081 info->chain_asic_status_t[i][j] = 'o'; 2082 else 2083 info->chain_asic_status_t[i][j] = 'x'; 2084 } else 2085 info->chain_asic_status_t[i][j] = '-'; 2086 j++; 2087 } 2088 info->chain_asic_status_t[i][j] = ' '; 2089 j++; 2090 } 2091 } 2092 applog(LOG_DEBUG, "%s%d: %s() chain(%d) asic_num=%d " 2093 "asic_exist=%08x%08x%08x%08x%08x%08x%08x%08x " 2094 "asic_status=%08x%08x%08x%08x%08x%08x%08x%08x", 2095 bitmain->drv->name, bitmain->device_id, 2096 __func__, i, info->chain_asic_num[i], 2097 info->chain_asic_exist[i*8+0], 2098 info->chain_asic_exist[i*8+1], 2099 info->chain_asic_exist[i*8+2], 2100 info->chain_asic_exist[i*8+3], 2101 info->chain_asic_exist[i*8+4], 2102 info->chain_asic_exist[i*8+5], 2103 info->chain_asic_exist[i*8+6], 2104 info->chain_asic_exist[i*8+7], 2105 info->chain_asic_status[i*8+0], 2106 info->chain_asic_status[i*8+1], 2107 info->chain_asic_status[i*8+2], 2108 info->chain_asic_status[i*8+3], 2109 info->chain_asic_status[i*8+4], 2110 info->chain_asic_status[i*8+5], 2111 info->chain_asic_status[i*8+6], 2112 info->chain_asic_status[i*8+7]); 2113 applog(LOG_ERR, "%s%d: %s() chain(%d) " 2114 "asic_num=%d asic_status=%s", 2115 bitmain->drv->name, bitmain->device_id, 2116 __func__, i, info->chain_asic_num[i], 2117 info->chain_asic_status_t[i]); 2118 } 2119 #endif 2120 bitmain_update_temps(bitmain, info, &rxstatusdata); 2121 statusok = 1; 2122 break; 2123 } 2124 } 2125 if (statusok) 2126 break; 2127 } 2128 } 2129 trycount--; 2130 p.tv_sec = 0; 2131 p.tv_nsec = BITMAIN_RESET_PITCH; 2132 nanosleep(&p, NULL); 2133 } 2134 2135 p.tv_sec = 0; 2136 p.tv_nsec = BITMAIN_RESET_PITCH; 2137 nanosleep(&p, NULL); 2138 2139 cgtime(&info->last_status_time); 2140 2141 if (statusok) { 2142 applog(LOG_ERR, "%s%d: %s() set_txconfig", 2143 bitmain->drv->name, bitmain->device_id, __func__); 2144 #ifdef USE_ANT_S1 2145 if (opt_bitmain_hwerror) 2146 eft = 1; 2147 else 2148 eft = 0; 2149 2150 sendlen = bitmain_set_txconfig((struct bitmain_txconfig_token *)sendbuf, 2151 1, 1, 1, 1, 1, 0, 1, eft, 2152 info->chain_num, info->asic_num, 2153 BITMAIN_DEFAULT_FAN_MAX_PWM, info->timeout, 2154 info->frequency, BITMAIN_DEFAULT_VOLTAGE, 2155 0, 0, 0x04, info->reg_data); 2156 #else // S2 2157 if (opt_bitmain_hwerror) 2158 hwerror_eft = 1; 2159 else 2160 hwerror_eft = 0; 2161 if (opt_bitmain_beeper) 2162 beeper_ctrl = 1; 2163 else 2164 beeper_ctrl = 0; 2165 if (opt_bitmain_tempoverctrl) 2166 tempover_ctrl = 1; 2167 else 2168 tempover_ctrl = 0; 2169 2170 sendlen = bitmain_set_txconfig((struct bitmain_txconfig_token *)sendbuf, 2171 1, 1, 1, 1, 1, 0, 1, hwerror_eft, 2172 beeper_ctrl, tempover_ctrl, 2173 info->chain_num, info->asic_num, 2174 BITMAIN_DEFAULT_FAN_MAX_PWM, info->timeout, 2175 info->frequency, info->voltage, 2176 0, 0, 0x04, info->reg_data); 2177 #endif 2178 if (sendlen <= 0) { 2179 applog(LOG_ERR, "%s%d: %s() set_txconfig error(%d)", 2180 bitmain->drv->name, bitmain->device_id, __func__, sendlen); 2181 return -1; 2182 } 2183 2184 ret = bitmain_send_data(sendbuf, sendlen, bitmain); 2185 if (unlikely(ret == BTM_SEND_ERROR)) { 2186 applog(LOG_ERR, "%s%d: %s() send_data error", 2187 bitmain->drv->name, bitmain->device_id, __func__); 2188 return -1; 2189 } 2190 applog(LOG_WARNING, "%s%d: %s() succeeded", 2191 bitmain->drv->name, bitmain->device_id, __func__); 2192 } else { 2193 applog(LOG_WARNING, "%s%d: %s() failed", 2194 bitmain->drv->name, bitmain->device_id, __func__); 2195 return -1; 2196 } 2197 return 0; 2198 } 2199 2200 #ifdef USE_ANT_S1 2201 static void ant_info(struct bitmain_info *info, int baud, int chain_num, int asic_num, int timeout, int frequency, uint8_t *reg_data) 2202 #else 2203 static void ant_info(struct bitmain_info *info, int baud, int chain_num, int asic_num) 2204 #endif 2205 { 2206 info->baud = baud; 2207 info->chain_num = chain_num; 2208 info->asic_num = asic_num; 2209 2210 #ifdef USE_ANT_S1 2211 info->timeout = timeout; 2212 info->frequency = frequency; 2213 memcpy(info->reg_data, reg_data, 4); 2214 info->voltage = BITMAIN_DEFAULT_VOLTAGE; 2215 #else 2216 info->timeout = BITMAIN_DEFAULT_TIMEOUT; 2217 info->frequency = BITMAIN_DEFAULT_FREQUENCY; 2218 memset(info->reg_data, BITMAIN_DEFAULT_REG_DATA, 4); 2219 info->voltage[0] = BITMAIN_VOLTAGE0_DEF; 2220 info->voltage[1] = BITMAIN_VOLTAGE1_DEF; 2221 2222 if (opt_bitmain_freq) { 2223 char buf[BUFSIZ+1]; 2224 char *colon, *colon2; 2225 uint8_t reg_data[4]; 2226 int timeout, freq; 2227 size_t len; 2228 2229 strncpy(buf, opt_bitmain_freq, sizeof(buf)); 2230 buf[sizeof(buf)-1] = '\0'; 2231 colon = strchr(buf, ':'); 2232 if (colon) 2233 *(colon++) = '\0'; 2234 timeout = atoi(buf); 2235 if (timeout > 0 && timeout <= 0xff) 2236 info->timeout = timeout; 2237 if (colon && *colon) { 2238 colon2 = strchr(colon, ':'); 2239 if (colon2) 2240 *(colon2++) = '\0'; 2241 freq = atoi(colon); 2242 if (freq >= BITMAIN_MIN_FREQUENCY && 2243 freq <= BITMAIN_MAX_FREQUENCY) { 2244 info->frequency = freq; 2245 } 2246 if (colon2 && *colon2) { 2247 len = strlen(colon2); 2248 if (len > 1 && len <= 8 && (len & 1) == 0) { 2249 memset(reg_data, BITMAIN_DEFAULT_REG_DATA, 4); 2250 if (hex2bin(reg_data, colon2, len/2)) 2251 memcpy(info->reg_data, reg_data, len/2); 2252 } 2253 } 2254 } 2255 } 2256 2257 if (opt_bitmain_voltage) { 2258 unsigned char v[2]; 2259 if (hex2bin(v, opt_bitmain_voltage, 2)) { 2260 info->voltage[0] = (uint8_t)(v[0]); 2261 info->voltage[1] = (uint8_t)(v[1]); 2262 } 2263 } 2264 #endif 2265 2266 info->fan_pwm = BITMAIN_DEFAULT_FAN_MIN_PWM; 2267 info->temp_max = 0; 2268 /* This is for check the temp/fan every 3~4s */ 2269 info->temp_history_count = (4 / (float)((float)info->timeout * ((float)1.67/0x32))) + 1; 2270 if (info->temp_history_count <= 0) 2271 info->temp_history_count = 1; 2272 2273 info->temp_history_index = 0; 2274 info->temp_sum = 0; 2275 } 2276 2277 #if (defined(USE_ANT_S1) || defined(USE_ANT_S3)) 2278 static struct cgpu_info *bitmain_detect_one(libusb_device *dev, struct usb_find_devices *found) 2279 { 2280 int baud, chain_num, asic_num = 0; 2281 #ifdef USE_ANT_S1 2282 int timeout, frequency = 0; 2283 uint8_t reg_data[4] = {0}; 2284 #endif 2285 struct bitmain_info *info; 2286 struct cgpu_info *bitmain; 2287 bool configured; 2288 int ret; 2289 2290 if (!opt_bitmain_options || !(*opt_bitmain_options)) { 2291 applog(LOG_ERR, "%s: no bitmain-options specified", ANTDRV.dname); 2292 return NULL; 2293 } 2294 2295 bitmain = usb_alloc_cgpu(&ANTDRV, BITMAIN_MINER_THREADS); 2296 2297 baud = BITMAIN_IO_SPEED; 2298 chain_num = BITMAIN_DEFAULT_CHAIN_NUM; 2299 asic_num = BITMAIN_DEFAULT_ASIC_NUM; 2300 #ifdef USE_ANT_S1 2301 timeout = BITMAIN_DEFAULT_TIMEOUT; 2302 frequency = BITMAIN_DEFAULT_FREQUENCY; 2303 #endif 2304 2305 if (!usb_init(bitmain, dev, found)) 2306 goto shin; 2307 2308 #ifdef USE_ANT_S1 2309 configured = get_options(++option_offset, &baud, &chain_num, 2310 &asic_num, &timeout, &frequency, reg_data); 2311 #else 2312 configured = get_options(++option_offset, &baud, &chain_num, &asic_num); 2313 #endif 2314 2315 /* Even though this is an FTDI type chip, we want to do the parsing 2316 * all ourselves so set it to std usb type */ 2317 bitmain->usbdev->usb_type = USB_TYPE_STD; 2318 2319 bitmain->device_data = calloc(sizeof(struct bitmain_info), 1); 2320 if (unlikely(!(bitmain->device_data))) 2321 quit(1, "Failed to calloc bitmain_info data"); 2322 info = bitmain->device_data; 2323 2324 #ifdef USE_ANT_S1 2325 if (configured) 2326 ant_info(info, baud, chain_num, asic_num, timeout, frequency, reg_data); 2327 else 2328 ant_info(info, BITMAIN_IO_SPEED, BITMAIN_DEFAULT_CHAIN_NUM, 2329 BITMAIN_DEFAULT_ASIC_NUM, BITMAIN_DEFAULT_TIMEOUT, 2330 BITMAIN_DEFAULT_FREQUENCY, reg_data); 2331 #else 2332 if (configured) 2333 ant_info(info, baud, chain_num, asic_num); 2334 else 2335 ant_info(info, BITMAIN_IO_SPEED, BITMAIN_DEFAULT_CHAIN_NUM, BITMAIN_DEFAULT_ASIC_NUM); 2336 2337 #endif 2338 2339 if (!add_cgpu(bitmain)) 2340 goto unshin; 2341 2342 applog(LOG_ERR, "%s: detected %s%d", 2343 ANTDRV.dname, bitmain->drv->name, bitmain->device_id); 2344 ret = bitmain_initialize(bitmain); 2345 if (ret && !configured) 2346 goto unshin; 2347 2348 update_usb_stats(bitmain); 2349 2350 info->errorcount = 0; 2351 2352 info->work_list = k_new_list("Work", sizeof(WITEM), ALLOC_WITEMS, LIMIT_WITEMS, true); 2353 info->work_ready = k_new_store(info->work_list); 2354 #ifdef USE_ANT_S3 2355 info->wbuild = k_new_store(info->work_list); 2356 dupalloc(bitmain, 10); 2357 #endif 2358 2359 applog(LOG_DEBUG, "%s%d: detected %s " 2360 "chain_num=%d asic_num=%d timeout=%d frequency=%d", 2361 bitmain->drv->name, bitmain->device_id, bitmain->device_path, 2362 info->chain_num, info->asic_num, info->timeout, 2363 info->frequency); 2364 2365 return bitmain; 2366 2367 unshin: 2368 2369 usb_uninit(bitmain); 2370 2371 shin: 2372 2373 free(bitmain->device_data); 2374 bitmain->device_data = NULL; 2375 2376 bitmain = usb_free_cgpu(bitmain); 2377 2378 return NULL; 2379 } 2380 #else // S2 (and only S2) 2381 static void ser_detect() 2382 { 2383 int baud, chain_num = 0, asic_num = 0; 2384 struct cgpu_info *bitmain; 2385 struct bitmain_info *info; 2386 bool configured; 2387 int ret; 2388 2389 applog(LOG_WARNING, "%s: checking for %s", ANTDRV.dname, opt_bitmain_dev); 2390 2391 if (!opt_bitmain_options || !(*opt_bitmain_options)) { 2392 applog(LOG_ERR, "%s: no bitmain-options specified", ANTDRV.dname); 2393 return; 2394 } 2395 2396 bitmain = calloc(1, sizeof(*bitmain)); 2397 if (unlikely(!bitmain)) 2398 quithere(1, "Failed to calloc bitmain"); 2399 2400 bitmain->drv = &ANTDRV; 2401 bitmain->deven = DEV_ENABLED; 2402 bitmain->threads = 1; 2403 2404 configured = get_options(++option_offset, &baud, &chain_num, &asic_num); 2405 2406 info = calloc(1, sizeof(*info)); 2407 if (unlikely(!info)) 2408 quit(1, "Failed to calloc bitmain_info"); 2409 bitmain->device_data = (void *)info; 2410 2411 info->device_fd = open(opt_bitmain_dev, O_RDWR|O_EXCL|O_NONBLOCK); 2412 if (info->device_fd == -1) { 2413 applog(LOG_DEBUG, "%s open %s error %d", 2414 bitmain->drv->dname, opt_bitmain_dev, errno); 2415 goto giveup; 2416 } 2417 2418 bitmain->device_path = strdup(opt_bitmain_dev); 2419 2420 if (configured) 2421 ant_info(info, baud, chain_num, asic_num); 2422 else 2423 ant_info(info, BITMAIN_IO_SPEED, BITMAIN_DEFAULT_CHAIN_NUM, BITMAIN_DEFAULT_ASIC_NUM); 2424 2425 if (!add_cgpu(bitmain)) 2426 goto cleen; 2427 2428 ret = bitmain_initialize(bitmain); 2429 if (ret && !configured) 2430 goto cleen; 2431 2432 info->errorcount = 0; 2433 2434 info->work_list = k_new_list("Work", sizeof(WITEM), ALLOC_WITEMS, LIMIT_WITEMS, true); 2435 info->work_ready = k_new_store(info->work_list); 2436 info->wbuild = k_new_store(info->work_list); 2437 2438 applog(LOG_DEBUG, "%s%d: detected %s " 2439 "chain_num=%d asic_num=%d timeout=%d frequency=%d", 2440 bitmain->drv->name, bitmain->device_id, bitmain->device_path, 2441 info->chain_num, info->asic_num, info->timeout, 2442 info->frequency); 2443 2444 dupalloc(bitmain, 10); 2445 2446 return; 2447 2448 cleen: 2449 if (info->device_fd != -1) 2450 close(info->device_fd); 2451 free(bitmain->device_path); 2452 2453 giveup: 2454 free(info); 2455 free(bitmain); 2456 } 2457 #endif 2458 2459 #ifdef USE_ANT_S1 2460 static void ants1_detect(bool __maybe_unused hotplug) 2461 { 2462 usb_detect(&ANTDRV, bitmain_detect_one); 2463 } 2464 #endif 2465 2466 #ifdef USE_ANT_S3 2467 static void ants3_detect(bool __maybe_unused hotplug) 2468 { 2469 usb_detect(&ANTDRV, bitmain_detect_one); 2470 } 2471 #else 2472 #ifdef USE_ANT_S2 2473 static bool first_ant = true; 2474 2475 static void ants2_detect(bool __maybe_unused hotplug) 2476 { 2477 // Only ever do this once 2478 if (!first_ant) 2479 return; 2480 2481 first_ant = false; 2482 2483 ser_detect(); 2484 } 2485 #endif 2486 #endif 2487 2488 static void do_bitmain_close(struct thr_info *thr) 2489 { 2490 struct cgpu_info *bitmain = thr->cgpu; 2491 struct bitmain_info *info = bitmain->device_data; 2492 2493 pthread_join(info->read_thr, NULL); 2494 bitmain_running_reset(info); 2495 2496 info->no_matching_work = 0; 2497 2498 cgsem_destroy(&info->write_sem); 2499 } 2500 2501 static void get_bitmain_statline_before(char *buf, size_t bufsiz, struct cgpu_info *bitmain) 2502 { 2503 struct bitmain_info *info = bitmain->device_data; 2504 int lowfan = 10000; 2505 int i = 0; 2506 2507 /* Find the lowest fan speed of the ASIC cooling fans. */ 2508 for (i = 0; i < info->fan_num; i++) { 2509 if (info->fan[i] >= 0 && info->fan[i] < lowfan) 2510 lowfan = info->fan[i]; 2511 } 2512 2513 tailsprintf(buf, bufsiz, "%2d/%3dC %04dR", info->temp_avg, info->temp_max, lowfan); 2514 } 2515 2516 /* We use a replacement algorithm to only remove references to work done from 2517 * the buffer when we need the extra space for new work. */ 2518 static bool bitmain_fill(struct cgpu_info *bitmain) 2519 { 2520 struct bitmain_info *info = bitmain->device_data; 2521 struct work *work, *usework; 2522 bool ret = true, dodelay = false; 2523 int sendret = 0, sentcount = 0, neednum = 0, queuednum = 0, sendnum = 0, sendlen = 0; 2524 int roll, roll_limit; 2525 uint8_t sendbuf[BITMAIN_SENDBUF_SIZE]; 2526 cgtimer_t ts_start; 2527 int senderror = 0; 2528 struct timeval now; 2529 int timediff = 0; 2530 K_ITEM *witem; 2531 2532 #ifdef USE_ANT_S1 2533 /* 2534 * Overheat just means delay the next work 2535 * since the temperature reply is only found with a work reply, 2536 * we can only sleep and hope it will cool down 2537 * TODO: of course it may be possible to read the temperature 2538 * without sending work ... 2539 */ 2540 if (info->overheat == true) { 2541 if (info->overheat_sleep_ms == 0) 2542 info->overheat_sleep_ms = BITMAIN_OVERHEAT_SLEEP_MS_DEF; 2543 2544 /* 2545 * If we slept and we are still here, and the temp didn't drop, 2546 * increment the sleep time to find a sleep time that causes a 2547 * temperature drop 2548 */ 2549 if (info->overheat_slept) { 2550 if (info->overheat_temp > info->temp_hi) 2551 info->overheat_temp = info->temp_hi; 2552 else { 2553 if (info->overheat_sleep_ms < BITMAIN_OVERHEAT_SLEEP_MS_MAX) 2554 info->overheat_sleep_ms += BITMAIN_OVERHEAT_SLEEP_MS_STEP; 2555 } 2556 } 2557 2558 applog(LOG_DEBUG, "%s%d: %s() sleeping %"PRIu32" - overheated", 2559 bitmain->drv->name, bitmain->device_id, 2560 __func__, info->overheat_sleep_ms); 2561 cgsleep_ms(info->overheat_sleep_ms); 2562 info->overheat_sleeps++; 2563 info->overheat_slept = info->overheat_sleep_ms; 2564 info->overheat_total_sleep += info->overheat_sleep_ms; 2565 } else { 2566 // If we slept and it cooled then try less next time 2567 if (info->overheat_slept) { 2568 if (info->overheat_sleep_ms > BITMAIN_OVERHEAT_SLEEP_MS_MIN) 2569 info->overheat_sleep_ms -= BITMAIN_OVERHEAT_SLEEP_MS_STEP; 2570 info->overheat_slept = 0; 2571 } 2572 2573 } 2574 #endif 2575 2576 applog(LOG_DEBUG, "%s%d: %s() start", 2577 bitmain->drv->name, bitmain->device_id, 2578 __func__); 2579 info->fill_calls++; 2580 mutex_lock(&info->qlock); 2581 if (info->fifo_space <= 0) { 2582 applog(LOG_DEBUG, "%s%d: %s() fifo space empty", 2583 bitmain->drv->name, bitmain->device_id, 2584 __func__); 2585 ret = true; 2586 info->fill_nospace++; 2587 dodelay = true; 2588 goto out_unlock; 2589 } 2590 2591 if (info->queued >= BITMAIN_MAX_WORK_QUEUE_NUM) 2592 ret = true; 2593 else 2594 ret = false; 2595 2596 if (info->fifo_space > 0) { 2597 work = get_queued(bitmain); 2598 if (!work) { 2599 if (info->fifo_space < BITMAIN_MAX_WORK_NUM) 2600 neednum = info->fifo_space; 2601 else 2602 neednum = BITMAIN_MAX_WORK_NUM; 2603 2604 queuednum = info->queued; 2605 2606 int need = neednum - queuednum; 2607 if (need > BITMAIN_MAX_WORK_NUM) { 2608 need = BITMAIN_MAX_WORK_NUM; 2609 info->need_over++; 2610 } else { 2611 if (need < 0) 2612 need = 0; 2613 } 2614 info->fill_nowork++; 2615 info->need_nowork[need]++; 2616 goto out_unlock; 2617 } 2618 2619 roll_limit = work->drv_rolllimit; 2620 info->fill_rolltot += roll_limit; 2621 if (info->fill_roll == 0) 2622 info->fill_rollmin = info->fill_rollmax = roll_limit; 2623 else { 2624 if (info->fill_rollmin > roll_limit) 2625 info->fill_rollmin = roll_limit; 2626 if (info->fill_rollmax < roll_limit) 2627 info->fill_rollmax = roll_limit; 2628 } 2629 info->fill_roll++; 2630 roll = 0; 2631 } 2632 2633 while (info->fifo_space > 0 && roll <= roll_limit) { 2634 info->fifo_checks++; 2635 if (info->fifo_space < BITMAIN_MAX_WORK_NUM) { 2636 neednum = info->fifo_space; 2637 info->fill_neededless++; 2638 } else 2639 neednum = BITMAIN_MAX_WORK_NUM; 2640 2641 info->fill_totalneeded += neednum; 2642 info->fill_need[neednum]++; 2643 2644 queuednum = info->queued; 2645 applog(LOG_DEBUG, "%s%d: Work task queued(%d) fifo space(%d) needsend(%d)", 2646 bitmain->drv->name, bitmain->device_id, 2647 queuednum, info->fifo_space, neednum); 2648 info->fill_want += (neednum - queuednum); 2649 while (queuednum < neednum && roll <= roll_limit) { 2650 applog(LOG_DEBUG, "%s%d: get work queued number:%d" 2651 " neednum:%d", 2652 bitmain->drv->name, 2653 bitmain->device_id, 2654 queuednum, neednum); 2655 2656 // Using devflag to say if it was rolled 2657 if (roll == 0) { 2658 usework = work; 2659 usework->devflag = false; 2660 } else { 2661 usework = copy_work_noffset(work, roll); 2662 usework->devflag = true; 2663 } 2664 2665 witem = k_unlink_tail(info->work_list); 2666 if (DATAW(witem)->work) { 2667 // Was it rolled? 2668 if (DATAW(witem)->work->devflag) 2669 free_work(DATAW(witem)->work); 2670 else 2671 work_completed(bitmain, DATAW(witem)->work); 2672 } 2673 DATAW(witem)->work = usework; 2674 DATAW(witem)->wid = ++info->last_wid; 2675 info->queued++; 2676 k_add_head(info->work_ready, witem); 2677 queuednum++; 2678 roll++; 2679 } 2680 if (queuednum < BITMAIN_MAX_DEAL_QUEUE_NUM) { 2681 if (queuednum < neednum) { 2682 info->fill_toosmall++; 2683 info->fill_less += (neednum - queuednum); 2684 applog(LOG_DEBUG, "%s%d: Not enough work to send, queue num=%d", 2685 bitmain->drv->name, bitmain->device_id, 2686 queuednum); 2687 goto out_unlock; 2688 } 2689 } 2690 info->fill_sends++; 2691 sendnum = queuednum < neednum ? queuednum : neednum; 2692 info->fill_totalsend += sendnum; 2693 info->fill_send[sendnum]++; 2694 info->fill_sendless[neednum - sendnum]++; 2695 sendlen = bitmain_set_txtask(info, sendbuf, &(info->last_work_block), &sentcount); 2696 info->queued -= sendnum; 2697 info->send_full_space += sendnum; 2698 if (info->queued < 0) 2699 info->queued = 0; 2700 2701 applog(LOG_DEBUG, "%s%d: Send work %d", 2702 bitmain->drv->name, bitmain->device_id, 2703 sentcount); 2704 if (sendlen > 0) { 2705 info->fifo_space -= sentcount; 2706 if (info->fifo_space < 0) 2707 info->fifo_space = 0; 2708 sendret = bitmain_send_data(sendbuf, sendlen, bitmain); 2709 if (unlikely(sendret == BTM_SEND_ERROR)) { 2710 info->fill_senderr++; 2711 applog(LOG_ERR, "%s%d: send work comms error", 2712 bitmain->drv->name, bitmain->device_id); 2713 //dev_error(bitmain, REASON_DEV_COMMS_ERROR); 2714 info->reset = true; 2715 info->errorcount++; 2716 senderror = 1; 2717 if (info->errorcount > 1000) { 2718 info->errorcount = 0; 2719 applog(LOG_ERR, "%s%d: Device disappeared," 2720 " shutting down thread", 2721 bitmain->drv->name, bitmain->device_id); 2722 bitmain->shutdown = true; 2723 } 2724 goto out_unlock; 2725 } else { 2726 applog(LOG_DEBUG, "%s%d: send_data ret=%d", 2727 bitmain->drv->name, bitmain->device_id, 2728 sendret); 2729 info->errorcount = 0; 2730 } 2731 } else { 2732 info->fill_seterr++; 2733 applog(LOG_DEBUG, "%s%d: Send work set_txtask error: %d", 2734 bitmain->drv->name, bitmain->device_id, 2735 sendlen); 2736 } 2737 } 2738 2739 out_unlock: 2740 cgtime(&now); 2741 timediff = now.tv_sec - info->last_status_time.tv_sec; 2742 if (timediff < 0) 2743 timediff = -timediff; 2744 if (timediff > BITMAIN_SEND_STATUS_TIME) { 2745 info->fill_sendstatus++; 2746 applog(LOG_DEBUG, "%s%d: Send RX Status Token fifo_space(%d) timediff(%d)", 2747 bitmain->drv->name, bitmain->device_id, 2748 info->fifo_space, timediff); 2749 copy_time(&(info->last_status_time), &now); 2750 2751 sendlen = bitmain_set_rxstatus((struct bitmain_rxstatus_token *) sendbuf, 0, 0, 0, 0); 2752 if (sendlen > 0) { 2753 sendret = bitmain_send_data(sendbuf, sendlen, bitmain); 2754 if (unlikely(sendret == BTM_SEND_ERROR)) { 2755 applog(LOG_ERR, "%s%d: send status comms error", 2756 bitmain->drv->name, bitmain->device_id); 2757 //dev_error(bitmain, REASON_DEV_COMMS_ERROR); 2758 info->reset = true; 2759 info->errorcount++; 2760 senderror = 1; 2761 if (info->errorcount > 1000) { 2762 info->errorcount = 0; 2763 applog(LOG_ERR, "%s%d: Device disappeared," 2764 " shutting down thread", 2765 bitmain->drv->name, bitmain->device_id); 2766 bitmain->shutdown = true; 2767 } 2768 } else { 2769 info->errorcount = 0; 2770 if (info->fifo_space <= 0) { 2771 senderror = 1; 2772 } 2773 } 2774 } 2775 } 2776 if (info->send_full_space > BITMAIN_SEND_FULL_SPACE) { 2777 info->send_full_space = 0; 2778 ret = true; 2779 } 2780 mutex_unlock(&info->qlock); 2781 if (senderror) { 2782 ret = true; 2783 cgsleep_prepare_r(&ts_start); 2784 cgsleep_ms_r(&ts_start, 50); 2785 } else { 2786 if (dodelay) 2787 cgsleep_ms(opt_bitmain_workdelay); 2788 } 2789 return ret; 2790 } 2791 2792 static int64_t bitmain_scanhash(struct thr_info *thr) 2793 { 2794 struct cgpu_info *bitmain = thr->cgpu; 2795 struct bitmain_info *info = bitmain->device_data; 2796 const int chain_num = info->chain_num; 2797 struct timeval now, then, tdiff; 2798 int64_t hash_count, us_timeout; 2799 2800 /* Half nonce range */ 2801 us_timeout = 0x80000000ll / info->asic_num / info->frequency; 2802 tdiff.tv_sec = us_timeout / 1000000; 2803 tdiff.tv_usec = us_timeout - (tdiff.tv_sec * 1000000); 2804 cgtime(&now); 2805 timeradd(&now, &tdiff, &then); 2806 2807 mutex_lock(&info->qlock); 2808 hash_count = 0xffffffffull * (uint64_t)info->nonces; 2809 info->results += info->nonces; 2810 if (info->results > chain_num) 2811 info->results = chain_num; 2812 if (!info->reset) 2813 info->results--; 2814 info->nonces = 0; 2815 mutex_unlock(&info->qlock); 2816 2817 /* Check for nothing but consecutive bad results or consistently less 2818 * results than we should be getting and reset the FPGA if necessary */ 2819 //if (info->results < -chain_num && !info->reset) { 2820 // applog(LOG_ERR, "%s%d: Result return rate low, resetting!", 2821 // bitmain->drv->name, bitmain->device_id); 2822 // info->reset = true; 2823 //} 2824 2825 #if (defined(USE_ANT_S1) || defined(USE_ANT_S3)) 2826 if (unlikely(bitmain->usbinfo.nodev)) { 2827 applog(LOG_ERR, "%s%d: Device disappeared, shutting down thread", 2828 bitmain->drv->name, bitmain->device_id); 2829 bitmain->shutdown = true; 2830 } 2831 #endif 2832 2833 /* This hashmeter is just a utility counter based on returned shares */ 2834 return hash_count; 2835 } 2836 2837 static void bitmain_flush_work(struct cgpu_info *bitmain) 2838 { 2839 struct bitmain_info *info = bitmain->device_data; 2840 //int i = 0; 2841 2842 applog(LOG_ERR, "%s%d: %s() queued=%d", 2843 bitmain->drv->name, bitmain->device_id, 2844 __func__, info->queued); 2845 mutex_lock(&info->qlock); 2846 /* Will overwrite any work queued */ 2847 info->queued = 0; 2848 k_list_transfer_to_tail(info->work_ready, info->work_list); 2849 //pthread_cond_signal(&info->qcond); 2850 mutex_unlock(&info->qlock); 2851 } 2852 2853 static struct api_data *bitmain_api_stats(struct cgpu_info *cgpu) 2854 { 2855 struct api_data *root = NULL; 2856 struct bitmain_info *info = cgpu->device_data; 2857 char needbuf[16 * (BITMAIN_MAX_WORK_NUM + 1)], *needptr; 2858 char fillbuf[64]; 2859 char rollbuf[64]; 2860 char regbuf[16]; 2861 float avg; 2862 int i = 0; 2863 size_t len; 2864 double hwp = (cgpu->hw_errors + cgpu->diff1) ? 2865 (double)(cgpu->hw_errors) / (double)(cgpu->hw_errors + cgpu->diff1) : 0; 2866 2867 root = api_add_int(root, "baud", &(info->baud), false); 2868 root = api_add_int(root, "miner_count", &(info->chain_num), false); 2869 root = api_add_int(root, "asic_count", &(info->asic_num), false); 2870 root = api_add_int(root, "timeout", &(info->timeout), false); 2871 root = api_add_int(root, "frequency", &(info->frequency), false); 2872 snprintf(regbuf, sizeof(regbuf), "0x%02x%02x%02x%02x", 2873 (int)(info->reg_data[0]), (int)(info->reg_data[1]), 2874 (int)(info->reg_data[2]), (int)(info->reg_data[3])); 2875 root = api_add_string(root, "regdata", regbuf, true); 2876 #ifdef USE_ANT_S1 2877 root = api_add_int(root, "voltage", &(info->voltage), false); 2878 #else 2879 char vbuf[8]; 2880 snprintf(vbuf, sizeof(vbuf), "0x%02x%02x", 2881 (int)(info->voltage[0]), (int)(info->voltage[1])); 2882 root = api_add_string(root, "voltage", vbuf, true); 2883 #endif 2884 #ifdef USE_ANT_S2 2885 root = api_add_int(root, "hwv1", &(info->hw_version[0]), false); 2886 root = api_add_int(root, "hwv2", &(info->hw_version[1]), false); 2887 root = api_add_int(root, "hwv3", &(info->hw_version[2]), false); 2888 root = api_add_int(root, "hwv4", &(info->hw_version[3]), false); 2889 #endif 2890 2891 root = api_add_int(root, "fan_num", &(info->fan_num), false); 2892 root = api_add_int(root, "fan1", &(info->fan[0]), false); 2893 root = api_add_int(root, "fan2", &(info->fan[1]), false); 2894 root = api_add_int(root, "fan3", &(info->fan[2]), false); 2895 root = api_add_int(root, "fan4", &(info->fan[3]), false); 2896 #ifdef USE_ANT_S2 2897 root = api_add_int(root, "fan5", &(info->fan[4]), false); 2898 root = api_add_int(root, "fan6", &(info->fan[5]), false); 2899 root = api_add_int(root, "fan7", &(info->fan[6]), false); 2900 root = api_add_int(root, "fan8", &(info->fan[7]), false); 2901 root = api_add_int(root, "fan9", &(info->fan[8]), false); 2902 root = api_add_int(root, "fan10", &(info->fan[9]), false); 2903 root = api_add_int(root, "fan11", &(info->fan[10]), false); 2904 root = api_add_int(root, "fan12", &(info->fan[11]), false); 2905 root = api_add_int(root, "fan13", &(info->fan[12]), false); 2906 root = api_add_int(root, "fan14", &(info->fan[13]), false); 2907 root = api_add_int(root, "fan15", &(info->fan[14]), false); 2908 root = api_add_int(root, "fan16", &(info->fan[15]), false); 2909 #endif 2910 2911 root = api_add_int(root, "temp_num", &(info->temp_num), false); 2912 root = api_add_int(root, "temp1", &(info->temp[0]), false); 2913 root = api_add_int(root, "temp2", &(info->temp[1]), false); 2914 root = api_add_int(root, "temp3", &(info->temp[2]), false); 2915 root = api_add_int(root, "temp4", &(info->temp[3]), false); 2916 #ifdef USE_ANT_S2 2917 root = api_add_int(root, "temp5", &(info->temp[4]), false); 2918 root = api_add_int(root, "temp6", &(info->temp[5]), false); 2919 root = api_add_int(root, "temp7", &(info->temp[6]), false); 2920 root = api_add_int(root, "temp8", &(info->temp[7]), false); 2921 root = api_add_int(root, "temp9", &(info->temp[8]), false); 2922 root = api_add_int(root, "temp10", &(info->temp[9]), false); 2923 root = api_add_int(root, "temp11", &(info->temp[10]), false); 2924 root = api_add_int(root, "temp12", &(info->temp[11]), false); 2925 root = api_add_int(root, "temp13", &(info->temp[12]), false); 2926 root = api_add_int(root, "temp14", &(info->temp[13]), false); 2927 root = api_add_int(root, "temp15", &(info->temp[14]), false); 2928 root = api_add_int(root, "temp16", &(info->temp[15]), false); 2929 #endif 2930 root = api_add_int(root, "temp_avg", &(info->temp_avg), true); 2931 root = api_add_int(root, "temp_max", &(info->temp_max), true); 2932 root = api_add_percent(root, "Device Hardware%", &hwp, true); 2933 root = api_add_int(root, "no_matching_work", &(info->no_matching_work), true); 2934 /* 2935 for (i = 0; i < info->chain_num; i++) { 2936 char mcw[24]; 2937 2938 sprintf(mcw, "match_work_count%d", i + 1); 2939 root = api_add_int(root, mcw, &(info->matching_work[i]), false); 2940 }*/ 2941 2942 root = api_add_int(root, "chain_acn1", &(info->chain_asic_num[0]), false); 2943 root = api_add_int(root, "chain_acn2", &(info->chain_asic_num[1]), false); 2944 root = api_add_int(root, "chain_acn3", &(info->chain_asic_num[2]), false); 2945 root = api_add_int(root, "chain_acn4", &(info->chain_asic_num[3]), false); 2946 #ifdef USE_ANT_S2 2947 root = api_add_int(root, "chain_acn5", &(info->chain_asic_num[4]), false); 2948 root = api_add_int(root, "chain_acn6", &(info->chain_asic_num[5]), false); 2949 root = api_add_int(root, "chain_acn7", &(info->chain_asic_num[6]), false); 2950 root = api_add_int(root, "chain_acn8", &(info->chain_asic_num[7]), false); 2951 root = api_add_int(root, "chain_acn9", &(info->chain_asic_num[8]), false); 2952 root = api_add_int(root, "chain_acn10", &(info->chain_asic_num[9]), false); 2953 root = api_add_int(root, "chain_acn11", &(info->chain_asic_num[10]), false); 2954 root = api_add_int(root, "chain_acn12", &(info->chain_asic_num[11]), false); 2955 root = api_add_int(root, "chain_acn13", &(info->chain_asic_num[12]), false); 2956 root = api_add_int(root, "chain_acn14", &(info->chain_asic_num[13]), false); 2957 root = api_add_int(root, "chain_acn15", &(info->chain_asic_num[14]), false); 2958 root = api_add_int(root, "chain_acn16", &(info->chain_asic_num[15]), false); 2959 #endif 2960 2961 root = api_add_string(root, "chain_acs1", info->chain_asic_status_t[0], false); 2962 root = api_add_string(root, "chain_acs2", info->chain_asic_status_t[1], false); 2963 root = api_add_string(root, "chain_acs3", info->chain_asic_status_t[2], false); 2964 root = api_add_string(root, "chain_acs4", info->chain_asic_status_t[3], false); 2965 #ifdef USE_ANT_S2 2966 root = api_add_string(root, "chain_acs5", info->chain_asic_status_t[4], false); 2967 root = api_add_string(root, "chain_acs6", info->chain_asic_status_t[5], false); 2968 root = api_add_string(root, "chain_acs7", info->chain_asic_status_t[6], false); 2969 root = api_add_string(root, "chain_acs8", info->chain_asic_status_t[7], false); 2970 root = api_add_string(root, "chain_acs9", info->chain_asic_status_t[8], false); 2971 root = api_add_string(root, "chain_acs10", info->chain_asic_status_t[9], false); 2972 root = api_add_string(root, "chain_acs11", info->chain_asic_status_t[10], false); 2973 root = api_add_string(root, "chain_acs12", info->chain_asic_status_t[11], false); 2974 root = api_add_string(root, "chain_acs13", info->chain_asic_status_t[12], false); 2975 root = api_add_string(root, "chain_acs14", info->chain_asic_status_t[13], false); 2976 root = api_add_string(root, "chain_acs15", info->chain_asic_status_t[14], false); 2977 root = api_add_string(root, "chain_acs16", info->chain_asic_status_t[15], false); 2978 #endif 2979 2980 root = api_add_int(root, "work_list_total", &(info->work_list->total), true); 2981 root = api_add_int(root, "work_list_count", &(info->work_list->count), true); 2982 root = api_add_int(root, "work_ready_count", &(info->work_ready->count), true); 2983 root = api_add_uint64(root, "work_search", &(info->work_search), true); 2984 root = api_add_uint64(root, "min_search", &(info->min_search), true); 2985 root = api_add_uint64(root, "max_search", &(info->max_search), true); 2986 avg = info->work_search ? (float)(info->tot_search) / 2987 (float)(info->work_search) : 0; 2988 root = api_add_avg(root, "avg_search", &avg, true); 2989 root = api_add_uint64(root, "failed_search", &(info->failed_search), true); 2990 root = api_add_uint64(root, "min_failed", &(info->min_failed), true); 2991 root = api_add_uint64(root, "max_failed", &(info->max_failed), true); 2992 avg = info->failed_search ? (float)(info->tot_failed) / 2993 (float)(info->failed_search) : 0; 2994 root = api_add_avg(root, "avg_failed", &avg, true); 2995 2996 root = api_add_int(root, "temp_hi", &(info->temp_hi), false); 2997 #ifdef USE_ANT_S1 2998 root = api_add_bool(root, "overheat", &(info->overheat), true); 2999 root = api_add_int(root, "overheat_temp", &(info->overheat_temp), true); 3000 root = api_add_uint32(root, "overheat_count", &(info->overheat_count), true); 3001 root = api_add_uint32(root, "overheat_sleep_ms", &(info->overheat_sleep_ms), true); 3002 root = api_add_uint32(root, "overheat_sleeps", &(info->overheat_sleeps), true); 3003 root = api_add_uint32(root, "overheat_slept", &(info->overheat_slept), true); 3004 root = api_add_uint64(root, "overheat_total_sleep", &(info->overheat_total_sleep), true); 3005 root = api_add_uint32(root, "overheat_recovers", &(info->overheat_recovers), true); 3006 #endif 3007 root = api_add_uint64(root, "fill_calls", &(info->fill_calls), true); 3008 root = api_add_uint64(root, "fill_nospace", &(info->fill_nospace), true); 3009 root = api_add_uint64(root, "fifo_checks", &(info->fifo_checks), true); 3010 root = api_add_uint64(root, "fill_neededless", &(info->fill_neededless), true); 3011 avg = info->fifo_checks ? (float)(info->fill_totalneeded) / 3012 (float)(info->fifo_checks) : 0; 3013 snprintf(fillbuf, sizeof(fillbuf), "%"PRId64"/%.2fav", info->fill_totalneeded, avg); 3014 root = api_add_string(root, "fill_needed", fillbuf, true); 3015 needptr = needbuf; 3016 for (i = 0; i <= BITMAIN_MAX_WORK_NUM; i++) { 3017 len = sizeof(needbuf) - (needptr - needbuf); 3018 if (len > 1) { 3019 snprintf(needptr, len, "%s%"PRIu64, 3020 i ? "/" : "", info->fill_need[i]); 3021 needptr += strlen(needptr); 3022 } 3023 } 3024 root = api_add_string(root, "fill_need", needbuf, true); 3025 avg = info->fifo_checks ? (float)(info->fill_want) / 3026 (float)(info->fifo_checks) : 0; 3027 snprintf(fillbuf, sizeof(fillbuf), "%"PRId64"/%.2fav", info->fill_want, avg); 3028 root = api_add_string(root, "fill_want", fillbuf, true); 3029 root = api_add_uint64(root, "fill_nowork", &(info->fill_nowork), true); 3030 root = api_add_uint64(root, "fill_toosmall", &(info->fill_toosmall), true); 3031 avg = info->fill_toosmall ? (float)(info->fill_less) / 3032 (float)(info->fill_toosmall) : 0; 3033 snprintf(fillbuf, sizeof(fillbuf), "%"PRId64" %.2fav", info->fill_less, avg); 3034 root = api_add_string(root, "fill_less", fillbuf, true); 3035 root = api_add_uint64(root, "fill_sends", &(info->fill_sends), true); 3036 avg = info->fill_sends ? (float)(info->fill_totalsend) / 3037 (float)(info->fill_sends) : 0; 3038 snprintf(fillbuf, sizeof(fillbuf), "%"PRId64"/%.2fav", info->fill_totalsend, avg); 3039 root = api_add_string(root, "fill_totalsend", fillbuf, true); 3040 needptr = needbuf; 3041 for (i = 0; i <= BITMAIN_MAX_WORK_NUM; i++) { 3042 len = sizeof(needbuf) - (needptr - needbuf); 3043 if (len > 1) { 3044 snprintf(needptr, len, "%s%"PRIu64, 3045 i ? "/" : "", info->fill_send[i]); 3046 needptr += strlen(needptr); 3047 } 3048 } 3049 root = api_add_string(root, "fill_send", needbuf, true); 3050 needptr = needbuf; 3051 for (i = 0; i <= BITMAIN_MAX_WORK_NUM; i++) { 3052 len = sizeof(needbuf) - (needptr - needbuf); 3053 if (len > 1) { 3054 snprintf(needptr, len, "%s%"PRIu64, 3055 i ? "/" : "", info->fill_sendless[i]); 3056 needptr += strlen(needptr); 3057 } 3058 } 3059 root = api_add_string(root, "fill_sendless", needbuf, true); 3060 root = api_add_uint64(root, "fill_seterr", &(info->fill_seterr), true); 3061 root = api_add_uint64(root, "fill_senderr", &(info->fill_senderr), true); 3062 3063 needptr = needbuf; 3064 for (i = 0; i <= BITMAIN_MAX_WORK_NUM; i++) { 3065 len = sizeof(needbuf) - (needptr - needbuf); 3066 if (len > 1) { 3067 snprintf(needptr, len, "%s%"PRIu64, 3068 i ? "/" : "", info->need_nowork[i]); 3069 needptr += strlen(needptr); 3070 } 3071 } 3072 len = sizeof(needbuf) - (needptr - needbuf); 3073 if (len > 1) 3074 snprintf(needptr, len, " %"PRIu64, info->need_over); 3075 root = api_add_string(root, "need_nowork", needbuf, true); 3076 avg = info->fill_roll ? (float)(info->fill_rolltot) / (float)(info->fill_roll) : 0; 3077 snprintf(rollbuf, sizeof(rollbuf), "%"PRIu64"/%d/%d/%.2fav", 3078 info->fill_roll, info->fill_rollmin, info->fill_rollmax, avg); 3079 root = api_add_string(root, "roll", rollbuf, true); 3080 i = BITMAIN_SEND_STATUS_TIME; 3081 root = api_add_int(root, "send_status_time", &i, true); 3082 root = api_add_timeval(root, "last_status_time", &(info->last_status_time), true); 3083 root = api_add_uint64(root, "fill_sendstatus", &(info->fill_sendstatus), true); 3084 root = api_add_uint64(root, "read_good", &(info->read_good), true); 3085 avg = info->read_good ? (float)(info->read_size) / (float)(info->read_good) : 0; 3086 snprintf(fillbuf, sizeof(fillbuf), "%"PRIu64"/%d/%d/%.2fav", 3087 info->read_size, info->read_sizemin, info->read_sizemax, avg); 3088 root = api_add_string(root, "read_size", fillbuf, true); 3089 root = api_add_uint64(root, "read_18s", &(info->read_18s), true); 3090 root = api_add_uint64(root, "read_bad", &(info->readbuf_over), true); 3091 root = api_add_uint64(root, "readbuf_over", &(info->readbuf_over), true); 3092 3093 #ifdef USE_ANT_S2 3094 root = api_add_bool(root, "opt_bitmain_beeper", &opt_bitmain_beeper, false); 3095 root = api_add_bool(root, "opt_bitmain_tempoverctrl", &opt_bitmain_tempoverctrl, false); 3096 #endif 3097 root = api_add_int(root, "opt_bitmain_temp", &opt_bitmain_temp, false); 3098 root = api_add_int(root, "opt_bitmain_workdelay", &opt_bitmain_workdelay, false); 3099 root = api_add_int(root, "opt_bitmain_overheat", &opt_bitmain_overheat, false); 3100 root = api_add_int(root, "opt_bitmain_fan_min", &opt_bitmain_fan_min, false); 3101 root = api_add_int(root, "opt_bitmain_fan_max", &opt_bitmain_fan_max, false); 3102 root = api_add_bool(root, "opt_bitmain_auto", &opt_bitmain_auto, false); 3103 3104 return root; 3105 } 3106 3107 static void bitmain_shutdown(struct thr_info *thr) 3108 { 3109 do_bitmain_close(thr); 3110 } 3111 3112 char *set_bitmain_fan(char *arg) 3113 { 3114 int val1, val2, ret; 3115 3116 ret = sscanf(arg, "%d-%d", &val1, &val2); 3117 if (ret < 1) 3118 return "No values passed to bitmain-fan"; 3119 if (ret == 1) 3120 val2 = val1; 3121 3122 if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1) 3123 return "Invalid value passed to bitmain-fan"; 3124 3125 opt_bitmain_fan_min = val1 * BITMAIN_PWM_MAX / 100; 3126 opt_bitmain_fan_max = val2 * BITMAIN_PWM_MAX / 100; 3127 3128 return NULL; 3129 } 3130 #endif // LINUX 3131 3132 #ifdef USE_ANT_S1 3133 struct device_drv ants1_drv = { 3134 .drv_id = DRIVER_ants1, 3135 .dname = "BitmainAntS1", 3136 .name = "ANT", 3137 .drv_detect = ants1_detect, 3138 #ifdef LINUX 3139 .thread_prepare = bitmain_prepare, 3140 .hash_work = hash_queued_work, 3141 .queue_full = bitmain_fill, 3142 .scanwork = bitmain_scanhash, 3143 .flush_work = bitmain_flush_work, 3144 .get_api_stats = bitmain_api_stats, 3145 .get_statline_before = get_bitmain_statline_before, 3146 .reinit_device = bitmain_init, 3147 .thread_shutdown = bitmain_shutdown, 3148 #endif 3149 }; 3150 #endif 3151 3152 #ifdef USE_ANT_S2 3153 #ifdef USE_ANT_S3 3154 struct device_drv ants3_drv = { 3155 .drv_id = DRIVER_ants3, 3156 .dname = "BitmainAntS3", 3157 .name = "AS3", 3158 .drv_detect = ants3_detect, 3159 #else 3160 struct device_drv ants2_drv = { 3161 .drv_id = DRIVER_ants2, 3162 .dname = "BitmainAntS2", 3163 .name = "AS2", 3164 .drv_detect = ants2_detect, 3165 #endif 3166 #ifdef LINUX 3167 .thread_prepare = bitmain_prepare, 3168 .hash_work = hash_queued_work, 3169 .queue_full = bitmain_fill, 3170 .scanwork = bitmain_scanhash, 3171 .flush_work = bitmain_flush_work, 3172 .get_api_stats = bitmain_api_stats, 3173 .get_statline_before = get_bitmain_statline_before, 3174 .reinit_device = bitmain_init, 3175 .thread_shutdown = bitmain_shutdown, 3176 #endif 3177 }; 3178 #endif