Home | History | Annotate | Line # | Download | only in cardbus
if_fxp_cardbus.c revision 1.6
      1  1.6     joda /*	$NetBSD: if_fxp_cardbus.c,v 1.6 2000/02/09 22:15:59 joda Exp $	*/
      2  1.1     haya 
      3  1.1     haya /*
      4  1.3     joda  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  1.3     joda  * All rights reserved.
      6  1.1     haya  *
      7  1.3     joda  * This code is derived from software contributed to
      8  1.3     joda  * The NetBSD Foundation by Johan Danielsson.
      9  1.1     haya  *
     10  1.3     joda  * Redistribution and use in source and binary forms, with or without
     11  1.3     joda  * modification, are permitted provided that the following conditions
     12  1.3     joda  * are met:
     13  1.1     haya  *
     14  1.3     joda  * 1. Redistributions of source code must retain the above copyright
     15  1.3     joda  *    notice, this list of conditions and the following disclaimer.
     16  1.3     joda  *
     17  1.3     joda  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.3     joda  *    notice, this list of conditions and the following disclaimer in the
     19  1.3     joda  *    documentation and/or other materials provided with the distribution.
     20  1.3     joda  *
     21  1.3     joda  * 3. Neither the name of The NetBSD Foundation nor the names of its
     22  1.3     joda  *    contributors may be used to endorse or promote products derived
     23  1.3     joda  *    from this software without specific prior written permission.
     24  1.3     joda  *
     25  1.3     joda  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  1.3     joda  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  1.3     joda  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  1.3     joda  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  1.3     joda  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  1.3     joda  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  1.3     joda  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  1.3     joda  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  1.3     joda  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  1.3     joda  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  1.1     haya  * POSSIBILITY OF SUCH DAMAGE.
     36  1.1     haya  */
     37  1.1     haya 
     38  1.1     haya 
     39  1.1     haya #include "opt_inet.h"
     40  1.1     haya #include "opt_ns.h"
     41  1.1     haya #include "bpfilter.h"
     42  1.1     haya #include "rnd.h"
     43  1.1     haya 
     44  1.1     haya #include <sys/param.h>
     45  1.1     haya #include <sys/systm.h>
     46  1.1     haya #include <sys/mbuf.h>
     47  1.1     haya #include <sys/malloc.h>
     48  1.1     haya #include <sys/kernel.h>
     49  1.1     haya #include <sys/socket.h>
     50  1.1     haya #include <sys/ioctl.h>
     51  1.1     haya #include <sys/errno.h>
     52  1.1     haya #include <sys/device.h>
     53  1.1     haya 
     54  1.1     haya #if NRND > 0
     55  1.1     haya #include <sys/rnd.h>
     56  1.1     haya #endif
     57  1.1     haya 
     58  1.1     haya #include <net/if.h>
     59  1.1     haya #include <net/if_dl.h>
     60  1.1     haya #include <net/if_media.h>
     61  1.1     haya #include <net/if_ether.h>
     62  1.5  thorpej 
     63  1.5  thorpej #include <machine/endian.h>
     64  1.1     haya 
     65  1.1     haya #if NBPFILTER > 0
     66  1.1     haya #include <net/bpf.h>
     67  1.1     haya #endif
     68  1.1     haya 
     69  1.1     haya #ifdef INET
     70  1.1     haya #include <netinet/in.h>
     71  1.1     haya #include <netinet/if_inarp.h>
     72  1.1     haya #endif
     73  1.1     haya 
     74  1.1     haya #ifdef NS
     75  1.1     haya #include <netns/ns.h>
     76  1.1     haya #include <netns/ns_if.h>
     77  1.1     haya #endif
     78  1.1     haya 
     79  1.1     haya #include <machine/bus.h>
     80  1.1     haya #include <machine/intr.h>
     81  1.1     haya 
     82  1.1     haya #include <dev/mii/miivar.h>
     83  1.1     haya 
     84  1.3     joda #include <dev/ic/i82557reg.h>
     85  1.3     joda #include <dev/ic/i82557var.h>
     86  1.1     haya 
     87  1.1     haya #include <dev/pci/pcivar.h>
     88  1.1     haya #include <dev/pci/pcireg.h>
     89  1.1     haya #include <dev/pci/pcidevs.h>
     90  1.1     haya 
     91  1.1     haya #include <dev/cardbus/cardbusvar.h>
     92  1.1     haya #include <dev/cardbus/cardbusdevs.h>
     93  1.1     haya 
     94  1.1     haya static int fxp_cardbus_match __P((struct device *, struct cfdata *, void *));
     95  1.1     haya static void fxp_cardbus_attach __P((struct device *, struct device *, void *));
     96  1.6     joda static int fxp_cardbus_detach __P((struct device *self, int flags));
     97  1.3     joda static void fxp_cardbus_setup __P((struct fxp_softc *sc));
     98  1.3     joda static int fxp_cardbus_enable __P((struct fxp_softc *sc));
     99  1.3     joda static void fxp_cardbus_disable __P((struct fxp_softc *sc));
    100  1.1     haya 
    101  1.1     haya struct fxp_cardbus_softc {
    102  1.1     haya     struct fxp_softc sc;
    103  1.3     joda     cardbus_devfunc_t ct;
    104  1.3     joda     pcireg_t base0_reg;
    105  1.1     haya     pcireg_t base1_reg;
    106  1.3     joda     bus_size_t size;
    107  1.1     haya };
    108  1.1     haya 
    109  1.1     haya struct cfattach fxp_cardbus_ca = {
    110  1.6     joda     sizeof(struct fxp_cardbus_softc), fxp_cardbus_match, fxp_cardbus_attach,
    111  1.6     joda     fxp_cardbus_detach
    112  1.1     haya };
    113  1.1     haya 
    114  1.1     haya #ifdef CBB_DEBUG
    115  1.1     haya #define DPRINTF(X) printf X
    116  1.1     haya #else
    117  1.1     haya #define DPRINTF(X)
    118  1.1     haya #endif
    119  1.1     haya 
    120  1.1     haya static int
    121  1.1     haya fxp_cardbus_match(parent, match, aux)
    122  1.1     haya      struct device *parent;
    123  1.1     haya      struct cfdata *match;
    124  1.1     haya      void *aux;
    125  1.1     haya {
    126  1.1     haya     struct cardbus_attach_args *ca = aux;
    127  1.1     haya 
    128  1.6     joda     if(CARDBUS_VENDOR(ca->ca_id) != CARDBUS_VENDOR_INTEL &&
    129  1.6     joda        CARDBUS_PRODUCT(ca->ca_id) != CARDBUS_PRODUCT_INTEL_82557)
    130  1.6     joda 	return 0;
    131  1.6     joda     return 1;
    132  1.1     haya }
    133  1.1     haya 
    134  1.1     haya static void
    135  1.1     haya fxp_cardbus_attach(parent, self, aux)
    136  1.1     haya      struct device *parent, *self;
    137  1.1     haya      void *aux;
    138  1.1     haya {
    139  1.6     joda     static const char __func__[] = "fxp_cardbus_attach";
    140  1.6     joda 
    141  1.1     haya     struct fxp_softc *sc = (struct fxp_softc*)self;
    142  1.1     haya     struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc*)self;
    143  1.1     haya     struct cardbus_attach_args *ca = aux;
    144  1.3     joda     bus_space_tag_t iot, memt;
    145  1.1     haya     bus_space_handle_t ioh, memh;
    146  1.1     haya 
    147  1.1     haya     bus_addr_t adr;
    148  1.3     joda     bus_size_t size;
    149  1.3     joda 
    150  1.3     joda     csc->ct = ca->ca_ct;
    151  1.1     haya 
    152  1.1     haya     /*
    153  1.1     haya      * Map control/status registers.
    154  1.1     haya      */
    155  1.3     joda     if(Cardbus_mapreg_map(csc->ct, CARDBUS_BASE0_REG,
    156  1.3     joda 			  PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
    157  1.3     joda 			  0, &memt, &memh, &adr, &size) == 0) {
    158  1.3     joda 	csc->base0_reg = adr;
    159  1.3     joda 	sc->sc_st = memt;
    160  1.1     haya 	sc->sc_sh = memh;
    161  1.3     joda 	csc->size = size;
    162  1.3     joda     } else if(Cardbus_mapreg_map(csc->ct, CARDBUS_BASE1_REG,
    163  1.3     joda 				 PCI_MAPREG_TYPE_IO,
    164  1.3     joda 				 0, &iot, &ioh, &adr, &size) == 0) {
    165  1.3     joda 	csc->base1_reg = adr | 1;
    166  1.3     joda 	sc->sc_st = iot;
    167  1.3     joda 	sc->sc_sh = ioh;
    168  1.3     joda 	csc->size = size;
    169  1.3     joda     } else
    170  1.6     joda 	panic("%s: failed to allocate mem and io space", __func__);
    171  1.1     haya 
    172  1.1     haya 
    173  1.6     joda     if(ca->ca_cis.cis1_info[0] && ca->ca_cis.cis1_info[1])
    174  1.6     joda 	printf(": %s %s\n", ca->ca_cis.cis1_info[0], ca->ca_cis.cis1_info[1]);
    175  1.6     joda     else
    176  1.6     joda 	printf("\n");
    177  1.3     joda     sc->sc_dmat = ca->ca_dmat;
    178  1.3     joda     sc->sc_enable = fxp_cardbus_enable;
    179  1.3     joda     sc->sc_disable = fxp_cardbus_disable;
    180  1.3     joda     sc->sc_enabled = 0;
    181  1.3     joda 
    182  1.3     joda     fxp_enable(sc);
    183  1.3     joda     fxp_attach(sc);
    184  1.3     joda     fxp_disable(sc);
    185  1.1     haya }
    186  1.1     haya 
    187  1.1     haya 
    188  1.1     haya static void
    189  1.1     haya fxp_cardbus_setup(struct fxp_softc *sc)
    190  1.1     haya {
    191  1.1     haya     struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc*)sc;
    192  1.1     haya     struct cardbus_softc *psc = (struct cardbus_softc *)sc->sc_dev.dv_parent;
    193  1.1     haya     cardbus_chipset_tag_t cc = psc->sc_cc;
    194  1.1     haya     cardbus_function_tag_t cf = psc->sc_cf;
    195  1.1     haya     pcireg_t command;
    196  1.1     haya 
    197  1.3     joda     cardbustag_t tag = cardbus_make_tag(cc, cf, csc->ct->ct_bus,
    198  1.3     joda 					csc->ct->ct_dev, csc->ct->ct_func);
    199  1.1     haya 
    200  1.3     joda     command = Cardbus_conf_read(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG);
    201  1.3     joda     if(csc->base0_reg) {
    202  1.3     joda 	Cardbus_conf_write(csc->ct, tag,
    203  1.3     joda 			   CARDBUS_BASE0_REG, csc->base0_reg);
    204  1.3     joda 	(cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
    205  1.3     joda 	command |= CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE;
    206  1.3     joda 
    207  1.3     joda     } else if(csc->base1_reg) {
    208  1.3     joda 	Cardbus_conf_write(csc->ct, tag,
    209  1.3     joda 			   CARDBUS_BASE1_REG, csc->base1_reg);
    210  1.3     joda 	(cf->cardbus_ctrl)(cc, CARDBUS_IO_ENABLE);
    211  1.3     joda 	command |= (CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE);
    212  1.3     joda     }
    213  1.1     haya     /* enable the card */
    214  1.3     joda     Cardbus_conf_write(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG, command);
    215  1.1     haya }
    216  1.1     haya 
    217  1.1     haya static int
    218  1.1     haya fxp_cardbus_enable(struct fxp_softc *sc)
    219  1.1     haya {
    220  1.1     haya     struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc*)sc;
    221  1.1     haya     struct cardbus_softc *psc = (struct cardbus_softc *)sc->sc_dev.dv_parent;
    222  1.1     haya     cardbus_chipset_tag_t cc = psc->sc_cc;
    223  1.1     haya     cardbus_function_tag_t cf = psc->sc_cf;
    224  1.1     haya 
    225  1.4     haya     Cardbus_function_enable(csc->ct);
    226  1.1     haya 
    227  1.1     haya     fxp_cardbus_setup(sc);
    228  1.1     haya 
    229  1.1     haya     /* Map and establish the interrupt. */
    230  1.1     haya 
    231  1.1     haya     sc->sc_ih = cardbus_intr_establish(cc, cf, psc->sc_intrline, IPL_NET,
    232  1.1     haya 				       fxp_intr, sc);
    233  1.1     haya     if (NULL == sc->sc_ih) {
    234  1.1     haya 	printf("%s: couldn't establish interrupt\n", sc->sc_dev.dv_xname);
    235  1.1     haya 	return 1;
    236  1.1     haya     }
    237  1.1     haya 
    238  1.1     haya     printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname, psc->sc_intrline);
    239  1.1     haya 
    240  1.1     haya     return 0;
    241  1.1     haya }
    242  1.1     haya 
    243  1.1     haya static void
    244  1.1     haya fxp_cardbus_disable(struct fxp_softc *sc)
    245  1.1     haya {
    246  1.1     haya     struct cardbus_softc *psc = (struct cardbus_softc *)sc->sc_dev.dv_parent;
    247  1.1     haya     cardbus_chipset_tag_t cc = psc->sc_cc;
    248  1.1     haya     cardbus_function_tag_t cf = psc->sc_cf;
    249  1.1     haya 
    250  1.1     haya     cardbus_intr_disestablish(cc, cf, sc->sc_ih); /* remove intr handler */
    251  1.1     haya 
    252  1.4     haya     Cardbus_function_disable(((struct fxp_cardbus_softc*)sc)->ct);
    253  1.6     joda }
    254  1.6     joda 
    255  1.6     joda static int
    256  1.6     joda fxp_cardbus_detach(self, flags)
    257  1.6     joda 	struct device *self;
    258  1.6     joda 	int flags;
    259  1.6     joda {
    260  1.6     joda 	struct fxp_softc *sc = (struct fxp_softc*)self;
    261  1.6     joda 	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc*)self;
    262  1.6     joda 	struct cardbus_devfunc *ct = csc->ct;
    263  1.6     joda 	int rv;
    264  1.6     joda 	int reg;
    265  1.6     joda 
    266  1.6     joda #ifdef DIAGNOSTIC
    267  1.6     joda 	if (ct == NULL) {
    268  1.6     joda 		panic("%s: data structure lacks\n", sc->sc_dev.dv_xname);
    269  1.6     joda 	}
    270  1.6     joda #endif
    271  1.6     joda 
    272  1.6     joda 	rv = fxp_detach(sc);
    273  1.6     joda 	if (rv == 0) {
    274  1.6     joda 		/*
    275  1.6     joda 		 * Unhook the interrupt handler.
    276  1.6     joda 		 */
    277  1.6     joda 		cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, sc->sc_ih);
    278  1.6     joda 
    279  1.6     joda 		/*
    280  1.6     joda 		 * release bus space and close window
    281  1.6     joda 		 */
    282  1.6     joda 		if(csc->base0_reg) {
    283  1.6     joda 			reg = CARDBUS_BASE0_REG;
    284  1.6     joda 		} else {
    285  1.6     joda 			reg = CARDBUS_BASE1_REG;
    286  1.6     joda 		}
    287  1.6     joda 		Cardbus_mapreg_unmap(ct, reg, sc->sc_st, sc->sc_sh, csc->size);
    288  1.6     joda 	}
    289  1.6     joda 	return (rv);
    290  1.1     haya }
    291