driver-bitmain.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269
  1. /*
  2. * Copyright 2012-2013 Lingchao Xu <lingchao.xu@bitmaintech.com>
  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 <limits.h>
  11. #include <math.h>
  12. #include <pthread.h>
  13. #include <stdio.h>
  14. #include <sys/time.h>
  15. #include <sys/types.h>
  16. #include <dirent.h>
  17. #include <unistd.h>
  18. #ifndef WIN32
  19. #include <sys/select.h>
  20. #include <termios.h>
  21. #include <sys/stat.h>
  22. #include <fcntl.h>
  23. #ifndef O_CLOEXEC
  24. #define O_CLOEXEC 0
  25. #endif
  26. #else
  27. #include "compat.h"
  28. #include <windows.h>
  29. #include <io.h>
  30. #endif
  31. #include <uthash.h>
  32. #include "deviceapi.h"
  33. #include "miner.h"
  34. #include "driver-bitmain.h"
  35. #include "util.h"
  36. #warning "FIXME: Make these --set able"
  37. char * const opt_bitmain_dev = "/dev/bitmain-asic";
  38. char * const opt_bitmain_options = "115200:32:8:7:200:0782:0725";
  39. const bool opt_bitmain_hwerror = true;
  40. char * const opt_bitmain_freq = "3:350:0d82";
  41. char * const opt_bitmain_voltage = "0725";
  42. BFG_REGISTER_DRIVER(bitmain_drv)
  43. static inline unsigned int bfg_work_block(struct work * const work)
  44. {
  45. return *((unsigned int*)(&work->data[4]));
  46. }
  47. #define htole8(x) (x)
  48. struct cgpu_info *btm_alloc_cgpu(struct device_drv *drv, int threads)
  49. {
  50. struct cgpu_info *cgpu = calloc(1, sizeof(*cgpu));
  51. if (unlikely(!cgpu))
  52. quit(1, "Failed to calloc cgpu for %s in usb_alloc_cgpu", drv->dname);
  53. cgpu->drv = drv;
  54. cgpu->deven = DEV_ENABLED;
  55. cgpu->threads = threads;
  56. cgpu->device_fd = -1;
  57. return cgpu;
  58. }
  59. struct cgpu_info *btm_free_cgpu(struct cgpu_info *cgpu)
  60. {
  61. if(cgpu->device_path) {
  62. free((char*)cgpu->device_path);
  63. }
  64. free(cgpu);
  65. return NULL;
  66. }
  67. bool btm_init(struct cgpu_info *cgpu, const char * devpath)
  68. {
  69. #ifdef WIN32
  70. int fd = -1;
  71. signed short timeout = 1;
  72. unsigned long baud = 115200;
  73. bool purge = true;
  74. HANDLE hSerial = NULL;
  75. applog(LOG_DEBUG, "btm_init cgpu->device_fd=%d", cgpu->device_fd);
  76. if(cgpu->device_fd >= 0) {
  77. return false;
  78. }
  79. hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  80. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  81. {
  82. DWORD e = GetLastError();
  83. switch (e) {
  84. case ERROR_ACCESS_DENIED:
  85. applog(LOG_DEBUG, "Do not have user privileges required to open %s", devpath);
  86. break;
  87. case ERROR_SHARING_VIOLATION:
  88. applog(LOG_DEBUG, "%s is already in use by another process", devpath);
  89. break;
  90. default:
  91. applog(LOG_DEBUG, "Open %s failed, GetLastError:%d", devpath, (int)e);
  92. break;
  93. }
  94. } else {
  95. // thanks to af_newbie for pointers about this
  96. COMMCONFIG comCfg = {0};
  97. comCfg.dwSize = sizeof(COMMCONFIG);
  98. comCfg.wVersion = 1;
  99. comCfg.dcb.DCBlength = sizeof(DCB);
  100. comCfg.dcb.BaudRate = baud;
  101. comCfg.dcb.fBinary = 1;
  102. comCfg.dcb.fDtrControl = DTR_CONTROL_ENABLE;
  103. comCfg.dcb.fRtsControl = RTS_CONTROL_ENABLE;
  104. comCfg.dcb.ByteSize = 8;
  105. SetCommConfig(hSerial, &comCfg, sizeof(comCfg));
  106. // Code must specify a valid timeout value (0 means don't timeout)
  107. const DWORD ctoms = (timeout * 100);
  108. COMMTIMEOUTS cto = {ctoms, 0, ctoms, 0, ctoms};
  109. SetCommTimeouts(hSerial, &cto);
  110. if (purge) {
  111. PurgeComm(hSerial, PURGE_RXABORT);
  112. PurgeComm(hSerial, PURGE_TXABORT);
  113. PurgeComm(hSerial, PURGE_RXCLEAR);
  114. PurgeComm(hSerial, PURGE_TXCLEAR);
  115. }
  116. fd = _open_osfhandle((intptr_t)hSerial, 0);
  117. }
  118. #else
  119. int fd = -1;
  120. if(cgpu->device_fd >= 0) {
  121. return false;
  122. }
  123. fd = open(devpath, O_RDWR|O_EXCL|O_NONBLOCK);
  124. #endif
  125. if(fd == -1) {
  126. applog(LOG_DEBUG, "%s open %s error %d",
  127. cgpu->drv->dname, devpath, errno);
  128. return false;
  129. }
  130. cgpu->device_path = strdup(devpath);
  131. cgpu->device_fd = fd;
  132. applog(LOG_DEBUG, "btm_init open device fd = %d", cgpu->device_fd);
  133. return true;
  134. }
  135. void btm_uninit(struct cgpu_info *cgpu)
  136. {
  137. applog(LOG_DEBUG, "BTM uninit %s%i", cgpu->drv->name, cgpu->device_fd);
  138. // May have happened already during a failed initialisation
  139. // if release_cgpu() was called due to a USB NODEV(err)
  140. close(cgpu->device_fd);
  141. if(cgpu->device_path) {
  142. free((char*)cgpu->device_path);
  143. cgpu->device_path = NULL;
  144. }
  145. }
  146. void btm_detect(struct device_drv *drv, bool (*device_detect)(const char*))
  147. {
  148. applog(LOG_DEBUG, "BTM scan devices: checking for %s devices", drv->name);
  149. device_detect("asic");
  150. }
  151. int btm_read(struct cgpu_info * const cgpu, void * const buf, const size_t bufsize)
  152. {
  153. int err = 0;
  154. //applog(LOG_DEBUG, "btm_read ----- %d -----", bufsize);
  155. err = read(cgpu->device_fd, buf, bufsize);
  156. return err;
  157. }
  158. int btm_write(struct cgpu_info * const cgpu, void * const buf, const size_t bufsize)
  159. {
  160. int err = 0;
  161. //applog(LOG_DEBUG, "btm_write ----- %d -----", bufsize);
  162. err = write(cgpu->device_fd, buf, bufsize);
  163. return err;
  164. }
  165. #define BITMAIN_CALC_DIFF1 1
  166. #ifdef WIN32
  167. #define BITMAIN_TEST
  168. #endif
  169. #define BITMAIN_TEST_PRINT_WORK 0
  170. #ifdef BITMAIN_TEST
  171. #define BITMAIN_TEST_NUM 19
  172. #define BITMAIN_TEST_USENUM 1
  173. int g_test_index = 0;
  174. const char btm_work_test_data[BITMAIN_TEST_NUM][256] = {
  175. "00000002ddc1ce5579dbec17f17fbb8f31ae218a814b2a0c1900f0d90000000100000000b58aa6ca86546b07a5a46698f736c7ca9c0eedc756d8f28ac33c20cc24d792675276f879190afc85b6888022000000800000000000000000000000000000000000000000000000000000000000000000",
  176. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000eb2d45233c5b02de50ddcb9049ba16040e0ba00e9750a474eec75891571d925b52dfda4a190266667145b02f000000800000000000000000000000000000000000000000000000000000000000000000",
  177. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b19000000000000000090c7d3743e0b0562e4f56d3dd35cece3c5e8275d0abb21bf7e503cb72bd7ed3b52dfda4a190266667bbb58d7000000800000000000000000000000000000000000000000000000000000000000000000",
  178. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b1900000000000000006e0561da06022bfbb42c5ecd74a46bfd91934f201b777e9155cc6c3674724ec652dfda4a19026666a0cd827b000000800000000000000000000000000000000000000000000000000000000000000000",
  179. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b1900000000000000000312f42ce4964cc23f2d8c039f106f25ddd58e10a1faed21b3bba4b0e621807b52dfda4a1902666629c9497d000000800000000000000000000000000000000000000000000000000000000000000000",
  180. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b19000000000000000033093a6540dbe8f7f3d19e3d2af05585ac58dafad890fa9a942e977334a23d6e52dfda4a190266665ae95079000000800000000000000000000000000000000000000000000000000000000000000000",
  181. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000bd7893057d06e69705bddf9a89c7bac6b40c5b32f15e2295fc8c5edf491ea24952dfda4a190266664b89b4d3000000800000000000000000000000000000000000000000000000000000000000000000",
  182. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b19000000000000000075e66f533e53837d14236a793ee4e493985642bc39e016b9e63adf14a584a2aa52dfda4a19026666ab5d638d000000800000000000000000000000000000000000000000000000000000000000000000",
  183. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000d936f90c5db5f0fe1d017344443854fbf9e40a07a9b7e74fedc8661c23162bff52dfda4a19026666338e79cb000000800000000000000000000000000000000000000000000000000000000000000000",
  184. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000d2c1a7d279a4355b017bc0a4b0a9425707786729f21ee18add3fda4252a31a4152dfda4a190266669bc90806000000800000000000000000000000000000000000000000000000000000000000000000",
  185. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000ad36d19f33d04ca779942843890bc3b083cec83a4b60b6c45cf7d21fc187746552dfda4a1902666675d81ab7000000800000000000000000000000000000000000000000000000000000000000000000",
  186. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b19000000000000000093b809cf82b76082eacb55bc35b79f31882ed0976fd102ef54783cd24341319b52dfda4a1902666642ab4e42000000800000000000000000000000000000000000000000000000000000000000000000",
  187. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b1900000000000000007411ff315430a7bbf41de8a685d457e82d5177c05640d6a4436a40f39e99667852dfda4a190266662affa4b5000000800000000000000000000000000000000000000000000000000000000000000000",
  188. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b1900000000000000001ad0db5b9e1e2b57c8d3654c160f5a51067521eab7e340a270639d97f00a3fa252dfda4a1902666601a47bb6000000800000000000000000000000000000000000000000000000000000000000000000",
  189. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b19000000000000000022e055c442c46bbe16df68603a26891f6e4cf85b90102b39fd7cadb602b4e34552dfda4a1902666695d33cea000000800000000000000000000000000000000000000000000000000000000000000000",
  190. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b1900000000000000009c8baf5a8a1e16de2d6ae949d5fec3ed751f10dcd4c99810f2ce08040fb9e31d52dfda4a19026666fe78849d000000800000000000000000000000000000000000000000000000000000000000000000",
  191. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000e5655532b414887f35eb4652bc7b11ebac12891f65bc08cbe0ce5b277b9e795152dfda4a19026666fcc0d1d1000000800000000000000000000000000000000000000000000000000000000000000000",
  192. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000f272c5508704e2b62dd1c30ea970372c40bf00f9203f9bf69d456b4a7fbfffe352dfda4a19026666c03d4399000000800000000000000000000000000000000000000000000000000000000000000000",
  193. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000fca3b4531ba627ad9b0e23cdd84c888952c23810df196e9c6db0bcecba6a830952dfda4a19026666c14009cb000000800000000000000000000000000000000000000000000000000000000000000000"
  194. };
  195. const char btm_work_test_midstate[BITMAIN_TEST_NUM][256] = {
  196. "2d8738e7f5bcf76dcb8316fec772e20e240cd58c88d47f2d3f5a6a9547ed0a35",
  197. "d31b6ce09c0bfc2af6f3fe3a03475ebefa5aa191fa70a327a354b2c22f9692f1",
  198. "84a8c8224b80d36caeb42eff2a100f634e1ff873e83fd02ef1306a34abef9dbe",
  199. "059882159439b9b32968c79a93c5521e769dbea9d840f56c2a17b9ad87e530b8",
  200. "17fa435d05012574f8f1da26994cc87b6cb9660b5e82072dc6a0881cec150a0d",
  201. "92a28cc5ec4ba6a2688471dfe2032b5fe97c805ca286c503e447d6749796c6af",
  202. "1677a03516d6e9509ac37e273d2482da9af6e077abe8392cdca6a30e916a7ae9",
  203. "50bbe09f1b8ac18c97aeb745d5d2c3b5d669b6ac7803e646f65ac7b763a392d1",
  204. "e46a0022ebdc303a7fb1a0ebfa82b523946c312e745e5b8a116b17ae6b4ce981",
  205. "8f2f61e7f5b4d76d854e6d266acfff4d40347548216838ccc4ef3b9e43d3c9ea",
  206. "0a450588ae99f75d676a08d0326e1ea874a3497f696722c78a80c7b6ee961ea6",
  207. "3c4c0fc2cf040b806c51b46de9ec0dcc678a7cc5cf3eff11c6c03de3bc7818cc",
  208. "f6c7c785ab5daddb8f98e5f854f2cb41879fcaf47289eb2b4196fefc1b28316f",
  209. "005312351ccb0d0794779f5023e4335b5cad221accf0dfa3da7b881266fa9f5a",
  210. "7b26d189c6bba7add54143179aadbba7ccaeff6887bd8d5bec9597d5716126e6",
  211. "a4718f4c801e7ddf913a9474eb71774993525684ffea1915f767ab16e05e6889",
  212. "6b6226a8c18919d0e55684638d33a6892a00d22492cc2f5906ca7a4ac21c74a7",
  213. "383114dccd1cb824b869158aa2984d157fcb02f46234ceca65943e919329e697",
  214. "d4d478df3016852b27cb1ae9e1e98d98617f8d0943bf9dc1217f47f817236222"
  215. };
  216. #endif
  217. bool opt_bitmain_checkall = false;
  218. bool opt_bitmain_nobeeper = false;
  219. bool opt_bitmain_notempoverctrl = false;
  220. bool opt_bitmain_homemode = false;
  221. int opt_bitmain_temp = BITMAIN_TEMP_TARGET;
  222. int opt_bitmain_overheat = BITMAIN_TEMP_OVERHEAT;
  223. int opt_bitmain_fan_min = BITMAIN_DEFAULT_FAN_MIN_PWM;
  224. int opt_bitmain_fan_max = BITMAIN_DEFAULT_FAN_MAX_PWM;
  225. int opt_bitmain_freq_min = BITMAIN_MIN_FREQUENCY;
  226. int opt_bitmain_freq_max = BITMAIN_MAX_FREQUENCY;
  227. bool opt_bitmain_auto;
  228. static int option_offset = -1;
  229. // --------------------------------------------------------------
  230. // CRC16 check table
  231. // --------------------------------------------------------------
  232. const uint8_t chCRCHTalbe[] = // CRC high byte table
  233. {
  234. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  235. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  236. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  237. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  238. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  239. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  240. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  241. 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  242. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  243. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  244. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  245. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  246. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  247. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  248. 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  249. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  250. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  251. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  252. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  253. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  254. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  255. 0x00, 0xC1, 0x81, 0x40
  256. };
  257. const uint8_t chCRCLTalbe[] = // CRC low byte table
  258. {
  259. 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
  260. 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
  261. 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9,
  262. 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
  263. 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
  264. 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32,
  265. 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D,
  266. 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38,
  267. 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF,
  268. 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
  269. 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1,
  270. 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4,
  271. 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB,
  272. 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA,
  273. 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
  274. 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
  275. 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97,
  276. 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E,
  277. 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89,
  278. 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
  279. 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
  280. 0x41, 0x81, 0x80, 0x40
  281. };
  282. static uint16_t CRC16(const uint8_t* p_data, uint16_t w_len)
  283. {
  284. uint8_t chCRCHi = 0xFF; // CRC high byte initialize
  285. uint8_t chCRCLo = 0xFF; // CRC low byte initialize
  286. uint16_t wIndex = 0; // CRC cycling index
  287. while (w_len--) {
  288. wIndex = chCRCLo ^ *p_data++;
  289. chCRCLo = chCRCHi ^ chCRCHTalbe[wIndex];
  290. chCRCHi = chCRCLTalbe[wIndex];
  291. }
  292. return ((chCRCHi << 8) | chCRCLo);
  293. }
  294. static uint32_t num2bit(int num) {
  295. switch(num) {
  296. case 0: return 0x80000000;
  297. case 1: return 0x40000000;
  298. case 2: return 0x20000000;
  299. case 3: return 0x10000000;
  300. case 4: return 0x08000000;
  301. case 5: return 0x04000000;
  302. case 6: return 0x02000000;
  303. case 7: return 0x01000000;
  304. case 8: return 0x00800000;
  305. case 9: return 0x00400000;
  306. case 10: return 0x00200000;
  307. case 11: return 0x00100000;
  308. case 12: return 0x00080000;
  309. case 13: return 0x00040000;
  310. case 14: return 0x00020000;
  311. case 15: return 0x00010000;
  312. case 16: return 0x00008000;
  313. case 17: return 0x00004000;
  314. case 18: return 0x00002000;
  315. case 19: return 0x00001000;
  316. case 20: return 0x00000800;
  317. case 21: return 0x00000400;
  318. case 22: return 0x00000200;
  319. case 23: return 0x00000100;
  320. case 24: return 0x00000080;
  321. case 25: return 0x00000040;
  322. case 26: return 0x00000020;
  323. case 27: return 0x00000010;
  324. case 28: return 0x00000008;
  325. case 29: return 0x00000004;
  326. case 30: return 0x00000002;
  327. case 31: return 0x00000001;
  328. default: return 0x00000000;
  329. }
  330. }
  331. static bool get_options(int this_option_offset, int *baud, int *chain_num,
  332. int *asic_num, int *timeout, int *frequency, char * frequency_t, uint8_t * reg_data, uint8_t * voltage, char * voltage_t)
  333. {
  334. char buf[BUFSIZ+1];
  335. char *ptr, *comma, *colon, *colon2, *colon3, *colon4, *colon5, *colon6;
  336. size_t max;
  337. int i, tmp;
  338. if (opt_bitmain_options == NULL)
  339. buf[0] = '\0';
  340. else {
  341. ptr = opt_bitmain_options;
  342. for (i = 0; i < this_option_offset; i++) {
  343. comma = strchr(ptr, ',');
  344. if (comma == NULL)
  345. break;
  346. ptr = comma + 1;
  347. }
  348. comma = strchr(ptr, ',');
  349. if (comma == NULL)
  350. max = strlen(ptr);
  351. else
  352. max = comma - ptr;
  353. if (max > BUFSIZ)
  354. max = BUFSIZ;
  355. strncpy(buf, ptr, max);
  356. buf[max] = '\0';
  357. }
  358. if (!(*buf))
  359. return false;
  360. colon = strchr(buf, ':');
  361. if (colon)
  362. *(colon++) = '\0';
  363. tmp = atoi(buf);
  364. switch (tmp) {
  365. case 115200:
  366. *baud = 115200;
  367. break;
  368. case 57600:
  369. *baud = 57600;
  370. break;
  371. case 38400:
  372. *baud = 38400;
  373. break;
  374. case 19200:
  375. *baud = 19200;
  376. break;
  377. default:
  378. quit(1, "Invalid bitmain-options for baud (%s) "
  379. "must be 115200, 57600, 38400 or 19200", buf);
  380. }
  381. if (colon && *colon) {
  382. colon2 = strchr(colon, ':');
  383. if (colon2)
  384. *(colon2++) = '\0';
  385. if (*colon) {
  386. tmp = atoi(colon);
  387. if (tmp > 0) {
  388. *chain_num = tmp;
  389. } else {
  390. quit(1, "Invalid bitmain-options for "
  391. "chain_num (%s) must be 1 ~ %d",
  392. colon, BITMAIN_DEFAULT_CHAIN_NUM);
  393. }
  394. }
  395. if (colon2 && *colon2) {
  396. colon3 = strchr(colon2, ':');
  397. if (colon3)
  398. *(colon3++) = '\0';
  399. tmp = atoi(colon2);
  400. if (tmp > 0 && tmp <= BITMAIN_DEFAULT_ASIC_NUM)
  401. *asic_num = tmp;
  402. else {
  403. quit(1, "Invalid bitmain-options for "
  404. "asic_num (%s) must be 1 ~ %d",
  405. colon2, BITMAIN_DEFAULT_ASIC_NUM);
  406. }
  407. if (colon3 && *colon3) {
  408. colon4 = strchr(colon3, ':');
  409. if (colon4)
  410. *(colon4++) = '\0';
  411. tmp = atoi(colon3);
  412. if (tmp > 0 && tmp <= 0xff)
  413. *timeout = tmp;
  414. else {
  415. quit(1, "Invalid bitmain-options for "
  416. "timeout (%s) must be 1 ~ %d",
  417. colon3, 0xff);
  418. }
  419. if (colon4 && *colon4) {
  420. colon5 = strchr(colon4, ':');
  421. if(colon5)
  422. *(colon5++) = '\0';
  423. tmp = atoi(colon4);
  424. if (tmp < BITMAIN_MIN_FREQUENCY || tmp > BITMAIN_MAX_FREQUENCY) {
  425. quit(1, "Invalid bitmain-options for frequency, must be %d <= frequency <= %d",
  426. BITMAIN_MIN_FREQUENCY, BITMAIN_MAX_FREQUENCY);
  427. } else {
  428. *frequency = tmp;
  429. strcpy(frequency_t, colon4);
  430. }
  431. if (colon5 && *colon5) {
  432. colon6 = strchr(colon5, ':');
  433. if(colon6)
  434. *(colon6++) = '\0';
  435. if(strlen(colon5) > 8 || strlen(colon5)%2 != 0 || strlen(colon5)/2 == 0) {
  436. quit(1, "Invalid bitmain-options for reg data, must be hex now: %s",
  437. colon5);
  438. }
  439. memset(reg_data, 0, 4);
  440. if(!hex2bin(reg_data, colon5, strlen(colon5)/2)) {
  441. quit(1, "Invalid bitmain-options for reg data, hex2bin error now: %s",
  442. colon5);
  443. }
  444. if (colon6 && *colon6) {
  445. if(strlen(colon6) > 4 || strlen(colon6)%2 != 0 || strlen(colon6)/2 == 0) {
  446. quit(1, "Invalid bitmain-options for voltage data, must be hex now: %s",
  447. colon6);
  448. }
  449. memset(voltage, 0, 2);
  450. if(!hex2bin(voltage, colon6, strlen(colon6)/2)) {
  451. quit(1, "Invalid bitmain-options for voltage data, hex2bin error now: %s",
  452. colon5);
  453. } else {
  454. sprintf(voltage_t, "%02x%02x", voltage[0], voltage[1]);
  455. voltage_t[5] = 0;
  456. voltage_t[4] = voltage_t[3];
  457. voltage_t[3] = voltage_t[2];
  458. voltage_t[2] = voltage_t[1];
  459. voltage_t[1] = '.';
  460. }
  461. }
  462. }
  463. }
  464. }
  465. }
  466. }
  467. return true;
  468. }
  469. static bool get_option_freq(int *timeout, int *frequency, char * frequency_t, uint8_t * reg_data)
  470. {
  471. char buf[BUFSIZ+1];
  472. char *ptr, *comma, *colon, *colon2;
  473. size_t max;
  474. int tmp;
  475. if (opt_bitmain_freq == NULL)
  476. return true;
  477. else {
  478. ptr = opt_bitmain_freq;
  479. comma = strchr(ptr, ',');
  480. if (comma == NULL)
  481. max = strlen(ptr);
  482. else
  483. max = comma - ptr;
  484. if (max > BUFSIZ)
  485. max = BUFSIZ;
  486. strncpy(buf, ptr, max);
  487. buf[max] = '\0';
  488. }
  489. if (!(*buf))
  490. return false;
  491. colon = strchr(buf, ':');
  492. if (colon)
  493. *(colon++) = '\0';
  494. tmp = atoi(buf);
  495. if (tmp > 0 && tmp <= 0xff)
  496. *timeout = tmp;
  497. else {
  498. quit(1, "Invalid bitmain-freq for "
  499. "timeout (%s) must be 1 ~ %d",
  500. buf, 0xff);
  501. }
  502. if (colon && *colon) {
  503. colon2 = strchr(colon, ':');
  504. if (colon2)
  505. *(colon2++) = '\0';
  506. tmp = atoi(colon);
  507. if (tmp < BITMAIN_MIN_FREQUENCY || tmp > BITMAIN_MAX_FREQUENCY) {
  508. quit(1, "Invalid bitmain-freq for frequency, must be %d <= frequency <= %d",
  509. BITMAIN_MIN_FREQUENCY, BITMAIN_MAX_FREQUENCY);
  510. } else {
  511. *frequency = tmp;
  512. strcpy(frequency_t, colon);
  513. }
  514. if (colon2 && *colon2) {
  515. if(strlen(colon2) > 8 || strlen(colon2)%2 != 0 || strlen(colon2)/2 == 0) {
  516. quit(1, "Invalid bitmain-freq for reg data, must be hex now: %s",
  517. colon2);
  518. }
  519. memset(reg_data, 0, 4);
  520. if(!hex2bin(reg_data, colon2, strlen(colon2)/2)) {
  521. quit(1, "Invalid bitmain-freq for reg data, hex2bin error now: %s",
  522. colon2);
  523. }
  524. }
  525. }
  526. return true;
  527. }
  528. static bool get_option_voltage(uint8_t * voltage, char * voltage_t)
  529. {
  530. if(opt_bitmain_voltage) {
  531. if(strlen(opt_bitmain_voltage) > 4 || strlen(opt_bitmain_voltage)%2 != 0 || strlen(opt_bitmain_voltage)/2 == 0) {
  532. applog(LOG_ERR, "Invalid bitmain-voltage for voltage data, must be hex now: %s,set default_volttage",
  533. opt_bitmain_voltage);
  534. return false;
  535. }
  536. memset(voltage, 0, 2);
  537. if(!hex2bin(voltage, opt_bitmain_voltage, strlen(opt_bitmain_voltage)/2)) {
  538. quit(1, "Invalid bitmain-voltage for voltage data, hex2bin error now: %s",
  539. opt_bitmain_voltage);
  540. } else {
  541. sprintf(voltage_t, "%02x%02x", voltage[0], voltage[1]);
  542. voltage_t[5] = 0;
  543. voltage_t[4] = voltage_t[3];
  544. voltage_t[3] = voltage_t[2];
  545. voltage_t[2] = voltage_t[1];
  546. voltage_t[1] = '.';
  547. }
  548. }
  549. return true;
  550. }
  551. static int bitmain_set_txconfig(struct bitmain_txconfig_token *bm,
  552. uint8_t reset, uint8_t fan_eft, uint8_t timeout_eft, uint8_t frequency_eft,
  553. uint8_t voltage_eft, uint8_t chain_check_time_eft, uint8_t chip_config_eft, uint8_t hw_error_eft,
  554. uint8_t beeper_ctrl, uint8_t temp_over_ctrl,uint8_t fan_home_mode,
  555. uint8_t chain_num, uint8_t asic_num, uint8_t fan_pwm_data, uint8_t timeout_data,
  556. uint16_t frequency, uint8_t * voltage, uint8_t chain_check_time,
  557. uint8_t chip_address, uint8_t reg_address, uint8_t * reg_data)
  558. {
  559. uint16_t crc = 0;
  560. int datalen = 0;
  561. uint8_t version = 0;
  562. uint8_t * sendbuf = (uint8_t *)bm;
  563. if (unlikely(!bm)) {
  564. applog(LOG_WARNING, "bitmain_set_txconfig bitmain_txconfig_token is null");
  565. return -1;
  566. }
  567. if (unlikely(timeout_data <= 0 || asic_num <= 0 || chain_num <= 0)) {
  568. applog(LOG_WARNING, "bitmain_set_txconfig parameter invalid timeout_data(%d) asic_num(%d) chain_num(%d)",
  569. timeout_data, asic_num, chain_num);
  570. return -1;
  571. }
  572. datalen = sizeof(struct bitmain_txconfig_token);
  573. memset(bm, 0, datalen);
  574. bm->token_type = BITMAIN_TOKEN_TYPE_TXCONFIG;
  575. bm->version = version;
  576. bm->length = datalen-4;
  577. bm->length = htole16(bm->length);
  578. bm->reset = reset;
  579. bm->fan_eft = fan_eft;
  580. bm->timeout_eft = timeout_eft;
  581. bm->frequency_eft = frequency_eft;
  582. bm->voltage_eft = voltage_eft;
  583. bm->chain_check_time_eft = chain_check_time_eft;
  584. bm->chip_config_eft = chip_config_eft;
  585. bm->hw_error_eft = hw_error_eft;
  586. bm->beeper_ctrl = beeper_ctrl;
  587. bm->temp_over_ctrl = temp_over_ctrl;
  588. bm->fan_home_mode = fan_home_mode;
  589. sendbuf[4] = htole8(sendbuf[4]);
  590. sendbuf[5] = htole8(sendbuf[5]);
  591. bm->chain_num = chain_num;
  592. bm->asic_num = asic_num;
  593. bm->fan_pwm_data = fan_pwm_data;
  594. bm->timeout_data = timeout_data;
  595. bm->frequency = htole16(frequency);
  596. memcpy(bm->voltage, voltage, 2);
  597. bm->chain_check_time = chain_check_time;
  598. memcpy(bm->reg_data, reg_data, 4);
  599. bm->chip_address = chip_address;
  600. bm->reg_address = reg_address;
  601. crc = CRC16((uint8_t *)bm, datalen-2);
  602. bm->crc = htole16(crc);
  603. applog(LOG_ERR, "BTM TxConfigToken:v(%d) reset(%d) fan_e(%d) tout_e(%d) fq_e(%d) vt_e(%d) chainc_e(%d) chipc_e(%d) hw_e(%d) b_c(%d) t_c(%d) f_m(%d) mnum(%d) anum(%d) fanpwmdata(%d) toutdata(%d) freq(%d) volt(%02x%02x) chainctime(%d) regdata(%02x%02x%02x%02x) chipaddr(%02x) regaddr(%02x) crc(%04x)",
  604. version, reset, fan_eft, timeout_eft, frequency_eft, voltage_eft,
  605. chain_check_time_eft, chip_config_eft, hw_error_eft, beeper_ctrl, temp_over_ctrl,fan_home_mode,chain_num, asic_num,
  606. fan_pwm_data, timeout_data, frequency, voltage[0], voltage[1],
  607. chain_check_time, reg_data[0], reg_data[1], reg_data[2], reg_data[3], chip_address, reg_address, crc);
  608. return datalen;
  609. }
  610. static int bitmain_set_txtask(uint8_t * sendbuf,
  611. unsigned int * last_work_block, struct work **works, int work_array_size, int work_array, int sendworkcount, int * sendcount)
  612. {
  613. uint16_t crc = 0;
  614. uint32_t work_id = 0;
  615. uint8_t version = 0;
  616. int datalen = 0;
  617. int i = 0;
  618. int index = work_array;
  619. uint8_t new_block= 0;
  620. struct bitmain_txtask_token *bm = (struct bitmain_txtask_token *)sendbuf;
  621. *sendcount = 0;
  622. int cursendcount = 0;
  623. int diff = 0;
  624. unsigned int difftmp = 0;
  625. unsigned int pooldiff = 0;
  626. int netdiff = 0;
  627. if (unlikely(!bm)) {
  628. applog(LOG_WARNING, "bitmain_set_txtask bitmain_txtask_token is null");
  629. return -1;
  630. }
  631. if (unlikely(!works)) {
  632. applog(LOG_WARNING, "bitmain_set_txtask work is null");
  633. return -1;
  634. }
  635. memset(bm, 0, sizeof(struct bitmain_txtask_token));
  636. bm->token_type = BITMAIN_TOKEN_TYPE_TXTASK;
  637. bm->version = version;
  638. datalen = 10;
  639. applog(LOG_DEBUG, "BTM send work count %d -----", sendworkcount);
  640. pooldiff = 0x100;
  641. unsigned lowest_goal_diff = UINT_MAX;
  642. for (i = 0; i < sendworkcount; ++i) {
  643. if (index > work_array_size) {
  644. index = 0;
  645. }
  646. if (!works[index]) {
  647. continue;
  648. }
  649. struct work * const work = works[index];
  650. if (work->work_difficulty < pooldiff)
  651. pooldiff = work->work_difficulty;
  652. const struct pool * const pool = work->pool;
  653. const struct mining_goal_info * const goal = pool->goal;
  654. if (goal->current_diff < lowest_goal_diff)
  655. lowest_goal_diff = goal->current_diff;
  656. }
  657. {
  658. difftmp = pooldiff;
  659. while(1) {
  660. difftmp = difftmp >> 1;
  661. if(difftmp > 0) {
  662. diff++;
  663. if(diff >= 255) {
  664. break;
  665. }
  666. } else {
  667. break;
  668. }
  669. }
  670. for (uint64_t netdifftmp = lowest_goal_diff; netdifftmp > 0; netdifftmp >>= 1) {
  671. ++netdiff;
  672. }
  673. pooldiff = pow(2, diff);
  674. }
  675. applog(LOG_DEBUG, "bitmain_set_txtask using nonce_diff=%u (log2=%d) and goal_diff=%u (log2=%d)", pooldiff, diff, lowest_goal_diff, netdiff);
  676. for(i = 0; i < sendworkcount; i++) {
  677. if(index > work_array_size) {
  678. index = 0;
  679. }
  680. if(works[index]) {
  681. const unsigned int work_block = bfg_work_block(works[index]);
  682. if(work_block != *last_work_block) {
  683. applog(LOG_ERR, "BTM send task new block %d old(%d)", work_block, *last_work_block);
  684. new_block = 1;
  685. *last_work_block = work_block;
  686. }
  687. #ifdef BITMAIN_TEST
  688. if(!hex2bin(works[index]->data, btm_work_test_data[g_test_index], 128)) {
  689. applog(LOG_DEBUG, "BTM send task set test data error");
  690. }
  691. if(!hex2bin(works[index]->midstate, btm_work_test_midstate[g_test_index], 32)) {
  692. applog(LOG_DEBUG, "BTM send task set test midstate error");
  693. }
  694. g_test_index++;
  695. if(g_test_index >= BITMAIN_TEST_USENUM) {
  696. g_test_index = 0;
  697. }
  698. applog(LOG_DEBUG, "BTM test index = %d", g_test_index);
  699. #endif
  700. work_id = works[index]->id;
  701. bm->works[cursendcount].work_id = htole32(work_id);
  702. applog(LOG_DEBUG, "BTM send task work id:%d %d", bm->works[cursendcount].work_id, work_id);
  703. memcpy(bm->works[cursendcount].midstate, works[index]->midstate, 32);
  704. memcpy(bm->works[cursendcount].data2, works[index]->data + 64, 12);
  705. works[index]->nonce_diff = pooldiff;
  706. if(BITMAIN_TEST_PRINT_WORK) {
  707. char ob_hex[(76 * 2) + 1];
  708. bin2hex(ob_hex, works[index]->data, 76);
  709. applog(LOG_ERR, "work %d data: %s", works[index]->id, ob_hex);
  710. }
  711. cursendcount++;
  712. }
  713. index++;
  714. }
  715. if(cursendcount <= 0) {
  716. applog(LOG_ERR, "BTM send work count %d", cursendcount);
  717. return 0;
  718. }
  719. datalen += 48*cursendcount;
  720. bm->length = datalen-4;
  721. bm->length = htole16(bm->length);
  722. //len = datalen-3;
  723. //len = htole16(len);
  724. //memcpy(sendbuf+1, &len, 2);
  725. bm->new_block = new_block;
  726. bm->diff = diff;
  727. bm->net_diff = htole16(netdiff);
  728. sendbuf[4] = htole8(sendbuf[4]);
  729. applog(LOG_DEBUG, "BitMain TxTask Token: %d %d %02x%02x%02x%02x%02x%02x",
  730. datalen, bm->length, sendbuf[0],sendbuf[1],sendbuf[2],sendbuf[3],sendbuf[4],sendbuf[5]);
  731. *sendcount = cursendcount;
  732. crc = CRC16(sendbuf, datalen-2);
  733. crc = htole16(crc);
  734. memcpy(sendbuf+datalen-2, &crc, 2);
  735. applog(LOG_DEBUG, "BitMain TxTask Token: v(%d) new_block(%d) diff(%d pool:%d net:%d) work_num(%d) crc(%04x)",
  736. version, new_block, diff, pooldiff,netdiff, cursendcount, crc);
  737. applog(LOG_DEBUG, "BitMain TxTask Token: %d %d %02x%02x%02x%02x%02x%02x",
  738. datalen, bm->length, sendbuf[0],sendbuf[1],sendbuf[2],sendbuf[3],sendbuf[4],sendbuf[5]);
  739. return datalen;
  740. }
  741. static int bitmain_set_rxstatus(struct bitmain_rxstatus_token *bm,
  742. uint8_t chip_status_eft, uint8_t detect_get, uint8_t chip_address, uint8_t reg_address)
  743. {
  744. uint16_t crc = 0;
  745. uint8_t version = 0;
  746. int datalen = 0;
  747. uint8_t * sendbuf = (uint8_t *)bm;
  748. if (unlikely(!bm)) {
  749. applog(LOG_WARNING, "bitmain_set_rxstatus bitmain_rxstatus_token is null");
  750. return -1;
  751. }
  752. datalen = sizeof(struct bitmain_rxstatus_token);
  753. memset(bm, 0, datalen);
  754. bm->token_type = BITMAIN_TOKEN_TYPE_RXSTATUS;
  755. bm->version = version;
  756. bm->length = datalen-4;
  757. bm->length = htole16(bm->length);
  758. bm->chip_status_eft = chip_status_eft;
  759. bm->detect_get = detect_get;
  760. sendbuf[4] = htole8(sendbuf[4]);
  761. bm->chip_address = chip_address;
  762. bm->reg_address = reg_address;
  763. crc = CRC16((uint8_t *)bm, datalen-2);
  764. bm->crc = htole16(crc);
  765. applog(LOG_ERR, "BitMain RxStatus Token: v(%d) chip_status_eft(%d) detect_get(%d) chip_address(%02x) reg_address(%02x) crc(%04x)",
  766. version, chip_status_eft, detect_get, chip_address, reg_address, crc);
  767. return datalen;
  768. }
  769. static int bitmain_parse_rxstatus(const uint8_t * data, int datalen, struct bitmain_rxstatus_data *bm)
  770. {
  771. uint16_t crc = 0;
  772. uint8_t version = 0;
  773. int i = 0, j = 0;
  774. int asic_num = 0;
  775. int dataindex = 0;
  776. uint8_t tmp = 0x01;
  777. if (unlikely(!bm)) {
  778. applog(LOG_WARNING, "bitmain_parse_rxstatus bitmain_rxstatus_data is null");
  779. return -1;
  780. }
  781. if (unlikely(!data || datalen <= 0)) {
  782. applog(LOG_WARNING, "bitmain_parse_rxstatus parameter invalid data is null or datalen(%d) error", datalen);
  783. return -1;
  784. }
  785. memset(bm, 0, sizeof(struct bitmain_rxstatus_data));
  786. memcpy(bm, data, 28);
  787. if (bm->data_type != BITMAIN_DATA_TYPE_RXSTATUS) {
  788. applog(LOG_ERR, "bitmain_parse_rxstatus datatype(%02x) error", bm->data_type);
  789. return -1;
  790. }
  791. if (bm->version != version) {
  792. applog(LOG_ERR, "bitmain_parse_rxstatus version(%02x) error", bm->version);
  793. return -1;
  794. }
  795. bm->length = htole16(bm->length);
  796. if (bm->length+4 != datalen) {
  797. applog(LOG_ERR, "bitmain_parse_rxstatus length(%d) datalen(%d) error", bm->length, datalen);
  798. return -1;
  799. }
  800. crc = CRC16(data, datalen-2);
  801. memcpy(&(bm->crc), data+datalen-2, 2);
  802. bm->crc = htole16(bm->crc);
  803. if(crc != bm->crc) {
  804. applog(LOG_ERR, "bitmain_parse_rxstatus check crc(%d) != bm crc(%d) datalen(%d)", crc, bm->crc, datalen);
  805. return -1;
  806. }
  807. bm->fifo_space = htole16(bm->fifo_space);
  808. bm->fan_exist = htole16(bm->fan_exist);
  809. bm->temp_exist = htole32(bm->temp_exist);
  810. bm->nonce_error = htole32(bm->nonce_error);
  811. if(bm->chain_num > BITMAIN_MAX_CHAIN_NUM) {
  812. applog(LOG_ERR, "bitmain_parse_rxstatus chain_num=%d error", bm->chain_num);
  813. return -1;
  814. }
  815. dataindex = 28;
  816. if(bm->chain_num > 0) {
  817. memcpy(bm->chain_asic_num, data+datalen-2-bm->chain_num-bm->temp_num-bm->fan_num, bm->chain_num);
  818. }
  819. for(i = 0; i < bm->chain_num; i++) {
  820. asic_num = bm->chain_asic_num[i];
  821. if(asic_num <= 0) {
  822. asic_num = 1;
  823. } else {
  824. if(asic_num % 32 == 0) {
  825. asic_num = asic_num / 32;
  826. } else {
  827. asic_num = asic_num / 32 + 1;
  828. }
  829. }
  830. memcpy((uint8_t *)bm->chain_asic_exist+i*32, data+dataindex, asic_num*4);
  831. dataindex += asic_num*4;
  832. }
  833. for(i = 0; i < bm->chain_num; i++) {
  834. asic_num = bm->chain_asic_num[i];
  835. if(asic_num <= 0) {
  836. asic_num = 1;
  837. } else {
  838. if(asic_num % 32 == 0) {
  839. asic_num = asic_num / 32;
  840. } else {
  841. asic_num = asic_num / 32 + 1;
  842. }
  843. }
  844. memcpy((uint8_t *)bm->chain_asic_status+i*32, data+dataindex, asic_num*4);
  845. dataindex += asic_num*4;
  846. }
  847. dataindex += bm->chain_num;
  848. if(dataindex + bm->temp_num + bm->fan_num + 2 != datalen) {
  849. applog(LOG_ERR, "bitmain_parse_rxstatus dataindex(%d) chain_num(%d) temp_num(%d) fan_num(%d) not match datalen(%d)",
  850. dataindex, bm->chain_num, bm->temp_num, bm->fan_num, datalen);
  851. return -1;
  852. }
  853. for(i = 0; i < bm->chain_num; i++) {
  854. //bm->chain_asic_status[i] = swab32(bm->chain_asic_status[i]);
  855. for(j = 0; j < 8; j++) {
  856. bm->chain_asic_exist[i*8+j] = htole32(bm->chain_asic_exist[i*8+j]);
  857. bm->chain_asic_status[i*8+j] = htole32(bm->chain_asic_status[i*8+j]);
  858. }
  859. }
  860. if(bm->temp_num > 0) {
  861. memcpy(bm->temp, data+dataindex, bm->temp_num);
  862. dataindex += bm->temp_num;
  863. }
  864. if(bm->fan_num > 0) {
  865. memcpy(bm->fan, data+dataindex, bm->fan_num);
  866. dataindex += bm->fan_num;
  867. }
  868. if(!opt_bitmain_checkall){
  869. if(tmp != htole8(tmp)){
  870. applog(LOG_ERR, "BitMain RxStatus byte4 0x%02x chip_value_eft %d reserved %d get_blk_num %d ",*((uint8_t* )bm +4),bm->chip_value_eft,bm->reserved1,bm->get_blk_num);
  871. memcpy(&tmp,data+4,1);
  872. bm->chip_value_eft = tmp >>7;
  873. bm->get_blk_num = tmp >> 4;
  874. bm->reserved1 = ((tmp << 4) & 0xff) >> 5;
  875. }
  876. found_blocks = bm->get_blk_num;
  877. applog(LOG_ERR, "BitMain RxStatus tmp :0x%02x byte4 0x%02x chip_value_eft %d reserved %d get_blk_num %d ",tmp,*((uint8_t* )bm +4),bm->chip_value_eft,bm->reserved1,bm->get_blk_num);
  878. }
  879. applog(LOG_DEBUG, "BitMain RxStatusData: chipv_e(%d) chainnum(%d) fifos(%d) v1(%d) v2(%d) v3(%d) v4(%d) fann(%d) tempn(%d) fanet(%04x) tempet(%08x) ne(%d) regvalue(%d) crc(%04x)",
  880. bm->chip_value_eft, bm->chain_num, bm->fifo_space, bm->hw_version[0], bm->hw_version[1], bm->hw_version[2], bm->hw_version[3], bm->fan_num, bm->temp_num, bm->fan_exist, bm->temp_exist, bm->nonce_error, bm->reg_value, bm->crc);
  881. applog(LOG_DEBUG, "BitMain RxStatus Data chain info:");
  882. for(i = 0; i < bm->chain_num; i++) {
  883. applog(LOG_DEBUG, "BitMain RxStatus Data chain(%d) asic num=%d asic_exist=%08x asic_status=%08x", i+1, bm->chain_asic_num[i], bm->chain_asic_exist[i*8], bm->chain_asic_status[i*8]);
  884. }
  885. applog(LOG_DEBUG, "BitMain RxStatus Data temp info:");
  886. for(i = 0; i < bm->temp_num; i++) {
  887. applog(LOG_DEBUG, "BitMain RxStatus Data temp(%d) temp=%d", i+1, bm->temp[i]);
  888. }
  889. applog(LOG_DEBUG, "BitMain RxStatus Data fan info:");
  890. for(i = 0; i < bm->fan_num; i++) {
  891. applog(LOG_DEBUG, "BitMain RxStatus Data fan(%d) fan=%d", i+1, bm->fan[i]);
  892. }
  893. return 0;
  894. }
  895. static int bitmain_parse_rxnonce(const uint8_t * data, int datalen, struct bitmain_rxnonce_data *bm, int * nonce_num)
  896. {
  897. int i = 0;
  898. uint16_t crc = 0;
  899. uint8_t version = 0;
  900. int curnoncenum = 0;
  901. if (unlikely(!bm)) {
  902. applog(LOG_ERR, "bitmain_parse_rxnonce bitmain_rxstatus_data null");
  903. return -1;
  904. }
  905. if (unlikely(!data || datalen <= 0)) {
  906. applog(LOG_ERR, "bitmain_parse_rxnonce data null or datalen(%d) error", datalen);
  907. return -1;
  908. }
  909. memcpy(bm, data, sizeof(struct bitmain_rxnonce_data));
  910. if (bm->data_type != BITMAIN_DATA_TYPE_RXNONCE) {
  911. applog(LOG_ERR, "bitmain_parse_rxnonce datatype(%02x) error", bm->data_type);
  912. return -1;
  913. }
  914. if (bm->version != version) {
  915. applog(LOG_ERR, "bitmain_parse_rxnonce version(%02x) error", bm->version);
  916. return -1;
  917. }
  918. bm->length = htole16(bm->length);
  919. if (bm->length+4 != datalen) {
  920. applog(LOG_ERR, "bitmain_parse_rxnonce length(%d) error", bm->length);
  921. return -1;
  922. }
  923. crc = CRC16(data, datalen-2);
  924. memcpy(&(bm->crc), data+datalen-2, 2);
  925. bm->crc = htole16(bm->crc);
  926. if(crc != bm->crc) {
  927. applog(LOG_ERR, "bitmain_parse_rxnonce check crc(%d) != bm crc(%d) datalen(%d)", crc, bm->crc, datalen);
  928. return -1;
  929. }
  930. bm->fifo_space = htole16(bm->fifo_space);
  931. bm->diff = htole16(bm->diff);
  932. bm->total_nonce_num = htole64(bm->total_nonce_num);
  933. curnoncenum = (datalen-14)/8;
  934. applog(LOG_DEBUG, "BitMain RxNonce Data: nonce_num(%d) fifo_space(%d) diff(%d) tnn(%lld)", curnoncenum, bm->fifo_space, bm->diff, bm->total_nonce_num);
  935. for(i = 0; i < curnoncenum; i++) {
  936. bm->nonces[i].work_id = htole32(bm->nonces[i].work_id);
  937. bm->nonces[i].nonce = htole32(bm->nonces[i].nonce);
  938. applog(LOG_DEBUG, "BitMain RxNonce Data %d: work_id(%d) nonce(%08x)(%d)",
  939. i, bm->nonces[i].work_id, bm->nonces[i].nonce, bm->nonces[i].nonce);
  940. }
  941. *nonce_num = curnoncenum;
  942. return 0;
  943. }
  944. static int bitmain_read(struct cgpu_info *bitmain, unsigned char *buf,
  945. size_t bufsize, int timeout)
  946. {
  947. int err = 0;
  948. size_t total = 0;
  949. if(bitmain == NULL || buf == NULL || bufsize <= 0) {
  950. applog(LOG_WARNING, "bitmain_read parameter error bufsize(%d)", bufsize);
  951. return -1;
  952. }
  953. {
  954. err = btm_read(bitmain, buf, bufsize);
  955. total = err;
  956. }
  957. return total;
  958. }
  959. static int bitmain_write(struct cgpu_info *bitmain, char *buf, ssize_t len)
  960. {
  961. int err;
  962. {
  963. int havelen = 0;
  964. while(havelen < len) {
  965. err = btm_write(bitmain, buf+havelen, len-havelen);
  966. if(err < 0) {
  967. applog(LOG_DEBUG, "%s%i: btm_write got err %d", bitmain->drv->name,
  968. bitmain->device_id, err);
  969. applog(LOG_WARNING, "usb_write error on bitmain_write");
  970. return BTM_SEND_ERROR;
  971. } else {
  972. havelen += err;
  973. }
  974. }
  975. }
  976. return BTM_SEND_OK;
  977. }
  978. static int bitmain_send_data(const uint8_t * data, int datalen, struct cgpu_info *bitmain)
  979. {
  980. int ret;
  981. if(datalen <= 0) {
  982. return 0;
  983. }
  984. //struct bitmain_info *info = bitmain->device_data;
  985. //int delay;
  986. //delay = datalen * 10 * 1000000;
  987. //delay = delay / info->baud;
  988. //delay += 4000;
  989. if(opt_debug) {
  990. char hex[(datalen * 2) + 1];
  991. bin2hex(hex, data, datalen);
  992. applog(LOG_DEBUG, "BitMain: Sent(%d): %s", datalen, hex);
  993. }
  994. //cgtimer_t ts_start;
  995. //cgsleep_prepare_r(&ts_start);
  996. //applog(LOG_DEBUG, "----bitmain_send_data start");
  997. ret = bitmain_write(bitmain, (char *)data, datalen);
  998. applog(LOG_DEBUG, "----bitmain_send_data stop ret=%d datalen=%d", ret, datalen);
  999. //cgsleep_us_r(&ts_start, delay);
  1000. //applog(LOG_DEBUG, "BitMain: Sent: Buffer delay: %dus", delay);
  1001. return ret;
  1002. }
  1003. static bool bitmain_decode_nonce(struct thr_info *thr, struct cgpu_info *bitmain,
  1004. struct bitmain_info *info, uint32_t nonce, struct work *work)
  1005. {
  1006. info = bitmain->device_data;
  1007. //info->matching_work[work->subid]++;
  1008. applog(LOG_DEBUG, "BitMain: submit nonce = %08x", nonce);
  1009. return submit_nonce(thr, work, nonce);
  1010. }
  1011. static void bitmain_inc_nvw(struct bitmain_info *info, struct thr_info *thr)
  1012. {
  1013. applog(LOG_INFO, "%s%d: No matching work - HW error",
  1014. thr->cgpu->drv->name, thr->cgpu->device_id);
  1015. inc_hw_errors_only(thr);
  1016. info->no_matching_work++;
  1017. }
  1018. static inline void record_temp_fan(struct bitmain_info *info, struct bitmain_rxstatus_data *bm, float *temp_avg)
  1019. {
  1020. int i = 0;
  1021. int maxfan = 0, maxtemp = 0;
  1022. *temp_avg = 0;
  1023. info->fan_num = bm->fan_num;
  1024. for(i = 0; i < bm->fan_num; i++) {
  1025. info->fan[i] = bm->fan[i] * BITMAIN_FAN_FACTOR;
  1026. if(info->fan[i] > maxfan)
  1027. maxfan = info->fan[i];
  1028. }
  1029. info->temp_num = bm->temp_num;
  1030. for(i = 0; i < bm->temp_num; i++) {
  1031. info->temp[i] = bm->temp[i];
  1032. /*
  1033. if(bm->temp[i] & 0x80) {
  1034. bm->temp[i] &= 0x7f;
  1035. info->temp[i] = 0 - ((~bm->temp[i] & 0x7f) + 1);
  1036. }*/
  1037. *temp_avg += info->temp[i];
  1038. if(info->temp[i] > info->temp_max) {
  1039. info->temp_max = info->temp[i];
  1040. }
  1041. if(info->temp[i] > maxtemp)
  1042. maxtemp = info->temp[i];
  1043. }
  1044. if(bm->temp_num > 0) {
  1045. *temp_avg = *temp_avg / bm->temp_num;
  1046. info->temp_avg = *temp_avg;
  1047. }
  1048. // inc_dev_status
  1049. mutex_lock(&stats_lock);
  1050. info->g_max_fan = maxfan;
  1051. info->g_max_temp = maxtemp;
  1052. mutex_unlock(&stats_lock);
  1053. }
  1054. static void bitmain_update_temps(struct cgpu_info *bitmain, struct bitmain_info *info,
  1055. struct bitmain_rxstatus_data *bm)
  1056. {
  1057. char tmp[64] = {0};
  1058. char msg[10240] = {0};
  1059. int i = 0;
  1060. record_temp_fan(info, bm, &(bitmain->temp));
  1061. strcpy(msg, "BitMain: ");
  1062. for(i = 0; i < bm->fan_num; i++) {
  1063. if(i != 0) {
  1064. strcat(msg, ", ");
  1065. }
  1066. sprintf(tmp, "Fan%d: %d/m", i+1, info->fan[i]);
  1067. strcat(msg, tmp);
  1068. }
  1069. strcat(msg, "\t");
  1070. for(i = 0; i < bm->temp_num; i++) {
  1071. if(i != 0) {
  1072. strcat(msg, ", ");
  1073. }
  1074. sprintf(tmp, "Temp%d: %dC", i+1, info->temp[i]);
  1075. strcat(msg, tmp);
  1076. }
  1077. sprintf(tmp, ", TempMAX: %dC", info->temp_max);
  1078. strcat(msg, tmp);
  1079. applog(LOG_INFO, msg);
  1080. info->temp_history_index++;
  1081. info->temp_sum += bitmain->temp;
  1082. applog(LOG_DEBUG, "BitMain: temp_index: %d, temp_count: %d, temp_old: %d",
  1083. info->temp_history_index, info->temp_history_count, info->temp_old);
  1084. if (info->temp_history_index == info->temp_history_count) {
  1085. info->temp_history_index = 0;
  1086. info->temp_sum = 0;
  1087. }
  1088. if (unlikely(info->temp_old >= opt_bitmain_overheat)) {
  1089. applog(LOG_WARNING, "BTM%d overheat! Idling", bitmain->device_id);
  1090. info->overheat = true;
  1091. } else if (info->overheat && info->temp_old <= opt_bitmain_temp) {
  1092. applog(LOG_WARNING, "BTM%d cooled, restarting", bitmain->device_id);
  1093. info->overheat = false;
  1094. }
  1095. }
  1096. extern void cg_logwork_uint32(struct work *work, uint32_t nonce, bool ok);
  1097. static void bitmain_parse_results(struct cgpu_info *bitmain, struct bitmain_info *info,
  1098. struct thr_info *thr, uint8_t *buf, int *offset)
  1099. {
  1100. int i, j, n, m, r, errordiff, spare = BITMAIN_READ_SIZE;
  1101. uint32_t checkbit = 0x00000000;
  1102. bool found = false;
  1103. struct work *work = NULL;
  1104. struct bitmain_packet_head packethead;
  1105. int asicnum = 0;
  1106. int idiff = 0;
  1107. int mod = 0,tmp = 0;
  1108. for (i = 0; i <= spare; i++) {
  1109. if(buf[i] == 0xa1) {
  1110. struct bitmain_rxstatus_data rxstatusdata;
  1111. applog(LOG_DEBUG, "bitmain_parse_results RxStatus Data");
  1112. if(*offset < 4) {
  1113. return;
  1114. }
  1115. memcpy(&packethead, buf+i, sizeof(struct bitmain_packet_head));
  1116. packethead.length = htole16(packethead.length);
  1117. if(packethead.length > 1130) {
  1118. applog(LOG_ERR, "bitmain_parse_results bitmain_parse_rxstatus datalen=%d error", packethead.length+4);
  1119. continue;
  1120. }
  1121. if(*offset < packethead.length + 4) {
  1122. return;
  1123. }
  1124. if(bitmain_parse_rxstatus(buf+i, packethead.length+4, &rxstatusdata) != 0) {
  1125. applog(LOG_ERR, "bitmain_parse_results bitmain_parse_rxstatus error len=%d", packethead.length+4);
  1126. } else {
  1127. mutex_lock(&info->qlock);
  1128. info->chain_num = rxstatusdata.chain_num;
  1129. info->fifo_space = rxstatusdata.fifo_space;
  1130. info->hw_version[0] = rxstatusdata.hw_version[0];
  1131. info->hw_version[1] = rxstatusdata.hw_version[1];
  1132. info->hw_version[2] = rxstatusdata.hw_version[2];
  1133. info->hw_version[3] = rxstatusdata.hw_version[3];
  1134. info->nonce_error = rxstatusdata.nonce_error;
  1135. errordiff = info->nonce_error-info->last_nonce_error;
  1136. //sprintf(g_miner_version, "%d.%d.%d.%d", info->hw_version[0], info->hw_version[1], info->hw_version[2], info->hw_version[3]);
  1137. applog(LOG_ERR, "bitmain_parse_results v=%d chain=%d fifo=%d hwv1=%d hwv2=%d hwv3=%d hwv4=%d nerr=%d-%d freq=%d chain info:",
  1138. rxstatusdata.version, info->chain_num, info->fifo_space, info->hw_version[0], info->hw_version[1], info->hw_version[2], info->hw_version[3],
  1139. info->last_nonce_error, info->nonce_error, info->frequency);
  1140. memcpy(info->chain_asic_exist, rxstatusdata.chain_asic_exist, BITMAIN_MAX_CHAIN_NUM*32);
  1141. memcpy(info->chain_asic_status, rxstatusdata.chain_asic_status, BITMAIN_MAX_CHAIN_NUM*32);
  1142. for(n = 0; n < rxstatusdata.chain_num; n++) {
  1143. info->chain_asic_num[n] = rxstatusdata.chain_asic_num[n];
  1144. memset(info->chain_asic_status_t[n], 0, 320);
  1145. j = 0;
  1146. mod = 0;
  1147. if(info->chain_asic_num[n] <= 0) {
  1148. asicnum = 0;
  1149. } else {
  1150. mod = info->chain_asic_num[n] % 32;
  1151. if(mod == 0) {
  1152. asicnum = info->chain_asic_num[n] / 32;
  1153. } else {
  1154. asicnum = info->chain_asic_num[n] / 32 + 1;
  1155. }
  1156. }
  1157. if(asicnum > 0) {
  1158. for(m = asicnum-1; m >= 0; m--) {
  1159. tmp = mod ? (32-mod): 0;
  1160. for(r = tmp;r < 32;r++){
  1161. if((r-tmp)%8 == 0 && (r-tmp) !=0){
  1162. info->chain_asic_status_t[n][j] = ' ';
  1163. j++;
  1164. }
  1165. checkbit = num2bit(r);
  1166. if(rxstatusdata.chain_asic_exist[n*8+m] & checkbit) {
  1167. if(rxstatusdata.chain_asic_status[n*8+m] & checkbit) {
  1168. info->chain_asic_status_t[n][j] = 'o';
  1169. } else {
  1170. info->chain_asic_status_t[n][j] = 'x';
  1171. }
  1172. } else {
  1173. info->chain_asic_status_t[n][j] = '-';
  1174. }
  1175. j++;
  1176. }
  1177. info->chain_asic_status_t[n][j] = ' ';
  1178. j++;
  1179. mod = 0;
  1180. }
  1181. }
  1182. applog(LOG_DEBUG, "bitmain_parse_results chain(%d) asic_num=%d asic_exist=%08x%08x%08x%08x%08x%08x%08x%08x asic_status=%08x%08x%08x%08x%08x%08x%08x%08x",
  1183. n, info->chain_asic_num[n],
  1184. info->chain_asic_exist[n*8+0], info->chain_asic_exist[n*8+1], info->chain_asic_exist[n*8+2], info->chain_asic_exist[n*8+3], info->chain_asic_exist[n*8+4], info->chain_asic_exist[n*8+5], info->chain_asic_exist[n*8+6], info->chain_asic_exist[n*8+7],
  1185. info->chain_asic_status[n*8+0], info->chain_asic_status[n*8+1], info->chain_asic_status[n*8+2], info->chain_asic_status[n*8+3], info->chain_asic_status[n*8+4], info->chain_asic_status[n*8+5], info->chain_asic_status[n*8+6], info->chain_asic_status[n*8+7]);
  1186. applog(LOG_ERR, "bitmain_parse_results chain(%d) asic_num=%d asic_status=%s", n, info->chain_asic_num[n], info->chain_asic_status_t[n]);
  1187. }
  1188. mutex_unlock(&info->qlock);
  1189. if(errordiff > 0) {
  1190. for(j = 0; j < errordiff; j++) {
  1191. bitmain_inc_nvw(info, thr);
  1192. }
  1193. mutex_lock(&info->qlock);
  1194. info->last_nonce_error += errordiff;
  1195. mutex_unlock(&info->qlock);
  1196. }
  1197. bitmain_update_temps(bitmain, info, &rxstatusdata);
  1198. }
  1199. found = true;
  1200. spare = packethead.length + 4 + i;
  1201. if(spare > *offset) {
  1202. applog(LOG_ERR, "bitmain_parse_rxresults space(%d) > offset(%d)", spare, *offset);
  1203. spare = *offset;
  1204. }
  1205. break;
  1206. } else if(buf[i] == 0xa2) {
  1207. struct bitmain_rxnonce_data rxnoncedata;
  1208. int nonce_num = 0;
  1209. applog(LOG_DEBUG, "bitmain_parse_results RxNonce Data");
  1210. if(*offset < 4) {
  1211. return;
  1212. }
  1213. memcpy(&packethead, buf+i, sizeof(struct bitmain_packet_head));
  1214. packethead.length = htole16(packethead.length);
  1215. if(packethead.length > 1030) {
  1216. applog(LOG_ERR, "bitmain_parse_results bitmain_parse_rxnonce datalen=%d error", packethead.length+4);
  1217. continue;
  1218. }
  1219. if(*offset < packethead.length + 4) {
  1220. return;
  1221. }
  1222. if(bitmain_parse_rxnonce(buf+i, packethead.length+4, &rxnoncedata, &nonce_num) != 0) {
  1223. applog(LOG_ERR, "bitmain_parse_results bitmain_parse_rxnonce error len=%d", packethead.length+4);
  1224. } else {
  1225. for(j = 0; j < nonce_num; j++) {
  1226. const int work_id = rxnoncedata.nonces[j].work_id;
  1227. HASH_FIND_INT(bitmain->queued_work, &work_id, work);
  1228. if(work) {
  1229. if(BITMAIN_TEST_PRINT_WORK) {
  1230. applog(LOG_ERR, "bitmain_parse_results nonce find work(%d-%d)(%08x)", work->id, rxnoncedata.nonces[j].work_id, rxnoncedata.nonces[j].nonce);
  1231. char ob_hex[(32 * 2) + 1];
  1232. bin2hex(ob_hex, work->midstate, 32);
  1233. applog(LOG_ERR, "work %d midstate: %s", work->id, ob_hex);
  1234. bin2hex(ob_hex, &work->data[64], 12);
  1235. applog(LOG_ERR, "work %d data2: %s", work->id, ob_hex);
  1236. }
  1237. if(bfg_work_block(work) != info->last_work_block) {
  1238. applog(LOG_ERR, "BitMain: bitmain_parse_rxnonce work(%d) nonce stale", rxnoncedata.nonces[j].work_id);
  1239. } else {
  1240. if (bitmain_decode_nonce(thr, bitmain, info, rxnoncedata.nonces[j].nonce, work)) {
  1241. mutex_lock(&info->qlock);
  1242. info->nonces++;
  1243. info->auto_nonces++;
  1244. mutex_unlock(&info->qlock);
  1245. } else {
  1246. //bitmain_inc_nvw(info, thr);
  1247. applog(LOG_ERR, "BitMain: bitmain_decode_nonce error work(%d)", rxnoncedata.nonces[j].work_id);
  1248. }
  1249. }
  1250. } else {
  1251. //bitmain_inc_nvw(info, thr);
  1252. applog(LOG_ERR, "BitMain: Nonce not find work(%d)", rxnoncedata.nonces[j].work_id);
  1253. }
  1254. }
  1255. #ifdef BITMAIN_CALC_DIFF1
  1256. if(opt_bitmain_hwerror) {
  1257. int difftmp = 0;
  1258. difftmp = rxnoncedata.diff;
  1259. idiff = 1;
  1260. while(difftmp > 0) {
  1261. difftmp--;
  1262. idiff = idiff << 1;
  1263. }
  1264. mutex_lock(&info->qlock);
  1265. difftmp = idiff*(rxnoncedata.total_nonce_num-info->total_nonce_num);
  1266. if(difftmp < 0)
  1267. difftmp = 0;
  1268. info->nonces = info->nonces+difftmp;
  1269. info->auto_nonces = info->auto_nonces+difftmp;
  1270. info->total_nonce_num = rxnoncedata.total_nonce_num;
  1271. info->fifo_space = rxnoncedata.fifo_space;
  1272. mutex_unlock(&info->qlock);
  1273. applog(LOG_DEBUG, "bitmain_parse_rxnonce fifo space=%d diff=%d rxtnn=%lld tnn=%lld", info->fifo_space, idiff, rxnoncedata.total_nonce_num, info->total_nonce_num);
  1274. } else {
  1275. mutex_lock(&info->qlock);
  1276. info->fifo_space = rxnoncedata.fifo_space;
  1277. mutex_unlock(&info->qlock);
  1278. applog(LOG_DEBUG, "bitmain_parse_rxnonce fifo space=%d", info->fifo_space);
  1279. }
  1280. #else
  1281. mutex_lock(&info->qlock);
  1282. info->fifo_space = rxnoncedata.fifo_space;
  1283. mutex_unlock(&info->qlock);
  1284. applog(LOG_DEBUG, "bitmain_parse_rxnonce fifo space=%d", info->fifo_space);
  1285. #endif
  1286. #ifndef WIN32
  1287. if(nonce_num < BITMAIN_MAX_NONCE_NUM)
  1288. cgsleep_ms(5);
  1289. #endif
  1290. }
  1291. found = true;
  1292. spare = packethead.length + 4 + i;
  1293. if(spare > *offset) {
  1294. applog(LOG_ERR, "bitmain_parse_rxnonce space(%d) > offset(%d)", spare, *offset);
  1295. spare = *offset;
  1296. }
  1297. break;
  1298. } else {
  1299. applog(LOG_ERR, "bitmain_parse_results data type error=%02x", buf[i]);
  1300. }
  1301. }
  1302. if (!found) {
  1303. spare = *offset - BITMAIN_READ_SIZE;
  1304. /* We are buffering and haven't accumulated one more corrupt
  1305. * work result. */
  1306. if (spare < (int)BITMAIN_READ_SIZE)
  1307. return;
  1308. bitmain_inc_nvw(info, thr);
  1309. }
  1310. *offset -= spare;
  1311. memmove(buf, buf + spare, *offset);
  1312. }
  1313. static void bitmain_running_reset(struct cgpu_info *bitmain, struct bitmain_info *info)
  1314. {
  1315. bitmain->results = 0;
  1316. info->reset = false;
  1317. }
  1318. static void *bitmain_get_results(void *userdata)
  1319. {
  1320. struct cgpu_info *bitmain = (struct cgpu_info *)userdata;
  1321. struct bitmain_info *info = bitmain->device_data;
  1322. int offset = 0, ret = 0;
  1323. const int rsize = BITMAIN_FTDI_READSIZE;
  1324. uint8_t readbuf[BITMAIN_READBUF_SIZE];
  1325. struct thr_info *thr = info->thr;
  1326. char threadname[24];
  1327. int errorcount = 0;
  1328. snprintf(threadname, 24, "btm_recv/%d", bitmain->device_id);
  1329. RenameThread(threadname);
  1330. while (likely(!bitmain->shutdown)) {
  1331. unsigned char buf[rsize];
  1332. //applog(LOG_DEBUG, "+++++++bitmain_get_results offset=%d", offset);
  1333. if (offset >= (int)BITMAIN_READ_SIZE) {
  1334. //applog(LOG_DEBUG, "======start bitmain_get_results ");
  1335. bitmain_parse_results(bitmain, info, thr, readbuf, &offset);
  1336. //applog(LOG_DEBUG, "======stop bitmain_get_results ");
  1337. }
  1338. if (unlikely(offset + rsize >= BITMAIN_READBUF_SIZE)) {
  1339. /* This should never happen */
  1340. applog(LOG_DEBUG, "BitMain readbuf overflow, resetting buffer");
  1341. offset = 0;
  1342. }
  1343. if (unlikely(info->reset)) {
  1344. bitmain_running_reset(bitmain, info);
  1345. /* Discard anything in the buffer */
  1346. offset = 0;
  1347. }
  1348. /* As the usb read returns after just 1ms, sleep long enough
  1349. * to leave the interface idle for writes to occur, but do not
  1350. * sleep if we have been receiving data as more may be coming. */
  1351. //if (offset == 0) {
  1352. // cgsleep_ms_r(&ts_start, BITMAIN_READ_TIMEOUT);
  1353. //}
  1354. //cgsleep_prepare_r(&ts_start);
  1355. //applog(LOG_DEBUG, "======start bitmain_get_results bitmain_read");
  1356. ret = bitmain_read(bitmain, buf, rsize, BITMAIN_READ_TIMEOUT);
  1357. //applog(LOG_DEBUG, "======stop bitmain_get_results bitmain_read=%d", ret);
  1358. if ((ret < 1) || (ret == 18)) {
  1359. errorcount++;
  1360. #ifdef WIN32
  1361. if(errorcount > 200) {
  1362. //applog(LOG_ERR, "bitmain_read errorcount ret=%d", ret);
  1363. cgsleep_ms(20);
  1364. errorcount = 0;
  1365. }
  1366. #else
  1367. if(errorcount > 3) {
  1368. //applog(LOG_ERR, "bitmain_read errorcount ret=%d", ret);
  1369. cgsleep_ms(20);
  1370. errorcount = 0;
  1371. }
  1372. #endif
  1373. if(ret < 1)
  1374. continue;
  1375. }
  1376. if (opt_debug) {
  1377. char hex[(ret * 2) + 1];
  1378. bin2hex(hex, buf, ret);
  1379. applog(LOG_DEBUG, "BitMain: get: %s", hex);
  1380. }
  1381. memcpy(readbuf+offset, buf, ret);
  1382. offset += ret;
  1383. }
  1384. return NULL;
  1385. }
  1386. static void bitmain_set_timeout(struct bitmain_info *info)
  1387. {
  1388. info->timeout = BITMAIN_TIMEOUT_FACTOR / info->frequency;
  1389. }
  1390. static void bitmain_init(struct cgpu_info *bitmain)
  1391. {
  1392. applog(LOG_INFO, "BitMain: Opened on %s", bitmain->device_path);
  1393. }
  1394. static bool bitmain_prepare(struct thr_info *thr)
  1395. {
  1396. struct cgpu_info *bitmain = thr->cgpu;
  1397. struct bitmain_info *info = bitmain->device_data;
  1398. free(bitmain->works);
  1399. bitmain->works = calloc(BITMAIN_MAX_WORK_NUM * sizeof(struct work *),
  1400. BITMAIN_ARRAY_SIZE);
  1401. if (!bitmain->works)
  1402. quit(1, "Failed to calloc bitmain works in bitmain_prepare");
  1403. info->thr = thr;
  1404. mutex_init(&info->lock);
  1405. mutex_init(&info->qlock);
  1406. if (unlikely(pthread_cond_init(&info->qcond, NULL)))
  1407. quit(1, "Failed to pthread_cond_init bitmain qcond");
  1408. if (pthread_create(&info->read_thr, NULL, bitmain_get_results, (void *)bitmain))
  1409. quit(1, "Failed to create bitmain read_thr");
  1410. bitmain_init(bitmain);
  1411. return true;
  1412. }
  1413. static int bitmain_initialize(struct cgpu_info *bitmain)
  1414. {
  1415. uint8_t data[BITMAIN_READBUF_SIZE];
  1416. struct bitmain_info *info = NULL;
  1417. int ret = 0;
  1418. uint8_t sendbuf[BITMAIN_SENDBUF_SIZE];
  1419. int readlen = 0;
  1420. int sendlen = 0;
  1421. int trycount = 3;
  1422. struct timespec p;
  1423. struct bitmain_rxstatus_data rxstatusdata;
  1424. int i = 0, j = 0, m = 0, r = 0, statusok = 0;
  1425. uint32_t checkbit = 0x00000000;
  1426. int hwerror_eft = 0;
  1427. int beeper_ctrl = 1;
  1428. int tempover_ctrl = 1;
  1429. int home_mode = 0;
  1430. struct bitmain_packet_head packethead;
  1431. int asicnum = 0;
  1432. int mod = 0,tmp = 0;
  1433. /* Send reset, then check for result */
  1434. if(!bitmain) {
  1435. applog(LOG_WARNING, "bitmain_initialize cgpu_info is null");
  1436. return -1;
  1437. }
  1438. info = bitmain->device_data;
  1439. /* clear read buf */
  1440. ret = bitmain_read(bitmain, data, BITMAIN_READBUF_SIZE,
  1441. BITMAIN_RESET_TIMEOUT);
  1442. if(ret > 0) {
  1443. if (opt_debug) {
  1444. char hex[(ret * 2) + 1];
  1445. bin2hex(hex, data, ret);
  1446. applog(LOG_DEBUG, "BTM%d Clear Read(%d): %s", bitmain->device_id, ret, hex);
  1447. }
  1448. }
  1449. sendlen = bitmain_set_rxstatus((struct bitmain_rxstatus_token *)sendbuf, 0, 1, 0, 0);
  1450. if(sendlen <= 0) {
  1451. applog(LOG_ERR, "bitmain_initialize bitmain_set_rxstatus error(%d)", sendlen);
  1452. return -1;
  1453. }
  1454. ret = bitmain_send_data(sendbuf, sendlen, bitmain);
  1455. if (unlikely(ret == BTM_SEND_ERROR)) {
  1456. applog(LOG_ERR, "bitmain_initialize bitmain_send_data error");
  1457. return -1;
  1458. }
  1459. while(trycount >= 0) {
  1460. ret = bitmain_read(bitmain, data+readlen, BITMAIN_READBUF_SIZE, BITMAIN_RESET_TIMEOUT);
  1461. if(ret > 0) {
  1462. readlen += ret;
  1463. if(readlen > BITMAIN_READ_SIZE) {
  1464. for(i = 0; i < readlen; i++) {
  1465. if(data[i] == 0xa1) {
  1466. if (opt_debug) {
  1467. char hex[(readlen * 2) + 1];
  1468. bin2hex(hex, data, readlen);
  1469. applog(LOG_DEBUG, "%s%d initset: get: %s", bitmain->drv->name, bitmain->device_id, hex);
  1470. }
  1471. memcpy(&packethead, data+i, sizeof(struct bitmain_packet_head));
  1472. packethead.length = htole16(packethead.length);
  1473. if(packethead.length > 1130) {
  1474. applog(LOG_ERR, "bitmain_initialize rxstatus datalen=%d error", packethead.length+4);
  1475. continue;
  1476. }
  1477. if(readlen-i < packethead.length+4) {
  1478. applog(LOG_ERR, "bitmain_initialize rxstatus datalen=%d<%d low", readlen-i, packethead.length+4);
  1479. continue;
  1480. }
  1481. if (bitmain_parse_rxstatus(data+i, packethead.length+4, &rxstatusdata) != 0) {
  1482. applog(LOG_ERR, "bitmain_initialize bitmain_parse_rxstatus error");
  1483. continue;
  1484. }
  1485. info->chain_num = rxstatusdata.chain_num;
  1486. info->fifo_space = rxstatusdata.fifo_space;
  1487. info->hw_version[0] = rxstatusdata.hw_version[0];
  1488. info->hw_version[1] = rxstatusdata.hw_version[1];
  1489. info->hw_version[2] = rxstatusdata.hw_version[2];
  1490. info->hw_version[3] = rxstatusdata.hw_version[3];
  1491. info->nonce_error = 0;
  1492. info->last_nonce_error = 0;
  1493. sprintf(info->g_miner_version, "%d.%d.%d.%d", info->hw_version[0], info->hw_version[1], info->hw_version[2], info->hw_version[3]);
  1494. applog(LOG_ERR, "bitmain_initialize rxstatus v(%d) chain(%d) fifo(%d) hwv1(%d) hwv2(%d) hwv3(%d) hwv4(%d) nerr(%d) freq=%d",
  1495. rxstatusdata.version, info->chain_num, info->fifo_space, info->hw_version[0], info->hw_version[1], info->hw_version[2], info->hw_version[3],
  1496. rxstatusdata.nonce_error, info->frequency);
  1497. memcpy(info->chain_asic_exist, rxstatusdata.chain_asic_exist, BITMAIN_MAX_CHAIN_NUM*32);
  1498. memcpy(info->chain_asic_status, rxstatusdata.chain_asic_status, BITMAIN_MAX_CHAIN_NUM*32);
  1499. for(i = 0; i < rxstatusdata.chain_num; i++) {
  1500. info->chain_asic_num[i] = rxstatusdata.chain_asic_num[i];
  1501. memset(info->chain_asic_status_t[i], 0, 320);
  1502. j = 0;
  1503. mod = 0;
  1504. if(info->chain_asic_num[i] <= 0) {
  1505. asicnum = 0;
  1506. } else {
  1507. mod = info->chain_asic_num[i] % 32;
  1508. if(mod == 0) {
  1509. asicnum = info->chain_asic_num[i] / 32;
  1510. } else {
  1511. asicnum = info->chain_asic_num[i] / 32 + 1;
  1512. }
  1513. }
  1514. if(asicnum > 0) {
  1515. for(m = asicnum-1; m >= 0; m--) {
  1516. tmp = mod ? (32-mod):0;
  1517. for(r = tmp;r < 32;r++){
  1518. if((r-tmp)%8 == 0 && (r-tmp) !=0){
  1519. info->chain_asic_status_t[i][j] = ' ';
  1520. j++;
  1521. }
  1522. checkbit = num2bit(r);
  1523. if(rxstatusdata.chain_asic_exist[i*8+m] & checkbit) {
  1524. if(rxstatusdata.chain_asic_status[i*8+m] & checkbit) {
  1525. info->chain_asic_status_t[i][j] = 'o';
  1526. } else {
  1527. info->chain_asic_status_t[i][j] = 'x';
  1528. }
  1529. } else {
  1530. info->chain_asic_status_t[i][j] = '-';
  1531. }
  1532. j++;
  1533. }
  1534. info->chain_asic_status_t[i][j] = ' ';
  1535. j++;
  1536. mod = 0;
  1537. }
  1538. }
  1539. applog(LOG_DEBUG, "bitmain_initialize chain(%d) asic_num=%d asic_exist=%08x%08x%08x%08x%08x%08x%08x%08x asic_status=%08x%08x%08x%08x%08x%08x%08x%08x",
  1540. i, info->chain_asic_num[i],
  1541. info->chain_asic_exist[i*8+0], info->chain_asic_exist[i*8+1], info->chain_asic_exist[i*8+2], info->chain_asic_exist[i*8+3], info->chain_asic_exist[i*8+4], info->chain_asic_exist[i*8+5], info->chain_asic_exist[i*8+6], info->chain_asic_exist[i*8+7],
  1542. info->chain_asic_status[i*8+0], info->chain_asic_status[i*8+1], info->chain_asic_status[i*8+2], info->chain_asic_status[i*8+3], info->chain_asic_status[i*8+4], info->chain_asic_status[i*8+5], info->chain_asic_status[i*8+6], info->chain_asic_status[i*8+7]);
  1543. applog(LOG_ERR, "bitmain_initialize chain(%d) asic_num=%d asic_status=%s", i, info->chain_asic_num[i], info->chain_asic_status_t[i]);
  1544. }
  1545. bitmain_update_temps(bitmain, info, &rxstatusdata);
  1546. statusok = 1;
  1547. break;
  1548. }
  1549. }
  1550. if(statusok) {
  1551. break;
  1552. }
  1553. }
  1554. }
  1555. trycount--;
  1556. p.tv_sec = 0;
  1557. p.tv_nsec = BITMAIN_RESET_PITCH;
  1558. nanosleep(&p, NULL);
  1559. }
  1560. p.tv_sec = 0;
  1561. p.tv_nsec = BITMAIN_RESET_PITCH;
  1562. nanosleep(&p, NULL);
  1563. cgtime(&info->last_status_time);
  1564. if(statusok) {
  1565. applog(LOG_ERR, "bitmain_initialize start send txconfig");
  1566. if(opt_bitmain_hwerror)
  1567. hwerror_eft = 1;
  1568. else
  1569. hwerror_eft = 0;
  1570. if(opt_bitmain_nobeeper)
  1571. beeper_ctrl = 0;
  1572. else
  1573. beeper_ctrl = 1;
  1574. if(opt_bitmain_notempoverctrl)
  1575. tempover_ctrl = 0;
  1576. else
  1577. tempover_ctrl = 1;
  1578. if(opt_bitmain_homemode)
  1579. home_mode= 1;
  1580. else
  1581. home_mode= 0;
  1582. sendlen = bitmain_set_txconfig((struct bitmain_txconfig_token *)sendbuf, 1, 1, 1, 1, 1, 0, 1, hwerror_eft, beeper_ctrl, tempover_ctrl,home_mode,
  1583. info->chain_num, info->asic_num, BITMAIN_DEFAULT_FAN_MAX_PWM, info->timeout,
  1584. info->frequency, info->voltage, 0, 0, 0x04, info->reg_data);
  1585. if(sendlen <= 0) {
  1586. applog(LOG_ERR, "bitmain_initialize bitmain_set_txconfig error(%d)", sendlen);
  1587. return -1;
  1588. }
  1589. ret = bitmain_send_data(sendbuf, sendlen, bitmain);
  1590. if (unlikely(ret == BTM_SEND_ERROR)) {
  1591. applog(LOG_ERR, "bitmain_initialize bitmain_send_data error");
  1592. return -1;
  1593. }
  1594. applog(LOG_WARNING, "BMM%d: InitSet succeeded", bitmain->device_id);
  1595. } else {
  1596. applog(LOG_WARNING, "BMS%d: InitSet error", bitmain->device_id);
  1597. return -1;
  1598. }
  1599. return 0;
  1600. }
  1601. static bool bitmain_detect_one(const char * devpath)
  1602. {
  1603. int baud, chain_num, asic_num, timeout, frequency = 0;
  1604. char frequency_t[256] = {0};
  1605. uint8_t reg_data[4] = {0};
  1606. uint8_t voltage[2] = {0};
  1607. char voltage_t[8] = {0};
  1608. int this_option_offset = ++option_offset;
  1609. struct bitmain_info *info;
  1610. struct cgpu_info *bitmain;
  1611. bool configured;
  1612. int ret;
  1613. if (opt_bitmain_options == NULL)
  1614. return false;
  1615. bitmain = btm_alloc_cgpu(&bitmain_drv, BITMAIN_MINER_THREADS);
  1616. configured = get_options(this_option_offset, &baud, &chain_num,
  1617. &asic_num, &timeout, &frequency, frequency_t, reg_data, voltage, voltage_t);
  1618. get_option_freq(&timeout, &frequency, frequency_t, reg_data);
  1619. get_option_voltage(voltage, voltage_t);
  1620. if (!btm_init(bitmain, opt_bitmain_dev))
  1621. goto shin;
  1622. applog(LOG_ERR, "bitmain_detect_one btm init ok");
  1623. bitmain->device_data = calloc(sizeof(struct bitmain_info), 1);
  1624. /* make sure initialize successfully*/
  1625. memset(bitmain->device_data,0,sizeof(struct bitmain_info));
  1626. if (unlikely(!(bitmain->device_data)))
  1627. quit(1, "Failed to calloc bitmain_info data");
  1628. info = bitmain->device_data;
  1629. if (configured) {
  1630. info->baud = baud;
  1631. info->chain_num = chain_num;
  1632. info->asic_num = asic_num;
  1633. info->timeout = timeout;
  1634. info->frequency = frequency;
  1635. strcpy(info->frequency_t, frequency_t);
  1636. memcpy(info->reg_data, reg_data, 4);
  1637. memcpy(info->voltage, voltage, 2);
  1638. strcpy(info->voltage_t, voltage_t);
  1639. } else {
  1640. info->baud = BITMAIN_IO_SPEED;
  1641. info->chain_num = BITMAIN_DEFAULT_CHAIN_NUM;
  1642. info->asic_num = BITMAIN_DEFAULT_ASIC_NUM;
  1643. info->timeout = BITMAIN_DEFAULT_TIMEOUT;
  1644. info->frequency = BITMAIN_DEFAULT_FREQUENCY;
  1645. sprintf(info->frequency_t, "%d", BITMAIN_DEFAULT_FREQUENCY);
  1646. memset(info->reg_data, 0, 4);
  1647. info->voltage[0] = BITMAIN_DEFAULT_VOLTAGE0;
  1648. info->voltage[1] = BITMAIN_DEFAULT_VOLTAGE1;
  1649. strcpy(info->voltage_t, BITMAIN_DEFAULT_VOLTAGE_T);
  1650. }
  1651. info->fan_pwm = BITMAIN_DEFAULT_FAN_MIN_PWM;
  1652. info->temp_max = 0;
  1653. /* This is for check the temp/fan every 3~4s */
  1654. info->temp_history_count = (4 / (float)((float)info->timeout * ((float)1.67/0x32))) + 1;
  1655. if (info->temp_history_count <= 0)
  1656. info->temp_history_count = 1;
  1657. info->temp_history_index = 0;
  1658. info->temp_sum = 0;
  1659. info->temp_old = 0;
  1660. if (!add_cgpu(bitmain))
  1661. goto unshin;
  1662. ret = bitmain_initialize(bitmain);
  1663. applog(LOG_ERR, "bitmain_detect_one stop bitmain_initialize %d", ret);
  1664. if (ret && !configured)
  1665. goto unshin;
  1666. info->errorcount = 0;
  1667. applog(LOG_ERR, "BitMain Detected: %s "
  1668. "(chain_num=%d asic_num=%d timeout=%d freq=%d-%s volt=%02x%02x-%s)",
  1669. bitmain->device_path, info->chain_num, info->asic_num, info->timeout,
  1670. info->frequency, info->frequency_t, info->voltage[0], info->voltage[1], info->voltage_t);
  1671. return true;
  1672. unshin:
  1673. btm_uninit(bitmain);
  1674. shin:
  1675. free(bitmain->device_data);
  1676. bitmain->device_data = NULL;
  1677. free(bitmain);
  1678. return false;
  1679. }
  1680. static void bitmain_detect()
  1681. {
  1682. applog(LOG_DEBUG, "BTM detect dev: %s", opt_bitmain_dev);
  1683. if (strlen(opt_bitmain_dev) > 0) {
  1684. btm_detect(&bitmain_drv, bitmain_detect_one);
  1685. }
  1686. }
  1687. static void do_bitmain_close(struct thr_info *thr)
  1688. {
  1689. struct cgpu_info *bitmain = thr->cgpu;
  1690. struct bitmain_info *info = bitmain->device_data;
  1691. pthread_join(info->read_thr, NULL);
  1692. bitmain_running_reset(bitmain, info);
  1693. info->no_matching_work = 0;
  1694. }
  1695. /* We use a replacement algorithm to only remove references to work done from
  1696. * the buffer when we need the extra space for new work. */
  1697. static bool bitmain_fill(struct cgpu_info *bitmain)
  1698. {
  1699. struct bitmain_info *info = bitmain->device_data;
  1700. int subid, slot;
  1701. struct work *work;
  1702. bool ret = true;
  1703. int sendret = 0, sendcount = 0, neednum = 0, queuednum = 0, sendnum = 0, sendlen = 0;
  1704. uint8_t sendbuf[BITMAIN_SENDBUF_SIZE];
  1705. int senderror = 0;
  1706. struct timeval now;
  1707. int timediff = 0;
  1708. //applog(LOG_DEBUG, "BTM bitmain_fill start--------");
  1709. mutex_lock(&info->qlock);
  1710. if(info->fifo_space <= 0) {
  1711. //applog(LOG_DEBUG, "BTM bitmain_fill fifo space empty--------");
  1712. ret = true;
  1713. goto out_unlock;
  1714. }
  1715. if (bitmain->queued >= BITMAIN_MAX_WORK_QUEUE_NUM) {
  1716. ret = true;
  1717. } else {
  1718. ret = false;
  1719. }
  1720. while(info->fifo_space > 0) {
  1721. neednum = info->fifo_space<BITMAIN_MAX_WORK_NUM?info->fifo_space:BITMAIN_MAX_WORK_NUM;
  1722. queuednum = bitmain->queued;
  1723. applog(LOG_DEBUG, "BTM: Work task queued(%d) fifo space(%d) needsend(%d)", queuednum, info->fifo_space, neednum);
  1724. if(queuednum < neednum) {
  1725. while(true) {
  1726. work = get_queued(bitmain);
  1727. if (unlikely(!work)) {
  1728. break;
  1729. } else {
  1730. applog(LOG_DEBUG, "BTM get work queued number:%d neednum:%d", queuednum, neednum);
  1731. subid = bitmain->queued++;
  1732. work->subid = subid;
  1733. slot = bitmain->work_array + subid;
  1734. if (slot > BITMAIN_ARRAY_SIZE) {
  1735. applog(LOG_DEBUG, "bitmain_fill array cyc %d", BITMAIN_ARRAY_SIZE);
  1736. slot = 0;
  1737. }
  1738. if (likely(bitmain->works[slot])) {
  1739. applog(LOG_DEBUG, "bitmain_fill work_completed %d", slot);
  1740. work_completed(bitmain, bitmain->works[slot]);
  1741. }
  1742. bitmain->works[slot] = work;
  1743. queuednum++;
  1744. if(queuednum >= neednum) {
  1745. break;
  1746. }
  1747. }
  1748. }
  1749. }
  1750. if(queuednum < BITMAIN_MAX_DEAL_QUEUE_NUM) {
  1751. if(queuednum < neednum) {
  1752. applog(LOG_DEBUG, "BTM: No enough work to send, queue num=%d", queuednum);
  1753. break;
  1754. }
  1755. }
  1756. sendnum = queuednum < neednum ? queuednum : neednum;
  1757. sendlen = bitmain_set_txtask(sendbuf, &(info->last_work_block), bitmain->works, BITMAIN_ARRAY_SIZE, bitmain->work_array, sendnum, &sendcount);
  1758. bitmain->queued -= sendnum;
  1759. info->send_full_space += sendnum;
  1760. if (bitmain->queued < 0)
  1761. bitmain->queued = 0;
  1762. if (bitmain->work_array + sendnum > BITMAIN_ARRAY_SIZE) {
  1763. bitmain->work_array = bitmain->work_array + sendnum-BITMAIN_ARRAY_SIZE;
  1764. } else {
  1765. bitmain->work_array += sendnum;
  1766. }
  1767. applog(LOG_DEBUG, "BTM: Send work array %d", bitmain->work_array);
  1768. if (sendlen > 0) {
  1769. info->fifo_space -= sendcount;
  1770. if (info->fifo_space < 0)
  1771. info->fifo_space = 0;
  1772. sendret = bitmain_send_data(sendbuf, sendlen, bitmain);
  1773. if (unlikely(sendret == BTM_SEND_ERROR)) {
  1774. applog(LOG_ERR, "BTM%i: Comms error(buffer)", bitmain->device_id);
  1775. //dev_error(bitmain, REASON_DEV_COMMS_ERROR);
  1776. info->reset = true;
  1777. info->errorcount++;
  1778. senderror = 1;
  1779. if (info->errorcount > 1000) {
  1780. info->errorcount = 0;
  1781. applog(LOG_ERR, "%s%d: Device disappeared, shutting down thread", bitmain->drv->name, bitmain->device_id);
  1782. bitmain->shutdown = true;
  1783. }
  1784. break;
  1785. } else {
  1786. applog(LOG_DEBUG, "bitmain_send_data send ret=%d", sendret);
  1787. info->errorcount = 0;
  1788. }
  1789. } else {
  1790. applog(LOG_DEBUG, "BTM: Send work bitmain_set_txtask error: %d", sendlen);
  1791. break;
  1792. }
  1793. }
  1794. out_unlock:
  1795. cgtime(&now);
  1796. timediff = now.tv_sec - info->last_status_time.tv_sec;
  1797. if(timediff < 0) timediff = -timediff;
  1798. if (timediff > BITMAIN_SEND_STATUS_TIME) {
  1799. applog(LOG_DEBUG, "BTM: Send RX Status Token fifo_space(%d) timediff(%d)", info->fifo_space, timediff);
  1800. copy_time(&(info->last_status_time), &now);
  1801. sendlen = bitmain_set_rxstatus((struct bitmain_rxstatus_token *) sendbuf, 0, 0, 0, 0);
  1802. if (sendlen > 0) {
  1803. sendret = bitmain_send_data(sendbuf, sendlen, bitmain);
  1804. if (unlikely(sendret == BTM_SEND_ERROR)) {
  1805. applog(LOG_ERR, "BTM%i: Comms error(buffer)", bitmain->device_id);
  1806. //dev_error(bitmain, REASON_DEV_COMMS_ERROR);
  1807. info->reset = true;
  1808. info->errorcount++;
  1809. senderror = 1;
  1810. if (info->errorcount > 1000) {
  1811. info->errorcount = 0;
  1812. applog(LOG_ERR, "%s%d: Device disappeared, shutting down thread", bitmain->drv->name, bitmain->device_id);
  1813. bitmain->shutdown = true;
  1814. }
  1815. } else {
  1816. info->errorcount = 0;
  1817. if (info->fifo_space <= 0) {
  1818. senderror = 1;
  1819. }
  1820. }
  1821. }
  1822. }
  1823. if(info->send_full_space > BITMAIN_SEND_FULL_SPACE) {
  1824. info->send_full_space = 0;
  1825. ret = true;
  1826. cgsleep_ms(1);
  1827. }
  1828. mutex_unlock(&info->qlock);
  1829. if(senderror) {
  1830. ret = true;
  1831. applog(LOG_DEBUG, "bitmain_fill send task sleep");
  1832. //cgsleep_ms(1);
  1833. }
  1834. return ret;
  1835. }
  1836. static int64_t bitmain_scanhash(struct thr_info *thr)
  1837. {
  1838. struct cgpu_info *bitmain = thr->cgpu;
  1839. struct bitmain_info *info = bitmain->device_data;
  1840. const int chain_num = info->chain_num;
  1841. int64_t hash_count;
  1842. //applog(LOG_DEBUG, "bitmain_scanhash info->qlock start");
  1843. mutex_lock(&info->qlock);
  1844. hash_count = 0xffffffffull * (uint64_t)info->nonces;
  1845. bitmain->results += info->nonces + info->idle;
  1846. if (bitmain->results > chain_num)
  1847. bitmain->results = chain_num;
  1848. if (!info->reset)
  1849. bitmain->results--;
  1850. info->nonces = info->idle = 0;
  1851. mutex_unlock(&info->qlock);
  1852. //applog(LOG_DEBUG, "bitmain_scanhash info->qlock stop");
  1853. /* Check for nothing but consecutive bad results or consistently less
  1854. * results than we should be getting and reset the FPGA if necessary */
  1855. //if (bitmain->results < -chain_num && !info->reset) {
  1856. // applog(LOG_ERR, "BTM%d: Result return rate low, resetting!",
  1857. // bitmain->device_id);
  1858. // info->reset = true;
  1859. //}
  1860. /* This hashmeter is just a utility counter based on returned shares */
  1861. return hash_count;
  1862. }
  1863. static void bitmain_flush_work(struct cgpu_info *bitmain)
  1864. {
  1865. struct bitmain_info *info = bitmain->device_data;
  1866. mutex_lock(&info->qlock);
  1867. /* Will overwrite any work queued */
  1868. applog(LOG_ERR, "bitmain_flush_work queued=%d array=%d", bitmain->queued, bitmain->work_array);
  1869. if(bitmain->queued > 0) {
  1870. if (bitmain->work_array + bitmain->queued > BITMAIN_ARRAY_SIZE) {
  1871. bitmain->work_array = bitmain->work_array + bitmain->queued-BITMAIN_ARRAY_SIZE;
  1872. } else {
  1873. bitmain->work_array += bitmain->queued;
  1874. }
  1875. }
  1876. bitmain->queued = 0;
  1877. //bitmain->work_array = 0;
  1878. //for (int i = 0; i < BITMAIN_ARRAY_SIZE; ++i) {
  1879. // bitmain->works[i] = NULL;
  1880. //}
  1881. //pthread_cond_signal(&info->qcond);
  1882. mutex_unlock(&info->qlock);
  1883. }
  1884. static struct api_data *bitmain_api_stats(struct cgpu_info *cgpu)
  1885. {
  1886. struct api_data *root = NULL;
  1887. struct bitmain_info *info = cgpu->device_data;
  1888. double hwp = (cgpu->hw_errors + cgpu->diff1) ?
  1889. (double)(cgpu->hw_errors) / (double)(cgpu->hw_errors + cgpu->diff1) : 0;
  1890. root = api_add_int(root, "baud", &(info->baud), false);
  1891. root = api_add_int(root, "miner_count", &(info->chain_num), false);
  1892. root = api_add_int(root, "asic_count", &(info->asic_num), false);
  1893. root = api_add_int(root, "timeout", &(info->timeout), false);
  1894. root = api_add_string(root, "frequency", info->frequency_t, false);
  1895. root = api_add_string(root, "voltage", info->voltage_t, false);
  1896. root = api_add_int(root, "hwv1", &(info->hw_version[0]), false);
  1897. root = api_add_int(root, "hwv2", &(info->hw_version[1]), false);
  1898. root = api_add_int(root, "hwv3", &(info->hw_version[2]), false);
  1899. root = api_add_int(root, "hwv4", &(info->hw_version[3]), false);
  1900. root = api_add_int(root, "fan_num", &(info->fan_num), false);
  1901. root = api_add_int(root, "fan1", &(info->fan[0]), false);
  1902. root = api_add_int(root, "fan2", &(info->fan[1]), false);
  1903. root = api_add_int(root, "fan3", &(info->fan[2]), false);
  1904. root = api_add_int(root, "fan4", &(info->fan[3]), false);
  1905. root = api_add_int(root, "fan5", &(info->fan[4]), false);
  1906. root = api_add_int(root, "fan6", &(info->fan[5]), false);
  1907. root = api_add_int(root, "fan7", &(info->fan[6]), false);
  1908. root = api_add_int(root, "fan8", &(info->fan[7]), false);
  1909. root = api_add_int(root, "fan9", &(info->fan[8]), false);
  1910. root = api_add_int(root, "fan10", &(info->fan[9]), false);
  1911. root = api_add_int(root, "fan11", &(info->fan[10]), false);
  1912. root = api_add_int(root, "fan12", &(info->fan[11]), false);
  1913. root = api_add_int(root, "fan13", &(info->fan[12]), false);
  1914. root = api_add_int(root, "fan14", &(info->fan[13]), false);
  1915. root = api_add_int(root, "fan15", &(info->fan[14]), false);
  1916. root = api_add_int(root, "fan16", &(info->fan[15]), false);
  1917. root = api_add_int(root, "temp_num", &(info->temp_num), false);
  1918. root = api_add_int(root, "temp1", &(info->temp[0]), false);
  1919. root = api_add_int(root, "temp2", &(info->temp[1]), false);
  1920. root = api_add_int(root, "temp3", &(info->temp[2]), false);
  1921. root = api_add_int(root, "temp4", &(info->temp[3]), false);
  1922. root = api_add_int(root, "temp5", &(info->temp[4]), false);
  1923. root = api_add_int(root, "temp6", &(info->temp[5]), false);
  1924. root = api_add_int(root, "temp7", &(info->temp[6]), false);
  1925. root = api_add_int(root, "temp8", &(info->temp[7]), false);
  1926. root = api_add_int(root, "temp9", &(info->temp[8]), false);
  1927. root = api_add_int(root, "temp10", &(info->temp[9]), false);
  1928. root = api_add_int(root, "temp11", &(info->temp[10]), false);
  1929. root = api_add_int(root, "temp12", &(info->temp[11]), false);
  1930. root = api_add_int(root, "temp13", &(info->temp[12]), false);
  1931. root = api_add_int(root, "temp14", &(info->temp[13]), false);
  1932. root = api_add_int(root, "temp15", &(info->temp[14]), false);
  1933. root = api_add_int(root, "temp16", &(info->temp[15]), false);
  1934. root = api_add_int(root, "temp_avg", &(info->temp_avg), false);
  1935. root = api_add_int(root, "temp_max", &(info->temp_max), false);
  1936. root = api_add_percent(root, "Device Hardware%", &hwp, true);
  1937. root = api_add_int(root, "no_matching_work", &(info->no_matching_work), false);
  1938. /*
  1939. for (int i = 0; i < info->chain_num; ++i) {
  1940. char mcw[24];
  1941. sprintf(mcw, "match_work_count%d", i + 1);
  1942. root = api_add_int(root, mcw, &(info->matching_work[i]), false);
  1943. }*/
  1944. root = api_add_int(root, "chain_acn1", &(info->chain_asic_num[0]), false);
  1945. root = api_add_int(root, "chain_acn2", &(info->chain_asic_num[1]), false);
  1946. root = api_add_int(root, "chain_acn3", &(info->chain_asic_num[2]), false);
  1947. root = api_add_int(root, "chain_acn4", &(info->chain_asic_num[3]), false);
  1948. root = api_add_int(root, "chain_acn5", &(info->chain_asic_num[4]), false);
  1949. root = api_add_int(root, "chain_acn6", &(info->chain_asic_num[5]), false);
  1950. root = api_add_int(root, "chain_acn7", &(info->chain_asic_num[6]), false);
  1951. root = api_add_int(root, "chain_acn8", &(info->chain_asic_num[7]), false);
  1952. root = api_add_int(root, "chain_acn9", &(info->chain_asic_num[8]), false);
  1953. root = api_add_int(root, "chain_acn10", &(info->chain_asic_num[9]), false);
  1954. root = api_add_int(root, "chain_acn11", &(info->chain_asic_num[10]), false);
  1955. root = api_add_int(root, "chain_acn12", &(info->chain_asic_num[11]), false);
  1956. root = api_add_int(root, "chain_acn13", &(info->chain_asic_num[12]), false);
  1957. root = api_add_int(root, "chain_acn14", &(info->chain_asic_num[13]), false);
  1958. root = api_add_int(root, "chain_acn15", &(info->chain_asic_num[14]), false);
  1959. root = api_add_int(root, "chain_acn16", &(info->chain_asic_num[15]), false);
  1960. //applog(LOG_ERR, "chain asic status:%s", info->chain_asic_status_t[0]);
  1961. root = api_add_string(root, "chain_acs1", info->chain_asic_status_t[0], false);
  1962. root = api_add_string(root, "chain_acs2", info->chain_asic_status_t[1], false);
  1963. root = api_add_string(root, "chain_acs3", info->chain_asic_status_t[2], false);
  1964. root = api_add_string(root, "chain_acs4", info->chain_asic_status_t[3], false);
  1965. root = api_add_string(root, "chain_acs5", info->chain_asic_status_t[4], false);
  1966. root = api_add_string(root, "chain_acs6", info->chain_asic_status_t[5], false);
  1967. root = api_add_string(root, "chain_acs7", info->chain_asic_status_t[6], false);
  1968. root = api_add_string(root, "chain_acs8", info->chain_asic_status_t[7], false);
  1969. root = api_add_string(root, "chain_acs9", info->chain_asic_status_t[8], false);
  1970. root = api_add_string(root, "chain_acs10", info->chain_asic_status_t[9], false);
  1971. root = api_add_string(root, "chain_acs11", info->chain_asic_status_t[10], false);
  1972. root = api_add_string(root, "chain_acs12", info->chain_asic_status_t[11], false);
  1973. root = api_add_string(root, "chain_acs13", info->chain_asic_status_t[12], false);
  1974. root = api_add_string(root, "chain_acs14", info->chain_asic_status_t[13], false);
  1975. root = api_add_string(root, "chain_acs15", info->chain_asic_status_t[14], false);
  1976. root = api_add_string(root, "chain_acs16", info->chain_asic_status_t[15], false);
  1977. //root = api_add_int(root, "chain_acs1", &(info->chain_asic_status[0]), false);
  1978. //root = api_add_int(root, "chain_acs2", &(info->chain_asic_status[1]), false);
  1979. //root = api_add_int(root, "chain_acs3", &(info->chain_asic_status[2]), false);
  1980. //root = api_add_int(root, "chain_acs4", &(info->chain_asic_status[3]), false);
  1981. return root;
  1982. }
  1983. static void bitmain_shutdown(struct thr_info *thr)
  1984. {
  1985. do_bitmain_close(thr);
  1986. }
  1987. char *set_bitmain_dev(char *arg)
  1988. {
  1989. if(arg == NULL || strlen(arg) <= 0) {
  1990. opt_bitmain_dev[0] = '\0';
  1991. } else {
  1992. strncpy(opt_bitmain_dev, arg, 256);
  1993. }
  1994. applog(LOG_DEBUG, "BTM set device: %s", opt_bitmain_dev);
  1995. return NULL;
  1996. }
  1997. char *set_bitmain_fan(char *arg)
  1998. {
  1999. int val1, val2, ret;
  2000. ret = sscanf(arg, "%d-%d", &val1, &val2);
  2001. if (ret < 1)
  2002. return "No values passed to bitmain-fan";
  2003. if (ret == 1)
  2004. val2 = val1;
  2005. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
  2006. return "Invalid value passed to bitmain-fan";
  2007. opt_bitmain_fan_min = val1 * BITMAIN_PWM_MAX / 100;
  2008. opt_bitmain_fan_max = val2 * BITMAIN_PWM_MAX / 100;
  2009. return NULL;
  2010. }
  2011. char *set_bitmain_freq(char *arg)
  2012. {
  2013. int val1, val2, ret;
  2014. ret = sscanf(arg, "%d-%d", &val1, &val2);
  2015. if (ret < 1)
  2016. return "No values passed to bitmain-freq";
  2017. if (ret == 1)
  2018. val2 = val1;
  2019. if (val1 < BITMAIN_MIN_FREQUENCY || val1 > BITMAIN_MAX_FREQUENCY ||
  2020. val2 < BITMAIN_MIN_FREQUENCY || val2 > BITMAIN_MAX_FREQUENCY ||
  2021. val2 < val1)
  2022. return "Invalid value passed to bitmain-freq";
  2023. opt_bitmain_freq_min = val1;
  2024. opt_bitmain_freq_max = val2;
  2025. return NULL;
  2026. }
  2027. struct device_drv bitmain_drv = {
  2028. .dname = "bitmain",
  2029. .name = "BTM",
  2030. .drv_detect = bitmain_detect,
  2031. .thread_prepare = bitmain_prepare,
  2032. .minerloop = hash_queued_work,
  2033. .queue_full = bitmain_fill,
  2034. .scanwork = bitmain_scanhash,
  2035. .flush_work = bitmain_flush_work,
  2036. .get_api_stats = bitmain_api_stats,
  2037. .reinit_device = bitmain_init,
  2038. .thread_shutdown = bitmain_shutdown,
  2039. };