eepro100.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096
  1. /*
  2. * QEMU i8255x (PRO100) emulation
  3. *
  4. * Copyright (C) 2006-2011 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) version 3 or 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, see <http://www.gnu.org/licenses/>.
  21. *
  22. * Tested features (i82559):
  23. * PXE boot (i386 guest, i386 / mips / mipsel / ppc host) ok
  24. * Linux networking (i386) ok
  25. *
  26. * Untested:
  27. * Windows networking
  28. *
  29. * References:
  30. *
  31. * Intel 8255x 10/100 Mbps Ethernet Controller Family
  32. * Open Source Software Developer Manual
  33. *
  34. * TODO:
  35. * * PHY emulation should be separated from nic emulation.
  36. * Most nic emulations could share the same phy code.
  37. * * i82550 is untested. It is programmed like the i82559.
  38. * * i82562 is untested. It is programmed like the i82559.
  39. * * Power management (i82558 and later) is not implemented.
  40. * * Wake-on-LAN is not implemented.
  41. */
  42. #include "qemu/osdep.h"
  43. #include "qemu/units.h"
  44. #include "hw/pci/pci.h"
  45. #include "hw/qdev-properties.h"
  46. #include "migration/vmstate.h"
  47. #include "net/net.h"
  48. #include "net/eth.h"
  49. #include "hw/nvram/eeprom93xx.h"
  50. #include "sysemu/sysemu.h"
  51. #include "sysemu/dma.h"
  52. #include "sysemu/reset.h"
  53. #include "qemu/bitops.h"
  54. #include "qemu/module.h"
  55. #include "qapi/error.h"
  56. /* QEMU sends frames smaller than 60 bytes to ethernet nics.
  57. * Such frames are rejected by real nics and their emulations.
  58. * To avoid this behaviour, other nic emulations pad received
  59. * frames. The following definition enables this padding for
  60. * eepro100, too. We keep the define around in case it might
  61. * become useful the future if the core networking is ever
  62. * changed to pad short packets itself. */
  63. #define CONFIG_PAD_RECEIVED_FRAMES
  64. /* Debug EEPRO100 card. */
  65. #if 0
  66. # define DEBUG_EEPRO100
  67. #endif
  68. #ifdef DEBUG_EEPRO100
  69. #define logout(fmt, ...) fprintf(stderr, "EE100\t%-24s" fmt, __func__, ## __VA_ARGS__)
  70. #else
  71. #define logout(fmt, ...) ((void)0)
  72. #endif
  73. /* Set flags to 0 to disable debug output. */
  74. #define INT 1 /* interrupt related actions */
  75. #define MDI 1 /* mdi related actions */
  76. #define OTHER 1
  77. #define RXTX 1
  78. #define EEPROM 1 /* eeprom related actions */
  79. #define TRACE(flag, command) ((flag) ? (command) : (void)0)
  80. #define missing(text) fprintf(stderr, "eepro100: feature is missing in this emulation: " text "\n")
  81. #define MAX_ETH_FRAME_SIZE 1514
  82. /* This driver supports several different devices which are declared here. */
  83. #define i82550 0x82550
  84. #define i82551 0x82551
  85. #define i82557A 0x82557a
  86. #define i82557B 0x82557b
  87. #define i82557C 0x82557c
  88. #define i82558A 0x82558a
  89. #define i82558B 0x82558b
  90. #define i82559A 0x82559a
  91. #define i82559B 0x82559b
  92. #define i82559C 0x82559c
  93. #define i82559ER 0x82559e
  94. #define i82562 0x82562
  95. #define i82801 0x82801
  96. /* Use 64 word EEPROM. TODO: could be a runtime option. */
  97. #define EEPROM_SIZE 64
  98. #define PCI_MEM_SIZE (4 * KiB)
  99. #define PCI_IO_SIZE 64
  100. #define PCI_FLASH_SIZE (128 * KiB)
  101. #define BITS(n, m) (((0xffffffffU << (31 - n)) >> (31 - n + m)) << m)
  102. /* The SCB accepts the following controls for the Tx and Rx units: */
  103. #define CU_NOP 0x0000 /* No operation. */
  104. #define CU_START 0x0010 /* CU start. */
  105. #define CU_RESUME 0x0020 /* CU resume. */
  106. #define CU_STATSADDR 0x0040 /* Load dump counters address. */
  107. #define CU_SHOWSTATS 0x0050 /* Dump statistical counters. */
  108. #define CU_CMD_BASE 0x0060 /* Load CU base address. */
  109. #define CU_DUMPSTATS 0x0070 /* Dump and reset statistical counters. */
  110. #define CU_SRESUME 0x00a0 /* CU static resume. */
  111. #define RU_NOP 0x0000
  112. #define RX_START 0x0001
  113. #define RX_RESUME 0x0002
  114. #define RU_ABORT 0x0004
  115. #define RX_ADDR_LOAD 0x0006
  116. #define RX_RESUMENR 0x0007
  117. #define INT_MASK 0x0100
  118. #define DRVR_INT 0x0200 /* Driver generated interrupt. */
  119. typedef struct {
  120. const char *name;
  121. const char *desc;
  122. uint16_t device_id;
  123. uint8_t revision;
  124. uint16_t subsystem_vendor_id;
  125. uint16_t subsystem_id;
  126. uint32_t device;
  127. uint8_t stats_size;
  128. bool has_extended_tcb_support;
  129. bool power_management;
  130. } E100PCIDeviceInfo;
  131. /* Offsets to the various registers.
  132. All accesses need not be longword aligned. */
  133. typedef enum {
  134. SCBStatus = 0, /* Status Word. */
  135. SCBAck = 1,
  136. SCBCmd = 2, /* Rx/Command Unit command and status. */
  137. SCBIntmask = 3,
  138. SCBPointer = 4, /* General purpose pointer. */
  139. SCBPort = 8, /* Misc. commands and operands. */
  140. SCBflash = 12, /* Flash memory control. */
  141. SCBeeprom = 14, /* EEPROM control. */
  142. SCBCtrlMDI = 16, /* MDI interface control. */
  143. SCBEarlyRx = 20, /* Early receive byte count. */
  144. SCBFlow = 24, /* Flow Control. */
  145. SCBpmdr = 27, /* Power Management Driver. */
  146. SCBgctrl = 28, /* General Control. */
  147. SCBgstat = 29, /* General Status. */
  148. } E100RegisterOffset;
  149. /* A speedo3 transmit buffer descriptor with two buffers... */
  150. typedef struct {
  151. uint16_t status;
  152. uint16_t command;
  153. uint32_t link; /* void * */
  154. uint32_t tbd_array_addr; /* transmit buffer descriptor array address. */
  155. uint16_t tcb_bytes; /* transmit command block byte count (in lower 14 bits */
  156. uint8_t tx_threshold; /* transmit threshold */
  157. uint8_t tbd_count; /* TBD number */
  158. #if 0
  159. /* This constitutes two "TBD" entries: hdr and data */
  160. uint32_t tx_buf_addr0; /* void *, header of frame to be transmitted. */
  161. int32_t tx_buf_size0; /* Length of Tx hdr. */
  162. uint32_t tx_buf_addr1; /* void *, data to be transmitted. */
  163. int32_t tx_buf_size1; /* Length of Tx data. */
  164. #endif
  165. } eepro100_tx_t;
  166. /* Receive frame descriptor. */
  167. typedef struct {
  168. int16_t status;
  169. uint16_t command;
  170. uint32_t link; /* struct RxFD * */
  171. uint32_t rx_buf_addr; /* void * */
  172. uint16_t count;
  173. uint16_t size;
  174. /* Ethernet frame data follows. */
  175. } eepro100_rx_t;
  176. typedef enum {
  177. COMMAND_EL = BIT(15),
  178. COMMAND_S = BIT(14),
  179. COMMAND_I = BIT(13),
  180. COMMAND_NC = BIT(4),
  181. COMMAND_SF = BIT(3),
  182. COMMAND_CMD = BITS(2, 0),
  183. } scb_command_bit;
  184. typedef enum {
  185. STATUS_C = BIT(15),
  186. STATUS_OK = BIT(13),
  187. } scb_status_bit;
  188. typedef struct {
  189. uint32_t tx_good_frames, tx_max_collisions, tx_late_collisions,
  190. tx_underruns, tx_lost_crs, tx_deferred, tx_single_collisions,
  191. tx_multiple_collisions, tx_total_collisions;
  192. uint32_t rx_good_frames, rx_crc_errors, rx_alignment_errors,
  193. rx_resource_errors, rx_overrun_errors, rx_cdt_errors,
  194. rx_short_frame_errors;
  195. uint32_t fc_xmt_pause, fc_rcv_pause, fc_rcv_unsupported;
  196. uint16_t xmt_tco_frames, rcv_tco_frames;
  197. /* TODO: i82559 has six reserved statistics but a total of 24 dwords. */
  198. uint32_t reserved[4];
  199. } eepro100_stats_t;
  200. typedef enum {
  201. cu_idle = 0,
  202. cu_suspended = 1,
  203. cu_active = 2,
  204. cu_lpq_active = 2,
  205. cu_hqp_active = 3
  206. } cu_state_t;
  207. typedef enum {
  208. ru_idle = 0,
  209. ru_suspended = 1,
  210. ru_no_resources = 2,
  211. ru_ready = 4
  212. } ru_state_t;
  213. typedef struct {
  214. PCIDevice dev;
  215. /* Hash register (multicast mask array, multiple individual addresses). */
  216. uint8_t mult[8];
  217. MemoryRegion mmio_bar;
  218. MemoryRegion io_bar;
  219. MemoryRegion flash_bar;
  220. NICState *nic;
  221. NICConf conf;
  222. uint8_t scb_stat; /* SCB stat/ack byte */
  223. uint8_t int_stat; /* PCI interrupt status */
  224. /* region must not be saved by nic_save. */
  225. uint16_t mdimem[32];
  226. eeprom_t *eeprom;
  227. uint32_t device; /* device variant */
  228. /* (cu_base + cu_offset) address the next command block in the command block list. */
  229. uint32_t cu_base; /* CU base address */
  230. uint32_t cu_offset; /* CU address offset */
  231. /* (ru_base + ru_offset) address the RFD in the Receive Frame Area. */
  232. uint32_t ru_base; /* RU base address */
  233. uint32_t ru_offset; /* RU address offset */
  234. uint32_t statsaddr; /* pointer to eepro100_stats_t */
  235. /* Temporary status information (no need to save these values),
  236. * used while processing CU commands. */
  237. eepro100_tx_t tx; /* transmit buffer descriptor */
  238. uint32_t cb_address; /* = cu_base + cu_offset */
  239. /* Statistical counters. Also used for wake-up packet (i82559). */
  240. eepro100_stats_t statistics;
  241. /* Data in mem is always in the byte order of the controller (le).
  242. * It must be dword aligned to allow direct access to 32 bit values. */
  243. uint8_t mem[PCI_MEM_SIZE] __attribute__((aligned(8)));
  244. /* Configuration bytes. */
  245. uint8_t configuration[22];
  246. /* vmstate for each particular nic */
  247. VMStateDescription *vmstate;
  248. /* Quasi static device properties (no need to save them). */
  249. uint16_t stats_size;
  250. bool has_extended_tcb_support;
  251. } EEPRO100State;
  252. /* Word indices in EEPROM. */
  253. typedef enum {
  254. EEPROM_CNFG_MDIX = 0x03,
  255. EEPROM_ID = 0x05,
  256. EEPROM_PHY_ID = 0x06,
  257. EEPROM_VENDOR_ID = 0x0c,
  258. EEPROM_CONFIG_ASF = 0x0d,
  259. EEPROM_DEVICE_ID = 0x23,
  260. EEPROM_SMBUS_ADDR = 0x90,
  261. } EEPROMOffset;
  262. /* Bit values for EEPROM ID word. */
  263. typedef enum {
  264. EEPROM_ID_MDM = BIT(0), /* Modem */
  265. EEPROM_ID_STB = BIT(1), /* Standby Enable */
  266. EEPROM_ID_WMR = BIT(2), /* ??? */
  267. EEPROM_ID_WOL = BIT(5), /* Wake on LAN */
  268. EEPROM_ID_DPD = BIT(6), /* Deep Power Down */
  269. EEPROM_ID_ALT = BIT(7), /* */
  270. /* BITS(10, 8) device revision */
  271. EEPROM_ID_BD = BIT(11), /* boot disable */
  272. EEPROM_ID_ID = BIT(13), /* id bit */
  273. /* BITS(15, 14) signature */
  274. EEPROM_ID_VALID = BIT(14), /* signature for valid eeprom */
  275. } eeprom_id_bit;
  276. /* Default values for MDI (PHY) registers */
  277. static const uint16_t eepro100_mdi_default[] = {
  278. /* MDI Registers 0 - 6, 7 */
  279. 0x3000, 0x780d, 0x02a8, 0x0154, 0x05e1, 0x0000, 0x0000, 0x0000,
  280. /* MDI Registers 8 - 15 */
  281. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  282. /* MDI Registers 16 - 31 */
  283. 0x0003, 0x0000, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  284. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  285. };
  286. /* Readonly mask for MDI (PHY) registers */
  287. static const uint16_t eepro100_mdi_mask[] = {
  288. 0x0000, 0xffff, 0xffff, 0xffff, 0xc01f, 0xffff, 0xffff, 0x0000,
  289. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  290. 0x0fff, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  291. 0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  292. };
  293. static E100PCIDeviceInfo *eepro100_get_class(EEPRO100State *s);
  294. /* Read a 16 bit control/status (CSR) register. */
  295. static uint16_t e100_read_reg2(EEPRO100State *s, E100RegisterOffset addr)
  296. {
  297. assert(!((uintptr_t)&s->mem[addr] & 1));
  298. return lduw_le_p(&s->mem[addr]);
  299. }
  300. /* Read a 32 bit control/status (CSR) register. */
  301. static uint32_t e100_read_reg4(EEPRO100State *s, E100RegisterOffset addr)
  302. {
  303. assert(!((uintptr_t)&s->mem[addr] & 3));
  304. return ldl_le_p(&s->mem[addr]);
  305. }
  306. /* Write a 16 bit control/status (CSR) register. */
  307. static void e100_write_reg2(EEPRO100State *s, E100RegisterOffset addr,
  308. uint16_t val)
  309. {
  310. assert(!((uintptr_t)&s->mem[addr] & 1));
  311. stw_le_p(&s->mem[addr], val);
  312. }
  313. /* Read a 32 bit control/status (CSR) register. */
  314. static void e100_write_reg4(EEPRO100State *s, E100RegisterOffset addr,
  315. uint32_t val)
  316. {
  317. assert(!((uintptr_t)&s->mem[addr] & 3));
  318. stl_le_p(&s->mem[addr], val);
  319. }
  320. #if defined(DEBUG_EEPRO100)
  321. static const char *nic_dump(const uint8_t * buf, unsigned size)
  322. {
  323. static char dump[3 * 16 + 1];
  324. char *p = &dump[0];
  325. if (size > 16) {
  326. size = 16;
  327. }
  328. while (size-- > 0) {
  329. p += sprintf(p, " %02x", *buf++);
  330. }
  331. return dump;
  332. }
  333. #endif /* DEBUG_EEPRO100 */
  334. enum scb_stat_ack {
  335. stat_ack_not_ours = 0x00,
  336. stat_ack_sw_gen = 0x04,
  337. stat_ack_rnr = 0x10,
  338. stat_ack_cu_idle = 0x20,
  339. stat_ack_frame_rx = 0x40,
  340. stat_ack_cu_cmd_done = 0x80,
  341. stat_ack_not_present = 0xFF,
  342. stat_ack_rx = (stat_ack_sw_gen | stat_ack_rnr | stat_ack_frame_rx),
  343. stat_ack_tx = (stat_ack_cu_idle | stat_ack_cu_cmd_done),
  344. };
  345. static void disable_interrupt(EEPRO100State * s)
  346. {
  347. if (s->int_stat) {
  348. TRACE(INT, logout("interrupt disabled\n"));
  349. pci_irq_deassert(&s->dev);
  350. s->int_stat = 0;
  351. }
  352. }
  353. static void enable_interrupt(EEPRO100State * s)
  354. {
  355. if (!s->int_stat) {
  356. TRACE(INT, logout("interrupt enabled\n"));
  357. pci_irq_assert(&s->dev);
  358. s->int_stat = 1;
  359. }
  360. }
  361. static void eepro100_acknowledge(EEPRO100State * s)
  362. {
  363. s->scb_stat &= ~s->mem[SCBAck];
  364. s->mem[SCBAck] = s->scb_stat;
  365. if (s->scb_stat == 0) {
  366. disable_interrupt(s);
  367. }
  368. }
  369. static void eepro100_interrupt(EEPRO100State * s, uint8_t status)
  370. {
  371. uint8_t mask = ~s->mem[SCBIntmask];
  372. s->mem[SCBAck] |= status;
  373. status = s->scb_stat = s->mem[SCBAck];
  374. status &= (mask | 0x0f);
  375. #if 0
  376. status &= (~s->mem[SCBIntmask] | 0x0xf);
  377. #endif
  378. if (status && (mask & 0x01)) {
  379. /* SCB mask and SCB Bit M do not disable interrupt. */
  380. enable_interrupt(s);
  381. } else if (s->int_stat) {
  382. disable_interrupt(s);
  383. }
  384. }
  385. static void eepro100_cx_interrupt(EEPRO100State * s)
  386. {
  387. /* CU completed action command. */
  388. /* Transmit not ok (82557 only, not in emulation). */
  389. eepro100_interrupt(s, 0x80);
  390. }
  391. static void eepro100_cna_interrupt(EEPRO100State * s)
  392. {
  393. /* CU left the active state. */
  394. eepro100_interrupt(s, 0x20);
  395. }
  396. static void eepro100_fr_interrupt(EEPRO100State * s)
  397. {
  398. /* RU received a complete frame. */
  399. eepro100_interrupt(s, 0x40);
  400. }
  401. static void eepro100_rnr_interrupt(EEPRO100State * s)
  402. {
  403. /* RU is not ready. */
  404. eepro100_interrupt(s, 0x10);
  405. }
  406. static void eepro100_mdi_interrupt(EEPRO100State * s)
  407. {
  408. /* MDI completed read or write cycle. */
  409. eepro100_interrupt(s, 0x08);
  410. }
  411. static void eepro100_swi_interrupt(EEPRO100State * s)
  412. {
  413. /* Software has requested an interrupt. */
  414. eepro100_interrupt(s, 0x04);
  415. }
  416. #if 0
  417. static void eepro100_fcp_interrupt(EEPRO100State * s)
  418. {
  419. /* Flow control pause interrupt (82558 and later). */
  420. eepro100_interrupt(s, 0x01);
  421. }
  422. #endif
  423. static void e100_pci_reset(EEPRO100State *s, Error **errp)
  424. {
  425. E100PCIDeviceInfo *info = eepro100_get_class(s);
  426. uint32_t device = s->device;
  427. uint8_t *pci_conf = s->dev.config;
  428. TRACE(OTHER, logout("%p\n", s));
  429. /* PCI Status */
  430. pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM |
  431. PCI_STATUS_FAST_BACK);
  432. /* PCI Latency Timer */
  433. pci_set_byte(pci_conf + PCI_LATENCY_TIMER, 0x20); /* latency timer = 32 clocks */
  434. /* Capability Pointer is set by PCI framework. */
  435. /* Interrupt Line */
  436. /* Interrupt Pin */
  437. pci_set_byte(pci_conf + PCI_INTERRUPT_PIN, 1); /* interrupt pin A */
  438. /* Minimum Grant */
  439. pci_set_byte(pci_conf + PCI_MIN_GNT, 0x08);
  440. /* Maximum Latency */
  441. pci_set_byte(pci_conf + PCI_MAX_LAT, 0x18);
  442. s->stats_size = info->stats_size;
  443. s->has_extended_tcb_support = info->has_extended_tcb_support;
  444. switch (device) {
  445. case i82550:
  446. case i82551:
  447. case i82557A:
  448. case i82557B:
  449. case i82557C:
  450. case i82558A:
  451. case i82558B:
  452. case i82559A:
  453. case i82559B:
  454. case i82559ER:
  455. case i82562:
  456. case i82801:
  457. case i82559C:
  458. break;
  459. default:
  460. logout("Device %X is undefined!\n", device);
  461. }
  462. /* Standard TxCB. */
  463. s->configuration[6] |= BIT(4);
  464. /* Standard statistical counters. */
  465. s->configuration[6] |= BIT(5);
  466. if (s->stats_size == 80) {
  467. /* TODO: check TCO Statistical Counters bit. Documentation not clear. */
  468. if (s->configuration[6] & BIT(2)) {
  469. /* TCO statistical counters. */
  470. assert(s->configuration[6] & BIT(5));
  471. } else {
  472. if (s->configuration[6] & BIT(5)) {
  473. /* No extended statistical counters, i82557 compatible. */
  474. s->stats_size = 64;
  475. } else {
  476. /* i82558 compatible. */
  477. s->stats_size = 76;
  478. }
  479. }
  480. } else {
  481. if (s->configuration[6] & BIT(5)) {
  482. /* No extended statistical counters. */
  483. s->stats_size = 64;
  484. }
  485. }
  486. assert(s->stats_size > 0 && s->stats_size <= sizeof(s->statistics));
  487. if (info->power_management) {
  488. /* Power Management Capabilities */
  489. int cfg_offset = 0xdc;
  490. int r = pci_add_capability(&s->dev, PCI_CAP_ID_PM,
  491. cfg_offset, PCI_PM_SIZEOF,
  492. errp);
  493. if (r < 0) {
  494. return;
  495. }
  496. pci_set_word(pci_conf + cfg_offset + PCI_PM_PMC, 0x7e21);
  497. #if 0 /* TODO: replace dummy code for power management emulation. */
  498. /* TODO: Power Management Control / Status. */
  499. pci_set_word(pci_conf + cfg_offset + PCI_PM_CTRL, 0x0000);
  500. /* TODO: Ethernet Power Consumption Registers (i82559 and later). */
  501. pci_set_byte(pci_conf + cfg_offset + PCI_PM_PPB_EXTENSIONS, 0x0000);
  502. #endif
  503. }
  504. #if EEPROM_SIZE > 0
  505. if (device == i82557C || device == i82558B || device == i82559C) {
  506. /*
  507. TODO: get vendor id from EEPROM for i82557C or later.
  508. TODO: get device id from EEPROM for i82557C or later.
  509. TODO: status bit 4 can be disabled by EEPROM for i82558, i82559.
  510. TODO: header type is determined by EEPROM for i82559.
  511. TODO: get subsystem id from EEPROM for i82557C or later.
  512. TODO: get subsystem vendor id from EEPROM for i82557C or later.
  513. TODO: exp. rom baddr depends on a bit in EEPROM for i82558 or later.
  514. TODO: capability pointer depends on EEPROM for i82558.
  515. */
  516. logout("Get device id and revision from EEPROM!!!\n");
  517. }
  518. #endif /* EEPROM_SIZE > 0 */
  519. }
  520. static void nic_selective_reset(EEPRO100State * s)
  521. {
  522. size_t i;
  523. uint16_t *eeprom_contents = eeprom93xx_data(s->eeprom);
  524. #if 0
  525. eeprom93xx_reset(s->eeprom);
  526. #endif
  527. memcpy(eeprom_contents, s->conf.macaddr.a, 6);
  528. eeprom_contents[EEPROM_ID] = EEPROM_ID_VALID;
  529. if (s->device == i82557B || s->device == i82557C)
  530. eeprom_contents[5] = 0x0100;
  531. eeprom_contents[EEPROM_PHY_ID] = 1;
  532. uint16_t sum = 0;
  533. for (i = 0; i < EEPROM_SIZE - 1; i++) {
  534. sum += eeprom_contents[i];
  535. }
  536. eeprom_contents[EEPROM_SIZE - 1] = 0xbaba - sum;
  537. TRACE(EEPROM, logout("checksum=0x%04x\n", eeprom_contents[EEPROM_SIZE - 1]));
  538. memset(s->mem, 0, sizeof(s->mem));
  539. e100_write_reg4(s, SCBCtrlMDI, BIT(21));
  540. assert(sizeof(s->mdimem) == sizeof(eepro100_mdi_default));
  541. memcpy(&s->mdimem[0], &eepro100_mdi_default[0], sizeof(s->mdimem));
  542. }
  543. static void nic_reset(void *opaque)
  544. {
  545. EEPRO100State *s = opaque;
  546. TRACE(OTHER, logout("%p\n", s));
  547. /* TODO: Clearing of hash register for selective reset, too? */
  548. memset(&s->mult[0], 0, sizeof(s->mult));
  549. nic_selective_reset(s);
  550. }
  551. #if defined(DEBUG_EEPRO100)
  552. static const char * const e100_reg[PCI_IO_SIZE / 4] = {
  553. "Command/Status",
  554. "General Pointer",
  555. "Port",
  556. "EEPROM/Flash Control",
  557. "MDI Control",
  558. "Receive DMA Byte Count",
  559. "Flow Control",
  560. "General Status/Control"
  561. };
  562. static char *regname(uint32_t addr)
  563. {
  564. static char buf[32];
  565. if (addr < PCI_IO_SIZE) {
  566. const char *r = e100_reg[addr / 4];
  567. if (r != 0) {
  568. snprintf(buf, sizeof(buf), "%s+%u", r, addr % 4);
  569. } else {
  570. snprintf(buf, sizeof(buf), "0x%02x", addr);
  571. }
  572. } else {
  573. snprintf(buf, sizeof(buf), "??? 0x%08x", addr);
  574. }
  575. return buf;
  576. }
  577. #endif /* DEBUG_EEPRO100 */
  578. /*****************************************************************************
  579. *
  580. * Command emulation.
  581. *
  582. ****************************************************************************/
  583. #if 0
  584. static uint16_t eepro100_read_command(EEPRO100State * s)
  585. {
  586. uint16_t val = 0xffff;
  587. TRACE(OTHER, logout("val=0x%04x\n", val));
  588. return val;
  589. }
  590. #endif
  591. /* Commands that can be put in a command list entry. */
  592. enum commands {
  593. CmdNOp = 0,
  594. CmdIASetup = 1,
  595. CmdConfigure = 2,
  596. CmdMulticastList = 3,
  597. CmdTx = 4,
  598. CmdTDR = 5, /* load microcode */
  599. CmdDump = 6,
  600. CmdDiagnose = 7,
  601. /* And some extra flags: */
  602. CmdSuspend = 0x4000, /* Suspend after completion. */
  603. CmdIntr = 0x2000, /* Interrupt after completion. */
  604. CmdTxFlex = 0x0008, /* Use "Flexible mode" for CmdTx command. */
  605. };
  606. static cu_state_t get_cu_state(EEPRO100State * s)
  607. {
  608. return ((s->mem[SCBStatus] & BITS(7, 6)) >> 6);
  609. }
  610. static void set_cu_state(EEPRO100State * s, cu_state_t state)
  611. {
  612. s->mem[SCBStatus] = (s->mem[SCBStatus] & ~BITS(7, 6)) + (state << 6);
  613. }
  614. static ru_state_t get_ru_state(EEPRO100State * s)
  615. {
  616. return ((s->mem[SCBStatus] & BITS(5, 2)) >> 2);
  617. }
  618. static void set_ru_state(EEPRO100State * s, ru_state_t state)
  619. {
  620. s->mem[SCBStatus] = (s->mem[SCBStatus] & ~BITS(5, 2)) + (state << 2);
  621. }
  622. static void dump_statistics(EEPRO100State * s)
  623. {
  624. /* Dump statistical data. Most data is never changed by the emulation
  625. * and always 0, so we first just copy the whole block and then those
  626. * values which really matter.
  627. * Number of data should check configuration!!!
  628. */
  629. pci_dma_write(&s->dev, s->statsaddr, &s->statistics, s->stats_size);
  630. stl_le_pci_dma(&s->dev, s->statsaddr + 0,
  631. s->statistics.tx_good_frames);
  632. stl_le_pci_dma(&s->dev, s->statsaddr + 36,
  633. s->statistics.rx_good_frames);
  634. stl_le_pci_dma(&s->dev, s->statsaddr + 48,
  635. s->statistics.rx_resource_errors);
  636. stl_le_pci_dma(&s->dev, s->statsaddr + 60,
  637. s->statistics.rx_short_frame_errors);
  638. #if 0
  639. stw_le_pci_dma(&s->dev, s->statsaddr + 76, s->statistics.xmt_tco_frames);
  640. stw_le_pci_dma(&s->dev, s->statsaddr + 78, s->statistics.rcv_tco_frames);
  641. missing("CU dump statistical counters");
  642. #endif
  643. }
  644. static void read_cb(EEPRO100State *s)
  645. {
  646. pci_dma_read(&s->dev, s->cb_address, &s->tx, sizeof(s->tx));
  647. s->tx.status = le16_to_cpu(s->tx.status);
  648. s->tx.command = le16_to_cpu(s->tx.command);
  649. s->tx.link = le32_to_cpu(s->tx.link);
  650. s->tx.tbd_array_addr = le32_to_cpu(s->tx.tbd_array_addr);
  651. s->tx.tcb_bytes = le16_to_cpu(s->tx.tcb_bytes);
  652. }
  653. static void tx_command(EEPRO100State *s)
  654. {
  655. uint32_t tbd_array = s->tx.tbd_array_addr;
  656. uint16_t tcb_bytes = s->tx.tcb_bytes & 0x3fff;
  657. /* Sends larger than MAX_ETH_FRAME_SIZE are allowed, up to 2600 bytes. */
  658. uint8_t buf[2600];
  659. uint16_t size = 0;
  660. uint32_t tbd_address = s->cb_address + 0x10;
  661. TRACE(RXTX, logout
  662. ("transmit, TBD array address 0x%08x, TCB byte count 0x%04x, TBD count %u\n",
  663. tbd_array, tcb_bytes, s->tx.tbd_count));
  664. if (tcb_bytes > 2600) {
  665. logout("TCB byte count too large, using 2600\n");
  666. tcb_bytes = 2600;
  667. }
  668. if (!((tcb_bytes > 0) || (tbd_array != 0xffffffff))) {
  669. logout
  670. ("illegal values of TBD array address and TCB byte count!\n");
  671. }
  672. assert(tcb_bytes <= sizeof(buf));
  673. while (size < tcb_bytes) {
  674. TRACE(RXTX, logout
  675. ("TBD (simplified mode): buffer address 0x%08x, size 0x%04x\n",
  676. tbd_address, tcb_bytes));
  677. pci_dma_read(&s->dev, tbd_address, &buf[size], tcb_bytes);
  678. size += tcb_bytes;
  679. }
  680. if (tbd_array == 0xffffffff) {
  681. /* Simplified mode. Was already handled by code above. */
  682. } else {
  683. /* Flexible mode. */
  684. uint8_t tbd_count = 0;
  685. if (s->has_extended_tcb_support && !(s->configuration[6] & BIT(4))) {
  686. /* Extended Flexible TCB. */
  687. for (; tbd_count < 2; tbd_count++) {
  688. uint32_t tx_buffer_address = ldl_le_pci_dma(&s->dev,
  689. tbd_address);
  690. uint16_t tx_buffer_size = lduw_le_pci_dma(&s->dev,
  691. tbd_address + 4);
  692. uint16_t tx_buffer_el = lduw_le_pci_dma(&s->dev,
  693. tbd_address + 6);
  694. tbd_address += 8;
  695. TRACE(RXTX, logout
  696. ("TBD (extended flexible mode): buffer address 0x%08x, size 0x%04x\n",
  697. tx_buffer_address, tx_buffer_size));
  698. tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
  699. pci_dma_read(&s->dev, tx_buffer_address,
  700. &buf[size], tx_buffer_size);
  701. size += tx_buffer_size;
  702. if (tx_buffer_el & 1) {
  703. break;
  704. }
  705. }
  706. }
  707. tbd_address = tbd_array;
  708. for (; tbd_count < s->tx.tbd_count; tbd_count++) {
  709. uint32_t tx_buffer_address = ldl_le_pci_dma(&s->dev, tbd_address);
  710. uint16_t tx_buffer_size = lduw_le_pci_dma(&s->dev, tbd_address + 4);
  711. uint16_t tx_buffer_el = lduw_le_pci_dma(&s->dev, tbd_address + 6);
  712. tbd_address += 8;
  713. TRACE(RXTX, logout
  714. ("TBD (flexible mode): buffer address 0x%08x, size 0x%04x\n",
  715. tx_buffer_address, tx_buffer_size));
  716. tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
  717. pci_dma_read(&s->dev, tx_buffer_address,
  718. &buf[size], tx_buffer_size);
  719. size += tx_buffer_size;
  720. if (tx_buffer_el & 1) {
  721. break;
  722. }
  723. }
  724. }
  725. TRACE(RXTX, logout("%p sending frame, len=%d,%s\n", s, size, nic_dump(buf, size)));
  726. qemu_send_packet(qemu_get_queue(s->nic), buf, size);
  727. s->statistics.tx_good_frames++;
  728. /* Transmit with bad status would raise an CX/TNO interrupt.
  729. * (82557 only). Emulation never has bad status. */
  730. #if 0
  731. eepro100_cx_interrupt(s);
  732. #endif
  733. }
  734. static void set_multicast_list(EEPRO100State *s)
  735. {
  736. uint16_t multicast_count = s->tx.tbd_array_addr & BITS(13, 0);
  737. uint16_t i;
  738. memset(&s->mult[0], 0, sizeof(s->mult));
  739. TRACE(OTHER, logout("multicast list, multicast count = %u\n", multicast_count));
  740. for (i = 0; i < multicast_count; i += 6) {
  741. uint8_t multicast_addr[6];
  742. pci_dma_read(&s->dev, s->cb_address + 10 + i, multicast_addr, 6);
  743. TRACE(OTHER, logout("multicast entry %s\n", nic_dump(multicast_addr, 6)));
  744. unsigned mcast_idx = (net_crc32(multicast_addr, ETH_ALEN) &
  745. BITS(7, 2)) >> 2;
  746. assert(mcast_idx < 64);
  747. s->mult[mcast_idx >> 3] |= (1 << (mcast_idx & 7));
  748. }
  749. }
  750. static void action_command(EEPRO100State *s)
  751. {
  752. /* The loop below won't stop if it gets special handcrafted data.
  753. Therefore we limit the number of iterations. */
  754. unsigned max_loop_count = 16;
  755. for (;;) {
  756. bool bit_el;
  757. bool bit_s;
  758. bool bit_i;
  759. bool bit_nc;
  760. uint16_t ok_status = STATUS_OK;
  761. s->cb_address = s->cu_base + s->cu_offset;
  762. read_cb(s);
  763. bit_el = ((s->tx.command & COMMAND_EL) != 0);
  764. bit_s = ((s->tx.command & COMMAND_S) != 0);
  765. bit_i = ((s->tx.command & COMMAND_I) != 0);
  766. bit_nc = ((s->tx.command & COMMAND_NC) != 0);
  767. #if 0
  768. bool bit_sf = ((s->tx.command & COMMAND_SF) != 0);
  769. #endif
  770. if (max_loop_count-- == 0) {
  771. /* Prevent an endless loop. */
  772. logout("loop in %s:%u\n", __FILE__, __LINE__);
  773. break;
  774. }
  775. s->cu_offset = s->tx.link;
  776. TRACE(OTHER,
  777. logout("val=(cu start), status=0x%04x, command=0x%04x, link=0x%08x\n",
  778. s->tx.status, s->tx.command, s->tx.link));
  779. switch (s->tx.command & COMMAND_CMD) {
  780. case CmdNOp:
  781. /* Do nothing. */
  782. break;
  783. case CmdIASetup:
  784. pci_dma_read(&s->dev, s->cb_address + 8, &s->conf.macaddr.a[0], 6);
  785. TRACE(OTHER, logout("macaddr: %s\n", nic_dump(&s->conf.macaddr.a[0], 6)));
  786. break;
  787. case CmdConfigure:
  788. pci_dma_read(&s->dev, s->cb_address + 8,
  789. &s->configuration[0], sizeof(s->configuration));
  790. TRACE(OTHER, logout("configuration: %s\n",
  791. nic_dump(&s->configuration[0], 16)));
  792. TRACE(OTHER, logout("configuration: %s\n",
  793. nic_dump(&s->configuration[16],
  794. ARRAY_SIZE(s->configuration) - 16)));
  795. if (s->configuration[20] & BIT(6)) {
  796. TRACE(OTHER, logout("Multiple IA bit\n"));
  797. }
  798. break;
  799. case CmdMulticastList:
  800. set_multicast_list(s);
  801. break;
  802. case CmdTx:
  803. if (bit_nc) {
  804. missing("CmdTx: NC = 0");
  805. ok_status = 0;
  806. break;
  807. }
  808. tx_command(s);
  809. break;
  810. case CmdTDR:
  811. TRACE(OTHER, logout("load microcode\n"));
  812. /* Starting with offset 8, the command contains
  813. * 64 dwords microcode which we just ignore here. */
  814. break;
  815. case CmdDiagnose:
  816. TRACE(OTHER, logout("diagnose\n"));
  817. /* Make sure error flag is not set. */
  818. s->tx.status = 0;
  819. break;
  820. default:
  821. missing("undefined command");
  822. ok_status = 0;
  823. break;
  824. }
  825. /* Write new status. */
  826. stw_le_pci_dma(&s->dev, s->cb_address,
  827. s->tx.status | ok_status | STATUS_C);
  828. if (bit_i) {
  829. /* CU completed action. */
  830. eepro100_cx_interrupt(s);
  831. }
  832. if (bit_el) {
  833. /* CU becomes idle. Terminate command loop. */
  834. set_cu_state(s, cu_idle);
  835. eepro100_cna_interrupt(s);
  836. break;
  837. } else if (bit_s) {
  838. /* CU becomes suspended. Terminate command loop. */
  839. set_cu_state(s, cu_suspended);
  840. eepro100_cna_interrupt(s);
  841. break;
  842. } else {
  843. /* More entries in list. */
  844. TRACE(OTHER, logout("CU list with at least one more entry\n"));
  845. }
  846. }
  847. TRACE(OTHER, logout("CU list empty\n"));
  848. /* List is empty. Now CU is idle or suspended. */
  849. }
  850. static void eepro100_cu_command(EEPRO100State * s, uint8_t val)
  851. {
  852. cu_state_t cu_state;
  853. switch (val) {
  854. case CU_NOP:
  855. /* No operation. */
  856. break;
  857. case CU_START:
  858. cu_state = get_cu_state(s);
  859. if (cu_state != cu_idle && cu_state != cu_suspended) {
  860. /* Intel documentation says that CU must be idle or suspended
  861. * for the CU start command. */
  862. logout("unexpected CU state is %u\n", cu_state);
  863. }
  864. set_cu_state(s, cu_active);
  865. s->cu_offset = e100_read_reg4(s, SCBPointer);
  866. action_command(s);
  867. break;
  868. case CU_RESUME:
  869. if (get_cu_state(s) != cu_suspended) {
  870. logout("bad CU resume from CU state %u\n", get_cu_state(s));
  871. /* Workaround for bad Linux eepro100 driver which resumes
  872. * from idle state. */
  873. #if 0
  874. missing("cu resume");
  875. #endif
  876. set_cu_state(s, cu_suspended);
  877. }
  878. if (get_cu_state(s) == cu_suspended) {
  879. TRACE(OTHER, logout("CU resuming\n"));
  880. set_cu_state(s, cu_active);
  881. action_command(s);
  882. }
  883. break;
  884. case CU_STATSADDR:
  885. /* Load dump counters address. */
  886. s->statsaddr = e100_read_reg4(s, SCBPointer);
  887. TRACE(OTHER, logout("val=0x%02x (dump counters address)\n", val));
  888. if (s->statsaddr & 3) {
  889. /* Memory must be Dword aligned. */
  890. logout("unaligned dump counters address\n");
  891. /* Handling of misaligned addresses is undefined.
  892. * Here we align the address by ignoring the lower bits. */
  893. /* TODO: Test unaligned dump counter address on real hardware. */
  894. s->statsaddr &= ~3;
  895. }
  896. break;
  897. case CU_SHOWSTATS:
  898. /* Dump statistical counters. */
  899. TRACE(OTHER, logout("val=0x%02x (dump stats)\n", val));
  900. dump_statistics(s);
  901. stl_le_pci_dma(&s->dev, s->statsaddr + s->stats_size, 0xa005);
  902. break;
  903. case CU_CMD_BASE:
  904. /* Load CU base. */
  905. TRACE(OTHER, logout("val=0x%02x (CU base address)\n", val));
  906. s->cu_base = e100_read_reg4(s, SCBPointer);
  907. break;
  908. case CU_DUMPSTATS:
  909. /* Dump and reset statistical counters. */
  910. TRACE(OTHER, logout("val=0x%02x (dump stats and reset)\n", val));
  911. dump_statistics(s);
  912. stl_le_pci_dma(&s->dev, s->statsaddr + s->stats_size, 0xa007);
  913. memset(&s->statistics, 0, sizeof(s->statistics));
  914. break;
  915. case CU_SRESUME:
  916. /* CU static resume. */
  917. missing("CU static resume");
  918. break;
  919. default:
  920. missing("Undefined CU command");
  921. }
  922. }
  923. static void eepro100_ru_command(EEPRO100State * s, uint8_t val)
  924. {
  925. switch (val) {
  926. case RU_NOP:
  927. /* No operation. */
  928. break;
  929. case RX_START:
  930. /* RU start. */
  931. if (get_ru_state(s) != ru_idle) {
  932. logout("RU state is %u, should be %u\n", get_ru_state(s), ru_idle);
  933. #if 0
  934. assert(!"wrong RU state");
  935. #endif
  936. }
  937. set_ru_state(s, ru_ready);
  938. s->ru_offset = e100_read_reg4(s, SCBPointer);
  939. qemu_flush_queued_packets(qemu_get_queue(s->nic));
  940. TRACE(OTHER, logout("val=0x%02x (rx start)\n", val));
  941. break;
  942. case RX_RESUME:
  943. /* Restart RU. */
  944. if (get_ru_state(s) != ru_suspended) {
  945. logout("RU state is %u, should be %u\n", get_ru_state(s),
  946. ru_suspended);
  947. #if 0
  948. assert(!"wrong RU state");
  949. #endif
  950. }
  951. set_ru_state(s, ru_ready);
  952. break;
  953. case RU_ABORT:
  954. /* RU abort. */
  955. if (get_ru_state(s) == ru_ready) {
  956. eepro100_rnr_interrupt(s);
  957. }
  958. set_ru_state(s, ru_idle);
  959. break;
  960. case RX_ADDR_LOAD:
  961. /* Load RU base. */
  962. TRACE(OTHER, logout("val=0x%02x (RU base address)\n", val));
  963. s->ru_base = e100_read_reg4(s, SCBPointer);
  964. break;
  965. default:
  966. logout("val=0x%02x (undefined RU command)\n", val);
  967. missing("Undefined SU command");
  968. }
  969. }
  970. static void eepro100_write_command(EEPRO100State * s, uint8_t val)
  971. {
  972. eepro100_ru_command(s, val & 0x0f);
  973. eepro100_cu_command(s, val & 0xf0);
  974. if ((val) == 0) {
  975. TRACE(OTHER, logout("val=0x%02x\n", val));
  976. }
  977. /* Clear command byte after command was accepted. */
  978. s->mem[SCBCmd] = 0;
  979. }
  980. /*****************************************************************************
  981. *
  982. * EEPROM emulation.
  983. *
  984. ****************************************************************************/
  985. #define EEPROM_CS 0x02
  986. #define EEPROM_SK 0x01
  987. #define EEPROM_DI 0x04
  988. #define EEPROM_DO 0x08
  989. static uint16_t eepro100_read_eeprom(EEPRO100State * s)
  990. {
  991. uint16_t val = e100_read_reg2(s, SCBeeprom);
  992. if (eeprom93xx_read(s->eeprom)) {
  993. val |= EEPROM_DO;
  994. } else {
  995. val &= ~EEPROM_DO;
  996. }
  997. TRACE(EEPROM, logout("val=0x%04x\n", val));
  998. return val;
  999. }
  1000. static void eepro100_write_eeprom(eeprom_t * eeprom, uint8_t val)
  1001. {
  1002. TRACE(EEPROM, logout("val=0x%02x\n", val));
  1003. /* mask unwritable bits */
  1004. #if 0
  1005. val = SET_MASKED(val, 0x31, eeprom->value);
  1006. #endif
  1007. int eecs = ((val & EEPROM_CS) != 0);
  1008. int eesk = ((val & EEPROM_SK) != 0);
  1009. int eedi = ((val & EEPROM_DI) != 0);
  1010. eeprom93xx_write(eeprom, eecs, eesk, eedi);
  1011. }
  1012. /*****************************************************************************
  1013. *
  1014. * MDI emulation.
  1015. *
  1016. ****************************************************************************/
  1017. #if defined(DEBUG_EEPRO100)
  1018. static const char * const mdi_op_name[] = {
  1019. "opcode 0",
  1020. "write",
  1021. "read",
  1022. "opcode 3"
  1023. };
  1024. static const char * const mdi_reg_name[] = {
  1025. "Control",
  1026. "Status",
  1027. "PHY Identification (Word 1)",
  1028. "PHY Identification (Word 2)",
  1029. "Auto-Negotiation Advertisement",
  1030. "Auto-Negotiation Link Partner Ability",
  1031. "Auto-Negotiation Expansion"
  1032. };
  1033. static const char *reg2name(uint8_t reg)
  1034. {
  1035. static char buffer[10];
  1036. const char *p = buffer;
  1037. if (reg < ARRAY_SIZE(mdi_reg_name)) {
  1038. p = mdi_reg_name[reg];
  1039. } else {
  1040. snprintf(buffer, sizeof(buffer), "reg=0x%02x", reg);
  1041. }
  1042. return p;
  1043. }
  1044. #endif /* DEBUG_EEPRO100 */
  1045. static uint32_t eepro100_read_mdi(EEPRO100State * s)
  1046. {
  1047. uint32_t val = e100_read_reg4(s, SCBCtrlMDI);
  1048. #ifdef DEBUG_EEPRO100
  1049. uint8_t raiseint = (val & BIT(29)) >> 29;
  1050. uint8_t opcode = (val & BITS(27, 26)) >> 26;
  1051. uint8_t phy = (val & BITS(25, 21)) >> 21;
  1052. uint8_t reg = (val & BITS(20, 16)) >> 16;
  1053. uint16_t data = (val & BITS(15, 0));
  1054. #endif
  1055. /* Emulation takes no time to finish MDI transaction. */
  1056. val |= BIT(28);
  1057. TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
  1058. val, raiseint, mdi_op_name[opcode], phy,
  1059. reg2name(reg), data));
  1060. return val;
  1061. }
  1062. static void eepro100_write_mdi(EEPRO100State *s)
  1063. {
  1064. uint32_t val = e100_read_reg4(s, SCBCtrlMDI);
  1065. uint8_t raiseint = (val & BIT(29)) >> 29;
  1066. uint8_t opcode = (val & BITS(27, 26)) >> 26;
  1067. uint8_t phy = (val & BITS(25, 21)) >> 21;
  1068. uint8_t reg = (val & BITS(20, 16)) >> 16;
  1069. uint16_t data = (val & BITS(15, 0));
  1070. TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
  1071. val, raiseint, mdi_op_name[opcode], phy, reg2name(reg), data));
  1072. if (phy != 1) {
  1073. /* Unsupported PHY address. */
  1074. #if 0
  1075. logout("phy must be 1 but is %u\n", phy);
  1076. #endif
  1077. data = 0;
  1078. } else if (opcode != 1 && opcode != 2) {
  1079. /* Unsupported opcode. */
  1080. logout("opcode must be 1 or 2 but is %u\n", opcode);
  1081. data = 0;
  1082. } else if (reg > 6) {
  1083. /* Unsupported register. */
  1084. logout("register must be 0...6 but is %u\n", reg);
  1085. data = 0;
  1086. } else {
  1087. TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
  1088. val, raiseint, mdi_op_name[opcode], phy,
  1089. reg2name(reg), data));
  1090. if (opcode == 1) {
  1091. /* MDI write */
  1092. switch (reg) {
  1093. case 0: /* Control Register */
  1094. if (data & 0x8000) {
  1095. /* Reset status and control registers to default. */
  1096. s->mdimem[0] = eepro100_mdi_default[0];
  1097. s->mdimem[1] = eepro100_mdi_default[1];
  1098. data = s->mdimem[reg];
  1099. } else {
  1100. /* Restart Auto Configuration = Normal Operation */
  1101. data &= ~0x0200;
  1102. }
  1103. break;
  1104. case 1: /* Status Register */
  1105. missing("not writable");
  1106. break;
  1107. case 2: /* PHY Identification Register (Word 1) */
  1108. case 3: /* PHY Identification Register (Word 2) */
  1109. missing("not implemented");
  1110. break;
  1111. case 4: /* Auto-Negotiation Advertisement Register */
  1112. case 5: /* Auto-Negotiation Link Partner Ability Register */
  1113. break;
  1114. case 6: /* Auto-Negotiation Expansion Register */
  1115. default:
  1116. missing("not implemented");
  1117. }
  1118. s->mdimem[reg] &= eepro100_mdi_mask[reg];
  1119. s->mdimem[reg] |= data & ~eepro100_mdi_mask[reg];
  1120. } else if (opcode == 2) {
  1121. /* MDI read */
  1122. switch (reg) {
  1123. case 0: /* Control Register */
  1124. if (data & 0x8000) {
  1125. /* Reset status and control registers to default. */
  1126. s->mdimem[0] = eepro100_mdi_default[0];
  1127. s->mdimem[1] = eepro100_mdi_default[1];
  1128. }
  1129. break;
  1130. case 1: /* Status Register */
  1131. s->mdimem[reg] |= 0x0020;
  1132. break;
  1133. case 2: /* PHY Identification Register (Word 1) */
  1134. case 3: /* PHY Identification Register (Word 2) */
  1135. case 4: /* Auto-Negotiation Advertisement Register */
  1136. break;
  1137. case 5: /* Auto-Negotiation Link Partner Ability Register */
  1138. s->mdimem[reg] = 0x41fe;
  1139. break;
  1140. case 6: /* Auto-Negotiation Expansion Register */
  1141. s->mdimem[reg] = 0x0001;
  1142. break;
  1143. }
  1144. data = s->mdimem[reg];
  1145. }
  1146. /* Emulation takes no time to finish MDI transaction.
  1147. * Set MDI bit in SCB status register. */
  1148. s->mem[SCBAck] |= 0x08;
  1149. val |= BIT(28);
  1150. if (raiseint) {
  1151. eepro100_mdi_interrupt(s);
  1152. }
  1153. }
  1154. val = (val & 0xffff0000) + data;
  1155. e100_write_reg4(s, SCBCtrlMDI, val);
  1156. }
  1157. /*****************************************************************************
  1158. *
  1159. * Port emulation.
  1160. *
  1161. ****************************************************************************/
  1162. #define PORT_SOFTWARE_RESET 0
  1163. #define PORT_SELFTEST 1
  1164. #define PORT_SELECTIVE_RESET 2
  1165. #define PORT_DUMP 3
  1166. #define PORT_SELECTION_MASK 3
  1167. typedef struct {
  1168. uint32_t st_sign; /* Self Test Signature */
  1169. uint32_t st_result; /* Self Test Results */
  1170. } eepro100_selftest_t;
  1171. static uint32_t eepro100_read_port(EEPRO100State * s)
  1172. {
  1173. return 0;
  1174. }
  1175. static void eepro100_write_port(EEPRO100State *s)
  1176. {
  1177. uint32_t val = e100_read_reg4(s, SCBPort);
  1178. uint32_t address = (val & ~PORT_SELECTION_MASK);
  1179. uint8_t selection = (val & PORT_SELECTION_MASK);
  1180. switch (selection) {
  1181. case PORT_SOFTWARE_RESET:
  1182. nic_reset(s);
  1183. break;
  1184. case PORT_SELFTEST:
  1185. TRACE(OTHER, logout("selftest address=0x%08x\n", address));
  1186. eepro100_selftest_t data;
  1187. pci_dma_read(&s->dev, address, (uint8_t *) &data, sizeof(data));
  1188. data.st_sign = 0xffffffff;
  1189. data.st_result = 0;
  1190. pci_dma_write(&s->dev, address, (uint8_t *) &data, sizeof(data));
  1191. break;
  1192. case PORT_SELECTIVE_RESET:
  1193. TRACE(OTHER, logout("selective reset, selftest address=0x%08x\n", address));
  1194. nic_selective_reset(s);
  1195. break;
  1196. default:
  1197. logout("val=0x%08x\n", val);
  1198. missing("unknown port selection");
  1199. }
  1200. }
  1201. /*****************************************************************************
  1202. *
  1203. * General hardware emulation.
  1204. *
  1205. ****************************************************************************/
  1206. static uint8_t eepro100_read1(EEPRO100State * s, uint32_t addr)
  1207. {
  1208. uint8_t val = 0;
  1209. if (addr <= sizeof(s->mem) - sizeof(val)) {
  1210. val = s->mem[addr];
  1211. }
  1212. switch (addr) {
  1213. case SCBStatus:
  1214. case SCBAck:
  1215. TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
  1216. break;
  1217. case SCBCmd:
  1218. TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
  1219. #if 0
  1220. val = eepro100_read_command(s);
  1221. #endif
  1222. break;
  1223. case SCBIntmask:
  1224. TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
  1225. break;
  1226. case SCBPort + 3:
  1227. TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
  1228. break;
  1229. case SCBeeprom:
  1230. val = eepro100_read_eeprom(s);
  1231. break;
  1232. case SCBCtrlMDI:
  1233. case SCBCtrlMDI + 1:
  1234. case SCBCtrlMDI + 2:
  1235. case SCBCtrlMDI + 3:
  1236. val = (uint8_t)(eepro100_read_mdi(s) >> (8 * (addr & 3)));
  1237. TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
  1238. break;
  1239. case SCBpmdr: /* Power Management Driver Register */
  1240. val = 0;
  1241. TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
  1242. break;
  1243. case SCBgctrl: /* General Control Register */
  1244. TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
  1245. break;
  1246. case SCBgstat: /* General Status Register */
  1247. /* 100 Mbps full duplex, valid link */
  1248. val = 0x07;
  1249. TRACE(OTHER, logout("addr=General Status val=%02x\n", val));
  1250. break;
  1251. default:
  1252. logout("addr=%s val=0x%02x\n", regname(addr), val);
  1253. missing("unknown byte read");
  1254. }
  1255. return val;
  1256. }
  1257. static uint16_t eepro100_read2(EEPRO100State * s, uint32_t addr)
  1258. {
  1259. uint16_t val = 0;
  1260. if (addr <= sizeof(s->mem) - sizeof(val)) {
  1261. val = e100_read_reg2(s, addr);
  1262. }
  1263. switch (addr) {
  1264. case SCBStatus:
  1265. case SCBCmd:
  1266. TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
  1267. break;
  1268. case SCBeeprom:
  1269. val = eepro100_read_eeprom(s);
  1270. TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
  1271. break;
  1272. case SCBCtrlMDI:
  1273. case SCBCtrlMDI + 2:
  1274. val = (uint16_t)(eepro100_read_mdi(s) >> (8 * (addr & 3)));
  1275. TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
  1276. break;
  1277. default:
  1278. logout("addr=%s val=0x%04x\n", regname(addr), val);
  1279. missing("unknown word read");
  1280. }
  1281. return val;
  1282. }
  1283. static uint32_t eepro100_read4(EEPRO100State * s, uint32_t addr)
  1284. {
  1285. uint32_t val = 0;
  1286. if (addr <= sizeof(s->mem) - sizeof(val)) {
  1287. val = e100_read_reg4(s, addr);
  1288. }
  1289. switch (addr) {
  1290. case SCBStatus:
  1291. TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
  1292. break;
  1293. case SCBPointer:
  1294. TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
  1295. break;
  1296. case SCBPort:
  1297. val = eepro100_read_port(s);
  1298. TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
  1299. break;
  1300. case SCBflash:
  1301. val = eepro100_read_eeprom(s);
  1302. TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
  1303. break;
  1304. case SCBCtrlMDI:
  1305. val = eepro100_read_mdi(s);
  1306. break;
  1307. default:
  1308. logout("addr=%s val=0x%08x\n", regname(addr), val);
  1309. missing("unknown longword read");
  1310. }
  1311. return val;
  1312. }
  1313. static void eepro100_write1(EEPRO100State * s, uint32_t addr, uint8_t val)
  1314. {
  1315. /* SCBStatus is readonly. */
  1316. if (addr > SCBStatus && addr <= sizeof(s->mem) - sizeof(val)) {
  1317. s->mem[addr] = val;
  1318. }
  1319. switch (addr) {
  1320. case SCBStatus:
  1321. TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
  1322. break;
  1323. case SCBAck:
  1324. TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
  1325. eepro100_acknowledge(s);
  1326. break;
  1327. case SCBCmd:
  1328. TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
  1329. eepro100_write_command(s, val);
  1330. break;
  1331. case SCBIntmask:
  1332. TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
  1333. if (val & BIT(1)) {
  1334. eepro100_swi_interrupt(s);
  1335. }
  1336. eepro100_interrupt(s, 0);
  1337. break;
  1338. case SCBPointer:
  1339. case SCBPointer + 1:
  1340. case SCBPointer + 2:
  1341. case SCBPointer + 3:
  1342. TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
  1343. break;
  1344. case SCBPort:
  1345. case SCBPort + 1:
  1346. case SCBPort + 2:
  1347. TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
  1348. break;
  1349. case SCBPort + 3:
  1350. TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
  1351. eepro100_write_port(s);
  1352. break;
  1353. case SCBFlow: /* does not exist on 82557 */
  1354. case SCBFlow + 1:
  1355. case SCBFlow + 2:
  1356. case SCBpmdr: /* does not exist on 82557 */
  1357. TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
  1358. break;
  1359. case SCBeeprom:
  1360. TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
  1361. eepro100_write_eeprom(s->eeprom, val);
  1362. break;
  1363. case SCBCtrlMDI:
  1364. case SCBCtrlMDI + 1:
  1365. case SCBCtrlMDI + 2:
  1366. TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
  1367. break;
  1368. case SCBCtrlMDI + 3:
  1369. TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
  1370. eepro100_write_mdi(s);
  1371. break;
  1372. default:
  1373. logout("addr=%s val=0x%02x\n", regname(addr), val);
  1374. missing("unknown byte write");
  1375. }
  1376. }
  1377. static void eepro100_write2(EEPRO100State * s, uint32_t addr, uint16_t val)
  1378. {
  1379. /* SCBStatus is readonly. */
  1380. if (addr > SCBStatus && addr <= sizeof(s->mem) - sizeof(val)) {
  1381. e100_write_reg2(s, addr, val);
  1382. }
  1383. switch (addr) {
  1384. case SCBStatus:
  1385. TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
  1386. s->mem[SCBAck] = (val >> 8);
  1387. eepro100_acknowledge(s);
  1388. break;
  1389. case SCBCmd:
  1390. TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
  1391. eepro100_write_command(s, val);
  1392. eepro100_write1(s, SCBIntmask, val >> 8);
  1393. break;
  1394. case SCBPointer:
  1395. case SCBPointer + 2:
  1396. TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
  1397. break;
  1398. case SCBPort:
  1399. TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
  1400. break;
  1401. case SCBPort + 2:
  1402. TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
  1403. eepro100_write_port(s);
  1404. break;
  1405. case SCBeeprom:
  1406. TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
  1407. eepro100_write_eeprom(s->eeprom, val);
  1408. break;
  1409. case SCBCtrlMDI:
  1410. TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
  1411. break;
  1412. case SCBCtrlMDI + 2:
  1413. TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
  1414. eepro100_write_mdi(s);
  1415. break;
  1416. default:
  1417. logout("addr=%s val=0x%04x\n", regname(addr), val);
  1418. missing("unknown word write");
  1419. }
  1420. }
  1421. static void eepro100_write4(EEPRO100State * s, uint32_t addr, uint32_t val)
  1422. {
  1423. if (addr <= sizeof(s->mem) - sizeof(val)) {
  1424. e100_write_reg4(s, addr, val);
  1425. }
  1426. switch (addr) {
  1427. case SCBPointer:
  1428. TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
  1429. break;
  1430. case SCBPort:
  1431. TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
  1432. eepro100_write_port(s);
  1433. break;
  1434. case SCBflash:
  1435. TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
  1436. val = val >> 16;
  1437. eepro100_write_eeprom(s->eeprom, val);
  1438. break;
  1439. case SCBCtrlMDI:
  1440. TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
  1441. eepro100_write_mdi(s);
  1442. break;
  1443. default:
  1444. logout("addr=%s val=0x%08x\n", regname(addr), val);
  1445. missing("unknown longword write");
  1446. }
  1447. }
  1448. static uint64_t eepro100_read(void *opaque, hwaddr addr,
  1449. unsigned size)
  1450. {
  1451. EEPRO100State *s = opaque;
  1452. switch (size) {
  1453. case 1: return eepro100_read1(s, addr);
  1454. case 2: return eepro100_read2(s, addr);
  1455. case 4: return eepro100_read4(s, addr);
  1456. default: abort();
  1457. }
  1458. }
  1459. static void eepro100_write(void *opaque, hwaddr addr,
  1460. uint64_t data, unsigned size)
  1461. {
  1462. EEPRO100State *s = opaque;
  1463. switch (size) {
  1464. case 1:
  1465. eepro100_write1(s, addr, data);
  1466. break;
  1467. case 2:
  1468. eepro100_write2(s, addr, data);
  1469. break;
  1470. case 4:
  1471. eepro100_write4(s, addr, data);
  1472. break;
  1473. default:
  1474. abort();
  1475. }
  1476. }
  1477. static const MemoryRegionOps eepro100_ops = {
  1478. .read = eepro100_read,
  1479. .write = eepro100_write,
  1480. .endianness = DEVICE_LITTLE_ENDIAN,
  1481. };
  1482. static ssize_t nic_receive(NetClientState *nc, const uint8_t * buf, size_t size)
  1483. {
  1484. /* TODO:
  1485. * - Magic packets should set bit 30 in power management driver register.
  1486. * - Interesting packets should set bit 29 in power management driver register.
  1487. */
  1488. EEPRO100State *s = qemu_get_nic_opaque(nc);
  1489. uint16_t rfd_status = 0xa000;
  1490. #if defined(CONFIG_PAD_RECEIVED_FRAMES)
  1491. uint8_t min_buf[60];
  1492. #endif
  1493. static const uint8_t broadcast_macaddr[6] =
  1494. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  1495. #if defined(CONFIG_PAD_RECEIVED_FRAMES)
  1496. /* Pad to minimum Ethernet frame length */
  1497. if (size < sizeof(min_buf)) {
  1498. memcpy(min_buf, buf, size);
  1499. memset(&min_buf[size], 0, sizeof(min_buf) - size);
  1500. buf = min_buf;
  1501. size = sizeof(min_buf);
  1502. }
  1503. #endif
  1504. if (s->configuration[8] & 0x80) {
  1505. /* CSMA is disabled. */
  1506. logout("%p received while CSMA is disabled\n", s);
  1507. return -1;
  1508. #if !defined(CONFIG_PAD_RECEIVED_FRAMES)
  1509. } else if (size < 64 && (s->configuration[7] & BIT(0))) {
  1510. /* Short frame and configuration byte 7/0 (discard short receive) set:
  1511. * Short frame is discarded */
  1512. logout("%p received short frame (%zu byte)\n", s, size);
  1513. s->statistics.rx_short_frame_errors++;
  1514. return -1;
  1515. #endif
  1516. } else if ((size > MAX_ETH_FRAME_SIZE + 4) && !(s->configuration[18] & BIT(3))) {
  1517. /* Long frame and configuration byte 18/3 (long receive ok) not set:
  1518. * Long frames are discarded. */
  1519. logout("%p received long frame (%zu byte), ignored\n", s, size);
  1520. return -1;
  1521. } else if (memcmp(buf, s->conf.macaddr.a, 6) == 0) { /* !!! */
  1522. /* Frame matches individual address. */
  1523. /* TODO: check configuration byte 15/4 (ignore U/L). */
  1524. TRACE(RXTX, logout("%p received frame for me, len=%zu\n", s, size));
  1525. } else if (memcmp(buf, broadcast_macaddr, 6) == 0) {
  1526. /* Broadcast frame. */
  1527. TRACE(RXTX, logout("%p received broadcast, len=%zu\n", s, size));
  1528. rfd_status |= 0x0002;
  1529. } else if (buf[0] & 0x01) {
  1530. /* Multicast frame. */
  1531. TRACE(RXTX, logout("%p received multicast, len=%zu,%s\n", s, size, nic_dump(buf, size)));
  1532. if (s->configuration[21] & BIT(3)) {
  1533. /* Multicast all bit is set, receive all multicast frames. */
  1534. } else {
  1535. unsigned mcast_idx = (net_crc32(buf, ETH_ALEN) & BITS(7, 2)) >> 2;
  1536. assert(mcast_idx < 64);
  1537. if (s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7))) {
  1538. /* Multicast frame is allowed in hash table. */
  1539. } else if (s->configuration[15] & BIT(0)) {
  1540. /* Promiscuous: receive all. */
  1541. rfd_status |= 0x0004;
  1542. } else {
  1543. TRACE(RXTX, logout("%p multicast ignored\n", s));
  1544. return -1;
  1545. }
  1546. }
  1547. /* TODO: Next not for promiscuous mode? */
  1548. rfd_status |= 0x0002;
  1549. } else if (s->configuration[15] & BIT(0)) {
  1550. /* Promiscuous: receive all. */
  1551. TRACE(RXTX, logout("%p received frame in promiscuous mode, len=%zu\n", s, size));
  1552. rfd_status |= 0x0004;
  1553. } else if (s->configuration[20] & BIT(6)) {
  1554. /* Multiple IA bit set. */
  1555. unsigned mcast_idx = net_crc32(buf, ETH_ALEN) >> 26;
  1556. assert(mcast_idx < 64);
  1557. if (s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7))) {
  1558. TRACE(RXTX, logout("%p accepted, multiple IA bit set\n", s));
  1559. } else {
  1560. TRACE(RXTX, logout("%p frame ignored, multiple IA bit set\n", s));
  1561. return -1;
  1562. }
  1563. } else {
  1564. TRACE(RXTX, logout("%p received frame, ignored, len=%zu,%s\n", s, size,
  1565. nic_dump(buf, size)));
  1566. return size;
  1567. }
  1568. if (get_ru_state(s) != ru_ready) {
  1569. /* No resources available. */
  1570. logout("no resources, state=%u\n", get_ru_state(s));
  1571. /* TODO: RNR interrupt only at first failed frame? */
  1572. eepro100_rnr_interrupt(s);
  1573. s->statistics.rx_resource_errors++;
  1574. #if 0
  1575. assert(!"no resources");
  1576. #endif
  1577. return -1;
  1578. }
  1579. /* !!! */
  1580. eepro100_rx_t rx;
  1581. pci_dma_read(&s->dev, s->ru_base + s->ru_offset,
  1582. &rx, sizeof(eepro100_rx_t));
  1583. uint16_t rfd_command = le16_to_cpu(rx.command);
  1584. uint16_t rfd_size = le16_to_cpu(rx.size);
  1585. if (size > rfd_size) {
  1586. logout("Receive buffer (%" PRId16 " bytes) too small for data "
  1587. "(%zu bytes); data truncated\n", rfd_size, size);
  1588. size = rfd_size;
  1589. }
  1590. #if !defined(CONFIG_PAD_RECEIVED_FRAMES)
  1591. if (size < 64) {
  1592. rfd_status |= 0x0080;
  1593. }
  1594. #endif
  1595. TRACE(OTHER, logout("command 0x%04x, link 0x%08x, addr 0x%08x, size %u\n",
  1596. rfd_command, rx.link, rx.rx_buf_addr, rfd_size));
  1597. stw_le_pci_dma(&s->dev, s->ru_base + s->ru_offset +
  1598. offsetof(eepro100_rx_t, status), rfd_status);
  1599. stw_le_pci_dma(&s->dev, s->ru_base + s->ru_offset +
  1600. offsetof(eepro100_rx_t, count), size);
  1601. /* Early receive interrupt not supported. */
  1602. #if 0
  1603. eepro100_er_interrupt(s);
  1604. #endif
  1605. /* Receive CRC Transfer not supported. */
  1606. if (s->configuration[18] & BIT(2)) {
  1607. missing("Receive CRC Transfer");
  1608. return -1;
  1609. }
  1610. /* TODO: check stripping enable bit. */
  1611. #if 0
  1612. assert(!(s->configuration[17] & BIT(0)));
  1613. #endif
  1614. pci_dma_write(&s->dev, s->ru_base + s->ru_offset +
  1615. sizeof(eepro100_rx_t), buf, size);
  1616. s->statistics.rx_good_frames++;
  1617. eepro100_fr_interrupt(s);
  1618. s->ru_offset = le32_to_cpu(rx.link);
  1619. if (rfd_command & COMMAND_EL) {
  1620. /* EL bit is set, so this was the last frame. */
  1621. logout("receive: Running out of frames\n");
  1622. set_ru_state(s, ru_no_resources);
  1623. eepro100_rnr_interrupt(s);
  1624. }
  1625. if (rfd_command & COMMAND_S) {
  1626. /* S bit is set. */
  1627. set_ru_state(s, ru_suspended);
  1628. }
  1629. return size;
  1630. }
  1631. static const VMStateDescription vmstate_eepro100 = {
  1632. .version_id = 3,
  1633. .minimum_version_id = 2,
  1634. .fields = (VMStateField[]) {
  1635. VMSTATE_PCI_DEVICE(dev, EEPRO100State),
  1636. VMSTATE_UNUSED(32),
  1637. VMSTATE_BUFFER(mult, EEPRO100State),
  1638. VMSTATE_BUFFER(mem, EEPRO100State),
  1639. /* Save all members of struct between scb_stat and mem. */
  1640. VMSTATE_UINT8(scb_stat, EEPRO100State),
  1641. VMSTATE_UINT8(int_stat, EEPRO100State),
  1642. VMSTATE_UNUSED(3*4),
  1643. VMSTATE_MACADDR(conf.macaddr, EEPRO100State),
  1644. VMSTATE_UNUSED(19*4),
  1645. VMSTATE_UINT16_ARRAY(mdimem, EEPRO100State, 32),
  1646. /* The eeprom should be saved and restored by its own routines. */
  1647. VMSTATE_UINT32(device, EEPRO100State),
  1648. /* TODO check device. */
  1649. VMSTATE_UINT32(cu_base, EEPRO100State),
  1650. VMSTATE_UINT32(cu_offset, EEPRO100State),
  1651. VMSTATE_UINT32(ru_base, EEPRO100State),
  1652. VMSTATE_UINT32(ru_offset, EEPRO100State),
  1653. VMSTATE_UINT32(statsaddr, EEPRO100State),
  1654. /* Save eepro100_stats_t statistics. */
  1655. VMSTATE_UINT32(statistics.tx_good_frames, EEPRO100State),
  1656. VMSTATE_UINT32(statistics.tx_max_collisions, EEPRO100State),
  1657. VMSTATE_UINT32(statistics.tx_late_collisions, EEPRO100State),
  1658. VMSTATE_UINT32(statistics.tx_underruns, EEPRO100State),
  1659. VMSTATE_UINT32(statistics.tx_lost_crs, EEPRO100State),
  1660. VMSTATE_UINT32(statistics.tx_deferred, EEPRO100State),
  1661. VMSTATE_UINT32(statistics.tx_single_collisions, EEPRO100State),
  1662. VMSTATE_UINT32(statistics.tx_multiple_collisions, EEPRO100State),
  1663. VMSTATE_UINT32(statistics.tx_total_collisions, EEPRO100State),
  1664. VMSTATE_UINT32(statistics.rx_good_frames, EEPRO100State),
  1665. VMSTATE_UINT32(statistics.rx_crc_errors, EEPRO100State),
  1666. VMSTATE_UINT32(statistics.rx_alignment_errors, EEPRO100State),
  1667. VMSTATE_UINT32(statistics.rx_resource_errors, EEPRO100State),
  1668. VMSTATE_UINT32(statistics.rx_overrun_errors, EEPRO100State),
  1669. VMSTATE_UINT32(statistics.rx_cdt_errors, EEPRO100State),
  1670. VMSTATE_UINT32(statistics.rx_short_frame_errors, EEPRO100State),
  1671. VMSTATE_UINT32(statistics.fc_xmt_pause, EEPRO100State),
  1672. VMSTATE_UINT32(statistics.fc_rcv_pause, EEPRO100State),
  1673. VMSTATE_UINT32(statistics.fc_rcv_unsupported, EEPRO100State),
  1674. VMSTATE_UINT16(statistics.xmt_tco_frames, EEPRO100State),
  1675. VMSTATE_UINT16(statistics.rcv_tco_frames, EEPRO100State),
  1676. /* Configuration bytes. */
  1677. VMSTATE_BUFFER(configuration, EEPRO100State),
  1678. VMSTATE_END_OF_LIST()
  1679. }
  1680. };
  1681. static void pci_nic_uninit(PCIDevice *pci_dev)
  1682. {
  1683. EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
  1684. vmstate_unregister(&pci_dev->qdev, s->vmstate, s);
  1685. g_free(s->vmstate);
  1686. eeprom93xx_free(&pci_dev->qdev, s->eeprom);
  1687. qemu_del_nic(s->nic);
  1688. }
  1689. static NetClientInfo net_eepro100_info = {
  1690. .type = NET_CLIENT_DRIVER_NIC,
  1691. .size = sizeof(NICState),
  1692. .receive = nic_receive,
  1693. };
  1694. static void e100_nic_realize(PCIDevice *pci_dev, Error **errp)
  1695. {
  1696. EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
  1697. E100PCIDeviceInfo *info = eepro100_get_class(s);
  1698. Error *local_err = NULL;
  1699. TRACE(OTHER, logout("\n"));
  1700. s->device = info->device;
  1701. e100_pci_reset(s, &local_err);
  1702. if (local_err) {
  1703. error_propagate(errp, local_err);
  1704. return;
  1705. }
  1706. /* Add 64 * 2 EEPROM. i82557 and i82558 support a 64 word EEPROM,
  1707. * i82559 and later support 64 or 256 word EEPROM. */
  1708. s->eeprom = eeprom93xx_new(&pci_dev->qdev, EEPROM_SIZE);
  1709. /* Handler for memory-mapped I/O */
  1710. memory_region_init_io(&s->mmio_bar, OBJECT(s), &eepro100_ops, s,
  1711. "eepro100-mmio", PCI_MEM_SIZE);
  1712. pci_register_bar(&s->dev, 0, PCI_BASE_ADDRESS_MEM_PREFETCH, &s->mmio_bar);
  1713. memory_region_init_io(&s->io_bar, OBJECT(s), &eepro100_ops, s,
  1714. "eepro100-io", PCI_IO_SIZE);
  1715. pci_register_bar(&s->dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar);
  1716. /* FIXME: flash aliases to mmio?! */
  1717. memory_region_init_io(&s->flash_bar, OBJECT(s), &eepro100_ops, s,
  1718. "eepro100-flash", PCI_FLASH_SIZE);
  1719. pci_register_bar(&s->dev, 2, 0, &s->flash_bar);
  1720. qemu_macaddr_default_if_unset(&s->conf.macaddr);
  1721. logout("macaddr: %s\n", nic_dump(&s->conf.macaddr.a[0], 6));
  1722. nic_reset(s);
  1723. s->nic = qemu_new_nic(&net_eepro100_info, &s->conf,
  1724. object_get_typename(OBJECT(pci_dev)), pci_dev->qdev.id, s);
  1725. qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
  1726. TRACE(OTHER, logout("%s\n", qemu_get_queue(s->nic)->info_str));
  1727. qemu_register_reset(nic_reset, s);
  1728. s->vmstate = g_memdup(&vmstate_eepro100, sizeof(vmstate_eepro100));
  1729. s->vmstate->name = qemu_get_queue(s->nic)->model;
  1730. vmstate_register(&pci_dev->qdev, -1, s->vmstate, s);
  1731. }
  1732. static void eepro100_instance_init(Object *obj)
  1733. {
  1734. EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, PCI_DEVICE(obj));
  1735. device_add_bootindex_property(obj, &s->conf.bootindex,
  1736. "bootindex", "/ethernet-phy@0",
  1737. DEVICE(s), NULL);
  1738. }
  1739. static E100PCIDeviceInfo e100_devices[] = {
  1740. {
  1741. .name = "i82550",
  1742. .desc = "Intel i82550 Ethernet",
  1743. .device = i82550,
  1744. /* TODO: check device id. */
  1745. .device_id = PCI_DEVICE_ID_INTEL_82551IT,
  1746. /* Revision ID: 0x0c, 0x0d, 0x0e. */
  1747. .revision = 0x0e,
  1748. /* TODO: check size of statistical counters. */
  1749. .stats_size = 80,
  1750. /* TODO: check extended tcb support. */
  1751. .has_extended_tcb_support = true,
  1752. .power_management = true,
  1753. },{
  1754. .name = "i82551",
  1755. .desc = "Intel i82551 Ethernet",
  1756. .device = i82551,
  1757. .device_id = PCI_DEVICE_ID_INTEL_82551IT,
  1758. /* Revision ID: 0x0f, 0x10. */
  1759. .revision = 0x0f,
  1760. /* TODO: check size of statistical counters. */
  1761. .stats_size = 80,
  1762. .has_extended_tcb_support = true,
  1763. .power_management = true,
  1764. },{
  1765. .name = "i82557a",
  1766. .desc = "Intel i82557A Ethernet",
  1767. .device = i82557A,
  1768. .device_id = PCI_DEVICE_ID_INTEL_82557,
  1769. .revision = 0x01,
  1770. .power_management = false,
  1771. },{
  1772. .name = "i82557b",
  1773. .desc = "Intel i82557B Ethernet",
  1774. .device = i82557B,
  1775. .device_id = PCI_DEVICE_ID_INTEL_82557,
  1776. .revision = 0x02,
  1777. .power_management = false,
  1778. },{
  1779. .name = "i82557c",
  1780. .desc = "Intel i82557C Ethernet",
  1781. .device = i82557C,
  1782. .device_id = PCI_DEVICE_ID_INTEL_82557,
  1783. .revision = 0x03,
  1784. .power_management = false,
  1785. },{
  1786. .name = "i82558a",
  1787. .desc = "Intel i82558A Ethernet",
  1788. .device = i82558A,
  1789. .device_id = PCI_DEVICE_ID_INTEL_82557,
  1790. .revision = 0x04,
  1791. .stats_size = 76,
  1792. .has_extended_tcb_support = true,
  1793. .power_management = true,
  1794. },{
  1795. .name = "i82558b",
  1796. .desc = "Intel i82558B Ethernet",
  1797. .device = i82558B,
  1798. .device_id = PCI_DEVICE_ID_INTEL_82557,
  1799. .revision = 0x05,
  1800. .stats_size = 76,
  1801. .has_extended_tcb_support = true,
  1802. .power_management = true,
  1803. },{
  1804. .name = "i82559a",
  1805. .desc = "Intel i82559A Ethernet",
  1806. .device = i82559A,
  1807. .device_id = PCI_DEVICE_ID_INTEL_82557,
  1808. .revision = 0x06,
  1809. .stats_size = 80,
  1810. .has_extended_tcb_support = true,
  1811. .power_management = true,
  1812. },{
  1813. .name = "i82559b",
  1814. .desc = "Intel i82559B Ethernet",
  1815. .device = i82559B,
  1816. .device_id = PCI_DEVICE_ID_INTEL_82557,
  1817. .revision = 0x07,
  1818. .stats_size = 80,
  1819. .has_extended_tcb_support = true,
  1820. .power_management = true,
  1821. },{
  1822. .name = "i82559c",
  1823. .desc = "Intel i82559C Ethernet",
  1824. .device = i82559C,
  1825. .device_id = PCI_DEVICE_ID_INTEL_82557,
  1826. #if 0
  1827. .revision = 0x08,
  1828. #endif
  1829. /* TODO: Windows wants revision id 0x0c. */
  1830. .revision = 0x0c,
  1831. #if EEPROM_SIZE > 0
  1832. .subsystem_vendor_id = PCI_VENDOR_ID_INTEL,
  1833. .subsystem_id = 0x0040,
  1834. #endif
  1835. .stats_size = 80,
  1836. .has_extended_tcb_support = true,
  1837. .power_management = true,
  1838. },{
  1839. .name = "i82559er",
  1840. .desc = "Intel i82559ER Ethernet",
  1841. .device = i82559ER,
  1842. .device_id = PCI_DEVICE_ID_INTEL_82551IT,
  1843. .revision = 0x09,
  1844. .stats_size = 80,
  1845. .has_extended_tcb_support = true,
  1846. .power_management = true,
  1847. },{
  1848. .name = "i82562",
  1849. .desc = "Intel i82562 Ethernet",
  1850. .device = i82562,
  1851. /* TODO: check device id. */
  1852. .device_id = PCI_DEVICE_ID_INTEL_82551IT,
  1853. /* TODO: wrong revision id. */
  1854. .revision = 0x0e,
  1855. .stats_size = 80,
  1856. .has_extended_tcb_support = true,
  1857. .power_management = true,
  1858. },{
  1859. /* Toshiba Tecra 8200. */
  1860. .name = "i82801",
  1861. .desc = "Intel i82801 Ethernet",
  1862. .device = i82801,
  1863. .device_id = 0x2449,
  1864. .revision = 0x03,
  1865. .stats_size = 80,
  1866. .has_extended_tcb_support = true,
  1867. .power_management = true,
  1868. }
  1869. };
  1870. static E100PCIDeviceInfo *eepro100_get_class_by_name(const char *typename)
  1871. {
  1872. E100PCIDeviceInfo *info = NULL;
  1873. int i;
  1874. /* This is admittedly awkward but also temporary. QOM allows for
  1875. * parameterized typing and for subclassing both of which would suitable
  1876. * handle what's going on here. But class_data is already being used as
  1877. * a stop-gap hack to allow incremental qdev conversion so we cannot use it
  1878. * right now. Once we merge the final QOM series, we can come back here and
  1879. * do this in a much more elegant fashion.
  1880. */
  1881. for (i = 0; i < ARRAY_SIZE(e100_devices); i++) {
  1882. if (strcmp(e100_devices[i].name, typename) == 0) {
  1883. info = &e100_devices[i];
  1884. break;
  1885. }
  1886. }
  1887. assert(info != NULL);
  1888. return info;
  1889. }
  1890. static E100PCIDeviceInfo *eepro100_get_class(EEPRO100State *s)
  1891. {
  1892. return eepro100_get_class_by_name(object_get_typename(OBJECT(s)));
  1893. }
  1894. static Property e100_properties[] = {
  1895. DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
  1896. DEFINE_PROP_END_OF_LIST(),
  1897. };
  1898. static void eepro100_class_init(ObjectClass *klass, void *data)
  1899. {
  1900. DeviceClass *dc = DEVICE_CLASS(klass);
  1901. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  1902. E100PCIDeviceInfo *info;
  1903. info = eepro100_get_class_by_name(object_class_get_name(klass));
  1904. set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
  1905. dc->props = e100_properties;
  1906. dc->desc = info->desc;
  1907. k->vendor_id = PCI_VENDOR_ID_INTEL;
  1908. k->class_id = PCI_CLASS_NETWORK_ETHERNET;
  1909. k->romfile = "pxe-eepro100.rom";
  1910. k->realize = e100_nic_realize;
  1911. k->exit = pci_nic_uninit;
  1912. k->device_id = info->device_id;
  1913. k->revision = info->revision;
  1914. k->subsystem_vendor_id = info->subsystem_vendor_id;
  1915. k->subsystem_id = info->subsystem_id;
  1916. }
  1917. static void eepro100_register_types(void)
  1918. {
  1919. size_t i;
  1920. for (i = 0; i < ARRAY_SIZE(e100_devices); i++) {
  1921. TypeInfo type_info = {};
  1922. E100PCIDeviceInfo *info = &e100_devices[i];
  1923. type_info.name = info->name;
  1924. type_info.parent = TYPE_PCI_DEVICE;
  1925. type_info.class_init = eepro100_class_init;
  1926. type_info.instance_size = sizeof(EEPRO100State);
  1927. type_info.instance_init = eepro100_instance_init;
  1928. type_info.interfaces = (InterfaceInfo[]) {
  1929. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  1930. { },
  1931. };
  1932. type_register(&type_info);
  1933. }
  1934. }
  1935. type_init(eepro100_register_types)