aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/ida/ida_pci.c
blob: 447ee1592d7d4b03515bc7715e84550e736c3834 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*-
 * Copyright (c) 1999,2000 Jonathan Lemon
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>

#include <sys/bio.h>
#include <sys/bus.h>
#include <sys/devicestat.h>
#include <sys/disk.h>

#include <machine/bus_memio.h>
#include <machine/bus_pio.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/rman.h>

#include <pci/pcireg.h>
#include <pci/pcivar.h>

#include <dev/ida/idavar.h>
#include <dev/ida/idareg.h>

#define IDA_PCI_MAX_DMA_ADDR	0xFFFFFFFF
#define IDA_PCI_MAX_DMA_COUNT	0xFFFFFFFF

#define IDA_PCI_MEMADDR		(PCIR_MAPS + 4)		/* Mem I/O Address */

#define IDA_DEVICEID_SMART		0xAE100E11
#define IDA_DEVICEID_DEC_SMART		0x00461011
#define IDA_DEVICEID_NCR_53C1510	0x00101000

static int
ida_v3_fifo_full(struct ida_softc *ida)
{
	return (ida_inl(ida, R_CMD_FIFO) == 0);
}

static void
ida_v3_submit(struct ida_softc *ida, struct ida_qcb *qcb)
{
	ida_outl(ida, R_CMD_FIFO, qcb->hwqcb_busaddr);
}

static bus_addr_t
ida_v3_done(struct ida_softc *ida)
{
	return (ida_inl(ida, R_DONE_FIFO));
}

static int
ida_v3_int_pending(struct ida_softc *ida)
{
	return (ida_inl(ida, R_INT_PENDING));
}

static void
ida_v3_int_enable(struct ida_softc *ida, int enable)
{
	ida_outl(ida, R_INT_MASK, enable ? INT_ENABLE : INT_DISABLE);
}

static int
ida_v4_fifo_full(struct ida_softc *ida)
{
	return (ida_inl(ida, R_42XX_REQUEST) != 0);
}

static void
ida_v4_submit(struct ida_softc *ida, struct ida_qcb *qcb)
{
	ida_outl(ida, R_42XX_REQUEST, qcb->hwqcb_busaddr);
}

static bus_addr_t
ida_v4_done(struct ida_softc *ida)
{
	bus_addr_t completed;

	completed = ida_inl(ida, R_42XX_REPLY);
	if (completed == -1)
		return (0);			/* fifo is empty */
	ida_outl(ida, R_42XX_REPLY, 0);		/* confirm read */
	return (completed);
}

static int
ida_v4_int_pending(struct ida_softc *ida)
{
	return (ida_inl(ida, R_42XX_STATUS) & STATUS_42XX_INT_PENDING);
}

static void
ida_v4_int_enable(struct ida_softc *ida, int enable)
{
	ida_outl(ida, R_42XX_INT_MASK,
	    enable ? INT_ENABLE_42XX : INT_DISABLE_42XX);
}

static struct ida_access ida_v3_access = {
	ida_v3_fifo_full,
	ida_v3_submit,
	ida_v3_done,
	ida_v3_int_pending,
	ida_v3_int_enable,
};

static struct ida_access ida_v4_access = {
	ida_v4_fifo_full,
	ida_v4_submit,
	ida_v4_done,
	ida_v4_int_pending,
	ida_v4_int_enable,
};

static struct ida_board board_id[] = {
	{ 0x4030, "Compaq SMART-2/P array controller",	    &ida_v3_access },
	{ 0x4031, "Compaq SMART-2SL array controller", 	    &ida_v3_access },
	{ 0x4032, "Compaq Smart Array 3200 controller",	    &ida_v3_access },
	{ 0x4033, "Compaq Smart Array 3100ES controller",   &ida_v3_access },
	{ 0x4034, "Compaq Smart Array 221 controller",	    &ida_v3_access },

	{ 0x4040, "Compaq Integrated Array controller",	    &ida_v4_access },
	{ 0x4050, "Compaq Smart Array 4200 controller",	    &ida_v4_access },
	{ 0x4051, "Compaq Smart Array 4250ES controller",   &ida_v4_access },
	{ 0x4058, "Compaq Smart Array 431 controller",      &ida_v4_access },

	{ IDA_DEVICEID_DEC_SMART,
		  "DEC/Compaq Smart Array 4200 controller", &ida_v4_access },
	{ IDA_DEVICEID_NCR_53C1510,
		  "NCR/Compaq Integrated Array controller", &ida_v4_access },

	{ 0, "", 0 },
};

static int ida_pci_probe(device_t dev);
static int ida_pci_attach(device_t dev);

static device_method_t ida_pci_methods[] = {
	DEVMETHOD(device_probe,		ida_pci_probe),
	DEVMETHOD(device_attach,	ida_pci_attach),
	DEVMETHOD(device_detach,	ida_detach),

	DEVMETHOD(bus_print_child,	bus_generic_print_child),

	{ 0, 0 }
};

static driver_t ida_pci_driver = {
	"ida",
	ida_pci_methods,
	sizeof(struct ida_softc)
};

static devclass_t ida_devclass;

static struct ida_board *
ida_pci_match(u_int32_t id)
{
	int i;

	for (i = 0; board_id[i].board; i++)
		if (board_id[i].board == id)
			return (&board_id[i]);
	return (NULL);
}

static int
ida_pci_probe(device_t dev)
{
	struct ida_board *board = NULL;
	u_int32_t id = pci_get_devid(dev);

	if (id == IDA_DEVICEID_SMART)
		board = ida_pci_match(pci_get_subdevice(dev));
	if (id == IDA_DEVICEID_DEC_SMART ||
	    id == IDA_DEVICEID_NCR_53C1510)
		board = ida_pci_match(id);
	if (board != NULL) {
		device_set_desc(dev, board->desc);
		return (0);
	}
	return (ENXIO);
}

static int
ida_pci_attach(device_t dev)
{
	struct ida_board *board;
	struct ida_softc *ida;
	u_int command;
	int error, rid;

	command = pci_read_config(dev, PCIR_COMMAND, 1);

	/*
	 * it appears that this board only does MEMIO access.
	 */
	if ((command & PCIM_CMD_MEMEN) == 0) {
                device_printf(dev, "Only memory mapped I/O is supported\n");
		return (ENXIO);
	}

	ida = (struct ida_softc *)device_get_softc(dev);
	ida->dev = dev;

	board = ida_pci_match(pci_get_subdevice(dev));
	if (board == NULL)
		board = ida_pci_match(pci_get_devid(dev));
	ida->cmd = *board->accessor;

	ida->regs_res_type = SYS_RES_MEMORY;
	ida->regs_res_id = IDA_PCI_MEMADDR;
	ida->regs = bus_alloc_resource(dev, ida->regs_res_type,
	    &ida->regs_res_id, 0, ~0, 1, RF_ACTIVE);
	if (ida->regs == NULL) {
		device_printf(dev, "can't allocate register resources\n");
		return (ENOMEM);
	}

	error = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1,
	    /*boundary*/0, /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
	    /*highaddr*/BUS_SPACE_MAXADDR, /*filter*/NULL, /*filterarg*/NULL,
	    /*maxsize*/MAXBSIZE, /*nsegments*/IDA_NSEG,
	    /*maxsegsize*/BUS_SPACE_MAXSIZE_32BIT, /*flags*/BUS_DMA_ALLOCNOW,
	    &ida->parent_dmat);
	if (error != 0) {
		device_printf(dev, "can't allocate DMA tag\n");
		ida_free(ida);
		return (ENOMEM);
	}

	rid = 0;
        ida->irq_res_type = SYS_RES_IRQ;
	ida->irq = bus_alloc_resource(dev, ida->irq_res_type, &rid,
	    0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
        if (ida->irq == NULL) {
                ida_free(ida);
                return (ENOMEM);
        }
	error = bus_setup_intr(dev, ida->irq, INTR_TYPE_BIO,
	    ida_intr, ida, &ida->ih);
	if (error) {
		device_printf(dev, "can't setup interrupt\n");
		ida_free(ida);
		return (ENOMEM);
	}

	error = ida_init(ida);
	if (error) {
                ida_free(ida);
                return (error);
        }
	ida_attach(ida);
	ida->flags = IDA_ATTACHED; 

	return (0);
}

DRIVER_MODULE(ida, pci, ida_pci_driver, ida_devclass, 0, 0);