pcnet.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752
  1. /*
  2. * QEMU AMD PC-Net II (Am79C970A) emulation
  3. *
  4. * Copyright (c) 2004 Antony T Curtis
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. /* This software was written to be compatible with the specification:
  25. * AMD Am79C970A PCnet-PCI II Ethernet Controller Data-Sheet
  26. * AMD Publication# 19436 Rev:E Amendment/0 Issue Date: June 2000
  27. */
  28. /*
  29. * On Sparc32, this is the Lance (Am7990) part of chip STP2000 (Master I/O), also
  30. * produced as NCR89C100. See
  31. * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C100.txt
  32. * and
  33. * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR92C990.txt
  34. */
  35. #include "qemu/osdep.h"
  36. #include "qemu/log.h"
  37. #include "hw/irq.h"
  38. #include "hw/qdev-properties.h"
  39. #include "migration/vmstate.h"
  40. #include "net/net.h"
  41. #include "net/eth.h"
  42. #include "qemu/timer.h"
  43. #include "trace.h"
  44. #include "pcnet.h"
  45. //#define PCNET_DEBUG
  46. //#define PCNET_DEBUG_IO
  47. //#define PCNET_DEBUG_BCR
  48. //#define PCNET_DEBUG_CSR
  49. //#define PCNET_DEBUG_RMD
  50. //#define PCNET_DEBUG_TMD
  51. //#define PCNET_DEBUG_MATCH
  52. struct qemu_ether_header {
  53. uint8_t ether_dhost[6];
  54. uint8_t ether_shost[6];
  55. uint16_t ether_type;
  56. };
  57. #define CSR_INIT(S) !!(((S)->csr[0])&0x0001)
  58. #define CSR_STRT(S) !!(((S)->csr[0])&0x0002)
  59. #define CSR_STOP(S) !!(((S)->csr[0])&0x0004)
  60. #define CSR_TDMD(S) !!(((S)->csr[0])&0x0008)
  61. #define CSR_TXON(S) !!(((S)->csr[0])&0x0010)
  62. #define CSR_RXON(S) !!(((S)->csr[0])&0x0020)
  63. #define CSR_INEA(S) !!(((S)->csr[0])&0x0040)
  64. #define CSR_BSWP(S) !!(((S)->csr[3])&0x0004)
  65. #define CSR_LAPPEN(S) !!(((S)->csr[3])&0x0020)
  66. #define CSR_DXSUFLO(S) !!(((S)->csr[3])&0x0040)
  67. #define CSR_ASTRP_RCV(S) !!(((S)->csr[4])&0x0800)
  68. #define CSR_DPOLL(S) !!(((S)->csr[4])&0x1000)
  69. #define CSR_SPND(S) !!(((S)->csr[5])&0x0001)
  70. #define CSR_LTINTEN(S) !!(((S)->csr[5])&0x4000)
  71. #define CSR_TOKINTD(S) !!(((S)->csr[5])&0x8000)
  72. #define CSR_DRX(S) !!(((S)->csr[15])&0x0001)
  73. #define CSR_DTX(S) !!(((S)->csr[15])&0x0002)
  74. #define CSR_LOOP(S) !!(((S)->csr[15])&0x0004)
  75. #define CSR_DXMTFCS(S) !!(((S)->csr[15])&0x0008)
  76. #define CSR_INTL(S) !!(((S)->csr[15])&0x0040)
  77. #define CSR_DRCVPA(S) !!(((S)->csr[15])&0x2000)
  78. #define CSR_DRCVBC(S) !!(((S)->csr[15])&0x4000)
  79. #define CSR_PROM(S) !!(((S)->csr[15])&0x8000)
  80. #define CSR_CRBC(S) ((S)->csr[40])
  81. #define CSR_CRST(S) ((S)->csr[41])
  82. #define CSR_CXBC(S) ((S)->csr[42])
  83. #define CSR_CXST(S) ((S)->csr[43])
  84. #define CSR_NRBC(S) ((S)->csr[44])
  85. #define CSR_NRST(S) ((S)->csr[45])
  86. #define CSR_POLL(S) ((S)->csr[46])
  87. #define CSR_PINT(S) ((S)->csr[47])
  88. #define CSR_RCVRC(S) ((S)->csr[72])
  89. #define CSR_XMTRC(S) ((S)->csr[74])
  90. #define CSR_RCVRL(S) ((S)->csr[76])
  91. #define CSR_XMTRL(S) ((S)->csr[78])
  92. #define CSR_MISSC(S) ((S)->csr[112])
  93. #define CSR_IADR(S) ((S)->csr[ 1] | ((uint32_t)(S)->csr[ 2] << 16))
  94. #define CSR_CRBA(S) ((S)->csr[18] | ((uint32_t)(S)->csr[19] << 16))
  95. #define CSR_CXBA(S) ((S)->csr[20] | ((uint32_t)(S)->csr[21] << 16))
  96. #define CSR_NRBA(S) ((S)->csr[22] | ((uint32_t)(S)->csr[23] << 16))
  97. #define CSR_BADR(S) ((S)->csr[24] | ((uint32_t)(S)->csr[25] << 16))
  98. #define CSR_NRDA(S) ((S)->csr[26] | ((uint32_t)(S)->csr[27] << 16))
  99. #define CSR_CRDA(S) ((S)->csr[28] | ((uint32_t)(S)->csr[29] << 16))
  100. #define CSR_BADX(S) ((S)->csr[30] | ((uint32_t)(S)->csr[31] << 16))
  101. #define CSR_NXDA(S) ((S)->csr[32] | ((uint32_t)(S)->csr[33] << 16))
  102. #define CSR_CXDA(S) ((S)->csr[34] | ((uint32_t)(S)->csr[35] << 16))
  103. #define CSR_NNRD(S) ((S)->csr[36] | ((uint32_t)(S)->csr[37] << 16))
  104. #define CSR_NNXD(S) ((S)->csr[38] | ((uint32_t)(S)->csr[39] << 16))
  105. #define CSR_PXDA(S) ((S)->csr[60] | ((uint32_t)(S)->csr[61] << 16))
  106. #define CSR_NXBA(S) ((S)->csr[64] | ((uint32_t)(S)->csr[65] << 16))
  107. #define PHYSADDR(S,A) \
  108. (BCR_SSIZE32(S) ? (A) : (A) | ((0xff00 & (uint32_t)(S)->csr[2])<<16))
  109. struct pcnet_initblk16 {
  110. uint16_t mode;
  111. uint16_t padr[3];
  112. uint16_t ladrf[4];
  113. uint32_t rdra;
  114. uint32_t tdra;
  115. };
  116. struct pcnet_initblk32 {
  117. uint16_t mode;
  118. uint8_t rlen;
  119. uint8_t tlen;
  120. uint16_t padr[3];
  121. uint16_t _res;
  122. uint16_t ladrf[4];
  123. uint32_t rdra;
  124. uint32_t tdra;
  125. };
  126. struct pcnet_TMD {
  127. uint32_t tbadr;
  128. int16_t length;
  129. int16_t status;
  130. uint32_t misc;
  131. uint32_t res;
  132. };
  133. #define TMDL_BCNT_MASK 0x0fff
  134. #define TMDL_BCNT_SH 0
  135. #define TMDL_ONES_MASK 0xf000
  136. #define TMDL_ONES_SH 12
  137. #define TMDS_BPE_MASK 0x0080
  138. #define TMDS_BPE_SH 7
  139. #define TMDS_ENP_MASK 0x0100
  140. #define TMDS_ENP_SH 8
  141. #define TMDS_STP_MASK 0x0200
  142. #define TMDS_STP_SH 9
  143. #define TMDS_DEF_MASK 0x0400
  144. #define TMDS_DEF_SH 10
  145. #define TMDS_ONE_MASK 0x0800
  146. #define TMDS_ONE_SH 11
  147. #define TMDS_LTINT_MASK 0x1000
  148. #define TMDS_LTINT_SH 12
  149. #define TMDS_NOFCS_MASK 0x2000
  150. #define TMDS_NOFCS_SH 13
  151. #define TMDS_ADDFCS_MASK TMDS_NOFCS_MASK
  152. #define TMDS_ADDFCS_SH TMDS_NOFCS_SH
  153. #define TMDS_ERR_MASK 0x4000
  154. #define TMDS_ERR_SH 14
  155. #define TMDS_OWN_MASK 0x8000
  156. #define TMDS_OWN_SH 15
  157. #define TMDM_TRC_MASK 0x0000000f
  158. #define TMDM_TRC_SH 0
  159. #define TMDM_TDR_MASK 0x03ff0000
  160. #define TMDM_TDR_SH 16
  161. #define TMDM_RTRY_MASK 0x04000000
  162. #define TMDM_RTRY_SH 26
  163. #define TMDM_LCAR_MASK 0x08000000
  164. #define TMDM_LCAR_SH 27
  165. #define TMDM_LCOL_MASK 0x10000000
  166. #define TMDM_LCOL_SH 28
  167. #define TMDM_EXDEF_MASK 0x20000000
  168. #define TMDM_EXDEF_SH 29
  169. #define TMDM_UFLO_MASK 0x40000000
  170. #define TMDM_UFLO_SH 30
  171. #define TMDM_BUFF_MASK 0x80000000
  172. #define TMDM_BUFF_SH 31
  173. struct pcnet_RMD {
  174. uint32_t rbadr;
  175. int16_t buf_length;
  176. int16_t status;
  177. uint32_t msg_length;
  178. uint32_t res;
  179. };
  180. #define RMDL_BCNT_MASK 0x0fff
  181. #define RMDL_BCNT_SH 0
  182. #define RMDL_ONES_MASK 0xf000
  183. #define RMDL_ONES_SH 12
  184. #define RMDS_BAM_MASK 0x0010
  185. #define RMDS_BAM_SH 4
  186. #define RMDS_LFAM_MASK 0x0020
  187. #define RMDS_LFAM_SH 5
  188. #define RMDS_PAM_MASK 0x0040
  189. #define RMDS_PAM_SH 6
  190. #define RMDS_BPE_MASK 0x0080
  191. #define RMDS_BPE_SH 7
  192. #define RMDS_ENP_MASK 0x0100
  193. #define RMDS_ENP_SH 8
  194. #define RMDS_STP_MASK 0x0200
  195. #define RMDS_STP_SH 9
  196. #define RMDS_BUFF_MASK 0x0400
  197. #define RMDS_BUFF_SH 10
  198. #define RMDS_CRC_MASK 0x0800
  199. #define RMDS_CRC_SH 11
  200. #define RMDS_OFLO_MASK 0x1000
  201. #define RMDS_OFLO_SH 12
  202. #define RMDS_FRAM_MASK 0x2000
  203. #define RMDS_FRAM_SH 13
  204. #define RMDS_ERR_MASK 0x4000
  205. #define RMDS_ERR_SH 14
  206. #define RMDS_OWN_MASK 0x8000
  207. #define RMDS_OWN_SH 15
  208. #define RMDM_MCNT_MASK 0x00000fff
  209. #define RMDM_MCNT_SH 0
  210. #define RMDM_ZEROS_MASK 0x0000f000
  211. #define RMDM_ZEROS_SH 12
  212. #define RMDM_RPC_MASK 0x00ff0000
  213. #define RMDM_RPC_SH 16
  214. #define RMDM_RCC_MASK 0xff000000
  215. #define RMDM_RCC_SH 24
  216. #define SET_FIELD(regp, name, field, value) \
  217. (*(regp) = (*(regp) & ~(name ## _ ## field ## _MASK)) \
  218. | ((value) << name ## _ ## field ## _SH))
  219. #define GET_FIELD(reg, name, field) \
  220. (((reg) & name ## _ ## field ## _MASK) >> name ## _ ## field ## _SH)
  221. #define PRINT_TMD(T) printf( \
  222. "TMD0 : TBADR=0x%08x\n" \
  223. "TMD1 : OWN=%d, ERR=%d, FCS=%d, LTI=%d, " \
  224. "ONE=%d, DEF=%d, STP=%d, ENP=%d,\n" \
  225. " BPE=%d, BCNT=%d\n" \
  226. "TMD2 : BUF=%d, UFL=%d, EXD=%d, LCO=%d, " \
  227. "LCA=%d, RTR=%d,\n" \
  228. " TDR=%d, TRC=%d\n", \
  229. (T)->tbadr, \
  230. GET_FIELD((T)->status, TMDS, OWN), \
  231. GET_FIELD((T)->status, TMDS, ERR), \
  232. GET_FIELD((T)->status, TMDS, NOFCS), \
  233. GET_FIELD((T)->status, TMDS, LTINT), \
  234. GET_FIELD((T)->status, TMDS, ONE), \
  235. GET_FIELD((T)->status, TMDS, DEF), \
  236. GET_FIELD((T)->status, TMDS, STP), \
  237. GET_FIELD((T)->status, TMDS, ENP), \
  238. GET_FIELD((T)->status, TMDS, BPE), \
  239. 4096-GET_FIELD((T)->length, TMDL, BCNT), \
  240. GET_FIELD((T)->misc, TMDM, BUFF), \
  241. GET_FIELD((T)->misc, TMDM, UFLO), \
  242. GET_FIELD((T)->misc, TMDM, EXDEF), \
  243. GET_FIELD((T)->misc, TMDM, LCOL), \
  244. GET_FIELD((T)->misc, TMDM, LCAR), \
  245. GET_FIELD((T)->misc, TMDM, RTRY), \
  246. GET_FIELD((T)->misc, TMDM, TDR), \
  247. GET_FIELD((T)->misc, TMDM, TRC))
  248. #define PRINT_RMD(R) printf( \
  249. "RMD0 : RBADR=0x%08x\n" \
  250. "RMD1 : OWN=%d, ERR=%d, FRAM=%d, OFLO=%d, " \
  251. "CRC=%d, BUFF=%d, STP=%d, ENP=%d,\n " \
  252. "BPE=%d, PAM=%d, LAFM=%d, BAM=%d, ONES=%d, BCNT=%d\n" \
  253. "RMD2 : RCC=%d, RPC=%d, MCNT=%d, ZEROS=%d\n", \
  254. (R)->rbadr, \
  255. GET_FIELD((R)->status, RMDS, OWN), \
  256. GET_FIELD((R)->status, RMDS, ERR), \
  257. GET_FIELD((R)->status, RMDS, FRAM), \
  258. GET_FIELD((R)->status, RMDS, OFLO), \
  259. GET_FIELD((R)->status, RMDS, CRC), \
  260. GET_FIELD((R)->status, RMDS, BUFF), \
  261. GET_FIELD((R)->status, RMDS, STP), \
  262. GET_FIELD((R)->status, RMDS, ENP), \
  263. GET_FIELD((R)->status, RMDS, BPE), \
  264. GET_FIELD((R)->status, RMDS, PAM), \
  265. GET_FIELD((R)->status, RMDS, LFAM), \
  266. GET_FIELD((R)->status, RMDS, BAM), \
  267. GET_FIELD((R)->buf_length, RMDL, ONES), \
  268. 4096-GET_FIELD((R)->buf_length, RMDL, BCNT), \
  269. GET_FIELD((R)->msg_length, RMDM, RCC), \
  270. GET_FIELD((R)->msg_length, RMDM, RPC), \
  271. GET_FIELD((R)->msg_length, RMDM, MCNT), \
  272. GET_FIELD((R)->msg_length, RMDM, ZEROS))
  273. static inline void pcnet_tmd_load(PCNetState *s, struct pcnet_TMD *tmd,
  274. hwaddr addr)
  275. {
  276. if (!BCR_SSIZE32(s)) {
  277. struct {
  278. uint32_t tbadr;
  279. int16_t length;
  280. int16_t status;
  281. } xda;
  282. s->phys_mem_read(s->dma_opaque, addr, (void *)&xda, sizeof(xda), 0);
  283. tmd->tbadr = le32_to_cpu(xda.tbadr) & 0xffffff;
  284. tmd->length = le16_to_cpu(xda.length);
  285. tmd->status = (le32_to_cpu(xda.tbadr) >> 16) & 0xff00;
  286. tmd->misc = le16_to_cpu(xda.status) << 16;
  287. tmd->res = 0;
  288. } else {
  289. s->phys_mem_read(s->dma_opaque, addr, (void *)tmd, sizeof(*tmd), 0);
  290. le32_to_cpus(&tmd->tbadr);
  291. le16_to_cpus((uint16_t *)&tmd->length);
  292. le16_to_cpus((uint16_t *)&tmd->status);
  293. le32_to_cpus(&tmd->misc);
  294. le32_to_cpus(&tmd->res);
  295. if (BCR_SWSTYLE(s) == 3) {
  296. uint32_t tmp = tmd->tbadr;
  297. tmd->tbadr = tmd->misc;
  298. tmd->misc = tmp;
  299. }
  300. }
  301. }
  302. static inline void pcnet_tmd_store(PCNetState *s, const struct pcnet_TMD *tmd,
  303. hwaddr addr)
  304. {
  305. if (!BCR_SSIZE32(s)) {
  306. struct {
  307. uint32_t tbadr;
  308. int16_t length;
  309. int16_t status;
  310. } xda;
  311. xda.tbadr = cpu_to_le32((tmd->tbadr & 0xffffff) |
  312. ((tmd->status & 0xff00) << 16));
  313. xda.length = cpu_to_le16(tmd->length);
  314. xda.status = cpu_to_le16(tmd->misc >> 16);
  315. s->phys_mem_write(s->dma_opaque, addr, (void *)&xda, sizeof(xda), 0);
  316. } else {
  317. struct {
  318. uint32_t tbadr;
  319. int16_t length;
  320. int16_t status;
  321. uint32_t misc;
  322. uint32_t res;
  323. } xda;
  324. xda.tbadr = cpu_to_le32(tmd->tbadr);
  325. xda.length = cpu_to_le16(tmd->length);
  326. xda.status = cpu_to_le16(tmd->status);
  327. xda.misc = cpu_to_le32(tmd->misc);
  328. xda.res = cpu_to_le32(tmd->res);
  329. if (BCR_SWSTYLE(s) == 3) {
  330. uint32_t tmp = xda.tbadr;
  331. xda.tbadr = xda.misc;
  332. xda.misc = tmp;
  333. }
  334. s->phys_mem_write(s->dma_opaque, addr, (void *)&xda, sizeof(xda), 0);
  335. }
  336. }
  337. static inline void pcnet_rmd_load(PCNetState *s, struct pcnet_RMD *rmd,
  338. hwaddr addr)
  339. {
  340. if (!BCR_SSIZE32(s)) {
  341. struct {
  342. uint32_t rbadr;
  343. int16_t buf_length;
  344. int16_t msg_length;
  345. } rda;
  346. s->phys_mem_read(s->dma_opaque, addr, (void *)&rda, sizeof(rda), 0);
  347. rmd->rbadr = le32_to_cpu(rda.rbadr) & 0xffffff;
  348. rmd->buf_length = le16_to_cpu(rda.buf_length);
  349. rmd->status = (le32_to_cpu(rda.rbadr) >> 16) & 0xff00;
  350. rmd->msg_length = le16_to_cpu(rda.msg_length);
  351. rmd->res = 0;
  352. } else {
  353. s->phys_mem_read(s->dma_opaque, addr, (void *)rmd, sizeof(*rmd), 0);
  354. le32_to_cpus(&rmd->rbadr);
  355. le16_to_cpus((uint16_t *)&rmd->buf_length);
  356. le16_to_cpus((uint16_t *)&rmd->status);
  357. le32_to_cpus(&rmd->msg_length);
  358. le32_to_cpus(&rmd->res);
  359. if (BCR_SWSTYLE(s) == 3) {
  360. uint32_t tmp = rmd->rbadr;
  361. rmd->rbadr = rmd->msg_length;
  362. rmd->msg_length = tmp;
  363. }
  364. }
  365. }
  366. static inline void pcnet_rmd_store(PCNetState *s, struct pcnet_RMD *rmd,
  367. hwaddr addr)
  368. {
  369. if (!BCR_SSIZE32(s)) {
  370. struct {
  371. uint32_t rbadr;
  372. int16_t buf_length;
  373. int16_t msg_length;
  374. } rda;
  375. rda.rbadr = cpu_to_le32((rmd->rbadr & 0xffffff) |
  376. ((rmd->status & 0xff00) << 16));
  377. rda.buf_length = cpu_to_le16(rmd->buf_length);
  378. rda.msg_length = cpu_to_le16(rmd->msg_length);
  379. s->phys_mem_write(s->dma_opaque, addr, (void *)&rda, sizeof(rda), 0);
  380. } else {
  381. struct {
  382. uint32_t rbadr;
  383. int16_t buf_length;
  384. int16_t status;
  385. uint32_t msg_length;
  386. uint32_t res;
  387. } rda;
  388. rda.rbadr = cpu_to_le32(rmd->rbadr);
  389. rda.buf_length = cpu_to_le16(rmd->buf_length);
  390. rda.status = cpu_to_le16(rmd->status);
  391. rda.msg_length = cpu_to_le32(rmd->msg_length);
  392. rda.res = cpu_to_le32(rmd->res);
  393. if (BCR_SWSTYLE(s) == 3) {
  394. uint32_t tmp = rda.rbadr;
  395. rda.rbadr = rda.msg_length;
  396. rda.msg_length = tmp;
  397. }
  398. s->phys_mem_write(s->dma_opaque, addr, (void *)&rda, sizeof(rda), 0);
  399. }
  400. }
  401. #define TMDLOAD(TMD,ADDR) pcnet_tmd_load(s,TMD,ADDR)
  402. #define TMDSTORE(TMD,ADDR) pcnet_tmd_store(s,TMD,ADDR)
  403. #define RMDLOAD(RMD,ADDR) pcnet_rmd_load(s,RMD,ADDR)
  404. #define RMDSTORE(RMD,ADDR) pcnet_rmd_store(s,RMD,ADDR)
  405. #if 1
  406. #define CHECK_RMD(ADDR,RES) do { \
  407. struct pcnet_RMD rmd; \
  408. RMDLOAD(&rmd,(ADDR)); \
  409. (RES) |= (GET_FIELD(rmd.buf_length, RMDL, ONES) != 15) \
  410. || (GET_FIELD(rmd.msg_length, RMDM, ZEROS) != 0); \
  411. } while (0)
  412. #define CHECK_TMD(ADDR,RES) do { \
  413. struct pcnet_TMD tmd; \
  414. TMDLOAD(&tmd,(ADDR)); \
  415. (RES) |= (GET_FIELD(tmd.length, TMDL, ONES) != 15); \
  416. } while (0)
  417. #else
  418. #define CHECK_RMD(ADDR,RES) do { \
  419. switch (BCR_SWSTYLE(s)) { \
  420. case 0x00: \
  421. { \
  422. uint16_t rda[4]; \
  423. s->phys_mem_read(s->dma_opaque, (ADDR), \
  424. (void *)&rda[0], sizeof(rda), 0); \
  425. (RES) |= (rda[2] & 0xf000)!=0xf000; \
  426. (RES) |= (rda[3] & 0xf000)!=0x0000; \
  427. } \
  428. break; \
  429. case 0x01: \
  430. case 0x02: \
  431. { \
  432. uint32_t rda[4]; \
  433. s->phys_mem_read(s->dma_opaque, (ADDR), \
  434. (void *)&rda[0], sizeof(rda), 0); \
  435. (RES) |= (rda[1] & 0x0000f000L)!=0x0000f000L; \
  436. (RES) |= (rda[2] & 0x0000f000L)!=0x00000000L; \
  437. } \
  438. break; \
  439. case 0x03: \
  440. { \
  441. uint32_t rda[4]; \
  442. s->phys_mem_read(s->dma_opaque, (ADDR), \
  443. (void *)&rda[0], sizeof(rda), 0); \
  444. (RES) |= (rda[0] & 0x0000f000L)!=0x00000000L; \
  445. (RES) |= (rda[1] & 0x0000f000L)!=0x0000f000L; \
  446. } \
  447. break; \
  448. } \
  449. } while (0)
  450. #define CHECK_TMD(ADDR,RES) do { \
  451. switch (BCR_SWSTYLE(s)) { \
  452. case 0x00: \
  453. { \
  454. uint16_t xda[4]; \
  455. s->phys_mem_read(s->dma_opaque, (ADDR), \
  456. (void *)&xda[0], sizeof(xda), 0); \
  457. (RES) |= (xda[2] & 0xf000)!=0xf000; \
  458. } \
  459. break; \
  460. case 0x01: \
  461. case 0x02: \
  462. case 0x03: \
  463. { \
  464. uint32_t xda[4]; \
  465. s->phys_mem_read(s->dma_opaque, (ADDR), \
  466. (void *)&xda[0], sizeof(xda), 0); \
  467. (RES) |= (xda[1] & 0x0000f000L)!=0x0000f000L; \
  468. } \
  469. break; \
  470. } \
  471. } while (0)
  472. #endif
  473. #define PRINT_PKTHDR(BUF) do { \
  474. struct qemu_ether_header *hdr = (void *)(BUF); \
  475. printf("packet dhost=%02x:%02x:%02x:%02x:%02x:%02x, " \
  476. "shost=%02x:%02x:%02x:%02x:%02x:%02x, " \
  477. "type=0x%04x\n", \
  478. hdr->ether_dhost[0],hdr->ether_dhost[1],hdr->ether_dhost[2], \
  479. hdr->ether_dhost[3],hdr->ether_dhost[4],hdr->ether_dhost[5], \
  480. hdr->ether_shost[0],hdr->ether_shost[1],hdr->ether_shost[2], \
  481. hdr->ether_shost[3],hdr->ether_shost[4],hdr->ether_shost[5], \
  482. be16_to_cpu(hdr->ether_type)); \
  483. } while (0)
  484. #define CRC(crc, ch) (crc = (crc >> 8) ^ crctab[(crc ^ (ch)) & 0xff])
  485. /* generated using the AUTODIN II polynomial
  486. * x^32 + x^26 + x^23 + x^22 + x^16 +
  487. * x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x^1 + 1
  488. */
  489. static const uint32_t crctab[256] = {
  490. 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
  491. 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
  492. 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
  493. 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
  494. 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
  495. 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
  496. 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,
  497. 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
  498. 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
  499. 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
  500. 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940,
  501. 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
  502. 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116,
  503. 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
  504. 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
  505. 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
  506. 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a,
  507. 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
  508. 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818,
  509. 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
  510. 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
  511. 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
  512. 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c,
  513. 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
  514. 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
  515. 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
  516. 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
  517. 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
  518. 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086,
  519. 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
  520. 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4,
  521. 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
  522. 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
  523. 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
  524. 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
  525. 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
  526. 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe,
  527. 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
  528. 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
  529. 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
  530. 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252,
  531. 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
  532. 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60,
  533. 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
  534. 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
  535. 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
  536. 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04,
  537. 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
  538. 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a,
  539. 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
  540. 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
  541. 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
  542. 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e,
  543. 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
  544. 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
  545. 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
  546. 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
  547. 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
  548. 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0,
  549. 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
  550. 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
  551. 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
  552. 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
  553. 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
  554. };
  555. static inline int padr_match(PCNetState *s, const uint8_t *buf, int size)
  556. {
  557. struct qemu_ether_header *hdr = (void *)buf;
  558. uint8_t padr[6] = {
  559. s->csr[12] & 0xff, s->csr[12] >> 8,
  560. s->csr[13] & 0xff, s->csr[13] >> 8,
  561. s->csr[14] & 0xff, s->csr[14] >> 8
  562. };
  563. int result = (!CSR_DRCVPA(s)) && !memcmp(hdr->ether_dhost, padr, 6);
  564. #ifdef PCNET_DEBUG_MATCH
  565. printf("packet dhost=%02x:%02x:%02x:%02x:%02x:%02x, "
  566. "padr=%02x:%02x:%02x:%02x:%02x:%02x\n",
  567. hdr->ether_dhost[0],hdr->ether_dhost[1],hdr->ether_dhost[2],
  568. hdr->ether_dhost[3],hdr->ether_dhost[4],hdr->ether_dhost[5],
  569. padr[0],padr[1],padr[2],padr[3],padr[4],padr[5]);
  570. printf("padr_match result=%d\n", result);
  571. #endif
  572. return result;
  573. }
  574. static inline int padr_bcast(PCNetState *s, const uint8_t *buf, int size)
  575. {
  576. static const uint8_t BCAST[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  577. struct qemu_ether_header *hdr = (void *)buf;
  578. int result = !CSR_DRCVBC(s) && !memcmp(hdr->ether_dhost, BCAST, 6);
  579. #ifdef PCNET_DEBUG_MATCH
  580. printf("padr_bcast result=%d\n", result);
  581. #endif
  582. return result;
  583. }
  584. static inline int ladr_match(PCNetState *s, const uint8_t *buf, int size)
  585. {
  586. struct qemu_ether_header *hdr = (void *)buf;
  587. if ((*(hdr->ether_dhost)&0x01) &&
  588. ((uint64_t *)&s->csr[8])[0] != 0LL) {
  589. uint8_t ladr[8] = {
  590. s->csr[8] & 0xff, s->csr[8] >> 8,
  591. s->csr[9] & 0xff, s->csr[9] >> 8,
  592. s->csr[10] & 0xff, s->csr[10] >> 8,
  593. s->csr[11] & 0xff, s->csr[11] >> 8
  594. };
  595. int index = net_crc32_le(hdr->ether_dhost, ETH_ALEN) >> 26;
  596. return !!(ladr[index >> 3] & (1 << (index & 7)));
  597. }
  598. return 0;
  599. }
  600. static inline hwaddr pcnet_rdra_addr(PCNetState *s, int idx)
  601. {
  602. while (idx < 1) {
  603. idx += CSR_RCVRL(s);
  604. }
  605. return s->rdra + ((CSR_RCVRL(s) - idx) * (BCR_SWSTYLE(s) ? 16 : 8));
  606. }
  607. static inline int64_t pcnet_get_next_poll_time(PCNetState *s, int64_t current_time)
  608. {
  609. int64_t next_time = current_time +
  610. (65536 - (CSR_SPND(s) ? 0 : CSR_POLL(s))) * 30;
  611. if (next_time <= current_time) {
  612. next_time = current_time + 1;
  613. }
  614. return next_time;
  615. }
  616. static void pcnet_poll(PCNetState *s);
  617. static void pcnet_poll_timer(void *opaque);
  618. static uint32_t pcnet_csr_readw(PCNetState *s, uint32_t rap);
  619. static void pcnet_csr_writew(PCNetState *s, uint32_t rap, uint32_t new_value);
  620. static void pcnet_bcr_writew(PCNetState *s, uint32_t rap, uint32_t val);
  621. static void pcnet_s_reset(PCNetState *s)
  622. {
  623. trace_pcnet_s_reset(s);
  624. s->rdra = 0;
  625. s->tdra = 0;
  626. s->rap = 0;
  627. s->bcr[BCR_BSBC] &= ~0x0080;
  628. s->csr[0] = 0x0004;
  629. s->csr[3] = 0x0000;
  630. s->csr[4] = 0x0115;
  631. s->csr[5] = 0x0000;
  632. s->csr[6] = 0x0000;
  633. s->csr[8] = 0;
  634. s->csr[9] = 0;
  635. s->csr[10] = 0;
  636. s->csr[11] = 0;
  637. s->csr[12] = le16_to_cpu(((uint16_t *)&s->prom[0])[0]);
  638. s->csr[13] = le16_to_cpu(((uint16_t *)&s->prom[0])[1]);
  639. s->csr[14] = le16_to_cpu(((uint16_t *)&s->prom[0])[2]);
  640. s->csr[15] &= 0x21c4;
  641. s->csr[72] = 1;
  642. s->csr[74] = 1;
  643. s->csr[76] = 1;
  644. s->csr[78] = 1;
  645. s->csr[80] = 0x1410;
  646. s->csr[88] = 0x1003;
  647. s->csr[89] = 0x0262;
  648. s->csr[94] = 0x0000;
  649. s->csr[100] = 0x0200;
  650. s->csr[103] = 0x0105;
  651. s->csr[112] = 0x0000;
  652. s->csr[114] = 0x0000;
  653. s->csr[122] = 0x0000;
  654. s->csr[124] = 0x0000;
  655. s->tx_busy = 0;
  656. }
  657. static void pcnet_update_irq(PCNetState *s)
  658. {
  659. int isr = 0;
  660. s->csr[0] &= ~0x0080;
  661. #if 1
  662. if (((s->csr[0] & ~s->csr[3]) & 0x5f00) ||
  663. (((s->csr[4]>>1) & ~s->csr[4]) & 0x0115) ||
  664. (((s->csr[5]>>1) & s->csr[5]) & 0x0048))
  665. #else
  666. if ((!(s->csr[3] & 0x4000) && !!(s->csr[0] & 0x4000)) /* BABL */ ||
  667. (!(s->csr[3] & 0x1000) && !!(s->csr[0] & 0x1000)) /* MISS */ ||
  668. (!(s->csr[3] & 0x0100) && !!(s->csr[0] & 0x0100)) /* IDON */ ||
  669. (!(s->csr[3] & 0x0200) && !!(s->csr[0] & 0x0200)) /* TINT */ ||
  670. (!(s->csr[3] & 0x0400) && !!(s->csr[0] & 0x0400)) /* RINT */ ||
  671. (!(s->csr[3] & 0x0800) && !!(s->csr[0] & 0x0800)) /* MERR */ ||
  672. (!(s->csr[4] & 0x0001) && !!(s->csr[4] & 0x0002)) /* JAB */ ||
  673. (!(s->csr[4] & 0x0004) && !!(s->csr[4] & 0x0008)) /* TXSTRT */ ||
  674. (!(s->csr[4] & 0x0010) && !!(s->csr[4] & 0x0020)) /* RCVO */ ||
  675. (!(s->csr[4] & 0x0100) && !!(s->csr[4] & 0x0200)) /* MFCO */ ||
  676. (!!(s->csr[5] & 0x0040) && !!(s->csr[5] & 0x0080)) /* EXDINT */ ||
  677. (!!(s->csr[5] & 0x0008) && !!(s->csr[5] & 0x0010)) /* MPINT */)
  678. #endif
  679. {
  680. isr = CSR_INEA(s);
  681. s->csr[0] |= 0x0080;
  682. }
  683. if (!!(s->csr[4] & 0x0080) && CSR_INEA(s)) { /* UINT */
  684. s->csr[4] &= ~0x0080;
  685. s->csr[4] |= 0x0040;
  686. s->csr[0] |= 0x0080;
  687. isr = 1;
  688. trace_pcnet_user_int(s);
  689. }
  690. #if 1
  691. if (((s->csr[5]>>1) & s->csr[5]) & 0x0500)
  692. #else
  693. if ((!!(s->csr[5] & 0x0400) && !!(s->csr[5] & 0x0800)) /* SINT */ ||
  694. (!!(s->csr[5] & 0x0100) && !!(s->csr[5] & 0x0200)) /* SLPINT */ )
  695. #endif
  696. {
  697. isr = 1;
  698. s->csr[0] |= 0x0080;
  699. }
  700. if (isr != s->isr) {
  701. trace_pcnet_isr_change(s, isr, s->isr);
  702. }
  703. qemu_set_irq(s->irq, isr);
  704. s->isr = isr;
  705. }
  706. static void pcnet_init(PCNetState *s)
  707. {
  708. int rlen, tlen;
  709. uint16_t padr[3], ladrf[4], mode;
  710. uint32_t rdra, tdra;
  711. trace_pcnet_init(s, PHYSADDR(s, CSR_IADR(s)));
  712. if (BCR_SSIZE32(s)) {
  713. struct pcnet_initblk32 initblk;
  714. s->phys_mem_read(s->dma_opaque, PHYSADDR(s,CSR_IADR(s)),
  715. (uint8_t *)&initblk, sizeof(initblk), 0);
  716. mode = le16_to_cpu(initblk.mode);
  717. rlen = initblk.rlen >> 4;
  718. tlen = initblk.tlen >> 4;
  719. ladrf[0] = le16_to_cpu(initblk.ladrf[0]);
  720. ladrf[1] = le16_to_cpu(initblk.ladrf[1]);
  721. ladrf[2] = le16_to_cpu(initblk.ladrf[2]);
  722. ladrf[3] = le16_to_cpu(initblk.ladrf[3]);
  723. padr[0] = le16_to_cpu(initblk.padr[0]);
  724. padr[1] = le16_to_cpu(initblk.padr[1]);
  725. padr[2] = le16_to_cpu(initblk.padr[2]);
  726. rdra = le32_to_cpu(initblk.rdra);
  727. tdra = le32_to_cpu(initblk.tdra);
  728. } else {
  729. struct pcnet_initblk16 initblk;
  730. s->phys_mem_read(s->dma_opaque, PHYSADDR(s,CSR_IADR(s)),
  731. (uint8_t *)&initblk, sizeof(initblk), 0);
  732. mode = le16_to_cpu(initblk.mode);
  733. ladrf[0] = le16_to_cpu(initblk.ladrf[0]);
  734. ladrf[1] = le16_to_cpu(initblk.ladrf[1]);
  735. ladrf[2] = le16_to_cpu(initblk.ladrf[2]);
  736. ladrf[3] = le16_to_cpu(initblk.ladrf[3]);
  737. padr[0] = le16_to_cpu(initblk.padr[0]);
  738. padr[1] = le16_to_cpu(initblk.padr[1]);
  739. padr[2] = le16_to_cpu(initblk.padr[2]);
  740. rdra = le32_to_cpu(initblk.rdra);
  741. tdra = le32_to_cpu(initblk.tdra);
  742. rlen = rdra >> 29;
  743. tlen = tdra >> 29;
  744. rdra &= 0x00ffffff;
  745. tdra &= 0x00ffffff;
  746. }
  747. trace_pcnet_rlen_tlen(s, rlen, tlen);
  748. CSR_RCVRL(s) = (rlen < 9) ? (1 << rlen) : 512;
  749. CSR_XMTRL(s) = (tlen < 9) ? (1 << tlen) : 512;
  750. s->csr[ 6] = (tlen << 12) | (rlen << 8);
  751. s->csr[15] = mode;
  752. s->csr[ 8] = ladrf[0];
  753. s->csr[ 9] = ladrf[1];
  754. s->csr[10] = ladrf[2];
  755. s->csr[11] = ladrf[3];
  756. s->csr[12] = padr[0];
  757. s->csr[13] = padr[1];
  758. s->csr[14] = padr[2];
  759. s->rdra = PHYSADDR(s, rdra);
  760. s->tdra = PHYSADDR(s, tdra);
  761. CSR_RCVRC(s) = CSR_RCVRL(s);
  762. CSR_XMTRC(s) = CSR_XMTRL(s);
  763. trace_pcnet_ss32_rdra_tdra(s, BCR_SSIZE32(s),
  764. s->rdra, CSR_RCVRL(s), s->tdra, CSR_XMTRL(s));
  765. s->csr[0] |= 0x0101;
  766. s->csr[0] &= ~0x0004; /* clear STOP bit */
  767. qemu_flush_queued_packets(qemu_get_queue(s->nic));
  768. }
  769. static void pcnet_start(PCNetState *s)
  770. {
  771. #ifdef PCNET_DEBUG
  772. printf("pcnet_start\n");
  773. #endif
  774. if (!CSR_DTX(s)) {
  775. s->csr[0] |= 0x0010; /* set TXON */
  776. }
  777. if (!CSR_DRX(s)) {
  778. s->csr[0] |= 0x0020; /* set RXON */
  779. }
  780. s->csr[0] &= ~0x0004; /* clear STOP bit */
  781. s->csr[0] |= 0x0002;
  782. pcnet_poll_timer(s);
  783. qemu_flush_queued_packets(qemu_get_queue(s->nic));
  784. }
  785. static void pcnet_stop(PCNetState *s)
  786. {
  787. #ifdef PCNET_DEBUG
  788. printf("pcnet_stop\n");
  789. #endif
  790. s->csr[0] &= ~0xffeb;
  791. s->csr[0] |= 0x0014;
  792. s->csr[4] &= ~0x02c2;
  793. s->csr[5] &= ~0x0011;
  794. pcnet_poll_timer(s);
  795. }
  796. static void pcnet_rdte_poll(PCNetState *s)
  797. {
  798. s->csr[28] = s->csr[29] = 0;
  799. if (s->rdra) {
  800. int bad = 0;
  801. #if 1
  802. hwaddr crda = pcnet_rdra_addr(s, CSR_RCVRC(s));
  803. hwaddr nrda = pcnet_rdra_addr(s, -1 + CSR_RCVRC(s));
  804. hwaddr nnrd = pcnet_rdra_addr(s, -2 + CSR_RCVRC(s));
  805. #else
  806. hwaddr crda = s->rdra +
  807. (CSR_RCVRL(s) - CSR_RCVRC(s)) *
  808. (BCR_SWSTYLE(s) ? 16 : 8 );
  809. int nrdc = CSR_RCVRC(s)<=1 ? CSR_RCVRL(s) : CSR_RCVRC(s)-1;
  810. hwaddr nrda = s->rdra +
  811. (CSR_RCVRL(s) - nrdc) *
  812. (BCR_SWSTYLE(s) ? 16 : 8 );
  813. int nnrc = nrdc<=1 ? CSR_RCVRL(s) : nrdc-1;
  814. hwaddr nnrd = s->rdra +
  815. (CSR_RCVRL(s) - nnrc) *
  816. (BCR_SWSTYLE(s) ? 16 : 8 );
  817. #endif
  818. CHECK_RMD(crda, bad);
  819. if (!bad) {
  820. CHECK_RMD(nrda, bad);
  821. if (bad || (nrda == crda)) nrda = 0;
  822. CHECK_RMD(nnrd, bad);
  823. if (bad || (nnrd == crda)) nnrd = 0;
  824. s->csr[28] = crda & 0xffff;
  825. s->csr[29] = crda >> 16;
  826. s->csr[26] = nrda & 0xffff;
  827. s->csr[27] = nrda >> 16;
  828. s->csr[36] = nnrd & 0xffff;
  829. s->csr[37] = nnrd >> 16;
  830. #ifdef PCNET_DEBUG
  831. if (bad) {
  832. printf("pcnet: BAD RMD RECORDS AFTER 0x" HWADDR_FMT_plx "\n",
  833. crda);
  834. }
  835. } else {
  836. printf("pcnet: BAD RMD RDA=0x" HWADDR_FMT_plx "\n", crda);
  837. #endif
  838. }
  839. }
  840. if (CSR_CRDA(s)) {
  841. struct pcnet_RMD rmd;
  842. RMDLOAD(&rmd, PHYSADDR(s,CSR_CRDA(s)));
  843. CSR_CRBC(s) = GET_FIELD(rmd.buf_length, RMDL, BCNT);
  844. CSR_CRST(s) = rmd.status;
  845. #ifdef PCNET_DEBUG_RMD_X
  846. printf("CRDA=0x%08x CRST=0x%04x RCVRC=%d RMDL=0x%04x RMDS=0x%04x RMDM=0x%08x\n",
  847. PHYSADDR(s,CSR_CRDA(s)), CSR_CRST(s), CSR_RCVRC(s),
  848. rmd.buf_length, rmd.status, rmd.msg_length);
  849. PRINT_RMD(&rmd);
  850. #endif
  851. } else {
  852. CSR_CRBC(s) = CSR_CRST(s) = 0;
  853. }
  854. if (CSR_NRDA(s)) {
  855. struct pcnet_RMD rmd;
  856. RMDLOAD(&rmd, PHYSADDR(s,CSR_NRDA(s)));
  857. CSR_NRBC(s) = GET_FIELD(rmd.buf_length, RMDL, BCNT);
  858. CSR_NRST(s) = rmd.status;
  859. } else {
  860. CSR_NRBC(s) = CSR_NRST(s) = 0;
  861. }
  862. }
  863. static int pcnet_tdte_poll(PCNetState *s)
  864. {
  865. s->csr[34] = s->csr[35] = 0;
  866. if (s->tdra) {
  867. hwaddr cxda = s->tdra +
  868. (CSR_XMTRL(s) - CSR_XMTRC(s)) *
  869. (BCR_SWSTYLE(s) ? 16 : 8);
  870. int bad = 0;
  871. CHECK_TMD(cxda, bad);
  872. if (!bad) {
  873. if (CSR_CXDA(s) != cxda) {
  874. s->csr[60] = s->csr[34];
  875. s->csr[61] = s->csr[35];
  876. s->csr[62] = CSR_CXBC(s);
  877. s->csr[63] = CSR_CXST(s);
  878. }
  879. s->csr[34] = cxda & 0xffff;
  880. s->csr[35] = cxda >> 16;
  881. #ifdef PCNET_DEBUG_X
  882. printf("pcnet: BAD TMD XDA=0x%08x\n", cxda);
  883. #endif
  884. }
  885. }
  886. if (CSR_CXDA(s)) {
  887. struct pcnet_TMD tmd;
  888. TMDLOAD(&tmd, PHYSADDR(s,CSR_CXDA(s)));
  889. CSR_CXBC(s) = GET_FIELD(tmd.length, TMDL, BCNT);
  890. CSR_CXST(s) = tmd.status;
  891. } else {
  892. CSR_CXBC(s) = CSR_CXST(s) = 0;
  893. }
  894. return !!(CSR_CXST(s) & 0x8000);
  895. }
  896. #define MIN_BUF_SIZE 60
  897. ssize_t pcnet_receive(NetClientState *nc, const uint8_t *buf, size_t size_)
  898. {
  899. PCNetState *s = qemu_get_nic_opaque(nc);
  900. int is_padr = 0, is_bcast = 0, is_ladr = 0;
  901. uint8_t buf1[60];
  902. int remaining;
  903. int crc_err = 0;
  904. size_t size = size_;
  905. if (CSR_DRX(s) || CSR_STOP(s) || CSR_SPND(s) || !size ||
  906. (CSR_LOOP(s) && !s->looptest)) {
  907. return -1;
  908. }
  909. #ifdef PCNET_DEBUG
  910. printf("pcnet_receive size=%zu\n", size);
  911. #endif
  912. /* if too small buffer, then expand it */
  913. if (size < MIN_BUF_SIZE) {
  914. memcpy(buf1, buf, size);
  915. memset(buf1 + size, 0, MIN_BUF_SIZE - size);
  916. buf = buf1;
  917. size = MIN_BUF_SIZE;
  918. }
  919. if (CSR_PROM(s)
  920. || (is_padr=padr_match(s, buf, size))
  921. || (is_bcast=padr_bcast(s, buf, size))
  922. || (is_ladr=ladr_match(s, buf, size))) {
  923. pcnet_rdte_poll(s);
  924. if (!(CSR_CRST(s) & 0x8000) && s->rdra) {
  925. struct pcnet_RMD rmd;
  926. int rcvrc = CSR_RCVRC(s)-1,i;
  927. hwaddr nrda;
  928. for (i = CSR_RCVRL(s)-1; i > 0; i--, rcvrc--) {
  929. if (rcvrc <= 1)
  930. rcvrc = CSR_RCVRL(s);
  931. nrda = s->rdra +
  932. (CSR_RCVRL(s) - rcvrc) *
  933. (BCR_SWSTYLE(s) ? 16 : 8 );
  934. RMDLOAD(&rmd, nrda);
  935. if (GET_FIELD(rmd.status, RMDS, OWN)) {
  936. #ifdef PCNET_DEBUG_RMD
  937. printf("pcnet - scan buffer: RCVRC=%d PREV_RCVRC=%d\n",
  938. rcvrc, CSR_RCVRC(s));
  939. #endif
  940. CSR_RCVRC(s) = rcvrc;
  941. pcnet_rdte_poll(s);
  942. break;
  943. }
  944. }
  945. }
  946. if (!(CSR_CRST(s) & 0x8000)) {
  947. #ifdef PCNET_DEBUG_RMD
  948. printf("pcnet - no buffer: RCVRC=%d\n", CSR_RCVRC(s));
  949. #endif
  950. s->csr[0] |= 0x1000; /* Set MISS flag */
  951. CSR_MISSC(s)++;
  952. } else {
  953. uint8_t *src = s->buffer;
  954. hwaddr crda = CSR_CRDA(s);
  955. struct pcnet_RMD rmd;
  956. int pktcount = 0;
  957. if (!s->looptest) {
  958. if (size > 4092) {
  959. #ifdef PCNET_DEBUG_RMD
  960. fprintf(stderr, "pcnet: truncates rx packet.\n");
  961. #endif
  962. size = 4092;
  963. }
  964. memcpy(src, buf, size);
  965. /* no need to compute the CRC */
  966. src[size] = 0;
  967. src[size + 1] = 0;
  968. src[size + 2] = 0;
  969. src[size + 3] = 0;
  970. size += 4;
  971. } else if (s->looptest == PCNET_LOOPTEST_CRC ||
  972. !CSR_DXMTFCS(s) || size < MIN_BUF_SIZE+4) {
  973. uint32_t fcs = ~0;
  974. uint8_t *p = src;
  975. while (p != &src[size])
  976. CRC(fcs, *p++);
  977. *(uint32_t *)p = htonl(fcs);
  978. size += 4;
  979. } else {
  980. uint32_t fcs = ~0;
  981. uint8_t *p = src;
  982. while (p != &src[size])
  983. CRC(fcs, *p++);
  984. crc_err = (*(uint32_t *)p != htonl(fcs));
  985. }
  986. #ifdef PCNET_DEBUG_MATCH
  987. PRINT_PKTHDR(buf);
  988. #endif
  989. RMDLOAD(&rmd, PHYSADDR(s,crda));
  990. /*if (!CSR_LAPPEN(s))*/
  991. SET_FIELD(&rmd.status, RMDS, STP, 1);
  992. #define PCNET_RECV_STORE() do { \
  993. int count = MIN(4096 - GET_FIELD(rmd.buf_length, RMDL, BCNT),remaining); \
  994. hwaddr rbadr = PHYSADDR(s, rmd.rbadr); \
  995. s->phys_mem_write(s->dma_opaque, rbadr, src, count, CSR_BSWP(s)); \
  996. src += count; remaining -= count; \
  997. SET_FIELD(&rmd.status, RMDS, OWN, 0); \
  998. RMDSTORE(&rmd, PHYSADDR(s,crda)); \
  999. pktcount++; \
  1000. } while (0)
  1001. remaining = size;
  1002. PCNET_RECV_STORE();
  1003. if ((remaining > 0) && CSR_NRDA(s)) {
  1004. hwaddr nrda = CSR_NRDA(s);
  1005. #ifdef PCNET_DEBUG_RMD
  1006. PRINT_RMD(&rmd);
  1007. #endif
  1008. RMDLOAD(&rmd, PHYSADDR(s,nrda));
  1009. if (GET_FIELD(rmd.status, RMDS, OWN)) {
  1010. crda = nrda;
  1011. PCNET_RECV_STORE();
  1012. #ifdef PCNET_DEBUG_RMD
  1013. PRINT_RMD(&rmd);
  1014. #endif
  1015. if ((remaining > 0) && (nrda=CSR_NNRD(s))) {
  1016. RMDLOAD(&rmd, PHYSADDR(s,nrda));
  1017. if (GET_FIELD(rmd.status, RMDS, OWN)) {
  1018. crda = nrda;
  1019. PCNET_RECV_STORE();
  1020. }
  1021. }
  1022. }
  1023. }
  1024. #undef PCNET_RECV_STORE
  1025. RMDLOAD(&rmd, PHYSADDR(s,crda));
  1026. if (remaining == 0) {
  1027. SET_FIELD(&rmd.msg_length, RMDM, MCNT, size);
  1028. SET_FIELD(&rmd.status, RMDS, ENP, 1);
  1029. SET_FIELD(&rmd.status, RMDS, PAM, !CSR_PROM(s) && is_padr);
  1030. SET_FIELD(&rmd.status, RMDS, LFAM, !CSR_PROM(s) && is_ladr);
  1031. SET_FIELD(&rmd.status, RMDS, BAM, !CSR_PROM(s) && is_bcast);
  1032. if (crc_err) {
  1033. SET_FIELD(&rmd.status, RMDS, CRC, 1);
  1034. SET_FIELD(&rmd.status, RMDS, ERR, 1);
  1035. }
  1036. } else {
  1037. SET_FIELD(&rmd.status, RMDS, OFLO, 1);
  1038. SET_FIELD(&rmd.status, RMDS, BUFF, 1);
  1039. SET_FIELD(&rmd.status, RMDS, ERR, 1);
  1040. }
  1041. RMDSTORE(&rmd, PHYSADDR(s,crda));
  1042. s->csr[0] |= 0x0400;
  1043. #ifdef PCNET_DEBUG
  1044. printf("RCVRC=%d CRDA=0x%08x BLKS=%d\n",
  1045. CSR_RCVRC(s), PHYSADDR(s,CSR_CRDA(s)), pktcount);
  1046. #endif
  1047. #ifdef PCNET_DEBUG_RMD
  1048. PRINT_RMD(&rmd);
  1049. #endif
  1050. while (pktcount--) {
  1051. if (CSR_RCVRC(s) <= 1) {
  1052. CSR_RCVRC(s) = CSR_RCVRL(s);
  1053. } else {
  1054. CSR_RCVRC(s)--;
  1055. }
  1056. }
  1057. pcnet_rdte_poll(s);
  1058. }
  1059. }
  1060. pcnet_poll(s);
  1061. pcnet_update_irq(s);
  1062. return size_;
  1063. }
  1064. void pcnet_set_link_status(NetClientState *nc)
  1065. {
  1066. PCNetState *d = qemu_get_nic_opaque(nc);
  1067. d->lnkst = nc->link_down ? 0 : 0x40;
  1068. }
  1069. static void pcnet_transmit(PCNetState *s)
  1070. {
  1071. hwaddr xmit_cxda = 0;
  1072. int count = CSR_XMTRL(s)-1;
  1073. int add_crc = 0;
  1074. int bcnt;
  1075. s->xmit_pos = -1;
  1076. if (!CSR_TXON(s)) {
  1077. s->csr[0] &= ~0x0008;
  1078. return;
  1079. }
  1080. s->tx_busy = 1;
  1081. txagain:
  1082. if (pcnet_tdte_poll(s)) {
  1083. struct pcnet_TMD tmd;
  1084. TMDLOAD(&tmd, PHYSADDR(s,CSR_CXDA(s)));
  1085. #ifdef PCNET_DEBUG_TMD
  1086. printf(" TMDLOAD 0x%08x\n", PHYSADDR(s,CSR_CXDA(s)));
  1087. PRINT_TMD(&tmd);
  1088. #endif
  1089. if (GET_FIELD(tmd.status, TMDS, STP)) {
  1090. s->xmit_pos = 0;
  1091. xmit_cxda = PHYSADDR(s,CSR_CXDA(s));
  1092. if (BCR_SWSTYLE(s) != 1)
  1093. add_crc = GET_FIELD(tmd.status, TMDS, ADDFCS);
  1094. }
  1095. if (s->lnkst == 0 &&
  1096. (!CSR_LOOP(s) || (!CSR_INTL(s) && !BCR_TMAULOOP(s)))) {
  1097. SET_FIELD(&tmd.misc, TMDM, LCAR, 1);
  1098. SET_FIELD(&tmd.status, TMDS, ERR, 1);
  1099. SET_FIELD(&tmd.status, TMDS, OWN, 0);
  1100. s->csr[0] |= 0xa000; /* ERR | CERR */
  1101. s->xmit_pos = -1;
  1102. goto txdone;
  1103. }
  1104. if (s->xmit_pos < 0) {
  1105. goto txdone;
  1106. }
  1107. bcnt = 4096 - GET_FIELD(tmd.length, TMDL, BCNT);
  1108. /* if multi-tmd packet outsizes s->buffer then skip it silently.
  1109. * Note: this is not what real hw does.
  1110. * Last four bytes of s->buffer are used to store CRC FCS code.
  1111. */
  1112. if (s->xmit_pos + bcnt > sizeof(s->buffer) - 4) {
  1113. s->xmit_pos = -1;
  1114. goto txdone;
  1115. }
  1116. s->phys_mem_read(s->dma_opaque, PHYSADDR(s, tmd.tbadr),
  1117. s->buffer + s->xmit_pos, bcnt, CSR_BSWP(s));
  1118. s->xmit_pos += bcnt;
  1119. if (!GET_FIELD(tmd.status, TMDS, ENP)) {
  1120. goto txdone;
  1121. }
  1122. #ifdef PCNET_DEBUG
  1123. printf("pcnet_transmit size=%d\n", s->xmit_pos);
  1124. #endif
  1125. if (CSR_LOOP(s)) {
  1126. if (BCR_SWSTYLE(s) == 1)
  1127. add_crc = !GET_FIELD(tmd.status, TMDS, NOFCS);
  1128. s->looptest = add_crc ? PCNET_LOOPTEST_CRC : PCNET_LOOPTEST_NOCRC;
  1129. qemu_receive_packet(qemu_get_queue(s->nic), s->buffer, s->xmit_pos);
  1130. s->looptest = 0;
  1131. } else {
  1132. if (s->nic) {
  1133. qemu_send_packet(qemu_get_queue(s->nic), s->buffer,
  1134. s->xmit_pos);
  1135. }
  1136. }
  1137. s->csr[0] &= ~0x0008; /* clear TDMD */
  1138. s->csr[4] |= 0x0004; /* set TXSTRT */
  1139. s->xmit_pos = -1;
  1140. txdone:
  1141. SET_FIELD(&tmd.status, TMDS, OWN, 0);
  1142. TMDSTORE(&tmd, PHYSADDR(s,CSR_CXDA(s)));
  1143. if (!CSR_TOKINTD(s)
  1144. || (CSR_LTINTEN(s) && GET_FIELD(tmd.status, TMDS, LTINT))) {
  1145. s->csr[0] |= 0x0200; /* set TINT */
  1146. }
  1147. if (CSR_XMTRC(s) <= 1) {
  1148. CSR_XMTRC(s) = CSR_XMTRL(s);
  1149. } else {
  1150. CSR_XMTRC(s)--;
  1151. }
  1152. if (count--) {
  1153. goto txagain;
  1154. }
  1155. } else if (s->xmit_pos >= 0) {
  1156. struct pcnet_TMD tmd;
  1157. TMDLOAD(&tmd, xmit_cxda);
  1158. SET_FIELD(&tmd.misc, TMDM, BUFF, 1);
  1159. SET_FIELD(&tmd.misc, TMDM, UFLO, 1);
  1160. SET_FIELD(&tmd.status, TMDS, ERR, 1);
  1161. SET_FIELD(&tmd.status, TMDS, OWN, 0);
  1162. TMDSTORE(&tmd, xmit_cxda);
  1163. s->csr[0] |= 0x0200; /* set TINT */
  1164. if (!CSR_DXSUFLO(s)) {
  1165. s->csr[0] &= ~0x0010;
  1166. } else if (count--) {
  1167. goto txagain;
  1168. }
  1169. }
  1170. s->tx_busy = 0;
  1171. }
  1172. static void pcnet_poll(PCNetState *s)
  1173. {
  1174. if (CSR_RXON(s)) {
  1175. pcnet_rdte_poll(s);
  1176. }
  1177. if (CSR_TDMD(s) || (CSR_TXON(s) && !CSR_DPOLL(s) && pcnet_tdte_poll(s))) {
  1178. /* prevent recursion */
  1179. if (s->tx_busy) {
  1180. return;
  1181. }
  1182. pcnet_transmit(s);
  1183. }
  1184. }
  1185. static void pcnet_poll_timer(void *opaque)
  1186. {
  1187. PCNetState *s = opaque;
  1188. timer_del(s->poll_timer);
  1189. if (CSR_TDMD(s)) {
  1190. pcnet_transmit(s);
  1191. }
  1192. pcnet_update_irq(s);
  1193. if (!CSR_STOP(s) && !CSR_SPND(s) && !CSR_DPOLL(s)) {
  1194. uint64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) * 33;
  1195. if (!s->timer || !now) {
  1196. s->timer = now;
  1197. } else {
  1198. uint64_t t = now - s->timer + CSR_POLL(s);
  1199. if (t > 0xffffLL) {
  1200. pcnet_poll(s);
  1201. CSR_POLL(s) = CSR_PINT(s);
  1202. } else {
  1203. CSR_POLL(s) = t;
  1204. }
  1205. }
  1206. timer_mod(s->poll_timer,
  1207. pcnet_get_next_poll_time(s,qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)));
  1208. }
  1209. }
  1210. static void pcnet_csr_writew(PCNetState *s, uint32_t rap, uint32_t new_value)
  1211. {
  1212. uint16_t val = new_value;
  1213. #ifdef PCNET_DEBUG_CSR
  1214. printf("pcnet_csr_writew rap=%d val=0x%04x\n", rap, val);
  1215. #endif
  1216. switch (rap) {
  1217. case 0:
  1218. s->csr[0] &= ~(val & 0x7f00); /* Clear any interrupt flags */
  1219. s->csr[0] = (s->csr[0] & ~0x0040) | (val & 0x0048);
  1220. val = (val & 0x007f) | (s->csr[0] & 0x7f00);
  1221. /* IFF STOP, STRT and INIT are set, clear STRT and INIT */
  1222. if ((val & 7) == 7) {
  1223. val &= ~3;
  1224. }
  1225. if (!CSR_STOP(s) && (val & 4)) {
  1226. pcnet_stop(s);
  1227. }
  1228. if (!CSR_INIT(s) && (val & 1)) {
  1229. pcnet_init(s);
  1230. }
  1231. if (!CSR_STRT(s) && (val & 2)) {
  1232. pcnet_start(s);
  1233. }
  1234. if (CSR_TDMD(s)) {
  1235. pcnet_transmit(s);
  1236. }
  1237. return;
  1238. case 1:
  1239. case 2:
  1240. case 8:
  1241. case 9:
  1242. case 10:
  1243. case 11:
  1244. case 12:
  1245. case 13:
  1246. case 14:
  1247. case 15:
  1248. case 18: /* CRBAL */
  1249. case 19: /* CRBAU */
  1250. case 20: /* CXBAL */
  1251. case 21: /* CXBAU */
  1252. case 22: /* NRBAU */
  1253. case 23: /* NRBAU */
  1254. case 24:
  1255. case 25:
  1256. case 26:
  1257. case 27:
  1258. case 28:
  1259. case 29:
  1260. case 30:
  1261. case 31:
  1262. case 32:
  1263. case 33:
  1264. case 34:
  1265. case 35:
  1266. case 36:
  1267. case 37:
  1268. case 38:
  1269. case 39:
  1270. case 40: /* CRBC */
  1271. case 41:
  1272. case 42: /* CXBC */
  1273. case 43:
  1274. case 44:
  1275. case 45:
  1276. case 46: /* POLL */
  1277. case 47: /* POLLINT */
  1278. case 72:
  1279. case 74:
  1280. break;
  1281. case 76: /* RCVRL */
  1282. case 78: /* XMTRL */
  1283. val = (val > 0) ? val : 512;
  1284. break;
  1285. case 112:
  1286. if (CSR_STOP(s) || CSR_SPND(s)) {
  1287. break;
  1288. }
  1289. return;
  1290. case 3:
  1291. break;
  1292. case 4:
  1293. s->csr[4] &= ~(val & 0x026a);
  1294. val &= ~0x026a; val |= s->csr[4] & 0x026a;
  1295. break;
  1296. case 5:
  1297. s->csr[5] &= ~(val & 0x0a90);
  1298. val &= ~0x0a90; val |= s->csr[5] & 0x0a90;
  1299. break;
  1300. case 16:
  1301. pcnet_csr_writew(s,1,val);
  1302. return;
  1303. case 17:
  1304. pcnet_csr_writew(s,2,val);
  1305. return;
  1306. case 58:
  1307. pcnet_bcr_writew(s,BCR_SWS,val);
  1308. break;
  1309. default:
  1310. return;
  1311. }
  1312. s->csr[rap] = val;
  1313. }
  1314. static uint32_t pcnet_csr_readw(PCNetState *s, uint32_t rap)
  1315. {
  1316. uint32_t val;
  1317. switch (rap) {
  1318. case 0:
  1319. pcnet_update_irq(s);
  1320. val = s->csr[0];
  1321. val |= (val & 0x7800) ? 0x8000 : 0;
  1322. break;
  1323. case 16:
  1324. return pcnet_csr_readw(s,1);
  1325. case 17:
  1326. return pcnet_csr_readw(s,2);
  1327. case 58:
  1328. return pcnet_bcr_readw(s,BCR_SWS);
  1329. case 88:
  1330. val = s->csr[89];
  1331. val <<= 16;
  1332. val |= s->csr[88];
  1333. break;
  1334. default:
  1335. val = s->csr[rap];
  1336. }
  1337. #ifdef PCNET_DEBUG_CSR
  1338. printf("pcnet_csr_readw rap=%d val=0x%04x\n", rap, val);
  1339. #endif
  1340. return val;
  1341. }
  1342. static void pcnet_bcr_writew(PCNetState *s, uint32_t rap, uint32_t val)
  1343. {
  1344. rap &= 127;
  1345. #ifdef PCNET_DEBUG_BCR
  1346. printf("pcnet_bcr_writew rap=%d val=0x%04x\n", rap, val);
  1347. #endif
  1348. switch (rap) {
  1349. case BCR_SWS:
  1350. if (!(CSR_STOP(s) || CSR_SPND(s)))
  1351. return;
  1352. val &= ~0x0300;
  1353. switch (val & 0x00ff) {
  1354. case 0:
  1355. val |= 0x0200;
  1356. break;
  1357. case 1:
  1358. val |= 0x0100;
  1359. break;
  1360. case 2:
  1361. case 3:
  1362. val |= 0x0300;
  1363. break;
  1364. default:
  1365. qemu_log_mask(LOG_GUEST_ERROR, "pcnet: Bad SWSTYLE=0x%02x\n",
  1366. val & 0xff);
  1367. val = 0x0200;
  1368. break;
  1369. }
  1370. #ifdef PCNET_DEBUG
  1371. printf("BCR_SWS=0x%04x\n", val);
  1372. #endif
  1373. /* fall through */
  1374. case BCR_LNKST:
  1375. case BCR_LED1:
  1376. case BCR_LED2:
  1377. case BCR_LED3:
  1378. case BCR_MC:
  1379. case BCR_FDC:
  1380. case BCR_BSBC:
  1381. case BCR_EECAS:
  1382. case BCR_PLAT:
  1383. s->bcr[rap] = val;
  1384. break;
  1385. default:
  1386. break;
  1387. }
  1388. }
  1389. uint32_t pcnet_bcr_readw(PCNetState *s, uint32_t rap)
  1390. {
  1391. uint32_t val;
  1392. rap &= 127;
  1393. switch (rap) {
  1394. case BCR_LNKST:
  1395. case BCR_LED1:
  1396. case BCR_LED2:
  1397. case BCR_LED3:
  1398. val = s->bcr[rap] & ~0x8000;
  1399. val |= (val & 0x017f & s->lnkst) ? 0x8000 : 0;
  1400. break;
  1401. default:
  1402. val = rap < 32 ? s->bcr[rap] : 0;
  1403. break;
  1404. }
  1405. #ifdef PCNET_DEBUG_BCR
  1406. printf("pcnet_bcr_readw rap=%d val=0x%04x\n", rap, val);
  1407. #endif
  1408. return val;
  1409. }
  1410. void pcnet_h_reset(void *opaque)
  1411. {
  1412. PCNetState *s = opaque;
  1413. s->bcr[BCR_MSRDA] = 0x0005;
  1414. s->bcr[BCR_MSWRA] = 0x0005;
  1415. s->bcr[BCR_MC ] = 0x0002;
  1416. s->bcr[BCR_LNKST] = 0x00c0;
  1417. s->bcr[BCR_LED1 ] = 0x0084;
  1418. s->bcr[BCR_LED2 ] = 0x0088;
  1419. s->bcr[BCR_LED3 ] = 0x0090;
  1420. s->bcr[BCR_FDC ] = 0x0000;
  1421. s->bcr[BCR_BSBC ] = 0x9001;
  1422. s->bcr[BCR_EECAS] = 0x0002;
  1423. s->bcr[BCR_SWS ] = 0x0200;
  1424. s->bcr[BCR_PLAT ] = 0xff06;
  1425. pcnet_s_reset(s);
  1426. pcnet_update_irq(s);
  1427. pcnet_poll_timer(s);
  1428. }
  1429. void pcnet_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
  1430. {
  1431. PCNetState *s = opaque;
  1432. pcnet_poll_timer(s);
  1433. #ifdef PCNET_DEBUG_IO
  1434. printf("pcnet_ioport_writew addr=0x%08x val=0x%04x\n", addr, val);
  1435. #endif
  1436. if (!BCR_DWIO(s)) {
  1437. switch (addr & 0x0f) {
  1438. case 0x00: /* RDP */
  1439. pcnet_csr_writew(s, s->rap, val);
  1440. break;
  1441. case 0x02:
  1442. s->rap = val & 0x7f;
  1443. break;
  1444. case 0x06:
  1445. pcnet_bcr_writew(s, s->rap, val);
  1446. break;
  1447. }
  1448. }
  1449. pcnet_update_irq(s);
  1450. }
  1451. uint32_t pcnet_ioport_readw(void *opaque, uint32_t addr)
  1452. {
  1453. PCNetState *s = opaque;
  1454. uint32_t val = -1;
  1455. pcnet_poll_timer(s);
  1456. if (!BCR_DWIO(s)) {
  1457. switch (addr & 0x0f) {
  1458. case 0x00: /* RDP */
  1459. val = pcnet_csr_readw(s, s->rap);
  1460. break;
  1461. case 0x02:
  1462. val = s->rap;
  1463. break;
  1464. case 0x04:
  1465. pcnet_s_reset(s);
  1466. val = 0;
  1467. break;
  1468. case 0x06:
  1469. val = pcnet_bcr_readw(s, s->rap);
  1470. break;
  1471. }
  1472. }
  1473. pcnet_update_irq(s);
  1474. #ifdef PCNET_DEBUG_IO
  1475. printf("pcnet_ioport_readw addr=0x%08x val=0x%04x\n", addr, val & 0xffff);
  1476. #endif
  1477. return val;
  1478. }
  1479. void pcnet_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
  1480. {
  1481. PCNetState *s = opaque;
  1482. pcnet_poll_timer(s);
  1483. #ifdef PCNET_DEBUG_IO
  1484. printf("pcnet_ioport_writel addr=0x%08x val=0x%08x\n", addr, val);
  1485. #endif
  1486. if (BCR_DWIO(s)) {
  1487. switch (addr & 0x0f) {
  1488. case 0x00: /* RDP */
  1489. pcnet_csr_writew(s, s->rap, val & 0xffff);
  1490. break;
  1491. case 0x04:
  1492. s->rap = val & 0x7f;
  1493. break;
  1494. case 0x0c:
  1495. pcnet_bcr_writew(s, s->rap, val & 0xffff);
  1496. break;
  1497. }
  1498. } else if ((addr & 0x0f) == 0) {
  1499. /* switch device to dword i/o mode */
  1500. pcnet_bcr_writew(s, BCR_BSBC, pcnet_bcr_readw(s, BCR_BSBC) | 0x0080);
  1501. #ifdef PCNET_DEBUG_IO
  1502. printf("device switched into dword i/o mode\n");
  1503. #endif
  1504. }
  1505. pcnet_update_irq(s);
  1506. }
  1507. uint32_t pcnet_ioport_readl(void *opaque, uint32_t addr)
  1508. {
  1509. PCNetState *s = opaque;
  1510. uint32_t val = -1;
  1511. pcnet_poll_timer(s);
  1512. if (BCR_DWIO(s)) {
  1513. switch (addr & 0x0f) {
  1514. case 0x00: /* RDP */
  1515. val = pcnet_csr_readw(s, s->rap);
  1516. break;
  1517. case 0x04:
  1518. val = s->rap;
  1519. break;
  1520. case 0x08:
  1521. pcnet_s_reset(s);
  1522. val = 0;
  1523. break;
  1524. case 0x0c:
  1525. val = pcnet_bcr_readw(s, s->rap);
  1526. break;
  1527. }
  1528. }
  1529. pcnet_update_irq(s);
  1530. #ifdef PCNET_DEBUG_IO
  1531. printf("pcnet_ioport_readl addr=0x%08x val=0x%08x\n", addr, val);
  1532. #endif
  1533. return val;
  1534. }
  1535. static bool is_version_2(void *opaque, int version_id)
  1536. {
  1537. return version_id == 2;
  1538. }
  1539. const VMStateDescription vmstate_pcnet = {
  1540. .name = "pcnet",
  1541. .version_id = 3,
  1542. .minimum_version_id = 2,
  1543. .fields = (VMStateField[]) {
  1544. VMSTATE_INT32(rap, PCNetState),
  1545. VMSTATE_INT32(isr, PCNetState),
  1546. VMSTATE_INT32(lnkst, PCNetState),
  1547. VMSTATE_UINT32(rdra, PCNetState),
  1548. VMSTATE_UINT32(tdra, PCNetState),
  1549. VMSTATE_BUFFER(prom, PCNetState),
  1550. VMSTATE_UINT16_ARRAY(csr, PCNetState, 128),
  1551. VMSTATE_UINT16_ARRAY(bcr, PCNetState, 32),
  1552. VMSTATE_UINT64(timer, PCNetState),
  1553. VMSTATE_INT32(xmit_pos, PCNetState),
  1554. VMSTATE_BUFFER(buffer, PCNetState),
  1555. VMSTATE_UNUSED_TEST(is_version_2, 4),
  1556. VMSTATE_INT32(tx_busy, PCNetState),
  1557. VMSTATE_TIMER_PTR(poll_timer, PCNetState),
  1558. VMSTATE_END_OF_LIST()
  1559. }
  1560. };
  1561. void pcnet_common_init(DeviceState *dev, PCNetState *s, NetClientInfo *info)
  1562. {
  1563. int i;
  1564. uint16_t checksum;
  1565. s->poll_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, pcnet_poll_timer, s);
  1566. qemu_macaddr_default_if_unset(&s->conf.macaddr);
  1567. s->nic = qemu_new_nic(info, &s->conf, object_get_typename(OBJECT(dev)), dev->id, s);
  1568. qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
  1569. /* Initialize the PROM */
  1570. /*
  1571. Datasheet: http://pdfdata.datasheetsite.com/web/24528/AM79C970A.pdf
  1572. page 95
  1573. */
  1574. memcpy(s->prom, s->conf.macaddr.a, 6);
  1575. /* Reserved Location: must be 00h */
  1576. s->prom[6] = s->prom[7] = 0x00;
  1577. /* Reserved Location: must be 00h */
  1578. s->prom[8] = 0x00;
  1579. /* Hardware ID: must be 11h if compatibility to AMD drivers is desired */
  1580. s->prom[9] = 0x11;
  1581. /* User programmable space, init with 0 */
  1582. s->prom[10] = s->prom[11] = 0x00;
  1583. /* LSByte of two-byte checksum, which is the sum of bytes 00h-0Bh
  1584. and bytes 0Eh and 0Fh, must therefore be initialized with 0! */
  1585. s->prom[12] = s->prom[13] = 0x00;
  1586. /* Must be ASCII W (57h) if compatibility to AMD
  1587. driver software is desired */
  1588. s->prom[14] = s->prom[15] = 0x57;
  1589. for (i = 0, checksum = 0; i < 16; i++) {
  1590. checksum += s->prom[i];
  1591. }
  1592. *(uint16_t *)&s->prom[12] = cpu_to_le16(checksum);
  1593. s->lnkst = 0x40; /* initial link state: up */
  1594. }