driver-avalon4.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  1. /*
  2. * Copyright 2014 Mikeqin <Fengling.Qin@gmail.com>
  3. * Copyright 2013-2014 Con Kolivas <kernel@kolivas.org>
  4. * Copyright 2012-2014 Xiangfu <xiangfu@openmobilefree.com>
  5. * Copyright 2012 Luke Dashjr
  6. * Copyright 2012 Andrew Smith
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 3 of the License, or (at your option)
  11. * any later version. See COPYING for more details.
  12. */
  13. #include "config.h"
  14. #include "miner.h"
  15. #include "driver-avalon4.h"
  16. #include "crc.h"
  17. #include "sha2.h"
  18. #include "hexdump.c"
  19. #define get_fan_pwm(v) (AVA4_PWM_MAX - (v) * AVA4_PWM_MAX / 100)
  20. int opt_avalon4_temp_target = AVA4_DEFAULT_TEMP_TARGET;
  21. int opt_avalon4_overheat = AVA4_DEFAULT_TEMP_OVERHEAT;
  22. int opt_avalon4_fan_min = AVA4_DEFAULT_FAN_MIN;
  23. int opt_avalon4_fan_max = AVA4_DEFAULT_FAN_MAX;
  24. bool opt_avalon4_autov;
  25. int opt_avalon4_voltage_min = AVA4_DEFAULT_VOLTAGE;
  26. int opt_avalon4_voltage_max = AVA4_DEFAULT_VOLTAGE;
  27. int opt_avalon4_freq[3] = {AVA4_DEFAULT_FREQUENCY,
  28. AVA4_DEFAULT_FREQUENCY,
  29. AVA4_DEFAULT_FREQUENCY};
  30. int opt_avalon4_polling_delay = AVA4_DEFAULT_POLLING_DELAY;
  31. int opt_avalon4_aucspeed = AVA4_AUC_SPEED;
  32. int opt_avalon4_aucxdelay = AVA4_AUC_XDELAY;
  33. int opt_avalon4_ntime_offset = AVA4_DEFAULT_ASIC_COUNT;
  34. #define UNPACK32(x, str) \
  35. { \
  36. *((str) + 3) = (uint8_t) ((x) ); \
  37. *((str) + 2) = (uint8_t) ((x) >> 8); \
  38. *((str) + 1) = (uint8_t) ((x) >> 16); \
  39. *((str) + 0) = (uint8_t) ((x) >> 24); \
  40. }
  41. static inline void sha256_prehash(const unsigned char *message, unsigned int len, unsigned char *digest)
  42. {
  43. sha256_ctx ctx;
  44. int i;
  45. sha256_init(&ctx);
  46. sha256_update(&ctx, message, len);
  47. for (i = 0; i < 8; i++) {
  48. UNPACK32(ctx.h[i], &digest[i << 2]);
  49. }
  50. }
  51. static inline uint8_t rev8(uint8_t d)
  52. {
  53. int i;
  54. uint8_t out = 0;
  55. /* (from left to right) */
  56. for (i = 0; i < 8; i++)
  57. if (d & (1 << i))
  58. out |= (1 << (7 - i));
  59. return out;
  60. }
  61. char *set_avalon4_fan(char *arg)
  62. {
  63. int val1, val2, ret;
  64. ret = sscanf(arg, "%d-%d", &val1, &val2);
  65. if (ret < 1)
  66. return "No values passed to avalon4-fan";
  67. if (ret == 1)
  68. val2 = val1;
  69. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
  70. return "Invalid value passed to avalon4-fan";
  71. opt_avalon4_fan_min = val1;
  72. opt_avalon4_fan_max = val2;
  73. return NULL;
  74. }
  75. char *set_avalon4_freq(char *arg)
  76. {
  77. char *colon1, *colon2;
  78. int val1 = 0, val2 = 0, val3 = 0;
  79. if (!(*arg))
  80. return NULL;
  81. colon1 = strchr(arg, ':');
  82. if (colon1)
  83. *(colon1++) = '\0';
  84. if (*arg) {
  85. val1 = atoi(arg);
  86. if (val1 < AVA4_DEFAULT_FREQUENCY_MIN || val1 > AVA4_DEFAULT_FREQUENCY_MAX)
  87. return "Invalid value1 passed to avalon4-freq";
  88. }
  89. if (colon1 && *colon1) {
  90. colon2 = strchr(colon1, ':');
  91. if (colon2)
  92. *(colon2++) = '\0';
  93. if (*colon1) {
  94. val2 = atoi(colon1);
  95. if (val2 < AVA4_DEFAULT_FREQUENCY_MIN || val2 > AVA4_DEFAULT_FREQUENCY_MAX)
  96. return "Invalid value2 passed to avalon4-freq";
  97. }
  98. if (colon2 && *colon2) {
  99. val3 = atoi(colon2);
  100. if (val3 < AVA4_DEFAULT_FREQUENCY_MIN || val3 > AVA4_DEFAULT_FREQUENCY_MAX)
  101. return "Invalid value3 passed to avalon4-freq";
  102. }
  103. }
  104. if (!val1)
  105. val3 = val2 = val1 = AVA4_DEFAULT_FREQUENCY;
  106. if (!val2)
  107. val3 = val2 = val1;
  108. if (!val3)
  109. val3 = val2;
  110. opt_avalon4_freq[0] = val1;
  111. opt_avalon4_freq[1] = val2;
  112. opt_avalon4_freq[2] = val3;
  113. return NULL;
  114. }
  115. char *set_avalon4_voltage(char *arg)
  116. {
  117. int val1, val2, ret;
  118. ret = sscanf(arg, "%d-%d", &val1, &val2);
  119. if (ret < 1)
  120. return "No values passed to avalon4-voltage";
  121. if (ret == 1)
  122. val2 = val1;
  123. if (val1 < AVA4_DEFAULT_VOLTAGE_MIN || val1 > AVA4_DEFAULT_VOLTAGE_MAX ||
  124. val2 < AVA4_DEFAULT_VOLTAGE_MIN || val2 > AVA4_DEFAULT_VOLTAGE_MAX ||
  125. val2 < val1)
  126. return "Invalid value passed to avalon4-voltage";
  127. opt_avalon4_voltage_min = val1;
  128. opt_avalon4_voltage_max = val2;
  129. return NULL;
  130. }
  131. static int avalon4_init_pkg(struct avalon4_pkg *pkg, uint8_t type, uint8_t idx, uint8_t cnt)
  132. {
  133. unsigned short crc;
  134. pkg->head[0] = AVA4_H1;
  135. pkg->head[1] = AVA4_H2;
  136. pkg->type = type;
  137. pkg->opt = 0;
  138. pkg->idx = idx;
  139. pkg->cnt = cnt;
  140. crc = crc16(pkg->data, AVA4_P_DATA_LEN);
  141. pkg->crc[0] = (crc & 0xff00) >> 8;
  142. pkg->crc[1] = crc & 0x00ff;
  143. return 0;
  144. }
  145. static int job_idcmp(uint8_t *job_id, char *pool_job_id)
  146. {
  147. int job_id_len;
  148. unsigned short crc, crc_expect;
  149. if (!pool_job_id)
  150. return 1;
  151. job_id_len = strlen(pool_job_id);
  152. crc_expect = crc16((unsigned char *)pool_job_id, job_id_len);
  153. crc = job_id[0] << 8 | job_id[1];
  154. if (crc_expect == crc)
  155. return 0;
  156. applog(LOG_DEBUG, "Avalon4: job_id doesn't match! [%04x:%04x (%s)]",
  157. crc, crc_expect, pool_job_id);
  158. return 1;
  159. }
  160. static inline int get_current_temp_max(struct avalon4_info *info)
  161. {
  162. int i;
  163. int t = info->temp[0];
  164. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  165. if (info->temp[i] > t)
  166. t = info->temp[i];
  167. }
  168. return t;
  169. }
  170. /* http://www.onsemi.com/pub_link/Collateral/ADP3208D.PDF */
  171. static uint32_t encode_voltage_adp3208d(uint32_t v)
  172. {
  173. return rev8((0x78 - v / 125) << 1 | 1) << 8;
  174. }
  175. static uint32_t decode_voltage_adp3208d(uint32_t v)
  176. {
  177. return (0x78 - (rev8(v >> 8) >> 1)) * 125;
  178. }
  179. /* http://www.onsemi.com/pub/Collateral/NCP5392P-D.PDF */
  180. static uint32_t encode_voltage_ncp5392p(uint32_t v)
  181. {
  182. if (v == 0)
  183. return 0xff00;
  184. return rev8(((0x59 - (v - 5000) / 125) & 0xff) << 1 | 1) << 8;
  185. }
  186. static uint32_t decode_voltage_ncp5392p(uint32_t v)
  187. {
  188. if (v == 0xff00)
  189. return 0;
  190. return (0x59 - (rev8(v >> 8) >> 1)) * 125 + 5000;
  191. }
  192. static inline uint32_t adjust_fan(struct avalon4_info *info, int id)
  193. {
  194. uint32_t pwm;
  195. int t = info->temp[id];
  196. if (t < opt_avalon4_temp_target - 10)
  197. info->fan_pct[id] = opt_avalon4_fan_min;
  198. else if (t > opt_avalon4_temp_target + 10 || t > opt_avalon4_overheat - 3)
  199. info->fan_pct[id] = opt_avalon4_fan_max;
  200. else if (t > opt_avalon4_temp_target + 1)
  201. info->fan_pct[id] += 2;
  202. else if (t < opt_avalon4_temp_target - 1)
  203. info->fan_pct[id] -= 2;
  204. if (info->fan_pct[id] < opt_avalon4_fan_min)
  205. info->fan_pct[id] = opt_avalon4_fan_min;
  206. if (info->fan_pct[id] > opt_avalon4_fan_max)
  207. info->fan_pct[id] = opt_avalon4_fan_max;
  208. pwm = get_fan_pwm(info->fan_pct[id]);
  209. applog(LOG_DEBUG, "[%d], Adjust_fan: %dC-%d%%(%03x)", id, t, info->fan_pct[id], pwm);
  210. return pwm;
  211. }
  212. static int decode_pkg(struct thr_info *thr, struct avalon4_ret *ar, int modular_id)
  213. {
  214. struct cgpu_info *avalon4 = thr->cgpu;
  215. struct avalon4_info *info = avalon4->device_data;
  216. struct pool *pool, *real_pool;
  217. struct pool *pool_stratum0 = &info->pool0;
  218. struct pool *pool_stratum1 = &info->pool1;
  219. struct pool *pool_stratum2 = &info->pool2;
  220. unsigned int expected_crc;
  221. unsigned int actual_crc;
  222. uint32_t nonce, nonce2, ntime, miner, chip_id, volt, tmp;
  223. uint8_t job_id[4];
  224. int pool_no;
  225. if (ar->head[0] != AVA4_H1 && ar->head[1] != AVA4_H2) {
  226. applog(LOG_DEBUG, "Avalon4: H1 %02x, H2 %02x", ar->head[0], ar->head[1]);
  227. hexdump(ar->data, 32);
  228. return 1;
  229. }
  230. expected_crc = crc16(ar->data, AVA4_P_DATA_LEN);
  231. actual_crc = (ar->crc[0] & 0xff) | ((ar->crc[1] & 0xff) << 8);
  232. if (expected_crc != actual_crc) {
  233. applog(LOG_DEBUG, "Avalon4: %02x: expected crc(%04x), actual_crc(%04x)",
  234. ar->type, expected_crc, actual_crc);
  235. return 1;
  236. }
  237. switch(ar->type) {
  238. case AVA4_P_NONCE:
  239. applog(LOG_DEBUG, "Avalon4: AVA4_P_NONCE");
  240. memcpy(&miner, ar->data + 0, 4);
  241. memcpy(&pool_no, ar->data + 4, 4);
  242. memcpy(&nonce2, ar->data + 8, 4);
  243. memcpy(&ntime, ar->data + 12, 4);
  244. memcpy(&nonce, ar->data + 16, 4);
  245. memcpy(job_id, ar->data + 20, 4);
  246. miner = be32toh(miner);
  247. chip_id = (miner >> 16) & 0xffff;
  248. miner &= 0xffff;
  249. pool_no = be32toh(pool_no);
  250. ntime = be32toh(ntime);
  251. if (miner >= AVA4_DEFAULT_MINERS ||
  252. pool_no >= total_pools || pool_no < 0) {
  253. applog(LOG_DEBUG, "Avalon4: Wrong miner/pool_no %d/%d", miner, pool_no);
  254. break;
  255. } else {
  256. info->matching_work[modular_id][miner]++;
  257. info->chipmatching_work[modular_id][miner][chip_id]++;
  258. }
  259. nonce2 = be32toh(nonce2);
  260. nonce = be32toh(nonce);
  261. nonce -= 0x4000;
  262. applog(LOG_DEBUG, "%s-%d-%d: Found! P:%d - N2:%08x N:%08x NR:%d [M:%d - MW: %d(%d,%d,%d,%d)]",
  263. avalon4->drv->name, avalon4->device_id, modular_id,
  264. pool_no, nonce2, nonce, ntime,
  265. miner, info->matching_work[modular_id][miner],
  266. info->chipmatching_work[modular_id][miner][0],
  267. info->chipmatching_work[modular_id][miner][1],
  268. info->chipmatching_work[modular_id][miner][2],
  269. info->chipmatching_work[modular_id][miner][3]);
  270. real_pool = pool = pools[pool_no];
  271. if (job_idcmp(job_id, pool->swork.job_id)) {
  272. if (!job_idcmp(job_id, pool_stratum0->swork.job_id)) {
  273. applog(LOG_DEBUG, "Avalon4: Match to previous stratum0! (%s)", pool_stratum0->swork.job_id);
  274. pool = pool_stratum0;
  275. } else if (!job_idcmp(job_id, pool_stratum1->swork.job_id)) {
  276. applog(LOG_DEBUG, "Avalon4: Match to previous stratum1! (%s)", pool_stratum1->swork.job_id);
  277. pool = pool_stratum1;
  278. } else if (!job_idcmp(job_id, pool_stratum2->swork.job_id)) {
  279. applog(LOG_DEBUG, "Avalon4: Match to previous stratum2! (%s)", pool_stratum2->swork.job_id);
  280. pool = pool_stratum2;
  281. } else {
  282. applog(LOG_ERR, "Avalon4: Cannot match to any stratum! (%s)", pool->swork.job_id);
  283. inc_hw_errors(thr);
  284. break;
  285. }
  286. }
  287. submit_nonce2_nonce(thr, pool, real_pool, nonce2, nonce, ntime);
  288. break;
  289. case AVA4_P_STATUS:
  290. applog(LOG_DEBUG, "Avalon4: AVA4_P_STATUS");
  291. hexdump(ar->data, 32);
  292. memcpy(&tmp, ar->data, 4);
  293. tmp = be32toh(tmp);
  294. info->temp[modular_id] = tmp;
  295. memcpy(&tmp, ar->data + 4, 4);
  296. tmp = be32toh(tmp);
  297. info->fan[modular_id] = tmp;
  298. memcpy(&(info->get_frequency[modular_id]), ar->data + 8, 4);
  299. memcpy(&(info->get_voltage[modular_id]), ar->data + 12, 4);
  300. memcpy(&(info->local_work[modular_id]), ar->data + 16, 4);
  301. memcpy(&(info->hw_work[modular_id]), ar->data + 20, 4);
  302. memcpy(&(info->power_good[modular_id]), ar->data + 24, 4);
  303. info->get_frequency[modular_id] = be32toh(info->get_frequency[modular_id]) * 3968 / 65;
  304. info->get_voltage[modular_id] = be32toh(info->get_voltage[modular_id]);
  305. info->local_work[modular_id] = be32toh(info->local_work[modular_id]);
  306. info->hw_work[modular_id] = be32toh(info->hw_work[modular_id]);
  307. info->power_good[modular_id] = be32toh(info->power_good[modular_id]);
  308. volt = info->get_voltage[modular_id];
  309. if (info->mod_type[modular_id] == AVA4_TYPE_MM40)
  310. tmp = decode_voltage_adp3208d(volt);
  311. if (info->mod_type[modular_id] == AVA4_TYPE_MM41)
  312. tmp = decode_voltage_ncp5392p(volt);
  313. info->get_voltage[modular_id] = tmp;
  314. info->local_works[modular_id] += info->local_work[modular_id];
  315. info->hw_works[modular_id] += info->hw_work[modular_id];
  316. info->lw5[modular_id][info->i_1m] += info->local_work[modular_id];
  317. info->hw5[modular_id][info->i_1m] += info->hw_work[modular_id];
  318. avalon4->temp = get_current_temp_max(info);
  319. break;
  320. case AVA4_P_ACKDETECT:
  321. applog(LOG_DEBUG, "Avalon4: AVA4_P_ACKDETECT");
  322. break;
  323. default:
  324. applog(LOG_DEBUG, "Avalon4: Unknown response");
  325. break;
  326. }
  327. return 0;
  328. }
  329. /*
  330. # IIC packet format: length[1]+transId[1]+sesId[1]+req[1]+data[60]
  331. # length: 4+len(data)
  332. # transId: 0
  333. # sesId: 0
  334. # req: checkout the header file
  335. # data:
  336. # INIT: clock_rate[4] + reserved[4] + payload[52]
  337. # XFER: txSz[1]+rxSz[1]+options[1]+slaveAddr[1] + payload[56]
  338. */
  339. static int avalon4_iic_init_pkg(uint8_t *iic_pkg, struct avalon4_iic_info *iic_info, uint8_t *buf, int wlen, int rlen)
  340. {
  341. memset(iic_pkg, 0, AVA4_AUC_P_SIZE);
  342. switch (iic_info->iic_op) {
  343. case AVA4_IIC_INIT:
  344. iic_pkg[0] = 12; /* 4 bytes IIC header + 4 bytes speed + 4 bytes xfer delay */
  345. iic_pkg[3] = AVA4_IIC_INIT;
  346. iic_pkg[4] = iic_info->iic_param.aucParam[0] & 0xff;
  347. iic_pkg[5] = (iic_info->iic_param.aucParam[0] >> 8) & 0xff;
  348. iic_pkg[6] = (iic_info->iic_param.aucParam[0] >> 16) & 0xff;
  349. iic_pkg[7] = iic_info->iic_param.aucParam[0] >> 24;
  350. iic_pkg[8] = iic_info->iic_param.aucParam[1] & 0xff;
  351. iic_pkg[9] = (iic_info->iic_param.aucParam[1] >> 8) & 0xff;
  352. iic_pkg[10] = (iic_info->iic_param.aucParam[1] >> 16) & 0xff;
  353. iic_pkg[11] = iic_info->iic_param.aucParam[1] >> 24;
  354. break;
  355. case AVA4_IIC_XFER:
  356. iic_pkg[0] = 8 + wlen;
  357. iic_pkg[3] = AVA4_IIC_XFER;
  358. iic_pkg[4] = wlen;
  359. iic_pkg[5] = rlen;
  360. iic_pkg[7] = iic_info->iic_param.slave_addr;
  361. if (buf && wlen)
  362. memcpy(iic_pkg + 8, buf, wlen);
  363. break;
  364. case AVA4_IIC_RESET:
  365. case AVA4_IIC_DEINIT:
  366. case AVA4_IIC_INFO:
  367. iic_pkg[0] = 4;
  368. iic_pkg[3] = iic_info->iic_op;
  369. break;
  370. default:
  371. break;
  372. }
  373. return 0;
  374. }
  375. static int avalon4_iic_xfer(struct cgpu_info *avalon4,
  376. uint8_t *wbuf, int wlen, int *write,
  377. uint8_t *rbuf, int rlen, int *read)
  378. {
  379. int err = -1;
  380. if (unlikely(avalon4->usbinfo.nodev))
  381. goto out;
  382. err = usb_write(avalon4, (char *)wbuf, wlen, write, C_AVA4_WRITE);
  383. if (err || *write != wlen) {
  384. applog(LOG_DEBUG, "Avalon4: AUC xfer %d, w(%d-%d)!", err, wlen, *write);
  385. usb_nodev(avalon4);
  386. goto out;
  387. }
  388. cgsleep_ms(opt_avalon4_aucxdelay / 4800 + 1);
  389. rlen += 4; /* Add 4 bytes IIC header */
  390. err = usb_read(avalon4, (char *)rbuf, rlen, read, C_AVA4_READ);
  391. if (err || *read != rlen) {
  392. applog(LOG_DEBUG, "Avalon4: AUC xfer %d, r(%d-%d)!", err, rlen - 4, *read);
  393. hexdump(rbuf, rlen);
  394. }
  395. *read = rbuf[0] - 4; /* Remove 4 bytes IIC header */
  396. out:
  397. return err;
  398. }
  399. static int avalon4_auc_init(struct cgpu_info *avalon4, char *ver)
  400. {
  401. struct avalon4_iic_info iic_info;
  402. int err, wlen, rlen;
  403. uint8_t wbuf[AVA4_AUC_P_SIZE];
  404. uint8_t rbuf[AVA4_AUC_P_SIZE];
  405. if (unlikely(avalon4->usbinfo.nodev))
  406. return 1;
  407. /* Try to clean the AUC buffer */
  408. err = usb_read(avalon4, (char *)rbuf, AVA4_AUC_P_SIZE, &rlen, C_AVA4_READ);
  409. applog(LOG_DEBUG, "Avalon4: AUC usb_read %d, %d!", err, rlen);
  410. hexdump(rbuf, AVA4_AUC_P_SIZE);
  411. /* Reset */
  412. iic_info.iic_op = AVA4_IIC_RESET;
  413. rlen = 0;
  414. avalon4_iic_init_pkg(wbuf, &iic_info, NULL, 0, rlen);
  415. memset(rbuf, 0, AVA4_AUC_P_SIZE);
  416. err = avalon4_iic_xfer(avalon4, wbuf, AVA4_AUC_P_SIZE, &wlen, rbuf, rlen, &rlen);
  417. if (err) {
  418. applog(LOG_ERR, "Avalon4: Failed to reset Avalon USB2IIC Converter");
  419. return 1;
  420. }
  421. /* Deinit */
  422. iic_info.iic_op = AVA4_IIC_DEINIT;
  423. rlen = 0;
  424. avalon4_iic_init_pkg(wbuf, &iic_info, NULL, 0, rlen);
  425. memset(rbuf, 0, AVA4_AUC_P_SIZE);
  426. err = avalon4_iic_xfer(avalon4, wbuf, AVA4_AUC_P_SIZE, &wlen, rbuf, rlen, &rlen);
  427. if (err) {
  428. applog(LOG_ERR, "Avalon4: Failed to deinit Avalon USB2IIC Converter");
  429. return 1;
  430. }
  431. /* Init */
  432. iic_info.iic_op = AVA4_IIC_INIT;
  433. iic_info.iic_param.aucParam[0] = opt_avalon4_aucspeed;
  434. iic_info.iic_param.aucParam[1] = opt_avalon4_aucxdelay;
  435. rlen = AVA4_AUC_VER_LEN;
  436. avalon4_iic_init_pkg(wbuf, &iic_info, NULL, 0, rlen);
  437. memset(rbuf, 0, AVA4_AUC_P_SIZE);
  438. err = avalon4_iic_xfer(avalon4, wbuf, AVA4_AUC_P_SIZE, &wlen, rbuf, rlen, &rlen);
  439. if (err) {
  440. applog(LOG_ERR, "Avalon4: Failed to init Avalon USB2IIC Converter");
  441. return 1;
  442. }
  443. hexdump(rbuf, AVA4_AUC_P_SIZE);
  444. memcpy(ver, rbuf + 4, AVA4_AUC_VER_LEN);
  445. ver[AVA4_AUC_VER_LEN] = '\0';
  446. applog(LOG_DEBUG, "Avalon4: USB2IIC Converter version: %s!", ver);
  447. return 0;
  448. }
  449. static int avalon4_auc_getinfo(struct cgpu_info *avalon4)
  450. {
  451. struct avalon4_iic_info iic_info;
  452. int err, wlen, rlen;
  453. uint8_t wbuf[AVA4_AUC_P_SIZE];
  454. uint8_t rbuf[AVA4_AUC_P_SIZE];
  455. uint8_t *pdata = rbuf + 4;
  456. uint16_t adc_val;
  457. struct avalon4_info *info = avalon4->device_data;
  458. iic_info.iic_op = AVA4_IIC_INFO;
  459. /* Device info: (9 bytes)
  460. * tempadc(2), reqRdIndex, reqWrIndex,
  461. * respRdIndex, respWrIndex, tx_flags, state
  462. * */
  463. rlen = 7;
  464. avalon4_iic_init_pkg(wbuf, &iic_info, NULL, 0, rlen);
  465. memset(rbuf, 0, AVA4_AUC_P_SIZE);
  466. err = avalon4_iic_xfer(avalon4, wbuf, AVA4_AUC_P_SIZE, &wlen, rbuf, rlen, &rlen);
  467. if (err) {
  468. applog(LOG_ERR, "Avalon4: AUC Failed to get info ");
  469. return 1;
  470. }
  471. applog(LOG_DEBUG, "Avalon4: AUC tempADC(%03d), reqcnt(%d), respcnt(%d), txflag(%d), state(%d)",
  472. pdata[1] << 8 | pdata[0],
  473. pdata[2],
  474. pdata[3],
  475. pdata[5] << 8 | pdata[4],
  476. pdata[6]);
  477. adc_val = pdata[1] << 8 | pdata[0];
  478. info->auc_temp = 3.3 * adc_val * 10000 / 1023;
  479. return 0;
  480. }
  481. static int avalon4_iic_xfer_pkg(struct cgpu_info *avalon4, uint8_t slave_addr,
  482. const struct avalon4_pkg *pkg, struct avalon4_ret *ret)
  483. {
  484. struct avalon4_iic_info iic_info;
  485. int err, wcnt, rcnt, rlen = 0;
  486. uint8_t wbuf[AVA4_AUC_P_SIZE];
  487. uint8_t rbuf[AVA4_AUC_P_SIZE];
  488. struct avalon4_info *info = avalon4->device_data;
  489. iic_info.iic_op = AVA4_IIC_XFER;
  490. iic_info.iic_param.slave_addr = slave_addr;
  491. if (ret)
  492. rlen = AVA4_READ_SIZE;
  493. avalon4_iic_init_pkg(wbuf, &iic_info, (uint8_t *)pkg, AVA4_WRITE_SIZE, rlen);
  494. err = avalon4_iic_xfer(avalon4, wbuf, wbuf[0], &wcnt, rbuf, rlen, &rcnt);
  495. if ((pkg->type != AVA4_P_DETECT) && err == -7 && !rcnt && rlen) {
  496. avalon4_iic_init_pkg(wbuf, &iic_info, NULL, 0, rlen);
  497. err = avalon4_iic_xfer(avalon4, wbuf, wbuf[0], &wcnt, rbuf, rlen, &rcnt);
  498. applog(LOG_DEBUG, "Avalon4: IIC read again!(err:%d)", err);
  499. }
  500. if (err || rcnt != rlen) {
  501. if (info->xfer_err_cnt++ == 100) {
  502. applog(LOG_DEBUG, "Avalon4: AUC xfer_err_cnt reach err = %d, rcnt = %d, rlen = %d", err, rcnt, rlen);
  503. cgsleep_ms(5 * 1000); /* Wait MM reset */
  504. avalon4_auc_init(avalon4, info->auc_version);
  505. }
  506. return AVA4_SEND_ERROR;
  507. }
  508. if (ret)
  509. memcpy((char *)ret, rbuf + 4, AVA4_READ_SIZE);
  510. info->xfer_err_cnt = 0;
  511. return AVA4_SEND_OK;
  512. }
  513. static int avalon4_send_bc_pkgs(struct cgpu_info *avalon4, const struct avalon4_pkg *pkg)
  514. {
  515. int ret;
  516. do {
  517. if (unlikely(avalon4->usbinfo.nodev))
  518. return -1;
  519. ret = avalon4_iic_xfer_pkg(avalon4, AVA4_MODULE_BROADCAST, pkg, NULL);
  520. } while (ret != AVA4_SEND_OK);
  521. return 0;
  522. }
  523. static void avalon4_stratum_pkgs(struct cgpu_info *avalon4, struct pool *pool)
  524. {
  525. const int merkle_offset = 36;
  526. struct avalon4_pkg pkg;
  527. int i, a, b, tmp;
  528. unsigned char target[32];
  529. int job_id_len, n2size;
  530. unsigned short crc;
  531. int coinbase_len_posthash, coinbase_len_prehash;
  532. uint8_t coinbase_prehash[32];
  533. /* Send out the first stratum message STATIC */
  534. applog(LOG_DEBUG, "Avalon4: Pool stratum message STATIC: %d, %d, %d, %d, %d",
  535. pool->coinbase_len,
  536. pool->nonce2_offset,
  537. pool->n2size,
  538. merkle_offset,
  539. pool->merkles);
  540. memset(pkg.data, 0, AVA4_P_DATA_LEN);
  541. tmp = be32toh(pool->coinbase_len);
  542. memcpy(pkg.data, &tmp, 4);
  543. tmp = be32toh(pool->nonce2_offset);
  544. memcpy(pkg.data + 4, &tmp, 4);
  545. n2size = pool->n2size >= 4 ? 4 : pool->n2size;
  546. tmp = be32toh(n2size);
  547. memcpy(pkg.data + 8, &tmp, 4);
  548. tmp = be32toh(merkle_offset);
  549. memcpy(pkg.data + 12, &tmp, 4);
  550. tmp = be32toh(pool->merkles);
  551. memcpy(pkg.data + 16, &tmp, 4);
  552. tmp = be32toh((int)pool->swork.diff);
  553. memcpy(pkg.data + 20, &tmp, 4);
  554. tmp = be32toh((int)pool->pool_no);
  555. memcpy(pkg.data + 24, &tmp, 4);
  556. avalon4_init_pkg(&pkg, AVA4_P_STATIC, 1, 1);
  557. if (avalon4_send_bc_pkgs(avalon4, &pkg))
  558. return;
  559. set_target(target, pool->sdiff);
  560. memcpy(pkg.data, target, 32);
  561. if (opt_debug) {
  562. char *target_str;
  563. target_str = bin2hex(target, 32);
  564. applog(LOG_DEBUG, "Avalon4: Pool stratum target: %s", target_str);
  565. free(target_str);
  566. }
  567. avalon4_init_pkg(&pkg, AVA4_P_TARGET, 1, 1);
  568. if (avalon4_send_bc_pkgs(avalon4, &pkg))
  569. return;
  570. memset(pkg.data, 0, AVA4_P_DATA_LEN);
  571. job_id_len = strlen(pool->swork.job_id);
  572. crc = crc16((unsigned char *)pool->swork.job_id, job_id_len);
  573. applog(LOG_DEBUG, "Avalon4: Pool stratum message JOBS_ID[%04x]: %s",
  574. crc, pool->swork.job_id);
  575. pkg.data[0] = (crc & 0xff00) >> 8;
  576. pkg.data[1] = crc & 0x00ff;
  577. avalon4_init_pkg(&pkg, AVA4_P_JOB_ID, 1, 1);
  578. if (avalon4_send_bc_pkgs(avalon4, &pkg))
  579. return;
  580. coinbase_len_prehash = pool->nonce2_offset - (pool->nonce2_offset % SHA256_BLOCK_SIZE);
  581. coinbase_len_posthash = pool->coinbase_len - coinbase_len_prehash;
  582. sha256_prehash(pool->coinbase, coinbase_len_prehash, coinbase_prehash);
  583. a = (coinbase_len_posthash / AVA4_P_DATA_LEN) + 1;
  584. b = coinbase_len_posthash % AVA4_P_DATA_LEN;
  585. memcpy(pkg.data, coinbase_prehash, 32);
  586. avalon4_init_pkg(&pkg, AVA4_P_COINBASE, 1, a + (b ? 1 : 0));
  587. if (avalon4_send_bc_pkgs(avalon4, &pkg))
  588. return;
  589. applog(LOG_DEBUG, "Avalon4: Pool stratum message modified COINBASE: %d %d", a, b);
  590. for (i = 1; i < a; i++) {
  591. memcpy(pkg.data, pool->coinbase + coinbase_len_prehash + i * 32 - 32, 32);
  592. avalon4_init_pkg(&pkg, AVA4_P_COINBASE, i + 1, a + (b ? 1 : 0));
  593. if (avalon4_send_bc_pkgs(avalon4, &pkg))
  594. return;
  595. }
  596. if (b) {
  597. memset(pkg.data, 0, AVA4_P_DATA_LEN);
  598. memcpy(pkg.data, pool->coinbase + coinbase_len_prehash + i * 32 - 32, b);
  599. avalon4_init_pkg(&pkg, AVA4_P_COINBASE, i + 1, i + 1);
  600. if (avalon4_send_bc_pkgs(avalon4, &pkg))
  601. return;
  602. }
  603. b = pool->merkles;
  604. applog(LOG_DEBUG, "Avalon4: Pool stratum message MERKLES: %d", b);
  605. for (i = 0; i < b; i++) {
  606. memset(pkg.data, 0, AVA4_P_DATA_LEN);
  607. memcpy(pkg.data, pool->swork.merkle_bin[i], 32);
  608. avalon4_init_pkg(&pkg, AVA4_P_MERKLES, i + 1, b);
  609. if (avalon4_send_bc_pkgs(avalon4, &pkg))
  610. return;
  611. }
  612. applog(LOG_DEBUG, "Avalon4: Pool stratum message HEADER: 4");
  613. for (i = 0; i < 4; i++) {
  614. memset(pkg.data, 0, AVA4_P_DATA_LEN);
  615. memcpy(pkg.data, pool->header_bin + i * 32, 32);
  616. avalon4_init_pkg(&pkg, AVA4_P_HEADER, i + 1, 4);
  617. if (avalon4_send_bc_pkgs(avalon4, &pkg))
  618. return;
  619. }
  620. avalon4_auc_getinfo(avalon4);
  621. }
  622. static struct cgpu_info *avalon4_auc_detect(struct libusb_device *dev, struct usb_find_devices *found)
  623. {
  624. int i;
  625. struct avalon4_info *info;
  626. struct cgpu_info *avalon4 = usb_alloc_cgpu(&avalon4_drv, 1);
  627. char auc_ver[AVA4_AUC_VER_LEN];
  628. if (!usb_init(avalon4, dev, found)) {
  629. applog(LOG_ERR, "Avalon4 failed usb_init");
  630. avalon4 = usb_free_cgpu(avalon4);
  631. return NULL;
  632. }
  633. /* Avalon4 prefers not to use zero length packets */
  634. avalon4->nozlp = true;
  635. /* We try twice on AUC init */
  636. if (avalon4_auc_init(avalon4, auc_ver) && avalon4_auc_init(avalon4, auc_ver))
  637. return NULL;
  638. /* We have an Avalon4 AUC connected */
  639. avalon4->threads = 1;
  640. add_cgpu(avalon4);
  641. update_usb_stats(avalon4);
  642. applog(LOG_INFO, "%s-%d: Found at %s", avalon4->drv->name, avalon4->device_id,
  643. avalon4->device_path);
  644. avalon4->device_data = calloc(sizeof(struct avalon4_info), 1);
  645. if (unlikely(!(avalon4->device_data)))
  646. quit(1, "Failed to calloc avalon4_info");
  647. info = avalon4->device_data;
  648. memcpy(info->auc_version, auc_ver, AVA4_AUC_VER_LEN);
  649. info->auc_version[AVA4_AUC_VER_LEN] = '\0';
  650. info->auc_speed = opt_avalon4_aucspeed;
  651. info->auc_xdelay = opt_avalon4_aucxdelay;
  652. info->polling_first = 1;
  653. info->set_voltage_broadcat = 1;
  654. for (i = 0; i < AVA4_DEFAULT_MODULARS; i++) {
  655. info->enable[i] = 0;
  656. info->mod_type[i] = AVA4_TYPE_NULL;
  657. info->fan_pct[i] = AVA4_DEFAULT_FAN_START;
  658. info->set_voltage[i] = opt_avalon4_voltage_min;
  659. }
  660. info->enable[0] = 1;
  661. info->mod_type[0] = AVA4_TYPE_MM40;
  662. info->set_frequency[0] = opt_avalon4_freq[0];
  663. info->set_frequency[1] = opt_avalon4_freq[1];
  664. info->set_frequency[2] = opt_avalon4_freq[2];
  665. return avalon4;
  666. }
  667. static inline void avalon4_detect(bool __maybe_unused hotplug)
  668. {
  669. usb_detect(&avalon4_drv, avalon4_auc_detect);
  670. }
  671. static bool avalon4_prepare(struct thr_info *thr)
  672. {
  673. int i;
  674. struct cgpu_info *avalon4 = thr->cgpu;
  675. struct avalon4_info *info = avalon4->device_data;
  676. info->polling_first = 1;
  677. cgtime(&(info->last_fan));
  678. cgtime(&(info->last_5m));
  679. cgtime(&(info->last_1m));
  680. cglock_init(&info->update_lock);
  681. cglock_init(&info->pool0.data_lock);
  682. cglock_init(&info->pool1.data_lock);
  683. cglock_init(&info->pool2.data_lock);
  684. info->set_voltage_broadcat = 1;
  685. for (i = 0; i < AVA4_DEFAULT_MODULARS; i++)
  686. info->fan_pct[i] = AVA4_DEFAULT_FAN_START;
  687. return true;
  688. }
  689. static void detect_modules(struct cgpu_info *avalon4)
  690. {
  691. struct avalon4_info *info = avalon4->device_data;
  692. struct thr_info *thr = avalon4->thr[0];
  693. struct avalon4_pkg detect_pkg;
  694. struct avalon4_ret ret_pkg;
  695. uint32_t tmp;
  696. int i, err;
  697. /* Detect new modules here */
  698. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  699. if (info->enable[i])
  700. continue;
  701. /* Send out detect pkg */
  702. applog(LOG_DEBUG, "%s %d: AVA4_P_DETECT ID[%d]",
  703. avalon4->drv->name, avalon4->device_id, i);
  704. memset(detect_pkg.data, 0, AVA4_P_DATA_LEN);
  705. tmp = be32toh(i); /* ID */
  706. memcpy(detect_pkg.data + 28, &tmp, 4);
  707. avalon4_init_pkg(&detect_pkg, AVA4_P_DETECT, 1, 1);
  708. err = avalon4_iic_xfer_pkg(avalon4, AVA4_MODULE_BROADCAST, &detect_pkg, &ret_pkg);
  709. if (err == AVA4_SEND_OK) {
  710. if (decode_pkg(thr, &ret_pkg, AVA4_MODULE_BROADCAST)) {
  711. applog(LOG_DEBUG, "%s %d: Should be AVA4_P_ACKDETECT(%d), but %d",
  712. avalon4->drv->name, avalon4->device_id, AVA4_P_ACKDETECT, ret_pkg.type);
  713. continue;
  714. }
  715. }
  716. if (err != AVA4_SEND_OK) {
  717. applog(LOG_DEBUG, "%s %d: AVA4_P_DETECT: Failed AUC xfer data with err %d",
  718. avalon4->drv->name, avalon4->device_id, err);
  719. break;
  720. }
  721. applog(LOG_DEBUG, "%s %d: Module detect ID[%d]: %d",
  722. avalon4->drv->name, avalon4->device_id, i, ret_pkg.type);
  723. if (ret_pkg.type != AVA4_P_ACKDETECT)
  724. break;
  725. cgtime(&info->elapsed[i]);
  726. info->enable[i] = 1;
  727. memcpy(info->mm_dna[i], ret_pkg.data, AVA4_MM_DNA_LEN);
  728. info->mm_dna[i][AVA4_MM_DNA_LEN] = '\0';
  729. memcpy(info->mm_version[i], ret_pkg.data + AVA4_MM_DNA_LEN, AVA4_MM_VER_LEN);
  730. info->mm_version[i][AVA4_MM_VER_LEN] = '\0';
  731. if (!strncmp((char *)&(info->mm_version[i]), AVA4_MM40_PREFIXSTR, 2))
  732. info->mod_type[i] = AVA4_TYPE_MM40;
  733. if (!strncmp((char *)&(info->mm_version[i]), AVA4_MM41_PREFIXSTR, 2))
  734. info->mod_type[i] = AVA4_TYPE_MM41;
  735. info->fan_pct[i] = AVA4_DEFAULT_FAN_START;
  736. info->set_voltage[i] = opt_avalon4_voltage_min;
  737. info->led_red[i] = 0;
  738. applog(LOG_NOTICE, "%s %d: New module detect! ID[%d]",
  739. avalon4->drv->name, avalon4->device_id, i);
  740. }
  741. }
  742. static int polling(struct thr_info *thr, struct cgpu_info *avalon4, struct avalon4_info *info)
  743. {
  744. struct avalon4_pkg send_pkg;
  745. struct avalon4_ret ar;
  746. int i, j, tmp, ret, decode_err = 0, do_polling = 0;
  747. struct timeval current_fan;
  748. int do_adjust_fan = 0;
  749. uint32_t fan_pwm;
  750. double device_tdiff;
  751. if (info->polling_first) {
  752. cgsleep_ms(300);
  753. info->polling_first = 0;
  754. }
  755. cgtime(&current_fan);
  756. device_tdiff = tdiff(&current_fan, &(info->last_fan));
  757. if (device_tdiff > 5.0 || device_tdiff < 0) {
  758. cgtime(&info->last_fan);
  759. do_adjust_fan = 1;
  760. }
  761. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  762. if (!info->enable[i])
  763. continue;
  764. do_polling = 1;
  765. cgsleep_ms(opt_avalon4_polling_delay);
  766. memset(send_pkg.data, 0, AVA4_P_DATA_LEN);
  767. /* Red LED */
  768. tmp = be32toh(info->led_red[i]);
  769. memcpy(send_pkg.data, &tmp, 4);
  770. /* Adjust fan every 10 seconds*/
  771. if (do_adjust_fan) {
  772. fan_pwm = adjust_fan(info, i);
  773. fan_pwm |= 0x80000000;
  774. tmp = be32toh(fan_pwm);
  775. memcpy(send_pkg.data + 4, &tmp, 4);
  776. }
  777. avalon4_init_pkg(&send_pkg, AVA4_P_POLLING, 1, 1);
  778. ret = avalon4_iic_xfer_pkg(avalon4, i, &send_pkg, &ar);
  779. if (ret == AVA4_SEND_OK)
  780. decode_err = decode_pkg(thr, &ar, i);
  781. if (ret != AVA4_SEND_OK || decode_err) {
  782. info->polling_err_cnt[i]++;
  783. if (info->polling_err_cnt[i] >= 4) {
  784. info->polling_err_cnt[i] = 0;
  785. info->mod_type[i] = AVA4_TYPE_NULL;
  786. info->enable[i] = 0;
  787. info->get_voltage[i] = 0;
  788. info->get_frequency[i] = 0;
  789. info->power_good[i] = 0;
  790. info->local_work[i] = 0;
  791. info->local_works[i] = 0;
  792. info->hw_work[i] = 0;
  793. info->hw_works[i] = 0;
  794. for (j = 0; j < 6; j++) {
  795. info->lw5[i][j] = 0;
  796. info->hw5[i][j] = 0;
  797. }
  798. for (j = 0; j < AVA4_DEFAULT_MINERS; j++) {
  799. info->matching_work[i][j] = 0;
  800. info->chipmatching_work[i][j][0] = 0;
  801. info->chipmatching_work[i][j][1] = 0;
  802. info->chipmatching_work[i][j][2] = 0;
  803. info->chipmatching_work[i][j][3] = 0;
  804. }
  805. applog(LOG_NOTICE, "%s %d: Module detached! ID[%d]",
  806. avalon4->drv->name, avalon4->device_id, i);
  807. }
  808. }
  809. if (ret == AVA4_SEND_OK && !decode_err)
  810. info->polling_err_cnt[i] = 0;
  811. }
  812. if (!do_polling)
  813. detect_modules(avalon4);
  814. return 0;
  815. }
  816. static void copy_pool_stratum(struct pool *pool_stratum, struct pool *pool)
  817. {
  818. int i;
  819. int merkles = pool->merkles;
  820. size_t coinbase_len = pool->coinbase_len;
  821. if (!pool->swork.job_id)
  822. return;
  823. if (!job_idcmp((unsigned char *)pool->swork.job_id, pool_stratum->swork.job_id))
  824. return;
  825. cg_wlock(&pool_stratum->data_lock);
  826. free(pool_stratum->swork.job_id);
  827. free(pool_stratum->nonce1);
  828. free(pool_stratum->coinbase);
  829. align_len(&coinbase_len);
  830. pool_stratum->coinbase = calloc(coinbase_len, 1);
  831. if (unlikely(!pool_stratum->coinbase))
  832. quit(1, "Failed to calloc pool_stratum coinbase in avalon4");
  833. memcpy(pool_stratum->coinbase, pool->coinbase, coinbase_len);
  834. for (i = 0; i < pool_stratum->merkles; i++)
  835. free(pool_stratum->swork.merkle_bin[i]);
  836. if (merkles) {
  837. pool_stratum->swork.merkle_bin = realloc(pool_stratum->swork.merkle_bin,
  838. sizeof(char *) * merkles + 1);
  839. for (i = 0; i < merkles; i++) {
  840. pool_stratum->swork.merkle_bin[i] = malloc(32);
  841. if (unlikely(!pool_stratum->swork.merkle_bin[i]))
  842. quit(1, "Failed to malloc pool_stratum swork merkle_bin");
  843. memcpy(pool_stratum->swork.merkle_bin[i], pool->swork.merkle_bin[i], 32);
  844. }
  845. }
  846. pool_stratum->sdiff = pool->sdiff;
  847. pool_stratum->coinbase_len = pool->coinbase_len;
  848. pool_stratum->nonce2_offset = pool->nonce2_offset;
  849. pool_stratum->n2size = pool->n2size;
  850. pool_stratum->merkles = pool->merkles;
  851. pool_stratum->swork.job_id = strdup(pool->swork.job_id);
  852. pool_stratum->nonce1 = strdup(pool->nonce1);
  853. memcpy(pool_stratum->ntime, pool->ntime, sizeof(pool_stratum->ntime));
  854. memcpy(pool_stratum->header_bin, pool->header_bin, sizeof(pool_stratum->header_bin));
  855. cg_wunlock(&pool_stratum->data_lock);
  856. }
  857. static void avalon4_stratum_set(struct cgpu_info *avalon4, struct pool *pool, int addr, int cutoff)
  858. {
  859. struct avalon4_info *info = avalon4->device_data;
  860. struct avalon4_pkg send_pkg;
  861. uint32_t tmp = 0, range, start, volt;
  862. info->set_frequency[0] = opt_avalon4_freq[0];
  863. info->set_frequency[1] = opt_avalon4_freq[1];
  864. info->set_frequency[2] = opt_avalon4_freq[2];
  865. /* Set the NTime, Voltage and Frequency */
  866. memset(send_pkg.data, 0, AVA4_P_DATA_LEN);
  867. if (opt_avalon4_ntime_offset != AVA4_DEFAULT_ASIC_COUNT) {
  868. tmp = opt_avalon4_ntime_offset | 0x80000000;
  869. tmp = be32toh(tmp);
  870. memcpy(send_pkg.data, &tmp, 4);
  871. }
  872. volt = info->set_voltage[addr];
  873. if (cutoff)
  874. volt = 0;
  875. if (info->mod_type[addr] == AVA4_TYPE_MM40)
  876. tmp = encode_voltage_adp3208d(volt);
  877. if (info->mod_type[addr] == AVA4_TYPE_MM41)
  878. tmp = encode_voltage_ncp5392p(volt);
  879. tmp = be32toh(tmp);
  880. memcpy(send_pkg.data + 4, &tmp, 4);
  881. tmp = info->set_frequency[0] | (info->set_frequency[1] << 10) | (info->set_frequency[2] << 20);
  882. tmp = be32toh(tmp);
  883. memcpy(send_pkg.data + 8, &tmp, 4);
  884. /* Configure the nonce2 offset and range */
  885. if (pool->n2size == 3)
  886. range = 0xffffff / (total_devices + 1);
  887. else
  888. range = 0xffffffff / (total_devices + 1);
  889. start = range * (avalon4->device_id + 1);
  890. tmp = be32toh(start);
  891. memcpy(send_pkg.data + 12, &tmp, 4);
  892. tmp = be32toh(range);
  893. memcpy(send_pkg.data + 16, &tmp, 4);
  894. /* Package the data */
  895. avalon4_init_pkg(&send_pkg, AVA4_P_SET, 1, 1);
  896. if (addr == AVA4_MODULE_BROADCAST)
  897. avalon4_send_bc_pkgs(avalon4, &send_pkg);
  898. else
  899. avalon4_iic_xfer_pkg(avalon4, addr, &send_pkg, NULL);
  900. }
  901. static void avalon4_stratum_finish(struct cgpu_info *avalon4)
  902. {
  903. struct avalon4_pkg send_pkg;
  904. memset(send_pkg.data, 0, AVA4_P_DATA_LEN);
  905. avalon4_init_pkg(&send_pkg, AVA4_P_FINISH, 1, 1);
  906. avalon4_send_bc_pkgs(avalon4, &send_pkg);
  907. }
  908. static void avalon4_update(struct cgpu_info *avalon4)
  909. {
  910. struct avalon4_info *info = avalon4->device_data;
  911. struct thr_info *thr = avalon4->thr[0];
  912. struct work *work;
  913. struct pool *pool;
  914. int coinbase_len_posthash, coinbase_len_prehash;
  915. int i, count = 0;
  916. applog(LOG_DEBUG, "Avalon4: New stratum: restart: %d, update: %d",
  917. thr->work_restart, thr->work_update);
  918. thr->work_update = false;
  919. thr->work_restart = false;
  920. /* Step 1: Make sure pool is ready */
  921. work = get_work(thr, thr->id);
  922. discard_work(work); /* Don't leak memory */
  923. /* Step 2: MM protocol check */
  924. pool = current_pool();
  925. if (!pool->has_stratum)
  926. quit(1, "Avalon4: MM has to use stratum pools");
  927. coinbase_len_prehash = pool->nonce2_offset - (pool->nonce2_offset % SHA256_BLOCK_SIZE);
  928. coinbase_len_posthash = pool->coinbase_len - coinbase_len_prehash;
  929. if (coinbase_len_posthash + SHA256_BLOCK_SIZE > AVA4_P_COINBASE_SIZE) {
  930. applog(LOG_ERR, "Avalon4: MM pool modified coinbase length(%d) is more than %d",
  931. coinbase_len_posthash + SHA256_BLOCK_SIZE, AVA4_P_COINBASE_SIZE);
  932. return;
  933. }
  934. if (pool->merkles > AVA4_P_MERKLES_COUNT) {
  935. applog(LOG_ERR, "Avalon4: MM merkles has to be less then %d", AVA4_P_MERKLES_COUNT);
  936. return;
  937. }
  938. if (pool->n2size < 3) {
  939. applog(LOG_ERR, "Avalon4: MM nonce2 size has to be >= 3 (%d)", pool->n2size);
  940. return;
  941. }
  942. /* Step 3: Send out stratum pkgs */
  943. cg_wlock(&info->update_lock);
  944. cg_rlock(&pool->data_lock);
  945. cgtime(&info->last_stratum);
  946. info->pool_no = pool->pool_no;
  947. copy_pool_stratum(&info->pool2, &info->pool1);
  948. copy_pool_stratum(&info->pool1, &info->pool0);
  949. copy_pool_stratum(&info->pool0, pool);
  950. avalon4_stratum_pkgs(avalon4, pool);
  951. cg_runlock(&pool->data_lock);
  952. cg_wunlock(&info->update_lock);
  953. /* Step 4: Try to detect new modules */
  954. detect_modules(avalon4);
  955. /* Step 5: Configure the parameter from outside */
  956. avalon4_stratum_set(avalon4, pool, AVA4_MODULE_BROADCAST, 0);
  957. if (!info->set_voltage_broadcat) {
  958. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  959. if (!info->enable[i])
  960. continue;
  961. if (info->set_voltage[i] == info->set_voltage[0])
  962. continue;
  963. avalon4_stratum_set(avalon4, pool, i, 0);
  964. }
  965. } else {
  966. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  967. if (!info->enable[i])
  968. continue;
  969. if (info->mod_type[i] == AVA4_TYPE_MM40)
  970. continue;
  971. avalon4_stratum_set(avalon4, pool, i, 0);
  972. }
  973. }
  974. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  975. if (!info->enable[i])
  976. continue;
  977. count++;
  978. if (info->temp[i] < opt_avalon4_overheat)
  979. continue;
  980. avalon4_stratum_set(avalon4, pool, i, 1);
  981. }
  982. info->mm_count = count;
  983. /* Step 6: Send out finish pkg */
  984. avalon4_stratum_finish(avalon4);
  985. }
  986. static int64_t avalon4_scanhash(struct thr_info *thr)
  987. {
  988. struct cgpu_info *avalon4 = thr->cgpu;
  989. struct avalon4_info *info = avalon4->device_data;
  990. struct timeval current;
  991. double device_tdiff, hwp;
  992. uint32_t a = 0, b = 0;
  993. uint64_t h;
  994. int i, j;
  995. if (unlikely(avalon4->usbinfo.nodev)) {
  996. applog(LOG_ERR, "%s-%d: Device disappeared, shutting down thread",
  997. avalon4->drv->name, avalon4->device_id);
  998. return -1;
  999. }
  1000. /* Stop polling the device if there is no stratum in 3 minutes, network is down */
  1001. cgtime(&current);
  1002. if (tdiff(&current, &(info->last_stratum)) > 180.0)
  1003. return 0;
  1004. cg_rlock(&info->update_lock);
  1005. polling(thr, avalon4, info);
  1006. cg_runlock(&info->update_lock);
  1007. cgtime(&current);
  1008. device_tdiff = tdiff(&current, &(info->last_1m));
  1009. if (device_tdiff >= 60.0 || device_tdiff < 0) {
  1010. copy_time(&info->last_1m, &current);
  1011. if (info->i_1m++ >= 6)
  1012. info->i_1m = 0;
  1013. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  1014. if (!info->enable[i])
  1015. continue;
  1016. info->lw5[i][info->i_1m] = 0;
  1017. info->hw5[i][info->i_1m] = 0;
  1018. }
  1019. }
  1020. cgtime(&current);
  1021. device_tdiff = tdiff(&current, &(info->last_5m));
  1022. if (opt_avalon4_autov && (device_tdiff > 480.0 || device_tdiff < 0)) {
  1023. copy_time(&info->last_5m, &current);
  1024. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  1025. if (!info->enable[i])
  1026. continue;
  1027. a = 0;
  1028. b = 0;
  1029. for (j = 0; j < 6; j++) {
  1030. a += info->lw5[i][j];
  1031. b += info->hw5[i][j];
  1032. }
  1033. hwp = a ? (double)b / (double)a : 0;
  1034. if (hwp > AVA4_DH_INC && (info->set_voltage[i] < info->set_voltage[0] + 125)) {
  1035. info->set_voltage[i] += 125;
  1036. applog(LOG_NOTICE, "%s %d: Automatic increase module[%d] voltage to %d",
  1037. avalon4->drv->name, avalon4->device_id, i, info->set_voltage[i]);
  1038. }
  1039. if (hwp < AVA4_DH_DEC && (info->set_voltage[i] > info->set_voltage[0] - (4 * 125))) {
  1040. info->set_voltage[i] -= 125;
  1041. applog(LOG_NOTICE, "%s %d: Automatic decrease module[%d] voltage to %d",
  1042. avalon4->drv->name, avalon4->device_id, i, info->set_voltage[i]);
  1043. }
  1044. }
  1045. }
  1046. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  1047. if (info->set_voltage[i] != info->set_voltage[0])
  1048. break;
  1049. }
  1050. if (i < AVA4_DEFAULT_MODULARS)
  1051. info->set_voltage_broadcat = 0;
  1052. else
  1053. info->set_voltage_broadcat = 1;
  1054. h = 0;
  1055. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  1056. h += info->enable[i] ? (info->local_work[i] - info->hw_work[i]) : 0;
  1057. info->local_work[i] = 0;
  1058. info->hw_work[i] = 0;
  1059. }
  1060. return h * 0xffffffffull;
  1061. }
  1062. #define STATBUFLEN 512
  1063. static struct api_data *avalon4_api_stats(struct cgpu_info *cgpu)
  1064. {
  1065. struct api_data *root = NULL;
  1066. struct avalon4_info *info = cgpu->device_data;
  1067. int i, j;
  1068. uint32_t a,b ;
  1069. double hwp, diff;
  1070. char buf[256];
  1071. char statbuf[AVA4_DEFAULT_MODULARS][STATBUFLEN];
  1072. struct timeval current;
  1073. memset(statbuf, 0, AVA4_DEFAULT_MODULARS * STATBUFLEN);
  1074. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  1075. if(info->mod_type[i] == AVA4_TYPE_NULL)
  1076. continue;
  1077. sprintf(buf, "Ver[%s]", info->mm_version[i]);
  1078. strcat(statbuf[i], buf);
  1079. }
  1080. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  1081. if(info->mod_type[i] == AVA4_TYPE_NULL)
  1082. continue;
  1083. sprintf(buf, " DNA[%02x%02x%02x%02x%02x%02x%02x%02x]",
  1084. info->mm_dna[i][0],
  1085. info->mm_dna[i][1],
  1086. info->mm_dna[i][2],
  1087. info->mm_dna[i][3],
  1088. info->mm_dna[i][4],
  1089. info->mm_dna[i][5],
  1090. info->mm_dna[i][6],
  1091. info->mm_dna[i][7]);
  1092. strcat(statbuf[i], buf);
  1093. }
  1094. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  1095. struct timeval now;
  1096. if (info->mod_type[i] == AVA4_TYPE_NULL)
  1097. continue;
  1098. cgtime(&now);
  1099. sprintf(buf, " Elapsed[%.0f]", tdiff(&now, &(info->elapsed[i])));
  1100. strcat(statbuf[i], buf);
  1101. }
  1102. #if 0
  1103. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  1104. if (info->mod_type[i] == AVA4_TYPE_NULL)
  1105. continue;
  1106. strcat(statbuf[i], " MW[");
  1107. for (j = 0; j < AVA4_DEFAULT_MINERS; j++) {
  1108. sprintf(buf, "%d ", info->matching_work[i][j]);
  1109. strcat(statbuf[i], buf);
  1110. }
  1111. statbuf[i][strlen(statbuf[i]) - 1] = ']';
  1112. }
  1113. #endif
  1114. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  1115. if(info->mod_type[i] == AVA4_TYPE_NULL)
  1116. continue;
  1117. sprintf(buf, " LW[%"PRIu64"]", info->local_works[i]);
  1118. strcat(statbuf[i], buf);
  1119. }
  1120. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  1121. if(info->mod_type[i] == AVA4_TYPE_NULL)
  1122. continue;
  1123. sprintf(buf, " HW[%"PRIu64"]", info->hw_works[i]);
  1124. strcat(statbuf[i], buf);
  1125. }
  1126. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  1127. if(info->mod_type[i] == AVA4_TYPE_NULL)
  1128. continue;
  1129. a = info->hw_works[i];
  1130. b = info->local_works[i];
  1131. hwp = b ? ((double)a / (double)b) * 100: 0;
  1132. sprintf(buf, " DH[%.3f%%]", hwp);
  1133. strcat(statbuf[i], buf);
  1134. }
  1135. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  1136. if(info->mod_type[i] == AVA4_TYPE_NULL)
  1137. continue;
  1138. a = 0;
  1139. b = 0;
  1140. for (j = 0; j < 6; j++) {
  1141. a += info->lw5[i][j];
  1142. b += info->hw5[i][j];
  1143. }
  1144. cgtime(&current);
  1145. diff = tdiff(&current, &(info->last_1m)) + 300.0;
  1146. hwp = a ? (double)b / (double)a * 100 : 0;
  1147. sprintf(buf, " GHS5m[%.2f] DH5m[%.3f%%]", ((double)a - (double)b) * 4.295 / diff, hwp);
  1148. strcat(statbuf[i], buf);
  1149. }
  1150. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  1151. if(info->mod_type[i] == AVA4_TYPE_NULL)
  1152. continue;
  1153. sprintf(buf, " Temp[%d]", info->temp[i]);
  1154. strcat(statbuf[i], buf);
  1155. }
  1156. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  1157. if(info->mod_type[i] == AVA4_TYPE_NULL)
  1158. continue;
  1159. sprintf(buf, " Fan[%d]", info->fan[i]);
  1160. strcat(statbuf[i], buf);
  1161. }
  1162. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  1163. if(info->mod_type[i] == AVA4_TYPE_NULL)
  1164. continue;
  1165. sprintf(buf, " Vol[%.4f]", (float)info->get_voltage[i] / 10000);
  1166. strcat(statbuf[i], buf);
  1167. }
  1168. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  1169. if(info->mod_type[i] == AVA4_TYPE_NULL)
  1170. continue;
  1171. sprintf(buf, " Freq[%.2f]", (float)info->get_frequency[i] / 1000);
  1172. strcat(statbuf[i], buf);
  1173. }
  1174. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  1175. if(info->mod_type[i] == AVA4_TYPE_NULL)
  1176. continue;
  1177. sprintf(buf, " PG[%d]", info->power_good[i]);
  1178. strcat(statbuf[i], buf);
  1179. }
  1180. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  1181. if(info->mod_type[i] == AVA4_TYPE_NULL)
  1182. continue;
  1183. sprintf(buf, " Led[%d]", info->led_red[i]);
  1184. strcat(statbuf[i], buf);
  1185. }
  1186. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  1187. if(info->mod_type[i] == AVA4_TYPE_NULL)
  1188. continue;
  1189. sprintf(buf, "MM ID%d", i);
  1190. root = api_add_string(root, buf, statbuf[i], true);
  1191. }
  1192. root = api_add_int(root, "MM Count", &(info->mm_count), true);
  1193. root = api_add_bool(root, "Automatic Voltage", &opt_avalon4_autov, true);
  1194. root = api_add_string(root, "AUC VER", info->auc_version, false);
  1195. root = api_add_int(root, "AUC I2C Speed", &(info->auc_speed), true);
  1196. root = api_add_int(root, "AUC I2C XDelay", &(info->auc_xdelay), true);
  1197. root = api_add_int(root, "AUC ADC", &(info->auc_temp), true);
  1198. return root;
  1199. }
  1200. static char *avalon4_set_device(struct cgpu_info *avalon4, char *option, char *setting, char *replybuf)
  1201. {
  1202. int val, i;
  1203. struct avalon4_info *info = avalon4->device_data;
  1204. if (strcasecmp(option, "help") == 0) {
  1205. sprintf(replybuf, "led|fan|voltage|frequency|pdelay");
  1206. return replybuf;
  1207. }
  1208. if (strcasecmp(option, "pdelay") == 0) {
  1209. if (!setting || !*setting) {
  1210. sprintf(replybuf, "missing polling delay setting");
  1211. return replybuf;
  1212. }
  1213. val = atoi(setting);
  1214. if (val < 1 || val > 65535) {
  1215. sprintf(replybuf, "invalid polling delay: %d, valid range 1-65535", val);
  1216. return replybuf;
  1217. }
  1218. opt_avalon4_polling_delay = val;
  1219. applog(LOG_NOTICE, "%s %d: Update polling delay to: %d",
  1220. avalon4->drv->name, avalon4->device_id, val);
  1221. return NULL;
  1222. }
  1223. if (strcasecmp(option, "fan") == 0) {
  1224. if (!setting || !*setting) {
  1225. sprintf(replybuf, "missing fan value");
  1226. return replybuf;
  1227. }
  1228. if (set_avalon4_fan(setting)) {
  1229. sprintf(replybuf, "invalid fan value, valid range 0-100");
  1230. return replybuf;
  1231. }
  1232. applog(LOG_NOTICE, "%s %d: Update fan to %d-%d",
  1233. avalon4->drv->name, avalon4->device_id,
  1234. opt_avalon4_fan_min, opt_avalon4_fan_max);
  1235. return NULL;
  1236. }
  1237. if (strcasecmp(option, "frequency") == 0) {
  1238. if (!setting || !*setting) {
  1239. sprintf(replybuf, "missing frequency value");
  1240. return replybuf;
  1241. }
  1242. if (set_avalon4_freq(setting)) {
  1243. sprintf(replybuf, "invalid frequency value, valid range %d-%d",
  1244. AVA4_DEFAULT_FREQUENCY_MIN, AVA4_DEFAULT_FREQUENCY_MAX);
  1245. return replybuf;
  1246. }
  1247. applog(LOG_NOTICE, "%s %d: Update frequency to %d",
  1248. avalon4->drv->name, avalon4->device_id,
  1249. (opt_avalon4_freq[0] * 4 + opt_avalon4_freq[1] * 4 + opt_avalon4_freq[2]) / 9);
  1250. return NULL;
  1251. }
  1252. if (strcasecmp(option, "led") == 0) {
  1253. if (!setting || !*setting) {
  1254. sprintf(replybuf, "missing module_id setting");
  1255. return replybuf;
  1256. }
  1257. val = atoi(setting);
  1258. if (val < 1 || val >= AVA4_DEFAULT_MODULARS) {
  1259. sprintf(replybuf, "invalid module_id: %d, valid range 1-%d", val, AVA4_DEFAULT_MODULARS);
  1260. return replybuf;
  1261. }
  1262. if (!info->enable[val]) {
  1263. sprintf(replybuf, "the current module was disabled %d", val);
  1264. return replybuf;
  1265. }
  1266. info->led_red[val] = !info->led_red[val];
  1267. applog(LOG_NOTICE, "%s %d: Module:%d, LED: %s",
  1268. avalon4->drv->name, avalon4->device_id,
  1269. val, info->led_red[val] ? "on" : "off");
  1270. return NULL;
  1271. }
  1272. if (strcasecmp(option, "voltage") == 0) {
  1273. int val_mod, val_volt, ret;
  1274. if (!setting || !*setting) {
  1275. sprintf(replybuf, "missing voltage value");
  1276. return replybuf;
  1277. }
  1278. ret = sscanf(setting, "%d-%d", &val_mod, &val_volt);
  1279. if (ret != 2) {
  1280. sprintf(replybuf, "invalid voltage parameter, format: moduleid-voltage");
  1281. return replybuf;
  1282. }
  1283. if (val_mod < 0 || val_mod >= AVA4_DEFAULT_MODULARS ||
  1284. val_volt < AVA4_DEFAULT_VOLTAGE_MIN || val_volt > AVA4_DEFAULT_VOLTAGE_MAX) {
  1285. sprintf(replybuf, "invalid module_id or voltage value, valid module_id range %d-%d, valid voltage range %d-%d",
  1286. 0, AVA4_DEFAULT_MODULARS,
  1287. AVA4_DEFAULT_VOLTAGE_MIN, AVA4_DEFAULT_VOLTAGE_MAX);
  1288. return replybuf;
  1289. }
  1290. if (!info->enable[val_mod]) {
  1291. sprintf(replybuf, "the current module was disabled %d", val_mod);
  1292. return replybuf;
  1293. }
  1294. info->set_voltage[val_mod] = val_volt;
  1295. if (val_mod == AVA4_MODULE_BROADCAST) {
  1296. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++)
  1297. info->set_voltage[i] = val_volt;
  1298. info->set_voltage_broadcat = 1;
  1299. } else
  1300. info->set_voltage_broadcat = 0;
  1301. applog(LOG_NOTICE, "%s %d: Update module[%d] voltage to %d",
  1302. avalon4->drv->name, avalon4->device_id, val_mod, val_volt);
  1303. return NULL;
  1304. }
  1305. sprintf(replybuf, "Unknown option: %s", option);
  1306. return replybuf;
  1307. }
  1308. static void avalon4_statline_before(char *buf, size_t bufsiz, struct cgpu_info *avalon4)
  1309. {
  1310. struct avalon4_info *info = avalon4->device_data;
  1311. int temp = get_current_temp_max(info);
  1312. int voltsmin = AVA4_DEFAULT_VOLTAGE_MAX, voltsmax = AVA4_DEFAULT_VOLTAGE_MIN;
  1313. int fanmin = AVA4_DEFAULT_FAN_MAX, fanmax = AVA4_DEFAULT_FAN_MIN;
  1314. int i, frequency;
  1315. for (i = 1; i < AVA4_DEFAULT_MODULARS; i++) {
  1316. if (!info->enable[i])
  1317. continue;
  1318. if (fanmax <= info->fan_pct[i])
  1319. fanmax = info->fan_pct[i];
  1320. if (fanmin >= info->fan_pct[i])
  1321. fanmin = info->fan_pct[i];
  1322. if (voltsmax <= info->get_voltage[i])
  1323. voltsmax = info->get_voltage[i];
  1324. if (voltsmin >= info->get_voltage[i])
  1325. voltsmin = info->get_voltage[i];
  1326. }
  1327. #if 0
  1328. tailsprintf(buf, bufsiz, "%2dMMs %.4fV-%.4fV %4dMhz %2dC %3d%%-%3d%%",
  1329. info->mm_count, (float)voltsmin / 10000, (float)voltsmax / 10000,
  1330. (info->set_frequency[0] * 4 + info->set_frequency[1] * 4 + info->set_frequency[2]) / 9,
  1331. temp, fanmin, fanmax);
  1332. #endif
  1333. frequency = (info->set_frequency[0] * 4 + info->set_frequency[1] * 4 + info->set_frequency[2]) / 9;
  1334. tailsprintf(buf, bufsiz, "%4dMhz %2dC %3d%% %.3fV", frequency,
  1335. temp, fanmin, (float)voltsmax / 10000);
  1336. }
  1337. struct device_drv avalon4_drv = {
  1338. .drv_id = DRIVER_avalon4,
  1339. .dname = "avalon4",
  1340. .name = "AV4",
  1341. .set_device = avalon4_set_device,
  1342. .get_api_stats = avalon4_api_stats,
  1343. .get_statline_before = avalon4_statline_before,
  1344. .drv_detect = avalon4_detect,
  1345. .thread_prepare = avalon4_prepare,
  1346. .hash_work = hash_driver_work,
  1347. .flush_work = avalon4_update,
  1348. .update_work = avalon4_update,
  1349. .scanwork = avalon4_scanhash,
  1350. };