eepro100.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  1. /*
  2. * QEMU i8255x (PRO100) emulation
  3. *
  4. * Copyright (c) 2006-2007 Stefan Weil
  5. *
  6. * Portions of the code are copies from grub / etherboot eepro100.c
  7. * and linux e100.c.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Tested features (i82559):
  24. * PXE boot (i386) no valid link
  25. * Linux networking (i386) ok
  26. *
  27. * Untested:
  28. * non-i386 platforms
  29. * Windows networking
  30. *
  31. * References:
  32. *
  33. * Intel 8255x 10/100 Mbps Ethernet Controller Family
  34. * Open Source Software Developer Manual
  35. */
  36. #if defined(TARGET_I386)
  37. # warning "PXE boot still not working!"
  38. #endif
  39. #include <assert.h>
  40. #include <stddef.h> /* offsetof */
  41. #include "hw.h"
  42. #include "pci.h"
  43. #include "net.h"
  44. #include "eeprom93xx.h"
  45. /* Common declarations for all PCI devices. */
  46. #define PCI_VENDOR_ID 0x00 /* 16 bits */
  47. #define PCI_DEVICE_ID 0x02 /* 16 bits */
  48. #define PCI_COMMAND 0x04 /* 16 bits */
  49. #define PCI_STATUS 0x06 /* 16 bits */
  50. #define PCI_REVISION_ID 0x08 /* 8 bits */
  51. #define PCI_CLASS_CODE 0x0b /* 8 bits */
  52. #define PCI_SUBCLASS_CODE 0x0a /* 8 bits */
  53. #define PCI_HEADER_TYPE 0x0e /* 8 bits */
  54. #define PCI_BASE_ADDRESS_0 0x10 /* 32 bits */
  55. #define PCI_BASE_ADDRESS_1 0x14 /* 32 bits */
  56. #define PCI_BASE_ADDRESS_2 0x18 /* 32 bits */
  57. #define PCI_BASE_ADDRESS_3 0x1c /* 32 bits */
  58. #define PCI_BASE_ADDRESS_4 0x20 /* 32 bits */
  59. #define PCI_BASE_ADDRESS_5 0x24 /* 32 bits */
  60. #define PCI_CONFIG_8(offset, value) \
  61. (pci_conf[offset] = (value))
  62. #define PCI_CONFIG_16(offset, value) \
  63. (*(uint16_t *)&pci_conf[offset] = cpu_to_le16(value))
  64. #define PCI_CONFIG_32(offset, value) \
  65. (*(uint32_t *)&pci_conf[offset] = cpu_to_le32(value))
  66. #define KiB 1024
  67. /* debug EEPRO100 card */
  68. //~ #define DEBUG_EEPRO100
  69. #ifdef DEBUG_EEPRO100
  70. #define logout(fmt, args...) fprintf(stderr, "EE100\t%-24s" fmt, __func__, ##args)
  71. #else
  72. #define logout(fmt, args...) ((void)0)
  73. #endif
  74. /* Set flags to 0 to disable debug output. */
  75. #define MDI 0
  76. #define TRACE(flag, command) ((flag) ? (command) : (void)0)
  77. #define missing(text) assert(!"feature is missing in this emulation: " text)
  78. #define MAX_ETH_FRAME_SIZE 1514
  79. /* This driver supports several different devices which are declared here. */
  80. #define i82551 0x82551
  81. #define i82557B 0x82557b
  82. #define i82557C 0x82557c
  83. #define i82558B 0x82558b
  84. #define i82559C 0x82559c
  85. #define i82559ER 0x82559e
  86. #define i82562 0x82562
  87. #define EEPROM_SIZE 64
  88. #define PCI_MEM_SIZE (4 * KiB)
  89. #define PCI_IO_SIZE 64
  90. #define PCI_FLASH_SIZE (128 * KiB)
  91. #define BIT(n) (1 << (n))
  92. #define BITS(n, m) (((0xffffffffU << (31 - n)) >> (31 - n + m)) << m)
  93. /* The SCB accepts the following controls for the Tx and Rx units: */
  94. #define CU_NOP 0x0000 /* No operation. */
  95. #define CU_START 0x0010 /* CU start. */
  96. #define CU_RESUME 0x0020 /* CU resume. */
  97. #define CU_STATSADDR 0x0040 /* Load dump counters address. */
  98. #define CU_SHOWSTATS 0x0050 /* Dump statistical counters. */
  99. #define CU_CMD_BASE 0x0060 /* Load CU base address. */
  100. #define CU_DUMPSTATS 0x0070 /* Dump and reset statistical counters. */
  101. #define CU_SRESUME 0x00a0 /* CU static resume. */
  102. #define RU_NOP 0x0000
  103. #define RX_START 0x0001
  104. #define RX_RESUME 0x0002
  105. #define RX_ABORT 0x0004
  106. #define RX_ADDR_LOAD 0x0006
  107. #define RX_RESUMENR 0x0007
  108. #define INT_MASK 0x0100
  109. #define DRVR_INT 0x0200 /* Driver generated interrupt. */
  110. typedef unsigned char bool;
  111. /* Offsets to the various registers.
  112. All accesses need not be longword aligned. */
  113. enum speedo_offsets {
  114. SCBStatus = 0,
  115. SCBAck = 1,
  116. SCBCmd = 2, /* Rx/Command Unit command and status. */
  117. SCBIntmask = 3,
  118. SCBPointer = 4, /* General purpose pointer. */
  119. SCBPort = 8, /* Misc. commands and operands. */
  120. SCBflash = 12, SCBeeprom = 14, /* EEPROM and flash memory control. */
  121. SCBCtrlMDI = 16, /* MDI interface control. */
  122. SCBEarlyRx = 20, /* Early receive byte count. */
  123. SCBFlow = 24,
  124. };
  125. /* A speedo3 transmit buffer descriptor with two buffers... */
  126. typedef struct {
  127. uint16_t status;
  128. uint16_t command;
  129. uint32_t link; /* void * */
  130. uint32_t tx_desc_addr; /* transmit buffer decsriptor array address. */
  131. uint16_t tcb_bytes; /* transmit command block byte count (in lower 14 bits */
  132. uint8_t tx_threshold; /* transmit threshold */
  133. uint8_t tbd_count; /* TBD number */
  134. //~ /* This constitutes two "TBD" entries: hdr and data */
  135. //~ uint32_t tx_buf_addr0; /* void *, header of frame to be transmitted. */
  136. //~ int32_t tx_buf_size0; /* Length of Tx hdr. */
  137. //~ uint32_t tx_buf_addr1; /* void *, data to be transmitted. */
  138. //~ int32_t tx_buf_size1; /* Length of Tx data. */
  139. } eepro100_tx_t;
  140. /* Receive frame descriptor. */
  141. typedef struct {
  142. int16_t status;
  143. uint16_t command;
  144. uint32_t link; /* struct RxFD * */
  145. uint32_t rx_buf_addr; /* void * */
  146. uint16_t count;
  147. uint16_t size;
  148. char packet[MAX_ETH_FRAME_SIZE + 4];
  149. } eepro100_rx_t;
  150. typedef struct {
  151. uint32_t tx_good_frames, tx_max_collisions, tx_late_collisions,
  152. tx_underruns, tx_lost_crs, tx_deferred, tx_single_collisions,
  153. tx_multiple_collisions, tx_total_collisions;
  154. uint32_t rx_good_frames, rx_crc_errors, rx_alignment_errors,
  155. rx_resource_errors, rx_overrun_errors, rx_cdt_errors,
  156. rx_short_frame_errors;
  157. uint32_t fc_xmt_pause, fc_rcv_pause, fc_rcv_unsupported;
  158. uint16_t xmt_tco_frames, rcv_tco_frames;
  159. uint32_t complete;
  160. } eepro100_stats_t;
  161. typedef enum {
  162. cu_idle = 0,
  163. cu_suspended = 1,
  164. cu_active = 2,
  165. cu_lpq_active = 2,
  166. cu_hqp_active = 3
  167. } cu_state_t;
  168. typedef enum {
  169. ru_idle = 0,
  170. ru_suspended = 1,
  171. ru_no_resources = 2,
  172. ru_ready = 4
  173. } ru_state_t;
  174. #if defined(__BIG_ENDIAN_BITFIELD)
  175. #define X(a,b) b,a
  176. #else
  177. #define X(a,b) a,b
  178. #endif
  179. typedef struct {
  180. #if 1
  181. uint8_t cmd;
  182. uint32_t start;
  183. uint32_t stop;
  184. uint8_t boundary;
  185. uint8_t tsr;
  186. uint8_t tpsr;
  187. uint16_t tcnt;
  188. uint16_t rcnt;
  189. uint32_t rsar;
  190. uint8_t rsr;
  191. uint8_t rxcr;
  192. uint8_t isr;
  193. uint8_t dcfg;
  194. uint8_t imr;
  195. uint8_t phys[6]; /* mac address */
  196. uint8_t curpag;
  197. uint8_t mult[8]; /* multicast mask array */
  198. int mmio_index;
  199. PCIDevice *pci_dev;
  200. VLANClientState *vc;
  201. #endif
  202. uint8_t scb_stat; /* SCB stat/ack byte */
  203. uint8_t int_stat; /* PCI interrupt status */
  204. uint32_t region[3]; /* PCI region addresses */
  205. uint8_t macaddr[6];
  206. uint32_t statcounter[19];
  207. uint16_t mdimem[32];
  208. eeprom_t *eeprom;
  209. uint32_t device; /* device variant */
  210. uint32_t pointer;
  211. /* (cu_base + cu_offset) address the next command block in the command block list. */
  212. uint32_t cu_base; /* CU base address */
  213. uint32_t cu_offset; /* CU address offset */
  214. /* (ru_base + ru_offset) address the RFD in the Receive Frame Area. */
  215. uint32_t ru_base; /* RU base address */
  216. uint32_t ru_offset; /* RU address offset */
  217. uint32_t statsaddr; /* pointer to eepro100_stats_t */
  218. eepro100_stats_t statistics; /* statistical counters */
  219. #if 0
  220. uint16_t status;
  221. #endif
  222. /* Configuration bytes. */
  223. uint8_t configuration[22];
  224. /* Data in mem is always in the byte order of the controller (le). */
  225. uint8_t mem[PCI_MEM_SIZE];
  226. } EEPRO100State;
  227. /* Default values for MDI (PHY) registers */
  228. static const uint16_t eepro100_mdi_default[] = {
  229. /* MDI Registers 0 - 6, 7 */
  230. 0x3000, 0x780d, 0x02a8, 0x0154, 0x05e1, 0x0000, 0x0000, 0x0000,
  231. /* MDI Registers 8 - 15 */
  232. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  233. /* MDI Registers 16 - 31 */
  234. 0x0003, 0x0000, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  235. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  236. };
  237. /* Readonly mask for MDI (PHY) registers */
  238. static const uint16_t eepro100_mdi_mask[] = {
  239. 0x0000, 0xffff, 0xffff, 0xffff, 0xc01f, 0xffff, 0xffff, 0x0000,
  240. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  241. 0x0fff, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  242. 0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  243. };
  244. #define POLYNOMIAL 0x04c11db6
  245. /* From FreeBSD */
  246. /* XXX: optimize */
  247. static int compute_mcast_idx(const uint8_t * ep)
  248. {
  249. uint32_t crc;
  250. int carry, i, j;
  251. uint8_t b;
  252. crc = 0xffffffff;
  253. for (i = 0; i < 6; i++) {
  254. b = *ep++;
  255. for (j = 0; j < 8; j++) {
  256. carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
  257. crc <<= 1;
  258. b >>= 1;
  259. if (carry)
  260. crc = ((crc ^ POLYNOMIAL) | carry);
  261. }
  262. }
  263. return (crc >> 26);
  264. }
  265. #if defined(DEBUG_EEPRO100)
  266. static const char *nic_dump(const uint8_t * buf, unsigned size)
  267. {
  268. static char dump[3 * 16 + 1];
  269. char *p = &dump[0];
  270. if (size > 16)
  271. size = 16;
  272. while (size-- > 0) {
  273. p += sprintf(p, " %02x", *buf++);
  274. }
  275. return dump;
  276. }
  277. #endif /* DEBUG_EEPRO100 */
  278. enum scb_stat_ack {
  279. stat_ack_not_ours = 0x00,
  280. stat_ack_sw_gen = 0x04,
  281. stat_ack_rnr = 0x10,
  282. stat_ack_cu_idle = 0x20,
  283. stat_ack_frame_rx = 0x40,
  284. stat_ack_cu_cmd_done = 0x80,
  285. stat_ack_not_present = 0xFF,
  286. stat_ack_rx = (stat_ack_sw_gen | stat_ack_rnr | stat_ack_frame_rx),
  287. stat_ack_tx = (stat_ack_cu_idle | stat_ack_cu_cmd_done),
  288. };
  289. static void disable_interrupt(EEPRO100State * s)
  290. {
  291. if (s->int_stat) {
  292. logout("interrupt disabled\n");
  293. qemu_irq_lower(s->pci_dev->irq[0]);
  294. s->int_stat = 0;
  295. }
  296. }
  297. static void enable_interrupt(EEPRO100State * s)
  298. {
  299. if (!s->int_stat) {
  300. logout("interrupt enabled\n");
  301. qemu_irq_raise(s->pci_dev->irq[0]);
  302. s->int_stat = 1;
  303. }
  304. }
  305. static void eepro100_acknowledge(EEPRO100State * s)
  306. {
  307. s->scb_stat &= ~s->mem[SCBAck];
  308. s->mem[SCBAck] = s->scb_stat;
  309. if (s->scb_stat == 0) {
  310. disable_interrupt(s);
  311. }
  312. }
  313. static void eepro100_interrupt(EEPRO100State * s, uint8_t stat)
  314. {
  315. uint8_t mask = ~s->mem[SCBIntmask];
  316. s->mem[SCBAck] |= stat;
  317. stat = s->scb_stat = s->mem[SCBAck];
  318. stat &= (mask | 0x0f);
  319. //~ stat &= (~s->mem[SCBIntmask] | 0x0xf);
  320. if (stat && (mask & 0x01)) {
  321. /* SCB mask and SCB Bit M do not disable interrupt. */
  322. enable_interrupt(s);
  323. } else if (s->int_stat) {
  324. disable_interrupt(s);
  325. }
  326. }
  327. static void eepro100_cx_interrupt(EEPRO100State * s)
  328. {
  329. /* CU completed action command. */
  330. /* Transmit not ok (82557 only, not in emulation). */
  331. eepro100_interrupt(s, 0x80);
  332. }
  333. static void eepro100_cna_interrupt(EEPRO100State * s)
  334. {
  335. /* CU left the active state. */
  336. eepro100_interrupt(s, 0x20);
  337. }
  338. static void eepro100_fr_interrupt(EEPRO100State * s)
  339. {
  340. /* RU received a complete frame. */
  341. eepro100_interrupt(s, 0x40);
  342. }
  343. #if 0
  344. static void eepro100_rnr_interrupt(EEPRO100State * s)
  345. {
  346. /* RU is not ready. */
  347. eepro100_interrupt(s, 0x10);
  348. }
  349. #endif
  350. static void eepro100_mdi_interrupt(EEPRO100State * s)
  351. {
  352. /* MDI completed read or write cycle. */
  353. eepro100_interrupt(s, 0x08);
  354. }
  355. static void eepro100_swi_interrupt(EEPRO100State * s)
  356. {
  357. /* Software has requested an interrupt. */
  358. eepro100_interrupt(s, 0x04);
  359. }
  360. #if 0
  361. static void eepro100_fcp_interrupt(EEPRO100State * s)
  362. {
  363. /* Flow control pause interrupt (82558 and later). */
  364. eepro100_interrupt(s, 0x01);
  365. }
  366. #endif
  367. static void pci_reset(EEPRO100State * s)
  368. {
  369. uint32_t device = s->device;
  370. uint8_t *pci_conf = s->pci_dev->config;
  371. logout("%p\n", s);
  372. /* PCI Vendor ID */
  373. pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
  374. /* PCI Device ID */
  375. pci_config_set_device_id(pci_conf, 0x1209);
  376. /* PCI Command */
  377. PCI_CONFIG_16(PCI_COMMAND, 0x0000);
  378. /* PCI Status */
  379. PCI_CONFIG_16(PCI_STATUS, 0x2800);
  380. /* PCI Revision ID */
  381. PCI_CONFIG_8(PCI_REVISION_ID, 0x08);
  382. /* PCI Class Code */
  383. PCI_CONFIG_8(0x09, 0x00);
  384. pci_config_set_class(pci_conf, PCI_CLASS_NETWORK_ETHERNET);
  385. /* PCI Cache Line Size */
  386. /* check cache line size!!! */
  387. //~ PCI_CONFIG_8(0x0c, 0x00);
  388. /* PCI Latency Timer */
  389. PCI_CONFIG_8(0x0d, 0x20); // latency timer = 32 clocks
  390. /* PCI Header Type */
  391. /* BIST (built-in self test) */
  392. #if defined(TARGET_I386)
  393. // !!! workaround for buggy bios
  394. //~ #define PCI_ADDRESS_SPACE_MEM_PREFETCH 0
  395. #endif
  396. #if 0
  397. /* PCI Base Address Registers */
  398. /* CSR Memory Mapped Base Address */
  399. PCI_CONFIG_32(PCI_BASE_ADDRESS_0,
  400. PCI_ADDRESS_SPACE_MEM | PCI_ADDRESS_SPACE_MEM_PREFETCH);
  401. /* CSR I/O Mapped Base Address */
  402. PCI_CONFIG_32(PCI_BASE_ADDRESS_1, PCI_ADDRESS_SPACE_IO);
  403. #if 0
  404. /* Flash Memory Mapped Base Address */
  405. PCI_CONFIG_32(PCI_BASE_ADDRESS_2, 0xfffe0000 | PCI_ADDRESS_SPACE_MEM);
  406. #endif
  407. #endif
  408. /* Expansion ROM Base Address (depends on boot disable!!!) */
  409. PCI_CONFIG_32(0x30, 0x00000000);
  410. /* Capability Pointer */
  411. PCI_CONFIG_8(0x34, 0xdc);
  412. /* Interrupt Pin */
  413. PCI_CONFIG_8(0x3d, 1); // interrupt pin 0
  414. /* Minimum Grant */
  415. PCI_CONFIG_8(0x3e, 0x08);
  416. /* Maximum Latency */
  417. PCI_CONFIG_8(0x3f, 0x18);
  418. /* Power Management Capabilities / Next Item Pointer / Capability ID */
  419. PCI_CONFIG_32(0xdc, 0x7e210001);
  420. switch (device) {
  421. case i82551:
  422. //~ PCI_CONFIG_16(PCI_DEVICE_ID, 0x1209);
  423. PCI_CONFIG_8(PCI_REVISION_ID, 0x0f);
  424. break;
  425. case i82557B:
  426. PCI_CONFIG_16(PCI_DEVICE_ID, 0x1229);
  427. PCI_CONFIG_8(PCI_REVISION_ID, 0x02);
  428. break;
  429. case i82557C:
  430. PCI_CONFIG_16(PCI_DEVICE_ID, 0x1229);
  431. PCI_CONFIG_8(PCI_REVISION_ID, 0x03);
  432. break;
  433. case i82558B:
  434. PCI_CONFIG_16(PCI_DEVICE_ID, 0x1229);
  435. PCI_CONFIG_16(PCI_STATUS, 0x2810);
  436. PCI_CONFIG_8(PCI_REVISION_ID, 0x05);
  437. break;
  438. case i82559C:
  439. PCI_CONFIG_16(PCI_DEVICE_ID, 0x1229);
  440. PCI_CONFIG_16(PCI_STATUS, 0x2810);
  441. //~ PCI_CONFIG_8(PCI_REVISION_ID, 0x08);
  442. break;
  443. case i82559ER:
  444. //~ PCI_CONFIG_16(PCI_DEVICE_ID, 0x1209);
  445. PCI_CONFIG_16(PCI_STATUS, 0x2810);
  446. PCI_CONFIG_8(PCI_REVISION_ID, 0x09);
  447. break;
  448. //~ PCI_CONFIG_16(PCI_DEVICE_ID, 0x1029);
  449. //~ PCI_CONFIG_16(PCI_DEVICE_ID, 0x1030); /* 82559 InBusiness 10/100 */
  450. default:
  451. logout("Device %X is undefined!\n", device);
  452. }
  453. if (device == i82557C || device == i82558B || device == i82559C) {
  454. logout("Get device id and revision from EEPROM!!!\n");
  455. }
  456. }
  457. static void nic_selective_reset(EEPRO100State * s)
  458. {
  459. size_t i;
  460. uint16_t *eeprom_contents = eeprom93xx_data(s->eeprom);
  461. //~ eeprom93xx_reset(s->eeprom);
  462. memcpy(eeprom_contents, s->macaddr, 6);
  463. eeprom_contents[0xa] = 0x4000;
  464. uint16_t sum = 0;
  465. for (i = 0; i < EEPROM_SIZE - 1; i++) {
  466. sum += eeprom_contents[i];
  467. }
  468. eeprom_contents[EEPROM_SIZE - 1] = 0xbaba - sum;
  469. memset(s->mem, 0, sizeof(s->mem));
  470. uint32_t val = BIT(21);
  471. memcpy(&s->mem[SCBCtrlMDI], &val, sizeof(val));
  472. assert(sizeof(s->mdimem) == sizeof(eepro100_mdi_default));
  473. memcpy(&s->mdimem[0], &eepro100_mdi_default[0], sizeof(s->mdimem));
  474. }
  475. static void nic_reset(void *opaque)
  476. {
  477. EEPRO100State *s = (EEPRO100State *) opaque;
  478. logout("%p\n", s);
  479. static int first;
  480. if (!first) {
  481. first = 1;
  482. }
  483. nic_selective_reset(s);
  484. }
  485. #if defined(DEBUG_EEPRO100)
  486. static const char *reg[PCI_IO_SIZE / 4] = {
  487. "Command/Status",
  488. "General Pointer",
  489. "Port",
  490. "EEPROM/Flash Control",
  491. "MDI Control",
  492. "Receive DMA Byte Count",
  493. "Flow control register",
  494. "General Status/Control"
  495. };
  496. static char *regname(uint32_t addr)
  497. {
  498. static char buf[16];
  499. if (addr < PCI_IO_SIZE) {
  500. const char *r = reg[addr / 4];
  501. if (r != 0) {
  502. sprintf(buf, "%s+%u", r, addr % 4);
  503. } else {
  504. sprintf(buf, "0x%02x", addr);
  505. }
  506. } else {
  507. sprintf(buf, "??? 0x%08x", addr);
  508. }
  509. return buf;
  510. }
  511. #endif /* DEBUG_EEPRO100 */
  512. #if 0
  513. static uint16_t eepro100_read_status(EEPRO100State * s)
  514. {
  515. uint16_t val = s->status;
  516. logout("val=0x%04x\n", val);
  517. return val;
  518. }
  519. static void eepro100_write_status(EEPRO100State * s, uint16_t val)
  520. {
  521. logout("val=0x%04x\n", val);
  522. s->status = val;
  523. }
  524. #endif
  525. /*****************************************************************************
  526. *
  527. * Command emulation.
  528. *
  529. ****************************************************************************/
  530. #if 0
  531. static uint16_t eepro100_read_command(EEPRO100State * s)
  532. {
  533. uint16_t val = 0xffff;
  534. //~ logout("val=0x%04x\n", val);
  535. return val;
  536. }
  537. #endif
  538. /* Commands that can be put in a command list entry. */
  539. enum commands {
  540. CmdNOp = 0,
  541. CmdIASetup = 1,
  542. CmdConfigure = 2,
  543. CmdMulticastList = 3,
  544. CmdTx = 4,
  545. CmdTDR = 5, /* load microcode */
  546. CmdDump = 6,
  547. CmdDiagnose = 7,
  548. /* And some extra flags: */
  549. CmdSuspend = 0x4000, /* Suspend after completion. */
  550. CmdIntr = 0x2000, /* Interrupt after completion. */
  551. CmdTxFlex = 0x0008, /* Use "Flexible mode" for CmdTx command. */
  552. };
  553. static cu_state_t get_cu_state(EEPRO100State * s)
  554. {
  555. return ((s->mem[SCBStatus] >> 6) & 0x03);
  556. }
  557. static void set_cu_state(EEPRO100State * s, cu_state_t state)
  558. {
  559. s->mem[SCBStatus] = (s->mem[SCBStatus] & 0x3f) + (state << 6);
  560. }
  561. static ru_state_t get_ru_state(EEPRO100State * s)
  562. {
  563. return ((s->mem[SCBStatus] >> 2) & 0x0f);
  564. }
  565. static void set_ru_state(EEPRO100State * s, ru_state_t state)
  566. {
  567. s->mem[SCBStatus] = (s->mem[SCBStatus] & 0xc3) + (state << 2);
  568. }
  569. static void dump_statistics(EEPRO100State * s)
  570. {
  571. /* Dump statistical data. Most data is never changed by the emulation
  572. * and always 0, so we first just copy the whole block and then those
  573. * values which really matter.
  574. * Number of data should check configuration!!!
  575. */
  576. cpu_physical_memory_write(s->statsaddr, (uint8_t *) & s->statistics, 64);
  577. stl_phys(s->statsaddr + 0, s->statistics.tx_good_frames);
  578. stl_phys(s->statsaddr + 36, s->statistics.rx_good_frames);
  579. stl_phys(s->statsaddr + 48, s->statistics.rx_resource_errors);
  580. stl_phys(s->statsaddr + 60, s->statistics.rx_short_frame_errors);
  581. //~ stw_phys(s->statsaddr + 76, s->statistics.xmt_tco_frames);
  582. //~ stw_phys(s->statsaddr + 78, s->statistics.rcv_tco_frames);
  583. //~ missing("CU dump statistical counters");
  584. }
  585. static void eepro100_cu_command(EEPRO100State * s, uint8_t val)
  586. {
  587. eepro100_tx_t tx;
  588. uint32_t cb_address;
  589. switch (val) {
  590. case CU_NOP:
  591. /* No operation. */
  592. break;
  593. case CU_START:
  594. if (get_cu_state(s) != cu_idle) {
  595. /* Intel documentation says that CU must be idle for the CU
  596. * start command. Intel driver for Linux also starts the CU
  597. * from suspended state. */
  598. logout("CU state is %u, should be %u\n", get_cu_state(s), cu_idle);
  599. //~ assert(!"wrong CU state");
  600. }
  601. set_cu_state(s, cu_active);
  602. s->cu_offset = s->pointer;
  603. next_command:
  604. cb_address = s->cu_base + s->cu_offset;
  605. cpu_physical_memory_read(cb_address, (uint8_t *) & tx, sizeof(tx));
  606. uint16_t status = le16_to_cpu(tx.status);
  607. uint16_t command = le16_to_cpu(tx.command);
  608. logout
  609. ("val=0x%02x (cu start), status=0x%04x, command=0x%04x, link=0x%08x\n",
  610. val, status, command, tx.link);
  611. bool bit_el = ((command & 0x8000) != 0);
  612. bool bit_s = ((command & 0x4000) != 0);
  613. bool bit_i = ((command & 0x2000) != 0);
  614. bool bit_nc = ((command & 0x0010) != 0);
  615. //~ bool bit_sf = ((command & 0x0008) != 0);
  616. uint16_t cmd = command & 0x0007;
  617. s->cu_offset = le32_to_cpu(tx.link);
  618. switch (cmd) {
  619. case CmdNOp:
  620. /* Do nothing. */
  621. break;
  622. case CmdIASetup:
  623. cpu_physical_memory_read(cb_address + 8, &s->macaddr[0], 6);
  624. logout("macaddr: %s\n", nic_dump(&s->macaddr[0], 6));
  625. break;
  626. case CmdConfigure:
  627. cpu_physical_memory_read(cb_address + 8, &s->configuration[0],
  628. sizeof(s->configuration));
  629. logout("configuration: %s\n", nic_dump(&s->configuration[0], 16));
  630. break;
  631. case CmdMulticastList:
  632. //~ missing("multicast list");
  633. break;
  634. case CmdTx:
  635. (void)0;
  636. uint32_t tbd_array = le32_to_cpu(tx.tx_desc_addr);
  637. uint16_t tcb_bytes = (le16_to_cpu(tx.tcb_bytes) & 0x3fff);
  638. logout
  639. ("transmit, TBD array address 0x%08x, TCB byte count 0x%04x, TBD count %u\n",
  640. tbd_array, tcb_bytes, tx.tbd_count);
  641. assert(!bit_nc);
  642. //~ assert(!bit_sf);
  643. assert(tcb_bytes <= 2600);
  644. /* Next assertion fails for local configuration. */
  645. //~ assert((tcb_bytes > 0) || (tbd_array != 0xffffffff));
  646. if (!((tcb_bytes > 0) || (tbd_array != 0xffffffff))) {
  647. logout
  648. ("illegal values of TBD array address and TCB byte count!\n");
  649. }
  650. uint8_t buf[MAX_ETH_FRAME_SIZE + 4];
  651. uint16_t size = 0;
  652. uint32_t tbd_address = cb_address + 0x10;
  653. assert(tcb_bytes <= sizeof(buf));
  654. while (size < tcb_bytes) {
  655. uint32_t tx_buffer_address = ldl_phys(tbd_address);
  656. uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
  657. //~ uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
  658. tbd_address += 8;
  659. logout
  660. ("TBD (simplified mode): buffer address 0x%08x, size 0x%04x\n",
  661. tx_buffer_address, tx_buffer_size);
  662. cpu_physical_memory_read(tx_buffer_address, &buf[size],
  663. tx_buffer_size);
  664. size += tx_buffer_size;
  665. }
  666. if (tbd_array == 0xffffffff) {
  667. /* Simplified mode. Was already handled by code above. */
  668. } else {
  669. /* Flexible mode. */
  670. uint8_t tbd_count = 0;
  671. if (!(s->configuration[6] & BIT(4))) {
  672. /* Extended TCB. */
  673. assert(tcb_bytes == 0);
  674. for (; tbd_count < 2; tbd_count++) {
  675. uint32_t tx_buffer_address = ldl_phys(tbd_address);
  676. uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
  677. uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
  678. tbd_address += 8;
  679. logout
  680. ("TBD (extended mode): buffer address 0x%08x, size 0x%04x\n",
  681. tx_buffer_address, tx_buffer_size);
  682. cpu_physical_memory_read(tx_buffer_address, &buf[size],
  683. tx_buffer_size);
  684. size += tx_buffer_size;
  685. if (tx_buffer_el & 1) {
  686. break;
  687. }
  688. }
  689. }
  690. tbd_address = tbd_array;
  691. for (; tbd_count < tx.tbd_count; tbd_count++) {
  692. uint32_t tx_buffer_address = ldl_phys(tbd_address);
  693. uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
  694. uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
  695. tbd_address += 8;
  696. logout
  697. ("TBD (flexible mode): buffer address 0x%08x, size 0x%04x\n",
  698. tx_buffer_address, tx_buffer_size);
  699. cpu_physical_memory_read(tx_buffer_address, &buf[size],
  700. tx_buffer_size);
  701. size += tx_buffer_size;
  702. if (tx_buffer_el & 1) {
  703. break;
  704. }
  705. }
  706. }
  707. qemu_send_packet(s->vc, buf, size);
  708. s->statistics.tx_good_frames++;
  709. /* Transmit with bad status would raise an CX/TNO interrupt.
  710. * (82557 only). Emulation never has bad status. */
  711. //~ eepro100_cx_interrupt(s);
  712. break;
  713. case CmdTDR:
  714. logout("load microcode\n");
  715. /* Starting with offset 8, the command contains
  716. * 64 dwords microcode which we just ignore here. */
  717. break;
  718. default:
  719. missing("undefined command");
  720. }
  721. /* Write new status (success). */
  722. stw_phys(cb_address, status | 0x8000 | 0x2000);
  723. if (bit_i) {
  724. /* CU completed action. */
  725. eepro100_cx_interrupt(s);
  726. }
  727. if (bit_el) {
  728. /* CU becomes idle. */
  729. set_cu_state(s, cu_idle);
  730. eepro100_cna_interrupt(s);
  731. } else if (bit_s) {
  732. /* CU becomes suspended. */
  733. set_cu_state(s, cu_suspended);
  734. eepro100_cna_interrupt(s);
  735. } else {
  736. /* More entries in list. */
  737. logout("CU list with at least one more entry\n");
  738. goto next_command;
  739. }
  740. logout("CU list empty\n");
  741. /* List is empty. Now CU is idle or suspended. */
  742. break;
  743. case CU_RESUME:
  744. if (get_cu_state(s) != cu_suspended) {
  745. logout("bad CU resume from CU state %u\n", get_cu_state(s));
  746. /* Workaround for bad Linux eepro100 driver which resumes
  747. * from idle state. */
  748. //~ missing("cu resume");
  749. set_cu_state(s, cu_suspended);
  750. }
  751. if (get_cu_state(s) == cu_suspended) {
  752. logout("CU resuming\n");
  753. set_cu_state(s, cu_active);
  754. goto next_command;
  755. }
  756. break;
  757. case CU_STATSADDR:
  758. /* Load dump counters address. */
  759. s->statsaddr = s->pointer;
  760. logout("val=0x%02x (status address)\n", val);
  761. break;
  762. case CU_SHOWSTATS:
  763. /* Dump statistical counters. */
  764. dump_statistics(s);
  765. break;
  766. case CU_CMD_BASE:
  767. /* Load CU base. */
  768. logout("val=0x%02x (CU base address)\n", val);
  769. s->cu_base = s->pointer;
  770. break;
  771. case CU_DUMPSTATS:
  772. /* Dump and reset statistical counters. */
  773. dump_statistics(s);
  774. memset(&s->statistics, 0, sizeof(s->statistics));
  775. break;
  776. case CU_SRESUME:
  777. /* CU static resume. */
  778. missing("CU static resume");
  779. break;
  780. default:
  781. missing("Undefined CU command");
  782. }
  783. }
  784. static void eepro100_ru_command(EEPRO100State * s, uint8_t val)
  785. {
  786. switch (val) {
  787. case RU_NOP:
  788. /* No operation. */
  789. break;
  790. case RX_START:
  791. /* RU start. */
  792. if (get_ru_state(s) != ru_idle) {
  793. logout("RU state is %u, should be %u\n", get_ru_state(s), ru_idle);
  794. //~ assert(!"wrong RU state");
  795. }
  796. set_ru_state(s, ru_ready);
  797. s->ru_offset = s->pointer;
  798. logout("val=0x%02x (rx start)\n", val);
  799. break;
  800. case RX_RESUME:
  801. /* Restart RU. */
  802. if (get_ru_state(s) != ru_suspended) {
  803. logout("RU state is %u, should be %u\n", get_ru_state(s),
  804. ru_suspended);
  805. //~ assert(!"wrong RU state");
  806. }
  807. set_ru_state(s, ru_ready);
  808. break;
  809. case RX_ADDR_LOAD:
  810. /* Load RU base. */
  811. logout("val=0x%02x (RU base address)\n", val);
  812. s->ru_base = s->pointer;
  813. break;
  814. default:
  815. logout("val=0x%02x (undefined RU command)\n", val);
  816. missing("Undefined SU command");
  817. }
  818. }
  819. static void eepro100_write_command(EEPRO100State * s, uint8_t val)
  820. {
  821. eepro100_ru_command(s, val & 0x0f);
  822. eepro100_cu_command(s, val & 0xf0);
  823. if ((val) == 0) {
  824. logout("val=0x%02x\n", val);
  825. }
  826. /* Clear command byte after command was accepted. */
  827. s->mem[SCBCmd] = 0;
  828. }
  829. /*****************************************************************************
  830. *
  831. * EEPROM emulation.
  832. *
  833. ****************************************************************************/
  834. #define EEPROM_CS 0x02
  835. #define EEPROM_SK 0x01
  836. #define EEPROM_DI 0x04
  837. #define EEPROM_DO 0x08
  838. static uint16_t eepro100_read_eeprom(EEPRO100State * s)
  839. {
  840. uint16_t val;
  841. memcpy(&val, &s->mem[SCBeeprom], sizeof(val));
  842. if (eeprom93xx_read(s->eeprom)) {
  843. val |= EEPROM_DO;
  844. } else {
  845. val &= ~EEPROM_DO;
  846. }
  847. return val;
  848. }
  849. static void eepro100_write_eeprom(eeprom_t * eeprom, uint8_t val)
  850. {
  851. logout("write val=0x%02x\n", val);
  852. /* mask unwriteable bits */
  853. //~ val = SET_MASKED(val, 0x31, eeprom->value);
  854. int eecs = ((val & EEPROM_CS) != 0);
  855. int eesk = ((val & EEPROM_SK) != 0);
  856. int eedi = ((val & EEPROM_DI) != 0);
  857. eeprom93xx_write(eeprom, eecs, eesk, eedi);
  858. }
  859. static void eepro100_write_pointer(EEPRO100State * s, uint32_t val)
  860. {
  861. s->pointer = le32_to_cpu(val);
  862. logout("val=0x%08x\n", val);
  863. }
  864. /*****************************************************************************
  865. *
  866. * MDI emulation.
  867. *
  868. ****************************************************************************/
  869. #if defined(DEBUG_EEPRO100)
  870. static const char *mdi_op_name[] = {
  871. "opcode 0",
  872. "write",
  873. "read",
  874. "opcode 3"
  875. };
  876. static const char *mdi_reg_name[] = {
  877. "Control",
  878. "Status",
  879. "PHY Identification (Word 1)",
  880. "PHY Identification (Word 2)",
  881. "Auto-Negotiation Advertisement",
  882. "Auto-Negotiation Link Partner Ability",
  883. "Auto-Negotiation Expansion"
  884. };
  885. #endif /* DEBUG_EEPRO100 */
  886. static uint32_t eepro100_read_mdi(EEPRO100State * s)
  887. {
  888. uint32_t val;
  889. memcpy(&val, &s->mem[0x10], sizeof(val));
  890. #ifdef DEBUG_EEPRO100
  891. uint8_t raiseint = (val & BIT(29)) >> 29;
  892. uint8_t opcode = (val & BITS(27, 26)) >> 26;
  893. uint8_t phy = (val & BITS(25, 21)) >> 21;
  894. uint8_t reg = (val & BITS(20, 16)) >> 16;
  895. uint16_t data = (val & BITS(15, 0));
  896. #endif
  897. /* Emulation takes no time to finish MDI transaction. */
  898. val |= BIT(28);
  899. TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
  900. val, raiseint, mdi_op_name[opcode], phy,
  901. mdi_reg_name[reg], data));
  902. return val;
  903. }
  904. //~ #define BITS(val, upper, lower) (val & ???)
  905. static void eepro100_write_mdi(EEPRO100State * s, uint32_t val)
  906. {
  907. uint8_t raiseint = (val & BIT(29)) >> 29;
  908. uint8_t opcode = (val & BITS(27, 26)) >> 26;
  909. uint8_t phy = (val & BITS(25, 21)) >> 21;
  910. uint8_t reg = (val & BITS(20, 16)) >> 16;
  911. uint16_t data = (val & BITS(15, 0));
  912. if (phy != 1) {
  913. /* Unsupported PHY address. */
  914. //~ logout("phy must be 1 but is %u\n", phy);
  915. data = 0;
  916. } else if (opcode != 1 && opcode != 2) {
  917. /* Unsupported opcode. */
  918. logout("opcode must be 1 or 2 but is %u\n", opcode);
  919. data = 0;
  920. } else if (reg > 6) {
  921. /* Unsupported register. */
  922. logout("register must be 0...6 but is %u\n", reg);
  923. data = 0;
  924. } else {
  925. TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
  926. val, raiseint, mdi_op_name[opcode], phy,
  927. mdi_reg_name[reg], data));
  928. if (opcode == 1) {
  929. /* MDI write */
  930. switch (reg) {
  931. case 0: /* Control Register */
  932. if (data & 0x8000) {
  933. /* Reset status and control registers to default. */
  934. s->mdimem[0] = eepro100_mdi_default[0];
  935. s->mdimem[1] = eepro100_mdi_default[1];
  936. data = s->mdimem[reg];
  937. } else {
  938. /* Restart Auto Configuration = Normal Operation */
  939. data &= ~0x0200;
  940. }
  941. break;
  942. case 1: /* Status Register */
  943. missing("not writable");
  944. data = s->mdimem[reg];
  945. break;
  946. case 2: /* PHY Identification Register (Word 1) */
  947. case 3: /* PHY Identification Register (Word 2) */
  948. missing("not implemented");
  949. break;
  950. case 4: /* Auto-Negotiation Advertisement Register */
  951. case 5: /* Auto-Negotiation Link Partner Ability Register */
  952. break;
  953. case 6: /* Auto-Negotiation Expansion Register */
  954. default:
  955. missing("not implemented");
  956. }
  957. s->mdimem[reg] = data;
  958. } else if (opcode == 2) {
  959. /* MDI read */
  960. switch (reg) {
  961. case 0: /* Control Register */
  962. if (data & 0x8000) {
  963. /* Reset status and control registers to default. */
  964. s->mdimem[0] = eepro100_mdi_default[0];
  965. s->mdimem[1] = eepro100_mdi_default[1];
  966. }
  967. break;
  968. case 1: /* Status Register */
  969. s->mdimem[reg] |= 0x0020;
  970. break;
  971. case 2: /* PHY Identification Register (Word 1) */
  972. case 3: /* PHY Identification Register (Word 2) */
  973. case 4: /* Auto-Negotiation Advertisement Register */
  974. break;
  975. case 5: /* Auto-Negotiation Link Partner Ability Register */
  976. s->mdimem[reg] = 0x41fe;
  977. break;
  978. case 6: /* Auto-Negotiation Expansion Register */
  979. s->mdimem[reg] = 0x0001;
  980. break;
  981. }
  982. data = s->mdimem[reg];
  983. }
  984. /* Emulation takes no time to finish MDI transaction.
  985. * Set MDI bit in SCB status register. */
  986. s->mem[SCBAck] |= 0x08;
  987. val |= BIT(28);
  988. if (raiseint) {
  989. eepro100_mdi_interrupt(s);
  990. }
  991. }
  992. val = (val & 0xffff0000) + data;
  993. memcpy(&s->mem[0x10], &val, sizeof(val));
  994. }
  995. /*****************************************************************************
  996. *
  997. * Port emulation.
  998. *
  999. ****************************************************************************/
  1000. #define PORT_SOFTWARE_RESET 0
  1001. #define PORT_SELFTEST 1
  1002. #define PORT_SELECTIVE_RESET 2
  1003. #define PORT_DUMP 3
  1004. #define PORT_SELECTION_MASK 3
  1005. typedef struct {
  1006. uint32_t st_sign; /* Self Test Signature */
  1007. uint32_t st_result; /* Self Test Results */
  1008. } eepro100_selftest_t;
  1009. static uint32_t eepro100_read_port(EEPRO100State * s)
  1010. {
  1011. return 0;
  1012. }
  1013. static void eepro100_write_port(EEPRO100State * s, uint32_t val)
  1014. {
  1015. val = le32_to_cpu(val);
  1016. uint32_t address = (val & ~PORT_SELECTION_MASK);
  1017. uint8_t selection = (val & PORT_SELECTION_MASK);
  1018. switch (selection) {
  1019. case PORT_SOFTWARE_RESET:
  1020. nic_reset(s);
  1021. break;
  1022. case PORT_SELFTEST:
  1023. logout("selftest address=0x%08x\n", address);
  1024. eepro100_selftest_t data;
  1025. cpu_physical_memory_read(address, (uint8_t *) & data, sizeof(data));
  1026. data.st_sign = 0xffffffff;
  1027. data.st_result = 0;
  1028. cpu_physical_memory_write(address, (uint8_t *) & data, sizeof(data));
  1029. break;
  1030. case PORT_SELECTIVE_RESET:
  1031. logout("selective reset, selftest address=0x%08x\n", address);
  1032. nic_selective_reset(s);
  1033. break;
  1034. default:
  1035. logout("val=0x%08x\n", val);
  1036. missing("unknown port selection");
  1037. }
  1038. }
  1039. /*****************************************************************************
  1040. *
  1041. * General hardware emulation.
  1042. *
  1043. ****************************************************************************/
  1044. static uint8_t eepro100_read1(EEPRO100State * s, uint32_t addr)
  1045. {
  1046. uint8_t val;
  1047. if (addr <= sizeof(s->mem) - sizeof(val)) {
  1048. memcpy(&val, &s->mem[addr], sizeof(val));
  1049. }
  1050. switch (addr) {
  1051. case SCBStatus:
  1052. //~ val = eepro100_read_status(s);
  1053. logout("addr=%s val=0x%02x\n", regname(addr), val);
  1054. break;
  1055. case SCBAck:
  1056. //~ val = eepro100_read_status(s);
  1057. logout("addr=%s val=0x%02x\n", regname(addr), val);
  1058. break;
  1059. case SCBCmd:
  1060. logout("addr=%s val=0x%02x\n", regname(addr), val);
  1061. //~ val = eepro100_read_command(s);
  1062. break;
  1063. case SCBIntmask:
  1064. logout("addr=%s val=0x%02x\n", regname(addr), val);
  1065. break;
  1066. case SCBPort + 3:
  1067. logout("addr=%s val=0x%02x\n", regname(addr), val);
  1068. break;
  1069. case SCBeeprom:
  1070. val = eepro100_read_eeprom(s);
  1071. break;
  1072. case 0x1b: /* PMDR (power management driver register) */
  1073. val = 0;
  1074. logout("addr=%s val=0x%02x\n", regname(addr), val);
  1075. break;
  1076. case 0x1d: /* general status register */
  1077. /* 100 Mbps full duplex, valid link */
  1078. val = 0x07;
  1079. logout("addr=General Status val=%02x\n", val);
  1080. break;
  1081. default:
  1082. logout("addr=%s val=0x%02x\n", regname(addr), val);
  1083. missing("unknown byte read");
  1084. }
  1085. return val;
  1086. }
  1087. static uint16_t eepro100_read2(EEPRO100State * s, uint32_t addr)
  1088. {
  1089. uint16_t val;
  1090. if (addr <= sizeof(s->mem) - sizeof(val)) {
  1091. memcpy(&val, &s->mem[addr], sizeof(val));
  1092. }
  1093. logout("addr=%s val=0x%04x\n", regname(addr), val);
  1094. switch (addr) {
  1095. case SCBStatus:
  1096. //~ val = eepro100_read_status(s);
  1097. break;
  1098. case SCBeeprom:
  1099. val = eepro100_read_eeprom(s);
  1100. break;
  1101. default:
  1102. logout("addr=%s val=0x%04x\n", regname(addr), val);
  1103. missing("unknown word read");
  1104. }
  1105. return val;
  1106. }
  1107. static uint32_t eepro100_read4(EEPRO100State * s, uint32_t addr)
  1108. {
  1109. uint32_t val;
  1110. if (addr <= sizeof(s->mem) - sizeof(val)) {
  1111. memcpy(&val, &s->mem[addr], sizeof(val));
  1112. }
  1113. switch (addr) {
  1114. case SCBStatus:
  1115. //~ val = eepro100_read_status(s);
  1116. logout("addr=%s val=0x%08x\n", regname(addr), val);
  1117. break;
  1118. case SCBPointer:
  1119. //~ val = eepro100_read_pointer(s);
  1120. logout("addr=%s val=0x%08x\n", regname(addr), val);
  1121. break;
  1122. case SCBPort:
  1123. val = eepro100_read_port(s);
  1124. logout("addr=%s val=0x%08x\n", regname(addr), val);
  1125. break;
  1126. case SCBCtrlMDI:
  1127. val = eepro100_read_mdi(s);
  1128. break;
  1129. default:
  1130. logout("addr=%s val=0x%08x\n", regname(addr), val);
  1131. missing("unknown longword read");
  1132. }
  1133. return val;
  1134. }
  1135. static void eepro100_write1(EEPRO100State * s, uint32_t addr, uint8_t val)
  1136. {
  1137. if (addr <= sizeof(s->mem) - sizeof(val)) {
  1138. memcpy(&s->mem[addr], &val, sizeof(val));
  1139. }
  1140. logout("addr=%s val=0x%02x\n", regname(addr), val);
  1141. switch (addr) {
  1142. case SCBStatus:
  1143. //~ eepro100_write_status(s, val);
  1144. break;
  1145. case SCBAck:
  1146. eepro100_acknowledge(s);
  1147. break;
  1148. case SCBCmd:
  1149. eepro100_write_command(s, val);
  1150. break;
  1151. case SCBIntmask:
  1152. if (val & BIT(1)) {
  1153. eepro100_swi_interrupt(s);
  1154. }
  1155. eepro100_interrupt(s, 0);
  1156. break;
  1157. case SCBPort + 3:
  1158. case SCBFlow:
  1159. case SCBFlow + 1:
  1160. case SCBFlow + 2:
  1161. case SCBFlow + 3:
  1162. logout("addr=%s val=0x%02x\n", regname(addr), val);
  1163. break;
  1164. case SCBeeprom:
  1165. eepro100_write_eeprom(s->eeprom, val);
  1166. break;
  1167. default:
  1168. logout("addr=%s val=0x%02x\n", regname(addr), val);
  1169. missing("unknown byte write");
  1170. }
  1171. }
  1172. static void eepro100_write2(EEPRO100State * s, uint32_t addr, uint16_t val)
  1173. {
  1174. if (addr <= sizeof(s->mem) - sizeof(val)) {
  1175. memcpy(&s->mem[addr], &val, sizeof(val));
  1176. }
  1177. logout("addr=%s val=0x%04x\n", regname(addr), val);
  1178. switch (addr) {
  1179. case SCBStatus:
  1180. //~ eepro100_write_status(s, val);
  1181. eepro100_acknowledge(s);
  1182. break;
  1183. case SCBCmd:
  1184. eepro100_write_command(s, val);
  1185. eepro100_write1(s, SCBIntmask, val >> 8);
  1186. break;
  1187. case SCBeeprom:
  1188. eepro100_write_eeprom(s->eeprom, val);
  1189. break;
  1190. default:
  1191. logout("addr=%s val=0x%04x\n", regname(addr), val);
  1192. missing("unknown word write");
  1193. }
  1194. }
  1195. static void eepro100_write4(EEPRO100State * s, uint32_t addr, uint32_t val)
  1196. {
  1197. if (addr <= sizeof(s->mem) - sizeof(val)) {
  1198. memcpy(&s->mem[addr], &val, sizeof(val));
  1199. }
  1200. switch (addr) {
  1201. case SCBPointer:
  1202. eepro100_write_pointer(s, val);
  1203. break;
  1204. case SCBPort:
  1205. logout("addr=%s val=0x%08x\n", regname(addr), val);
  1206. eepro100_write_port(s, val);
  1207. break;
  1208. case SCBCtrlMDI:
  1209. eepro100_write_mdi(s, val);
  1210. break;
  1211. default:
  1212. logout("addr=%s val=0x%08x\n", regname(addr), val);
  1213. missing("unknown longword write");
  1214. }
  1215. }
  1216. static uint32_t ioport_read1(void *opaque, uint32_t addr)
  1217. {
  1218. EEPRO100State *s = opaque;
  1219. //~ logout("addr=%s\n", regname(addr));
  1220. return eepro100_read1(s, addr - s->region[1]);
  1221. }
  1222. static uint32_t ioport_read2(void *opaque, uint32_t addr)
  1223. {
  1224. EEPRO100State *s = opaque;
  1225. return eepro100_read2(s, addr - s->region[1]);
  1226. }
  1227. static uint32_t ioport_read4(void *opaque, uint32_t addr)
  1228. {
  1229. EEPRO100State *s = opaque;
  1230. return eepro100_read4(s, addr - s->region[1]);
  1231. }
  1232. static void ioport_write1(void *opaque, uint32_t addr, uint32_t val)
  1233. {
  1234. EEPRO100State *s = opaque;
  1235. //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
  1236. eepro100_write1(s, addr - s->region[1], val);
  1237. }
  1238. static void ioport_write2(void *opaque, uint32_t addr, uint32_t val)
  1239. {
  1240. EEPRO100State *s = opaque;
  1241. eepro100_write2(s, addr - s->region[1], val);
  1242. }
  1243. static void ioport_write4(void *opaque, uint32_t addr, uint32_t val)
  1244. {
  1245. EEPRO100State *s = opaque;
  1246. eepro100_write4(s, addr - s->region[1], val);
  1247. }
  1248. /***********************************************************/
  1249. /* PCI EEPRO100 definitions */
  1250. typedef struct PCIEEPRO100State {
  1251. PCIDevice dev;
  1252. EEPRO100State eepro100;
  1253. } PCIEEPRO100State;
  1254. static void pci_map(PCIDevice * pci_dev, int region_num,
  1255. uint32_t addr, uint32_t size, int type)
  1256. {
  1257. PCIEEPRO100State *d = (PCIEEPRO100State *) pci_dev;
  1258. EEPRO100State *s = &d->eepro100;
  1259. logout("region %d, addr=0x%08x, size=0x%08x, type=%d\n",
  1260. region_num, addr, size, type);
  1261. assert(region_num == 1);
  1262. register_ioport_write(addr, size, 1, ioport_write1, s);
  1263. register_ioport_read(addr, size, 1, ioport_read1, s);
  1264. register_ioport_write(addr, size, 2, ioport_write2, s);
  1265. register_ioport_read(addr, size, 2, ioport_read2, s);
  1266. register_ioport_write(addr, size, 4, ioport_write4, s);
  1267. register_ioport_read(addr, size, 4, ioport_read4, s);
  1268. s->region[region_num] = addr;
  1269. }
  1270. static void pci_mmio_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
  1271. {
  1272. EEPRO100State *s = opaque;
  1273. //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
  1274. eepro100_write1(s, addr, val);
  1275. }
  1276. static void pci_mmio_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
  1277. {
  1278. EEPRO100State *s = opaque;
  1279. //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
  1280. eepro100_write2(s, addr, val);
  1281. }
  1282. static void pci_mmio_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
  1283. {
  1284. EEPRO100State *s = opaque;
  1285. //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
  1286. eepro100_write4(s, addr, val);
  1287. }
  1288. static uint32_t pci_mmio_readb(void *opaque, target_phys_addr_t addr)
  1289. {
  1290. EEPRO100State *s = opaque;
  1291. //~ logout("addr=%s\n", regname(addr));
  1292. return eepro100_read1(s, addr);
  1293. }
  1294. static uint32_t pci_mmio_readw(void *opaque, target_phys_addr_t addr)
  1295. {
  1296. EEPRO100State *s = opaque;
  1297. //~ logout("addr=%s\n", regname(addr));
  1298. return eepro100_read2(s, addr);
  1299. }
  1300. static uint32_t pci_mmio_readl(void *opaque, target_phys_addr_t addr)
  1301. {
  1302. EEPRO100State *s = opaque;
  1303. //~ logout("addr=%s\n", regname(addr));
  1304. return eepro100_read4(s, addr);
  1305. }
  1306. static CPUWriteMemoryFunc *pci_mmio_write[] = {
  1307. pci_mmio_writeb,
  1308. pci_mmio_writew,
  1309. pci_mmio_writel
  1310. };
  1311. static CPUReadMemoryFunc *pci_mmio_read[] = {
  1312. pci_mmio_readb,
  1313. pci_mmio_readw,
  1314. pci_mmio_readl
  1315. };
  1316. static void pci_mmio_map(PCIDevice * pci_dev, int region_num,
  1317. uint32_t addr, uint32_t size, int type)
  1318. {
  1319. PCIEEPRO100State *d = (PCIEEPRO100State *) pci_dev;
  1320. logout("region %d, addr=0x%08x, size=0x%08x, type=%d\n",
  1321. region_num, addr, size, type);
  1322. if (region_num == 0) {
  1323. /* Map control / status registers. */
  1324. cpu_register_physical_memory(addr, size, d->eepro100.mmio_index);
  1325. d->eepro100.region[region_num] = addr;
  1326. }
  1327. }
  1328. static int nic_can_receive(void *opaque)
  1329. {
  1330. EEPRO100State *s = opaque;
  1331. logout("%p\n", s);
  1332. return get_ru_state(s) == ru_ready;
  1333. //~ return !eepro100_buffer_full(s);
  1334. }
  1335. #define MIN_BUF_SIZE 60
  1336. static void nic_receive(void *opaque, const uint8_t * buf, int size)
  1337. {
  1338. /* TODO:
  1339. * - Magic packets should set bit 30 in power management driver register.
  1340. * - Interesting packets should set bit 29 in power management driver register.
  1341. */
  1342. EEPRO100State *s = opaque;
  1343. uint16_t rfd_status = 0xa000;
  1344. static const uint8_t broadcast_macaddr[6] =
  1345. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  1346. /* TODO: check multiple IA bit. */
  1347. assert(!(s->configuration[20] & BIT(6)));
  1348. if (s->configuration[8] & 0x80) {
  1349. /* CSMA is disabled. */
  1350. logout("%p received while CSMA is disabled\n", s);
  1351. return;
  1352. } else if (size < 64 && (s->configuration[7] & 1)) {
  1353. /* Short frame and configuration byte 7/0 (discard short receive) set:
  1354. * Short frame is discarded */
  1355. logout("%p received short frame (%d byte)\n", s, size);
  1356. s->statistics.rx_short_frame_errors++;
  1357. //~ return;
  1358. } else if ((size > MAX_ETH_FRAME_SIZE + 4) && !(s->configuration[18] & 8)) {
  1359. /* Long frame and configuration byte 18/3 (long receive ok) not set:
  1360. * Long frames are discarded. */
  1361. logout("%p received long frame (%d byte), ignored\n", s, size);
  1362. return;
  1363. } else if (memcmp(buf, s->macaddr, 6) == 0) { // !!!
  1364. /* Frame matches individual address. */
  1365. /* TODO: check configuration byte 15/4 (ignore U/L). */
  1366. logout("%p received frame for me, len=%d\n", s, size);
  1367. } else if (memcmp(buf, broadcast_macaddr, 6) == 0) {
  1368. /* Broadcast frame. */
  1369. logout("%p received broadcast, len=%d\n", s, size);
  1370. rfd_status |= 0x0002;
  1371. } else if (buf[0] & 0x01) { // !!!
  1372. /* Multicast frame. */
  1373. logout("%p received multicast, len=%d\n", s, size);
  1374. /* TODO: check multicast all bit. */
  1375. assert(!(s->configuration[21] & BIT(3)));
  1376. int mcast_idx = compute_mcast_idx(buf);
  1377. if (!(s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7)))) {
  1378. return;
  1379. }
  1380. rfd_status |= 0x0002;
  1381. } else if (s->configuration[15] & 1) {
  1382. /* Promiscuous: receive all. */
  1383. logout("%p received frame in promiscuous mode, len=%d\n", s, size);
  1384. rfd_status |= 0x0004;
  1385. } else {
  1386. logout("%p received frame, ignored, len=%d,%s\n", s, size,
  1387. nic_dump(buf, size));
  1388. return;
  1389. }
  1390. if (get_ru_state(s) != ru_ready) {
  1391. /* No ressources available. */
  1392. logout("no ressources, state=%u\n", get_ru_state(s));
  1393. s->statistics.rx_resource_errors++;
  1394. //~ assert(!"no ressources");
  1395. return;
  1396. }
  1397. //~ !!!
  1398. //~ $3 = {status = 0x0, command = 0xc000, link = 0x2d220, rx_buf_addr = 0x207dc, count = 0x0, size = 0x5f8, packet = {0x0 <repeats 1518 times>}}
  1399. eepro100_rx_t rx;
  1400. cpu_physical_memory_read(s->ru_base + s->ru_offset, (uint8_t *) & rx,
  1401. offsetof(eepro100_rx_t, packet));
  1402. uint16_t rfd_command = le16_to_cpu(rx.command);
  1403. uint16_t rfd_size = le16_to_cpu(rx.size);
  1404. assert(size <= rfd_size);
  1405. if (size < 64) {
  1406. rfd_status |= 0x0080;
  1407. }
  1408. logout("command 0x%04x, link 0x%08x, addr 0x%08x, size %u\n", rfd_command,
  1409. rx.link, rx.rx_buf_addr, rfd_size);
  1410. stw_phys(s->ru_base + s->ru_offset + offsetof(eepro100_rx_t, status),
  1411. rfd_status);
  1412. stw_phys(s->ru_base + s->ru_offset + offsetof(eepro100_rx_t, count), size);
  1413. /* Early receive interrupt not supported. */
  1414. //~ eepro100_er_interrupt(s);
  1415. /* Receive CRC Transfer not supported. */
  1416. assert(!(s->configuration[18] & 4));
  1417. /* TODO: check stripping enable bit. */
  1418. //~ assert(!(s->configuration[17] & 1));
  1419. cpu_physical_memory_write(s->ru_base + s->ru_offset +
  1420. offsetof(eepro100_rx_t, packet), buf, size);
  1421. s->statistics.rx_good_frames++;
  1422. eepro100_fr_interrupt(s);
  1423. s->ru_offset = le32_to_cpu(rx.link);
  1424. if (rfd_command & 0x8000) {
  1425. /* EL bit is set, so this was the last frame. */
  1426. assert(0);
  1427. }
  1428. if (rfd_command & 0x4000) {
  1429. /* S bit is set. */
  1430. set_ru_state(s, ru_suspended);
  1431. }
  1432. }
  1433. static int nic_load(QEMUFile * f, void *opaque, int version_id)
  1434. {
  1435. EEPRO100State *s = (EEPRO100State *) opaque;
  1436. int i;
  1437. int ret;
  1438. if (version_id > 3)
  1439. return -EINVAL;
  1440. if (s->pci_dev && version_id >= 3) {
  1441. ret = pci_device_load(s->pci_dev, f);
  1442. if (ret < 0)
  1443. return ret;
  1444. }
  1445. if (version_id >= 2) {
  1446. qemu_get_8s(f, &s->rxcr);
  1447. } else {
  1448. s->rxcr = 0x0c;
  1449. }
  1450. qemu_get_8s(f, &s->cmd);
  1451. qemu_get_be32s(f, &s->start);
  1452. qemu_get_be32s(f, &s->stop);
  1453. qemu_get_8s(f, &s->boundary);
  1454. qemu_get_8s(f, &s->tsr);
  1455. qemu_get_8s(f, &s->tpsr);
  1456. qemu_get_be16s(f, &s->tcnt);
  1457. qemu_get_be16s(f, &s->rcnt);
  1458. qemu_get_be32s(f, &s->rsar);
  1459. qemu_get_8s(f, &s->rsr);
  1460. qemu_get_8s(f, &s->isr);
  1461. qemu_get_8s(f, &s->dcfg);
  1462. qemu_get_8s(f, &s->imr);
  1463. qemu_get_buffer(f, s->phys, 6);
  1464. qemu_get_8s(f, &s->curpag);
  1465. qemu_get_buffer(f, s->mult, 8);
  1466. qemu_get_buffer(f, s->mem, sizeof(s->mem));
  1467. /* Restore all members of struct between scv_stat and mem */
  1468. qemu_get_8s(f, &s->scb_stat);
  1469. qemu_get_8s(f, &s->int_stat);
  1470. for (i = 0; i < 3; i++)
  1471. qemu_get_be32s(f, &s->region[i]);
  1472. qemu_get_buffer(f, s->macaddr, 6);
  1473. for (i = 0; i < 19; i++)
  1474. qemu_get_be32s(f, &s->statcounter[i]);
  1475. for (i = 0; i < 32; i++)
  1476. qemu_get_be16s(f, &s->mdimem[i]);
  1477. /* The eeprom should be saved and restored by its own routines */
  1478. qemu_get_be32s(f, &s->device);
  1479. qemu_get_be32s(f, &s->pointer);
  1480. qemu_get_be32s(f, &s->cu_base);
  1481. qemu_get_be32s(f, &s->cu_offset);
  1482. qemu_get_be32s(f, &s->ru_base);
  1483. qemu_get_be32s(f, &s->ru_offset);
  1484. qemu_get_be32s(f, &s->statsaddr);
  1485. /* Restore epro100_stats_t statistics */
  1486. qemu_get_be32s(f, &s->statistics.tx_good_frames);
  1487. qemu_get_be32s(f, &s->statistics.tx_max_collisions);
  1488. qemu_get_be32s(f, &s->statistics.tx_late_collisions);
  1489. qemu_get_be32s(f, &s->statistics.tx_underruns);
  1490. qemu_get_be32s(f, &s->statistics.tx_lost_crs);
  1491. qemu_get_be32s(f, &s->statistics.tx_deferred);
  1492. qemu_get_be32s(f, &s->statistics.tx_single_collisions);
  1493. qemu_get_be32s(f, &s->statistics.tx_multiple_collisions);
  1494. qemu_get_be32s(f, &s->statistics.tx_total_collisions);
  1495. qemu_get_be32s(f, &s->statistics.rx_good_frames);
  1496. qemu_get_be32s(f, &s->statistics.rx_crc_errors);
  1497. qemu_get_be32s(f, &s->statistics.rx_alignment_errors);
  1498. qemu_get_be32s(f, &s->statistics.rx_resource_errors);
  1499. qemu_get_be32s(f, &s->statistics.rx_overrun_errors);
  1500. qemu_get_be32s(f, &s->statistics.rx_cdt_errors);
  1501. qemu_get_be32s(f, &s->statistics.rx_short_frame_errors);
  1502. qemu_get_be32s(f, &s->statistics.fc_xmt_pause);
  1503. qemu_get_be32s(f, &s->statistics.fc_rcv_pause);
  1504. qemu_get_be32s(f, &s->statistics.fc_rcv_unsupported);
  1505. qemu_get_be16s(f, &s->statistics.xmt_tco_frames);
  1506. qemu_get_be16s(f, &s->statistics.rcv_tco_frames);
  1507. qemu_get_be32s(f, &s->statistics.complete);
  1508. #if 0
  1509. qemu_get_be16s(f, &s->status);
  1510. #endif
  1511. /* Configuration bytes. */
  1512. qemu_get_buffer(f, s->configuration, sizeof(s->configuration));
  1513. return 0;
  1514. }
  1515. static void nic_save(QEMUFile * f, void *opaque)
  1516. {
  1517. EEPRO100State *s = (EEPRO100State *) opaque;
  1518. int i;
  1519. if (s->pci_dev)
  1520. pci_device_save(s->pci_dev, f);
  1521. qemu_put_8s(f, &s->rxcr);
  1522. qemu_put_8s(f, &s->cmd);
  1523. qemu_put_be32s(f, &s->start);
  1524. qemu_put_be32s(f, &s->stop);
  1525. qemu_put_8s(f, &s->boundary);
  1526. qemu_put_8s(f, &s->tsr);
  1527. qemu_put_8s(f, &s->tpsr);
  1528. qemu_put_be16s(f, &s->tcnt);
  1529. qemu_put_be16s(f, &s->rcnt);
  1530. qemu_put_be32s(f, &s->rsar);
  1531. qemu_put_8s(f, &s->rsr);
  1532. qemu_put_8s(f, &s->isr);
  1533. qemu_put_8s(f, &s->dcfg);
  1534. qemu_put_8s(f, &s->imr);
  1535. qemu_put_buffer(f, s->phys, 6);
  1536. qemu_put_8s(f, &s->curpag);
  1537. qemu_put_buffer(f, s->mult, 8);
  1538. qemu_put_buffer(f, s->mem, sizeof(s->mem));
  1539. /* Save all members of struct between scv_stat and mem */
  1540. qemu_put_8s(f, &s->scb_stat);
  1541. qemu_put_8s(f, &s->int_stat);
  1542. for (i = 0; i < 3; i++)
  1543. qemu_put_be32s(f, &s->region[i]);
  1544. qemu_put_buffer(f, s->macaddr, 6);
  1545. for (i = 0; i < 19; i++)
  1546. qemu_put_be32s(f, &s->statcounter[i]);
  1547. for (i = 0; i < 32; i++)
  1548. qemu_put_be16s(f, &s->mdimem[i]);
  1549. /* The eeprom should be saved and restored by its own routines */
  1550. qemu_put_be32s(f, &s->device);
  1551. qemu_put_be32s(f, &s->pointer);
  1552. qemu_put_be32s(f, &s->cu_base);
  1553. qemu_put_be32s(f, &s->cu_offset);
  1554. qemu_put_be32s(f, &s->ru_base);
  1555. qemu_put_be32s(f, &s->ru_offset);
  1556. qemu_put_be32s(f, &s->statsaddr);
  1557. /* Save epro100_stats_t statistics */
  1558. qemu_put_be32s(f, &s->statistics.tx_good_frames);
  1559. qemu_put_be32s(f, &s->statistics.tx_max_collisions);
  1560. qemu_put_be32s(f, &s->statistics.tx_late_collisions);
  1561. qemu_put_be32s(f, &s->statistics.tx_underruns);
  1562. qemu_put_be32s(f, &s->statistics.tx_lost_crs);
  1563. qemu_put_be32s(f, &s->statistics.tx_deferred);
  1564. qemu_put_be32s(f, &s->statistics.tx_single_collisions);
  1565. qemu_put_be32s(f, &s->statistics.tx_multiple_collisions);
  1566. qemu_put_be32s(f, &s->statistics.tx_total_collisions);
  1567. qemu_put_be32s(f, &s->statistics.rx_good_frames);
  1568. qemu_put_be32s(f, &s->statistics.rx_crc_errors);
  1569. qemu_put_be32s(f, &s->statistics.rx_alignment_errors);
  1570. qemu_put_be32s(f, &s->statistics.rx_resource_errors);
  1571. qemu_put_be32s(f, &s->statistics.rx_overrun_errors);
  1572. qemu_put_be32s(f, &s->statistics.rx_cdt_errors);
  1573. qemu_put_be32s(f, &s->statistics.rx_short_frame_errors);
  1574. qemu_put_be32s(f, &s->statistics.fc_xmt_pause);
  1575. qemu_put_be32s(f, &s->statistics.fc_rcv_pause);
  1576. qemu_put_be32s(f, &s->statistics.fc_rcv_unsupported);
  1577. qemu_put_be16s(f, &s->statistics.xmt_tco_frames);
  1578. qemu_put_be16s(f, &s->statistics.rcv_tco_frames);
  1579. qemu_put_be32s(f, &s->statistics.complete);
  1580. #if 0
  1581. qemu_put_be16s(f, &s->status);
  1582. #endif
  1583. /* Configuration bytes. */
  1584. qemu_put_buffer(f, s->configuration, sizeof(s->configuration));
  1585. }
  1586. static void nic_cleanup(VLANClientState *vc)
  1587. {
  1588. EEPRO100State *s = vc->opaque;
  1589. unregister_savevm(vc->model, s);
  1590. eeprom93xx_free(s->eeprom);
  1591. }
  1592. static int pci_nic_uninit(PCIDevice *dev)
  1593. {
  1594. PCIEEPRO100State *d = (PCIEEPRO100State *) dev;
  1595. EEPRO100State *s = &d->eepro100;
  1596. cpu_unregister_io_memory(s->mmio_index);
  1597. return 0;
  1598. }
  1599. static PCIDevice *nic_init(PCIBus * bus, NICInfo * nd, uint32_t device)
  1600. {
  1601. PCIEEPRO100State *d;
  1602. EEPRO100State *s;
  1603. logout("\n");
  1604. d = (PCIEEPRO100State *) pci_register_device(bus, nd->model,
  1605. sizeof(PCIEEPRO100State), -1,
  1606. NULL, NULL);
  1607. if (!d)
  1608. return NULL;
  1609. d->dev.unregister = pci_nic_uninit;
  1610. s = &d->eepro100;
  1611. s->device = device;
  1612. s->pci_dev = &d->dev;
  1613. pci_reset(s);
  1614. /* Add 64 * 2 EEPROM. i82557 and i82558 support a 64 word EEPROM,
  1615. * i82559 and later support 64 or 256 word EEPROM. */
  1616. s->eeprom = eeprom93xx_new(EEPROM_SIZE);
  1617. /* Handler for memory-mapped I/O */
  1618. d->eepro100.mmio_index =
  1619. cpu_register_io_memory(0, pci_mmio_read, pci_mmio_write, s);
  1620. pci_register_io_region(&d->dev, 0, PCI_MEM_SIZE,
  1621. PCI_ADDRESS_SPACE_MEM |
  1622. PCI_ADDRESS_SPACE_MEM_PREFETCH, pci_mmio_map);
  1623. pci_register_io_region(&d->dev, 1, PCI_IO_SIZE, PCI_ADDRESS_SPACE_IO,
  1624. pci_map);
  1625. pci_register_io_region(&d->dev, 2, PCI_FLASH_SIZE, PCI_ADDRESS_SPACE_MEM,
  1626. pci_mmio_map);
  1627. memcpy(s->macaddr, nd->macaddr, 6);
  1628. logout("macaddr: %s\n", nic_dump(&s->macaddr[0], 6));
  1629. assert(s->region[1] == 0);
  1630. nic_reset(s);
  1631. s->vc = qemu_new_vlan_client(nd->vlan, nd->model, nd->name,
  1632. nic_receive, nic_can_receive,
  1633. nic_cleanup, s);
  1634. qemu_format_nic_info_str(s->vc, s->macaddr);
  1635. qemu_register_reset(nic_reset, s);
  1636. register_savevm(s->vc->model, -1, 3, nic_save, nic_load, s);
  1637. return (PCIDevice *)d;
  1638. }
  1639. PCIDevice *pci_i82551_init(PCIBus * bus, NICInfo * nd, int devfn)
  1640. {
  1641. return nic_init(bus, nd, i82551);
  1642. }
  1643. PCIDevice *pci_i82557b_init(PCIBus * bus, NICInfo * nd, int devfn)
  1644. {
  1645. return nic_init(bus, nd, i82557B);
  1646. }
  1647. PCIDevice *pci_i82559er_init(PCIBus * bus, NICInfo * nd, int devfn)
  1648. {
  1649. return nic_init(bus, nd, i82559ER);
  1650. }
  1651. /* eof */