]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/dpt/dpt_pci.c
MFV r329552: less v530.
[FreeBSD/FreeBSD.git] / sys / dev / dpt / dpt_pci.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000 Matthew N. Dodd <winter@jurai.net>
5  * All rights reserved.
6  *
7  * Copyright (c) 1997 Simon Shapiro
8  * All Rights Reserved
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/bus.h>
42
43 #include <machine/bus.h>
44 #include <machine/resource.h>
45 #include <sys/rman.h>
46
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcivar.h>
49
50 #include <cam/scsi/scsi_all.h>
51
52 #include <dev/dpt/dpt.h>
53
54 #define DPT_VENDOR_ID           0x1044
55 #define DPT_DEVICE_ID           0xa400
56
57 #define DPT_PCI_IOADDR          PCIR_BAR(0)             /* I/O Address */
58 #define DPT_PCI_MEMADDR         PCIR_BAR(1)             /* Mem I/O Address */
59
60 #define ISA_PRIMARY_WD_ADDRESS  0x1f8
61
62 static int      dpt_pci_probe   (device_t);
63 static int      dpt_pci_attach  (device_t);
64
65 static int
66 dpt_pci_probe (device_t dev)
67 {
68         if ((pci_get_vendor(dev) == DPT_VENDOR_ID) &&
69             (pci_get_device(dev) == DPT_DEVICE_ID)) {
70                 device_set_desc(dev, "DPT Caching SCSI RAID Controller");
71                 return (BUS_PROBE_DEFAULT);
72         }
73         return (ENXIO);
74 }
75
76 static int
77 dpt_pci_attach (device_t dev)
78 {
79         dpt_softc_t *   dpt;
80         int             error = 0;
81
82         dpt = device_get_softc(dev);
83         dpt->dev = dev;
84         dpt_alloc(dev);
85
86 #ifdef DPT_ALLOW_MMIO
87         dpt->io_rid = DPT_PCI_MEMADDR;
88         dpt->io_type = SYS_RES_MEMORY;
89         dpt->io_res = bus_alloc_resource_any(dev, dpt->io_type,
90             &dpt->io_rid, RF_ACTIVE);
91 #endif
92         if (dpt->io_res == NULL) {
93                 dpt->io_rid = DPT_PCI_IOADDR;
94                 dpt->io_type = SYS_RES_IOPORT;
95                 dpt->io_res = bus_alloc_resource_any(dev, dpt->io_type,
96                                                      &dpt->io_rid, RF_ACTIVE);
97         }
98
99         if (dpt->io_res == NULL) {
100                 device_printf(dev, "can't allocate register resources\n");
101                 error = ENOMEM;
102                 goto bad;
103         }
104         dpt->io_offset = 0x10;
105
106         dpt->irq_rid = 0;
107         dpt->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &dpt->irq_rid,
108                                               RF_ACTIVE | RF_SHAREABLE);
109         if (dpt->irq_res == NULL) {
110                 device_printf(dev, "No irq?!\n");
111                 error = ENOMEM;
112                 goto bad;
113         }
114
115         /* Ensure busmastering is enabled */
116         pci_enable_busmaster(dev);
117
118         if (rman_get_start(dpt->io_res) == (ISA_PRIMARY_WD_ADDRESS - 0x10)) {
119 #ifdef DPT_DEBUG_WARN
120                 device_printf(dev, "Mapped as an IDE controller.  "
121                                    "Disabling SCSI setup\n");
122 #endif
123                 error = ENXIO;
124                 goto bad;
125         }
126
127         /* Allocate a dmatag representing the capabilities of this attachment */
128         if (bus_dma_tag_create( /* PCI parent */ bus_get_dma_tag(dev),
129                                 /* alignemnt */ 1,
130                                 /* boundary  */ 0,
131                                 /* lowaddr   */ BUS_SPACE_MAXADDR_32BIT,
132                                 /* highaddr  */ BUS_SPACE_MAXADDR,
133                                 /* filter    */ NULL,
134                                 /* filterarg */ NULL,
135                                 /* maxsize   */ BUS_SPACE_MAXSIZE_32BIT,
136                                 /* nsegments */ ~0,
137                                 /* maxsegsz  */ BUS_SPACE_MAXSIZE_32BIT,
138                                 /* flags     */ 0,
139                                 /* lockfunc  */ NULL,
140                                 /* lockarg   */ NULL,
141                                 &dpt->parent_dmat) != 0) {
142                 error = ENXIO;
143                 goto bad;
144         }
145
146         if (dpt_init(dpt) != 0) {
147                 error = ENXIO;
148                 goto bad;
149         }
150
151         /* Register with the XPT */
152         dpt_attach(dpt);
153
154         if (bus_setup_intr(dev, dpt->irq_res, INTR_TYPE_CAM | INTR_ENTROPY |
155             INTR_MPSAFE, NULL, dpt_intr, dpt, &dpt->ih)) {
156                 device_printf(dev, "Unable to register interrupt handler\n");
157                 error = ENXIO;
158                 goto bad;
159         }
160         gone_in_dev(dev, 12, "dpt(4) driver");
161
162         return (error);
163
164 bad:
165         dpt_release_resources(dev);
166
167         dpt_free(dpt);
168
169         return (error);
170 }
171
172 static device_method_t dpt_pci_methods[] = {
173         /* Device interface */
174         DEVMETHOD(device_probe,         dpt_pci_probe),
175         DEVMETHOD(device_attach,        dpt_pci_attach),
176         DEVMETHOD(device_detach,        dpt_detach),
177
178         { 0, 0 }
179 };
180
181 static driver_t dpt_pci_driver = {
182         "dpt",
183         dpt_pci_methods,
184         sizeof(dpt_softc_t),
185 };
186
187 DRIVER_MODULE(dpt, pci, dpt_pci_driver, dpt_devclass, 0, 0);
188 MODULE_DEPEND(dpt, pci, 1, 1, 1);
189 MODULE_DEPEND(dpt, cam, 1, 1, 1);