source: rtems-libbsd/freebsd/sys/dev/smc/if_smc.c @ 66659ff

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 66659ff was 66659ff, checked in by Sebastian Huber <sebastian.huber@…>, on 11/06/13 at 15:20:21

Update to FreeBSD 9.2

  • Property mode set to 100644
File size: 27.3 KB
Line 
1#include <machine/rtems-bsd-kernel-space.h>
2
3/*-
4 * Copyright (c) 2008 Benno Rice.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following 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 ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
29
30/*
31 * Driver for SMSC LAN91C111, may work for older variants.
32 */
33
34#ifdef HAVE_KERNEL_OPTION_HEADERS
35#include <rtems/bsd/local/opt_device_polling.h>
36#endif
37
38#include <rtems/bsd/sys/param.h>
39#include <sys/systm.h>
40#include <rtems/bsd/sys/errno.h>
41#include <sys/kernel.h>
42#include <sys/sockio.h>
43#include <sys/malloc.h>
44#include <sys/mbuf.h>
45#include <sys/queue.h>
46#include <sys/socket.h>
47#include <sys/syslog.h>
48#include <sys/taskqueue.h>
49
50#include <sys/module.h>
51#include <sys/bus.h>
52
53#include <machine/bus.h>
54#include <machine/resource.h>
55#include <sys/rman.h>
56
57#include <net/ethernet.h>
58#include <net/if.h>
59#include <net/if_arp.h>
60#include <net/if_dl.h>
61#include <net/if_types.h>
62#include <net/if_mib.h>
63#include <net/if_media.h>
64
65#ifdef INET
66#include <netinet/in.h>
67#include <netinet/in_systm.h>
68#include <netinet/in_var.h>
69#include <netinet/ip.h>
70#endif
71
72#include <net/bpf.h>
73#include <net/bpfdesc.h>
74
75#include <dev/smc/if_smcreg.h>
76#include <dev/smc/if_smcvar.h>
77
78#include <dev/mii/mii.h>
79#include <dev/mii/mii_bitbang.h>
80#include <dev/mii/miivar.h>
81
82#define SMC_LOCK(sc)            mtx_lock(&(sc)->smc_mtx)
83#define SMC_UNLOCK(sc)          mtx_unlock(&(sc)->smc_mtx)
84#define SMC_ASSERT_LOCKED(sc)   mtx_assert(&(sc)->smc_mtx, MA_OWNED)
85
86#define SMC_INTR_PRIORITY       0
87#define SMC_RX_PRIORITY         5
88#define SMC_TX_PRIORITY         10
89
90devclass_t      smc_devclass;
91
92static const char *smc_chip_ids[16] = {
93        NULL, NULL, NULL,
94        /* 3 */ "SMSC LAN91C90 or LAN91C92",
95        /* 4 */ "SMSC LAN91C94",
96        /* 5 */ "SMSC LAN91C95",
97        /* 6 */ "SMSC LAN91C96",
98        /* 7 */ "SMSC LAN91C100",
99        /* 8 */ "SMSC LAN91C100FD",
100        /* 9 */ "SMSC LAN91C110FD or LAN91C111FD",
101        NULL, NULL, NULL,
102        NULL, NULL, NULL
103};
104
105static void     smc_init(void *);
106static void     smc_start(struct ifnet *);
107static void     smc_stop(struct smc_softc *);
108static int      smc_ioctl(struct ifnet *, u_long, caddr_t);
109
110static void     smc_init_locked(struct smc_softc *);
111static void     smc_start_locked(struct ifnet *);
112static void     smc_reset(struct smc_softc *);
113static int      smc_mii_ifmedia_upd(struct ifnet *);
114static void     smc_mii_ifmedia_sts(struct ifnet *, struct ifmediareq *);
115static void     smc_mii_tick(void *);
116static void     smc_mii_mediachg(struct smc_softc *);
117static int      smc_mii_mediaioctl(struct smc_softc *, struct ifreq *, u_long);
118
119static void     smc_task_intr(void *, int);
120static void     smc_task_rx(void *, int);
121static void     smc_task_tx(void *, int);
122
123static driver_filter_t  smc_intr;
124static timeout_t        smc_watchdog;
125#ifdef DEVICE_POLLING
126static poll_handler_t   smc_poll;
127#endif
128
129/*
130 * MII bit-bang glue
131 */
132static uint32_t smc_mii_bitbang_read(device_t);
133static void smc_mii_bitbang_write(device_t, uint32_t);
134
135static const struct mii_bitbang_ops smc_mii_bitbang_ops = {
136        smc_mii_bitbang_read,
137        smc_mii_bitbang_write,
138        {
139                MGMT_MDO,       /* MII_BIT_MDO */
140                MGMT_MDI,       /* MII_BIT_MDI */
141                MGMT_MCLK,      /* MII_BIT_MDC */
142                MGMT_MDOE,      /* MII_BIT_DIR_HOST_PHY */
143                0,              /* MII_BIT_DIR_PHY_HOST */
144        }
145};
146
147static __inline void
148smc_select_bank(struct smc_softc *sc, uint16_t bank)
149{
150
151        bus_barrier(sc->smc_reg, BSR, 2,
152            BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
153        bus_write_2(sc->smc_reg, BSR, bank & BSR_BANK_MASK);
154        bus_barrier(sc->smc_reg, BSR, 2,
155            BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
156}
157
158/* Never call this when not in bank 2. */
159static __inline void
160smc_mmu_wait(struct smc_softc *sc)
161{
162
163        KASSERT((bus_read_2(sc->smc_reg, BSR) &
164            BSR_BANK_MASK) == 2, ("%s: smc_mmu_wait called when not in bank 2",
165            device_get_nameunit(sc->smc_dev)));
166        while (bus_read_2(sc->smc_reg, MMUCR) & MMUCR_BUSY)
167                ;
168}
169
170static __inline uint8_t
171smc_read_1(struct smc_softc *sc, bus_size_t offset)
172{
173
174        return (bus_read_1(sc->smc_reg, offset));
175}
176
177static __inline void
178smc_write_1(struct smc_softc *sc, bus_size_t offset, uint8_t val)
179{
180
181        bus_write_1(sc->smc_reg, offset, val);
182}
183
184static __inline uint16_t
185smc_read_2(struct smc_softc *sc, bus_size_t offset)
186{
187
188        return (bus_read_2(sc->smc_reg, offset));
189}
190
191static __inline void
192smc_write_2(struct smc_softc *sc, bus_size_t offset, uint16_t val)
193{
194
195        bus_write_2(sc->smc_reg, offset, val);
196}
197
198static __inline void
199smc_read_multi_2(struct smc_softc *sc, bus_size_t offset, uint16_t *datap,
200    bus_size_t count)
201{
202
203        bus_read_multi_2(sc->smc_reg, offset, datap, count);
204}
205
206static __inline void
207smc_write_multi_2(struct smc_softc *sc, bus_size_t offset, uint16_t *datap,
208    bus_size_t count)
209{
210
211        bus_write_multi_2(sc->smc_reg, offset, datap, count);
212}
213
214static __inline void
215smc_barrier(struct smc_softc *sc, bus_size_t offset, bus_size_t length,
216    int flags)
217{
218
219        bus_barrier(sc->smc_reg, offset, length, flags);
220}
221
222int
223smc_probe(device_t dev)
224{
225        int                     rid, type, error;
226        uint16_t                val;
227        struct smc_softc        *sc;
228        struct resource         *reg;
229
230        sc = device_get_softc(dev);
231        rid = 0;
232        type = SYS_RES_IOPORT;
233        error = 0;
234
235        if (sc->smc_usemem)
236                type = SYS_RES_MEMORY;
237
238        reg = bus_alloc_resource(dev, type, &rid, 0, ~0, 16, RF_ACTIVE);
239        if (reg == NULL) {
240                if (bootverbose)
241                        device_printf(dev,
242                            "could not allocate I/O resource for probe\n");
243                return (ENXIO);
244        }
245
246        /* Check for the identification value in the BSR. */
247        val = bus_read_2(reg, BSR);
248        if ((val & BSR_IDENTIFY_MASK) != BSR_IDENTIFY) {
249                if (bootverbose)
250                        device_printf(dev, "identification value not in BSR\n");
251                error = ENXIO;
252                goto done;
253        }
254
255        /*
256         * Try switching banks and make sure we still get the identification
257         * value.
258         */
259        bus_write_2(reg, BSR, 0);
260        val = bus_read_2(reg, BSR);
261        if ((val & BSR_IDENTIFY_MASK) != BSR_IDENTIFY) {
262                if (bootverbose)
263                        device_printf(dev,
264                            "identification value not in BSR after write\n");
265                error = ENXIO;
266                goto done;
267        }
268
269#if 0
270        /* Check the BAR. */
271        bus_write_2(reg, BSR, 1);
272        val = bus_read_2(reg, BAR);
273        val = BAR_ADDRESS(val);
274        if (rman_get_start(reg) != val) {
275                if (bootverbose)
276                        device_printf(dev, "BAR address %x does not match "
277                            "I/O resource address %lx\n", val,
278                            rman_get_start(reg));
279                error = ENXIO;
280                goto done;
281        }
282#endif
283
284        /* Compare REV against known chip revisions. */
285        bus_write_2(reg, BSR, 3);
286        val = bus_read_2(reg, REV);
287        val = (val & REV_CHIP_MASK) >> REV_CHIP_SHIFT;
288        if (smc_chip_ids[val] == NULL) {
289                if (bootverbose)
290                        device_printf(dev, "Unknown chip revision: %d\n", val);
291                error = ENXIO;
292                goto done;
293        }
294
295        device_set_desc(dev, smc_chip_ids[val]);
296
297done:
298        bus_release_resource(dev, type, rid, reg);
299        return (error);
300}
301
302int
303smc_attach(device_t dev)
304{
305        int                     type, error;
306        uint16_t                val;
307        u_char                  eaddr[ETHER_ADDR_LEN];
308        struct smc_softc        *sc;
309        struct ifnet            *ifp;
310
311        sc = device_get_softc(dev);
312        error = 0;
313
314        sc->smc_dev = dev;
315
316        ifp = sc->smc_ifp = if_alloc(IFT_ETHER);
317        if (ifp == NULL) {
318                error = ENOSPC;
319                goto done;
320        }
321
322        mtx_init(&sc->smc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
323
324        /* Set up watchdog callout. */
325        callout_init_mtx(&sc->smc_watchdog, &sc->smc_mtx, 0);
326
327        type = SYS_RES_IOPORT;
328        if (sc->smc_usemem)
329                type = SYS_RES_MEMORY;
330
331        sc->smc_reg_rid = 0;
332        sc->smc_reg = bus_alloc_resource(dev, type, &sc->smc_reg_rid, 0, ~0,
333            16, RF_ACTIVE);
334        if (sc->smc_reg == NULL) {
335                error = ENXIO;
336                goto done;
337        }
338
339        sc->smc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->smc_irq_rid, 0,
340            ~0, 1, RF_ACTIVE | RF_SHAREABLE);
341        if (sc->smc_irq == NULL) {
342                error = ENXIO;
343                goto done;
344        }
345
346        SMC_LOCK(sc);
347        smc_reset(sc);
348        SMC_UNLOCK(sc);
349
350        smc_select_bank(sc, 3);
351        val = smc_read_2(sc, REV);
352        sc->smc_chip = (val & REV_CHIP_MASK) >> REV_CHIP_SHIFT;
353        sc->smc_rev = (val * REV_REV_MASK) >> REV_REV_SHIFT;
354        if (bootverbose)
355                device_printf(dev, "revision %x\n", sc->smc_rev);
356
357        callout_init_mtx(&sc->smc_mii_tick_ch, &sc->smc_mtx,
358            CALLOUT_RETURNUNLOCKED);
359        if (sc->smc_chip >= REV_CHIP_91110FD) {
360                (void)mii_attach(dev, &sc->smc_miibus, ifp,
361                    smc_mii_ifmedia_upd, smc_mii_ifmedia_sts, BMSR_DEFCAPMASK,
362                    MII_PHY_ANY, MII_OFFSET_ANY, 0);
363                if (sc->smc_miibus != NULL) {
364                        sc->smc_mii_tick = smc_mii_tick;
365                        sc->smc_mii_mediachg = smc_mii_mediachg;
366                        sc->smc_mii_mediaioctl = smc_mii_mediaioctl;
367                }
368        }
369
370        smc_select_bank(sc, 1);
371        eaddr[0] = smc_read_1(sc, IAR0);
372        eaddr[1] = smc_read_1(sc, IAR1);
373        eaddr[2] = smc_read_1(sc, IAR2);
374        eaddr[3] = smc_read_1(sc, IAR3);
375        eaddr[4] = smc_read_1(sc, IAR4);
376        eaddr[5] = smc_read_1(sc, IAR5);
377
378        if_initname(ifp, device_get_name(dev), device_get_unit(dev));
379        ifp->if_softc = sc;
380        ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
381        ifp->if_init = smc_init;
382        ifp->if_ioctl = smc_ioctl;
383        ifp->if_start = smc_start;
384        IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
385        IFQ_SET_READY(&ifp->if_snd);
386
387        ifp->if_capabilities = ifp->if_capenable = 0;
388
389#ifdef DEVICE_POLLING
390        ifp->if_capabilities |= IFCAP_POLLING;
391#endif
392
393        ether_ifattach(ifp, eaddr);
394
395        /* Set up taskqueue */
396        TASK_INIT(&sc->smc_intr, SMC_INTR_PRIORITY, smc_task_intr, ifp);
397        TASK_INIT(&sc->smc_rx, SMC_RX_PRIORITY, smc_task_rx, ifp);
398        TASK_INIT(&sc->smc_tx, SMC_TX_PRIORITY, smc_task_tx, ifp);
399        sc->smc_tq = taskqueue_create_fast("smc_taskq", M_NOWAIT,
400            taskqueue_thread_enqueue, &sc->smc_tq);
401        taskqueue_start_threads(&sc->smc_tq, 1, PI_NET, "%s taskq",
402            device_get_nameunit(sc->smc_dev));
403
404        /* Mask all interrupts. */
405        sc->smc_mask = 0;
406        smc_write_1(sc, MSK, 0);
407
408        /* Wire up interrupt */
409        error = bus_setup_intr(dev, sc->smc_irq,
410            INTR_TYPE_NET|INTR_MPSAFE, smc_intr, NULL, sc, &sc->smc_ih);
411        if (error != 0)
412                goto done;
413
414done:
415        if (error != 0)
416                smc_detach(dev);
417        return (error);
418}
419
420int
421smc_detach(device_t dev)
422{
423        int                     type;
424        struct smc_softc        *sc;
425
426        sc = device_get_softc(dev);
427        SMC_LOCK(sc);
428        smc_stop(sc);
429        SMC_UNLOCK(sc);
430
431        if (sc->smc_ifp != NULL) {
432                ether_ifdetach(sc->smc_ifp);
433        }
434       
435        callout_drain(&sc->smc_watchdog);
436        callout_drain(&sc->smc_mii_tick_ch);
437       
438#ifdef DEVICE_POLLING
439        if (sc->smc_ifp->if_capenable & IFCAP_POLLING)
440                ether_poll_deregister(sc->smc_ifp);
441#endif
442
443        if (sc->smc_ih != NULL)
444                bus_teardown_intr(sc->smc_dev, sc->smc_irq, sc->smc_ih);
445
446        if (sc->smc_tq != NULL) {
447                taskqueue_drain(sc->smc_tq, &sc->smc_intr);
448                taskqueue_drain(sc->smc_tq, &sc->smc_rx);
449                taskqueue_drain(sc->smc_tq, &sc->smc_tx);
450                taskqueue_free(sc->smc_tq);
451                sc->smc_tq = NULL;
452        }
453
454        if (sc->smc_ifp != NULL) {
455                if_free(sc->smc_ifp);
456        }
457
458        if (sc->smc_miibus != NULL) {
459                device_delete_child(sc->smc_dev, sc->smc_miibus);
460                bus_generic_detach(sc->smc_dev);
461        }
462
463        if (sc->smc_reg != NULL) {
464                type = SYS_RES_IOPORT;
465                if (sc->smc_usemem)
466                        type = SYS_RES_MEMORY;
467
468                bus_release_resource(sc->smc_dev, type, sc->smc_reg_rid,
469                    sc->smc_reg);
470        }
471
472        if (sc->smc_irq != NULL)
473                bus_release_resource(sc->smc_dev, SYS_RES_IRQ, sc->smc_irq_rid,
474                   sc->smc_irq);
475
476        if (mtx_initialized(&sc->smc_mtx))
477                mtx_destroy(&sc->smc_mtx);
478
479        return (0);
480}
481
482static void
483smc_start(struct ifnet *ifp)
484{
485        struct smc_softc        *sc;
486
487        sc = ifp->if_softc;
488        SMC_LOCK(sc);
489        smc_start_locked(ifp);
490        SMC_UNLOCK(sc);
491}
492
493static void
494smc_start_locked(struct ifnet *ifp)
495{
496        struct smc_softc        *sc;
497        struct mbuf             *m;
498        u_int                   len, npages, spin_count;
499
500        sc = ifp->if_softc;
501        SMC_ASSERT_LOCKED(sc);
502
503        if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
504                return;
505        if (IFQ_IS_EMPTY(&ifp->if_snd))
506                return;
507
508        /*
509         * Grab the next packet.  If it's too big, drop it.
510         */
511        IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
512        len = m_length(m, NULL);
513        len += (len & 1);
514        if (len > ETHER_MAX_LEN - ETHER_CRC_LEN) {
515                if_printf(ifp, "large packet discarded\n");
516                ++ifp->if_oerrors;
517                m_freem(m);
518                return; /* XXX readcheck? */
519        }
520
521        /*
522         * Flag that we're busy.
523         */
524        ifp->if_drv_flags |= IFF_DRV_OACTIVE;
525        sc->smc_pending = m;
526
527        /*
528         * Work out how many 256 byte "pages" we need.  We have to include the
529         * control data for the packet in this calculation.
530         */
531        npages = (len * PKT_CTRL_DATA_LEN) >> 8;
532        if (npages == 0)
533                npages = 1;
534
535        /*
536         * Request memory.
537         */
538        smc_select_bank(sc, 2);
539        smc_mmu_wait(sc);
540        smc_write_2(sc, MMUCR, MMUCR_CMD_TX_ALLOC | npages);
541
542        /*
543         * Spin briefly to see if the allocation succeeds.
544         */
545        spin_count = TX_ALLOC_WAIT_TIME;
546        do {
547                if (smc_read_1(sc, IST) & ALLOC_INT) {
548                        smc_write_1(sc, ACK, ALLOC_INT);
549                        break;
550                }
551        } while (--spin_count);
552
553        /*
554         * If the allocation is taking too long, unmask the alloc interrupt
555         * and wait.
556         */
557        if (spin_count == 0) {
558                sc->smc_mask |= ALLOC_INT;
559                if ((ifp->if_capenable & IFCAP_POLLING) == 0)
560                        smc_write_1(sc, MSK, sc->smc_mask);
561                return;
562        }
563
564        taskqueue_enqueue_fast(sc->smc_tq, &sc->smc_tx);
565}
566
567static void
568smc_task_tx(void *context, int pending)
569{
570        struct ifnet            *ifp;
571        struct smc_softc        *sc;
572        struct mbuf             *m, *m0;
573        u_int                   packet, len;
574        int                     last_len;
575        uint8_t                 *data;
576
577        (void)pending;
578        ifp = (struct ifnet *)context;
579        sc = ifp->if_softc;
580
581        SMC_LOCK(sc);
582       
583        if (sc->smc_pending == NULL) {
584                SMC_UNLOCK(sc);
585                goto next_packet;
586        }
587
588        m = m0 = sc->smc_pending;
589        sc->smc_pending = NULL;
590        smc_select_bank(sc, 2);
591
592        /*
593         * Check the allocation result.
594         */
595        packet = smc_read_1(sc, ARR);
596
597        /*
598         * If the allocation failed, requeue the packet and retry.
599         */
600        if (packet & ARR_FAILED) {
601                IFQ_DRV_PREPEND(&ifp->if_snd, m);
602                ++ifp->if_oerrors;
603                ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
604                smc_start_locked(ifp);
605                SMC_UNLOCK(sc);
606                return;
607        }
608
609        /*
610         * Tell the device to write to our packet number.
611         */
612        smc_write_1(sc, PNR, packet);
613        smc_write_2(sc, PTR, 0 | PTR_AUTO_INCR);
614
615        /*
616         * Tell the device how long the packet is (including control data).
617         */
618        len = m_length(m, 0);
619        len += PKT_CTRL_DATA_LEN;
620        smc_write_2(sc, DATA0, 0);
621        smc_write_2(sc, DATA0, len);
622
623        /*
624         * Push the data out to the device.
625         */
626        data = NULL;
627        last_len = 0;
628        for (; m != NULL; m = m->m_next) {
629                data = mtod(m, uint8_t *);
630                smc_write_multi_2(sc, DATA0, (uint16_t *)data, m->m_len / 2);
631                last_len = m->m_len;
632        }
633
634        /*
635         * Push out the control byte and and the odd byte if needed.
636         */
637        if ((len & 1) != 0 && data != NULL)
638                smc_write_2(sc, DATA0, (CTRL_ODD << 8) | data[last_len - 1]);
639        else
640                smc_write_2(sc, DATA0, 0);
641
642        /*
643         * Unmask the TX empty interrupt.
644         */
645        sc->smc_mask |= TX_EMPTY_INT;
646        if ((ifp->if_capenable & IFCAP_POLLING) == 0)
647                smc_write_1(sc, MSK, sc->smc_mask);
648
649        /*
650         * Enqueue the packet.
651         */
652        smc_mmu_wait(sc);
653        smc_write_2(sc, MMUCR, MMUCR_CMD_ENQUEUE);
654        callout_reset(&sc->smc_watchdog, hz * 2, smc_watchdog, sc);
655
656        /*
657         * Finish up.
658         */
659        ifp->if_opackets++;
660        ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
661        SMC_UNLOCK(sc);
662        BPF_MTAP(ifp, m0);
663        m_freem(m0);
664
665next_packet:
666        /*
667         * See if there's anything else to do.
668         */
669        smc_start(ifp);
670}
671
672static void
673smc_task_rx(void *context, int pending)
674{
675        u_int                   packet, status, len;
676        uint8_t                 *data;
677        struct ifnet            *ifp;
678        struct smc_softc        *sc;
679        struct mbuf             *m, *mhead, *mtail;
680
681        (void)pending;
682        ifp = (struct ifnet *)context;
683        sc = ifp->if_softc;
684        mhead = mtail = NULL;
685
686        SMC_LOCK(sc);
687
688        packet = smc_read_1(sc, FIFO_RX);
689        while ((packet & FIFO_EMPTY) == 0) {
690                /*
691                 * Grab an mbuf and attach a cluster.
692                 */
693                MGETHDR(m, M_NOWAIT, MT_DATA);
694                if (m == NULL) {
695                        break;
696                }
697                MCLGET(m, M_NOWAIT);
698                if ((m->m_flags & M_EXT) == 0) {
699                        m_freem(m);
700                        break;
701                }
702       
703                /*
704                 * Point to the start of the packet.
705                 */
706                smc_select_bank(sc, 2);
707                smc_write_1(sc, PNR, packet);
708                smc_write_2(sc, PTR, 0 | PTR_READ | PTR_RCV | PTR_AUTO_INCR);
709
710                /*
711                 * Grab status and packet length.
712                 */
713                status = smc_read_2(sc, DATA0);
714                len = smc_read_2(sc, DATA0) & RX_LEN_MASK;
715                len -= 6;
716                if (status & RX_ODDFRM)
717                        len += 1;
718
719                /*
720                 * Check for errors.
721                 */
722                if (status & (RX_TOOSHORT | RX_TOOLNG | RX_BADCRC | RX_ALGNERR)) {
723                        smc_mmu_wait(sc);
724                        smc_write_2(sc, MMUCR, MMUCR_CMD_RELEASE);
725                        ifp->if_ierrors++;
726                        m_freem(m);
727                        break;
728                }
729       
730                /*
731                 * Set the mbuf up the way we want it.
732                 */
733                m->m_pkthdr.rcvif = ifp;
734                m->m_pkthdr.len = m->m_len = len + 2; /* XXX: Is this right? */
735                m_adj(m, ETHER_ALIGN);
736       
737                /*
738                 * Pull the packet out of the device.  Make sure we're in the
739                 * right bank first as things may have changed while we were
740                 * allocating our mbuf.
741                 */
742                smc_select_bank(sc, 2);
743                smc_write_1(sc, PNR, packet);
744                smc_write_2(sc, PTR, 4 | PTR_READ | PTR_RCV | PTR_AUTO_INCR);
745                data = mtod(m, uint8_t *);
746                smc_read_multi_2(sc, DATA0, (uint16_t *)data, len >> 1);
747                if (len & 1) {
748                        data += len & ~1;
749                        *data = smc_read_1(sc, DATA0);
750                }
751
752                /*
753                 * Tell the device we're done.
754                 */
755                smc_mmu_wait(sc);
756                smc_write_2(sc, MMUCR, MMUCR_CMD_RELEASE);
757                if (m == NULL) {
758                        break;
759                }
760               
761                if (mhead == NULL) {
762                        mhead = mtail = m;
763                        m->m_next = NULL;
764                } else {
765                        mtail->m_next = m;
766                        mtail = m;
767                }
768                packet = smc_read_1(sc, FIFO_RX);
769        }
770
771        sc->smc_mask |= RCV_INT;
772        if ((ifp->if_capenable & IFCAP_POLLING) == 0)
773                smc_write_1(sc, MSK, sc->smc_mask);
774
775        SMC_UNLOCK(sc);
776
777        while (mhead != NULL) {
778                m = mhead;
779                mhead = mhead->m_next;
780                m->m_next = NULL;
781                ifp->if_ipackets++;
782                (*ifp->if_input)(ifp, m);
783        }
784}
785
786#ifdef DEVICE_POLLING
787static void
788smc_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
789{
790        struct smc_softc        *sc;
791
792        sc = ifp->if_softc;
793
794        SMC_LOCK(sc);
795        if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
796                SMC_UNLOCK(sc);
797                return;
798        }
799        SMC_UNLOCK(sc);
800
801        if (cmd == POLL_AND_CHECK_STATUS)
802                taskqueue_enqueue_fast(sc->smc_tq, &sc->smc_intr);
803}
804#endif
805
806static int
807smc_intr(void *context)
808{
809        struct smc_softc        *sc;
810       
811        sc = (struct smc_softc *)context;
812        taskqueue_enqueue_fast(sc->smc_tq, &sc->smc_intr);
813        return (FILTER_HANDLED);
814}
815
816static void
817smc_task_intr(void *context, int pending)
818{
819        struct smc_softc        *sc;
820        struct ifnet            *ifp;
821        u_int                   status, packet, counter, tcr;
822
823        (void)pending;
824        ifp = (struct ifnet *)context;
825        sc = ifp->if_softc;
826
827        SMC_LOCK(sc);
828       
829        smc_select_bank(sc, 2);
830
831        /*
832         * Get the current mask, and then block all interrupts while we're
833         * working.
834         */
835        if ((ifp->if_capenable & IFCAP_POLLING) == 0)
836                smc_write_1(sc, MSK, 0);
837
838        /*
839         * Find out what interrupts are flagged.
840         */
841        status = smc_read_1(sc, IST) & sc->smc_mask;
842
843        /*
844         * Transmit error
845         */
846        if (status & TX_INT) {
847                /*
848                 * Kill off the packet if there is one and re-enable transmit.
849                 */
850                packet = smc_read_1(sc, FIFO_TX);
851                if ((packet & FIFO_EMPTY) == 0) {
852                        smc_write_1(sc, PNR, packet);
853                        smc_write_2(sc, PTR, 0 | PTR_READ |
854                            PTR_AUTO_INCR);
855                        tcr = smc_read_2(sc, DATA0);
856                        if ((tcr & EPHSR_TX_SUC) == 0)
857                                device_printf(sc->smc_dev,
858                                    "bad packet\n");
859                        smc_mmu_wait(sc);
860                        smc_write_2(sc, MMUCR, MMUCR_CMD_RELEASE_PKT);
861
862                        smc_select_bank(sc, 0);
863                        tcr = smc_read_2(sc, TCR);
864                        tcr |= TCR_TXENA | TCR_PAD_EN;
865                        smc_write_2(sc, TCR, tcr);
866                        smc_select_bank(sc, 2);
867                        taskqueue_enqueue_fast(sc->smc_tq, &sc->smc_tx);
868                }
869
870                /*
871                 * Ack the interrupt.
872                 */
873                smc_write_1(sc, ACK, TX_INT);
874        }
875
876        /*
877         * Receive
878         */
879        if (status & RCV_INT) {
880                smc_write_1(sc, ACK, RCV_INT);
881                sc->smc_mask &= ~RCV_INT;
882                taskqueue_enqueue_fast(sc->smc_tq, &sc->smc_rx);
883        }
884
885        /*
886         * Allocation
887         */
888        if (status & ALLOC_INT) {
889                smc_write_1(sc, ACK, ALLOC_INT);
890                sc->smc_mask &= ~ALLOC_INT;
891                taskqueue_enqueue_fast(sc->smc_tq, &sc->smc_tx);
892        }
893
894        /*
895         * Receive overrun
896         */
897        if (status & RX_OVRN_INT) {
898                smc_write_1(sc, ACK, RX_OVRN_INT);
899                ifp->if_ierrors++;
900        }
901
902        /*
903         * Transmit empty
904         */
905        if (status & TX_EMPTY_INT) {
906                smc_write_1(sc, ACK, TX_EMPTY_INT);
907                sc->smc_mask &= ~TX_EMPTY_INT;
908                callout_stop(&sc->smc_watchdog);
909
910                /*
911                 * Update collision stats.
912                 */
913                smc_select_bank(sc, 0);
914                counter = smc_read_2(sc, ECR);
915                smc_select_bank(sc, 2);
916                ifp->if_collisions +=
917                    (counter & ECR_SNGLCOL_MASK) >> ECR_SNGLCOL_SHIFT;
918                ifp->if_collisions +=
919                    (counter & ECR_MULCOL_MASK) >> ECR_MULCOL_SHIFT;
920
921                /*
922                 * See if there are any packets to transmit.
923                 */
924                taskqueue_enqueue_fast(sc->smc_tq, &sc->smc_tx);
925        }
926
927        /*
928         * Update the interrupt mask.
929         */
930        if ((ifp->if_capenable & IFCAP_POLLING) == 0)
931                smc_write_1(sc, MSK, sc->smc_mask);
932
933        SMC_UNLOCK(sc);
934}
935
936static uint32_t
937smc_mii_bitbang_read(device_t dev)
938{
939        struct smc_softc        *sc;
940        uint32_t                val;
941
942        sc = device_get_softc(dev);
943
944        SMC_ASSERT_LOCKED(sc);
945        KASSERT((smc_read_2(sc, BSR) & BSR_BANK_MASK) == 3,
946            ("%s: smc_mii_bitbang_read called with bank %d (!= 3)",
947            device_get_nameunit(sc->smc_dev),
948            smc_read_2(sc, BSR) & BSR_BANK_MASK));
949
950        val = smc_read_2(sc, MGMT);
951        smc_barrier(sc, MGMT, 2,
952            BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
953
954        return (val);
955}
956
957static void
958smc_mii_bitbang_write(device_t dev, uint32_t val)
959{
960        struct smc_softc        *sc;
961
962        sc = device_get_softc(dev);
963
964        SMC_ASSERT_LOCKED(sc);
965        KASSERT((smc_read_2(sc, BSR) & BSR_BANK_MASK) == 3,
966            ("%s: smc_mii_bitbang_write called with bank %d (!= 3)",
967            device_get_nameunit(sc->smc_dev),
968            smc_read_2(sc, BSR) & BSR_BANK_MASK));
969
970        smc_write_2(sc, MGMT, val);
971        smc_barrier(sc, MGMT, 2,
972            BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
973}
974
975int
976smc_miibus_readreg(device_t dev, int phy, int reg)
977{
978        struct smc_softc        *sc;
979        int                     val;
980
981        sc = device_get_softc(dev);
982
983        SMC_LOCK(sc);
984
985        smc_select_bank(sc, 3);
986
987        val = mii_bitbang_readreg(dev, &smc_mii_bitbang_ops, phy, reg);
988
989        SMC_UNLOCK(sc);
990        return (val);
991}
992
993int
994smc_miibus_writereg(device_t dev, int phy, int reg, int data)
995{
996        struct smc_softc        *sc;
997
998        sc = device_get_softc(dev);
999
1000        SMC_LOCK(sc);
1001
1002        smc_select_bank(sc, 3);
1003
1004        mii_bitbang_writereg(dev, &smc_mii_bitbang_ops, phy, reg, data);
1005
1006        SMC_UNLOCK(sc);
1007        return (0);
1008}
1009
1010void
1011smc_miibus_statchg(device_t dev)
1012{
1013        struct smc_softc        *sc;
1014        struct mii_data         *mii;
1015        uint16_t                tcr;
1016
1017        sc = device_get_softc(dev);
1018        mii = device_get_softc(sc->smc_miibus);
1019
1020        SMC_LOCK(sc);
1021
1022        smc_select_bank(sc, 0);
1023        tcr = smc_read_2(sc, TCR);
1024
1025        if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
1026                tcr |= TCR_SWFDUP;
1027        else
1028                tcr &= ~TCR_SWFDUP;
1029
1030        smc_write_2(sc, TCR, tcr);
1031
1032        SMC_UNLOCK(sc);
1033}
1034
1035static int
1036smc_mii_ifmedia_upd(struct ifnet *ifp)
1037{
1038        struct smc_softc        *sc;
1039        struct mii_data         *mii;
1040
1041        sc = ifp->if_softc;
1042        if (sc->smc_miibus == NULL)
1043                return (ENXIO);
1044
1045        mii = device_get_softc(sc->smc_miibus);
1046        return (mii_mediachg(mii));
1047}
1048
1049static void
1050smc_mii_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1051{
1052        struct smc_softc        *sc;
1053        struct mii_data         *mii;
1054
1055        sc = ifp->if_softc;
1056        if (sc->smc_miibus == NULL)
1057                return;
1058
1059        mii = device_get_softc(sc->smc_miibus);
1060        mii_pollstat(mii);
1061        ifmr->ifm_active = mii->mii_media_active;
1062        ifmr->ifm_status = mii->mii_media_status;
1063}
1064
1065static void
1066smc_mii_tick(void *context)
1067{
1068        struct smc_softc        *sc;
1069
1070        sc = (struct smc_softc *)context;
1071
1072        if (sc->smc_miibus == NULL)
1073                return;
1074
1075        SMC_UNLOCK(sc);
1076
1077        mii_tick(device_get_softc(sc->smc_miibus));
1078        callout_reset(&sc->smc_mii_tick_ch, hz, smc_mii_tick, sc);
1079}
1080
1081static void
1082smc_mii_mediachg(struct smc_softc *sc)
1083{
1084
1085        if (sc->smc_miibus == NULL)
1086                return;
1087        mii_mediachg(device_get_softc(sc->smc_miibus));
1088}
1089
1090static int
1091smc_mii_mediaioctl(struct smc_softc *sc, struct ifreq *ifr, u_long command)
1092{
1093        struct mii_data *mii;
1094
1095        if (sc->smc_miibus == NULL)
1096                return (EINVAL);
1097
1098        mii = device_get_softc(sc->smc_miibus);
1099        return (ifmedia_ioctl(sc->smc_ifp, ifr, &mii->mii_media, command));
1100}
1101
1102static void
1103smc_reset(struct smc_softc *sc)
1104{
1105        u_int   ctr;
1106
1107        SMC_ASSERT_LOCKED(sc);
1108
1109        smc_select_bank(sc, 2);
1110
1111        /*
1112         * Mask all interrupts.
1113         */
1114        smc_write_1(sc, MSK, 0);
1115
1116        /*
1117         * Tell the device to reset.
1118         */
1119        smc_select_bank(sc, 0);
1120        smc_write_2(sc, RCR, RCR_SOFT_RST);
1121
1122        /*
1123         * Set up the configuration register.
1124         */
1125        smc_select_bank(sc, 1);
1126        smc_write_2(sc, CR, CR_EPH_POWER_EN);
1127        DELAY(1);
1128
1129        /*
1130         * Turn off transmit and receive.
1131         */
1132        smc_select_bank(sc, 0);
1133        smc_write_2(sc, TCR, 0);
1134        smc_write_2(sc, RCR, 0);
1135
1136        /*
1137         * Set up the control register.
1138         */
1139        smc_select_bank(sc, 1);
1140        ctr = smc_read_2(sc, CTR);
1141        ctr |= CTR_LE_ENABLE | CTR_AUTO_RELEASE;
1142        smc_write_2(sc, CTR, ctr);
1143
1144        /*
1145         * Reset the MMU.
1146         */
1147        smc_select_bank(sc, 2);
1148        smc_mmu_wait(sc);
1149        smc_write_2(sc, MMUCR, MMUCR_CMD_MMU_RESET);
1150}
1151
1152static void
1153smc_enable(struct smc_softc *sc)
1154{
1155        struct ifnet            *ifp;
1156
1157        SMC_ASSERT_LOCKED(sc);
1158        ifp = sc->smc_ifp;
1159
1160        /*
1161         * Set up the receive/PHY control register.
1162         */
1163        smc_select_bank(sc, 0);
1164        smc_write_2(sc, RPCR, RPCR_ANEG | (RPCR_LED_LINK_ANY << RPCR_LSA_SHIFT)
1165            | (RPCR_LED_ACT_ANY << RPCR_LSB_SHIFT));
1166
1167        /*
1168         * Set up the transmit and receive control registers.
1169         */
1170        smc_write_2(sc, TCR, TCR_TXENA | TCR_PAD_EN);
1171        smc_write_2(sc, RCR, RCR_RXEN | RCR_STRIP_CRC);
1172
1173        /*
1174         * Set up the interrupt mask.
1175         */
1176        smc_select_bank(sc, 2);
1177        sc->smc_mask = EPH_INT | RX_OVRN_INT | RCV_INT | TX_INT;
1178        if ((ifp->if_capenable & IFCAP_POLLING) != 0)
1179                smc_write_1(sc, MSK, sc->smc_mask);
1180}
1181
1182static void
1183smc_stop(struct smc_softc *sc)
1184{
1185
1186        SMC_ASSERT_LOCKED(sc);
1187
1188        /*
1189         * Turn off callouts.
1190         */
1191        callout_stop(&sc->smc_watchdog);
1192        callout_stop(&sc->smc_mii_tick_ch);
1193
1194        /*
1195         * Mask all interrupts.
1196         */
1197        smc_select_bank(sc, 2);
1198        sc->smc_mask = 0;
1199        smc_write_1(sc, MSK, 0);
1200#ifdef DEVICE_POLLING
1201        ether_poll_deregister(sc->smc_ifp);
1202        sc->smc_ifp->if_capenable &= ~IFCAP_POLLING;
1203        sc->smc_ifp->if_capenable &= ~IFCAP_POLLING_NOCOUNT;
1204#endif
1205
1206        /*
1207         * Disable transmit and receive.
1208         */
1209        smc_select_bank(sc, 0);
1210        smc_write_2(sc, TCR, 0);
1211        smc_write_2(sc, RCR, 0);
1212
1213        sc->smc_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1214}
1215
1216static void
1217smc_watchdog(void *arg)
1218{
1219        struct smc_softc        *sc;
1220       
1221        sc = (struct smc_softc *)arg;
1222        device_printf(sc->smc_dev, "watchdog timeout\n");
1223        taskqueue_enqueue_fast(sc->smc_tq, &sc->smc_intr);
1224}
1225
1226static void
1227smc_init(void *context)
1228{
1229        struct smc_softc        *sc;
1230
1231        sc = (struct smc_softc *)context;
1232        SMC_LOCK(sc);
1233        smc_init_locked(sc);
1234        SMC_UNLOCK(sc);
1235}
1236
1237static void
1238smc_init_locked(struct smc_softc *sc)
1239{
1240        struct ifnet    *ifp;
1241
1242        ifp = sc->smc_ifp;
1243
1244        SMC_ASSERT_LOCKED(sc);
1245
1246        smc_reset(sc);
1247        smc_enable(sc);
1248
1249        ifp->if_drv_flags |= IFF_DRV_RUNNING;
1250        ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1251
1252        smc_start_locked(ifp);
1253
1254        if (sc->smc_mii_tick != NULL)
1255                callout_reset(&sc->smc_mii_tick_ch, hz, sc->smc_mii_tick, sc);
1256
1257#ifdef DEVICE_POLLING
1258        SMC_UNLOCK(sc);
1259        ether_poll_register(smc_poll, ifp);
1260        SMC_LOCK(sc);
1261        ifp->if_capenable |= IFCAP_POLLING;
1262        ifp->if_capenable |= IFCAP_POLLING_NOCOUNT;
1263#endif
1264}
1265
1266static int
1267smc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1268{
1269        struct smc_softc        *sc;
1270        int                     error;
1271
1272        sc = ifp->if_softc;
1273        error = 0;
1274
1275        switch (cmd) {
1276        case SIOCSIFFLAGS:
1277                if ((ifp->if_flags & IFF_UP) == 0 &&
1278                    (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
1279                        SMC_LOCK(sc);
1280                        smc_stop(sc);
1281                        SMC_UNLOCK(sc);
1282                } else {
1283                        smc_init(sc);
1284                        if (sc->smc_mii_mediachg != NULL)
1285                                sc->smc_mii_mediachg(sc);
1286                }
1287                break;
1288
1289        case SIOCADDMULTI:
1290        case SIOCDELMULTI:
1291                /* XXX
1292                SMC_LOCK(sc);
1293                smc_setmcast(sc);
1294                SMC_UNLOCK(sc);
1295                */
1296                error = EINVAL;
1297                break;
1298
1299        case SIOCGIFMEDIA:
1300        case SIOCSIFMEDIA:
1301                if (sc->smc_mii_mediaioctl == NULL) {
1302                        error = EINVAL;
1303                        break;
1304                }
1305                sc->smc_mii_mediaioctl(sc, (struct ifreq *)data, cmd);
1306                break;
1307
1308        default:
1309                error = ether_ioctl(ifp, cmd, data);
1310                break;
1311        }
1312
1313        return (error);
1314}
Note: See TracBrowser for help on using the repository browser.