1bd0e6a36130308515ffddbfc118d730155e60a0
[dragonfly.git] / sys / dev / netif / mxge / if_mxge.c
1 /******************************************************************************
2
3 Copyright (c) 2006-2013, Myricom Inc.
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8
9  1. Redistributions of source code must retain the above copyright notice,
10     this list of conditions and the following disclaimer.
11
12  2. Neither the name of the Myricom Inc, nor the names of its
13     contributors may be used to endorse or promote products derived from
14     this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27
28 $FreeBSD: head/sys/dev/mxge/if_mxge.c 254263 2013-08-12 23:30:01Z scottl $
29
30 ***************************************************************************/
31
32 #include "opt_inet.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/linker.h>
37 #include <sys/firmware.h>
38 #include <sys/endian.h>
39 #include <sys/in_cksum.h>
40 #include <sys/sockio.h>
41 #include <sys/mbuf.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h>
44 #include <sys/module.h>
45 #include <sys/serialize.h>
46 #include <sys/socket.h>
47 #include <sys/sysctl.h>
48
49 #include <net/if.h>
50 #include <net/if_arp.h>
51 #include <net/ifq_var.h>
52 #include <net/ethernet.h>
53 #include <net/if_dl.h>
54 #include <net/if_media.h>
55
56 #include <net/bpf.h>
57
58 #include <net/if_types.h>
59 #include <net/vlan/if_vlan_var.h>
60 #include <net/zlib.h>
61
62 #include <netinet/in_systm.h>
63 #include <netinet/in.h>
64 #include <netinet/ip.h>
65 #include <netinet/tcp.h>
66
67 #include <sys/bus.h>
68 #include <sys/rman.h>
69
70 #include <bus/pci/pcireg.h>
71 #include <bus/pci/pcivar.h>
72 #include <bus/pci/pci_private.h> /* XXX for pci_cfg_restore */
73
74 #include <vm/vm.h>              /* for pmap_mapdev() */
75 #include <vm/pmap.h>
76
77 #if defined(__i386__) || defined(__x86_64__)
78 #include <machine/specialreg.h>
79 #endif
80
81 #include <dev/netif/mxge/mxge_mcp.h>
82 #include <dev/netif/mxge/mcp_gen_header.h>
83 #include <dev/netif/mxge/if_mxge_var.h>
84
85 /* tunable params */
86 static int mxge_nvidia_ecrc_enable = 1;
87 static int mxge_force_firmware = 0;
88 static int mxge_intr_coal_delay = MXGE_INTR_COAL_DELAY;
89 static int mxge_deassert_wait = 1;
90 static int mxge_flow_control = 1;
91 static int mxge_ticks;
92 static int mxge_max_slices = 1;
93 static int mxge_always_promisc = 0;
94 static int mxge_throttle = 0;
95 static int mxge_msi_enable = 1;
96
97 static const char *mxge_fw_unaligned = "mxge_ethp_z8e";
98 static const char *mxge_fw_aligned = "mxge_eth_z8e";
99 static const char *mxge_fw_rss_aligned = "mxge_rss_eth_z8e";
100 static const char *mxge_fw_rss_unaligned = "mxge_rss_ethp_z8e";
101
102 TUNABLE_INT("hw.mxge.max_slices", &mxge_max_slices);
103 TUNABLE_INT("hw.mxge.flow_control_enabled", &mxge_flow_control);
104 TUNABLE_INT("hw.mxge.intr_coal_delay", &mxge_intr_coal_delay);  
105 TUNABLE_INT("hw.mxge.nvidia_ecrc_enable", &mxge_nvidia_ecrc_enable);    
106 TUNABLE_INT("hw.mxge.force_firmware", &mxge_force_firmware);    
107 TUNABLE_INT("hw.mxge.deassert_wait", &mxge_deassert_wait);      
108 TUNABLE_INT("hw.mxge.ticks", &mxge_ticks);
109 TUNABLE_INT("hw.mxge.always_promisc", &mxge_always_promisc);
110 TUNABLE_INT("hw.mxge.throttle", &mxge_throttle);
111 TUNABLE_INT("hw.mxge.msi.enable", &mxge_msi_enable);
112
113 static int mxge_probe(device_t dev);
114 static int mxge_attach(device_t dev);
115 static int mxge_detach(device_t dev);
116 static int mxge_shutdown(device_t dev);
117 static void mxge_intr(void *arg);
118
119 static device_method_t mxge_methods[] = {
120         /* Device interface */
121         DEVMETHOD(device_probe, mxge_probe),
122         DEVMETHOD(device_attach, mxge_attach),
123         DEVMETHOD(device_detach, mxge_detach),
124         DEVMETHOD(device_shutdown, mxge_shutdown),
125         DEVMETHOD_END
126 };
127
128 static driver_t mxge_driver = {
129         "mxge",
130         mxge_methods,
131         sizeof(mxge_softc_t),
132 };
133
134 static devclass_t mxge_devclass;
135
136 /* Declare ourselves to be a child of the PCI bus.*/
137 DRIVER_MODULE(mxge, pci, mxge_driver, mxge_devclass, NULL, NULL);
138 MODULE_DEPEND(mxge, firmware, 1, 1, 1);
139 MODULE_DEPEND(mxge, zlib, 1, 1, 1);
140
141 static int mxge_load_firmware(mxge_softc_t *sc, int adopt);
142 static int mxge_send_cmd(mxge_softc_t *sc, uint32_t cmd, mxge_cmd_t *data);
143 static void mxge_close(mxge_softc_t *sc, int down);
144 static int mxge_open(mxge_softc_t *sc);
145 static void mxge_tick(void *arg);
146 static void mxge_watchdog_reset(mxge_softc_t *sc);
147 static void mxge_warn_stuck(mxge_softc_t *sc, mxge_tx_ring_t *tx, int slice);
148
149 static int
150 mxge_probe(device_t dev)
151 {
152         if (pci_get_vendor(dev) == MXGE_PCI_VENDOR_MYRICOM &&
153             (pci_get_device(dev) == MXGE_PCI_DEVICE_Z8E ||
154              pci_get_device(dev) == MXGE_PCI_DEVICE_Z8E_9)) {
155                 int rev = pci_get_revid(dev);
156
157                 switch (rev) {
158                 case MXGE_PCI_REV_Z8E:
159                         device_set_desc(dev, "Myri10G-PCIE-8A");
160                         break;
161                 case MXGE_PCI_REV_Z8ES:
162                         device_set_desc(dev, "Myri10G-PCIE-8B");
163                         break;
164                 default:
165                         device_set_desc(dev, "Myri10G-PCIE-8??");
166                         device_printf(dev, "Unrecognized rev %d NIC\n", rev);
167                         break;  
168                 }
169                 return 0;
170         }
171         return ENXIO;
172 }
173
174 static void
175 mxge_enable_wc(mxge_softc_t *sc)
176 {
177 #if defined(__i386__) || defined(__x86_64__)
178         vm_offset_t len;
179
180         sc->wc = 1;
181         len = rman_get_size(sc->mem_res);
182         pmap_change_attr((vm_offset_t) sc->sram, len / PAGE_SIZE,
183             PAT_WRITE_COMBINING);
184 #endif
185 }
186
187 static int
188 mxge_dma_alloc(mxge_softc_t *sc, bus_dmamem_t *dma, size_t bytes,
189     bus_size_t alignment)
190 {
191         bus_size_t boundary;
192         int err;
193
194         if (bytes > 4096 && alignment == 4096)
195                 boundary = 0;
196         else
197                 boundary = 4096;
198
199         err = bus_dmamem_coherent(sc->parent_dmat, alignment, boundary,
200             BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, bytes,
201             BUS_DMA_WAITOK | BUS_DMA_ZERO, dma);
202         if (err != 0) {
203                 device_printf(sc->dev, "bus_dmamem_coherent failed: %d\n", err);
204                 return err;
205         }
206         return 0;
207 }
208
209 static void
210 mxge_dma_free(bus_dmamem_t *dma)
211 {
212         bus_dmamap_unload(dma->dmem_tag, dma->dmem_map);
213         bus_dmamem_free(dma->dmem_tag, dma->dmem_addr, dma->dmem_map);
214         bus_dma_tag_destroy(dma->dmem_tag);
215 }
216
217 /*
218  * The eeprom strings on the lanaiX have the format
219  * SN=x\0
220  * MAC=x:x:x:x:x:x\0
221  * PC=text\0
222  */
223 static int
224 mxge_parse_strings(mxge_softc_t *sc)
225 {
226         const char *ptr;
227         int i, found_mac, found_sn2;
228         char *endptr;
229
230         ptr = sc->eeprom_strings;
231         found_mac = 0;
232         found_sn2 = 0;
233         while (*ptr != '\0') {
234                 if (strncmp(ptr, "MAC=", 4) == 0) {
235                         ptr += 4;
236                         for (i = 0;;) {
237                                 sc->mac_addr[i] = strtoul(ptr, &endptr, 16);
238                                 if (endptr - ptr != 2)
239                                         goto abort;
240                                 ptr = endptr;
241                                 if (++i == 6)
242                                         break;
243                                 if (*ptr++ != ':')
244                                         goto abort;
245                         }
246                         found_mac = 1;
247                 } else if (strncmp(ptr, "PC=", 3) == 0) {
248                         ptr += 3;
249                         strlcpy(sc->product_code_string, ptr,
250                             sizeof(sc->product_code_string));
251                 } else if (!found_sn2 && (strncmp(ptr, "SN=", 3) == 0)) {
252                         ptr += 3;
253                         strlcpy(sc->serial_number_string, ptr,
254                             sizeof(sc->serial_number_string));
255                 } else if (strncmp(ptr, "SN2=", 4) == 0) {
256                         /* SN2 takes precedence over SN */
257                         ptr += 4;
258                         found_sn2 = 1;
259                         strlcpy(sc->serial_number_string, ptr,
260                             sizeof(sc->serial_number_string));
261                 }
262                 while (*ptr++ != '\0') {}
263         }
264
265         if (found_mac)
266                 return 0;
267
268 abort:
269         device_printf(sc->dev, "failed to parse eeprom_strings\n");
270         return ENXIO;
271 }
272
273 #if defined(__i386__) || defined(__x86_64__)
274
275 static void
276 mxge_enable_nvidia_ecrc(mxge_softc_t *sc)
277 {
278         uint32_t val;
279         unsigned long base, off;
280         char *va, *cfgptr;
281         device_t pdev, mcp55;
282         uint16_t vendor_id, device_id, word;
283         uintptr_t bus, slot, func, ivend, idev;
284         uint32_t *ptr32;
285
286         if (!mxge_nvidia_ecrc_enable)
287                 return;
288
289         pdev = device_get_parent(device_get_parent(sc->dev));
290         if (pdev == NULL) {
291                 device_printf(sc->dev, "could not find parent?\n");
292                 return;
293         }
294         vendor_id = pci_read_config(pdev, PCIR_VENDOR, 2);
295         device_id = pci_read_config(pdev, PCIR_DEVICE, 2);
296
297         if (vendor_id != 0x10de)
298                 return;
299
300         base = 0;
301
302         if (device_id == 0x005d) {
303                 /* ck804, base address is magic */
304                 base = 0xe0000000UL;
305         } else if (device_id >= 0x0374 && device_id <= 0x378) {
306                 /* mcp55, base address stored in chipset */
307                 mcp55 = pci_find_bsf(0, 0, 0);
308                 if (mcp55 &&
309                     0x10de == pci_read_config(mcp55, PCIR_VENDOR, 2) &&
310                     0x0369 == pci_read_config(mcp55, PCIR_DEVICE, 2)) {
311                         word = pci_read_config(mcp55, 0x90, 2);
312                         base = ((unsigned long)word & 0x7ffeU) << 25;
313                 }
314         }
315         if (!base)
316                 return;
317
318         /*
319          * XXXX
320          * Test below is commented because it is believed that doing
321          * config read/write beyond 0xff will access the config space
322          * for the next larger function.  Uncomment this and remove 
323          * the hacky pmap_mapdev() way of accessing config space when
324          * FreeBSD grows support for extended pcie config space access
325          */
326 #if 0
327         /*
328          * See if we can, by some miracle, access the extended
329          * config space
330          */
331         val = pci_read_config(pdev, 0x178, 4);
332         if (val != 0xffffffff) {
333                 val |= 0x40;
334                 pci_write_config(pdev, 0x178, val, 4);
335                 return;
336         }
337 #endif
338         /*
339          * Rather than using normal pci config space writes, we must
340          * map the Nvidia config space ourselves.  This is because on
341          * opteron/nvidia class machine the 0xe000000 mapping is
342          * handled by the nvidia chipset, that means the internal PCI
343          * device (the on-chip northbridge), or the amd-8131 bridge
344          * and things behind them are not visible by this method.
345          */
346
347         BUS_READ_IVAR(device_get_parent(pdev), pdev,
348                       PCI_IVAR_BUS, &bus);
349         BUS_READ_IVAR(device_get_parent(pdev), pdev,
350                       PCI_IVAR_SLOT, &slot);
351         BUS_READ_IVAR(device_get_parent(pdev), pdev,
352                       PCI_IVAR_FUNCTION, &func);
353         BUS_READ_IVAR(device_get_parent(pdev), pdev,
354                       PCI_IVAR_VENDOR, &ivend);
355         BUS_READ_IVAR(device_get_parent(pdev), pdev,
356                       PCI_IVAR_DEVICE, &idev);
357
358         off =  base + 0x00100000UL * (unsigned long)bus +
359             0x00001000UL * (unsigned long)(func + 8 * slot);
360
361         /* map it into the kernel */
362         va = pmap_mapdev(trunc_page((vm_paddr_t)off), PAGE_SIZE);
363         if (va == NULL) {
364                 device_printf(sc->dev, "pmap_kenter_temporary didn't\n");
365                 return;
366         }
367         /* get a pointer to the config space mapped into the kernel */
368         cfgptr = va + (off & PAGE_MASK);
369
370         /* make sure that we can really access it */
371         vendor_id = *(uint16_t *)(cfgptr + PCIR_VENDOR);
372         device_id = *(uint16_t *)(cfgptr + PCIR_DEVICE);
373         if (!(vendor_id == ivend && device_id == idev)) {
374                 device_printf(sc->dev, "mapping failed: 0x%x:0x%x\n",
375                     vendor_id, device_id);
376                 pmap_unmapdev((vm_offset_t)va, PAGE_SIZE);
377                 return;
378         }
379
380         ptr32 = (uint32_t*)(cfgptr + 0x178);
381         val = *ptr32;
382
383         if (val == 0xffffffff) {
384                 device_printf(sc->dev, "extended mapping failed\n");
385                 pmap_unmapdev((vm_offset_t)va, PAGE_SIZE);
386                 return;
387         }
388         *ptr32 = val | 0x40;
389         pmap_unmapdev((vm_offset_t)va, PAGE_SIZE);
390         if (bootverbose) {
391                 device_printf(sc->dev, "Enabled ECRC on upstream "
392                     "Nvidia bridge at %d:%d:%d\n",
393                     (int)bus, (int)slot, (int)func);
394         }
395 }
396
397 #else   /* __i386__ || __x86_64__ */
398
399 static void
400 mxge_enable_nvidia_ecrc(mxge_softc_t *sc)
401 {
402         device_printf(sc->dev, "Nforce 4 chipset on non-x86/x86_64!?!?!\n");
403 }
404
405 #endif
406
407 static int
408 mxge_dma_test(mxge_softc_t *sc, int test_type)
409 {
410         mxge_cmd_t cmd;
411         bus_addr_t dmatest_bus = sc->dmabench_dma.dmem_busaddr;
412         int status;
413         uint32_t len;
414         const char *test = " ";
415
416         /*
417          * Run a small DMA test.
418          * The magic multipliers to the length tell the firmware
419          * to do DMA read, write, or read+write tests.  The
420          * results are returned in cmd.data0.  The upper 16
421          * bits of the return is the number of transfers completed.
422          * The lower 16 bits is the time in 0.5us ticks that the
423          * transfers took to complete.
424          */
425
426         len = sc->tx_boundary;
427
428         cmd.data0 = MXGE_LOWPART_TO_U32(dmatest_bus);
429         cmd.data1 = MXGE_HIGHPART_TO_U32(dmatest_bus);
430         cmd.data2 = len * 0x10000;
431         status = mxge_send_cmd(sc, test_type, &cmd);
432         if (status != 0) {
433                 test = "read";
434                 goto abort;
435         }
436         sc->read_dma = ((cmd.data0>>16) * len * 2) / (cmd.data0 & 0xffff);
437
438         cmd.data0 = MXGE_LOWPART_TO_U32(dmatest_bus);
439         cmd.data1 = MXGE_HIGHPART_TO_U32(dmatest_bus);
440         cmd.data2 = len * 0x1;
441         status = mxge_send_cmd(sc, test_type, &cmd);
442         if (status != 0) {
443                 test = "write";
444                 goto abort;
445         }
446         sc->write_dma = ((cmd.data0>>16) * len * 2) / (cmd.data0 & 0xffff);
447
448         cmd.data0 = MXGE_LOWPART_TO_U32(dmatest_bus);
449         cmd.data1 = MXGE_HIGHPART_TO_U32(dmatest_bus);
450         cmd.data2 = len * 0x10001;
451         status = mxge_send_cmd(sc, test_type, &cmd);
452         if (status != 0) {
453                 test = "read/write";
454                 goto abort;
455         }
456         sc->read_write_dma = ((cmd.data0>>16) * len * 2 * 2) /
457             (cmd.data0 & 0xffff);
458
459 abort:
460         if (status != 0 && test_type != MXGEFW_CMD_UNALIGNED_TEST) {
461                 device_printf(sc->dev, "DMA %s benchmark failed: %d\n",
462                     test, status);
463         }
464         return status;
465 }
466
467 /*
468  * The Lanai Z8E PCI-E interface achieves higher Read-DMA throughput
469  * when the PCI-E Completion packets are aligned on an 8-byte
470  * boundary.  Some PCI-E chip sets always align Completion packets; on
471  * the ones that do not, the alignment can be enforced by enabling
472  * ECRC generation (if supported).
473  *
474  * When PCI-E Completion packets are not aligned, it is actually more
475  * efficient to limit Read-DMA transactions to 2KB, rather than 4KB.
476  *
477  * If the driver can neither enable ECRC nor verify that it has
478  * already been enabled, then it must use a firmware image which works
479  * around unaligned completion packets (ethp_z8e.dat), and it should
480  * also ensure that it never gives the device a Read-DMA which is
481  * larger than 2KB by setting the tx_boundary to 2KB.  If ECRC is
482  * enabled, then the driver should use the aligned (eth_z8e.dat)
483  * firmware image, and set tx_boundary to 4KB.
484  */
485 static int
486 mxge_firmware_probe(mxge_softc_t *sc)
487 {
488         device_t dev = sc->dev;
489         int reg, status;
490         uint16_t pectl;
491
492         sc->tx_boundary = 4096;
493
494         /*
495          * Verify the max read request size was set to 4KB
496          * before trying the test with 4KB.
497          */
498         if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0) {
499                 pectl = pci_read_config(dev, reg + 0x8, 2);
500                 if ((pectl & (5 << 12)) != (5 << 12)) {
501                         device_printf(dev, "Max Read Req. size != 4k (0x%x)\n",
502                             pectl);
503                         sc->tx_boundary = 2048;
504                 }
505         }
506
507         /*
508          * Load the optimized firmware (which assumes aligned PCIe
509          * completions) in order to see if it works on this host.
510          */
511         sc->fw_name = mxge_fw_aligned;
512         status = mxge_load_firmware(sc, 1);
513         if (status != 0)
514                 return status;
515
516         /*
517          * Enable ECRC if possible
518          */
519         mxge_enable_nvidia_ecrc(sc);
520
521         /* 
522          * Run a DMA test which watches for unaligned completions and
523          * aborts on the first one seen.  Not required on Z8ES or newer.
524          */
525         if (pci_get_revid(sc->dev) >= MXGE_PCI_REV_Z8ES)
526                 return 0;
527
528         status = mxge_dma_test(sc, MXGEFW_CMD_UNALIGNED_TEST);
529         if (status == 0)
530                 return 0; /* keep the aligned firmware */
531
532         if (status != E2BIG)
533                 device_printf(dev, "DMA test failed: %d\n", status);
534         if (status == ENOSYS) {
535                 device_printf(dev, "Falling back to ethp! "
536                     "Please install up to date fw\n");
537         }
538         return status;
539 }
540
541 static int
542 mxge_select_firmware(mxge_softc_t *sc)
543 {
544         int aligned = 0;
545         int force_firmware = mxge_force_firmware;
546
547         if (sc->throttle)
548                 force_firmware = sc->throttle;
549
550         if (force_firmware != 0) {
551                 if (force_firmware == 1)
552                         aligned = 1;
553                 else
554                         aligned = 0;
555                 if (bootverbose) {
556                         device_printf(sc->dev,
557                             "Assuming %s completions (forced)\n",
558                             aligned ? "aligned" : "unaligned");
559                 }
560                 goto abort;
561         }
562
563         /*
564          * If the PCIe link width is 4 or less, we can use the aligned
565          * firmware and skip any checks
566          */
567         if (sc->link_width != 0 && sc->link_width <= 4) {
568                 device_printf(sc->dev, "PCIe x%d Link, "
569                     "expect reduced performance\n", sc->link_width);
570                 aligned = 1;
571                 goto abort;
572         }
573
574         if (mxge_firmware_probe(sc) == 0)
575                 return 0;
576
577 abort:
578         if (aligned) {
579                 sc->fw_name = mxge_fw_aligned;
580                 sc->tx_boundary = 4096;
581         } else {
582                 sc->fw_name = mxge_fw_unaligned;
583                 sc->tx_boundary = 2048;
584         }
585         return mxge_load_firmware(sc, 0);
586 }
587
588 static int
589 mxge_validate_firmware(mxge_softc_t *sc, const mcp_gen_header_t *hdr)
590 {
591         if (be32toh(hdr->mcp_type) != MCP_TYPE_ETH) {
592                 if_printf(sc->ifp, "Bad firmware type: 0x%x\n",
593                     be32toh(hdr->mcp_type));
594                 return EIO;
595         }
596
597         /* Save firmware version for sysctl */
598         strlcpy(sc->fw_version, hdr->version, sizeof(sc->fw_version));
599         if (bootverbose)
600                 if_printf(sc->ifp, "firmware id: %s\n", hdr->version);
601
602         ksscanf(sc->fw_version, "%d.%d.%d", &sc->fw_ver_major,
603             &sc->fw_ver_minor, &sc->fw_ver_tiny);
604
605         if (!(sc->fw_ver_major == MXGEFW_VERSION_MAJOR &&
606               sc->fw_ver_minor == MXGEFW_VERSION_MINOR)) {
607                 if_printf(sc->ifp, "Found firmware version %s\n",
608                     sc->fw_version);
609                 if_printf(sc->ifp, "Driver needs %d.%d\n",
610                     MXGEFW_VERSION_MAJOR, MXGEFW_VERSION_MINOR);
611                 return EINVAL;
612         }
613         return 0;
614 }
615
616 static void *
617 z_alloc(void *nil, u_int items, u_int size)
618 {
619         return kmalloc(items * size, M_TEMP, M_WAITOK);
620 }
621
622 static void
623 z_free(void *nil, void *ptr)
624 {
625         kfree(ptr, M_TEMP);
626 }
627
628 static int
629 mxge_load_firmware_helper(mxge_softc_t *sc, uint32_t *limit)
630 {
631         z_stream zs;
632         char *inflate_buffer;
633         const struct firmware *fw;
634         const mcp_gen_header_t *hdr;
635         unsigned hdr_offset;
636         int status;
637         unsigned int i;
638         char dummy;
639         size_t fw_len;
640
641         fw = firmware_get(sc->fw_name);
642         if (fw == NULL) {
643                 if_printf(sc->ifp, "Could not find firmware image %s\n",
644                     sc->fw_name);
645                 return ENOENT;
646         }
647
648         /* Setup zlib and decompress f/w */
649         bzero(&zs, sizeof(zs));
650         zs.zalloc = z_alloc;
651         zs.zfree = z_free;
652         status = inflateInit(&zs);
653         if (status != Z_OK) {
654                 status = EIO;
655                 goto abort_with_fw;
656         }
657
658         /*
659          * The uncompressed size is stored as the firmware version,
660          * which would otherwise go unused
661          */
662         fw_len = (size_t)fw->version;
663         inflate_buffer = kmalloc(fw_len, M_TEMP, M_WAITOK);
664         zs.avail_in = fw->datasize;
665         zs.next_in = __DECONST(char *, fw->data);
666         zs.avail_out = fw_len;
667         zs.next_out = inflate_buffer;
668         status = inflate(&zs, Z_FINISH);
669         if (status != Z_STREAM_END) {
670                 if_printf(sc->ifp, "zlib %d\n", status);
671                 status = EIO;
672                 goto abort_with_buffer;
673         }
674
675         /* Check id */
676         hdr_offset =
677         htobe32(*(const uint32_t *)(inflate_buffer + MCP_HEADER_PTR_OFFSET));
678         if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > fw_len) {
679                 if_printf(sc->ifp, "Bad firmware file");
680                 status = EIO;
681                 goto abort_with_buffer;
682         }
683         hdr = (const void*)(inflate_buffer + hdr_offset);
684
685         status = mxge_validate_firmware(sc, hdr);
686         if (status != 0)
687                 goto abort_with_buffer;
688
689         /* Copy the inflated firmware to NIC SRAM. */
690         for (i = 0; i < fw_len; i += 256) {
691                 mxge_pio_copy(sc->sram + MXGE_FW_OFFSET + i, inflate_buffer + i,
692                     min(256U, (unsigned)(fw_len - i)));
693                 wmb();
694                 dummy = *sc->sram;
695                 wmb();
696         }
697
698         *limit = fw_len;
699         status = 0;
700 abort_with_buffer:
701         kfree(inflate_buffer, M_TEMP);
702         inflateEnd(&zs);
703 abort_with_fw:
704         firmware_put(fw, FIRMWARE_UNLOAD);
705         return status;
706 }
707
708 /*
709  * Enable or disable periodic RDMAs from the host to make certain
710  * chipsets resend dropped PCIe messages
711  */
712 static void
713 mxge_dummy_rdma(mxge_softc_t *sc, int enable)
714 {
715         char buf_bytes[72];
716         volatile uint32_t *confirm;
717         volatile char *submit;
718         uint32_t *buf, dma_low, dma_high;
719         int i;
720
721         buf = (uint32_t *)((unsigned long)(buf_bytes + 7) & ~7UL);
722
723         /* Clear confirmation addr */
724         confirm = (volatile uint32_t *)sc->cmd;
725         *confirm = 0;
726         wmb();
727
728         /*
729          * Send an rdma command to the PCIe engine, and wait for the
730          * response in the confirmation address.  The firmware should
731          * write a -1 there to indicate it is alive and well
732          */
733         dma_low = MXGE_LOWPART_TO_U32(sc->cmd_dma.dmem_busaddr);
734         dma_high = MXGE_HIGHPART_TO_U32(sc->cmd_dma.dmem_busaddr);
735         buf[0] = htobe32(dma_high);             /* confirm addr MSW */
736         buf[1] = htobe32(dma_low);              /* confirm addr LSW */
737         buf[2] = htobe32(0xffffffff);           /* confirm data */
738         dma_low = MXGE_LOWPART_TO_U32(sc->zeropad_dma.dmem_busaddr);
739         dma_high = MXGE_HIGHPART_TO_U32(sc->zeropad_dma.dmem_busaddr);
740         buf[3] = htobe32(dma_high);             /* dummy addr MSW */
741         buf[4] = htobe32(dma_low);              /* dummy addr LSW */
742         buf[5] = htobe32(enable);               /* enable? */
743
744         submit = (volatile char *)(sc->sram + MXGEFW_BOOT_DUMMY_RDMA);
745
746         mxge_pio_copy(submit, buf, 64);
747         wmb();
748         DELAY(1000);
749         wmb();
750         i = 0;
751         while (*confirm != 0xffffffff && i < 20) {
752                 DELAY(1000);
753                 i++;
754         }
755         if (*confirm != 0xffffffff) {
756                 if_printf(sc->ifp, "dummy rdma %s failed (%p = 0x%x)",
757                     (enable ? "enable" : "disable"), confirm, *confirm);
758         }
759 }
760
761 static int 
762 mxge_send_cmd(mxge_softc_t *sc, uint32_t cmd, mxge_cmd_t *data)
763 {
764         mcp_cmd_t *buf;
765         char buf_bytes[sizeof(*buf) + 8];
766         volatile mcp_cmd_response_t *response = sc->cmd;
767         volatile char *cmd_addr = sc->sram + MXGEFW_ETH_CMD;
768         uint32_t dma_low, dma_high;
769         int err, sleep_total = 0;
770
771         /* Ensure buf is aligned to 8 bytes */
772         buf = (mcp_cmd_t *)((unsigned long)(buf_bytes + 7) & ~7UL);
773
774         buf->data0 = htobe32(data->data0);
775         buf->data1 = htobe32(data->data1);
776         buf->data2 = htobe32(data->data2);
777         buf->cmd = htobe32(cmd);
778         dma_low = MXGE_LOWPART_TO_U32(sc->cmd_dma.dmem_busaddr);
779         dma_high = MXGE_HIGHPART_TO_U32(sc->cmd_dma.dmem_busaddr);
780
781         buf->response_addr.low = htobe32(dma_low);
782         buf->response_addr.high = htobe32(dma_high);
783
784         response->result = 0xffffffff;
785         wmb();
786         mxge_pio_copy((volatile void *)cmd_addr, buf, sizeof (*buf));
787
788         /*
789          * Wait up to 20ms
790          */
791         err = EAGAIN;
792         for (sleep_total = 0; sleep_total < 20; sleep_total++) {
793                 wmb();
794                 switch (be32toh(response->result)) {
795                 case 0:
796                         data->data0 = be32toh(response->data);
797                         err = 0;
798                         break;
799                 case 0xffffffff:
800                         DELAY(1000);
801                         break;
802                 case MXGEFW_CMD_UNKNOWN:
803                         err = ENOSYS;
804                         break;
805                 case MXGEFW_CMD_ERROR_UNALIGNED:
806                         err = E2BIG;
807                         break;
808                 case MXGEFW_CMD_ERROR_BUSY:
809                         err = EBUSY;
810                         break;
811                 case MXGEFW_CMD_ERROR_I2C_ABSENT:
812                         err = ENXIO;
813                         break;
814                 default:
815                         if_printf(sc->ifp, "command %d failed, result = %d\n",
816                             cmd, be32toh(response->result));
817                         err = ENXIO;
818                         break;
819                 }
820                 if (err != EAGAIN)
821                         break;
822         }
823         if (err == EAGAIN) {
824                 if_printf(sc->ifp, "command %d timed out result = %d\n",
825                     cmd, be32toh(response->result));
826         }
827         return err;
828 }
829
830 static int
831 mxge_adopt_running_firmware(mxge_softc_t *sc)
832 {
833         struct mcp_gen_header *hdr;
834         const size_t bytes = sizeof(struct mcp_gen_header);
835         size_t hdr_offset;
836         int status;
837
838         /*
839          * Find running firmware header
840          */
841         hdr_offset =
842         htobe32(*(volatile uint32_t *)(sc->sram + MCP_HEADER_PTR_OFFSET));
843
844         if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > sc->sram_size) {
845                 if_printf(sc->ifp, "Running firmware has bad header offset "
846                     "(%zu)\n", hdr_offset);
847                 return EIO;
848         }
849
850         /*
851          * Copy header of running firmware from SRAM to host memory to
852          * validate firmware
853          */
854         hdr = kmalloc(bytes, M_DEVBUF, M_WAITOK);
855         bus_space_read_region_1(rman_get_bustag(sc->mem_res),
856             rman_get_bushandle(sc->mem_res), hdr_offset, (char *)hdr, bytes);
857         status = mxge_validate_firmware(sc, hdr);
858         kfree(hdr, M_DEVBUF);
859
860         /* 
861          * Check to see if adopted firmware has bug where adopting
862          * it will cause broadcasts to be filtered unless the NIC
863          * is kept in ALLMULTI mode
864          */
865         if (sc->fw_ver_major == 1 && sc->fw_ver_minor == 4 &&
866             sc->fw_ver_tiny >= 4 && sc->fw_ver_tiny <= 11) {
867                 sc->adopted_rx_filter_bug = 1;
868                 if_printf(sc->ifp, "Adopting fw %d.%d.%d: "
869                     "working around rx filter bug\n",
870                     sc->fw_ver_major, sc->fw_ver_minor, sc->fw_ver_tiny);
871         }
872
873         return status;
874 }
875
876 static int
877 mxge_load_firmware(mxge_softc_t *sc, int adopt)
878 {
879         volatile uint32_t *confirm;
880         volatile char *submit;
881         char buf_bytes[72];
882         uint32_t *buf, size, dma_low, dma_high;
883         int status, i;
884
885         buf = (uint32_t *)((unsigned long)(buf_bytes + 7) & ~7UL);
886
887         size = sc->sram_size;
888         status = mxge_load_firmware_helper(sc, &size);
889         if (status) {
890                 if (!adopt)
891                         return status;
892
893                 /*
894                  * Try to use the currently running firmware, if
895                  * it is new enough
896                  */
897                 status = mxge_adopt_running_firmware(sc);
898                 if (status) {
899                         if_printf(sc->ifp,
900                             "failed to adopt running firmware\n");
901                         return status;
902                 }
903                 if_printf(sc->ifp, "Successfully adopted running firmware\n");
904
905                 if (sc->tx_boundary == 4096) {
906                         if_printf(sc->ifp,
907                              "Using firmware currently running on NIC.  "
908                              "For optimal\n");
909                         if_printf(sc->ifp, "performance consider loading "
910                              "optimized firmware\n");
911                 }
912                 sc->fw_name = mxge_fw_unaligned;
913                 sc->tx_boundary = 2048;
914                 return 0;
915         }
916
917         /* Clear confirmation addr */
918         confirm = (volatile uint32_t *)sc->cmd;
919         *confirm = 0;
920         wmb();
921
922         /*
923          * Send a reload command to the bootstrap MCP, and wait for the
924          * response in the confirmation address.  The firmware should
925          * write a -1 there to indicate it is alive and well
926          */
927
928         dma_low = MXGE_LOWPART_TO_U32(sc->cmd_dma.dmem_busaddr);
929         dma_high = MXGE_HIGHPART_TO_U32(sc->cmd_dma.dmem_busaddr);
930
931         buf[0] = htobe32(dma_high);     /* confirm addr MSW */
932         buf[1] = htobe32(dma_low);      /* confirm addr LSW */
933         buf[2] = htobe32(0xffffffff);   /* confirm data */
934
935         /*
936          * FIX: All newest firmware should un-protect the bottom of
937          * the sram before handoff. However, the very first interfaces
938          * do not. Therefore the handoff copy must skip the first 8 bytes
939          */
940                                         /* where the code starts*/
941         buf[3] = htobe32(MXGE_FW_OFFSET + 8);
942         buf[4] = htobe32(size - 8);     /* length of code */
943         buf[5] = htobe32(8);            /* where to copy to */
944         buf[6] = htobe32(0);            /* where to jump to */
945
946         submit = (volatile char *)(sc->sram + MXGEFW_BOOT_HANDOFF);
947         mxge_pio_copy(submit, buf, 64);
948         wmb();
949         DELAY(1000);
950         wmb();
951         i = 0;
952         while (*confirm != 0xffffffff && i < 20) {
953                 DELAY(1000*10);
954                 i++;
955         }
956         if (*confirm != 0xffffffff) {
957                 if_printf(sc->ifp,"handoff failed (%p = 0x%x)", 
958                     confirm, *confirm);
959                 return ENXIO;
960         }
961         return 0;
962 }
963
964 static int
965 mxge_update_mac_address(mxge_softc_t *sc)
966 {
967         mxge_cmd_t cmd;
968         uint8_t *addr = sc->mac_addr;
969
970         cmd.data0 = (addr[0] << 24) | (addr[1] << 16) |
971             (addr[2] << 8) | addr[3];
972         cmd.data1 = (addr[4] << 8) | (addr[5]);
973         return mxge_send_cmd(sc, MXGEFW_SET_MAC_ADDRESS, &cmd);
974 }
975
976 static int
977 mxge_change_pause(mxge_softc_t *sc, int pause)
978 {       
979         mxge_cmd_t cmd;
980         int status;
981
982         if (pause)
983                 status = mxge_send_cmd(sc, MXGEFW_ENABLE_FLOW_CONTROL, &cmd);
984         else
985                 status = mxge_send_cmd(sc, MXGEFW_DISABLE_FLOW_CONTROL, &cmd);
986         if (status) {
987                 if_printf(sc->ifp, "Failed to set flow control mode\n");
988                 return ENXIO;
989         }
990         sc->pause = pause;
991         return 0;
992 }
993
994 static void
995 mxge_change_promisc(mxge_softc_t *sc, int promisc)
996 {       
997         mxge_cmd_t cmd;
998         int status;
999
1000         if (mxge_always_promisc)
1001                 promisc = 1;
1002
1003         if (promisc)
1004                 status = mxge_send_cmd(sc, MXGEFW_ENABLE_PROMISC, &cmd);
1005         else
1006                 status = mxge_send_cmd(sc, MXGEFW_DISABLE_PROMISC, &cmd);
1007         if (status)
1008                 if_printf(sc->ifp, "Failed to set promisc mode\n");
1009 }
1010
1011 static void
1012 mxge_set_multicast_list(mxge_softc_t *sc)
1013 {
1014         mxge_cmd_t cmd;
1015         struct ifmultiaddr *ifma;
1016         struct ifnet *ifp = sc->ifp;
1017         int err;
1018
1019         /* This firmware is known to not support multicast */
1020         if (!sc->fw_multicast_support)
1021                 return;
1022
1023         /* Disable multicast filtering while we play with the lists*/
1024         err = mxge_send_cmd(sc, MXGEFW_ENABLE_ALLMULTI, &cmd);
1025         if (err != 0) {
1026                 if_printf(ifp, "Failed MXGEFW_ENABLE_ALLMULTI, "
1027                     "error status: %d\n", err);
1028                 return;
1029         }
1030
1031         if (sc->adopted_rx_filter_bug)
1032                 return;
1033         
1034         if (ifp->if_flags & IFF_ALLMULTI) {
1035                 /* Request to disable multicast filtering, so quit here */
1036                 return;
1037         }
1038
1039         /* Flush all the filters */
1040         err = mxge_send_cmd(sc, MXGEFW_LEAVE_ALL_MULTICAST_GROUPS, &cmd);
1041         if (err != 0) {
1042                 if_printf(ifp, "Failed MXGEFW_LEAVE_ALL_MULTICAST_GROUPS, "
1043                     "error status: %d\n", err);
1044                 return;
1045         }
1046
1047         /*
1048          * Walk the multicast list, and add each address
1049          */
1050         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1051                 if (ifma->ifma_addr->sa_family != AF_LINK)
1052                         continue;
1053
1054                 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1055                     &cmd.data0, 4);
1056                 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr) + 4,
1057                     &cmd.data1, 2);
1058                 cmd.data0 = htonl(cmd.data0);
1059                 cmd.data1 = htonl(cmd.data1);
1060                 err = mxge_send_cmd(sc, MXGEFW_JOIN_MULTICAST_GROUP, &cmd);
1061                 if (err != 0) {
1062                         if_printf(ifp, "Failed MXGEFW_JOIN_MULTICAST_GROUP, "
1063                             "error status: %d\n", err);
1064                         /* Abort, leaving multicast filtering off */
1065                         return;
1066                 }
1067         }
1068
1069         /* Enable multicast filtering */
1070         err = mxge_send_cmd(sc, MXGEFW_DISABLE_ALLMULTI, &cmd);
1071         if (err != 0) {
1072                 if_printf(ifp, "Failed MXGEFW_DISABLE_ALLMULTI, "
1073                     "error status: %d\n", err);
1074         }
1075 }
1076
1077 #if 0
1078 static int
1079 mxge_max_mtu(mxge_softc_t *sc)
1080 {
1081         mxge_cmd_t cmd;
1082         int status;
1083
1084         if (MJUMPAGESIZE - MXGEFW_PAD >  MXGEFW_MAX_MTU)
1085                 return  MXGEFW_MAX_MTU - MXGEFW_PAD;
1086
1087         /* try to set nbufs to see if it we can
1088            use virtually contiguous jumbos */
1089         cmd.data0 = 0;
1090         status = mxge_send_cmd(sc, MXGEFW_CMD_ALWAYS_USE_N_BIG_BUFFERS,
1091                                &cmd);
1092         if (status == 0)
1093                 return  MXGEFW_MAX_MTU - MXGEFW_PAD;
1094
1095         /* otherwise, we're limited to MJUMPAGESIZE */
1096         return MJUMPAGESIZE - MXGEFW_PAD;
1097 }
1098 #endif
1099
1100 static int
1101 mxge_reset(mxge_softc_t *sc, int interrupts_setup)
1102 {
1103         struct mxge_slice_state *ss;
1104         mxge_rx_done_t *rx_done;
1105         volatile uint32_t *irq_claim;
1106         mxge_cmd_t cmd;
1107         int slice, status;
1108
1109         /*
1110          * Try to send a reset command to the card to see if it
1111          * is alive
1112          */
1113         memset(&cmd, 0, sizeof (cmd));
1114         status = mxge_send_cmd(sc, MXGEFW_CMD_RESET, &cmd);
1115         if (status != 0) {
1116                 if_printf(sc->ifp, "failed reset\n");
1117                 return ENXIO;
1118         }
1119
1120         mxge_dummy_rdma(sc, 1);
1121
1122         /* Set the intrq size */
1123         cmd.data0 = sc->rx_ring_size;
1124         status = mxge_send_cmd(sc, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd);
1125
1126         /*
1127          * Even though we already know how many slices are supported
1128          * via mxge_slice_probe(), MXGEFW_CMD_GET_MAX_RSS_QUEUES
1129          * has magic side effects, and must be called after a reset.
1130          * It must be called prior to calling any RSS related cmds,
1131          * including assigning an interrupt queue for anything but
1132          * slice 0.  It must also be called *after*
1133          * MXGEFW_CMD_SET_INTRQ_SIZE, since the intrq size is used by
1134          * the firmware to compute offsets.
1135          */
1136         if (sc->num_slices > 1) {
1137                 /* Ask the maximum number of slices it supports */
1138                 status = mxge_send_cmd(sc, MXGEFW_CMD_GET_MAX_RSS_QUEUES, &cmd);
1139                 if (status != 0) {
1140                         if_printf(sc->ifp, "failed to get number of slices\n");
1141                         return status;
1142                 }
1143
1144                 /* 
1145                  * MXGEFW_CMD_ENABLE_RSS_QUEUES must be called prior
1146                  * to setting up the interrupt queue DMA
1147                  */
1148                 cmd.data0 = sc->num_slices;
1149                 cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
1150 #ifdef IFNET_BUF_RING
1151                 cmd.data1 |= MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES;
1152 #endif
1153                 status = mxge_send_cmd(sc, MXGEFW_CMD_ENABLE_RSS_QUEUES, &cmd);
1154                 if (status != 0) {
1155                         if_printf(sc->ifp, "failed to set number of slices\n");
1156                         return status;
1157                 }
1158         }
1159
1160         if (interrupts_setup) {
1161                 /* Now exchange information about interrupts  */
1162                 for (slice = 0; slice < sc->num_slices; slice++) {
1163                         rx_done = &sc->ss[slice].rx_done;
1164                         memset(rx_done->entry, 0, sc->rx_ring_size);
1165                         cmd.data0 =
1166                             MXGE_LOWPART_TO_U32(rx_done->dma.dmem_busaddr);
1167                         cmd.data1 =
1168                             MXGE_HIGHPART_TO_U32(rx_done->dma.dmem_busaddr);
1169                         cmd.data2 = slice;
1170                         status |= mxge_send_cmd(sc, MXGEFW_CMD_SET_INTRQ_DMA,
1171                             &cmd);
1172                 }
1173         }
1174
1175         status |= mxge_send_cmd(sc, MXGEFW_CMD_GET_INTR_COAL_DELAY_OFFSET,
1176             &cmd);
1177         sc->intr_coal_delay_ptr = (volatile uint32_t *)(sc->sram + cmd.data0);
1178
1179         status |= mxge_send_cmd(sc, MXGEFW_CMD_GET_IRQ_ACK_OFFSET, &cmd);
1180         irq_claim = (volatile uint32_t *)(sc->sram + cmd.data0);
1181
1182         status |= mxge_send_cmd(sc,  MXGEFW_CMD_GET_IRQ_DEASSERT_OFFSET, &cmd);
1183         sc->irq_deassert = (volatile uint32_t *)(sc->sram + cmd.data0);
1184
1185         if (status != 0) {
1186                 if_printf(sc->ifp, "failed set interrupt parameters\n");
1187                 return status;
1188         }
1189
1190         *sc->intr_coal_delay_ptr = htobe32(sc->intr_coal_delay);
1191
1192         /* Run a DMA benchmark */
1193         mxge_dma_test(sc, MXGEFW_DMA_TEST);
1194
1195         for (slice = 0; slice < sc->num_slices; slice++) {
1196                 ss = &sc->ss[slice];
1197
1198                 ss->irq_claim = irq_claim + (2 * slice);
1199
1200                 /* Reset mcp/driver shared state back to 0 */
1201                 ss->rx_done.idx = 0;
1202                 ss->rx_done.cnt = 0;
1203                 ss->tx.req = 0;
1204                 ss->tx.done = 0;
1205                 ss->tx.pkt_done = 0;
1206                 ss->tx.queue_active = 0;
1207                 ss->tx.activate = 0;
1208                 ss->tx.deactivate = 0;
1209                 ss->rx_big.cnt = 0;
1210                 ss->rx_small.cnt = 0;
1211                 if (ss->fw_stats != NULL)
1212                         bzero(ss->fw_stats, sizeof(*ss->fw_stats));
1213         }
1214         sc->rdma_tags_available = 15;
1215
1216         status = mxge_update_mac_address(sc);
1217         mxge_change_promisc(sc, sc->ifp->if_flags & IFF_PROMISC);
1218         mxge_change_pause(sc, sc->pause);
1219         mxge_set_multicast_list(sc);
1220
1221         if (sc->throttle) {
1222                 cmd.data0 = sc->throttle;
1223                 if (mxge_send_cmd(sc, MXGEFW_CMD_SET_THROTTLE_FACTOR, &cmd))
1224                         if_printf(sc->ifp, "can't enable throttle\n");
1225         }
1226         return status;
1227 }
1228
1229 static int
1230 mxge_change_throttle(SYSCTL_HANDLER_ARGS)
1231 {
1232         mxge_cmd_t cmd;
1233         mxge_softc_t *sc;
1234         int err;
1235         unsigned int throttle;
1236
1237         sc = arg1;
1238         throttle = sc->throttle;
1239         err = sysctl_handle_int(oidp, &throttle, arg2, req);
1240         if (err != 0)
1241                 return err;
1242
1243         if (throttle == sc->throttle)
1244                 return 0;
1245
1246         if (throttle < MXGE_MIN_THROTTLE || throttle > MXGE_MAX_THROTTLE)
1247                 return EINVAL;
1248
1249         lwkt_serialize_enter(sc->ifp->if_serializer);
1250
1251         cmd.data0 = throttle;
1252         err = mxge_send_cmd(sc, MXGEFW_CMD_SET_THROTTLE_FACTOR, &cmd);
1253         if (err == 0)
1254                 sc->throttle = throttle;
1255
1256         lwkt_serialize_exit(sc->ifp->if_serializer);
1257         return err;
1258 }
1259
1260 static int
1261 mxge_change_intr_coal(SYSCTL_HANDLER_ARGS)
1262 {
1263         mxge_softc_t *sc;
1264         unsigned int intr_coal_delay;
1265         int err;
1266
1267         sc = arg1;
1268         intr_coal_delay = sc->intr_coal_delay;
1269         err = sysctl_handle_int(oidp, &intr_coal_delay, arg2, req);
1270         if (err != 0)
1271                 return err;
1272
1273         if (intr_coal_delay == sc->intr_coal_delay)
1274                 return 0;
1275
1276         if (intr_coal_delay == 0 || intr_coal_delay > 1000*1000)
1277                 return EINVAL;
1278
1279         lwkt_serialize_enter(sc->ifp->if_serializer);
1280
1281         *sc->intr_coal_delay_ptr = htobe32(intr_coal_delay);
1282         sc->intr_coal_delay = intr_coal_delay;
1283
1284         lwkt_serialize_exit(sc->ifp->if_serializer);
1285         return err;
1286 }
1287
1288 static int
1289 mxge_change_flow_control(SYSCTL_HANDLER_ARGS)
1290 {
1291         mxge_softc_t *sc;
1292         unsigned int enabled;
1293         int err;
1294
1295         sc = arg1;
1296         enabled = sc->pause;
1297         err = sysctl_handle_int(oidp, &enabled, arg2, req);
1298         if (err != 0)
1299                 return err;
1300
1301         if (enabled == sc->pause)
1302                 return 0;
1303
1304         lwkt_serialize_enter(sc->ifp->if_serializer);
1305         err = mxge_change_pause(sc, enabled);
1306         lwkt_serialize_exit(sc->ifp->if_serializer);
1307
1308         return err;
1309 }
1310
1311 static int
1312 mxge_handle_be32(SYSCTL_HANDLER_ARGS)
1313 {
1314         int err;
1315
1316         if (arg1 == NULL)
1317                 return EFAULT;
1318         arg2 = be32toh(*(int *)arg1);
1319         arg1 = NULL;
1320         err = sysctl_handle_int(oidp, arg1, arg2, req);
1321
1322         return err;
1323 }
1324
1325 static void
1326 mxge_rem_sysctls(mxge_softc_t *sc)
1327 {
1328         if (sc->ss != NULL) {
1329                 struct mxge_slice_state *ss;
1330                 int slice;
1331
1332                 for (slice = 0; slice < sc->num_slices; slice++) {
1333                         ss = &sc->ss[slice];
1334                         if (ss->sysctl_tree != NULL) {
1335                                 sysctl_ctx_free(&ss->sysctl_ctx);
1336                                 ss->sysctl_tree = NULL;
1337                         }
1338                 }
1339         }
1340
1341         if (sc->slice_sysctl_tree != NULL) {
1342                 sysctl_ctx_free(&sc->slice_sysctl_ctx);
1343                 sc->slice_sysctl_tree = NULL;
1344         }
1345
1346         if (sc->sysctl_tree != NULL) {
1347                 sysctl_ctx_free(&sc->sysctl_ctx);
1348                 sc->sysctl_tree = NULL;
1349         }
1350 }
1351
1352 static void
1353 mxge_add_sysctls(mxge_softc_t *sc)
1354 {
1355         struct sysctl_ctx_list *ctx;
1356         struct sysctl_oid_list *children;
1357         mcp_irq_data_t *fw;
1358         struct mxge_slice_state *ss;
1359         int slice;
1360         char slice_num[8];
1361
1362         ctx = &sc->sysctl_ctx;
1363         sysctl_ctx_init(ctx);
1364         sc->sysctl_tree = SYSCTL_ADD_NODE(ctx, SYSCTL_STATIC_CHILDREN(_hw),
1365             OID_AUTO, device_get_nameunit(sc->dev), CTLFLAG_RD, 0, "");
1366         if (sc->sysctl_tree == NULL) {
1367                 device_printf(sc->dev, "can't add sysctl node\n");
1368                 return;
1369         }
1370
1371         children = SYSCTL_CHILDREN(sc->sysctl_tree);
1372         fw = sc->ss[0].fw_stats;
1373
1374         /*
1375          * Random information
1376          */
1377         SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "firmware_version",
1378             CTLFLAG_RD, &sc->fw_version, 0, "firmware version");
1379
1380         SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "serial_number",
1381             CTLFLAG_RD, &sc->serial_number_string, 0, "serial number");
1382
1383         SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "product_code",
1384             CTLFLAG_RD, &sc->product_code_string, 0, "product code");
1385
1386         SYSCTL_ADD_INT(ctx, children, OID_AUTO, "pcie_link_width",
1387             CTLFLAG_RD, &sc->link_width, 0, "link width");
1388
1389         SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_boundary",
1390             CTLFLAG_RD, &sc->tx_boundary, 0, "tx boundary");
1391
1392         SYSCTL_ADD_INT(ctx, children, OID_AUTO, "write_combine",
1393             CTLFLAG_RD, &sc->wc, 0, "write combining PIO");
1394
1395         SYSCTL_ADD_INT(ctx, children, OID_AUTO, "read_dma_MBs",
1396             CTLFLAG_RD, &sc->read_dma, 0, "DMA Read speed in MB/s");
1397
1398         SYSCTL_ADD_INT(ctx, children, OID_AUTO, "write_dma_MBs",
1399             CTLFLAG_RD, &sc->write_dma, 0, "DMA Write speed in MB/s");
1400
1401         SYSCTL_ADD_INT(ctx, children, OID_AUTO, "read_write_dma_MBs",
1402             CTLFLAG_RD, &sc->read_write_dma, 0,
1403             "DMA concurrent Read/Write speed in MB/s");
1404
1405         SYSCTL_ADD_INT(ctx, children, OID_AUTO, "watchdog_resets",
1406             CTLFLAG_RD, &sc->watchdog_resets, 0,
1407             "Number of times NIC was reset");
1408
1409         /*
1410          * Performance related tunables
1411          */
1412         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "intr_coal_delay",
1413             CTLTYPE_INT|CTLFLAG_RW, sc, 0, mxge_change_intr_coal, "I",
1414             "Interrupt coalescing delay in usecs");
1415
1416         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "throttle",
1417             CTLTYPE_INT|CTLFLAG_RW, sc, 0, mxge_change_throttle, "I",
1418             "Transmit throttling");
1419
1420         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "flow_control_enabled",
1421             CTLTYPE_INT|CTLFLAG_RW, sc, 0, mxge_change_flow_control, "I",
1422             "Interrupt coalescing delay in usecs");
1423
1424         SYSCTL_ADD_INT(ctx, children, OID_AUTO, "deassert_wait",
1425             CTLFLAG_RW, &mxge_deassert_wait, 0,
1426             "Wait for IRQ line to go low in ihandler");
1427
1428         /*
1429          * Stats block from firmware is in network byte order.
1430          * Need to swap it
1431          */
1432         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "link_up",
1433             CTLTYPE_INT|CTLFLAG_RD, &fw->link_up, 0,
1434             mxge_handle_be32, "I", "link up");
1435
1436         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rdma_tags_available",
1437             CTLTYPE_INT|CTLFLAG_RD, &fw->rdma_tags_available, 0,
1438             mxge_handle_be32, "I", "rdma_tags_available");
1439
1440         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dropped_bad_crc32",
1441             CTLTYPE_INT|CTLFLAG_RD, &fw->dropped_bad_crc32, 0,
1442             mxge_handle_be32, "I", "dropped_bad_crc32");
1443
1444         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dropped_bad_phy",
1445             CTLTYPE_INT|CTLFLAG_RD, &fw->dropped_bad_phy, 0,
1446             mxge_handle_be32, "I", "dropped_bad_phy");
1447
1448         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dropped_link_error_or_filtered",
1449             CTLTYPE_INT|CTLFLAG_RD, &fw->dropped_link_error_or_filtered, 0,
1450             mxge_handle_be32, "I", "dropped_link_error_or_filtered");
1451
1452         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dropped_link_overflow",
1453             CTLTYPE_INT|CTLFLAG_RD, &fw->dropped_link_overflow, 0,
1454             mxge_handle_be32, "I", "dropped_link_overflow");
1455
1456         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dropped_multicast_filtered",
1457             CTLTYPE_INT|CTLFLAG_RD, &fw->dropped_multicast_filtered, 0,
1458             mxge_handle_be32, "I", "dropped_multicast_filtered");
1459
1460         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dropped_no_big_buffer",
1461             CTLTYPE_INT|CTLFLAG_RD, &fw->dropped_no_big_buffer, 0,
1462             mxge_handle_be32, "I", "dropped_no_big_buffer");
1463
1464         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dropped_no_small_buffer",
1465             CTLTYPE_INT|CTLFLAG_RD, &fw->dropped_no_small_buffer, 0,
1466             mxge_handle_be32, "I", "dropped_no_small_buffer");
1467
1468         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dropped_overrun",
1469             CTLTYPE_INT|CTLFLAG_RD, &fw->dropped_overrun, 0,
1470             mxge_handle_be32, "I", "dropped_overrun");
1471
1472         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dropped_pause",
1473             CTLTYPE_INT|CTLFLAG_RD, &fw->dropped_pause, 0,
1474             mxge_handle_be32, "I", "dropped_pause");
1475
1476         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dropped_runt",
1477             CTLTYPE_INT|CTLFLAG_RD, &fw->dropped_runt, 0,
1478             mxge_handle_be32, "I", "dropped_runt");
1479
1480         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dropped_unicast_filtered",
1481             CTLTYPE_INT|CTLFLAG_RD, &fw->dropped_unicast_filtered, 0,
1482             mxge_handle_be32, "I", "dropped_unicast_filtered");
1483
1484         /* add counters exported for debugging from all slices */
1485         sysctl_ctx_init(&sc->slice_sysctl_ctx);
1486         sc->slice_sysctl_tree = SYSCTL_ADD_NODE(&sc->slice_sysctl_ctx,
1487             children, OID_AUTO, "slice", CTLFLAG_RD, 0, "");
1488         if (sc->slice_sysctl_tree == NULL) {
1489                 device_printf(sc->dev, "can't add slice sysctl node\n");
1490                 return;
1491         }
1492
1493         for (slice = 0; slice < sc->num_slices; slice++) {
1494                 ss = &sc->ss[slice];
1495                 sysctl_ctx_init(&ss->sysctl_ctx);
1496                 ctx = &ss->sysctl_ctx;
1497                 children = SYSCTL_CHILDREN(sc->slice_sysctl_tree);
1498                 ksprintf(slice_num, "%d", slice);
1499                 ss->sysctl_tree = SYSCTL_ADD_NODE(ctx, children, OID_AUTO,
1500                     slice_num, CTLFLAG_RD, 0, "");
1501                 if (ss->sysctl_tree == NULL) {
1502                         device_printf(sc->dev,
1503                             "can't add %d slice sysctl node\n", slice);
1504                         return; /* XXX continue? */
1505                 }
1506                 children = SYSCTL_CHILDREN(ss->sysctl_tree);
1507
1508                 /*
1509                  * XXX change to ULONG
1510                  */
1511
1512                 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_small_cnt",
1513                     CTLFLAG_RD, &ss->rx_small.cnt, 0, "rx_small_cnt");
1514
1515                 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_big_cnt",
1516                     CTLFLAG_RD, &ss->rx_big.cnt, 0, "rx_small_cnt");
1517
1518 #ifndef IFNET_BUF_RING
1519                 /* only transmit from slice 0 for now */
1520                 if (slice > 0)
1521                         continue;
1522 #endif
1523
1524                 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_req",
1525                     CTLFLAG_RD, &ss->tx.req, 0, "tx_req");
1526
1527                 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_done",
1528                     CTLFLAG_RD, &ss->tx.done, 0, "tx_done");
1529
1530                 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_pkt_done",
1531                     CTLFLAG_RD, &ss->tx.pkt_done, 0, "tx_done");
1532
1533                 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_queue_active",
1534                     CTLFLAG_RD, &ss->tx.queue_active, 0, "tx_queue_active");
1535
1536                 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_activate",
1537                     CTLFLAG_RD, &ss->tx.activate, 0, "tx_activate");
1538
1539                 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_deactivate",
1540                     CTLFLAG_RD, &ss->tx.deactivate, 0, "tx_deactivate");
1541         }
1542 }
1543
1544 /*
1545  * Copy an array of mcp_kreq_ether_send_t's to the mcp.  Copy 
1546  * backwards one at a time and handle ring wraps
1547  */
1548 static __inline void 
1549 mxge_submit_req_backwards(mxge_tx_ring_t *tx,
1550     mcp_kreq_ether_send_t *src, int cnt)
1551 {
1552         int idx, starting_slot;
1553
1554         starting_slot = tx->req;
1555         while (cnt > 1) {
1556                 cnt--;
1557                 idx = (starting_slot + cnt) & tx->mask;
1558                 mxge_pio_copy(&tx->lanai[idx], &src[cnt], sizeof(*src));
1559                 wmb();
1560         }
1561 }
1562
1563 /*
1564  * Copy an array of mcp_kreq_ether_send_t's to the mcp.  Copy
1565  * at most 32 bytes at a time, so as to avoid involving the software
1566  * pio handler in the nic.  We re-write the first segment's flags
1567  * to mark them valid only after writing the entire chain 
1568  */
1569 static __inline void 
1570 mxge_submit_req(mxge_tx_ring_t *tx, mcp_kreq_ether_send_t *src, int cnt)
1571 {
1572         int idx, i;
1573         uint32_t *src_ints;
1574         volatile uint32_t *dst_ints;
1575         mcp_kreq_ether_send_t *srcp;
1576         volatile mcp_kreq_ether_send_t *dstp, *dst;
1577         uint8_t last_flags;
1578
1579         idx = tx->req & tx->mask;
1580
1581         last_flags = src->flags;
1582         src->flags = 0;
1583         wmb();
1584         dst = dstp = &tx->lanai[idx];
1585         srcp = src;
1586
1587         if ((idx + cnt) < tx->mask) {
1588                 for (i = 0; i < cnt - 1; i += 2) {
1589                         mxge_pio_copy(dstp, srcp, 2 * sizeof(*src));
1590                         wmb(); /* force write every 32 bytes */
1591                         srcp += 2;
1592                         dstp += 2;
1593                 }
1594         } else {
1595                 /*
1596                  * Submit all but the first request, and ensure 
1597                  * that it is submitted below
1598                  */
1599                 mxge_submit_req_backwards(tx, src, cnt);
1600                 i = 0;
1601         }
1602         if (i < cnt) {
1603                 /* Submit the first request */
1604                 mxge_pio_copy(dstp, srcp, sizeof(*src));
1605                 wmb(); /* barrier before setting valid flag */
1606         }
1607
1608         /* Re-write the last 32-bits with the valid flags */
1609         src->flags = last_flags;
1610         src_ints = (uint32_t *)src;
1611         src_ints+=3;
1612         dst_ints = (volatile uint32_t *)dst;
1613         dst_ints+=3;
1614         *dst_ints = *src_ints;
1615         tx->req += cnt;
1616         wmb();
1617 }
1618
1619 static int
1620 mxge_pullup_tso(struct mbuf **mp)
1621 {
1622         int hoff, iphlen, thoff;
1623         struct mbuf *m;
1624
1625         m = *mp;
1626         KASSERT(M_WRITABLE(m), ("TSO mbuf not writable"));
1627
1628         iphlen = m->m_pkthdr.csum_iphlen;
1629         thoff = m->m_pkthdr.csum_thlen;
1630         hoff = m->m_pkthdr.csum_lhlen;
1631
1632         KASSERT(iphlen > 0, ("invalid ip hlen"));
1633         KASSERT(thoff > 0, ("invalid tcp hlen"));
1634         KASSERT(hoff > 0, ("invalid ether hlen"));
1635
1636         if (__predict_false(m->m_len < hoff + iphlen + thoff)) {
1637                 m = m_pullup(m, hoff + iphlen + thoff);
1638                 if (m == NULL) {
1639                         *mp = NULL;
1640                         return ENOBUFS;
1641                 }
1642                 *mp = m;
1643         }
1644         return 0;
1645 }
1646
1647 static int
1648 mxge_encap_tso(mxge_tx_ring_t *tx, struct mbuf *m, int busdma_seg_cnt)
1649 {
1650         mcp_kreq_ether_send_t *req;
1651         bus_dma_segment_t *seg;
1652         uint32_t low, high_swapped;
1653         int len, seglen, cum_len, cum_len_next;
1654         int next_is_first, chop, cnt, rdma_count, small;
1655         uint16_t pseudo_hdr_offset, cksum_offset, mss;
1656         uint8_t flags, flags_next;
1657
1658         mss = m->m_pkthdr.tso_segsz;
1659
1660         /*
1661          * Negative cum_len signifies to the send loop that we are
1662          * still in the header portion of the TSO packet.
1663          */
1664         cum_len = -(m->m_pkthdr.csum_lhlen + m->m_pkthdr.csum_iphlen +
1665             m->m_pkthdr.csum_thlen);
1666
1667         /*
1668          * TSO implies checksum offload on this hardware
1669          */
1670         cksum_offset = m->m_pkthdr.csum_lhlen + m->m_pkthdr.csum_iphlen;
1671         flags = MXGEFW_FLAGS_TSO_HDR | MXGEFW_FLAGS_FIRST;
1672
1673         /*
1674          * For TSO, pseudo_hdr_offset holds mss.  The firmware figures
1675          * out where to put the checksum by parsing the header.
1676          */
1677         pseudo_hdr_offset = htobe16(mss);
1678
1679         req = tx->req_list;
1680         seg = tx->seg_list;
1681         cnt = 0;
1682         rdma_count = 0;
1683
1684         /*
1685          * "rdma_count" is the number of RDMAs belonging to the current
1686          * packet BEFORE the current send request.  For non-TSO packets,
1687          * this is equal to "count".
1688          *
1689          * For TSO packets, rdma_count needs to be reset to 0 after a
1690          * segment cut.
1691          *
1692          * The rdma_count field of the send request is the number of
1693          * RDMAs of the packet starting at that request.  For TSO send
1694          * requests with one ore more cuts in the middle, this is the
1695          * number of RDMAs starting after the last cut in the request.
1696          * All previous segments before the last cut implicitly have 1
1697          * RDMA.
1698          *
1699          * Since the number of RDMAs is not known beforehand, it must be
1700          * filled-in retroactively - after each segmentation cut or at
1701          * the end of the entire packet.
1702          */
1703
1704         while (busdma_seg_cnt) {
1705                 /*
1706                  * Break the busdma segment up into pieces
1707                  */
1708                 low = MXGE_LOWPART_TO_U32(seg->ds_addr);
1709                 high_swapped = htobe32(MXGE_HIGHPART_TO_U32(seg->ds_addr));
1710                 len = seg->ds_len;
1711
1712                 while (len) {
1713                         flags_next = flags & ~MXGEFW_FLAGS_FIRST;
1714                         seglen = len;
1715                         cum_len_next = cum_len + seglen;
1716                         (req - rdma_count)->rdma_count = rdma_count + 1;
1717                         if (__predict_true(cum_len >= 0)) {
1718                                 /* Payload */
1719                                 chop = (cum_len_next > mss);
1720                                 cum_len_next = cum_len_next % mss;
1721                                 next_is_first = (cum_len_next == 0);
1722                                 flags |= chop * MXGEFW_FLAGS_TSO_CHOP;
1723                                 flags_next |=
1724                                     next_is_first * MXGEFW_FLAGS_FIRST;
1725                                 rdma_count |= -(chop | next_is_first);
1726                                 rdma_count += chop & !next_is_first;
1727                         } else if (cum_len_next >= 0) {
1728                                 /* Header ends */
1729                                 rdma_count = -1;
1730                                 cum_len_next = 0;
1731                                 seglen = -cum_len;
1732                                 small = (mss <= MXGEFW_SEND_SMALL_SIZE);
1733                                 flags_next = MXGEFW_FLAGS_TSO_PLD |
1734                                     MXGEFW_FLAGS_FIRST |
1735                                     (small * MXGEFW_FLAGS_SMALL);
1736                         }
1737
1738                         req->addr_high = high_swapped;
1739                         req->addr_low = htobe32(low);
1740                         req->pseudo_hdr_offset = pseudo_hdr_offset;
1741                         req->pad = 0;
1742                         req->rdma_count = 1;
1743                         req->length = htobe16(seglen);
1744                         req->cksum_offset = cksum_offset;
1745                         req->flags =
1746                             flags | ((cum_len & 1) * MXGEFW_FLAGS_ALIGN_ODD);
1747                         low += seglen;
1748                         len -= seglen;
1749                         cum_len = cum_len_next;
1750                         flags = flags_next;
1751                         req++;
1752                         cnt++;
1753                         rdma_count++;
1754                         if (__predict_false(cksum_offset > seglen))
1755                                 cksum_offset -= seglen;
1756                         else
1757                                 cksum_offset = 0;
1758                         if (__predict_false(cnt > tx->max_desc))
1759                                 goto drop;
1760                 }
1761                 busdma_seg_cnt--;
1762                 seg++;
1763         }
1764         (req - rdma_count)->rdma_count = rdma_count;
1765
1766         do {
1767                 req--;
1768                 req->flags |= MXGEFW_FLAGS_TSO_LAST;
1769         } while (!(req->flags & (MXGEFW_FLAGS_TSO_CHOP | MXGEFW_FLAGS_FIRST)));
1770
1771         tx->info[((cnt - 1) + tx->req) & tx->mask].flag = 1;
1772         mxge_submit_req(tx, tx->req_list, cnt);
1773 #ifdef IFNET_BUF_RING
1774         if ((ss->sc->num_slices > 1) && tx->queue_active == 0) {
1775                 /* tell the NIC to start polling this slice */
1776                 *tx->send_go = 1;
1777                 tx->queue_active = 1;
1778                 tx->activate++;
1779                 wmb();
1780         }
1781 #endif
1782         return 0;
1783
1784 drop:
1785         bus_dmamap_unload(tx->dmat, tx->info[tx->req & tx->mask].map);
1786         m_freem(m);
1787 #if 0
1788         /* TODO update oerror counter */
1789 #endif
1790         return ENOBUFS;
1791 }
1792
1793 static int
1794 mxge_encap(struct mxge_slice_state *ss, struct mbuf *m)
1795 {
1796         mxge_softc_t *sc;
1797         mcp_kreq_ether_send_t *req;
1798         bus_dma_segment_t *seg;
1799         mxge_tx_ring_t *tx;
1800         int cnt, cum_len, err, i, idx, odd_flag;
1801         uint16_t pseudo_hdr_offset;
1802         uint8_t flags, cksum_offset;
1803
1804         sc = ss->sc;
1805         tx = &ss->tx;
1806
1807         if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1808                 err = mxge_pullup_tso(&m);
1809                 if (__predict_false(err))
1810                         return err;
1811         }
1812
1813         /*
1814          * Map the frame for DMA
1815          */
1816         idx = tx->req & tx->mask;
1817         err = bus_dmamap_load_mbuf_defrag(tx->dmat, tx->info[idx].map, &m,
1818             tx->seg_list, tx->max_desc - 2, &cnt, BUS_DMA_NOWAIT);
1819         if (__predict_false(err != 0))
1820                 goto drop;
1821         bus_dmamap_sync(tx->dmat, tx->info[idx].map, BUS_DMASYNC_PREWRITE);
1822         tx->info[idx].m = m;
1823
1824         /*
1825          * TSO is different enough, we handle it in another routine
1826          */
1827         if (m->m_pkthdr.csum_flags & CSUM_TSO)
1828                 return mxge_encap_tso(tx, m, cnt);
1829
1830         req = tx->req_list;
1831         cksum_offset = 0;
1832         pseudo_hdr_offset = 0;
1833         flags = MXGEFW_FLAGS_NO_TSO;
1834
1835         /*
1836          * Checksum offloading
1837          */
1838         if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1839                 cksum_offset = m->m_pkthdr.csum_lhlen + m->m_pkthdr.csum_iphlen;
1840                 pseudo_hdr_offset = cksum_offset +  m->m_pkthdr.csum_data;
1841                 pseudo_hdr_offset = htobe16(pseudo_hdr_offset);
1842                 req->cksum_offset = cksum_offset;
1843                 flags |= MXGEFW_FLAGS_CKSUM;
1844                 odd_flag = MXGEFW_FLAGS_ALIGN_ODD;
1845         } else {
1846                 odd_flag = 0;
1847         }
1848         if (m->m_pkthdr.len < MXGEFW_SEND_SMALL_SIZE)
1849                 flags |= MXGEFW_FLAGS_SMALL;
1850
1851         /*
1852          * Convert segments into a request list
1853          */
1854         cum_len = 0;
1855         seg = tx->seg_list;
1856         req->flags = MXGEFW_FLAGS_FIRST;
1857         for (i = 0; i < cnt; i++) {
1858                 req->addr_low = htobe32(MXGE_LOWPART_TO_U32(seg->ds_addr));
1859                 req->addr_high = htobe32(MXGE_HIGHPART_TO_U32(seg->ds_addr));
1860                 req->length = htobe16(seg->ds_len);
1861                 req->cksum_offset = cksum_offset;
1862                 if (cksum_offset > seg->ds_len)
1863                         cksum_offset -= seg->ds_len;
1864                 else
1865                         cksum_offset = 0;
1866                 req->pseudo_hdr_offset = pseudo_hdr_offset;
1867                 req->pad = 0; /* complete solid 16-byte block */
1868                 req->rdma_count = 1;
1869                 req->flags |= flags | ((cum_len & 1) * odd_flag);
1870                 cum_len += seg->ds_len;
1871                 seg++;
1872                 req++;
1873                 req->flags = 0;
1874         }
1875         req--;
1876
1877         /*
1878          * Pad runt to 60 bytes
1879          */
1880         if (cum_len < 60) {
1881                 req++;
1882                 req->addr_low =
1883                     htobe32(MXGE_LOWPART_TO_U32(sc->zeropad_dma.dmem_busaddr));
1884                 req->addr_high =
1885                     htobe32(MXGE_HIGHPART_TO_U32(sc->zeropad_dma.dmem_busaddr));
1886                 req->length = htobe16(60 - cum_len);
1887                 req->cksum_offset = 0;
1888                 req->pseudo_hdr_offset = pseudo_hdr_offset;
1889                 req->pad = 0; /* complete solid 16-byte block */
1890                 req->rdma_count = 1;
1891                 req->flags |= flags | ((cum_len & 1) * odd_flag);
1892                 cnt++;
1893         }
1894
1895         tx->req_list[0].rdma_count = cnt;
1896 #if 0
1897         /* print what the firmware will see */
1898         for (i = 0; i < cnt; i++) {
1899                 kprintf("%d: addr: 0x%x 0x%x len:%d pso%d,"
1900                     "cso:%d, flags:0x%x, rdma:%d\n",
1901                     i, (int)ntohl(tx->req_list[i].addr_high),
1902                     (int)ntohl(tx->req_list[i].addr_low),
1903                     (int)ntohs(tx->req_list[i].length),
1904                     (int)ntohs(tx->req_list[i].pseudo_hdr_offset),
1905                     tx->req_list[i].cksum_offset, tx->req_list[i].flags,
1906                     tx->req_list[i].rdma_count);
1907         }
1908         kprintf("--------------\n");
1909 #endif
1910         tx->info[((cnt - 1) + tx->req) & tx->mask].flag = 1;
1911         mxge_submit_req(tx, tx->req_list, cnt);
1912 #ifdef IFNET_BUF_RING
1913         if ((ss->sc->num_slices > 1) && tx->queue_active == 0) {
1914                 /* tell the NIC to start polling this slice */
1915                 *tx->send_go = 1;
1916                 tx->queue_active = 1;
1917                 tx->activate++;
1918                 wmb();
1919         }
1920 #endif
1921         return 0;
1922
1923 drop:
1924         m_freem(m);
1925         ss->oerrors++;
1926         return err;
1927 }
1928
1929 static void
1930 mxge_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1931 {
1932         mxge_softc_t *sc = ifp->if_softc;
1933         struct mxge_slice_state *ss;
1934         mxge_tx_ring_t *tx;
1935         int encap = 0;
1936
1937         ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
1938         ASSERT_SERIALIZED(sc->ifp->if_serializer);
1939
1940         if ((ifp->if_flags & IFF_RUNNING) == 0 || ifsq_is_oactive(ifsq))
1941                 return;
1942
1943         /* XXX Only use the first slice for now */
1944         ss = &sc->ss[0];
1945         tx = &ss->tx;
1946
1947         while (tx->mask - (tx->req - tx->done) > tx->max_desc) {
1948                 struct mbuf *m;
1949                 int error;
1950
1951                 m = ifsq_dequeue(ifsq);
1952                 if (m == NULL)
1953                         goto done;
1954
1955                 BPF_MTAP(ifp, m);
1956                 error = mxge_encap(ss, m);
1957                 if (!error)
1958                         encap = 1;
1959         }
1960
1961         /* Ran out of transmit slots */
1962         ifsq_set_oactive(ifsq);
1963 done:
1964         if (encap)
1965                 ifp->if_timer = 5;
1966 }
1967
1968 static void
1969 mxge_watchdog(struct ifnet *ifp)
1970 {
1971         struct mxge_softc *sc = ifp->if_softc;
1972         uint32_t rx_pause = be32toh(sc->ss->fw_stats->dropped_pause);
1973         mxge_tx_ring_t *tx = &sc->ss[0].tx;
1974
1975         ASSERT_SERIALIZED(ifp->if_serializer);
1976
1977         /* Check for pause blocking before resetting */
1978         if (tx->watchdog_rx_pause == rx_pause) {
1979                 mxge_warn_stuck(sc, tx, 0);
1980                 mxge_watchdog_reset(sc);
1981                 return;
1982         } else {
1983                 if_printf(ifp, "Flow control blocking xmits, "
1984                     "check link partner\n");
1985         }
1986         tx->watchdog_rx_pause = rx_pause;
1987 }
1988
1989 /*
1990  * copy an array of mcp_kreq_ether_recv_t's to the mcp.  Copy
1991  * at most 32 bytes at a time, so as to avoid involving the software
1992  * pio handler in the nic.   We re-write the first segment's low
1993  * DMA address to mark it valid only after we write the entire chunk
1994  * in a burst
1995  */
1996 static __inline void
1997 mxge_submit_8rx(volatile mcp_kreq_ether_recv_t *dst,
1998     mcp_kreq_ether_recv_t *src)
1999 {
2000         uint32_t low;
2001
2002         low = src->addr_low;
2003         src->addr_low = 0xffffffff;
2004         mxge_pio_copy(dst, src, 4 * sizeof (*src));
2005         wmb();
2006         mxge_pio_copy(dst + 4, src + 4, 4 * sizeof (*src));
2007         wmb();
2008         src->addr_low = low;
2009         dst->addr_low = low;
2010         wmb();
2011 }
2012
2013 static int
2014 mxge_get_buf_small(mxge_rx_ring_t *rx, bus_dmamap_t map, int idx,
2015     boolean_t init)
2016 {
2017         bus_dma_segment_t seg;
2018         struct mbuf *m;
2019         int cnt, err, mflag;
2020
2021         mflag = MB_DONTWAIT;
2022         if (__predict_false(init))
2023                 mflag = MB_WAIT;
2024
2025         m = m_gethdr(mflag, MT_DATA);
2026         if (m == NULL) {
2027                 rx->alloc_fail++;
2028                 err = ENOBUFS;
2029                 if (__predict_false(init)) {
2030                         /*
2031                          * During initialization, there
2032                          * is nothing to setup; bail out
2033                          */
2034                         return err;
2035                 }
2036                 goto done;
2037         }
2038         m->m_len = m->m_pkthdr.len = MHLEN;
2039
2040         err = bus_dmamap_load_mbuf_segment(rx->dmat, map, m,
2041             &seg, 1, &cnt, BUS_DMA_NOWAIT);
2042         if (err != 0) {
2043                 m_freem(m);
2044                 if (__predict_false(init)) {
2045                         /*
2046                          * During initialization, there
2047                          * is nothing to setup; bail out
2048                          */
2049                         return err;
2050                 }
2051                 goto done;
2052         }
2053
2054         rx->info[idx].m = m;
2055         rx->shadow[idx].addr_low = htobe32(MXGE_LOWPART_TO_U32(seg.ds_addr));
2056         rx->shadow[idx].addr_high = htobe32(MXGE_HIGHPART_TO_U32(seg.ds_addr));
2057
2058 done:
2059         if ((idx & 7) == 7)
2060                 mxge_submit_8rx(&rx->lanai[idx - 7], &rx->shadow[idx - 7]);
2061         return err;
2062 }
2063
2064 static int
2065 mxge_get_buf_big(mxge_rx_ring_t *rx, bus_dmamap_t map, int idx,
2066     boolean_t init)
2067 {
2068         bus_dma_segment_t seg;
2069         struct mbuf *m;
2070         int cnt, err, mflag;
2071
2072         mflag = MB_DONTWAIT;
2073         if (__predict_false(init))
2074                 mflag = MB_WAIT;
2075
2076         if (rx->cl_size == MCLBYTES)
2077                 m = m_getcl(mflag, MT_DATA, M_PKTHDR);
2078         else
2079                 m = m_getjcl(mflag, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
2080         if (m == NULL) {
2081                 rx->alloc_fail++;
2082                 err = ENOBUFS;
2083                 if (__predict_false(init)) {
2084                         /*
2085                          * During initialization, there
2086                          * is nothing to setup; bail out
2087                          */
2088                         return err;
2089                 }
2090                 goto done;
2091         }
2092         m->m_len = m->m_pkthdr.len = rx->mlen;
2093
2094         err = bus_dmamap_load_mbuf_segment(rx->dmat, map, m,
2095             &seg, 1, &cnt, BUS_DMA_NOWAIT);
2096         if (err != 0) {
2097                 m_freem(m);
2098                 if (__predict_false(init)) {
2099                         /*
2100                          * During initialization, there
2101                          * is nothing to setup; bail out
2102                          */
2103                         return err;
2104                 }
2105                 goto done;
2106         }
2107
2108         rx->info[idx].m = m;
2109         rx->shadow[idx].addr_low = htobe32(MXGE_LOWPART_TO_U32(seg.ds_addr));
2110         rx->shadow[idx].addr_high = htobe32(MXGE_HIGHPART_TO_U32(seg.ds_addr));
2111
2112 done:
2113         if ((idx & 7) == 7)
2114                 mxge_submit_8rx(&rx->lanai[idx - 7], &rx->shadow[idx - 7]);
2115         return err;
2116 }
2117
2118 /* 
2119  * Myri10GE hardware checksums are not valid if the sender
2120  * padded the frame with non-zero padding.  This is because
2121  * the firmware just does a simple 16-bit 1s complement
2122  * checksum across the entire frame, excluding the first 14
2123  * bytes.  It is best to simply to check the checksum and
2124  * tell the stack about it only if the checksum is good
2125  */
2126 static __inline uint16_t
2127 mxge_rx_csum(struct mbuf *m, int csum)
2128 {
2129         const struct ether_header *eh;
2130         const struct ip *ip;
2131         uint16_t c;
2132
2133         eh = mtod(m, const struct ether_header *);
2134
2135         /* Only deal with IPv4 TCP & UDP for now */
2136         if (__predict_false(eh->ether_type != htons(ETHERTYPE_IP)))
2137                 return 1;
2138
2139         ip = (const struct ip *)(eh + 1);
2140         if (__predict_false(ip->ip_p != IPPROTO_TCP && ip->ip_p != IPPROTO_UDP))
2141                 return 1;
2142
2143 #ifdef INET
2144         c = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
2145             htonl(ntohs(csum) + ntohs(ip->ip_len) +
2146                   - (ip->ip_hl << 2) + ip->ip_p));
2147 #else
2148         c = 1;
2149 #endif
2150         c ^= 0xffff;
2151         return c;
2152 }
2153
2154 static void
2155 mxge_vlan_tag_remove(struct mbuf *m, uint32_t *csum)
2156 {
2157         struct ether_vlan_header *evl;
2158         uint32_t partial;
2159
2160         evl = mtod(m, struct ether_vlan_header *);
2161
2162         /*
2163          * Fix checksum by subtracting EVL_ENCAPLEN bytes after
2164          * what the firmware thought was the end of the ethernet
2165          * header.
2166          */
2167
2168         /* Put checksum into host byte order */
2169         *csum = ntohs(*csum);
2170
2171         partial = ntohl(*(uint32_t *)(mtod(m, char *) + ETHER_HDR_LEN));
2172         *csum += ~partial;
2173         *csum += ((*csum) < ~partial);
2174         *csum = ((*csum) >> 16) + ((*csum) & 0xFFFF);
2175         *csum = ((*csum) >> 16) + ((*csum) & 0xFFFF);
2176
2177         /*
2178          * Restore checksum to network byte order;
2179          * later consumers expect this
2180          */
2181         *csum = htons(*csum);
2182
2183         /* save the tag */
2184         m->m_pkthdr.ether_vlantag = ntohs(evl->evl_tag);
2185         m->m_flags |= M_VLANTAG;
2186
2187         /*
2188          * Remove the 802.1q header by copying the Ethernet
2189          * addresses over it and adjusting the beginning of
2190          * the data in the mbuf.  The encapsulated Ethernet
2191          * type field is already in place.
2192          */
2193         bcopy((char *)evl, (char *)evl + EVL_ENCAPLEN,
2194             ETHER_HDR_LEN - ETHER_TYPE_LEN);
2195         m_adj(m, EVL_ENCAPLEN);
2196 }
2197
2198
2199 static __inline void
2200 mxge_rx_done_big(struct mxge_slice_state *ss, uint32_t len, uint32_t csum)
2201 {
2202         mxge_softc_t *sc;
2203         struct ifnet *ifp;
2204         struct mbuf *m;
2205         const struct ether_header *eh;
2206         mxge_rx_ring_t *rx;
2207         bus_dmamap_t old_map;
2208         int idx;
2209
2210         sc = ss->sc;
2211         ifp = sc->ifp;
2212         rx = &ss->rx_big;
2213
2214         idx = rx->cnt & rx->mask;
2215         rx->cnt++;
2216
2217         /* Save a pointer to the received mbuf */
2218         m = rx->info[idx].m;
2219
2220         /* Try to replace the received mbuf */
2221         if (mxge_get_buf_big(rx, rx->extra_map, idx, FALSE)) {
2222                 /* Drop the frame -- the old mbuf is re-cycled */
2223                 IFNET_STAT_INC(ifp, ierrors, 1);
2224                 return;
2225         }
2226
2227         /* Unmap the received buffer */
2228         old_map = rx->info[idx].map;
2229         bus_dmamap_sync(rx->dmat, old_map, BUS_DMASYNC_POSTREAD);
2230         bus_dmamap_unload(rx->dmat, old_map);
2231
2232         /* Swap the bus_dmamap_t's */
2233         rx->info[idx].map = rx->extra_map;
2234         rx->extra_map = old_map;
2235
2236         /*
2237          * mcp implicitly skips 1st 2 bytes so that packet is properly
2238          * aligned
2239          */
2240         m->m_data += MXGEFW_PAD;
2241
2242         m->m_pkthdr.rcvif = ifp;
2243         m->m_len = m->m_pkthdr.len = len;
2244
2245         ss->ipackets++;
2246
2247         eh = mtod(m, const struct ether_header *);
2248         if (eh->ether_type == htons(ETHERTYPE_VLAN))
2249                 mxge_vlan_tag_remove(m, &csum);
2250
2251         /* If the checksum is valid, mark it in the mbuf header */
2252         if ((ifp->if_capenable & IFCAP_RXCSUM) &&
2253             mxge_rx_csum(m, csum) == 0) {
2254                 /* Tell the stack that the checksum is good */
2255                 m->m_pkthdr.csum_data = 0xffff;
2256                 m->m_pkthdr.csum_flags = CSUM_PSEUDO_HDR |
2257                     CSUM_DATA_VALID;
2258         }
2259         ifp->if_input(ifp, m);
2260 }
2261
2262 static __inline void
2263 mxge_rx_done_small(struct mxge_slice_state *ss, uint32_t len, uint32_t csum)
2264 {
2265         mxge_softc_t *sc;
2266         struct ifnet *ifp;
2267         const struct ether_header *eh;
2268         struct mbuf *m;
2269         mxge_rx_ring_t *rx;
2270         bus_dmamap_t old_map;
2271         int idx;
2272
2273         sc = ss->sc;
2274         ifp = sc->ifp;
2275         rx = &ss->rx_small;
2276
2277         idx = rx->cnt & rx->mask;
2278         rx->cnt++;
2279
2280         /* Save a pointer to the received mbuf */
2281         m = rx->info[idx].m;
2282
2283         /* Try to replace the received mbuf */
2284         if (mxge_get_buf_small(rx, rx->extra_map, idx, FALSE)) {
2285                 /* Drop the frame -- the old mbuf is re-cycled */
2286                 IFNET_STAT_INC(ifp, ierrors, 1);
2287                 return;
2288         }
2289
2290         /* Unmap the received buffer */
2291         old_map = rx->info[idx].map;
2292         bus_dmamap_sync(rx->dmat, old_map, BUS_DMASYNC_POSTREAD);
2293         bus_dmamap_unload(rx->dmat, old_map);
2294
2295         /* Swap the bus_dmamap_t's */
2296         rx->info[idx].map = rx->extra_map;
2297         rx->extra_map = old_map;
2298
2299         /*
2300          * mcp implicitly skips 1st 2 bytes so that packet is properly
2301          * aligned
2302          */
2303         m->m_data += MXGEFW_PAD;
2304
2305         m->m_pkthdr.rcvif = ifp;
2306         m->m_len = m->m_pkthdr.len = len;
2307
2308         ss->ipackets++;
2309
2310         eh = mtod(m, const struct ether_header *);
2311         if (eh->ether_type == htons(ETHERTYPE_VLAN))
2312                 mxge_vlan_tag_remove(m, &csum);
2313
2314         /* If the checksum is valid, mark it in the mbuf header */
2315         if ((ifp->if_capenable & IFCAP_RXCSUM) &&
2316             mxge_rx_csum(m, csum) == 0) {
2317                 /* Tell the stack that the checksum is good */
2318                 m->m_pkthdr.csum_data = 0xffff;
2319                 m->m_pkthdr.csum_flags = CSUM_PSEUDO_HDR |
2320                     CSUM_DATA_VALID;
2321         }
2322         ifp->if_input(ifp, m);
2323 }
2324
2325 static __inline void
2326 mxge_clean_rx_done(struct mxge_slice_state *ss)
2327 {
2328         mxge_rx_done_t *rx_done = &ss->rx_done;
2329
2330         while (rx_done->entry[rx_done->idx].length != 0) {
2331                 uint16_t length, checksum;
2332
2333                 length = ntohs(rx_done->entry[rx_done->idx].length);
2334                 rx_done->entry[rx_done->idx].length = 0;
2335
2336                 checksum = rx_done->entry[rx_done->idx].checksum;
2337
2338                 if (length <= (MHLEN - MXGEFW_PAD))
2339                         mxge_rx_done_small(ss, length, checksum);
2340                 else
2341                         mxge_rx_done_big(ss, length, checksum);
2342
2343                 rx_done->cnt++;
2344                 rx_done->idx = rx_done->cnt & rx_done->mask;
2345         }
2346 }
2347
2348 static __inline void
2349 mxge_tx_done(struct mxge_slice_state *ss, uint32_t mcp_idx)
2350 {
2351         struct ifnet *ifp;
2352         mxge_tx_ring_t *tx;
2353
2354         tx = &ss->tx;
2355         ifp = ss->sc->ifp;
2356         ASSERT_SERIALIZED(ifp->if_serializer);
2357
2358         while (tx->pkt_done != mcp_idx) {
2359                 struct mbuf *m;
2360                 int idx;
2361
2362                 idx = tx->done & tx->mask;
2363                 tx->done++;
2364
2365                 m = tx->info[idx].m;
2366                 /*
2367                  * mbuf and DMA map only attached to the first
2368                  * segment per-mbuf.
2369                  */
2370                 if (m != NULL) {
2371                         ss->opackets++;
2372                         tx->info[idx].m = NULL;
2373                         bus_dmamap_unload(tx->dmat, tx->info[idx].map);
2374                         m_freem(m);
2375                 }
2376                 if (tx->info[idx].flag) {
2377                         tx->info[idx].flag = 0;
2378                         tx->pkt_done++;
2379                 }
2380         }
2381
2382         /*
2383          * If we have space, clear OACTIVE to tell the stack that
2384          * its OK to send packets
2385          */
2386         if (tx->req - tx->done < (tx->mask + 1) / 4) {
2387                 ifq_clr_oactive(&ifp->if_snd);
2388                 if (tx->req == tx->done)
2389                         ifp->if_timer = 0;
2390         }
2391
2392         if (!ifq_is_empty(&ifp->if_snd))
2393                 if_devstart(ifp);
2394
2395 #ifdef IFNET_BUF_RING
2396         if ((ss->sc->num_slices > 1) && (tx->req == tx->done)) {
2397                 /* let the NIC stop polling this queue, since there
2398                  * are no more transmits pending */
2399                 if (tx->req == tx->done) {
2400                         *tx->send_stop = 1;
2401                         tx->queue_active = 0;
2402                         tx->deactivate++;
2403                         wmb();
2404                 }
2405         }
2406 #endif
2407 }
2408
2409 static struct mxge_media_type mxge_xfp_media_types[] = {
2410         {IFM_10G_CX4,   0x7f,           "10GBASE-CX4 (module)"},
2411         {IFM_10G_SR,    (1 << 7),       "10GBASE-SR"},
2412         {IFM_10G_LR,    (1 << 6),       "10GBASE-LR"},
2413         {0,             (1 << 5),       "10GBASE-ER"},
2414         {IFM_10G_LRM,   (1 << 4),       "10GBASE-LRM"},
2415         {0,             (1 << 3),       "10GBASE-SW"},
2416         {0,             (1 << 2),       "10GBASE-LW"},
2417         {0,             (1 << 1),       "10GBASE-EW"},
2418         {0,             (1 << 0),       "Reserved"}
2419 };
2420
2421 static struct mxge_media_type mxge_sfp_media_types[] = {
2422         {IFM_10G_TWINAX,      0,        "10GBASE-Twinax"},
2423         {0,             (1 << 7),       "Reserved"},
2424         {IFM_10G_LRM,   (1 << 6),       "10GBASE-LRM"},
2425         {IFM_10G_LR,    (1 << 5),       "10GBASE-LR"},
2426         {IFM_10G_SR,    (1 << 4),       "10GBASE-SR"},
2427         {IFM_10G_TWINAX,(1 << 0),       "10GBASE-Twinax"}
2428 };
2429
2430 static void
2431 mxge_media_set(mxge_softc_t *sc, int media_type)
2432 {
2433         ifmedia_add(&sc->media, IFM_ETHER | IFM_FDX | media_type, 0, NULL);
2434         ifmedia_set(&sc->media, IFM_ETHER | IFM_FDX | media_type);
2435         sc->current_media = media_type;
2436         sc->media.ifm_media = sc->media.ifm_cur->ifm_media;
2437 }
2438
2439 static void
2440 mxge_media_init(mxge_softc_t *sc)
2441 {
2442         const char *ptr;
2443         int i;
2444
2445         ifmedia_removeall(&sc->media);
2446         mxge_media_set(sc, IFM_AUTO);
2447
2448         /* 
2449          * parse the product code to deterimine the interface type
2450          * (CX4, XFP, Quad Ribbon Fiber) by looking at the character
2451          * after the 3rd dash in the driver's cached copy of the
2452          * EEPROM's product code string.
2453          */
2454         ptr = sc->product_code_string;
2455         if (ptr == NULL) {
2456                 if_printf(sc->ifp, "Missing product code\n");
2457                 return;
2458         }
2459
2460         for (i = 0; i < 3; i++, ptr++) {
2461                 ptr = strchr(ptr, '-');
2462                 if (ptr == NULL) {
2463                         if_printf(sc->ifp, "only %d dashes in PC?!?\n", i);
2464                         return;
2465                 }
2466         }
2467         if (*ptr == 'C' || *(ptr +1) == 'C') {
2468                 /* -C is CX4 */
2469                 sc->connector = MXGE_CX4;
2470                 mxge_media_set(sc, IFM_10G_CX4);
2471         } else if (*ptr == 'Q') {
2472                 /* -Q is Quad Ribbon Fiber */
2473                 sc->connector = MXGE_QRF;
2474                 if_printf(sc->ifp, "Quad Ribbon Fiber Media\n");
2475                 /* FreeBSD has no media type for Quad ribbon fiber */
2476         } else if (*ptr == 'R') {
2477                 /* -R is XFP */
2478                 sc->connector = MXGE_XFP;
2479         } else if (*ptr == 'S' || *(ptr +1) == 'S') {
2480                 /* -S or -2S is SFP+ */
2481                 sc->connector = MXGE_SFP;
2482         } else {
2483                 if_printf(sc->ifp, "Unknown media type: %c\n", *ptr);
2484         }
2485 }
2486
2487 /*
2488  * Determine the media type for a NIC.  Some XFPs will identify
2489  * themselves only when their link is up, so this is initiated via a
2490  * link up interrupt.  However, this can potentially take up to
2491  * several milliseconds, so it is run via the watchdog routine, rather
2492  * than in the interrupt handler itself. 
2493  */
2494 static void
2495 mxge_media_probe(mxge_softc_t *sc)
2496 {
2497         mxge_cmd_t cmd;
2498         const char *cage_type;
2499         struct mxge_media_type *mxge_media_types = NULL;
2500         int i, err, ms, mxge_media_type_entries;
2501         uint32_t byte;
2502
2503         sc->need_media_probe = 0;
2504
2505         if (sc->connector == MXGE_XFP) {
2506                 /* -R is XFP */
2507                 mxge_media_types = mxge_xfp_media_types;
2508                 mxge_media_type_entries = sizeof(mxge_xfp_media_types) /
2509                     sizeof(mxge_xfp_media_types[0]);
2510                 byte = MXGE_XFP_COMPLIANCE_BYTE;
2511                 cage_type = "XFP";
2512         } else  if (sc->connector == MXGE_SFP) {
2513                 /* -S or -2S is SFP+ */
2514                 mxge_media_types = mxge_sfp_media_types;
2515                 mxge_media_type_entries = sizeof(mxge_sfp_media_types) /
2516                     sizeof(mxge_sfp_media_types[0]);
2517                 cage_type = "SFP+";
2518                 byte = 3;
2519         } else {
2520                 /* nothing to do; media type cannot change */
2521                 return;
2522         }
2523
2524         /*
2525          * At this point we know the NIC has an XFP cage, so now we
2526          * try to determine what is in the cage by using the
2527          * firmware's XFP I2C commands to read the XFP 10GbE compilance
2528          * register.  We read just one byte, which may take over
2529          * a millisecond
2530          */
2531
2532         cmd.data0 = 0;   /* just fetch 1 byte, not all 256 */
2533         cmd.data1 = byte;
2534         err = mxge_send_cmd(sc, MXGEFW_CMD_I2C_READ, &cmd);
2535         if (err == MXGEFW_CMD_ERROR_I2C_FAILURE)
2536                 if_printf(sc->ifp, "failed to read XFP\n");
2537         if (err == MXGEFW_CMD_ERROR_I2C_ABSENT)
2538                 if_printf(sc->ifp, "Type R/S with no XFP!?!?\n");
2539         if (err != MXGEFW_CMD_OK)
2540                 return;
2541
2542         /* Now we wait for the data to be cached */
2543         cmd.data0 = byte;
2544         err = mxge_send_cmd(sc, MXGEFW_CMD_I2C_BYTE, &cmd);
2545         for (ms = 0; err == EBUSY && ms < 50; ms++) {
2546                 DELAY(1000);
2547                 cmd.data0 = byte;
2548                 err = mxge_send_cmd(sc, MXGEFW_CMD_I2C_BYTE, &cmd);
2549         }
2550         if (err != MXGEFW_CMD_OK) {
2551                 if_printf(sc->ifp, "failed to read %s (%d, %dms)\n",
2552                     cage_type, err, ms);
2553                 return;
2554         }
2555
2556         if (cmd.data0 == mxge_media_types[0].bitmask) {
2557                 if (bootverbose) {
2558                         if_printf(sc->ifp, "%s:%s\n", cage_type,
2559                             mxge_media_types[0].name);
2560                 }
2561                 if (sc->current_media != mxge_media_types[0].flag) {
2562                         mxge_media_init(sc);
2563                         mxge_media_set(sc, mxge_media_types[0].flag);
2564                 }
2565                 return;
2566         }
2567         for (i = 1; i < mxge_media_type_entries; i++) {
2568                 if (cmd.data0 & mxge_media_types[i].bitmask) {
2569                         if (bootverbose) {
2570                                 if_printf(sc->ifp, "%s:%s\n", cage_type,
2571                                     mxge_media_types[i].name);
2572                         }
2573
2574                         if (sc->current_media != mxge_media_types[i].flag) {
2575                                 mxge_media_init(sc);
2576                                 mxge_media_set(sc, mxge_media_types[i].flag);
2577                         }
2578                         return;
2579                 }
2580         }
2581         if (bootverbose) {
2582                 if_printf(sc->ifp, "%s media 0x%x unknown\n", cage_type,
2583                     cmd.data0);
2584         }
2585 }
2586
2587 static void
2588 mxge_intr(void *arg)
2589 {
2590         struct mxge_slice_state *ss = arg;
2591         mxge_softc_t *sc = ss->sc;
2592         mcp_irq_data_t *stats = ss->fw_stats;
2593         mxge_tx_ring_t *tx = &ss->tx;
2594         mxge_rx_done_t *rx_done = &ss->rx_done;
2595         uint32_t send_done_count;
2596         uint8_t valid;
2597
2598
2599 #ifndef IFNET_BUF_RING
2600         /* an interrupt on a non-zero slice is implicitly valid
2601            since MSI-X irqs are not shared */
2602         if (ss != sc->ss) {
2603                 mxge_clean_rx_done(ss);
2604                 *ss->irq_claim = be32toh(3);
2605                 return;
2606         }
2607 #endif
2608
2609         /* make sure the DMA has finished */
2610         if (!stats->valid) {
2611                 return;
2612         }
2613         valid = stats->valid;
2614
2615         if (sc->irq_type == PCI_INTR_TYPE_LEGACY) {
2616                 /* lower legacy IRQ  */
2617                 *sc->irq_deassert = 0;
2618                 if (!mxge_deassert_wait)
2619                         /* don't wait for conf. that irq is low */
2620                         stats->valid = 0;
2621         } else {
2622                 stats->valid = 0;
2623         }
2624
2625         /* loop while waiting for legacy irq deassertion */
2626         do {
2627                 /* check for transmit completes and receives */
2628                 send_done_count = be32toh(stats->send_done_count);
2629                 while ((send_done_count != tx->pkt_done) ||
2630                        (rx_done->entry[rx_done->idx].length != 0)) {
2631                         if (send_done_count != tx->pkt_done)
2632                                 mxge_tx_done(ss, (int)send_done_count);
2633                         mxge_clean_rx_done(ss);
2634                         send_done_count = be32toh(stats->send_done_count);
2635                 }
2636                 if (sc->irq_type == PCI_INTR_TYPE_LEGACY && mxge_deassert_wait)
2637                         wmb();
2638         } while (*((volatile uint8_t *) &stats->valid));
2639
2640         /* fw link & error stats meaningful only on the first slice */
2641         if (__predict_false((ss == sc->ss) && stats->stats_updated)) {
2642                 if (sc->link_state != stats->link_up) {
2643                         sc->link_state = stats->link_up;
2644                         if (sc->link_state) {
2645                                 sc->ifp->if_link_state = LINK_STATE_UP;
2646                                 if_link_state_change(sc->ifp);
2647                                 if (bootverbose)
2648                                         device_printf(sc->dev, "link up\n");
2649                         } else {
2650                                 sc->ifp->if_link_state = LINK_STATE_DOWN;
2651                                 if_link_state_change(sc->ifp);
2652                                 if (bootverbose)
2653                                         device_printf(sc->dev, "link down\n");
2654                         }
2655                         sc->need_media_probe = 1;
2656                 }
2657                 if (sc->rdma_tags_available !=
2658                     be32toh(stats->rdma_tags_available)) {
2659                         sc->rdma_tags_available = 
2660                                 be32toh(stats->rdma_tags_available);
2661                         device_printf(sc->dev, "RDMA timed out! %d tags "
2662                                       "left\n", sc->rdma_tags_available);
2663                 }
2664
2665                 if (stats->link_down) {
2666                         sc->down_cnt += stats->link_down;
2667                         sc->link_state = 0;
2668                         sc->ifp->if_link_state = LINK_STATE_DOWN;
2669                         if_link_state_change(sc->ifp);
2670                 }
2671         }
2672
2673         /* check to see if we have rx token to pass back */
2674         if (valid & 0x1)
2675             *ss->irq_claim = be32toh(3);
2676         *(ss->irq_claim + 1) = be32toh(3);
2677 }
2678
2679 static void
2680 mxge_init(void *arg)
2681 {
2682         struct mxge_softc *sc = arg;
2683
2684         ASSERT_SERIALIZED(sc->ifp->if_serializer);
2685         if ((sc->ifp->if_flags & IFF_RUNNING) == 0)
2686                 mxge_open(sc);
2687 }
2688
2689 static void
2690 mxge_free_slice_mbufs(struct mxge_slice_state *ss)
2691 {
2692         int i;
2693
2694         for (i = 0; i <= ss->rx_big.mask; i++) {
2695                 if (ss->rx_big.info[i].m == NULL)
2696                         continue;
2697                 bus_dmamap_unload(ss->rx_big.dmat, ss->rx_big.info[i].map);
2698                 m_freem(ss->rx_big.info[i].m);
2699                 ss->rx_big.info[i].m = NULL;
2700         }
2701
2702         for (i = 0; i <= ss->rx_small.mask; i++) {
2703                 if (ss->rx_small.info[i].m == NULL)
2704                         continue;
2705                 bus_dmamap_unload(ss->rx_small.dmat, ss->rx_small.info[i].map);
2706                 m_freem(ss->rx_small.info[i].m);
2707                 ss->rx_small.info[i].m = NULL;
2708         }
2709
2710         /* Transmit ring used only on the first slice */
2711         if (ss->tx.info == NULL)
2712                 return;
2713
2714         for (i = 0; i <= ss->tx.mask; i++) {
2715                 ss->tx.info[i].flag = 0;
2716                 if (ss->tx.info[i].m == NULL)
2717                         continue;
2718                 bus_dmamap_unload(ss->tx.dmat, ss->tx.info[i].map);
2719                 m_freem(ss->tx.info[i].m);
2720                 ss->tx.info[i].m = NULL;
2721         }
2722 }
2723
2724 static void
2725 mxge_free_mbufs(mxge_softc_t *sc)
2726 {
2727         int slice;
2728
2729         for (slice = 0; slice < sc->num_slices; slice++)
2730                 mxge_free_slice_mbufs(&sc->ss[slice]);
2731 }
2732
2733 static void
2734 mxge_free_slice_rings(struct mxge_slice_state *ss)
2735 {
2736         int i;
2737
2738         if (ss->rx_done.entry != NULL) {
2739                 mxge_dma_free(&ss->rx_done.dma);
2740                 ss->rx_done.entry = NULL;
2741         }
2742
2743         if (ss->tx.req_bytes != NULL) {
2744                 kfree(ss->tx.req_bytes, M_DEVBUF);
2745                 ss->tx.req_bytes = NULL;
2746         }
2747
2748         if (ss->tx.seg_list != NULL) {
2749                 kfree(ss->tx.seg_list, M_DEVBUF);
2750                 ss->tx.seg_list = NULL;
2751         }
2752
2753         if (ss->rx_small.shadow != NULL) {
2754                 kfree(ss->rx_small.shadow, M_DEVBUF);
2755                 ss->rx_small.shadow = NULL;
2756         }
2757
2758         if (ss->rx_big.shadow != NULL) {
2759                 kfree(ss->rx_big.shadow, M_DEVBUF);
2760                 ss->rx_big.shadow = NULL;
2761         }
2762
2763         if (ss->tx.info != NULL) {
2764                 if (ss->tx.dmat != NULL) {
2765                         for (i = 0; i <= ss->tx.mask; i++) {
2766                                 bus_dmamap_destroy(ss->tx.dmat,
2767                                     ss->tx.info[i].map);
2768                         }
2769                         bus_dma_tag_destroy(ss->tx.dmat);
2770                 }
2771                 kfree(ss->tx.info, M_DEVBUF);
2772                 ss->tx.info = NULL;
2773         }
2774
2775         if (ss->rx_small.info != NULL) {
2776                 if (ss->rx_small.dmat != NULL) {
2777                         for (i = 0; i <= ss->rx_small.mask; i++) {
2778                                 bus_dmamap_destroy(ss->rx_small.dmat,
2779                                     ss->rx_small.info[i].map);
2780                         }
2781                         bus_dmamap_destroy(ss->rx_small.dmat,
2782                             ss->rx_small.extra_map);
2783                         bus_dma_tag_destroy(ss->rx_small.dmat);
2784                 }
2785                 kfree(ss->rx_small.info, M_DEVBUF);
2786                 ss->rx_small.info = NULL;
2787         }
2788
2789         if (ss->rx_big.info != NULL) {
2790                 if (ss->rx_big.dmat != NULL) {
2791                         for (i = 0; i <= ss->rx_big.mask; i++) {
2792                                 bus_dmamap_destroy(ss->rx_big.dmat,
2793                                     ss->rx_big.info[i].map);
2794                         }
2795                         bus_dmamap_destroy(ss->rx_big.dmat,
2796                             ss->rx_big.extra_map);
2797                         bus_dma_tag_destroy(ss->rx_big.dmat);
2798                 }
2799                 kfree(ss->rx_big.info, M_DEVBUF);
2800                 ss->rx_big.info = NULL;
2801         }
2802 }
2803
2804 static void
2805 mxge_free_rings(mxge_softc_t *sc)
2806 {
2807         int slice;
2808
2809         if (sc->ss == NULL)
2810                 return;
2811
2812         for (slice = 0; slice < sc->num_slices; slice++)
2813                 mxge_free_slice_rings(&sc->ss[slice]);
2814 }
2815
2816 static int
2817 mxge_alloc_slice_rings(struct mxge_slice_state *ss, int rx_ring_entries,
2818     int tx_ring_entries)
2819 {
2820         mxge_softc_t *sc = ss->sc;
2821         size_t bytes;
2822         int err, i;
2823
2824         /*
2825          * Allocate per-slice receive resources
2826          */
2827
2828         ss->rx_small.mask = ss->rx_big.mask = rx_ring_entries - 1;
2829         ss->rx_done.mask = (2 * rx_ring_entries) - 1;
2830
2831         /* Allocate the rx shadow rings */
2832         bytes = rx_ring_entries * sizeof(*ss->rx_small.shadow);
2833         ss->rx_small.shadow = kmalloc(bytes, M_DEVBUF, M_ZERO|M_WAITOK);
2834
2835         bytes = rx_ring_entries * sizeof(*ss->rx_big.shadow);
2836         ss->rx_big.shadow = kmalloc(bytes, M_DEVBUF, M_ZERO|M_WAITOK);
2837
2838         /* Allocate the rx host info rings */
2839         bytes = rx_ring_entries * sizeof(*ss->rx_small.info);
2840         ss->rx_small.info = kmalloc(bytes, M_DEVBUF, M_ZERO|M_WAITOK);
2841
2842         bytes = rx_ring_entries * sizeof(*ss->rx_big.info);
2843         ss->rx_big.info = kmalloc(bytes, M_DEVBUF, M_ZERO|M_WAITOK);
2844
2845         /* Allocate the rx busdma resources */
2846         err = bus_dma_tag_create(sc->parent_dmat,       /* parent */
2847                                  1,                     /* alignment */
2848                                  4096,                  /* boundary */
2849                                  BUS_SPACE_MAXADDR,     /* low */
2850                                  BUS_SPACE_MAXADDR,     /* high */
2851                                  NULL, NULL,            /* filter */
2852                                  MHLEN,                 /* maxsize */
2853                                  1,                     /* num segs */
2854                                  MHLEN,                 /* maxsegsize */
2855                                  BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW,
2856                                                         /* flags */
2857                                  &ss->rx_small.dmat);   /* tag */
2858         if (err != 0) {
2859                 device_printf(sc->dev, "Err %d allocating rx_small dmat\n",
2860                     err);
2861                 return err;
2862         }
2863
2864         err = bus_dmamap_create(ss->rx_small.dmat, BUS_DMA_WAITOK,
2865             &ss->rx_small.extra_map);
2866         if (err != 0) {
2867                 device_printf(sc->dev, "Err %d extra rx_small dmamap\n", err);
2868                 bus_dma_tag_destroy(ss->rx_small.dmat);
2869                 ss->rx_small.dmat = NULL;
2870                 return err;
2871         }
2872         for (i = 0; i <= ss->rx_small.mask; i++) {
2873                 err = bus_dmamap_create(ss->rx_small.dmat, BUS_DMA_WAITOK,
2874                     &ss->rx_small.info[i].map);
2875                 if (err != 0) {
2876                         int j;
2877
2878                         device_printf(sc->dev, "Err %d rx_small dmamap\n", err);
2879
2880                         for (j = 0; j < i; ++j) {
2881                                 bus_dmamap_destroy(ss->rx_small.dmat,
2882                                     ss->rx_small.info[j].map);
2883                         }
2884                         bus_dmamap_destroy(ss->rx_small.dmat,
2885                             ss->rx_small.extra_map);
2886                         bus_dma_tag_destroy(ss->rx_small.dmat);
2887                         ss->rx_small.dmat = NULL;
2888                         return err;
2889                 }
2890         }
2891
2892         err = bus_dma_tag_create(sc->parent_dmat,       /* parent */
2893                                  1,                     /* alignment */
2894                                  4096,                  /* boundary */
2895                                  BUS_SPACE_MAXADDR,     /* low */
2896                                  BUS_SPACE_MAXADDR,     /* high */
2897                                  NULL, NULL,            /* filter */
2898                                  4096,                  /* maxsize */
2899                                  1,                     /* num segs */
2900                                  4096,                  /* maxsegsize*/
2901                                  BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW,
2902                                                         /* flags */
2903                                  &ss->rx_big.dmat);     /* tag */
2904         if (err != 0) {
2905                 device_printf(sc->dev, "Err %d allocating rx_big dmat\n",
2906                     err);
2907                 return err;
2908         }
2909
2910         err = bus_dmamap_create(ss->rx_big.dmat, BUS_DMA_WAITOK,
2911             &ss->rx_big.extra_map);
2912         if (err != 0) {
2913                 device_printf(sc->dev, "Err %d extra rx_big dmamap\n", err);
2914                 bus_dma_tag_destroy(ss->rx_big.dmat);
2915                 ss->rx_big.dmat = NULL;
2916                 return err;
2917         }
2918         for (i = 0; i <= ss->rx_big.mask; i++) {
2919                 err = bus_dmamap_create(ss->rx_big.dmat, BUS_DMA_WAITOK,
2920                     &ss->rx_big.info[i].map);
2921                 if (err != 0) {
2922                         int j;
2923
2924                         device_printf(sc->dev, "Err %d rx_big dmamap\n", err);
2925                         for (j = 0; j < i; ++j) {
2926                                 bus_dmamap_destroy(ss->rx_big.dmat,
2927                                     ss->rx_big.info[j].map);
2928                         }
2929                         bus_dmamap_destroy(ss->rx_big.dmat,
2930                             ss->rx_big.extra_map);
2931                         bus_dma_tag_destroy(ss->rx_big.dmat);
2932                         ss->rx_big.dmat = NULL;
2933                         return err;
2934                 }
2935         }
2936
2937         /*
2938          * Now allocate TX resources
2939          */
2940
2941 #ifndef IFNET_BUF_RING
2942         /* only use a single TX ring for now */
2943         if (ss != ss->sc->ss)
2944                 return 0;
2945 #endif
2946
2947         ss->tx.mask = tx_ring_entries - 1;
2948         ss->tx.max_desc = MIN(MXGE_MAX_SEND_DESC, tx_ring_entries / 4);
2949
2950         /* Allocate the tx request copy block XXX */
2951         bytes = 8 + sizeof(*ss->tx.req_list) * (ss->tx.max_desc + 4);
2952         ss->tx.req_bytes = kmalloc(bytes, M_DEVBUF, M_WAITOK);
2953         /* Ensure req_list entries are aligned to 8 bytes */
2954         ss->tx.req_list = (mcp_kreq_ether_send_t *)
2955             ((unsigned long)(ss->tx.req_bytes + 7) & ~7UL);
2956
2957         /* Allocate the tx busdma segment list */
2958         bytes = sizeof(*ss->tx.seg_list) * ss->tx.max_desc;
2959         ss->tx.seg_list = kmalloc(bytes, M_DEVBUF, M_WAITOK);
2960
2961         /* Allocate the tx host info ring */
2962         bytes = tx_ring_entries * sizeof(*ss->tx.info);
2963         ss->tx.info = kmalloc(bytes, M_DEVBUF, M_ZERO|M_WAITOK);
2964         
2965         /* Allocate the tx busdma resources */
2966         err = bus_dma_tag_create(sc->parent_dmat,       /* parent */
2967                                  1,                     /* alignment */
2968                                  sc->tx_boundary,       /* boundary */
2969                                  BUS_SPACE_MAXADDR,     /* low */
2970                                  BUS_SPACE_MAXADDR,     /* high */
2971                                  NULL, NULL,            /* filter */
2972                                  IP_MAXPACKET +
2973                                  sizeof(struct ether_vlan_header),
2974                                                         /* maxsize */
2975                                  ss->tx.max_desc - 2,   /* num segs */
2976                                  sc->tx_boundary,       /* maxsegsz */
2977                                  BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW |
2978                                  BUS_DMA_ONEBPAGE,      /* flags */
2979                                  &ss->tx.dmat);         /* tag */
2980         if (err != 0) {
2981                 device_printf(sc->dev, "Err %d allocating tx dmat\n", err);
2982                 return err;
2983         }
2984
2985         /*
2986          * Now use these tags to setup DMA maps for each slot in the ring
2987          */
2988         for (i = 0; i <= ss->tx.mask; i++) {
2989                 err = bus_dmamap_create(ss->tx.dmat,
2990                     BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE, &ss->tx.info[i].map);
2991                 if (err != 0) {
2992                         int j;
2993
2994                         device_printf(sc->dev, "Err %d tx dmamap\n", err);
2995                         for (j = 0; j < i; ++j) {
2996                                 bus_dmamap_destroy(ss->tx.dmat,
2997                                     ss->tx.info[j].map);
2998                         }
2999                         bus_dma_tag_destroy(ss->tx.dmat);
3000                         ss->tx.dmat = NULL;
3001                         return err;
3002                 }
3003         }
3004         return 0;
3005 }
3006
3007 static int
3008 mxge_alloc_rings(mxge_softc_t *sc)
3009 {
3010         mxge_cmd_t cmd;
3011         int tx_ring_size;
3012         int tx_ring_entries, rx_ring_entries;
3013         int err, slice;
3014
3015         /* Get ring sizes */
3016         err = mxge_send_cmd(sc, MXGEFW_CMD_GET_SEND_RING_SIZE, &cmd);
3017         if (err != 0) {
3018                 device_printf(sc->dev, "Cannot determine tx ring sizes\n");
3019                 return err;
3020         }
3021         tx_ring_size = cmd.data0;
3022
3023         tx_ring_entries = tx_ring_size / sizeof(mcp_kreq_ether_send_t);
3024         rx_ring_entries = sc->rx_ring_size / sizeof(mcp_dma_addr_t);
3025         ifq_set_maxlen(&sc->ifp->if_snd, tx_ring_entries - 1);
3026         ifq_set_ready(&sc->ifp->if_snd);
3027
3028         for (slice = 0; slice < sc->num_slices; slice++) {
3029                 err = mxge_alloc_slice_rings(&sc->ss[slice],
3030                     rx_ring_entries, tx_ring_entries);
3031                 if (err != 0) {
3032                         device_printf(sc->dev,
3033                             "alloc %d slice rings failed\n", slice);
3034                         return err;
3035                 }
3036         }
3037         return 0;
3038 }
3039
3040 static void
3041 mxge_choose_params(int mtu, int *cl_size)
3042 {
3043         int bufsize = mtu + ETHER_HDR_LEN + EVL_ENCAPLEN + MXGEFW_PAD;
3044
3045         if (bufsize < MCLBYTES) {
3046                 *cl_size = MCLBYTES;
3047         } else {
3048                 KASSERT(bufsize < MJUMPAGESIZE, ("invalid MTU %d", mtu));
3049                 *cl_size = MJUMPAGESIZE;
3050         }
3051 }
3052
3053 static int
3054 mxge_slice_open(struct mxge_slice_state *ss, int cl_size)
3055 {
3056         mxge_cmd_t cmd;
3057         int err, i, slice;
3058
3059         slice = ss - ss->sc->ss;
3060
3061         /*
3062          * Get the lanai pointers to the send and receive rings
3063          */
3064         err = 0;
3065 #ifndef IFNET_BUF_RING
3066         /* We currently only send from the first slice */
3067         if (slice == 0) {
3068 #endif
3069                 cmd.data0 = slice;
3070                 err = mxge_send_cmd(ss->sc, MXGEFW_CMD_GET_SEND_OFFSET, &cmd);
3071                 ss->tx.lanai = (volatile mcp_kreq_ether_send_t *)
3072                     (ss->sc->sram + cmd.data0);
3073                 ss->tx.send_go = (volatile uint32_t *)
3074                     (ss->sc->sram + MXGEFW_ETH_SEND_GO + 64 * slice);
3075                 ss->tx.send_stop = (volatile uint32_t *)
3076                     (ss->sc->sram + MXGEFW_ETH_SEND_STOP + 64 * slice);
3077 #ifndef IFNET_BUF_RING
3078         }
3079 #endif
3080
3081         cmd.data0 = slice;
3082         err |= mxge_send_cmd(ss->sc, MXGEFW_CMD_GET_SMALL_RX_OFFSET, &cmd);
3083         ss->rx_small.lanai =
3084             (volatile mcp_kreq_ether_recv_t *)(ss->sc->sram + cmd.data0);
3085
3086         cmd.data0 = slice;
3087         err |= mxge_send_cmd(ss->sc, MXGEFW_CMD_GET_BIG_RX_OFFSET, &cmd);
3088         ss->rx_big.lanai =
3089             (volatile mcp_kreq_ether_recv_t *)(ss->sc->sram + cmd.data0);
3090
3091         if (err != 0) {
3092                 if_printf(ss->sc->ifp,
3093                     "failed to get ring sizes or locations\n");
3094                 return EIO;
3095         }
3096
3097         /*
3098          * Stock small receive ring
3099          */
3100         for (i = 0; i <= ss->rx_small.mask; i++) {
3101                 err = mxge_get_buf_small(&ss->rx_small,
3102                     ss->rx_small.info[i].map, i, TRUE);
3103                 if (err) {
3104                         if_printf(ss->sc->ifp, "alloced %d/%d smalls\n", i,
3105                             ss->rx_small.mask + 1);
3106                         return ENOMEM;
3107                 }
3108         }
3109
3110         /*
3111          * Stock big receive ring
3112          */
3113         for (i = 0; i <= ss->rx_big.mask; i++) {
3114                 ss->rx_big.shadow[i].addr_low = 0xffffffff;
3115                 ss->rx_big.shadow[i].addr_high = 0xffffffff;
3116         }
3117
3118         ss->rx_big.cl_size = cl_size;
3119         ss->rx_big.mlen = ss->sc->ifp->if_mtu + ETHER_HDR_LEN +
3120             EVL_ENCAPLEN + MXGEFW_PAD;
3121
3122         for (i = 0; i <= ss->rx_big.mask; i++) {
3123                 err = mxge_get_buf_big(&ss->rx_big,
3124                     ss->rx_big.info[i].map, i, TRUE);
3125                 if (err) {
3126                         if_printf(ss->sc->ifp, "alloced %d/%d bigs\n", i,
3127                             ss->rx_big.mask + 1);
3128                         return ENOMEM;
3129                 }
3130         }
3131         return 0;
3132 }
3133
3134 static int 
3135 mxge_open(mxge_softc_t *sc)
3136 {
3137         struct ifnet *ifp = sc->ifp;
3138         mxge_cmd_t cmd;
3139         int err, slice, cl_size, i;
3140         bus_addr_t bus;
3141         volatile uint8_t *itable;
3142         struct mxge_slice_state *ss;
3143
3144         ASSERT_SERIALIZED(ifp->if_serializer);
3145
3146         /* Copy the MAC address in case it was overridden */
3147         bcopy(IF_LLADDR(ifp), sc->mac_addr, ETHER_ADDR_LEN);
3148
3149         err = mxge_reset(sc, 1);
3150         if (err != 0) {
3151                 if_printf(ifp, "failed to reset\n");
3152                 return EIO;
3153         }
3154
3155         if (sc->num_slices > 1) {
3156                 /* Setup the indirection table */
3157                 cmd.data0 = sc->num_slices;
3158                 err = mxge_send_cmd(sc, MXGEFW_CMD_SET_RSS_TABLE_SIZE, &cmd);
3159
3160                 err |= mxge_send_cmd(sc, MXGEFW_CMD_GET_RSS_TABLE_OFFSET, &cmd);
3161                 if (err != 0) {
3162                         if_printf(ifp, "failed to setup rss tables\n");
3163                         return err;
3164                 }
3165
3166                 /* Just enable an identity mapping */
3167                 itable = sc->sram + cmd.data0;
3168                 for (i = 0; i < sc->num_slices; i++)
3169                         itable[i] = (uint8_t)i;
3170
3171                 cmd.data0 = 1;
3172                 cmd.data1 = MXGEFW_RSS_HASH_TYPE_TCP_IPV4;
3173                 err = mxge_send_cmd(sc, MXGEFW_CMD_SET_RSS_ENABLE, &cmd);
3174                 if (err != 0) {
3175                         if_printf(ifp, "failed to enable slices\n");
3176                         return err;
3177                 }
3178         }
3179
3180         cmd.data0 = MXGEFW_TSO_MODE_NDIS;
3181         err = mxge_send_cmd(sc, MXGEFW_CMD_SET_TSO_MODE, &cmd);
3182         if (err) {
3183                 /*
3184                  * Can't change TSO mode to NDIS, never allow TSO then
3185                  */
3186                 if_printf(ifp, "failed to set TSO mode\n");
3187                 ifp->if_capenable &= ~IFCAP_TSO;
3188                 ifp->if_capabilities &= ~IFCAP_TSO;
3189                 ifp->if_hwassist &= ~CSUM_TSO;
3190         }
3191
3192         mxge_choose_params(ifp->if_mtu, &cl_size);
3193
3194         cmd.data0 = 1;
3195         err = mxge_send_cmd(sc, MXGEFW_CMD_ALWAYS_USE_N_BIG_BUFFERS, &cmd);
3196         /*
3197          * Error is only meaningful if we're trying to set
3198          * MXGEFW_CMD_ALWAYS_USE_N_BIG_BUFFERS > 1
3199          */
3200
3201         /*
3202          * Give the firmware the mtu and the big and small buffer
3203          * sizes.  The firmware wants the big buf size to be a power
3204          * of two. Luckily, FreeBSD's clusters are powers of two
3205          */
3206         cmd.data0 = ifp->if_mtu + ETHER_HDR_LEN + EVL_ENCAPLEN;
3207         err = mxge_send_cmd(sc, MXGEFW_CMD_SET_MTU, &cmd);
3208
3209         /* XXX need to cut MXGEFW_PAD here? */
3210         cmd.data0 = MHLEN - MXGEFW_PAD;
3211         err |= mxge_send_cmd(sc, MXGEFW_CMD_SET_SMALL_BUFFER_SIZE, &cmd);
3212
3213         cmd.data0 = cl_size;
3214         err |= mxge_send_cmd(sc, MXGEFW_CMD_SET_BIG_BUFFER_SIZE, &cmd);
3215
3216         if (err != 0) {
3217                 if_printf(ifp, "failed to setup params\n");
3218                 goto abort;
3219         }
3220
3221         /* Now give him the pointer to the stats block */
3222         for (slice = 0; 
3223 #ifdef IFNET_BUF_RING
3224              slice < sc->num_slices;
3225 #else
3226              slice < 1;
3227 #endif
3228              slice++) {
3229                 ss = &sc->ss[slice];
3230                 cmd.data0 = MXGE_LOWPART_TO_U32(ss->fw_stats_dma.dmem_busaddr);
3231                 cmd.data1 = MXGE_HIGHPART_TO_U32(ss->fw_stats_dma.dmem_busaddr);
3232                 cmd.data2 = sizeof(struct mcp_irq_data);
3233                 cmd.data2 |= (slice << 16);
3234                 err |= mxge_send_cmd(sc, MXGEFW_CMD_SET_STATS_DMA_V2, &cmd);
3235         }
3236
3237         if (err != 0) {
3238                 bus = sc->ss->fw_stats_dma.dmem_busaddr;
3239                 bus += offsetof(struct mcp_irq_data, send_done_count);
3240                 cmd.data0 = MXGE_LOWPART_TO_U32(bus);
3241                 cmd.data1 = MXGE_HIGHPART_TO_U32(bus);
3242                 err = mxge_send_cmd(sc, MXGEFW_CMD_SET_STATS_DMA_OBSOLETE,
3243                     &cmd);
3244
3245                 /* Firmware cannot support multicast without STATS_DMA_V2 */
3246                 sc->fw_multicast_support = 0;
3247         } else {
3248                 sc->fw_multicast_support = 1;
3249         }
3250
3251         if (err != 0) {
3252                 if_printf(ifp, "failed to setup params\n");
3253                 goto abort;
3254         }
3255
3256         for (slice = 0; slice < sc->num_slices; slice++) {
3257                 err = mxge_slice_open(&sc->ss[slice], cl_size);
3258                 if (err != 0) {
3259                         if_printf(ifp, "couldn't open slice %d\n", slice);
3260                         goto abort;
3261                 }
3262         }
3263
3264         /* Finally, start the firmware running */
3265         err = mxge_send_cmd(sc, MXGEFW_CMD_ETHERNET_UP, &cmd);
3266         if (err) {
3267                 if_printf(ifp, "Couldn't bring up link\n");
3268                 goto abort;
3269         }
3270         ifp->if_flags |= IFF_RUNNING;
3271         ifq_clr_oactive(&ifp->if_snd);
3272         ifp->if_timer = 0;
3273
3274         return 0;
3275
3276 abort:
3277         mxge_free_mbufs(sc);
3278         return err;
3279 }
3280
3281 static void
3282 mxge_close(mxge_softc_t *sc, int down)
3283 {
3284         struct ifnet *ifp = sc->ifp;
3285         mxge_cmd_t cmd;
3286         int err, old_down_cnt;
3287
3288         ASSERT_SERIALIZED(ifp->if_serializer);
3289
3290         ifp->if_flags &= ~IFF_RUNNING;
3291         ifq_clr_oactive(&ifp->if_snd);
3292         ifp->if_timer = 0;
3293
3294         if (!down) {
3295                 old_down_cnt = sc->down_cnt;
3296                 wmb();
3297
3298                 err = mxge_send_cmd(sc, MXGEFW_CMD_ETHERNET_DOWN, &cmd);
3299                 if (err)
3300                         if_printf(ifp, "Couldn't bring down link\n");
3301
3302                 if (old_down_cnt == sc->down_cnt) {
3303                         /* Wait for down irq */
3304                         lwkt_serialize_exit(ifp->if_serializer);
3305                         DELAY(10 * sc->intr_coal_delay);
3306                         lwkt_serialize_enter(ifp->if_serializer);
3307                 }
3308
3309                 wmb();
3310                 if (old_down_cnt == sc->down_cnt)
3311                         if_printf(ifp, "never got down irq\n");
3312         }
3313         mxge_free_mbufs(sc);
3314 }
3315
3316 static void
3317 mxge_setup_cfg_space(mxge_softc_t *sc)
3318 {
3319         device_t dev = sc->dev;
3320         int reg;
3321         uint16_t lnk, pectl;
3322
3323         /* Find the PCIe link width and set max read request to 4KB */
3324         if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0) {
3325                 lnk = pci_read_config(dev, reg + 0x12, 2);
3326                 sc->link_width = (lnk >> 4) & 0x3f;
3327
3328                 if (sc->pectl == 0) {
3329                         pectl = pci_read_config(dev, reg + 0x8, 2);
3330                         pectl = (pectl & ~0x7000) | (5 << 12);
3331                         pci_write_config(dev, reg + 0x8, pectl, 2);
3332                         sc->pectl = pectl;
3333                 } else {
3334                         /* Restore saved pectl after watchdog reset */
3335                         pci_write_config(dev, reg + 0x8, sc->pectl, 2);
3336                 }
3337         }
3338
3339         /* Enable DMA and memory space access */
3340         pci_enable_busmaster(dev);
3341 }
3342
3343 static uint32_t
3344 mxge_read_reboot(mxge_softc_t *sc)
3345 {
3346         device_t dev = sc->dev;
3347         uint32_t vs;
3348
3349         /* Find the vendor specific offset */
3350         if (pci_find_extcap(dev, PCIY_VENDOR, &vs) != 0) {
3351                 if_printf(sc->ifp, "could not find vendor specific offset\n");
3352                 return (uint32_t)-1;
3353         }
3354         /* Enable read32 mode */
3355         pci_write_config(dev, vs + 0x10, 0x3, 1);
3356         /* Tell NIC which register to read */
3357         pci_write_config(dev, vs + 0x18, 0xfffffff0, 4);
3358         return pci_read_config(dev, vs + 0x14, 4);
3359 }
3360
3361 static void
3362 mxge_watchdog_reset(mxge_softc_t *sc)
3363 {
3364         struct pci_devinfo *dinfo;
3365         int err, running;
3366         uint32_t reboot;
3367         uint16_t cmd;
3368
3369         err = ENXIO;
3370
3371         if_printf(sc->ifp, "Watchdog reset!\n");
3372
3373         /*
3374          * Check to see if the NIC rebooted.  If it did, then all of
3375          * PCI config space has been reset, and things like the
3376          * busmaster bit will be zero.  If this is the case, then we
3377          * must restore PCI config space before the NIC can be used
3378          * again
3379          */
3380         cmd = pci_read_config(sc->dev, PCIR_COMMAND, 2);
3381         if (cmd == 0xffff) {
3382                 /* 
3383                  * Maybe the watchdog caught the NIC rebooting; wait
3384                  * up to 100ms for it to finish.  If it does not come
3385                  * back, then give up 
3386                  */
3387                 DELAY(1000*100);
3388                 cmd = pci_read_config(sc->dev, PCIR_COMMAND, 2);
3389                 if (cmd == 0xffff)
3390                         if_printf(sc->ifp, "NIC disappeared!\n");
3391         }
3392         if ((cmd & PCIM_CMD_BUSMASTEREN) == 0) {
3393                 /* Print the reboot status */
3394                 reboot = mxge_read_reboot(sc);
3395                 if_printf(sc->ifp, "NIC rebooted, status = 0x%x\n", reboot);
3396
3397                 running = sc->ifp->if_flags & IFF_RUNNING;
3398                 if (running) {
3399                         /*
3400                          * Quiesce NIC so that TX routines will not try to
3401                          * xmit after restoration of BAR
3402                          */
3403
3404                         /* Mark the link as down */
3405                         if (sc->link_state) {
3406                                 sc->ifp->if_link_state = LINK_STATE_DOWN;
3407                                 if_link_state_change(sc->ifp);
3408                         }
3409                         mxge_close(sc, 1);
3410                 }
3411                 /* Restore PCI configuration space */
3412                 dinfo = device_get_ivars(sc->dev);
3413                 pci_cfg_restore(sc->dev, dinfo);
3414
3415                 /* And redo any changes we made to our config space */
3416                 mxge_setup_cfg_space(sc);
3417
3418                 /* Reload f/w */
3419                 err = mxge_load_firmware(sc, 0);
3420                 if (err)
3421                         if_printf(sc->ifp, "Unable to re-load f/w\n");
3422                 if (running && !err) {
3423                         err = mxge_open(sc);
3424                         if_devstart_sched(sc->ifp);
3425                 }
3426                 sc->watchdog_resets++;
3427         } else {
3428                 if_printf(sc->ifp, "NIC did not reboot, not resetting\n");
3429                 err = 0;
3430         }
3431         if (err) {
3432                 if_printf(sc->ifp, "watchdog reset failed\n");
3433         } else {
3434                 if (sc->dying == 2)
3435                         sc->dying = 0;
3436                 callout_reset(&sc->co_hdl, mxge_ticks, mxge_tick, sc);
3437         }
3438 }
3439
3440 static void
3441 mxge_warn_stuck(mxge_softc_t *sc, mxge_tx_ring_t *tx, int slice)
3442 {
3443         if_printf(sc->ifp, "slice %d struck? ring state:\n", slice);
3444         if_printf(sc->ifp, "tx.req=%d tx.done=%d, tx.queue_active=%d\n",
3445             tx->req, tx->done, tx->queue_active);
3446         if_printf(sc->ifp, "tx.activate=%d tx.deactivate=%d\n",
3447             tx->activate, tx->deactivate);
3448         if_printf(sc->ifp, "pkt_done=%d fw=%d\n",
3449             tx->pkt_done, be32toh(sc->ss->fw_stats->send_done_count));
3450 }
3451
3452 static u_long
3453 mxge_update_stats(mxge_softc_t *sc)
3454 {
3455         struct mxge_slice_state *ss;
3456         u_long pkts = 0;
3457         u_long ipackets = 0, old_ipackets;
3458         u_long opackets = 0, old_opackets;
3459 #ifdef IFNET_BUF_RING
3460         u_long obytes = 0;
3461         u_long omcasts = 0;
3462         u_long odrops = 0;
3463 #endif
3464         u_long oerrors = 0;
3465         int slice;
3466
3467         for (slice = 0; slice < sc->num_slices; slice++) {
3468                 ss = &sc->ss[slice];
3469                 ipackets += ss->ipackets;
3470                 opackets += ss->opackets;
3471 #ifdef IFNET_BUF_RING
3472                 obytes += ss->obytes;
3473                 omcasts += ss->omcasts;
3474                 odrops += ss->tx.br->br_drops;
3475 #endif
3476                 oerrors += ss->oerrors;
3477         }
3478         IFNET_STAT_GET(sc->ifp, ipackets, old_ipackets);
3479         IFNET_STAT_GET(sc->ifp, opackets, old_opackets);
3480
3481         pkts = ipackets - old_ipackets;
3482         pkts += opackets - old_opackets;
3483
3484         IFNET_STAT_SET(sc->ifp, ipackets, ipackets);
3485         IFNET_STAT_SET(sc->ifp, opackets, opackets);
3486 #ifdef IFNET_BUF_RING
3487         sc->ifp->if_obytes = obytes;
3488         sc->ifp->if_omcasts = omcasts;
3489         sc->ifp->if_snd.ifq_drops = odrops;
3490 #endif
3491         IFNET_STAT_SET(sc->ifp, oerrors, oerrors);
3492         return pkts;
3493 }
3494
3495 static void
3496 mxge_tick(void *arg)
3497 {
3498         mxge_softc_t *sc = arg;
3499         u_long pkts = 0;
3500         int err = 0;
3501         int ticks;
3502         uint16_t cmd;
3503
3504         lwkt_serialize_enter(sc->ifp->if_serializer);
3505
3506         ticks = mxge_ticks;
3507         if (sc->ifp->if_flags & IFF_RUNNING) {
3508                 /* Aggregate stats from different slices */
3509                 pkts = mxge_update_stats(sc);
3510                 if (sc->need_media_probe)
3511                         mxge_media_probe(sc);
3512         }
3513         if (pkts == 0) {
3514                 /* Ensure NIC did not suffer h/w fault while idle */
3515                 cmd = pci_read_config(sc->dev, PCIR_COMMAND, 2);                
3516                 if ((cmd & PCIM_CMD_BUSMASTEREN) == 0) {
3517                         sc->dying = 2;
3518                         mxge_watchdog_reset(sc);
3519                         err = ENXIO;
3520                 }
3521                 /* Look less often if NIC is idle */
3522                 ticks *= 4;
3523         }
3524
3525         if (err == 0)
3526                 callout_reset(&sc->co_hdl, ticks, mxge_tick, sc);
3527
3528         lwkt_serialize_exit(sc->ifp->if_serializer);
3529 }
3530
3531 static int
3532 mxge_media_change(struct ifnet *ifp)
3533 {
3534         return EINVAL;
3535 }
3536
3537 static int
3538 mxge_change_mtu(mxge_softc_t *sc, int mtu)
3539 {
3540         struct ifnet *ifp = sc->ifp;
3541         int real_mtu, old_mtu;
3542         int err = 0;
3543
3544         real_mtu = mtu + ETHER_HDR_LEN + EVL_ENCAPLEN;
3545         if (mtu > sc->max_mtu || real_mtu < 60)
3546                 return EINVAL;
3547
3548         old_mtu = ifp->if_mtu;
3549         ifp->if_mtu = mtu;
3550         if (ifp->if_flags & IFF_RUNNING) {
3551                 mxge_close(sc, 0);
3552                 err = mxge_open(sc);
3553                 if (err != 0) {
3554                         ifp->if_mtu = old_mtu;
3555                         mxge_close(sc, 0);
3556                         mxge_open(sc);
3557                 }
3558         }
3559         return err;
3560 }       
3561
3562 static void
3563 mxge_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
3564 {
3565         mxge_softc_t *sc = ifp->if_softc;
3566         
3567
3568         if (sc == NULL)
3569                 return;
3570         ifmr->ifm_status = IFM_AVALID;
3571         ifmr->ifm_active = IFM_ETHER | IFM_FDX;
3572         ifmr->ifm_status |= sc->link_state ? IFM_ACTIVE : 0;
3573         ifmr->ifm_active |= sc->current_media;
3574 }
3575
3576 static int
3577 mxge_ioctl(struct ifnet *ifp, u_long command, caddr_t data,
3578     struct ucred *cr __unused)
3579 {
3580         mxge_softc_t *sc = ifp->if_softc;
3581         struct ifreq *ifr = (struct ifreq *)data;
3582         int err, mask;
3583
3584         ASSERT_SERIALIZED(ifp->if_serializer);
3585         err = 0;
3586
3587         switch (command) {
3588         case SIOCSIFMTU:
3589                 err = mxge_change_mtu(sc, ifr->ifr_mtu);
3590                 break;
3591
3592         case SIOCSIFFLAGS:
3593                 if (sc->dying)
3594                         return EINVAL;
3595
3596                 if (ifp->if_flags & IFF_UP) {
3597                         if (!(ifp->if_flags & IFF_RUNNING)) {
3598                                 err = mxge_open(sc);
3599                         } else {
3600                                 /*
3601                                  * Take care of PROMISC and ALLMULTI
3602                                  * flag changes
3603                                  */
3604                                 mxge_change_promisc(sc,
3605                                     ifp->if_flags & IFF_PROMISC);
3606                                 mxge_set_multicast_list(sc);
3607                         }
3608                 } else {
3609                         if (ifp->if_flags & IFF_RUNNING)
3610                                 mxge_close(sc, 0);
3611                 }
3612                 break;
3613
3614         case SIOCADDMULTI:
3615         case SIOCDELMULTI:
3616                 mxge_set_multicast_list(sc);
3617                 break;
3618
3619         case SIOCSIFCAP:
3620                 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
3621                 if (mask & IFCAP_TXCSUM) {
3622                         ifp->if_capenable ^= IFCAP_TXCSUM;
3623                         if (ifp->if_capenable & IFCAP_TXCSUM)
3624                                 ifp->if_hwassist |= CSUM_TCP | CSUM_UDP;
3625                         else
3626                                 ifp->if_hwassist &= ~(CSUM_TCP | CSUM_UDP);
3627                 }
3628                 if (mask & IFCAP_TSO) {
3629                         ifp->if_capenable ^= IFCAP_TSO;
3630                         if (ifp->if_capenable & IFCAP_TSO)
3631                                 ifp->if_hwassist |= CSUM_TSO;
3632                         else
3633                                 ifp->if_hwassist &= ~CSUM_TSO;
3634                 }
3635                 if (mask & IFCAP_RXCSUM)
3636                         ifp->if_capenable ^= IFCAP_RXCSUM;
3637                 if (mask & IFCAP_VLAN_HWTAGGING)
3638                         ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
3639                 break;
3640
3641         case SIOCGIFMEDIA:
3642                 mxge_media_probe(sc);
3643                 err = ifmedia_ioctl(ifp, (struct ifreq *)data,
3644                     &sc->media, command);
3645                 break;
3646
3647         default:
3648                 err = ether_ioctl(ifp, command, data);
3649                 break;
3650         }
3651         return err;
3652 }
3653
3654 static void
3655 mxge_fetch_tunables(mxge_softc_t *sc)
3656 {
3657         sc->intr_coal_delay = mxge_intr_coal_delay;
3658         if (sc->intr_coal_delay < 0 || sc->intr_coal_delay > (10 * 1000))
3659                 sc->intr_coal_delay = MXGE_INTR_COAL_DELAY;
3660
3661         /* XXX */
3662         if (mxge_ticks == 0)
3663                 mxge_ticks = hz / 2;
3664
3665         sc->pause = mxge_flow_control;
3666
3667         sc->throttle = mxge_throttle;
3668         if (sc->throttle && sc->throttle > MXGE_MAX_THROTTLE)
3669                 sc->throttle = MXGE_MAX_THROTTLE;
3670         if (sc->throttle && sc->throttle < MXGE_MIN_THROTTLE)
3671                 sc->throttle = MXGE_MIN_THROTTLE;
3672 }
3673
3674 static void
3675 mxge_free_slices(mxge_softc_t *sc)
3676 {
3677         struct mxge_slice_state *ss;
3678         int i;
3679
3680         if (sc->ss == NULL)
3681                 return;
3682
3683         for (i = 0; i < sc->num_slices; i++) {
3684                 ss = &sc->ss[i];
3685                 if (ss->fw_stats != NULL) {
3686                         mxge_dma_free(&ss->fw_stats_dma);
3687                         ss->fw_stats = NULL;
3688                 }
3689                 if (ss->rx_done.entry != NULL) {
3690                         mxge_dma_free(&ss->rx_done.dma);
3691                         ss->rx_done.entry = NULL;
3692                 }
3693         }
3694         kfree(sc->ss, M_DEVBUF);
3695         sc->ss = NULL;
3696 }
3697
3698 static int
3699 mxge_alloc_slices(mxge_softc_t *sc)
3700 {
3701         mxge_cmd_t cmd;
3702         struct mxge_slice_state *ss;
3703         size_t bytes;
3704         int err, i, max_intr_slots;
3705
3706         err = mxge_send_cmd(sc, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd);
3707         if (err != 0) {
3708                 device_printf(sc->dev, "Cannot determine rx ring size\n");
3709                 return err;
3710         }
3711         sc->rx_ring_size = cmd.data0;
3712         max_intr_slots = 2 * (sc->rx_ring_size / sizeof (mcp_dma_addr_t));
3713
3714         bytes = sizeof(*sc->ss) * sc->num_slices;
3715         sc->ss = kmalloc(bytes, M_DEVBUF, M_WAITOK | M_ZERO);
3716
3717         for (i = 0; i < sc->num_slices; i++) {
3718                 ss = &sc->ss[i];
3719
3720                 ss->sc = sc;
3721
3722                 /*
3723                  * Allocate per-slice rx interrupt queues
3724                  */
3725                 bytes = max_intr_slots * sizeof(*ss->rx_done.entry);
3726                 err = mxge_dma_alloc(sc, &ss->rx_done.dma, bytes, 4096);
3727                 if (err != 0) {
3728                         device_printf(sc->dev,
3729                             "alloc %d slice rx_done failed\n", i);
3730                         return err;
3731                 }
3732                 ss->rx_done.entry = ss->rx_done.dma.dmem_addr;
3733
3734                 /* 
3735                  * Allocate the per-slice firmware stats; stats
3736                  * (including tx) are used used only on the first
3737                  * slice for now
3738                  */
3739 #ifndef IFNET_BUF_RING
3740                 if (i > 0)
3741                         continue;
3742 #endif
3743
3744                 bytes = sizeof(*ss->fw_stats);
3745                 err = mxge_dma_alloc(sc, &ss->fw_stats_dma,
3746                     sizeof(*ss->fw_stats), 64);
3747                 if (err != 0) {
3748                         device_printf(sc->dev,
3749                             "alloc %d fw_stats failed\n", i);
3750                         return err;
3751                 }
3752                 ss->fw_stats = ss->fw_stats_dma.dmem_addr;
3753         }
3754         return 0;
3755 }
3756
3757 static void
3758 mxge_slice_probe(mxge_softc_t *sc)
3759 {
3760         mxge_cmd_t cmd;
3761         const char *old_fw;
3762         int msix_cnt, status, max_intr_slots;
3763
3764         sc->num_slices = 1;
3765
3766         /*
3767          * XXX
3768          *
3769          * Don't enable multiple slices if they are not enabled,
3770          * or if this is not an SMP system 
3771          */
3772         if (mxge_max_slices == 0 || mxge_max_slices == 1 || ncpus < 2)
3773                 return;
3774
3775         /* see how many MSI-X interrupts are available */
3776         msix_cnt = pci_msix_count(sc->dev);
3777         if (msix_cnt < 2)
3778                 return;
3779
3780         /* now load the slice aware firmware see what it supports */
3781         old_fw = sc->fw_name;
3782         if (old_fw == mxge_fw_aligned)
3783                 sc->fw_name = mxge_fw_rss_aligned;
3784         else
3785                 sc->fw_name = mxge_fw_rss_unaligned;
3786         status = mxge_load_firmware(sc, 0);
3787         if (status != 0) {
3788                 device_printf(sc->dev, "Falling back to a single slice\n");
3789                 return;
3790         }
3791         
3792         /* try to send a reset command to the card to see if it
3793            is alive */
3794         memset(&cmd, 0, sizeof (cmd));
3795         status = mxge_send_cmd(sc, MXGEFW_CMD_RESET, &cmd);
3796         if (status != 0) {
3797                 device_printf(sc->dev, "failed reset\n");
3798                 goto abort_with_fw;
3799         }
3800
3801         /* get rx ring size */
3802         status = mxge_send_cmd(sc, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd);
3803         if (status != 0) {
3804                 device_printf(sc->dev, "Cannot determine rx ring size\n");
3805                 goto abort_with_fw;
3806         }
3807         max_intr_slots = 2 * (cmd.data0 / sizeof (mcp_dma_addr_t));
3808
3809         /* tell it the size of the interrupt queues */
3810         cmd.data0 = max_intr_slots * sizeof (struct mcp_slot);
3811         status = mxge_send_cmd(sc, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd);
3812         if (status != 0) {
3813                 device_printf(sc->dev, "failed MXGEFW_CMD_SET_INTRQ_SIZE\n");
3814                 goto abort_with_fw;
3815         }
3816
3817         /* ask the maximum number of slices it supports */
3818         status = mxge_send_cmd(sc, MXGEFW_CMD_GET_MAX_RSS_QUEUES, &cmd);
3819         if (status != 0) {
3820                 device_printf(sc->dev,
3821                               "failed MXGEFW_CMD_GET_MAX_RSS_QUEUES\n");
3822                 goto abort_with_fw;
3823         }
3824         sc->num_slices = cmd.data0;
3825         if (sc->num_slices > msix_cnt)
3826                 sc->num_slices = msix_cnt;
3827
3828         if (mxge_max_slices == -1) {
3829                 /* cap to number of CPUs in system */
3830                 if (sc->num_slices > ncpus)
3831                         sc->num_slices = ncpus;
3832         } else {
3833                 if (sc->num_slices > mxge_max_slices)
3834                         sc->num_slices = mxge_max_slices;
3835         }
3836         /* make sure it is a power of two */
3837         while (sc->num_slices & (sc->num_slices - 1))
3838                 sc->num_slices--;
3839
3840         if (bootverbose)
3841                 device_printf(sc->dev, "using %d slices\n",
3842                               sc->num_slices);
3843         
3844         return;
3845
3846 abort_with_fw:
3847         sc->fw_name = old_fw;
3848         (void) mxge_load_firmware(sc, 0);
3849 }
3850
3851 #if 0
3852 static int
3853 mxge_add_msix_irqs(mxge_softc_t *sc)
3854 {
3855         size_t bytes;
3856         int count, err, i, rid;
3857
3858         rid = PCIR_BAR(2);
3859         sc->msix_table_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3860                                                     &rid, RF_ACTIVE);
3861
3862         if (sc->msix_table_res == NULL) {
3863                 device_printf(sc->dev, "couldn't alloc MSIX table res\n");
3864                 return ENXIO;
3865         }
3866
3867         count = sc->num_slices;
3868         err = pci_alloc_msix(sc->dev, &count);
3869         if (err != 0) {
3870                 device_printf(sc->dev, "pci_alloc_msix: failed, wanted %d"
3871                               "err = %d \n", sc->num_slices, err);
3872                 goto abort_with_msix_table;
3873         }
3874         if (count < sc->num_slices) {
3875                 device_printf(sc->dev, "pci_alloc_msix: need %d, got %d\n",
3876                               count, sc->num_slices);
3877                 device_printf(sc->dev,
3878                               "Try setting hw.mxge.max_slices to %d\n",
3879                               count);
3880                 err = ENOSPC;
3881                 goto abort_with_msix;
3882         }
3883         bytes = sizeof (*sc->msix_irq_res) * sc->num_slices;
3884         sc->msix_irq_res = kmalloc(bytes, M_DEVBUF, M_NOWAIT|M_ZERO);
3885         if (sc->msix_irq_res == NULL) {
3886                 err = ENOMEM;
3887                 goto abort_with_msix;
3888         }
3889
3890         for (i = 0; i < sc->num_slices; i++) {
3891                 rid = i + 1;
3892                 sc->msix_irq_res[i] = bus_alloc_resource_any(sc->dev,
3893                                                           SYS_RES_IRQ,
3894                                                           &rid, RF_ACTIVE);
3895                 if (sc->msix_irq_res[i] == NULL) {
3896                         device_printf(sc->dev, "couldn't allocate IRQ res"
3897                                       " for message %d\n", i);
3898                         err = ENXIO;
3899                         goto abort_with_res;
3900                 }
3901         }
3902
3903         bytes = sizeof (*sc->msix_ih) * sc->num_slices;
3904         sc->msix_ih =  kmalloc(bytes, M_DEVBUF, M_NOWAIT|M_ZERO);
3905
3906         for (i = 0; i < sc->num_slices; i++) {
3907                 err = bus_setup_intr(sc->dev, sc->msix_irq_res[i], 
3908                                      INTR_MPSAFE,
3909                                      mxge_intr, &sc->ss[i], &sc->msix_ih[i],
3910                                      sc->ifp->if_serializer);
3911                 if (err != 0) {
3912                         device_printf(sc->dev, "couldn't setup intr for "
3913                                       "message %d\n", i);
3914                         goto abort_with_intr;
3915                 }
3916         }
3917
3918         if (bootverbose) {
3919                 device_printf(sc->dev, "using %d msix IRQs:",
3920                               sc->num_slices);
3921                 for (i = 0; i < sc->num_slices; i++)
3922                         kprintf(" %ld",  rman_get_start(sc->msix_irq_res[i]));
3923                 kprintf("\n");
3924         }
3925         return (0);
3926
3927 abort_with_intr:
3928         for (i = 0; i < sc->num_slices; i++) {
3929                 if (sc->msix_ih[i] != NULL) {
3930                         bus_teardown_intr(sc->dev, sc->msix_irq_res[i],
3931                                           sc->msix_ih[i]);
3932                         sc->msix_ih[i] = NULL;
3933                 }
3934         }
3935         kfree(sc->msix_ih, M_DEVBUF);
3936
3937
3938 abort_with_res:
3939         for (i = 0; i < sc->num_slices; i++) {
3940                 rid = i + 1;
3941                 if (sc->msix_irq_res[i] != NULL)
3942                         bus_release_resource(sc->dev, SYS_RES_IRQ, rid,
3943                                              sc->msix_irq_res[i]);
3944                 sc->msix_irq_res[i] = NULL;
3945         }
3946         kfree(sc->msix_irq_res, M_DEVBUF);
3947
3948
3949 abort_with_msix:
3950         pci_release_msi(sc->dev);
3951
3952 abort_with_msix_table:
3953         bus_release_resource(sc->dev, SYS_RES_MEMORY, PCIR_BAR(2),
3954                              sc->msix_table_res);
3955
3956         return err;
3957 }
3958 #endif
3959
3960 static int
3961 mxge_add_single_irq(mxge_softc_t *sc)
3962 {
3963         u_int irq_flags;
3964
3965         sc->irq_type = pci_alloc_1intr(sc->dev, mxge_msi_enable,
3966             &sc->irq_rid, &irq_flags);
3967
3968         sc->irq_res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ,
3969             &sc->irq_rid, irq_flags);
3970         if (sc->irq_res == NULL) {
3971                 device_printf(sc->dev, "could not alloc interrupt\n");
3972                 return ENXIO;
3973         }
3974
3975         return bus_setup_intr(sc->dev, sc->irq_res, INTR_MPSAFE,
3976             mxge_intr, &sc->ss[0], &sc->ih, sc->ifp->if_serializer);
3977 }
3978
3979 #if 0
3980 static void
3981 mxge_rem_msix_irqs(mxge_softc_t *sc)
3982 {
3983         int i, rid;
3984
3985         for (i = 0; i < sc->num_slices; i++) {
3986                 if (sc->msix_ih[i] != NULL) {
3987                         bus_teardown_intr(sc->dev, sc->msix_irq_res[i],
3988                                           sc->msix_ih[i]);
3989                         sc->msix_ih[i] = NULL;
3990                 }
3991         }
3992         kfree(sc->msix_ih, M_DEVBUF);
3993
3994         for (i = 0; i < sc->num_slices; i++) {
3995                 rid = i + 1;
3996                 if (sc->msix_irq_res[i] != NULL)
3997                         bus_release_resource(sc->dev, SYS_RES_IRQ, rid,
3998                                              sc->msix_irq_res[i]);
3999                 sc->msix_irq_res[i] = NULL;
4000         }
4001         kfree(sc->msix_irq_res, M_DEVBUF);
4002
4003         bus_release_resource(sc->dev, SYS_RES_MEMORY, PCIR_BAR(2),
4004                              sc->msix_table_res);
4005
4006         pci_release_msi(sc->dev);
4007         return;
4008 }
4009 #endif
4010
4011 static int
4012 mxge_add_irq(mxge_softc_t *sc)
4013 {
4014 #if 0
4015         int err;
4016
4017         if (sc->num_slices > 1)
4018                 err = mxge_add_msix_irqs(sc);
4019         else
4020                 err = mxge_add_single_irq(sc);
4021         
4022         if (0 && err == 0 && sc->num_slices > 1) {
4023                 mxge_rem_msix_irqs(sc);
4024                 err = mxge_add_msix_irqs(sc);
4025         }
4026         return err;
4027 #else
4028         return mxge_add_single_irq(sc);
4029 #endif
4030 }
4031
4032 static int 
4033 mxge_attach(device_t dev)
4034 {
4035         mxge_softc_t *sc = device_get_softc(dev);
4036         struct ifnet *ifp = &sc->arpcom.ac_if;
4037         int err, rid;
4038
4039         /*
4040          * Avoid rewriting half the lines in this file to use
4041          * &sc->arpcom.ac_if instead
4042          */
4043         sc->ifp = ifp;
4044         sc->dev = dev;
4045         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
4046         ifmedia_init(&sc->media, 0, mxge_media_change, mxge_media_status);
4047
4048         mxge_fetch_tunables(sc);
4049
4050         err = bus_dma_tag_create(NULL,                  /* parent */
4051                                  1,                     /* alignment */
4052                                  0,                     /* boundary */
4053                                  BUS_SPACE_MAXADDR,     /* low */
4054                                  BUS_SPACE_MAXADDR,     /* high */
4055                                  NULL, NULL,            /* filter */
4056                                  BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
4057                                  0,                     /* num segs */
4058                                  BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
4059                                  0,                     /* flags */
4060                                  &sc->parent_dmat);     /* tag */
4061         if (err != 0) {
4062                 device_printf(dev, "Err %d allocating parent dmat\n", err);
4063                 goto failed;
4064         }
4065
4066         callout_init_mp(&sc->co_hdl);
4067
4068         mxge_setup_cfg_space(sc);
4069
4070         /*
4071          * Map the board into the kernel
4072          */
4073         rid = PCIR_BARS;
4074         sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
4075             &rid, RF_ACTIVE);
4076         if (sc->mem_res == NULL) {
4077                 device_printf(dev, "could not map memory\n");
4078                 err = ENXIO;
4079                 goto failed;
4080         }
4081
4082         sc->sram = rman_get_virtual(sc->mem_res);
4083         sc->sram_size = 2*1024*1024 - (2*(48*1024)+(32*1024)) - 0x100;
4084         if (sc->sram_size > rman_get_size(sc->mem_res)) {
4085                 device_printf(dev, "impossible memory region size %ld\n",
4086                     rman_get_size(sc->mem_res));
4087                 err = ENXIO;
4088                 goto failed;
4089         }
4090
4091         /*
4092          * Make NULL terminated copy of the EEPROM strings section of
4093          * lanai SRAM
4094          */
4095         bzero(sc->eeprom_strings, MXGE_EEPROM_STRINGS_SIZE);
4096         bus_space_read_region_1(rman_get_bustag(sc->mem_res),
4097             rman_get_bushandle(sc->mem_res),
4098             sc->sram_size - MXGE_EEPROM_STRINGS_SIZE,
4099             sc->eeprom_strings, MXGE_EEPROM_STRINGS_SIZE - 2);
4100         err = mxge_parse_strings(sc);
4101         if (err != 0) {
4102                 device_printf(dev, "parse EEPROM string failed\n");
4103                 goto failed;
4104         }
4105
4106         /*
4107          * Enable write combining for efficient use of PCIe bus
4108          */
4109         mxge_enable_wc(sc);
4110
4111         /*
4112          * Allocate the out of band DMA memory
4113          */
4114         err = mxge_dma_alloc(sc, &sc->cmd_dma, sizeof(mxge_cmd_t), 64);
4115         if (err != 0) {
4116                 device_printf(dev, "alloc cmd DMA buf failed\n");
4117                 goto failed;
4118         }
4119         sc->cmd = sc->cmd_dma.dmem_addr;
4120
4121         err = mxge_dma_alloc(sc, &sc->zeropad_dma, 64, 64);
4122         if (err != 0) {
4123                 device_printf(dev, "alloc zeropad DMA buf failed\n");
4124                 goto failed;
4125         }
4126
4127         err = mxge_dma_alloc(sc, &sc->dmabench_dma, 4096, 4096);
4128         if (err != 0) {
4129                 device_printf(dev, "alloc dmabench DMA buf failed\n");
4130                 goto failed;
4131         }
4132
4133         /* Select & load the firmware */
4134         err = mxge_select_firmware(sc);
4135         if (err != 0) {
4136                 device_printf(dev, "select firmware failed\n");
4137                 goto failed;
4138         }
4139
4140         mxge_slice_probe(sc);
4141         err = mxge_alloc_slices(sc);
4142         if (err != 0) {
4143                 device_printf(dev, "alloc slices failed\n");
4144                 goto failed;
4145         }
4146
4147         err = mxge_reset(sc, 0);
4148         if (err != 0) {
4149                 device_printf(dev, "reset failed\n");
4150                 goto failed;
4151         }
4152
4153         err = mxge_alloc_rings(sc);
4154         if (err != 0) {
4155                 device_printf(dev, "failed to allocate rings\n");
4156                 goto failed;
4157         }
4158
4159         ifp->if_baudrate = IF_Gbps(10UL);
4160         ifp->if_capabilities = IFCAP_RXCSUM | IFCAP_TXCSUM | IFCAP_TSO;
4161         ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_TSO;
4162
4163         ifp->if_capabilities |= IFCAP_VLAN_MTU;
4164 #if 0
4165         /* Well, its software, sigh */
4166         ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING;
4167 #endif
4168         ifp->if_capenable = ifp->if_capabilities;
4169
4170         ifp->if_softc = sc;
4171         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
4172         ifp->if_init = mxge_init;
4173         ifp->if_ioctl = mxge_ioctl;
4174         ifp->if_start = mxge_start;
4175         ifp->if_watchdog = mxge_watchdog;
4176
4177         /* Increase TSO burst length */
4178         ifp->if_tsolen = (32 * ETHERMTU);
4179
4180         /* Initialise the ifmedia structure */
4181         mxge_media_init(sc);
4182         mxge_media_probe(sc);
4183
4184         ether_ifattach(ifp, sc->mac_addr, NULL);
4185
4186         /*
4187          * XXX
4188          * We are not ready to do "gather" jumbo frame, so
4189          * limit MTU to MJUMPAGESIZE
4190          */
4191         sc->max_mtu = MJUMPAGESIZE -
4192             ETHER_HDR_LEN - EVL_ENCAPLEN - MXGEFW_PAD - 1;
4193         sc->dying = 0;
4194
4195         /* must come after ether_ifattach() */
4196         err = mxge_add_irq(sc);
4197         if (err != 0) {
4198                 device_printf(dev, "alloc and setup intr failed\n");
4199                 ether_ifdetach(ifp);
4200                 goto failed;
4201         }
4202         ifq_set_cpuid(&ifp->if_snd, rman_get_cpuid(sc->irq_res));
4203
4204         mxge_add_sysctls(sc);
4205
4206         callout_reset(&sc->co_hdl, mxge_ticks, mxge_tick, sc);
4207         return 0;
4208
4209 failed:
4210         mxge_detach(dev);
4211         return err;
4212 }
4213
4214 static int
4215 mxge_detach(device_t dev)
4216 {
4217         mxge_softc_t *sc = device_get_softc(dev);
4218
4219         if (device_is_attached(dev)) {
4220                 struct ifnet *ifp = sc->ifp;
4221
4222                 lwkt_serialize_enter(ifp->if_serializer);
4223
4224                 sc->dying = 1;
4225                 if (ifp->if_flags & IFF_RUNNING)
4226                         mxge_close(sc, 1);
4227                 callout_stop(&sc->co_hdl);
4228
4229                 bus_teardown_intr(sc->dev, sc->irq_res, sc->ih);
4230
4231                 lwkt_serialize_exit(ifp->if_serializer);
4232
4233                 callout_terminate(&sc->co_hdl);
4234
4235                 ether_ifdetach(ifp);
4236         }
4237         ifmedia_removeall(&sc->media);
4238
4239         if (sc->cmd != NULL && sc->zeropad_dma.dmem_addr != NULL &&
4240             sc->sram != NULL)
4241                 mxge_dummy_rdma(sc, 0);
4242
4243         mxge_rem_sysctls(sc);
4244         mxge_free_rings(sc);
4245
4246         /* MUST after sysctls and rings are freed */
4247         mxge_free_slices(sc);
4248
4249         if (sc->dmabench_dma.dmem_addr != NULL)
4250                 mxge_dma_free(&sc->dmabench_dma);
4251         if (sc->zeropad_dma.dmem_addr != NULL)
4252                 mxge_dma_free(&sc->zeropad_dma);
4253         if (sc->cmd_dma.dmem_addr != NULL)
4254                 mxge_dma_free(&sc->cmd_dma);
4255
4256         if (sc->irq_res != NULL) {
4257                 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,
4258                     sc->irq_res);
4259         }
4260         if (sc->irq_type == PCI_INTR_TYPE_MSI)
4261                 pci_release_msi(dev);
4262
4263         if (sc->mem_res != NULL) {
4264                 bus_release_resource(dev, SYS_RES_MEMORY, PCIR_BARS,
4265                     sc->mem_res);
4266         }
4267
4268         if (sc->parent_dmat != NULL)
4269                 bus_dma_tag_destroy(sc->parent_dmat);
4270
4271         return 0;
4272 }
4273
4274 static int
4275 mxge_shutdown(device_t dev)
4276 {
4277         return 0;
4278 }