xref: /freebsd/sys/dev/e1000/if_em.c (revision 2c02e6ca7154593d214b62578f67d9fe7db23d70)
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 <sys/sbuf.h>
32 #include <machine/_inttypes.h>
33 
34 #define em_mac_min e1000_82571
35 #define igb_mac_min e1000_82575
36 
37 /*********************************************************************
38  *  Driver version:
39  *********************************************************************/
40 static const char em_driver_version[] = "7.7.8-fbsd";
41 static const char igb_driver_version[] = "2.5.28-fbsd";
42 
43 /*********************************************************************
44  *  PCI Device ID Table
45  *
46  *  Used by probe to select devices to load on
47  *  Last field stores an index into e1000_strings
48  *  Last entry must be all 0s
49  *
50  *  { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
51  *********************************************************************/
52 
53 static const pci_vendor_info_t em_vendor_info_array[] =
54 {
55 	/* Intel(R) - lem-class legacy devices */
56 	PVID(0x8086, E1000_DEV_ID_82540EM,
57 	    "Intel(R) Legacy PRO/1000 MT 82540EM"),
58 	PVID(0x8086, E1000_DEV_ID_82540EM_LOM,
59 	    "Intel(R) Legacy PRO/1000 MT 82540EM (LOM)"),
60 	PVID(0x8086, E1000_DEV_ID_82540EP,
61 	    "Intel(R) Legacy PRO/1000 MT 82540EP"),
62 	PVID(0x8086, E1000_DEV_ID_82540EP_LOM,
63 	    "Intel(R) Legacy PRO/1000 MT 82540EP (LOM)"),
64 	PVID(0x8086, E1000_DEV_ID_82540EP_LP,
65 	    "Intel(R) Legacy PRO/1000 MT 82540EP (Mobile)"),
66 
67 	PVID(0x8086, E1000_DEV_ID_82541EI,
68 	    "Intel(R) Legacy PRO/1000 MT 82541EI (Copper)"),
69 	PVID(0x8086, E1000_DEV_ID_82541ER,
70 	    "Intel(R) Legacy PRO/1000 82541ER"),
71 	PVID(0x8086, E1000_DEV_ID_82541ER_LOM,
72 	    "Intel(R) Legacy PRO/1000 MT 82541ER"),
73 	PVID(0x8086, E1000_DEV_ID_82541EI_MOBILE,
74 	    "Intel(R) Legacy PRO/1000 MT 82541EI (Mobile)"),
75 	PVID(0x8086, E1000_DEV_ID_82541GI,
76 	    "Intel(R) Legacy PRO/1000 MT 82541GI"),
77 	PVID(0x8086, E1000_DEV_ID_82541GI_LF,
78 	    "Intel(R) Legacy PRO/1000 GT 82541PI"),
79 	PVID(0x8086, E1000_DEV_ID_82541GI_MOBILE,
80 	    "Intel(R) Legacy PRO/1000 MT 82541GI (Mobile)"),
81 
82 	PVID(0x8086, E1000_DEV_ID_82542,
83 	    "Intel(R) Legacy PRO/1000 82542 (Fiber)"),
84 
85 	PVID(0x8086, E1000_DEV_ID_82543GC_FIBER,
86 	    "Intel(R) Legacy PRO/1000 F 82543GC (Fiber)"),
87 	PVID(0x8086, E1000_DEV_ID_82543GC_COPPER,
88 	    "Intel(R) Legacy PRO/1000 T 82543GC (Copper)"),
89 
90 	PVID(0x8086, E1000_DEV_ID_82544EI_COPPER,
91 	    "Intel(R) Legacy PRO/1000 XT 82544EI (Copper)"),
92 	PVID(0x8086, E1000_DEV_ID_82544EI_FIBER,
93 	    "Intel(R) Legacy PRO/1000 XF 82544EI (Fiber)"),
94 	PVID(0x8086, E1000_DEV_ID_82544GC_COPPER,
95 	    "Intel(R) Legacy PRO/1000 T 82544GC (Copper)"),
96 	PVID(0x8086, E1000_DEV_ID_82544GC_LOM,
97 	    "Intel(R) Legacy PRO/1000 XT 82544GC (LOM)"),
98 
99 	PVID(0x8086, E1000_DEV_ID_82545EM_COPPER,
100 	    "Intel(R) Legacy PRO/1000 MT 82545EM (Copper)"),
101 	PVID(0x8086, E1000_DEV_ID_82545EM_FIBER,
102 	    "Intel(R) Legacy PRO/1000 MF 82545EM (Fiber)"),
103 	PVID(0x8086, E1000_DEV_ID_82545GM_COPPER,
104 	    "Intel(R) Legacy PRO/1000 MT 82545GM (Copper)"),
105 	PVID(0x8086, E1000_DEV_ID_82545GM_FIBER,
106 	    "Intel(R) Legacy PRO/1000 MF 82545GM (Fiber)"),
107 	PVID(0x8086, E1000_DEV_ID_82545GM_SERDES,
108 	    "Intel(R) Legacy PRO/1000 MB 82545GM (SERDES)"),
109 
110 	PVID(0x8086, E1000_DEV_ID_82546EB_COPPER,
111 	    "Intel(R) Legacy PRO/1000 MT 82546EB (Copper)"),
112 	PVID(0x8086, E1000_DEV_ID_82546EB_FIBER,
113 	    "Intel(R) Legacy PRO/1000 MF 82546EB (Fiber)"),
114 	PVID(0x8086, E1000_DEV_ID_82546EB_QUAD_COPPER,
115 	    "Intel(R) Legacy PRO/1000 MT 82546EB (Quad Copper"),
116 	PVID(0x8086, E1000_DEV_ID_82546GB_COPPER,
117 	    "Intel(R) Legacy PRO/1000 MT 82546GB (Copper)"),
118 	PVID(0x8086, E1000_DEV_ID_82546GB_FIBER,
119 	    "Intel(R) Legacy PRO/1000 MF 82546GB (Fiber)"),
120 	PVID(0x8086, E1000_DEV_ID_82546GB_SERDES,
121 	    "Intel(R) Legacy PRO/1000 MB 82546GB (SERDES)"),
122 	PVID(0x8086, E1000_DEV_ID_82546GB_PCIE,
123 	    "Intel(R) Legacy PRO/1000 P 82546GB (PCIe)"),
124 	PVID(0x8086, E1000_DEV_ID_82546GB_QUAD_COPPER,
125 	    "Intel(R) Legacy PRO/1000 GT 82546GB (Quad Copper)"),
126 	PVID(0x8086, E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3,
127 	    "Intel(R) Legacy PRO/1000 GT 82546GB (Quad Copper)"),
128 
129 	PVID(0x8086, E1000_DEV_ID_82547EI,
130 	    "Intel(R) Legacy PRO/1000 CT 82547EI"),
131 	PVID(0x8086, E1000_DEV_ID_82547EI_MOBILE,
132 	    "Intel(R) Legacy PRO/1000 CT 82547EI (Mobile)"),
133 	PVID(0x8086, E1000_DEV_ID_82547GI,
134 	    "Intel(R) Legacy PRO/1000 CT 82547GI"),
135 
136 	/* Intel(R) - em-class devices */
137 	PVID(0x8086, E1000_DEV_ID_82571EB_COPPER,
138 	    "Intel(R) PRO/1000 PT 82571EB/82571GB (Copper)"),
139 	PVID(0x8086, E1000_DEV_ID_82571EB_FIBER,
140 	    "Intel(R) PRO/1000 PF 82571EB/82571GB (Fiber)"),
141 	PVID(0x8086, E1000_DEV_ID_82571EB_SERDES,
142 	    "Intel(R) PRO/1000 PB 82571EB (SERDES)"),
143 	PVID(0x8086, E1000_DEV_ID_82571EB_SERDES_DUAL,
144 	    "Intel(R) PRO/1000 82571EB (Dual Mezzanine)"),
145 	PVID(0x8086, E1000_DEV_ID_82571EB_SERDES_QUAD,
146 	    "Intel(R) PRO/1000 82571EB (Quad Mezzanine)"),
147 	PVID(0x8086, E1000_DEV_ID_82571EB_QUAD_COPPER,
148 	    "Intel(R) PRO/1000 PT 82571EB/82571GB (Quad Copper)"),
149 	PVID(0x8086, E1000_DEV_ID_82571EB_QUAD_COPPER_LP,
150 	    "Intel(R) PRO/1000 PT 82571EB/82571GB (Quad Copper)"),
151 	PVID(0x8086, E1000_DEV_ID_82571EB_QUAD_FIBER,
152 	    "Intel(R) PRO/1000 PF 82571EB (Quad Fiber)"),
153 	PVID(0x8086, E1000_DEV_ID_82571PT_QUAD_COPPER,
154 	    "Intel(R) PRO/1000 PT 82571PT (Quad Copper)"),
155 	PVID(0x8086, E1000_DEV_ID_82572EI,
156 	    "Intel(R) PRO/1000 PT 82572EI (Copper)"),
157 	PVID(0x8086, E1000_DEV_ID_82572EI_COPPER,
158 	    "Intel(R) PRO/1000 PT 82572EI (Copper)"),
159 	PVID(0x8086, E1000_DEV_ID_82572EI_FIBER,
160 	    "Intel(R) PRO/1000 PF 82572EI (Fiber)"),
161 	PVID(0x8086, E1000_DEV_ID_82572EI_SERDES,
162 	    "Intel(R) PRO/1000 82572EI (SERDES)"),
163 	PVID(0x8086, E1000_DEV_ID_82573E,
164 	    "Intel(R) PRO/1000 82573E (Copper)"),
165 	PVID(0x8086, E1000_DEV_ID_82573E_IAMT,
166 	    "Intel(R) PRO/1000 82573E AMT (Copper)"),
167 	PVID(0x8086, E1000_DEV_ID_82573L, "Intel(R) PRO/1000 82573L"),
168 	PVID(0x8086, E1000_DEV_ID_82583V, "Intel(R) 82583V"),
169 	PVID(0x8086, E1000_DEV_ID_80003ES2LAN_COPPER_SPT,
170 	    "Intel(R) 80003ES2LAN (Copper)"),
171 	PVID(0x8086, E1000_DEV_ID_80003ES2LAN_SERDES_SPT,
172 	    "Intel(R) 80003ES2LAN (SERDES)"),
173 	PVID(0x8086, E1000_DEV_ID_80003ES2LAN_COPPER_DPT,
174 	    "Intel(R) 80003ES2LAN (Dual Copper)"),
175 	PVID(0x8086, E1000_DEV_ID_80003ES2LAN_SERDES_DPT,
176 	    "Intel(R) 80003ES2LAN (Dual SERDES)"),
177 	PVID(0x8086, E1000_DEV_ID_ICH8_IGP_M_AMT,
178 	    "Intel(R) 82566MM ICH8 AMT (Mobile)"),
179 	PVID(0x8086, E1000_DEV_ID_ICH8_IGP_AMT, "Intel(R) 82566DM ICH8 AMT"),
180 	PVID(0x8086, E1000_DEV_ID_ICH8_IGP_C, "Intel(R) 82566DC ICH8"),
181 	PVID(0x8086, E1000_DEV_ID_ICH8_IFE, "Intel(R) 82562V ICH8"),
182 	PVID(0x8086, E1000_DEV_ID_ICH8_IFE_GT, "Intel(R) 82562GT ICH8"),
183 	PVID(0x8086, E1000_DEV_ID_ICH8_IFE_G, "Intel(R) 82562G ICH8"),
184 	PVID(0x8086, E1000_DEV_ID_ICH8_IGP_M, "Intel(R) 82566MC ICH8"),
185 	PVID(0x8086, E1000_DEV_ID_ICH8_82567V_3, "Intel(R) 82567V-3 ICH8"),
186 	PVID(0x8086, E1000_DEV_ID_ICH9_IGP_M_AMT,
187 	    "Intel(R) 82567LM ICH9 AMT"),
188 	PVID(0x8086, E1000_DEV_ID_ICH9_IGP_AMT,
189 	    "Intel(R) 82566DM-2 ICH9 AMT"),
190 	PVID(0x8086, E1000_DEV_ID_ICH9_IGP_C, "Intel(R) 82566DC-2 ICH9"),
191 	PVID(0x8086, E1000_DEV_ID_ICH9_IGP_M, "Intel(R) 82567LF ICH9"),
192 	PVID(0x8086, E1000_DEV_ID_ICH9_IGP_M_V, "Intel(R) 82567V ICH9"),
193 	PVID(0x8086, E1000_DEV_ID_ICH9_IFE, "Intel(R) 82562V-2 ICH9"),
194 	PVID(0x8086, E1000_DEV_ID_ICH9_IFE_GT, "Intel(R) 82562GT-2 ICH9"),
195 	PVID(0x8086, E1000_DEV_ID_ICH9_IFE_G, "Intel(R) 82562G-2 ICH9"),
196 	PVID(0x8086, E1000_DEV_ID_ICH9_BM, "Intel(R) 82567LM-4 ICH9"),
197 	PVID(0x8086, E1000_DEV_ID_82574L, "Intel(R) Gigabit CT 82574L"),
198 	PVID(0x8086, E1000_DEV_ID_82574LA, "Intel(R) 82574L-Apple"),
199 	PVID(0x8086, E1000_DEV_ID_ICH10_R_BM_LM, "Intel(R) 82567LM-2 ICH10"),
200 	PVID(0x8086, E1000_DEV_ID_ICH10_R_BM_LF, "Intel(R) 82567LF-2 ICH10"),
201 	PVID(0x8086, E1000_DEV_ID_ICH10_R_BM_V, "Intel(R) 82567V-2 ICH10"),
202 	PVID(0x8086, E1000_DEV_ID_ICH10_D_BM_LM, "Intel(R) 82567LM-3 ICH10"),
203 	PVID(0x8086, E1000_DEV_ID_ICH10_D_BM_LF, "Intel(R) 82567LF-3 ICH10"),
204 	PVID(0x8086, E1000_DEV_ID_ICH10_D_BM_V, "Intel(R) 82567V-4 ICH10"),
205 	PVID(0x8086, E1000_DEV_ID_PCH_M_HV_LM, "Intel(R) 82577LM"),
206 	PVID(0x8086, E1000_DEV_ID_PCH_M_HV_LC, "Intel(R) 82577LC"),
207 	PVID(0x8086, E1000_DEV_ID_PCH_D_HV_DM, "Intel(R) 82578DM"),
208 	PVID(0x8086, E1000_DEV_ID_PCH_D_HV_DC, "Intel(R) 82578DC"),
209 	PVID(0x8086, E1000_DEV_ID_PCH2_LV_LM, "Intel(R) 82579LM"),
210 	PVID(0x8086, E1000_DEV_ID_PCH2_LV_V, "Intel(R) 82579V"),
211 	PVID(0x8086, E1000_DEV_ID_PCH_LPT_I217_LM, "Intel(R) I217-LM LPT"),
212 	PVID(0x8086, E1000_DEV_ID_PCH_LPT_I217_V, "Intel(R) I217-V LPT"),
213 	PVID(0x8086, E1000_DEV_ID_PCH_LPTLP_I218_LM,
214 	    "Intel(R) I218-LM LPTLP"),
215 	PVID(0x8086, E1000_DEV_ID_PCH_LPTLP_I218_V, "Intel(R) I218-V LPTLP"),
216 	PVID(0x8086, E1000_DEV_ID_PCH_I218_LM2, "Intel(R) I218-LM (2)"),
217 	PVID(0x8086, E1000_DEV_ID_PCH_I218_V2, "Intel(R) I218-V (2)"),
218 	PVID(0x8086, E1000_DEV_ID_PCH_I218_LM3, "Intel(R) I218-LM (3)"),
219 	PVID(0x8086, E1000_DEV_ID_PCH_I218_V3, "Intel(R) I218-V (3)"),
220 	PVID(0x8086, E1000_DEV_ID_PCH_SPT_I219_LM, "Intel(R) I219-LM SPT"),
221 	PVID(0x8086, E1000_DEV_ID_PCH_SPT_I219_V, "Intel(R) I219-V SPT"),
222 	PVID(0x8086, E1000_DEV_ID_PCH_SPT_I219_LM2,
223 	    "Intel(R) I219-LM SPT-H(2)"),
224 	PVID(0x8086, E1000_DEV_ID_PCH_SPT_I219_V2,
225 	    "Intel(R) I219-V SPT-H(2)"),
226 	PVID(0x8086, E1000_DEV_ID_PCH_LBG_I219_LM3,
227 	    "Intel(R) I219-LM LBG(3)"),
228 	PVID(0x8086, E1000_DEV_ID_PCH_SPT_I219_LM4,
229 	    "Intel(R) I219-LM SPT(4)"),
230 	PVID(0x8086, E1000_DEV_ID_PCH_SPT_I219_V4, "Intel(R) I219-V SPT(4)"),
231 	PVID(0x8086, E1000_DEV_ID_PCH_SPT_I219_LM5,
232 	    "Intel(R) I219-LM SPT(5)"),
233 	PVID(0x8086, E1000_DEV_ID_PCH_SPT_I219_V5, "Intel(R) I219-V SPT(5)"),
234 	PVID(0x8086, E1000_DEV_ID_PCH_CNP_I219_LM6,
235 	    "Intel(R) I219-LM CNP(6)"),
236 	PVID(0x8086, E1000_DEV_ID_PCH_CNP_I219_V6, "Intel(R) I219-V CNP(6)"),
237 	PVID(0x8086, E1000_DEV_ID_PCH_CNP_I219_LM7,
238 	    "Intel(R) I219-LM CNP(7)"),
239 	PVID(0x8086, E1000_DEV_ID_PCH_CNP_I219_V7, "Intel(R) I219-V CNP(7)"),
240 	PVID(0x8086, E1000_DEV_ID_PCH_ICP_I219_LM8,
241 	    "Intel(R) I219-LM ICP(8)"),
242 	PVID(0x8086, E1000_DEV_ID_PCH_ICP_I219_V8, "Intel(R) I219-V ICP(8)"),
243 	PVID(0x8086, E1000_DEV_ID_PCH_ICP_I219_LM9,
244 	    "Intel(R) I219-LM ICP(9)"),
245 	PVID(0x8086, E1000_DEV_ID_PCH_ICP_I219_V9, "Intel(R) I219-V ICP(9)"),
246 	PVID(0x8086, E1000_DEV_ID_PCH_CMP_I219_LM10,
247 	    "Intel(R) I219-LM CMP(10)"),
248 	PVID(0x8086, E1000_DEV_ID_PCH_CMP_I219_V10,
249 	    "Intel(R) I219-V CMP(10)"),
250 	PVID(0x8086, E1000_DEV_ID_PCH_CMP_I219_LM11,
251 	    "Intel(R) I219-LM CMP(11)"),
252 	PVID(0x8086, E1000_DEV_ID_PCH_CMP_I219_V11,
253 	    "Intel(R) I219-V CMP(11)"),
254 	PVID(0x8086, E1000_DEV_ID_PCH_CMP_I219_LM12,
255 	    "Intel(R) I219-LM CMP(12)"),
256 	PVID(0x8086, E1000_DEV_ID_PCH_CMP_I219_V12,
257 	    "Intel(R) I219-V CMP(12)"),
258 	PVID(0x8086, E1000_DEV_ID_PCH_TGP_I219_LM13,
259 	    "Intel(R) I219-LM TGP(13)"),
260 	PVID(0x8086, E1000_DEV_ID_PCH_TGP_I219_V13,
261 	    "Intel(R) I219-V TGP(13)"),
262 	PVID(0x8086, E1000_DEV_ID_PCH_TGP_I219_LM14,
263 	    "Intel(R) I219-LM TGP(14)"),
264 	PVID(0x8086, E1000_DEV_ID_PCH_TGP_I219_V14,
265 	    "Intel(R) I219-V GTP(14)"),
266 	PVID(0x8086, E1000_DEV_ID_PCH_TGP_I219_LM15,
267 	    "Intel(R) I219-LM TGP(15)"),
268 	PVID(0x8086, E1000_DEV_ID_PCH_TGP_I219_V15,
269 	    "Intel(R) I219-V TGP(15)"),
270 	PVID(0x8086, E1000_DEV_ID_PCH_ADL_I219_LM16,
271 	    "Intel(R) I219-LM ADL(16)"),
272 	PVID(0x8086, E1000_DEV_ID_PCH_ADL_I219_V16,
273 	    "Intel(R) I219-V ADL(16)"),
274 	PVID(0x8086, E1000_DEV_ID_PCH_ADL_I219_LM17,
275 	    "Intel(R) I219-LM ADL(17)"),
276 	PVID(0x8086, E1000_DEV_ID_PCH_ADL_I219_V17,
277 	    "Intel(R) I219-V ADL(17)"),
278 	PVID(0x8086, E1000_DEV_ID_PCH_MTP_I219_LM18,
279 	    "Intel(R) I219-LM MTP(18)"),
280 	PVID(0x8086, E1000_DEV_ID_PCH_MTP_I219_V18,
281 	    "Intel(R) I219-V MTP(18)"),
282 	PVID(0x8086, E1000_DEV_ID_PCH_ADL_I219_LM19,
283 	    "Intel(R) I219-LM ADL(19)"),
284 	PVID(0x8086, E1000_DEV_ID_PCH_ADL_I219_V19,
285 	    "Intel(R) I219-V ADL(19)"),
286 	PVID(0x8086, E1000_DEV_ID_PCH_LNL_I219_LM20,
287 	    "Intel(R) I219-LM LNL(20)"),
288 	PVID(0x8086, E1000_DEV_ID_PCH_LNL_I219_V20,
289 	    "Intel(R) I219-V LNL(20)"),
290 	PVID(0x8086, E1000_DEV_ID_PCH_LNL_I219_LM21,
291 	    "Intel(R) I219-LM LNL(21)"),
292 	PVID(0x8086, E1000_DEV_ID_PCH_LNL_I219_V21,
293 	    "Intel(R) I219-V LNL(21)"),
294 	PVID(0x8086, E1000_DEV_ID_PCH_RPL_I219_LM22,
295 	    "Intel(R) I219-LM RPL(22)"),
296 	PVID(0x8086, E1000_DEV_ID_PCH_RPL_I219_V22,
297 	    "Intel(R) I219-V RPL(22)"),
298 	PVID(0x8086, E1000_DEV_ID_PCH_RPL_I219_LM23,
299 	    "Intel(R) I219-LM RPL(23)"),
300 	PVID(0x8086, E1000_DEV_ID_PCH_RPL_I219_V23,
301 	    "Intel(R) I219-V RPL(23)"),
302 	PVID(0x8086, E1000_DEV_ID_PCH_ARL_I219_LM24,
303 	    "Intel(R) I219-LM ARL(24)"),
304 	PVID(0x8086, E1000_DEV_ID_PCH_ARL_I219_V24,
305 	    "Intel(R) I219-V ARL(24)"),
306 	PVID(0x8086, E1000_DEV_ID_PCH_PTP_I219_LM25,
307 	    "Intel(R) I219-LM PTP(25)"),
308 	PVID(0x8086, E1000_DEV_ID_PCH_PTP_I219_V25,
309 	    "Intel(R) I219-V PTP(25)"),
310 	PVID(0x8086, E1000_DEV_ID_PCH_PTP_I219_LM26,
311 	    "Intel(R) I219-LM PTP(26)"),
312 	PVID(0x8086, E1000_DEV_ID_PCH_PTP_I219_V26,
313 	    "Intel(R) I219-V PTP(26)"),
314 	PVID(0x8086, E1000_DEV_ID_PCH_PTP_I219_LM27,
315 	    "Intel(R) I219-LM PTP(27)"),
316 	PVID(0x8086, E1000_DEV_ID_PCH_PTP_I219_V27,
317 	    "Intel(R) I219-V PTP(27)"),
318 	/* required last entry */
319 	PVID_END
320 };
321 
322 static const pci_vendor_info_t igb_vendor_info_array[] =
323 {
324 	/* Intel(R) - igb-class devices */
325 	PVID(0x8086, E1000_DEV_ID_82575EB_COPPER,
326 	    "Intel(R) PRO/1000 82575EB (Copper)"),
327 	PVID(0x8086, E1000_DEV_ID_82575EB_FIBER_SERDES,
328 	    "Intel(R) PRO/1000 82575EB (SERDES)"),
329 	PVID(0x8086, E1000_DEV_ID_82575GB_QUAD_COPPER,
330 	    "Intel(R) PRO/1000 VT 82575GB (Quad Copper)"),
331 	PVID(0x8086, E1000_DEV_ID_82576, "Intel(R) PRO/1000 82576"),
332 	PVID(0x8086, E1000_DEV_ID_82576_NS, "Intel(R) PRO/1000 82576NS"),
333 	PVID(0x8086, E1000_DEV_ID_82576_NS_SERDES,
334 	    "Intel(R) PRO/1000 82576NS (SERDES)"),
335 	PVID(0x8086, E1000_DEV_ID_82576_FIBER,
336 	    "Intel(R) PRO/1000 EF 82576 (Dual Fiber)"),
337 	PVID(0x8086, E1000_DEV_ID_82576_SERDES,
338 	    "Intel(R) PRO/1000 82576 (Dual SERDES)"),
339 	PVID(0x8086, E1000_DEV_ID_82576_SERDES_QUAD,
340 	    "Intel(R) PRO/1000 ET 82576 (Quad SERDES)"),
341 	PVID(0x8086, E1000_DEV_ID_82576_QUAD_COPPER,
342 	    "Intel(R) PRO/1000 ET 82576 (Quad Copper)"),
343 	PVID(0x8086, E1000_DEV_ID_82576_QUAD_COPPER_ET2,
344 	    "Intel(R) PRO/1000 ET(2) 82576 (Quad Copper)"),
345 	PVID(0x8086, E1000_DEV_ID_82576_VF,
346 	    "Intel(R) PRO/1000 82576 Virtual Function"),
347 	PVID(0x8086, E1000_DEV_ID_82580_COPPER,
348 	    "Intel(R) I340 82580 (Copper)"),
349 	PVID(0x8086, E1000_DEV_ID_82580_FIBER, "Intel(R) I340 82580 (Fiber)"),
350 	PVID(0x8086, E1000_DEV_ID_82580_SERDES,
351 	    "Intel(R) I340 82580 (SERDES)"),
352 	PVID(0x8086, E1000_DEV_ID_82580_SGMII, "Intel(R) I340 82580 (SGMII)"),
353 	PVID(0x8086, E1000_DEV_ID_82580_COPPER_DUAL,
354 	    "Intel(R) I340-T2 82580 (Dual Copper)"),
355 	PVID(0x8086, E1000_DEV_ID_82580_QUAD_FIBER,
356 	    "Intel(R) I340-F4 82580 (Quad Fiber)"),
357 	PVID(0x8086, E1000_DEV_ID_DH89XXCC_SERDES,
358 	    "Intel(R) DH89XXCC (SERDES)"),
359 	PVID(0x8086, E1000_DEV_ID_DH89XXCC_SGMII,
360 	    "Intel(R) I347-AT4 DH89XXCC"),
361 	PVID(0x8086, E1000_DEV_ID_DH89XXCC_SFP, "Intel(R) DH89XXCC (SFP)"),
362 	PVID(0x8086, E1000_DEV_ID_DH89XXCC_BACKPLANE,
363 	    "Intel(R) DH89XXCC (Backplane)"),
364 	PVID(0x8086, E1000_DEV_ID_I350_COPPER, "Intel(R) I350 (Copper)"),
365 	PVID(0x8086, E1000_DEV_ID_I350_FIBER, "Intel(R) I350 (Fiber)"),
366 	PVID(0x8086, E1000_DEV_ID_I350_SERDES, "Intel(R) I350 (SERDES)"),
367 	PVID(0x8086, E1000_DEV_ID_I350_SGMII, "Intel(R) I350 (SGMII)"),
368 	PVID(0x8086, E1000_DEV_ID_I350_VF, "Intel(R) I350 Virtual Function"),
369 	PVID(0x8086, E1000_DEV_ID_I210_COPPER, "Intel(R) I210 (Copper)"),
370 	PVID(0x8086, E1000_DEV_ID_I210_COPPER_IT,
371 	    "Intel(R) I210 IT (Copper)"),
372 	PVID(0x8086, E1000_DEV_ID_I210_COPPER_OEM1, "Intel(R) I210 (OEM)"),
373 	PVID(0x8086, E1000_DEV_ID_I210_COPPER_FLASHLESS,
374 	    "Intel(R) I210 Flashless (Copper)"),
375 	PVID(0x8086, E1000_DEV_ID_I210_SERDES_FLASHLESS,
376 	    "Intel(R) I210 Flashless (SERDES)"),
377 	PVID(0x8086, E1000_DEV_ID_I210_SGMII_FLASHLESS,
378 	    "Intel(R) I210 Flashless (SGMII)"),
379 	PVID(0x8086, E1000_DEV_ID_I210_FIBER, "Intel(R) I210 (Fiber)"),
380 	PVID(0x8086, E1000_DEV_ID_I210_SERDES, "Intel(R) I210 (SERDES)"),
381 	PVID(0x8086, E1000_DEV_ID_I210_SGMII, "Intel(R) I210 (SGMII)"),
382 	PVID(0x8086, E1000_DEV_ID_I211_COPPER, "Intel(R) I211 (Copper)"),
383 	PVID(0x8086, E1000_DEV_ID_I354_BACKPLANE_1GBPS,
384 	    "Intel(R) I354 (1.0 GbE Backplane)"),
385 	PVID(0x8086, E1000_DEV_ID_I354_BACKPLANE_2_5GBPS,
386 	    "Intel(R) I354 (2.5 GbE Backplane)"),
387 	PVID(0x8086, E1000_DEV_ID_I354_SGMII, "Intel(R) I354 (SGMII)"),
388 	/* required last entry */
389 	PVID_END
390 };
391 
392 /*********************************************************************
393  *  Function prototypes
394  *********************************************************************/
395 static void	*em_register(device_t);
396 static void	*igb_register(device_t);
397 static int	em_if_attach_pre(if_ctx_t);
398 static int	em_if_attach_post(if_ctx_t);
399 static int	em_if_detach(if_ctx_t);
400 static int	em_if_shutdown(if_ctx_t);
401 static int	em_if_suspend(if_ctx_t);
402 static int	em_if_resume(if_ctx_t);
403 
404 static int	em_if_tx_queues_alloc(if_ctx_t, caddr_t *, uint64_t *, int,
405     int);
406 static int	em_if_rx_queues_alloc(if_ctx_t, caddr_t *, uint64_t *, int,
407     int);
408 static void	em_if_queues_free(if_ctx_t);
409 
410 static uint64_t	em_if_get_vf_counter(if_ctx_t, ift_counter);
411 static uint64_t	em_if_get_counter(if_ctx_t, ift_counter);
412 static void	em_if_init(if_ctx_t);
413 static void	em_if_stop(if_ctx_t);
414 static void	em_if_media_status(if_ctx_t, struct ifmediareq *);
415 static int	em_if_media_change(if_ctx_t);
416 static int	em_if_mtu_set(if_ctx_t, uint32_t);
417 static void	em_if_timer(if_ctx_t, uint16_t);
418 static void	em_if_vlan_register(if_ctx_t, u16);
419 static void	em_if_vlan_unregister(if_ctx_t, u16);
420 static void	em_if_watchdog_reset(if_ctx_t);
421 static bool	em_if_needs_restart(if_ctx_t, enum iflib_restart_event);
422 
423 static void	em_identify_hardware(if_ctx_t);
424 static int	em_allocate_pci_resources(if_ctx_t);
425 static void	em_free_pci_resources(if_ctx_t);
426 static void	em_reset(if_ctx_t);
427 static int	em_setup_interface(if_ctx_t);
428 static int	em_setup_msix(if_ctx_t);
429 
430 static void	em_initialize_transmit_unit(if_ctx_t);
431 static void	em_initialize_receive_unit(if_ctx_t);
432 
433 static void	em_if_intr_enable(if_ctx_t);
434 static void	em_if_intr_disable(if_ctx_t);
435 static void	igb_if_intr_enable(if_ctx_t);
436 static void	igb_if_intr_disable(if_ctx_t);
437 static int	em_if_rx_queue_intr_enable(if_ctx_t, uint16_t);
438 static int	em_if_tx_queue_intr_enable(if_ctx_t, uint16_t);
439 static int	igb_if_rx_queue_intr_enable(if_ctx_t, uint16_t);
440 static int	igb_if_tx_queue_intr_enable(if_ctx_t, uint16_t);
441 static void	em_if_multi_set(if_ctx_t);
442 static void	em_if_update_admin_status(if_ctx_t);
443 static void	em_if_debug(if_ctx_t);
444 static void	em_update_vf_stats_counters(struct e1000_softc *);
445 static void	em_update_stats_counters(struct e1000_softc *);
446 static void	em_add_hw_stats(struct e1000_softc *);
447 static int	em_if_set_promisc(if_ctx_t, int);
448 static bool	em_if_vlan_filter_capable(if_ctx_t);
449 static bool	em_if_vlan_filter_used(if_ctx_t);
450 static void	em_if_vlan_filter_enable(struct e1000_softc *);
451 static void	em_if_vlan_filter_disable(struct e1000_softc *);
452 static void	em_if_vlan_filter_write(struct e1000_softc *);
453 static void	em_setup_vlan_hw_support(if_ctx_t ctx);
454 static int	em_sysctl_nvm_info(SYSCTL_HANDLER_ARGS);
455 static void	em_print_nvm_info(struct e1000_softc *);
456 static void	em_fw_version_locked(if_ctx_t);
457 static void	em_sbuf_fw_version(struct e1000_fw_version *, struct sbuf *);
458 static void	em_print_fw_version(struct e1000_softc *);
459 static int	em_sysctl_print_fw_version(SYSCTL_HANDLER_ARGS);
460 static int	em_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
461 static int	em_get_rs(SYSCTL_HANDLER_ARGS);
462 static void	em_print_debug_info(struct e1000_softc *);
463 static int 	em_is_valid_ether_addr(u8 *);
464 static void	em_newitr(struct e1000_softc *, struct em_rx_queue *,
465     struct tx_ring *, struct rx_ring *);
466 static bool	em_automask_tso(if_ctx_t);
467 static int	em_sysctl_tso_tcp_flags_mask(SYSCTL_HANDLER_ARGS);
468 static int	em_sysctl_int_delay(SYSCTL_HANDLER_ARGS);
469 static void	em_add_int_delay_sysctl(struct e1000_softc *, const char *,
470     const char *, struct em_int_delay_info *, int, int);
471 /* Management and WOL Support */
472 static void	em_init_manageability(struct e1000_softc *);
473 static void	em_release_manageability(struct e1000_softc *);
474 static void	em_get_hw_control(struct e1000_softc *);
475 static void	em_release_hw_control(struct e1000_softc *);
476 static void	em_get_wakeup(if_ctx_t);
477 static void	em_enable_wakeup(if_ctx_t);
478 static int	em_enable_phy_wakeup(struct e1000_softc *);
479 static void	em_disable_aspm(struct e1000_softc *);
480 
481 int		em_intr(void *);
482 
483 /* MSI-X handlers */
484 static int	em_if_msix_intr_assign(if_ctx_t, int);
485 static int	em_msix_link(void *);
486 static void	em_handle_link(void *);
487 
488 static void	em_enable_vectors_82574(if_ctx_t);
489 
490 static int	em_set_flowcntl(SYSCTL_HANDLER_ARGS);
491 static int	em_sysctl_eee(SYSCTL_HANDLER_ARGS);
492 static int	igb_sysctl_dmac(SYSCTL_HANDLER_ARGS);
493 static void	em_if_led_func(if_ctx_t, int);
494 
495 static int	em_get_regs(SYSCTL_HANDLER_ARGS);
496 
497 static void	lem_smartspeed(struct e1000_softc *);
498 static void	igb_configure_queues(struct e1000_softc *);
499 static void	em_flush_desc_rings(struct e1000_softc *);
500 
501 
502 /*********************************************************************
503  *  FreeBSD Device Interface Entry Points
504  *********************************************************************/
505 static device_method_t em_methods[] = {
506 	/* Device interface */
507 	DEVMETHOD(device_register, em_register),
508 	DEVMETHOD(device_probe, iflib_device_probe),
509 	DEVMETHOD(device_attach, iflib_device_attach),
510 	DEVMETHOD(device_detach, iflib_device_detach),
511 	DEVMETHOD(device_shutdown, iflib_device_shutdown),
512 	DEVMETHOD(device_suspend, iflib_device_suspend),
513 	DEVMETHOD(device_resume, iflib_device_resume),
514 	DEVMETHOD_END
515 };
516 
517 static device_method_t igb_methods[] = {
518 	/* Device interface */
519 	DEVMETHOD(device_register, igb_register),
520 	DEVMETHOD(device_probe, iflib_device_probe),
521 	DEVMETHOD(device_attach, iflib_device_attach),
522 	DEVMETHOD(device_detach, iflib_device_detach),
523 	DEVMETHOD(device_shutdown, iflib_device_shutdown),
524 	DEVMETHOD(device_suspend, iflib_device_suspend),
525 	DEVMETHOD(device_resume, iflib_device_resume),
526 	DEVMETHOD_END
527 };
528 
529 
530 static driver_t em_driver = {
531 	"em", em_methods, sizeof(struct e1000_softc),
532 };
533 
534 DRIVER_MODULE(em, pci, em_driver, 0, 0);
535 
536 MODULE_DEPEND(em, pci, 1, 1, 1);
537 MODULE_DEPEND(em, ether, 1, 1, 1);
538 MODULE_DEPEND(em, iflib, 1, 1, 1);
539 
540 IFLIB_PNP_INFO(pci, em, em_vendor_info_array);
541 
542 static driver_t igb_driver = {
543 	"igb", igb_methods, sizeof(struct e1000_softc),
544 };
545 
546 DRIVER_MODULE(igb, pci, igb_driver, 0, 0);
547 
548 MODULE_DEPEND(igb, pci, 1, 1, 1);
549 MODULE_DEPEND(igb, ether, 1, 1, 1);
550 MODULE_DEPEND(igb, iflib, 1, 1, 1);
551 
552 IFLIB_PNP_INFO(pci, igb, igb_vendor_info_array);
553 
554 static device_method_t em_if_methods[] = {
555 	DEVMETHOD(ifdi_attach_pre, em_if_attach_pre),
556 	DEVMETHOD(ifdi_attach_post, em_if_attach_post),
557 	DEVMETHOD(ifdi_detach, em_if_detach),
558 	DEVMETHOD(ifdi_shutdown, em_if_shutdown),
559 	DEVMETHOD(ifdi_suspend, em_if_suspend),
560 	DEVMETHOD(ifdi_resume, em_if_resume),
561 	DEVMETHOD(ifdi_init, em_if_init),
562 	DEVMETHOD(ifdi_stop, em_if_stop),
563 	DEVMETHOD(ifdi_msix_intr_assign, em_if_msix_intr_assign),
564 	DEVMETHOD(ifdi_intr_enable, em_if_intr_enable),
565 	DEVMETHOD(ifdi_intr_disable, em_if_intr_disable),
566 	DEVMETHOD(ifdi_tx_queues_alloc, em_if_tx_queues_alloc),
567 	DEVMETHOD(ifdi_rx_queues_alloc, em_if_rx_queues_alloc),
568 	DEVMETHOD(ifdi_queues_free, em_if_queues_free),
569 	DEVMETHOD(ifdi_update_admin_status, em_if_update_admin_status),
570 	DEVMETHOD(ifdi_multi_set, em_if_multi_set),
571 	DEVMETHOD(ifdi_media_status, em_if_media_status),
572 	DEVMETHOD(ifdi_media_change, em_if_media_change),
573 	DEVMETHOD(ifdi_mtu_set, em_if_mtu_set),
574 	DEVMETHOD(ifdi_promisc_set, em_if_set_promisc),
575 	DEVMETHOD(ifdi_timer, em_if_timer),
576 	DEVMETHOD(ifdi_watchdog_reset, em_if_watchdog_reset),
577 	DEVMETHOD(ifdi_vlan_register, em_if_vlan_register),
578 	DEVMETHOD(ifdi_vlan_unregister, em_if_vlan_unregister),
579 	DEVMETHOD(ifdi_get_counter, em_if_get_counter),
580 	DEVMETHOD(ifdi_led_func, em_if_led_func),
581 	DEVMETHOD(ifdi_rx_queue_intr_enable, em_if_rx_queue_intr_enable),
582 	DEVMETHOD(ifdi_tx_queue_intr_enable, em_if_tx_queue_intr_enable),
583 	DEVMETHOD(ifdi_debug, em_if_debug),
584 	DEVMETHOD(ifdi_needs_restart, em_if_needs_restart),
585 	DEVMETHOD_END
586 };
587 
588 static driver_t em_if_driver = {
589 	"em_if", em_if_methods, sizeof(struct e1000_softc)
590 };
591 
592 static device_method_t igb_if_methods[] = {
593 	DEVMETHOD(ifdi_attach_pre, em_if_attach_pre),
594 	DEVMETHOD(ifdi_attach_post, em_if_attach_post),
595 	DEVMETHOD(ifdi_detach, em_if_detach),
596 	DEVMETHOD(ifdi_shutdown, em_if_shutdown),
597 	DEVMETHOD(ifdi_suspend, em_if_suspend),
598 	DEVMETHOD(ifdi_resume, em_if_resume),
599 	DEVMETHOD(ifdi_init, em_if_init),
600 	DEVMETHOD(ifdi_stop, em_if_stop),
601 	DEVMETHOD(ifdi_msix_intr_assign, em_if_msix_intr_assign),
602 	DEVMETHOD(ifdi_intr_enable, igb_if_intr_enable),
603 	DEVMETHOD(ifdi_intr_disable, igb_if_intr_disable),
604 	DEVMETHOD(ifdi_tx_queues_alloc, em_if_tx_queues_alloc),
605 	DEVMETHOD(ifdi_rx_queues_alloc, em_if_rx_queues_alloc),
606 	DEVMETHOD(ifdi_queues_free, em_if_queues_free),
607 	DEVMETHOD(ifdi_update_admin_status, em_if_update_admin_status),
608 	DEVMETHOD(ifdi_multi_set, em_if_multi_set),
609 	DEVMETHOD(ifdi_media_status, em_if_media_status),
610 	DEVMETHOD(ifdi_media_change, em_if_media_change),
611 	DEVMETHOD(ifdi_mtu_set, em_if_mtu_set),
612 	DEVMETHOD(ifdi_promisc_set, em_if_set_promisc),
613 	DEVMETHOD(ifdi_timer, em_if_timer),
614 	DEVMETHOD(ifdi_watchdog_reset, em_if_watchdog_reset),
615 	DEVMETHOD(ifdi_vlan_register, em_if_vlan_register),
616 	DEVMETHOD(ifdi_vlan_unregister, em_if_vlan_unregister),
617 	DEVMETHOD(ifdi_get_counter, em_if_get_counter),
618 	DEVMETHOD(ifdi_led_func, em_if_led_func),
619 	DEVMETHOD(ifdi_rx_queue_intr_enable, igb_if_rx_queue_intr_enable),
620 	DEVMETHOD(ifdi_tx_queue_intr_enable, igb_if_tx_queue_intr_enable),
621 	DEVMETHOD(ifdi_debug, em_if_debug),
622 	DEVMETHOD(ifdi_needs_restart, em_if_needs_restart),
623 	DEVMETHOD_END
624 };
625 
626 static driver_t igb_if_driver = {
627 	"igb_if", igb_if_methods, sizeof(struct e1000_softc)
628 };
629 
630 /*********************************************************************
631  *  Tunable default values.
632  *********************************************************************/
633 
634 #define EM_TICKS_TO_USECS(ticks)	((1024 * (ticks) + 500) / 1000)
635 #define EM_USECS_TO_TICKS(usecs)	((1000 * (usecs) + 512) / 1024)
636 
637 /* Allow common code without TSO */
638 #ifndef CSUM_TSO
639 #define CSUM_TSO	0
640 #endif
641 
642 static SYSCTL_NODE(_hw, OID_AUTO, em, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
643     "EM driver parameters");
644 
645 static int em_disable_crc_stripping = 0;
646 SYSCTL_INT(_hw_em, OID_AUTO, disable_crc_stripping, CTLFLAG_RDTUN,
647     &em_disable_crc_stripping, 0, "Disable CRC Stripping");
648 
649 static int em_tx_int_delay_dflt = EM_TICKS_TO_USECS(EM_TIDV);
650 static int em_rx_int_delay_dflt = EM_TICKS_TO_USECS(EM_RDTR);
651 SYSCTL_INT(_hw_em, OID_AUTO, tx_int_delay, CTLFLAG_RDTUN,
652     &em_tx_int_delay_dflt, 0, "Default transmit interrupt delay in usecs");
653 SYSCTL_INT(_hw_em, OID_AUTO, rx_int_delay, CTLFLAG_RDTUN,
654     &em_rx_int_delay_dflt, 0, "Default receive interrupt delay in usecs");
655 
656 static int em_tx_abs_int_delay_dflt = EM_TICKS_TO_USECS(EM_TADV);
657 static int em_rx_abs_int_delay_dflt = EM_TICKS_TO_USECS(EM_RADV);
658 SYSCTL_INT(_hw_em, OID_AUTO, tx_abs_int_delay, CTLFLAG_RDTUN,
659     &em_tx_abs_int_delay_dflt, 0,
660     "Default transmit interrupt delay limit in usecs");
661 SYSCTL_INT(_hw_em, OID_AUTO, rx_abs_int_delay, CTLFLAG_RDTUN,
662     &em_rx_abs_int_delay_dflt, 0,
663     "Default receive interrupt delay limit in usecs");
664 
665 static int em_smart_pwr_down = false;
666 SYSCTL_INT(_hw_em, OID_AUTO, smart_pwr_down, CTLFLAG_RDTUN,
667     &em_smart_pwr_down,
668     0, "Set to true to leave smart power down enabled on newer adapters");
669 
670 static bool em_unsupported_tso = false;
671 SYSCTL_BOOL(_hw_em, OID_AUTO, unsupported_tso, CTLFLAG_RDTUN,
672     &em_unsupported_tso, 0, "Allow unsupported em(4) TSO configurations");
673 
674 /* Controls whether promiscuous also shows bad packets */
675 static int em_debug_sbp = false;
676 SYSCTL_INT(_hw_em, OID_AUTO, sbp, CTLFLAG_RDTUN, &em_debug_sbp, 0,
677     "Show bad packets in promiscuous mode");
678 
679 /* Energy efficient ethernet - default to OFF */
680 static int eee_setting = 1;
681 SYSCTL_INT(_hw_em, OID_AUTO, eee_setting, CTLFLAG_RDTUN, &eee_setting, 0,
682     "Enable Energy Efficient Ethernet");
683 
684 /*
685  * AIM: Adaptive Interrupt Moderation
686  * which means that the interrupt rate is varied over time based on the
687  * traffic for that interrupt vector
688  */
689 static int em_enable_aim = 1;
690 SYSCTL_INT(_hw_em, OID_AUTO, enable_aim, CTLFLAG_RWTUN, &em_enable_aim,
691     0, "Enable adaptive interrupt moderation (1=normal, 2=lowlatency)");
692 
693 /*
694 ** Tuneable Interrupt rate
695 */
696 static int em_max_interrupt_rate = EM_INTS_DEFAULT;
697 SYSCTL_INT(_hw_em, OID_AUTO, max_interrupt_rate, CTLFLAG_RDTUN,
698     &em_max_interrupt_rate, 0, "Maximum interrupts per second");
699 
700 /* Global used in WOL setup with multiport cards */
701 static int global_quad_port_a = 0;
702 
703 extern struct if_txrx igb_txrx;
704 extern struct if_txrx em_txrx;
705 extern struct if_txrx lem_txrx;
706 
707 static struct if_shared_ctx em_sctx_init = {
708 	.isc_magic = IFLIB_MAGIC,
709 	.isc_q_align = PAGE_SIZE,
710 	.isc_tx_maxsize = EM_TSO_SIZE + sizeof(struct ether_vlan_header),
711 	.isc_tx_maxsegsize = PAGE_SIZE,
712 	.isc_tso_maxsize = EM_TSO_SIZE + sizeof(struct ether_vlan_header),
713 	.isc_tso_maxsegsize = EM_TSO_SEG_SIZE,
714 	.isc_rx_maxsize = MJUM9BYTES,
715 	.isc_rx_nsegments = 1,
716 	.isc_rx_maxsegsize = MJUM9BYTES,
717 	.isc_nfl = 1,
718 	.isc_nrxqs = 1,
719 	.isc_ntxqs = 1,
720 	.isc_admin_intrcnt = 1,
721 	.isc_vendor_info = em_vendor_info_array,
722 	.isc_driver_version = em_driver_version,
723 	.isc_driver = &em_if_driver,
724 	.isc_flags =
725 	    IFLIB_NEED_SCRATCH | IFLIB_TSO_INIT_IP | IFLIB_NEED_ZERO_CSUM,
726 
727 	.isc_nrxd_min = {EM_MIN_RXD},
728 	.isc_ntxd_min = {EM_MIN_TXD},
729 	.isc_nrxd_max = {EM_MAX_RXD},
730 	.isc_ntxd_max = {EM_MAX_TXD},
731 	.isc_nrxd_default = {EM_DEFAULT_RXD},
732 	.isc_ntxd_default = {EM_DEFAULT_TXD},
733 };
734 
735 static struct if_shared_ctx igb_sctx_init = {
736 	.isc_magic = IFLIB_MAGIC,
737 	.isc_q_align = PAGE_SIZE,
738 	.isc_tx_maxsize = EM_TSO_SIZE + sizeof(struct ether_vlan_header),
739 	.isc_tx_maxsegsize = PAGE_SIZE,
740 	.isc_tso_maxsize = EM_TSO_SIZE + sizeof(struct ether_vlan_header),
741 	.isc_tso_maxsegsize = EM_TSO_SEG_SIZE,
742 	.isc_rx_maxsize = MJUM9BYTES,
743 	.isc_rx_nsegments = 1,
744 	.isc_rx_maxsegsize = MJUM9BYTES,
745 	.isc_nfl = 1,
746 	.isc_nrxqs = 1,
747 	.isc_ntxqs = 1,
748 	.isc_admin_intrcnt = 1,
749 	.isc_vendor_info = igb_vendor_info_array,
750 	.isc_driver_version = igb_driver_version,
751 	.isc_driver = &igb_if_driver,
752 	.isc_flags =
753 	    IFLIB_NEED_SCRATCH | IFLIB_TSO_INIT_IP | IFLIB_NEED_ZERO_CSUM,
754 
755 	.isc_nrxd_min = {EM_MIN_RXD},
756 	.isc_ntxd_min = {EM_MIN_TXD},
757 	.isc_nrxd_max = {IGB_MAX_RXD},
758 	.isc_ntxd_max = {IGB_MAX_TXD},
759 	.isc_nrxd_default = {EM_DEFAULT_RXD},
760 	.isc_ntxd_default = {EM_DEFAULT_TXD},
761 };
762 
763 /*****************************************************************
764  *
765  * Dump Registers
766  *
767  ****************************************************************/
768 #define IGB_REGS_LEN 739
769 
em_get_regs(SYSCTL_HANDLER_ARGS)770 static int em_get_regs(SYSCTL_HANDLER_ARGS)
771 {
772 	struct e1000_softc *sc = (struct e1000_softc *)arg1;
773 	struct e1000_hw *hw = &sc->hw;
774 	struct sbuf *sb;
775 	u32 *regs_buff;
776 	int rc;
777 
778 	regs_buff = malloc(sizeof(u32) * IGB_REGS_LEN, M_DEVBUF, M_WAITOK);
779 	memset(regs_buff, 0, IGB_REGS_LEN * sizeof(u32));
780 
781 	rc = sysctl_wire_old_buffer(req, 0);
782 	MPASS(rc == 0);
783 	if (rc != 0) {
784 		free(regs_buff, M_DEVBUF);
785 		return (rc);
786 	}
787 
788 	sb = sbuf_new_for_sysctl(NULL, NULL, 32*400, req);
789 	MPASS(sb != NULL);
790 	if (sb == NULL) {
791 		free(regs_buff, M_DEVBUF);
792 		return (ENOMEM);
793 	}
794 
795 	/* General Registers */
796 	regs_buff[0] = E1000_READ_REG(hw, E1000_CTRL);
797 	regs_buff[1] = E1000_READ_REG(hw, E1000_STATUS);
798 	regs_buff[2] = E1000_READ_REG(hw, E1000_CTRL_EXT);
799 	regs_buff[3] = E1000_READ_REG(hw, E1000_ICR);
800 	regs_buff[4] = E1000_READ_REG(hw, E1000_RCTL);
801 	regs_buff[5] = E1000_READ_REG(hw, E1000_RDLEN(0));
802 	regs_buff[6] = E1000_READ_REG(hw, E1000_RDH(0));
803 	regs_buff[7] = E1000_READ_REG(hw, E1000_RDT(0));
804 	regs_buff[8] = E1000_READ_REG(hw, E1000_RXDCTL(0));
805 	regs_buff[9] = E1000_READ_REG(hw, E1000_RDBAL(0));
806 	regs_buff[10] = E1000_READ_REG(hw, E1000_RDBAH(0));
807 	regs_buff[11] = E1000_READ_REG(hw, E1000_TCTL);
808 	regs_buff[12] = E1000_READ_REG(hw, E1000_TDBAL(0));
809 	regs_buff[13] = E1000_READ_REG(hw, E1000_TDBAH(0));
810 	regs_buff[14] = E1000_READ_REG(hw, E1000_TDLEN(0));
811 	regs_buff[15] = E1000_READ_REG(hw, E1000_TDH(0));
812 	regs_buff[16] = E1000_READ_REG(hw, E1000_TDT(0));
813 	regs_buff[17] = E1000_READ_REG(hw, E1000_TXDCTL(0));
814 	regs_buff[18] = E1000_READ_REG(hw, E1000_TDFH);
815 	regs_buff[19] = E1000_READ_REG(hw, E1000_TDFT);
816 	regs_buff[20] = E1000_READ_REG(hw, E1000_TDFHS);
817 	regs_buff[21] = E1000_READ_REG(hw, E1000_TDFPC);
818 
819 	sbuf_printf(sb, "General Registers\n");
820 	sbuf_printf(sb, "\tCTRL\t %08x\n", regs_buff[0]);
821 	sbuf_printf(sb, "\tSTATUS\t %08x\n", regs_buff[1]);
822 	sbuf_printf(sb, "\tCTRL_EXT\t %08x\n\n", regs_buff[2]);
823 
824 	sbuf_printf(sb, "Interrupt Registers\n");
825 	sbuf_printf(sb, "\tICR\t %08x\n\n", regs_buff[3]);
826 
827 	sbuf_printf(sb, "RX Registers\n");
828 	sbuf_printf(sb, "\tRCTL\t %08x\n", regs_buff[4]);
829 	sbuf_printf(sb, "\tRDLEN\t %08x\n", regs_buff[5]);
830 	sbuf_printf(sb, "\tRDH\t %08x\n", regs_buff[6]);
831 	sbuf_printf(sb, "\tRDT\t %08x\n", regs_buff[7]);
832 	sbuf_printf(sb, "\tRXDCTL\t %08x\n", regs_buff[8]);
833 	sbuf_printf(sb, "\tRDBAL\t %08x\n", regs_buff[9]);
834 	sbuf_printf(sb, "\tRDBAH\t %08x\n\n", regs_buff[10]);
835 
836 	sbuf_printf(sb, "TX Registers\n");
837 	sbuf_printf(sb, "\tTCTL\t %08x\n", regs_buff[11]);
838 	sbuf_printf(sb, "\tTDBAL\t %08x\n", regs_buff[12]);
839 	sbuf_printf(sb, "\tTDBAH\t %08x\n", regs_buff[13]);
840 	sbuf_printf(sb, "\tTDLEN\t %08x\n", regs_buff[14]);
841 	sbuf_printf(sb, "\tTDH\t %08x\n", regs_buff[15]);
842 	sbuf_printf(sb, "\tTDT\t %08x\n", regs_buff[16]);
843 	sbuf_printf(sb, "\tTXDCTL\t %08x\n", regs_buff[17]);
844 	sbuf_printf(sb, "\tTDFH\t %08x\n", regs_buff[18]);
845 	sbuf_printf(sb, "\tTDFT\t %08x\n", regs_buff[19]);
846 	sbuf_printf(sb, "\tTDFHS\t %08x\n", regs_buff[20]);
847 	sbuf_printf(sb, "\tTDFPC\t %08x\n\n", regs_buff[21]);
848 
849 	free(regs_buff, M_DEVBUF);
850 
851 #ifdef DUMP_DESCS
852 	{
853 		if_softc_ctx_t scctx = sc->shared;
854 		struct rx_ring *rxr = &rx_que->rxr;
855 		struct tx_ring *txr = &tx_que->txr;
856 		int ntxd = scctx->isc_ntxd[0];
857 		int nrxd = scctx->isc_nrxd[0];
858 		int j;
859 
860 	for (j = 0; j < nrxd; j++) {
861 		u32 staterr = le32toh(rxr->rx_base[j].wb.upper.status_error);
862 		u32 length =  le32toh(rxr->rx_base[j].wb.upper.length);
863 		sbuf_printf(sb, "\tReceive Descriptor Address %d: %08"
864 		    PRIx64 "  Error:%d  Length:%d\n",
865 		    j, rxr->rx_base[j].read.buffer_addr, staterr, length);
866 	}
867 
868 	for (j = 0; j < min(ntxd, 256); j++) {
869 		unsigned int *ptr = (unsigned int *)&txr->tx_base[j];
870 
871 		sbuf_printf(sb,
872 		    "\tTXD[%03d] [0]: %08x [1]: %08x [2]: %08x [3]: %08x"
873 		    "  eop: %d DD=%d\n",
874 		    j, ptr[0], ptr[1], ptr[2], ptr[3], buf->eop,
875 		    buf->eop != -1 ?
876 		    txr->tx_base[buf->eop].upper.fields.status &
877 		    E1000_TXD_STAT_DD : 0);
878 
879 	}
880 	}
881 #endif
882 
883 	rc = sbuf_finish(sb);
884 	sbuf_delete(sb);
885 	return(rc);
886 }
887 
888 static void *
em_register(device_t dev)889 em_register(device_t dev)
890 {
891 	return (&em_sctx_init);
892 }
893 
894 static void *
igb_register(device_t dev)895 igb_register(device_t dev)
896 {
897 	return (&igb_sctx_init);
898 }
899 
900 static int
em_set_num_queues(if_ctx_t ctx)901 em_set_num_queues(if_ctx_t ctx)
902 {
903 	struct e1000_softc *sc = iflib_get_softc(ctx);
904 	int maxqueues;
905 
906 	/* Sanity check based on HW */
907 	switch (sc->hw.mac.type) {
908 	case e1000_82576:
909 	case e1000_82580:
910 	case e1000_i350:
911 	case e1000_i354:
912 		maxqueues = 8;
913 		break;
914 	case e1000_i210:
915 	case e1000_82575:
916 		maxqueues = 4;
917 		break;
918 	case e1000_i211:
919 	case e1000_82574:
920 		maxqueues = 2;
921 		break;
922 	default:
923 		maxqueues = 1;
924 		break;
925 	}
926 
927 	return (maxqueues);
928 }
929 
930 #define LEM_CAPS \
931     IFCAP_HWCSUM | IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING | \
932     IFCAP_VLAN_HWCSUM | IFCAP_WOL | IFCAP_VLAN_HWFILTER | IFCAP_TSO4 | \
933     IFCAP_LRO | IFCAP_VLAN_HWTSO | IFCAP_JUMBO_MTU | IFCAP_HWCSUM_IPV6
934 
935 #define EM_CAPS \
936     IFCAP_HWCSUM | IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING | \
937     IFCAP_VLAN_HWCSUM | IFCAP_WOL | IFCAP_VLAN_HWFILTER | IFCAP_TSO4 | \
938     IFCAP_LRO | IFCAP_VLAN_HWTSO | IFCAP_JUMBO_MTU | IFCAP_HWCSUM_IPV6 | \
939     IFCAP_TSO6
940 
941 #define IGB_CAPS \
942     IFCAP_HWCSUM | IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING | \
943     IFCAP_VLAN_HWCSUM | IFCAP_WOL | IFCAP_VLAN_HWFILTER | IFCAP_TSO4 | \
944     IFCAP_LRO | IFCAP_VLAN_HWTSO | IFCAP_JUMBO_MTU | IFCAP_HWCSUM_IPV6 | \
945     IFCAP_TSO6
946 
947 /*********************************************************************
948  *  Device initialization routine
949  *
950  *  The attach entry point is called when the driver is being loaded.
951  *  This routine identifies the type of hardware, allocates all resources
952  *  and initializes the hardware.
953  *
954  *  return 0 on success, positive on failure
955  *********************************************************************/
956 static int
em_if_attach_pre(if_ctx_t ctx)957 em_if_attach_pre(if_ctx_t ctx)
958 {
959 	struct e1000_softc *sc;
960 	if_softc_ctx_t scctx;
961 	device_t dev;
962 	struct e1000_hw *hw;
963 	struct sysctl_oid_list *child;
964 	struct sysctl_ctx_list *ctx_list;
965 	int error = 0;
966 
967 	INIT_DEBUGOUT("em_if_attach_pre: begin");
968 	dev = iflib_get_dev(ctx);
969 	sc = iflib_get_softc(ctx);
970 
971 	sc->ctx = sc->osdep.ctx = ctx;
972 	sc->dev = sc->osdep.dev = dev;
973 	scctx = sc->shared = iflib_get_softc_ctx(ctx);
974 	sc->media = iflib_get_media(ctx);
975 	hw = &sc->hw;
976 
977 	/* Determine hardware and mac info */
978 	em_identify_hardware(ctx);
979 
980 	/* SYSCTL stuff */
981 	ctx_list = device_get_sysctl_ctx(dev);
982 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
983 
984 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "nvm",
985 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0,
986 	    em_sysctl_nvm_info, "I", "NVM Information");
987 
988 	sc->enable_aim = em_enable_aim;
989 	SYSCTL_ADD_INT(ctx_list, child, OID_AUTO, "enable_aim",
990 	    CTLFLAG_RW, &sc->enable_aim, 0,
991 	    "Interrupt Moderation (1=normal, 2=lowlatency)");
992 
993 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "fw_version",
994 	    CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
995 	    em_sysctl_print_fw_version, "A",
996 	    "Prints FW/NVM Versions");
997 
998 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "debug",
999 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0,
1000 	    em_sysctl_debug_info, "I", "Debug Information");
1001 
1002 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "fc",
1003 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
1004 	    em_set_flowcntl, "I", "Flow Control");
1005 
1006 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "reg_dump",
1007 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
1008 	    em_get_regs, "A", "Dump Registers");
1009 
1010 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "rs_dump",
1011 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
1012 	    em_get_rs, "I", "Dump RS indexes");
1013 
1014 	if (hw->mac.type >= e1000_i350) {
1015 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "dmac",
1016 		    CTLTYPE_INT | CTLFLAG_RW, sc, 0,
1017 		    igb_sysctl_dmac, "I", "DMA Coalesce");
1018 	}
1019 
1020 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO,
1021 	    "tso_tcp_flags_mask_first_segment",
1022 	    CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1023 	    sc, 0, em_sysctl_tso_tcp_flags_mask, "IU",
1024 	    "TSO TCP flags mask for first segment");
1025 
1026 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO,
1027 	    "tso_tcp_flags_mask_middle_segment",
1028 	    CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1029 	    sc, 1, em_sysctl_tso_tcp_flags_mask, "IU",
1030 	    "TSO TCP flags mask for middle segment");
1031 
1032 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO,
1033 	    "tso_tcp_flags_mask_last_segment",
1034 	    CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1035 	    sc, 2, em_sysctl_tso_tcp_flags_mask, "IU",
1036 	    "TSO TCP flags mask for last segment");
1037 
1038 	scctx->isc_tx_nsegments = EM_MAX_SCATTER;
1039 	scctx->isc_nrxqsets_max =
1040 	    scctx->isc_ntxqsets_max = em_set_num_queues(ctx);
1041 	if (bootverbose)
1042 		device_printf(dev, "attach_pre capping queues at %d\n",
1043 		    scctx->isc_ntxqsets_max);
1044 
1045 	if (hw->mac.type >= igb_mac_min) {
1046 		scctx->isc_txqsizes[0] = roundup2(scctx->isc_ntxd[0] *
1047 		    sizeof(union e1000_adv_tx_desc), EM_DBA_ALIGN);
1048 		scctx->isc_rxqsizes[0] = roundup2(scctx->isc_nrxd[0] *
1049 		    sizeof(union e1000_adv_rx_desc), EM_DBA_ALIGN);
1050 		scctx->isc_txd_size[0] = sizeof(union e1000_adv_tx_desc);
1051 		scctx->isc_rxd_size[0] = sizeof(union e1000_adv_rx_desc);
1052 		scctx->isc_txrx = &igb_txrx;
1053 		scctx->isc_tx_tso_segments_max = EM_MAX_SCATTER;
1054 		scctx->isc_tx_tso_size_max = EM_TSO_SIZE;
1055 		scctx->isc_tx_tso_segsize_max = EM_TSO_SEG_SIZE;
1056 		scctx->isc_capabilities = scctx->isc_capenable = IGB_CAPS;
1057 		scctx->isc_tx_csum_flags = CSUM_TCP | CSUM_UDP | CSUM_TSO |
1058 		     CSUM_IP6_TCP | CSUM_IP6_UDP;
1059 		if (hw->mac.type != e1000_82575)
1060 			scctx->isc_tx_csum_flags |= CSUM_SCTP | CSUM_IP6_SCTP;
1061 		/*
1062 		** Some new devices, as with ixgbe, now may
1063 		** use a different BAR, so we need to keep
1064 		** track of which is used.
1065 		*/
1066 		scctx->isc_msix_bar = pci_msix_table_bar(dev);
1067 	} else if (hw->mac.type >= em_mac_min) {
1068 		scctx->isc_txqsizes[0] = roundup2(scctx->isc_ntxd[0] *
1069 		    sizeof(struct e1000_tx_desc), EM_DBA_ALIGN);
1070 		scctx->isc_rxqsizes[0] = roundup2(scctx->isc_nrxd[0] *
1071 		    sizeof(union e1000_rx_desc_extended), EM_DBA_ALIGN);
1072 		scctx->isc_txd_size[0] = sizeof(struct e1000_tx_desc);
1073 		scctx->isc_rxd_size[0] = sizeof(union e1000_rx_desc_extended);
1074 		scctx->isc_txrx = &em_txrx;
1075 		scctx->isc_tx_tso_segments_max = EM_MAX_SCATTER;
1076 		scctx->isc_tx_tso_size_max = EM_TSO_SIZE;
1077 		scctx->isc_tx_tso_segsize_max = EM_TSO_SEG_SIZE;
1078 		scctx->isc_capabilities = scctx->isc_capenable = EM_CAPS;
1079 		scctx->isc_tx_csum_flags = CSUM_TCP | CSUM_UDP | CSUM_IP_TSO |
1080 		    CSUM_IP6_TCP | CSUM_IP6_UDP;
1081 
1082 		/* Disable TSO on all em(4) until ring stalls are debugged */
1083 		scctx->isc_capenable &= ~IFCAP_TSO;
1084 
1085 		/*
1086 		 * Disable TSO on SPT due to errata that downclocks DMA
1087 		 * performance
1088 		 * i218-i219 Specification Update 1.5.4.5
1089 		 */
1090 		if (hw->mac.type == e1000_pch_spt)
1091 			scctx->isc_capenable &= ~IFCAP_TSO;
1092 
1093 		/*
1094 		 * We support MSI-X with 82574 only, but indicate to iflib(4)
1095 		 * that it shall give MSI at least a try with other devices.
1096 		 */
1097 		if (hw->mac.type == e1000_82574) {
1098 			scctx->isc_msix_bar = pci_msix_table_bar(dev);
1099 		} else {
1100 			scctx->isc_msix_bar = -1;
1101 			scctx->isc_disable_msix = 1;
1102 		}
1103 	} else {
1104 		scctx->isc_txqsizes[0] = roundup2((scctx->isc_ntxd[0] + 1) *
1105 		    sizeof(struct e1000_tx_desc), EM_DBA_ALIGN);
1106 		scctx->isc_rxqsizes[0] = roundup2((scctx->isc_nrxd[0] + 1) *
1107 		    sizeof(struct e1000_rx_desc), EM_DBA_ALIGN);
1108 		scctx->isc_txd_size[0] = sizeof(struct e1000_tx_desc);
1109 		scctx->isc_rxd_size[0] = sizeof(struct e1000_rx_desc);
1110 		scctx->isc_txrx = &lem_txrx;
1111 		scctx->isc_tx_tso_segments_max = EM_MAX_SCATTER;
1112 		scctx->isc_tx_tso_size_max = EM_TSO_SIZE;
1113 		scctx->isc_tx_tso_segsize_max = EM_TSO_SEG_SIZE;
1114 		scctx->isc_capabilities = scctx->isc_capenable = LEM_CAPS;
1115 		if (em_unsupported_tso)
1116 			scctx->isc_capabilities |= IFCAP_TSO6;
1117 		scctx->isc_tx_csum_flags = CSUM_TCP | CSUM_UDP | CSUM_IP_TSO |
1118 		    CSUM_IP6_TCP | CSUM_IP6_UDP;
1119 
1120 		/* Disable TSO on all lem(4) until ring stalls debugged */
1121 		scctx->isc_capenable &= ~IFCAP_TSO;
1122 
1123 		/* 82541ER doesn't do HW tagging */
1124 		if (hw->device_id == E1000_DEV_ID_82541ER ||
1125 		    hw->device_id == E1000_DEV_ID_82541ER_LOM) {
1126 			scctx->isc_capabilities &= ~IFCAP_VLAN_HWTAGGING;
1127 			scctx->isc_capenable = scctx->isc_capabilities;
1128 		}
1129 		/* This is the first e1000 chip and it does not do offloads */
1130 		if (hw->mac.type == e1000_82542) {
1131 			scctx->isc_capabilities &= ~(IFCAP_HWCSUM |
1132 			    IFCAP_VLAN_HWCSUM | IFCAP_HWCSUM_IPV6 |
1133 			    IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWFILTER |
1134 			    IFCAP_TSO | IFCAP_VLAN_HWTSO);
1135 			scctx->isc_capenable = scctx->isc_capabilities;
1136 		}
1137 		/* These can't do TSO for various reasons */
1138 		if (hw->mac.type < e1000_82544 ||
1139 		    hw->mac.type == e1000_82547 ||
1140 		    hw->mac.type == e1000_82547_rev_2) {
1141 			scctx->isc_capabilities &=
1142 			    ~(IFCAP_TSO |IFCAP_VLAN_HWTSO);
1143 			scctx->isc_capenable = scctx->isc_capabilities;
1144 		}
1145 		/* XXXKB: No IPv6 before this? */
1146 		if (hw->mac.type < e1000_82545){
1147 			scctx->isc_capabilities &= ~IFCAP_HWCSUM_IPV6;
1148 			scctx->isc_capenable = scctx->isc_capabilities;
1149 		}
1150 		/*
1151 		 * "PCI/PCI-X SDM 4.0" page 33 (b):
1152 		 * FDX requirement on these chips
1153 		 */
1154 		if (hw->mac.type == e1000_82547 ||
1155 		    hw->mac.type == e1000_82547_rev_2)
1156 			scctx->isc_capenable &= ~(IFCAP_HWCSUM |
1157 			    IFCAP_VLAN_HWCSUM | IFCAP_HWCSUM_IPV6);
1158 
1159 		/* INTx only */
1160 		scctx->isc_msix_bar = 0;
1161 	}
1162 
1163 	/* Setup PCI resources */
1164 	if (em_allocate_pci_resources(ctx)) {
1165 		device_printf(dev, "Allocation of PCI resources failed\n");
1166 		error = ENXIO;
1167 		goto err_pci;
1168 	}
1169 
1170 	/*
1171 	** For ICH8 and family we need to
1172 	** map the flash memory, and this
1173 	** must happen after the MAC is
1174 	** identified
1175 	*/
1176 	if ((hw->mac.type == e1000_ich8lan) ||
1177 	    (hw->mac.type == e1000_ich9lan) ||
1178 	    (hw->mac.type == e1000_ich10lan) ||
1179 	    (hw->mac.type == e1000_pchlan) ||
1180 	    (hw->mac.type == e1000_pch2lan) ||
1181 	    (hw->mac.type == e1000_pch_lpt)) {
1182 		int rid = EM_BAR_TYPE_FLASH;
1183 		sc->flash = bus_alloc_resource_any(dev,
1184 		    SYS_RES_MEMORY, &rid, RF_ACTIVE);
1185 		if (sc->flash == NULL) {
1186 			device_printf(dev, "Mapping of Flash failed\n");
1187 			error = ENXIO;
1188 			goto err_pci;
1189 		}
1190 		/* This is used in the shared code */
1191 		hw->flash_address = (u8 *)sc->flash;
1192 		sc->osdep.flash_bus_space_tag =
1193 		    rman_get_bustag(sc->flash);
1194 		sc->osdep.flash_bus_space_handle =
1195 		    rman_get_bushandle(sc->flash);
1196 	}
1197 	/*
1198 	** In the new SPT device flash is not  a
1199 	** separate BAR, rather it is also in BAR0,
1200 	** so use the same tag and an offset handle for the
1201 	** FLASH read/write macros in the shared code.
1202 	*/
1203 	else if (hw->mac.type >= e1000_pch_spt) {
1204 		sc->osdep.flash_bus_space_tag = sc->osdep.mem_bus_space_tag;
1205 		sc->osdep.flash_bus_space_handle =
1206 		    sc->osdep.mem_bus_space_handle + E1000_FLASH_BASE_ADDR;
1207 	}
1208 
1209 	/* Do Shared Code initialization */
1210 	error = e1000_setup_init_funcs(hw, true);
1211 	if (error) {
1212 		device_printf(dev, "Setup of Shared code failed, error %d\n",
1213 		    error);
1214 		error = ENXIO;
1215 		goto err_pci;
1216 	}
1217 
1218 	em_setup_msix(ctx);
1219 	e1000_get_bus_info(hw);
1220 
1221 	/* Set up some sysctls for the tunable interrupt delays */
1222 	if (hw->mac.type < igb_mac_min) {
1223 		em_add_int_delay_sysctl(sc, "rx_int_delay",
1224 		    "receive interrupt delay in usecs", &sc->rx_int_delay,
1225 		    E1000_REGISTER(hw, E1000_RDTR), em_rx_int_delay_dflt);
1226 		em_add_int_delay_sysctl(sc, "tx_int_delay",
1227 		    "transmit interrupt delay in usecs", &sc->tx_int_delay,
1228 		    E1000_REGISTER(hw, E1000_TIDV), em_tx_int_delay_dflt);
1229 	}
1230 	if (hw->mac.type >= e1000_82540 && hw->mac.type < igb_mac_min) {
1231 		em_add_int_delay_sysctl(sc, "rx_abs_int_delay",
1232 		    "receive interrupt delay limit in usecs",
1233 		    &sc->rx_abs_int_delay,
1234 		    E1000_REGISTER(hw, E1000_RADV), em_rx_abs_int_delay_dflt);
1235 		em_add_int_delay_sysctl(sc, "tx_abs_int_delay",
1236 		    "transmit interrupt delay limit in usecs",
1237 		    &sc->tx_abs_int_delay,
1238 		    E1000_REGISTER(hw, E1000_TADV), em_tx_abs_int_delay_dflt);
1239 	}
1240 
1241 	hw->mac.autoneg = DO_AUTO_NEG;
1242 	hw->phy.autoneg_wait_to_complete = false;
1243 	hw->phy.autoneg_advertised = AUTONEG_ADV_DEFAULT;
1244 
1245 	if (hw->mac.type < em_mac_min) {
1246 		e1000_init_script_state_82541(hw, true);
1247 		e1000_set_tbi_compatibility_82543(hw, true);
1248 	}
1249 	/* Copper options */
1250 	if (hw->phy.media_type == e1000_media_type_copper) {
1251 		hw->phy.mdix = AUTO_ALL_MODES;
1252 		hw->phy.disable_polarity_correction = false;
1253 		hw->phy.ms_type = EM_MASTER_SLAVE;
1254 	}
1255 
1256 	/*
1257 	 * Set the frame limits assuming
1258 	 * standard ethernet sized frames.
1259 	 */
1260 	scctx->isc_max_frame_size = hw->mac.max_frame_size =
1261 	    ETHERMTU + ETHER_HDR_LEN + ETHERNET_FCS_SIZE;
1262 
1263 	/*
1264 	 * This controls when hardware reports transmit completion
1265 	 * status.
1266 	 */
1267 	hw->mac.report_tx_early = 1;
1268 
1269 	/* Allocate multicast array memory. */
1270 	sc->mta = malloc(sizeof(u8) * ETHER_ADDR_LEN *
1271 	    MAX_NUM_MULTICAST_ADDRESSES, M_DEVBUF, M_NOWAIT);
1272 	if (sc->mta == NULL) {
1273 		device_printf(dev,
1274 		    "Can not allocate multicast setup array\n");
1275 		error = ENOMEM;
1276 		goto err_late;
1277 	}
1278 
1279 	/* Clear the IFCAP_TSO auto mask */
1280 	sc->tso_automasked = 0;
1281 
1282 	/* Check SOL/IDER usage */
1283 	if (e1000_check_reset_block(hw))
1284 		device_printf(dev,
1285 		    "PHY reset is blocked due to SOL/IDER session.\n");
1286 
1287 	/* Sysctl for setting Energy Efficient Ethernet */
1288 	if (hw->mac.type < igb_mac_min)
1289 		hw->dev_spec.ich8lan.eee_disable = eee_setting;
1290 	else
1291 		hw->dev_spec._82575.eee_disable = eee_setting;
1292 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "eee_control",
1293 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
1294 	    em_sysctl_eee, "I", "Disable Energy Efficient Ethernet");
1295 
1296 	/*
1297 	** Start from a known state, this is
1298 	** important in reading the nvm and
1299 	** mac from that.
1300 	*/
1301 	e1000_reset_hw(hw);
1302 
1303 	/* Make sure we have a good EEPROM before we read from it */
1304 	if (e1000_validate_nvm_checksum(hw) < 0) {
1305 		/*
1306 		** Some PCI-E parts fail the first check due to
1307 		** the link being in sleep state, call it again,
1308 		** if it fails a second time its a real issue.
1309 		*/
1310 		if (e1000_validate_nvm_checksum(hw) < 0) {
1311 			device_printf(dev,
1312 			    "The EEPROM Checksum Is Not Valid\n");
1313 			error = EIO;
1314 			goto err_late;
1315 		}
1316 	}
1317 
1318 	/* Copy the permanent MAC address out of the EEPROM */
1319 	if (e1000_read_mac_addr(hw) < 0) {
1320 		device_printf(dev,
1321 		    "EEPROM read error while reading MAC address\n");
1322 		error = EIO;
1323 		goto err_late;
1324 	}
1325 
1326 	if (!em_is_valid_ether_addr(hw->mac.addr)) {
1327 		if (sc->vf_ifp) {
1328 			ether_gen_addr(iflib_get_ifp(ctx),
1329 			    (struct ether_addr *)hw->mac.addr);
1330 		} else {
1331 			device_printf(dev, "Invalid MAC address\n");
1332 			error = EIO;
1333 			goto err_late;
1334 		}
1335 	}
1336 
1337 	/* Save the EEPROM/NVM versions, must be done under IFLIB_CTX_LOCK */
1338 	em_fw_version_locked(ctx);
1339 
1340 	em_print_fw_version(sc);
1341 
1342 	/*
1343 	 * Get Wake-on-Lan and Management info for later use
1344 	 */
1345 	em_get_wakeup(ctx);
1346 
1347 	/* Enable only WOL MAGIC by default */
1348 	scctx->isc_capenable &= ~IFCAP_WOL;
1349 	if (sc->wol != 0)
1350 		scctx->isc_capenable |= IFCAP_WOL_MAGIC;
1351 
1352 	iflib_set_mac(ctx, hw->mac.addr);
1353 
1354 	return (0);
1355 
1356 err_late:
1357 	em_release_hw_control(sc);
1358 err_pci:
1359 	em_free_pci_resources(ctx);
1360 	free(sc->mta, M_DEVBUF);
1361 
1362 	return (error);
1363 }
1364 
1365 static int
em_if_attach_post(if_ctx_t ctx)1366 em_if_attach_post(if_ctx_t ctx)
1367 {
1368 	struct e1000_softc *sc = iflib_get_softc(ctx);
1369 	struct e1000_hw *hw = &sc->hw;
1370 	int error = 0;
1371 
1372 	/* Setup OS specific network interface */
1373 	error = em_setup_interface(ctx);
1374 	if (error != 0) {
1375 		device_printf(sc->dev, "Interface setup failed: %d\n", error);
1376 		goto err_late;
1377 	}
1378 
1379 	em_reset(ctx);
1380 
1381 	/* Initialize statistics */
1382 	if (sc->vf_ifp)
1383 		sc->ustats.vf_stats = (struct e1000_vf_stats){};
1384 	else
1385 		sc->ustats.stats = (struct e1000_hw_stats){};
1386 
1387 	em_update_stats_counters(sc);
1388 	hw->mac.get_link_status = 1;
1389 	em_if_update_admin_status(ctx);
1390 	em_add_hw_stats(sc);
1391 
1392 	/* Non-AMT based hardware can now take control from firmware */
1393 	if (sc->has_manage && !sc->has_amt)
1394 		em_get_hw_control(sc);
1395 
1396 	INIT_DEBUGOUT("em_if_attach_post: end");
1397 
1398 	return (0);
1399 
1400 err_late:
1401 	/*
1402 	 * Upon em_if_attach_post() error, iflib calls em_if_detach() to
1403 	 * free resources
1404 	 */
1405 	return (error);
1406 }
1407 
1408 /*********************************************************************
1409  *  Device removal routine
1410  *
1411  *  The detach entry point is called when the driver is being removed.
1412  *  This routine stops the adapter and deallocates all the resources
1413  *  that were allocated for driver operation.
1414  *
1415  *  return 0 on success, positive on failure
1416  *********************************************************************/
1417 static int
em_if_detach(if_ctx_t ctx)1418 em_if_detach(if_ctx_t ctx)
1419 {
1420 	struct e1000_softc	*sc = iflib_get_softc(ctx);
1421 
1422 	INIT_DEBUGOUT("em_if_detach: begin");
1423 
1424 	e1000_phy_hw_reset(&sc->hw);
1425 
1426 	em_release_manageability(sc);
1427 	em_release_hw_control(sc);
1428 	em_free_pci_resources(ctx);
1429 	free(sc->mta, M_DEVBUF);
1430 	sc->mta = NULL;
1431 
1432 	return (0);
1433 }
1434 
1435 /*********************************************************************
1436  *
1437  *  Shutdown entry point
1438  *
1439  **********************************************************************/
1440 
1441 static int
em_if_shutdown(if_ctx_t ctx)1442 em_if_shutdown(if_ctx_t ctx)
1443 {
1444 	return em_if_suspend(ctx);
1445 }
1446 
1447 /*
1448  * Suspend/resume device methods.
1449  */
1450 static int
em_if_suspend(if_ctx_t ctx)1451 em_if_suspend(if_ctx_t ctx)
1452 {
1453 	struct e1000_softc *sc = iflib_get_softc(ctx);
1454 
1455 	em_release_manageability(sc);
1456 	em_release_hw_control(sc);
1457 	em_enable_wakeup(ctx);
1458 	return (0);
1459 }
1460 
1461 static int
em_if_resume(if_ctx_t ctx)1462 em_if_resume(if_ctx_t ctx)
1463 {
1464 	struct e1000_softc *sc = iflib_get_softc(ctx);
1465 
1466 	if (sc->hw.mac.type == e1000_pch2lan)
1467 		e1000_resume_workarounds_pchlan(&sc->hw);
1468 	em_if_init(ctx);
1469 	em_init_manageability(sc);
1470 
1471 	return(0);
1472 }
1473 
1474 static int
em_if_mtu_set(if_ctx_t ctx,uint32_t mtu)1475 em_if_mtu_set(if_ctx_t ctx, uint32_t mtu)
1476 {
1477 	int max_frame_size;
1478 	struct e1000_softc *sc = iflib_get_softc(ctx);
1479 	if_softc_ctx_t scctx = iflib_get_softc_ctx(ctx);
1480 
1481 	IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFMTU (Set Interface MTU)");
1482 
1483 	switch (sc->hw.mac.type) {
1484 	case e1000_82571:
1485 	case e1000_82572:
1486 	case e1000_ich9lan:
1487 	case e1000_ich10lan:
1488 	case e1000_pch2lan:
1489 	case e1000_pch_lpt:
1490 	case e1000_pch_spt:
1491 	case e1000_pch_cnp:
1492 	case e1000_pch_tgp:
1493 	case e1000_pch_adp:
1494 	case e1000_pch_mtp:
1495 	case e1000_pch_ptp:
1496 	case e1000_82574:
1497 	case e1000_82583:
1498 	case e1000_80003es2lan:
1499 		/* 9K Jumbo Frame size */
1500 		max_frame_size = 9234;
1501 		break;
1502 	case e1000_pchlan:
1503 		max_frame_size = 4096;
1504 		break;
1505 	case e1000_82542:
1506 	case e1000_ich8lan:
1507 		/* Adapters that do not support jumbo frames */
1508 		max_frame_size = ETHER_MAX_LEN;
1509 		break;
1510 	default:
1511 		if (sc->hw.mac.type >= igb_mac_min)
1512 			max_frame_size = 9234;
1513 		else /* lem */
1514 			max_frame_size = MAX_JUMBO_FRAME_SIZE;
1515 	}
1516 	if (mtu > max_frame_size - ETHER_HDR_LEN - ETHER_CRC_LEN) {
1517 		return (EINVAL);
1518 	}
1519 
1520 	scctx->isc_max_frame_size = sc->hw.mac.max_frame_size =
1521 	    mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
1522 	return (0);
1523 }
1524 
1525 /*********************************************************************
1526  *  Init entry point
1527  *
1528  *  This routine is used in two ways. It is used by the stack as
1529  *  init entry point in network interface structure. It is also used
1530  *  by the driver as a hw/sw initialization routine to get to a
1531  *  consistent state.
1532  *
1533  **********************************************************************/
1534 static void
em_if_init(if_ctx_t ctx)1535 em_if_init(if_ctx_t ctx)
1536 {
1537 	struct e1000_softc *sc = iflib_get_softc(ctx);
1538 	if_softc_ctx_t scctx = sc->shared;
1539 	if_t ifp = iflib_get_ifp(ctx);
1540 	struct em_tx_queue *tx_que;
1541 	int i;
1542 
1543 	INIT_DEBUGOUT("em_if_init: begin");
1544 
1545 	/* Get the latest mac address, User can use a LAA */
1546 	bcopy(if_getlladdr(ifp), sc->hw.mac.addr, ETHER_ADDR_LEN);
1547 
1548 	/* Put the address into the Receive Address Array */
1549 	e1000_rar_set(&sc->hw, sc->hw.mac.addr, 0);
1550 
1551 	/*
1552 	 * With the 82571 adapter, RAR[0] may be overwritten
1553 	 * when the other port is reset, we make a duplicate
1554 	 * in RAR[14] for that eventuality, this assures
1555 	 * the interface continues to function.
1556 	 */
1557 	if (sc->hw.mac.type == e1000_82571) {
1558 		e1000_set_laa_state_82571(&sc->hw, true);
1559 		e1000_rar_set(&sc->hw, sc->hw.mac.addr,
1560 		    E1000_RAR_ENTRIES - 1);
1561 	}
1562 
1563 	/* Initialize the hardware */
1564 	em_reset(ctx);
1565 	em_if_update_admin_status(ctx);
1566 
1567 	for (i = 0, tx_que = sc->tx_queues; i < sc->tx_num_queues;
1568 	    i++, tx_que++) {
1569 		struct tx_ring *txr = &tx_que->txr;
1570 
1571 		txr->tx_rs_cidx = txr->tx_rs_pidx;
1572 
1573 		/* Initialize the last processed descriptor to be the end of
1574 		 * the ring, rather than the start, so that we avoid an
1575 		 * off-by-one error when calculating how many descriptors are
1576 		 * done in the credits_update function.
1577 		 */
1578 		txr->tx_cidx_processed = scctx->isc_ntxd[0] - 1;
1579 	}
1580 
1581 	/* Setup VLAN support, basic and offload if available */
1582 	E1000_WRITE_REG(&sc->hw, E1000_VET, ETHERTYPE_VLAN);
1583 
1584 	/* Clear bad data from Rx FIFOs */
1585 	if (sc->hw.mac.type >= igb_mac_min && !sc->vf_ifp)
1586 		e1000_rx_fifo_flush_base(&sc->hw);
1587 
1588 	/* Configure for OS presence */
1589 	em_init_manageability(sc);
1590 
1591 	/* Prepare transmit descriptors and buffers */
1592 	em_initialize_transmit_unit(ctx);
1593 
1594 	/* Setup Multicast table */
1595 	em_if_multi_set(ctx);
1596 
1597 	sc->rx_mbuf_sz = iflib_get_rx_mbuf_sz(ctx);
1598 	em_initialize_receive_unit(ctx);
1599 
1600 	/* Set up VLAN support and filter */
1601 	em_setup_vlan_hw_support(ctx);
1602 
1603 	/* Don't lose promiscuous settings */
1604 	em_if_set_promisc(ctx, if_getflags(ifp));
1605 
1606 	if (sc->hw.mac.ops.clear_hw_cntrs != NULL)
1607 		sc->hw.mac.ops.clear_hw_cntrs(&sc->hw);
1608 
1609 	/* MSI-X configuration for 82574 */
1610 	if (sc->hw.mac.type == e1000_82574) {
1611 		int tmp = E1000_READ_REG(&sc->hw, E1000_CTRL_EXT);
1612 
1613 		tmp |= E1000_CTRL_EXT_PBA_CLR;
1614 		E1000_WRITE_REG(&sc->hw, E1000_CTRL_EXT, tmp);
1615 		/* Set the IVAR - interrupt vector routing. */
1616 		E1000_WRITE_REG(&sc->hw, E1000_IVAR, sc->ivars);
1617 	} else if (sc->intr_type == IFLIB_INTR_MSIX) {
1618 		/* Set up queue routing */
1619 		igb_configure_queues(sc);
1620 	}
1621 
1622 	/* this clears any pending interrupts */
1623 	E1000_READ_REG(&sc->hw, E1000_ICR);
1624 	E1000_WRITE_REG(&sc->hw, E1000_ICS, E1000_ICS_LSC);
1625 
1626 	/* AMT based hardware can now take control from firmware */
1627 	if (sc->has_manage && sc->has_amt)
1628 		em_get_hw_control(sc);
1629 
1630 	/* Set Energy Efficient Ethernet */
1631 	if (sc->hw.mac.type >= igb_mac_min &&
1632 	    sc->hw.phy.media_type == e1000_media_type_copper) {
1633 		if (sc->hw.mac.type == e1000_i354)
1634 			e1000_set_eee_i354(&sc->hw, true, true);
1635 		else
1636 			e1000_set_eee_i350(&sc->hw, true, true);
1637 	}
1638 }
1639 
1640 enum itr_latency_target {
1641 	itr_latency_disabled = 0,
1642 	itr_latency_lowest = 1,
1643 	itr_latency_low = 2,
1644 	itr_latency_bulk = 3
1645 };
1646 /*********************************************************************
1647  *
1648  *  Helper to calculate next (E)ITR value for AIM
1649  *
1650  *********************************************************************/
1651 static void
em_newitr(struct e1000_softc * sc,struct em_rx_queue * que,struct tx_ring * txr,struct rx_ring * rxr)1652 em_newitr(struct e1000_softc *sc, struct em_rx_queue *que,
1653     struct tx_ring *txr, struct rx_ring *rxr)
1654 {
1655 	struct e1000_hw *hw = &sc->hw;
1656 	unsigned long bytes, bytes_per_packet, packets;
1657 	unsigned long rxbytes, rxpackets, txbytes, txpackets;
1658 	u32 newitr;
1659 	u8 nextlatency;
1660 
1661 	rxbytes = atomic_load_long(&rxr->rx_bytes);
1662 	txbytes = atomic_load_long(&txr->tx_bytes);
1663 
1664 	/* Idle, do nothing */
1665 	if (txbytes == 0 && rxbytes == 0)
1666 		return;
1667 
1668 	newitr = 0;
1669 
1670 	if (sc->enable_aim) {
1671 		nextlatency = rxr->rx_nextlatency;
1672 
1673 		/* Use half default (4K) ITR if sub-gig */
1674 		if (sc->link_speed != 1000) {
1675 			newitr = EM_INTS_4K;
1676 			goto em_set_next_itr;
1677 		}
1678 		/* Want at least enough packet buffer for two frames to AIM */
1679 		if (sc->shared->isc_max_frame_size * 2 > (sc->pba << 10)) {
1680 			newitr = em_max_interrupt_rate;
1681 			sc->enable_aim = 0;
1682 			goto em_set_next_itr;
1683 		}
1684 
1685 		bytes = bytes_per_packet = 0;
1686 		/* Get largest values from the associated tx and rx ring */
1687 		txpackets = atomic_load_long(&txr->tx_packets);
1688 		if (txpackets != 0) {
1689 			bytes = txbytes;
1690 			bytes_per_packet = txbytes / txpackets;
1691 			packets = txpackets;
1692 		}
1693 		rxpackets = atomic_load_long(&rxr->rx_packets);
1694 		if (rxpackets != 0) {
1695 			bytes = lmax(bytes, rxbytes);
1696 			bytes_per_packet =
1697 			    lmax(bytes_per_packet, rxbytes / rxpackets);
1698 			packets = lmax(packets, rxpackets);
1699 		}
1700 
1701 		/* Latency state machine */
1702 		switch (nextlatency) {
1703 		case itr_latency_disabled: /* Bootstrapping */
1704 			nextlatency = itr_latency_low;
1705 			break;
1706 		case itr_latency_lowest: /* 70k ints/s */
1707 			/* TSO and jumbo frames */
1708 			if (bytes_per_packet > 8000)
1709 				nextlatency = itr_latency_bulk;
1710 			else if ((packets < 5) && (bytes > 512))
1711 				nextlatency = itr_latency_low;
1712 			break;
1713 		case itr_latency_low: /* 20k ints/s */
1714 			if (bytes > 10000) {
1715 				/* Handle TSO */
1716 				if (bytes_per_packet > 8000)
1717 					nextlatency = itr_latency_bulk;
1718 				else if ((packets < 10) ||
1719 				    (bytes_per_packet > 1200))
1720 					nextlatency = itr_latency_bulk;
1721 				else if (packets > 35)
1722 					nextlatency = itr_latency_lowest;
1723 			} else if (bytes_per_packet > 2000) {
1724 				nextlatency = itr_latency_bulk;
1725 			} else if (packets < 3 && bytes < 512) {
1726 				nextlatency = itr_latency_lowest;
1727 			}
1728 			break;
1729 		case itr_latency_bulk: /* 4k ints/s */
1730 			if (bytes > 25000) {
1731 				if (packets > 35)
1732 					nextlatency = itr_latency_low;
1733 			} else if (bytes < 1500)
1734 				nextlatency = itr_latency_low;
1735 			break;
1736 		default:
1737 			nextlatency = itr_latency_low;
1738 			device_printf(sc->dev,
1739 			    "Unexpected newitr transition %d\n", nextlatency);
1740 			break;
1741 		}
1742 
1743 		/* Trim itr_latency_lowest for default AIM setting */
1744 		if (sc->enable_aim == 1 && nextlatency == itr_latency_lowest)
1745 			nextlatency = itr_latency_low;
1746 
1747 		/* Request new latency */
1748 		rxr->rx_nextlatency = nextlatency;
1749 	} else {
1750 		/* We may have toggled to AIM disabled */
1751 		nextlatency = itr_latency_disabled;
1752 		rxr->rx_nextlatency = nextlatency;
1753 	}
1754 
1755 	/* ITR state machine */
1756 	switch(nextlatency) {
1757 	case itr_latency_lowest:
1758 		newitr = EM_INTS_70K;
1759 		break;
1760 	case itr_latency_low:
1761 		newitr = EM_INTS_20K;
1762 		break;
1763 	case itr_latency_bulk:
1764 		newitr = EM_INTS_4K;
1765 		break;
1766 	case itr_latency_disabled:
1767 	default:
1768 		newitr = em_max_interrupt_rate;
1769 		break;
1770 	}
1771 
1772 em_set_next_itr:
1773 	if (hw->mac.type >= igb_mac_min) {
1774 		newitr = IGB_INTS_TO_EITR(newitr);
1775 
1776 		if (hw->mac.type == e1000_82575)
1777 			newitr |= newitr << 16;
1778 		else
1779 			newitr |= E1000_EITR_CNT_IGNR;
1780 
1781 		if (newitr != que->itr_setting) {
1782 			que->itr_setting = newitr;
1783 			E1000_WRITE_REG(hw, E1000_EITR(que->msix),
1784 			    que->itr_setting);
1785 		}
1786 	} else {
1787 		newitr = EM_INTS_TO_ITR(newitr);
1788 
1789 		if (newitr != que->itr_setting) {
1790 			que->itr_setting = newitr;
1791 			if (hw->mac.type == e1000_82574 && que->msix) {
1792 				E1000_WRITE_REG(hw,
1793 				    E1000_EITR_82574(que->msix),
1794 				    que->itr_setting);
1795 			} else {
1796 				E1000_WRITE_REG(hw, E1000_ITR,
1797 				    que->itr_setting);
1798 			}
1799 		}
1800 	}
1801 }
1802 
1803 /*********************************************************************
1804  *
1805  *  Fast Legacy/MSI Combined Interrupt Service routine
1806  *
1807  *********************************************************************/
1808 int
em_intr(void * arg)1809 em_intr(void *arg)
1810 {
1811 	struct e1000_softc *sc = arg;
1812 	struct e1000_hw *hw = &sc->hw;
1813 	struct em_rx_queue *que = &sc->rx_queues[0];
1814 	struct tx_ring *txr = &sc->tx_queues[0].txr;
1815 	struct rx_ring *rxr = &que->rxr;
1816 	if_ctx_t ctx = sc->ctx;
1817 	u32 reg_icr;
1818 
1819 	reg_icr = E1000_READ_REG(hw, E1000_ICR);
1820 
1821 	/* Hot eject? */
1822 	if (reg_icr == 0xffffffff)
1823 		return FILTER_STRAY;
1824 
1825 	/* Definitely not our interrupt. */
1826 	if (reg_icr == 0x0)
1827 		return FILTER_STRAY;
1828 
1829 	/*
1830 	 * Starting with the 82571 chip, bit 31 should be used to
1831 	 * determine whether the interrupt belongs to us.
1832 	 */
1833 	if (hw->mac.type >= e1000_82571 &&
1834 	    (reg_icr & E1000_ICR_INT_ASSERTED) == 0)
1835 		return FILTER_STRAY;
1836 
1837 	/*
1838 	 * Only MSI-X interrupts have one-shot behavior by taking advantage
1839 	 * of the EIAC register.  Thus, explicitly disable interrupts.  This
1840 	 * also works around the MSI message reordering errata on certain
1841 	 * systems.
1842 	 */
1843 	IFDI_INTR_DISABLE(ctx);
1844 
1845 	/* Link status change */
1846 	if (reg_icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))
1847 		em_handle_link(ctx);
1848 
1849 	if (reg_icr & E1000_ICR_RXO)
1850 		sc->rx_overruns++;
1851 
1852 	if (hw->mac.type >= e1000_82540)
1853 		em_newitr(sc, que, txr, rxr);
1854 
1855 	/* Reset state */
1856 	txr->tx_bytes = 0;
1857 	txr->tx_packets = 0;
1858 	rxr->rx_bytes = 0;
1859 	rxr->rx_packets = 0;
1860 
1861 	return (FILTER_SCHEDULE_THREAD);
1862 }
1863 
1864 static int
em_if_rx_queue_intr_enable(if_ctx_t ctx,uint16_t rxqid)1865 em_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid)
1866 {
1867 	struct e1000_softc *sc = iflib_get_softc(ctx);
1868 	struct em_rx_queue *rxq = &sc->rx_queues[rxqid];
1869 
1870 	E1000_WRITE_REG(&sc->hw, E1000_IMS, rxq->eims);
1871 	return (0);
1872 }
1873 
1874 static int
em_if_tx_queue_intr_enable(if_ctx_t ctx,uint16_t txqid)1875 em_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid)
1876 {
1877 	struct e1000_softc *sc = iflib_get_softc(ctx);
1878 	struct em_tx_queue *txq = &sc->tx_queues[txqid];
1879 
1880 	E1000_WRITE_REG(&sc->hw, E1000_IMS, txq->eims);
1881 	return (0);
1882 }
1883 
1884 static int
igb_if_rx_queue_intr_enable(if_ctx_t ctx,uint16_t rxqid)1885 igb_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid)
1886 {
1887 	struct e1000_softc *sc = iflib_get_softc(ctx);
1888 	struct em_rx_queue *rxq = &sc->rx_queues[rxqid];
1889 
1890 	E1000_WRITE_REG(&sc->hw, E1000_EIMS, rxq->eims);
1891 	return (0);
1892 }
1893 
1894 static int
igb_if_tx_queue_intr_enable(if_ctx_t ctx,uint16_t txqid)1895 igb_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid)
1896 {
1897 	struct e1000_softc *sc = iflib_get_softc(ctx);
1898 	struct em_tx_queue *txq = &sc->tx_queues[txqid];
1899 
1900 	E1000_WRITE_REG(&sc->hw, E1000_EIMS, txq->eims);
1901 	return (0);
1902 }
1903 
1904 /*********************************************************************
1905  *
1906  *  MSI-X RX Interrupt Service routine
1907  *
1908  **********************************************************************/
1909 static int
em_msix_que(void * arg)1910 em_msix_que(void *arg)
1911 {
1912 	struct em_rx_queue *que = arg;
1913 	struct e1000_softc *sc = que->sc;
1914 	struct tx_ring *txr = &sc->tx_queues[que->msix].txr;
1915 	struct rx_ring *rxr = &que->rxr;
1916 
1917 	++que->irqs;
1918 
1919 	em_newitr(sc, que, txr, rxr);
1920 
1921 	/* Reset state */
1922 	txr->tx_bytes = 0;
1923 	txr->tx_packets = 0;
1924 	rxr->rx_bytes = 0;
1925 	rxr->rx_packets = 0;
1926 
1927 	return (FILTER_SCHEDULE_THREAD);
1928 }
1929 
1930 /*********************************************************************
1931  *
1932  *  MSI-X Link Fast Interrupt Service routine
1933  *
1934  **********************************************************************/
1935 static int
em_msix_link(void * arg)1936 em_msix_link(void *arg)
1937 {
1938 	struct e1000_softc *sc = arg;
1939 	u32 reg_icr;
1940 
1941 	++sc->link_irq;
1942 	MPASS(sc->hw.back != NULL);
1943 	reg_icr = E1000_READ_REG(&sc->hw, E1000_ICR);
1944 
1945 	if (reg_icr & E1000_ICR_RXO)
1946 		sc->rx_overruns++;
1947 
1948 	if (reg_icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))
1949 		em_handle_link(sc->ctx);
1950 
1951 	/* Re-arm unconditionally */
1952 	if (sc->hw.mac.type >= igb_mac_min) {
1953 		E1000_WRITE_REG(&sc->hw, E1000_IMS, E1000_IMS_LSC);
1954 		E1000_WRITE_REG(&sc->hw, E1000_EIMS, sc->link_mask);
1955 	} else if (sc->hw.mac.type == e1000_82574) {
1956 		E1000_WRITE_REG(&sc->hw, E1000_IMS,
1957 		    E1000_IMS_LSC | E1000_IMS_OTHER);
1958 		/*
1959 		 * Because we must read the ICR for this interrupt it may
1960 		 * clear other causes using autoclear, for this reason we
1961 		 * simply create a soft interrupt for all these vectors.
1962 		 */
1963 		if (reg_icr)
1964 			E1000_WRITE_REG(&sc->hw, E1000_ICS, sc->ims);
1965 	} else
1966 		E1000_WRITE_REG(&sc->hw, E1000_IMS, E1000_IMS_LSC);
1967 
1968 	return (FILTER_HANDLED);
1969 }
1970 
1971 static void
em_handle_link(void * context)1972 em_handle_link(void *context)
1973 {
1974 	if_ctx_t ctx = context;
1975 	struct e1000_softc *sc = iflib_get_softc(ctx);
1976 
1977 	sc->hw.mac.get_link_status = 1;
1978 	iflib_admin_intr_deferred(ctx);
1979 }
1980 
1981 /*********************************************************************
1982  *
1983  *  Media Ioctl callback
1984  *
1985  *  This routine is called whenever the user queries the status of
1986  *  the interface using ifconfig.
1987  *
1988  **********************************************************************/
1989 static void
em_if_media_status(if_ctx_t ctx,struct ifmediareq * ifmr)1990 em_if_media_status(if_ctx_t ctx, struct ifmediareq *ifmr)
1991 {
1992 	struct e1000_softc *sc = iflib_get_softc(ctx);
1993 	u_char fiber_type = IFM_1000_SX;
1994 
1995 	INIT_DEBUGOUT("em_if_media_status: begin");
1996 
1997 	iflib_admin_intr_deferred(ctx);
1998 
1999 	ifmr->ifm_status = IFM_AVALID;
2000 	ifmr->ifm_active = IFM_ETHER;
2001 
2002 	if (!sc->link_active) {
2003 		return;
2004 	}
2005 
2006 	ifmr->ifm_status |= IFM_ACTIVE;
2007 
2008 	if ((sc->hw.phy.media_type == e1000_media_type_fiber) ||
2009 	    (sc->hw.phy.media_type == e1000_media_type_internal_serdes)) {
2010 		if (sc->hw.mac.type == e1000_82545)
2011 			fiber_type = IFM_1000_LX;
2012 		ifmr->ifm_active |= fiber_type | IFM_FDX;
2013 	} else {
2014 		switch (sc->link_speed) {
2015 		case 10:
2016 			ifmr->ifm_active |= IFM_10_T;
2017 			break;
2018 		case 100:
2019 			ifmr->ifm_active |= IFM_100_TX;
2020 			break;
2021 		case 1000:
2022 			ifmr->ifm_active |= IFM_1000_T;
2023 			break;
2024 		}
2025 		if (sc->link_duplex == FULL_DUPLEX)
2026 			ifmr->ifm_active |= IFM_FDX;
2027 		else
2028 			ifmr->ifm_active |= IFM_HDX;
2029 	}
2030 }
2031 
2032 /*********************************************************************
2033  *
2034  *  Media Ioctl callback
2035  *
2036  *  This routine is called when the user changes speed/duplex using
2037  *  media/mediopt option with ifconfig.
2038  *
2039  **********************************************************************/
2040 static int
em_if_media_change(if_ctx_t ctx)2041 em_if_media_change(if_ctx_t ctx)
2042 {
2043 	struct e1000_softc *sc = iflib_get_softc(ctx);
2044 	struct ifmedia *ifm = iflib_get_media(ctx);
2045 
2046 	INIT_DEBUGOUT("em_if_media_change: begin");
2047 
2048 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
2049 		return (EINVAL);
2050 
2051 	switch (IFM_SUBTYPE(ifm->ifm_media)) {
2052 	case IFM_AUTO:
2053 		sc->hw.mac.autoneg = DO_AUTO_NEG;
2054 		sc->hw.phy.autoneg_advertised = AUTONEG_ADV_DEFAULT;
2055 		break;
2056 	case IFM_1000_LX:
2057 	case IFM_1000_SX:
2058 	case IFM_1000_T:
2059 		sc->hw.mac.autoneg = DO_AUTO_NEG;
2060 		sc->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
2061 		break;
2062 	case IFM_100_TX:
2063 		sc->hw.mac.autoneg = false;
2064 		sc->hw.phy.autoneg_advertised = 0;
2065 		if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
2066 			sc->hw.mac.forced_speed_duplex = ADVERTISE_100_FULL;
2067 		else
2068 			sc->hw.mac.forced_speed_duplex = ADVERTISE_100_HALF;
2069 		break;
2070 	case IFM_10_T:
2071 		sc->hw.mac.autoneg = false;
2072 		sc->hw.phy.autoneg_advertised = 0;
2073 		if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
2074 			sc->hw.mac.forced_speed_duplex = ADVERTISE_10_FULL;
2075 		else
2076 			sc->hw.mac.forced_speed_duplex = ADVERTISE_10_HALF;
2077 		break;
2078 	default:
2079 		device_printf(sc->dev, "Unsupported media type\n");
2080 	}
2081 
2082 	em_if_init(ctx);
2083 
2084 	return (0);
2085 }
2086 
2087 static int
em_if_set_promisc(if_ctx_t ctx,int flags)2088 em_if_set_promisc(if_ctx_t ctx, int flags)
2089 {
2090 	struct e1000_softc *sc = iflib_get_softc(ctx);
2091 	if_t ifp = iflib_get_ifp(ctx);
2092 	u32 reg_rctl;
2093 	int mcnt = 0;
2094 
2095 	reg_rctl = E1000_READ_REG(&sc->hw, E1000_RCTL);
2096 	reg_rctl &= ~(E1000_RCTL_SBP | E1000_RCTL_UPE);
2097 	if (flags & IFF_ALLMULTI)
2098 		mcnt = MAX_NUM_MULTICAST_ADDRESSES;
2099 	else
2100 		mcnt = min(if_llmaddr_count(ifp),
2101 		    MAX_NUM_MULTICAST_ADDRESSES);
2102 
2103 	if (mcnt < MAX_NUM_MULTICAST_ADDRESSES)
2104 		reg_rctl &= (~E1000_RCTL_MPE);
2105 	E1000_WRITE_REG(&sc->hw, E1000_RCTL, reg_rctl);
2106 
2107 	if (flags & IFF_PROMISC) {
2108 		reg_rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
2109 		em_if_vlan_filter_disable(sc);
2110 		/* Turn this on if you want to see bad packets */
2111 		if (em_debug_sbp)
2112 			reg_rctl |= E1000_RCTL_SBP;
2113 		E1000_WRITE_REG(&sc->hw, E1000_RCTL, reg_rctl);
2114 	} else {
2115 		if (flags & IFF_ALLMULTI) {
2116 			reg_rctl |= E1000_RCTL_MPE;
2117 			reg_rctl &= ~E1000_RCTL_UPE;
2118 			E1000_WRITE_REG(&sc->hw, E1000_RCTL, reg_rctl);
2119 		}
2120 		if (em_if_vlan_filter_used(ctx))
2121 			em_if_vlan_filter_enable(sc);
2122 	}
2123 	return (0);
2124 }
2125 
2126 static u_int
em_copy_maddr(void * arg,struct sockaddr_dl * sdl,u_int idx)2127 em_copy_maddr(void *arg, struct sockaddr_dl *sdl, u_int idx)
2128 {
2129 	u8 *mta = arg;
2130 
2131 	if (idx == MAX_NUM_MULTICAST_ADDRESSES)
2132 		return (0);
2133 
2134 	bcopy(LLADDR(sdl), &mta[idx * ETHER_ADDR_LEN], ETHER_ADDR_LEN);
2135 
2136 	return (1);
2137 }
2138 
2139 /*********************************************************************
2140  *  Multicast Update
2141  *
2142  *  This routine is called whenever multicast address list is updated.
2143  *
2144  **********************************************************************/
2145 static void
em_if_multi_set(if_ctx_t ctx)2146 em_if_multi_set(if_ctx_t ctx)
2147 {
2148 	struct e1000_softc *sc = iflib_get_softc(ctx);
2149 	if_t ifp = iflib_get_ifp(ctx);
2150 	u8 *mta; /* Multicast array memory */
2151 	u32 reg_rctl = 0;
2152 	int mcnt = 0;
2153 
2154 	IOCTL_DEBUGOUT("em_set_multi: begin");
2155 
2156 	mta = sc->mta;
2157 	bzero(mta, sizeof(u8) * ETHER_ADDR_LEN * MAX_NUM_MULTICAST_ADDRESSES);
2158 
2159 	if (sc->hw.mac.type == e1000_82542 &&
2160 	    sc->hw.revision_id == E1000_REVISION_2) {
2161 		reg_rctl = E1000_READ_REG(&sc->hw, E1000_RCTL);
2162 		if (sc->hw.bus.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
2163 			e1000_pci_clear_mwi(&sc->hw);
2164 		reg_rctl |= E1000_RCTL_RST;
2165 		E1000_WRITE_REG(&sc->hw, E1000_RCTL, reg_rctl);
2166 		msec_delay(5);
2167 	}
2168 
2169 	mcnt = if_foreach_llmaddr(ifp, em_copy_maddr, mta);
2170 
2171 	if (mcnt < MAX_NUM_MULTICAST_ADDRESSES)
2172 		e1000_update_mc_addr_list(&sc->hw, mta, mcnt);
2173 
2174 	reg_rctl = E1000_READ_REG(&sc->hw, E1000_RCTL);
2175 
2176 	if (if_getflags(ifp) & IFF_PROMISC)
2177 		reg_rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
2178 	else if (mcnt >= MAX_NUM_MULTICAST_ADDRESSES ||
2179 	    if_getflags(ifp) & IFF_ALLMULTI) {
2180 		reg_rctl |= E1000_RCTL_MPE;
2181 		reg_rctl &= ~E1000_RCTL_UPE;
2182 	} else
2183 		reg_rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE);
2184 
2185 	E1000_WRITE_REG(&sc->hw, E1000_RCTL, reg_rctl);
2186 
2187 	if (sc->hw.mac.type == e1000_82542 &&
2188 	    sc->hw.revision_id == E1000_REVISION_2) {
2189 		reg_rctl = E1000_READ_REG(&sc->hw, E1000_RCTL);
2190 		reg_rctl &= ~E1000_RCTL_RST;
2191 		E1000_WRITE_REG(&sc->hw, E1000_RCTL, reg_rctl);
2192 		msec_delay(5);
2193 		if (sc->hw.bus.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
2194 			e1000_pci_set_mwi(&sc->hw);
2195 	}
2196 }
2197 
2198 /*********************************************************************
2199  *  Timer routine
2200  *
2201  *  This routine schedules em_if_update_admin_status() to check for
2202  *  link status and to gather statistics as well as to perform some
2203  *  controller-specific hardware patting.
2204  *
2205  **********************************************************************/
2206 static void
em_if_timer(if_ctx_t ctx,uint16_t qid)2207 em_if_timer(if_ctx_t ctx, uint16_t qid)
2208 {
2209 	if (qid != 0)
2210 		return;
2211 
2212 	iflib_admin_intr_deferred(ctx);
2213 }
2214 
2215 static void
em_if_update_admin_status(if_ctx_t ctx)2216 em_if_update_admin_status(if_ctx_t ctx)
2217 {
2218 	struct e1000_softc *sc = iflib_get_softc(ctx);
2219 	struct e1000_hw *hw = &sc->hw;
2220 	device_t dev = iflib_get_dev(ctx);
2221 	u32 link_check, thstat, ctrl;
2222 	bool automasked = false;
2223 
2224 	link_check = thstat = ctrl = 0;
2225 	/* Get the cached link value or read phy for real */
2226 	switch (hw->phy.media_type) {
2227 	case e1000_media_type_copper:
2228 		if (hw->mac.get_link_status) {
2229 			if (hw->mac.type == e1000_pch_spt)
2230 				msec_delay(50);
2231 			/* Do the work to read phy */
2232 			e1000_check_for_link(hw);
2233 			link_check = !hw->mac.get_link_status;
2234 			if (link_check) /* ESB2 fix */
2235 				e1000_cfg_on_link_up(hw);
2236 		} else {
2237 			link_check = true;
2238 		}
2239 		break;
2240 	case e1000_media_type_fiber:
2241 		e1000_check_for_link(hw);
2242 		link_check =
2243 		    (E1000_READ_REG(hw, E1000_STATUS) & E1000_STATUS_LU);
2244 		break;
2245 	case e1000_media_type_internal_serdes:
2246 		e1000_check_for_link(hw);
2247 		link_check = hw->mac.serdes_has_link;
2248 		break;
2249 	/* VF device is type_unknown */
2250 	case e1000_media_type_unknown:
2251 		e1000_check_for_link(hw);
2252 		link_check = !hw->mac.get_link_status;
2253 		/* FALLTHROUGH */
2254 	default:
2255 		break;
2256 	}
2257 
2258 	/* Check for thermal downshift or shutdown */
2259 	if (hw->mac.type == e1000_i350) {
2260 		thstat = E1000_READ_REG(hw, E1000_THSTAT);
2261 		ctrl = E1000_READ_REG(hw, E1000_CTRL_EXT);
2262 	}
2263 
2264 	/* Now check for a transition */
2265 	if (link_check && (sc->link_active == 0)) {
2266 		e1000_get_speed_and_duplex(hw, &sc->link_speed,
2267 		    &sc->link_duplex);
2268 		/* Check if we must disable SPEED_MODE bit on PCI-E */
2269 		if ((sc->link_speed != SPEED_1000) &&
2270 		    ((hw->mac.type == e1000_82571) ||
2271 		    (hw->mac.type == e1000_82572))) {
2272 			int tarc0;
2273 			tarc0 = E1000_READ_REG(hw, E1000_TARC(0));
2274 			tarc0 &= ~TARC_SPEED_MODE_BIT;
2275 			E1000_WRITE_REG(hw, E1000_TARC(0), tarc0);
2276 		}
2277 		if (bootverbose)
2278 			device_printf(dev, "Link is up %d Mbps %s\n",
2279 			    sc->link_speed,
2280 			    ((sc->link_duplex == FULL_DUPLEX) ?
2281 			    "Full Duplex" : "Half Duplex"));
2282 		sc->link_active = 1;
2283 		sc->smartspeed = 0;
2284 		if ((ctrl & E1000_CTRL_EXT_LINK_MODE_MASK) ==
2285 		    E1000_CTRL_EXT_LINK_MODE_GMII &&
2286 		    (thstat & E1000_THSTAT_LINK_THROTTLE))
2287 			device_printf(dev, "Link: thermal downshift\n");
2288 		/* Delay Link Up for Phy update */
2289 		if (((hw->mac.type == e1000_i210) ||
2290 		    (hw->mac.type == e1000_i211)) &&
2291 		    (hw->phy.id == I210_I_PHY_ID))
2292 			msec_delay(I210_LINK_DELAY);
2293 		/* Reset if the media type changed. */
2294 		if (hw->dev_spec._82575.media_changed &&
2295 		    hw->mac.type >= igb_mac_min) {
2296 			hw->dev_spec._82575.media_changed = false;
2297 			sc->flags |= IGB_MEDIA_RESET;
2298 			em_reset(ctx);
2299 		}
2300 		/* Only do TSO on gigabit for older chips due to errata */
2301 		if (hw->mac.type < igb_mac_min)
2302 			automasked = em_automask_tso(ctx);
2303 
2304 		/* Automasking resets the interface so don't mark it up yet */
2305 		if (!automasked)
2306 			iflib_link_state_change(ctx, LINK_STATE_UP,
2307 			    IF_Mbps(sc->link_speed));
2308 	} else if (!link_check && (sc->link_active == 1)) {
2309 		sc->link_speed = 0;
2310 		sc->link_duplex = 0;
2311 		sc->link_active = 0;
2312 		iflib_link_state_change(ctx, LINK_STATE_DOWN, 0);
2313 	}
2314 	em_update_stats_counters(sc);
2315 
2316 	/* Reset LAA into RAR[0] on 82571 */
2317 	if (hw->mac.type == e1000_82571 && e1000_get_laa_state_82571(hw))
2318 		e1000_rar_set(hw, hw->mac.addr, 0);
2319 
2320 	if (hw->mac.type < em_mac_min)
2321 		lem_smartspeed(sc);
2322 }
2323 
2324 static void
em_if_watchdog_reset(if_ctx_t ctx)2325 em_if_watchdog_reset(if_ctx_t ctx)
2326 {
2327 	struct e1000_softc *sc = iflib_get_softc(ctx);
2328 
2329 	/*
2330 	 * Just count the event; iflib(4) will already trigger a
2331 	 * sufficient reset of the controller.
2332 	 */
2333 	sc->watchdog_events++;
2334 }
2335 
2336 /*********************************************************************
2337  *
2338  *  This routine disables all traffic on the adapter by issuing a
2339  *  global reset on the MAC.
2340  *
2341  **********************************************************************/
2342 static void
em_if_stop(if_ctx_t ctx)2343 em_if_stop(if_ctx_t ctx)
2344 {
2345 	struct e1000_softc *sc = iflib_get_softc(ctx);
2346 
2347 	INIT_DEBUGOUT("em_if_stop: begin");
2348 
2349 	/* I219 needs special flushing to avoid hangs */
2350 	if (sc->hw.mac.type >= e1000_pch_spt && sc->hw.mac.type < igb_mac_min)
2351 		em_flush_desc_rings(sc);
2352 
2353 	e1000_reset_hw(&sc->hw);
2354 	if (sc->hw.mac.type >= e1000_82544 && !sc->vf_ifp)
2355 		E1000_WRITE_REG(&sc->hw, E1000_WUFC, 0);
2356 
2357 	e1000_led_off(&sc->hw);
2358 	e1000_cleanup_led(&sc->hw);
2359 }
2360 
2361 /*********************************************************************
2362  *
2363  *  Determine hardware revision.
2364  *
2365  **********************************************************************/
2366 static void
em_identify_hardware(if_ctx_t ctx)2367 em_identify_hardware(if_ctx_t ctx)
2368 {
2369 	device_t dev = iflib_get_dev(ctx);
2370 	struct e1000_softc *sc = iflib_get_softc(ctx);
2371 
2372 	/* Make sure our PCI config space has the necessary stuff set */
2373 	sc->hw.bus.pci_cmd_word = pci_read_config(dev, PCIR_COMMAND, 2);
2374 
2375 	/* Save off the information about this board */
2376 	sc->hw.vendor_id = pci_get_vendor(dev);
2377 	sc->hw.device_id = pci_get_device(dev);
2378 	sc->hw.revision_id = pci_read_config(dev, PCIR_REVID, 1);
2379 	sc->hw.subsystem_vendor_id = pci_read_config(dev, PCIR_SUBVEND_0, 2);
2380 	sc->hw.subsystem_device_id = pci_read_config(dev, PCIR_SUBDEV_0, 2);
2381 
2382 	/* Do Shared Code Init and Setup */
2383 	if (e1000_set_mac_type(&sc->hw)) {
2384 		device_printf(dev, "Setup init failure\n");
2385 		return;
2386 	}
2387 
2388 	/* Are we a VF device? */
2389 	if ((sc->hw.mac.type == e1000_vfadapt) ||
2390 	    (sc->hw.mac.type == e1000_vfadapt_i350))
2391 		sc->vf_ifp = 1;
2392 	else
2393 		sc->vf_ifp = 0;
2394 }
2395 
2396 static int
em_allocate_pci_resources(if_ctx_t ctx)2397 em_allocate_pci_resources(if_ctx_t ctx)
2398 {
2399 	struct e1000_softc *sc = iflib_get_softc(ctx);
2400 	device_t dev = iflib_get_dev(ctx);
2401 	int rid, val;
2402 
2403 	rid = PCIR_BAR(0);
2404 	sc->memory = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
2405 	    RF_ACTIVE);
2406 	if (sc->memory == NULL) {
2407 		device_printf(dev,
2408 		    "Unable to allocate bus resource: memory\n");
2409 		return (ENXIO);
2410 	}
2411 	sc->osdep.mem_bus_space_tag = rman_get_bustag(sc->memory);
2412 	sc->osdep.mem_bus_space_handle = rman_get_bushandle(sc->memory);
2413 #ifdef INVARIANTS
2414 	sc->osdep.mem_bus_space_size = rman_get_size(sc->memory);
2415 #endif
2416 	sc->hw.hw_addr = (u8 *)&sc->osdep.mem_bus_space_handle;
2417 
2418 	/* Only older adapters use IO mapping */
2419 	if (sc->hw.mac.type < em_mac_min && sc->hw.mac.type > e1000_82543) {
2420 		/* Figure our where our IO BAR is ? */
2421 		for (rid = PCIR_BAR(0); rid < PCIR_CIS;) {
2422 			val = pci_read_config(dev, rid, 4);
2423 			if (EM_BAR_TYPE(val) == EM_BAR_TYPE_IO) {
2424 				break;
2425 			}
2426 			rid += 4;
2427 			/* check for 64bit BAR */
2428 			if (EM_BAR_MEM_TYPE(val) == EM_BAR_MEM_TYPE_64BIT)
2429 				rid += 4;
2430 		}
2431 		if (rid >= PCIR_CIS) {
2432 			device_printf(dev, "Unable to locate IO BAR\n");
2433 			return (ENXIO);
2434 		}
2435 		sc->ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
2436 		    &rid, RF_ACTIVE);
2437 		if (sc->ioport == NULL) {
2438 			device_printf(dev,
2439 			    "Unable to allocate bus resource: ioport\n");
2440 			return (ENXIO);
2441 		}
2442 		sc->hw.io_base = 0;
2443 		sc->osdep.io_bus_space_tag =
2444 		    rman_get_bustag(sc->ioport);
2445 		sc->osdep.io_bus_space_handle =
2446 		    rman_get_bushandle(sc->ioport);
2447 	}
2448 
2449 	sc->hw.back = &sc->osdep;
2450 
2451 	return (0);
2452 }
2453 
2454 /*********************************************************************
2455  *
2456  *  Set up the MSI-X Interrupt handlers
2457  *
2458  **********************************************************************/
2459 static int
em_if_msix_intr_assign(if_ctx_t ctx,int msix)2460 em_if_msix_intr_assign(if_ctx_t ctx, int msix)
2461 {
2462 	struct e1000_softc *sc = iflib_get_softc(ctx);
2463 	struct em_rx_queue *rx_que = sc->rx_queues;
2464 	struct em_tx_queue *tx_que = sc->tx_queues;
2465 	int error, rid, i, vector = 0, rx_vectors;
2466 	char buf[16];
2467 
2468 	/* First set up ring resources */
2469 	for (i = 0; i < sc->rx_num_queues; i++, rx_que++, vector++) {
2470 		rid = vector + 1;
2471 		snprintf(buf, sizeof(buf), "rxq%d", i);
2472 		error = iflib_irq_alloc_generic(ctx, &rx_que->que_irq, rid,
2473 		    IFLIB_INTR_RXTX, em_msix_que, rx_que, rx_que->me, buf);
2474 		if (error) {
2475 			device_printf(iflib_get_dev(ctx),
2476 			    "Failed to allocate que int %d err: %d",
2477 			    i, error);
2478 			sc->rx_num_queues = i + 1;
2479 			goto fail;
2480 		}
2481 
2482 		rx_que->msix =  vector;
2483 
2484 		/*
2485 		 * Set the bit to enable interrupt
2486 		 * in E1000_IMS -- bits 20 and 21
2487 		 * are for RX0 and RX1, note this has
2488 		 * NOTHING to do with the MSI-X vector
2489 		 */
2490 		if (sc->hw.mac.type == e1000_82574) {
2491 			rx_que->eims = 1 << (20 + i);
2492 			sc->ims |= rx_que->eims;
2493 			sc->ivars |= (8 | rx_que->msix) << (i * 4);
2494 		} else if (sc->hw.mac.type == e1000_82575)
2495 			rx_que->eims = E1000_EICR_TX_QUEUE0 << vector;
2496 		else
2497 			rx_que->eims = 1 << vector;
2498 	}
2499 	rx_vectors = vector;
2500 
2501 	vector = 0;
2502 	for (i = 0; i < sc->tx_num_queues; i++, tx_que++, vector++) {
2503 		snprintf(buf, sizeof(buf), "txq%d", i);
2504 		tx_que = &sc->tx_queues[i];
2505 		iflib_softirq_alloc_generic(ctx,
2506 		    &sc->rx_queues[i % sc->rx_num_queues].que_irq,
2507 		    IFLIB_INTR_TX, tx_que, tx_que->me, buf);
2508 
2509 		tx_que->msix = (vector % sc->rx_num_queues);
2510 
2511 		/*
2512 		 * Set the bit to enable interrupt
2513 		 * in E1000_IMS -- bits 22 and 23
2514 		 * are for TX0 and TX1, note this has
2515 		 * NOTHING to do with the MSI-X vector
2516 		 */
2517 		if (sc->hw.mac.type == e1000_82574) {
2518 			tx_que->eims = 1 << (22 + i);
2519 			sc->ims |= tx_que->eims;
2520 			sc->ivars |= (8 | tx_que->msix) << (8 + (i * 4));
2521 		} else if (sc->hw.mac.type == e1000_82575) {
2522 			tx_que->eims = E1000_EICR_TX_QUEUE0 << i;
2523 		} else {
2524 			tx_que->eims = 1 << i;
2525 		}
2526 	}
2527 
2528 	/* Link interrupt */
2529 	rid = rx_vectors + 1;
2530 	error = iflib_irq_alloc_generic(ctx, &sc->irq, rid, IFLIB_INTR_ADMIN,
2531 	    em_msix_link, sc, 0, "aq");
2532 
2533 	if (error) {
2534 		device_printf(iflib_get_dev(ctx),
2535 		    "Failed to register admin handler");
2536 		goto fail;
2537 	}
2538 	sc->linkvec = rx_vectors;
2539 	if (sc->hw.mac.type < igb_mac_min) {
2540 		sc->ivars |=  (8 | rx_vectors) << 16;
2541 		sc->ivars |= 0x80000000;
2542 		/* Enable the "Other" interrupt type for link status change */
2543 		sc->ims |= E1000_IMS_OTHER;
2544 	}
2545 
2546 	return (0);
2547 fail:
2548 	iflib_irq_free(ctx, &sc->irq);
2549 	rx_que = sc->rx_queues;
2550 	for (int i = 0; i < sc->rx_num_queues; i++, rx_que++)
2551 		iflib_irq_free(ctx, &rx_que->que_irq);
2552 	return (error);
2553 }
2554 
2555 static void
igb_configure_queues(struct e1000_softc * sc)2556 igb_configure_queues(struct e1000_softc *sc)
2557 {
2558 	struct e1000_hw *hw = &sc->hw;
2559 	struct em_rx_queue *rx_que;
2560 	struct em_tx_queue *tx_que;
2561 	u32 tmp, ivar = 0, newitr = 0;
2562 
2563 	/* First turn on RSS capability */
2564 	if (hw->mac.type != e1000_82575)
2565 		E1000_WRITE_REG(hw, E1000_GPIE,
2566 		    E1000_GPIE_MSIX_MODE | E1000_GPIE_EIAME |
2567 		    E1000_GPIE_PBA | E1000_GPIE_NSICR);
2568 
2569 	/* Turn on MSI-X */
2570 	switch (hw->mac.type) {
2571 	case e1000_82580:
2572 	case e1000_i350:
2573 	case e1000_i354:
2574 	case e1000_i210:
2575 	case e1000_i211:
2576 	case e1000_vfadapt:
2577 	case e1000_vfadapt_i350:
2578 		/* RX entries */
2579 		for (int i = 0; i < sc->rx_num_queues; i++) {
2580 			u32 index = i >> 1;
2581 			ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
2582 			rx_que = &sc->rx_queues[i];
2583 			if (i & 1) {
2584 				ivar &= 0xFF00FFFF;
2585 				ivar |= (rx_que->msix | E1000_IVAR_VALID) <<
2586 				    16;
2587 			} else {
2588 				ivar &= 0xFFFFFF00;
2589 				ivar |= rx_que->msix | E1000_IVAR_VALID;
2590 			}
2591 			E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
2592 		}
2593 		/* TX entries */
2594 		for (int i = 0; i < sc->tx_num_queues; i++) {
2595 			u32 index = i >> 1;
2596 			ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
2597 			tx_que = &sc->tx_queues[i];
2598 			if (i & 1) {
2599 				ivar &= 0x00FFFFFF;
2600 				ivar |= (tx_que->msix | E1000_IVAR_VALID) <<
2601 				    24;
2602 			} else {
2603 				ivar &= 0xFFFF00FF;
2604 				ivar |= (tx_que->msix | E1000_IVAR_VALID) <<
2605 				    8;
2606 			}
2607 			E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
2608 			sc->que_mask |= tx_que->eims;
2609 		}
2610 
2611 		/* And for the link interrupt */
2612 		ivar = (sc->linkvec | E1000_IVAR_VALID) << 8;
2613 		sc->link_mask = 1 << sc->linkvec;
2614 		E1000_WRITE_REG(hw, E1000_IVAR_MISC, ivar);
2615 		break;
2616 	case e1000_82576:
2617 		/* RX entries */
2618 		for (int i = 0; i < sc->rx_num_queues; i++) {
2619 			u32 index = i & 0x7; /* Each IVAR has two entries */
2620 			ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
2621 			rx_que = &sc->rx_queues[i];
2622 			if (i < 8) {
2623 				ivar &= 0xFFFFFF00;
2624 				ivar |= rx_que->msix | E1000_IVAR_VALID;
2625 			} else {
2626 				ivar &= 0xFF00FFFF;
2627 				ivar |= (rx_que->msix | E1000_IVAR_VALID) <<
2628 				    16;
2629 			}
2630 			E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
2631 			sc->que_mask |= rx_que->eims;
2632 		}
2633 		/* TX entries */
2634 		for (int i = 0; i < sc->tx_num_queues; i++) {
2635 			u32 index = i & 0x7; /* Each IVAR has two entries */
2636 			ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
2637 			tx_que = &sc->tx_queues[i];
2638 			if (i < 8) {
2639 				ivar &= 0xFFFF00FF;
2640 				ivar |= (tx_que->msix | E1000_IVAR_VALID) <<
2641 				    8;
2642 			} else {
2643 				ivar &= 0x00FFFFFF;
2644 				ivar |= (tx_que->msix | E1000_IVAR_VALID) <<
2645 				    24;
2646 			}
2647 			E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
2648 			sc->que_mask |= tx_que->eims;
2649 		}
2650 
2651 		/* And for the link interrupt */
2652 		ivar = (sc->linkvec | E1000_IVAR_VALID) << 8;
2653 		sc->link_mask = 1 << sc->linkvec;
2654 		E1000_WRITE_REG(hw, E1000_IVAR_MISC, ivar);
2655 		break;
2656 
2657 	case e1000_82575:
2658 		/* enable MSI-X support*/
2659 		tmp = E1000_READ_REG(hw, E1000_CTRL_EXT);
2660 		tmp |= E1000_CTRL_EXT_PBA_CLR;
2661 		/* Auto-Mask interrupts upon ICR read. */
2662 		tmp |= E1000_CTRL_EXT_EIAME;
2663 		tmp |= E1000_CTRL_EXT_IRCA;
2664 		E1000_WRITE_REG(hw, E1000_CTRL_EXT, tmp);
2665 
2666 		/* Queues */
2667 		for (int i = 0; i < sc->rx_num_queues; i++) {
2668 			rx_que = &sc->rx_queues[i];
2669 			tmp = E1000_EICR_RX_QUEUE0 << i;
2670 			tmp |= E1000_EICR_TX_QUEUE0 << i;
2671 			rx_que->eims = tmp;
2672 			E1000_WRITE_REG_ARRAY(hw, E1000_MSIXBM(0), i,
2673 			    rx_que->eims);
2674 			sc->que_mask |= rx_que->eims;
2675 		}
2676 
2677 		/* Link */
2678 		E1000_WRITE_REG(hw, E1000_MSIXBM(sc->linkvec),
2679 		    E1000_EIMS_OTHER);
2680 		sc->link_mask |= E1000_EIMS_OTHER;
2681 	default:
2682 		break;
2683 	}
2684 
2685 	/* Set the igb starting interrupt rate */
2686 	if (em_max_interrupt_rate > 0) {
2687 		newitr = IGB_INTS_TO_EITR(em_max_interrupt_rate);
2688 
2689 		if (hw->mac.type == e1000_82575)
2690 			newitr |= newitr << 16;
2691 		else
2692 			newitr |= E1000_EITR_CNT_IGNR;
2693 
2694 		for (int i = 0; i < sc->rx_num_queues; i++) {
2695 			rx_que = &sc->rx_queues[i];
2696 			E1000_WRITE_REG(hw, E1000_EITR(rx_que->msix), newitr);
2697 		}
2698 	}
2699 
2700 	return;
2701 }
2702 
2703 static void
em_free_pci_resources(if_ctx_t ctx)2704 em_free_pci_resources(if_ctx_t ctx)
2705 {
2706 	struct e1000_softc *sc = iflib_get_softc(ctx);
2707 	struct em_rx_queue *que = sc->rx_queues;
2708 	device_t dev = iflib_get_dev(ctx);
2709 
2710 	/* Release all MSI-X queue resources */
2711 	if (sc->intr_type == IFLIB_INTR_MSIX)
2712 		iflib_irq_free(ctx, &sc->irq);
2713 
2714 	if (que != NULL) {
2715 		for (int i = 0; i < sc->rx_num_queues; i++, que++) {
2716 			iflib_irq_free(ctx, &que->que_irq);
2717 		}
2718 	}
2719 
2720 	if (sc->memory != NULL) {
2721 		bus_release_resource(dev, SYS_RES_MEMORY,
2722 		    rman_get_rid(sc->memory), sc->memory);
2723 		sc->memory = NULL;
2724 	}
2725 
2726 	if (sc->flash != NULL) {
2727 		bus_release_resource(dev, SYS_RES_MEMORY,
2728 		    rman_get_rid(sc->flash), sc->flash);
2729 		sc->flash = NULL;
2730 	}
2731 
2732 	if (sc->ioport != NULL) {
2733 		bus_release_resource(dev, SYS_RES_IOPORT,
2734 		    rman_get_rid(sc->ioport), sc->ioport);
2735 		sc->ioport = NULL;
2736 	}
2737 }
2738 
2739 /* Set up MSI or MSI-X */
2740 static int
em_setup_msix(if_ctx_t ctx)2741 em_setup_msix(if_ctx_t ctx)
2742 {
2743 	struct e1000_softc *sc = iflib_get_softc(ctx);
2744 
2745 	if (sc->hw.mac.type == e1000_82574) {
2746 		em_enable_vectors_82574(ctx);
2747 	}
2748 	return (0);
2749 }
2750 
2751 /*********************************************************************
2752  *
2753  *  Workaround for SmartSpeed on 82541 and 82547 controllers
2754  *
2755  **********************************************************************/
2756 static void
lem_smartspeed(struct e1000_softc * sc)2757 lem_smartspeed(struct e1000_softc *sc)
2758 {
2759 	u16 phy_tmp;
2760 
2761 	if (sc->link_active || (sc->hw.phy.type != e1000_phy_igp) ||
2762 	    sc->hw.mac.autoneg == 0 ||
2763 	    (sc->hw.phy.autoneg_advertised & ADVERTISE_1000_FULL) == 0)
2764 		return;
2765 
2766 	if (sc->smartspeed == 0) {
2767 		/* If Master/Slave config fault is asserted twice,
2768 		 * we assume back-to-back */
2769 		e1000_read_phy_reg(&sc->hw, PHY_1000T_STATUS, &phy_tmp);
2770 		if (!(phy_tmp & SR_1000T_MS_CONFIG_FAULT))
2771 			return;
2772 		e1000_read_phy_reg(&sc->hw, PHY_1000T_STATUS, &phy_tmp);
2773 		if (phy_tmp & SR_1000T_MS_CONFIG_FAULT) {
2774 			e1000_read_phy_reg(&sc->hw,
2775 			    PHY_1000T_CTRL, &phy_tmp);
2776 			if(phy_tmp & CR_1000T_MS_ENABLE) {
2777 				phy_tmp &= ~CR_1000T_MS_ENABLE;
2778 				e1000_write_phy_reg(&sc->hw,
2779 				    PHY_1000T_CTRL, phy_tmp);
2780 				sc->smartspeed++;
2781 				if(sc->hw.mac.autoneg &&
2782 				   !e1000_copper_link_autoneg(&sc->hw) &&
2783 				   !e1000_read_phy_reg(&sc->hw,
2784 				    PHY_CONTROL, &phy_tmp)) {
2785 					phy_tmp |= (MII_CR_AUTO_NEG_EN |
2786 						    MII_CR_RESTART_AUTO_NEG);
2787 					e1000_write_phy_reg(&sc->hw,
2788 					    PHY_CONTROL, phy_tmp);
2789 				}
2790 			}
2791 		}
2792 		return;
2793 	} else if(sc->smartspeed == EM_SMARTSPEED_DOWNSHIFT) {
2794 		/* If still no link, perhaps using 2/3 pair cable */
2795 		e1000_read_phy_reg(&sc->hw, PHY_1000T_CTRL, &phy_tmp);
2796 		phy_tmp |= CR_1000T_MS_ENABLE;
2797 		e1000_write_phy_reg(&sc->hw, PHY_1000T_CTRL, phy_tmp);
2798 		if(sc->hw.mac.autoneg &&
2799 		   !e1000_copper_link_autoneg(&sc->hw) &&
2800 		   !e1000_read_phy_reg(&sc->hw, PHY_CONTROL, &phy_tmp)) {
2801 			phy_tmp |= (MII_CR_AUTO_NEG_EN |
2802 				    MII_CR_RESTART_AUTO_NEG);
2803 			e1000_write_phy_reg(&sc->hw, PHY_CONTROL, phy_tmp);
2804 		}
2805 	}
2806 	/* Restart process after EM_SMARTSPEED_MAX iterations */
2807 	if(sc->smartspeed++ == EM_SMARTSPEED_MAX)
2808 		sc->smartspeed = 0;
2809 }
2810 
2811 /*********************************************************************
2812  *
2813  *  Initialize the DMA Coalescing feature
2814  *
2815  **********************************************************************/
2816 static void
igb_init_dmac(struct e1000_softc * sc,u32 pba)2817 igb_init_dmac(struct e1000_softc *sc, u32 pba)
2818 {
2819 	device_t	dev = sc->dev;
2820 	struct e1000_hw *hw = &sc->hw;
2821 	u32 		dmac, reg = ~E1000_DMACR_DMAC_EN;
2822 	u16		hwm;
2823 	u16		max_frame_size;
2824 
2825 	if (hw->mac.type == e1000_i211)
2826 		return;
2827 
2828 	max_frame_size = sc->shared->isc_max_frame_size;
2829 	if (hw->mac.type > e1000_82580) {
2830 
2831 		if (sc->dmac == 0) { /* Disabling it */
2832 			E1000_WRITE_REG(hw, E1000_DMACR, reg);
2833 			return;
2834 		} else
2835 			device_printf(dev, "DMA Coalescing enabled\n");
2836 
2837 		/* Set starting threshold */
2838 		E1000_WRITE_REG(hw, E1000_DMCTXTH, 0);
2839 
2840 		hwm = 64 * pba - max_frame_size / 16;
2841 		if (hwm < 64 * (pba - 6))
2842 			hwm = 64 * (pba - 6);
2843 		reg = E1000_READ_REG(hw, E1000_FCRTC);
2844 		reg &= ~E1000_FCRTC_RTH_COAL_MASK;
2845 		reg |= ((hwm << E1000_FCRTC_RTH_COAL_SHIFT)
2846 		    & E1000_FCRTC_RTH_COAL_MASK);
2847 		E1000_WRITE_REG(hw, E1000_FCRTC, reg);
2848 
2849 
2850 		dmac = pba - max_frame_size / 512;
2851 		if (dmac < pba - 10)
2852 			dmac = pba - 10;
2853 		reg = E1000_READ_REG(hw, E1000_DMACR);
2854 		reg &= ~E1000_DMACR_DMACTHR_MASK;
2855 		reg |= ((dmac << E1000_DMACR_DMACTHR_SHIFT)
2856 		    & E1000_DMACR_DMACTHR_MASK);
2857 
2858 		/* transition to L0x or L1 if available..*/
2859 		reg |= (E1000_DMACR_DMAC_EN | E1000_DMACR_DMAC_LX_MASK);
2860 
2861 		/* Check if status is 2.5Gb backplane connection
2862 		* before configuration of watchdog timer, which is
2863 		* in msec values in 12.8usec intervals
2864 		* watchdog timer= msec values in 32usec intervals
2865 		* for non 2.5Gb connection
2866 		*/
2867 		if (hw->mac.type == e1000_i354) {
2868 			int status = E1000_READ_REG(hw, E1000_STATUS);
2869 			if ((status & E1000_STATUS_2P5_SKU) &&
2870 			    (!(status & E1000_STATUS_2P5_SKU_OVER)))
2871 				reg |= ((sc->dmac * 5) >> 6);
2872 			else
2873 				reg |= (sc->dmac >> 5);
2874 		} else {
2875 			reg |= (sc->dmac >> 5);
2876 		}
2877 
2878 		E1000_WRITE_REG(hw, E1000_DMACR, reg);
2879 
2880 		E1000_WRITE_REG(hw, E1000_DMCRTRH, 0);
2881 
2882 		/* Set the interval before transition */
2883 		reg = E1000_READ_REG(hw, E1000_DMCTLX);
2884 		if (hw->mac.type == e1000_i350)
2885 			reg |= IGB_DMCTLX_DCFLUSH_DIS;
2886 		/*
2887 		** in 2.5Gb connection, TTLX unit is 0.4 usec
2888 		** which is 0x4*2 = 0xA. But delay is still 4 usec
2889 		*/
2890 		if (hw->mac.type == e1000_i354) {
2891 			int status = E1000_READ_REG(hw, E1000_STATUS);
2892 			if ((status & E1000_STATUS_2P5_SKU) &&
2893 			    (!(status & E1000_STATUS_2P5_SKU_OVER)))
2894 				reg |= 0xA;
2895 			else
2896 				reg |= 0x4;
2897 		} else {
2898 			reg |= 0x4;
2899 		}
2900 
2901 		E1000_WRITE_REG(hw, E1000_DMCTLX, reg);
2902 
2903 		/* free space in tx packet buffer to wake from DMA coal */
2904 		E1000_WRITE_REG(hw, E1000_DMCTXTH, (IGB_TXPBSIZE -
2905 		    (2 * max_frame_size)) >> 6);
2906 
2907 		/* make low power state decision controlled by DMA coal */
2908 		reg = E1000_READ_REG(hw, E1000_PCIEMISC);
2909 		reg &= ~E1000_PCIEMISC_LX_DECISION;
2910 		E1000_WRITE_REG(hw, E1000_PCIEMISC, reg);
2911 
2912 	} else if (hw->mac.type == e1000_82580) {
2913 		u32 reg = E1000_READ_REG(hw, E1000_PCIEMISC);
2914 		E1000_WRITE_REG(hw, E1000_PCIEMISC,
2915 		    reg & ~E1000_PCIEMISC_LX_DECISION);
2916 		E1000_WRITE_REG(hw, E1000_DMACR, 0);
2917 	}
2918 }
2919 /*********************************************************************
2920  * The 3 following flush routines are used as a workaround in the
2921  * I219 client parts and only for them.
2922  *
2923  * em_flush_tx_ring - remove all descriptors from the tx_ring
2924  *
2925  * We want to clear all pending descriptors from the TX ring.
2926  * zeroing happens when the HW reads the regs. We assign the ring itself as
2927  * the data of the next descriptor. We don't care about the data we are about
2928  * to reset the HW.
2929  **********************************************************************/
2930 static void
em_flush_tx_ring(struct e1000_softc * sc)2931 em_flush_tx_ring(struct e1000_softc *sc)
2932 {
2933 	struct e1000_hw *hw = &sc->hw;
2934 	struct tx_ring *txr = &sc->tx_queues->txr;
2935 	struct e1000_tx_desc *txd;
2936 	u32 tctl, txd_lower = E1000_TXD_CMD_IFCS;
2937 	u16 size = 512;
2938 
2939 	tctl = E1000_READ_REG(hw, E1000_TCTL);
2940 	E1000_WRITE_REG(hw, E1000_TCTL, tctl | E1000_TCTL_EN);
2941 
2942 	txd = &txr->tx_base[txr->tx_cidx_processed];
2943 
2944 	/* Just use the ring as a dummy buffer addr */
2945 	txd->buffer_addr = txr->tx_paddr;
2946 	txd->lower.data = htole32(txd_lower | size);
2947 	txd->upper.data = 0;
2948 
2949 	/* flush descriptors to memory before notifying the HW */
2950 	wmb();
2951 
2952 	E1000_WRITE_REG(hw, E1000_TDT(0), txr->tx_cidx_processed);
2953 	mb();
2954 	usec_delay(250);
2955 }
2956 
2957 /*********************************************************************
2958  * em_flush_rx_ring - remove all descriptors from the rx_ring
2959  *
2960  * Mark all descriptors in the RX ring as consumed and disable the rx ring
2961  **********************************************************************/
2962 static void
em_flush_rx_ring(struct e1000_softc * sc)2963 em_flush_rx_ring(struct e1000_softc *sc)
2964 {
2965 	struct e1000_hw *hw = &sc->hw;
2966 	u32 rctl, rxdctl;
2967 
2968 	rctl = E1000_READ_REG(hw, E1000_RCTL);
2969 	E1000_WRITE_REG(hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
2970 	E1000_WRITE_FLUSH(hw);
2971 	usec_delay(150);
2972 
2973 	rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(0));
2974 	/* zero the lower 14 bits (prefetch and host thresholds) */
2975 	rxdctl &= 0xffffc000;
2976 	/*
2977 	 * update thresholds: prefetch threshold to 31, host threshold to 1
2978 	 * and make sure the granularity is "descriptors" and not
2979 	 * "cache lines"
2980 	 */
2981 	rxdctl |= (0x1F | (1 << 8) | E1000_RXDCTL_THRESH_UNIT_DESC);
2982 	E1000_WRITE_REG(hw, E1000_RXDCTL(0), rxdctl);
2983 
2984 	/* momentarily enable the RX ring for the changes to take effect */
2985 	E1000_WRITE_REG(hw, E1000_RCTL, rctl | E1000_RCTL_EN);
2986 	E1000_WRITE_FLUSH(hw);
2987 	usec_delay(150);
2988 	E1000_WRITE_REG(hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
2989 }
2990 
2991 /*********************************************************************
2992  * em_flush_desc_rings - remove all descriptors from the descriptor rings
2993  *
2994  * In I219, the descriptor rings must be emptied before resetting the HW
2995  * or before changing the device state to D3 during runtime (runtime PM).
2996  *
2997  * Failure to do this will cause the HW to enter a unit hang state which can
2998  * only be released by PCI reset on the device
2999  *
3000  **********************************************************************/
3001 static void
em_flush_desc_rings(struct e1000_softc * sc)3002 em_flush_desc_rings(struct e1000_softc *sc)
3003 {
3004 	struct e1000_hw	*hw = &sc->hw;
3005 	device_t dev = sc->dev;
3006 	u16 hang_state;
3007 	u32 fext_nvm11, tdlen;
3008 
3009 	/* First, disable MULR fix in FEXTNVM11 */
3010 	fext_nvm11 = E1000_READ_REG(hw, E1000_FEXTNVM11);
3011 	fext_nvm11 |= E1000_FEXTNVM11_DISABLE_MULR_FIX;
3012 	E1000_WRITE_REG(hw, E1000_FEXTNVM11, fext_nvm11);
3013 
3014 	/* do nothing if we're not in faulty state, or the queue is empty */
3015 	tdlen = E1000_READ_REG(hw, E1000_TDLEN(0));
3016 	hang_state = pci_read_config(dev, PCICFG_DESC_RING_STATUS, 2);
3017 	if (!(hang_state & FLUSH_DESC_REQUIRED) || !tdlen)
3018 		return;
3019 	em_flush_tx_ring(sc);
3020 
3021 	/* recheck, maybe the fault is caused by the rx ring */
3022 	hang_state = pci_read_config(dev, PCICFG_DESC_RING_STATUS, 2);
3023 	if (hang_state & FLUSH_DESC_REQUIRED)
3024 		em_flush_rx_ring(sc);
3025 }
3026 
3027 
3028 /*********************************************************************
3029  *
3030  *  Initialize the hardware to a configuration as specified by the
3031  *  sc structure.
3032  *
3033  **********************************************************************/
3034 static void
em_reset(if_ctx_t ctx)3035 em_reset(if_ctx_t ctx)
3036 {
3037 	device_t dev = iflib_get_dev(ctx);
3038 	struct e1000_softc *sc = iflib_get_softc(ctx);
3039 	if_t ifp = iflib_get_ifp(ctx);
3040 	struct e1000_hw *hw = &sc->hw;
3041 	u32 rx_buffer_size;
3042 	u32 pba;
3043 
3044 	INIT_DEBUGOUT("em_reset: begin");
3045 	/* Let the firmware know the OS is in control */
3046 	em_get_hw_control(sc);
3047 
3048 	/* Set up smart power down as default off on newer adapters. */
3049 	if (!em_smart_pwr_down && (hw->mac.type == e1000_82571 ||
3050 	    hw->mac.type == e1000_82572)) {
3051 		u16 phy_tmp = 0;
3052 
3053 		/* Speed up time to link by disabling smart power down. */
3054 		e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT, &phy_tmp);
3055 		phy_tmp &= ~IGP02E1000_PM_SPD;
3056 		e1000_write_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT, phy_tmp);
3057 	}
3058 
3059 	/*
3060 	 * Packet Buffer Allocation (PBA)
3061 	 * Writing PBA sets the receive portion of the buffer
3062 	 * the remainder is used for the transmit buffer.
3063 	 */
3064 	switch (hw->mac.type) {
3065 	/* 82547: Total Packet Buffer is 40K */
3066 	case e1000_82547:
3067 	case e1000_82547_rev_2:
3068 		if (hw->mac.max_frame_size > 8192)
3069 			pba = E1000_PBA_22K; /* 22K for Rx, 18K for Tx */
3070 		else
3071 			pba = E1000_PBA_30K; /* 30K for Rx, 10K for Tx */
3072 		break;
3073 	/* 82571/82572/80003es2lan: Total Packet Buffer is 48K */
3074 	case e1000_82571:
3075 	case e1000_82572:
3076 	case e1000_80003es2lan:
3077 			pba = E1000_PBA_32K; /* 32K for Rx, 16K for Tx */
3078 		break;
3079 	/* 82573: Total Packet Buffer is 32K */
3080 	case e1000_82573:
3081 			pba = E1000_PBA_12K; /* 12K for Rx, 20K for Tx */
3082 		break;
3083 	case e1000_82574:
3084 	case e1000_82583:
3085 			pba = E1000_PBA_20K; /* 20K for Rx, 20K for Tx */
3086 		break;
3087 	case e1000_ich8lan:
3088 		pba = E1000_PBA_8K;
3089 		break;
3090 	case e1000_ich9lan:
3091 	case e1000_ich10lan:
3092 		/* Boost Receive side for jumbo frames */
3093 		if (hw->mac.max_frame_size > 4096)
3094 			pba = E1000_PBA_14K;
3095 		else
3096 			pba = E1000_PBA_10K;
3097 		break;
3098 	case e1000_pchlan:
3099 	case e1000_pch2lan:
3100 	case e1000_pch_lpt:
3101 	case e1000_pch_spt:
3102 	case e1000_pch_cnp:
3103 	case e1000_pch_tgp:
3104 	case e1000_pch_adp:
3105 	case e1000_pch_mtp:
3106 	case e1000_pch_ptp:
3107 		pba = E1000_PBA_26K;
3108 		break;
3109 	case e1000_82575:
3110 		pba = E1000_PBA_32K;
3111 		break;
3112 	case e1000_82576:
3113 	case e1000_vfadapt:
3114 		pba = E1000_READ_REG(hw, E1000_RXPBS);
3115 		pba &= E1000_RXPBS_SIZE_MASK_82576;
3116 		break;
3117 	case e1000_82580:
3118 	case e1000_i350:
3119 	case e1000_i354:
3120 	case e1000_vfadapt_i350:
3121 		pba = E1000_READ_REG(hw, E1000_RXPBS);
3122 		pba = e1000_rxpbs_adjust_82580(pba);
3123 		break;
3124 	case e1000_i210:
3125 	case e1000_i211:
3126 		pba = E1000_PBA_34K;
3127 		break;
3128 	default:
3129 		/* Remaining devices assumed to have Packet Buffer of 64K. */
3130 		if (hw->mac.max_frame_size > 8192)
3131 			pba = E1000_PBA_40K; /* 40K for Rx, 24K for Tx */
3132 		else
3133 			pba = E1000_PBA_48K; /* 48K for Rx, 16K for Tx */
3134 	}
3135 
3136 	/* Special needs in case of Jumbo frames */
3137 	if ((hw->mac.type == e1000_82575) && (if_getmtu(ifp) > ETHERMTU)) {
3138 		u32 tx_space, min_tx, min_rx;
3139 		pba = E1000_READ_REG(hw, E1000_PBA);
3140 		tx_space = pba >> 16;
3141 		pba &= 0xffff;
3142 		min_tx = (hw->mac.max_frame_size +
3143 		    sizeof(struct e1000_tx_desc) - ETHERNET_FCS_SIZE) * 2;
3144 		min_tx = roundup2(min_tx, 1024);
3145 		min_tx >>= 10;
3146 		min_rx = hw->mac.max_frame_size;
3147 		min_rx = roundup2(min_rx, 1024);
3148 		min_rx >>= 10;
3149 		if (tx_space < min_tx &&
3150 		    ((min_tx - tx_space) < pba)) {
3151 			pba = pba - (min_tx - tx_space);
3152 			/*
3153 			 * if short on rx space, rx wins
3154 			 * and must trump tx adjustment
3155 			 */
3156 			if (pba < min_rx)
3157 				pba = min_rx;
3158 		}
3159 		E1000_WRITE_REG(hw, E1000_PBA, pba);
3160 	}
3161 
3162 	if (hw->mac.type < igb_mac_min)
3163 		E1000_WRITE_REG(hw, E1000_PBA, pba);
3164 
3165 	INIT_DEBUGOUT1("em_reset: pba=%dK",pba);
3166 
3167 	/*
3168 	 * These parameters control the automatic generation (Tx) and
3169 	 * response (Rx) to Ethernet PAUSE frames.
3170 	 * - High water mark should allow for at least two frames to be
3171 	 *   received after sending an XOFF.
3172 	 * - Low water mark works best when it is very near the high water
3173 	     mark.
3174 	 *   This allows the receiver to restart by sending XON when it has
3175 	 *   drained a bit. Here we use an arbitrary value of 1500 which will
3176 	 *   restart after one full frame is pulled from the buffer. There
3177 	 *   could be several smaller frames in the buffer and if so they will
3178 	 *   not trigger the XON until their total number reduces the buffer
3179 	 *   by 1500.
3180 	 * - The pause time is fairly large at 1000 x 512ns = 512 usec.
3181 	 */
3182 	rx_buffer_size = (pba & 0xffff) << 10;
3183 	hw->fc.high_water = rx_buffer_size -
3184 	    roundup2(hw->mac.max_frame_size, 1024);
3185 	hw->fc.low_water = hw->fc.high_water - 1500;
3186 
3187 	if (sc->fc) /* locally set flow control value? */
3188 		hw->fc.requested_mode = sc->fc;
3189 	else
3190 		hw->fc.requested_mode = e1000_fc_full;
3191 
3192 	if (hw->mac.type == e1000_80003es2lan)
3193 		hw->fc.pause_time = 0xFFFF;
3194 	else
3195 		hw->fc.pause_time = EM_FC_PAUSE_TIME;
3196 
3197 	hw->fc.send_xon = true;
3198 
3199 	/* Device specific overrides/settings */
3200 	switch (hw->mac.type) {
3201 	case e1000_pchlan:
3202 		/* Workaround: no TX flow ctrl for PCH */
3203 		hw->fc.requested_mode = e1000_fc_rx_pause;
3204 		hw->fc.pause_time = 0xFFFF; /* override */
3205 		if (if_getmtu(ifp) > ETHERMTU) {
3206 			hw->fc.high_water = 0x3500;
3207 			hw->fc.low_water = 0x1500;
3208 		} else {
3209 			hw->fc.high_water = 0x5000;
3210 			hw->fc.low_water = 0x3000;
3211 		}
3212 		hw->fc.refresh_time = 0x1000;
3213 		break;
3214 	case e1000_pch2lan:
3215 	case e1000_pch_lpt:
3216 	case e1000_pch_spt:
3217 	case e1000_pch_cnp:
3218 	case e1000_pch_tgp:
3219 	case e1000_pch_adp:
3220 	case e1000_pch_mtp:
3221 	case e1000_pch_ptp:
3222 		hw->fc.high_water = 0x5C20;
3223 		hw->fc.low_water = 0x5048;
3224 		hw->fc.pause_time = 0x0650;
3225 		hw->fc.refresh_time = 0x0400;
3226 		/* Jumbos need adjusted PBA */
3227 		if (if_getmtu(ifp) > ETHERMTU)
3228 			E1000_WRITE_REG(hw, E1000_PBA, 12);
3229 		else
3230 			E1000_WRITE_REG(hw, E1000_PBA, 26);
3231 		break;
3232 	case e1000_82575:
3233 	case e1000_82576:
3234 		/* 8-byte granularity */
3235 		hw->fc.low_water = hw->fc.high_water - 8;
3236 		break;
3237 	case e1000_82580:
3238 	case e1000_i350:
3239 	case e1000_i354:
3240 	case e1000_i210:
3241 	case e1000_i211:
3242 	case e1000_vfadapt:
3243 	case e1000_vfadapt_i350:
3244 		/* 16-byte granularity */
3245 		hw->fc.low_water = hw->fc.high_water - 16;
3246 		break;
3247 	case e1000_ich9lan:
3248 	case e1000_ich10lan:
3249 		if (if_getmtu(ifp) > ETHERMTU) {
3250 			hw->fc.high_water = 0x2800;
3251 			hw->fc.low_water = hw->fc.high_water - 8;
3252 			break;
3253 		}
3254 		/* FALLTHROUGH */
3255 	default:
3256 		if (hw->mac.type == e1000_80003es2lan)
3257 			hw->fc.pause_time = 0xFFFF;
3258 		break;
3259 	}
3260 
3261 	/* I219 needs some special flushing to avoid hangs */
3262 	if (sc->hw.mac.type >= e1000_pch_spt && sc->hw.mac.type < igb_mac_min)
3263 		em_flush_desc_rings(sc);
3264 
3265 	/* Issue a global reset */
3266 	e1000_reset_hw(hw);
3267 	if (!sc->vf_ifp) {
3268 		if (hw->mac.type >= igb_mac_min) {
3269 			E1000_WRITE_REG(hw, E1000_WUC, 0);
3270 		} else {
3271 			E1000_WRITE_REG(hw, E1000_WUFC, 0);
3272 			em_disable_aspm(sc);
3273 		}
3274 	}
3275 	if (sc->flags & IGB_MEDIA_RESET) {
3276 		e1000_setup_init_funcs(hw, true);
3277 		e1000_get_bus_info(hw);
3278 		sc->flags &= ~IGB_MEDIA_RESET;
3279 	}
3280 	/* and a re-init */
3281 	if (e1000_init_hw(hw) < 0) {
3282 		device_printf(dev, "Hardware Initialization Failed\n");
3283 		return;
3284 	}
3285 	if (hw->mac.type >= igb_mac_min)
3286 		igb_init_dmac(sc, pba);
3287 
3288 	/* Save the final PBA off if it needs to be used elsewhere i.e. AIM */
3289 	sc->pba = pba;
3290 
3291 	E1000_WRITE_REG(hw, E1000_VET, ETHERTYPE_VLAN);
3292 	e1000_get_phy_info(hw);
3293 	e1000_check_for_link(hw);
3294 }
3295 
3296 /*
3297  * Initialise the RSS mapping for NICs that support multiple transmit/
3298  * receive rings.
3299  */
3300 
3301 #define RSSKEYLEN 10
3302 static void
em_initialize_rss_mapping(struct e1000_softc * sc)3303 em_initialize_rss_mapping(struct e1000_softc *sc)
3304 {
3305 	uint8_t rss_key[4 * RSSKEYLEN];
3306 	uint32_t reta = 0;
3307 	struct e1000_hw *hw = &sc->hw;
3308 	int i;
3309 
3310 	/*
3311 	 * Configure RSS key
3312 	 */
3313 	arc4rand(rss_key, sizeof(rss_key), 0);
3314 	for (i = 0; i < RSSKEYLEN; ++i) {
3315 		uint32_t rssrk = 0;
3316 
3317 		rssrk = EM_RSSRK_VAL(rss_key, i);
3318 		E1000_WRITE_REG(hw,E1000_RSSRK(i), rssrk);
3319 	}
3320 
3321 	/*
3322 	 * Configure RSS redirect table in following fashion:
3323 	 * (hash & ring_cnt_mask) == rdr_table[(hash & rdr_table_mask)]
3324 	 */
3325 	for (i = 0; i < sizeof(reta); ++i) {
3326 		uint32_t q;
3327 
3328 		q = (i % sc->rx_num_queues) << 7;
3329 		reta |= q << (8 * i);
3330 	}
3331 
3332 	for (i = 0; i < 32; ++i)
3333 		E1000_WRITE_REG(hw, E1000_RETA(i), reta);
3334 
3335 	E1000_WRITE_REG(hw, E1000_MRQC, E1000_MRQC_RSS_ENABLE_2Q |
3336 			E1000_MRQC_RSS_FIELD_IPV4_TCP |
3337 			E1000_MRQC_RSS_FIELD_IPV4 |
3338 			E1000_MRQC_RSS_FIELD_IPV6_TCP_EX |
3339 			E1000_MRQC_RSS_FIELD_IPV6_EX |
3340 			E1000_MRQC_RSS_FIELD_IPV6);
3341 }
3342 
3343 static void
igb_initialize_rss_mapping(struct e1000_softc * sc)3344 igb_initialize_rss_mapping(struct e1000_softc *sc)
3345 {
3346 	struct e1000_hw *hw = &sc->hw;
3347 	int i;
3348 	int queue_id;
3349 	u32 reta;
3350 	u32 rss_key[10], mrqc, shift = 0;
3351 
3352 	/* XXX? */
3353 	if (hw->mac.type == e1000_82575)
3354 		shift = 6;
3355 
3356 	/*
3357 	 * The redirection table controls which destination
3358 	 * queue each bucket redirects traffic to.
3359 	 * Each DWORD represents four queues, with the LSB
3360 	 * being the first queue in the DWORD.
3361 	 *
3362 	 * This just allocates buckets to queues using round-robin
3363 	 * allocation.
3364 	 *
3365 	 * NOTE: It Just Happens to line up with the default
3366 	 * RSS allocation method.
3367 	 */
3368 
3369 	/* Warning FM follows */
3370 	reta = 0;
3371 	for (i = 0; i < 128; i++) {
3372 #ifdef RSS
3373 		queue_id = rss_get_indirection_to_bucket(i);
3374 		/*
3375 		 * If we have more queues than buckets, we'll
3376 		 * end up mapping buckets to a subset of the
3377 		 * queues.
3378 		 *
3379 		 * If we have more buckets than queues, we'll
3380 		 * end up instead assigning multiple buckets
3381 		 * to queues.
3382 		 *
3383 		 * Both are suboptimal, but we need to handle
3384 		 * the case so we don't go out of bounds
3385 		 * indexing arrays and such.
3386 		 */
3387 		queue_id = queue_id % sc->rx_num_queues;
3388 #else
3389 		queue_id = (i % sc->rx_num_queues);
3390 #endif
3391 		/* Adjust if required */
3392 		queue_id = queue_id << shift;
3393 
3394 		/*
3395 		 * The low 8 bits are for hash value (n+0);
3396 		 * The next 8 bits are for hash value (n+1), etc.
3397 		 */
3398 		reta = reta >> 8;
3399 		reta = reta | ( ((uint32_t) queue_id) << 24);
3400 		if ((i & 3) == 3) {
3401 			E1000_WRITE_REG(hw, E1000_RETA(i >> 2), reta);
3402 			reta = 0;
3403 		}
3404 	}
3405 
3406 	/* Now fill in hash table */
3407 
3408 	/*
3409 	 * MRQC: Multiple Receive Queues Command
3410 	 * Set queuing to RSS control, number depends on the device.
3411 	 */
3412 	mrqc = E1000_MRQC_ENABLE_RSS_MQ;
3413 
3414 #ifdef RSS
3415 	/* XXX ew typecasting */
3416 	rss_getkey((uint8_t *) &rss_key);
3417 #else
3418 	arc4rand(&rss_key, sizeof(rss_key), 0);
3419 #endif
3420 	for (i = 0; i < 10; i++)
3421 		E1000_WRITE_REG_ARRAY(hw, E1000_RSSRK(0), i, rss_key[i]);
3422 
3423 	/*
3424 	 * Configure the RSS fields to hash upon.
3425 	 */
3426 	mrqc |= (E1000_MRQC_RSS_FIELD_IPV4 |
3427 	    E1000_MRQC_RSS_FIELD_IPV4_TCP);
3428 	mrqc |= (E1000_MRQC_RSS_FIELD_IPV6 |
3429 	    E1000_MRQC_RSS_FIELD_IPV6_TCP);
3430 	mrqc |=( E1000_MRQC_RSS_FIELD_IPV4_UDP |
3431 	    E1000_MRQC_RSS_FIELD_IPV6_UDP);
3432 	mrqc |=( E1000_MRQC_RSS_FIELD_IPV6_UDP_EX |
3433 	    E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
3434 
3435 	E1000_WRITE_REG(hw, E1000_MRQC, mrqc);
3436 }
3437 
3438 /*********************************************************************
3439  *
3440  *  Setup networking device structure and register interface media.
3441  *
3442  **********************************************************************/
3443 static int
em_setup_interface(if_ctx_t ctx)3444 em_setup_interface(if_ctx_t ctx)
3445 {
3446 	if_t ifp = iflib_get_ifp(ctx);
3447 	struct e1000_softc *sc = iflib_get_softc(ctx);
3448 	if_softc_ctx_t scctx = sc->shared;
3449 
3450 	INIT_DEBUGOUT("em_setup_interface: begin");
3451 
3452 	/* Single Queue */
3453 	if (sc->tx_num_queues == 1) {
3454 		if_setsendqlen(ifp, scctx->isc_ntxd[0] - 1);
3455 		if_setsendqready(ifp);
3456 	}
3457 
3458 	/*
3459 	 * Specify the media types supported by this adapter and register
3460 	 * callbacks to update media and link information
3461 	 */
3462 	if (sc->hw.phy.media_type == e1000_media_type_fiber ||
3463 	    sc->hw.phy.media_type == e1000_media_type_internal_serdes) {
3464 		u_char fiber_type = IFM_1000_SX;	/* default type */
3465 
3466 		if (sc->hw.mac.type == e1000_82545)
3467 			fiber_type = IFM_1000_LX;
3468 		ifmedia_add(sc->media,
3469 		    IFM_ETHER | fiber_type | IFM_FDX, 0, NULL);
3470 		ifmedia_add(sc->media, IFM_ETHER | fiber_type, 0, NULL);
3471 	} else {
3472 		ifmedia_add(sc->media, IFM_ETHER | IFM_10_T, 0, NULL);
3473 		ifmedia_add(sc->media,
3474 		    IFM_ETHER | IFM_10_T | IFM_FDX, 0, NULL);
3475 		ifmedia_add(sc->media, IFM_ETHER | IFM_100_TX, 0, NULL);
3476 		ifmedia_add(sc->media,
3477 		    IFM_ETHER | IFM_100_TX | IFM_FDX, 0, NULL);
3478 		if (sc->hw.phy.type != e1000_phy_ife) {
3479 			ifmedia_add(sc->media,
3480 			    IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL);
3481 			ifmedia_add(sc->media,
3482 			    IFM_ETHER | IFM_1000_T, 0, NULL);
3483 		}
3484 	}
3485 	ifmedia_add(sc->media, IFM_ETHER | IFM_AUTO, 0, NULL);
3486 	ifmedia_set(sc->media, IFM_ETHER | IFM_AUTO);
3487 	return (0);
3488 }
3489 
3490 static int
em_if_tx_queues_alloc(if_ctx_t ctx,caddr_t * vaddrs,uint64_t * paddrs,int ntxqs,int ntxqsets)3491 em_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
3492     int ntxqs, int ntxqsets)
3493 {
3494 	struct e1000_softc *sc = iflib_get_softc(ctx);
3495 	if_softc_ctx_t scctx = sc->shared;
3496 	int error = E1000_SUCCESS;
3497 	struct em_tx_queue *que;
3498 	int i, j;
3499 
3500 	MPASS(sc->tx_num_queues > 0);
3501 	MPASS(sc->tx_num_queues == ntxqsets);
3502 
3503 	/* First allocate the top level queue structs */
3504 	if (!(sc->tx_queues =
3505 	    (struct em_tx_queue *) malloc(sizeof(struct em_tx_queue) *
3506 	    sc->tx_num_queues, M_DEVBUF, M_NOWAIT | M_ZERO))) {
3507 		device_printf(iflib_get_dev(ctx),
3508 		    "Unable to allocate queue memory\n");
3509 		return(ENOMEM);
3510 	}
3511 
3512 	for (i = 0, que = sc->tx_queues; i < sc->tx_num_queues; i++, que++) {
3513 		/* Set up some basics */
3514 
3515 		struct tx_ring *txr = &que->txr;
3516 		txr->sc = que->sc = sc;
3517 		que->me = txr->me =  i;
3518 
3519 		/* Allocate report status array */
3520 		if (!(txr->tx_rsq =
3521 		    (qidx_t *) malloc(sizeof(qidx_t) * scctx->isc_ntxd[0],
3522 		    M_DEVBUF, M_NOWAIT | M_ZERO))) {
3523 			device_printf(iflib_get_dev(ctx),
3524 			    "failed to allocate rs_idxs memory\n");
3525 			error = ENOMEM;
3526 			goto fail;
3527 		}
3528 		for (j = 0; j < scctx->isc_ntxd[0]; j++)
3529 			txr->tx_rsq[j] = QIDX_INVALID;
3530 		/* get the virtual and physical address of hardware queues */
3531 		txr->tx_base = (struct e1000_tx_desc *)vaddrs[i*ntxqs];
3532 		txr->tx_paddr = paddrs[i*ntxqs];
3533 	}
3534 
3535 	if (bootverbose)
3536 		device_printf(iflib_get_dev(ctx),
3537 		    "allocated for %d tx_queues\n", sc->tx_num_queues);
3538 	return (0);
3539 fail:
3540 	em_if_queues_free(ctx);
3541 	return (error);
3542 }
3543 
3544 static int
em_if_rx_queues_alloc(if_ctx_t ctx,caddr_t * vaddrs,uint64_t * paddrs,int nrxqs,int nrxqsets)3545 em_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
3546     int nrxqs, int nrxqsets)
3547 {
3548 	struct e1000_softc *sc = iflib_get_softc(ctx);
3549 	int error = E1000_SUCCESS;
3550 	struct em_rx_queue *que;
3551 	int i;
3552 
3553 	MPASS(sc->rx_num_queues > 0);
3554 	MPASS(sc->rx_num_queues == nrxqsets);
3555 
3556 	/* First allocate the top level queue structs */
3557 	if (!(sc->rx_queues =
3558 	    (struct em_rx_queue *) malloc(sizeof(struct em_rx_queue) *
3559 	    sc->rx_num_queues, M_DEVBUF, M_NOWAIT | M_ZERO))) {
3560 		device_printf(iflib_get_dev(ctx),
3561 		    "Unable to allocate queue memory\n");
3562 		error = ENOMEM;
3563 		goto fail;
3564 	}
3565 
3566 	for (i = 0, que = sc->rx_queues; i < nrxqsets; i++, que++) {
3567 		/* Set up some basics */
3568 		struct rx_ring *rxr = &que->rxr;
3569 		rxr->sc = que->sc = sc;
3570 		rxr->que = que;
3571 		que->me = rxr->me =  i;
3572 
3573 		/* get the virtual and physical address of hardware queues */
3574 		rxr->rx_base =
3575 		    (union e1000_rx_desc_extended *)vaddrs[i*nrxqs];
3576 		rxr->rx_paddr = paddrs[i*nrxqs];
3577 	}
3578 
3579 	if (bootverbose)
3580 		device_printf(iflib_get_dev(ctx),
3581 		    "allocated for %d rx_queues\n", sc->rx_num_queues);
3582 
3583 	return (0);
3584 fail:
3585 	em_if_queues_free(ctx);
3586 	return (error);
3587 }
3588 
3589 static void
em_if_queues_free(if_ctx_t ctx)3590 em_if_queues_free(if_ctx_t ctx)
3591 {
3592 	struct e1000_softc *sc = iflib_get_softc(ctx);
3593 	struct em_tx_queue *tx_que = sc->tx_queues;
3594 	struct em_rx_queue *rx_que = sc->rx_queues;
3595 
3596 	if (tx_que != NULL) {
3597 		for (int i = 0; i < sc->tx_num_queues; i++, tx_que++) {
3598 			struct tx_ring *txr = &tx_que->txr;
3599 			if (txr->tx_rsq == NULL)
3600 				break;
3601 
3602 			free(txr->tx_rsq, M_DEVBUF);
3603 			txr->tx_rsq = NULL;
3604 		}
3605 		free(sc->tx_queues, M_DEVBUF);
3606 		sc->tx_queues = NULL;
3607 	}
3608 
3609 	if (rx_que != NULL) {
3610 		free(sc->rx_queues, M_DEVBUF);
3611 		sc->rx_queues = NULL;
3612 	}
3613 }
3614 
3615 /*********************************************************************
3616  *
3617  *  Enable transmit unit.
3618  *
3619  **********************************************************************/
3620 static void
em_initialize_transmit_unit(if_ctx_t ctx)3621 em_initialize_transmit_unit(if_ctx_t ctx)
3622 {
3623 	struct e1000_softc *sc = iflib_get_softc(ctx);
3624 	if_softc_ctx_t scctx = sc->shared;
3625 	struct em_tx_queue *que;
3626 	struct tx_ring	*txr;
3627 	struct e1000_hw	*hw = &sc->hw;
3628 	u32 tctl, txdctl = 0, tarc, tipg = 0;
3629 
3630 	INIT_DEBUGOUT("em_initialize_transmit_unit: begin");
3631 
3632 	for (int i = 0; i < sc->tx_num_queues; i++, txr++) {
3633 		u64 bus_addr;
3634 		caddr_t offp, endp;
3635 
3636 		que = &sc->tx_queues[i];
3637 		txr = &que->txr;
3638 		bus_addr = txr->tx_paddr;
3639 
3640 		/* Clear checksum offload context. */
3641 		offp = (caddr_t)&txr->csum_flags;
3642 		endp = (caddr_t)(txr + 1);
3643 		bzero(offp, endp - offp);
3644 
3645 		/* Base and Len of TX Ring */
3646 		E1000_WRITE_REG(hw, E1000_TDLEN(i),
3647 		    scctx->isc_ntxd[0] * sizeof(struct e1000_tx_desc));
3648 		E1000_WRITE_REG(hw, E1000_TDBAH(i), (u32)(bus_addr >> 32));
3649 		E1000_WRITE_REG(hw, E1000_TDBAL(i), (u32)bus_addr);
3650 		/* Init the HEAD/TAIL indices */
3651 		E1000_WRITE_REG(hw, E1000_TDT(i), 0);
3652 		E1000_WRITE_REG(hw, E1000_TDH(i), 0);
3653 
3654 		HW_DEBUGOUT2("Base = %x, Length = %x\n",
3655 		    E1000_READ_REG(hw, E1000_TDBAL(i)),
3656 		    E1000_READ_REG(hw, E1000_TDLEN(i)));
3657 
3658 		txdctl = 0; /* clear txdctl */
3659 		txdctl |= 0x1f; /* PTHRESH */
3660 		txdctl |= 1 << 8; /* HTHRESH */
3661 		txdctl |= 1 << 16;/* WTHRESH */
3662 		txdctl |= 1 << 22; /* Reserved bit 22 must always be 1 */
3663 		txdctl |= E1000_TXDCTL_GRAN;
3664 		txdctl |= 1 << 25; /* LWTHRESH */
3665 
3666 		E1000_WRITE_REG(hw, E1000_TXDCTL(i), txdctl);
3667 	}
3668 
3669 	/* Set the default values for the Tx Inter Packet Gap timer */
3670 	switch (hw->mac.type) {
3671 	case e1000_80003es2lan:
3672 		tipg = DEFAULT_82543_TIPG_IPGR1;
3673 		tipg |= DEFAULT_80003ES2LAN_TIPG_IPGR2 <<
3674 		    E1000_TIPG_IPGR2_SHIFT;
3675 		break;
3676 	case e1000_82542:
3677 		tipg = DEFAULT_82542_TIPG_IPGT;
3678 		tipg |= DEFAULT_82542_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
3679 		tipg |= DEFAULT_82542_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
3680 		break;
3681 	default:
3682 		if (hw->phy.media_type == e1000_media_type_fiber ||
3683 		    hw->phy.media_type == e1000_media_type_internal_serdes)
3684 			tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
3685 		else
3686 			tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
3687 		tipg |= DEFAULT_82543_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
3688 		tipg |= DEFAULT_82543_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
3689 	}
3690 
3691 	if (hw->mac.type < igb_mac_min) {
3692 		E1000_WRITE_REG(hw, E1000_TIPG, tipg);
3693 		E1000_WRITE_REG(hw, E1000_TIDV, sc->tx_int_delay.value);
3694 
3695 		if (sc->tx_int_delay.value > 0)
3696 			sc->txd_cmd |= E1000_TXD_CMD_IDE;
3697 	}
3698 
3699 	if (hw->mac.type >= e1000_82540)
3700 		E1000_WRITE_REG(hw, E1000_TADV, sc->tx_abs_int_delay.value);
3701 
3702 	if (hw->mac.type == e1000_82571 || hw->mac.type == e1000_82572) {
3703 		tarc = E1000_READ_REG(hw, E1000_TARC(0));
3704 		tarc |= TARC_SPEED_MODE_BIT;
3705 		E1000_WRITE_REG(hw, E1000_TARC(0), tarc);
3706 	} else if (hw->mac.type == e1000_80003es2lan) {
3707 		/* errata: program both queues to unweighted RR */
3708 		tarc = E1000_READ_REG(hw, E1000_TARC(0));
3709 		tarc |= 1;
3710 		E1000_WRITE_REG(hw, E1000_TARC(0), tarc);
3711 		tarc = E1000_READ_REG(hw, E1000_TARC(1));
3712 		tarc |= 1;
3713 		E1000_WRITE_REG(hw, E1000_TARC(1), tarc);
3714 	} else if (hw->mac.type == e1000_82574) {
3715 		tarc = E1000_READ_REG(hw, E1000_TARC(0));
3716 		tarc |= TARC_ERRATA_BIT;
3717 		if ( sc->tx_num_queues > 1) {
3718 			tarc |= (TARC_COMPENSATION_MODE | TARC_MQ_FIX);
3719 			E1000_WRITE_REG(hw, E1000_TARC(0), tarc);
3720 			E1000_WRITE_REG(hw, E1000_TARC(1), tarc);
3721 		} else
3722 			E1000_WRITE_REG(hw, E1000_TARC(0), tarc);
3723 	}
3724 
3725 	/* Program the Transmit Control Register */
3726 	tctl = E1000_READ_REG(hw, E1000_TCTL);
3727 	tctl &= ~E1000_TCTL_CT;
3728 	tctl |= (E1000_TCTL_PSP | E1000_TCTL_RTLC | E1000_TCTL_EN |
3729 		   (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT));
3730 
3731 	if (hw->mac.type >= e1000_82571 && hw->mac.type < igb_mac_min)
3732 		tctl |= E1000_TCTL_MULR;
3733 
3734 	/* This write will effectively turn on the transmit unit. */
3735 	E1000_WRITE_REG(hw, E1000_TCTL, tctl);
3736 
3737 	/* SPT and KBL errata workarounds */
3738 	if (hw->mac.type == e1000_pch_spt) {
3739 		u32 reg;
3740 		reg = E1000_READ_REG(hw, E1000_IOSFPC);
3741 		reg |= E1000_RCTL_RDMTS_HEX;
3742 		E1000_WRITE_REG(hw, E1000_IOSFPC, reg);
3743 		/* i218-i219 Specification Update 1.5.4.5 */
3744 		reg = E1000_READ_REG(hw, E1000_TARC(0));
3745 		reg &= ~E1000_TARC0_CB_MULTIQ_3_REQ;
3746 		reg |= E1000_TARC0_CB_MULTIQ_2_REQ;
3747 		E1000_WRITE_REG(hw, E1000_TARC(0), reg);
3748 	}
3749 }
3750 
3751 /*********************************************************************
3752  *
3753  *  Enable receive unit.
3754  *
3755  **********************************************************************/
3756 #define BSIZEPKT_ROUNDUP ((1<<E1000_SRRCTL_BSIZEPKT_SHIFT)-1)
3757 
3758 static void
em_initialize_receive_unit(if_ctx_t ctx)3759 em_initialize_receive_unit(if_ctx_t ctx)
3760 {
3761 	struct e1000_softc *sc = iflib_get_softc(ctx);
3762 	if_softc_ctx_t scctx = sc->shared;
3763 	if_t ifp = iflib_get_ifp(ctx);
3764 	struct e1000_hw *hw = &sc->hw;
3765 	struct em_rx_queue *que;
3766 	int i;
3767 	uint32_t rctl, rxcsum;
3768 
3769 	INIT_DEBUGOUT("em_initialize_receive_units: begin");
3770 
3771 	/*
3772 	 * Make sure receives are disabled while setting
3773 	 * up the descriptor ring
3774 	 */
3775 	rctl = E1000_READ_REG(hw, E1000_RCTL);
3776 	/* Do not disable if ever enabled on this hardware */
3777 	if ((hw->mac.type != e1000_82574) && (hw->mac.type != e1000_82583))
3778 		E1000_WRITE_REG(hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
3779 
3780 	/* Setup the Receive Control Register */
3781 	rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
3782 	rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
3783 	    E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
3784 	    (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
3785 
3786 	/* Do not store bad packets */
3787 	rctl &= ~E1000_RCTL_SBP;
3788 
3789 	/* Enable Long Packet receive */
3790 	if (if_getmtu(ifp) > ETHERMTU)
3791 		rctl |= E1000_RCTL_LPE;
3792 	else
3793 		rctl &= ~E1000_RCTL_LPE;
3794 
3795 	/* Strip the CRC */
3796 	if (!em_disable_crc_stripping)
3797 		rctl |= E1000_RCTL_SECRC;
3798 
3799 	/* lem/em default interrupt moderation */
3800 	if (hw->mac.type < igb_mac_min) {
3801 		if (hw->mac.type >= e1000_82540) {
3802 			E1000_WRITE_REG(hw, E1000_RADV,
3803 			    sc->rx_abs_int_delay.value);
3804 
3805 			/* Set the default interrupt throttling rate */
3806 			E1000_WRITE_REG(hw, E1000_ITR,
3807 			    EM_INTS_TO_ITR(em_max_interrupt_rate));
3808 		}
3809 
3810 		/* XXX TEMPORARY WORKAROUND: on some systems with 82573
3811 		 * long latencies are observed, like Lenovo X60. This
3812 		 * change eliminates the problem, but since having positive
3813 		 * values in RDTR is a known source of problems on other
3814 		 * platforms another solution is being sought.
3815 		 */
3816 		if (hw->mac.type == e1000_82573)
3817 			E1000_WRITE_REG(hw, E1000_RDTR, 0x20);
3818 		else
3819 			E1000_WRITE_REG(hw, E1000_RDTR,
3820 			    sc->rx_int_delay.value);
3821 	}
3822 
3823 	if (hw->mac.type >= em_mac_min && !sc->vf_ifp) {
3824 		uint32_t rfctl;
3825 		/* Use extended rx descriptor formats */
3826 		rfctl = E1000_READ_REG(hw, E1000_RFCTL);
3827 		rfctl |= E1000_RFCTL_EXTEN;
3828 
3829 		/*
3830 		 * When using MSI-X interrupts we need to throttle
3831 		 * using the EITR register (82574 only)
3832 		 */
3833 		if (hw->mac.type == e1000_82574) {
3834 			for (int i = 0; i < 4; i++)
3835 				E1000_WRITE_REG(hw, E1000_EITR_82574(i),
3836 				    EM_INTS_TO_ITR(em_max_interrupt_rate));
3837 			/* Disable accelerated acknowledge */
3838 			rfctl |= E1000_RFCTL_ACK_DIS;
3839 		}
3840 		E1000_WRITE_REG(hw, E1000_RFCTL, rfctl);
3841 	}
3842 
3843 	/*
3844 	 * Set up L3 and L4 csum Rx descriptor offloads only on Physical
3845 	 * Functions. Virtual Functions have no access to this register.
3846 	 */
3847 	if (!sc->vf_ifp) {
3848 		rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
3849 		if (if_getcapenable(ifp) & IFCAP_RXCSUM) {
3850 			rxcsum |= E1000_RXCSUM_TUOFL | E1000_RXCSUM_IPOFL;
3851 			if (hw->mac.type > e1000_82575)
3852 				rxcsum |= E1000_RXCSUM_CRCOFL;
3853 			else if (hw->mac.type < em_mac_min &&
3854 			    if_getcapenable(ifp) & IFCAP_HWCSUM_IPV6)
3855 				rxcsum |= E1000_RXCSUM_IPV6OFL;
3856 		} else {
3857 			rxcsum &= ~(E1000_RXCSUM_IPOFL | E1000_RXCSUM_TUOFL);
3858 			if (hw->mac.type > e1000_82575)
3859 				rxcsum &= ~E1000_RXCSUM_CRCOFL;
3860 			else if (hw->mac.type < em_mac_min)
3861 				rxcsum &= ~E1000_RXCSUM_IPV6OFL;
3862 		}
3863 
3864 		if (sc->rx_num_queues > 1) {
3865 			/* RSS hash needed in the Rx descriptor */
3866 			rxcsum |= E1000_RXCSUM_PCSD;
3867 
3868 			if (hw->mac.type >= igb_mac_min)
3869 				igb_initialize_rss_mapping(sc);
3870 			else
3871 				em_initialize_rss_mapping(sc);
3872 		}
3873 		E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
3874 	}
3875 
3876 	for (i = 0, que = sc->rx_queues; i < sc->rx_num_queues; i++, que++) {
3877 		struct rx_ring *rxr = &que->rxr;
3878 		/* Setup the Base and Length of the Rx Descriptor Ring */
3879 		u64 bus_addr = rxr->rx_paddr;
3880 #if 0
3881 		u32 rdt = sc->rx_num_queues -1;  /* default */
3882 #endif
3883 
3884 		E1000_WRITE_REG(hw, E1000_RDLEN(i),
3885 		    scctx->isc_nrxd[0] *
3886 		    sizeof(union e1000_rx_desc_extended));
3887 		E1000_WRITE_REG(hw, E1000_RDBAH(i), (u32)(bus_addr >> 32));
3888 		E1000_WRITE_REG(hw, E1000_RDBAL(i), (u32)bus_addr);
3889 		/* Setup the Head and Tail Descriptor Pointers */
3890 		E1000_WRITE_REG(hw, E1000_RDH(i), 0);
3891 		E1000_WRITE_REG(hw, E1000_RDT(i), 0);
3892 	}
3893 
3894 	/*
3895 	 * Set PTHRESH for improved jumbo performance
3896 	 * According to 10.2.5.11 of Intel 82574 Datasheet,
3897 	 * RXDCTL(1) is written whenever RXDCTL(0) is written.
3898 	 * Only write to RXDCTL(1) if there is a need for different
3899 	 * settings.
3900 	 */
3901 	if ((hw->mac.type == e1000_ich9lan || hw->mac.type == e1000_pch2lan ||
3902 	    hw->mac.type == e1000_ich10lan) && if_getmtu(ifp) > ETHERMTU) {
3903 		u32 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(0));
3904 		E1000_WRITE_REG(hw, E1000_RXDCTL(0), rxdctl | 3);
3905 	} else if (hw->mac.type == e1000_82574) {
3906 		for (int i = 0; i < sc->rx_num_queues; i++) {
3907 			u32 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(i));
3908 			rxdctl |= 0x20; /* PTHRESH */
3909 			rxdctl |= 4 << 8; /* HTHRESH */
3910 			rxdctl |= 4 << 16;/* WTHRESH */
3911 			rxdctl |= 1 << 24; /* Switch to granularity */
3912 			E1000_WRITE_REG(hw, E1000_RXDCTL(i), rxdctl);
3913 		}
3914 	} else if (hw->mac.type >= igb_mac_min) {
3915 		u32 psize, srrctl = 0;
3916 
3917 		if (if_getmtu(ifp) > ETHERMTU) {
3918 			psize = scctx->isc_max_frame_size;
3919 			/* are we on a vlan? */
3920 			if (if_vlantrunkinuse(ifp))
3921 				psize += VLAN_TAG_SIZE;
3922 
3923 			if (sc->vf_ifp)
3924 				e1000_rlpml_set_vf(hw, psize);
3925 			else
3926 				E1000_WRITE_REG(hw, E1000_RLPML, psize);
3927 		}
3928 
3929 		/* Set maximum packet buffer len */
3930 		srrctl |= (sc->rx_mbuf_sz + BSIZEPKT_ROUNDUP) >>
3931 		    E1000_SRRCTL_BSIZEPKT_SHIFT;
3932 
3933 		/*
3934 		 * If TX flow control is disabled and there's >1 queue
3935 		 * defined, enable DROP.
3936 		 *
3937 		 * This drops frames rather than hanging the RX MAC for all
3938 		 * queues.
3939 		 */
3940 		if ((sc->rx_num_queues > 1) &&
3941 		    (sc->fc == e1000_fc_none ||
3942 		     sc->fc == e1000_fc_rx_pause)) {
3943 			srrctl |= E1000_SRRCTL_DROP_EN;
3944 		}
3945 		/* Setup the Base and Length of the Rx Descriptor Rings */
3946 		for (i = 0, que = sc->rx_queues; i < sc->rx_num_queues;
3947 		    i++, que++) {
3948 			struct rx_ring *rxr = &que->rxr;
3949 			u64 bus_addr = rxr->rx_paddr;
3950 			u32 rxdctl;
3951 
3952 #ifdef notyet
3953 			/* Configure for header split? -- ignore for now */
3954 			rxr->hdr_split = igb_header_split;
3955 #else
3956 			srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
3957 #endif
3958 
3959 			E1000_WRITE_REG(hw, E1000_RDLEN(i),
3960 			    scctx->isc_nrxd[0] *
3961 			    sizeof(struct e1000_rx_desc));
3962 			E1000_WRITE_REG(hw, E1000_RDBAH(i),
3963 			    (uint32_t)(bus_addr >> 32));
3964 			E1000_WRITE_REG(hw, E1000_RDBAL(i),
3965 			    (uint32_t)bus_addr);
3966 			E1000_WRITE_REG(hw, E1000_SRRCTL(i), srrctl);
3967 			/* Enable this Queue */
3968 			rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(i));
3969 			rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
3970 			rxdctl &= 0xFFF00000;
3971 			rxdctl |= IGB_RX_PTHRESH;
3972 			rxdctl |= IGB_RX_HTHRESH << 8;
3973 			rxdctl |= IGB_RX_WTHRESH << 16;
3974 			E1000_WRITE_REG(hw, E1000_RXDCTL(i), rxdctl);
3975 		}
3976 	} else if (hw->mac.type >= e1000_pch2lan) {
3977 		if (if_getmtu(ifp) > ETHERMTU)
3978 			e1000_lv_jumbo_workaround_ich8lan(hw, true);
3979 		else
3980 			e1000_lv_jumbo_workaround_ich8lan(hw, false);
3981 	}
3982 
3983 	/* Make sure VLAN Filters are off */
3984 	rctl &= ~E1000_RCTL_VFE;
3985 
3986 	/* Set up packet buffer size, overridden by per queue srrctl on igb */
3987 	if (hw->mac.type < igb_mac_min) {
3988 		if (sc->rx_mbuf_sz > 2048 && sc->rx_mbuf_sz <= 4096)
3989 			rctl |= E1000_RCTL_SZ_4096 | E1000_RCTL_BSEX;
3990 		else if (sc->rx_mbuf_sz > 4096 && sc->rx_mbuf_sz <= 8192)
3991 			rctl |= E1000_RCTL_SZ_8192 | E1000_RCTL_BSEX;
3992 		else if (sc->rx_mbuf_sz > 8192)
3993 			rctl |= E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX;
3994 		else {
3995 			rctl |= E1000_RCTL_SZ_2048;
3996 			rctl &= ~E1000_RCTL_BSEX;
3997 		}
3998 	} else
3999 		rctl |= E1000_RCTL_SZ_2048;
4000 
4001 	/*
4002 	 * rctl bits 11:10 are as follows
4003 	 * lem: reserved
4004 	 * em: DTYPE
4005 	 * igb: reserved
4006 	 * and should be 00 on all of the above
4007 	 */
4008 	rctl &= ~0x00000C00;
4009 
4010 	/* Write out the settings */
4011 	E1000_WRITE_REG(hw, E1000_RCTL, rctl);
4012 
4013 	return;
4014 }
4015 
4016 static void
em_if_vlan_register(if_ctx_t ctx,u16 vtag)4017 em_if_vlan_register(if_ctx_t ctx, u16 vtag)
4018 {
4019 	struct e1000_softc *sc = iflib_get_softc(ctx);
4020 	u32 index, bit;
4021 
4022 	index = (vtag >> 5) & 0x7F;
4023 	bit = vtag & 0x1F;
4024 	sc->shadow_vfta[index] |= (1 << bit);
4025 	++sc->num_vlans;
4026 	em_if_vlan_filter_write(sc);
4027 }
4028 
4029 static void
em_if_vlan_unregister(if_ctx_t ctx,u16 vtag)4030 em_if_vlan_unregister(if_ctx_t ctx, u16 vtag)
4031 {
4032 	struct e1000_softc *sc = iflib_get_softc(ctx);
4033 	u32 index, bit;
4034 
4035 	index = (vtag >> 5) & 0x7F;
4036 	bit = vtag & 0x1F;
4037 	sc->shadow_vfta[index] &= ~(1 << bit);
4038 	--sc->num_vlans;
4039 	em_if_vlan_filter_write(sc);
4040 }
4041 
4042 static bool
em_if_vlan_filter_capable(if_ctx_t ctx)4043 em_if_vlan_filter_capable(if_ctx_t ctx)
4044 {
4045 	if_t ifp = iflib_get_ifp(ctx);
4046 
4047 	if ((if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER) &&
4048 	    !em_disable_crc_stripping)
4049 		return (true);
4050 
4051 	return (false);
4052 }
4053 
4054 static bool
em_if_vlan_filter_used(if_ctx_t ctx)4055 em_if_vlan_filter_used(if_ctx_t ctx)
4056 {
4057 	struct e1000_softc *sc = iflib_get_softc(ctx);
4058 
4059 	if (!em_if_vlan_filter_capable(ctx))
4060 		return (false);
4061 
4062 	for (int i = 0; i < EM_VFTA_SIZE; i++)
4063 		if (sc->shadow_vfta[i] != 0)
4064 			return (true);
4065 
4066 	return (false);
4067 }
4068 
4069 static void
em_if_vlan_filter_enable(struct e1000_softc * sc)4070 em_if_vlan_filter_enable(struct e1000_softc *sc)
4071 {
4072 	struct e1000_hw *hw = &sc->hw;
4073 	u32 reg;
4074 
4075 	reg = E1000_READ_REG(hw, E1000_RCTL);
4076 	reg &= ~E1000_RCTL_CFIEN;
4077 	reg |= E1000_RCTL_VFE;
4078 	E1000_WRITE_REG(hw, E1000_RCTL, reg);
4079 }
4080 
4081 static void
em_if_vlan_filter_disable(struct e1000_softc * sc)4082 em_if_vlan_filter_disable(struct e1000_softc *sc)
4083 {
4084 	struct e1000_hw *hw = &sc->hw;
4085 	u32 reg;
4086 
4087 	reg = E1000_READ_REG(hw, E1000_RCTL);
4088 	reg &= ~(E1000_RCTL_VFE | E1000_RCTL_CFIEN);
4089 	E1000_WRITE_REG(hw, E1000_RCTL, reg);
4090 }
4091 
4092 static void
em_if_vlan_filter_write(struct e1000_softc * sc)4093 em_if_vlan_filter_write(struct e1000_softc *sc)
4094 {
4095 	struct e1000_hw *hw = &sc->hw;
4096 
4097 	if (sc->vf_ifp)
4098 		return;
4099 
4100 	/* Disable interrupts for lem(4) devices during the filter change */
4101 	if (hw->mac.type < em_mac_min)
4102 		em_if_intr_disable(sc->ctx);
4103 
4104 	for (int i = 0; i < EM_VFTA_SIZE; i++)
4105 		if (sc->shadow_vfta[i] != 0) {
4106 			/* XXXKB: incomplete VF support, we returned above */
4107 			if (sc->vf_ifp)
4108 				e1000_vfta_set_vf(hw, sc->shadow_vfta[i],
4109 				    true);
4110 			else
4111 				e1000_write_vfta(hw, i, sc->shadow_vfta[i]);
4112 		}
4113 
4114 	/* Re-enable interrupts for lem-class devices */
4115 	if (hw->mac.type < em_mac_min)
4116 		em_if_intr_enable(sc->ctx);
4117 }
4118 
4119 static void
em_setup_vlan_hw_support(if_ctx_t ctx)4120 em_setup_vlan_hw_support(if_ctx_t ctx)
4121 {
4122 	struct e1000_softc *sc = iflib_get_softc(ctx);
4123 	struct e1000_hw *hw = &sc->hw;
4124 	if_t ifp = iflib_get_ifp(ctx);
4125 	u32 reg;
4126 
4127 	/* XXXKB: Return early if we are a VF until VF decap and filter
4128 	 * management is ready and tested.
4129 	 */
4130 	if (sc->vf_ifp)
4131 		return;
4132 
4133 	if (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING &&
4134 	    !em_disable_crc_stripping) {
4135 		reg = E1000_READ_REG(hw, E1000_CTRL);
4136 		reg |= E1000_CTRL_VME;
4137 		E1000_WRITE_REG(hw, E1000_CTRL, reg);
4138 	} else {
4139 		reg = E1000_READ_REG(hw, E1000_CTRL);
4140 		reg &= ~E1000_CTRL_VME;
4141 		E1000_WRITE_REG(hw, E1000_CTRL, reg);
4142 	}
4143 
4144 	/* If we aren't doing HW filtering, we're done */
4145 	if (!em_if_vlan_filter_capable(ctx))  {
4146 		em_if_vlan_filter_disable(sc);
4147 		return;
4148 	}
4149 
4150 	/*
4151 	 * A soft reset zero's out the VFTA, so
4152 	 * we need to repopulate it now.
4153 	 * We also insert VLAN 0 in the filter list, so we pass VLAN 0 tagged
4154 	 * traffic through. This will write the entire table.
4155 	 */
4156 	em_if_vlan_register(ctx, 0);
4157 
4158 	/* Enable the Filter Table */
4159 	em_if_vlan_filter_enable(sc);
4160 }
4161 
4162 static void
em_if_intr_enable(if_ctx_t ctx)4163 em_if_intr_enable(if_ctx_t ctx)
4164 {
4165 	struct e1000_softc *sc = iflib_get_softc(ctx);
4166 	struct e1000_hw *hw = &sc->hw;
4167 	u32 ims_mask = IMS_ENABLE_MASK;
4168 
4169 	if (sc->intr_type == IFLIB_INTR_MSIX) {
4170 		E1000_WRITE_REG(hw, EM_EIAC, sc->ims);
4171 		ims_mask |= sc->ims;
4172 	}
4173 
4174 	E1000_WRITE_REG(hw, E1000_IMS, ims_mask);
4175 	E1000_WRITE_FLUSH(hw);
4176 }
4177 
4178 static void
em_if_intr_disable(if_ctx_t ctx)4179 em_if_intr_disable(if_ctx_t ctx)
4180 {
4181 	struct e1000_softc *sc = iflib_get_softc(ctx);
4182 	struct e1000_hw *hw = &sc->hw;
4183 
4184 	if (sc->intr_type == IFLIB_INTR_MSIX)
4185 		E1000_WRITE_REG(hw, EM_EIAC, 0);
4186 	E1000_WRITE_REG(hw, E1000_IMC, 0xffffffff);
4187 	E1000_WRITE_FLUSH(hw);
4188 }
4189 
4190 static void
igb_if_intr_enable(if_ctx_t ctx)4191 igb_if_intr_enable(if_ctx_t ctx)
4192 {
4193 	struct e1000_softc *sc = iflib_get_softc(ctx);
4194 	struct e1000_hw *hw = &sc->hw;
4195 	u32 mask;
4196 
4197 	if (__predict_true(sc->intr_type == IFLIB_INTR_MSIX)) {
4198 		mask = (sc->que_mask | sc->link_mask);
4199 		E1000_WRITE_REG(hw, E1000_EIAC, mask);
4200 		E1000_WRITE_REG(hw, E1000_EIAM, mask);
4201 		E1000_WRITE_REG(hw, E1000_EIMS, mask);
4202 		E1000_WRITE_REG(hw, E1000_IMS, E1000_IMS_LSC);
4203 	} else
4204 		E1000_WRITE_REG(hw, E1000_IMS, IMS_ENABLE_MASK);
4205 	E1000_WRITE_FLUSH(hw);
4206 }
4207 
4208 static void
igb_if_intr_disable(if_ctx_t ctx)4209 igb_if_intr_disable(if_ctx_t ctx)
4210 {
4211 	struct e1000_softc *sc = iflib_get_softc(ctx);
4212 	struct e1000_hw *hw = &sc->hw;
4213 
4214 	if (__predict_true(sc->intr_type == IFLIB_INTR_MSIX)) {
4215 		E1000_WRITE_REG(hw, E1000_EIMC, 0xffffffff);
4216 		E1000_WRITE_REG(hw, E1000_EIAC, 0);
4217 	}
4218 	E1000_WRITE_REG(hw, E1000_IMC, 0xffffffff);
4219 	E1000_WRITE_FLUSH(hw);
4220 }
4221 
4222 /*
4223  * Bit of a misnomer, what this really means is
4224  * to enable OS management of the system... aka
4225  * to disable special hardware management features
4226  */
4227 static void
em_init_manageability(struct e1000_softc * sc)4228 em_init_manageability(struct e1000_softc *sc)
4229 {
4230 	/* A shared code workaround */
4231 #define E1000_82542_MANC2H E1000_MANC2H
4232 	if (sc->has_manage) {
4233 		int manc2h = E1000_READ_REG(&sc->hw, E1000_MANC2H);
4234 		int manc = E1000_READ_REG(&sc->hw, E1000_MANC);
4235 
4236 		/* disable hardware interception of ARP */
4237 		manc &= ~(E1000_MANC_ARP_EN);
4238 
4239 		/* enable receiving management packets to the host */
4240 		manc |= E1000_MANC_EN_MNG2HOST;
4241 #define E1000_MNG2HOST_PORT_623 (1 << 5)
4242 #define E1000_MNG2HOST_PORT_664 (1 << 6)
4243 		manc2h |= E1000_MNG2HOST_PORT_623;
4244 		manc2h |= E1000_MNG2HOST_PORT_664;
4245 		E1000_WRITE_REG(&sc->hw, E1000_MANC2H, manc2h);
4246 		E1000_WRITE_REG(&sc->hw, E1000_MANC, manc);
4247 	}
4248 }
4249 
4250 /*
4251  * Give control back to hardware management
4252  * controller if there is one.
4253  */
4254 static void
em_release_manageability(struct e1000_softc * sc)4255 em_release_manageability(struct e1000_softc *sc)
4256 {
4257 	if (sc->has_manage) {
4258 		int manc = E1000_READ_REG(&sc->hw, E1000_MANC);
4259 
4260 		/* re-enable hardware interception of ARP */
4261 		manc |= E1000_MANC_ARP_EN;
4262 		manc &= ~E1000_MANC_EN_MNG2HOST;
4263 
4264 		E1000_WRITE_REG(&sc->hw, E1000_MANC, manc);
4265 	}
4266 }
4267 
4268 /*
4269  * em_get_hw_control sets the {CTRL_EXT|FWSM}:DRV_LOAD bit.
4270  * For ASF and Pass Through versions of f/w this means
4271  * that the driver is loaded. For AMT version type f/w
4272  * this means that the network i/f is open.
4273  */
4274 static void
em_get_hw_control(struct e1000_softc * sc)4275 em_get_hw_control(struct e1000_softc *sc)
4276 {
4277 	u32 ctrl_ext, swsm;
4278 
4279 	if (sc->vf_ifp)
4280 		return;
4281 
4282 	if (sc->hw.mac.type == e1000_82573) {
4283 		swsm = E1000_READ_REG(&sc->hw, E1000_SWSM);
4284 		E1000_WRITE_REG(&sc->hw, E1000_SWSM,
4285 		    swsm | E1000_SWSM_DRV_LOAD);
4286 		return;
4287 	}
4288 	/* else */
4289 	ctrl_ext = E1000_READ_REG(&sc->hw, E1000_CTRL_EXT);
4290 	E1000_WRITE_REG(&sc->hw, E1000_CTRL_EXT,
4291 	    ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
4292 }
4293 
4294 /*
4295  * em_release_hw_control resets {CTRL_EXT|FWSM}:DRV_LOAD bit.
4296  * For ASF and Pass Through versions of f/w this means that
4297  * the driver is no longer loaded. For AMT versions of the
4298  * f/w this means that the network i/f is closed.
4299  */
4300 static void
em_release_hw_control(struct e1000_softc * sc)4301 em_release_hw_control(struct e1000_softc *sc)
4302 {
4303 	u32 ctrl_ext, swsm;
4304 
4305 	if (!sc->has_manage)
4306 		return;
4307 
4308 	if (sc->hw.mac.type == e1000_82573) {
4309 		swsm = E1000_READ_REG(&sc->hw, E1000_SWSM);
4310 		E1000_WRITE_REG(&sc->hw, E1000_SWSM,
4311 		    swsm & ~E1000_SWSM_DRV_LOAD);
4312 		return;
4313 	}
4314 	/* else */
4315 	ctrl_ext = E1000_READ_REG(&sc->hw, E1000_CTRL_EXT);
4316 	E1000_WRITE_REG(&sc->hw, E1000_CTRL_EXT,
4317 	    ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
4318 	return;
4319 }
4320 
4321 static int
em_is_valid_ether_addr(u8 * addr)4322 em_is_valid_ether_addr(u8 *addr)
4323 {
4324 	char zero_addr[6] = { 0, 0, 0, 0, 0, 0 };
4325 
4326 	if ((addr[0] & 1) || (!bcmp(addr, zero_addr, ETHER_ADDR_LEN))) {
4327 		return (false);
4328 	}
4329 
4330 	return (true);
4331 }
4332 
4333 static bool
em_automask_tso(if_ctx_t ctx)4334 em_automask_tso(if_ctx_t ctx)
4335 {
4336 	struct e1000_softc *sc = iflib_get_softc(ctx);
4337 	if_softc_ctx_t scctx = iflib_get_softc_ctx(ctx);
4338 	if_t ifp = iflib_get_ifp(ctx);
4339 
4340 	if (!em_unsupported_tso && sc->link_speed &&
4341 	    sc->link_speed != SPEED_1000 &&
4342 	    scctx->isc_capenable & IFCAP_TSO) {
4343 		device_printf(sc->dev,
4344 		    "Disabling TSO for 10/100 Ethernet.\n");
4345 		sc->tso_automasked = scctx->isc_capenable & IFCAP_TSO;
4346 		scctx->isc_capenable &= ~IFCAP_TSO;
4347 		if_setcapenablebit(ifp, 0, IFCAP_TSO);
4348 		/* iflib_init_locked handles ifnet hwassistbits */
4349 		iflib_request_reset(ctx);
4350 		return true;
4351 	} else if (sc->link_speed == SPEED_1000 && sc->tso_automasked) {
4352 		device_printf(sc->dev, "Re-enabling TSO for GbE.\n");
4353 		scctx->isc_capenable |= sc->tso_automasked;
4354 		if_setcapenablebit(ifp, sc->tso_automasked, 0);
4355 		sc->tso_automasked = 0;
4356 		/* iflib_init_locked handles ifnet hwassistbits */
4357 		iflib_request_reset(ctx);
4358 		return true;
4359 	}
4360 
4361 	return false;
4362 }
4363 
4364 /*
4365 ** Parse the interface capabilities with regard
4366 ** to both system management and wake-on-lan for
4367 ** later use.
4368 */
4369 static void
em_get_wakeup(if_ctx_t ctx)4370 em_get_wakeup(if_ctx_t ctx)
4371 {
4372 	struct e1000_softc *sc = iflib_get_softc(ctx);
4373 	device_t dev = iflib_get_dev(ctx);
4374 	u16 eeprom_data = 0, device_id, apme_mask;
4375 
4376 	sc->has_manage = e1000_enable_mng_pass_thru(&sc->hw);
4377 	apme_mask = EM_EEPROM_APME;
4378 
4379 	switch (sc->hw.mac.type) {
4380 	case e1000_82542:
4381 	case e1000_82543:
4382 	case e1000_vfadapt:
4383 	case e1000_vfadapt_i350:
4384 		break;
4385 	case e1000_82544:
4386 		e1000_read_nvm(&sc->hw,
4387 		    NVM_INIT_CONTROL2_REG, 1, &eeprom_data);
4388 		apme_mask = EM_82544_APME;
4389 		break;
4390 	case e1000_82546:
4391 	case e1000_82546_rev_3:
4392 		if (sc->hw.bus.func == 1) {
4393 			e1000_read_nvm(&sc->hw,
4394 			    NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
4395 			break;
4396 		} else
4397 			e1000_read_nvm(&sc->hw,
4398 			    NVM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
4399 		break;
4400 	case e1000_82573:
4401 	case e1000_82583:
4402 		sc->has_amt = true;
4403 		/* FALLTHROUGH */
4404 	case e1000_82571:
4405 	case e1000_82572:
4406 	case e1000_80003es2lan:
4407 		if (sc->hw.bus.func == 1) {
4408 			e1000_read_nvm(&sc->hw,
4409 			    NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
4410 			break;
4411 		} else
4412 			e1000_read_nvm(&sc->hw,
4413 			    NVM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
4414 		break;
4415 	case e1000_ich8lan:
4416 	case e1000_ich9lan:
4417 	case e1000_ich10lan:
4418 	case e1000_pchlan:
4419 	case e1000_pch2lan:
4420 	case e1000_pch_lpt:
4421 	case e1000_pch_spt:
4422 	case e1000_82575:	/* listing all igb devices */
4423 	case e1000_82576:
4424 	case e1000_82580:
4425 	case e1000_i350:
4426 	case e1000_i354:
4427 	case e1000_i210:
4428 	case e1000_i211:
4429 		apme_mask = E1000_WUC_APME;
4430 		sc->has_amt = true;
4431 		eeprom_data = E1000_READ_REG(&sc->hw, E1000_WUC);
4432 		break;
4433 	default:
4434 		e1000_read_nvm(&sc->hw,
4435 		    NVM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
4436 		break;
4437 	}
4438 	if (eeprom_data & apme_mask)
4439 		sc->wol = (E1000_WUFC_MAG | E1000_WUFC_MC);
4440 	/*
4441 	 * We have the eeprom settings, now apply the special cases
4442 	 * where the eeprom may be wrong or the board won't support
4443 	 * wake on lan on a particular port
4444 	 */
4445 	device_id = pci_get_device(dev);
4446 	switch (device_id) {
4447 	case E1000_DEV_ID_82546GB_PCIE:
4448 		sc->wol = 0;
4449 		break;
4450 	case E1000_DEV_ID_82546EB_FIBER:
4451 	case E1000_DEV_ID_82546GB_FIBER:
4452 		/* Wake events only supported on port A for dual fiber
4453 		 * regardless of eeprom setting */
4454 		if (E1000_READ_REG(&sc->hw, E1000_STATUS) &
4455 		    E1000_STATUS_FUNC_1)
4456 			sc->wol = 0;
4457 		break;
4458 	case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
4459 		/* if quad port adapter, disable WoL on all but port A */
4460 		if (global_quad_port_a != 0)
4461 			sc->wol = 0;
4462 		/* Reset for multiple quad port adapters */
4463 		if (++global_quad_port_a == 4)
4464 			global_quad_port_a = 0;
4465 		break;
4466 	case E1000_DEV_ID_82571EB_FIBER:
4467 		/* Wake events only supported on port A for dual fiber
4468 		 * regardless of eeprom setting */
4469 		if (E1000_READ_REG(&sc->hw, E1000_STATUS) &
4470 		    E1000_STATUS_FUNC_1)
4471 			sc->wol = 0;
4472 		break;
4473 	case E1000_DEV_ID_82571EB_QUAD_COPPER:
4474 	case E1000_DEV_ID_82571EB_QUAD_FIBER:
4475 	case E1000_DEV_ID_82571EB_QUAD_COPPER_LP:
4476 		/* if quad port adapter, disable WoL on all but port A */
4477 		if (global_quad_port_a != 0)
4478 			sc->wol = 0;
4479 		/* Reset for multiple quad port adapters */
4480 		if (++global_quad_port_a == 4)
4481 			global_quad_port_a = 0;
4482 		break;
4483 	}
4484 }
4485 
4486 
4487 /*
4488  * Enable PCI Wake On Lan capability
4489  */
4490 static void
em_enable_wakeup(if_ctx_t ctx)4491 em_enable_wakeup(if_ctx_t ctx)
4492 {
4493 	struct e1000_softc *sc = iflib_get_softc(ctx);
4494 	device_t dev = iflib_get_dev(ctx);
4495 	if_t ifp = iflib_get_ifp(ctx);
4496 	int error = 0;
4497 	u32 ctrl, ctrl_ext, rctl;
4498 
4499 	if (!pci_has_pm(dev))
4500 		return;
4501 
4502 	/*
4503 	 * Determine type of Wakeup: note that wol
4504 	 * is set with all bits on by default.
4505 	 */
4506 	if ((if_getcapenable(ifp) & IFCAP_WOL_MAGIC) == 0)
4507 		sc->wol &= ~E1000_WUFC_MAG;
4508 
4509 	if ((if_getcapenable(ifp) & IFCAP_WOL_UCAST) == 0)
4510 		sc->wol &= ~E1000_WUFC_EX;
4511 
4512 	if ((if_getcapenable(ifp) & IFCAP_WOL_MCAST) == 0)
4513 		sc->wol &= ~E1000_WUFC_MC;
4514 	else {
4515 		rctl = E1000_READ_REG(&sc->hw, E1000_RCTL);
4516 		rctl |= E1000_RCTL_MPE;
4517 		E1000_WRITE_REG(&sc->hw, E1000_RCTL, rctl);
4518 	}
4519 
4520 	if (!(sc->wol & (E1000_WUFC_EX | E1000_WUFC_MAG | E1000_WUFC_MC)))
4521 		goto pme;
4522 
4523 	/* Advertise the wakeup capability */
4524 	ctrl = E1000_READ_REG(&sc->hw, E1000_CTRL);
4525 	ctrl |= (E1000_CTRL_SWDPIN2 | E1000_CTRL_SWDPIN3);
4526 	E1000_WRITE_REG(&sc->hw, E1000_CTRL, ctrl);
4527 
4528 	/* Keep the laser running on Fiber adapters */
4529 	if (sc->hw.phy.media_type == e1000_media_type_fiber ||
4530 	    sc->hw.phy.media_type == e1000_media_type_internal_serdes) {
4531 		ctrl_ext = E1000_READ_REG(&sc->hw, E1000_CTRL_EXT);
4532 		ctrl_ext |= E1000_CTRL_EXT_SDP3_DATA;
4533 		E1000_WRITE_REG(&sc->hw, E1000_CTRL_EXT, ctrl_ext);
4534 	}
4535 
4536 	if ((sc->hw.mac.type == e1000_ich8lan) ||
4537 	    (sc->hw.mac.type == e1000_pchlan) ||
4538 	    (sc->hw.mac.type == e1000_ich9lan) ||
4539 	    (sc->hw.mac.type == e1000_ich10lan))
4540 		e1000_suspend_workarounds_ich8lan(&sc->hw);
4541 
4542 	if ( sc->hw.mac.type >= e1000_pchlan) {
4543 		error = em_enable_phy_wakeup(sc);
4544 		if (error)
4545 			goto pme;
4546 	} else {
4547 		/* Enable wakeup by the MAC */
4548 		E1000_WRITE_REG(&sc->hw, E1000_WUC, E1000_WUC_PME_EN);
4549 		E1000_WRITE_REG(&sc->hw, E1000_WUFC, sc->wol);
4550 	}
4551 
4552 	if (sc->hw.phy.type == e1000_phy_igp_3)
4553 		e1000_igp3_phy_powerdown_workaround_ich8lan(&sc->hw);
4554 
4555 pme:
4556 	if (!error && (if_getcapenable(ifp) & IFCAP_WOL))
4557 		pci_enable_pme(dev);
4558 
4559 	return;
4560 }
4561 
4562 /*
4563  * WOL in the newer chipset interfaces (pchlan)
4564  * require thing to be copied into the phy
4565  */
4566 static int
em_enable_phy_wakeup(struct e1000_softc * sc)4567 em_enable_phy_wakeup(struct e1000_softc *sc)
4568 {
4569 	struct e1000_hw *hw = &sc->hw;
4570 	u32 mreg, ret = 0;
4571 	u16 preg;
4572 
4573 	/* copy MAC RARs to PHY RARs */
4574 	e1000_copy_rx_addrs_to_phy_ich8lan(hw);
4575 
4576 	/* copy MAC MTA to PHY MTA */
4577 	for (int i = 0; i < hw->mac.mta_reg_count; i++) {
4578 		mreg = E1000_READ_REG_ARRAY(hw, E1000_MTA, i);
4579 		e1000_write_phy_reg(hw, BM_MTA(i), (u16)(mreg & 0xFFFF));
4580 		e1000_write_phy_reg(hw, BM_MTA(i) + 1,
4581 		    (u16)((mreg >> 16) & 0xFFFF));
4582 	}
4583 
4584 	/* configure PHY Rx Control register */
4585 	e1000_read_phy_reg(hw, BM_RCTL, &preg);
4586 	mreg = E1000_READ_REG(hw, E1000_RCTL);
4587 	if (mreg & E1000_RCTL_UPE)
4588 		preg |= BM_RCTL_UPE;
4589 	if (mreg & E1000_RCTL_MPE)
4590 		preg |= BM_RCTL_MPE;
4591 	preg &= ~(BM_RCTL_MO_MASK);
4592 	if (mreg & E1000_RCTL_MO_3)
4593 		preg |= (((mreg & E1000_RCTL_MO_3) >> E1000_RCTL_MO_SHIFT)
4594 				<< BM_RCTL_MO_SHIFT);
4595 	if (mreg & E1000_RCTL_BAM)
4596 		preg |= BM_RCTL_BAM;
4597 	if (mreg & E1000_RCTL_PMCF)
4598 		preg |= BM_RCTL_PMCF;
4599 	mreg = E1000_READ_REG(hw, E1000_CTRL);
4600 	if (mreg & E1000_CTRL_RFCE)
4601 		preg |= BM_RCTL_RFCE;
4602 	e1000_write_phy_reg(hw, BM_RCTL, preg);
4603 
4604 	/* enable PHY wakeup in MAC register */
4605 	E1000_WRITE_REG(hw, E1000_WUC,
4606 	    E1000_WUC_PHY_WAKE | E1000_WUC_PME_EN | E1000_WUC_APME);
4607 	E1000_WRITE_REG(hw, E1000_WUFC, sc->wol);
4608 
4609 	/* configure and enable PHY wakeup in PHY registers */
4610 	e1000_write_phy_reg(hw, BM_WUFC, sc->wol);
4611 	e1000_write_phy_reg(hw, BM_WUC, E1000_WUC_PME_EN);
4612 
4613 	/* activate PHY wakeup */
4614 	ret = hw->phy.ops.acquire(hw);
4615 	if (ret) {
4616 		printf("Could not acquire PHY\n");
4617 		return ret;
4618 	}
4619 	e1000_write_phy_reg_mdic(hw, IGP01E1000_PHY_PAGE_SELECT,
4620 	                         (BM_WUC_ENABLE_PAGE << IGP_PAGE_SHIFT));
4621 	ret = e1000_read_phy_reg_mdic(hw, BM_WUC_ENABLE_REG, &preg);
4622 	if (ret) {
4623 		printf("Could not read PHY page 769\n");
4624 		goto out;
4625 	}
4626 	preg |= BM_WUC_ENABLE_BIT | BM_WUC_HOST_WU_BIT;
4627 	ret = e1000_write_phy_reg_mdic(hw, BM_WUC_ENABLE_REG, preg);
4628 	if (ret)
4629 		printf("Could not set PHY Host Wakeup bit\n");
4630 out:
4631 	hw->phy.ops.release(hw);
4632 
4633 	return ret;
4634 }
4635 
4636 static void
em_if_led_func(if_ctx_t ctx,int onoff)4637 em_if_led_func(if_ctx_t ctx, int onoff)
4638 {
4639 	struct e1000_softc *sc = iflib_get_softc(ctx);
4640 
4641 	if (onoff) {
4642 		e1000_setup_led(&sc->hw);
4643 		e1000_led_on(&sc->hw);
4644 	} else {
4645 		e1000_led_off(&sc->hw);
4646 		e1000_cleanup_led(&sc->hw);
4647 	}
4648 }
4649 
4650 /*
4651  * Disable the L0S and L1 LINK states
4652  */
4653 static void
em_disable_aspm(struct e1000_softc * sc)4654 em_disable_aspm(struct e1000_softc *sc)
4655 {
4656 	int base, reg;
4657 	u16 link_cap,link_ctrl;
4658 	device_t dev = sc->dev;
4659 
4660 	switch (sc->hw.mac.type) {
4661 	case e1000_82573:
4662 	case e1000_82574:
4663 	case e1000_82583:
4664 		break;
4665 	default:
4666 		return;
4667 	}
4668 	if (pci_find_cap(dev, PCIY_EXPRESS, &base) != 0)
4669 		return;
4670 	reg = base + PCIER_LINK_CAP;
4671 	link_cap = pci_read_config(dev, reg, 2);
4672 	if ((link_cap & PCIEM_LINK_CAP_ASPM) == 0)
4673 		return;
4674 	reg = base + PCIER_LINK_CTL;
4675 	link_ctrl = pci_read_config(dev, reg, 2);
4676 	link_ctrl &= ~PCIEM_LINK_CTL_ASPMC;
4677 	pci_write_config(dev, reg, link_ctrl, 2);
4678 	return;
4679 }
4680 
4681 /**********************************************************************
4682  *
4683  *  Update the board statistics counters.
4684  *
4685  **********************************************************************/
4686 static void
em_update_stats_counters(struct e1000_softc * sc)4687 em_update_stats_counters(struct e1000_softc *sc)
4688 {
4689 	struct e1000_hw_stats *stats;
4690 	u64 prev_xoffrxc;
4691 
4692 	if (sc->vf_ifp) {
4693 		em_update_vf_stats_counters(sc);
4694 		return;
4695 	}
4696 
4697 	stats = &sc->ustats.stats;
4698 	prev_xoffrxc = stats->xoffrxc;
4699 
4700 	if(sc->hw.phy.media_type == e1000_media_type_copper ||
4701 	   (E1000_READ_REG(&sc->hw, E1000_STATUS) & E1000_STATUS_LU)) {
4702 		stats->symerrs += E1000_READ_REG(&sc->hw, E1000_SYMERRS);
4703 		stats->sec += E1000_READ_REG(&sc->hw, E1000_SEC);
4704 	}
4705 	stats->crcerrs += E1000_READ_REG(&sc->hw, E1000_CRCERRS);
4706 	stats->mpc += E1000_READ_REG(&sc->hw, E1000_MPC);
4707 	stats->scc += E1000_READ_REG(&sc->hw, E1000_SCC);
4708 	stats->ecol += E1000_READ_REG(&sc->hw, E1000_ECOL);
4709 
4710 	stats->mcc += E1000_READ_REG(&sc->hw, E1000_MCC);
4711 	stats->latecol += E1000_READ_REG(&sc->hw, E1000_LATECOL);
4712 	stats->colc += E1000_READ_REG(&sc->hw, E1000_COLC);
4713 	stats->dc += E1000_READ_REG(&sc->hw, E1000_DC);
4714 	stats->rlec += E1000_READ_REG(&sc->hw, E1000_RLEC);
4715 	stats->xonrxc += E1000_READ_REG(&sc->hw, E1000_XONRXC);
4716 	stats->xontxc += E1000_READ_REG(&sc->hw, E1000_XONTXC);
4717 	stats->xoffrxc += E1000_READ_REG(&sc->hw, E1000_XOFFRXC);
4718 	/*
4719 	 ** For watchdog management we need to know if we have been
4720 	 ** paused during the last interval, so capture that here.
4721 	*/
4722 	if (stats->xoffrxc != prev_xoffrxc)
4723 		sc->shared->isc_pause_frames = 1;
4724 	stats->xofftxc += E1000_READ_REG(&sc->hw, E1000_XOFFTXC);
4725 	stats->fcruc += E1000_READ_REG(&sc->hw, E1000_FCRUC);
4726 	stats->prc64 += E1000_READ_REG(&sc->hw, E1000_PRC64);
4727 	stats->prc127 += E1000_READ_REG(&sc->hw, E1000_PRC127);
4728 	stats->prc255 += E1000_READ_REG(&sc->hw, E1000_PRC255);
4729 	stats->prc511 += E1000_READ_REG(&sc->hw, E1000_PRC511);
4730 	stats->prc1023 += E1000_READ_REG(&sc->hw, E1000_PRC1023);
4731 	stats->prc1522 += E1000_READ_REG(&sc->hw, E1000_PRC1522);
4732 	stats->gprc += E1000_READ_REG(&sc->hw, E1000_GPRC);
4733 	stats->bprc += E1000_READ_REG(&sc->hw, E1000_BPRC);
4734 	stats->mprc += E1000_READ_REG(&sc->hw, E1000_MPRC);
4735 	stats->gptc += E1000_READ_REG(&sc->hw, E1000_GPTC);
4736 
4737 	/* For the 64-bit byte counters the low dword must be read first. */
4738 	/* Both registers clear on the read of the high dword */
4739 
4740 	stats->gorc += E1000_READ_REG(&sc->hw, E1000_GORCL) +
4741 	    ((u64)E1000_READ_REG(&sc->hw, E1000_GORCH) << 32);
4742 	stats->gotc += E1000_READ_REG(&sc->hw, E1000_GOTCL) +
4743 	    ((u64)E1000_READ_REG(&sc->hw, E1000_GOTCH) << 32);
4744 
4745 	stats->rnbc += E1000_READ_REG(&sc->hw, E1000_RNBC);
4746 	stats->ruc += E1000_READ_REG(&sc->hw, E1000_RUC);
4747 	stats->rfc += E1000_READ_REG(&sc->hw, E1000_RFC);
4748 	stats->roc += E1000_READ_REG(&sc->hw, E1000_ROC);
4749 	stats->rjc += E1000_READ_REG(&sc->hw, E1000_RJC);
4750 
4751 	stats->mgprc += E1000_READ_REG(&sc->hw, E1000_MGTPRC);
4752 	stats->mgpdc += E1000_READ_REG(&sc->hw, E1000_MGTPDC);
4753 	stats->mgptc += E1000_READ_REG(&sc->hw, E1000_MGTPTC);
4754 
4755 	stats->tor += E1000_READ_REG(&sc->hw, E1000_TORH);
4756 	stats->tot += E1000_READ_REG(&sc->hw, E1000_TOTH);
4757 
4758 	stats->tpr += E1000_READ_REG(&sc->hw, E1000_TPR);
4759 	stats->tpt += E1000_READ_REG(&sc->hw, E1000_TPT);
4760 	stats->ptc64 += E1000_READ_REG(&sc->hw, E1000_PTC64);
4761 	stats->ptc127 += E1000_READ_REG(&sc->hw, E1000_PTC127);
4762 	stats->ptc255 += E1000_READ_REG(&sc->hw, E1000_PTC255);
4763 	stats->ptc511 += E1000_READ_REG(&sc->hw, E1000_PTC511);
4764 	stats->ptc1023 += E1000_READ_REG(&sc->hw, E1000_PTC1023);
4765 	stats->ptc1522 += E1000_READ_REG(&sc->hw, E1000_PTC1522);
4766 	stats->mptc += E1000_READ_REG(&sc->hw, E1000_MPTC);
4767 	stats->bptc += E1000_READ_REG(&sc->hw, E1000_BPTC);
4768 
4769 	/* Interrupt Counts */
4770 
4771 	stats->iac += E1000_READ_REG(&sc->hw, E1000_IAC);
4772 	stats->icrxptc += E1000_READ_REG(&sc->hw, E1000_ICRXPTC);
4773 	stats->icrxatc += E1000_READ_REG(&sc->hw, E1000_ICRXATC);
4774 	stats->ictxptc += E1000_READ_REG(&sc->hw, E1000_ICTXPTC);
4775 	stats->ictxatc += E1000_READ_REG(&sc->hw, E1000_ICTXATC);
4776 	stats->ictxqec += E1000_READ_REG(&sc->hw, E1000_ICTXQEC);
4777 	stats->ictxqmtc += E1000_READ_REG(&sc->hw, E1000_ICTXQMTC);
4778 	stats->icrxdmtc += E1000_READ_REG(&sc->hw, E1000_ICRXDMTC);
4779 	stats->icrxoc += E1000_READ_REG(&sc->hw, E1000_ICRXOC);
4780 
4781 	if (sc->hw.mac.type >= e1000_82543) {
4782 		stats->algnerrc +=
4783 		E1000_READ_REG(&sc->hw, E1000_ALGNERRC);
4784 		stats->rxerrc +=
4785 		E1000_READ_REG(&sc->hw, E1000_RXERRC);
4786 		stats->tncrs +=
4787 		E1000_READ_REG(&sc->hw, E1000_TNCRS);
4788 		stats->cexterr +=
4789 		E1000_READ_REG(&sc->hw, E1000_CEXTERR);
4790 		stats->tsctc +=
4791 		E1000_READ_REG(&sc->hw, E1000_TSCTC);
4792 		stats->tsctfc +=
4793 		E1000_READ_REG(&sc->hw, E1000_TSCTFC);
4794 	}
4795 }
4796 
4797 static void
em_update_vf_stats_counters(struct e1000_softc * sc)4798 em_update_vf_stats_counters(struct e1000_softc *sc)
4799 {
4800 	struct e1000_vf_stats *stats;
4801 
4802 	if (sc->link_speed == 0)
4803 		return;
4804 
4805 	stats = &sc->ustats.vf_stats;
4806 
4807 	UPDATE_VF_REG(E1000_VFGPRC,
4808 	    stats->last_gprc, stats->gprc);
4809 	UPDATE_VF_REG(E1000_VFGORC,
4810 	    stats->last_gorc, stats->gorc);
4811 	UPDATE_VF_REG(E1000_VFGPTC,
4812 	    stats->last_gptc, stats->gptc);
4813 	UPDATE_VF_REG(E1000_VFGOTC,
4814 	    stats->last_gotc, stats->gotc);
4815 	UPDATE_VF_REG(E1000_VFMPRC,
4816 	    stats->last_mprc, stats->mprc);
4817 }
4818 
4819 static uint64_t
em_if_get_vf_counter(if_ctx_t ctx,ift_counter cnt)4820 em_if_get_vf_counter(if_ctx_t ctx, ift_counter cnt)
4821 {
4822 	struct e1000_softc *sc = iflib_get_softc(ctx);
4823 	if_t ifp = iflib_get_ifp(ctx);
4824 
4825 	switch (cnt) {
4826 	case IFCOUNTER_IERRORS:
4827 		return sc->dropped_pkts;
4828 	case IFCOUNTER_OERRORS:
4829 		return (if_get_counter_default(ifp, cnt) +
4830 		    sc->watchdog_events);
4831 	default:
4832 		return (if_get_counter_default(ifp, cnt));
4833 	}
4834 }
4835 
4836 static uint64_t
em_if_get_counter(if_ctx_t ctx,ift_counter cnt)4837 em_if_get_counter(if_ctx_t ctx, ift_counter cnt)
4838 {
4839 	struct e1000_softc *sc = iflib_get_softc(ctx);
4840 	struct e1000_hw_stats *stats;
4841 	if_t ifp = iflib_get_ifp(ctx);
4842 
4843 	if (sc->vf_ifp)
4844 		return (em_if_get_vf_counter(ctx, cnt));
4845 
4846 	stats = &sc->ustats.stats;
4847 
4848 	switch (cnt) {
4849 	case IFCOUNTER_COLLISIONS:
4850 		return (stats->colc);
4851 	case IFCOUNTER_IERRORS:
4852 		return (sc->dropped_pkts + stats->rxerrc +
4853 		    stats->crcerrs + stats->algnerrc +
4854 		    stats->ruc + stats->roc +
4855 		    stats->mpc + stats->cexterr);
4856 	case IFCOUNTER_OERRORS:
4857 		return (if_get_counter_default(ifp, cnt) +
4858 		    stats->ecol + stats->latecol + sc->watchdog_events);
4859 	default:
4860 		return (if_get_counter_default(ifp, cnt));
4861 	}
4862 }
4863 
4864 /* em_if_needs_restart - Tell iflib when the driver needs to be reinitialized
4865  * @ctx: iflib context
4866  * @event: event code to check
4867  *
4868  * Defaults to returning false for unknown events.
4869  *
4870  * @returns true if iflib needs to reinit the interface
4871  */
4872 static bool
em_if_needs_restart(if_ctx_t ctx __unused,enum iflib_restart_event event)4873 em_if_needs_restart(if_ctx_t ctx __unused, enum iflib_restart_event event)
4874 {
4875 	switch (event) {
4876 	case IFLIB_RESTART_VLAN_CONFIG:
4877 	default:
4878 		return (false);
4879 	}
4880 }
4881 
4882 /* Export a single 32-bit register via a read-only sysctl. */
4883 static int
em_sysctl_reg_handler(SYSCTL_HANDLER_ARGS)4884 em_sysctl_reg_handler(SYSCTL_HANDLER_ARGS)
4885 {
4886 	struct e1000_softc *sc;
4887 	u_int val;
4888 
4889 	sc = oidp->oid_arg1;
4890 	val = E1000_READ_REG(&sc->hw, oidp->oid_arg2);
4891 	return (sysctl_handle_int(oidp, &val, 0, req));
4892 }
4893 
4894 /* Per queue holdoff interrupt rate handler */
4895 static int
em_sysctl_interrupt_rate_handler(SYSCTL_HANDLER_ARGS)4896 em_sysctl_interrupt_rate_handler(SYSCTL_HANDLER_ARGS)
4897 {
4898 	struct em_rx_queue *rque;
4899 	struct em_tx_queue *tque;
4900 	struct e1000_hw *hw;
4901 	int error;
4902 	u32 reg, usec, rate;
4903 
4904 	bool tx = oidp->oid_arg2;
4905 
4906 	if (tx) {
4907 		tque = oidp->oid_arg1;
4908 		hw = &tque->sc->hw;
4909 		if (hw->mac.type >= igb_mac_min)
4910 			reg = E1000_READ_REG(hw, E1000_EITR(tque->me));
4911 		else if (hw->mac.type == e1000_82574 && tque->msix)
4912 			reg = E1000_READ_REG(hw, E1000_EITR_82574(tque->me));
4913 		else
4914 			reg = E1000_READ_REG(hw, E1000_ITR);
4915 	} else {
4916 		rque = oidp->oid_arg1;
4917 		hw = &rque->sc->hw;
4918 		if (hw->mac.type >= igb_mac_min)
4919 			reg = E1000_READ_REG(hw, E1000_EITR(rque->msix));
4920 		else if (hw->mac.type == e1000_82574 && rque->msix)
4921 			reg = E1000_READ_REG(hw,
4922 			    E1000_EITR_82574(rque->msix));
4923 		else
4924 			reg = E1000_READ_REG(hw, E1000_ITR);
4925 	}
4926 
4927 	if (hw->mac.type < igb_mac_min) {
4928 		if (reg > 0)
4929 			rate = EM_INTS_TO_ITR(reg);
4930 		else
4931 			rate = 0;
4932 	} else {
4933 		usec = (reg & IGB_QVECTOR_MASK);
4934 		if (usec > 0)
4935 			rate = IGB_INTS_TO_EITR(usec);
4936 		else
4937 			rate = 0;
4938 	}
4939 
4940 	error = sysctl_handle_int(oidp, &rate, 0, req);
4941 	if (error || !req->newptr)
4942 		return error;
4943 	return 0;
4944 }
4945 
4946 /*
4947  * Add sysctl variables, one per statistic, to the system.
4948  */
4949 static void
em_add_hw_stats(struct e1000_softc * sc)4950 em_add_hw_stats(struct e1000_softc *sc)
4951 {
4952 	device_t dev = iflib_get_dev(sc->ctx);
4953 	struct em_tx_queue *tx_que = sc->tx_queues;
4954 	struct em_rx_queue *rx_que = sc->rx_queues;
4955 
4956 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
4957 	struct sysctl_oid *tree = device_get_sysctl_tree(dev);
4958 	struct sysctl_oid_list *child = SYSCTL_CHILDREN(tree);
4959 	struct e1000_hw_stats *stats;
4960 
4961 	struct sysctl_oid *stat_node, *queue_node, *int_node;
4962 	struct sysctl_oid_list *stat_list, *queue_list, *int_list;
4963 
4964 #define QUEUE_NAME_LEN 32
4965 	char namebuf[QUEUE_NAME_LEN];
4966 
4967 	/* Driver Statistics */
4968 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "dropped",
4969 	    CTLFLAG_RD, &sc->dropped_pkts,
4970 	    "Driver dropped packets");
4971 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "link_irq",
4972 	    CTLFLAG_RD, &sc->link_irq,
4973 	    "Link MSI-X IRQ Handled");
4974 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_overruns",
4975 	    CTLFLAG_RD, &sc->rx_overruns,
4976 	    "RX overruns");
4977 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "watchdog_timeouts",
4978 	    CTLFLAG_RD, &sc->watchdog_events,
4979 	    "Watchdog timeouts");
4980 	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "device_control",
4981 	    CTLTYPE_UINT | CTLFLAG_RD,
4982 	    sc, E1000_CTRL, em_sysctl_reg_handler, "IU",
4983 	    "Device Control Register");
4984 	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "rx_control",
4985 	    CTLTYPE_UINT | CTLFLAG_RD,
4986 	    sc, E1000_RCTL, em_sysctl_reg_handler, "IU",
4987 	    "Receiver Control Register");
4988 	SYSCTL_ADD_UINT(ctx, child, OID_AUTO, "fc_high_water",
4989 	    CTLFLAG_RD, &sc->hw.fc.high_water, 0,
4990 	    "Flow Control High Watermark");
4991 	SYSCTL_ADD_UINT(ctx, child, OID_AUTO, "fc_low_water",
4992 	    CTLFLAG_RD, &sc->hw.fc.low_water, 0,
4993 	    "Flow Control Low Watermark");
4994 
4995 	for (int i = 0; i < sc->tx_num_queues; i++, tx_que++) {
4996 		struct tx_ring *txr = &tx_que->txr;
4997 		snprintf(namebuf, QUEUE_NAME_LEN, "queue_tx_%d", i);
4998 		queue_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
4999 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TX Queue Name");
5000 		queue_list = SYSCTL_CHILDREN(queue_node);
5001 
5002 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "interrupt_rate",
5003 		    CTLTYPE_UINT | CTLFLAG_RD, tx_que,
5004 		    true, em_sysctl_interrupt_rate_handler,
5005 		    "IU", "Interrupt Rate");
5006 
5007 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "txd_head",
5008 		    CTLTYPE_UINT | CTLFLAG_RD, sc,
5009 		    E1000_TDH(txr->me), em_sysctl_reg_handler, "IU",
5010 		    "Transmit Descriptor Head");
5011 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "txd_tail",
5012 		    CTLTYPE_UINT | CTLFLAG_RD, sc,
5013 		    E1000_TDT(txr->me), em_sysctl_reg_handler, "IU",
5014 		    "Transmit Descriptor Tail");
5015 		SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO, "tx_irq",
5016 		    CTLFLAG_RD, &txr->tx_irq,
5017 		    "Queue MSI-X Transmit Interrupts");
5018 	}
5019 
5020 	for (int j = 0; j < sc->rx_num_queues; j++, rx_que++) {
5021 		struct rx_ring *rxr = &rx_que->rxr;
5022 		snprintf(namebuf, QUEUE_NAME_LEN, "queue_rx_%d", j);
5023 		queue_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
5024 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "RX Queue Name");
5025 		queue_list = SYSCTL_CHILDREN(queue_node);
5026 
5027 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "interrupt_rate",
5028 		    CTLTYPE_UINT | CTLFLAG_RD, rx_que,
5029 		    false, em_sysctl_interrupt_rate_handler,
5030 		    "IU", "Interrupt Rate");
5031 
5032 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "rxd_head",
5033 		    CTLTYPE_UINT | CTLFLAG_RD, sc,
5034 		    E1000_RDH(rxr->me), em_sysctl_reg_handler, "IU",
5035 		    "Receive Descriptor Head");
5036 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "rxd_tail",
5037 		    CTLTYPE_UINT | CTLFLAG_RD, sc,
5038 		    E1000_RDT(rxr->me), em_sysctl_reg_handler, "IU",
5039 		    "Receive Descriptor Tail");
5040 		SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO, "rx_irq",
5041 		    CTLFLAG_RD, &rxr->rx_irq,
5042 		    "Queue MSI-X Receive Interrupts");
5043 	}
5044 
5045 	/* MAC stats get their own sub node */
5046 	stat_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "mac_stats",
5047 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Statistics");
5048 	stat_list = SYSCTL_CHILDREN(stat_node);
5049 
5050 	/*
5051 	** VF adapter has a very limited set of stats
5052 	** since its not managing the metal, so to speak.
5053 	*/
5054 	if (sc->vf_ifp) {
5055 		struct e1000_vf_stats *vfstats = &sc->ustats.vf_stats;
5056 
5057 		SYSCTL_ADD_QUAD(ctx, stat_list, OID_AUTO, "good_pkts_recvd",
5058 		    CTLFLAG_RD, &vfstats->gprc,
5059 		    "Good Packets Received");
5060 		SYSCTL_ADD_QUAD(ctx, stat_list, OID_AUTO, "good_pkts_txd",
5061 		    CTLFLAG_RD, &vfstats->gptc,
5062 		    "Good Packets Transmitted");
5063 		SYSCTL_ADD_QUAD(ctx, stat_list, OID_AUTO, "good_octets_recvd",
5064 		    CTLFLAG_RD, &vfstats->gorc,
5065 		    "Good Octets Received");
5066 		SYSCTL_ADD_QUAD(ctx, stat_list, OID_AUTO, "good_octets_txd",
5067 		    CTLFLAG_RD, &vfstats->gotc,
5068 		    "Good Octets Transmitted");
5069 		SYSCTL_ADD_QUAD(ctx, stat_list, OID_AUTO, "mcast_pkts_recvd",
5070 		    CTLFLAG_RD, &vfstats->mprc,
5071 		    "Multicast Packets Received");
5072 		return;
5073 	}
5074 
5075 	stats = &sc->ustats.stats;
5076 
5077 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "excess_coll",
5078 	    CTLFLAG_RD, &stats->ecol,
5079 	    "Excessive collisions");
5080 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "single_coll",
5081 	    CTLFLAG_RD, &stats->scc,
5082 	    "Single collisions");
5083 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "multiple_coll",
5084 	    CTLFLAG_RD, &stats->mcc,
5085 	    "Multiple collisions");
5086 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "late_coll",
5087 	    CTLFLAG_RD, &stats->latecol,
5088 	    "Late collisions");
5089 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "collision_count",
5090 	    CTLFLAG_RD, &stats->colc,
5091 	    "Collision Count");
5092 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "symbol_errors",
5093 	    CTLFLAG_RD, &stats->symerrs,
5094 	    "Symbol Errors");
5095 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "sequence_errors",
5096 	    CTLFLAG_RD, &stats->sec,
5097 	    "Sequence Errors");
5098 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "defer_count",
5099 	    CTLFLAG_RD, &stats->dc,
5100 	    "Defer Count");
5101 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "missed_packets",
5102 	    CTLFLAG_RD, &stats->mpc,
5103 	    "Missed Packets");
5104 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_length_errors",
5105 	    CTLFLAG_RD, &stats->rlec,
5106 	    "Receive Length Errors");
5107 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_no_buff",
5108 	    CTLFLAG_RD, &stats->rnbc,
5109 	    "Receive No Buffers");
5110 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_undersize",
5111 	    CTLFLAG_RD, &stats->ruc,
5112 	    "Receive Undersize");
5113 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_fragmented",
5114 	    CTLFLAG_RD, &stats->rfc,
5115 	    "Fragmented Packets Received ");
5116 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_oversize",
5117 	    CTLFLAG_RD, &stats->roc,
5118 	    "Oversized Packets Received");
5119 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_jabber",
5120 	    CTLFLAG_RD, &stats->rjc,
5121 	    "Recevied Jabber");
5122 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_errs",
5123 	    CTLFLAG_RD, &stats->rxerrc,
5124 	    "Receive Errors");
5125 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "crc_errs",
5126 	    CTLFLAG_RD, &stats->crcerrs,
5127 	    "CRC errors");
5128 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "alignment_errs",
5129 	    CTLFLAG_RD, &stats->algnerrc,
5130 	    "Alignment Errors");
5131 	/* On 82575 these are collision counts */
5132 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "coll_ext_errs",
5133 	    CTLFLAG_RD, &stats->cexterr,
5134 	    "Collision/Carrier extension errors");
5135 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xon_recvd",
5136 	    CTLFLAG_RD, &stats->xonrxc,
5137 	    "XON Received");
5138 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xon_txd",
5139 	    CTLFLAG_RD, &stats->xontxc,
5140 	    "XON Transmitted");
5141 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xoff_recvd",
5142 	    CTLFLAG_RD, &stats->xoffrxc,
5143 	    "XOFF Received");
5144 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xoff_txd",
5145 	    CTLFLAG_RD, &stats->xofftxc,
5146 	    "XOFF Transmitted");
5147 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "unsupported_fc_recvd",
5148 	    CTLFLAG_RD, &stats->fcruc,
5149 	    "Unsupported Flow Control Received");
5150 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "mgmt_pkts_recvd",
5151 	    CTLFLAG_RD, &stats->mgprc,
5152 	    "Management Packets Received");
5153 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "mgmt_pkts_drop",
5154 	    CTLFLAG_RD, &stats->mgpdc,
5155 	    "Management Packets Dropped");
5156 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "mgmt_pkts_txd",
5157 	    CTLFLAG_RD, &stats->mgptc,
5158 	    "Management Packets Transmitted");
5159 
5160 	/* Packet Reception Stats */
5161 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "total_pkts_recvd",
5162 	    CTLFLAG_RD, &stats->tpr,
5163 	    "Total Packets Received ");
5164 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_pkts_recvd",
5165 	    CTLFLAG_RD, &stats->gprc,
5166 	    "Good Packets Received");
5167 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "bcast_pkts_recvd",
5168 	    CTLFLAG_RD, &stats->bprc,
5169 	    "Broadcast Packets Received");
5170 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "mcast_pkts_recvd",
5171 	    CTLFLAG_RD, &stats->mprc,
5172 	    "Multicast Packets Received");
5173 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_64",
5174 	    CTLFLAG_RD, &stats->prc64,
5175 	    "64 byte frames received ");
5176 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_65_127",
5177 	    CTLFLAG_RD, &stats->prc127,
5178 	    "65-127 byte frames received");
5179 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_128_255",
5180 	    CTLFLAG_RD, &stats->prc255,
5181 	    "128-255 byte frames received");
5182 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_256_511",
5183 	    CTLFLAG_RD, &stats->prc511,
5184 	    "256-511 byte frames received");
5185 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_512_1023",
5186 	    CTLFLAG_RD, &stats->prc1023,
5187 	    "512-1023 byte frames received");
5188 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_1024_1522",
5189 	    CTLFLAG_RD, &stats->prc1522,
5190 	    "1023-1522 byte frames received");
5191 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_octets_recvd",
5192 	    CTLFLAG_RD, &stats->gorc,
5193 	    "Good Octets Received");
5194 
5195 	/* Packet Transmission Stats */
5196 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_octets_txd",
5197 	    CTLFLAG_RD, &stats->gotc,
5198 	    "Good Octets Transmitted");
5199 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "total_pkts_txd",
5200 	    CTLFLAG_RD, &stats->tpt,
5201 	    "Total Packets Transmitted");
5202 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_pkts_txd",
5203 	    CTLFLAG_RD, &stats->gptc,
5204 	    "Good Packets Transmitted");
5205 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "bcast_pkts_txd",
5206 	    CTLFLAG_RD, &stats->bptc,
5207 	    "Broadcast Packets Transmitted");
5208 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "mcast_pkts_txd",
5209 	    CTLFLAG_RD, &stats->mptc,
5210 	    "Multicast Packets Transmitted");
5211 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_64",
5212 	    CTLFLAG_RD, &stats->ptc64,
5213 	    "64 byte frames transmitted ");
5214 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_65_127",
5215 	    CTLFLAG_RD, &stats->ptc127,
5216 	    "65-127 byte frames transmitted");
5217 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_128_255",
5218 	    CTLFLAG_RD, &stats->ptc255,
5219 	    "128-255 byte frames transmitted");
5220 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_256_511",
5221 	    CTLFLAG_RD, &stats->ptc511,
5222 	    "256-511 byte frames transmitted");
5223 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_512_1023",
5224 	    CTLFLAG_RD, &stats->ptc1023,
5225 	    "512-1023 byte frames transmitted");
5226 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_1024_1522",
5227 	    CTLFLAG_RD, &stats->ptc1522,
5228 	    "1024-1522 byte frames transmitted");
5229 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tso_txd",
5230 	    CTLFLAG_RD, &stats->tsctc,
5231 	    "TSO Contexts Transmitted");
5232 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tso_ctx_fail",
5233 	    CTLFLAG_RD, &stats->tsctfc,
5234 	    "TSO Contexts Failed");
5235 
5236 	/* Interrupt Stats */
5237 	int_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "interrupts",
5238 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Interrupt Statistics");
5239 	int_list = SYSCTL_CHILDREN(int_node);
5240 
5241 	SYSCTL_ADD_UQUAD(ctx, int_list, OID_AUTO, "asserts",
5242 	    CTLFLAG_RD, &stats->iac,
5243 	    "Interrupt Assertion Count");
5244 
5245 	SYSCTL_ADD_UQUAD(ctx, int_list, OID_AUTO, "rx_pkt_timer",
5246 	    CTLFLAG_RD, &stats->icrxptc,
5247 	    "Interrupt Cause Rx Pkt Timer Expire Count");
5248 
5249 	SYSCTL_ADD_UQUAD(ctx, int_list, OID_AUTO, "rx_abs_timer",
5250 	    CTLFLAG_RD, &stats->icrxatc,
5251 	    "Interrupt Cause Rx Abs Timer Expire Count");
5252 
5253 	SYSCTL_ADD_UQUAD(ctx, int_list, OID_AUTO, "tx_pkt_timer",
5254 	    CTLFLAG_RD, &stats->ictxptc,
5255 	    "Interrupt Cause Tx Pkt Timer Expire Count");
5256 
5257 	SYSCTL_ADD_UQUAD(ctx, int_list, OID_AUTO, "tx_abs_timer",
5258 	    CTLFLAG_RD, &stats->ictxatc,
5259 	    "Interrupt Cause Tx Abs Timer Expire Count");
5260 
5261 	SYSCTL_ADD_UQUAD(ctx, int_list, OID_AUTO, "tx_queue_empty",
5262 	    CTLFLAG_RD, &stats->ictxqec,
5263 	    "Interrupt Cause Tx Queue Empty Count");
5264 
5265 	SYSCTL_ADD_UQUAD(ctx, int_list, OID_AUTO, "tx_queue_min_thresh",
5266 	    CTLFLAG_RD, &stats->ictxqmtc,
5267 	    "Interrupt Cause Tx Queue Min Thresh Count");
5268 
5269 	SYSCTL_ADD_UQUAD(ctx, int_list, OID_AUTO, "rx_desc_min_thresh",
5270 	    CTLFLAG_RD, &stats->icrxdmtc,
5271 	    "Interrupt Cause Rx Desc Min Thresh Count");
5272 
5273 	SYSCTL_ADD_UQUAD(ctx, int_list, OID_AUTO, "rx_overrun",
5274 	    CTLFLAG_RD, &stats->icrxoc,
5275 	    "Interrupt Cause Receiver Overrun Count");
5276 }
5277 
5278 static void
em_fw_version_locked(if_ctx_t ctx)5279 em_fw_version_locked(if_ctx_t ctx)
5280 {
5281 	struct e1000_softc *sc = iflib_get_softc(ctx);
5282 	struct e1000_hw *hw = &sc->hw;
5283 	struct e1000_fw_version *fw_ver = &sc->fw_ver;
5284 	uint16_t eep = 0;
5285 
5286 	/*
5287 	 * em_fw_version_locked() must run under the IFLIB_CTX_LOCK to meet
5288 	 * the NVM locking model, so we do it in em_if_attach_pre() and store
5289 	 * the info in the softc
5290 	 */
5291 	ASSERT_CTX_LOCK_HELD(hw);
5292 
5293 	*fw_ver = (struct e1000_fw_version){0};
5294 
5295 	if (hw->mac.type >= igb_mac_min) {
5296 		/*
5297 		 * Use the Shared Code for igb(4)
5298 		 */
5299 		e1000_get_fw_version(hw, fw_ver);
5300 	} else {
5301 		/*
5302 		 * Otherwise, EEPROM version should be present on (almost?)
5303 		 * all devices here
5304 		 */
5305 		if(e1000_read_nvm(hw, NVM_VERSION, 1, &eep)) {
5306 			INIT_DEBUGOUT("can't get EEPROM version");
5307 			return;
5308 		}
5309 
5310 		fw_ver->eep_major = (eep & NVM_MAJOR_MASK) >> NVM_MAJOR_SHIFT;
5311 		fw_ver->eep_minor = (eep & NVM_MINOR_MASK) >> NVM_MINOR_SHIFT;
5312 		fw_ver->eep_build = (eep & NVM_IMAGE_ID_MASK);
5313 	}
5314 }
5315 
5316 static void
em_sbuf_fw_version(struct e1000_fw_version * fw_ver,struct sbuf * buf)5317 em_sbuf_fw_version(struct e1000_fw_version *fw_ver, struct sbuf *buf)
5318 {
5319 	const char *space = "";
5320 
5321 	if (fw_ver->eep_major || fw_ver->eep_minor || fw_ver->eep_build) {
5322 		sbuf_printf(buf, "EEPROM V%d.%d-%d", fw_ver->eep_major,
5323 			    fw_ver->eep_minor, fw_ver->eep_build);
5324 		space = " ";
5325 	}
5326 
5327 	if (fw_ver->invm_major || fw_ver->invm_minor ||
5328 	    fw_ver->invm_img_type) {
5329 		sbuf_printf(buf, "%sNVM V%d.%d imgtype%d",
5330 		    space, fw_ver->invm_major, fw_ver->invm_minor,
5331 		    fw_ver->invm_img_type);
5332 		space = " ";
5333 	}
5334 
5335 	if (fw_ver->or_valid) {
5336 		sbuf_printf(buf, "%sOption ROM V%d-b%d-p%d",
5337 		    space, fw_ver->or_major, fw_ver->or_build,
5338 		    fw_ver->or_patch);
5339 		space = " ";
5340 	}
5341 
5342 	if (fw_ver->etrack_id)
5343 		sbuf_printf(buf, "%seTrack 0x%08x", space, fw_ver->etrack_id);
5344 }
5345 
5346 static void
em_print_fw_version(struct e1000_softc * sc)5347 em_print_fw_version(struct e1000_softc *sc )
5348 {
5349 	device_t dev = sc->dev;
5350 	struct sbuf *buf;
5351 	int error = 0;
5352 
5353 	buf = sbuf_new_auto();
5354 	if (!buf) {
5355 		device_printf(dev, "Could not allocate sbuf for output.\n");
5356 		return;
5357 	}
5358 
5359 	em_sbuf_fw_version(&sc->fw_ver, buf);
5360 
5361 	error = sbuf_finish(buf);
5362 	if (error)
5363 		device_printf(dev, "Error finishing sbuf: %d\n", error);
5364 	else if (sbuf_len(buf))
5365 		device_printf(dev, "%s\n", sbuf_data(buf));
5366 
5367 	sbuf_delete(buf);
5368 }
5369 
5370 static int
em_sysctl_print_fw_version(SYSCTL_HANDLER_ARGS)5371 em_sysctl_print_fw_version(SYSCTL_HANDLER_ARGS)
5372 {
5373 	struct e1000_softc *sc = (struct e1000_softc *)arg1;
5374 	device_t dev = sc->dev;
5375 	struct sbuf *buf;
5376 	int error = 0;
5377 
5378 	buf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
5379 	if (!buf) {
5380 		device_printf(dev, "Could not allocate sbuf for output.\n");
5381 		return (ENOMEM);
5382 	}
5383 
5384 	em_sbuf_fw_version(&sc->fw_ver, buf);
5385 
5386 	error = sbuf_finish(buf);
5387 	if (error)
5388 		device_printf(dev, "Error finishing sbuf: %d\n", error);
5389 
5390 	sbuf_delete(buf);
5391 
5392 	return (0);
5393 }
5394 
5395 /**********************************************************************
5396  *
5397  *  This routine provides a way to dump out the adapter eeprom,
5398  *  often a useful debug/service tool. This only dumps the first
5399  *  32 words, stuff that matters is in that extent.
5400  *
5401  **********************************************************************/
5402 static int
em_sysctl_nvm_info(SYSCTL_HANDLER_ARGS)5403 em_sysctl_nvm_info(SYSCTL_HANDLER_ARGS)
5404 {
5405 	struct e1000_softc *sc = (struct e1000_softc *)arg1;
5406 	int error;
5407 	int result;
5408 
5409 	result = -1;
5410 	error = sysctl_handle_int(oidp, &result, 0, req);
5411 
5412 	if (error || !req->newptr)
5413 		return (error);
5414 
5415 	/*
5416 	 * This value will cause a hex dump of the
5417 	 * first 32 16-bit words of the EEPROM to
5418 	 * the screen.
5419 	 */
5420 	if (result == 1)
5421 		em_print_nvm_info(sc);
5422 
5423 	return (error);
5424 }
5425 
5426 static void
em_print_nvm_info(struct e1000_softc * sc)5427 em_print_nvm_info(struct e1000_softc *sc)
5428 {
5429 	struct e1000_hw *hw = &sc->hw;
5430 	struct sx *iflib_ctx_lock = iflib_ctx_lock_get(sc->ctx);
5431 	u16 eeprom_data;
5432 	int i, j, row = 0;
5433 
5434 	/* Its a bit crude, but it gets the job done */
5435 	printf("\nInterface EEPROM Dump:\n");
5436 	printf("Offset\n0x0000  ");
5437 
5438 	/* We rely on the IFLIB_CTX_LOCK as part of NVM locking model */
5439 	sx_xlock(iflib_ctx_lock);
5440 	ASSERT_CTX_LOCK_HELD(hw);
5441 	for (i = 0, j = 0; i < 32; i++, j++) {
5442 		if (j == 8) { /* Make the offset block */
5443 			j = 0; ++row;
5444 			printf("\n0x00%x0  ",row);
5445 		}
5446 		e1000_read_nvm(hw, i, 1, &eeprom_data);
5447 		printf("%04x ", eeprom_data);
5448 	}
5449 	sx_xunlock(iflib_ctx_lock);
5450 	printf("\n");
5451 }
5452 
5453 static int
em_sysctl_int_delay(SYSCTL_HANDLER_ARGS)5454 em_sysctl_int_delay(SYSCTL_HANDLER_ARGS)
5455 {
5456 	struct em_int_delay_info *info;
5457 	struct e1000_softc *sc;
5458 	u32 regval;
5459 	int error, usecs, ticks;
5460 
5461 	info = (struct em_int_delay_info *) arg1;
5462 	usecs = info->value;
5463 	error = sysctl_handle_int(oidp, &usecs, 0, req);
5464 	if (error != 0 || req->newptr == NULL)
5465 		return (error);
5466 	if (usecs < 0 || usecs > EM_TICKS_TO_USECS(65535))
5467 		return (EINVAL);
5468 	info->value = usecs;
5469 	ticks = EM_USECS_TO_TICKS(usecs);
5470 
5471 	sc = info->sc;
5472 
5473 	regval = E1000_READ_OFFSET(&sc->hw, info->offset);
5474 	regval = (regval & ~0xffff) | (ticks & 0xffff);
5475 	/* Handle a few special cases. */
5476 	switch (info->offset) {
5477 	case E1000_RDTR:
5478 		break;
5479 	case E1000_TIDV:
5480 		if (ticks == 0) {
5481 			sc->txd_cmd &= ~E1000_TXD_CMD_IDE;
5482 			/* Don't write 0 into the TIDV register. */
5483 			regval++;
5484 		} else
5485 			sc->txd_cmd |= E1000_TXD_CMD_IDE;
5486 		break;
5487 	}
5488 	E1000_WRITE_OFFSET(&sc->hw, info->offset, regval);
5489 	return (0);
5490 }
5491 
5492 static int
em_sysctl_tso_tcp_flags_mask(SYSCTL_HANDLER_ARGS)5493 em_sysctl_tso_tcp_flags_mask(SYSCTL_HANDLER_ARGS)
5494 {
5495 	struct e1000_softc *sc;
5496 	u32 reg, val, shift;
5497 	int error, mask;
5498 
5499 	sc = oidp->oid_arg1;
5500 	switch (oidp->oid_arg2) {
5501 	case 0:
5502 		reg = E1000_DTXTCPFLGL;
5503 		shift = 0;
5504 		break;
5505 	case 1:
5506 		reg = E1000_DTXTCPFLGL;
5507 		shift = 16;
5508 		break;
5509 	case 2:
5510 		reg = E1000_DTXTCPFLGH;
5511 		shift = 0;
5512 		break;
5513 	default:
5514 		return (EINVAL);
5515 		break;
5516 	}
5517 	val = E1000_READ_REG(&sc->hw, reg);
5518 	mask = (val >> shift) & 0xfff;
5519 	error = sysctl_handle_int(oidp, &mask, 0, req);
5520 	if (error != 0 || req->newptr == NULL)
5521 		return (error);
5522 	if (mask < 0 || mask > 0xfff)
5523 		return (EINVAL);
5524 	val = (val & ~(0xfff << shift)) | (mask << shift);
5525 	E1000_WRITE_REG(&sc->hw, reg, val);
5526 	return (0);
5527 }
5528 
5529 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)5530 em_add_int_delay_sysctl(struct e1000_softc *sc, const char *name,
5531     const char *description, struct em_int_delay_info *info, int offset,
5532     int value)
5533 {
5534 	info->sc = sc;
5535 	info->offset = offset;
5536 	info->value = value;
5537 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->dev),
5538 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
5539 	    OID_AUTO, name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
5540 	    info, 0, em_sysctl_int_delay, "I", description);
5541 }
5542 
5543 /*
5544  * Set flow control using sysctl:
5545  * Flow control values:
5546  *      0 - off
5547  *      1 - rx pause
5548  *      2 - tx pause
5549  *      3 - full
5550  */
5551 static int
em_set_flowcntl(SYSCTL_HANDLER_ARGS)5552 em_set_flowcntl(SYSCTL_HANDLER_ARGS)
5553 {
5554 	int error;
5555 	static int input = 3; /* default is full */
5556 	struct e1000_softc *sc = (struct e1000_softc *) arg1;
5557 
5558 	error = sysctl_handle_int(oidp, &input, 0, req);
5559 
5560 	if ((error) || (req->newptr == NULL))
5561 		return (error);
5562 
5563 	if (input == sc->fc) /* no change? */
5564 		return (error);
5565 
5566 	switch (input) {
5567 	case e1000_fc_rx_pause:
5568 	case e1000_fc_tx_pause:
5569 	case e1000_fc_full:
5570 	case e1000_fc_none:
5571 		sc->hw.fc.requested_mode = input;
5572 		sc->fc = input;
5573 		break;
5574 	default:
5575 		/* Do nothing */
5576 		return (error);
5577 	}
5578 
5579 	sc->hw.fc.current_mode = sc->hw.fc.requested_mode;
5580 	e1000_force_mac_fc(&sc->hw);
5581 	return (error);
5582 }
5583 
5584 /*
5585  * Manage DMA Coalesce:
5586  * Control values:
5587  * 	0/1 - off/on
5588  *	Legal timer values are:
5589  *	250,500,1000-10000 in thousands
5590  */
5591 static int
igb_sysctl_dmac(SYSCTL_HANDLER_ARGS)5592 igb_sysctl_dmac(SYSCTL_HANDLER_ARGS)
5593 {
5594 	struct e1000_softc *sc = (struct e1000_softc *) arg1;
5595 	int error;
5596 
5597 	error = sysctl_handle_int(oidp, &sc->dmac, 0, req);
5598 
5599 	if ((error) || (req->newptr == NULL))
5600 		return (error);
5601 
5602 	switch (sc->dmac) {
5603 		case 0:
5604 			/* Disabling */
5605 			break;
5606 		case 1: /* Just enable and use default */
5607 			sc->dmac = 1000;
5608 			break;
5609 		case 250:
5610 		case 500:
5611 		case 1000:
5612 		case 2000:
5613 		case 3000:
5614 		case 4000:
5615 		case 5000:
5616 		case 6000:
5617 		case 7000:
5618 		case 8000:
5619 		case 9000:
5620 		case 10000:
5621 			/* Legal values - allow */
5622 			break;
5623 		default:
5624 			/* Do nothing, illegal value */
5625 			sc->dmac = 0;
5626 			return (EINVAL);
5627 	}
5628 	/* Reinit the interface */
5629 	em_if_init(sc->ctx);
5630 	return (error);
5631 }
5632 
5633 /*
5634  * Manage Energy Efficient Ethernet:
5635  * Control values:
5636  *     0/1 - enabled/disabled
5637  */
5638 static int
em_sysctl_eee(SYSCTL_HANDLER_ARGS)5639 em_sysctl_eee(SYSCTL_HANDLER_ARGS)
5640 {
5641 	struct e1000_softc *sc = (struct e1000_softc *) arg1;
5642 	int error, value;
5643 
5644 	if (sc->hw.mac.type < igb_mac_min)
5645 		value = sc->hw.dev_spec.ich8lan.eee_disable;
5646 	else
5647 		value = sc->hw.dev_spec._82575.eee_disable;
5648 	error = sysctl_handle_int(oidp, &value, 0, req);
5649 	if (error || req->newptr == NULL)
5650 		return (error);
5651 	if (sc->hw.mac.type < igb_mac_min)
5652 		sc->hw.dev_spec.ich8lan.eee_disable = (value != 0);
5653 	else
5654 		sc->hw.dev_spec._82575.eee_disable = (value != 0);
5655 	em_if_init(sc->ctx);
5656 
5657 	return (0);
5658 }
5659 
5660 static int
em_sysctl_debug_info(SYSCTL_HANDLER_ARGS)5661 em_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
5662 {
5663 	struct e1000_softc *sc;
5664 	int error;
5665 	int result;
5666 
5667 	result = -1;
5668 	error = sysctl_handle_int(oidp, &result, 0, req);
5669 
5670 	if (error || !req->newptr)
5671 		return (error);
5672 
5673 	if (result == 1) {
5674 		sc = (struct e1000_softc *) arg1;
5675 		em_print_debug_info(sc);
5676 	}
5677 
5678 	return (error);
5679 }
5680 
5681 static int
em_get_rs(SYSCTL_HANDLER_ARGS)5682 em_get_rs(SYSCTL_HANDLER_ARGS)
5683 {
5684 	struct e1000_softc *sc = (struct e1000_softc *) arg1;
5685 	int error;
5686 	int result;
5687 
5688 	result = 0;
5689 	error = sysctl_handle_int(oidp, &result, 0, req);
5690 
5691 	if (error || !req->newptr || result != 1)
5692 		return (error);
5693 	em_dump_rs(sc);
5694 
5695 	return (error);
5696 }
5697 
5698 static void
em_if_debug(if_ctx_t ctx)5699 em_if_debug(if_ctx_t ctx)
5700 {
5701 	em_dump_rs(iflib_get_softc(ctx));
5702 }
5703 
5704 /*
5705  * This routine is meant to be fluid, add whatever is
5706  * needed for debugging a problem.  -jfv
5707  */
5708 static void
em_print_debug_info(struct e1000_softc * sc)5709 em_print_debug_info(struct e1000_softc *sc)
5710 {
5711 	device_t dev = iflib_get_dev(sc->ctx);
5712 	if_t ifp = iflib_get_ifp(sc->ctx);
5713 	struct tx_ring *txr = &sc->tx_queues->txr;
5714 	struct rx_ring *rxr = &sc->rx_queues->rxr;
5715 
5716 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
5717 		printf("Interface is RUNNING ");
5718 	else
5719 		printf("Interface is NOT RUNNING\n");
5720 
5721 	if (if_getdrvflags(ifp) & IFF_DRV_OACTIVE)
5722 		printf("and INACTIVE\n");
5723 	else
5724 		printf("and ACTIVE\n");
5725 
5726 	for (int i = 0; i < sc->tx_num_queues; i++, txr++) {
5727 		device_printf(dev, "TX Queue %d ------\n", i);
5728 		device_printf(dev, "hw tdh = %d, hw tdt = %d\n",
5729 		    E1000_READ_REG(&sc->hw, E1000_TDH(i)),
5730 		    E1000_READ_REG(&sc->hw, E1000_TDT(i)));
5731 
5732 	}
5733 	for (int j=0; j < sc->rx_num_queues; j++, rxr++) {
5734 		device_printf(dev, "RX Queue %d ------\n", j);
5735 		device_printf(dev, "hw rdh = %d, hw rdt = %d\n",
5736 		    E1000_READ_REG(&sc->hw, E1000_RDH(j)),
5737 		    E1000_READ_REG(&sc->hw, E1000_RDT(j)));
5738 	}
5739 }
5740 
5741 /*
5742  * 82574 only:
5743  * Write a new value to the EEPROM increasing the number of MSI-X
5744  * vectors from 3 to 5, for proper multiqueue support.
5745  */
5746 static void
em_enable_vectors_82574(if_ctx_t ctx)5747 em_enable_vectors_82574(if_ctx_t ctx)
5748 {
5749 	struct e1000_softc *sc = iflib_get_softc(ctx);
5750 	struct e1000_hw *hw = &sc->hw;
5751 	device_t dev = iflib_get_dev(ctx);
5752 	u16 edata;
5753 
5754 	e1000_read_nvm(hw, EM_NVM_PCIE_CTRL, 1, &edata);
5755 	if (bootverbose)
5756 		device_printf(dev, "EM_NVM_PCIE_CTRL = %#06x\n", edata);
5757 	if (((edata & EM_NVM_MSIX_N_MASK) >> EM_NVM_MSIX_N_SHIFT) != 4) {
5758 		device_printf(dev, "Writing to eeprom: increasing "
5759 		    "reported MSI-X vectors from 3 to 5...\n");
5760 		edata &= ~(EM_NVM_MSIX_N_MASK);
5761 		edata |= 4 << EM_NVM_MSIX_N_SHIFT;
5762 		e1000_write_nvm(hw, EM_NVM_PCIE_CTRL, 1, &edata);
5763 		e1000_update_nvm_checksum(hw);
5764 		device_printf(dev, "Writing to eeprom: done\n");
5765 	}
5766 }
5767