tulip.c 27 KB

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