xref: /openbsd-src/sys/dev/pci/if_em.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /**************************************************************************
2 
3 Copyright (c) 2001-2003, Intel Corporation
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9  1. Redistributions of source code must retain the above copyright notice,
10     this list of conditions and the following disclaimer.
11 
12  2. Redistributions in binary form must reproduce the above copyright
13     notice, this list of conditions and the following disclaimer in the
14     documentation and/or other materials provided with the distribution.
15 
16  3. Neither the name of the Intel Corporation nor the names of its
17     contributors may be used to endorse or promote products derived from
18     this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
31 
32 ***************************************************************************/
33 
34 /* $OpenBSD: if_em.c,v 1.331 2016/04/13 10:34:32 mpi Exp $ */
35 /* $FreeBSD: if_em.c,v 1.46 2004/09/29 18:28:28 mlaier Exp $ */
36 
37 #include <dev/pci/if_em.h>
38 #include <dev/pci/if_em_soc.h>
39 
40 /*********************************************************************
41  *  Driver version
42  *********************************************************************/
43 
44 #define EM_DRIVER_VERSION	"6.2.9"
45 
46 /*********************************************************************
47  *  PCI Device ID Table
48  *********************************************************************/
49 const struct pci_matchid em_devices[] = {
50 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_80003ES2LAN_CPR_DPT },
51 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_80003ES2LAN_SDS_DPT },
52 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_80003ES2LAN_CPR_SPT },
53 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_80003ES2LAN_SDS_SPT },
54 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82540EM },
55 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82540EM_LOM },
56 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82540EP },
57 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82540EP_LOM },
58 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82540EP_LP },
59 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82541EI },
60 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82541EI_MOBILE },
61 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82541ER },
62 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82541ER_LOM },
63 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82541GI },
64 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82541GI_LF },
65 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82541GI_MOBILE },
66 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82542 },
67 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82543GC_COPPER },
68 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82543GC_FIBER },
69 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82544EI_COPPER },
70 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82544EI_FIBER },
71 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82544GC_COPPER },
72 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82544GC_LOM },
73 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82545EM_COPPER },
74 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82545EM_FIBER },
75 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82545GM_COPPER },
76 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82545GM_FIBER },
77 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82545GM_SERDES },
78 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82546EB_COPPER },
79 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82546EB_FIBER },
80 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82546EB_QUAD_CPR },
81 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82546GB_COPPER },
82 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82546GB_FIBER },
83 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82546GB_PCIE },
84 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82546GB_QUAD_CPR },
85 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82546GB_QUAD_CPR_K },
86 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82546GB_SERDES },
87 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82546GB_2 },
88 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82547EI },
89 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82547EI_MOBILE },
90 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82547GI },
91 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82571EB_AF },
92 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82571EB_AT },
93 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82571EB_COPPER },
94 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82571EB_FIBER },
95 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82571EB_QUAD_CPR },
96 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82571EB_QUAD_CPR_LP },
97 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82571EB_QUAD_FBR },
98 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82571EB_SERDES },
99 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82571EB_SDS_DUAL },
100 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82571EB_SDS_QUAD },
101 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82571PT_QUAD_CPR },
102 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82572EI_COPPER },
103 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82572EI_FIBER },
104 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82572EI_SERDES },
105 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82572EI },
106 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82573E },
107 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82573E_IAMT },
108 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82573E_PM },
109 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82573L },
110 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82573L_PL_1 },
111 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82573L_PL_2 },
112 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82573V_PM },
113 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82574L },
114 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82574LA },
115 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82575EB_COPPER },
116 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82575EB_SERDES },
117 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82575GB_QUAD_CPR },
118 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82575GB_QP_PM },
119 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82576 },
120 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82576_FIBER },
121 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82576_SERDES },
122 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82576_QUAD_COPPER },
123 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82576_QUAD_CU_ET2 },
124 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82576_NS },
125 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82576_NS_SERDES },
126 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82576_SERDES_QUAD },
127 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82577LC },
128 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82577LM },
129 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82578DC },
130 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82578DM },
131 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82579LM },
132 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82579V },
133 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I210_COPPER },
134 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I210_FIBER },
135 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I210_SERDES },
136 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I210_SGMII },
137 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I210_COPPER_NF },
138 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I210_SERDES_NF },
139 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I211_COPPER },
140 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I217_LM },
141 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I217_V },
142 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I218_LM },
143 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I218_LM_2 },
144 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I218_LM_3 },
145 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I218_V },
146 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I218_V_2 },
147 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I218_V_3 },
148 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I219_LM },
149 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I219_V },
150 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I219_LM2 },
151 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I219_V2 },
152 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82580_COPPER },
153 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82580_FIBER },
154 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82580_SERDES },
155 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82580_SGMII },
156 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82580_COPPER_DUAL },
157 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82580_QUAD_FIBER },
158 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_DH89XXCC_SGMII },
159 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_DH89XXCC_SERDES },
160 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_DH89XXCC_BPLANE },
161 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_DH89XXCC_SFP },
162 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82583V },
163 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I350_COPPER },
164 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I350_FIBER },
165 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I350_SERDES },
166 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I350_SGMII },
167 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I354_BP_1GBPS },
168 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I354_BP_2_5GBPS },
169 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I354_SGMII },
170 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_ICH8_82567V_3 },
171 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_ICH8_IFE },
172 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_ICH8_IFE_G },
173 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_ICH8_IFE_GT },
174 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_ICH8_IGP_AMT },
175 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_ICH8_IGP_C },
176 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_ICH8_IGP_M },
177 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_ICH8_IGP_M_AMT },
178 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_ICH9_BM },
179 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_ICH9_IFE },
180 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_ICH9_IFE_G },
181 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_ICH9_IFE_GT },
182 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_ICH9_IGP_AMT },
183 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_ICH9_IGP_C },
184 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_ICH9_IGP_M },
185 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_ICH9_IGP_M_AMT },
186 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_ICH9_IGP_M_V },
187 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_ICH10_D_BM_LF },
188 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_ICH10_D_BM_LM },
189 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_ICH10_R_BM_LF },
190 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_ICH10_R_BM_LM },
191 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_ICH10_R_BM_V },
192 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_EP80579_LAN_1 },
193 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_EP80579_LAN_2 },
194 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_EP80579_LAN_3 },
195 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_EP80579_LAN_4 },
196 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_EP80579_LAN_5 },
197 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_EP80579_LAN_6 }
198 };
199 
200 /*********************************************************************
201  *  Function prototypes
202  *********************************************************************/
203 int  em_probe(struct device *, void *, void *);
204 void em_attach(struct device *, struct device *, void *);
205 void em_defer_attach(struct device*);
206 int  em_detach(struct device *, int);
207 int  em_activate(struct device *, int);
208 int  em_intr(void *);
209 void em_start(struct ifnet *);
210 int  em_ioctl(struct ifnet *, u_long, caddr_t);
211 void em_watchdog(struct ifnet *);
212 void em_init(void *);
213 void em_stop(void *, int);
214 void em_media_status(struct ifnet *, struct ifmediareq *);
215 int  em_media_change(struct ifnet *);
216 uint64_t  em_flowstatus(struct em_softc *);
217 void em_identify_hardware(struct em_softc *);
218 int  em_allocate_pci_resources(struct em_softc *);
219 void em_free_pci_resources(struct em_softc *);
220 void em_local_timer(void *);
221 int  em_hardware_init(struct em_softc *);
222 void em_setup_interface(struct em_softc *);
223 int  em_setup_transmit_structures(struct em_softc *);
224 void em_initialize_transmit_unit(struct em_softc *);
225 int  em_setup_receive_structures(struct em_softc *);
226 void em_initialize_receive_unit(struct em_softc *);
227 void em_enable_intr(struct em_softc *);
228 void em_disable_intr(struct em_softc *);
229 void em_free_transmit_structures(struct em_softc *);
230 void em_free_receive_structures(struct em_softc *);
231 void em_update_stats_counters(struct em_softc *);
232 void em_disable_aspm(struct em_softc *);
233 void em_txeof(struct em_softc *);
234 int  em_allocate_receive_structures(struct em_softc *);
235 int  em_allocate_transmit_structures(struct em_softc *);
236 int  em_rxfill(struct em_softc *);
237 int  em_rxeof(struct em_softc *);
238 void em_receive_checksum(struct em_softc *, struct em_rx_desc *,
239 			 struct mbuf *);
240 u_int	em_transmit_checksum_setup(struct em_softc *, struct mbuf *, u_int,
241 	    u_int32_t *, u_int32_t *);
242 void em_iff(struct em_softc *);
243 #ifdef EM_DEBUG
244 void em_print_hw_stats(struct em_softc *);
245 #endif
246 void em_update_link_status(struct em_softc *);
247 int  em_get_buf(struct em_softc *, int);
248 void em_enable_hw_vlans(struct em_softc *);
249 u_int em_encap(struct em_softc *, struct mbuf *);
250 void em_smartspeed(struct em_softc *);
251 int  em_82547_fifo_workaround(struct em_softc *, int);
252 void em_82547_update_fifo_head(struct em_softc *, int);
253 int  em_82547_tx_fifo_reset(struct em_softc *);
254 void em_82547_move_tail(void *arg);
255 void em_82547_move_tail_locked(struct em_softc *);
256 int  em_dma_malloc(struct em_softc *, bus_size_t, struct em_dma_alloc *);
257 void em_dma_free(struct em_softc *, struct em_dma_alloc *);
258 u_int32_t em_fill_descriptors(u_int64_t address, u_int32_t length,
259 			      PDESC_ARRAY desc_array);
260 void em_flush_tx_ring(struct em_softc *);
261 void em_flush_rx_ring(struct em_softc *);
262 void em_flush_desc_rings(struct em_softc *);
263 
264 /*********************************************************************
265  *  OpenBSD Device Interface Entry Points
266  *********************************************************************/
267 
268 struct cfattach em_ca = {
269 	sizeof(struct em_softc), em_probe, em_attach, em_detach,
270 	em_activate
271 };
272 
273 struct cfdriver em_cd = {
274 	NULL, "em", DV_IFNET
275 };
276 
277 static int em_smart_pwr_down = FALSE;
278 
279 /*********************************************************************
280  *  Device identification routine
281  *
282  *  em_probe determines if the driver should be loaded on
283  *  adapter based on PCI vendor/device id of the adapter.
284  *
285  *  return 0 on no match, positive on match
286  *********************************************************************/
287 
288 int
289 em_probe(struct device *parent, void *match, void *aux)
290 {
291 	INIT_DEBUGOUT("em_probe: begin");
292 
293 	return (pci_matchbyid((struct pci_attach_args *)aux, em_devices,
294 	    nitems(em_devices)));
295 }
296 
297 void
298 em_defer_attach(struct device *self)
299 {
300 	struct em_softc *sc = (struct em_softc *)self;
301 	struct pci_attach_args *pa = &sc->osdep.em_pa;
302 	pci_chipset_tag_t	pc = pa->pa_pc;
303 	void *gcu;
304 
305 	INIT_DEBUGOUT("em_defer_attach: begin");
306 
307 	if ((gcu = em_lookup_gcu(self)) == 0) {
308 		printf("%s: No GCU found, defered attachment failed\n",
309 		    DEVNAME(sc));
310 
311 		if (sc->sc_intrhand)
312 			pci_intr_disestablish(pc, sc->sc_intrhand);
313 		sc->sc_intrhand = 0;
314 
315 		em_stop(sc, 1);
316 
317 		em_free_pci_resources(sc);
318 
319 		sc->sc_rx_desc_ring = NULL;
320 		em_dma_free(sc, &sc->sc_rx_dma);
321 		sc->sc_tx_desc_ring = NULL;
322 		em_dma_free(sc, &sc->sc_tx_dma);
323 
324 		return;
325 	}
326 
327 	sc->hw.gcu = gcu;
328 
329 	em_attach_miibus(self);
330 
331 	em_setup_interface(sc);
332 
333 	em_setup_link(&sc->hw);
334 
335 	em_update_link_status(sc);
336 }
337 
338 /*********************************************************************
339  *  Device initialization routine
340  *
341  *  The attach entry point is called when the driver is being loaded.
342  *  This routine identifies the type of hardware, allocates all resources
343  *  and initializes the hardware.
344  *
345  *********************************************************************/
346 
347 void
348 em_attach(struct device *parent, struct device *self, void *aux)
349 {
350 	struct pci_attach_args *pa = aux;
351 	struct em_softc *sc;
352 	int defer = 0;
353 
354 	INIT_DEBUGOUT("em_attach: begin");
355 
356 	sc = (struct em_softc *)self;
357 	sc->sc_dmat = pa->pa_dmat;
358 	sc->osdep.em_pa = *pa;
359 
360 	timeout_set(&sc->timer_handle, em_local_timer, sc);
361 	timeout_set(&sc->tx_fifo_timer_handle, em_82547_move_tail, sc);
362 
363 	/* Determine hardware revision */
364 	em_identify_hardware(sc);
365 
366 	/*
367 	 * Only use MSI on the newer PCIe parts, with the exception
368 	 * of 82571/82572 due to "Byte Enables 2 and 3 Are Not Set" errata
369 	 */
370 	if (sc->hw.mac_type <= em_82572)
371 		sc->osdep.em_pa.pa_flags &= ~PCI_FLAGS_MSI_ENABLED;
372 
373 	/* Parameters (to be read from user) */
374 	if (sc->hw.mac_type >= em_82544) {
375 		sc->sc_tx_slots = EM_MAX_TXD;
376 		sc->sc_rx_slots = EM_MAX_RXD;
377 	} else {
378 		sc->sc_tx_slots = EM_MAX_TXD_82543;
379 		sc->sc_rx_slots = EM_MAX_RXD_82543;
380 	}
381 	sc->tx_int_delay = EM_TIDV;
382 	sc->tx_abs_int_delay = EM_TADV;
383 	sc->rx_int_delay = EM_RDTR;
384 	sc->rx_abs_int_delay = EM_RADV;
385 	sc->hw.autoneg = DO_AUTO_NEG;
386 	sc->hw.wait_autoneg_complete = WAIT_FOR_AUTO_NEG_DEFAULT;
387 	sc->hw.autoneg_advertised = AUTONEG_ADV_DEFAULT;
388 	sc->hw.tbi_compatibility_en = TRUE;
389 	sc->sc_rx_buffer_len = EM_RXBUFFER_2048;
390 
391 	sc->hw.phy_init_script = 1;
392 	sc->hw.phy_reset_disable = FALSE;
393 
394 #ifndef EM_MASTER_SLAVE
395 	sc->hw.master_slave = em_ms_hw_default;
396 #else
397 	sc->hw.master_slave = EM_MASTER_SLAVE;
398 #endif
399 
400 	/*
401 	 * This controls when hardware reports transmit completion
402 	 * status.
403 	 */
404 	sc->hw.report_tx_early = 1;
405 
406 	if (em_allocate_pci_resources(sc))
407 		goto err_pci;
408 
409 	/* Initialize eeprom parameters */
410 	em_init_eeprom_params(&sc->hw);
411 
412 	/*
413 	 * Set the max frame size assuming standard Ethernet
414 	 * sized frames.
415 	 */
416 	switch (sc->hw.mac_type) {
417 		case em_82573:
418 		{
419 			uint16_t	eeprom_data = 0;
420 
421 			/*
422 			 * 82573 only supports Jumbo frames
423 			 * if ASPM is disabled.
424 			 */
425 			em_read_eeprom(&sc->hw, EEPROM_INIT_3GIO_3,
426 			    1, &eeprom_data);
427 			if (eeprom_data & EEPROM_WORD1A_ASPM_MASK) {
428 				sc->hw.max_frame_size = ETHER_MAX_LEN;
429 				break;
430 			}
431 			/* Allow Jumbo frames */
432 			/* FALLTHROUGH */
433 		}
434 		case em_82571:
435 		case em_82572:
436 		case em_82574:
437 		case em_82575:
438 		case em_82580:
439 		case em_i210:
440 		case em_i350:
441 		case em_ich9lan:
442 		case em_ich10lan:
443 		case em_pch2lan:
444 		case em_pch_lpt:
445 		case em_pch_spt:
446 		case em_80003es2lan:
447 			/* 9K Jumbo Frame size */
448 			sc->hw.max_frame_size = 9234;
449 			break;
450 		case em_pchlan:
451 			sc->hw.max_frame_size = 4096;
452 			break;
453 		case em_82542_rev2_0:
454 		case em_82542_rev2_1:
455 		case em_ich8lan:
456 			/* Adapters that do not support Jumbo frames */
457 			sc->hw.max_frame_size = ETHER_MAX_LEN;
458 			break;
459 		default:
460 			sc->hw.max_frame_size =
461 			    MAX_JUMBO_FRAME_SIZE;
462 	}
463 
464 	sc->hw.min_frame_size =
465 	    ETHER_MIN_LEN + ETHER_CRC_LEN;
466 
467 	/* Allocate Transmit Descriptor ring */
468 	if (em_dma_malloc(sc, sc->sc_tx_slots * sizeof(struct em_tx_desc),
469 	    &sc->sc_tx_dma) != 0) {
470 		printf("%s: Unable to allocate tx_desc memory\n",
471 		       DEVNAME(sc));
472 		goto err_tx_desc;
473 	}
474 	sc->sc_tx_desc_ring = (struct em_tx_desc *)sc->sc_tx_dma.dma_vaddr;
475 
476 	/* Allocate Receive Descriptor ring */
477 	if (em_dma_malloc(sc, sc->sc_rx_slots * sizeof(struct em_rx_desc),
478 	    &sc->sc_rx_dma) != 0) {
479 		printf("%s: Unable to allocate rx_desc memory\n",
480 		       DEVNAME(sc));
481 		goto err_rx_desc;
482 	}
483 	sc->sc_rx_desc_ring = (struct em_rx_desc *)sc->sc_rx_dma.dma_vaddr;
484 
485 	/* Initialize the hardware */
486 	if ((defer = em_hardware_init(sc))) {
487 		if (defer == EAGAIN)
488 			config_defer(self, em_defer_attach);
489 		else {
490 			printf("%s: Unable to initialize the hardware\n",
491 			    DEVNAME(sc));
492 			goto err_hw_init;
493 		}
494 	}
495 
496 	if (sc->hw.mac_type == em_80003es2lan || sc->hw.mac_type == em_82575 ||
497 	    sc->hw.mac_type == em_82580 || sc->hw.mac_type == em_i210 ||
498 	    sc->hw.mac_type == em_i350) {
499 		uint32_t reg = EM_READ_REG(&sc->hw, E1000_STATUS);
500 		sc->hw.bus_func = (reg & E1000_STATUS_FUNC_MASK) >>
501 		    E1000_STATUS_FUNC_SHIFT;
502 
503 		switch (sc->hw.bus_func) {
504 		case 0:
505 			sc->hw.swfw = E1000_SWFW_PHY0_SM;
506 			break;
507 		case 1:
508 			sc->hw.swfw = E1000_SWFW_PHY1_SM;
509 			break;
510 		case 2:
511 			sc->hw.swfw = E1000_SWFW_PHY2_SM;
512 			break;
513 		case 3:
514 			sc->hw.swfw = E1000_SWFW_PHY3_SM;
515 			break;
516 		}
517 	} else {
518 		sc->hw.bus_func = 0;
519 	}
520 
521 	/* Copy the permanent MAC address out of the EEPROM */
522 	if (em_read_mac_addr(&sc->hw) < 0) {
523 		printf("%s: EEPROM read error while reading mac address\n",
524 		       DEVNAME(sc));
525 		goto err_mac_addr;
526 	}
527 
528 	bcopy(sc->hw.mac_addr, sc->sc_ac.ac_enaddr, ETHER_ADDR_LEN);
529 
530 	/* Setup OS specific network interface */
531 	if (!defer)
532 		em_setup_interface(sc);
533 
534 	/* Initialize statistics */
535 	em_clear_hw_cntrs(&sc->hw);
536 #ifndef SMALL_KERNEL
537 	em_update_stats_counters(sc);
538 #endif
539 	sc->hw.get_link_status = 1;
540 	if (!defer)
541 		em_update_link_status(sc);
542 
543 	printf(", address %s\n", ether_sprintf(sc->sc_ac.ac_enaddr));
544 
545 	/* Indicate SOL/IDER usage */
546 	if (em_check_phy_reset_block(&sc->hw))
547 		printf("%s: PHY reset is blocked due to SOL/IDER session.\n",
548 		    DEVNAME(sc));
549 
550 	/* Identify 82544 on PCI-X */
551 	em_get_bus_info(&sc->hw);
552 	if (sc->hw.bus_type == em_bus_type_pcix &&
553 	    sc->hw.mac_type == em_82544)
554 		sc->pcix_82544 = TRUE;
555         else
556 		sc->pcix_82544 = FALSE;
557 
558 	sc->hw.icp_xxxx_is_link_up = FALSE;
559 
560 	INIT_DEBUGOUT("em_attach: end");
561 	return;
562 
563 err_mac_addr:
564 err_hw_init:
565 	sc->sc_rx_desc_ring = NULL;
566 	em_dma_free(sc, &sc->sc_rx_dma);
567 err_rx_desc:
568 	sc->sc_tx_desc_ring = NULL;
569 	em_dma_free(sc, &sc->sc_tx_dma);
570 err_tx_desc:
571 err_pci:
572 	em_free_pci_resources(sc);
573 }
574 
575 /*********************************************************************
576  *  Transmit entry point
577  *
578  *  em_start is called by the stack to initiate a transmit.
579  *  The driver will remain in this routine as long as there are
580  *  packets to transmit and transmit resources are available.
581  *  In case resources are not available stack is notified and
582  *  the packet is requeued.
583  **********************************************************************/
584 
585 void
586 em_start(struct ifnet *ifp)
587 {
588 	struct em_softc *sc = ifp->if_softc;
589 	u_int head, free, used;
590 	struct mbuf *m;
591 	int post = 0;
592 
593 	if (!sc->link_active) {
594 		IFQ_PURGE(&ifp->if_snd);
595 		return;
596 	}
597 
598 	/* calculate free space */
599 	head = sc->sc_tx_desc_head;
600 	free = sc->sc_tx_desc_tail;
601 	if (free <= head)
602 		free += sc->sc_tx_slots;
603 	free -= head;
604 
605 	if (sc->hw.mac_type != em_82547) {
606 		bus_dmamap_sync(sc->sc_dmat, sc->sc_tx_dma.dma_map,
607 		    0, sc->sc_tx_dma.dma_map->dm_mapsize,
608 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
609 	}
610 
611 	for (;;) {
612 		/* use 2 because cksum setup can use an extra slot */
613 		if (EM_MAX_SCATTER + 2 > free) {
614 			ifq_set_oactive(&ifp->if_snd);
615 			break;
616 		}
617 
618 		m = ifq_dequeue(&ifp->if_snd);
619 		if (m == NULL)
620 			break;
621 
622 		used = em_encap(sc, m);
623 		if (used == 0) {
624 			m_freem(m);
625 			continue;
626 		}
627 
628 		KASSERT(used <= free);
629 
630 		free -= used;
631 
632 #if NBPFILTER > 0
633 		/* Send a copy of the frame to the BPF listener */
634 		if (ifp->if_bpf)
635 			bpf_mtap_ether(ifp->if_bpf, m, BPF_DIRECTION_OUT);
636 #endif
637 
638 		/* Set timeout in case hardware has problems transmitting */
639 		ifp->if_timer = EM_TX_TIMEOUT;
640 
641 		if (sc->hw.mac_type == em_82547) {
642 			int len = m->m_pkthdr.len;
643 
644 			if (sc->link_duplex == HALF_DUPLEX)
645 				em_82547_move_tail_locked(sc);
646 			else {
647 				E1000_WRITE_REG(&sc->hw, TDT,
648 				    sc->sc_tx_desc_head);
649 				em_82547_update_fifo_head(sc, len);
650 			}
651 		}
652 
653 		post = 1;
654 	}
655 
656 	if (sc->hw.mac_type != em_82547) {
657 		bus_dmamap_sync(sc->sc_dmat, sc->sc_tx_dma.dma_map,
658 		    0, sc->sc_tx_dma.dma_map->dm_mapsize,
659 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
660 		/*
661 		 * Advance the Transmit Descriptor Tail (Tdt),
662 		 * this tells the E1000 that this frame is
663 		 * available to transmit.
664 		 */
665 		if (post)
666 			E1000_WRITE_REG(&sc->hw, TDT, sc->sc_tx_desc_head);
667 	}
668 }
669 
670 /*********************************************************************
671  *  Ioctl entry point
672  *
673  *  em_ioctl is called when the user wants to configure the
674  *  interface.
675  *
676  *  return 0 on success, positive on failure
677  **********************************************************************/
678 
679 int
680 em_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
681 {
682 	int		error = 0;
683 	struct ifreq   *ifr = (struct ifreq *) data;
684 	struct em_softc *sc = ifp->if_softc;
685 	int s;
686 
687 	s = splnet();
688 
689 	switch (command) {
690 	case SIOCSIFADDR:
691 		IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFADDR (Set Interface "
692 			       "Addr)");
693 		if (!(ifp->if_flags & IFF_UP)) {
694 			ifp->if_flags |= IFF_UP;
695 			em_init(sc);
696 		}
697 		break;
698 
699 	case SIOCSIFFLAGS:
700 		IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFFLAGS (Set Interface Flags)");
701 		if (ifp->if_flags & IFF_UP) {
702 			if (ifp->if_flags & IFF_RUNNING)
703 				error = ENETRESET;
704 			else
705 				em_init(sc);
706 		} else {
707 			if (ifp->if_flags & IFF_RUNNING)
708 				em_stop(sc, 0);
709 		}
710 		break;
711 
712 	case SIOCSIFMEDIA:
713 		/* Check SOL/IDER usage */
714 		if (em_check_phy_reset_block(&sc->hw)) {
715 			printf("%s: Media change is blocked due to SOL/IDER session.\n",
716 			    DEVNAME(sc));
717 			break;
718 		}
719 	case SIOCGIFMEDIA:
720 		IOCTL_DEBUGOUT("ioctl rcv'd: SIOCxIFMEDIA (Get/Set Interface Media)");
721 		error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
722 		break;
723 
724 	case SIOCGIFRXR:
725 		error = if_rxr_ioctl((struct if_rxrinfo *)ifr->ifr_data,
726 		    NULL, EM_MCLBYTES, &sc->sc_rx_ring);
727 		break;
728 
729 	default:
730 		error = ether_ioctl(ifp, &sc->sc_ac, command, data);
731 	}
732 
733 	if (error == ENETRESET) {
734 		if (ifp->if_flags & IFF_RUNNING) {
735 			em_disable_intr(sc);
736 			em_iff(sc);
737 			if (sc->hw.mac_type == em_82542_rev2_0)
738 				em_initialize_receive_unit(sc);
739 			em_enable_intr(sc);
740 		}
741 		error = 0;
742 	}
743 
744 	splx(s);
745 	return (error);
746 }
747 
748 /*********************************************************************
749  *  Watchdog entry point
750  *
751  *  This routine is called whenever hardware quits transmitting.
752  *
753  **********************************************************************/
754 
755 void
756 em_watchdog(struct ifnet *ifp)
757 {
758 	struct em_softc *sc = ifp->if_softc;
759 
760 	/* If we are in this routine because of pause frames, then
761 	 * don't reset the hardware.
762 	 */
763 	if (E1000_READ_REG(&sc->hw, STATUS) & E1000_STATUS_TXOFF) {
764 		ifp->if_timer = EM_TX_TIMEOUT;
765 		return;
766 	}
767 	printf("%s: watchdog: head %u tail %u TDH %u TDT %u\n",
768 	    DEVNAME(sc),
769 	    sc->sc_tx_desc_head, sc->sc_tx_desc_tail,
770 	    E1000_READ_REG(&sc->hw, TDH), E1000_READ_REG(&sc->hw, TDT));
771 
772 	em_init(sc);
773 
774 	sc->watchdog_events++;
775 }
776 
777 /*********************************************************************
778  *  Init entry point
779  *
780  *  This routine is used in two ways. It is used by the stack as
781  *  init entry point in network interface structure. It is also used
782  *  by the driver as a hw/sw initialization routine to get to a
783  *  consistent state.
784  *
785  **********************************************************************/
786 
787 void
788 em_init(void *arg)
789 {
790 	struct em_softc *sc = arg;
791 	struct ifnet   *ifp = &sc->sc_ac.ac_if;
792 	uint32_t	pba;
793 	int s;
794 
795 	s = splnet();
796 
797 	INIT_DEBUGOUT("em_init: begin");
798 
799 	em_stop(sc, 0);
800 
801 	/*
802 	 * Packet Buffer Allocation (PBA)
803 	 * Writing PBA sets the receive portion of the buffer
804 	 * the remainder is used for the transmit buffer.
805 	 *
806 	 * Devices before the 82547 had a Packet Buffer of 64K.
807 	 *   Default allocation: PBA=48K for Rx, leaving 16K for Tx.
808 	 * After the 82547 the buffer was reduced to 40K.
809 	 *   Default allocation: PBA=30K for Rx, leaving 10K for Tx.
810 	 *   Note: default does not leave enough room for Jumbo Frame >10k.
811 	 */
812 	switch (sc->hw.mac_type) {
813 	case em_82547:
814 	case em_82547_rev_2: /* 82547: Total Packet Buffer is 40K */
815 		if (sc->hw.max_frame_size > EM_RXBUFFER_8192)
816 			pba = E1000_PBA_22K; /* 22K for Rx, 18K for Tx */
817 		else
818 			pba = E1000_PBA_30K; /* 30K for Rx, 10K for Tx */
819 		sc->tx_fifo_head = 0;
820 		sc->tx_head_addr = pba << EM_TX_HEAD_ADDR_SHIFT;
821 		sc->tx_fifo_size = (E1000_PBA_40K - pba) << EM_PBA_BYTES_SHIFT;
822 		break;
823 	case em_82571:
824 	case em_82572: /* Total Packet Buffer on these is 48k */
825 	case em_82575:
826 	case em_82580:
827 	case em_80003es2lan:
828 	case em_i350:
829 		pba = E1000_PBA_32K; /* 32K for Rx, 16K for Tx */
830 		break;
831 	case em_i210:
832 		pba = E1000_PBA_34K;
833 		break;
834 	case em_82573: /* 82573: Total Packet Buffer is 32K */
835 		/* Jumbo frames not supported */
836 		pba = E1000_PBA_12K; /* 12K for Rx, 20K for Tx */
837 		break;
838 	case em_82574: /* Total Packet Buffer is 40k */
839 		pba = E1000_PBA_20K; /* 20K for Rx, 20K for Tx */
840 		break;
841 	case em_ich8lan:
842 		pba = E1000_PBA_8K;
843 		break;
844 	case em_ich9lan:
845 	case em_ich10lan:
846 		/* Boost Receive side for jumbo frames */
847 		if (sc->hw.max_frame_size > EM_RXBUFFER_4096)
848 			pba = E1000_PBA_14K;
849 		else
850 			pba = E1000_PBA_10K;
851 		break;
852 	case em_pchlan:
853 	case em_pch2lan:
854 	case em_pch_lpt:
855 	case em_pch_spt:
856 		pba = E1000_PBA_26K;
857 		break;
858 	default:
859 		/* Devices before 82547 had a Packet Buffer of 64K.   */
860 		if (sc->hw.max_frame_size > EM_RXBUFFER_8192)
861 			pba = E1000_PBA_40K; /* 40K for Rx, 24K for Tx */
862 		else
863 			pba = E1000_PBA_48K; /* 48K for Rx, 16K for Tx */
864 	}
865 	INIT_DEBUGOUT1("em_init: pba=%dK",pba);
866 	E1000_WRITE_REG(&sc->hw, PBA, pba);
867 
868 	/* Get the latest mac address, User can use a LAA */
869 	bcopy(sc->sc_ac.ac_enaddr, sc->hw.mac_addr, ETHER_ADDR_LEN);
870 
871 	/* Initialize the hardware */
872 	if (em_hardware_init(sc)) {
873 		printf("%s: Unable to initialize the hardware\n",
874 		       DEVNAME(sc));
875 		splx(s);
876 		return;
877 	}
878 	em_update_link_status(sc);
879 
880 	E1000_WRITE_REG(&sc->hw, VET, ETHERTYPE_VLAN);
881 	if (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING)
882 		em_enable_hw_vlans(sc);
883 
884 	/* Prepare transmit descriptors and buffers */
885 	if (em_setup_transmit_structures(sc)) {
886 		printf("%s: Could not setup transmit structures\n",
887 		       DEVNAME(sc));
888 		em_stop(sc, 0);
889 		splx(s);
890 		return;
891 	}
892 	em_initialize_transmit_unit(sc);
893 
894 	/* Prepare receive descriptors and buffers */
895 	if (em_setup_receive_structures(sc)) {
896 		printf("%s: Could not setup receive structures\n",
897 		       DEVNAME(sc));
898 		em_stop(sc, 0);
899 		splx(s);
900 		return;
901 	}
902 	em_initialize_receive_unit(sc);
903 
904 	/* Program promiscuous mode and multicast filters. */
905 	em_iff(sc);
906 
907 	ifp->if_flags |= IFF_RUNNING;
908 	ifq_clr_oactive(&ifp->if_snd);
909 
910 	timeout_add_sec(&sc->timer_handle, 1);
911 	em_clear_hw_cntrs(&sc->hw);
912 	em_enable_intr(sc);
913 
914 	/* Don't reset the phy next time init gets called */
915 	sc->hw.phy_reset_disable = TRUE;
916 
917 	splx(s);
918 }
919 
920 /*********************************************************************
921  *
922  *  Interrupt Service routine
923  *
924  **********************************************************************/
925 int
926 em_intr(void *arg)
927 {
928 	struct em_softc	*sc = arg;
929 	struct ifnet	*ifp = &sc->sc_ac.ac_if;
930 	u_int32_t	reg_icr, test_icr;
931 
932 	test_icr = reg_icr = E1000_READ_REG(&sc->hw, ICR);
933 	if (sc->hw.mac_type >= em_82571)
934 		test_icr = (reg_icr & E1000_ICR_INT_ASSERTED);
935 	if (!test_icr)
936 		return (0);
937 
938 	if (ifp->if_flags & IFF_RUNNING) {
939 		em_txeof(sc);
940 
941 		if (em_rxeof(sc) || ISSET(reg_icr, E1000_ICR_RXO)) {
942 			if (em_rxfill(sc)) {
943 				E1000_WRITE_REG(&sc->hw, RDT,
944 				    sc->sc_rx_desc_head);
945 			}
946 		}
947 	}
948 
949 	/* Link status change */
950 	if (reg_icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
951 		KERNEL_LOCK();
952 		sc->hw.get_link_status = 1;
953 		em_check_for_link(&sc->hw);
954 		em_update_link_status(sc);
955 		KERNEL_UNLOCK();
956 	}
957 
958 	return (1);
959 }
960 
961 /*********************************************************************
962  *
963  *  Media Ioctl callback
964  *
965  *  This routine is called whenever the user queries the status of
966  *  the interface using ifconfig.
967  *
968  **********************************************************************/
969 void
970 em_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
971 {
972 	struct em_softc *sc = ifp->if_softc;
973 	uint64_t fiber_type = IFM_1000_SX;
974 	u_int16_t gsr;
975 
976 	INIT_DEBUGOUT("em_media_status: begin");
977 
978 	em_check_for_link(&sc->hw);
979 	em_update_link_status(sc);
980 
981 	ifmr->ifm_status = IFM_AVALID;
982 	ifmr->ifm_active = IFM_ETHER;
983 
984 	if (!sc->link_active) {
985 		ifmr->ifm_active |= IFM_NONE;
986 		return;
987 	}
988 
989 	ifmr->ifm_status |= IFM_ACTIVE;
990 
991 	if (sc->hw.media_type == em_media_type_fiber ||
992 	    sc->hw.media_type == em_media_type_internal_serdes) {
993 		if (sc->hw.mac_type == em_82545)
994 			fiber_type = IFM_1000_LX;
995 		ifmr->ifm_active |= fiber_type | IFM_FDX;
996 	} else {
997 		switch (sc->link_speed) {
998 		case 10:
999 			ifmr->ifm_active |= IFM_10_T;
1000 			break;
1001 		case 100:
1002 			ifmr->ifm_active |= IFM_100_TX;
1003 			break;
1004 		case 1000:
1005 			ifmr->ifm_active |= IFM_1000_T;
1006 			break;
1007 		}
1008 
1009 		if (sc->link_duplex == FULL_DUPLEX)
1010 			ifmr->ifm_active |= em_flowstatus(sc) | IFM_FDX;
1011 		else
1012 			ifmr->ifm_active |= IFM_HDX;
1013 
1014 		if (IFM_SUBTYPE(ifmr->ifm_active) == IFM_1000_T) {
1015 			em_read_phy_reg(&sc->hw, PHY_1000T_STATUS, &gsr);
1016 			if (gsr & SR_1000T_MS_CONFIG_RES)
1017 				ifmr->ifm_active |= IFM_ETH_MASTER;
1018 		}
1019 	}
1020 }
1021 
1022 /*********************************************************************
1023  *
1024  *  Media Ioctl callback
1025  *
1026  *  This routine is called when the user changes speed/duplex using
1027  *  media/mediopt option with ifconfig.
1028  *
1029  **********************************************************************/
1030 int
1031 em_media_change(struct ifnet *ifp)
1032 {
1033 	struct em_softc *sc = ifp->if_softc;
1034 	struct ifmedia	*ifm = &sc->media;
1035 
1036 	INIT_DEBUGOUT("em_media_change: begin");
1037 
1038 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1039 		return (EINVAL);
1040 
1041 	switch (IFM_SUBTYPE(ifm->ifm_media)) {
1042 	case IFM_AUTO:
1043 		sc->hw.autoneg = DO_AUTO_NEG;
1044 		sc->hw.autoneg_advertised = AUTONEG_ADV_DEFAULT;
1045 		break;
1046 	case IFM_1000_LX:
1047 	case IFM_1000_SX:
1048 	case IFM_1000_T:
1049 		sc->hw.autoneg = DO_AUTO_NEG;
1050 		sc->hw.autoneg_advertised = ADVERTISE_1000_FULL;
1051 		break;
1052 	case IFM_100_TX:
1053 		sc->hw.autoneg = FALSE;
1054 		sc->hw.autoneg_advertised = 0;
1055 		if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
1056 			sc->hw.forced_speed_duplex = em_100_full;
1057 		else
1058 			sc->hw.forced_speed_duplex = em_100_half;
1059 		break;
1060 	case IFM_10_T:
1061 		sc->hw.autoneg = FALSE;
1062 		sc->hw.autoneg_advertised = 0;
1063 		if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
1064 			sc->hw.forced_speed_duplex = em_10_full;
1065 		else
1066 			sc->hw.forced_speed_duplex = em_10_half;
1067 		break;
1068 	default:
1069 		printf("%s: Unsupported media type\n", DEVNAME(sc));
1070 	}
1071 
1072 	/*
1073 	 * As the speed/duplex settings may have changed we need to
1074 	 * reset the PHY.
1075 	 */
1076 	sc->hw.phy_reset_disable = FALSE;
1077 
1078 	em_init(sc);
1079 
1080 	return (0);
1081 }
1082 
1083 uint64_t
1084 em_flowstatus(struct em_softc *sc)
1085 {
1086 	u_int16_t ar, lpar;
1087 
1088 	if (sc->hw.media_type == em_media_type_fiber ||
1089 	    sc->hw.media_type == em_media_type_internal_serdes)
1090 		return (0);
1091 
1092 	em_read_phy_reg(&sc->hw, PHY_AUTONEG_ADV, &ar);
1093 	em_read_phy_reg(&sc->hw, PHY_LP_ABILITY, &lpar);
1094 
1095 	if ((ar & NWAY_AR_PAUSE) && (lpar & NWAY_LPAR_PAUSE))
1096 		return (IFM_FLOW|IFM_ETH_TXPAUSE|IFM_ETH_RXPAUSE);
1097 	else if (!(ar & NWAY_AR_PAUSE) && (ar & NWAY_AR_ASM_DIR) &&
1098 		(lpar & NWAY_LPAR_PAUSE) && (lpar & NWAY_LPAR_ASM_DIR))
1099 		return (IFM_FLOW|IFM_ETH_TXPAUSE);
1100 	else if ((ar & NWAY_AR_PAUSE) && (ar & NWAY_AR_ASM_DIR) &&
1101 		!(lpar & NWAY_LPAR_PAUSE) && (lpar & NWAY_LPAR_ASM_DIR))
1102 		return (IFM_FLOW|IFM_ETH_RXPAUSE);
1103 
1104 	return (0);
1105 }
1106 
1107 /*********************************************************************
1108  *
1109  *  This routine maps the mbufs to tx descriptors.
1110  *
1111  *  return 0 on success, positive on failure
1112  **********************************************************************/
1113 u_int
1114 em_encap(struct em_softc *sc, struct mbuf *m)
1115 {
1116 	struct em_packet *pkt;
1117 	struct em_tx_desc *desc;
1118 	bus_dmamap_t map;
1119 	u_int32_t txd_upper, txd_lower;
1120 	u_int head, last, used = 0;
1121 	int i, j;
1122 
1123 	/* For 82544 Workaround */
1124 	DESC_ARRAY		desc_array;
1125 	u_int32_t		array_elements;
1126 
1127 	/* get a dmamap for this packet from the next free slot */
1128 	head = sc->sc_tx_desc_head;
1129 	pkt = &sc->sc_tx_pkts_ring[head];
1130 	map = pkt->pkt_map;
1131 
1132 	switch (bus_dmamap_load_mbuf(sc->sc_dmat, map, m, BUS_DMA_NOWAIT)) {
1133 	case 0:
1134 		break;
1135 	case EFBIG:
1136 		if (m_defrag(m, M_DONTWAIT) == 0 &&
1137 		    bus_dmamap_load_mbuf(sc->sc_dmat, map, m,
1138 		     BUS_DMA_NOWAIT) == 0)
1139 			break;
1140 
1141 		/* FALLTHROUGH */
1142 	default:
1143 		sc->no_tx_dma_setup++;
1144 		return (0);
1145 	}
1146 
1147 	bus_dmamap_sync(sc->sc_dmat, map,
1148 	    0, map->dm_mapsize,
1149 	    BUS_DMASYNC_PREWRITE);
1150 
1151 	if (sc->hw.mac_type == em_82547) {
1152 		bus_dmamap_sync(sc->sc_dmat, sc->sc_tx_dma.dma_map,
1153 		    0, sc->sc_tx_dma.dma_map->dm_mapsize,
1154 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1155 	}
1156 
1157 	if (sc->hw.mac_type >= em_82543 && sc->hw.mac_type != em_82575 &&
1158 	    sc->hw.mac_type != em_82580 && sc->hw.mac_type != em_i210 &&
1159 	    sc->hw.mac_type != em_i350) {
1160 		used += em_transmit_checksum_setup(sc, m, head,
1161 		    &txd_upper, &txd_lower);
1162 	} else {
1163 		txd_upper = txd_lower = 0;
1164 	}
1165 
1166 	head += used;
1167 	if (head >= sc->sc_tx_slots)
1168 		head -= sc->sc_tx_slots;
1169 
1170 	for (i = 0; i < map->dm_nsegs; i++) {
1171 		/* If sc is 82544 and on PCI-X bus */
1172 		if (sc->pcix_82544) {
1173 			/*
1174 			 * Check the Address and Length combination and
1175 			 * split the data accordingly
1176 			 */
1177 			array_elements = em_fill_descriptors(
1178 			    map->dm_segs[i].ds_addr, map->dm_segs[i].ds_len,
1179 			    &desc_array);
1180 			for (j = 0; j < array_elements; j++) {
1181 				desc = &sc->sc_tx_desc_ring[head];
1182 
1183 				desc->buffer_addr = htole64(
1184 					desc_array.descriptor[j].address);
1185 				desc->lower.data = htole32(
1186 					(sc->sc_txd_cmd | txd_lower |
1187 					 (u_int16_t)desc_array.descriptor[j].length));
1188 				desc->upper.data = htole32(txd_upper);
1189 
1190 				last = head;
1191 				if (++head == sc->sc_tx_slots)
1192 					head = 0;
1193 
1194 				used++;
1195 			}
1196 		} else {
1197 			desc = &sc->sc_tx_desc_ring[head];
1198 
1199 			desc->buffer_addr = htole64(map->dm_segs[i].ds_addr);
1200 			desc->lower.data = htole32(sc->sc_txd_cmd |
1201 			    txd_lower | map->dm_segs[i].ds_len);
1202 			desc->upper.data = htole32(txd_upper);
1203 
1204 			last = head;
1205 			if (++head == sc->sc_tx_slots)
1206 	        		head = 0;
1207 
1208 			used++;
1209 		}
1210 	}
1211 
1212 #if NVLAN > 0
1213 	/* Find out if we are in VLAN mode */
1214 	if (m->m_flags & M_VLANTAG) {
1215 		/* Set the VLAN id */
1216 		desc->upper.fields.special = htole16(m->m_pkthdr.ether_vtag);
1217 
1218 		/* Tell hardware to add tag */
1219 		desc->lower.data |= htole32(E1000_TXD_CMD_VLE);
1220 	}
1221 #endif
1222 
1223 	/* mark the packet with the mbuf and last desc slot */
1224 	pkt->pkt_m = m;
1225 	pkt->pkt_eop = last;
1226 
1227 	sc->sc_tx_desc_head = head;
1228 
1229 	/*
1230 	 * Last Descriptor of Packet
1231 	 * needs End Of Packet (EOP)
1232 	 * and Report Status (RS)
1233 	 */
1234 	desc->lower.data |= htole32(E1000_TXD_CMD_EOP | E1000_TXD_CMD_RS);
1235 
1236 	if (sc->hw.mac_type == em_82547) {
1237 		bus_dmamap_sync(sc->sc_dmat, sc->sc_tx_dma.dma_map,
1238 		    0, sc->sc_tx_dma.dma_map->dm_mapsize,
1239 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1240 	}
1241 
1242 	return (used);
1243 }
1244 
1245 /*********************************************************************
1246  *
1247  * 82547 workaround to avoid controller hang in half-duplex environment.
1248  * The workaround is to avoid queuing a large packet that would span
1249  * the internal Tx FIFO ring boundary. We need to reset the FIFO pointers
1250  * in this case. We do that only when FIFO is quiescent.
1251  *
1252  **********************************************************************/
1253 void
1254 em_82547_move_tail_locked(struct em_softc *sc)
1255 {
1256 	uint16_t hw_tdt;
1257 	uint16_t sw_tdt;
1258 	struct em_tx_desc *tx_desc;
1259 	uint16_t length = 0;
1260 	boolean_t eop = 0;
1261 
1262 	hw_tdt = E1000_READ_REG(&sc->hw, TDT);
1263 	sw_tdt = sc->sc_tx_desc_head;
1264 
1265 	while (hw_tdt != sw_tdt) {
1266 		tx_desc = &sc->sc_tx_desc_ring[hw_tdt];
1267 		length += tx_desc->lower.flags.length;
1268 		eop = tx_desc->lower.data & E1000_TXD_CMD_EOP;
1269 		if (++hw_tdt == sc->sc_tx_slots)
1270 			hw_tdt = 0;
1271 
1272 		if (eop) {
1273 			if (em_82547_fifo_workaround(sc, length)) {
1274 				sc->tx_fifo_wrk_cnt++;
1275 				timeout_add(&sc->tx_fifo_timer_handle, 1);
1276 				break;
1277 			}
1278 			E1000_WRITE_REG(&sc->hw, TDT, hw_tdt);
1279 			em_82547_update_fifo_head(sc, length);
1280 			length = 0;
1281 		}
1282 	}
1283 }
1284 
1285 void
1286 em_82547_move_tail(void *arg)
1287 {
1288 	struct em_softc *sc = arg;
1289 	int s;
1290 
1291 	s = splnet();
1292 	em_82547_move_tail_locked(sc);
1293 	splx(s);
1294 }
1295 
1296 int
1297 em_82547_fifo_workaround(struct em_softc *sc, int len)
1298 {
1299 	int fifo_space, fifo_pkt_len;
1300 
1301 	fifo_pkt_len = EM_ROUNDUP(len + EM_FIFO_HDR, EM_FIFO_HDR);
1302 
1303 	if (sc->link_duplex == HALF_DUPLEX) {
1304 		fifo_space = sc->tx_fifo_size - sc->tx_fifo_head;
1305 
1306 		if (fifo_pkt_len >= (EM_82547_PKT_THRESH + fifo_space)) {
1307 			if (em_82547_tx_fifo_reset(sc))
1308 				return (0);
1309 			else
1310 				return (1);
1311 		}
1312 	}
1313 
1314 	return (0);
1315 }
1316 
1317 void
1318 em_82547_update_fifo_head(struct em_softc *sc, int len)
1319 {
1320 	int fifo_pkt_len = EM_ROUNDUP(len + EM_FIFO_HDR, EM_FIFO_HDR);
1321 
1322 	/* tx_fifo_head is always 16 byte aligned */
1323 	sc->tx_fifo_head += fifo_pkt_len;
1324 	if (sc->tx_fifo_head >= sc->tx_fifo_size)
1325 		sc->tx_fifo_head -= sc->tx_fifo_size;
1326 }
1327 
1328 int
1329 em_82547_tx_fifo_reset(struct em_softc *sc)
1330 {
1331 	uint32_t tctl;
1332 
1333 	if ((E1000_READ_REG(&sc->hw, TDT) ==
1334 	     E1000_READ_REG(&sc->hw, TDH)) &&
1335 	    (E1000_READ_REG(&sc->hw, TDFT) ==
1336 	     E1000_READ_REG(&sc->hw, TDFH)) &&
1337 	    (E1000_READ_REG(&sc->hw, TDFTS) ==
1338 	     E1000_READ_REG(&sc->hw, TDFHS)) &&
1339 	    (E1000_READ_REG(&sc->hw, TDFPC) == 0)) {
1340 
1341 		/* Disable TX unit */
1342 		tctl = E1000_READ_REG(&sc->hw, TCTL);
1343 		E1000_WRITE_REG(&sc->hw, TCTL, tctl & ~E1000_TCTL_EN);
1344 
1345 		/* Reset FIFO pointers */
1346 		E1000_WRITE_REG(&sc->hw, TDFT, sc->tx_head_addr);
1347 		E1000_WRITE_REG(&sc->hw, TDFH, sc->tx_head_addr);
1348 		E1000_WRITE_REG(&sc->hw, TDFTS, sc->tx_head_addr);
1349 		E1000_WRITE_REG(&sc->hw, TDFHS, sc->tx_head_addr);
1350 
1351 		/* Re-enable TX unit */
1352 		E1000_WRITE_REG(&sc->hw, TCTL, tctl);
1353 		E1000_WRITE_FLUSH(&sc->hw);
1354 
1355 		sc->tx_fifo_head = 0;
1356 		sc->tx_fifo_reset_cnt++;
1357 
1358 		return (TRUE);
1359 	} else
1360 		return (FALSE);
1361 }
1362 
1363 void
1364 em_iff(struct em_softc *sc)
1365 {
1366 	struct ifnet *ifp = &sc->sc_ac.ac_if;
1367 	struct arpcom *ac = &sc->sc_ac;
1368 	u_int32_t reg_rctl = 0;
1369 	u_int8_t  mta[MAX_NUM_MULTICAST_ADDRESSES * ETH_LENGTH_OF_ADDRESS];
1370 	struct ether_multi *enm;
1371 	struct ether_multistep step;
1372 	int i = 0;
1373 
1374 	IOCTL_DEBUGOUT("em_iff: begin");
1375 
1376 	if (sc->hw.mac_type == em_82542_rev2_0) {
1377 		reg_rctl = E1000_READ_REG(&sc->hw, RCTL);
1378 		if (sc->hw.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
1379 			em_pci_clear_mwi(&sc->hw);
1380 		reg_rctl |= E1000_RCTL_RST;
1381 		E1000_WRITE_REG(&sc->hw, RCTL, reg_rctl);
1382 		msec_delay(5);
1383 	}
1384 
1385 	reg_rctl = E1000_READ_REG(&sc->hw, RCTL);
1386 	reg_rctl &= ~(E1000_RCTL_MPE | E1000_RCTL_UPE);
1387 	ifp->if_flags &= ~IFF_ALLMULTI;
1388 
1389 	if (ifp->if_flags & IFF_PROMISC || ac->ac_multirangecnt > 0 ||
1390 	    ac->ac_multicnt > MAX_NUM_MULTICAST_ADDRESSES) {
1391 		ifp->if_flags |= IFF_ALLMULTI;
1392 		reg_rctl |= E1000_RCTL_MPE;
1393 		if (ifp->if_flags & IFF_PROMISC)
1394 			reg_rctl |= E1000_RCTL_UPE;
1395 	} else {
1396 		ETHER_FIRST_MULTI(step, ac, enm);
1397 		while (enm != NULL) {
1398 			bcopy(enm->enm_addrlo, mta + i, ETH_LENGTH_OF_ADDRESS);
1399 			i += ETH_LENGTH_OF_ADDRESS;
1400 
1401 			ETHER_NEXT_MULTI(step, enm);
1402 		}
1403 
1404 		em_mc_addr_list_update(&sc->hw, mta, ac->ac_multicnt, 0, 1);
1405 	}
1406 
1407 	E1000_WRITE_REG(&sc->hw, RCTL, reg_rctl);
1408 
1409 	if (sc->hw.mac_type == em_82542_rev2_0) {
1410 		reg_rctl = E1000_READ_REG(&sc->hw, RCTL);
1411 		reg_rctl &= ~E1000_RCTL_RST;
1412 		E1000_WRITE_REG(&sc->hw, RCTL, reg_rctl);
1413 		msec_delay(5);
1414 		if (sc->hw.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
1415 			em_pci_set_mwi(&sc->hw);
1416 	}
1417 }
1418 
1419 /*********************************************************************
1420  *  Timer routine
1421  *
1422  *  This routine checks for link status and updates statistics.
1423  *
1424  **********************************************************************/
1425 
1426 void
1427 em_local_timer(void *arg)
1428 {
1429 	struct ifnet   *ifp;
1430 	struct em_softc *sc = arg;
1431 	int s;
1432 
1433 	ifp = &sc->sc_ac.ac_if;
1434 
1435 	s = splnet();
1436 
1437 #ifndef SMALL_KERNEL
1438 	em_update_stats_counters(sc);
1439 #ifdef EM_DEBUG
1440 	if (ifp->if_flags & IFF_DEBUG && ifp->if_flags & IFF_RUNNING)
1441 		em_print_hw_stats(sc);
1442 #endif
1443 #endif
1444 	em_smartspeed(sc);
1445 
1446 	timeout_add_sec(&sc->timer_handle, 1);
1447 
1448 	splx(s);
1449 }
1450 
1451 void
1452 em_update_link_status(struct em_softc *sc)
1453 {
1454 	struct ifnet *ifp = &sc->sc_ac.ac_if;
1455 
1456 	if (E1000_READ_REG(&sc->hw, STATUS) & E1000_STATUS_LU) {
1457 		if (sc->link_active == 0) {
1458 			em_get_speed_and_duplex(&sc->hw,
1459 						&sc->link_speed,
1460 						&sc->link_duplex);
1461 			/* Check if we may set SPEED_MODE bit on PCI-E */
1462 			if ((sc->link_speed == SPEED_1000) &&
1463 			    ((sc->hw.mac_type == em_82571) ||
1464 			    (sc->hw.mac_type == em_82572) ||
1465 			    (sc->hw.mac_type == em_82575) ||
1466 			    (sc->hw.mac_type == em_82580))) {
1467 				int tarc0;
1468 
1469 				tarc0 = E1000_READ_REG(&sc->hw, TARC0);
1470 				tarc0 |= SPEED_MODE_BIT;
1471 				E1000_WRITE_REG(&sc->hw, TARC0, tarc0);
1472 			}
1473 			sc->link_active = 1;
1474 			sc->smartspeed = 0;
1475 			ifp->if_baudrate = IF_Mbps(sc->link_speed);
1476 		}
1477 		if (!LINK_STATE_IS_UP(ifp->if_link_state)) {
1478 			if (sc->link_duplex == FULL_DUPLEX)
1479 				ifp->if_link_state = LINK_STATE_FULL_DUPLEX;
1480 			else
1481 				ifp->if_link_state = LINK_STATE_HALF_DUPLEX;
1482 			if_link_state_change(ifp);
1483 		}
1484 	} else {
1485 		if (sc->link_active == 1) {
1486 			ifp->if_baudrate = sc->link_speed = 0;
1487 			sc->link_duplex = 0;
1488 			sc->link_active = 0;
1489 		}
1490 		if (ifp->if_link_state != LINK_STATE_DOWN) {
1491 			ifp->if_link_state = LINK_STATE_DOWN;
1492 			if_link_state_change(ifp);
1493 		}
1494 	}
1495 }
1496 
1497 /*********************************************************************
1498  *
1499  *  This routine disables all traffic on the adapter by issuing a
1500  *  global reset on the MAC and deallocates TX/RX buffers.
1501  *
1502  **********************************************************************/
1503 
1504 void
1505 em_stop(void *arg, int softonly)
1506 {
1507 	struct em_softc *sc = arg;
1508 	struct ifnet   *ifp = &sc->sc_ac.ac_if;
1509 
1510 	/* Tell the stack that the interface is no longer active */
1511 	ifp->if_flags &= ~IFF_RUNNING;
1512 
1513 	INIT_DEBUGOUT("em_stop: begin");
1514 
1515 	timeout_del(&sc->timer_handle);
1516 	timeout_del(&sc->tx_fifo_timer_handle);
1517 
1518 	if (!softonly)
1519 		em_disable_intr(sc);
1520 	if (sc->hw.mac_type == em_pch_spt)
1521 		em_flush_desc_rings(sc);
1522 	if (!softonly)
1523 		em_reset_hw(&sc->hw);
1524 
1525 	intr_barrier(sc->sc_intrhand);
1526 	ifq_barrier(&ifp->if_snd);
1527 
1528 	KASSERT((ifp->if_flags & IFF_RUNNING) == 0);
1529 
1530 	ifq_clr_oactive(&ifp->if_snd);
1531 	ifp->if_timer = 0;
1532 
1533 	em_free_transmit_structures(sc);
1534 	em_free_receive_structures(sc);
1535 }
1536 
1537 /*********************************************************************
1538  *
1539  *  Determine hardware revision.
1540  *
1541  **********************************************************************/
1542 void
1543 em_identify_hardware(struct em_softc *sc)
1544 {
1545 	u_int32_t reg;
1546 	struct pci_attach_args *pa = &sc->osdep.em_pa;
1547 
1548 	/* Make sure our PCI config space has the necessary stuff set */
1549 	sc->hw.pci_cmd_word = pci_conf_read(pa->pa_pc, pa->pa_tag,
1550 					    PCI_COMMAND_STATUS_REG);
1551 
1552 	/* Save off the information about this board */
1553 	sc->hw.vendor_id = PCI_VENDOR(pa->pa_id);
1554 	sc->hw.device_id = PCI_PRODUCT(pa->pa_id);
1555 
1556 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_CLASS_REG);
1557 	sc->hw.revision_id = PCI_REVISION(reg);
1558 
1559 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
1560 	sc->hw.subsystem_vendor_id = PCI_VENDOR(reg);
1561 	sc->hw.subsystem_id = PCI_PRODUCT(reg);
1562 
1563 	/* Identify the MAC */
1564 	if (em_set_mac_type(&sc->hw))
1565 		printf("%s: Unknown MAC Type\n", DEVNAME(sc));
1566 
1567 	if (sc->hw.mac_type == em_pchlan)
1568 		sc->hw.revision_id = PCI_PRODUCT(pa->pa_id) & 0x0f;
1569 
1570 	if (sc->hw.mac_type == em_82541 ||
1571 	    sc->hw.mac_type == em_82541_rev_2 ||
1572 	    sc->hw.mac_type == em_82547 ||
1573 	    sc->hw.mac_type == em_82547_rev_2)
1574 		sc->hw.phy_init_script = TRUE;
1575 }
1576 
1577 void
1578 em_legacy_irq_quirk_spt(struct em_softc *sc)
1579 {
1580 	uint32_t	reg;
1581 
1582 	/* Legacy interrupt: SPT needs a quirk. */
1583 	if (sc->hw.mac_type != em_pch_spt)
1584 		return;
1585 	if (sc->legacy_irq == 0)
1586 		return;
1587 
1588 	reg = EM_READ_REG(&sc->hw, E1000_FEXTNVM7);
1589 	reg |= E1000_FEXTNVM7_SIDE_CLK_UNGATE;
1590 	EM_WRITE_REG(&sc->hw, E1000_FEXTNVM7, reg);
1591 
1592 	reg = EM_READ_REG(&sc->hw, E1000_FEXTNVM9);
1593 	reg |= E1000_FEXTNVM9_IOSFSB_CLKGATE_DIS |
1594 	    E1000_FEXTNVM9_IOSFSB_CLKREQ_DIS;
1595 	EM_WRITE_REG(&sc->hw, E1000_FEXTNVM9, reg);
1596 }
1597 
1598 int
1599 em_allocate_pci_resources(struct em_softc *sc)
1600 {
1601 	int		val, rid;
1602 	pci_intr_handle_t	ih;
1603 	const char		*intrstr = NULL;
1604 	struct pci_attach_args *pa = &sc->osdep.em_pa;
1605 	pci_chipset_tag_t	pc = pa->pa_pc;
1606 
1607 	val = pci_conf_read(pa->pa_pc, pa->pa_tag, EM_MMBA);
1608 	if (PCI_MAPREG_TYPE(val) != PCI_MAPREG_TYPE_MEM) {
1609 		printf(": mmba is not mem space\n");
1610 		return (ENXIO);
1611 	}
1612 	if (pci_mapreg_map(pa, EM_MMBA, PCI_MAPREG_MEM_TYPE(val), 0,
1613 	    &sc->osdep.mem_bus_space_tag, &sc->osdep.mem_bus_space_handle,
1614 	    &sc->osdep.em_membase, &sc->osdep.em_memsize, 0)) {
1615 		printf(": cannot find mem space\n");
1616 		return (ENXIO);
1617 	}
1618 
1619 	switch (sc->hw.mac_type) {
1620 	case em_82544:
1621 	case em_82540:
1622 	case em_82545:
1623 	case em_82546:
1624 	case em_82541:
1625 	case em_82541_rev_2:
1626 		/* Figure out where our I/O BAR is ? */
1627 		for (rid = PCI_MAPREG_START; rid < PCI_MAPREG_END;) {
1628 			val = pci_conf_read(pa->pa_pc, pa->pa_tag, rid);
1629 			if (PCI_MAPREG_TYPE(val) == PCI_MAPREG_TYPE_IO) {
1630 				sc->io_rid = rid;
1631 				break;
1632 			}
1633 			rid += 4;
1634 			if (PCI_MAPREG_MEM_TYPE(val) ==
1635 			    PCI_MAPREG_MEM_TYPE_64BIT)
1636 				rid += 4;	/* skip high bits, too */
1637 		}
1638 
1639 		if (pci_mapreg_map(pa, rid, PCI_MAPREG_TYPE_IO, 0,
1640 		    &sc->osdep.io_bus_space_tag, &sc->osdep.io_bus_space_handle,
1641 		    &sc->osdep.em_iobase, &sc->osdep.em_iosize, 0)) {
1642 			printf(": cannot find i/o space\n");
1643 			return (ENXIO);
1644 		}
1645 
1646 		sc->hw.io_base = 0;
1647 		break;
1648 	default:
1649 		break;
1650 	}
1651 
1652 	sc->osdep.em_flashoffset = 0;
1653 	/* for ICH8 and family we need to find the flash memory */
1654 	if (sc->hw.mac_type == em_pch_spt) {
1655 		sc->osdep.flash_bus_space_tag = sc->osdep.mem_bus_space_tag;
1656 		sc->osdep.flash_bus_space_handle = sc->osdep.mem_bus_space_handle;
1657 		sc->osdep.em_flashbase = 0;
1658 		sc->osdep.em_flashsize = 0;
1659 		sc->osdep.em_flashoffset = 0xe000;
1660 	} else if (IS_ICH8(sc->hw.mac_type)) {
1661 		val = pci_conf_read(pa->pa_pc, pa->pa_tag, EM_FLASH);
1662 		if (PCI_MAPREG_TYPE(val) != PCI_MAPREG_TYPE_MEM) {
1663 			printf(": flash is not mem space\n");
1664 			return (ENXIO);
1665 		}
1666 
1667 		if (pci_mapreg_map(pa, EM_FLASH, PCI_MAPREG_MEM_TYPE(val), 0,
1668 		    &sc->osdep.flash_bus_space_tag, &sc->osdep.flash_bus_space_handle,
1669 		    &sc->osdep.em_flashbase, &sc->osdep.em_flashsize, 0)) {
1670 			printf(": cannot find mem space\n");
1671 			return (ENXIO);
1672 		}
1673         }
1674 
1675 	sc->legacy_irq = 0;
1676 	if (pci_intr_map_msi(pa, &ih)) {
1677 		if (pci_intr_map(pa, &ih)) {
1678 			printf(": couldn't map interrupt\n");
1679 			return (ENXIO);
1680 		}
1681 		sc->legacy_irq = 1;
1682 	}
1683 
1684 	sc->osdep.dev = (struct device *)sc;
1685 	sc->hw.back = &sc->osdep;
1686 
1687 	intrstr = pci_intr_string(pc, ih);
1688 	sc->sc_intrhand = pci_intr_establish(pc, ih, IPL_NET | IPL_MPSAFE,
1689 	    em_intr, sc, DEVNAME(sc));
1690 	if (sc->sc_intrhand == NULL) {
1691 		printf(": couldn't establish interrupt");
1692 		if (intrstr != NULL)
1693 			printf(" at %s", intrstr);
1694 		printf("\n");
1695 		return (ENXIO);
1696 	}
1697 	printf(": %s", intrstr);
1698 
1699 	/*
1700 	 * the ICP_xxxx device has multiple, duplicate register sets for
1701 	 * use when it is being used as a network processor. Disable those
1702 	 * registers here, as they are not necessary in this context and
1703 	 * can confuse the system
1704 	 */
1705 	if(sc->hw.mac_type == em_icp_xxxx) {
1706 		int offset;
1707 		pcireg_t val;
1708 
1709 		if (!pci_get_capability(sc->osdep.em_pa.pa_pc,
1710 		    sc->osdep.em_pa.pa_tag, PCI_CAP_ID_ST, &offset, &val)) {
1711 			return (0);
1712 		}
1713 		offset += PCI_ST_SMIA_OFFSET;
1714 		pci_conf_write(sc->osdep.em_pa.pa_pc, sc->osdep.em_pa.pa_tag,
1715 		    offset, 0x06);
1716 		E1000_WRITE_REG(&sc->hw, IMC1, ~0x0);
1717 		E1000_WRITE_REG(&sc->hw, IMC2, ~0x0);
1718 	}
1719 	return (0);
1720 }
1721 
1722 void
1723 em_free_pci_resources(struct em_softc *sc)
1724 {
1725 	struct pci_attach_args *pa = &sc->osdep.em_pa;
1726 	pci_chipset_tag_t	pc = pa->pa_pc;
1727 
1728 	if (sc->sc_intrhand)
1729 		pci_intr_disestablish(pc, sc->sc_intrhand);
1730 	sc->sc_intrhand = 0;
1731 
1732 	if (sc->osdep.em_flashbase)
1733 		bus_space_unmap(sc->osdep.flash_bus_space_tag, sc->osdep.flash_bus_space_handle,
1734 				sc->osdep.em_flashsize);
1735 	sc->osdep.em_flashbase = 0;
1736 
1737 	if (sc->osdep.em_iobase)
1738 		bus_space_unmap(sc->osdep.io_bus_space_tag, sc->osdep.io_bus_space_handle,
1739 				sc->osdep.em_iosize);
1740 	sc->osdep.em_iobase = 0;
1741 
1742 	if (sc->osdep.em_membase)
1743 		bus_space_unmap(sc->osdep.mem_bus_space_tag, sc->osdep.mem_bus_space_handle,
1744 				sc->osdep.em_memsize);
1745 	sc->osdep.em_membase = 0;
1746 }
1747 
1748 /*********************************************************************
1749  *
1750  *  Initialize the hardware to a configuration as specified by the
1751  *  em_softc structure. The controller is reset, the EEPROM is
1752  *  verified, the MAC address is set, then the shared initialization
1753  *  routines are called.
1754  *
1755  **********************************************************************/
1756 int
1757 em_hardware_init(struct em_softc *sc)
1758 {
1759 	uint32_t ret_val;
1760 	u_int16_t rx_buffer_size;
1761 
1762 	INIT_DEBUGOUT("em_hardware_init: begin");
1763 	if (sc->hw.mac_type == em_pch_spt)
1764 		em_flush_desc_rings(sc);
1765 	/* Issue a global reset */
1766 	em_reset_hw(&sc->hw);
1767 
1768 	/* When hardware is reset, fifo_head is also reset */
1769 	sc->tx_fifo_head = 0;
1770 
1771 	/* Make sure we have a good EEPROM before we read from it */
1772 	if (em_get_flash_presence_i210(&sc->hw) &&
1773 	    em_validate_eeprom_checksum(&sc->hw) < 0) {
1774 		/*
1775 		 * Some PCIe parts fail the first check due to
1776 		 * the link being in sleep state, call it again,
1777 		 * if it fails a second time its a real issue.
1778 		 */
1779 		if (em_validate_eeprom_checksum(&sc->hw) < 0) {
1780 			printf("%s: The EEPROM Checksum Is Not Valid\n",
1781 			       DEVNAME(sc));
1782 			return (EIO);
1783 		}
1784 	}
1785 
1786 	if (em_get_flash_presence_i210(&sc->hw) &&
1787 	    em_read_part_num(&sc->hw, &(sc->part_num)) < 0) {
1788 		printf("%s: EEPROM read error while reading part number\n",
1789 		       DEVNAME(sc));
1790 		return (EIO);
1791 	}
1792 
1793 	/* Set up smart power down as default off on newer adapters */
1794 	if (!em_smart_pwr_down &&
1795 	     (sc->hw.mac_type == em_82571 ||
1796 	      sc->hw.mac_type == em_82572 ||
1797 	      sc->hw.mac_type == em_82575 ||
1798 	      sc->hw.mac_type == em_82580 ||
1799 	      sc->hw.mac_type == em_i210 ||
1800 	      sc->hw.mac_type == em_i350 )) {
1801 		uint16_t phy_tmp = 0;
1802 
1803 		/* Speed up time to link by disabling smart power down */
1804 		em_read_phy_reg(&sc->hw, IGP02E1000_PHY_POWER_MGMT, &phy_tmp);
1805 		phy_tmp &= ~IGP02E1000_PM_SPD;
1806 		em_write_phy_reg(&sc->hw, IGP02E1000_PHY_POWER_MGMT, phy_tmp);
1807 	}
1808 
1809 	em_legacy_irq_quirk_spt(sc);
1810 
1811 	/*
1812 	 * These parameters control the automatic generation (Tx) and
1813 	 * response (Rx) to Ethernet PAUSE frames.
1814 	 * - High water mark should allow for at least two frames to be
1815 	 *   received after sending an XOFF.
1816 	 * - Low water mark works best when it is very near the high water mark.
1817 	 *   This allows the receiver to restart by sending XON when it has
1818 	 *   drained a bit.  Here we use an arbitary value of 1500 which will
1819 	 *   restart after one full frame is pulled from the buffer.  There
1820 	 *   could be several smaller frames in the buffer and if so they will
1821 	 *   not trigger the XON until their total number reduces the buffer
1822 	 *   by 1500.
1823 	 * - The pause time is fairly large at 1000 x 512ns = 512 usec.
1824 	 */
1825 	rx_buffer_size = ((E1000_READ_REG(&sc->hw, PBA) & 0xffff) << 10 );
1826 
1827 	sc->hw.fc_high_water = rx_buffer_size -
1828 	    EM_ROUNDUP(sc->hw.max_frame_size, 1024);
1829 	sc->hw.fc_low_water = sc->hw.fc_high_water - 1500;
1830 	if (sc->hw.mac_type == em_80003es2lan)
1831 		sc->hw.fc_pause_time = 0xFFFF;
1832 	else
1833 		sc->hw.fc_pause_time = 1000;
1834 	sc->hw.fc_send_xon = TRUE;
1835 	sc->hw.fc = E1000_FC_FULL;
1836 
1837 	em_disable_aspm(sc);
1838 
1839 	if ((ret_val = em_init_hw(&sc->hw)) != 0) {
1840 		if (ret_val == E1000_DEFER_INIT) {
1841 			INIT_DEBUGOUT("\nHardware Initialization Deferred ");
1842 			return (EAGAIN);
1843 		}
1844 		printf("\n%s: Hardware Initialization Failed\n",
1845 		       DEVNAME(sc));
1846 		return (EIO);
1847 	}
1848 
1849 	em_check_for_link(&sc->hw);
1850 
1851 	return (0);
1852 }
1853 
1854 /*********************************************************************
1855  *
1856  *  Setup networking device structure and register an interface.
1857  *
1858  **********************************************************************/
1859 void
1860 em_setup_interface(struct em_softc *sc)
1861 {
1862 	struct ifnet   *ifp;
1863 	uint64_t fiber_type = IFM_1000_SX;
1864 
1865 	INIT_DEBUGOUT("em_setup_interface: begin");
1866 
1867 	ifp = &sc->sc_ac.ac_if;
1868 	strlcpy(ifp->if_xname, DEVNAME(sc), IFNAMSIZ);
1869 	ifp->if_softc = sc;
1870 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1871 	ifp->if_xflags = IFXF_MPSAFE;
1872 	ifp->if_ioctl = em_ioctl;
1873 	ifp->if_start = em_start;
1874 	ifp->if_watchdog = em_watchdog;
1875 	ifp->if_hardmtu =
1876 		sc->hw.max_frame_size - ETHER_HDR_LEN - ETHER_CRC_LEN;
1877 	IFQ_SET_MAXLEN(&ifp->if_snd, sc->sc_tx_slots - 1);
1878 
1879 	ifp->if_capabilities = IFCAP_VLAN_MTU;
1880 
1881 #if NVLAN > 0
1882 	if (sc->hw.mac_type != em_82575 && sc->hw.mac_type != em_82580 &&
1883 	    sc->hw.mac_type != em_i210 && sc->hw.mac_type != em_i350)
1884 		ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING;
1885 #endif
1886 
1887 	if (sc->hw.mac_type >= em_82543 && sc->hw.mac_type != em_82575 &&
1888 	    sc->hw.mac_type != em_82580 && sc->hw.mac_type != em_i210 &&
1889 	    sc->hw.mac_type != em_i350)
1890 		ifp->if_capabilities |= IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4;
1891 
1892 	/*
1893 	 * Specify the media types supported by this adapter and register
1894 	 * callbacks to update media and link information
1895 	 */
1896 	ifmedia_init(&sc->media, IFM_IMASK, em_media_change,
1897 		     em_media_status);
1898 	if (sc->hw.media_type == em_media_type_fiber ||
1899 	    sc->hw.media_type == em_media_type_internal_serdes) {
1900 		if (sc->hw.mac_type == em_82545)
1901 			fiber_type = IFM_1000_LX;
1902 		ifmedia_add(&sc->media, IFM_ETHER | fiber_type | IFM_FDX,
1903 			    0, NULL);
1904 		ifmedia_add(&sc->media, IFM_ETHER | fiber_type,
1905 			    0, NULL);
1906 	} else {
1907 		ifmedia_add(&sc->media, IFM_ETHER | IFM_10_T, 0, NULL);
1908 		ifmedia_add(&sc->media, IFM_ETHER | IFM_10_T | IFM_FDX,
1909 			    0, NULL);
1910 		ifmedia_add(&sc->media, IFM_ETHER | IFM_100_TX,
1911 			    0, NULL);
1912 		ifmedia_add(&sc->media, IFM_ETHER | IFM_100_TX | IFM_FDX,
1913 			    0, NULL);
1914 		if (sc->hw.phy_type != em_phy_ife) {
1915 			ifmedia_add(&sc->media, IFM_ETHER | IFM_1000_T | IFM_FDX,
1916 				    0, NULL);
1917 			ifmedia_add(&sc->media, IFM_ETHER | IFM_1000_T, 0, NULL);
1918 		}
1919 	}
1920 	ifmedia_add(&sc->media, IFM_ETHER | IFM_AUTO, 0, NULL);
1921 	ifmedia_set(&sc->media, IFM_ETHER | IFM_AUTO);
1922 
1923 	if_attach(ifp);
1924 	ether_ifattach(ifp);
1925 }
1926 
1927 int
1928 em_detach(struct device *self, int flags)
1929 {
1930 	struct em_softc *sc = (struct em_softc *)self;
1931 	struct ifnet *ifp = &sc->sc_ac.ac_if;
1932 	struct pci_attach_args *pa = &sc->osdep.em_pa;
1933 	pci_chipset_tag_t	pc = pa->pa_pc;
1934 
1935 	if (sc->sc_intrhand)
1936 		pci_intr_disestablish(pc, sc->sc_intrhand);
1937 	sc->sc_intrhand = 0;
1938 
1939 	em_stop(sc, 1);
1940 
1941 	em_free_pci_resources(sc);
1942 
1943 	if (sc->sc_rx_desc_ring != NULL) {
1944 		sc->sc_rx_desc_ring = NULL;
1945 		em_dma_free(sc, &sc->sc_rx_dma);
1946 	}
1947 	if (sc->sc_tx_desc_ring != NULL) {
1948 		sc->sc_tx_desc_ring = NULL;
1949 		em_dma_free(sc, &sc->sc_tx_dma);
1950 	}
1951 
1952 	ether_ifdetach(ifp);
1953 	if_detach(ifp);
1954 
1955 	return (0);
1956 }
1957 
1958 int
1959 em_activate(struct device *self, int act)
1960 {
1961 	struct em_softc *sc = (struct em_softc *)self;
1962 	struct ifnet *ifp = &sc->sc_ac.ac_if;
1963 	int rv = 0;
1964 
1965 	switch (act) {
1966 	case DVACT_SUSPEND:
1967 		if (ifp->if_flags & IFF_RUNNING)
1968 			em_stop(sc, 0);
1969 		/* We have no children atm, but we will soon */
1970 		rv = config_activate_children(self, act);
1971 		break;
1972 	case DVACT_RESUME:
1973 		if (ifp->if_flags & IFF_UP)
1974 			em_init(sc);
1975 		break;
1976 	default:
1977 		rv = config_activate_children(self, act);
1978 		break;
1979 	}
1980 	return (rv);
1981 }
1982 
1983 /*********************************************************************
1984  *
1985  *  Workaround for SmartSpeed on 82541 and 82547 controllers
1986  *
1987  **********************************************************************/
1988 void
1989 em_smartspeed(struct em_softc *sc)
1990 {
1991 	uint16_t phy_tmp;
1992 
1993 	if (sc->link_active || (sc->hw.phy_type != em_phy_igp) ||
1994 	    !sc->hw.autoneg || !(sc->hw.autoneg_advertised & ADVERTISE_1000_FULL))
1995 		return;
1996 
1997 	if (sc->smartspeed == 0) {
1998 		/* If Master/Slave config fault is asserted twice,
1999 		 * we assume back-to-back */
2000 		em_read_phy_reg(&sc->hw, PHY_1000T_STATUS, &phy_tmp);
2001 		if (!(phy_tmp & SR_1000T_MS_CONFIG_FAULT))
2002 			return;
2003 		em_read_phy_reg(&sc->hw, PHY_1000T_STATUS, &phy_tmp);
2004 		if (phy_tmp & SR_1000T_MS_CONFIG_FAULT) {
2005 			em_read_phy_reg(&sc->hw, PHY_1000T_CTRL,
2006 					&phy_tmp);
2007 			if (phy_tmp & CR_1000T_MS_ENABLE) {
2008 				phy_tmp &= ~CR_1000T_MS_ENABLE;
2009 				em_write_phy_reg(&sc->hw,
2010 						    PHY_1000T_CTRL, phy_tmp);
2011 				sc->smartspeed++;
2012 				if (sc->hw.autoneg &&
2013 				    !em_phy_setup_autoneg(&sc->hw) &&
2014 				    !em_read_phy_reg(&sc->hw, PHY_CTRL,
2015 						       &phy_tmp)) {
2016 					phy_tmp |= (MII_CR_AUTO_NEG_EN |
2017 						    MII_CR_RESTART_AUTO_NEG);
2018 					em_write_phy_reg(&sc->hw,
2019 							 PHY_CTRL, phy_tmp);
2020 				}
2021 			}
2022 		}
2023 		return;
2024 	} else if (sc->smartspeed == EM_SMARTSPEED_DOWNSHIFT) {
2025 		/* If still no link, perhaps using 2/3 pair cable */
2026 		em_read_phy_reg(&sc->hw, PHY_1000T_CTRL, &phy_tmp);
2027 		phy_tmp |= CR_1000T_MS_ENABLE;
2028 		em_write_phy_reg(&sc->hw, PHY_1000T_CTRL, phy_tmp);
2029 		if (sc->hw.autoneg &&
2030 		    !em_phy_setup_autoneg(&sc->hw) &&
2031 		    !em_read_phy_reg(&sc->hw, PHY_CTRL, &phy_tmp)) {
2032 			phy_tmp |= (MII_CR_AUTO_NEG_EN |
2033 				    MII_CR_RESTART_AUTO_NEG);
2034 			em_write_phy_reg(&sc->hw, PHY_CTRL, phy_tmp);
2035 		}
2036 	}
2037 	/* Restart process after EM_SMARTSPEED_MAX iterations */
2038 	if (sc->smartspeed++ == EM_SMARTSPEED_MAX)
2039 		sc->smartspeed = 0;
2040 }
2041 
2042 /*
2043  * Manage DMA'able memory.
2044  */
2045 int
2046 em_dma_malloc(struct em_softc *sc, bus_size_t size, struct em_dma_alloc *dma)
2047 {
2048 	int r;
2049 
2050 	r = bus_dmamap_create(sc->sc_dmat, size, 1,
2051 	    size, 0, BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &dma->dma_map);
2052 	if (r != 0)
2053 		return (r);
2054 
2055 	r = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &dma->dma_seg,
2056 	    1, &dma->dma_nseg, BUS_DMA_WAITOK | BUS_DMA_ZERO);
2057 	if (r != 0)
2058 		goto destroy;
2059 
2060 	r = bus_dmamem_map(sc->sc_dmat, &dma->dma_seg, dma->dma_nseg, size,
2061 	    &dma->dma_vaddr, BUS_DMA_WAITOK);
2062 	if (r != 0)
2063 		goto free;
2064 
2065 	r = bus_dmamap_load(sc->sc_dmat, dma->dma_map, dma->dma_vaddr, size,
2066 	    NULL, BUS_DMA_WAITOK);
2067 	if (r != 0)
2068 		goto unmap;
2069 
2070 	dma->dma_size = size;
2071 	return (0);
2072 
2073 unmap:
2074 	bus_dmamem_unmap(sc->sc_dmat, dma->dma_vaddr, size);
2075 free:
2076 	bus_dmamem_free(sc->sc_dmat, &dma->dma_seg, dma->dma_nseg);
2077 destroy:
2078 	bus_dmamap_destroy(sc->sc_dmat, dma->dma_map);
2079 
2080 	return (r);
2081 }
2082 
2083 void
2084 em_dma_free(struct em_softc *sc, struct em_dma_alloc *dma)
2085 {
2086 	bus_dmamap_unload(sc->sc_dmat, dma->dma_map);
2087 	bus_dmamem_unmap(sc->sc_dmat, dma->dma_vaddr, dma->dma_size);
2088 	bus_dmamem_free(sc->sc_dmat, &dma->dma_seg, dma->dma_nseg);
2089 	bus_dmamap_destroy(sc->sc_dmat, dma->dma_map);
2090 }
2091 
2092 /*********************************************************************
2093  *
2094  *  Allocate memory for tx_buffer structures. The tx_buffer stores all
2095  *  the information needed to transmit a packet on the wire.
2096  *
2097  **********************************************************************/
2098 int
2099 em_allocate_transmit_structures(struct em_softc *sc)
2100 {
2101 	bus_dmamap_sync(sc->sc_dmat, sc->sc_tx_dma.dma_map,
2102 	    0, sc->sc_tx_dma.dma_map->dm_mapsize,
2103 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2104 
2105 	sc->sc_tx_pkts_ring = mallocarray(sc->sc_tx_slots,
2106 	    sizeof(*sc->sc_tx_pkts_ring), M_DEVBUF, M_NOWAIT | M_ZERO);
2107 	if (sc->sc_tx_pkts_ring == NULL) {
2108 		printf("%s: Unable to allocate tx_buffer memory\n",
2109 		       DEVNAME(sc));
2110 		return (ENOMEM);
2111 	}
2112 
2113 	return (0);
2114 }
2115 
2116 /*********************************************************************
2117  *
2118  *  Allocate and initialize transmit structures.
2119  *
2120  **********************************************************************/
2121 int
2122 em_setup_transmit_structures(struct em_softc *sc)
2123 {
2124 	struct em_packet *pkt;
2125 	int error, i;
2126 
2127 	if ((error = em_allocate_transmit_structures(sc)) != 0)
2128 		goto fail;
2129 
2130 	bzero((void *) sc->sc_tx_desc_ring,
2131 	      (sizeof(struct em_tx_desc)) * sc->sc_tx_slots);
2132 
2133 	for (i = 0; i < sc->sc_tx_slots; i++) {
2134 		pkt = &sc->sc_tx_pkts_ring[i];
2135 		error = bus_dmamap_create(sc->sc_dmat, MAX_JUMBO_FRAME_SIZE,
2136 		    EM_MAX_SCATTER / (sc->pcix_82544 ? 2 : 1),
2137 		    MAX_JUMBO_FRAME_SIZE, 0, BUS_DMA_NOWAIT, &pkt->pkt_map);
2138 		if (error != 0) {
2139 			printf("%s: Unable to create TX DMA map\n",
2140 			    DEVNAME(sc));
2141 			goto fail;
2142 		}
2143 	}
2144 
2145 	sc->sc_tx_desc_head = 0;
2146 	sc->sc_tx_desc_tail = 0;
2147 
2148 	/* Set checksum context */
2149 	sc->active_checksum_context = OFFLOAD_NONE;
2150 
2151 	return (0);
2152 
2153 fail:
2154 	em_free_transmit_structures(sc);
2155 	return (error);
2156 }
2157 
2158 /*********************************************************************
2159  *
2160  *  Enable transmit unit.
2161  *
2162  **********************************************************************/
2163 void
2164 em_initialize_transmit_unit(struct em_softc *sc)
2165 {
2166 	u_int32_t	reg_tctl, reg_tipg = 0;
2167 	u_int64_t	bus_addr;
2168 
2169 	INIT_DEBUGOUT("em_initialize_transmit_unit: begin");
2170 
2171 	/* Setup the Base and Length of the Tx Descriptor Ring */
2172 	bus_addr = sc->sc_tx_dma.dma_map->dm_segs[0].ds_addr;
2173 	E1000_WRITE_REG(&sc->hw, TDLEN,
2174 			sc->sc_tx_slots *
2175 			sizeof(struct em_tx_desc));
2176 	E1000_WRITE_REG(&sc->hw, TDBAH, (u_int32_t)(bus_addr >> 32));
2177 	E1000_WRITE_REG(&sc->hw, TDBAL, (u_int32_t)bus_addr);
2178 
2179 	/* Setup the HW Tx Head and Tail descriptor pointers */
2180 	E1000_WRITE_REG(&sc->hw, TDT, 0);
2181 	E1000_WRITE_REG(&sc->hw, TDH, 0);
2182 
2183 	HW_DEBUGOUT2("Base = %x, Length = %x\n",
2184 		     E1000_READ_REG(&sc->hw, TDBAL),
2185 		     E1000_READ_REG(&sc->hw, TDLEN));
2186 
2187 	/* Set the default values for the Tx Inter Packet Gap timer */
2188 	switch (sc->hw.mac_type) {
2189 	case em_82542_rev2_0:
2190 	case em_82542_rev2_1:
2191 		reg_tipg = DEFAULT_82542_TIPG_IPGT;
2192 		reg_tipg |= DEFAULT_82542_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
2193 		reg_tipg |= DEFAULT_82542_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
2194 		break;
2195 	case em_80003es2lan:
2196 		reg_tipg = DEFAULT_82543_TIPG_IPGR1;
2197 		reg_tipg |= DEFAULT_80003ES2LAN_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
2198 		break;
2199 	default:
2200 		if (sc->hw.media_type == em_media_type_fiber ||
2201 		    sc->hw.media_type == em_media_type_internal_serdes)
2202 			reg_tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
2203 		else
2204 			reg_tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
2205 		reg_tipg |= DEFAULT_82543_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
2206 		reg_tipg |= DEFAULT_82543_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
2207 	}
2208 
2209 
2210 	E1000_WRITE_REG(&sc->hw, TIPG, reg_tipg);
2211 	E1000_WRITE_REG(&sc->hw, TIDV, sc->tx_int_delay);
2212 	if (sc->hw.mac_type >= em_82540)
2213 		E1000_WRITE_REG(&sc->hw, TADV, sc->tx_abs_int_delay);
2214 
2215 	/* Setup Transmit Descriptor Base Settings */
2216 	sc->sc_txd_cmd = E1000_TXD_CMD_IFCS;
2217 
2218 	if (sc->hw.mac_type == em_82575 || sc->hw.mac_type == em_82580 ||
2219 	    sc->hw.mac_type == em_i210 || sc->hw.mac_type == em_i350) {
2220 		/* 82575/6 need to enable the TX queue and lack the IDE bit */
2221 		reg_tctl = E1000_READ_REG(&sc->hw, TXDCTL);
2222 		reg_tctl |= E1000_TXDCTL_QUEUE_ENABLE;
2223 		E1000_WRITE_REG(&sc->hw, TXDCTL, reg_tctl);
2224 	} else if (sc->tx_int_delay > 0)
2225 		sc->sc_txd_cmd |= E1000_TXD_CMD_IDE;
2226 
2227 	/* Program the Transmit Control Register */
2228 	reg_tctl = E1000_TCTL_PSP | E1000_TCTL_EN |
2229 		   (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
2230 	if (sc->hw.mac_type >= em_82571)
2231 		reg_tctl |= E1000_TCTL_MULR;
2232 	if (sc->link_duplex == FULL_DUPLEX)
2233 		reg_tctl |= E1000_FDX_COLLISION_DISTANCE << E1000_COLD_SHIFT;
2234 	else
2235 		reg_tctl |= E1000_HDX_COLLISION_DISTANCE << E1000_COLD_SHIFT;
2236 	/* This write will effectively turn on the transmit unit */
2237 	E1000_WRITE_REG(&sc->hw, TCTL, reg_tctl);
2238 
2239 	/* SPT Si errata workaround to avoid data corruption */
2240 
2241 	if (sc->hw.mac_type == em_pch_spt) {
2242 		uint32_t	reg_val;
2243 
2244 		reg_val = EM_READ_REG(&sc->hw, E1000_IOSFPC);
2245 		reg_val |= E1000_RCTL_RDMTS_HEX;
2246 		EM_WRITE_REG(&sc->hw, E1000_IOSFPC, reg_val);
2247 
2248 		reg_val = E1000_READ_REG(&sc->hw, TARC0);
2249 		reg_val |= E1000_TARC0_CB_MULTIQ_3_REQ;
2250 		E1000_WRITE_REG(&sc->hw, TARC0, reg_val);
2251 	}
2252 }
2253 
2254 /*********************************************************************
2255  *
2256  *  Free all transmit related data structures.
2257  *
2258  **********************************************************************/
2259 void
2260 em_free_transmit_structures(struct em_softc *sc)
2261 {
2262 	struct em_packet *pkt;
2263 	int i;
2264 
2265 	INIT_DEBUGOUT("free_transmit_structures: begin");
2266 
2267 	if (sc->sc_tx_pkts_ring != NULL) {
2268 		for (i = 0; i < sc->sc_tx_slots; i++) {
2269 			pkt = &sc->sc_tx_pkts_ring[i];
2270 
2271 			if (pkt->pkt_m != NULL) {
2272 				bus_dmamap_sync(sc->sc_dmat, pkt->pkt_map,
2273 				    0, pkt->pkt_map->dm_mapsize,
2274 				    BUS_DMASYNC_POSTWRITE);
2275 				bus_dmamap_unload(sc->sc_dmat, pkt->pkt_map);
2276 
2277 				m_freem(pkt->pkt_m);
2278 				pkt->pkt_m = NULL;
2279 			}
2280 
2281 			if (pkt->pkt_map != NULL) {
2282 				bus_dmamap_destroy(sc->sc_dmat, pkt->pkt_map);
2283 				pkt->pkt_map = NULL;
2284 			}
2285 		}
2286 
2287 		free(sc->sc_tx_pkts_ring, M_DEVBUF,
2288 		    sc->sc_tx_slots * sizeof(*sc->sc_tx_pkts_ring));
2289 		sc->sc_tx_pkts_ring = NULL;
2290 	}
2291 
2292 	bus_dmamap_sync(sc->sc_dmat, sc->sc_tx_dma.dma_map,
2293 	    0, sc->sc_tx_dma.dma_map->dm_mapsize,
2294 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2295 }
2296 
2297 /*********************************************************************
2298  *
2299  *  The offload context needs to be set when we transfer the first
2300  *  packet of a particular protocol (TCP/UDP). We change the
2301  *  context only if the protocol type changes.
2302  *
2303  **********************************************************************/
2304 u_int
2305 em_transmit_checksum_setup(struct em_softc *sc, struct mbuf *mp, u_int head,
2306     u_int32_t *txd_upper, u_int32_t *txd_lower)
2307 {
2308 	struct em_context_desc *TXD;
2309 
2310 	if (mp->m_pkthdr.csum_flags & M_TCP_CSUM_OUT) {
2311 		*txd_upper = E1000_TXD_POPTS_TXSM << 8;
2312 		*txd_lower = E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
2313 		if (sc->active_checksum_context == OFFLOAD_TCP_IP)
2314 			return (0);
2315 		else
2316 			sc->active_checksum_context = OFFLOAD_TCP_IP;
2317 	} else if (mp->m_pkthdr.csum_flags & M_UDP_CSUM_OUT) {
2318 		*txd_upper = E1000_TXD_POPTS_TXSM << 8;
2319 		*txd_lower = E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
2320 		if (sc->active_checksum_context == OFFLOAD_UDP_IP)
2321 			return (0);
2322 		else
2323 			sc->active_checksum_context = OFFLOAD_UDP_IP;
2324 	} else {
2325 		*txd_upper = 0;
2326 		*txd_lower = 0;
2327 		return (0);
2328 	}
2329 
2330 	/* If we reach this point, the checksum offload context
2331 	 * needs to be reset.
2332 	 */
2333 	TXD = (struct em_context_desc *)&sc->sc_tx_desc_ring[head];
2334 
2335 	TXD->lower_setup.ip_fields.ipcss = ETHER_HDR_LEN;
2336 	TXD->lower_setup.ip_fields.ipcso =
2337 	    ETHER_HDR_LEN + offsetof(struct ip, ip_sum);
2338 	TXD->lower_setup.ip_fields.ipcse =
2339 	    htole16(ETHER_HDR_LEN + sizeof(struct ip) - 1);
2340 
2341 	TXD->upper_setup.tcp_fields.tucss =
2342 	    ETHER_HDR_LEN + sizeof(struct ip);
2343 	TXD->upper_setup.tcp_fields.tucse = htole16(0);
2344 
2345 	if (sc->active_checksum_context == OFFLOAD_TCP_IP) {
2346 		TXD->upper_setup.tcp_fields.tucso =
2347 		    ETHER_HDR_LEN + sizeof(struct ip) +
2348 		    offsetof(struct tcphdr, th_sum);
2349 	} else if (sc->active_checksum_context == OFFLOAD_UDP_IP) {
2350 		TXD->upper_setup.tcp_fields.tucso =
2351 		    ETHER_HDR_LEN + sizeof(struct ip) +
2352 		    offsetof(struct udphdr, uh_sum);
2353 	}
2354 
2355 	TXD->tcp_seg_setup.data = htole32(0);
2356 	TXD->cmd_and_length = htole32(sc->sc_txd_cmd | E1000_TXD_CMD_DEXT);
2357 
2358 	return (1);
2359 }
2360 
2361 /**********************************************************************
2362  *
2363  *  Examine each tx_buffer in the used queue. If the hardware is done
2364  *  processing the packet then free associated resources. The
2365  *  tx_buffer is put back on the free queue.
2366  *
2367  **********************************************************************/
2368 void
2369 em_txeof(struct em_softc *sc)
2370 {
2371 	struct ifnet *ifp = &sc->sc_ac.ac_if;
2372 	struct em_packet *pkt;
2373 	struct em_tx_desc *desc;
2374 	u_int head, tail;
2375 	u_int free = 0;
2376 
2377 	head = sc->sc_tx_desc_head;
2378 	tail = sc->sc_tx_desc_tail;
2379 
2380 	if (head == tail)
2381 		return;
2382 
2383 	bus_dmamap_sync(sc->sc_dmat, sc->sc_tx_dma.dma_map,
2384 	    0, sc->sc_tx_dma.dma_map->dm_mapsize,
2385 	    BUS_DMASYNC_POSTREAD);
2386 
2387 	do {
2388 		pkt = &sc->sc_tx_pkts_ring[tail];
2389 		desc = &sc->sc_tx_desc_ring[pkt->pkt_eop];
2390 
2391 		if (!ISSET(desc->upper.fields.status, E1000_TXD_STAT_DD))
2392 			break;
2393 
2394 		bus_dmamap_sync(sc->sc_dmat, pkt->pkt_map,
2395 		    0, pkt->pkt_map->dm_mapsize,
2396 		    BUS_DMASYNC_POSTWRITE);
2397 		bus_dmamap_unload(sc->sc_dmat, pkt->pkt_map);
2398 
2399 		KASSERT(pkt->pkt_m != NULL);
2400 
2401 		m_freem(pkt->pkt_m);
2402 		pkt->pkt_m = NULL;
2403 
2404 		tail = pkt->pkt_eop;
2405 
2406 		if (++tail == sc->sc_tx_slots)
2407 			tail = 0;
2408 
2409 		free++;
2410 	} while (tail != head);
2411 
2412 	bus_dmamap_sync(sc->sc_dmat, sc->sc_tx_dma.dma_map,
2413 	    0, sc->sc_tx_dma.dma_map->dm_mapsize,
2414 	    BUS_DMASYNC_PREREAD);
2415 
2416 	if (free == 0)
2417 		return;
2418 
2419 	ifp->if_opackets += free;
2420 
2421 	sc->sc_tx_desc_tail = tail;
2422 
2423 	if (ifq_is_oactive(&ifp->if_snd))
2424 		ifq_restart(&ifp->if_snd);
2425 	else if (tail == head)
2426 		ifp->if_timer = 0;
2427 }
2428 
2429 /*********************************************************************
2430  *
2431  *  Get a buffer from system mbuf buffer pool.
2432  *
2433  **********************************************************************/
2434 int
2435 em_get_buf(struct em_softc *sc, int i)
2436 {
2437 	struct mbuf    *m;
2438 	struct em_packet *pkt;
2439 	struct em_rx_desc *desc;
2440 	int error;
2441 
2442 	pkt = &sc->sc_rx_pkts_ring[i];
2443 	desc = &sc->sc_rx_desc_ring[i];
2444 
2445 	KASSERT(pkt->pkt_m == NULL);
2446 
2447 	m = MCLGETI(NULL, M_DONTWAIT, NULL, EM_MCLBYTES);
2448 	if (m == NULL) {
2449 		sc->mbuf_cluster_failed++;
2450 		return (ENOBUFS);
2451 	}
2452 	m->m_len = m->m_pkthdr.len = EM_MCLBYTES;
2453 #ifdef __STRICT_ALIGNMENT
2454 	m_adj(m, ETHER_ALIGN);
2455 #endif
2456 
2457 	error = bus_dmamap_load_mbuf(sc->sc_dmat, pkt->pkt_map,
2458 	    m, BUS_DMA_NOWAIT);
2459 	if (error) {
2460 		m_freem(m);
2461 		return (error);
2462 	}
2463 
2464 	bus_dmamap_sync(sc->sc_dmat, pkt->pkt_map,
2465 	    0, pkt->pkt_map->dm_mapsize,
2466 	    BUS_DMASYNC_PREREAD);
2467 	pkt->pkt_m = m;
2468 
2469 	memset(desc, 0, sizeof(*desc));
2470 	htolem64(&desc->buffer_addr, pkt->pkt_map->dm_segs[0].ds_addr);
2471 
2472 	return (0);
2473 }
2474 
2475 /*********************************************************************
2476  *
2477  *  Allocate memory for rx_buffer structures. Since we use one
2478  *  rx_buffer per received packet, the maximum number of rx_buffer's
2479  *  that we'll need is equal to the number of receive descriptors
2480  *  that we've allocated.
2481  *
2482  **********************************************************************/
2483 int
2484 em_allocate_receive_structures(struct em_softc *sc)
2485 {
2486 	struct em_packet *pkt;
2487 	int i;
2488 	int error;
2489 
2490 	sc->sc_rx_pkts_ring = mallocarray(sc->sc_rx_slots,
2491 	    sizeof(*sc->sc_rx_pkts_ring), M_DEVBUF, M_NOWAIT | M_ZERO);
2492 	if (sc->sc_rx_pkts_ring == NULL) {
2493 		printf("%s: Unable to allocate rx_buffer memory\n",
2494 		    DEVNAME(sc));
2495 		return (ENOMEM);
2496 	}
2497 
2498 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rx_dma.dma_map,
2499 	    0, sc->sc_rx_dma.dma_map->dm_mapsize,
2500 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2501 
2502 	for (i = 0; i < sc->sc_rx_slots; i++) {
2503 		pkt = &sc->sc_rx_pkts_ring[i];
2504 
2505 		error = bus_dmamap_create(sc->sc_dmat, EM_MCLBYTES, 1,
2506 		    EM_MCLBYTES, 0, BUS_DMA_NOWAIT, &pkt->pkt_map);
2507 		if (error != 0) {
2508 			printf("%s: em_allocate_receive_structures: "
2509 			    "bus_dmamap_create failed; error %u\n",
2510 			    DEVNAME(sc), error);
2511 			goto fail;
2512 		}
2513 
2514 		pkt->pkt_m = NULL;
2515 	}
2516 
2517         return (0);
2518 
2519 fail:
2520 	em_free_receive_structures(sc);
2521 	return (error);
2522 }
2523 
2524 /*********************************************************************
2525  *
2526  *  Allocate and initialize receive structures.
2527  *
2528  **********************************************************************/
2529 int
2530 em_setup_receive_structures(struct em_softc *sc)
2531 {
2532 	struct ifnet *ifp = &sc->sc_ac.ac_if;
2533 	u_int lwm;
2534 
2535 	memset(sc->sc_rx_desc_ring, 0,
2536 	    sc->sc_rx_slots * sizeof(*sc->sc_rx_desc_ring));
2537 
2538 	if (em_allocate_receive_structures(sc))
2539 		return (ENOMEM);
2540 
2541 	/* Setup our descriptor pointers */
2542 	sc->sc_rx_desc_tail = 0;
2543 	sc->sc_rx_desc_head = sc->sc_rx_slots - 1;
2544 
2545 	lwm = max(4, 2 * ((ifp->if_hardmtu / MCLBYTES) + 1));
2546 	if_rxr_init(&sc->sc_rx_ring, lwm, sc->sc_rx_slots);
2547 
2548 	if (em_rxfill(sc) == 0) {
2549 		printf("%s: unable to fill any rx descriptors\n",
2550 		    DEVNAME(sc));
2551 	}
2552 
2553 	return (0);
2554 }
2555 
2556 /*********************************************************************
2557  *
2558  *  Enable receive unit.
2559  *
2560  **********************************************************************/
2561 void
2562 em_initialize_receive_unit(struct em_softc *sc)
2563 {
2564 	u_int32_t	reg_rctl;
2565 	u_int32_t	reg_rxcsum;
2566 	u_int64_t	bus_addr;
2567 
2568 	INIT_DEBUGOUT("em_initialize_receive_unit: begin");
2569 
2570 	/* Make sure receives are disabled while setting up the descriptor ring */
2571 	E1000_WRITE_REG(&sc->hw, RCTL, 0);
2572 
2573 	/* Set the Receive Delay Timer Register */
2574 	E1000_WRITE_REG(&sc->hw, RDTR,
2575 			sc->rx_int_delay | E1000_RDT_FPDB);
2576 
2577 	if (sc->hw.mac_type >= em_82540) {
2578 		if (sc->rx_int_delay)
2579 			E1000_WRITE_REG(&sc->hw, RADV, sc->rx_abs_int_delay);
2580 
2581 		/* Set the interrupt throttling rate.  Value is calculated
2582 		 * as DEFAULT_ITR = 1/(MAX_INTS_PER_SEC * 256ns) */
2583 		E1000_WRITE_REG(&sc->hw, ITR, DEFAULT_ITR);
2584 	}
2585 
2586 	/* Setup the Base and Length of the Rx Descriptor Ring */
2587 	bus_addr = sc->sc_rx_dma.dma_map->dm_segs[0].ds_addr;
2588 	E1000_WRITE_REG(&sc->hw, RDLEN,
2589 	    sc->sc_rx_slots * sizeof(*sc->sc_rx_desc_ring));
2590 	E1000_WRITE_REG(&sc->hw, RDBAH, (u_int32_t)(bus_addr >> 32));
2591 	E1000_WRITE_REG(&sc->hw, RDBAL, (u_int32_t)bus_addr);
2592 
2593 	/* Setup the Receive Control Register */
2594 	reg_rctl = E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
2595 	    E1000_RCTL_RDMTS_HALF |
2596 	    (sc->hw.mc_filter_type << E1000_RCTL_MO_SHIFT);
2597 
2598 	if (sc->hw.tbi_compatibility_on == TRUE)
2599 		reg_rctl |= E1000_RCTL_SBP;
2600 
2601 	/*
2602 	 * The i350 has a bug where it always strips the CRC whether
2603 	 * asked to or not.  So ask for stripped CRC here and
2604 	 * cope in rxeof
2605 	 */
2606 	if (sc->hw.mac_type == em_i210 || sc->hw.mac_type == em_i350)
2607 		reg_rctl |= E1000_RCTL_SECRC;
2608 
2609 	switch (sc->sc_rx_buffer_len) {
2610 	default:
2611 	case EM_RXBUFFER_2048:
2612 		reg_rctl |= E1000_RCTL_SZ_2048;
2613 		break;
2614 	case EM_RXBUFFER_4096:
2615 		reg_rctl |= E1000_RCTL_SZ_4096|E1000_RCTL_BSEX|E1000_RCTL_LPE;
2616 		break;
2617 	case EM_RXBUFFER_8192:
2618 		reg_rctl |= E1000_RCTL_SZ_8192|E1000_RCTL_BSEX|E1000_RCTL_LPE;
2619 		break;
2620 	case EM_RXBUFFER_16384:
2621 		reg_rctl |= E1000_RCTL_SZ_16384|E1000_RCTL_BSEX|E1000_RCTL_LPE;
2622 		break;
2623 	}
2624 
2625 	if (sc->hw.max_frame_size != ETHER_MAX_LEN)
2626 		reg_rctl |= E1000_RCTL_LPE;
2627 
2628 	/* Enable 82543 Receive Checksum Offload for TCP and UDP */
2629 	if (sc->hw.mac_type >= em_82543) {
2630 		reg_rxcsum = E1000_READ_REG(&sc->hw, RXCSUM);
2631 		reg_rxcsum |= (E1000_RXCSUM_IPOFL | E1000_RXCSUM_TUOFL);
2632 		E1000_WRITE_REG(&sc->hw, RXCSUM, reg_rxcsum);
2633 	}
2634 
2635 	/*
2636 	 * XXX TEMPORARY WORKAROUND: on some systems with 82573
2637 	 * long latencies are observed, like Lenovo X60.
2638 	 */
2639 	if (sc->hw.mac_type == em_82573)
2640 		E1000_WRITE_REG(&sc->hw, RDTR, 0x20);
2641 
2642 	if (sc->hw.mac_type == em_82575 || sc->hw.mac_type == em_82580 ||
2643 	    sc->hw.mac_type == em_i210 || sc->hw.mac_type == em_i350) {
2644 		/* 82575/6 need to enable the RX queue */
2645 		uint32_t reg;
2646 		reg = E1000_READ_REG(&sc->hw, RXDCTL);
2647 		reg |= E1000_RXDCTL_QUEUE_ENABLE;
2648 		E1000_WRITE_REG(&sc->hw, RXDCTL, reg);
2649 	}
2650 
2651 	/* Enable Receives */
2652 	E1000_WRITE_REG(&sc->hw, RCTL, reg_rctl);
2653 
2654 	/* Setup the HW Rx Head and Tail Descriptor Pointers */
2655 	E1000_WRITE_REG(&sc->hw, RDH, 0);
2656 	E1000_WRITE_REG(&sc->hw, RDT, sc->sc_rx_desc_head);
2657 }
2658 
2659 /*********************************************************************
2660  *
2661  *  Free receive related data structures.
2662  *
2663  **********************************************************************/
2664 void
2665 em_free_receive_structures(struct em_softc *sc)
2666 {
2667 	struct em_packet *pkt;
2668 	int i;
2669 
2670 	INIT_DEBUGOUT("free_receive_structures: begin");
2671 
2672 	if_rxr_init(&sc->sc_rx_ring, 0, 0);
2673 
2674 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rx_dma.dma_map,
2675 	    0, sc->sc_rx_dma.dma_map->dm_mapsize,
2676 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2677 
2678 	if (sc->sc_rx_pkts_ring != NULL) {
2679 		for (i = 0; i < sc->sc_rx_slots; i++) {
2680 			pkt = &sc->sc_rx_pkts_ring[i];
2681 			if (pkt->pkt_m != NULL) {
2682 				bus_dmamap_sync(sc->sc_dmat, pkt->pkt_map,
2683 				    0, pkt->pkt_map->dm_mapsize,
2684 				    BUS_DMASYNC_POSTREAD);
2685 				bus_dmamap_unload(sc->sc_dmat, pkt->pkt_map);
2686 				m_freem(pkt->pkt_m);
2687 				pkt->pkt_m = NULL;
2688 			}
2689 			bus_dmamap_destroy(sc->sc_dmat, pkt->pkt_map);
2690 		}
2691 
2692 		free(sc->sc_rx_pkts_ring, M_DEVBUF,
2693 		    sc->sc_rx_slots * sizeof(*sc->sc_rx_pkts_ring));
2694 		sc->sc_rx_pkts_ring = NULL;
2695 	}
2696 
2697 	if (sc->fmp != NULL) {
2698 		m_freem(sc->fmp);
2699 		sc->fmp = NULL;
2700 		sc->lmp = NULL;
2701 	}
2702 }
2703 
2704 int
2705 em_rxfill(struct em_softc *sc)
2706 {
2707 	u_int slots;
2708 	int post = 0;
2709 	int i;
2710 
2711 	i = sc->sc_rx_desc_head;
2712 
2713 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rx_dma.dma_map,
2714 	    0, sc->sc_rx_dma.dma_map->dm_mapsize,
2715 	    BUS_DMASYNC_POSTWRITE);
2716 
2717 	for (slots = if_rxr_get(&sc->sc_rx_ring, sc->sc_rx_slots);
2718 	    slots > 0; slots--) {
2719 		if (++i == sc->sc_rx_slots)
2720 			i = 0;
2721 
2722 		if (em_get_buf(sc, i) != 0)
2723 			break;
2724 
2725 		sc->sc_rx_desc_head = i;
2726 		post = 1;
2727 	}
2728 
2729 	if_rxr_put(&sc->sc_rx_ring, slots);
2730 
2731 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rx_dma.dma_map,
2732 	    0, sc->sc_rx_dma.dma_map->dm_mapsize,
2733 	    BUS_DMASYNC_PREWRITE);
2734 
2735 	return (post);
2736 }
2737 
2738 /*********************************************************************
2739  *
2740  *  This routine executes in interrupt context. It replenishes
2741  *  the mbufs in the descriptor and sends data which has been
2742  *  dma'ed into host memory to upper layer.
2743  *
2744  *********************************************************************/
2745 int
2746 em_rxeof(struct em_softc *sc)
2747 {
2748 	struct ifnet	    *ifp = &sc->sc_ac.ac_if;
2749 	struct mbuf_list    ml = MBUF_LIST_INITIALIZER();
2750 	struct mbuf	    *m;
2751 	u_int8_t	    accept_frame = 0;
2752 	u_int8_t	    eop = 0;
2753 	u_int16_t	    len, desc_len, prev_len_adj;
2754 	int		    i, rv = 0;
2755 
2756 	/* Pointer to the receive descriptor being examined. */
2757 	struct em_rx_desc   *desc;
2758 	struct em_packet    *pkt;
2759 	u_int8_t	    status;
2760 
2761 	if (if_rxr_inuse(&sc->sc_rx_ring) == 0)
2762 		return (0);
2763 
2764 	i = sc->sc_rx_desc_tail;
2765 
2766 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rx_dma.dma_map,
2767 	    0, sc->sc_rx_dma.dma_map->dm_mapsize,
2768 	    BUS_DMASYNC_POSTREAD);
2769 
2770 	do {
2771 		m = NULL;
2772 
2773 		pkt = &sc->sc_rx_pkts_ring[i];
2774 		desc = &sc->sc_rx_desc_ring[i];
2775 
2776 		status = desc->status;
2777 		if (!ISSET(status, E1000_RXD_STAT_DD))
2778 			break;
2779 
2780 		/* pull the mbuf off the ring */
2781 		bus_dmamap_sync(sc->sc_dmat, pkt->pkt_map,
2782 		    0, pkt->pkt_map->dm_mapsize,
2783 		    BUS_DMASYNC_POSTREAD);
2784 		bus_dmamap_unload(sc->sc_dmat, pkt->pkt_map);
2785 		m = pkt->pkt_m;
2786 		pkt->pkt_m = NULL;
2787 
2788 		KASSERT(m != NULL);
2789 
2790 		if_rxr_put(&sc->sc_rx_ring, 1);
2791 		rv = 1;
2792 
2793 		accept_frame = 1;
2794 		prev_len_adj = 0;
2795 		desc_len = letoh16(desc->length);
2796 
2797 		if (status & E1000_RXD_STAT_EOP) {
2798 			eop = 1;
2799 			if (desc_len < ETHER_CRC_LEN) {
2800 				len = 0;
2801 				prev_len_adj = ETHER_CRC_LEN - desc_len;
2802 			} else if (sc->hw.mac_type == em_i210 ||
2803 			    sc->hw.mac_type == em_i350)
2804 				len = desc_len;
2805 			else
2806 				len = desc_len - ETHER_CRC_LEN;
2807 		} else {
2808 			eop = 0;
2809 			len = desc_len;
2810 		}
2811 
2812 		if (desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK) {
2813 			u_int8_t last_byte;
2814 			u_int32_t pkt_len = desc_len;
2815 
2816 			if (sc->fmp != NULL)
2817 				pkt_len += sc->fmp->m_pkthdr.len;
2818 
2819 			last_byte = *(mtod(m, caddr_t) + desc_len - 1);
2820 			if (TBI_ACCEPT(&sc->hw, status, desc->errors,
2821 			    pkt_len, last_byte)) {
2822 #ifndef SMALL_KERNEL
2823 				em_tbi_adjust_stats(&sc->hw, &sc->stats,
2824 				    pkt_len, sc->hw.mac_addr);
2825 #endif
2826 				if (len > 0)
2827 					len--;
2828 			} else
2829 				accept_frame = 0;
2830 		}
2831 
2832 		if (accept_frame) {
2833 			/* Assign correct length to the current fragment */
2834 			m->m_len = len;
2835 
2836 			if (sc->fmp == NULL) {
2837 				m->m_pkthdr.len = m->m_len;
2838 				sc->fmp = m;	 /* Store the first mbuf */
2839 				sc->lmp = m;
2840 			} else {
2841 				/* Chain mbuf's together */
2842 				m->m_flags &= ~M_PKTHDR;
2843 				/*
2844 				 * Adjust length of previous mbuf in chain if
2845 				 * we received less than 4 bytes in the last
2846 				 * descriptor.
2847 				 */
2848 				if (prev_len_adj > 0) {
2849 					sc->lmp->m_len -= prev_len_adj;
2850 					sc->fmp->m_pkthdr.len -= prev_len_adj;
2851 				}
2852 				sc->lmp->m_next = m;
2853 				sc->lmp = m;
2854 				sc->fmp->m_pkthdr.len += m->m_len;
2855 			}
2856 
2857 			if (eop) {
2858 				m = sc->fmp;
2859 
2860 				em_receive_checksum(sc, desc, m);
2861 #if NVLAN > 0
2862 				if (desc->status & E1000_RXD_STAT_VP) {
2863 					m->m_pkthdr.ether_vtag =
2864 					    letoh16(desc->special);
2865 					m->m_flags |= M_VLANTAG;
2866 				}
2867 #endif
2868 				ml_enqueue(&ml, m);
2869 
2870 				sc->fmp = NULL;
2871 				sc->lmp = NULL;
2872 			}
2873 		} else {
2874 			sc->dropped_pkts++;
2875 
2876 			if (sc->fmp != NULL) {
2877 				m_freem(sc->fmp);
2878 				sc->fmp = NULL;
2879 				sc->lmp = NULL;
2880 			}
2881 
2882 			m_freem(m);
2883 		}
2884 
2885 		/* Advance our pointers to the next descriptor. */
2886 		if (++i == sc->sc_rx_slots)
2887 			i = 0;
2888 	} while (if_rxr_inuse(&sc->sc_rx_ring) > 0);
2889 
2890 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rx_dma.dma_map,
2891 	    0, sc->sc_rx_dma.dma_map->dm_mapsize,
2892 	    BUS_DMASYNC_PREREAD);
2893 
2894 	sc->sc_rx_desc_tail = i;
2895 
2896 	if_input(ifp, &ml);
2897 
2898 	return (rv);
2899 }
2900 
2901 /*********************************************************************
2902  *
2903  *  Verify that the hardware indicated that the checksum is valid.
2904  *  Inform the stack about the status of checksum so that stack
2905  *  doesn't spend time verifying the checksum.
2906  *
2907  *********************************************************************/
2908 void
2909 em_receive_checksum(struct em_softc *sc, struct em_rx_desc *rx_desc,
2910     struct mbuf *mp)
2911 {
2912 	/* 82543 or newer only */
2913 	if ((sc->hw.mac_type < em_82543) ||
2914 	    /* Ignore Checksum bit is set */
2915 	    (rx_desc->status & E1000_RXD_STAT_IXSM)) {
2916 		mp->m_pkthdr.csum_flags = 0;
2917 		return;
2918 	}
2919 
2920 	if (rx_desc->status & E1000_RXD_STAT_IPCS) {
2921 		/* Did it pass? */
2922 		if (!(rx_desc->errors & E1000_RXD_ERR_IPE)) {
2923 			/* IP Checksum Good */
2924 			mp->m_pkthdr.csum_flags = M_IPV4_CSUM_IN_OK;
2925 
2926 		} else
2927 			mp->m_pkthdr.csum_flags = 0;
2928 	}
2929 
2930 	if (rx_desc->status & E1000_RXD_STAT_TCPCS) {
2931 		/* Did it pass? */
2932 		if (!(rx_desc->errors & E1000_RXD_ERR_TCPE))
2933 			mp->m_pkthdr.csum_flags |=
2934 				M_TCP_CSUM_IN_OK | M_UDP_CSUM_IN_OK;
2935 	}
2936 }
2937 
2938 /*
2939  * This turns on the hardware offload of the VLAN
2940  * tag insertion and strip
2941  */
2942 void
2943 em_enable_hw_vlans(struct em_softc *sc)
2944 {
2945 	uint32_t ctrl;
2946 
2947 	ctrl = E1000_READ_REG(&sc->hw, CTRL);
2948 	ctrl |= E1000_CTRL_VME;
2949 	E1000_WRITE_REG(&sc->hw, CTRL, ctrl);
2950 }
2951 
2952 void
2953 em_enable_intr(struct em_softc *sc)
2954 {
2955 	E1000_WRITE_REG(&sc->hw, IMS, (IMS_ENABLE_MASK));
2956 }
2957 
2958 void
2959 em_disable_intr(struct em_softc *sc)
2960 {
2961 	/*
2962 	 * The first version of 82542 had an errata where when link
2963 	 * was forced it would stay up even if the cable was disconnected
2964 	 * Sequence errors were used to detect the disconnect and then
2965 	 * the driver would unforce the link.  This code is in the ISR.
2966 	 * For this to work correctly the Sequence error interrupt had
2967 	 * to be enabled all the time.
2968 	 */
2969 
2970 	if (sc->hw.mac_type == em_82542_rev2_0)
2971 		E1000_WRITE_REG(&sc->hw, IMC, (0xffffffff & ~E1000_IMC_RXSEQ));
2972 	else
2973 		E1000_WRITE_REG(&sc->hw, IMC, 0xffffffff);
2974 }
2975 
2976 void
2977 em_write_pci_cfg(struct em_hw *hw, uint32_t reg, uint16_t *value)
2978 {
2979 	struct pci_attach_args *pa = &((struct em_osdep *)hw->back)->em_pa;
2980 	pcireg_t val;
2981 
2982 	val = pci_conf_read(pa->pa_pc, pa->pa_tag, reg & ~0x3);
2983 	if (reg & 0x2) {
2984 		val &= 0x0000ffff;
2985 		val |= (*value << 16);
2986 	} else {
2987 		val &= 0xffff0000;
2988 		val |= *value;
2989 	}
2990 	pci_conf_write(pa->pa_pc, pa->pa_tag, reg & ~0x3, val);
2991 }
2992 
2993 void
2994 em_read_pci_cfg(struct em_hw *hw, uint32_t reg, uint16_t *value)
2995 {
2996 	struct pci_attach_args *pa = &((struct em_osdep *)hw->back)->em_pa;
2997 	pcireg_t val;
2998 
2999 	val = pci_conf_read(pa->pa_pc, pa->pa_tag, reg & ~0x3);
3000 	if (reg & 0x2)
3001 		*value = (val >> 16) & 0xffff;
3002 	else
3003 		*value = val & 0xffff;
3004 }
3005 
3006 void
3007 em_pci_set_mwi(struct em_hw *hw)
3008 {
3009 	struct pci_attach_args *pa = &((struct em_osdep *)hw->back)->em_pa;
3010 
3011 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
3012 		(hw->pci_cmd_word | CMD_MEM_WRT_INVALIDATE));
3013 }
3014 
3015 void
3016 em_pci_clear_mwi(struct em_hw *hw)
3017 {
3018 	struct pci_attach_args *pa = &((struct em_osdep *)hw->back)->em_pa;
3019 
3020 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
3021 		(hw->pci_cmd_word & ~CMD_MEM_WRT_INVALIDATE));
3022 }
3023 
3024 /*
3025  * We may eventually really do this, but its unnecessary
3026  * for now so we just return unsupported.
3027  */
3028 int32_t
3029 em_read_pcie_cap_reg(struct em_hw *hw, uint32_t reg, uint16_t *value)
3030 {
3031 	return -E1000_NOT_IMPLEMENTED;
3032 }
3033 
3034 /*********************************************************************
3035 * 82544 Coexistence issue workaround.
3036 *    There are 2 issues.
3037 *       1. Transmit Hang issue.
3038 *    To detect this issue, following equation can be used...
3039 *          SIZE[3:0] + ADDR[2:0] = SUM[3:0].
3040 *          If SUM[3:0] is in between 1 to 4, we will have this issue.
3041 *
3042 *       2. DAC issue.
3043 *    To detect this issue, following equation can be used...
3044 *          SIZE[3:0] + ADDR[2:0] = SUM[3:0].
3045 *          If SUM[3:0] is in between 9 to c, we will have this issue.
3046 *
3047 *
3048 *    WORKAROUND:
3049 *          Make sure we do not have ending address as 1,2,3,4(Hang) or 9,a,b,c (DAC)
3050 *
3051 *** *********************************************************************/
3052 u_int32_t
3053 em_fill_descriptors(u_int64_t address, u_int32_t length,
3054     PDESC_ARRAY desc_array)
3055 {
3056         /* Since issue is sensitive to length and address.*/
3057         /* Let us first check the address...*/
3058         u_int32_t safe_terminator;
3059         if (length <= 4) {
3060                 desc_array->descriptor[0].address = address;
3061                 desc_array->descriptor[0].length = length;
3062                 desc_array->elements = 1;
3063                 return desc_array->elements;
3064         }
3065         safe_terminator = (u_int32_t)((((u_int32_t)address & 0x7) + (length & 0xF)) & 0xF);
3066         /* if it does not fall between 0x1 to 0x4 and 0x9 to 0xC then return */
3067         if (safe_terminator == 0   ||
3068         (safe_terminator > 4   &&
3069         safe_terminator < 9)   ||
3070         (safe_terminator > 0xC &&
3071         safe_terminator <= 0xF)) {
3072                 desc_array->descriptor[0].address = address;
3073                 desc_array->descriptor[0].length = length;
3074                 desc_array->elements = 1;
3075                 return desc_array->elements;
3076         }
3077 
3078         desc_array->descriptor[0].address = address;
3079         desc_array->descriptor[0].length = length - 4;
3080         desc_array->descriptor[1].address = address + (length - 4);
3081         desc_array->descriptor[1].length = 4;
3082         desc_array->elements = 2;
3083         return desc_array->elements;
3084 }
3085 
3086 /*
3087  * Disable the L0S and L1 LINK states.
3088  */
3089 void
3090 em_disable_aspm(struct em_softc *sc)
3091 {
3092 	int offset;
3093 	pcireg_t val;
3094 
3095 	switch (sc->hw.mac_type) {
3096 		case em_82571:
3097 		case em_82572:
3098 		case em_82573:
3099 		case em_82574:
3100 			break;
3101 		default:
3102 			return;
3103 	}
3104 
3105 	if (!pci_get_capability(sc->osdep.em_pa.pa_pc, sc->osdep.em_pa.pa_tag,
3106 	    PCI_CAP_PCIEXPRESS, &offset, NULL))
3107 		return;
3108 
3109 	/* Disable PCIe Active State Power Management (ASPM). */
3110 	val = pci_conf_read(sc->osdep.em_pa.pa_pc, sc->osdep.em_pa.pa_tag,
3111 	    offset + PCI_PCIE_LCSR);
3112 
3113 	switch (sc->hw.mac_type) {
3114 		case em_82571:
3115 		case em_82572:
3116 			val &= ~PCI_PCIE_LCSR_ASPM_L1;
3117 			break;
3118 		case em_82573:
3119 		case em_82574:
3120 			val &= ~(PCI_PCIE_LCSR_ASPM_L0S |
3121 			    PCI_PCIE_LCSR_ASPM_L1);
3122 			break;
3123 		default:
3124 			break;
3125 	}
3126 
3127 	pci_conf_write(sc->osdep.em_pa.pa_pc, sc->osdep.em_pa.pa_tag,
3128 	    offset + PCI_PCIE_LCSR, val);
3129 }
3130 
3131 /*
3132  * em_flush_tx_ring - remove all descriptors from the tx_ring
3133  *
3134  * We want to clear all pending descriptors from the TX ring.
3135  * zeroing happens when the HW reads the regs. We assign the ring itself as
3136  * the data of the next descriptor. We don't care about the data we are about
3137  * to reset the HW.
3138  */
3139 void
3140 em_flush_tx_ring(struct em_softc *sc)
3141 {
3142 	uint32_t		 tctl, txd_lower = E1000_TXD_CMD_IFCS;
3143 	uint16_t		 size = 512;
3144 	struct em_tx_desc	*txd;
3145 
3146 	KASSERT(sc->sc_tx_desc_ring != NULL);
3147 
3148 	tctl = EM_READ_REG(&sc->hw, E1000_TCTL);
3149 	EM_WRITE_REG(&sc->hw, E1000_TCTL, tctl | E1000_TCTL_EN);
3150 
3151 	KASSERT(EM_READ_REG(&sc->hw, E1000_TDT) == sc->sc_tx_desc_head);
3152 
3153 	txd = &sc->sc_tx_desc_ring[sc->sc_tx_desc_head];
3154 	txd->buffer_addr = sc->sc_tx_dma.dma_map->dm_segs[0].ds_addr;
3155 	txd->lower.data = htole32(txd_lower | size);
3156 	txd->upper.data = 0;
3157 
3158 	/* flush descriptors to memory before notifying the HW */
3159 	bus_space_barrier(sc->osdep.mem_bus_space_tag,
3160 	    sc->osdep.mem_bus_space_handle, 0, 0, BUS_SPACE_BARRIER_WRITE);
3161 
3162 	if (++sc->sc_tx_desc_head == sc->sc_tx_slots)
3163 		sc->sc_tx_desc_head = 0;
3164 
3165 	EM_WRITE_REG(&sc->hw, E1000_TDT, sc->sc_tx_desc_head);
3166 	bus_space_barrier(sc->osdep.mem_bus_space_tag, sc->osdep.mem_bus_space_handle,
3167 	    0, 0, BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);
3168 	usec_delay(250);
3169 }
3170 
3171 /*
3172  * em_flush_rx_ring - remove all descriptors from the rx_ring
3173  *
3174  * Mark all descriptors in the RX ring as consumed and disable the rx ring
3175  */
3176 void
3177 em_flush_rx_ring(struct em_softc *sc)
3178 {
3179 	uint32_t	rctl, rxdctl;
3180 
3181 	rctl = EM_READ_REG(&sc->hw, E1000_RCTL);
3182 	EM_WRITE_REG(&sc->hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
3183 	E1000_WRITE_FLUSH(&sc->hw);
3184 	usec_delay(150);
3185 
3186 	rxdctl = EM_READ_REG(&sc->hw, E1000_RXDCTL);
3187 	/* zero the lower 14 bits (prefetch and host thresholds) */
3188 	rxdctl &= 0xffffc000;
3189 	/*
3190 	 * update thresholds: prefetch threshold to 31, host threshold to 1
3191 	 * and make sure the granularity is "descriptors" and not "cache lines"
3192 	 */
3193 	rxdctl |= (0x1F | (1 << 8) | E1000_RXDCTL_THRESH_UNIT_DESC);
3194 	EM_WRITE_REG(&sc->hw, E1000_RXDCTL, rxdctl);
3195 
3196 	/* momentarily enable the RX ring for the changes to take effect */
3197 	EM_WRITE_REG(&sc->hw, E1000_RCTL, rctl | E1000_RCTL_EN);
3198 	E1000_WRITE_FLUSH(&sc->hw);
3199 	usec_delay(150);
3200 	EM_WRITE_REG(&sc->hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
3201 }
3202 
3203 /*
3204  * em_flush_desc_rings - remove all descriptors from the descriptor rings
3205  *
3206  * In i219, the descriptor rings must be emptied before resetting the HW
3207  * or before changing the device state to D3 during runtime (runtime PM).
3208  *
3209  * Failure to do this will cause the HW to enter a unit hang state which can
3210  * only be released by PCI reset on the device
3211  *
3212  */
3213 void
3214 em_flush_desc_rings(struct em_softc *sc)
3215 {
3216 	struct pci_attach_args	*pa = &sc->osdep.em_pa;
3217 	uint32_t		 fextnvm11, tdlen;
3218 	uint16_t		 hang_state;
3219 
3220 	/* First, disable MULR fix in FEXTNVM11 */
3221 	fextnvm11 = EM_READ_REG(&sc->hw, E1000_FEXTNVM11);
3222 	fextnvm11 |= E1000_FEXTNVM11_DISABLE_MULR_FIX;
3223 	EM_WRITE_REG(&sc->hw, E1000_FEXTNVM11, fextnvm11);
3224 
3225 	/* do nothing if we're not in faulty state, or if the queue is empty */
3226 	tdlen = EM_READ_REG(&sc->hw, E1000_TDLEN);
3227 	hang_state = pci_conf_read(pa->pa_pc, pa->pa_tag, PCICFG_DESC_RING_STATUS);
3228 	if (!(hang_state & FLUSH_DESC_REQUIRED) || !tdlen)
3229 		return;
3230 	em_flush_tx_ring(sc);
3231 
3232 	/* recheck, maybe the fault is caused by the rx ring */
3233 	hang_state = pci_conf_read(pa->pa_pc, pa->pa_tag, PCICFG_DESC_RING_STATUS);
3234 	if (hang_state & FLUSH_DESC_REQUIRED)
3235 		em_flush_rx_ring(sc);
3236 }
3237 
3238 #ifndef SMALL_KERNEL
3239 /**********************************************************************
3240  *
3241  *  Update the board statistics counters.
3242  *
3243  **********************************************************************/
3244 void
3245 em_update_stats_counters(struct em_softc *sc)
3246 {
3247 	struct ifnet   *ifp = &sc->sc_ac.ac_if;
3248 
3249 	sc->stats.crcerrs += E1000_READ_REG(&sc->hw, CRCERRS);
3250 	sc->stats.mpc += E1000_READ_REG(&sc->hw, MPC);
3251 	sc->stats.ecol += E1000_READ_REG(&sc->hw, ECOL);
3252 
3253 	sc->stats.latecol += E1000_READ_REG(&sc->hw, LATECOL);
3254 	sc->stats.colc += E1000_READ_REG(&sc->hw, COLC);
3255 
3256 	sc->stats.ruc += E1000_READ_REG(&sc->hw, RUC);
3257 	sc->stats.roc += E1000_READ_REG(&sc->hw, ROC);
3258 
3259 	if (sc->hw.mac_type >= em_82543) {
3260 		sc->stats.algnerrc +=
3261 		E1000_READ_REG(&sc->hw, ALGNERRC);
3262 		sc->stats.rxerrc +=
3263 		E1000_READ_REG(&sc->hw, RXERRC);
3264 		sc->stats.cexterr +=
3265 		E1000_READ_REG(&sc->hw, CEXTERR);
3266 	}
3267 
3268 #ifdef EM_DEBUG
3269 	if (sc->hw.media_type == em_media_type_copper ||
3270 	    (E1000_READ_REG(&sc->hw, STATUS) & E1000_STATUS_LU)) {
3271 		sc->stats.symerrs += E1000_READ_REG(&sc->hw, SYMERRS);
3272 		sc->stats.sec += E1000_READ_REG(&sc->hw, SEC);
3273 	}
3274 	sc->stats.scc += E1000_READ_REG(&sc->hw, SCC);
3275 
3276 	sc->stats.mcc += E1000_READ_REG(&sc->hw, MCC);
3277 	sc->stats.dc += E1000_READ_REG(&sc->hw, DC);
3278 	sc->stats.rlec += E1000_READ_REG(&sc->hw, RLEC);
3279 	sc->stats.xonrxc += E1000_READ_REG(&sc->hw, XONRXC);
3280 	sc->stats.xontxc += E1000_READ_REG(&sc->hw, XONTXC);
3281 	sc->stats.xoffrxc += E1000_READ_REG(&sc->hw, XOFFRXC);
3282 	sc->stats.xofftxc += E1000_READ_REG(&sc->hw, XOFFTXC);
3283 	sc->stats.fcruc += E1000_READ_REG(&sc->hw, FCRUC);
3284 	sc->stats.prc64 += E1000_READ_REG(&sc->hw, PRC64);
3285 	sc->stats.prc127 += E1000_READ_REG(&sc->hw, PRC127);
3286 	sc->stats.prc255 += E1000_READ_REG(&sc->hw, PRC255);
3287 	sc->stats.prc511 += E1000_READ_REG(&sc->hw, PRC511);
3288 	sc->stats.prc1023 += E1000_READ_REG(&sc->hw, PRC1023);
3289 	sc->stats.prc1522 += E1000_READ_REG(&sc->hw, PRC1522);
3290 	sc->stats.gprc += E1000_READ_REG(&sc->hw, GPRC);
3291 	sc->stats.bprc += E1000_READ_REG(&sc->hw, BPRC);
3292 	sc->stats.mprc += E1000_READ_REG(&sc->hw, MPRC);
3293 	sc->stats.gptc += E1000_READ_REG(&sc->hw, GPTC);
3294 
3295 	/* For the 64-bit byte counters the low dword must be read first. */
3296 	/* Both registers clear on the read of the high dword */
3297 
3298 	sc->stats.gorcl += E1000_READ_REG(&sc->hw, GORCL);
3299 	sc->stats.gorch += E1000_READ_REG(&sc->hw, GORCH);
3300 	sc->stats.gotcl += E1000_READ_REG(&sc->hw, GOTCL);
3301 	sc->stats.gotch += E1000_READ_REG(&sc->hw, GOTCH);
3302 
3303 	sc->stats.rnbc += E1000_READ_REG(&sc->hw, RNBC);
3304 	sc->stats.rfc += E1000_READ_REG(&sc->hw, RFC);
3305 	sc->stats.rjc += E1000_READ_REG(&sc->hw, RJC);
3306 
3307 	sc->stats.torl += E1000_READ_REG(&sc->hw, TORL);
3308 	sc->stats.torh += E1000_READ_REG(&sc->hw, TORH);
3309 	sc->stats.totl += E1000_READ_REG(&sc->hw, TOTL);
3310 	sc->stats.toth += E1000_READ_REG(&sc->hw, TOTH);
3311 
3312 	sc->stats.tpr += E1000_READ_REG(&sc->hw, TPR);
3313 	sc->stats.tpt += E1000_READ_REG(&sc->hw, TPT);
3314 	sc->stats.ptc64 += E1000_READ_REG(&sc->hw, PTC64);
3315 	sc->stats.ptc127 += E1000_READ_REG(&sc->hw, PTC127);
3316 	sc->stats.ptc255 += E1000_READ_REG(&sc->hw, PTC255);
3317 	sc->stats.ptc511 += E1000_READ_REG(&sc->hw, PTC511);
3318 	sc->stats.ptc1023 += E1000_READ_REG(&sc->hw, PTC1023);
3319 	sc->stats.ptc1522 += E1000_READ_REG(&sc->hw, PTC1522);
3320 	sc->stats.mptc += E1000_READ_REG(&sc->hw, MPTC);
3321 	sc->stats.bptc += E1000_READ_REG(&sc->hw, BPTC);
3322 
3323 	if (sc->hw.mac_type >= em_82543) {
3324 		sc->stats.tncrs +=
3325 		E1000_READ_REG(&sc->hw, TNCRS);
3326 		sc->stats.tsctc +=
3327 		E1000_READ_REG(&sc->hw, TSCTC);
3328 		sc->stats.tsctfc +=
3329 		E1000_READ_REG(&sc->hw, TSCTFC);
3330 	}
3331 #endif
3332 
3333 	/* Fill out the OS statistics structure */
3334 	ifp->if_collisions = sc->stats.colc;
3335 
3336 	/* Rx Errors */
3337 	ifp->if_ierrors =
3338 	    sc->dropped_pkts +
3339 	    sc->stats.rxerrc +
3340 	    sc->stats.crcerrs +
3341 	    sc->stats.algnerrc +
3342 	    sc->stats.ruc + sc->stats.roc +
3343 	    sc->stats.mpc + sc->stats.cexterr +
3344 	    sc->rx_overruns;
3345 
3346 	/* Tx Errors */
3347 	ifp->if_oerrors = sc->stats.ecol + sc->stats.latecol +
3348 	    sc->watchdog_events;
3349 }
3350 
3351 #ifdef EM_DEBUG
3352 /**********************************************************************
3353  *
3354  *  This routine is called only when IFF_DEBUG is enabled.
3355  *  This routine provides a way to take a look at important statistics
3356  *  maintained by the driver and hardware.
3357  *
3358  **********************************************************************/
3359 void
3360 em_print_hw_stats(struct em_softc *sc)
3361 {
3362 	const char * const unit = DEVNAME(sc);
3363 
3364 	printf("%s: Excessive collisions = %lld\n", unit,
3365 		(long long)sc->stats.ecol);
3366 	printf("%s: Symbol errors = %lld\n", unit,
3367 		(long long)sc->stats.symerrs);
3368 	printf("%s: Sequence errors = %lld\n", unit,
3369 		(long long)sc->stats.sec);
3370 	printf("%s: Defer count = %lld\n", unit,
3371 		(long long)sc->stats.dc);
3372 
3373 	printf("%s: Missed Packets = %lld\n", unit,
3374 		(long long)sc->stats.mpc);
3375 	printf("%s: Receive No Buffers = %lld\n", unit,
3376 		(long long)sc->stats.rnbc);
3377 	/* RLEC is inaccurate on some hardware, calculate our own */
3378 	printf("%s: Receive Length Errors = %lld\n", unit,
3379 		((long long)sc->stats.roc +
3380 		(long long)sc->stats.ruc));
3381 	printf("%s: Receive errors = %lld\n", unit,
3382 		(long long)sc->stats.rxerrc);
3383 	printf("%s: Crc errors = %lld\n", unit,
3384 		(long long)sc->stats.crcerrs);
3385 	printf("%s: Alignment errors = %lld\n", unit,
3386 		(long long)sc->stats.algnerrc);
3387 	printf("%s: Carrier extension errors = %lld\n", unit,
3388 		(long long)sc->stats.cexterr);
3389 
3390 	printf("%s: RX overruns = %ld\n", unit,
3391 		sc->rx_overruns);
3392 	printf("%s: watchdog timeouts = %ld\n", unit,
3393 		sc->watchdog_events);
3394 
3395 	printf("%s: XON Rcvd = %lld\n", unit,
3396 		(long long)sc->stats.xonrxc);
3397 	printf("%s: XON Xmtd = %lld\n", unit,
3398 		(long long)sc->stats.xontxc);
3399 	printf("%s: XOFF Rcvd = %lld\n", unit,
3400 		(long long)sc->stats.xoffrxc);
3401 	printf("%s: XOFF Xmtd = %lld\n", unit,
3402 		(long long)sc->stats.xofftxc);
3403 
3404 	printf("%s: Good Packets Rcvd = %lld\n", unit,
3405 		(long long)sc->stats.gprc);
3406 	printf("%s: Good Packets Xmtd = %lld\n", unit,
3407 		(long long)sc->stats.gptc);
3408 }
3409 #endif
3410 #endif /* !SMALL_KERNEL */
3411