ne2000.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /*
  2. * QEMU NE2000 emulation
  3. *
  4. * Copyright (c) 2003-2004 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "hw.h"
  25. #include "pci.h"
  26. #include "net.h"
  27. #include "ne2000.h"
  28. #include "loader.h"
  29. #include "sysemu.h"
  30. /* debug NE2000 card */
  31. //#define DEBUG_NE2000
  32. #define MAX_ETH_FRAME_SIZE 1514
  33. #define E8390_CMD 0x00 /* The command register (for all pages) */
  34. /* Page 0 register offsets. */
  35. #define EN0_CLDALO 0x01 /* Low byte of current local dma addr RD */
  36. #define EN0_STARTPG 0x01 /* Starting page of ring bfr WR */
  37. #define EN0_CLDAHI 0x02 /* High byte of current local dma addr RD */
  38. #define EN0_STOPPG 0x02 /* Ending page +1 of ring bfr WR */
  39. #define EN0_BOUNDARY 0x03 /* Boundary page of ring bfr RD WR */
  40. #define EN0_TSR 0x04 /* Transmit status reg RD */
  41. #define EN0_TPSR 0x04 /* Transmit starting page WR */
  42. #define EN0_NCR 0x05 /* Number of collision reg RD */
  43. #define EN0_TCNTLO 0x05 /* Low byte of tx byte count WR */
  44. #define EN0_FIFO 0x06 /* FIFO RD */
  45. #define EN0_TCNTHI 0x06 /* High byte of tx byte count WR */
  46. #define EN0_ISR 0x07 /* Interrupt status reg RD WR */
  47. #define EN0_CRDALO 0x08 /* low byte of current remote dma address RD */
  48. #define EN0_RSARLO 0x08 /* Remote start address reg 0 */
  49. #define EN0_CRDAHI 0x09 /* high byte, current remote dma address RD */
  50. #define EN0_RSARHI 0x09 /* Remote start address reg 1 */
  51. #define EN0_RCNTLO 0x0a /* Remote byte count reg WR */
  52. #define EN0_RTL8029ID0 0x0a /* Realtek ID byte #1 RD */
  53. #define EN0_RCNTHI 0x0b /* Remote byte count reg WR */
  54. #define EN0_RTL8029ID1 0x0b /* Realtek ID byte #2 RD */
  55. #define EN0_RSR 0x0c /* rx status reg RD */
  56. #define EN0_RXCR 0x0c /* RX configuration reg WR */
  57. #define EN0_TXCR 0x0d /* TX configuration reg WR */
  58. #define EN0_COUNTER0 0x0d /* Rcv alignment error counter RD */
  59. #define EN0_DCFG 0x0e /* Data configuration reg WR */
  60. #define EN0_COUNTER1 0x0e /* Rcv CRC error counter RD */
  61. #define EN0_IMR 0x0f /* Interrupt mask reg WR */
  62. #define EN0_COUNTER2 0x0f /* Rcv missed frame error counter RD */
  63. #define EN1_PHYS 0x11
  64. #define EN1_CURPAG 0x17
  65. #define EN1_MULT 0x18
  66. #define EN2_STARTPG 0x21 /* Starting page of ring bfr RD */
  67. #define EN2_STOPPG 0x22 /* Ending page +1 of ring bfr RD */
  68. #define EN3_CONFIG0 0x33
  69. #define EN3_CONFIG1 0x34
  70. #define EN3_CONFIG2 0x35
  71. #define EN3_CONFIG3 0x36
  72. /* Register accessed at EN_CMD, the 8390 base addr. */
  73. #define E8390_STOP 0x01 /* Stop and reset the chip */
  74. #define E8390_START 0x02 /* Start the chip, clear reset */
  75. #define E8390_TRANS 0x04 /* Transmit a frame */
  76. #define E8390_RREAD 0x08 /* Remote read */
  77. #define E8390_RWRITE 0x10 /* Remote write */
  78. #define E8390_NODMA 0x20 /* Remote DMA */
  79. #define E8390_PAGE0 0x00 /* Select page chip registers */
  80. #define E8390_PAGE1 0x40 /* using the two high-order bits */
  81. #define E8390_PAGE2 0x80 /* Page 3 is invalid. */
  82. /* Bits in EN0_ISR - Interrupt status register */
  83. #define ENISR_RX 0x01 /* Receiver, no error */
  84. #define ENISR_TX 0x02 /* Transmitter, no error */
  85. #define ENISR_RX_ERR 0x04 /* Receiver, with error */
  86. #define ENISR_TX_ERR 0x08 /* Transmitter, with error */
  87. #define ENISR_OVER 0x10 /* Receiver overwrote the ring */
  88. #define ENISR_COUNTERS 0x20 /* Counters need emptying */
  89. #define ENISR_RDC 0x40 /* remote dma complete */
  90. #define ENISR_RESET 0x80 /* Reset completed */
  91. #define ENISR_ALL 0x3f /* Interrupts we will enable */
  92. /* Bits in received packet status byte and EN0_RSR*/
  93. #define ENRSR_RXOK 0x01 /* Received a good packet */
  94. #define ENRSR_CRC 0x02 /* CRC error */
  95. #define ENRSR_FAE 0x04 /* frame alignment error */
  96. #define ENRSR_FO 0x08 /* FIFO overrun */
  97. #define ENRSR_MPA 0x10 /* missed pkt */
  98. #define ENRSR_PHY 0x20 /* physical/multicast address */
  99. #define ENRSR_DIS 0x40 /* receiver disable. set in monitor mode */
  100. #define ENRSR_DEF 0x80 /* deferring */
  101. /* Transmitted packet status, EN0_TSR. */
  102. #define ENTSR_PTX 0x01 /* Packet transmitted without error */
  103. #define ENTSR_ND 0x02 /* The transmit wasn't deferred. */
  104. #define ENTSR_COL 0x04 /* The transmit collided at least once. */
  105. #define ENTSR_ABT 0x08 /* The transmit collided 16 times, and was deferred. */
  106. #define ENTSR_CRS 0x10 /* The carrier sense was lost. */
  107. #define ENTSR_FU 0x20 /* A "FIFO underrun" occurred during transmit. */
  108. #define ENTSR_CDH 0x40 /* The collision detect "heartbeat" signal was lost. */
  109. #define ENTSR_OWC 0x80 /* There was an out-of-window collision. */
  110. typedef struct PCINE2000State {
  111. PCIDevice dev;
  112. NE2000State ne2000;
  113. } PCINE2000State;
  114. void ne2000_reset(NE2000State *s)
  115. {
  116. int i;
  117. s->isr = ENISR_RESET;
  118. memcpy(s->mem, &s->c.macaddr, 6);
  119. s->mem[14] = 0x57;
  120. s->mem[15] = 0x57;
  121. /* duplicate prom data */
  122. for(i = 15;i >= 0; i--) {
  123. s->mem[2 * i] = s->mem[i];
  124. s->mem[2 * i + 1] = s->mem[i];
  125. }
  126. }
  127. static void ne2000_update_irq(NE2000State *s)
  128. {
  129. int isr;
  130. isr = (s->isr & s->imr) & 0x7f;
  131. #if defined(DEBUG_NE2000)
  132. printf("NE2000: Set IRQ to %d (%02x %02x)\n",
  133. isr ? 1 : 0, s->isr, s->imr);
  134. #endif
  135. qemu_set_irq(s->irq, (isr != 0));
  136. }
  137. static int ne2000_buffer_full(NE2000State *s)
  138. {
  139. int avail, index, boundary;
  140. index = s->curpag << 8;
  141. boundary = s->boundary << 8;
  142. if (index < boundary)
  143. avail = boundary - index;
  144. else
  145. avail = (s->stop - s->start) - (index - boundary);
  146. if (avail < (MAX_ETH_FRAME_SIZE + 4))
  147. return 1;
  148. return 0;
  149. }
  150. int ne2000_can_receive(NetClientState *nc)
  151. {
  152. NE2000State *s = DO_UPCAST(NICState, nc, nc)->opaque;
  153. if (s->cmd & E8390_STOP)
  154. return 1;
  155. return !ne2000_buffer_full(s);
  156. }
  157. #define MIN_BUF_SIZE 60
  158. ssize_t ne2000_receive(NetClientState *nc, const uint8_t *buf, size_t size_)
  159. {
  160. NE2000State *s = DO_UPCAST(NICState, nc, nc)->opaque;
  161. int size = size_;
  162. uint8_t *p;
  163. unsigned int total_len, next, avail, len, index, mcast_idx;
  164. uint8_t buf1[60];
  165. static const uint8_t broadcast_macaddr[6] =
  166. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  167. #if defined(DEBUG_NE2000)
  168. printf("NE2000: received len=%d\n", size);
  169. #endif
  170. if (s->cmd & E8390_STOP || ne2000_buffer_full(s))
  171. return -1;
  172. /* XXX: check this */
  173. if (s->rxcr & 0x10) {
  174. /* promiscuous: receive all */
  175. } else {
  176. if (!memcmp(buf, broadcast_macaddr, 6)) {
  177. /* broadcast address */
  178. if (!(s->rxcr & 0x04))
  179. return size;
  180. } else if (buf[0] & 0x01) {
  181. /* multicast */
  182. if (!(s->rxcr & 0x08))
  183. return size;
  184. mcast_idx = compute_mcast_idx(buf);
  185. if (!(s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7))))
  186. return size;
  187. } else if (s->mem[0] == buf[0] &&
  188. s->mem[2] == buf[1] &&
  189. s->mem[4] == buf[2] &&
  190. s->mem[6] == buf[3] &&
  191. s->mem[8] == buf[4] &&
  192. s->mem[10] == buf[5]) {
  193. /* match */
  194. } else {
  195. return size;
  196. }
  197. }
  198. /* if too small buffer, then expand it */
  199. if (size < MIN_BUF_SIZE) {
  200. memcpy(buf1, buf, size);
  201. memset(buf1 + size, 0, MIN_BUF_SIZE - size);
  202. buf = buf1;
  203. size = MIN_BUF_SIZE;
  204. }
  205. index = s->curpag << 8;
  206. /* 4 bytes for header */
  207. total_len = size + 4;
  208. /* address for next packet (4 bytes for CRC) */
  209. next = index + ((total_len + 4 + 255) & ~0xff);
  210. if (next >= s->stop)
  211. next -= (s->stop - s->start);
  212. /* prepare packet header */
  213. p = s->mem + index;
  214. s->rsr = ENRSR_RXOK; /* receive status */
  215. /* XXX: check this */
  216. if (buf[0] & 0x01)
  217. s->rsr |= ENRSR_PHY;
  218. p[0] = s->rsr;
  219. p[1] = next >> 8;
  220. p[2] = total_len;
  221. p[3] = total_len >> 8;
  222. index += 4;
  223. /* write packet data */
  224. while (size > 0) {
  225. if (index <= s->stop)
  226. avail = s->stop - index;
  227. else
  228. avail = 0;
  229. len = size;
  230. if (len > avail)
  231. len = avail;
  232. memcpy(s->mem + index, buf, len);
  233. buf += len;
  234. index += len;
  235. if (index == s->stop)
  236. index = s->start;
  237. size -= len;
  238. }
  239. s->curpag = next >> 8;
  240. /* now we can signal we have received something */
  241. s->isr |= ENISR_RX;
  242. ne2000_update_irq(s);
  243. return size_;
  244. }
  245. static void ne2000_ioport_write(void *opaque, uint32_t addr, uint32_t val)
  246. {
  247. NE2000State *s = opaque;
  248. int offset, page, index;
  249. addr &= 0xf;
  250. #ifdef DEBUG_NE2000
  251. printf("NE2000: write addr=0x%x val=0x%02x\n", addr, val);
  252. #endif
  253. if (addr == E8390_CMD) {
  254. /* control register */
  255. s->cmd = val;
  256. if (!(val & E8390_STOP)) { /* START bit makes no sense on RTL8029... */
  257. s->isr &= ~ENISR_RESET;
  258. /* test specific case: zero length transfer */
  259. if ((val & (E8390_RREAD | E8390_RWRITE)) &&
  260. s->rcnt == 0) {
  261. s->isr |= ENISR_RDC;
  262. ne2000_update_irq(s);
  263. }
  264. if (val & E8390_TRANS) {
  265. index = (s->tpsr << 8);
  266. /* XXX: next 2 lines are a hack to make netware 3.11 work */
  267. if (index >= NE2000_PMEM_END)
  268. index -= NE2000_PMEM_SIZE;
  269. /* fail safe: check range on the transmitted length */
  270. if (index + s->tcnt <= NE2000_PMEM_END) {
  271. qemu_send_packet(&s->nic->nc, s->mem + index, s->tcnt);
  272. }
  273. /* signal end of transfer */
  274. s->tsr = ENTSR_PTX;
  275. s->isr |= ENISR_TX;
  276. s->cmd &= ~E8390_TRANS;
  277. ne2000_update_irq(s);
  278. }
  279. }
  280. } else {
  281. page = s->cmd >> 6;
  282. offset = addr | (page << 4);
  283. switch(offset) {
  284. case EN0_STARTPG:
  285. s->start = val << 8;
  286. break;
  287. case EN0_STOPPG:
  288. s->stop = val << 8;
  289. break;
  290. case EN0_BOUNDARY:
  291. s->boundary = val;
  292. break;
  293. case EN0_IMR:
  294. s->imr = val;
  295. ne2000_update_irq(s);
  296. break;
  297. case EN0_TPSR:
  298. s->tpsr = val;
  299. break;
  300. case EN0_TCNTLO:
  301. s->tcnt = (s->tcnt & 0xff00) | val;
  302. break;
  303. case EN0_TCNTHI:
  304. s->tcnt = (s->tcnt & 0x00ff) | (val << 8);
  305. break;
  306. case EN0_RSARLO:
  307. s->rsar = (s->rsar & 0xff00) | val;
  308. break;
  309. case EN0_RSARHI:
  310. s->rsar = (s->rsar & 0x00ff) | (val << 8);
  311. break;
  312. case EN0_RCNTLO:
  313. s->rcnt = (s->rcnt & 0xff00) | val;
  314. break;
  315. case EN0_RCNTHI:
  316. s->rcnt = (s->rcnt & 0x00ff) | (val << 8);
  317. break;
  318. case EN0_RXCR:
  319. s->rxcr = val;
  320. break;
  321. case EN0_DCFG:
  322. s->dcfg = val;
  323. break;
  324. case EN0_ISR:
  325. s->isr &= ~(val & 0x7f);
  326. ne2000_update_irq(s);
  327. break;
  328. case EN1_PHYS ... EN1_PHYS + 5:
  329. s->phys[offset - EN1_PHYS] = val;
  330. break;
  331. case EN1_CURPAG:
  332. s->curpag = val;
  333. break;
  334. case EN1_MULT ... EN1_MULT + 7:
  335. s->mult[offset - EN1_MULT] = val;
  336. break;
  337. }
  338. }
  339. }
  340. static uint32_t ne2000_ioport_read(void *opaque, uint32_t addr)
  341. {
  342. NE2000State *s = opaque;
  343. int offset, page, ret;
  344. addr &= 0xf;
  345. if (addr == E8390_CMD) {
  346. ret = s->cmd;
  347. } else {
  348. page = s->cmd >> 6;
  349. offset = addr | (page << 4);
  350. switch(offset) {
  351. case EN0_TSR:
  352. ret = s->tsr;
  353. break;
  354. case EN0_BOUNDARY:
  355. ret = s->boundary;
  356. break;
  357. case EN0_ISR:
  358. ret = s->isr;
  359. break;
  360. case EN0_RSARLO:
  361. ret = s->rsar & 0x00ff;
  362. break;
  363. case EN0_RSARHI:
  364. ret = s->rsar >> 8;
  365. break;
  366. case EN1_PHYS ... EN1_PHYS + 5:
  367. ret = s->phys[offset - EN1_PHYS];
  368. break;
  369. case EN1_CURPAG:
  370. ret = s->curpag;
  371. break;
  372. case EN1_MULT ... EN1_MULT + 7:
  373. ret = s->mult[offset - EN1_MULT];
  374. break;
  375. case EN0_RSR:
  376. ret = s->rsr;
  377. break;
  378. case EN2_STARTPG:
  379. ret = s->start >> 8;
  380. break;
  381. case EN2_STOPPG:
  382. ret = s->stop >> 8;
  383. break;
  384. case EN0_RTL8029ID0:
  385. ret = 0x50;
  386. break;
  387. case EN0_RTL8029ID1:
  388. ret = 0x43;
  389. break;
  390. case EN3_CONFIG0:
  391. ret = 0; /* 10baseT media */
  392. break;
  393. case EN3_CONFIG2:
  394. ret = 0x40; /* 10baseT active */
  395. break;
  396. case EN3_CONFIG3:
  397. ret = 0x40; /* Full duplex */
  398. break;
  399. default:
  400. ret = 0x00;
  401. break;
  402. }
  403. }
  404. #ifdef DEBUG_NE2000
  405. printf("NE2000: read addr=0x%x val=%02x\n", addr, ret);
  406. #endif
  407. return ret;
  408. }
  409. static inline void ne2000_mem_writeb(NE2000State *s, uint32_t addr,
  410. uint32_t val)
  411. {
  412. if (addr < 32 ||
  413. (addr >= NE2000_PMEM_START && addr < NE2000_MEM_SIZE)) {
  414. s->mem[addr] = val;
  415. }
  416. }
  417. static inline void ne2000_mem_writew(NE2000State *s, uint32_t addr,
  418. uint32_t val)
  419. {
  420. addr &= ~1; /* XXX: check exact behaviour if not even */
  421. if (addr < 32 ||
  422. (addr >= NE2000_PMEM_START && addr < NE2000_MEM_SIZE)) {
  423. *(uint16_t *)(s->mem + addr) = cpu_to_le16(val);
  424. }
  425. }
  426. static inline void ne2000_mem_writel(NE2000State *s, uint32_t addr,
  427. uint32_t val)
  428. {
  429. addr &= ~1; /* XXX: check exact behaviour if not even */
  430. if (addr < 32 ||
  431. (addr >= NE2000_PMEM_START && addr < NE2000_MEM_SIZE)) {
  432. cpu_to_le32wu((uint32_t *)(s->mem + addr), val);
  433. }
  434. }
  435. static inline uint32_t ne2000_mem_readb(NE2000State *s, uint32_t addr)
  436. {
  437. if (addr < 32 ||
  438. (addr >= NE2000_PMEM_START && addr < NE2000_MEM_SIZE)) {
  439. return s->mem[addr];
  440. } else {
  441. return 0xff;
  442. }
  443. }
  444. static inline uint32_t ne2000_mem_readw(NE2000State *s, uint32_t addr)
  445. {
  446. addr &= ~1; /* XXX: check exact behaviour if not even */
  447. if (addr < 32 ||
  448. (addr >= NE2000_PMEM_START && addr < NE2000_MEM_SIZE)) {
  449. return le16_to_cpu(*(uint16_t *)(s->mem + addr));
  450. } else {
  451. return 0xffff;
  452. }
  453. }
  454. static inline uint32_t ne2000_mem_readl(NE2000State *s, uint32_t addr)
  455. {
  456. addr &= ~1; /* XXX: check exact behaviour if not even */
  457. if (addr < 32 ||
  458. (addr >= NE2000_PMEM_START && addr < NE2000_MEM_SIZE)) {
  459. return le32_to_cpupu((uint32_t *)(s->mem + addr));
  460. } else {
  461. return 0xffffffff;
  462. }
  463. }
  464. static inline void ne2000_dma_update(NE2000State *s, int len)
  465. {
  466. s->rsar += len;
  467. /* wrap */
  468. /* XXX: check what to do if rsar > stop */
  469. if (s->rsar == s->stop)
  470. s->rsar = s->start;
  471. if (s->rcnt <= len) {
  472. s->rcnt = 0;
  473. /* signal end of transfer */
  474. s->isr |= ENISR_RDC;
  475. ne2000_update_irq(s);
  476. } else {
  477. s->rcnt -= len;
  478. }
  479. }
  480. static void ne2000_asic_ioport_write(void *opaque, uint32_t addr, uint32_t val)
  481. {
  482. NE2000State *s = opaque;
  483. #ifdef DEBUG_NE2000
  484. printf("NE2000: asic write val=0x%04x\n", val);
  485. #endif
  486. if (s->rcnt == 0)
  487. return;
  488. if (s->dcfg & 0x01) {
  489. /* 16 bit access */
  490. ne2000_mem_writew(s, s->rsar, val);
  491. ne2000_dma_update(s, 2);
  492. } else {
  493. /* 8 bit access */
  494. ne2000_mem_writeb(s, s->rsar, val);
  495. ne2000_dma_update(s, 1);
  496. }
  497. }
  498. static uint32_t ne2000_asic_ioport_read(void *opaque, uint32_t addr)
  499. {
  500. NE2000State *s = opaque;
  501. int ret;
  502. if (s->dcfg & 0x01) {
  503. /* 16 bit access */
  504. ret = ne2000_mem_readw(s, s->rsar);
  505. ne2000_dma_update(s, 2);
  506. } else {
  507. /* 8 bit access */
  508. ret = ne2000_mem_readb(s, s->rsar);
  509. ne2000_dma_update(s, 1);
  510. }
  511. #ifdef DEBUG_NE2000
  512. printf("NE2000: asic read val=0x%04x\n", ret);
  513. #endif
  514. return ret;
  515. }
  516. static void ne2000_asic_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
  517. {
  518. NE2000State *s = opaque;
  519. #ifdef DEBUG_NE2000
  520. printf("NE2000: asic writel val=0x%04x\n", val);
  521. #endif
  522. if (s->rcnt == 0)
  523. return;
  524. /* 32 bit access */
  525. ne2000_mem_writel(s, s->rsar, val);
  526. ne2000_dma_update(s, 4);
  527. }
  528. static uint32_t ne2000_asic_ioport_readl(void *opaque, uint32_t addr)
  529. {
  530. NE2000State *s = opaque;
  531. int ret;
  532. /* 32 bit access */
  533. ret = ne2000_mem_readl(s, s->rsar);
  534. ne2000_dma_update(s, 4);
  535. #ifdef DEBUG_NE2000
  536. printf("NE2000: asic readl val=0x%04x\n", ret);
  537. #endif
  538. return ret;
  539. }
  540. static void ne2000_reset_ioport_write(void *opaque, uint32_t addr, uint32_t val)
  541. {
  542. /* nothing to do (end of reset pulse) */
  543. }
  544. static uint32_t ne2000_reset_ioport_read(void *opaque, uint32_t addr)
  545. {
  546. NE2000State *s = opaque;
  547. ne2000_reset(s);
  548. return 0;
  549. }
  550. static int ne2000_post_load(void* opaque, int version_id)
  551. {
  552. NE2000State* s = opaque;
  553. if (version_id < 2) {
  554. s->rxcr = 0x0c;
  555. }
  556. return 0;
  557. }
  558. const VMStateDescription vmstate_ne2000 = {
  559. .name = "ne2000",
  560. .version_id = 2,
  561. .minimum_version_id = 0,
  562. .minimum_version_id_old = 0,
  563. .post_load = ne2000_post_load,
  564. .fields = (VMStateField []) {
  565. VMSTATE_UINT8_V(rxcr, NE2000State, 2),
  566. VMSTATE_UINT8(cmd, NE2000State),
  567. VMSTATE_UINT32(start, NE2000State),
  568. VMSTATE_UINT32(stop, NE2000State),
  569. VMSTATE_UINT8(boundary, NE2000State),
  570. VMSTATE_UINT8(tsr, NE2000State),
  571. VMSTATE_UINT8(tpsr, NE2000State),
  572. VMSTATE_UINT16(tcnt, NE2000State),
  573. VMSTATE_UINT16(rcnt, NE2000State),
  574. VMSTATE_UINT32(rsar, NE2000State),
  575. VMSTATE_UINT8(rsr, NE2000State),
  576. VMSTATE_UINT8(isr, NE2000State),
  577. VMSTATE_UINT8(dcfg, NE2000State),
  578. VMSTATE_UINT8(imr, NE2000State),
  579. VMSTATE_BUFFER(phys, NE2000State),
  580. VMSTATE_UINT8(curpag, NE2000State),
  581. VMSTATE_BUFFER(mult, NE2000State),
  582. VMSTATE_UNUSED(4), /* was irq */
  583. VMSTATE_BUFFER(mem, NE2000State),
  584. VMSTATE_END_OF_LIST()
  585. }
  586. };
  587. static const VMStateDescription vmstate_pci_ne2000 = {
  588. .name = "ne2000",
  589. .version_id = 3,
  590. .minimum_version_id = 3,
  591. .minimum_version_id_old = 3,
  592. .fields = (VMStateField []) {
  593. VMSTATE_PCI_DEVICE(dev, PCINE2000State),
  594. VMSTATE_STRUCT(ne2000, PCINE2000State, 0, vmstate_ne2000, NE2000State),
  595. VMSTATE_END_OF_LIST()
  596. }
  597. };
  598. static uint64_t ne2000_read(void *opaque, target_phys_addr_t addr,
  599. unsigned size)
  600. {
  601. NE2000State *s = opaque;
  602. if (addr < 0x10 && size == 1) {
  603. return ne2000_ioport_read(s, addr);
  604. } else if (addr == 0x10) {
  605. if (size <= 2) {
  606. return ne2000_asic_ioport_read(s, addr);
  607. } else {
  608. return ne2000_asic_ioport_readl(s, addr);
  609. }
  610. } else if (addr == 0x1f && size == 1) {
  611. return ne2000_reset_ioport_read(s, addr);
  612. }
  613. return ((uint64_t)1 << (size * 8)) - 1;
  614. }
  615. static void ne2000_write(void *opaque, target_phys_addr_t addr,
  616. uint64_t data, unsigned size)
  617. {
  618. NE2000State *s = opaque;
  619. if (addr < 0x10 && size == 1) {
  620. ne2000_ioport_write(s, addr, data);
  621. } else if (addr == 0x10) {
  622. if (size <= 2) {
  623. ne2000_asic_ioport_write(s, addr, data);
  624. } else {
  625. ne2000_asic_ioport_writel(s, addr, data);
  626. }
  627. } else if (addr == 0x1f && size == 1) {
  628. ne2000_reset_ioport_write(s, addr, data);
  629. }
  630. }
  631. static const MemoryRegionOps ne2000_ops = {
  632. .read = ne2000_read,
  633. .write = ne2000_write,
  634. .endianness = DEVICE_NATIVE_ENDIAN,
  635. };
  636. /***********************************************************/
  637. /* PCI NE2000 definitions */
  638. void ne2000_setup_io(NE2000State *s, unsigned size)
  639. {
  640. memory_region_init_io(&s->io, &ne2000_ops, s, "ne2000", size);
  641. }
  642. static void ne2000_cleanup(NetClientState *nc)
  643. {
  644. NE2000State *s = DO_UPCAST(NICState, nc, nc)->opaque;
  645. s->nic = NULL;
  646. }
  647. static NetClientInfo net_ne2000_info = {
  648. .type = NET_CLIENT_OPTIONS_KIND_NIC,
  649. .size = sizeof(NICState),
  650. .can_receive = ne2000_can_receive,
  651. .receive = ne2000_receive,
  652. .cleanup = ne2000_cleanup,
  653. };
  654. static int pci_ne2000_init(PCIDevice *pci_dev)
  655. {
  656. PCINE2000State *d = DO_UPCAST(PCINE2000State, dev, pci_dev);
  657. NE2000State *s;
  658. uint8_t *pci_conf;
  659. pci_conf = d->dev.config;
  660. pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin A */
  661. s = &d->ne2000;
  662. ne2000_setup_io(s, 0x100);
  663. pci_register_bar(&d->dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &s->io);
  664. s->irq = d->dev.irq[0];
  665. qemu_macaddr_default_if_unset(&s->c.macaddr);
  666. ne2000_reset(s);
  667. s->nic = qemu_new_nic(&net_ne2000_info, &s->c,
  668. object_get_typename(OBJECT(pci_dev)), pci_dev->qdev.id, s);
  669. qemu_format_nic_info_str(&s->nic->nc, s->c.macaddr.a);
  670. add_boot_device_path(s->c.bootindex, &pci_dev->qdev, "/ethernet-phy@0");
  671. return 0;
  672. }
  673. static void pci_ne2000_exit(PCIDevice *pci_dev)
  674. {
  675. PCINE2000State *d = DO_UPCAST(PCINE2000State, dev, pci_dev);
  676. NE2000State *s = &d->ne2000;
  677. memory_region_destroy(&s->io);
  678. qemu_del_net_client(&s->nic->nc);
  679. }
  680. static Property ne2000_properties[] = {
  681. DEFINE_NIC_PROPERTIES(PCINE2000State, ne2000.c),
  682. DEFINE_PROP_END_OF_LIST(),
  683. };
  684. static void ne2000_class_init(ObjectClass *klass, void *data)
  685. {
  686. DeviceClass *dc = DEVICE_CLASS(klass);
  687. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  688. k->init = pci_ne2000_init;
  689. k->exit = pci_ne2000_exit;
  690. k->romfile = "pxe-ne2k_pci.rom",
  691. k->vendor_id = PCI_VENDOR_ID_REALTEK;
  692. k->device_id = PCI_DEVICE_ID_REALTEK_8029;
  693. k->class_id = PCI_CLASS_NETWORK_ETHERNET;
  694. dc->vmsd = &vmstate_pci_ne2000;
  695. dc->props = ne2000_properties;
  696. }
  697. static TypeInfo ne2000_info = {
  698. .name = "ne2k_pci",
  699. .parent = TYPE_PCI_DEVICE,
  700. .instance_size = sizeof(PCINE2000State),
  701. .class_init = ne2000_class_init,
  702. };
  703. static void ne2000_register_types(void)
  704. {
  705. type_register_static(&ne2000_info);
  706. }
  707. type_init(ne2000_register_types)