1/*******************************************************************************
2
3Copyright (c) 2001-2004, Intel Corporation
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, 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
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30POSSIBILITY OF SUCH DAMAGE.
31
32***************************************************************************/
33
34/*$FreeBSD: head/sys/dev/ixgb/if_ixgb.c 148887 2005-08-09 10:20:02Z rwatson $*/
35
36#include <dev/ixgb/if_ixgb.h>
37
38/*********************************************************************
39 * Set this to one to display debug statistics
40 *********************************************************************/
41int ixgb_display_debug_stats = 0;
42
43/*********************************************************************
44 * Linked list of board private structures for all NICs found
45 *********************************************************************/
46
47struct adapter *ixgb_adapter_list = NULL;
48
49
50
51/*********************************************************************
52 * Driver version
53 *********************************************************************/
54
55char ixgb_driver_version[] = "1.0.6";
56char ixgb_copyright[] = "Copyright (c) 2001-2004 Intel Corporation.";
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 ixgb_strings
63 * Last entry must be all 0s
64 *
65 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
66 *********************************************************************/
67
68static ixgb_vendor_info_t ixgb_vendor_info_array[] =
69{
70 /* Intel(R) PRO/10000 Network Connection */
71 {INTEL_VENDOR_ID, IXGB_DEVICE_ID_82597EX, PCI_ANY_ID, PCI_ANY_ID, 0},
72 {INTEL_VENDOR_ID, IXGB_DEVICE_ID_82597EX_SR, PCI_ANY_ID, PCI_ANY_ID, 0},
73 /* required last entry */
74 {0, 0, 0, 0, 0}
75};
76
77/*********************************************************************
78 * Table of branding strings for all supported NICs.
79 *********************************************************************/
80
81static char *ixgb_strings[] = {
82 "Intel(R) PRO/10GbE Network Driver"
83};
84
85/*********************************************************************
86 * Function prototypes
87 *********************************************************************/
88static int ixgb_probe(device_t);
89static int ixgb_attach(device_t);
90static int ixgb_detach(device_t);
91static int ixgb_shutdown(device_t);
92static void ixgb_intr(void *);
93static void ixgb_start(struct ifnet *);
94static void ixgb_start_locked(struct ifnet *);
95static int ixgb_ioctl(struct ifnet *, IOCTL_CMD_TYPE, caddr_t);
96static void ixgb_watchdog(struct ifnet *);
97static void ixgb_init(void *);
98static void ixgb_init_locked(struct adapter *);
99static void ixgb_stop(void *);
100static void ixgb_media_status(struct ifnet *, struct ifmediareq *);
101static int ixgb_media_change(struct ifnet *);
102static void ixgb_identify_hardware(struct adapter *);
103static int ixgb_allocate_pci_resources(struct adapter *);
104static void ixgb_free_pci_resources(struct adapter *);
105static void ixgb_local_timer(void *);
106static int ixgb_hardware_init(struct adapter *);
107static void ixgb_setup_interface(device_t, struct adapter *);
108static int ixgb_setup_transmit_structures(struct adapter *);
109static void ixgb_initialize_transmit_unit(struct adapter *);
110static int ixgb_setup_receive_structures(struct adapter *);
111static void ixgb_initialize_receive_unit(struct adapter *);
112static void ixgb_enable_intr(struct adapter *);
113static void ixgb_disable_intr(struct adapter *);
114static void ixgb_free_transmit_structures(struct adapter *);
115static void ixgb_free_receive_structures(struct adapter *);
116static void ixgb_update_stats_counters(struct adapter *);
117static void ixgb_clean_transmit_interrupts(struct adapter *);
118static int ixgb_allocate_receive_structures(struct adapter *);
119static int ixgb_allocate_transmit_structures(struct adapter *);
120static void ixgb_process_receive_interrupts(struct adapter *, int);
121static void
122ixgb_receive_checksum(struct adapter *,
123 struct ixgb_rx_desc * rx_desc,
124 struct mbuf *);
125static void
126ixgb_transmit_checksum_setup(struct adapter *,
127 struct mbuf *,
128 u_int8_t *);
129static void ixgb_set_promisc(struct adapter *);
130static void ixgb_disable_promisc(struct adapter *);
131static void ixgb_set_multi(struct adapter *);
132static void ixgb_print_hw_stats(struct adapter *);
133static void ixgb_print_link_status(struct adapter *);
134static int
135ixgb_get_buf(int i, struct adapter *,
136 struct mbuf *);
137static void ixgb_enable_vlans(struct adapter * adapter);
138static int ixgb_encap(struct adapter * adapter, struct mbuf * m_head);
139static int ixgb_sysctl_stats(SYSCTL_HANDLER_ARGS);
140static int
141ixgb_dma_malloc(struct adapter *, bus_size_t,
142 struct ixgb_dma_alloc *, int);
143static void ixgb_dma_free(struct adapter *, struct ixgb_dma_alloc *);
144
145/*********************************************************************
146 * FreeBSD Device Interface Entry Points
147 *********************************************************************/
148
149static device_method_t ixgb_methods[] = {
150 /* Device interface */
151 DEVMETHOD(device_probe, ixgb_probe),
152 DEVMETHOD(device_attach, ixgb_attach),
153 DEVMETHOD(device_detach, ixgb_detach),
154 DEVMETHOD(device_shutdown, ixgb_shutdown),
155 {0, 0}
156};
157
158static driver_t ixgb_driver = {
159 "ixgb", ixgb_methods, sizeof(struct adapter),
160};
161
162static devclass_t ixgb_devclass;
163DRIVER_MODULE(if_ixgb, pci, ixgb_driver, ixgb_devclass, 0, 0);
164
165MODULE_DEPEND(if_ixgb, pci, 1, 1, 1);
166MODULE_DEPEND(if_ixgb, ether, 1, 1, 1);
167
168/* some defines for controlling descriptor fetches in h/w */
169#define RXDCTL_PTHRESH_DEFAULT 128 /* chip considers prefech below this */
170#define RXDCTL_HTHRESH_DEFAULT 16 /* chip will only prefetch if tail is
171 * pushed this many descriptors from
172 * head */
173#define RXDCTL_WTHRESH_DEFAULT 0 /* chip writes back at this many or RXT0 */
174
175
176/*********************************************************************
177 * Device identification routine
178 *
179 * ixgb_probe determines if the driver should be loaded on
180 * adapter based on PCI vendor/device id of the adapter.
181 *
182 * return 0 on success, positive on failure
183 *********************************************************************/
184
185static int
186ixgb_probe(device_t dev)
187{
188 ixgb_vendor_info_t *ent;
189
190 u_int16_t pci_vendor_id = 0;
191 u_int16_t pci_device_id = 0;
192 u_int16_t pci_subvendor_id = 0;
193 u_int16_t pci_subdevice_id = 0;
194 char adapter_name[60];
195
196 INIT_DEBUGOUT("ixgb_probe: begin");
197
198 pci_vendor_id = pci_get_vendor(dev);
199 if (pci_vendor_id != IXGB_VENDOR_ID)
200 return (ENXIO);
201
202 pci_device_id = pci_get_device(dev);
203 pci_subvendor_id = pci_get_subvendor(dev);
204 pci_subdevice_id = pci_get_subdevice(dev);
205
206 ent = ixgb_vendor_info_array;
207 while (ent->vendor_id != 0) {
208 if ((pci_vendor_id == ent->vendor_id) &&
209 (pci_device_id == ent->device_id) &&
210
211 ((pci_subvendor_id == ent->subvendor_id) ||
212 (ent->subvendor_id == PCI_ANY_ID)) &&
213
214 ((pci_subdevice_id == ent->subdevice_id) ||
215 (ent->subdevice_id == PCI_ANY_ID))) {
216 sprintf(adapter_name, "%s, Version - %s",
217 ixgb_strings[ent->index],
218 ixgb_driver_version);
219 device_set_desc_copy(dev, adapter_name);
220 return (BUS_PROBE_DEFAULT);
221 }
222 ent++;
223 }
224
225 return (ENXIO);
226}
227
228/*********************************************************************
229 * Device initialization routine
230 *
231 * The attach entry point is called when the driver is being loaded.
232 * This routine identifies the type of hardware, allocates all resources
233 * and initializes the hardware.
234 *
235 * return 0 on success, positive on failure
236 *********************************************************************/
237
238static int
239ixgb_attach(device_t dev)
240{
241 struct adapter *adapter;
242 int tsize, rsize;
243 int error = 0;
244
245 printf("ixgb%d: %s\n", device_get_unit(dev), ixgb_copyright);
246 INIT_DEBUGOUT("ixgb_attach: begin");
247
248 /* Allocate, clear, and link in our adapter structure */
249 if (!(adapter = device_get_softc(dev))) {
250 printf("ixgb: adapter structure allocation failed\n");
251 return (ENOMEM);
252 }
253 bzero(adapter, sizeof(struct adapter));
254 adapter->dev = dev;
255 adapter->osdep.dev = dev;
256 adapter->unit = device_get_unit(dev);
257 IXGB_LOCK_INIT(adapter, device_get_nameunit(dev));
258
259 if (ixgb_adapter_list != NULL)
260 ixgb_adapter_list->prev = adapter;
261 adapter->next = ixgb_adapter_list;
262 ixgb_adapter_list = adapter;
263
264 /* SYSCTL APIs */
265 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
266 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
267 OID_AUTO, "stats", CTLTYPE_INT | CTLFLAG_RW,
268 (void *)adapter, 0,
269 ixgb_sysctl_stats, "I", "Statistics");
270
271 callout_init(&adapter->timer, CALLOUT_MPSAFE);
272
273 /* Determine hardware revision */
274 ixgb_identify_hardware(adapter);
275
276 /* Parameters (to be read from user) */
277 adapter->num_tx_desc = IXGB_MAX_TXD;
278 adapter->num_rx_desc = IXGB_MAX_RXD;
279 adapter->tx_int_delay = TIDV;
280 adapter->rx_int_delay = RDTR;
281 adapter->rx_buffer_len = IXGB_RXBUFFER_2048;
282
283 adapter->hw.fc.high_water = FCRTH;
284 adapter->hw.fc.low_water = FCRTL;
285 adapter->hw.fc.pause_time = FCPAUSE;
286 adapter->hw.fc.send_xon = TRUE;
287 adapter->hw.fc.type = FLOW_CONTROL;
288
289
290 /* Set the max frame size assuming standard ethernet sized frames */
291 adapter->hw.max_frame_size =
292 ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN;
293
294 if (ixgb_allocate_pci_resources(adapter)) {
295 printf("ixgb%d: Allocation of PCI resources failed\n",
296 adapter->unit);
297 error = ENXIO;
298 goto err_pci;
299 }
300 tsize = IXGB_ROUNDUP(adapter->num_tx_desc *
301 sizeof(struct ixgb_tx_desc), 4096);
302
303 /* Allocate Transmit Descriptor ring */
304 if (ixgb_dma_malloc(adapter, tsize, &adapter->txdma, BUS_DMA_NOWAIT)) {
305 printf("ixgb%d: Unable to allocate TxDescriptor memory\n",
306 adapter->unit);
307 error = ENOMEM;
308 goto err_tx_desc;
309 }
310 adapter->tx_desc_base = (struct ixgb_tx_desc *) adapter->txdma.dma_vaddr;
311
312 rsize = IXGB_ROUNDUP(adapter->num_rx_desc *
313 sizeof(struct ixgb_rx_desc), 4096);
314
315 /* Allocate Receive Descriptor ring */
316 if (ixgb_dma_malloc(adapter, rsize, &adapter->rxdma, BUS_DMA_NOWAIT)) {
317 printf("ixgb%d: Unable to allocate rx_desc memory\n",
318 adapter->unit);
319 error = ENOMEM;
320 goto err_rx_desc;
321 }
322 adapter->rx_desc_base = (struct ixgb_rx_desc *) adapter->rxdma.dma_vaddr;
323
324 /* Initialize the hardware */
325 if (ixgb_hardware_init(adapter)) {
326 printf("ixgb%d: Unable to initialize the hardware\n",
327 adapter->unit);
328 error = EIO;
329 goto err_hw_init;
330 }
331 /* Setup OS specific network interface */
332 ixgb_setup_interface(dev, adapter);
333
334 /* Initialize statistics */
335 ixgb_clear_hw_cntrs(&adapter->hw);
336 ixgb_update_stats_counters(adapter);
337
338 INIT_DEBUGOUT("ixgb_attach: end");
339 return (0);
340
341err_hw_init:
342 ixgb_dma_free(adapter, &adapter->rxdma);
343err_rx_desc:
344 ixgb_dma_free(adapter, &adapter->txdma);
345err_tx_desc:
346err_pci:
347 ixgb_free_pci_resources(adapter);
348 sysctl_ctx_free(&adapter->sysctl_ctx);
349 return (error);
350
351}
352
353/*********************************************************************
354 * Device removal routine
355 *
356 * The detach entry point is called when the driver is being removed.
357 * This routine stops the adapter and deallocates all the resources
358 * that were allocated for driver operation.
359 *
360 * return 0 on success, positive on failure
361 *********************************************************************/
362
363static int
364ixgb_detach(device_t dev)
365{
366 struct adapter *adapter = device_get_softc(dev);
367 struct ifnet *ifp = adapter->ifp;
368
369 INIT_DEBUGOUT("ixgb_detach: begin");
370
371 IXGB_LOCK(adapter);
372 adapter->in_detach = 1;
373
374 ixgb_stop(adapter);
375 IXGB_UNLOCK(adapter);
376
377#if __FreeBSD_version < 500000
378 ether_ifdetach(adapter->ifp, ETHER_BPF_SUPPORTED);
379#else
380 ether_ifdetach(adapter->ifp);
381 if_free(adapter->ifp);
382#endif
383 ixgb_free_pci_resources(adapter);
384
385
386 /* Free Transmit Descriptor ring */
387 if (adapter->tx_desc_base) {
388 ixgb_dma_free(adapter, &adapter->txdma);
389 adapter->tx_desc_base = NULL;
390 }
391 /* Free Receive Descriptor ring */
392 if (adapter->rx_desc_base) {
393 ixgb_dma_free(adapter, &adapter->rxdma);
394 adapter->rx_desc_base = NULL;
395 }
396 /* Remove from the adapter list */
397 if (ixgb_adapter_list == adapter)
398 ixgb_adapter_list = adapter->next;
399 if (adapter->next != NULL)
400 adapter->next->prev = adapter->prev;
401 if (adapter->prev != NULL)
402 adapter->prev->next = adapter->next;
403
404 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
405 ifp->if_timer = 0;
406
407 IXGB_LOCK_DESTROY(adapter);
408 return (0);
409}
410
411/*********************************************************************
412 *
413 * Shutdown entry point
414 *
415 **********************************************************************/
416
417static int
418ixgb_shutdown(device_t dev)
419{
420 struct adapter *adapter = device_get_softc(dev);
421 IXGB_LOCK(adapter);
422 ixgb_stop(adapter);
423 IXGB_UNLOCK(adapter);
424 return (0);
425}
426
427
428/*********************************************************************
429 * Transmit entry point
430 *
431 * ixgb_start is called by the stack to initiate a transmit.
432 * The driver will remain in this routine as long as there are
433 * packets to transmit and transmit resources are available.
434 * In case resources are not available stack is notified and
435 * the packet is requeued.
436 **********************************************************************/
437
438static void
439ixgb_start_locked(struct ifnet * ifp)
440{
441 struct mbuf *m_head;
442 struct adapter *adapter = ifp->if_softc;
443
444 IXGB_LOCK_ASSERT(adapter);
445
446 if (!adapter->link_active)
447 return;
448
449 while (ifp->if_snd.ifq_head != NULL) {
450 IF_DEQUEUE(&ifp->if_snd, m_head);
451
452 if (m_head == NULL)
453 break;
454
455 if (ixgb_encap(adapter, m_head)) {
456 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
457 IF_PREPEND(&ifp->if_snd, m_head);
458 break;
459 }
460 /* Send a copy of the frame to the BPF listener */
461#if __FreeBSD_version < 500000
462 if (ifp->if_bpf)
463 bpf_mtap(ifp, m_head);
464#else
465 BPF_MTAP(ifp, m_head);
466#endif
467 /* Set timeout in case hardware has problems transmitting */
468 ifp->if_timer = IXGB_TX_TIMEOUT;
469
470 }
471 return;
472}
473
474static void
475ixgb_start(struct ifnet *ifp)
476{
477 struct adapter *adapter = ifp->if_softc;
478
479 IXGB_LOCK(adapter);
480 ixgb_start_locked(ifp);
481 IXGB_UNLOCK(adapter);
482 return;
483}
484
485/*********************************************************************
486 * Ioctl entry point
487 *
488 * ixgb_ioctl is called when the user wants to configure the
489 * interface.
490 *
491 * return 0 on success, positive on failure
492 **********************************************************************/
493
494static int
495ixgb_ioctl(struct ifnet * ifp, IOCTL_CMD_TYPE command, caddr_t data)
496{
497 int mask, error = 0;
498 struct ifreq *ifr = (struct ifreq *) data;
499 struct adapter *adapter = ifp->if_softc;
500
501 if (adapter->in_detach)
502 goto out;
503
504 switch (command) {
505 case SIOCSIFADDR:
506 case SIOCGIFADDR:
507 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCxIFADDR (Get/Set Interface Addr)");
508 ether_ioctl(ifp, command, data);
509 break;
510 case SIOCSIFMTU:
511 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFMTU (Set Interface MTU)");
512 if (ifr->ifr_mtu > IXGB_MAX_JUMBO_FRAME_SIZE - ETHER_HDR_LEN) {
513 error = EINVAL;
514 } else {
515 IXGB_LOCK(adapter);
516 ifp->if_mtu = ifr->ifr_mtu;
517 adapter->hw.max_frame_size =
518 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
519
520 ixgb_init_locked(adapter);
521 IXGB_UNLOCK(adapter);
522 }
523 break;
524 case SIOCSIFFLAGS:
525 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFFLAGS (Set Interface Flags)");
526 IXGB_LOCK(adapter);
527 if (ifp->if_flags & IFF_UP) {
528 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
529 ixgb_init_locked(adapter);
530 }
531 ixgb_disable_promisc(adapter);
532 ixgb_set_promisc(adapter);
533 } else {
534 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
535 ixgb_stop(adapter);
536 }
537 }
538 IXGB_UNLOCK(adapter);
539 break;
540 case SIOCADDMULTI:
541 case SIOCDELMULTI:
542 IOCTL_DEBUGOUT("ioctl rcv'd: SIOC(ADD|DEL)MULTI");
543 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
544 IXGB_LOCK(adapter);
545 ixgb_disable_intr(adapter);
546 ixgb_set_multi(adapter);
547 ixgb_enable_intr(adapter);
548 IXGB_UNLOCK(adapter);
549 }
550 break;
551 case SIOCSIFMEDIA:
552 case SIOCGIFMEDIA:
553 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCxIFMEDIA (Get/Set Interface Media)");
554 error = ifmedia_ioctl(ifp, ifr, &adapter->media, command);
555 break;
556 case SIOCSIFCAP:
557 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFCAP (Set Capabilities)");
558 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
559 if (mask & IFCAP_HWCSUM) {
560 if (IFCAP_HWCSUM & ifp->if_capenable)
561 ifp->if_capenable &= ~IFCAP_HWCSUM;
562 else
563 ifp->if_capenable |= IFCAP_HWCSUM;
564 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
565 ixgb_init(adapter);
566 }
567 break;
568 default:
569 IOCTL_DEBUGOUT1("ioctl received: UNKNOWN (0x%X)\n", (int)command);
570 error = EINVAL;
571 }
572
573out:
574 return (error);
575}
576
577/*********************************************************************
578 * Watchdog entry point
579 *
580 * This routine is called whenever hardware quits transmitting.
581 *
582 **********************************************************************/
583
584static void
585ixgb_watchdog(struct ifnet * ifp)
586{
587 struct adapter *adapter;
588 adapter = ifp->if_softc;
589
590 /*
591 * If we are in this routine because of pause frames, then don't
592 * reset the hardware.
593 */
594 if (IXGB_READ_REG(&adapter->hw, STATUS) & IXGB_STATUS_TXOFF) {
595 ifp->if_timer = IXGB_TX_TIMEOUT;
596 return;
597 }
598 printf("ixgb%d: watchdog timeout -- resetting\n", adapter->unit);
599
600 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
601
602
603 ixgb_stop(adapter);
604 ixgb_init(adapter);
605
606
607 ifp->if_oerrors++;
608
609 return;
610}
611
612/*********************************************************************
613 * Init entry point
614 *
615 * This routine is used in two ways. It is used by the stack as
616 * init entry point in network interface structure. It is also used
617 * by the driver as a hw/sw initialization routine to get to a
618 * consistent state.
619 *
620 * return 0 on success, positive on failure
621 **********************************************************************/
622
623static void
624ixgb_init_locked(struct adapter *adapter)
625{
626 struct ifnet *ifp;
627
628 INIT_DEBUGOUT("ixgb_init: begin");
629
630 IXGB_LOCK_ASSERT(adapter);
631
632 ixgb_stop(adapter);
633
634 /* Get the latest mac address, User can use a LAA */
635 bcopy(IFP2ENADDR(adapter->ifp), adapter->hw.curr_mac_addr,
636 IXGB_ETH_LENGTH_OF_ADDRESS);
637
638 /* Initialize the hardware */
639 if (ixgb_hardware_init(adapter)) {
640 printf("ixgb%d: Unable to initialize the hardware\n",
641 adapter->unit);
642 return;
643 }
644 ixgb_enable_vlans(adapter);
645
646 /* Prepare transmit descriptors and buffers */
647 if (ixgb_setup_transmit_structures(adapter)) {
648 printf("ixgb%d: Could not setup transmit structures\n",
649 adapter->unit);
650 ixgb_stop(adapter);
651 return;
652 }
653 ixgb_initialize_transmit_unit(adapter);
654
655 /* Setup Multicast table */
656 ixgb_set_multi(adapter);
657
658 /* Prepare receive descriptors and buffers */
659 if (ixgb_setup_receive_structures(adapter)) {
660 printf("ixgb%d: Could not setup receive structures\n",
661 adapter->unit);
662 ixgb_stop(adapter);
663 return;
664 }
665 ixgb_initialize_receive_unit(adapter);
666
667 /* Don't loose promiscuous settings */
668 ixgb_set_promisc(adapter);
669
670 ifp = adapter->ifp;
671 ifp->if_drv_flags |= IFF_DRV_RUNNING;
672 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
673
674
675 if (ifp->if_capenable & IFCAP_TXCSUM)
676 ifp->if_hwassist = IXGB_CHECKSUM_FEATURES;
677 else
678 ifp->if_hwassist = 0;
679
680
681 /* Enable jumbo frames */
682 if (ifp->if_mtu > ETHERMTU) {
683 uint32_t temp_reg;
684 IXGB_WRITE_REG(&adapter->hw, MFS,
685 adapter->hw.max_frame_size << IXGB_MFS_SHIFT);
686 temp_reg = IXGB_READ_REG(&adapter->hw, CTRL0);
687 temp_reg |= IXGB_CTRL0_JFE;
688 IXGB_WRITE_REG(&adapter->hw, CTRL0, temp_reg);
689 }
690 callout_reset(&adapter->timer, 2 * hz, ixgb_local_timer, adapter);
691 ixgb_clear_hw_cntrs(&adapter->hw);
692#ifdef DEVICE_POLLING
693 /*
694 * Only disable interrupts if we are polling, make sure they are on
695 * otherwise.
696 */
697 if (ifp->if_flags & IFF_POLLING)
698 ixgb_disable_intr(adapter);
699 else
700#endif /* DEVICE_POLLING */
701 ixgb_enable_intr(adapter);
702
703 return;
704}
705
706static void
707ixgb_init(void *arg)
708{
709 struct adapter *adapter = arg;
710
711 IXGB_LOCK(adapter);
712 ixgb_init_locked(adapter);
713 IXGB_UNLOCK(adapter);
714 return;
715}
716
717#ifdef DEVICE_POLLING
718static poll_handler_t ixgb_poll;
719
720static void
721ixgb_poll_locked(struct ifnet * ifp, enum poll_cmd cmd, int count)
722{
723 struct adapter *adapter = ifp->if_softc;
724 u_int32_t reg_icr;
725
726 IXGB_LOCK_ASSERT(adapter);
727
728 if (!(ifp->if_capenable & IFCAP_POLLING)) {
729 ether_poll_deregister(ifp);
730 cmd = POLL_DEREGISTER;
731 }
732
733 if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */
734 ixgb_enable_intr(adapter);
735 return;
736 }
737 if (cmd == POLL_AND_CHECK_STATUS) {
738 reg_icr = IXGB_READ_REG(&adapter->hw, ICR);
739 if (reg_icr & (IXGB_INT_RXSEQ | IXGB_INT_LSC)) {
740 callout_stop(&adapter->timer);
741 ixgb_check_for_link(&adapter->hw);
742 ixgb_print_link_status(adapter);
743 callout_reset(&adapter->timer, 2 * hz, ixgb_local_timer,
744 adapter);
745 }
746 }
747 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
748 ixgb_process_receive_interrupts(adapter, count);
749 ixgb_clean_transmit_interrupts(adapter);
750 }
751 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
752 ifp->if_snd.ifq_head != NULL)
753 ixgb_start_locked(ifp);
754}
755
756static void
757ixgb_poll(struct ifnet * ifp, enum poll_cmd cmd, int count)
758{
759 struct adapter *adapter = ifp->if_softc;
760
761 IXGB_LOCK(adapter);
762 ixgb_poll_locked(ifp, cmd, count);
763 IXGB_UNLOCK(adapter);
764}
765#endif /* DEVICE_POLLING */
766
767/*********************************************************************
768 *
769 * Interrupt Service routine
770 *
771 **********************************************************************/
772
773static void
774ixgb_intr(void *arg)
775{
776 u_int32_t loop_cnt = IXGB_MAX_INTR;
777 u_int32_t reg_icr;
778 struct ifnet *ifp;
779 struct adapter *adapter = arg;
780 boolean_t rxdmt0 = FALSE;
781
782 IXGB_LOCK(adapter);
783
784 ifp = adapter->ifp;
785
786#ifdef DEVICE_POLLING
787 if (ifp->if_flags & IFF_POLLING) {
788 IXGB_UNLOCK(adapter);
789 return;
790 }
791
792 if ((ifp->if_capenable & IFCAP_POLLING) &&
793 ether_poll_register(ixgb_poll, ifp)) {
794 ixgb_disable_intr(adapter);
795 ixgb_poll_locked(ifp, 0, 1);
796 IXGB_UNLOCK(adapter);
797 return;
798 }
799#endif /* DEVICE_POLLING */
800
801 reg_icr = IXGB_READ_REG(&adapter->hw, ICR);
802 if (reg_icr == 0) {
803 IXGB_UNLOCK(adapter);
804 return;
805 }
806
807 if (reg_icr & IXGB_INT_RXDMT0)
808 rxdmt0 = TRUE;
809
810#ifdef _SV_
811 if (reg_icr & IXGB_INT_RXDMT0)
812 adapter->sv_stats.icr_rxdmt0++;
813 if (reg_icr & IXGB_INT_RXO)
814 adapter->sv_stats.icr_rxo++;
815 if (reg_icr & IXGB_INT_RXT0)
816 adapter->sv_stats.icr_rxt0++;
817 if (reg_icr & IXGB_INT_TXDW)
818 adapter->sv_stats.icr_TXDW++;
819#endif /* _SV_ */
820
821 /* Link status change */
822 if (reg_icr & (IXGB_INT_RXSEQ | IXGB_INT_LSC)) {
823 callout_stop(&adapter->timer);
824 ixgb_check_for_link(&adapter->hw);
825 ixgb_print_link_status(adapter);
826 callout_reset(&adapter->timer, 2 * hz, ixgb_local_timer,
827 adapter);
828 }
829 while (loop_cnt > 0) {
830 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
831 ixgb_process_receive_interrupts(adapter, -1);
832 ixgb_clean_transmit_interrupts(adapter);
833 }
834 loop_cnt--;
835 }
836
837 if (rxdmt0 && adapter->raidc) {
838 IXGB_WRITE_REG(&adapter->hw, IMC, IXGB_INT_RXDMT0);
839 IXGB_WRITE_REG(&adapter->hw, IMS, IXGB_INT_RXDMT0);
840 }
841 if (ifp->if_drv_flags & IFF_DRV_RUNNING && ifp->if_snd.ifq_head != NULL)
842 ixgb_start_locked(ifp);
843
844 IXGB_UNLOCK(adapter);
845 return;
846}
847
848
849/*********************************************************************
850 *
851 * Media Ioctl callback
852 *
853 * This routine is called whenever the user queries the status of
854 * the interface using ifconfig.
855 *
856 **********************************************************************/
857static void
858ixgb_media_status(struct ifnet * ifp, struct ifmediareq * ifmr)
859{
860 struct adapter *adapter = ifp->if_softc;
861
862 INIT_DEBUGOUT("ixgb_media_status: begin");
863
864 ixgb_check_for_link(&adapter->hw);
865 ixgb_print_link_status(adapter);
866
867 ifmr->ifm_status = IFM_AVALID;
868 ifmr->ifm_active = IFM_ETHER;
869
870 if (!adapter->hw.link_up)
871 return;
872
873 ifmr->ifm_status |= IFM_ACTIVE;
874 ifmr->ifm_active |= IFM_1000_SX | IFM_FDX;
875
876 return;
877}
878
879/*********************************************************************
880 *
881 * Media Ioctl callback
882 *
883 * This routine is called when the user changes speed/duplex using
884 * media/mediopt option with ifconfig.
885 *
886 **********************************************************************/
887static int
888ixgb_media_change(struct ifnet * ifp)
889{
890 struct adapter *adapter = ifp->if_softc;
891 struct ifmedia *ifm = &adapter->media;
892
893 INIT_DEBUGOUT("ixgb_media_change: begin");
894
895 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
896 return (EINVAL);
897
898 return (0);
899}
900
901/*********************************************************************
902 *
903 * This routine maps the mbufs to tx descriptors.
904 *
905 * return 0 on success, positive on failure
906 **********************************************************************/
907
908static int
909ixgb_encap(struct adapter * adapter, struct mbuf * m_head)
910{
911 u_int8_t txd_popts;
912 int i, j, error, nsegs;
913
914#if __FreeBSD_version < 500000
915 struct ifvlan *ifv = NULL;
916#else
917 struct m_tag *mtag;
918#endif
919 bus_dma_segment_t segs[IXGB_MAX_SCATTER];
920 bus_dmamap_t map;
921 struct ixgb_buffer *tx_buffer = NULL;
922 struct ixgb_tx_desc *current_tx_desc = NULL;
923 struct ifnet *ifp = adapter->ifp;
924
925 /*
926 * Force a cleanup if number of TX descriptors available hits the
927 * threshold
928 */
929 if (adapter->num_tx_desc_avail <= IXGB_TX_CLEANUP_THRESHOLD) {
930 ixgb_clean_transmit_interrupts(adapter);
931 }
932 if (adapter->num_tx_desc_avail <= IXGB_TX_CLEANUP_THRESHOLD) {
933 adapter->no_tx_desc_avail1++;
934 return (ENOBUFS);
935 }
936 /*
937 * Map the packet for DMA.
938 */
939 if (bus_dmamap_create(adapter->txtag, BUS_DMA_NOWAIT, &map)) {
940 adapter->no_tx_map_avail++;
941 return (ENOMEM);
942 }
943 error = bus_dmamap_load_mbuf_sg(adapter->txtag, map, m_head, segs,
944 &nsegs, BUS_DMA_NOWAIT);
945 if (error != 0) {
946 adapter->no_tx_dma_setup++;
947 printf("ixgb%d: ixgb_encap: bus_dmamap_load_mbuf failed; "
948 "error %u\n", adapter->unit, error);
949 bus_dmamap_destroy(adapter->txtag, map);
950 return (error);
951 }
952 KASSERT(nsegs != 0, ("ixgb_encap: empty packet"));
953
954 if (nsegs > adapter->num_tx_desc_avail) {
955 adapter->no_tx_desc_avail2++;
956 bus_dmamap_destroy(adapter->txtag, map);
957 return (ENOBUFS);
958 }
959 if (ifp->if_hwassist > 0) {
960 ixgb_transmit_checksum_setup(adapter, m_head,
961 &txd_popts);
962 } else
963 txd_popts = 0;
964
965 /* Find out if we are in vlan mode */
966#if __FreeBSD_version < 500000
967 if ((m_head->m_flags & (M_PROTO1 | M_PKTHDR)) == (M_PROTO1 | M_PKTHDR) &&
968 m_head->m_pkthdr.rcvif != NULL &&
969 m_head->m_pkthdr.rcvif->if_type == IFT_L2VLAN)
970 ifv = m_head->m_pkthdr.rcvif->if_softc;
971#else
972 mtag = VLAN_OUTPUT_TAG(ifp, m_head);
973#endif
974 i = adapter->next_avail_tx_desc;
975 for (j = 0; j < nsegs; j++) {
976 tx_buffer = &adapter->tx_buffer_area[i];
977 current_tx_desc = &adapter->tx_desc_base[i];
978
979 current_tx_desc->buff_addr = htole64(segs[j].ds_addr);
980 current_tx_desc->cmd_type_len = (adapter->txd_cmd | segs[j].ds_len);
981 current_tx_desc->popts = txd_popts;
982 if (++i == adapter->num_tx_desc)
983 i = 0;
984
985 tx_buffer->m_head = NULL;
986 }
987
988 adapter->num_tx_desc_avail -= nsegs;
989 adapter->next_avail_tx_desc = i;
990
991#if __FreeBSD_version < 500000
992 if (ifv != NULL) {
993 /* Set the vlan id */
994 current_tx_desc->vlan = ifv->ifv_tag;
995#else
996 if (mtag != NULL) {
997 /* Set the vlan id */
998 current_tx_desc->vlan = VLAN_TAG_VALUE(mtag);
999#endif
1000
1001 /* Tell hardware to add tag */
1002 current_tx_desc->cmd_type_len |= IXGB_TX_DESC_CMD_VLE;
1003 }
1004 tx_buffer->m_head = m_head;
1005 tx_buffer->map = map;
1006 bus_dmamap_sync(adapter->txtag, map, BUS_DMASYNC_PREWRITE);
1007
1008 /*
1009 * Last Descriptor of Packet needs End Of Packet (EOP)
1010 */
1011 current_tx_desc->cmd_type_len |= (IXGB_TX_DESC_CMD_EOP);
1012
1013 /*
1014 * Advance the Transmit Descriptor Tail (Tdt), this tells the E1000
1015 * that this frame is available to transmit.
1016 */
1017 IXGB_WRITE_REG(&adapter->hw, TDT, i);
1018
1019 return (0);
1020}
1021
1022static void
1023ixgb_set_promisc(struct adapter * adapter)
1024{
1025
1026 u_int32_t reg_rctl;
1027 struct ifnet *ifp = adapter->ifp;
1028
1029 reg_rctl = IXGB_READ_REG(&adapter->hw, RCTL);
1030
1031 if (ifp->if_flags & IFF_PROMISC) {
1032 reg_rctl |= (IXGB_RCTL_UPE | IXGB_RCTL_MPE);
1033 IXGB_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1034 } else if (ifp->if_flags & IFF_ALLMULTI) {
1035 reg_rctl |= IXGB_RCTL_MPE;
1036 reg_rctl &= ~IXGB_RCTL_UPE;
1037 IXGB_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1038 }
1039 return;
1040}
1041
1042static void
1043ixgb_disable_promisc(struct adapter * adapter)
1044{
1045 u_int32_t reg_rctl;
1046
1047 reg_rctl = IXGB_READ_REG(&adapter->hw, RCTL);
1048
1049 reg_rctl &= (~IXGB_RCTL_UPE);
1050 reg_rctl &= (~IXGB_RCTL_MPE);
1051 IXGB_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1052
1053 return;
1054}
1055
1056
1057/*********************************************************************
1058 * Multicast Update
1059 *
1060 * This routine is called whenever multicast address list is updated.
1061 *
1062 **********************************************************************/
1063
1064static void
1065ixgb_set_multi(struct adapter * adapter)
1066{
1067 u_int32_t reg_rctl = 0;
1068 u_int8_t mta[MAX_NUM_MULTICAST_ADDRESSES * IXGB_ETH_LENGTH_OF_ADDRESS];
1069 struct ifmultiaddr *ifma;
1070 int mcnt = 0;
1071 struct ifnet *ifp = adapter->ifp;
1072
1073 IOCTL_DEBUGOUT("ixgb_set_multi: begin");
1074
1075 IF_ADDR_LOCK(ifp);
1076#if __FreeBSD_version < 500000
1077 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1078#else
1079 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1080#endif
1081 if (ifma->ifma_addr->sa_family != AF_LINK)
1082 continue;
1083
1084 bcopy(LLADDR((struct sockaddr_dl *) ifma->ifma_addr),
1085 &mta[mcnt * IXGB_ETH_LENGTH_OF_ADDRESS], IXGB_ETH_LENGTH_OF_ADDRESS);
1086 mcnt++;
1087 }
1088 IF_ADDR_UNLOCK(ifp);
1089
1090 if (mcnt > MAX_NUM_MULTICAST_ADDRESSES) {
1091 reg_rctl = IXGB_READ_REG(&adapter->hw, RCTL);
1092 reg_rctl |= IXGB_RCTL_MPE;
1093 IXGB_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1094 } else
1095 ixgb_mc_addr_list_update(&adapter->hw, mta, mcnt, 0);
1096
1097 return;
1098}
1099
1100
1101/*********************************************************************
1102 * Timer routine
1103 *
1104 * This routine checks for link status and updates statistics.
1105 *
1106 **********************************************************************/
1107
1108static void
1109ixgb_local_timer(void *arg)
1110{
1111 struct ifnet *ifp;
1112 struct adapter *adapter = arg;
1113 ifp = adapter->ifp;
1114
1115 IXGB_LOCK(adapter);
1116
1117 ixgb_check_for_link(&adapter->hw);
1118 ixgb_print_link_status(adapter);
1119 ixgb_update_stats_counters(adapter);
1120 if (ixgb_display_debug_stats && ifp->if_drv_flags & IFF_DRV_RUNNING) {
1121 ixgb_print_hw_stats(adapter);
1122 }
1123 callout_reset(&adapter->timer, 2 * hz, ixgb_local_timer, adapter);
1124
1125 IXGB_UNLOCK(adapter);
1126 return;
1127}
1128
1129static void
1130ixgb_print_link_status(struct adapter * adapter)
1131{
1132 if (adapter->hw.link_up) {
1133 if (!adapter->link_active) {
1134 printf("ixgb%d: Link is up %d Mbps %s \n",
1135 adapter->unit,
1136 10000,
1137 "Full Duplex");
1138 adapter->link_active = 1;
1139 }
1140 } else {
1141 if (adapter->link_active) {
1142 printf("ixgb%d: Link is Down \n", adapter->unit);
1143 adapter->link_active = 0;
1144 }
1145 }
1146
1147 return;
1148}
1149
1150
1151
1152/*********************************************************************
1153 *
1154 * This routine disables all traffic on the adapter by issuing a
1155 * global reset on the MAC and deallocates TX/RX buffers.
1156 *
1157 **********************************************************************/
1158
1159static void
1160ixgb_stop(void *arg)
1161{
1162 struct ifnet *ifp;
1163 struct adapter *adapter = arg;
1164 ifp = adapter->ifp;
1165
1166 IXGB_LOCK_ASSERT(adapter);
1167
1168 INIT_DEBUGOUT("ixgb_stop: begin\n");
1169 ixgb_disable_intr(adapter);
1170 adapter->hw.adapter_stopped = FALSE;
1171 ixgb_adapter_stop(&adapter->hw);
1172 callout_stop(&adapter->timer);
1173 ixgb_free_transmit_structures(adapter);
1174 ixgb_free_receive_structures(adapter);
1175
1176
1177 /* Tell the stack that the interface is no longer active */
1178 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1179
1180 return;
1181}
1182
1183
1184/*********************************************************************
1185 *
1186 * Determine hardware revision.
1187 *
1188 **********************************************************************/
1189static void
1190ixgb_identify_hardware(struct adapter * adapter)
1191{
1192 device_t dev = adapter->dev;
1193
1194 /* Make sure our PCI config space has the necessary stuff set */
1195 adapter->hw.pci_cmd_word = pci_read_config(dev, PCIR_COMMAND, 2);
1196 if (!((adapter->hw.pci_cmd_word & PCIM_CMD_BUSMASTEREN) &&
1197 (adapter->hw.pci_cmd_word & PCIM_CMD_MEMEN))) {
1198 printf("ixgb%d: Memory Access and/or Bus Master bits were not set!\n",
1199 adapter->unit);
1200 adapter->hw.pci_cmd_word |=
1201 (PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN);
1202 pci_write_config(dev, PCIR_COMMAND, adapter->hw.pci_cmd_word, 2);
1203 }
1204 /* Save off the information about this board */
1205 adapter->hw.vendor_id = pci_get_vendor(dev);
1206 adapter->hw.device_id = pci_get_device(dev);
1207 adapter->hw.revision_id = pci_read_config(dev, PCIR_REVID, 1);
1208 adapter->hw.subsystem_vendor_id = pci_read_config(dev, PCIR_SUBVEND_0, 2);
1209 adapter->hw.subsystem_id = pci_read_config(dev, PCIR_SUBDEV_0, 2);
1210
1211 /* Set MacType, etc. based on this PCI info */
1212 switch (adapter->hw.device_id) {
1213 case IXGB_DEVICE_ID_82597EX:
1214 case IXGB_DEVICE_ID_82597EX_SR:
1215 adapter->hw.mac_type = ixgb_82597;
1216 break;
1217 default:
1218 INIT_DEBUGOUT1("Unknown device if 0x%x", adapter->hw.device_id);
1219 printf("ixgb%d: unsupported device id 0x%x\n", adapter->unit, adapter->hw.device_id);
1220 }
1221
1222 return;
1223}
1224
1225static int
1226ixgb_allocate_pci_resources(struct adapter * adapter)
1227{
1228 int rid;
1229 device_t dev = adapter->dev;
1230
1231 rid = IXGB_MMBA;
1232 adapter->res_memory = bus_alloc_resource(dev, SYS_RES_MEMORY,
1233 &rid, 0, ~0, 1,
1234 RF_ACTIVE);
1235 if (!(adapter->res_memory)) {
1236 printf("ixgb%d: Unable to allocate bus resource: memory\n",
1237 adapter->unit);
1238 return (ENXIO);
1239 }
1240 adapter->osdep.mem_bus_space_tag =
1241 rman_get_bustag(adapter->res_memory);
1242 adapter->osdep.mem_bus_space_handle =
1243 rman_get_bushandle(adapter->res_memory);
1244 adapter->hw.hw_addr = (uint8_t *) & adapter->osdep.mem_bus_space_handle;
1245
1246 rid = 0x0;
1247 adapter->res_interrupt = bus_alloc_resource(dev, SYS_RES_IRQ,
1248 &rid, 0, ~0, 1,
1249 RF_SHAREABLE | RF_ACTIVE);
1250 if (!(adapter->res_interrupt)) {
1251 printf("ixgb%d: Unable to allocate bus resource: interrupt\n",
1252 adapter->unit);
1253 return (ENXIO);
1254 }
1255 if (bus_setup_intr(dev, adapter->res_interrupt,
1256 INTR_TYPE_NET | INTR_MPSAFE,
1257 (void (*) (void *))ixgb_intr, adapter,
1258 &adapter->int_handler_tag)) {
1259 printf("ixgb%d: Error registering interrupt handler!\n",
1260 adapter->unit);
1261 return (ENXIO);
1262 }
1263 adapter->hw.back = &adapter->osdep;
1264
1265 return (0);
1266}
1267
1268static void
1269ixgb_free_pci_resources(struct adapter * adapter)
1270{
1271 device_t dev = adapter->dev;
1272
1273 if (adapter->res_interrupt != NULL) {
1274 bus_teardown_intr(dev, adapter->res_interrupt,
1275 adapter->int_handler_tag);
1276 bus_release_resource(dev, SYS_RES_IRQ, 0,
1277 adapter->res_interrupt);
1278 }
1279 if (adapter->res_memory != NULL) {
1280 bus_release_resource(dev, SYS_RES_MEMORY, IXGB_MMBA,
1281 adapter->res_memory);
1282 }
1283 if (adapter->res_ioport != NULL) {
1284 bus_release_resource(dev, SYS_RES_IOPORT, adapter->io_rid,
1285 adapter->res_ioport);
1286 }
1287 return;
1288}
1289
1290/*********************************************************************
1291 *
1292 * Initialize the hardware to a configuration as specified by the
1293 * adapter structure. The controller is reset, the EEPROM is
1294 * verified, the MAC address is set, then the shared initialization
1295 * routines are called.
1296 *
1297 **********************************************************************/
1298static int
1299ixgb_hardware_init(struct adapter * adapter)
1300{
1301 /* Issue a global reset */
1302 adapter->hw.adapter_stopped = FALSE;
1303 ixgb_adapter_stop(&adapter->hw);
1304
1305 /* Make sure we have a good EEPROM before we read from it */
1306 if (!ixgb_validate_eeprom_checksum(&adapter->hw)) {
1307 printf("ixgb%d: The EEPROM Checksum Is Not Valid\n",
1308 adapter->unit);
1309 return (EIO);
1310 }
1311 if (!ixgb_init_hw(&adapter->hw)) {
1312 printf("ixgb%d: Hardware Initialization Failed",
1313 adapter->unit);
1314 return (EIO);
1315 }
1316
1317 return (0);
1318}
1319
1320/*********************************************************************
1321 *
1322 * Setup networking device structure and register an interface.
1323 *
1324 **********************************************************************/
1325static void
1326ixgb_setup_interface(device_t dev, struct adapter * adapter)
1327{
1328 struct ifnet *ifp;
1329 INIT_DEBUGOUT("ixgb_setup_interface: begin");
1330
1331 ifp = adapter->ifp = if_alloc(IFT_ETHER);
1332 if (ifp == NULL)
1333 panic("%s: can not if_alloc()\n", device_get_nameunit(dev));
1334#if __FreeBSD_version >= 502000
1335 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1336#else
1337 ifp->if_unit = adapter->unit;
1338 ifp->if_name = "ixgb";
1339#endif
1340 ifp->if_mtu = ETHERMTU;
1341 ifp->if_baudrate = 1000000000;
1342 ifp->if_init = ixgb_init;
1343 ifp->if_softc = adapter;
1344 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1345 ifp->if_ioctl = ixgb_ioctl;
1346 ifp->if_start = ixgb_start;
1347 ifp->if_watchdog = ixgb_watchdog;
1348 ifp->if_snd.ifq_maxlen = adapter->num_tx_desc - 1;
1349
1350#if __FreeBSD_version < 500000
1351 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
1352#else
1353 ether_ifattach(ifp, adapter->hw.curr_mac_addr);
1354#endif
1355
1356 ifp->if_capabilities = IFCAP_HWCSUM;
1357#ifdef DEVICE_POLLING
1358 ifp->if_capabilities |= IFCAP_POLLING;
1359#endif
1360
1361 /*
1362 * Tell the upper layer(s) we support long frames.
1363 */
1364 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1365
1366#if __FreeBSD_version >= 500000
1367 ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU;
1368#endif
1369
1370 ifp->if_capenable = ifp->if_capabilities;
1371
1372 /*
1373 * Specify the media types supported by this adapter and register
1374 * callbacks to update media and link information
1375 */
1376 ifmedia_init(&adapter->media, IFM_IMASK, ixgb_media_change,
1377 ixgb_media_status);
1378 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_SX | IFM_FDX,
1379 0, NULL);
1380 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_SX,
1381 0, NULL);
1382 ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
1383 ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
1384
1385 return;
1386}
1387
1388/********************************************************************
1389 * Manage DMA'able memory.
1390 *******************************************************************/
1391static void
1392ixgb_dmamap_cb(void *arg, bus_dma_segment_t * segs, int nseg, int error)
1393{
1394 if (error)
1395 return;
1396 *(bus_addr_t *) arg = segs->ds_addr;
1397 return;
1398}
1399
1400static int
1401ixgb_dma_malloc(struct adapter * adapter, bus_size_t size,
1402 struct ixgb_dma_alloc * dma, int mapflags)
1403{
1404 int r;
1405
1406 r = bus_dma_tag_create(NULL, /* parent */
1407 PAGE_SIZE, 0, /* alignment, bounds */
1408 BUS_SPACE_MAXADDR, /* lowaddr */
1409 BUS_SPACE_MAXADDR, /* highaddr */
1410 NULL, NULL, /* filter, filterarg */
1411 size, /* maxsize */
1412 1, /* nsegments */
1413 size, /* maxsegsize */
1414 BUS_DMA_ALLOCNOW, /* flags */
1415#if __FreeBSD_version >= 502000
1416 NULL, /* lockfunc */
1417 NULL, /* lockfuncarg */
1418#endif
1419 &dma->dma_tag);
1420 if (r != 0) {
1421 printf("ixgb%d: ixgb_dma_malloc: bus_dma_tag_create failed; "
1422 "error %u\n", adapter->unit, r);
1423 goto fail_0;
1424 }
1425 r = bus_dmamem_alloc(dma->dma_tag, (void **)&dma->dma_vaddr,
1426 BUS_DMA_NOWAIT, &dma->dma_map);
1427 if (r != 0) {
1428 printf("ixgb%d: ixgb_dma_malloc: bus_dmamem_alloc failed; "
1429 "error %u\n", adapter->unit, r);
1430 goto fail_1;
1431 }
1432 r = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
1433 size,
1434 ixgb_dmamap_cb,
1435 &dma->dma_paddr,
1436 mapflags | BUS_DMA_NOWAIT);
1437 if (r != 0) {
1438 printf("ixgb%d: ixgb_dma_malloc: bus_dmamap_load failed; "
1439 "error %u\n", adapter->unit, r);
1440 goto fail_2;
1441 }
1442 dma->dma_size = size;
1443 return (0);
1444fail_2:
1445 bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
1446fail_1:
1447 bus_dma_tag_destroy(dma->dma_tag);
1448fail_0:
1449 dma->dma_map = NULL;
1450 dma->dma_tag = NULL;
1451 return (r);
1452}
1453
1454
1455
1456static void
1457ixgb_dma_free(struct adapter * adapter, struct ixgb_dma_alloc * dma)
1458{
1459 bus_dmamap_unload(dma->dma_tag, dma->dma_map);
1460 bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
1461 bus_dma_tag_destroy(dma->dma_tag);
1462}
1463
1464/*********************************************************************
1465 *
1466 * Allocate memory for tx_buffer structures. The tx_buffer stores all
1467 * the information needed to transmit a packet on the wire.
1468 *
1469 **********************************************************************/
1470static int
1471ixgb_allocate_transmit_structures(struct adapter * adapter)
1472{
1473 if (!(adapter->tx_buffer_area =
1474 (struct ixgb_buffer *) malloc(sizeof(struct ixgb_buffer) *
1475 adapter->num_tx_desc, M_DEVBUF,
1476 M_NOWAIT | M_ZERO))) {
1477 printf("ixgb%d: Unable to allocate tx_buffer memory\n",
1478 adapter->unit);
1479 return ENOMEM;
1480 }
1481 bzero(adapter->tx_buffer_area,
1482 sizeof(struct ixgb_buffer) * adapter->num_tx_desc);
1483
1484 return 0;
1485}
1486
1487/*********************************************************************
1488 *
1489 * Allocate and initialize transmit structures.
1490 *
1491 **********************************************************************/
1492static int
1493ixgb_setup_transmit_structures(struct adapter * adapter)
1494{
1495 /*
1496 * Setup DMA descriptor areas.
1497 */
1498 if (bus_dma_tag_create(NULL, /* parent */
1499 PAGE_SIZE, 0, /* alignment, bounds */
1500 BUS_SPACE_MAXADDR, /* lowaddr */
1501 BUS_SPACE_MAXADDR, /* highaddr */
1502 NULL, NULL, /* filter, filterarg */
1503 MCLBYTES * IXGB_MAX_SCATTER, /* maxsize */
1504 IXGB_MAX_SCATTER, /* nsegments */
1505 MCLBYTES, /* maxsegsize */
1506 BUS_DMA_ALLOCNOW, /* flags */
1507#if __FreeBSD_version >= 502000
1508 NULL, /* lockfunc */
1509 NULL, /* lockfuncarg */
1510#endif
1511 &adapter->txtag)) {
1512 printf("ixgb%d: Unable to allocate TX DMA tag\n", adapter->unit);
1513 return (ENOMEM);
1514 }
1515 if (ixgb_allocate_transmit_structures(adapter))
1516 return ENOMEM;
1517
1518 bzero((void *)adapter->tx_desc_base,
1519 (sizeof(struct ixgb_tx_desc)) * adapter->num_tx_desc);
1520
1521 adapter->next_avail_tx_desc = 0;
1522 adapter->oldest_used_tx_desc = 0;
1523
1524 /* Set number of descriptors available */
1525 adapter->num_tx_desc_avail = adapter->num_tx_desc;
1526
1527 /* Set checksum context */
1528 adapter->active_checksum_context = OFFLOAD_NONE;
1529
1530 return 0;
1531}
1532
1533/*********************************************************************
1534 *
1535 * Enable transmit unit.
1536 *
1537 **********************************************************************/
1538static void
1539ixgb_initialize_transmit_unit(struct adapter * adapter)
1540{
1541 u_int32_t reg_tctl;
1542 u_int64_t tdba = adapter->txdma.dma_paddr;
1543
1544 /* Setup the Base and Length of the Tx Descriptor Ring */
1545 IXGB_WRITE_REG(&adapter->hw, TDBAL,
1546 (tdba & 0x00000000ffffffffULL));
1547 IXGB_WRITE_REG(&adapter->hw, TDBAH, (tdba >> 32));
1548 IXGB_WRITE_REG(&adapter->hw, TDLEN,
1549 adapter->num_tx_desc *
1550 sizeof(struct ixgb_tx_desc));
1551
1552 /* Setup the HW Tx Head and Tail descriptor pointers */
1553 IXGB_WRITE_REG(&adapter->hw, TDH, 0);
1554 IXGB_WRITE_REG(&adapter->hw, TDT, 0);
1555
1556
1557 HW_DEBUGOUT2("Base = %x, Length = %x\n",
1558 IXGB_READ_REG(&adapter->hw, TDBAL),
1559 IXGB_READ_REG(&adapter->hw, TDLEN));
1560
1561 IXGB_WRITE_REG(&adapter->hw, TIDV, adapter->tx_int_delay);
1562
1563
1564 /* Program the Transmit Control Register */
1565 reg_tctl = IXGB_READ_REG(&adapter->hw, TCTL);
1566 reg_tctl = IXGB_TCTL_TCE | IXGB_TCTL_TXEN | IXGB_TCTL_TPDE;
1567 IXGB_WRITE_REG(&adapter->hw, TCTL, reg_tctl);
1568
1569 /* Setup Transmit Descriptor Settings for this adapter */
1570 adapter->txd_cmd = IXGB_TX_DESC_TYPE | IXGB_TX_DESC_CMD_RS;
1571
1572 if (adapter->tx_int_delay > 0)
1573 adapter->txd_cmd |= IXGB_TX_DESC_CMD_IDE;
1574 return;
1575}
1576
1577/*********************************************************************
1578 *
1579 * Free all transmit related data structures.
1580 *
1581 **********************************************************************/
1582static void
1583ixgb_free_transmit_structures(struct adapter * adapter)
1584{
1585 struct ixgb_buffer *tx_buffer;
1586 int i;
1587
1588 INIT_DEBUGOUT("free_transmit_structures: begin");
1589
1590 if (adapter->tx_buffer_area != NULL) {
1591 tx_buffer = adapter->tx_buffer_area;
1592 for (i = 0; i < adapter->num_tx_desc; i++, tx_buffer++) {
1593 if (tx_buffer->m_head != NULL) {
1594 bus_dmamap_unload(adapter->txtag, tx_buffer->map);
1595 bus_dmamap_destroy(adapter->txtag, tx_buffer->map);
1596 m_freem(tx_buffer->m_head);
1597 }
1598 tx_buffer->m_head = NULL;
1599 }
1600 }
1601 if (adapter->tx_buffer_area != NULL) {
1602 free(adapter->tx_buffer_area, M_DEVBUF);
1603 adapter->tx_buffer_area = NULL;
1604 }
1605 if (adapter->txtag != NULL) {
1606 bus_dma_tag_destroy(adapter->txtag);
1607 adapter->txtag = NULL;
1608 }
1609 return;
1610}
1611
1612/*********************************************************************
1613 *
1614 * The offload context needs to be set when we transfer the first
1615 * packet of a particular protocol (TCP/UDP). We change the
1616 * context only if the protocol type changes.
1617 *
1618 **********************************************************************/
1619static void
1620ixgb_transmit_checksum_setup(struct adapter * adapter,
1621 struct mbuf * mp,
1622 u_int8_t * txd_popts)
1623{
1624 struct ixgb_context_desc *TXD;
1625 struct ixgb_buffer *tx_buffer;
1626 int curr_txd;
1627
1628 if (mp->m_pkthdr.csum_flags) {
1629
1630 if (mp->m_pkthdr.csum_flags & CSUM_TCP) {
1631 *txd_popts = IXGB_TX_DESC_POPTS_TXSM;
1632 if (adapter->active_checksum_context == OFFLOAD_TCP_IP)
1633 return;
1634 else
1635 adapter->active_checksum_context = OFFLOAD_TCP_IP;
1636 } else if (mp->m_pkthdr.csum_flags & CSUM_UDP) {
1637 *txd_popts = IXGB_TX_DESC_POPTS_TXSM;
1638 if (adapter->active_checksum_context == OFFLOAD_UDP_IP)
1639 return;
1640 else
1641 adapter->active_checksum_context = OFFLOAD_UDP_IP;
1642 } else {
1643 *txd_popts = 0;
1644 return;
1645 }
1646 } else {
1647 *txd_popts = 0;
1648 return;
1649 }
1650
1651 /*
1652 * If we reach this point, the checksum offload context needs to be
1653 * reset.
1654 */
1655 curr_txd = adapter->next_avail_tx_desc;
1656 tx_buffer = &adapter->tx_buffer_area[curr_txd];
1657 TXD = (struct ixgb_context_desc *) & adapter->tx_desc_base[curr_txd];
1658
1659
1660 TXD->tucss = ENET_HEADER_SIZE + sizeof(struct ip);
1661 TXD->tucse = 0;
1662
1663 TXD->mss = 0;
1664
1665 if (adapter->active_checksum_context == OFFLOAD_TCP_IP) {
1666 TXD->tucso =
1667 ENET_HEADER_SIZE + sizeof(struct ip) +
1668 offsetof(struct tcphdr, th_sum);
1669 } else if (adapter->active_checksum_context == OFFLOAD_UDP_IP) {
1670 TXD->tucso =
1671 ENET_HEADER_SIZE + sizeof(struct ip) +
1672 offsetof(struct udphdr, uh_sum);
1673 }
1674 TXD->cmd_type_len = IXGB_CONTEXT_DESC_CMD_TCP | IXGB_TX_DESC_CMD_RS | IXGB_CONTEXT_DESC_CMD_IDE;
1675
1676 tx_buffer->m_head = NULL;
1677
1678 if (++curr_txd == adapter->num_tx_desc)
1679 curr_txd = 0;
1680
1681 adapter->num_tx_desc_avail--;
1682 adapter->next_avail_tx_desc = curr_txd;
1683 return;
1684}
1685
1686/**********************************************************************
1687 *
1688 * Examine each tx_buffer in the used queue. If the hardware is done
1689 * processing the packet then free associated resources. The
1690 * tx_buffer is put back on the free queue.
1691 *
1692 **********************************************************************/
1693static void
1694ixgb_clean_transmit_interrupts(struct adapter * adapter)
1695{
1696 int i, num_avail;
1697 struct ixgb_buffer *tx_buffer;
1698 struct ixgb_tx_desc *tx_desc;
1699
1700 IXGB_LOCK_ASSERT(adapter);
1701
1702 if (adapter->num_tx_desc_avail == adapter->num_tx_desc)
1703 return;
1704
1705#ifdef _SV_
1706 adapter->clean_tx_interrupts++;
1707#endif
1708 num_avail = adapter->num_tx_desc_avail;
1709 i = adapter->oldest_used_tx_desc;
1710
1711 tx_buffer = &adapter->tx_buffer_area[i];
1712 tx_desc = &adapter->tx_desc_base[i];
1713
1714 while (tx_desc->status & IXGB_TX_DESC_STATUS_DD) {
1715
1716 tx_desc->status = 0;
1717 num_avail++;
1718
1719 if (tx_buffer->m_head) {
1720 bus_dmamap_sync(adapter->txtag, tx_buffer->map,
1721 BUS_DMASYNC_POSTWRITE);
1722 bus_dmamap_unload(adapter->txtag, tx_buffer->map);
1723 bus_dmamap_destroy(adapter->txtag, tx_buffer->map);
1724 m_freem(tx_buffer->m_head);
1725 tx_buffer->m_head = NULL;
1726 }
1727 if (++i == adapter->num_tx_desc)
1728 i = 0;
1729
1730 tx_buffer = &adapter->tx_buffer_area[i];
1731 tx_desc = &adapter->tx_desc_base[i];
1732 }
1733
1734 adapter->oldest_used_tx_desc = i;
1735
1736 /*
1737 * If we have enough room, clear IFF_DRV_OACTIVE to tell the stack that
1738 * it is OK to send packets. If there are no pending descriptors,
1739 * clear the timeout. Otherwise, if some descriptors have been freed,
1740 * restart the timeout.
1741 */
1742 if (num_avail > IXGB_TX_CLEANUP_THRESHOLD) {
1743 struct ifnet *ifp = adapter->ifp;
1744
1745 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1746 if (num_avail == adapter->num_tx_desc)
1747 ifp->if_timer = 0;
1748 else if (num_avail == adapter->num_tx_desc_avail)
1749 ifp->if_timer = IXGB_TX_TIMEOUT;
1750 }
1751 adapter->num_tx_desc_avail = num_avail;
1752 return;
1753}
1754
1755
1756/*********************************************************************
1757 *
1758 * Get a buffer from system mbuf buffer pool.
1759 *
1760 **********************************************************************/
1761static int
1762ixgb_get_buf(int i, struct adapter * adapter,
1763 struct mbuf * nmp)
1764{
1765 register struct mbuf *mp = nmp;
1766 struct ixgb_buffer *rx_buffer;
1767 struct ifnet *ifp;
1768 bus_addr_t paddr;
1769 int error;
1770
1771 ifp = adapter->ifp;
1772
1773 if (mp == NULL) {
1774
1775 mp = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1776
1777 if (mp == NULL) {
1778 adapter->mbuf_alloc_failed++;
1779 return (ENOBUFS);
1780 }
1781 mp->m_len = mp->m_pkthdr.len = MCLBYTES;
1782 } else {
1783 mp->m_len = mp->m_pkthdr.len = MCLBYTES;
1784 mp->m_data = mp->m_ext.ext_buf;
1785 mp->m_next = NULL;
1786 }
1787
1788 if (ifp->if_mtu <= ETHERMTU) {
1789 m_adj(mp, ETHER_ALIGN);
1790 }
1791 rx_buffer = &adapter->rx_buffer_area[i];
1792
1793 /*
1794 * Using memory from the mbuf cluster pool, invoke the bus_dma
1795 * machinery to arrange the memory mapping.
1796 */
1797 error = bus_dmamap_load(adapter->rxtag, rx_buffer->map,
1798 mtod(mp, void *), mp->m_len,
1799 ixgb_dmamap_cb, &paddr, 0);
1800 if (error) {
1801 m_free(mp);
1802 return (error);
1803 }
1804 rx_buffer->m_head = mp;
1805 adapter->rx_desc_base[i].buff_addr = htole64(paddr);
1806 bus_dmamap_sync(adapter->rxtag, rx_buffer->map, BUS_DMASYNC_PREREAD);
1807
1808 return (0);
1809}
1810
1811/*********************************************************************
1812 *
1813 * Allocate memory for rx_buffer structures. Since we use one
1814 * rx_buffer per received packet, the maximum number of rx_buffer's
1815 * that we'll need is equal to the number of receive descriptors
1816 * that we've allocated.
1817 *
1818 **********************************************************************/
1819static int
1820ixgb_allocate_receive_structures(struct adapter * adapter)
1821{
1822 int i, error;
1823 struct ixgb_buffer *rx_buffer;
1824
1825 if (!(adapter->rx_buffer_area =
1826 (struct ixgb_buffer *) malloc(sizeof(struct ixgb_buffer) *
1827 adapter->num_rx_desc, M_DEVBUF,
1828 M_NOWAIT | M_ZERO))) {
1829 printf("ixgb%d: Unable to allocate rx_buffer memory\n",
1830 adapter->unit);
1831 return (ENOMEM);
1832 }
1833 bzero(adapter->rx_buffer_area,
1834 sizeof(struct ixgb_buffer) * adapter->num_rx_desc);
1835
1836 error = bus_dma_tag_create(NULL, /* parent */
1837 PAGE_SIZE, 0, /* alignment, bounds */
1838 BUS_SPACE_MAXADDR, /* lowaddr */
1839 BUS_SPACE_MAXADDR, /* highaddr */
1840 NULL, NULL, /* filter, filterarg */
1841 MCLBYTES, /* maxsize */
1842 1, /* nsegments */
1843 MCLBYTES, /* maxsegsize */
1844 BUS_DMA_ALLOCNOW, /* flags */
1845#if __FreeBSD_version >= 502000
1846 NULL, /* lockfunc */
1847 NULL, /* lockfuncarg */
1848#endif
1849 &adapter->rxtag);
1850 if (error != 0) {
1851 printf("ixgb%d: ixgb_allocate_receive_structures: "
1852 "bus_dma_tag_create failed; error %u\n",
1853 adapter->unit, error);
1854 goto fail_0;
1855 }
1856 rx_buffer = adapter->rx_buffer_area;
1857 for (i = 0; i < adapter->num_rx_desc; i++, rx_buffer++) {
1858 error = bus_dmamap_create(adapter->rxtag, BUS_DMA_NOWAIT,
1859 &rx_buffer->map);
1860 if (error != 0) {
1861 printf("ixgb%d: ixgb_allocate_receive_structures: "
1862 "bus_dmamap_create failed; error %u\n",
1863 adapter->unit, error);
1864 goto fail_1;
1865 }
1866 }
1867
1868 for (i = 0; i < adapter->num_rx_desc; i++) {
1869 if (ixgb_get_buf(i, adapter, NULL) == ENOBUFS) {
1870 adapter->rx_buffer_area[i].m_head = NULL;
1871 adapter->rx_desc_base[i].buff_addr = 0;
1872 return (ENOBUFS);
1873 }
1874 }
1875
1876 return (0);
1877fail_1:
1878 bus_dma_tag_destroy(adapter->rxtag);
1879fail_0:
1880 adapter->rxtag = NULL;
1881 free(adapter->rx_buffer_area, M_DEVBUF);
1882 adapter->rx_buffer_area = NULL;
1883 return (error);
1884}
1885
1886/*********************************************************************
1887 *
1888 * Allocate and initialize receive structures.
1889 *
1890 **********************************************************************/
1891static int
1892ixgb_setup_receive_structures(struct adapter * adapter)
1893{
1894 bzero((void *)adapter->rx_desc_base,
1895 (sizeof(struct ixgb_rx_desc)) * adapter->num_rx_desc);
1896
1897 if (ixgb_allocate_receive_structures(adapter))
1898 return ENOMEM;
1899
1900 /* Setup our descriptor pointers */
1901 adapter->next_rx_desc_to_check = 0;
1902 adapter->next_rx_desc_to_use = 0;
1903 return (0);
1904}
1905
1906/*********************************************************************
1907 *
1908 * Enable receive unit.
1909 *
1910 **********************************************************************/
1911static void
1912ixgb_initialize_receive_unit(struct adapter * adapter)
1913{
1914 u_int32_t reg_rctl;
1915 u_int32_t reg_rxcsum;
1916 u_int32_t reg_rxdctl;
1917 struct ifnet *ifp;
1918 u_int64_t rdba = adapter->rxdma.dma_paddr;
1919
1920 ifp = adapter->ifp;
1921
1922 /*
1923 * Make sure receives are disabled while setting up the descriptor
1924 * ring
1925 */
1926 reg_rctl = IXGB_READ_REG(&adapter->hw, RCTL);
1927 IXGB_WRITE_REG(&adapter->hw, RCTL, reg_rctl & ~IXGB_RCTL_RXEN);
1928
1929 /* Set the Receive Delay Timer Register */
1930 IXGB_WRITE_REG(&adapter->hw, RDTR,
1931 adapter->rx_int_delay);
1932
1933
1934 /* Setup the Base and Length of the Rx Descriptor Ring */
1935 IXGB_WRITE_REG(&adapter->hw, RDBAL,
1936 (rdba & 0x00000000ffffffffULL));
1937 IXGB_WRITE_REG(&adapter->hw, RDBAH, (rdba >> 32));
1938 IXGB_WRITE_REG(&adapter->hw, RDLEN, adapter->num_rx_desc *
1939 sizeof(struct ixgb_rx_desc));
1940
1941 /* Setup the HW Rx Head and Tail Descriptor Pointers */
1942 IXGB_WRITE_REG(&adapter->hw, RDH, 0);
1943
1944 IXGB_WRITE_REG(&adapter->hw, RDT, adapter->num_rx_desc - 1);
1945
1946
1947
1948 reg_rxdctl = RXDCTL_WTHRESH_DEFAULT << IXGB_RXDCTL_WTHRESH_SHIFT
1949 | RXDCTL_HTHRESH_DEFAULT << IXGB_RXDCTL_HTHRESH_SHIFT
1950 | RXDCTL_PTHRESH_DEFAULT << IXGB_RXDCTL_PTHRESH_SHIFT;
1951 IXGB_WRITE_REG(&adapter->hw, RXDCTL, reg_rxdctl);
1952
1953
1954 adapter->raidc = 1;
1955 if (adapter->raidc) {
1956 uint32_t raidc;
1957 uint8_t poll_threshold;
1958#define IXGB_RAIDC_POLL_DEFAULT 120
1959
1960 poll_threshold = ((adapter->num_rx_desc - 1) >> 3);
1961 poll_threshold >>= 1;
1962 poll_threshold &= 0x3F;
1963 raidc = IXGB_RAIDC_EN | IXGB_RAIDC_RXT_GATE |
1964 (IXGB_RAIDC_POLL_DEFAULT << IXGB_RAIDC_POLL_SHIFT) |
1965 (adapter->rx_int_delay << IXGB_RAIDC_DELAY_SHIFT) |
1966 poll_threshold;
1967 IXGB_WRITE_REG(&adapter->hw, RAIDC, raidc);
1968 }
1969 /* Enable Receive Checksum Offload for TCP and UDP ? */
1970 if (ifp->if_capenable & IFCAP_RXCSUM) {
1971 reg_rxcsum = IXGB_READ_REG(&adapter->hw, RXCSUM);
1972 reg_rxcsum |= IXGB_RXCSUM_TUOFL;
1973 IXGB_WRITE_REG(&adapter->hw, RXCSUM, reg_rxcsum);
1974 }
1975 /* Setup the Receive Control Register */
1976 reg_rctl = IXGB_READ_REG(&adapter->hw, RCTL);
1977 reg_rctl &= ~(3 << IXGB_RCTL_MO_SHIFT);
1978 reg_rctl |= IXGB_RCTL_BAM | IXGB_RCTL_RDMTS_1_2 | IXGB_RCTL_SECRC |
1979 IXGB_RCTL_CFF |
1980 (adapter->hw.mc_filter_type << IXGB_RCTL_MO_SHIFT);
1981
1982 switch (adapter->rx_buffer_len) {
1983 default:
1984 case IXGB_RXBUFFER_2048:
1985 reg_rctl |= IXGB_RCTL_BSIZE_2048;
1986 break;
1987 case IXGB_RXBUFFER_4096:
1988 reg_rctl |= IXGB_RCTL_BSIZE_4096;
1989 break;
1990 case IXGB_RXBUFFER_8192:
1991 reg_rctl |= IXGB_RCTL_BSIZE_8192;
1992 break;
1993 case IXGB_RXBUFFER_16384:
1994 reg_rctl |= IXGB_RCTL_BSIZE_16384;
1995 break;
1996 }
1997
1998 reg_rctl |= IXGB_RCTL_RXEN;
1999
2000
2001 /* Enable Receives */
2002 IXGB_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
2003
2004 return;
2005}
2006
2007/*********************************************************************
2008 *
2009 * Free receive related data structures.
2010 *
2011 **********************************************************************/
2012static void
2013ixgb_free_receive_structures(struct adapter * adapter)
2014{
2015 struct ixgb_buffer *rx_buffer;
2016 int i;
2017
2018 INIT_DEBUGOUT("free_receive_structures: begin");
2019
2020 if (adapter->rx_buffer_area != NULL) {
2021 rx_buffer = adapter->rx_buffer_area;
2022 for (i = 0; i < adapter->num_rx_desc; i++, rx_buffer++) {
2023 if (rx_buffer->map != NULL) {
2024 bus_dmamap_unload(adapter->rxtag, rx_buffer->map);
2025 bus_dmamap_destroy(adapter->rxtag, rx_buffer->map);
2026 }
2027 if (rx_buffer->m_head != NULL)
2028 m_freem(rx_buffer->m_head);
2029 rx_buffer->m_head = NULL;
2030 }
2031 }
2032 if (adapter->rx_buffer_area != NULL) {
2033 free(adapter->rx_buffer_area, M_DEVBUF);
2034 adapter->rx_buffer_area = NULL;
2035 }
2036 if (adapter->rxtag != NULL) {
2037 bus_dma_tag_destroy(adapter->rxtag);
2038 adapter->rxtag = NULL;
2039 }
2040 return;
2041}
2042
2043/*********************************************************************
2044 *
2045 * This routine executes in interrupt context. It replenishes
2046 * the mbufs in the descriptor and sends data which has been
2047 * dma'ed into host memory to upper layer.
2048 *
2049 * We loop at most count times if count is > 0, or until done if
2050 * count < 0.
2051 *
2052 *********************************************************************/
2053static void
2054ixgb_process_receive_interrupts(struct adapter * adapter, int count)
2055{
2056 struct ifnet *ifp;
2057 struct mbuf *mp;
2058#if __FreeBSD_version < 500000
2059 struct ether_header *eh;
2060#endif
2061 int eop = 0;
2062 int len;
2063 u_int8_t accept_frame = 0;
2064 int i;
2065 int next_to_use = 0;
2066 int eop_desc;
2067 /* Pointer to the receive descriptor being examined. */
2068 struct ixgb_rx_desc *current_desc;
2069
2070 IXGB_LOCK_ASSERT(adapter);
2071
2072 ifp = adapter->ifp;
2073 i = adapter->next_rx_desc_to_check;
2074 next_to_use = adapter->next_rx_desc_to_use;
2075 eop_desc = adapter->next_rx_desc_to_check;
2076 current_desc = &adapter->rx_desc_base[i];
2077
2078 if (!((current_desc->status) & IXGB_RX_DESC_STATUS_DD)) {
2079#ifdef _SV_
2080 adapter->no_pkts_avail++;
2081#endif
2082 return;
2083 }
2084 while ((current_desc->status & IXGB_RX_DESC_STATUS_DD) && (count != 0)) {
2085
2086 mp = adapter->rx_buffer_area[i].m_head;
2087 bus_dmamap_sync(adapter->rxtag, adapter->rx_buffer_area[i].map,
2088 BUS_DMASYNC_POSTREAD);
2089 accept_frame = 1;
2090 if (current_desc->status & IXGB_RX_DESC_STATUS_EOP) {
2091 count--;
2092 eop = 1;
2093 } else {
2094 eop = 0;
2095 }
2096 len = current_desc->length;
2097
2098 if (current_desc->errors & (IXGB_RX_DESC_ERRORS_CE |
2099 IXGB_RX_DESC_ERRORS_SE | IXGB_RX_DESC_ERRORS_P |
2100 IXGB_RX_DESC_ERRORS_RXE)) {
2101 accept_frame = 0;
2102 }
2103 if (accept_frame) {
2104
2105 /* Assign correct length to the current fragment */
2106 mp->m_len = len;
2107
2108 if (adapter->fmp == NULL) {
2109 mp->m_pkthdr.len = len;
2110 adapter->fmp = mp; /* Store the first mbuf */
2111 adapter->lmp = mp;
2112 } else {
2113 /* Chain mbuf's together */
2114 mp->m_flags &= ~M_PKTHDR;
2115 adapter->lmp->m_next = mp;
2116 adapter->lmp = adapter->lmp->m_next;
2117 adapter->fmp->m_pkthdr.len += len;
2118 }
2119
2120 if (eop) {
2121 eop_desc = i;
2122 adapter->fmp->m_pkthdr.rcvif = ifp;
2123
2124#if __FreeBSD_version < 500000
2125 eh = mtod(adapter->fmp, struct ether_header *);
2126
2127 /* Remove ethernet header from mbuf */
2128 m_adj(adapter->fmp, sizeof(struct ether_header));
2129 ixgb_receive_checksum(adapter, current_desc,
2130 adapter->fmp);
2131
2132 if (current_desc->status & IXGB_RX_DESC_STATUS_VP)
2133 VLAN_INPUT_TAG(eh, adapter->fmp,
2134 current_desc->special);
2135 else
2136 ether_input(ifp, eh, adapter->fmp);
2137#else
2138 ixgb_receive_checksum(adapter, current_desc,
2139 adapter->fmp);
2140 if (current_desc->status & IXGB_RX_DESC_STATUS_VP)
2141 VLAN_INPUT_TAG(ifp, adapter->fmp,
2142 current_desc->special,
2143 adapter->fmp = NULL);
2144
2145 if (adapter->fmp != NULL) {
2146 IXGB_UNLOCK(adapter);
2147 (*ifp->if_input) (ifp, adapter->fmp);
2148 IXGB_LOCK(adapter);
2149 }
2150#endif
2151 adapter->fmp = NULL;
2152 adapter->lmp = NULL;
2153 }
2154 adapter->rx_buffer_area[i].m_head = NULL;
2155 } else {
2156 adapter->dropped_pkts++;
2157 if (adapter->fmp != NULL)
2158 m_freem(adapter->fmp);
2159 adapter->fmp = NULL;
2160 adapter->lmp = NULL;
2161 }
2162
2163 /* Zero out the receive descriptors status */
2164 current_desc->status = 0;
2165
2166 /* Advance our pointers to the next descriptor */
2167 if (++i == adapter->num_rx_desc) {
2168 i = 0;
2169 current_desc = adapter->rx_desc_base;
2170 } else
2171 current_desc++;
2172 }
2173 adapter->next_rx_desc_to_check = i;
2174
2175 if (--i < 0)
2176 i = (adapter->num_rx_desc - 1);
2177
2178 /*
2179 * 82597EX: Workaround for redundent write back in receive descriptor ring (causes
2180 * memory corruption). Avoid using and re-submitting the most recently received RX
2181 * descriptor back to hardware.
2182 *
2183 * if(Last written back descriptor == EOP bit set descriptor)
2184 * then avoid re-submitting the most recently received RX descriptor
2185 * back to hardware.
2186 * if(Last written back descriptor != EOP bit set descriptor)
2187 * then avoid re-submitting the most recently received RX descriptors
2188 * till last EOP bit set descriptor.
2189 */
2190 if (eop_desc != i) {
2191 if (++eop_desc == adapter->num_rx_desc)
2192 eop_desc = 0;
2193 i = eop_desc;
2194 }
2195 /* Replenish the descriptors with new mbufs till last EOP bit set descriptor */
2196 while (next_to_use != i) {
2197 current_desc = &adapter->rx_desc_base[next_to_use];
2198 if ((current_desc->errors & (IXGB_RX_DESC_ERRORS_CE |
2199 IXGB_RX_DESC_ERRORS_SE | IXGB_RX_DESC_ERRORS_P |
2200 IXGB_RX_DESC_ERRORS_RXE))) {
2201 mp = adapter->rx_buffer_area[next_to_use].m_head;
2202 ixgb_get_buf(next_to_use, adapter, mp);
2203 } else {
2204 if (ixgb_get_buf(next_to_use, adapter, NULL) == ENOBUFS)
2205 break;
2206 }
2207 /* Advance our pointers to the next descriptor */
2208 if (++next_to_use == adapter->num_rx_desc) {
2209 next_to_use = 0;
2210 current_desc = adapter->rx_desc_base;
2211 } else
2212 current_desc++;
2213 }
2214 adapter->next_rx_desc_to_use = next_to_use;
2215 if (--next_to_use < 0)
2216 next_to_use = (adapter->num_rx_desc - 1);
2217 /* Advance the IXGB's Receive Queue #0 "Tail Pointer" */
2218 IXGB_WRITE_REG(&adapter->hw, RDT, next_to_use);
2219
2220 return;
2221}
2222
2223/*********************************************************************
2224 *
2225 * Verify that the hardware indicated that the checksum is valid.
2226 * Inform the stack about the status of checksum so that stack
2227 * doesn't spend time verifying the checksum.
2228 *
2229 *********************************************************************/
2230static void
2231ixgb_receive_checksum(struct adapter * adapter,
2232 struct ixgb_rx_desc * rx_desc,
2233 struct mbuf * mp)
2234{
2235 if (rx_desc->status & IXGB_RX_DESC_STATUS_IXSM) {
2236 mp->m_pkthdr.csum_flags = 0;
2237 return;
2238 }
2239 if (rx_desc->status & IXGB_RX_DESC_STATUS_IPCS) {
2240 /* Did it pass? */
2241 if (!(rx_desc->errors & IXGB_RX_DESC_ERRORS_IPE)) {
2242 /* IP Checksum Good */
2243 mp->m_pkthdr.csum_flags = CSUM_IP_CHECKED;
2244 mp->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2245
2246 } else {
2247 mp->m_pkthdr.csum_flags = 0;
2248 }
2249 }
2250 if (rx_desc->status & IXGB_RX_DESC_STATUS_TCPCS) {
2251 /* Did it pass? */
2252 if (!(rx_desc->errors & IXGB_RX_DESC_ERRORS_TCPE)) {
2253 mp->m_pkthdr.csum_flags |=
2254 (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
2255 mp->m_pkthdr.csum_data = htons(0xffff);
2256 }
2257 }
2258 return;
2259}
2260
2261
2262static void
2263ixgb_enable_vlans(struct adapter * adapter)
2264{
2265 uint32_t ctrl;
2266
2267 ctrl = IXGB_READ_REG(&adapter->hw, CTRL0);
2268 ctrl |= IXGB_CTRL0_VME;
2269 IXGB_WRITE_REG(&adapter->hw, CTRL0, ctrl);
2270
2271 return;
2272}
2273
2274
2275static void
2276ixgb_enable_intr(struct adapter * adapter)
2277{
2278 IXGB_WRITE_REG(&adapter->hw, IMS, (IXGB_INT_RXT0 | IXGB_INT_TXDW |
2279 IXGB_INT_RXDMT0 | IXGB_INT_LSC | IXGB_INT_RXO));
2280 return;
2281}
2282
2283static void
2284ixgb_disable_intr(struct adapter * adapter)
2285{
2286 IXGB_WRITE_REG(&adapter->hw, IMC, ~0);
2287 return;
2288}
2289
2290void
2291ixgb_write_pci_cfg(struct ixgb_hw * hw,
2292 uint32_t reg,
2293 uint16_t * value)
2294{
2295 pci_write_config(((struct ixgb_osdep *) hw->back)->dev, reg,
2296 *value, 2);
2297}
2298
2299/**********************************************************************
2300 *
2301 * Update the board statistics counters.
2302 *
2303 **********************************************************************/
2304static void
2305ixgb_update_stats_counters(struct adapter * adapter)
2306{
2307 struct ifnet *ifp;
2308
2309 adapter->stats.crcerrs += IXGB_READ_REG(&adapter->hw, CRCERRS);
2310 adapter->stats.gprcl += IXGB_READ_REG(&adapter->hw, GPRCL);
2311 adapter->stats.gprch += IXGB_READ_REG(&adapter->hw, GPRCH);
2312 adapter->stats.gorcl += IXGB_READ_REG(&adapter->hw, GORCL);
2313 adapter->stats.gorch += IXGB_READ_REG(&adapter->hw, GORCH);
2314 adapter->stats.bprcl += IXGB_READ_REG(&adapter->hw, BPRCL);
2315 adapter->stats.bprch += IXGB_READ_REG(&adapter->hw, BPRCH);
2316 adapter->stats.mprcl += IXGB_READ_REG(&adapter->hw, MPRCL);
2317 adapter->stats.mprch += IXGB_READ_REG(&adapter->hw, MPRCH);
2318 adapter->stats.roc += IXGB_READ_REG(&adapter->hw, ROC);
2319
2320 adapter->stats.mpc += IXGB_READ_REG(&adapter->hw, MPC);
2321 adapter->stats.dc += IXGB_READ_REG(&adapter->hw, DC);
2322 adapter->stats.rlec += IXGB_READ_REG(&adapter->hw, RLEC);
2323 adapter->stats.xonrxc += IXGB_READ_REG(&adapter->hw, XONRXC);
2324 adapter->stats.xontxc += IXGB_READ_REG(&adapter->hw, XONTXC);
2325 adapter->stats.xoffrxc += IXGB_READ_REG(&adapter->hw, XOFFRXC);
2326 adapter->stats.xofftxc += IXGB_READ_REG(&adapter->hw, XOFFTXC);
2327 adapter->stats.gptcl += IXGB_READ_REG(&adapter->hw, GPTCL);
2328 adapter->stats.gptch += IXGB_READ_REG(&adapter->hw, GPTCH);
2329 adapter->stats.gotcl += IXGB_READ_REG(&adapter->hw, GOTCL);
2330 adapter->stats.gotch += IXGB_READ_REG(&adapter->hw, GOTCH);
2331 adapter->stats.ruc += IXGB_READ_REG(&adapter->hw, RUC);
2332 adapter->stats.rfc += IXGB_READ_REG(&adapter->hw, RFC);
2333 adapter->stats.rjc += IXGB_READ_REG(&adapter->hw, RJC);
2334 adapter->stats.torl += IXGB_READ_REG(&adapter->hw, TORL);
2335 adapter->stats.torh += IXGB_READ_REG(&adapter->hw, TORH);
2336 adapter->stats.totl += IXGB_READ_REG(&adapter->hw, TOTL);
2337 adapter->stats.toth += IXGB_READ_REG(&adapter->hw, TOTH);
2338 adapter->stats.tprl += IXGB_READ_REG(&adapter->hw, TPRL);
2339 adapter->stats.tprh += IXGB_READ_REG(&adapter->hw, TPRH);
2340 adapter->stats.tptl += IXGB_READ_REG(&adapter->hw, TPTL);
2341 adapter->stats.tpth += IXGB_READ_REG(&adapter->hw, TPTH);
2342 adapter->stats.plt64c += IXGB_READ_REG(&adapter->hw, PLT64C);
2343 adapter->stats.mptcl += IXGB_READ_REG(&adapter->hw, MPTCL);
2344 adapter->stats.mptch += IXGB_READ_REG(&adapter->hw, MPTCH);
2345 adapter->stats.bptcl += IXGB_READ_REG(&adapter->hw, BPTCL);
2346 adapter->stats.bptch += IXGB_READ_REG(&adapter->hw, BPTCH);
2347
2348 adapter->stats.uprcl += IXGB_READ_REG(&adapter->hw, UPRCL);
2349 adapter->stats.uprch += IXGB_READ_REG(&adapter->hw, UPRCH);
2350 adapter->stats.vprcl += IXGB_READ_REG(&adapter->hw, VPRCL);
2351 adapter->stats.vprch += IXGB_READ_REG(&adapter->hw, VPRCH);
2352 adapter->stats.jprcl += IXGB_READ_REG(&adapter->hw, JPRCL);
2353 adapter->stats.jprch += IXGB_READ_REG(&adapter->hw, JPRCH);
2354 adapter->stats.rnbc += IXGB_READ_REG(&adapter->hw, RNBC);
2355 adapter->stats.icbc += IXGB_READ_REG(&adapter->hw, ICBC);
2356 adapter->stats.ecbc += IXGB_READ_REG(&adapter->hw, ECBC);
2357 adapter->stats.uptcl += IXGB_READ_REG(&adapter->hw, UPTCL);
2358 adapter->stats.uptch += IXGB_READ_REG(&adapter->hw, UPTCH);
2359 adapter->stats.vptcl += IXGB_READ_REG(&adapter->hw, VPTCL);
2360 adapter->stats.vptch += IXGB_READ_REG(&adapter->hw, VPTCH);
2361 adapter->stats.jptcl += IXGB_READ_REG(&adapter->hw, JPTCL);
2362 adapter->stats.jptch += IXGB_READ_REG(&adapter->hw, JPTCH);
2363 adapter->stats.tsctc += IXGB_READ_REG(&adapter->hw, TSCTC);
2364 adapter->stats.tsctfc += IXGB_READ_REG(&adapter->hw, TSCTFC);
2365 adapter->stats.ibic += IXGB_READ_REG(&adapter->hw, IBIC);
2366 adapter->stats.lfc += IXGB_READ_REG(&adapter->hw, LFC);
2367 adapter->stats.pfrc += IXGB_READ_REG(&adapter->hw, PFRC);
2368 adapter->stats.pftc += IXGB_READ_REG(&adapter->hw, PFTC);
2369 adapter->stats.mcfrc += IXGB_READ_REG(&adapter->hw, MCFRC);
2370
2371 ifp = adapter->ifp;
2372
2373 /* Fill out the OS statistics structure */
2374 ifp->if_ipackets = adapter->stats.gprcl;
2375 ifp->if_opackets = adapter->stats.gptcl;
2376 ifp->if_ibytes = adapter->stats.gorcl;
2377 ifp->if_obytes = adapter->stats.gotcl;
2378 ifp->if_imcasts = adapter->stats.mprcl;
2379 ifp->if_collisions = 0;
2380
2381 /* Rx Errors */
2382 ifp->if_ierrors =
2383 adapter->dropped_pkts +
2384 adapter->stats.crcerrs +
2385 adapter->stats.rnbc +
2386 adapter->stats.mpc +
2387 adapter->stats.rlec;
2388
2389
2390}
2391
2392
2393/**********************************************************************
2394 *
2395 * This routine is called only when ixgb_display_debug_stats is enabled.
2396 * This routine provides a way to take a look at important statistics
2397 * maintained by the driver and hardware.
2398 *
2399 **********************************************************************/
2400static void
2401ixgb_print_hw_stats(struct adapter * adapter)
2402{
2403 char buf_speed[100], buf_type[100];
2404 ixgb_bus_speed bus_speed;
2405 ixgb_bus_type bus_type;
2406 int unit = adapter->unit;
2407
2408#ifdef _SV_
2409 printf("ixgb%d: Packets not Avail = %ld\n", unit,
2410 adapter->no_pkts_avail);
2411 printf("ixgb%d: CleanTxInterrupts = %ld\n", unit,
2412 adapter->clean_tx_interrupts);
2413 printf("ixgb%d: ICR RXDMT0 = %lld\n", unit,
2414 (long long)adapter->sv_stats.icr_rxdmt0);
2415 printf("ixgb%d: ICR RXO = %lld\n", unit,
2416 (long long)adapter->sv_stats.icr_rxo);
2417 printf("ixgb%d: ICR RXT0 = %lld\n", unit,
2418 (long long)adapter->sv_stats.icr_rxt0);
2419 printf("ixgb%d: ICR TXDW = %lld\n", unit,
2420 (long long)adapter->sv_stats.icr_TXDW);
2421#endif /* _SV_ */
2422
2423 bus_speed = adapter->hw.bus.speed;
2424 bus_type = adapter->hw.bus.type;
2425 sprintf(buf_speed,
2426 bus_speed == ixgb_bus_speed_33 ? "33MHz" :
2427 bus_speed == ixgb_bus_speed_66 ? "66MHz" :
2428 bus_speed == ixgb_bus_speed_100 ? "100MHz" :
2429 bus_speed == ixgb_bus_speed_133 ? "133MHz" :
2430 "UNKNOWN");
2431 printf("ixgb%d: PCI_Bus_Speed = %s\n", unit,
2432 buf_speed);
2433
2434 sprintf(buf_type,
2435 bus_type == ixgb_bus_type_pci ? "PCI" :
2436 bus_type == ixgb_bus_type_pcix ? "PCI-X" :
2437 "UNKNOWN");
2438 printf("ixgb%d: PCI_Bus_Type = %s\n", unit,
2439 buf_type);
2440
2441 printf("ixgb%d: Tx Descriptors not Avail1 = %ld\n", unit,
2442 adapter->no_tx_desc_avail1);
2443 printf("ixgb%d: Tx Descriptors not Avail2 = %ld\n", unit,
2444 adapter->no_tx_desc_avail2);
2445 printf("ixgb%d: Std Mbuf Failed = %ld\n", unit,
2446 adapter->mbuf_alloc_failed);
2447 printf("ixgb%d: Std Cluster Failed = %ld\n", unit,
2448 adapter->mbuf_cluster_failed);
2449
2450 printf("ixgb%d: Defer count = %lld\n", unit,
2451 (long long)adapter->stats.dc);
2452 printf("ixgb%d: Missed Packets = %lld\n", unit,
2453 (long long)adapter->stats.mpc);
2454 printf("ixgb%d: Receive No Buffers = %lld\n", unit,
2455 (long long)adapter->stats.rnbc);
2456 printf("ixgb%d: Receive length errors = %lld\n", unit,
2457 (long long)adapter->stats.rlec);
2458 printf("ixgb%d: Crc errors = %lld\n", unit,
2459 (long long)adapter->stats.crcerrs);
2460 printf("ixgb%d: Driver dropped packets = %ld\n", unit,
2461 adapter->dropped_pkts);
2462
2463 printf("ixgb%d: XON Rcvd = %lld\n", unit,
2464 (long long)adapter->stats.xonrxc);
2465 printf("ixgb%d: XON Xmtd = %lld\n", unit,
2466 (long long)adapter->stats.xontxc);
2467 printf("ixgb%d: XOFF Rcvd = %lld\n", unit,
2468 (long long)adapter->stats.xoffrxc);
2469 printf("ixgb%d: XOFF Xmtd = %lld\n", unit,
2470 (long long)adapter->stats.xofftxc);
2471
2472 printf("ixgb%d: Good Packets Rcvd = %lld\n", unit,
2473 (long long)adapter->stats.gprcl);
2474 printf("ixgb%d: Good Packets Xmtd = %lld\n", unit,
2475 (long long)adapter->stats.gptcl);
2476
2477 printf("ixgb%d: Jumbo frames recvd = %lld\n", unit,
2478 (long long)adapter->stats.jprcl);
2479 printf("ixgb%d: Jumbo frames Xmtd = %lld\n", unit,
2480 (long long)adapter->stats.jptcl);
2481
2482 return;
2483
2484}
2485
2486static int
2487ixgb_sysctl_stats(SYSCTL_HANDLER_ARGS)
2488{
2489 int error;
2490 int result;
2491 struct adapter *adapter;
2492
2493 result = -1;
2494 error = sysctl_handle_int(oidp, &result, 0, req);
2495
2496 if (error || !req->newptr)
2497 return (error);
2498
2499 if (result == 1) {
2500 adapter = (struct adapter *) arg1;
2501 ixgb_print_hw_stats(adapter);
2502 }
2503 return error;
2504}
2
3Copyright (c) 2001-2004, Intel Corporation
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, 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
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30POSSIBILITY OF SUCH DAMAGE.
31
32***************************************************************************/
33
34/*$FreeBSD: head/sys/dev/ixgb/if_ixgb.c 148887 2005-08-09 10:20:02Z rwatson $*/
35
36#include <dev/ixgb/if_ixgb.h>
37
38/*********************************************************************
39 * Set this to one to display debug statistics
40 *********************************************************************/
41int ixgb_display_debug_stats = 0;
42
43/*********************************************************************
44 * Linked list of board private structures for all NICs found
45 *********************************************************************/
46
47struct adapter *ixgb_adapter_list = NULL;
48
49
50
51/*********************************************************************
52 * Driver version
53 *********************************************************************/
54
55char ixgb_driver_version[] = "1.0.6";
56char ixgb_copyright[] = "Copyright (c) 2001-2004 Intel Corporation.";
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 ixgb_strings
63 * Last entry must be all 0s
64 *
65 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
66 *********************************************************************/
67
68static ixgb_vendor_info_t ixgb_vendor_info_array[] =
69{
70 /* Intel(R) PRO/10000 Network Connection */
71 {INTEL_VENDOR_ID, IXGB_DEVICE_ID_82597EX, PCI_ANY_ID, PCI_ANY_ID, 0},
72 {INTEL_VENDOR_ID, IXGB_DEVICE_ID_82597EX_SR, PCI_ANY_ID, PCI_ANY_ID, 0},
73 /* required last entry */
74 {0, 0, 0, 0, 0}
75};
76
77/*********************************************************************
78 * Table of branding strings for all supported NICs.
79 *********************************************************************/
80
81static char *ixgb_strings[] = {
82 "Intel(R) PRO/10GbE Network Driver"
83};
84
85/*********************************************************************
86 * Function prototypes
87 *********************************************************************/
88static int ixgb_probe(device_t);
89static int ixgb_attach(device_t);
90static int ixgb_detach(device_t);
91static int ixgb_shutdown(device_t);
92static void ixgb_intr(void *);
93static void ixgb_start(struct ifnet *);
94static void ixgb_start_locked(struct ifnet *);
95static int ixgb_ioctl(struct ifnet *, IOCTL_CMD_TYPE, caddr_t);
96static void ixgb_watchdog(struct ifnet *);
97static void ixgb_init(void *);
98static void ixgb_init_locked(struct adapter *);
99static void ixgb_stop(void *);
100static void ixgb_media_status(struct ifnet *, struct ifmediareq *);
101static int ixgb_media_change(struct ifnet *);
102static void ixgb_identify_hardware(struct adapter *);
103static int ixgb_allocate_pci_resources(struct adapter *);
104static void ixgb_free_pci_resources(struct adapter *);
105static void ixgb_local_timer(void *);
106static int ixgb_hardware_init(struct adapter *);
107static void ixgb_setup_interface(device_t, struct adapter *);
108static int ixgb_setup_transmit_structures(struct adapter *);
109static void ixgb_initialize_transmit_unit(struct adapter *);
110static int ixgb_setup_receive_structures(struct adapter *);
111static void ixgb_initialize_receive_unit(struct adapter *);
112static void ixgb_enable_intr(struct adapter *);
113static void ixgb_disable_intr(struct adapter *);
114static void ixgb_free_transmit_structures(struct adapter *);
115static void ixgb_free_receive_structures(struct adapter *);
116static void ixgb_update_stats_counters(struct adapter *);
117static void ixgb_clean_transmit_interrupts(struct adapter *);
118static int ixgb_allocate_receive_structures(struct adapter *);
119static int ixgb_allocate_transmit_structures(struct adapter *);
120static void ixgb_process_receive_interrupts(struct adapter *, int);
121static void
122ixgb_receive_checksum(struct adapter *,
123 struct ixgb_rx_desc * rx_desc,
124 struct mbuf *);
125static void
126ixgb_transmit_checksum_setup(struct adapter *,
127 struct mbuf *,
128 u_int8_t *);
129static void ixgb_set_promisc(struct adapter *);
130static void ixgb_disable_promisc(struct adapter *);
131static void ixgb_set_multi(struct adapter *);
132static void ixgb_print_hw_stats(struct adapter *);
133static void ixgb_print_link_status(struct adapter *);
134static int
135ixgb_get_buf(int i, struct adapter *,
136 struct mbuf *);
137static void ixgb_enable_vlans(struct adapter * adapter);
138static int ixgb_encap(struct adapter * adapter, struct mbuf * m_head);
139static int ixgb_sysctl_stats(SYSCTL_HANDLER_ARGS);
140static int
141ixgb_dma_malloc(struct adapter *, bus_size_t,
142 struct ixgb_dma_alloc *, int);
143static void ixgb_dma_free(struct adapter *, struct ixgb_dma_alloc *);
144
145/*********************************************************************
146 * FreeBSD Device Interface Entry Points
147 *********************************************************************/
148
149static device_method_t ixgb_methods[] = {
150 /* Device interface */
151 DEVMETHOD(device_probe, ixgb_probe),
152 DEVMETHOD(device_attach, ixgb_attach),
153 DEVMETHOD(device_detach, ixgb_detach),
154 DEVMETHOD(device_shutdown, ixgb_shutdown),
155 {0, 0}
156};
157
158static driver_t ixgb_driver = {
159 "ixgb", ixgb_methods, sizeof(struct adapter),
160};
161
162static devclass_t ixgb_devclass;
163DRIVER_MODULE(if_ixgb, pci, ixgb_driver, ixgb_devclass, 0, 0);
164
165MODULE_DEPEND(if_ixgb, pci, 1, 1, 1);
166MODULE_DEPEND(if_ixgb, ether, 1, 1, 1);
167
168/* some defines for controlling descriptor fetches in h/w */
169#define RXDCTL_PTHRESH_DEFAULT 128 /* chip considers prefech below this */
170#define RXDCTL_HTHRESH_DEFAULT 16 /* chip will only prefetch if tail is
171 * pushed this many descriptors from
172 * head */
173#define RXDCTL_WTHRESH_DEFAULT 0 /* chip writes back at this many or RXT0 */
174
175
176/*********************************************************************
177 * Device identification routine
178 *
179 * ixgb_probe determines if the driver should be loaded on
180 * adapter based on PCI vendor/device id of the adapter.
181 *
182 * return 0 on success, positive on failure
183 *********************************************************************/
184
185static int
186ixgb_probe(device_t dev)
187{
188 ixgb_vendor_info_t *ent;
189
190 u_int16_t pci_vendor_id = 0;
191 u_int16_t pci_device_id = 0;
192 u_int16_t pci_subvendor_id = 0;
193 u_int16_t pci_subdevice_id = 0;
194 char adapter_name[60];
195
196 INIT_DEBUGOUT("ixgb_probe: begin");
197
198 pci_vendor_id = pci_get_vendor(dev);
199 if (pci_vendor_id != IXGB_VENDOR_ID)
200 return (ENXIO);
201
202 pci_device_id = pci_get_device(dev);
203 pci_subvendor_id = pci_get_subvendor(dev);
204 pci_subdevice_id = pci_get_subdevice(dev);
205
206 ent = ixgb_vendor_info_array;
207 while (ent->vendor_id != 0) {
208 if ((pci_vendor_id == ent->vendor_id) &&
209 (pci_device_id == ent->device_id) &&
210
211 ((pci_subvendor_id == ent->subvendor_id) ||
212 (ent->subvendor_id == PCI_ANY_ID)) &&
213
214 ((pci_subdevice_id == ent->subdevice_id) ||
215 (ent->subdevice_id == PCI_ANY_ID))) {
216 sprintf(adapter_name, "%s, Version - %s",
217 ixgb_strings[ent->index],
218 ixgb_driver_version);
219 device_set_desc_copy(dev, adapter_name);
220 return (BUS_PROBE_DEFAULT);
221 }
222 ent++;
223 }
224
225 return (ENXIO);
226}
227
228/*********************************************************************
229 * Device initialization routine
230 *
231 * The attach entry point is called when the driver is being loaded.
232 * This routine identifies the type of hardware, allocates all resources
233 * and initializes the hardware.
234 *
235 * return 0 on success, positive on failure
236 *********************************************************************/
237
238static int
239ixgb_attach(device_t dev)
240{
241 struct adapter *adapter;
242 int tsize, rsize;
243 int error = 0;
244
245 printf("ixgb%d: %s\n", device_get_unit(dev), ixgb_copyright);
246 INIT_DEBUGOUT("ixgb_attach: begin");
247
248 /* Allocate, clear, and link in our adapter structure */
249 if (!(adapter = device_get_softc(dev))) {
250 printf("ixgb: adapter structure allocation failed\n");
251 return (ENOMEM);
252 }
253 bzero(adapter, sizeof(struct adapter));
254 adapter->dev = dev;
255 adapter->osdep.dev = dev;
256 adapter->unit = device_get_unit(dev);
257 IXGB_LOCK_INIT(adapter, device_get_nameunit(dev));
258
259 if (ixgb_adapter_list != NULL)
260 ixgb_adapter_list->prev = adapter;
261 adapter->next = ixgb_adapter_list;
262 ixgb_adapter_list = adapter;
263
264 /* SYSCTL APIs */
265 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
266 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
267 OID_AUTO, "stats", CTLTYPE_INT | CTLFLAG_RW,
268 (void *)adapter, 0,
269 ixgb_sysctl_stats, "I", "Statistics");
270
271 callout_init(&adapter->timer, CALLOUT_MPSAFE);
272
273 /* Determine hardware revision */
274 ixgb_identify_hardware(adapter);
275
276 /* Parameters (to be read from user) */
277 adapter->num_tx_desc = IXGB_MAX_TXD;
278 adapter->num_rx_desc = IXGB_MAX_RXD;
279 adapter->tx_int_delay = TIDV;
280 adapter->rx_int_delay = RDTR;
281 adapter->rx_buffer_len = IXGB_RXBUFFER_2048;
282
283 adapter->hw.fc.high_water = FCRTH;
284 adapter->hw.fc.low_water = FCRTL;
285 adapter->hw.fc.pause_time = FCPAUSE;
286 adapter->hw.fc.send_xon = TRUE;
287 adapter->hw.fc.type = FLOW_CONTROL;
288
289
290 /* Set the max frame size assuming standard ethernet sized frames */
291 adapter->hw.max_frame_size =
292 ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN;
293
294 if (ixgb_allocate_pci_resources(adapter)) {
295 printf("ixgb%d: Allocation of PCI resources failed\n",
296 adapter->unit);
297 error = ENXIO;
298 goto err_pci;
299 }
300 tsize = IXGB_ROUNDUP(adapter->num_tx_desc *
301 sizeof(struct ixgb_tx_desc), 4096);
302
303 /* Allocate Transmit Descriptor ring */
304 if (ixgb_dma_malloc(adapter, tsize, &adapter->txdma, BUS_DMA_NOWAIT)) {
305 printf("ixgb%d: Unable to allocate TxDescriptor memory\n",
306 adapter->unit);
307 error = ENOMEM;
308 goto err_tx_desc;
309 }
310 adapter->tx_desc_base = (struct ixgb_tx_desc *) adapter->txdma.dma_vaddr;
311
312 rsize = IXGB_ROUNDUP(adapter->num_rx_desc *
313 sizeof(struct ixgb_rx_desc), 4096);
314
315 /* Allocate Receive Descriptor ring */
316 if (ixgb_dma_malloc(adapter, rsize, &adapter->rxdma, BUS_DMA_NOWAIT)) {
317 printf("ixgb%d: Unable to allocate rx_desc memory\n",
318 adapter->unit);
319 error = ENOMEM;
320 goto err_rx_desc;
321 }
322 adapter->rx_desc_base = (struct ixgb_rx_desc *) adapter->rxdma.dma_vaddr;
323
324 /* Initialize the hardware */
325 if (ixgb_hardware_init(adapter)) {
326 printf("ixgb%d: Unable to initialize the hardware\n",
327 adapter->unit);
328 error = EIO;
329 goto err_hw_init;
330 }
331 /* Setup OS specific network interface */
332 ixgb_setup_interface(dev, adapter);
333
334 /* Initialize statistics */
335 ixgb_clear_hw_cntrs(&adapter->hw);
336 ixgb_update_stats_counters(adapter);
337
338 INIT_DEBUGOUT("ixgb_attach: end");
339 return (0);
340
341err_hw_init:
342 ixgb_dma_free(adapter, &adapter->rxdma);
343err_rx_desc:
344 ixgb_dma_free(adapter, &adapter->txdma);
345err_tx_desc:
346err_pci:
347 ixgb_free_pci_resources(adapter);
348 sysctl_ctx_free(&adapter->sysctl_ctx);
349 return (error);
350
351}
352
353/*********************************************************************
354 * Device removal routine
355 *
356 * The detach entry point is called when the driver is being removed.
357 * This routine stops the adapter and deallocates all the resources
358 * that were allocated for driver operation.
359 *
360 * return 0 on success, positive on failure
361 *********************************************************************/
362
363static int
364ixgb_detach(device_t dev)
365{
366 struct adapter *adapter = device_get_softc(dev);
367 struct ifnet *ifp = adapter->ifp;
368
369 INIT_DEBUGOUT("ixgb_detach: begin");
370
371 IXGB_LOCK(adapter);
372 adapter->in_detach = 1;
373
374 ixgb_stop(adapter);
375 IXGB_UNLOCK(adapter);
376
377#if __FreeBSD_version < 500000
378 ether_ifdetach(adapter->ifp, ETHER_BPF_SUPPORTED);
379#else
380 ether_ifdetach(adapter->ifp);
381 if_free(adapter->ifp);
382#endif
383 ixgb_free_pci_resources(adapter);
384
385
386 /* Free Transmit Descriptor ring */
387 if (adapter->tx_desc_base) {
388 ixgb_dma_free(adapter, &adapter->txdma);
389 adapter->tx_desc_base = NULL;
390 }
391 /* Free Receive Descriptor ring */
392 if (adapter->rx_desc_base) {
393 ixgb_dma_free(adapter, &adapter->rxdma);
394 adapter->rx_desc_base = NULL;
395 }
396 /* Remove from the adapter list */
397 if (ixgb_adapter_list == adapter)
398 ixgb_adapter_list = adapter->next;
399 if (adapter->next != NULL)
400 adapter->next->prev = adapter->prev;
401 if (adapter->prev != NULL)
402 adapter->prev->next = adapter->next;
403
404 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
405 ifp->if_timer = 0;
406
407 IXGB_LOCK_DESTROY(adapter);
408 return (0);
409}
410
411/*********************************************************************
412 *
413 * Shutdown entry point
414 *
415 **********************************************************************/
416
417static int
418ixgb_shutdown(device_t dev)
419{
420 struct adapter *adapter = device_get_softc(dev);
421 IXGB_LOCK(adapter);
422 ixgb_stop(adapter);
423 IXGB_UNLOCK(adapter);
424 return (0);
425}
426
427
428/*********************************************************************
429 * Transmit entry point
430 *
431 * ixgb_start is called by the stack to initiate a transmit.
432 * The driver will remain in this routine as long as there are
433 * packets to transmit and transmit resources are available.
434 * In case resources are not available stack is notified and
435 * the packet is requeued.
436 **********************************************************************/
437
438static void
439ixgb_start_locked(struct ifnet * ifp)
440{
441 struct mbuf *m_head;
442 struct adapter *adapter = ifp->if_softc;
443
444 IXGB_LOCK_ASSERT(adapter);
445
446 if (!adapter->link_active)
447 return;
448
449 while (ifp->if_snd.ifq_head != NULL) {
450 IF_DEQUEUE(&ifp->if_snd, m_head);
451
452 if (m_head == NULL)
453 break;
454
455 if (ixgb_encap(adapter, m_head)) {
456 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
457 IF_PREPEND(&ifp->if_snd, m_head);
458 break;
459 }
460 /* Send a copy of the frame to the BPF listener */
461#if __FreeBSD_version < 500000
462 if (ifp->if_bpf)
463 bpf_mtap(ifp, m_head);
464#else
465 BPF_MTAP(ifp, m_head);
466#endif
467 /* Set timeout in case hardware has problems transmitting */
468 ifp->if_timer = IXGB_TX_TIMEOUT;
469
470 }
471 return;
472}
473
474static void
475ixgb_start(struct ifnet *ifp)
476{
477 struct adapter *adapter = ifp->if_softc;
478
479 IXGB_LOCK(adapter);
480 ixgb_start_locked(ifp);
481 IXGB_UNLOCK(adapter);
482 return;
483}
484
485/*********************************************************************
486 * Ioctl entry point
487 *
488 * ixgb_ioctl is called when the user wants to configure the
489 * interface.
490 *
491 * return 0 on success, positive on failure
492 **********************************************************************/
493
494static int
495ixgb_ioctl(struct ifnet * ifp, IOCTL_CMD_TYPE command, caddr_t data)
496{
497 int mask, error = 0;
498 struct ifreq *ifr = (struct ifreq *) data;
499 struct adapter *adapter = ifp->if_softc;
500
501 if (adapter->in_detach)
502 goto out;
503
504 switch (command) {
505 case SIOCSIFADDR:
506 case SIOCGIFADDR:
507 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCxIFADDR (Get/Set Interface Addr)");
508 ether_ioctl(ifp, command, data);
509 break;
510 case SIOCSIFMTU:
511 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFMTU (Set Interface MTU)");
512 if (ifr->ifr_mtu > IXGB_MAX_JUMBO_FRAME_SIZE - ETHER_HDR_LEN) {
513 error = EINVAL;
514 } else {
515 IXGB_LOCK(adapter);
516 ifp->if_mtu = ifr->ifr_mtu;
517 adapter->hw.max_frame_size =
518 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
519
520 ixgb_init_locked(adapter);
521 IXGB_UNLOCK(adapter);
522 }
523 break;
524 case SIOCSIFFLAGS:
525 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFFLAGS (Set Interface Flags)");
526 IXGB_LOCK(adapter);
527 if (ifp->if_flags & IFF_UP) {
528 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
529 ixgb_init_locked(adapter);
530 }
531 ixgb_disable_promisc(adapter);
532 ixgb_set_promisc(adapter);
533 } else {
534 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
535 ixgb_stop(adapter);
536 }
537 }
538 IXGB_UNLOCK(adapter);
539 break;
540 case SIOCADDMULTI:
541 case SIOCDELMULTI:
542 IOCTL_DEBUGOUT("ioctl rcv'd: SIOC(ADD|DEL)MULTI");
543 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
544 IXGB_LOCK(adapter);
545 ixgb_disable_intr(adapter);
546 ixgb_set_multi(adapter);
547 ixgb_enable_intr(adapter);
548 IXGB_UNLOCK(adapter);
549 }
550 break;
551 case SIOCSIFMEDIA:
552 case SIOCGIFMEDIA:
553 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCxIFMEDIA (Get/Set Interface Media)");
554 error = ifmedia_ioctl(ifp, ifr, &adapter->media, command);
555 break;
556 case SIOCSIFCAP:
557 IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFCAP (Set Capabilities)");
558 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
559 if (mask & IFCAP_HWCSUM) {
560 if (IFCAP_HWCSUM & ifp->if_capenable)
561 ifp->if_capenable &= ~IFCAP_HWCSUM;
562 else
563 ifp->if_capenable |= IFCAP_HWCSUM;
564 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
565 ixgb_init(adapter);
566 }
567 break;
568 default:
569 IOCTL_DEBUGOUT1("ioctl received: UNKNOWN (0x%X)\n", (int)command);
570 error = EINVAL;
571 }
572
573out:
574 return (error);
575}
576
577/*********************************************************************
578 * Watchdog entry point
579 *
580 * This routine is called whenever hardware quits transmitting.
581 *
582 **********************************************************************/
583
584static void
585ixgb_watchdog(struct ifnet * ifp)
586{
587 struct adapter *adapter;
588 adapter = ifp->if_softc;
589
590 /*
591 * If we are in this routine because of pause frames, then don't
592 * reset the hardware.
593 */
594 if (IXGB_READ_REG(&adapter->hw, STATUS) & IXGB_STATUS_TXOFF) {
595 ifp->if_timer = IXGB_TX_TIMEOUT;
596 return;
597 }
598 printf("ixgb%d: watchdog timeout -- resetting\n", adapter->unit);
599
600 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
601
602
603 ixgb_stop(adapter);
604 ixgb_init(adapter);
605
606
607 ifp->if_oerrors++;
608
609 return;
610}
611
612/*********************************************************************
613 * Init entry point
614 *
615 * This routine is used in two ways. It is used by the stack as
616 * init entry point in network interface structure. It is also used
617 * by the driver as a hw/sw initialization routine to get to a
618 * consistent state.
619 *
620 * return 0 on success, positive on failure
621 **********************************************************************/
622
623static void
624ixgb_init_locked(struct adapter *adapter)
625{
626 struct ifnet *ifp;
627
628 INIT_DEBUGOUT("ixgb_init: begin");
629
630 IXGB_LOCK_ASSERT(adapter);
631
632 ixgb_stop(adapter);
633
634 /* Get the latest mac address, User can use a LAA */
635 bcopy(IFP2ENADDR(adapter->ifp), adapter->hw.curr_mac_addr,
636 IXGB_ETH_LENGTH_OF_ADDRESS);
637
638 /* Initialize the hardware */
639 if (ixgb_hardware_init(adapter)) {
640 printf("ixgb%d: Unable to initialize the hardware\n",
641 adapter->unit);
642 return;
643 }
644 ixgb_enable_vlans(adapter);
645
646 /* Prepare transmit descriptors and buffers */
647 if (ixgb_setup_transmit_structures(adapter)) {
648 printf("ixgb%d: Could not setup transmit structures\n",
649 adapter->unit);
650 ixgb_stop(adapter);
651 return;
652 }
653 ixgb_initialize_transmit_unit(adapter);
654
655 /* Setup Multicast table */
656 ixgb_set_multi(adapter);
657
658 /* Prepare receive descriptors and buffers */
659 if (ixgb_setup_receive_structures(adapter)) {
660 printf("ixgb%d: Could not setup receive structures\n",
661 adapter->unit);
662 ixgb_stop(adapter);
663 return;
664 }
665 ixgb_initialize_receive_unit(adapter);
666
667 /* Don't loose promiscuous settings */
668 ixgb_set_promisc(adapter);
669
670 ifp = adapter->ifp;
671 ifp->if_drv_flags |= IFF_DRV_RUNNING;
672 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
673
674
675 if (ifp->if_capenable & IFCAP_TXCSUM)
676 ifp->if_hwassist = IXGB_CHECKSUM_FEATURES;
677 else
678 ifp->if_hwassist = 0;
679
680
681 /* Enable jumbo frames */
682 if (ifp->if_mtu > ETHERMTU) {
683 uint32_t temp_reg;
684 IXGB_WRITE_REG(&adapter->hw, MFS,
685 adapter->hw.max_frame_size << IXGB_MFS_SHIFT);
686 temp_reg = IXGB_READ_REG(&adapter->hw, CTRL0);
687 temp_reg |= IXGB_CTRL0_JFE;
688 IXGB_WRITE_REG(&adapter->hw, CTRL0, temp_reg);
689 }
690 callout_reset(&adapter->timer, 2 * hz, ixgb_local_timer, adapter);
691 ixgb_clear_hw_cntrs(&adapter->hw);
692#ifdef DEVICE_POLLING
693 /*
694 * Only disable interrupts if we are polling, make sure they are on
695 * otherwise.
696 */
697 if (ifp->if_flags & IFF_POLLING)
698 ixgb_disable_intr(adapter);
699 else
700#endif /* DEVICE_POLLING */
701 ixgb_enable_intr(adapter);
702
703 return;
704}
705
706static void
707ixgb_init(void *arg)
708{
709 struct adapter *adapter = arg;
710
711 IXGB_LOCK(adapter);
712 ixgb_init_locked(adapter);
713 IXGB_UNLOCK(adapter);
714 return;
715}
716
717#ifdef DEVICE_POLLING
718static poll_handler_t ixgb_poll;
719
720static void
721ixgb_poll_locked(struct ifnet * ifp, enum poll_cmd cmd, int count)
722{
723 struct adapter *adapter = ifp->if_softc;
724 u_int32_t reg_icr;
725
726 IXGB_LOCK_ASSERT(adapter);
727
728 if (!(ifp->if_capenable & IFCAP_POLLING)) {
729 ether_poll_deregister(ifp);
730 cmd = POLL_DEREGISTER;
731 }
732
733 if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */
734 ixgb_enable_intr(adapter);
735 return;
736 }
737 if (cmd == POLL_AND_CHECK_STATUS) {
738 reg_icr = IXGB_READ_REG(&adapter->hw, ICR);
739 if (reg_icr & (IXGB_INT_RXSEQ | IXGB_INT_LSC)) {
740 callout_stop(&adapter->timer);
741 ixgb_check_for_link(&adapter->hw);
742 ixgb_print_link_status(adapter);
743 callout_reset(&adapter->timer, 2 * hz, ixgb_local_timer,
744 adapter);
745 }
746 }
747 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
748 ixgb_process_receive_interrupts(adapter, count);
749 ixgb_clean_transmit_interrupts(adapter);
750 }
751 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
752 ifp->if_snd.ifq_head != NULL)
753 ixgb_start_locked(ifp);
754}
755
756static void
757ixgb_poll(struct ifnet * ifp, enum poll_cmd cmd, int count)
758{
759 struct adapter *adapter = ifp->if_softc;
760
761 IXGB_LOCK(adapter);
762 ixgb_poll_locked(ifp, cmd, count);
763 IXGB_UNLOCK(adapter);
764}
765#endif /* DEVICE_POLLING */
766
767/*********************************************************************
768 *
769 * Interrupt Service routine
770 *
771 **********************************************************************/
772
773static void
774ixgb_intr(void *arg)
775{
776 u_int32_t loop_cnt = IXGB_MAX_INTR;
777 u_int32_t reg_icr;
778 struct ifnet *ifp;
779 struct adapter *adapter = arg;
780 boolean_t rxdmt0 = FALSE;
781
782 IXGB_LOCK(adapter);
783
784 ifp = adapter->ifp;
785
786#ifdef DEVICE_POLLING
787 if (ifp->if_flags & IFF_POLLING) {
788 IXGB_UNLOCK(adapter);
789 return;
790 }
791
792 if ((ifp->if_capenable & IFCAP_POLLING) &&
793 ether_poll_register(ixgb_poll, ifp)) {
794 ixgb_disable_intr(adapter);
795 ixgb_poll_locked(ifp, 0, 1);
796 IXGB_UNLOCK(adapter);
797 return;
798 }
799#endif /* DEVICE_POLLING */
800
801 reg_icr = IXGB_READ_REG(&adapter->hw, ICR);
802 if (reg_icr == 0) {
803 IXGB_UNLOCK(adapter);
804 return;
805 }
806
807 if (reg_icr & IXGB_INT_RXDMT0)
808 rxdmt0 = TRUE;
809
810#ifdef _SV_
811 if (reg_icr & IXGB_INT_RXDMT0)
812 adapter->sv_stats.icr_rxdmt0++;
813 if (reg_icr & IXGB_INT_RXO)
814 adapter->sv_stats.icr_rxo++;
815 if (reg_icr & IXGB_INT_RXT0)
816 adapter->sv_stats.icr_rxt0++;
817 if (reg_icr & IXGB_INT_TXDW)
818 adapter->sv_stats.icr_TXDW++;
819#endif /* _SV_ */
820
821 /* Link status change */
822 if (reg_icr & (IXGB_INT_RXSEQ | IXGB_INT_LSC)) {
823 callout_stop(&adapter->timer);
824 ixgb_check_for_link(&adapter->hw);
825 ixgb_print_link_status(adapter);
826 callout_reset(&adapter->timer, 2 * hz, ixgb_local_timer,
827 adapter);
828 }
829 while (loop_cnt > 0) {
830 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
831 ixgb_process_receive_interrupts(adapter, -1);
832 ixgb_clean_transmit_interrupts(adapter);
833 }
834 loop_cnt--;
835 }
836
837 if (rxdmt0 && adapter->raidc) {
838 IXGB_WRITE_REG(&adapter->hw, IMC, IXGB_INT_RXDMT0);
839 IXGB_WRITE_REG(&adapter->hw, IMS, IXGB_INT_RXDMT0);
840 }
841 if (ifp->if_drv_flags & IFF_DRV_RUNNING && ifp->if_snd.ifq_head != NULL)
842 ixgb_start_locked(ifp);
843
844 IXGB_UNLOCK(adapter);
845 return;
846}
847
848
849/*********************************************************************
850 *
851 * Media Ioctl callback
852 *
853 * This routine is called whenever the user queries the status of
854 * the interface using ifconfig.
855 *
856 **********************************************************************/
857static void
858ixgb_media_status(struct ifnet * ifp, struct ifmediareq * ifmr)
859{
860 struct adapter *adapter = ifp->if_softc;
861
862 INIT_DEBUGOUT("ixgb_media_status: begin");
863
864 ixgb_check_for_link(&adapter->hw);
865 ixgb_print_link_status(adapter);
866
867 ifmr->ifm_status = IFM_AVALID;
868 ifmr->ifm_active = IFM_ETHER;
869
870 if (!adapter->hw.link_up)
871 return;
872
873 ifmr->ifm_status |= IFM_ACTIVE;
874 ifmr->ifm_active |= IFM_1000_SX | IFM_FDX;
875
876 return;
877}
878
879/*********************************************************************
880 *
881 * Media Ioctl callback
882 *
883 * This routine is called when the user changes speed/duplex using
884 * media/mediopt option with ifconfig.
885 *
886 **********************************************************************/
887static int
888ixgb_media_change(struct ifnet * ifp)
889{
890 struct adapter *adapter = ifp->if_softc;
891 struct ifmedia *ifm = &adapter->media;
892
893 INIT_DEBUGOUT("ixgb_media_change: begin");
894
895 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
896 return (EINVAL);
897
898 return (0);
899}
900
901/*********************************************************************
902 *
903 * This routine maps the mbufs to tx descriptors.
904 *
905 * return 0 on success, positive on failure
906 **********************************************************************/
907
908static int
909ixgb_encap(struct adapter * adapter, struct mbuf * m_head)
910{
911 u_int8_t txd_popts;
912 int i, j, error, nsegs;
913
914#if __FreeBSD_version < 500000
915 struct ifvlan *ifv = NULL;
916#else
917 struct m_tag *mtag;
918#endif
919 bus_dma_segment_t segs[IXGB_MAX_SCATTER];
920 bus_dmamap_t map;
921 struct ixgb_buffer *tx_buffer = NULL;
922 struct ixgb_tx_desc *current_tx_desc = NULL;
923 struct ifnet *ifp = adapter->ifp;
924
925 /*
926 * Force a cleanup if number of TX descriptors available hits the
927 * threshold
928 */
929 if (adapter->num_tx_desc_avail <= IXGB_TX_CLEANUP_THRESHOLD) {
930 ixgb_clean_transmit_interrupts(adapter);
931 }
932 if (adapter->num_tx_desc_avail <= IXGB_TX_CLEANUP_THRESHOLD) {
933 adapter->no_tx_desc_avail1++;
934 return (ENOBUFS);
935 }
936 /*
937 * Map the packet for DMA.
938 */
939 if (bus_dmamap_create(adapter->txtag, BUS_DMA_NOWAIT, &map)) {
940 adapter->no_tx_map_avail++;
941 return (ENOMEM);
942 }
943 error = bus_dmamap_load_mbuf_sg(adapter->txtag, map, m_head, segs,
944 &nsegs, BUS_DMA_NOWAIT);
945 if (error != 0) {
946 adapter->no_tx_dma_setup++;
947 printf("ixgb%d: ixgb_encap: bus_dmamap_load_mbuf failed; "
948 "error %u\n", adapter->unit, error);
949 bus_dmamap_destroy(adapter->txtag, map);
950 return (error);
951 }
952 KASSERT(nsegs != 0, ("ixgb_encap: empty packet"));
953
954 if (nsegs > adapter->num_tx_desc_avail) {
955 adapter->no_tx_desc_avail2++;
956 bus_dmamap_destroy(adapter->txtag, map);
957 return (ENOBUFS);
958 }
959 if (ifp->if_hwassist > 0) {
960 ixgb_transmit_checksum_setup(adapter, m_head,
961 &txd_popts);
962 } else
963 txd_popts = 0;
964
965 /* Find out if we are in vlan mode */
966#if __FreeBSD_version < 500000
967 if ((m_head->m_flags & (M_PROTO1 | M_PKTHDR)) == (M_PROTO1 | M_PKTHDR) &&
968 m_head->m_pkthdr.rcvif != NULL &&
969 m_head->m_pkthdr.rcvif->if_type == IFT_L2VLAN)
970 ifv = m_head->m_pkthdr.rcvif->if_softc;
971#else
972 mtag = VLAN_OUTPUT_TAG(ifp, m_head);
973#endif
974 i = adapter->next_avail_tx_desc;
975 for (j = 0; j < nsegs; j++) {
976 tx_buffer = &adapter->tx_buffer_area[i];
977 current_tx_desc = &adapter->tx_desc_base[i];
978
979 current_tx_desc->buff_addr = htole64(segs[j].ds_addr);
980 current_tx_desc->cmd_type_len = (adapter->txd_cmd | segs[j].ds_len);
981 current_tx_desc->popts = txd_popts;
982 if (++i == adapter->num_tx_desc)
983 i = 0;
984
985 tx_buffer->m_head = NULL;
986 }
987
988 adapter->num_tx_desc_avail -= nsegs;
989 adapter->next_avail_tx_desc = i;
990
991#if __FreeBSD_version < 500000
992 if (ifv != NULL) {
993 /* Set the vlan id */
994 current_tx_desc->vlan = ifv->ifv_tag;
995#else
996 if (mtag != NULL) {
997 /* Set the vlan id */
998 current_tx_desc->vlan = VLAN_TAG_VALUE(mtag);
999#endif
1000
1001 /* Tell hardware to add tag */
1002 current_tx_desc->cmd_type_len |= IXGB_TX_DESC_CMD_VLE;
1003 }
1004 tx_buffer->m_head = m_head;
1005 tx_buffer->map = map;
1006 bus_dmamap_sync(adapter->txtag, map, BUS_DMASYNC_PREWRITE);
1007
1008 /*
1009 * Last Descriptor of Packet needs End Of Packet (EOP)
1010 */
1011 current_tx_desc->cmd_type_len |= (IXGB_TX_DESC_CMD_EOP);
1012
1013 /*
1014 * Advance the Transmit Descriptor Tail (Tdt), this tells the E1000
1015 * that this frame is available to transmit.
1016 */
1017 IXGB_WRITE_REG(&adapter->hw, TDT, i);
1018
1019 return (0);
1020}
1021
1022static void
1023ixgb_set_promisc(struct adapter * adapter)
1024{
1025
1026 u_int32_t reg_rctl;
1027 struct ifnet *ifp = adapter->ifp;
1028
1029 reg_rctl = IXGB_READ_REG(&adapter->hw, RCTL);
1030
1031 if (ifp->if_flags & IFF_PROMISC) {
1032 reg_rctl |= (IXGB_RCTL_UPE | IXGB_RCTL_MPE);
1033 IXGB_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1034 } else if (ifp->if_flags & IFF_ALLMULTI) {
1035 reg_rctl |= IXGB_RCTL_MPE;
1036 reg_rctl &= ~IXGB_RCTL_UPE;
1037 IXGB_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1038 }
1039 return;
1040}
1041
1042static void
1043ixgb_disable_promisc(struct adapter * adapter)
1044{
1045 u_int32_t reg_rctl;
1046
1047 reg_rctl = IXGB_READ_REG(&adapter->hw, RCTL);
1048
1049 reg_rctl &= (~IXGB_RCTL_UPE);
1050 reg_rctl &= (~IXGB_RCTL_MPE);
1051 IXGB_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1052
1053 return;
1054}
1055
1056
1057/*********************************************************************
1058 * Multicast Update
1059 *
1060 * This routine is called whenever multicast address list is updated.
1061 *
1062 **********************************************************************/
1063
1064static void
1065ixgb_set_multi(struct adapter * adapter)
1066{
1067 u_int32_t reg_rctl = 0;
1068 u_int8_t mta[MAX_NUM_MULTICAST_ADDRESSES * IXGB_ETH_LENGTH_OF_ADDRESS];
1069 struct ifmultiaddr *ifma;
1070 int mcnt = 0;
1071 struct ifnet *ifp = adapter->ifp;
1072
1073 IOCTL_DEBUGOUT("ixgb_set_multi: begin");
1074
1075 IF_ADDR_LOCK(ifp);
1076#if __FreeBSD_version < 500000
1077 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1078#else
1079 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1080#endif
1081 if (ifma->ifma_addr->sa_family != AF_LINK)
1082 continue;
1083
1084 bcopy(LLADDR((struct sockaddr_dl *) ifma->ifma_addr),
1085 &mta[mcnt * IXGB_ETH_LENGTH_OF_ADDRESS], IXGB_ETH_LENGTH_OF_ADDRESS);
1086 mcnt++;
1087 }
1088 IF_ADDR_UNLOCK(ifp);
1089
1090 if (mcnt > MAX_NUM_MULTICAST_ADDRESSES) {
1091 reg_rctl = IXGB_READ_REG(&adapter->hw, RCTL);
1092 reg_rctl |= IXGB_RCTL_MPE;
1093 IXGB_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
1094 } else
1095 ixgb_mc_addr_list_update(&adapter->hw, mta, mcnt, 0);
1096
1097 return;
1098}
1099
1100
1101/*********************************************************************
1102 * Timer routine
1103 *
1104 * This routine checks for link status and updates statistics.
1105 *
1106 **********************************************************************/
1107
1108static void
1109ixgb_local_timer(void *arg)
1110{
1111 struct ifnet *ifp;
1112 struct adapter *adapter = arg;
1113 ifp = adapter->ifp;
1114
1115 IXGB_LOCK(adapter);
1116
1117 ixgb_check_for_link(&adapter->hw);
1118 ixgb_print_link_status(adapter);
1119 ixgb_update_stats_counters(adapter);
1120 if (ixgb_display_debug_stats && ifp->if_drv_flags & IFF_DRV_RUNNING) {
1121 ixgb_print_hw_stats(adapter);
1122 }
1123 callout_reset(&adapter->timer, 2 * hz, ixgb_local_timer, adapter);
1124
1125 IXGB_UNLOCK(adapter);
1126 return;
1127}
1128
1129static void
1130ixgb_print_link_status(struct adapter * adapter)
1131{
1132 if (adapter->hw.link_up) {
1133 if (!adapter->link_active) {
1134 printf("ixgb%d: Link is up %d Mbps %s \n",
1135 adapter->unit,
1136 10000,
1137 "Full Duplex");
1138 adapter->link_active = 1;
1139 }
1140 } else {
1141 if (adapter->link_active) {
1142 printf("ixgb%d: Link is Down \n", adapter->unit);
1143 adapter->link_active = 0;
1144 }
1145 }
1146
1147 return;
1148}
1149
1150
1151
1152/*********************************************************************
1153 *
1154 * This routine disables all traffic on the adapter by issuing a
1155 * global reset on the MAC and deallocates TX/RX buffers.
1156 *
1157 **********************************************************************/
1158
1159static void
1160ixgb_stop(void *arg)
1161{
1162 struct ifnet *ifp;
1163 struct adapter *adapter = arg;
1164 ifp = adapter->ifp;
1165
1166 IXGB_LOCK_ASSERT(adapter);
1167
1168 INIT_DEBUGOUT("ixgb_stop: begin\n");
1169 ixgb_disable_intr(adapter);
1170 adapter->hw.adapter_stopped = FALSE;
1171 ixgb_adapter_stop(&adapter->hw);
1172 callout_stop(&adapter->timer);
1173 ixgb_free_transmit_structures(adapter);
1174 ixgb_free_receive_structures(adapter);
1175
1176
1177 /* Tell the stack that the interface is no longer active */
1178 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1179
1180 return;
1181}
1182
1183
1184/*********************************************************************
1185 *
1186 * Determine hardware revision.
1187 *
1188 **********************************************************************/
1189static void
1190ixgb_identify_hardware(struct adapter * adapter)
1191{
1192 device_t dev = adapter->dev;
1193
1194 /* Make sure our PCI config space has the necessary stuff set */
1195 adapter->hw.pci_cmd_word = pci_read_config(dev, PCIR_COMMAND, 2);
1196 if (!((adapter->hw.pci_cmd_word & PCIM_CMD_BUSMASTEREN) &&
1197 (adapter->hw.pci_cmd_word & PCIM_CMD_MEMEN))) {
1198 printf("ixgb%d: Memory Access and/or Bus Master bits were not set!\n",
1199 adapter->unit);
1200 adapter->hw.pci_cmd_word |=
1201 (PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN);
1202 pci_write_config(dev, PCIR_COMMAND, adapter->hw.pci_cmd_word, 2);
1203 }
1204 /* Save off the information about this board */
1205 adapter->hw.vendor_id = pci_get_vendor(dev);
1206 adapter->hw.device_id = pci_get_device(dev);
1207 adapter->hw.revision_id = pci_read_config(dev, PCIR_REVID, 1);
1208 adapter->hw.subsystem_vendor_id = pci_read_config(dev, PCIR_SUBVEND_0, 2);
1209 adapter->hw.subsystem_id = pci_read_config(dev, PCIR_SUBDEV_0, 2);
1210
1211 /* Set MacType, etc. based on this PCI info */
1212 switch (adapter->hw.device_id) {
1213 case IXGB_DEVICE_ID_82597EX:
1214 case IXGB_DEVICE_ID_82597EX_SR:
1215 adapter->hw.mac_type = ixgb_82597;
1216 break;
1217 default:
1218 INIT_DEBUGOUT1("Unknown device if 0x%x", adapter->hw.device_id);
1219 printf("ixgb%d: unsupported device id 0x%x\n", adapter->unit, adapter->hw.device_id);
1220 }
1221
1222 return;
1223}
1224
1225static int
1226ixgb_allocate_pci_resources(struct adapter * adapter)
1227{
1228 int rid;
1229 device_t dev = adapter->dev;
1230
1231 rid = IXGB_MMBA;
1232 adapter->res_memory = bus_alloc_resource(dev, SYS_RES_MEMORY,
1233 &rid, 0, ~0, 1,
1234 RF_ACTIVE);
1235 if (!(adapter->res_memory)) {
1236 printf("ixgb%d: Unable to allocate bus resource: memory\n",
1237 adapter->unit);
1238 return (ENXIO);
1239 }
1240 adapter->osdep.mem_bus_space_tag =
1241 rman_get_bustag(adapter->res_memory);
1242 adapter->osdep.mem_bus_space_handle =
1243 rman_get_bushandle(adapter->res_memory);
1244 adapter->hw.hw_addr = (uint8_t *) & adapter->osdep.mem_bus_space_handle;
1245
1246 rid = 0x0;
1247 adapter->res_interrupt = bus_alloc_resource(dev, SYS_RES_IRQ,
1248 &rid, 0, ~0, 1,
1249 RF_SHAREABLE | RF_ACTIVE);
1250 if (!(adapter->res_interrupt)) {
1251 printf("ixgb%d: Unable to allocate bus resource: interrupt\n",
1252 adapter->unit);
1253 return (ENXIO);
1254 }
1255 if (bus_setup_intr(dev, adapter->res_interrupt,
1256 INTR_TYPE_NET | INTR_MPSAFE,
1257 (void (*) (void *))ixgb_intr, adapter,
1258 &adapter->int_handler_tag)) {
1259 printf("ixgb%d: Error registering interrupt handler!\n",
1260 adapter->unit);
1261 return (ENXIO);
1262 }
1263 adapter->hw.back = &adapter->osdep;
1264
1265 return (0);
1266}
1267
1268static void
1269ixgb_free_pci_resources(struct adapter * adapter)
1270{
1271 device_t dev = adapter->dev;
1272
1273 if (adapter->res_interrupt != NULL) {
1274 bus_teardown_intr(dev, adapter->res_interrupt,
1275 adapter->int_handler_tag);
1276 bus_release_resource(dev, SYS_RES_IRQ, 0,
1277 adapter->res_interrupt);
1278 }
1279 if (adapter->res_memory != NULL) {
1280 bus_release_resource(dev, SYS_RES_MEMORY, IXGB_MMBA,
1281 adapter->res_memory);
1282 }
1283 if (adapter->res_ioport != NULL) {
1284 bus_release_resource(dev, SYS_RES_IOPORT, adapter->io_rid,
1285 adapter->res_ioport);
1286 }
1287 return;
1288}
1289
1290/*********************************************************************
1291 *
1292 * Initialize the hardware to a configuration as specified by the
1293 * adapter structure. The controller is reset, the EEPROM is
1294 * verified, the MAC address is set, then the shared initialization
1295 * routines are called.
1296 *
1297 **********************************************************************/
1298static int
1299ixgb_hardware_init(struct adapter * adapter)
1300{
1301 /* Issue a global reset */
1302 adapter->hw.adapter_stopped = FALSE;
1303 ixgb_adapter_stop(&adapter->hw);
1304
1305 /* Make sure we have a good EEPROM before we read from it */
1306 if (!ixgb_validate_eeprom_checksum(&adapter->hw)) {
1307 printf("ixgb%d: The EEPROM Checksum Is Not Valid\n",
1308 adapter->unit);
1309 return (EIO);
1310 }
1311 if (!ixgb_init_hw(&adapter->hw)) {
1312 printf("ixgb%d: Hardware Initialization Failed",
1313 adapter->unit);
1314 return (EIO);
1315 }
1316
1317 return (0);
1318}
1319
1320/*********************************************************************
1321 *
1322 * Setup networking device structure and register an interface.
1323 *
1324 **********************************************************************/
1325static void
1326ixgb_setup_interface(device_t dev, struct adapter * adapter)
1327{
1328 struct ifnet *ifp;
1329 INIT_DEBUGOUT("ixgb_setup_interface: begin");
1330
1331 ifp = adapter->ifp = if_alloc(IFT_ETHER);
1332 if (ifp == NULL)
1333 panic("%s: can not if_alloc()\n", device_get_nameunit(dev));
1334#if __FreeBSD_version >= 502000
1335 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1336#else
1337 ifp->if_unit = adapter->unit;
1338 ifp->if_name = "ixgb";
1339#endif
1340 ifp->if_mtu = ETHERMTU;
1341 ifp->if_baudrate = 1000000000;
1342 ifp->if_init = ixgb_init;
1343 ifp->if_softc = adapter;
1344 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1345 ifp->if_ioctl = ixgb_ioctl;
1346 ifp->if_start = ixgb_start;
1347 ifp->if_watchdog = ixgb_watchdog;
1348 ifp->if_snd.ifq_maxlen = adapter->num_tx_desc - 1;
1349
1350#if __FreeBSD_version < 500000
1351 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
1352#else
1353 ether_ifattach(ifp, adapter->hw.curr_mac_addr);
1354#endif
1355
1356 ifp->if_capabilities = IFCAP_HWCSUM;
1357#ifdef DEVICE_POLLING
1358 ifp->if_capabilities |= IFCAP_POLLING;
1359#endif
1360
1361 /*
1362 * Tell the upper layer(s) we support long frames.
1363 */
1364 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1365
1366#if __FreeBSD_version >= 500000
1367 ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU;
1368#endif
1369
1370 ifp->if_capenable = ifp->if_capabilities;
1371
1372 /*
1373 * Specify the media types supported by this adapter and register
1374 * callbacks to update media and link information
1375 */
1376 ifmedia_init(&adapter->media, IFM_IMASK, ixgb_media_change,
1377 ixgb_media_status);
1378 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_SX | IFM_FDX,
1379 0, NULL);
1380 ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_SX,
1381 0, NULL);
1382 ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
1383 ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
1384
1385 return;
1386}
1387
1388/********************************************************************
1389 * Manage DMA'able memory.
1390 *******************************************************************/
1391static void
1392ixgb_dmamap_cb(void *arg, bus_dma_segment_t * segs, int nseg, int error)
1393{
1394 if (error)
1395 return;
1396 *(bus_addr_t *) arg = segs->ds_addr;
1397 return;
1398}
1399
1400static int
1401ixgb_dma_malloc(struct adapter * adapter, bus_size_t size,
1402 struct ixgb_dma_alloc * dma, int mapflags)
1403{
1404 int r;
1405
1406 r = bus_dma_tag_create(NULL, /* parent */
1407 PAGE_SIZE, 0, /* alignment, bounds */
1408 BUS_SPACE_MAXADDR, /* lowaddr */
1409 BUS_SPACE_MAXADDR, /* highaddr */
1410 NULL, NULL, /* filter, filterarg */
1411 size, /* maxsize */
1412 1, /* nsegments */
1413 size, /* maxsegsize */
1414 BUS_DMA_ALLOCNOW, /* flags */
1415#if __FreeBSD_version >= 502000
1416 NULL, /* lockfunc */
1417 NULL, /* lockfuncarg */
1418#endif
1419 &dma->dma_tag);
1420 if (r != 0) {
1421 printf("ixgb%d: ixgb_dma_malloc: bus_dma_tag_create failed; "
1422 "error %u\n", adapter->unit, r);
1423 goto fail_0;
1424 }
1425 r = bus_dmamem_alloc(dma->dma_tag, (void **)&dma->dma_vaddr,
1426 BUS_DMA_NOWAIT, &dma->dma_map);
1427 if (r != 0) {
1428 printf("ixgb%d: ixgb_dma_malloc: bus_dmamem_alloc failed; "
1429 "error %u\n", adapter->unit, r);
1430 goto fail_1;
1431 }
1432 r = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
1433 size,
1434 ixgb_dmamap_cb,
1435 &dma->dma_paddr,
1436 mapflags | BUS_DMA_NOWAIT);
1437 if (r != 0) {
1438 printf("ixgb%d: ixgb_dma_malloc: bus_dmamap_load failed; "
1439 "error %u\n", adapter->unit, r);
1440 goto fail_2;
1441 }
1442 dma->dma_size = size;
1443 return (0);
1444fail_2:
1445 bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
1446fail_1:
1447 bus_dma_tag_destroy(dma->dma_tag);
1448fail_0:
1449 dma->dma_map = NULL;
1450 dma->dma_tag = NULL;
1451 return (r);
1452}
1453
1454
1455
1456static void
1457ixgb_dma_free(struct adapter * adapter, struct ixgb_dma_alloc * dma)
1458{
1459 bus_dmamap_unload(dma->dma_tag, dma->dma_map);
1460 bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
1461 bus_dma_tag_destroy(dma->dma_tag);
1462}
1463
1464/*********************************************************************
1465 *
1466 * Allocate memory for tx_buffer structures. The tx_buffer stores all
1467 * the information needed to transmit a packet on the wire.
1468 *
1469 **********************************************************************/
1470static int
1471ixgb_allocate_transmit_structures(struct adapter * adapter)
1472{
1473 if (!(adapter->tx_buffer_area =
1474 (struct ixgb_buffer *) malloc(sizeof(struct ixgb_buffer) *
1475 adapter->num_tx_desc, M_DEVBUF,
1476 M_NOWAIT | M_ZERO))) {
1477 printf("ixgb%d: Unable to allocate tx_buffer memory\n",
1478 adapter->unit);
1479 return ENOMEM;
1480 }
1481 bzero(adapter->tx_buffer_area,
1482 sizeof(struct ixgb_buffer) * adapter->num_tx_desc);
1483
1484 return 0;
1485}
1486
1487/*********************************************************************
1488 *
1489 * Allocate and initialize transmit structures.
1490 *
1491 **********************************************************************/
1492static int
1493ixgb_setup_transmit_structures(struct adapter * adapter)
1494{
1495 /*
1496 * Setup DMA descriptor areas.
1497 */
1498 if (bus_dma_tag_create(NULL, /* parent */
1499 PAGE_SIZE, 0, /* alignment, bounds */
1500 BUS_SPACE_MAXADDR, /* lowaddr */
1501 BUS_SPACE_MAXADDR, /* highaddr */
1502 NULL, NULL, /* filter, filterarg */
1503 MCLBYTES * IXGB_MAX_SCATTER, /* maxsize */
1504 IXGB_MAX_SCATTER, /* nsegments */
1505 MCLBYTES, /* maxsegsize */
1506 BUS_DMA_ALLOCNOW, /* flags */
1507#if __FreeBSD_version >= 502000
1508 NULL, /* lockfunc */
1509 NULL, /* lockfuncarg */
1510#endif
1511 &adapter->txtag)) {
1512 printf("ixgb%d: Unable to allocate TX DMA tag\n", adapter->unit);
1513 return (ENOMEM);
1514 }
1515 if (ixgb_allocate_transmit_structures(adapter))
1516 return ENOMEM;
1517
1518 bzero((void *)adapter->tx_desc_base,
1519 (sizeof(struct ixgb_tx_desc)) * adapter->num_tx_desc);
1520
1521 adapter->next_avail_tx_desc = 0;
1522 adapter->oldest_used_tx_desc = 0;
1523
1524 /* Set number of descriptors available */
1525 adapter->num_tx_desc_avail = adapter->num_tx_desc;
1526
1527 /* Set checksum context */
1528 adapter->active_checksum_context = OFFLOAD_NONE;
1529
1530 return 0;
1531}
1532
1533/*********************************************************************
1534 *
1535 * Enable transmit unit.
1536 *
1537 **********************************************************************/
1538static void
1539ixgb_initialize_transmit_unit(struct adapter * adapter)
1540{
1541 u_int32_t reg_tctl;
1542 u_int64_t tdba = adapter->txdma.dma_paddr;
1543
1544 /* Setup the Base and Length of the Tx Descriptor Ring */
1545 IXGB_WRITE_REG(&adapter->hw, TDBAL,
1546 (tdba & 0x00000000ffffffffULL));
1547 IXGB_WRITE_REG(&adapter->hw, TDBAH, (tdba >> 32));
1548 IXGB_WRITE_REG(&adapter->hw, TDLEN,
1549 adapter->num_tx_desc *
1550 sizeof(struct ixgb_tx_desc));
1551
1552 /* Setup the HW Tx Head and Tail descriptor pointers */
1553 IXGB_WRITE_REG(&adapter->hw, TDH, 0);
1554 IXGB_WRITE_REG(&adapter->hw, TDT, 0);
1555
1556
1557 HW_DEBUGOUT2("Base = %x, Length = %x\n",
1558 IXGB_READ_REG(&adapter->hw, TDBAL),
1559 IXGB_READ_REG(&adapter->hw, TDLEN));
1560
1561 IXGB_WRITE_REG(&adapter->hw, TIDV, adapter->tx_int_delay);
1562
1563
1564 /* Program the Transmit Control Register */
1565 reg_tctl = IXGB_READ_REG(&adapter->hw, TCTL);
1566 reg_tctl = IXGB_TCTL_TCE | IXGB_TCTL_TXEN | IXGB_TCTL_TPDE;
1567 IXGB_WRITE_REG(&adapter->hw, TCTL, reg_tctl);
1568
1569 /* Setup Transmit Descriptor Settings for this adapter */
1570 adapter->txd_cmd = IXGB_TX_DESC_TYPE | IXGB_TX_DESC_CMD_RS;
1571
1572 if (adapter->tx_int_delay > 0)
1573 adapter->txd_cmd |= IXGB_TX_DESC_CMD_IDE;
1574 return;
1575}
1576
1577/*********************************************************************
1578 *
1579 * Free all transmit related data structures.
1580 *
1581 **********************************************************************/
1582static void
1583ixgb_free_transmit_structures(struct adapter * adapter)
1584{
1585 struct ixgb_buffer *tx_buffer;
1586 int i;
1587
1588 INIT_DEBUGOUT("free_transmit_structures: begin");
1589
1590 if (adapter->tx_buffer_area != NULL) {
1591 tx_buffer = adapter->tx_buffer_area;
1592 for (i = 0; i < adapter->num_tx_desc; i++, tx_buffer++) {
1593 if (tx_buffer->m_head != NULL) {
1594 bus_dmamap_unload(adapter->txtag, tx_buffer->map);
1595 bus_dmamap_destroy(adapter->txtag, tx_buffer->map);
1596 m_freem(tx_buffer->m_head);
1597 }
1598 tx_buffer->m_head = NULL;
1599 }
1600 }
1601 if (adapter->tx_buffer_area != NULL) {
1602 free(adapter->tx_buffer_area, M_DEVBUF);
1603 adapter->tx_buffer_area = NULL;
1604 }
1605 if (adapter->txtag != NULL) {
1606 bus_dma_tag_destroy(adapter->txtag);
1607 adapter->txtag = NULL;
1608 }
1609 return;
1610}
1611
1612/*********************************************************************
1613 *
1614 * The offload context needs to be set when we transfer the first
1615 * packet of a particular protocol (TCP/UDP). We change the
1616 * context only if the protocol type changes.
1617 *
1618 **********************************************************************/
1619static void
1620ixgb_transmit_checksum_setup(struct adapter * adapter,
1621 struct mbuf * mp,
1622 u_int8_t * txd_popts)
1623{
1624 struct ixgb_context_desc *TXD;
1625 struct ixgb_buffer *tx_buffer;
1626 int curr_txd;
1627
1628 if (mp->m_pkthdr.csum_flags) {
1629
1630 if (mp->m_pkthdr.csum_flags & CSUM_TCP) {
1631 *txd_popts = IXGB_TX_DESC_POPTS_TXSM;
1632 if (adapter->active_checksum_context == OFFLOAD_TCP_IP)
1633 return;
1634 else
1635 adapter->active_checksum_context = OFFLOAD_TCP_IP;
1636 } else if (mp->m_pkthdr.csum_flags & CSUM_UDP) {
1637 *txd_popts = IXGB_TX_DESC_POPTS_TXSM;
1638 if (adapter->active_checksum_context == OFFLOAD_UDP_IP)
1639 return;
1640 else
1641 adapter->active_checksum_context = OFFLOAD_UDP_IP;
1642 } else {
1643 *txd_popts = 0;
1644 return;
1645 }
1646 } else {
1647 *txd_popts = 0;
1648 return;
1649 }
1650
1651 /*
1652 * If we reach this point, the checksum offload context needs to be
1653 * reset.
1654 */
1655 curr_txd = adapter->next_avail_tx_desc;
1656 tx_buffer = &adapter->tx_buffer_area[curr_txd];
1657 TXD = (struct ixgb_context_desc *) & adapter->tx_desc_base[curr_txd];
1658
1659
1660 TXD->tucss = ENET_HEADER_SIZE + sizeof(struct ip);
1661 TXD->tucse = 0;
1662
1663 TXD->mss = 0;
1664
1665 if (adapter->active_checksum_context == OFFLOAD_TCP_IP) {
1666 TXD->tucso =
1667 ENET_HEADER_SIZE + sizeof(struct ip) +
1668 offsetof(struct tcphdr, th_sum);
1669 } else if (adapter->active_checksum_context == OFFLOAD_UDP_IP) {
1670 TXD->tucso =
1671 ENET_HEADER_SIZE + sizeof(struct ip) +
1672 offsetof(struct udphdr, uh_sum);
1673 }
1674 TXD->cmd_type_len = IXGB_CONTEXT_DESC_CMD_TCP | IXGB_TX_DESC_CMD_RS | IXGB_CONTEXT_DESC_CMD_IDE;
1675
1676 tx_buffer->m_head = NULL;
1677
1678 if (++curr_txd == adapter->num_tx_desc)
1679 curr_txd = 0;
1680
1681 adapter->num_tx_desc_avail--;
1682 adapter->next_avail_tx_desc = curr_txd;
1683 return;
1684}
1685
1686/**********************************************************************
1687 *
1688 * Examine each tx_buffer in the used queue. If the hardware is done
1689 * processing the packet then free associated resources. The
1690 * tx_buffer is put back on the free queue.
1691 *
1692 **********************************************************************/
1693static void
1694ixgb_clean_transmit_interrupts(struct adapter * adapter)
1695{
1696 int i, num_avail;
1697 struct ixgb_buffer *tx_buffer;
1698 struct ixgb_tx_desc *tx_desc;
1699
1700 IXGB_LOCK_ASSERT(adapter);
1701
1702 if (adapter->num_tx_desc_avail == adapter->num_tx_desc)
1703 return;
1704
1705#ifdef _SV_
1706 adapter->clean_tx_interrupts++;
1707#endif
1708 num_avail = adapter->num_tx_desc_avail;
1709 i = adapter->oldest_used_tx_desc;
1710
1711 tx_buffer = &adapter->tx_buffer_area[i];
1712 tx_desc = &adapter->tx_desc_base[i];
1713
1714 while (tx_desc->status & IXGB_TX_DESC_STATUS_DD) {
1715
1716 tx_desc->status = 0;
1717 num_avail++;
1718
1719 if (tx_buffer->m_head) {
1720 bus_dmamap_sync(adapter->txtag, tx_buffer->map,
1721 BUS_DMASYNC_POSTWRITE);
1722 bus_dmamap_unload(adapter->txtag, tx_buffer->map);
1723 bus_dmamap_destroy(adapter->txtag, tx_buffer->map);
1724 m_freem(tx_buffer->m_head);
1725 tx_buffer->m_head = NULL;
1726 }
1727 if (++i == adapter->num_tx_desc)
1728 i = 0;
1729
1730 tx_buffer = &adapter->tx_buffer_area[i];
1731 tx_desc = &adapter->tx_desc_base[i];
1732 }
1733
1734 adapter->oldest_used_tx_desc = i;
1735
1736 /*
1737 * If we have enough room, clear IFF_DRV_OACTIVE to tell the stack that
1738 * it is OK to send packets. If there are no pending descriptors,
1739 * clear the timeout. Otherwise, if some descriptors have been freed,
1740 * restart the timeout.
1741 */
1742 if (num_avail > IXGB_TX_CLEANUP_THRESHOLD) {
1743 struct ifnet *ifp = adapter->ifp;
1744
1745 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1746 if (num_avail == adapter->num_tx_desc)
1747 ifp->if_timer = 0;
1748 else if (num_avail == adapter->num_tx_desc_avail)
1749 ifp->if_timer = IXGB_TX_TIMEOUT;
1750 }
1751 adapter->num_tx_desc_avail = num_avail;
1752 return;
1753}
1754
1755
1756/*********************************************************************
1757 *
1758 * Get a buffer from system mbuf buffer pool.
1759 *
1760 **********************************************************************/
1761static int
1762ixgb_get_buf(int i, struct adapter * adapter,
1763 struct mbuf * nmp)
1764{
1765 register struct mbuf *mp = nmp;
1766 struct ixgb_buffer *rx_buffer;
1767 struct ifnet *ifp;
1768 bus_addr_t paddr;
1769 int error;
1770
1771 ifp = adapter->ifp;
1772
1773 if (mp == NULL) {
1774
1775 mp = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1776
1777 if (mp == NULL) {
1778 adapter->mbuf_alloc_failed++;
1779 return (ENOBUFS);
1780 }
1781 mp->m_len = mp->m_pkthdr.len = MCLBYTES;
1782 } else {
1783 mp->m_len = mp->m_pkthdr.len = MCLBYTES;
1784 mp->m_data = mp->m_ext.ext_buf;
1785 mp->m_next = NULL;
1786 }
1787
1788 if (ifp->if_mtu <= ETHERMTU) {
1789 m_adj(mp, ETHER_ALIGN);
1790 }
1791 rx_buffer = &adapter->rx_buffer_area[i];
1792
1793 /*
1794 * Using memory from the mbuf cluster pool, invoke the bus_dma
1795 * machinery to arrange the memory mapping.
1796 */
1797 error = bus_dmamap_load(adapter->rxtag, rx_buffer->map,
1798 mtod(mp, void *), mp->m_len,
1799 ixgb_dmamap_cb, &paddr, 0);
1800 if (error) {
1801 m_free(mp);
1802 return (error);
1803 }
1804 rx_buffer->m_head = mp;
1805 adapter->rx_desc_base[i].buff_addr = htole64(paddr);
1806 bus_dmamap_sync(adapter->rxtag, rx_buffer->map, BUS_DMASYNC_PREREAD);
1807
1808 return (0);
1809}
1810
1811/*********************************************************************
1812 *
1813 * Allocate memory for rx_buffer structures. Since we use one
1814 * rx_buffer per received packet, the maximum number of rx_buffer's
1815 * that we'll need is equal to the number of receive descriptors
1816 * that we've allocated.
1817 *
1818 **********************************************************************/
1819static int
1820ixgb_allocate_receive_structures(struct adapter * adapter)
1821{
1822 int i, error;
1823 struct ixgb_buffer *rx_buffer;
1824
1825 if (!(adapter->rx_buffer_area =
1826 (struct ixgb_buffer *) malloc(sizeof(struct ixgb_buffer) *
1827 adapter->num_rx_desc, M_DEVBUF,
1828 M_NOWAIT | M_ZERO))) {
1829 printf("ixgb%d: Unable to allocate rx_buffer memory\n",
1830 adapter->unit);
1831 return (ENOMEM);
1832 }
1833 bzero(adapter->rx_buffer_area,
1834 sizeof(struct ixgb_buffer) * adapter->num_rx_desc);
1835
1836 error = bus_dma_tag_create(NULL, /* parent */
1837 PAGE_SIZE, 0, /* alignment, bounds */
1838 BUS_SPACE_MAXADDR, /* lowaddr */
1839 BUS_SPACE_MAXADDR, /* highaddr */
1840 NULL, NULL, /* filter, filterarg */
1841 MCLBYTES, /* maxsize */
1842 1, /* nsegments */
1843 MCLBYTES, /* maxsegsize */
1844 BUS_DMA_ALLOCNOW, /* flags */
1845#if __FreeBSD_version >= 502000
1846 NULL, /* lockfunc */
1847 NULL, /* lockfuncarg */
1848#endif
1849 &adapter->rxtag);
1850 if (error != 0) {
1851 printf("ixgb%d: ixgb_allocate_receive_structures: "
1852 "bus_dma_tag_create failed; error %u\n",
1853 adapter->unit, error);
1854 goto fail_0;
1855 }
1856 rx_buffer = adapter->rx_buffer_area;
1857 for (i = 0; i < adapter->num_rx_desc; i++, rx_buffer++) {
1858 error = bus_dmamap_create(adapter->rxtag, BUS_DMA_NOWAIT,
1859 &rx_buffer->map);
1860 if (error != 0) {
1861 printf("ixgb%d: ixgb_allocate_receive_structures: "
1862 "bus_dmamap_create failed; error %u\n",
1863 adapter->unit, error);
1864 goto fail_1;
1865 }
1866 }
1867
1868 for (i = 0; i < adapter->num_rx_desc; i++) {
1869 if (ixgb_get_buf(i, adapter, NULL) == ENOBUFS) {
1870 adapter->rx_buffer_area[i].m_head = NULL;
1871 adapter->rx_desc_base[i].buff_addr = 0;
1872 return (ENOBUFS);
1873 }
1874 }
1875
1876 return (0);
1877fail_1:
1878 bus_dma_tag_destroy(adapter->rxtag);
1879fail_0:
1880 adapter->rxtag = NULL;
1881 free(adapter->rx_buffer_area, M_DEVBUF);
1882 adapter->rx_buffer_area = NULL;
1883 return (error);
1884}
1885
1886/*********************************************************************
1887 *
1888 * Allocate and initialize receive structures.
1889 *
1890 **********************************************************************/
1891static int
1892ixgb_setup_receive_structures(struct adapter * adapter)
1893{
1894 bzero((void *)adapter->rx_desc_base,
1895 (sizeof(struct ixgb_rx_desc)) * adapter->num_rx_desc);
1896
1897 if (ixgb_allocate_receive_structures(adapter))
1898 return ENOMEM;
1899
1900 /* Setup our descriptor pointers */
1901 adapter->next_rx_desc_to_check = 0;
1902 adapter->next_rx_desc_to_use = 0;
1903 return (0);
1904}
1905
1906/*********************************************************************
1907 *
1908 * Enable receive unit.
1909 *
1910 **********************************************************************/
1911static void
1912ixgb_initialize_receive_unit(struct adapter * adapter)
1913{
1914 u_int32_t reg_rctl;
1915 u_int32_t reg_rxcsum;
1916 u_int32_t reg_rxdctl;
1917 struct ifnet *ifp;
1918 u_int64_t rdba = adapter->rxdma.dma_paddr;
1919
1920 ifp = adapter->ifp;
1921
1922 /*
1923 * Make sure receives are disabled while setting up the descriptor
1924 * ring
1925 */
1926 reg_rctl = IXGB_READ_REG(&adapter->hw, RCTL);
1927 IXGB_WRITE_REG(&adapter->hw, RCTL, reg_rctl & ~IXGB_RCTL_RXEN);
1928
1929 /* Set the Receive Delay Timer Register */
1930 IXGB_WRITE_REG(&adapter->hw, RDTR,
1931 adapter->rx_int_delay);
1932
1933
1934 /* Setup the Base and Length of the Rx Descriptor Ring */
1935 IXGB_WRITE_REG(&adapter->hw, RDBAL,
1936 (rdba & 0x00000000ffffffffULL));
1937 IXGB_WRITE_REG(&adapter->hw, RDBAH, (rdba >> 32));
1938 IXGB_WRITE_REG(&adapter->hw, RDLEN, adapter->num_rx_desc *
1939 sizeof(struct ixgb_rx_desc));
1940
1941 /* Setup the HW Rx Head and Tail Descriptor Pointers */
1942 IXGB_WRITE_REG(&adapter->hw, RDH, 0);
1943
1944 IXGB_WRITE_REG(&adapter->hw, RDT, adapter->num_rx_desc - 1);
1945
1946
1947
1948 reg_rxdctl = RXDCTL_WTHRESH_DEFAULT << IXGB_RXDCTL_WTHRESH_SHIFT
1949 | RXDCTL_HTHRESH_DEFAULT << IXGB_RXDCTL_HTHRESH_SHIFT
1950 | RXDCTL_PTHRESH_DEFAULT << IXGB_RXDCTL_PTHRESH_SHIFT;
1951 IXGB_WRITE_REG(&adapter->hw, RXDCTL, reg_rxdctl);
1952
1953
1954 adapter->raidc = 1;
1955 if (adapter->raidc) {
1956 uint32_t raidc;
1957 uint8_t poll_threshold;
1958#define IXGB_RAIDC_POLL_DEFAULT 120
1959
1960 poll_threshold = ((adapter->num_rx_desc - 1) >> 3);
1961 poll_threshold >>= 1;
1962 poll_threshold &= 0x3F;
1963 raidc = IXGB_RAIDC_EN | IXGB_RAIDC_RXT_GATE |
1964 (IXGB_RAIDC_POLL_DEFAULT << IXGB_RAIDC_POLL_SHIFT) |
1965 (adapter->rx_int_delay << IXGB_RAIDC_DELAY_SHIFT) |
1966 poll_threshold;
1967 IXGB_WRITE_REG(&adapter->hw, RAIDC, raidc);
1968 }
1969 /* Enable Receive Checksum Offload for TCP and UDP ? */
1970 if (ifp->if_capenable & IFCAP_RXCSUM) {
1971 reg_rxcsum = IXGB_READ_REG(&adapter->hw, RXCSUM);
1972 reg_rxcsum |= IXGB_RXCSUM_TUOFL;
1973 IXGB_WRITE_REG(&adapter->hw, RXCSUM, reg_rxcsum);
1974 }
1975 /* Setup the Receive Control Register */
1976 reg_rctl = IXGB_READ_REG(&adapter->hw, RCTL);
1977 reg_rctl &= ~(3 << IXGB_RCTL_MO_SHIFT);
1978 reg_rctl |= IXGB_RCTL_BAM | IXGB_RCTL_RDMTS_1_2 | IXGB_RCTL_SECRC |
1979 IXGB_RCTL_CFF |
1980 (adapter->hw.mc_filter_type << IXGB_RCTL_MO_SHIFT);
1981
1982 switch (adapter->rx_buffer_len) {
1983 default:
1984 case IXGB_RXBUFFER_2048:
1985 reg_rctl |= IXGB_RCTL_BSIZE_2048;
1986 break;
1987 case IXGB_RXBUFFER_4096:
1988 reg_rctl |= IXGB_RCTL_BSIZE_4096;
1989 break;
1990 case IXGB_RXBUFFER_8192:
1991 reg_rctl |= IXGB_RCTL_BSIZE_8192;
1992 break;
1993 case IXGB_RXBUFFER_16384:
1994 reg_rctl |= IXGB_RCTL_BSIZE_16384;
1995 break;
1996 }
1997
1998 reg_rctl |= IXGB_RCTL_RXEN;
1999
2000
2001 /* Enable Receives */
2002 IXGB_WRITE_REG(&adapter->hw, RCTL, reg_rctl);
2003
2004 return;
2005}
2006
2007/*********************************************************************
2008 *
2009 * Free receive related data structures.
2010 *
2011 **********************************************************************/
2012static void
2013ixgb_free_receive_structures(struct adapter * adapter)
2014{
2015 struct ixgb_buffer *rx_buffer;
2016 int i;
2017
2018 INIT_DEBUGOUT("free_receive_structures: begin");
2019
2020 if (adapter->rx_buffer_area != NULL) {
2021 rx_buffer = adapter->rx_buffer_area;
2022 for (i = 0; i < adapter->num_rx_desc; i++, rx_buffer++) {
2023 if (rx_buffer->map != NULL) {
2024 bus_dmamap_unload(adapter->rxtag, rx_buffer->map);
2025 bus_dmamap_destroy(adapter->rxtag, rx_buffer->map);
2026 }
2027 if (rx_buffer->m_head != NULL)
2028 m_freem(rx_buffer->m_head);
2029 rx_buffer->m_head = NULL;
2030 }
2031 }
2032 if (adapter->rx_buffer_area != NULL) {
2033 free(adapter->rx_buffer_area, M_DEVBUF);
2034 adapter->rx_buffer_area = NULL;
2035 }
2036 if (adapter->rxtag != NULL) {
2037 bus_dma_tag_destroy(adapter->rxtag);
2038 adapter->rxtag = NULL;
2039 }
2040 return;
2041}
2042
2043/*********************************************************************
2044 *
2045 * This routine executes in interrupt context. It replenishes
2046 * the mbufs in the descriptor and sends data which has been
2047 * dma'ed into host memory to upper layer.
2048 *
2049 * We loop at most count times if count is > 0, or until done if
2050 * count < 0.
2051 *
2052 *********************************************************************/
2053static void
2054ixgb_process_receive_interrupts(struct adapter * adapter, int count)
2055{
2056 struct ifnet *ifp;
2057 struct mbuf *mp;
2058#if __FreeBSD_version < 500000
2059 struct ether_header *eh;
2060#endif
2061 int eop = 0;
2062 int len;
2063 u_int8_t accept_frame = 0;
2064 int i;
2065 int next_to_use = 0;
2066 int eop_desc;
2067 /* Pointer to the receive descriptor being examined. */
2068 struct ixgb_rx_desc *current_desc;
2069
2070 IXGB_LOCK_ASSERT(adapter);
2071
2072 ifp = adapter->ifp;
2073 i = adapter->next_rx_desc_to_check;
2074 next_to_use = adapter->next_rx_desc_to_use;
2075 eop_desc = adapter->next_rx_desc_to_check;
2076 current_desc = &adapter->rx_desc_base[i];
2077
2078 if (!((current_desc->status) & IXGB_RX_DESC_STATUS_DD)) {
2079#ifdef _SV_
2080 adapter->no_pkts_avail++;
2081#endif
2082 return;
2083 }
2084 while ((current_desc->status & IXGB_RX_DESC_STATUS_DD) && (count != 0)) {
2085
2086 mp = adapter->rx_buffer_area[i].m_head;
2087 bus_dmamap_sync(adapter->rxtag, adapter->rx_buffer_area[i].map,
2088 BUS_DMASYNC_POSTREAD);
2089 accept_frame = 1;
2090 if (current_desc->status & IXGB_RX_DESC_STATUS_EOP) {
2091 count--;
2092 eop = 1;
2093 } else {
2094 eop = 0;
2095 }
2096 len = current_desc->length;
2097
2098 if (current_desc->errors & (IXGB_RX_DESC_ERRORS_CE |
2099 IXGB_RX_DESC_ERRORS_SE | IXGB_RX_DESC_ERRORS_P |
2100 IXGB_RX_DESC_ERRORS_RXE)) {
2101 accept_frame = 0;
2102 }
2103 if (accept_frame) {
2104
2105 /* Assign correct length to the current fragment */
2106 mp->m_len = len;
2107
2108 if (adapter->fmp == NULL) {
2109 mp->m_pkthdr.len = len;
2110 adapter->fmp = mp; /* Store the first mbuf */
2111 adapter->lmp = mp;
2112 } else {
2113 /* Chain mbuf's together */
2114 mp->m_flags &= ~M_PKTHDR;
2115 adapter->lmp->m_next = mp;
2116 adapter->lmp = adapter->lmp->m_next;
2117 adapter->fmp->m_pkthdr.len += len;
2118 }
2119
2120 if (eop) {
2121 eop_desc = i;
2122 adapter->fmp->m_pkthdr.rcvif = ifp;
2123
2124#if __FreeBSD_version < 500000
2125 eh = mtod(adapter->fmp, struct ether_header *);
2126
2127 /* Remove ethernet header from mbuf */
2128 m_adj(adapter->fmp, sizeof(struct ether_header));
2129 ixgb_receive_checksum(adapter, current_desc,
2130 adapter->fmp);
2131
2132 if (current_desc->status & IXGB_RX_DESC_STATUS_VP)
2133 VLAN_INPUT_TAG(eh, adapter->fmp,
2134 current_desc->special);
2135 else
2136 ether_input(ifp, eh, adapter->fmp);
2137#else
2138 ixgb_receive_checksum(adapter, current_desc,
2139 adapter->fmp);
2140 if (current_desc->status & IXGB_RX_DESC_STATUS_VP)
2141 VLAN_INPUT_TAG(ifp, adapter->fmp,
2142 current_desc->special,
2143 adapter->fmp = NULL);
2144
2145 if (adapter->fmp != NULL) {
2146 IXGB_UNLOCK(adapter);
2147 (*ifp->if_input) (ifp, adapter->fmp);
2148 IXGB_LOCK(adapter);
2149 }
2150#endif
2151 adapter->fmp = NULL;
2152 adapter->lmp = NULL;
2153 }
2154 adapter->rx_buffer_area[i].m_head = NULL;
2155 } else {
2156 adapter->dropped_pkts++;
2157 if (adapter->fmp != NULL)
2158 m_freem(adapter->fmp);
2159 adapter->fmp = NULL;
2160 adapter->lmp = NULL;
2161 }
2162
2163 /* Zero out the receive descriptors status */
2164 current_desc->status = 0;
2165
2166 /* Advance our pointers to the next descriptor */
2167 if (++i == adapter->num_rx_desc) {
2168 i = 0;
2169 current_desc = adapter->rx_desc_base;
2170 } else
2171 current_desc++;
2172 }
2173 adapter->next_rx_desc_to_check = i;
2174
2175 if (--i < 0)
2176 i = (adapter->num_rx_desc - 1);
2177
2178 /*
2179 * 82597EX: Workaround for redundent write back in receive descriptor ring (causes
2180 * memory corruption). Avoid using and re-submitting the most recently received RX
2181 * descriptor back to hardware.
2182 *
2183 * if(Last written back descriptor == EOP bit set descriptor)
2184 * then avoid re-submitting the most recently received RX descriptor
2185 * back to hardware.
2186 * if(Last written back descriptor != EOP bit set descriptor)
2187 * then avoid re-submitting the most recently received RX descriptors
2188 * till last EOP bit set descriptor.
2189 */
2190 if (eop_desc != i) {
2191 if (++eop_desc == adapter->num_rx_desc)
2192 eop_desc = 0;
2193 i = eop_desc;
2194 }
2195 /* Replenish the descriptors with new mbufs till last EOP bit set descriptor */
2196 while (next_to_use != i) {
2197 current_desc = &adapter->rx_desc_base[next_to_use];
2198 if ((current_desc->errors & (IXGB_RX_DESC_ERRORS_CE |
2199 IXGB_RX_DESC_ERRORS_SE | IXGB_RX_DESC_ERRORS_P |
2200 IXGB_RX_DESC_ERRORS_RXE))) {
2201 mp = adapter->rx_buffer_area[next_to_use].m_head;
2202 ixgb_get_buf(next_to_use, adapter, mp);
2203 } else {
2204 if (ixgb_get_buf(next_to_use, adapter, NULL) == ENOBUFS)
2205 break;
2206 }
2207 /* Advance our pointers to the next descriptor */
2208 if (++next_to_use == adapter->num_rx_desc) {
2209 next_to_use = 0;
2210 current_desc = adapter->rx_desc_base;
2211 } else
2212 current_desc++;
2213 }
2214 adapter->next_rx_desc_to_use = next_to_use;
2215 if (--next_to_use < 0)
2216 next_to_use = (adapter->num_rx_desc - 1);
2217 /* Advance the IXGB's Receive Queue #0 "Tail Pointer" */
2218 IXGB_WRITE_REG(&adapter->hw, RDT, next_to_use);
2219
2220 return;
2221}
2222
2223/*********************************************************************
2224 *
2225 * Verify that the hardware indicated that the checksum is valid.
2226 * Inform the stack about the status of checksum so that stack
2227 * doesn't spend time verifying the checksum.
2228 *
2229 *********************************************************************/
2230static void
2231ixgb_receive_checksum(struct adapter * adapter,
2232 struct ixgb_rx_desc * rx_desc,
2233 struct mbuf * mp)
2234{
2235 if (rx_desc->status & IXGB_RX_DESC_STATUS_IXSM) {
2236 mp->m_pkthdr.csum_flags = 0;
2237 return;
2238 }
2239 if (rx_desc->status & IXGB_RX_DESC_STATUS_IPCS) {
2240 /* Did it pass? */
2241 if (!(rx_desc->errors & IXGB_RX_DESC_ERRORS_IPE)) {
2242 /* IP Checksum Good */
2243 mp->m_pkthdr.csum_flags = CSUM_IP_CHECKED;
2244 mp->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2245
2246 } else {
2247 mp->m_pkthdr.csum_flags = 0;
2248 }
2249 }
2250 if (rx_desc->status & IXGB_RX_DESC_STATUS_TCPCS) {
2251 /* Did it pass? */
2252 if (!(rx_desc->errors & IXGB_RX_DESC_ERRORS_TCPE)) {
2253 mp->m_pkthdr.csum_flags |=
2254 (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
2255 mp->m_pkthdr.csum_data = htons(0xffff);
2256 }
2257 }
2258 return;
2259}
2260
2261
2262static void
2263ixgb_enable_vlans(struct adapter * adapter)
2264{
2265 uint32_t ctrl;
2266
2267 ctrl = IXGB_READ_REG(&adapter->hw, CTRL0);
2268 ctrl |= IXGB_CTRL0_VME;
2269 IXGB_WRITE_REG(&adapter->hw, CTRL0, ctrl);
2270
2271 return;
2272}
2273
2274
2275static void
2276ixgb_enable_intr(struct adapter * adapter)
2277{
2278 IXGB_WRITE_REG(&adapter->hw, IMS, (IXGB_INT_RXT0 | IXGB_INT_TXDW |
2279 IXGB_INT_RXDMT0 | IXGB_INT_LSC | IXGB_INT_RXO));
2280 return;
2281}
2282
2283static void
2284ixgb_disable_intr(struct adapter * adapter)
2285{
2286 IXGB_WRITE_REG(&adapter->hw, IMC, ~0);
2287 return;
2288}
2289
2290void
2291ixgb_write_pci_cfg(struct ixgb_hw * hw,
2292 uint32_t reg,
2293 uint16_t * value)
2294{
2295 pci_write_config(((struct ixgb_osdep *) hw->back)->dev, reg,
2296 *value, 2);
2297}
2298
2299/**********************************************************************
2300 *
2301 * Update the board statistics counters.
2302 *
2303 **********************************************************************/
2304static void
2305ixgb_update_stats_counters(struct adapter * adapter)
2306{
2307 struct ifnet *ifp;
2308
2309 adapter->stats.crcerrs += IXGB_READ_REG(&adapter->hw, CRCERRS);
2310 adapter->stats.gprcl += IXGB_READ_REG(&adapter->hw, GPRCL);
2311 adapter->stats.gprch += IXGB_READ_REG(&adapter->hw, GPRCH);
2312 adapter->stats.gorcl += IXGB_READ_REG(&adapter->hw, GORCL);
2313 adapter->stats.gorch += IXGB_READ_REG(&adapter->hw, GORCH);
2314 adapter->stats.bprcl += IXGB_READ_REG(&adapter->hw, BPRCL);
2315 adapter->stats.bprch += IXGB_READ_REG(&adapter->hw, BPRCH);
2316 adapter->stats.mprcl += IXGB_READ_REG(&adapter->hw, MPRCL);
2317 adapter->stats.mprch += IXGB_READ_REG(&adapter->hw, MPRCH);
2318 adapter->stats.roc += IXGB_READ_REG(&adapter->hw, ROC);
2319
2320 adapter->stats.mpc += IXGB_READ_REG(&adapter->hw, MPC);
2321 adapter->stats.dc += IXGB_READ_REG(&adapter->hw, DC);
2322 adapter->stats.rlec += IXGB_READ_REG(&adapter->hw, RLEC);
2323 adapter->stats.xonrxc += IXGB_READ_REG(&adapter->hw, XONRXC);
2324 adapter->stats.xontxc += IXGB_READ_REG(&adapter->hw, XONTXC);
2325 adapter->stats.xoffrxc += IXGB_READ_REG(&adapter->hw, XOFFRXC);
2326 adapter->stats.xofftxc += IXGB_READ_REG(&adapter->hw, XOFFTXC);
2327 adapter->stats.gptcl += IXGB_READ_REG(&adapter->hw, GPTCL);
2328 adapter->stats.gptch += IXGB_READ_REG(&adapter->hw, GPTCH);
2329 adapter->stats.gotcl += IXGB_READ_REG(&adapter->hw, GOTCL);
2330 adapter->stats.gotch += IXGB_READ_REG(&adapter->hw, GOTCH);
2331 adapter->stats.ruc += IXGB_READ_REG(&adapter->hw, RUC);
2332 adapter->stats.rfc += IXGB_READ_REG(&adapter->hw, RFC);
2333 adapter->stats.rjc += IXGB_READ_REG(&adapter->hw, RJC);
2334 adapter->stats.torl += IXGB_READ_REG(&adapter->hw, TORL);
2335 adapter->stats.torh += IXGB_READ_REG(&adapter->hw, TORH);
2336 adapter->stats.totl += IXGB_READ_REG(&adapter->hw, TOTL);
2337 adapter->stats.toth += IXGB_READ_REG(&adapter->hw, TOTH);
2338 adapter->stats.tprl += IXGB_READ_REG(&adapter->hw, TPRL);
2339 adapter->stats.tprh += IXGB_READ_REG(&adapter->hw, TPRH);
2340 adapter->stats.tptl += IXGB_READ_REG(&adapter->hw, TPTL);
2341 adapter->stats.tpth += IXGB_READ_REG(&adapter->hw, TPTH);
2342 adapter->stats.plt64c += IXGB_READ_REG(&adapter->hw, PLT64C);
2343 adapter->stats.mptcl += IXGB_READ_REG(&adapter->hw, MPTCL);
2344 adapter->stats.mptch += IXGB_READ_REG(&adapter->hw, MPTCH);
2345 adapter->stats.bptcl += IXGB_READ_REG(&adapter->hw, BPTCL);
2346 adapter->stats.bptch += IXGB_READ_REG(&adapter->hw, BPTCH);
2347
2348 adapter->stats.uprcl += IXGB_READ_REG(&adapter->hw, UPRCL);
2349 adapter->stats.uprch += IXGB_READ_REG(&adapter->hw, UPRCH);
2350 adapter->stats.vprcl += IXGB_READ_REG(&adapter->hw, VPRCL);
2351 adapter->stats.vprch += IXGB_READ_REG(&adapter->hw, VPRCH);
2352 adapter->stats.jprcl += IXGB_READ_REG(&adapter->hw, JPRCL);
2353 adapter->stats.jprch += IXGB_READ_REG(&adapter->hw, JPRCH);
2354 adapter->stats.rnbc += IXGB_READ_REG(&adapter->hw, RNBC);
2355 adapter->stats.icbc += IXGB_READ_REG(&adapter->hw, ICBC);
2356 adapter->stats.ecbc += IXGB_READ_REG(&adapter->hw, ECBC);
2357 adapter->stats.uptcl += IXGB_READ_REG(&adapter->hw, UPTCL);
2358 adapter->stats.uptch += IXGB_READ_REG(&adapter->hw, UPTCH);
2359 adapter->stats.vptcl += IXGB_READ_REG(&adapter->hw, VPTCL);
2360 adapter->stats.vptch += IXGB_READ_REG(&adapter->hw, VPTCH);
2361 adapter->stats.jptcl += IXGB_READ_REG(&adapter->hw, JPTCL);
2362 adapter->stats.jptch += IXGB_READ_REG(&adapter->hw, JPTCH);
2363 adapter->stats.tsctc += IXGB_READ_REG(&adapter->hw, TSCTC);
2364 adapter->stats.tsctfc += IXGB_READ_REG(&adapter->hw, TSCTFC);
2365 adapter->stats.ibic += IXGB_READ_REG(&adapter->hw, IBIC);
2366 adapter->stats.lfc += IXGB_READ_REG(&adapter->hw, LFC);
2367 adapter->stats.pfrc += IXGB_READ_REG(&adapter->hw, PFRC);
2368 adapter->stats.pftc += IXGB_READ_REG(&adapter->hw, PFTC);
2369 adapter->stats.mcfrc += IXGB_READ_REG(&adapter->hw, MCFRC);
2370
2371 ifp = adapter->ifp;
2372
2373 /* Fill out the OS statistics structure */
2374 ifp->if_ipackets = adapter->stats.gprcl;
2375 ifp->if_opackets = adapter->stats.gptcl;
2376 ifp->if_ibytes = adapter->stats.gorcl;
2377 ifp->if_obytes = adapter->stats.gotcl;
2378 ifp->if_imcasts = adapter->stats.mprcl;
2379 ifp->if_collisions = 0;
2380
2381 /* Rx Errors */
2382 ifp->if_ierrors =
2383 adapter->dropped_pkts +
2384 adapter->stats.crcerrs +
2385 adapter->stats.rnbc +
2386 adapter->stats.mpc +
2387 adapter->stats.rlec;
2388
2389
2390}
2391
2392
2393/**********************************************************************
2394 *
2395 * This routine is called only when ixgb_display_debug_stats is enabled.
2396 * This routine provides a way to take a look at important statistics
2397 * maintained by the driver and hardware.
2398 *
2399 **********************************************************************/
2400static void
2401ixgb_print_hw_stats(struct adapter * adapter)
2402{
2403 char buf_speed[100], buf_type[100];
2404 ixgb_bus_speed bus_speed;
2405 ixgb_bus_type bus_type;
2406 int unit = adapter->unit;
2407
2408#ifdef _SV_
2409 printf("ixgb%d: Packets not Avail = %ld\n", unit,
2410 adapter->no_pkts_avail);
2411 printf("ixgb%d: CleanTxInterrupts = %ld\n", unit,
2412 adapter->clean_tx_interrupts);
2413 printf("ixgb%d: ICR RXDMT0 = %lld\n", unit,
2414 (long long)adapter->sv_stats.icr_rxdmt0);
2415 printf("ixgb%d: ICR RXO = %lld\n", unit,
2416 (long long)adapter->sv_stats.icr_rxo);
2417 printf("ixgb%d: ICR RXT0 = %lld\n", unit,
2418 (long long)adapter->sv_stats.icr_rxt0);
2419 printf("ixgb%d: ICR TXDW = %lld\n", unit,
2420 (long long)adapter->sv_stats.icr_TXDW);
2421#endif /* _SV_ */
2422
2423 bus_speed = adapter->hw.bus.speed;
2424 bus_type = adapter->hw.bus.type;
2425 sprintf(buf_speed,
2426 bus_speed == ixgb_bus_speed_33 ? "33MHz" :
2427 bus_speed == ixgb_bus_speed_66 ? "66MHz" :
2428 bus_speed == ixgb_bus_speed_100 ? "100MHz" :
2429 bus_speed == ixgb_bus_speed_133 ? "133MHz" :
2430 "UNKNOWN");
2431 printf("ixgb%d: PCI_Bus_Speed = %s\n", unit,
2432 buf_speed);
2433
2434 sprintf(buf_type,
2435 bus_type == ixgb_bus_type_pci ? "PCI" :
2436 bus_type == ixgb_bus_type_pcix ? "PCI-X" :
2437 "UNKNOWN");
2438 printf("ixgb%d: PCI_Bus_Type = %s\n", unit,
2439 buf_type);
2440
2441 printf("ixgb%d: Tx Descriptors not Avail1 = %ld\n", unit,
2442 adapter->no_tx_desc_avail1);
2443 printf("ixgb%d: Tx Descriptors not Avail2 = %ld\n", unit,
2444 adapter->no_tx_desc_avail2);
2445 printf("ixgb%d: Std Mbuf Failed = %ld\n", unit,
2446 adapter->mbuf_alloc_failed);
2447 printf("ixgb%d: Std Cluster Failed = %ld\n", unit,
2448 adapter->mbuf_cluster_failed);
2449
2450 printf("ixgb%d: Defer count = %lld\n", unit,
2451 (long long)adapter->stats.dc);
2452 printf("ixgb%d: Missed Packets = %lld\n", unit,
2453 (long long)adapter->stats.mpc);
2454 printf("ixgb%d: Receive No Buffers = %lld\n", unit,
2455 (long long)adapter->stats.rnbc);
2456 printf("ixgb%d: Receive length errors = %lld\n", unit,
2457 (long long)adapter->stats.rlec);
2458 printf("ixgb%d: Crc errors = %lld\n", unit,
2459 (long long)adapter->stats.crcerrs);
2460 printf("ixgb%d: Driver dropped packets = %ld\n", unit,
2461 adapter->dropped_pkts);
2462
2463 printf("ixgb%d: XON Rcvd = %lld\n", unit,
2464 (long long)adapter->stats.xonrxc);
2465 printf("ixgb%d: XON Xmtd = %lld\n", unit,
2466 (long long)adapter->stats.xontxc);
2467 printf("ixgb%d: XOFF Rcvd = %lld\n", unit,
2468 (long long)adapter->stats.xoffrxc);
2469 printf("ixgb%d: XOFF Xmtd = %lld\n", unit,
2470 (long long)adapter->stats.xofftxc);
2471
2472 printf("ixgb%d: Good Packets Rcvd = %lld\n", unit,
2473 (long long)adapter->stats.gprcl);
2474 printf("ixgb%d: Good Packets Xmtd = %lld\n", unit,
2475 (long long)adapter->stats.gptcl);
2476
2477 printf("ixgb%d: Jumbo frames recvd = %lld\n", unit,
2478 (long long)adapter->stats.jprcl);
2479 printf("ixgb%d: Jumbo frames Xmtd = %lld\n", unit,
2480 (long long)adapter->stats.jptcl);
2481
2482 return;
2483
2484}
2485
2486static int
2487ixgb_sysctl_stats(SYSCTL_HANDLER_ARGS)
2488{
2489 int error;
2490 int result;
2491 struct adapter *adapter;
2492
2493 result = -1;
2494 error = sysctl_handle_int(oidp, &result, 0, req);
2495
2496 if (error || !req->newptr)
2497 return (error);
2498
2499 if (result == 1) {
2500 adapter = (struct adapter *) arg1;
2501 ixgb_print_hw_stats(adapter);
2502 }
2503 return error;
2504}