tulip.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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. 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. 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 > sizeof(s->rx_frame) - 4
  201. || s->rx_frame_len || tulip_rx_stopped(s)) {
  202. return 0;
  203. }
  204. if (!tulip_filter_address(s, buf)) {
  205. return size;
  206. }
  207. do {
  208. tulip_desc_read(s, s->current_rx_desc, &desc);
  209. tulip_dump_rx_descriptor(s, &desc);
  210. if (!(desc.status & RDES0_OWN)) {
  211. s->csr[5] |= CSR5_RU;
  212. tulip_update_int(s);
  213. return s->rx_frame_size - s->rx_frame_len;
  214. }
  215. desc.status = 0;
  216. if (!s->rx_frame_len) {
  217. s->rx_frame_size = size + 4;
  218. s->rx_status = RDES0_LS |
  219. ((s->rx_frame_size & RDES0_FL_MASK) << RDES0_FL_SHIFT);
  220. desc.status |= RDES0_FS;
  221. memcpy(s->rx_frame, buf, size);
  222. s->rx_frame_len = s->rx_frame_size;
  223. }
  224. tulip_copy_rx_bytes(s, &desc);
  225. if (!s->rx_frame_len) {
  226. desc.status |= s->rx_status;
  227. s->csr[5] |= CSR5_RI;
  228. tulip_update_int(s);
  229. }
  230. tulip_dump_rx_descriptor(s, &desc);
  231. tulip_desc_write(s, s->current_rx_desc, &desc);
  232. tulip_next_rx_descriptor(s, &desc);
  233. } while (s->rx_frame_len);
  234. return size;
  235. }
  236. static ssize_t tulip_receive_nc(NetClientState *nc,
  237. const uint8_t *buf, size_t size)
  238. {
  239. return tulip_receive(qemu_get_nic_opaque(nc), buf, size);
  240. }
  241. static NetClientInfo net_tulip_info = {
  242. .type = NET_CLIENT_DRIVER_NIC,
  243. .size = sizeof(NICState),
  244. .receive = tulip_receive_nc,
  245. };
  246. static const char *tulip_reg_name(const hwaddr addr)
  247. {
  248. switch (addr) {
  249. case CSR(0):
  250. return "CSR0";
  251. case CSR(1):
  252. return "CSR1";
  253. case CSR(2):
  254. return "CSR2";
  255. case CSR(3):
  256. return "CSR3";
  257. case CSR(4):
  258. return "CSR4";
  259. case CSR(5):
  260. return "CSR5";
  261. case CSR(6):
  262. return "CSR6";
  263. case CSR(7):
  264. return "CSR7";
  265. case CSR(8):
  266. return "CSR8";
  267. case CSR(9):
  268. return "CSR9";
  269. case CSR(10):
  270. return "CSR10";
  271. case CSR(11):
  272. return "CSR11";
  273. case CSR(12):
  274. return "CSR12";
  275. case CSR(13):
  276. return "CSR13";
  277. case CSR(14):
  278. return "CSR14";
  279. case CSR(15):
  280. return "CSR15";
  281. default:
  282. break;
  283. }
  284. return "";
  285. }
  286. static const char *tulip_rx_state_name(int state)
  287. {
  288. switch (state) {
  289. case CSR5_RS_STOPPED:
  290. return "STOPPED";
  291. case CSR5_RS_RUNNING_FETCH:
  292. return "RUNNING/FETCH";
  293. case CSR5_RS_RUNNING_CHECK_EOR:
  294. return "RUNNING/CHECK EOR";
  295. case CSR5_RS_RUNNING_WAIT_RECEIVE:
  296. return "WAIT RECEIVE";
  297. case CSR5_RS_SUSPENDED:
  298. return "SUSPENDED";
  299. case CSR5_RS_RUNNING_CLOSE:
  300. return "RUNNING/CLOSE";
  301. case CSR5_RS_RUNNING_FLUSH:
  302. return "RUNNING/FLUSH";
  303. case CSR5_RS_RUNNING_QUEUE:
  304. return "RUNNING/QUEUE";
  305. default:
  306. break;
  307. }
  308. return "";
  309. }
  310. static const char *tulip_tx_state_name(int state)
  311. {
  312. switch (state) {
  313. case CSR5_TS_STOPPED:
  314. return "STOPPED";
  315. case CSR5_TS_RUNNING_FETCH:
  316. return "RUNNING/FETCH";
  317. case CSR5_TS_RUNNING_WAIT_EOT:
  318. return "RUNNING/WAIT EOT";
  319. case CSR5_TS_RUNNING_READ_BUF:
  320. return "RUNNING/READ BUF";
  321. case CSR5_TS_RUNNING_SETUP:
  322. return "RUNNING/SETUP";
  323. case CSR5_TS_SUSPENDED:
  324. return "SUSPENDED";
  325. case CSR5_TS_RUNNING_CLOSE:
  326. return "RUNNING/CLOSE";
  327. default:
  328. break;
  329. }
  330. return "";
  331. }
  332. static void tulip_update_rs(TULIPState *s, int state)
  333. {
  334. s->csr[5] &= ~(CSR5_RS_MASK << CSR5_RS_SHIFT);
  335. s->csr[5] |= (state & CSR5_RS_MASK) << CSR5_RS_SHIFT;
  336. trace_tulip_rx_state(tulip_rx_state_name(state));
  337. }
  338. static uint16_t tulip_mdi_default[] = {
  339. /* MDI Registers 0 - 6, 7 */
  340. 0x3100, 0xf02c, 0x7810, 0x0000, 0x0501, 0x4181, 0x0000, 0x0000,
  341. /* MDI Registers 8 - 15 */
  342. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  343. /* MDI Registers 16 - 31 */
  344. 0x0003, 0x0000, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  345. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  346. };
  347. /* Readonly mask for MDI (PHY) registers */
  348. static const uint16_t tulip_mdi_mask[] = {
  349. 0x0000, 0xffff, 0xffff, 0xffff, 0xc01f, 0xffff, 0xffff, 0x0000,
  350. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  351. 0x0fff, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  352. 0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  353. };
  354. static uint16_t tulip_mii_read(TULIPState *s, int phy, int reg)
  355. {
  356. uint16_t ret = 0;
  357. if (phy == 1) {
  358. ret = tulip_mdi_default[reg];
  359. }
  360. trace_tulip_mii_read(phy, reg, ret);
  361. return ret;
  362. }
  363. static void tulip_mii_write(TULIPState *s, int phy, int reg, uint16_t data)
  364. {
  365. trace_tulip_mii_write(phy, reg, data);
  366. if (phy != 1) {
  367. return;
  368. }
  369. tulip_mdi_default[reg] &= ~tulip_mdi_mask[reg];
  370. tulip_mdi_default[reg] |= (data & tulip_mdi_mask[reg]);
  371. }
  372. static void tulip_mii(TULIPState *s)
  373. {
  374. uint32_t changed = s->old_csr9 ^ s->csr[9];
  375. uint16_t data;
  376. int op, phy, reg;
  377. if (!(changed & CSR9_MDC)) {
  378. return;
  379. }
  380. if (!(s->csr[9] & CSR9_MDC)) {
  381. return;
  382. }
  383. s->mii_bitcnt++;
  384. s->mii_word <<= 1;
  385. if (s->csr[9] & CSR9_MDO && (s->mii_bitcnt < 16 ||
  386. !(s->csr[9] & CSR9_MII))) {
  387. /* write op or address bits */
  388. s->mii_word |= 1;
  389. }
  390. if (s->mii_bitcnt >= 16 && (s->csr[9] & CSR9_MII)) {
  391. if (s->mii_word & 0x8000) {
  392. s->csr[9] |= CSR9_MDI;
  393. } else {
  394. s->csr[9] &= ~CSR9_MDI;
  395. }
  396. }
  397. if (s->mii_word == 0xffffffff) {
  398. s->mii_bitcnt = 0;
  399. } else if (s->mii_bitcnt == 16) {
  400. op = (s->mii_word >> 12) & 0x0f;
  401. phy = (s->mii_word >> 7) & 0x1f;
  402. reg = (s->mii_word >> 2) & 0x1f;
  403. if (op == 6) {
  404. s->mii_word = tulip_mii_read(s, phy, reg);
  405. }
  406. } else if (s->mii_bitcnt == 32) {
  407. op = (s->mii_word >> 28) & 0x0f;
  408. phy = (s->mii_word >> 23) & 0x1f;
  409. reg = (s->mii_word >> 18) & 0x1f;
  410. data = s->mii_word & 0xffff;
  411. if (op == 5) {
  412. tulip_mii_write(s, phy, reg, data);
  413. }
  414. }
  415. }
  416. static uint32_t tulip_csr9_read(TULIPState *s)
  417. {
  418. if (s->csr[9] & CSR9_SR) {
  419. if (eeprom93xx_read(s->eeprom)) {
  420. s->csr[9] |= CSR9_SR_DO;
  421. } else {
  422. s->csr[9] &= ~CSR9_SR_DO;
  423. }
  424. }
  425. tulip_mii(s);
  426. return s->csr[9];
  427. }
  428. static void tulip_update_ts(TULIPState *s, int state)
  429. {
  430. s->csr[5] &= ~(CSR5_TS_MASK << CSR5_TS_SHIFT);
  431. s->csr[5] |= (state & CSR5_TS_MASK) << CSR5_TS_SHIFT;
  432. trace_tulip_tx_state(tulip_tx_state_name(state));
  433. }
  434. static uint64_t tulip_read(void *opaque, hwaddr addr,
  435. unsigned size)
  436. {
  437. TULIPState *s = opaque;
  438. uint64_t data = 0;
  439. switch (addr) {
  440. case CSR(9):
  441. data = tulip_csr9_read(s);
  442. break;
  443. case CSR(12):
  444. /* Fake autocompletion complete until we have PHY emulation */
  445. data = 5 << CSR12_ANS_SHIFT;
  446. break;
  447. default:
  448. if (addr & 7) {
  449. qemu_log_mask(LOG_GUEST_ERROR, "%s: read access at unknown address"
  450. " 0x%"PRIx64"\n", __func__, addr);
  451. } else {
  452. data = s->csr[addr >> 3];
  453. }
  454. break;
  455. }
  456. trace_tulip_reg_read(addr, tulip_reg_name(addr), size, data);
  457. return data;
  458. }
  459. static void tulip_tx(TULIPState *s, struct tulip_descriptor *desc)
  460. {
  461. if (s->tx_frame_len) {
  462. if ((s->csr[6] >> CSR6_OM_SHIFT) & CSR6_OM_MASK) {
  463. /* Internal or external Loopback */
  464. tulip_receive(s, s->tx_frame, s->tx_frame_len);
  465. } else if (s->tx_frame_len <= sizeof(s->tx_frame)) {
  466. qemu_send_packet(qemu_get_queue(s->nic),
  467. s->tx_frame, s->tx_frame_len);
  468. }
  469. }
  470. if (desc->control & TDES1_IC) {
  471. s->csr[5] |= CSR5_TI;
  472. tulip_update_int(s);
  473. }
  474. }
  475. static int tulip_copy_tx_buffers(TULIPState *s, struct tulip_descriptor *desc)
  476. {
  477. int len1 = (desc->control >> TDES1_BUF1_SIZE_SHIFT) & TDES1_BUF1_SIZE_MASK;
  478. int len2 = (desc->control >> TDES1_BUF2_SIZE_SHIFT) & TDES1_BUF2_SIZE_MASK;
  479. if (s->tx_frame_len + len1 > sizeof(s->tx_frame)) {
  480. qemu_log_mask(LOG_GUEST_ERROR,
  481. "%s: descriptor overflow (ofs: %u, len:%d, size:%zu)\n",
  482. __func__, s->tx_frame_len, len1, sizeof(s->tx_frame));
  483. return -1;
  484. }
  485. if (len1) {
  486. pci_dma_read(&s->dev, desc->buf_addr1,
  487. s->tx_frame + s->tx_frame_len, len1);
  488. s->tx_frame_len += len1;
  489. }
  490. if (s->tx_frame_len + len2 > sizeof(s->tx_frame)) {
  491. qemu_log_mask(LOG_GUEST_ERROR,
  492. "%s: descriptor overflow (ofs: %u, len:%d, size:%zu)\n",
  493. __func__, s->tx_frame_len, len2, sizeof(s->tx_frame));
  494. return -1;
  495. }
  496. if (len2) {
  497. pci_dma_read(&s->dev, desc->buf_addr2,
  498. s->tx_frame + s->tx_frame_len, len2);
  499. s->tx_frame_len += len2;
  500. }
  501. desc->status = (len1 + len2) ? 0 : 0x7fffffff;
  502. return 0;
  503. }
  504. static void tulip_setup_filter_addr(TULIPState *s, uint8_t *buf, int n)
  505. {
  506. int offset = n * 12;
  507. s->filter[n][0] = buf[offset];
  508. s->filter[n][1] = buf[offset + 1];
  509. s->filter[n][2] = buf[offset + 4];
  510. s->filter[n][3] = buf[offset + 5];
  511. s->filter[n][4] = buf[offset + 8];
  512. s->filter[n][5] = buf[offset + 9];
  513. trace_tulip_setup_filter(n, s->filter[n][5], s->filter[n][4],
  514. s->filter[n][3], s->filter[n][2], s->filter[n][1], s->filter[n][0]);
  515. }
  516. static void tulip_setup_frame(TULIPState *s,
  517. struct tulip_descriptor *desc)
  518. {
  519. uint8_t buf[4096];
  520. int len = (desc->control >> TDES1_BUF1_SIZE_SHIFT) & TDES1_BUF1_SIZE_MASK;
  521. int i;
  522. trace_tulip_setup_frame();
  523. if (len == 192) {
  524. pci_dma_read(&s->dev, desc->buf_addr1, buf, len);
  525. for (i = 0; i < 16; i++) {
  526. tulip_setup_filter_addr(s, buf, i);
  527. }
  528. }
  529. desc->status = 0x7fffffff;
  530. if (desc->control & TDES1_IC) {
  531. s->csr[5] |= CSR5_TI;
  532. tulip_update_int(s);
  533. }
  534. }
  535. static void tulip_next_tx_descriptor(TULIPState *s,
  536. struct tulip_descriptor *desc)
  537. {
  538. if (desc->control & TDES1_TER) {
  539. s->current_tx_desc = s->csr[4];
  540. } else if (desc->control & TDES1_TCH) {
  541. s->current_tx_desc = desc->buf_addr2;
  542. } else {
  543. s->current_tx_desc += sizeof(struct tulip_descriptor) +
  544. (((s->csr[0] >> CSR0_DSL_SHIFT) & CSR0_DSL_MASK) << 2);
  545. }
  546. s->current_tx_desc &= ~3ULL;
  547. }
  548. static uint32_t tulip_ts(TULIPState *s)
  549. {
  550. return (s->csr[5] >> CSR5_TS_SHIFT) & CSR5_TS_MASK;
  551. }
  552. static void tulip_xmit_list_update(TULIPState *s)
  553. {
  554. #define TULIP_DESC_MAX 128
  555. uint8_t i = 0;
  556. struct tulip_descriptor desc;
  557. if (tulip_ts(s) != CSR5_TS_SUSPENDED) {
  558. return;
  559. }
  560. for (i = 0; i < TULIP_DESC_MAX; i++) {
  561. tulip_desc_read(s, s->current_tx_desc, &desc);
  562. tulip_dump_tx_descriptor(s, &desc);
  563. if (!(desc.status & TDES0_OWN)) {
  564. tulip_update_ts(s, CSR5_TS_SUSPENDED);
  565. s->csr[5] |= CSR5_TU;
  566. tulip_update_int(s);
  567. return;
  568. }
  569. if (desc.control & TDES1_SET) {
  570. tulip_setup_frame(s, &desc);
  571. } else {
  572. if (desc.control & TDES1_FS) {
  573. s->tx_frame_len = 0;
  574. }
  575. if (!tulip_copy_tx_buffers(s, &desc)) {
  576. if (desc.control & TDES1_LS) {
  577. tulip_tx(s, &desc);
  578. }
  579. }
  580. }
  581. tulip_desc_write(s, s->current_tx_desc, &desc);
  582. tulip_next_tx_descriptor(s, &desc);
  583. }
  584. }
  585. static void tulip_csr9_write(TULIPState *s, uint32_t old_val,
  586. uint32_t new_val)
  587. {
  588. if (new_val & CSR9_SR) {
  589. eeprom93xx_write(s->eeprom,
  590. !!(new_val & CSR9_SR_CS),
  591. !!(new_val & CSR9_SR_SK),
  592. !!(new_val & CSR9_SR_DI));
  593. }
  594. }
  595. static void tulip_reset(TULIPState *s)
  596. {
  597. trace_tulip_reset();
  598. s->csr[0] = 0xfe000000;
  599. s->csr[1] = 0xffffffff;
  600. s->csr[2] = 0xffffffff;
  601. s->csr[5] = 0xf0000000;
  602. s->csr[6] = 0x32000040;
  603. s->csr[7] = 0xf3fe0000;
  604. s->csr[8] = 0xe0000000;
  605. s->csr[9] = 0xfff483ff;
  606. s->csr[11] = 0xfffe0000;
  607. s->csr[12] = 0x000000c6;
  608. s->csr[13] = 0xffff0000;
  609. s->csr[14] = 0xffffffff;
  610. s->csr[15] = 0x8ff00000;
  611. }
  612. static void tulip_qdev_reset(DeviceState *dev)
  613. {
  614. PCIDevice *d = PCI_DEVICE(dev);
  615. TULIPState *s = TULIP(d);
  616. tulip_reset(s);
  617. }
  618. static void tulip_write(void *opaque, hwaddr addr,
  619. uint64_t data, unsigned size)
  620. {
  621. TULIPState *s = opaque;
  622. trace_tulip_reg_write(addr, tulip_reg_name(addr), size, data);
  623. switch (addr) {
  624. case CSR(0):
  625. s->csr[0] = data;
  626. if (data & CSR0_SWR) {
  627. tulip_reset(s);
  628. tulip_update_int(s);
  629. }
  630. break;
  631. case CSR(1):
  632. tulip_xmit_list_update(s);
  633. break;
  634. case CSR(2):
  635. qemu_flush_queued_packets(qemu_get_queue(s->nic));
  636. break;
  637. case CSR(3):
  638. s->csr[3] = data & ~3ULL;
  639. s->current_rx_desc = s->csr[3];
  640. qemu_flush_queued_packets(qemu_get_queue(s->nic));
  641. break;
  642. case CSR(4):
  643. s->csr[4] = data & ~3ULL;
  644. s->current_tx_desc = s->csr[4];
  645. tulip_xmit_list_update(s);
  646. break;
  647. case CSR(5):
  648. /* Status register, write clears bit */
  649. s->csr[5] &= ~(data & (CSR5_TI | CSR5_TPS | CSR5_TU | CSR5_TJT |
  650. CSR5_LNP_ANC | CSR5_UNF | CSR5_RI | CSR5_RU |
  651. CSR5_RPS | CSR5_RWT | CSR5_ETI | CSR5_GTE |
  652. CSR5_LNF | CSR5_FBE | CSR5_ERI | CSR5_AIS |
  653. CSR5_NIS | CSR5_GPI | CSR5_LC));
  654. tulip_update_int(s);
  655. break;
  656. case CSR(6):
  657. s->csr[6] = data;
  658. if (s->csr[6] & CSR6_SR) {
  659. tulip_update_rs(s, CSR5_RS_RUNNING_WAIT_RECEIVE);
  660. qemu_flush_queued_packets(qemu_get_queue(s->nic));
  661. } else {
  662. tulip_update_rs(s, CSR5_RS_STOPPED);
  663. }
  664. if (s->csr[6] & CSR6_ST) {
  665. tulip_update_ts(s, CSR5_TS_SUSPENDED);
  666. tulip_xmit_list_update(s);
  667. } else {
  668. tulip_update_ts(s, CSR5_TS_STOPPED);
  669. }
  670. break;
  671. case CSR(7):
  672. s->csr[7] = data;
  673. tulip_update_int(s);
  674. break;
  675. case CSR(8):
  676. s->csr[9] = data;
  677. break;
  678. case CSR(9):
  679. tulip_csr9_write(s, s->csr[9], data);
  680. /* don't clear MII read data */
  681. s->csr[9] &= CSR9_MDI;
  682. s->csr[9] |= (data & ~CSR9_MDI);
  683. tulip_mii(s);
  684. s->old_csr9 = s->csr[9];
  685. break;
  686. case CSR(10):
  687. s->csr[10] = data;
  688. break;
  689. case CSR(11):
  690. s->csr[11] = data;
  691. break;
  692. case CSR(12):
  693. /* SIA Status register, some bits are cleared by writing 1 */
  694. s->csr[12] &= ~(data & (CSR12_MRA | CSR12_TRA | CSR12_ARA));
  695. break;
  696. case CSR(13):
  697. s->csr[13] = data;
  698. break;
  699. case CSR(14):
  700. s->csr[14] = data;
  701. break;
  702. case CSR(15):
  703. s->csr[15] = data;
  704. break;
  705. default:
  706. qemu_log_mask(LOG_GUEST_ERROR, "%s: write to CSR at unknown address "
  707. "0x%"PRIx64"\n", __func__, addr);
  708. break;
  709. }
  710. }
  711. static const MemoryRegionOps tulip_ops = {
  712. .read = tulip_read,
  713. .write = tulip_write,
  714. .endianness = DEVICE_LITTLE_ENDIAN,
  715. .impl = {
  716. .min_access_size = 4,
  717. .max_access_size = 4,
  718. },
  719. };
  720. static void tulip_idblock_crc(TULIPState *s, uint16_t *srom)
  721. {
  722. int word, n;
  723. int bit;
  724. unsigned char bitval, crc;
  725. const int len = 9;
  726. n = 0;
  727. crc = -1;
  728. for (word = 0; word < len; word++) {
  729. for (bit = 15; bit >= 0; bit--) {
  730. if ((word == (len - 1)) && (bit == 7)) {
  731. /*
  732. * Insert the correct CRC result into input data stream
  733. * in place.
  734. */
  735. srom[len - 1] = (srom[len - 1] & 0xff00) | (unsigned short)crc;
  736. break;
  737. }
  738. n++;
  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. s->eeprom = eeprom93xx_new(&pci_dev->qdev, 64);
  810. tulip_fill_eeprom(s);
  811. memory_region_init_io(&s->io, OBJECT(&s->dev), &tulip_ops, s,
  812. "tulip-io", 128);
  813. memory_region_init_io(&s->memory, OBJECT(&s->dev), &tulip_ops, s,
  814. "tulip-mem", 128);
  815. pci_register_bar(&s->dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &s->io);
  816. pci_register_bar(&s->dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->memory);
  817. s->irq = pci_allocate_irq(&s->dev);
  818. qemu_macaddr_default_if_unset(&s->c.macaddr);
  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)