[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/em/if_em.c

Version: -  FREEBSD  -  FREEBSD7  -  FREEBSD70  -  FREEBSD6  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  linux-2.6  -  linux-2.4.22  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  OPENSOLARIS  -  minix-3-1-1  -  TRUSTEDBSD-SEBSD  -  TRUSTEDBSD-SEDARWIN  -  TRUSTEDBSD-SEDARWIN7 
Ident_Mode: -  plain  -  excerpts 

  1 /******************************************************************************
  2 
  3   Copyright (c) 2001-2008, 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 ******************************************************************************/
 32 ******************************************************************************/
 33 
 33 /*$FreeBSD: src/sys/dev/em/if_em.c,v 1.194 2008/05/21 21:06:17 jfv Exp $*/
 33 /*$FreeBSD: src/sys/dev/em/if_em.c,v 1.194 2008/05/21 21:06:17 jfv Exp $*/
 34 
 35 #ifdef HAVE_KERNEL_OPTION_HEADERS
 36 
 36 #include "opt_device_polling.h"
 37 
 37 #endif
 38 
 39 
 39 #include <sys/param.h>
 40 
 40 #include <sys/systm.h>
 41 
 41 #include <sys/bus.h>
 42 
 42 #include <sys/endian.h>
 43 
 43 #include <sys/kernel.h>
 44 
 44 #include <sys/kthread.h>
 45 
 45 #include <sys/malloc.h>
 46 
 46 #include <sys/mbuf.h>
 47 
 47 #include <sys/module.h>
 48 
 48 #include <sys/rman.h>
 49 
 49 #include <sys/socket.h>
 50 
 50 #include <sys/sockio.h>
 51 
 51 #include <sys/sysctl.h>
 52 
 52 #include <sys/taskqueue.h>
 53 
 53 #ifdef EM_TIMESYNC
 54 
 54 #include <sys/ioccom.h>
 55 
 55 #include <sys/time.h>
 56 
 56 #endif
 57 
 57 #include <machine/bus.h>
 58 
 58 #include <machine/resource.h>
 59 
 59 
 60 
 60 #include <net/bpf.h>
 61 
 61 #include <net/ethernet.h>
 62 
 62 #include <net/if.h>
 63 
 63 #include <net/if_arp.h>
 64 
 64 #include <net/if_dl.h>
 65 
 65 #include <net/if_media.h>
 66 
 66 
 67 
 67 #include <net/if_types.h>
 68 
 68 #include <net/if_vlan_var.h>
 69 
 69 
 70 
 70 #include <netinet/in_systm.h>
 71 
 71 #include <netinet/in.h>
 72 
 72 #include <netinet/if_ether.h>
 73 
 73 #include <netinet/ip.h>
 74 
 74 #include <netinet/ip6.h>
 75 
 75 #include <netinet/tcp.h>
 76 
 76 #include <netinet/udp.h>
 77 
 77 
 78 
 78 #include <machine/in_cksum.h>
 79 
 79 #include <dev/pci/pcivar.h>
 80 
 80 #include <dev/pci/pcireg.h>
 81 
 81 
 82 
 82 #include "e1000_api.h"
 83 
 83 #include "e1000_82571.h"
 84 
 84 #include "if_em.h"
 85 
 85 
 86 
 86 /*********************************************************************
 87  *  Set this to one to display debug statistics
 88  *********************************************************************/
 88  *********************************************************************/
 89 int     em_display_debug_stats = 0;
 90 
 91 
 91 /*********************************************************************
 92  *  Driver version:
 93  *********************************************************************/
 93  *********************************************************************/
 94 char em_driver_version[] = 
 94 char em_driver_version[] = "6.9.5"
 94 char em_driver_version[] = "6.9.5";
 95 
 96 
 97 
 97 /*********************************************************************
 98  *  PCI Device ID Table
 99  *
100  *  Used by probe to select devices to load on
101  *  Last field stores an index into e1000_strings
102  *  Last entry must be all 0s
103  *
104  *  { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
105  *********************************************************************/
105  *********************************************************************/
106 
107 static em_vendor_info_t em_vendor_info_array[] =
108 {
109         
109         /* Intel(R) PRO/1000 Network Connection */
109         /* Intel(R) PRO/1000 Network Connection */
110         { 0x8086, E1000_DEV_ID_82540EM,         PCI_ANY_ID, PCI_ANY_ID, 0},
111         { 0x8086, E1000_DEV_ID_82540EM_LOM,     PCI_ANY_ID, PCI_ANY_ID, 0},
112         { 0x8086, E1000_DEV_ID_82540EP,         PCI_ANY_ID, PCI_ANY_ID, 0},
113         { 0x8086, E1000_DEV_ID_82540EP_LOM,     PCI_ANY_ID, PCI_ANY_ID, 0},
114         { 0x8086, E1000_DEV_ID_82540EP_LP,      PCI_ANY_ID, PCI_ANY_ID, 0},
115 
116         { 0x8086, E1000_DEV_ID_82541EI,         PCI_ANY_ID, PCI_ANY_ID, 0},
117         { 0x8086, E1000_DEV_ID_82541ER,         PCI_ANY_ID, PCI_ANY_ID, 0},
118         { 0x8086, E1000_DEV_ID_82541ER_LOM,     PCI_ANY_ID, PCI_ANY_ID, 0},
119         { 0x8086, E1000_DEV_ID_82541EI_MOBILE,  PCI_ANY_ID, PCI_ANY_ID, 0},
120         { 0x8086, E1000_DEV_ID_82541GI,         PCI_ANY_ID, PCI_ANY_ID, 0},
121         { 0x8086, E1000_DEV_ID_82541GI_LF,      PCI_ANY_ID, PCI_ANY_ID, 0},
122         { 0x8086, E1000_DEV_ID_82541GI_MOBILE,  PCI_ANY_ID, PCI_ANY_ID, 0},
123 
124         { 0x8086, E1000_DEV_ID_82542,           PCI_ANY_ID, PCI_ANY_ID, 0},
125 
126         { 0x8086, E1000_DEV_ID_82543GC_FIBER,   PCI_ANY_ID, PCI_ANY_ID, 0},
127         { 0x8086, E1000_DEV_ID_82543GC_COPPER,  PCI_ANY_ID, PCI_ANY_ID, 0},
128 
129         { 0x8086, E1000_DEV_ID_82544EI_COPPER,  PCI_ANY_ID, PCI_ANY_ID, 0},
130         { 0x8086, E1000_DEV_ID_82544EI_FIBER,   PCI_ANY_ID, PCI_ANY_ID, 0},
131         { 0x8086, E1000_DEV_ID_82544GC_COPPER,  PCI_ANY_ID, PCI_ANY_ID, 0},
132         { 0x8086, E1000_DEV_ID_82544GC_LOM,     PCI_ANY_ID, PCI_ANY_ID, 0},
133 
134         { 0x8086, E1000_DEV_ID_82545EM_COPPER,  PCI_ANY_ID, PCI_ANY_ID, 0},
135         { 0x8086, E1000_DEV_ID_82545EM_FIBER,   PCI_ANY_ID, PCI_ANY_ID, 0},
136         { 0x8086, E1000_DEV_ID_82545GM_COPPER,  PCI_ANY_ID, PCI_ANY_ID, 0},
137         { 0x8086, E1000_DEV_ID_82545GM_FIBER,   PCI_ANY_ID, PCI_ANY_ID, 0},
138         { 0x8086, E1000_DEV_ID_82545GM_SERDES,  PCI_ANY_ID, PCI_ANY_ID, 0},
139 
140         { 0x8086, E1000_DEV_ID_82546EB_COPPER,  PCI_ANY_ID, PCI_ANY_ID, 0},
141         { 0x8086, E1000_DEV_ID_82546EB_FIBER,   PCI_ANY_ID, PCI_ANY_ID, 0},
142         { 0x8086, E1000_DEV_ID_82546EB_QUAD_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0},
143         { 0x8086, E1000_DEV_ID_82546GB_COPPER,  PCI_ANY_ID, PCI_ANY_ID, 0},
144         { 0x8086, E1000_DEV_ID_82546GB_FIBER,   PCI_ANY_ID, PCI_ANY_ID, 0},
145         { 0x8086, E1000_DEV_ID_82546GB_SERDES,  PCI_ANY_ID, PCI_ANY_ID, 0},
146         { 0x8086, E1000_DEV_ID_82546GB_PCIE,    PCI_ANY_ID, PCI_ANY_ID, 0},
147         { 0x8086, E1000_DEV_ID_82546GB_QUAD_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0},
148         { 0x8086, E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3,
149                                                 PCI_ANY_ID, PCI_ANY_ID, 0},
150 
151         { 0x8086, E1000_DEV_ID_82547EI,         PCI_ANY_ID, PCI_ANY_ID, 0},
152         { 0x8086, E1000_DEV_ID_82547EI_MOBILE,  PCI_ANY_ID, PCI_ANY_ID, 0},
153         { 0x8086, E1000_DEV_ID_82547GI,         PCI_ANY_ID, PCI_ANY_ID, 0},
154 
155         { 0x8086, E1000_DEV_ID_82571EB_COPPER,  PCI_ANY_ID, PCI_ANY_ID, 0},
156         { 0x8086, E1000_DEV_ID_82571EB_FIBER,   PCI_ANY_ID, PCI_ANY_ID, 0},
157         { 0x8086, E1000_DEV_ID_82571EB_SERDES,  PCI_ANY_ID, PCI_ANY_ID, 0},
158         { 0x8086, E1000_DEV_ID_82571EB_SERDES_DUAL,
159                                                 PCI_ANY_ID, PCI_ANY_ID, 0},
160         { 0x8086, E1000_DEV_ID_82571EB_SERDES_QUAD,
161                                                 PCI_ANY_ID, PCI_ANY_ID, 0},
162         { 0x8086, E1000_DEV_ID_82571EB_QUAD_COPPER,
163                                                 PCI_ANY_ID, PCI_ANY_ID, 0},
164         { 0x8086, E1000_DEV_ID_82571EB_QUAD_COPPER_LP,
165                                                 PCI_ANY_ID, PCI_ANY_ID, 0},
166         { 0x8086, E1000_DEV_ID_82571EB_QUAD_FIBER,
167                                                 PCI_ANY_ID, PCI_ANY_ID, 0},
168         { 0x8086, E1000_DEV_ID_82571PT_QUAD_COPPER,
169                                                 PCI_ANY_ID, PCI_ANY_ID, 0},
170         { 0x8086, E1000_DEV_ID_82572EI_COPPER,  PCI_ANY_ID, PCI_ANY_ID, 0},
171         { 0x8086, E1000_DEV_ID_82572EI_FIBER,   PCI_ANY_ID, PCI_ANY_ID, 0},
172         { 0x8086, E1000_DEV_ID_82572EI_SERDES,  PCI_ANY_ID, PCI_ANY_ID, 0},
173         { 0x8086, E1000_DEV_ID_82572EI,         PCI_ANY_ID, PCI_ANY_ID, 0},
174 
175         { 0x8086, E1000_DEV_ID_82573E,          PCI_ANY_ID, PCI_ANY_ID, 0},
176         { 0x8086, E1000_DEV_ID_82573E_IAMT,     PCI_ANY_ID, PCI_ANY_ID, 0},
177         { 0x8086, E1000_DEV_ID_82573L,          PCI_ANY_ID, PCI_ANY_ID, 0},
178         { 0x8086, E1000_DEV_ID_80003ES2LAN_COPPER_SPT,
179                                                 PCI_ANY_ID, PCI_ANY_ID, 0},
180         { 0x8086, E1000_DEV_ID_80003ES2LAN_SERDES_SPT,
181                                                 PCI_ANY_ID, PCI_ANY_ID, 0},
182         { 0x8086, E1000_DEV_ID_80003ES2LAN_COPPER_DPT,
183                                                 PCI_ANY_ID, PCI_ANY_ID, 0},
184         { 0x8086, E1000_DEV_ID_80003ES2LAN_SERDES_DPT,
185                                                 PCI_ANY_ID, PCI_ANY_ID, 0},
186         { 0x8086, E1000_DEV_ID_ICH8_IGP_M_AMT,  PCI_ANY_ID, PCI_ANY_ID, 0},
187         { 0x8086, E1000_DEV_ID_ICH8_IGP_AMT,    PCI_ANY_ID, PCI_ANY_ID, 0},
188         { 0x8086, E1000_DEV_ID_ICH8_IGP_C,      PCI_ANY_ID, PCI_ANY_ID, 0},
189         { 0x8086, E1000_DEV_ID_ICH8_IFE,        PCI_ANY_ID, PCI_ANY_ID, 0},
190         { 0x8086, E1000_DEV_ID_ICH8_IFE_GT,     PCI_ANY_ID, PCI_ANY_ID, 0},
191         { 0x8086, E1000_DEV_ID_ICH8_IFE_G,      PCI_ANY_ID, PCI_ANY_ID, 0},
192         { 0x8086, E1000_DEV_ID_ICH8_IGP_M,      PCI_ANY_ID, PCI_ANY_ID, 0},
193 
194         { 0x8086, E1000_DEV_ID_ICH9_IGP_M_AMT,  PCI_ANY_ID, PCI_ANY_ID, 0},
195         { 0x8086, E1000_DEV_ID_ICH9_IGP_AMT,    PCI_ANY_ID, PCI_ANY_ID, 0},
196         { 0x8086, E1000_DEV_ID_ICH9_IGP_C,      PCI_ANY_ID, PCI_ANY_ID, 0},
197         { 0x8086, E1000_DEV_ID_ICH9_IGP_M,      PCI_ANY_ID, PCI_ANY_ID, 0},
198         { 0x8086, E1000_DEV_ID_ICH9_IGP_M_V,    PCI_ANY_ID, PCI_ANY_ID, 0},
199         { 0x8086, E1000_DEV_ID_ICH9_IFE,        PCI_ANY_ID, PCI_ANY_ID, 0},
200         { 0x8086, E1000_DEV_ID_ICH9_IFE_GT,     PCI_ANY_ID, PCI_ANY_ID, 0},
201         { 0x8086, E1000_DEV_ID_ICH9_IFE_G,      PCI_ANY_ID, PCI_ANY_ID, 0},
202         { 0x8086, E1000_DEV_ID_ICH9_BM,         PCI_ANY_ID, PCI_ANY_ID, 0},
203         { 0x8086, E1000_DEV_ID_82574L,          PCI_ANY_ID, PCI_ANY_ID, 0},
204         { 0x8086, E1000_DEV_ID_ICH10_R_BM_LM,   PCI_ANY_ID, PCI_ANY_ID, 0},
205         { 0x8086, E1000_DEV_ID_ICH10_R_BM_LF,   PCI_ANY_ID, PCI_ANY_ID, 0},
206         { 0x8086, E1000_DEV_ID_ICH10_R_BM_V,    PCI_ANY_ID, PCI_ANY_ID, 0},
207         { 0x8086, E1000_DEV_ID_ICH10_D_BM_LM,   PCI_ANY_ID, PCI_ANY_ID, 0},
208         { 0x8086, E1000_DEV_ID_ICH10_D_BM_LF,   PCI_ANY_ID, PCI_ANY_ID, 0},
209         
209         /* required last entry */
209         /* required last entry */
210         { 0, 0, 0, 0, 0}
211 };
212 
213 
213 /*********************************************************************
214  *  Table of branding strings for all supported NICs.
215  *********************************************************************/
215  *********************************************************************/
216 
217 static char *em_strings[] = {
218         
218         "Intel(R) PRO/1000 Network Connection"
218         "Intel(R) PRO/1000 Network Connection"
219 };
220 
221 
221 /*********************************************************************
222  *  Function prototypes
223  *********************************************************************/
223  *********************************************************************/
224 static int      em_probe(device_t);
225 static int      em_attach(device_t);
226 static int      em_detach(device_t);
227 static int      em_shutdown(device_t);
228 static int      em_suspend(device_t);
229 static int      em_resume(device_t);
230 static void     em_start(struct ifnet *);
231 static void     em_start_locked(struct ifnet *ifp);
232 static int      em_ioctl(struct ifnet *, u_long, caddr_t);
233 static void     em_watchdog(struct adapter *);
234 static void     em_init(void *);
235 static void     em_init_locked(struct adapter *);
236 static void     em_stop(void *);
237 static void     em_media_status(struct ifnet *, struct ifmediareq *);
238 static int      em_media_change(struct ifnet *);
239 static void     em_identify_hardware(struct adapter *);
240 static int      em_allocate_pci_resources(struct adapter *);
241 static int      em_allocate_legacy(struct adapter *adapter);
242 static int      em_allocate_msix(struct adapter *adapter);
243 static int      em_setup_msix(struct adapter *);
244 static void     em_free_pci_resources(struct adapter *);
245 static void     em_local_timer(void *);
246 static int      em_hardware_init(struct adapter *);
247 static void     em_setup_interface(device_t, struct adapter *);
248 static void     em_setup_transmit_structures(struct adapter *);
249 static void     em_initialize_transmit_unit(struct adapter *);
250 static int      em_setup_receive_structures(struct adapter *);
251 static void     em_initialize_receive_unit(struct adapter *);
252 static void     em_enable_intr(struct adapter *);
253 static void     em_disable_intr(struct adapter *);
254 static void     em_free_transmit_structures(struct adapter *);
255 static void     em_free_receive_structures(struct adapter *);
256 static void     em_update_stats_counters(struct adapter *);
257 static void     em_txeof(struct adapter *);
258 static void     em_tx_purge(struct adapter *);
259 static int      em_allocate_receive_structures(struct adapter *);
260 static int      em_allocate_transmit_structures(struct adapter *);
261 static int      em_rxeof(struct adapter *, int);
262 #ifndef __NO_STRICT_ALIGNMENT
263 static int      em_fixup_rx(struct adapter *);
264 #endif
265 static void     em_receive_checksum(struct adapter *, struct e1000_rx_desc *,
266                     struct mbuf *);
267 static void     em_transmit_checksum_setup(struct adapter *, struct mbuf *,
268                     u32 *, u32 *);
269 #if __FreeBSD_version >= 700000
270 static bool     em_tso_setup(struct adapter *, struct mbuf *,
271                     u32 *, u32 *);
272 #endif 
272 #endif /* FreeBSD_version >= 700000 */
272 #endif /* FreeBSD_version >= 700000 */
273 static void     em_set_promisc(struct adapter *);
274 static void     em_disable_promisc(struct adapter *);
275 static void     em_set_multi(struct adapter *);
276 static void     em_print_hw_stats(struct adapter *);
277 static void     em_update_link_status(struct adapter *);
278 static int      em_get_buf(struct adapter *, int);
279 static void     em_enable_hw_vlans(struct adapter *);
280 static int      em_xmit(struct adapter *, struct mbuf **);
281 static void     em_smartspeed(struct adapter *);
282 static int      em_82547_fifo_workaround(struct adapter *, int);
283 static void     em_82547_update_fifo_head(struct adapter *, int);
284 static int      em_82547_tx_fifo_reset(struct adapter *);
285 static void     em_82547_move_tail(void *);
286 static int      em_dma_malloc(struct adapter *, bus_size_t,
287                     struct em_dma_alloc *, int);
288 static void     em_dma_free(struct adapter *, struct em_dma_alloc *);
289 static void     em_print_debug_info(struct adapter *);
290 static void     em_print_nvm_info(struct adapter *);
291 static int      em_is_valid_ether_addr(u8 *);
292 static int      em_sysctl_stats(SYSCTL_HANDLER_ARGS);
293 static int      em_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
294 static u32      em_fill_descriptors (bus_addr_t address, u32 length,
295                     PDESC_ARRAY desc_array);
296 static int      em_sysctl_int_delay(SYSCTL_HANDLER_ARGS);
297 static void     em_add_int_delay_sysctl(struct adapter *, const char *,
298                     const char *, struct em_int_delay_info *, int, int);
299 
299 /* Management and WOL Support */
299 /* Management and WOL Support */
300 static void     em_init_manageability(struct adapter *);
301 static void     em_release_manageability(struct adapter *);
302 static void     em_get_hw_control(struct adapter *);
303 static void     em_release_hw_control(struct adapter *);
304 static void     em_enable_wakeup(device_t);
305 
306 #ifdef EM_TIMESYNC
307 
307 /* Precision Time sync support */
307 /* Precision Time sync support */
308 static int      em_tsync_init(struct adapter *);
309 static void     em_tsync_disable(struct adapter *);
310 #endif
311 
312 #ifdef EM_LEGACY_IRQ
313 static void     em_intr(void *);
314 #else 
314 #else /* FAST IRQ */
314 #else /* FAST IRQ */
315 #if __FreeBSD_version < 700000
316 static void     em_irq_fast(void *);
317 #else
318 static int      em_irq_fast(void *);
319 #endif
320 
320 /* MSIX handlers */
320 /* MSIX handlers */
321 static void     em_msix_tx(void *);
322 static void     em_msix_rx(void *);
323 static void     em_msix_link(void *);
324 static void     em_add_rx_process_limit(struct adapter *, const char *,
325                     const char *, int *, int);
326 static void     em_handle_rxtx(void *context, int pending);
327 static void     em_handle_rx(void *context, int pending);
328 static void     em_handle_tx(void *context, int pending);
329 static void     em_handle_link(void *context, int pending);
330 #endif 
330 #endif /* EM_LEGACY_IRQ */
330 #endif /* EM_LEGACY_IRQ */
331 
332 #ifdef DEVICE_POLLING
333 static poll_handler_t em_poll;
334 #endif 
334 #endif /* POLLING */
334 #endif /* POLLING */
335 
336 
336 /*********************************************************************
337  *  FreeBSD Device Interface Entry Points
338  *********************************************************************/
338  *********************************************************************/
339 
340 static device_method_t em_methods[] = {
341         
341         /* Device interface */
341         /* Device interface */
342         DEVMETHOD(device_probe, em_probe),
343         DEVMETHOD(device_attach, em_attach),
344         DEVMETHOD(device_detach, em_detach),
345         DEVMETHOD(device_shutdown, em_shutdown),
346         DEVMETHOD(device_suspend, em_suspend),
347         DEVMETHOD(device_resume, em_resume),
348         {0, 0}
349 };
350 
351 static driver_t em_driver = {
352         
352         "em"
352         "em", em_methods, sizeof(struct adapter),
353 };
354 
355 static devclass_t em_devclass;
356 DRIVER_MODULE(em, pci, em_driver, em_devclass, 0, 0);
357 MODULE_DEPEND(em, pci, 1, 1, 1);
358 MODULE_DEPEND(em, ether, 1, 1, 1);
359 
360 
360 /*********************************************************************
361  *  Tunable default values.
362  *********************************************************************/
362  *********************************************************************/
363 
364 #define EM_TICKS_TO_USECS(ticks)        ((1024 * (ticks) + 500) / 1000)
365 #define EM_USECS_TO_TICKS(usecs)        ((1000 * (usecs) + 512) / 1024)
366 #define M_TSO_LEN                       66
367 
368 
368 /* Allow common code without TSO */
368 /* Allow common code without TSO */
369 #ifndef CSUM_TSO
370 #define CSUM_TSO        0
371 #endif
372 
373 static int em_tx_int_delay_dflt = EM_TICKS_TO_USECS(EM_TIDV);
374 static int em_rx_int_delay_dflt = EM_TICKS_TO_USECS(EM_RDTR);
375 static int em_tx_abs_int_delay_dflt = EM_TICKS_TO_USECS(EM_TADV);
376 static int em_rx_abs_int_delay_dflt = EM_TICKS_TO_USECS(EM_RADV);
377 static int em_rxd = EM_DEFAULT_RXD;
378 static int em_txd = EM_DEFAULT_TXD;
379 static int em_smart_pwr_down = FALSE;
380 
380 /* Controls whether promiscuous also shows bad packets */
380 /* Controls whether promiscuous also shows bad packets */
381 static int em_debug_sbp = FALSE;
382 
382 /* Local switch for MSI/MSIX */
382 /* Local switch for MSI/MSIX */
383 static int em_enable_msi = TRUE;
384 
385 TUNABLE_INT(
385 TUNABLE_INT("hw.em.tx_int_delay"
385 TUNABLE_INT("hw.em.tx_int_delay", &em_tx_int_delay_dflt);
386 TUNABLE_INT(
386 TUNABLE_INT("hw.em.rx_int_delay"
386 TUNABLE_INT("hw.em.rx_int_delay", &em_rx_int_delay_dflt);
387 TUNABLE_INT(
387 TUNABLE_INT("hw.em.tx_abs_int_delay"
387 TUNABLE_INT("hw.em.tx_abs_int_delay", &em_tx_abs_int_delay_dflt);
388 TUNABLE_INT(
388 TUNABLE_INT("hw.em.rx_abs_int_delay"
388 TUNABLE_INT("hw.em.rx_abs_int_delay", &em_rx_abs_int_delay_dflt);
389 TUNABLE_INT(
389 TUNABLE_INT("hw.em.rxd"
389 TUNABLE_INT("hw.em.rxd", &em_rxd);
390 TUNABLE_INT(
390 TUNABLE_INT("hw.em.txd"
390 TUNABLE_INT("hw.em.txd", &em_txd);
391 TUNABLE_INT(
391 TUNABLE_INT("hw.em.smart_pwr_down"
391 TUNABLE_INT("hw.em.smart_pwr_down", &em_smart_pwr_down);
392 TUNABLE_INT(
392 TUNABLE_INT("hw.em.sbp"
392 TUNABLE_INT("hw.em.sbp", &em_debug_sbp);
393 TUNABLE_INT(
393 TUNABLE_INT("hw.em.enable_msi"
393 TUNABLE_INT("hw.em.enable_msi", &em_enable_msi);
394 
395 #ifndef EM_LEGACY_IRQ
396 
396 /* How many packets rxeof tries to clean at a time */
396 /* How many packets rxeof tries to clean at a time */
397 static int em_rx_process_limit = 100;
398 TUNABLE_INT(
398 TUNABLE_INT("hw.em.rx_process_limit"
398 TUNABLE_INT("hw.em.rx_process_limit", &em_rx_process_limit);
399 #endif
400 
401 
401 /* Global used in WOL setup with multiport cards */
401 /* Global used in WOL setup with multiport cards */
402 static int global_quad_port_a = 0;
403 
404 
404 /*********************************************************************
405  *  Device identification routine
406  *
407  *  em_probe determines if the driver should be loaded on
408  *  adapter based on PCI vendor/device id of the adapter.
409  *
410  *  return BUS_PROBE_DEFAULT on success, positive on failure
411  *********************************************************************/
411  *********************************************************************/
412 
413 static int
414 em_probe(device_t dev)
415 {
416         char            adapter_name[60];
417         u16             pci_vendor_id = 0;
418         u16             pci_device_id = 0;
419         u16             pci_subvendor_id = 0;
420         u16             pci_subdevice_id = 0;
421         em_vendor_info_t *ent;
422 
423         INIT_DEBUGOUT(
423         INIT_DEBUGOUT("em_probe: begin"
423         INIT_DEBUGOUT("em_probe: begin");
424 
425         pci_vendor_id = pci_get_vendor(dev);
426         if (pci_vendor_id != EM_VENDOR_ID)
427                 return (ENXIO);
428 
429         pci_device_id = pci_get_device(dev);
430         pci_subvendor_id = pci_get_subvendor(dev);
431         pci_subdevice_id = pci_get_subdevice(dev);
432 
433         ent = em_vendor_info_array;
434         while (ent->vendor_id != 0) {
435                 if ((pci_vendor_id == ent->vendor_id) &&
436                     (pci_device_id == ent->device_id) &&
437 
438                     ((pci_subvendor_id == ent->subvendor_id) ||
439                     (ent->subvendor_id == PCI_ANY_ID)) &&
440 
441                     ((pci_subdevice_id == ent->subdevice_id) ||
442                     (ent->subdevice_id == PCI_ANY_ID))) {
443                         sprintf(adapter_name, 
443                         sprintf(adapter_name, "%s %s"
443                         sprintf(adapter_name, "%s %s",
444                                 em_strings[ent->index],
445                                 em_driver_version);
446                         device_set_desc_copy(dev, adapter_name);
447                         return (BUS_PROBE_DEFAULT);
448                 }
449                 ent++;
450         }
451 
452         return (ENXIO);
453 }
454 
455 
455 /*********************************************************************
456  *  Device initialization routine
457  *
458  *  The attach entry point is called when the driver is being loaded.
459  *  This routine identifies the type of hardware, allocates all resources
460  *  and initializes the hardware.
461  *
462  *  return 0 on success, positive on failure
463  *********************************************************************/
463  *********************************************************************/
464 
465 static int
466 em_attach(device_t dev)
467 {
468         struct adapter  *adapter;
469         int             tsize, rsize;
470         int             error = 0;
471         u16             eeprom_data, device_id;
472 
473         INIT_DEBUGOUT(
473         INIT_DEBUGOUT("em_attach: begin"
473         INIT_DEBUGOUT("em_attach: begin");
474 
475         adapter = device_get_softc(dev);
476         adapter->dev = adapter->osdep.dev = dev;
477         EM_CORE_LOCK_INIT(adapter, device_get_nameunit(dev));
478         EM_TX_LOCK_INIT(adapter, device_get_nameunit(dev));
479         EM_RX_LOCK_INIT(adapter, device_get_nameunit(dev));
480 
481         
481         /* SYSCTL stuff */
481         /* SYSCTL stuff */
482         SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
483             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
484             OID_AUTO, 
484             OID_AUTO, "debug"
484             OID_AUTO, "debug", CTLTYPE_INT|CTLFLAG_RW, adapter, 0,
485             em_sysctl_debug_info, 
485             em_sysctl_debug_info, "I"
485             em_sysctl_debug_info, "I", 
485             em_sysctl_debug_info, "I", "Debug Information"
485             em_sysctl_debug_info, "I", "Debug Information");
486 
487         SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
488             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
489             OID_AUTO, 
489             OID_AUTO, "stats"
489             OID_AUTO, "stats", CTLTYPE_INT|CTLFLAG_RW, adapter, 0,
490             em_sysctl_stats, 
490             em_sysctl_stats, "I"
490             em_sysctl_stats, "I", 
490             em_sysctl_stats, "I", "Statistics"
490             em_sysctl_stats, "I", "Statistics");
491 
492         callout_init_mtx(&adapter->timer, &adapter->core_mtx, 0);
493         callout_init_mtx(&adapter->tx_fifo_timer, &adapter->tx_mtx, 0);
494 
495         
495         /* Determine hardware and mac info */
495         /* Determine hardware and mac info */
496         em_identify_hardware(adapter);
497 
498         
498         /* Setup PCI resources */
498         /* Setup PCI resources */
499         if (em_allocate_pci_resources(adapter)) {
500                 device_printf(dev, 
500                 device_printf(dev, "Allocation of PCI resources failed\n"
500                 device_printf(dev, "Allocation of PCI resources failed\n");
501                 error = ENXIO;
502                 goto err_pci;
503         }
504 
505         
505         /*
506         ** For ICH8 and family we need to
507         ** map the flash memory, and this
508         ** must happen after the MAC is 
509         ** identified
510         */
510         */
511         if ((adapter->hw.mac.type == e1000_ich8lan) ||
512             (adapter->hw.mac.type == e1000_ich10lan) ||
513             (adapter->hw.mac.type == e1000_ich9lan)) {
514                 int rid = EM_BAR_TYPE_FLASH;
515                 adapter->flash = bus_alloc_resource_any(dev,
516                     SYS_RES_MEMORY, &rid, RF_ACTIVE);
517                 if (adapter->flash == NULL) {
518                         device_printf(dev, 
518                         device_printf(dev, "Mapping of Flash failed\n"
518                         device_printf(dev, "Mapping of Flash failed\n");
519                         error = ENXIO;
520                         goto err_pci;
521                 }
522                 
522                 /* This is used in the shared code */
522                 /* This is used in the shared code */
523                 adapter->hw.flash_address = (u8 *)adapter->flash;
524                 adapter->osdep.flash_bus_space_tag =
525                     rman_get_bustag(adapter->flash);
526                 adapter->osdep.flash_bus_space_handle =
527                     rman_get_bushandle(adapter->flash);
528         }
529 
530         
530         /* Do Shared Code initialization */
530         /* Do Shared Code initialization */
531         if (e1000_setup_init_funcs(&adapter->hw, TRUE)) {
532                 device_printf(dev, 
532                 device_printf(dev, "Setup of Shared code failed\n"
532                 device_printf(dev, "Setup of Shared code failed\n");
533                 error = ENXIO;
534                 goto err_pci;
535         }
536 
537         e1000_get_bus_info(&adapter->hw);
538 
539         
539         /* Set up some sysctls for the tunable interrupt delays */
539         /* Set up some sysctls for the tunable interrupt delays */
540         em_add_int_delay_sysctl(adapter, 
540         em_add_int_delay_sysctl(adapter, "rx_int_delay"
540         em_add_int_delay_sysctl(adapter, "rx_int_delay",
541             
541             "receive interrupt delay in usecs"
541             "receive interrupt delay in usecs", &adapter->rx_int_delay,
542             E1000_REGISTER(&adapter->hw, E1000_RDTR), em_rx_int_delay_dflt);
543         em_add_int_delay_sysctl(adapter, 
543         em_add_int_delay_sysctl(adapter, "tx_int_delay"
543         em_add_int_delay_sysctl(adapter, "tx_int_delay",
544             
544             "transmit interrupt delay in usecs"
544             "transmit interrupt delay in usecs", &adapter->tx_int_delay,
545             E1000_REGISTER(&adapter->hw, E1000_TIDV), em_tx_int_delay_dflt);
546         if (adapter->hw.mac.type >= e1000_82540) {
547                 em_add_int_delay_sysctl(adapter, 
547                 em_add_int_delay_sysctl(adapter, "rx_abs_int_delay"
547                 em_add_int_delay_sysctl(adapter, "rx_abs_int_delay",
548                     
548                     "receive interrupt delay limit in usecs"
548                     "receive interrupt delay limit in usecs",
549                     &adapter->rx_abs_int_delay,
550                     E1000_REGISTER(&adapter->hw, E1000_RADV),
551                     em_rx_abs_int_delay_dflt);
552                 em_add_int_delay_sysctl(adapter, 
552                 em_add_int_delay_sysctl(adapter, "tx_abs_int_delay"
552                 em_add_int_delay_sysctl(adapter, "tx_abs_int_delay",
553                     
553                     "transmit interrupt delay limit in usecs"
553                     "transmit interrupt delay limit in usecs",
554                     &adapter->tx_abs_int_delay,
555                     E1000_REGISTER(&adapter->hw, E1000_TADV),
556                     em_tx_abs_int_delay_dflt);
557         }
558 
559 #ifndef EM_LEGACY_IRQ
560         
560         /* Sysctls for limiting the amount of work done in the taskqueue */
560         /* Sysctls for limiting the amount of work done in the taskqueue */
561         em_add_rx_process_limit(adapter, 
561         em_add_rx_process_limit(adapter, "rx_processing_limit"
561         em_add_rx_process_limit(adapter, "rx_processing_limit",
562             
562             "max number of rx packets to process"
562             "max number of rx packets to process", &adapter->rx_process_limit,
563             em_rx_process_limit);
564 #endif
565 
566         
566         /*
567          * Validate number of transmit and receive descriptors. It
568          * must not exceed hardware maximum, and must be multiple
569          * of E1000_DBA_ALIGN.
570          */
570          */
571         if (((em_txd * sizeof(struct e1000_tx_desc)) % EM_DBA_ALIGN) != 0 ||
572             (adapter->hw.mac.type >= e1000_82544 && em_txd > EM_MAX_TXD) ||
573             (adapter->hw.mac.type < e1000_82544 && em_txd > EM_MAX_TXD_82543) ||
574             (em_txd < EM_MIN_TXD)) {
575                 device_printf(dev, 
575                 device_printf(dev, "Using %d TX descriptors instead of %d!\n"
575                 device_printf(dev, "Using %d TX descriptors instead of %d!\n",
576                     EM_DEFAULT_TXD, em_txd);
577                 adapter->num_tx_desc = EM_DEFAULT_TXD;
578         } else
579                 adapter->num_tx_desc = em_txd;
580         if (((em_rxd * sizeof(struct e1000_rx_desc)) % EM_DBA_ALIGN) != 0 ||
581             (adapter->hw.mac.type >= e1000_82544 && em_rxd > EM_MAX_RXD) ||
582             (adapter->hw.mac.type < e1000_82544 && em_rxd > EM_MAX_RXD_82543) ||
583             (em_rxd < EM_MIN_RXD)) {
584                 device_printf(dev, 
584                 device_printf(dev, "Using %d RX descriptors instead of %d!\n"
584                 device_printf(dev, "Using %d RX descriptors instead of %d!\n",
585                     EM_DEFAULT_RXD, em_rxd);
586                 adapter->num_rx_desc = EM_DEFAULT_RXD;
587         } else
588                 adapter->num_rx_desc = em_rxd;
589 
590         adapter->hw.mac.autoneg = DO_AUTO_NEG;
591         adapter->hw.phy.autoneg_wait_to_complete = FALSE;
592         adapter->hw.phy.autoneg_advertised = AUTONEG_ADV_DEFAULT;
593         adapter->rx_buffer_len = 2048;
594 
595         e1000_init_script_state_82541(&adapter->hw, TRUE);
596         e1000_set_tbi_compatibility_82543(&adapter->hw, TRUE);
597 
598         
598         /* Copper options */
598         /* Copper options */
599         if (adapter->hw.phy.media_type == e1000_media_type_copper) {
600                 adapter->hw.phy.mdix = AUTO_ALL_MODES;
601                 adapter->hw.phy.disable_polarity_correction = FALSE;
602                 adapter->hw.phy.ms_type = EM_MASTER_SLAVE;
603         }
604 
605         
605         /*
606          * Set the frame limits assuming
607          * standard ethernet sized frames.
608          */
608          */
609         adapter->max_frame_size = ETHERMTU + ETHER_HDR_LEN + ETHERNET_FCS_SIZE;
610         adapter->min_frame_size = ETH_ZLEN + ETHERNET_FCS_SIZE;
611 
612         
612         /*
613          * This controls when hardware reports transmit completion
614          * status.
615          */
615          */
616         adapter->hw.mac.report_tx_early = 1;
617 
618         tsize = roundup2(adapter->num_tx_desc * sizeof(struct e1000_tx_desc),
619             EM_DBA_ALIGN);
620 
621         
621         /* Allocate Transmit Descriptor ring */
621         /* Allocate Transmit Descriptor ring */
622         if (em_dma_malloc(adapter, tsize, &adapter->txdma, BUS_DMA_NOWAIT)) {
623                 device_printf(dev, 
623                 device_printf(dev, "Unable to allocate tx_desc memory\n"
623                 device_printf(dev, "Unable to allocate tx_desc memory\n");
624                 error = ENOMEM;
625                 goto err_tx_desc;
626         }
627         adapter->tx_desc_base = 
628             (struct e1000_tx_desc *)adapter->txdma.dma_vaddr;
629 
630         rsize = roundup2(adapter->num_rx_desc * sizeof(struct e1000_rx_desc),
631             EM_DBA_ALIGN);
632 
633         
633         /* Allocate Receive Descriptor ring */
633         /* Allocate Receive Descriptor ring */
634         if (em_dma_malloc(adapter, rsize, &adapter->rxdma, BUS_DMA_NOWAIT)) {
635                 device_printf(dev, 
635                 device_printf(dev, "Unable to allocate rx_desc memory\n"
635                 device_printf(dev, "Unable to allocate rx_desc memory\n");
636                 error = ENOMEM;
637                 goto err_rx_desc;
638         }
639         adapter->rx_desc_base =
640             (struct e1000_rx_desc *)adapter->rxdma.dma_vaddr;
641 
642         
642         /* Make sure we have a good EEPROM before we read from it */
642         /* Make sure we have a good EEPROM before we read from it */
643         if (e1000_validate_nvm_checksum(&adapter->hw) < 0) {
644                 
644                 /*
645                 ** Some PCI-E parts fail the first check due to
646                 ** the link being in sleep state, call it again,
647                 ** if it fails a second time its a real issue.
648                 */
648                 */
649                 if (e1000_validate_nvm_checksum(&adapter->hw) < 0) {
650                         device_printf(dev,
651                             
651                             "The EEPROM Checksum Is Not Valid\n"
651                             "The EEPROM Checksum Is Not Valid\n");
652                         error = EIO;
653                         goto err_hw_init;
654                 }
655         }
656 
657         
657         /* Initialize the hardware */
657         /* Initialize the hardware */
658         if (em_hardware_init(adapter)) {
659                 device_printf(dev, 
659                 device_printf(dev, "Unable to initialize the hardware\n"
659                 device_printf(dev, "Unable to initialize the hardware\n");
660                 error = EIO;
661                 goto err_hw_init;
662         }
663 
664         
664         /* Copy the permanent MAC address out of the EEPROM */
664         /* Copy the permanent MAC address out of the EEPROM */
665         if (e1000_read_mac_addr(&adapter->hw) < 0) {
666                 device_printf(dev, 
666                 device_printf(dev, "EEPROM read error while reading MAC"
666                 device_printf(dev, "EEPROM read error while reading MAC"
667                     
667                     " address\n"
667                     " address\n");
668                 error = EIO;
669                 goto err_hw_init;
670         }
671 
672         if (!em_is_valid_ether_addr(adapter->hw.mac.addr)) {
673                 device_printf(dev, 
673                 device_printf(dev, "Invalid MAC address\n"
673                 device_printf(dev, "Invalid MAC address\n");
674                 error = EIO;
675                 goto err_hw_init;
676         }
677 
678         
678         /* Allocate transmit descriptors and buffers */
678         /* Allocate transmit descriptors and buffers */
679         if (em_allocate_transmit_structures(adapter)) {
680                 device_printf(dev, 
680                 device_printf(dev, "Could not setup transmit structures\n"
680                 device_printf(dev, "Could not setup transmit structures\n");
681                 error = ENOMEM;
682                 goto err_tx_struct;
683         }
684 
685         
685         /* Allocate receive descriptors and buffers */
685         /* Allocate receive descriptors and buffers */
686         if (em_allocate_receive_structures(adapter)) {
687                 device_printf(dev, 
687                 device_printf(dev, "Could not setup receive structures\n"
687                 device_printf(dev, "Could not setup receive structures\n");
688                 error = ENOMEM;
689                 goto err_rx_struct;
690         }
691 
692         
692         /*
693         **  Do interrupt configuration
694         */
694         */
695         if (adapter->msi > 1) 
695         if (adapter->msi > 1) /* Do MSI/X */
695         if (adapter->msi > 1) /* Do MSI/X */
696                 error = em_allocate_msix(adapter);
697         else  
697         else  /* MSI or Legacy */
697         else  /* MSI or Legacy */
698                 error = em_allocate_legacy(adapter);
699         if (error)
700                 goto err_rx_struct;
701 
702