driver-avalonmm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. unsigned module_id;
  205. uint16_t temp[2];
  206. uint32_t clock_actual;
  207. uint32_t voltcfg_actual;
  208. };
  209. static
  210. uint16_t avalonmm_voltage_config_from_dmvolts(uint32_t dmvolts)
  211. {
  212. return ((uint16_t)bitflip8((0x78 - dmvolts / 125) << 1 | 1)) << 8;
  213. }
  214. // Potentially lossy!
  215. static
  216. uint32_t avalonmm_dmvolts_from_voltage_config(uint32_t voltcfg)
  217. {
  218. return (0x78 - (bitflip8(voltcfg >> 8) >> 1)) * 125;
  219. }
  220. static struct cgpu_info *avalonmm_dev_for_module_id(struct cgpu_info *, uint32_t);
  221. static bool avalonmm_poll_once(struct cgpu_info *, int64_t *);
  222. static
  223. bool avalonmm_init(struct thr_info * const master_thr)
  224. {
  225. struct cgpu_info * const master_dev = master_thr->cgpu, *dev = NULL;
  226. const char * const devpath = master_dev->device_path;
  227. const int fd = serial_open(devpath, 115200, 1, true);
  228. uint8_t buf[AVALONMM_PKT_DATA_SIZE] = {0};
  229. int64_t module_id;
  230. master_dev->device_fd = fd;
  231. if (unlikely(fd == -1))
  232. applogr(false, LOG_ERR, "%s: Failed to initialise", master_dev->dev_repr);
  233. struct avalonmm_chain_state * const chain = malloc(sizeof(*chain));
  234. *chain = (struct avalonmm_chain_state){
  235. .voltcfg_desired = avalonmm_voltage_config_from_dmvolts(6625),
  236. };
  237. work2d_init();
  238. if (!reserve_work2d_(&chain->xnonce1))
  239. {
  240. applog(LOG_ERR, "%s: Failed to reserve 2D work", master_dev->dev_repr);
  241. free(chain);
  242. serial_close(fd);
  243. return false;
  244. }
  245. for_each_managed_proc(proc, master_dev)
  246. {
  247. if (dev == proc->device)
  248. continue;
  249. dev = proc->device;
  250. struct thr_info * const thr = proc->thr[0];
  251. struct avalonmm_module_state * const module = malloc(sizeof(*module));
  252. *module = (struct avalonmm_module_state){
  253. .module_id = (intptr_t)dev->device_data,
  254. };
  255. proc->device_data = chain;
  256. thr->cgpu_data = module;
  257. }
  258. dev = NULL;
  259. for_each_managed_proc(proc, master_dev)
  260. {
  261. cgpu_set_defaults(proc);
  262. proc->status = LIFE_INIT2;
  263. }
  264. if (!chain->clock_desired)
  265. {
  266. // Get a reasonable default frequency
  267. dev = master_dev;
  268. struct thr_info * const thr = dev->thr[0];
  269. struct avalonmm_module_state * const module = thr->cgpu_data;
  270. resend:
  271. pk_u32be(buf, AVALONMM_PKT_DATA_SIZE - 4, module->module_id);
  272. avalonmm_write_cmd(fd, AMC_POLL, buf, AVALONMM_PKT_DATA_SIZE);
  273. while (avalonmm_poll_once(master_dev, &module_id))
  274. {
  275. if (module_id != module->module_id)
  276. continue;
  277. if (module->clock_actual)
  278. {
  279. chain->clock_desired = module->clock_actual;
  280. break;
  281. }
  282. else
  283. goto resend;
  284. }
  285. }
  286. if (likely(chain->clock_desired))
  287. applog(LOG_DEBUG, "%s: Frequency is initialised with %d MHz", master_dev->dev_repr, chain->clock_desired);
  288. else
  289. applogr(false, LOG_ERR, "%s: No frequency detected, please use --set %s@%s:clock=MHZ", master_dev->dev_repr, master_dev->drv->dname, devpath);
  290. return true;
  291. }
  292. static
  293. 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)
  294. {
  295. uint8_t buf[AVALONMM_PKT_DATA_SIZE];
  296. bytes_t coinbase = BYTES_INIT;
  297. int coinbase_len = bytes_len(&swork->coinbase);
  298. if (coinbase_len > AVALONMM_MAX_COINBASE_SIZE)
  299. return false;
  300. if (swork->merkles > AVALONMM_MAX_MERKLES)
  301. return false;
  302. pk_u32be(buf, 0, coinbase_len);
  303. const size_t xnonce2_offset = swork->nonce2_offset + work2d_pad_xnonce_size(swork) + work2d_xnonce1sz;
  304. pk_u32be(buf, 4, xnonce2_offset);
  305. pk_u32be(buf, 8, 4); // extranonce2 size, but only 4 is supported - smaller sizes are handled by limiting the range
  306. pk_u32be(buf, 0x0c, 0x24); // merkle_offset, always 0x24 for Bitcoin
  307. pk_u32be(buf, 0x10, swork->merkles);
  308. pk_u32be(buf, 0x14, 1); // diff? poorly defined
  309. pk_u32be(buf, 0x18, 0); // pool number - none of its business
  310. if (!avalonmm_write_cmd(fd, AMC_NEW_JOB, buf, 0x1c))
  311. return false;
  312. double nonce_diff = target_diff(swork->target);
  313. if (nonce_diff >= AVALONMM_MAX_NONCE_DIFF)
  314. set_target_to_pdiff(buf, nonce_diff = AVALONMM_MAX_NONCE_DIFF);
  315. else
  316. memcpy(buf, swork->target, 0x20);
  317. *out_nonce_diff = nonce_diff;
  318. if (!avalonmm_write_cmd(fd, AMC_TARGET, buf, 0x20))
  319. return false;
  320. pk_u32be(buf, 0, jobid);
  321. if (!avalonmm_write_cmd(fd, AMC_JOB_ID, buf, 4))
  322. return false;
  323. // Need to add extranonce padding and extranonce2
  324. bytes_cpy(&coinbase, &swork->coinbase);
  325. uint8_t *cbp = bytes_buf(&coinbase);
  326. cbp += swork->nonce2_offset;
  327. work2d_pad_xnonce(cbp, swork, false);
  328. cbp += work2d_pad_xnonce_size(swork);
  329. memcpy(cbp, &chain->xnonce1, work2d_xnonce1sz);
  330. cbp += work2d_xnonce1sz;
  331. if (!avalonmm_write_cmd(fd, AMC_COINBASE, bytes_buf(&coinbase), bytes_len(&coinbase)))
  332. return false;
  333. if (!avalonmm_write_cmd(fd, AMC_MERKLES, bytes_buf(&swork->merkle_bin), bytes_len(&swork->merkle_bin)))
  334. return false;
  335. uint8_t header_bin[0x80];
  336. memcpy(&header_bin[ 0], swork->header1, 0x24);
  337. memset(&header_bin[0x24], '\0', 0x20); // merkle root
  338. pk_u32be(header_bin, 0x44, swork->ntime);
  339. memcpy(&header_bin[0x48], swork->diffbits, 4);
  340. memset(&header_bin[0x4c], '\0', 4); // nonce
  341. memcpy(&header_bin[0x50], bfg_workpadding_bin, 0x30);
  342. if (!avalonmm_write_cmd(fd, AMC_BLKHDR, header_bin, sizeof(header_bin)))
  343. return false;
  344. // 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
  345. const int fixed_mm_xnonce2_bytes = (work2d_xnonce2sz >= 4) ? 0 : (4 - work2d_xnonce2sz);
  346. uint8_t mm_xnonce2_start[4];
  347. uint32_t xnonce2_range;
  348. memset(mm_xnonce2_start, '\0', 4);
  349. cbp += work2d_xnonce2sz;
  350. for (int i = 1; i <= fixed_mm_xnonce2_bytes; ++i)
  351. mm_xnonce2_start[fixed_mm_xnonce2_bytes - i] = cbp++[0];
  352. if (fixed_mm_xnonce2_bytes > 0)
  353. xnonce2_range = (1 << (8 * work2d_xnonce2sz)) - 1;
  354. else
  355. xnonce2_range = 0xffffffff;
  356. pk_u32be(buf, 0, 80); // fan speed %
  357. pk_u32be(buf, 4, chain->voltcfg_desired);
  358. pk_u32be(buf, 8, chain->clock_desired);
  359. memcpy(&buf[0xc], mm_xnonce2_start, 4);
  360. pk_u32be(buf, 0x10, xnonce2_range);
  361. if (!avalonmm_write_cmd(fd, AMC_START, buf, 0x14))
  362. return false;
  363. return true;
  364. }
  365. static
  366. void avalonmm_free_job(struct avalonmm_job * const mmjob)
  367. {
  368. stratum_work_clean(&mmjob->swork);
  369. free(mmjob);
  370. }
  371. static
  372. bool avalonmm_update_swork_from_pool(struct cgpu_info * const master_dev, struct pool * const pool)
  373. {
  374. struct avalonmm_chain_state * const chain = master_dev->device_data;
  375. const int fd = master_dev->device_fd;
  376. struct avalonmm_job *mmjob = malloc(sizeof(*mmjob));
  377. *mmjob = (struct avalonmm_job){
  378. .jobid = chain->next_jobid,
  379. };
  380. cg_rlock(&pool->data_lock);
  381. stratum_work_cpy(&mmjob->swork, &pool->swork);
  382. cg_runlock(&pool->data_lock);
  383. timer_set_now(&mmjob->tv_prepared);
  384. mmjob->swork.data_lock_p = NULL;
  385. if (!avalonmm_send_swork(fd, chain, &mmjob->swork, mmjob->jobid, &mmjob->nonce_diff))
  386. {
  387. avalonmm_free_job(mmjob);
  388. return false;
  389. }
  390. applog(LOG_DEBUG, "%s: Upload of job id %08lx complete", master_dev->dev_repr, (unsigned long)mmjob->jobid);
  391. ++chain->next_jobid;
  392. struct avalonmm_job **jobentry = &chain->jobs[mmjob->jobid % AVALONMM_CACHED_JOBS];
  393. if (*jobentry)
  394. avalonmm_free_job(*jobentry);
  395. *jobentry = mmjob;
  396. return true;
  397. }
  398. static
  399. struct cgpu_info *avalonmm_dev_for_module_id(struct cgpu_info * const master_dev, const uint32_t module_id)
  400. {
  401. struct cgpu_info *dev = NULL;
  402. for_each_managed_proc(proc, master_dev)
  403. {
  404. if (dev == proc->device)
  405. continue;
  406. dev = proc->device;
  407. struct thr_info * const thr = dev->thr[0];
  408. struct avalonmm_module_state * const module = thr->cgpu_data;
  409. if (module->module_id == module_id)
  410. return dev;
  411. }
  412. return NULL;
  413. }
  414. static
  415. bool avalonmm_poll_once(struct cgpu_info * const master_dev, int64_t *out_module_id)
  416. {
  417. struct avalonmm_chain_state * const chain = master_dev->device_data;
  418. const int fd = master_dev->device_fd;
  419. uint8_t buf[AVALONMM_PKT_DATA_SIZE];
  420. enum avalonmm_reply reply;
  421. *out_module_id = -1;
  422. if (avalonmm_read(fd, LOG_ERR, &reply, buf, sizeof(buf)) < 0)
  423. return false;
  424. switch (reply)
  425. {
  426. case AMR_STATUS:
  427. {
  428. const uint32_t module_id = upk_u32be(buf, AVALONMM_PKT_DATA_SIZE - 4);
  429. struct cgpu_info * const dev = avalonmm_dev_for_module_id(master_dev, module_id);
  430. if (unlikely(!dev))
  431. {
  432. struct thr_info * const master_thr = master_dev->thr[0];
  433. applog(LOG_ERR, "%s: %s for unknown module id %lu", master_dev->dev_repr, "Status", (unsigned long)module_id);
  434. inc_hw_errors_only(master_thr);
  435. break;
  436. }
  437. *out_module_id = module_id;
  438. struct thr_info * const thr = dev->thr[0];
  439. struct avalonmm_module_state * const module = thr->cgpu_data;
  440. module->temp[0] = upk_u16be(buf, 0);
  441. module->temp[1] = upk_u16be(buf, 2);
  442. #if 0
  443. module->fan [0] = upk_u16be(buf, 4);
  444. module->fan [1] = upk_u16be(buf, 6);
  445. #endif
  446. module->clock_actual = upk_u32be(buf, 8);
  447. module->voltcfg_actual = upk_u32be(buf, 0x0c);
  448. dev->temp = max(module->temp[0], module->temp[1]);
  449. break;
  450. }
  451. case AMR_NONCE:
  452. {
  453. const int fixed_mm_xnonce2_bytes = (work2d_xnonce2sz >= 4) ? 0 : (4 - work2d_xnonce2sz);
  454. const uint8_t * const backward_xnonce2 = &buf[8 + fixed_mm_xnonce2_bytes];
  455. const uint32_t nonce = upk_u32be(buf, 0x10) - AVALONMM_NONCE_OFFSET;
  456. const uint32_t jobid = upk_u32be(buf, 0x14);
  457. const uint32_t module_id = upk_u32be(buf, AVALONMM_PKT_DATA_SIZE - 4);
  458. struct cgpu_info * const dev = avalonmm_dev_for_module_id(master_dev, module_id);
  459. if (unlikely(!dev))
  460. {
  461. struct thr_info * const master_thr = master_dev->thr[0];
  462. applog(LOG_ERR, "%s: %s for unknown module id %lu", master_dev->dev_repr, "Nonce", (unsigned long)module_id);
  463. inc_hw_errors_only(master_thr);
  464. break;
  465. }
  466. *out_module_id = module_id;
  467. struct thr_info * const thr = dev->thr[0];
  468. bool invalid_jobid = false;
  469. if (unlikely((uint32_t)(chain->next_jobid - AVALONMM_CACHED_JOBS) > chain->next_jobid))
  470. // Jobs wrap around
  471. invalid_jobid = (jobid < chain->next_jobid - AVALONMM_CACHED_JOBS && jobid >= chain->next_jobid);
  472. else
  473. invalid_jobid = (jobid < chain->next_jobid - AVALONMM_CACHED_JOBS || jobid >= chain->next_jobid);
  474. if (unlikely(invalid_jobid))
  475. {
  476. applog(LOG_ERR, "%s: Bad job id %08lx", dev->dev_repr, (unsigned long)jobid);
  477. inc_hw_errors_only(thr);
  478. break;
  479. }
  480. struct avalonmm_job * const mmjob = chain->jobs[jobid % AVALONMM_CACHED_JOBS];
  481. uint8_t xnonce2[work2d_xnonce2sz];
  482. for (int i = 0; i < work2d_xnonce2sz; ++i)
  483. xnonce2[i] = backward_xnonce2[(work2d_xnonce2sz - 1) - i];
  484. work2d_submit_nonce(thr, &mmjob->swork, &mmjob->tv_prepared, xnonce2, chain->xnonce1, nonce, mmjob->swork.ntime, NULL, mmjob->nonce_diff);
  485. hashes_done2(thr, mmjob->nonce_diff * 0x100000000, NULL);
  486. break;
  487. }
  488. }
  489. return true;
  490. }
  491. static
  492. void avalonmm_poll(struct cgpu_info * const master_dev, int n)
  493. {
  494. int64_t dummy;
  495. while (n > 0)
  496. {
  497. if (avalonmm_poll_once(master_dev, &dummy))
  498. --n;
  499. }
  500. }
  501. static
  502. void avalonmm_minerloop(struct thr_info * const master_thr)
  503. {
  504. struct cgpu_info * const master_dev = master_thr->cgpu;
  505. const int fd = master_dev->device_fd;
  506. struct pool *nextpool = current_pool(), *pool = NULL;
  507. uint8_t buf[AVALONMM_PKT_DATA_SIZE] = {0};
  508. while (likely(!master_dev->shutdown))
  509. {
  510. master_thr->work_restart = false;
  511. if (!pool_has_usable_swork(nextpool))
  512. ; // FIXME
  513. else
  514. if (avalonmm_update_swork_from_pool(master_dev, nextpool))
  515. pool = nextpool;
  516. while (likely(!(master_thr->work_restart || ((nextpool = current_pool()) != pool && pool_has_usable_swork(nextpool)))))
  517. {
  518. cgsleep_ms(10);
  519. struct cgpu_info *dev = NULL;
  520. int n = 0;
  521. for_each_managed_proc(proc, master_dev)
  522. {
  523. if (dev == proc->device)
  524. continue;
  525. dev = proc->device;
  526. struct thr_info * const thr = dev->thr[0];
  527. struct avalonmm_module_state * const module = thr->cgpu_data;
  528. pk_u32be(buf, AVALONMM_PKT_DATA_SIZE - 4, module->module_id);
  529. avalonmm_write_cmd(fd, AMC_POLL, buf, AVALONMM_PKT_DATA_SIZE);
  530. ++n;
  531. }
  532. avalonmm_poll(master_dev, n);
  533. }
  534. }
  535. }
  536. static
  537. 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)
  538. {
  539. struct avalonmm_chain_state * const chain = proc->device_data;
  540. const int nv = atoi(newvalue);
  541. if (nv < 0)
  542. return "Invalid clock";
  543. chain->clock_desired = nv;
  544. return NULL;
  545. }
  546. static
  547. 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)
  548. {
  549. struct avalonmm_chain_state * const chain = proc->device_data;
  550. const long val = atof(newvalue) * 10000;
  551. if (val < 0 || val > 15000)
  552. return "Invalid voltage value";
  553. chain->voltcfg_desired = avalonmm_voltage_config_from_dmvolts(val);
  554. return NULL;
  555. }
  556. static const struct bfg_set_device_definition avalonmm_set_device_funcs[] = {
  557. {"clock", avalonmm_set_clock, "clock frequency"},
  558. {"voltage", avalonmm_set_voltage, "voltage (0 to 1.5 volts)"},
  559. {NULL},
  560. };
  561. struct device_drv avalonmm_drv = {
  562. .dname = "avalonmm",
  563. .name = "AVM",
  564. .lowl_probe = avalonmm_lowl_probe,
  565. .thread_init = avalonmm_init,
  566. .minerloop = avalonmm_minerloop,
  567. };