if_em.c revision 90628
1/**************************************************************************
2
3Copyright (c) 2001 Intel Corporation
4All rights reserved.
5
6Redistribution and use in source and binary forms of the Software, with or
7without modification, are permitted provided that the following conditions
8are met:
9
10 1. Redistributions of source code of the Software may retain the above
11    copyright notice, this list of conditions and the following disclaimer.
12
13 2. Redistributions in binary form of the Software may reproduce the above
14    copyright notice, this list of conditions and the following disclaimer
15    in the documentation and/or other materials provided with the
16    distribution.
17
18 3. Neither the name of the Intel Corporation nor the names of its
19    contributors shall be used to endorse or promote products derived from
20    this Software without specific prior written permission.
21
22THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR ITS CONTRIBUTORS BE LIABLE
26FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32SUCH DAMAGE.
33
34***************************************************************************/
35
36/*$FreeBSD: head/sys/dev/em/if_em.c 90628 2002-02-13 18:19:27Z pdeuskar $*/
37
38#include <dev/em/if_em.h>
39
40/*********************************************************************
41 *  Set this to one to display debug statistics
42 *********************************************************************/
43int             em_display_debug_stats = 0;
44
45/*********************************************************************
46 *  Linked list of board private structures for all NICs found
47 *********************************************************************/
48
49struct adapter *em_adapter_list = NULL;
50
51
52/*********************************************************************
53 *  Driver version
54 *********************************************************************/
55
56char em_driver_version[] = "1.1.10";
57
58
59/*********************************************************************
60 *  PCI Device ID Table
61 *
62 *  Used by probe to select devices to load on
63 *  Last field stores an index into em_strings
64 *  Last entry must be all 0s
65 *
66 *  { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
67 *********************************************************************/
68static 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    /* required last entry */
79    { 0, 0, 0, 0, 0}
80};
81
82
83/*********************************************************************
84 *  Table of branding strings for all supported NICs.
85 *********************************************************************/
86
87static char *em_strings[] = {
88    "Intel(R) PRO/1000 Network Connection"
89};
90
91/*********************************************************************
92 *  Function prototypes
93 *********************************************************************/
94static int em_probe     __P((device_t));
95static int em_attach        __P((device_t));
96static int em_detach        __P((device_t));
97static int em_shutdown        __P((device_t));
98static void em_intr __P((void *));
99static void em_start __P((struct ifnet *));
100static int em_ioctl __P((struct ifnet *, IOCTL_CMD_TYPE, caddr_t));
101static void em_watchdog __P((struct ifnet *));
102static void em_init __P((void *));
103static void em_stop __P((void *));
104static void em_media_status __P((struct ifnet *, struct ifmediareq *));
105static int em_media_change __P((struct ifnet *));
106static void em_identify_hardware __P((struct adapter *));
107static int em_allocate_pci_resources __P((struct adapter *));
108static void em_free_pci_resources __P((struct adapter *));
109static void em_local_timer __P((void *));
110static int em_hardware_init __P((struct adapter *));
111static void em_read_mac_address __P((struct adapter *, u_int8_t *));
112static void em_setup_interface __P((device_t, struct adapter *));
113static int em_setup_transmit_structures __P((struct adapter *));
114static void em_initialize_transmit_unit __P((struct adapter *));
115static int em_setup_receive_structures __P((struct adapter *));
116static void em_initialize_receive_unit __P((struct adapter *));
117static void EnableInterrupts __P((struct adapter *));
118static void DisableInterrupts __P((struct adapter *));
119static void em_free_transmit_structures __P((struct adapter *));
120static void em_free_receive_structures __P((struct adapter *));
121static void em_update_stats_counters __P((struct adapter *));
122static void em_clean_transmit_interrupts __P((struct adapter *));
123static int em_allocate_receive_structures __P((struct adapter *));
124static int em_allocate_transmit_structures __P((struct adapter *));
125static void em_process_receive_interrupts __P((struct adapter *));
126static void em_receive_checksum __P((struct adapter *,
127                                     struct em_rx_desc * RxDescriptor,
128                                     struct mbuf *));
129static void em_transmit_checksum_setup __P((struct adapter *,
130                                            struct mbuf *,
131                                            struct em_tx_buffer *,
132                                            u_int32_t *,
133                                            u_int32_t *));
134static void em_set_promisc __P((struct adapter *));
135static void em_disable_promisc __P((struct adapter *));
136static void em_set_multi __P((struct adapter *));
137static void em_print_hw_stats __P((struct adapter *));
138static void em_print_link_status __P((struct adapter *));
139static int em_get_buf __P((struct em_rx_buffer *, struct adapter *,
140                           struct mbuf *));
141/*********************************************************************
142 *  FreeBSD Device Interface Entry Points
143 *********************************************************************/
144
145static device_method_t em_methods[] = {
146   /* Device interface */
147   DEVMETHOD(device_probe, em_probe),
148   DEVMETHOD(device_attach, em_attach),
149   DEVMETHOD(device_detach, em_detach),
150   DEVMETHOD(device_shutdown, em_shutdown),
151   {0, 0}
152};
153
154static driver_t em_driver = {
155   "em", em_methods, sizeof(struct adapter ),
156};
157
158static devclass_t em_devclass;
159DRIVER_MODULE(if_em, pci, em_driver, em_devclass, 0, 0);
160
161/*********************************************************************
162 *  Device identification routine
163 *
164 *  em_probe determines if the driver should be loaded on
165 *  adapter based on PCI vendor/device id of the adapter.
166 *
167 *  return 0 on success, positive on failure
168 *********************************************************************/
169
170static int
171em_probe(device_t dev)
172{
173   em_vendor_info_t *ent;
174
175   u_int16_t       pci_vendor_id = 0;
176   u_int16_t       pci_device_id = 0;
177   u_int16_t       pci_subvendor_id = 0;
178   u_int16_t       pci_subdevice_id = 0;
179   char            adapter_name[60];
180
181   INIT_DEBUGOUT("em_probe: begin");
182
183   pci_vendor_id = pci_get_vendor(dev);
184   if (pci_vendor_id != EM_VENDOR_ID)
185      return (ENXIO);
186
187   pci_device_id = pci_get_device(dev);
188   pci_subvendor_id = pci_get_subvendor(dev);
189   pci_subdevice_id = pci_get_subdevice(dev);
190
191   ent = em_vendor_info_array;
192   while(ent->vendor_id != 0) {
193      if ((pci_vendor_id == ent->vendor_id) &&
194          (pci_device_id == ent->device_id) &&
195
196          ((pci_subvendor_id == ent->subvendor_id) ||
197           (ent->subvendor_id == PCI_ANY_ID)) &&
198
199          ((pci_subdevice_id == ent->subdevice_id) ||
200           (ent->subdevice_id == PCI_ANY_ID))) {
201         INIT_DEBUGOUT1("em_probe: Found PRO/1000  (pci_device_id=0x%x)",
202                        pci_device_id);
203         sprintf(adapter_name, "%s, Version - %s", em_strings[ent->index],
204                 em_driver_version);
205         device_set_desc_copy(dev, adapter_name);
206         return(0);
207      }
208      ent++;
209   }
210
211   return (ENXIO);
212}
213
214/*********************************************************************
215 *  Device initialization routine
216 *
217 *  The attach entry point is called when the driver is being loaded.
218 *  This routine identifies the type of hardware, allocates all resources
219 *  and initializes the hardware.
220 *
221 *  return 0 on success, positive on failure
222 *********************************************************************/
223
224static int
225em_attach(device_t dev)
226{
227   struct adapter * Adapter;
228   int             s;
229   int             tsize, rsize;
230
231   INIT_DEBUGOUT("em_attach: begin");
232   s = splimp();
233
234   /* Allocate, clear, and link in our Adapter structure */
235   if (!(Adapter = device_get_softc(dev))) {
236      printf("em: Adapter structure allocation failed\n");
237      splx(s);
238      return(ENOMEM);
239   }
240   bzero(Adapter, sizeof(struct adapter ));
241   Adapter->dev = dev;
242   Adapter->osdep.dev = dev;
243   Adapter->unit = device_get_unit(dev);
244
245   if (em_adapter_list != NULL)
246      em_adapter_list->prev = Adapter;
247   Adapter->next = em_adapter_list;
248   em_adapter_list = Adapter;
249
250   callout_handle_init(&Adapter->timer_handle);
251
252   /* Determine hardware revision */
253   em_identify_hardware(Adapter);
254
255   /* Parameters (to be read from user) */
256   Adapter->NumTxDescriptors = MAX_TXD;
257   Adapter->NumRxDescriptors = MAX_RXD;
258   Adapter->TxIntDelay = TIDV;
259   Adapter->RxIntDelay = RIDV;
260   Adapter->shared.autoneg = DO_AUTO_NEG;
261   Adapter->shared.wait_autoneg_complete = WAIT_FOR_AUTO_NEG_DEFAULT;
262   Adapter->shared.autoneg_advertised = AUTONEG_ADV_DEFAULT;
263   Adapter->shared.tbi_compatibility_en = TRUE;
264   Adapter->RxBufferLen = EM_RXBUFFER_2048;
265   Adapter->RxChecksum = EM_ENABLE_RXCSUM_OFFLOAD;
266
267   Adapter->shared.fc_high_water = FC_DEFAULT_HI_THRESH;
268   Adapter->shared.fc_low_water  = FC_DEFAULT_LO_THRESH;
269   Adapter->shared.fc_pause_time = FC_DEFAULT_TX_TIMER;
270   Adapter->shared.fc_send_xon   = TRUE;
271   Adapter->shared.fc = em_fc_full;
272
273
274   /* Set the max frame size assuming standard ethernet sized frames */
275   Adapter->shared.max_frame_size = ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN;
276
277   /* This controls when hardware reports transmit completion status. */
278   if ((EM_REPORT_TX_EARLY == 0) || (EM_REPORT_TX_EARLY == 1)) {
279      Adapter->shared.report_tx_early = EM_REPORT_TX_EARLY;
280   } else {
281      if(Adapter->shared.mac_type < em_82543) {
282         Adapter->shared.report_tx_early = 0;
283      } else {
284         Adapter->shared.report_tx_early = 1;
285      }
286   }
287
288   if (em_allocate_pci_resources(Adapter)) {
289      printf("em%d: Allocation of PCI resources failed\n", Adapter->unit);
290      em_free_pci_resources(Adapter);
291      splx(s);
292      return(ENXIO);
293   }
294
295   tsize = EM_ROUNDUP(Adapter->NumTxDescriptors *
296                      sizeof(struct em_tx_desc), 4096);
297
298   /* Allocate Transmit Descriptor ring */
299   if (!(Adapter->TxDescBase = (struct em_tx_desc *)
300         contigmalloc(tsize, M_DEVBUF, M_NOWAIT, 0, ~0, PAGE_SIZE, 0))) {
301      printf("em%d: Unable to allocate TxDescriptor memory\n", Adapter->unit);
302      em_free_pci_resources(Adapter);
303      splx(s);
304      return(ENOMEM);
305   }
306
307   rsize = EM_ROUNDUP(Adapter->NumRxDescriptors *
308                      sizeof(struct em_rx_desc), 4096);
309
310   /* Allocate Receive Descriptor ring */
311   if (!(Adapter->RxDescBase = (struct em_rx_desc *)
312        contigmalloc(rsize, M_DEVBUF, M_NOWAIT, 0, ~0, PAGE_SIZE, 0))) {
313      printf("em%d: Unable to allocate RxDescriptor memory\n", Adapter->unit);
314      em_free_pci_resources(Adapter);
315      contigfree(Adapter->TxDescBase, tsize, M_DEVBUF);
316      splx(s);
317      return(ENOMEM);
318   }
319
320   /* Initialize the hardware */
321   if (em_hardware_init(Adapter)) {
322      printf("em%d: Unable to initialize the hardware\n",Adapter->unit);
323      em_free_pci_resources(Adapter);
324      contigfree(Adapter->TxDescBase, tsize, M_DEVBUF);
325      contigfree(Adapter->RxDescBase, rsize, M_DEVBUF);
326      splx(s);
327      return(EIO);
328   }
329
330   /* Setup OS specific network interface */
331   em_setup_interface(dev, Adapter);
332
333   /* Initialize statistics */
334   em_clear_hw_cntrs(&Adapter->shared);
335   em_update_stats_counters(Adapter);
336   Adapter->shared.get_link_status = 1;
337   em_check_for_link(&Adapter->shared);
338
339   /* Print the link status */
340   if (Adapter->LinkIsActive == 1) {
341      em_get_speed_and_duplex(&Adapter->shared, &Adapter->LineSpeed, &Adapter->FullDuplex);
342      printf("em%d:  Speed:%d Mbps  Duplex:%s\n",
343             Adapter->unit,
344             Adapter->LineSpeed,
345             Adapter->FullDuplex == FULL_DUPLEX ? "Full" : "Half");
346   }
347   else
348      printf("em%d:  Speed:N/A  Duplex:N/A\n", Adapter->unit);
349
350
351   INIT_DEBUGOUT("em_attach: end");
352   splx(s);
353   return(0);
354}
355
356/*********************************************************************
357 *  Device removal routine
358 *
359 *  The detach entry point is called when the driver is being removed.
360 *  This routine stops the adapter and deallocates all the resources
361 *  that were allocated for driver operation.
362 *
363 *  return 0 on success, positive on failure
364 *********************************************************************/
365
366static int
367em_detach(device_t dev)
368{
369   struct adapter * Adapter = device_get_softc(dev);
370   struct ifnet   *ifp = &Adapter->interface_data.ac_if;
371   int             s;
372   int             size;
373
374   INIT_DEBUGOUT("em_detach: begin");
375   s = splimp();
376
377   em_stop(Adapter);
378   em_phy_hw_reset(&Adapter->shared);
379   ether_ifdetach(&Adapter->interface_data.ac_if, ETHER_BPF_SUPPORTED);
380   em_free_pci_resources(Adapter);
381
382   size = EM_ROUNDUP(Adapter->NumTxDescriptors *
383                     sizeof(struct em_tx_desc), 4096);
384
385   /* Free Transmit Descriptor ring */
386   if (Adapter->TxDescBase) {
387      contigfree(Adapter->TxDescBase, size, M_DEVBUF);
388      Adapter->TxDescBase = NULL;
389   }
390
391   size = EM_ROUNDUP(Adapter->NumRxDescriptors *
392                     sizeof(struct em_rx_desc), 4096);
393
394   /* Free Receive Descriptor ring */
395   if (Adapter->RxDescBase) {
396      contigfree(Adapter->RxDescBase, size, M_DEVBUF);
397      Adapter->RxDescBase = NULL;
398   }
399
400   /* Remove from the adapter list */
401   if(em_adapter_list == Adapter)
402      em_adapter_list = Adapter->next;
403   if(Adapter->next != NULL)
404      Adapter->next->prev = Adapter->prev;
405   if(Adapter->prev != NULL)
406      Adapter->prev->next = Adapter->next;
407
408   ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
409   ifp->if_timer = 0;
410
411   splx(s);
412   return(0);
413}
414
415static int
416em_shutdown(device_t dev)
417{
418   struct adapter * Adapter = device_get_softc(dev);
419
420   /* Issue a global reset */
421   em_adapter_stop(&Adapter->shared);
422   return(0);
423}
424
425
426/*********************************************************************
427 *  Transmit entry point
428 *
429 *  em_start is called by the stack to initiate a transmit.
430 *  The driver will remain in this routine as long as there are
431 *  packets to transmit and transmit resources are available.
432 *  In case resources are not available stack is notified and
433 *  the packet is requeued.
434 **********************************************************************/
435
436static void
437em_start(struct ifnet *ifp)
438{
439   int             s;
440   struct em_tx_buffer   *tx_buffer;
441   struct mbuf    *m_head;
442   struct mbuf    *mp;
443   vm_offset_t     VirtualAddress;
444   u_int32_t       txd_upper;
445   u_int32_t       txd_lower;
446   struct em_tx_desc * CurrentTxDescriptor = NULL;
447   struct adapter * Adapter = ifp->if_softc;
448
449   TXRX_DEBUGOUT("em_start: begin");
450
451   if (!Adapter->LinkIsActive)
452      return;
453
454   s = splimp();
455   while (ifp->if_snd.ifq_head != NULL) {
456
457      IF_DEQUEUE(&ifp->if_snd, m_head);
458
459      if(m_head == NULL) break;
460
461      if (Adapter->NumTxDescriptorsAvail <= TX_CLEANUP_THRESHOLD)
462         em_clean_transmit_interrupts(Adapter);
463
464      if (Adapter->NumTxDescriptorsAvail <= TX_CLEANUP_THRESHOLD) {
465         ifp->if_flags |= IFF_OACTIVE;
466         IF_PREPEND(&ifp->if_snd, m_head);
467#ifdef DBG_STATS
468         Adapter->NoTxDescAvail++;
469#endif
470         break;
471      }
472
473      tx_buffer =  STAILQ_FIRST(&Adapter->FreeSwTxPacketList);
474      if (!tx_buffer) {
475#ifdef DBG_STATS
476         Adapter->NoTxBufferAvail1++;
477#endif
478         /*
479          * OK so we should not get here but I've seen it so lets try to
480          * clean up and then try to get a SwPacket again and only break
481          * if we still don't get one
482          */
483         em_clean_transmit_interrupts(Adapter);
484         tx_buffer = STAILQ_FIRST(&Adapter->FreeSwTxPacketList);
485         if (!tx_buffer) {
486            ifp->if_flags |= IFF_OACTIVE;
487            IF_PREPEND(&ifp->if_snd, m_head);
488#ifdef DBG_STATS
489            Adapter->NoTxBufferAvail2++;
490#endif
491            break;
492         }
493      }
494      STAILQ_REMOVE_HEAD(&Adapter->FreeSwTxPacketList, em_tx_entry);
495      tx_buffer->NumTxDescriptorsUsed = 0;
496      tx_buffer->Packet = m_head;
497
498      if (ifp->if_hwassist > 0) {
499         em_transmit_checksum_setup(Adapter,  m_head, tx_buffer, &txd_upper, &txd_lower);
500      } else {
501         txd_upper = 0;
502         txd_lower = 0;
503      }
504
505      for (mp = m_head; mp != NULL; mp = mp->m_next) {
506         if (mp->m_len == 0)
507            continue;
508         CurrentTxDescriptor = Adapter->NextAvailTxDescriptor;
509         VirtualAddress = mtod(mp, vm_offset_t);
510         CurrentTxDescriptor->buffer_addr = vtophys(VirtualAddress);
511
512         CurrentTxDescriptor->lower.data = (txd_lower | mp->m_len);
513         CurrentTxDescriptor->upper.data = (txd_upper);
514
515         if (CurrentTxDescriptor == Adapter->LastTxDescriptor)
516            Adapter->NextAvailTxDescriptor =
517            Adapter->FirstTxDescriptor;
518         else
519            Adapter->NextAvailTxDescriptor++;
520
521         Adapter->NumTxDescriptorsAvail--;
522         tx_buffer->NumTxDescriptorsUsed++;
523      }
524      /* Put this tx_buffer at the end in the "in use" list */
525      STAILQ_INSERT_TAIL(&Adapter->UsedSwTxPacketList, tx_buffer, em_tx_entry);
526
527      /*
528       * Last Descriptor of Packet needs End Of Packet (EOP), Report Status
529       * (RS) and append Ethernet CRC (IFCS) bits set.
530       */
531      CurrentTxDescriptor->lower.data |= (Adapter->TxdCmd | E1000_TXD_CMD_EOP);
532
533      /* Send a copy of the frame to the BPF listener */
534      if (ifp->if_bpf)
535         bpf_mtap(ifp, m_head);
536      /*
537       * Advance the Transmit Descriptor Tail (Tdt), this tells the E1000
538       * that this frame is available to transmit.
539       */
540      E1000_WRITE_REG(&Adapter->shared, TDT, (((u_int32_t) Adapter->NextAvailTxDescriptor -
541                             (u_int32_t) Adapter->FirstTxDescriptor) >> 4));
542   } /* end of while loop */
543
544   splx(s);
545
546   /* Set timeout in case chip has problems transmitting */
547   ifp->if_timer = EM_TX_TIMEOUT;
548
549   return;
550}
551
552/*********************************************************************
553 *  Ioctl entry point
554 *
555 *  em_ioctl is called when the user wants to configure the
556 *  interface.
557 *
558 *  return 0 on success, positive on failure
559 **********************************************************************/
560
561static int
562em_ioctl(struct ifnet *ifp, IOCTL_CMD_TYPE command, caddr_t data)
563{
564   int             s,
565                   error = 0;
566   struct ifreq   *ifr = (struct ifreq *) data;
567   struct adapter * Adapter = ifp->if_softc;
568
569   s = splimp();
570   switch (command) {
571   case SIOCSIFADDR:
572   case SIOCGIFADDR:
573      IOCTL_DEBUGOUT("ioctl rcv'd: SIOCxIFADDR (Get/Set Interface Addr)");
574      ether_ioctl(ifp, command, data);
575      break;
576   case SIOCSIFMTU:
577      IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFMTU (Set Interface MTU)");
578      if (ifr->ifr_mtu > MAX_JUMBO_FRAME_SIZE - ETHER_HDR_LEN) {
579         error = EINVAL;
580      } else {
581         ifp->if_mtu = ifr->ifr_mtu;
582         Adapter->shared.max_frame_size = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
583         em_init(Adapter);
584      }
585      break;
586   case SIOCSIFFLAGS:
587      IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFFLAGS (Set Interface Flags)");
588      if (ifp->if_flags & IFF_UP) {
589         if (ifp->if_flags & IFF_RUNNING &&
590             ifp->if_flags & IFF_PROMISC) {
591            em_set_promisc(Adapter);
592         } else if (ifp->if_flags & IFF_RUNNING &&
593                    !(ifp->if_flags & IFF_PROMISC)) {
594            em_disable_promisc(Adapter);
595         } else
596            em_init(Adapter);
597      } else {
598         if (ifp->if_flags & IFF_RUNNING) {
599            em_stop(Adapter);
600         }
601      }
602      break;
603   case SIOCADDMULTI:
604   case SIOCDELMULTI:
605      IOCTL_DEBUGOUT("ioctl rcv'd: SIOC(ADD|DEL)MULTI");
606      if (ifp->if_flags & IFF_RUNNING) {
607         DisableInterrupts(Adapter);
608         em_set_multi(Adapter);
609         if(Adapter->shared.mac_type == em_82542_rev2_0)
610            em_initialize_receive_unit(Adapter);
611         EnableInterrupts(Adapter);
612      }
613      break;
614   case SIOCSIFMEDIA:
615   case SIOCGIFMEDIA:
616      IOCTL_DEBUGOUT("ioctl rcv'd: SIOCxIFMEDIA (Get/Set Interface Media)");
617      error = ifmedia_ioctl(ifp, ifr, &Adapter->media, command);
618      break;
619   default:
620      IOCTL_DEBUGOUT1("ioctl received: UNKNOWN (0x%d)\n", (int)command);
621      error = EINVAL;
622   }
623
624   splx(s);
625   return(error);
626}
627
628static void
629em_set_promisc(struct adapter * Adapter)
630{
631
632   u_int32_t       reg_rctl;
633   struct ifnet   *ifp = &Adapter->interface_data.ac_if;
634
635   reg_rctl = E1000_READ_REG(&Adapter->shared, RCTL);
636
637   if(ifp->if_flags & IFF_PROMISC) {
638      reg_rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
639      E1000_WRITE_REG(&Adapter->shared, RCTL, reg_rctl);
640   }
641   else if (ifp->if_flags & IFF_ALLMULTI) {
642      reg_rctl |= E1000_RCTL_MPE;
643      reg_rctl &= ~E1000_RCTL_UPE;
644      E1000_WRITE_REG(&Adapter->shared, RCTL, reg_rctl);
645   }
646
647   return;
648}
649
650static void
651em_disable_promisc(struct adapter * Adapter)
652{
653   u_int32_t       reg_rctl;
654
655   reg_rctl = E1000_READ_REG(&Adapter->shared, RCTL);
656
657   reg_rctl &=  (~E1000_RCTL_UPE);
658   reg_rctl &=  (~E1000_RCTL_MPE);
659   E1000_WRITE_REG(&Adapter->shared, RCTL, reg_rctl);
660
661   return;
662}
663
664
665/*********************************************************************
666 *  Multicast Update
667 *
668 *  This routine is called whenever multicast address list is updated.
669 *
670 **********************************************************************/
671
672static void
673em_set_multi(struct adapter * Adapter)
674{
675   u_int32_t reg_rctl = 0;
676   u_int8_t  mta[MAX_NUM_MULTICAST_ADDRESSES * ETH_LENGTH_OF_ADDRESS];
677   u_int16_t PciCommandWord;
678   struct ifmultiaddr  *ifma;
679   int mcnt = 0;
680   struct ifnet   *ifp = &Adapter->interface_data.ac_if;
681
682   IOCTL_DEBUGOUT("em_set_multi: begin");
683
684    if(Adapter->shared.mac_type == em_82542_rev2_0) {
685       reg_rctl = E1000_READ_REG(&Adapter->shared, RCTL);
686       if(Adapter->shared.pci_cmd_word & CMD_MEM_WRT_INVALIDATE) {
687          PciCommandWord = Adapter->shared.pci_cmd_word & ~CMD_MEM_WRT_INVALIDATE;
688          pci_write_config(Adapter->dev, PCIR_COMMAND, PciCommandWord, 2);
689       }
690       reg_rctl |= E1000_RCTL_RST;
691       E1000_WRITE_REG(&Adapter->shared, RCTL, reg_rctl);
692       msec_delay(5);
693    }
694
695#if __FreeBSD_version < 500000
696    LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
697#else
698    TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
699#endif
700       if (ifma->ifma_addr->sa_family != AF_LINK)
701          continue;
702
703       bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
704             &mta[mcnt*ETH_LENGTH_OF_ADDRESS], ETH_LENGTH_OF_ADDRESS);
705       mcnt++;
706    }
707
708    if (mcnt > MAX_NUM_MULTICAST_ADDRESSES) {
709       reg_rctl = E1000_READ_REG(&Adapter->shared, RCTL);
710       reg_rctl |= E1000_RCTL_MPE;
711       E1000_WRITE_REG(&Adapter->shared, RCTL, reg_rctl);
712    }
713    else
714       em_mc_addr_list_update(&Adapter->shared, mta, mcnt, 0);
715
716    if(Adapter->shared.mac_type == em_82542_rev2_0) {
717       reg_rctl = E1000_READ_REG(&Adapter->shared, RCTL);
718       reg_rctl &= ~E1000_RCTL_RST;
719       E1000_WRITE_REG(&Adapter->shared, RCTL, reg_rctl);
720       msec_delay(5);
721       if(Adapter->shared.pci_cmd_word & CMD_MEM_WRT_INVALIDATE) {
722          pci_write_config(Adapter->dev, PCIR_COMMAND, Adapter->shared.pci_cmd_word, 2);
723       }
724    }
725
726    return;
727}
728
729/*********************************************************************
730 *  Watchdog entry point
731 *
732 *  This routine is called whenever hardware quits transmitting.
733 *
734 **********************************************************************/
735
736static void
737em_watchdog(struct ifnet *ifp)
738{
739   struct adapter * Adapter;
740   Adapter = ifp->if_softc;
741
742   /* If we are in this routine because of pause frames, then
743    * don't reset the hardware.
744    */
745   if(E1000_READ_REG(&Adapter->shared, STATUS) & E1000_STATUS_TXOFF) {
746      ifp->if_timer = EM_TX_TIMEOUT;
747      return;
748   }
749
750   printf("em%d: watchdog timeout -- resetting\n", Adapter->unit);
751
752   ifp->if_flags &= ~IFF_RUNNING;
753
754   em_stop(Adapter);
755   em_init(Adapter);
756
757   ifp->if_oerrors++;
758   return;
759}
760
761/*********************************************************************
762 *  Timer routine
763 *
764 *  This routine checks for link status and updates statistics.
765 *
766 **********************************************************************/
767
768static void
769em_local_timer(void *arg)
770{
771   int s;
772   struct ifnet   *ifp;
773   struct adapter * Adapter = arg;
774   ifp = &Adapter->interface_data.ac_if;
775
776   s = splimp();
777
778   em_check_for_link(&Adapter->shared);
779   em_print_link_status(Adapter);
780   em_update_stats_counters(Adapter);
781   if(em_display_debug_stats && ifp->if_flags & IFF_RUNNING) {
782      em_print_hw_stats(Adapter);
783   }
784   Adapter->timer_handle = timeout(em_local_timer, Adapter, 2*hz);
785
786   splx(s);
787   return;
788}
789
790static void
791em_print_link_status(struct adapter * Adapter)
792{
793   if(E1000_READ_REG(&Adapter->shared, STATUS) & E1000_STATUS_LU) {
794      if(Adapter->LinkIsActive == 0) {
795         em_get_speed_and_duplex(&Adapter->shared, &Adapter->LineSpeed, &Adapter->FullDuplex);
796         printf("em%d: Link is up %d Mbps %s\n",
797                Adapter->unit,
798                Adapter->LineSpeed,
799                ((Adapter->FullDuplex == FULL_DUPLEX) ?
800                 "Full Duplex" : "Half Duplex"));
801         Adapter->LinkIsActive = 1;
802      }
803   } else {
804      if(Adapter->LinkIsActive == 1) {
805         Adapter->LineSpeed = 0;
806         Adapter->FullDuplex = 0;
807         printf("em%d: Link is Down\n", Adapter->unit);
808         Adapter->LinkIsActive = 0;
809      }
810   }
811
812   return;
813}
814
815/*********************************************************************
816 *  Init entry point
817 *
818 *  This routine is used in two ways. It is used by the stack as
819 *  init entry point in network interface structure. It is also used
820 *  by the driver as a hw/sw initialization routine to get to a
821 *  consistent state.
822 *
823 *  return 0 on success, positive on failure
824 **********************************************************************/
825
826static void
827em_init(void *arg)
828{
829   int             s;
830   struct ifnet   *ifp;
831   struct adapter * Adapter = arg;
832
833   INIT_DEBUGOUT("em_init: begin");
834
835   s = splimp();
836
837   em_stop(Adapter);
838
839   /* Initialize the hardware */
840   if (em_hardware_init(Adapter)) {
841      printf("em%d: Unable to initialize the hardware\n", Adapter->unit);
842      splx(s);
843      return;
844   }
845   Adapter->shared.adapter_stopped = FALSE;
846
847   /* Prepare transmit descriptors and buffers */
848   if (em_setup_transmit_structures(Adapter)) {
849      printf("em%d: Could not setup transmit structures\n", Adapter->unit);
850      em_stop(Adapter);
851      splx(s);
852      return;
853   }
854   em_initialize_transmit_unit(Adapter);
855
856   /* Setup Multicast table */
857   em_set_multi(Adapter);
858
859   /* Prepare receive descriptors and buffers */
860   if (em_setup_receive_structures(Adapter)) {
861      printf("em%d: Could not setup receive structures\n", Adapter->unit);
862      em_stop(Adapter);
863      splx(s);
864      return;
865   }
866   em_initialize_receive_unit(Adapter);
867
868   ifp = &Adapter->interface_data.ac_if;
869   ifp->if_flags |= IFF_RUNNING;
870   ifp->if_flags &= ~IFF_OACTIVE;
871
872   if(Adapter->shared.mac_type >= em_82543)
873      ifp->if_hwassist = EM_CHECKSUM_FEATURES;
874
875   Adapter->timer_handle = timeout(em_local_timer, Adapter, 2*hz);
876   em_clear_hw_cntrs(&Adapter->shared);
877   EnableInterrupts(Adapter);
878
879   splx(s);
880   return;
881}
882
883
884/*********************************************************************
885 *
886 *  This routine disables all traffic on the adapter by issuing a
887 *  global reset on the MAC and deallocates TX/RX buffers.
888 *
889 **********************************************************************/
890
891static void
892em_stop(void *arg)
893{
894   struct ifnet   *ifp;
895   struct adapter * Adapter = arg;
896   ifp = &Adapter->interface_data.ac_if;
897
898   INIT_DEBUGOUT("em_stop: begin\n");
899   DisableInterrupts(Adapter);
900   em_adapter_stop(&Adapter->shared);
901   untimeout(em_local_timer, Adapter, Adapter->timer_handle);
902   em_free_transmit_structures(Adapter);
903   em_free_receive_structures(Adapter);
904
905
906   /* Tell the stack that the interface is no longer active */
907   ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
908
909   return;
910}
911
912/*********************************************************************
913 *
914 *  Interrupt Service routine
915 *
916 **********************************************************************/
917
918static void
919em_intr(void *arg)
920{
921   u_int32_t            ProcessCount = EM_MAX_INTR;
922   u_int32_t            IcrContents;
923   struct ifnet   *ifp;
924   struct adapter *Adapter = arg;
925
926   ifp = &Adapter->interface_data.ac_if;
927
928   DisableInterrupts(Adapter);
929   while(ProcessCount > 0 && (IcrContents = E1000_READ_REG(&Adapter->shared, ICR)) != 0) {
930
931      /* Link status change */
932      if(IcrContents & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
933         untimeout(em_local_timer, Adapter, Adapter->timer_handle);
934         Adapter->shared.get_link_status = 1;
935         em_check_for_link(&Adapter->shared);
936         em_print_link_status(Adapter);
937         Adapter->timer_handle = timeout(em_local_timer, Adapter, 2*hz);
938      }
939
940      if (ifp->if_flags & IFF_RUNNING) {
941         em_process_receive_interrupts(Adapter);
942         em_clean_transmit_interrupts(Adapter);
943      }
944      ProcessCount--;
945   }
946
947   EnableInterrupts(Adapter);
948
949   if(ifp->if_flags & IFF_RUNNING && ifp->if_snd.ifq_head != NULL)
950      em_start(ifp);
951
952   return;
953}
954
955
956/*********************************************************************
957 *
958 *  Media Ioctl callback
959 *
960 *  This routine is called whenever the user queries the status of
961 *  the interface using ifconfig.
962 *
963 **********************************************************************/
964static void
965em_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
966{
967   struct adapter * Adapter = ifp->if_softc;
968
969   INIT_DEBUGOUT("em_media_status: begin");
970
971   em_check_for_link(&Adapter->shared);
972   if(E1000_READ_REG(&Adapter->shared, STATUS) & E1000_STATUS_LU) {
973      if(Adapter->LinkIsActive == 0) {
974         em_get_speed_and_duplex(&Adapter->shared, &Adapter->LineSpeed, &Adapter->FullDuplex);
975         Adapter->LinkIsActive = 1;
976      }
977   }
978   else {
979      if(Adapter->LinkIsActive == 1) {
980         Adapter->LineSpeed = 0;
981         Adapter->FullDuplex = 0;
982         Adapter->LinkIsActive = 0;
983      }
984   }
985
986   ifmr->ifm_status = IFM_AVALID;
987   ifmr->ifm_active = IFM_ETHER;
988
989   if (!Adapter->LinkIsActive)
990      return;
991
992   ifmr->ifm_status |= IFM_ACTIVE;
993
994   if (Adapter->shared.media_type == em_media_type_fiber) {
995      ifmr->ifm_active |= IFM_1000_SX | IFM_FDX;
996   } else {
997      switch (Adapter->LineSpeed) {
998      case 10:
999         ifmr->ifm_active |= IFM_10_T;
1000         break;
1001      case 100:
1002         ifmr->ifm_active |= IFM_100_TX;
1003         break;
1004      case 1000:
1005         ifmr->ifm_active |= IFM_1000_TX;
1006         break;
1007      }
1008      if (Adapter->FullDuplex == FULL_DUPLEX)
1009         ifmr->ifm_active |= IFM_FDX;
1010      else
1011         ifmr->ifm_active |= IFM_HDX;
1012   }
1013   return;
1014}
1015
1016/*********************************************************************
1017 *
1018 *  Media Ioctl callback
1019 *
1020 *  This routine is called when the user changes speed/duplex using
1021 *  media/mediopt option with ifconfig.
1022 *
1023 **********************************************************************/
1024static int
1025em_media_change(struct ifnet *ifp)
1026{
1027   struct adapter * Adapter = ifp->if_softc;
1028   struct ifmedia  *ifm = &Adapter->media;
1029
1030   INIT_DEBUGOUT("em_media_change: begin");
1031
1032   if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1033      return(EINVAL);
1034
1035   switch(IFM_SUBTYPE(ifm->ifm_media)) {
1036   case IFM_AUTO:
1037      if (Adapter->shared.autoneg)
1038         return 0;
1039      else {
1040         Adapter->shared.autoneg = DO_AUTO_NEG;
1041         Adapter->shared.autoneg_advertised = AUTONEG_ADV_DEFAULT;
1042      }
1043      break;
1044   case IFM_1000_SX:
1045   case IFM_1000_TX:
1046      Adapter->shared.autoneg = DO_AUTO_NEG;
1047      Adapter->shared.autoneg_advertised = ADVERTISE_1000_FULL;
1048      break;
1049   case IFM_100_TX:
1050      Adapter->shared.autoneg = FALSE;
1051      Adapter->shared.autoneg_advertised = 0;
1052      if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
1053         Adapter->shared.forced_speed_duplex = em_100_full;
1054      else
1055         Adapter->shared.forced_speed_duplex = em_100_half;
1056      break;
1057   case IFM_10_T:
1058     Adapter->shared.autoneg = FALSE;
1059     Adapter->shared.autoneg_advertised = 0;
1060     if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
1061        Adapter->shared.forced_speed_duplex = em_10_full;
1062     else
1063        Adapter->shared.forced_speed_duplex = em_10_half;
1064     break;
1065   default:
1066      printf("em%d: Unsupported media type\n", Adapter->unit);
1067   }
1068
1069   em_init(Adapter);
1070
1071   return(0);
1072}
1073/* Section end: Other registered entry points */
1074
1075
1076/*********************************************************************
1077 *
1078 *  Determine hardware revision.
1079 *
1080 **********************************************************************/
1081static void
1082em_identify_hardware(struct adapter * Adapter)
1083{
1084   device_t dev = Adapter->dev;
1085
1086   /* Make sure our PCI config space has the necessary stuff set */
1087   Adapter->shared.pci_cmd_word = pci_read_config(dev, PCIR_COMMAND, 2);
1088   if (!((Adapter->shared.pci_cmd_word & PCIM_CMD_BUSMASTEREN) &&
1089         (Adapter->shared.pci_cmd_word & PCIM_CMD_MEMEN))) {
1090      printf("em%d: Memory Access and/or Bus Master bits were not set!\n",
1091             Adapter->unit);
1092      Adapter->shared.pci_cmd_word |= (PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN);
1093      pci_write_config(dev, PCIR_COMMAND, Adapter->shared.pci_cmd_word, 2);
1094   }
1095
1096   /* Save off the information about this board */
1097   Adapter->VendorId = pci_get_vendor(dev);
1098   Adapter->DeviceId = pci_get_device(dev);
1099   Adapter->RevId = pci_read_config(dev, PCIR_REVID, 1);
1100   Adapter->SubVendorId = pci_read_config(dev, PCIR_SUBVEND_0, 2);
1101   Adapter->SubSystemId = pci_read_config(dev, PCIR_SUBDEV_0, 2);
1102
1103   INIT_DEBUGOUT2("device id = 0x%x, Revid = 0x%x", Adapter->DeviceId, Adapter->RevId);
1104
1105   /* Set MacType, etc. based on this PCI info */
1106   switch (Adapter->DeviceId) {
1107   case PCI_DEVICE_ID_82542:
1108      Adapter->shared.mac_type = (Adapter->RevId == 3) ?
1109         em_82542_rev2_1 : em_82542_rev2_0;
1110      break;
1111   case PCI_DEVICE_ID_82543GC_FIBER:
1112   case PCI_DEVICE_ID_82543GC_COPPER:
1113      Adapter->shared.mac_type = em_82543;
1114      break;
1115   case PCI_DEVICE_ID_82544EI_FIBER:
1116   case PCI_DEVICE_ID_82544EI_COPPER:
1117   case PCI_DEVICE_ID_82544GC_COPPER:
1118   case PCI_DEVICE_ID_82544GC_STRG:
1119      Adapter->shared.mac_type = em_82544;
1120      break;
1121   default:
1122      INIT_DEBUGOUT1("Unknown device id 0x%x", Adapter->DeviceId);
1123   }
1124   return;
1125}
1126
1127static int
1128em_allocate_pci_resources(struct adapter * Adapter)
1129{
1130   int             resource_id = EM_MMBA;
1131   device_t        dev = Adapter->dev;
1132
1133   Adapter->res_memory = bus_alloc_resource(dev, SYS_RES_MEMORY,
1134                                            &resource_id, 0, ~0, 1,
1135                                            RF_ACTIVE);
1136   if (!(Adapter->res_memory)) {
1137      printf("em%d: Unable to allocate bus resource: memory\n", Adapter->unit);
1138      return(ENXIO);
1139   }
1140   Adapter->osdep.bus_space_tag = rman_get_bustag(Adapter->res_memory);
1141   Adapter->osdep.bus_space_handle = rman_get_bushandle(Adapter->res_memory);
1142   Adapter->shared.hw_addr = (uint8_t *)Adapter->osdep.bus_space_handle;
1143
1144   resource_id = 0x0;
1145   Adapter->res_interrupt = bus_alloc_resource(dev, SYS_RES_IRQ,
1146                                               &resource_id, 0, ~0, 1,
1147                                               RF_SHAREABLE | RF_ACTIVE);
1148   if (!(Adapter->res_interrupt)) {
1149      printf("em%d: Unable to allocate bus resource: interrupt\n", Adapter->unit);
1150      return(ENXIO);
1151   }
1152   if (bus_setup_intr(dev, Adapter->res_interrupt, INTR_TYPE_NET,
1153                  (void (*)(void *)) em_intr, Adapter,
1154                  &Adapter->int_handler_tag)) {
1155      printf("em%d: Error registering interrupt handler!\n", Adapter->unit);
1156      return(ENXIO);
1157   }
1158
1159   Adapter->shared.back = &Adapter->osdep;
1160
1161   return(0);
1162}
1163
1164static void
1165em_free_pci_resources(struct adapter * Adapter)
1166{
1167   device_t dev = Adapter->dev;
1168
1169   if(Adapter->res_interrupt != NULL) {
1170      bus_teardown_intr(dev, Adapter->res_interrupt, Adapter->int_handler_tag);
1171      bus_release_resource(dev, SYS_RES_IRQ, 0, Adapter->res_interrupt);
1172   }
1173   if (Adapter->res_memory != NULL) {
1174      bus_release_resource(dev, SYS_RES_MEMORY, EM_MMBA, Adapter->res_memory);
1175   }
1176   return;
1177}
1178
1179/*********************************************************************
1180 *
1181 *  Initialize the hardware to a configuration as specified by the
1182 *  Adapter structure. The controller is reset, the EEPROM is
1183 *  verified, the MAC address is set, then the shared initialization
1184 *  routines are called.
1185 *
1186 **********************************************************************/
1187static int
1188em_hardware_init(struct adapter * Adapter)
1189{
1190   /* Issue a global reset */
1191   Adapter->shared.adapter_stopped = FALSE;
1192   em_adapter_stop(&Adapter->shared);
1193   Adapter->shared.adapter_stopped = FALSE;
1194
1195   /* Make sure we have a good EEPROM before we read from it */
1196   if (!em_validate_eeprom_checksum(&Adapter->shared)) {
1197      printf("em%d: The EEPROM Checksum Is Not Valid\n", Adapter->unit);
1198      return EIO;
1199   }
1200   /* Copy the permanent MAC address and part number out of the EEPROM */
1201   em_read_mac_address(Adapter, Adapter->interface_data.ac_enaddr);
1202   memcpy(Adapter->shared.mac_addr, Adapter->interface_data.ac_enaddr,
1203         ETH_LENGTH_OF_ADDRESS);
1204   em_read_part_num(&Adapter->shared, &(Adapter->PartNumber));
1205
1206   if (!em_init_hw(&Adapter->shared)) {
1207      printf("em%d: Hardware Initialization Failed", Adapter->unit);
1208      return EIO;
1209   }
1210
1211   em_check_for_link(&Adapter->shared);
1212   if (E1000_READ_REG(&Adapter->shared, STATUS) & E1000_STATUS_LU)
1213      Adapter->LinkIsActive = 1;
1214   else
1215      Adapter->LinkIsActive = 0;
1216
1217   if (Adapter->LinkIsActive) {
1218      em_get_speed_and_duplex(&Adapter->shared, &Adapter->LineSpeed, &Adapter->FullDuplex);
1219   } else {
1220      Adapter->LineSpeed = 0;
1221      Adapter->FullDuplex = 0;
1222   }
1223
1224   return 0;
1225}
1226
1227static void
1228em_read_mac_address(struct adapter * Adapter, u_int8_t * NodeAddress)
1229{
1230   u_int16_t       EepromWordValue;
1231   int             i;
1232
1233   for (i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
1234      EepromWordValue =
1235         em_read_eeprom(&Adapter->shared, EEPROM_NODE_ADDRESS_BYTE_0 + (i / 2));
1236      NodeAddress[i] = (uint8_t) (EepromWordValue & 0x00FF);
1237      NodeAddress[i + 1] = (uint8_t) (EepromWordValue >> 8);
1238   }
1239
1240   return;
1241}
1242
1243/*********************************************************************
1244 *
1245 *  Setup networking device structure and register an interface.
1246 *
1247 **********************************************************************/
1248static void
1249em_setup_interface(device_t dev, struct adapter * Adapter)
1250{
1251   struct ifnet   *ifp;
1252   INIT_DEBUGOUT("em_setup_interface: begin");
1253
1254   ifp = &Adapter->interface_data.ac_if;
1255   ifp->if_unit = Adapter->unit;
1256   ifp->if_name = "em";
1257   ifp->if_mtu = ETHERMTU;
1258   ifp->if_output = ether_output;
1259   ifp->if_baudrate = 1000000000;
1260   ifp->if_init =  em_init;
1261   ifp->if_softc = Adapter;
1262   ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1263   ifp->if_ioctl = em_ioctl;
1264   ifp->if_start = em_start;
1265   ifp->if_watchdog = em_watchdog;
1266   ifp->if_snd.ifq_maxlen = Adapter->NumTxDescriptors - 1;
1267   ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
1268
1269   /*
1270    * Specify the media types supported by this adapter and register
1271    * callbacks to update media and link information
1272    */
1273   ifmedia_init(&Adapter->media, IFM_IMASK, em_media_change,
1274                em_media_status);
1275   if (Adapter->shared.media_type == em_media_type_fiber) {
1276      ifmedia_add(&Adapter->media, IFM_ETHER | IFM_1000_SX | IFM_FDX, 0,
1277                  NULL);
1278      ifmedia_add(&Adapter->media, IFM_ETHER | IFM_1000_SX , 0, NULL);
1279   } else {
1280      ifmedia_add(&Adapter->media, IFM_ETHER | IFM_10_T, 0, NULL);
1281      ifmedia_add(&Adapter->media, IFM_ETHER | IFM_10_T | IFM_FDX, 0,
1282                  NULL);
1283      ifmedia_add(&Adapter->media, IFM_ETHER | IFM_100_TX, 0, NULL);
1284      ifmedia_add(&Adapter->media, IFM_ETHER | IFM_100_TX | IFM_FDX, 0,
1285                  NULL);
1286      ifmedia_add(&Adapter->media, IFM_ETHER | IFM_1000_TX | IFM_FDX, 0,
1287                  NULL);
1288      ifmedia_add(&Adapter->media, IFM_ETHER | IFM_1000_TX, 0, NULL);
1289   }
1290   ifmedia_add(&Adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
1291   ifmedia_set(&Adapter->media, IFM_ETHER | IFM_AUTO);
1292
1293   INIT_DEBUGOUT("em_setup_interface: end");
1294   return;
1295}
1296
1297
1298/*********************************************************************
1299 *
1300 *  Allocate memory for tx_buffer structures. The tx_buffer stores all
1301 *  the information needed to transmit a packet on the wire.
1302 *
1303 **********************************************************************/
1304static int
1305em_allocate_transmit_structures(struct adapter * Adapter)
1306{
1307   if (!(Adapter->tx_buffer_area =
1308         (struct em_tx_buffer *) malloc(sizeof(struct em_tx_buffer) *
1309                                        Adapter->NumTxDescriptors, M_DEVBUF,
1310                                        M_NOWAIT))) {
1311      printf("em%d: Unable to allocate tx_buffer memory\n", Adapter->unit);
1312      return ENOMEM;
1313   }
1314
1315   bzero(Adapter->tx_buffer_area,
1316         sizeof(struct em_tx_buffer) * Adapter->NumTxDescriptors);
1317
1318   return 0;
1319}
1320
1321/*********************************************************************
1322 *
1323 *  Allocate and initialize transmit structures.
1324 *
1325 **********************************************************************/
1326static int
1327em_setup_transmit_structures(struct adapter * Adapter)
1328{
1329   struct em_tx_buffer   *tx_buffer;
1330   int             i;
1331
1332   if (em_allocate_transmit_structures(Adapter))
1333      return ENOMEM;
1334
1335   Adapter->FirstTxDescriptor = Adapter->TxDescBase;
1336   Adapter->LastTxDescriptor =
1337      Adapter->FirstTxDescriptor + (Adapter->NumTxDescriptors - 1);
1338
1339
1340   STAILQ_INIT(&Adapter->FreeSwTxPacketList);
1341   STAILQ_INIT(&Adapter->UsedSwTxPacketList);
1342
1343   tx_buffer = Adapter->tx_buffer_area;
1344
1345   /* Setup the linked list of the tx_buffer's */
1346   for (i = 0; i < Adapter->NumTxDescriptors; i++, tx_buffer++) {
1347      bzero((void *) tx_buffer, sizeof(struct em_tx_buffer));
1348      STAILQ_INSERT_TAIL(&Adapter->FreeSwTxPacketList, tx_buffer, em_tx_entry);
1349   }
1350
1351   bzero((void *) Adapter->FirstTxDescriptor,
1352        (sizeof(struct em_tx_desc)) * Adapter->NumTxDescriptors);
1353
1354   /* Setup TX descriptor pointers */
1355   Adapter->NextAvailTxDescriptor = Adapter->FirstTxDescriptor;
1356   Adapter->OldestUsedTxDescriptor = Adapter->FirstTxDescriptor;
1357
1358   /* Set number of descriptors available */
1359   Adapter->NumTxDescriptorsAvail = Adapter->NumTxDescriptors;
1360
1361   /* Set checksum context */
1362   Adapter->ActiveChecksumContext = OFFLOAD_NONE;
1363
1364   return 0;
1365}
1366
1367/*********************************************************************
1368 *
1369 *  Enable transmit unit.
1370 *
1371 **********************************************************************/
1372static void
1373em_initialize_transmit_unit(struct adapter * Adapter)
1374{
1375   u_int32_t       reg_tctl;
1376   u_int32_t       reg_tipg = 0;
1377
1378   /* Setup the Base and Length of the Tx Descriptor Ring */
1379   E1000_WRITE_REG(&Adapter->shared, TDBAL, vtophys((vm_offset_t) Adapter->TxDescBase));
1380   E1000_WRITE_REG(&Adapter->shared, TDBAH, 0);
1381   E1000_WRITE_REG(&Adapter->shared, TDLEN, Adapter->NumTxDescriptors *
1382               sizeof(struct em_tx_desc));
1383
1384   /* Setup the HW Tx Head and Tail descriptor pointers */
1385   E1000_WRITE_REG(&Adapter->shared, TDH, 0);
1386   E1000_WRITE_REG(&Adapter->shared, TDT, 0);
1387
1388
1389   HW_DEBUGOUT2("Base = %x, Length = %x\n", E1000_READ_REG(&Adapter->shared, TDBAL),
1390                E1000_READ_REG(&Adapter->shared, TDLEN));
1391
1392
1393   /* Set the default values for the Tx Inter Packet Gap timer */
1394   switch (Adapter->shared.mac_type) {
1395   case em_82543:
1396   case em_82544:
1397      if (Adapter->shared.media_type == em_media_type_fiber)
1398         reg_tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
1399      else
1400         reg_tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
1401      reg_tipg |= DEFAULT_82543_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
1402      reg_tipg |= DEFAULT_82543_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
1403      break;
1404   case em_82542_rev2_0:
1405   case em_82542_rev2_1:
1406      reg_tipg = DEFAULT_82542_TIPG_IPGT;
1407      reg_tipg |= DEFAULT_82542_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
1408      reg_tipg |= DEFAULT_82542_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
1409      break;
1410   default:
1411      printf("em%d: Invalid mac type detected\n", Adapter->unit);
1412   }
1413   E1000_WRITE_REG(&Adapter->shared, TIPG, reg_tipg);
1414   E1000_WRITE_REG(&Adapter->shared, TIDV, Adapter->TxIntDelay);
1415
1416   /* Program the Transmit Control Register */
1417   reg_tctl = E1000_TCTL_PSP | E1000_TCTL_EN |
1418      (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
1419   if (Adapter->FullDuplex == 1) {
1420      reg_tctl |= E1000_FDX_COLLISION_DISTANCE << E1000_COLD_SHIFT;
1421   } else {
1422      reg_tctl |= E1000_HDX_COLLISION_DISTANCE << E1000_COLD_SHIFT;
1423   }
1424   E1000_WRITE_REG(&Adapter->shared, TCTL, reg_tctl);
1425
1426   /* Setup Transmit Descriptor Settings for this adapter */
1427   Adapter->TxdCmd = E1000_TXD_CMD_IFCS;
1428
1429   if(Adapter->TxIntDelay > 0)
1430      Adapter->TxdCmd |= E1000_TXD_CMD_IDE;
1431
1432   if(Adapter->shared.report_tx_early == 1)
1433      Adapter->TxdCmd |= E1000_TXD_CMD_RS;
1434   else
1435      Adapter->TxdCmd |= E1000_TXD_CMD_RPS;
1436
1437   return;
1438}
1439
1440/*********************************************************************
1441 *
1442 *  Free all transmit related data structures.
1443 *
1444 **********************************************************************/
1445static void
1446em_free_transmit_structures(struct adapter * Adapter)
1447{
1448   struct em_tx_buffer   *tx_buffer;
1449   int             i;
1450
1451   INIT_DEBUGOUT("free_transmit_structures: begin");
1452
1453   if (Adapter->tx_buffer_area != NULL) {
1454      tx_buffer = Adapter->tx_buffer_area;
1455      for (i = 0; i < Adapter->NumTxDescriptors; i++, tx_buffer++) {
1456         if (tx_buffer->Packet != NULL)
1457            m_freem(tx_buffer->Packet);
1458         tx_buffer->Packet = NULL;
1459      }
1460   }
1461   if (Adapter->tx_buffer_area != NULL) {
1462      free(Adapter->tx_buffer_area, M_DEVBUF);
1463      Adapter->tx_buffer_area = NULL;
1464   }
1465   return;
1466}
1467
1468/*********************************************************************
1469 *
1470 *  The offload context needs to be set when we transfer the first
1471 *  packet of a particular protocol (TCP/UDP). We change the
1472 *  context only if the protocol type changes.
1473 *
1474 **********************************************************************/
1475static void
1476em_transmit_checksum_setup(struct adapter * Adapter,
1477                struct mbuf *mp,
1478                struct em_tx_buffer *tx_buffer,
1479                u_int32_t *txd_upper,
1480                u_int32_t *txd_lower)
1481{
1482   struct em_context_desc *TXD;
1483   struct em_tx_desc * CurrentTxDescriptor;
1484
1485   if (mp->m_pkthdr.csum_flags) {
1486
1487      if(mp->m_pkthdr.csum_flags & CSUM_TCP) {
1488         TXCSUM_DEBUGOUT("Checksum TCP");
1489         *txd_upper = E1000_TXD_POPTS_TXSM << 8;
1490         *txd_lower = E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
1491         if(Adapter->ActiveChecksumContext == OFFLOAD_TCP_IP)
1492            return;
1493         else
1494            Adapter->ActiveChecksumContext = OFFLOAD_TCP_IP;
1495
1496      } else if(mp->m_pkthdr.csum_flags & CSUM_UDP) {
1497         TXCSUM_DEBUGOUT("Checksum UDP");
1498         *txd_upper = E1000_TXD_POPTS_TXSM << 8;
1499         *txd_lower = E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
1500         if(Adapter->ActiveChecksumContext == OFFLOAD_UDP_IP)
1501            return;
1502         else
1503            Adapter->ActiveChecksumContext = OFFLOAD_UDP_IP;
1504      } else {
1505         TXCSUM_DEBUGOUT("Invalid protocol for checksum calculation\n");
1506         *txd_upper = 0;
1507         *txd_lower = 0;
1508         return;
1509      }
1510   }
1511   else {
1512      TXCSUM_DEBUGOUT("No checksum detected\n");
1513      *txd_upper = 0;
1514      *txd_lower = 0;
1515      return;
1516   }
1517
1518   /* If we reach this point, the checksum offload context
1519    * needs to be reset.
1520    */
1521   CurrentTxDescriptor = Adapter->NextAvailTxDescriptor;
1522   TXD = (struct em_context_desc *)CurrentTxDescriptor;
1523
1524   TXD->lower_setup.ip_fields.ipcss = ETHER_HDR_LEN;
1525   TXD->lower_setup.ip_fields.ipcso = ETHER_HDR_LEN + offsetof(struct ip, ip_sum);
1526   TXD->lower_setup.ip_fields.ipcse = ETHER_HDR_LEN + sizeof(struct ip) - 1;
1527
1528   TXD->upper_setup.tcp_fields.tucss = ETHER_HDR_LEN + sizeof(struct ip);
1529   TXD->upper_setup.tcp_fields.tucse = 0;
1530
1531   if(Adapter->ActiveChecksumContext == OFFLOAD_TCP_IP) {
1532      TXD->upper_setup.tcp_fields.tucso = ETHER_HDR_LEN + sizeof(struct ip) +
1533         offsetof(struct tcphdr, th_sum);
1534   } else if (Adapter->ActiveChecksumContext == OFFLOAD_UDP_IP) {
1535      TXD->upper_setup.tcp_fields.tucso = ETHER_HDR_LEN + sizeof(struct ip) +
1536         offsetof(struct udphdr, uh_sum);
1537   }
1538
1539   TXD->tcp_seg_setup.data = 0;
1540   TXD->cmd_and_length = E1000_TXD_CMD_DEXT;
1541
1542   if (CurrentTxDescriptor == Adapter->LastTxDescriptor)
1543      Adapter->NextAvailTxDescriptor = Adapter->FirstTxDescriptor;
1544   else
1545      Adapter->NextAvailTxDescriptor++;
1546
1547   Adapter->NumTxDescriptorsAvail--;
1548
1549   tx_buffer->NumTxDescriptorsUsed++;
1550   return;
1551}
1552
1553
1554/*********************************************************************
1555 *
1556 *  Get a buffer from system mbuf buffer pool.
1557 *
1558 **********************************************************************/
1559static int
1560em_get_buf(struct em_rx_buffer *rx_buffer, struct adapter *Adapter,
1561           struct mbuf *mp)
1562{
1563   struct mbuf    *nmp;
1564   struct ifnet   *ifp;
1565
1566   ifp = &Adapter->interface_data.ac_if;
1567
1568   if (mp == NULL) {
1569      MGETHDR(nmp, M_DONTWAIT, MT_DATA);
1570      if (nmp == NULL) {
1571         printf("em%d: Mbuf allocation failed\n", Adapter->unit);
1572         Adapter->StdMbufFailed++;
1573         return (ENOBUFS);
1574      }
1575      MCLGET(nmp, M_DONTWAIT);
1576      if ((nmp->m_flags & M_EXT) == 0) {
1577         m_freem(nmp);
1578         printf("em%d: Mbuf cluster allocation failed\n", Adapter->unit);
1579         Adapter->StdClusterFailed++;
1580         return (ENOBUFS);
1581      }
1582      nmp->m_len = nmp->m_pkthdr.len = MCLBYTES;
1583   } else {
1584      nmp = mp;
1585      nmp->m_len = nmp->m_pkthdr.len = MCLBYTES;
1586      nmp->m_data = nmp->m_ext.ext_buf;
1587      nmp->m_next = NULL;
1588   }
1589
1590   if (ifp->if_mtu <= ETHERMTU) {
1591      m_adj(nmp, ETHER_ALIGN);
1592   }
1593
1594   rx_buffer->Packet = nmp;
1595   rx_buffer->LowPhysicalAddress = vtophys(mtod(nmp, vm_offset_t));
1596   rx_buffer->HighPhysicalAddress = 0;
1597
1598   return (0);
1599}
1600
1601/*********************************************************************
1602 *
1603 *  Allocate memory for rx_buffer structures. Since we use one
1604 *  rx_buffer per received packet, the maximum number of rx_buffer's
1605 *  that we'll need is equal to the number of receive descriptors
1606 *  that we've allocated.
1607 *
1608 **********************************************************************/
1609static int
1610em_allocate_receive_structures(struct adapter * Adapter)
1611{
1612   int             i;
1613   struct em_rx_buffer   *rx_buffer;
1614
1615   if (!(Adapter->rx_buffer_area =
1616        (struct em_rx_buffer *) malloc(sizeof(struct em_rx_buffer) *
1617                                       Adapter->NumRxDescriptors, M_DEVBUF,
1618                                       M_NOWAIT))) {
1619      printf("em%d: Unable to allocate rx_buffer memory\n", Adapter->unit);
1620      return (ENOMEM);
1621   }
1622
1623   bzero(Adapter->rx_buffer_area,
1624         sizeof(struct em_rx_buffer) * Adapter->NumRxDescriptors);
1625
1626   for (i = 0, rx_buffer = Adapter->rx_buffer_area;
1627       i < Adapter->NumRxDescriptors; i++, rx_buffer++) {
1628
1629      if (em_get_buf(rx_buffer, Adapter, NULL) == ENOBUFS) {
1630         rx_buffer->Packet = NULL;
1631         return (ENOBUFS);
1632      }
1633   }
1634
1635   return (0);
1636}
1637
1638/*********************************************************************
1639 *
1640 *  Allocate and initialize receive structures.
1641 *
1642 **********************************************************************/
1643static int
1644em_setup_receive_structures(struct adapter * Adapter)
1645{
1646   struct em_rx_buffer   *rx_buffer;
1647   struct em_rx_desc * RxDescriptorPtr;
1648   int             i;
1649
1650   if(em_allocate_receive_structures(Adapter))
1651      return ENOMEM;
1652
1653   STAILQ_INIT(&Adapter->RxSwPacketList);
1654
1655   Adapter->FirstRxDescriptor =
1656      (struct em_rx_desc *) Adapter->RxDescBase;
1657   Adapter->LastRxDescriptor =
1658      Adapter->FirstRxDescriptor + (Adapter->NumRxDescriptors - 1);
1659
1660   rx_buffer = (struct em_rx_buffer *) Adapter->rx_buffer_area;
1661
1662   bzero((void *) Adapter->FirstRxDescriptor,
1663        (sizeof(struct em_rx_desc)) * Adapter->NumRxDescriptors);
1664
1665   /* Build a linked list of rx_buffer's */
1666   for (i = 0, RxDescriptorPtr = Adapter->FirstRxDescriptor;
1667       i < Adapter->NumRxDescriptors;
1668       i++, rx_buffer++, RxDescriptorPtr++) {
1669      if (rx_buffer->Packet == NULL)
1670         printf("em%d: Receive buffer memory not allocated", Adapter->unit);
1671      else {
1672         RxDescriptorPtr->buffer_addr = rx_buffer->LowPhysicalAddress;
1673         STAILQ_INSERT_TAIL(&Adapter->RxSwPacketList, rx_buffer, em_rx_entry);
1674      }
1675   }
1676
1677   /* Setup our descriptor pointers */
1678   Adapter->NextRxDescriptorToCheck = Adapter->FirstRxDescriptor;
1679
1680   return(0);
1681}
1682
1683/*********************************************************************
1684 *
1685 *  Enable receive unit.
1686 *
1687 **********************************************************************/
1688static void
1689em_initialize_receive_unit(struct adapter * Adapter)
1690{
1691   u_int32_t       reg_rctl;
1692   u_int32_t       reg_rxcsum;
1693
1694   /* Make sure receives are disabled while setting up the descriptor ring */
1695   E1000_WRITE_REG(&Adapter->shared, RCTL, 0);
1696
1697   /* Set the Receive Delay Timer Register */
1698   E1000_WRITE_REG(&Adapter->shared, RDTR, Adapter->RxIntDelay | E1000_RDT_FPDB);
1699
1700   /* Setup the Base and Length of the Rx Descriptor Ring */
1701   E1000_WRITE_REG(&Adapter->shared, RDBAL, vtophys((vm_offset_t) Adapter->RxDescBase));
1702   E1000_WRITE_REG(&Adapter->shared, RDBAH, 0);
1703   E1000_WRITE_REG(&Adapter->shared, RDLEN, Adapter->NumRxDescriptors *
1704               sizeof(struct em_rx_desc));
1705
1706   /* Setup the HW Rx Head and Tail Descriptor Pointers */
1707   E1000_WRITE_REG(&Adapter->shared, RDH, 0);
1708   E1000_WRITE_REG(&Adapter->shared, RDT,
1709               (((u_int32_t) Adapter->LastRxDescriptor -
1710                 (u_int32_t) Adapter->FirstRxDescriptor) >> 4));
1711
1712   /* Setup the Receive Control Register */
1713   reg_rctl = E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
1714      E1000_RCTL_RDMTS_HALF |
1715      (Adapter->shared.mc_filter_type << E1000_RCTL_MO_SHIFT);
1716
1717   if (Adapter->shared.tbi_compatibility_on == TRUE)
1718      reg_rctl |= E1000_RCTL_SBP;
1719
1720
1721   switch (Adapter->RxBufferLen) {
1722   case EM_RXBUFFER_2048:
1723      reg_rctl |= E1000_RCTL_SZ_2048 | E1000_RCTL_LPE;
1724      break;
1725   case EM_RXBUFFER_4096:
1726      reg_rctl |= E1000_RCTL_SZ_4096 | E1000_RCTL_BSEX | E1000_RCTL_LPE;
1727      break;
1728   case EM_RXBUFFER_8192:
1729      reg_rctl |= E1000_RCTL_SZ_8192 | E1000_RCTL_BSEX | E1000_RCTL_LPE;
1730      break;
1731   case EM_RXBUFFER_16384:
1732      reg_rctl |= E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX | E1000_RCTL_LPE;
1733      break;
1734   default:
1735      reg_rctl |= E1000_RCTL_SZ_2048;
1736   }
1737
1738   /* Enable 82543 Receive Checksum Offload for TCP and UDP */
1739   if((Adapter->shared.mac_type >= em_82543) && (Adapter->RxChecksum == 1)) {
1740      reg_rxcsum = E1000_READ_REG(&Adapter->shared, RXCSUM);
1741      reg_rxcsum |= (E1000_RXCSUM_IPOFL | E1000_RXCSUM_TUOFL);
1742      E1000_WRITE_REG(&Adapter->shared, RXCSUM, reg_rxcsum);
1743   }
1744
1745   /* Enable Receives */
1746   E1000_WRITE_REG(&Adapter->shared, RCTL, reg_rctl);
1747
1748   return;
1749}
1750
1751/*********************************************************************
1752 *
1753 *  Free receive related data structures.
1754 *
1755 **********************************************************************/
1756static void
1757em_free_receive_structures(struct adapter * Adapter)
1758{
1759   struct em_rx_buffer   *rx_buffer;
1760   int             i;
1761
1762   INIT_DEBUGOUT("free_receive_structures: begin");
1763
1764   if (Adapter->rx_buffer_area != NULL) {
1765      rx_buffer = Adapter->rx_buffer_area;
1766      for (i = 0; i < Adapter->NumRxDescriptors; i++, rx_buffer++) {
1767         if (rx_buffer->Packet != NULL)
1768            m_freem(rx_buffer->Packet);
1769         rx_buffer->Packet = NULL;
1770      }
1771   }
1772   if (Adapter->rx_buffer_area != NULL) {
1773      free(Adapter->rx_buffer_area, M_DEVBUF);
1774      Adapter->rx_buffer_area = NULL;
1775   }
1776   return;
1777}
1778
1779/*********************************************************************
1780 *
1781 *  This routine executes in interrupt context. It replenishes
1782 *  the mbufs in the descriptor and sends data which has been
1783 *  dma'ed into host memory to upper layer.
1784 *
1785 *********************************************************************/
1786static void
1787em_process_receive_interrupts(struct adapter * Adapter)
1788{
1789   struct mbuf         *mp;
1790   struct ifnet        *ifp;
1791   struct ether_header *eh;
1792   u_int16_t           Length;
1793   u_int8_t            LastByte;
1794   u_int8_t            AcceptFrame = 0;
1795   u_int8_t            EndOfPacket = 0;
1796   u_int32_t           PacketLength = 0;
1797
1798   /* Pointer to the receive descriptor being examined. */
1799   struct em_rx_desc * CurrentDescriptor;
1800   struct em_rx_desc * LastDescriptorProcessed;
1801   struct em_rx_buffer   *rx_buffer;
1802
1803   TXRX_DEBUGOUT("em_process_receive_interrupts: begin");
1804
1805   ifp = &Adapter->interface_data.ac_if;
1806   CurrentDescriptor = Adapter->NextRxDescriptorToCheck;
1807
1808   if (!((CurrentDescriptor->status) & E1000_RXD_STAT_DD)) {
1809#ifdef DBG_STATS
1810      Adapter->NoPacketsAvail++;
1811#endif
1812      return;
1813   }
1814
1815   while (CurrentDescriptor->status & E1000_RXD_STAT_DD) {
1816
1817      /* Get a pointer to the actual receive buffer */
1818      rx_buffer = STAILQ_FIRST(&Adapter->RxSwPacketList);
1819
1820      if(rx_buffer == NULL) {
1821         printf("em%d: Found null rx_buffer\n", Adapter->unit);
1822         return;
1823      }
1824
1825      mp = rx_buffer->Packet;
1826      AcceptFrame = 1;
1827
1828      if (CurrentDescriptor->status & E1000_RXD_STAT_EOP) {
1829         EndOfPacket = 1;
1830         Length = CurrentDescriptor->length - ETHER_CRC_LEN;
1831      }
1832      else {
1833         EndOfPacket = 0;
1834         Length = CurrentDescriptor->length;
1835      }
1836
1837      if(CurrentDescriptor->errors & E1000_RXD_ERR_FRAME_ERR_MASK) {
1838
1839         /* Compute packet length for tbi_accept macro */
1840         PacketLength = CurrentDescriptor->length;
1841         if (Adapter->fmp != NULL) {
1842            PacketLength += Adapter->fmp->m_pkthdr.len;
1843         }
1844
1845         LastByte = *(mtod(rx_buffer->Packet,caddr_t) + CurrentDescriptor->length - 1);
1846
1847         if (TBI_ACCEPT(&Adapter->shared, 0, CurrentDescriptor->errors,
1848                        PacketLength, LastByte)) {
1849            PacketLength = em_tbi_adjust_stats(&Adapter->shared, &Adapter->stats,
1850                                               PacketLength, Adapter->shared.mac_addr);
1851            Length--;
1852         } else {
1853            AcceptFrame = 0;
1854         }
1855      }
1856
1857      if (AcceptFrame) {
1858
1859         if (em_get_buf(rx_buffer, Adapter, NULL) == ENOBUFS) {
1860            Adapter->DroppedPackets++;
1861            em_get_buf(rx_buffer, Adapter, mp);
1862            if(Adapter->fmp != NULL) m_freem(Adapter->fmp);
1863            Adapter->fmp = NULL;
1864            Adapter->lmp = NULL;
1865            break;
1866         }
1867
1868         /* Assign correct length to the current fragment */
1869         mp->m_len = Length;
1870
1871         if(Adapter->fmp == NULL) {
1872            mp->m_pkthdr.len = Length;
1873            Adapter->fmp = mp;       /* Store the first mbuf */
1874            Adapter->lmp = mp;
1875         }
1876         else {
1877            /* Chain mbuf's together */
1878            mp->m_flags &= ~M_PKTHDR;
1879            Adapter->lmp->m_next = mp;
1880            Adapter->lmp = Adapter->lmp->m_next;
1881            Adapter->fmp->m_pkthdr.len += Length;
1882         }
1883
1884         if (EndOfPacket) {
1885            Adapter->fmp->m_pkthdr.rcvif = ifp;
1886
1887            eh = mtod(Adapter->fmp, struct ether_header *);
1888
1889            /* Remove ethernet header from mbuf */
1890            m_adj(Adapter->fmp, sizeof(struct ether_header));
1891            em_receive_checksum(Adapter, CurrentDescriptor, Adapter->fmp);
1892            ether_input(ifp, eh, Adapter->fmp);
1893
1894            Adapter->fmp = NULL;
1895            Adapter->lmp = NULL;
1896         }
1897      } else {
1898         Adapter->DroppedPackets++;
1899         em_get_buf(rx_buffer, Adapter, mp);
1900         if(Adapter->fmp != NULL) m_freem(Adapter->fmp);
1901         Adapter->fmp = NULL;
1902         Adapter->lmp = NULL;
1903      }
1904
1905      /* Zero out the receive descriptors status  */
1906      CurrentDescriptor->status = 0;
1907
1908      if (rx_buffer->Packet != NULL) {
1909         CurrentDescriptor->buffer_addr = rx_buffer->LowPhysicalAddress;
1910      }
1911
1912      /* Advance our pointers to the next descriptor (checking for wrap). */
1913      if (CurrentDescriptor == Adapter->LastRxDescriptor)
1914         Adapter->NextRxDescriptorToCheck = Adapter->FirstRxDescriptor;
1915      else
1916         ((Adapter)->NextRxDescriptorToCheck)++;
1917
1918      LastDescriptorProcessed = CurrentDescriptor;
1919      CurrentDescriptor = Adapter->NextRxDescriptorToCheck;
1920      /*
1921       * Put the buffer that we just indicated back at the end of our list
1922       */
1923      STAILQ_REMOVE_HEAD(&Adapter->RxSwPacketList, em_rx_entry);
1924      STAILQ_INSERT_TAIL(&Adapter->RxSwPacketList, rx_buffer, em_rx_entry);
1925
1926      /* Advance the E1000's Receive Queue #0  "Tail Pointer". */
1927      E1000_WRITE_REG(&Adapter->shared, RDT, (((u_int32_t) LastDescriptorProcessed -
1928                        (u_int32_t) Adapter->FirstRxDescriptor) >> 4));
1929   }
1930   return;
1931}
1932
1933/*********************************************************************
1934 *
1935 *  Verify that the hardware indicated that the checksum is valid.
1936 *  Inform the stack about the status of checksum so that stack
1937 *  doesn't spend time verifying the checksum.
1938 *
1939 *********************************************************************/
1940static void
1941em_receive_checksum(struct adapter * Adapter,
1942           struct em_rx_desc * RxDescriptor,
1943           struct mbuf *mp)
1944{
1945   /* 82543 or newer only */
1946   if((Adapter->shared.mac_type < em_82543) ||
1947      /* Ignore Checksum bit is set */
1948      (RxDescriptor->status & E1000_RXD_STAT_IXSM)) {
1949      RXCSUM_DEBUGOUT("Ignoring checksum");
1950      mp->m_pkthdr.csum_flags = 0;
1951      return;
1952   }
1953
1954   if (RxDescriptor->status & E1000_RXD_STAT_IPCS) {
1955      /* Did it pass? */
1956      if (!(RxDescriptor->errors & E1000_RXD_ERR_IPE)) {
1957         /* IP Checksum Good */
1958         RXCSUM_DEBUGOUT("Good IP checksum");
1959         mp->m_pkthdr.csum_flags = CSUM_IP_CHECKED;
1960         mp->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1961
1962      }
1963      else {
1964         RXCSUM_DEBUGOUT("Bad IP checksum");
1965         mp->m_pkthdr.csum_flags = 0;
1966      }
1967   }
1968   else {
1969      RXCSUM_DEBUGOUT("IP Checksum not verified");
1970   }
1971
1972   if (RxDescriptor->status & E1000_RXD_STAT_TCPCS) {
1973      /* Did it pass? */
1974      if (!(RxDescriptor->errors & E1000_RXD_ERR_TCPE)) {
1975         RXCSUM_DEBUGOUT("Good TCP/UDP checksum");
1976         mp->m_pkthdr.csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1977         mp->m_pkthdr.csum_data = htons(0xffff);
1978      }
1979      else {
1980         RXCSUM_DEBUGOUT("Bad TCP/UDP checksum");
1981      }
1982   }
1983   else {
1984      RXCSUM_DEBUGOUT("TCP/UDP checksum not verified");
1985   }
1986
1987
1988   return;
1989}
1990
1991
1992static void
1993EnableInterrupts(struct adapter * Adapter)
1994{
1995   E1000_WRITE_REG(&Adapter->shared, IMS, (IMS_ENABLE_MASK));
1996   return;
1997}
1998
1999static void
2000DisableInterrupts(struct adapter * Adapter)
2001{
2002   E1000_WRITE_REG(&Adapter->shared, IMC, (0xffffffff & ~E1000_IMC_RXSEQ));
2003   return;
2004}
2005
2006void em_write_pci_cfg(struct em_shared_adapter *Adapter,
2007                      uint32_t reg,
2008                      uint16_t * value)
2009{
2010   pci_write_config(((struct em_osdep *)Adapter->back)->dev, reg, *value, 2);
2011}
2012
2013
2014/**********************************************************************
2015 *
2016 *  Update the board statistics counters.
2017 *
2018 **********************************************************************/
2019static void
2020em_update_stats_counters(struct adapter * Adapter)
2021{
2022   struct ifnet   *ifp;
2023
2024   Adapter->stats.crcerrs += E1000_READ_REG(&Adapter->shared, CRCERRS);
2025   Adapter->stats.symerrs += E1000_READ_REG(&Adapter->shared, SYMERRS);
2026   Adapter->stats.mpc += E1000_READ_REG(&Adapter->shared, MPC);
2027   Adapter->stats.scc += E1000_READ_REG(&Adapter->shared, SCC);
2028   Adapter->stats.ecol += E1000_READ_REG(&Adapter->shared, ECOL);
2029   Adapter->stats.mcc += E1000_READ_REG(&Adapter->shared, MCC);
2030   Adapter->stats.latecol += E1000_READ_REG(&Adapter->shared, LATECOL);
2031   Adapter->stats.colc += E1000_READ_REG(&Adapter->shared, COLC);
2032   Adapter->stats.dc += E1000_READ_REG(&Adapter->shared, DC);
2033   Adapter->stats.sec += E1000_READ_REG(&Adapter->shared, SEC);
2034   Adapter->stats.rlec += E1000_READ_REG(&Adapter->shared, RLEC);
2035   Adapter->stats.xonrxc += E1000_READ_REG(&Adapter->shared, XONRXC);
2036   Adapter->stats.xontxc += E1000_READ_REG(&Adapter->shared, XONTXC);
2037   Adapter->stats.xoffrxc += E1000_READ_REG(&Adapter->shared, XOFFRXC);
2038   Adapter->stats.xofftxc += E1000_READ_REG(&Adapter->shared, XOFFTXC);
2039   Adapter->stats.fcruc += E1000_READ_REG(&Adapter->shared, FCRUC);
2040   Adapter->stats.prc64 += E1000_READ_REG(&Adapter->shared, PRC64);
2041   Adapter->stats.prc127 += E1000_READ_REG(&Adapter->shared, PRC127);
2042   Adapter->stats.prc255 += E1000_READ_REG(&Adapter->shared, PRC255);
2043   Adapter->stats.prc511 += E1000_READ_REG(&Adapter->shared, PRC511);
2044   Adapter->stats.prc1023 += E1000_READ_REG(&Adapter->shared, PRC1023);
2045   Adapter->stats.prc1522 += E1000_READ_REG(&Adapter->shared, PRC1522);
2046   Adapter->stats.gprc += E1000_READ_REG(&Adapter->shared, GPRC);
2047   Adapter->stats.bprc += E1000_READ_REG(&Adapter->shared, BPRC);
2048   Adapter->stats.mprc += E1000_READ_REG(&Adapter->shared, MPRC);
2049   Adapter->stats.gptc += E1000_READ_REG(&Adapter->shared, GPTC);
2050
2051   /* For the 64-bit byte counters the low dword must be read first. */
2052   /* Both registers clear on the read of the high dword */
2053
2054   Adapter->stats.gorcl += E1000_READ_REG(&Adapter->shared, GORCL);
2055   Adapter->stats.gorch += E1000_READ_REG(&Adapter->shared, GORCH);
2056   Adapter->stats.gotcl += E1000_READ_REG(&Adapter->shared, GOTCL);
2057   Adapter->stats.gotch += E1000_READ_REG(&Adapter->shared, GOTCH);
2058
2059   Adapter->stats.rnbc += E1000_READ_REG(&Adapter->shared, RNBC);
2060   Adapter->stats.ruc += E1000_READ_REG(&Adapter->shared, RUC);
2061   Adapter->stats.rfc += E1000_READ_REG(&Adapter->shared, RFC);
2062   Adapter->stats.roc += E1000_READ_REG(&Adapter->shared, ROC);
2063   Adapter->stats.rjc += E1000_READ_REG(&Adapter->shared, RJC);
2064
2065   Adapter->stats.torl += E1000_READ_REG(&Adapter->shared, TORL);
2066   Adapter->stats.torh += E1000_READ_REG(&Adapter->shared, TORH);
2067   Adapter->stats.totl += E1000_READ_REG(&Adapter->shared, TOTL);
2068   Adapter->stats.toth += E1000_READ_REG(&Adapter->shared, TOTH);
2069
2070   Adapter->stats.tpr += E1000_READ_REG(&Adapter->shared, TPR);
2071   Adapter->stats.tpt += E1000_READ_REG(&Adapter->shared, TPT);
2072   Adapter->stats.ptc64 += E1000_READ_REG(&Adapter->shared, PTC64);
2073   Adapter->stats.ptc127 += E1000_READ_REG(&Adapter->shared, PTC127);
2074   Adapter->stats.ptc255 += E1000_READ_REG(&Adapter->shared, PTC255);
2075   Adapter->stats.ptc511 += E1000_READ_REG(&Adapter->shared, PTC511);
2076   Adapter->stats.ptc1023 += E1000_READ_REG(&Adapter->shared, PTC1023);
2077   Adapter->stats.ptc1522 += E1000_READ_REG(&Adapter->shared, PTC1522);
2078   Adapter->stats.mptc += E1000_READ_REG(&Adapter->shared, MPTC);
2079   Adapter->stats.bptc += E1000_READ_REG(&Adapter->shared, BPTC);
2080
2081   if (Adapter->shared.mac_type >= em_82543) {
2082      Adapter->stats.algnerrc += E1000_READ_REG(&Adapter->shared, ALGNERRC);
2083      Adapter->stats.rxerrc += E1000_READ_REG(&Adapter->shared, RXERRC);
2084      Adapter->stats.tncrs += E1000_READ_REG(&Adapter->shared, TNCRS);
2085      Adapter->stats.cexterr += E1000_READ_REG(&Adapter->shared, CEXTERR);
2086      Adapter->stats.tsctc += E1000_READ_REG(&Adapter->shared, TSCTC);
2087      Adapter->stats.tsctfc += E1000_READ_REG(&Adapter->shared, TSCTFC);
2088   }
2089   ifp = &Adapter->interface_data.ac_if;
2090
2091   /* Fill out the OS statistics structure */
2092   ifp->if_ipackets = Adapter->stats.gprc;
2093   ifp->if_opackets = Adapter->stats.gptc;
2094   ifp->if_ibytes = Adapter->stats.gorcl;
2095   ifp->if_obytes = Adapter->stats.gotcl;
2096   ifp->if_imcasts = Adapter->stats.mprc;
2097   ifp->if_collisions = Adapter->stats.colc;
2098
2099   /* Rx Errors */
2100   ifp->if_ierrors =
2101      Adapter->DroppedPackets +
2102      Adapter->stats.rxerrc +
2103      Adapter->stats.crcerrs +
2104      Adapter->stats.algnerrc +
2105      Adapter->stats.rlec + Adapter->stats.rnbc +
2106      Adapter->stats.mpc + Adapter->stats.cexterr;
2107
2108   /* Tx Errors */
2109   ifp->if_oerrors = Adapter->stats.ecol + Adapter->stats.latecol;
2110
2111}
2112
2113
2114/**********************************************************************
2115 *
2116 *  This routine is called only when em_display_debug_stats is enabled.
2117 *  This routine provides a way to take a look at important statistics
2118 *  maintained by the driver and hardware.
2119 *
2120 **********************************************************************/
2121static void
2122em_print_hw_stats(struct adapter * Adapter)
2123{
2124   int unit = Adapter->unit;
2125
2126#ifdef DBG_STATS
2127   printf("em%d: Tx Descriptors not Avail = %ld\n", unit, Adapter->NoTxDescAvail);
2128   printf("em%d: Packets not Avail = %ld\n", unit, Adapter->NoPacketsAvail);
2129   printf("em%d: CleanTxInterrupts = %ld\n", unit, Adapter->CleanTxInterrupts);
2130   printf("em%d: Tx Buffer not avail1 = %ld\n", unit, Adapter->NoTxBufferAvail1);
2131   printf("em%d: Tx Buffer not avail2 = %ld\n", unit, Adapter->NoTxBufferAvail2);
2132#endif
2133   printf("em%d: No Jumbo Buffer Avail = %ld\n",unit, Adapter->NoJumboBufAvail);
2134   printf("em%d: Jumbo Mbuf Failed = %ld\n",unit, Adapter->JumboMbufFailed);
2135   printf("em%d: Jumbo Cluster Failed = %ld\n",unit, Adapter->JumboClusterFailed);
2136   printf("em%d: Std Mbuf Failed = %ld\n",unit, Adapter->StdMbufFailed);
2137   printf("em%d: Std Cluster Failed = %ld\n",unit, Adapter->StdClusterFailed);
2138
2139   printf("em%d: Symbol errors = %lld\n", unit, Adapter->stats.symerrs);
2140   printf("em%d: Sequence errors = %lld\n", unit, Adapter->stats.sec);
2141   printf("em%d: Defer count = %lld\n", unit, Adapter->stats.dc);
2142
2143   printf("em%d: Missed Packets = %lld\n", unit, Adapter->stats.mpc);
2144   printf("em%d: Receive No Buffers = %lld\n", unit, Adapter->stats.rnbc);
2145   printf("em%d: Receive length errors = %lld\n", unit, Adapter->stats.rlec);
2146   printf("em%d: Receive errors = %lld\n", unit, Adapter->stats.rxerrc);
2147   printf("em%d: Crc errors = %lld\n", unit, Adapter->stats.crcerrs);
2148   printf("em%d: Alignment errors = %lld\n", unit, Adapter->stats.algnerrc);
2149   printf("em%d: Carrier extension errors = %lld\n", unit, Adapter->stats.cexterr);
2150   printf("em%d: Driver dropped packets = %ld\n", unit, Adapter->DroppedPackets);
2151
2152   printf("em%d: XON Rcvd = %lld\n", unit, Adapter->stats.xonrxc);
2153   printf("em%d: XON Xmtd = %lld\n", unit, Adapter->stats.xontxc);
2154   printf("em%d: XOFF Rcvd = %lld\n", unit, Adapter->stats.xoffrxc);
2155   printf("em%d: XOFF Xmtd = %lld\n", unit, Adapter->stats.xofftxc);
2156
2157   printf("em%d: Good Packets Rcvd = %lld\n", unit, Adapter->stats.gprc);
2158   printf("em%d: Good Packets Xmtd = %lld\n", unit, Adapter->stats.gptc);
2159}
2160
2161
2162/**********************************************************************
2163 *
2164 *  Examine each tx_buffer in the used queue. If the hardware is done
2165 *  processing the packet then free associated resources. The
2166 *  tx_buffer is put back on the free queue.
2167 *
2168 **********************************************************************/
2169static void
2170em_clean_transmit_interrupts(struct adapter * Adapter)
2171{
2172   struct em_tx_buffer *tx_buffer;
2173   struct em_tx_desc *TransmitDescriptor;
2174   int             s;
2175   struct ifnet   *ifp;
2176
2177   s = splimp();
2178#ifdef DBG_STATS
2179   Adapter->CleanTxInterrupts++;
2180#endif
2181
2182   for (tx_buffer = STAILQ_FIRST(&Adapter->UsedSwTxPacketList);
2183        tx_buffer;
2184        tx_buffer = STAILQ_FIRST(&Adapter->UsedSwTxPacketList)) {
2185
2186      /*
2187       * Get hold of the next descriptor that the em will report status
2188       * back to (this will be the last descriptor of a given tx_buffer). We
2189       * only want to free the tx_buffer (and it resources) if the driver is
2190       * done with ALL of the descriptors.  If the driver is done with the
2191       * last one then it is done with all of them.
2192       */
2193
2194      TransmitDescriptor = Adapter->OldestUsedTxDescriptor +
2195         (tx_buffer->NumTxDescriptorsUsed - 1);
2196
2197      /* Check for wrap case */
2198      if (TransmitDescriptor > Adapter->LastTxDescriptor)
2199         TransmitDescriptor -= Adapter->NumTxDescriptors;
2200
2201
2202      /*
2203       * If the descriptor done bit is set free tx_buffer and associated
2204       * resources
2205       */
2206      if (TransmitDescriptor->upper.fields.status &
2207         E1000_TXD_STAT_DD) {
2208
2209         STAILQ_REMOVE_HEAD(&Adapter->UsedSwTxPacketList, em_tx_entry);
2210
2211         if ((TransmitDescriptor == Adapter->LastTxDescriptor))
2212            Adapter->OldestUsedTxDescriptor =
2213               Adapter->FirstTxDescriptor;
2214         else
2215            Adapter->OldestUsedTxDescriptor = (TransmitDescriptor + 1);
2216
2217         /* Make available the descriptors that were previously used */
2218         Adapter->NumTxDescriptorsAvail +=
2219            tx_buffer->NumTxDescriptorsUsed;
2220
2221         tx_buffer->NumTxDescriptorsUsed = 0;
2222
2223         if (tx_buffer->Packet) {
2224            m_freem(tx_buffer->Packet);
2225            tx_buffer->Packet = NULL;
2226         }
2227         /* Return this "Software packet" back to the "free" list */
2228         STAILQ_INSERT_TAIL(&Adapter->FreeSwTxPacketList, tx_buffer, em_tx_entry);
2229      } else {
2230         /*
2231          * Found a tx_buffer that the em is not done with then there is
2232          * no reason to check the rest of the queue.
2233          */
2234         break;
2235      }
2236   }                     /* end for each tx_buffer */
2237
2238   ifp = &Adapter->interface_data.ac_if;
2239
2240   /* Tell the stack that it is OK to send packets */
2241   if (Adapter->NumTxDescriptorsAvail > TX_CLEANUP_THRESHOLD) {
2242      ifp->if_timer = 0;
2243      ifp->if_flags &= ~IFF_OACTIVE;
2244   }
2245   splx(s);
2246   return;
2247}
2248
2249