driver-hashfast.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954
  1. /*
  2. * Copyright 2013-2014 Con Kolivas <kernel@kolivas.org>
  3. * Copyright 2013 Hashfast Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #include <stdbool.h>
  12. #include <math.h>
  13. #include "miner.h"
  14. #include "usbutils.h"
  15. #include "driver-hashfast.h"
  16. int opt_hfa_ntime_roll = 1;
  17. int opt_hfa_hash_clock = HFA_CLOCK_DEFAULT;
  18. int opt_hfa_overheat = HFA_TEMP_OVERHEAT;
  19. int opt_hfa_target = HFA_TEMP_TARGET;
  20. bool opt_hfa_pll_bypass;
  21. bool opt_hfa_dfu_boot;
  22. int opt_hfa_fan_default = HFA_FAN_DEFAULT;
  23. int opt_hfa_fan_max = HFA_FAN_MAX;
  24. int opt_hfa_fan_min = HFA_FAN_MIN;
  25. int opt_hfa_fail_drop = 10;
  26. bool opt_hfa_noshed;
  27. char *opt_hfa_name;
  28. char *opt_hfa_options;
  29. ////////////////////////////////////////////////////////////////////////////////
  30. // Support for the CRC's used in header (CRC-8) and packet body (CRC-32)
  31. ////////////////////////////////////////////////////////////////////////////////
  32. #define GP8 0x107 /* x^8 + x^2 + x + 1 */
  33. #define DI8 0x07
  34. static bool hfa_crc8_set;
  35. char *set_hfa_fan(char *arg)
  36. {
  37. int val1, val2, ret;
  38. ret = sscanf(arg, "%d-%d", &val1, &val2);
  39. if (ret < 1)
  40. return "No values passed to hfa-fan";
  41. if (ret == 1)
  42. val2 = val1;
  43. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
  44. return "Invalid value passed to hfa-fan";
  45. opt_hfa_fan_min = val1;
  46. opt_hfa_fan_max = val2;
  47. if (opt_hfa_fan_min > opt_hfa_fan_default)
  48. opt_hfa_fan_default = opt_hfa_fan_min;
  49. if (opt_hfa_fan_max < opt_hfa_fan_default)
  50. opt_hfa_fan_default = opt_hfa_fan_max;
  51. return NULL;
  52. }
  53. static unsigned char crc8_table[256]; /* CRC-8 table */
  54. static void hfa_init_crc8(void)
  55. {
  56. int i,j;
  57. unsigned char crc;
  58. hfa_crc8_set = true;
  59. for (i = 0; i < 256; i++) {
  60. crc = i;
  61. for (j = 0; j < 8; j++)
  62. crc = (crc << 1) ^ ((crc & 0x80) ? DI8 : 0);
  63. crc8_table[i] = crc & 0xFF;
  64. }
  65. }
  66. static unsigned char hfa_crc8(unsigned char *h)
  67. {
  68. int i;
  69. unsigned char crc;
  70. h++; // Preamble not included
  71. for (i = 1, crc = 0xff; i < 7; i++)
  72. crc = crc8_table[crc ^ *h++];
  73. return crc;
  74. }
  75. struct hfa_cmd {
  76. uint8_t cmd;
  77. char *cmd_name;
  78. enum usb_cmds usb_cmd;
  79. };
  80. /* Entries in this array need to align with the actual op values specified
  81. * in hf_protocol.h */
  82. #define C_NULL C_MAX
  83. static const struct hfa_cmd hfa_cmds[] = {
  84. {OP_NULL, "OP_NULL", C_NULL}, // 0
  85. {OP_ROOT, "OP_ROOT", C_NULL},
  86. {OP_RESET, "OP_RESET", C_HF_RESET},
  87. {OP_PLL_CONFIG, "OP_PLL_CONFIG", C_HF_PLL_CONFIG},
  88. {OP_ADDRESS, "OP_ADDRESS", C_HF_ADDRESS},
  89. {OP_READDRESS, "OP_READDRESS", C_NULL},
  90. {OP_HIGHEST, "OP_HIGHEST", C_NULL},
  91. {OP_BAUD, "OP_BAUD", C_HF_BAUD},
  92. {OP_UNROOT, "OP_UNROOT", C_NULL}, // 8
  93. {OP_HASH, "OP_HASH", C_HF_HASH},
  94. {OP_NONCE, "OP_NONCE", C_HF_NONCE},
  95. {OP_ABORT, "OP_ABORT", C_HF_ABORT},
  96. {OP_STATUS, "OP_STATUS", C_HF_STATUS},
  97. {OP_GPIO, "OP_GPIO", C_NULL},
  98. {OP_CONFIG, "OP_CONFIG", C_HF_CONFIG},
  99. {OP_STATISTICS, "OP_STATISTICS", C_HF_STATISTICS},
  100. {OP_GROUP, "OP_GROUP", C_NULL}, // 16
  101. {OP_CLOCKGATE, "OP_CLOCKGATE", C_HF_CLOCKGATE},
  102. {OP_USB_INIT, "OP_USB_INIT", C_HF_USB_INIT}, // 18
  103. {OP_GET_TRACE, "OP_GET_TRACE", C_NULL},
  104. {OP_LOOPBACK_USB, "OP_LOOPBACK_USB", C_NULL},
  105. {OP_LOOPBACK_UART, "OP_LOOPBACK_UART", C_NULL},
  106. {OP_DFU, "OP_DFU", C_HF_DFU},
  107. {OP_USB_SHUTDOWN, "OP_USB_SHUTDOWN", C_NULL},
  108. {OP_DIE_STATUS, "OP_DIE_STATUS", C_HF_DIE_STATUS}, // 24
  109. {OP_GWQ_STATUS, "OP_GWQ_STATUS", C_HF_GWQ_STATUS},
  110. {OP_WORK_RESTART, "OP_WORK_RESTART", C_HF_WORK_RESTART},
  111. {OP_USB_STATS1, "OP_USB_STATS1", C_NULL},
  112. {OP_USB_GWQSTATS, "OP_USB_GWQSTATS", C_HF_GWQSTATS},
  113. {OP_USB_NOTICE, "OP_USB_NOTICE", C_HF_NOTICE},
  114. {OP_PING, "OP_PING", C_HF_PING},
  115. {OP_CORE_MAP, "OP_CORE_MAP", C_NULL},
  116. {OP_VERSION, "OP_VERSION", C_NULL}, // 32
  117. {OP_FAN, "OP_FAN", C_HF_FAN},
  118. {OP_NAME, "OP_NAME", C_OP_NAME}
  119. };
  120. #define HF_USB_CMD_OFFSET (128 - 18)
  121. #define HF_USB_CMD(X) (X - HF_USB_CMD_OFFSET)
  122. /* Send an arbitrary frame, consisting of an 8 byte header and an optional
  123. * packet body. */
  124. static bool __hfa_send_frame(struct cgpu_info *hashfast, uint8_t opcode, int tx_length,
  125. uint8_t *packet)
  126. {
  127. struct hashfast_info *info = hashfast->device_data;
  128. int ret, amount;
  129. bool retried = false;
  130. if (unlikely(hashfast->usbinfo.nodev))
  131. return false;
  132. info->last_send = time(NULL);
  133. applog(LOG_DEBUG, "%s %s: Sending %s frame", hashfast->drv->name, hashfast->unique_id, hfa_cmds[opcode].cmd_name);
  134. retry:
  135. ret = usb_write(hashfast, (char *)packet, tx_length, &amount,
  136. hfa_cmds[opcode].usb_cmd);
  137. if (unlikely(ret < 0 || amount != tx_length)) {
  138. if (hashfast->usbinfo.nodev)
  139. return false;
  140. if (!retried) {
  141. applog(LOG_ERR, "%s %s: hfa_send_frame: USB Send error, ret %d amount %d vs. tx_length %d, retrying",
  142. hashfast->drv->name, hashfast->unique_id, ret, amount, tx_length);
  143. retried = true;
  144. goto retry;
  145. }
  146. applog(LOG_ERR, "%s %s: hfa_send_frame: USB Send error, ret %d amount %d vs. tx_length %d",
  147. hashfast->drv->name, hashfast->unique_id, ret, amount, tx_length);
  148. return false;
  149. }
  150. if (retried)
  151. applog(LOG_WARNING, "%s %s: hfa_send_frame: recovered OK", hashfast->drv->name, hashfast->unique_id);
  152. return true;
  153. }
  154. static bool hfa_send_frame(struct cgpu_info *hashfast, uint8_t opcode, uint16_t hdata,
  155. uint8_t *data, int len)
  156. {
  157. uint8_t packet[256];
  158. struct hf_header *p = (struct hf_header *)packet;
  159. int tx_length;
  160. p->preamble = HF_PREAMBLE;
  161. p->operation_code = hfa_cmds[opcode].cmd;
  162. p->chip_address = HF_GWQ_ADDRESS;
  163. p->core_address = 0;
  164. p->hdata = htole16(hdata);
  165. p->data_length = len / 4;
  166. p->crc8 = hfa_crc8(packet);
  167. if (len)
  168. memcpy(&packet[sizeof(struct hf_header)], data, len);
  169. tx_length = sizeof(struct hf_header) + len;
  170. return (__hfa_send_frame(hashfast, opcode, tx_length, packet));
  171. }
  172. /* Send an already assembled packet, consisting of an 8 byte header which may
  173. * or may not be followed by a packet body. */
  174. static bool hfa_send_packet(struct cgpu_info *hashfast, struct hf_header *h, int cmd)
  175. {
  176. int amount, ret, len;
  177. if (unlikely(hashfast->usbinfo.nodev))
  178. return false;
  179. len = sizeof(*h) + h->data_length * 4;
  180. ret = usb_write(hashfast, (char *)h, len, &amount, hfa_cmds[cmd].usb_cmd);
  181. if (ret < 0 || amount != len) {
  182. applog(LOG_WARNING, "%s %s: send_packet: %s USB Send error, ret %d amount %d vs. length %d",
  183. hashfast->drv->name, hashfast->unique_id, hfa_cmds[cmd].cmd_name, ret, amount, len);
  184. return false;
  185. }
  186. return true;
  187. }
  188. #define HFA_GET_HEADER_BUFSIZE 512
  189. static bool hfa_get_header(struct cgpu_info *hashfast, struct hf_header *h, uint8_t *computed_crc)
  190. {
  191. int amount, ret, orig_len, len, ofs = 0;
  192. cgtimer_t ts_start;
  193. char buf[HFA_GET_HEADER_BUFSIZE];
  194. char *header;
  195. if (unlikely(hashfast->usbinfo.nodev))
  196. return false;
  197. orig_len = len = sizeof(*h);
  198. /* Read for up to 500ms till we find the first occurrence of HF_PREAMBLE
  199. * though it should be the first byte unless we get woefully out of
  200. * sync. */
  201. cgtimer_time(&ts_start);
  202. do {
  203. cgtimer_t ts_now, ts_diff;
  204. cgtimer_time(&ts_now);
  205. cgtimer_sub(&ts_now, &ts_start, &ts_diff);
  206. if (cgtimer_to_ms(&ts_diff) > 500)
  207. return false;
  208. if (unlikely(hashfast->usbinfo.nodev))
  209. return false;
  210. if(ofs + len > HFA_GET_HEADER_BUFSIZE) {
  211. // Not expected to happen.
  212. applog(LOG_WARNING, "hfa_get_header() tried to overflow buf[].");
  213. return false;
  214. }
  215. ret = usb_read(hashfast, buf + ofs, len, &amount, C_HF_GETHEADER);
  216. if (unlikely(ret && ret != LIBUSB_ERROR_TIMEOUT))
  217. return false;
  218. ofs += amount;
  219. header = memchr(buf, HF_PREAMBLE, ofs);
  220. if (header) {
  221. /* Toss any leading data we can't use */
  222. if (header != buf) {
  223. memmove(buf, header, ofs);
  224. ofs -= header - buf;
  225. }
  226. len -= ofs;
  227. }
  228. else {
  229. /* HF_PREAMBLE not found, toss all the useless leading data. */
  230. ofs = 0;
  231. len = sizeof(*h);
  232. }
  233. } while (len > 0);
  234. memcpy(h, header, orig_len);
  235. *computed_crc = hfa_crc8((uint8_t *)h);
  236. return true;
  237. }
  238. static bool hfa_get_data(struct cgpu_info *hashfast, char *buf, int len4)
  239. {
  240. int amount, ret, len = len4 * 4;
  241. if (unlikely(hashfast->usbinfo.nodev))
  242. return false;
  243. ret = usb_read(hashfast, buf, len, &amount, C_HF_GETDATA);
  244. if (ret)
  245. return false;
  246. if (amount != len) {
  247. applog(LOG_WARNING, "%s %s: get_data: Strange amount returned %d vs. expected %d",
  248. hashfast->drv->name, hashfast->unique_id, amount, len);
  249. return false;
  250. }
  251. return true;
  252. }
  253. static const char *hf_usb_init_errors[] = {
  254. "Success",
  255. "Reset timeout",
  256. "Address cycle timeout",
  257. "Clockgate operation timeout",
  258. "Configuration operation timeout",
  259. "Excessive core failures",
  260. "All cores failed diagnostics",
  261. "Too many groups configured - increase ntime roll amount",
  262. "Chaining connections detected but secondary board(s) did not respond",
  263. "Secondary board communication error",
  264. "Main board 12V power is bad",
  265. "Secondary board(s) 12V power is bad",
  266. "Main board FPGA programming error",
  267. "Main board FPGA SPI read timeout",
  268. "Main board FPGA Bad magic number",
  269. "Main board FPGA SPI write timeout",
  270. "Main board FPGA register read/write test failed",
  271. "ASIC core power fault",
  272. "Dynamic baud rate change timeout",
  273. "Address failure",
  274. "Regulator programming error",
  275. "Address range inconsistent after mixed reconfiguration",
  276. "Timeout after mixed reconfiguration"
  277. };
  278. static bool hfa_clear_readbuf(struct cgpu_info *hashfast);
  279. struct op_nameframe {
  280. struct hf_header h;
  281. char name[32];
  282. } __attribute__((packed));
  283. static void hfa_write_opname(struct cgpu_info *hashfast, struct hashfast_info *info)
  284. {
  285. const uint8_t opcode = HF_USB_CMD(OP_NAME);
  286. struct op_nameframe nameframe;
  287. struct hf_header *h = (struct hf_header *)&nameframe;
  288. const int tx_length = sizeof(struct op_nameframe);
  289. memset(&nameframe, 0, sizeof(nameframe));
  290. strncpy(nameframe.name, info->op_name, 30);
  291. h->preamble = HF_PREAMBLE;
  292. h->operation_code = hfa_cmds[opcode].cmd;
  293. h->core_address = 1;
  294. h->data_length = 32 / 4;
  295. h->crc8 = hfa_crc8((unsigned char *)h);
  296. applog(LOG_DEBUG, "%s %d: Opname being set to %s", hashfast->drv->name,
  297. hashfast->device_id, info->op_name);
  298. __hfa_send_frame(hashfast, opcode, tx_length, (uint8_t *)&nameframe);
  299. }
  300. /* If no opname or an invalid opname is set, change it to the serial number if
  301. * it exists, or a random name based on timestamp if not. */
  302. static void hfa_choose_opname(struct cgpu_info *hashfast, struct hashfast_info *info)
  303. {
  304. uint64_t usecs;
  305. if (info->serial_number)
  306. sprintf(info->op_name, "%08x", info->serial_number);
  307. else {
  308. struct timeval tv_now;
  309. cgtime(&tv_now);
  310. usecs = (uint64_t)(tv_now.tv_sec) * (uint64_t)1000000 + (uint64_t)tv_now.tv_usec;
  311. sprintf(info->op_name, "%lx", (long unsigned int)usecs);
  312. }
  313. hfa_write_opname(hashfast, info);
  314. }
  315. static bool hfa_send_shutdown(struct cgpu_info *hashfast);
  316. static bool hfa_reset(struct cgpu_info *hashfast, struct hashfast_info *info)
  317. {
  318. struct hf_usb_init_header usb_init[2], *hu = usb_init;
  319. struct hf_usb_init_base *db;
  320. struct hf_usb_init_options *ho;
  321. int retries = 0, i;
  322. bool ret = false;
  323. char buf[1024];
  324. struct hf_header *h = (struct hf_header *)buf;
  325. uint8_t hcrc;
  326. /* Hash clock rate in Mhz. Set to opt_hfa_hash_clock if it has not
  327. * been inherited across a restart. */
  328. if (!info->hash_clock_rate)
  329. info->hash_clock_rate = opt_hfa_hash_clock;
  330. info->group_ntime_roll = opt_hfa_ntime_roll;
  331. info->core_ntime_roll = 1;
  332. // Assemble the USB_INIT request
  333. memset(hu, 0, sizeof(*hu));
  334. hu->preamble = HF_PREAMBLE;
  335. hu->operation_code = OP_USB_INIT;
  336. hu->protocol = PROTOCOL_GLOBAL_WORK_QUEUE; // Protocol to use
  337. if (!opt_hfa_noshed)
  338. hu->shed_supported = true;
  339. // Force PLL bypass
  340. hu->pll_bypass = opt_hfa_pll_bypass;
  341. hu->hash_clock = info->hash_clock_rate; // Hash clock rate in Mhz
  342. if (info->group_ntime_roll > 1 && info->core_ntime_roll) {
  343. ho = (struct hf_usb_init_options *)(hu + 1);
  344. memset(ho, 0, sizeof(*ho));
  345. ho->group_ntime_roll = info->group_ntime_roll;
  346. ho->core_ntime_roll = info->core_ntime_roll;
  347. hu->data_length = sizeof(*ho) / 4;
  348. }
  349. hu->crc8 = hfa_crc8((uint8_t *)hu);
  350. applog(LOG_INFO, "%s %s: Sending OP_USB_INIT with GWQ protocol specified",
  351. hashfast->drv->name, hashfast->unique_id);
  352. resend:
  353. if (unlikely(hashfast->usbinfo.nodev))
  354. goto out;
  355. if (!hfa_clear_readbuf(hashfast))
  356. goto out;
  357. if (!hfa_send_packet(hashfast, (struct hf_header *)hu, HF_USB_CMD(OP_USB_INIT)))
  358. goto out;
  359. // Check for the correct response.
  360. // We extend the normal timeout - a complete device initialization, including
  361. // bringing power supplies up from standby, etc., can take over a second.
  362. tryagain:
  363. for (i = 0; i < 10; i++) {
  364. ret = hfa_get_header(hashfast, h, &hcrc);
  365. if (unlikely(hashfast->usbinfo.nodev))
  366. goto out;
  367. if (ret)
  368. break;
  369. }
  370. if (!ret) {
  371. if (retries++ < 3)
  372. goto resend;
  373. applog(LOG_WARNING, "%s %s: OP_USB_INIT failed!", hashfast->drv->name, hashfast->unique_id);
  374. goto out;
  375. }
  376. if (h->crc8 != hcrc) {
  377. applog(LOG_WARNING, "%s %s: OP_USB_INIT failed! CRC mismatch", hashfast->drv->name, hashfast->unique_id);
  378. ret = false;
  379. goto out;
  380. }
  381. if (h->operation_code != OP_USB_INIT) {
  382. // This can happen if valid packet(s) were in transit *before* the OP_USB_INIT arrived
  383. // at the device, so we just toss the packets and keep looking for the response.
  384. applog(LOG_WARNING, "%s %s: OP_USB_INIT: Tossing packet, valid but unexpected type %d",
  385. hashfast->drv->name, hashfast->unique_id, h->operation_code);
  386. hfa_get_data(hashfast, buf, h->data_length);
  387. if (retries++ < 3)
  388. goto tryagain;
  389. ret = false;
  390. goto out;
  391. }
  392. applog(LOG_DEBUG, "%s %s: Good reply to OP_USB_INIT", hashfast->drv->name, hashfast->unique_id);
  393. applog(LOG_DEBUG, "%s %s: OP_USB_INIT: %d die in chain, %d cores, device_type %d, refclk %d Mhz",
  394. hashfast->drv->name, hashfast->unique_id, h->chip_address, h->core_address, h->hdata & 0xff, (h->hdata >> 8) & 0xff);
  395. // Save device configuration
  396. info->asic_count = h->chip_address;
  397. info->core_count = h->core_address;
  398. info->device_type = (uint8_t)h->hdata;
  399. info->ref_frequency = (uint8_t)(h->hdata >> 8);
  400. info->hash_sequence_head = 0;
  401. info->hash_sequence_tail = 0;
  402. info->device_sequence_tail = 0;
  403. if (info->asic_count == 12)
  404. hashfast->drv->name = "HFS";
  405. else if (info->asic_count == 4)
  406. hashfast->drv->name = "HFB";
  407. // Size in bytes of the core bitmap in bytes
  408. info->core_bitmap_size = (((info->asic_count * info->core_count) + 31) / 32) * 4;
  409. // Get the usb_init_base structure
  410. if (!hfa_get_data(hashfast, (char *)&info->usb_init_base, U32SIZE(info->usb_init_base))) {
  411. applog(LOG_WARNING, "%s %s: OP_USB_INIT failed! Failure to get usb_init_base data",
  412. hashfast->drv->name, hashfast->unique_id);
  413. ret = false;
  414. goto out;
  415. }
  416. db = &info->usb_init_base;
  417. info->firmware_version = ((db->firmware_rev >> 8) & 0xff) + (double)(db->firmware_rev & 0xff) / 10.0;
  418. info->hardware_version = ((db->hardware_rev >> 8) & 0xff) + (double)(db->hardware_rev & 0xff) / 10.0;
  419. applog(LOG_INFO, "%s %s: firmware_rev: %.1f", hashfast->drv->name, hashfast->unique_id,
  420. info->firmware_version);
  421. applog(LOG_INFO, "%s %s: hardware_rev: %.1f", hashfast->drv->name, hashfast->unique_id,
  422. info->hardware_version);
  423. applog(LOG_INFO, "%s %s: serial number: %08x", hashfast->drv->name, hashfast->unique_id,
  424. db->serial_number);
  425. applog(LOG_INFO, "%s %s: hash clockrate: %d Mhz", hashfast->drv->name, hashfast->unique_id,
  426. db->hash_clockrate);
  427. applog(LOG_INFO, "%s %s: inflight_target: %d", hashfast->drv->name, hashfast->unique_id,
  428. db->inflight_target);
  429. applog(LOG_INFO, "%s %s: sequence_modulus: %d", hashfast->drv->name, hashfast->unique_id,
  430. db->sequence_modulus);
  431. // Now a copy of the config data used
  432. if (!hfa_get_data(hashfast, (char *)&info->config_data, U32SIZE(info->config_data))) {
  433. applog(LOG_WARNING, "%s %s: OP_USB_INIT failed! Failure to get config_data",
  434. hashfast->drv->name, hashfast->unique_id);
  435. ret = false;
  436. goto out;
  437. }
  438. // Now the core bitmap
  439. info->core_bitmap = malloc(info->core_bitmap_size);
  440. if (!info->core_bitmap)
  441. quit(1, "Failed to malloc info core bitmap in hfa_reset");
  442. if (!hfa_get_data(hashfast, (char *)info->core_bitmap, info->core_bitmap_size / 4)) {
  443. applog(LOG_WARNING, "%s %s: OP_USB_INIT failed! Failure to get core_bitmap", hashfast->drv->name, hashfast->unique_id);
  444. ret = false;
  445. goto out;
  446. }
  447. // See if the initialization suceeded
  448. if (db->operation_status) {
  449. applog(LOG_ERR, "%s %s: OP_USB_INIT failed! Operation status %d (%s)",
  450. hashfast->drv->name, hashfast->unique_id, db->operation_status,
  451. (db->operation_status < sizeof(hf_usb_init_errors)/sizeof(hf_usb_init_errors[0])) ?
  452. hf_usb_init_errors[db->operation_status] : "Unknown error code");
  453. ret = false;
  454. switch (db->operation_status) {
  455. case E_CORE_POWER_FAULT:
  456. for (i = 0; i < 4; i++) {
  457. if (((db->extra_status_1 >> i) & 0x11) == 0x1) {
  458. applog(LOG_ERR, "%s %s: OP_USB_INIT: Quadrant %d (of 4) regulator failure",
  459. hashfast->drv->name, hashfast->unique_id, i + 1);
  460. }
  461. }
  462. break;
  463. default:
  464. break;
  465. }
  466. goto out;
  467. }
  468. if (!db->hash_clockrate) {
  469. applog(LOG_INFO, "%s %s: OP_USB_INIT failed! Clockrate reported as zero",
  470. hashfast->drv->name, hashfast->unique_id);
  471. ret = false;
  472. goto out;
  473. }
  474. info->num_sequence = db->sequence_modulus;
  475. info->serial_number = db->serial_number;
  476. info->base_clock = db->hash_clockrate;
  477. ret = hfa_clear_readbuf(hashfast);
  478. out:
  479. if (!ret) {
  480. hfa_send_shutdown(hashfast);
  481. usb_nodev(hashfast);
  482. }
  483. return ret;
  484. }
  485. static bool hfa_clear_readbuf(struct cgpu_info *hashfast)
  486. {
  487. int amount, ret = 0;
  488. char buf[512];
  489. do {
  490. if (hashfast->usbinfo.nodev) {
  491. ret = LIBUSB_ERROR_NO_DEVICE;
  492. break;
  493. }
  494. ret = usb_read(hashfast, buf, 512, &amount, C_HF_CLEAR_READ);
  495. } while (!ret && amount);
  496. if (ret && ret != LIBUSB_ERROR_TIMEOUT)
  497. return false;
  498. return true;
  499. }
  500. static bool hfa_send_shutdown(struct cgpu_info *hashfast)
  501. {
  502. bool ret = false;
  503. if (hashfast->usbinfo.nodev)
  504. return ret;
  505. /* Send a restart before the shutdown frame to tell the device to
  506. * discard any work it thinks is in flight for a cleaner restart. */
  507. if (!hfa_send_frame(hashfast, HF_USB_CMD(OP_WORK_RESTART), 0, (uint8_t *)NULL, 0))
  508. return ret;
  509. if (hfa_send_frame(hashfast, HF_USB_CMD(OP_USB_SHUTDOWN), 0, NULL, 0)) {
  510. /* Wait to allow device to properly shut down. */
  511. cgsleep_ms(1000);
  512. ret = true;
  513. }
  514. return ret;
  515. }
  516. static struct cgpu_info *hfa_old_device(struct cgpu_info *hashfast, struct hashfast_info *info)
  517. {
  518. struct cgpu_info *cgpu, *found = NULL;
  519. struct hashfast_info *cinfo = NULL;
  520. int i;
  521. /* See if we can find a zombie instance of the same device */
  522. for (i = 0; i < mining_threads; i++) {
  523. cgpu = mining_thr[i]->cgpu;
  524. if (!cgpu)
  525. continue;
  526. if (cgpu == hashfast)
  527. continue;
  528. if (cgpu->drv->drv_id != DRIVER_hashfast)
  529. continue;
  530. if (!cgpu->usbinfo.nodev)
  531. continue;
  532. cinfo = cgpu->device_data;
  533. if (!cinfo)
  534. continue;
  535. if (info->op_name[0] != '\0' && !strncmp(info->op_name, cinfo->op_name, 32)) {
  536. found = cgpu;
  537. break;
  538. }
  539. if (info->serial_number && info->serial_number == cinfo->serial_number) {
  540. found = cgpu;
  541. break;
  542. }
  543. }
  544. return found;
  545. }
  546. static void hfa_set_clock(struct cgpu_info *hashfast, struct hashfast_info *info)
  547. {
  548. uint16_t hdata;
  549. int i;
  550. hdata = (WR_CLOCK_VALUE << WR_COMMAND_SHIFT) | info->hash_clock_rate;
  551. hfa_send_frame(hashfast, HF_USB_CMD(OP_WORK_RESTART), hdata, (uint8_t *)NULL, 0);
  552. /* We won't know what the real clock is in this case without a
  553. * usb_init_base message so we have to assume it's what we asked. */
  554. info->base_clock = info->hash_clock_rate;
  555. for (i = 0; i < info->asic_count; i++)
  556. info->die_data[i].hash_clock = info->base_clock;
  557. }
  558. /* Look for an op name match and apply any options to its first attempted
  559. * init sequence. This function allows any arbitrary number of extra parameters
  560. * to be added in the future. */
  561. static void hfa_check_options(struct hashfast_info *info)
  562. {
  563. char *p, *options, *found = NULL;
  564. int maxlen, option = 0;
  565. if (!opt_hfa_options)
  566. return;
  567. if (!info->op_name)
  568. return;
  569. maxlen = strlen(info->op_name);
  570. options = strdup(opt_hfa_options);
  571. for (p = strtok(options, ","); p; p = strtok(NULL, ",")) {
  572. int cmplen = strlen(p);
  573. if (maxlen < cmplen)
  574. cmplen = maxlen;
  575. if (cmplen < maxlen)
  576. continue;
  577. if (!strncmp(info->op_name, p, cmplen)) {
  578. found = strdup(p);
  579. break;
  580. }
  581. }
  582. free(options);
  583. if (!found)
  584. return;
  585. for (p = strtok(found, ":"); p; p = strtok(NULL, ":")) {
  586. long lval;
  587. /* Parse each option in order, leaving room to add more */
  588. switch(option++) {
  589. default:
  590. break;
  591. case 1:
  592. lval = strtol(p, NULL, 10);
  593. if (lval < HFA_CLOCK_MIN || lval > HFA_CLOCK_MAX) {
  594. applog(LOG_ERR, "Invalid clock speed %ld set with hashfast option for %s",
  595. lval, info->op_name);
  596. break;
  597. }
  598. info->hash_clock_rate = lval;
  599. break;
  600. }
  601. }
  602. free(found);
  603. }
  604. static bool hfa_detect_common(struct cgpu_info *hashfast)
  605. {
  606. struct hashfast_info *info;
  607. char buf[1024];
  608. struct hf_header *h = (struct hf_header *)buf;
  609. uint8_t hcrc;
  610. bool ret;
  611. int i;
  612. info = calloc(sizeof(struct hashfast_info), 1);
  613. if (!info)
  614. quit(1, "Failed to calloc hashfast_info in hfa_detect_common");
  615. hashfast->device_data = info;
  616. /* Try sending and receiving an OP_NAME */
  617. ret = hfa_send_frame(hashfast, HF_USB_CMD(OP_NAME), 0, (uint8_t *)NULL, 0);
  618. if (hashfast->usbinfo.nodev) {
  619. ret = false;
  620. goto out;
  621. }
  622. if (!ret) {
  623. applog(LOG_WARNING, "%s %d: Failed to send OP_NAME!", hashfast->drv->name,
  624. hashfast->device_id);
  625. goto out;
  626. }
  627. ret = hfa_get_header(hashfast, h, &hcrc);
  628. if (hashfast->usbinfo.nodev) {
  629. ret = false;
  630. goto out;
  631. }
  632. if (!ret) {
  633. /* We should receive a valid header even if OP_NAME isn't
  634. * supported by the firmware. */
  635. applog(LOG_NOTICE, "%s %d: No response to name query - failed init or firmware upgrade required.",
  636. hashfast->drv->name, hashfast->device_id);
  637. ret = true;
  638. } else {
  639. /* Only try to parse the name if the firmware supports OP_NAME */
  640. if (h->operation_code == OP_NAME) {
  641. if (!hfa_get_data(hashfast, info->op_name, 32 / 4)) {
  642. applog(LOG_WARNING, "%s %d: OP_NAME failed! Failure to get op_name data",
  643. hashfast->drv->name, hashfast->device_id);
  644. goto out;
  645. }
  646. info->has_opname = info->opname_valid = true;
  647. applog(LOG_DEBUG, "%s: Returned an OP_NAME", hashfast->drv->name);
  648. for (i = 0; i < 32; i++) {
  649. if (i > 0 && info->op_name[i] == '\0')
  650. break;
  651. /* Make sure the op_name is valid ascii only */
  652. if (info->op_name[i] < 32 || info->op_name[i] > 126) {
  653. info->opname_valid = false;
  654. break;
  655. }
  656. }
  657. }
  658. }
  659. info->cgpu = hashfast;
  660. /* Look for a matching zombie instance and inherit values from it if it
  661. * exists. */
  662. if (info->has_opname && info->opname_valid) {
  663. info->old_cgpu = hfa_old_device(hashfast, info);
  664. if (info->old_cgpu) {
  665. struct hashfast_info *cinfo = info->old_cgpu->device_data;
  666. applog(LOG_NOTICE, "%s: Found old instance by op name %s at device %d",
  667. hashfast->drv->name, info->op_name, info->old_cgpu->device_id);
  668. info->resets = ++cinfo->resets;
  669. info->hash_clock_rate = cinfo->hash_clock_rate;
  670. } else {
  671. applog(LOG_NOTICE, "%s: Found device with name %s", hashfast->drv->name,
  672. info->op_name);
  673. hfa_check_options(info);
  674. }
  675. }
  676. out:
  677. if (!ret) {
  678. if (!hashfast->usbinfo.nodev)
  679. hfa_clear_readbuf(hashfast);
  680. hashfast->device_data = NULL;
  681. free(info);
  682. }
  683. return ret;
  684. }
  685. static bool hfa_initialise(struct cgpu_info *hashfast)
  686. {
  687. int err = 7;
  688. if (hashfast->usbinfo.nodev)
  689. return false;
  690. if (!hfa_clear_readbuf(hashfast))
  691. return false;
  692. #ifdef WIN32
  693. err = usb_transfer(hashfast, 0, 9, 1, 0, C_ATMEL_RESET);
  694. if (!err)
  695. err = usb_transfer(hashfast, 0x21, 0x22, 0, 0, C_ATMEL_OPEN);
  696. if (!err) {
  697. uint32_t buf[2];
  698. /* Magic sequence to reset device only really needed for windows
  699. * but harmless on linux. */
  700. buf[0] = 0x80250000;
  701. buf[1] = 0x00000800;
  702. err = usb_transfer_data(hashfast, 0x21, 0x20, 0x0000, 0, buf,
  703. 7, C_ATMEL_INIT);
  704. }
  705. if (err < 0) {
  706. applog(LOG_INFO, "%s %s: Failed to open with error %s",
  707. hashfast->drv->name, hashfast->unique_id, libusb_error_name(err));
  708. }
  709. #endif
  710. /* Must have transmitted init sequence sized buffer */
  711. return (err == 7);
  712. }
  713. static void hfa_dfu_boot(struct cgpu_info *hashfast)
  714. {
  715. bool ret;
  716. if (unlikely(hashfast->usbinfo.nodev))
  717. return;
  718. ret = hfa_send_frame(hashfast, HF_USB_CMD(OP_DFU), 0, NULL, 0);
  719. applog(LOG_WARNING, "%s %s: %03d:%03d DFU Boot %s", hashfast->drv->name, hashfast->unique_id,
  720. hashfast->usbinfo.bus_number, hashfast->usbinfo.device_address,
  721. ret ? "Succeeded" : "Failed");
  722. }
  723. static struct cgpu_info *hfa_detect_one(libusb_device *dev, struct usb_find_devices *found)
  724. {
  725. struct cgpu_info *hashfast;
  726. hashfast = usb_alloc_cgpu(&hashfast_drv, HASHFAST_MINER_THREADS);
  727. if (!hashfast)
  728. quit(1, "Failed to usb_alloc_cgpu hashfast");
  729. hashfast->unique_id = "";
  730. if (!usb_init(hashfast, dev, found)) {
  731. hashfast = usb_free_cgpu(hashfast);
  732. return NULL;
  733. }
  734. hashfast->usbdev->usb_type = USB_TYPE_STD;
  735. if (!hfa_initialise(hashfast)) {
  736. hashfast = usb_free_cgpu(hashfast);
  737. return NULL;
  738. }
  739. if (opt_hfa_dfu_boot) {
  740. hfa_dfu_boot(hashfast);
  741. hashfast = usb_free_cgpu(hashfast);
  742. opt_hfa_dfu_boot = false;
  743. return NULL;
  744. }
  745. if (!hfa_detect_common(hashfast)) {
  746. usb_uninit(hashfast);
  747. hashfast = usb_free_cgpu(hashfast);
  748. return NULL;
  749. }
  750. if (!add_cgpu(hashfast))
  751. return NULL;
  752. if (opt_hfa_name) {
  753. struct hashfast_info *info = hashfast->device_data;
  754. strncpy(info->op_name, opt_hfa_name, 30);
  755. applog(LOG_NOTICE, "%s %d %03d:%03d: Writing name %s", hashfast->drv->name,
  756. hashfast->device_id, hashfast->usbinfo.bus_number, hashfast->usbinfo.device_address,
  757. info->op_name);
  758. hfa_write_opname(hashfast, info);
  759. opt_hfa_name = NULL;
  760. }
  761. return hashfast;
  762. }
  763. static void hfa_detect(bool __maybe_unused hotplug)
  764. {
  765. /* Set up the CRC tables only once. */
  766. if (!hfa_crc8_set)
  767. hfa_init_crc8();
  768. usb_detect(&hashfast_drv, hfa_detect_one);
  769. }
  770. static bool hfa_get_packet(struct cgpu_info *hashfast, struct hf_header *h)
  771. {
  772. uint8_t hcrc;
  773. bool ret;
  774. if (unlikely(hashfast->usbinfo.nodev))
  775. return false;
  776. ret = hfa_get_header(hashfast, h, &hcrc);
  777. if (unlikely(!ret))
  778. goto out;
  779. if (unlikely(h->crc8 != hcrc)) {
  780. applog(LOG_WARNING, "%s %s: Bad CRC %d vs %d, discarding packet",
  781. hashfast->drv->name, hashfast->unique_id, h->crc8, hcrc);
  782. ret = false;
  783. goto out;
  784. }
  785. if (h->data_length > 0)
  786. ret = hfa_get_data(hashfast, (char *)(h + 1), h->data_length);
  787. if (unlikely(!ret)) {
  788. applog(LOG_WARNING, "%s %s: Failed to get data associated with header",
  789. hashfast->drv->name, hashfast->unique_id);
  790. }
  791. out:
  792. return ret;
  793. }
  794. static void hfa_running_shutdown(struct cgpu_info *hashfast, struct hashfast_info *info);
  795. static void hfa_parse_gwq_status(struct cgpu_info *hashfast, struct hashfast_info *info,
  796. struct hf_header *h)
  797. {
  798. struct hf_gwq_data *g = (struct hf_gwq_data *)(h + 1);
  799. struct work *work;
  800. applog(LOG_DEBUG, "%s %s: OP_GWQ_STATUS, device_head %4d tail %4d my tail %4d shed %3d inflight %4d",
  801. hashfast->drv->name, hashfast->unique_id, g->sequence_head, g->sequence_tail, info->hash_sequence_tail,
  802. g->shed_count, HF_SEQUENCE_DISTANCE(info->hash_sequence_head,g->sequence_tail));
  803. /* This is a special flag that the thermal overload has been tripped */
  804. if (unlikely(h->core_address & 0x80)) {
  805. applog(LOG_ERR, "%s %s: Thermal overload tripped! Shutting down device",
  806. hashfast->drv->name, hashfast->unique_id);
  807. hfa_running_shutdown(hashfast, info);
  808. usb_nodev(hashfast);
  809. return;
  810. }
  811. mutex_lock(&info->lock);
  812. info->raw_hashes += g->hash_count;
  813. info->device_sequence_head = g->sequence_head;
  814. info->device_sequence_tail = g->sequence_tail;
  815. info->shed_count = g->shed_count;
  816. /* Free any work that is no longer required */
  817. while (info->device_sequence_tail != info->hash_sequence_tail) {
  818. if (++info->hash_sequence_tail >= info->num_sequence)
  819. info->hash_sequence_tail = 0;
  820. if (unlikely(!(work = info->works[info->hash_sequence_tail]))) {
  821. applog(LOG_ERR, "%s %s: Bad work sequence tail %d head %d devhead %d devtail %d sequence %d",
  822. hashfast->drv->name, hashfast->unique_id, info->hash_sequence_tail,
  823. info->hash_sequence_head, info->device_sequence_head,
  824. info->device_sequence_tail, info->num_sequence);
  825. hashfast->shutdown = true;
  826. usb_nodev(hashfast);
  827. break;
  828. }
  829. applog(LOG_DEBUG, "%s %s: Completing work on hash_sequence_tail %d",
  830. hashfast->drv->name, hashfast->unique_id, info->hash_sequence_tail);
  831. free_work(work);
  832. info->works[info->hash_sequence_tail] = NULL;
  833. }
  834. mutex_unlock(&info->lock);
  835. }
  836. /* Board temperature conversion */
  837. static float board_temperature(uint16_t adc)
  838. {
  839. float t, r, f, b;
  840. if (adc < 40 || adc > 650)
  841. return((float) 0.0); // Bad count
  842. b = 3590.0;
  843. f = (float)adc / 1023.0;
  844. r = 1.0 / (1.0 / f - 1.0);
  845. t = log(r) / b;
  846. t += 1.0 / (25.0 + 273.15);
  847. t = 1.0 / t - 273.15;
  848. return t;
  849. }
  850. static void hfa_update_die_status(struct cgpu_info *hashfast, struct hashfast_info *info,
  851. struct hf_header *h)
  852. {
  853. struct hf_g1_die_data *d = (struct hf_g1_die_data *)(h + 1), *ds;
  854. int num_included = (h->data_length * 4) / sizeof(struct hf_g1_die_data);
  855. int i, j, die = h->chip_address;
  856. float die_temperature, board_temp;
  857. float core_voltage[6];
  858. // Copy in the data. They're numbered sequentially from the starting point
  859. ds = info->die_status + h->chip_address;
  860. for (i = 0; i < num_included; i++)
  861. memcpy(ds++, d++, sizeof(struct hf_g1_die_data));
  862. for (i = 0, d = &info->die_status[h->chip_address]; i < num_included; i++, d++) {
  863. die += i;
  864. die_temperature = GN_DIE_TEMPERATURE(d->die.die_temperature);
  865. /* Sanity checking */
  866. if (unlikely(die_temperature > 255))
  867. die_temperature = info->die_data[die].temp;
  868. else
  869. info->die_data[die].temp = die_temperature;
  870. board_temp = board_temperature(d->temperature);
  871. if (unlikely(board_temp > 255))
  872. board_temp = info->die_data[die].board_temp;
  873. else
  874. info->die_data[die].board_temp = board_temp;
  875. for (j = 0; j < 6; j++)
  876. core_voltage[j] = GN_CORE_VOLTAGE(d->die.core_voltage[j]);
  877. applog(LOG_DEBUG, "%s %s: die %2d: OP_DIE_STATUS Temps die %.1fC board %.1fC vdd's %.2f %.2f %.2f %.2f %.2f %.2f",
  878. hashfast->drv->name, hashfast->unique_id, die, die_temperature, board_temp,
  879. core_voltage[0], core_voltage[1], core_voltage[2],
  880. core_voltage[3], core_voltage[4], core_voltage[5]);
  881. // XXX Convert board phase currents, voltage, temperature
  882. }
  883. if (die == info->asic_count - 1) {
  884. /* We have a full set of die temperatures, find the highest
  885. * current temperature. */
  886. float max_temp = 0;
  887. info->temp_updates++;
  888. for (die = 0; die < info->asic_count; die++) {
  889. if (info->die_data[die].temp > max_temp)
  890. max_temp = info->die_data[die].temp;
  891. if (info->die_data[die].board_temp > max_temp)
  892. max_temp = info->die_data[die].board_temp;
  893. }
  894. /* Exponentially change the max_temp to smooth out troughs. */
  895. hashfast->temp = hashfast->temp * 0.63 + max_temp * 0.37;
  896. }
  897. if (unlikely(hashfast->temp >= opt_hfa_overheat)) {
  898. /* -1 means new overheat condition */
  899. if (!info->overheat)
  900. info->overheat = -1;
  901. } else if (unlikely(info->overheat && hashfast->temp < opt_hfa_overheat - HFA_TEMP_HYSTERESIS))
  902. info->overheat = 0;
  903. }
  904. static void hfa_parse_nonce(struct thr_info *thr, struct cgpu_info *hashfast,
  905. struct hashfast_info *info, struct hf_header *h)
  906. {
  907. struct hf_candidate_nonce *n = (struct hf_candidate_nonce *)(h + 1);
  908. int i, num_nonces = h->data_length / U32SIZE(sizeof(struct hf_candidate_nonce));
  909. applog(LOG_DEBUG, "%s %s: OP_NONCE: %2d/%2d:, num_nonces %d hdata 0x%04x",
  910. hashfast->drv->name, hashfast->unique_id, h->chip_address, h->core_address, num_nonces, h->hdata);
  911. for (i = 0; i < num_nonces; i++, n++) {
  912. struct work *work = NULL;
  913. applog(LOG_DEBUG, "%s %s: OP_NONCE: %2d: %2d: ntime %2d sequence %4d nonce 0x%08x",
  914. hashfast->drv->name, hashfast->unique_id, h->chip_address, i, n->ntime & HF_NTIME_MASK, n->sequence, n->nonce);
  915. if (n->sequence < info->usb_init_base.sequence_modulus) {
  916. // Find the job from the sequence number
  917. mutex_lock(&info->lock);
  918. work = info->works[n->sequence];
  919. mutex_unlock(&info->lock);
  920. } else {
  921. applog(LOG_INFO, "%s %s: OP_NONCE: Sequence out of range %4d max %4d",
  922. hashfast->drv->name, hashfast->unique_id, n->sequence, info->usb_init_base.sequence_modulus);
  923. }
  924. if (unlikely(!work)) {
  925. info->no_matching_work++;
  926. applog(LOG_INFO, "%s %s: No matching work!", hashfast->drv->name, hashfast->unique_id);
  927. } else {
  928. applog(LOG_DEBUG, "%s %s: OP_NONCE: sequence %d: submitting nonce 0x%08x ntime %d",
  929. hashfast->drv->name, hashfast->unique_id, n->sequence, n->nonce, n->ntime & HF_NTIME_MASK);
  930. if (submit_noffset_nonce(thr, work, n->nonce, n->ntime & HF_NTIME_MASK)) {
  931. mutex_lock(&info->lock);
  932. info->hash_count += 0xffffffffull * work->device_diff;
  933. mutex_unlock(&info->lock);
  934. }
  935. #if 0 /* Not used */
  936. if (unlikely(n->ntime & HF_NONCE_SEARCH)) {
  937. /* This tells us there is another share in the
  938. * next 128 nonces */
  939. applog(LOG_DEBUG, "%s %s: OP_NONCE: SEARCH PROXIMITY EVENT FOUND",
  940. hashfast->drv->name, hashfast->unique_id);
  941. }
  942. #endif
  943. }
  944. }
  945. }
  946. static void hfa_update_die_statistics(struct hashfast_info *info, struct hf_header *h)
  947. {
  948. struct hf_statistics *s = (struct hf_statistics *)(h + 1);
  949. struct hf_long_statistics *l;
  950. // Accumulate the data
  951. l = info->die_statistics + h->chip_address;
  952. l->rx_header_crc += s->rx_header_crc;
  953. l->rx_body_crc += s->rx_body_crc;
  954. l->rx_header_timeouts += s->rx_header_timeouts;
  955. l->rx_body_timeouts += s->rx_body_timeouts;
  956. l->core_nonce_fifo_full += s->core_nonce_fifo_full;
  957. l->array_nonce_fifo_full += s->array_nonce_fifo_full;
  958. l->stats_overrun += s->stats_overrun;
  959. }
  960. static void hfa_update_stats1(struct cgpu_info *hashfast, struct hashfast_info *info,
  961. struct hf_header *h)
  962. {
  963. struct hf_long_usb_stats1 *s1 = &info->stats1;
  964. struct hf_usb_stats1 *sd = (struct hf_usb_stats1 *)(h + 1);
  965. s1->usb_rx_preambles += sd->usb_rx_preambles;
  966. s1->usb_rx_receive_byte_errors += sd->usb_rx_receive_byte_errors;
  967. s1->usb_rx_bad_hcrc += sd->usb_rx_bad_hcrc;
  968. s1->usb_tx_attempts += sd->usb_tx_attempts;
  969. s1->usb_tx_packets += sd->usb_tx_packets;
  970. s1->usb_tx_timeouts += sd->usb_tx_timeouts;
  971. s1->usb_tx_incompletes += sd->usb_tx_incompletes;
  972. s1->usb_tx_endpointstalled += sd->usb_tx_endpointstalled;
  973. s1->usb_tx_disconnected += sd->usb_tx_disconnected;
  974. s1->usb_tx_suspended += sd->usb_tx_suspended;
  975. #if 0
  976. /* We don't care about UART stats so they're not in our struct */
  977. s1->uart_tx_queue_dma += sd->uart_tx_queue_dma;
  978. s1->uart_tx_interrupts += sd->uart_tx_interrupts;
  979. s1->uart_rx_preamble_ints += sd->uart_rx_preamble_ints;
  980. s1->uart_rx_missed_preamble_ints += sd->uart_rx_missed_preamble_ints;
  981. s1->uart_rx_header_done += sd->uart_rx_header_done;
  982. s1->uart_rx_data_done += sd->uart_rx_data_done;
  983. s1->uart_rx_bad_hcrc += sd->uart_rx_bad_hcrc;
  984. s1->uart_rx_bad_dma += sd->uart_rx_bad_dma;
  985. s1->uart_rx_short_dma += sd->uart_rx_short_dma;
  986. s1->uart_rx_buffers_full += sd->uart_rx_buffers_full;
  987. #endif
  988. if (sd->max_tx_buffers > s1->max_tx_buffers)
  989. s1->max_tx_buffers = sd->max_tx_buffers;
  990. if (sd->max_rx_buffers > s1->max_rx_buffers)
  991. s1->max_rx_buffers = sd->max_rx_buffers;
  992. applog(LOG_DEBUG, "%s %s: OP_USB_STATS1:", hashfast->drv->name, hashfast->unique_id);
  993. applog(LOG_DEBUG, " usb_rx_preambles: %6d", sd->usb_rx_preambles);
  994. applog(LOG_DEBUG, " usb_rx_receive_byte_errors: %6d", sd->usb_rx_receive_byte_errors);
  995. applog(LOG_DEBUG, " usb_rx_bad_hcrc: %6d", sd->usb_rx_bad_hcrc);
  996. applog(LOG_DEBUG, " usb_tx_attempts: %6d", sd->usb_tx_attempts);
  997. applog(LOG_DEBUG, " usb_tx_packets: %6d", sd->usb_tx_packets);
  998. applog(LOG_DEBUG, " usb_tx_timeouts: %6d", sd->usb_tx_timeouts);
  999. applog(LOG_DEBUG, " usb_tx_incompletes: %6d", sd->usb_tx_incompletes);
  1000. applog(LOG_DEBUG, " usb_tx_endpointstalled: %6d", sd->usb_tx_endpointstalled);
  1001. applog(LOG_DEBUG, " usb_tx_disconnected: %6d", sd->usb_tx_disconnected);
  1002. applog(LOG_DEBUG, " usb_tx_suspended: %6d", sd->usb_tx_suspended);
  1003. #if 0
  1004. applog(LOG_DEBUG, " uart_tx_queue_dma: %6d", sd->uart_tx_queue_dma);
  1005. applog(LOG_DEBUG, " uart_tx_interrupts: %6d", sd->uart_tx_interrupts);
  1006. applog(LOG_DEBUG, " uart_rx_preamble_ints: %6d", sd->uart_rx_preamble_ints);
  1007. applog(LOG_DEBUG, " uart_rx_missed_preamble_ints: %6d", sd->uart_rx_missed_preamble_ints);
  1008. applog(LOG_DEBUG, " uart_rx_header_done: %6d", sd->uart_rx_header_done);
  1009. applog(LOG_DEBUG, " uart_rx_data_done: %6d", sd->uart_rx_data_done);
  1010. applog(LOG_DEBUG, " uart_rx_bad_hcrc: %6d", sd->uart_rx_bad_hcrc);
  1011. applog(LOG_DEBUG, " uart_rx_bad_dma: %6d", sd->uart_rx_bad_dma);
  1012. applog(LOG_DEBUG, " uart_rx_short_dma: %6d", sd->uart_rx_short_dma);
  1013. applog(LOG_DEBUG, " uart_rx_buffers_full: %6d", sd->uart_rx_buffers_full);
  1014. #endif
  1015. applog(LOG_DEBUG, " max_tx_buffers: %6d", sd->max_tx_buffers);
  1016. applog(LOG_DEBUG, " max_rx_buffers: %6d", sd->max_rx_buffers);
  1017. }
  1018. static void hfa_parse_notice(struct cgpu_info *hashfast, struct hf_header *h)
  1019. {
  1020. struct hf_usb_notice_data *d;
  1021. if (h->data_length == 0) {
  1022. applog(LOG_DEBUG, "%s %s: Received OP_USB_NOTICE with zero data length",
  1023. hashfast->drv->name, hashfast->unique_id);
  1024. return;
  1025. }
  1026. d = (struct hf_usb_notice_data *)(h + 1);
  1027. /* FIXME Do something with the notification code d->extra_data here */
  1028. applog(LOG_NOTICE, "%s %s NOTICE: %s", hashfast->drv->name, hashfast->unique_id, d->message);
  1029. }
  1030. static void *hfa_read(void *arg)
  1031. {
  1032. struct thr_info *thr = (struct thr_info *)arg;
  1033. struct cgpu_info *hashfast = thr->cgpu;
  1034. struct hashfast_info *info = hashfast->device_data;
  1035. char threadname[16];
  1036. snprintf(threadname, sizeof(threadname), "%d/%sRead", hashfast->device_id, hashfast->drv->name);
  1037. RenameThread(threadname);
  1038. while (likely(!hashfast->shutdown)) {
  1039. char buf[512];
  1040. struct hf_header *h = (struct hf_header *)buf;
  1041. bool ret;
  1042. mutex_lock(&info->rlock);
  1043. ret = hfa_get_packet(hashfast, h);
  1044. mutex_unlock(&info->rlock);
  1045. if (unlikely(hashfast->usbinfo.nodev))
  1046. break;
  1047. if (unlikely(!ret))
  1048. continue;
  1049. switch (h->operation_code) {
  1050. case OP_GWQ_STATUS:
  1051. hfa_parse_gwq_status(hashfast, info, h);
  1052. break;
  1053. case OP_DIE_STATUS:
  1054. hfa_update_die_status(hashfast, info, h);
  1055. break;
  1056. case OP_NONCE:
  1057. hfa_parse_nonce(thr, hashfast, info, h);
  1058. break;
  1059. case OP_STATISTICS:
  1060. hfa_update_die_statistics(info, h);
  1061. break;
  1062. case OP_USB_STATS1:
  1063. hfa_update_stats1(hashfast, info, h);
  1064. break;
  1065. case OP_USB_NOTICE:
  1066. hfa_parse_notice(hashfast, h);
  1067. break;
  1068. case OP_PING:
  1069. /* Do nothing */
  1070. break;
  1071. default:
  1072. if (h->operation_code == OP_FAN) {
  1073. applog(LOG_NOTICE, "%s %s: Firmware upgrade required to support fan control",
  1074. hashfast->drv->name, hashfast->unique_id);
  1075. opt_hfa_target = 0;
  1076. break;
  1077. }
  1078. applog(LOG_WARNING, "%s %s: Unhandled operation code %d",
  1079. hashfast->drv->name, hashfast->unique_id, h->operation_code);
  1080. break;
  1081. }
  1082. /* Make sure we send something to the device at least every 5
  1083. * seconds so it knows the driver is still alive for when we
  1084. * run out of work. The read thread never blocks so is the
  1085. * best place to do this. */
  1086. if (time(NULL) - info->last_send > 5)
  1087. hfa_send_frame(hashfast, HF_USB_CMD(OP_PING), 0, NULL, 0);
  1088. }
  1089. applog(LOG_DEBUG, "%s %s: Shutting down read thread", hashfast->drv->name, hashfast->unique_id);
  1090. return NULL;
  1091. }
  1092. static void hfa_set_fanspeed(struct cgpu_info *hashfast, struct hashfast_info *info,
  1093. int fanspeed);
  1094. static bool hfa_init(struct thr_info *thr)
  1095. {
  1096. struct cgpu_info *hashfast = thr->cgpu;
  1097. struct hashfast_info *info = hashfast->device_data;
  1098. struct timeval now;
  1099. bool ret;
  1100. int i;
  1101. if (hashfast->usbinfo.nodev)
  1102. return false;
  1103. /* hashfast_reset should fill in details for info */
  1104. ret = hfa_reset(hashfast, info);
  1105. // The per-die status array
  1106. info->die_status = calloc(info->asic_count, sizeof(struct hf_g1_die_data));
  1107. if (unlikely(!(info->die_status)))
  1108. quit(1, "Failed to calloc die_status");
  1109. info->die_data = calloc(info->asic_count, sizeof(struct hf_die_data));
  1110. if (unlikely(!(info->die_data)))
  1111. quit(1, "Failed to calloc die_data");
  1112. for (i = 0; i < info->asic_count; i++)
  1113. info->die_data[i].hash_clock = info->base_clock;
  1114. // The per-die statistics array
  1115. info->die_statistics = calloc(info->asic_count, sizeof(struct hf_long_statistics));
  1116. if (unlikely(!(info->die_statistics)))
  1117. quit(1, "Failed to calloc die_statistics");
  1118. info->works = calloc(sizeof(struct work *), info->num_sequence);
  1119. if (!info->works)
  1120. quit(1, "Failed to calloc info works in hfa_detect_common");
  1121. if (!ret)
  1122. goto out;
  1123. /* We will have extracted the serial number by now */
  1124. if (info->has_opname && !info->opname_valid)
  1125. hfa_choose_opname(hashfast, info);
  1126. /* Use the opname as the displayed unique identifier */
  1127. hashfast->unique_id = info->op_name;
  1128. /* Inherit the old device id */
  1129. if (info->old_cgpu)
  1130. hashfast->device_id = info->old_cgpu->device_id;
  1131. /* If we haven't found a matching old instance, we might not have
  1132. * a valid op_name yet or lack support so try to match based on
  1133. * serial number. */
  1134. if (!info->old_cgpu)
  1135. info->old_cgpu = hfa_old_device(hashfast, info);
  1136. if (!info->has_opname && info->old_cgpu) {
  1137. struct hashfast_info *cinfo = info->old_cgpu->device_data;
  1138. applog(LOG_NOTICE, "%s: Found old instance by serial number %08x at device %d",
  1139. hashfast->drv->name, info->serial_number, info->old_cgpu->device_id);
  1140. info->resets = ++cinfo->resets;
  1141. /* Set the device with the last hash_clock_rate if it's
  1142. * different. */
  1143. if (info->hash_clock_rate != cinfo->hash_clock_rate) {
  1144. info->hash_clock_rate = cinfo->hash_clock_rate;
  1145. hfa_set_clock(hashfast, info);
  1146. }
  1147. }
  1148. mutex_init(&info->lock);
  1149. mutex_init(&info->rlock);
  1150. if (pthread_create(&info->read_thr, NULL, hfa_read, (void *)thr))
  1151. quit(1, "Failed to pthread_create read thr in hfa_prepare");
  1152. cgtime(&now);
  1153. get_datestamp(hashfast->init, sizeof(hashfast->init), &now);
  1154. hashfast->last_device_valid_work = time(NULL);
  1155. hfa_set_fanspeed(hashfast, info, opt_hfa_fan_default);
  1156. out:
  1157. if (hashfast->usbinfo.nodev)
  1158. ret = false;
  1159. if (!ret) {
  1160. hfa_clear_readbuf(hashfast);
  1161. free(info);
  1162. hashfast->device_data = NULL;
  1163. usb_nodev(hashfast);
  1164. }
  1165. return ret;
  1166. }
  1167. /* If this ever returns 0 it means we have shed all the cores which will lead
  1168. * to no work being done which will trigger the watchdog. */
  1169. static inline int hfa_basejobs(struct hashfast_info *info)
  1170. {
  1171. return info->usb_init_base.inflight_target - info->shed_count;
  1172. }
  1173. /* Figure out how many jobs to send. */
  1174. static int hfa_jobs(struct cgpu_info *hashfast, struct hashfast_info *info)
  1175. {
  1176. int ret = 0;
  1177. if (unlikely(info->overheat)) {
  1178. /* Acknowledge and notify of new condition.*/
  1179. if (info->overheat < 0) {
  1180. applog(LOG_WARNING, "%s %s: Hit overheat temp %.1f, throttling!",
  1181. hashfast->drv->name, hashfast->unique_id, hashfast->temp);
  1182. /* Value of 1 means acknowledged overheat */
  1183. info->overheat = 1;
  1184. }
  1185. goto out;
  1186. }
  1187. mutex_lock(&info->lock);
  1188. ret = hfa_basejobs(info) - HF_SEQUENCE_DISTANCE(info->hash_sequence_head, info->device_sequence_tail);
  1189. /* Place an upper limit on how many jobs to queue to prevent sending
  1190. * more work than the device can use after a period of outage. */
  1191. if (ret > hfa_basejobs(info))
  1192. ret = hfa_basejobs(info);
  1193. mutex_unlock(&info->lock);
  1194. if (unlikely(ret < 0))
  1195. ret = 0;
  1196. out:
  1197. return ret;
  1198. }
  1199. static void hfa_set_fanspeed(struct cgpu_info *hashfast, struct hashfast_info *info,
  1200. int fandiff)
  1201. {
  1202. const uint8_t opcode = HF_USB_CMD(OP_FAN);
  1203. uint8_t packet[256];
  1204. struct hf_header *p = (struct hf_header *)packet;
  1205. const int tx_length = sizeof(struct hf_header);
  1206. uint16_t hdata;
  1207. int fandata;
  1208. info->fanspeed += fandiff;
  1209. if (info->fanspeed > opt_hfa_fan_max)
  1210. info->fanspeed = opt_hfa_fan_max;
  1211. else if (info->fanspeed < opt_hfa_fan_min)
  1212. info->fanspeed = opt_hfa_fan_min;
  1213. fandata = info->fanspeed * 255 / 100; // Fanspeed is in percent, hdata 0-255
  1214. hdata = fandata; // Use an int first to avoid overflowing uint16_t
  1215. p->preamble = HF_PREAMBLE;
  1216. p->operation_code = hfa_cmds[opcode].cmd;
  1217. p->chip_address = 0xff;
  1218. p->core_address = 1;
  1219. p->hdata = htole16(hdata);
  1220. p->data_length = 0;
  1221. p->crc8 = hfa_crc8(packet);
  1222. __hfa_send_frame(hashfast, opcode, tx_length, packet);
  1223. }
  1224. static void hfa_increase_clock(struct cgpu_info *hashfast, struct hashfast_info *info,
  1225. int die)
  1226. {
  1227. int i, high_clock = 0, low_clock = info->hash_clock_rate;
  1228. struct hf_die_data *hdd = &info->die_data[die];
  1229. uint32_t diebit = 0x00000001ul << die;
  1230. uint16_t hdata, increase = 10;
  1231. if (hdd->hash_clock + increase > info->hash_clock_rate)
  1232. increase = info->hash_clock_rate - hdd->hash_clock;
  1233. hdd->hash_clock += increase;
  1234. hdata = (WR_MHZ_INCREASE << 12) | increase;
  1235. if (info->clock_offset) {
  1236. for (i = 0; i < info->asic_count; i++) {
  1237. if (info->die_data[i].hash_clock > high_clock)
  1238. high_clock = info->die_data[i].hash_clock;
  1239. if (info->die_data[i].hash_clock < low_clock)
  1240. low_clock = info->die_data[i].hash_clock;
  1241. }
  1242. if (info->firmware_version < 0.5 && low_clock + HFA_CLOCK_MAXDIFF > high_clock) {
  1243. /* We can increase all clocks again */
  1244. for (i = 0; i < info->asic_count; i++) {
  1245. if (i == die) /* We've already added to this die */
  1246. continue;
  1247. info->die_data[i].hash_clock += increase;
  1248. }
  1249. applog(LOG_INFO, "%s %s: Die %d temp below range %.1f, increasing ALL dies by %d",
  1250. hashfast->drv->name, hashfast->unique_id, die, info->die_data[die].temp, increase);
  1251. hfa_send_frame(hashfast, HF_USB_CMD(OP_WORK_RESTART), hdata, (uint8_t *)NULL, 0);
  1252. info->clock_offset -= increase;
  1253. return;
  1254. }
  1255. }
  1256. applog(LOG_INFO, "%s %s: Die temp below range %.1f, increasing die %d clock to %d",
  1257. hashfast->drv->name, hashfast->unique_id, info->die_data[die].temp, die, hdd->hash_clock);
  1258. hfa_send_frame(hashfast, HF_USB_CMD(OP_WORK_RESTART), hdata, (uint8_t *)&diebit, 4);
  1259. }
  1260. static void hfa_decrease_clock(struct cgpu_info *hashfast, struct hashfast_info *info,
  1261. int die)
  1262. {
  1263. struct hf_die_data *hdd = &info->die_data[die];
  1264. uint32_t diebit = 0x00000001ul << die;
  1265. uint16_t hdata, decrease = 20;
  1266. int i, high_clock = 0;
  1267. /* Find the fastest die for comparison */
  1268. for (i = 0; i < info->asic_count; i++) {
  1269. if (info->die_data[i].hash_clock > high_clock)
  1270. high_clock = info->die_data[i].hash_clock;
  1271. }
  1272. if (hdd->hash_clock - decrease < HFA_CLOCK_MIN)
  1273. decrease = hdd->hash_clock - HFA_CLOCK_MIN;
  1274. hdata = (WR_MHZ_DECREASE << 12) | decrease;
  1275. if (info->firmware_version < 0.5 && high_clock >= hdd->hash_clock + HFA_CLOCK_MAXDIFF) {
  1276. /* We can't have huge differences in clocks as it will lead to
  1277. * starvation of the faster cores so we have no choice but to
  1278. * slow down all dies to tame this one. */
  1279. for (i = 0; i < info->asic_count; i++)
  1280. info->die_data[i].hash_clock -= decrease;
  1281. applog(LOG_INFO, "%s %s: Die %d temp above range %.1f, decreasing ALL die clocks by %d",
  1282. hashfast->drv->name, hashfast->unique_id, die, info->die_data[die].temp, decrease);
  1283. hfa_send_frame(hashfast, HF_USB_CMD(OP_WORK_RESTART), hdata, (uint8_t *)NULL, 0);
  1284. info->clock_offset += decrease;
  1285. return;
  1286. }
  1287. hdd->hash_clock -= decrease;
  1288. applog(LOG_INFO, "%s %s: Die temp above range %.1f, decreasing die %d clock to %d",
  1289. hashfast->drv->name, hashfast->unique_id, info->die_data[die].temp, die, hdd->hash_clock);
  1290. hfa_send_frame(hashfast, HF_USB_CMD(OP_WORK_RESTART), hdata, (uint8_t *)&diebit, 4);
  1291. }
  1292. /* Adjust clock according to temperature if need be by changing the clock
  1293. * setting and issuing a work restart with the new clock speed. */
  1294. static void hfa_temp_clock(struct cgpu_info *hashfast, struct hashfast_info *info)
  1295. {
  1296. int temp_change, i, low_clock;
  1297. time_t now_t = time(NULL);
  1298. bool throttled = false;
  1299. if (!opt_hfa_target)
  1300. return;
  1301. /* First find out if any dies are throttled before trying to optimise
  1302. * fanspeed, and find the slowest clock. */
  1303. low_clock = info->hash_clock_rate;
  1304. for (i = 0; i < info->asic_count ; i++) {
  1305. struct hf_die_data *hdd = &info->die_data[i];
  1306. if (hdd->hash_clock < info->hash_clock_rate)
  1307. throttled = true;
  1308. if (hdd->hash_clock < low_clock)
  1309. low_clock = hdd->hash_clock;
  1310. }
  1311. /* Find the direction of temperature change since we last checked */
  1312. if (info->temp_updates < 5)
  1313. goto dies_only;
  1314. info->temp_updates = 0;
  1315. temp_change = hashfast->temp - info->last_max_temp;
  1316. info->last_max_temp = hashfast->temp;
  1317. /* Adjust fanspeeds first if possible before die speeds, increasing
  1318. * speed quickly and lowering speed slowly */
  1319. if (hashfast->temp > opt_hfa_target ||
  1320. (throttled && hashfast->temp >= opt_hfa_target - HFA_TEMP_HYSTERESIS)) {
  1321. /* We should be trying to decrease temperature, if it's not on
  1322. * its way down. */
  1323. if (info->fanspeed < opt_hfa_fan_max) {
  1324. if (!temp_change)
  1325. hfa_set_fanspeed(hashfast, info, 5);
  1326. else if (temp_change > 0)
  1327. hfa_set_fanspeed(hashfast, info, 10);
  1328. }
  1329. } else if (hashfast->temp >= opt_hfa_target - HFA_TEMP_HYSTERESIS) {
  1330. /* In optimal range, try and maintain the same temp */
  1331. if (temp_change > 0) {
  1332. /* Temp rising, tweak fanspeed up */
  1333. if (info->fanspeed < opt_hfa_fan_max)
  1334. hfa_set_fanspeed(hashfast, info, 2);
  1335. } else if (temp_change < 0) {
  1336. /* Temp falling, tweak fanspeed down */
  1337. if (info->fanspeed > opt_hfa_fan_min)
  1338. hfa_set_fanspeed(hashfast, info, -1);
  1339. }
  1340. } else {
  1341. /* Below optimal range, try and increase temp */
  1342. if (temp_change <= 0 && !throttled) {
  1343. if (info->fanspeed > opt_hfa_fan_min)
  1344. hfa_set_fanspeed(hashfast, info, -1);
  1345. }
  1346. }
  1347. dies_only:
  1348. /* Do no restarts at all if there has been one less than 15 seconds
  1349. * ago */
  1350. if (now_t - info->last_restart < 15)
  1351. return;
  1352. for (i = 1; i <= info->asic_count ; i++) {
  1353. int die = (info->last_die_adjusted + i) % info->asic_count;
  1354. struct hf_die_data *hdd = &info->die_data[die];
  1355. /* Sanity check */
  1356. if (unlikely(hdd->temp == 0.0 || hdd->temp > 255))
  1357. continue;
  1358. /* In target temperature */
  1359. if (hdd->temp >= opt_hfa_target - HFA_TEMP_HYSTERESIS && hdd->temp <= opt_hfa_target)
  1360. continue;
  1361. if (hdd->temp > opt_hfa_target) {
  1362. /* Temp above target range */
  1363. /* Already at min speed */
  1364. if (hdd->hash_clock == HFA_CLOCK_MIN)
  1365. continue;
  1366. /* Have some leeway before throttling speed */
  1367. if (hdd->temp < opt_hfa_target + HFA_TEMP_HYSTERESIS)
  1368. break;
  1369. hfa_decrease_clock(hashfast, info, die);
  1370. } else {
  1371. /* Temp below target range. Only send a restart to
  1372. * increase speed no more than every 60 seconds. */
  1373. if (now_t - hdd->last_restart < 60)
  1374. continue;
  1375. /* Already at max speed */
  1376. if (hdd->hash_clock == info->hash_clock_rate)
  1377. continue;
  1378. /* Do not increase the clocks on any dies if we have
  1379. * a forced offset due to wild differences in clocks,
  1380. * unless this is the slowest one. */
  1381. if (info->clock_offset && hdd->hash_clock > low_clock)
  1382. continue;
  1383. hfa_increase_clock(hashfast, info, die);
  1384. }
  1385. /* Keep track of the last die adjusted since we only adjust
  1386. * one at a time to ensure we end up iterating over all of
  1387. * them. */
  1388. info->last_restart = hdd->last_restart = now_t;
  1389. info->last_die_adjusted = die;
  1390. break;
  1391. }
  1392. }
  1393. static void hfa_running_shutdown(struct cgpu_info *hashfast, struct hashfast_info *info)
  1394. {
  1395. int iruntime = cgpu_runtime(hashfast);
  1396. /* If the device has already disapperaed, don't drop the clock in case
  1397. * it was just unplugged as opposed to a failure. */
  1398. if (hashfast->usbinfo.nodev)
  1399. return;
  1400. /* Only decrease the clock speed if the device has run at this speed
  1401. * for less than an hour before failing, otherwise the hashrate gains
  1402. * are worth the occasional restart which takes at most a minute. */
  1403. if (iruntime < 3600 && info->hash_clock_rate > HFA_CLOCK_DEFAULT && opt_hfa_fail_drop) {
  1404. info->hash_clock_rate -= opt_hfa_fail_drop;
  1405. if (info->hash_clock_rate < HFA_CLOCK_DEFAULT)
  1406. info->hash_clock_rate = HFA_CLOCK_DEFAULT;
  1407. if (info->old_cgpu && info->old_cgpu->device_data) {
  1408. struct hashfast_info *cinfo = info->old_cgpu->device_data;
  1409. /* Set the master device's clock speed if this is a copy */
  1410. cinfo->hash_clock_rate = info->hash_clock_rate;
  1411. }
  1412. applog(LOG_WARNING, "%s %s: Decreasing clock speed to %d with reset",
  1413. hashfast->drv->name, hashfast->unique_id, info->hash_clock_rate);
  1414. }
  1415. if (!hfa_send_shutdown(hashfast))
  1416. return;
  1417. if (hashfast->usbinfo.nodev)
  1418. return;
  1419. mutex_lock(&info->rlock);
  1420. hfa_clear_readbuf(hashfast);
  1421. mutex_unlock(&info->rlock);
  1422. usb_nodev(hashfast);
  1423. }
  1424. static int64_t hfa_scanwork(struct thr_info *thr)
  1425. {
  1426. struct cgpu_info *hashfast = thr->cgpu;
  1427. struct hashfast_info *info = hashfast->device_data;
  1428. struct work *base_work = NULL;
  1429. int jobs, ret, cycles = 0;
  1430. double fail_time;
  1431. int64_t hashes;
  1432. if (unlikely(hashfast->usbinfo.nodev)) {
  1433. applog(LOG_WARNING, "%s %s: device disappeared, disabling",
  1434. hashfast->drv->name, hashfast->unique_id);
  1435. return -1;
  1436. }
  1437. /* Base the fail time on no valid nonces for 25 full nonce ranges at
  1438. * the current expected hashrate. */
  1439. fail_time = 25.0 * (double)hashfast->drv->max_diff * 0xffffffffull /
  1440. (double)(info->base_clock * 1000000) / hfa_basejobs(info);
  1441. if (unlikely(share_work_tdiff(hashfast) > fail_time)) {
  1442. applog(LOG_WARNING, "%s %s: No valid hashes for over %.0f seconds, shutting down thread",
  1443. hashfast->drv->name, hashfast->unique_id, fail_time);
  1444. hfa_running_shutdown(hashfast, info);
  1445. return -1;
  1446. }
  1447. if (unlikely(thr->work_restart)) {
  1448. restart:
  1449. info->last_restart = time(NULL);
  1450. thr->work_restart = false;
  1451. ret = hfa_send_frame(hashfast, HF_USB_CMD(OP_WORK_RESTART), 0, (uint8_t *)NULL, 0);
  1452. if (unlikely(!ret)) {
  1453. hfa_running_shutdown(hashfast, info);
  1454. return -1;
  1455. }
  1456. /* Give a full allotment of jobs after a restart, not waiting
  1457. * for the status update telling us how much to give. */
  1458. jobs = hfa_basejobs(info);
  1459. } else {
  1460. /* Only adjust die clocks if there's no restart since two
  1461. * restarts back to back get ignored. */
  1462. hfa_temp_clock(hashfast, info);
  1463. jobs = hfa_jobs(hashfast, info);
  1464. }
  1465. /* Wait on restart_wait for up to 0.5 seconds or submit jobs as soon as
  1466. * they're required. */
  1467. while (!jobs && ++cycles < 5) {
  1468. ret = restart_wait(thr, 100);
  1469. if (unlikely(!ret))
  1470. goto restart;
  1471. jobs = hfa_jobs(hashfast, info);
  1472. }
  1473. if (jobs) {
  1474. applog(LOG_DEBUG, "%s %s: Sending %d new jobs", hashfast->drv->name, hashfast->unique_id,
  1475. jobs);
  1476. }
  1477. while (jobs-- > 0) {
  1478. struct hf_hash_usb op_hash_data;
  1479. struct work *work;
  1480. uint64_t intdiff;
  1481. int i, sequence;
  1482. uint32_t *p;
  1483. /* This is a blocking function if there's no work */
  1484. if (!base_work)
  1485. base_work = get_work(thr, thr->id);
  1486. /* HFA hardware actually had ntime rolling disabled so we
  1487. * can roll the work ourselves here to minimise the amount of
  1488. * work we need to generate. */
  1489. if (base_work->drv_rolllimit > jobs) {
  1490. base_work->drv_rolllimit--;
  1491. roll_work(base_work);
  1492. work = make_clone(base_work);
  1493. } else {
  1494. work = base_work;
  1495. base_work = NULL;
  1496. }
  1497. /* Assemble the data frame and send the OP_HASH packet */
  1498. memcpy(op_hash_data.midstate, work->midstate, sizeof(op_hash_data.midstate));
  1499. memcpy(op_hash_data.merkle_residual, work->data + 64, 4);
  1500. p = (uint32_t *)(work->data + 64 + 4);
  1501. op_hash_data.timestamp = *p++;
  1502. op_hash_data.bits = *p++;
  1503. op_hash_data.starting_nonce = 0;
  1504. op_hash_data.nonce_loops = 0;
  1505. op_hash_data.ntime_loops = 0;
  1506. /* Set the number of leading zeroes to look for based on diff.
  1507. * Diff 1 = 32, Diff 2 = 33, Diff 4 = 34 etc. */
  1508. intdiff = (uint64_t)work->device_diff;
  1509. for (i = 31; intdiff; i++, intdiff >>= 1);
  1510. op_hash_data.search_difficulty = i;
  1511. op_hash_data.group = 0;
  1512. if ((sequence = info->hash_sequence_head + 1) >= info->num_sequence)
  1513. sequence = 0;
  1514. ret = hfa_send_frame(hashfast, OP_HASH, sequence, (uint8_t *)&op_hash_data, sizeof(op_hash_data));
  1515. if (unlikely(!ret)) {
  1516. free_work(work);
  1517. if (base_work)
  1518. free_work(base_work);
  1519. hfa_running_shutdown(hashfast, info);
  1520. return -1;
  1521. }
  1522. mutex_lock(&info->lock);
  1523. info->hash_sequence_head = sequence;
  1524. info->works[info->hash_sequence_head] = work;
  1525. mutex_unlock(&info->lock);
  1526. applog(LOG_DEBUG, "%s %s: OP_HASH sequence %d search_difficulty %d work_difficulty %g",
  1527. hashfast->drv->name, hashfast->unique_id, info->hash_sequence_head,
  1528. op_hash_data.search_difficulty, work->work_difficulty);
  1529. }
  1530. if (base_work)
  1531. free_work(base_work);
  1532. /* Only count 2/3 of the hashes to smooth out the hashrate for cycles
  1533. * that have no hashes added. */
  1534. mutex_lock(&info->lock);
  1535. hashes = info->hash_count / 3 * 2;
  1536. info->calc_hashes += hashes;
  1537. info->hash_count -= hashes;
  1538. mutex_unlock(&info->lock);
  1539. return hashes;
  1540. }
  1541. static struct api_data *hfa_api_stats(struct cgpu_info *cgpu)
  1542. {
  1543. struct hashfast_info *info;
  1544. struct hf_long_usb_stats1 *s1;
  1545. struct api_data *root = NULL;
  1546. struct hf_usb_init_base *db;
  1547. int varint, i;
  1548. char buf[64];
  1549. info = cgpu->device_data;
  1550. if (!info)
  1551. return NULL;
  1552. root = api_add_int(root, "asic count", &info->asic_count, false);
  1553. root = api_add_int(root, "core count", &info->core_count, false);
  1554. root = api_add_double(root, "firmware rev", &info->firmware_version, false);
  1555. root = api_add_double(root, "hardware rev", &info->hardware_version, false);
  1556. db = &info->usb_init_base;
  1557. root = api_add_hex32(root, "serial number", &db->serial_number, true);
  1558. varint = db->hash_clockrate;
  1559. root = api_add_int(root, "base clockrate", &varint, true);
  1560. varint = db->inflight_target;
  1561. root = api_add_int(root, "inflight target", &varint, true);
  1562. varint = db->sequence_modulus;
  1563. root = api_add_int(root, "sequence modulus", &varint, true);
  1564. root = api_add_int(root, "fan percent", &info->fanspeed, false);
  1565. if (info->op_name[0] != '\0')
  1566. root = api_add_string(root, "op name", info->op_name, false);
  1567. s1 = &info->stats1;
  1568. root = api_add_uint64(root, "rx preambles", &s1->usb_rx_preambles, false);
  1569. root = api_add_uint64(root, "rx rcv byte err", &s1->usb_rx_receive_byte_errors, false);
  1570. root = api_add_uint64(root, "rx bad hcrc", &s1->usb_rx_bad_hcrc, false);
  1571. root = api_add_uint64(root, "tx attempts", &s1->usb_tx_attempts, false);
  1572. root = api_add_uint64(root, "tx packets", &s1->usb_tx_packets, false);
  1573. root = api_add_uint64(root, "tx incompletes", &s1->usb_tx_incompletes, false);
  1574. root = api_add_uint64(root, "tx ep stalled", &s1->usb_tx_endpointstalled, false);
  1575. root = api_add_uint64(root, "tx disconnect", &s1->usb_tx_disconnected, false);
  1576. root = api_add_uint64(root, "tx suspend", &s1->usb_tx_suspended, false);
  1577. varint = s1->max_tx_buffers;
  1578. root = api_add_int(root, "max tx buf", &varint, true);
  1579. varint = s1->max_rx_buffers;
  1580. root = api_add_int(root, "max rx buf", &varint, true);
  1581. for (i = 0; i < info->asic_count; i++) {
  1582. struct hf_long_statistics *l;
  1583. struct hf_g1_die_data *d;
  1584. char which[16];
  1585. double val;
  1586. int j;
  1587. if (!info->die_statistics || !info->die_status)
  1588. continue;
  1589. l = &info->die_statistics[i];
  1590. if (!l)
  1591. continue;
  1592. d = &info->die_status[i];
  1593. if (!d)
  1594. continue;
  1595. snprintf(which, sizeof(which), "Asic%d", i);
  1596. snprintf(buf, sizeof(buf), "%s hash clockrate", which);
  1597. root = api_add_int(root, buf, &(info->die_data[i].hash_clock), false);
  1598. snprintf(buf, sizeof(buf), "%s die temperature", which);
  1599. val = GN_DIE_TEMPERATURE(d->die.die_temperature);
  1600. root = api_add_double(root, buf, &val, true);
  1601. snprintf(buf, sizeof(buf), "%s board temperature", which);
  1602. val = board_temperature(d->temperature);
  1603. root = api_add_double(root, buf, &val, true);
  1604. for (j = 0; j < 6; j++) {
  1605. snprintf(buf, sizeof(buf), "%s voltage %d", which, j);
  1606. val = GN_CORE_VOLTAGE(d->die.core_voltage[j]);
  1607. root = api_add_utility(root, buf, &val, true);
  1608. }
  1609. snprintf(buf, sizeof(buf), "%s rx header crc", which);
  1610. root = api_add_uint64(root, buf, &l->rx_header_crc, false);
  1611. snprintf(buf, sizeof(buf), "%s rx body crc", which);
  1612. root = api_add_uint64(root, buf, &l->rx_body_crc, false);
  1613. snprintf(buf, sizeof(buf), "%s rx header to", which);
  1614. root = api_add_uint64(root, buf, &l->rx_header_timeouts, false);
  1615. snprintf(buf, sizeof(buf), "%s rx body to", which);
  1616. root = api_add_uint64(root, buf, &l->rx_body_timeouts, false);
  1617. snprintf(buf, sizeof(buf), "%s cn fifo full", which);
  1618. root = api_add_uint64(root, buf, &l->core_nonce_fifo_full, false);
  1619. snprintf(buf, sizeof(buf), "%s an fifo full", which);
  1620. root = api_add_uint64(root, buf, &l->array_nonce_fifo_full, false);
  1621. snprintf(buf, sizeof(buf), "%s stats overrun", which);
  1622. root = api_add_uint64(root, buf, &l->stats_overrun, false);
  1623. }
  1624. root = api_add_uint64(root, "raw hashcount", &info->raw_hashes, false);
  1625. root = api_add_uint64(root, "calc hashcount", &info->calc_hashes, false);
  1626. root = api_add_int(root, "no matching work", &info->no_matching_work, false);
  1627. root = api_add_uint16(root, "shed count", &info->shed_count, false);
  1628. root = api_add_int(root, "resets", &info->resets, false);
  1629. return root;
  1630. }
  1631. static void hfa_statline_before(char *buf, size_t bufsiz, struct cgpu_info *hashfast)
  1632. {
  1633. struct hashfast_info *info;
  1634. struct hf_g1_die_data *d;
  1635. double max_volt;
  1636. int i;
  1637. if (!hashfast->device_data)
  1638. return;
  1639. info = hashfast->device_data;
  1640. /* Can happen during init sequence */
  1641. if (!info->die_status)
  1642. return;
  1643. max_volt = 0.0;
  1644. for (i = 0; i < info->asic_count; i++) {
  1645. int j;
  1646. d = &info->die_status[i];
  1647. for (j = 0; j < 6; j++) {
  1648. double volt = GN_CORE_VOLTAGE(d->die.core_voltage[j]);
  1649. if (volt > max_volt)
  1650. max_volt = volt;
  1651. }
  1652. }
  1653. tailsprintf(buf, bufsiz, "%3dMHz %3.0fC %3d%% %3.2fV", info->base_clock,
  1654. hashfast->temp, info->fanspeed, max_volt);
  1655. }
  1656. /* We cannot re-initialise so just shut down the device for it to hotplug
  1657. * again. */
  1658. static void hfa_reinit(struct cgpu_info *hashfast)
  1659. {
  1660. if (hashfast && hashfast->device_data)
  1661. hfa_running_shutdown(hashfast, hashfast->device_data);
  1662. }
  1663. static void hfa_free_all_work(struct hashfast_info *info)
  1664. {
  1665. while (info->device_sequence_tail != info->hash_sequence_head) {
  1666. struct work *work;
  1667. if (++info->hash_sequence_tail >= info->num_sequence)
  1668. info->hash_sequence_tail = 0;
  1669. if (unlikely(!(work = info->works[info->hash_sequence_tail])))
  1670. break;
  1671. free_work(work);
  1672. info->works[info->hash_sequence_tail] = NULL;
  1673. }
  1674. }
  1675. static void hfa_shutdown(struct thr_info *thr)
  1676. {
  1677. struct cgpu_info *hashfast = thr->cgpu;
  1678. struct hashfast_info *info = hashfast->device_data;
  1679. hfa_send_shutdown(hashfast);
  1680. pthread_join(info->read_thr, NULL);
  1681. hfa_free_all_work(info);
  1682. hfa_clear_readbuf(hashfast);
  1683. free(info->works);
  1684. free(info->die_statistics);
  1685. free(info->die_status);
  1686. free(info->die_data);
  1687. /* Keep the device data intact to allow new instances to match old
  1688. * ones. */
  1689. }
  1690. struct device_drv hashfast_drv = {
  1691. .drv_id = DRIVER_hashfast,
  1692. .dname = "Hashfast",
  1693. .name = "HFA",
  1694. .max_diff = 32.0, // Limit max diff to get some nonces back regardless
  1695. .drv_detect = hfa_detect,
  1696. .thread_init = hfa_init,
  1697. .hash_work = &hash_driver_work,
  1698. .scanwork = hfa_scanwork,
  1699. .get_api_stats = hfa_api_stats,
  1700. .get_statline_before = hfa_statline_before,
  1701. .reinit_device = hfa_reinit,
  1702. .thread_shutdown = hfa_shutdown,
  1703. };