xref: /freebsd/sys/dev/e1000/if_em.c (revision 037d45a0526f452d96f44059e88f735298a9063d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2001-2024, Intel Corporation
5  * Copyright (c) 2016 Nicole Graziano <nicole@nextbsd.org>
6  * Copyright (c) 2024 Kevin Bowling <kbowling@FreeBSD.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include "if_em.h"
31 #include "if_igb_iov.h"
32 #include <sys/sbuf.h>
33 #include <machine/_inttypes.h>
34 
35 #define em_mac_min e1000_82571
36 #define igb_mac_min e1000_82575
37 
38 /*********************************************************************
39  *  Driver version:
40  *********************************************************************/
41 static const char em_driver_version[] = "7.7.8-fbsd";
42 static const char igb_driver_version[] = "2.5.28-fbsd";
43 
44 /*********************************************************************
45  *  PCI Device ID Table
46  *
47  *  Used by probe to select devices to load on
48  *  Last field stores an index into e1000_strings
49  *  Last entry must be all 0s
50  *
51  *  { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
52  *********************************************************************/
53 
54 static const pci_vendor_info_t em_vendor_info_array[] =
55 {
56 	/* Intel(R) - lem-class legacy devices */
57 	PVID(0x8086, E1000_DEV_ID_82540EM,
58 	    "Intel(R) Legacy PRO/1000 MT 82540EM"),
59 	PVID(0x8086, E1000_DEV_ID_82540EM_LOM,
60 	    "Intel(R) Legacy PRO/1000 MT 82540EM (LOM)"),
61 	PVID(0x8086, E1000_DEV_ID_82540EP,
62 	    "Intel(R) Legacy PRO/1000 MT 82540EP"),
63 	PVID(0x8086, E1000_DEV_ID_82540EP_LOM,
64 	    "Intel(R) Legacy PRO/1000 MT 82540EP (LOM)"),
65 	PVID(0x8086, E1000_DEV_ID_82540EP_LP,
66 	    "Intel(R) Legacy PRO/1000 MT 82540EP (Mobile)"),
67 
68 	PVID(0x8086, E1000_DEV_ID_82541EI,
69 	    "Intel(R) Legacy PRO/1000 MT 82541EI (Copper)"),
70 	PVID(0x8086, E1000_DEV_ID_82541ER,
71 	    "Intel(R) Legacy PRO/1000 82541ER"),
72 	PVID(0x8086, E1000_DEV_ID_82541ER_LOM,
73 	    "Intel(R) Legacy PRO/1000 MT 82541ER"),
74 	PVID(0x8086, E1000_DEV_ID_82541EI_MOBILE,
75 	    "Intel(R) Legacy PRO/1000 MT 82541EI (Mobile)"),
76 	PVID(0x8086, E1000_DEV_ID_82541GI,
77 	    "Intel(R) Legacy PRO/1000 MT 82541GI"),
78 	PVID(0x8086, E1000_DEV_ID_82541GI_LF,
79 	    "Intel(R) Legacy PRO/1000 GT 82541PI"),
80 	PVID(0x8086, E1000_DEV_ID_82541GI_MOBILE,
81 	    "Intel(R) Legacy PRO/1000 MT 82541GI (Mobile)"),
82 
83 	PVID(0x8086, E1000_DEV_ID_82542,
84 	    "Intel(R) Legacy PRO/1000 82542 (Fiber)"),
85 
86 	PVID(0x8086, E1000_DEV_ID_82543GC_FIBER,
87 	    "Intel(R) Legacy PRO/1000 F 82543GC (Fiber)"),
88 	PVID(0x8086, E1000_DEV_ID_82543GC_COPPER,
89 	    "Intel(R) Legacy PRO/1000 T 82543GC (Copper)"),
90 
91 	PVID(0x8086, E1000_DEV_ID_82544EI_COPPER,
92 	    "Intel(R) Legacy PRO/1000 XT 82544EI (Copper)"),
93 	PVID(0x8086, E1000_DEV_ID_82544EI_FIBER,
94 	    "Intel(R) Legacy PRO/1000 XF 82544EI (Fiber)"),
95 	PVID(0x8086, E1000_DEV_ID_82544GC_COPPER,
96 	    "Intel(R) Legacy PRO/1000 T 82544GC (Copper)"),
97 	PVID(0x8086, E1000_DEV_ID_82544GC_LOM,
98 	    "Intel(R) Legacy PRO/1000 XT 82544GC (LOM)"),
99 
100 	PVID(0x8086, E1000_DEV_ID_82545EM_COPPER,
101 	    "Intel(R) Legacy PRO/1000 MT 82545EM (Copper)"),
102 	PVID(0x8086, E1000_DEV_ID_82545EM_FIBER,
103 	    "Intel(R) Legacy PRO/1000 MF 82545EM (Fiber)"),
104 	PVID(0x8086, E1000_DEV_ID_82545GM_COPPER,
105 	    "Intel(R) Legacy PRO/1000 MT 82545GM (Copper)"),
106 	PVID(0x8086, E1000_DEV_ID_82545GM_FIBER,
107 	    "Intel(R) Legacy PRO/1000 MF 82545GM (Fiber)"),
108 	PVID(0x8086, E1000_DEV_ID_82545GM_SERDES,
109 	    "Intel(R) Legacy PRO/1000 MB 82545GM (SERDES)"),
110 
111 	PVID(0x8086, E1000_DEV_ID_82546EB_COPPER,
112 	    "Intel(R) Legacy PRO/1000 MT 82546EB (Copper)"),
113 	PVID(0x8086, E1000_DEV_ID_82546EB_FIBER,
114 	    "Intel(R) Legacy PRO/1000 MF 82546EB (Fiber)"),
115 	PVID(0x8086, E1000_DEV_ID_82546EB_QUAD_COPPER,
116 	    "Intel(R) Legacy PRO/1000 MT 82546EB (Quad Copper"),
117 	PVID(0x8086, E1000_DEV_ID_82546GB_COPPER,
118 	    "Intel(R) Legacy PRO/1000 MT 82546GB (Copper)"),
119 	PVID(0x8086, E1000_DEV_ID_82546GB_FIBER,
120 	    "Intel(R) Legacy PRO/1000 MF 82546GB (Fiber)"),
121 	PVID(0x8086, E1000_DEV_ID_82546GB_SERDES,
122 	    "Intel(R) Legacy PRO/1000 MB 82546GB (SERDES)"),
123 	PVID(0x8086, E1000_DEV_ID_82546GB_PCIE,
124 	    "Intel(R) Legacy PRO/1000 P 82546GB (PCIe)"),
125 	PVID(0x8086, E1000_DEV_ID_82546GB_QUAD_COPPER,
126 	    "Intel(R) Legacy PRO/1000 GT 82546GB (Quad Copper)"),
127 	PVID(0x8086, E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3,
128 	    "Intel(R) Legacy PRO/1000 GT 82546GB (Quad Copper)"),
129 
130 	PVID(0x8086, E1000_DEV_ID_82547EI,
131 	    "Intel(R) Legacy PRO/1000 CT 82547EI"),
132 	PVID(0x8086, E1000_DEV_ID_82547EI_MOBILE,
133 	    "Intel(R) Legacy PRO/1000 CT 82547EI (Mobile)"),
134 	PVID(0x8086, E1000_DEV_ID_82547GI,
135 	    "Intel(R) Legacy PRO/1000 CT 82547GI"),
136 
137 	/* Intel(R) - em-class devices */
138 	PVID(0x8086, E1000_DEV_ID_82571EB_COPPER,
139 	    "Intel(R) PRO/1000 PT 82571EB/82571GB (Copper)"),
140 	PVID(0x8086, E1000_DEV_ID_82571EB_FIBER,
141 	    "Intel(R) PRO/1000 PF 82571EB/82571GB (Fiber)"),
142 	PVID(0x8086, E1000_DEV_ID_82571EB_SERDES,
143 	    "Intel(R) PRO/1000 PB 82571EB (SERDES)"),
144 	PVID(0x8086, E1000_DEV_ID_82571EB_SERDES_DUAL,
145 	    "Intel(R) PRO/1000 82571EB (Dual Mezzanine)"),
146 	PVID(0x8086, E1000_DEV_ID_82571EB_SERDES_QUAD,
147 	    "Intel(R) PRO/1000 82571EB (Quad Mezzanine)"),
148 	PVID(0x8086, E1000_DEV_ID_82571EB_QUAD_COPPER,
149 	    "Intel(R) PRO/1000 PT 82571EB/82571GB (Quad Copper)"),
150 	PVID(0x8086, E1000_DEV_ID_82571EB_QUAD_COPPER_LP,
151 	    "Intel(R) PRO/1000 PT 82571EB/82571GB (Quad Copper)"),
152 	PVID(0x8086, E1000_DEV_ID_82571EB_QUAD_FIBER,
153 	    "Intel(R) PRO/1000 PF 82571EB (Quad Fiber)"),
154 	PVID(0x8086, E1000_DEV_ID_82571PT_QUAD_COPPER,
155 	    "Intel(R) PRO/1000 PT 82571PT (Quad Copper)"),
156 	PVID(0x8086, E1000_DEV_ID_82572EI,
157 	    "Intel(R) PRO/1000 PT 82572EI (Copper)"),
158 	PVID(0x8086, E1000_DEV_ID_82572EI_COPPER,
159 	    "Intel(R) PRO/1000 PT 82572EI (Copper)"),
160 	PVID(0x8086, E1000_DEV_ID_82572EI_FIBER,
161 	    "Intel(R) PRO/1000 PF 82572EI (Fiber)"),
162 	PVID(0x8086, E1000_DEV_ID_82572EI_SERDES,
163 	    "Intel(R) PRO/1000 82572EI (SERDES)"),
164 	PVID(0x8086, E1000_DEV_ID_82573E,
165 	    "Intel(R) PRO/1000 82573E (Copper)"),
166 	PVID(0x8086, E1000_DEV_ID_82573E_IAMT,
167 	    "Intel(R) PRO/1000 82573E AMT (Copper)"),
168 	PVID(0x8086, E1000_DEV_ID_82573L, "Intel(R) PRO/1000 82573L"),
169 	PVID(0x8086, E1000_DEV_ID_82583V, "Intel(R) 82583V"),
170 	PVID(0x8086, E1000_DEV_ID_80003ES2LAN_COPPER_SPT,
171 	    "Intel(R) 80003ES2LAN (Copper)"),
172 	PVID(0x8086, E1000_DEV_ID_80003ES2LAN_SERDES_SPT,
173 	    "Intel(R) 80003ES2LAN (SERDES)"),
174 	PVID(0x8086, E1000_DEV_ID_80003ES2LAN_COPPER_DPT,
175 	    "Intel(R) 80003ES2LAN (Dual Copper)"),
176 	PVID(0x8086, E1000_DEV_ID_80003ES2LAN_SERDES_DPT,
177 	    "Intel(R) 80003ES2LAN (Dual SERDES)"),
178 	PVID(0x8086, E1000_DEV_ID_ICH8_IGP_M_AMT,
179 	    "Intel(R) 82566MM ICH8 AMT (Mobile)"),
180 	PVID(0x8086, E1000_DEV_ID_ICH8_IGP_AMT, "Intel(R) 82566DM ICH8 AMT"),
181 	PVID(0x8086, E1000_DEV_ID_ICH8_IGP_C, "Intel(R) 82566DC ICH8"),
182 	PVID(0x8086, E1000_DEV_ID_ICH8_IFE, "Intel(R) 82562V ICH8"),
183 	PVID(0x8086, E1000_DEV_ID_ICH8_IFE_GT, "Intel(R) 82562GT ICH8"),
184 	PVID(0x8086, E1000_DEV_ID_ICH8_IFE_G, "Intel(R) 82562G ICH8"),
185 	PVID(0x8086, E1000_DEV_ID_ICH8_IGP_M, "Intel(R) 82566MC ICH8"),
186 	PVID(0x8086, E1000_DEV_ID_ICH8_82567V_3, "Intel(R) 82567V-3 ICH8"),
187 	PVID(0x8086, E1000_DEV_ID_ICH9_IGP_M_AMT,
188 	    "Intel(R) 82567LM ICH9 AMT"),
189 	PVID(0x8086, E1000_DEV_ID_ICH9_IGP_AMT,
190 	    "Intel(R) 82566DM-2 ICH9 AMT"),
191 	PVID(0x8086, E1000_DEV_ID_ICH9_IGP_C, "Intel(R) 82566DC-2 ICH9"),
192 	PVID(0x8086, E1000_DEV_ID_ICH9_IGP_M, "Intel(R) 82567LF ICH9"),
193 	PVID(0x8086, E1000_DEV_ID_ICH9_IGP_M_V, "Intel(R) 82567V ICH9"),
194 	PVID(0x8086, E1000_DEV_ID_ICH9_IFE, "Intel(R) 82562V-2 ICH9"),
195 	PVID(0x8086, E1000_DEV_ID_ICH9_IFE_GT, "Intel(R) 82562GT-2 ICH9"),
196 	PVID(0x8086, E1000_DEV_ID_ICH9_IFE_G, "Intel(R) 82562G-2 ICH9"),
197 	PVID(0x8086, E1000_DEV_ID_ICH9_BM, "Intel(R) 82567LM-4 ICH9"),
198 	PVID(0x8086, E1000_DEV_ID_82574L, "Intel(R) Gigabit CT 82574L"),
199 	PVID(0x8086, E1000_DEV_ID_82574LA, "Intel(R) 82574L-Apple"),
200 	PVID(0x8086, E1000_DEV_ID_ICH10_R_BM_LM, "Intel(R) 82567LM-2 ICH10"),
201 	PVID(0x8086, E1000_DEV_ID_ICH10_R_BM_LF, "Intel(R) 82567LF-2 ICH10"),
202 	PVID(0x8086, E1000_DEV_ID_ICH10_R_BM_V, "Intel(R) 82567V-2 ICH10"),
203 	PVID(0x8086, E1000_DEV_ID_ICH10_D_BM_LM, "Intel(R) 82567LM-3 ICH10"),
204 	PVID(0x8086, E1000_DEV_ID_ICH10_D_BM_LF, "Intel(R) 82567LF-3 ICH10"),
205 	PVID(0x8086, E1000_DEV_ID_ICH10_D_BM_V, "Intel(R) 82567V-4 ICH10"),
206 	PVID(0x8086, E1000_DEV_ID_PCH_M_HV_LM, "Intel(R) 82577LM"),
207 	PVID(0x8086, E1000_DEV_ID_PCH_M_HV_LC, "Intel(R) 82577LC"),
208 	PVID(0x8086, E1000_DEV_ID_PCH_D_HV_DM, "Intel(R) 82578DM"),
209 	PVID(0x8086, E1000_DEV_ID_PCH_D_HV_DC, "Intel(R) 82578DC"),
210 	PVID(0x8086, E1000_DEV_ID_PCH2_LV_LM, "Intel(R) 82579LM"),
211 	PVID(0x8086, E1000_DEV_ID_PCH2_LV_V, "Intel(R) 82579V"),
212 	PVID(0x8086, E1000_DEV_ID_PCH_LPT_I217_LM, "Intel(R) I217-LM LPT"),
213 	PVID(0x8086, E1000_DEV_ID_PCH_LPT_I217_V, "Intel(R) I217-V LPT"),
214 	PVID(0x8086, E1000_DEV_ID_PCH_LPTLP_I218_LM,
215 	    "Intel(R) I218-LM LPTLP"),
216 	PVID(0x8086, E1000_DEV_ID_PCH_LPTLP_I218_V, "Intel(R) I218-V LPTLP"),
217 	PVID(0x8086, E1000_DEV_ID_PCH_I218_LM2, "Intel(R) I218-LM (2)"),
218 	PVID(0x8086, E1000_DEV_ID_PCH_I218_V2, "Intel(R) I218-V (2)"),
219 	PVID(0x8086, E1000_DEV_ID_PCH_I218_LM3, "Intel(R) I218-LM (3)"),
220 	PVID(0x8086, E1000_DEV_ID_PCH_I218_V3, "Intel(R) I218-V (3)"),
221 	PVID(0x8086, E1000_DEV_ID_PCH_SPT_I219_LM, "Intel(R) I219-LM SPT"),
222 	PVID(0x8086, E1000_DEV_ID_PCH_SPT_I219_V, "Intel(R) I219-V SPT"),
223 	PVID(0x8086, E1000_DEV_ID_PCH_SPT_I219_LM2,
224 	    "Intel(R) I219-LM SPT-H(2)"),
225 	PVID(0x8086, E1000_DEV_ID_PCH_SPT_I219_V2,
226 	    "Intel(R) I219-V SPT-H(2)"),
227 	PVID(0x8086, E1000_DEV_ID_PCH_LBG_I219_LM3,
228 	    "Intel(R) I219-LM LBG(3)"),
229 	PVID(0x8086, E1000_DEV_ID_PCH_SPT_I219_LM4,
230 	    "Intel(R) I219-LM SPT(4)"),
231 	PVID(0x8086, E1000_DEV_ID_PCH_SPT_I219_V4, "Intel(R) I219-V SPT(4)"),
232 	PVID(0x8086, E1000_DEV_ID_PCH_SPT_I219_LM5,
233 	    "Intel(R) I219-LM SPT(5)"),
234 	PVID(0x8086, E1000_DEV_ID_PCH_SPT_I219_V5, "Intel(R) I219-V SPT(5)"),
235 	PVID(0x8086, E1000_DEV_ID_PCH_CNP_I219_LM6,
236 	    "Intel(R) I219-LM CNP(6)"),
237 	PVID(0x8086, E1000_DEV_ID_PCH_CNP_I219_V6, "Intel(R) I219-V CNP(6)"),
238 	PVID(0x8086, E1000_DEV_ID_PCH_CNP_I219_LM7,
239 	    "Intel(R) I219-LM CNP(7)"),
240 	PVID(0x8086, E1000_DEV_ID_PCH_CNP_I219_V7, "Intel(R) I219-V CNP(7)"),
241 	PVID(0x8086, E1000_DEV_ID_PCH_ICP_I219_LM8,
242 	    "Intel(R) I219-LM ICP(8)"),
243 	PVID(0x8086, E1000_DEV_ID_PCH_ICP_I219_V8, "Intel(R) I219-V ICP(8)"),
244 	PVID(0x8086, E1000_DEV_ID_PCH_ICP_I219_LM9,
245 	    "Intel(R) I219-LM ICP(9)"),
246 	PVID(0x8086, E1000_DEV_ID_PCH_ICP_I219_V9, "Intel(R) I219-V ICP(9)"),
247 	PVID(0x8086, E1000_DEV_ID_PCH_CMP_I219_LM10,
248 	    "Intel(R) I219-LM CMP(10)"),
249 	PVID(0x8086, E1000_DEV_ID_PCH_CMP_I219_V10,
250 	    "Intel(R) I219-V CMP(10)"),
251 	PVID(0x8086, E1000_DEV_ID_PCH_CMP_I219_LM11,
252 	    "Intel(R) I219-LM CMP(11)"),
253 	PVID(0x8086, E1000_DEV_ID_PCH_CMP_I219_V11,
254 	    "Intel(R) I219-V CMP(11)"),
255 	PVID(0x8086, E1000_DEV_ID_PCH_CMP_I219_LM12,
256 	    "Intel(R) I219-LM CMP(12)"),
257 	PVID(0x8086, E1000_DEV_ID_PCH_CMP_I219_V12,
258 	    "Intel(R) I219-V CMP(12)"),
259 	PVID(0x8086, E1000_DEV_ID_PCH_TGP_I219_LM13,
260 	    "Intel(R) I219-LM TGP(13)"),
261 	PVID(0x8086, E1000_DEV_ID_PCH_TGP_I219_V13,
262 	    "Intel(R) I219-V TGP(13)"),
263 	PVID(0x8086, E1000_DEV_ID_PCH_TGP_I219_LM14,
264 	    "Intel(R) I219-LM TGP(14)"),
265 	PVID(0x8086, E1000_DEV_ID_PCH_TGP_I219_V14,
266 	    "Intel(R) I219-V GTP(14)"),
267 	PVID(0x8086, E1000_DEV_ID_PCH_TGP_I219_LM15,
268 	    "Intel(R) I219-LM TGP(15)"),
269 	PVID(0x8086, E1000_DEV_ID_PCH_TGP_I219_V15,
270 	    "Intel(R) I219-V TGP(15)"),
271 	PVID(0x8086, E1000_DEV_ID_PCH_ADL_I219_LM16,
272 	    "Intel(R) I219-LM ADL(16)"),
273 	PVID(0x8086, E1000_DEV_ID_PCH_ADL_I219_V16,
274 	    "Intel(R) I219-V ADL(16)"),
275 	PVID(0x8086, E1000_DEV_ID_PCH_ADL_I219_LM17,
276 	    "Intel(R) I219-LM ADL(17)"),
277 	PVID(0x8086, E1000_DEV_ID_PCH_ADL_I219_V17,
278 	    "Intel(R) I219-V ADL(17)"),
279 	PVID(0x8086, E1000_DEV_ID_PCH_MTP_I219_LM18,
280 	    "Intel(R) I219-LM MTP(18)"),
281 	PVID(0x8086, E1000_DEV_ID_PCH_MTP_I219_V18,
282 	    "Intel(R) I219-V MTP(18)"),
283 	PVID(0x8086, E1000_DEV_ID_PCH_ADL_I219_LM19,
284 	    "Intel(R) I219-LM ADL(19)"),
285 	PVID(0x8086, E1000_DEV_ID_PCH_ADL_I219_V19,
286 	    "Intel(R) I219-V ADL(19)"),
287 	PVID(0x8086, E1000_DEV_ID_PCH_LNL_I219_LM20,
288 	    "Intel(R) I219-LM LNL(20)"),
289 	PVID(0x8086, E1000_DEV_ID_PCH_LNL_I219_V20,
290 	    "Intel(R) I219-V LNL(20)"),
291 	PVID(0x8086, E1000_DEV_ID_PCH_LNL_I219_LM21,
292 	    "Intel(R) I219-LM LNL(21)"),
293 	PVID(0x8086, E1000_DEV_ID_PCH_LNL_I219_V21,
294 	    "Intel(R) I219-V LNL(21)"),
295 	PVID(0x8086, E1000_DEV_ID_PCH_RPL_I219_LM22,
296 	    "Intel(R) I219-LM RPL(22)"),
297 	PVID(0x8086, E1000_DEV_ID_PCH_RPL_I219_V22,
298 	    "Intel(R) I219-V RPL(22)"),
299 	PVID(0x8086, E1000_DEV_ID_PCH_RPL_I219_LM23,
300 	    "Intel(R) I219-LM RPL(23)"),
301 	PVID(0x8086, E1000_DEV_ID_PCH_RPL_I219_V23,
302 	    "Intel(R) I219-V RPL(23)"),
303 	PVID(0x8086, E1000_DEV_ID_PCH_ARL_I219_LM24,
304 	    "Intel(R) I219-LM ARL(24)"),
305 	PVID(0x8086, E1000_DEV_ID_PCH_ARL_I219_V24,
306 	    "Intel(R) I219-V ARL(24)"),
307 	PVID(0x8086, E1000_DEV_ID_PCH_PTP_I219_LM25,
308 	    "Intel(R) I219-LM PTP(25)"),
309 	PVID(0x8086, E1000_DEV_ID_PCH_PTP_I219_V25,
310 	    "Intel(R) I219-V PTP(25)"),
311 	PVID(0x8086, E1000_DEV_ID_PCH_PTP_I219_LM26,
312 	    "Intel(R) I219-LM PTP(26)"),
313 	PVID(0x8086, E1000_DEV_ID_PCH_PTP_I219_V26,
314 	    "Intel(R) I219-V PTP(26)"),
315 	PVID(0x8086, E1000_DEV_ID_PCH_PTP_I219_LM27,
316 	    "Intel(R) I219-LM PTP(27)"),
317 	PVID(0x8086, E1000_DEV_ID_PCH_PTP_I219_V27,
318 	    "Intel(R) I219-V PTP(27)"),
319 	PVID(0x8086, E1000_DEV_ID_PCH_NVL_I219_LM29,
320 	    "Intel(R) I219-LM NVL(29)"),
321 	PVID(0x8086, E1000_DEV_ID_PCH_NVL_I219_V29,
322 	    "Intel(R) I219-V NVL(29)"),
323 	/* required last entry */
324 	PVID_END
325 };
326 
327 static const pci_vendor_info_t igb_vendor_info_array[] =
328 {
329 	/* Intel(R) - igb-class devices */
330 	PVID(0x8086, E1000_DEV_ID_82575EB_COPPER,
331 	    "Intel(R) PRO/1000 82575EB (Copper)"),
332 	PVID(0x8086, E1000_DEV_ID_82575EB_FIBER_SERDES,
333 	    "Intel(R) PRO/1000 82575EB (SERDES)"),
334 	PVID(0x8086, E1000_DEV_ID_82575GB_QUAD_COPPER,
335 	    "Intel(R) PRO/1000 VT 82575GB (Quad Copper)"),
336 	PVID(0x8086, E1000_DEV_ID_82576, "Intel(R) PRO/1000 82576"),
337 	PVID(0x8086, E1000_DEV_ID_82576_NS, "Intel(R) PRO/1000 82576NS"),
338 	PVID(0x8086, E1000_DEV_ID_82576_NS_SERDES,
339 	    "Intel(R) PRO/1000 82576NS (SERDES)"),
340 	PVID(0x8086, E1000_DEV_ID_82576_FIBER,
341 	    "Intel(R) PRO/1000 EF 82576 (Dual Fiber)"),
342 	PVID(0x8086, E1000_DEV_ID_82576_SERDES,
343 	    "Intel(R) PRO/1000 82576 (Dual SERDES)"),
344 	PVID(0x8086, E1000_DEV_ID_82576_SERDES_QUAD,
345 	    "Intel(R) PRO/1000 ET 82576 (Quad SERDES)"),
346 	PVID(0x8086, E1000_DEV_ID_82576_QUAD_COPPER,
347 	    "Intel(R) PRO/1000 ET 82576 (Quad Copper)"),
348 	PVID(0x8086, E1000_DEV_ID_82576_QUAD_COPPER_ET2,
349 	    "Intel(R) PRO/1000 ET(2) 82576 (Quad Copper)"),
350 	PVID(0x8086, E1000_DEV_ID_82580_COPPER,
351 	    "Intel(R) I340 82580 (Copper)"),
352 	PVID(0x8086, E1000_DEV_ID_82580_FIBER, "Intel(R) I340 82580 (Fiber)"),
353 	PVID(0x8086, E1000_DEV_ID_82580_SERDES,
354 	    "Intel(R) I340 82580 (SERDES)"),
355 	PVID(0x8086, E1000_DEV_ID_82580_SGMII, "Intel(R) I340 82580 (SGMII)"),
356 	PVID(0x8086, E1000_DEV_ID_82580_COPPER_DUAL,
357 	    "Intel(R) I340-T2 82580 (Dual Copper)"),
358 	PVID(0x8086, E1000_DEV_ID_82580_QUAD_FIBER,
359 	    "Intel(R) I340-F4 82580 (Quad Fiber)"),
360 	PVID(0x8086, E1000_DEV_ID_DH89XXCC_SERDES,
361 	    "Intel(R) DH89XXCC (SERDES)"),
362 	PVID(0x8086, E1000_DEV_ID_DH89XXCC_SGMII,
363 	    "Intel(R) I347-AT4 DH89XXCC"),
364 	PVID(0x8086, E1000_DEV_ID_DH89XXCC_SFP, "Intel(R) DH89XXCC (SFP)"),
365 	PVID(0x8086, E1000_DEV_ID_DH89XXCC_BACKPLANE,
366 	    "Intel(R) DH89XXCC (Backplane)"),
367 	PVID(0x8086, E1000_DEV_ID_I350_COPPER, "Intel(R) I350 (Copper)"),
368 	PVID(0x8086, E1000_DEV_ID_I350_FIBER, "Intel(R) I350 (Fiber)"),
369 	PVID(0x8086, E1000_DEV_ID_I350_SERDES, "Intel(R) I350 (SERDES)"),
370 	PVID(0x8086, E1000_DEV_ID_I350_SGMII, "Intel(R) I350 (SGMII)"),
371 	PVID(0x8086, E1000_DEV_ID_I210_COPPER, "Intel(R) I210 (Copper)"),
372 	PVID(0x8086, E1000_DEV_ID_I210_COPPER_IT,
373 	    "Intel(R) I210 IT (Copper)"),
374 	PVID(0x8086, E1000_DEV_ID_I210_COPPER_OEM1, "Intel(R) I210 (OEM)"),
375 	PVID(0x8086, E1000_DEV_ID_I210_COPPER_FLASHLESS,
376 	    "Intel(R) I210 Flashless (Copper)"),
377 	PVID(0x8086, E1000_DEV_ID_I210_SERDES_FLASHLESS,
378 	    "Intel(R) I210 Flashless (SERDES)"),
379 	PVID(0x8086, E1000_DEV_ID_I210_SGMII_FLASHLESS,
380 	    "Intel(R) I210 Flashless (SGMII)"),
381 	PVID(0x8086, E1000_DEV_ID_I210_FIBER, "Intel(R) I210 (Fiber)"),
382 	PVID(0x8086, E1000_DEV_ID_I210_SERDES, "Intel(R) I210 (SERDES)"),
383 	PVID(0x8086, E1000_DEV_ID_I210_SGMII, "Intel(R) I210 (SGMII)"),
384 	PVID(0x8086, E1000_DEV_ID_I211_COPPER, "Intel(R) I211 (Copper)"),
385 	PVID(0x8086, E1000_DEV_ID_I354_BACKPLANE_1GBPS,
386 	    "Intel(R) I354 (1.0 GbE Backplane)"),
387 	PVID(0x8086, E1000_DEV_ID_I354_BACKPLANE_2_5GBPS,
388 	    "Intel(R) I354 (2.5 GbE Backplane)"),
389 	PVID(0x8086, E1000_DEV_ID_I354_SGMII, "Intel(R) I354 (SGMII)"),
390 	/* required last entry */
391 	PVID_END
392 };
393 
394 static const pci_vendor_info_t igbv_vendor_info_array[] = {
395 	PVID(0x8086, E1000_DEV_ID_82576_VF,
396 	    "Intel(R) PRO/1000 82576 Virtual Function"),
397 	PVID(0x8086, E1000_DEV_ID_82576_VF_HV,
398 	    "Intel(R) PRO/1000 82576 Virtual Function"),
399 	PVID(0x8086, E1000_DEV_ID_I350_VF,
400 	    "Intel(R) I350 Virtual Function"),
401 	PVID(0x8086, E1000_DEV_ID_I350_VF_HV,
402 	    "Intel(R) I350 Virtual Function"),
403 	PVID_END
404 };
405 
406 /*********************************************************************
407  *  Function prototypes
408  *********************************************************************/
409 static void	*em_register(device_t);
410 static void	*igb_register(device_t);
411 static void	*igbv_register(device_t);
412 static int	igb_device_attach(device_t);
413 #ifdef PCI_IOV
414 static int	igb_device_iov_init(device_t, uint16_t, const nvlist_t *);
415 static void	igb_device_iov_uninit(device_t);
416 #endif
417 static int	em_if_detach(if_ctx_t);
418 static int	em_if_shutdown(if_ctx_t);
419 static int	em_if_suspend(if_ctx_t);
420 static int	em_if_resume(if_ctx_t);
421 
422 static int	em_if_tx_queues_alloc(if_ctx_t, caddr_t *, uint64_t *, int,
423     int);
424 static int	em_if_rx_queues_alloc(if_ctx_t, caddr_t *, uint64_t *, int,
425     int);
426 static void	em_if_queues_free(if_ctx_t);
427 
428 static uint64_t	em_if_get_vf_counter(if_ctx_t, ift_counter);
429 static uint64_t	em_if_get_counter(if_ctx_t, ift_counter);
430 static void	em_if_init(if_ctx_t);
431 static void	em_if_stop(if_ctx_t);
432 static void	em_fence_pci_busmaster(struct e1000_softc *);
433 static int	em_enable_pci_busmaster(struct e1000_softc *);
434 static void	em_if_media_status(if_ctx_t, struct ifmediareq *);
435 static int	em_if_media_change(if_ctx_t);
436 static int	em_if_mtu_set(if_ctx_t, uint32_t);
437 static void	em_if_timer(if_ctx_t, uint16_t);
438 static void	em_if_vlan_register(if_ctx_t, u16);
439 static void	em_if_vlan_unregister(if_ctx_t, u16);
440 static bool	em_if_needs_restart(if_ctx_t, enum iflib_restart_event);
441 
442 static void	em_identify_hardware(if_ctx_t);
443 static int	em_allocate_pci_resources(if_ctx_t);
444 static void	em_free_pci_resources(if_ctx_t);
445 static int	em_reset(if_ctx_t);
446 static int	em_setup_interface(if_ctx_t);
447 static int	em_setup_msix(if_ctx_t);
448 
449 static void	em_initialize_transmit_unit(if_ctx_t);
450 static void	em_initialize_receive_unit(if_ctx_t);
451 
452 static void	em_if_intr_enable(if_ctx_t);
453 static void	em_if_intr_disable(if_ctx_t);
454 static void	igb_if_intr_enable(if_ctx_t);
455 static void	igb_if_intr_disable(if_ctx_t);
456 static int	em_if_rx_queue_intr_enable(if_ctx_t, uint16_t);
457 static int	em_if_tx_queue_intr_enable(if_ctx_t, uint16_t);
458 static int	igb_if_rx_queue_intr_enable(if_ctx_t, uint16_t);
459 static int	igb_if_tx_queue_intr_enable(if_ctx_t, uint16_t);
460 static void	em_handle_fatal_error_intr(struct e1000_softc *, u32);
461 static bool	em_handle_fatal_error_admin(struct e1000_softc *);
462 static u32	igb_device_reset_intr_mask(struct e1000_softc *);
463 static bool	igb_device_reset_pending(struct e1000_softc *);
464 static bool	igb_handle_device_reset(struct e1000_softc *, u32);
465 static void	igb_prepare_device_reset(struct e1000_softc *);
466 static bool	igb_finish_device_reset(struct e1000_softc *, u32);
467 static void	em_prepare_fatal_error_reset(struct e1000_softc *);
468 static void	em_finish_fatal_error_reset(struct e1000_softc *);
469 static void	em_configure_peind_memory_errors(struct e1000_softc *);
470 static void	em_configure_82575_memory_errors(struct e1000_softc *);
471 static void	em_configure_82580_memory_errors(struct e1000_softc *);
472 static void	em_update_82580_ecc_stats(struct e1000_softc *, u32, u32,
473 		    u32);
474 static void	em_if_multi_set(if_ctx_t);
475 static void	em_if_update_admin_status(if_ctx_t);
476 static void	em_if_debug(if_ctx_t);
477 static void	em_initialize_vf_stats(struct e1000_softc *);
478 static void	em_rebase_vf_stats(struct e1000_softc *);
479 static void	em_update_vf_stats_counters(struct e1000_softc *);
480 static void	em_add_hw_stats(struct e1000_softc *);
481 static bool	em_mac_has_eee(enum e1000_mac_type);
482 static int	em_if_set_promisc(if_ctx_t, int);
483 static bool	em_if_defer_promisc(struct e1000_softc *);
484 static bool	em_if_vlan_filter_capable(if_ctx_t);
485 static bool	em_if_vlan_filter_used(if_ctx_t);
486 static void	em_if_vlan_filter_enable(struct e1000_softc *);
487 static void	em_if_vlan_filter_disable(struct e1000_softc *);
488 static void	em_if_vlan_filter_write(struct e1000_softc *, int);
489 static void	em_setup_vlan_hw_support(if_ctx_t ctx);
490 static int	em_sysctl_nvm_info(SYSCTL_HANDLER_ARGS);
491 static void	em_print_nvm_info(struct e1000_softc *);
492 static void	em_fw_version_locked(if_ctx_t);
493 static void	em_sbuf_fw_version(struct e1000_fw_version *, struct sbuf *);
494 static void	em_print_fw_version(struct e1000_softc *);
495 static int	em_sysctl_print_fw_version(SYSCTL_HANDLER_ARGS);
496 static int	em_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
497 static int	em_get_rs(SYSCTL_HANDLER_ARGS);
498 static void	em_print_debug_info(struct e1000_softc *);
499 static void	em_newitr(struct e1000_softc *, struct em_rx_queue *,
500     struct rx_ring *);
501 static bool	em_automask_tso(if_ctx_t);
502 static int	em_sysctl_tso_tcp_flags_mask(SYSCTL_HANDLER_ARGS);
503 static int	em_sysctl_int_delay(SYSCTL_HANDLER_ARGS);
504 static void	em_add_int_delay_sysctl(struct e1000_softc *, const char *,
505     const char *, struct em_int_delay_info *, int, int);
506 /* Management and WOL Support */
507 static void	em_init_manageability(struct e1000_softc *);
508 static void	em_release_manageability(struct e1000_softc *);
509 static void	em_get_hw_control(struct e1000_softc *);
510 static void	em_release_hw_control(struct e1000_softc *);
511 static void	em_get_wakeup(if_ctx_t);
512 static void	em_fill_wakeup_mta(struct e1000_hw *);
513 static int	em_enable_wakeup(if_ctx_t);
514 static void	em_configure_sx_low_power(struct e1000_softc *, u32);
515 static int	em_enable_phy_wakeup(struct e1000_softc *, u32);
516 static int	em_disable_phy_wakeup(struct e1000_softc *, u16 *);
517 static void	em_power_up_wakeup_link(struct e1000_softc *);
518 static void	em_power_down_wakeup_link(struct e1000_softc *);
519 static void	em_disable_aspm(struct e1000_softc *);
520 
521 int		em_intr(void *);
522 
523 enum em_fatal_error_state {
524 	EM_FATAL_ERROR_NONE,
525 	EM_FATAL_ERROR_CAPTURING,
526 	EM_FATAL_ERROR_DETECTED,
527 	EM_FATAL_ERROR_RESET_REQUESTED,
528 	EM_FATAL_ERROR_RESET_PREPARED,
529 };
530 
531 enum igb_device_reset_state {
532 	IGB_DEVICE_RESET_NONE,
533 	IGB_DEVICE_RESET_DETECTED,
534 	IGB_DEVICE_RESET_REQUESTED,
535 	IGB_DEVICE_RESET_PREPARED,
536 };
537 
538 #define IGB_DEVICE_RESET_TIMEOUT_MS	100
539 
540 /* MSI-X handlers */
541 static int	em_if_msix_intr_assign(if_ctx_t, int);
542 static int	em_msix_link(void *);
543 static void	em_handle_link(void *);
544 
545 static void	em_enable_vectors_82574(if_ctx_t);
546 
547 static int	em_set_flowcntl(SYSCTL_HANDLER_ARGS);
548 static int	em_sysctl_eee(SYSCTL_HANDLER_ARGS);
549 static int	igb_sysctl_dmac(SYSCTL_HANDLER_ARGS);
550 static void	em_if_led_func(if_ctx_t, int);
551 
552 static int	em_get_regs(SYSCTL_HANDLER_ARGS);
553 static void	lem_smartspeed(struct e1000_softc *);
554 static void	igb_configure_queues(struct e1000_softc *);
555 static void	igb_initialize_interrupt_rate(struct e1000_softc *);
556 static void	em_flush_desc_rings(struct e1000_softc *);
557 
558 
559 /*********************************************************************
560  *  FreeBSD Device Interface Entry Points
561  *********************************************************************/
562 static device_method_t em_methods[] = {
563 	/* Device interface */
564 	DEVMETHOD(device_register, em_register),
565 	DEVMETHOD(device_probe, iflib_device_probe),
566 	DEVMETHOD(device_attach, iflib_device_attach),
567 	DEVMETHOD(device_detach, iflib_device_detach),
568 	DEVMETHOD(device_shutdown, iflib_device_shutdown),
569 	DEVMETHOD(device_suspend, iflib_device_suspend),
570 	DEVMETHOD(device_resume, iflib_device_resume),
571 	DEVMETHOD_END
572 };
573 
574 static device_method_t igb_methods[] = {
575 	/* Device interface */
576 	DEVMETHOD(device_register, igb_register),
577 	DEVMETHOD(device_probe, iflib_device_probe),
578 	DEVMETHOD(device_attach, igb_device_attach),
579 	DEVMETHOD(device_detach, iflib_device_detach),
580 	DEVMETHOD(device_shutdown, iflib_device_shutdown),
581 	DEVMETHOD(device_suspend, iflib_device_suspend),
582 	DEVMETHOD(device_resume, iflib_device_resume),
583 #ifdef PCI_IOV
584 	DEVMETHOD(pci_iov_init, igb_device_iov_init),
585 	DEVMETHOD(pci_iov_uninit, igb_device_iov_uninit),
586 	DEVMETHOD(pci_iov_add_vf, iflib_device_iov_add_vf),
587 #endif
588 	DEVMETHOD_END
589 };
590 
591 static device_method_t igbv_methods[] = {
592 	/* Device interface */
593 	DEVMETHOD(device_register, igbv_register),
594 	DEVMETHOD(device_probe, iflib_device_probe),
595 	DEVMETHOD(device_attach, iflib_device_attach),
596 	DEVMETHOD(device_detach, iflib_device_detach),
597 	DEVMETHOD(device_shutdown, iflib_device_shutdown),
598 	DEVMETHOD(device_suspend, iflib_device_suspend),
599 	DEVMETHOD(device_resume, iflib_device_resume),
600 	DEVMETHOD_END
601 };
602 
603 
604 static driver_t em_driver = {
605 	"em", em_methods, sizeof(struct e1000_softc),
606 };
607 
608 DRIVER_MODULE(em, pci, em_driver, 0, 0);
609 
610 MODULE_DEPEND(em, pci, 1, 1, 1);
611 MODULE_DEPEND(em, ether, 1, 1, 1);
612 MODULE_DEPEND(em, iflib, 1, 1, 1);
613 
614 IFLIB_PNP_INFO(pci, em, em_vendor_info_array);
615 
616 static driver_t igb_driver = {
617 	"igb", igb_methods, sizeof(struct e1000_softc),
618 };
619 
620 DRIVER_MODULE(igb, pci, igb_driver, 0, 0);
621 
622 MODULE_DEPEND(igb, pci, 1, 1, 1);
623 MODULE_DEPEND(igb, ether, 1, 1, 1);
624 MODULE_DEPEND(igb, iflib, 1, 1, 1);
625 
626 IFLIB_PNP_INFO(pci, igb, igb_vendor_info_array);
627 
628 static driver_t igbv_driver = {
629 	"igbv", igbv_methods, sizeof(struct e1000_softc),
630 };
631 
632 DRIVER_MODULE(igbv, pci, igbv_driver, 0, 0);
633 
634 MODULE_DEPEND(igbv, pci, 1, 1, 1);
635 MODULE_DEPEND(igbv, ether, 1, 1, 1);
636 MODULE_DEPEND(igbv, iflib, 1, 1, 1);
637 
638 IFLIB_PNP_INFO(pci, igbv_driver, igbv_vendor_info_array);
639 
640 static device_method_t em_if_methods[] = {
641 	DEVMETHOD(ifdi_attach_pre, em_if_attach_pre),
642 	DEVMETHOD(ifdi_attach_post, em_if_attach_post),
643 	DEVMETHOD(ifdi_detach, em_if_detach),
644 	DEVMETHOD(ifdi_shutdown, em_if_shutdown),
645 	DEVMETHOD(ifdi_suspend, em_if_suspend),
646 	DEVMETHOD(ifdi_resume, em_if_resume),
647 	DEVMETHOD(ifdi_init, em_if_init),
648 	DEVMETHOD(ifdi_stop, em_if_stop),
649 	DEVMETHOD(ifdi_msix_intr_assign, em_if_msix_intr_assign),
650 	DEVMETHOD(ifdi_intr_enable, em_if_intr_enable),
651 	DEVMETHOD(ifdi_intr_disable, em_if_intr_disable),
652 	DEVMETHOD(ifdi_tx_queues_alloc, em_if_tx_queues_alloc),
653 	DEVMETHOD(ifdi_rx_queues_alloc, em_if_rx_queues_alloc),
654 	DEVMETHOD(ifdi_queues_free, em_if_queues_free),
655 	DEVMETHOD(ifdi_update_admin_status, em_if_update_admin_status),
656 	DEVMETHOD(ifdi_multi_set, em_if_multi_set),
657 	DEVMETHOD(ifdi_media_status, em_if_media_status),
658 	DEVMETHOD(ifdi_media_change, em_if_media_change),
659 	DEVMETHOD(ifdi_mtu_set, em_if_mtu_set),
660 	DEVMETHOD(ifdi_promisc_set, em_if_set_promisc),
661 	DEVMETHOD(ifdi_timer, em_if_timer),
662 	DEVMETHOD(ifdi_vlan_register, em_if_vlan_register),
663 	DEVMETHOD(ifdi_vlan_unregister, em_if_vlan_unregister),
664 	DEVMETHOD(ifdi_get_counter, em_if_get_counter),
665 	DEVMETHOD(ifdi_led_func, em_if_led_func),
666 	DEVMETHOD(ifdi_rx_queue_intr_enable, em_if_rx_queue_intr_enable),
667 	DEVMETHOD(ifdi_tx_queue_intr_enable, em_if_tx_queue_intr_enable),
668 	DEVMETHOD(ifdi_debug, em_if_debug),
669 	DEVMETHOD(ifdi_needs_restart, em_if_needs_restart),
670 	DEVMETHOD_END
671 };
672 
673 static driver_t em_if_driver = {
674 	"em_if", em_if_methods, sizeof(struct e1000_softc)
675 };
676 
677 static device_method_t igb_if_methods[] = {
678 	DEVMETHOD(ifdi_attach_pre, em_if_attach_pre),
679 	DEVMETHOD(ifdi_attach_post, em_if_attach_post),
680 	DEVMETHOD(ifdi_detach, em_if_detach),
681 	DEVMETHOD(ifdi_shutdown, em_if_shutdown),
682 	DEVMETHOD(ifdi_suspend, em_if_suspend),
683 	DEVMETHOD(ifdi_resume, em_if_resume),
684 	DEVMETHOD(ifdi_init, em_if_init),
685 	DEVMETHOD(ifdi_stop, em_if_stop),
686 	DEVMETHOD(ifdi_msix_intr_assign, em_if_msix_intr_assign),
687 	DEVMETHOD(ifdi_intr_enable, igb_if_intr_enable),
688 	DEVMETHOD(ifdi_intr_disable, igb_if_intr_disable),
689 	DEVMETHOD(ifdi_tx_queues_alloc, em_if_tx_queues_alloc),
690 	DEVMETHOD(ifdi_rx_queues_alloc, em_if_rx_queues_alloc),
691 	DEVMETHOD(ifdi_queues_free, em_if_queues_free),
692 	DEVMETHOD(ifdi_update_admin_status, em_if_update_admin_status),
693 	DEVMETHOD(ifdi_multi_set, em_if_multi_set),
694 	DEVMETHOD(ifdi_media_status, em_if_media_status),
695 	DEVMETHOD(ifdi_media_change, em_if_media_change),
696 	DEVMETHOD(ifdi_mtu_set, em_if_mtu_set),
697 	DEVMETHOD(ifdi_promisc_set, em_if_set_promisc),
698 	DEVMETHOD(ifdi_timer, em_if_timer),
699 	DEVMETHOD(ifdi_vlan_register, em_if_vlan_register),
700 	DEVMETHOD(ifdi_vlan_unregister, em_if_vlan_unregister),
701 	DEVMETHOD(ifdi_get_counter, em_if_get_counter),
702 	DEVMETHOD(ifdi_led_func, em_if_led_func),
703 	DEVMETHOD(ifdi_rx_queue_intr_enable, igb_if_rx_queue_intr_enable),
704 	DEVMETHOD(ifdi_tx_queue_intr_enable, igb_if_tx_queue_intr_enable),
705 	DEVMETHOD(ifdi_debug, em_if_debug),
706 	DEVMETHOD(ifdi_needs_restart, em_if_needs_restart),
707 #ifdef PCI_IOV
708 	DEVMETHOD(ifdi_iov_init, igb_if_iov_init),
709 	DEVMETHOD(ifdi_iov_uninit, igb_if_iov_uninit),
710 	DEVMETHOD(ifdi_iov_vf_add, igb_if_iov_vf_add),
711 #endif
712 	DEVMETHOD_END
713 };
714 
715 static driver_t igb_if_driver = {
716 	"igb_if", igb_if_methods, sizeof(struct e1000_softc)
717 };
718 
719 static device_method_t igbv_if_methods[] = {
720 	DEVMETHOD(ifdi_attach_pre, igbv_if_attach_pre),
721 	DEVMETHOD(ifdi_attach_post, igbv_if_attach_post),
722 	DEVMETHOD(ifdi_detach, em_if_detach),
723 	DEVMETHOD(ifdi_shutdown, em_if_shutdown),
724 	DEVMETHOD(ifdi_suspend, em_if_suspend),
725 	DEVMETHOD(ifdi_resume, em_if_resume),
726 	DEVMETHOD(ifdi_init, em_if_init),
727 	DEVMETHOD(ifdi_stop, em_if_stop),
728 	DEVMETHOD(ifdi_msix_intr_assign, em_if_msix_intr_assign),
729 	DEVMETHOD(ifdi_intr_enable, igbv_if_intr_enable),
730 	DEVMETHOD(ifdi_intr_disable, igbv_if_intr_disable),
731 	DEVMETHOD(ifdi_tx_queues_alloc, em_if_tx_queues_alloc),
732 	DEVMETHOD(ifdi_rx_queues_alloc, em_if_rx_queues_alloc),
733 	DEVMETHOD(ifdi_queues_free, em_if_queues_free),
734 	DEVMETHOD(ifdi_update_admin_status, igbv_if_update_admin_status),
735 	DEVMETHOD(ifdi_multi_set, em_if_multi_set),
736 	DEVMETHOD(ifdi_media_status, em_if_media_status),
737 	DEVMETHOD(ifdi_media_change, igbv_if_media_change),
738 	DEVMETHOD(ifdi_mtu_set, em_if_mtu_set),
739 	DEVMETHOD(ifdi_promisc_set, em_if_set_promisc),
740 	DEVMETHOD(ifdi_timer, em_if_timer),
741 	DEVMETHOD(ifdi_vlan_register, em_if_vlan_register),
742 	DEVMETHOD(ifdi_vlan_unregister, em_if_vlan_unregister),
743 	DEVMETHOD(ifdi_get_counter, em_if_get_counter),
744 	DEVMETHOD(ifdi_rx_queue_intr_enable, igb_if_rx_queue_intr_enable),
745 	DEVMETHOD(ifdi_tx_queue_intr_enable, igb_if_tx_queue_intr_enable),
746 	DEVMETHOD(ifdi_debug, em_if_debug),
747 	DEVMETHOD(ifdi_needs_restart, em_if_needs_restart),
748 	DEVMETHOD_END
749 };
750 
751 static driver_t igbv_if_driver = {
752 	"igbv_if", igbv_if_methods, sizeof(struct e1000_softc)
753 };
754 
755 /*********************************************************************
756  *  Tunable default values.
757  *********************************************************************/
758 
759 #define EM_TICKS_TO_USECS(ticks)	((1024 * (ticks) + 500) / 1000)
760 #define EM_USECS_TO_TICKS(usecs)	((1000 * (usecs) + 512) / 1024)
761 
762 /* Allow common code without TSO */
763 #ifndef CSUM_TSO
764 #define CSUM_TSO	0
765 #endif
766 
767 static SYSCTL_NODE(_hw, OID_AUTO, em, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
768     "EM driver parameters");
769 
770 static int em_disable_crc_stripping = 0;
771 SYSCTL_INT(_hw_em, OID_AUTO, disable_crc_stripping, CTLFLAG_RDTUN,
772     &em_disable_crc_stripping, 0, "Disable CRC Stripping");
773 
774 static int em_tx_int_delay_dflt = EM_TICKS_TO_USECS(EM_TIDV);
775 static int em_rx_int_delay_dflt = EM_TICKS_TO_USECS(EM_RDTR);
776 SYSCTL_INT(_hw_em, OID_AUTO, tx_int_delay, CTLFLAG_RDTUN,
777     &em_tx_int_delay_dflt, 0, "Default transmit interrupt delay in usecs");
778 SYSCTL_INT(_hw_em, OID_AUTO, rx_int_delay, CTLFLAG_RDTUN,
779     &em_rx_int_delay_dflt, 0, "Default receive interrupt delay in usecs");
780 
781 static int em_tx_abs_int_delay_dflt = EM_TICKS_TO_USECS(EM_TADV);
782 static int em_rx_abs_int_delay_dflt = EM_TICKS_TO_USECS(EM_RADV);
783 SYSCTL_INT(_hw_em, OID_AUTO, tx_abs_int_delay, CTLFLAG_RDTUN,
784     &em_tx_abs_int_delay_dflt, 0,
785     "Default transmit interrupt delay limit in usecs");
786 SYSCTL_INT(_hw_em, OID_AUTO, rx_abs_int_delay, CTLFLAG_RDTUN,
787     &em_rx_abs_int_delay_dflt, 0,
788     "Default receive interrupt delay limit in usecs");
789 
790 static int em_smart_pwr_down = false;
791 SYSCTL_INT(_hw_em, OID_AUTO, smart_pwr_down, CTLFLAG_RDTUN,
792     &em_smart_pwr_down,
793     0, "Set to true to leave smart power down enabled on newer adapters");
794 
795 static bool em_unsupported_tso = false;
796 SYSCTL_BOOL(_hw_em, OID_AUTO, unsupported_tso, CTLFLAG_RDTUN,
797     &em_unsupported_tso, 0, "Allow unsupported em(4) TSO configurations");
798 
799 /* Controls whether promiscuous also shows bad packets */
800 static int em_debug_sbp = false;
801 SYSCTL_INT(_hw_em, OID_AUTO, sbp, CTLFLAG_RDTUN, &em_debug_sbp, 0,
802     "Show bad packets in promiscuous mode");
803 
804 /* Energy efficient ethernet - default to OFF */
805 static int eee_setting = 1;
806 SYSCTL_INT(_hw_em, OID_AUTO, eee_setting, CTLFLAG_RDTUN, &eee_setting, 0,
807     "Enable Energy Efficient Ethernet");
808 
809 /*
810  * AIM: Adaptive Interrupt Moderation
811  * which means that the interrupt rate is varied over time based on the
812  * traffic for that interrupt vector
813  */
814 static int em_enable_aim = 1;
815 SYSCTL_INT(_hw_em, OID_AUTO, enable_aim, CTLFLAG_RWTUN, &em_enable_aim,
816     0, "Enable adaptive interrupt moderation (1=normal, 2=lowlatency)");
817 
818 /*
819 ** Tuneable Interrupt rate
820 */
821 static int em_max_interrupt_rate = EM_INTS_DEFAULT;
822 SYSCTL_INT(_hw_em, OID_AUTO, max_interrupt_rate, CTLFLAG_RDTUN,
823     &em_max_interrupt_rate, 0, "Maximum interrupts per second");
824 
825 /* Global used in WOL setup with multiport cards */
826 static int global_quad_port_a = 0;
827 
828 extern struct if_txrx igb_txrx;
829 extern struct if_txrx em_txrx;
830 extern struct if_txrx lem_txrx;
831 
832 static struct if_shared_ctx em_sctx_init = {
833 	.isc_magic = IFLIB_MAGIC,
834 	.isc_q_align = PAGE_SIZE,
835 	.isc_tx_maxsize = EM_TSO_SIZE + sizeof(struct ether_vlan_header),
836 	.isc_tx_maxsegsize = PAGE_SIZE,
837 	.isc_tso_maxsize = EM_TSO_SIZE + sizeof(struct ether_vlan_header),
838 	.isc_tso_maxsegsize = EM_TSO_SEG_SIZE,
839 	.isc_rx_maxsize = MJUM9BYTES,
840 	.isc_rx_nsegments = 1,
841 	.isc_rx_maxsegsize = MJUM9BYTES,
842 	.isc_nfl = 1,
843 	.isc_nrxqs = 1,
844 	.isc_ntxqs = 1,
845 	.isc_admin_intrcnt = 1,
846 	.isc_vendor_info = em_vendor_info_array,
847 	.isc_driver_version = em_driver_version,
848 	.isc_driver = &em_if_driver,
849 	.isc_flags =
850 	    IFLIB_NEED_SCRATCH | IFLIB_TSO_INIT_IP | IFLIB_NEED_ZERO_CSUM,
851 
852 	.isc_nrxd_min = {EM_MIN_RXD},
853 	.isc_ntxd_min = {EM_MIN_TXD},
854 	.isc_nrxd_max = {EM_MAX_RXD},
855 	.isc_ntxd_max = {EM_MAX_TXD},
856 	.isc_nrxd_default = {EM_DEFAULT_RXD},
857 	.isc_ntxd_default = {EM_DEFAULT_TXD},
858 };
859 
860 static struct if_shared_ctx igb_sctx_init = {
861 	.isc_magic = IFLIB_MAGIC,
862 	.isc_q_align = PAGE_SIZE,
863 	.isc_tx_maxsize = EM_TSO_SIZE + sizeof(struct ether_vlan_header),
864 	.isc_tx_maxsegsize = PAGE_SIZE,
865 	.isc_tso_maxsize = EM_TSO_SIZE + sizeof(struct ether_vlan_header),
866 	.isc_tso_maxsegsize = EM_TSO_SEG_SIZE,
867 	.isc_rx_maxsize = MJUM9BYTES,
868 	.isc_rx_nsegments = 1,
869 	.isc_rx_maxsegsize = MJUM9BYTES,
870 	.isc_nfl = 1,
871 	.isc_nrxqs = 1,
872 	.isc_ntxqs = 1,
873 	.isc_admin_intrcnt = 1,
874 	.isc_vendor_info = igb_vendor_info_array,
875 	.isc_driver_version = igb_driver_version,
876 	.isc_driver = &igb_if_driver,
877 	.isc_flags =
878 	    IFLIB_NEED_SCRATCH | IFLIB_TSO_INIT_IP | IFLIB_NEED_ZERO_CSUM,
879 
880 	.isc_nrxd_min = {EM_MIN_RXD},
881 	.isc_ntxd_min = {EM_MIN_TXD},
882 	.isc_nrxd_max = {IGB_MAX_RXD},
883 	.isc_ntxd_max = {IGB_MAX_TXD},
884 	.isc_nrxd_default = {EM_DEFAULT_RXD},
885 	.isc_ntxd_default = {EM_DEFAULT_TXD},
886 };
887 
888 /*
889  * igb PFs and igbv VFs share the common datapath implementation.  Keep a
890  * separate ifdi policy for VFs so they cannot inherit PF-only callbacks or
891  * interrupt modes.
892  */
893 static struct if_shared_ctx igbv_sctx_init = {
894 	.isc_magic = IFLIB_MAGIC,
895 	.isc_q_align = PAGE_SIZE,
896 	.isc_tx_maxsize = EM_TSO_SIZE + sizeof(struct ether_vlan_header),
897 	.isc_tx_maxsegsize = PAGE_SIZE,
898 	.isc_tso_maxsize = EM_TSO_SIZE + sizeof(struct ether_vlan_header),
899 	.isc_tso_maxsegsize = EM_TSO_SEG_SIZE,
900 	.isc_rx_maxsize = MJUM9BYTES,
901 	.isc_rx_nsegments = 1,
902 	.isc_rx_maxsegsize = MJUM9BYTES,
903 	.isc_nfl = 1,
904 	.isc_nrxqs = 1,
905 	.isc_ntxqs = 1,
906 	.isc_admin_intrcnt = 1,
907 	.isc_vendor_info = igbv_vendor_info_array,
908 	.isc_driver_version = igb_driver_version,
909 	.isc_driver = &igbv_if_driver,
910 	.isc_flags =
911 	    IFLIB_NEED_SCRATCH | IFLIB_TSO_INIT_IP | IFLIB_NEED_ZERO_CSUM |
912 	    IFLIB_IS_VF,
913 
914 	.isc_nrxd_min = {EM_MIN_RXD},
915 	.isc_ntxd_min = {EM_MIN_TXD},
916 	.isc_nrxd_max = {IGB_MAX_RXD},
917 	.isc_ntxd_max = {IGB_MAX_TXD},
918 	.isc_nrxd_default = {EM_DEFAULT_RXD},
919 	.isc_ntxd_default = {EM_DEFAULT_TXD},
920 };
921 
922 /*****************************************************************
923  *
924  * Dump Registers
925  *
926  ****************************************************************/
927 #define IGB_REGS_LEN 739
928 
em_get_regs(SYSCTL_HANDLER_ARGS)929 static int em_get_regs(SYSCTL_HANDLER_ARGS)
930 {
931 	struct e1000_softc *sc = (struct e1000_softc *)arg1;
932 	struct e1000_hw *hw = &sc->hw;
933 	struct sbuf *sb;
934 	u32 *regs_buff;
935 	int rc;
936 	uint32_t rxqid, txqid;
937 
938 	/*
939 	 * This sysctl is registered before iflib allocates the queue arrays,
940 	 * and remains registered while iflib tears them down.
941 	 */
942 	if (sc->rx_queues == NULL || sc->tx_queues == NULL)
943 		return (ENXIO);
944 
945 	regs_buff = malloc(sizeof(u32) * IGB_REGS_LEN, M_DEVBUF, M_WAITOK);
946 	memset(regs_buff, 0, IGB_REGS_LEN * sizeof(u32));
947 	rxqid = sc->rx_queues[0].rxr.me;
948 	txqid = sc->tx_queues[0].txr.me;
949 
950 	rc = sysctl_wire_old_buffer(req, 0);
951 	MPASS(rc == 0);
952 	if (rc != 0) {
953 		free(regs_buff, M_DEVBUF);
954 		return (rc);
955 	}
956 
957 	sb = sbuf_new_for_sysctl(NULL, NULL, 32*400, req);
958 	MPASS(sb != NULL);
959 	if (sb == NULL) {
960 		free(regs_buff, M_DEVBUF);
961 		return (ENOMEM);
962 	}
963 
964 	/* General Registers */
965 	regs_buff[0] = E1000_READ_REG(hw, E1000_CTRL);
966 	regs_buff[1] = E1000_READ_REG(hw, E1000_STATUS);
967 	regs_buff[2] = E1000_READ_REG(hw, E1000_CTRL_EXT);
968 	regs_buff[3] = E1000_READ_REG(hw, E1000_ICR);
969 	regs_buff[4] = E1000_READ_REG(hw, E1000_RCTL);
970 	regs_buff[5] = E1000_READ_REG(hw, E1000_RDLEN(rxqid));
971 	regs_buff[6] = E1000_READ_REG(hw, E1000_RDH(rxqid));
972 	regs_buff[7] = E1000_READ_REG(hw, E1000_RDT(rxqid));
973 	regs_buff[8] = E1000_READ_REG(hw, E1000_RXDCTL(rxqid));
974 	regs_buff[9] = E1000_READ_REG(hw, E1000_RDBAL(rxqid));
975 	regs_buff[10] = E1000_READ_REG(hw, E1000_RDBAH(rxqid));
976 	regs_buff[11] = E1000_READ_REG(hw, E1000_TCTL);
977 	regs_buff[12] = E1000_READ_REG(hw, E1000_TDBAL(txqid));
978 	regs_buff[13] = E1000_READ_REG(hw, E1000_TDBAH(txqid));
979 	regs_buff[14] = E1000_READ_REG(hw, E1000_TDLEN(txqid));
980 	regs_buff[15] = E1000_READ_REG(hw, E1000_TDH(txqid));
981 	regs_buff[16] = E1000_READ_REG(hw, E1000_TDT(txqid));
982 	regs_buff[17] = E1000_READ_REG(hw, E1000_TXDCTL(txqid));
983 	regs_buff[18] = E1000_READ_REG(hw, E1000_TDFH);
984 	regs_buff[19] = E1000_READ_REG(hw, E1000_TDFT);
985 	regs_buff[20] = E1000_READ_REG(hw, E1000_TDFHS);
986 	regs_buff[21] = E1000_READ_REG(hw, E1000_TDFPC);
987 
988 	sbuf_printf(sb, "General Registers\n");
989 	sbuf_printf(sb, "\tCTRL\t %08x\n", regs_buff[0]);
990 	sbuf_printf(sb, "\tSTATUS\t %08x\n", regs_buff[1]);
991 	sbuf_printf(sb, "\tCTRL_EXT\t %08x\n\n", regs_buff[2]);
992 
993 	sbuf_printf(sb, "Interrupt Registers\n");
994 	sbuf_printf(sb, "\tICR\t %08x\n\n", regs_buff[3]);
995 
996 	sbuf_printf(sb, "RX Registers\n");
997 	sbuf_printf(sb, "\tRCTL\t %08x\n", regs_buff[4]);
998 	sbuf_printf(sb, "\tRDLEN\t %08x\n", regs_buff[5]);
999 	sbuf_printf(sb, "\tRDH\t %08x\n", regs_buff[6]);
1000 	sbuf_printf(sb, "\tRDT\t %08x\n", regs_buff[7]);
1001 	sbuf_printf(sb, "\tRXDCTL\t %08x\n", regs_buff[8]);
1002 	sbuf_printf(sb, "\tRDBAL\t %08x\n", regs_buff[9]);
1003 	sbuf_printf(sb, "\tRDBAH\t %08x\n\n", regs_buff[10]);
1004 
1005 	sbuf_printf(sb, "TX Registers\n");
1006 	sbuf_printf(sb, "\tTCTL\t %08x\n", regs_buff[11]);
1007 	sbuf_printf(sb, "\tTDBAL\t %08x\n", regs_buff[12]);
1008 	sbuf_printf(sb, "\tTDBAH\t %08x\n", regs_buff[13]);
1009 	sbuf_printf(sb, "\tTDLEN\t %08x\n", regs_buff[14]);
1010 	sbuf_printf(sb, "\tTDH\t %08x\n", regs_buff[15]);
1011 	sbuf_printf(sb, "\tTDT\t %08x\n", regs_buff[16]);
1012 	sbuf_printf(sb, "\tTXDCTL\t %08x\n", regs_buff[17]);
1013 	sbuf_printf(sb, "\tTDFH\t %08x\n", regs_buff[18]);
1014 	sbuf_printf(sb, "\tTDFT\t %08x\n", regs_buff[19]);
1015 	sbuf_printf(sb, "\tTDFHS\t %08x\n", regs_buff[20]);
1016 	sbuf_printf(sb, "\tTDFPC\t %08x\n\n", regs_buff[21]);
1017 
1018 	free(regs_buff, M_DEVBUF);
1019 
1020 #ifdef DUMP_DESCS
1021 	{
1022 		if_softc_ctx_t scctx = sc->shared;
1023 		struct rx_ring *rxr = &rx_que->rxr;
1024 		struct tx_ring *txr = &tx_que->txr;
1025 		int ntxd = scctx->isc_ntxd[0];
1026 		int nrxd = scctx->isc_nrxd[0];
1027 		int j;
1028 
1029 	for (j = 0; j < nrxd; j++) {
1030 		u32 staterr = le32toh(rxr->rx_base[j].wb.upper.status_error);
1031 		u32 length =  le32toh(rxr->rx_base[j].wb.upper.length);
1032 		sbuf_printf(sb, "\tReceive Descriptor Address %d: %08"
1033 		    PRIx64 "  Error:%d  Length:%d\n",
1034 		    j, rxr->rx_base[j].read.buffer_addr, staterr, length);
1035 	}
1036 
1037 	for (j = 0; j < min(ntxd, 256); j++) {
1038 		unsigned int *ptr = (unsigned int *)&txr->tx_base[j];
1039 
1040 		sbuf_printf(sb,
1041 		    "\tTXD[%03d] [0]: %08x [1]: %08x [2]: %08x [3]: %08x"
1042 		    "  eop: %d DD=%d\n",
1043 		    j, ptr[0], ptr[1], ptr[2], ptr[3], buf->eop,
1044 		    buf->eop != -1 ?
1045 		    txr->tx_base[buf->eop].upper.fields.status &
1046 		    E1000_TXD_STAT_DD : 0);
1047 
1048 	}
1049 	}
1050 #endif
1051 
1052 	rc = sbuf_finish(sb);
1053 	sbuf_delete(sb);
1054 	return(rc);
1055 }
1056 
1057 static void *
em_register(device_t dev)1058 em_register(device_t dev)
1059 {
1060 	return (&em_sctx_init);
1061 }
1062 
1063 static void *
igb_register(device_t dev)1064 igb_register(device_t dev)
1065 {
1066 	return (&igb_sctx_init);
1067 }
1068 
1069 static void *
igbv_register(device_t dev)1070 igbv_register(device_t dev)
1071 {
1072 	return (&igbv_sctx_init);
1073 }
1074 
1075 static int
igb_device_attach(device_t dev)1076 igb_device_attach(device_t dev)
1077 {
1078 	struct e1000_softc *sc;
1079 	if_ctx_t ctx;
1080 	int error;
1081 
1082 	error = iflib_device_attach(dev);
1083 	if (error != 0)
1084 		return (error);
1085 
1086 	ctx = device_get_softc(dev);
1087 	sc = iflib_get_softc(ctx);
1088 	(void)igb_iov_attach(sc);
1089 	return (0);
1090 }
1091 
1092 #ifdef PCI_IOV
1093 static int
igb_device_iov_init(device_t dev,uint16_t num_vfs,const nvlist_t * params)1094 igb_device_iov_init(device_t dev, uint16_t num_vfs,
1095     const nvlist_t *params)
1096 {
1097 	struct e1000_softc *sc;
1098 	if_ctx_t ctx;
1099 	int error;
1100 
1101 	ctx = device_get_softc(dev);
1102 	sc = iflib_get_softc(ctx);
1103 	error = igb_iov_validate(sc, num_vfs);
1104 	if (error != 0)
1105 		return (error);
1106 	return (iflib_device_iov_init_restart(dev, num_vfs, params));
1107 }
1108 
1109 static void
igb_device_iov_uninit(device_t dev)1110 igb_device_iov_uninit(device_t dev)
1111 {
1112 	struct e1000_softc *sc;
1113 	if_ctx_t ctx;
1114 
1115 	ctx = device_get_softc(dev);
1116 	sc = iflib_get_softc(ctx);
1117 	/*
1118 	 * pci_iov(4) has already detached the VF devices.  Tell the stop
1119 	 * half of iflib's restart transaction not to wait for acknowledgements
1120 	 * from VFs which can no longer service their mailbox vectors.
1121 	 */
1122 	atomic_store_rel_32(&sc->iov_teardown, 1);
1123 	iflib_device_iov_uninit_restart(dev);
1124 }
1125 
1126 #endif
1127 
1128 static int
em_set_num_queues(if_ctx_t ctx)1129 em_set_num_queues(if_ctx_t ctx)
1130 {
1131 	struct e1000_softc *sc = iflib_get_softc(ctx);
1132 	int maxqueues;
1133 
1134 	/* Sanity check based on HW */
1135 	switch (sc->hw.mac.type) {
1136 	case e1000_82576:
1137 	case e1000_82580:
1138 	case e1000_i350:
1139 	case e1000_i354:
1140 		maxqueues = 8;
1141 		break;
1142 	case e1000_i210:
1143 	case e1000_82575:
1144 		maxqueues = 4;
1145 		break;
1146 	case e1000_i211:
1147 	case e1000_82574:
1148 		maxqueues = 2;
1149 		break;
1150 	case e1000_vfadapt:
1151 		/* Keep 82576 VFs at one RX/TX queue for mixed-driver safety. */
1152 	case e1000_vfadapt_i350:
1153 		maxqueues = 1;
1154 		break;
1155 	default:
1156 		maxqueues = 1;
1157 		break;
1158 	}
1159 
1160 	return (maxqueues);
1161 }
1162 
1163 #define LEM_CAPS ( \
1164     IFCAP_HWCSUM | IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING | \
1165     IFCAP_VLAN_HWCSUM | IFCAP_WOL | IFCAP_VLAN_HWFILTER | IFCAP_TSO4 | \
1166     IFCAP_LRO | IFCAP_VLAN_HWTSO | IFCAP_JUMBO_MTU | IFCAP_HWCSUM_IPV6)
1167 
1168 #define EM_CAPS ( \
1169     IFCAP_HWCSUM | IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING | \
1170     IFCAP_VLAN_HWCSUM | IFCAP_WOL | IFCAP_VLAN_HWFILTER | IFCAP_TSO4 | \
1171     IFCAP_LRO | IFCAP_VLAN_HWTSO | IFCAP_JUMBO_MTU | IFCAP_HWCSUM_IPV6 | \
1172     IFCAP_TSO6)
1173 
1174 #define IGB_CAPS ( \
1175     IFCAP_HWCSUM | IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING | \
1176     IFCAP_VLAN_HWCSUM | IFCAP_WOL | IFCAP_VLAN_HWFILTER | IFCAP_TSO4 | \
1177     IFCAP_LRO | IFCAP_VLAN_HWTSO | IFCAP_JUMBO_MTU | IFCAP_HWCSUM_IPV6 | \
1178     IFCAP_TSO6)
1179 
1180 /*
1181  * VLAN filtering is an effective VF capability, but its policy is owned by
1182  * the PF and cannot be disabled from the VF.  vlan(4) registration callbacks
1183  * are independent of this capability bit.
1184  */
1185 #define IGBV_CAPS	(IGB_CAPS & ~IFCAP_WOL)
1186 
1187 void
em_add_device_sysctls(struct e1000_softc * sc)1188 em_add_device_sysctls(struct e1000_softc *sc)
1189 {
1190 	struct e1000_hw *hw;
1191 	struct sysctl_oid_list *child;
1192 	struct sysctl_ctx_list *ctx_list;
1193 
1194 	hw = &sc->hw;
1195 	ctx_list = device_get_sysctl_ctx(sc->dev);
1196 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev));
1197 
1198 	sc->enable_aim = em_enable_aim;
1199 	SYSCTL_ADD_INT(ctx_list, child, OID_AUTO, "enable_aim",
1200 	    CTLFLAG_RW, &sc->enable_aim, 0,
1201 	    "Interrupt Moderation (1=normal, 2=lowlatency)");
1202 
1203 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "debug",
1204 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0,
1205 	    em_sysctl_debug_info, "I", "Debug Information");
1206 
1207 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "rs_dump",
1208 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
1209 	    em_get_rs, "I", "Dump RS indexes");
1210 
1211 	if (sc->vf_ifp) {
1212 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "reg_dump",
1213 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
1214 		    igbv_get_regs, "A", "Dump VF registers");
1215 		return;
1216 	}
1217 
1218 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "nvm",
1219 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0,
1220 	    em_sysctl_nvm_info, "I", "NVM Information");
1221 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "fw_version",
1222 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
1223 	    em_sysctl_print_fw_version, "A",
1224 	    "Prints FW/NVM Versions");
1225 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "fc",
1226 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
1227 	    em_set_flowcntl, "I", "Flow Control");
1228 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "reg_dump",
1229 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
1230 	    em_get_regs, "A", "Dump Registers");
1231 
1232 	if (hw->mac.type >= e1000_i350 && hw->mac.type != e1000_i211) {
1233 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "dmac",
1234 		    CTLTYPE_INT | CTLFLAG_RW, sc, 0,
1235 		    igb_sysctl_dmac, "I", "DMA Coalesce");
1236 	}
1237 
1238 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO,
1239 	    "tso_tcp_flags_mask_first_segment",
1240 	    CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1241 	    sc, 0, em_sysctl_tso_tcp_flags_mask, "IU",
1242 	    "TSO TCP flags mask for first segment");
1243 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO,
1244 	    "tso_tcp_flags_mask_middle_segment",
1245 	    CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1246 	    sc, 1, em_sysctl_tso_tcp_flags_mask, "IU",
1247 	    "TSO TCP flags mask for middle segment");
1248 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO,
1249 	    "tso_tcp_flags_mask_last_segment",
1250 	    CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1251 	    sc, 2, em_sysctl_tso_tcp_flags_mask, "IU",
1252 	    "TSO TCP flags mask for last segment");
1253 }
1254 
1255 /*********************************************************************
1256  *  Device initialization routine
1257  *
1258  *  The attach entry point is called when the driver is being loaded.
1259  *  This routine identifies the type of hardware, allocates all resources
1260  *  and initializes the hardware.
1261  *
1262  *  return 0 on success, positive on failure
1263  *********************************************************************/
1264 int
em_if_attach_pre(if_ctx_t ctx)1265 em_if_attach_pre(if_ctx_t ctx)
1266 {
1267 	struct e1000_softc *sc;
1268 	if_softc_ctx_t scctx;
1269 	device_t dev;
1270 	struct e1000_hw *hw;
1271 	struct sysctl_oid_list *child;
1272 	struct sysctl_ctx_list *ctx_list;
1273 	int error = 0;
1274 
1275 	INIT_DEBUGOUT("em_if_attach_pre: begin");
1276 	dev = iflib_get_dev(ctx);
1277 	sc = iflib_get_softc(ctx);
1278 
1279 	if (em_max_interrupt_rate <= 0) {
1280 		device_printf(dev,
1281 		    "Invalid max_interrupt_rate %d; using default %d\n",
1282 		    em_max_interrupt_rate, EM_INTS_DEFAULT);
1283 		em_max_interrupt_rate = EM_INTS_DEFAULT;
1284 	}
1285 
1286 	sc->ctx = sc->osdep.ctx = ctx;
1287 	sc->dev = sc->osdep.dev = dev;
1288 	scctx = sc->shared = iflib_get_softc_ctx(ctx);
1289 	sc->media = iflib_get_media(ctx);
1290 	hw = &sc->hw;
1291 	sc->vf_ifp =
1292 	    (iflib_get_sctx(ctx)->isc_flags & IFLIB_IS_VF) != 0;
1293 	sc->osdep.vf = sc->vf_ifp;
1294 
1295 	/* Determine hardware and mac info */
1296 	em_identify_hardware(ctx);
1297 	sc->osdep.vf_82576 = sc->hw.mac.type == e1000_vfadapt;
1298 
1299 	/* VF sysctls are deferred until attach-post confirms MSI-X. */
1300 	ctx_list = device_get_sysctl_ctx(dev);
1301 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
1302 	if (!sc->vf_ifp)
1303 		em_add_device_sysctls(sc);
1304 
1305 	scctx->isc_tx_nsegments = EM_MAX_SCATTER;
1306 	scctx->isc_nrxqsets_max =
1307 	    scctx->isc_ntxqsets_max = em_set_num_queues(ctx);
1308 	if (bootverbose)
1309 		device_printf(dev, "attach_pre capping queues at %d\n",
1310 		    scctx->isc_ntxqsets_max);
1311 
1312 	if (hw->mac.type >= igb_mac_min) {
1313 		scctx->isc_txqsizes[0] = roundup2(scctx->isc_ntxd[0] *
1314 		    sizeof(union e1000_adv_tx_desc), EM_DBA_ALIGN);
1315 		scctx->isc_rxqsizes[0] = roundup2(scctx->isc_nrxd[0] *
1316 		    sizeof(union e1000_adv_rx_desc), EM_DBA_ALIGN);
1317 		scctx->isc_txd_size[0] = sizeof(union e1000_adv_tx_desc);
1318 		scctx->isc_rxd_size[0] = sizeof(union e1000_adv_rx_desc);
1319 		scctx->isc_txrx = &igb_txrx;
1320 		scctx->isc_tx_tso_segments_max = EM_MAX_SCATTER;
1321 		scctx->isc_tx_tso_size_max = EM_TSO_SIZE;
1322 		scctx->isc_tx_tso_segsize_max = EM_TSO_SEG_SIZE;
1323 		scctx->isc_capabilities = scctx->isc_capenable =
1324 		    sc->vf_ifp ? IGBV_CAPS : IGB_CAPS;
1325 		scctx->isc_tx_csum_flags = CSUM_TCP | CSUM_UDP | CSUM_TSO |
1326 		     CSUM_IP6_TCP | CSUM_IP6_UDP;
1327 		if (hw->mac.type != e1000_82575)
1328 			scctx->isc_tx_csum_flags |= CSUM_SCTP | CSUM_IP6_SCTP;
1329 		/*
1330 		** Some new devices, as with ixgbe, now may
1331 		** use a different BAR, so we need to keep
1332 		** track of which is used.
1333 		*/
1334 		scctx->isc_msix_bar = pci_msix_table_bar(dev);
1335 	} else if (hw->mac.type >= em_mac_min) {
1336 		scctx->isc_txqsizes[0] = roundup2(scctx->isc_ntxd[0] *
1337 		    sizeof(struct e1000_tx_desc), EM_DBA_ALIGN);
1338 		scctx->isc_rxqsizes[0] = roundup2(scctx->isc_nrxd[0] *
1339 		    sizeof(union e1000_rx_desc_extended), EM_DBA_ALIGN);
1340 		scctx->isc_txd_size[0] = sizeof(struct e1000_tx_desc);
1341 		scctx->isc_rxd_size[0] = sizeof(union e1000_rx_desc_extended);
1342 		scctx->isc_txrx = &em_txrx;
1343 		scctx->isc_tx_tso_segments_max = EM_MAX_SCATTER;
1344 		scctx->isc_tx_tso_size_max = EM_TSO_SIZE;
1345 		scctx->isc_tx_tso_segsize_max = EM_TSO_SEG_SIZE;
1346 		scctx->isc_capabilities = scctx->isc_capenable = EM_CAPS;
1347 		scctx->isc_tx_csum_flags = CSUM_TCP | CSUM_UDP | CSUM_IP_TSO |
1348 		    CSUM_IP6_TCP | CSUM_IP6_UDP;
1349 
1350 		/* Disable TSO on all em(4) until ring stalls are debugged */
1351 		scctx->isc_capenable &= ~IFCAP_TSO;
1352 
1353 		/*
1354 		 * Disable TSO on SPT due to errata that downclocks DMA
1355 		 * performance
1356 		 * i218-i219 Specification Update 1.5.4.5
1357 		 */
1358 		if (hw->mac.type == e1000_pch_spt)
1359 			scctx->isc_capenable &= ~IFCAP_TSO;
1360 
1361 		/*
1362 		 * We support MSI-X with 82574 only, but indicate to iflib(4)
1363 		 * that it shall give MSI at least a try with other devices.
1364 		 */
1365 		if (hw->mac.type == e1000_82574) {
1366 			scctx->isc_msix_bar = pci_msix_table_bar(dev);
1367 		} else {
1368 			scctx->isc_msix_bar = -1;
1369 			scctx->isc_disable_msix = 1;
1370 		}
1371 	} else {
1372 		scctx->isc_txqsizes[0] = roundup2((scctx->isc_ntxd[0] + 1) *
1373 		    sizeof(struct e1000_tx_desc), EM_DBA_ALIGN);
1374 		scctx->isc_rxqsizes[0] = roundup2((scctx->isc_nrxd[0] + 1) *
1375 		    sizeof(struct e1000_rx_desc), EM_DBA_ALIGN);
1376 		scctx->isc_txd_size[0] = sizeof(struct e1000_tx_desc);
1377 		scctx->isc_rxd_size[0] = sizeof(struct e1000_rx_desc);
1378 		scctx->isc_txrx = &lem_txrx;
1379 		scctx->isc_tx_tso_segments_max = EM_MAX_SCATTER;
1380 		scctx->isc_tx_tso_size_max = EM_TSO_SIZE;
1381 		scctx->isc_tx_tso_segsize_max = EM_TSO_SEG_SIZE;
1382 		scctx->isc_capabilities = scctx->isc_capenable = LEM_CAPS;
1383 		if (em_unsupported_tso)
1384 			scctx->isc_capabilities |= IFCAP_TSO6;
1385 		scctx->isc_tx_csum_flags = CSUM_TCP | CSUM_UDP | CSUM_IP_TSO |
1386 		    CSUM_IP6_TCP | CSUM_IP6_UDP;
1387 
1388 		/* Disable TSO on all lem(4) until ring stalls debugged */
1389 		scctx->isc_capenable &= ~IFCAP_TSO;
1390 
1391 		/* 82541ER doesn't do HW tagging */
1392 		if (hw->device_id == E1000_DEV_ID_82541ER ||
1393 		    hw->device_id == E1000_DEV_ID_82541ER_LOM) {
1394 			scctx->isc_capabilities &= ~IFCAP_VLAN_HWTAGGING;
1395 			scctx->isc_capenable = scctx->isc_capabilities;
1396 		}
1397 		/* This is the first e1000 chip and it does not do offloads */
1398 		if (hw->mac.type == e1000_82542) {
1399 			scctx->isc_capabilities &= ~(IFCAP_HWCSUM |
1400 			    IFCAP_VLAN_HWCSUM | IFCAP_HWCSUM_IPV6 |
1401 			    IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWFILTER |
1402 			    IFCAP_TSO | IFCAP_VLAN_HWTSO);
1403 			scctx->isc_capenable = scctx->isc_capabilities;
1404 		}
1405 		/* These can't do TSO for various reasons */
1406 		if (hw->mac.type < e1000_82544 ||
1407 		    hw->mac.type == e1000_82547 ||
1408 		    hw->mac.type == e1000_82547_rev_2) {
1409 			scctx->isc_capabilities &=
1410 			    ~(IFCAP_TSO |IFCAP_VLAN_HWTSO);
1411 			scctx->isc_capenable = scctx->isc_capabilities;
1412 		}
1413 		/* XXXKB: No IPv6 before this? */
1414 		if (hw->mac.type < e1000_82545){
1415 			scctx->isc_capabilities &= ~IFCAP_HWCSUM_IPV6;
1416 			scctx->isc_capenable = scctx->isc_capabilities;
1417 		}
1418 		/*
1419 		 * "PCI/PCI-X SDM 4.0" page 33 (b):
1420 		 * FDX requirement on these chips
1421 		 */
1422 		if (hw->mac.type == e1000_82547 ||
1423 		    hw->mac.type == e1000_82547_rev_2)
1424 			scctx->isc_capenable &= ~(IFCAP_HWCSUM |
1425 			    IFCAP_VLAN_HWCSUM | IFCAP_HWCSUM_IPV6);
1426 
1427 		/* INTx only */
1428 		scctx->isc_msix_bar = 0;
1429 	}
1430 
1431 	/* Setup PCI resources */
1432 	if (em_allocate_pci_resources(ctx)) {
1433 		device_printf(dev, "Allocation of PCI resources failed\n");
1434 		error = ENXIO;
1435 		goto err_pci;
1436 	}
1437 	/*
1438 	 * A VF can retain queue enable bits and DMA addresses across VFLR.
1439 	 * Fence bus mastering before the first mailbox reset so state left by
1440 	 * a previous owner cannot issue DMA while the driver attaches.
1441 	 */
1442 	if (sc->vf_ifp)
1443 		em_fence_pci_busmaster(sc);
1444 	/*
1445 	 * 82579 can lose a host CSR write while the Management Engine owns
1446 	 * the PCIm2PCI arbiter.  Enable the OS register write interlock before
1447 	 * shared code initialization performs any MAC writes.
1448 	 */
1449 	if (hw->mac.type == e1000_pch2lan &&
1450 	    (E1000_READ_REG(hw, E1000_FWSM) &
1451 	    E1000_ICH_FWSM_FW_VALID) != 0)
1452 		sc->osdep.pcim2pci_arbiter_wa = true;
1453 
1454 	/*
1455 	** For ICH8 and family we need to
1456 	** map the flash memory, and this
1457 	** must happen after the MAC is
1458 	** identified
1459 	*/
1460 	if ((hw->mac.type == e1000_ich8lan) ||
1461 	    (hw->mac.type == e1000_ich9lan) ||
1462 	    (hw->mac.type == e1000_ich10lan) ||
1463 	    (hw->mac.type == e1000_pchlan) ||
1464 	    (hw->mac.type == e1000_pch2lan) ||
1465 	    (hw->mac.type == e1000_pch_lpt)) {
1466 		int rid = EM_BAR_TYPE_FLASH;
1467 		sc->flash = bus_alloc_resource_any(dev,
1468 		    SYS_RES_MEMORY, &rid, RF_ACTIVE);
1469 		if (sc->flash == NULL) {
1470 			device_printf(dev, "Mapping of Flash failed\n");
1471 			error = ENXIO;
1472 			goto err_pci;
1473 		}
1474 		/* This is used in the shared code */
1475 		hw->flash_address = (u8 *)sc->flash;
1476 		sc->osdep.flash_bus_space_tag =
1477 		    rman_get_bustag(sc->flash);
1478 		sc->osdep.flash_bus_space_handle =
1479 		    rman_get_bushandle(sc->flash);
1480 	}
1481 	/*
1482 	** In the new SPT device flash is not  a
1483 	** separate BAR, rather it is also in BAR0,
1484 	** so use the same tag and an offset handle for the
1485 	** FLASH read/write macros in the shared code.
1486 	*/
1487 	else if (hw->mac.type >= e1000_pch_spt) {
1488 		sc->osdep.flash_bus_space_tag = sc->osdep.mem_bus_space_tag;
1489 		sc->osdep.flash_bus_space_handle =
1490 		    sc->osdep.mem_bus_space_handle + E1000_FLASH_BASE_ADDR;
1491 	}
1492 
1493 	/* Do Shared Code initialization */
1494 	error = e1000_setup_init_funcs(hw, true);
1495 	if (error) {
1496 		device_printf(dev, "Setup of Shared code failed, error %d\n",
1497 		    error);
1498 		error = ENXIO;
1499 		goto err_pci;
1500 	}
1501 
1502 	em_setup_msix(ctx);
1503 	e1000_get_bus_info(hw);
1504 
1505 	/*
1506 	 * Some conventional PCI systems hang when e1000 devices use
1507 	 * DMA addresses above 4 GB.  Keep PCI-mode DMA below that boundary
1508 	 * by default; PCI-X and PCIe retain 64-bit DMA.
1509 	 */
1510 	if (hw->bus.type == e1000_bus_type_pci) {
1511 		SYSCTL_ADD_BOOL(ctx_list, child, OID_AUTO, "allow_64bit_dma",
1512 		    CTLFLAG_RDTUN, &sc->allow_64bit_dma, 0,
1513 		    "Allow 64-bit DMA in conventional PCI mode");
1514 		if (sc->allow_64bit_dma)
1515 			device_printf(dev, "64-bit DMA in conventional PCI mode.  "
1516 			    "Some chipsets are unstable.\n");
1517 		else {
1518 			scctx->isc_dma_width = 32;
1519 			device_printf(dev, "32-bit DMA in conventional PCI mode.  "
1520 			    "Set dev.%s.%d.allow_64bit_dma=1 at boot to enable "
1521 			    "64-bit DMA if the chipset is stable with it.\n",
1522 			    device_get_name(dev), device_get_unit(dev));
1523 		}
1524 	}
1525 
1526 	/* Set up some sysctls for the tunable interrupt delays */
1527 	if (hw->mac.type < igb_mac_min) {
1528 		em_add_int_delay_sysctl(sc, "rx_int_delay",
1529 		    "receive interrupt delay in usecs", &sc->rx_int_delay,
1530 		    E1000_REGISTER(hw, E1000_RDTR), em_rx_int_delay_dflt);
1531 		em_add_int_delay_sysctl(sc, "tx_int_delay",
1532 		    "transmit interrupt delay in usecs", &sc->tx_int_delay,
1533 		    E1000_REGISTER(hw, E1000_TIDV), em_tx_int_delay_dflt);
1534 	}
1535 	if (hw->mac.type >= e1000_82540 && hw->mac.type < igb_mac_min) {
1536 		em_add_int_delay_sysctl(sc, "rx_abs_int_delay",
1537 		    "receive interrupt delay limit in usecs",
1538 		    &sc->rx_abs_int_delay,
1539 		    E1000_REGISTER(hw, E1000_RADV), em_rx_abs_int_delay_dflt);
1540 		em_add_int_delay_sysctl(sc, "tx_abs_int_delay",
1541 		    "transmit interrupt delay limit in usecs",
1542 		    &sc->tx_abs_int_delay,
1543 		    E1000_REGISTER(hw, E1000_TADV), em_tx_abs_int_delay_dflt);
1544 	}
1545 
1546 	hw->mac.autoneg = DO_AUTO_NEG;
1547 	hw->phy.autoneg_wait_to_complete = false;
1548 	hw->phy.autoneg_advertised = AUTONEG_ADV_DEFAULT;
1549 
1550 	if (hw->mac.type < em_mac_min) {
1551 		e1000_init_script_state_82541(hw, true);
1552 		e1000_set_tbi_compatibility_82543(hw, true);
1553 	}
1554 	/* Copper options */
1555 	if (hw->phy.media_type == e1000_media_type_copper) {
1556 		hw->phy.mdix = AUTO_ALL_MODES;
1557 		hw->phy.disable_polarity_correction = false;
1558 		hw->phy.ms_type = EM_MASTER_SLAVE;
1559 	}
1560 
1561 	/*
1562 	 * Set the frame limits assuming
1563 	 * standard ethernet sized frames.
1564 	 */
1565 	scctx->isc_max_frame_size = hw->mac.max_frame_size =
1566 	    ETHERMTU + ETHER_HDR_LEN + ETHERNET_FCS_SIZE;
1567 
1568 	/*
1569 	 * This controls when hardware reports transmit completion
1570 	 * status.
1571 	 */
1572 	hw->mac.report_tx_early = 1;
1573 
1574 	/* Allocate multicast array memory. */
1575 	sc->mta = malloc(sizeof(u8) * ETHER_ADDR_LEN *
1576 	    MAX_NUM_MULTICAST_ADDRESSES, M_DEVBUF, M_NOWAIT);
1577 	if (sc->mta == NULL) {
1578 		device_printf(dev,
1579 		    "Can not allocate multicast setup array\n");
1580 		error = ENOMEM;
1581 		goto err_late;
1582 	}
1583 
1584 	/* Clear the IFCAP_TSO auto mask */
1585 	sc->tso_automasked = 0;
1586 
1587 	/* Check SOL/IDER usage on physical functions. */
1588 	if (!sc->vf_ifp && e1000_check_reset_block(hw))
1589 		device_printf(dev,
1590 		    "PHY reset is blocked due to SOL/IDER session.\n");
1591 
1592 	/* Sysctl for setting Energy Efficient Ethernet */
1593 	if (!sc->vf_ifp) {
1594 		if (hw->mac.type < igb_mac_min)
1595 			hw->dev_spec.ich8lan.eee_disable = eee_setting;
1596 		else
1597 			hw->dev_spec._82575.eee_disable = eee_setting;
1598 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "eee_control",
1599 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
1600 		    em_sysctl_eee, "I", "Disable Energy Efficient Ethernet");
1601 	}
1602 
1603 	/*
1604 	** Start from a known state, this is
1605 	** important in reading the nvm and
1606 	** mac from that.
1607 	*/
1608 	error = e1000_reset_hw(hw);
1609 	if (sc->vf_ifp) {
1610 		atomic_store_rel_32(&sc->vf_mbx_ready,
1611 		    error == E1000_SUCCESS);
1612 		if (error != E1000_SUCCESS)
1613 			igbv_log_reset_failure(sc, error, true);
1614 		sc->vf_queues_sanitized = igbv_sanitize_queues(sc);
1615 	} else if (error != E1000_SUCCESS) {
1616 		device_printf(dev, "Hardware reset failed: %d\n", error);
1617 		error = EIO;
1618 		goto err_late;
1619 	}
1620 
1621 	/* Make sure a PF has a good EEPROM before we read from it. */
1622 	if (!sc->vf_ifp && e1000_validate_nvm_checksum(hw) < 0) {
1623 		/*
1624 		** Some PCI-E parts fail the first check due to
1625 		** the link being in sleep state, call it again,
1626 		** if it fails a second time its a real issue.
1627 		*/
1628 		if (e1000_validate_nvm_checksum(hw) < 0) {
1629 			device_printf(dev,
1630 			    "The EEPROM Checksum Is Not Valid\n");
1631 			error = EIO;
1632 			goto err_late;
1633 		}
1634 	}
1635 
1636 	/* Copy the permanent MAC address out of the EEPROM */
1637 	if (e1000_read_mac_addr(hw) < 0) {
1638 		device_printf(dev,
1639 		    "EEPROM read error while reading MAC address\n");
1640 		error = EIO;
1641 		goto err_late;
1642 	}
1643 
1644 	if (!em_is_valid_ether_addr(hw->mac.addr)) {
1645 		if (sc->vf_ifp) {
1646 			device_printf(dev,
1647 			    "PF did not assign a MAC address; using a "
1648 			    "locally generated address\n");
1649 			ether_gen_addr(iflib_get_ifp(ctx),
1650 			    (struct ether_addr *)hw->mac.addr);
1651 		} else {
1652 			device_printf(dev, "Invalid MAC address\n");
1653 			error = EIO;
1654 			goto err_late;
1655 		}
1656 	}
1657 
1658 	if (!sc->vf_ifp) {
1659 		/* Save NVM versions while holding the IFLIB context lock. */
1660 		em_fw_version_locked(ctx);
1661 		em_print_fw_version(sc);
1662 	}
1663 
1664 	/*
1665 	 * Get Wake-on-Lan and Management info for later use
1666 	 */
1667 	if (!sc->vf_ifp) {
1668 		em_get_wakeup(ctx);
1669 	}
1670 
1671 	iflib_set_mac(ctx, hw->mac.addr);
1672 
1673 	return (0);
1674 
1675 err_late:
1676 	em_release_hw_control(sc);
1677 err_pci:
1678 	em_free_pci_resources(ctx);
1679 	free(sc->mta, M_DEVBUF);
1680 	sc->mta = NULL;
1681 
1682 	return (error);
1683 }
1684 
1685 int
em_if_attach_post(if_ctx_t ctx)1686 em_if_attach_post(if_ctx_t ctx)
1687 {
1688 	struct e1000_softc *sc = iflib_get_softc(ctx);
1689 	struct e1000_hw *hw = &sc->hw;
1690 	int error = 0;
1691 
1692 	/* Setup OS specific network interface */
1693 	error = em_setup_interface(ctx);
1694 	if (error != 0) {
1695 		device_printf(sc->dev, "Interface setup failed: %d\n", error);
1696 		goto err_late;
1697 	}
1698 
1699 	if (sc->vf_ifp) {
1700 		(void)igbv_reset(ctx);
1701 	} else if (em_reset(ctx) != E1000_SUCCESS) {
1702 		error = EIO;
1703 		goto err_late;
1704 	}
1705 
1706 	/* Initialize statistics */
1707 	if (sc->vf_ifp)
1708 		em_initialize_vf_stats(sc);
1709 	else
1710 		sc->ustats.stats = (struct e1000_hw_stats){};
1711 
1712 	em_update_stats_counters(sc);
1713 	atomic_readandclear_32(&sc->stats_pending);
1714 	hw->mac.get_link_status = 1;
1715 	if (sc->vf_ifp)
1716 		igbv_if_update_admin_status(ctx);
1717 	else
1718 		em_if_update_admin_status(ctx);
1719 	em_add_hw_stats(sc);
1720 
1721 	/* Non-AMT based hardware can now take control from firmware */
1722 	if (sc->has_manage && !sc->has_amt)
1723 		em_get_hw_control(sc);
1724 
1725 	INIT_DEBUGOUT("em_if_attach_post: end");
1726 
1727 	return (0);
1728 
1729 err_late:
1730 	/*
1731 	 * Upon em_if_attach_post() error, iflib calls em_if_detach() to
1732 	 * free resources
1733 	 */
1734 	return (error);
1735 }
1736 
1737 /*********************************************************************
1738  *  Device removal routine
1739  *
1740  *  The detach entry point is called when the driver is being removed.
1741  *  This routine stops the adapter and deallocates all the resources
1742  *  that were allocated for driver operation.
1743  *
1744  *  return 0 on success, positive on failure
1745  *********************************************************************/
1746 static int
em_if_detach(if_ctx_t ctx)1747 em_if_detach(if_ctx_t ctx)
1748 {
1749 	struct e1000_softc	*sc = iflib_get_softc(ctx);
1750 
1751 	INIT_DEBUGOUT("em_if_detach: begin");
1752 
1753 	igb_iov_detach(sc);
1754 	if (sc->vf_ifp) {
1755 		igbv_queue_retry_detach(sc);
1756 		igbv_mbx_retry_detach(sc);
1757 	} else {
1758 		e1000_phy_hw_reset(&sc->hw);
1759 	}
1760 
1761 	em_release_manageability(sc);
1762 	em_release_hw_control(sc);
1763 	em_free_pci_resources(ctx);
1764 	free(sc->mta, M_DEVBUF);
1765 	sc->mta = NULL;
1766 
1767 	return (0);
1768 }
1769 
1770 /*********************************************************************
1771  *
1772  *  Shutdown entry point
1773  *
1774  **********************************************************************/
1775 
1776 static int
em_if_shutdown(if_ctx_t ctx)1777 em_if_shutdown(if_ctx_t ctx)
1778 {
1779 	int error;
1780 
1781 	error = em_if_suspend(ctx);
1782 	if (error != 0)
1783 		device_printf(iflib_get_dev(ctx),
1784 		    "Wake configuration failed during shutdown: %d\n", error);
1785 	return (0);
1786 }
1787 
1788 /*
1789  * Suspend/resume device methods.
1790  */
1791 static int
em_if_suspend(if_ctx_t ctx)1792 em_if_suspend(if_ctx_t ctx)
1793 {
1794 	struct e1000_softc *sc = iflib_get_softc(ctx);
1795 	int error;
1796 
1797 	if (sc->vf_ifp) {
1798 		igbv_queue_retry_stop(sc);
1799 		igbv_mbx_retry_stop(sc);
1800 	}
1801 	error = em_enable_wakeup(ctx);
1802 	em_release_manageability(sc);
1803 	em_release_hw_control(sc);
1804 	return (error);
1805 }
1806 
1807 static int
em_if_resume(if_ctx_t ctx)1808 em_if_resume(if_ctx_t ctx)
1809 {
1810 	struct e1000_softc *sc = iflib_get_softc(ctx);
1811 	u32 wus;
1812 	u16 phy_wus;
1813 	int error;
1814 
1815 	if (sc->hw.mac.type >= e1000_pch2lan &&
1816 	    sc->hw.mac.type < igb_mac_min)
1817 		e1000_resume_workarounds_pchlan(&sc->hw);
1818 
1819 	if (sc->wol_phy_armed) {
1820 		/*
1821 		 * The PHY wake sequence requires an LCD reset before host wake
1822 		 * ownership is cleared.  Wake registers survive this reset.
1823 		 */
1824 		(void)e1000_phy_hw_reset(&sc->hw);
1825 		error = em_disable_phy_wakeup(sc, &phy_wus);
1826 		if (error != E1000_SUCCESS)
1827 			device_printf(sc->dev,
1828 			    "Could not clear PHY wakeup state: %d\n", error);
1829 		else if (phy_wus != 0)
1830 			device_printf(sc->dev, "PHY wakeup status: %#06x\n",
1831 			    phy_wus);
1832 	}
1833 	if (!sc->vf_ifp && sc->hw.mac.type >= e1000_82544) {
1834 		wus = E1000_READ_REG(&sc->hw, E1000_WUS);
1835 		if (!sc->wol_phy_wakeup && wus != 0)
1836 			device_printf(sc->dev, "MAC wakeup status: %#010x\n",
1837 			    wus);
1838 		E1000_WRITE_REG(&sc->hw, E1000_WUFC, 0);
1839 		E1000_WRITE_REG(&sc->hw, E1000_WUC, 0);
1840 		E1000_WRITE_REG(&sc->hw, E1000_WUS, ~0U);
1841 	}
1842 	/* Clear PME after its MAC or PHY wake source has been removed. */
1843 	pci_clear_pme(sc->dev);
1844 
1845 	return (0);
1846 }
1847 
1848 static int
em_if_mtu_set(if_ctx_t ctx,uint32_t mtu)1849 em_if_mtu_set(if_ctx_t ctx, uint32_t mtu)
1850 {
1851 	int max_frame_size;
1852 	struct e1000_softc *sc = iflib_get_softc(ctx);
1853 	if_softc_ctx_t scctx = iflib_get_softc_ctx(ctx);
1854 
1855 	IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFMTU (Set Interface MTU)");
1856 
1857 	switch (sc->hw.mac.type) {
1858 	case e1000_82571:
1859 	case e1000_82572:
1860 	case e1000_ich9lan:
1861 	case e1000_ich10lan:
1862 	case e1000_pch2lan:
1863 	case e1000_pch_lpt:
1864 	case e1000_pch_spt:
1865 	case e1000_pch_cnp:
1866 	case e1000_pch_tgp:
1867 	case e1000_pch_adp:
1868 	case e1000_pch_mtp:
1869 	case e1000_pch_ptp:
1870 	case e1000_pch_nvp:
1871 	case e1000_82574:
1872 	case e1000_82583:
1873 	case e1000_80003es2lan:
1874 		/* 9K Jumbo Frame size */
1875 		max_frame_size = 9234;
1876 		break;
1877 	case e1000_pchlan:
1878 		max_frame_size = 4096;
1879 		break;
1880 	case e1000_82542:
1881 	case e1000_ich8lan:
1882 		/* Adapters that do not support jumbo frames */
1883 		max_frame_size = ETHER_MAX_LEN;
1884 		break;
1885 	default:
1886 		if (sc->hw.mac.type >= igb_mac_min)
1887 			max_frame_size = IGB_MAX_FRAME_SIZE;
1888 		else /* lem */
1889 			max_frame_size = MAX_JUMBO_FRAME_SIZE;
1890 	}
1891 	if (mtu > max_frame_size - ETHER_HDR_LEN - ETHER_CRC_LEN) {
1892 		return (EINVAL);
1893 	}
1894 
1895 	scctx->isc_max_frame_size = sc->hw.mac.max_frame_size =
1896 	    mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
1897 	return (0);
1898 }
1899 
1900 /*********************************************************************
1901  *  Init entry point
1902  *
1903  *  This routine is used in two ways. It is used by the stack as
1904  *  init entry point in network interface structure. It is also used
1905  *  by the driver as a hw/sw initialization routine to get to a
1906  *  consistent state.
1907  *
1908  **********************************************************************/
1909 static void
em_if_init(if_ctx_t ctx)1910 em_if_init(if_ctx_t ctx)
1911 {
1912 	struct e1000_softc *sc = iflib_get_softc(ctx);
1913 	if_softc_ctx_t scctx = sc->shared;
1914 	if_t ifp = iflib_get_ifp(ctx);
1915 	struct em_tx_queue *tx_que;
1916 	int i;
1917 
1918 	INIT_DEBUGOUT("em_if_init: begin");
1919 	if (sc->vf_ifp) {
1920 		igbv_queue_retry_prepare(sc);
1921 		igbv_mbx_retry_prepare(sc);
1922 		sc->vf_reset_pending = true;
1923 	}
1924 	if (sc->suspend_link_powered_down)
1925 		em_power_up_wakeup_link(sc);
1926 
1927 	/* Get the latest mac address, User can use a LAA */
1928 	bcopy(if_getlladdr(ifp), sc->hw.mac.addr, ETHER_ADDR_LEN);
1929 
1930 	/*
1931 	 * A VF restores its address only after its reset handshake establishes
1932 	 * CTS.  The PF path programs RAR[0] directly here.
1933 	 */
1934 	if (!sc->vf_ifp)
1935 		e1000_rar_set(&sc->hw, sc->hw.mac.addr, 0);
1936 
1937 	/*
1938 	 * With the 82571 adapter, RAR[0] may be overwritten
1939 	 * when the other port is reset, we make a duplicate
1940 	 * in RAR[14] for that eventuality, this assures
1941 	 * the interface continues to function.
1942 	 */
1943 	if (sc->hw.mac.type == e1000_82571) {
1944 		e1000_set_laa_state_82571(&sc->hw, true);
1945 		e1000_rar_set(&sc->hw, sc->hw.mac.addr,
1946 		    E1000_RAR_ENTRIES - 1);
1947 	}
1948 
1949 	/* Initialize the hardware */
1950 	igb_iov_reset_prepare(sc);
1951 	if (sc->vf_ifp) {
1952 		(void)igbv_reset(ctx);
1953 		em_rebase_vf_stats(sc);
1954 	} else if (em_reset(ctx) != E1000_SUCCESS) {
1955 		iflib_init_failed(ctx);
1956 		return;
1957 	}
1958 	if (sc->vf_ifp && !sc->vf_queues_sanitized) {
1959 		/*
1960 		 * Do not program or enable rings while retained queue state
1961 		 * might still contain a previous VF owner's DMA address.  A
1962 		 * bounded callout retries initialization after iflib leaves the
1963 		 * failed initialization stopped.
1964 		 */
1965 		em_fence_pci_busmaster(sc);
1966 		igbv_queue_retry_failed(ctx);
1967 		return;
1968 	}
1969 	if (sc->vf_ifp &&
1970 	    atomic_load_acq_32(&sc->vf_mbx_ready) == 0) {
1971 		igbv_mbx_retry_failed(ctx);
1972 		return;
1973 	}
1974 	/*
1975 	 * Keep a fail-closed device fenced until reset and VF queue
1976 	 * sanitization have removed every stale DMA address.
1977 	 */
1978 	if (sc->vf_ifp && em_enable_pci_busmaster(sc) != 0) {
1979 		device_printf(sc->dev,
1980 		    "Unable to enable PCI bus mastering\n");
1981 		iflib_init_failed(ctx);
1982 		return;
1983 	}
1984 	if (sc->vf_ifp)
1985 		igbv_reconcile_mac(sc, ifp);
1986 	/* Re-arm a link-up transition deferred for this reset. */
1987 	if (sc->link_state == EM_LINK_STATE_DOWN_RESET_PENDING ||
1988 	    sc->link_state == EM_LINK_STATE_UP_RESET_PENDING)
1989 		sc->link_state = EM_LINK_STATE_DOWN;
1990 	if (sc->vf_ifp)
1991 		igbv_if_update_admin_status(ctx);
1992 	else
1993 		em_if_update_admin_status(ctx);
1994 
1995 	for (i = 0, tx_que = sc->tx_queues; i < sc->tx_num_queues;
1996 	    i++, tx_que++) {
1997 		struct tx_ring *txr = &tx_que->txr;
1998 
1999 		txr->tx_rs_cidx = txr->tx_rs_pidx;
2000 
2001 		/* Initialize the last processed descriptor to be the end of
2002 		 * the ring, rather than the start, so that we avoid an
2003 		 * off-by-one error when calculating how many descriptors are
2004 		 * done in the credits_update function.
2005 		 */
2006 		txr->tx_cidx_processed = scctx->isc_ntxd[0] - 1;
2007 	}
2008 
2009 	/* The VF VLAN EtherType is fixed and has no VET register. */
2010 	if (!sc->vf_ifp)
2011 		E1000_WRITE_REG(&sc->hw, E1000_VET, ETHERTYPE_VLAN);
2012 
2013 	/* Clear bad data from Rx FIFOs */
2014 	if (sc->hw.mac.type >= igb_mac_min && !sc->vf_ifp)
2015 		e1000_rx_fifo_flush_base(&sc->hw);
2016 
2017 	/* Configure for OS presence */
2018 	em_init_manageability(sc);
2019 
2020 	/* Prepare transmit descriptors and buffers */
2021 	if (sc->vf_ifp)
2022 		igbv_initialize_transmit_unit(ctx);
2023 	else
2024 		em_initialize_transmit_unit(ctx);
2025 
2026 	/*
2027 	 * A failed VF reset has no CTS channel on which to restore mailbox
2028 	 * state.  The reset detector schedules another complete init, which
2029 	 * replays these interface-owned lists after the handshake succeeds.
2030 	 */
2031 	em_if_multi_set(ctx);
2032 
2033 	sc->rx_mbuf_sz = iflib_get_rx_mbuf_sz(ctx);
2034 	if (sc->vf_ifp)
2035 		igbv_initialize_receive_unit(ctx);
2036 	else
2037 		em_initialize_receive_unit(ctx);
2038 
2039 	/* Set up VLAN support and filter. */
2040 	em_setup_vlan_hw_support(ctx);
2041 
2042 	/* Don't lose promiscuous settings. */
2043 	em_if_set_promisc_impl(ctx, if_getflags(ifp));
2044 	atomic_readandclear_32(&sc->promisc_pending);
2045 
2046 	/* Restore PF/VF pool configuration after the global reset. */
2047 	igb_iov_initialize(sc);
2048 
2049 	if (sc->hw.mac.ops.clear_hw_cntrs != NULL)
2050 		sc->hw.mac.ops.clear_hw_cntrs(&sc->hw);
2051 
2052 	/* MSI-X configuration for 82574 */
2053 	if (sc->hw.mac.type == e1000_82574) {
2054 		int tmp = E1000_READ_REG(&sc->hw, E1000_CTRL_EXT);
2055 
2056 		tmp |= E1000_CTRL_EXT_PBA_CLR;
2057 		E1000_WRITE_REG(&sc->hw, E1000_CTRL_EXT, tmp);
2058 		/* Set the IVAR - interrupt vector routing. */
2059 		E1000_WRITE_REG(&sc->hw, E1000_IVAR, sc->ivars);
2060 	} else if (sc->intr_type == IFLIB_INTR_MSIX) {
2061 		/* Set up queue routing */
2062 		igb_configure_queues(sc);
2063 	}
2064 	if (sc->hw.mac.type >= igb_mac_min)
2065 		igb_initialize_interrupt_rate(sc);
2066 
2067 	/* AMT based hardware can now take control from firmware */
2068 	if (sc->has_manage && sc->has_amt)
2069 		em_get_hw_control(sc);
2070 
2071 	/* Set Energy Efficient Ethernet */
2072 	if (sc->hw.mac.type >= igb_mac_min &&
2073 	    sc->hw.phy.media_type == e1000_media_type_copper) {
2074 		if (sc->hw.mac.type == e1000_i354)
2075 			e1000_set_eee_i354(&sc->hw, true, true);
2076 		else
2077 			e1000_set_eee_i350(&sc->hw, true, true);
2078 	}
2079 	em_configure_peind_memory_errors(sc);
2080 	em_configure_82575_memory_errors(sc);
2081 	em_configure_82580_memory_errors(sc);
2082 	if (sc->vf_ifp) {
2083 		sc->vf_reset_pending = false;
2084 	} else {
2085 		u32 icr;
2086 
2087 		/*
2088 		 * Drain stale causes only after register reconstruction is
2089 		 * complete.  DRSTA and DEV_RST_SET together close the window in
2090 		 * which another device reset can arrive while interrupts are
2091 		 * masked.
2092 		 */
2093 		icr = E1000_READ_REG(&sc->hw, E1000_ICR);
2094 		if (igb_finish_device_reset(sc, icr)) {
2095 			iflib_init_failed(ctx);
2096 			return;
2097 		}
2098 		E1000_WRITE_REG(&sc->hw, E1000_ICS, E1000_ICS_LSC);
2099 	}
2100 }
2101 
2102 /*
2103  * RX publishes its byte and packet counters as one snapshot when iflib
2104  * returns descriptors to hardware.  This also covers watchdog-driven RX
2105  * processing, which can run while the interrupt vector is unmasked.
2106  */
2107 static __inline void
em_aim_rx_delta(struct rx_ring * rxr,u32 * bytes,u32 * packets)2108 em_aim_rx_delta(struct rx_ring *rxr, u32 *bytes, u32 *packets)
2109 {
2110 	uint64_t snapshot;
2111 	u32 now_bytes, now_packets;
2112 
2113 	snapshot = atomic_load_acq_64(&rxr->rx_aim_snapshot);
2114 	now_bytes = snapshot >> 32;
2115 	now_packets = (u32)snapshot;
2116 	*bytes = now_bytes - rxr->rx_bytes_last;
2117 	*packets = now_packets - rxr->rx_packets_last;
2118 	rxr->rx_bytes_last = now_bytes;
2119 	rxr->rx_packets_last = now_packets;
2120 }
2121 
2122 /*
2123  * TX publishes its byte and packet counters as one snapshot at the doorbell,
2124  * because encapsulation can overlap the interrupt filter.  The two halves
2125  * remain independent free running u32 counters, so their deltas are correct
2126  * across wrap.
2127  */
2128 static __inline void
em_aim_tx_delta(struct tx_ring * txr,u32 * bytes,u32 * packets)2129 em_aim_tx_delta(struct tx_ring *txr, u32 *bytes, u32 *packets)
2130 {
2131 	uint64_t snapshot;
2132 	u32 now_bytes, now_packets;
2133 
2134 	snapshot = atomic_load_acq_64(&txr->tx_aim_snapshot);
2135 	now_bytes = snapshot >> 32;
2136 	now_packets = (u32)snapshot;
2137 	*bytes = now_bytes - txr->tx_bytes_last;
2138 	*packets = now_packets - txr->tx_packets_last;
2139 	txr->tx_bytes_last = now_bytes;
2140 	txr->tx_packets_last = now_packets;
2141 }
2142 
2143 /*********************************************************************
2144  *
2145  *  Do Adaptive Interrupt Moderation:
2146  *    - Calculate based on average size over the last interval
2147  *
2148  *  Returns interrupts per second rather than a register value, so that the
2149  *  caller's EM_INTS_TO_ITR()/IGB_INTS_TO_EITR() conversion applies, or zero
2150  *  if the interval carried no packet to measure.
2151  *
2152  *********************************************************************/
2153 static u32
em_ring_itr(struct e1000_softc * sc,u32 rxbytes,u32 rxpackets,u32 txbytes,u32 txpackets)2154 em_ring_itr(struct e1000_softc *sc, u32 rxbytes, u32 rxpackets, u32 txbytes,
2155     u32 txpackets)
2156 {
2157 	u32 newitr = 0;
2158 
2159 	if (txbytes && txpackets)
2160 		newitr = txbytes / txpackets;
2161 	if (rxbytes && rxpackets)
2162 		newitr = max(newitr, rxbytes / rxpackets);
2163 
2164 	/*
2165 	 * No packet was observed, so there is no size to work from.  Report no
2166 	 * observation and let the caller keep the rate it already has.
2167 	 */
2168 	if (newitr == 0)
2169 		return (0);
2170 
2171 	newitr += 24; /* account for hardware frame, crc */
2172 	/* set an upper boundary */
2173 	newitr = min(newitr, 3000);
2174 	/* Be nice to the mid range */
2175 	if ((newitr > 300) && (newitr < 1200))
2176 		newitr = (newitr / 3);
2177 	else
2178 		newitr = (newitr / 2);
2179 
2180 	/* The value above was written straight to EITR; make it a rate */
2181 	newitr = EM_AIM_DIVIDEND / newitr;
2182 
2183 	/*
2184 	 * Cap the rate: enable_aim=1 is the normal setting, enable_aim=2 opts
2185 	 * into the low latency end.  The original was unbounded and would ask
2186 	 * for ~95k ints/s on minimum sized frames.  There is deliberately no
2187 	 * floor, so jumbo traffic settles near 2.7k ints/s.
2188 	 */
2189 	if (sc->enable_aim == 1)
2190 		newitr = min(newitr, EM_INTS_20K);
2191 	else
2192 		newitr = min(newitr, EM_INTS_70K);
2193 
2194 	return (newitr);
2195 }
2196 
2197 /*********************************************************************
2198  *
2199  *  Helper to calculate next (E)ITR value for AIM
2200  *
2201  *********************************************************************/
2202 static void
em_newitr(struct e1000_softc * sc,struct em_rx_queue * que,struct rx_ring * rxr)2203 em_newitr(struct e1000_softc *sc, struct em_rx_queue *que,
2204     struct rx_ring *rxr)
2205 {
2206 	struct e1000_hw *hw = &sc->hw;
2207 	struct em_tx_queue *tx_que;
2208 	u32 ringbytes, ringpackets, rxbytes, rxpackets, txbytes, txpackets;
2209 	u32 newitr;
2210 	int i;
2211 
2212 	em_aim_rx_delta(rxr, &rxbytes, &rxpackets);
2213 
2214 	/*
2215 	 * A vector can service more than one TX ring when iflib is configured
2216 	 * with unequal RX and TX queue counts.  Sample every ring routed to
2217 	 * this vector rather than treating the vector as a TX queue index.
2218 	 */
2219 	txbytes = txpackets = 0;
2220 	for (i = 0; i < sc->tx_num_queues; i++) {
2221 		tx_que = &sc->tx_queues[i];
2222 		if (tx_que->msix != que->msix)
2223 			continue;
2224 		em_aim_tx_delta(&tx_que->txr, &ringbytes, &ringpackets);
2225 		txbytes += ringbytes;
2226 		txpackets += ringpackets;
2227 	}
2228 
2229 	/* Idle, do nothing */
2230 	if (txbytes == 0 && rxbytes == 0)
2231 		return;
2232 
2233 	if (sc->enable_aim == 0) {
2234 		newitr = em_max_interrupt_rate;
2235 	} else if (sc->link_speed < SPEED_1000) {
2236 		/* Use half default (4K) ITR if sub-gig */
2237 		newitr = EM_INTS_4K;
2238 	} else if (!sc->vf_ifp &&
2239 	    sc->shared->isc_max_frame_size * 2 > (sc->pba << 10)) {
2240 		/* Want at least enough packet buffer for two frames to AIM */
2241 		newitr = em_max_interrupt_rate;
2242 	} else {
2243 		newitr = em_ring_itr(sc, rxbytes, rxpackets, txbytes,
2244 		    txpackets);
2245 		/* No usable observation; leave the rate where it is */
2246 		if (newitr == 0)
2247 			return;
2248 	}
2249 
2250 	if (hw->mac.type >= igb_mac_min) {
2251 		newitr = IGB_INTS_TO_EITR(newitr);
2252 
2253 		if (hw->mac.type == e1000_82575)
2254 			newitr |= newitr << 16;
2255 		else
2256 			newitr |= E1000_EITR_CNT_IGNR;
2257 
2258 		if (newitr != que->itr_setting) {
2259 			que->itr_setting = newitr;
2260 			E1000_WRITE_REG(hw, E1000_EITR(que->msix),
2261 			    que->itr_setting);
2262 		}
2263 	} else {
2264 		newitr = EM_INTS_TO_ITR(newitr);
2265 
2266 		if (newitr != que->itr_setting) {
2267 			que->itr_setting = newitr;
2268 			if (hw->mac.type == e1000_82574 &&
2269 			    sc->intr_type == IFLIB_INTR_MSIX) {
2270 				E1000_WRITE_REG(hw,
2271 				    E1000_EITR_82574(que->msix),
2272 				    que->itr_setting);
2273 			} else {
2274 				E1000_WRITE_REG(hw, E1000_ITR,
2275 				    que->itr_setting);
2276 			}
2277 		}
2278 	}
2279 }
2280 
2281 static bool
em_has_pch_ecc(const struct e1000_hw * hw)2282 em_has_pch_ecc(const struct e1000_hw *hw)
2283 {
2284 
2285 	return (hw->mac.type >= e1000_pch_lpt &&
2286 	    hw->mac.type < e1000_82575);
2287 }
2288 
2289 static bool
em_has_82571_ecc_stats(const struct e1000_hw * hw)2290 em_has_82571_ecc_stats(const struct e1000_hw *hw)
2291 {
2292 
2293 	return (hw->mac.type == e1000_82571);
2294 }
2295 
2296 static bool
em_has_82575_memory_errors(const struct e1000_hw * hw)2297 em_has_82575_memory_errors(const struct e1000_hw *hw)
2298 {
2299 
2300 	return (hw->mac.type == e1000_82575);
2301 }
2302 
2303 static void
em_configure_82575_memory_errors(struct e1000_softc * sc)2304 em_configure_82575_memory_errors(struct e1000_softc *sc)
2305 {
2306 	struct e1000_hw *hw;
2307 	u32 ctrl_ext;
2308 
2309 	hw = &sc->hw;
2310 	if (!em_has_82575_memory_errors(hw))
2311 		return;
2312 
2313 	/* Discard pre-driver status before enabling the hardware reaction. */
2314 	(void)E1000_READ_REG(hw, E1000_PBECCSTS_82575);
2315 	(void)E1000_READ_REG(hw, E1000_RDHESTS_82575);
2316 	(void)E1000_READ_REG(hw, E1000_TDHESTS_82575);
2317 	E1000_WRITE_REG(hw, E1000_PBECCSTS_82575,
2318 	    E1000_ECC_82575_ENABLE);
2319 	E1000_WRITE_REG(hw, E1000_RDHESTS_82575,
2320 	    E1000_ECC_82575_ENABLE);
2321 	E1000_WRITE_REG(hw, E1000_TDHESTS_82575,
2322 	    E1000_ECC_82575_ENABLE);
2323 
2324 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
2325 	E1000_WRITE_REG(hw, E1000_CTRL_EXT,
2326 	    ctrl_ext | E1000_CTRL_EXT_MEHE);
2327 	E1000_WRITE_FLUSH(hw);
2328 }
2329 
2330 static bool
em_has_82576_memory_errors(const struct e1000_hw * hw)2331 em_has_82576_memory_errors(const struct e1000_hw *hw)
2332 {
2333 
2334 	return (hw->mac.type == e1000_82576);
2335 }
2336 
2337 static bool
em_82576_has_ipsec(const struct e1000_hw * hw)2338 em_82576_has_ipsec(const struct e1000_hw *hw)
2339 {
2340 
2341 	return (hw->device_id != E1000_DEV_ID_82576_NS &&
2342 	    hw->device_id != E1000_DEV_ID_82576_NS_SERDES);
2343 }
2344 
2345 static void
em_configure_82576_memory_errors(struct e1000_softc * sc)2346 em_configure_82576_memory_errors(struct e1000_softc *sc)
2347 {
2348 	struct e1000_hw *hw;
2349 	u32 peindm, reactions;
2350 
2351 	hw = &sc->hw;
2352 	if (!em_has_82576_memory_errors(hw))
2353 		return;
2354 
2355 	reactions = E1000_PEIND_82576_NONFATAL_MASK |
2356 	    E1000_PEIND_82576_FATAL_MASK |
2357 	    E1000_PEINDM_82576_PARITY_ENABLE;
2358 	if (!em_82576_has_ipsec(hw))
2359 		reactions &= ~E1000_PEIND_82576_IPSEC_MASK;
2360 
2361 	/* Discard indications left by firmware before enabling reactions. */
2362 	(void)E1000_READ_REG(hw, E1000_PEIND);
2363 	peindm = E1000_READ_REG(hw, E1000_PEINDM);
2364 	E1000_WRITE_REG(hw, E1000_PEINDM, peindm | reactions);
2365 	E1000_WRITE_FLUSH(hw);
2366 }
2367 
2368 static bool
em_has_82580_memory_errors(const struct e1000_hw * hw)2369 em_has_82580_memory_errors(const struct e1000_hw *hw)
2370 {
2371 
2372 	return (hw->mac.type == e1000_82580);
2373 }
2374 
2375 static void
em_clear_82580_memory_error_status(struct e1000_hw * hw,u32 reg)2376 em_clear_82580_memory_error_status(struct e1000_hw *hw, u32 reg)
2377 {
2378 	u32 status;
2379 
2380 	status = E1000_READ_REG(hw, reg);
2381 	if (status != 0)
2382 		E1000_WRITE_REG(hw, reg, status);
2383 }
2384 
2385 static void
em_configure_82580_memory_errors(struct e1000_softc * sc)2386 em_configure_82580_memory_errors(struct e1000_softc *sc)
2387 {
2388 	struct e1000_hw *hw;
2389 	u32 reg;
2390 
2391 	hw = &sc->hw;
2392 	if (!em_has_82580_memory_errors(hw))
2393 		return;
2394 
2395 	/* Clear status left before the driver completed its memory tables. */
2396 	(void)E1000_READ_REG(hw, E1000_PEIND);
2397 	em_clear_82580_memory_error_status(hw, E1000_DTPARS_82580);
2398 	em_clear_82580_memory_error_status(hw, E1000_DRPARS_82580);
2399 	em_clear_82580_memory_error_status(hw, E1000_DDPARS_82580);
2400 	em_clear_82580_memory_error_status(hw, E1000_PCIEERRSTS);
2401 	(void)E1000_READ_REG(hw, E1000_LANPERRSTS);
2402 	em_update_82580_ecc_stats(sc,
2403 	    E1000_READ_REG(hw, E1000_RPBECCSTS),
2404 	    E1000_READ_REG(hw, E1000_TPBECCSTS),
2405 	    E1000_READ_REG(hw, E1000_PCIEECCSTS));
2406 	E1000_WRITE_REG(hw, E1000_RPBECCSTS,
2407 	    E1000_PBECCSTS_82580_ECC_ENABLE);
2408 	E1000_WRITE_REG(hw, E1000_TPBECCSTS,
2409 	    E1000_PBECCSTS_82580_ECC_ENABLE);
2410 
2411 	reg = E1000_READ_REG(hw, E1000_DTPARC_82580);
2412 	E1000_WRITE_REG(hw, E1000_DTPARC_82580,
2413 	    reg | E1000_DTPARC_82580_ENABLE_MASK);
2414 	reg = E1000_READ_REG(hw, E1000_DRPARC_82580);
2415 	E1000_WRITE_REG(hw, E1000_DRPARC_82580,
2416 	    reg | E1000_DRPARC_82580_ENABLE_MASK);
2417 	reg = E1000_READ_REG(hw, E1000_DDPARC_82580);
2418 	E1000_WRITE_REG(hw, E1000_DDPARC_82580,
2419 	    reg | E1000_DDPARC_82580_ENABLE_MASK);
2420 	reg = E1000_READ_REG(hw, E1000_PCIEERRCTL_82580);
2421 	E1000_WRITE_REG(hw, E1000_PCIEERRCTL_82580,
2422 	    reg | E1000_PCIEERRCTL_82580_ENABLE_MASK);
2423 	reg = E1000_READ_REG(hw, E1000_PCIEECCCTL_82580);
2424 	E1000_WRITE_REG(hw, E1000_PCIEECCCTL_82580,
2425 	    reg | E1000_PCIEECCCTL_82580_ENABLE_MASK);
2426 	reg = E1000_READ_REG(hw, E1000_LANPERRCTL_82580);
2427 	reg |= E1000_LANPERRCTL_82580_HOST_MASK;
2428 	/* The RSS memory is initialized only for a multiqueue layout. */
2429 	if (sc->rx_num_queues <= 1)
2430 		reg &= ~E1000_LANPERRCTL_82580_RSS_ENABLE;
2431 	E1000_WRITE_REG(hw, E1000_LANPERRCTL_82580, reg);
2432 	reg = E1000_READ_REG(hw, E1000_PEINDM);
2433 	E1000_WRITE_REG(hw, E1000_PEINDM,
2434 	    reg | E1000_PEIND_FATAL_MASK);
2435 	E1000_WRITE_FLUSH(hw);
2436 }
2437 
2438 static bool
em_has_i210_memory_errors(const struct e1000_hw * hw)2439 em_has_i210_memory_errors(const struct e1000_hw *hw)
2440 {
2441 
2442 	return (hw->mac.type == e1000_i210 ||
2443 	    hw->mac.type == e1000_i211);
2444 }
2445 
2446 static bool
em_has_i350_i354_memory_errors(const struct e1000_hw * hw)2447 em_has_i350_i354_memory_errors(const struct e1000_hw *hw)
2448 {
2449 
2450 	return (hw->mac.type == e1000_i350 ||
2451 	    hw->mac.type == e1000_i354);
2452 }
2453 
2454 static void
em_configure_peind_memory_errors(struct e1000_softc * sc)2455 em_configure_peind_memory_errors(struct e1000_softc *sc)
2456 {
2457 	struct e1000_hw *hw;
2458 	u32 peindm;
2459 
2460 	hw = &sc->hw;
2461 	if (!em_has_i350_i354_memory_errors(hw) &&
2462 	    !em_has_i210_memory_errors(hw))
2463 		return;
2464 
2465 	/* Discard indications left by firmware before enabling reactions. */
2466 	(void)E1000_READ_REG(hw, E1000_PEIND);
2467 	/* Do not depend on firmware preserving the datasheet defaults. */
2468 	peindm = E1000_READ_REG(hw, E1000_PEINDM);
2469 	E1000_WRITE_REG(hw, E1000_PEINDM,
2470 	    peindm | E1000_PEIND_FATAL_MASK);
2471 	E1000_WRITE_FLUSH(hw);
2472 }
2473 
2474 static bool
em_has_peind_memory_errors(const struct e1000_hw * hw)2475 em_has_peind_memory_errors(const struct e1000_hw *hw)
2476 {
2477 
2478 	return (em_has_82580_memory_errors(hw) ||
2479 	    em_has_i350_i354_memory_errors(hw) ||
2480 	    em_has_i210_memory_errors(hw));
2481 }
2482 
2483 static u32
em_pcie_fatal_error_mask(const struct e1000_hw * hw)2484 em_pcie_fatal_error_mask(const struct e1000_hw *hw)
2485 {
2486 
2487 	if (em_has_82580_memory_errors(hw))
2488 		return (~0U);
2489 	if (em_has_i350_i354_memory_errors(hw))
2490 		return (E1000_PCIEERRSTS_I350_I354_FATAL_MASK);
2491 	if (em_has_i210_memory_errors(hw))
2492 		return (E1000_PCIEERRSTS_I210_FATAL_MASK);
2493 	return (0);
2494 }
2495 
2496 static u32
em_memory_error_intr_mask(const struct e1000_hw * hw)2497 em_memory_error_intr_mask(const struct e1000_hw *hw)
2498 {
2499 
2500 	if (em_has_82575_memory_errors(hw))
2501 		return (E1000_IMS_82575_MEMORY_ERROR_MASK);
2502 	if (em_has_82576_memory_errors(hw))
2503 		return (E1000_IMS_FER | E1000_IMS_NFER);
2504 	if (em_has_pch_ecc(hw) || em_has_peind_memory_errors(hw))
2505 		return (E1000_IMS_FER);
2506 	return (0);
2507 }
2508 
2509 static bool
em_has_memory_errors(const struct e1000_hw * hw)2510 em_has_memory_errors(const struct e1000_hw *hw)
2511 {
2512 
2513 	return (em_memory_error_intr_mask(hw) != 0);
2514 }
2515 
2516 static bool
em_has_memory_error_stats(const struct e1000_hw * hw)2517 em_has_memory_error_stats(const struct e1000_hw *hw)
2518 {
2519 
2520 	return (em_has_82571_ecc_stats(hw) || em_has_memory_errors(hw));
2521 }
2522 
2523 static u32
em_fatal_error_intr_mask(struct e1000_softc * sc)2524 em_fatal_error_intr_mask(struct e1000_softc *sc)
2525 {
2526 	if (!em_has_memory_errors(&sc->hw))
2527 		return (0);
2528 	if (atomic_load_acq_32(&sc->fatal_error_state) !=
2529 	    EM_FATAL_ERROR_NONE)
2530 		return (0);
2531 	return (em_memory_error_intr_mask(&sc->hw));
2532 }
2533 
2534 static void
em_update_82580_ecc_stats(struct e1000_softc * sc,u32 rpbeccsts,u32 tpbeccsts,u32 pcieeccsts)2535 em_update_82580_ecc_stats(struct e1000_softc *sc, u32 rpbeccsts,
2536     u32 tpbeccsts, u32 pcieeccsts)
2537 {
2538 	u32 status;
2539 
2540 	sc->corrected_error_packet_buffer_count +=
2541 	    (rpbeccsts & E1000_PBECCSTS_82580_CORR_CNT_MASK) +
2542 	    (tpbeccsts & E1000_PBECCSTS_82580_CORR_CNT_MASK);
2543 	status = pcieeccsts & E1000_PCIEECCSTS_82580_ERROR_MASK;
2544 	sc->uncorrected_error_pcie_count += bitcount32(status);
2545 	if (status != 0)
2546 		E1000_WRITE_REG(&sc->hw, E1000_PCIEECCSTS, status);
2547 }
2548 
2549 static void
em_update_82575_ecc_stats(struct e1000_softc * sc,u32 pbeccsts,u32 rdhests,u32 tdhests)2550 em_update_82575_ecc_stats(struct e1000_softc *sc, u32 pbeccsts,
2551     u32 rdhests, u32 tdhests)
2552 {
2553 
2554 	sc->corrected_error_packet_buffer_count +=
2555 	    pbeccsts & E1000_ECC_82575_CORR_CNT_MASK;
2556 	sc->uncorrected_error_packet_buffer_count +=
2557 	    (pbeccsts & E1000_ECC_82575_UNCORR_CNT_MASK) >>
2558 	    E1000_ECC_82575_UNCORR_CNT_SHIFT;
2559 	sc->corrected_error_dma_count +=
2560 	    (rdhests & E1000_ECC_82575_CORR_CNT_MASK) +
2561 	    (tdhests & E1000_ECC_82575_CORR_CNT_MASK);
2562 	sc->uncorrected_error_dma_count +=
2563 	    ((rdhests & E1000_ECC_82575_UNCORR_CNT_MASK) >>
2564 	    E1000_ECC_82575_UNCORR_CNT_SHIFT) +
2565 	    ((tdhests & E1000_ECC_82575_UNCORR_CNT_MASK) >>
2566 	    E1000_ECC_82575_UNCORR_CNT_SHIFT);
2567 }
2568 
2569 static void
em_update_82576_ecc_counter(struct e1000_softc * sc,u32 reg,u64 * corrected,u64 * uncorrected)2570 em_update_82576_ecc_counter(struct e1000_softc *sc, u32 reg,
2571     u64 *corrected, u64 *uncorrected)
2572 {
2573 	u32 status;
2574 
2575 	status = E1000_READ_REG(&sc->hw, reg);
2576 	*corrected += status & E1000_ECC_82576_CORR_CNT_MASK;
2577 	if (uncorrected != NULL)
2578 		*uncorrected +=
2579 		    (status & E1000_ECC_82576_UNCORR_CNT_MASK) >>
2580 		    E1000_ECC_82576_UNCORR_CNT_SHIFT;
2581 }
2582 
2583 static void
em_update_82576_ecc_stats(struct e1000_softc * sc)2584 em_update_82576_ecc_stats(struct e1000_softc *sc)
2585 {
2586 
2587 	/*
2588 	 * These counters are clear-on-read.  PRBESTS and PMSIXESTS are
2589 	 * controller-shared, so whichever LAN port samples them first owns
2590 	 * the software count.
2591 	 */
2592 	em_update_82576_ecc_counter(sc, E1000_RPBECCSTS,
2593 	    &sc->corrected_error_packet_buffer_count,
2594 	    &sc->uncorrected_error_packet_buffer_count);
2595 	em_update_82576_ecc_counter(sc, E1000_TPBECCSTS,
2596 	    &sc->corrected_error_packet_buffer_count,
2597 	    &sc->uncorrected_error_packet_buffer_count);
2598 	em_update_82576_ecc_counter(sc, E1000_SWPBECCSTS_82576,
2599 	    &sc->corrected_error_packet_buffer_count,
2600 	    &sc->uncorrected_error_packet_buffer_count);
2601 	if (em_82576_has_ipsec(&sc->hw))
2602 		em_update_82576_ecc_counter(sc, E1000_IPPBECCSTS_82576,
2603 		    &sc->corrected_error_packet_buffer_count,
2604 		    &sc->uncorrected_error_packet_buffer_count);
2605 
2606 	em_update_82576_ecc_counter(sc, E1000_RDHESTS_82576,
2607 	    &sc->corrected_error_dma_count,
2608 	    &sc->uncorrected_error_dma_count);
2609 	em_update_82576_ecc_counter(sc, E1000_TDHESTS_82576,
2610 	    &sc->corrected_error_dma_count,
2611 	    &sc->uncorrected_error_dma_count);
2612 
2613 	em_update_82576_ecc_counter(sc, E1000_PRBESTS_82576,
2614 	    &sc->corrected_error_pcie_retry_count, NULL);
2615 	em_update_82576_ecc_counter(sc, E1000_PWBESTS_82576,
2616 	    &sc->corrected_error_pcie_tx_data_count, NULL);
2617 	em_update_82576_ecc_counter(sc, E1000_PMSIXESTS_82576,
2618 	    &sc->corrected_error_pcie_other_count, NULL);
2619 }
2620 
2621 static void
em_update_pch_ecc_stats(struct e1000_softc * sc,u32 pbeccsts)2622 em_update_pch_ecc_stats(struct e1000_softc *sc, u32 pbeccsts)
2623 {
2624 
2625 	sc->corrected_error_packet_buffer_count +=
2626 	    pbeccsts & E1000_PBECCSTS_CORR_ERR_CNT_MASK;
2627 	sc->uncorrected_error_packet_buffer_count +=
2628 	    (pbeccsts & E1000_PBECCSTS_UNCORR_ERR_CNT_MASK) >>
2629 	    E1000_PBECCSTS_UNCORR_ERR_CNT_SHIFT;
2630 }
2631 
2632 static void
em_update_82571_ecc_stats(struct e1000_softc * sc)2633 em_update_82571_ecc_stats(struct e1000_softc *sc)
2634 {
2635 	struct e1000_hw *hw;
2636 	u32 count, pba_ecc;
2637 
2638 	hw = &sc->hw;
2639 	pba_ecc = E1000_READ_REG(hw, E1000_PBA_ECC);
2640 	count = (pba_ecc & E1000_PBA_ECC_COUNTER_MASK) >>
2641 	    E1000_PBA_ECC_COUNTER_SHIFT;
2642 	if (count == 0)
2643 		return;
2644 	sc->corrected_error_packet_buffer_count += count;
2645 	/* Preserve correction and reserved state while clearing statistics. */
2646 	E1000_WRITE_REG(hw, E1000_PBA_ECC,
2647 	    pba_ecc | E1000_PBA_ECC_STAT_CLR);
2648 }
2649 
2650 static void
em_update_i210_ecc_stats(struct e1000_softc * sc)2651 em_update_i210_ecc_stats(struct e1000_softc *sc)
2652 {
2653 	struct e1000_hw *hw;
2654 	u32 pbeccsts, pcieeccsts;
2655 
2656 	hw = &sc->hw;
2657 	pbeccsts = E1000_READ_REG(hw, E1000_PBECCSTS_I210);
2658 	if (pbeccsts & E1000_PBECCSTS_I210_CORR_ERR) {
2659 		sc->corrected_error_dma_count++;
2660 		/* Preserve the enable bit while clearing the RW1C status. */
2661 		E1000_WRITE_REG(hw, E1000_PBECCSTS_I210,
2662 		    pbeccsts & (E1000_PBECCSTS_I210_ECC_ENABLE |
2663 		    E1000_PBECCSTS_I210_CORR_ERR));
2664 	}
2665 
2666 	pcieeccsts = E1000_READ_REG(hw, E1000_PCIEECCSTS) &
2667 	    E1000_PCIEECCSTS_I210_CORR_MASK;
2668 	if (pcieeccsts & E1000_PCIEECCSTS_TX_WR_DATA)
2669 		sc->corrected_error_pcie_tx_data_count++;
2670 	if (pcieeccsts & E1000_PCIEECCSTS_RETRY_BUF)
2671 		sc->corrected_error_pcie_retry_count++;
2672 	if (pcieeccsts != 0)
2673 		E1000_WRITE_REG(hw, E1000_PCIEECCSTS, pcieeccsts);
2674 }
2675 
2676 static void
em_update_i350_i354_ecc_stats(struct e1000_softc * sc)2677 em_update_i350_i354_ecc_stats(struct e1000_softc *sc)
2678 {
2679 	struct e1000_hw *hw;
2680 	u32 pbeccsts, pcieecc_mask, status;
2681 
2682 	hw = &sc->hw;
2683 	status = E1000_READ_REG(hw, E1000_DTPARS) &
2684 	    E1000_DTPARS_CORR_MASK;
2685 	if (status != 0) {
2686 		sc->corrected_error_dma_count += bitcount32(status);
2687 		E1000_WRITE_REG(hw, E1000_DTPARS, status);
2688 	}
2689 	status = E1000_READ_REG(hw, E1000_DRPARS) &
2690 	    E1000_DRPARS_CORR_MASK;
2691 	if (status != 0) {
2692 		sc->corrected_error_dma_count += bitcount32(status);
2693 		E1000_WRITE_REG(hw, E1000_DRPARS, status);
2694 	}
2695 	status = E1000_READ_REG(hw, E1000_DDECCS) &
2696 	    E1000_DDECCS_CORR_MASK;
2697 	if (status != 0) {
2698 		sc->corrected_error_dma_count += bitcount32(status);
2699 		E1000_WRITE_REG(hw, E1000_DDECCS, status);
2700 	}
2701 	status = E1000_READ_REG(hw, E1000_LANPERRSTS) &
2702 	    E1000_LANPERRSTS_MNG_FIFO_CORR;
2703 	if (status != 0) {
2704 		sc->corrected_error_lan_mng_fifo_count++;
2705 		E1000_WRITE_REG(hw, E1000_LANPERRSTS, status);
2706 	}
2707 
2708 	pbeccsts = E1000_READ_REG(hw, E1000_RPBECCSTS);
2709 	status = pbeccsts & E1000_PBECCSTS_I350_I354_CORR_MASK;
2710 	if (status != 0) {
2711 		sc->corrected_error_packet_buffer_count += bitcount32(status);
2712 		/* Preserve the enable bits while clearing RW1C status. */
2713 		E1000_WRITE_REG(hw, E1000_RPBECCSTS,
2714 		    pbeccsts & (E1000_PBECCSTS_I350_I354_ENABLE_MASK |
2715 		    E1000_PBECCSTS_I350_I354_CORR_MASK));
2716 	}
2717 	pbeccsts = E1000_READ_REG(hw, E1000_TPBECCSTS);
2718 	status = pbeccsts & E1000_PBECCSTS_I350_I354_CORR_MASK;
2719 	if (status != 0) {
2720 		sc->corrected_error_packet_buffer_count += bitcount32(status);
2721 		E1000_WRITE_REG(hw, E1000_TPBECCSTS,
2722 		    pbeccsts & (E1000_PBECCSTS_I350_I354_ENABLE_MASK |
2723 		    E1000_PBECCSTS_I350_I354_CORR_MASK));
2724 	}
2725 
2726 	pcieecc_mask = hw->mac.type == e1000_i354 ?
2727 	    E1000_PCIEECCSTS_I354_CORR_MASK :
2728 	    E1000_PCIEECCSTS_I350_CORR_MASK;
2729 	status = E1000_READ_REG(hw, E1000_PCIEECCSTS) & pcieecc_mask;
2730 	if (status & E1000_PCIEECCSTS_TX_WR_DATA)
2731 		sc->corrected_error_pcie_tx_data_count++;
2732 	if (status & E1000_PCIEECCSTS_RETRY_BUF)
2733 		sc->corrected_error_pcie_retry_count++;
2734 	sc->corrected_error_pcie_other_count += bitcount32(status &
2735 	    E1000_PCIEECCSTS_I350_I354_OTHER_MASK);
2736 	if (status != 0)
2737 		E1000_WRITE_REG(hw, E1000_PCIEECCSTS, status);
2738 }
2739 
2740 /*
2741  * Internal-memory error causes are read-clear.  Capture them before handing
2742  * fatal recovery or non-fatal acknowledgement to the iflib admin task.
2743  */
2744 static void
em_handle_fatal_error_intr(struct e1000_softc * sc,u32 icr)2745 em_handle_fatal_error_intr(struct e1000_softc *sc, u32 icr)
2746 {
2747 	struct e1000_hw *hw;
2748 	u32 dma_host, dma_rx, dma_tx, error_mask, lanerr, pcieerr, peind;
2749 
2750 	error_mask = em_memory_error_intr_mask(&sc->hw);
2751 	if (!em_has_memory_errors(&sc->hw) ||
2752 	    (icr & error_mask) == 0)
2753 		return;
2754 
2755 	hw = &sc->hw;
2756 	E1000_WRITE_REG(hw, E1000_IMC, error_mask);
2757 	if (!atomic_cmpset_32(&sc->fatal_error_state,
2758 	    EM_FATAL_ERROR_NONE, EM_FATAL_ERROR_CAPTURING))
2759 		return;
2760 
2761 	sc->fatal_error_icr = icr & error_mask;
2762 	if (em_has_pch_ecc(hw)) {
2763 		sc->fatal_error_pbeccsts =
2764 		    E1000_READ_REG(hw, E1000_PBECCSTS);
2765 	} else if (em_has_82575_memory_errors(hw)) {
2766 		sc->fatal_error_pbeccsts =
2767 		    E1000_READ_REG(hw, E1000_PBECCSTS_82575);
2768 		sc->fatal_error_dma_rx =
2769 		    E1000_READ_REG(hw, E1000_RDHESTS_82575);
2770 		sc->fatal_error_dma_tx =
2771 		    E1000_READ_REG(hw, E1000_TDHESTS_82575);
2772 	} else if (em_has_82576_memory_errors(hw)) {
2773 		sc->fatal_error_peind = E1000_READ_REG(hw, E1000_PEIND);
2774 	} else {
2775 		peind = E1000_READ_REG(hw, E1000_PEIND) &
2776 		    E1000_PEIND_FATAL_MASK;
2777 		pcieerr = E1000_READ_REG(hw, E1000_PCIEERRSTS) &
2778 		    em_pcie_fatal_error_mask(hw);
2779 		dma_host = 0;
2780 		if (em_has_82580_memory_errors(hw)) {
2781 			/*
2782 			 * PEIND is visible through every function.  Retain the
2783 			 * management indication, which has no subordinate status,
2784 			 * but attribute host-owned regions from this function's
2785 			 * status registers.
2786 			 */
2787 			peind &= E1000_PEIND_MNG_PARITY_FATAL;
2788 			dma_tx = E1000_READ_REG(hw, E1000_DTPARS_82580);
2789 			dma_rx = E1000_READ_REG(hw, E1000_DRPARS_82580);
2790 			dma_host = E1000_READ_REG(hw,
2791 			    E1000_DDPARS_82580);
2792 			lanerr = E1000_READ_REG(hw, E1000_LANPERRSTS) &
2793 			    E1000_LANPERRSTS_82580_ERROR_MASK;
2794 		} else if (em_has_i350_i354_memory_errors(hw)) {
2795 			dma_tx = E1000_READ_REG(hw, E1000_DTPARS) &
2796 			    E1000_DTPARS_FATAL_MASK;
2797 			dma_rx = E1000_READ_REG(hw, E1000_DRPARS) &
2798 			    E1000_DRPARS_FATAL_MASK;
2799 			lanerr = E1000_READ_REG(hw, E1000_LANPERRSTS) &
2800 			    E1000_LANPERRSTS_I350_I354_FATAL_MASK;
2801 		} else {
2802 			dma_tx = 0;
2803 			dma_rx = 0;
2804 			lanerr = E1000_READ_REG(hw, E1000_LANPERRSTS) &
2805 			    E1000_LANPERRSTS_RETX_BUF;
2806 		}
2807 		if (pcieerr != 0)
2808 			peind |= E1000_PEIND_PCIE_PARITY_FATAL;
2809 		if (lanerr != 0)
2810 			peind |= E1000_PEIND_LANPORT_PARITY_FATAL;
2811 		if (dma_tx != 0 || dma_rx != 0 || dma_host != 0)
2812 			peind |= E1000_PEIND_DMA_PARITY_FATAL;
2813 		sc->fatal_error_peind = peind;
2814 		sc->fatal_error_pcie = pcieerr;
2815 		sc->fatal_error_lan = lanerr;
2816 		sc->fatal_error_dma_tx = dma_tx;
2817 		sc->fatal_error_dma_rx = dma_rx;
2818 		sc->fatal_error_dma_host = dma_host;
2819 	}
2820 	atomic_store_rel_32(&sc->fatal_error_state,
2821 	    EM_FATAL_ERROR_DETECTED);
2822 	iflib_admin_intr_deferred(sc->ctx);
2823 }
2824 
2825 static bool
em_handle_fatal_error_admin(struct e1000_softc * sc)2826 em_handle_fatal_error_admin(struct e1000_softc *sc)
2827 {
2828 	u32 error_mask, pcieecc, peind;
2829 	bool reset_required;
2830 
2831 	if (!atomic_cmpset_acq_32(&sc->fatal_error_state,
2832 	    EM_FATAL_ERROR_DETECTED, EM_FATAL_ERROR_RESET_REQUESTED))
2833 		return (atomic_load_acq_32(&sc->fatal_error_state) !=
2834 		    EM_FATAL_ERROR_NONE);
2835 
2836 	if (em_has_pch_ecc(&sc->hw)) {
2837 		em_update_pch_ecc_stats(sc, sc->fatal_error_pbeccsts);
2838 		device_printf(sc->dev,
2839 		    "uncorrectable packet-buffer ECC error: "
2840 		    "PBECCSTS %#x; requesting reset\n",
2841 		    sc->fatal_error_pbeccsts);
2842 	} else if (em_has_82575_memory_errors(&sc->hw)) {
2843 		em_update_82575_ecc_stats(sc, sc->fatal_error_pbeccsts,
2844 		    sc->fatal_error_dma_rx, sc->fatal_error_dma_tx);
2845 		device_printf(sc->dev,
2846 		    "unrecoverable internal memory ECC error: ICR %#x, "
2847 		    "PBECCSTS %#x, RDHESTS %#x, TDHESTS %#x; "
2848 		    "requesting reset\n", sc->fatal_error_icr,
2849 		    sc->fatal_error_pbeccsts, sc->fatal_error_dma_rx,
2850 		    sc->fatal_error_dma_tx);
2851 	} else if (em_has_82576_memory_errors(&sc->hw)) {
2852 		peind = sc->fatal_error_peind;
2853 		em_update_82576_ecc_stats(sc);
2854 		reset_required =
2855 		    (sc->fatal_error_icr & E1000_ICR_FER) != 0 ||
2856 		    (peind & (E1000_PEIND_82576_FATAL_MASK |
2857 		    E1000_PEIND_82576_MEMORY_HANG)) != 0;
2858 		if (!reset_required) {
2859 			device_printf(sc->dev,
2860 			    "non-fatal internal memory error: PEIND %#x\n",
2861 			    peind);
2862 			sc->fatal_error_icr = 0;
2863 			sc->fatal_error_peind = 0;
2864 			atomic_store_rel_32(&sc->fatal_error_state,
2865 			    EM_FATAL_ERROR_NONE);
2866 			error_mask = E1000_IMS_FER | E1000_IMS_NFER;
2867 			E1000_WRITE_REG(&sc->hw, E1000_IMS, error_mask);
2868 			E1000_WRITE_FLUSH(&sc->hw);
2869 			return (true);
2870 		}
2871 		if ((peind & (E1000_PEIND_82576_FATAL_MASK |
2872 		    E1000_PEIND_82576_MEMORY_HANG)) == 0)
2873 			sc->fatal_error_unknown_count++;
2874 		device_printf(sc->dev,
2875 		    "fatal internal memory error: PEIND %#x; "
2876 		    "requesting reset\n", peind);
2877 	} else {
2878 		peind = sc->fatal_error_peind;
2879 		if (em_has_82580_memory_errors(&sc->hw)) {
2880 			pcieecc = E1000_READ_REG(&sc->hw,
2881 			    E1000_PCIEECCSTS) &
2882 			    E1000_PCIEECCSTS_82580_ERROR_MASK;
2883 			sc->fatal_error_pcie_ecc |= pcieecc;
2884 			if (pcieecc != 0) {
2885 				peind |= E1000_PEIND_PCIE_PARITY_FATAL;
2886 				sc->fatal_error_peind = peind;
2887 			}
2888 			em_update_82580_ecc_stats(sc,
2889 			    E1000_READ_REG(&sc->hw, E1000_RPBECCSTS),
2890 			    E1000_READ_REG(&sc->hw, E1000_TPBECCSTS),
2891 			    pcieecc);
2892 		} else if (em_has_i350_i354_memory_errors(&sc->hw))
2893 			em_update_i350_i354_ecc_stats(sc);
2894 		if (peind & E1000_PEIND_LANPORT_PARITY_FATAL)
2895 			sc->fatal_error_lan_count++;
2896 		if (peind & E1000_PEIND_MNG_PARITY_FATAL)
2897 			sc->fatal_error_mng_count++;
2898 		if (peind & E1000_PEIND_PCIE_PARITY_FATAL)
2899 			sc->fatal_error_pcie_count++;
2900 		if (peind & E1000_PEIND_DMA_PARITY_FATAL)
2901 			sc->fatal_error_dma_count++;
2902 		if (peind == 0)
2903 			sc->fatal_error_unknown_count++;
2904 		if (em_has_82580_memory_errors(&sc->hw)) {
2905 			device_printf(sc->dev,
2906 			    "fatal internal memory error: PEIND %#x, "
2907 			    "PCIEERRSTS %#x, PCIEECCSTS %#x, "
2908 			    "DTPARS %#x, DRPARS %#x, DDPARS %#x, "
2909 			    "LANPERRSTS %#x\n", peind,
2910 			    sc->fatal_error_pcie,
2911 			    sc->fatal_error_pcie_ecc,
2912 			    sc->fatal_error_dma_tx,
2913 			    sc->fatal_error_dma_rx,
2914 			    sc->fatal_error_dma_host,
2915 			    sc->fatal_error_lan);
2916 		} else {
2917 			device_printf(sc->dev,
2918 			    "fatal internal memory error: PEIND %#x, "
2919 			    "PCIEERRSTS %#x, DTPARS %#x, DRPARS %#x, "
2920 			    "LANPERRSTS %#x\n", peind,
2921 			    sc->fatal_error_pcie,
2922 			    sc->fatal_error_dma_tx,
2923 			    sc->fatal_error_dma_rx,
2924 			    sc->fatal_error_lan);
2925 		}
2926 
2927 		reset_required = (peind &
2928 		    (E1000_PEIND_PCIE_PARITY_FATAL |
2929 		    E1000_PEIND_DMA_PARITY_FATAL)) != 0;
2930 		if (peind == 0)
2931 			reset_required = true;
2932 		if (peind & E1000_PEIND_LANPORT_PARITY_FATAL) {
2933 			if (!em_has_i350_i354_memory_errors(&sc->hw) ||
2934 			    sc->fatal_error_lan == 0 ||
2935 			    (sc->fatal_error_lan &
2936 			    E1000_LANPERRSTS_I350_I354_RESET_MASK) != 0)
2937 				reset_required = true;
2938 		}
2939 		/* Management-memory recovery belongs to management firmware. */
2940 		if (!reset_required) {
2941 			if (em_has_i350_i354_memory_errors(&sc->hw) &&
2942 			    sc->fatal_error_lan != 0)
2943 				E1000_WRITE_REG(&sc->hw, E1000_LANPERRSTS,
2944 				    sc->fatal_error_lan &
2945 				    E1000_LANPERRSTS_I350_I354_NO_RESET_MASK);
2946 			sc->fatal_error_peind = 0;
2947 			sc->fatal_error_pcie = 0;
2948 			sc->fatal_error_pcie_ecc = 0;
2949 			sc->fatal_error_lan = 0;
2950 			sc->fatal_error_dma_tx = 0;
2951 			sc->fatal_error_dma_rx = 0;
2952 			sc->fatal_error_dma_host = 0;
2953 			atomic_store_rel_32(&sc->fatal_error_state,
2954 			    EM_FATAL_ERROR_NONE);
2955 			E1000_WRITE_REG(&sc->hw, E1000_IMS,
2956 			    E1000_IMS_FER);
2957 			E1000_WRITE_FLUSH(&sc->hw);
2958 			return (true);
2959 		}
2960 		device_printf(sc->dev,
2961 		    "requesting reset after memory error\n");
2962 	}
2963 	sc->fatal_error_reset_count++;
2964 	iflib_request_reset(sc->ctx);
2965 	/* Re-enter the admin task so it observes the reset request. */
2966 	iflib_admin_intr_deferred(sc->ctx);
2967 	return (true);
2968 }
2969 
2970 /*
2971  * ICR bit 30 is reserved on 82575 and is the TCP timer on 82576.  It becomes
2972  * the Device Reset Asserted interrupt starting with 82580.
2973  */
2974 static u32
igb_device_reset_intr_mask(struct e1000_softc * sc)2975 igb_device_reset_intr_mask(struct e1000_softc *sc)
2976 {
2977 
2978 	return (sc->hw.mac.type >= e1000_82580 ? E1000_IMS_DRSTA : 0);
2979 }
2980 
2981 /* Keep interrupt-side work quiesced until device-reset recovery completes. */
2982 static bool
igb_device_reset_pending(struct e1000_softc * sc)2983 igb_device_reset_pending(struct e1000_softc *sc)
2984 {
2985 
2986 	return (!sc->vf_ifp && igb_device_reset_intr_mask(sc) != 0 &&
2987 	    atomic_load_acq_32(&sc->device_reset_state) !=
2988 	    IGB_DEVICE_RESET_NONE);
2989 }
2990 
2991 /*
2992  * CTRL.DEV_RST resets every port in the device.  ICR.DRSTA tells the other
2993  * ports that their registers and descriptor rings must be reinitialized.
2994  */
2995 static bool
igb_handle_device_reset(struct e1000_softc * sc,u32 icr)2996 igb_handle_device_reset(struct e1000_softc *sc, u32 icr)
2997 {
2998 	u32 state;
2999 
3000 	if (sc->vf_ifp || igb_device_reset_intr_mask(sc) == 0 ||
3001 	    (icr & E1000_ICR_DRSTA) == 0)
3002 		return (false);
3003 	state = atomic_swap_32(&sc->device_reset_state,
3004 	    IGB_DEVICE_RESET_DETECTED);
3005 	if (state == IGB_DEVICE_RESET_DETECTED)
3006 		return (true);
3007 
3008 	iflib_admin_intr_deferred(sc->ctx);
3009 	return (true);
3010 }
3011 
3012 /*
3013  * A device reset can leave a sibling port accessible before its internal
3014  * reset and PCIe transactions have completed.  For 82580 and newer parts,
3015  * wait for that device-wide reset to finish and acknowledge it before any
3016  * ordinary port register programming.  I350 and newer parts also publish
3017  * explicit EEPROM autoload and PF-reset completion indications.
3018  *
3019  * The wait is bounded because the only useful fallback for a controller
3020  * that never completes the device reset is the port reset already requested
3021  * by the interrupt handler.
3022  */
3023 static void
igb_prepare_device_reset(struct e1000_softc * sc)3024 igb_prepare_device_reset(struct e1000_softc *sc)
3025 {
3026 	struct e1000_hw *hw;
3027 	u32 state;
3028 	u32 eecd, gcr, status;
3029 	int i;
3030 
3031 	hw = &sc->hw;
3032 	state = atomic_load_acq_32(&sc->device_reset_state);
3033 	if (state != IGB_DEVICE_RESET_DETECTED &&
3034 	    state != IGB_DEVICE_RESET_REQUESTED &&
3035 	    hw->mac.type >= e1000_82580) {
3036 		/*
3037 		 * A reset can start while this interface has interrupts disabled.
3038 		 * GCR is the documented gate before ordinary port accesses.  STATUS
3039 		 * also detects a reset that completed while this interface was down
3040 		 * or after an earlier preparation pass.
3041 		 */
3042 		gcr = E1000_READ_REG(hw, E1000_GCR);
3043 		if (gcr != 0xffffffff &&
3044 		    (gcr & E1000_GCR_DEV_RST_IN_PROGRESS) != 0) {
3045 			atomic_store_rel_32(&sc->device_reset_state,
3046 			    IGB_DEVICE_RESET_DETECTED);
3047 			state = IGB_DEVICE_RESET_DETECTED;
3048 		} else if (gcr != 0xffffffff) {
3049 			status = E1000_READ_REG(hw, E1000_STATUS);
3050 			if (status != 0xffffffff &&
3051 			    (status & E1000_STAT_DEV_RST_SET) != 0) {
3052 				atomic_store_rel_32(&sc->device_reset_state,
3053 				    IGB_DEVICE_RESET_DETECTED);
3054 				state = IGB_DEVICE_RESET_DETECTED;
3055 			}
3056 		}
3057 	}
3058 	if (state != IGB_DEVICE_RESET_DETECTED &&
3059 	    state != IGB_DEVICE_RESET_REQUESTED)
3060 		return;
3061 
3062 	if (hw->mac.type >= e1000_82580) {
3063 		for (i = 0; i < IGB_DEVICE_RESET_TIMEOUT_MS; i++) {
3064 			gcr = E1000_READ_REG(hw, E1000_GCR);
3065 			if (gcr != 0xffffffff &&
3066 			    (gcr & E1000_GCR_DEV_RST_IN_PROGRESS) == 0)
3067 				break;
3068 			msec_delay(1);
3069 		}
3070 		if (i == IGB_DEVICE_RESET_TIMEOUT_MS) {
3071 			device_printf(sc->dev,
3072 			    "device-wide reset did not complete; "
3073 			    "attempting port reset\n");
3074 			goto prepared;
3075 		}
3076 
3077 		/* STATUS.DEV_RST_SET is write-one-to-clear. */
3078 		E1000_WRITE_REG(hw, E1000_STATUS, E1000_STAT_DEV_RST_SET);
3079 
3080 		if (hw->mac.type >= e1000_i350) {
3081 			for (i = 0; i < IGB_DEVICE_RESET_TIMEOUT_MS; i++) {
3082 				eecd = E1000_READ_REG(hw, E1000_EECD);
3083 				status = E1000_READ_REG(hw, E1000_STATUS);
3084 				if (eecd != 0xffffffff && status != 0xffffffff &&
3085 				    (eecd & E1000_EECD_AUTO_RD) != 0 &&
3086 				    (status & E1000_STATUS_RST_DONE) != 0)
3087 					break;
3088 				msec_delay(1);
3089 			}
3090 			if (i == IGB_DEVICE_RESET_TIMEOUT_MS)
3091 				device_printf(sc->dev,
3092 				    "device-wide reset did not finish EEPROM "
3093 				    "autoload or port reset; attempting port "
3094 				    "reset\n");
3095 		}
3096 	}
3097 
3098 prepared:
3099 	atomic_store_rel_32(&sc->device_reset_state,
3100 	    IGB_DEVICE_RESET_PREPARED);
3101 }
3102 
3103 /*
3104  * A second device reset can arrive while the port is being initialized.
3105  * Leave its status latched for the next preparation pass and do not let
3106  * iflib publish this incomplete initialization as a running datapath.
3107  */
3108 static bool
igb_finish_device_reset(struct e1000_softc * sc,u32 icr)3109 igb_finish_device_reset(struct e1000_softc *sc, u32 icr)
3110 {
3111 	bool reset_again;
3112 	u32 gcr, state, status;
3113 
3114 	if (igb_device_reset_intr_mask(sc) == 0)
3115 		return (false);
3116 
3117 	state = atomic_load_acq_32(&sc->device_reset_state);
3118 	reset_again = icr != 0xffffffff &&
3119 	    (icr & E1000_ICR_DRSTA) != 0;
3120 	if (sc->hw.mac.type >= e1000_82580) {
3121 		gcr = E1000_READ_REG(&sc->hw, E1000_GCR);
3122 		if (gcr != 0xffffffff &&
3123 		    (gcr & E1000_GCR_DEV_RST_IN_PROGRESS) != 0)
3124 			reset_again = true;
3125 		status = E1000_READ_REG(&sc->hw, E1000_STATUS);
3126 		if (status == 0xffffffff &&
3127 		    state != IGB_DEVICE_RESET_NONE) {
3128 			/*
3129 			 * MMIO can disappear briefly while SR-IOV is changing, but
3130 			 * config space remains readable.  If both are gone, retain the
3131 			 * stopped state without queueing an endless reset loop.
3132 			 */
3133 			if (pci_read_config(sc->dev, PCIR_VENDOR, 2) == 0xffff) {
3134 				atomic_store_rel_32(&sc->device_reset_state,
3135 				    IGB_DEVICE_RESET_DETECTED);
3136 				device_printf(sc->dev,
3137 				    "device unavailable after device-wide reset; "
3138 				    "leaving interface stopped\n");
3139 				return (true);
3140 			}
3141 			reset_again = true;
3142 		} else if (status != 0xffffffff &&
3143 		    (status & E1000_STAT_DEV_RST_SET) != 0)
3144 			reset_again = true;
3145 	}
3146 	if (state == IGB_DEVICE_RESET_DETECTED ||
3147 	    state == IGB_DEVICE_RESET_REQUESTED)
3148 		reset_again = true;
3149 	if (!reset_again) {
3150 		if (state == IGB_DEVICE_RESET_PREPARED &&
3151 		    !atomic_cmpset_rel_32(&sc->device_reset_state,
3152 		    IGB_DEVICE_RESET_PREPARED, IGB_DEVICE_RESET_NONE))
3153 			return (true);
3154 		return (false);
3155 	}
3156 
3157 	state = atomic_swap_32(&sc->device_reset_state,
3158 	    IGB_DEVICE_RESET_DETECTED);
3159 	if (state != IGB_DEVICE_RESET_DETECTED) {
3160 		iflib_request_reset_if_up(sc->ctx);
3161 		iflib_admin_intr_deferred(sc->ctx);
3162 	}
3163 	return (true);
3164 }
3165 
3166 /*
3167  * A PCIe-region parity failure stops PCIe and DMA traffic.  I350, I354,
3168  * I210, and I211 require a port reset before master disable in this case.
3169  * 82580 stops PCIe traffic for a fatal error in any host-owned region, so use
3170  * the same order for every 82580 recovery.  This differs from the normal
3171  * reset path, which disables the bus master first.
3172  *
3173  * Indications that relatch after admin accounting are discarded during
3174  * reset; sticky bits cannot distinguish them from the saved event.
3175  */
3176 static void
em_prepare_fatal_error_reset(struct e1000_softc * sc)3177 em_prepare_fatal_error_reset(struct e1000_softc *sc)
3178 {
3179 	struct e1000_hw *hw;
3180 	s32 error;
3181 	u32 ctrl, pcieecc, pcieerr;
3182 	int i;
3183 
3184 	if (!em_has_peind_memory_errors(&sc->hw) ||
3185 	    atomic_load_acq_32(&sc->fatal_error_state) !=
3186 	    EM_FATAL_ERROR_RESET_REQUESTED)
3187 		return;
3188 
3189 	pcieerr = sc->fatal_error_pcie |
3190 	    (E1000_READ_REG(&sc->hw, E1000_PCIEERRSTS) &
3191 	    em_pcie_fatal_error_mask(&sc->hw));
3192 	pcieecc = sc->fatal_error_pcie_ecc;
3193 	if (!em_has_82580_memory_errors(&sc->hw) &&
3194 	    (sc->fatal_error_peind & E1000_PEIND_PCIE_PARITY_FATAL) == 0 &&
3195 	    pcieerr == 0)
3196 		return;
3197 
3198 	hw = &sc->hw;
3199 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
3200 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl | E1000_CTRL_RST);
3201 	/* Do not access device registers for at least 3 ms after RST. */
3202 	msec_delay(3);
3203 	for (i = 0; i < AUTO_READ_DONE_TIMEOUT; i++) {
3204 		if ((E1000_READ_REG(hw, E1000_EECD) &
3205 		    E1000_EECD_AUTO_RD) != 0 &&
3206 		    (em_has_82580_memory_errors(hw) ||
3207 		    (E1000_READ_REG(hw, E1000_STATUS) &
3208 		    E1000_STATUS_RST_DONE) != 0))
3209 			break;
3210 		msec_delay(1);
3211 	}
3212 	if (i == AUTO_READ_DONE_TIMEOUT)
3213 		device_printf(sc->dev,
3214 		    "port reset did not complete during parity recovery\n");
3215 	error = e1000_disable_pcie_master_generic(hw);
3216 	if (error != E1000_SUCCESS)
3217 		device_printf(sc->dev,
3218 		    "PCIe master disable failed during parity recovery: %d\n",
3219 		    error);
3220 	pcieerr |= E1000_READ_REG(hw, E1000_PCIEERRSTS) &
3221 	    em_pcie_fatal_error_mask(hw);
3222 	if (pcieerr != 0)
3223 		E1000_WRITE_REG(hw, E1000_PCIEERRSTS, pcieerr);
3224 	if (em_has_82580_memory_errors(hw)) {
3225 		pcieecc |= E1000_READ_REG(hw, E1000_PCIEECCSTS) &
3226 		    E1000_PCIEECCSTS_82580_ERROR_MASK;
3227 		if (pcieecc != 0)
3228 			E1000_WRITE_REG(hw, E1000_PCIEECCSTS, pcieecc);
3229 	}
3230 	atomic_store_rel_32(&sc->fatal_error_state,
3231 	    EM_FATAL_ERROR_RESET_PREPARED);
3232 }
3233 
3234 static void
em_finish_fatal_error_reset(struct e1000_softc * sc)3235 em_finish_fatal_error_reset(struct e1000_softc *sc)
3236 {
3237 	struct e1000_hw *hw;
3238 	u32 dma_host, dma_rx, dma_tx, lanerr, pcieecc, pcieerr;
3239 	u32 state;
3240 
3241 	state = atomic_load_acq_32(&sc->fatal_error_state);
3242 	if (state != EM_FATAL_ERROR_RESET_REQUESTED &&
3243 	    state != EM_FATAL_ERROR_RESET_PREPARED)
3244 		return;
3245 
3246 	hw = &sc->hw;
3247 	if (em_has_82575_memory_errors(hw)) {
3248 		sc->fatal_error_dma_tx = 0;
3249 		sc->fatal_error_dma_rx = 0;
3250 	} else if (em_has_82576_memory_errors(hw)) {
3251 		/* Drain any indication relatched while the port was resetting. */
3252 		(void)E1000_READ_REG(hw, E1000_PEIND);
3253 		sc->fatal_error_peind = 0;
3254 	} else if (em_has_82580_memory_errors(hw)) {
3255 		pcieerr = sc->fatal_error_pcie |
3256 		    E1000_READ_REG(hw, E1000_PCIEERRSTS);
3257 		if (pcieerr != 0)
3258 			E1000_WRITE_REG(hw, E1000_PCIEERRSTS, pcieerr);
3259 		pcieecc = sc->fatal_error_pcie_ecc |
3260 		    (E1000_READ_REG(hw, E1000_PCIEECCSTS) &
3261 		    E1000_PCIEECCSTS_82580_ERROR_MASK);
3262 		if (pcieecc != 0)
3263 			E1000_WRITE_REG(hw, E1000_PCIEECCSTS, pcieecc);
3264 		dma_tx = sc->fatal_error_dma_tx |
3265 		    E1000_READ_REG(hw, E1000_DTPARS_82580);
3266 		if (dma_tx != 0)
3267 			E1000_WRITE_REG(hw, E1000_DTPARS_82580, dma_tx);
3268 		dma_rx = sc->fatal_error_dma_rx |
3269 		    E1000_READ_REG(hw, E1000_DRPARS_82580);
3270 		if (dma_rx != 0)
3271 			E1000_WRITE_REG(hw, E1000_DRPARS_82580, dma_rx);
3272 		dma_host = sc->fatal_error_dma_host |
3273 		    E1000_READ_REG(hw, E1000_DDPARS_82580);
3274 		if (dma_host != 0)
3275 			E1000_WRITE_REG(hw, E1000_DDPARS_82580, dma_host);
3276 		/* LANPERRSTS is read-only and is cleared by the port reset. */
3277 		lanerr = E1000_READ_REG(hw, E1000_LANPERRSTS) &
3278 		    E1000_LANPERRSTS_82580_ERROR_MASK;
3279 		if (lanerr != 0)
3280 			device_printf(sc->dev,
3281 			    "LAN parity status remained set after reset: %#x\n",
3282 			    lanerr);
3283 		(void)E1000_READ_REG(hw, E1000_PEIND);
3284 		sc->fatal_error_peind = 0;
3285 		sc->fatal_error_pcie = 0;
3286 		sc->fatal_error_pcie_ecc = 0;
3287 		sc->fatal_error_lan = 0;
3288 		sc->fatal_error_dma_tx = 0;
3289 		sc->fatal_error_dma_rx = 0;
3290 		sc->fatal_error_dma_host = 0;
3291 	} else if (em_has_peind_memory_errors(hw)) {
3292 		pcieerr = sc->fatal_error_pcie |
3293 		    (E1000_READ_REG(hw, E1000_PCIEERRSTS) &
3294 		    em_pcie_fatal_error_mask(hw));
3295 		if (pcieerr != 0)
3296 			E1000_WRITE_REG(hw, E1000_PCIEERRSTS, pcieerr);
3297 		if (em_has_i350_i354_memory_errors(hw)) {
3298 			dma_tx = sc->fatal_error_dma_tx |
3299 			    (E1000_READ_REG(hw, E1000_DTPARS) &
3300 			    E1000_DTPARS_FATAL_MASK);
3301 			if (dma_tx != 0)
3302 				E1000_WRITE_REG(hw, E1000_DTPARS, dma_tx);
3303 			dma_rx = sc->fatal_error_dma_rx |
3304 			    (E1000_READ_REG(hw, E1000_DRPARS) &
3305 			    E1000_DRPARS_FATAL_MASK);
3306 			if (dma_rx != 0)
3307 				E1000_WRITE_REG(hw, E1000_DRPARS, dma_rx);
3308 			lanerr = sc->fatal_error_lan |
3309 			    (E1000_READ_REG(hw, E1000_LANPERRSTS) &
3310 			    E1000_LANPERRSTS_I350_I354_FATAL_MASK);
3311 		} else {
3312 			lanerr = sc->fatal_error_lan |
3313 			    (E1000_READ_REG(hw, E1000_LANPERRSTS) &
3314 			    E1000_LANPERRSTS_RETX_BUF);
3315 		}
3316 		if (lanerr != 0)
3317 			E1000_WRITE_REG(hw, E1000_LANPERRSTS, lanerr);
3318 		/*
3319 		 * RST can relatch PEIND from a subordinate status register
3320 		 * before that register is cleared.  Drain the recovered
3321 		 * indication before unmasking FER.
3322 		 */
3323 		(void)E1000_READ_REG(hw, E1000_PEIND);
3324 		sc->fatal_error_peind = 0;
3325 		sc->fatal_error_pcie = 0;
3326 		sc->fatal_error_pcie_ecc = 0;
3327 		sc->fatal_error_lan = 0;
3328 		sc->fatal_error_dma_tx = 0;
3329 		sc->fatal_error_dma_rx = 0;
3330 		sc->fatal_error_dma_host = 0;
3331 	}
3332 	sc->fatal_error_icr = 0;
3333 	sc->fatal_error_pbeccsts = 0;
3334 	atomic_store_rel_32(&sc->fatal_error_state, EM_FATAL_ERROR_NONE);
3335 }
3336 
3337 /*********************************************************************
3338  *
3339  *  Fast Legacy/MSI Combined Interrupt Service routine
3340  *
3341  *********************************************************************/
3342 int
em_intr(void * arg)3343 em_intr(void *arg)
3344 {
3345 	struct e1000_softc *sc = arg;
3346 	struct e1000_hw *hw = &sc->hw;
3347 	struct em_rx_queue *que = &sc->rx_queues[0];
3348 	struct rx_ring *rxr = &que->rxr;
3349 	if_ctx_t ctx = sc->ctx;
3350 	u32 reg_icr;
3351 
3352 	reg_icr = E1000_READ_REG(hw, E1000_ICR);
3353 
3354 	/* Hot eject? */
3355 	if (reg_icr == 0xffffffff)
3356 		return FILTER_STRAY;
3357 
3358 	/* Definitely not our interrupt. */
3359 	if (reg_icr == 0x0)
3360 		return FILTER_STRAY;
3361 
3362 	/*
3363 	 * Starting with the 82571 chip, bit 31 should be used to
3364 	 * determine whether the interrupt belongs to us.
3365 	 */
3366 	if (hw->mac.type >= e1000_82571 &&
3367 	    (reg_icr & E1000_ICR_INT_ASSERTED) == 0)
3368 		return FILTER_STRAY;
3369 	if (igb_handle_device_reset(sc, reg_icr))
3370 		return (FILTER_HANDLED);
3371 	if (igb_device_reset_pending(sc))
3372 		return (FILTER_HANDLED);
3373 
3374 	/*
3375 	 * IAM auto-masks igb shared interrupts when ICR is read.  Older em
3376 	 * hardware still needs an explicit disable, which also works around
3377 	 * MSI message reordering errata on certain systems.
3378 	 */
3379 	if (sc->vf_ifp || hw->mac.type < igb_mac_min)
3380 		IFDI_INTR_DISABLE(ctx);
3381 
3382 	/* Link status change */
3383 	if (reg_icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))
3384 		em_handle_link(ctx);
3385 
3386 	if (reg_icr & E1000_ICR_RXO)
3387 		sc->rx_overruns++;
3388 
3389 	em_handle_fatal_error_intr(sc, reg_icr);
3390 
3391 	if (hw->mac.type >= e1000_82540)
3392 		em_newitr(sc, que, rxr);
3393 
3394 	return (FILTER_SCHEDULE_THREAD);
3395 }
3396 
3397 static int
em_if_rx_queue_intr_enable(if_ctx_t ctx,uint16_t rxqid)3398 em_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid)
3399 {
3400 	struct e1000_softc *sc = iflib_get_softc(ctx);
3401 	struct em_rx_queue *rxq = &sc->rx_queues[rxqid];
3402 
3403 	E1000_WRITE_REG(&sc->hw, E1000_IMS, rxq->eims);
3404 	return (0);
3405 }
3406 
3407 static int
em_if_tx_queue_intr_enable(if_ctx_t ctx,uint16_t txqid)3408 em_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid)
3409 {
3410 	struct e1000_softc *sc = iflib_get_softc(ctx);
3411 	struct em_tx_queue *txq = &sc->tx_queues[txqid];
3412 
3413 	E1000_WRITE_REG(&sc->hw, E1000_IMS, txq->eims);
3414 	return (0);
3415 }
3416 
3417 static int
igb_if_rx_queue_intr_enable(if_ctx_t ctx,uint16_t rxqid)3418 igb_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid)
3419 {
3420 	struct e1000_softc *sc = iflib_get_softc(ctx);
3421 	struct em_rx_queue *rxq = &sc->rx_queues[rxqid];
3422 
3423 	if (igb_device_reset_pending(sc))
3424 		return (0);
3425 	E1000_WRITE_REG(&sc->hw, E1000_EIMS, rxq->eims);
3426 	return (0);
3427 }
3428 
3429 static int
igb_if_tx_queue_intr_enable(if_ctx_t ctx,uint16_t txqid)3430 igb_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid)
3431 {
3432 	struct e1000_softc *sc = iflib_get_softc(ctx);
3433 	struct em_tx_queue *txq = &sc->tx_queues[txqid];
3434 
3435 	if (igb_device_reset_pending(sc))
3436 		return (0);
3437 	E1000_WRITE_REG(&sc->hw, E1000_EIMS, txq->eims);
3438 	return (0);
3439 }
3440 
3441 /*********************************************************************
3442  *
3443  *  MSI-X RX Interrupt Service routine
3444  *
3445  **********************************************************************/
3446 static int
em_msix_que(void * arg)3447 em_msix_que(void *arg)
3448 {
3449 	struct em_rx_queue *que = arg;
3450 	struct e1000_softc *sc = que->sc;
3451 	struct rx_ring *rxr = &que->rxr;
3452 
3453 	++que->irqs;
3454 
3455 	if (igb_device_reset_pending(sc))
3456 		return (FILTER_HANDLED);
3457 	em_newitr(sc, que, rxr);
3458 
3459 	return (FILTER_SCHEDULE_THREAD);
3460 }
3461 
3462 /*********************************************************************
3463  *
3464  *  MSI-X Link Fast Interrupt Service routine
3465  *
3466  **********************************************************************/
3467 static int
em_msix_link(void * arg)3468 em_msix_link(void *arg)
3469 {
3470 	struct e1000_softc *sc = arg;
3471 	u32 reg_icr;
3472 
3473 	++sc->link_irq;
3474 	MPASS(sc->hw.back != NULL);
3475 	/*
3476 	 * The VF's admin vector represents mailbox and link activity.  It has
3477 	 * no PF ICR at E1000_ICR, so process every admin-vector interrupt,
3478 	 * matching the igbvf misc-vector model.
3479 	 */
3480 	if (sc->vf_ifp) {
3481 		sc->hw.mac.get_link_status = true;
3482 		iflib_admin_intr_deferred(sc->ctx);
3483 		E1000_WRITE_REG(&sc->hw, E1000_EIMS, sc->link_mask);
3484 		return (FILTER_HANDLED);
3485 	}
3486 
3487 	reg_icr = E1000_READ_REG(&sc->hw, E1000_ICR);
3488 	if (igb_device_reset_pending(sc))
3489 		return (FILTER_HANDLED);
3490 
3491 	/*
3492 	 * Enabling or disabling SR-IOV can briefly make PF MMIO reads return
3493 	 * all ones.  This is not an interrupt cause; in particular, do not
3494 	 * turn it into a malicious-driver event.
3495 	 */
3496 	if (__predict_false(reg_icr == 0xffffffff))
3497 		goto rearm;
3498 	if (igb_handle_device_reset(sc, reg_icr))
3499 		return (FILTER_HANDLED);
3500 
3501 	if (reg_icr & E1000_ICR_RXO)
3502 		sc->rx_overruns++;
3503 
3504 	if (reg_icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))
3505 		em_handle_link(sc->ctx);
3506 	if (reg_icr & E1000_ICR_MDDET)
3507 		igb_iov_mdd_event(sc);
3508 	if (reg_icr & E1000_ICR_VMMB)
3509 		iflib_admin_intr_deferred(sc->ctx);
3510 	em_handle_fatal_error_intr(sc, reg_icr);
3511 
3512 rearm:
3513 	/* Re-arm unconditionally */
3514 	if (sc->hw.mac.type >= igb_mac_min) {
3515 		E1000_WRITE_REG(&sc->hw, E1000_IMS,
3516 		    E1000_IMS_LSC | igb_device_reset_intr_mask(sc) |
3517 		    igb_iov_intr_mask(sc) |
3518 		    em_fatal_error_intr_mask(sc));
3519 		E1000_WRITE_REG(&sc->hw, E1000_EIMS, sc->link_mask);
3520 	} else if (sc->hw.mac.type == e1000_82574) {
3521 		E1000_WRITE_REG(&sc->hw, E1000_IMS,
3522 		    E1000_IMS_LSC | E1000_IMS_OTHER);
3523 		/*
3524 		 * Because we must read the ICR for this interrupt it may
3525 		 * clear other causes using autoclear, for this reason we
3526 		 * simply create a soft interrupt for all these vectors.
3527 		 */
3528 		if (reg_icr)
3529 			E1000_WRITE_REG(&sc->hw, E1000_ICS, sc->ims);
3530 	} else
3531 		E1000_WRITE_REG(&sc->hw, E1000_IMS,
3532 		    E1000_IMS_LSC | em_fatal_error_intr_mask(sc));
3533 
3534 	return (FILTER_HANDLED);
3535 }
3536 
3537 static void
em_handle_link(void * context)3538 em_handle_link(void *context)
3539 {
3540 	if_ctx_t ctx = context;
3541 	struct e1000_softc *sc = iflib_get_softc(ctx);
3542 
3543 	sc->hw.mac.get_link_status = 1;
3544 	iflib_admin_intr_deferred(ctx);
3545 }
3546 
3547 /*********************************************************************
3548  *
3549  *  Media Ioctl callback
3550  *
3551  *  This routine is called whenever the user queries the status of
3552  *  the interface using ifconfig.
3553  *
3554  **********************************************************************/
3555 static void
em_if_media_status(if_ctx_t ctx,struct ifmediareq * ifmr)3556 em_if_media_status(if_ctx_t ctx, struct ifmediareq *ifmr)
3557 {
3558 	struct e1000_softc *sc = iflib_get_softc(ctx);
3559 	u_char fiber_type = IFM_1000_SX;
3560 
3561 	INIT_DEBUGOUT("em_if_media_status: begin");
3562 
3563 	iflib_admin_intr_deferred(ctx);
3564 
3565 	ifmr->ifm_status = IFM_AVALID;
3566 	ifmr->ifm_active = IFM_ETHER;
3567 
3568 	if (sc->link_state == EM_LINK_STATE_DOWN ||
3569 	    sc->link_state == EM_LINK_STATE_DOWN_RESET_PENDING) {
3570 		return;
3571 	}
3572 
3573 	ifmr->ifm_status |= IFM_ACTIVE;
3574 
3575 	if ((sc->hw.phy.media_type == e1000_media_type_fiber) ||
3576 	    (sc->hw.phy.media_type == e1000_media_type_internal_serdes)) {
3577 		if (sc->hw.mac.type == e1000_82545)
3578 			fiber_type = IFM_1000_LX;
3579 		ifmr->ifm_active |= fiber_type | IFM_FDX;
3580 	} else {
3581 		switch (sc->link_speed) {
3582 		case 10:
3583 			ifmr->ifm_active |= IFM_10_T;
3584 			break;
3585 		case 100:
3586 			ifmr->ifm_active |= IFM_100_TX;
3587 			break;
3588 		case 1000:
3589 			ifmr->ifm_active |= IFM_1000_T;
3590 			break;
3591 		}
3592 		if (sc->link_duplex == FULL_DUPLEX)
3593 			ifmr->ifm_active |= IFM_FDX;
3594 		else
3595 			ifmr->ifm_active |= IFM_HDX;
3596 	}
3597 }
3598 
3599 /*********************************************************************
3600  *
3601  *  Media Ioctl callback
3602  *
3603  *  This routine is called when the user changes speed/duplex using
3604  *  media/mediopt option with ifconfig.
3605  *
3606  **********************************************************************/
3607 static int
em_if_media_change(if_ctx_t ctx)3608 em_if_media_change(if_ctx_t ctx)
3609 {
3610 	struct e1000_softc *sc = iflib_get_softc(ctx);
3611 	struct ifmedia *ifm = iflib_get_media(ctx);
3612 
3613 	INIT_DEBUGOUT("em_if_media_change: begin");
3614 
3615 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
3616 		return (EINVAL);
3617 
3618 	switch (IFM_SUBTYPE(ifm->ifm_media)) {
3619 	case IFM_AUTO:
3620 		sc->hw.mac.autoneg = DO_AUTO_NEG;
3621 		sc->hw.phy.autoneg_advertised = AUTONEG_ADV_DEFAULT;
3622 		break;
3623 	case IFM_1000_LX:
3624 	case IFM_1000_SX:
3625 	case IFM_1000_T:
3626 		sc->hw.mac.autoneg = DO_AUTO_NEG;
3627 		sc->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
3628 		break;
3629 	case IFM_100_TX:
3630 		sc->hw.mac.autoneg = false;
3631 		sc->hw.phy.autoneg_advertised = 0;
3632 		if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
3633 			sc->hw.mac.forced_speed_duplex = ADVERTISE_100_FULL;
3634 		else
3635 			sc->hw.mac.forced_speed_duplex = ADVERTISE_100_HALF;
3636 		break;
3637 	case IFM_10_T:
3638 		sc->hw.mac.autoneg = false;
3639 		sc->hw.phy.autoneg_advertised = 0;
3640 		if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
3641 			sc->hw.mac.forced_speed_duplex = ADVERTISE_10_FULL;
3642 		else
3643 			sc->hw.mac.forced_speed_duplex = ADVERTISE_10_HALF;
3644 		break;
3645 	default:
3646 		device_printf(sc->dev, "Unsupported media type\n");
3647 	}
3648 
3649 	return (0);
3650 }
3651 
3652 static int
em_if_set_promisc(if_ctx_t ctx,int flags)3653 em_if_set_promisc(if_ctx_t ctx, int flags)
3654 {
3655 	struct e1000_softc *sc;
3656 
3657 	sc = iflib_get_softc(ctx);
3658 	if (em_if_defer_promisc(sc))
3659 		return (0);
3660 	return (em_if_set_promisc_impl(ctx, flags));
3661 }
3662 
3663 static bool
em_if_defer_promisc(struct e1000_softc * sc)3664 em_if_defer_promisc(struct e1000_softc *sc)
3665 {
3666 	switch (sc->hw.mac.type) {
3667 	case e1000_82576:
3668 	case e1000_i350:
3669 	case e1000_vfadapt:
3670 	case e1000_vfadapt_i350:
3671 		break;
3672 	default:
3673 		return (false);
3674 	}
3675 
3676 	/*
3677 	 * iflib drops its context lock around IFDI_PROMISC_SET.  Run mailbox
3678 	 * and IOV register operations later from the locked admin task.
3679 	 * A deferred VF mailbox rejection cannot be returned to ifconfig; the
3680 	 * admin task logs it instead.
3681 	 */
3682 	atomic_set_32(&sc->promisc_pending, 1);
3683 	iflib_admin_intr_deferred(sc->ctx);
3684 	return (true);
3685 }
3686 
3687 int
em_if_set_promisc_impl(if_ctx_t ctx,int flags)3688 em_if_set_promisc_impl(if_ctx_t ctx, int flags)
3689 {
3690 	struct e1000_softc *sc = iflib_get_softc(ctx);
3691 	if_t ifp = iflib_get_ifp(ctx);
3692 	enum e1000_promisc_type type;
3693 	s32 error;
3694 	u32 reg_rctl;
3695 	int mcnt = 0;
3696 
3697 	if (sc->vf_ifp) {
3698 		if (flags & IFF_PROMISC)
3699 			type = e1000_promisc_enabled;
3700 		else if (flags & IFF_ALLMULTI)
3701 			type = e1000_promisc_multicast;
3702 		else
3703 			type = e1000_promisc_disabled;
3704 		error = e1000_promisc_set_vf(&sc->hw, type);
3705 		if (error != E1000_SUCCESS) {
3706 			device_printf(sc->dev,
3707 			    "VF promiscuous-mode request failed\n");
3708 			return (EPERM);
3709 		}
3710 		return (0);
3711 	}
3712 
3713 	reg_rctl = E1000_READ_REG(&sc->hw, E1000_RCTL);
3714 	reg_rctl &= ~(E1000_RCTL_SBP | E1000_RCTL_UPE);
3715 	if (flags & IFF_ALLMULTI)
3716 		mcnt = MAX_NUM_MULTICAST_ADDRESSES;
3717 	else
3718 		mcnt = min(if_llmaddr_count(ifp),
3719 		    MAX_NUM_MULTICAST_ADDRESSES);
3720 
3721 	if (mcnt < MAX_NUM_MULTICAST_ADDRESSES)
3722 		reg_rctl &= (~E1000_RCTL_MPE);
3723 	E1000_WRITE_REG(&sc->hw, E1000_RCTL, reg_rctl);
3724 
3725 	if (flags & IFF_PROMISC) {
3726 		reg_rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
3727 		/* Turn this on if you want to see bad packets */
3728 		if (em_debug_sbp)
3729 			reg_rctl |= E1000_RCTL_SBP;
3730 		E1000_WRITE_REG(&sc->hw, E1000_RCTL, reg_rctl);
3731 		if (igb_iov_enabled(sc))
3732 			em_if_vlan_filter_enable(sc);
3733 		else
3734 			em_if_vlan_filter_disable(sc);
3735 	} else {
3736 		if (flags & IFF_ALLMULTI) {
3737 			reg_rctl |= E1000_RCTL_MPE;
3738 			reg_rctl &= ~E1000_RCTL_UPE;
3739 			E1000_WRITE_REG(&sc->hw, E1000_RCTL, reg_rctl);
3740 		}
3741 		if (igb_iov_enabled(sc) || em_if_vlan_filter_used(ctx))
3742 			em_if_vlan_filter_enable(sc);
3743 	}
3744 	igb_iov_update_pf_vmolr(sc);
3745 	igb_iov_rebuild_vlan(sc);
3746 	return (0);
3747 }
3748 
3749 static u_int
em_copy_maddr(void * arg,struct sockaddr_dl * sdl,u_int idx)3750 em_copy_maddr(void *arg, struct sockaddr_dl *sdl, u_int idx)
3751 {
3752 	u8 *mta = arg;
3753 
3754 	if (idx == MAX_NUM_MULTICAST_ADDRESSES)
3755 		return (0);
3756 
3757 	bcopy(LLADDR(sdl), &mta[idx * ETHER_ADDR_LEN], ETHER_ADDR_LEN);
3758 
3759 	return (1);
3760 }
3761 
3762 /* Make every multicast hash eligible on parts whose wake matcher needs MTA. */
3763 static void
em_fill_wakeup_mta(struct e1000_hw * hw)3764 em_fill_wakeup_mta(struct e1000_hw *hw)
3765 {
3766 	int i;
3767 
3768 	memset(hw->mac.mta_shadow, 0xff, sizeof(hw->mac.mta_shadow));
3769 	for (i = hw->mac.mta_reg_count - 1; i >= 0; i--)
3770 		E1000_WRITE_REG_ARRAY(hw, E1000_MTA, i,
3771 		    hw->mac.mta_shadow[i]);
3772 	E1000_WRITE_FLUSH(hw);
3773 }
3774 
3775 /*********************************************************************
3776  *  Multicast Update
3777  *
3778  *  This routine is called whenever multicast address list is updated.
3779  *
3780  **********************************************************************/
3781 static void
em_if_multi_set(if_ctx_t ctx)3782 em_if_multi_set(if_ctx_t ctx)
3783 {
3784 	struct e1000_softc *sc = iflib_get_softc(ctx);
3785 	if_t ifp = iflib_get_ifp(ctx);
3786 	u8 *mta; /* Multicast array memory */
3787 	u32 reg_rctl = 0;
3788 	int mcnt = 0;
3789 
3790 	IOCTL_DEBUGOUT("em_set_multi: begin");
3791 
3792 	mta = sc->mta;
3793 	bzero(mta, sizeof(u8) * ETHER_ADDR_LEN * MAX_NUM_MULTICAST_ADDRESSES);
3794 
3795 	if (sc->hw.mac.type == e1000_82542 &&
3796 	    sc->hw.revision_id == E1000_REVISION_2) {
3797 		reg_rctl = E1000_READ_REG(&sc->hw, E1000_RCTL);
3798 		if (sc->hw.bus.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
3799 			e1000_pci_clear_mwi(&sc->hw);
3800 		reg_rctl |= E1000_RCTL_RST;
3801 		E1000_WRITE_REG(&sc->hw, E1000_RCTL, reg_rctl);
3802 		msec_delay(5);
3803 	}
3804 
3805 	mcnt = if_foreach_llmaddr(ifp, em_copy_maddr, mta);
3806 
3807 	if (sc->vf_ifp) {
3808 		e1000_update_mc_addr_list(&sc->hw, mta, mcnt);
3809 		igbv_update_uc_addr_list(sc, ifp);
3810 		return;
3811 	}
3812 
3813 	if (mcnt < MAX_NUM_MULTICAST_ADDRESSES &&
3814 	    !igb_iov_enabled(sc))
3815 		e1000_update_mc_addr_list(&sc->hw, mta, mcnt);
3816 
3817 	reg_rctl = E1000_READ_REG(&sc->hw, E1000_RCTL);
3818 
3819 	if (if_getflags(ifp) & IFF_PROMISC)
3820 		reg_rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
3821 	else if (mcnt >= MAX_NUM_MULTICAST_ADDRESSES ||
3822 	    if_getflags(ifp) & IFF_ALLMULTI) {
3823 		reg_rctl |= E1000_RCTL_MPE;
3824 		reg_rctl &= ~E1000_RCTL_UPE;
3825 	} else
3826 		reg_rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE);
3827 
3828 	E1000_WRITE_REG(&sc->hw, E1000_RCTL, reg_rctl);
3829 
3830 	if (sc->hw.mac.type == e1000_82542 &&
3831 	    sc->hw.revision_id == E1000_REVISION_2) {
3832 		reg_rctl = E1000_READ_REG(&sc->hw, E1000_RCTL);
3833 		reg_rctl &= ~E1000_RCTL_RST;
3834 		E1000_WRITE_REG(&sc->hw, E1000_RCTL, reg_rctl);
3835 		msec_delay(5);
3836 		if (sc->hw.bus.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
3837 			e1000_pci_set_mwi(&sc->hw);
3838 	}
3839 	igb_iov_rebuild_mta(sc);
3840 	igb_iov_update_pf_vmolr(sc);
3841 }
3842 
3843 /*********************************************************************
3844  *  Timer routine
3845  *
3846  *  This routine schedules em_if_update_admin_status() to check for
3847  *  link status and to gather statistics as well as to perform some
3848  *  controller-specific hardware patting.
3849  *
3850  **********************************************************************/
3851 static void
em_if_timer(if_ctx_t ctx,uint16_t qid)3852 em_if_timer(if_ctx_t ctx, uint16_t qid)
3853 {
3854 	struct e1000_softc *sc;
3855 
3856 	if (qid != 0)
3857 		return;
3858 
3859 	sc = iflib_get_softc(ctx);
3860 	atomic_set_32(&sc->stats_pending, 1);
3861 	iflib_admin_intr_deferred(ctx);
3862 }
3863 
3864 static void
em_if_update_admin_status(if_ctx_t ctx)3865 em_if_update_admin_status(if_ctx_t ctx)
3866 {
3867 	struct e1000_softc *sc = iflib_get_softc(ctx);
3868 	struct e1000_hw *hw = &sc->hw;
3869 	device_t dev = iflib_get_dev(ctx);
3870 	u32 link_check, thstat, ctrl;
3871 	bool reset_requested = false;
3872 
3873 	KASSERT(!sc->vf_ifp, ("%s called for a VF", __func__));
3874 	if (em_handle_fatal_error_admin(sc))
3875 		return;
3876 	/* A sibling-port reset invalidated the registers and VF mailboxes. */
3877 	if (atomic_cmpset_acq_32(&sc->device_reset_state,
3878 	    IGB_DEVICE_RESET_DETECTED, IGB_DEVICE_RESET_REQUESTED)) {
3879 		if (sc->link_state == EM_LINK_STATE_UP)
3880 			iflib_link_state_change(ctx, LINK_STATE_DOWN, 0);
3881 		sc->link_speed = 0;
3882 		sc->link_duplex = 0;
3883 		sc->link_state = EM_LINK_STATE_DOWN_RESET_PENDING;
3884 		/* Request the reset here; interrupt filters cannot take STATE_LOCK. */
3885 		iflib_request_reset_if_up(ctx);
3886 		/* Re-enter the admin task so it observes the reset request. */
3887 		iflib_admin_intr_deferred(ctx);
3888 		return;
3889 	}
3890 	if (atomic_load_acq_32(&sc->device_reset_state) !=
3891 	    IGB_DEVICE_RESET_NONE)
3892 		return;
3893 
3894 	if (atomic_readandclear_32(&sc->promisc_pending) != 0)
3895 		(void)em_if_set_promisc_impl(ctx,
3896 		    if_getflags(iflib_get_ifp(ctx)));
3897 	igb_iov_handle_mdd(sc);
3898 	igb_iov_handle_mbx(sc);
3899 
3900 	link_check = thstat = ctrl = 0;
3901 	/* Get the cached link value or read phy for real */
3902 	switch (hw->phy.media_type) {
3903 	case e1000_media_type_copper:
3904 		if (hw->mac.get_link_status) {
3905 			if (hw->mac.type == e1000_pch_spt)
3906 				msec_delay(50);
3907 			/* Do the work to read phy */
3908 			e1000_check_for_link(hw);
3909 			link_check = !hw->mac.get_link_status;
3910 			if (link_check) /* ESB2 fix */
3911 				e1000_cfg_on_link_up(hw);
3912 		} else {
3913 			link_check = true;
3914 		}
3915 		break;
3916 	case e1000_media_type_fiber:
3917 		e1000_check_for_link(hw);
3918 		link_check =
3919 		    (E1000_READ_REG(hw, E1000_STATUS) & E1000_STATUS_LU);
3920 		break;
3921 	case e1000_media_type_internal_serdes:
3922 		e1000_check_for_link(hw);
3923 		link_check = hw->mac.serdes_has_link;
3924 		break;
3925 	default:
3926 		break;
3927 	}
3928 
3929 	/* Check for thermal downshift or shutdown */
3930 	if (hw->mac.type == e1000_i350) {
3931 		thstat = E1000_READ_REG(hw, E1000_THSTAT);
3932 		ctrl = E1000_READ_REG(hw, E1000_CTRL_EXT);
3933 	}
3934 
3935 	/* Now check for a transition */
3936 	if (link_check &&
3937 	    (sc->link_state == EM_LINK_STATE_DOWN ||
3938 	    sc->link_state == EM_LINK_STATE_DOWN_RESET_PENDING)) {
3939 		bool reset_pending;
3940 
3941 		reset_pending =
3942 		    sc->link_state == EM_LINK_STATE_DOWN_RESET_PENDING;
3943 		e1000_get_speed_and_duplex(hw, &sc->link_speed,
3944 		    &sc->link_duplex);
3945 		/* Check if we must disable SPEED_MODE bit on PCI-E */
3946 		if ((sc->link_speed != SPEED_1000) &&
3947 		    ((hw->mac.type == e1000_82571) ||
3948 		    (hw->mac.type == e1000_82572))) {
3949 			int tarc0;
3950 			tarc0 = E1000_READ_REG(hw, E1000_TARC(0));
3951 			tarc0 &= ~TARC_SPEED_MODE_BIT;
3952 			E1000_WRITE_REG(hw, E1000_TARC(0), tarc0);
3953 		}
3954 		if (bootverbose)
3955 			device_printf(dev, "Link is up %d Mbps %s\n",
3956 			    sc->link_speed,
3957 			    ((sc->link_duplex == FULL_DUPLEX) ?
3958 			    "Full Duplex" : "Half Duplex"));
3959 		sc->link_state = EM_LINK_STATE_UP;
3960 		sc->smartspeed = 0;
3961 		if (hw->mac.type == e1000_i350 &&
3962 		    (ctrl & E1000_CTRL_EXT_LINK_MODE_MASK) ==
3963 		    E1000_CTRL_EXT_LINK_MODE_GMII &&
3964 		    (thstat & E1000_THSTAT_LINK_THROTTLE))
3965 			device_printf(dev, "Link: thermal downshift\n");
3966 		/* Delay Link Up for Phy update */
3967 		if (((hw->mac.type == e1000_i210) ||
3968 		    (hw->mac.type == e1000_i211)) &&
3969 		    (hw->phy.id == I210_I_PHY_ID))
3970 			msec_delay(I210_LINK_DELAY);
3971 		/* Reset if the media type changed. */
3972 		if (hw->dev_spec._82575.media_changed &&
3973 		    hw->mac.type >= igb_mac_min) {
3974 			hw->dev_spec._82575.media_changed = false;
3975 			sc->flags |= IGB_MEDIA_RESET;
3976 			iflib_request_reset(ctx);
3977 			iflib_admin_intr_deferred(ctx);
3978 			reset_requested = true;
3979 		}
3980 		/* Only do TSO on gigabit for older chips due to errata */
3981 		if (hw->mac.type < igb_mac_min)
3982 			reset_requested = em_automask_tso(ctx);
3983 
3984 		if (reset_pending || reset_requested) {
3985 			/*
3986 			 * The PHY is up, but publish it only after the TSO
3987 			 * capability-change reset.
3988 			 */
3989 			sc->link_state = EM_LINK_STATE_UP_RESET_PENDING;
3990 		} else {
3991 			iflib_link_state_change(ctx, LINK_STATE_UP,
3992 			    IF_Mbps(sc->link_speed));
3993 		}
3994 		igb_iov_ping_all_vfs(sc);
3995 	} else if (!link_check &&
3996 	    (sc->link_state == EM_LINK_STATE_UP ||
3997 	    sc->link_state == EM_LINK_STATE_UP_RESET_PENDING)) {
3998 		bool link_was_published;
3999 		bool reset_pending;
4000 
4001 		link_was_published = sc->link_state == EM_LINK_STATE_UP;
4002 		reset_pending =
4003 		    sc->link_state == EM_LINK_STATE_UP_RESET_PENDING;
4004 		sc->link_speed = 0;
4005 		sc->link_duplex = 0;
4006 		sc->link_state = reset_pending ?
4007 		    EM_LINK_STATE_DOWN_RESET_PENDING : EM_LINK_STATE_DOWN;
4008 		if (link_was_published)
4009 			iflib_link_state_change(ctx, LINK_STATE_DOWN, 0);
4010 		igb_iov_ping_all_vfs(sc);
4011 	}
4012 	/*
4013 	 * Mailbox, link, and timer events share this admin task.  The PF
4014 	 * statistics sweep performs 66 MMIO reads, so run it only when the
4015 	 * ordinary iflib timer requests a sample rather than once per mailbox
4016 	 * message.  Exported counters can consequently trail hardware by the
4017 	 * timer interval (normally 500 ms).
4018 	 */
4019 	if (atomic_readandclear_32(&sc->stats_pending) != 0) {
4020 		em_update_stats_counters(sc);
4021 		/*
4022 		 * The 82574 PHY can enter a state in which both its receive
4023 		 * error and idle error counters saturate.  Require two
4024 		 * consecutive timer samples before resetting, matching Intel's
4025 		 * e1000e recovery policy and avoiding a reset on a transient
4026 		 * register sample.
4027 		 */
4028 		if (hw->mac.type == e1000_82574) {
4029 			if (e1000_check_phy_82574(hw))
4030 				sc->phy_hang_count++;
4031 			else
4032 				sc->phy_hang_count = 0;
4033 			if (sc->phy_hang_count > 1) {
4034 				sc->phy_hang_count = 0;
4035 				device_printf(dev,
4036 				    "PHY appears hung; requesting reset\n");
4037 				iflib_request_reset(ctx);
4038 				iflib_admin_intr_deferred(ctx);
4039 				return;
4040 			}
4041 		}
4042 	}
4043 
4044 	/* Reset LAA into RAR[0] on 82571 */
4045 	if (hw->mac.type == e1000_82571 && e1000_get_laa_state_82571(hw))
4046 		e1000_rar_set(hw, hw->mac.addr, 0);
4047 
4048 	if (hw->mac.type < em_mac_min)
4049 		lem_smartspeed(sc);
4050 }
4051 
4052 /*
4053  * Last-resort DMA fence.  iflib releases DMA mappings after the driver's
4054  * stop callback, so continuing with bus mastering still enabled would turn
4055  * a recoverable NIC failure into memory corruption.  Treat failure of the
4056  * PCI command bit as a fail-stop invariant violation.
4057  */
4058 static void
em_fence_pci_busmaster(struct e1000_softc * sc)4059 em_fence_pci_busmaster(struct e1000_softc *sc)
4060 {
4061 	device_t dev;
4062 	u_int timeout;
4063 	u16 command;
4064 	int error;
4065 
4066 	dev = sc->dev;
4067 	error = pci_disable_busmaster(dev);
4068 	command = pci_read_config(dev, PCIR_COMMAND, 2);
4069 	if (command != 0xffff && (command & PCIM_CMD_BUSMASTEREN) != 0)
4070 		panic("%s: unable to fence device DMA (error %d)",
4071 		    device_get_nameunit(dev), error);
4072 	if (error != 0 && command != 0xffff)
4073 		device_printf(dev,
4074 		    "PCI bus-master disable returned %d; readback is disabled\n",
4075 		    error);
4076 
4077 	timeout = max(pcie_get_max_completion_timeout(dev) / 1000, 10);
4078 	if (command != 0xffff &&
4079 	    !pcie_wait_for_pending_transactions(dev, timeout)) {
4080 		/* A function removed during the wait can no longer issue DMA. */
4081 		command = pci_read_config(dev, PCIR_COMMAND, 2);
4082 		if (command != 0xffff)
4083 			panic("%s: DMA transactions remain pending after fencing",
4084 			    device_get_nameunit(dev));
4085 	}
4086 }
4087 
4088 static int
em_enable_pci_busmaster(struct e1000_softc * sc)4089 em_enable_pci_busmaster(struct e1000_softc *sc)
4090 {
4091 	device_t dev;
4092 	u16 command;
4093 	int error;
4094 
4095 	dev = sc->dev;
4096 	command = pci_read_config(dev, PCIR_COMMAND, 2);
4097 	if (command == 0xffff)
4098 		return (ENXIO);
4099 	if ((command & PCIM_CMD_BUSMASTEREN) != 0)
4100 		return (0);
4101 
4102 	error = pci_enable_busmaster(dev);
4103 	command = pci_read_config(dev, PCIR_COMMAND, 2);
4104 	if (command == 0xffff)
4105 		return (ENXIO);
4106 	if ((command & PCIM_CMD_BUSMASTEREN) == 0)
4107 		return (error != 0 ? error : EIO);
4108 	return (0);
4109 }
4110 
4111 /*********************************************************************
4112  *
4113  *  This routine disables all traffic on the adapter by issuing a
4114  *  global reset on the MAC.
4115  *
4116  **********************************************************************/
4117 static void
em_if_stop(if_ctx_t ctx)4118 em_if_stop(if_ctx_t ctx)
4119 {
4120 	struct e1000_softc *sc = iflib_get_softc(ctx);
4121 	s32 error;
4122 
4123 	INIT_DEBUGOUT("em_if_stop: begin");
4124 
4125 	if (sc->vf_ifp) {
4126 		igbv_queue_retry_stop(sc);
4127 		igbv_mbx_retry_stop(sc);
4128 	}
4129 
4130 	/* I219 needs special flushing to avoid hangs */
4131 	if (sc->hw.mac.type >= e1000_pch_spt && sc->hw.mac.type < igb_mac_min)
4132 		em_flush_desc_rings(sc);
4133 
4134 	igb_iov_reset_prepare(sc);
4135 	if (!sc->vf_ifp ||
4136 	    (atomic_load_acq_32(&sc->vf_mbx_ready) != 0 &&
4137 	    (if_getflags(iflib_get_ifp(ctx)) & IFF_UP) == 0)) {
4138 		em_prepare_fatal_error_reset(sc);
4139 		error = e1000_reset_hw(&sc->hw);
4140 		if (!sc->vf_ifp && error != E1000_SUCCESS) {
4141 			device_printf(sc->dev, "Hardware reset failed while "
4142 			    "stopping: %d\n", error);
4143 			return;
4144 		}
4145 	}
4146 	if (sc->vf_ifp) {
4147 		sc->vf_queues_sanitized = igbv_sanitize_queues(sc);
4148 		atomic_store_rel_32(&sc->vf_mbx_ready, 0);
4149 		if (!sc->vf_queues_sanitized)
4150 			em_fence_pci_busmaster(sc);
4151 	}
4152 	if (sc->hw.mac.type >= e1000_82544 && !sc->vf_ifp)
4153 		E1000_WRITE_REG(&sc->hw, E1000_WUFC, 0);
4154 
4155 	if (!sc->vf_ifp) {
4156 		e1000_led_off(&sc->hw);
4157 		e1000_cleanup_led(&sc->hw);
4158 	} else {
4159 		sc->link_speed = 0;
4160 		sc->link_duplex = 0;
4161 		if (sc->link_state != EM_LINK_STATE_DOWN) {
4162 			sc->link_state = EM_LINK_STATE_DOWN;
4163 			iflib_link_state_change(ctx, LINK_STATE_DOWN, 0);
4164 		}
4165 	}
4166 }
4167 
4168 /*********************************************************************
4169  *
4170  *  Determine hardware revision.
4171  *
4172  **********************************************************************/
4173 static void
em_identify_hardware(if_ctx_t ctx)4174 em_identify_hardware(if_ctx_t ctx)
4175 {
4176 	device_t dev = iflib_get_dev(ctx);
4177 	struct e1000_softc *sc = iflib_get_softc(ctx);
4178 
4179 	/* Make sure our PCI config space has the necessary stuff set */
4180 	sc->hw.bus.pci_cmd_word = pci_read_config(dev, PCIR_COMMAND, 2);
4181 
4182 	/* Save off the information about this board */
4183 	sc->hw.vendor_id = pci_get_vendor(dev);
4184 	sc->hw.device_id = pci_get_device(dev);
4185 	sc->hw.revision_id = pci_read_config(dev, PCIR_REVID, 1);
4186 	sc->hw.subsystem_vendor_id = pci_read_config(dev, PCIR_SUBVEND_0, 2);
4187 	sc->hw.subsystem_device_id = pci_read_config(dev, PCIR_SUBDEV_0, 2);
4188 
4189 	/* Do Shared Code Init and Setup */
4190 	if (e1000_set_mac_type(&sc->hw)) {
4191 		device_printf(dev, "Setup init failure\n");
4192 		return;
4193 	}
4194 
4195 	/*
4196 	 * Function type comes from the selected iflib shared context, not from
4197 	 * enum ordering.  Keep the detected MAC type as an independent check
4198 	 * that the igb/igbv probe tables selected the right policy.
4199 	 */
4200 	KASSERT(sc->vf_ifp ==
4201 	    (sc->hw.mac.type == e1000_vfadapt ||
4202 	    sc->hw.mac.type == e1000_vfadapt_i350),
4203 	    ("%s: iflib function type and MAC type disagree", __func__));
4204 }
4205 
4206 static int
em_allocate_pci_resources(if_ctx_t ctx)4207 em_allocate_pci_resources(if_ctx_t ctx)
4208 {
4209 	struct e1000_softc *sc = iflib_get_softc(ctx);
4210 	device_t dev = iflib_get_dev(ctx);
4211 	int rid, val;
4212 
4213 	rid = PCIR_BAR(0);
4214 	sc->memory = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
4215 	    RF_ACTIVE);
4216 	if (sc->memory == NULL) {
4217 		device_printf(dev,
4218 		    "Unable to allocate bus resource: memory\n");
4219 		return (ENXIO);
4220 	}
4221 	sc->osdep.mem_bus_space_tag = rman_get_bustag(sc->memory);
4222 	sc->osdep.mem_bus_space_handle = rman_get_bushandle(sc->memory);
4223 #ifdef INVARIANTS
4224 	sc->osdep.mem_bus_space_size = rman_get_size(sc->memory);
4225 #endif
4226 	sc->hw.hw_addr = (u8 *)&sc->osdep.mem_bus_space_handle;
4227 
4228 	/* Only older adapters use IO mapping */
4229 	if (sc->hw.mac.type < em_mac_min && sc->hw.mac.type > e1000_82543) {
4230 		/* Figure our where our IO BAR is ? */
4231 		for (rid = PCIR_BAR(0); rid < PCIR_CIS;) {
4232 			val = pci_read_config(dev, rid, 4);
4233 			if (EM_BAR_TYPE(val) == EM_BAR_TYPE_IO) {
4234 				break;
4235 			}
4236 			rid += 4;
4237 			/* check for 64bit BAR */
4238 			if (EM_BAR_MEM_TYPE(val) == EM_BAR_MEM_TYPE_64BIT)
4239 				rid += 4;
4240 		}
4241 		if (rid >= PCIR_CIS) {
4242 			device_printf(dev, "Unable to locate IO BAR\n");
4243 			return (ENXIO);
4244 		}
4245 		sc->ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
4246 		    &rid, RF_ACTIVE);
4247 		if (sc->ioport == NULL) {
4248 			device_printf(dev,
4249 			    "Unable to allocate bus resource: ioport\n");
4250 			return (ENXIO);
4251 		}
4252 		sc->hw.io_base = 0;
4253 		sc->osdep.io_bus_space_tag =
4254 		    rman_get_bustag(sc->ioport);
4255 		sc->osdep.io_bus_space_handle =
4256 		    rman_get_bushandle(sc->ioport);
4257 	}
4258 
4259 	sc->hw.back = &sc->osdep;
4260 
4261 	return (0);
4262 }
4263 
4264 /*********************************************************************
4265  *
4266  *  Set up the MSI-X Interrupt handlers
4267  *
4268  **********************************************************************/
4269 static int
em_if_msix_intr_assign(if_ctx_t ctx,int msix)4270 em_if_msix_intr_assign(if_ctx_t ctx, int msix)
4271 {
4272 	struct e1000_softc *sc = iflib_get_softc(ctx);
4273 	struct em_rx_queue *rx_que = sc->rx_queues;
4274 	struct em_tx_queue *tx_que = sc->tx_queues;
4275 	int error, rid, i, vector = 0, rx_vectors;
4276 	char buf[16];
4277 
4278 	/* First set up ring resources */
4279 	for (i = 0; i < sc->rx_num_queues; i++, rx_que++, vector++) {
4280 		rid = vector + 1;
4281 		snprintf(buf, sizeof(buf), "rxq%d", i);
4282 		error = iflib_irq_alloc_generic(ctx, &rx_que->que_irq, rid,
4283 		    IFLIB_INTR_RXTX, em_msix_que, rx_que, rx_que->me, buf);
4284 		if (error) {
4285 			device_printf(iflib_get_dev(ctx),
4286 			    "Failed to allocate que int %d err: %d",
4287 			    i, error);
4288 			sc->rx_num_queues = i + 1;
4289 			goto fail;
4290 		}
4291 
4292 		rx_que->msix =  vector;
4293 
4294 		/*
4295 		 * Set the bit to enable interrupt
4296 		 * in E1000_IMS -- bits 20 and 21
4297 		 * are for RX0 and RX1, note this has
4298 		 * NOTHING to do with the MSI-X vector
4299 		 */
4300 		if (sc->hw.mac.type == e1000_82574) {
4301 			rx_que->eims = 1 << (20 + i);
4302 			sc->ims |= rx_que->eims;
4303 			sc->ivars |= (8 | rx_que->msix) << (i * 4);
4304 		} else if (sc->hw.mac.type == e1000_82575)
4305 			rx_que->eims = E1000_EICR_TX_QUEUE0 << vector;
4306 		else
4307 			rx_que->eims = 1 << vector;
4308 	}
4309 	rx_vectors = vector;
4310 
4311 	vector = 0;
4312 	for (i = 0; i < sc->tx_num_queues; i++, tx_que++, vector++) {
4313 		snprintf(buf, sizeof(buf), "txq%d", i);
4314 		tx_que = &sc->tx_queues[i];
4315 		iflib_softirq_alloc_generic(ctx,
4316 		    &sc->rx_queues[i % sc->rx_num_queues].que_irq,
4317 		    IFLIB_INTR_TX, tx_que, tx_que->me, buf);
4318 
4319 		tx_que->msix = (vector % sc->rx_num_queues);
4320 
4321 		/*
4322 		 * Set the bit to enable interrupt
4323 		 * in E1000_IMS -- bits 22 and 23
4324 		 * are for TX0 and TX1, note this has
4325 		 * NOTHING to do with the MSI-X vector
4326 		 */
4327 		if (sc->hw.mac.type == e1000_82574) {
4328 			tx_que->eims = 1 << (22 + i);
4329 			sc->ims |= tx_que->eims;
4330 			sc->ivars |= (8 | tx_que->msix) << (8 + (i * 4));
4331 		} else if (sc->hw.mac.type == e1000_82575) {
4332 			tx_que->eims = E1000_EICR_TX_QUEUE0 << i;
4333 		} else {
4334 			tx_que->eims = 1 << i;
4335 		}
4336 	}
4337 
4338 	/* Link interrupt */
4339 	rid = rx_vectors + 1;
4340 	error = iflib_irq_alloc_generic(ctx, &sc->irq, rid, IFLIB_INTR_ADMIN,
4341 	    em_msix_link, sc, 0, "aq");
4342 
4343 	if (error) {
4344 		device_printf(iflib_get_dev(ctx),
4345 		    "Failed to register admin handler");
4346 		goto fail;
4347 	}
4348 	sc->linkvec = rx_vectors;
4349 	if (sc->hw.mac.type < igb_mac_min) {
4350 		sc->ivars |=  (8 | rx_vectors) << 16;
4351 		sc->ivars |= 0x80000000;
4352 		/* Enable the "Other" interrupt type for link status change */
4353 		sc->ims |= E1000_IMS_OTHER;
4354 	}
4355 
4356 	return (0);
4357 fail:
4358 	iflib_irq_free(ctx, &sc->irq);
4359 	rx_que = sc->rx_queues;
4360 	for (int i = 0; i < sc->rx_num_queues; i++, rx_que++)
4361 		iflib_irq_free(ctx, &rx_que->que_irq);
4362 	return (error);
4363 }
4364 
4365 static void
igb_configure_queues(struct e1000_softc * sc)4366 igb_configure_queues(struct e1000_softc *sc)
4367 {
4368 	struct e1000_hw *hw = &sc->hw;
4369 	struct em_rx_queue *rx_que;
4370 	struct em_tx_queue *tx_que;
4371 	u32 tmp, ivar = 0;
4372 
4373 	/*
4374 	 * Queue ownership can change when SR-IOV is enabled or disabled.
4375 	 * Rebuild the interrupt mask for the current layout instead of
4376 	 * retaining vectors from a previous initialization.
4377 	 */
4378 	sc->que_mask = 0;
4379 	sc->link_mask = 0;
4380 
4381 	/* GPIE controls the PF interrupt block and is not in the VF BAR. */
4382 	if (!sc->vf_ifp && hw->mac.type != e1000_82575)
4383 		E1000_WRITE_REG(hw, E1000_GPIE,
4384 		    E1000_GPIE_MSIX_MODE | E1000_GPIE_EIAME |
4385 		    E1000_GPIE_PBA | E1000_GPIE_NSICR);
4386 
4387 	/* Turn on MSI-X */
4388 	switch (hw->mac.type) {
4389 	case e1000_82580:
4390 	case e1000_i350:
4391 	case e1000_i354:
4392 	case e1000_i210:
4393 	case e1000_i211:
4394 	case e1000_vfadapt:
4395 	case e1000_vfadapt_i350:
4396 		/* RX entries */
4397 		for (int i = 0; i < sc->rx_num_queues; i++) {
4398 			uint32_t index, qid;
4399 
4400 			rx_que = &sc->rx_queues[i];
4401 			qid = rx_que->rxr.me;
4402 			index = qid >> 1;
4403 			ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
4404 			if (qid & 1) {
4405 				ivar &= 0xFF00FFFF;
4406 				ivar |= (rx_que->msix | E1000_IVAR_VALID) <<
4407 				    16;
4408 			} else {
4409 				ivar &= 0xFFFFFF00;
4410 				ivar |= rx_que->msix | E1000_IVAR_VALID;
4411 			}
4412 			E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
4413 			sc->que_mask |= rx_que->eims;
4414 		}
4415 		/* TX entries */
4416 		for (int i = 0; i < sc->tx_num_queues; i++) {
4417 			uint32_t index, qid;
4418 
4419 			tx_que = &sc->tx_queues[i];
4420 			qid = tx_que->txr.me;
4421 			index = qid >> 1;
4422 			ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
4423 			if (qid & 1) {
4424 				ivar &= 0x00FFFFFF;
4425 				ivar |= (tx_que->msix | E1000_IVAR_VALID) <<
4426 				    24;
4427 			} else {
4428 				ivar &= 0xFFFF00FF;
4429 				ivar |= (tx_que->msix | E1000_IVAR_VALID) <<
4430 				    8;
4431 			}
4432 			E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
4433 			sc->que_mask |= tx_que->eims;
4434 		}
4435 
4436 		/* And for the link interrupt */
4437 		if (sc->vf_ifp) {
4438 			/*
4439 			 * VTIVAR_MISC maps the VF mailbox in bits 7:0.
4440 			 * The PF IVAR_MISC maps other causes in bits 15:8.
4441 			 */
4442 			ivar = sc->linkvec | E1000_IVAR_VALID;
4443 		} else
4444 			ivar = (sc->linkvec | E1000_IVAR_VALID) << 8;
4445 		sc->link_mask = 1 << sc->linkvec;
4446 		E1000_WRITE_REG(hw, E1000_IVAR_MISC, ivar);
4447 		break;
4448 	case e1000_82576:
4449 		/* RX entries */
4450 		for (int i = 0; i < sc->rx_num_queues; i++) {
4451 			uint32_t index, qid;
4452 
4453 			rx_que = &sc->rx_queues[i];
4454 			qid = rx_que->rxr.me;
4455 			index = qid & 0x7; /* Each IVAR has two entries */
4456 			ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
4457 			if (qid < 8) {
4458 				ivar &= 0xFFFFFF00;
4459 				ivar |= rx_que->msix | E1000_IVAR_VALID;
4460 			} else {
4461 				ivar &= 0xFF00FFFF;
4462 				ivar |= (rx_que->msix | E1000_IVAR_VALID) <<
4463 				    16;
4464 			}
4465 			E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
4466 			sc->que_mask |= rx_que->eims;
4467 		}
4468 		/* TX entries */
4469 		for (int i = 0; i < sc->tx_num_queues; i++) {
4470 			uint32_t index, qid;
4471 
4472 			tx_que = &sc->tx_queues[i];
4473 			qid = tx_que->txr.me;
4474 			index = qid & 0x7; /* Each IVAR has two entries */
4475 			ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
4476 			if (qid < 8) {
4477 				ivar &= 0xFFFF00FF;
4478 				ivar |= (tx_que->msix | E1000_IVAR_VALID) <<
4479 				    8;
4480 			} else {
4481 				ivar &= 0x00FFFFFF;
4482 				ivar |= (tx_que->msix | E1000_IVAR_VALID) <<
4483 				    24;
4484 			}
4485 			E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
4486 			sc->que_mask |= tx_que->eims;
4487 		}
4488 
4489 		/* And for the link interrupt */
4490 		ivar = (sc->linkvec | E1000_IVAR_VALID) << 8;
4491 		sc->link_mask = 1 << sc->linkvec;
4492 		E1000_WRITE_REG(hw, E1000_IVAR_MISC, ivar);
4493 		break;
4494 
4495 	case e1000_82575:
4496 		/* enable MSI-X support*/
4497 		tmp = E1000_READ_REG(hw, E1000_CTRL_EXT);
4498 		tmp |= E1000_CTRL_EXT_PBA_CLR;
4499 		/* Auto-Mask interrupts upon ICR read. */
4500 		tmp |= E1000_CTRL_EXT_EIAME;
4501 		tmp |= E1000_CTRL_EXT_IRCA;
4502 		E1000_WRITE_REG(hw, E1000_CTRL_EXT, tmp);
4503 
4504 		/* Queues */
4505 		for (int i = 0; i < sc->rx_num_queues; i++) {
4506 			rx_que = &sc->rx_queues[i];
4507 			tmp = E1000_EICR_RX_QUEUE0 << i;
4508 			tmp |= E1000_EICR_TX_QUEUE0 << i;
4509 			rx_que->eims = tmp;
4510 			E1000_WRITE_REG_ARRAY(hw, E1000_MSIXBM(0), i,
4511 			    rx_que->eims);
4512 			sc->que_mask |= rx_que->eims;
4513 		}
4514 
4515 		/* Link */
4516 		E1000_WRITE_REG(hw, E1000_MSIXBM(sc->linkvec),
4517 		    E1000_EIMS_OTHER);
4518 		sc->link_mask |= E1000_EIMS_OTHER;
4519 	default:
4520 		break;
4521 	}
4522 
4523 	return;
4524 }
4525 
4526 static void
igb_initialize_interrupt_rate(struct e1000_softc * sc)4527 igb_initialize_interrupt_rate(struct e1000_softc *sc)
4528 {
4529 	struct e1000_hw *hw = &sc->hw;
4530 	struct em_rx_queue *rx_que;
4531 	u32 newitr;
4532 
4533 	newitr = IGB_INTS_TO_EITR(em_max_interrupt_rate);
4534 	if (hw->mac.type == e1000_82575)
4535 		newitr |= newitr << 16;
4536 	else
4537 		newitr |= E1000_EITR_CNT_IGNR;
4538 
4539 	for (int i = 0; i < sc->rx_num_queues; i++) {
4540 		rx_que = &sc->rx_queues[i];
4541 		rx_que->itr_setting = newitr;
4542 		E1000_WRITE_REG(hw, E1000_EITR(rx_que->msix),
4543 		    rx_que->itr_setting);
4544 	}
4545 	if (sc->intr_type == IFLIB_INTR_MSIX)
4546 		E1000_WRITE_REG(hw, E1000_EITR(sc->linkvec), newitr);
4547 }
4548 
4549 static void
em_free_pci_resources(if_ctx_t ctx)4550 em_free_pci_resources(if_ctx_t ctx)
4551 {
4552 	struct e1000_softc *sc = iflib_get_softc(ctx);
4553 	struct em_rx_queue *que = sc->rx_queues;
4554 	device_t dev = iflib_get_dev(ctx);
4555 
4556 	/* Release all MSI-X queue resources */
4557 	if (sc->intr_type == IFLIB_INTR_MSIX)
4558 		iflib_irq_free(ctx, &sc->irq);
4559 
4560 	if (que != NULL) {
4561 		for (int i = 0; i < sc->rx_num_queues; i++, que++) {
4562 			iflib_irq_free(ctx, &que->que_irq);
4563 		}
4564 	}
4565 
4566 	if (sc->memory != NULL) {
4567 		bus_release_resource(dev, SYS_RES_MEMORY,
4568 		    rman_get_rid(sc->memory), sc->memory);
4569 		sc->memory = NULL;
4570 	}
4571 
4572 	if (sc->flash != NULL) {
4573 		bus_release_resource(dev, SYS_RES_MEMORY,
4574 		    rman_get_rid(sc->flash), sc->flash);
4575 		sc->flash = NULL;
4576 	}
4577 
4578 	if (sc->ioport != NULL) {
4579 		bus_release_resource(dev, SYS_RES_IOPORT,
4580 		    rman_get_rid(sc->ioport), sc->ioport);
4581 		sc->ioport = NULL;
4582 	}
4583 }
4584 
4585 /* Set up MSI or MSI-X */
4586 static int
em_setup_msix(if_ctx_t ctx)4587 em_setup_msix(if_ctx_t ctx)
4588 {
4589 	struct e1000_softc *sc = iflib_get_softc(ctx);
4590 
4591 	if (sc->hw.mac.type == e1000_82574) {
4592 		em_enable_vectors_82574(ctx);
4593 	}
4594 	return (0);
4595 }
4596 
4597 /*********************************************************************
4598  *
4599  *  Workaround for SmartSpeed on 82541 and 82547 controllers
4600  *
4601  **********************************************************************/
4602 static void
lem_smartspeed(struct e1000_softc * sc)4603 lem_smartspeed(struct e1000_softc *sc)
4604 {
4605 	u16 phy_tmp;
4606 
4607 	if (sc->link_state == EM_LINK_STATE_UP ||
4608 	    sc->link_state == EM_LINK_STATE_UP_RESET_PENDING ||
4609 	    (sc->hw.phy.type != e1000_phy_igp) ||
4610 	    sc->hw.mac.autoneg == 0 ||
4611 	    (sc->hw.phy.autoneg_advertised & ADVERTISE_1000_FULL) == 0)
4612 		return;
4613 
4614 	if (sc->smartspeed == 0) {
4615 		/* If Master/Slave config fault is asserted twice,
4616 		 * we assume back-to-back */
4617 		e1000_read_phy_reg(&sc->hw, PHY_1000T_STATUS, &phy_tmp);
4618 		if (!(phy_tmp & SR_1000T_MS_CONFIG_FAULT))
4619 			return;
4620 		e1000_read_phy_reg(&sc->hw, PHY_1000T_STATUS, &phy_tmp);
4621 		if (phy_tmp & SR_1000T_MS_CONFIG_FAULT) {
4622 			e1000_read_phy_reg(&sc->hw,
4623 			    PHY_1000T_CTRL, &phy_tmp);
4624 			if(phy_tmp & CR_1000T_MS_ENABLE) {
4625 				phy_tmp &= ~CR_1000T_MS_ENABLE;
4626 				e1000_write_phy_reg(&sc->hw,
4627 				    PHY_1000T_CTRL, phy_tmp);
4628 				sc->smartspeed++;
4629 				if(sc->hw.mac.autoneg &&
4630 				   !e1000_copper_link_autoneg(&sc->hw) &&
4631 				   !e1000_read_phy_reg(&sc->hw,
4632 				    PHY_CONTROL, &phy_tmp)) {
4633 					phy_tmp |= (MII_CR_AUTO_NEG_EN |
4634 						    MII_CR_RESTART_AUTO_NEG);
4635 					e1000_write_phy_reg(&sc->hw,
4636 					    PHY_CONTROL, phy_tmp);
4637 				}
4638 			}
4639 		}
4640 		return;
4641 	} else if(sc->smartspeed == EM_SMARTSPEED_DOWNSHIFT) {
4642 		/* If still no link, perhaps using 2/3 pair cable */
4643 		e1000_read_phy_reg(&sc->hw, PHY_1000T_CTRL, &phy_tmp);
4644 		phy_tmp |= CR_1000T_MS_ENABLE;
4645 		e1000_write_phy_reg(&sc->hw, PHY_1000T_CTRL, phy_tmp);
4646 		if(sc->hw.mac.autoneg &&
4647 		   !e1000_copper_link_autoneg(&sc->hw) &&
4648 		   !e1000_read_phy_reg(&sc->hw, PHY_CONTROL, &phy_tmp)) {
4649 			phy_tmp |= (MII_CR_AUTO_NEG_EN |
4650 				    MII_CR_RESTART_AUTO_NEG);
4651 			e1000_write_phy_reg(&sc->hw, PHY_CONTROL, phy_tmp);
4652 		}
4653 	}
4654 	/* Restart process after EM_SMARTSPEED_MAX iterations */
4655 	if(sc->smartspeed++ == EM_SMARTSPEED_MAX)
4656 		sc->smartspeed = 0;
4657 }
4658 
4659 static void
igb_disable_dmac(struct e1000_hw * hw)4660 igb_disable_dmac(struct e1000_hw *hw)
4661 {
4662 	u32 reg;
4663 
4664 	reg = E1000_READ_REG(hw, E1000_DMACR);
4665 	reg &= ~E1000_DMACR_DMAC_EN;
4666 	/* Retain the documented Lx policy and I210 reserved encoding. */
4667 	reg |= E1000_DMACR_DMAC_LX_MASK;
4668 	E1000_WRITE_REG(hw, E1000_DMACR, reg);
4669 }
4670 
4671 /*********************************************************************
4672  *
4673  *  Initialize the DMA Coalescing feature
4674  *
4675  **********************************************************************/
4676 static void
igb_init_dmac(struct e1000_softc * sc,u32 pba)4677 igb_init_dmac(struct e1000_softc *sc, u32 pba)
4678 {
4679 	device_t	dev = sc->dev;
4680 	struct e1000_hw *hw = &sc->hw;
4681 	u32		dmac, dmacwt, reg, ttlx;
4682 	u16		hwm;
4683 	u16		max_frame_size;
4684 
4685 	KASSERT(!sc->vf_ifp, ("%s: DMA coalescing requested for a VF",
4686 	    __func__));
4687 
4688 	if (hw->mac.type == e1000_i211)
4689 		return;
4690 
4691 	/*
4692 	 * I350 DMA coalescing and SR-IOV are mutually exclusive.  Preserve
4693 	 * the configured value so it can be restored after IOV is disabled.
4694 	 */
4695 	if (igb_iov_enabled(sc)) {
4696 		if (hw->mac.type > e1000_82580)
4697 			igb_disable_dmac(hw);
4698 		return;
4699 	}
4700 
4701 	max_frame_size = sc->shared->isc_max_frame_size;
4702 	if (hw->mac.type > e1000_82580) {
4703 
4704 		if (sc->dmac == 0) { /* Disabling it */
4705 			igb_disable_dmac(hw);
4706 			return;
4707 		} else
4708 			device_printf(dev, "DMA Coalescing enabled\n");
4709 
4710 		/* Set starting threshold */
4711 		E1000_WRITE_REG(hw, E1000_DMCTXTH, 0);
4712 
4713 		hwm = 64 * pba - max_frame_size / 16;
4714 		if (hwm < 64 * (pba - 6))
4715 			hwm = 64 * (pba - 6);
4716 		reg = E1000_READ_REG(hw, E1000_FCRTC);
4717 		reg &= ~E1000_FCRTC_RTH_COAL_MASK;
4718 		reg |= ((hwm << E1000_FCRTC_RTH_COAL_SHIFT)
4719 		    & E1000_FCRTC_RTH_COAL_MASK);
4720 		E1000_WRITE_REG(hw, E1000_FCRTC, reg);
4721 
4722 
4723 		dmac = pba - max_frame_size / 512;
4724 		if (dmac < pba - 10)
4725 			dmac = pba - 10;
4726 		reg = E1000_READ_REG(hw, E1000_DMACR);
4727 		reg &= ~(E1000_DMACR_DMACWT_MASK |
4728 		    E1000_DMACR_DMACTHR_MASK | E1000_DMACR_DMAC_LX_MASK |
4729 		    E1000_DMACR_DMAC_EN | E1000_DMACR_DC_LPBKW_EN |
4730 		    E1000_DMACR_DC_BMC2OSW_EN);
4731 		reg |= ((dmac << E1000_DMACR_DMACTHR_SHIFT)
4732 		    & E1000_DMACR_DMACTHR_MASK);
4733 
4734 		/* Transition to L0s or L1 if available. */
4735 		reg |= (E1000_DMACR_DMAC_EN | E1000_DMACR_DMAC_LX_MASK);
4736 
4737 		/*
4738 		 * The watchdog uses 12.8 usec units on an I354 2.5 Gb/s
4739 		 * backplane connection and 32 usec units otherwise.
4740 		 */
4741 		if (hw->mac.type == e1000_i354) {
4742 			int status = E1000_READ_REG(hw, E1000_STATUS);
4743 			if ((status & E1000_STATUS_2P5_SKU) &&
4744 			    (!(status & E1000_STATUS_2P5_SKU_OVER)))
4745 				dmacwt = (sc->dmac * 5) >> 6;
4746 			else
4747 				dmacwt = sc->dmac >> 5;
4748 		} else {
4749 			dmacwt = sc->dmac >> 5;
4750 		}
4751 		reg |= dmacwt & E1000_DMACR_DMACWT_MASK;
4752 		if (hw->mac.type == e1000_i350 ||
4753 		    hw->mac.type == e1000_i354)
4754 			reg |= E1000_DMACR_DC_LPBKW_EN;
4755 		if (hw->mac.type == e1000_i354)
4756 			reg |= E1000_DMACR_DC_BMC2OSW_EN;
4757 
4758 		E1000_WRITE_REG(hw, E1000_DMACR, reg);
4759 
4760 		E1000_WRITE_REG(hw, E1000_DMCRTRH, 0);
4761 
4762 		/* Set the interval before transition. */
4763 		reg = E1000_READ_REG(hw, E1000_DMCTLX);
4764 		reg &= ~E1000_DMCTLX_TTLX_MASK;
4765 		if (hw->mac.type == e1000_i350)
4766 			reg |= IGB_DMCTLX_DCFLUSH_DIS;
4767 		/*
4768 		 * I210 documents TTLX as reserved with a required value of 0x20.
4769 		 * At 2.5 Gb/s the I354 unit is 0.4 usec, so ten ticks retain
4770 		 * the four usec interval used at other speeds.
4771 		 */
4772 		if (hw->mac.type == e1000_i210) {
4773 			ttlx = 0x20;
4774 		} else if (hw->mac.type == e1000_i354) {
4775 			int status = E1000_READ_REG(hw, E1000_STATUS);
4776 			if ((status & E1000_STATUS_2P5_SKU) &&
4777 			    (!(status & E1000_STATUS_2P5_SKU_OVER)))
4778 				ttlx = 0xA;
4779 			else
4780 				ttlx = 0x4;
4781 		} else {
4782 			ttlx = 0x4;
4783 		}
4784 		reg |= ttlx & E1000_DMCTLX_TTLX_MASK;
4785 
4786 		E1000_WRITE_REG(hw, E1000_DMCTLX, reg);
4787 
4788 		/* free space in tx packet buffer to wake from DMA coal */
4789 		E1000_WRITE_REG(hw, E1000_DMCTXTH, (IGB_TXPBSIZE -
4790 		    (2 * max_frame_size)) >> 6);
4791 
4792 		/* make low power state decision controlled by DMA coal */
4793 		reg = E1000_READ_REG(hw, E1000_PCIEMISC);
4794 		reg |= E1000_PCIEMISC_LX_DECISION;
4795 		E1000_WRITE_REG(hw, E1000_PCIEMISC, reg);
4796 
4797 	} else if (hw->mac.type == e1000_82580) {
4798 		u32 reg = E1000_READ_REG(hw, E1000_PCIEMISC);
4799 		E1000_WRITE_REG(hw, E1000_PCIEMISC,
4800 		    reg & ~E1000_PCIEMISC_LX_DECISION);
4801 		E1000_WRITE_REG(hw, E1000_DMACR, 0);
4802 	}
4803 }
4804 /*********************************************************************
4805  * The 3 following flush routines are used as a workaround in the
4806  * I219 client parts and only for them.
4807  *
4808  * em_flush_tx_ring - remove all descriptors from the tx_ring
4809  *
4810  * We want to clear all pending descriptors from the TX ring.
4811  * zeroing happens when the HW reads the regs. We assign the ring itself as
4812  * the data of the next descriptor. We don't care about the data we are about
4813  * to reset the HW.
4814  **********************************************************************/
4815 static void
em_flush_tx_ring(struct e1000_softc * sc)4816 em_flush_tx_ring(struct e1000_softc *sc)
4817 {
4818 	struct e1000_hw *hw = &sc->hw;
4819 	struct tx_ring *txr = &sc->tx_queues->txr;
4820 	struct e1000_tx_desc *txd;
4821 	u32 tctl, txd_lower = E1000_TXD_CMD_IFCS;
4822 	u16 size = 512;
4823 
4824 	tctl = E1000_READ_REG(hw, E1000_TCTL);
4825 	E1000_WRITE_REG(hw, E1000_TCTL, tctl | E1000_TCTL_EN);
4826 
4827 	txd = &txr->tx_base[txr->tx_cidx_processed];
4828 
4829 	/* Just use the ring as a dummy buffer addr */
4830 	txd->buffer_addr = txr->tx_paddr;
4831 	txd->lower.data = htole32(txd_lower | size);
4832 	txd->upper.data = 0;
4833 
4834 	/* flush descriptors to memory before notifying the HW */
4835 	wmb();
4836 
4837 	E1000_WRITE_REG(hw, E1000_TDT(0), txr->tx_cidx_processed);
4838 	mb();
4839 	usec_delay(250);
4840 }
4841 
4842 /*********************************************************************
4843  * em_flush_rx_ring - remove all descriptors from the rx_ring
4844  *
4845  * Mark all descriptors in the RX ring as consumed and disable the rx ring
4846  **********************************************************************/
4847 static void
em_flush_rx_ring(struct e1000_softc * sc)4848 em_flush_rx_ring(struct e1000_softc *sc)
4849 {
4850 	struct e1000_hw *hw = &sc->hw;
4851 	u32 rctl, rxdctl;
4852 
4853 	rctl = E1000_READ_REG(hw, E1000_RCTL);
4854 	E1000_WRITE_REG(hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
4855 	E1000_WRITE_FLUSH(hw);
4856 	usec_delay(150);
4857 
4858 	rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(0));
4859 	/* zero the lower 14 bits (prefetch and host thresholds) */
4860 	rxdctl &= 0xffffc000;
4861 	/*
4862 	 * update thresholds: prefetch threshold to 31, host threshold to 1
4863 	 * and make sure the granularity is "descriptors" and not
4864 	 * "cache lines"
4865 	 */
4866 	rxdctl |= (0x1F | (1 << 8) | E1000_RXDCTL_THRESH_UNIT_DESC);
4867 	E1000_WRITE_REG(hw, E1000_RXDCTL(0), rxdctl);
4868 
4869 	/* momentarily enable the RX ring for the changes to take effect */
4870 	E1000_WRITE_REG(hw, E1000_RCTL, rctl | E1000_RCTL_EN);
4871 	E1000_WRITE_FLUSH(hw);
4872 	usec_delay(150);
4873 	E1000_WRITE_REG(hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
4874 }
4875 
4876 /*********************************************************************
4877  * em_flush_desc_rings - remove all descriptors from the descriptor rings
4878  *
4879  * In I219, the descriptor rings must be emptied before resetting the HW
4880  * or before changing the device state to D3 during runtime (runtime PM).
4881  *
4882  * Failure to do this will cause the HW to enter a unit hang state which can
4883  * only be released by PCI reset on the device
4884  *
4885  **********************************************************************/
4886 static void
em_flush_desc_rings(struct e1000_softc * sc)4887 em_flush_desc_rings(struct e1000_softc *sc)
4888 {
4889 	struct e1000_hw	*hw = &sc->hw;
4890 	device_t dev = sc->dev;
4891 	u16 hang_state;
4892 	u32 fext_nvm11, tdlen;
4893 
4894 	/* First, disable MULR fix in FEXTNVM11 */
4895 	fext_nvm11 = E1000_READ_REG(hw, E1000_FEXTNVM11);
4896 	fext_nvm11 |= E1000_FEXTNVM11_DISABLE_MULR_FIX;
4897 	E1000_WRITE_REG(hw, E1000_FEXTNVM11, fext_nvm11);
4898 
4899 	/* do nothing if we're not in faulty state, or the queue is empty */
4900 	tdlen = E1000_READ_REG(hw, E1000_TDLEN(0));
4901 	hang_state = pci_read_config(dev, PCICFG_DESC_RING_STATUS, 2);
4902 	if (!(hang_state & FLUSH_DESC_REQUIRED) || !tdlen)
4903 		return;
4904 	em_flush_tx_ring(sc);
4905 
4906 	/* recheck, maybe the fault is caused by the rx ring */
4907 	hang_state = pci_read_config(dev, PCICFG_DESC_RING_STATUS, 2);
4908 	if (hang_state & FLUSH_DESC_REQUIRED)
4909 		em_flush_rx_ring(sc);
4910 }
4911 
4912 
4913 /*********************************************************************
4914  *
4915  *  Initialize the hardware to a configuration as specified by the
4916  *  sc structure.
4917  *
4918  **********************************************************************/
4919 static int
em_reset(if_ctx_t ctx)4920 em_reset(if_ctx_t ctx)
4921 {
4922 	device_t dev = iflib_get_dev(ctx);
4923 	struct e1000_softc *sc = iflib_get_softc(ctx);
4924 	if_t ifp = iflib_get_ifp(ctx);
4925 	struct e1000_hw *hw = &sc->hw;
4926 	u32 rx_buffer_size;
4927 	u32 pba;
4928 	s32 error;
4929 
4930 	INIT_DEBUGOUT("em_reset: begin");
4931 	KASSERT(!sc->vf_ifp, ("%s called for a VF", __func__));
4932 
4933 	/* Let the firmware know the OS is in control */
4934 	em_get_hw_control(sc);
4935 
4936 	/* Set up smart power down as default off on newer adapters. */
4937 	if (!em_smart_pwr_down && (hw->mac.type == e1000_82571 ||
4938 	    hw->mac.type == e1000_82572)) {
4939 		u16 phy_tmp = 0;
4940 
4941 		/* Speed up time to link by disabling smart power down. */
4942 		e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT, &phy_tmp);
4943 		phy_tmp &= ~IGP02E1000_PM_SPD;
4944 		e1000_write_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT, phy_tmp);
4945 	}
4946 
4947 	/*
4948 	 * Packet Buffer Allocation (PBA)
4949 	 * Writing PBA sets the receive portion of the buffer
4950 	 * the remainder is used for the transmit buffer.
4951 	 */
4952 	switch (hw->mac.type) {
4953 	/* 82547: Total Packet Buffer is 40K */
4954 	case e1000_82547:
4955 	case e1000_82547_rev_2:
4956 		if (hw->mac.max_frame_size > 8192)
4957 			pba = E1000_PBA_22K; /* 22K for Rx, 18K for Tx */
4958 		else
4959 			pba = E1000_PBA_30K; /* 30K for Rx, 10K for Tx */
4960 		break;
4961 	/* 82571/82572/80003es2lan: Total Packet Buffer is 48K */
4962 	case e1000_82571:
4963 	case e1000_82572:
4964 	case e1000_80003es2lan:
4965 			pba = E1000_PBA_32K; /* 32K for Rx, 16K for Tx */
4966 		break;
4967 	/* 82573: Total Packet Buffer is 32K */
4968 	case e1000_82573:
4969 			pba = E1000_PBA_12K; /* 12K for Rx, 20K for Tx */
4970 		break;
4971 	/* 82574/82583: Total Packet Buffer is 40K */
4972 	case e1000_82574:
4973 	case e1000_82583:
4974 		if (hw->mac.max_frame_size > 8192)
4975 			pba = E1000_PBA_22K; /* 22K for Rx, 18K for Tx */
4976 		else
4977 			pba = E1000_PBA_32K; /* 32K for RX, 8K for Tx */
4978 		break;
4979 	case e1000_ich8lan:
4980 		pba = E1000_PBA_8K;
4981 		break;
4982 	case e1000_ich9lan:
4983 	case e1000_ich10lan:
4984 		/* Boost Receive side for jumbo frames */
4985 		if (hw->mac.max_frame_size > 4096)
4986 			pba = E1000_PBA_14K;
4987 		else
4988 			pba = E1000_PBA_10K;
4989 		break;
4990 	case e1000_pchlan:
4991 	case e1000_pch2lan:
4992 	case e1000_pch_lpt:
4993 	case e1000_pch_spt:
4994 	case e1000_pch_cnp:
4995 	case e1000_pch_tgp:
4996 	case e1000_pch_adp:
4997 	case e1000_pch_mtp:
4998 	case e1000_pch_ptp:
4999 	case e1000_pch_nvp:
5000 		pba = E1000_PBA_26K;
5001 		break;
5002 	case e1000_82575:
5003 		pba = E1000_PBA_32K;
5004 		break;
5005 	case e1000_82576:
5006 		pba = E1000_READ_REG(hw, E1000_RXPBS);
5007 		pba &= E1000_RXPBS_SIZE_MASK_82576;
5008 		break;
5009 	case e1000_82580:
5010 	case e1000_i350:
5011 	case e1000_i354:
5012 		pba = E1000_READ_REG(hw, E1000_RXPBS);
5013 		pba = e1000_rxpbs_adjust_82580(pba);
5014 		break;
5015 	case e1000_i210:
5016 	case e1000_i211:
5017 		pba = E1000_PBA_34K;
5018 		break;
5019 	default:
5020 		/* Remaining devices assumed to have Packet Buffer of 64K. */
5021 		if (hw->mac.max_frame_size > 8192)
5022 			pba = E1000_PBA_40K; /* 40K for Rx, 24K for Tx */
5023 		else
5024 			pba = E1000_PBA_48K; /* 48K for Rx, 16K for Tx */
5025 	}
5026 
5027 	/* Special needs in case of Jumbo frames */
5028 	if ((hw->mac.type == e1000_82575) && (if_getmtu(ifp) > ETHERMTU)) {
5029 		u32 tx_space, min_tx, min_rx;
5030 		pba = E1000_READ_REG(hw, E1000_PBA);
5031 		tx_space = pba >> 16;
5032 		pba &= 0xffff;
5033 		min_tx = (hw->mac.max_frame_size +
5034 		    sizeof(struct e1000_tx_desc) - ETHERNET_FCS_SIZE) * 2;
5035 		min_tx = roundup2(min_tx, 1024);
5036 		min_tx >>= 10;
5037 		min_rx = hw->mac.max_frame_size;
5038 		min_rx = roundup2(min_rx, 1024);
5039 		min_rx >>= 10;
5040 		if (tx_space < min_tx &&
5041 		    ((min_tx - tx_space) < pba)) {
5042 			pba = pba - (min_tx - tx_space);
5043 			/*
5044 			 * if short on rx space, rx wins
5045 			 * and must trump tx adjustment
5046 			 */
5047 			if (pba < min_rx)
5048 				pba = min_rx;
5049 		}
5050 		E1000_WRITE_REG(hw, E1000_PBA, pba);
5051 	}
5052 
5053 	if (hw->mac.type < igb_mac_min)
5054 		E1000_WRITE_REG(hw, E1000_PBA, pba);
5055 
5056 	INIT_DEBUGOUT1("em_reset: pba=%dK", pba);
5057 
5058 	/*
5059 	 * These parameters control the automatic generation (Tx) and
5060 	 * response (Rx) to Ethernet PAUSE frames.
5061 	 * - High water mark should allow for at least two frames to be
5062 	 *   received after sending an XOFF.
5063 	 * - Low water mark works best when it is very near the high water
5064 	     mark.
5065 	 *   This allows the receiver to restart by sending XON when it has
5066 	 *   drained a bit. Here we use an arbitrary value of 1500 which will
5067 	 *   restart after one full frame is pulled from the buffer. There
5068 	 *   could be several smaller frames in the buffer and if so they will
5069 	 *   not trigger the XON until their total number reduces the buffer
5070 	 *   by 1500.
5071 	 * - The pause time is fairly large at 1000 x 512ns = 512 usec.
5072 	 */
5073 	rx_buffer_size = (pba & 0xffff) << 10;
5074 	hw->fc.high_water = rx_buffer_size -
5075 	    roundup2(hw->mac.max_frame_size, 1024);
5076 	hw->fc.low_water = hw->fc.high_water - 1500;
5077 
5078 	if (sc->fc) /* locally set flow control value? */
5079 		hw->fc.requested_mode = sc->fc;
5080 	else
5081 		hw->fc.requested_mode = e1000_fc_full;
5082 
5083 	if (hw->mac.type == e1000_80003es2lan)
5084 		hw->fc.pause_time = 0xFFFF;
5085 	else
5086 		hw->fc.pause_time = EM_FC_PAUSE_TIME;
5087 
5088 	hw->fc.send_xon = true;
5089 
5090 	/* Device specific overrides/settings */
5091 	switch (hw->mac.type) {
5092 	case e1000_pchlan:
5093 		/* Workaround: no TX flow ctrl for PCH */
5094 		hw->fc.requested_mode = e1000_fc_rx_pause;
5095 		hw->fc.pause_time = 0xFFFF; /* override */
5096 		if (if_getmtu(ifp) > ETHERMTU) {
5097 			hw->fc.high_water = 0x3500;
5098 			hw->fc.low_water = 0x1500;
5099 		} else {
5100 			hw->fc.high_water = 0x5000;
5101 			hw->fc.low_water = 0x3000;
5102 		}
5103 		hw->fc.refresh_time = 0x1000;
5104 		break;
5105 	case e1000_pch2lan:
5106 	case e1000_pch_lpt:
5107 	case e1000_pch_spt:
5108 	case e1000_pch_cnp:
5109 	case e1000_pch_tgp:
5110 	case e1000_pch_adp:
5111 	case e1000_pch_mtp:
5112 	case e1000_pch_ptp:
5113 	case e1000_pch_nvp:
5114 		hw->fc.high_water = 0x5C20;
5115 		hw->fc.low_water = 0x5048;
5116 		hw->fc.pause_time = 0xFFFF;
5117 		hw->fc.refresh_time = 0xFFFF;
5118 		/* Jumbos need adjusted PBA */
5119 		if (if_getmtu(ifp) > ETHERMTU)
5120 			pba = E1000_PBA_12K;
5121 		else
5122 			pba = E1000_PBA_26K;
5123 		E1000_WRITE_REG(hw, E1000_PBA, pba);
5124 		break;
5125 	case e1000_82575:
5126 	case e1000_82576:
5127 		/* 8-byte granularity */
5128 		hw->fc.low_water = hw->fc.high_water - 8;
5129 		break;
5130 	case e1000_82580:
5131 	case e1000_i350:
5132 	case e1000_i354:
5133 	case e1000_i210:
5134 	case e1000_i211:
5135 		/* 16-byte granularity */
5136 		hw->fc.low_water = hw->fc.high_water - 16;
5137 		break;
5138 	case e1000_ich9lan:
5139 	case e1000_ich10lan:
5140 		if (if_getmtu(ifp) > ETHERMTU) {
5141 			hw->fc.high_water = 0x2800;
5142 			hw->fc.low_water = hw->fc.high_water - 8;
5143 			break;
5144 		}
5145 		/* FALLTHROUGH */
5146 	default:
5147 		if (hw->mac.type == e1000_80003es2lan)
5148 			hw->fc.pause_time = 0xFFFF;
5149 		break;
5150 	}
5151 
5152 	/* I219 needs some special flushing to avoid hangs */
5153 	if (sc->hw.mac.type >= e1000_pch_spt && sc->hw.mac.type < igb_mac_min)
5154 		em_flush_desc_rings(sc);
5155 
5156 	/* Issue a global reset */
5157 	em_prepare_fatal_error_reset(sc);
5158 	error = e1000_reset_hw(hw);
5159 	if (error != E1000_SUCCESS) {
5160 		device_printf(dev, "Hardware reset failed: %d\n", error);
5161 		return (error);
5162 	}
5163 	if (hw->mac.type >= igb_mac_min) {
5164 		E1000_WRITE_REG(hw, E1000_WUC, 0);
5165 	} else {
5166 		E1000_WRITE_REG(hw, E1000_WUFC, 0);
5167 		em_disable_aspm(sc);
5168 	}
5169 	if (sc->flags & IGB_MEDIA_RESET) {
5170 		e1000_setup_init_funcs(hw, true);
5171 		e1000_get_bus_info(hw);
5172 		sc->flags &= ~IGB_MEDIA_RESET;
5173 	}
5174 	/* and a re-init */
5175 	error = e1000_init_hw(hw);
5176 	if (error != E1000_SUCCESS) {
5177 		device_printf(dev, "Hardware initialization failed: %d\n",
5178 		    error);
5179 		return (error);
5180 	}
5181 	em_configure_82576_memory_errors(sc);
5182 	em_finish_fatal_error_reset(sc);
5183 	if (hw->mac.type >= igb_mac_min)
5184 		igb_init_dmac(sc, pba);
5185 
5186 	/* Save the receive packet-buffer allocation for AIM. */
5187 	sc->pba = pba;
5188 
5189 	E1000_WRITE_REG(hw, E1000_VET, ETHERTYPE_VLAN);
5190 	e1000_get_phy_info(hw);
5191 	e1000_check_for_link(hw);
5192 	sc->phy_hang_count = 0;
5193 
5194 	return (E1000_SUCCESS);
5195 }
5196 
5197 /*
5198  * Initialise the RSS mapping for NICs that support multiple transmit/
5199  * receive rings.
5200  */
5201 
5202 #define RSSKEYLEN 10
5203 static void
em_initialize_rss_mapping(struct e1000_softc * sc)5204 em_initialize_rss_mapping(struct e1000_softc *sc)
5205 {
5206 	uint8_t rss_key[4 * RSSKEYLEN];
5207 	uint32_t reta = 0;
5208 	struct e1000_hw *hw = &sc->hw;
5209 	int i;
5210 
5211 	/*
5212 	 * Configure RSS key
5213 	 */
5214 	arc4rand(rss_key, sizeof(rss_key), 0);
5215 	for (i = 0; i < RSSKEYLEN; ++i) {
5216 		uint32_t rssrk = 0;
5217 
5218 		rssrk = EM_RSSRK_VAL(rss_key, i);
5219 		E1000_WRITE_REG(hw,E1000_RSSRK(i), rssrk);
5220 	}
5221 
5222 	/*
5223 	 * Configure RSS redirect table in following fashion:
5224 	 * (hash & ring_cnt_mask) == rdr_table[(hash & rdr_table_mask)]
5225 	 */
5226 	for (i = 0; i < sizeof(reta); ++i) {
5227 		uint32_t q;
5228 
5229 		q = (i % sc->rx_num_queues) << 7;
5230 		reta |= q << (8 * i);
5231 	}
5232 
5233 	for (i = 0; i < 32; ++i)
5234 		E1000_WRITE_REG(hw, E1000_RETA(i), reta);
5235 
5236 	E1000_WRITE_REG(hw, E1000_MRQC, E1000_MRQC_RSS_ENABLE_2Q |
5237 			E1000_MRQC_RSS_FIELD_IPV4_TCP |
5238 			E1000_MRQC_RSS_FIELD_IPV4 |
5239 			E1000_MRQC_RSS_FIELD_IPV6_TCP_EX |
5240 			E1000_MRQC_RSS_FIELD_IPV6_EX |
5241 			E1000_MRQC_RSS_FIELD_IPV6);
5242 }
5243 
5244 static void
igb_initialize_rss_mapping(struct e1000_softc * sc)5245 igb_initialize_rss_mapping(struct e1000_softc *sc)
5246 {
5247 	struct e1000_hw *hw = &sc->hw;
5248 	int i;
5249 	int queue_id;
5250 	u32 reta;
5251 	u32 rss_key[10], mrqc, shift = 0;
5252 
5253 	/* XXX? */
5254 	if (hw->mac.type == e1000_82575)
5255 		shift = 6;
5256 
5257 	/*
5258 	 * The redirection table controls which destination
5259 	 * queue each bucket redirects traffic to.
5260 	 * Each DWORD represents four queues, with the LSB
5261 	 * being the first queue in the DWORD.
5262 	 *
5263 	 * This just allocates buckets to queues using round-robin
5264 	 * allocation.
5265 	 *
5266 	 * NOTE: It Just Happens to line up with the default
5267 	 * RSS allocation method.
5268 	 */
5269 
5270 	/* Warning FM follows */
5271 	reta = 0;
5272 	for (i = 0; i < 128; i++) {
5273 #ifdef RSS
5274 		queue_id = rss_get_indirection_to_bucket(i);
5275 		/*
5276 		 * If we have more queues than buckets, we'll
5277 		 * end up mapping buckets to a subset of the
5278 		 * queues.
5279 		 *
5280 		 * If we have more buckets than queues, we'll
5281 		 * end up instead assigning multiple buckets
5282 		 * to queues.
5283 		 *
5284 		 * Both are suboptimal, but we need to handle
5285 		 * the case so we don't go out of bounds
5286 		 * indexing arrays and such.
5287 		 */
5288 		queue_id = queue_id % sc->rx_num_queues;
5289 #else
5290 		queue_id = (i % sc->rx_num_queues);
5291 #endif
5292 		/* Adjust if required */
5293 		queue_id = queue_id << shift;
5294 
5295 		/*
5296 		 * The low 8 bits are for hash value (n+0);
5297 		 * The next 8 bits are for hash value (n+1), etc.
5298 		 */
5299 		reta = reta >> 8;
5300 		reta = reta | ( ((uint32_t) queue_id) << 24);
5301 		if ((i & 3) == 3) {
5302 			E1000_WRITE_REG(hw, E1000_RETA(i >> 2), reta);
5303 			reta = 0;
5304 		}
5305 	}
5306 
5307 	/* Now fill in hash table */
5308 
5309 	/*
5310 	 * MRQC: Multiple Receive Queues Command
5311 	 * Set queuing to RSS control, number depends on the device.
5312 	 */
5313 	mrqc = E1000_MRQC_ENABLE_RSS_MQ;
5314 
5315 	/* XXX ew typecasting */
5316 	rss_getkey((uint8_t *) &rss_key);
5317 	for (i = 0; i < 10; i++)
5318 		E1000_WRITE_REG_ARRAY(hw, E1000_RSSRK(0), i, rss_key[i]);
5319 
5320 	/*
5321 	 * Configure the RSS fields to hash upon.
5322 	 */
5323 	mrqc |= (E1000_MRQC_RSS_FIELD_IPV4 |
5324 	    E1000_MRQC_RSS_FIELD_IPV4_TCP);
5325 	mrqc |= (E1000_MRQC_RSS_FIELD_IPV6 |
5326 	    E1000_MRQC_RSS_FIELD_IPV6_TCP);
5327 	mrqc |=( E1000_MRQC_RSS_FIELD_IPV4_UDP |
5328 	    E1000_MRQC_RSS_FIELD_IPV6_UDP);
5329 	mrqc |=( E1000_MRQC_RSS_FIELD_IPV6_UDP_EX |
5330 	    E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
5331 
5332 	E1000_WRITE_REG(hw, E1000_MRQC, mrqc);
5333 }
5334 
5335 /*********************************************************************
5336  *
5337  *  Setup networking device structure and register interface media.
5338  *
5339  **********************************************************************/
5340 static int
em_setup_interface(if_ctx_t ctx)5341 em_setup_interface(if_ctx_t ctx)
5342 {
5343 	if_t ifp = iflib_get_ifp(ctx);
5344 	struct e1000_softc *sc = iflib_get_softc(ctx);
5345 	if_softc_ctx_t scctx = sc->shared;
5346 
5347 	INIT_DEBUGOUT("em_setup_interface: begin");
5348 
5349 	/* Single Queue */
5350 	if (sc->tx_num_queues == 1) {
5351 		if_setsendqlen(ifp, scctx->isc_ntxd[0] - 1);
5352 		if_setsendqready(ifp);
5353 	}
5354 
5355 	/*
5356 	 * Specify the media types supported by this adapter and register
5357 	 * callbacks to update media and link information
5358 	 */
5359 	if (sc->vf_ifp) {
5360 		ifmedia_add(sc->media,
5361 		    IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL);
5362 		ifmedia_set(sc->media,
5363 		    IFM_ETHER | IFM_1000_T | IFM_FDX);
5364 		return (0);
5365 	}
5366 
5367 	if (sc->hw.phy.media_type == e1000_media_type_fiber ||
5368 	    sc->hw.phy.media_type == e1000_media_type_internal_serdes) {
5369 		u_char fiber_type = IFM_1000_SX;	/* default type */
5370 
5371 		if (sc->hw.mac.type == e1000_82545)
5372 			fiber_type = IFM_1000_LX;
5373 		ifmedia_add(sc->media,
5374 		    IFM_ETHER | fiber_type | IFM_FDX, 0, NULL);
5375 		ifmedia_add(sc->media, IFM_ETHER | fiber_type, 0, NULL);
5376 	} else {
5377 		ifmedia_add(sc->media, IFM_ETHER | IFM_10_T, 0, NULL);
5378 		ifmedia_add(sc->media,
5379 		    IFM_ETHER | IFM_10_T | IFM_FDX, 0, NULL);
5380 		ifmedia_add(sc->media, IFM_ETHER | IFM_100_TX, 0, NULL);
5381 		ifmedia_add(sc->media,
5382 		    IFM_ETHER | IFM_100_TX | IFM_FDX, 0, NULL);
5383 		if (sc->hw.phy.type != e1000_phy_ife) {
5384 			ifmedia_add(sc->media,
5385 			    IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL);
5386 			ifmedia_add(sc->media,
5387 			    IFM_ETHER | IFM_1000_T, 0, NULL);
5388 		}
5389 	}
5390 	ifmedia_add(sc->media, IFM_ETHER | IFM_AUTO, 0, NULL);
5391 	ifmedia_set(sc->media, IFM_ETHER | IFM_AUTO);
5392 	return (0);
5393 }
5394 
5395 static int
em_if_tx_queues_alloc(if_ctx_t ctx,caddr_t * vaddrs,uint64_t * paddrs,int ntxqs,int ntxqsets)5396 em_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
5397     int ntxqs, int ntxqsets)
5398 {
5399 	struct e1000_softc *sc = iflib_get_softc(ctx);
5400 	if_softc_ctx_t scctx = sc->shared;
5401 	int error = E1000_SUCCESS;
5402 	struct em_tx_queue *que;
5403 	int i, j;
5404 
5405 	MPASS(sc->tx_num_queues > 0);
5406 	MPASS(sc->tx_num_queues == ntxqsets);
5407 
5408 	/* First allocate the top level queue structs */
5409 	if (!(sc->tx_queues =
5410 	    (struct em_tx_queue *) malloc(sizeof(struct em_tx_queue) *
5411 	    sc->tx_num_queues, M_DEVBUF, M_NOWAIT | M_ZERO))) {
5412 		device_printf(iflib_get_dev(ctx),
5413 		    "Unable to allocate queue memory\n");
5414 		return(ENOMEM);
5415 	}
5416 
5417 	for (i = 0, que = sc->tx_queues; i < sc->tx_num_queues; i++, que++) {
5418 		/* Set up some basics */
5419 
5420 		struct tx_ring *txr = &que->txr;
5421 		KASSERT(__is_aligned(&txr->tx_aim_snapshot, sizeof(uint64_t)),
5422 		    ("%s: misaligned TX AIM snapshot %p", __func__,
5423 		    &txr->tx_aim_snapshot));
5424 		txr->sc = que->sc = sc;
5425 		que->me = txr->me =  i;
5426 
5427 		/* Allocate report status array */
5428 		if (!(txr->tx_rsq =
5429 		    (qidx_t *) malloc(sizeof(qidx_t) * scctx->isc_ntxd[0],
5430 		    M_DEVBUF, M_NOWAIT | M_ZERO))) {
5431 			device_printf(iflib_get_dev(ctx),
5432 			    "failed to allocate rs_idxs memory\n");
5433 			error = ENOMEM;
5434 			goto fail;
5435 		}
5436 		for (j = 0; j < scctx->isc_ntxd[0]; j++)
5437 			txr->tx_rsq[j] = QIDX_INVALID;
5438 		/* get the virtual and physical address of hardware queues */
5439 		txr->tx_base = (struct e1000_tx_desc *)vaddrs[i*ntxqs];
5440 		txr->tx_paddr = paddrs[i*ntxqs];
5441 	}
5442 
5443 	if (bootverbose)
5444 		device_printf(iflib_get_dev(ctx),
5445 		    "allocated for %d tx_queues\n", sc->tx_num_queues);
5446 	return (0);
5447 fail:
5448 	em_if_queues_free(ctx);
5449 	return (error);
5450 }
5451 
5452 static int
em_if_rx_queues_alloc(if_ctx_t ctx,caddr_t * vaddrs,uint64_t * paddrs,int nrxqs,int nrxqsets)5453 em_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
5454     int nrxqs, int nrxqsets)
5455 {
5456 	struct e1000_softc *sc = iflib_get_softc(ctx);
5457 	int error = E1000_SUCCESS;
5458 	struct em_rx_queue *que;
5459 	int i;
5460 
5461 	MPASS(sc->rx_num_queues > 0);
5462 	MPASS(sc->rx_num_queues == nrxqsets);
5463 
5464 	/* First allocate the top level queue structs */
5465 	if (!(sc->rx_queues =
5466 	    (struct em_rx_queue *) malloc(sizeof(struct em_rx_queue) *
5467 	    sc->rx_num_queues, M_DEVBUF, M_NOWAIT | M_ZERO))) {
5468 		device_printf(iflib_get_dev(ctx),
5469 		    "Unable to allocate queue memory\n");
5470 		error = ENOMEM;
5471 		goto fail;
5472 	}
5473 
5474 	for (i = 0, que = sc->rx_queues; i < nrxqsets; i++, que++) {
5475 		/* Set up some basics */
5476 		struct rx_ring *rxr = &que->rxr;
5477 		KASSERT(__is_aligned(&rxr->rx_aim_snapshot, sizeof(uint64_t)),
5478 		    ("%s: misaligned RX AIM snapshot %p", __func__,
5479 		    &rxr->rx_aim_snapshot));
5480 		rxr->sc = que->sc = sc;
5481 		rxr->que = que;
5482 		que->me = rxr->me =  i;
5483 
5484 		/* get the virtual and physical address of hardware queues */
5485 		rxr->rx_base =
5486 		    (union e1000_rx_desc_extended *)vaddrs[i*nrxqs];
5487 		rxr->rx_paddr = paddrs[i*nrxqs];
5488 	}
5489 
5490 	if (bootverbose)
5491 		device_printf(iflib_get_dev(ctx),
5492 		    "allocated for %d rx_queues\n", sc->rx_num_queues);
5493 
5494 	return (0);
5495 fail:
5496 	em_if_queues_free(ctx);
5497 	return (error);
5498 }
5499 
5500 static void
em_if_queues_free(if_ctx_t ctx)5501 em_if_queues_free(if_ctx_t ctx)
5502 {
5503 	struct e1000_softc *sc = iflib_get_softc(ctx);
5504 	struct em_tx_queue *tx_que = sc->tx_queues;
5505 	struct em_rx_queue *rx_que = sc->rx_queues;
5506 
5507 	if (tx_que != NULL) {
5508 		for (int i = 0; i < sc->tx_num_queues; i++, tx_que++) {
5509 			struct tx_ring *txr = &tx_que->txr;
5510 			if (txr->tx_rsq == NULL)
5511 				break;
5512 
5513 			free(txr->tx_rsq, M_DEVBUF);
5514 			txr->tx_rsq = NULL;
5515 		}
5516 		free(sc->tx_queues, M_DEVBUF);
5517 		sc->tx_queues = NULL;
5518 	}
5519 
5520 	if (rx_que != NULL) {
5521 		free(sc->rx_queues, M_DEVBUF);
5522 		sc->rx_queues = NULL;
5523 	}
5524 }
5525 
5526 static u32
em_legacy_txdctl(struct e1000_hw * hw)5527 em_legacy_txdctl(struct e1000_hw *hw)
5528 {
5529 	u32 txdctl;
5530 
5531 	/*
5532 	 * Start with the established full-descriptor writeback policy.
5533 	 * Several generations have descriptor-queue errata for which it is
5534 	 * a documented workaround.  The unsafe early controllers are
5535 	 * overridden below.
5536 	 */
5537 	txdctl = EM_TX_PTHRESH | (EM_TX_HTHRESH << 8) |
5538 	    (EM_TX_WTHRESH << 16) | E1000_TXDCTL_GRAN;
5539 
5540 	switch (hw->mac.type) {
5541 	case e1000_82571:
5542 	case e1000_82572:
5543 	case e1000_82573:
5544 	case e1000_82574:
5545 	case e1000_82583:
5546 	case e1000_80003es2lan:
5547 		/* Match the Intel shared-code policy for these families. */
5548 		txdctl |= E1000_TXDCTL_COUNT_DESC;
5549 		break;
5550 	case e1000_ich8lan:
5551 	case e1000_ich9lan:
5552 	case e1000_ich10lan:
5553 	case e1000_pchlan:
5554 	case e1000_pch2lan:
5555 	case e1000_pch_lpt:
5556 	case e1000_pch_spt:
5557 	case e1000_pch_cnp:
5558 	case e1000_pch_tgp:
5559 	case e1000_pch_adp:
5560 	case e1000_pch_mtp:
5561 	case e1000_pch_ptp:
5562 	case e1000_pch_nvp:
5563 		/* Preserve the required bit set by the integrated shared code. */
5564 		txdctl |= (1U << 22);
5565 		break;
5566 	case e1000_82542:
5567 	case e1000_82543:
5568 	case e1000_82544:
5569 		/*
5570 		 * 82543 erratum 35 and 82544 erratum 20 require
5571 		 * WTHRESH=0.  Leave all descriptor-control thresholds at
5572 		 * their reset values on these early controllers.
5573 		 */
5574 		txdctl = 0;
5575 		break;
5576 	case e1000_82540:
5577 	case e1000_82545:
5578 	case e1000_82545_rev_3:
5579 	case e1000_82546:
5580 	case e1000_82546_rev_3:
5581 	case e1000_82541:
5582 	case e1000_82541_rev_2:
5583 	case e1000_82547:
5584 	case e1000_82547_rev_2:
5585 		break;
5586 	default:
5587 		KASSERT(0, ("%s: unsupported MAC type %d", __func__,
5588 		    hw->mac.type));
5589 		break;
5590 	}
5591 
5592 	return (txdctl);
5593 }
5594 
5595 static u32
igb_txdctl(struct e1000_hw * hw)5596 igb_txdctl(struct e1000_hw *hw)
5597 {
5598 	u32 pthresh;
5599 
5600 	switch (hw->mac.type) {
5601 	case e1000_i354:
5602 		pthresh = I354_TX_PTHRESH;
5603 		break;
5604 	case e1000_82575:
5605 	case e1000_82576:
5606 	case e1000_82580:
5607 	case e1000_i350:
5608 	case e1000_i210:
5609 	case e1000_i211:
5610 	case e1000_vfadapt:
5611 	case e1000_vfadapt_i350:
5612 		pthresh = IGB_TX_PTHRESH;
5613 		break;
5614 	default:
5615 		KASSERT(0, ("%s: unsupported MAC type %d", __func__,
5616 		    hw->mac.type));
5617 		pthresh = IGB_TX_PTHRESH;
5618 		break;
5619 	}
5620 
5621 	return (pthresh | (IGB_TX_HTHRESH << 8) |
5622 	    E1000_TXDCTL_QUEUE_ENABLE);
5623 }
5624 
5625 /*********************************************************************
5626  *
5627  *  Enable transmit unit.
5628  *
5629  **********************************************************************/
5630 void
em_initialize_transmit_rings(if_ctx_t ctx)5631 em_initialize_transmit_rings(if_ctx_t ctx)
5632 {
5633 	struct e1000_softc *sc = iflib_get_softc(ctx);
5634 	if_softc_ctx_t scctx = sc->shared;
5635 	struct em_tx_queue *que;
5636 	struct tx_ring	*txr;
5637 	struct e1000_hw	*hw = &sc->hw;
5638 	u32 txdctl;
5639 
5640 	for (int i = 0; i < sc->tx_num_queues; i++) {
5641 		u64 bus_addr;
5642 		caddr_t offp, endp;
5643 		uint32_t qid;
5644 
5645 		que = &sc->tx_queues[i];
5646 		txr = &que->txr;
5647 		qid = txr->me;
5648 		bus_addr = txr->tx_paddr;
5649 
5650 		/* Clear checksum offload context. */
5651 		offp = (caddr_t)txr + offsetof(struct tx_ring, csum_flags);
5652 		endp = (caddr_t)(txr + 1);
5653 		memset(offp, 0, endp - offp);
5654 
5655 		if (hw->mac.type >= igb_mac_min) {
5656 			txdctl = E1000_READ_REG(hw, E1000_TXDCTL(qid));
5657 			E1000_WRITE_REG(hw, E1000_TXDCTL(qid),
5658 			    txdctl & ~E1000_TXDCTL_QUEUE_ENABLE);
5659 			E1000_WRITE_FLUSH(hw);
5660 		}
5661 
5662 		/* Base and Len of TX Ring */
5663 		E1000_WRITE_REG(hw, E1000_TDLEN(qid),
5664 		    scctx->isc_ntxd[0] * sizeof(struct e1000_tx_desc));
5665 		E1000_WRITE_REG(hw, E1000_TDBAH(qid), (u32)(bus_addr >> 32));
5666 		E1000_WRITE_REG(hw, E1000_TDBAL(qid), (u32)bus_addr);
5667 		/* Init the HEAD/TAIL indices */
5668 		E1000_WRITE_REG(hw, E1000_TDT(qid), 0);
5669 		E1000_WRITE_REG(hw, E1000_TDH(qid), 0);
5670 
5671 		HW_DEBUGOUT2("Base = %x, Length = %x\n",
5672 		    E1000_READ_REG(hw, E1000_TDBAL(qid)),
5673 		    E1000_READ_REG(hw, E1000_TDLEN(qid)));
5674 
5675 		if (hw->mac.type < igb_mac_min)
5676 			txdctl = em_legacy_txdctl(hw);
5677 		else
5678 			txdctl = igb_txdctl(hw);
5679 
5680 		E1000_WRITE_REG(hw, E1000_TXDCTL(qid), txdctl);
5681 	}
5682 }
5683 
5684 static void
em_initialize_transmit_unit(if_ctx_t ctx)5685 em_initialize_transmit_unit(if_ctx_t ctx)
5686 {
5687 	struct e1000_softc *sc = iflib_get_softc(ctx);
5688 	struct e1000_hw *hw = &sc->hw;
5689 	u32 tctl, tarc, tipg = 0;
5690 
5691 	INIT_DEBUGOUT("em_initialize_transmit_unit: begin");
5692 	KASSERT(!sc->vf_ifp, ("%s called for a VF", __func__));
5693 
5694 	em_initialize_transmit_rings(ctx);
5695 
5696 	/* Set the default values for the Tx Inter Packet Gap timer */
5697 	switch (hw->mac.type) {
5698 	case e1000_80003es2lan:
5699 		tipg = DEFAULT_82543_TIPG_IPGR1;
5700 		tipg |= DEFAULT_80003ES2LAN_TIPG_IPGR2 <<
5701 		    E1000_TIPG_IPGR2_SHIFT;
5702 		break;
5703 	case e1000_82542:
5704 		tipg = DEFAULT_82542_TIPG_IPGT;
5705 		tipg |= DEFAULT_82542_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
5706 		tipg |= DEFAULT_82542_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
5707 		break;
5708 	default:
5709 		if (hw->phy.media_type == e1000_media_type_fiber ||
5710 		    hw->phy.media_type == e1000_media_type_internal_serdes)
5711 			tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
5712 		else
5713 			tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
5714 		tipg |= DEFAULT_82543_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
5715 		tipg |= DEFAULT_82543_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
5716 	}
5717 
5718 	if (hw->mac.type < igb_mac_min) {
5719 		E1000_WRITE_REG(hw, E1000_TIPG, tipg);
5720 		E1000_WRITE_REG(hw, E1000_TIDV, sc->tx_int_delay.value);
5721 
5722 		if (sc->tx_int_delay.value > 0)
5723 			sc->txd_cmd |= E1000_TXD_CMD_IDE;
5724 	}
5725 
5726 	if (hw->mac.type >= e1000_82540 && hw->mac.type < igb_mac_min)
5727 		E1000_WRITE_REG(hw, E1000_TADV, sc->tx_abs_int_delay.value);
5728 
5729 	if (hw->mac.type == e1000_82571 || hw->mac.type == e1000_82572) {
5730 		tarc = E1000_READ_REG(hw, E1000_TARC(0));
5731 		tarc |= TARC_SPEED_MODE_BIT;
5732 		E1000_WRITE_REG(hw, E1000_TARC(0), tarc);
5733 	} else if (hw->mac.type == e1000_80003es2lan) {
5734 		/* errata: program both queues to unweighted RR */
5735 		tarc = E1000_READ_REG(hw, E1000_TARC(0));
5736 		tarc |= 1;
5737 		E1000_WRITE_REG(hw, E1000_TARC(0), tarc);
5738 		tarc = E1000_READ_REG(hw, E1000_TARC(1));
5739 		tarc |= 1;
5740 		E1000_WRITE_REG(hw, E1000_TARC(1), tarc);
5741 	} else if (hw->mac.type == e1000_82574) {
5742 		tarc = E1000_READ_REG(hw, E1000_TARC(0));
5743 		tarc |= TARC_ERRATA_BIT;
5744 		if ( sc->tx_num_queues > 1) {
5745 			tarc |= (TARC_COMPENSATION_MODE | TARC_MQ_FIX);
5746 			E1000_WRITE_REG(hw, E1000_TARC(0), tarc);
5747 			E1000_WRITE_REG(hw, E1000_TARC(1), tarc);
5748 		} else
5749 			E1000_WRITE_REG(hw, E1000_TARC(0), tarc);
5750 	}
5751 
5752 	/* Program the Transmit Control Register */
5753 	tctl = E1000_READ_REG(hw, E1000_TCTL);
5754 	tctl &= ~E1000_TCTL_CT;
5755 	tctl |= (E1000_TCTL_PSP | E1000_TCTL_RTLC | E1000_TCTL_EN |
5756 		   (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT));
5757 
5758 	if (hw->mac.type >= e1000_82571 && hw->mac.type < igb_mac_min)
5759 		tctl |= E1000_TCTL_MULR;
5760 
5761 	/* This write will effectively turn on the transmit unit. */
5762 	E1000_WRITE_REG(hw, E1000_TCTL, tctl);
5763 
5764 	/* SPT and KBL errata workarounds */
5765 	if (hw->mac.type == e1000_pch_spt) {
5766 		u32 reg;
5767 		reg = E1000_READ_REG(hw, E1000_IOSFPC);
5768 		reg |= E1000_RCTL_RDMTS_HEX;
5769 		E1000_WRITE_REG(hw, E1000_IOSFPC, reg);
5770 		/* i218-i219 Specification Update 1.5.4.5 */
5771 		reg = E1000_READ_REG(hw, E1000_TARC(0));
5772 		reg &= ~E1000_TARC0_CB_MULTIQ_3_REQ;
5773 		reg |= E1000_TARC0_CB_MULTIQ_2_REQ;
5774 		E1000_WRITE_REG(hw, E1000_TARC(0), reg);
5775 	}
5776 }
5777 
5778 /*********************************************************************
5779  *
5780  *  Enable receive unit.
5781  *
5782  **********************************************************************/
5783 #define BSIZEPKT_ROUNDUP ((1<<E1000_SRRCTL_BSIZEPKT_SHIFT)-1)
5784 
5785 static u32
igb_rxdctl(struct e1000_softc * sc,u32 rxdctl)5786 igb_rxdctl(struct e1000_softc *sc, u32 rxdctl)
5787 {
5788 	struct e1000_hw *hw;
5789 	u32 mask, pthresh, wthresh;
5790 
5791 	hw = &sc->hw;
5792 	mask = IGB_RXDCTL_THRESH_MASK;
5793 	switch (hw->mac.type) {
5794 	case e1000_82575:
5795 		mask = IGB_82575_RXDCTL_THRESH_MASK;
5796 		pthresh = IGB_RX_PTHRESH;
5797 		wthresh = IGB_RX_WTHRESH;
5798 		break;
5799 	case e1000_82576:
5800 		pthresh = IGB_RX_PTHRESH;
5801 		wthresh = sc->intr_type == IFLIB_INTR_MSIX ?
5802 		    IGB_82576_RX_WTHRESH : IGB_RX_WTHRESH;
5803 		break;
5804 	case e1000_vfadapt:
5805 		/* 82576 VFs always need the MSI-X writeback workaround. */
5806 		pthresh = IGB_RX_PTHRESH;
5807 		wthresh = IGB_82576_RX_WTHRESH;
5808 		break;
5809 	case e1000_i354:
5810 		pthresh = I354_RX_PTHRESH;
5811 		wthresh = IGB_RX_WTHRESH;
5812 		break;
5813 	case e1000_82580:
5814 	case e1000_i350:
5815 	case e1000_i210:
5816 	case e1000_i211:
5817 	case e1000_vfadapt_i350:
5818 		pthresh = IGB_RX_PTHRESH;
5819 		wthresh = IGB_RX_WTHRESH;
5820 		break;
5821 	default:
5822 		KASSERT(0, ("%s: unsupported MAC type %d", __func__,
5823 		    hw->mac.type));
5824 		pthresh = IGB_RX_PTHRESH;
5825 		wthresh = IGB_RX_WTHRESH;
5826 		break;
5827 	}
5828 
5829 	rxdctl &= ~mask;
5830 	rxdctl |= pthresh | (IGB_RX_HTHRESH << 8) |
5831 	    (wthresh << 16) | E1000_RXDCTL_QUEUE_ENABLE;
5832 	return (rxdctl);
5833 }
5834 
5835 void
igb_initialize_receive_rings(if_ctx_t ctx,bool drop)5836 igb_initialize_receive_rings(if_ctx_t ctx, bool drop)
5837 {
5838 	struct e1000_softc *sc = iflib_get_softc(ctx);
5839 	if_softc_ctx_t scctx = sc->shared;
5840 	struct e1000_hw *hw = &sc->hw;
5841 	struct em_rx_queue *que;
5842 	u32 srrctl;
5843 
5844 	srrctl = (sc->rx_mbuf_sz + BSIZEPKT_ROUNDUP) >>
5845 	    E1000_SRRCTL_BSIZEPKT_SHIFT;
5846 	srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
5847 	if (drop)
5848 		srrctl |= E1000_SRRCTL_DROP_EN;
5849 
5850 	for (int i = 0; i < sc->rx_num_queues; i++) {
5851 		struct rx_ring *rxr;
5852 		u64 bus_addr;
5853 		u32 rxdctl;
5854 		uint32_t qid;
5855 
5856 		que = &sc->rx_queues[i];
5857 		rxr = &que->rxr;
5858 		bus_addr = rxr->rx_paddr;
5859 		qid = rxr->me;
5860 
5861 		rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(qid));
5862 		E1000_WRITE_REG(hw, E1000_RXDCTL(qid),
5863 		    rxdctl & ~E1000_RXDCTL_QUEUE_ENABLE);
5864 		E1000_WRITE_FLUSH(hw);
5865 
5866 		E1000_WRITE_REG(hw, E1000_RDLEN(qid),
5867 		    scctx->isc_nrxd[0] * sizeof(struct e1000_rx_desc));
5868 		E1000_WRITE_REG(hw, E1000_RDBAH(qid),
5869 		    (uint32_t)(bus_addr >> 32));
5870 		E1000_WRITE_REG(hw, E1000_RDBAL(qid), (uint32_t)bus_addr);
5871 		E1000_WRITE_REG(hw, E1000_RDH(qid), 0);
5872 		E1000_WRITE_REG(hw, E1000_RDT(qid), 0);
5873 		E1000_WRITE_REG(hw, E1000_SRRCTL(qid), srrctl);
5874 
5875 		rxdctl = igb_rxdctl(sc, rxdctl);
5876 		E1000_WRITE_REG(hw, E1000_RXDCTL(qid), rxdctl);
5877 	}
5878 }
5879 
5880 static bool
em_integrated_jumbo_rx(struct e1000_hw * hw)5881 em_integrated_jumbo_rx(struct e1000_hw *hw)
5882 {
5883 	switch (hw->mac.type) {
5884 	case e1000_ich9lan:
5885 	case e1000_ich10lan:
5886 	case e1000_pchlan:
5887 	case e1000_pch2lan:
5888 	case e1000_pch_lpt:
5889 	case e1000_pch_spt:
5890 	case e1000_pch_cnp:
5891 	case e1000_pch_tgp:
5892 	case e1000_pch_adp:
5893 	case e1000_pch_mtp:
5894 	case e1000_pch_ptp:
5895 	case e1000_pch_nvp:
5896 		return (true);
5897 	default:
5898 		return (false);
5899 	}
5900 }
5901 
5902 static void
em_initialize_receive_unit(if_ctx_t ctx)5903 em_initialize_receive_unit(if_ctx_t ctx)
5904 {
5905 	struct e1000_softc *sc = iflib_get_softc(ctx);
5906 	if_softc_ctx_t scctx = sc->shared;
5907 	if_t ifp = iflib_get_ifp(ctx);
5908 	struct e1000_hw *hw = &sc->hw;
5909 	struct em_rx_queue *que;
5910 	int i;
5911 	uint32_t rctl, rxcsum;
5912 
5913 	INIT_DEBUGOUT("em_initialize_receive_units: begin");
5914 	KASSERT(!sc->vf_ifp, ("%s called for a VF", __func__));
5915 
5916 	/*
5917 	 * Make sure receives are disabled while setting up the descriptor
5918 	 * ring.
5919 	 */
5920 	rctl = E1000_READ_REG(hw, E1000_RCTL);
5921 	/* Do not disable if ever enabled on this hardware. */
5922 	if (hw->mac.type != e1000_82574 &&
5923 	    hw->mac.type != e1000_82583)
5924 		E1000_WRITE_REG(hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
5925 
5926 	/* Setup the Receive Control Register. */
5927 	rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
5928 	rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
5929 	    E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
5930 	    (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
5931 	rctl &= ~E1000_RCTL_SBP;
5932 
5933 	if (igb_iov_enabled(sc) || if_getmtu(ifp) > ETHERMTU)
5934 		rctl |= E1000_RCTL_LPE;
5935 	else
5936 		rctl &= ~E1000_RCTL_LPE;
5937 	if (!em_disable_crc_stripping)
5938 		rctl |= E1000_RCTL_SECRC;
5939 
5940 	/* lem/em default interrupt moderation */
5941 	if (hw->mac.type < igb_mac_min) {
5942 		if (hw->mac.type >= e1000_82540) {
5943 			E1000_WRITE_REG(hw, E1000_RADV,
5944 			    sc->rx_abs_int_delay.value);
5945 
5946 			/* Set the default interrupt throttling rate */
5947 			E1000_WRITE_REG(hw, E1000_ITR,
5948 			    EM_INTS_TO_ITR(em_max_interrupt_rate));
5949 
5950 			/*
5951 			 * The 82574 MSI-X EITR registers are programmed
5952 			 * with the same value further below.  Either way
5953 			 * the hardware now holds the default rate, so seed
5954 			 * the software copy to match; otherwise a stale
5955 			 * itr_setting left over from AIM makes em_newitr()
5956 			 * skip the write that would restore it.
5957 			 */
5958 			for (i = 0, que = sc->rx_queues; i < sc->rx_num_queues;
5959 			    i++, que++)
5960 				que->itr_setting =
5961 				    EM_INTS_TO_ITR(em_max_interrupt_rate);
5962 		}
5963 
5964 		/* XXX TEMPORARY WORKAROUND: on some systems with 82573
5965 		 * long latencies are observed, like Lenovo X60. This
5966 		 * change eliminates the problem, but since having positive
5967 		 * values in RDTR is a known source of problems on other
5968 		 * platforms another solution is being sought.
5969 		 */
5970 		if (hw->mac.type == e1000_82573)
5971 			E1000_WRITE_REG(hw, E1000_RDTR, 0x20);
5972 		else
5973 			E1000_WRITE_REG(hw, E1000_RDTR,
5974 			    sc->rx_int_delay.value);
5975 	}
5976 
5977 	if (hw->mac.type >= em_mac_min) {
5978 		uint32_t rfctl;
5979 		/* Use extended rx descriptor formats */
5980 		rfctl = E1000_READ_REG(hw, E1000_RFCTL);
5981 		rfctl |= E1000_RFCTL_EXTEN;
5982 
5983 		/*
5984 		 * When using MSI-X interrupts we need to throttle
5985 		 * using the EITR register (82574 only)
5986 		 */
5987 		if (hw->mac.type == e1000_82574) {
5988 			for (int i = 0; i < 4; i++)
5989 				E1000_WRITE_REG(hw, E1000_EITR_82574(i),
5990 				    EM_INTS_TO_ITR(em_max_interrupt_rate));
5991 			/* Disable accelerated acknowledge */
5992 			rfctl |= E1000_RFCTL_ACK_DIS;
5993 		}
5994 		E1000_WRITE_REG(hw, E1000_RFCTL, rfctl);
5995 	}
5996 
5997 	rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
5998 	if (if_getcapenable(ifp) & IFCAP_RXCSUM) {
5999 		rxcsum |= E1000_RXCSUM_TUOFL | E1000_RXCSUM_IPOFL;
6000 		if (hw->mac.type > e1000_82575)
6001 			rxcsum |= E1000_RXCSUM_CRCOFL;
6002 		else if (hw->mac.type < em_mac_min &&
6003 		    if_getcapenable(ifp) & IFCAP_HWCSUM_IPV6)
6004 			rxcsum |= E1000_RXCSUM_IPV6OFL;
6005 	} else {
6006 		rxcsum &= ~(E1000_RXCSUM_IPOFL | E1000_RXCSUM_TUOFL);
6007 		if (hw->mac.type > e1000_82575)
6008 			rxcsum &= ~E1000_RXCSUM_CRCOFL;
6009 		else if (hw->mac.type < em_mac_min)
6010 			rxcsum &= ~E1000_RXCSUM_IPV6OFL;
6011 	}
6012 
6013 	if (sc->rx_num_queues > 1) {
6014 		/* RSS hash needed in the Rx descriptor */
6015 		rxcsum |= E1000_RXCSUM_PCSD;
6016 
6017 		if (hw->mac.type >= igb_mac_min)
6018 			igb_initialize_rss_mapping(sc);
6019 		else
6020 			em_initialize_rss_mapping(sc);
6021 	}
6022 	E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
6023 
6024 	for (i = 0, que = sc->rx_queues;
6025 	    hw->mac.type < igb_mac_min && i < sc->rx_num_queues;
6026 	    i++, que++) {
6027 		struct rx_ring *rxr = &que->rxr;
6028 		/* Setup the Base and Length of the Rx Descriptor Ring */
6029 		u64 bus_addr = rxr->rx_paddr;
6030 		uint32_t qid = rxr->me;
6031 #if 0
6032 		u32 rdt = sc->rx_num_queues -1;  /* default */
6033 #endif
6034 
6035 		E1000_WRITE_REG(hw, E1000_RDLEN(qid),
6036 		    scctx->isc_nrxd[0] *
6037 		    sizeof(union e1000_rx_desc_extended));
6038 		E1000_WRITE_REG(hw, E1000_RDBAH(qid), (u32)(bus_addr >> 32));
6039 		E1000_WRITE_REG(hw, E1000_RDBAL(qid), (u32)bus_addr);
6040 		/* Setup the Head and Tail Descriptor Pointers */
6041 		E1000_WRITE_REG(hw, E1000_RDH(qid), 0);
6042 		E1000_WRITE_REG(hw, E1000_RDT(qid), 0);
6043 	}
6044 
6045 	/* Increase receive-descriptor prefetching for integrated jumbo MACs. */
6046 	if (em_integrated_jumbo_rx(hw) && if_getmtu(ifp) > ETHERMTU) {
6047 		u32 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(0));
6048 
6049 		rxdctl &= ~(EM_RXDCTL_PTHRESH_MASK |
6050 		    EM_RXDCTL_HTHRESH_MASK);
6051 		rxdctl |= EM_JUMBO_RX_PTHRESH |
6052 		    (EM_JUMBO_RX_HTHRESH << 8);
6053 		E1000_WRITE_REG(hw, E1000_RXDCTL(0), rxdctl);
6054 	} else if (hw->mac.type == e1000_82574) {
6055 		/* RXDCTL(0) writes are mirrored to RXDCTL(1) on 82574. */
6056 		for (int i = 0; i < sc->rx_num_queues; i++) {
6057 			u32 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(i));
6058 
6059 			rxdctl &= ~EM_RXDCTL_THRESH_MASK;
6060 			rxdctl |= EM_82574_RX_PTHRESH |
6061 			    (EM_82574_RX_HTHRESH << 8) |
6062 			    (EM_82574_RX_WTHRESH << 16) |
6063 			    E1000_RXDCTL_THRESH_UNIT_DESC;
6064 			E1000_WRITE_REG(hw, E1000_RXDCTL(i), rxdctl);
6065 		}
6066 	} else if (hw->mac.type >= igb_mac_min) {
6067 		bool drop;
6068 		u32 psize;
6069 
6070 		if (igb_iov_enabled(sc)) {
6071 			E1000_WRITE_REG(hw, E1000_RLPML,
6072 			    IGB_IOV_MAX_FRAME_SIZE);
6073 		} else if (if_getmtu(ifp) > ETHERMTU) {
6074 			psize = scctx->isc_max_frame_size;
6075 			/* are we on a vlan? */
6076 			if (if_vlantrunkinuse(ifp))
6077 				psize += VLAN_TAG_SIZE;
6078 
6079 			E1000_WRITE_REG(hw, E1000_RLPML, psize);
6080 		}
6081 
6082 		/*
6083 		 * If TX flow control is disabled and there's >1 queue
6084 		 * defined, enable DROP.
6085 		 *
6086 		 * This drops frames rather than hanging the RX MAC for all
6087 		 * queues.
6088 		 */
6089 		drop = igb_iov_enabled(sc) ||
6090 		    ((sc->rx_num_queues > 1) &&
6091 		    (sc->fc == e1000_fc_none ||
6092 		    sc->fc == e1000_fc_rx_pause));
6093 		igb_initialize_receive_rings(ctx, drop);
6094 	} else if (hw->mac.type >= e1000_pch2lan) {
6095 		if (if_getmtu(ifp) > ETHERMTU)
6096 			e1000_lv_jumbo_workaround_ich8lan(hw, true);
6097 		else
6098 			e1000_lv_jumbo_workaround_ich8lan(hw, false);
6099 	}
6100 
6101 	/* Make sure VLAN Filters are off */
6102 	rctl &= ~E1000_RCTL_VFE;
6103 
6104 	/* Set up packet buffer size, overridden by per queue srrctl on igb */
6105 	if (hw->mac.type < igb_mac_min) {
6106 		if (sc->rx_mbuf_sz > 2048 && sc->rx_mbuf_sz <= 4096)
6107 			rctl |= E1000_RCTL_SZ_4096 | E1000_RCTL_BSEX;
6108 		else if (sc->rx_mbuf_sz > 4096 && sc->rx_mbuf_sz <= 8192)
6109 			rctl |= E1000_RCTL_SZ_8192 | E1000_RCTL_BSEX;
6110 		else if (sc->rx_mbuf_sz > 8192)
6111 			rctl |= E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX;
6112 		else {
6113 			rctl |= E1000_RCTL_SZ_2048;
6114 			rctl &= ~E1000_RCTL_BSEX;
6115 		}
6116 	} else
6117 		rctl |= E1000_RCTL_SZ_2048;
6118 
6119 	/*
6120 	 * rctl bits 11:10 are as follows
6121 	 * lem: reserved
6122 	 * em: DTYPE
6123 	 * igb: reserved
6124 	 * and should be 00 on all of the above
6125 	 */
6126 	rctl &= ~0x00000C00;
6127 
6128 	/* Write out the settings */
6129 	E1000_WRITE_REG(hw, E1000_RCTL, rctl);
6130 
6131 	return;
6132 }
6133 
6134 static void
em_if_vlan_register(if_ctx_t ctx,u16 vtag)6135 em_if_vlan_register(if_ctx_t ctx, u16 vtag)
6136 {
6137 	struct e1000_softc *sc = iflib_get_softc(ctx);
6138 	bool present;
6139 	u32 index, mask;
6140 
6141 	index = (vtag >> 5) & 0x7F;
6142 	mask = 1U << (vtag & 0x1F);
6143 	present = (sc->shadow_vfta[index] & mask) != 0;
6144 	/*
6145 	 * On a VF, record registration intent for replay even if the PF is not
6146 	 * ready to accept it yet.
6147 	 */
6148 	sc->shadow_vfta[index] |= mask;
6149 	sc->vf_vfta_stale[index] &= ~mask;
6150 	if (!present)
6151 		++sc->num_vlans;
6152 	if (sc->vf_ifp &&
6153 	    e1000_vfta_set_vf(&sc->hw, vtag, true) != E1000_SUCCESS) {
6154 		igbv_vlan_retry_add(sc, vtag);
6155 		device_printf(sc->dev,
6156 		    "VF VLAN %u add request failed\n", vtag);
6157 	} else if (sc->vf_ifp)
6158 		igbv_vlan_retry_clear(sc, vtag);
6159 	if (!sc->vf_ifp) {
6160 		if (igb_iov_enabled(sc))
6161 			igb_iov_rebuild_vlan(sc);
6162 		else
6163 			em_if_vlan_filter_write(sc, index);
6164 	}
6165 }
6166 
6167 static void
em_if_vlan_unregister(if_ctx_t ctx,u16 vtag)6168 em_if_vlan_unregister(if_ctx_t ctx, u16 vtag)
6169 {
6170 	struct e1000_softc *sc = iflib_get_softc(ctx);
6171 	bool present;
6172 	u32 index, mask;
6173 
6174 	index = (vtag >> 5) & 0x7F;
6175 	mask = 1U << (vtag & 0x1F);
6176 	present = (sc->shadow_vfta[index] & mask) != 0;
6177 	if (sc->vf_ifp)
6178 		igbv_vlan_retry_clear(sc, vtag);
6179 	if (sc->vf_ifp &&
6180 	    e1000_vfta_set_vf(&sc->hw, vtag, false) != E1000_SUCCESS) {
6181 		device_printf(sc->dev,
6182 		    "VF VLAN %u remove request failed\n", vtag);
6183 		/*
6184 		 * Hardware might still admit this VID.  Preserve its receive
6185 		 * tag until a successful VF reset proves the stale filter gone.
6186 		 */
6187 		sc->vf_vfta_stale[index] |= mask;
6188 	} else {
6189 		sc->vf_vfta_stale[index] &= ~mask;
6190 	}
6191 	sc->shadow_vfta[index] &= ~mask;
6192 	if (present)
6193 		--sc->num_vlans;
6194 	if (!sc->vf_ifp) {
6195 		if (igb_iov_enabled(sc))
6196 			igb_iov_rebuild_vlan(sc);
6197 		else
6198 			em_if_vlan_filter_write(sc, index);
6199 	}
6200 }
6201 
6202 static bool
em_if_vlan_filter_capable(if_ctx_t ctx)6203 em_if_vlan_filter_capable(if_ctx_t ctx)
6204 {
6205 	if_t ifp = iflib_get_ifp(ctx);
6206 
6207 	if ((if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER) &&
6208 	    !em_disable_crc_stripping)
6209 		return (true);
6210 
6211 	return (false);
6212 }
6213 
6214 static bool
em_if_vlan_filter_used(if_ctx_t ctx)6215 em_if_vlan_filter_used(if_ctx_t ctx)
6216 {
6217 	struct e1000_softc *sc = iflib_get_softc(ctx);
6218 
6219 	if (!em_if_vlan_filter_capable(ctx))
6220 		return (false);
6221 
6222 	for (int i = 0; i < EM_VFTA_SIZE; i++)
6223 		if (sc->shadow_vfta[i] != 0)
6224 			return (true);
6225 
6226 	return (false);
6227 }
6228 
6229 static void
em_if_vlan_filter_enable(struct e1000_softc * sc)6230 em_if_vlan_filter_enable(struct e1000_softc *sc)
6231 {
6232 	struct e1000_hw *hw = &sc->hw;
6233 	u32 reg;
6234 
6235 	reg = E1000_READ_REG(hw, E1000_RCTL);
6236 	reg &= ~E1000_RCTL_CFIEN;
6237 	reg |= E1000_RCTL_VFE;
6238 	E1000_WRITE_REG(hw, E1000_RCTL, reg);
6239 }
6240 
6241 static void
em_if_vlan_filter_disable(struct e1000_softc * sc)6242 em_if_vlan_filter_disable(struct e1000_softc *sc)
6243 {
6244 	struct e1000_hw *hw = &sc->hw;
6245 	u32 reg;
6246 
6247 	reg = E1000_READ_REG(hw, E1000_RCTL);
6248 	reg &= ~(E1000_RCTL_VFE | E1000_RCTL_CFIEN);
6249 	E1000_WRITE_REG(hw, E1000_RCTL, reg);
6250 }
6251 
6252 static void
em_if_vlan_filter_write(struct e1000_softc * sc,int changed_index)6253 em_if_vlan_filter_write(struct e1000_softc *sc, int changed_index)
6254 {
6255 	struct e1000_hw *hw = &sc->hw;
6256 
6257 	KASSERT(!sc->vf_ifp, ("VLAN filter write on VF\n"));
6258 
6259 	/* Disable interrupts for lem(4) devices during the filter change */
6260 	if (hw->mac.type < em_mac_min)
6261 		em_if_intr_disable(sc->ctx);
6262 
6263 	/*
6264 	 * Restore every retained VLAN after reset.  Also write the changed
6265 	 * word when its final VLAN was removed so stale hardware membership
6266 	 * does not survive a zero shadow value.
6267 	 */
6268 	for (int i = 0; i < EM_VFTA_SIZE; i++)
6269 		if (sc->shadow_vfta[i] != 0 || i == changed_index)
6270 			e1000_write_vfta(hw, i, sc->shadow_vfta[i]);
6271 
6272 	/* Re-enable interrupts for lem-class devices */
6273 	if (hw->mac.type < em_mac_min)
6274 		em_if_intr_enable(sc->ctx);
6275 }
6276 
6277 static void
em_setup_vlan_hw_support(if_ctx_t ctx)6278 em_setup_vlan_hw_support(if_ctx_t ctx)
6279 {
6280 	struct e1000_softc *sc = iflib_get_softc(ctx);
6281 	struct e1000_hw *hw = &sc->hw;
6282 	if_t ifp = iflib_get_ifp(ctx);
6283 	s32 error;
6284 	u32 max_frame_size, reg;
6285 	u16 vid;
6286 	int restore_failures;
6287 
6288 	/*
6289 	 * Only PFs have control over VLAN HW filtering
6290 	 * configuration. VFs have to act as if it's always
6291 	 * enabled.
6292 	 */
6293 	if (sc->vf_ifp) {
6294 		max_frame_size = min(sc->shared->isc_max_frame_size +
6295 		    VLAN_TAG_SIZE, IGB_IOV_MAX_FRAME_SIZE);
6296 		e1000_rlpml_set_vf(hw, max_frame_size);
6297 		restore_failures = 0;
6298 		for (vid = 0; vid < 4096; vid++) {
6299 			if ((sc->shadow_vfta[vid >> 5] &
6300 			    (1U << (vid & 0x1f))) == 0)
6301 				continue;
6302 			/*
6303 			 * Desired state remains in shadow_vfta for the next
6304 			 * replay if the PF mailbox is absent during reset.
6305 			 */
6306 			error = e1000_vfta_set_vf(hw, vid, true);
6307 			if (error != E1000_SUCCESS) {
6308 				igbv_vlan_retry_add(sc, vid);
6309 				restore_failures++;
6310 			} else
6311 				igbv_vlan_retry_clear(sc, vid);
6312 		}
6313 		if (restore_failures != 0)
6314 			device_printf(sc->dev,
6315 			    "VF VLAN restore failed for %d VIDs; retrying\n",
6316 			    restore_failures);
6317 		return;
6318 	}
6319 
6320 	if (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING &&
6321 	    !em_disable_crc_stripping) {
6322 		reg = E1000_READ_REG(hw, E1000_CTRL);
6323 		reg |= E1000_CTRL_VME;
6324 		E1000_WRITE_REG(hw, E1000_CTRL, reg);
6325 	} else {
6326 		reg = E1000_READ_REG(hw, E1000_CTRL);
6327 		reg &= ~E1000_CTRL_VME;
6328 		E1000_WRITE_REG(hw, E1000_CTRL, reg);
6329 	}
6330 
6331 	/*
6332 	 * SR-IOV always needs VFE for VF isolation.  When PF hardware VLAN
6333 	 * filtering is disabled, the IOV VLAN rebuild instead makes the PF
6334 	 * VLAN-promiscuous without disabling the global filter.
6335 	 */
6336 	if (!em_if_vlan_filter_capable(ctx))  {
6337 		if (igb_iov_enabled(sc)) {
6338 #ifdef PCI_IOV
6339 			sc->iov_pf_vlan_promisc = true;
6340 #endif
6341 			em_if_vlan_filter_enable(sc);
6342 		} else
6343 			em_if_vlan_filter_disable(sc);
6344 		return;
6345 	}
6346 #ifdef PCI_IOV
6347 	if (igb_iov_enabled(sc))
6348 		sc->iov_pf_vlan_promisc = false;
6349 #endif
6350 
6351 	/*
6352 	 * A soft reset zero's out the VFTA, so
6353 	 * we need to repopulate it now.
6354 	 * We also insert VLAN 0 in the filter list, so we pass VLAN 0 tagged
6355 	 * traffic through. This will write the entire table.
6356 	 */
6357 	em_if_vlan_register(ctx, 0);
6358 
6359 	/* Enable the Filter Table */
6360 	em_if_vlan_filter_enable(sc);
6361 }
6362 
6363 static void
em_if_intr_enable(if_ctx_t ctx)6364 em_if_intr_enable(if_ctx_t ctx)
6365 {
6366 	struct e1000_softc *sc = iflib_get_softc(ctx);
6367 	struct e1000_hw *hw = &sc->hw;
6368 	u32 ims_mask = IMS_ENABLE_MASK | em_fatal_error_intr_mask(sc);
6369 
6370 	if (sc->intr_type == IFLIB_INTR_MSIX) {
6371 		E1000_WRITE_REG(hw, EM_EIAC, sc->ims);
6372 		ims_mask |= sc->ims;
6373 	}
6374 
6375 	E1000_WRITE_REG(hw, E1000_IMS, ims_mask);
6376 	E1000_WRITE_FLUSH(hw);
6377 }
6378 
6379 static void
em_if_intr_disable(if_ctx_t ctx)6380 em_if_intr_disable(if_ctx_t ctx)
6381 {
6382 	struct e1000_softc *sc = iflib_get_softc(ctx);
6383 	struct e1000_hw *hw = &sc->hw;
6384 
6385 	if (sc->intr_type == IFLIB_INTR_MSIX)
6386 		E1000_WRITE_REG(hw, EM_EIAC, 0);
6387 	E1000_WRITE_REG(hw, E1000_IMC, 0xffffffff);
6388 	E1000_WRITE_FLUSH(hw);
6389 }
6390 
6391 static void
igb_if_intr_enable(if_ctx_t ctx)6392 igb_if_intr_enable(if_ctx_t ctx)
6393 {
6394 	struct e1000_softc *sc = iflib_get_softc(ctx);
6395 	struct e1000_hw *hw = &sc->hw;
6396 	u32 mask, reg;
6397 
6398 	if (igb_device_reset_pending(sc))
6399 		return;
6400 	if (__predict_true(sc->intr_type == IFLIB_INTR_MSIX)) {
6401 		mask = (sc->que_mask | sc->link_mask);
6402 		/*
6403 		 * VF interrupt controls are also mapped into these registers.
6404 		 * Preserve them and change only the PF vectors we own.
6405 		 */
6406 		reg = E1000_READ_REG(hw, E1000_EIAC);
6407 		E1000_WRITE_REG(hw, E1000_EIAC, reg | mask);
6408 		reg = E1000_READ_REG(hw, E1000_EIAM);
6409 		E1000_WRITE_REG(hw, E1000_EIAM, reg | mask);
6410 		igb_iov_intr_drain_stale(sc);
6411 		E1000_WRITE_REG(hw, E1000_EIMS, mask);
6412 		E1000_WRITE_REG(hw, E1000_IMS,
6413 		    E1000_IMS_LSC | igb_device_reset_intr_mask(sc) |
6414 		    igb_iov_intr_mask(sc) |
6415 		    em_fatal_error_intr_mask(sc));
6416 	} else {
6417 		mask = IMS_ENABLE_MASK | igb_device_reset_intr_mask(sc) |
6418 		    em_fatal_error_intr_mask(sc);
6419 		/* Reading ICR masks every shared interrupt before the filter runs. */
6420 		E1000_WRITE_REG(hw, E1000_IAM, mask);
6421 		E1000_WRITE_REG(hw, E1000_IMS, mask);
6422 	}
6423 	E1000_WRITE_FLUSH(hw);
6424 }
6425 
6426 static void
igb_if_intr_disable(if_ctx_t ctx)6427 igb_if_intr_disable(if_ctx_t ctx)
6428 {
6429 	struct e1000_softc *sc = iflib_get_softc(ctx);
6430 	struct e1000_hw *hw = &sc->hw;
6431 	u32 mask, reg;
6432 
6433 	/* This is the first CTX-owned register access after ICR.DRSTA. */
6434 	igb_prepare_device_reset(sc);
6435 
6436 	if (__predict_true(sc->intr_type == IFLIB_INTR_MSIX)) {
6437 		/*
6438 		 * Do not use a blanket EIMC write here.  VF interrupt controls
6439 		 * are mapped into the same PF register space, so clearing bits
6440 		 * we do not own can leave running VFs with interrupts masked.
6441 		 * Before initial queue configuration the owned mask is zero
6442 		 * because this driver has not enabled a vector yet.
6443 		 */
6444 		mask = (sc->que_mask | sc->link_mask);
6445 		reg = E1000_READ_REG(hw, E1000_EIAM);
6446 		E1000_WRITE_REG(hw, E1000_EIAM, reg & ~mask);
6447 		E1000_WRITE_REG(hw, E1000_EIMC, mask);
6448 		reg = E1000_READ_REG(hw, E1000_EIAC);
6449 		E1000_WRITE_REG(hw, E1000_EIAC, reg & ~mask);
6450 	} else
6451 		E1000_WRITE_REG(hw, E1000_IAM, 0);
6452 	E1000_WRITE_REG(hw, E1000_IMC, 0xffffffff);
6453 	E1000_WRITE_FLUSH(hw);
6454 }
6455 
6456 /*
6457  * Bit of a misnomer, what this really means is
6458  * to enable OS management of the system... aka
6459  * to disable special hardware management features
6460  */
6461 static void
em_init_manageability(struct e1000_softc * sc)6462 em_init_manageability(struct e1000_softc *sc)
6463 {
6464 	/* A shared code workaround */
6465 #define E1000_82542_MANC2H E1000_MANC2H
6466 	if (sc->has_manage) {
6467 		int manc2h = E1000_READ_REG(&sc->hw, E1000_MANC2H);
6468 		int manc = E1000_READ_REG(&sc->hw, E1000_MANC);
6469 
6470 		/* disable hardware interception of ARP */
6471 		manc &= ~(E1000_MANC_ARP_EN);
6472 
6473 		/* enable receiving management packets to the host */
6474 		manc |= E1000_MANC_EN_MNG2HOST;
6475 #define E1000_MNG2HOST_PORT_623 (1 << 5)
6476 #define E1000_MNG2HOST_PORT_664 (1 << 6)
6477 		manc2h |= E1000_MNG2HOST_PORT_623;
6478 		manc2h |= E1000_MNG2HOST_PORT_664;
6479 		E1000_WRITE_REG(&sc->hw, E1000_MANC2H, manc2h);
6480 		E1000_WRITE_REG(&sc->hw, E1000_MANC, manc);
6481 	}
6482 }
6483 
6484 /*
6485  * Give control back to hardware management
6486  * controller if there is one.
6487  */
6488 static void
em_release_manageability(struct e1000_softc * sc)6489 em_release_manageability(struct e1000_softc *sc)
6490 {
6491 	if (sc->has_manage) {
6492 		int manc = E1000_READ_REG(&sc->hw, E1000_MANC);
6493 
6494 		/* re-enable hardware interception of ARP */
6495 		manc |= E1000_MANC_ARP_EN;
6496 		manc &= ~E1000_MANC_EN_MNG2HOST;
6497 
6498 		E1000_WRITE_REG(&sc->hw, E1000_MANC, manc);
6499 	}
6500 }
6501 
6502 /*
6503  * em_get_hw_control sets the {CTRL_EXT|FWSM}:DRV_LOAD bit.
6504  * For ASF and Pass Through versions of f/w this means
6505  * that the driver is loaded. For AMT version type f/w
6506  * this means that the network i/f is open.
6507  */
6508 static void
em_get_hw_control(struct e1000_softc * sc)6509 em_get_hw_control(struct e1000_softc *sc)
6510 {
6511 	u32 ctrl_ext, swsm;
6512 
6513 	if (sc->vf_ifp)
6514 		return;
6515 
6516 	if (sc->hw.mac.type == e1000_82573) {
6517 		swsm = E1000_READ_REG(&sc->hw, E1000_SWSM);
6518 		E1000_WRITE_REG(&sc->hw, E1000_SWSM,
6519 		    swsm | E1000_SWSM_DRV_LOAD);
6520 		return;
6521 	}
6522 	/* else */
6523 	ctrl_ext = E1000_READ_REG(&sc->hw, E1000_CTRL_EXT);
6524 	E1000_WRITE_REG(&sc->hw, E1000_CTRL_EXT,
6525 	    ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
6526 }
6527 
6528 /*
6529  * em_release_hw_control resets {CTRL_EXT|FWSM}:DRV_LOAD bit.
6530  * For ASF and Pass Through versions of f/w this means that
6531  * the driver is no longer loaded. For AMT versions of the
6532  * f/w this means that the network i/f is closed.
6533  */
6534 static void
em_release_hw_control(struct e1000_softc * sc)6535 em_release_hw_control(struct e1000_softc *sc)
6536 {
6537 	u32 ctrl_ext, swsm;
6538 
6539 	if (!sc->has_manage)
6540 		return;
6541 
6542 	if (sc->hw.mac.type == e1000_82573) {
6543 		swsm = E1000_READ_REG(&sc->hw, E1000_SWSM);
6544 		E1000_WRITE_REG(&sc->hw, E1000_SWSM,
6545 		    swsm & ~E1000_SWSM_DRV_LOAD);
6546 		return;
6547 	}
6548 	/* else */
6549 	ctrl_ext = E1000_READ_REG(&sc->hw, E1000_CTRL_EXT);
6550 	E1000_WRITE_REG(&sc->hw, E1000_CTRL_EXT,
6551 	    ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
6552 	return;
6553 }
6554 
6555 bool
em_is_valid_ether_addr(const u8 * addr)6556 em_is_valid_ether_addr(const u8 *addr)
6557 {
6558 	static const u8 zero_addr[ETHER_ADDR_LEN];
6559 
6560 	return (!ETHER_IS_MULTICAST(addr) &&
6561 	    memcmp(addr, zero_addr, ETHER_ADDR_LEN) != 0);
6562 }
6563 
6564 static bool
em_automask_tso(if_ctx_t ctx)6565 em_automask_tso(if_ctx_t ctx)
6566 {
6567 	struct e1000_softc *sc = iflib_get_softc(ctx);
6568 	if_softc_ctx_t scctx = iflib_get_softc_ctx(ctx);
6569 	if_t ifp = iflib_get_ifp(ctx);
6570 	bool reset_needed;
6571 	int drvflags;
6572 
6573 	if (!em_unsupported_tso && sc->link_speed &&
6574 	    sc->link_speed != SPEED_1000 &&
6575 	    scctx->isc_capenable & IFCAP_TSO) {
6576 		device_printf(sc->dev,
6577 		    "Disabling TSO for 10/100 Ethernet.\n");
6578 		sc->tso_automasked = scctx->isc_capenable & IFCAP_TSO;
6579 		scctx->isc_capenable &= ~IFCAP_TSO;
6580 		if_setcapenablebit(ifp, 0, IFCAP_TSO);
6581 	} else if (sc->link_speed == SPEED_1000 && sc->tso_automasked) {
6582 		device_printf(sc->dev, "Re-enabling TSO for GbE.\n");
6583 		scctx->isc_capenable |= sc->tso_automasked;
6584 		if_setcapenablebit(ifp, sc->tso_automasked, 0);
6585 		sc->tso_automasked = 0;
6586 	} else {
6587 		return (false);
6588 	}
6589 
6590 	/*
6591 	 * Reset a running interface, or one being initialized while
6592 	 * administratively up.  OACTIVE remains set after iflib_stop(), so
6593 	 * it alone cannot distinguish initialization from an interface that
6594 	 * is down.  In other states, the next initialization will apply the
6595 	 * updated capabilities.
6596 	 */
6597 	drvflags = if_getdrvflags(ifp);
6598 	reset_needed = (drvflags & IFF_DRV_RUNNING) != 0 ||
6599 	    ((drvflags & IFF_DRV_OACTIVE) != 0 &&
6600 	    (if_getflags(ifp) & IFF_UP) != 0);
6601 	if (!reset_needed)
6602 		return (false);
6603 
6604 	/* iflib_init_locked handles ifnet hwassistbits */
6605 	iflib_request_reset(ctx);
6606 	return (true);
6607 }
6608 
6609 /*
6610 ** Parse the interface capabilities with regard
6611 ** to both system management and wake-on-lan for
6612 ** later use.
6613 */
6614 static void
em_get_wakeup(if_ctx_t ctx)6615 em_get_wakeup(if_ctx_t ctx)
6616 {
6617 	struct e1000_softc *sc = iflib_get_softc(ctx);
6618 	if_softc_ctx_t scctx = iflib_get_softc_ctx(ctx);
6619 	device_t dev = iflib_get_dev(ctx);
6620 	u16 eeprom_data = 0, device_id, apme_mask;
6621 	bool apme;
6622 	int error, wol_capabilities;
6623 
6624 	sc->has_manage = e1000_enable_mng_pass_thru(&sc->hw);
6625 	apme_mask = EM_EEPROM_APME_HIGH;
6626 	error = E1000_SUCCESS;
6627 
6628 	switch (sc->hw.mac.type) {
6629 	case e1000_82542:
6630 	case e1000_82543:
6631 		break;
6632 	case e1000_82544:
6633 		error = e1000_read_nvm(&sc->hw,
6634 		    NVM_INIT_CONTROL2_REG, 1, &eeprom_data);
6635 		apme_mask = EM_EEPROM_APME_LOW;
6636 		break;
6637 	case e1000_82541:
6638 	case e1000_82547:
6639 		error = e1000_read_nvm(&sc->hw,
6640 		    NVM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
6641 		/* The EI parts place APM Enable in the low byte. */
6642 		if (sc->hw.device_id != E1000_DEV_ID_82541ER_LOM)
6643 			apme_mask = EM_EEPROM_APME_LOW;
6644 		break;
6645 	case e1000_82546:
6646 	case e1000_82546_rev_3:
6647 		if (sc->hw.bus.func == 1) {
6648 			error = e1000_read_nvm(&sc->hw,
6649 			    NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
6650 			break;
6651 		} else
6652 			error = e1000_read_nvm(&sc->hw,
6653 			    NVM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
6654 		break;
6655 	case e1000_82573:
6656 	case e1000_82583:
6657 		sc->has_amt = true;
6658 		/* FALLTHROUGH */
6659 	case e1000_82571:
6660 	case e1000_82572:
6661 	case e1000_80003es2lan:
6662 		if (sc->hw.bus.func == 1) {
6663 			error = e1000_read_nvm(&sc->hw,
6664 			    NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
6665 			break;
6666 		} else
6667 			error = e1000_read_nvm(&sc->hw,
6668 			    NVM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
6669 		break;
6670 	case e1000_ich8lan:
6671 	case e1000_ich9lan:
6672 	case e1000_ich10lan:
6673 	case e1000_pchlan:
6674 	case e1000_pch2lan:
6675 	case e1000_pch_lpt:
6676 	case e1000_pch_spt:
6677 	case e1000_pch_cnp:
6678 	case e1000_pch_tgp:
6679 	case e1000_pch_adp:
6680 	case e1000_pch_mtp:
6681 	case e1000_pch_ptp:
6682 	case e1000_pch_nvp:
6683 		apme_mask = E1000_WUC_APME;
6684 		sc->has_amt = true;
6685 		eeprom_data = E1000_READ_REG(&sc->hw, E1000_WUC);
6686 		if (sc->hw.mac.type > e1000_ich10lan &&
6687 		    (eeprom_data & E1000_WUC_PHY_WAKE) != 0)
6688 			sc->wol_phy_wakeup = true;
6689 		break;
6690 	case e1000_82575:
6691 	case e1000_82576:
6692 		if (sc->hw.bus.func == 1)
6693 			error = e1000_read_nvm(&sc->hw,
6694 			    NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
6695 		else
6696 			error = e1000_read_nvm(&sc->hw,
6697 			    NVM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
6698 		sc->has_amt = true;
6699 		break;
6700 	case e1000_82580:
6701 	case e1000_i350:
6702 	case e1000_i354:
6703 	case e1000_i210:
6704 	case e1000_i211:
6705 		error = e1000_read_nvm(&sc->hw,
6706 		    NVM_INIT_CONTROL3_PORT_A +
6707 		    NVM_82580_LAN_FUNC_OFFSET(sc->hw.bus.func), 1,
6708 		    &eeprom_data);
6709 		sc->has_amt = true;
6710 		break;
6711 	default:
6712 		error = e1000_read_nvm(&sc->hw,
6713 		    NVM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
6714 		break;
6715 	}
6716 	if (error != E1000_SUCCESS && bootverbose)
6717 		device_printf(dev, "NVM read failed while checking WoL: %d\n",
6718 		    error);
6719 	if ((sc->hw.mac.type == e1000_i210 ||
6720 	    sc->hw.mac.type == e1000_i211) &&
6721 	    sc->hw.nvm.type == e1000_nvm_invm) {
6722 		/* The shared reader does not expose the optional iNVM word. */
6723 		apme = (E1000_READ_REG(&sc->hw, E1000_WUC) &
6724 		    E1000_WUC_APME) != 0;
6725 	} else {
6726 		apme = error == E1000_SUCCESS &&
6727 		    (eeprom_data & apme_mask) != 0;
6728 	}
6729 	wol_capabilities = pci_has_pme(dev, PCI_POWERSTATE_D3_HOT) ?
6730 	    IFCAP_WOL : 0;
6731 	if (sc->hw.mac.type == e1000_82542 ||
6732 	    sc->hw.mac.type == e1000_82543)
6733 		wol_capabilities = 0;
6734 
6735 	/* APME selects the default; board and port restrictions select support. */
6736 	device_id = pci_get_device(dev);
6737 	switch (device_id) {
6738 	case E1000_DEV_ID_82542:
6739 	case E1000_DEV_ID_82543GC_FIBER:
6740 	case E1000_DEV_ID_82543GC_COPPER:
6741 	case E1000_DEV_ID_82541ER:
6742 	case E1000_DEV_ID_82541ER_LOM:
6743 	case E1000_DEV_ID_82544EI_FIBER:
6744 	case E1000_DEV_ID_82545EM_COPPER:
6745 	case E1000_DEV_ID_82545EM_FIBER:
6746 	case E1000_DEV_ID_82546EB_QUAD_COPPER:
6747 	case E1000_DEV_ID_82546GB_QUAD_COPPER:
6748 	case E1000_DEV_ID_82546GB_PCIE:
6749 		wol_capabilities = 0;
6750 		break;
6751 	case E1000_DEV_ID_82546EB_FIBER:
6752 	case E1000_DEV_ID_82546GB_FIBER:
6753 		/*
6754 		 * Wake events are supported only on port A for dual fiber,
6755 		 * regardless of the NVM setting.
6756 		 */
6757 		if (sc->hw.bus.func == 1)
6758 			wol_capabilities = 0;
6759 		break;
6760 	case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
6761 		/* if quad port adapter, disable WoL on all but port A */
6762 		if (global_quad_port_a != 0)
6763 			wol_capabilities = 0;
6764 		else
6765 			wol_capabilities &= ~IFCAP_WOL_UCAST;
6766 		/* Reset for multiple quad port adapters */
6767 		if (++global_quad_port_a == 4)
6768 			global_quad_port_a = 0;
6769 		break;
6770 	case E1000_DEV_ID_82571EB_COPPER:
6771 	case E1000_DEV_ID_82571EB_FIBER:
6772 	case E1000_DEV_ID_82571EB_SERDES:
6773 		/* These dual-port adapters support wake only on port A. */
6774 		if (sc->hw.bus.func == 1)
6775 			wol_capabilities = 0;
6776 		break;
6777 	case E1000_DEV_ID_82571EB_SERDES_QUAD:
6778 		wol_capabilities = 0;
6779 		break;
6780 	case E1000_DEV_ID_82571EB_QUAD_COPPER:
6781 	case E1000_DEV_ID_82571EB_QUAD_FIBER:
6782 	case E1000_DEV_ID_82571EB_QUAD_COPPER_LP:
6783 	case E1000_DEV_ID_82571PT_QUAD_COPPER:
6784 		/* if quad port adapter, disable WoL on all but port A */
6785 		if (global_quad_port_a != 0)
6786 			wol_capabilities = 0;
6787 		/* Reset for multiple quad port adapters */
6788 		if (++global_quad_port_a == 4)
6789 			global_quad_port_a = 0;
6790 		break;
6791 	case E1000_DEV_ID_82575GB_QUAD_COPPER:
6792 		wol_capabilities = 0;
6793 		break;
6794 	case E1000_DEV_ID_82575EB_FIBER_SERDES:
6795 	case E1000_DEV_ID_82576_FIBER:
6796 	case E1000_DEV_ID_82576_SERDES:
6797 		if (sc->hw.bus.func == 1)
6798 			wol_capabilities = 0;
6799 		break;
6800 	case E1000_DEV_ID_82576_QUAD_COPPER:
6801 	case E1000_DEV_ID_82576_QUAD_COPPER_ET2:
6802 		if (global_quad_port_a != 0)
6803 			wol_capabilities = 0;
6804 		if (++global_quad_port_a == 4)
6805 			global_quad_port_a = 0;
6806 		break;
6807 	default:
6808 		break;
6809 	}
6810 	/* Legacy and igb non-primary ports require an explicit NVM setting. */
6811 	if ((sc->hw.mac.type < e1000_82571 ||
6812 	    sc->hw.mac.type >= igb_mac_min) && sc->hw.bus.func != 0 &&
6813 	    !apme)
6814 		wol_capabilities = 0;
6815 
6816 	/* Some I350-family systems expose wake support but default it off. */
6817 	if ((sc->hw.mac.type == e1000_i350 &&
6818 	    pci_get_subvendor(dev) == EM_SUBVENDOR_HP) ||
6819 	    ((sc->hw.mac.type == e1000_i350 ||
6820 	    sc->hw.mac.type == e1000_i354) &&
6821 	    pci_get_subvendor(dev) == EM_SUBVENDOR_DELL) ||
6822 	    (sc->hw.mac.type == e1000_i350 &&
6823 	    ((pci_get_subdevice(dev) == EM_I350_SUBDEVICE_WOL_2 ||
6824 	    pci_get_subdevice(dev) == EM_I350_SUBDEVICE_WOL_3) &&
6825 	    sc->hw.bus.func == 0))) {
6826 		wol_capabilities = pci_has_pme(dev, PCI_POWERSTATE_D3_HOT) ?
6827 		    IFCAP_WOL : 0;
6828 		apme = false;
6829 	}
6830 	if (sc->hw.mac.type == e1000_i350 &&
6831 	    pci_get_subdevice(dev) == EM_I350_SUBDEVICE_WOL_1)
6832 		wol_capabilities = pci_has_pme(dev, PCI_POWERSTATE_D3_HOT) ?
6833 		    IFCAP_WOL : 0;
6834 
6835 	scctx->isc_capabilities &= ~IFCAP_WOL;
6836 	scctx->isc_capabilities |= wol_capabilities;
6837 	scctx->isc_capenable &= ~IFCAP_WOL;
6838 	if (wol_capabilities != 0 && apme)
6839 		scctx->isc_capenable |= IFCAP_WOL_MAGIC;
6840 }
6841 
6842 /* Configure the requested PCI Wake-on-LAN filters for suspend. */
6843 static int
em_enable_wakeup(if_ctx_t ctx)6844 em_enable_wakeup(if_ctx_t ctx)
6845 {
6846 	struct e1000_softc *sc = iflib_get_softc(ctx);
6847 	device_t dev = iflib_get_dev(ctx);
6848 	if_t ifp = iflib_get_ifp(ctx);
6849 	int enabled, error = 0, master_error, mcnt;
6850 	u32 ctrl, ctrl_ext, rctl, saved_rctl, wuc, wufc;
6851 	bool manage, rctl_modified;
6852 
6853 	if (sc->vf_ifp)
6854 		return (0);
6855 	if (!pci_has_pme(dev, PCI_POWERSTATE_D3_HOT))
6856 		return (0);
6857 
6858 	enabled = if_getcapenable(ifp) & if_getcapabilities(ifp) & IFCAP_WOL;
6859 	manage = e1000_enable_mng_pass_thru(&sc->hw);
6860 	rctl_modified = false;
6861 	wuc = 0;
6862 	/* Early 82545EM/82546EB need APM clocks for D3 manageability. */
6863 	if (manage && (sc->hw.mac.type == e1000_82545 ||
6864 	    sc->hw.mac.type == e1000_82546))
6865 		wuc = E1000_WUC_APME;
6866 	wufc = 0;
6867 	if ((enabled & IFCAP_WOL_MAGIC) != 0)
6868 		wufc |= E1000_WUFC_MAG;
6869 	if ((enabled & IFCAP_WOL_UCAST) != 0)
6870 		wufc |= E1000_WUFC_EX;
6871 	if ((enabled & IFCAP_WOL_MCAST) != 0) {
6872 		wufc |= E1000_WUFC_MC;
6873 		bzero(sc->mta, ETHER_ADDR_LEN *
6874 		    MAX_NUM_MULTICAST_ADDRESSES);
6875 		mcnt = if_foreach_llmaddr(ifp, em_copy_maddr, sc->mta);
6876 		if (mcnt < MAX_NUM_MULTICAST_ADDRESSES) {
6877 			e1000_update_mc_addr_list(&sc->hw, sc->mta, mcnt);
6878 		} else {
6879 			switch (sc->hw.mac.type) {
6880 			case e1000_82544:
6881 			case e1000_82540:
6882 			case e1000_82545:
6883 			case e1000_82545_rev_3:
6884 			case e1000_82546:
6885 			case e1000_82546_rev_3:
6886 			case e1000_82541:
6887 			case e1000_82541_rev_2:
6888 			case e1000_82547:
6889 			case e1000_82547_rev_2:
6890 			case e1000_82575:
6891 			case e1000_82576:
6892 			case e1000_82580:
6893 				/* These parts require an MTA hit for WUFC_MC. */
6894 				em_fill_wakeup_mta(&sc->hw);
6895 				break;
6896 			default:
6897 				break;
6898 			}
6899 		}
6900 	}
6901 
6902 	if (wufc == 0) {
6903 		if (sc->hw.mac.type >= e1000_82544) {
6904 			E1000_WRITE_REG(&sc->hw, E1000_WUFC, 0);
6905 			E1000_WRITE_REG(&sc->hw, E1000_WUC, wuc);
6906 			E1000_WRITE_REG(&sc->hw, E1000_WUS, ~0U);
6907 		}
6908 		if (sc->wol_phy_wakeup && sc->wol_phy_armed)
6909 			(void)em_disable_phy_wakeup(sc, NULL);
6910 		if (manage) {
6911 			if (sc->suspend_link_powered_down)
6912 				em_power_up_wakeup_link(sc);
6913 			em_configure_sx_low_power(sc, 0);
6914 			pci_enable_pme(dev);
6915 		} else {
6916 			em_power_down_wakeup_link(sc);
6917 			pci_clear_pme(dev);
6918 		}
6919 		goto master_disable;
6920 	}
6921 	bcopy(if_getlladdr(ifp), sc->hw.mac.addr, ETHER_ADDR_LEN);
6922 	error = e1000_rar_set(&sc->hw, sc->hw.mac.addr, 0);
6923 	if (error != E1000_SUCCESS) {
6924 		device_printf(dev,
6925 		    "Could not restore unicast wake address: %d\n", error);
6926 		goto pme;
6927 	}
6928 	saved_rctl = E1000_READ_REG(&sc->hw, E1000_RCTL);
6929 	rctl = saved_rctl;
6930 	rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_MO_3);
6931 	rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
6932 	    (sc->hw.mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
6933 	if ((wufc & E1000_WUFC_MC) != 0)
6934 		rctl |= E1000_RCTL_MPE;
6935 	E1000_WRITE_REG(&sc->hw, E1000_RCTL, rctl);
6936 	rctl_modified = true;
6937 
6938 	/* Advertise the wakeup capability */
6939 	if (sc->hw.mac.type >= e1000_82540) {
6940 		ctrl = E1000_READ_REG(&sc->hw, E1000_CTRL);
6941 		ctrl |= E1000_CTRL_ADVD3WUC;
6942 		if (sc->hw.mac.type < igb_mac_min && !sc->wol_phy_wakeup)
6943 			ctrl |= E1000_CTRL_EN_PHY_PWR_MGMT;
6944 		E1000_WRITE_REG(&sc->hw, E1000_CTRL, ctrl);
6945 	}
6946 
6947 	/* Keep the laser running on legacy fiber and SerDes adapters. */
6948 	if (sc->hw.mac.type < igb_mac_min &&
6949 	    (sc->hw.phy.media_type == e1000_media_type_fiber ||
6950 	    sc->hw.phy.media_type == e1000_media_type_internal_serdes)) {
6951 		ctrl_ext = E1000_READ_REG(&sc->hw, E1000_CTRL_EXT);
6952 		ctrl_ext |= E1000_CTRL_EXT_SDP3_DATA;
6953 		E1000_WRITE_REG(&sc->hw, E1000_CTRL_EXT, ctrl_ext);
6954 	}
6955 	E1000_WRITE_REG(&sc->hw, E1000_WUS, ~0U);
6956 	em_power_up_wakeup_link(sc);
6957 
6958 	if (sc->hw.mac.type >= e1000_ich8lan &&
6959 	    sc->hw.mac.type < igb_mac_min)
6960 		e1000_suspend_workarounds_ich8lan(&sc->hw);
6961 
6962 	if (sc->wol_phy_wakeup) {
6963 		error = em_enable_phy_wakeup(sc, wufc);
6964 		if (error)
6965 			goto pme;
6966 	} else {
6967 		/* Enable wakeup by the MAC */
6968 		E1000_WRITE_REG(&sc->hw, E1000_WUC,
6969 		    wuc | E1000_WUC_PME_EN);
6970 		E1000_WRITE_REG(&sc->hw, E1000_WUFC, wufc);
6971 	}
6972 
6973 	/* The IGP3 D3 power-down workaround is specific to the em family. */
6974 	if (sc->hw.mac.type < igb_mac_min &&
6975 	    sc->hw.phy.type == e1000_phy_igp_3)
6976 		e1000_igp3_phy_powerdown_workaround_ich8lan(&sc->hw);
6977 	em_configure_sx_low_power(sc, wufc);
6978 
6979 pme:
6980 	if (!error)
6981 		pci_enable_pme(dev);
6982 	else {
6983 		E1000_WRITE_REG(&sc->hw, E1000_WUFC, 0);
6984 		E1000_WRITE_REG(&sc->hw, E1000_WUC, wuc);
6985 		if (rctl_modified)
6986 			E1000_WRITE_REG(&sc->hw, E1000_RCTL, saved_rctl);
6987 		pci_clear_pme(dev);
6988 	}
6989 
6990 master_disable:
6991 	master_error = e1000_disable_pcie_master(&sc->hw);
6992 	if (master_error != E1000_SUCCESS)
6993 		device_printf(dev, "PCIe master disable timed out: %d\n",
6994 		    master_error);
6995 	master_error = pci_disable_busmaster(dev);
6996 	if (master_error != 0)
6997 		device_printf(dev, "PCI bus-master disable failed: %d\n",
6998 		    master_error);
6999 
7000 	return (error == E1000_SUCCESS ? 0 : EIO);
7001 }
7002 
7003 /* Configure the PCH low-power link modes used while the system sleeps. */
7004 static void
em_configure_sx_low_power(struct e1000_softc * sc,u32 wufc)7005 em_configure_sx_low_power(struct e1000_softc *sc, u32 wufc)
7006 {
7007 	struct e1000_hw *hw = &sc->hw;
7008 	struct e1000_dev_spec_ich8lan *dev_spec;
7009 	s32 error;
7010 	u16 eee_advert, lpi_ctrl;
7011 
7012 	if (hw->mac.type < e1000_pch_lpt || hw->mac.type >= igb_mac_min ||
7013 	    sc->suspend_link_powered_down)
7014 		return;
7015 
7016 	if (wufc != 0 &&
7017 	    (wufc & (E1000_WUFC_EX | E1000_WUFC_MC | E1000_WUFC_BC)) == 0) {
7018 		/* ULP cannot preserve directed or broad multicast wake. */
7019 		error = e1000_enable_ulp_lpt_lp(hw, true);
7020 		if (error != E1000_SUCCESS) {
7021 			device_printf(sc->dev,
7022 			    "Could not enter PHY ultra-low-power mode: %d\n",
7023 			    error);
7024 			return;
7025 		}
7026 	}
7027 
7028 	dev_spec = &hw->dev_spec.ich8lan;
7029 	if (hw->phy.type != e1000_phy_i217 || dev_spec->eee_disable ||
7030 	    dev_spec->eee_lp_ability == 0)
7031 		return;
7032 
7033 	error = hw->phy.ops.acquire(hw);
7034 	if (error != E1000_SUCCESS)
7035 		goto out;
7036 	error = hw->phy.ops.read_reg_locked(hw, I82579_LPI_CTRL, &lpi_ctrl);
7037 	if (error != E1000_SUCCESS)
7038 		goto release;
7039 	error = e1000_read_emi_reg_locked(hw, I217_EEE_ADVERTISEMENT,
7040 	    &eee_advert);
7041 	if (error != E1000_SUCCESS)
7042 		goto release;
7043 
7044 	if ((eee_advert & dev_spec->eee_lp_ability &
7045 	    I82579_EEE_100_SUPPORTED) != 0)
7046 		lpi_ctrl |= I82579_LPI_CTRL_100_ENABLE;
7047 	if ((eee_advert & dev_spec->eee_lp_ability &
7048 	    I82579_EEE_1000_SUPPORTED) != 0)
7049 		lpi_ctrl |= I82579_LPI_CTRL_1000_ENABLE;
7050 	error = hw->phy.ops.write_reg_locked(hw, I82579_LPI_CTRL, lpi_ctrl);
7051 release:
7052 	hw->phy.ops.release(hw);
7053 out:
7054 	if (error != E1000_SUCCESS)
7055 		device_printf(sc->dev,
7056 		    "Could not configure Energy Efficient Ethernet for sleep: %d\n",
7057 		    error);
7058 }
7059 
7060 static void
em_power_up_wakeup_link(struct e1000_softc * sc)7061 em_power_up_wakeup_link(struct e1000_softc *sc)
7062 {
7063 	struct e1000_hw *hw = &sc->hw;
7064 
7065 	if (hw->mac.type < igb_mac_min)
7066 		e1000_power_up_phy(hw);
7067 	else if (hw->phy.media_type == e1000_media_type_copper)
7068 		e1000_power_up_phy(hw);
7069 	else {
7070 		e1000_power_up_fiber_serdes_link(hw);
7071 		(void)e1000_setup_link(hw);
7072 	}
7073 	sc->suspend_link_powered_down = false;
7074 }
7075 
7076 /* Drop the unused suspend link through the controller's shared-code hook. */
7077 static void
em_power_down_wakeup_link(struct e1000_softc * sc)7078 em_power_down_wakeup_link(struct e1000_softc *sc)
7079 {
7080 	struct e1000_hw *hw = &sc->hw;
7081 
7082 	if (hw->mac.type >= igb_mac_min &&
7083 	    hw->phy.media_type != e1000_media_type_copper)
7084 		e1000_shutdown_fiber_serdes_link(hw);
7085 	else
7086 		e1000_power_down_phy(hw);
7087 	sc->suspend_link_powered_down = true;
7088 }
7089 
7090 /* PCH PHY wake requires the MAC receive state on the BM wake page. */
7091 static int
em_enable_phy_wakeup(struct e1000_softc * sc,u32 wufc)7092 em_enable_phy_wakeup(struct e1000_softc *sc, u32 wufc)
7093 {
7094 	struct e1000_hw *hw = &sc->hw;
7095 	u32 mreg, wuc;
7096 	u16 preg, wuc_enable;
7097 	s32 error, restore_error;
7098 
7099 	/* Copy MAC RARs to PHY RARs before selecting the BM wake page. */
7100 	error = e1000_copy_rx_addrs_to_phy_ich8lan(hw);
7101 	if (error != E1000_SUCCESS)
7102 		goto out;
7103 
7104 	error = hw->phy.ops.acquire(hw);
7105 	if (error != E1000_SUCCESS) {
7106 		device_printf(sc->dev, "Could not acquire PHY for wakeup\n");
7107 		goto out;
7108 	}
7109 
7110 	error = e1000_enable_phy_wakeup_reg_access_bm(hw, &wuc_enable);
7111 	if (error != E1000_SUCCESS)
7112 		goto release;
7113 
7114 	/* Wake status is RW1C and survives controller reset. */
7115 	error = hw->phy.ops.write_reg_page(hw, BM_WUS, 0xffff);
7116 	if (error != E1000_SUCCESS)
7117 		goto restore;
7118 
7119 	/* copy MAC MTA to PHY MTA */
7120 	for (int i = 0; i < hw->mac.mta_reg_count; i++) {
7121 		mreg = E1000_READ_REG_ARRAY(hw, E1000_MTA, i);
7122 		error = hw->phy.ops.write_reg_page(hw, BM_MTA(i),
7123 		    (u16)(mreg & 0xffff));
7124 		if (error != E1000_SUCCESS)
7125 			goto restore;
7126 		error = hw->phy.ops.write_reg_page(hw, BM_MTA(i) + 1,
7127 		    (u16)(mreg >> 16));
7128 		if (error != E1000_SUCCESS)
7129 			goto restore;
7130 	}
7131 
7132 	/* configure PHY Rx Control register */
7133 	error = hw->phy.ops.read_reg_page(hw, BM_RCTL, &preg);
7134 	if (error != E1000_SUCCESS)
7135 		goto restore;
7136 	mreg = E1000_READ_REG(hw, E1000_RCTL);
7137 	if (mreg & E1000_RCTL_UPE)
7138 		preg |= BM_RCTL_UPE;
7139 	if (mreg & E1000_RCTL_MPE)
7140 		preg |= BM_RCTL_MPE;
7141 	preg &= ~(BM_RCTL_MO_MASK);
7142 	if (mreg & E1000_RCTL_MO_3)
7143 		preg |= (((mreg & E1000_RCTL_MO_3) >> E1000_RCTL_MO_SHIFT)
7144 				<< BM_RCTL_MO_SHIFT);
7145 	if (mreg & E1000_RCTL_BAM)
7146 		preg |= BM_RCTL_BAM;
7147 	if (mreg & E1000_RCTL_PMCF)
7148 		preg |= BM_RCTL_PMCF;
7149 	mreg = E1000_READ_REG(hw, E1000_CTRL);
7150 	if (mreg & E1000_CTRL_RFCE)
7151 		preg |= BM_RCTL_RFCE;
7152 	error = hw->phy.ops.write_reg_page(hw, BM_RCTL, preg);
7153 	if (error != E1000_SUCCESS)
7154 		goto restore;
7155 
7156 	wuc = E1000_WUC_PME_EN;
7157 	if ((wufc & (E1000_WUFC_MAG | E1000_WUFC_LNKC)) != 0)
7158 		wuc |= E1000_WUC_APME;
7159 
7160 	/* enable PHY wakeup in MAC register */
7161 	E1000_WRITE_REG(hw, E1000_WUFC, wufc);
7162 	E1000_WRITE_REG(hw, E1000_WUC, E1000_WUC_PHY_WAKE |
7163 	    E1000_WUC_APMPME | E1000_WUC_PME_STATUS | wuc);
7164 
7165 	/* configure and enable PHY wakeup in PHY registers */
7166 	error = hw->phy.ops.write_reg_page(hw, BM_WUFC, wufc);
7167 	if (error != E1000_SUCCESS)
7168 		goto restore;
7169 	error = hw->phy.ops.write_reg_page(hw, BM_WUC, wuc);
7170 	if (error != E1000_SUCCESS)
7171 		goto restore;
7172 
7173 restore:
7174 	/* Restore the page selector and expose only a complete setup. */
7175 	if (error == E1000_SUCCESS)
7176 		wuc_enable |= BM_WUC_ENABLE_BIT | BM_WUC_HOST_WU_BIT;
7177 	else
7178 		wuc_enable &= ~BM_WUC_HOST_WU_BIT;
7179 	restore_error = e1000_disable_phy_wakeup_reg_access_bm(hw,
7180 	    &wuc_enable);
7181 	if (error == E1000_SUCCESS)
7182 		error = restore_error;
7183 release:
7184 	hw->phy.ops.release(hw);
7185 out:
7186 	sc->wol_phy_armed = error == E1000_SUCCESS;
7187 	if (error != E1000_SUCCESS)
7188 		device_printf(sc->dev, "Could not configure PHY wakeup: %d\n",
7189 		    error);
7190 
7191 	return (error);
7192 }
7193 
7194 /* Clear host ownership and sticky status without disturbing ME wake. */
7195 static int
em_disable_phy_wakeup(struct e1000_softc * sc,u16 * wus)7196 em_disable_phy_wakeup(struct e1000_softc *sc, u16 *wus)
7197 {
7198 	struct e1000_hw *hw = &sc->hw;
7199 	s32 error, restore_error;
7200 	u16 phy_wus, wuc_enable;
7201 
7202 	error = hw->phy.ops.acquire(hw);
7203 	if (error != E1000_SUCCESS)
7204 		return (error);
7205 	error = e1000_enable_phy_wakeup_reg_access_bm(hw, &wuc_enable);
7206 	if (error != E1000_SUCCESS)
7207 		goto release;
7208 
7209 	error = hw->phy.ops.read_reg_page(hw, BM_WUS, &phy_wus);
7210 	if (error == E1000_SUCCESS)
7211 		error = hw->phy.ops.write_reg_page(hw, BM_WUS, 0xffff);
7212 	wuc_enable &= ~BM_WUC_HOST_WU_BIT;
7213 	restore_error = e1000_disable_phy_wakeup_reg_access_bm(hw,
7214 	    &wuc_enable);
7215 	if (error == E1000_SUCCESS)
7216 		error = restore_error;
7217 release:
7218 	hw->phy.ops.release(hw);
7219 	if (error == E1000_SUCCESS) {
7220 		sc->wol_phy_armed = false;
7221 		if (wus != NULL)
7222 			*wus = phy_wus;
7223 	}
7224 	return (error);
7225 }
7226 
7227 static void
em_if_led_func(if_ctx_t ctx,int onoff)7228 em_if_led_func(if_ctx_t ctx, int onoff)
7229 {
7230 	struct e1000_softc *sc = iflib_get_softc(ctx);
7231 
7232 	if (onoff) {
7233 		e1000_setup_led(&sc->hw);
7234 		if (sc->hw.phy.media_type == e1000_media_type_internal_serdes)
7235 			e1000_blink_led(&sc->hw);
7236 		else
7237 			e1000_led_on(&sc->hw);
7238 	} else {
7239 		e1000_led_off(&sc->hw);
7240 		e1000_cleanup_led(&sc->hw);
7241 	}
7242 }
7243 
7244 /*
7245  * Disable the L0S and L1 LINK states
7246  */
7247 static void
em_disable_aspm(struct e1000_softc * sc)7248 em_disable_aspm(struct e1000_softc *sc)
7249 {
7250 	int base, reg;
7251 	u16 link_cap,link_ctrl;
7252 	device_t dev = sc->dev;
7253 
7254 	switch (sc->hw.mac.type) {
7255 	case e1000_82573:
7256 	case e1000_82574:
7257 	case e1000_82583:
7258 		break;
7259 	default:
7260 		return;
7261 	}
7262 	if (pci_find_cap(dev, PCIY_EXPRESS, &base) != 0)
7263 		return;
7264 	reg = base + PCIER_LINK_CAP;
7265 	link_cap = pci_read_config(dev, reg, 2);
7266 	if ((link_cap & PCIEM_LINK_CAP_ASPM) == 0)
7267 		return;
7268 	reg = base + PCIER_LINK_CTL;
7269 	link_ctrl = pci_read_config(dev, reg, 2);
7270 	link_ctrl &= ~PCIEM_LINK_CTL_ASPMC;
7271 	pci_write_config(dev, reg, link_ctrl, 2);
7272 	return;
7273 }
7274 
7275 /**********************************************************************
7276  *
7277  *  Update the board statistics counters.
7278  *
7279  **********************************************************************/
7280 void
em_update_stats_counters(struct e1000_softc * sc)7281 em_update_stats_counters(struct e1000_softc *sc)
7282 {
7283 	struct e1000_hw_stats *stats;
7284 	u64 prev_xoffrxc;
7285 
7286 	if (sc->vf_ifp) {
7287 		em_update_vf_stats_counters(sc);
7288 		return;
7289 	}
7290 
7291 	stats = &sc->ustats.stats;
7292 	prev_xoffrxc = stats->xoffrxc;
7293 
7294 	if(sc->hw.phy.media_type == e1000_media_type_copper ||
7295 	   (E1000_READ_REG(&sc->hw, E1000_STATUS) & E1000_STATUS_LU)) {
7296 		stats->symerrs += E1000_READ_REG(&sc->hw, E1000_SYMERRS);
7297 		stats->sec += E1000_READ_REG(&sc->hw, E1000_SEC);
7298 	}
7299 	stats->crcerrs += E1000_READ_REG(&sc->hw, E1000_CRCERRS);
7300 	stats->mpc += E1000_READ_REG(&sc->hw, E1000_MPC);
7301 	stats->scc += E1000_READ_REG(&sc->hw, E1000_SCC);
7302 	stats->ecol += E1000_READ_REG(&sc->hw, E1000_ECOL);
7303 
7304 	stats->mcc += E1000_READ_REG(&sc->hw, E1000_MCC);
7305 	stats->latecol += E1000_READ_REG(&sc->hw, E1000_LATECOL);
7306 	stats->colc += E1000_READ_REG(&sc->hw, E1000_COLC);
7307 	stats->dc += E1000_READ_REG(&sc->hw, E1000_DC);
7308 	stats->rlec += E1000_READ_REG(&sc->hw, E1000_RLEC);
7309 	stats->xonrxc += E1000_READ_REG(&sc->hw, E1000_XONRXC);
7310 	stats->xontxc += E1000_READ_REG(&sc->hw, E1000_XONTXC);
7311 	stats->xoffrxc += E1000_READ_REG(&sc->hw, E1000_XOFFRXC);
7312 	/*
7313 	 ** For watchdog management we need to know if we have been
7314 	 ** paused during the last interval, so capture that here.
7315 	*/
7316 	if (stats->xoffrxc != prev_xoffrxc)
7317 		sc->shared->isc_pause_frames = 1;
7318 	stats->xofftxc += E1000_READ_REG(&sc->hw, E1000_XOFFTXC);
7319 	stats->fcruc += E1000_READ_REG(&sc->hw, E1000_FCRUC);
7320 	stats->prc64 += E1000_READ_REG(&sc->hw, E1000_PRC64);
7321 	stats->prc127 += E1000_READ_REG(&sc->hw, E1000_PRC127);
7322 	stats->prc255 += E1000_READ_REG(&sc->hw, E1000_PRC255);
7323 	stats->prc511 += E1000_READ_REG(&sc->hw, E1000_PRC511);
7324 	stats->prc1023 += E1000_READ_REG(&sc->hw, E1000_PRC1023);
7325 	stats->prc1522 += E1000_READ_REG(&sc->hw, E1000_PRC1522);
7326 	stats->gprc += E1000_READ_REG(&sc->hw, E1000_GPRC);
7327 	stats->bprc += E1000_READ_REG(&sc->hw, E1000_BPRC);
7328 	stats->mprc += E1000_READ_REG(&sc->hw, E1000_MPRC);
7329 	stats->gptc += E1000_READ_REG(&sc->hw, E1000_GPTC);
7330 
7331 	/* For the 64-bit byte counters the low dword must be read first. */
7332 	/* Both registers clear on the read of the high dword */
7333 
7334 	stats->gorc += E1000_READ_REG(&sc->hw, E1000_GORCL) +
7335 	    ((u64)E1000_READ_REG(&sc->hw, E1000_GORCH) << 32);
7336 	stats->gotc += E1000_READ_REG(&sc->hw, E1000_GOTCL) +
7337 	    ((u64)E1000_READ_REG(&sc->hw, E1000_GOTCH) << 32);
7338 
7339 	stats->rnbc += E1000_READ_REG(&sc->hw, E1000_RNBC);
7340 	stats->ruc += E1000_READ_REG(&sc->hw, E1000_RUC);
7341 	stats->rfc += E1000_READ_REG(&sc->hw, E1000_RFC);
7342 	stats->roc += E1000_READ_REG(&sc->hw, E1000_ROC);
7343 	stats->rjc += E1000_READ_REG(&sc->hw, E1000_RJC);
7344 
7345 	stats->mgprc += E1000_READ_REG(&sc->hw, E1000_MGTPRC);
7346 	stats->mgpdc += E1000_READ_REG(&sc->hw, E1000_MGTPDC);
7347 	stats->mgptc += E1000_READ_REG(&sc->hw, E1000_MGTPTC);
7348 
7349 	stats->tor += E1000_READ_REG(&sc->hw, E1000_TORH);
7350 	stats->tot += E1000_READ_REG(&sc->hw, E1000_TOTH);
7351 
7352 	stats->tpr += E1000_READ_REG(&sc->hw, E1000_TPR);
7353 	stats->tpt += E1000_READ_REG(&sc->hw, E1000_TPT);
7354 	stats->ptc64 += E1000_READ_REG(&sc->hw, E1000_PTC64);
7355 	stats->ptc127 += E1000_READ_REG(&sc->hw, E1000_PTC127);
7356 	stats->ptc255 += E1000_READ_REG(&sc->hw, E1000_PTC255);
7357 	stats->ptc511 += E1000_READ_REG(&sc->hw, E1000_PTC511);
7358 	stats->ptc1023 += E1000_READ_REG(&sc->hw, E1000_PTC1023);
7359 	stats->ptc1522 += E1000_READ_REG(&sc->hw, E1000_PTC1522);
7360 	stats->mptc += E1000_READ_REG(&sc->hw, E1000_MPTC);
7361 	stats->bptc += E1000_READ_REG(&sc->hw, E1000_BPTC);
7362 
7363 	/* TLPIC and RLPIC are clear-on-read. */
7364 	if (em_mac_has_eee(sc->hw.mac.type)) {
7365 		stats->tlpic += E1000_READ_REG(&sc->hw, E1000_TLPIC);
7366 		stats->rlpic += E1000_READ_REG(&sc->hw, E1000_RLPIC);
7367 	}
7368 
7369 	/* Interrupt Counts */
7370 
7371 	stats->iac += E1000_READ_REG(&sc->hw, E1000_IAC);
7372 	stats->icrxptc += E1000_READ_REG(&sc->hw, E1000_ICRXPTC);
7373 	stats->icrxatc += E1000_READ_REG(&sc->hw, E1000_ICRXATC);
7374 	stats->ictxptc += E1000_READ_REG(&sc->hw, E1000_ICTXPTC);
7375 	stats->ictxatc += E1000_READ_REG(&sc->hw, E1000_ICTXATC);
7376 	stats->ictxqec += E1000_READ_REG(&sc->hw, E1000_ICTXQEC);
7377 	stats->ictxqmtc += E1000_READ_REG(&sc->hw, E1000_ICTXQMTC);
7378 	stats->icrxdmtc += E1000_READ_REG(&sc->hw, E1000_ICRXDMTC);
7379 	stats->icrxoc += E1000_READ_REG(&sc->hw, E1000_ICRXOC);
7380 
7381 	if (sc->hw.mac.type >= e1000_82543) {
7382 		stats->algnerrc +=
7383 		E1000_READ_REG(&sc->hw, E1000_ALGNERRC);
7384 		stats->rxerrc +=
7385 		E1000_READ_REG(&sc->hw, E1000_RXERRC);
7386 		stats->tncrs +=
7387 		E1000_READ_REG(&sc->hw, E1000_TNCRS);
7388 		stats->cexterr +=
7389 		E1000_READ_REG(&sc->hw, E1000_CEXTERR);
7390 		stats->tsctc +=
7391 		E1000_READ_REG(&sc->hw, E1000_TSCTC);
7392 		stats->tsctfc +=
7393 		E1000_READ_REG(&sc->hw, E1000_TSCTFC);
7394 	}
7395 
7396 	if (em_has_82571_ecc_stats(&sc->hw))
7397 		em_update_82571_ecc_stats(sc);
7398 	else if (em_has_pch_ecc(&sc->hw))
7399 		em_update_pch_ecc_stats(sc,
7400 		    E1000_READ_REG(&sc->hw, E1000_PBECCSTS));
7401 	else if (em_has_82575_memory_errors(&sc->hw))
7402 		em_update_82575_ecc_stats(sc,
7403 		    E1000_READ_REG(&sc->hw, E1000_PBECCSTS_82575),
7404 		    E1000_READ_REG(&sc->hw, E1000_RDHESTS_82575),
7405 		    E1000_READ_REG(&sc->hw, E1000_TDHESTS_82575));
7406 	else if (em_has_82576_memory_errors(&sc->hw))
7407 		em_update_82576_ecc_stats(sc);
7408 	else if (em_has_82580_memory_errors(&sc->hw))
7409 		em_update_82580_ecc_stats(sc,
7410 		    E1000_READ_REG(&sc->hw, E1000_RPBECCSTS),
7411 		    E1000_READ_REG(&sc->hw, E1000_TPBECCSTS),
7412 		    E1000_READ_REG(&sc->hw, E1000_PCIEECCSTS));
7413 	else if (em_has_i350_i354_memory_errors(&sc->hw))
7414 		em_update_i350_i354_ecc_stats(sc);
7415 	else if (em_has_i210_memory_errors(&sc->hw))
7416 		em_update_i210_ecc_stats(sc);
7417 }
7418 
7419 static bool
em_mac_has_eee(enum e1000_mac_type type)7420 em_mac_has_eee(enum e1000_mac_type type)
7421 {
7422 
7423 	return ((type >= e1000_pch2lan && type < e1000_82575) ||
7424 	    (type >= e1000_i350 && type <= e1000_i211));
7425 }
7426 
7427 static void
em_initialize_vf_stats(struct e1000_softc * sc)7428 em_initialize_vf_stats(struct e1000_softc *sc)
7429 {
7430 	struct e1000_vf_stats *stats;
7431 
7432 	stats = &sc->ustats.vf_stats;
7433 	*stats = (struct e1000_vf_stats){};
7434 	em_rebase_vf_stats(sc);
7435 }
7436 
7437 static void
em_rebase_vf_stats(struct e1000_softc * sc)7438 em_rebase_vf_stats(struct e1000_softc *sc)
7439 {
7440 	struct e1000_vf_stats *stats;
7441 
7442 	/*
7443 	 * A PF reset starts a new VF counter epoch.  Preserve the accumulated
7444 	 * totals while establishing a new raw baseline so the reset is not
7445 	 * mistaken for a 32-bit wrap.
7446 	 */
7447 	stats = &sc->ustats.vf_stats;
7448 #define INIT_VF_REG(reg, name) do {					\
7449 	stats->last_##name = E1000_READ_REG(&sc->hw, reg);		\
7450 } while (0)
7451 	INIT_VF_REG(E1000_VFGPRC, gprc);
7452 	INIT_VF_REG(E1000_VFGORC, gorc);
7453 	INIT_VF_REG(E1000_VFGPTC, gptc);
7454 	INIT_VF_REG(E1000_VFGOTC, gotc);
7455 	/*
7456 	 * I350 specification update erratum 31 says VFMPRC is not
7457 	 * accessible from VF memory.  The 0xf3c register remains valid on
7458 	 * 82576 VFs, but must not be read on vfadapt_i350.
7459 	 */
7460 	if (sc->hw.mac.type == e1000_vfadapt)
7461 		INIT_VF_REG(E1000_VFMPRC, mprc);
7462 	else
7463 		stats->last_mprc = 0;
7464 	INIT_VF_REG(E1000_VFGOTLBC, gotlbc);
7465 	INIT_VF_REG(E1000_VFGPTLBC, gptlbc);
7466 	INIT_VF_REG(E1000_VFGORLBC, gorlbc);
7467 	INIT_VF_REG(E1000_VFGPRLBC, gprlbc);
7468 #undef INIT_VF_REG
7469 }
7470 
7471 static void
em_update_vf_stats_counters(struct e1000_softc * sc)7472 em_update_vf_stats_counters(struct e1000_softc *sc)
7473 {
7474 	struct e1000_vf_stats *stats;
7475 
7476 	stats = &sc->ustats.vf_stats;
7477 
7478 	/*
7479 	 * Internal VF loopback traffic can continue without physical link,
7480 	 * so sample the counters regardless of link state.
7481 	 */
7482 	UPDATE_VF_REG(E1000_VFGPRC,
7483 	    stats->last_gprc, stats->gprc);
7484 	UPDATE_VF_REG(E1000_VFGORC,
7485 	    stats->last_gorc, stats->gorc);
7486 	UPDATE_VF_REG(E1000_VFGPTC,
7487 	    stats->last_gptc, stats->gptc);
7488 	UPDATE_VF_REG(E1000_VFGOTC,
7489 	    stats->last_gotc, stats->gotc);
7490 	if (sc->hw.mac.type == e1000_vfadapt)
7491 		UPDATE_VF_REG(E1000_VFMPRC,
7492 		    stats->last_mprc, stats->mprc);
7493 	UPDATE_VF_REG(E1000_VFGOTLBC,
7494 	    stats->last_gotlbc, stats->gotlbc);
7495 	UPDATE_VF_REG(E1000_VFGPTLBC,
7496 	    stats->last_gptlbc, stats->gptlbc);
7497 	UPDATE_VF_REG(E1000_VFGORLBC,
7498 	    stats->last_gorlbc, stats->gorlbc);
7499 	UPDATE_VF_REG(E1000_VFGPRLBC,
7500 	    stats->last_gprlbc, stats->gprlbc);
7501 }
7502 
7503 static uint64_t
em_if_get_vf_counter(if_ctx_t ctx,ift_counter cnt)7504 em_if_get_vf_counter(if_ctx_t ctx, ift_counter cnt)
7505 {
7506 	struct e1000_softc *sc = iflib_get_softc(ctx);
7507 	if_t ifp = iflib_get_ifp(ctx);
7508 
7509 	switch (cnt) {
7510 	case IFCOUNTER_IERRORS:
7511 		return sc->dropped_pkts;
7512 	default:
7513 		return (if_get_counter_default(ifp, cnt));
7514 	}
7515 }
7516 
7517 static uint64_t
em_if_get_counter(if_ctx_t ctx,ift_counter cnt)7518 em_if_get_counter(if_ctx_t ctx, ift_counter cnt)
7519 {
7520 	struct e1000_softc *sc = iflib_get_softc(ctx);
7521 	struct e1000_hw_stats *stats;
7522 	if_t ifp = iflib_get_ifp(ctx);
7523 
7524 	if (sc->vf_ifp)
7525 		return (em_if_get_vf_counter(ctx, cnt));
7526 
7527 	stats = &sc->ustats.stats;
7528 
7529 	switch (cnt) {
7530 	case IFCOUNTER_COLLISIONS:
7531 		return (stats->colc);
7532 	case IFCOUNTER_IERRORS:
7533 		return (sc->dropped_pkts + stats->rxerrc +
7534 		    stats->crcerrs + stats->algnerrc +
7535 		    stats->ruc + stats->roc +
7536 		    stats->mpc + stats->cexterr);
7537 	case IFCOUNTER_OERRORS:
7538 		return (if_get_counter_default(ifp, cnt) +
7539 		    stats->ecol + stats->latecol);
7540 	default:
7541 		return (if_get_counter_default(ifp, cnt));
7542 	}
7543 }
7544 
7545 /* em_if_needs_restart - Tell iflib when the driver needs to be reinitialized
7546  * @ctx: iflib context
7547  * @event: event code to check
7548  *
7549  * Defaults to returning false for unknown events.
7550  *
7551  * @returns true if iflib needs to reinit the interface
7552  */
7553 static bool
em_if_needs_restart(if_ctx_t ctx __unused,enum iflib_restart_event event)7554 em_if_needs_restart(if_ctx_t ctx __unused, enum iflib_restart_event event)
7555 {
7556 	switch (event) {
7557 	case IFLIB_RESTART_VLAN_CONFIG:
7558 	default:
7559 		return (false);
7560 	}
7561 }
7562 
7563 /* Export a single 32-bit register via a read-only sysctl. */
7564 static int
em_sysctl_reg_handler(SYSCTL_HANDLER_ARGS)7565 em_sysctl_reg_handler(SYSCTL_HANDLER_ARGS)
7566 {
7567 	struct e1000_softc *sc;
7568 	u_int val;
7569 
7570 	sc = oidp->oid_arg1;
7571 	val = E1000_READ_REG(&sc->hw, oidp->oid_arg2);
7572 	return (sysctl_handle_int(oidp, &val, 0, req));
7573 }
7574 
7575 enum em_ring_register {
7576 	EM_RING_HEAD,
7577 	EM_RING_TAIL,
7578 };
7579 
7580 /* Queue register addresses can change when the PF enters IOV mode. */
7581 static int
em_sysctl_tx_ring_handler(SYSCTL_HANDLER_ARGS)7582 em_sysctl_tx_ring_handler(SYSCTL_HANDLER_ARGS)
7583 {
7584 	struct tx_ring *txr;
7585 	u_int reg, val;
7586 
7587 	txr = oidp->oid_arg1;
7588 	reg = oidp->oid_arg2 == EM_RING_HEAD ? E1000_TDH(txr->me) :
7589 	    E1000_TDT(txr->me);
7590 	val = E1000_READ_REG(&txr->sc->hw, reg);
7591 	return (sysctl_handle_int(oidp, &val, 0, req));
7592 }
7593 
7594 static int
em_sysctl_rx_ring_handler(SYSCTL_HANDLER_ARGS)7595 em_sysctl_rx_ring_handler(SYSCTL_HANDLER_ARGS)
7596 {
7597 	struct rx_ring *rxr;
7598 	u_int reg, val;
7599 
7600 	rxr = oidp->oid_arg1;
7601 	reg = oidp->oid_arg2 == EM_RING_HEAD ? E1000_RDH(rxr->me) :
7602 	    E1000_RDT(rxr->me);
7603 	val = E1000_READ_REG(&rxr->sc->hw, reg);
7604 	return (sysctl_handle_int(oidp, &val, 0, req));
7605 }
7606 
7607 /* Per queue holdoff interrupt rate handler */
7608 static int
em_sysctl_interrupt_rate_handler(SYSCTL_HANDLER_ARGS)7609 em_sysctl_interrupt_rate_handler(SYSCTL_HANDLER_ARGS)
7610 {
7611 	struct em_rx_queue *rque;
7612 	struct em_tx_queue *tque;
7613 	struct e1000_hw *hw;
7614 	int error;
7615 	u32 reg, usec, rate;
7616 
7617 	bool tx = oidp->oid_arg2;
7618 
7619 	if (tx) {
7620 		tque = oidp->oid_arg1;
7621 		hw = &tque->sc->hw;
7622 		if (hw->mac.type >= igb_mac_min)
7623 			reg = E1000_READ_REG(hw, E1000_EITR(tque->msix));
7624 		else if (hw->mac.type == e1000_82574 &&
7625 		    tque->sc->intr_type == IFLIB_INTR_MSIX)
7626 			reg = E1000_READ_REG(hw, E1000_EITR_82574(tque->msix));
7627 		else
7628 			reg = E1000_READ_REG(hw, E1000_ITR);
7629 	} else {
7630 		rque = oidp->oid_arg1;
7631 		hw = &rque->sc->hw;
7632 		if (hw->mac.type >= igb_mac_min)
7633 			reg = E1000_READ_REG(hw, E1000_EITR(rque->msix));
7634 		else if (hw->mac.type == e1000_82574 &&
7635 		    rque->sc->intr_type == IFLIB_INTR_MSIX)
7636 			reg = E1000_READ_REG(hw,
7637 			    E1000_EITR_82574(rque->msix));
7638 		else
7639 			reg = E1000_READ_REG(hw, E1000_ITR);
7640 	}
7641 
7642 	if (hw->mac.type < igb_mac_min) {
7643 		if (reg > 0)
7644 			rate = EM_INTS_TO_ITR(reg);
7645 		else
7646 			rate = 0;
7647 	} else {
7648 		usec = (reg & IGB_QVECTOR_MASK);
7649 		if (usec > 0)
7650 			rate = IGB_EITR_TO_INTS(usec);
7651 		else
7652 			rate = 0;
7653 	}
7654 
7655 	error = sysctl_handle_int(oidp, &rate, 0, req);
7656 	if (error || !req->newptr)
7657 		return error;
7658 	return 0;
7659 }
7660 
7661 /*
7662  * Add sysctl variables, one per statistic, to the system.
7663  */
7664 static void
em_add_hw_stats(struct e1000_softc * sc)7665 em_add_hw_stats(struct e1000_softc *sc)
7666 {
7667 	device_t dev = iflib_get_dev(sc->ctx);
7668 	struct em_tx_queue *tx_que = sc->tx_queues;
7669 	struct em_rx_queue *rx_que = sc->rx_queues;
7670 
7671 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
7672 	struct sysctl_oid *tree = device_get_sysctl_tree(dev);
7673 	struct sysctl_oid_list *child = SYSCTL_CHILDREN(tree);
7674 	struct e1000_hw_stats *stats;
7675 
7676 	struct sysctl_oid *stat_node, *queue_node, *int_node;
7677 	struct sysctl_oid_list *stat_list, *queue_list, *int_list;
7678 
7679 #define QUEUE_NAME_LEN 32
7680 	char namebuf[QUEUE_NAME_LEN];
7681 
7682 	/* Driver Statistics */
7683 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "dropped",
7684 	    CTLFLAG_RD, &sc->dropped_pkts,
7685 	    "Driver dropped packets");
7686 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "link_irq",
7687 	    CTLFLAG_RD, &sc->link_irq,
7688 	    "Link MSI-X IRQ Handled");
7689 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_overruns",
7690 	    CTLFLAG_RD, &sc->rx_overruns,
7691 	    "RX overruns");
7692 	if (!sc->vf_ifp) {
7693 		SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "device_control",
7694 		    CTLTYPE_UINT | CTLFLAG_RD,
7695 		    sc, E1000_CTRL, em_sysctl_reg_handler, "IU",
7696 		    "Device Control Register");
7697 		SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "rx_control",
7698 		    CTLTYPE_UINT | CTLFLAG_RD,
7699 		    sc, E1000_RCTL, em_sysctl_reg_handler, "IU",
7700 		    "Receiver Control Register");
7701 		SYSCTL_ADD_UINT(ctx, child, OID_AUTO, "fc_high_water",
7702 		    CTLFLAG_RD, &sc->hw.fc.high_water, 0,
7703 		    "Flow Control High Watermark");
7704 		SYSCTL_ADD_UINT(ctx, child, OID_AUTO, "fc_low_water",
7705 		    CTLFLAG_RD, &sc->hw.fc.low_water, 0,
7706 		    "Flow Control Low Watermark");
7707 	}
7708 
7709 	for (int i = 0; i < sc->tx_num_queues; i++, tx_que++) {
7710 		struct tx_ring *txr = &tx_que->txr;
7711 		snprintf(namebuf, QUEUE_NAME_LEN, "queue_tx_%d", i);
7712 		queue_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
7713 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TX Queue Name");
7714 		queue_list = SYSCTL_CHILDREN(queue_node);
7715 
7716 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "interrupt_rate",
7717 		    CTLTYPE_UINT | CTLFLAG_RD, tx_que,
7718 		    true, em_sysctl_interrupt_rate_handler,
7719 		    "IU", "Interrupt Rate");
7720 
7721 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "txd_head",
7722 		    CTLTYPE_UINT | CTLFLAG_RD, txr, EM_RING_HEAD,
7723 		    em_sysctl_tx_ring_handler, "IU",
7724 		    "Transmit Descriptor Head");
7725 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "txd_tail",
7726 		    CTLTYPE_UINT | CTLFLAG_RD, txr, EM_RING_TAIL,
7727 		    em_sysctl_tx_ring_handler, "IU",
7728 		    "Transmit Descriptor Tail");
7729 		SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO, "tx_irq",
7730 		    CTLFLAG_RD, &txr->tx_irq,
7731 		    "Queue MSI-X Transmit Interrupts");
7732 	}
7733 
7734 	for (int j = 0; j < sc->rx_num_queues; j++, rx_que++) {
7735 		struct rx_ring *rxr = &rx_que->rxr;
7736 		snprintf(namebuf, QUEUE_NAME_LEN, "queue_rx_%d", j);
7737 		queue_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
7738 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "RX Queue Name");
7739 		queue_list = SYSCTL_CHILDREN(queue_node);
7740 
7741 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "interrupt_rate",
7742 		    CTLTYPE_UINT | CTLFLAG_RD, rx_que,
7743 		    false, em_sysctl_interrupt_rate_handler,
7744 		    "IU", "Interrupt Rate");
7745 
7746 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "rxd_head",
7747 		    CTLTYPE_UINT | CTLFLAG_RD, rxr, EM_RING_HEAD,
7748 		    em_sysctl_rx_ring_handler, "IU",
7749 		    "Receive Descriptor Head");
7750 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "rxd_tail",
7751 		    CTLTYPE_UINT | CTLFLAG_RD, rxr, EM_RING_TAIL,
7752 		    em_sysctl_rx_ring_handler, "IU",
7753 		    "Receive Descriptor Tail");
7754 		SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO, "rx_irq",
7755 		    CTLFLAG_RD, &rxr->rx_irq,
7756 		    "Queue MSI-X Receive Interrupts");
7757 	}
7758 
7759 	/* MAC stats get their own sub node */
7760 	stat_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "mac_stats",
7761 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Statistics");
7762 	stat_list = SYSCTL_CHILDREN(stat_node);
7763 
7764 	/*
7765 	** VF adapter has a very limited set of stats
7766 	** since its not managing the metal, so to speak.
7767 	*/
7768 	if (sc->vf_ifp) {
7769 		struct e1000_vf_stats *vfstats = &sc->ustats.vf_stats;
7770 
7771 		SYSCTL_ADD_QUAD(ctx, stat_list, OID_AUTO, "good_pkts_recvd",
7772 		    CTLFLAG_RD, &vfstats->gprc,
7773 		    "Good Packets Received");
7774 		SYSCTL_ADD_QUAD(ctx, stat_list, OID_AUTO, "good_pkts_txd",
7775 		    CTLFLAG_RD, &vfstats->gptc,
7776 		    "Good Packets Transmitted");
7777 		SYSCTL_ADD_QUAD(ctx, stat_list, OID_AUTO, "good_octets_recvd",
7778 		    CTLFLAG_RD, &vfstats->gorc,
7779 		    "Good Octets Received");
7780 		SYSCTL_ADD_QUAD(ctx, stat_list, OID_AUTO, "good_octets_txd",
7781 		    CTLFLAG_RD, &vfstats->gotc,
7782 		    "Good Octets Transmitted");
7783 		if (sc->hw.mac.type == e1000_vfadapt) {
7784 			SYSCTL_ADD_QUAD(ctx, stat_list, OID_AUTO,
7785 			    "mcast_pkts_recvd", CTLFLAG_RD, &vfstats->mprc,
7786 			    "Multicast Packets Received");
7787 		}
7788 		SYSCTL_ADD_QUAD(ctx, stat_list, OID_AUTO,
7789 		    "loopback_good_pkts_recvd",
7790 		    CTLFLAG_RD, &vfstats->gprlbc,
7791 		    "Good Loopback Packets Received");
7792 		SYSCTL_ADD_QUAD(ctx, stat_list, OID_AUTO,
7793 		    "loopback_good_pkts_txd",
7794 		    CTLFLAG_RD, &vfstats->gptlbc,
7795 		    "Good Loopback Packets Transmitted");
7796 		SYSCTL_ADD_QUAD(ctx, stat_list, OID_AUTO,
7797 		    "loopback_good_octets_recvd",
7798 		    CTLFLAG_RD, &vfstats->gorlbc,
7799 		    "Good Loopback Octets Received");
7800 		SYSCTL_ADD_QUAD(ctx, stat_list, OID_AUTO,
7801 		    "loopback_good_octets_txd",
7802 		    CTLFLAG_RD, &vfstats->gotlbc,
7803 		    "Good Loopback Octets Transmitted");
7804 		SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO,
7805 		    "rx_csum_offload_good",
7806 		    CTLFLAG_RD, &sc->rx_csum_good,
7807 		    "Receive Checksum Offload Successes");
7808 		SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO,
7809 		    "rx_csum_offload_errors",
7810 		    CTLFLAG_RD, &sc->rx_csum_errors,
7811 		    "Receive Checksum Offload Errors");
7812 		return;
7813 	}
7814 
7815 	stats = &sc->ustats.stats;
7816 	if (em_mac_has_eee(sc->hw.mac.type)) {
7817 		struct sysctl_oid *eee_node;
7818 		struct sysctl_oid_list *eee_list;
7819 
7820 		eee_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "eee",
7821 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
7822 		    "Energy Efficient Ethernet statistics");
7823 		eee_list = SYSCTL_CHILDREN(eee_node);
7824 		SYSCTL_ADD_UQUAD(ctx, eee_list, OID_AUTO, "tx_lpi_count",
7825 		    CTLFLAG_RD, &stats->tlpic, "TX LPI event count");
7826 		SYSCTL_ADD_UQUAD(ctx, eee_list, OID_AUTO, "rx_lpi_count",
7827 		    CTLFLAG_RD, &stats->rlpic, "RX LPI event count");
7828 	}
7829 	if (em_has_memory_error_stats(&sc->hw)) {
7830 		struct sysctl_oid *memerr_node;
7831 		struct sysctl_oid_list *memerr_list;
7832 
7833 		memerr_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO,
7834 		    "memory_errors", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
7835 		    "Internal memory error indications");
7836 		memerr_list = SYSCTL_CHILDREN(memerr_node);
7837 		if (em_has_memory_errors(&sc->hw))
7838 			SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7839 			    "fatal_resets", CTLFLAG_RD,
7840 			    &sc->fatal_error_reset_count,
7841 			    "Resets requested for fatal internal memory errors");
7842 		if (em_has_82571_ecc_stats(&sc->hw)) {
7843 			SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7844 			    "detected_packet_buffer", CTLFLAG_RD,
7845 			    &sc->corrected_error_packet_buffer_count,
7846 			    "Detected packet-buffer ECC errors");
7847 		} else if (em_has_pch_ecc(&sc->hw)) {
7848 			SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7849 			    "corrected_packet_buffer", CTLFLAG_RD,
7850 			    &sc->corrected_error_packet_buffer_count,
7851 			    "Corrected packet-buffer ECC errors");
7852 			SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7853 			    "uncorrected_packet_buffer", CTLFLAG_RD,
7854 			    &sc->uncorrected_error_packet_buffer_count,
7855 			    "Uncorrected packet-buffer ECC errors");
7856 		} else if (em_has_82575_memory_errors(&sc->hw)) {
7857 			SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7858 			    "corrected_packet_buffer", CTLFLAG_RD,
7859 			    &sc->corrected_error_packet_buffer_count,
7860 			    "Corrected packet-buffer ECC errors");
7861 			SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7862 			    "uncorrected_packet_buffer", CTLFLAG_RD,
7863 			    &sc->uncorrected_error_packet_buffer_count,
7864 			    "Uncorrected packet-buffer ECC errors");
7865 			SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7866 			    "corrected_descriptor_handler", CTLFLAG_RD,
7867 			    &sc->corrected_error_dma_count,
7868 			    "Corrected descriptor-handler ECC errors");
7869 			SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7870 			    "uncorrected_descriptor_handler", CTLFLAG_RD,
7871 			    &sc->uncorrected_error_dma_count,
7872 			    "Uncorrected descriptor-handler ECC errors");
7873 		} else if (em_has_82576_memory_errors(&sc->hw)) {
7874 			SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7875 			    "fatal_unknown", CTLFLAG_RD,
7876 			    &sc->fatal_error_unknown_count,
7877 			    "Fatal memory errors without a reported source");
7878 			SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7879 			    "corrected_packet_buffer", CTLFLAG_RD,
7880 			    &sc->corrected_error_packet_buffer_count,
7881 			    "Corrected packet and switch-buffer ECC errors");
7882 			SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7883 			    "uncorrected_packet_buffer", CTLFLAG_RD,
7884 			    &sc->uncorrected_error_packet_buffer_count,
7885 			    "Uncorrected packet and switch-buffer ECC errors");
7886 			SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7887 			    "corrected_descriptor_handler", CTLFLAG_RD,
7888 			    &sc->corrected_error_dma_count,
7889 			    "Corrected descriptor-handler ECC errors");
7890 			SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7891 			    "uncorrected_descriptor_handler", CTLFLAG_RD,
7892 			    &sc->uncorrected_error_dma_count,
7893 			    "Uncorrected descriptor-handler ECC errors");
7894 			SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7895 			    "corrected_pcie_write_buffer", CTLFLAG_RD,
7896 			    &sc->corrected_error_pcie_tx_data_count,
7897 			    "Corrected PCIe write-buffer ECC errors");
7898 			SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7899 			    "corrected_pcie_retry_buffer", CTLFLAG_RD,
7900 			    &sc->corrected_error_pcie_retry_count,
7901 			    "Corrected controller-shared PCIe retry-buffer errors");
7902 			SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7903 			    "corrected_pcie_msix", CTLFLAG_RD,
7904 			    &sc->corrected_error_pcie_other_count,
7905 			    "Corrected controller-shared PCIe MSI-X errors");
7906 		} else {
7907 			SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7908 			    "fatal_lan", CTLFLAG_RD,
7909 			    &sc->fatal_error_lan_count,
7910 			    "Fatal LAN-port memory error indications");
7911 			SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7912 			    "fatal_management", CTLFLAG_RD,
7913 			    &sc->fatal_error_mng_count,
7914 			    "Fatal management-memory error indications");
7915 			SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7916 			    "fatal_pcie", CTLFLAG_RD,
7917 			    &sc->fatal_error_pcie_count,
7918 			    "Fatal PCIe memory error indications");
7919 			SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7920 			    "fatal_dma", CTLFLAG_RD,
7921 			    &sc->fatal_error_dma_count,
7922 			    "Fatal DMA memory error indications");
7923 			SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7924 			    "fatal_unknown", CTLFLAG_RD,
7925 			    &sc->fatal_error_unknown_count,
7926 			    "Fatal memory errors without a reported region");
7927 			if (em_has_82580_memory_errors(&sc->hw)) {
7928 				SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7929 				    "corrected_packet_buffer", CTLFLAG_RD,
7930 				    &sc->corrected_error_packet_buffer_count,
7931 				    "Corrected packet-buffer ECC errors");
7932 				SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7933 				    "uncorrected_pcie", CTLFLAG_RD,
7934 				    &sc->uncorrected_error_pcie_count,
7935 				    "Uncorrected PCIe command-memory ECC indications");
7936 			} else if (em_has_i210_memory_errors(&sc->hw)) {
7937 				SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7938 				    "corrected_dma", CTLFLAG_RD,
7939 				    &sc->corrected_error_dma_count,
7940 				    "Corrected DMA memory error indications");
7941 				SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7942 				    "corrected_pcie_tx_data", CTLFLAG_RD,
7943 				    &sc->corrected_error_pcie_tx_data_count,
7944 				    "Corrected PCIe transmit-data memory indications");
7945 				SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7946 				    "corrected_pcie_retry", CTLFLAG_RD,
7947 				    &sc->corrected_error_pcie_retry_count,
7948 				    "Corrected PCIe retry-buffer memory indications");
7949 			} else {
7950 				SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7951 				    "corrected_dma", CTLFLAG_RD,
7952 				    &sc->corrected_error_dma_count,
7953 				    "Corrected DMA memory indications");
7954 				SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7955 				    "corrected_packet_buffer", CTLFLAG_RD,
7956 				    &sc->corrected_error_packet_buffer_count,
7957 				    "Corrected packet-buffer memory indications");
7958 				SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7959 				    "corrected_lan_mng_fifo", CTLFLAG_RD,
7960 				    &sc->corrected_error_lan_mng_fifo_count,
7961 				    "Corrected LAN management transmit-FIFO ECC "
7962 				    "indications");
7963 				SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7964 				    "corrected_pcie_tx_data", CTLFLAG_RD,
7965 				    &sc->corrected_error_pcie_tx_data_count,
7966 				    "Corrected PCIe transmit-data memory indications");
7967 				if (sc->hw.mac.type == e1000_i350)
7968 					SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7969 					    "corrected_pcie_retry", CTLFLAG_RD,
7970 					    &sc->corrected_error_pcie_retry_count,
7971 					    "Corrected PCIe retry-buffer memory "
7972 					    "indications");
7973 				SYSCTL_ADD_UQUAD(ctx, memerr_list, OID_AUTO,
7974 				    "corrected_pcie_other", CTLFLAG_RD,
7975 				    &sc->corrected_error_pcie_other_count,
7976 				    "Other corrected PCIe memory indications");
7977 			}
7978 		}
7979 	}
7980 
7981 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "excess_coll",
7982 	    CTLFLAG_RD, &stats->ecol,
7983 	    "Excessive collisions");
7984 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "single_coll",
7985 	    CTLFLAG_RD, &stats->scc,
7986 	    "Single collisions");
7987 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "multiple_coll",
7988 	    CTLFLAG_RD, &stats->mcc,
7989 	    "Multiple collisions");
7990 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "late_coll",
7991 	    CTLFLAG_RD, &stats->latecol,
7992 	    "Late collisions");
7993 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "collision_count",
7994 	    CTLFLAG_RD, &stats->colc,
7995 	    "Collision Count");
7996 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "symbol_errors",
7997 	    CTLFLAG_RD, &stats->symerrs,
7998 	    "Symbol Errors");
7999 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "sequence_errors",
8000 	    CTLFLAG_RD, &stats->sec,
8001 	    "Sequence Errors");
8002 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "defer_count",
8003 	    CTLFLAG_RD, &stats->dc,
8004 	    "Defer Count");
8005 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "missed_packets",
8006 	    CTLFLAG_RD, &stats->mpc,
8007 	    "Missed Packets");
8008 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_length_errors",
8009 	    CTLFLAG_RD, &stats->rlec,
8010 	    "Receive Length Errors");
8011 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_no_buff",
8012 	    CTLFLAG_RD, &stats->rnbc,
8013 	    "Receive No Buffers");
8014 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_undersize",
8015 	    CTLFLAG_RD, &stats->ruc,
8016 	    "Receive Undersize");
8017 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_fragmented",
8018 	    CTLFLAG_RD, &stats->rfc,
8019 	    "Fragmented Packets Received ");
8020 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_oversize",
8021 	    CTLFLAG_RD, &stats->roc,
8022 	    "Oversized Packets Received");
8023 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_jabber",
8024 	    CTLFLAG_RD, &stats->rjc,
8025 	    "Recevied Jabber");
8026 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_errs",
8027 	    CTLFLAG_RD, &stats->rxerrc,
8028 	    "Receive Errors");
8029 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "crc_errs",
8030 	    CTLFLAG_RD, &stats->crcerrs,
8031 	    "CRC errors");
8032 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "alignment_errs",
8033 	    CTLFLAG_RD, &stats->algnerrc,
8034 	    "Alignment Errors");
8035 	/* On 82575 these are collision counts */
8036 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "coll_ext_errs",
8037 	    CTLFLAG_RD, &stats->cexterr,
8038 	    "Collision/Carrier extension errors");
8039 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xon_recvd",
8040 	    CTLFLAG_RD, &stats->xonrxc,
8041 	    "XON Received");
8042 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xon_txd",
8043 	    CTLFLAG_RD, &stats->xontxc,
8044 	    "XON Transmitted");
8045 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xoff_recvd",
8046 	    CTLFLAG_RD, &stats->xoffrxc,
8047 	    "XOFF Received");
8048 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xoff_txd",
8049 	    CTLFLAG_RD, &stats->xofftxc,
8050 	    "XOFF Transmitted");
8051 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "unsupported_fc_recvd",
8052 	    CTLFLAG_RD, &stats->fcruc,
8053 	    "Unsupported Flow Control Received");
8054 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "mgmt_pkts_recvd",
8055 	    CTLFLAG_RD, &stats->mgprc,
8056 	    "Management Packets Received");
8057 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "mgmt_pkts_drop",
8058 	    CTLFLAG_RD, &stats->mgpdc,
8059 	    "Management Packets Dropped");
8060 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "mgmt_pkts_txd",
8061 	    CTLFLAG_RD, &stats->mgptc,
8062 	    "Management Packets Transmitted");
8063 
8064 	/* Packet Reception Stats */
8065 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "total_pkts_recvd",
8066 	    CTLFLAG_RD, &stats->tpr,
8067 	    "Total Packets Received ");
8068 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_pkts_recvd",
8069 	    CTLFLAG_RD, &stats->gprc,
8070 	    "Good Packets Received");
8071 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "bcast_pkts_recvd",
8072 	    CTLFLAG_RD, &stats->bprc,
8073 	    "Broadcast Packets Received");
8074 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "mcast_pkts_recvd",
8075 	    CTLFLAG_RD, &stats->mprc,
8076 	    "Multicast Packets Received");
8077 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_64",
8078 	    CTLFLAG_RD, &stats->prc64,
8079 	    "64 byte frames received ");
8080 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_65_127",
8081 	    CTLFLAG_RD, &stats->prc127,
8082 	    "65-127 byte frames received");
8083 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_128_255",
8084 	    CTLFLAG_RD, &stats->prc255,
8085 	    "128-255 byte frames received");
8086 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_256_511",
8087 	    CTLFLAG_RD, &stats->prc511,
8088 	    "256-511 byte frames received");
8089 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_512_1023",
8090 	    CTLFLAG_RD, &stats->prc1023,
8091 	    "512-1023 byte frames received");
8092 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_1024_1522",
8093 	    CTLFLAG_RD, &stats->prc1522,
8094 	    "1023-1522 byte frames received");
8095 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_octets_recvd",
8096 	    CTLFLAG_RD, &stats->gorc,
8097 	    "Good Octets Received");
8098 
8099 	/* Packet Transmission Stats */
8100 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_octets_txd",
8101 	    CTLFLAG_RD, &stats->gotc,
8102 	    "Good Octets Transmitted");
8103 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "total_pkts_txd",
8104 	    CTLFLAG_RD, &stats->tpt,
8105 	    "Total Packets Transmitted");
8106 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_pkts_txd",
8107 	    CTLFLAG_RD, &stats->gptc,
8108 	    "Good Packets Transmitted");
8109 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "bcast_pkts_txd",
8110 	    CTLFLAG_RD, &stats->bptc,
8111 	    "Broadcast Packets Transmitted");
8112 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "mcast_pkts_txd",
8113 	    CTLFLAG_RD, &stats->mptc,
8114 	    "Multicast Packets Transmitted");
8115 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_64",
8116 	    CTLFLAG_RD, &stats->ptc64,
8117 	    "64 byte frames transmitted ");
8118 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_65_127",
8119 	    CTLFLAG_RD, &stats->ptc127,
8120 	    "65-127 byte frames transmitted");
8121 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_128_255",
8122 	    CTLFLAG_RD, &stats->ptc255,
8123 	    "128-255 byte frames transmitted");
8124 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_256_511",
8125 	    CTLFLAG_RD, &stats->ptc511,
8126 	    "256-511 byte frames transmitted");
8127 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_512_1023",
8128 	    CTLFLAG_RD, &stats->ptc1023,
8129 	    "512-1023 byte frames transmitted");
8130 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_1024_1522",
8131 	    CTLFLAG_RD, &stats->ptc1522,
8132 	    "1024-1522 byte frames transmitted");
8133 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tso_txd",
8134 	    CTLFLAG_RD, &stats->tsctc,
8135 	    "TSO Contexts Transmitted");
8136 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tso_ctx_fail",
8137 	    CTLFLAG_RD, &stats->tsctfc,
8138 	    "TSO Contexts Failed");
8139 
8140 	/* Interrupt Stats */
8141 	int_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "interrupts",
8142 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Interrupt Statistics");
8143 	int_list = SYSCTL_CHILDREN(int_node);
8144 
8145 	SYSCTL_ADD_UQUAD(ctx, int_list, OID_AUTO, "asserts",
8146 	    CTLFLAG_RD, &stats->iac,
8147 	    "Interrupt Assertion Count");
8148 
8149 	SYSCTL_ADD_UQUAD(ctx, int_list, OID_AUTO, "rx_pkt_timer",
8150 	    CTLFLAG_RD, &stats->icrxptc,
8151 	    "Interrupt Cause Rx Pkt Timer Expire Count");
8152 
8153 	SYSCTL_ADD_UQUAD(ctx, int_list, OID_AUTO, "rx_abs_timer",
8154 	    CTLFLAG_RD, &stats->icrxatc,
8155 	    "Interrupt Cause Rx Abs Timer Expire Count");
8156 
8157 	SYSCTL_ADD_UQUAD(ctx, int_list, OID_AUTO, "tx_pkt_timer",
8158 	    CTLFLAG_RD, &stats->ictxptc,
8159 	    "Interrupt Cause Tx Pkt Timer Expire Count");
8160 
8161 	SYSCTL_ADD_UQUAD(ctx, int_list, OID_AUTO, "tx_abs_timer",
8162 	    CTLFLAG_RD, &stats->ictxatc,
8163 	    "Interrupt Cause Tx Abs Timer Expire Count");
8164 
8165 	SYSCTL_ADD_UQUAD(ctx, int_list, OID_AUTO, "tx_queue_empty",
8166 	    CTLFLAG_RD, &stats->ictxqec,
8167 	    "Interrupt Cause Tx Queue Empty Count");
8168 
8169 	SYSCTL_ADD_UQUAD(ctx, int_list, OID_AUTO, "tx_queue_min_thresh",
8170 	    CTLFLAG_RD, &stats->ictxqmtc,
8171 	    "Interrupt Cause Tx Queue Min Thresh Count");
8172 
8173 	SYSCTL_ADD_UQUAD(ctx, int_list, OID_AUTO, "rx_desc_min_thresh",
8174 	    CTLFLAG_RD, &stats->icrxdmtc,
8175 	    "Interrupt Cause Rx Desc Min Thresh Count");
8176 
8177 	SYSCTL_ADD_UQUAD(ctx, int_list, OID_AUTO, "rx_overrun",
8178 	    CTLFLAG_RD, &stats->icrxoc,
8179 	    "Interrupt Cause Receiver Overrun Count");
8180 }
8181 
8182 static void
em_fw_version_locked(if_ctx_t ctx)8183 em_fw_version_locked(if_ctx_t ctx)
8184 {
8185 	struct e1000_softc *sc = iflib_get_softc(ctx);
8186 	struct e1000_hw *hw = &sc->hw;
8187 	struct e1000_fw_version *fw_ver = &sc->fw_ver;
8188 	uint16_t eep = 0;
8189 
8190 	/*
8191 	 * em_fw_version_locked() must run under the IFLIB_CTX_LOCK to meet
8192 	 * the NVM locking model, so we do it in em_if_attach_pre() and store
8193 	 * the info in the softc
8194 	 */
8195 	ASSERT_CTX_LOCK_HELD(hw);
8196 
8197 	*fw_ver = (struct e1000_fw_version){0};
8198 
8199 	if (hw->mac.type >= igb_mac_min) {
8200 		/*
8201 		 * Use the Shared Code for igb(4)
8202 		 */
8203 		e1000_get_fw_version(hw, fw_ver);
8204 	} else {
8205 		/*
8206 		 * Otherwise, EEPROM version should be present on (almost?)
8207 		 * all devices here
8208 		 */
8209 		if(e1000_read_nvm(hw, NVM_VERSION, 1, &eep)) {
8210 			INIT_DEBUGOUT("can't get EEPROM version");
8211 			return;
8212 		}
8213 
8214 		fw_ver->eep_major = (eep & NVM_MAJOR_MASK) >> NVM_MAJOR_SHIFT;
8215 		fw_ver->eep_minor = (eep & NVM_MINOR_MASK) >> NVM_MINOR_SHIFT;
8216 		fw_ver->eep_build = (eep & NVM_IMAGE_ID_MASK);
8217 	}
8218 }
8219 
8220 static void
em_sbuf_fw_version(struct e1000_fw_version * fw_ver,struct sbuf * buf)8221 em_sbuf_fw_version(struct e1000_fw_version *fw_ver, struct sbuf *buf)
8222 {
8223 	const char *space = "";
8224 
8225 	if (fw_ver->eep_major || fw_ver->eep_minor || fw_ver->eep_build) {
8226 		sbuf_printf(buf, "EEPROM V%d.%d-%d", fw_ver->eep_major,
8227 			    fw_ver->eep_minor, fw_ver->eep_build);
8228 		space = " ";
8229 	}
8230 
8231 	if (fw_ver->invm_major || fw_ver->invm_minor ||
8232 	    fw_ver->invm_img_type) {
8233 		sbuf_printf(buf, "%sNVM V%d.%d imgtype%d",
8234 		    space, fw_ver->invm_major, fw_ver->invm_minor,
8235 		    fw_ver->invm_img_type);
8236 		space = " ";
8237 	}
8238 
8239 	if (fw_ver->or_valid) {
8240 		sbuf_printf(buf, "%sOption ROM V%d-b%d-p%d",
8241 		    space, fw_ver->or_major, fw_ver->or_build,
8242 		    fw_ver->or_patch);
8243 		space = " ";
8244 	}
8245 
8246 	if (fw_ver->etrack_id)
8247 		sbuf_printf(buf, "%seTrack 0x%08x", space, fw_ver->etrack_id);
8248 }
8249 
8250 static void
em_print_fw_version(struct e1000_softc * sc)8251 em_print_fw_version(struct e1000_softc *sc )
8252 {
8253 	device_t dev = sc->dev;
8254 	struct sbuf *buf;
8255 	int error = 0;
8256 
8257 	buf = sbuf_new_auto();
8258 	if (!buf) {
8259 		device_printf(dev, "Could not allocate sbuf for output.\n");
8260 		return;
8261 	}
8262 
8263 	em_sbuf_fw_version(&sc->fw_ver, buf);
8264 
8265 	error = sbuf_finish(buf);
8266 	if (error)
8267 		device_printf(dev, "Error finishing sbuf: %d\n", error);
8268 	else if (sbuf_len(buf))
8269 		device_printf(dev, "%s\n", sbuf_data(buf));
8270 
8271 	sbuf_delete(buf);
8272 }
8273 
8274 static int
em_sysctl_print_fw_version(SYSCTL_HANDLER_ARGS)8275 em_sysctl_print_fw_version(SYSCTL_HANDLER_ARGS)
8276 {
8277 	struct e1000_softc *sc = (struct e1000_softc *)arg1;
8278 	device_t dev = sc->dev;
8279 	struct sbuf *buf;
8280 	int error = 0;
8281 
8282 	buf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8283 	if (!buf) {
8284 		device_printf(dev, "Could not allocate sbuf for output.\n");
8285 		return (ENOMEM);
8286 	}
8287 
8288 	em_sbuf_fw_version(&sc->fw_ver, buf);
8289 
8290 	error = sbuf_finish(buf);
8291 	if (error)
8292 		device_printf(dev, "Error finishing sbuf: %d\n", error);
8293 
8294 	sbuf_delete(buf);
8295 
8296 	return (0);
8297 }
8298 
8299 /**********************************************************************
8300  *
8301  *  This routine provides a way to dump out the adapter eeprom,
8302  *  often a useful debug/service tool. This only dumps the first
8303  *  32 words, stuff that matters is in that extent.
8304  *
8305  **********************************************************************/
8306 static int
em_sysctl_nvm_info(SYSCTL_HANDLER_ARGS)8307 em_sysctl_nvm_info(SYSCTL_HANDLER_ARGS)
8308 {
8309 	struct e1000_softc *sc = (struct e1000_softc *)arg1;
8310 	int error;
8311 	int result;
8312 
8313 	result = -1;
8314 	error = sysctl_handle_int(oidp, &result, 0, req);
8315 
8316 	if (error || !req->newptr)
8317 		return (error);
8318 
8319 	/*
8320 	 * This value will cause a hex dump of the
8321 	 * first 32 16-bit words of the EEPROM to
8322 	 * the screen.
8323 	 */
8324 	if (result == 1)
8325 		em_print_nvm_info(sc);
8326 
8327 	return (error);
8328 }
8329 
8330 static void
em_print_nvm_info(struct e1000_softc * sc)8331 em_print_nvm_info(struct e1000_softc *sc)
8332 {
8333 	struct e1000_hw *hw = &sc->hw;
8334 	struct sx *iflib_ctx_lock = iflib_ctx_lock_get(sc->ctx);
8335 	u16 eeprom_data;
8336 	int i, j, row = 0;
8337 
8338 	/* Its a bit crude, but it gets the job done */
8339 	printf("\nInterface EEPROM Dump:\n");
8340 	printf("Offset\n0x0000  ");
8341 
8342 	/* We rely on the IFLIB_CTX_LOCK as part of NVM locking model */
8343 	sx_xlock(iflib_ctx_lock);
8344 	ASSERT_CTX_LOCK_HELD(hw);
8345 	for (i = 0, j = 0; i < 32; i++, j++) {
8346 		if (j == 8) { /* Make the offset block */
8347 			j = 0; ++row;
8348 			printf("\n0x00%x0  ",row);
8349 		}
8350 		eeprom_data = 0;
8351 		if (e1000_read_nvm(hw, i, 1, &eeprom_data) !=
8352 		    E1000_SUCCESS) {
8353 			printf("\nNVM read failed at offset %#x\n", i);
8354 			break;
8355 		}
8356 		printf("%04x ", eeprom_data);
8357 	}
8358 	sx_xunlock(iflib_ctx_lock);
8359 	printf("\n");
8360 }
8361 
8362 static int
em_sysctl_int_delay(SYSCTL_HANDLER_ARGS)8363 em_sysctl_int_delay(SYSCTL_HANDLER_ARGS)
8364 {
8365 	struct em_int_delay_info *info;
8366 	struct e1000_softc *sc;
8367 	u32 regval;
8368 	int error, usecs, ticks;
8369 
8370 	info = (struct em_int_delay_info *) arg1;
8371 	usecs = info->value;
8372 	error = sysctl_handle_int(oidp, &usecs, 0, req);
8373 	if (error != 0 || req->newptr == NULL)
8374 		return (error);
8375 	if (usecs < 0 || usecs > EM_TICKS_TO_USECS(65535))
8376 		return (EINVAL);
8377 	info->value = usecs;
8378 	ticks = EM_USECS_TO_TICKS(usecs);
8379 
8380 	sc = info->sc;
8381 
8382 	regval = E1000_READ_OFFSET(&sc->hw, info->offset);
8383 	regval = (regval & ~0xffff) | (ticks & 0xffff);
8384 	/* Handle a few special cases. */
8385 	switch (info->offset) {
8386 	case E1000_RDTR:
8387 		break;
8388 	case E1000_TIDV:
8389 		if (ticks == 0) {
8390 			sc->txd_cmd &= ~E1000_TXD_CMD_IDE;
8391 			/* Don't write 0 into the TIDV register. */
8392 			regval++;
8393 		} else
8394 			sc->txd_cmd |= E1000_TXD_CMD_IDE;
8395 		break;
8396 	}
8397 	E1000_WRITE_OFFSET(&sc->hw, info->offset, regval);
8398 	return (0);
8399 }
8400 
8401 static int
em_sysctl_tso_tcp_flags_mask(SYSCTL_HANDLER_ARGS)8402 em_sysctl_tso_tcp_flags_mask(SYSCTL_HANDLER_ARGS)
8403 {
8404 	struct e1000_softc *sc;
8405 	u32 reg, val, shift;
8406 	int error, mask;
8407 
8408 	sc = oidp->oid_arg1;
8409 	switch (oidp->oid_arg2) {
8410 	case 0:
8411 		reg = E1000_DTXTCPFLGL;
8412 		shift = 0;
8413 		break;
8414 	case 1:
8415 		reg = E1000_DTXTCPFLGL;
8416 		shift = 16;
8417 		break;
8418 	case 2:
8419 		reg = E1000_DTXTCPFLGH;
8420 		shift = 0;
8421 		break;
8422 	default:
8423 		return (EINVAL);
8424 		break;
8425 	}
8426 	val = E1000_READ_REG(&sc->hw, reg);
8427 	mask = (val >> shift) & 0xfff;
8428 	error = sysctl_handle_int(oidp, &mask, 0, req);
8429 	if (error != 0 || req->newptr == NULL)
8430 		return (error);
8431 	if (mask < 0 || mask > 0xfff)
8432 		return (EINVAL);
8433 	val = (val & ~(0xfff << shift)) | (mask << shift);
8434 	E1000_WRITE_REG(&sc->hw, reg, val);
8435 	return (0);
8436 }
8437 
8438 static void
em_add_int_delay_sysctl(struct e1000_softc * sc,const char * name,const char * description,struct em_int_delay_info * info,int offset,int value)8439 em_add_int_delay_sysctl(struct e1000_softc *sc, const char *name,
8440     const char *description, struct em_int_delay_info *info, int offset,
8441     int value)
8442 {
8443 	info->sc = sc;
8444 	info->offset = offset;
8445 	info->value = value;
8446 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->dev),
8447 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
8448 	    OID_AUTO, name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
8449 	    info, 0, em_sysctl_int_delay, "I", description);
8450 }
8451 
8452 /*
8453  * Set flow control using sysctl:
8454  * Flow control values:
8455  *      0 - off
8456  *      1 - rx pause
8457  *      2 - tx pause
8458  *      3 - full
8459  */
8460 static int
em_set_flowcntl(SYSCTL_HANDLER_ARGS)8461 em_set_flowcntl(SYSCTL_HANDLER_ARGS)
8462 {
8463 	int error;
8464 	static int input = 3; /* default is full */
8465 	struct e1000_softc *sc = (struct e1000_softc *) arg1;
8466 
8467 	error = sysctl_handle_int(oidp, &input, 0, req);
8468 
8469 	if ((error) || (req->newptr == NULL))
8470 		return (error);
8471 
8472 	if (input == sc->fc) /* no change? */
8473 		return (error);
8474 
8475 	switch (input) {
8476 	case e1000_fc_rx_pause:
8477 	case e1000_fc_tx_pause:
8478 	case e1000_fc_full:
8479 	case e1000_fc_none:
8480 		sc->hw.fc.requested_mode = input;
8481 		sc->fc = input;
8482 		break;
8483 	default:
8484 		/* Do nothing */
8485 		return (error);
8486 	}
8487 
8488 	sc->hw.fc.current_mode = sc->hw.fc.requested_mode;
8489 	e1000_force_mac_fc(&sc->hw);
8490 	return (error);
8491 }
8492 
8493 static void
em_sysctl_request_reinit(struct e1000_softc * sc)8494 em_sysctl_request_reinit(struct e1000_softc *sc)
8495 {
8496 	if ((if_getflags(iflib_get_ifp(sc->ctx)) & IFF_UP) == 0)
8497 		return;
8498 
8499 	iflib_request_reset(sc->ctx);
8500 	iflib_admin_intr_deferred(sc->ctx);
8501 }
8502 
8503 /*
8504  * Manage DMA Coalesce:
8505  * Control values:
8506  * 	0/1 - off/on
8507  *	Legal timer values are:
8508  *	250,500,1000-10000 in thousands
8509  */
8510 static int
igb_sysctl_dmac(SYSCTL_HANDLER_ARGS)8511 igb_sysctl_dmac(SYSCTL_HANDLER_ARGS)
8512 {
8513 	struct e1000_softc *sc = (struct e1000_softc *) arg1;
8514 	int error;
8515 
8516 	error = sysctl_handle_int(oidp, &sc->dmac, 0, req);
8517 
8518 	if ((error) || (req->newptr == NULL))
8519 		return (error);
8520 
8521 	switch (sc->dmac) {
8522 		case 0:
8523 			/* Disabling */
8524 			break;
8525 		case 1: /* Just enable and use default */
8526 			sc->dmac = 1000;
8527 			break;
8528 		case 250:
8529 		case 500:
8530 		case 1000:
8531 		case 2000:
8532 		case 3000:
8533 		case 4000:
8534 		case 5000:
8535 		case 6000:
8536 		case 7000:
8537 		case 8000:
8538 		case 9000:
8539 		case 10000:
8540 			/* Legal values - allow */
8541 			break;
8542 		default:
8543 			/* Do nothing, illegal value */
8544 			sc->dmac = 0;
8545 			return (EINVAL);
8546 	}
8547 	/* Reinit the interface */
8548 	em_sysctl_request_reinit(sc);
8549 	return (error);
8550 }
8551 
8552 /*
8553  * Manage Energy Efficient Ethernet:
8554  * Control values:
8555  *     0/1 - enabled/disabled
8556  */
8557 static int
em_sysctl_eee(SYSCTL_HANDLER_ARGS)8558 em_sysctl_eee(SYSCTL_HANDLER_ARGS)
8559 {
8560 	struct e1000_softc *sc = (struct e1000_softc *) arg1;
8561 	int error, value;
8562 
8563 	if (sc->hw.mac.type < igb_mac_min)
8564 		value = sc->hw.dev_spec.ich8lan.eee_disable;
8565 	else
8566 		value = sc->hw.dev_spec._82575.eee_disable;
8567 	error = sysctl_handle_int(oidp, &value, 0, req);
8568 	if (error || req->newptr == NULL)
8569 		return (error);
8570 	if (sc->hw.mac.type < igb_mac_min)
8571 		sc->hw.dev_spec.ich8lan.eee_disable = (value != 0);
8572 	else
8573 		sc->hw.dev_spec._82575.eee_disable = (value != 0);
8574 	em_sysctl_request_reinit(sc);
8575 
8576 	return (0);
8577 }
8578 
8579 static int
em_sysctl_debug_info(SYSCTL_HANDLER_ARGS)8580 em_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
8581 {
8582 	struct e1000_softc *sc;
8583 	int error;
8584 	int result;
8585 
8586 	result = -1;
8587 	error = sysctl_handle_int(oidp, &result, 0, req);
8588 
8589 	if (error || !req->newptr)
8590 		return (error);
8591 
8592 	if (result == 1) {
8593 		sc = (struct e1000_softc *) arg1;
8594 		em_print_debug_info(sc);
8595 	}
8596 
8597 	return (error);
8598 }
8599 
8600 static int
em_get_rs(SYSCTL_HANDLER_ARGS)8601 em_get_rs(SYSCTL_HANDLER_ARGS)
8602 {
8603 	struct e1000_softc *sc = (struct e1000_softc *) arg1;
8604 	int error;
8605 	int result;
8606 
8607 	result = 0;
8608 	error = sysctl_handle_int(oidp, &result, 0, req);
8609 
8610 	if (error || !req->newptr || result != 1)
8611 		return (error);
8612 	em_dump_rs(sc);
8613 
8614 	return (error);
8615 }
8616 
8617 static void
em_if_debug(if_ctx_t ctx)8618 em_if_debug(if_ctx_t ctx)
8619 {
8620 	em_dump_rs(iflib_get_softc(ctx));
8621 }
8622 
8623 /*
8624  * This routine is meant to be fluid, add whatever is
8625  * needed for debugging a problem.  -jfv
8626  */
8627 static void
em_print_debug_info(struct e1000_softc * sc)8628 em_print_debug_info(struct e1000_softc *sc)
8629 {
8630 	device_t dev = iflib_get_dev(sc->ctx);
8631 	if_t ifp = iflib_get_ifp(sc->ctx);
8632 	struct tx_ring *txr;
8633 	struct rx_ring *rxr;
8634 
8635 	if (sc->tx_queues == NULL || sc->rx_queues == NULL) {
8636 		device_printf(dev, "queue state is unavailable\n");
8637 		return;
8638 	}
8639 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
8640 		printf("Interface is RUNNING ");
8641 	else
8642 		printf("Interface is NOT RUNNING\n");
8643 
8644 	if (if_getdrvflags(ifp) & IFF_DRV_OACTIVE)
8645 		printf("and INACTIVE\n");
8646 	else
8647 		printf("and ACTIVE\n");
8648 
8649 	for (int i = 0; i < sc->tx_num_queues; i++) {
8650 		txr = &sc->tx_queues[i].txr;
8651 		device_printf(dev, "TX Queue %d ------\n", i);
8652 		device_printf(dev, "hw tdh = %d, hw tdt = %d\n",
8653 		    E1000_READ_REG(&sc->hw, E1000_TDH(txr->me)),
8654 		    E1000_READ_REG(&sc->hw, E1000_TDT(txr->me)));
8655 
8656 	}
8657 	for (int j = 0; j < sc->rx_num_queues; j++) {
8658 		rxr = &sc->rx_queues[j].rxr;
8659 		device_printf(dev, "RX Queue %d ------\n", j);
8660 		device_printf(dev, "hw rdh = %d, hw rdt = %d\n",
8661 		    E1000_READ_REG(&sc->hw, E1000_RDH(rxr->me)),
8662 		    E1000_READ_REG(&sc->hw, E1000_RDT(rxr->me)));
8663 	}
8664 }
8665 
8666 /*
8667  * 82574 only:
8668  * Write a new value to the EEPROM increasing the number of MSI-X
8669  * vectors from 3 to 5, for proper multiqueue support.
8670  */
8671 static void
em_enable_vectors_82574(if_ctx_t ctx)8672 em_enable_vectors_82574(if_ctx_t ctx)
8673 {
8674 	struct e1000_softc *sc = iflib_get_softc(ctx);
8675 	struct e1000_hw *hw = &sc->hw;
8676 	device_t dev = iflib_get_dev(ctx);
8677 	u16 edata;
8678 
8679 	e1000_read_nvm(hw, EM_NVM_PCIE_CTRL, 1, &edata);
8680 	if (bootverbose)
8681 		device_printf(dev, "EM_NVM_PCIE_CTRL = %#06x\n", edata);
8682 	if (((edata & EM_NVM_MSIX_N_MASK) >> EM_NVM_MSIX_N_SHIFT) != 4) {
8683 		device_printf(dev, "Writing to eeprom: increasing "
8684 		    "reported MSI-X vectors from 3 to 5...\n");
8685 		edata &= ~(EM_NVM_MSIX_N_MASK);
8686 		edata |= 4 << EM_NVM_MSIX_N_SHIFT;
8687 		e1000_write_nvm(hw, EM_NVM_PCIE_CTRL, 1, &edata);
8688 		e1000_update_nvm_checksum(hw);
8689 		device_printf(dev, "Writing to eeprom: done\n");
8690 	}
8691 }
8692