2
0

e1000.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  1. /*
  2. * QEMU e1000 emulation
  3. *
  4. * Software developer's manual:
  5. * http://download.intel.com/design/network/manuals/8254x_GBe_SDM.pdf
  6. *
  7. * Nir Peleg, Tutis Systems Ltd. for Qumranet Inc.
  8. * Copyright (c) 2008 Qumranet
  9. * Based on work done by:
  10. * Copyright (c) 2007 Dan Aloni
  11. * Copyright (c) 2004 Antony T Curtis
  12. *
  13. * This library is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2 of the License, or (at your option) any later version.
  17. *
  18. * This library is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  25. */
  26. #include "hw.h"
  27. #include "pci.h"
  28. #include "net.h"
  29. #include "net/checksum.h"
  30. #include "loader.h"
  31. #include "sysemu.h"
  32. #include "dma.h"
  33. #include "e1000_hw.h"
  34. #define E1000_DEBUG
  35. #ifdef E1000_DEBUG
  36. enum {
  37. DEBUG_GENERAL, DEBUG_IO, DEBUG_MMIO, DEBUG_INTERRUPT,
  38. DEBUG_RX, DEBUG_TX, DEBUG_MDIC, DEBUG_EEPROM,
  39. DEBUG_UNKNOWN, DEBUG_TXSUM, DEBUG_TXERR, DEBUG_RXERR,
  40. DEBUG_RXFILTER, DEBUG_NOTYET,
  41. };
  42. #define DBGBIT(x) (1<<DEBUG_##x)
  43. static int debugflags = DBGBIT(TXERR) | DBGBIT(GENERAL);
  44. #define DBGOUT(what, fmt, ...) do { \
  45. if (debugflags & DBGBIT(what)) \
  46. fprintf(stderr, "e1000: " fmt, ## __VA_ARGS__); \
  47. } while (0)
  48. #else
  49. #define DBGOUT(what, fmt, ...) do {} while (0)
  50. #endif
  51. #define IOPORT_SIZE 0x40
  52. #define PNPMMIO_SIZE 0x20000
  53. #define MIN_BUF_SIZE 60 /* Min. octets in an ethernet frame sans FCS */
  54. /*
  55. * HW models:
  56. * E1000_DEV_ID_82540EM works with Windows and Linux
  57. * E1000_DEV_ID_82573L OK with windoze and Linux 2.6.22,
  58. * appears to perform better than 82540EM, but breaks with Linux 2.6.18
  59. * E1000_DEV_ID_82544GC_COPPER appears to work; not well tested
  60. * Others never tested
  61. */
  62. enum { E1000_DEVID = E1000_DEV_ID_82540EM };
  63. /*
  64. * May need to specify additional MAC-to-PHY entries --
  65. * Intel's Windows driver refuses to initialize unless they match
  66. */
  67. enum {
  68. PHY_ID2_INIT = E1000_DEVID == E1000_DEV_ID_82573L ? 0xcc2 :
  69. E1000_DEVID == E1000_DEV_ID_82544GC_COPPER ? 0xc30 :
  70. /* default to E1000_DEV_ID_82540EM */ 0xc20
  71. };
  72. typedef struct E1000State_st {
  73. PCIDevice dev;
  74. NICState *nic;
  75. NICConf conf;
  76. MemoryRegion mmio;
  77. MemoryRegion io;
  78. uint32_t mac_reg[0x8000];
  79. uint16_t phy_reg[0x20];
  80. uint16_t eeprom_data[64];
  81. uint32_t rxbuf_size;
  82. uint32_t rxbuf_min_shift;
  83. int check_rxov;
  84. struct e1000_tx {
  85. unsigned char header[256];
  86. unsigned char vlan_header[4];
  87. /* Fields vlan and data must not be reordered or separated. */
  88. unsigned char vlan[4];
  89. unsigned char data[0x10000];
  90. uint16_t size;
  91. unsigned char sum_needed;
  92. unsigned char vlan_needed;
  93. uint8_t ipcss;
  94. uint8_t ipcso;
  95. uint16_t ipcse;
  96. uint8_t tucss;
  97. uint8_t tucso;
  98. uint16_t tucse;
  99. uint8_t hdr_len;
  100. uint16_t mss;
  101. uint32_t paylen;
  102. uint16_t tso_frames;
  103. char tse;
  104. int8_t ip;
  105. int8_t tcp;
  106. char cptse; // current packet tse bit
  107. } tx;
  108. struct {
  109. uint32_t val_in; // shifted in from guest driver
  110. uint16_t bitnum_in;
  111. uint16_t bitnum_out;
  112. uint16_t reading;
  113. uint32_t old_eecd;
  114. } eecd_state;
  115. } E1000State;
  116. #define defreg(x) x = (E1000_##x>>2)
  117. enum {
  118. defreg(CTRL), defreg(EECD), defreg(EERD), defreg(GPRC),
  119. defreg(GPTC), defreg(ICR), defreg(ICS), defreg(IMC),
  120. defreg(IMS), defreg(LEDCTL), defreg(MANC), defreg(MDIC),
  121. defreg(MPC), defreg(PBA), defreg(RCTL), defreg(RDBAH),
  122. defreg(RDBAL), defreg(RDH), defreg(RDLEN), defreg(RDT),
  123. defreg(STATUS), defreg(SWSM), defreg(TCTL), defreg(TDBAH),
  124. defreg(TDBAL), defreg(TDH), defreg(TDLEN), defreg(TDT),
  125. defreg(TORH), defreg(TORL), defreg(TOTH), defreg(TOTL),
  126. defreg(TPR), defreg(TPT), defreg(TXDCTL), defreg(WUFC),
  127. defreg(RA), defreg(MTA), defreg(CRCERRS),defreg(VFTA),
  128. defreg(VET),
  129. };
  130. enum { PHY_R = 1, PHY_W = 2, PHY_RW = PHY_R | PHY_W };
  131. static const char phy_regcap[0x20] = {
  132. [PHY_STATUS] = PHY_R, [M88E1000_EXT_PHY_SPEC_CTRL] = PHY_RW,
  133. [PHY_ID1] = PHY_R, [M88E1000_PHY_SPEC_CTRL] = PHY_RW,
  134. [PHY_CTRL] = PHY_RW, [PHY_1000T_CTRL] = PHY_RW,
  135. [PHY_LP_ABILITY] = PHY_R, [PHY_1000T_STATUS] = PHY_R,
  136. [PHY_AUTONEG_ADV] = PHY_RW, [M88E1000_RX_ERR_CNTR] = PHY_R,
  137. [PHY_ID2] = PHY_R, [M88E1000_PHY_SPEC_STATUS] = PHY_R
  138. };
  139. static void
  140. set_interrupt_cause(E1000State *s, int index, uint32_t val)
  141. {
  142. if (val)
  143. val |= E1000_ICR_INT_ASSERTED;
  144. s->mac_reg[ICR] = val;
  145. s->mac_reg[ICS] = val;
  146. qemu_set_irq(s->dev.irq[0], (s->mac_reg[IMS] & s->mac_reg[ICR]) != 0);
  147. }
  148. static void
  149. set_ics(E1000State *s, int index, uint32_t val)
  150. {
  151. DBGOUT(INTERRUPT, "set_ics %x, ICR %x, IMR %x\n", val, s->mac_reg[ICR],
  152. s->mac_reg[IMS]);
  153. set_interrupt_cause(s, 0, val | s->mac_reg[ICR]);
  154. }
  155. static int
  156. rxbufsize(uint32_t v)
  157. {
  158. v &= E1000_RCTL_BSEX | E1000_RCTL_SZ_16384 | E1000_RCTL_SZ_8192 |
  159. E1000_RCTL_SZ_4096 | E1000_RCTL_SZ_2048 | E1000_RCTL_SZ_1024 |
  160. E1000_RCTL_SZ_512 | E1000_RCTL_SZ_256;
  161. switch (v) {
  162. case E1000_RCTL_BSEX | E1000_RCTL_SZ_16384:
  163. return 16384;
  164. case E1000_RCTL_BSEX | E1000_RCTL_SZ_8192:
  165. return 8192;
  166. case E1000_RCTL_BSEX | E1000_RCTL_SZ_4096:
  167. return 4096;
  168. case E1000_RCTL_SZ_1024:
  169. return 1024;
  170. case E1000_RCTL_SZ_512:
  171. return 512;
  172. case E1000_RCTL_SZ_256:
  173. return 256;
  174. }
  175. return 2048;
  176. }
  177. static void
  178. set_ctrl(E1000State *s, int index, uint32_t val)
  179. {
  180. /* RST is self clearing */
  181. s->mac_reg[CTRL] = val & ~E1000_CTRL_RST;
  182. }
  183. static void
  184. set_rx_control(E1000State *s, int index, uint32_t val)
  185. {
  186. s->mac_reg[RCTL] = val;
  187. s->rxbuf_size = rxbufsize(val);
  188. s->rxbuf_min_shift = ((val / E1000_RCTL_RDMTS_QUAT) & 3) + 1;
  189. DBGOUT(RX, "RCTL: %d, mac_reg[RCTL] = 0x%x\n", s->mac_reg[RDT],
  190. s->mac_reg[RCTL]);
  191. }
  192. static void
  193. set_mdic(E1000State *s, int index, uint32_t val)
  194. {
  195. uint32_t data = val & E1000_MDIC_DATA_MASK;
  196. uint32_t addr = ((val & E1000_MDIC_REG_MASK) >> E1000_MDIC_REG_SHIFT);
  197. if ((val & E1000_MDIC_PHY_MASK) >> E1000_MDIC_PHY_SHIFT != 1) // phy #
  198. val = s->mac_reg[MDIC] | E1000_MDIC_ERROR;
  199. else if (val & E1000_MDIC_OP_READ) {
  200. DBGOUT(MDIC, "MDIC read reg 0x%x\n", addr);
  201. if (!(phy_regcap[addr] & PHY_R)) {
  202. DBGOUT(MDIC, "MDIC read reg %x unhandled\n", addr);
  203. val |= E1000_MDIC_ERROR;
  204. } else
  205. val = (val ^ data) | s->phy_reg[addr];
  206. } else if (val & E1000_MDIC_OP_WRITE) {
  207. DBGOUT(MDIC, "MDIC write reg 0x%x, value 0x%x\n", addr, data);
  208. if (!(phy_regcap[addr] & PHY_W)) {
  209. DBGOUT(MDIC, "MDIC write reg %x unhandled\n", addr);
  210. val |= E1000_MDIC_ERROR;
  211. } else
  212. s->phy_reg[addr] = data;
  213. }
  214. s->mac_reg[MDIC] = val | E1000_MDIC_READY;
  215. set_ics(s, 0, E1000_ICR_MDAC);
  216. }
  217. static uint32_t
  218. get_eecd(E1000State *s, int index)
  219. {
  220. uint32_t ret = E1000_EECD_PRES|E1000_EECD_GNT | s->eecd_state.old_eecd;
  221. DBGOUT(EEPROM, "reading eeprom bit %d (reading %d)\n",
  222. s->eecd_state.bitnum_out, s->eecd_state.reading);
  223. if (!s->eecd_state.reading ||
  224. ((s->eeprom_data[(s->eecd_state.bitnum_out >> 4) & 0x3f] >>
  225. ((s->eecd_state.bitnum_out & 0xf) ^ 0xf))) & 1)
  226. ret |= E1000_EECD_DO;
  227. return ret;
  228. }
  229. static void
  230. set_eecd(E1000State *s, int index, uint32_t val)
  231. {
  232. uint32_t oldval = s->eecd_state.old_eecd;
  233. s->eecd_state.old_eecd = val & (E1000_EECD_SK | E1000_EECD_CS |
  234. E1000_EECD_DI|E1000_EECD_FWE_MASK|E1000_EECD_REQ);
  235. if (!(E1000_EECD_CS & val)) // CS inactive; nothing to do
  236. return;
  237. if (E1000_EECD_CS & (val ^ oldval)) { // CS rise edge; reset state
  238. s->eecd_state.val_in = 0;
  239. s->eecd_state.bitnum_in = 0;
  240. s->eecd_state.bitnum_out = 0;
  241. s->eecd_state.reading = 0;
  242. }
  243. if (!(E1000_EECD_SK & (val ^ oldval))) // no clock edge
  244. return;
  245. if (!(E1000_EECD_SK & val)) { // falling edge
  246. s->eecd_state.bitnum_out++;
  247. return;
  248. }
  249. s->eecd_state.val_in <<= 1;
  250. if (val & E1000_EECD_DI)
  251. s->eecd_state.val_in |= 1;
  252. if (++s->eecd_state.bitnum_in == 9 && !s->eecd_state.reading) {
  253. s->eecd_state.bitnum_out = ((s->eecd_state.val_in & 0x3f)<<4)-1;
  254. s->eecd_state.reading = (((s->eecd_state.val_in >> 6) & 7) ==
  255. EEPROM_READ_OPCODE_MICROWIRE);
  256. }
  257. DBGOUT(EEPROM, "eeprom bitnum in %d out %d, reading %d\n",
  258. s->eecd_state.bitnum_in, s->eecd_state.bitnum_out,
  259. s->eecd_state.reading);
  260. }
  261. static uint32_t
  262. flash_eerd_read(E1000State *s, int x)
  263. {
  264. unsigned int index, r = s->mac_reg[EERD] & ~E1000_EEPROM_RW_REG_START;
  265. if ((s->mac_reg[EERD] & E1000_EEPROM_RW_REG_START) == 0)
  266. return (s->mac_reg[EERD]);
  267. if ((index = r >> E1000_EEPROM_RW_ADDR_SHIFT) > EEPROM_CHECKSUM_REG)
  268. return (E1000_EEPROM_RW_REG_DONE | r);
  269. return ((s->eeprom_data[index] << E1000_EEPROM_RW_REG_DATA) |
  270. E1000_EEPROM_RW_REG_DONE | r);
  271. }
  272. static void
  273. putsum(uint8_t *data, uint32_t n, uint32_t sloc, uint32_t css, uint32_t cse)
  274. {
  275. uint32_t sum;
  276. if (cse && cse < n)
  277. n = cse + 1;
  278. if (sloc < n-1) {
  279. sum = net_checksum_add(n-css, data+css);
  280. cpu_to_be16wu((uint16_t *)(data + sloc),
  281. net_checksum_finish(sum));
  282. }
  283. }
  284. static inline int
  285. vlan_enabled(E1000State *s)
  286. {
  287. return ((s->mac_reg[CTRL] & E1000_CTRL_VME) != 0);
  288. }
  289. static inline int
  290. vlan_rx_filter_enabled(E1000State *s)
  291. {
  292. return ((s->mac_reg[RCTL] & E1000_RCTL_VFE) != 0);
  293. }
  294. static inline int
  295. is_vlan_packet(E1000State *s, const uint8_t *buf)
  296. {
  297. return (be16_to_cpup((uint16_t *)(buf + 12)) ==
  298. le16_to_cpup((uint16_t *)(s->mac_reg + VET)));
  299. }
  300. static inline int
  301. is_vlan_txd(uint32_t txd_lower)
  302. {
  303. return ((txd_lower & E1000_TXD_CMD_VLE) != 0);
  304. }
  305. /* FCS aka Ethernet CRC-32. We don't get it from backends and can't
  306. * fill it in, just pad descriptor length by 4 bytes unless guest
  307. * told us to strip it off the packet. */
  308. static inline int
  309. fcs_len(E1000State *s)
  310. {
  311. return (s->mac_reg[RCTL] & E1000_RCTL_SECRC) ? 0 : 4;
  312. }
  313. static void
  314. xmit_seg(E1000State *s)
  315. {
  316. uint16_t len, *sp;
  317. unsigned int frames = s->tx.tso_frames, css, sofar, n;
  318. struct e1000_tx *tp = &s->tx;
  319. if (tp->tse && tp->cptse) {
  320. css = tp->ipcss;
  321. DBGOUT(TXSUM, "frames %d size %d ipcss %d\n",
  322. frames, tp->size, css);
  323. if (tp->ip) { // IPv4
  324. cpu_to_be16wu((uint16_t *)(tp->data+css+2),
  325. tp->size - css);
  326. cpu_to_be16wu((uint16_t *)(tp->data+css+4),
  327. be16_to_cpup((uint16_t *)(tp->data+css+4))+frames);
  328. } else // IPv6
  329. cpu_to_be16wu((uint16_t *)(tp->data+css+4),
  330. tp->size - css);
  331. css = tp->tucss;
  332. len = tp->size - css;
  333. DBGOUT(TXSUM, "tcp %d tucss %d len %d\n", tp->tcp, css, len);
  334. if (tp->tcp) {
  335. sofar = frames * tp->mss;
  336. cpu_to_be32wu((uint32_t *)(tp->data+css+4), // seq
  337. be32_to_cpupu((uint32_t *)(tp->data+css+4))+sofar);
  338. if (tp->paylen - sofar > tp->mss)
  339. tp->data[css + 13] &= ~9; // PSH, FIN
  340. } else // UDP
  341. cpu_to_be16wu((uint16_t *)(tp->data+css+4), len);
  342. if (tp->sum_needed & E1000_TXD_POPTS_TXSM) {
  343. unsigned int phsum;
  344. // add pseudo-header length before checksum calculation
  345. sp = (uint16_t *)(tp->data + tp->tucso);
  346. phsum = be16_to_cpup(sp) + len;
  347. phsum = (phsum >> 16) + (phsum & 0xffff);
  348. cpu_to_be16wu(sp, phsum);
  349. }
  350. tp->tso_frames++;
  351. }
  352. if (tp->sum_needed & E1000_TXD_POPTS_TXSM)
  353. putsum(tp->data, tp->size, tp->tucso, tp->tucss, tp->tucse);
  354. if (tp->sum_needed & E1000_TXD_POPTS_IXSM)
  355. putsum(tp->data, tp->size, tp->ipcso, tp->ipcss, tp->ipcse);
  356. if (tp->vlan_needed) {
  357. memmove(tp->vlan, tp->data, 4);
  358. memmove(tp->data, tp->data + 4, 8);
  359. memcpy(tp->data + 8, tp->vlan_header, 4);
  360. qemu_send_packet(&s->nic->nc, tp->vlan, tp->size + 4);
  361. } else
  362. qemu_send_packet(&s->nic->nc, tp->data, tp->size);
  363. s->mac_reg[TPT]++;
  364. s->mac_reg[GPTC]++;
  365. n = s->mac_reg[TOTL];
  366. if ((s->mac_reg[TOTL] += s->tx.size) < n)
  367. s->mac_reg[TOTH]++;
  368. }
  369. static void
  370. process_tx_desc(E1000State *s, struct e1000_tx_desc *dp)
  371. {
  372. uint32_t txd_lower = le32_to_cpu(dp->lower.data);
  373. uint32_t dtype = txd_lower & (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D);
  374. unsigned int split_size = txd_lower & 0xffff, bytes, sz, op;
  375. unsigned int msh = 0xfffff, hdr = 0;
  376. uint64_t addr;
  377. struct e1000_context_desc *xp = (struct e1000_context_desc *)dp;
  378. struct e1000_tx *tp = &s->tx;
  379. if (dtype == E1000_TXD_CMD_DEXT) { // context descriptor
  380. op = le32_to_cpu(xp->cmd_and_length);
  381. tp->ipcss = xp->lower_setup.ip_fields.ipcss;
  382. tp->ipcso = xp->lower_setup.ip_fields.ipcso;
  383. tp->ipcse = le16_to_cpu(xp->lower_setup.ip_fields.ipcse);
  384. tp->tucss = xp->upper_setup.tcp_fields.tucss;
  385. tp->tucso = xp->upper_setup.tcp_fields.tucso;
  386. tp->tucse = le16_to_cpu(xp->upper_setup.tcp_fields.tucse);
  387. tp->paylen = op & 0xfffff;
  388. tp->hdr_len = xp->tcp_seg_setup.fields.hdr_len;
  389. tp->mss = le16_to_cpu(xp->tcp_seg_setup.fields.mss);
  390. tp->ip = (op & E1000_TXD_CMD_IP) ? 1 : 0;
  391. tp->tcp = (op & E1000_TXD_CMD_TCP) ? 1 : 0;
  392. tp->tse = (op & E1000_TXD_CMD_TSE) ? 1 : 0;
  393. tp->tso_frames = 0;
  394. if (tp->tucso == 0) { // this is probably wrong
  395. DBGOUT(TXSUM, "TCP/UDP: cso 0!\n");
  396. tp->tucso = tp->tucss + (tp->tcp ? 16 : 6);
  397. }
  398. return;
  399. } else if (dtype == (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D)) {
  400. // data descriptor
  401. if (tp->size == 0) {
  402. tp->sum_needed = le32_to_cpu(dp->upper.data) >> 8;
  403. }
  404. tp->cptse = ( txd_lower & E1000_TXD_CMD_TSE ) ? 1 : 0;
  405. } else {
  406. // legacy descriptor
  407. tp->cptse = 0;
  408. }
  409. if (vlan_enabled(s) && is_vlan_txd(txd_lower) &&
  410. (tp->cptse || txd_lower & E1000_TXD_CMD_EOP)) {
  411. tp->vlan_needed = 1;
  412. cpu_to_be16wu((uint16_t *)(tp->vlan_header),
  413. le16_to_cpup((uint16_t *)(s->mac_reg + VET)));
  414. cpu_to_be16wu((uint16_t *)(tp->vlan_header + 2),
  415. le16_to_cpu(dp->upper.fields.special));
  416. }
  417. addr = le64_to_cpu(dp->buffer_addr);
  418. if (tp->tse && tp->cptse) {
  419. hdr = tp->hdr_len;
  420. msh = hdr + tp->mss;
  421. do {
  422. bytes = split_size;
  423. if (tp->size + bytes > msh)
  424. bytes = msh - tp->size;
  425. bytes = MIN(sizeof(tp->data) - tp->size, bytes);
  426. pci_dma_read(&s->dev, addr, tp->data + tp->size, bytes);
  427. if ((sz = tp->size + bytes) >= hdr && tp->size < hdr)
  428. memmove(tp->header, tp->data, hdr);
  429. tp->size = sz;
  430. addr += bytes;
  431. if (sz == msh) {
  432. xmit_seg(s);
  433. memmove(tp->data, tp->header, hdr);
  434. tp->size = hdr;
  435. }
  436. } while (split_size -= bytes);
  437. } else if (!tp->tse && tp->cptse) {
  438. // context descriptor TSE is not set, while data descriptor TSE is set
  439. DBGOUT(TXERR, "TCP segmentaion Error\n");
  440. } else {
  441. split_size = MIN(sizeof(tp->data) - tp->size, split_size);
  442. pci_dma_read(&s->dev, addr, tp->data + tp->size, split_size);
  443. tp->size += split_size;
  444. }
  445. if (!(txd_lower & E1000_TXD_CMD_EOP))
  446. return;
  447. if (!(tp->tse && tp->cptse && tp->size < hdr))
  448. xmit_seg(s);
  449. tp->tso_frames = 0;
  450. tp->sum_needed = 0;
  451. tp->vlan_needed = 0;
  452. tp->size = 0;
  453. tp->cptse = 0;
  454. }
  455. static uint32_t
  456. txdesc_writeback(E1000State *s, dma_addr_t base, struct e1000_tx_desc *dp)
  457. {
  458. uint32_t txd_upper, txd_lower = le32_to_cpu(dp->lower.data);
  459. if (!(txd_lower & (E1000_TXD_CMD_RS|E1000_TXD_CMD_RPS)))
  460. return 0;
  461. txd_upper = (le32_to_cpu(dp->upper.data) | E1000_TXD_STAT_DD) &
  462. ~(E1000_TXD_STAT_EC | E1000_TXD_STAT_LC | E1000_TXD_STAT_TU);
  463. dp->upper.data = cpu_to_le32(txd_upper);
  464. pci_dma_write(&s->dev, base + ((char *)&dp->upper - (char *)dp),
  465. (void *)&dp->upper, sizeof(dp->upper));
  466. return E1000_ICR_TXDW;
  467. }
  468. static uint64_t tx_desc_base(E1000State *s)
  469. {
  470. uint64_t bah = s->mac_reg[TDBAH];
  471. uint64_t bal = s->mac_reg[TDBAL] & ~0xf;
  472. return (bah << 32) + bal;
  473. }
  474. static void
  475. start_xmit(E1000State *s)
  476. {
  477. dma_addr_t base;
  478. struct e1000_tx_desc desc;
  479. uint32_t tdh_start = s->mac_reg[TDH], cause = E1000_ICS_TXQE;
  480. if (!(s->mac_reg[TCTL] & E1000_TCTL_EN)) {
  481. DBGOUT(TX, "tx disabled\n");
  482. return;
  483. }
  484. while (s->mac_reg[TDH] != s->mac_reg[TDT]) {
  485. base = tx_desc_base(s) +
  486. sizeof(struct e1000_tx_desc) * s->mac_reg[TDH];
  487. pci_dma_read(&s->dev, base, (void *)&desc, sizeof(desc));
  488. DBGOUT(TX, "index %d: %p : %x %x\n", s->mac_reg[TDH],
  489. (void *)(intptr_t)desc.buffer_addr, desc.lower.data,
  490. desc.upper.data);
  491. process_tx_desc(s, &desc);
  492. cause |= txdesc_writeback(s, base, &desc);
  493. if (++s->mac_reg[TDH] * sizeof(desc) >= s->mac_reg[TDLEN])
  494. s->mac_reg[TDH] = 0;
  495. /*
  496. * the following could happen only if guest sw assigns
  497. * bogus values to TDT/TDLEN.
  498. * there's nothing too intelligent we could do about this.
  499. */
  500. if (s->mac_reg[TDH] == tdh_start) {
  501. DBGOUT(TXERR, "TDH wraparound @%x, TDT %x, TDLEN %x\n",
  502. tdh_start, s->mac_reg[TDT], s->mac_reg[TDLEN]);
  503. break;
  504. }
  505. }
  506. set_ics(s, 0, cause);
  507. }
  508. static int
  509. receive_filter(E1000State *s, const uint8_t *buf, int size)
  510. {
  511. static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  512. static const int mta_shift[] = {4, 3, 2, 0};
  513. uint32_t f, rctl = s->mac_reg[RCTL], ra[2], *rp;
  514. if (is_vlan_packet(s, buf) && vlan_rx_filter_enabled(s)) {
  515. uint16_t vid = be16_to_cpup((uint16_t *)(buf + 14));
  516. uint32_t vfta = le32_to_cpup((uint32_t *)(s->mac_reg + VFTA) +
  517. ((vid >> 5) & 0x7f));
  518. if ((vfta & (1 << (vid & 0x1f))) == 0)
  519. return 0;
  520. }
  521. if (rctl & E1000_RCTL_UPE) // promiscuous
  522. return 1;
  523. if ((buf[0] & 1) && (rctl & E1000_RCTL_MPE)) // promiscuous mcast
  524. return 1;
  525. if ((rctl & E1000_RCTL_BAM) && !memcmp(buf, bcast, sizeof bcast))
  526. return 1;
  527. for (rp = s->mac_reg + RA; rp < s->mac_reg + RA + 32; rp += 2) {
  528. if (!(rp[1] & E1000_RAH_AV))
  529. continue;
  530. ra[0] = cpu_to_le32(rp[0]);
  531. ra[1] = cpu_to_le32(rp[1]);
  532. if (!memcmp(buf, (uint8_t *)ra, 6)) {
  533. DBGOUT(RXFILTER,
  534. "unicast match[%d]: %02x:%02x:%02x:%02x:%02x:%02x\n",
  535. (int)(rp - s->mac_reg - RA)/2,
  536. buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
  537. return 1;
  538. }
  539. }
  540. DBGOUT(RXFILTER, "unicast mismatch: %02x:%02x:%02x:%02x:%02x:%02x\n",
  541. buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
  542. f = mta_shift[(rctl >> E1000_RCTL_MO_SHIFT) & 3];
  543. f = (((buf[5] << 8) | buf[4]) >> f) & 0xfff;
  544. if (s->mac_reg[MTA + (f >> 5)] & (1 << (f & 0x1f)))
  545. return 1;
  546. DBGOUT(RXFILTER,
  547. "dropping, inexact filter mismatch: %02x:%02x:%02x:%02x:%02x:%02x MO %d MTA[%d] %x\n",
  548. buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
  549. (rctl >> E1000_RCTL_MO_SHIFT) & 3, f >> 5,
  550. s->mac_reg[MTA + (f >> 5)]);
  551. return 0;
  552. }
  553. static void
  554. e1000_set_link_status(VLANClientState *nc)
  555. {
  556. E1000State *s = DO_UPCAST(NICState, nc, nc)->opaque;
  557. uint32_t old_status = s->mac_reg[STATUS];
  558. if (nc->link_down) {
  559. s->mac_reg[STATUS] &= ~E1000_STATUS_LU;
  560. s->phy_reg[PHY_STATUS] &= ~MII_SR_LINK_STATUS;
  561. } else {
  562. s->mac_reg[STATUS] |= E1000_STATUS_LU;
  563. s->phy_reg[PHY_STATUS] |= MII_SR_LINK_STATUS;
  564. }
  565. if (s->mac_reg[STATUS] != old_status)
  566. set_ics(s, 0, E1000_ICR_LSC);
  567. }
  568. static bool e1000_has_rxbufs(E1000State *s, size_t total_size)
  569. {
  570. int bufs;
  571. /* Fast-path short packets */
  572. if (total_size <= s->rxbuf_size) {
  573. return s->mac_reg[RDH] != s->mac_reg[RDT] || !s->check_rxov;
  574. }
  575. if (s->mac_reg[RDH] < s->mac_reg[RDT]) {
  576. bufs = s->mac_reg[RDT] - s->mac_reg[RDH];
  577. } else if (s->mac_reg[RDH] > s->mac_reg[RDT] || !s->check_rxov) {
  578. bufs = s->mac_reg[RDLEN] / sizeof(struct e1000_rx_desc) +
  579. s->mac_reg[RDT] - s->mac_reg[RDH];
  580. } else {
  581. return false;
  582. }
  583. return total_size <= bufs * s->rxbuf_size;
  584. }
  585. static int
  586. e1000_can_receive(VLANClientState *nc)
  587. {
  588. E1000State *s = DO_UPCAST(NICState, nc, nc)->opaque;
  589. return (s->mac_reg[RCTL] & E1000_RCTL_EN) && e1000_has_rxbufs(s, 1);
  590. }
  591. static uint64_t rx_desc_base(E1000State *s)
  592. {
  593. uint64_t bah = s->mac_reg[RDBAH];
  594. uint64_t bal = s->mac_reg[RDBAL] & ~0xf;
  595. return (bah << 32) + bal;
  596. }
  597. static ssize_t
  598. e1000_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
  599. {
  600. E1000State *s = DO_UPCAST(NICState, nc, nc)->opaque;
  601. struct e1000_rx_desc desc;
  602. dma_addr_t base;
  603. unsigned int n, rdt;
  604. uint32_t rdh_start;
  605. uint16_t vlan_special = 0;
  606. uint8_t vlan_status = 0, vlan_offset = 0;
  607. uint8_t min_buf[MIN_BUF_SIZE];
  608. size_t desc_offset;
  609. size_t desc_size;
  610. size_t total_size;
  611. if (!(s->mac_reg[RCTL] & E1000_RCTL_EN))
  612. return -1;
  613. /* Pad to minimum Ethernet frame length */
  614. if (size < sizeof(min_buf)) {
  615. memcpy(min_buf, buf, size);
  616. memset(&min_buf[size], 0, sizeof(min_buf) - size);
  617. buf = min_buf;
  618. size = sizeof(min_buf);
  619. }
  620. if (!receive_filter(s, buf, size))
  621. return size;
  622. if (vlan_enabled(s) && is_vlan_packet(s, buf)) {
  623. vlan_special = cpu_to_le16(be16_to_cpup((uint16_t *)(buf + 14)));
  624. memmove((uint8_t *)buf + 4, buf, 12);
  625. vlan_status = E1000_RXD_STAT_VP;
  626. vlan_offset = 4;
  627. size -= 4;
  628. }
  629. rdh_start = s->mac_reg[RDH];
  630. desc_offset = 0;
  631. total_size = size + fcs_len(s);
  632. if (!e1000_has_rxbufs(s, total_size)) {
  633. set_ics(s, 0, E1000_ICS_RXO);
  634. return -1;
  635. }
  636. do {
  637. desc_size = total_size - desc_offset;
  638. if (desc_size > s->rxbuf_size) {
  639. desc_size = s->rxbuf_size;
  640. }
  641. base = rx_desc_base(s) + sizeof(desc) * s->mac_reg[RDH];
  642. pci_dma_read(&s->dev, base, (void *)&desc, sizeof(desc));
  643. desc.special = vlan_special;
  644. desc.status |= (vlan_status | E1000_RXD_STAT_DD);
  645. if (desc.buffer_addr) {
  646. if (desc_offset < size) {
  647. size_t copy_size = size - desc_offset;
  648. if (copy_size > s->rxbuf_size) {
  649. copy_size = s->rxbuf_size;
  650. }
  651. pci_dma_write(&s->dev, le64_to_cpu(desc.buffer_addr),
  652. (void *)(buf + desc_offset + vlan_offset),
  653. copy_size);
  654. }
  655. desc_offset += desc_size;
  656. desc.length = cpu_to_le16(desc_size);
  657. if (desc_offset >= total_size) {
  658. desc.status |= E1000_RXD_STAT_EOP | E1000_RXD_STAT_IXSM;
  659. } else {
  660. /* Guest zeroing out status is not a hardware requirement.
  661. Clear EOP in case guest didn't do it. */
  662. desc.status &= ~E1000_RXD_STAT_EOP;
  663. }
  664. } else { // as per intel docs; skip descriptors with null buf addr
  665. DBGOUT(RX, "Null RX descriptor!!\n");
  666. }
  667. pci_dma_write(&s->dev, base, (void *)&desc, sizeof(desc));
  668. if (++s->mac_reg[RDH] * sizeof(desc) >= s->mac_reg[RDLEN])
  669. s->mac_reg[RDH] = 0;
  670. s->check_rxov = 1;
  671. /* see comment in start_xmit; same here */
  672. if (s->mac_reg[RDH] == rdh_start) {
  673. DBGOUT(RXERR, "RDH wraparound @%x, RDT %x, RDLEN %x\n",
  674. rdh_start, s->mac_reg[RDT], s->mac_reg[RDLEN]);
  675. set_ics(s, 0, E1000_ICS_RXO);
  676. return -1;
  677. }
  678. } while (desc_offset < total_size);
  679. s->mac_reg[GPRC]++;
  680. s->mac_reg[TPR]++;
  681. /* TOR - Total Octets Received:
  682. * This register includes bytes received in a packet from the <Destination
  683. * Address> field through the <CRC> field, inclusively.
  684. */
  685. n = s->mac_reg[TORL] + size + /* Always include FCS length. */ 4;
  686. if (n < s->mac_reg[TORL])
  687. s->mac_reg[TORH]++;
  688. s->mac_reg[TORL] = n;
  689. n = E1000_ICS_RXT0;
  690. if ((rdt = s->mac_reg[RDT]) < s->mac_reg[RDH])
  691. rdt += s->mac_reg[RDLEN] / sizeof(desc);
  692. if (((rdt - s->mac_reg[RDH]) * sizeof(desc)) <= s->mac_reg[RDLEN] >>
  693. s->rxbuf_min_shift)
  694. n |= E1000_ICS_RXDMT0;
  695. set_ics(s, 0, n);
  696. return size;
  697. }
  698. static uint32_t
  699. mac_readreg(E1000State *s, int index)
  700. {
  701. return s->mac_reg[index];
  702. }
  703. static uint32_t
  704. mac_icr_read(E1000State *s, int index)
  705. {
  706. uint32_t ret = s->mac_reg[ICR];
  707. DBGOUT(INTERRUPT, "ICR read: %x\n", ret);
  708. set_interrupt_cause(s, 0, 0);
  709. return ret;
  710. }
  711. static uint32_t
  712. mac_read_clr4(E1000State *s, int index)
  713. {
  714. uint32_t ret = s->mac_reg[index];
  715. s->mac_reg[index] = 0;
  716. return ret;
  717. }
  718. static uint32_t
  719. mac_read_clr8(E1000State *s, int index)
  720. {
  721. uint32_t ret = s->mac_reg[index];
  722. s->mac_reg[index] = 0;
  723. s->mac_reg[index-1] = 0;
  724. return ret;
  725. }
  726. static void
  727. mac_writereg(E1000State *s, int index, uint32_t val)
  728. {
  729. s->mac_reg[index] = val;
  730. }
  731. static void
  732. set_rdt(E1000State *s, int index, uint32_t val)
  733. {
  734. s->check_rxov = 0;
  735. s->mac_reg[index] = val & 0xffff;
  736. }
  737. static void
  738. set_16bit(E1000State *s, int index, uint32_t val)
  739. {
  740. s->mac_reg[index] = val & 0xffff;
  741. }
  742. static void
  743. set_dlen(E1000State *s, int index, uint32_t val)
  744. {
  745. s->mac_reg[index] = val & 0xfff80;
  746. }
  747. static void
  748. set_tctl(E1000State *s, int index, uint32_t val)
  749. {
  750. s->mac_reg[index] = val;
  751. s->mac_reg[TDT] &= 0xffff;
  752. start_xmit(s);
  753. }
  754. static void
  755. set_icr(E1000State *s, int index, uint32_t val)
  756. {
  757. DBGOUT(INTERRUPT, "set_icr %x\n", val);
  758. set_interrupt_cause(s, 0, s->mac_reg[ICR] & ~val);
  759. }
  760. static void
  761. set_imc(E1000State *s, int index, uint32_t val)
  762. {
  763. s->mac_reg[IMS] &= ~val;
  764. set_ics(s, 0, 0);
  765. }
  766. static void
  767. set_ims(E1000State *s, int index, uint32_t val)
  768. {
  769. s->mac_reg[IMS] |= val;
  770. set_ics(s, 0, 0);
  771. }
  772. #define getreg(x) [x] = mac_readreg
  773. static uint32_t (*macreg_readops[])(E1000State *, int) = {
  774. getreg(PBA), getreg(RCTL), getreg(TDH), getreg(TXDCTL),
  775. getreg(WUFC), getreg(TDT), getreg(CTRL), getreg(LEDCTL),
  776. getreg(MANC), getreg(MDIC), getreg(SWSM), getreg(STATUS),
  777. getreg(TORL), getreg(TOTL), getreg(IMS), getreg(TCTL),
  778. getreg(RDH), getreg(RDT), getreg(VET), getreg(ICS),
  779. getreg(TDBAL), getreg(TDBAH), getreg(RDBAH), getreg(RDBAL),
  780. getreg(TDLEN), getreg(RDLEN),
  781. [TOTH] = mac_read_clr8, [TORH] = mac_read_clr8, [GPRC] = mac_read_clr4,
  782. [GPTC] = mac_read_clr4, [TPR] = mac_read_clr4, [TPT] = mac_read_clr4,
  783. [ICR] = mac_icr_read, [EECD] = get_eecd, [EERD] = flash_eerd_read,
  784. [CRCERRS ... MPC] = &mac_readreg,
  785. [RA ... RA+31] = &mac_readreg,
  786. [MTA ... MTA+127] = &mac_readreg,
  787. [VFTA ... VFTA+127] = &mac_readreg,
  788. };
  789. enum { NREADOPS = ARRAY_SIZE(macreg_readops) };
  790. #define putreg(x) [x] = mac_writereg
  791. static void (*macreg_writeops[])(E1000State *, int, uint32_t) = {
  792. putreg(PBA), putreg(EERD), putreg(SWSM), putreg(WUFC),
  793. putreg(TDBAL), putreg(TDBAH), putreg(TXDCTL), putreg(RDBAH),
  794. putreg(RDBAL), putreg(LEDCTL), putreg(VET),
  795. [TDLEN] = set_dlen, [RDLEN] = set_dlen, [TCTL] = set_tctl,
  796. [TDT] = set_tctl, [MDIC] = set_mdic, [ICS] = set_ics,
  797. [TDH] = set_16bit, [RDH] = set_16bit, [RDT] = set_rdt,
  798. [IMC] = set_imc, [IMS] = set_ims, [ICR] = set_icr,
  799. [EECD] = set_eecd, [RCTL] = set_rx_control, [CTRL] = set_ctrl,
  800. [RA ... RA+31] = &mac_writereg,
  801. [MTA ... MTA+127] = &mac_writereg,
  802. [VFTA ... VFTA+127] = &mac_writereg,
  803. };
  804. enum { NWRITEOPS = ARRAY_SIZE(macreg_writeops) };
  805. static void
  806. e1000_mmio_write(void *opaque, target_phys_addr_t addr, uint64_t val,
  807. unsigned size)
  808. {
  809. E1000State *s = opaque;
  810. unsigned int index = (addr & 0x1ffff) >> 2;
  811. if (index < NWRITEOPS && macreg_writeops[index]) {
  812. macreg_writeops[index](s, index, val);
  813. } else if (index < NREADOPS && macreg_readops[index]) {
  814. DBGOUT(MMIO, "e1000_mmio_writel RO %x: 0x%04"PRIx64"\n", index<<2, val);
  815. } else {
  816. DBGOUT(UNKNOWN, "MMIO unknown write addr=0x%08x,val=0x%08"PRIx64"\n",
  817. index<<2, val);
  818. }
  819. }
  820. static uint64_t
  821. e1000_mmio_read(void *opaque, target_phys_addr_t addr, unsigned size)
  822. {
  823. E1000State *s = opaque;
  824. unsigned int index = (addr & 0x1ffff) >> 2;
  825. if (index < NREADOPS && macreg_readops[index])
  826. {
  827. return macreg_readops[index](s, index);
  828. }
  829. DBGOUT(UNKNOWN, "MMIO unknown read addr=0x%08x\n", index<<2);
  830. return 0;
  831. }
  832. static const MemoryRegionOps e1000_mmio_ops = {
  833. .read = e1000_mmio_read,
  834. .write = e1000_mmio_write,
  835. .endianness = DEVICE_LITTLE_ENDIAN,
  836. .impl = {
  837. .min_access_size = 4,
  838. .max_access_size = 4,
  839. },
  840. };
  841. static uint64_t e1000_io_read(void *opaque, target_phys_addr_t addr,
  842. unsigned size)
  843. {
  844. E1000State *s = opaque;
  845. (void)s;
  846. return 0;
  847. }
  848. static void e1000_io_write(void *opaque, target_phys_addr_t addr,
  849. uint64_t val, unsigned size)
  850. {
  851. E1000State *s = opaque;
  852. (void)s;
  853. }
  854. static const MemoryRegionOps e1000_io_ops = {
  855. .read = e1000_io_read,
  856. .write = e1000_io_write,
  857. .endianness = DEVICE_LITTLE_ENDIAN,
  858. };
  859. static bool is_version_1(void *opaque, int version_id)
  860. {
  861. return version_id == 1;
  862. }
  863. static const VMStateDescription vmstate_e1000 = {
  864. .name = "e1000",
  865. .version_id = 2,
  866. .minimum_version_id = 1,
  867. .minimum_version_id_old = 1,
  868. .fields = (VMStateField []) {
  869. VMSTATE_PCI_DEVICE(dev, E1000State),
  870. VMSTATE_UNUSED_TEST(is_version_1, 4), /* was instance id */
  871. VMSTATE_UNUSED(4), /* Was mmio_base. */
  872. VMSTATE_UINT32(rxbuf_size, E1000State),
  873. VMSTATE_UINT32(rxbuf_min_shift, E1000State),
  874. VMSTATE_UINT32(eecd_state.val_in, E1000State),
  875. VMSTATE_UINT16(eecd_state.bitnum_in, E1000State),
  876. VMSTATE_UINT16(eecd_state.bitnum_out, E1000State),
  877. VMSTATE_UINT16(eecd_state.reading, E1000State),
  878. VMSTATE_UINT32(eecd_state.old_eecd, E1000State),
  879. VMSTATE_UINT8(tx.ipcss, E1000State),
  880. VMSTATE_UINT8(tx.ipcso, E1000State),
  881. VMSTATE_UINT16(tx.ipcse, E1000State),
  882. VMSTATE_UINT8(tx.tucss, E1000State),
  883. VMSTATE_UINT8(tx.tucso, E1000State),
  884. VMSTATE_UINT16(tx.tucse, E1000State),
  885. VMSTATE_UINT32(tx.paylen, E1000State),
  886. VMSTATE_UINT8(tx.hdr_len, E1000State),
  887. VMSTATE_UINT16(tx.mss, E1000State),
  888. VMSTATE_UINT16(tx.size, E1000State),
  889. VMSTATE_UINT16(tx.tso_frames, E1000State),
  890. VMSTATE_UINT8(tx.sum_needed, E1000State),
  891. VMSTATE_INT8(tx.ip, E1000State),
  892. VMSTATE_INT8(tx.tcp, E1000State),
  893. VMSTATE_BUFFER(tx.header, E1000State),
  894. VMSTATE_BUFFER(tx.data, E1000State),
  895. VMSTATE_UINT16_ARRAY(eeprom_data, E1000State, 64),
  896. VMSTATE_UINT16_ARRAY(phy_reg, E1000State, 0x20),
  897. VMSTATE_UINT32(mac_reg[CTRL], E1000State),
  898. VMSTATE_UINT32(mac_reg[EECD], E1000State),
  899. VMSTATE_UINT32(mac_reg[EERD], E1000State),
  900. VMSTATE_UINT32(mac_reg[GPRC], E1000State),
  901. VMSTATE_UINT32(mac_reg[GPTC], E1000State),
  902. VMSTATE_UINT32(mac_reg[ICR], E1000State),
  903. VMSTATE_UINT32(mac_reg[ICS], E1000State),
  904. VMSTATE_UINT32(mac_reg[IMC], E1000State),
  905. VMSTATE_UINT32(mac_reg[IMS], E1000State),
  906. VMSTATE_UINT32(mac_reg[LEDCTL], E1000State),
  907. VMSTATE_UINT32(mac_reg[MANC], E1000State),
  908. VMSTATE_UINT32(mac_reg[MDIC], E1000State),
  909. VMSTATE_UINT32(mac_reg[MPC], E1000State),
  910. VMSTATE_UINT32(mac_reg[PBA], E1000State),
  911. VMSTATE_UINT32(mac_reg[RCTL], E1000State),
  912. VMSTATE_UINT32(mac_reg[RDBAH], E1000State),
  913. VMSTATE_UINT32(mac_reg[RDBAL], E1000State),
  914. VMSTATE_UINT32(mac_reg[RDH], E1000State),
  915. VMSTATE_UINT32(mac_reg[RDLEN], E1000State),
  916. VMSTATE_UINT32(mac_reg[RDT], E1000State),
  917. VMSTATE_UINT32(mac_reg[STATUS], E1000State),
  918. VMSTATE_UINT32(mac_reg[SWSM], E1000State),
  919. VMSTATE_UINT32(mac_reg[TCTL], E1000State),
  920. VMSTATE_UINT32(mac_reg[TDBAH], E1000State),
  921. VMSTATE_UINT32(mac_reg[TDBAL], E1000State),
  922. VMSTATE_UINT32(mac_reg[TDH], E1000State),
  923. VMSTATE_UINT32(mac_reg[TDLEN], E1000State),
  924. VMSTATE_UINT32(mac_reg[TDT], E1000State),
  925. VMSTATE_UINT32(mac_reg[TORH], E1000State),
  926. VMSTATE_UINT32(mac_reg[TORL], E1000State),
  927. VMSTATE_UINT32(mac_reg[TOTH], E1000State),
  928. VMSTATE_UINT32(mac_reg[TOTL], E1000State),
  929. VMSTATE_UINT32(mac_reg[TPR], E1000State),
  930. VMSTATE_UINT32(mac_reg[TPT], E1000State),
  931. VMSTATE_UINT32(mac_reg[TXDCTL], E1000State),
  932. VMSTATE_UINT32(mac_reg[WUFC], E1000State),
  933. VMSTATE_UINT32(mac_reg[VET], E1000State),
  934. VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, RA, 32),
  935. VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, MTA, 128),
  936. VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, VFTA, 128),
  937. VMSTATE_END_OF_LIST()
  938. }
  939. };
  940. static const uint16_t e1000_eeprom_template[64] = {
  941. 0x0000, 0x0000, 0x0000, 0x0000, 0xffff, 0x0000, 0x0000, 0x0000,
  942. 0x3000, 0x1000, 0x6403, E1000_DEVID, 0x8086, E1000_DEVID, 0x8086, 0x3040,
  943. 0x0008, 0x2000, 0x7e14, 0x0048, 0x1000, 0x00d8, 0x0000, 0x2700,
  944. 0x6cc9, 0x3150, 0x0722, 0x040b, 0x0984, 0x0000, 0xc000, 0x0706,
  945. 0x1008, 0x0000, 0x0f04, 0x7fff, 0x4d01, 0xffff, 0xffff, 0xffff,
  946. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  947. 0x0100, 0x4000, 0x121c, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  948. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0x0000,
  949. };
  950. static const uint16_t phy_reg_init[] = {
  951. [PHY_CTRL] = 0x1140, [PHY_STATUS] = 0x796d, // link initially up
  952. [PHY_ID1] = 0x141, [PHY_ID2] = PHY_ID2_INIT,
  953. [PHY_1000T_CTRL] = 0x0e00, [M88E1000_PHY_SPEC_CTRL] = 0x360,
  954. [M88E1000_EXT_PHY_SPEC_CTRL] = 0x0d60, [PHY_AUTONEG_ADV] = 0xde1,
  955. [PHY_LP_ABILITY] = 0x1e0, [PHY_1000T_STATUS] = 0x3c00,
  956. [M88E1000_PHY_SPEC_STATUS] = 0xac00,
  957. };
  958. static const uint32_t mac_reg_init[] = {
  959. [PBA] = 0x00100030,
  960. [LEDCTL] = 0x602,
  961. [CTRL] = E1000_CTRL_SWDPIN2 | E1000_CTRL_SWDPIN0 |
  962. E1000_CTRL_SPD_1000 | E1000_CTRL_SLU,
  963. [STATUS] = 0x80000000 | E1000_STATUS_GIO_MASTER_ENABLE |
  964. E1000_STATUS_ASDV | E1000_STATUS_MTXCKOK |
  965. E1000_STATUS_SPEED_1000 | E1000_STATUS_FD |
  966. E1000_STATUS_LU,
  967. [MANC] = E1000_MANC_EN_MNG2HOST | E1000_MANC_RCV_TCO_EN |
  968. E1000_MANC_ARP_EN | E1000_MANC_0298_EN |
  969. E1000_MANC_RMCP_EN,
  970. };
  971. /* PCI interface */
  972. static void
  973. e1000_mmio_setup(E1000State *d)
  974. {
  975. int i;
  976. const uint32_t excluded_regs[] = {
  977. E1000_MDIC, E1000_ICR, E1000_ICS, E1000_IMS,
  978. E1000_IMC, E1000_TCTL, E1000_TDT, PNPMMIO_SIZE
  979. };
  980. memory_region_init_io(&d->mmio, &e1000_mmio_ops, d, "e1000-mmio",
  981. PNPMMIO_SIZE);
  982. memory_region_add_coalescing(&d->mmio, 0, excluded_regs[0]);
  983. for (i = 0; excluded_regs[i] != PNPMMIO_SIZE; i++)
  984. memory_region_add_coalescing(&d->mmio, excluded_regs[i] + 4,
  985. excluded_regs[i+1] - excluded_regs[i] - 4);
  986. memory_region_init_io(&d->io, &e1000_io_ops, d, "e1000-io", IOPORT_SIZE);
  987. }
  988. static void
  989. e1000_cleanup(VLANClientState *nc)
  990. {
  991. E1000State *s = DO_UPCAST(NICState, nc, nc)->opaque;
  992. s->nic = NULL;
  993. }
  994. static int
  995. pci_e1000_uninit(PCIDevice *dev)
  996. {
  997. E1000State *d = DO_UPCAST(E1000State, dev, dev);
  998. memory_region_destroy(&d->mmio);
  999. memory_region_destroy(&d->io);
  1000. qemu_del_vlan_client(&d->nic->nc);
  1001. return 0;
  1002. }
  1003. static void e1000_reset(void *opaque)
  1004. {
  1005. E1000State *d = opaque;
  1006. memset(d->phy_reg, 0, sizeof d->phy_reg);
  1007. memmove(d->phy_reg, phy_reg_init, sizeof phy_reg_init);
  1008. memset(d->mac_reg, 0, sizeof d->mac_reg);
  1009. memmove(d->mac_reg, mac_reg_init, sizeof mac_reg_init);
  1010. d->rxbuf_min_shift = 1;
  1011. memset(&d->tx, 0, sizeof d->tx);
  1012. }
  1013. static NetClientInfo net_e1000_info = {
  1014. .type = NET_CLIENT_TYPE_NIC,
  1015. .size = sizeof(NICState),
  1016. .can_receive = e1000_can_receive,
  1017. .receive = e1000_receive,
  1018. .cleanup = e1000_cleanup,
  1019. .link_status_changed = e1000_set_link_status,
  1020. };
  1021. static int pci_e1000_init(PCIDevice *pci_dev)
  1022. {
  1023. E1000State *d = DO_UPCAST(E1000State, dev, pci_dev);
  1024. uint8_t *pci_conf;
  1025. uint16_t checksum = 0;
  1026. int i;
  1027. uint8_t *macaddr;
  1028. pci_conf = d->dev.config;
  1029. /* TODO: RST# value should be 0, PCI spec 6.2.4 */
  1030. pci_conf[PCI_CACHE_LINE_SIZE] = 0x10;
  1031. pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin A */
  1032. e1000_mmio_setup(d);
  1033. pci_register_bar(&d->dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio);
  1034. pci_register_bar(&d->dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &d->io);
  1035. memmove(d->eeprom_data, e1000_eeprom_template,
  1036. sizeof e1000_eeprom_template);
  1037. qemu_macaddr_default_if_unset(&d->conf.macaddr);
  1038. macaddr = d->conf.macaddr.a;
  1039. for (i = 0; i < 3; i++)
  1040. d->eeprom_data[i] = (macaddr[2*i+1]<<8) | macaddr[2*i];
  1041. for (i = 0; i < EEPROM_CHECKSUM_REG; i++)
  1042. checksum += d->eeprom_data[i];
  1043. checksum = (uint16_t) EEPROM_SUM - checksum;
  1044. d->eeprom_data[EEPROM_CHECKSUM_REG] = checksum;
  1045. d->nic = qemu_new_nic(&net_e1000_info, &d->conf,
  1046. d->dev.qdev.info->name, d->dev.qdev.id, d);
  1047. qemu_format_nic_info_str(&d->nic->nc, macaddr);
  1048. add_boot_device_path(d->conf.bootindex, &pci_dev->qdev, "/ethernet-phy@0");
  1049. return 0;
  1050. }
  1051. static void qdev_e1000_reset(DeviceState *dev)
  1052. {
  1053. E1000State *d = DO_UPCAST(E1000State, dev.qdev, dev);
  1054. e1000_reset(d);
  1055. }
  1056. static PCIDeviceInfo e1000_info = {
  1057. .qdev.name = "e1000",
  1058. .qdev.desc = "Intel Gigabit Ethernet",
  1059. .qdev.size = sizeof(E1000State),
  1060. .qdev.reset = qdev_e1000_reset,
  1061. .qdev.vmsd = &vmstate_e1000,
  1062. .init = pci_e1000_init,
  1063. .exit = pci_e1000_uninit,
  1064. .romfile = "pxe-e1000.rom",
  1065. .vendor_id = PCI_VENDOR_ID_INTEL,
  1066. .device_id = E1000_DEVID,
  1067. .revision = 0x03,
  1068. .class_id = PCI_CLASS_NETWORK_ETHERNET,
  1069. .qdev.props = (Property[]) {
  1070. DEFINE_NIC_PROPERTIES(E1000State, conf),
  1071. DEFINE_PROP_END_OF_LIST(),
  1072. }
  1073. };
  1074. static void e1000_register_devices(void)
  1075. {
  1076. pci_qdev_register(&e1000_info);
  1077. }
  1078. device_init(e1000_register_devices)