Merge from vendor branch FILE:
[dragonfly.git] / sys / dev / raid / dpt / dpt_eisa.c
1 /*-
2  * Copyright (c) 1997, 2000 Matthew N. Dodd <winter@jurai.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *      $FreeBSD: src/sys/dev/dpt/dpt_eisa.c,v 1.12.2.1 2000/08/07 18:48:14 peter Exp $
27  *      $DragonFly: src/sys/dev/raid/dpt/dpt_eisa.c,v 1.7 2006/10/25 20:56:01 dillon Exp $
28  */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/rman.h>
36 #include <sys/thread2.h>
37
38 #include <bus/eisa/eisaconf.h>
39
40 #include <bus/cam/scsi/scsi_all.h>
41
42 #include "dpt.h"
43
44 #define DPT_EISA_IOSIZE                 0x100
45 #define DPT_EISA_SLOT_OFFSET            0x0c00
46 #define DPT_EISA_EATA_REG_OFFSET        0x0088
47
48 #define DPT_EISA_DPT2402        0x12142402      /* DPT PM2012A/9X       */
49 #define DPT_EISA_DPTA401        0x1214A401      /* DPT PM2012B/9X       */
50 #define DPT_EISA_DPTA402        0x1214A402      /* DPT PM2012B2/9X      */
51 #define DPT_EISA_DPTA410        0x1214A410      /* DPT PM2x22A/9X       */
52 #define DPT_EISA_DPTA411        0x1214A411      /* DPT Spectre          */
53 #define DPT_EISA_DPTA412        0x1214A412      /* DPT PM2021A/9X       */
54 #define DPT_EISA_DPTA420        0x1214A420      /* DPT Smart Cache IV (PM2042) */
55 #define DPT_EISA_DPTA501        0x1214A501      /* DPT PM2012B1/9X"     */
56 #define DPT_EISA_DPTA502        0x1214A502      /* DPT PM2012Bx/9X      */
57 #define DPT_EISA_DPTA701        0x1214A701      /* DPT PM2011B1/9X      */
58 #define DPT_EISA_DPTBC01        0x1214BC01      /* DPT PM3011/7X ESDI   */ 
59 #define DPT_EISA_DPT8200        0x12148200      /* NEC EATA SCSI        */
60 #define DPT_EISA_DPT2408        0x12142408      /* ATT EATA SCSI        */
61
62 /* Function Prototypes */
63
64 static const char *     dpt_eisa_match  (eisa_id_t);
65 static int              dpt_eisa_probe  (device_t);
66 static int              dpt_eisa_attach (device_t);
67
68
69 static int
70 dpt_eisa_probe (device_t dev)
71 {
72         const char *    desc;
73         u_int32_t       io_base;
74         dpt_conf_t *    conf;
75
76         desc = dpt_eisa_match(eisa_get_id(dev));
77         if (!desc)
78                 return (ENXIO);
79         device_set_desc(dev, desc);
80
81         io_base = (eisa_get_slot(dev) * EISA_SLOT_SIZE) + DPT_EISA_SLOT_OFFSET;
82
83         conf = dpt_pio_get_conf(io_base + DPT_EISA_EATA_REG_OFFSET);
84         if (!conf) {
85                 printf("dpt: dpt_pio_get_conf() failed.\n");
86                 return (ENXIO);
87         }
88
89         eisa_add_iospace(dev, io_base, DPT_EISA_IOSIZE, RESVADDR_NONE);
90         eisa_add_intr(dev, conf->IRQ,
91                       (conf->IRQ_TR ? EISA_TRIGGER_LEVEL : EISA_TRIGGER_EDGE));
92
93         return 0;
94 }
95
96 static int
97 dpt_eisa_attach (device_t dev)
98 {
99         dpt_softc_t * dpt;
100         struct resource *io = 0;
101         struct resource *irq = 0;
102         int             rid;
103         void *          ih;
104         int             error = 0;
105
106         rid = 0;
107         io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
108         if (!io) {
109                 device_printf(dev, "No I/O space?!\n");
110                 error = ENOMEM;
111                 goto bad;
112         }
113
114         rid = 0;
115         irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_ACTIVE);
116         if (!irq) {
117                 device_printf(dev, "No irq?!\n");
118                 error = ENOMEM;
119                 goto bad;
120         }
121
122         dpt = dpt_alloc(dev, rman_get_bustag(io),
123                         rman_get_bushandle(io) + DPT_EISA_EATA_REG_OFFSET);
124         if (dpt == NULL) {
125                 error = ENOMEM;
126                 goto bad;
127         }
128
129         /* Allocate a dmatag representing the capabilities of this attachment */
130         /* XXX Should be a child of the EISA bus dma tag */
131         if (bus_dma_tag_create( /* parent    */ NULL,
132                                 /* alignemnt */ 1,
133                                 /* boundary  */ 0,
134                                 /* lowaddr   */ BUS_SPACE_MAXADDR_32BIT,
135                                 /* highaddr  */ BUS_SPACE_MAXADDR,
136                                 /* filter    */ NULL,
137                                 /* filterarg */ NULL,
138                                 /* maxsize   */ BUS_SPACE_MAXSIZE_32BIT,
139                                 /* nsegments */ BUS_SPACE_UNRESTRICTED,
140                                 /* maxsegsz  */ BUS_SPACE_MAXSIZE_32BIT,
141                                 /* flags     */0,
142                                 &dpt->parent_dmat) != 0) {
143                 dpt_free(dpt);
144                 error = ENXIO;
145                 goto bad;
146         }
147
148         crit_enter();
149
150         if (dpt_init(dpt) != 0) {
151                 dpt_free(dpt);
152                 error = ENXIO;
153                 goto bad;
154         }
155
156         /* Register with the XPT */
157         dpt_attach(dpt);
158
159         crit_exit();
160
161         error = bus_setup_intr(dev, irq, 0, dpt_intr, dpt, &ih, NULL);
162         if (error) {
163                 device_printf(dev, "Unable to register interrupt handler\n");
164                 error = ENXIO;
165                 goto bad;
166         }
167
168         return (error);
169
170  bad:
171         if (io)
172                 bus_release_resource(dev, SYS_RES_IOPORT, 0, io);
173         if (irq)
174                 bus_release_resource(dev, SYS_RES_IRQ, 0, irq);
175
176         return (error);
177 }
178
179 static const char       *
180 dpt_eisa_match(type)
181         eisa_id_t       type;
182 {
183         switch (type) {
184                 case DPT_EISA_DPT2402:
185                 case DPT_EISA_DPTA401:
186                 case DPT_EISA_DPTA402:
187                 case DPT_EISA_DPTA410:
188                 case DPT_EISA_DPTA411:
189                 case DPT_EISA_DPTA412:
190                 case DPT_EISA_DPTA420:
191                 case DPT_EISA_DPTA501:
192                 case DPT_EISA_DPTA502:
193                 case DPT_EISA_DPTA701:
194                 case DPT_EISA_DPTBC01:
195                 case DPT_EISA_DPT8200:
196                 case DPT_EISA_DPT2408:
197                         return ("DPT SCSI Host Bus Adapter");
198                         break;
199                 default:
200                         break;
201         }
202         
203         return (NULL);
204 }
205
206 static device_method_t dpt_eisa_methods[] = {
207         /* Device interface */
208         DEVMETHOD(device_probe,         dpt_eisa_probe),
209         DEVMETHOD(device_attach,        dpt_eisa_attach),
210
211         { 0, 0 }
212 };
213
214 static driver_t dpt_eisa_driver = {
215         "dpt",
216         dpt_eisa_methods,
217         sizeof(dpt_softc_t),
218 };
219
220 static devclass_t dpt_devclass;
221
222 DRIVER_MODULE(dpt, eisa, dpt_eisa_driver, dpt_devclass, 0, 0);