240c088d3345a4863ca98a208160a7681f536bec
[dragonfly.git] / sys / dev / netif / em / if_em.c
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 /*$FreeBSD: src/sys/dev/em/if_em.c,v 1.2.2.15 2003/06/09 22:10:15 pdeuskar Exp $*/
35 /*$DragonFly: src/sys/dev/netif/em/if_em.c,v 1.7 2004/03/14 15:36:49 joerg Exp $*/
36
37 #include "if_em.h"
38
39 /*********************************************************************
40  *  Set this to one to display debug statistics                                                   
41  *********************************************************************/
42 int             em_display_debug_stats = 0;
43
44 /*********************************************************************
45  *  Linked list of board private structures for all NICs found
46  *********************************************************************/
47
48 struct adapter *em_adapter_list = NULL;
49
50
51 /*********************************************************************
52  *  Driver version
53  *********************************************************************/
54
55 char em_driver_version[] = "1.6.6";
56
57
58 /*********************************************************************
59  *  PCI Device ID Table
60  *
61  *  Used by probe to select devices to load on
62  *  Last field stores an index into em_strings
63  *  Last entry must be all 0s
64  *
65  *  { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
66  *********************************************************************/
67
68 static em_vendor_info_t em_vendor_info_array[] =
69 {
70         /* Intel(R) PRO/1000 Network Connection */
71         { 0x8086, 0x1000, PCI_ANY_ID, PCI_ANY_ID, 0},
72         { 0x8086, 0x1001, PCI_ANY_ID, PCI_ANY_ID, 0},
73         { 0x8086, 0x1004, PCI_ANY_ID, PCI_ANY_ID, 0},
74         { 0x8086, 0x1008, PCI_ANY_ID, PCI_ANY_ID, 0},
75         { 0x8086, 0x1009, PCI_ANY_ID, PCI_ANY_ID, 0},
76         { 0x8086, 0x100C, PCI_ANY_ID, PCI_ANY_ID, 0},
77         { 0x8086, 0x100D, PCI_ANY_ID, PCI_ANY_ID, 0},
78         { 0x8086, 0x100E, PCI_ANY_ID, PCI_ANY_ID, 0},
79         { 0x8086, 0x100F, PCI_ANY_ID, PCI_ANY_ID, 0},
80         { 0x8086, 0x1010, PCI_ANY_ID, PCI_ANY_ID, 0},
81         { 0x8086, 0x1011, PCI_ANY_ID, PCI_ANY_ID, 0},
82         { 0x8086, 0x1012, PCI_ANY_ID, PCI_ANY_ID, 0},
83         { 0x8086, 0x1013, PCI_ANY_ID, PCI_ANY_ID, 0},
84         { 0x8086, 0x1014, PCI_ANY_ID, PCI_ANY_ID, 0},
85         { 0x8086, 0x1015, PCI_ANY_ID, PCI_ANY_ID, 0},
86         { 0x8086, 0x1016, PCI_ANY_ID, PCI_ANY_ID, 0},
87         { 0x8086, 0x1017, PCI_ANY_ID, PCI_ANY_ID, 0},
88         { 0x8086, 0x1018, PCI_ANY_ID, PCI_ANY_ID, 0},
89         { 0x8086, 0x1019, PCI_ANY_ID, PCI_ANY_ID, 0},
90         { 0x8086, 0x101A, PCI_ANY_ID, PCI_ANY_ID, 0},
91         { 0x8086, 0x101D, PCI_ANY_ID, PCI_ANY_ID, 0},
92         { 0x8086, 0x101E, PCI_ANY_ID, PCI_ANY_ID, 0},
93         /* required last entry */
94         { 0, 0, 0, 0, 0}
95 };
96
97 /*********************************************************************
98  *  Table of branding strings for all supported NICs.
99  *********************************************************************/
100
101 static char *em_strings[] = {
102         "Intel(R) PRO/1000 Network Connection"
103 };
104
105 /*********************************************************************
106  *  Function prototypes            
107  *********************************************************************/
108 static int  em_probe(device_t);
109 static int  em_attach(device_t);
110 static int  em_detach(device_t);
111 static int  em_shutdown(device_t);
112 static void em_intr(void *);
113 static void em_start(struct ifnet *);
114 static int  em_ioctl(struct ifnet *, u_long, caddr_t);
115 static void em_watchdog(struct ifnet *);
116 static void em_init(void *);
117 static void em_stop(void *);
118 static void em_media_status(struct ifnet *, struct ifmediareq *);
119 static int  em_media_change(struct ifnet *);
120 static void em_identify_hardware(struct adapter *);
121 static int  em_allocate_pci_resources(struct adapter *);
122 static void em_free_pci_resources(struct adapter *);
123 static void em_local_timer(void *);
124 static int  em_hardware_init(struct adapter *);
125 static void em_setup_interface(device_t, struct adapter *);
126 static int  em_setup_transmit_structures(struct adapter *);
127 static void em_initialize_transmit_unit(struct adapter *);
128 static int  em_setup_receive_structures(struct adapter *);
129 static void em_initialize_receive_unit(struct adapter *);
130 static void em_enable_intr(struct adapter *);
131 static void em_disable_intr(struct adapter *);
132 static void em_free_transmit_structures(struct adapter *);
133 static void em_free_receive_structures(struct adapter *);
134 static void em_update_stats_counters(struct adapter *);
135 static void em_clean_transmit_interrupts(struct adapter *);
136 static int  em_allocate_receive_structures(struct adapter *);
137 static int  em_allocate_transmit_structures(struct adapter *);
138 static void em_process_receive_interrupts(struct adapter *, int);
139 static void em_receive_checksum(struct adapter *, 
140                                 struct em_rx_desc *,
141                                 struct mbuf *);
142 static void em_transmit_checksum_setup(struct adapter *,
143                                        struct mbuf *,
144                                        u_int32_t *,
145                                        u_int32_t *);
146 static void em_set_promisc(struct adapter *);
147 static void em_disable_promisc(struct adapter *);
148 static void em_set_multi(struct adapter *);
149 static void em_print_hw_stats(struct adapter *);
150 static void em_print_link_status(struct adapter *);
151 static int  em_get_buf(int i, struct adapter *,
152                        struct mbuf *);
153 static void em_enable_vlans(struct adapter *);
154 static int  em_encap(struct adapter *, struct mbuf *);
155 static void em_smartspeed(struct adapter *);
156 static int  em_82547_fifo_workaround(struct adapter *, int);
157 static void em_82547_update_fifo_head(struct adapter *, int);
158 static int  em_82547_tx_fifo_reset(struct adapter *);
159 static void em_82547_move_tail(void *arg);
160 static void em_print_debug_info(struct adapter *);
161 static int  em_is_valid_ether_addr(u_int8_t *);
162 static int  em_sysctl_stats(SYSCTL_HANDLER_ARGS);
163 static int  em_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
164
165 /*********************************************************************
166  *  FreeBSD Device Interface Entry Points                    
167  *********************************************************************/
168
169 static device_method_t em_methods[] = {
170         /* Device interface */
171         DEVMETHOD(device_probe, em_probe),
172         DEVMETHOD(device_attach, em_attach),
173         DEVMETHOD(device_detach, em_detach),
174         DEVMETHOD(device_shutdown, em_shutdown),
175         {0, 0}
176 };
177
178 static driver_t em_driver = {
179         "em", em_methods, sizeof(struct adapter ),
180 };
181
182 static devclass_t em_devclass;
183
184 DECLARE_DUMMY_MODULE(if_em);
185 DRIVER_MODULE(if_em, pci, em_driver, em_devclass, 0, 0);
186
187 /*********************************************************************
188  *  Device identification routine
189  *
190  *  em_probe determines if the driver should be loaded on
191  *  adapter based on PCI vendor/device id of the adapter.
192  *
193  *  return 0 on success, positive on failure
194  *********************************************************************/
195
196 static int
197 em_probe(device_t dev)
198 {
199         em_vendor_info_t *ent;
200
201         u_int16_t       pci_vendor_id = 0;
202         u_int16_t       pci_device_id = 0;
203         u_int16_t       pci_subvendor_id = 0;
204         u_int16_t       pci_subdevice_id = 0;
205         char            adapter_name[60];
206
207         INIT_DEBUGOUT("em_probe: begin");
208
209         pci_vendor_id = pci_get_vendor(dev);
210         if (pci_vendor_id != EM_VENDOR_ID)
211                 return(ENXIO);
212
213         pci_device_id = pci_get_device(dev);
214         pci_subvendor_id = pci_get_subvendor(dev);
215         pci_subdevice_id = pci_get_subdevice(dev);
216
217         ent = em_vendor_info_array;
218         while (ent->vendor_id != 0) {
219                 if ((pci_vendor_id == ent->vendor_id) &&
220                     (pci_device_id == ent->device_id) &&
221
222                     ((pci_subvendor_id == ent->subvendor_id) ||
223                      (ent->subvendor_id == PCI_ANY_ID)) &&
224
225                     ((pci_subdevice_id == ent->subdevice_id) ||
226                      (ent->subdevice_id == PCI_ANY_ID))) {
227                         sprintf(adapter_name, "%s, Version - %s", 
228                                 em_strings[ent->index], 
229                                 em_driver_version);
230                         device_set_desc_copy(dev, adapter_name);
231                         return(0);
232                 }
233                 ent++;
234         }
235
236         return(ENXIO);
237 }
238
239 /*********************************************************************
240  *  Device initialization routine
241  *
242  *  The attach entry point is called when the driver is being loaded.
243  *  This routine identifies the type of hardware, allocates all resources 
244  *  and initializes the hardware.     
245  *  
246  *  return 0 on success, positive on failure
247  *********************************************************************/
248
249 static int
250 em_attach(device_t dev)
251 {
252         struct adapter * adapter;
253         int             s;
254         int             tsize, rsize;
255         int             error = 0;
256
257         INIT_DEBUGOUT("em_attach: begin");
258         s = splimp();
259
260         /* Allocate, clear, and link in our adapter structure */
261         if (!(adapter = device_get_softc(dev))) {
262                 printf("em: adapter structure allocation failed\n");
263                 splx(s);
264                 return(ENOMEM);
265         }
266         bzero(adapter, sizeof(struct adapter ));
267         adapter->dev = dev;
268         adapter->osdep.dev = dev;
269         adapter->unit = device_get_unit(dev);
270
271         if (em_adapter_list != NULL)
272                 em_adapter_list->prev = adapter;
273         adapter->next = em_adapter_list;
274         em_adapter_list = adapter;
275
276         /* SYSCTL stuff */
277         sysctl_ctx_init(&adapter->sysctl_ctx);
278         adapter->sysctl_tree = SYSCTL_ADD_NODE(&adapter->sysctl_ctx,
279                                                SYSCTL_STATIC_CHILDREN(_hw),
280                                                OID_AUTO, 
281                                                device_get_nameunit(dev),
282                                                CTLFLAG_RD,
283                                                0, "");
284         if (adapter->sysctl_tree == NULL) {
285                 error = EIO;
286                 goto err_sysctl;
287         }
288  
289         SYSCTL_ADD_PROC(&adapter->sysctl_ctx,  
290                         SYSCTL_CHILDREN(adapter->sysctl_tree),
291                         OID_AUTO, "debug_info", CTLTYPE_INT|CTLFLAG_RW, 
292                         (void *)adapter, 0,
293                         em_sysctl_debug_info, "I", "Debug Information");
294
295         SYSCTL_ADD_PROC(&adapter->sysctl_ctx,  
296                         SYSCTL_CHILDREN(adapter->sysctl_tree),
297                         OID_AUTO, "stats", CTLTYPE_INT|CTLFLAG_RW, 
298                         (void *)adapter, 0,
299                         em_sysctl_stats, "I", "Statistics");
300
301         callout_handle_init(&adapter->timer_handle);
302         callout_handle_init(&adapter->tx_fifo_timer_handle);
303
304         /* Determine hardware revision */
305         em_identify_hardware(adapter);
306       
307         /* Parameters (to be read from user) */   
308         adapter->num_tx_desc = EM_MAX_TXD;
309         adapter->num_rx_desc = EM_MAX_RXD;
310         adapter->tx_int_delay = EM_TIDV;
311         adapter->tx_abs_int_delay = EM_TADV;
312         adapter->rx_int_delay = EM_RDTR;
313         adapter->rx_abs_int_delay = EM_RADV;
314         adapter->hw.autoneg = DO_AUTO_NEG;
315         adapter->hw.wait_autoneg_complete = WAIT_FOR_AUTO_NEG_DEFAULT;
316         adapter->hw.autoneg_advertised = AUTONEG_ADV_DEFAULT;
317         adapter->hw.tbi_compatibility_en = TRUE;
318         adapter->rx_buffer_len = EM_RXBUFFER_2048;
319                         
320         /* 
321          * These parameters control the automatic generation(Tx) and 
322          * response(Rx) to Ethernet PAUSE frames.
323          */
324         adapter->hw.fc_high_water = FC_DEFAULT_HI_THRESH;
325         adapter->hw.fc_low_water  = FC_DEFAULT_LO_THRESH;
326         adapter->hw.fc_pause_time = FC_DEFAULT_TX_TIMER;
327         adapter->hw.fc_send_xon   = TRUE;
328         adapter->hw.fc = em_fc_full;
329         
330         adapter->hw.phy_init_script = 1;
331
332         /* 
333          * Set the max frame size assuming standard ethernet 
334          * sized frames 
335          */   
336         adapter->hw.max_frame_size = 
337                 ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN;
338
339         adapter->hw.min_frame_size = 
340                 MINIMUM_ETHERNET_PACKET_SIZE + ETHER_CRC_LEN;
341
342         /* 
343          * This controls when hardware reports transmit completion 
344          * status. 
345          */
346         adapter->hw.report_tx_early = 1;
347
348
349         if (em_allocate_pci_resources(adapter)) {
350                 printf("em%d: Allocation of PCI resources failed\n", 
351                        adapter->unit);
352                 error = ENXIO;
353                 goto err_pci;
354         }
355         
356         em_init_eeprom_params(&adapter->hw);
357
358         tsize = EM_ROUNDUP(adapter->num_tx_desc *
359                            sizeof(struct em_tx_desc), 4096);
360
361         /* Allocate Transmit Descriptor ring */
362         if (!(adapter->tx_desc_base = (struct em_tx_desc *)
363               contigmalloc(tsize, M_DEVBUF, M_NOWAIT, 0, ~0, 
364                            PAGE_SIZE, 0))) {
365                 printf("em%d: Unable to allocate TxDescriptor memory\n", 
366                        adapter->unit);
367                 error = ENOMEM;
368                 goto err_tx_desc;
369         }
370
371         rsize = EM_ROUNDUP(adapter->num_rx_desc *
372                            sizeof(struct em_rx_desc), 4096);
373
374         /* Allocate Receive Descriptor ring */
375         if (!(adapter->rx_desc_base = (struct em_rx_desc *)
376               contigmalloc(rsize, M_DEVBUF, M_NOWAIT, 0, ~0, 
377                            PAGE_SIZE, 0))) {
378                 printf("em%d: Unable to allocate rx_desc memory\n", 
379                        adapter->unit);
380                 error = ENOMEM;
381                 goto err_rx_desc;
382         }
383
384         /* Initialize the hardware */
385         if (em_hardware_init(adapter)) {
386                 printf("em%d: Unable to initialize the hardware\n",
387                        adapter->unit);
388                 error = EIO;
389                 goto err_hw_init;
390         }
391
392         /* Copy the permanent MAC address out of the EEPROM */
393         if (em_read_mac_addr(&adapter->hw) < 0) {
394                 printf("em%d: EEPROM read error while reading mac address\n",
395                        adapter->unit);
396                 error = EIO;
397                 goto err_mac_addr;
398         }
399
400         if (!em_is_valid_ether_addr(adapter->hw.mac_addr)) {
401                 printf("em%d: Invalid mac address\n", adapter->unit);
402                 error = EIO;
403                 goto err_mac_addr;
404         }
405
406
407         bcopy(adapter->hw.mac_addr, adapter->interface_data.ac_enaddr,
408               ETHER_ADDR_LEN);
409
410         /* Setup OS specific network interface */
411         em_setup_interface(dev, adapter);
412
413         /* Initialize statistics */
414         em_clear_hw_cntrs(&adapter->hw);
415         em_update_stats_counters(adapter);
416         adapter->hw.get_link_status = 1;
417         em_check_for_link(&adapter->hw);
418
419         /* Print the link status */
420         if (adapter->link_active == 1) {
421                 em_get_speed_and_duplex(&adapter->hw, &adapter->link_speed, 
422                                         &adapter->link_duplex);
423                 printf("em%d:  Speed:%d Mbps  Duplex:%s\n",
424                        adapter->unit,
425                        adapter->link_speed,
426                        adapter->link_duplex == FULL_DUPLEX ? "Full" : "Half");
427         } else
428                 printf("em%d:  Speed:N/A  Duplex:N/A\n", adapter->unit);
429
430         INIT_DEBUGOUT("em_attach: end");
431         splx(s);
432         return(error);
433
434
435 err_mac_addr:
436 err_hw_init:
437         contigfree(adapter->rx_desc_base, rsize, M_DEVBUF);
438 err_rx_desc:
439         contigfree(adapter->tx_desc_base, tsize, M_DEVBUF);
440 err_tx_desc:
441 err_pci:
442         em_free_pci_resources(adapter);
443         sysctl_ctx_free(&adapter->sysctl_ctx);
444 err_sysctl:
445         splx(s);
446         return(error);
447 }
448
449 /*********************************************************************
450  *  Device removal routine
451  *
452  *  The detach entry point is called when the driver is being removed.
453  *  This routine stops the adapter and deallocates all the resources
454  *  that were allocated for driver operation.
455  *  
456  *  return 0 on success, positive on failure
457  *********************************************************************/
458
459 static int
460 em_detach(device_t dev)
461 {
462         struct adapter * adapter = device_get_softc(dev);
463         struct ifnet   *ifp = &adapter->interface_data.ac_if;
464         int             s;
465         int             size;
466
467         INIT_DEBUGOUT("em_detach: begin");
468         s = splimp();
469
470         em_stop(adapter);
471         em_phy_hw_reset(&adapter->hw);
472         ether_ifdetach(&adapter->interface_data.ac_if);
473         em_free_pci_resources(adapter);
474
475         size = EM_ROUNDUP(adapter->num_tx_desc *
476                           sizeof(struct em_tx_desc), 4096);
477
478         /* Free Transmit Descriptor ring */
479         if (adapter->tx_desc_base) {
480                 contigfree(adapter->tx_desc_base, size, M_DEVBUF);
481                 adapter->tx_desc_base = NULL;
482         }
483
484         size = EM_ROUNDUP(adapter->num_rx_desc *
485                           sizeof(struct em_rx_desc), 4096);
486
487         /* Free Receive Descriptor ring */
488         if (adapter->rx_desc_base) {
489                 contigfree(adapter->rx_desc_base, size, M_DEVBUF);
490                 adapter->rx_desc_base = NULL;
491         }
492
493         /* Remove from the adapter list */
494         if (em_adapter_list == adapter)
495                 em_adapter_list = adapter->next;
496         if (adapter->next != NULL)
497                 adapter->next->prev = adapter->prev;
498         if (adapter->prev != NULL)
499                 adapter->prev->next = adapter->next;
500
501         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
502         ifp->if_timer = 0;
503
504         adapter->sysctl_tree = NULL;
505         sysctl_ctx_free(&adapter->sysctl_ctx);
506
507         splx(s);
508         return(0);
509 }
510
511 /*********************************************************************
512  *
513  *  Shutdown entry point
514  *
515  **********************************************************************/ 
516
517 static int
518 em_shutdown(device_t dev)
519 {
520         struct adapter *adapter = device_get_softc(dev);
521         em_stop(adapter);
522         return(0);
523 }
524
525
526 /*********************************************************************
527  *  Transmit entry point
528  *
529  *  em_start is called by the stack to initiate a transmit.
530  *  The driver will remain in this routine as long as there are
531  *  packets to transmit and transmit resources are available.
532  *  In case resources are not available stack is notified and
533  *  the packet is requeued.
534  **********************************************************************/
535
536 static void
537 em_start(struct ifnet *ifp)
538 {
539         int             s;
540         struct mbuf    *m_head;
541         struct adapter *adapter = ifp->if_softc;
542
543         if (!adapter->link_active)
544                 return;
545
546         s = splimp();
547         while (ifp->if_snd.ifq_head != NULL) {
548
549                 IF_DEQUEUE(&ifp->if_snd, m_head);
550                 
551                 if (m_head == NULL) break;
552                         
553                 if (em_encap(adapter, m_head)) { 
554                         ifp->if_flags |= IFF_OACTIVE;
555                         IF_PREPEND(&ifp->if_snd, m_head);
556                         break;
557                 }
558
559                 /* Send a copy of the frame to the BPF listener */
560 #if defined(__DragonFly__) || __FreeBSD_version < 500000
561                 if (ifp->if_bpf)
562                         bpf_mtap(ifp, m_head);
563 #else
564                 BPF_MTAP(ifp, m_head);
565 #endif
566         
567                 /* Set timeout in case hardware has problems transmitting */
568                 ifp->if_timer = EM_TX_TIMEOUT;
569         
570         }
571         splx(s);
572         return;
573 }
574
575 /*********************************************************************
576  *  Ioctl entry point
577  *
578  *  em_ioctl is called when the user wants to configure the
579  *  interface.
580  *
581  *  return 0 on success, positive on failure
582  **********************************************************************/
583
584 static int
585 em_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
586 {
587         int             s, mask, error = 0;
588         struct ifreq   *ifr = (struct ifreq *) data;
589         struct adapter * adapter = ifp->if_softc;
590
591         s = splimp();
592         switch (command) {
593         case SIOCSIFADDR:
594         case SIOCGIFADDR:
595                 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCxIFADDR (Get/Set Interface Addr)");
596                 ether_ioctl(ifp, command, data);
597                 break;
598         case SIOCSIFMTU:
599                 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFMTU (Set Interface MTU)");
600                 if (ifr->ifr_mtu > MAX_JUMBO_FRAME_SIZE - ETHER_HDR_LEN) {
601                         error = EINVAL;
602                 } else {
603                         ifp->if_mtu = ifr->ifr_mtu;
604                         adapter->hw.max_frame_size = 
605                         ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
606                         em_init(adapter);
607                 }
608                 break;
609         case SIOCSIFFLAGS:
610                 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFFLAGS (Set Interface Flags)");
611                 if (ifp->if_flags & IFF_UP) {
612                         if (!(ifp->if_flags & IFF_RUNNING))
613                                 em_init(adapter);
614
615                         em_disable_promisc(adapter);
616                         em_set_promisc(adapter);
617                 } else {
618                         if (ifp->if_flags & IFF_RUNNING) {
619                                 em_stop(adapter);
620                         }
621                 }
622                 break;
623         case SIOCADDMULTI:
624         case SIOCDELMULTI:
625                 IOCTL_DEBUGOUT("ioctl rcv'd: SIOC(ADD|DEL)MULTI");
626                 if (ifp->if_flags & IFF_RUNNING) {
627                         em_disable_intr(adapter);
628                         em_set_multi(adapter);
629                         if (adapter->hw.mac_type == em_82542_rev2_0) {
630                                 em_initialize_receive_unit(adapter);
631                         }
632 #ifdef DEVICE_POLLING
633                         if (!(ifp->if_ipending & IFF_POLLING))
634 #endif
635                                 em_enable_intr(adapter);
636                 }
637                 break;
638         case SIOCSIFMEDIA:
639         case SIOCGIFMEDIA:
640                 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCxIFMEDIA (Get/Set Interface Media)");
641                 error = ifmedia_ioctl(ifp, ifr, &adapter->media, command);
642                 break;
643         case SIOCSIFCAP:
644                 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFCAP (Set Capabilities)");
645                 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
646                 if (mask & IFCAP_HWCSUM) {
647                         if (IFCAP_HWCSUM & ifp->if_capenable)
648                                 ifp->if_capenable &= ~IFCAP_HWCSUM;
649                         else
650                                 ifp->if_capenable |= IFCAP_HWCSUM;
651                         if (ifp->if_flags & IFF_RUNNING)
652                                 em_init(adapter);
653                 }
654                 break;
655         default:
656                 IOCTL_DEBUGOUT1("ioctl received: UNKNOWN (0x%d)\n", (int)command);
657                 error = EINVAL;
658         }
659
660         splx(s);
661         return(error);
662 }
663
664 /*********************************************************************
665  *  Watchdog entry point
666  *
667  *  This routine is called whenever hardware quits transmitting.
668  *
669  **********************************************************************/
670
671 static void
672 em_watchdog(struct ifnet *ifp)
673 {
674         struct adapter * adapter;
675         adapter = ifp->if_softc;
676
677         /* If we are in this routine because of pause frames, then
678          * don't reset the hardware.
679          */
680         if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_TXOFF) {
681                 ifp->if_timer = EM_TX_TIMEOUT;
682                 return;
683         }
684
685         printf("em%d: watchdog timeout -- resetting\n", adapter->unit);
686
687         ifp->if_flags &= ~IFF_RUNNING;
688
689         em_stop(adapter);
690         em_init(adapter);
691
692         ifp->if_oerrors++;
693         return;
694 }
695
696 /*********************************************************************
697  *  Init entry point
698  *
699  *  This routine is used in two ways. It is used by the stack as
700  *  init entry point in network interface structure. It is also used
701  *  by the driver as a hw/sw initialization routine to get to a 
702  *  consistent state.
703  *
704  *  return 0 on success, positive on failure
705  **********************************************************************/
706
707 static void
708 em_init(void *arg)
709 {
710         int             s;
711         struct ifnet   *ifp;
712         struct adapter * adapter = arg;
713
714         INIT_DEBUGOUT("em_init: begin");
715
716         s = splimp();
717
718         em_stop(adapter);
719
720         /* Initialize the hardware */
721         if (em_hardware_init(adapter)) {
722                 printf("em%d: Unable to initialize the hardware\n", 
723                        adapter->unit);
724                 splx(s);
725                 return;
726         }
727
728         em_enable_vlans(adapter);
729
730         /* Prepare transmit descriptors and buffers */
731         if (em_setup_transmit_structures(adapter)) {
732                 printf("em%d: Could not setup transmit structures\n", 
733                        adapter->unit);
734                 em_stop(adapter); 
735                 splx(s);
736                 return;
737         }
738         em_initialize_transmit_unit(adapter);
739
740         /* Setup Multicast table */
741         em_set_multi(adapter);
742
743         /* Prepare receive descriptors and buffers */
744         if (em_setup_receive_structures(adapter)) {
745                 printf("em%d: Could not setup receive structures\n", 
746                        adapter->unit);
747                 em_stop(adapter);
748                 splx(s);
749                 return;
750         }
751         em_initialize_receive_unit(adapter);
752
753         ifp = &adapter->interface_data.ac_if;
754         ifp->if_flags |= IFF_RUNNING;
755         ifp->if_flags &= ~IFF_OACTIVE;
756
757         if (adapter->hw.mac_type >= em_82543) {
758                 if (ifp->if_capenable & IFCAP_TXCSUM)
759                         ifp->if_hwassist = EM_CHECKSUM_FEATURES;
760                 else
761                         ifp->if_hwassist = 0;
762         }
763
764         adapter->timer_handle = timeout(em_local_timer, adapter, 2*hz);
765         em_clear_hw_cntrs(&adapter->hw);
766 #ifdef DEVICE_POLLING
767         /*
768          * Only enable interrupts if we are not polling, make sure
769          * they are off otherwise.
770          */
771         if (ifp->if_ipending & IFF_POLLING)
772                 em_disable_intr(adapter);
773         else
774 #endif /* DEVICE_POLLING */
775                 em_enable_intr(adapter);
776
777         splx(s);
778         return;
779 }
780
781
782 #ifdef DEVICE_POLLING
783 static poll_handler_t em_poll;
784         
785 static void     
786 em_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
787 {
788         struct adapter *adapter = ifp->if_softc;
789         u_int32_t reg_icr;
790
791         if (cmd == POLL_DEREGISTER) {       /* final call, enable interrupts */
792                 em_enable_intr(adapter);
793                 return;
794         }
795         if (cmd == POLL_AND_CHECK_STATUS) {
796                 reg_icr = E1000_READ_REG(&adapter->hw, ICR);
797                 if (reg_icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
798                         untimeout(em_local_timer, adapter, adapter->timer_handle);
799                         adapter->hw.get_link_status = 1;
800                         em_check_for_link(&adapter->hw);
801                         em_print_link_status(adapter);
802                         adapter->timer_handle = timeout(em_local_timer, adapter, 2*hz);
803                 }
804         }
805         if (ifp->if_flags & IFF_RUNNING) {
806                 em_process_receive_interrupts(adapter, count);
807                 em_clean_transmit_interrupts(adapter);
808         }
809         
810         if (ifp->if_flags & IFF_RUNNING && ifp->if_snd.ifq_head != NULL)
811                 em_start(ifp);
812 }
813 #endif /* DEVICE_POLLING */
814
815 /*********************************************************************
816  *
817  *  Interrupt Service routine  
818  *
819  **********************************************************************/
820 static void
821 em_intr(void *arg)
822 {
823         u_int32_t       loop_cnt = EM_MAX_INTR;
824         u_int32_t       reg_icr;
825         struct ifnet    *ifp;
826         struct adapter  *adapter = arg;
827
828         ifp = &adapter->interface_data.ac_if;  
829
830 #ifdef DEVICE_POLLING
831         if (ifp->if_ipending & IFF_POLLING)
832                 return;
833
834         if (ether_poll_register(em_poll, ifp)) {
835                 em_disable_intr(adapter);
836                 em_poll(ifp, 0, 1);
837                 return;
838         }
839 #endif /* DEVICE_POLLING */
840         
841         reg_icr = E1000_READ_REG(&adapter->hw, ICR);
842         if (!reg_icr) {
843                 return;
844         }
845
846         /* Link status change */
847         if (reg_icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
848                 untimeout(em_local_timer, adapter,
849                           adapter->timer_handle);
850                 adapter->hw.get_link_status = 1;
851                 em_check_for_link(&adapter->hw);
852                 em_print_link_status(adapter);
853                 adapter->timer_handle =
854                 timeout(em_local_timer, adapter, 2*hz);
855         }
856
857         while (loop_cnt > 0) {
858                 if (ifp->if_flags & IFF_RUNNING) {
859                         em_process_receive_interrupts(adapter, -1);
860                         em_clean_transmit_interrupts(adapter);
861                 }
862                 loop_cnt--;
863         }
864
865         if (ifp->if_flags & IFF_RUNNING && ifp->if_snd.ifq_head != NULL)
866                 em_start(ifp);
867
868         return;
869 }
870
871
872
873 /*********************************************************************
874  *
875  *  Media Ioctl callback
876  *
877  *  This routine is called whenever the user queries the status of
878  *  the interface using ifconfig.
879  *
880  **********************************************************************/
881 static void
882 em_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
883 {
884         struct adapter * adapter = ifp->if_softc;
885
886         INIT_DEBUGOUT("em_media_status: begin");
887
888         em_check_for_link(&adapter->hw);
889         if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU) {
890                 if (adapter->link_active == 0) {
891                         em_get_speed_and_duplex(&adapter->hw, 
892                                                 &adapter->link_speed, 
893                                                 &adapter->link_duplex);
894                         adapter->link_active = 1;
895                 }
896         } else {
897                 if (adapter->link_active == 1) {
898                         adapter->link_speed = 0;
899                         adapter->link_duplex = 0;
900                         adapter->link_active = 0;
901                 }
902         }
903
904         ifmr->ifm_status = IFM_AVALID;
905         ifmr->ifm_active = IFM_ETHER;
906
907         if (!adapter->link_active)
908                 return;
909
910         ifmr->ifm_status |= IFM_ACTIVE;
911
912         if (adapter->hw.media_type == em_media_type_fiber) {
913                 ifmr->ifm_active |= IFM_1000_SX | IFM_FDX;
914         } else {
915                 switch (adapter->link_speed) {
916                 case 10:
917                         ifmr->ifm_active |= IFM_10_T;
918                         break;
919                 case 100:
920                         ifmr->ifm_active |= IFM_100_TX;
921                         break;
922                 case 1000:
923 #if defined(__DragonFly__) || __FreeBSD_version < 500000 
924                         ifmr->ifm_active |= IFM_1000_TX;
925 #else
926                         ifmr->ifm_active |= IFM_1000_T;
927 #endif
928                         break;
929                 }
930                 if (adapter->link_duplex == FULL_DUPLEX)
931                         ifmr->ifm_active |= IFM_FDX;
932                 else
933                         ifmr->ifm_active |= IFM_HDX;
934         }
935         return;
936 }
937
938 /*********************************************************************
939  *
940  *  Media Ioctl callback
941  *
942  *  This routine is called when the user changes speed/duplex using
943  *  media/mediopt option with ifconfig.
944  *
945  **********************************************************************/
946 static int
947 em_media_change(struct ifnet *ifp)
948 {
949         struct adapter * adapter = ifp->if_softc;
950         struct ifmedia  *ifm = &adapter->media;
951
952         INIT_DEBUGOUT("em_media_change: begin");
953
954         if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
955                 return(EINVAL);
956
957         switch (IFM_SUBTYPE(ifm->ifm_media)) {
958         case IFM_AUTO:
959                 adapter->hw.autoneg = DO_AUTO_NEG;
960                 adapter->hw.autoneg_advertised = AUTONEG_ADV_DEFAULT;
961                 break;
962         case IFM_1000_SX:
963 #if defined(__DragonFly__) || __FreeBSD_version < 500000 
964         case IFM_1000_TX:
965 #else
966         case IFM_1000_T:
967 #endif
968                 adapter->hw.autoneg = DO_AUTO_NEG;
969                 adapter->hw.autoneg_advertised = ADVERTISE_1000_FULL;
970                 break;
971         case IFM_100_TX:
972                 adapter->hw.autoneg = FALSE;
973                 adapter->hw.autoneg_advertised = 0;
974                 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
975                         adapter->hw.forced_speed_duplex = em_100_full;
976                 else
977                         adapter->hw.forced_speed_duplex = em_100_half;
978                 break;
979         case IFM_10_T:
980                 adapter->hw.autoneg = FALSE;
981                 adapter->hw.autoneg_advertised = 0;
982                 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
983                         adapter->hw.forced_speed_duplex = em_10_full;
984                 else
985                         adapter->hw.forced_speed_duplex = em_10_half;
986                 break;
987         default:
988                 printf("em%d: Unsupported media type\n", adapter->unit);
989         }
990
991         em_init(adapter);
992
993         return(0);
994 }
995
996 #define EM_FIFO_HDR              0x10
997 #define EM_82547_PKT_THRESH      0x3e0
998 #define EM_82547_TX_FIFO_SIZE    0x2800
999 #define EM_82547_TX_FIFO_BEGIN   0xf00
1000 /*********************************************************************
1001  *
1002  *  This routine maps the mbufs to tx descriptors.
1003  *
1004  *  return 0 on success, positive on failure
1005  **********************************************************************/
1006
1007 static int
1008 em_encap(struct adapter *adapter, struct mbuf *m_head)
1009
1010         vm_offset_t     virtual_addr;
1011         u_int32_t       txd_upper;
1012         u_int32_t       txd_lower;
1013         int             txd_used, i, txd_saved;
1014         struct mbuf     *mp;
1015
1016 #if defined(__DragonFly__) || __FreeBSD_version < 500000
1017         struct ifvlan *ifv = NULL;
1018 #else
1019         struct m_tag    *mtag;
1020 #endif
1021         struct em_buffer   *tx_buffer = NULL;
1022         struct em_tx_desc *current_tx_desc = NULL;
1023         struct ifnet   *ifp = &adapter->interface_data.ac_if;
1024
1025         /* 
1026          * Force a cleanup if number of TX descriptors 
1027          * available hits the threshold 
1028          */
1029         if (adapter->num_tx_desc_avail <= EM_TX_CLEANUP_THRESHOLD)
1030                 em_clean_transmit_interrupts(adapter);
1031
1032         if (adapter->num_tx_desc_avail <= EM_TX_CLEANUP_THRESHOLD) {
1033                 adapter->no_tx_desc_avail1++;
1034                 return (ENOBUFS);
1035         }
1036
1037         if (ifp->if_hwassist > 0) {
1038                 em_transmit_checksum_setup(adapter,  m_head,
1039                                            &txd_upper, &txd_lower);
1040         }
1041         else 
1042                 txd_upper = txd_lower = 0;
1043
1044
1045         /* Find out if we are in vlan mode */
1046 #if defined(__DragonFly__) || __FreeBSD_version < 500000
1047         if ((m_head->m_flags & (M_PROTO1|M_PKTHDR)) == (M_PROTO1|M_PKTHDR) &&
1048             m_head->m_pkthdr.rcvif != NULL &&
1049             m_head->m_pkthdr.rcvif->if_type == IFT_L2VLAN)
1050                 ifv = m_head->m_pkthdr.rcvif->if_softc;
1051 #else
1052         mtag = VLAN_OUTPUT_TAG(ifp, m_head);
1053 #endif
1054
1055         i = adapter->next_avail_tx_desc;
1056         txd_saved = i;
1057         txd_used = 0;
1058         for (mp = m_head; mp != NULL; mp = mp->m_next) {
1059                 if (mp->m_len == 0)
1060                         continue;
1061
1062                 if (txd_used == adapter->num_tx_desc_avail) {
1063                         adapter->next_avail_tx_desc = txd_saved;
1064                         adapter->no_tx_desc_avail2++;
1065                         return (ENOBUFS);
1066                 }
1067
1068                 tx_buffer = &adapter->tx_buffer_area[i];
1069                 current_tx_desc = &adapter->tx_desc_base[i];
1070                 virtual_addr = mtod(mp, vm_offset_t);
1071                 current_tx_desc->buffer_addr = vtophys(virtual_addr);
1072
1073                 current_tx_desc->lower.data = (adapter->txd_cmd | txd_lower | mp->m_len);
1074                 current_tx_desc->upper.data = (txd_upper);
1075
1076                 if (++i == adapter->num_tx_desc)
1077                         i = 0;
1078
1079                 tx_buffer->m_head = NULL;
1080
1081                 txd_used++;
1082         }
1083
1084         adapter->num_tx_desc_avail -= txd_used;
1085         adapter->next_avail_tx_desc = i;
1086
1087 #if defined(__DragonFly__) || __FreeBSD_version < 500000
1088         if (ifv != NULL) {
1089                 /* Set the vlan id */
1090                 current_tx_desc->upper.fields.special = ifv->ifv_tag;
1091 #else
1092         if (mtag != NULL) {
1093                 /* Set the vlan id */
1094                 current_tx_desc->upper.fields.special = VLAN_TAG_VALUE(mtag);
1095 #endif
1096                 /* Tell hardware to add tag */
1097                 current_tx_desc->lower.data |= E1000_TXD_CMD_VLE;
1098         }
1099
1100         tx_buffer->m_head = m_head;
1101         
1102         /* 
1103          * Last Descriptor of Packet needs End Of Packet (EOP)
1104          */
1105         current_tx_desc->lower.data |= (E1000_TXD_CMD_EOP);
1106
1107         /* 
1108          * Advance the Transmit Descriptor Tail (Tdt), this tells the E1000
1109          * that this frame is available to transmit.
1110          */
1111         if (adapter->hw.mac_type == em_82547 &&
1112             adapter->link_duplex == HALF_DUPLEX) {
1113                 em_82547_move_tail(adapter);
1114         }
1115         else {
1116                 E1000_WRITE_REG(&adapter->hw, TDT, i);
1117                 if (adapter->hw.mac_type == em_82547) {
1118                         em_82547_update_fifo_head(adapter, m_head->m_pkthdr.len);
1119                 }
1120         }
1121
1122         return (0);
1123 }
1124
1125
1126 /*********************************************************************
1127  *
1128  * 82547 workaround to avoid controller hang in half-duplex environment.
1129  * The workaround is to avoid queuing a large packet that would span   
1130  * the internal Tx FIFO ring boundary. We need to reset the FIFO pointers
1131  * in this case. We do that only when FIFO is queiced.
1132  *
1133  **********************************************************************/
1134 static void
1135 em_82547_move_tail(void *arg)
1136 {
1137         int s;
1138         struct adapter *adapter = arg;
1139         uint16_t hw_tdt;
1140         uint16_t sw_tdt;
1141         struct em_tx_desc *tx_desc;
1142         uint16_t length = 0;
1143         boolean_t eop = 0;
1144
1145         s = splimp();
1146         hw_tdt = E1000_READ_REG(&adapter->hw, TDT);
1147         sw_tdt = adapter->next_avail_tx_desc;
1148         
1149         while (hw_tdt != sw_tdt) {
1150                 tx_desc = &adapter->tx_desc_base[hw_tdt];
1151                 length += tx_desc->lower.flags.length;
1152                 eop = tx_desc->lower.data & E1000_TXD_CMD_EOP;
1153                 if(++hw_tdt == adapter->num_tx_desc)
1154                         hw_tdt = 0;
1155
1156                 if(eop) {
1157                         if (em_82547_fifo_workaround(adapter, length)) {
1158                                 adapter->tx_fifo_wrk++;
1159                                 adapter->tx_fifo_timer_handle = 
1160                                         timeout(em_82547_move_tail,
1161                                                 adapter, 1);
1162                                 splx(s);
1163                                 return;
1164                         }
1165                         else {
1166                                 E1000_WRITE_REG(&adapter->hw, TDT, hw_tdt);
1167                                 em_82547_update_fifo_head(adapter, length);
1168                                 length = 0;
1169                         }
1170                 }
1171         }       
1172         splx(s);
1173         return;
1174 }
1175
1176 static int
1177 em_82547_fifo_workaround(struct adapter *adapter, int len)
1178 {       
1179         int fifo_space, fifo_pkt_len;
1180
1181         fifo_pkt_len = EM_ROUNDUP(len + EM_FIFO_HDR, EM_FIFO_HDR);
1182
1183         if (adapter->link_duplex == HALF_DUPLEX) {
1184                 fifo_space = EM_82547_TX_FIFO_SIZE - adapter->tx_fifo_head;
1185
1186                 if (fifo_pkt_len >= (EM_82547_PKT_THRESH + fifo_space)) {
1187                         if (em_82547_tx_fifo_reset(adapter)) {
1188                                 return(0);
1189                         }
1190                         else {
1191                                 return(1);
1192                         }
1193                 }
1194         }
1195
1196         return(0);
1197 }
1198
1199 static void
1200 em_82547_update_fifo_head(struct adapter *adapter, int len)
1201 {
1202         int fifo_pkt_len = EM_ROUNDUP(len + EM_FIFO_HDR, EM_FIFO_HDR);
1203         
1204         /* tx_fifo_head is always 16 byte aligned */
1205         adapter->tx_fifo_head += fifo_pkt_len;
1206         if (adapter->tx_fifo_head >= EM_82547_TX_FIFO_SIZE) {
1207                 adapter->tx_fifo_head -= EM_82547_TX_FIFO_SIZE;
1208         }
1209
1210         return;
1211 }
1212
1213
1214 static int
1215 em_82547_tx_fifo_reset(struct adapter *adapter)
1216 {       
1217         uint32_t tctl;
1218
1219         if ( (E1000_READ_REG(&adapter->hw, TDT) ==
1220               E1000_READ_REG(&adapter->hw, TDH)) &&
1221              (E1000_READ_REG(&adapter->hw, TDFT) == 
1222               E1000_READ_REG(&adapter->hw, TDFH)) &&
1223              (E1000_READ_REG(&adapter->hw, TDFTS) ==
1224               E1000_READ_REG(&adapter->hw, TDFHS)) &&
1225              (E1000_READ_REG(&adapter->hw, TDFPC) == 0)) {
1226
1227                 /* Disable TX unit */
1228                 tctl = E1000_READ_REG(&adapter->hw, TCTL);
1229                 E1000_WRITE_REG(&adapter->hw, TCTL, tctl & ~E1000_TCTL_EN);
1230
1231                 /* Reset FIFO pointers */
1232                 E1000_WRITE_REG(&adapter->hw, TDFT, EM_82547_TX_FIFO_BEGIN);
1233                 E1000_WRITE_REG(&adapter->hw, TDFH, EM_82547_TX_FIFO_BEGIN);
1234                 E1000_WRITE_REG(&adapter->hw, TDFTS, EM_82547_TX_FIFO_BEGIN);
1235                 E1000_WRITE_REG(&adapter->hw, TDFHS, EM_82547_TX_FIFO_BEGIN);
1236
1237                 /* Re-enable TX unit */
1238                 E1000_WRITE_REG(&adapter->hw, TCTL, tctl);
1239                 E1000_WRITE_FLUSH(&adapter->hw);
1240
1241                 adapter->tx_fifo_head = 0;
1242                 adapter->tx_fifo_reset++;
1243
1244                 return(TRUE);
1245         }
1246         else {
1247                 return(FALSE);
1248         }
1249 }
1250
1251 static void
1252 em_set_promisc(struct adapter * adapter)
1253 {
1254
1255         u_int32_t       reg_rctl;
1256         struct ifnet   *ifp = &adapter->interface_data.ac_if;
1257
1258         reg_rctl = E1000_READ_REG(&adapter->hw, RCTL);
1259
1260         if (ifp->if_flags & IFF_PROMISC) {
1261                 reg_rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
1262                 E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1263         } else if (ifp->if_flags & IFF_ALLMULTI) {
1264                 reg_rctl |= E1000_RCTL_MPE;
1265                 reg_rctl &= ~E1000_RCTL_UPE;
1266                 E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1267         }
1268
1269         return;
1270 }
1271
1272 static void
1273 em_disable_promisc(struct adapter * adapter)
1274 {
1275         u_int32_t       reg_rctl;
1276
1277         reg_rctl = E1000_READ_REG(&adapter->hw, RCTL);
1278
1279         reg_rctl &=  (~E1000_RCTL_UPE);
1280         reg_rctl &=  (~E1000_RCTL_MPE);
1281         E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1282
1283         return;
1284 }
1285
1286
1287 /*********************************************************************
1288  *  Multicast Update
1289  *
1290  *  This routine is called whenever multicast address list is updated.
1291  *
1292  **********************************************************************/
1293
1294 static void
1295 em_set_multi(struct adapter * adapter)
1296 {
1297         u_int32_t reg_rctl = 0;
1298         u_int8_t  mta[MAX_NUM_MULTICAST_ADDRESSES * ETH_LENGTH_OF_ADDRESS];
1299         struct ifmultiaddr  *ifma;
1300         int mcnt = 0;
1301         struct ifnet   *ifp = &adapter->interface_data.ac_if;
1302     
1303         IOCTL_DEBUGOUT("em_set_multi: begin");
1304  
1305         if (adapter->hw.mac_type == em_82542_rev2_0) {
1306                 reg_rctl = E1000_READ_REG(&adapter->hw, RCTL);
1307                 if (adapter->hw.pci_cmd_word & CMD_MEM_WRT_INVALIDATE) { 
1308                         em_pci_clear_mwi(&adapter->hw);
1309                 }
1310                 reg_rctl |= E1000_RCTL_RST;
1311                 E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1312                 msec_delay(5);
1313         }
1314         
1315 #if defined(__DragonFly__) || __FreeBSD_version < 500000
1316         LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1317 #else
1318         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1319 #endif  
1320                 if (ifma->ifma_addr->sa_family != AF_LINK)
1321                         continue;
1322  
1323                 if (mcnt == MAX_NUM_MULTICAST_ADDRESSES) break;
1324
1325                 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1326                       &mta[mcnt*ETH_LENGTH_OF_ADDRESS], ETH_LENGTH_OF_ADDRESS);
1327                 mcnt++;
1328         }
1329
1330         if (mcnt >= MAX_NUM_MULTICAST_ADDRESSES) {
1331                 reg_rctl = E1000_READ_REG(&adapter->hw, RCTL);
1332                 reg_rctl |= E1000_RCTL_MPE;
1333                 E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1334         } else
1335                 em_mc_addr_list_update(&adapter->hw, mta, mcnt, 0);
1336
1337         if (adapter->hw.mac_type == em_82542_rev2_0) {
1338                 reg_rctl = E1000_READ_REG(&adapter->hw, RCTL);
1339                 reg_rctl &= ~E1000_RCTL_RST;
1340                 E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1341                 msec_delay(5);
1342                 if (adapter->hw.pci_cmd_word & CMD_MEM_WRT_INVALIDATE) {
1343                         em_pci_set_mwi(&adapter->hw);
1344                 }
1345         }
1346
1347         return;
1348 }
1349
1350
1351 /*********************************************************************
1352  *  Timer routine
1353  *
1354  *  This routine checks for link status and updates statistics.
1355  *
1356  **********************************************************************/
1357
1358 static void
1359 em_local_timer(void *arg)
1360 {
1361         int s;
1362         struct ifnet   *ifp;
1363         struct adapter * adapter = arg;
1364         ifp = &adapter->interface_data.ac_if;
1365
1366         s = splimp();
1367
1368         em_check_for_link(&adapter->hw);
1369         em_print_link_status(adapter);
1370         em_update_stats_counters(adapter);   
1371         if (em_display_debug_stats && ifp->if_flags & IFF_RUNNING) {
1372                 em_print_hw_stats(adapter);
1373         }
1374         em_smartspeed(adapter);
1375
1376         adapter->timer_handle = timeout(em_local_timer, adapter, 2*hz);
1377
1378         splx(s);
1379         return;
1380 }
1381
1382 static void
1383 em_print_link_status(struct adapter * adapter)
1384 {
1385         if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU) {
1386                 if (adapter->link_active == 0) {
1387                         em_get_speed_and_duplex(&adapter->hw, 
1388                                                 &adapter->link_speed, 
1389                                                 &adapter->link_duplex);
1390                         printf("em%d: Link is up %d Mbps %s\n",
1391                                adapter->unit,
1392                                adapter->link_speed,
1393                                ((adapter->link_duplex == FULL_DUPLEX) ?
1394                                 "Full Duplex" : "Half Duplex"));
1395                         adapter->link_active = 1;
1396                         adapter->smartspeed = 0;
1397                 }
1398         } else {
1399                 if (adapter->link_active == 1) {
1400                         adapter->link_speed = 0;
1401                         adapter->link_duplex = 0;
1402                         printf("em%d: Link is Down\n", adapter->unit);
1403                         adapter->link_active = 0;
1404                 }
1405         }
1406
1407         return;
1408 }
1409
1410 /*********************************************************************
1411  *
1412  *  This routine disables all traffic on the adapter by issuing a
1413  *  global reset on the MAC and deallocates TX/RX buffers. 
1414  *
1415  **********************************************************************/
1416
1417 static void
1418 em_stop(void *arg)
1419 {
1420         struct ifnet   *ifp;
1421         struct adapter * adapter = arg;
1422         ifp = &adapter->interface_data.ac_if;
1423
1424         INIT_DEBUGOUT("em_stop: begin\n");
1425         em_disable_intr(adapter);
1426         em_reset_hw(&adapter->hw);
1427         untimeout(em_local_timer, adapter, adapter->timer_handle);      
1428         untimeout(em_82547_move_tail, adapter, 
1429                   adapter->tx_fifo_timer_handle);
1430         em_free_transmit_structures(adapter);
1431         em_free_receive_structures(adapter);
1432
1433
1434         /* Tell the stack that the interface is no longer active */
1435         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1436
1437         return;
1438 }
1439
1440
1441 /*********************************************************************
1442  *
1443  *  Determine hardware revision.
1444  *
1445  **********************************************************************/
1446 static void
1447 em_identify_hardware(struct adapter * adapter)
1448 {
1449         device_t dev = adapter->dev;
1450
1451         /* Make sure our PCI config space has the necessary stuff set */
1452         adapter->hw.pci_cmd_word = pci_read_config(dev, PCIR_COMMAND, 2);
1453         if (!((adapter->hw.pci_cmd_word & PCIM_CMD_BUSMASTEREN) &&
1454               (adapter->hw.pci_cmd_word & PCIM_CMD_MEMEN))) {
1455                 printf("em%d: Memory Access and/or Bus Master bits were not set!\n", 
1456                        adapter->unit);
1457                 adapter->hw.pci_cmd_word |= 
1458                 (PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN);
1459                 pci_write_config(dev, PCIR_COMMAND, adapter->hw.pci_cmd_word, 2);
1460         }
1461
1462         /* Save off the information about this board */
1463         adapter->hw.vendor_id = pci_get_vendor(dev);
1464         adapter->hw.device_id = pci_get_device(dev);
1465         adapter->hw.revision_id = pci_read_config(dev, PCIR_REVID, 1);
1466         adapter->hw.subsystem_vendor_id = pci_read_config(dev, PCIR_SUBVEND_0, 2);
1467         adapter->hw.subsystem_id = pci_read_config(dev, PCIR_SUBDEV_0, 2);
1468
1469         /* Identify the MAC */
1470         if (em_set_mac_type(&adapter->hw))
1471                 printf("em%d: Unknown MAC Type\n", adapter->unit);
1472
1473         return;
1474 }
1475
1476 static int
1477 em_allocate_pci_resources(struct adapter * adapter)
1478 {
1479         int             i, val, rid;
1480         device_t        dev = adapter->dev;
1481
1482         rid = EM_MMBA;
1483         adapter->res_memory = bus_alloc_resource(dev, SYS_RES_MEMORY,
1484                                                  &rid, 0, ~0, 1,
1485                                                  RF_ACTIVE);
1486         if (!(adapter->res_memory)) {
1487                 printf("em%d: Unable to allocate bus resource: memory\n", 
1488                        adapter->unit);
1489                 return(ENXIO);
1490         }
1491         adapter->osdep.mem_bus_space_tag = 
1492         rman_get_bustag(adapter->res_memory);
1493         adapter->osdep.mem_bus_space_handle = 
1494         rman_get_bushandle(adapter->res_memory);
1495         adapter->hw.hw_addr = (uint8_t *)&adapter->osdep.mem_bus_space_handle;
1496
1497
1498         if (adapter->hw.mac_type > em_82543) {
1499                 /* Figure our where our IO BAR is ? */
1500                 rid = EM_MMBA;
1501                 for (i = 0; i < 5; i++) {
1502                         val = pci_read_config(dev, rid, 4);
1503                         if (val & 0x00000001) {
1504                                 adapter->io_rid = rid;
1505                                 break;
1506                         }
1507                         rid += 4;
1508                 }
1509
1510                 adapter->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,  
1511                                                          &adapter->io_rid, 0, ~0, 1,
1512                                                          RF_ACTIVE);   
1513                 if (!(adapter->res_ioport)) {
1514                         printf("em%d: Unable to allocate bus resource: ioport\n",
1515                                adapter->unit);
1516                         return(ENXIO);  
1517                 }
1518
1519                 adapter->hw.io_base =
1520                 rman_get_start(adapter->res_ioport);
1521         }
1522
1523         rid = 0x0;
1524         adapter->res_interrupt = bus_alloc_resource(dev, SYS_RES_IRQ,
1525                                                     &rid, 0, ~0, 1,
1526                                                     RF_SHAREABLE | RF_ACTIVE);
1527         if (!(adapter->res_interrupt)) {
1528                 printf("em%d: Unable to allocate bus resource: interrupt\n", 
1529                        adapter->unit);
1530                 return(ENXIO);
1531         }
1532         if (bus_setup_intr(dev, adapter->res_interrupt, INTR_TYPE_NET,
1533                            (void (*)(void *)) em_intr, adapter,
1534                            &adapter->int_handler_tag)) {
1535                 printf("em%d: Error registering interrupt handler!\n", 
1536                        adapter->unit);
1537                 return(ENXIO);
1538         }
1539
1540         adapter->hw.back = &adapter->osdep;
1541
1542         return(0);
1543 }
1544
1545 static void
1546 em_free_pci_resources(struct adapter * adapter)
1547 {
1548         device_t dev = adapter->dev;
1549
1550         if (adapter->res_interrupt != NULL) {
1551                 bus_teardown_intr(dev, adapter->res_interrupt, 
1552                                   adapter->int_handler_tag);
1553                 bus_release_resource(dev, SYS_RES_IRQ, 0, 
1554                                      adapter->res_interrupt);
1555         }
1556         if (adapter->res_memory != NULL) {
1557                 bus_release_resource(dev, SYS_RES_MEMORY, EM_MMBA, 
1558                                      adapter->res_memory);
1559         }
1560
1561         if (adapter->res_ioport != NULL) {
1562                 bus_release_resource(dev, SYS_RES_IOPORT, adapter->io_rid, 
1563                                      adapter->res_ioport);
1564         }
1565         return;
1566 }
1567
1568 /*********************************************************************
1569  *
1570  *  Initialize the hardware to a configuration as specified by the
1571  *  adapter structure. The controller is reset, the EEPROM is
1572  *  verified, the MAC address is set, then the shared initialization
1573  *  routines are called.
1574  *
1575  **********************************************************************/
1576 static int
1577 em_hardware_init(struct adapter * adapter)
1578 {
1579         /* Issue a global reset */
1580         em_reset_hw(&adapter->hw);
1581
1582         /* When hardware is reset, fifo_head is also reset */
1583         adapter->tx_fifo_head = 0;
1584
1585         /* Make sure we have a good EEPROM before we read from it */
1586         if (em_validate_eeprom_checksum(&adapter->hw) < 0) {
1587                 printf("em%d: The EEPROM Checksum Is Not Valid\n",
1588                        adapter->unit);
1589                 return(EIO);
1590         }
1591
1592         if (em_read_part_num(&adapter->hw, &(adapter->part_num)) < 0) {
1593                 printf("em%d: EEPROM read error while reading part number\n",
1594                        adapter->unit);
1595                 return(EIO);
1596         }
1597
1598         if (em_init_hw(&adapter->hw) < 0) {
1599                 printf("em%d: Hardware Initialization Failed",
1600                        adapter->unit);
1601                 return(EIO);
1602         }
1603
1604         em_check_for_link(&adapter->hw);
1605         if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU)
1606                 adapter->link_active = 1;
1607         else
1608                 adapter->link_active = 0;
1609
1610         if (adapter->link_active) {
1611                 em_get_speed_and_duplex(&adapter->hw, 
1612                                         &adapter->link_speed, 
1613                                         &adapter->link_duplex);
1614         } else {
1615                 adapter->link_speed = 0;
1616                 adapter->link_duplex = 0;
1617         }
1618
1619         return(0);
1620 }
1621
1622 /*********************************************************************
1623  *
1624  *  Setup networking device structure and register an interface.
1625  *
1626  **********************************************************************/
1627 static void
1628 em_setup_interface(device_t dev, struct adapter * adapter)
1629 {
1630         struct ifnet   *ifp;
1631         INIT_DEBUGOUT("em_setup_interface: begin");
1632
1633         ifp = &adapter->interface_data.ac_if;
1634         if_initname(ifp, "em", adapter->unit);
1635         ifp->if_mtu = ETHERMTU;
1636         ifp->if_output = ether_output;
1637         ifp->if_baudrate = 1000000000;
1638         ifp->if_init =  em_init;
1639         ifp->if_softc = adapter;
1640         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1641         ifp->if_ioctl = em_ioctl;
1642         ifp->if_start = em_start;
1643         ifp->if_watchdog = em_watchdog;
1644         ifp->if_snd.ifq_maxlen = adapter->num_tx_desc - 1;
1645
1646         ether_ifattach(ifp, adapter->interface_data.ac_enaddr);
1647
1648         if (adapter->hw.mac_type >= em_82543) {
1649                 ifp->if_capabilities = IFCAP_HWCSUM;
1650                 ifp->if_capenable = ifp->if_capabilities;
1651         }
1652
1653         /*
1654          * Tell the upper layer(s) we support long frames.
1655          */
1656         ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1657 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
1658         ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU;
1659 #endif
1660
1661
1662         /* 
1663          * Specify the media types supported by this adapter and register
1664          * callbacks to update media and link information
1665          */
1666         ifmedia_init(&adapter->media, IFM_IMASK, em_media_change,
1667                      em_media_status);
1668         if (adapter->hw.media_type == em_media_type_fiber) {
1669                 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_SX | IFM_FDX, 
1670                             0, NULL);
1671                 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_SX, 
1672                             0, NULL);
1673         } else {
1674                 ifmedia_add(&adapter->media, IFM_ETHER | IFM_10_T, 0, NULL);
1675                 ifmedia_add(&adapter->media, IFM_ETHER | IFM_10_T | IFM_FDX, 
1676                             0, NULL);
1677                 ifmedia_add(&adapter->media, IFM_ETHER | IFM_100_TX, 
1678                             0, NULL);
1679                 ifmedia_add(&adapter->media, IFM_ETHER | IFM_100_TX | IFM_FDX, 
1680                             0, NULL);
1681 #if defined(__DragonFly__) || __FreeBSD_version < 500000 
1682                 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_TX | IFM_FDX, 
1683                             0, NULL);
1684                 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_TX, 0, NULL);
1685 #else
1686                 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_T | IFM_FDX, 
1687                             0, NULL);
1688                 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_T, 0, NULL);
1689 #endif
1690         }
1691         ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
1692         ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
1693
1694         return;
1695 }
1696
1697
1698 /*********************************************************************
1699  *
1700  *  Workaround for SmartSpeed on 82541 and 82547 controllers
1701  *
1702  **********************************************************************/        
1703 static void
1704 em_smartspeed(struct adapter *adapter)
1705 {
1706         uint16_t phy_tmp;
1707  
1708         if(adapter->link_active || (adapter->hw.phy_type != em_phy_igp) || 
1709            !adapter->hw.autoneg || !(adapter->hw.autoneg_advertised & ADVERTISE_1000_FULL))
1710                 return;
1711
1712         if(adapter->smartspeed == 0) {
1713                 /* If Master/Slave config fault is asserted twice,
1714                  * we assume back-to-back */
1715                 em_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_tmp);
1716                 if(!(phy_tmp & SR_1000T_MS_CONFIG_FAULT)) return;
1717                 em_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_tmp);
1718                 if(phy_tmp & SR_1000T_MS_CONFIG_FAULT) {
1719                         em_read_phy_reg(&adapter->hw, PHY_1000T_CTRL,
1720                                         &phy_tmp);
1721                         if(phy_tmp & CR_1000T_MS_ENABLE) {
1722                                 phy_tmp &= ~CR_1000T_MS_ENABLE;
1723                                 em_write_phy_reg(&adapter->hw,
1724                                                     PHY_1000T_CTRL, phy_tmp);
1725                                 adapter->smartspeed++;
1726                                 if(adapter->hw.autoneg &&
1727                                    !em_phy_setup_autoneg(&adapter->hw) &&
1728                                    !em_read_phy_reg(&adapter->hw, PHY_CTRL,
1729                                                        &phy_tmp)) {
1730                                         phy_tmp |= (MII_CR_AUTO_NEG_EN |  
1731                                                     MII_CR_RESTART_AUTO_NEG);
1732                                         em_write_phy_reg(&adapter->hw,
1733                                                          PHY_CTRL, phy_tmp);
1734                                 }
1735                         }
1736                 }
1737                 return;
1738         } else if(adapter->smartspeed == EM_SMARTSPEED_DOWNSHIFT) {
1739                 /* If still no link, perhaps using 2/3 pair cable */
1740                 em_read_phy_reg(&adapter->hw, PHY_1000T_CTRL, &phy_tmp);
1741                 phy_tmp |= CR_1000T_MS_ENABLE;
1742                 em_write_phy_reg(&adapter->hw, PHY_1000T_CTRL, phy_tmp);
1743                 if(adapter->hw.autoneg &&
1744                    !em_phy_setup_autoneg(&adapter->hw) &&
1745                    !em_read_phy_reg(&adapter->hw, PHY_CTRL, &phy_tmp)) {
1746                         phy_tmp |= (MII_CR_AUTO_NEG_EN |
1747                                     MII_CR_RESTART_AUTO_NEG);
1748                         em_write_phy_reg(&adapter->hw, PHY_CTRL, phy_tmp);
1749                 }
1750         }
1751         /* Restart process after EM_SMARTSPEED_MAX iterations */
1752         if(adapter->smartspeed++ == EM_SMARTSPEED_MAX)
1753                 adapter->smartspeed = 0;
1754
1755         return;
1756 }
1757
1758
1759 /*********************************************************************
1760  *
1761  *  Allocate memory for tx_buffer structures. The tx_buffer stores all 
1762  *  the information needed to transmit a packet on the wire. 
1763  *
1764  **********************************************************************/
1765 static int
1766 em_allocate_transmit_structures(struct adapter * adapter)
1767 {
1768         if (!(adapter->tx_buffer_area =
1769               (struct em_buffer *) malloc(sizeof(struct em_buffer) *
1770                                              adapter->num_tx_desc, M_DEVBUF,
1771                                              M_NOWAIT))) {
1772                 printf("em%d: Unable to allocate tx_buffer memory\n", 
1773                        adapter->unit);
1774                 return ENOMEM;
1775         }
1776
1777         bzero(adapter->tx_buffer_area,
1778               sizeof(struct em_buffer) * adapter->num_tx_desc);
1779
1780         return 0;
1781 }
1782
1783 /*********************************************************************
1784  *
1785  *  Allocate and initialize transmit structures. 
1786  *
1787  **********************************************************************/
1788 static int
1789 em_setup_transmit_structures(struct adapter * adapter)
1790 {
1791         if (em_allocate_transmit_structures(adapter))
1792                 return ENOMEM;
1793
1794         bzero((void *) adapter->tx_desc_base,
1795               (sizeof(struct em_tx_desc)) * adapter->num_tx_desc);
1796                           
1797         adapter->next_avail_tx_desc = 0;
1798         adapter->oldest_used_tx_desc = 0;
1799
1800         /* Set number of descriptors available */
1801         adapter->num_tx_desc_avail = adapter->num_tx_desc;
1802
1803         /* Set checksum context */
1804         adapter->active_checksum_context = OFFLOAD_NONE;
1805
1806         return 0;
1807 }
1808
1809 /*********************************************************************
1810  *
1811  *  Enable transmit unit.
1812  *
1813  **********************************************************************/
1814 static void
1815 em_initialize_transmit_unit(struct adapter * adapter)
1816 {
1817         u_int32_t       reg_tctl;
1818         u_int32_t       reg_tipg = 0;
1819         u_int64_t       tdba = vtophys((vm_offset_t)adapter->tx_desc_base);
1820
1821         /* Setup the Base and Length of the Tx Descriptor Ring */
1822         E1000_WRITE_REG(&adapter->hw, TDBAL,
1823                         (tdba & 0x00000000ffffffffULL));
1824         E1000_WRITE_REG(&adapter->hw, TDBAH, (tdba >> 32));
1825         E1000_WRITE_REG(&adapter->hw, TDLEN, 
1826                         adapter->num_tx_desc *
1827                         sizeof(struct em_tx_desc));
1828
1829         /* Setup the HW Tx Head and Tail descriptor pointers */
1830         E1000_WRITE_REG(&adapter->hw, TDH, 0);
1831         E1000_WRITE_REG(&adapter->hw, TDT, 0);
1832
1833
1834         HW_DEBUGOUT2("Base = %x, Length = %x\n", 
1835                      E1000_READ_REG(&adapter->hw, TDBAL),
1836                      E1000_READ_REG(&adapter->hw, TDLEN));
1837
1838
1839         /* Set the default values for the Tx Inter Packet Gap timer */
1840         switch (adapter->hw.mac_type) {
1841         case em_82542_rev2_0:
1842         case em_82542_rev2_1:
1843                 reg_tipg = DEFAULT_82542_TIPG_IPGT;
1844                 reg_tipg |= DEFAULT_82542_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
1845                 reg_tipg |= DEFAULT_82542_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
1846                 break;
1847         default:
1848                 if (adapter->hw.media_type == em_media_type_fiber)
1849                         reg_tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
1850                 else
1851                         reg_tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
1852                 reg_tipg |= DEFAULT_82543_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
1853                 reg_tipg |= DEFAULT_82543_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
1854         }
1855
1856         E1000_WRITE_REG(&adapter->hw, TIPG, reg_tipg);
1857         E1000_WRITE_REG(&adapter->hw, TIDV, adapter->tx_int_delay);
1858         if(adapter->hw.mac_type >= em_82540)
1859                 E1000_WRITE_REG(&adapter->hw, TADV, adapter->tx_abs_int_delay);
1860
1861         /* Program the Transmit Control Register */
1862         reg_tctl = E1000_TCTL_PSP | E1000_TCTL_EN |
1863                    (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
1864         if (adapter->link_duplex == 1) {
1865                 reg_tctl |= E1000_FDX_COLLISION_DISTANCE << E1000_COLD_SHIFT;
1866         } else {
1867                 reg_tctl |= E1000_HDX_COLLISION_DISTANCE << E1000_COLD_SHIFT;
1868         }
1869         E1000_WRITE_REG(&adapter->hw, TCTL, reg_tctl);
1870
1871         /* Setup Transmit Descriptor Settings for this adapter */   
1872         adapter->txd_cmd = E1000_TXD_CMD_IFCS | E1000_TXD_CMD_RS;
1873
1874         if (adapter->tx_int_delay > 0)
1875                 adapter->txd_cmd |= E1000_TXD_CMD_IDE;
1876
1877         return;
1878 }
1879
1880 /*********************************************************************
1881  *
1882  *  Free all transmit related data structures.
1883  *
1884  **********************************************************************/
1885 static void
1886 em_free_transmit_structures(struct adapter * adapter)
1887 {
1888         struct em_buffer   *tx_buffer;
1889         int             i;
1890
1891         INIT_DEBUGOUT("free_transmit_structures: begin");
1892
1893         if (adapter->tx_buffer_area != NULL) {
1894                 tx_buffer = adapter->tx_buffer_area;
1895                 for (i = 0; i < adapter->num_tx_desc; i++, tx_buffer++) {
1896                         if (tx_buffer->m_head != NULL)
1897                                 m_freem(tx_buffer->m_head);
1898                         tx_buffer->m_head = NULL;
1899                 }
1900         }
1901         if (adapter->tx_buffer_area != NULL) {
1902                 free(adapter->tx_buffer_area, M_DEVBUF);
1903                 adapter->tx_buffer_area = NULL;
1904         }
1905         return;
1906 }
1907
1908 /*********************************************************************
1909  *
1910  *  The offload context needs to be set when we transfer the first
1911  *  packet of a particular protocol (TCP/UDP). We change the
1912  *  context only if the protocol type changes.
1913  *
1914  **********************************************************************/
1915 static void
1916 em_transmit_checksum_setup(struct adapter * adapter,
1917                            struct mbuf *mp,
1918                            u_int32_t *txd_upper,
1919                            u_int32_t *txd_lower) 
1920 {
1921         struct em_context_desc *TXD;
1922         struct em_buffer *tx_buffer;
1923         int curr_txd;
1924
1925         if (mp->m_pkthdr.csum_flags) {
1926
1927                 if (mp->m_pkthdr.csum_flags & CSUM_TCP) {
1928                         *txd_upper = E1000_TXD_POPTS_TXSM << 8;
1929                         *txd_lower = E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
1930                         if (adapter->active_checksum_context == OFFLOAD_TCP_IP)
1931                                 return;
1932                         else
1933                                 adapter->active_checksum_context = OFFLOAD_TCP_IP;
1934
1935                 } else if (mp->m_pkthdr.csum_flags & CSUM_UDP) {
1936                         *txd_upper = E1000_TXD_POPTS_TXSM << 8;
1937                         *txd_lower = E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
1938                         if (adapter->active_checksum_context == OFFLOAD_UDP_IP)
1939                                 return;
1940                         else
1941                                 adapter->active_checksum_context = OFFLOAD_UDP_IP;
1942                 } else {
1943                         *txd_upper = 0;
1944                         *txd_lower = 0;
1945                         return;
1946                 }
1947         } else {
1948                 *txd_upper = 0;
1949                 *txd_lower = 0;
1950                 return;
1951         }
1952
1953         /* If we reach this point, the checksum offload context
1954          * needs to be reset.
1955          */
1956         curr_txd = adapter->next_avail_tx_desc;
1957         tx_buffer = &adapter->tx_buffer_area[curr_txd];
1958         TXD = (struct em_context_desc *) &adapter->tx_desc_base[curr_txd];
1959
1960         TXD->lower_setup.ip_fields.ipcss = ETHER_HDR_LEN;
1961         TXD->lower_setup.ip_fields.ipcso = 
1962                 ETHER_HDR_LEN + offsetof(struct ip, ip_sum);
1963         TXD->lower_setup.ip_fields.ipcse = 
1964                 ETHER_HDR_LEN + sizeof(struct ip) - 1;
1965
1966         TXD->upper_setup.tcp_fields.tucss = 
1967                 ETHER_HDR_LEN + sizeof(struct ip);
1968         TXD->upper_setup.tcp_fields.tucse = 0;
1969
1970         if (adapter->active_checksum_context == OFFLOAD_TCP_IP) {
1971                 TXD->upper_setup.tcp_fields.tucso = 
1972                         ETHER_HDR_LEN + sizeof(struct ip) + 
1973                         offsetof(struct tcphdr, th_sum);
1974         } else if (adapter->active_checksum_context == OFFLOAD_UDP_IP) {
1975                 TXD->upper_setup.tcp_fields.tucso = 
1976                         ETHER_HDR_LEN + sizeof(struct ip) + 
1977                         offsetof(struct udphdr, uh_sum);
1978         }
1979
1980         TXD->tcp_seg_setup.data = 0;
1981         TXD->cmd_and_length = (adapter->txd_cmd | E1000_TXD_CMD_DEXT);
1982
1983         tx_buffer->m_head = NULL;
1984
1985         if (++curr_txd == adapter->num_tx_desc)
1986                 curr_txd = 0;
1987
1988         adapter->num_tx_desc_avail--;
1989         adapter->next_avail_tx_desc = curr_txd;
1990
1991         return;
1992 }
1993
1994 /**********************************************************************
1995  *
1996  *  Examine each tx_buffer in the used queue. If the hardware is done
1997  *  processing the packet then free associated resources. The
1998  *  tx_buffer is put back on the free queue.
1999  *
2000  **********************************************************************/
2001 static void
2002 em_clean_transmit_interrupts(struct adapter * adapter)
2003 {
2004         int s;
2005         int i, num_avail;
2006         struct em_buffer *tx_buffer;
2007         struct em_tx_desc   *tx_desc;
2008         struct ifnet   *ifp = &adapter->interface_data.ac_if;
2009
2010         if (adapter->num_tx_desc_avail == adapter->num_tx_desc)
2011                 return;
2012
2013         s = splimp();
2014 #ifdef DBG_STATS
2015         adapter->clean_tx_interrupts++;
2016 #endif
2017         num_avail = adapter->num_tx_desc_avail; 
2018         i = adapter->oldest_used_tx_desc;
2019
2020         tx_buffer = &adapter->tx_buffer_area[i];
2021         tx_desc = &adapter->tx_desc_base[i];
2022
2023         while(tx_desc->upper.fields.status & E1000_TXD_STAT_DD) {
2024
2025                 tx_desc->upper.data = 0;
2026                 num_avail++;                        
2027
2028                 if (tx_buffer->m_head) {
2029                         ifp->if_opackets++;
2030                         m_freem(tx_buffer->m_head);
2031                         tx_buffer->m_head = NULL;
2032                 }
2033                 
2034                 if (++i == adapter->num_tx_desc)
2035                         i = 0;
2036
2037                 tx_buffer = &adapter->tx_buffer_area[i];
2038                 tx_desc = &adapter->tx_desc_base[i];
2039         }
2040
2041         adapter->oldest_used_tx_desc = i;
2042
2043         /*
2044          * If we have enough room, clear IFF_OACTIVE to tell the stack
2045          * that it is OK to send packets.
2046          * If there are no pending descriptors, clear the timeout. Otherwise,
2047          * if some descriptors have been freed, restart the timeout.
2048          */
2049         if (num_avail > EM_TX_CLEANUP_THRESHOLD) {
2050                 ifp->if_flags &= ~IFF_OACTIVE;
2051                 if (num_avail == adapter->num_tx_desc)
2052                         ifp->if_timer = 0;
2053                 else if (num_avail == adapter->num_tx_desc_avail)
2054                         ifp->if_timer = EM_TX_TIMEOUT;
2055         }
2056         adapter->num_tx_desc_avail = num_avail;
2057         splx(s);
2058         return;
2059 }
2060
2061 /*********************************************************************
2062  *
2063  *  Get a buffer from system mbuf buffer pool.
2064  *
2065  **********************************************************************/
2066 static int
2067 em_get_buf(int i, struct adapter *adapter,
2068            struct mbuf *nmp)
2069 {
2070         register struct mbuf    *mp = nmp;
2071         struct ifnet   *ifp;
2072
2073         ifp = &adapter->interface_data.ac_if;
2074
2075         if (mp == NULL) {
2076                 MGETHDR(mp, M_DONTWAIT, MT_DATA);
2077                 if (mp == NULL) {
2078                         adapter->mbuf_alloc_failed++;
2079                         return(ENOBUFS);
2080                 }
2081                 MCLGET(mp, M_DONTWAIT);
2082                 if ((mp->m_flags & M_EXT) == 0) {
2083                         m_freem(mp);
2084                         adapter->mbuf_cluster_failed++;
2085                         return(ENOBUFS);
2086                 }
2087                 mp->m_len = mp->m_pkthdr.len = MCLBYTES;
2088         } else {
2089                 mp->m_len = mp->m_pkthdr.len = MCLBYTES;
2090                 mp->m_data = mp->m_ext.ext_buf;
2091                 mp->m_next = NULL;
2092         }
2093
2094         if (ifp->if_mtu <= ETHERMTU) {
2095                 m_adj(mp, ETHER_ALIGN);
2096         }
2097  
2098         adapter->rx_buffer_area[i].m_head = mp;
2099         adapter->rx_desc_base[i].buffer_addr =
2100                 vtophys(mtod(mp, vm_offset_t));
2101
2102         return(0);
2103 }
2104
2105 /*********************************************************************
2106  *
2107  *  Allocate memory for rx_buffer structures. Since we use one 
2108  *  rx_buffer per received packet, the maximum number of rx_buffer's 
2109  *  that we'll need is equal to the number of receive descriptors 
2110  *  that we've allocated.
2111  *
2112  **********************************************************************/
2113 static int
2114 em_allocate_receive_structures(struct adapter * adapter)
2115 {
2116         int             i;
2117
2118         if (!(adapter->rx_buffer_area =
2119               (struct em_buffer *) malloc(sizeof(struct em_buffer) *
2120                                              adapter->num_rx_desc, M_DEVBUF,
2121                                              M_NOWAIT))) {
2122                 printf("em%d: Unable to allocate rx_buffer memory\n", 
2123                        adapter->unit);
2124                 return(ENOMEM);
2125         }
2126
2127         bzero(adapter->rx_buffer_area,
2128               sizeof(struct em_buffer) * adapter->num_rx_desc);
2129
2130         for (i = 0; i < adapter->num_rx_desc; i++) {
2131                 if (em_get_buf(i, adapter, NULL) == ENOBUFS) {
2132                         adapter->rx_buffer_area[i].m_head = NULL;
2133                         adapter->rx_desc_base[i].buffer_addr = 0;
2134                         return(ENOBUFS);
2135                 }
2136         }
2137
2138         return(0);
2139 }
2140
2141 /*********************************************************************
2142  *
2143  *  Allocate and initialize receive structures.
2144  *  
2145  **********************************************************************/
2146 static int
2147 em_setup_receive_structures(struct adapter * adapter)
2148 {
2149         bzero((void *) adapter->rx_desc_base,
2150               (sizeof(struct em_rx_desc)) * adapter->num_rx_desc);
2151
2152         if (em_allocate_receive_structures(adapter))
2153                 return ENOMEM;
2154
2155         /* Setup our descriptor pointers */
2156         adapter->next_rx_desc_to_check = 0;
2157         return(0);
2158 }
2159
2160 /*********************************************************************
2161  *
2162  *  Enable receive unit.
2163  *  
2164  **********************************************************************/
2165 static void
2166 em_initialize_receive_unit(struct adapter * adapter)
2167 {
2168         u_int32_t       reg_rctl;
2169         u_int32_t       reg_rxcsum;
2170         struct ifnet    *ifp;
2171         u_int64_t       rdba = vtophys((vm_offset_t)adapter->rx_desc_base);
2172
2173         ifp = &adapter->interface_data.ac_if;
2174
2175         /* Make sure receives are disabled while setting up the descriptor ring */
2176         E1000_WRITE_REG(&adapter->hw, RCTL, 0);
2177
2178         /* Set the Receive Delay Timer Register */
2179         E1000_WRITE_REG(&adapter->hw, RDTR, 
2180                         adapter->rx_int_delay | E1000_RDT_FPDB);
2181
2182         if(adapter->hw.mac_type >= em_82540) {
2183                 E1000_WRITE_REG(&adapter->hw, RADV, adapter->rx_abs_int_delay);
2184
2185                 /* Set the interrupt throttling rate.  Value is calculated
2186                  * as DEFAULT_ITR = 1/(MAX_INTS_PER_SEC * 256ns) */
2187 #define MAX_INTS_PER_SEC        8000
2188 #define DEFAULT_ITR             1000000000/(MAX_INTS_PER_SEC * 256)
2189                 E1000_WRITE_REG(&adapter->hw, ITR, DEFAULT_ITR);
2190         }       
2191
2192         /* Setup the Base and Length of the Rx Descriptor Ring */
2193         E1000_WRITE_REG(&adapter->hw, RDBAL, 
2194                         (rdba & 0x00000000ffffffffULL));
2195         E1000_WRITE_REG(&adapter->hw, RDBAH, (rdba >> 32));
2196         E1000_WRITE_REG(&adapter->hw, RDLEN, 
2197                         adapter->num_rx_desc *
2198                         sizeof(struct em_rx_desc));
2199
2200         /* Setup the HW Rx Head and Tail Descriptor Pointers */
2201         E1000_WRITE_REG(&adapter->hw, RDH, 0);
2202         E1000_WRITE_REG(&adapter->hw, RDT, adapter->num_rx_desc - 1);
2203
2204         /* Setup the Receive Control Register */
2205         reg_rctl = E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
2206                    E1000_RCTL_RDMTS_HALF |
2207                    (adapter->hw.mc_filter_type << E1000_RCTL_MO_SHIFT);
2208
2209         if (adapter->hw.tbi_compatibility_on == TRUE)
2210                 reg_rctl |= E1000_RCTL_SBP;
2211
2212
2213         switch (adapter->rx_buffer_len) {
2214         default:
2215         case EM_RXBUFFER_2048:
2216                 reg_rctl |= E1000_RCTL_SZ_2048;
2217                 break;
2218         case EM_RXBUFFER_4096:
2219                 reg_rctl |= E1000_RCTL_SZ_4096 | E1000_RCTL_BSEX | E1000_RCTL_LPE;
2220                 break;            
2221         case EM_RXBUFFER_8192:
2222                 reg_rctl |= E1000_RCTL_SZ_8192 | E1000_RCTL_BSEX | E1000_RCTL_LPE;
2223                 break;
2224         case EM_RXBUFFER_16384:
2225                 reg_rctl |= E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX | E1000_RCTL_LPE;
2226                 break;
2227         }
2228
2229         if (ifp->if_mtu > ETHERMTU)
2230                 reg_rctl |= E1000_RCTL_LPE;
2231
2232         /* Enable 82543 Receive Checksum Offload for TCP and UDP */
2233         if ((adapter->hw.mac_type >= em_82543) && 
2234             (ifp->if_capenable & IFCAP_RXCSUM)) {
2235                 reg_rxcsum = E1000_READ_REG(&adapter->hw, RXCSUM);
2236                 reg_rxcsum |= (E1000_RXCSUM_IPOFL | E1000_RXCSUM_TUOFL);
2237                 E1000_WRITE_REG(&adapter->hw, RXCSUM, reg_rxcsum);
2238         }
2239
2240         /* Enable Receives */
2241         E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
2242
2243         return;
2244 }
2245
2246 /*********************************************************************
2247  *
2248  *  Free receive related data structures.
2249  *
2250  **********************************************************************/
2251 static void
2252 em_free_receive_structures(struct adapter *adapter)
2253 {
2254         struct em_buffer   *rx_buffer;
2255         int             i;
2256
2257         INIT_DEBUGOUT("free_receive_structures: begin");
2258
2259         if (adapter->rx_buffer_area != NULL) {
2260                 rx_buffer = adapter->rx_buffer_area;
2261                 for (i = 0; i < adapter->num_rx_desc; i++, rx_buffer++) {
2262                         if (rx_buffer->m_head != NULL)
2263                                 m_freem(rx_buffer->m_head);
2264                         rx_buffer->m_head = NULL;
2265                 }
2266         }
2267         if (adapter->rx_buffer_area != NULL) {
2268                 free(adapter->rx_buffer_area, M_DEVBUF);
2269                 adapter->rx_buffer_area = NULL;
2270         }
2271         return;
2272 }
2273
2274 /*********************************************************************
2275  *
2276  *  This routine executes in interrupt context. It replenishes
2277  *  the mbufs in the descriptor and sends data which has been
2278  *  dma'ed into host memory to upper layer.
2279  *
2280  *  We loop at most count times if count is > 0, or until done if
2281  *  count < 0.
2282  *
2283  *********************************************************************/
2284 static void
2285 em_process_receive_interrupts(struct adapter * adapter, int count)
2286 {
2287         struct ifnet        *ifp;
2288         struct mbuf         *mp;
2289 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2290         struct ether_header *eh;
2291 #endif
2292         u_int8_t            accept_frame = 0;
2293         u_int8_t            eop = 0;
2294         u_int16_t           len;
2295         int                 i;
2296
2297         /* Pointer to the receive descriptor being examined. */
2298         struct em_rx_desc   *current_desc;
2299
2300         ifp = &adapter->interface_data.ac_if;
2301         i = adapter->next_rx_desc_to_check;
2302         current_desc = &adapter->rx_desc_base[i];
2303
2304         if (!((current_desc->status) & E1000_RXD_STAT_DD)) {
2305 #ifdef DBG_STATS
2306                 adapter->no_pkts_avail++;
2307 #endif
2308                 return;
2309         }
2310
2311         while ((current_desc->status & E1000_RXD_STAT_DD) && (count != 0)) {
2312                 
2313                 mp = adapter->rx_buffer_area[i].m_head;
2314
2315                 accept_frame = 1;
2316                 if (current_desc->status & E1000_RXD_STAT_EOP) {
2317                         count--;
2318                         eop = 1;
2319                         len = current_desc->length - ETHER_CRC_LEN;
2320                 } else {
2321                         eop = 0;
2322                         len = current_desc->length;
2323                 }
2324
2325                 if (current_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK) {
2326                         u_int8_t            last_byte;
2327                         u_int32_t           pkt_len = current_desc->length;
2328
2329                         if (adapter->fmp != NULL)
2330                                 pkt_len += adapter->fmp->m_pkthdr.len; 
2331  
2332                         last_byte = *(mtod(mp, caddr_t) +
2333                                       current_desc->length - 1);                        
2334
2335                         if (TBI_ACCEPT(&adapter->hw, current_desc->status, 
2336                                        current_desc->errors, 
2337                                        pkt_len, last_byte)) {
2338                                 em_tbi_adjust_stats(&adapter->hw, 
2339                                                     &adapter->stats, 
2340                                                     pkt_len, 
2341                                                     adapter->hw.mac_addr);
2342                                 len--;
2343                         } 
2344                         else {
2345                                 accept_frame = 0;
2346                         }
2347                 }
2348
2349                 if (accept_frame) {
2350
2351                         if (em_get_buf(i, adapter, NULL) == ENOBUFS) {
2352                                 adapter->dropped_pkts++;
2353                                 em_get_buf(i, adapter, mp);
2354                                 if (adapter->fmp != NULL) 
2355                                         m_freem(adapter->fmp);
2356                                 adapter->fmp = NULL;
2357                                 adapter->lmp = NULL;
2358                                 break;
2359                         }
2360
2361                         /* Assign correct length to the current fragment */
2362                         mp->m_len = len;
2363
2364                         if (adapter->fmp == NULL) {
2365                                 mp->m_pkthdr.len = len;
2366                                 adapter->fmp = mp;       /* Store the first mbuf */
2367                                 adapter->lmp = mp;
2368                         } else {
2369                                 /* Chain mbuf's together */
2370                                 mp->m_flags &= ~M_PKTHDR;
2371                                 adapter->lmp->m_next = mp;
2372                                 adapter->lmp = adapter->lmp->m_next;
2373                                 adapter->fmp->m_pkthdr.len += len;
2374                         }
2375
2376                         if (eop) {
2377                                 adapter->fmp->m_pkthdr.rcvif = ifp;
2378                                 ifp->if_ipackets++;
2379
2380 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2381                                 eh = mtod(adapter->fmp, struct ether_header *);
2382                                 /* Remove ethernet header from mbuf */
2383                                 m_adj(adapter->fmp, sizeof(struct ether_header));
2384                                 em_receive_checksum(adapter, current_desc,
2385                                                     adapter->fmp);
2386                                 if (current_desc->status & E1000_RXD_STAT_VP)
2387                                         VLAN_INPUT_TAG(eh, adapter->fmp,
2388                                                        (current_desc->special & 
2389                                                         E1000_RXD_SPC_VLAN_MASK));
2390                                 else
2391                                         ether_input(ifp, eh, adapter->fmp);
2392 #else
2393
2394                                 em_receive_checksum(adapter, current_desc,
2395                                                     adapter->fmp);
2396                                 if (current_desc->status & E1000_RXD_STAT_VP)
2397                                         VLAN_INPUT_TAG(ifp, adapter->fmp,
2398                                                        (current_desc->special &
2399                                                         E1000_RXD_SPC_VLAN_MASK),
2400                                                        adapter->fmp = NULL);
2401  
2402                                 if (adapter->fmp != NULL)
2403                                         (*ifp->if_input)(ifp, adapter->fmp);
2404 #endif
2405                                 adapter->fmp = NULL;
2406                                 adapter->lmp = NULL;
2407                         }
2408                 } else {
2409                         adapter->dropped_pkts++;
2410                         em_get_buf(i, adapter, mp);
2411                         if (adapter->fmp != NULL) 
2412                                 m_freem(adapter->fmp);
2413                         adapter->fmp = NULL;
2414                         adapter->lmp = NULL;
2415                 }
2416
2417                 /* Zero out the receive descriptors status  */
2418                 current_desc->status = 0;
2419  
2420                 /* Advance the E1000's Receive Queue #0  "Tail Pointer". */
2421                 E1000_WRITE_REG(&adapter->hw, RDT, i);
2422
2423                 /* Advance our pointers to the next descriptor */
2424                 if (++i == adapter->num_rx_desc) {
2425                         i = 0;
2426                         current_desc = adapter->rx_desc_base;
2427                 } else
2428                         current_desc++;
2429         }
2430         adapter->next_rx_desc_to_check = i;
2431         return;
2432 }
2433
2434 /*********************************************************************
2435  *
2436  *  Verify that the hardware indicated that the checksum is valid. 
2437  *  Inform the stack about the status of checksum so that stack
2438  *  doesn't spend time verifying the checksum.
2439  *
2440  *********************************************************************/
2441 static void
2442 em_receive_checksum(struct adapter *adapter,
2443                     struct em_rx_desc *rx_desc,
2444                     struct mbuf *mp)
2445 {
2446         /* 82543 or newer only */
2447         if ((adapter->hw.mac_type < em_82543) ||
2448             /* Ignore Checksum bit is set */
2449             (rx_desc->status & E1000_RXD_STAT_IXSM)) {
2450                 mp->m_pkthdr.csum_flags = 0;
2451                 return;
2452         }
2453
2454         if (rx_desc->status & E1000_RXD_STAT_IPCS) {
2455                 /* Did it pass? */
2456                 if (!(rx_desc->errors & E1000_RXD_ERR_IPE)) {
2457                         /* IP Checksum Good */
2458                         mp->m_pkthdr.csum_flags = CSUM_IP_CHECKED;
2459                         mp->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2460
2461                 } else {
2462                         mp->m_pkthdr.csum_flags = 0;
2463                 }
2464         }
2465
2466         if (rx_desc->status & E1000_RXD_STAT_TCPCS) {
2467                 /* Did it pass? */        
2468                 if (!(rx_desc->errors & E1000_RXD_ERR_TCPE)) {
2469                         mp->m_pkthdr.csum_flags |= 
2470                         (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
2471                         mp->m_pkthdr.csum_data = htons(0xffff);
2472                 }
2473         }
2474
2475         return;
2476 }
2477
2478
2479 static void 
2480 em_enable_vlans(struct adapter *adapter)
2481 {
2482         uint32_t ctrl;
2483
2484         E1000_WRITE_REG(&adapter->hw, VET, ETHERTYPE_VLAN);
2485
2486         ctrl = E1000_READ_REG(&adapter->hw, CTRL);
2487         ctrl |= E1000_CTRL_VME; 
2488         E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
2489
2490         return;
2491 }
2492
2493 static void
2494 em_enable_intr(struct adapter * adapter)
2495 {
2496         E1000_WRITE_REG(&adapter->hw, IMS, (IMS_ENABLE_MASK));
2497         return;
2498 }
2499
2500 static void
2501 em_disable_intr(struct adapter *adapter)
2502 {
2503         E1000_WRITE_REG(&adapter->hw, IMC, 
2504                         (0xffffffff & ~E1000_IMC_RXSEQ));
2505         return;
2506 }
2507
2508 static int
2509 em_is_valid_ether_addr(u_int8_t *addr)
2510 {
2511         char zero_addr[6] = { 0, 0, 0, 0, 0, 0 };
2512
2513         if ((addr[0] & 1) || (!bcmp(addr, zero_addr, ETHER_ADDR_LEN))) {
2514                 return (FALSE);
2515         }
2516
2517         return(TRUE);
2518 }
2519
2520 void 
2521 em_write_pci_cfg(struct em_hw *hw,
2522                       uint32_t reg,
2523                       uint16_t *value)
2524 {
2525         pci_write_config(((struct em_osdep *)hw->back)->dev, reg, 
2526                          *value, 2);
2527 }
2528
2529 void 
2530 em_read_pci_cfg(struct em_hw *hw, uint32_t reg,
2531                      uint16_t *value)
2532 {
2533         *value = pci_read_config(((struct em_osdep *)hw->back)->dev,
2534                                  reg, 2);
2535         return;
2536 }
2537
2538 void
2539 em_pci_set_mwi(struct em_hw *hw)
2540 {
2541         pci_write_config(((struct em_osdep *)hw->back)->dev,
2542                          PCIR_COMMAND,
2543                          (hw->pci_cmd_word | CMD_MEM_WRT_INVALIDATE), 2);
2544         return;
2545 }
2546
2547 void
2548 em_pci_clear_mwi(struct em_hw *hw)
2549 {
2550         pci_write_config(((struct em_osdep *)hw->back)->dev,
2551                          PCIR_COMMAND,
2552                          (hw->pci_cmd_word & ~CMD_MEM_WRT_INVALIDATE), 2);
2553         return;
2554 }
2555
2556 uint32_t 
2557 em_io_read(struct em_hw *hw, uint32_t port)
2558 {
2559         return(inl(port));
2560 }
2561
2562 void 
2563 em_io_write(struct em_hw *hw, uint32_t port, uint32_t value)
2564 {
2565         outl(port, value);
2566         return;
2567 }
2568
2569
2570 /**********************************************************************
2571  *
2572  *  Update the board statistics counters. 
2573  *
2574  **********************************************************************/
2575 static void
2576 em_update_stats_counters(struct adapter *adapter)
2577 {
2578         struct ifnet   *ifp;
2579
2580         adapter->stats.crcerrs += E1000_READ_REG(&adapter->hw, CRCERRS);
2581         adapter->stats.symerrs += E1000_READ_REG(&adapter->hw, SYMERRS);
2582         adapter->stats.mpc += E1000_READ_REG(&adapter->hw, MPC);
2583         adapter->stats.scc += E1000_READ_REG(&adapter->hw, SCC);
2584         adapter->stats.ecol += E1000_READ_REG(&adapter->hw, ECOL);
2585
2586         adapter->stats.mcc += E1000_READ_REG(&adapter->hw, MCC);
2587         adapter->stats.latecol += E1000_READ_REG(&adapter->hw, LATECOL);
2588         adapter->stats.colc += E1000_READ_REG(&adapter->hw, COLC);
2589         adapter->stats.dc += E1000_READ_REG(&adapter->hw, DC);
2590         adapter->stats.sec += E1000_READ_REG(&adapter->hw, SEC);
2591         adapter->stats.rlec += E1000_READ_REG(&adapter->hw, RLEC);
2592         adapter->stats.xonrxc += E1000_READ_REG(&adapter->hw, XONRXC);
2593         adapter->stats.xontxc += E1000_READ_REG(&adapter->hw, XONTXC);
2594         adapter->stats.xoffrxc += E1000_READ_REG(&adapter->hw, XOFFRXC);
2595         adapter->stats.xofftxc += E1000_READ_REG(&adapter->hw, XOFFTXC);
2596         adapter->stats.fcruc += E1000_READ_REG(&adapter->hw, FCRUC);
2597         adapter->stats.prc64 += E1000_READ_REG(&adapter->hw, PRC64);
2598         adapter->stats.prc127 += E1000_READ_REG(&adapter->hw, PRC127);
2599         adapter->stats.prc255 += E1000_READ_REG(&adapter->hw, PRC255);
2600         adapter->stats.prc511 += E1000_READ_REG(&adapter->hw, PRC511);
2601         adapter->stats.prc1023 += E1000_READ_REG(&adapter->hw, PRC1023);
2602         adapter->stats.prc1522 += E1000_READ_REG(&adapter->hw, PRC1522);
2603         adapter->stats.gprc += E1000_READ_REG(&adapter->hw, GPRC);
2604         adapter->stats.bprc += E1000_READ_REG(&adapter->hw, BPRC);
2605         adapter->stats.mprc += E1000_READ_REG(&adapter->hw, MPRC);
2606         adapter->stats.gptc += E1000_READ_REG(&adapter->hw, GPTC);
2607
2608         /* For the 64-bit byte counters the low dword must be read first. */
2609         /* Both registers clear on the read of the high dword */
2610
2611         adapter->stats.gorcl += E1000_READ_REG(&adapter->hw, GORCL); 
2612         adapter->stats.gorch += E1000_READ_REG(&adapter->hw, GORCH);
2613         adapter->stats.gotcl += E1000_READ_REG(&adapter->hw, GOTCL);
2614         adapter->stats.gotch += E1000_READ_REG(&adapter->hw, GOTCH);
2615
2616         adapter->stats.rnbc += E1000_READ_REG(&adapter->hw, RNBC);
2617         adapter->stats.ruc += E1000_READ_REG(&adapter->hw, RUC);
2618         adapter->stats.rfc += E1000_READ_REG(&adapter->hw, RFC);
2619         adapter->stats.roc += E1000_READ_REG(&adapter->hw, ROC);
2620         adapter->stats.rjc += E1000_READ_REG(&adapter->hw, RJC);
2621
2622         adapter->stats.torl += E1000_READ_REG(&adapter->hw, TORL);
2623         adapter->stats.torh += E1000_READ_REG(&adapter->hw, TORH);
2624         adapter->stats.totl += E1000_READ_REG(&adapter->hw, TOTL);
2625         adapter->stats.toth += E1000_READ_REG(&adapter->hw, TOTH);
2626
2627         adapter->stats.tpr += E1000_READ_REG(&adapter->hw, TPR);
2628         adapter->stats.tpt += E1000_READ_REG(&adapter->hw, TPT);
2629         adapter->stats.ptc64 += E1000_READ_REG(&adapter->hw, PTC64);
2630         adapter->stats.ptc127 += E1000_READ_REG(&adapter->hw, PTC127);
2631         adapter->stats.ptc255 += E1000_READ_REG(&adapter->hw, PTC255);
2632         adapter->stats.ptc511 += E1000_READ_REG(&adapter->hw, PTC511);
2633         adapter->stats.ptc1023 += E1000_READ_REG(&adapter->hw, PTC1023);
2634         adapter->stats.ptc1522 += E1000_READ_REG(&adapter->hw, PTC1522);
2635         adapter->stats.mptc += E1000_READ_REG(&adapter->hw, MPTC);
2636         adapter->stats.bptc += E1000_READ_REG(&adapter->hw, BPTC);
2637
2638         if (adapter->hw.mac_type >= em_82543) {
2639                 adapter->stats.algnerrc += 
2640                 E1000_READ_REG(&adapter->hw, ALGNERRC);
2641                 adapter->stats.rxerrc += 
2642                 E1000_READ_REG(&adapter->hw, RXERRC);
2643                 adapter->stats.tncrs += 
2644                 E1000_READ_REG(&adapter->hw, TNCRS);
2645                 adapter->stats.cexterr += 
2646                 E1000_READ_REG(&adapter->hw, CEXTERR);
2647                 adapter->stats.tsctc += 
2648                 E1000_READ_REG(&adapter->hw, TSCTC);
2649                 adapter->stats.tsctfc += 
2650                 E1000_READ_REG(&adapter->hw, TSCTFC);
2651         }
2652         ifp = &adapter->interface_data.ac_if;
2653
2654         /* Fill out the OS statistics structure */
2655         ifp->if_ibytes = adapter->stats.gorcl;
2656         ifp->if_obytes = adapter->stats.gotcl;
2657         ifp->if_imcasts = adapter->stats.mprc;
2658         ifp->if_collisions = adapter->stats.colc;
2659
2660         /* Rx Errors */
2661         ifp->if_ierrors =
2662         adapter->dropped_pkts +
2663         adapter->stats.rxerrc +
2664         adapter->stats.crcerrs +
2665         adapter->stats.algnerrc +
2666         adapter->stats.rlec + adapter->stats.rnbc + 
2667         adapter->stats.mpc + adapter->stats.cexterr;
2668
2669         /* Tx Errors */
2670         ifp->if_oerrors = adapter->stats.ecol + adapter->stats.latecol;
2671
2672 }
2673
2674
2675 /**********************************************************************
2676  *
2677  *  This routine is called only when em_display_debug_stats is enabled.
2678  *  This routine provides a way to take a look at important statistics
2679  *  maintained by the driver and hardware.
2680  *
2681  **********************************************************************/
2682 static void
2683 em_print_debug_info(struct adapter *adapter)
2684 {
2685         int unit = adapter->unit;
2686
2687 #ifdef DBG_STATS
2688         printf("em%d: Packets not Avail = %ld\n", unit, 
2689                adapter->no_pkts_avail);
2690         printf("em%d: CleanTxInterrupts = %ld\n", unit, 
2691                adapter->clean_tx_interrupts);
2692 #endif
2693         printf("em%d: fifo workaround = %lld, fifo_reset = %lld\n", unit, 
2694                (long long)adapter->tx_fifo_wrk, 
2695                (long long)adapter->tx_fifo_reset);
2696         printf("em%d: hw tdh = %d, hw tdt = %d\n", unit,
2697                E1000_READ_REG(&adapter->hw, TDH), 
2698                E1000_READ_REG(&adapter->hw, TDT));
2699         printf("em%d: Num Tx descriptors avail = %d\n", unit,
2700                adapter->num_tx_desc_avail);
2701         printf("em%d: Tx Descriptors not avail1 = %ld\n", unit, 
2702                adapter->no_tx_desc_avail1);
2703         printf("em%d: Tx Descriptors not avail2 = %ld\n", unit, 
2704                adapter->no_tx_desc_avail2);
2705         printf("em%d: Std mbuf failed = %ld\n", unit, 
2706                adapter->mbuf_alloc_failed);
2707         printf("em%d: Std mbuf cluster failed = %ld\n", unit, 
2708                adapter->mbuf_cluster_failed);
2709         printf("em%d: Driver dropped packets = %ld\n", unit, 
2710                adapter->dropped_pkts);
2711
2712         return;
2713 }
2714
2715 static void
2716 em_print_hw_stats(struct adapter *adapter)
2717 {
2718         int unit = adapter->unit;
2719
2720         printf("em%d: Excessive collisions = %lld\n", unit,
2721                (long long)adapter->stats.ecol);
2722         printf("em%d: Symbol errors = %lld\n", unit, 
2723                (long long)adapter->stats.symerrs);
2724         printf("em%d: Sequence errors = %lld\n", unit, 
2725                (long long)adapter->stats.sec);
2726         printf("em%d: Defer count = %lld\n", unit, 
2727                (long long)adapter->stats.dc);
2728
2729         printf("em%d: Missed Packets = %lld\n", unit, 
2730                (long long)adapter->stats.mpc);
2731         printf("em%d: Receive No Buffers = %lld\n", unit, 
2732                (long long)adapter->stats.rnbc);
2733         printf("em%d: Receive length errors = %lld\n", unit, 
2734                (long long)adapter->stats.rlec);
2735         printf("em%d: Receive errors = %lld\n", unit, 
2736                (long long)adapter->stats.rxerrc);
2737         printf("em%d: Crc errors = %lld\n", unit, 
2738                (long long)adapter->stats.crcerrs);
2739         printf("em%d: Alignment errors = %lld\n", unit, 
2740                (long long)adapter->stats.algnerrc);
2741         printf("em%d: Carrier extension errors = %lld\n", unit,
2742                (long long)adapter->stats.cexterr);
2743
2744         printf("em%d: XON Rcvd = %lld\n", unit, 
2745                (long long)adapter->stats.xonrxc);
2746         printf("em%d: XON Xmtd = %lld\n", unit, 
2747                (long long)adapter->stats.xontxc);
2748         printf("em%d: XOFF Rcvd = %lld\n", unit, 
2749                (long long)adapter->stats.xoffrxc);
2750         printf("em%d: XOFF Xmtd = %lld\n", unit, 
2751                (long long)adapter->stats.xofftxc);
2752
2753         printf("em%d: Good Packets Rcvd = %lld\n", unit,
2754                (long long)adapter->stats.gprc);
2755         printf("em%d: Good Packets Xmtd = %lld\n", unit,
2756                (long long)adapter->stats.gptc);
2757
2758         return;
2759 }
2760
2761 static int
2762 em_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
2763 {
2764         int error;
2765         int result;
2766         struct adapter *adapter;
2767
2768         result = -1;
2769         error = sysctl_handle_int(oidp, &result, 0, req);
2770         
2771         if (error || !req->newptr)
2772                 return (error);
2773
2774         if (result == 1) {
2775                 adapter = (struct adapter *)arg1;
2776                 em_print_debug_info(adapter);
2777         }
2778
2779         return error;
2780 }
2781
2782
2783 static int
2784 em_sysctl_stats(SYSCTL_HANDLER_ARGS)
2785 {
2786         int error;
2787         int result;
2788         struct adapter *adapter;
2789         
2790         result = -1;
2791         error = sysctl_handle_int(oidp, &result, 0, req);
2792         
2793         if (error || !req->newptr)
2794                 return (error);
2795
2796         if (result == 1) {
2797                 adapter = (struct adapter *)arg1;
2798                 em_print_hw_stats(adapter);
2799         }
2800
2801         return error;
2802 }
2803