xref: /freebsd/sys/dev/ixgbe/if_sriov.c (revision df02513fd44bcede0aa8833ae5c2af826f63ce10)
1 /******************************************************************************
2 
3   Copyright (c) 2001-2017, Intel Corporation
4   All rights reserved.
5 
6   Redistribution and use in source and binary forms, with or without
7   modification, are permitted provided that the following conditions are met:
8 
9    1. Redistributions of source code must retain the above copyright notice,
10       this list of conditions and the following disclaimer.
11 
12    2. Redistributions in binary form must reproduce the above copyright
13       notice, this list of conditions and the following disclaimer in the
14       documentation and/or other materials provided with the distribution.
15 
16    3. Neither the name of the Intel Corporation nor the names of its
17       contributors may be used to endorse or promote products derived from
18       this software without specific prior written permission.
19 
20   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   POSSIBILITY OF SUCH DAMAGE.
31 
32 ******************************************************************************/
33 
34 #include "ixgbe.h"
35 #include "ixgbe_sriov.h"
36 
37 #ifdef PCI_IOV
38 
39 #include <sys/iov.h>
40 #include <sys/ktr.h>
41 
42 MALLOC_DEFINE(M_IXGBE_SRIOV, "ix_sriov", "ix SR-IOV allocations");
43 
44 #define IXGBE_VF_MBX_CLEANUP_GRACE	(2 * SBT_1S)
45 #define IXGBE_PRIMARY_ABORT_LIMIT 5
46 
47 static const struct timeval ixgbe_mdd_log_interval = { 2, 0 };
48 static const struct timeval ixgbe_dma_abort_log_interval = { 10, 0 };
49 
50 static bool
ixgbe_has_legacy_iov_recovery(const struct ixgbe_hw * hw)51 ixgbe_has_legacy_iov_recovery(const struct ixgbe_hw *hw)
52 {
53 
54 	return (hw->mac.type == ixgbe_mac_82599EB ||
55 	    hw->mac.type == ixgbe_mac_X540);
56 }
57 
58 /************************************************************************
59  * ixgbe_define_iov_schemas
60  ************************************************************************/
61 void
ixgbe_define_iov_schemas(device_t dev,int * error)62 ixgbe_define_iov_schemas(device_t dev, int *error)
63 {
64 	nvlist_t *pf_schema, *vf_schema;
65 
66 	pf_schema = pci_iov_schema_alloc_node();
67 	vf_schema = pci_iov_schema_alloc_node();
68 	pci_iov_schema_add_unicast_mac(vf_schema, "mac-addr", 0, NULL);
69 	pci_iov_schema_add_bool(vf_schema, "mac-anti-spoof",
70 	    IOV_SCHEMA_HASDEFAULT, true);
71 	pci_iov_schema_add_bool(vf_schema, "allow-set-mac",
72 	    IOV_SCHEMA_HASDEFAULT, false);
73 	pci_iov_schema_add_bool(vf_schema, "allow-promisc",
74 	    IOV_SCHEMA_HASDEFAULT, false);
75 	pci_iov_schema_add_vlan(vf_schema, "vlan", IOV_SCHEMA_HASDEFAULT,
76 	    VF_VLAN_TRUNK);
77 	*error = pci_iov_attach(dev, pf_schema, vf_schema);
78 	if (*error != 0) {
79 		device_printf(dev,
80 		    "Error %d setting up SR-IOV\n", *error);
81 	}
82 } /* ixgbe_define_iov_schemas */
83 
84 /************************************************************************
85  * ixgbe_align_all_queue_indices
86  ************************************************************************/
87 inline void
ixgbe_align_all_queue_indices(struct ixgbe_softc * sc)88 ixgbe_align_all_queue_indices(struct ixgbe_softc *sc)
89 {
90 	int i;
91 	int index;
92 
93 	for (i = 0; i < sc->num_rx_queues; i++) {
94 		index = ixgbe_vf_que_index(sc->iov_mode, sc->pool, i);
95 		sc->rx_queues[i].rxr.me = index;
96 	}
97 
98 	for (i = 0; i < sc->num_tx_queues; i++) {
99 		index = ixgbe_vf_que_index(sc->iov_mode, sc->pool, i);
100 		sc->tx_queues[i].txr.me = index;
101 	}
102 }
103 
104 /* Support functions for SR-IOV/VF management */
105 static inline void
ixgbe_send_vf_msg(struct ixgbe_hw * hw,struct ixgbe_vf * vf,u32 msg)106 ixgbe_send_vf_msg(struct ixgbe_hw *hw, struct ixgbe_vf *vf, u32 msg)
107 {
108 	if (vf->flags & IXGBE_VF_CTS)
109 		msg |= IXGBE_VT_MSGTYPE_CTS;
110 
111 	ixgbe_write_mbx(hw, &msg, 1, vf->pool);
112 }
113 
114 static inline void
ixgbe_send_vf_success(struct ixgbe_softc * sc,struct ixgbe_vf * vf,u32 msg)115 ixgbe_send_vf_success(struct ixgbe_softc *sc, struct ixgbe_vf *vf, u32 msg)
116 {
117 	msg &= IXGBE_VT_MSG_MASK;
118 	ixgbe_send_vf_msg(&sc->hw, vf, msg | IXGBE_VT_MSGTYPE_SUCCESS);
119 }
120 
121 static inline void
ixgbe_send_vf_failure(struct ixgbe_softc * sc,struct ixgbe_vf * vf,u32 msg)122 ixgbe_send_vf_failure(struct ixgbe_softc *sc, struct ixgbe_vf *vf, u32 msg)
123 {
124 	msg &= IXGBE_VT_MSG_MASK;
125 	ixgbe_send_vf_msg(&sc->hw, vf, msg | IXGBE_VT_MSGTYPE_FAILURE);
126 }
127 
128 static inline void
ixgbe_process_vf_ack(struct ixgbe_softc * sc,struct ixgbe_vf * vf)129 ixgbe_process_vf_ack(struct ixgbe_softc *sc, struct ixgbe_vf *vf)
130 {
131 	if (!(vf->flags & IXGBE_VF_CTS))
132 		ixgbe_send_vf_failure(sc, vf, 0);
133 }
134 
135 static void
ixgbe_vf_set_anti_spoof(struct ixgbe_softc * sc,struct ixgbe_vf * vf)136 ixgbe_vf_set_anti_spoof(struct ixgbe_softc *sc, struct ixgbe_vf *vf)
137 {
138 	struct ixgbe_hw *hw;
139 	uint32_t reg;
140 	bool enable;
141 
142 	hw = &sc->hw;
143 	enable = (vf->flags & IXGBE_VF_ANTI_SPOOF) != 0;
144 	if (hw->mac.ops.set_mac_anti_spoofing != NULL)
145 		hw->mac.ops.set_mac_anti_spoofing(hw, enable, vf->pool);
146 	if (hw->mac.ops.set_vlan_anti_spoofing != NULL)
147 		hw->mac.ops.set_vlan_anti_spoofing(hw, enable, vf->pool);
148 	if (hw->mac.ops.set_ethertype_anti_spoofing != NULL) {
149 		if (enable) {
150 			IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_LLDP),
151 			    IXGBE_ETQF_FILTER_EN | IXGBE_ETQF_TX_ANTISPOOF |
152 			    ETHERTYPE_LLDP);
153 			IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_FC),
154 			    IXGBE_ETQF_FILTER_EN | IXGBE_ETQF_TX_ANTISPOOF |
155 			    ETHERTYPE_FLOWCONTROL);
156 		}
157 		hw->mac.ops.set_ethertype_anti_spoofing(hw, enable,
158 		    vf->pool);
159 	}
160 
161 	reg = IXGBE_READ_REG(hw, IXGBE_VMECM(IXGBE_VF_INDEX(vf->pool)));
162 	if (enable)
163 		reg |= IXGBE_VF_BIT(vf->pool);
164 	else
165 		reg &= ~IXGBE_VF_BIT(vf->pool);
166 	IXGBE_WRITE_REG(hw, IXGBE_VMECM(IXGBE_VF_INDEX(vf->pool)), reg);
167 }
168 
169 static inline boolean_t
ixgbe_vf_mac_changed(struct ixgbe_vf * vf,const uint8_t * mac)170 ixgbe_vf_mac_changed(struct ixgbe_vf *vf, const uint8_t *mac)
171 {
172 	return (bcmp(mac, vf->ether_addr, ETHER_ADDR_LEN) != 0);
173 }
174 
175 static bool
ixgbe_rar_mac_in_use(struct ixgbe_softc * sc,const uint8_t * mac,int excluded_rar)176 ixgbe_rar_mac_in_use(struct ixgbe_softc *sc, const uint8_t *mac,
177     int excluded_rar)
178 {
179 	struct ixgbe_hw *hw;
180 	uint32_t addr_high, addr_low, rah;
181 	int i;
182 
183 	hw = &sc->hw;
184 	addr_low = (uint32_t)mac[0] | ((uint32_t)mac[1] << 8) |
185 	    ((uint32_t)mac[2] << 16) | ((uint32_t)mac[3] << 24);
186 	addr_high = (uint32_t)mac[4] | ((uint32_t)mac[5] << 8);
187 	for (i = 0; i < hw->mac.num_rar_entries; i++) {
188 		if (i == excluded_rar)
189 			continue;
190 		rah = IXGBE_READ_REG(hw, IXGBE_RAH(i));
191 		if ((rah & IXGBE_RAH_AV) != 0 &&
192 		    (rah & 0xffff) == addr_high &&
193 		    IXGBE_READ_REG(hw, IXGBE_RAL(i)) == addr_low)
194 			return (true);
195 	}
196 	return (ixgbe_validate_mac_addr(hw->mac.san_addr) == IXGBE_SUCCESS &&
197 	    bcmp(hw->mac.san_addr, mac, ETHER_ADDR_LEN) == 0);
198 }
199 
200 static inline int
ixgbe_vf_queues(int mode)201 ixgbe_vf_queues(int mode)
202 {
203 	switch (mode) {
204 	case IXGBE_64_VM:
205 		return (2);
206 	case IXGBE_32_VM:
207 		return (4);
208 	case IXGBE_NO_VM:
209 	default:
210 		return (0);
211 	}
212 }
213 
214 inline int
ixgbe_vf_que_index(int mode,int vfnum,int num)215 ixgbe_vf_que_index(int mode, int vfnum, int num)
216 {
217 	return ((vfnum * ixgbe_vf_queues(mode)) + num);
218 }
219 
220 static inline void
ixgbe_update_max_frame(struct ixgbe_softc * sc,int max_frame)221 ixgbe_update_max_frame(struct ixgbe_softc * sc, int max_frame)
222 {
223 	if (sc->max_frame_size < max_frame)
224 		sc->max_frame_size = max_frame;
225 }
226 
227 inline u32
ixgbe_get_mrqc(int iov_mode)228 ixgbe_get_mrqc(int iov_mode)
229 {
230 	u32 mrqc;
231 
232 	switch (iov_mode) {
233 	case IXGBE_64_VM:
234 		mrqc = IXGBE_MRQC_VMDQRSS64EN;
235 		break;
236 	case IXGBE_32_VM:
237 		mrqc = IXGBE_MRQC_VMDQRSS32EN;
238 		break;
239 	case IXGBE_NO_VM:
240 		mrqc = IXGBE_MRQC_RSSEN;
241 		break;
242 	default:
243 		panic("Unexpected SR-IOV mode %d", iov_mode);
244 	}
245 
246 	return mrqc;
247 }
248 
249 
250 inline u32
ixgbe_get_mtqc(int iov_mode)251 ixgbe_get_mtqc(int iov_mode)
252 {
253 	uint32_t mtqc;
254 
255 	switch (iov_mode) {
256 	case IXGBE_64_VM:
257 		mtqc = IXGBE_MTQC_64VF | IXGBE_MTQC_VT_ENA;
258 		break;
259 	case IXGBE_32_VM:
260 		mtqc = IXGBE_MTQC_32VF | IXGBE_MTQC_VT_ENA;
261 		break;
262 	case IXGBE_NO_VM:
263 		mtqc = IXGBE_MTQC_64Q_1PB;
264 		break;
265 	default:
266 		panic("Unexpected SR-IOV mode %d", iov_mode);
267 	}
268 
269 	return mtqc;
270 }
271 
272 void
ixgbe_ping_all_vfs(struct ixgbe_softc * sc)273 ixgbe_ping_all_vfs(struct ixgbe_softc *sc)
274 {
275 	struct ixgbe_vf *vf;
276 
277 	for (int i = 0; i < sc->num_vfs; i++) {
278 		vf = &sc->vfs[i];
279 		if ((vf->flags &
280 		    (IXGBE_VF_ACTIVE | IXGBE_VF_TRAFFIC_DISABLED)) ==
281 		    IXGBE_VF_ACTIVE)
282 			ixgbe_send_vf_msg(&sc->hw, vf, IXGBE_PF_CONTROL_MSG);
283 	}
284 } /* ixgbe_ping_all_vfs */
285 
286 /*
287  * Stop VF DMA before resetting the PF.  A PF reset invalidates the VF queue
288  * state, so allowing an active VF to resume with its old rings can strand
289  * descriptors in both the VF and PF.  Clearing CTS makes a cooperative VF
290  * renegotiate its state after the PF comes back; it is deliberately separate
291  * from IXGBE_VF_IO_DISABLED, which records a persistent administrative or
292  * recovery decision.
293  */
294 void
ixgbe_quiesce_vfs(struct ixgbe_softc * sc)295 ixgbe_quiesce_vfs(struct ixgbe_softc *sc)
296 {
297 	struct ixgbe_hw *hw;
298 	struct ixgbe_vf *vf;
299 	uint32_t index, mask, vfre, vfte;
300 	int i;
301 
302 	hw = &sc->hw;
303 	for (i = 0; i < sc->num_vfs; i++) {
304 		vf = &sc->vfs[i];
305 		if (!(vf->flags & IXGBE_VF_ACTIVE))
306 			continue;
307 
308 		vf->flags &= ~IXGBE_VF_CTS;
309 		index = IXGBE_VF_INDEX(vf->pool);
310 		mask = IXGBE_VF_BIT(vf->pool);
311 		vfte = IXGBE_READ_REG(hw, IXGBE_VFTE(index));
312 		vfre = IXGBE_READ_REG(hw, IXGBE_VFRE(index));
313 		IXGBE_WRITE_REG(hw, IXGBE_VFTE(index), vfte & ~mask);
314 		IXGBE_WRITE_REG(hw, IXGBE_VFRE(index), vfre & ~mask);
315 		ixgbe_send_vf_msg(hw, vf, IXGBE_PF_CONTROL_MSG);
316 	}
317 	IXGBE_WRITE_FLUSH(hw);
318 } /* ixgbe_quiesce_vfs */
319 
320 
321 static bool
ixgbe_pf_owns_vlan(struct ixgbe_softc * sc,uint16_t tag)322 ixgbe_pf_owns_vlan(struct ixgbe_softc *sc, uint16_t tag)
323 {
324 	if_t ifp;
325 
326 	ifp = iflib_get_ifp(sc->ctx);
327 	if ((if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER) == 0)
328 		return (true);
329 	return ((sc->shadow_vfta[tag >> 5] &
330 	    (1U << (tag & 0x1f))) != 0);
331 }
332 
333 static bool
ixgbe_vf_owns_vlan(const struct ixgbe_vf * vf,uint16_t tag)334 ixgbe_vf_owns_vlan(const struct ixgbe_vf *vf, uint16_t tag)
335 {
336 
337 	return ((vf->vlans[tag >> 5] & (1U << (tag & 0x1f))) != 0);
338 }
339 
340 static void
ixgbe_vf_vlan_release_pf_only(struct ixgbe_softc * sc,uint16_t tag)341 ixgbe_vf_vlan_release_pf_only(struct ixgbe_softc *sc, uint16_t tag)
342 {
343 	struct ixgbe_hw *hw;
344 	uint32_t bits[2];
345 	s32 slot;
346 
347 	hw = &sc->hw;
348 	slot = ixgbe_find_vlvf_slot(hw, tag, true);
349 	if (slot <= 0)
350 		return;
351 	bits[0] = IXGBE_READ_REG(hw, IXGBE_VLVFB(slot * 2));
352 	bits[1] = IXGBE_READ_REG(hw, IXGBE_VLVFB(slot * 2 + 1));
353 	bits[sc->pool / 32] &= ~(1U << (sc->pool % 32));
354 	if (bits[0] != 0 || bits[1] != 0)
355 		return;
356 
357 	/* The VFTA bit still admits this VLAN to the PF's default pool. */
358 	IXGBE_WRITE_REG(hw, IXGBE_VLVF(slot), 0);
359 	IXGBE_WRITE_REG(hw, IXGBE_VLVFB(slot * 2), 0);
360 	IXGBE_WRITE_REG(hw, IXGBE_VLVFB(slot * 2 + 1), 0);
361 }
362 
363 static s32
ixgbe_vf_vlan_hw_update(struct ixgbe_softc * sc,struct ixgbe_vf * vf,uint16_t tag,bool enable)364 ixgbe_vf_vlan_hw_update(struct ixgbe_softc *sc, struct ixgbe_vf *vf,
365     uint16_t tag, bool enable)
366 {
367 	struct ixgbe_hw *hw;
368 	s32 error;
369 
370 	hw = &sc->hw;
371 	if (!enable &&
372 	    ixgbe_find_vlvf_slot(hw, tag, true) < IXGBE_SUCCESS)
373 		return (IXGBE_SUCCESS);
374 	/*
375 	 * Allocate the VLVF entry with the PF first when it also owns this
376 	 * VLAN.  This guarantees that adding the VF cannot hide the VLAN from
377 	 * the PF when the shared VFTA bit becomes pool-selective.
378 	 */
379 	if (enable && ixgbe_pf_owns_vlan(sc, tag)) {
380 		error = ixgbe_set_vfta(hw, tag, sc->pool, true, false);
381 		if (error != IXGBE_SUCCESS)
382 			return (error);
383 	}
384 
385 	error = ixgbe_set_vfta(hw, tag, vf->pool, enable, false);
386 	if (error != IXGBE_SUCCESS)
387 		return (error);
388 
389 	/* Free a PF-only VLVF slot without removing the PF's VFTA bit. */
390 	if (!enable && ixgbe_pf_owns_vlan(sc, tag))
391 		ixgbe_vf_vlan_release_pf_only(sc, tag);
392 	return (IXGBE_SUCCESS);
393 }
394 
395 static void
ixgbe_vf_vlan_record(struct ixgbe_vf * vf,uint16_t tag,bool enable)396 ixgbe_vf_vlan_record(struct ixgbe_vf *vf, uint16_t tag, bool enable)
397 {
398 	u32 mask;
399 
400 	mask = 1U << (tag & 0x1f);
401 	if (enable) {
402 		vf->vlans[tag >> 5] |= mask;
403 		vf->num_vlans++;
404 	} else {
405 		vf->vlans[tag >> 5] &= ~mask;
406 		vf->num_vlans--;
407 	}
408 }
409 
410 static void
ixgbe_vf_configure_vmolr(struct ixgbe_softc * sc,struct ixgbe_vf * vf)411 ixgbe_vf_configure_vmolr(struct ixgbe_softc *sc, struct ixgbe_vf *vf)
412 {
413 	struct ixgbe_hw *hw;
414 	uint32_t vmolr, vmvir;
415 	uint8_t xcast_mode;
416 	uint16_t tag;
417 
418 	hw = &sc->hw;
419 	tag = vf->default_vlan;
420 
421 	vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf->pool));
422 
423 	vmolr &= ~(IXGBE_VMOLR_UPE | IXGBE_VMOLR_ROMPE |
424 	    IXGBE_VMOLR_ROPE | IXGBE_VMOLR_MPE | IXGBE_VMOLR_VPE |
425 	    IXGBE_VMOLR_AUPE);
426 
427 	/* Accept broadcasts. */
428 	vmolr |= IXGBE_VMOLR_BAM;
429 
430 	if (tag == 0) {
431 		/* Accept non-vlan tagged traffic. */
432 		vmolr |= IXGBE_VMOLR_AUPE;
433 
434 		/* Allow VM to tag outgoing traffic; no default tag. */
435 		vmvir = 0;
436 	} else {
437 		/* Require vlan-tagged traffic. */
438 		vmolr &= ~IXGBE_VMOLR_AUPE;
439 
440 		/* Tag all traffic with provided vlan tag. */
441 		vmvir = (tag | IXGBE_VMVIR_VLANA_DEFAULT);
442 	}
443 
444 	xcast_mode = vf->xcast_mode;
445 	if ((vf->api_ver == IXGBE_API_VER_UNKNOWN ||
446 	    vf->api_ver < IXGBE_API_VER_1_2) && vf->num_mc_hashes != 0)
447 		xcast_mode = IXGBEVF_XCAST_MODE_MULTI;
448 	switch (xcast_mode) {
449 	case IXGBEVF_XCAST_MODE_PROMISC:
450 		vmolr |= IXGBE_VMOLR_UPE;
451 		/* FALLTHROUGH */
452 	case IXGBEVF_XCAST_MODE_ALLMULTI:
453 		vmolr |= IXGBE_VMOLR_MPE;
454 		/* FALLTHROUGH */
455 	case IXGBEVF_XCAST_MODE_MULTI:
456 		vmolr |= IXGBE_VMOLR_ROMPE;
457 		break;
458 	case IXGBEVF_XCAST_MODE_NONE:
459 	default:
460 		break;
461 	}
462 	IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf->pool), vmolr);
463 	IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf->pool), vmvir);
464 } /* ixgbe_vf_configure_vmolr */
465 
466 static void
ixgbe_vf_clear_vlans(struct ixgbe_softc * sc,struct ixgbe_vf * vf,bool clear_hw)467 ixgbe_vf_clear_vlans(struct ixgbe_softc *sc, struct ixgbe_vf *vf,
468     bool clear_hw)
469 {
470 	uint32_t bits;
471 	int bit, index;
472 
473 	for (index = 0; index < IXGBE_VFTA_SIZE; index++) {
474 		bits = vf->vlans[index];
475 		while (bits != 0) {
476 			bit = ffs(bits) - 1;
477 			if (clear_hw)
478 				(void)ixgbe_vf_vlan_hw_update(sc, vf,
479 				    index * 32 + bit, false);
480 			bits &= ~(1U << bit);
481 		}
482 	}
483 	bzero(vf->vlans, sizeof(vf->vlans));
484 	vf->num_vlans = 0;
485 }
486 
487 static s32
ixgbe_vf_reset_vlan(struct ixgbe_softc * sc,struct ixgbe_vf * vf,bool clear_hw)488 ixgbe_vf_reset_vlan(struct ixgbe_softc *sc, struct ixgbe_vf *vf,
489     bool clear_hw)
490 {
491 	s32 error;
492 
493 	ixgbe_vf_clear_vlans(sc, vf, clear_hw);
494 	if (vf->default_vlan == 0) {
495 		/* VLAN 0 membership is implicit and not VF-removable. */
496 		error = ixgbe_vf_vlan_hw_update(sc, vf, 0, true);
497 	} else {
498 		error = ixgbe_vf_vlan_hw_update(sc, vf, vf->default_vlan, true);
499 		if (error == IXGBE_SUCCESS)
500 			ixgbe_vf_vlan_record(vf, vf->default_vlan, true);
501 	}
502 	ixgbe_vf_configure_vmolr(sc, vf);
503 	return (error);
504 }
505 
506 static void
ixgbe_vf_clear_mac_filters(struct ixgbe_softc * sc,struct ixgbe_vf * vf,bool clear_hw)507 ixgbe_vf_clear_mac_filters(struct ixgbe_softc *sc, struct ixgbe_vf *vf,
508     bool clear_hw)
509 {
510 	struct ixgbe_vf_mac_filter *filter;
511 	int i;
512 
513 	for (i = 0; i < sc->num_vf_mac_filters; i++) {
514 		filter = &sc->vf_mac_filters[i];
515 		if (!filter->active || filter->pool != vf->pool)
516 			continue;
517 		if (clear_hw)
518 			(void)ixgbe_clear_rar(&sc->hw, filter->rar_index);
519 		filter->active = false;
520 		bzero(filter->mac, sizeof(filter->mac));
521 	}
522 	vf->num_mac_filters = 0;
523 }
524 
525 static boolean_t
ixgbe_vf_frame_size_compatible(struct ixgbe_softc * sc,struct ixgbe_vf * vf)526 ixgbe_vf_frame_size_compatible(struct ixgbe_softc *sc, struct ixgbe_vf *vf)
527 {
528 	if_t ifp;
529 	bool pf_jumbo;
530 
531 	/*
532 	 * Frame size compatibility between PF and VF is only a problem on
533 	 * 82599-based cards.  X540 and later support any combination of jumbo
534 	 * frames on PFs and VFs.
535 	 */
536 	if (sc->hw.mac.type != ixgbe_mac_82599EB)
537 		return (true);
538 
539 	/* sc->max_frame_size includes VF requests; use the PF's actual MTU. */
540 	ifp = iflib_get_ifp(sc->ctx);
541 	pf_jumbo = if_getmtu(ifp) > ETHERMTU;
542 
543 	switch (vf->api_ver) {
544 	case IXGBE_API_VER_1_0:
545 	case IXGBE_API_VER_UNKNOWN:
546 		/*
547 		 * On legacy (1.0 and older) VF versions, we don't support
548 		 * jumbo frames on either the PF or the VF.
549 		 */
550 		if (pf_jumbo || vf->maximum_frame_size > ETHER_MAX_LEN)
551 			return (false);
552 
553 		return (true);
554 
555 		break;
556 	case IXGBE_API_VER_1_1:
557 	default:
558 		/*
559 		 * 1.1 or later VF versions always work if they aren't using
560 		 * jumbo frames.
561 		 */
562 		if (vf->maximum_frame_size <= ETHER_MAX_LEN)
563 			return (true);
564 
565 		/*
566 		 * Jumbo frames only work with VFs if the PF is also using
567 		 * jumbo frames.
568 		 */
569 		return (pf_jumbo);
570 	}
571 } /* ixgbe_vf_frame_size_compatible */
572 
573 
574 static void
ixgbe_process_vf_reset(struct ixgbe_softc * sc,struct ixgbe_vf * vf)575 ixgbe_process_vf_reset(struct ixgbe_softc *sc, struct ixgbe_vf *vf)
576 {
577 	struct ixgbe_hw *hw;
578 	bool rebuild_mta;
579 	s32 error;
580 	int i, queue_count;
581 
582 	hw = &sc->hw;
583 	vf->flags &= ~IXGBE_VF_CTS;
584 	rebuild_mta = vf->num_mc_hashes != 0;
585 	vf->xcast_mode = IXGBEVF_XCAST_MODE_NONE;
586 	vf->num_mc_hashes = 0;
587 	bzero(vf->mc_hash, sizeof(vf->mc_hash));
588 	error = ixgbe_vf_reset_vlan(sc, vf, true);
589 	if (error != IXGBE_SUCCESS)
590 		device_printf(sc->dev,
591 		    "VF %u default VLAN restore failed: %d\n",
592 		    vf->pool, error);
593 	if (rebuild_mta)
594 		ixgbe_iov_rebuild_mta(sc);
595 
596 	ixgbe_vf_clear_mac_filters(sc, vf, true);
597 	ixgbe_clear_rar(hw, vf->rar_index);
598 	ixgbe_vf_set_anti_spoof(sc, vf);
599 	ixgbe_toggle_txdctl(hw, vf->pool);
600 
601 	/* VFLR does not clear transmit head write-back state. */
602 	queue_count = ixgbe_vf_queues(sc->iov_mode);
603 	for (i = 0; i < queue_count; i++) {
604 		IXGBE_WRITE_REG(hw,
605 		    IXGBE_PVFTDWBAHn(queue_count, vf->pool, i), 0);
606 		IXGBE_WRITE_REG(hw,
607 		    IXGBE_PVFTDWBALn(queue_count, vf->pool, i), 0);
608 	}
609 
610 	/* VFLR also leaves malicious-driver queue blocks asserted. */
611 	ixgbe_restore_mdd_vf(hw, vf->pool);
612 
613 	vf->flags &= ~(IXGBE_VF_INIT_DONE | IXGBE_VF_MDD_BLOCKED |
614 	    IXGBE_VF_MDD_NOTIFY_PENDING);
615 	vf->api_ver = IXGBE_API_VER_UNKNOWN;
616 	vf->recovery_tx_pending = 0;
617 } /* ixgbe_process_vf_reset */
618 
619 
620 static void
ixgbe_vf_enable_transmit(struct ixgbe_softc * sc,struct ixgbe_vf * vf)621 ixgbe_vf_enable_transmit(struct ixgbe_softc *sc, struct ixgbe_vf *vf)
622 {
623 	struct ixgbe_hw *hw;
624 	uint32_t vf_index, vfte;
625 
626 	hw = &sc->hw;
627 
628 	vf_index = IXGBE_VF_INDEX(vf->pool);
629 	vfte = IXGBE_READ_REG(hw, IXGBE_VFTE(vf_index));
630 	if (vf->flags & IXGBE_VF_TRAFFIC_DISABLED)
631 		vfte &= ~IXGBE_VF_BIT(vf->pool);
632 	else
633 		vfte |= IXGBE_VF_BIT(vf->pool);
634 	IXGBE_WRITE_REG(hw, IXGBE_VFTE(vf_index), vfte);
635 } /* ixgbe_vf_enable_transmit */
636 
637 
638 static void
ixgbe_vf_set_rx_drop(struct ixgbe_softc * sc,struct ixgbe_vf * vf,bool enable)639 ixgbe_vf_set_rx_drop(struct ixgbe_softc *sc, struct ixgbe_vf *vf,
640     bool enable)
641 {
642 	struct ixgbe_hw *hw;
643 	u32 qde;
644 	int i, queue_count;
645 
646 	hw = &sc->hw;
647 	queue_count = ixgbe_vf_queues(sc->iov_mode);
648 	for (i = 0; i < queue_count; i++) {
649 		qde = IXGBE_QDE_WRITE |
650 		    (ixgbe_vf_que_index(sc->iov_mode, vf->pool, i) <<
651 		    IXGBE_QDE_IDX_SHIFT);
652 		if (enable) {
653 			qde |= IXGBE_QDE_ENABLE;
654 			if (vf->default_vlan != 0 &&
655 			    hw->mac.type >= ixgbe_mac_X550)
656 				qde |= IXGBE_QDE_HIDE_VLAN;
657 		}
658 		IXGBE_WRITE_REG(hw, IXGBE_QDE, qde);
659 		IXGBE_WRITE_FLUSH(hw);
660 	}
661 }
662 
663 static void
ixgbe_vf_enable_receive(struct ixgbe_softc * sc,struct ixgbe_vf * vf)664 ixgbe_vf_enable_receive(struct ixgbe_softc *sc, struct ixgbe_vf *vf)
665 {
666 	struct ixgbe_hw *hw;
667 	uint32_t vf_index, vfre;
668 
669 	hw = &sc->hw;
670 	/* Keep one VF without receive descriptors from blocking its peers. */
671 	ixgbe_vf_set_rx_drop(sc, vf, true);
672 
673 	vf_index = IXGBE_VF_INDEX(vf->pool);
674 	vfre = IXGBE_READ_REG(hw, IXGBE_VFRE(vf_index));
675 	if (!(vf->flags & IXGBE_VF_TRAFFIC_DISABLED) &&
676 	    ixgbe_vf_frame_size_compatible(sc, vf))
677 		vfre |= IXGBE_VF_BIT(vf->pool);
678 	else
679 		vfre &= ~IXGBE_VF_BIT(vf->pool);
680 	IXGBE_WRITE_REG(hw, IXGBE_VFRE(vf_index), vfre);
681 } /* ixgbe_vf_enable_receive */
682 
683 
684 static void
ixgbe_find_vf_devices(struct ixgbe_softc * sc,int iov_pos,device_t * vfdev,int num_vfs)685 ixgbe_find_vf_devices(struct ixgbe_softc *sc, int iov_pos,
686     device_t *vfdev, int num_vfs)
687 {
688 	device_t *children;
689 	u16 first_rid, rid_offset, rid_stride, vf_device_id;
690 	int child_count, i, vfnum;
691 
692 	rid_offset = pci_read_config(sc->dev,
693 	    iov_pos + PCIR_SRIOV_VF_OFF, 2);
694 	rid_stride = pci_read_config(sc->dev,
695 	    iov_pos + PCIR_SRIOV_VF_STRIDE, 2);
696 	vf_device_id = pci_read_config(sc->dev,
697 	    iov_pos + PCIR_SRIOV_VF_DID, 2);
698 	first_rid = pci_get_rid(sc->dev) + rid_offset;
699 	if (device_get_children(device_get_parent(sc->dev), &children,
700 	    &child_count) != 0)
701 		return;
702 	for (i = 0; i < child_count; i++) {
703 		if (pci_get_vendor(children[i]) != IXGBE_INTEL_VENDOR_ID ||
704 		    pci_get_device(children[i]) != vf_device_id)
705 			continue;
706 		for (vfnum = 0; vfnum < num_vfs; vfnum++) {
707 			if (pci_get_rid(children[i]) ==
708 			    first_rid + vfnum * rid_stride) {
709 				vfdev[vfnum] = children[i];
710 				break;
711 			}
712 		}
713 	}
714 	free(children, M_TEMP);
715 }
716 
717 static void
ixgbe_vf_tx_sample_reset(struct ixgbe_vf * vf)718 ixgbe_vf_tx_sample_reset(struct ixgbe_vf *vf)
719 {
720 
721 	vf->recovery_tx_pending = 0;
722 }
723 
724 /*
725  * TXDGPC is a clear-on-read, PF-wide counter.  A zero sample says only that
726  * no packet completed during the interval; it does not distinguish an idle
727  * VF from a stalled one.  Require a VF queue to have outstanding descriptors
728  * and an unchanged head across two consecutive zero-completion samples.
729  */
730 static bool
ixgbe_vf_tx_stalled(struct ixgbe_softc * sc,struct ixgbe_vf * vf)731 ixgbe_vf_tx_stalled(struct ixgbe_softc *sc, struct ixgbe_vf *vf)
732 {
733 	u32 head, pending, previous, tail;
734 	int i, queue_count;
735 	bool stalled;
736 
737 	queue_count = ixgbe_vf_queues(sc->iov_mode);
738 	previous = vf->recovery_tx_pending;
739 	pending = 0;
740 	stalled = false;
741 	for (i = 0; i < queue_count; i++) {
742 		head = IXGBE_READ_REG(&sc->hw,
743 		    IXGBE_PVFTDHn(queue_count, vf->pool, i));
744 		tail = IXGBE_READ_REG(&sc->hw,
745 		    IXGBE_PVFTDTn(queue_count, vf->pool, i));
746 		if (head == UINT32_MAX || tail == UINT32_MAX) {
747 			ixgbe_vf_tx_sample_reset(vf);
748 			return (false);
749 		}
750 		if (head != tail) {
751 			pending |= 1U << i;
752 			if ((previous & (1U << i)) != 0 &&
753 			    vf->recovery_tx_head[i] == head)
754 				stalled = true;
755 		}
756 		vf->recovery_tx_head[i] = head;
757 	}
758 	vf->recovery_tx_pending = pending;
759 	return (stalled);
760 }
761 
762 static void
ixgbe_iov_recovery_task(void * context,int pending __unused)763 ixgbe_iov_recovery_task(void *context, int pending __unused)
764 {
765 	if_ctx_t ctx;
766 	device_t vfdev[IXGBE_64_VM] = {};
767 	struct ixgbe_softc *sc;
768 	struct ixgbe_vf *vf;
769 	struct sx *ctx_lock;
770 	bool new_event, quarantined, report, scan, success;
771 	u16 command, status;
772 	u32 tx_good;
773 	u64 recovery_vfs, stalled_vfs;
774 	u_int flr_delay;
775 	int i, iov_pos, n, num_vfs = 0, recovery_vf;
776 
777 	ctx = context;
778 	if (iflib_in_detach(ctx))
779 		return;
780 	sc = iflib_get_softc(ctx);
781 	ctx_lock = iflib_ctx_lock_get(ctx);
782 
783 	/* Match PCI-IOV's topology-before-driver lock order. */
784 	bus_topo_lock();
785 	sx_xlock(ctx_lock);
786 	if (sc->iov_recovery_stop ||
787 	    !(sc->feat_en & IXGBE_FEATURE_SRIOV) ||
788 	    !ixgbe_has_legacy_iov_recovery(&sc->hw) ||
789 	    pci_find_extcap(sc->dev, PCIZ_SRIOV, &iov_pos) != 0)
790 		goto out_unlock;
791 	num_vfs = sc->num_vfs;
792 	ixgbe_find_vf_devices(sc, iov_pos, vfdev, num_vfs);
793 	for (i = 0; i < num_vfs; i++) {
794 		if (vfdev[i] != NULL)
795 			device_busy(vfdev[i]);
796 	}
797 
798 	recovery_vfs = 0;
799 	stalled_vfs = 0;
800 	recovery_vf = -1;
801 	new_event = false;
802 	quarantined = false;
803 	for (i = 0; i < num_vfs; i++) {
804 		if (sc->vfs[i].flags & IXGBE_VF_DMA_ABORT_PENDING)
805 			recovery_vfs |= 1ULL << i;
806 	}
807 	scan = false;
808 	if (sc->link_up) {
809 		tx_good = IXGBE_READ_REG(&sc->hw, IXGBE_TXDGPC);
810 		scan = tx_good == 0;
811 		if (tx_good == UINT32_MAX)
812 			scan = false;
813 	}
814 	if (scan) {
815 		for (i = 0; i < num_vfs; i++) {
816 			vf = &sc->vfs[i];
817 			if (vfdev[i] == NULL ||
818 			    !(vf->flags & IXGBE_VF_ACTIVE) ||
819 			    (vf->flags & IXGBE_VF_IO_DISABLED)) {
820 				ixgbe_vf_tx_sample_reset(vf);
821 				continue;
822 			}
823 			if (ixgbe_vf_tx_stalled(sc, vf))
824 				stalled_vfs |= 1ULL << i;
825 		}
826 	} else {
827 		for (i = 0; i < num_vfs; i++)
828 			ixgbe_vf_tx_sample_reset(&sc->vfs[i]);
829 	}
830 
831 	if (scan) {
832 		for (n = 0; n < num_vfs; n++) {
833 			i = (sc->iov_recovery_cursor + n) % num_vfs;
834 			vf = &sc->vfs[i];
835 			if ((stalled_vfs & (1ULL << i)) == 0)
836 				continue;
837 			status = pci_read_config(vfdev[i], PCIR_STATUS, 2);
838 			if (status == UINT16_MAX ||
839 			    !(status & PCIM_STATUS_RMABORT))
840 				continue;
841 
842 			/* PCI status error bits are write-one-to-clear. */
843 			pci_write_config(vfdev[i], PCIR_STATUS,
844 			    PCIM_STATUS_RMABORT, 2);
845 			ixgbe_vf_tx_sample_reset(vf);
846 			vf->flags |= IXGBE_VF_DMA_ABORT_PENDING;
847 			vf->flags &= ~IXGBE_VF_CTS;
848 			vf->primary_abort_count++;
849 			if (vf->primary_abort_count ==
850 			    IXGBE_PRIMARY_ABORT_LIMIT) {
851 				vf->flags |= IXGBE_VF_QUARANTINED;
852 				sc->iov_dma_abort_quarantines++;
853 				sc->iov_quarantined_vfs |= 1ULL << i;
854 				quarantined = true;
855 			}
856 			ixgbe_vf_enable_transmit(sc, vf);
857 			ixgbe_vf_enable_receive(sc, vf);
858 			IXGBE_WRITE_FLUSH(&sc->hw);
859 			sc->iov_dma_abort_events++;
860 			recovery_vfs |= 1ULL << i;
861 			recovery_vf = i;
862 			new_event = true;
863 			break;
864 		}
865 	}
866 	if (recovery_vf == -1) {
867 		for (n = 0; n < num_vfs; n++) {
868 			i = (sc->iov_recovery_cursor + n) % num_vfs;
869 			if ((recovery_vfs & (1ULL << i)) != 0 &&
870 			    vfdev[i] != NULL) {
871 				recovery_vf = i;
872 				break;
873 			}
874 		}
875 	}
876 	if (recovery_vf == -1)
877 		goto out_unlock;
878 	/* Bound latency to one VF FLR per pass without starving other VFs. */
879 	sc->iov_recovery_cursor = (recovery_vf + 1) % num_vfs;
880 
881 	/* Busy references keep every VF child stable while Giant is dropped. */
882 	bus_topo_unlock();
883 	i = recovery_vf;
884 	vf = &sc->vfs[i];
885 	flr_delay = MAX(pcie_get_max_completion_timeout(vfdev[i]) / 1000, 10);
886 	command = pci_read_config(vfdev[i], PCIR_COMMAND, 2);
887 	if (command != UINT16_MAX) {
888 		if (!(vf->flags & IXGBE_VF_PCI_STATE_SAVED)) {
889 			pci_save_state(vfdev[i]);
890 			vf->pci_saved_command = command;
891 			vf->flags |= IXGBE_VF_PCI_STATE_SAVED;
892 		}
893 		success = pcie_flr(vfdev[i], flr_delay, true);
894 	} else
895 		success = false;
896 	if (success && !(vf->flags & IXGBE_VF_QUARANTINED)) {
897 		/* Restore and verify the complete state for a usable VF. */
898 		pci_restore_state(vfdev[i]);
899 		command = pci_read_config(vfdev[i], PCIR_COMMAND, 2);
900 		success = command == vf->pci_saved_command;
901 	} else if (success) {
902 		/*
903 		 * Leave the function in post-FLR configuration.  Ensure that
904 		 * decode and bus mastering remain disabled, then refresh the
905 		 * PCI layer's cached Command state so a later restore cannot
906 		 * re-enable them.
907 		 */
908 		command = pci_read_config(vfdev[i], PCIR_COMMAND, 2);
909 		if (command != UINT16_MAX) {
910 			command &= ~(PCIM_CMD_PORTEN | PCIM_CMD_MEMEN |
911 			    PCIM_CMD_BUSMASTEREN);
912 			pci_write_config(vfdev[i], PCIR_COMMAND, command, 2);
913 			command = pci_read_config(vfdev[i], PCIR_COMMAND, 2);
914 		}
915 		success = command != UINT16_MAX &&
916 		    (command & (PCIM_CMD_PORTEN | PCIM_CMD_MEMEN |
917 		    PCIM_CMD_BUSMASTEREN)) == 0;
918 		if (success)
919 			pci_save_state(vfdev[i]);
920 	}
921 	if (success) {
922 		vf->flags &= ~(IXGBE_VF_DMA_ABORT_PENDING |
923 		    IXGBE_VF_PCI_STATE_SAVED);
924 		vf->pci_saved_command = 0;
925 	} else
926 		sc->iov_dma_abort_flr_failures++;
927 	if (quarantined) {
928 		device_printf(sc->dev,
929 		    "quarantined VF %u after %u invalid DMA targets%s\n",
930 		    vf->pool, IXGBE_PRIMARY_ABORT_LIMIT,
931 		    success ? "" : "; function-level reset failed");
932 	} else {
933 		report = ratecheck(&vf->last_dma_abort_log,
934 		    &ixgbe_dma_abort_log_interval) != 0;
935 		if (report && (new_event || !success)) {
936 			if (success)
937 				device_printf(sc->dev,
938 				    "invalid DMA target from VF %u; reset VF\n",
939 				    vf->pool);
940 			else
941 				device_printf(sc->dev,
942 				    "could not reset VF %u after an invalid DMA "
943 				    "target; VF remains disabled\n", vf->pool);
944 		}
945 	}
946 	sx_xunlock(ctx_lock);
947 	for (i = 0; i < num_vfs; i++) {
948 		if (vfdev[i] != NULL)
949 			device_unbusy(vfdev[i]);
950 	}
951 	return;
952 
953 out_unlock:
954 	for (i = 0; i < num_vfs; i++) {
955 		if (vfdev[i] != NULL)
956 			device_unbusy(vfdev[i]);
957 	}
958 	sx_xunlock(ctx_lock);
959 	bus_topo_unlock();
960 }
961 
962 void
ixgbe_init_iov_recovery(struct ixgbe_softc * sc)963 ixgbe_init_iov_recovery(struct ixgbe_softc *sc)
964 {
965 
966 	sc->iov_recovery_stop = false;
967 	iflib_config_task_init(sc->ctx, &sc->iov_recovery_task,
968 	    ixgbe_iov_recovery_task);
969 }
970 
971 void
ixgbe_schedule_iov_recovery(struct ixgbe_softc * sc)972 ixgbe_schedule_iov_recovery(struct ixgbe_softc *sc)
973 {
974 	bool pending;
975 	sbintime_t now;
976 	int i;
977 
978 	if (sc->iov_recovery_stop ||
979 	    !(sc->feat_en & IXGBE_FEATURE_SRIOV) ||
980 	    !ixgbe_has_legacy_iov_recovery(&sc->hw))
981 		return;
982 	pending = false;
983 	for (i = 0; i < sc->num_vfs; i++) {
984 		if (sc->vfs[i].flags & IXGBE_VF_DMA_ABORT_PENDING) {
985 			pending = true;
986 			break;
987 		}
988 	}
989 	if (!sc->link_up && !pending)
990 		return;
991 	now = sbinuptime();
992 	if (now < sc->iov_recovery_time)
993 		return;
994 	sc->iov_recovery_time = now + 2 * SBT_1S;
995 	iflib_config_task_enqueue(sc->ctx, &sc->iov_recovery_task);
996 }
997 
998 
999 static void
ixgbe_vf_reset_msg(struct ixgbe_softc * sc,struct ixgbe_vf * vf,uint32_t * msg)1000 ixgbe_vf_reset_msg(struct ixgbe_softc *sc, struct ixgbe_vf *vf, uint32_t *msg)
1001 {
1002 	struct ixgbe_hw *hw;
1003 	uint32_t ack;
1004 	uint32_t resp[IXGBE_VF_PERMADDR_MSG_LEN];
1005 
1006 	hw = &sc->hw;
1007 	if (vf->flags & IXGBE_VF_IO_DISABLED) {
1008 		ixgbe_send_vf_failure(sc, vf, msg[0]);
1009 		return;
1010 	}
1011 
1012 	ixgbe_process_vf_reset(sc, vf);
1013 
1014 	if (ixgbe_validate_mac_addr(vf->ether_addr) == 0) {
1015 		ixgbe_set_rar(&sc->hw, vf->rar_index, vf->ether_addr,
1016 		    vf->pool, true);
1017 		ack = IXGBE_VT_MSGTYPE_SUCCESS;
1018 	} else
1019 		ack = IXGBE_VT_MSGTYPE_FAILURE;
1020 
1021 	ixgbe_vf_enable_transmit(sc, vf);
1022 	ixgbe_vf_enable_receive(sc, vf);
1023 
1024 	vf->flags |= IXGBE_VF_CTS | IXGBE_VF_INIT_DONE;
1025 
1026 	resp[0] = IXGBE_VF_RESET | ack;
1027 	bcopy(vf->ether_addr, &resp[1], ETHER_ADDR_LEN);
1028 	resp[3] = hw->mac.mc_filter_type;
1029 	ixgbe_write_mbx(hw, resp, IXGBE_VF_PERMADDR_MSG_LEN, vf->pool);
1030 } /* ixgbe_vf_reset_msg */
1031 
1032 
1033 static void
ixgbe_vf_set_mac(struct ixgbe_softc * sc,struct ixgbe_vf * vf,uint32_t * msg)1034 ixgbe_vf_set_mac(struct ixgbe_softc *sc, struct ixgbe_vf *vf, uint32_t *msg)
1035 {
1036 	uint8_t *mac;
1037 
1038 	mac = (uint8_t*)&msg[1];
1039 
1040 	/* Check that the VF has permission to change the MAC address. */
1041 	if (!(vf->flags & IXGBE_VF_CAP_MAC) && ixgbe_vf_mac_changed(vf, mac)) {
1042 		ixgbe_send_vf_failure(sc, vf, msg[0]);
1043 		return;
1044 	}
1045 
1046 	if (ixgbe_validate_mac_addr(mac) != 0) {
1047 		ixgbe_send_vf_failure(sc, vf, msg[0]);
1048 		return;
1049 	}
1050 	if (ixgbe_vf_mac_changed(vf, mac) &&
1051 	    ixgbe_rar_mac_in_use(sc, mac, vf->rar_index)) {
1052 		ixgbe_send_vf_failure(sc, vf, msg[0]);
1053 		return;
1054 	}
1055 
1056 	bcopy(mac, vf->ether_addr, ETHER_ADDR_LEN);
1057 
1058 	ixgbe_set_rar(&sc->hw, vf->rar_index, vf->ether_addr, vf->pool,
1059 	    true);
1060 
1061 	ixgbe_send_vf_success(sc, vf, msg[0]);
1062 } /* ixgbe_vf_set_mac */
1063 
1064 
1065 /*
1066  * VF multicast addresses are set by using the appropriate bit in
1067  * 1 of 128 32 bit addresses (4096 possible).
1068  */
1069 static void
ixgbe_vf_set_mc_addr(struct ixgbe_softc * sc,struct ixgbe_vf * vf,u32 * msg)1070 ixgbe_vf_set_mc_addr(struct ixgbe_softc *sc, struct ixgbe_vf *vf, u32 *msg)
1071 {
1072 	u16	*list = (u16*)&msg[1];
1073 	int	entries;
1074 
1075 	entries = (msg[0] & IXGBE_VT_MSGINFO_MASK) >>
1076 	    IXGBE_VT_MSGINFO_SHIFT;
1077 	entries = min(entries, IXGBE_MAX_VF_MC);
1078 
1079 	bzero(vf->mc_hash, sizeof(vf->mc_hash));
1080 	bcopy(list, vf->mc_hash, entries * sizeof(*list));
1081 	vf->num_mc_hashes = entries;
1082 	if (entries != 0 && vf->xcast_mode == IXGBEVF_XCAST_MODE_NONE)
1083 		vf->xcast_mode = IXGBEVF_XCAST_MODE_MULTI;
1084 	else if (entries == 0 &&
1085 	    vf->xcast_mode == IXGBEVF_XCAST_MODE_MULTI)
1086 		vf->xcast_mode = IXGBEVF_XCAST_MODE_NONE;
1087 	ixgbe_iov_rebuild_mta(sc);
1088 	ixgbe_vf_configure_vmolr(sc, vf);
1089 	ixgbe_send_vf_success(sc, vf, msg[0]);
1090 } /* ixgbe_vf_set_mc_addr */
1091 
1092 
1093 static void
ixgbe_vf_set_vlan(struct ixgbe_softc * sc,struct ixgbe_vf * vf,uint32_t * msg)1094 ixgbe_vf_set_vlan(struct ixgbe_softc *sc, struct ixgbe_vf *vf, uint32_t *msg)
1095 {
1096 	bool enable, present;
1097 	s32 error;
1098 	uint16_t tag;
1099 
1100 	enable = IXGBE_VT_MSGINFO(msg[0]) != 0;
1101 	tag = msg[1] & IXGBE_VLVF_VLANID_MASK;
1102 
1103 	if (!(vf->flags & IXGBE_VF_CAP_VLAN) || vf->default_vlan != 0 ||
1104 	    (msg[1] & ~IXGBE_VLVF_VLANID_MASK) != 0) {
1105 		ixgbe_send_vf_failure(sc, vf, msg[0]);
1106 		return;
1107 	}
1108 
1109 	/* It is illegal to enable vlan tag 0. */
1110 	if (tag == 0 && enable) {
1111 		ixgbe_send_vf_failure(sc, vf, msg[0]);
1112 		return;
1113 	}
1114 
1115 	present = ixgbe_vf_owns_vlan(vf, tag);
1116 	if (enable == present) {
1117 		ixgbe_send_vf_success(sc, vf, msg[0]);
1118 		return;
1119 	}
1120 
1121 	error = ixgbe_vf_vlan_hw_update(sc, vf, tag, enable);
1122 	if (error != IXGBE_SUCCESS) {
1123 		ixgbe_send_vf_failure(sc, vf, msg[0]);
1124 		return;
1125 	}
1126 	ixgbe_vf_vlan_record(vf, tag, enable);
1127 	ixgbe_send_vf_success(sc, vf, msg[0]);
1128 } /* ixgbe_vf_set_vlan */
1129 
1130 
1131 static void
ixgbe_vf_set_lpe(struct ixgbe_softc * sc,struct ixgbe_vf * vf,uint32_t * msg)1132 ixgbe_vf_set_lpe(struct ixgbe_softc *sc, struct ixgbe_vf *vf, uint32_t *msg)
1133 {
1134 	struct ixgbe_hw *hw;
1135 	uint32_t vf_max_size, pf_max_size, mhadd;
1136 
1137 	hw = &sc->hw;
1138 	vf_max_size = msg[1];
1139 
1140 	if (vf_max_size < ETHER_CRC_LEN) {
1141 		/* We intentionally ACK invalid LPE requests. */
1142 		ixgbe_send_vf_success(sc, vf, msg[0]);
1143 		return;
1144 	}
1145 
1146 	vf_max_size -= ETHER_CRC_LEN;
1147 
1148 	if (vf_max_size > IXGBE_MAX_FRAME_SIZE) {
1149 		/* We intentionally ACK invalid LPE requests. */
1150 		ixgbe_send_vf_success(sc, vf, msg[0]);
1151 		return;
1152 	}
1153 
1154 	vf->maximum_frame_size = vf_max_size;
1155 	ixgbe_recalculate_max_frame(sc);
1156 
1157 	/*
1158 	 * We might have to disable reception to this VF if the frame size is
1159 	 * not compatible with the config on the PF.
1160 	 */
1161 	ixgbe_vf_enable_receive(sc, vf);
1162 
1163 	mhadd = IXGBE_READ_REG(hw, IXGBE_MHADD);
1164 	pf_max_size = (mhadd & IXGBE_MHADD_MFS_MASK) >> IXGBE_MHADD_MFS_SHIFT;
1165 
1166 	if (pf_max_size != sc->max_frame_size) {
1167 		mhadd &= ~IXGBE_MHADD_MFS_MASK;
1168 		mhadd |= sc->max_frame_size << IXGBE_MHADD_MFS_SHIFT;
1169 		IXGBE_WRITE_REG(hw, IXGBE_MHADD, mhadd);
1170 	}
1171 
1172 	ixgbe_send_vf_success(sc, vf, msg[0]);
1173 } /* ixgbe_vf_set_lpe */
1174 
1175 
1176 static void
ixgbe_vf_set_macvlan(struct ixgbe_softc * sc,struct ixgbe_vf * vf,uint32_t * msg)1177 ixgbe_vf_set_macvlan(struct ixgbe_softc *sc, struct ixgbe_vf *vf,
1178     uint32_t *msg)
1179 {
1180 	struct ixgbe_vf_mac_filter *filter, *free_filter;
1181 	struct ixgbe_hw *hw;
1182 	uint8_t *mac;
1183 	int i, index;
1184 
1185 	hw = &sc->hw;
1186 	index = IXGBE_VT_MSGINFO(msg[0]);
1187 	if (index == 0) {
1188 		ixgbe_vf_clear_mac_filters(sc, vf, true);
1189 		ixgbe_vf_set_anti_spoof(sc, vf);
1190 		ixgbe_send_vf_success(sc, vf, msg[0]);
1191 		return;
1192 	}
1193 	if (!(vf->flags & IXGBE_VF_CAP_MAC))
1194 		goto failure;
1195 	/* This hardware can anti-spoof only the VF's primary source MAC. */
1196 	if ((vf->flags & IXGBE_VF_ANTI_SPOOF) != 0)
1197 		goto failure;
1198 
1199 	mac = (uint8_t *)&msg[1];
1200 	if (ixgbe_validate_mac_addr(mac) != IXGBE_SUCCESS)
1201 		goto failure;
1202 	if (index == 1)
1203 		ixgbe_vf_clear_mac_filters(sc, vf, true);
1204 	if (bcmp(mac, vf->ether_addr, ETHER_ADDR_LEN) == 0) {
1205 		ixgbe_vf_set_anti_spoof(sc, vf);
1206 		ixgbe_send_vf_success(sc, vf, msg[0]);
1207 		return;
1208 	}
1209 
1210 	free_filter = NULL;
1211 	for (i = 0; i < sc->num_vf_mac_filters; i++) {
1212 		filter = &sc->vf_mac_filters[i];
1213 		if (!filter->active) {
1214 			if (free_filter == NULL)
1215 				free_filter = filter;
1216 			continue;
1217 		}
1218 		if (bcmp(filter->mac, mac, ETHER_ADDR_LEN) != 0)
1219 			continue;
1220 		if (filter->pool != vf->pool)
1221 			goto failure;
1222 		ixgbe_send_vf_success(sc, vf, msg[0]);
1223 		return;
1224 	}
1225 	if (vf->num_mac_filters >= IXGBE_MAX_VF_MAC_FILTERS ||
1226 	    free_filter == NULL)
1227 		goto failure;
1228 
1229 	/* Reject collisions with PF, VF-primary, or other reserved RARs. */
1230 	if (ixgbe_rar_mac_in_use(sc, mac, -1))
1231 		goto failure;
1232 
1233 	if (ixgbe_set_rar(hw, free_filter->rar_index, mac, vf->pool,
1234 	    true) != IXGBE_SUCCESS)
1235 		goto failure;
1236 	free_filter->active = true;
1237 	free_filter->pool = vf->pool;
1238 	bcopy(mac, free_filter->mac, ETHER_ADDR_LEN);
1239 	vf->num_mac_filters++;
1240 	/* MAC anti-spoofing accepts only the primary address on this family. */
1241 	ixgbe_vf_set_anti_spoof(sc, vf);
1242 	ixgbe_send_vf_success(sc, vf, msg[0]);
1243 	return;
1244 
1245 failure:
1246 	ixgbe_vf_set_anti_spoof(sc, vf);
1247 	ixgbe_send_vf_failure(sc, vf, msg[0]);
1248 } /* ixgbe_vf_set_macvlan */
1249 
1250 
1251 static void
ixgbe_vf_api_negotiate(struct ixgbe_softc * sc,struct ixgbe_vf * vf,uint32_t * msg)1252 ixgbe_vf_api_negotiate(struct ixgbe_softc *sc, struct ixgbe_vf *vf,
1253     uint32_t *msg)
1254 {
1255 	if (msg[1] == IXGBE_API_VER_1_6) {
1256 		if (sc->hw.mac.type != ixgbe_mac_E610)
1257 			goto failure;
1258 		vf->api_ver = msg[1];
1259 		ixgbe_send_vf_success(sc, vf, msg[0]);
1260 		return;
1261 	}
1262 
1263 	switch (msg[1]) {
1264 	case IXGBE_API_VER_1_0:
1265 	case IXGBE_API_VER_1_1:
1266 	case IXGBE_API_VER_1_2:
1267 	case IXGBE_API_VER_1_3:
1268 		vf->api_ver = msg[1];
1269 		ixgbe_send_vf_success(sc, vf, msg[0]);
1270 		break;
1271 	default:
1272 		goto failure;
1273 	}
1274 	return;
1275 
1276 failure:
1277 	vf->api_ver = IXGBE_API_VER_UNKNOWN;
1278 	ixgbe_send_vf_failure(sc, vf, msg[0]);
1279 } /* ixgbe_vf_api_negotiate */
1280 
1281 static void
ixgbe_vf_update_xcast_mode(struct ixgbe_softc * sc,struct ixgbe_vf * vf,uint32_t * msg)1282 ixgbe_vf_update_xcast_mode(struct ixgbe_softc *sc, struct ixgbe_vf *vf,
1283     uint32_t *msg)
1284 {
1285 	struct ixgbe_hw *hw;
1286 	uint32_t mode;
1287 
1288 	hw = &sc->hw;
1289 	mode = msg[1];
1290 	switch (vf->api_ver) {
1291 	case IXGBE_API_VER_1_2:
1292 		if (mode == IXGBEVF_XCAST_MODE_PROMISC)
1293 			goto failure;
1294 		break;
1295 	case IXGBE_API_VER_1_3:
1296 	case IXGBE_API_VER_1_6:
1297 		break;
1298 	default:
1299 		goto failure;
1300 	}
1301 	if (mode > IXGBEVF_XCAST_MODE_PROMISC)
1302 		goto failure;
1303 	if (mode > IXGBEVF_XCAST_MODE_MULTI &&
1304 	    !(vf->flags & IXGBE_VF_ALLOW_PROMISC))
1305 		goto failure;
1306 	if (mode == IXGBEVF_XCAST_MODE_PROMISC &&
1307 	    (hw->mac.type <= ixgbe_mac_82599EB ||
1308 	    !(IXGBE_READ_REG(hw, IXGBE_FCTRL) & IXGBE_FCTRL_UPE)))
1309 		goto failure;
1310 
1311 	vf->xcast_mode = mode;
1312 	ixgbe_vf_configure_vmolr(sc, vf);
1313 	msg[0] &= IXGBE_VT_MSG_MASK;
1314 	msg[0] |= IXGBE_VT_MSGTYPE_SUCCESS | IXGBE_VT_MSGTYPE_CTS;
1315 	msg[1] = mode;
1316 	ixgbe_write_mbx(hw, msg, 2, vf->pool);
1317 	return;
1318 
1319 failure:
1320 	ixgbe_send_vf_failure(sc, vf, msg[0]);
1321 } /* ixgbe_vf_update_xcast_mode */
1322 
1323 
1324 static void
ixgbe_vf_get_queues(struct ixgbe_softc * sc,struct ixgbe_vf * vf,uint32_t * msg)1325 ixgbe_vf_get_queues(struct ixgbe_softc *sc, struct ixgbe_vf *vf,
1326     uint32_t *msg)
1327 {
1328 	struct ixgbe_hw *hw;
1329 	uint32_t resp[IXGBE_VF_GET_QUEUES_RESP_LEN];
1330 	int num_queues;
1331 
1332 	hw = &sc->hw;
1333 
1334 	/* GET_QUEUES is not supported on pre-1.1 APIs. */
1335 	switch (vf->api_ver) {
1336 	case IXGBE_API_VER_1_0:
1337 	case IXGBE_API_VER_UNKNOWN:
1338 		ixgbe_send_vf_failure(sc, vf, msg[0]);
1339 		return;
1340 	}
1341 
1342 	resp[0] = IXGBE_VF_GET_QUEUES | IXGBE_VT_MSGTYPE_SUCCESS |
1343 	    IXGBE_VT_MSGTYPE_CTS;
1344 
1345 	num_queues = ixgbe_vf_queues(sc->iov_mode);
1346 	resp[IXGBE_VF_TX_QUEUES] = num_queues;
1347 	resp[IXGBE_VF_RX_QUEUES] = num_queues;
1348 	resp[IXGBE_VF_TRANS_VLAN] = (vf->default_vlan != 0);
1349 	resp[IXGBE_VF_DEF_QUEUE] = 0;
1350 
1351 	ixgbe_write_mbx(hw, resp, IXGBE_VF_GET_QUEUES_RESP_LEN, vf->pool);
1352 } /* ixgbe_vf_get_queues */
1353 
1354 static void
ixgbe_vf_get_link_state(struct ixgbe_softc * sc,struct ixgbe_vf * vf,uint32_t * msg)1355 ixgbe_vf_get_link_state(struct ixgbe_softc *sc, struct ixgbe_vf *vf,
1356     uint32_t *msg)
1357 {
1358 	switch (vf->api_ver) {
1359 	case IXGBE_API_VER_1_2:
1360 	case IXGBE_API_VER_1_3:
1361 	case IXGBE_API_VER_1_6:
1362 		break;
1363 	default:
1364 		ixgbe_send_vf_failure(sc, vf, msg[0]);
1365 		return;
1366 	}
1367 
1368 	msg[0] = IXGBE_VF_GET_LINK_STATE | IXGBE_VT_MSGTYPE_SUCCESS |
1369 	    IXGBE_VT_MSGTYPE_CTS;
1370 	msg[1] = 1;
1371 	ixgbe_write_mbx(&sc->hw, msg, 2, vf->pool);
1372 } /* ixgbe_vf_get_link_state */
1373 
1374 static void
ixgbe_vf_get_pf_link_state(struct ixgbe_softc * sc,struct ixgbe_vf * vf,uint32_t * msg)1375 ixgbe_vf_get_pf_link_state(struct ixgbe_softc *sc, struct ixgbe_vf *vf,
1376     uint32_t *msg)
1377 {
1378 	if (sc->hw.mac.type != ixgbe_mac_E610 ||
1379 	    vf->api_ver != IXGBE_API_VER_1_6) {
1380 		ixgbe_send_vf_failure(sc, vf, msg[0]);
1381 		return;
1382 	}
1383 
1384 	msg[0] = IXGBE_VF_GET_PF_LINK_STATE | IXGBE_VT_MSGTYPE_SUCCESS |
1385 	    IXGBE_VT_MSGTYPE_CTS;
1386 	msg[1] = sc->link_speed;
1387 	msg[2] = sc->link_up;
1388 	ixgbe_write_mbx(&sc->hw, msg, 3, vf->pool);
1389 } /* ixgbe_vf_get_pf_link_state */
1390 
1391 
1392 static bool
ixgbe_process_vf_msg(if_ctx_t ctx,struct ixgbe_vf * vf,bool reset_pending)1393 ixgbe_process_vf_msg(if_ctx_t ctx, struct ixgbe_vf *vf, bool reset_pending)
1394 {
1395 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
1396 #ifdef KTR
1397 	if_t ifp = iflib_get_ifp(ctx);
1398 #endif
1399 	struct ixgbe_hw *hw;
1400 	uint32_t msg[IXGBE_VFMAILBOX_SIZE];
1401 	bool cleanup_complete;
1402 	int error;
1403 
1404 	hw = &sc->hw;
1405 	cleanup_complete = true;
1406 
1407 	error = ixgbe_read_mbx(hw, msg, IXGBE_VFMAILBOX_SIZE, vf->pool);
1408 
1409 	if (error != 0)
1410 		return (false);
1411 	/*
1412 	 * Some devices do not clear VFMBMEM on VFLR.  Copy a pending request
1413 	 * first because the VF posts its mailbox reset request after raising
1414 	 * the reset event.
1415 	 */
1416 	if (reset_pending || msg[0] == IXGBE_VF_RESET)
1417 		cleanup_complete =
1418 		    ixgbe_clear_mbx(hw, vf->pool) == IXGBE_SUCCESS;
1419 	/* The successful read ACKed the request; dispatch it even if not clear. */
1420 
1421 	CTR3(KTR_MALLOC, "%s: received msg %x from %d", if_name(ifp),
1422 	    msg[0], vf->pool);
1423 	if (msg[0] == IXGBE_VF_RESET) {
1424 		ixgbe_vf_reset_msg(sc, vf, msg);
1425 		return (cleanup_complete);
1426 	}
1427 	/* Discard requests from the mailbox session invalidated by VFLR. */
1428 	if (reset_pending)
1429 		return (cleanup_complete);
1430 
1431 	if (!(vf->flags & IXGBE_VF_CTS)) {
1432 		ixgbe_send_vf_failure(sc, vf, msg[0]);
1433 		return (true);
1434 	}
1435 
1436 	switch (msg[0] & IXGBE_VT_MSG_MASK) {
1437 	case IXGBE_VF_SET_MAC_ADDR:
1438 		ixgbe_vf_set_mac(sc, vf, msg);
1439 		break;
1440 	case IXGBE_VF_SET_MULTICAST:
1441 		ixgbe_vf_set_mc_addr(sc, vf, msg);
1442 		break;
1443 	case IXGBE_VF_SET_VLAN:
1444 		ixgbe_vf_set_vlan(sc, vf, msg);
1445 		break;
1446 	case IXGBE_VF_SET_LPE:
1447 		ixgbe_vf_set_lpe(sc, vf, msg);
1448 		break;
1449 	case IXGBE_VF_SET_MACVLAN:
1450 		ixgbe_vf_set_macvlan(sc, vf, msg);
1451 		break;
1452 	case IXGBE_VF_API_NEGOTIATE:
1453 		ixgbe_vf_api_negotiate(sc, vf, msg);
1454 		break;
1455 	case IXGBE_VF_GET_QUEUES:
1456 		ixgbe_vf_get_queues(sc, vf, msg);
1457 		break;
1458 	case IXGBE_VF_UPDATE_XCAST_MODE:
1459 		ixgbe_vf_update_xcast_mode(sc, vf, msg);
1460 		break;
1461 	case IXGBE_VF_GET_LINK_STATE:
1462 		ixgbe_vf_get_link_state(sc, vf, msg);
1463 		break;
1464 	case IXGBE_VF_GET_PF_LINK_STATE:
1465 		ixgbe_vf_get_pf_link_state(sc, vf, msg);
1466 		break;
1467 	default:
1468 		ixgbe_send_vf_failure(sc, vf, msg[0]);
1469 	}
1470 	return (true);
1471 } /* ixgbe_process_vf_msg */
1472 
1473 static void
ixgbe_cleanup_vf_mbx(struct ixgbe_softc * sc,struct ixgbe_vf * vf)1474 ixgbe_cleanup_vf_mbx(struct ixgbe_softc *sc, struct ixgbe_vf *vf)
1475 {
1476 	struct ixgbe_hw *hw;
1477 	sbintime_t now;
1478 
1479 	hw = &sc->hw;
1480 	if (ixgbe_clear_mbx(hw, vf->pool) == IXGBE_SUCCESS) {
1481 		vf->flags &= ~IXGBE_VF_MBX_CLEANUP;
1482 		return;
1483 	}
1484 
1485 	now = getsbinuptime();
1486 	if (now < vf->mbx_cleanup_deadline)
1487 		return;
1488 
1489 	/*
1490 	 * A functioning VF posts VFREQ within the grace interval.  Ownership
1491 	 * still held after that interval is residue from the old reset epoch.
1492 	 * The force-clear helper rechecks VFREQ before asserting RVFU.
1493 	 */
1494 	if (ixgbe_force_clear_mbx_pf(hw, vf->pool) == IXGBE_SUCCESS) {
1495 		vf->flags &= ~IXGBE_VF_MBX_CLEANUP;
1496 		return;
1497 	}
1498 
1499 	/* A request raced the force-clear attempt; give it another interval. */
1500 	vf->mbx_cleanup_deadline = now + IXGBE_VF_MBX_CLEANUP_GRACE;
1501 }
1502 
1503 static void
ixgbe_notify_vf_mdd_reset(struct ixgbe_softc * sc,struct ixgbe_vf * vf)1504 ixgbe_notify_vf_mdd_reset(struct ixgbe_softc *sc, struct ixgbe_vf *vf)
1505 {
1506 	u32 msg;
1507 	int error;
1508 
1509 	msg = IXGBE_PF_CONTROL_MSG | IXGBE_VT_MSGTYPE_FAILURE;
1510 	error = ixgbe_write_mbx(&sc->hw, &msg, 1, vf->pool);
1511 	if (error == IXGBE_SUCCESS) {
1512 		vf->flags &= ~IXGBE_VF_MDD_NOTIFY_PENDING;
1513 		return;
1514 	}
1515 
1516 	/* The periodic admin pass retries after any mailbox collision. */
1517 	if (ratecheck(&vf->last_mdd_log, &ixgbe_mdd_log_interval))
1518 		device_printf(sc->dev,
1519 		    "could not notify VF %u of malicious-driver reset: %d; "
1520 		    "will retry\n", vf->pool, error);
1521 }
1522 
1523 static void
ixgbe_handle_mdd(struct ixgbe_softc * sc)1524 ixgbe_handle_mdd(struct ixgbe_softc *sc)
1525 {
1526 	struct ixgbe_hw *hw;
1527 	struct ixgbe_vf *vf;
1528 	bool pf_reset;
1529 	u32 rx_cause, tx_cause;
1530 	u32 vf_bitmap[2] = {};
1531 	int pool;
1532 
1533 	hw = &sc->hw;
1534 	ixgbe_mdd_event(hw, vf_bitmap);
1535 	if (vf_bitmap[0] != 0 || vf_bitmap[1] != 0) {
1536 		tx_cause = IXGBE_READ_REG(hw, IXGBE_LVMMC_TX);
1537 		rx_cause = IXGBE_READ_REG(hw, IXGBE_LVMMC_RX);
1538 	} else {
1539 		tx_cause = 0;
1540 		rx_cause = 0;
1541 	}
1542 	pf_reset = false;
1543 	for (pool = 0; pool < 64; pool++) {
1544 		if ((vf_bitmap[pool / 32] & (1U << (pool % 32))) == 0)
1545 			continue;
1546 
1547 		if (pool == sc->pool) {
1548 			if (sc->iov_pf_mdd_reset_pending)
1549 				continue;
1550 			sc->iov_pf_mdd_reset_pending = true;
1551 			pf_reset = true;
1552 			if (ratecheck(&sc->iov_last_mdd_log,
1553 			    &ixgbe_mdd_log_interval))
1554 				device_printf(sc->dev,
1555 				    "malicious-driver event on PF pool "
1556 				    "(last tx cause %#x, last rx cause %#x); "
1557 				    "resetting PF\n",
1558 				    tx_cause, rx_cause);
1559 			continue;
1560 		}
1561 		if (pool >= sc->num_vfs) {
1562 			ixgbe_restore_mdd_vf(hw, pool);
1563 			continue;
1564 		}
1565 		vf = &sc->vfs[pool];
1566 		if (!(vf->flags & IXGBE_VF_ACTIVE)) {
1567 			ixgbe_restore_mdd_vf(hw, pool);
1568 			continue;
1569 		}
1570 
1571 		if ((vf->flags & IXGBE_VF_MDD_BLOCKED) != 0)
1572 			continue;
1573 		if (ratecheck(&vf->last_mdd_log, &ixgbe_mdd_log_interval))
1574 			device_printf(sc->dev,
1575 			    "malicious-driver event from VF %u "
1576 			    "(last tx cause %#x, last rx cause %#x); "
1577 			    "requesting VF reset\n",
1578 			    vf->pool, tx_cause, rx_cause);
1579 
1580 		/*
1581 		 * Keep both the pool gate and the per-queue WQBR block asserted.
1582 		 * PFVFTE stops packet-data fetches but can still allow descriptor
1583 		 * fetches into the internal queue, so releasing WQBR here would
1584 		 * leave a hostile VF able to retrigger MDD before it resets.
1585 		 * ixgbe_process_vf_reset() releases WQBR in the new reset epoch.
1586 		 */
1587 		vf->flags |= IXGBE_VF_MDD_BLOCKED;
1588 		vf->flags &= ~IXGBE_VF_CTS;
1589 		ixgbe_vf_enable_transmit(sc, vf);
1590 		ixgbe_vf_enable_receive(sc, vf);
1591 		IXGBE_WRITE_FLUSH(hw);
1592 		vf->flags |= IXGBE_VF_MDD_NOTIFY_PENDING;
1593 	}
1594 
1595 	if (pf_reset) {
1596 		iflib_request_reset(sc->ctx);
1597 		iflib_admin_intr_deferred(sc->ctx);
1598 	}
1599 }
1600 
1601 /* Tasklet for handling VF -> PF mailbox messages */
1602 void
ixgbe_handle_mbx(void * context)1603 ixgbe_handle_mbx(void *context)
1604 {
1605 	if_ctx_t ctx = context;
1606 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
1607 	struct ixgbe_hw *hw;
1608 	struct ixgbe_vf *vf;
1609 	bool cleanup_pending, mbx_activity, recovering, reset_pending;
1610 	bool reset_seen;
1611 	int i;
1612 
1613 	hw = &sc->hw;
1614 	cleanup_pending = false;
1615 	ixgbe_handle_mdd(sc);
1616 
1617 	for (i = 0; i < sc->num_vfs; i++) {
1618 		vf = &sc->vfs[i];
1619 		mbx_activity = false;
1620 		reset_seen = hw->mbx.ops[i].check_for_rst(hw, i) == 0;
1621 
1622 		if (!(vf->flags & IXGBE_VF_ACTIVE)) {
1623 			if (hw->mbx.ops[i].check_for_msg(hw, i) == 0 ||
1624 			    reset_seen)
1625 				ixgbe_clear_mbx(hw, i);
1626 			(void)hw->mbx.ops[i].check_for_ack(hw, i);
1627 			continue;
1628 		}
1629 
1630 		if (reset_seen) {
1631 			/* A reset does not prove recovery succeeded. */
1632 			recovering = (vf->flags &
1633 			    IXGBE_VF_DMA_ABORT_PENDING) != 0;
1634 			vf->flags |= IXGBE_VF_MBX_CLEANUP;
1635 			vf->mbx_cleanup_deadline = getsbinuptime() +
1636 			    IXGBE_VF_MBX_CLEANUP_GRACE;
1637 			ixgbe_process_vf_reset(sc, vf);
1638 			if (!recovering) {
1639 				vf->flags &= ~(IXGBE_VF_DMA_ABORT_PENDING |
1640 				    IXGBE_VF_PCI_STATE_SAVED);
1641 				vf->pci_saved_command = 0;
1642 			}
1643 		}
1644 		reset_pending = (vf->flags & IXGBE_VF_MBX_CLEANUP) != 0;
1645 
1646 		if (hw->mbx.ops[vf->pool].check_for_msg(hw,
1647 		    vf->pool) == 0) {
1648 			mbx_activity = true;
1649 			if (ixgbe_process_vf_msg(ctx, vf, reset_pending))
1650 				vf->flags &= ~IXGBE_VF_MBX_CLEANUP;
1651 		}
1652 		if (reset_pending &&
1653 		    (vf->flags & IXGBE_VF_MBX_CLEANUP) != 0)
1654 			ixgbe_cleanup_vf_mbx(sc, vf);
1655 
1656 		if (hw->mbx.ops[vf->pool].check_for_ack(hw, vf->pool) == 0) {
1657 			mbx_activity = true;
1658 			ixgbe_process_vf_ack(sc, vf);
1659 		}
1660 
1661 		/* Do not overwrite a response produced by this mailbox pass. */
1662 		if (!mbx_activity &&
1663 		    (vf->flags & IXGBE_VF_MDD_NOTIFY_PENDING) != 0)
1664 			ixgbe_notify_vf_mdd_reset(sc, vf);
1665 
1666 		if (vf->flags & IXGBE_VF_MBX_CLEANUP)
1667 			cleanup_pending = true;
1668 	}
1669 	sc->iov_mbx_cleanup_pending = cleanup_pending;
1670 } /* ixgbe_handle_mbx */
1671 
1672 /*
1673  * VFREQ, VFACK, a bare VFLR, and WQBR can remain latched without a usable
1674  * shared mailbox interrupt across a PF stop/restart or an MDD mask interval.
1675  * Sample their aggregate registers from the periodic admin pass so work does
1676  * not depend on another edge.  Pending asynchronous MDD notifications use the
1677  * same pass to retry after the VF wins a mailbox collision.
1678  */
1679 bool
ixgbe_mbx_pending(struct ixgbe_softc * sc)1680 ixgbe_mbx_pending(struct ixgbe_softc *sc)
1681 {
1682 	struct ixgbe_hw *hw;
1683 	uint32_t events, vf_mbx[4], vf_mdd[2], vf_rst[2];
1684 	int i, index;
1685 
1686 	if (sc->num_vfs == 0)
1687 		return (false);
1688 
1689 	bzero(vf_mbx, sizeof(vf_mbx));
1690 	bzero(vf_mdd, sizeof(vf_mdd));
1691 	bzero(vf_rst, sizeof(vf_rst));
1692 	for (i = 0; i < sc->num_vfs; i++) {
1693 		if ((sc->vfs[i].flags & IXGBE_VF_MDD_NOTIFY_PENDING) != 0)
1694 			return (true);
1695 		index = IXGBE_PFMBICR_INDEX(i);
1696 		vf_mbx[index] |=
1697 		    IXGBE_PFMBICR_VFREQ_VF1 <<
1698 		    IXGBE_PFMBICR_SHIFT(i);
1699 		vf_mbx[index] |=
1700 		    IXGBE_PFMBICR_VFACK_VF1 <<
1701 		    IXGBE_PFMBICR_SHIFT(i);
1702 		index = IXGBE_PFVFLRE_INDEX(i);
1703 		vf_rst[index] |= 1U << IXGBE_PFVFLRE_SHIFT(i);
1704 	}
1705 
1706 	hw = &sc->hw;
1707 	for (index = 0; index < nitems(vf_mbx); index++) {
1708 		if (vf_mbx[index] != 0 &&
1709 		    (IXGBE_READ_REG(hw, IXGBE_PFMBICR(index)) &
1710 		    vf_mbx[index]) != 0)
1711 			return (true);
1712 	}
1713 	for (index = 0; index < nitems(vf_rst); index++) {
1714 		if (vf_rst[index] == 0)
1715 			continue;
1716 		switch (hw->mac.type) {
1717 		case ixgbe_mac_82599EB:
1718 			events = IXGBE_READ_REG(hw, IXGBE_PFVFLRE(index));
1719 			break;
1720 		case ixgbe_mac_X540:
1721 		case ixgbe_mac_X550:
1722 		case ixgbe_mac_X550EM_x:
1723 		case ixgbe_mac_X550EM_a:
1724 		case ixgbe_mac_E610:
1725 			events = IXGBE_READ_REG(hw, IXGBE_PFVFLREC(index));
1726 			break;
1727 		default:
1728 			return (false);
1729 		}
1730 		if ((events & vf_rst[index]) != 0)
1731 			return (true);
1732 	}
1733 	ixgbe_mdd_event(hw, vf_mdd);
1734 	/* A fenced VF retains WQBR until reset; do not reschedule for it. */
1735 	for (i = 0; i < sc->num_vfs; i++) {
1736 		if ((sc->vfs[i].flags & IXGBE_VF_MDD_BLOCKED) != 0)
1737 			vf_mdd[i / 32] &= ~(1U << (i % 32));
1738 	}
1739 	if (sc->iov_pf_mdd_reset_pending)
1740 		vf_mdd[sc->pool / 32] &= ~(1U << (sc->pool % 32));
1741 	if (vf_mdd[0] != 0 || vf_mdd[1] != 0)
1742 		return (true);
1743 	return (false);
1744 }
1745 
1746 int
ixgbe_iov_validate(struct ixgbe_softc * sc,u16 num_vfs)1747 ixgbe_iov_validate(struct ixgbe_softc *sc, u16 num_vfs)
1748 {
1749 	int mode, pool, queue_count;
1750 
1751 	if (!(sc->feat_cap & IXGBE_FEATURE_SRIOV))
1752 		return (ENXIO);
1753 	if (num_vfs == 0)
1754 		return (EINVAL);
1755 	if (sc->vfs != NULL || sc->vf_mac_filters != NULL ||
1756 	    (sc->feat_en & IXGBE_FEATURE_SRIOV))
1757 		return (EBUSY);
1758 	if (sc->feat_en & IXGBE_FEATURE_FDIR) {
1759 		device_printf(sc->dev,
1760 		    "Flow Director is not supported with SR-IOV\n");
1761 		return (ENOTSUP);
1762 	}
1763 	if (sc->intr_type != IFLIB_INTR_MSIX) {
1764 		device_printf(sc->dev, "SR-IOV requires MSI-X\n");
1765 		return (ENOTSUP);
1766 	}
1767 
1768 	/*
1769 	 * We've got to reserve a VM's worth of queues for the PF,
1770 	 * thus we go into "64 VF mode" if 32+ VFs are requested.
1771 	 * With 64 VFs, you can only have two queues per VF.
1772 	 * With 32 VFs, you can have up to four queues per VF.
1773 	 */
1774 	if (num_vfs >= IXGBE_32_VM)
1775 		mode = IXGBE_64_VM;
1776 	else
1777 		mode = IXGBE_32_VM;
1778 	queue_count = ixgbe_vf_queues(mode);
1779 	if (sc->num_rx_queues > queue_count ||
1780 	    sc->num_tx_queues > queue_count) {
1781 		device_printf(sc->dev,
1782 		    "SR-IOV mode supports %d PF queues, but %d RX and %d TX "
1783 		    "queues are allocated\n", queue_count, sc->num_rx_queues,
1784 		    sc->num_tx_queues);
1785 		return (ENOSPC);
1786 	}
1787 
1788 	/* Again, reserving 1 VM's worth of queues for the PF */
1789 	pool = mode - 1;
1790 	if (num_vfs > pool || num_vfs >= IXGBE_64_VM)
1791 		return (ENOSPC);
1792 	return (0);
1793 }
1794 
1795 int
ixgbe_if_iov_init(if_ctx_t ctx,u16 num_vfs,const nvlist_t * config)1796 ixgbe_if_iov_init(if_ctx_t ctx, u16 num_vfs, const nvlist_t *config)
1797 {
1798 	struct ixgbe_softc *sc;
1799 	int i, num_filters, retval;
1800 
1801 	(void)config;
1802 	sc = iflib_get_softc(ctx);
1803 	retval = ixgbe_iov_validate(sc, num_vfs);
1804 	if (retval != 0)
1805 		return (retval);
1806 
1807 	if (num_vfs >= IXGBE_32_VM)
1808 		sc->iov_mode = IXGBE_64_VM;
1809 	else
1810 		sc->iov_mode = IXGBE_32_VM;
1811 	sc->pool = sc->iov_mode - 1;
1812 
1813 	sc->vfs = malloc(sizeof(*sc->vfs) * num_vfs, M_IXGBE_SRIOV,
1814 	    M_NOWAIT | M_ZERO);
1815 
1816 	if (sc->vfs == NULL) {
1817 		retval = ENOMEM;
1818 		goto err_init_iov;
1819 	}
1820 	num_filters = sc->hw.mac.num_rar_entries - num_vfs - 1 -
1821 	    IXGBE_MAX_PF_MAC_FILTERS;
1822 	if (num_filters > 0) {
1823 		sc->vf_mac_filters = mallocarray(num_filters,
1824 		    sizeof(*sc->vf_mac_filters), M_IXGBE_SRIOV,
1825 		    M_NOWAIT | M_ZERO);
1826 		if (sc->vf_mac_filters != NULL) {
1827 			sc->num_vf_mac_filters = num_filters;
1828 			for (i = 0; i < num_filters; i++)
1829 				sc->vf_mac_filters[i].rar_index =
1830 				    IXGBE_MAX_PF_MAC_FILTERS + 1 + i;
1831 		} else
1832 			device_printf(sc->dev,
1833 			    "could not allocate VF secondary MAC filters; "
1834 			    "SET_MACVLAN will be unavailable\n");
1835 	}
1836 
1837 	sc->num_vfs = num_vfs;
1838 	sc->iov_mbx_cleanup_pending = false;
1839 	sc->iov_pf_mdd_reset_pending = false;
1840 	sc->iov_recovery_time = 0;
1841 	sc->iov_recovery_cursor = 0;
1842 	sc->iov_quarantined_vfs = 0;
1843 	ixgbe_init_mbx_params_pf(&sc->hw);
1844 
1845 	sc->feat_en |= IXGBE_FEATURE_SRIOV;
1846 
1847 	return (retval);
1848 
1849 err_init_iov:
1850 	free(sc->vf_mac_filters, M_IXGBE_SRIOV);
1851 	sc->vf_mac_filters = NULL;
1852 	sc->num_vf_mac_filters = 0;
1853 	free(sc->vfs, M_IXGBE_SRIOV);
1854 	sc->vfs = NULL;
1855 	sc->num_vfs = 0;
1856 	sc->pool = 0;
1857 	sc->iov_mode = IXGBE_NO_VM;
1858 	sc->iov_mbx_cleanup_pending = false;
1859 	sc->iov_pf_mdd_reset_pending = false;
1860 	sc->iov_quarantined_vfs = 0;
1861 
1862 	return (retval);
1863 } /* ixgbe_if_iov_init */
1864 
1865 void
ixgbe_if_iov_uninit(if_ctx_t ctx)1866 ixgbe_if_iov_uninit(if_ctx_t ctx)
1867 {
1868 	struct ixgbe_hw *hw;
1869 	struct ixgbe_softc *sc;
1870 	uint32_t pf_reg, vf_reg;
1871 	int error, i, iov_pos;
1872 	u16 iov_ctl;
1873 
1874 	sc = iflib_get_softc(ctx);
1875 	hw = &sc->hw;
1876 	ixgbe_disable_mdd(hw);
1877 
1878 	/* Enable rx/tx for the PF and disable it for all VFs. */
1879 	pf_reg = IXGBE_VF_INDEX(sc->pool);
1880 	IXGBE_WRITE_REG(hw, IXGBE_VFRE(pf_reg), IXGBE_VF_BIT(sc->pool));
1881 	IXGBE_WRITE_REG(hw, IXGBE_VFTE(pf_reg), IXGBE_VF_BIT(sc->pool));
1882 
1883 	if (pf_reg == 0)
1884 		vf_reg = 1;
1885 	else
1886 		vf_reg = 0;
1887 	IXGBE_WRITE_REG(hw, IXGBE_VFRE(vf_reg), 0);
1888 	IXGBE_WRITE_REG(hw, IXGBE_VFTE(vf_reg), 0);
1889 	IXGBE_WRITE_FLUSH(hw);
1890 
1891 	/*
1892 	 * pci_iov(4) normally clears VF Enable after this callback returns,
1893 	 * but iflib's restart transaction reuses the PF queues first.  Disable
1894 	 * the VFs here and allow outstanding transactions to drain before the
1895 	 * queue layout changes.
1896 	 */
1897 	error = pci_find_extcap(sc->dev, PCIZ_SRIOV, &iov_pos);
1898 	if (error == 0) {
1899 		iov_ctl = pci_read_config(sc->dev,
1900 		    iov_pos + PCIR_SRIOV_CTL, 2);
1901 		iov_ctl &= ~(PCIM_SRIOV_VF_EN | PCIM_SRIOV_VF_MSE);
1902 		pci_write_config(sc->dev, iov_pos + PCIR_SRIOV_CTL,
1903 		    iov_ctl, 2);
1904 		pause("ixiov", MAX(1, howmany(hz, 10)));
1905 	} else
1906 		device_printf(sc->dev,
1907 		    "could not disable PCI SR-IOV before queue reuse: %d\n",
1908 		    error);
1909 
1910 	for (i = 0; i < sc->num_vfs; i++) {
1911 		if (!(sc->vfs[i].flags & IXGBE_VF_ACTIVE))
1912 			continue;
1913 		ixgbe_vf_set_rx_drop(sc, &sc->vfs[i], false);
1914 		ixgbe_vf_clear_mac_filters(sc, &sc->vfs[i], true);
1915 		sc->vfs[i].flags &= ~IXGBE_VF_ANTI_SPOOF;
1916 		ixgbe_vf_set_anti_spoof(sc, &sc->vfs[i]);
1917 	}
1918 	if (hw->mac.ops.set_ethertype_anti_spoofing != NULL) {
1919 		IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_LLDP), 0);
1920 		IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_FC), 0);
1921 	}
1922 
1923 	IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, 0);
1924 
1925 	sc->num_vfs = 0;
1926 	ixgbe_iov_rebuild_mta(sc);
1927 	free(sc->vf_mac_filters, M_IXGBE_SRIOV);
1928 	sc->vf_mac_filters = NULL;
1929 	sc->num_vf_mac_filters = 0;
1930 	free(sc->vfs, M_IXGBE_SRIOV);
1931 	sc->vfs = NULL;
1932 	sc->feat_en &= ~IXGBE_FEATURE_SRIOV;
1933 	sc->iov_quarantined_vfs = 0;
1934 	sc->pool = 0;
1935 	sc->iov_mode = IXGBE_NO_VM;
1936 	ixgbe_align_all_queue_indices(sc);
1937 	sc->iov_vfta_valid = false;
1938 	sc->iov_vlan_promisc = false;
1939 	sc->iov_mbx_cleanup_pending = false;
1940 	sc->iov_pf_mdd_reset_pending = false;
1941 	(void)ixgbe_clear_vfta(hw);
1942 	ixgbe_setup_vlan_hw_support(ctx);
1943 } /* ixgbe_if_iov_uninit */
1944 
1945 static s32
ixgbe_init_vf(struct ixgbe_softc * sc,struct ixgbe_vf * vf)1946 ixgbe_init_vf(struct ixgbe_softc *sc, struct ixgbe_vf *vf)
1947 {
1948 	struct ixgbe_hw *hw;
1949 	uint32_t vf_index, pfmbimr;
1950 	s32 error;
1951 
1952 	hw = &sc->hw;
1953 	/* Preserve quarantine until the SR-IOV configuration is destroyed. */
1954 	vf->flags &= ~(IXGBE_VF_INIT_DONE | IXGBE_VF_DMA_ABORT_PENDING |
1955 	    IXGBE_VF_PCI_STATE_SAVED | IXGBE_VF_MDD_BLOCKED |
1956 	    IXGBE_VF_MDD_NOTIFY_PENDING);
1957 	vf->pci_saved_command = 0;
1958 	vf->recovery_tx_pending = 0;
1959 
1960 	if (!(vf->flags & IXGBE_VF_ACTIVE))
1961 		return (IXGBE_SUCCESS);
1962 	if (vf->flags & IXGBE_VF_QUARANTINED) {
1963 		vf->flags &= ~IXGBE_VF_CTS;
1964 		return (IXGBE_SUCCESS);
1965 	}
1966 
1967 	vf_index = IXGBE_VF_INDEX(vf->pool);
1968 	pfmbimr = IXGBE_READ_REG(hw, IXGBE_PFMBIMR(vf_index));
1969 	pfmbimr |= IXGBE_VF_BIT(vf->pool);
1970 	IXGBE_WRITE_REG(hw, IXGBE_PFMBIMR(vf_index), pfmbimr);
1971 
1972 	vf->xcast_mode = IXGBEVF_XCAST_MODE_NONE;
1973 	vf->api_ver = IXGBE_API_VER_UNKNOWN;
1974 	vf->num_mc_hashes = 0;
1975 	bzero(vf->mc_hash, sizeof(vf->mc_hash));
1976 	ixgbe_vf_clear_mac_filters(sc, vf, false);
1977 	error = ixgbe_vf_reset_vlan(sc, vf, false);
1978 	if (error != IXGBE_SUCCESS)
1979 		return (error);
1980 
1981 	if (ixgbe_validate_mac_addr(vf->ether_addr) == 0) {
1982 		ixgbe_set_rar(&sc->hw, vf->rar_index,
1983 		    vf->ether_addr, vf->pool, true);
1984 	}
1985 	ixgbe_vf_set_anti_spoof(sc, vf);
1986 
1987 	vf->flags |= IXGBE_VF_INIT_DONE;
1988 	return (IXGBE_SUCCESS);
1989 } /* ixgbe_init_vf */
1990 
1991 static void
ixgbe_activate_vf(struct ixgbe_softc * sc,struct ixgbe_vf * vf)1992 ixgbe_activate_vf(struct ixgbe_softc *sc, struct ixgbe_vf *vf)
1993 {
1994 	if ((vf->flags & (IXGBE_VF_ACTIVE | IXGBE_VF_INIT_DONE)) !=
1995 	    (IXGBE_VF_ACTIVE | IXGBE_VF_INIT_DONE) ||
1996 	    (vf->flags & IXGBE_VF_TRAFFIC_DISABLED))
1997 		return;
1998 
1999 	ixgbe_vf_enable_transmit(sc, vf);
2000 	ixgbe_vf_enable_receive(sc, vf);
2001 	ixgbe_send_vf_msg(&sc->hw, vf, IXGBE_PF_CONTROL_MSG);
2002 } /* ixgbe_activate_vf */
2003 
2004 void
ixgbe_initialize_iov(struct ixgbe_softc * sc)2005 ixgbe_initialize_iov(struct ixgbe_softc *sc)
2006 {
2007 	struct ixgbe_hw *hw = &sc->hw;
2008 	uint32_t mrqc, mtqc, vt_ctl, vf_reg, gcr_ext, gpie;
2009 	int i;
2010 
2011 	if (sc->iov_mode == IXGBE_NO_VM)
2012 		return;
2013 	sc->iov_pf_mdd_reset_pending = false;
2014 
2015 	/* RMW appropriate registers based on IOV mode */
2016 	/* Read... */
2017 	mrqc = IXGBE_READ_REG(hw, IXGBE_MRQC);
2018 	gcr_ext = IXGBE_READ_REG(hw, IXGBE_GCR_EXT);
2019 	gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
2020 	/* Modify... */
2021 	mrqc &= ~IXGBE_MRQC_MRQE_MASK;
2022 	mtqc = IXGBE_MTQC_VT_ENA;      /* No initial MTQC read needed */
2023 	gcr_ext |= IXGBE_GCR_EXT_MSIX_EN;
2024 	gcr_ext &= ~IXGBE_GCR_EXT_VT_MODE_MASK;
2025 	gpie &= ~IXGBE_GPIE_VTMODE_MASK;
2026 	switch (sc->iov_mode) {
2027 	case IXGBE_64_VM:
2028 		mrqc |= IXGBE_MRQC_VMDQRSS64EN;
2029 		mtqc |= IXGBE_MTQC_64VF;
2030 		gcr_ext |= IXGBE_GCR_EXT_VT_MODE_64;
2031 		gpie |= IXGBE_GPIE_VTMODE_64;
2032 		break;
2033 	case IXGBE_32_VM:
2034 		mrqc |= IXGBE_MRQC_VMDQRSS32EN;
2035 		mtqc |= IXGBE_MTQC_32VF;
2036 		gcr_ext |= IXGBE_GCR_EXT_VT_MODE_32;
2037 		gpie |= IXGBE_GPIE_VTMODE_32;
2038 		break;
2039 	default:
2040 		panic("Unexpected SR-IOV mode %d", sc->iov_mode);
2041 	}
2042 	/* Write... */
2043 	IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
2044 	IXGBE_WRITE_REG(hw, IXGBE_MTQC, mtqc);
2045 	IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, gcr_ext);
2046 	IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
2047 
2048 	/* Enable rx/tx for the PF. */
2049 	vf_reg = IXGBE_VF_INDEX(sc->pool);
2050 	IXGBE_WRITE_REG(hw, IXGBE_VFRE(vf_reg), IXGBE_VF_BIT(sc->pool));
2051 	IXGBE_WRITE_REG(hw, IXGBE_VFTE(vf_reg), IXGBE_VF_BIT(sc->pool));
2052 
2053 	/* Allow VM-to-VM communication. */
2054 	IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, IXGBE_PFDTXGSWC_VT_LBEN);
2055 
2056 	vt_ctl = IXGBE_VT_CTL_VT_ENABLE | IXGBE_VT_CTL_REPLEN;
2057 	vt_ctl |= (sc->pool << IXGBE_VT_CTL_POOL_SHIFT);
2058 	IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, vt_ctl);
2059 
2060 	for (i = 0; i < sc->num_vfs; i++) {
2061 		if (ixgbe_init_vf(sc, &sc->vfs[i]) != IXGBE_SUCCESS)
2062 			device_printf(sc->dev,
2063 			    "VF %d default VLAN restore failed\n", i);
2064 	}
2065 } /* ixgbe_initialize_iov */
2066 
2067 void
ixgbe_activate_vfs(struct ixgbe_softc * sc)2068 ixgbe_activate_vfs(struct ixgbe_softc *sc)
2069 {
2070 	int i;
2071 
2072 	for (i = 0; i < sc->num_vfs; i++)
2073 		ixgbe_activate_vf(sc, &sc->vfs[i]);
2074 } /* ixgbe_activate_vfs */
2075 
2076 
2077 /* Recompute the maximum frame setting of the PF and all active VFs. */
2078 void
ixgbe_recalculate_max_frame(struct ixgbe_softc * sc)2079 ixgbe_recalculate_max_frame(struct ixgbe_softc *sc)
2080 {
2081 	struct ixgbe_vf *vf;
2082 
2083 	sc->max_frame_size = if_getmtu(iflib_get_ifp(sc->ctx)) + IXGBE_MTU_HDR;
2084 	for (int i = 0; i < sc->num_vfs; i++) {
2085 		vf = &sc->vfs[i];
2086 		if (vf->flags & IXGBE_VF_ACTIVE)
2087 			ixgbe_update_max_frame(sc, vf->maximum_frame_size);
2088 	}
2089 } /* ixgbe_recalculate_max_frame */
2090 
2091 int
ixgbe_if_iov_vf_add(if_ctx_t ctx,u16 vfnum,const nvlist_t * config)2092 ixgbe_if_iov_vf_add(if_ctx_t ctx, u16 vfnum, const nvlist_t *config)
2093 {
2094 	struct ixgbe_softc *sc;
2095 	struct ixgbe_vf *vf;
2096 	const void *mac;
2097 	uint8_t mac_addr[ETHER_ADDR_LEN];
2098 	uint64_t configured_vlan;
2099 	uint16_t vlan;
2100 	s32 error;
2101 
2102 	sc = iflib_get_softc(ctx);
2103 
2104 	KASSERT(vfnum < sc->num_vfs, ("VF index %d is out of range %d",
2105 	    vfnum, sc->num_vfs));
2106 
2107 	vf = &sc->vfs[vfnum];
2108 	if (vf->flags & IXGBE_VF_ACTIVE)
2109 		return (EBUSY);
2110 	bzero(vf, sizeof(*vf));
2111 
2112 	configured_vlan = nvlist_get_number(config, "vlan");
2113 	if (configured_vlan > VF_VLAN_TRUNK)
2114 		return (EINVAL);
2115 	vlan = configured_vlan;
2116 	if (vlan == 0)
2117 		return (ENOTSUP);
2118 	if (vlan == VF_VLAN_TRUNK)
2119 		vlan = 0;
2120 
2121 	vf->pool = vfnum;
2122 
2123 	/* Allocate VF-primary RARs from the top, away from PF filters. */
2124 	vf->rar_index = sc->hw.mac.num_rar_entries - (vfnum + 1);
2125 	vf->default_vlan = vlan;
2126 	vf->maximum_frame_size = ETHER_MAX_LEN;
2127 	ixgbe_update_max_frame(sc, vf->maximum_frame_size);
2128 	if (nvlist_get_bool(config, "mac-anti-spoof"))
2129 		vf->flags |= IXGBE_VF_ANTI_SPOOF;
2130 	if (nvlist_get_bool(config, "allow-promisc"))
2131 		vf->flags |= IXGBE_VF_ALLOW_PROMISC;
2132 
2133 	if (nvlist_exists_binary(config, "mac-addr")) {
2134 		mac = nvlist_get_binary(config, "mac-addr", NULL);
2135 		bcopy(mac, mac_addr, sizeof(mac_addr));
2136 		if (ixgbe_validate_mac_addr(mac_addr) != IXGBE_SUCCESS)
2137 			return (EINVAL);
2138 		if (ixgbe_rar_mac_in_use(sc, mac_addr, vf->rar_index))
2139 			return (EADDRINUSE);
2140 		bcopy(mac_addr, vf->ether_addr, ETHER_ADDR_LEN);
2141 		if (nvlist_get_bool(config, "allow-set-mac"))
2142 			vf->flags |= IXGBE_VF_CAP_MAC;
2143 	} else
2144 		/*
2145 		 * If the administrator has not specified a MAC address then
2146 		 * we must allow the VF to choose one.
2147 		 */
2148 		vf->flags |= IXGBE_VF_CAP_MAC;
2149 	if (vf->default_vlan == 0)
2150 		vf->flags |= IXGBE_VF_CAP_VLAN;
2151 
2152 	vf->flags |= IXGBE_VF_ACTIVE;
2153 
2154 	error = ixgbe_init_vf(sc, vf);
2155 	if (error != IXGBE_SUCCESS) {
2156 		vf->flags &= ~IXGBE_VF_ACTIVE;
2157 		vf->default_vlan = 0;
2158 		ixgbe_vf_clear_vlans(sc, vf, true);
2159 		return (ENOSPC);
2160 	}
2161 	ixgbe_activate_vf(sc, vf);
2162 
2163 	return (0);
2164 } /* ixgbe_if_iov_vf_add */
2165 
2166 #else
2167 
2168 void
ixgbe_handle_mbx(void * context)2169 ixgbe_handle_mbx(void *context)
2170 {
2171 	UNREFERENCED_PARAMETER(context);
2172 } /* ixgbe_handle_mbx */
2173 
2174 bool
ixgbe_mbx_pending(struct ixgbe_softc * sc)2175 ixgbe_mbx_pending(struct ixgbe_softc *sc)
2176 {
2177 	UNREFERENCED_PARAMETER(sc);
2178 	return (false);
2179 }
2180 
2181 #endif
2182