1a2ed165fSKevin Bowling /*-
2a2ed165fSKevin Bowling * SPDX-License-Identifier: BSD-3-Clause
3a2ed165fSKevin Bowling *
4a2ed165fSKevin Bowling * Copyright (c) 2010-2016, Intel Corporation
5a2ed165fSKevin Bowling * Copyright (c) 2026 Kevin Bowling <kbowling@FreeBSD.org>
6a2ed165fSKevin Bowling */
7a2ed165fSKevin Bowling
8a2ed165fSKevin Bowling #include "if_em.h"
9a2ed165fSKevin Bowling #include "if_igb_iov.h"
10a2ed165fSKevin Bowling
11a2ed165fSKevin Bowling #ifdef PCI_IOV
12a2ed165fSKevin Bowling
13a2ed165fSKevin Bowling #include <sys/iov.h>
145ce6c94fSKevin Bowling #include <sys/sdt.h>
15a2ed165fSKevin Bowling #include <sys/time.h>
16a2ed165fSKevin Bowling
17a2ed165fSKevin Bowling #define IGB_IOV_RAH_POOLSEL_SHIFT 18
18a2ed165fSKevin Bowling #define IGB_IOV_RAH_POOLSEL_MASK (0xffU << IGB_IOV_RAH_POOLSEL_SHIFT)
19a2ed165fSKevin Bowling #define IGB_IOV_MAX_MAC_FILTERS 3
20a2ed165fSKevin Bowling #define IGB_IOV_MAX_MC_HASHES 30
21a2ed165fSKevin Bowling #define IGB_IOV_MBX_RETRY_COUNT 6
22e8f3b96bSKevin Bowling /* Allow two complete 31-VID replays, then sustain eight additions/second. */
23e8f3b96bSKevin Bowling #define IGB_IOV_VLAN_CHANGE_BURST 64
24e8f3b96bSKevin Bowling #define IGB_IOV_VLAN_CHANGE_INTERVAL (SBT_1S / 8)
25a2ed165fSKevin Bowling /* 82576 Datasheet rev. 2.0, Section 8.14.16: VMOLR[31] must be one. */
26a2ed165fSKevin Bowling #define IGB_82576_VMOLR_RSV (1U << 31)
27a2ed165fSKevin Bowling #define IGB_82576_LVMMC_BLOCK_MASK 0x1c
288c872470SKevin Bowling #define IGB_82576_NUM_QUEUES 16
29a2ed165fSKevin Bowling #define IGB_82576_QUEUE_MASK 0xffff
30a2ed165fSKevin Bowling #define IGB_82576_STAGGERED_QUEUE_SHIFT 8
318c872470SKevin Bowling #define IGB_82576_VF_QUEUE_STRIDE 8
328c872470SKevin Bowling #define IGB_82576_VF_QUEUES 2
33a2ed165fSKevin Bowling #define IGB_I350_DTXCTL_ENABLE_SPOOF_QUEUE (1U << 2)
34a2ed165fSKevin Bowling #define IGB_I350_LVMMC_MAC_VLAN_SPOOF (1U << 25)
35a2ed165fSKevin Bowling #define IGB_I350_LVMMC_LAST_Q_SHIFT 29
36a2ed165fSKevin Bowling #define IGB_I350_LVMMC_LAST_Q_MASK 0x7
378c872470SKevin Bowling #define IGB_I350_NUM_QUEUES 8
38a2ed165fSKevin Bowling #define IGB_I350_QUEUE_MASK 0xff
39a2ed165fSKevin Bowling #define IGB_I350_RESET_ACK_TIMEOUT (100 * SBT_1MS)
408c872470SKevin Bowling #define IGB_I350_VF_QUEUES 1
418c872470SKevin Bowling #define IGB_IOV_QUEUE_DISABLE_BUSY_RETRIES 10
428c872470SKevin Bowling #define IGB_IOV_QUEUE_DISABLE_DELAY_US 10
438c872470SKevin Bowling #define IGB_IOV_QUEUE_DISABLE_PAUSE (100 * SBT_1US)
448c872470SKevin Bowling #define IGB_IOV_QUEUE_DISABLE_RETRIES 20
458c872470SKevin Bowling #define IGB_IOV_VF_QUEUES_MAX 2
46a2ed165fSKevin Bowling
47a2ed165fSKevin Bowling #define IGB_VF_CTS (1U << 0)
48a2ed165fSKevin Bowling #define IGB_VF_CAP_MAC (1U << 1)
49a2ed165fSKevin Bowling #define IGB_VF_ACTIVE (1U << 2)
50a2ed165fSKevin Bowling #define IGB_VF_MAC_ANTI_SPOOF (1U << 3)
51a2ed165fSKevin Bowling #define IGB_VF_ALLOW_PROMISC (1U << 4)
52a2ed165fSKevin Bowling #define IGB_VF_UCAST_PROMISC (1U << 5)
53a2ed165fSKevin Bowling #define IGB_VF_MCAST_PROMISC (1U << 6)
54a2ed165fSKevin Bowling #define IGB_VF_MCAST_OVERFLOW (1U << 7)
55a2ed165fSKevin Bowling #define IGB_VF_MCAST_OVERFLOW_WARNED (1U << 8)
56a2ed165fSKevin Bowling #define IGB_VF_MDD_BLOCKED (1U << 9)
57a2ed165fSKevin Bowling #define IGB_VF_MBX_PENDING (1U << 10)
58a2ed165fSKevin Bowling /*
59a2ed165fSKevin Bowling * After bounded PFU retries, suppress future or overlapping VF requests until
60a2ed165fSKevin Bowling * RST/VFLR starts a new mailbox epoch. Intel VF drivers assert CTRL.RST
61a2ed165fSKevin Bowling * before sending their mailbox reset request.
62a2ed165fSKevin Bowling */
63a2ed165fSKevin Bowling #define IGB_VF_MBX_GAVE_UP (1U << 11)
64a2ed165fSKevin Bowling #define IGB_VF_MDD_NOTIFY_PENDING (1U << 12)
65a2ed165fSKevin Bowling
66a2ed165fSKevin Bowling struct igb_vf {
67a2ed165fSKevin Bowling u32 flags;
68a2ed165fSKevin Bowling struct timeval last_nack;
69a2ed165fSKevin Bowling struct timeval last_mbx_log;
70a2ed165fSKevin Bowling struct timeval last_spoof_log;
71a2ed165fSKevin Bowling struct timeval last_mdd_log;
728c872470SKevin Bowling struct timeval last_queue_log;
73a2ed165fSKevin Bowling sbintime_t mbx_retry_at;
74a2ed165fSKevin Bowling sbintime_t mdd_notify_at;
75e8f3b96bSKevin Bowling sbintime_t vlan_token_time;
76a2ed165fSKevin Bowling u16 pool;
77a2ed165fSKevin Bowling u16 rar_index;
78a2ed165fSKevin Bowling u16 max_frame_size;
79a2ed165fSKevin Bowling u16 mc_count;
80a2ed165fSKevin Bowling u16 vlan_count;
81e8f3b96bSKevin Bowling u16 vlan_tokens;
82a2ed165fSKevin Bowling u16 default_vlan;
83a2ed165fSKevin Bowling u8 mbx_retry_count;
84a2ed165fSKevin Bowling u8 mac[ETHER_ADDR_LEN];
85a2ed165fSKevin Bowling u16 mc_hashes[IGB_IOV_MAX_MC_HASHES];
86a2ed165fSKevin Bowling u32 vlans[EM_VFTA_SIZE];
87a2ed165fSKevin Bowling };
88a2ed165fSKevin Bowling
89a2ed165fSKevin Bowling struct igb_vf_mac_filter {
90a2ed165fSKevin Bowling bool active;
91a2ed165fSKevin Bowling u16 pool;
92a2ed165fSKevin Bowling u16 rar_index;
93a2ed165fSKevin Bowling u8 mac[ETHER_ADDR_LEN];
94a2ed165fSKevin Bowling };
95a2ed165fSKevin Bowling
96a2ed165fSKevin Bowling MALLOC_DEFINE(M_IGB_IOV, "igb_iov", "igb SR-IOV allocations");
97a2ed165fSKevin Bowling
985ce6c94fSKevin Bowling /*
995ce6c94fSKevin Bowling * These logical-write probes let hardware tests verify the elision policy.
1005ce6c94fSKevin Bowling * e1000_write_vfta_i350() expands one VFTA call into ten physical writes, so
1015ce6c94fSKevin Bowling * the probes intentionally count calls made by the rebuild rather than MMIO
1025ce6c94fSKevin Bowling * transactions. The state probe exposes the final software images while the
1035ce6c94fSKevin Bowling * stack arrays are still live.
1045ce6c94fSKevin Bowling */
1055ce6c94fSKevin Bowling SDT_PROVIDER_DEFINE(igb_iov);
1065ce6c94fSKevin Bowling SDT_PROBE_DEFINE3(igb_iov, vlan, rebuild, vfta_clear,
1075ce6c94fSKevin Bowling "struct e1000_softc *", "u_int", "uint32_t");
1085ce6c94fSKevin Bowling SDT_PROBE_DEFINE3(igb_iov, vlan, rebuild, vlvf_write,
1095ce6c94fSKevin Bowling "struct e1000_softc *", "u_int", "uint32_t");
1105ce6c94fSKevin Bowling SDT_PROBE_DEFINE3(igb_iov, vlan, rebuild, vfta_set,
1115ce6c94fSKevin Bowling "struct e1000_softc *", "u_int", "uint32_t");
1125ce6c94fSKevin Bowling SDT_PROBE_DEFINE3(igb_iov, vlan, rebuild, state,
1135ce6c94fSKevin Bowling "struct e1000_softc *", "uint32_t *", "uint32_t *");
114ac2be06eSKevin Bowling SDT_PROBE_DEFINE4(igb_iov, mdd, sample, wvbr,
115ac2be06eSKevin Bowling "struct e1000_softc *", "uint32_t", "uint32_t", "uint32_t");
1165ce6c94fSKevin Bowling
117a2ed165fSKevin Bowling static const struct timeval igb_iov_nack_interval = { 2, 0 };
118a2ed165fSKevin Bowling static const struct timeval igb_iov_mbx_log_interval = { 2, 0 };
119a2ed165fSKevin Bowling static const struct timeval igb_iov_spoof_log_interval = { 2, 0 };
120a2ed165fSKevin Bowling static const struct timeval igb_iov_mdd_log_interval = { 2, 0 };
121a2ed165fSKevin Bowling static const sbintime_t igb_iov_mdd_notify_retry = SBT_1S / 2;
122a2ed165fSKevin Bowling static const sbintime_t igb_iov_mbx_retry_delay[IGB_IOV_MBX_RETRY_COUNT] = {
123a2ed165fSKevin Bowling SBT_1MS,
124a2ed165fSKevin Bowling 2 * SBT_1MS,
125a2ed165fSKevin Bowling 4 * SBT_1MS,
126a2ed165fSKevin Bowling 8 * SBT_1MS,
127a2ed165fSKevin Bowling 16 * SBT_1MS,
128a2ed165fSKevin Bowling 32 * SBT_1MS,
129a2ed165fSKevin Bowling };
130a2ed165fSKevin Bowling
131a2ed165fSKevin Bowling static void igb_iov_clear_mac_filters(struct e1000_softc *,
132a2ed165fSKevin Bowling const struct igb_vf *);
133a2ed165fSKevin Bowling static bool igb_iov_mac_in_use(struct e1000_softc *, const u8 *,
134a2ed165fSKevin Bowling const struct igb_vf *);
135a2ed165fSKevin Bowling static bool igb_iov_vlan_present(struct e1000_softc *, u16, bool);
136a2ed165fSKevin Bowling static int igb_iov_vlan_unique_count(struct e1000_softc *, bool);
137a2ed165fSKevin Bowling
138a2ed165fSKevin Bowling static void
igb_iov_mbx_retry_callout(void * arg)139a2ed165fSKevin Bowling igb_iov_mbx_retry_callout(void *arg)
140a2ed165fSKevin Bowling {
141a2ed165fSKevin Bowling struct e1000_softc *sc;
142a2ed165fSKevin Bowling
143a2ed165fSKevin Bowling sc = arg;
144a2ed165fSKevin Bowling /*
145a2ed165fSKevin Bowling * Mailbox service is serialized by iflib's context lock. The
146a2ed165fSKevin Bowling * callout only re-enters through the ordinary admin task.
147a2ed165fSKevin Bowling */
148a2ed165fSKevin Bowling iflib_admin_intr_deferred(sc->ctx);
149a2ed165fSKevin Bowling }
150a2ed165fSKevin Bowling
151a2ed165fSKevin Bowling static u_int
igb_iov_copy_maddr(void * arg,struct sockaddr_dl * sdl,u_int idx)152a2ed165fSKevin Bowling igb_iov_copy_maddr(void *arg, struct sockaddr_dl *sdl, u_int idx)
153a2ed165fSKevin Bowling {
154a2ed165fSKevin Bowling u8 *mta;
155a2ed165fSKevin Bowling
156a2ed165fSKevin Bowling if (idx == MAX_NUM_MULTICAST_ADDRESSES)
157a2ed165fSKevin Bowling return (0);
158a2ed165fSKevin Bowling mta = arg;
159a2ed165fSKevin Bowling memcpy(&mta[idx * ETHER_ADDR_LEN], LLADDR(sdl), ETHER_ADDR_LEN);
160a2ed165fSKevin Bowling return (1);
161a2ed165fSKevin Bowling }
162a2ed165fSKevin Bowling
163a2ed165fSKevin Bowling static bool
igb_iov_pf_vlan_promisc(struct e1000_softc * sc)164a2ed165fSKevin Bowling igb_iov_pf_vlan_promisc(struct e1000_softc *sc)
165a2ed165fSKevin Bowling {
166a2ed165fSKevin Bowling if_t ifp;
167a2ed165fSKevin Bowling
168a2ed165fSKevin Bowling ifp = iflib_get_ifp(sc->ctx);
169a2ed165fSKevin Bowling return (sc->iov_pf_vlan_promisc ||
170a2ed165fSKevin Bowling (if_getflags(ifp) & IFF_PROMISC) != 0);
171a2ed165fSKevin Bowling }
172a2ed165fSKevin Bowling
173a2ed165fSKevin Bowling static bool
igb_iov_mac_valid(const u8 * mac)174a2ed165fSKevin Bowling igb_iov_mac_valid(const u8 *mac)
175a2ed165fSKevin Bowling {
176a2ed165fSKevin Bowling static const u8 zero[ETHER_ADDR_LEN];
177a2ed165fSKevin Bowling
178a2ed165fSKevin Bowling return (!ETHER_IS_MULTICAST(mac) &&
179a2ed165fSKevin Bowling memcmp(mac, zero, ETHER_ADDR_LEN) != 0);
180a2ed165fSKevin Bowling }
181a2ed165fSKevin Bowling
182a2ed165fSKevin Bowling static bool
igb_iov_nack_allowed(struct igb_vf * vf)183a2ed165fSKevin Bowling igb_iov_nack_allowed(struct igb_vf *vf)
184a2ed165fSKevin Bowling {
185a2ed165fSKevin Bowling return (ratecheck(&vf->last_nack, &igb_iov_nack_interval) != 0);
186a2ed165fSKevin Bowling }
187a2ed165fSKevin Bowling
188e8f3b96bSKevin Bowling static void
igb_iov_reset_vlan_rate(struct igb_vf * vf)189e8f3b96bSKevin Bowling igb_iov_reset_vlan_rate(struct igb_vf *vf)
190e8f3b96bSKevin Bowling {
191e8f3b96bSKevin Bowling
192e8f3b96bSKevin Bowling vf->vlan_token_time = getsbinuptime();
193e8f3b96bSKevin Bowling vf->vlan_tokens = IGB_IOV_VLAN_CHANGE_BURST;
194e8f3b96bSKevin Bowling }
195e8f3b96bSKevin Bowling
196e8f3b96bSKevin Bowling static bool
igb_iov_vlan_add_allowed(struct igb_vf * vf)197e8f3b96bSKevin Bowling igb_iov_vlan_add_allowed(struct igb_vf *vf)
198e8f3b96bSKevin Bowling {
199e8f3b96bSKevin Bowling sbintime_t elapsed, now;
200e8f3b96bSKevin Bowling uint64_t refill;
201e8f3b96bSKevin Bowling
202e8f3b96bSKevin Bowling now = getsbinuptime();
203e8f3b96bSKevin Bowling elapsed = now - vf->vlan_token_time;
204e8f3b96bSKevin Bowling if (elapsed >= IGB_IOV_VLAN_CHANGE_INTERVAL) {
205e8f3b96bSKevin Bowling refill = elapsed / IGB_IOV_VLAN_CHANGE_INTERVAL;
206e8f3b96bSKevin Bowling vf->vlan_tokens = min((uint64_t)IGB_IOV_VLAN_CHANGE_BURST,
207e8f3b96bSKevin Bowling vf->vlan_tokens + refill);
208e8f3b96bSKevin Bowling vf->vlan_token_time = now;
209e8f3b96bSKevin Bowling }
210e8f3b96bSKevin Bowling if (vf->vlan_tokens == 0)
211e8f3b96bSKevin Bowling return (false);
212e8f3b96bSKevin Bowling vf->vlan_tokens--;
213e8f3b96bSKevin Bowling return (true);
214e8f3b96bSKevin Bowling }
215e8f3b96bSKevin Bowling
216a2ed165fSKevin Bowling static u32
igb_iov_reply_header(u32 request,bool cts,bool ack)217a2ed165fSKevin Bowling igb_iov_reply_header(u32 request, bool cts, bool ack)
218a2ed165fSKevin Bowling {
219a2ed165fSKevin Bowling u32 reply, type;
220a2ed165fSKevin Bowling
221a2ed165fSKevin Bowling type = request & 0xffff;
222a2ed165fSKevin Bowling if (type == E1000_VF_SET_MAC_ADDR &&
223a2ed165fSKevin Bowling (request & E1000_VT_MSGINFO_MASK) != 0)
224a2ed165fSKevin Bowling reply = request;
225a2ed165fSKevin Bowling else
226a2ed165fSKevin Bowling reply = type;
227a2ed165fSKevin Bowling reply &= ~(E1000_VT_MSGTYPE_ACK | E1000_VT_MSGTYPE_NACK |
228a2ed165fSKevin Bowling E1000_VT_MSGTYPE_CTS);
229a2ed165fSKevin Bowling if (cts)
230a2ed165fSKevin Bowling reply |= E1000_VT_MSGTYPE_CTS;
231a2ed165fSKevin Bowling reply |= ack ? E1000_VT_MSGTYPE_ACK : E1000_VT_MSGTYPE_NACK;
232a2ed165fSKevin Bowling return (reply);
233a2ed165fSKevin Bowling }
234a2ed165fSKevin Bowling
235a2ed165fSKevin Bowling bool
igb_iov_supported(const struct e1000_softc * sc)236a2ed165fSKevin Bowling igb_iov_supported(const struct e1000_softc *sc)
237a2ed165fSKevin Bowling {
238a2ed165fSKevin Bowling switch (sc->hw.mac.type) {
239a2ed165fSKevin Bowling case e1000_82576:
240a2ed165fSKevin Bowling case e1000_i350:
241a2ed165fSKevin Bowling return (true);
242a2ed165fSKevin Bowling default:
243a2ed165fSKevin Bowling return (false);
244a2ed165fSKevin Bowling }
245a2ed165fSKevin Bowling }
246a2ed165fSKevin Bowling
247a2ed165fSKevin Bowling bool
igb_iov_enabled(const struct e1000_softc * sc)248a2ed165fSKevin Bowling igb_iov_enabled(const struct e1000_softc *sc)
249a2ed165fSKevin Bowling {
250a2ed165fSKevin Bowling return (sc->num_vfs != 0);
251a2ed165fSKevin Bowling }
252a2ed165fSKevin Bowling
253a2ed165fSKevin Bowling int
igb_iov_attach(struct e1000_softc * sc)254a2ed165fSKevin Bowling igb_iov_attach(struct e1000_softc *sc)
255a2ed165fSKevin Bowling {
256a2ed165fSKevin Bowling nvlist_t *pf_schema, *vf_schema;
257a2ed165fSKevin Bowling int error, iov_pos;
258a2ed165fSKevin Bowling
259a2ed165fSKevin Bowling if (!igb_iov_supported(sc))
260a2ed165fSKevin Bowling return (0);
261a2ed165fSKevin Bowling if (pci_find_extcap(sc->dev, PCIZ_SRIOV, &iov_pos) != 0)
262a2ed165fSKevin Bowling return (0);
263a2ed165fSKevin Bowling
264a2ed165fSKevin Bowling pf_schema = pci_iov_schema_alloc_node();
265a2ed165fSKevin Bowling vf_schema = pci_iov_schema_alloc_node();
266a2ed165fSKevin Bowling pci_iov_schema_add_unicast_mac(vf_schema, "mac-addr", 0, NULL);
267a2ed165fSKevin Bowling pci_iov_schema_add_bool(vf_schema, "mac-anti-spoof",
268a2ed165fSKevin Bowling IOV_SCHEMA_HASDEFAULT, true);
269a2ed165fSKevin Bowling pci_iov_schema_add_bool(vf_schema, "allow-set-mac",
270a2ed165fSKevin Bowling IOV_SCHEMA_HASDEFAULT, false);
271a2ed165fSKevin Bowling pci_iov_schema_add_bool(vf_schema, "allow-promisc",
272a2ed165fSKevin Bowling IOV_SCHEMA_HASDEFAULT, false);
273a2ed165fSKevin Bowling pci_iov_schema_add_vlan(vf_schema, "vlan", IOV_SCHEMA_HASDEFAULT,
274a2ed165fSKevin Bowling VF_VLAN_TRUNK);
275a2ed165fSKevin Bowling
276a2ed165fSKevin Bowling error = pci_iov_attach(sc->dev, pf_schema, vf_schema);
277a2ed165fSKevin Bowling if (error != 0)
278a2ed165fSKevin Bowling device_printf(sc->dev,
279a2ed165fSKevin Bowling "failed to attach SR-IOV configuration interface: %d\n",
280a2ed165fSKevin Bowling error);
281a2ed165fSKevin Bowling else {
282a2ed165fSKevin Bowling callout_init(&sc->iov_mbx_retry, 1);
283a2ed165fSKevin Bowling sc->iov_mbx_retry_initialized = true;
284a2ed165fSKevin Bowling }
285a2ed165fSKevin Bowling return (error);
286a2ed165fSKevin Bowling }
287a2ed165fSKevin Bowling
288a2ed165fSKevin Bowling void
igb_iov_detach(struct e1000_softc * sc)289a2ed165fSKevin Bowling igb_iov_detach(struct e1000_softc *sc)
290a2ed165fSKevin Bowling {
291a2ed165fSKevin Bowling
292a2ed165fSKevin Bowling if (!sc->iov_mbx_retry_initialized)
293a2ed165fSKevin Bowling return;
294a2ed165fSKevin Bowling callout_drain(&sc->iov_mbx_retry);
295a2ed165fSKevin Bowling sc->iov_mbx_retry_initialized = false;
296a2ed165fSKevin Bowling }
297a2ed165fSKevin Bowling
298a2ed165fSKevin Bowling static u32
igb_iov_active_mask(struct e1000_softc * sc)299a2ed165fSKevin Bowling igb_iov_active_mask(struct e1000_softc *sc)
300a2ed165fSKevin Bowling {
301a2ed165fSKevin Bowling u32 mask;
302a2ed165fSKevin Bowling int i;
303a2ed165fSKevin Bowling
304a2ed165fSKevin Bowling mask = 0;
305a2ed165fSKevin Bowling for (i = 0; i < sc->num_vfs; i++)
306a2ed165fSKevin Bowling if (sc->vfs[i].flags & IGB_VF_ACTIVE)
307a2ed165fSKevin Bowling mask |= 1U << i;
308a2ed165fSKevin Bowling return (mask);
309a2ed165fSKevin Bowling }
310a2ed165fSKevin Bowling
311a2ed165fSKevin Bowling static void
igb_iov_map_rar(struct e1000_softc * sc,u16 rar,const u8 * mac,u16 pool)312a2ed165fSKevin Bowling igb_iov_map_rar(struct e1000_softc *sc, u16 rar, const u8 *mac, u16 pool)
313a2ed165fSKevin Bowling {
314a2ed165fSKevin Bowling struct e1000_hw *hw;
315a2ed165fSKevin Bowling u32 rah;
316a2ed165fSKevin Bowling
317a2ed165fSKevin Bowling hw = &sc->hw;
318a2ed165fSKevin Bowling e1000_rar_set(hw, __DECONST(u8 *, mac), rar);
319a2ed165fSKevin Bowling rah = E1000_READ_REG(hw, E1000_RAH(rar));
320a2ed165fSKevin Bowling rah &= ~IGB_IOV_RAH_POOLSEL_MASK;
321a2ed165fSKevin Bowling rah |= 1U << (IGB_IOV_RAH_POOLSEL_SHIFT + pool);
322a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_RAH(rar), rah);
323a2ed165fSKevin Bowling }
324a2ed165fSKevin Bowling
325a2ed165fSKevin Bowling static void
igb_iov_clear_rar(struct e1000_softc * sc,u16 rar)326a2ed165fSKevin Bowling igb_iov_clear_rar(struct e1000_softc *sc, u16 rar)
327a2ed165fSKevin Bowling {
328a2ed165fSKevin Bowling u8 zero[ETHER_ADDR_LEN] = {};
329a2ed165fSKevin Bowling
330a2ed165fSKevin Bowling e1000_rar_set(&sc->hw, zero, rar);
331a2ed165fSKevin Bowling }
332a2ed165fSKevin Bowling
333a2ed165fSKevin Bowling static void
igb_iov_clear_mac_filters(struct e1000_softc * sc,const struct igb_vf * vf)334a2ed165fSKevin Bowling igb_iov_clear_mac_filters(struct e1000_softc *sc, const struct igb_vf *vf)
335a2ed165fSKevin Bowling {
336a2ed165fSKevin Bowling struct igb_vf_mac_filter *filter;
337a2ed165fSKevin Bowling int i;
338a2ed165fSKevin Bowling
339a2ed165fSKevin Bowling for (i = 0; i < sc->num_vf_mac_filters; i++) {
340a2ed165fSKevin Bowling filter = &sc->vf_mac_filters[i];
341a2ed165fSKevin Bowling if (!filter->active || filter->pool != vf->pool)
342a2ed165fSKevin Bowling continue;
343a2ed165fSKevin Bowling igb_iov_clear_rar(sc, filter->rar_index);
344a2ed165fSKevin Bowling filter->active = false;
345a2ed165fSKevin Bowling memset(filter->mac, 0, sizeof(filter->mac));
346a2ed165fSKevin Bowling }
347a2ed165fSKevin Bowling }
348a2ed165fSKevin Bowling
349a2ed165fSKevin Bowling static u32
igb_iov_switch_reg(struct e1000_softc * sc)350a2ed165fSKevin Bowling igb_iov_switch_reg(struct e1000_softc *sc)
351a2ed165fSKevin Bowling {
352a2ed165fSKevin Bowling return (sc->hw.mac.type == e1000_82576 ?
353a2ed165fSKevin Bowling E1000_DTXSWC : E1000_TXSWC);
354a2ed165fSKevin Bowling }
355a2ed165fSKevin Bowling
356a2ed165fSKevin Bowling static void
igb_iov_set_anti_spoof(struct e1000_softc * sc,struct igb_vf * vf)357a2ed165fSKevin Bowling igb_iov_set_anti_spoof(struct e1000_softc *sc, struct igb_vf *vf)
358a2ed165fSKevin Bowling {
359a2ed165fSKevin Bowling struct e1000_hw *hw;
360a2ed165fSKevin Bowling u32 reg, value;
361a2ed165fSKevin Bowling
362a2ed165fSKevin Bowling hw = &sc->hw;
363a2ed165fSKevin Bowling reg = igb_iov_switch_reg(sc);
364a2ed165fSKevin Bowling value = E1000_READ_REG(hw, reg);
365a2ed165fSKevin Bowling value &= ~((1U << vf->pool) |
366a2ed165fSKevin Bowling (1U << (vf->pool + E1000_DTXSWC_VLAN_SPOOF_SHIFT)));
367a2ed165fSKevin Bowling if (vf->flags & IGB_VF_MAC_ANTI_SPOOF)
368a2ed165fSKevin Bowling value |= 1U << vf->pool;
369a2ed165fSKevin Bowling if (vf->flags & IGB_VF_ACTIVE)
370a2ed165fSKevin Bowling value |= 1U <<
371a2ed165fSKevin Bowling (vf->pool + E1000_DTXSWC_VLAN_SPOOF_SHIFT);
372a2ed165fSKevin Bowling E1000_WRITE_REG(hw, reg, value);
373a2ed165fSKevin Bowling }
374a2ed165fSKevin Bowling
375a2ed165fSKevin Bowling static void
igb_iov_set_uta(struct e1000_softc * sc)376a2ed165fSKevin Bowling igb_iov_set_uta(struct e1000_softc *sc)
377a2ed165fSKevin Bowling {
378a2ed165fSKevin Bowling struct e1000_hw *hw;
379a2ed165fSKevin Bowling bool enable;
380a2ed165fSKevin Bowling int i;
381a2ed165fSKevin Bowling
382a2ed165fSKevin Bowling if (!igb_iov_enabled(sc) || sc->hw.mac.type != e1000_82576)
383a2ed165fSKevin Bowling return;
384a2ed165fSKevin Bowling
385a2ed165fSKevin Bowling hw = &sc->hw;
386a2ed165fSKevin Bowling enable = (E1000_READ_REG(hw, E1000_VMOLR(sc->pool)) &
387a2ed165fSKevin Bowling E1000_VMOLR_ROPE) != 0;
388a2ed165fSKevin Bowling for (i = 0; i < sc->num_vfs; i++)
389a2ed165fSKevin Bowling if ((sc->vfs[i].flags &
390a2ed165fSKevin Bowling (IGB_VF_ACTIVE | IGB_VF_UCAST_PROMISC)) ==
391a2ed165fSKevin Bowling (IGB_VF_ACTIVE | IGB_VF_UCAST_PROMISC)) {
392a2ed165fSKevin Bowling enable = true;
393a2ed165fSKevin Bowling break;
394a2ed165fSKevin Bowling }
395a2ed165fSKevin Bowling
396a2ed165fSKevin Bowling for (i = 0; i < MAX_MTA_REG; i++)
397a2ed165fSKevin Bowling E1000_WRITE_REG_ARRAY(hw, E1000_UTA, i,
398a2ed165fSKevin Bowling enable ? 0xffffffffU : 0);
399a2ed165fSKevin Bowling }
400a2ed165fSKevin Bowling
401a2ed165fSKevin Bowling static void
igb_iov_configure_dvmolr(struct e1000_softc * sc,u16 pool,bool strip_vlan,bool hide_vlan,bool vf_pool)402a2ed165fSKevin Bowling igb_iov_configure_dvmolr(struct e1000_softc *sc, u16 pool,
403a2ed165fSKevin Bowling bool strip_vlan, bool hide_vlan, bool vf_pool)
404a2ed165fSKevin Bowling {
405a2ed165fSKevin Bowling struct e1000_hw *hw;
406a2ed165fSKevin Bowling u32 dvmolr;
407a2ed165fSKevin Bowling
408a2ed165fSKevin Bowling hw = &sc->hw;
409a2ed165fSKevin Bowling if (hw->mac.type != e1000_i350)
410a2ed165fSKevin Bowling return;
411a2ed165fSKevin Bowling
412a2ed165fSKevin Bowling dvmolr = E1000_READ_REG(hw, E1000_DVMOLR(pool));
413a2ed165fSKevin Bowling dvmolr &= ~(E1000_DVMOLR_HIDVLAN | E1000_DVMOLR_STRVLAN |
414a2ed165fSKevin Bowling E1000_DVMOLR_STRCRC);
415a2ed165fSKevin Bowling if (hide_vlan)
416a2ed165fSKevin Bowling dvmolr |= E1000_DVMOLR_HIDVLAN;
417a2ed165fSKevin Bowling if (strip_vlan)
418a2ed165fSKevin Bowling dvmolr |= E1000_DVMOLR_STRVLAN;
419a2ed165fSKevin Bowling if (vf_pool || strip_vlan ||
420a2ed165fSKevin Bowling (E1000_READ_REG(hw, E1000_RCTL) & E1000_RCTL_SECRC) != 0)
421a2ed165fSKevin Bowling dvmolr |= E1000_DVMOLR_STRCRC;
422a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_DVMOLR(pool), dvmolr);
423a2ed165fSKevin Bowling }
424a2ed165fSKevin Bowling
425a2ed165fSKevin Bowling static void
igb_iov_configure_vmolr(struct e1000_softc * sc,struct igb_vf * vf)426a2ed165fSKevin Bowling igb_iov_configure_vmolr(struct e1000_softc *sc, struct igb_vf *vf)
427a2ed165fSKevin Bowling {
428a2ed165fSKevin Bowling struct e1000_hw *hw;
429a2ed165fSKevin Bowling u32 max_frame_size, vmolr, vmvir;
430a2ed165fSKevin Bowling
431a2ed165fSKevin Bowling hw = &sc->hw;
432a2ed165fSKevin Bowling max_frame_size = vf->max_frame_size;
433a2ed165fSKevin Bowling if (vf->vlan_count != 0)
434a2ed165fSKevin Bowling max_frame_size = min(max_frame_size + VLAN_TAG_SIZE,
435a2ed165fSKevin Bowling IGB_IOV_MAX_FRAME_SIZE);
436a2ed165fSKevin Bowling vmolr = E1000_READ_REG(hw, E1000_VMOLR(vf->pool));
437a2ed165fSKevin Bowling vmolr &= ~(E1000_VMOLR_RLPML_MASK | E1000_VMOLR_RSSE |
438a2ed165fSKevin Bowling E1000_VMOLR_VPE | E1000_VMOLR_UPE | E1000_VMOLR_ROMPE |
439a2ed165fSKevin Bowling E1000_VMOLR_ROPE | E1000_VMOLR_MPME | E1000_VMOLR_STRVLAN);
440a2ed165fSKevin Bowling vmolr |= E1000_VMOLR_BAM | E1000_VMOLR_LPE |
441a2ed165fSKevin Bowling (max_frame_size & E1000_VMOLR_RLPML_MASK);
442a2ed165fSKevin Bowling if (vf->default_vlan == 0)
443a2ed165fSKevin Bowling vmolr |= E1000_VMOLR_AUPE;
444a2ed165fSKevin Bowling if (vf->mc_count != 0 &&
445a2ed165fSKevin Bowling (vf->flags & (IGB_VF_MCAST_PROMISC |
446a2ed165fSKevin Bowling IGB_VF_MCAST_OVERFLOW)) == 0)
447a2ed165fSKevin Bowling vmolr |= E1000_VMOLR_ROMPE;
448a2ed165fSKevin Bowling if (hw->mac.type == e1000_82576)
449a2ed165fSKevin Bowling vmolr |= IGB_82576_VMOLR_RSV;
450a2ed165fSKevin Bowling
451a2ed165fSKevin Bowling if (vf->flags & IGB_VF_UCAST_PROMISC) {
452a2ed165fSKevin Bowling if (hw->mac.type == e1000_82576)
453a2ed165fSKevin Bowling vmolr |= E1000_VMOLR_ROPE;
454a2ed165fSKevin Bowling else
455a2ed165fSKevin Bowling vmolr |= E1000_VMOLR_UPE;
456a2ed165fSKevin Bowling }
457a2ed165fSKevin Bowling /*
458a2ed165fSKevin Bowling * The mailbox can describe only 30 hashes. Fall back to receiving all
459a2ed165fSKevin Bowling * multicast within the VF's VLAN membership when that list overflows.
460a2ed165fSKevin Bowling */
461a2ed165fSKevin Bowling if ((vf->flags & (IGB_VF_MCAST_PROMISC |
462a2ed165fSKevin Bowling IGB_VF_MCAST_OVERFLOW)) != 0)
463a2ed165fSKevin Bowling vmolr |= E1000_VMOLR_MPME;
464a2ed165fSKevin Bowling if (hw->mac.type == e1000_82576 && vf->vlan_count != 0)
465a2ed165fSKevin Bowling vmolr |= E1000_VMOLR_STRVLAN;
466a2ed165fSKevin Bowling /* A nonzero default VLAN makes this VF an untagged access port. */
467a2ed165fSKevin Bowling if (vf->default_vlan == 0)
468a2ed165fSKevin Bowling vmvir = 0;
469a2ed165fSKevin Bowling else
470a2ed165fSKevin Bowling vmvir = vf->default_vlan | E1000_VMVIR_VLANA_DEFAULT;
471a2ed165fSKevin Bowling
472a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_VMOLR(vf->pool), vmolr);
473a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_VMVIR(vf->pool), vmvir);
474a2ed165fSKevin Bowling igb_iov_configure_dvmolr(sc, vf->pool, vf->vlan_count != 0,
475a2ed165fSKevin Bowling vf->default_vlan != 0, true);
476a2ed165fSKevin Bowling }
477a2ed165fSKevin Bowling
478a2ed165fSKevin Bowling static void
igb_iov_configure_pf_vmolr(struct e1000_softc * sc)479a2ed165fSKevin Bowling igb_iov_configure_pf_vmolr(struct e1000_softc *sc)
480a2ed165fSKevin Bowling {
481a2ed165fSKevin Bowling struct e1000_hw *hw;
482a2ed165fSKevin Bowling if_t ifp;
483a2ed165fSKevin Bowling bool strip_vlan;
484a2ed165fSKevin Bowling u32 max_frame_size;
485a2ed165fSKevin Bowling u32 old_vmolr, vmolr;
486a2ed165fSKevin Bowling
487a2ed165fSKevin Bowling hw = &sc->hw;
488a2ed165fSKevin Bowling ifp = iflib_get_ifp(sc->ctx);
489a2ed165fSKevin Bowling max_frame_size = min(sc->shared->isc_max_frame_size + VLAN_TAG_SIZE,
490a2ed165fSKevin Bowling IGB_IOV_MAX_FRAME_SIZE);
491a2ed165fSKevin Bowling strip_vlan = (E1000_READ_REG(hw, E1000_CTRL) & E1000_CTRL_VME) != 0;
492a2ed165fSKevin Bowling old_vmolr = E1000_READ_REG(hw, E1000_VMOLR(sc->pool));
493a2ed165fSKevin Bowling vmolr = E1000_VMOLR_BAM | E1000_VMOLR_AUPE |
494a2ed165fSKevin Bowling E1000_VMOLR_LPE |
495a2ed165fSKevin Bowling (max_frame_size & E1000_VMOLR_RLPML_MASK);
496a2ed165fSKevin Bowling if (hw->mac.type == e1000_82576) {
497a2ed165fSKevin Bowling vmolr |= IGB_82576_VMOLR_RSV;
498a2ed165fSKevin Bowling if (strip_vlan)
499a2ed165fSKevin Bowling vmolr |= E1000_VMOLR_STRVLAN;
500a2ed165fSKevin Bowling } else
501a2ed165fSKevin Bowling vmolr |= old_vmolr & E1000_VMOLR_VPE;
502a2ed165fSKevin Bowling
503a2ed165fSKevin Bowling if (if_getflags(ifp) & IFF_PROMISC) {
504a2ed165fSKevin Bowling if (hw->mac.type == e1000_82576)
505a2ed165fSKevin Bowling vmolr |= E1000_VMOLR_ROPE;
506a2ed165fSKevin Bowling else
507a2ed165fSKevin Bowling vmolr |= E1000_VMOLR_UPE | E1000_VMOLR_VPE;
508a2ed165fSKevin Bowling vmolr |= E1000_VMOLR_MPME;
509a2ed165fSKevin Bowling } else if ((if_getflags(ifp) & IFF_ALLMULTI) != 0 ||
510a2ed165fSKevin Bowling if_llmaddr_count(ifp) >= MAX_NUM_MULTICAST_ADDRESSES)
511a2ed165fSKevin Bowling vmolr |= E1000_VMOLR_MPME;
512a2ed165fSKevin Bowling else if (if_llmaddr_count(ifp) != 0)
513a2ed165fSKevin Bowling vmolr |= E1000_VMOLR_ROMPE;
514a2ed165fSKevin Bowling
515a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_VMOLR(sc->pool), vmolr);
516a2ed165fSKevin Bowling igb_iov_configure_dvmolr(sc, sc->pool, strip_vlan, false, false);
517a2ed165fSKevin Bowling }
518a2ed165fSKevin Bowling
519a2ed165fSKevin Bowling void
igb_iov_update_pf_vmolr(struct e1000_softc * sc)520a2ed165fSKevin Bowling igb_iov_update_pf_vmolr(struct e1000_softc *sc)
521a2ed165fSKevin Bowling {
522a2ed165fSKevin Bowling if (!igb_iov_enabled(sc))
523a2ed165fSKevin Bowling return;
524a2ed165fSKevin Bowling
525a2ed165fSKevin Bowling igb_iov_configure_pf_vmolr(sc);
526a2ed165fSKevin Bowling igb_iov_set_uta(sc);
527a2ed165fSKevin Bowling }
528a2ed165fSKevin Bowling
529a2ed165fSKevin Bowling u32
igb_iov_intr_mask(const struct e1000_softc * sc)530a2ed165fSKevin Bowling igb_iov_intr_mask(const struct e1000_softc *sc)
531a2ed165fSKevin Bowling {
532a2ed165fSKevin Bowling if (!sc->iov_hw_active)
533a2ed165fSKevin Bowling return (0);
534a2ed165fSKevin Bowling return (E1000_IMS_VMMB | E1000_IMS_MDDET);
535a2ed165fSKevin Bowling }
536a2ed165fSKevin Bowling
537176259efSKevin Bowling void
igb_iov_intr_drain_stale(struct e1000_softc * sc)538176259efSKevin Bowling igb_iov_intr_drain_stale(struct e1000_softc *sc)
539176259efSKevin Bowling {
540176259efSKevin Bowling struct e1000_hw *hw;
541176259efSKevin Bowling u32 icr;
542176259efSKevin Bowling
543176259efSKevin Bowling if (atomic_readandclear_32(&sc->iov_intr_drain_pending) == 0)
544176259efSKevin Bowling return;
545176259efSKevin Bowling hw = &sc->hw;
546176259efSKevin Bowling /*
547176259efSKevin Bowling * Consume setup-time diagnostic state at the actual transition from
548176259efSKevin Bowling * masked to armed. Read ICR last so an event arriving after the drain
549176259efSKevin Bowling * remains pending and is delivered when the caller enables MDDET.
550176259efSKevin Bowling */
551176259efSKevin Bowling (void)E1000_READ_REG(hw, E1000_LVMMC);
552176259efSKevin Bowling if (hw->mac.type == e1000_82576)
553176259efSKevin Bowling (void)E1000_READ_REG(hw, E1000_WVBR);
554176259efSKevin Bowling icr = E1000_READ_REG(hw, E1000_ICR);
555176259efSKevin Bowling /*
556176259efSKevin Bowling * em_if_init() injects LSC after IOV setup to close the post-reset
557176259efSKevin Bowling * link race. Preserve that cause across this MDDET-specific drain.
558176259efSKevin Bowling */
559176259efSKevin Bowling if (__predict_true(icr != 0xffffffff) &&
560176259efSKevin Bowling (icr & E1000_ICR_LSC) != 0)
561176259efSKevin Bowling E1000_WRITE_REG(hw, E1000_ICS, E1000_ICS_LSC);
562176259efSKevin Bowling }
563176259efSKevin Bowling
564a2ed165fSKevin Bowling static void
igb_iov_vfta_shadow_invalidate(struct e1000_softc * sc)5655ce6c94fSKevin Bowling igb_iov_vfta_shadow_invalidate(struct e1000_softc *sc)
5665ce6c94fSKevin Bowling {
5675ce6c94fSKevin Bowling
5685ce6c94fSKevin Bowling /*
5695ce6c94fSKevin Bowling * I350 erratum 20 makes VFTA reads unreliable while VMDq loopback or
5705ce6c94fSKevin Bowling * anti-spoofing is active. The shadow is therefore authoritative
5715ce6c94fSKevin Bowling * until a reset or another independent hardware writer invalidates
5725ce6c94fSKevin Bowling * it. Readback cannot reliably audit a stale-but-valid shadow on
5735ce6c94fSKevin Bowling * this part, so keep all shadow mutation in these two helpers.
5745ce6c94fSKevin Bowling */
5755ce6c94fSKevin Bowling memset(sc->iov_vfta, 0, sizeof(sc->iov_vfta));
5765ce6c94fSKevin Bowling sc->iov_vfta_valid = false;
5775ce6c94fSKevin Bowling }
5785ce6c94fSKevin Bowling
5795ce6c94fSKevin Bowling static void
igb_iov_vfta_shadow_store(struct e1000_softc * sc,const u32 * vfta)5805ce6c94fSKevin Bowling igb_iov_vfta_shadow_store(struct e1000_softc *sc, const u32 *vfta)
5815ce6c94fSKevin Bowling {
5825ce6c94fSKevin Bowling
5835ce6c94fSKevin Bowling memcpy(sc->iov_vfta, vfta, sizeof(sc->iov_vfta));
5845ce6c94fSKevin Bowling sc->iov_vfta_valid = true;
5855ce6c94fSKevin Bowling }
5865ce6c94fSKevin Bowling
5875ce6c94fSKevin Bowling static void
igb_iov_notify_vfs_reset(struct e1000_softc * sc)588a2ed165fSKevin Bowling igb_iov_notify_vfs_reset(struct e1000_softc *sc)
589a2ed165fSKevin Bowling {
590a2ed165fSKevin Bowling struct igb_vf *vf;
591a2ed165fSKevin Bowling struct e1000_hw *hw;
592a2ed165fSKevin Bowling sbintime_t deadline;
593a2ed165fSKevin Bowling u32 msg, pending, undelivered;
594a2ed165fSKevin Bowling int i;
595a2ed165fSKevin Bowling
596a2ed165fSKevin Bowling hw = &sc->hw;
597a2ed165fSKevin Bowling /*
598a2ed165fSKevin Bowling * Process VFLRs first and wait only for VFs that completed their
599a2ed165fSKevin Bowling * mailbox handshake. An unattached VF has nobody who can acknowledge.
600a2ed165fSKevin Bowling */
601a2ed165fSKevin Bowling igb_iov_handle_mbx(sc);
602a2ed165fSKevin Bowling pending = 0;
603a2ed165fSKevin Bowling for (i = 0; i < sc->num_vfs; i++) {
604a2ed165fSKevin Bowling vf = &sc->vfs[i];
605a2ed165fSKevin Bowling if ((vf->flags & (IGB_VF_ACTIVE | IGB_VF_CTS)) ==
606a2ed165fSKevin Bowling (IGB_VF_ACTIVE | IGB_VF_CTS))
607a2ed165fSKevin Bowling pending |= 1U << i;
608a2ed165fSKevin Bowling }
609a2ed165fSKevin Bowling if (pending == 0)
610a2ed165fSKevin Bowling return;
611a2ed165fSKevin Bowling
612a2ed165fSKevin Bowling /*
613a2ed165fSKevin Bowling * I350 SDM section 4.6.11.2.3 requires each VF to acknowledge a
614a2ed165fSKevin Bowling * mailbox warning before the PF asserts CTRL.RST.
615a2ed165fSKevin Bowling *
616a2ed165fSKevin Bowling * The mailbox pass above drained requests and stale acknowledgements.
617a2ed165fSKevin Bowling * A VF read of the new notification sets its ACK bit.
618a2ed165fSKevin Bowling */
619a2ed165fSKevin Bowling undelivered = 0;
620a2ed165fSKevin Bowling for (i = 0; i < sc->num_vfs; i++) {
621a2ed165fSKevin Bowling if ((pending & (1U << i)) == 0)
622a2ed165fSKevin Bowling continue;
623a2ed165fSKevin Bowling msg = E1000_PF_CONTROL_MSG;
624a2ed165fSKevin Bowling if (e1000_write_mbx(hw, &msg, 1, i) != 0) {
625a2ed165fSKevin Bowling undelivered |= 1U << i;
626a2ed165fSKevin Bowling pending &= ~(1U << i);
627a2ed165fSKevin Bowling }
628a2ed165fSKevin Bowling }
629a2ed165fSKevin Bowling if (undelivered != 0)
630a2ed165fSKevin Bowling device_printf(sc->dev,
631a2ed165fSKevin Bowling "could not deliver reset warning to VF mask %#x\n",
632a2ed165fSKevin Bowling undelivered);
633a2ed165fSKevin Bowling
634a2ed165fSKevin Bowling deadline = getsbinuptime() + IGB_I350_RESET_ACK_TIMEOUT;
635a2ed165fSKevin Bowling while (pending != 0 && getsbinuptime() < deadline) {
636a2ed165fSKevin Bowling for (i = 0; i < sc->num_vfs; i++) {
637a2ed165fSKevin Bowling if ((pending & (1U << i)) != 0 &&
638a2ed165fSKevin Bowling e1000_check_for_ack(hw, i) == 0)
639a2ed165fSKevin Bowling pending &= ~(1U << i);
640a2ed165fSKevin Bowling }
641a2ed165fSKevin Bowling if (pending != 0)
642a2ed165fSKevin Bowling pause_sbt("igback", SBT_1MS, 0, C_HARDCLOCK);
643a2ed165fSKevin Bowling }
644a2ed165fSKevin Bowling if (pending != 0)
645a2ed165fSKevin Bowling device_printf(sc->dev,
646a2ed165fSKevin Bowling "VF reset acknowledgement timed out for mask %#x\n",
647a2ed165fSKevin Bowling pending);
648a2ed165fSKevin Bowling }
649a2ed165fSKevin Bowling
650a2ed165fSKevin Bowling void
igb_iov_reset_prepare(struct e1000_softc * sc)651a2ed165fSKevin Bowling igb_iov_reset_prepare(struct e1000_softc *sc)
652a2ed165fSKevin Bowling {
653a2ed165fSKevin Bowling struct e1000_hw *hw;
654a2ed165fSKevin Bowling u32 mask;
655a2ed165fSKevin Bowling
656a2ed165fSKevin Bowling if (sc->iov_hw_active) {
657a2ed165fSKevin Bowling hw = &sc->hw;
658a2ed165fSKevin Bowling if (atomic_load_acq_32(&sc->iov_teardown) == 0) {
659a2ed165fSKevin Bowling if (hw->mac.type == e1000_i350)
660a2ed165fSKevin Bowling igb_iov_notify_vfs_reset(sc);
661a2ed165fSKevin Bowling else
662a2ed165fSKevin Bowling igb_iov_ping_all_vfs(sc);
663a2ed165fSKevin Bowling }
664a2ed165fSKevin Bowling
665a2ed165fSKevin Bowling /* Stop VF DMA before the PF asserts CTRL.RST. */
666a2ed165fSKevin Bowling mask = 1U << sc->pool;
667a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_VFRE, mask);
668a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_VFTE, mask);
669a2ed165fSKevin Bowling E1000_WRITE_FLUSH(hw);
670a2ed165fSKevin Bowling }
671a2ed165fSKevin Bowling sc->iov_hw_active = false;
672a2ed165fSKevin Bowling if (sc->iov_mbx_retry_initialized)
673a2ed165fSKevin Bowling callout_stop(&sc->iov_mbx_retry);
674350211abSKevin Bowling sc->iov_mta_valid = false;
6755ce6c94fSKevin Bowling igb_iov_vfta_shadow_invalidate(sc);
676a2ed165fSKevin Bowling atomic_readandclear_32(&sc->iov_mdd_cause);
677a2ed165fSKevin Bowling atomic_readandclear_32(&sc->iov_pending);
678a2ed165fSKevin Bowling atomic_readandclear_32(&sc->iov_spoof_pending);
679ac2be06eSKevin Bowling atomic_readandclear_32(&sc->iov_blocked_pending);
6801c91c3aeSKevin Bowling /*
6811c91c3aeSKevin Bowling * Normal iflib initialization prepares the reset before
6821c91c3aeSKevin Bowling * igb_iov_initialize() requests this drain. Preserve a still-pending
6831c91c3aeSKevin Bowling * I350 request across a later stop or repeated preparation so the next
6841c91c3aeSKevin Bowling * interrupt arm consumes it. Other families retain the ordinary
6851c91c3aeSKevin Bowling * stop-time cleanup.
6861c91c3aeSKevin Bowling */
6871c91c3aeSKevin Bowling if (sc->hw.mac.type != e1000_i350)
688176259efSKevin Bowling atomic_readandclear_32(&sc->iov_intr_drain_pending);
689a2ed165fSKevin Bowling }
690a2ed165fSKevin Bowling
691a2ed165fSKevin Bowling void
igb_iov_rebuild_mta(struct e1000_softc * sc)692a2ed165fSKevin Bowling igb_iov_rebuild_mta(struct e1000_softc *sc)
693a2ed165fSKevin Bowling {
694a2ed165fSKevin Bowling struct e1000_hw *hw;
695a2ed165fSKevin Bowling struct igb_vf *vf;
696350211abSKevin Bowling u32 hash_bit, hash_reg, hash_value;
697350211abSKevin Bowling u32 mta[MAX_MTA_REG] = {};
698a2ed165fSKevin Bowling u16 hash;
699350211abSKevin Bowling bool changed;
700a2ed165fSKevin Bowling int i, j, mcnt;
701a2ed165fSKevin Bowling
702a2ed165fSKevin Bowling if (!igb_iov_enabled(sc))
703a2ed165fSKevin Bowling return;
704a2ed165fSKevin Bowling
705a2ed165fSKevin Bowling hw = &sc->hw;
706a2ed165fSKevin Bowling memset(sc->mta, 0,
707a2ed165fSKevin Bowling ETHER_ADDR_LEN * MAX_NUM_MULTICAST_ADDRESSES);
708a2ed165fSKevin Bowling mcnt = if_foreach_llmaddr(iflib_get_ifp(sc->ctx),
709a2ed165fSKevin Bowling igb_iov_copy_maddr, sc->mta);
710350211abSKevin Bowling mcnt = min(mcnt, MAX_NUM_MULTICAST_ADDRESSES);
711350211abSKevin Bowling for (i = 0; i < mcnt; i++) {
712350211abSKevin Bowling hash_value = e1000_hash_mc_addr(hw,
713350211abSKevin Bowling &sc->mta[i * ETHER_ADDR_LEN]);
714350211abSKevin Bowling hash_reg = (hash_value >> 5) &
715350211abSKevin Bowling (hw->mac.mta_reg_count - 1);
716350211abSKevin Bowling hash_bit = hash_value & 0x1f;
717350211abSKevin Bowling mta[hash_reg] |= 1U << hash_bit;
718350211abSKevin Bowling }
719a2ed165fSKevin Bowling for (i = 0; i < sc->num_vfs; i++) {
720a2ed165fSKevin Bowling vf = &sc->vfs[i];
721a2ed165fSKevin Bowling if (!(vf->flags & IGB_VF_ACTIVE))
722a2ed165fSKevin Bowling continue;
723a2ed165fSKevin Bowling for (j = 0; j < vf->mc_count; j++) {
724a2ed165fSKevin Bowling hash = vf->mc_hashes[j] & 0xfff;
725350211abSKevin Bowling mta[(hash >> 5) & (hw->mac.mta_reg_count - 1)] |=
726350211abSKevin Bowling 1U << (hash & 0x1f);
727a2ed165fSKevin Bowling }
728a2ed165fSKevin Bowling }
729350211abSKevin Bowling
730350211abSKevin Bowling changed = false;
731350211abSKevin Bowling for (i = hw->mac.mta_reg_count - 1; i >= 0; i--) {
732350211abSKevin Bowling if (sc->iov_mta_valid && hw->mac.mta_shadow[i] == mta[i])
733350211abSKevin Bowling continue;
734350211abSKevin Bowling hw->mac.mta_shadow[i] = mta[i];
735350211abSKevin Bowling E1000_WRITE_REG_ARRAY(hw, E1000_MTA, i, mta[i]);
736350211abSKevin Bowling changed = true;
737350211abSKevin Bowling }
738350211abSKevin Bowling if (changed)
739350211abSKevin Bowling E1000_WRITE_FLUSH(hw);
740350211abSKevin Bowling sc->iov_mta_valid = true;
741a2ed165fSKevin Bowling }
742a2ed165fSKevin Bowling
743a2ed165fSKevin Bowling static int
igb_iov_vlvf_add(u32 * vlvf,const u32 * old_vlvf,u16 vid,u16 pool,bool preserve_only)744a2ed165fSKevin Bowling igb_iov_vlvf_add(u32 *vlvf, const u32 *old_vlvf, u16 vid, u16 pool,
745a2ed165fSKevin Bowling bool preserve_only)
746a2ed165fSKevin Bowling {
747a2ed165fSKevin Bowling int free_slot, i;
748a2ed165fSKevin Bowling
749a2ed165fSKevin Bowling free_slot = -1;
750a2ed165fSKevin Bowling for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
751a2ed165fSKevin Bowling if ((vlvf[i] & E1000_VLVF_VLANID_ENABLE) != 0 &&
752a2ed165fSKevin Bowling (vlvf[i] & E1000_VLVF_VLANID_MASK) == vid) {
753a2ed165fSKevin Bowling vlvf[i] |= 1U << (E1000_VLVF_POOLSEL_SHIFT + pool);
754a2ed165fSKevin Bowling return (0);
755a2ed165fSKevin Bowling }
756a2ed165fSKevin Bowling if (free_slot == -1 &&
757a2ed165fSKevin Bowling (vlvf[i] & E1000_VLVF_VLANID_ENABLE) == 0)
758a2ed165fSKevin Bowling free_slot = i;
759a2ed165fSKevin Bowling }
760a2ed165fSKevin Bowling for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++)
761a2ed165fSKevin Bowling if ((old_vlvf[i] & E1000_VLVF_VLANID_ENABLE) != 0 &&
762a2ed165fSKevin Bowling (old_vlvf[i] & E1000_VLVF_VLANID_MASK) == vid &&
763a2ed165fSKevin Bowling (vlvf[i] & E1000_VLVF_VLANID_ENABLE) == 0) {
764a2ed165fSKevin Bowling free_slot = i;
765a2ed165fSKevin Bowling break;
766a2ed165fSKevin Bowling }
767a2ed165fSKevin Bowling if (preserve_only && (i == E1000_VLVF_ARRAY_SIZE))
768a2ed165fSKevin Bowling return (ENOENT);
769a2ed165fSKevin Bowling if (free_slot == -1)
770a2ed165fSKevin Bowling return (ENOSPC);
771a2ed165fSKevin Bowling
772a2ed165fSKevin Bowling vlvf[free_slot] = E1000_VLVF_VLANID_ENABLE | vid |
773a2ed165fSKevin Bowling (1U << (E1000_VLVF_POOLSEL_SHIFT + pool));
774a2ed165fSKevin Bowling return (0);
775a2ed165fSKevin Bowling }
776a2ed165fSKevin Bowling
777a2ed165fSKevin Bowling void
igb_iov_rebuild_vlan(struct e1000_softc * sc)778a2ed165fSKevin Bowling igb_iov_rebuild_vlan(struct e1000_softc *sc)
779a2ed165fSKevin Bowling {
780a2ed165fSKevin Bowling struct e1000_hw *hw;
781a2ed165fSKevin Bowling struct igb_vf *vf;
782a2ed165fSKevin Bowling u32 old_vlvf[E1000_VLVF_ARRAY_SIZE];
7835ce6c94fSKevin Bowling u32 effective_vfta[EM_VFTA_SIZE], vfta[EM_VFTA_SIZE];
7845ce6c94fSKevin Bowling u32 vlvf[E1000_VLVF_ARRAY_SIZE];
785a2ed165fSKevin Bowling u32 old_vfta, rctl, vmolr;
7865ce6c94fSKevin Bowling bool force_vfta, pf_overflow, pf_vlan_promisc, preserve_pf;
7875ce6c94fSKevin Bowling bool vfta_changed, vlvf_changed;
788a2ed165fSKevin Bowling int i, vid;
789a2ed165fSKevin Bowling
790a2ed165fSKevin Bowling if (!igb_iov_enabled(sc))
791a2ed165fSKevin Bowling return;
792a2ed165fSKevin Bowling
793a2ed165fSKevin Bowling hw = &sc->hw;
794a2ed165fSKevin Bowling rctl = E1000_READ_REG(hw, E1000_RCTL);
795a2ed165fSKevin Bowling rctl &= ~E1000_RCTL_CFIEN;
796a2ed165fSKevin Bowling rctl |= E1000_RCTL_VFE;
797a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_RCTL, rctl);
798a2ed165fSKevin Bowling memcpy(vfta, sc->shadow_vfta, sizeof(vfta));
799a2ed165fSKevin Bowling memset(vlvf, 0, sizeof(vlvf));
800a2ed165fSKevin Bowling for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++)
801a2ed165fSKevin Bowling old_vlvf[i] = E1000_READ_REG(hw, E1000_VLVF(i));
802a2ed165fSKevin Bowling
803a2ed165fSKevin Bowling pf_vlan_promisc = igb_iov_pf_vlan_promisc(sc);
804a2ed165fSKevin Bowling pf_overflow = !pf_vlan_promisc && hw->mac.type == e1000_i350 &&
805a2ed165fSKevin Bowling igb_iov_vlan_unique_count(sc, true) > E1000_VLVF_ARRAY_SIZE;
806a2ed165fSKevin Bowling preserve_pf = !pf_vlan_promisc && !pf_overflow;
807a2ed165fSKevin Bowling
808a2ed165fSKevin Bowling /* First keep every surviving VF mapping in its current slot. */
809a2ed165fSKevin Bowling for (i = 0; i < sc->num_vfs; i++) {
810a2ed165fSKevin Bowling vf = &sc->vfs[i];
811a2ed165fSKevin Bowling if (!(vf->flags & IGB_VF_ACTIVE))
812a2ed165fSKevin Bowling continue;
813a2ed165fSKevin Bowling for (vid = 0; vid < 4096; vid++) {
814a2ed165fSKevin Bowling if ((vf->vlans[vid >> 5] & (1U << (vid & 0x1f))) ==
815a2ed165fSKevin Bowling 0)
816a2ed165fSKevin Bowling continue;
817a2ed165fSKevin Bowling (void)igb_iov_vlvf_add(vlvf, old_vlvf, vid,
818a2ed165fSKevin Bowling vf->pool, true);
819a2ed165fSKevin Bowling }
820a2ed165fSKevin Bowling }
821a2ed165fSKevin Bowling
822a2ed165fSKevin Bowling /*
823a2ed165fSKevin Bowling * Preserve PF mappings unless I350 needs their slots for VFs.
824a2ed165fSKevin Bowling * PF-only VLANs on 82576 intentionally have no VLVF mapping and
825a2ed165fSKevin Bowling * reach the default PF pool after passing the global VFTA.
826a2ed165fSKevin Bowling */
827a2ed165fSKevin Bowling if (preserve_pf)
828a2ed165fSKevin Bowling for (vid = 0; vid < 4096; vid++) {
829a2ed165fSKevin Bowling if ((sc->shadow_vfta[vid >> 5] &
830a2ed165fSKevin Bowling (1U << (vid & 0x1f))) == 0)
831a2ed165fSKevin Bowling continue;
832a2ed165fSKevin Bowling if (hw->mac.type == e1000_82576 &&
833a2ed165fSKevin Bowling !igb_iov_vlan_present(sc, vid, false))
834a2ed165fSKevin Bowling continue;
835a2ed165fSKevin Bowling (void)igb_iov_vlvf_add(vlvf, old_vlvf, vid,
836a2ed165fSKevin Bowling sc->pool, true);
837a2ed165fSKevin Bowling }
838a2ed165fSKevin Bowling
839a2ed165fSKevin Bowling /* Allocate new VF mappings before PF mappings. */
840a2ed165fSKevin Bowling for (i = 0; i < sc->num_vfs; i++) {
841a2ed165fSKevin Bowling vf = &sc->vfs[i];
842a2ed165fSKevin Bowling if (!(vf->flags & IGB_VF_ACTIVE))
843a2ed165fSKevin Bowling continue;
844a2ed165fSKevin Bowling for (vid = 0; vid < 4096; vid++) {
845a2ed165fSKevin Bowling if ((vf->vlans[vid >> 5] & (1U << (vid & 0x1f))) ==
846a2ed165fSKevin Bowling 0)
847a2ed165fSKevin Bowling continue;
848a2ed165fSKevin Bowling if (igb_iov_vlvf_add(vlvf, old_vlvf, vid,
849a2ed165fSKevin Bowling vf->pool, false) == 0)
850a2ed165fSKevin Bowling vfta[vid >> 5] |= 1U << (vid & 0x1f);
851a2ed165fSKevin Bowling }
852a2ed165fSKevin Bowling igb_iov_configure_vmolr(sc, vf);
853a2ed165fSKevin Bowling }
854a2ed165fSKevin Bowling if (!pf_vlan_promisc)
855a2ed165fSKevin Bowling for (vid = 0; vid < 4096; vid++) {
856a2ed165fSKevin Bowling if ((sc->shadow_vfta[vid >> 5] &
857a2ed165fSKevin Bowling (1U << (vid & 0x1f))) == 0)
858a2ed165fSKevin Bowling continue;
859a2ed165fSKevin Bowling /*
860a2ed165fSKevin Bowling * With no VLVF match, 82576 sends a globally admitted
861a2ed165fSKevin Bowling * VLAN to the default PF pool. A VLVF entry is needed
862a2ed165fSKevin Bowling * only when this VLAN is also assigned to a VF.
863a2ed165fSKevin Bowling */
864a2ed165fSKevin Bowling if (hw->mac.type == e1000_82576 &&
865a2ed165fSKevin Bowling !igb_iov_vlan_present(sc, vid, false))
866a2ed165fSKevin Bowling continue;
867a2ed165fSKevin Bowling if (igb_iov_vlvf_add(vlvf, old_vlvf, vid,
868a2ed165fSKevin Bowling sc->pool, false) != 0)
869a2ed165fSKevin Bowling pf_overflow = true;
870a2ed165fSKevin Bowling }
871a2ed165fSKevin Bowling
872a2ed165fSKevin Bowling if (pf_vlan_promisc) {
873a2ed165fSKevin Bowling memset(vfta, 0xff, sizeof(vfta));
874a2ed165fSKevin Bowling for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++)
875a2ed165fSKevin Bowling if ((vlvf[i] & E1000_VLVF_VLANID_ENABLE) != 0)
876a2ed165fSKevin Bowling vlvf[i] |= 1U <<
877a2ed165fSKevin Bowling (E1000_VLVF_POOLSEL_SHIFT + sc->pool);
878a2ed165fSKevin Bowling }
879a2ed165fSKevin Bowling
880a2ed165fSKevin Bowling /*
881a2ed165fSKevin Bowling * Establish the PF fallback before an overflowing I350 rebuild can
882a2ed165fSKevin Bowling * displace one of its old VLVF mappings.
883a2ed165fSKevin Bowling */
884a2ed165fSKevin Bowling vmolr = E1000_READ_REG(hw, E1000_VMOLR(sc->pool));
885a2ed165fSKevin Bowling vmolr &= ~E1000_VMOLR_VPE;
886a2ed165fSKevin Bowling if (hw->mac.type == e1000_i350 &&
887a2ed165fSKevin Bowling (pf_overflow || pf_vlan_promisc))
888a2ed165fSKevin Bowling vmolr |= E1000_VMOLR_VPE;
889a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_VMOLR(sc->pool), vmolr);
890a2ed165fSKevin Bowling
891a2ed165fSKevin Bowling /*
892a2ed165fSKevin Bowling * Remove global VFTA membership before removing a VLAN entirely, and
893a2ed165fSKevin Bowling * add a VLVF mapping before globally admitting a new VF VLAN. A
894a2ed165fSKevin Bowling * transition to a PF-only VLAN deliberately retains VFTA membership
895a2ed165fSKevin Bowling * and falls through to the default PF pool.
896a2ed165fSKevin Bowling */
8975ce6c94fSKevin Bowling force_vfta = hw->mac.type == e1000_i350 &&
8985ce6c94fSKevin Bowling !sc->iov_vfta_valid;
8995ce6c94fSKevin Bowling vfta_changed = false;
900a2ed165fSKevin Bowling for (i = 0; i < EM_VFTA_SIZE; i++) {
901a2ed165fSKevin Bowling /*
902a2ed165fSKevin Bowling * I350 erratum 20 makes VFTA reads unreliable while VMDq
903a2ed165fSKevin Bowling * loopback or anti-spoofing is active. Its ten-write
9045ce6c94fSKevin Bowling * workaround is already in e1000_write_vfta_i350(). Force a
9055ce6c94fSKevin Bowling * complete clear when the authoritative shadow is invalid;
9065ce6c94fSKevin Bowling * 82576 can safely diff against its live register contents.
907a2ed165fSKevin Bowling */
908a2ed165fSKevin Bowling if (hw->mac.type == e1000_i350)
9095ce6c94fSKevin Bowling old_vfta = force_vfta ? 0 : sc->iov_vfta[i];
910a2ed165fSKevin Bowling else
911a2ed165fSKevin Bowling old_vfta =
912a2ed165fSKevin Bowling E1000_READ_REG_ARRAY(hw, E1000_VFTA, i);
9135ce6c94fSKevin Bowling effective_vfta[i] = old_vfta & vfta[i];
9145ce6c94fSKevin Bowling if (force_vfta || effective_vfta[i] != old_vfta) {
9155ce6c94fSKevin Bowling SDT_PROBE3(igb_iov, vlan, rebuild, vfta_clear,
9165ce6c94fSKevin Bowling sc, i, effective_vfta[i]);
9175ce6c94fSKevin Bowling e1000_write_vfta(hw, i, effective_vfta[i]);
9185ce6c94fSKevin Bowling vfta_changed = true;
919a2ed165fSKevin Bowling }
9205ce6c94fSKevin Bowling }
9215ce6c94fSKevin Bowling if (vfta_changed)
922a2ed165fSKevin Bowling E1000_WRITE_FLUSH(hw);
9235ce6c94fSKevin Bowling vlvf_changed = false;
924a2ed165fSKevin Bowling for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++)
9255ce6c94fSKevin Bowling if (vlvf[i] != old_vlvf[i]) {
9265ce6c94fSKevin Bowling SDT_PROBE3(igb_iov, vlan, rebuild, vlvf_write,
9275ce6c94fSKevin Bowling sc, i, vlvf[i]);
928a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_VLVF(i), vlvf[i]);
9295ce6c94fSKevin Bowling vlvf_changed = true;
9305ce6c94fSKevin Bowling }
9315ce6c94fSKevin Bowling if (vlvf_changed)
932a2ed165fSKevin Bowling E1000_WRITE_FLUSH(hw);
9335ce6c94fSKevin Bowling vfta_changed = false;
934a2ed165fSKevin Bowling for (i = 0; i < EM_VFTA_SIZE; i++)
9355ce6c94fSKevin Bowling if (vfta[i] != effective_vfta[i]) {
9365ce6c94fSKevin Bowling SDT_PROBE3(igb_iov, vlan, rebuild, vfta_set,
9375ce6c94fSKevin Bowling sc, i, vfta[i]);
938a2ed165fSKevin Bowling e1000_write_vfta(hw, i, vfta[i]);
9395ce6c94fSKevin Bowling vfta_changed = true;
9405ce6c94fSKevin Bowling }
9415ce6c94fSKevin Bowling if (vfta_changed)
9425ce6c94fSKevin Bowling E1000_WRITE_FLUSH(hw);
9435ce6c94fSKevin Bowling SDT_PROBE3(igb_iov, vlan, rebuild, state, sc, vfta, vlvf);
9445ce6c94fSKevin Bowling igb_iov_vfta_shadow_store(sc, vfta);
945a2ed165fSKevin Bowling }
946a2ed165fSKevin Bowling
947a2ed165fSKevin Bowling static bool
igb_iov_vlan_present(struct e1000_softc * sc,u16 vid,bool include_pf)948a2ed165fSKevin Bowling igb_iov_vlan_present(struct e1000_softc *sc, u16 vid, bool include_pf)
949a2ed165fSKevin Bowling {
950a2ed165fSKevin Bowling int i;
951a2ed165fSKevin Bowling
952a2ed165fSKevin Bowling if (include_pf &&
953a2ed165fSKevin Bowling (sc->shadow_vfta[vid >> 5] & (1U << (vid & 0x1f))) != 0)
954a2ed165fSKevin Bowling return (true);
955a2ed165fSKevin Bowling for (i = 0; i < sc->num_vfs; i++)
956a2ed165fSKevin Bowling if ((sc->vfs[i].flags & IGB_VF_ACTIVE) != 0 &&
957a2ed165fSKevin Bowling (sc->vfs[i].vlans[vid >> 5] &
958a2ed165fSKevin Bowling (1U << (vid & 0x1f))) != 0)
959a2ed165fSKevin Bowling return (true);
960a2ed165fSKevin Bowling return (false);
961a2ed165fSKevin Bowling }
962a2ed165fSKevin Bowling
963a2ed165fSKevin Bowling static int
igb_iov_vlan_unique_count(struct e1000_softc * sc,bool include_pf)964a2ed165fSKevin Bowling igb_iov_vlan_unique_count(struct e1000_softc *sc, bool include_pf)
965a2ed165fSKevin Bowling {
966e8f3b96bSKevin Bowling u32 vlans;
967e8f3b96bSKevin Bowling int count, i, word;
968a2ed165fSKevin Bowling
969a2ed165fSKevin Bowling count = 0;
970e8f3b96bSKevin Bowling for (word = 0; word < EM_VFTA_SIZE; word++) {
971e8f3b96bSKevin Bowling vlans = include_pf ? sc->shadow_vfta[word] : 0;
972e8f3b96bSKevin Bowling for (i = 0; i < sc->num_vfs; i++)
973e8f3b96bSKevin Bowling if ((sc->vfs[i].flags & IGB_VF_ACTIVE) != 0)
974e8f3b96bSKevin Bowling vlans |= sc->vfs[i].vlans[word];
975e8f3b96bSKevin Bowling count += bitcount32(vlans);
976e8f3b96bSKevin Bowling }
977a2ed165fSKevin Bowling return (count);
978a2ed165fSKevin Bowling }
979a2ed165fSKevin Bowling
980a2ed165fSKevin Bowling static int
igb_iov_set_vlan(struct e1000_softc * sc,struct igb_vf * vf,u16 vid,bool add)981a2ed165fSKevin Bowling igb_iov_set_vlan(struct e1000_softc *sc, struct igb_vf *vf, u16 vid,
982a2ed165fSKevin Bowling bool add)
983a2ed165fSKevin Bowling {
984a2ed165fSKevin Bowling u32 bit;
985a2ed165fSKevin Bowling bool present;
986a2ed165fSKevin Bowling
987a2ed165fSKevin Bowling bit = 1U << (vid & 0x1f);
988a2ed165fSKevin Bowling present = (vf->vlans[vid >> 5] & bit) != 0;
989a2ed165fSKevin Bowling if (vid == 0) {
990a2ed165fSKevin Bowling if (!present) {
991a2ed165fSKevin Bowling vf->vlans[0] |= 1U;
992a2ed165fSKevin Bowling igb_iov_rebuild_vlan(sc);
993a2ed165fSKevin Bowling }
994a2ed165fSKevin Bowling return (0);
995a2ed165fSKevin Bowling }
996a2ed165fSKevin Bowling if (add == present)
997a2ed165fSKevin Bowling return (0);
998a2ed165fSKevin Bowling
999e8f3b96bSKevin Bowling /*
1000e8f3b96bSKevin Bowling * Removals always reduce privilege and remain available. Charge only
1001e8f3b96bSKevin Bowling * additions, which a hostile VF must alternate with removals to force
1002e8f3b96bSKevin Bowling * repeated global VLAN rebuilds.
1003e8f3b96bSKevin Bowling */
1004a2ed165fSKevin Bowling if (add && !igb_iov_vlan_present(sc, vid, false) &&
1005a2ed165fSKevin Bowling igb_iov_vlan_unique_count(sc, false) >=
1006a2ed165fSKevin Bowling E1000_VLVF_ARRAY_SIZE)
1007a2ed165fSKevin Bowling return (ENOSPC);
1008e8f3b96bSKevin Bowling if (add && !igb_iov_vlan_add_allowed(vf))
1009e8f3b96bSKevin Bowling return (EBUSY);
1010a2ed165fSKevin Bowling
1011a2ed165fSKevin Bowling if (add) {
1012a2ed165fSKevin Bowling vf->vlans[vid >> 5] |= bit;
1013a2ed165fSKevin Bowling vf->vlan_count++;
1014a2ed165fSKevin Bowling } else {
1015a2ed165fSKevin Bowling vf->vlans[vid >> 5] &= ~bit;
1016a2ed165fSKevin Bowling vf->vlan_count--;
1017a2ed165fSKevin Bowling }
1018a2ed165fSKevin Bowling igb_iov_rebuild_vlan(sc);
1019a2ed165fSKevin Bowling return (0);
1020a2ed165fSKevin Bowling }
1021a2ed165fSKevin Bowling
1022a2ed165fSKevin Bowling static void
igb_iov_reset_vf_state(struct e1000_softc * sc,struct igb_vf * vf)1023a2ed165fSKevin Bowling igb_iov_reset_vf_state(struct e1000_softc *sc, struct igb_vf *vf)
1024a2ed165fSKevin Bowling {
1025a2ed165fSKevin Bowling bool update_uta;
1026a2ed165fSKevin Bowling
1027a2ed165fSKevin Bowling update_uta = (vf->flags & IGB_VF_UCAST_PROMISC) != 0;
1028a2ed165fSKevin Bowling vf->flags &= ~(IGB_VF_CTS | IGB_VF_UCAST_PROMISC |
1029a2ed165fSKevin Bowling IGB_VF_MCAST_PROMISC | IGB_VF_MCAST_OVERFLOW |
1030a2ed165fSKevin Bowling IGB_VF_MBX_PENDING | IGB_VF_MBX_GAVE_UP |
1031a2ed165fSKevin Bowling IGB_VF_MDD_NOTIFY_PENDING);
1032a2ed165fSKevin Bowling vf->mbx_retry_at = 0;
1033a2ed165fSKevin Bowling vf->mdd_notify_at = 0;
1034a2ed165fSKevin Bowling vf->mbx_retry_count = 0;
1035a2ed165fSKevin Bowling /*
1036a2ed165fSKevin Bowling * A reset starts a new mailbox epoch. Permit one immediate NACK so a
1037a2ed165fSKevin Bowling * premature non-reset request does not wait for its posted-read
1038a2ed165fSKevin Bowling * timeout.
1039a2ed165fSKevin Bowling */
1040a2ed165fSKevin Bowling memset(&vf->last_nack, 0, sizeof(vf->last_nack));
1041a2ed165fSKevin Bowling vf->max_frame_size = ETHER_MAX_LEN;
1042a2ed165fSKevin Bowling vf->mc_count = 0;
1043a2ed165fSKevin Bowling vf->vlan_count = 0;
1044a2ed165fSKevin Bowling memset(vf->mc_hashes, 0, sizeof(vf->mc_hashes));
1045a2ed165fSKevin Bowling memset(vf->vlans, 0, sizeof(vf->vlans));
1046a2ed165fSKevin Bowling /* Preserve the administrative access VLAN across VF and PF resets. */
1047a2ed165fSKevin Bowling if (vf->default_vlan == 0)
1048a2ed165fSKevin Bowling vf->vlans[0] = 1U;
1049a2ed165fSKevin Bowling else {
1050a2ed165fSKevin Bowling vf->vlans[vf->default_vlan >> 5] =
1051a2ed165fSKevin Bowling 1U << (vf->default_vlan & 0x1f);
1052a2ed165fSKevin Bowling vf->vlan_count = 1;
1053a2ed165fSKevin Bowling }
1054a2ed165fSKevin Bowling igb_iov_configure_vmolr(sc, vf);
1055a2ed165fSKevin Bowling if (update_uta)
1056a2ed165fSKevin Bowling igb_iov_set_uta(sc);
1057a2ed165fSKevin Bowling }
1058a2ed165fSKevin Bowling
1059a2ed165fSKevin Bowling static bool
igb_iov_vf_vlan_is_default(const struct igb_vf * vf)1060a2ed165fSKevin Bowling igb_iov_vf_vlan_is_default(const struct igb_vf *vf)
1061a2ed165fSKevin Bowling {
1062a2ed165fSKevin Bowling u32 expected;
1063a2ed165fSKevin Bowling int i;
1064a2ed165fSKevin Bowling
1065a2ed165fSKevin Bowling for (i = 0; i < EM_VFTA_SIZE; i++) {
1066a2ed165fSKevin Bowling expected = 0;
1067a2ed165fSKevin Bowling if (i == vf->default_vlan >> 5)
1068a2ed165fSKevin Bowling expected = 1U << (vf->default_vlan & 0x1f);
1069a2ed165fSKevin Bowling if (vf->vlans[i] != expected)
1070a2ed165fSKevin Bowling return (false);
1071a2ed165fSKevin Bowling }
1072a2ed165fSKevin Bowling return (true);
1073a2ed165fSKevin Bowling }
1074a2ed165fSKevin Bowling
10758c872470SKevin Bowling static bool
igb_iov_sanitize_vf_queues(struct e1000_softc * sc,struct igb_vf * vf)10768c872470SKevin Bowling igb_iov_sanitize_vf_queues(struct e1000_softc *sc,
10778c872470SKevin Bowling struct igb_vf *vf)
10788c872470SKevin Bowling {
10798c872470SKevin Bowling struct e1000_hw *hw;
10808c872470SKevin Bowling u16 qid[IGB_IOV_VF_QUEUES_MAX];
10818c872470SKevin Bowling u32 rxdctl, txdctl;
10828c872470SKevin Bowling int i, nqueues, retry;
10838c872470SKevin Bowling
10848c872470SKevin Bowling hw = &sc->hw;
10858c872470SKevin Bowling switch (hw->mac.type) {
10868c872470SKevin Bowling case e1000_82576:
10878c872470SKevin Bowling nqueues = IGB_82576_VF_QUEUES;
10888c872470SKevin Bowling qid[0] = vf->pool;
10898c872470SKevin Bowling qid[1] = vf->pool + IGB_82576_VF_QUEUE_STRIDE;
10908c872470SKevin Bowling break;
10918c872470SKevin Bowling case e1000_i350:
10928c872470SKevin Bowling nqueues = IGB_I350_VF_QUEUES;
10938c872470SKevin Bowling qid[0] = vf->pool;
10948c872470SKevin Bowling break;
10958c872470SKevin Bowling default:
10968c872470SKevin Bowling return (true);
10978c872470SKevin Bowling }
10988c872470SKevin Bowling
10998c872470SKevin Bowling /*
11008c872470SKevin Bowling * I350 maps pool n to queue n. 82576 gives VF n physical queues n
11018c872470SKevin Bowling * and n + 8, so both retained queue configurations must be cleared.
11028c872470SKevin Bowling */
11038c872470SKevin Bowling for (i = 0; i < nqueues; i++)
11048c872470SKevin Bowling KASSERT(qid[i] < (hw->mac.type == e1000_82576 ?
11058c872470SKevin Bowling IGB_82576_NUM_QUEUES : IGB_I350_NUM_QUEUES),
11068c872470SKevin Bowling ("%s: invalid VF queue %u", __func__, qid[i]));
11078c872470SKevin Bowling
11088c872470SKevin Bowling /*
11098c872470SKevin Bowling * The 82576 and I350 specification updates, Software Clarification 3,
11108c872470SKevin Bowling * note that VFLR does not reset the VF queue configuration. Clear the
11118c872470SKevin Bowling * PF-programmable state before acknowledging the reset so a new VF
11128c872470SKevin Bowling * owner cannot inherit it, particularly a descriptor-head write-back
11138c872470SKevin Bowling * DMA address. The new VF driver initializes its active ring pointers
11148c872470SKevin Bowling * during queue setup.
11158c872470SKevin Bowling *
11168c872470SKevin Bowling * Disable every queue first, then wait for outstanding DMA activity to
11178c872470SKevin Bowling * stop before clearing TDWBAL/H and the remaining retained state.
11188c872470SKevin Bowling * Spin only for the normal fast transition, then sleep so a VF that
11198c872470SKevin Bowling * keeps asserting QUEUE_ENABLE cannot busy-wait the PF for 10 ms.
11208c872470SKevin Bowling */
11218c872470SKevin Bowling for (i = 0; i < nqueues; i++) {
11228c872470SKevin Bowling E1000_WRITE_REG(hw, E1000_RXDCTL(qid[i]), 0);
11238c872470SKevin Bowling E1000_WRITE_REG(hw, E1000_TXDCTL(qid[i]), 0);
11248c872470SKevin Bowling }
11258c872470SKevin Bowling E1000_WRITE_FLUSH(hw);
11268c872470SKevin Bowling for (retry = 0; retry < IGB_IOV_QUEUE_DISABLE_RETRIES; retry++) {
11278c872470SKevin Bowling for (i = 0; i < nqueues; i++) {
11288c872470SKevin Bowling rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(qid[i]));
11298c872470SKevin Bowling txdctl = E1000_READ_REG(hw, E1000_TXDCTL(qid[i]));
11308c872470SKevin Bowling if ((rxdctl & E1000_RXDCTL_QUEUE_ENABLE) != 0 ||
11318c872470SKevin Bowling (txdctl & E1000_TXDCTL_QUEUE_ENABLE) != 0)
11328c872470SKevin Bowling break;
11338c872470SKevin Bowling }
11348c872470SKevin Bowling if (i == nqueues)
11358c872470SKevin Bowling break;
11368c872470SKevin Bowling if (retry + 1 < IGB_IOV_QUEUE_DISABLE_RETRIES) {
11378c872470SKevin Bowling if (retry < IGB_IOV_QUEUE_DISABLE_BUSY_RETRIES)
11388c872470SKevin Bowling DELAY(IGB_IOV_QUEUE_DISABLE_DELAY_US);
11398c872470SKevin Bowling else
11408c872470SKevin Bowling pause_sbt("igbqds",
11418c872470SKevin Bowling IGB_IOV_QUEUE_DISABLE_PAUSE, 0,
11428c872470SKevin Bowling C_PREL(1));
11438c872470SKevin Bowling }
11448c872470SKevin Bowling }
11458c872470SKevin Bowling if (retry == IGB_IOV_QUEUE_DISABLE_RETRIES) {
11468c872470SKevin Bowling if (ratecheck(&vf->last_queue_log,
11478c872470SKevin Bowling &igb_iov_mbx_log_interval))
11488c872470SKevin Bowling device_printf(sc->dev,
11498c872470SKevin Bowling "could not disable queues for VF %u; "
11508c872470SKevin Bowling "reset deferred\n", vf->pool);
11518c872470SKevin Bowling return (false);
11528c872470SKevin Bowling }
11538c872470SKevin Bowling
11548c872470SKevin Bowling for (i = 0; i < nqueues; i++) {
11558c872470SKevin Bowling E1000_WRITE_REG(hw, E1000_SRRCTL(qid[i]), 0);
11568c872470SKevin Bowling E1000_WRITE_REG(hw, E1000_DCA_RXCTRL(qid[i]), 0);
11578c872470SKevin Bowling E1000_WRITE_REG(hw, E1000_TDWBAL(qid[i]), 0);
11588c872470SKevin Bowling E1000_WRITE_REG(hw, E1000_TDWBAH(qid[i]), 0);
11598c872470SKevin Bowling E1000_WRITE_REG(hw, E1000_DCA_TXCTRL(qid[i]), 0);
11608c872470SKevin Bowling }
11618c872470SKevin Bowling E1000_WRITE_REG(hw, E1000_PSRTYPE(vf->pool), 0);
11628c872470SKevin Bowling E1000_WRITE_FLUSH(hw);
11638c872470SKevin Bowling return (true);
11648c872470SKevin Bowling }
11658c872470SKevin Bowling
11668c872470SKevin Bowling static bool
igb_iov_reset_event_common(struct e1000_softc * sc,struct igb_vf * vf,bool reset_intrs)1167a2ed165fSKevin Bowling igb_iov_reset_event_common(struct e1000_softc *sc, struct igb_vf *vf,
1168a2ed165fSKevin Bowling bool reset_intrs)
1169a2ed165fSKevin Bowling {
1170a2ed165fSKevin Bowling struct e1000_hw *hw;
11718c872470SKevin Bowling bool rebuild_mta, rebuild_vlan, sanitized;
1172a2ed165fSKevin Bowling u32 reg;
1173a2ed165fSKevin Bowling
1174a2ed165fSKevin Bowling hw = &sc->hw;
1175a2ed165fSKevin Bowling rebuild_mta = vf->mc_count != 0;
1176a2ed165fSKevin Bowling rebuild_vlan = !igb_iov_vf_vlan_is_default(vf);
1177a2ed165fSKevin Bowling reg = E1000_READ_REG(hw, E1000_VFTE);
1178a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_VFTE, reg & ~(1U << vf->pool));
1179a2ed165fSKevin Bowling reg = E1000_READ_REG(hw, E1000_VFRE);
1180a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_VFRE, reg & ~(1U << vf->pool));
1181a2ed165fSKevin Bowling if (reset_intrs)
1182a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_VTCTRL(vf->pool),
1183a2ed165fSKevin Bowling E1000_VTCTRL_RST);
11848c872470SKevin Bowling sanitized = igb_iov_sanitize_vf_queues(sc, vf);
1185a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_VMVIR(vf->pool), 0);
1186a2ed165fSKevin Bowling igb_iov_clear_mac_filters(sc, vf);
1187a2ed165fSKevin Bowling igb_iov_clear_rar(sc, vf->rar_index);
1188a2ed165fSKevin Bowling igb_iov_reset_vf_state(sc, vf);
1189a2ed165fSKevin Bowling if (rebuild_mta)
1190a2ed165fSKevin Bowling igb_iov_rebuild_mta(sc);
1191a2ed165fSKevin Bowling if (rebuild_vlan)
1192a2ed165fSKevin Bowling igb_iov_rebuild_vlan(sc);
11938c872470SKevin Bowling return (sanitized);
1194a2ed165fSKevin Bowling }
1195a2ed165fSKevin Bowling
11968c872470SKevin Bowling static bool
igb_iov_reset_event(struct e1000_softc * sc,struct igb_vf * vf)1197a2ed165fSKevin Bowling igb_iov_reset_event(struct e1000_softc *sc, struct igb_vf *vf)
1198a2ed165fSKevin Bowling {
11998c872470SKevin Bowling return (igb_iov_reset_event_common(sc, vf, true));
1200a2ed165fSKevin Bowling }
1201a2ed165fSKevin Bowling
1202a2ed165fSKevin Bowling static void
igb_iov_mdd_reset_event(struct e1000_softc * sc,struct igb_vf * vf)1203a2ed165fSKevin Bowling igb_iov_mdd_reset_event(struct e1000_softc *sc, struct igb_vf *vf)
1204a2ed165fSKevin Bowling {
1205a2ed165fSKevin Bowling /*
1206a2ed165fSKevin Bowling * VTCTRL.RST clears the VF's queue-enable and interrupt registers
1207a2ed165fSKevin Bowling * (I350 section 8.28.1). It therefore also removes the admin-vector
1208a2ed165fSKevin Bowling * route needed to deliver the reset notification below. MDD recovery
1209a2ed165fSKevin Bowling * explicitly permits toggling VFTE instead (section 7.8.3.8.3).
1210a2ed165fSKevin Bowling *
1211a2ed165fSKevin Bowling * Leave the interrupt registers intact, keep VFTE/VFRE disabled until
1212a2ed165fSKevin Bowling * the VF completes a new reset handshake, and use the no-CTS control
1213a2ed165fSKevin Bowling * message to make the guest reinitialize. FreeBSD and DPDK consume
1214a2ed165fSKevin Bowling * that message directly; Linux ACKs it and the PF's non-CTS ACK path
1215a2ed165fSKevin Bowling * replies with the NACK that schedules igbvf's reset task.
12168c872470SKevin Bowling *
12178c872470SKevin Bowling * Sanitization failure leaves the pool disabled. The VF reset
12188c872470SKevin Bowling * handshake retries it and is NACKed while a queue remains active.
1219a2ed165fSKevin Bowling */
12208c872470SKevin Bowling (void)igb_iov_reset_event_common(sc, vf, false);
1221a2ed165fSKevin Bowling }
1222a2ed165fSKevin Bowling
1223a2ed165fSKevin Bowling static void
igb_iov_reset_msg(struct e1000_softc * sc,struct igb_vf * vf)1224a2ed165fSKevin Bowling igb_iov_reset_msg(struct e1000_softc *sc, struct igb_vf *vf)
1225a2ed165fSKevin Bowling {
1226a2ed165fSKevin Bowling struct e1000_hw *hw;
1227a2ed165fSKevin Bowling u32 msg[3], reg;
1228a2ed165fSKevin Bowling
1229a2ed165fSKevin Bowling hw = &sc->hw;
12308c872470SKevin Bowling if (!igb_iov_reset_event(sc, vf)) {
12318c872470SKevin Bowling msg[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_NACK;
12328c872470SKevin Bowling e1000_write_mbx(hw, msg, 1, vf->pool);
12338c872470SKevin Bowling return;
12348c872470SKevin Bowling }
1235a2ed165fSKevin Bowling igb_iov_map_rar(sc, vf->rar_index, vf->mac, vf->pool);
1236a2ed165fSKevin Bowling igb_iov_set_anti_spoof(sc, vf);
1237a2ed165fSKevin Bowling
1238a2ed165fSKevin Bowling reg = E1000_READ_REG(hw, E1000_VFTE);
1239a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_VFTE, reg | (1U << vf->pool));
1240a2ed165fSKevin Bowling reg = E1000_READ_REG(hw, E1000_VFRE);
1241a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_VFRE, reg | (1U << vf->pool));
1242a2ed165fSKevin Bowling /*
1243a2ed165fSKevin Bowling * 82576's WVBR blocked bitmap is read-clear, so the reset handshake
1244a2ed165fSKevin Bowling * completes that event's lifetime. I350 MDFB might be read-only;
1245a2ed165fSKevin Bowling * re-arm its edge latch only after a valid MDFB sample reads clear.
1246a2ed165fSKevin Bowling */
1247a2ed165fSKevin Bowling if (hw->mac.type == e1000_82576)
1248a2ed165fSKevin Bowling vf->flags &= ~IGB_VF_MDD_BLOCKED;
1249a2ed165fSKevin Bowling vf->flags |= IGB_VF_CTS;
1250a2ed165fSKevin Bowling
1251a2ed165fSKevin Bowling memset(msg, 0, sizeof(msg));
1252a2ed165fSKevin Bowling msg[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_ACK;
1253a2ed165fSKevin Bowling memcpy(&msg[1], vf->mac, ETHER_ADDR_LEN);
1254a2ed165fSKevin Bowling e1000_write_mbx(hw, msg, 3, vf->pool);
1255a2ed165fSKevin Bowling }
1256a2ed165fSKevin Bowling
1257a2ed165fSKevin Bowling static int
igb_iov_set_mac_filter(struct e1000_softc * sc,struct igb_vf * vf,u32 * msg)1258a2ed165fSKevin Bowling igb_iov_set_mac_filter(struct e1000_softc *sc, struct igb_vf *vf, u32 *msg)
1259a2ed165fSKevin Bowling {
1260a2ed165fSKevin Bowling struct igb_vf_mac_filter *filter, *free_filter;
1261a2ed165fSKevin Bowling const u8 *mac;
1262a2ed165fSKevin Bowling u32 info;
1263a2ed165fSKevin Bowling int count, i;
1264a2ed165fSKevin Bowling
1265a2ed165fSKevin Bowling info = msg[0] & E1000_VT_MSGINFO_MASK;
1266a2ed165fSKevin Bowling if (info == E1000_VF_MAC_FILTER_CLR) {
1267a2ed165fSKevin Bowling igb_iov_clear_mac_filters(sc, vf);
1268a2ed165fSKevin Bowling return (0);
1269a2ed165fSKevin Bowling }
1270a2ed165fSKevin Bowling if (info != E1000_VF_MAC_FILTER_ADD)
1271a2ed165fSKevin Bowling return (EINVAL);
1272a2ed165fSKevin Bowling if ((vf->flags & IGB_VF_CAP_MAC) == 0)
1273a2ed165fSKevin Bowling return (EPERM);
1274a2ed165fSKevin Bowling
1275a2ed165fSKevin Bowling mac = (const u8 *)&msg[1];
1276a2ed165fSKevin Bowling if (!igb_iov_mac_valid(mac))
1277a2ed165fSKevin Bowling return (EINVAL);
1278a2ed165fSKevin Bowling if (memcmp(mac, vf->mac, ETHER_ADDR_LEN) == 0)
1279a2ed165fSKevin Bowling return (0);
1280a2ed165fSKevin Bowling
1281a2ed165fSKevin Bowling count = 0;
1282a2ed165fSKevin Bowling free_filter = NULL;
1283a2ed165fSKevin Bowling for (i = 0; i < sc->num_vf_mac_filters; i++) {
1284a2ed165fSKevin Bowling filter = &sc->vf_mac_filters[i];
1285a2ed165fSKevin Bowling if (!filter->active) {
1286a2ed165fSKevin Bowling if (free_filter == NULL)
1287a2ed165fSKevin Bowling free_filter = filter;
1288a2ed165fSKevin Bowling continue;
1289a2ed165fSKevin Bowling }
1290a2ed165fSKevin Bowling if (memcmp(filter->mac, mac, ETHER_ADDR_LEN) != 0)
1291a2ed165fSKevin Bowling continue;
1292a2ed165fSKevin Bowling return (filter->pool == vf->pool ? 0 : EADDRINUSE);
1293a2ed165fSKevin Bowling }
1294a2ed165fSKevin Bowling for (i = 0; i < sc->num_vf_mac_filters; i++)
1295a2ed165fSKevin Bowling if (sc->vf_mac_filters[i].active &&
1296a2ed165fSKevin Bowling sc->vf_mac_filters[i].pool == vf->pool)
1297a2ed165fSKevin Bowling count++;
1298a2ed165fSKevin Bowling if (igb_iov_mac_in_use(sc, mac, vf))
1299a2ed165fSKevin Bowling return (EADDRINUSE);
1300a2ed165fSKevin Bowling if (count >= IGB_IOV_MAX_MAC_FILTERS)
1301a2ed165fSKevin Bowling return (ENOSPC);
1302a2ed165fSKevin Bowling if (free_filter == NULL)
1303a2ed165fSKevin Bowling return (ENOSPC);
1304a2ed165fSKevin Bowling
1305a2ed165fSKevin Bowling free_filter->active = true;
1306a2ed165fSKevin Bowling free_filter->pool = vf->pool;
1307a2ed165fSKevin Bowling memcpy(free_filter->mac, mac, ETHER_ADDR_LEN);
1308a2ed165fSKevin Bowling igb_iov_map_rar(sc, free_filter->rar_index, free_filter->mac, vf->pool);
1309a2ed165fSKevin Bowling return (0);
1310a2ed165fSKevin Bowling }
1311a2ed165fSKevin Bowling
1312a2ed165fSKevin Bowling static int
igb_iov_set_mac(struct e1000_softc * sc,struct igb_vf * vf,u32 * msg)1313a2ed165fSKevin Bowling igb_iov_set_mac(struct e1000_softc *sc, struct igb_vf *vf, u32 *msg)
1314a2ed165fSKevin Bowling {
1315a2ed165fSKevin Bowling u8 *mac;
1316a2ed165fSKevin Bowling
1317a2ed165fSKevin Bowling if ((msg[0] & E1000_VT_MSGINFO_MASK) != 0)
1318a2ed165fSKevin Bowling return (igb_iov_set_mac_filter(sc, vf, msg));
1319a2ed165fSKevin Bowling
1320a2ed165fSKevin Bowling mac = (u8 *)&msg[1];
1321a2ed165fSKevin Bowling if (!igb_iov_mac_valid(mac))
1322a2ed165fSKevin Bowling return (EINVAL);
1323a2ed165fSKevin Bowling if (memcmp(mac, vf->mac, ETHER_ADDR_LEN) != 0 &&
1324a2ed165fSKevin Bowling !(vf->flags & IGB_VF_CAP_MAC))
1325a2ed165fSKevin Bowling return (EPERM);
1326a2ed165fSKevin Bowling if (memcmp(mac, vf->mac, ETHER_ADDR_LEN) != 0 &&
1327a2ed165fSKevin Bowling igb_iov_mac_in_use(sc, mac, vf))
1328a2ed165fSKevin Bowling return (EADDRINUSE);
1329a2ed165fSKevin Bowling
1330a2ed165fSKevin Bowling memcpy(vf->mac, mac, ETHER_ADDR_LEN);
1331a2ed165fSKevin Bowling igb_iov_map_rar(sc, vf->rar_index, vf->mac, vf->pool);
1332a2ed165fSKevin Bowling return (0);
1333a2ed165fSKevin Bowling }
1334a2ed165fSKevin Bowling
1335a2ed165fSKevin Bowling static int
igb_iov_set_multicast(struct e1000_softc * sc,struct igb_vf * vf,u32 * msg)1336a2ed165fSKevin Bowling igb_iov_set_multicast(struct e1000_softc *sc, struct igb_vf *vf, u32 *msg)
1337a2ed165fSKevin Bowling {
1338a2ed165fSKevin Bowling u16 hashes[IGB_IOV_MAX_MC_HASHES] = {};
1339a2ed165fSKevin Bowling bool overflow;
1340a2ed165fSKevin Bowling int count, i;
1341a2ed165fSKevin Bowling
1342a2ed165fSKevin Bowling count = (msg[0] & E1000_VF_SET_MULTICAST_COUNT_MASK) >>
1343a2ed165fSKevin Bowling E1000_VT_MSGINFO_SHIFT;
1344a2ed165fSKevin Bowling overflow = count > IGB_IOV_MAX_MC_HASHES ||
1345a2ed165fSKevin Bowling (msg[0] & E1000_VF_SET_MULTICAST_OVERFLOW) != 0;
1346a2ed165fSKevin Bowling count = min(count, IGB_IOV_MAX_MC_HASHES);
1347a2ed165fSKevin Bowling for (i = 0; i < count; i++)
1348a2ed165fSKevin Bowling hashes[i] =
1349a2ed165fSKevin Bowling (msg[1 + i / 2] >> ((i & 1) * 16)) & 0xffff;
1350a2ed165fSKevin Bowling if (vf->mc_count == count &&
1351a2ed165fSKevin Bowling ((vf->flags & IGB_VF_MCAST_OVERFLOW) != 0) == overflow &&
1352a2ed165fSKevin Bowling memcmp(vf->mc_hashes, hashes, sizeof(hashes)) == 0)
1353a2ed165fSKevin Bowling return (0);
1354a2ed165fSKevin Bowling memcpy(vf->mc_hashes, hashes, sizeof(vf->mc_hashes));
1355a2ed165fSKevin Bowling vf->mc_count = count;
1356a2ed165fSKevin Bowling if (overflow)
1357a2ed165fSKevin Bowling vf->flags |= IGB_VF_MCAST_OVERFLOW;
1358a2ed165fSKevin Bowling else
1359a2ed165fSKevin Bowling vf->flags &= ~IGB_VF_MCAST_OVERFLOW;
1360a2ed165fSKevin Bowling if (overflow &&
1361a2ed165fSKevin Bowling (vf->flags & IGB_VF_MCAST_OVERFLOW_WARNED) == 0) {
1362a2ed165fSKevin Bowling vf->flags |= IGB_VF_MCAST_OVERFLOW_WARNED;
1363a2ed165fSKevin Bowling device_printf(sc->dev,
1364a2ed165fSKevin Bowling "VF %u multicast list exceeds 30 entries; "
1365a2ed165fSKevin Bowling "enabling all-multicast reception\n", vf->pool);
1366a2ed165fSKevin Bowling }
1367e8f3b96bSKevin Bowling igb_iov_configure_vmolr(sc, vf);
1368a2ed165fSKevin Bowling igb_iov_rebuild_mta(sc);
1369a2ed165fSKevin Bowling return (0);
1370a2ed165fSKevin Bowling }
1371a2ed165fSKevin Bowling
1372a2ed165fSKevin Bowling static int
igb_iov_set_lpe(struct e1000_softc * sc,struct igb_vf * vf,u32 * msg)1373a2ed165fSKevin Bowling igb_iov_set_lpe(struct e1000_softc *sc, struct igb_vf *vf, u32 *msg)
1374a2ed165fSKevin Bowling {
1375a2ed165fSKevin Bowling u32 size;
1376a2ed165fSKevin Bowling
1377a2ed165fSKevin Bowling size = msg[1];
1378a2ed165fSKevin Bowling if (size < ETHER_MIN_LEN)
1379a2ed165fSKevin Bowling return (EINVAL);
1380a2ed165fSKevin Bowling vf->max_frame_size = min(size, IGB_IOV_MAX_FRAME_SIZE);
1381a2ed165fSKevin Bowling igb_iov_configure_vmolr(sc, vf);
1382a2ed165fSKevin Bowling return (0);
1383a2ed165fSKevin Bowling }
1384a2ed165fSKevin Bowling
1385a2ed165fSKevin Bowling static int
igb_iov_set_promisc(struct e1000_softc * sc,struct igb_vf * vf,u32 msg)1386a2ed165fSKevin Bowling igb_iov_set_promisc(struct e1000_softc *sc, struct igb_vf *vf, u32 msg)
1387a2ed165fSKevin Bowling {
1388a2ed165fSKevin Bowling u32 mode;
1389a2ed165fSKevin Bowling
1390a2ed165fSKevin Bowling mode = msg & E1000_VT_MSGINFO_MASK;
1391a2ed165fSKevin Bowling if (mode & ~(E1000_VF_SET_PROMISC_UNICAST |
1392a2ed165fSKevin Bowling E1000_VF_SET_PROMISC_MULTICAST))
1393a2ed165fSKevin Bowling return (EINVAL);
1394a2ed165fSKevin Bowling if (mode != 0 && !(vf->flags & IGB_VF_ALLOW_PROMISC))
1395a2ed165fSKevin Bowling return (EPERM);
1396a2ed165fSKevin Bowling
1397a2ed165fSKevin Bowling vf->flags &= ~(IGB_VF_UCAST_PROMISC | IGB_VF_MCAST_PROMISC);
1398a2ed165fSKevin Bowling if (mode & E1000_VF_SET_PROMISC_UNICAST)
1399a2ed165fSKevin Bowling vf->flags |= IGB_VF_UCAST_PROMISC;
1400a2ed165fSKevin Bowling if (mode & E1000_VF_SET_PROMISC_MULTICAST)
1401a2ed165fSKevin Bowling vf->flags |= IGB_VF_MCAST_PROMISC;
1402a2ed165fSKevin Bowling igb_iov_configure_vmolr(sc, vf);
1403a2ed165fSKevin Bowling igb_iov_set_uta(sc);
1404a2ed165fSKevin Bowling return (0);
1405a2ed165fSKevin Bowling }
1406a2ed165fSKevin Bowling
1407a2ed165fSKevin Bowling static bool
igb_iov_process_msg(struct e1000_softc * sc,struct igb_vf * vf)1408a2ed165fSKevin Bowling igb_iov_process_msg(struct e1000_softc *sc, struct igb_vf *vf)
1409a2ed165fSKevin Bowling {
1410a2ed165fSKevin Bowling struct e1000_hw *hw;
1411a2ed165fSKevin Bowling u32 msg[E1000_VFMAILBOX_SIZE], type;
1412a2ed165fSKevin Bowling int error;
1413a2ed165fSKevin Bowling
1414a2ed165fSKevin Bowling hw = &sc->hw;
1415a2ed165fSKevin Bowling memset(msg, 0, sizeof(msg));
1416a2ed165fSKevin Bowling if (e1000_read_mbx(hw, msg, nitems(msg), vf->pool, false) != 0)
1417a2ed165fSKevin Bowling return (false);
1418a2ed165fSKevin Bowling vf->flags &= ~IGB_VF_MBX_PENDING;
1419a2ed165fSKevin Bowling vf->mbx_retry_at = 0;
1420a2ed165fSKevin Bowling vf->mbx_retry_count = 0;
1421a2ed165fSKevin Bowling
1422a2ed165fSKevin Bowling if (msg[0] & (E1000_VT_MSGTYPE_ACK | E1000_VT_MSGTYPE_NACK)) {
1423a2ed165fSKevin Bowling e1000_unlock_mbx(hw, vf->pool);
1424a2ed165fSKevin Bowling return (true);
1425a2ed165fSKevin Bowling }
1426a2ed165fSKevin Bowling if (msg[0] == E1000_VF_RESET) {
1427a2ed165fSKevin Bowling igb_iov_reset_msg(sc, vf);
1428a2ed165fSKevin Bowling return (true);
1429a2ed165fSKevin Bowling }
1430a2ed165fSKevin Bowling if (!(vf->flags & IGB_VF_CTS)) {
1431a2ed165fSKevin Bowling if (igb_iov_nack_allowed(vf)) {
1432a2ed165fSKevin Bowling msg[0] = igb_iov_reply_header(msg[0], false, false);
1433a2ed165fSKevin Bowling e1000_write_mbx(hw, msg, 1, vf->pool);
1434a2ed165fSKevin Bowling } else
1435a2ed165fSKevin Bowling e1000_unlock_mbx(hw, vf->pool);
1436a2ed165fSKevin Bowling return (true);
1437a2ed165fSKevin Bowling }
1438a2ed165fSKevin Bowling
1439a2ed165fSKevin Bowling type = msg[0] & 0xffff;
1440a2ed165fSKevin Bowling switch (type) {
1441a2ed165fSKevin Bowling case E1000_VF_SET_MAC_ADDR:
1442a2ed165fSKevin Bowling error = igb_iov_set_mac(sc, vf, msg);
1443a2ed165fSKevin Bowling break;
1444a2ed165fSKevin Bowling case E1000_VF_SET_MULTICAST:
1445a2ed165fSKevin Bowling error = igb_iov_set_multicast(sc, vf, msg);
1446a2ed165fSKevin Bowling break;
1447a2ed165fSKevin Bowling case E1000_VF_SET_VLAN:
1448a2ed165fSKevin Bowling if (vf->default_vlan != 0)
1449a2ed165fSKevin Bowling error = EPERM;
1450a2ed165fSKevin Bowling else if ((msg[1] & ~E1000_VLVF_VLANID_MASK) != 0)
1451a2ed165fSKevin Bowling error = EINVAL;
1452a2ed165fSKevin Bowling else
1453a2ed165fSKevin Bowling error = igb_iov_set_vlan(sc, vf,
1454a2ed165fSKevin Bowling msg[1] & E1000_VLVF_VLANID_MASK,
1455a2ed165fSKevin Bowling (msg[0] & E1000_VF_SET_VLAN_ADD) != 0);
1456a2ed165fSKevin Bowling break;
1457a2ed165fSKevin Bowling case E1000_VF_SET_LPE:
1458a2ed165fSKevin Bowling error = igb_iov_set_lpe(sc, vf, msg);
1459a2ed165fSKevin Bowling break;
1460a2ed165fSKevin Bowling case E1000_VF_SET_PROMISC:
1461a2ed165fSKevin Bowling error = igb_iov_set_promisc(sc, vf, msg[0]);
1462a2ed165fSKevin Bowling break;
1463a2ed165fSKevin Bowling default:
1464a2ed165fSKevin Bowling error = EOPNOTSUPP;
1465a2ed165fSKevin Bowling break;
1466a2ed165fSKevin Bowling }
1467a2ed165fSKevin Bowling
1468a2ed165fSKevin Bowling msg[0] = igb_iov_reply_header(msg[0], true, error == 0);
1469a2ed165fSKevin Bowling e1000_write_mbx(hw, msg, 1, vf->pool);
1470a2ed165fSKevin Bowling return (true);
1471a2ed165fSKevin Bowling }
1472a2ed165fSKevin Bowling
1473a2ed165fSKevin Bowling static sbintime_t
igb_iov_service_pending_mbx(struct e1000_softc * sc,struct igb_vf * vf,sbintime_t now)1474a2ed165fSKevin Bowling igb_iov_service_pending_mbx(struct e1000_softc *sc, struct igb_vf *vf,
1475a2ed165fSKevin Bowling sbintime_t now)
1476a2ed165fSKevin Bowling {
1477a2ed165fSKevin Bowling sbintime_t delay;
1478a2ed165fSKevin Bowling
1479a2ed165fSKevin Bowling if ((vf->flags & IGB_VF_MBX_PENDING) == 0)
1480a2ed165fSKevin Bowling return (0);
1481a2ed165fSKevin Bowling if (vf->mbx_retry_at != 0 && now < vf->mbx_retry_at)
1482a2ed165fSKevin Bowling return (vf->mbx_retry_at);
1483a2ed165fSKevin Bowling if (igb_iov_process_msg(sc, vf))
1484a2ed165fSKevin Bowling return (0);
1485a2ed165fSKevin Bowling
1486a2ed165fSKevin Bowling now = getsbinuptime();
1487a2ed165fSKevin Bowling if (vf->mbx_retry_count < IGB_IOV_MBX_RETRY_COUNT) {
1488a2ed165fSKevin Bowling delay = igb_iov_mbx_retry_delay[vf->mbx_retry_count++];
1489a2ed165fSKevin Bowling vf->mbx_retry_at = now + delay;
1490a2ed165fSKevin Bowling return (vf->mbx_retry_at);
1491a2ed165fSKevin Bowling }
1492a2ed165fSKevin Bowling
1493a2ed165fSKevin Bowling vf->flags &= ~(IGB_VF_CTS | IGB_VF_MBX_PENDING);
1494a2ed165fSKevin Bowling vf->flags |= IGB_VF_MBX_GAVE_UP;
1495a2ed165fSKevin Bowling vf->mbx_retry_at = 0;
1496a2ed165fSKevin Bowling if (ratecheck(&vf->last_mbx_log, &igb_iov_mbx_log_interval))
1497a2ed165fSKevin Bowling device_printf(sc->dev,
1498a2ed165fSKevin Bowling "mailbox remained busy for VF %u; CTS revoked\n",
1499a2ed165fSKevin Bowling vf->pool);
1500a2ed165fSKevin Bowling return (0);
1501a2ed165fSKevin Bowling }
1502a2ed165fSKevin Bowling
1503a2ed165fSKevin Bowling void
igb_iov_handle_mbx(struct e1000_softc * sc)1504a2ed165fSKevin Bowling igb_iov_handle_mbx(struct e1000_softc *sc)
1505a2ed165fSKevin Bowling {
1506a2ed165fSKevin Bowling struct e1000_hw *hw;
1507a2ed165fSKevin Bowling struct igb_vf *vf;
1508a2ed165fSKevin Bowling sbintime_t delay, next_retry_at, now, retry_at;
1509a2ed165fSKevin Bowling u32 msg;
1510a2ed165fSKevin Bowling int i;
1511a2ed165fSKevin Bowling
1512a2ed165fSKevin Bowling if (!sc->iov_hw_active)
1513a2ed165fSKevin Bowling return;
1514a2ed165fSKevin Bowling
1515a2ed165fSKevin Bowling hw = &sc->hw;
1516a2ed165fSKevin Bowling next_retry_at = 0;
1517a2ed165fSKevin Bowling for (i = 0; i < sc->num_vfs; i++) {
1518a2ed165fSKevin Bowling vf = &sc->vfs[i];
1519a2ed165fSKevin Bowling if (!(vf->flags & IGB_VF_ACTIVE))
1520a2ed165fSKevin Bowling continue;
1521a2ed165fSKevin Bowling now = getsbinuptime();
15228c872470SKevin Bowling if (e1000_check_for_rst(hw, vf->pool) == 0) {
15238c872470SKevin Bowling /*
15248c872470SKevin Bowling * The old VF is gone. A new owner's reset handshake
15258c872470SKevin Bowling * reruns sanitization before enabling its pool.
15268c872470SKevin Bowling */
15278c872470SKevin Bowling (void)igb_iov_reset_event(sc, vf);
15288c872470SKevin Bowling }
1529a2ed165fSKevin Bowling if ((vf->flags &
1530a2ed165fSKevin Bowling (IGB_VF_MBX_PENDING | IGB_VF_MBX_GAVE_UP)) == 0 &&
1531a2ed165fSKevin Bowling e1000_check_for_msg(hw, vf->pool) == 0) {
1532a2ed165fSKevin Bowling vf->flags |= IGB_VF_MBX_PENDING;
1533a2ed165fSKevin Bowling vf->mbx_retry_at = 0;
1534a2ed165fSKevin Bowling vf->mbx_retry_count = 0;
1535a2ed165fSKevin Bowling }
1536a2ed165fSKevin Bowling retry_at = igb_iov_service_pending_mbx(sc, vf, now);
1537a2ed165fSKevin Bowling if (retry_at != 0 &&
1538a2ed165fSKevin Bowling (next_retry_at == 0 || retry_at < next_retry_at))
1539a2ed165fSKevin Bowling next_retry_at = retry_at;
1540a2ed165fSKevin Bowling if (e1000_check_for_ack(hw, vf->pool) == 0 &&
1541a2ed165fSKevin Bowling !(vf->flags & IGB_VF_CTS) && igb_iov_nack_allowed(vf)) {
1542a2ed165fSKevin Bowling msg = E1000_VT_MSGTYPE_NACK;
1543a2ed165fSKevin Bowling e1000_write_mbx(hw, &msg, 1, vf->pool);
1544a2ed165fSKevin Bowling }
1545a2ed165fSKevin Bowling }
1546a2ed165fSKevin Bowling if (next_retry_at != 0) {
1547a2ed165fSKevin Bowling delay = next_retry_at - getsbinuptime();
1548a2ed165fSKevin Bowling if (delay <= 0)
1549a2ed165fSKevin Bowling delay = SBT_1MS;
1550a2ed165fSKevin Bowling callout_reset_sbt(&sc->iov_mbx_retry, delay, 0,
1551a2ed165fSKevin Bowling igb_iov_mbx_retry_callout, sc, C_PREL(1));
1552a2ed165fSKevin Bowling }
1553a2ed165fSKevin Bowling }
1554a2ed165fSKevin Bowling
1555a2ed165fSKevin Bowling static bool
igb_iov_notify_vf_mdd_reset(struct e1000_softc * sc,struct igb_vf * vf)1556a2ed165fSKevin Bowling igb_iov_notify_vf_mdd_reset(struct e1000_softc *sc, struct igb_vf *vf)
1557a2ed165fSKevin Bowling {
1558a2ed165fSKevin Bowling u32 msg;
1559a2ed165fSKevin Bowling
1560a2ed165fSKevin Bowling /*
1561a2ed165fSKevin Bowling * MDD recovery preserves the VF's admin-vector configuration. Send
1562a2ed165fSKevin Bowling * the same no-CTS control message used for PF reset notification so
1563a2ed165fSKevin Bowling * the VF discards its state and completes a new reset handshake.
1564a2ed165fSKevin Bowling * A failed write is retried from the timer-driven admin pass; the VF's
1565a2ed165fSKevin Bowling * transmit watchdog remains the final fallback when traffic is still
1566a2ed165fSKevin Bowling * queued and notification never succeeds.
1567a2ed165fSKevin Bowling */
1568a2ed165fSKevin Bowling msg = E1000_PF_CONTROL_MSG;
1569a2ed165fSKevin Bowling if (e1000_write_mbx(&sc->hw, &msg, 1, vf->pool) != 0) {
1570a2ed165fSKevin Bowling vf->mdd_notify_at =
1571a2ed165fSKevin Bowling getsbinuptime() + igb_iov_mdd_notify_retry;
1572a2ed165fSKevin Bowling if (ratecheck(&vf->last_mbx_log,
1573a2ed165fSKevin Bowling &igb_iov_mbx_log_interval))
1574a2ed165fSKevin Bowling device_printf(sc->dev,
1575a2ed165fSKevin Bowling "could not notify VF %u of malicious-driver "
1576a2ed165fSKevin Bowling "reset; will retry\n", vf->pool);
1577a2ed165fSKevin Bowling return (false);
1578a2ed165fSKevin Bowling }
1579a2ed165fSKevin Bowling vf->flags &= ~IGB_VF_MDD_NOTIFY_PENDING;
1580a2ed165fSKevin Bowling vf->mdd_notify_at = 0;
1581a2ed165fSKevin Bowling return (true);
1582a2ed165fSKevin Bowling }
1583a2ed165fSKevin Bowling
1584a2ed165fSKevin Bowling void
igb_iov_handle_mdd(struct e1000_softc * sc)1585a2ed165fSKevin Bowling igb_iov_handle_mdd(struct e1000_softc *sc)
1586a2ed165fSKevin Bowling {
1587a2ed165fSKevin Bowling struct igb_vf *vf;
1588ac2be06eSKevin Bowling u32 blocked, cleared, handled, lvmmc;
1589a2ed165fSKevin Bowling u32 readback, spoofed;
1590a2ed165fSKevin Bowling bool mdfb_valid, pending;
1591a2ed165fSKevin Bowling int i;
1592a2ed165fSKevin Bowling
1593a2ed165fSKevin Bowling pending = atomic_readandclear_32(&sc->iov_pending) != 0;
1594a2ed165fSKevin Bowling lvmmc = pending ?
1595a2ed165fSKevin Bowling atomic_readandclear_32(&sc->iov_mdd_cause) : 0;
1596ac2be06eSKevin Bowling if (!sc->iov_hw_active) {
1597ac2be06eSKevin Bowling atomic_readandclear_32(&sc->iov_spoof_pending);
1598ac2be06eSKevin Bowling atomic_readandclear_32(&sc->iov_blocked_pending);
1599a2ed165fSKevin Bowling return;
1600ac2be06eSKevin Bowling }
1601a2ed165fSKevin Bowling
1602a2ed165fSKevin Bowling blocked = 0;
1603a2ed165fSKevin Bowling handled = 0;
1604a2ed165fSKevin Bowling mdfb_valid = false;
1605a2ed165fSKevin Bowling if (sc->hw.mac.type == e1000_i350) {
1606a2ed165fSKevin Bowling u32 mdfb;
1607a2ed165fSKevin Bowling
1608ac2be06eSKevin Bowling spoofed = atomic_readandclear_32(&sc->iov_spoof_pending);
1609a2ed165fSKevin Bowling /*
1610a2ed165fSKevin Bowling * I350 reports ordinary MAC/VLAN spoofing through the
1611a2ed165fSKevin Bowling * interrupt-time LVMMC snapshot rather than WVBR. The
1612a2ed165fSKevin Bowling * filter accumulates Last_Q into iov_spoof_pending so events
1613a2ed165fSKevin Bowling * from different VFs coalesce safely until this timer-driven
1614a2ed165fSKevin Bowling * admin pass.
1615a2ed165fSKevin Bowling */
1616a2ed165fSKevin Bowling spoofed &= IGB_I350_QUEUE_MASK;
1617a2ed165fSKevin Bowling /*
1618a2ed165fSKevin Bowling * Sample MDFB on every admin pass so a blocked queue is not
1619a2ed165fSKevin Bowling * mislabeled as an ordinary spoof when no MDDET observation
1620a2ed165fSKevin Bowling * is pending.
1621a2ed165fSKevin Bowling */
1622a2ed165fSKevin Bowling mdfb = E1000_READ_REG(&sc->hw, E1000_MDFB);
1623a2ed165fSKevin Bowling if (__predict_false(mdfb == 0xffffffff))
1624a2ed165fSKevin Bowling mdfb = 0;
1625a2ed165fSKevin Bowling else {
1626a2ed165fSKevin Bowling mdfb &= IGB_I350_QUEUE_MASK;
1627a2ed165fSKevin Bowling mdfb_valid = true;
1628a2ed165fSKevin Bowling }
1629a2ed165fSKevin Bowling /*
1630a2ed165fSKevin Bowling * I350 SDM sections 8.14.10 and 8.14.11: WVBR reports
1631a2ed165fSKevin Bowling * spoof and malicious-driver events, while MDFB identifies
1632a2ed165fSKevin Bowling * the queues actually blocked for malicious behavior.
1633a2ed165fSKevin Bowling */
1634a2ed165fSKevin Bowling spoofed &= ~mdfb;
1635a2ed165fSKevin Bowling blocked = mdfb;
1636a2ed165fSKevin Bowling if (blocked != 0 && lvmmc == 0)
1637a2ed165fSKevin Bowling lvmmc = E1000_READ_REG(&sc->hw, E1000_LVMMC);
1638a2ed165fSKevin Bowling /*
1639a2ed165fSKevin Bowling * A failed diagnostic read does not invalidate the
1640a2ed165fSKevin Bowling * blocked-queue bitmap that was read successfully above.
1641a2ed165fSKevin Bowling */
1642a2ed165fSKevin Bowling if (__predict_false(lvmmc == 0xffffffff))
1643a2ed165fSKevin Bowling lvmmc = 0;
1644a2ed165fSKevin Bowling /*
1645a2ed165fSKevin Bowling * MDFB is authoritative for queues stopped by malicious-driver
1646a2ed165fSKevin Bowling * detection. LVMMC reports causes such as VLAN IERR and
1647a2ed165fSKevin Bowling * Mal_PF, but its Last_Q field does not establish that a queue
1648a2ed165fSKevin Bowling * was blocked. Do not manufacture a blocked bit when MDFB is
1649a2ed165fSKevin Bowling * clear.
1650a2ed165fSKevin Bowling */
1651a2ed165fSKevin Bowling } else {
1652a2ed165fSKevin Bowling if (!pending)
1653a2ed165fSKevin Bowling return;
1654ac2be06eSKevin Bowling /*
1655ac2be06eSKevin Bowling * WVBR is read-clear and does not preserve every queue across
1656ac2be06eSKevin Bowling * multiple MDDET interrupts. The interrupt filter snapshots and
1657ac2be06eSKevin Bowling * accumulates its pool bitmaps before this deferred admin pass.
1658ac2be06eSKevin Bowling */
1659ac2be06eSKevin Bowling spoofed = atomic_readandclear_32(&sc->iov_spoof_pending);
1660ac2be06eSKevin Bowling blocked = atomic_readandclear_32(&sc->iov_blocked_pending);
1661ac2be06eSKevin Bowling /* A blocked-queue classification dominates its WVBR low bit. */
1662ac2be06eSKevin Bowling spoofed &= ~blocked;
1663a2ed165fSKevin Bowling }
1664a2ed165fSKevin Bowling
1665a2ed165fSKevin Bowling for (i = 0; i < sc->num_vfs; i++) {
1666a2ed165fSKevin Bowling vf = &sc->vfs[i];
1667a2ed165fSKevin Bowling if (!(vf->flags & IGB_VF_ACTIVE))
1668a2ed165fSKevin Bowling continue;
1669a2ed165fSKevin Bowling if ((vf->flags & IGB_VF_MDD_NOTIFY_PENDING) != 0 &&
1670a2ed165fSKevin Bowling getsbinuptime() >= vf->mdd_notify_at)
1671a2ed165fSKevin Bowling (void)igb_iov_notify_vf_mdd_reset(sc, vf);
1672a2ed165fSKevin Bowling /*
1673a2ed165fSKevin Bowling * An invalid MDFB sample must neither report a new edge nor
1674a2ed165fSKevin Bowling * masquerade as evidence that an old edge has cleared.
1675a2ed165fSKevin Bowling */
1676a2ed165fSKevin Bowling if (sc->hw.mac.type == e1000_i350 && mdfb_valid &&
1677a2ed165fSKevin Bowling (blocked & (1U << i)) == 0)
1678a2ed165fSKevin Bowling vf->flags &= ~IGB_VF_MDD_BLOCKED;
1679a2ed165fSKevin Bowling if ((spoofed & (1U << i)) != 0 &&
1680a2ed165fSKevin Bowling ratecheck(&vf->last_spoof_log,
1681a2ed165fSKevin Bowling &igb_iov_spoof_log_interval))
1682a2ed165fSKevin Bowling device_printf(sc->dev,
1683a2ed165fSKevin Bowling "spoof event detected from VF %u; packet dropped\n",
1684a2ed165fSKevin Bowling vf->pool);
1685a2ed165fSKevin Bowling if ((blocked & (1U << i)) == 0)
1686a2ed165fSKevin Bowling continue;
1687a2ed165fSKevin Bowling if ((vf->flags & IGB_VF_MDD_BLOCKED) != 0)
1688a2ed165fSKevin Bowling continue;
1689a2ed165fSKevin Bowling vf->flags |= IGB_VF_MDD_BLOCKED;
1690a2ed165fSKevin Bowling if (ratecheck(&vf->last_mdd_log, &igb_iov_mdd_log_interval))
1691a2ed165fSKevin Bowling device_printf(sc->dev,
1692a2ed165fSKevin Bowling "malicious-driver event 0x%08x from VF %u; "
1693a2ed165fSKevin Bowling "resetting VF\n", lvmmc, vf->pool);
1694a2ed165fSKevin Bowling igb_iov_mdd_reset_event(sc, vf);
1695a2ed165fSKevin Bowling vf->flags |= IGB_VF_MDD_NOTIFY_PENDING;
1696a2ed165fSKevin Bowling (void)igb_iov_notify_vf_mdd_reset(sc, vf);
1697a2ed165fSKevin Bowling handled |= 1U << i;
1698a2ed165fSKevin Bowling }
1699a2ed165fSKevin Bowling if (sc->hw.mac.type == e1000_i350 && mdfb_valid &&
1700a2ed165fSKevin Bowling (blocked & (1U << sc->pool)) == 0)
1701a2ed165fSKevin Bowling sc->iov_pf_mdd_blocked = false;
1702a2ed165fSKevin Bowling if ((blocked & (1U << sc->pool)) != 0 &&
1703a2ed165fSKevin Bowling (sc->hw.mac.type != e1000_i350 ||
1704a2ed165fSKevin Bowling !sc->iov_pf_mdd_blocked)) {
1705a2ed165fSKevin Bowling if (sc->hw.mac.type == e1000_i350)
1706a2ed165fSKevin Bowling sc->iov_pf_mdd_blocked = true;
1707a2ed165fSKevin Bowling if (ratecheck(&sc->iov_last_mdd_log,
1708a2ed165fSKevin Bowling &igb_iov_mdd_log_interval))
1709a2ed165fSKevin Bowling device_printf(sc->dev,
1710a2ed165fSKevin Bowling "malicious-driver event 0x%08x from PF queue; "
1711a2ed165fSKevin Bowling "resetting PF\n", lvmmc);
1712a2ed165fSKevin Bowling iflib_request_reset(sc->ctx);
1713a2ed165fSKevin Bowling iflib_admin_intr_deferred(sc->ctx);
1714a2ed165fSKevin Bowling handled |= 1U << sc->pool;
1715a2ed165fSKevin Bowling }
1716a2ed165fSKevin Bowling if (sc->hw.mac.type == e1000_i350 && handled != 0) {
1717a2ed165fSKevin Bowling /*
1718a2ed165fSKevin Bowling * I350 documentation conflicts: the register summary calls
1719a2ed165fSKevin Bowling * MDFB RWS while the detailed field table calls it RO. I350
1720a2ed165fSKevin Bowling * silicon clears a blocked bit when software writes it back.
1721a2ed165fSKevin Bowling * Write only bits whose recovery was initiated. If a revision
1722a2ed165fSKevin Bowling * instead implements MDFB as RO, the edge latch above prevents
1723a2ed165fSKevin Bowling * a reset loop and this one transition-time write is harmless.
1724a2ed165fSKevin Bowling */
1725a2ed165fSKevin Bowling E1000_WRITE_REG(&sc->hw, E1000_MDFB, handled);
1726a2ed165fSKevin Bowling E1000_WRITE_FLUSH(&sc->hw);
1727a2ed165fSKevin Bowling /*
1728a2ed165fSKevin Bowling * Rearm from observed hardware state instead of waiting for
1729a2ed165fSKevin Bowling * the next admin pass. The PF context lock prevents a reset
1730a2ed165fSKevin Bowling * handshake from re-enabling the VF before this readback. A
1731a2ed165fSKevin Bowling * write-to-clear part reports zero; a read-only part retains
1732a2ed165fSKevin Bowling * the bit and therefore retains the one-shot edge latch.
1733a2ed165fSKevin Bowling */
1734a2ed165fSKevin Bowling readback = E1000_READ_REG(&sc->hw, E1000_MDFB);
1735a2ed165fSKevin Bowling if (__predict_false(readback == 0xffffffff))
1736a2ed165fSKevin Bowling cleared = 0;
1737a2ed165fSKevin Bowling else
1738a2ed165fSKevin Bowling cleared = handled &
1739a2ed165fSKevin Bowling ~(readback & IGB_I350_QUEUE_MASK);
1740a2ed165fSKevin Bowling for (i = 0; i < sc->num_vfs; i++)
1741a2ed165fSKevin Bowling if ((cleared & (1U << i)) != 0)
1742a2ed165fSKevin Bowling sc->vfs[i].flags &= ~IGB_VF_MDD_BLOCKED;
1743a2ed165fSKevin Bowling if ((cleared & (1U << sc->pool)) != 0)
1744a2ed165fSKevin Bowling sc->iov_pf_mdd_blocked = false;
1745a2ed165fSKevin Bowling }
17461c91c3aeSKevin Bowling if (sc->hw.mac.type == e1000_i350) {
17471c91c3aeSKevin Bowling /*
17481c91c3aeSKevin Bowling * I350 can retain EICR.OTHER without delivering the admin MSI-X
17491c91c3aeSKevin Bowling * even though its EIMS and legacy IMS bits remain enabled. Kick
17501c91c3aeSKevin Bowling * the already-enabled vector on each admin pass so its filter
17511c91c3aeSKevin Bowling * consumes any retained ICR/LVMMC cause. A synthetic interrupt
17521c91c3aeSKevin Bowling * with no legacy cause is handled entirely by the filter.
17531c91c3aeSKevin Bowling */
17541c91c3aeSKevin Bowling E1000_WRITE_REG(&sc->hw, E1000_EICS, sc->link_mask);
17551c91c3aeSKevin Bowling E1000_WRITE_FLUSH(&sc->hw);
17561c91c3aeSKevin Bowling }
1757a2ed165fSKevin Bowling }
1758a2ed165fSKevin Bowling
1759a2ed165fSKevin Bowling void
igb_iov_mdd_event(struct e1000_softc * sc)1760a2ed165fSKevin Bowling igb_iov_mdd_event(struct e1000_softc *sc)
1761a2ed165fSKevin Bowling {
1762ac2be06eSKevin Bowling u32 blocked, cause, queues, queue, spoofed, wvbr;
1763a2ed165fSKevin Bowling
1764a2ed165fSKevin Bowling /*
1765a2ed165fSKevin Bowling * LVMMC is clear-on-read. Preserve it in the interrupt filter, as
1766a2ed165fSKevin Bowling * Intel's igb driver does, rather than deferring the only copy.
1767a2ed165fSKevin Bowling */
1768a2ed165fSKevin Bowling cause = E1000_READ_REG(&sc->hw, E1000_LVMMC);
1769a2ed165fSKevin Bowling if (__predict_false(cause == 0xffffffff))
1770a2ed165fSKevin Bowling return;
1771ac2be06eSKevin Bowling if (sc->hw.mac.type == e1000_82576) {
1772ac2be06eSKevin Bowling /*
1773ac2be06eSKevin Bowling * Snapshot WVBR in the interrupt filter. Waiting for the admin
1774ac2be06eSKevin Bowling * task loses all but the last of back-to-back VF MDD events on
1775ac2be06eSKevin Bowling * 82576. Convert the staggered queue map into pool bits and OR
1776ac2be06eSKevin Bowling * each observation into software latches for deferred recovery.
1777ac2be06eSKevin Bowling */
1778ac2be06eSKevin Bowling wvbr = E1000_READ_REG(&sc->hw, E1000_WVBR);
1779ac2be06eSKevin Bowling if (__predict_false(wvbr == 0xffffffff)) {
1780ac2be06eSKevin Bowling spoofed = 0;
1781ac2be06eSKevin Bowling blocked = 0;
1782ac2be06eSKevin Bowling } else {
1783ac2be06eSKevin Bowling queues = wvbr & IGB_82576_QUEUE_MASK;
1784ac2be06eSKevin Bowling spoofed = (queues & 0xff) |
1785ac2be06eSKevin Bowling (queues >> IGB_82576_STAGGERED_QUEUE_SHIFT);
1786ac2be06eSKevin Bowling queues = (wvbr >> 16) & IGB_82576_QUEUE_MASK;
1787ac2be06eSKevin Bowling blocked = (queues & 0xff) |
1788ac2be06eSKevin Bowling (queues >> IGB_82576_STAGGERED_QUEUE_SHIFT);
1789ac2be06eSKevin Bowling }
1790ac2be06eSKevin Bowling SDT_PROBE4(igb_iov, mdd, sample, wvbr, sc, wvbr, spoofed,
1791ac2be06eSKevin Bowling blocked);
1792ac2be06eSKevin Bowling /*
1793ac2be06eSKevin Bowling * 82576 can report a coalesced block-class event with all affected
1794ac2be06eSKevin Bowling * queues in WVBR's low half and no high-half blocked bits. If an
1795ac2be06eSKevin Bowling * ordinary spoof shares that snapshot, the register has no per-queue
1796ac2be06eSKevin Bowling * cause information. Deliberately fail closed by recovering every
1797ac2be06eSKevin Bowling * low-half queue; this can reset a spoof-only sibling, but avoids
1798ac2be06eSKevin Bowling * stranding a blocked VF. LVMMC.Last_Q identifies only the final
1799ac2be06eSKevin Bowling * event and lost simultaneous blocked VFs on tested silicon.
1800ac2be06eSKevin Bowling */
1801ac2be06eSKevin Bowling if (blocked == 0 &&
1802ac2be06eSKevin Bowling (cause & IGB_82576_LVMMC_BLOCK_MASK) != 0) {
1803ac2be06eSKevin Bowling blocked = spoofed;
1804ac2be06eSKevin Bowling if (blocked == 0) {
1805ac2be06eSKevin Bowling queue = (cause >> 16) & 0xf;
1806ac2be06eSKevin Bowling blocked = 1U << (queue & 0x7);
1807ac2be06eSKevin Bowling }
1808ac2be06eSKevin Bowling }
1809ac2be06eSKevin Bowling if (spoofed != 0)
1810ac2be06eSKevin Bowling atomic_set_32(&sc->iov_spoof_pending, spoofed);
1811ac2be06eSKevin Bowling if (blocked != 0)
1812ac2be06eSKevin Bowling atomic_set_32(&sc->iov_blocked_pending, blocked);
1813ac2be06eSKevin Bowling }
1814a2ed165fSKevin Bowling if (sc->hw.mac.type == e1000_i350 &&
1815a2ed165fSKevin Bowling (cause & IGB_I350_LVMMC_MAC_VLAN_SPOOF) != 0) {
1816a2ed165fSKevin Bowling queue = (cause >> IGB_I350_LVMMC_LAST_Q_SHIFT) &
1817a2ed165fSKevin Bowling IGB_I350_LVMMC_LAST_Q_MASK;
1818a2ed165fSKevin Bowling /*
1819a2ed165fSKevin Bowling * FreeBSD assigns one queue to each VF pool, so Last_Q is
1820a2ed165fSKevin Bowling * also the VF number. Preserve all VFs observed before the
1821a2ed165fSKevin Bowling * timer pass, and do not overwrite an unrelated blocked
1822a2ed165fSKevin Bowling * queue's diagnostic with this non-blocking spoof event.
1823a2ed165fSKevin Bowling */
1824a2ed165fSKevin Bowling atomic_set_32(&sc->iov_spoof_pending, 1U << queue);
1825a2ed165fSKevin Bowling return;
1826a2ed165fSKevin Bowling }
1827a2ed165fSKevin Bowling atomic_store_rel_32(&sc->iov_mdd_cause, cause);
1828a2ed165fSKevin Bowling atomic_set_32(&sc->iov_pending, 1);
1829a2ed165fSKevin Bowling }
1830a2ed165fSKevin Bowling
1831a2ed165fSKevin Bowling void
igb_iov_ping_all_vfs(struct e1000_softc * sc)1832a2ed165fSKevin Bowling igb_iov_ping_all_vfs(struct e1000_softc *sc)
1833a2ed165fSKevin Bowling {
1834a2ed165fSKevin Bowling struct igb_vf *vf;
1835a2ed165fSKevin Bowling u32 msg;
1836a2ed165fSKevin Bowling int i;
1837a2ed165fSKevin Bowling
1838a2ed165fSKevin Bowling if (!sc->iov_hw_active)
1839a2ed165fSKevin Bowling return;
1840a2ed165fSKevin Bowling
1841a2ed165fSKevin Bowling for (i = 0; i < sc->num_vfs; i++) {
1842a2ed165fSKevin Bowling vf = &sc->vfs[i];
1843a2ed165fSKevin Bowling if (!(vf->flags & IGB_VF_ACTIVE))
1844a2ed165fSKevin Bowling continue;
1845a2ed165fSKevin Bowling msg = E1000_PF_CONTROL_MSG;
1846a2ed165fSKevin Bowling if (vf->flags & IGB_VF_CTS)
1847a2ed165fSKevin Bowling msg |= E1000_VT_MSGTYPE_CTS;
1848a2ed165fSKevin Bowling e1000_write_mbx(&sc->hw, &msg, 1, vf->pool);
1849a2ed165fSKevin Bowling }
1850a2ed165fSKevin Bowling }
1851a2ed165fSKevin Bowling
1852a2ed165fSKevin Bowling void
igb_iov_initialize(struct e1000_softc * sc)1853a2ed165fSKevin Bowling igb_iov_initialize(struct e1000_softc *sc)
1854a2ed165fSKevin Bowling {
1855a2ed165fSKevin Bowling struct e1000_hw *hw;
1856a2ed165fSKevin Bowling struct igb_vf *vf;
1857a2ed165fSKevin Bowling u32 ctrl_ext, dtxctl, mask, rctl, rplolr, vt_ctl;
1858a2ed165fSKevin Bowling int i;
1859a2ed165fSKevin Bowling
1860a2ed165fSKevin Bowling if (sc->num_vfs == 0)
1861a2ed165fSKevin Bowling return;
1862a2ed165fSKevin Bowling
1863a2ed165fSKevin Bowling hw = &sc->hw;
1864a2ed165fSKevin Bowling atomic_readandclear_32(&sc->iov_mdd_cause);
1865a2ed165fSKevin Bowling atomic_readandclear_32(&sc->iov_pending);
1866a2ed165fSKevin Bowling atomic_readandclear_32(&sc->iov_spoof_pending);
1867ac2be06eSKevin Bowling atomic_readandclear_32(&sc->iov_blocked_pending);
1868ac2be06eSKevin Bowling /* Plain VMDq keeps every 82576 PF/VF pool on queue zero. */
1869a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_MRQC, E1000_MRQC_ENABLE_VMDQ);
1870a2ed165fSKevin Bowling
1871a2ed165fSKevin Bowling vt_ctl = E1000_READ_REG(hw, E1000_VT_CTL);
1872a2ed165fSKevin Bowling vt_ctl &= ~(E1000_VT_CTL_DEFAULT_POOL_MASK |
1873a2ed165fSKevin Bowling E1000_VT_CTL_DISABLE_DEF_POOL);
1874a2ed165fSKevin Bowling vt_ctl |= sc->pool << E1000_VT_CTL_DEFAULT_POOL_SHIFT;
1875a2ed165fSKevin Bowling vt_ctl |= E1000_VT_CTL_VM_REPL_EN;
1876a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_VT_CTL, vt_ctl);
1877a2ed165fSKevin Bowling
1878a2ed165fSKevin Bowling mask = 1U << sc->pool;
1879a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_VFRE, mask);
1880a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_VFTE, mask);
1881a2ed165fSKevin Bowling /* A VF without RX descriptors must not block any other pool. */
1882a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_QDE,
1883a2ed165fSKevin Bowling hw->mac.type == e1000_i350 ? IGB_I350_QUEUE_MASK : ALL_QUEUES);
1884a2ed165fSKevin Bowling e1000_vmdq_set_loopback_pf(hw, true);
1885a2ed165fSKevin Bowling dtxctl = E1000_READ_REG(hw, E1000_DTXCTL);
1886a2ed165fSKevin Bowling dtxctl |= E1000_DTXCTL_MDP_EN;
1887a2ed165fSKevin Bowling if (hw->mac.type == e1000_82576) {
1888a2ed165fSKevin Bowling dtxctl |= E1000_DTXCTL_VLAN_ADDED |
1889a2ed165fSKevin Bowling E1000_DTXCTL_SPOOF_INT;
1890a2ed165fSKevin Bowling rplolr = E1000_READ_REG(hw, E1000_RPLOLR);
1891a2ed165fSKevin Bowling rplolr |= E1000_RPLOLR_STRVLAN;
1892a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_RPLOLR, rplolr);
1893a2ed165fSKevin Bowling } else {
1894a2ed165fSKevin Bowling /*
1895a2ed165fSKevin Bowling * I350 SDM section 8.12.5 defines this field with inverted
1896a2ed165fSKevin Bowling * polarity: setting it keeps an ordinary spoof from disabling
1897a2ed165fSKevin Bowling * the VF queue. Enable its notification as well. I350
1898a2ed165fSKevin Bowling * hardware reports the VF in LVMMC.Last_Q (WVBR remains zero);
1899a2ed165fSKevin Bowling * the moderated admin vector captures that value, while
1900a2ed165fSKevin Bowling * timer-driven administration and per-VF ratecheck bound the
1901a2ed165fSKevin Bowling * work and console output.
1902a2ed165fSKevin Bowling */
1903a2ed165fSKevin Bowling dtxctl |= E1000_DTXCTL_SPOOF_INT |
1904a2ed165fSKevin Bowling IGB_I350_DTXCTL_ENABLE_SPOOF_QUEUE;
1905a2ed165fSKevin Bowling }
1906a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_DTXCTL, dtxctl);
1907a2ed165fSKevin Bowling
1908a2ed165fSKevin Bowling igb_iov_map_rar(sc, 0, hw->mac.addr, sc->pool);
1909a2ed165fSKevin Bowling igb_iov_configure_pf_vmolr(sc);
1910a2ed165fSKevin Bowling igb_iov_set_uta(sc);
1911a2ed165fSKevin Bowling for (i = 0; i < sc->num_vfs; i++) {
1912a2ed165fSKevin Bowling vf = &sc->vfs[i];
1913a2ed165fSKevin Bowling if (!(vf->flags & IGB_VF_ACTIVE))
1914a2ed165fSKevin Bowling continue;
1915e8f3b96bSKevin Bowling /*
1916e8f3b96bSKevin Bowling * A PF-wide reset is trusted and can require a complete guest
1917e8f3b96bSKevin Bowling * replay. Guest-controlled RESET and VFLR do not refill this
1918e8f3b96bSKevin Bowling * allowance.
1919e8f3b96bSKevin Bowling */
1920e8f3b96bSKevin Bowling igb_iov_reset_vlan_rate(vf);
1921a2ed165fSKevin Bowling igb_iov_clear_mac_filters(sc, vf);
1922a2ed165fSKevin Bowling igb_iov_reset_vf_state(sc, vf);
1923a2ed165fSKevin Bowling igb_iov_clear_rar(sc, vf->rar_index);
1924a2ed165fSKevin Bowling igb_iov_set_anti_spoof(sc, vf);
1925a2ed165fSKevin Bowling }
1926a2ed165fSKevin Bowling igb_iov_rebuild_mta(sc);
1927a2ed165fSKevin Bowling igb_iov_rebuild_vlan(sc);
1928a2ed165fSKevin Bowling
1929a2ed165fSKevin Bowling rctl = E1000_READ_REG(hw, E1000_RCTL);
1930a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_RCTL, rctl | E1000_RCTL_VFE);
1931a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_MBVFIMR, igb_iov_active_mask(sc));
1932a2ed165fSKevin Bowling if (hw->mac.type == e1000_i350)
1933a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_DMACR, 0);
1934a2ed165fSKevin Bowling
1935a2ed165fSKevin Bowling ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
1936a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_CTRL_EXT,
1937a2ed165fSKevin Bowling ctrl_ext | E1000_CTRL_EXT_PFRSTD);
1938a2ed165fSKevin Bowling E1000_WRITE_FLUSH(hw);
1939176259efSKevin Bowling /*
1940176259efSKevin Bowling * MDDET remains masked until iov_hw_active is published and iflib
1941176259efSKevin Bowling * rearms the admin vector. Programming the per-pool policy above can
1942176259efSKevin Bowling * leave a setup-time MDDET observation in the read-clear registers.
1943176259efSKevin Bowling * If that stale cause is carried across the unmask, a later ordinary
1944176259efSKevin Bowling * spoof can update LVMMC without generating a new interrupt edge.
1945176259efSKevin Bowling *
1946176259efSKevin Bowling * Drain only after all IOV policy is installed and before exposing the
1947176259efSKevin Bowling * active state. Mailbox requests are also serviced by the periodic
1948176259efSKevin Bowling * admin pass, and ping_all_vfs() below supplies a fresh notification.
1949176259efSKevin Bowling */
19501c91c3aeSKevin Bowling /*
19511c91c3aeSKevin Bowling * Clear the setup-time interrupt latch before its diagnostic state.
19521c91c3aeSKevin Bowling * I350 does not reliably generate the next MDDET edge when LVMMC is
19531c91c3aeSKevin Bowling * consumed while ICR.MDDET remains latched. This differs deliberately
19541c91c3aeSKevin Bowling * from the final arm-time drain, where ICR is read last so a later event
19551c91c3aeSKevin Bowling * remains pending for the unmask.
19561c91c3aeSKevin Bowling */
19571c91c3aeSKevin Bowling if (hw->mac.type == e1000_i350)
19581c91c3aeSKevin Bowling (void)E1000_READ_REG(hw, E1000_ICR);
1959176259efSKevin Bowling (void)E1000_READ_REG(hw, E1000_LVMMC);
1960176259efSKevin Bowling if (hw->mac.type == e1000_82576)
1961176259efSKevin Bowling (void)E1000_READ_REG(hw, E1000_WVBR);
19621c91c3aeSKevin Bowling if (hw->mac.type != e1000_i350)
1963176259efSKevin Bowling (void)E1000_READ_REG(hw, E1000_ICR);
1964176259efSKevin Bowling atomic_readandclear_32(&sc->iov_mdd_cause);
1965176259efSKevin Bowling atomic_readandclear_32(&sc->iov_pending);
1966176259efSKevin Bowling atomic_readandclear_32(&sc->iov_spoof_pending);
1967176259efSKevin Bowling atomic_readandclear_32(&sc->iov_blocked_pending);
1968176259efSKevin Bowling atomic_store_rel_32(&sc->iov_intr_drain_pending, 1);
1969a2ed165fSKevin Bowling sc->iov_hw_active = true;
1970a2ed165fSKevin Bowling igb_iov_ping_all_vfs(sc);
1971a2ed165fSKevin Bowling }
1972a2ed165fSKevin Bowling
1973a2ed165fSKevin Bowling int
igb_iov_validate(struct e1000_softc * sc,u16 num_vfs)1974a2ed165fSKevin Bowling igb_iov_validate(struct e1000_softc *sc, u16 num_vfs)
1975a2ed165fSKevin Bowling {
1976a2ed165fSKevin Bowling if (!igb_iov_supported(sc))
1977a2ed165fSKevin Bowling return (ENXIO);
1978a2ed165fSKevin Bowling /* One of the eight hardware pools is reserved for the PF. */
1979a2ed165fSKevin Bowling if (num_vfs == 0 || num_vfs > MAX_NUM_VFS)
1980a2ed165fSKevin Bowling return (EINVAL);
1981a2ed165fSKevin Bowling if (sc->vfs != NULL)
1982a2ed165fSKevin Bowling return (EBUSY);
1983a2ed165fSKevin Bowling if (sc->intr_type != IFLIB_INTR_MSIX) {
1984a2ed165fSKevin Bowling device_printf(sc->dev, "SR-IOV requires MSI-X\n");
1985a2ed165fSKevin Bowling return (ENOTSUP);
1986a2ed165fSKevin Bowling }
1987a2ed165fSKevin Bowling if (sc->tx_num_queues != 1 || sc->rx_num_queues != 1) {
1988a2ed165fSKevin Bowling device_printf(sc->dev,
1989a2ed165fSKevin Bowling "SR-IOV requires one PF TX and RX queue; set "
1990a2ed165fSKevin Bowling "dev.igb.%d.iflib.override_ntxqs=1 and "
1991a2ed165fSKevin Bowling "dev.igb.%d.iflib.override_nrxqs=1 before attach\n",
1992a2ed165fSKevin Bowling device_get_unit(sc->dev), device_get_unit(sc->dev));
1993a2ed165fSKevin Bowling return (EINVAL);
1994a2ed165fSKevin Bowling }
1995a2ed165fSKevin Bowling return (0);
1996a2ed165fSKevin Bowling }
1997a2ed165fSKevin Bowling
1998a2ed165fSKevin Bowling int
igb_if_iov_init(if_ctx_t ctx,u16 num_vfs,const nvlist_t * config)1999a2ed165fSKevin Bowling igb_if_iov_init(if_ctx_t ctx, u16 num_vfs, const nvlist_t *config)
2000a2ed165fSKevin Bowling {
2001a2ed165fSKevin Bowling struct e1000_softc *sc;
2002a2ed165fSKevin Bowling int error, i;
2003a2ed165fSKevin Bowling
2004a2ed165fSKevin Bowling sc = iflib_get_softc(ctx);
2005a2ed165fSKevin Bowling (void)config;
2006*2cf580c6SKevin Bowling /*
2007*2cf580c6SKevin Bowling * This callback may run while the PF is down. Record the software
2008*2cf580c6SKevin Bowling * layout here; igb_iov_initialize() programs it during interface init.
2009*2cf580c6SKevin Bowling */
2010a2ed165fSKevin Bowling atomic_store_rel_32(&sc->iov_teardown, 0);
2011a2ed165fSKevin Bowling error = igb_iov_validate(sc, num_vfs);
2012a2ed165fSKevin Bowling if (error != 0)
2013a2ed165fSKevin Bowling return (error);
2014a2ed165fSKevin Bowling
2015a2ed165fSKevin Bowling sc->vfs = mallocarray(num_vfs, sizeof(*sc->vfs), M_IGB_IOV,
2016a2ed165fSKevin Bowling M_WAITOK | M_ZERO);
2017a2ed165fSKevin Bowling sc->num_vf_mac_filters =
2018a2ed165fSKevin Bowling sc->hw.mac.rar_entry_count - num_vfs - 1;
2019a2ed165fSKevin Bowling sc->vf_mac_filters = mallocarray(sc->num_vf_mac_filters,
2020a2ed165fSKevin Bowling sizeof(*sc->vf_mac_filters), M_IGB_IOV, M_WAITOK | M_ZERO);
2021a2ed165fSKevin Bowling for (i = 0; i < sc->num_vf_mac_filters; i++)
2022a2ed165fSKevin Bowling sc->vf_mac_filters[i].rar_index = i + 1;
2023a2ed165fSKevin Bowling sc->pool = num_vfs;
2024350211abSKevin Bowling sc->iov_mta_valid = false;
2025a2ed165fSKevin Bowling sc->iov_pf_mdd_blocked = false;
2026a2ed165fSKevin Bowling sc->tx_queues[0].txr.me = sc->pool;
2027a2ed165fSKevin Bowling sc->rx_queues[0].rxr.me = sc->pool;
2028a2ed165fSKevin Bowling e1000_init_mbx_params_pf(&sc->hw);
2029a2ed165fSKevin Bowling sc->num_vfs = num_vfs;
2030a2ed165fSKevin Bowling return (0);
2031a2ed165fSKevin Bowling }
2032a2ed165fSKevin Bowling
2033a2ed165fSKevin Bowling void
igb_if_iov_uninit(if_ctx_t ctx)2034a2ed165fSKevin Bowling igb_if_iov_uninit(if_ctx_t ctx)
2035a2ed165fSKevin Bowling {
2036a2ed165fSKevin Bowling struct e1000_softc *sc;
2037a2ed165fSKevin Bowling struct e1000_hw *hw;
2038a2ed165fSKevin Bowling u32 mask, rah;
2039a2ed165fSKevin Bowling int error, i, iov_pos;
2040a2ed165fSKevin Bowling u16 iov_ctl;
2041a2ed165fSKevin Bowling
2042a2ed165fSKevin Bowling sc = iflib_get_softc(ctx);
2043a2ed165fSKevin Bowling if (sc->vfs == NULL)
2044a2ed165fSKevin Bowling return;
2045a2ed165fSKevin Bowling hw = &sc->hw;
2046a2ed165fSKevin Bowling sc->iov_hw_active = false;
2047a2ed165fSKevin Bowling if (sc->iov_mbx_retry_initialized)
2048a2ed165fSKevin Bowling callout_drain(&sc->iov_mbx_retry);
2049a2ed165fSKevin Bowling
2050a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_MBVFIMR, 0);
2051a2ed165fSKevin Bowling mask = 1U << sc->pool;
2052a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_VFRE, mask);
2053a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_VFTE, mask);
2054a2ed165fSKevin Bowling
2055a2ed165fSKevin Bowling /*
2056a2ed165fSKevin Bowling * pci_iov(4) invokes the driver before it clears VF Enable. Quiesce
2057a2ed165fSKevin Bowling * the VFs and clear it here so that 82576's queue-reuse interval is
2058a2ed165fSKevin Bowling * measured from the actual IOV-disable event.
2059a2ed165fSKevin Bowling */
2060a2ed165fSKevin Bowling error = pci_find_extcap(sc->dev, PCIZ_SRIOV, &iov_pos);
2061a2ed165fSKevin Bowling if (error == 0) {
2062a2ed165fSKevin Bowling iov_ctl = pci_read_config(sc->dev,
2063a2ed165fSKevin Bowling iov_pos + PCIR_SRIOV_CTL, 2);
2064a2ed165fSKevin Bowling iov_ctl &= ~(PCIM_SRIOV_VF_EN | PCIM_SRIOV_VF_MSE);
2065a2ed165fSKevin Bowling pci_write_config(sc->dev, iov_pos + PCIR_SRIOV_CTL,
2066a2ed165fSKevin Bowling iov_ctl, 2);
2067a2ed165fSKevin Bowling if (hw->mac.type == e1000_82576) {
2068a2ed165fSKevin Bowling pause("igbiov", MAX(1, howmany(hz, 10)));
2069a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_IOVCTL,
2070a2ed165fSKevin Bowling E1000_IOVCTL_REUSE_VFQ);
2071a2ed165fSKevin Bowling E1000_WRITE_FLUSH(hw);
2072a2ed165fSKevin Bowling pause("igbiov", MAX(1, howmany(hz, 10)));
2073a2ed165fSKevin Bowling }
2074a2ed165fSKevin Bowling } else
2075a2ed165fSKevin Bowling device_printf(sc->dev,
2076a2ed165fSKevin Bowling "could not disable PCI SR-IOV before queue reuse: %d\n",
2077a2ed165fSKevin Bowling error);
2078a2ed165fSKevin Bowling
2079a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_VT_CTL, 0);
2080a2ed165fSKevin Bowling e1000_vmdq_set_loopback_pf(hw, false);
2081a2ed165fSKevin Bowling e1000_vmdq_set_anti_spoofing_pf(hw, false, 0);
2082a2ed165fSKevin Bowling for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++)
2083a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_VLVF(i), 0);
2084a2ed165fSKevin Bowling for (i = 0; i < sc->num_vfs; i++)
2085a2ed165fSKevin Bowling if (sc->vfs[i].flags & IGB_VF_ACTIVE)
2086a2ed165fSKevin Bowling igb_iov_clear_rar(sc, sc->vfs[i].rar_index);
2087a2ed165fSKevin Bowling for (i = 0; i < sc->num_vf_mac_filters; i++)
2088a2ed165fSKevin Bowling if (sc->vf_mac_filters[i].active)
2089a2ed165fSKevin Bowling igb_iov_clear_rar(sc, sc->vf_mac_filters[i].rar_index);
2090a2ed165fSKevin Bowling rah = E1000_READ_REG(hw, E1000_RAH(0));
2091a2ed165fSKevin Bowling rah &= ~IGB_IOV_RAH_POOLSEL_MASK;
2092a2ed165fSKevin Bowling E1000_WRITE_REG(hw, E1000_RAH(0), rah);
2093a2ed165fSKevin Bowling
2094a2ed165fSKevin Bowling free(sc->vfs, M_IGB_IOV);
2095a2ed165fSKevin Bowling free(sc->vf_mac_filters, M_IGB_IOV);
2096a2ed165fSKevin Bowling sc->vfs = NULL;
2097a2ed165fSKevin Bowling sc->vf_mac_filters = NULL;
2098a2ed165fSKevin Bowling sc->num_vfs = 0;
2099a2ed165fSKevin Bowling sc->num_vf_mac_filters = 0;
2100a2ed165fSKevin Bowling sc->pool = 0;
2101350211abSKevin Bowling sc->iov_mta_valid = false;
2102a2ed165fSKevin Bowling sc->iov_pf_mdd_blocked = false;
2103a2ed165fSKevin Bowling sc->iov_pf_vlan_promisc = false;
21045ce6c94fSKevin Bowling igb_iov_vfta_shadow_invalidate(sc);
2105a2ed165fSKevin Bowling sc->tx_queues[0].txr.me = 0;
2106a2ed165fSKevin Bowling sc->rx_queues[0].rxr.me = 0;
2107a2ed165fSKevin Bowling atomic_readandclear_32(&sc->iov_mdd_cause);
2108a2ed165fSKevin Bowling atomic_readandclear_32(&sc->iov_pending);
2109a2ed165fSKevin Bowling atomic_readandclear_32(&sc->iov_spoof_pending);
2110ac2be06eSKevin Bowling atomic_readandclear_32(&sc->iov_blocked_pending);
2111176259efSKevin Bowling atomic_readandclear_32(&sc->iov_intr_drain_pending);
2112a2ed165fSKevin Bowling atomic_store_rel_32(&sc->iov_teardown, 0);
2113a2ed165fSKevin Bowling }
2114a2ed165fSKevin Bowling
2115a2ed165fSKevin Bowling static bool
igb_iov_mac_in_use(struct e1000_softc * sc,const u8 * mac,const struct igb_vf * skip)2116a2ed165fSKevin Bowling igb_iov_mac_in_use(struct e1000_softc *sc, const u8 *mac,
2117a2ed165fSKevin Bowling const struct igb_vf *skip)
2118a2ed165fSKevin Bowling {
2119a2ed165fSKevin Bowling int i;
2120a2ed165fSKevin Bowling
2121a2ed165fSKevin Bowling if (memcmp(sc->hw.mac.addr, mac, ETHER_ADDR_LEN) == 0)
2122a2ed165fSKevin Bowling return (true);
2123a2ed165fSKevin Bowling for (i = 0; i < sc->num_vfs; i++)
2124a2ed165fSKevin Bowling if (&sc->vfs[i] != skip &&
2125a2ed165fSKevin Bowling (sc->vfs[i].flags & IGB_VF_ACTIVE) != 0 &&
2126a2ed165fSKevin Bowling memcmp(sc->vfs[i].mac, mac, ETHER_ADDR_LEN) == 0)
2127a2ed165fSKevin Bowling return (true);
2128a2ed165fSKevin Bowling for (i = 0; i < sc->num_vf_mac_filters; i++)
2129a2ed165fSKevin Bowling if (sc->vf_mac_filters[i].active &&
2130a2ed165fSKevin Bowling memcmp(sc->vf_mac_filters[i].mac, mac,
2131a2ed165fSKevin Bowling ETHER_ADDR_LEN) == 0)
2132a2ed165fSKevin Bowling return (true);
2133a2ed165fSKevin Bowling return (false);
2134a2ed165fSKevin Bowling }
2135a2ed165fSKevin Bowling
2136a2ed165fSKevin Bowling int
igb_if_iov_vf_add(if_ctx_t ctx,u16 vfnum,const nvlist_t * config)2137a2ed165fSKevin Bowling igb_if_iov_vf_add(if_ctx_t ctx, u16 vfnum, const nvlist_t *config)
2138a2ed165fSKevin Bowling {
2139a2ed165fSKevin Bowling struct e1000_softc *sc;
2140a2ed165fSKevin Bowling struct igb_vf *vf;
2141a2ed165fSKevin Bowling struct ether_addr generated;
2142a2ed165fSKevin Bowling const void *mac;
2143a2ed165fSKevin Bowling char nameunit[IFNAMSIZ + sizeof("-vf65535")];
2144a2ed165fSKevin Bowling size_t mac_size;
2145a2ed165fSKevin Bowling uint64_t configured_vlan;
2146a2ed165fSKevin Bowling u16 vlan;
2147a2ed165fSKevin Bowling
2148a2ed165fSKevin Bowling sc = iflib_get_softc(ctx);
2149a2ed165fSKevin Bowling if (vfnum >= sc->num_vfs)
2150a2ed165fSKevin Bowling return (EINVAL);
2151a2ed165fSKevin Bowling vf = &sc->vfs[vfnum];
2152a2ed165fSKevin Bowling if (vf->flags & IGB_VF_ACTIVE)
2153a2ed165fSKevin Bowling return (EBUSY);
2154a2ed165fSKevin Bowling
2155a2ed165fSKevin Bowling configured_vlan = nvlist_get_number(config, "vlan");
2156a2ed165fSKevin Bowling if (configured_vlan > VF_VLAN_TRUNK)
2157a2ed165fSKevin Bowling return (EINVAL);
2158a2ed165fSKevin Bowling vlan = configured_vlan;
2159a2ed165fSKevin Bowling if (vlan == 0)
2160a2ed165fSKevin Bowling return (ENOTSUP);
2161a2ed165fSKevin Bowling if (vlan == VF_VLAN_TRUNK)
2162a2ed165fSKevin Bowling vlan = 0;
2163a2ed165fSKevin Bowling if (!igb_iov_vlan_present(sc, vlan, false) &&
2164a2ed165fSKevin Bowling igb_iov_vlan_unique_count(sc, false) >=
2165a2ed165fSKevin Bowling E1000_VLVF_ARRAY_SIZE)
2166a2ed165fSKevin Bowling return (ENOSPC);
2167a2ed165fSKevin Bowling
2168a2ed165fSKevin Bowling vf->pool = vfnum;
2169a2ed165fSKevin Bowling vf->rar_index = sc->hw.mac.rar_entry_count - (vfnum + 1);
2170a2ed165fSKevin Bowling vf->max_frame_size = ETHER_MAX_LEN;
2171a2ed165fSKevin Bowling vf->default_vlan = vlan;
2172e8f3b96bSKevin Bowling igb_iov_reset_vlan_rate(vf);
2173a2ed165fSKevin Bowling if (nvlist_exists_binary(config, "mac-addr")) {
2174a2ed165fSKevin Bowling mac = nvlist_get_binary(config, "mac-addr", &mac_size);
2175a2ed165fSKevin Bowling if (mac_size != ETHER_ADDR_LEN || !igb_iov_mac_valid(mac))
2176a2ed165fSKevin Bowling return (EINVAL);
2177a2ed165fSKevin Bowling if (igb_iov_mac_in_use(sc, mac, vf))
2178a2ed165fSKevin Bowling return (EADDRINUSE);
2179a2ed165fSKevin Bowling memcpy(vf->mac, mac, ETHER_ADDR_LEN);
2180a2ed165fSKevin Bowling } else {
2181a2ed165fSKevin Bowling snprintf(nameunit, sizeof(nameunit), "%s-vf%u",
2182a2ed165fSKevin Bowling device_get_nameunit(sc->dev), vfnum);
2183a2ed165fSKevin Bowling ether_gen_addr_byname(nameunit, &generated);
2184a2ed165fSKevin Bowling memcpy(vf->mac, generated.octet, ETHER_ADDR_LEN);
2185a2ed165fSKevin Bowling if (igb_iov_mac_in_use(sc, vf->mac, vf))
2186a2ed165fSKevin Bowling return (EADDRINUSE);
2187a2ed165fSKevin Bowling }
2188a2ed165fSKevin Bowling if (nvlist_get_bool(config, "allow-set-mac"))
2189a2ed165fSKevin Bowling vf->flags |= IGB_VF_CAP_MAC;
2190a2ed165fSKevin Bowling if (nvlist_get_bool(config, "mac-anti-spoof"))
2191a2ed165fSKevin Bowling vf->flags |= IGB_VF_MAC_ANTI_SPOOF;
2192a2ed165fSKevin Bowling if (nvlist_get_bool(config, "allow-promisc"))
2193a2ed165fSKevin Bowling vf->flags |= IGB_VF_ALLOW_PROMISC;
2194a2ed165fSKevin Bowling vf->flags |= IGB_VF_ACTIVE;
2195a2ed165fSKevin Bowling
2196a2ed165fSKevin Bowling igb_iov_reset_vf_state(sc, vf);
2197a2ed165fSKevin Bowling igb_iov_set_anti_spoof(sc, vf);
2198a2ed165fSKevin Bowling igb_iov_rebuild_vlan(sc);
2199a2ed165fSKevin Bowling E1000_WRITE_REG(&sc->hw, E1000_MBVFIMR, igb_iov_active_mask(sc));
2200a2ed165fSKevin Bowling return (0);
2201a2ed165fSKevin Bowling }
2202a2ed165fSKevin Bowling
2203a2ed165fSKevin Bowling #endif /* PCI_IOV */
2204