Home | History | Annotate | Line # | Download | only in pci
if_athn_pci.c revision 1.12.18.1
      1  1.12.18.1      phil /*	$NetBSD: if_athn_pci.c,v 1.12.18.1 2018/07/12 16:35:33 phil Exp $	*/
      2        1.1  christos /*	$OpenBSD: if_athn_pci.c,v 1.11 2011/01/08 10:02:32 damien Exp $	*/
      3        1.1  christos 
      4        1.1  christos /*-
      5        1.1  christos  * Copyright (c) 2009 Damien Bergamini <damien.bergamini (at) free.fr>
      6        1.1  christos  *
      7        1.1  christos  * Permission to use, copy, modify, and distribute this software for any
      8        1.1  christos  * purpose with or without fee is hereby granted, provided that the above
      9        1.1  christos  * copyright notice and this permission notice appear in all copies.
     10        1.1  christos  *
     11        1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12        1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13        1.1  christos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14        1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15        1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16        1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17        1.1  christos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18        1.1  christos  */
     19        1.1  christos 
     20        1.1  christos /*
     21        1.1  christos  * PCI front-end for Atheros 802.11a/g/n chipsets.
     22        1.1  christos  */
     23        1.1  christos 
     24        1.1  christos #include <sys/cdefs.h>
     25  1.12.18.1      phil __KERNEL_RCSID(0, "$NetBSD: if_athn_pci.c,v 1.12.18.1 2018/07/12 16:35:33 phil Exp $");
     26        1.1  christos 
     27        1.1  christos #include "opt_inet.h"
     28        1.1  christos 
     29        1.1  christos #include <sys/param.h>
     30        1.1  christos #include <sys/sockio.h>
     31        1.1  christos #include <sys/mbuf.h>
     32        1.1  christos #include <sys/kernel.h>
     33        1.1  christos #include <sys/socket.h>
     34        1.1  christos #include <sys/systm.h>
     35        1.1  christos #include <sys/malloc.h>
     36        1.1  christos #include <sys/callout.h>
     37        1.1  christos #include <sys/device.h>
     38        1.1  christos 
     39        1.1  christos #include <sys/bus.h>
     40        1.1  christos #include <sys/intr.h>
     41        1.1  christos 
     42        1.1  christos #include <net/if.h>
     43        1.5  christos #include <net/if_ether.h>
     44        1.1  christos #include <net/if_media.h>
     45  1.12.18.1      phil #include <net/route.h>
     46        1.1  christos 
     47        1.1  christos #include <net80211/ieee80211_var.h>
     48        1.1  christos #include <net80211/ieee80211_amrr.h>
     49        1.1  christos #include <net80211/ieee80211_radiotap.h>
     50        1.1  christos 
     51        1.1  christos #include <dev/ic/athnreg.h>
     52        1.1  christos #include <dev/ic/athnvar.h>
     53        1.1  christos 
     54        1.1  christos #include <dev/pci/pcireg.h>
     55        1.1  christos #include <dev/pci/pcivar.h>
     56        1.1  christos #include <dev/pci/pcidevs.h>
     57        1.1  christos 
     58        1.1  christos #define PCI_SUBSYSID_ATHEROS_COEX2WIRE		0x309b
     59        1.1  christos #define PCI_SUBSYSID_ATHEROS_COEX3WIRE_SA	0x30aa
     60        1.1  christos #define PCI_SUBSYSID_ATHEROS_COEX3WIRE_DA	0x30ab
     61        1.1  christos 
     62        1.1  christos #define ATHN_PCI_MMBA	PCI_BAR(0)	/* memory mapped base */
     63        1.1  christos 
     64        1.1  christos struct athn_pci_softc {
     65        1.1  christos 	struct athn_softc	psc_sc;
     66        1.1  christos 
     67        1.1  christos 	/* PCI specific goo. */
     68        1.1  christos 	pci_chipset_tag_t	psc_pc;
     69        1.1  christos 	pcitag_t		psc_tag;
     70       1.12  jakllsch 	pci_intr_handle_t	psc_pih;
     71        1.1  christos 	void			*psc_ih;
     72        1.1  christos 	bus_space_tag_t		psc_iot;
     73        1.1  christos 	bus_space_handle_t	psc_ioh;
     74        1.1  christos 	bus_size_t		psc_mapsz;
     75        1.1  christos 	int			psc_cap_off;
     76        1.1  christos };
     77        1.1  christos 
     78        1.1  christos #define Static static
     79        1.1  christos 
     80        1.1  christos Static int	athn_pci_match(device_t, cfdata_t, void *);
     81        1.1  christos Static void	athn_pci_attach(device_t, device_t, void *);
     82        1.1  christos Static int	athn_pci_detach(device_t, int);
     83        1.1  christos Static int	athn_pci_activate(device_t, enum devact);
     84        1.1  christos 
     85        1.1  christos CFATTACH_DECL_NEW(athn_pci, sizeof(struct athn_pci_softc), athn_pci_match,
     86        1.1  christos     athn_pci_attach, athn_pci_detach, athn_pci_activate);
     87        1.1  christos 
     88        1.1  christos Static bool	athn_pci_resume(device_t, const pmf_qual_t *);
     89        1.1  christos Static bool	athn_pci_suspend(device_t, const pmf_qual_t *);
     90        1.1  christos Static uint32_t	athn_pci_read(struct athn_softc *, uint32_t);
     91        1.1  christos Static void	athn_pci_write(struct athn_softc *, uint32_t, uint32_t);
     92        1.1  christos Static void	athn_pci_write_barrier(struct athn_softc *);
     93        1.1  christos Static void	athn_pci_disable_aspm(struct athn_softc *);
     94        1.1  christos 
     95        1.1  christos Static int
     96        1.1  christos athn_pci_match(device_t parent, cfdata_t match, void *aux)
     97        1.1  christos {
     98        1.1  christos 	static const struct {
     99        1.1  christos 		pci_vendor_id_t		apd_vendor;
    100        1.1  christos 		pci_product_id_t	apd_product;
    101        1.1  christos 	} athn_pci_devices[] = {
    102        1.1  christos 		{ PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR5416 },
    103        1.1  christos 		{ PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR5418 },
    104        1.1  christos 		{ PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9160 },
    105        1.1  christos 		{ PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9280 },
    106        1.1  christos 		{ PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9281 },
    107        1.1  christos 		{ PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9285 },
    108        1.1  christos 		{ PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR2427 },
    109        1.1  christos 		{ PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9227 },
    110        1.1  christos 		{ PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9287 },
    111        1.1  christos 		{ PCI_VENDOR_ATHEROS, PCI_PRODUCT_ATHEROS_AR9300 }
    112        1.1  christos 	};
    113        1.1  christos 	struct pci_attach_args *pa = aux;
    114        1.1  christos 	size_t i;
    115        1.1  christos 
    116        1.1  christos 	for (i = 0; i < __arraycount(athn_pci_devices); i++) {
    117        1.1  christos 		if (PCI_VENDOR(pa->pa_id) == athn_pci_devices[i].apd_vendor &&
    118        1.2    martin 		    PCI_PRODUCT(pa->pa_id) == athn_pci_devices[i].apd_product)
    119        1.7    martin 			/*
    120        1.7    martin 			 * Match better than 1, we prefer this driver
    121        1.7    martin 			 * over ath(4)
    122        1.7    martin 			 */
    123        1.7    martin 			return 10;
    124        1.1  christos 	}
    125        1.1  christos 	return 0;
    126        1.1  christos }
    127        1.1  christos 
    128        1.1  christos Static void
    129        1.1  christos athn_pci_attach(device_t parent, device_t self, void *aux)
    130        1.1  christos {
    131        1.1  christos 	struct athn_pci_softc *psc = device_private(self);
    132        1.1  christos 	struct athn_softc *sc = &psc->psc_sc;
    133        1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
    134        1.1  christos 	struct pci_attach_args *pa = aux;
    135        1.1  christos 	const char *intrstr;
    136        1.1  christos 	pcireg_t memtype, reg;
    137        1.1  christos 	pci_product_id_t subsysid;
    138        1.1  christos 	int error;
    139       1.10  christos 	char intrbuf[PCI_INTRSTR_LEN];
    140        1.1  christos 
    141        1.3    martin 	sc->sc_dev = self;
    142        1.1  christos 	sc->sc_dmat = pa->pa_dmat;
    143        1.1  christos 	psc->psc_pc = pa->pa_pc;
    144        1.1  christos 	psc->psc_tag = pa->pa_tag;
    145        1.1  christos 
    146        1.1  christos 	sc->sc_ops.read = athn_pci_read;
    147        1.1  christos 	sc->sc_ops.write = athn_pci_write;
    148        1.1  christos 	sc->sc_ops.write_barrier = athn_pci_write_barrier;
    149        1.1  christos 
    150        1.1  christos 	/*
    151        1.1  christos 	 * Get the offset of the PCI Express Capability Structure in PCI
    152        1.1  christos 	 * Configuration Space (Linux hardcodes it as 0x60.)
    153        1.1  christos 	 */
    154        1.1  christos 	error = pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_PCIEXPRESS,
    155        1.1  christos 	    &psc->psc_cap_off, NULL);
    156        1.1  christos 	if (error != 0) {	/* Found. */
    157        1.1  christos 		sc->sc_disable_aspm = athn_pci_disable_aspm;
    158        1.1  christos 		sc->sc_flags |= ATHN_FLAG_PCIE;
    159        1.1  christos 	}
    160        1.1  christos 	/*
    161        1.1  christos 	 * Noone knows why this shit is necessary but there are claims that
    162        1.1  christos 	 * not doing this may cause very frequent PCI FATAL interrupts from
    163        1.1  christos 	 * the card: http://bugzilla.kernel.org/show_bug.cgi?id=13483
    164        1.1  christos 	 */
    165        1.1  christos 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x40);
    166        1.1  christos 	if (reg & 0xff00)
    167        1.1  christos 		pci_conf_write(pa->pa_pc, pa->pa_tag, 0x40, reg & ~0xff00);
    168        1.1  christos 
    169        1.1  christos 	/* Change latency timer; default value yields poor results. */
    170        1.1  christos 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG);
    171        1.1  christos 	reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
    172        1.1  christos 	reg |= 168 << PCI_LATTIMER_SHIFT;
    173        1.1  christos 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG, reg);
    174        1.1  christos 
    175        1.1  christos 	/* Determine if bluetooth is also supported (combo chip.) */
    176        1.1  christos 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    177        1.1  christos 	subsysid = PCI_PRODUCT(reg);
    178        1.1  christos 	if (subsysid == PCI_SUBSYSID_ATHEROS_COEX3WIRE_SA ||
    179        1.1  christos 	    subsysid == PCI_SUBSYSID_ATHEROS_COEX3WIRE_DA)
    180        1.1  christos 		sc->sc_flags |= ATHN_FLAG_BTCOEX3WIRE;
    181        1.1  christos 	else if (subsysid == PCI_SUBSYSID_ATHEROS_COEX2WIRE)
    182        1.1  christos 		sc->sc_flags |= ATHN_FLAG_BTCOEX2WIRE;
    183        1.1  christos 
    184        1.1  christos 	/*
    185        1.1  christos 	 * Setup memory-mapping of PCI registers.
    186        1.1  christos 	 */
    187        1.1  christos 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, ATHN_PCI_MMBA);
    188        1.1  christos 	if (memtype != PCI_MAPREG_TYPE_MEM &&
    189        1.1  christos 	    memtype != PCI_MAPREG_MEM_TYPE_64BIT) {
    190        1.1  christos 		aprint_error_dev(self, "bad pci register type %d\n",
    191        1.1  christos 		    (int)memtype);
    192        1.1  christos 		goto fail;
    193        1.1  christos 	}
    194        1.9    martin 	error = pci_mapreg_map(pa, ATHN_PCI_MMBA, memtype, 0, &psc->psc_iot,
    195        1.1  christos 	    &psc->psc_ioh, NULL, &psc->psc_mapsz);
    196        1.1  christos 	if (error != 0) {
    197        1.1  christos 		aprint_error_dev(self, "cannot map register space\n");
    198        1.1  christos 		goto fail;
    199        1.1  christos 	}
    200        1.1  christos 
    201        1.1  christos 	/*
    202        1.1  christos 	 * Arrange interrupt line.
    203        1.1  christos 	 */
    204       1.12  jakllsch 	if (pci_intr_map(pa, &psc->psc_pih) != 0) {
    205        1.1  christos 		aprint_error_dev(self, "couldn't map interrupt\n");
    206        1.1  christos 		goto fail1;
    207        1.1  christos 	}
    208        1.1  christos 
    209       1.12  jakllsch 	intrstr = pci_intr_string(psc->psc_pc, psc->psc_pih, intrbuf, sizeof(intrbuf));
    210       1.12  jakllsch 	psc->psc_ih = pci_intr_establish(psc->psc_pc, psc->psc_pih, IPL_NET,
    211       1.12  jakllsch 	    athn_intr, sc);
    212        1.1  christos 	if (psc->psc_ih == NULL) {
    213        1.1  christos 		aprint_error_dev(self, "couldn't map interrupt\n");
    214        1.1  christos 		goto fail1;
    215        1.1  christos 	}
    216        1.1  christos 
    217  1.12.18.1      phil #if notyet
    218  1.12.18.1      phil 	/* NNN -- Does ic_vaps have anything yet???? */
    219  1.12.18.1      phil 	TAILQ_FIRST(&(sc->sc_ic.ic_vaps))->iv_ifp = &sc->sc_if;
    220  1.12.18.1      phil #endif
    221  1.12.18.1      phil 
    222        1.1  christos 	if (athn_attach(sc) != 0)
    223        1.1  christos 		goto fail2;
    224        1.1  christos 
    225        1.6    martin 	aprint_verbose_dev(self, "interrupting at %s\n", intrstr);
    226        1.6    martin 
    227        1.1  christos 	if (pmf_device_register(self, athn_pci_suspend, athn_pci_resume)) {
    228        1.1  christos 		pmf_class_network_register(self, &sc->sc_if);
    229        1.1  christos 		pmf_device_suspend(self, &sc->sc_qual);
    230        1.1  christos 	}
    231        1.1  christos 	else
    232        1.1  christos 		aprint_error_dev(self, "couldn't establish power handler\n");
    233        1.1  christos 
    234        1.1  christos 	ieee80211_announce(ic);
    235        1.1  christos 	return;
    236        1.1  christos 
    237        1.1  christos  fail2:
    238        1.1  christos 	pci_intr_disestablish(psc->psc_pc, psc->psc_ih);
    239        1.1  christos 	psc->psc_ih = NULL;
    240        1.1  christos  fail1:
    241        1.1  christos 	bus_space_unmap(psc->psc_iot, psc->psc_ioh, psc->psc_mapsz);
    242        1.1  christos 	psc->psc_mapsz = 0;
    243        1.1  christos  fail:
    244        1.1  christos 	return;
    245        1.1  christos }
    246        1.1  christos 
    247        1.1  christos Static int
    248        1.1  christos athn_pci_detach(device_t self, int flags)
    249        1.1  christos {
    250        1.1  christos 	struct athn_pci_softc *psc = device_private(self);
    251        1.1  christos 	struct athn_softc *sc = &psc->psc_sc;
    252        1.1  christos 
    253        1.1  christos 	if (psc->psc_ih != NULL) {
    254        1.1  christos 		athn_detach(sc);
    255        1.1  christos 		pci_intr_disestablish(psc->psc_pc, psc->psc_ih);
    256        1.1  christos 		psc->psc_ih = NULL;
    257        1.1  christos 	}
    258        1.1  christos 	if (psc->psc_mapsz > 0) {
    259        1.1  christos 		bus_space_unmap(psc->psc_iot, psc->psc_ioh, psc->psc_mapsz);
    260        1.1  christos 		psc->psc_mapsz = 0;
    261        1.1  christos 	}
    262        1.1  christos 	return 0;
    263        1.1  christos }
    264        1.1  christos 
    265        1.1  christos Static int
    266        1.1  christos athn_pci_activate(device_t self, enum devact act)
    267        1.1  christos {
    268        1.1  christos 	struct athn_pci_softc *psc = device_private(self);
    269        1.1  christos 	struct athn_softc *sc = &psc->psc_sc;
    270        1.1  christos 
    271        1.1  christos 	switch (act) {
    272        1.1  christos 	case DVACT_DEACTIVATE:
    273  1.12.18.1      phil 		if_deactivate(TAILQ_FIRST(&(sc->sc_ic.ic_vaps))->iv_ifp);
    274        1.1  christos 		break;
    275        1.1  christos 	}
    276        1.1  christos 	return 0;
    277        1.1  christos }
    278        1.1  christos 
    279        1.1  christos Static bool
    280        1.1  christos athn_pci_suspend(device_t self, const pmf_qual_t *qual)
    281        1.1  christos {
    282        1.1  christos 	struct athn_pci_softc *psc = device_private(self);
    283        1.1  christos 	struct athn_softc *sc = &psc->psc_sc;
    284        1.1  christos 
    285        1.1  christos 	athn_suspend(sc);
    286        1.1  christos 	if (psc->psc_ih != NULL) {
    287        1.1  christos 		pci_intr_disestablish(psc->psc_pc, psc->psc_ih);
    288        1.1  christos 		psc->psc_ih = NULL;
    289        1.1  christos 	}
    290        1.1  christos 	return true;
    291        1.1  christos }
    292        1.1  christos 
    293        1.1  christos Static bool
    294        1.1  christos athn_pci_resume(device_t self, const pmf_qual_t *qual)
    295        1.1  christos {
    296        1.1  christos 	struct athn_pci_softc *psc = device_private(self);
    297        1.1  christos 	struct athn_softc *sc = &psc->psc_sc;
    298        1.1  christos 	pcireg_t reg;
    299        1.1  christos 
    300        1.1  christos 	/*
    301        1.1  christos 	 * XXX: see comment in athn_attach().
    302        1.1  christos 	 */
    303        1.1  christos 	reg = pci_conf_read(psc->psc_pc, psc->psc_tag, 0x40);
    304        1.1  christos 	if (reg & 0xff00)
    305        1.1  christos 		pci_conf_write(psc->psc_pc, psc->psc_tag, 0x40, reg & ~0xff00);
    306        1.1  christos 
    307       1.12  jakllsch 	psc->psc_ih = pci_intr_establish(psc->psc_pc, psc->psc_pih, IPL_NET,
    308       1.12  jakllsch 	    athn_intr, sc);
    309        1.1  christos 	if (psc->psc_ih == NULL) {
    310        1.1  christos 		aprint_error_dev(self, "couldn't map interrupt\n");
    311        1.1  christos 		return false;
    312        1.1  christos 	}
    313        1.1  christos 	return athn_resume(sc);
    314        1.1  christos }
    315        1.1  christos 
    316        1.1  christos Static uint32_t
    317        1.1  christos athn_pci_read(struct athn_softc *sc, uint32_t addr)
    318        1.1  christos {
    319        1.1  christos 	struct athn_pci_softc *psc = (struct athn_pci_softc *)sc;
    320        1.1  christos 
    321        1.1  christos 	return bus_space_read_4(psc->psc_iot, psc->psc_ioh, addr);
    322        1.1  christos }
    323        1.1  christos 
    324        1.1  christos Static void
    325        1.1  christos athn_pci_write(struct athn_softc *sc, uint32_t addr, uint32_t val)
    326        1.1  christos {
    327        1.1  christos 	struct athn_pci_softc *psc = (struct athn_pci_softc *)sc;
    328        1.1  christos 
    329        1.1  christos 	bus_space_write_4(psc->psc_iot, psc->psc_ioh, addr, val);
    330        1.1  christos }
    331        1.1  christos 
    332        1.1  christos Static void
    333        1.1  christos athn_pci_write_barrier(struct athn_softc *sc)
    334        1.1  christos {
    335        1.1  christos 	struct athn_pci_softc *psc = (struct athn_pci_softc *)sc;
    336        1.1  christos 
    337        1.1  christos 	bus_space_barrier(psc->psc_iot, psc->psc_ioh, 0, psc->psc_mapsz,
    338        1.1  christos 	    BUS_SPACE_BARRIER_WRITE);
    339        1.1  christos }
    340        1.1  christos 
    341        1.1  christos Static void
    342        1.1  christos athn_pci_disable_aspm(struct athn_softc *sc)
    343        1.1  christos {
    344        1.1  christos 	struct athn_pci_softc *psc = (struct athn_pci_softc *)sc;
    345        1.1  christos 	pcireg_t reg;
    346        1.1  christos 
    347        1.1  christos 	/* Disable PCIe Active State Power Management (ASPM). */
    348        1.1  christos 	reg = pci_conf_read(psc->psc_pc, psc->psc_tag,
    349        1.8   msaitoh 	    psc->psc_cap_off + PCIE_LCSR);
    350        1.8   msaitoh 	reg &= ~(PCIE_LCSR_ASPM_L0S | PCIE_LCSR_ASPM_L1);
    351        1.1  christos 	pci_conf_write(psc->psc_pc, psc->psc_tag,
    352        1.8   msaitoh 	    psc->psc_cap_off + PCIE_LCSR, reg);
    353        1.1  christos }
    354