driver-bitmain.c 69 KB

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