xref: /freebsd/sys/dev/pst/pst-pci.c (revision cab6a39d7b343596a5823e65c0f7b426551ec22d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2001,2002,2003 Søren Schmidt <sos@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer,
12  *    without modification, immediately at the beginning of the file.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <sys/bio.h>
40 #include <sys/malloc.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <vm/vm.h>
44 #include <vm/pmap.h>
45 #include <machine/stdarg.h>
46 #include <machine/resource.h>
47 #include <machine/bus.h>
48 #include <sys/rman.h>
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pcireg.h>
51 
52 #include "dev/pst/pst-iop.h"
53 
54 static int
55 iop_pci_probe(device_t dev)
56 {
57     /* tested with actual hardware kindly donated by Promise */
58     if (pci_get_devid(dev) == 0x19628086 && pci_get_subvendor(dev) == 0x105a) {
59 	device_set_desc(dev, "Promise SuperTrak SX6000 ATA RAID controller");
60 	return BUS_PROBE_DEFAULT;
61     }
62 
63     /* support the older SuperTrak 100 as well */
64     if (pci_get_devid(dev) == 0x19608086 && pci_get_subvendor(dev) == 0x105a) {
65 	device_set_desc(dev, "Promise SuperTrak 100 ATA RAID controller");
66 	return BUS_PROBE_DEFAULT;
67     }
68 
69     return ENXIO;
70 }
71 
72 static int
73 iop_pci_attach(device_t dev)
74 {
75     struct iop_softc *sc = device_get_softc(dev);
76     int rid;
77 
78     /* get resources */
79     rid = PCIR_BAR(0);
80     sc->r_mem =
81 	bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
82 
83     if (!sc->r_mem)
84 	return ENXIO;
85 
86     rid = 0x00;
87     sc->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
88 				       RF_SHAREABLE | RF_ACTIVE);
89 
90     /* now setup the infrastructure to talk to the device */
91     pci_enable_busmaster(dev);
92 
93     sc->ibase = rman_get_virtual(sc->r_mem);
94     sc->reg = (struct i2o_registers *)sc->ibase;
95     sc->dev = dev;
96     mtx_init(&sc->mtx, "pst lock", NULL, MTX_DEF);
97 
98     if (!iop_init(sc))
99 	return 0;
100     return bus_generic_attach(dev);
101 }
102 
103 static int
104 iop_pci_detach(device_t dev)
105 {
106     struct iop_softc *sc = device_get_softc(dev);
107     int error;
108 
109     error = bus_generic_detach(dev);
110     if (error)
111 	    return (error);
112     bus_teardown_intr(dev, sc->r_irq, sc->handle);
113     bus_release_resource(dev, SYS_RES_IRQ, 0x00, sc->r_irq);
114     bus_release_resource(dev, SYS_RES_MEMORY, PCIR_BAR(0), sc->r_mem);
115     mtx_destroy(&sc->mtx);
116     return (0);
117 }
118 
119 static device_method_t pst_pci_methods[] = {
120     DEVMETHOD(device_probe,		iop_pci_probe),
121     DEVMETHOD(device_attach,		iop_pci_attach),
122     DEVMETHOD(device_detach,		iop_pci_detach),
123     { 0, 0 }
124 };
125 
126 static driver_t pst_pci_driver = {
127     "pstpci",
128     pst_pci_methods,
129     sizeof(struct iop_softc),
130 };
131 
132 static devclass_t pst_pci_devclass;
133 
134 DRIVER_MODULE(pstpci, pci, pst_pci_driver, pst_pci_devclass, 0, 0);
135