driver-avalonmm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /*
  2. * Copyright 2014 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #include <stdbool.h>
  11. #include <stdint.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <utlist.h>
  16. #include "deviceapi.h"
  17. #include "logging.h"
  18. #include "lowlevel.h"
  19. #include "lowl-vcom.h"
  20. #include "miner.h"
  21. #include "util.h"
  22. #include "work2d.h"
  23. #define AVALONMM_MAX_MODULES 4
  24. #define AVALONMM_MAX_COINBASE_SIZE (6 * 1024)
  25. #define AVALONMM_MAX_MERKLES 20
  26. #define AVALONMM_MAX_NONCE_DIFF 0x20
  27. // Must be a power of two
  28. #define AVALONMM_CACHED_JOBS 2
  29. #define AVALONMM_NONCE_OFFSET 0x180
  30. BFG_REGISTER_DRIVER(avalonmm_drv)
  31. static const struct bfg_set_device_definition avalonmm_set_device_funcs[];
  32. #define AVALONMM_PKT_DATA_SIZE 0x20
  33. #define AVALONMM_PKT_SIZE (AVALONMM_PKT_DATA_SIZE + 7)
  34. enum avalonmm_cmd {
  35. AMC_DETECT = 0x0a,
  36. AMC_NEW_JOB = 0x0b,
  37. AMC_JOB_ID = 0x0c,
  38. AMC_COINBASE = 0x0d,
  39. AMC_MERKLES = 0x0e,
  40. AMC_BLKHDR = 0x0f,
  41. AMC_POLL = 0x10,
  42. AMC_TARGET = 0x11,
  43. AMC_START = 0x13,
  44. };
  45. enum avalonmm_reply {
  46. AMR_NONCE = 0x17,
  47. AMR_STATUS = 0x18,
  48. AMR_DETECT_ACK = 0x19,
  49. };
  50. static
  51. bool avalonmm_write_cmd(const int fd, const enum avalonmm_cmd cmd, const void *data, size_t datasz)
  52. {
  53. uint8_t packets = ((datasz + AVALONMM_PKT_DATA_SIZE - 1) / AVALONMM_PKT_DATA_SIZE) ?: 1;
  54. uint8_t pkt[AVALONMM_PKT_SIZE] = {'A', 'V', cmd, 1, packets};
  55. uint16_t crc;
  56. ssize_t r;
  57. while (true)
  58. {
  59. size_t copysz = AVALONMM_PKT_DATA_SIZE;
  60. if (datasz < copysz)
  61. {
  62. copysz = datasz;
  63. memset(&pkt[5 + copysz], '\0', AVALONMM_PKT_DATA_SIZE - copysz);
  64. }
  65. if (copysz)
  66. memcpy(&pkt[5], data, copysz);
  67. crc = crc16xmodem(&pkt[5], AVALONMM_PKT_DATA_SIZE);
  68. pk_u16be(pkt, 5 + AVALONMM_PKT_DATA_SIZE, crc);
  69. r = write(fd, pkt, sizeof(pkt));
  70. if (opt_dev_protocol)
  71. {
  72. char hex[(sizeof(pkt) * 2) + 1];
  73. bin2hex(hex, pkt, sizeof(pkt));
  74. applog(LOG_DEBUG, "DEVPROTO fd=%d SEND: %s => %d", fd, hex, (int)r);
  75. }
  76. if (sizeof(pkt) != r)
  77. return false;
  78. datasz -= copysz;
  79. if (!datasz)
  80. break;
  81. data += copysz;
  82. ++pkt[3];
  83. }
  84. return true;
  85. }
  86. static
  87. ssize_t avalonmm_read(const int fd, const int logprio, enum avalonmm_reply *out_reply, void * const bufp, size_t bufsz)
  88. {
  89. uint8_t *buf = bufp;
  90. uint8_t pkt[AVALONMM_PKT_SIZE];
  91. uint8_t packets = 0, got = 0;
  92. uint16_t good_crc, actual_crc;
  93. ssize_t r;
  94. while (true)
  95. {
  96. r = serial_read(fd, pkt, sizeof(pkt));
  97. if (opt_dev_protocol)
  98. {
  99. if (r >= 0)
  100. {
  101. char hex[(r * 2) + 1];
  102. bin2hex(hex, pkt, r);
  103. applog(LOG_DEBUG, "DEVPROTO fd=%d RECV: %s", fd, hex);
  104. }
  105. else
  106. applog(LOG_DEBUG, "DEVPROTO fd=%d RECV (%d)", fd, (int)r);
  107. }
  108. if (r != sizeof(pkt))
  109. return -1;
  110. if (memcmp(pkt, "AV", 2))
  111. applogr(-1, logprio, "%s: bad header", __func__);
  112. good_crc = crc16xmodem(&pkt[5], AVALONMM_PKT_DATA_SIZE);
  113. actual_crc = upk_u16le(pkt, 5 + AVALONMM_PKT_DATA_SIZE);
  114. if (good_crc != actual_crc)
  115. applogr(-1, logprio, "%s: bad CRC (good=%04x actual=%04x)", __func__, good_crc, actual_crc);
  116. *out_reply = pkt[2];
  117. if (!got)
  118. {
  119. if (pkt[3] != 1)
  120. applogr(-1, logprio, "%s: first packet is not index 1", __func__);
  121. ++got;
  122. packets = pkt[4];
  123. }
  124. else
  125. {
  126. if (pkt[3] != ++got)
  127. applogr(-1, logprio, "%s: packet %d is not index %d", __func__, got, got);
  128. if (pkt[4] != packets)
  129. applogr(-1, logprio, "%s: packet %d total packet count is %d rather than original value of %d", __func__, got, pkt[4], packets);
  130. }
  131. if (bufsz)
  132. {
  133. if (likely(bufsz > AVALONMM_PKT_DATA_SIZE))
  134. {
  135. memcpy(buf, &pkt[5], AVALONMM_PKT_DATA_SIZE);
  136. bufsz -= AVALONMM_PKT_DATA_SIZE;
  137. buf += AVALONMM_PKT_DATA_SIZE;
  138. }
  139. else
  140. {
  141. memcpy(buf, &pkt[5], bufsz);
  142. bufsz = 0;
  143. }
  144. }
  145. if (got == packets)
  146. break;
  147. }
  148. return (((ssize_t)got) * AVALONMM_PKT_DATA_SIZE);
  149. }
  150. static
  151. bool avalonmm_detect_one(const char * const devpath)
  152. {
  153. uint8_t buf[AVALONMM_PKT_DATA_SIZE] = {0};
  154. enum avalonmm_reply reply;
  155. const int fd = serial_open(devpath, 0, 1, true);
  156. struct cgpu_info *prev_cgpu = NULL;
  157. if (fd == -1)
  158. applogr(false, LOG_DEBUG, "%s: Failed to open %s", __func__, devpath);
  159. for (int i = 0; i < AVALONMM_MAX_MODULES; ++i)
  160. {
  161. pk_u32be(buf, AVALONMM_PKT_DATA_SIZE - 4, i);
  162. avalonmm_write_cmd(fd, AMC_DETECT, buf, AVALONMM_PKT_DATA_SIZE);
  163. }
  164. while (avalonmm_read(fd, LOG_DEBUG, &reply, NULL, 0) > 0)
  165. {
  166. if (reply != AMR_DETECT_ACK)
  167. continue;
  168. int moduleno = upk_u32be(buf, AVALONMM_PKT_DATA_SIZE - 4);
  169. struct cgpu_info * const cgpu = malloc(sizeof(*cgpu));
  170. *cgpu = (struct cgpu_info){
  171. .drv = &avalonmm_drv,
  172. .device_path = prev_cgpu ? prev_cgpu->device_path : strdup(devpath),
  173. .device_data = (void*)(intptr_t)moduleno,
  174. .set_device_funcs = avalonmm_set_device_funcs,
  175. .deven = DEV_ENABLED,
  176. .procs = 1,
  177. .threads = prev_cgpu ? 0 : 1,
  178. };
  179. add_cgpu_slave(cgpu, prev_cgpu);
  180. prev_cgpu = cgpu;
  181. }
  182. serial_close(fd);
  183. return prev_cgpu;
  184. }
  185. static
  186. bool avalonmm_lowl_probe(const struct lowlevel_device_info * const info)
  187. {
  188. return vcom_lowl_probe_wrapper(info, avalonmm_detect_one);
  189. }
  190. struct avalonmm_job {
  191. struct stratum_work swork;
  192. uint32_t jobid;
  193. struct timeval tv_prepared;
  194. double nonce_diff;
  195. };
  196. struct avalonmm_chain_state {
  197. uint32_t xnonce1;
  198. struct avalonmm_job *jobs[AVALONMM_CACHED_JOBS];
  199. uint32_t next_jobid;
  200. uint32_t clock_desired;
  201. uint32_t voltcfg_desired;
  202. };
  203. struct avalonmm_module_state {
  204. uint32_t module_id;
  205. uint16_t temp[2];
  206. uint16_t fan[2];
  207. uint32_t clock_actual;
  208. uint32_t voltcfg_actual;
  209. };
  210. static
  211. uint16_t avalonmm_voltage_config_from_dmvolts(uint32_t dmvolts)
  212. {
  213. return ((uint16_t)bitflip8((0x78 - dmvolts / 125) << 1 | 1)) << 8;
  214. }
  215. // Potentially lossy!
  216. static
  217. uint32_t avalonmm_dmvolts_from_voltage_config(uint32_t voltcfg)
  218. {
  219. return (0x78 - (bitflip8(voltcfg >> 8) >> 1)) * 125;
  220. }
  221. static struct cgpu_info *avalonmm_dev_for_module_id(struct cgpu_info *, uint32_t);
  222. static bool avalonmm_poll_once(struct cgpu_info *, int64_t *);
  223. static
  224. bool avalonmm_init(struct thr_info * const master_thr)
  225. {
  226. struct cgpu_info * const master_dev = master_thr->cgpu, *dev = NULL;
  227. const char * const devpath = master_dev->device_path;
  228. const int fd = serial_open(devpath, 115200, 1, true);
  229. uint8_t buf[AVALONMM_PKT_DATA_SIZE] = {0};
  230. int64_t module_id;
  231. master_dev->device_fd = fd;
  232. if (unlikely(fd == -1))
  233. applogr(false, LOG_ERR, "%s: Failed to initialise", master_dev->dev_repr);
  234. struct avalonmm_chain_state * const chain = malloc(sizeof(*chain));
  235. *chain = (struct avalonmm_chain_state){
  236. .voltcfg_desired = avalonmm_voltage_config_from_dmvolts(6625),
  237. };
  238. work2d_init();
  239. if (!reserve_work2d_(&chain->xnonce1))
  240. {
  241. applog(LOG_ERR, "%s: Failed to reserve 2D work", master_dev->dev_repr);
  242. free(chain);
  243. serial_close(fd);
  244. return false;
  245. }
  246. for_each_managed_proc(proc, master_dev)
  247. {
  248. if (dev == proc->device)
  249. continue;
  250. dev = proc->device;
  251. struct thr_info * const thr = proc->thr[0];
  252. struct avalonmm_module_state * const module = malloc(sizeof(*module));
  253. *module = (struct avalonmm_module_state){
  254. .module_id = (intptr_t)dev->device_data,
  255. };
  256. proc->device_data = chain;
  257. thr->cgpu_data = module;
  258. }
  259. dev = NULL;
  260. for_each_managed_proc(proc, master_dev)
  261. {
  262. cgpu_set_defaults(proc);
  263. proc->status = LIFE_INIT2;
  264. }
  265. if (!chain->clock_desired)
  266. {
  267. // Get a reasonable default frequency
  268. dev = master_dev;
  269. struct thr_info * const thr = dev->thr[0];
  270. struct avalonmm_module_state * const module = thr->cgpu_data;
  271. resend:
  272. pk_u32be(buf, AVALONMM_PKT_DATA_SIZE - 4, module->module_id);
  273. avalonmm_write_cmd(fd, AMC_POLL, buf, AVALONMM_PKT_DATA_SIZE);
  274. while (avalonmm_poll_once(master_dev, &module_id))
  275. {
  276. if (module_id != module->module_id)
  277. continue;
  278. if (module->clock_actual)
  279. {
  280. chain->clock_desired = module->clock_actual;
  281. break;
  282. }
  283. else
  284. goto resend;
  285. }
  286. }
  287. if (likely(chain->clock_desired))
  288. applog(LOG_DEBUG, "%s: Frequency is initialised with %d MHz", master_dev->dev_repr, chain->clock_desired);
  289. else
  290. applogr(false, LOG_ERR, "%s: No frequency detected, please use --set %s@%s:clock=MHZ", master_dev->dev_repr, master_dev->drv->dname, devpath);
  291. return true;
  292. }
  293. static
  294. bool avalonmm_send_swork(const int fd, struct avalonmm_chain_state * const chain, const struct stratum_work * const swork, uint32_t jobid, double *out_nonce_diff)
  295. {
  296. uint8_t buf[AVALONMM_PKT_DATA_SIZE];
  297. bytes_t coinbase = BYTES_INIT;
  298. int coinbase_len = bytes_len(&swork->coinbase);
  299. if (coinbase_len > AVALONMM_MAX_COINBASE_SIZE)
  300. return false;
  301. if (swork->merkles > AVALONMM_MAX_MERKLES)
  302. return false;
  303. pk_u32be(buf, 0, coinbase_len);
  304. const size_t xnonce2_offset = swork->nonce2_offset + work2d_pad_xnonce_size(swork) + work2d_xnonce1sz;
  305. pk_u32be(buf, 4, xnonce2_offset);
  306. pk_u32be(buf, 8, 4); // extranonce2 size, but only 4 is supported - smaller sizes are handled by limiting the range
  307. pk_u32be(buf, 0x0c, 0x24); // merkle_offset, always 0x24 for Bitcoin
  308. pk_u32be(buf, 0x10, swork->merkles);
  309. pk_u32be(buf, 0x14, 1); // diff? poorly defined
  310. pk_u32be(buf, 0x18, 0); // pool number - none of its business
  311. if (!avalonmm_write_cmd(fd, AMC_NEW_JOB, buf, 0x1c))
  312. return false;
  313. double nonce_diff = target_diff(swork->target);
  314. if (nonce_diff >= AVALONMM_MAX_NONCE_DIFF)
  315. set_target_to_pdiff(buf, nonce_diff = AVALONMM_MAX_NONCE_DIFF);
  316. else
  317. memcpy(buf, swork->target, 0x20);
  318. *out_nonce_diff = nonce_diff;
  319. if (!avalonmm_write_cmd(fd, AMC_TARGET, buf, 0x20))
  320. return false;
  321. pk_u32be(buf, 0, jobid);
  322. if (!avalonmm_write_cmd(fd, AMC_JOB_ID, buf, 4))
  323. return false;
  324. // Need to add extranonce padding and extranonce2
  325. bytes_cpy(&coinbase, &swork->coinbase);
  326. uint8_t *cbp = bytes_buf(&coinbase);
  327. cbp += swork->nonce2_offset;
  328. work2d_pad_xnonce(cbp, swork, false);
  329. cbp += work2d_pad_xnonce_size(swork);
  330. memcpy(cbp, &chain->xnonce1, work2d_xnonce1sz);
  331. cbp += work2d_xnonce1sz;
  332. if (!avalonmm_write_cmd(fd, AMC_COINBASE, bytes_buf(&coinbase), bytes_len(&coinbase)))
  333. return false;
  334. if (!avalonmm_write_cmd(fd, AMC_MERKLES, bytes_buf(&swork->merkle_bin), bytes_len(&swork->merkle_bin)))
  335. return false;
  336. uint8_t header_bin[0x80];
  337. memcpy(&header_bin[ 0], swork->header1, 0x24);
  338. memset(&header_bin[0x24], '\0', 0x20); // merkle root
  339. pk_u32be(header_bin, 0x44, swork->ntime);
  340. memcpy(&header_bin[0x48], swork->diffbits, 4);
  341. memset(&header_bin[0x4c], '\0', 4); // nonce
  342. memcpy(&header_bin[0x50], bfg_workpadding_bin, 0x30);
  343. if (!avalonmm_write_cmd(fd, AMC_BLKHDR, header_bin, sizeof(header_bin)))
  344. return false;
  345. // Avalon MM cannot handle xnonce2_size other than 4, and works in big endian, so we use a range to ensure the following bytes match
  346. const int fixed_mm_xnonce2_bytes = (work2d_xnonce2sz >= 4) ? 0 : (4 - work2d_xnonce2sz);
  347. uint8_t mm_xnonce2_start[4];
  348. uint32_t xnonce2_range;
  349. memset(mm_xnonce2_start, '\0', 4);
  350. cbp += work2d_xnonce2sz;
  351. for (int i = 1; i <= fixed_mm_xnonce2_bytes; ++i)
  352. mm_xnonce2_start[fixed_mm_xnonce2_bytes - i] = cbp++[0];
  353. if (fixed_mm_xnonce2_bytes > 0)
  354. xnonce2_range = (1 << (8 * work2d_xnonce2sz)) - 1;
  355. else
  356. xnonce2_range = 0xffffffff;
  357. pk_u32be(buf, 0, 80); // fan speed %
  358. pk_u32be(buf, 4, chain->voltcfg_desired);
  359. pk_u32be(buf, 8, chain->clock_desired);
  360. memcpy(&buf[0xc], mm_xnonce2_start, 4);
  361. pk_u32be(buf, 0x10, xnonce2_range);
  362. if (!avalonmm_write_cmd(fd, AMC_START, buf, 0x14))
  363. return false;
  364. return true;
  365. }
  366. static
  367. void avalonmm_free_job(struct avalonmm_job * const mmjob)
  368. {
  369. stratum_work_clean(&mmjob->swork);
  370. free(mmjob);
  371. }
  372. static
  373. bool avalonmm_update_swork_from_pool(struct cgpu_info * const master_dev, struct pool * const pool)
  374. {
  375. struct avalonmm_chain_state * const chain = master_dev->device_data;
  376. const int fd = master_dev->device_fd;
  377. struct avalonmm_job *mmjob = malloc(sizeof(*mmjob));
  378. *mmjob = (struct avalonmm_job){
  379. .jobid = chain->next_jobid,
  380. };
  381. cg_rlock(&pool->data_lock);
  382. stratum_work_cpy(&mmjob->swork, &pool->swork);
  383. cg_runlock(&pool->data_lock);
  384. timer_set_now(&mmjob->tv_prepared);
  385. mmjob->swork.data_lock_p = NULL;
  386. if (!avalonmm_send_swork(fd, chain, &mmjob->swork, mmjob->jobid, &mmjob->nonce_diff))
  387. {
  388. avalonmm_free_job(mmjob);
  389. return false;
  390. }
  391. applog(LOG_DEBUG, "%s: Upload of job id %08lx complete", master_dev->dev_repr, (unsigned long)mmjob->jobid);
  392. ++chain->next_jobid;
  393. struct avalonmm_job **jobentry = &chain->jobs[mmjob->jobid % AVALONMM_CACHED_JOBS];
  394. if (*jobentry)
  395. avalonmm_free_job(*jobentry);
  396. *jobentry = mmjob;
  397. return true;
  398. }
  399. static
  400. struct cgpu_info *avalonmm_dev_for_module_id(struct cgpu_info * const master_dev, const uint32_t module_id)
  401. {
  402. struct cgpu_info *dev = NULL;
  403. for_each_managed_proc(proc, master_dev)
  404. {
  405. if (dev == proc->device)
  406. continue;
  407. dev = proc->device;
  408. struct thr_info * const thr = dev->thr[0];
  409. struct avalonmm_module_state * const module = thr->cgpu_data;
  410. if (module->module_id == module_id)
  411. return dev;
  412. }
  413. return NULL;
  414. }
  415. static
  416. bool avalonmm_poll_once(struct cgpu_info * const master_dev, int64_t *out_module_id)
  417. {
  418. struct avalonmm_chain_state * const chain = master_dev->device_data;
  419. const int fd = master_dev->device_fd;
  420. uint8_t buf[AVALONMM_PKT_DATA_SIZE];
  421. enum avalonmm_reply reply;
  422. *out_module_id = -1;
  423. if (avalonmm_read(fd, LOG_ERR, &reply, buf, sizeof(buf)) < 0)
  424. return false;
  425. switch (reply)
  426. {
  427. case AMR_STATUS:
  428. {
  429. const uint32_t module_id = upk_u32be(buf, AVALONMM_PKT_DATA_SIZE - 4);
  430. struct cgpu_info * const dev = avalonmm_dev_for_module_id(master_dev, module_id);
  431. if (unlikely(!dev))
  432. {
  433. struct thr_info * const master_thr = master_dev->thr[0];
  434. applog(LOG_ERR, "%s: %s for unknown module id %lu", master_dev->dev_repr, "Status", (unsigned long)module_id);
  435. inc_hw_errors_only(master_thr);
  436. break;
  437. }
  438. *out_module_id = module_id;
  439. struct thr_info * const thr = dev->thr[0];
  440. struct avalonmm_module_state * const module = thr->cgpu_data;
  441. module->temp[0] = upk_u16be(buf, 0);
  442. module->temp[1] = upk_u16be(buf, 2);
  443. module->fan [0] = upk_u16be(buf, 4);
  444. module->fan [1] = upk_u16be(buf, 6);
  445. module->clock_actual = upk_u32be(buf, 8);
  446. module->voltcfg_actual = upk_u32be(buf, 0x0c);
  447. dev->temp = max(module->temp[0], module->temp[1]);
  448. break;
  449. }
  450. case AMR_NONCE:
  451. {
  452. const int fixed_mm_xnonce2_bytes = (work2d_xnonce2sz >= 4) ? 0 : (4 - work2d_xnonce2sz);
  453. const uint8_t * const backward_xnonce2 = &buf[8 + fixed_mm_xnonce2_bytes];
  454. const uint32_t nonce = upk_u32be(buf, 0x10) - AVALONMM_NONCE_OFFSET;
  455. const uint32_t jobid = upk_u32be(buf, 0x14);
  456. const uint32_t module_id = upk_u32be(buf, AVALONMM_PKT_DATA_SIZE - 4);
  457. struct cgpu_info * const dev = avalonmm_dev_for_module_id(master_dev, module_id);
  458. if (unlikely(!dev))
  459. {
  460. struct thr_info * const master_thr = master_dev->thr[0];
  461. applog(LOG_ERR, "%s: %s for unknown module id %lu", master_dev->dev_repr, "Nonce", (unsigned long)module_id);
  462. inc_hw_errors_only(master_thr);
  463. break;
  464. }
  465. *out_module_id = module_id;
  466. struct thr_info * const thr = dev->thr[0];
  467. bool invalid_jobid = false;
  468. if (unlikely((uint32_t)(chain->next_jobid - AVALONMM_CACHED_JOBS) > chain->next_jobid))
  469. // Jobs wrap around
  470. invalid_jobid = (jobid < chain->next_jobid - AVALONMM_CACHED_JOBS && jobid >= chain->next_jobid);
  471. else
  472. invalid_jobid = (jobid < chain->next_jobid - AVALONMM_CACHED_JOBS || jobid >= chain->next_jobid);
  473. if (unlikely(invalid_jobid))
  474. {
  475. applog(LOG_ERR, "%s: Bad job id %08lx", dev->dev_repr, (unsigned long)jobid);
  476. inc_hw_errors_only(thr);
  477. break;
  478. }
  479. struct avalonmm_job * const mmjob = chain->jobs[jobid % AVALONMM_CACHED_JOBS];
  480. uint8_t xnonce2[work2d_xnonce2sz];
  481. for (int i = 0; i < work2d_xnonce2sz; ++i)
  482. xnonce2[i] = backward_xnonce2[(work2d_xnonce2sz - 1) - i];
  483. work2d_submit_nonce(thr, &mmjob->swork, &mmjob->tv_prepared, xnonce2, chain->xnonce1, nonce, mmjob->swork.ntime, NULL, mmjob->nonce_diff);
  484. hashes_done2(thr, mmjob->nonce_diff * 0x100000000, NULL);
  485. break;
  486. }
  487. }
  488. return true;
  489. }
  490. static
  491. void avalonmm_poll(struct cgpu_info * const master_dev, int n)
  492. {
  493. int64_t dummy;
  494. while (n > 0)
  495. {
  496. if (avalonmm_poll_once(master_dev, &dummy))
  497. --n;
  498. }
  499. }
  500. static
  501. struct thr_info *avalonmm_should_disable(struct cgpu_info * const master_dev)
  502. {
  503. for_each_managed_proc(proc, master_dev)
  504. {
  505. struct thr_info * const thr = proc->thr[0];
  506. if (thr->pause || proc->deven != DEV_ENABLED)
  507. return thr;
  508. }
  509. return NULL;
  510. }
  511. static
  512. void avalonmm_minerloop(struct thr_info * const master_thr)
  513. {
  514. struct cgpu_info * const master_dev = master_thr->cgpu;
  515. const int fd = master_dev->device_fd;
  516. struct pool *nextpool = current_pool(), *pool = NULL;
  517. uint8_t buf[AVALONMM_PKT_DATA_SIZE] = {0};
  518. while (likely(!master_dev->shutdown))
  519. {
  520. if (avalonmm_should_disable(master_dev))
  521. {
  522. struct thr_info *thr;
  523. while ( (thr = avalonmm_should_disable(master_dev)) )
  524. {
  525. if (!thr->_mt_disable_called)
  526. if (avalonmm_write_cmd(fd, AMC_NEW_JOB, NULL, 0))
  527. {
  528. for_each_managed_proc(proc, master_dev)
  529. {
  530. struct thr_info * const thr = proc->thr[0];
  531. mt_disable_start(thr);
  532. }
  533. }
  534. notifier_read(thr->notifier);
  535. }
  536. for_each_managed_proc(proc, master_dev)
  537. {
  538. struct thr_info * const thr = proc->thr[0];
  539. mt_disable_finish(thr);
  540. }
  541. }
  542. master_thr->work_restart = false;
  543. if (!pool_has_usable_swork(nextpool))
  544. ; // FIXME
  545. else
  546. if (avalonmm_update_swork_from_pool(master_dev, nextpool))
  547. pool = nextpool;
  548. while (likely(!(master_thr->work_restart || ((nextpool = current_pool()) != pool && pool_has_usable_swork(nextpool)) || avalonmm_should_disable(master_dev))))
  549. {
  550. cgsleep_ms(10);
  551. struct cgpu_info *dev = NULL;
  552. int n = 0;
  553. for_each_managed_proc(proc, master_dev)
  554. {
  555. if (dev == proc->device)
  556. continue;
  557. dev = proc->device;
  558. struct thr_info * const thr = dev->thr[0];
  559. struct avalonmm_module_state * const module = thr->cgpu_data;
  560. pk_u32be(buf, AVALONMM_PKT_DATA_SIZE - 4, module->module_id);
  561. avalonmm_write_cmd(fd, AMC_POLL, buf, AVALONMM_PKT_DATA_SIZE);
  562. ++n;
  563. }
  564. avalonmm_poll(master_dev, n);
  565. }
  566. }
  567. }
  568. static
  569. const char *avalonmm_set_clock(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  570. {
  571. struct cgpu_info * const dev = proc->device;
  572. struct avalonmm_chain_state * const chain = dev->device_data;
  573. const int nv = atoi(newvalue);
  574. if (nv < 0)
  575. return "Invalid clock";
  576. chain->clock_desired = nv;
  577. return NULL;
  578. }
  579. static
  580. const char *avalonmm_set_voltage(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const success)
  581. {
  582. struct cgpu_info * const dev = proc->device;
  583. struct avalonmm_chain_state * const chain = dev->device_data;
  584. const long val = atof(newvalue) * 10000;
  585. if (val < 0 || val > 15000)
  586. return "Invalid voltage value";
  587. chain->voltcfg_desired = avalonmm_voltage_config_from_dmvolts(val);
  588. return NULL;
  589. }
  590. static const struct bfg_set_device_definition avalonmm_set_device_funcs[] = {
  591. {"clock", avalonmm_set_clock, "clock frequency"},
  592. {"voltage", avalonmm_set_voltage, "voltage (0 to 1.5 volts)"},
  593. {NULL},
  594. };
  595. static
  596. struct api_data *avalonmm_api_extra_device_detail(struct cgpu_info * const proc)
  597. {
  598. struct cgpu_info * const dev = proc->device;
  599. struct avalonmm_chain_state * const chain = dev->device_data;
  600. struct thr_info * const thr = dev->thr[0];
  601. struct avalonmm_module_state * const module = thr->cgpu_data;
  602. struct api_data *root = NULL;
  603. root = api_add_uint32(root, "Module Id", &module->module_id, false);
  604. root = api_add_uint32(root, "ExtraNonce1", &chain->xnonce1, false);
  605. return root;
  606. }
  607. static
  608. struct api_data *avalonmm_api_extra_device_status(struct cgpu_info * const proc)
  609. {
  610. struct cgpu_info * const dev = proc->device;
  611. struct thr_info * const thr = dev->thr[0];
  612. struct avalonmm_module_state * const module = thr->cgpu_data;
  613. struct api_data *root = NULL;
  614. char buf[0x10];
  615. strcpy(buf, "Temperature");
  616. for (int i = 0; i < 2; ++i)
  617. {
  618. if (module->temp[i])
  619. {
  620. float temp = module->temp[i];
  621. buf[0xb] = '0' + i;
  622. root = api_add_temp(root, buf, &temp, true);
  623. }
  624. }
  625. strcpy(buf, "Fan RPM ");
  626. for (int i = 0; i < 2; ++i)
  627. {
  628. if (module->fan[i])
  629. {
  630. buf[8] = '0' + i;
  631. root = api_add_uint16(root, buf, &module->fan[i], false);
  632. }
  633. }
  634. if (module->clock_actual)
  635. {
  636. double freq = module->clock_actual;
  637. root = api_add_freq(root, "Frequency", &freq, true);
  638. }
  639. if (module->voltcfg_actual)
  640. {
  641. float volts = avalonmm_dmvolts_from_voltage_config(module->voltcfg_actual);
  642. volts /= 10000;
  643. root = api_add_volts(root, "Voltage", &volts, true);
  644. }
  645. return root;
  646. }
  647. #ifdef HAVE_CURSES
  648. static
  649. void avalonmm_wlogprint_status(struct cgpu_info * const proc)
  650. {
  651. struct cgpu_info * const dev = proc->device;
  652. struct avalonmm_chain_state * const chain = dev->device_data;
  653. struct thr_info * const thr = dev->thr[0];
  654. struct avalonmm_module_state * const module = thr->cgpu_data;
  655. bool flag;
  656. wlogprint("ExtraNonce1:%0*lx ModuleId:%lu\n", work2d_xnonce1sz * 2, (unsigned long)chain->xnonce1, (unsigned long)module->module_id);
  657. flag = false;
  658. if (module->temp[0] && module->temp[1])
  659. {
  660. flag = true;
  661. wlogprint("Temperatures: %uC %uC", (unsigned)module->temp[0], (unsigned)module->temp[1]);
  662. if (module->fan[0] || module->fan[1])
  663. wlogprint(" ");
  664. }
  665. if (module->fan[0])
  666. {
  667. flag = true;
  668. if (module->fan[1])
  669. wlogprint("Fans: %u RPM %u RPM", (unsigned)module->fan[0], (unsigned)module->fan[1]);
  670. else
  671. wlogprint("Fan: %u RPM", (unsigned)module->fan[0]);
  672. }
  673. else
  674. if (module->fan[1])
  675. {
  676. flag = true;
  677. wlogprint("Fan: %u RPM", (unsigned)module->fan[1]);
  678. }
  679. if (flag)
  680. wlogprint("\n");
  681. if (module->clock_actual)
  682. wlogprint("Clock speed: %lu\n", (unsigned long)module->clock_actual);
  683. if (module->voltcfg_actual)
  684. {
  685. const uint32_t dmvolts = avalonmm_dmvolts_from_voltage_config(module->voltcfg_actual);
  686. wlogprint("Voltage: %u.%04u V\n", (unsigned)(dmvolts / 10000), (unsigned)(dmvolts % 10000));
  687. }
  688. }
  689. static
  690. void avalonmm_tui_wlogprint_choices(struct cgpu_info * const proc)
  691. {
  692. wlogprint("[C]lock speed ");
  693. //wlogprint("[F]an speed "); // To be implemented
  694. wlogprint("[V]oltage ");
  695. }
  696. static
  697. const char *avalonmm_tui_wrapper(struct cgpu_info * const proc, bfg_set_device_func_t func, const char * const prompt)
  698. {
  699. static char replybuf[0x20];
  700. char * const cvar = curses_input(prompt);
  701. if (!cvar)
  702. return "Cancelled\n";
  703. const char *reply = func(proc, NULL, cvar, NULL, NULL);
  704. free(cvar);
  705. if (reply)
  706. {
  707. snprintf(replybuf, sizeof(replybuf), "%s\n", reply);
  708. return replybuf;
  709. }
  710. return "Successful\n";
  711. }
  712. static
  713. const char *avalonmm_tui_handle_choice(struct cgpu_info * const proc, const int input)
  714. {
  715. switch (input)
  716. {
  717. case 'c': case 'C':
  718. return avalonmm_tui_wrapper(proc, avalonmm_set_clock , "Set clock speed (Avalon2: 1500; Avalon3: 450)");
  719. case 'v': case 'V':
  720. return avalonmm_tui_wrapper(proc, avalonmm_set_voltage, "Set voltage (Avalon2: 1.0; Avalon3: 0.6625)");
  721. }
  722. return NULL;
  723. }
  724. #endif
  725. struct device_drv avalonmm_drv = {
  726. .dname = "avalonmm",
  727. .name = "AVM",
  728. .lowl_probe = avalonmm_lowl_probe,
  729. .thread_init = avalonmm_init,
  730. .minerloop = avalonmm_minerloop,
  731. .get_api_extra_device_detail = avalonmm_api_extra_device_detail,
  732. .get_api_extra_device_status = avalonmm_api_extra_device_status,
  733. #ifdef HAVE_CURSES
  734. .proc_wlogprint_status = avalonmm_wlogprint_status,
  735. .proc_tui_wlogprint_choices = avalonmm_tui_wlogprint_choices,
  736. .proc_tui_handle_choice = avalonmm_tui_handle_choice,
  737. #endif
  738. };