e1000x_common.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 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/pci/pci.h"
  27. #include "net/net.h"
  28. #include "e1000x_common.h"
  29. #include "trace.h"
  30. bool e1000x_rx_ready(PCIDevice *d, uint32_t *mac)
  31. {
  32. bool link_up = mac[STATUS] & E1000_STATUS_LU;
  33. bool rx_enabled = mac[RCTL] & E1000_RCTL_EN;
  34. bool pci_master = d->config[PCI_COMMAND] & PCI_COMMAND_MASTER;
  35. if (!link_up || !rx_enabled || !pci_master) {
  36. trace_e1000x_rx_can_recv_disabled(link_up, rx_enabled, pci_master);
  37. return false;
  38. }
  39. return true;
  40. }
  41. bool e1000x_is_vlan_packet(const uint8_t *buf, uint16_t vet)
  42. {
  43. uint16_t eth_proto = lduw_be_p(buf + 12);
  44. bool res = (eth_proto == vet);
  45. trace_e1000x_vlan_is_vlan_pkt(res, eth_proto, vet);
  46. return res;
  47. }
  48. bool e1000x_rx_group_filter(uint32_t *mac, const uint8_t *buf)
  49. {
  50. static const int mta_shift[] = { 4, 3, 2, 0 };
  51. uint32_t f, ra[2], *rp, rctl = mac[RCTL];
  52. for (rp = mac + RA; rp < mac + RA + 32; rp += 2) {
  53. if (!(rp[1] & E1000_RAH_AV)) {
  54. continue;
  55. }
  56. ra[0] = cpu_to_le32(rp[0]);
  57. ra[1] = cpu_to_le32(rp[1]);
  58. if (!memcmp(buf, (uint8_t *)ra, 6)) {
  59. trace_e1000x_rx_flt_ucast_match((int)(rp - mac - RA) / 2,
  60. MAC_ARG(buf));
  61. return true;
  62. }
  63. }
  64. trace_e1000x_rx_flt_ucast_mismatch(MAC_ARG(buf));
  65. f = mta_shift[(rctl >> E1000_RCTL_MO_SHIFT) & 3];
  66. f = (((buf[5] << 8) | buf[4]) >> f) & 0xfff;
  67. if (mac[MTA + (f >> 5)] & (1 << (f & 0x1f))) {
  68. e1000x_inc_reg_if_not_full(mac, MPRC);
  69. return true;
  70. }
  71. trace_e1000x_rx_flt_inexact_mismatch(MAC_ARG(buf),
  72. (rctl >> E1000_RCTL_MO_SHIFT) & 3,
  73. f >> 5,
  74. mac[MTA + (f >> 5)]);
  75. return false;
  76. }
  77. bool e1000x_hw_rx_enabled(uint32_t *mac)
  78. {
  79. if (!(mac[STATUS] & E1000_STATUS_LU)) {
  80. trace_e1000x_rx_link_down(mac[STATUS]);
  81. return false;
  82. }
  83. if (!(mac[RCTL] & E1000_RCTL_EN)) {
  84. trace_e1000x_rx_disabled(mac[RCTL]);
  85. return false;
  86. }
  87. return true;
  88. }
  89. bool e1000x_is_oversized(uint32_t *mac, size_t size)
  90. {
  91. /* this is the size past which hardware will
  92. drop packets when setting LPE=0 */
  93. static const int maximum_ethernet_vlan_size = 1522;
  94. /* this is the size past which hardware will
  95. drop packets when setting LPE=1 */
  96. static const int maximum_ethernet_lpe_size = 16 * KiB;
  97. if ((size > maximum_ethernet_lpe_size ||
  98. (size > maximum_ethernet_vlan_size
  99. && !(mac[RCTL] & E1000_RCTL_LPE)))
  100. && !(mac[RCTL] & E1000_RCTL_SBP)) {
  101. e1000x_inc_reg_if_not_full(mac, ROC);
  102. trace_e1000x_rx_oversized(size);
  103. return true;
  104. }
  105. return false;
  106. }
  107. void e1000x_restart_autoneg(uint32_t *mac, uint16_t *phy, QEMUTimer *timer)
  108. {
  109. e1000x_update_regs_on_link_down(mac, phy);
  110. trace_e1000x_link_negotiation_start();
  111. timer_mod(timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
  112. }
  113. void e1000x_reset_mac_addr(NICState *nic, uint32_t *mac_regs,
  114. uint8_t *mac_addr)
  115. {
  116. int i;
  117. mac_regs[RA] = 0;
  118. mac_regs[RA + 1] = E1000_RAH_AV;
  119. for (i = 0; i < 4; i++) {
  120. mac_regs[RA] |= mac_addr[i] << (8 * i);
  121. mac_regs[RA + 1] |=
  122. (i < 2) ? mac_addr[i + 4] << (8 * i) : 0;
  123. }
  124. qemu_format_nic_info_str(qemu_get_queue(nic), mac_addr);
  125. trace_e1000x_mac_indicate(MAC_ARG(mac_addr));
  126. }
  127. void e1000x_update_regs_on_autoneg_done(uint32_t *mac, uint16_t *phy)
  128. {
  129. e1000x_update_regs_on_link_up(mac, phy);
  130. phy[PHY_LP_ABILITY] |= MII_LPAR_LPACK;
  131. phy[PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE;
  132. trace_e1000x_link_negotiation_done();
  133. }
  134. void
  135. e1000x_core_prepare_eeprom(uint16_t *eeprom,
  136. const uint16_t *templ,
  137. uint32_t templ_size,
  138. uint16_t dev_id,
  139. const uint8_t *macaddr)
  140. {
  141. uint16_t checksum = 0;
  142. int i;
  143. memmove(eeprom, templ, templ_size);
  144. for (i = 0; i < 3; i++) {
  145. eeprom[i] = (macaddr[2 * i + 1] << 8) | macaddr[2 * i];
  146. }
  147. eeprom[11] = eeprom[13] = dev_id;
  148. for (i = 0; i < EEPROM_CHECKSUM_REG; i++) {
  149. checksum += eeprom[i];
  150. }
  151. checksum = (uint16_t) EEPROM_SUM - checksum;
  152. eeprom[EEPROM_CHECKSUM_REG] = checksum;
  153. }
  154. uint32_t
  155. e1000x_rxbufsize(uint32_t rctl)
  156. {
  157. rctl &= E1000_RCTL_BSEX | E1000_RCTL_SZ_16384 | E1000_RCTL_SZ_8192 |
  158. E1000_RCTL_SZ_4096 | E1000_RCTL_SZ_2048 | E1000_RCTL_SZ_1024 |
  159. E1000_RCTL_SZ_512 | E1000_RCTL_SZ_256;
  160. switch (rctl) {
  161. case E1000_RCTL_BSEX | E1000_RCTL_SZ_16384:
  162. return 16384;
  163. case E1000_RCTL_BSEX | E1000_RCTL_SZ_8192:
  164. return 8192;
  165. case E1000_RCTL_BSEX | E1000_RCTL_SZ_4096:
  166. return 4096;
  167. case E1000_RCTL_SZ_1024:
  168. return 1024;
  169. case E1000_RCTL_SZ_512:
  170. return 512;
  171. case E1000_RCTL_SZ_256:
  172. return 256;
  173. }
  174. return 2048;
  175. }
  176. void
  177. e1000x_update_rx_total_stats(uint32_t *mac,
  178. size_t data_size,
  179. size_t data_fcs_size)
  180. {
  181. static const int PRCregs[6] = { PRC64, PRC127, PRC255, PRC511,
  182. PRC1023, PRC1522 };
  183. e1000x_increase_size_stats(mac, PRCregs, data_fcs_size);
  184. e1000x_inc_reg_if_not_full(mac, TPR);
  185. mac[GPRC] = mac[TPR];
  186. /* TOR - Total Octets Received:
  187. * This register includes bytes received in a packet from the <Destination
  188. * Address> field through the <CRC> field, inclusively.
  189. * Always include FCS length (4) in size.
  190. */
  191. e1000x_grow_8reg_if_not_full(mac, TORL, data_size + 4);
  192. mac[GORCL] = mac[TORL];
  193. mac[GORCH] = mac[TORH];
  194. }
  195. void
  196. e1000x_increase_size_stats(uint32_t *mac, const int *size_regs, int size)
  197. {
  198. if (size > 1023) {
  199. e1000x_inc_reg_if_not_full(mac, size_regs[5]);
  200. } else if (size > 511) {
  201. e1000x_inc_reg_if_not_full(mac, size_regs[4]);
  202. } else if (size > 255) {
  203. e1000x_inc_reg_if_not_full(mac, size_regs[3]);
  204. } else if (size > 127) {
  205. e1000x_inc_reg_if_not_full(mac, size_regs[2]);
  206. } else if (size > 64) {
  207. e1000x_inc_reg_if_not_full(mac, size_regs[1]);
  208. } else if (size == 64) {
  209. e1000x_inc_reg_if_not_full(mac, size_regs[0]);
  210. }
  211. }
  212. void
  213. e1000x_read_tx_ctx_descr(struct e1000_context_desc *d,
  214. e1000x_txd_props *props)
  215. {
  216. uint32_t op = le32_to_cpu(d->cmd_and_length);
  217. props->ipcss = d->lower_setup.ip_fields.ipcss;
  218. props->ipcso = d->lower_setup.ip_fields.ipcso;
  219. props->ipcse = le16_to_cpu(d->lower_setup.ip_fields.ipcse);
  220. props->tucss = d->upper_setup.tcp_fields.tucss;
  221. props->tucso = d->upper_setup.tcp_fields.tucso;
  222. props->tucse = le16_to_cpu(d->upper_setup.tcp_fields.tucse);
  223. props->paylen = op & 0xfffff;
  224. props->hdr_len = d->tcp_seg_setup.fields.hdr_len;
  225. props->mss = le16_to_cpu(d->tcp_seg_setup.fields.mss);
  226. props->ip = (op & E1000_TXD_CMD_IP) ? 1 : 0;
  227. props->tcp = (op & E1000_TXD_CMD_TCP) ? 1 : 0;
  228. props->tse = (op & E1000_TXD_CMD_TSE) ? 1 : 0;
  229. }