Home | History | Annotate | Line # | Download | only in pci
ahcisata_pci.c revision 1.12.4.2.4.1
      1  1.12.4.2.4.1      matt /*	$NetBSD: ahcisata_pci.c,v 1.12.4.2.4.1 2013/11/05 18:32:45 matt Exp $	*/
      2           1.1    bouyer 
      3           1.1    bouyer /*
      4           1.1    bouyer  * Copyright (c) 2006 Manuel Bouyer.
      5           1.1    bouyer  *
      6           1.1    bouyer  * Redistribution and use in source and binary forms, with or without
      7           1.1    bouyer  * modification, are permitted provided that the following conditions
      8           1.1    bouyer  * are met:
      9           1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     10           1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     11           1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     12           1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     13           1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     14           1.1    bouyer  * 3. All advertising materials mentioning features or use of this software
     15           1.1    bouyer  *    must display the following acknowledgement:
     16           1.1    bouyer  *	This product includes software developed by Manuel Bouyer.
     17           1.1    bouyer  * 4. The name of the author may not be used to endorse or promote products
     18           1.1    bouyer  *    derived from this software without specific prior written permission.
     19           1.1    bouyer  *
     20           1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21           1.1    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22           1.1    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23           1.1    bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24           1.1    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25           1.1    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26           1.1    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27           1.1    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28           1.1    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29           1.1    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30           1.1    bouyer  *
     31           1.1    bouyer  */
     32           1.1    bouyer 
     33           1.1    bouyer #include <sys/cdefs.h>
     34  1.12.4.2.4.1      matt __KERNEL_RCSID(0, "$NetBSD: ahcisata_pci.c,v 1.12.4.2.4.1 2013/11/05 18:32:45 matt Exp $");
     35           1.1    bouyer 
     36           1.1    bouyer #include <sys/types.h>
     37           1.1    bouyer #include <sys/malloc.h>
     38           1.1    bouyer #include <sys/param.h>
     39           1.1    bouyer #include <sys/kernel.h>
     40           1.1    bouyer #include <sys/systm.h>
     41           1.1    bouyer #include <sys/disklabel.h>
     42           1.2  jmcneill #include <sys/pmf.h>
     43           1.1    bouyer 
     44           1.1    bouyer #include <uvm/uvm_extern.h>
     45           1.1    bouyer 
     46           1.1    bouyer #include <dev/pci/pcivar.h>
     47           1.1    bouyer #include <dev/pci/pcidevs.h>
     48           1.1    bouyer #include <dev/pci/pciidereg.h>
     49           1.1    bouyer #include <dev/pci/pciidevar.h>
     50           1.1    bouyer #include <dev/ic/ahcisatavar.h>
     51           1.1    bouyer 
     52          1.12     dillo #define AHCI_PCI_QUIRK_FORCE	1	/* force attach */
     53  1.12.4.2.4.1      matt #define	AHCI_PCI_QUIRK_BAR0	2
     54          1.12     dillo 
     55          1.12     dillo static const struct pci_quirkdata ahci_pci_quirks[] = {
     56          1.12     dillo 	{ PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_SATA,
     57          1.12     dillo 	    AHCI_PCI_QUIRK_FORCE },
     58      1.12.4.2       snj 	{ PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_SATA2,
     59      1.12.4.2       snj 	    AHCI_PCI_QUIRK_FORCE },
     60      1.12.4.2       snj 	{ PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_SATA3,
     61      1.12.4.2       snj 	    AHCI_PCI_QUIRK_FORCE },
     62      1.12.4.2       snj 	{ PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_SATA4,
     63      1.12.4.2       snj 	    AHCI_PCI_QUIRK_FORCE },
     64          1.12     dillo 	{ PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP67_SATA,
     65          1.12     dillo 	    AHCI_PCI_QUIRK_FORCE },
     66      1.12.4.1       snj 	{ PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP73_AHCI_1,
     67      1.12.4.1       snj 	    AHCI_PCI_QUIRK_FORCE },
     68  1.12.4.2.4.1      matt 	{ PCI_VENDOR_NETLOGIC, PCI_PRODUCT_NETLOGIC_XLP_AHCISATA,
     69  1.12.4.2.4.1      matt 	    AHCI_PCI_QUIRK_BAR0 },
     70          1.12     dillo };
     71          1.12     dillo 
     72           1.2  jmcneill struct ahci_pci_softc {
     73           1.9      cube 	struct ahci_softc ah_sc;
     74           1.2  jmcneill 	pci_chipset_tag_t sc_pc;
     75           1.2  jmcneill 	pcitag_t sc_pcitag;
     76           1.2  jmcneill };
     77           1.2  jmcneill 
     78           1.2  jmcneill 
     79           1.9      cube static int  ahci_pci_match(device_t, cfdata_t, void *);
     80           1.9      cube static void ahci_pci_attach(device_t, device_t, void *);
     81          1.12     dillo const struct pci_quirkdata *ahci_pci_lookup_quirkdata(pci_vendor_id_t,
     82          1.12     dillo 						      pci_product_id_t);
     83           1.8    dyoung static bool ahci_pci_resume(device_t PMF_FN_PROTO);
     84           1.2  jmcneill 
     85           1.1    bouyer 
     86           1.9      cube CFATTACH_DECL_NEW(ahcisata_pci, sizeof(struct ahci_pci_softc),
     87           1.1    bouyer     ahci_pci_match, ahci_pci_attach, NULL, NULL);
     88           1.1    bouyer 
     89           1.1    bouyer static int
     90           1.9      cube ahci_pci_match(device_t parent, cfdata_t match, void *aux)
     91           1.1    bouyer {
     92           1.1    bouyer 	struct pci_attach_args *pa = aux;
     93           1.1    bouyer 	bus_space_tag_t regt;
     94           1.1    bouyer 	bus_space_handle_t regh;
     95           1.1    bouyer 	bus_size_t size;
     96           1.1    bouyer 	int ret = 0;
     97          1.12     dillo 	const struct pci_quirkdata *quirks;
     98  1.12.4.2.4.1      matt 	int bar = AHCI_PCI_ABAR;
     99          1.12     dillo 
    100          1.12     dillo 	quirks = ahci_pci_lookup_quirkdata(PCI_VENDOR(pa->pa_id),
    101          1.12     dillo 					   PCI_PRODUCT(pa->pa_id));
    102           1.1    bouyer 
    103          1.12     dillo 	/* if wrong class and not forced by quirks, don't match */
    104          1.12     dillo 	if ((PCI_CLASS(pa->pa_class) != PCI_CLASS_MASS_STORAGE ||
    105          1.12     dillo 	    ((PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_MASS_STORAGE_SATA ||
    106          1.12     dillo 	     PCI_INTERFACE(pa->pa_class) != PCI_INTERFACE_SATA_AHCI) &&
    107          1.12     dillo 	     PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_MASS_STORAGE_RAID)) &&
    108          1.12     dillo 	    (quirks == NULL || (quirks->quirks & AHCI_PCI_QUIRK_FORCE) == 0))
    109          1.12     dillo 		return 0;
    110          1.12     dillo 
    111  1.12.4.2.4.1      matt 	/*
    112  1.12.4.2.4.1      matt 	 * Sometimes people just can't read specs.
    113  1.12.4.2.4.1      matt 	 */
    114  1.12.4.2.4.1      matt 	if (quirks != NULL && (quirks->quirks & AHCI_PCI_QUIRK_BAR0)) {
    115  1.12.4.2.4.1      matt 		bar = PCI_BAR0;
    116  1.12.4.2.4.1      matt 	}
    117  1.12.4.2.4.1      matt 
    118  1.12.4.2.4.1      matt 	pcireg_t mem_type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, bar);
    119  1.12.4.2.4.1      matt 	if (PCI_MAPREG_TYPE(mem_type) != PCI_MAPREG_TYPE_MEM) {
    120  1.12.4.2.4.1      matt 		printf("%s: tag %#lx: unexpected type %#x\n",
    121  1.12.4.2.4.1      matt 		    __func__, pa->pa_tag, mem_type);
    122  1.12.4.2.4.1      matt 		return 0;
    123  1.12.4.2.4.1      matt 	}
    124  1.12.4.2.4.1      matt 
    125  1.12.4.2.4.1      matt 	if (pci_mapreg_map(pa, bar, mem_type, 0,
    126  1.12.4.2.4.1      matt 	    &regt, &regh, NULL, &size) != 0) {
    127  1.12.4.2.4.1      matt 		printf("%s: tag %#lx: pci_mapreg_map failed\n",
    128  1.12.4.2.4.1      matt 		    __func__, pa->pa_tag);
    129          1.12     dillo 		return 0;
    130  1.12.4.2.4.1      matt 	}
    131          1.12     dillo 
    132          1.12     dillo 	if ((PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_SATA &&
    133           1.3   xtraeme 	     PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_SATA_AHCI) ||
    134          1.12     dillo 	    (quirks && quirks->quirks & AHCI_PCI_QUIRK_FORCE) ||
    135          1.12     dillo 	    (bus_space_read_4(regt, regh, AHCI_GHC) & AHCI_GHC_AE))
    136          1.12     dillo 		ret = 3;
    137           1.1    bouyer 
    138          1.12     dillo 	bus_space_unmap(regt, regh, size);
    139           1.5   jnemeth 	return ret;
    140           1.1    bouyer }
    141           1.1    bouyer 
    142           1.1    bouyer static void
    143           1.9      cube ahci_pci_attach(device_t parent, device_t self, void *aux)
    144           1.1    bouyer {
    145           1.1    bouyer 	struct pci_attach_args *pa = aux;
    146           1.9      cube 	struct ahci_pci_softc *psc = device_private(self);
    147           1.2  jmcneill 	struct ahci_softc *sc = &psc->ah_sc;
    148           1.1    bouyer 	bus_size_t size;
    149           1.1    bouyer 	char devinfo[256];
    150           1.1    bouyer 	const char *intrstr;
    151           1.1    bouyer 	pci_intr_handle_t intrhandle;
    152           1.1    bouyer 	void *ih;
    153  1.12.4.2.4.1      matt 	const struct pci_quirkdata *quirks;
    154  1.12.4.2.4.1      matt 	int bar = AHCI_PCI_ABAR;
    155           1.1    bouyer 
    156          1.11      cube 	sc->sc_atac.atac_dev = self;
    157          1.10      cube 
    158  1.12.4.2.4.1      matt 	/*
    159  1.12.4.2.4.1      matt 	 * Sometimes people just can't read specs.
    160  1.12.4.2.4.1      matt 	 */
    161  1.12.4.2.4.1      matt 	quirks = ahci_pci_lookup_quirkdata(PCI_VENDOR(pa->pa_id),
    162  1.12.4.2.4.1      matt 					   PCI_PRODUCT(pa->pa_id));
    163  1.12.4.2.4.1      matt 	if (quirks != NULL && (quirks->quirks & AHCI_PCI_QUIRK_BAR0)) {
    164  1.12.4.2.4.1      matt 		bar = PCI_BAR0;
    165  1.12.4.2.4.1      matt 	}
    166  1.12.4.2.4.1      matt 
    167  1.12.4.2.4.1      matt 	if (pci_mapreg_map(pa, bar, PCI_MAPREG_TYPE_MEM, 0,
    168           1.1    bouyer 	    &sc->sc_ahcit, &sc->sc_ahcih, NULL, &size) != 0) {
    169  1.12.4.2.4.1      matt 		aprint_error_dev(self, ": can't map ahci registers\n");
    170           1.1    bouyer 		return;
    171           1.1    bouyer 	}
    172           1.2  jmcneill 	psc->sc_pc = pa->pa_pc;
    173           1.2  jmcneill 	psc->sc_pcitag = pa->pa_tag;
    174           1.2  jmcneill 
    175           1.1    bouyer 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    176           1.1    bouyer 	aprint_naive(": AHCI disk controller\n");
    177           1.1    bouyer 	aprint_normal(": %s\n", devinfo);
    178           1.1    bouyer 
    179           1.1    bouyer 	if (pci_intr_map(pa, &intrhandle) != 0) {
    180           1.1    bouyer 		aprint_error("%s: couldn't map interrupt\n", AHCINAME(sc));
    181           1.1    bouyer 		return;
    182           1.1    bouyer 	}
    183           1.1    bouyer 	intrstr = pci_intr_string(pa->pa_pc, intrhandle);
    184           1.1    bouyer 	ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_BIO, ahci_intr, sc);
    185           1.1    bouyer 	if (ih == NULL) {
    186           1.1    bouyer 		aprint_error("%s: couldn't establish interrupt", AHCINAME(sc));
    187           1.1    bouyer 		return;
    188           1.1    bouyer 	}
    189           1.1    bouyer 	aprint_normal("%s: interrupting at %s\n", AHCINAME(sc),
    190           1.1    bouyer 	    intrstr ? intrstr : "unknown interrupt");
    191           1.1    bouyer 	sc->sc_dmat = pa->pa_dmat;
    192           1.3   xtraeme 
    193           1.6   mlelstv 	if (PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_RAID) {
    194           1.7   mlelstv 		AHCIDEBUG_PRINT(("%s: RAID mode\n", AHCINAME(sc)), DEBUG_PROBE);
    195           1.3   xtraeme 		sc->sc_atac_capflags = ATAC_CAP_RAID;
    196           1.6   mlelstv 	} else {
    197           1.7   mlelstv 		AHCIDEBUG_PRINT(("%s: SATA mode\n", AHCINAME(sc)), DEBUG_PROBE);
    198           1.6   mlelstv 	}
    199           1.3   xtraeme 
    200           1.1    bouyer 	ahci_attach(sc);
    201           1.2  jmcneill 
    202           1.2  jmcneill 	if (!pmf_device_register(self, NULL, ahci_pci_resume))
    203           1.2  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    204           1.2  jmcneill }
    205           1.2  jmcneill 
    206           1.2  jmcneill static bool
    207           1.8    dyoung ahci_pci_resume(device_t dv PMF_FN_ARGS)
    208           1.2  jmcneill {
    209           1.2  jmcneill 	struct ahci_pci_softc *psc = device_private(dv);
    210           1.2  jmcneill 	struct ahci_softc *sc = &psc->ah_sc;
    211           1.2  jmcneill 	int s;
    212           1.2  jmcneill 
    213           1.2  jmcneill 	s = splbio();
    214           1.2  jmcneill 	ahci_reset(sc);
    215           1.2  jmcneill 	ahci_setup_ports(sc);
    216           1.2  jmcneill 	ahci_reprobe_drives(sc);
    217           1.2  jmcneill 	ahci_enable_intrs(sc);
    218           1.2  jmcneill 	splx(s);
    219           1.2  jmcneill 
    220           1.2  jmcneill 	return true;
    221           1.1    bouyer }
    222          1.12     dillo 
    223          1.12     dillo const struct pci_quirkdata *
    224          1.12     dillo ahci_pci_lookup_quirkdata(pci_vendor_id_t vendor, pci_product_id_t product)
    225          1.12     dillo {
    226          1.12     dillo 	int i;
    227          1.12     dillo 
    228          1.12     dillo 	for (i = 0; i < (sizeof ahci_pci_quirks / sizeof ahci_pci_quirks[0]);
    229          1.12     dillo 	     i++)
    230          1.12     dillo 		if (vendor == ahci_pci_quirks[i].vendor &&
    231          1.12     dillo 		    product == ahci_pci_quirks[i].product)
    232          1.12     dillo 			return (&ahci_pci_quirks[i]);
    233          1.12     dillo 	return (NULL);
    234          1.12     dillo }
    235