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