e1000x_common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * QEMU e1000(e) emulation - shared code
  3. *
  4. * Copyright (c) 2008 Qumranet
  5. *
  6. * Based on work done by:
  7. * Nir Peleg, Tutis Systems Ltd. for Qumranet Inc.
  8. * Copyright (c) 2007 Dan Aloni
  9. * Copyright (c) 2004 Antony T Curtis
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu/units.h"
  26. #include "hw/net/mii.h"
  27. #include "hw/pci/pci_device.h"
  28. #include "net/eth.h"
  29. #include "net/net.h"
  30. #include "e1000_common.h"
  31. #include "e1000x_common.h"
  32. #include "trace.h"
  33. bool e1000x_rx_ready(PCIDevice *d, uint32_t *mac)
  34. {
  35. bool link_up = mac[STATUS] & E1000_STATUS_LU;
  36. bool rx_enabled = mac[RCTL] & E1000_RCTL_EN;
  37. bool pci_master = d->config[PCI_COMMAND] & PCI_COMMAND_MASTER;
  38. if (!link_up || !rx_enabled || !pci_master) {
  39. trace_e1000x_rx_can_recv_disabled(link_up, rx_enabled, pci_master);
  40. return false;
  41. }
  42. return true;
  43. }
  44. bool e1000x_is_vlan_packet(const void *buf, uint16_t vet)
  45. {
  46. uint16_t eth_proto = lduw_be_p(&PKT_GET_ETH_HDR(buf)->h_proto);
  47. bool res = (eth_proto == vet);
  48. trace_e1000x_vlan_is_vlan_pkt(res, eth_proto, vet);
  49. return res;
  50. }
  51. bool e1000x_rx_vlan_filter(uint32_t *mac, const struct vlan_header *vhdr)
  52. {
  53. if (e1000x_vlan_rx_filter_enabled(mac)) {
  54. uint16_t vid = lduw_be_p(&vhdr->h_tci);
  55. uint32_t vfta =
  56. ldl_le_p((uint32_t *)(mac + VFTA) +
  57. ((vid >> E1000_VFTA_ENTRY_SHIFT) & E1000_VFTA_ENTRY_MASK));
  58. if ((vfta & (1 << (vid & E1000_VFTA_ENTRY_BIT_SHIFT_MASK))) == 0) {
  59. trace_e1000x_rx_flt_vlan_mismatch(vid);
  60. return false;
  61. }
  62. trace_e1000x_rx_flt_vlan_match(vid);
  63. }
  64. return true;
  65. }
  66. bool e1000x_rx_group_filter(uint32_t *mac, const struct eth_header *ehdr)
  67. {
  68. static const int mta_shift[] = { 4, 3, 2, 0 };
  69. uint32_t f, ra[2], *rp, rctl = mac[RCTL];
  70. if (is_broadcast_ether_addr(ehdr->h_dest)) {
  71. if (rctl & E1000_RCTL_BAM) {
  72. return true;
  73. }
  74. } else if (is_multicast_ether_addr(ehdr->h_dest)) {
  75. if (rctl & E1000_RCTL_MPE) {
  76. return true;
  77. }
  78. } else {
  79. if (rctl & E1000_RCTL_UPE) {
  80. return true;
  81. }
  82. }
  83. for (rp = mac + RA; rp < mac + RA + 32; rp += 2) {
  84. if (!(rp[1] & E1000_RAH_AV)) {
  85. continue;
  86. }
  87. ra[0] = cpu_to_le32(rp[0]);
  88. ra[1] = cpu_to_le32(rp[1]);
  89. if (!memcmp(ehdr->h_dest, (uint8_t *)ra, ETH_ALEN)) {
  90. trace_e1000x_rx_flt_ucast_match((int)(rp - mac - RA) / 2,
  91. MAC_ARG(ehdr->h_dest));
  92. return true;
  93. }
  94. }
  95. trace_e1000x_rx_flt_ucast_mismatch(MAC_ARG(ehdr->h_dest));
  96. f = mta_shift[(rctl >> E1000_RCTL_MO_SHIFT) & 3];
  97. f = (((ehdr->h_dest[5] << 8) | ehdr->h_dest[4]) >> f) & 0xfff;
  98. if (mac[MTA + (f >> 5)] & (1 << (f & 0x1f))) {
  99. return true;
  100. }
  101. trace_e1000x_rx_flt_inexact_mismatch(MAC_ARG(ehdr->h_dest),
  102. (rctl >> E1000_RCTL_MO_SHIFT) & 3,
  103. f >> 5,
  104. mac[MTA + (f >> 5)]);
  105. return false;
  106. }
  107. bool e1000x_hw_rx_enabled(uint32_t *mac)
  108. {
  109. if (!(mac[STATUS] & E1000_STATUS_LU)) {
  110. trace_e1000x_rx_link_down(mac[STATUS]);
  111. return false;
  112. }
  113. if (!(mac[RCTL] & E1000_RCTL_EN)) {
  114. trace_e1000x_rx_disabled(mac[RCTL]);
  115. return false;
  116. }
  117. return true;
  118. }
  119. bool e1000x_is_oversized(uint32_t *mac, size_t size)
  120. {
  121. size_t header_size = sizeof(struct eth_header) + sizeof(struct vlan_header);
  122. /* this is the size past which hardware will
  123. drop packets when setting LPE=0 */
  124. size_t maximum_short_size = header_size + ETH_MTU;
  125. /* this is the size past which hardware will
  126. drop packets when setting LPE=1 */
  127. size_t maximum_large_size = 16 * KiB - ETH_FCS_LEN;
  128. if ((size > maximum_large_size ||
  129. (size > maximum_short_size && !(mac[RCTL] & E1000_RCTL_LPE)))
  130. && !(mac[RCTL] & E1000_RCTL_SBP)) {
  131. e1000x_inc_reg_if_not_full(mac, ROC);
  132. trace_e1000x_rx_oversized(size);
  133. return true;
  134. }
  135. return false;
  136. }
  137. void e1000x_restart_autoneg(uint32_t *mac, uint16_t *phy, QEMUTimer *timer)
  138. {
  139. e1000x_update_regs_on_link_down(mac, phy);
  140. trace_e1000x_link_negotiation_start();
  141. timer_mod(timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
  142. }
  143. void e1000x_reset_mac_addr(NICState *nic, uint32_t *mac_regs,
  144. uint8_t *mac_addr)
  145. {
  146. int i;
  147. mac_regs[RA] = 0;
  148. mac_regs[RA + 1] = E1000_RAH_AV;
  149. for (i = 0; i < 4; i++) {
  150. mac_regs[RA] |= mac_addr[i] << (8 * i);
  151. mac_regs[RA + 1] |=
  152. (i < 2) ? mac_addr[i + 4] << (8 * i) : 0;
  153. }
  154. qemu_format_nic_info_str(qemu_get_queue(nic), mac_addr);
  155. trace_e1000x_mac_indicate(MAC_ARG(mac_addr));
  156. }
  157. void e1000x_update_regs_on_autoneg_done(uint32_t *mac, uint16_t *phy)
  158. {
  159. e1000x_update_regs_on_link_up(mac, phy);
  160. phy[MII_ANLPAR] |= MII_ANLPAR_ACK;
  161. phy[MII_BMSR] |= MII_BMSR_AN_COMP;
  162. trace_e1000x_link_negotiation_done();
  163. }
  164. void
  165. e1000x_core_prepare_eeprom(uint16_t *eeprom,
  166. const uint16_t *templ,
  167. uint32_t templ_size,
  168. uint16_t dev_id,
  169. const uint8_t *macaddr)
  170. {
  171. uint16_t checksum = 0;
  172. int i;
  173. memmove(eeprom, templ, templ_size);
  174. for (i = 0; i < 3; i++) {
  175. eeprom[i] = (macaddr[2 * i + 1] << 8) | macaddr[2 * i];
  176. }
  177. eeprom[11] = eeprom[13] = dev_id;
  178. for (i = 0; i < EEPROM_CHECKSUM_REG; i++) {
  179. checksum += eeprom[i];
  180. }
  181. checksum = (uint16_t) EEPROM_SUM - checksum;
  182. eeprom[EEPROM_CHECKSUM_REG] = checksum;
  183. }
  184. uint32_t
  185. e1000x_rxbufsize(uint32_t rctl)
  186. {
  187. rctl &= E1000_RCTL_BSEX | E1000_RCTL_SZ_16384 | E1000_RCTL_SZ_8192 |
  188. E1000_RCTL_SZ_4096 | E1000_RCTL_SZ_2048 | E1000_RCTL_SZ_1024 |
  189. E1000_RCTL_SZ_512 | E1000_RCTL_SZ_256;
  190. switch (rctl) {
  191. case E1000_RCTL_BSEX | E1000_RCTL_SZ_16384:
  192. return 16384;
  193. case E1000_RCTL_BSEX | E1000_RCTL_SZ_8192:
  194. return 8192;
  195. case E1000_RCTL_BSEX | E1000_RCTL_SZ_4096:
  196. return 4096;
  197. case E1000_RCTL_SZ_1024:
  198. return 1024;
  199. case E1000_RCTL_SZ_512:
  200. return 512;
  201. case E1000_RCTL_SZ_256:
  202. return 256;
  203. }
  204. return 2048;
  205. }
  206. void
  207. e1000x_update_rx_total_stats(uint32_t *mac,
  208. eth_pkt_types_e pkt_type,
  209. size_t pkt_size,
  210. size_t pkt_fcs_size)
  211. {
  212. static const int PRCregs[6] = { PRC64, PRC127, PRC255, PRC511,
  213. PRC1023, PRC1522 };
  214. e1000x_increase_size_stats(mac, PRCregs, pkt_fcs_size);
  215. e1000x_inc_reg_if_not_full(mac, TPR);
  216. e1000x_inc_reg_if_not_full(mac, GPRC);
  217. /* TOR - Total Octets Received:
  218. * This register includes bytes received in a packet from the <Destination
  219. * Address> field through the <CRC> field, inclusively.
  220. * Always include FCS length (4) in size.
  221. */
  222. e1000x_grow_8reg_if_not_full(mac, TORL, pkt_size + 4);
  223. e1000x_grow_8reg_if_not_full(mac, GORCL, pkt_size + 4);
  224. switch (pkt_type) {
  225. case ETH_PKT_BCAST:
  226. e1000x_inc_reg_if_not_full(mac, BPRC);
  227. break;
  228. case ETH_PKT_MCAST:
  229. e1000x_inc_reg_if_not_full(mac, MPRC);
  230. break;
  231. default:
  232. break;
  233. }
  234. }
  235. void
  236. e1000x_increase_size_stats(uint32_t *mac, const int *size_regs, int size)
  237. {
  238. if (size > 1023) {
  239. e1000x_inc_reg_if_not_full(mac, size_regs[5]);
  240. } else if (size > 511) {
  241. e1000x_inc_reg_if_not_full(mac, size_regs[4]);
  242. } else if (size > 255) {
  243. e1000x_inc_reg_if_not_full(mac, size_regs[3]);
  244. } else if (size > 127) {
  245. e1000x_inc_reg_if_not_full(mac, size_regs[2]);
  246. } else if (size > 64) {
  247. e1000x_inc_reg_if_not_full(mac, size_regs[1]);
  248. } else if (size == 64) {
  249. e1000x_inc_reg_if_not_full(mac, size_regs[0]);
  250. }
  251. }
  252. void
  253. e1000x_read_tx_ctx_descr(struct e1000_context_desc *d,
  254. e1000x_txd_props *props)
  255. {
  256. uint32_t op = le32_to_cpu(d->cmd_and_length);
  257. props->ipcss = d->lower_setup.ip_fields.ipcss;
  258. props->ipcso = d->lower_setup.ip_fields.ipcso;
  259. props->ipcse = le16_to_cpu(d->lower_setup.ip_fields.ipcse);
  260. props->tucss = d->upper_setup.tcp_fields.tucss;
  261. props->tucso = d->upper_setup.tcp_fields.tucso;
  262. props->tucse = le16_to_cpu(d->upper_setup.tcp_fields.tucse);
  263. props->paylen = op & 0xfffff;
  264. props->hdr_len = d->tcp_seg_setup.fields.hdr_len;
  265. props->mss = le16_to_cpu(d->tcp_seg_setup.fields.mss);
  266. props->ip = (op & E1000_TXD_CMD_IP) ? 1 : 0;
  267. props->tcp = (op & E1000_TXD_CMD_TCP) ? 1 : 0;
  268. props->tse = (op & E1000_TXD_CMD_TSE) ? 1 : 0;
  269. }
  270. void e1000x_timestamp(uint32_t *mac, int64_t timadj, size_t lo, size_t hi)
  271. {
  272. int64_t ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  273. uint32_t timinca = mac[TIMINCA];
  274. uint32_t incvalue = timinca & E1000_TIMINCA_INCVALUE_MASK;
  275. uint32_t incperiod = MAX(timinca >> E1000_TIMINCA_INCPERIOD_SHIFT, 1);
  276. int64_t timestamp = timadj + muldiv64(ns, incvalue, incperiod * 16);
  277. mac[lo] = timestamp & 0xffffffff;
  278. mac[hi] = timestamp >> 32;
  279. }
  280. void e1000x_set_timinca(uint32_t *mac, int64_t *timadj, uint32_t val)
  281. {
  282. int64_t ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  283. uint32_t old_val = mac[TIMINCA];
  284. uint32_t old_incvalue = old_val & E1000_TIMINCA_INCVALUE_MASK;
  285. uint32_t old_incperiod = MAX(old_val >> E1000_TIMINCA_INCPERIOD_SHIFT, 1);
  286. uint32_t incvalue = val & E1000_TIMINCA_INCVALUE_MASK;
  287. uint32_t incperiod = MAX(val >> E1000_TIMINCA_INCPERIOD_SHIFT, 1);
  288. mac[TIMINCA] = val;
  289. *timadj += (muldiv64(ns, incvalue, incperiod) - muldiv64(ns, old_incvalue, old_incperiod)) / 16;
  290. }