e1000.c 43 KB

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