2
0

e1000x_common.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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_group_filter(uint32_t *mac, const uint8_t *buf)
  52. {
  53. static const int mta_shift[] = { 4, 3, 2, 0 };
  54. uint32_t f, ra[2], *rp, rctl = mac[RCTL];
  55. for (rp = mac + RA; rp < mac + RA + 32; rp += 2) {
  56. if (!(rp[1] & E1000_RAH_AV)) {
  57. continue;
  58. }
  59. ra[0] = cpu_to_le32(rp[0]);
  60. ra[1] = cpu_to_le32(rp[1]);
  61. if (!memcmp(buf, (uint8_t *)ra, ETH_ALEN)) {
  62. trace_e1000x_rx_flt_ucast_match((int)(rp - mac - RA) / 2,
  63. MAC_ARG(buf));
  64. return true;
  65. }
  66. }
  67. trace_e1000x_rx_flt_ucast_mismatch(MAC_ARG(buf));
  68. f = mta_shift[(rctl >> E1000_RCTL_MO_SHIFT) & 3];
  69. f = (((buf[5] << 8) | buf[4]) >> f) & 0xfff;
  70. if (mac[MTA + (f >> 5)] & (1 << (f & 0x1f))) {
  71. return true;
  72. }
  73. trace_e1000x_rx_flt_inexact_mismatch(MAC_ARG(buf),
  74. (rctl >> E1000_RCTL_MO_SHIFT) & 3,
  75. f >> 5,
  76. mac[MTA + (f >> 5)]);
  77. return false;
  78. }
  79. bool e1000x_hw_rx_enabled(uint32_t *mac)
  80. {
  81. if (!(mac[STATUS] & E1000_STATUS_LU)) {
  82. trace_e1000x_rx_link_down(mac[STATUS]);
  83. return false;
  84. }
  85. if (!(mac[RCTL] & E1000_RCTL_EN)) {
  86. trace_e1000x_rx_disabled(mac[RCTL]);
  87. return false;
  88. }
  89. return true;
  90. }
  91. bool e1000x_is_oversized(uint32_t *mac, size_t size)
  92. {
  93. /* this is the size past which hardware will
  94. drop packets when setting LPE=0 */
  95. static const int maximum_ethernet_vlan_size = 1522;
  96. /* this is the size past which hardware will
  97. drop packets when setting LPE=1 */
  98. static const int maximum_ethernet_lpe_size = 16 * KiB;
  99. if ((size > maximum_ethernet_lpe_size ||
  100. (size > maximum_ethernet_vlan_size
  101. && !(mac[RCTL] & E1000_RCTL_LPE)))
  102. && !(mac[RCTL] & E1000_RCTL_SBP)) {
  103. e1000x_inc_reg_if_not_full(mac, ROC);
  104. trace_e1000x_rx_oversized(size);
  105. return true;
  106. }
  107. return false;
  108. }
  109. void e1000x_restart_autoneg(uint32_t *mac, uint16_t *phy, QEMUTimer *timer)
  110. {
  111. e1000x_update_regs_on_link_down(mac, phy);
  112. trace_e1000x_link_negotiation_start();
  113. timer_mod(timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
  114. }
  115. void e1000x_reset_mac_addr(NICState *nic, uint32_t *mac_regs,
  116. uint8_t *mac_addr)
  117. {
  118. int i;
  119. mac_regs[RA] = 0;
  120. mac_regs[RA + 1] = E1000_RAH_AV;
  121. for (i = 0; i < 4; i++) {
  122. mac_regs[RA] |= mac_addr[i] << (8 * i);
  123. mac_regs[RA + 1] |=
  124. (i < 2) ? mac_addr[i + 4] << (8 * i) : 0;
  125. }
  126. qemu_format_nic_info_str(qemu_get_queue(nic), mac_addr);
  127. trace_e1000x_mac_indicate(MAC_ARG(mac_addr));
  128. }
  129. void e1000x_update_regs_on_autoneg_done(uint32_t *mac, uint16_t *phy)
  130. {
  131. e1000x_update_regs_on_link_up(mac, phy);
  132. phy[MII_ANLPAR] |= MII_ANLPAR_ACK;
  133. phy[MII_BMSR] |= MII_BMSR_AN_COMP;
  134. trace_e1000x_link_negotiation_done();
  135. }
  136. void
  137. e1000x_core_prepare_eeprom(uint16_t *eeprom,
  138. const uint16_t *templ,
  139. uint32_t templ_size,
  140. uint16_t dev_id,
  141. const uint8_t *macaddr)
  142. {
  143. uint16_t checksum = 0;
  144. int i;
  145. memmove(eeprom, templ, templ_size);
  146. for (i = 0; i < 3; i++) {
  147. eeprom[i] = (macaddr[2 * i + 1] << 8) | macaddr[2 * i];
  148. }
  149. eeprom[11] = eeprom[13] = dev_id;
  150. for (i = 0; i < EEPROM_CHECKSUM_REG; i++) {
  151. checksum += eeprom[i];
  152. }
  153. checksum = (uint16_t) EEPROM_SUM - checksum;
  154. eeprom[EEPROM_CHECKSUM_REG] = checksum;
  155. }
  156. uint32_t
  157. e1000x_rxbufsize(uint32_t rctl)
  158. {
  159. rctl &= E1000_RCTL_BSEX | E1000_RCTL_SZ_16384 | E1000_RCTL_SZ_8192 |
  160. E1000_RCTL_SZ_4096 | E1000_RCTL_SZ_2048 | E1000_RCTL_SZ_1024 |
  161. E1000_RCTL_SZ_512 | E1000_RCTL_SZ_256;
  162. switch (rctl) {
  163. case E1000_RCTL_BSEX | E1000_RCTL_SZ_16384:
  164. return 16384;
  165. case E1000_RCTL_BSEX | E1000_RCTL_SZ_8192:
  166. return 8192;
  167. case E1000_RCTL_BSEX | E1000_RCTL_SZ_4096:
  168. return 4096;
  169. case E1000_RCTL_SZ_1024:
  170. return 1024;
  171. case E1000_RCTL_SZ_512:
  172. return 512;
  173. case E1000_RCTL_SZ_256:
  174. return 256;
  175. }
  176. return 2048;
  177. }
  178. void
  179. e1000x_update_rx_total_stats(uint32_t *mac,
  180. eth_pkt_types_e pkt_type,
  181. size_t pkt_size,
  182. size_t pkt_fcs_size)
  183. {
  184. static const int PRCregs[6] = { PRC64, PRC127, PRC255, PRC511,
  185. PRC1023, PRC1522 };
  186. e1000x_increase_size_stats(mac, PRCregs, pkt_fcs_size);
  187. e1000x_inc_reg_if_not_full(mac, TPR);
  188. e1000x_inc_reg_if_not_full(mac, GPRC);
  189. /* TOR - Total Octets Received:
  190. * This register includes bytes received in a packet from the <Destination
  191. * Address> field through the <CRC> field, inclusively.
  192. * Always include FCS length (4) in size.
  193. */
  194. e1000x_grow_8reg_if_not_full(mac, TORL, pkt_size + 4);
  195. e1000x_grow_8reg_if_not_full(mac, GORCL, pkt_size + 4);
  196. switch (pkt_type) {
  197. case ETH_PKT_BCAST:
  198. e1000x_inc_reg_if_not_full(mac, BPRC);
  199. break;
  200. case ETH_PKT_MCAST:
  201. e1000x_inc_reg_if_not_full(mac, MPRC);
  202. break;
  203. default:
  204. break;
  205. }
  206. }
  207. void
  208. e1000x_increase_size_stats(uint32_t *mac, const int *size_regs, int size)
  209. {
  210. if (size > 1023) {
  211. e1000x_inc_reg_if_not_full(mac, size_regs[5]);
  212. } else if (size > 511) {
  213. e1000x_inc_reg_if_not_full(mac, size_regs[4]);
  214. } else if (size > 255) {
  215. e1000x_inc_reg_if_not_full(mac, size_regs[3]);
  216. } else if (size > 127) {
  217. e1000x_inc_reg_if_not_full(mac, size_regs[2]);
  218. } else if (size > 64) {
  219. e1000x_inc_reg_if_not_full(mac, size_regs[1]);
  220. } else if (size == 64) {
  221. e1000x_inc_reg_if_not_full(mac, size_regs[0]);
  222. }
  223. }
  224. void
  225. e1000x_read_tx_ctx_descr(struct e1000_context_desc *d,
  226. e1000x_txd_props *props)
  227. {
  228. uint32_t op = le32_to_cpu(d->cmd_and_length);
  229. props->ipcss = d->lower_setup.ip_fields.ipcss;
  230. props->ipcso = d->lower_setup.ip_fields.ipcso;
  231. props->ipcse = le16_to_cpu(d->lower_setup.ip_fields.ipcse);
  232. props->tucss = d->upper_setup.tcp_fields.tucss;
  233. props->tucso = d->upper_setup.tcp_fields.tucso;
  234. props->tucse = le16_to_cpu(d->upper_setup.tcp_fields.tucse);
  235. props->paylen = op & 0xfffff;
  236. props->hdr_len = d->tcp_seg_setup.fields.hdr_len;
  237. props->mss = le16_to_cpu(d->tcp_seg_setup.fields.mss);
  238. props->ip = (op & E1000_TXD_CMD_IP) ? 1 : 0;
  239. props->tcp = (op & E1000_TXD_CMD_TCP) ? 1 : 0;
  240. props->tse = (op & E1000_TXD_CMD_TSE) ? 1 : 0;
  241. }
  242. void e1000x_timestamp(uint32_t *mac, int64_t timadj, size_t lo, size_t hi)
  243. {
  244. int64_t ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  245. uint32_t timinca = mac[TIMINCA];
  246. uint32_t incvalue = timinca & E1000_TIMINCA_INCVALUE_MASK;
  247. uint32_t incperiod = MAX(timinca >> E1000_TIMINCA_INCPERIOD_SHIFT, 1);
  248. int64_t timestamp = timadj + muldiv64(ns, incvalue, incperiod * 16);
  249. mac[lo] = timestamp & 0xffffffff;
  250. mac[hi] = timestamp >> 32;
  251. }
  252. void e1000x_set_timinca(uint32_t *mac, int64_t *timadj, uint32_t val)
  253. {
  254. int64_t ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  255. uint32_t old_val = mac[TIMINCA];
  256. uint32_t old_incvalue = old_val & E1000_TIMINCA_INCVALUE_MASK;
  257. uint32_t old_incperiod = MAX(old_val >> E1000_TIMINCA_INCPERIOD_SHIFT, 1);
  258. uint32_t incvalue = val & E1000_TIMINCA_INCVALUE_MASK;
  259. uint32_t incperiod = MAX(val >> E1000_TIMINCA_INCPERIOD_SHIFT, 1);
  260. mac[TIMINCA] = val;
  261. *timadj += (muldiv64(ns, incvalue, incperiod) - muldiv64(ns, old_incvalue, old_incperiod)) / 16;
  262. }