driver-avalon.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License as published by the Free
  4. * Software Foundation; either version 3 of the License, or (at your option)
  5. * any later version. See COPYING for more details.
  6. */
  7. #ifndef AVALON_H
  8. #define AVALON_H
  9. struct avalon_task {
  10. uint8_t reset :1;
  11. uint8_t flush_fifo :1;
  12. uint8_t fan_eft :1;
  13. uint8_t timer_eft :1;
  14. uint8_t chip_num :4;
  15. uint8_t fan_pwm_data;
  16. uint8_t timeout_data;
  17. uint8_t miner_num; // Word[0]
  18. uint8_t nonce_elf :1;
  19. uint32_t miner_ctrl :31;
  20. uint32_t pad0; //Word[2:1]
  21. uint32_t midstate[8];
  22. uint32_t data[3];
  23. // nonce_range: Word[??:14]
  24. } __attribute__((packed));
  25. struct avalon_result {
  26. uint32_t data[3];
  27. uint32_t midstate[8];
  28. uint32_t nonce;
  29. uint32_t reserved;
  30. } __attribute__((packed));
  31. #define AVALON_GET_WORK_COUNT 1 /* FIXME: should be ~20 */
  32. #define AVALON_MINER_THREADS 1
  33. // The serial I/O speed - Linux uses a define 'B115200' in bits/termios.h
  34. #define AVALON_IO_SPEED 115200
  35. // The size of a successful nonce read
  36. #define AVALON_READ_SIZE 4 /* FIXME: should be 13*4 , the result length */
  37. // Ensure the sizes are correct for the Serial read
  38. #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]
  39. ASSERT1(sizeof(uint32_t) == 4);
  40. #define AVALON_READ_TIME(baud) ((double)AVALON_READ_SIZE * (double)8.0 / (double)(baud))
  41. // Fraction of a second, USB timeout is measured in
  42. // i.e. 10 means 1/10 of a second
  43. #define TIME_FACTOR 10
  44. // It's 10 per second, thus value = 10/TIME_FACTOR =
  45. #define AVALON_RESET_FAULT_DECISECONDS 1
  46. // In timing mode: Default starting value until an estimate can be obtained
  47. // 5 seconds allows for up to a ~840MH/s device
  48. #define AVALON_READ_COUNT_TIMING (5 * TIME_FACTOR)
  49. // For a standard Avalon REV3 (to 5 places)
  50. // Since this rounds up a the last digit - it is a slight overestimate
  51. // Thus the hash rate will be a VERY slight underestimate
  52. // (by a lot less than the displayed accuracy)
  53. #define AVALON_REV3_HASH_TIME 0.0000000026316
  54. #define NANOSEC 1000000000.0
  55. // Avalon Rev3 doesn't send a completion message when it finishes
  56. // the full nonce range, so to avoid being idle we must abort the
  57. // work (by starting a new work) shortly before it finishes
  58. //
  59. // Thus we need to estimate 2 things:
  60. // 1) How many hashes were done if the work was aborted
  61. // 2) How high can the timeout be before the Avalon is idle,
  62. // to minimise the number of work started
  63. // We set 2) to 'the calculated estimate' - 1
  64. // to ensure the estimate ends before idle
  65. //
  66. // The simple calculation used is:
  67. // Tn = Total time in seconds to calculate n hashes
  68. // Hs = seconds per hash
  69. // Xn = number of hashes
  70. // W = code overhead per work
  71. //
  72. // Rough but reasonable estimate:
  73. // Tn = Hs * Xn + W (of the form y = mx + b)
  74. //
  75. // Thus:
  76. // Line of best fit (using least squares)
  77. //
  78. // Hs = (n*Sum(XiTi)-Sum(Xi)*Sum(Ti))/(n*Sum(Xi^2)-Sum(Xi)^2)
  79. // W = Sum(Ti)/n - (Hs*Sum(Xi))/n
  80. //
  81. // N.B. W is less when aborting work since we aren't waiting for the reply
  82. // to be transferred back (AVALON_READ_TIME)
  83. // Calculating the hashes aborted at n seconds is thus just n/Hs
  84. // (though this is still a slight overestimate due to code delays)
  85. //
  86. // Both below must be exceeded to complete a set of data
  87. // Minimum how long after the first, the last data point must be
  88. #define HISTORY_SEC 60
  89. // Minimum how many points a single AVALON_HISTORY should have
  90. #define MIN_DATA_COUNT 5
  91. // The value above used is doubled each history until it exceeds:
  92. #define MAX_MIN_DATA_COUNT 100
  93. // Store the last INFO_HISTORY data sets
  94. // [0] = current data, not yet ready to be included as an estimate
  95. // Each new data set throws the last old set off the end thus
  96. // keeping a ongoing average of recent data
  97. #define INFO_HISTORY 10
  98. struct AVALON_HISTORY {
  99. struct timeval finish;
  100. double sumXiTi;
  101. double sumXi;
  102. double sumTi;
  103. double sumXi2;
  104. uint32_t values;
  105. uint32_t hash_count_min;
  106. uint32_t hash_count_max;
  107. };
  108. enum timing_mode { MODE_DEFAULT, MODE_SHORT, MODE_LONG, MODE_VALUE };
  109. struct AVALON_INFO {
  110. // time to calculate the golden_ob
  111. uint64_t golden_hashes;
  112. struct timeval golden_tv;
  113. struct AVALON_HISTORY history[INFO_HISTORY+1];
  114. uint32_t min_data_count;
  115. // seconds per Hash
  116. double Hs;
  117. int read_count;
  118. enum timing_mode timing_mode;
  119. bool do_avalon_timing;
  120. double fullnonce;
  121. int count;
  122. double W;
  123. uint32_t values;
  124. uint64_t hash_count_range;
  125. // Determine the cost of history processing
  126. // (which will only affect W)
  127. uint64_t history_count;
  128. struct timeval history_time;
  129. // avalon-options
  130. int baud;
  131. int work_division;
  132. int asic_count;
  133. uint32_t nonce_mask;
  134. };
  135. #define END_CONDITION 0x0000ffff
  136. #define avalon_open2(devpath, baud, purge) serial_open(devpath, baud, AVALON_RESET_FAULT_DECISECONDS, purge)
  137. #define avalon_open(devpath, baud) avalon_open2(devpath, baud, false)
  138. #define AVA_GETS_ERROR -1
  139. #define AVA_GETS_OK 0
  140. #define AVA_GETS_RESTART 1
  141. #define AVA_GETS_TIMEOUT 2
  142. #endif /* AVALON_H */