5d28e1dcbabe12b707f98a18879ede89752cb53d
[dragonfly.git] / sys / dev / netif / jme / if_jme.c
1 /*-
2  * Copyright (c) 2008, Pyun YongHyeon <yongari@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/dev/jme/if_jme.c,v 1.2 2008/07/18 04:20:48 yongari Exp $
28  * $DragonFly: src/sys/dev/netif/jme/if_jme.c,v 1.8 2008/09/17 08:51:29 sephe Exp $
29  */
30
31 #include <sys/param.h>
32 #include <sys/endian.h>
33 #include <sys/kernel.h>
34 #include <sys/bus.h>
35 #include <sys/interrupt.h>
36 #include <sys/malloc.h>
37 #include <sys/proc.h>
38 #include <sys/rman.h>
39 #include <sys/serialize.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #include <sys/sysctl.h>
43
44 #include <net/ethernet.h>
45 #include <net/if.h>
46 #include <net/bpf.h>
47 #include <net/if_arp.h>
48 #include <net/if_dl.h>
49 #include <net/if_media.h>
50 #include <net/ifq_var.h>
51 #include <net/vlan/if_vlan_var.h>
52 #include <net/vlan/if_vlan_ether.h>
53
54 #include <dev/netif/mii_layer/miivar.h>
55 #include <dev/netif/mii_layer/jmphyreg.h>
56
57 #include <bus/pci/pcireg.h>
58 #include <bus/pci/pcivar.h>
59 #include <bus/pci/pcidevs.h>
60
61 #include <dev/netif/jme/if_jmereg.h>
62 #include <dev/netif/jme/if_jmevar.h>
63
64 #include "miibus_if.h"
65
66 /* Define the following to disable printing Rx errors. */
67 #undef  JME_SHOW_ERRORS
68
69 #define JME_CSUM_FEATURES       (CSUM_IP | CSUM_TCP | CSUM_UDP)
70
71 static int      jme_probe(device_t);
72 static int      jme_attach(device_t);
73 static int      jme_detach(device_t);
74 static int      jme_shutdown(device_t);
75 static int      jme_suspend(device_t);
76 static int      jme_resume(device_t);
77
78 static int      jme_miibus_readreg(device_t, int, int);
79 static int      jme_miibus_writereg(device_t, int, int, int);
80 static void     jme_miibus_statchg(device_t);
81
82 static void     jme_init(void *);
83 static int      jme_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
84 static void     jme_start(struct ifnet *);
85 static void     jme_watchdog(struct ifnet *);
86 static void     jme_mediastatus(struct ifnet *, struct ifmediareq *);
87 static int      jme_mediachange(struct ifnet *);
88
89 static void     jme_intr(void *);
90 static void     jme_txeof(struct jme_softc *);
91 static void     jme_rxeof(struct jme_softc *);
92
93 static int      jme_dma_alloc(struct jme_softc *);
94 static void     jme_dma_free(struct jme_softc *);
95 static void     jme_dmamap_ring_cb(void *, bus_dma_segment_t *, int, int);
96 static void     jme_dmamap_buf_cb(void *, bus_dma_segment_t *, int,
97                                   bus_size_t, int);
98 static int      jme_init_rx_ring(struct jme_softc *);
99 static void     jme_init_tx_ring(struct jme_softc *);
100 static void     jme_init_ssb(struct jme_softc *);
101 static int      jme_newbuf(struct jme_softc *, struct jme_rxdesc *, int);
102 static int      jme_encap(struct jme_softc *, struct mbuf **);
103 static void     jme_rxpkt(struct jme_softc *);
104
105 static void     jme_tick(void *);
106 static void     jme_stop(struct jme_softc *);
107 static void     jme_reset(struct jme_softc *);
108 static void     jme_set_vlan(struct jme_softc *);
109 static void     jme_set_filter(struct jme_softc *);
110 static void     jme_stop_tx(struct jme_softc *);
111 static void     jme_stop_rx(struct jme_softc *);
112 static void     jme_mac_config(struct jme_softc *);
113 static void     jme_reg_macaddr(struct jme_softc *, uint8_t[]);
114 static int      jme_eeprom_macaddr(struct jme_softc *, uint8_t[]);
115 static int      jme_eeprom_read_byte(struct jme_softc *, uint8_t, uint8_t *);
116 #ifdef notyet
117 static void     jme_setwol(struct jme_softc *);
118 static void     jme_setlinkspeed(struct jme_softc *);
119 #endif
120
121 static void     jme_sysctl_node(struct jme_softc *);
122 static int      sysctl_hw_jme_tx_coal_to(SYSCTL_HANDLER_ARGS);
123 static int      sysctl_hw_jme_tx_coal_pkt(SYSCTL_HANDLER_ARGS);
124 static int      sysctl_hw_jme_rx_coal_to(SYSCTL_HANDLER_ARGS);
125 static int      sysctl_hw_jme_rx_coal_pkt(SYSCTL_HANDLER_ARGS);
126
127 /*
128  * Devices supported by this driver.
129  */
130 static const struct jme_dev {
131         uint16_t        jme_vendorid;
132         uint16_t        jme_deviceid;
133         uint32_t        jme_caps;
134         const char      *jme_name;
135 } jme_devs[] = {
136         { PCI_VENDOR_JMICRON, PCI_PRODUCT_JMICRON_JMC250,
137             JME_CAP_JUMBO,
138             "JMicron Inc, JMC250 Gigabit Ethernet" },
139         { PCI_VENDOR_JMICRON, PCI_PRODUCT_JMICRON_JMC260,
140             JME_CAP_FASTETH,
141             "JMicron Inc, JMC260 Fast Ethernet" },
142         { 0, 0, 0, NULL }
143 };
144
145 static device_method_t jme_methods[] = {
146         /* Device interface. */
147         DEVMETHOD(device_probe,         jme_probe),
148         DEVMETHOD(device_attach,        jme_attach),
149         DEVMETHOD(device_detach,        jme_detach),
150         DEVMETHOD(device_shutdown,      jme_shutdown),
151         DEVMETHOD(device_suspend,       jme_suspend),
152         DEVMETHOD(device_resume,        jme_resume),
153
154         /* Bus interface. */
155         DEVMETHOD(bus_print_child,      bus_generic_print_child),
156         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
157
158         /* MII interface. */
159         DEVMETHOD(miibus_readreg,       jme_miibus_readreg),
160         DEVMETHOD(miibus_writereg,      jme_miibus_writereg),
161         DEVMETHOD(miibus_statchg,       jme_miibus_statchg),
162
163         { NULL, NULL }
164 };
165
166 static driver_t jme_driver = {
167         "jme",
168         jme_methods,
169         sizeof(struct jme_softc)
170 };
171
172 static devclass_t jme_devclass;
173
174 DECLARE_DUMMY_MODULE(if_jme);
175 MODULE_DEPEND(if_jme, miibus, 1, 1, 1);
176 DRIVER_MODULE(if_jme, pci, jme_driver, jme_devclass, 0, 0);
177 DRIVER_MODULE(miibus, jme, miibus_driver, miibus_devclass, 0, 0);
178
179 /*
180  *      Read a PHY register on the MII of the JMC250.
181  */
182 static int
183 jme_miibus_readreg(device_t dev, int phy, int reg)
184 {
185         struct jme_softc *sc = device_get_softc(dev);
186         uint32_t val;
187         int i;
188
189         /* For FPGA version, PHY address 0 should be ignored. */
190         if (sc->jme_caps & JME_CAP_FPGA) {
191                 if (phy == 0)
192                         return (0);
193         } else {
194                 if (sc->jme_phyaddr != phy)
195                         return (0);
196         }
197
198         CSR_WRITE_4(sc, JME_SMI, SMI_OP_READ | SMI_OP_EXECUTE |
199             SMI_PHY_ADDR(phy) | SMI_REG_ADDR(reg));
200
201         for (i = JME_PHY_TIMEOUT; i > 0; i--) {
202                 DELAY(1);
203                 if (((val = CSR_READ_4(sc, JME_SMI)) & SMI_OP_EXECUTE) == 0)
204                         break;
205         }
206         if (i == 0) {
207                 device_printf(sc->jme_dev, "phy read timeout: "
208                               "phy %d, reg %d\n", phy, reg);
209                 return (0);
210         }
211
212         return ((val & SMI_DATA_MASK) >> SMI_DATA_SHIFT);
213 }
214
215 /*
216  *      Write a PHY register on the MII of the JMC250.
217  */
218 static int
219 jme_miibus_writereg(device_t dev, int phy, int reg, int val)
220 {
221         struct jme_softc *sc = device_get_softc(dev);
222         int i;
223
224         /* For FPGA version, PHY address 0 should be ignored. */
225         if (sc->jme_caps & JME_CAP_FPGA) {
226                 if (phy == 0)
227                         return (0);
228         } else {
229                 if (sc->jme_phyaddr != phy)
230                         return (0);
231         }
232
233         CSR_WRITE_4(sc, JME_SMI, SMI_OP_WRITE | SMI_OP_EXECUTE |
234             ((val << SMI_DATA_SHIFT) & SMI_DATA_MASK) |
235             SMI_PHY_ADDR(phy) | SMI_REG_ADDR(reg));
236
237         for (i = JME_PHY_TIMEOUT; i > 0; i--) {
238                 DELAY(1);
239                 if (((val = CSR_READ_4(sc, JME_SMI)) & SMI_OP_EXECUTE) == 0)
240                         break;
241         }
242         if (i == 0) {
243                 device_printf(sc->jme_dev, "phy write timeout: "
244                               "phy %d, reg %d\n", phy, reg);
245         }
246
247         return (0);
248 }
249
250 /*
251  *      Callback from MII layer when media changes.
252  */
253 static void
254 jme_miibus_statchg(device_t dev)
255 {
256         struct jme_softc *sc = device_get_softc(dev);
257         struct ifnet *ifp = &sc->arpcom.ac_if;
258         struct mii_data *mii;
259         struct jme_txdesc *txd;
260         bus_addr_t paddr;
261         int i;
262
263         ASSERT_SERIALIZED(ifp->if_serializer);
264
265         if ((ifp->if_flags & IFF_RUNNING) == 0)
266                 return;
267
268         mii = device_get_softc(sc->jme_miibus);
269
270         sc->jme_flags &= ~JME_FLAG_LINK;
271         if ((mii->mii_media_status & IFM_AVALID) != 0) {
272                 switch (IFM_SUBTYPE(mii->mii_media_active)) {
273                 case IFM_10_T:
274                 case IFM_100_TX:
275                         sc->jme_flags |= JME_FLAG_LINK;
276                         break;
277                 case IFM_1000_T:
278                         if (sc->jme_caps & JME_CAP_FASTETH)
279                                 break;
280                         sc->jme_flags |= JME_FLAG_LINK;
281                         break;
282                 default:
283                         break;
284                 }
285         }
286
287         /*
288          * Disabling Rx/Tx MACs have a side-effect of resetting
289          * JME_TXNDA/JME_RXNDA register to the first address of
290          * Tx/Rx descriptor address. So driver should reset its
291          * internal procucer/consumer pointer and reclaim any
292          * allocated resources.  Note, just saving the value of
293          * JME_TXNDA and JME_RXNDA registers before stopping MAC
294          * and restoring JME_TXNDA/JME_RXNDA register is not
295          * sufficient to make sure correct MAC state because
296          * stopping MAC operation can take a while and hardware
297          * might have updated JME_TXNDA/JME_RXNDA registers
298          * during the stop operation.
299          */
300
301         /* Disable interrupts */
302         CSR_WRITE_4(sc, JME_INTR_MASK_CLR, JME_INTRS);
303
304         /* Stop driver */
305         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
306         ifp->if_timer = 0;
307         callout_stop(&sc->jme_tick_ch);
308
309         /* Stop receiver/transmitter. */
310         jme_stop_rx(sc);
311         jme_stop_tx(sc);
312
313         jme_rxeof(sc);
314         if (sc->jme_cdata.jme_rxhead != NULL)
315                 m_freem(sc->jme_cdata.jme_rxhead);
316         JME_RXCHAIN_RESET(sc);
317
318         jme_txeof(sc);
319         if (sc->jme_cdata.jme_tx_cnt != 0) {
320                 /* Remove queued packets for transmit. */
321                 for (i = 0; i < JME_TX_RING_CNT; i++) {
322                         txd = &sc->jme_cdata.jme_txdesc[i];
323                         if (txd->tx_m != NULL) {
324                                 bus_dmamap_unload(
325                                     sc->jme_cdata.jme_tx_tag,
326                                     txd->tx_dmamap);
327                                 m_freem(txd->tx_m);
328                                 txd->tx_m = NULL;
329                                 txd->tx_ndesc = 0;
330                                 ifp->if_oerrors++;
331                         }
332                 }
333         }
334
335         /*
336          * Reuse configured Rx descriptors and reset
337          * procuder/consumer index.
338          */
339         sc->jme_cdata.jme_rx_cons = 0;
340
341         jme_init_tx_ring(sc);
342
343         /* Initialize shadow status block. */
344         jme_init_ssb(sc);
345
346         /* Program MAC with resolved speed/duplex/flow-control. */
347         if (sc->jme_flags & JME_FLAG_LINK) {
348                 jme_mac_config(sc);
349
350                 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr);
351                 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr);
352
353                 /* Set Tx ring address to the hardware. */
354                 paddr = JME_TX_RING_ADDR(sc, 0);
355                 CSR_WRITE_4(sc, JME_TXDBA_HI, JME_ADDR_HI(paddr));
356                 CSR_WRITE_4(sc, JME_TXDBA_LO, JME_ADDR_LO(paddr));
357
358                 /* Set Rx ring address to the hardware. */
359                 paddr = JME_RX_RING_ADDR(sc, 0);
360                 CSR_WRITE_4(sc, JME_RXDBA_HI, JME_ADDR_HI(paddr));
361                 CSR_WRITE_4(sc, JME_RXDBA_LO, JME_ADDR_LO(paddr));
362
363                 /* Restart receiver/transmitter. */
364                 CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr | RXCSR_RX_ENB |
365                     RXCSR_RXQ_START);
366                 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr | TXCSR_TX_ENB);
367         }
368
369         ifp->if_flags |= IFF_RUNNING;
370         ifp->if_flags &= ~IFF_OACTIVE;
371         callout_reset(&sc->jme_tick_ch, hz, jme_tick, sc);
372
373         /* Reenable interrupts. */
374         CSR_WRITE_4(sc, JME_INTR_MASK_SET, JME_INTRS);
375 }
376
377 /*
378  *      Get the current interface media status.
379  */
380 static void
381 jme_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
382 {
383         struct jme_softc *sc = ifp->if_softc;
384         struct mii_data *mii = device_get_softc(sc->jme_miibus);
385
386         ASSERT_SERIALIZED(ifp->if_serializer);
387
388         mii_pollstat(mii);
389         ifmr->ifm_status = mii->mii_media_status;
390         ifmr->ifm_active = mii->mii_media_active;
391 }
392
393 /*
394  *      Set hardware to newly-selected media.
395  */
396 static int
397 jme_mediachange(struct ifnet *ifp)
398 {
399         struct jme_softc *sc = ifp->if_softc;
400         struct mii_data *mii = device_get_softc(sc->jme_miibus);
401         int error;
402
403         ASSERT_SERIALIZED(ifp->if_serializer);
404
405         if (mii->mii_instance != 0) {
406                 struct mii_softc *miisc;
407
408                 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
409                         mii_phy_reset(miisc);
410         }
411         error = mii_mediachg(mii);
412
413         return (error);
414 }
415
416 static int
417 jme_probe(device_t dev)
418 {
419         const struct jme_dev *sp;
420         uint16_t vid, did;
421
422         vid = pci_get_vendor(dev);
423         did = pci_get_device(dev);
424         for (sp = jme_devs; sp->jme_name != NULL; ++sp) {
425                 if (vid == sp->jme_vendorid && did == sp->jme_deviceid) {
426                         struct jme_softc *sc = device_get_softc(dev);
427
428                         sc->jme_caps = sp->jme_caps;
429                         if (did == PCI_PRODUCT_JMICRON_JMC250 &&
430                             pci_get_revid(dev) == JME_REV_JMC250_A2)
431                                 sc->jme_caps |= JME_CAP_EXTFIFO;
432
433                         device_set_desc(dev, sp->jme_name);
434                         return (0);
435                 }
436         }
437         return (ENXIO);
438 }
439
440 static int
441 jme_eeprom_read_byte(struct jme_softc *sc, uint8_t addr, uint8_t *val)
442 {
443         uint32_t reg;
444         int i;
445
446         *val = 0;
447         for (i = JME_TIMEOUT; i > 0; i--) {
448                 reg = CSR_READ_4(sc, JME_SMBCSR);
449                 if ((reg & SMBCSR_HW_BUSY_MASK) == SMBCSR_HW_IDLE)
450                         break;
451                 DELAY(1);
452         }
453
454         if (i == 0) {
455                 device_printf(sc->jme_dev, "EEPROM idle timeout!\n");
456                 return (ETIMEDOUT);
457         }
458
459         reg = ((uint32_t)addr << SMBINTF_ADDR_SHIFT) & SMBINTF_ADDR_MASK;
460         CSR_WRITE_4(sc, JME_SMBINTF, reg | SMBINTF_RD | SMBINTF_CMD_TRIGGER);
461         for (i = JME_TIMEOUT; i > 0; i--) {
462                 DELAY(1);
463                 reg = CSR_READ_4(sc, JME_SMBINTF);
464                 if ((reg & SMBINTF_CMD_TRIGGER) == 0)
465                         break;
466         }
467
468         if (i == 0) {
469                 device_printf(sc->jme_dev, "EEPROM read timeout!\n");
470                 return (ETIMEDOUT);
471         }
472
473         reg = CSR_READ_4(sc, JME_SMBINTF);
474         *val = (reg & SMBINTF_RD_DATA_MASK) >> SMBINTF_RD_DATA_SHIFT;
475
476         return (0);
477 }
478
479 static int
480 jme_eeprom_macaddr(struct jme_softc *sc, uint8_t eaddr[])
481 {
482         uint8_t fup, reg, val;
483         uint32_t offset;
484         int match;
485
486         offset = 0;
487         if (jme_eeprom_read_byte(sc, offset++, &fup) != 0 ||
488             fup != JME_EEPROM_SIG0)
489                 return (ENOENT);
490         if (jme_eeprom_read_byte(sc, offset++, &fup) != 0 ||
491             fup != JME_EEPROM_SIG1)
492                 return (ENOENT);
493         match = 0;
494         do {
495                 if (jme_eeprom_read_byte(sc, offset, &fup) != 0)
496                         break;
497                 /* Check for the end of EEPROM descriptor. */
498                 if ((fup & JME_EEPROM_DESC_END) == JME_EEPROM_DESC_END)
499                         break;
500                 if ((uint8_t)JME_EEPROM_MKDESC(JME_EEPROM_FUNC0,
501                     JME_EEPROM_PAGE_BAR1) == fup) {
502                         if (jme_eeprom_read_byte(sc, offset + 1, &reg) != 0)
503                                 break;
504                         if (reg >= JME_PAR0 &&
505                             reg < JME_PAR0 + ETHER_ADDR_LEN) {
506                                 if (jme_eeprom_read_byte(sc, offset + 2,
507                                     &val) != 0)
508                                         break;
509                                 eaddr[reg - JME_PAR0] = val;
510                                 match++;
511                         }
512                 }
513                 /* Try next eeprom descriptor. */
514                 offset += JME_EEPROM_DESC_BYTES;
515         } while (match != ETHER_ADDR_LEN && offset < JME_EEPROM_END);
516
517         if (match == ETHER_ADDR_LEN)
518                 return (0);
519
520         return (ENOENT);
521 }
522
523 static void
524 jme_reg_macaddr(struct jme_softc *sc, uint8_t eaddr[])
525 {
526         uint32_t par0, par1;
527
528         /* Read station address. */
529         par0 = CSR_READ_4(sc, JME_PAR0);
530         par1 = CSR_READ_4(sc, JME_PAR1);
531         par1 &= 0xFFFF;
532         if ((par0 == 0 && par1 == 0) || (par0 & 0x1)) {
533                 device_printf(sc->jme_dev,
534                     "generating fake ethernet address.\n");
535                 par0 = karc4random();
536                 /* Set OUI to JMicron. */
537                 eaddr[0] = 0x00;
538                 eaddr[1] = 0x1B;
539                 eaddr[2] = 0x8C;
540                 eaddr[3] = (par0 >> 16) & 0xff;
541                 eaddr[4] = (par0 >> 8) & 0xff;
542                 eaddr[5] = par0 & 0xff;
543         } else {
544                 eaddr[0] = (par0 >> 0) & 0xFF;
545                 eaddr[1] = (par0 >> 8) & 0xFF;
546                 eaddr[2] = (par0 >> 16) & 0xFF;
547                 eaddr[3] = (par0 >> 24) & 0xFF;
548                 eaddr[4] = (par1 >> 0) & 0xFF;
549                 eaddr[5] = (par1 >> 8) & 0xFF;
550         }
551 }
552
553 static int
554 jme_attach(device_t dev)
555 {
556         struct jme_softc *sc = device_get_softc(dev);
557         struct ifnet *ifp = &sc->arpcom.ac_if;
558         uint32_t reg;
559         uint8_t pcie_ptr;
560         int error = 0;
561         uint8_t eaddr[ETHER_ADDR_LEN];
562
563         sc->jme_dev = dev;
564         ifp = &sc->arpcom.ac_if;
565         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
566
567         callout_init(&sc->jme_tick_ch);
568
569 #ifndef BURN_BRIDGES
570         if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
571                 uint32_t irq, mem;
572
573                 irq = pci_read_config(dev, PCIR_INTLINE, 4);
574                 mem = pci_read_config(dev, JME_PCIR_BAR, 4);
575
576                 device_printf(dev, "chip is in D%d power mode "
577                     "-- setting to D0\n", pci_get_powerstate(dev));
578
579                 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
580
581                 pci_write_config(dev, PCIR_INTLINE, irq, 4);
582                 pci_write_config(dev, JME_PCIR_BAR, mem, 4);
583         }
584 #endif  /* !BURN_BRIDGE */
585
586         /* Enable bus mastering */
587         pci_enable_busmaster(dev);
588
589         /*
590          * Allocate IO memory
591          *
592          * JMC250 supports both memory mapped and I/O register space
593          * access.  Because I/O register access should use different
594          * BARs to access registers it's waste of time to use I/O
595          * register spce access.  JMC250 uses 16K to map entire memory
596          * space.
597          */
598         sc->jme_mem_rid = JME_PCIR_BAR;
599         sc->jme_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
600                                                  &sc->jme_mem_rid, RF_ACTIVE);
601         if (sc->jme_mem_res == NULL) {
602                 device_printf(dev, "can't allocate IO memory\n");
603                 return ENXIO;
604         }
605         sc->jme_mem_bt = rman_get_bustag(sc->jme_mem_res);
606         sc->jme_mem_bh = rman_get_bushandle(sc->jme_mem_res);
607
608         /*
609          * Allocate IRQ
610          */
611         sc->jme_irq_rid = 0;
612         sc->jme_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
613                                                  &sc->jme_irq_rid,
614                                                  RF_SHAREABLE | RF_ACTIVE);
615         if (sc->jme_irq_res == NULL) {
616                 device_printf(dev, "can't allocate irq\n");
617                 error = ENXIO;
618                 goto fail;
619         }
620
621         /*
622          * Extract FPGA revision
623          */
624         reg = CSR_READ_4(sc, JME_CHIPMODE);
625         if (((reg & CHIPMODE_FPGA_REV_MASK) >> CHIPMODE_FPGA_REV_SHIFT) !=
626             CHIPMODE_NOT_FPGA) {
627                 sc->jme_caps |= JME_CAP_FPGA;
628                 if (bootverbose) {
629                         device_printf(dev, "FPGA revision : 0x%04x\n",
630                                       (reg & CHIPMODE_FPGA_REV_MASK) >>
631                                       CHIPMODE_FPGA_REV_SHIFT);
632                 }
633         }
634
635         /* Reset the ethernet controller. */
636         jme_reset(sc);
637
638         /* Get station address. */
639         reg = CSR_READ_4(sc, JME_SMBCSR);
640         if (reg & SMBCSR_EEPROM_PRESENT)
641                 error = jme_eeprom_macaddr(sc, eaddr);
642         if (error != 0 || (reg & SMBCSR_EEPROM_PRESENT) == 0) {
643                 if (error != 0 && (bootverbose)) {
644                         device_printf(dev, "ethernet hardware address "
645                                       "not found in EEPROM.\n");
646                 }
647                 jme_reg_macaddr(sc, eaddr);
648         }
649
650         /*
651          * Save PHY address.
652          * Integrated JR0211 has fixed PHY address whereas FPGA version
653          * requires PHY probing to get correct PHY address.
654          */
655         if ((sc->jme_caps & JME_CAP_FPGA) == 0) {
656                 sc->jme_phyaddr = CSR_READ_4(sc, JME_GPREG0) &
657                     GPREG0_PHY_ADDR_MASK;
658                 if (bootverbose) {
659                         device_printf(dev, "PHY is at address %d.\n",
660                             sc->jme_phyaddr);
661                 }
662         } else {
663                 sc->jme_phyaddr = 0;
664         }
665
666         /* Set max allowable DMA size. */
667         pcie_ptr = pci_get_pciecap_ptr(dev);
668         if (pcie_ptr != 0) {
669                 uint16_t ctrl;
670
671                 sc->jme_caps |= JME_CAP_PCIE;
672                 ctrl = pci_read_config(dev, pcie_ptr + PCIER_DEVCTRL, 2);
673                 if (bootverbose) {
674                         device_printf(dev, "Read request size : %d bytes.\n",
675                             128 << ((ctrl >> 12) & 0x07));
676                         device_printf(dev, "TLP payload size : %d bytes.\n",
677                             128 << ((ctrl >> 5) & 0x07));
678                 }
679                 switch (ctrl & PCIEM_DEVCTL_MAX_READRQ_MASK) {
680                 case PCIEM_DEVCTL_MAX_READRQ_128:
681                         sc->jme_tx_dma_size = TXCSR_DMA_SIZE_128;
682                         break;
683                 case PCIEM_DEVCTL_MAX_READRQ_256:
684                         sc->jme_tx_dma_size = TXCSR_DMA_SIZE_256;
685                         break;
686                 default:
687                         sc->jme_tx_dma_size = TXCSR_DMA_SIZE_512;
688                         break;
689                 }
690                 sc->jme_rx_dma_size = RXCSR_DMA_SIZE_128;
691         } else {
692                 sc->jme_tx_dma_size = TXCSR_DMA_SIZE_512;
693                 sc->jme_rx_dma_size = RXCSR_DMA_SIZE_128;
694         }
695
696 #ifdef notyet
697         if (pci_find_extcap(dev, PCIY_PMG, &pmc) == 0)
698                 sc->jme_caps |= JME_CAP_PMCAP;
699 #endif
700
701         /*
702          * Create sysctl tree
703          */
704         jme_sysctl_node(sc);
705
706         /* Allocate DMA stuffs */
707         error = jme_dma_alloc(sc);
708         if (error)
709                 goto fail;
710
711         ifp->if_softc = sc;
712         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
713         ifp->if_init = jme_init;
714         ifp->if_ioctl = jme_ioctl;
715         ifp->if_start = jme_start;
716         ifp->if_watchdog = jme_watchdog;
717         ifq_set_maxlen(&ifp->if_snd, JME_TX_RING_CNT - 1);
718         ifq_set_ready(&ifp->if_snd);
719
720         /* JMC250 supports Tx/Rx checksum offload and hardware vlan tagging. */
721         ifp->if_capabilities = IFCAP_HWCSUM |
722                                IFCAP_VLAN_MTU |
723                                IFCAP_VLAN_HWTAGGING;
724         ifp->if_hwassist = JME_CSUM_FEATURES;
725         ifp->if_capenable = ifp->if_capabilities;
726
727         /* Set up MII bus. */
728         error = mii_phy_probe(dev, &sc->jme_miibus,
729                               jme_mediachange, jme_mediastatus);
730         if (error) {
731                 device_printf(dev, "no PHY found!\n");
732                 goto fail;
733         }
734
735         /*
736          * Save PHYADDR for FPGA mode PHY.
737          */
738         if (sc->jme_caps & JME_CAP_FPGA) {
739                 struct mii_data *mii = device_get_softc(sc->jme_miibus);
740
741                 if (mii->mii_instance != 0) {
742                         struct mii_softc *miisc;
743
744                         LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
745                                 if (miisc->mii_phy != 0) {
746                                         sc->jme_phyaddr = miisc->mii_phy;
747                                         break;
748                                 }
749                         }
750                         if (sc->jme_phyaddr != 0) {
751                                 device_printf(sc->jme_dev,
752                                     "FPGA PHY is at %d\n", sc->jme_phyaddr);
753                                 /* vendor magic. */
754                                 jme_miibus_writereg(dev, sc->jme_phyaddr,
755                                     JMPHY_CONF, JMPHY_CONF_DEFFIFO);
756
757                                 /* XXX should we clear JME_CAP_EXTFIFO */
758                         }
759                 }
760         }
761
762         ether_ifattach(ifp, eaddr, NULL);
763
764         /* Tell the upper layer(s) we support long frames. */
765         ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
766
767         error = bus_setup_intr(dev, sc->jme_irq_res, INTR_MPSAFE, jme_intr, sc,
768                                &sc->jme_irq_handle, ifp->if_serializer);
769         if (error) {
770                 device_printf(dev, "could not set up interrupt handler.\n");
771                 ether_ifdetach(ifp);
772                 goto fail;
773         }
774
775         ifp->if_cpuid = ithread_cpuid(rman_get_start(sc->jme_irq_res));
776         KKASSERT(ifp->if_cpuid >= 0 && ifp->if_cpuid < ncpus);
777         return 0;
778 fail:
779         jme_detach(dev);
780         return (error);
781 }
782
783 static int
784 jme_detach(device_t dev)
785 {
786         struct jme_softc *sc = device_get_softc(dev);
787
788         if (device_is_attached(dev)) {
789                 struct ifnet *ifp = &sc->arpcom.ac_if;
790
791                 lwkt_serialize_enter(ifp->if_serializer);
792                 jme_stop(sc);
793                 bus_teardown_intr(dev, sc->jme_irq_res, sc->jme_irq_handle);
794                 lwkt_serialize_exit(ifp->if_serializer);
795
796                 ether_ifdetach(ifp);
797         }
798
799         if (sc->jme_sysctl_tree != NULL)
800                 sysctl_ctx_free(&sc->jme_sysctl_ctx);
801
802         if (sc->jme_miibus != NULL)
803                 device_delete_child(dev, sc->jme_miibus);
804         bus_generic_detach(dev);
805
806         if (sc->jme_irq_res != NULL) {
807                 bus_release_resource(dev, SYS_RES_IRQ, sc->jme_irq_rid,
808                                      sc->jme_irq_res);
809         }
810
811         if (sc->jme_mem_res != NULL) {
812                 bus_release_resource(dev, SYS_RES_MEMORY, sc->jme_mem_rid,
813                                      sc->jme_mem_res);
814         }
815
816         jme_dma_free(sc);
817
818         return (0);
819 }
820
821 static void
822 jme_sysctl_node(struct jme_softc *sc)
823 {
824         int error;
825
826         sysctl_ctx_init(&sc->jme_sysctl_ctx);
827         sc->jme_sysctl_tree = SYSCTL_ADD_NODE(&sc->jme_sysctl_ctx,
828                                 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
829                                 device_get_nameunit(sc->jme_dev),
830                                 CTLFLAG_RD, 0, "");
831         if (sc->jme_sysctl_tree == NULL) {
832                 device_printf(sc->jme_dev, "can't add sysctl node\n");
833                 return;
834         }
835
836         SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
837             SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
838             "tx_coal_to", CTLTYPE_INT | CTLFLAG_RW, &sc->jme_tx_coal_to,
839             0, sysctl_hw_jme_tx_coal_to, "I", "jme tx coalescing timeout");
840
841         SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
842             SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
843             "tx_coal_pkt", CTLTYPE_INT | CTLFLAG_RW, &sc->jme_tx_coal_pkt,
844             0, sysctl_hw_jme_tx_coal_pkt, "I", "jme tx coalescing packet");
845
846         SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
847             SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
848             "rx_coal_to", CTLTYPE_INT | CTLFLAG_RW, &sc->jme_rx_coal_to,
849             0, sysctl_hw_jme_rx_coal_to, "I", "jme rx coalescing timeout");
850
851         SYSCTL_ADD_PROC(&sc->jme_sysctl_ctx,
852             SYSCTL_CHILDREN(sc->jme_sysctl_tree), OID_AUTO,
853             "rx_coal_pkt", CTLTYPE_INT | CTLFLAG_RW, &sc->jme_rx_coal_pkt,
854             0, sysctl_hw_jme_rx_coal_pkt, "I", "jme rx coalescing packet");
855
856         /* Pull in device tunables. */
857         sc->jme_process_limit = JME_PROC_DEFAULT;
858
859         sc->jme_tx_coal_to = PCCTX_COAL_TO_DEFAULT;
860         error = resource_int_value(device_get_name(sc->jme_dev),
861             device_get_unit(sc->jme_dev), "tx_coal_to", &sc->jme_tx_coal_to);
862         if (error == 0) {
863                 if (sc->jme_tx_coal_to < PCCTX_COAL_TO_MIN ||
864                     sc->jme_tx_coal_to > PCCTX_COAL_TO_MAX) {
865                         device_printf(sc->jme_dev,
866                             "tx_coal_to value out of range; "
867                             "using default: %d\n", PCCTX_COAL_TO_DEFAULT);
868                         sc->jme_tx_coal_to = PCCTX_COAL_TO_DEFAULT;
869                 }
870         }
871
872         sc->jme_tx_coal_pkt = PCCTX_COAL_PKT_DEFAULT;
873         error = resource_int_value(device_get_name(sc->jme_dev),
874             device_get_unit(sc->jme_dev), "tx_coal_pkt", &sc->jme_tx_coal_to);
875         if (error == 0) {
876                 if (sc->jme_tx_coal_pkt < PCCTX_COAL_PKT_MIN ||
877                     sc->jme_tx_coal_pkt > PCCTX_COAL_PKT_MAX) {
878                         device_printf(sc->jme_dev,
879                             "tx_coal_pkt value out of range; "
880                             "using default: %d\n", PCCTX_COAL_PKT_DEFAULT);
881                         sc->jme_tx_coal_pkt = PCCTX_COAL_PKT_DEFAULT;
882                 }
883         }
884
885         sc->jme_rx_coal_to = PCCRX_COAL_TO_DEFAULT;
886         error = resource_int_value(device_get_name(sc->jme_dev),
887             device_get_unit(sc->jme_dev), "rx_coal_to", &sc->jme_rx_coal_to);
888         if (error == 0) {
889                 if (sc->jme_rx_coal_to < PCCRX_COAL_TO_MIN ||
890                     sc->jme_rx_coal_to > PCCRX_COAL_TO_MAX) {
891                         device_printf(sc->jme_dev,
892                             "rx_coal_to value out of range; "
893                             "using default: %d\n", PCCRX_COAL_TO_DEFAULT);
894                         sc->jme_rx_coal_to = PCCRX_COAL_TO_DEFAULT;
895                 }
896         }
897
898         sc->jme_rx_coal_pkt = PCCRX_COAL_PKT_DEFAULT;
899         error = resource_int_value(device_get_name(sc->jme_dev),
900             device_get_unit(sc->jme_dev), "rx_coal_pkt", &sc->jme_rx_coal_to);
901         if (error == 0) {
902                 if (sc->jme_rx_coal_pkt < PCCRX_COAL_PKT_MIN ||
903                     sc->jme_rx_coal_pkt > PCCRX_COAL_PKT_MAX) {
904                         device_printf(sc->jme_dev,
905                             "tx_coal_pkt value out of range; "
906                             "using default: %d\n", PCCRX_COAL_PKT_DEFAULT);
907                         sc->jme_rx_coal_pkt = PCCRX_COAL_PKT_DEFAULT;
908                 }
909         }
910 }
911
912 static void
913 jme_dmamap_ring_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
914 {
915         if (error)
916                 return;
917
918         KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
919         *((bus_addr_t *)arg) = segs->ds_addr;
920 }
921
922 static void
923 jme_dmamap_buf_cb(void *xctx, bus_dma_segment_t *segs, int nsegs,
924                   bus_size_t mapsz __unused, int error)
925 {
926         struct jme_dmamap_ctx *ctx = xctx;
927         int i;
928
929         if (error)
930                 return;
931
932         if (nsegs > ctx->nsegs) {
933                 ctx->nsegs = 0;
934                 return;
935         }
936
937         ctx->nsegs = nsegs;
938         for (i = 0; i < nsegs; ++i)
939                 ctx->segs[i] = segs[i];
940 }
941
942 static int
943 jme_dma_alloc(struct jme_softc *sc)
944 {
945         struct jme_txdesc *txd;
946         struct jme_rxdesc *rxd;
947         bus_addr_t busaddr, lowaddr, rx_ring_end, tx_ring_end;
948         int error, i;
949
950         lowaddr = BUS_SPACE_MAXADDR;
951
952 again:
953         /* Create parent ring tag. */
954         error = bus_dma_tag_create(NULL,/* parent */
955             1, 0,                       /* algnmnt, boundary */
956             lowaddr,                    /* lowaddr */
957             BUS_SPACE_MAXADDR,          /* highaddr */
958             NULL, NULL,                 /* filter, filterarg */
959             BUS_SPACE_MAXSIZE_32BIT,    /* maxsize */
960             0,                          /* nsegments */
961             BUS_SPACE_MAXSIZE_32BIT,    /* maxsegsize */
962             0,                          /* flags */
963             &sc->jme_cdata.jme_ring_tag);
964         if (error) {
965                 device_printf(sc->jme_dev,
966                     "could not create parent ring DMA tag.\n");
967                 return error;
968         }
969
970         /*
971          * Create DMA stuffs for TX ring
972          */
973
974         /* Create tag for Tx ring. */
975         error = bus_dma_tag_create(sc->jme_cdata.jme_ring_tag,/* parent */
976             JME_TX_RING_ALIGN, 0,       /* algnmnt, boundary */
977             BUS_SPACE_MAXADDR,          /* lowaddr */
978             BUS_SPACE_MAXADDR,          /* highaddr */
979             NULL, NULL,                 /* filter, filterarg */
980             JME_TX_RING_SIZE,           /* maxsize */
981             1,                          /* nsegments */
982             JME_TX_RING_SIZE,           /* maxsegsize */
983             0,                          /* flags */
984             &sc->jme_cdata.jme_tx_ring_tag);
985         if (error) {
986                 device_printf(sc->jme_dev,
987                     "could not allocate Tx ring DMA tag.\n");
988                 return error;
989         }
990
991         /* Allocate DMA'able memory for TX ring */
992         error = bus_dmamem_alloc(sc->jme_cdata.jme_tx_ring_tag,
993             (void **)&sc->jme_rdata.jme_tx_ring,
994             BUS_DMA_WAITOK | BUS_DMA_ZERO,
995             &sc->jme_cdata.jme_tx_ring_map);
996         if (error) {
997                 device_printf(sc->jme_dev,
998                     "could not allocate DMA'able memory for Tx ring.\n");
999                 bus_dma_tag_destroy(sc->jme_cdata.jme_tx_ring_tag);
1000                 sc->jme_cdata.jme_tx_ring_tag = NULL;
1001                 return error;
1002         }
1003
1004         /*  Load the DMA map for Tx ring. */
1005         error = bus_dmamap_load(sc->jme_cdata.jme_tx_ring_tag,
1006             sc->jme_cdata.jme_tx_ring_map, sc->jme_rdata.jme_tx_ring,
1007             JME_TX_RING_SIZE, jme_dmamap_ring_cb, &busaddr, BUS_DMA_NOWAIT);
1008         if (error) {
1009                 device_printf(sc->jme_dev,
1010                     "could not load DMA'able memory for Tx ring.\n");
1011                 bus_dmamem_free(sc->jme_cdata.jme_tx_ring_tag,
1012                                 sc->jme_rdata.jme_tx_ring,
1013                                 sc->jme_cdata.jme_tx_ring_map);
1014                 bus_dma_tag_destroy(sc->jme_cdata.jme_tx_ring_tag);
1015                 sc->jme_cdata.jme_tx_ring_tag = NULL;
1016                 return error;
1017         }
1018         sc->jme_rdata.jme_tx_ring_paddr = busaddr;
1019
1020         /*
1021          * Create DMA stuffs for RX ring
1022          */
1023
1024         /* Create tag for Rx ring. */
1025         error = bus_dma_tag_create(sc->jme_cdata.jme_ring_tag,/* parent */
1026             JME_RX_RING_ALIGN, 0,       /* algnmnt, boundary */
1027             lowaddr,                    /* lowaddr */
1028             BUS_SPACE_MAXADDR,          /* highaddr */
1029             NULL, NULL,                 /* filter, filterarg */
1030             JME_RX_RING_SIZE,           /* maxsize */
1031             1,                          /* nsegments */
1032             JME_RX_RING_SIZE,           /* maxsegsize */
1033             0,                          /* flags */
1034             &sc->jme_cdata.jme_rx_ring_tag);
1035         if (error) {
1036                 device_printf(sc->jme_dev,
1037                     "could not allocate Rx ring DMA tag.\n");
1038                 return error;
1039         }
1040
1041         /* Allocate DMA'able memory for RX ring */
1042         error = bus_dmamem_alloc(sc->jme_cdata.jme_rx_ring_tag,
1043             (void **)&sc->jme_rdata.jme_rx_ring,
1044             BUS_DMA_WAITOK | BUS_DMA_ZERO,
1045             &sc->jme_cdata.jme_rx_ring_map);
1046         if (error) {
1047                 device_printf(sc->jme_dev,
1048                     "could not allocate DMA'able memory for Rx ring.\n");
1049                 bus_dma_tag_destroy(sc->jme_cdata.jme_rx_ring_tag);
1050                 sc->jme_cdata.jme_rx_ring_tag = NULL;
1051                 return error;
1052         }
1053
1054         /* Load the DMA map for Rx ring. */
1055         error = bus_dmamap_load(sc->jme_cdata.jme_rx_ring_tag,
1056             sc->jme_cdata.jme_rx_ring_map, sc->jme_rdata.jme_rx_ring,
1057             JME_RX_RING_SIZE, jme_dmamap_ring_cb, &busaddr, BUS_DMA_NOWAIT);
1058         if (error) {
1059                 device_printf(sc->jme_dev,
1060                     "could not load DMA'able memory for Rx ring.\n");
1061                 bus_dmamem_free(sc->jme_cdata.jme_rx_ring_tag,
1062                                 sc->jme_rdata.jme_rx_ring,
1063                                 sc->jme_cdata.jme_rx_ring_map);
1064                 bus_dma_tag_destroy(sc->jme_cdata.jme_rx_ring_tag);
1065                 sc->jme_cdata.jme_rx_ring_tag = NULL;
1066                 return error;
1067         }
1068         sc->jme_rdata.jme_rx_ring_paddr = busaddr;
1069
1070         /* Tx/Rx descriptor queue should reside within 4GB boundary. */
1071         tx_ring_end = sc->jme_rdata.jme_tx_ring_paddr + JME_TX_RING_SIZE;
1072         rx_ring_end = sc->jme_rdata.jme_rx_ring_paddr + JME_RX_RING_SIZE;
1073         if ((JME_ADDR_HI(tx_ring_end) !=
1074              JME_ADDR_HI(sc->jme_rdata.jme_tx_ring_paddr)) ||
1075             (JME_ADDR_HI(rx_ring_end) !=
1076              JME_ADDR_HI(sc->jme_rdata.jme_rx_ring_paddr))) {
1077                 device_printf(sc->jme_dev, "4GB boundary crossed, "
1078                     "switching to 32bit DMA address mode.\n");
1079                 jme_dma_free(sc);
1080                 /* Limit DMA address space to 32bit and try again. */
1081                 lowaddr = BUS_SPACE_MAXADDR_32BIT;
1082                 goto again;
1083         }
1084
1085         /* Create parent buffer tag. */
1086         error = bus_dma_tag_create(NULL,/* parent */
1087             1, 0,                       /* algnmnt, boundary */
1088             BUS_SPACE_MAXADDR,          /* lowaddr */
1089             BUS_SPACE_MAXADDR,          /* highaddr */
1090             NULL, NULL,                 /* filter, filterarg */
1091             BUS_SPACE_MAXSIZE_32BIT,    /* maxsize */
1092             0,                          /* nsegments */
1093             BUS_SPACE_MAXSIZE_32BIT,    /* maxsegsize */
1094             0,                          /* flags */
1095             &sc->jme_cdata.jme_buffer_tag);
1096         if (error) {
1097                 device_printf(sc->jme_dev,
1098                     "could not create parent buffer DMA tag.\n");
1099                 return error;
1100         }
1101
1102         /*
1103          * Create DMA stuffs for shadow status block
1104          */
1105
1106         /* Create shadow status block tag. */
1107         error = bus_dma_tag_create(sc->jme_cdata.jme_buffer_tag,/* parent */
1108             JME_SSB_ALIGN, 0,           /* algnmnt, boundary */
1109             BUS_SPACE_MAXADDR,          /* lowaddr */
1110             BUS_SPACE_MAXADDR,          /* highaddr */
1111             NULL, NULL,                 /* filter, filterarg */
1112             JME_SSB_SIZE,               /* maxsize */
1113             1,                          /* nsegments */
1114             JME_SSB_SIZE,               /* maxsegsize */
1115             0,                          /* flags */
1116             &sc->jme_cdata.jme_ssb_tag);
1117         if (error) {
1118                 device_printf(sc->jme_dev,
1119                     "could not create shared status block DMA tag.\n");
1120                 return error;
1121         }
1122
1123         /* Allocate DMA'able memory for shared status block. */
1124         error = bus_dmamem_alloc(sc->jme_cdata.jme_ssb_tag,
1125             (void **)&sc->jme_rdata.jme_ssb_block,
1126             BUS_DMA_WAITOK | BUS_DMA_ZERO,
1127             &sc->jme_cdata.jme_ssb_map);
1128         if (error) {
1129                 device_printf(sc->jme_dev, "could not allocate DMA'able "
1130                     "memory for shared status block.\n");
1131                 bus_dma_tag_destroy(sc->jme_cdata.jme_ssb_tag);
1132                 sc->jme_cdata.jme_ssb_tag = NULL;
1133                 return error;
1134         }
1135
1136         /* Load the DMA map for shared status block */
1137         error = bus_dmamap_load(sc->jme_cdata.jme_ssb_tag,
1138             sc->jme_cdata.jme_ssb_map, sc->jme_rdata.jme_ssb_block,
1139             JME_SSB_SIZE, jme_dmamap_ring_cb, &busaddr, BUS_DMA_NOWAIT);
1140         if (error) {
1141                 device_printf(sc->jme_dev, "could not load DMA'able memory "
1142                     "for shared status block.\n");
1143                 bus_dmamem_free(sc->jme_cdata.jme_ssb_tag,
1144                                 sc->jme_rdata.jme_ssb_block,
1145                                 sc->jme_cdata.jme_ssb_map);
1146                 bus_dma_tag_destroy(sc->jme_cdata.jme_ssb_tag);
1147                 sc->jme_cdata.jme_ssb_tag = NULL;
1148                 return error;
1149         }
1150         sc->jme_rdata.jme_ssb_block_paddr = busaddr;
1151
1152         /*
1153          * Create DMA stuffs for TX buffers
1154          */
1155
1156         /* Create tag for Tx buffers. */
1157         error = bus_dma_tag_create(sc->jme_cdata.jme_buffer_tag,/* parent */
1158             1, 0,                       /* algnmnt, boundary */
1159             BUS_SPACE_MAXADDR,          /* lowaddr */
1160             BUS_SPACE_MAXADDR,          /* highaddr */
1161             NULL, NULL,                 /* filter, filterarg */
1162             JME_TSO_MAXSIZE,            /* maxsize */
1163             JME_MAXTXSEGS,              /* nsegments */
1164             JME_TSO_MAXSEGSIZE,         /* maxsegsize */
1165             0,                          /* flags */
1166             &sc->jme_cdata.jme_tx_tag);
1167         if (error != 0) {
1168                 device_printf(sc->jme_dev, "could not create Tx DMA tag.\n");
1169                 return error;
1170         }
1171
1172         /* Create DMA maps for Tx buffers. */
1173         for (i = 0; i < JME_TX_RING_CNT; i++) {
1174                 txd = &sc->jme_cdata.jme_txdesc[i];
1175                 error = bus_dmamap_create(sc->jme_cdata.jme_tx_tag, 0,
1176                     &txd->tx_dmamap);
1177                 if (error) {
1178                         int j;
1179
1180                         device_printf(sc->jme_dev,
1181                             "could not create %dth Tx dmamap.\n", i);
1182
1183                         for (j = 0; j < i; ++j) {
1184                                 txd = &sc->jme_cdata.jme_txdesc[j];
1185                                 bus_dmamap_destroy(sc->jme_cdata.jme_tx_tag,
1186                                                    txd->tx_dmamap);
1187                         }
1188                         bus_dma_tag_destroy(sc->jme_cdata.jme_tx_tag);
1189                         sc->jme_cdata.jme_tx_tag = NULL;
1190                         return error;
1191                 }
1192         }
1193
1194         /*
1195          * Create DMA stuffs for RX buffers
1196          */
1197
1198         /* Create tag for Rx buffers. */
1199         error = bus_dma_tag_create(sc->jme_cdata.jme_buffer_tag,/* parent */
1200             JME_RX_BUF_ALIGN, 0,        /* algnmnt, boundary */
1201             BUS_SPACE_MAXADDR,          /* lowaddr */
1202             BUS_SPACE_MAXADDR,          /* highaddr */
1203             NULL, NULL,                 /* filter, filterarg */
1204             MCLBYTES,                   /* maxsize */
1205             1,                          /* nsegments */
1206             MCLBYTES,                   /* maxsegsize */
1207             0,                          /* flags */
1208             &sc->jme_cdata.jme_rx_tag);
1209         if (error) {
1210                 device_printf(sc->jme_dev, "could not create Rx DMA tag.\n");
1211                 return error;
1212         }
1213
1214         /* Create DMA maps for Rx buffers. */
1215         error = bus_dmamap_create(sc->jme_cdata.jme_rx_tag, 0,
1216                                   &sc->jme_cdata.jme_rx_sparemap);
1217         if (error) {
1218                 device_printf(sc->jme_dev,
1219                     "could not create spare Rx dmamap.\n");
1220                 bus_dma_tag_destroy(sc->jme_cdata.jme_rx_tag);
1221                 sc->jme_cdata.jme_rx_tag = NULL;
1222                 return error;
1223         }
1224         for (i = 0; i < JME_RX_RING_CNT; i++) {
1225                 rxd = &sc->jme_cdata.jme_rxdesc[i];
1226                 error = bus_dmamap_create(sc->jme_cdata.jme_rx_tag, 0,
1227                     &rxd->rx_dmamap);
1228                 if (error) {
1229                         int j;
1230
1231                         device_printf(sc->jme_dev,
1232                             "could not create %dth Rx dmamap.\n", i);
1233
1234                         for (j = 0; j < i; ++j) {
1235                                 rxd = &sc->jme_cdata.jme_rxdesc[j];
1236                                 bus_dmamap_destroy(sc->jme_cdata.jme_rx_tag,
1237                                                    rxd->rx_dmamap);
1238                         }
1239                         bus_dmamap_destroy(sc->jme_cdata.jme_rx_tag,
1240                             sc->jme_cdata.jme_rx_sparemap);
1241                         bus_dma_tag_destroy(sc->jme_cdata.jme_rx_tag);
1242                         sc->jme_cdata.jme_rx_tag = NULL;
1243                         return error;
1244                 }
1245         }
1246         return 0;
1247 }
1248
1249 static void
1250 jme_dma_free(struct jme_softc *sc)
1251 {
1252         struct jme_txdesc *txd;
1253         struct jme_rxdesc *rxd;
1254         int i;
1255
1256         /* Tx ring */
1257         if (sc->jme_cdata.jme_tx_ring_tag != NULL) {
1258                 bus_dmamap_unload(sc->jme_cdata.jme_tx_ring_tag,
1259                     sc->jme_cdata.jme_tx_ring_map);
1260                 bus_dmamem_free(sc->jme_cdata.jme_tx_ring_tag,
1261                     sc->jme_rdata.jme_tx_ring,
1262                     sc->jme_cdata.jme_tx_ring_map);
1263                 bus_dma_tag_destroy(sc->jme_cdata.jme_tx_ring_tag);
1264                 sc->jme_cdata.jme_tx_ring_tag = NULL;
1265         }
1266
1267         /* Rx ring */
1268         if (sc->jme_cdata.jme_rx_ring_tag != NULL) {
1269                 bus_dmamap_unload(sc->jme_cdata.jme_rx_ring_tag,
1270                     sc->jme_cdata.jme_rx_ring_map);
1271                 bus_dmamem_free(sc->jme_cdata.jme_rx_ring_tag,
1272                     sc->jme_rdata.jme_rx_ring,
1273                     sc->jme_cdata.jme_rx_ring_map);
1274                 bus_dma_tag_destroy(sc->jme_cdata.jme_rx_ring_tag);
1275                 sc->jme_cdata.jme_rx_ring_tag = NULL;
1276         }
1277
1278         /* Tx buffers */
1279         if (sc->jme_cdata.jme_tx_tag != NULL) {
1280                 for (i = 0; i < JME_TX_RING_CNT; i++) {
1281                         txd = &sc->jme_cdata.jme_txdesc[i];
1282                         bus_dmamap_destroy(sc->jme_cdata.jme_tx_tag,
1283                             txd->tx_dmamap);
1284                 }
1285                 bus_dma_tag_destroy(sc->jme_cdata.jme_tx_tag);
1286                 sc->jme_cdata.jme_tx_tag = NULL;
1287         }
1288
1289         /* Rx buffers */
1290         if (sc->jme_cdata.jme_rx_tag != NULL) {
1291                 for (i = 0; i < JME_RX_RING_CNT; i++) {
1292                         rxd = &sc->jme_cdata.jme_rxdesc[i];
1293                         bus_dmamap_destroy(sc->jme_cdata.jme_rx_tag,
1294                             rxd->rx_dmamap);
1295                 }
1296                 bus_dmamap_destroy(sc->jme_cdata.jme_rx_tag,
1297                     sc->jme_cdata.jme_rx_sparemap);
1298                 bus_dma_tag_destroy(sc->jme_cdata.jme_rx_tag);
1299                 sc->jme_cdata.jme_rx_tag = NULL;
1300         }
1301
1302         /* Shadow status block. */
1303         if (sc->jme_cdata.jme_ssb_tag != NULL) {
1304                 bus_dmamap_unload(sc->jme_cdata.jme_ssb_tag,
1305                     sc->jme_cdata.jme_ssb_map);
1306                 bus_dmamem_free(sc->jme_cdata.jme_ssb_tag,
1307                     sc->jme_rdata.jme_ssb_block,
1308                     sc->jme_cdata.jme_ssb_map);
1309                 bus_dma_tag_destroy(sc->jme_cdata.jme_ssb_tag);
1310                 sc->jme_cdata.jme_ssb_tag = NULL;
1311         }
1312
1313         if (sc->jme_cdata.jme_buffer_tag != NULL) {
1314                 bus_dma_tag_destroy(sc->jme_cdata.jme_buffer_tag);
1315                 sc->jme_cdata.jme_buffer_tag = NULL;
1316         }
1317         if (sc->jme_cdata.jme_ring_tag != NULL) {
1318                 bus_dma_tag_destroy(sc->jme_cdata.jme_ring_tag);
1319                 sc->jme_cdata.jme_ring_tag = NULL;
1320         }
1321 }
1322
1323 /*
1324  *      Make sure the interface is stopped at reboot time.
1325  */
1326 static int
1327 jme_shutdown(device_t dev)
1328 {
1329         return jme_suspend(dev);
1330 }
1331
1332 #ifdef notyet
1333 /*
1334  * Unlike other ethernet controllers, JMC250 requires
1335  * explicit resetting link speed to 10/100Mbps as gigabit
1336  * link will cunsume more power than 375mA.
1337  * Note, we reset the link speed to 10/100Mbps with
1338  * auto-negotiation but we don't know whether that operation
1339  * would succeed or not as we have no control after powering
1340  * off. If the renegotiation fail WOL may not work. Running
1341  * at 1Gbps draws more power than 375mA at 3.3V which is
1342  * specified in PCI specification and that would result in
1343  * complete shutdowning power to ethernet controller.
1344  *
1345  * TODO
1346  *  Save current negotiated media speed/duplex/flow-control
1347  *  to softc and restore the same link again after resuming.
1348  *  PHY handling such as power down/resetting to 100Mbps
1349  *  may be better handled in suspend method in phy driver.
1350  */
1351 static void
1352 jme_setlinkspeed(struct jme_softc *sc)
1353 {
1354         struct mii_data *mii;
1355         int aneg, i;
1356
1357         JME_LOCK_ASSERT(sc);
1358
1359         mii = device_get_softc(sc->jme_miibus);
1360         mii_pollstat(mii);
1361         aneg = 0;
1362         if ((mii->mii_media_status & IFM_AVALID) != 0) {
1363                 switch IFM_SUBTYPE(mii->mii_media_active) {
1364                 case IFM_10_T:
1365                 case IFM_100_TX:
1366                         return;
1367                 case IFM_1000_T:
1368                         aneg++;
1369                 default:
1370                         break;
1371                 }
1372         }
1373         jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr, MII_100T2CR, 0);
1374         jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr, MII_ANAR,
1375             ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA);
1376         jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr, MII_BMCR,
1377             BMCR_AUTOEN | BMCR_STARTNEG);
1378         DELAY(1000);
1379         if (aneg != 0) {
1380                 /* Poll link state until jme(4) get a 10/100 link. */
1381                 for (i = 0; i < MII_ANEGTICKS_GIGE; i++) {
1382                         mii_pollstat(mii);
1383                         if ((mii->mii_media_status & IFM_AVALID) != 0) {
1384                                 switch (IFM_SUBTYPE(mii->mii_media_active)) {
1385                                 case IFM_10_T:
1386                                 case IFM_100_TX:
1387                                         jme_mac_config(sc);
1388                                         return;
1389                                 default:
1390                                         break;
1391                                 }
1392                         }
1393                         JME_UNLOCK(sc);
1394                         pause("jmelnk", hz);
1395                         JME_LOCK(sc);
1396                 }
1397                 if (i == MII_ANEGTICKS_GIGE)
1398                         device_printf(sc->jme_dev, "establishing link failed, "
1399                             "WOL may not work!");
1400         }
1401         /*
1402          * No link, force MAC to have 100Mbps, full-duplex link.
1403          * This is the last resort and may/may not work.
1404          */
1405         mii->mii_media_status = IFM_AVALID | IFM_ACTIVE;
1406         mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
1407         jme_mac_config(sc);
1408 }
1409
1410 static void
1411 jme_setwol(struct jme_softc *sc)
1412 {
1413         struct ifnet *ifp = &sc->arpcom.ac_if;
1414         uint32_t gpr, pmcs;
1415         uint16_t pmstat;
1416         int pmc;
1417
1418         if (pci_find_extcap(sc->jme_dev, PCIY_PMG, &pmc) != 0) {
1419                 /* No PME capability, PHY power down. */
1420                 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr,
1421                     MII_BMCR, BMCR_PDOWN);
1422                 return;
1423         }
1424
1425         gpr = CSR_READ_4(sc, JME_GPREG0) & ~GPREG0_PME_ENB;
1426         pmcs = CSR_READ_4(sc, JME_PMCS);
1427         pmcs &= ~PMCS_WOL_ENB_MASK;
1428         if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0) {
1429                 pmcs |= PMCS_MAGIC_FRAME | PMCS_MAGIC_FRAME_ENB;
1430                 /* Enable PME message. */
1431                 gpr |= GPREG0_PME_ENB;
1432                 /* For gigabit controllers, reset link speed to 10/100. */
1433                 if ((sc->jme_caps & JME_CAP_FASTETH) == 0)
1434                         jme_setlinkspeed(sc);
1435         }
1436
1437         CSR_WRITE_4(sc, JME_PMCS, pmcs);
1438         CSR_WRITE_4(sc, JME_GPREG0, gpr);
1439
1440         /* Request PME. */
1441         pmstat = pci_read_config(sc->jme_dev, pmc + PCIR_POWER_STATUS, 2);
1442         pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
1443         if ((ifp->if_capenable & IFCAP_WOL) != 0)
1444                 pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
1445         pci_write_config(sc->jme_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
1446         if ((ifp->if_capenable & IFCAP_WOL) == 0) {
1447                 /* No WOL, PHY power down. */
1448                 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr,
1449                     MII_BMCR, BMCR_PDOWN);
1450         }
1451 }
1452 #endif
1453
1454 static int
1455 jme_suspend(device_t dev)
1456 {
1457         struct jme_softc *sc = device_get_softc(dev);
1458         struct ifnet *ifp = &sc->arpcom.ac_if;
1459
1460         lwkt_serialize_enter(ifp->if_serializer);
1461         jme_stop(sc);
1462 #ifdef notyet
1463         jme_setwol(sc);
1464 #endif
1465         lwkt_serialize_exit(ifp->if_serializer);
1466
1467         return (0);
1468 }
1469
1470 static int
1471 jme_resume(device_t dev)
1472 {
1473         struct jme_softc *sc = device_get_softc(dev);
1474         struct ifnet *ifp = &sc->arpcom.ac_if;
1475 #ifdef notyet
1476         int pmc;
1477 #endif
1478
1479         lwkt_serialize_enter(ifp->if_serializer);
1480
1481 #ifdef notyet
1482         if (pci_find_extcap(sc->jme_dev, PCIY_PMG, &pmc) != 0) {
1483                 uint16_t pmstat;
1484
1485                 pmstat = pci_read_config(sc->jme_dev,
1486                     pmc + PCIR_POWER_STATUS, 2);
1487                 /* Disable PME clear PME status. */
1488                 pmstat &= ~PCIM_PSTAT_PMEENABLE;
1489                 pci_write_config(sc->jme_dev,
1490                     pmc + PCIR_POWER_STATUS, pmstat, 2);
1491         }
1492 #endif
1493
1494         if (ifp->if_flags & IFF_UP)
1495                 jme_init(sc);
1496
1497         lwkt_serialize_exit(ifp->if_serializer);
1498
1499         return (0);
1500 }
1501
1502 static int
1503 jme_encap(struct jme_softc *sc, struct mbuf **m_head)
1504 {
1505         struct jme_txdesc *txd;
1506         struct jme_desc *desc;
1507         struct mbuf *m;
1508         struct jme_dmamap_ctx ctx;
1509         bus_dma_segment_t txsegs[JME_MAXTXSEGS];
1510         int maxsegs;
1511         int error, i, prod;
1512         uint32_t cflags;
1513
1514         M_ASSERTPKTHDR((*m_head));
1515
1516         prod = sc->jme_cdata.jme_tx_prod;
1517         txd = &sc->jme_cdata.jme_txdesc[prod];
1518
1519         maxsegs = (JME_TX_RING_CNT - sc->jme_cdata.jme_tx_cnt) -
1520                   (JME_TXD_RSVD + 1);
1521         if (maxsegs > JME_MAXTXSEGS)
1522                 maxsegs = JME_MAXTXSEGS;
1523         KASSERT(maxsegs >= (sc->jme_txd_spare - 1),
1524                 ("not enough segments %d\n", maxsegs));
1525
1526         ctx.nsegs = maxsegs;
1527         ctx.segs = txsegs;
1528         error = bus_dmamap_load_mbuf(sc->jme_cdata.jme_tx_tag, txd->tx_dmamap,
1529                                      *m_head, jme_dmamap_buf_cb, &ctx,
1530                                      BUS_DMA_NOWAIT);
1531         if (!error && ctx.nsegs == 0) {
1532                 bus_dmamap_unload(sc->jme_cdata.jme_tx_tag, txd->tx_dmamap);
1533                 error = EFBIG;
1534         }
1535         if (error == EFBIG) {
1536                 m = m_defrag(*m_head, MB_DONTWAIT);
1537                 if (m == NULL) {
1538                         if_printf(&sc->arpcom.ac_if,
1539                                   "could not defrag TX mbuf\n");
1540                         m_freem(*m_head);
1541                         *m_head = NULL;
1542                         return (ENOMEM);
1543                 }
1544                 *m_head = m;
1545
1546                 ctx.nsegs = maxsegs;
1547                 ctx.segs = txsegs;
1548                 error = bus_dmamap_load_mbuf(sc->jme_cdata.jme_tx_tag,
1549                                              txd->tx_dmamap, *m_head,
1550                                              jme_dmamap_buf_cb, &ctx,
1551                                              BUS_DMA_NOWAIT);
1552                 if (error || ctx.nsegs == 0) {
1553                         if_printf(&sc->arpcom.ac_if,
1554                                   "could not load defragged TX mbuf\n");
1555                         if (!error) {
1556                                 bus_dmamap_unload(sc->jme_cdata.jme_tx_tag,
1557                                                   txd->tx_dmamap);
1558                                 error = EFBIG;
1559                         }
1560                         m_freem(*m_head);
1561                         *m_head = NULL;
1562                         return (error);
1563                 }
1564         } else if (error) {
1565                 if_printf(&sc->arpcom.ac_if, "could not load TX mbuf\n");
1566                 return (error);
1567         }
1568
1569         m = *m_head;
1570         cflags = 0;
1571
1572         /* Configure checksum offload. */
1573         if (m->m_pkthdr.csum_flags & CSUM_IP)
1574                 cflags |= JME_TD_IPCSUM;
1575         if (m->m_pkthdr.csum_flags & CSUM_TCP)
1576                 cflags |= JME_TD_TCPCSUM;
1577         if (m->m_pkthdr.csum_flags & CSUM_UDP)
1578                 cflags |= JME_TD_UDPCSUM;
1579
1580         /* Configure VLAN. */
1581         if (m->m_flags & M_VLANTAG) {
1582                 cflags |= (m->m_pkthdr.ether_vlantag & JME_TD_VLAN_MASK);
1583                 cflags |= JME_TD_VLAN_TAG;
1584         }
1585
1586         desc = &sc->jme_rdata.jme_tx_ring[prod];
1587         desc->flags = htole32(cflags);
1588         desc->buflen = 0;
1589         desc->addr_hi = htole32(m->m_pkthdr.len);
1590         desc->addr_lo = 0;
1591         sc->jme_cdata.jme_tx_cnt++;
1592         KKASSERT(sc->jme_cdata.jme_tx_cnt < JME_TX_RING_CNT - JME_TXD_RSVD);
1593         JME_DESC_INC(prod, JME_TX_RING_CNT);
1594         for (i = 0; i < ctx.nsegs; i++) {
1595                 desc = &sc->jme_rdata.jme_tx_ring[prod];
1596                 desc->flags = htole32(JME_TD_OWN | JME_TD_64BIT);
1597                 desc->buflen = htole32(txsegs[i].ds_len);
1598                 desc->addr_hi = htole32(JME_ADDR_HI(txsegs[i].ds_addr));
1599                 desc->addr_lo = htole32(JME_ADDR_LO(txsegs[i].ds_addr));
1600
1601                 sc->jme_cdata.jme_tx_cnt++;
1602                 KKASSERT(sc->jme_cdata.jme_tx_cnt <=
1603                          JME_TX_RING_CNT - JME_TXD_RSVD);
1604                 JME_DESC_INC(prod, JME_TX_RING_CNT);
1605         }
1606
1607         /* Update producer index. */
1608         sc->jme_cdata.jme_tx_prod = prod;
1609         /*
1610          * Finally request interrupt and give the first descriptor
1611          * owenership to hardware.
1612          */
1613         desc = txd->tx_desc;
1614         desc->flags |= htole32(JME_TD_OWN | JME_TD_INTR);
1615
1616         txd->tx_m = m;
1617         txd->tx_ndesc = ctx.nsegs + 1;
1618
1619         /* Sync descriptors. */
1620         bus_dmamap_sync(sc->jme_cdata.jme_tx_tag, txd->tx_dmamap,
1621                         BUS_DMASYNC_PREWRITE);
1622         bus_dmamap_sync(sc->jme_cdata.jme_tx_ring_tag,
1623                         sc->jme_cdata.jme_tx_ring_map, BUS_DMASYNC_PREWRITE);
1624         return (0);
1625 }
1626
1627 static void
1628 jme_start(struct ifnet *ifp)
1629 {
1630         struct jme_softc *sc = ifp->if_softc;
1631         struct mbuf *m_head;
1632         int enq = 0;
1633
1634         ASSERT_SERIALIZED(ifp->if_serializer);
1635
1636         if ((sc->jme_flags & JME_FLAG_LINK) == 0) {
1637                 ifq_purge(&ifp->if_snd);
1638                 return;
1639         }
1640
1641         if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1642                 return;
1643
1644         if (sc->jme_cdata.jme_tx_cnt >= JME_TX_DESC_HIWAT)
1645                 jme_txeof(sc);
1646
1647         while (!ifq_is_empty(&ifp->if_snd)) {
1648                 /*
1649                  * Check number of available TX descs, always
1650                  * leave JME_TXD_RSVD free TX descs.
1651                  */
1652                 if (sc->jme_cdata.jme_tx_cnt + sc->jme_txd_spare >
1653                     JME_TX_RING_CNT - JME_TXD_RSVD) {
1654                         ifp->if_flags |= IFF_OACTIVE;
1655                         break;
1656                 }
1657
1658                 m_head = ifq_dequeue(&ifp->if_snd, NULL);
1659                 if (m_head == NULL)
1660                         break;
1661
1662                 /*
1663                  * Pack the data into the transmit ring. If we
1664                  * don't have room, set the OACTIVE flag and wait
1665                  * for the NIC to drain the ring.
1666                  */
1667                 if (jme_encap(sc, &m_head)) {
1668                         if (m_head == NULL) {
1669                                 ifp->if_oerrors++;
1670                                 break;
1671                         }
1672                         ifq_prepend(&ifp->if_snd, m_head);
1673                         ifp->if_flags |= IFF_OACTIVE;
1674                         break;
1675                 }
1676                 enq++;
1677
1678                 /*
1679                  * If there's a BPF listener, bounce a copy of this frame
1680                  * to him.
1681                  */
1682                 ETHER_BPF_MTAP(ifp, m_head);
1683         }
1684
1685         if (enq > 0) {
1686                 /*
1687                  * Reading TXCSR takes very long time under heavy load
1688                  * so cache TXCSR value and writes the ORed value with
1689                  * the kick command to the TXCSR. This saves one register
1690                  * access cycle.
1691                  */
1692                 CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr | TXCSR_TX_ENB |
1693                     TXCSR_TXQ_N_START(TXCSR_TXQ0));
1694                 /* Set a timeout in case the chip goes out to lunch. */
1695                 ifp->if_timer = JME_TX_TIMEOUT;
1696         }
1697 }
1698
1699 static void
1700 jme_watchdog(struct ifnet *ifp)
1701 {
1702         struct jme_softc *sc = ifp->if_softc;
1703
1704         ASSERT_SERIALIZED(ifp->if_serializer);
1705
1706         if ((sc->jme_flags & JME_FLAG_LINK) == 0) {
1707                 if_printf(ifp, "watchdog timeout (missed link)\n");
1708                 ifp->if_oerrors++;
1709                 jme_init(sc);
1710                 return;
1711         }
1712
1713         jme_txeof(sc);
1714         if (sc->jme_cdata.jme_tx_cnt == 0) {
1715                 if_printf(ifp, "watchdog timeout (missed Tx interrupts) "
1716                           "-- recovering\n");
1717                 if (!ifq_is_empty(&ifp->if_snd))
1718                         if_devstart(ifp);
1719                 return;
1720         }
1721
1722         if_printf(ifp, "watchdog timeout\n");
1723         ifp->if_oerrors++;
1724         jme_init(sc);
1725         if (!ifq_is_empty(&ifp->if_snd))
1726                 if_devstart(ifp);
1727 }
1728
1729 static int
1730 jme_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1731 {
1732         struct jme_softc *sc = ifp->if_softc;
1733         struct mii_data *mii = device_get_softc(sc->jme_miibus);
1734         struct ifreq *ifr = (struct ifreq *)data;
1735         int error = 0, mask;
1736
1737         ASSERT_SERIALIZED(ifp->if_serializer);
1738
1739         switch (cmd) {
1740         case SIOCSIFMTU:
1741                 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > JME_JUMBO_MTU ||
1742                     (!(sc->jme_caps & JME_CAP_JUMBO) &&
1743                      ifr->ifr_mtu > JME_MAX_MTU)) {
1744                         error = EINVAL;
1745                         break;
1746                 }
1747
1748                 if (ifp->if_mtu != ifr->ifr_mtu) {
1749                         /*
1750                          * No special configuration is required when interface
1751                          * MTU is changed but availability of Tx checksum
1752                          * offload should be chcked against new MTU size as
1753                          * FIFO size is just 2K.
1754                          */
1755                         if (ifr->ifr_mtu >= JME_TX_FIFO_SIZE) {
1756                                 ifp->if_capenable &= ~IFCAP_TXCSUM;
1757                                 ifp->if_hwassist &= ~JME_CSUM_FEATURES;
1758                         }
1759                         ifp->if_mtu = ifr->ifr_mtu;
1760                         if (ifp->if_flags & IFF_RUNNING)
1761                                 jme_init(sc);
1762                 }
1763                 break;
1764
1765         case SIOCSIFFLAGS:
1766                 if (ifp->if_flags & IFF_UP) {
1767                         if (ifp->if_flags & IFF_RUNNING) {
1768                                 if ((ifp->if_flags ^ sc->jme_if_flags) &
1769                                     (IFF_PROMISC | IFF_ALLMULTI))
1770                                         jme_set_filter(sc);
1771                         } else {
1772                                 jme_init(sc);
1773                         }
1774                 } else {
1775                         if (ifp->if_flags & IFF_RUNNING)
1776                                 jme_stop(sc);
1777                 }
1778                 sc->jme_if_flags = ifp->if_flags;
1779                 break;
1780
1781         case SIOCADDMULTI:
1782         case SIOCDELMULTI:
1783                 if (ifp->if_flags & IFF_RUNNING)
1784                         jme_set_filter(sc);
1785                 break;
1786
1787         case SIOCSIFMEDIA:
1788         case SIOCGIFMEDIA:
1789                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1790                 break;
1791
1792         case SIOCSIFCAP:
1793                 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1794
1795                 if ((mask & IFCAP_TXCSUM) && ifp->if_mtu < JME_TX_FIFO_SIZE) {
1796                         if (IFCAP_TXCSUM & ifp->if_capabilities) {
1797                                 ifp->if_capenable ^= IFCAP_TXCSUM;
1798                                 if (IFCAP_TXCSUM & ifp->if_capenable)
1799                                         ifp->if_hwassist |= JME_CSUM_FEATURES;
1800                                 else
1801                                         ifp->if_hwassist &= ~JME_CSUM_FEATURES;
1802                         }
1803                 }
1804                 if ((mask & IFCAP_RXCSUM) &&
1805                     (IFCAP_RXCSUM & ifp->if_capabilities)) {
1806                         uint32_t reg;
1807
1808                         ifp->if_capenable ^= IFCAP_RXCSUM;
1809                         reg = CSR_READ_4(sc, JME_RXMAC);
1810                         reg &= ~RXMAC_CSUM_ENB;
1811                         if (ifp->if_capenable & IFCAP_RXCSUM)
1812                                 reg |= RXMAC_CSUM_ENB;
1813                         CSR_WRITE_4(sc, JME_RXMAC, reg);
1814                 }
1815
1816                 if ((mask & IFCAP_VLAN_HWTAGGING) &&
1817                     (IFCAP_VLAN_HWTAGGING & ifp->if_capabilities)) {
1818                         ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1819                         jme_set_vlan(sc);
1820                 }
1821                 break;
1822
1823         default:
1824                 error = ether_ioctl(ifp, cmd, data);
1825                 break;
1826         }
1827         return (error);
1828 }
1829
1830 static void
1831 jme_mac_config(struct jme_softc *sc)
1832 {
1833         struct mii_data *mii;
1834         uint32_t ghc, rxmac, txmac, txpause;
1835         int phyconf = JMPHY_CONF_DEFFIFO;
1836
1837         mii = device_get_softc(sc->jme_miibus);
1838
1839         CSR_WRITE_4(sc, JME_GHC, GHC_RESET);
1840         DELAY(10);
1841         CSR_WRITE_4(sc, JME_GHC, 0);
1842         ghc = 0;
1843         rxmac = CSR_READ_4(sc, JME_RXMAC);
1844         rxmac &= ~RXMAC_FC_ENB;
1845         txmac = CSR_READ_4(sc, JME_TXMAC);
1846         txmac &= ~(TXMAC_CARRIER_EXT | TXMAC_FRAME_BURST);
1847         txpause = CSR_READ_4(sc, JME_TXPFC);
1848         txpause &= ~TXPFC_PAUSE_ENB;
1849         if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
1850                 ghc |= GHC_FULL_DUPLEX;
1851                 rxmac &= ~RXMAC_COLL_DET_ENB;
1852                 txmac &= ~(TXMAC_COLL_ENB | TXMAC_CARRIER_SENSE |
1853                     TXMAC_BACKOFF | TXMAC_CARRIER_EXT |
1854                     TXMAC_FRAME_BURST);
1855 #ifdef notyet
1856                 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
1857                         txpause |= TXPFC_PAUSE_ENB;
1858                 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
1859                         rxmac |= RXMAC_FC_ENB;
1860 #endif
1861                 /* Disable retry transmit timer/retry limit. */
1862                 CSR_WRITE_4(sc, JME_TXTRHD, CSR_READ_4(sc, JME_TXTRHD) &
1863                     ~(TXTRHD_RT_PERIOD_ENB | TXTRHD_RT_LIMIT_ENB));
1864         } else {
1865                 rxmac |= RXMAC_COLL_DET_ENB;
1866                 txmac |= TXMAC_COLL_ENB | TXMAC_CARRIER_SENSE | TXMAC_BACKOFF;
1867                 /* Enable retry transmit timer/retry limit. */
1868                 CSR_WRITE_4(sc, JME_TXTRHD, CSR_READ_4(sc, JME_TXTRHD) |
1869                     TXTRHD_RT_PERIOD_ENB | TXTRHD_RT_LIMIT_ENB);
1870         }
1871
1872         /* Reprogram Tx/Rx MACs with resolved speed/duplex. */
1873         switch (IFM_SUBTYPE(mii->mii_media_active)) {
1874         case IFM_10_T:
1875                 ghc |= GHC_SPEED_10;
1876                 break;
1877
1878         case IFM_100_TX:
1879                 ghc |= GHC_SPEED_100;
1880
1881                 /*
1882                  * Use extended FIFO depth to workaround CRC errors
1883                  * emitted by chips before JMC250B
1884                  */
1885                 phyconf = JMPHY_CONF_EXTFIFO;
1886                 break;
1887
1888         case IFM_1000_T:
1889                 if (sc->jme_caps & JME_CAP_FASTETH)
1890                         break;
1891
1892                 ghc |= GHC_SPEED_1000;
1893                 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) == 0)
1894                         txmac |= TXMAC_CARRIER_EXT | TXMAC_FRAME_BURST;
1895                 break;
1896
1897         default:
1898                 break;
1899         }
1900         CSR_WRITE_4(sc, JME_GHC, ghc);
1901         CSR_WRITE_4(sc, JME_RXMAC, rxmac);
1902         CSR_WRITE_4(sc, JME_TXMAC, txmac);
1903         CSR_WRITE_4(sc, JME_TXPFC, txpause);
1904
1905         if (sc->jme_caps & JME_CAP_EXTFIFO) {
1906                 jme_miibus_writereg(sc->jme_dev, sc->jme_phyaddr,
1907                                     JMPHY_CONF, phyconf);
1908         }
1909 }
1910
1911 static void
1912 jme_intr(void *xsc)
1913 {
1914         struct jme_softc *sc = xsc;
1915         struct ifnet *ifp = &sc->arpcom.ac_if;
1916         uint32_t status;
1917
1918         ASSERT_SERIALIZED(ifp->if_serializer);
1919
1920         status = CSR_READ_4(sc, JME_INTR_REQ_STATUS);
1921         if (status == 0 || status == 0xFFFFFFFF)
1922                 return;
1923
1924         /* Disable interrupts. */
1925         CSR_WRITE_4(sc, JME_INTR_MASK_CLR, JME_INTRS);
1926
1927         status = CSR_READ_4(sc, JME_INTR_STATUS);
1928         if ((status & JME_INTRS) == 0 || status == 0xFFFFFFFF)
1929                 goto back;
1930
1931         /* Reset PCC counter/timer and Ack interrupts. */
1932         status &= ~(INTR_TXQ_COMP | INTR_RXQ_COMP);
1933         if (status & (INTR_TXQ_COAL | INTR_TXQ_COAL_TO))
1934                 status |= INTR_TXQ_COAL | INTR_TXQ_COAL_TO | INTR_TXQ_COMP;
1935         if (status & (INTR_RXQ_COAL | INTR_RXQ_COAL_TO))
1936                 status |= INTR_RXQ_COAL | INTR_RXQ_COAL_TO | INTR_RXQ_COMP;
1937         CSR_WRITE_4(sc, JME_INTR_STATUS, status);
1938
1939         if (ifp->if_flags & IFF_RUNNING) {
1940                 if (status & (INTR_RXQ_COAL | INTR_RXQ_COAL_TO))
1941                         jme_rxeof(sc);
1942
1943                 if (status & INTR_RXQ_DESC_EMPTY) {
1944                         /*
1945                          * Notify hardware availability of new Rx buffers.
1946                          * Reading RXCSR takes very long time under heavy
1947                          * load so cache RXCSR value and writes the ORed
1948                          * value with the kick command to the RXCSR. This
1949                          * saves one register access cycle.
1950                          */
1951                         CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr |
1952                             RXCSR_RX_ENB | RXCSR_RXQ_START);
1953                 }
1954
1955                 if (status & (INTR_TXQ_COAL | INTR_TXQ_COAL_TO)) {
1956                         jme_txeof(sc);
1957                         if (!ifq_is_empty(&ifp->if_snd))
1958                                 if_devstart(ifp);
1959                 }
1960         }
1961 back:
1962         /* Reenable interrupts. */
1963         CSR_WRITE_4(sc, JME_INTR_MASK_SET, JME_INTRS);
1964 }
1965
1966 static void
1967 jme_txeof(struct jme_softc *sc)
1968 {
1969         struct ifnet *ifp = &sc->arpcom.ac_if;
1970         struct jme_txdesc *txd;
1971         uint32_t status;
1972         int cons, nsegs;
1973
1974         cons = sc->jme_cdata.jme_tx_cons;
1975         if (cons == sc->jme_cdata.jme_tx_prod)
1976                 return;
1977
1978         bus_dmamap_sync(sc->jme_cdata.jme_tx_ring_tag,
1979                         sc->jme_cdata.jme_tx_ring_map,
1980                         BUS_DMASYNC_POSTREAD);
1981
1982         /*
1983          * Go through our Tx list and free mbufs for those
1984          * frames which have been transmitted.
1985          */
1986         while (cons != sc->jme_cdata.jme_tx_prod) {
1987                 txd = &sc->jme_cdata.jme_txdesc[cons];
1988                 KASSERT(txd->tx_m != NULL,
1989                         ("%s: freeing NULL mbuf!\n", __func__));
1990
1991                 status = le32toh(txd->tx_desc->flags);
1992                 if ((status & JME_TD_OWN) == JME_TD_OWN)
1993                         break;
1994
1995                 if (status & (JME_TD_TMOUT | JME_TD_RETRY_EXP)) {
1996                         ifp->if_oerrors++;
1997                 } else {
1998                         ifp->if_opackets++;
1999                         if (status & JME_TD_COLLISION) {
2000                                 ifp->if_collisions +=
2001                                     le32toh(txd->tx_desc->buflen) &
2002                                     JME_TD_BUF_LEN_MASK;
2003                         }
2004                 }
2005
2006                 /*
2007                  * Only the first descriptor of multi-descriptor
2008                  * transmission is updated so driver have to skip entire
2009                  * chained buffers for the transmiited frame. In other
2010                  * words, JME_TD_OWN bit is valid only at the first
2011                  * descriptor of a multi-descriptor transmission.
2012                  */
2013                 for (nsegs = 0; nsegs < txd->tx_ndesc; nsegs++) {
2014                         sc->jme_rdata.jme_tx_ring[cons].flags = 0;
2015                         JME_DESC_INC(cons, JME_TX_RING_CNT);
2016                 }
2017
2018                 /* Reclaim transferred mbufs. */
2019                 bus_dmamap_unload(sc->jme_cdata.jme_tx_tag, txd->tx_dmamap);
2020                 m_freem(txd->tx_m);
2021                 txd->tx_m = NULL;
2022                 sc->jme_cdata.jme_tx_cnt -= txd->tx_ndesc;
2023                 KASSERT(sc->jme_cdata.jme_tx_cnt >= 0,
2024                         ("%s: Active Tx desc counter was garbled\n", __func__));
2025                 txd->tx_ndesc = 0;
2026         }
2027         sc->jme_cdata.jme_tx_cons = cons;
2028
2029         if (sc->jme_cdata.jme_tx_cnt == 0)
2030                 ifp->if_timer = 0;
2031
2032         if (sc->jme_cdata.jme_tx_cnt + sc->jme_txd_spare <=
2033             JME_TX_RING_CNT - JME_TXD_RSVD)
2034                 ifp->if_flags &= ~IFF_OACTIVE;
2035
2036         bus_dmamap_sync(sc->jme_cdata.jme_tx_ring_tag,
2037                         sc->jme_cdata.jme_tx_ring_map,
2038                         BUS_DMASYNC_PREWRITE);
2039 }
2040
2041 static __inline void
2042 jme_discard_rxbufs(struct jme_softc *sc, int cons, int count)
2043 {
2044         int i;
2045
2046         for (i = 0; i < count; ++i) {
2047                 struct jme_desc *desc = &sc->jme_rdata.jme_rx_ring[cons];
2048
2049                 desc->flags = htole32(JME_RD_OWN | JME_RD_INTR | JME_RD_64BIT);
2050                 desc->buflen = htole32(MCLBYTES);
2051                 JME_DESC_INC(cons, JME_RX_RING_CNT);
2052         }
2053 }
2054
2055 /* Receive a frame. */
2056 static void
2057 jme_rxpkt(struct jme_softc *sc)
2058 {
2059         struct ifnet *ifp = &sc->arpcom.ac_if;
2060         struct jme_desc *desc;
2061         struct jme_rxdesc *rxd;
2062         struct mbuf *mp, *m;
2063         uint32_t flags, status;
2064         int cons, count, nsegs;
2065
2066         cons = sc->jme_cdata.jme_rx_cons;
2067         desc = &sc->jme_rdata.jme_rx_ring[cons];
2068         flags = le32toh(desc->flags);
2069         status = le32toh(desc->buflen);
2070         nsegs = JME_RX_NSEGS(status);
2071
2072         if (status & JME_RX_ERR_STAT) {
2073                 ifp->if_ierrors++;
2074                 jme_discard_rxbufs(sc, cons, nsegs);
2075 #ifdef JME_SHOW_ERRORS
2076                 device_printf(sc->jme_dev, "%s : receive error = 0x%b\n",
2077                     __func__, JME_RX_ERR(status), JME_RX_ERR_BITS);
2078 #endif
2079                 sc->jme_cdata.jme_rx_cons += nsegs;
2080                 sc->jme_cdata.jme_rx_cons %= JME_RX_RING_CNT;
2081                 return;
2082         }
2083
2084         sc->jme_cdata.jme_rxlen = JME_RX_BYTES(status) - JME_RX_PAD_BYTES;
2085         for (count = 0; count < nsegs; count++,
2086              JME_DESC_INC(cons, JME_RX_RING_CNT)) {
2087                 rxd = &sc->jme_cdata.jme_rxdesc[cons];
2088                 mp = rxd->rx_m;
2089
2090                 /* Add a new receive buffer to the ring. */
2091                 if (jme_newbuf(sc, rxd, 0) != 0) {
2092                         ifp->if_iqdrops++;
2093                         /* Reuse buffer. */
2094                         jme_discard_rxbufs(sc, cons, nsegs - count);
2095                         if (sc->jme_cdata.jme_rxhead != NULL) {
2096                                 m_freem(sc->jme_cdata.jme_rxhead);
2097                                 JME_RXCHAIN_RESET(sc);
2098                         }
2099                         break;
2100                 }
2101
2102                 /*
2103                  * Assume we've received a full sized frame.
2104                  * Actual size is fixed when we encounter the end of
2105                  * multi-segmented frame.
2106                  */
2107                 mp->m_len = MCLBYTES;
2108
2109                 /* Chain received mbufs. */
2110                 if (sc->jme_cdata.jme_rxhead == NULL) {
2111                         sc->jme_cdata.jme_rxhead = mp;
2112                         sc->jme_cdata.jme_rxtail = mp;
2113                 } else {
2114                         /*
2115                          * Receive processor can receive a maximum frame
2116                          * size of 65535 bytes.
2117                          */
2118                         mp->m_flags &= ~M_PKTHDR;
2119                         sc->jme_cdata.jme_rxtail->m_next = mp;
2120                         sc->jme_cdata.jme_rxtail = mp;
2121                 }
2122
2123                 if (count == nsegs - 1) {
2124                         /* Last desc. for this frame. */
2125                         m = sc->jme_cdata.jme_rxhead;
2126                         /* XXX assert PKTHDR? */
2127                         m->m_flags |= M_PKTHDR;
2128                         m->m_pkthdr.len = sc->jme_cdata.jme_rxlen;
2129                         if (nsegs > 1) {
2130                                 /* Set first mbuf size. */
2131                                 m->m_len = MCLBYTES - JME_RX_PAD_BYTES;
2132                                 /* Set last mbuf size. */
2133                                 mp->m_len = sc->jme_cdata.jme_rxlen -
2134                                     ((MCLBYTES - JME_RX_PAD_BYTES) +
2135                                     (MCLBYTES * (nsegs - 2)));
2136                         } else {
2137                                 m->m_len = sc->jme_cdata.jme_rxlen;
2138                         }
2139                         m->m_pkthdr.rcvif = ifp;
2140
2141                         /*
2142                          * Account for 10bytes auto padding which is used
2143                          * to align IP header on 32bit boundary. Also note,
2144                          * CRC bytes is automatically removed by the
2145                          * hardware.
2146                          */
2147                         m->m_data += JME_RX_PAD_BYTES;
2148
2149                         /* Set checksum information. */
2150                         if ((ifp->if_capenable & IFCAP_RXCSUM) &&
2151                             (flags & JME_RD_IPV4)) {
2152                                 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
2153                                 if (flags & JME_RD_IPCSUM)
2154                                         m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2155                                 if ((flags & JME_RD_MORE_FRAG) == 0 &&
2156                                     ((flags & (JME_RD_TCP | JME_RD_TCPCSUM)) ==
2157                                      (JME_RD_TCP | JME_RD_TCPCSUM) ||
2158                                      (flags & (JME_RD_UDP | JME_RD_UDPCSUM)) ==
2159                                      (JME_RD_UDP | JME_RD_UDPCSUM))) {
2160                                         m->m_pkthdr.csum_flags |=
2161                                             CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
2162                                         m->m_pkthdr.csum_data = 0xffff;
2163                                 }
2164                         }
2165
2166                         /* Check for VLAN tagged packets. */
2167                         if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) &&
2168                             (flags & JME_RD_VLAN_TAG)) {
2169                                 m->m_pkthdr.ether_vlantag =
2170                                     flags & JME_RD_VLAN_MASK;
2171                                 m->m_flags |= M_VLANTAG;
2172                         }
2173
2174                         ifp->if_ipackets++;
2175                         /* Pass it on. */
2176                         ifp->if_input(ifp, m);
2177
2178                         /* Reset mbuf chains. */
2179                         JME_RXCHAIN_RESET(sc);
2180                 }
2181         }
2182
2183         sc->jme_cdata.jme_rx_cons += nsegs;
2184         sc->jme_cdata.jme_rx_cons %= JME_RX_RING_CNT;
2185 }
2186
2187 static void
2188 jme_rxeof(struct jme_softc *sc)
2189 {
2190         struct jme_desc *desc;
2191         int nsegs, prog, pktlen;
2192
2193         bus_dmamap_sync(sc->jme_cdata.jme_rx_ring_tag,
2194                         sc->jme_cdata.jme_rx_ring_map,
2195                         BUS_DMASYNC_POSTREAD);
2196
2197         prog = 0;
2198         for (;;) {
2199                 desc = &sc->jme_rdata.jme_rx_ring[sc->jme_cdata.jme_rx_cons];
2200                 if ((le32toh(desc->flags) & JME_RD_OWN) == JME_RD_OWN)
2201                         break;
2202                 if ((le32toh(desc->buflen) & JME_RD_VALID) == 0)
2203                         break;
2204
2205                 /*
2206                  * Check number of segments against received bytes.
2207                  * Non-matching value would indicate that hardware
2208                  * is still trying to update Rx descriptors. I'm not
2209                  * sure whether this check is needed.
2210                  */
2211                 nsegs = JME_RX_NSEGS(le32toh(desc->buflen));
2212                 pktlen = JME_RX_BYTES(le32toh(desc->buflen));
2213                 if (nsegs != howmany(pktlen, MCLBYTES)) {
2214                         if_printf(&sc->arpcom.ac_if, "RX fragment count(%d) "
2215                                   "and packet size(%d) mismach\n",
2216                                   nsegs, pktlen);
2217                         break;
2218                 }
2219
2220                 /* Received a frame. */
2221                 jme_rxpkt(sc);
2222                 prog++;
2223         }
2224
2225         if (prog > 0) {
2226                 bus_dmamap_sync(sc->jme_cdata.jme_rx_ring_tag,
2227                                 sc->jme_cdata.jme_rx_ring_map,
2228                                 BUS_DMASYNC_PREWRITE);
2229         }
2230 }
2231
2232 static void
2233 jme_tick(void *xsc)
2234 {
2235         struct jme_softc *sc = xsc;
2236         struct ifnet *ifp = &sc->arpcom.ac_if;
2237         struct mii_data *mii = device_get_softc(sc->jme_miibus);
2238
2239         lwkt_serialize_enter(ifp->if_serializer);
2240
2241         mii_tick(mii);
2242         callout_reset(&sc->jme_tick_ch, hz, jme_tick, sc);
2243
2244         lwkt_serialize_exit(ifp->if_serializer);
2245 }
2246
2247 static void
2248 jme_reset(struct jme_softc *sc)
2249 {
2250 #ifdef foo
2251         /* Stop receiver, transmitter. */
2252         jme_stop_rx(sc);
2253         jme_stop_tx(sc);
2254 #endif
2255         CSR_WRITE_4(sc, JME_GHC, GHC_RESET);
2256         DELAY(10);
2257         CSR_WRITE_4(sc, JME_GHC, 0);
2258 }
2259
2260 static void
2261 jme_init(void *xsc)
2262 {
2263         struct jme_softc *sc = xsc;
2264         struct ifnet *ifp = &sc->arpcom.ac_if;
2265         struct mii_data *mii;
2266         uint8_t eaddr[ETHER_ADDR_LEN];
2267         bus_addr_t paddr;
2268         uint32_t reg;
2269         int error;
2270
2271         ASSERT_SERIALIZED(ifp->if_serializer);
2272
2273         /*
2274          * Cancel any pending I/O.
2275          */
2276         jme_stop(sc);
2277
2278         /*
2279          * Reset the chip to a known state.
2280          */
2281         jme_reset(sc);
2282
2283         /*
2284          * Since we always use 64bit address mode for transmitting,
2285          * each Tx request requires one more dummy descriptor.
2286          */
2287         sc->jme_txd_spare =
2288         howmany(ifp->if_mtu + sizeof(struct ether_vlan_header), MCLBYTES) + 1;
2289         KKASSERT(sc->jme_txd_spare >= 2);
2290
2291         /* Init descriptors. */
2292         error = jme_init_rx_ring(sc);
2293         if (error != 0) {
2294                 device_printf(sc->jme_dev,
2295                     "%s: initialization failed: no memory for Rx buffers.\n",
2296                     __func__);
2297                 jme_stop(sc);
2298                 return;
2299         }
2300         jme_init_tx_ring(sc);
2301
2302         /* Initialize shadow status block. */
2303         jme_init_ssb(sc);
2304
2305         /* Reprogram the station address. */
2306         bcopy(IF_LLADDR(ifp), eaddr, ETHER_ADDR_LEN);
2307         CSR_WRITE_4(sc, JME_PAR0,
2308             eaddr[3] << 24 | eaddr[2] << 16 | eaddr[1] << 8 | eaddr[0]);
2309         CSR_WRITE_4(sc, JME_PAR1, eaddr[5] << 8 | eaddr[4]);
2310
2311         /*
2312          * Configure Tx queue.
2313          *  Tx priority queue weight value : 0
2314          *  Tx FIFO threshold for processing next packet : 16QW
2315          *  Maximum Tx DMA length : 512
2316          *  Allow Tx DMA burst.
2317          */
2318         sc->jme_txcsr = TXCSR_TXQ_N_SEL(TXCSR_TXQ0);
2319         sc->jme_txcsr |= TXCSR_TXQ_WEIGHT(TXCSR_TXQ_WEIGHT_MIN);
2320         sc->jme_txcsr |= TXCSR_FIFO_THRESH_16QW;
2321         sc->jme_txcsr |= sc->jme_tx_dma_size;
2322         sc->jme_txcsr |= TXCSR_DMA_BURST;
2323         CSR_WRITE_4(sc, JME_TXCSR, sc->jme_txcsr);
2324
2325         /* Set Tx descriptor counter. */
2326         CSR_WRITE_4(sc, JME_TXQDC, JME_TX_RING_CNT);
2327
2328         /* Set Tx ring address to the hardware. */
2329         paddr = JME_TX_RING_ADDR(sc, 0);
2330         CSR_WRITE_4(sc, JME_TXDBA_HI, JME_ADDR_HI(paddr));
2331         CSR_WRITE_4(sc, JME_TXDBA_LO, JME_ADDR_LO(paddr));
2332
2333         /* Configure TxMAC parameters. */
2334         reg = TXMAC_IFG1_DEFAULT | TXMAC_IFG2_DEFAULT | TXMAC_IFG_ENB;
2335         reg |= TXMAC_THRESH_1_PKT;
2336         reg |= TXMAC_CRC_ENB | TXMAC_PAD_ENB;
2337         CSR_WRITE_4(sc, JME_TXMAC, reg);
2338
2339         /*
2340          * Configure Rx queue.
2341          *  FIFO full threshold for transmitting Tx pause packet : 128T
2342          *  FIFO threshold for processing next packet : 128QW
2343          *  Rx queue 0 select
2344          *  Max Rx DMA length : 128
2345          *  Rx descriptor retry : 32
2346          *  Rx descriptor retry time gap : 256ns
2347          *  Don't receive runt/bad frame.
2348          */
2349         sc->jme_rxcsr = RXCSR_FIFO_FTHRESH_128T;
2350         /*
2351          * Since Rx FIFO size is 4K bytes, receiving frames larger
2352          * than 4K bytes will suffer from Rx FIFO overruns. So
2353          * decrease FIFO threshold to reduce the FIFO overruns for
2354          * frames larger than 4000 bytes.
2355          * For best performance of standard MTU sized frames use
2356          * maximum allowable FIFO threshold, 128QW.
2357          */
2358         if ((ifp->if_mtu + ETHER_HDR_LEN + EVL_ENCAPLEN + ETHER_CRC_LEN) >
2359             JME_RX_FIFO_SIZE)
2360                 sc->jme_rxcsr |= RXCSR_FIFO_THRESH_16QW;
2361         else
2362                 sc->jme_rxcsr |= RXCSR_FIFO_THRESH_128QW;
2363         sc->jme_rxcsr |= sc->jme_rx_dma_size | RXCSR_RXQ_N_SEL(RXCSR_RXQ0);
2364         sc->jme_rxcsr |= RXCSR_DESC_RT_CNT(RXCSR_DESC_RT_CNT_DEFAULT);
2365         sc->jme_rxcsr |= RXCSR_DESC_RT_GAP_256 & RXCSR_DESC_RT_GAP_MASK;
2366         /* XXX TODO DROP_BAD */
2367         CSR_WRITE_4(sc, JME_RXCSR, sc->jme_rxcsr);
2368
2369         /* Set Rx descriptor counter. */
2370         CSR_WRITE_4(sc, JME_RXQDC, JME_RX_RING_CNT);
2371
2372         /* Set Rx ring address to the hardware. */
2373         paddr = JME_RX_RING_ADDR(sc, 0);
2374         CSR_WRITE_4(sc, JME_RXDBA_HI, JME_ADDR_HI(paddr));
2375         CSR_WRITE_4(sc, JME_RXDBA_LO, JME_ADDR_LO(paddr));
2376
2377         /* Clear receive filter. */
2378         CSR_WRITE_4(sc, JME_RXMAC, 0);
2379
2380         /* Set up the receive filter. */
2381         jme_set_filter(sc);
2382         jme_set_vlan(sc);
2383
2384         /*
2385          * Disable all WOL bits as WOL can interfere normal Rx
2386          * operation. Also clear WOL detection status bits.
2387          */
2388         reg = CSR_READ_4(sc, JME_PMCS);
2389         reg &= ~PMCS_WOL_ENB_MASK;
2390         CSR_WRITE_4(sc, JME_PMCS, reg);
2391
2392         /*
2393          * Pad 10bytes right before received frame. This will greatly
2394          * help Rx performance on strict-alignment architectures as
2395          * it does not need to copy the frame to align the payload.
2396          */
2397         reg = CSR_READ_4(sc, JME_RXMAC);
2398         reg |= RXMAC_PAD_10BYTES;
2399
2400         if (ifp->if_capenable & IFCAP_RXCSUM)
2401                 reg |= RXMAC_CSUM_ENB;
2402         CSR_WRITE_4(sc, JME_RXMAC, reg);
2403
2404         /* Configure general purpose reg0 */
2405         reg = CSR_READ_4(sc, JME_GPREG0);
2406         reg &= ~GPREG0_PCC_UNIT_MASK;
2407         /* Set PCC timer resolution to micro-seconds unit. */
2408         reg |= GPREG0_PCC_UNIT_US;
2409         /*
2410          * Disable all shadow register posting as we have to read
2411          * JME_INTR_STATUS register in jme_intr. Also it seems
2412          * that it's hard to synchronize interrupt status between
2413          * hardware and software with shadow posting due to
2414          * requirements of bus_dmamap_sync(9).
2415          */
2416         reg |= GPREG0_SH_POST_DW7_DIS | GPREG0_SH_POST_DW6_DIS |
2417             GPREG0_SH_POST_DW5_DIS | GPREG0_SH_POST_DW4_DIS |
2418             GPREG0_SH_POST_DW3_DIS | GPREG0_SH_POST_DW2_DIS |
2419             GPREG0_SH_POST_DW1_DIS | GPREG0_SH_POST_DW0_DIS;
2420         /* Disable posting of DW0. */
2421         reg &= ~GPREG0_POST_DW0_ENB;
2422         /* Clear PME message. */
2423         reg &= ~GPREG0_PME_ENB;
2424         /* Set PHY address. */
2425         reg &= ~GPREG0_PHY_ADDR_MASK;
2426         reg |= sc->jme_phyaddr;
2427         CSR_WRITE_4(sc, JME_GPREG0, reg);
2428
2429         /* Configure Tx queue 0 packet completion coalescing. */
2430         reg = (sc->jme_tx_coal_to << PCCTX_COAL_TO_SHIFT) &
2431             PCCTX_COAL_TO_MASK;
2432         reg |= (sc->jme_tx_coal_pkt << PCCTX_COAL_PKT_SHIFT) &
2433             PCCTX_COAL_PKT_MASK;
2434         reg |= PCCTX_COAL_TXQ0;
2435         CSR_WRITE_4(sc, JME_PCCTX, reg);
2436
2437         /* Configure Rx queue 0 packet completion coalescing. */
2438         reg = (sc->jme_rx_coal_to << PCCRX_COAL_TO_SHIFT) &
2439             PCCRX_COAL_TO_MASK;
2440         reg |= (sc->jme_rx_coal_pkt << PCCRX_COAL_PKT_SHIFT) &
2441             PCCRX_COAL_PKT_MASK;
2442         CSR_WRITE_4(sc, JME_PCCRX0, reg);
2443
2444         /* Configure shadow status block but don't enable posting. */
2445         paddr = sc->jme_rdata.jme_ssb_block_paddr;
2446         CSR_WRITE_4(sc, JME_SHBASE_ADDR_HI, JME_ADDR_HI(paddr));
2447         CSR_WRITE_4(sc, JME_SHBASE_ADDR_LO, JME_ADDR_LO(paddr));
2448
2449         /* Disable Timer 1 and Timer 2. */
2450         CSR_WRITE_4(sc, JME_TIMER1, 0);
2451         CSR_WRITE_4(sc, JME_TIMER2, 0);
2452
2453         /* Configure retry transmit period, retry limit value. */
2454         CSR_WRITE_4(sc, JME_TXTRHD,
2455             ((TXTRHD_RT_PERIOD_DEFAULT << TXTRHD_RT_PERIOD_SHIFT) &
2456             TXTRHD_RT_PERIOD_MASK) |
2457             ((TXTRHD_RT_LIMIT_DEFAULT << TXTRHD_RT_LIMIT_SHIFT) &
2458             TXTRHD_RT_LIMIT_SHIFT));
2459
2460         /* Disable RSS. */
2461         CSR_WRITE_4(sc, JME_RSSC, RSSC_DIS_RSS);
2462
2463         /* Initialize the interrupt mask. */
2464         CSR_WRITE_4(sc, JME_INTR_MASK_SET, JME_INTRS);
2465         CSR_WRITE_4(sc, JME_INTR_STATUS, 0xFFFFFFFF);
2466
2467         /*
2468          * Enabling Tx/Rx DMA engines and Rx queue processing is
2469          * done after detection of valid link in jme_miibus_statchg.
2470          */
2471         sc->jme_flags &= ~JME_FLAG_LINK;
2472
2473         /* Set the current media. */
2474         mii = device_get_softc(sc->jme_miibus);
2475         mii_mediachg(mii);
2476
2477         callout_reset(&sc->jme_tick_ch, hz, jme_tick, sc);
2478
2479         ifp->if_flags |= IFF_RUNNING;
2480         ifp->if_flags &= ~IFF_OACTIVE;
2481 }
2482
2483 static void
2484 jme_stop(struct jme_softc *sc)
2485 {
2486         struct ifnet *ifp = &sc->arpcom.ac_if;
2487         struct jme_txdesc *txd;
2488         struct jme_rxdesc *rxd;
2489         int i;
2490
2491         ASSERT_SERIALIZED(ifp->if_serializer);
2492
2493         /*
2494          * Mark the interface down and cancel the watchdog timer.
2495          */
2496         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2497         ifp->if_timer = 0;
2498
2499         callout_stop(&sc->jme_tick_ch);
2500         sc->jme_flags &= ~JME_FLAG_LINK;
2501
2502         /*
2503          * Disable interrupts.
2504          */
2505         CSR_WRITE_4(sc, JME_INTR_MASK_CLR, JME_INTRS);
2506         CSR_WRITE_4(sc, JME_INTR_STATUS, 0xFFFFFFFF);
2507
2508         /* Disable updating shadow status block. */
2509         CSR_WRITE_4(sc, JME_SHBASE_ADDR_LO,
2510             CSR_READ_4(sc, JME_SHBASE_ADDR_LO) & ~SHBASE_POST_ENB);
2511
2512         /* Stop receiver, transmitter. */
2513         jme_stop_rx(sc);
2514         jme_stop_tx(sc);
2515
2516 #ifdef foo
2517          /* Reclaim Rx/Tx buffers that have been completed. */
2518         jme_rxeof(sc);
2519         if (sc->jme_cdata.jme_rxhead != NULL)
2520                 m_freem(sc->jme_cdata.jme_rxhead);
2521         JME_RXCHAIN_RESET(sc);
2522         jme_txeof(sc);
2523 #endif
2524
2525         /*
2526          * Free partial finished RX segments
2527          */
2528         if (sc->jme_cdata.jme_rxhead != NULL)
2529                 m_freem(sc->jme_cdata.jme_rxhead);
2530         JME_RXCHAIN_RESET(sc);
2531
2532         /*
2533          * Free RX and TX mbufs still in the queues.
2534          */
2535         for (i = 0; i < JME_RX_RING_CNT; i++) {
2536                 rxd = &sc->jme_cdata.jme_rxdesc[i];
2537                 if (rxd->rx_m != NULL) {
2538                         bus_dmamap_unload(sc->jme_cdata.jme_rx_tag,
2539                             rxd->rx_dmamap);
2540                         m_freem(rxd->rx_m);
2541                         rxd->rx_m = NULL;
2542                 }
2543         }
2544         for (i = 0; i < JME_TX_RING_CNT; i++) {
2545                 txd = &sc->jme_cdata.jme_txdesc[i];
2546                 if (txd->tx_m != NULL) {
2547                         bus_dmamap_unload(sc->jme_cdata.jme_tx_tag,
2548                             txd->tx_dmamap);
2549                         m_freem(txd->tx_m);
2550                         txd->tx_m = NULL;
2551                         txd->tx_ndesc = 0;
2552                 }
2553         }
2554 }
2555
2556 static void
2557 jme_stop_tx(struct jme_softc *sc)
2558 {
2559         uint32_t reg;
2560         int i;
2561
2562         reg = CSR_READ_4(sc, JME_TXCSR);
2563         if ((reg & TXCSR_TX_ENB) == 0)
2564                 return;
2565         reg &= ~TXCSR_TX_ENB;
2566         CSR_WRITE_4(sc, JME_TXCSR, reg);
2567         for (i = JME_TIMEOUT; i > 0; i--) {
2568                 DELAY(1);
2569                 if ((CSR_READ_4(sc, JME_TXCSR) & TXCSR_TX_ENB) == 0)
2570                         break;
2571         }
2572         if (i == 0)
2573                 device_printf(sc->jme_dev, "stopping transmitter timeout!\n");
2574 }
2575
2576 static void
2577 jme_stop_rx(struct jme_softc *sc)
2578 {
2579         uint32_t reg;
2580         int i;
2581
2582         reg = CSR_READ_4(sc, JME_RXCSR);
2583         if ((reg & RXCSR_RX_ENB) == 0)
2584                 return;
2585         reg &= ~RXCSR_RX_ENB;
2586         CSR_WRITE_4(sc, JME_RXCSR, reg);
2587         for (i = JME_TIMEOUT; i > 0; i--) {
2588                 DELAY(1);
2589                 if ((CSR_READ_4(sc, JME_RXCSR) & RXCSR_RX_ENB) == 0)
2590                         break;
2591         }
2592         if (i == 0)
2593                 device_printf(sc->jme_dev, "stopping recevier timeout!\n");
2594 }
2595
2596 static void
2597 jme_init_tx_ring(struct jme_softc *sc)
2598 {
2599         struct jme_ring_data *rd;
2600         struct jme_txdesc *txd;
2601         int i;
2602
2603         sc->jme_cdata.jme_tx_prod = 0;
2604         sc->jme_cdata.jme_tx_cons = 0;
2605         sc->jme_cdata.jme_tx_cnt = 0;
2606
2607         rd = &sc->jme_rdata;
2608         bzero(rd->jme_tx_ring, JME_TX_RING_SIZE);
2609         for (i = 0; i < JME_TX_RING_CNT; i++) {
2610                 txd = &sc->jme_cdata.jme_txdesc[i];
2611                 txd->tx_m = NULL;
2612                 txd->tx_desc = &rd->jme_tx_ring[i];
2613                 txd->tx_ndesc = 0;
2614         }
2615
2616         bus_dmamap_sync(sc->jme_cdata.jme_tx_ring_tag,
2617                         sc->jme_cdata.jme_tx_ring_map,
2618                         BUS_DMASYNC_PREWRITE);
2619 }
2620
2621 static void
2622 jme_init_ssb(struct jme_softc *sc)
2623 {
2624         struct jme_ring_data *rd;
2625
2626         rd = &sc->jme_rdata;
2627         bzero(rd->jme_ssb_block, JME_SSB_SIZE);
2628         bus_dmamap_sync(sc->jme_cdata.jme_ssb_tag, sc->jme_cdata.jme_ssb_map,
2629                         BUS_DMASYNC_PREWRITE);
2630 }
2631
2632 static int
2633 jme_init_rx_ring(struct jme_softc *sc)
2634 {
2635         struct jme_ring_data *rd;
2636         struct jme_rxdesc *rxd;
2637         int i;
2638
2639         KKASSERT(sc->jme_cdata.jme_rxhead == NULL &&
2640                  sc->jme_cdata.jme_rxtail == NULL &&
2641                  sc->jme_cdata.jme_rxlen == 0);
2642         sc->jme_cdata.jme_rx_cons = 0;
2643
2644         rd = &sc->jme_rdata;
2645         bzero(rd->jme_rx_ring, JME_RX_RING_SIZE);
2646         for (i = 0; i < JME_RX_RING_CNT; i++) {
2647                 int error;
2648
2649                 rxd = &sc->jme_cdata.jme_rxdesc[i];
2650                 rxd->rx_m = NULL;
2651                 rxd->rx_desc = &rd->jme_rx_ring[i];
2652                 error = jme_newbuf(sc, rxd, 1);
2653                 if (error)
2654                         return (error);
2655         }
2656
2657         bus_dmamap_sync(sc->jme_cdata.jme_rx_ring_tag,
2658                         sc->jme_cdata.jme_rx_ring_map,
2659                         BUS_DMASYNC_PREWRITE);
2660         return (0);
2661 }
2662
2663 static int
2664 jme_newbuf(struct jme_softc *sc, struct jme_rxdesc *rxd, int init)
2665 {
2666         struct jme_desc *desc;
2667         struct mbuf *m;
2668         struct jme_dmamap_ctx ctx;
2669         bus_dma_segment_t segs;
2670         bus_dmamap_t map;
2671         int error;
2672
2673         m = m_getcl(init ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
2674         if (m == NULL)
2675                 return (ENOBUFS);
2676         /*
2677          * JMC250 has 64bit boundary alignment limitation so jme(4)
2678          * takes advantage of 10 bytes padding feature of hardware
2679          * in order not to copy entire frame to align IP header on
2680          * 32bit boundary.
2681          */
2682         m->m_len = m->m_pkthdr.len = MCLBYTES;
2683
2684         ctx.nsegs = 1;
2685         ctx.segs = &segs;
2686         error = bus_dmamap_load_mbuf(sc->jme_cdata.jme_rx_tag,
2687                                      sc->jme_cdata.jme_rx_sparemap,
2688                                      m, jme_dmamap_buf_cb, &ctx,
2689                                      BUS_DMA_NOWAIT);
2690         if (error || ctx.nsegs == 0) {
2691                 if (!error) {
2692                         bus_dmamap_unload(sc->jme_cdata.jme_rx_tag,
2693                                           sc->jme_cdata.jme_rx_sparemap);
2694                         error = EFBIG;
2695                         if_printf(&sc->arpcom.ac_if, "too many segments?!\n");
2696                 }
2697                 m_freem(m);
2698
2699                 if (init)
2700                         if_printf(&sc->arpcom.ac_if, "can't load RX mbuf\n");
2701                 return (error);
2702         }
2703
2704         if (rxd->rx_m != NULL) {
2705                 bus_dmamap_sync(sc->jme_cdata.jme_rx_tag, rxd->rx_dmamap,
2706                                 BUS_DMASYNC_POSTREAD);
2707                 bus_dmamap_unload(sc->jme_cdata.jme_rx_tag, rxd->rx_dmamap);
2708         }
2709         map = rxd->rx_dmamap;
2710         rxd->rx_dmamap = sc->jme_cdata.jme_rx_sparemap;
2711         sc->jme_cdata.jme_rx_sparemap = map;
2712         rxd->rx_m = m;
2713
2714         desc = rxd->rx_desc;
2715         desc->buflen = htole32(segs.ds_len);
2716         desc->addr_lo = htole32(JME_ADDR_LO(segs.ds_addr));
2717         desc->addr_hi = htole32(JME_ADDR_HI(segs.ds_addr));
2718         desc->flags = htole32(JME_RD_OWN | JME_RD_INTR | JME_RD_64BIT);
2719
2720         return (0);
2721 }
2722
2723 static void
2724 jme_set_vlan(struct jme_softc *sc)
2725 {
2726         struct ifnet *ifp = &sc->arpcom.ac_if;
2727         uint32_t reg;
2728
2729         ASSERT_SERIALIZED(ifp->if_serializer);
2730
2731         reg = CSR_READ_4(sc, JME_RXMAC);
2732         reg &= ~RXMAC_VLAN_ENB;
2733         if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
2734                 reg |= RXMAC_VLAN_ENB;
2735         CSR_WRITE_4(sc, JME_RXMAC, reg);
2736 }
2737
2738 static void
2739 jme_set_filter(struct jme_softc *sc)
2740 {
2741         struct ifnet *ifp = &sc->arpcom.ac_if;
2742         struct ifmultiaddr *ifma;
2743         uint32_t crc;
2744         uint32_t mchash[2];
2745         uint32_t rxcfg;
2746
2747         ASSERT_SERIALIZED(ifp->if_serializer);
2748
2749         rxcfg = CSR_READ_4(sc, JME_RXMAC);
2750         rxcfg &= ~(RXMAC_BROADCAST | RXMAC_PROMISC | RXMAC_MULTICAST |
2751             RXMAC_ALLMULTI);
2752
2753         /*
2754          * Always accept frames destined to our station address.
2755          * Always accept broadcast frames.
2756          */
2757         rxcfg |= RXMAC_UNICAST | RXMAC_BROADCAST;
2758
2759         if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) {
2760                 if (ifp->if_flags & IFF_PROMISC)
2761                         rxcfg |= RXMAC_PROMISC;
2762                 if (ifp->if_flags & IFF_ALLMULTI)
2763                         rxcfg |= RXMAC_ALLMULTI;
2764                 CSR_WRITE_4(sc, JME_MAR0, 0xFFFFFFFF);
2765                 CSR_WRITE_4(sc, JME_MAR1, 0xFFFFFFFF);
2766                 CSR_WRITE_4(sc, JME_RXMAC, rxcfg);
2767                 return;
2768         }
2769
2770         /*
2771          * Set up the multicast address filter by passing all multicast
2772          * addresses through a CRC generator, and then using the low-order
2773          * 6 bits as an index into the 64 bit multicast hash table.  The
2774          * high order bits select the register, while the rest of the bits
2775          * select the bit within the register.
2776          */
2777         rxcfg |= RXMAC_MULTICAST;
2778         bzero(mchash, sizeof(mchash));
2779
2780         LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2781                 if (ifma->ifma_addr->sa_family != AF_LINK)
2782                         continue;
2783                 crc = ether_crc32_be(LLADDR((struct sockaddr_dl *)
2784                     ifma->ifma_addr), ETHER_ADDR_LEN);
2785
2786                 /* Just want the 6 least significant bits. */
2787                 crc &= 0x3f;
2788
2789                 /* Set the corresponding bit in the hash table. */
2790                 mchash[crc >> 5] |= 1 << (crc & 0x1f);
2791         }
2792
2793         CSR_WRITE_4(sc, JME_MAR0, mchash[0]);
2794         CSR_WRITE_4(sc, JME_MAR1, mchash[1]);
2795         CSR_WRITE_4(sc, JME_RXMAC, rxcfg);
2796 }
2797
2798 static int
2799 sysctl_hw_jme_tx_coal_to(SYSCTL_HANDLER_ARGS)
2800 {
2801         return (sysctl_int_range(oidp, arg1, arg2, req,
2802             PCCTX_COAL_TO_MIN, PCCTX_COAL_TO_MAX));
2803 }
2804
2805 static int
2806 sysctl_hw_jme_tx_coal_pkt(SYSCTL_HANDLER_ARGS)
2807 {
2808         return (sysctl_int_range(oidp, arg1, arg2, req,
2809             PCCTX_COAL_PKT_MIN, PCCTX_COAL_PKT_MAX));
2810 }
2811
2812 static int
2813 sysctl_hw_jme_rx_coal_to(SYSCTL_HANDLER_ARGS)
2814 {
2815         return (sysctl_int_range(oidp, arg1, arg2, req,
2816             PCCRX_COAL_TO_MIN, PCCRX_COAL_TO_MAX));
2817 }
2818
2819 static int
2820 sysctl_hw_jme_rx_coal_pkt(SYSCTL_HANDLER_ARGS)
2821 {
2822         return (sysctl_int_range(oidp, arg1, arg2, req,
2823             PCCRX_COAL_PKT_MIN, PCCRX_COAL_PKT_MAX));
2824 }