2
0

tulip.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. /*
  2. * QEMU TULIP Emulation
  3. *
  4. * Copyright (c) 2019 Sven Schnelle <svens@stackframe.org>
  5. *
  6. * This work is licensed under the GNU GPL license version 2 or later.
  7. */
  8. #include "qemu/osdep.h"
  9. #include "qemu/log.h"
  10. #include "hw/irq.h"
  11. #include "hw/pci/pci_device.h"
  12. #include "hw/qdev-properties.h"
  13. #include "hw/nvram/eeprom93xx.h"
  14. #include "migration/vmstate.h"
  15. #include "sysemu/sysemu.h"
  16. #include "tulip.h"
  17. #include "trace.h"
  18. #include "net/eth.h"
  19. struct TULIPState {
  20. PCIDevice dev;
  21. MemoryRegion io;
  22. MemoryRegion memory;
  23. NICConf c;
  24. qemu_irq irq;
  25. NICState *nic;
  26. eeprom_t *eeprom;
  27. uint32_t csr[16];
  28. /* state for MII */
  29. uint32_t old_csr9;
  30. uint32_t mii_word;
  31. uint32_t mii_bitcnt;
  32. hwaddr current_rx_desc;
  33. hwaddr current_tx_desc;
  34. uint8_t rx_frame[2048];
  35. uint8_t tx_frame[2048];
  36. uint16_t tx_frame_len;
  37. uint16_t rx_frame_len;
  38. uint16_t rx_frame_size;
  39. uint32_t rx_status;
  40. uint8_t filter[16][6];
  41. };
  42. static const VMStateDescription vmstate_pci_tulip = {
  43. .name = "tulip",
  44. .fields = (VMStateField[]) {
  45. VMSTATE_PCI_DEVICE(dev, TULIPState),
  46. VMSTATE_UINT32_ARRAY(csr, TULIPState, 16),
  47. VMSTATE_UINT32(old_csr9, TULIPState),
  48. VMSTATE_UINT32(mii_word, TULIPState),
  49. VMSTATE_UINT32(mii_bitcnt, TULIPState),
  50. VMSTATE_UINT64(current_rx_desc, TULIPState),
  51. VMSTATE_UINT64(current_tx_desc, TULIPState),
  52. VMSTATE_BUFFER(rx_frame, TULIPState),
  53. VMSTATE_BUFFER(tx_frame, TULIPState),
  54. VMSTATE_UINT16(rx_frame_len, TULIPState),
  55. VMSTATE_UINT16(tx_frame_len, TULIPState),
  56. VMSTATE_UINT16(rx_frame_size, TULIPState),
  57. VMSTATE_UINT32(rx_status, TULIPState),
  58. VMSTATE_UINT8_2DARRAY(filter, TULIPState, 16, 6),
  59. VMSTATE_END_OF_LIST()
  60. }
  61. };
  62. static void tulip_desc_read(TULIPState *s, hwaddr p,
  63. struct tulip_descriptor *desc)
  64. {
  65. const MemTxAttrs attrs = { .memory = true };
  66. if (s->csr[0] & CSR0_DBO) {
  67. ldl_be_pci_dma(&s->dev, p, &desc->status, attrs);
  68. ldl_be_pci_dma(&s->dev, p + 4, &desc->control, attrs);
  69. ldl_be_pci_dma(&s->dev, p + 8, &desc->buf_addr1, attrs);
  70. ldl_be_pci_dma(&s->dev, p + 12, &desc->buf_addr2, attrs);
  71. } else {
  72. ldl_le_pci_dma(&s->dev, p, &desc->status, attrs);
  73. ldl_le_pci_dma(&s->dev, p + 4, &desc->control, attrs);
  74. ldl_le_pci_dma(&s->dev, p + 8, &desc->buf_addr1, attrs);
  75. ldl_le_pci_dma(&s->dev, p + 12, &desc->buf_addr2, attrs);
  76. }
  77. }
  78. static void tulip_desc_write(TULIPState *s, hwaddr p,
  79. struct tulip_descriptor *desc)
  80. {
  81. const MemTxAttrs attrs = { .memory = true };
  82. if (s->csr[0] & CSR0_DBO) {
  83. stl_be_pci_dma(&s->dev, p, desc->status, attrs);
  84. stl_be_pci_dma(&s->dev, p + 4, desc->control, attrs);
  85. stl_be_pci_dma(&s->dev, p + 8, desc->buf_addr1, attrs);
  86. stl_be_pci_dma(&s->dev, p + 12, desc->buf_addr2, attrs);
  87. } else {
  88. stl_le_pci_dma(&s->dev, p, desc->status, attrs);
  89. stl_le_pci_dma(&s->dev, p + 4, desc->control, attrs);
  90. stl_le_pci_dma(&s->dev, p + 8, desc->buf_addr1, attrs);
  91. stl_le_pci_dma(&s->dev, p + 12, desc->buf_addr2, attrs);
  92. }
  93. }
  94. static void tulip_update_int(TULIPState *s)
  95. {
  96. uint32_t ie = s->csr[5] & s->csr[7];
  97. bool assert = false;
  98. s->csr[5] &= ~(CSR5_AIS | CSR5_NIS);
  99. if (ie & (CSR5_TI | CSR5_TU | CSR5_RI | CSR5_GTE | CSR5_ERI)) {
  100. s->csr[5] |= CSR5_NIS;
  101. }
  102. if (ie & (CSR5_LC | CSR5_GPI | CSR5_FBE | CSR5_LNF | CSR5_ETI | CSR5_RWT |
  103. CSR5_RPS | CSR5_RU | CSR5_UNF | CSR5_LNP_ANC | CSR5_TJT |
  104. CSR5_TPS)) {
  105. s->csr[5] |= CSR5_AIS;
  106. }
  107. assert = s->csr[5] & s->csr[7] & (CSR5_AIS | CSR5_NIS);
  108. trace_tulip_irq(s->csr[5], s->csr[7], assert ? "assert" : "deassert");
  109. qemu_set_irq(s->irq, assert);
  110. }
  111. static bool tulip_rx_stopped(TULIPState *s)
  112. {
  113. return ((s->csr[5] >> CSR5_RS_SHIFT) & CSR5_RS_MASK) == CSR5_RS_STOPPED;
  114. }
  115. static void tulip_dump_tx_descriptor(TULIPState *s,
  116. struct tulip_descriptor *desc)
  117. {
  118. trace_tulip_descriptor("TX ", s->current_tx_desc,
  119. desc->status, desc->control >> 22,
  120. desc->control & 0x7ff, (desc->control >> 11) & 0x7ff,
  121. desc->buf_addr1, desc->buf_addr2);
  122. }
  123. static void tulip_dump_rx_descriptor(TULIPState *s,
  124. struct tulip_descriptor *desc)
  125. {
  126. trace_tulip_descriptor("RX ", s->current_rx_desc,
  127. desc->status, desc->control >> 22,
  128. desc->control & 0x7ff, (desc->control >> 11) & 0x7ff,
  129. desc->buf_addr1, desc->buf_addr2);
  130. }
  131. static void tulip_next_rx_descriptor(TULIPState *s,
  132. struct tulip_descriptor *desc)
  133. {
  134. if (desc->control & RDES1_RER) {
  135. s->current_rx_desc = s->csr[3];
  136. } else if (desc->control & RDES1_RCH) {
  137. s->current_rx_desc = desc->buf_addr2;
  138. } else {
  139. s->current_rx_desc += sizeof(struct tulip_descriptor) +
  140. (((s->csr[0] >> CSR0_DSL_SHIFT) & CSR0_DSL_MASK) << 2);
  141. }
  142. s->current_rx_desc &= ~3ULL;
  143. }
  144. static void tulip_copy_rx_bytes(TULIPState *s, struct tulip_descriptor *desc)
  145. {
  146. int len1 = (desc->control >> RDES1_BUF1_SIZE_SHIFT) & RDES1_BUF1_SIZE_MASK;
  147. int len2 = (desc->control >> RDES1_BUF2_SIZE_SHIFT) & RDES1_BUF2_SIZE_MASK;
  148. int len;
  149. if (s->rx_frame_len && len1) {
  150. if (s->rx_frame_len > len1) {
  151. len = len1;
  152. } else {
  153. len = s->rx_frame_len;
  154. }
  155. pci_dma_write(&s->dev, desc->buf_addr1, s->rx_frame +
  156. (s->rx_frame_size - s->rx_frame_len), len);
  157. s->rx_frame_len -= len;
  158. }
  159. if (s->rx_frame_len && len2) {
  160. if (s->rx_frame_len > len2) {
  161. len = len2;
  162. } else {
  163. len = s->rx_frame_len;
  164. }
  165. pci_dma_write(&s->dev, desc->buf_addr2, s->rx_frame +
  166. (s->rx_frame_size - s->rx_frame_len), len);
  167. s->rx_frame_len -= len;
  168. }
  169. }
  170. static bool tulip_filter_address(TULIPState *s, const uint8_t *addr)
  171. {
  172. static const char broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  173. bool ret = false;
  174. int i;
  175. for (i = 0; i < 16 && ret == false; i++) {
  176. if (!memcmp(&s->filter[i], addr, ETH_ALEN)) {
  177. ret = true;
  178. }
  179. }
  180. if (!memcmp(addr, broadcast, ETH_ALEN)) {
  181. return true;
  182. }
  183. if (s->csr[6] & (CSR6_PR | CSR6_RA)) {
  184. /* Promiscuous mode enabled */
  185. s->rx_status |= RDES0_FF;
  186. return true;
  187. }
  188. if ((s->csr[6] & CSR6_PM) && (addr[0] & 1)) {
  189. /* Pass all Multicast enabled */
  190. s->rx_status |= RDES0_MF;
  191. return true;
  192. }
  193. if (s->csr[6] & CSR6_IF) {
  194. ret ^= true;
  195. }
  196. return ret;
  197. }
  198. static ssize_t tulip_receive(TULIPState *s, const uint8_t *buf, size_t size)
  199. {
  200. struct tulip_descriptor desc;
  201. trace_tulip_receive(buf, size);
  202. if (size < 14 || size > sizeof(s->rx_frame) - 4
  203. || s->rx_frame_len || tulip_rx_stopped(s)) {
  204. return 0;
  205. }
  206. if (!tulip_filter_address(s, buf)) {
  207. return size;
  208. }
  209. do {
  210. tulip_desc_read(s, s->current_rx_desc, &desc);
  211. tulip_dump_rx_descriptor(s, &desc);
  212. if (!(desc.status & RDES0_OWN)) {
  213. s->csr[5] |= CSR5_RU;
  214. tulip_update_int(s);
  215. return s->rx_frame_size - s->rx_frame_len;
  216. }
  217. desc.status = 0;
  218. if (!s->rx_frame_len) {
  219. s->rx_frame_size = size + 4;
  220. s->rx_status = RDES0_LS |
  221. ((s->rx_frame_size & RDES0_FL_MASK) << RDES0_FL_SHIFT);
  222. desc.status |= RDES0_FS;
  223. memcpy(s->rx_frame, buf, size);
  224. s->rx_frame_len = s->rx_frame_size;
  225. }
  226. tulip_copy_rx_bytes(s, &desc);
  227. if (!s->rx_frame_len) {
  228. desc.status |= s->rx_status;
  229. s->csr[5] |= CSR5_RI;
  230. tulip_update_int(s);
  231. }
  232. tulip_dump_rx_descriptor(s, &desc);
  233. tulip_desc_write(s, s->current_rx_desc, &desc);
  234. tulip_next_rx_descriptor(s, &desc);
  235. } while (s->rx_frame_len);
  236. return size;
  237. }
  238. static ssize_t tulip_receive_nc(NetClientState *nc,
  239. const uint8_t *buf, size_t size)
  240. {
  241. return tulip_receive(qemu_get_nic_opaque(nc), buf, size);
  242. }
  243. static NetClientInfo net_tulip_info = {
  244. .type = NET_CLIENT_DRIVER_NIC,
  245. .size = sizeof(NICState),
  246. .receive = tulip_receive_nc,
  247. };
  248. static const char *tulip_reg_name(const hwaddr addr)
  249. {
  250. switch (addr) {
  251. case CSR(0):
  252. return "CSR0";
  253. case CSR(1):
  254. return "CSR1";
  255. case CSR(2):
  256. return "CSR2";
  257. case CSR(3):
  258. return "CSR3";
  259. case CSR(4):
  260. return "CSR4";
  261. case CSR(5):
  262. return "CSR5";
  263. case CSR(6):
  264. return "CSR6";
  265. case CSR(7):
  266. return "CSR7";
  267. case CSR(8):
  268. return "CSR8";
  269. case CSR(9):
  270. return "CSR9";
  271. case CSR(10):
  272. return "CSR10";
  273. case CSR(11):
  274. return "CSR11";
  275. case CSR(12):
  276. return "CSR12";
  277. case CSR(13):
  278. return "CSR13";
  279. case CSR(14):
  280. return "CSR14";
  281. case CSR(15):
  282. return "CSR15";
  283. default:
  284. break;
  285. }
  286. return "";
  287. }
  288. static const char *tulip_rx_state_name(int state)
  289. {
  290. switch (state) {
  291. case CSR5_RS_STOPPED:
  292. return "STOPPED";
  293. case CSR5_RS_RUNNING_FETCH:
  294. return "RUNNING/FETCH";
  295. case CSR5_RS_RUNNING_CHECK_EOR:
  296. return "RUNNING/CHECK EOR";
  297. case CSR5_RS_RUNNING_WAIT_RECEIVE:
  298. return "WAIT RECEIVE";
  299. case CSR5_RS_SUSPENDED:
  300. return "SUSPENDED";
  301. case CSR5_RS_RUNNING_CLOSE:
  302. return "RUNNING/CLOSE";
  303. case CSR5_RS_RUNNING_FLUSH:
  304. return "RUNNING/FLUSH";
  305. case CSR5_RS_RUNNING_QUEUE:
  306. return "RUNNING/QUEUE";
  307. default:
  308. break;
  309. }
  310. return "";
  311. }
  312. static const char *tulip_tx_state_name(int state)
  313. {
  314. switch (state) {
  315. case CSR5_TS_STOPPED:
  316. return "STOPPED";
  317. case CSR5_TS_RUNNING_FETCH:
  318. return "RUNNING/FETCH";
  319. case CSR5_TS_RUNNING_WAIT_EOT:
  320. return "RUNNING/WAIT EOT";
  321. case CSR5_TS_RUNNING_READ_BUF:
  322. return "RUNNING/READ BUF";
  323. case CSR5_TS_RUNNING_SETUP:
  324. return "RUNNING/SETUP";
  325. case CSR5_TS_SUSPENDED:
  326. return "SUSPENDED";
  327. case CSR5_TS_RUNNING_CLOSE:
  328. return "RUNNING/CLOSE";
  329. default:
  330. break;
  331. }
  332. return "";
  333. }
  334. static void tulip_update_rs(TULIPState *s, int state)
  335. {
  336. s->csr[5] &= ~(CSR5_RS_MASK << CSR5_RS_SHIFT);
  337. s->csr[5] |= (state & CSR5_RS_MASK) << CSR5_RS_SHIFT;
  338. trace_tulip_rx_state(tulip_rx_state_name(state));
  339. }
  340. static uint16_t tulip_mdi_default[] = {
  341. /* MDI Registers 0 - 6, 7 */
  342. 0x3100, 0xf02c, 0x7810, 0x0000, 0x0501, 0x4181, 0x0000, 0x0000,
  343. /* MDI Registers 8 - 15 */
  344. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  345. /* MDI Registers 16 - 31 */
  346. 0x0003, 0x0000, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  347. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  348. };
  349. /* Readonly mask for MDI (PHY) registers */
  350. static const uint16_t tulip_mdi_mask[] = {
  351. 0x0000, 0xffff, 0xffff, 0xffff, 0xc01f, 0xffff, 0xffff, 0x0000,
  352. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  353. 0x0fff, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  354. 0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  355. };
  356. static uint16_t tulip_mii_read(TULIPState *s, int phy, int reg)
  357. {
  358. uint16_t ret = 0;
  359. if (phy == 1) {
  360. ret = tulip_mdi_default[reg];
  361. }
  362. trace_tulip_mii_read(phy, reg, ret);
  363. return ret;
  364. }
  365. static void tulip_mii_write(TULIPState *s, int phy, int reg, uint16_t data)
  366. {
  367. trace_tulip_mii_write(phy, reg, data);
  368. if (phy != 1) {
  369. return;
  370. }
  371. tulip_mdi_default[reg] &= ~tulip_mdi_mask[reg];
  372. tulip_mdi_default[reg] |= (data & tulip_mdi_mask[reg]);
  373. }
  374. static void tulip_mii(TULIPState *s)
  375. {
  376. uint32_t changed = s->old_csr9 ^ s->csr[9];
  377. uint16_t data;
  378. int op, phy, reg;
  379. if (!(changed & CSR9_MDC)) {
  380. return;
  381. }
  382. if (!(s->csr[9] & CSR9_MDC)) {
  383. return;
  384. }
  385. s->mii_bitcnt++;
  386. s->mii_word <<= 1;
  387. if (s->csr[9] & CSR9_MDO && (s->mii_bitcnt < 16 ||
  388. !(s->csr[9] & CSR9_MII))) {
  389. /* write op or address bits */
  390. s->mii_word |= 1;
  391. }
  392. if (s->mii_bitcnt >= 16 && (s->csr[9] & CSR9_MII)) {
  393. if (s->mii_word & 0x8000) {
  394. s->csr[9] |= CSR9_MDI;
  395. } else {
  396. s->csr[9] &= ~CSR9_MDI;
  397. }
  398. }
  399. if (s->mii_word == 0xffffffff) {
  400. s->mii_bitcnt = 0;
  401. } else if (s->mii_bitcnt == 16) {
  402. op = (s->mii_word >> 12) & 0x0f;
  403. phy = (s->mii_word >> 7) & 0x1f;
  404. reg = (s->mii_word >> 2) & 0x1f;
  405. if (op == 6) {
  406. s->mii_word = tulip_mii_read(s, phy, reg);
  407. }
  408. } else if (s->mii_bitcnt == 32) {
  409. op = (s->mii_word >> 28) & 0x0f;
  410. phy = (s->mii_word >> 23) & 0x1f;
  411. reg = (s->mii_word >> 18) & 0x1f;
  412. data = s->mii_word & 0xffff;
  413. if (op == 5) {
  414. tulip_mii_write(s, phy, reg, data);
  415. }
  416. }
  417. }
  418. static uint32_t tulip_csr9_read(TULIPState *s)
  419. {
  420. if (s->csr[9] & CSR9_SR) {
  421. if (eeprom93xx_read(s->eeprom)) {
  422. s->csr[9] |= CSR9_SR_DO;
  423. } else {
  424. s->csr[9] &= ~CSR9_SR_DO;
  425. }
  426. }
  427. tulip_mii(s);
  428. return s->csr[9];
  429. }
  430. static void tulip_update_ts(TULIPState *s, int state)
  431. {
  432. s->csr[5] &= ~(CSR5_TS_MASK << CSR5_TS_SHIFT);
  433. s->csr[5] |= (state & CSR5_TS_MASK) << CSR5_TS_SHIFT;
  434. trace_tulip_tx_state(tulip_tx_state_name(state));
  435. }
  436. static uint64_t tulip_read(void *opaque, hwaddr addr,
  437. unsigned size)
  438. {
  439. TULIPState *s = opaque;
  440. uint64_t data = 0;
  441. switch (addr) {
  442. case CSR(9):
  443. data = tulip_csr9_read(s);
  444. break;
  445. case CSR(12):
  446. /* Fake autocompletion complete until we have PHY emulation */
  447. data = 5 << CSR12_ANS_SHIFT;
  448. break;
  449. default:
  450. if (addr & 7) {
  451. qemu_log_mask(LOG_GUEST_ERROR, "%s: read access at unknown address"
  452. " 0x%"PRIx64"\n", __func__, addr);
  453. } else {
  454. data = s->csr[addr >> 3];
  455. }
  456. break;
  457. }
  458. trace_tulip_reg_read(addr, tulip_reg_name(addr), size, data);
  459. return data;
  460. }
  461. static void tulip_tx(TULIPState *s, struct tulip_descriptor *desc)
  462. {
  463. if (s->tx_frame_len) {
  464. if ((s->csr[6] >> CSR6_OM_SHIFT) & CSR6_OM_MASK) {
  465. /* Internal or external Loopback */
  466. tulip_receive(s, s->tx_frame, s->tx_frame_len);
  467. } else if (s->tx_frame_len <= sizeof(s->tx_frame)) {
  468. qemu_send_packet(qemu_get_queue(s->nic),
  469. s->tx_frame, s->tx_frame_len);
  470. }
  471. }
  472. if (desc->control & TDES1_IC) {
  473. s->csr[5] |= CSR5_TI;
  474. tulip_update_int(s);
  475. }
  476. }
  477. static int tulip_copy_tx_buffers(TULIPState *s, struct tulip_descriptor *desc)
  478. {
  479. int len1 = (desc->control >> TDES1_BUF1_SIZE_SHIFT) & TDES1_BUF1_SIZE_MASK;
  480. int len2 = (desc->control >> TDES1_BUF2_SIZE_SHIFT) & TDES1_BUF2_SIZE_MASK;
  481. if (s->tx_frame_len + len1 > sizeof(s->tx_frame)) {
  482. qemu_log_mask(LOG_GUEST_ERROR,
  483. "%s: descriptor overflow (ofs: %u, len:%d, size:%zu)\n",
  484. __func__, s->tx_frame_len, len1, sizeof(s->tx_frame));
  485. return -1;
  486. }
  487. if (len1) {
  488. pci_dma_read(&s->dev, desc->buf_addr1,
  489. s->tx_frame + s->tx_frame_len, len1);
  490. s->tx_frame_len += len1;
  491. }
  492. if (s->tx_frame_len + len2 > sizeof(s->tx_frame)) {
  493. qemu_log_mask(LOG_GUEST_ERROR,
  494. "%s: descriptor overflow (ofs: %u, len:%d, size:%zu)\n",
  495. __func__, s->tx_frame_len, len2, sizeof(s->tx_frame));
  496. return -1;
  497. }
  498. if (len2) {
  499. pci_dma_read(&s->dev, desc->buf_addr2,
  500. s->tx_frame + s->tx_frame_len, len2);
  501. s->tx_frame_len += len2;
  502. }
  503. desc->status = (len1 + len2) ? 0 : 0x7fffffff;
  504. return 0;
  505. }
  506. static void tulip_setup_filter_addr(TULIPState *s, uint8_t *buf, int n)
  507. {
  508. int offset = n * 12;
  509. s->filter[n][0] = buf[offset];
  510. s->filter[n][1] = buf[offset + 1];
  511. s->filter[n][2] = buf[offset + 4];
  512. s->filter[n][3] = buf[offset + 5];
  513. s->filter[n][4] = buf[offset + 8];
  514. s->filter[n][5] = buf[offset + 9];
  515. trace_tulip_setup_filter(n, s->filter[n][5], s->filter[n][4],
  516. s->filter[n][3], s->filter[n][2], s->filter[n][1], s->filter[n][0]);
  517. }
  518. static void tulip_setup_frame(TULIPState *s,
  519. struct tulip_descriptor *desc)
  520. {
  521. uint8_t buf[4096];
  522. int len = (desc->control >> TDES1_BUF1_SIZE_SHIFT) & TDES1_BUF1_SIZE_MASK;
  523. int i;
  524. trace_tulip_setup_frame();
  525. if (len == 192) {
  526. pci_dma_read(&s->dev, desc->buf_addr1, buf, len);
  527. for (i = 0; i < 16; i++) {
  528. tulip_setup_filter_addr(s, buf, i);
  529. }
  530. }
  531. desc->status = 0x7fffffff;
  532. if (desc->control & TDES1_IC) {
  533. s->csr[5] |= CSR5_TI;
  534. tulip_update_int(s);
  535. }
  536. }
  537. static void tulip_next_tx_descriptor(TULIPState *s,
  538. struct tulip_descriptor *desc)
  539. {
  540. if (desc->control & TDES1_TER) {
  541. s->current_tx_desc = s->csr[4];
  542. } else if (desc->control & TDES1_TCH) {
  543. s->current_tx_desc = desc->buf_addr2;
  544. } else {
  545. s->current_tx_desc += sizeof(struct tulip_descriptor) +
  546. (((s->csr[0] >> CSR0_DSL_SHIFT) & CSR0_DSL_MASK) << 2);
  547. }
  548. s->current_tx_desc &= ~3ULL;
  549. }
  550. static uint32_t tulip_ts(TULIPState *s)
  551. {
  552. return (s->csr[5] >> CSR5_TS_SHIFT) & CSR5_TS_MASK;
  553. }
  554. static void tulip_xmit_list_update(TULIPState *s)
  555. {
  556. #define TULIP_DESC_MAX 128
  557. uint8_t i = 0;
  558. struct tulip_descriptor desc;
  559. if (tulip_ts(s) != CSR5_TS_SUSPENDED) {
  560. return;
  561. }
  562. for (i = 0; i < TULIP_DESC_MAX; i++) {
  563. tulip_desc_read(s, s->current_tx_desc, &desc);
  564. tulip_dump_tx_descriptor(s, &desc);
  565. if (!(desc.status & TDES0_OWN)) {
  566. tulip_update_ts(s, CSR5_TS_SUSPENDED);
  567. s->csr[5] |= CSR5_TU;
  568. tulip_update_int(s);
  569. return;
  570. }
  571. if (desc.control & TDES1_SET) {
  572. tulip_setup_frame(s, &desc);
  573. } else {
  574. if (desc.control & TDES1_FS) {
  575. s->tx_frame_len = 0;
  576. }
  577. if (!tulip_copy_tx_buffers(s, &desc)) {
  578. if (desc.control & TDES1_LS) {
  579. tulip_tx(s, &desc);
  580. }
  581. }
  582. }
  583. tulip_desc_write(s, s->current_tx_desc, &desc);
  584. tulip_next_tx_descriptor(s, &desc);
  585. }
  586. }
  587. static void tulip_csr9_write(TULIPState *s, uint32_t old_val,
  588. uint32_t new_val)
  589. {
  590. if (new_val & CSR9_SR) {
  591. eeprom93xx_write(s->eeprom,
  592. !!(new_val & CSR9_SR_CS),
  593. !!(new_val & CSR9_SR_SK),
  594. !!(new_val & CSR9_SR_DI));
  595. }
  596. }
  597. static void tulip_reset(TULIPState *s)
  598. {
  599. trace_tulip_reset();
  600. s->csr[0] = 0xfe000000;
  601. s->csr[1] = 0xffffffff;
  602. s->csr[2] = 0xffffffff;
  603. s->csr[5] = 0xf0000000;
  604. s->csr[6] = 0x32000040;
  605. s->csr[7] = 0xf3fe0000;
  606. s->csr[8] = 0xe0000000;
  607. s->csr[9] = 0xfff483ff;
  608. s->csr[11] = 0xfffe0000;
  609. s->csr[12] = 0x000000c6;
  610. s->csr[13] = 0xffff0000;
  611. s->csr[14] = 0xffffffff;
  612. s->csr[15] = 0x8ff00000;
  613. }
  614. static void tulip_qdev_reset(DeviceState *dev)
  615. {
  616. PCIDevice *d = PCI_DEVICE(dev);
  617. TULIPState *s = TULIP(d);
  618. tulip_reset(s);
  619. }
  620. static void tulip_write(void *opaque, hwaddr addr,
  621. uint64_t data, unsigned size)
  622. {
  623. TULIPState *s = opaque;
  624. trace_tulip_reg_write(addr, tulip_reg_name(addr), size, data);
  625. switch (addr) {
  626. case CSR(0):
  627. s->csr[0] = data;
  628. if (data & CSR0_SWR) {
  629. tulip_reset(s);
  630. tulip_update_int(s);
  631. }
  632. break;
  633. case CSR(1):
  634. tulip_xmit_list_update(s);
  635. break;
  636. case CSR(2):
  637. qemu_flush_queued_packets(qemu_get_queue(s->nic));
  638. break;
  639. case CSR(3):
  640. s->csr[3] = data & ~3ULL;
  641. s->current_rx_desc = s->csr[3];
  642. qemu_flush_queued_packets(qemu_get_queue(s->nic));
  643. break;
  644. case CSR(4):
  645. s->csr[4] = data & ~3ULL;
  646. s->current_tx_desc = s->csr[4];
  647. tulip_xmit_list_update(s);
  648. break;
  649. case CSR(5):
  650. /* Status register, write clears bit */
  651. s->csr[5] &= ~(data & (CSR5_TI | CSR5_TPS | CSR5_TU | CSR5_TJT |
  652. CSR5_LNP_ANC | CSR5_UNF | CSR5_RI | CSR5_RU |
  653. CSR5_RPS | CSR5_RWT | CSR5_ETI | CSR5_GTE |
  654. CSR5_LNF | CSR5_FBE | CSR5_ERI | CSR5_AIS |
  655. CSR5_NIS | CSR5_GPI | CSR5_LC));
  656. tulip_update_int(s);
  657. break;
  658. case CSR(6):
  659. s->csr[6] = data;
  660. if (s->csr[6] & CSR6_SR) {
  661. tulip_update_rs(s, CSR5_RS_RUNNING_WAIT_RECEIVE);
  662. qemu_flush_queued_packets(qemu_get_queue(s->nic));
  663. } else {
  664. tulip_update_rs(s, CSR5_RS_STOPPED);
  665. }
  666. if (s->csr[6] & CSR6_ST) {
  667. tulip_update_ts(s, CSR5_TS_SUSPENDED);
  668. tulip_xmit_list_update(s);
  669. } else {
  670. tulip_update_ts(s, CSR5_TS_STOPPED);
  671. }
  672. break;
  673. case CSR(7):
  674. s->csr[7] = data;
  675. tulip_update_int(s);
  676. break;
  677. case CSR(8):
  678. s->csr[9] = data;
  679. break;
  680. case CSR(9):
  681. tulip_csr9_write(s, s->csr[9], data);
  682. /* don't clear MII read data */
  683. s->csr[9] &= CSR9_MDI;
  684. s->csr[9] |= (data & ~CSR9_MDI);
  685. tulip_mii(s);
  686. s->old_csr9 = s->csr[9];
  687. break;
  688. case CSR(10):
  689. s->csr[10] = data;
  690. break;
  691. case CSR(11):
  692. s->csr[11] = data;
  693. break;
  694. case CSR(12):
  695. /* SIA Status register, some bits are cleared by writing 1 */
  696. s->csr[12] &= ~(data & (CSR12_MRA | CSR12_TRA | CSR12_ARA));
  697. break;
  698. case CSR(13):
  699. s->csr[13] = data;
  700. break;
  701. case CSR(14):
  702. s->csr[14] = data;
  703. break;
  704. case CSR(15):
  705. s->csr[15] = data;
  706. break;
  707. default:
  708. qemu_log_mask(LOG_GUEST_ERROR, "%s: write to CSR at unknown address "
  709. "0x%"PRIx64"\n", __func__, addr);
  710. break;
  711. }
  712. }
  713. static const MemoryRegionOps tulip_ops = {
  714. .read = tulip_read,
  715. .write = tulip_write,
  716. .endianness = DEVICE_LITTLE_ENDIAN,
  717. .impl = {
  718. .min_access_size = 4,
  719. .max_access_size = 4,
  720. },
  721. };
  722. static void tulip_idblock_crc(TULIPState *s, uint16_t *srom)
  723. {
  724. int word;
  725. int bit;
  726. unsigned char bitval, crc;
  727. const int len = 9;
  728. crc = -1;
  729. for (word = 0; word < len; word++) {
  730. for (bit = 15; bit >= 0; bit--) {
  731. if ((word == (len - 1)) && (bit == 7)) {
  732. /*
  733. * Insert the correct CRC result into input data stream
  734. * in place.
  735. */
  736. srom[len - 1] = (srom[len - 1] & 0xff00) | (unsigned short)crc;
  737. break;
  738. }
  739. bitval = ((srom[word] >> bit) & 1) ^ ((crc >> 7) & 1);
  740. crc = crc << 1;
  741. if (bitval == 1) {
  742. crc ^= 6;
  743. crc |= 0x01;
  744. }
  745. }
  746. }
  747. }
  748. static uint16_t tulip_srom_crc(TULIPState *s, uint8_t *eeprom, size_t len)
  749. {
  750. unsigned long crc = 0xffffffff;
  751. unsigned long flippedcrc = 0;
  752. unsigned char currentbyte;
  753. unsigned int msb, bit, i;
  754. for (i = 0; i < len; i++) {
  755. currentbyte = eeprom[i];
  756. for (bit = 0; bit < 8; bit++) {
  757. msb = (crc >> 31) & 1;
  758. crc <<= 1;
  759. if (msb ^ (currentbyte & 1)) {
  760. crc ^= 0x04c11db6;
  761. crc |= 0x00000001;
  762. }
  763. currentbyte >>= 1;
  764. }
  765. }
  766. for (i = 0; i < 32; i++) {
  767. flippedcrc <<= 1;
  768. bit = crc & 1;
  769. crc >>= 1;
  770. flippedcrc += bit;
  771. }
  772. return (flippedcrc ^ 0xffffffff) & 0xffff;
  773. }
  774. static const uint8_t eeprom_default[128] = {
  775. 0x3c, 0x10, 0x4f, 0x10, 0x00, 0x00, 0x00, 0x00,
  776. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  777. 0x56, 0x08, 0x04, 0x01, 0x00, 0x80, 0x48, 0xb3,
  778. 0x0e, 0xa7, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x08,
  779. 0x01, 0x8d, 0x03, 0x00, 0x00, 0x00, 0x00, 0x78,
  780. 0xe0, 0x01, 0x00, 0x50, 0x00, 0x18, 0x00, 0x00,
  781. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  782. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  783. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  784. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  785. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  786. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe8, 0x6b,
  787. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
  788. 0x48, 0xb3, 0x0e, 0xa7, 0x40, 0x00, 0x00, 0x00,
  789. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  790. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  791. };
  792. static void tulip_fill_eeprom(TULIPState *s)
  793. {
  794. uint16_t *eeprom = eeprom93xx_data(s->eeprom);
  795. memcpy(eeprom, eeprom_default, 128);
  796. /* patch in our mac address */
  797. eeprom[10] = cpu_to_le16(s->c.macaddr.a[0] | (s->c.macaddr.a[1] << 8));
  798. eeprom[11] = cpu_to_le16(s->c.macaddr.a[2] | (s->c.macaddr.a[3] << 8));
  799. eeprom[12] = cpu_to_le16(s->c.macaddr.a[4] | (s->c.macaddr.a[5] << 8));
  800. tulip_idblock_crc(s, eeprom);
  801. eeprom[63] = cpu_to_le16(tulip_srom_crc(s, (uint8_t *)eeprom, 126));
  802. }
  803. static void pci_tulip_realize(PCIDevice *pci_dev, Error **errp)
  804. {
  805. TULIPState *s = DO_UPCAST(TULIPState, dev, pci_dev);
  806. uint8_t *pci_conf;
  807. pci_conf = s->dev.config;
  808. pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin A */
  809. qemu_macaddr_default_if_unset(&s->c.macaddr);
  810. s->eeprom = eeprom93xx_new(&pci_dev->qdev, 64);
  811. tulip_fill_eeprom(s);
  812. memory_region_init_io(&s->io, OBJECT(&s->dev), &tulip_ops, s,
  813. "tulip-io", 128);
  814. memory_region_init_io(&s->memory, OBJECT(&s->dev), &tulip_ops, s,
  815. "tulip-mem", 128);
  816. pci_register_bar(&s->dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &s->io);
  817. pci_register_bar(&s->dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->memory);
  818. s->irq = pci_allocate_irq(&s->dev);
  819. s->nic = qemu_new_nic(&net_tulip_info, &s->c,
  820. object_get_typename(OBJECT(pci_dev)),
  821. pci_dev->qdev.id, s);
  822. qemu_format_nic_info_str(qemu_get_queue(s->nic), s->c.macaddr.a);
  823. }
  824. static void pci_tulip_exit(PCIDevice *pci_dev)
  825. {
  826. TULIPState *s = DO_UPCAST(TULIPState, dev, pci_dev);
  827. qemu_del_nic(s->nic);
  828. qemu_free_irq(s->irq);
  829. eeprom93xx_free(&pci_dev->qdev, s->eeprom);
  830. }
  831. static void tulip_instance_init(Object *obj)
  832. {
  833. PCIDevice *pci_dev = PCI_DEVICE(obj);
  834. TULIPState *d = DO_UPCAST(TULIPState, dev, pci_dev);
  835. device_add_bootindex_property(obj, &d->c.bootindex,
  836. "bootindex", "/ethernet-phy@0",
  837. &pci_dev->qdev);
  838. }
  839. static Property tulip_properties[] = {
  840. DEFINE_NIC_PROPERTIES(TULIPState, c),
  841. DEFINE_PROP_END_OF_LIST(),
  842. };
  843. static void tulip_class_init(ObjectClass *klass, void *data)
  844. {
  845. DeviceClass *dc = DEVICE_CLASS(klass);
  846. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  847. k->realize = pci_tulip_realize;
  848. k->exit = pci_tulip_exit;
  849. k->vendor_id = PCI_VENDOR_ID_DEC;
  850. k->device_id = PCI_DEVICE_ID_DEC_21143;
  851. k->subsystem_vendor_id = 0x103c;
  852. k->subsystem_id = 0x104f;
  853. k->class_id = PCI_CLASS_NETWORK_ETHERNET;
  854. dc->vmsd = &vmstate_pci_tulip;
  855. device_class_set_props(dc, tulip_properties);
  856. dc->reset = tulip_qdev_reset;
  857. set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
  858. }
  859. static const TypeInfo tulip_info = {
  860. .name = TYPE_TULIP,
  861. .parent = TYPE_PCI_DEVICE,
  862. .instance_size = sizeof(TULIPState),
  863. .class_init = tulip_class_init,
  864. .instance_init = tulip_instance_init,
  865. .interfaces = (InterfaceInfo[]) {
  866. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  867. { },
  868. },
  869. };
  870. static void tulip_register_types(void)
  871. {
  872. type_register_static(&tulip_info);
  873. }
  874. type_init(tulip_register_types)