Deleted Added
full compact
ata-all.c (56988) ata-all.c (57325)
1/*-
2 * Copyright (c) 1998,1999,2000 S�ren Schmidt
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

--- 11 unchanged lines hidden (view full) ---

20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
1/*-
2 * Copyright (c) 1998,1999,2000 S�ren Schmidt
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

--- 11 unchanged lines hidden (view full) ---

20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $FreeBSD: head/sys/dev/ata/ata-all.c 56988 2000-02-04 10:20:22Z sos $
28 * $FreeBSD: head/sys/dev/ata/ata-all.c 57325 2000-02-18 20:57:33Z sos $
29 */
30
31#include "ata.h"
29 */
30
31#include "ata.h"
32#include "apm.h"
33#include "isa.h"
32#include "isa.h"
33#include "card.h"
34#include "pci.h"
35#include "atadisk.h"
36#include "atapicd.h"
37#include "atapifd.h"
38#include "atapist.h"
39#include "opt_global.h"
40#include "opt_ata.h"
41#include <sys/param.h>

--- 18 unchanged lines hidden (view full) ---

60#endif
61#include <isa/isavar.h>
62#include <isa/isareg.h>
63#include <machine/clock.h>
64#ifdef __i386__
65#include <machine/smp.h>
66#include <i386/isa/intr_machdep.h>
67#endif
34#include "pci.h"
35#include "atadisk.h"
36#include "atapicd.h"
37#include "atapifd.h"
38#include "atapist.h"
39#include "opt_global.h"
40#include "opt_ata.h"
41#include <sys/param.h>

--- 18 unchanged lines hidden (view full) ---

60#endif
61#include <isa/isavar.h>
62#include <isa/isareg.h>
63#include <machine/clock.h>
64#ifdef __i386__
65#include <machine/smp.h>
66#include <i386/isa/intr_machdep.h>
67#endif
68#if NAPM > 0
69#include <machine/apm_bios.h>
68#ifdef __alpha__
69#include <machine/md_var.h>
70#endif
71#include <dev/ata/ata-all.h>
72#include <dev/ata/ata-disk.h>
73#include <dev/ata/atapi-all.h>
74
75/* misc defines */
70#endif
71#include <dev/ata/ata-all.h>
72#include <dev/ata/ata-disk.h>
73#include <dev/ata/atapi-all.h>
74
75/* misc defines */
76#if SMP == 0
77#define isa_apic_irq(x) x
78#endif
79#define IOMASK 0xfffffffc
76#define IOMASK 0xfffffffc
77#define ATA_IOADDR_RID 0
78#define ATA_ALTIOADDR_RID 1
79#define ATA_BMADDR_RID 2
80
81/* prototypes */
80
81/* prototypes */
82static int32_t ata_probe(int32_t, int32_t, int32_t, device_t, int32_t *);
83static void ata_attach(void *);
82static int ata_probe(device_t);
83static int ata_attach(device_t);
84static int ata_detach(device_t);
85static int ata_resume(device_t);
86static void ata_boot_attach(void);
87static void ata_intr(void *);
84static int32_t ata_getparam(struct ata_softc *, int32_t, u_int8_t);
88static int32_t ata_getparam(struct ata_softc *, int32_t, u_int8_t);
85static void ataintr(void *);
86static int8_t *active2str(int32_t);
87static void bswap(int8_t *, int32_t);
88static void btrim(int8_t *, int32_t);
89static void bpack(int8_t *, int8_t *, int32_t);
90
91/* local vars */
89static int8_t *active2str(int32_t);
90static void bswap(int8_t *, int32_t);
91static void btrim(int8_t *, int32_t);
92static void bpack(int8_t *, int8_t *, int32_t);
93
94/* local vars */
92static int32_t atanlun = 2;
93static struct intr_config_hook *ata_attach_hook = NULL;
94static devclass_t ata_devclass;
95static devclass_t ata_devclass;
95struct ata_softc *atadevices[MAXATA];
96static devclass_t ata_pci_devclass;
97static struct intr_config_hook *ata_delayed_attach = NULL;
98static char ata_conf[256];
96MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
97
98#if NISA > 0
99static struct isa_pnp_id ata_ids[] = {
100 {0x0006d041, "Generic ESDI/IDE/ATA controller"}, /* PNP0600 */
101 {0x0106d041, "Plus Hardcard II"}, /* PNP0601 */
102 {0x0206d041, "Plus Hardcard IIXL/EZ"}, /* PNP0602 */
103 {0x0306d041, "Generic ATA"}, /* PNP0603 */
104 {0}
105};
106
107static int
99MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
100
101#if NISA > 0
102static struct isa_pnp_id ata_ids[] = {
103 {0x0006d041, "Generic ESDI/IDE/ATA controller"}, /* PNP0600 */
104 {0x0106d041, "Plus Hardcard II"}, /* PNP0601 */
105 {0x0206d041, "Plus Hardcard IIXL/EZ"}, /* PNP0602 */
106 {0x0306d041, "Generic ATA"}, /* PNP0603 */
107 {0}
108};
109
110static int
108ata_isaprobe(device_t dev)
111ata_isa_probe(device_t dev)
109{
112{
113 struct ata_softc *scp = device_get_softc(dev);
110 struct resource *port;
111 int rid;
114 struct resource *port;
115 int rid;
112 int32_t ctlr, res;
113 int32_t lun;
116 u_long tmp;
114
117
115 /* Check isapnp ids */
118 /* check isapnp ids */
116 if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO)
117 return ENXIO;
118
119 if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO)
120 return ENXIO;
121
119 /* Allocate the port range */
120 rid = 0;
121 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
122 /* allocate the port range */
123 rid = ATA_IOADDR_RID;
124 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
125 ATA_IOSIZE, RF_ACTIVE);
122 if (!port)
123 return ENOMEM;
124
126 if (!port)
127 return ENOMEM;
128
125 /* check if allready in use by a PCI device */
126 for (ctlr = 0; ctlr < atanlun; ctlr++) {
127 if (atadevices[ctlr] && atadevices[ctlr]->ioaddr==rman_get_start(port)){
128 printf("ata-isa%d: already registered as ata%d\n",
129 device_get_unit(dev), ctlr);
130 bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
131 return ENXIO;
132 }
129 /* alloctate the altport range */
130 if (bus_get_resource(dev, SYS_RES_IOPORT, 1, &tmp, &tmp)) {
131 bus_set_resource(dev, SYS_RES_IOPORT, 1,
132 rman_get_start(port) + ATA_ALTPORT,
133 ATA_ALTIOSIZE);
133 }
134 }
135 bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
136 scp->unit = device_get_unit(dev);
137 scp->flags |= ATA_USE_16BIT;
138 return ata_probe(dev);
139}
134
140
135 lun = 0;
136 res = ata_probe(rman_get_start(port), rman_get_start(port) + ATA_ALTPORT,
137 0, dev, &lun);
141static device_method_t ata_isa_methods[] = {
142 /* device interface */
143 DEVMETHOD(device_probe, ata_isa_probe),
144 DEVMETHOD(device_attach, ata_attach),
145 DEVMETHOD(device_resume, ata_resume),
146 { 0, 0 }
147};
138
148
139 bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
149static driver_t ata_isa_driver = {
150 "ata",
151 ata_isa_methods,
152 sizeof(struct ata_softc),
153};
140
154
141 if (res) {
142 isa_set_portsize(dev, res);
143 *(int *)device_get_softc(dev) = lun;
144 atadevices[lun]->flags |= ATA_USE_16BIT;
145 return 0;
146 }
147 return ENXIO;
148}
155DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0);
156#endif
149
157
158#if NCARD > 0
150static int
159static int
151ata_isaattach(device_t dev)
160ata_pccard_probe(device_t dev)
152{
161{
162 struct ata_softc *scp = device_get_softc(dev);
153 struct resource *port;
163 struct resource *port;
154 struct resource *irq;
155 void *ih;
156 int rid;
164 int rid;
165 u_long tmp;
157
166
158 /* Allocate the port range and interrupt */
159 rid = 0;
160 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
167 /* allocate the port range */
168 rid = ATA_IOADDR_RID;
169 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
170 ATA_IOSIZE, RF_ACTIVE);
161 if (!port)
171 if (!port)
162 return (ENOMEM);
172 return ENOMEM;
163
173
164 rid = 0;
165 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_ACTIVE);
166 if (!irq) {
167 bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
168 return (ENOMEM);
174 /* alloctate the altport range */
175 if (bus_get_resource(dev, SYS_RES_IOPORT, 1, &tmp, &tmp)) {
176 bus_set_resource(dev, SYS_RES_IOPORT, 1,
177 rman_get_start(port) + ATA_ALTPORT_PCCARD,
178 ATA_ALTIOSIZE);
169 }
179 }
170 return bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr,
171 atadevices[*(int *)device_get_softc(dev)], &ih);
180 bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
181 scp->unit = device_get_unit(dev);
182 scp->flags |= ATA_USE_16BIT;
183 return ata_probe(dev);
172}
173
184}
185
174static device_method_t ata_isa_methods[] = {
175 /* Device interface */
176 DEVMETHOD(device_probe, ata_isaprobe),
177 DEVMETHOD(device_attach, ata_isaattach),
186static device_method_t ata_pccard_methods[] = {
187 /* device interface */
188 DEVMETHOD(device_probe, ata_pccard_probe),
189 DEVMETHOD(device_attach, ata_attach),
190 DEVMETHOD(device_detach, ata_detach),
191 DEVMETHOD(device_resume, ata_resume),
178 { 0, 0 }
179};
180
192 { 0, 0 }
193};
194
181static driver_t ata_isa_driver = {
195static driver_t ata_pccard_driver = {
182 "ata",
196 "ata",
183 ata_isa_methods,
184 sizeof(int),
197 ata_pccard_methods,
198 sizeof(struct ata_softc),
185};
186
199};
200
187DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0);
201DRIVER_MODULE(ata, pccard, ata_pccard_driver, ata_devclass, 0, 0);
188#endif
189
190#if NPCI > 0
202#endif
203
204#if NPCI > 0
205struct ata_pci_softc {
206 struct resource *bmio;
207 struct resource bmio_1;
208 struct resource bmio_2;
209 struct resource *irq;
210 int32_t irqcnt;
211};
212
213static int32_t
214ata_find_dev(device_t dev, int32_t type)
215{
216 device_t *children, child;
217 int nchildren, i;
218
219 if (device_get_children(device_get_parent(dev), &children, &nchildren))
220 return 0;
221
222 for (i = 0; i < nchildren; i++) {
223 child = children[i];
224
225 /* check that it's on the same silicon and the device we want */
226 if (pci_get_slot(dev) == pci_get_slot(child) &&
227 pci_get_vendor(child) == (type & 0xffff) &&
228 pci_get_device(child) == ((type & 0xffff0000) >> 16)) {
229 free(children, M_TEMP);
230 return 1;
231 }
232 }
233 free(children, M_TEMP);
234 return 0;
235}
236
191static const char *
237static const char *
192ata_pcimatch(device_t dev)
238ata_pci_match(device_t dev)
193{
194 if (pci_get_class(dev) != PCIC_STORAGE)
195 return NULL;
196
197 switch (pci_get_devid(dev)) {
198 /* supported chipsets */
199 case 0x12308086:
200 return "Intel PIIX ATA controller";
201
202 case 0x70108086:
203 return "Intel PIIX3 ATA controller";
204
205 case 0x71118086:
206 case 0x71998086:
239{
240 if (pci_get_class(dev) != PCIC_STORAGE)
241 return NULL;
242
243 switch (pci_get_devid(dev)) {
244 /* supported chipsets */
245 case 0x12308086:
246 return "Intel PIIX ATA controller";
247
248 case 0x70108086:
249 return "Intel PIIX3 ATA controller";
250
251 case 0x71118086:
252 case 0x71998086:
207 return "Intel PIIX4 ATA-33 controller";
253 return "Intel PIIX4 ATA33 controller";
208
209 case 0x24118086:
254
255 case 0x24118086:
210 return "Intel ICH ATA-66 controller";
256 return "Intel ICH ATA66 controller";
211
212 case 0x24218086:
257
258 case 0x24218086:
213 return "Intel ICH0 ATA-33 controller";
259 return "Intel ICH0 ATA33 controller";
214
215 case 0x522910b9:
260
261 case 0x522910b9:
216 return "AcerLabs Aladdin ATA-33 controller";
262 return "AcerLabs Aladdin ATA33 controller";
217
263
218 case 0x05711106: /* 82c586 & 82c686 */
264 case 0x05711106:
219 if (ata_find_dev(dev, 0x05861106))
265 if (ata_find_dev(dev, 0x05861106))
220 return "VIA 82C586 ATA-33 controller";
266 return "VIA 82C586 ATA33 controller";
221 if (ata_find_dev(dev, 0x05961106))
267 if (ata_find_dev(dev, 0x05961106))
222 return "VIA 82C596 ATA-33 controller";
268 return "VIA 82C596 ATA33 controller";
223 if (ata_find_dev(dev, 0x06861106))
269 if (ata_find_dev(dev, 0x06861106))
224 return "VIA 82C686 ATA-66 controller";
270 return "VIA 82C686 ATA66 controller";
225 return "VIA Apollo ATA controller";
226
227 case 0x55131039:
271 return "VIA Apollo ATA controller";
272
273 case 0x55131039:
228 return "SiS 5591 ATA-33 controller";
274 return "SiS 5591 ATA33 controller";
229
275
276 case 0x06461095:
277 return "CMD 646 ATA controller";
278
230 case 0x74091022:
279 case 0x74091022:
231 return "AMD 756 ATA-66 controller";
280 return "AMD 756 ATA66 controller";
232
233 case 0x4d33105a:
281
282 case 0x4d33105a:
234 return "Promise ATA-33 controller";
283 return "Promise ATA33 controller";
235
236 case 0x4d38105a:
284
285 case 0x4d38105a:
237 return "Promise ATA-66 controller";
286 return "Promise ATA66 controller";
238
239 case 0x00041103:
287
288 case 0x00041103:
240 return "HighPoint HPT366 ATA-66 controller";
289 return "HighPoint HPT366 ATA66 controller";
241
242 /* unsupported but known chipsets, generic DMA only */
290
291 /* unsupported but known chipsets, generic DMA only */
292 case 0x10001042:
293 case 0x10011042:
294 return "RZ 100? ATA controller !WARNING! buggy chip data loss possible";
295
243 case 0x06401095:
296 case 0x06401095:
244 return "CMD 640 ATA controller (generic mode)";
297 return "CMD 640 ATA controller !WARNING! buggy chip data loss possible";
245
298
246 case 0x06461095:
247 return "CMD 646 ATA controller (generic mode)";
248
249 case 0xc6931080:
299 case 0xc6931080:
250 return "Cypress 82C693 ATA controller (generic mode)";
300 if (pci_get_subclass(dev) == PCIS_STORAGE_IDE)
301 return "Cypress 82C693 ATA controller (generic mode)";
302 break;
251
252 case 0x01021078:
253 return "Cyrix 5530 ATA controller (generic mode)";
254
303
304 case 0x01021078:
305 return "Cyrix 5530 ATA controller (generic mode)";
306
307 /* unknown chipsets, try generic DMA if it seems possible */
255 default:
256 if (pci_get_class(dev) == PCIC_STORAGE &&
257 (pci_get_subclass(dev) == PCIS_STORAGE_IDE))
258 return "Unknown PCI ATA controller (generic mode)";
259 }
260 return NULL;
261}
262
263static int
308 default:
309 if (pci_get_class(dev) == PCIC_STORAGE &&
310 (pci_get_subclass(dev) == PCIS_STORAGE_IDE))
311 return "Unknown PCI ATA controller (generic mode)";
312 }
313 return NULL;
314}
315
316static int
264ata_pciprobe(device_t dev)
317ata_pci_probe(device_t dev)
265{
318{
266 const char *desc = ata_pcimatch(dev);
319 const char *desc = ata_pci_match(dev);
267
268 if (desc) {
269 device_set_desc(dev, desc);
270 return 0;
271 }
272 else
273 return ENXIO;
274}
275
276static int
320
321 if (desc) {
322 device_set_desc(dev, desc);
323 return 0;
324 }
325 else
326 return ENXIO;
327}
328
329static int
277ata_pciattach(device_t dev)
330ata_pci_add_child(device_t dev, int unit)
278{
331{
279 int unit = device_get_unit(dev);
280 struct ata_softc *scp;
281 u_int32_t type;
332 device_t child;
333 int lun;
334
335 /* check if this is located at one of the std addresses */
336 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV)
337 lun = unit;
338 else
339 lun = -1;
340
341 if (!(child = device_add_child(dev, "ata", lun)))
342 return ENOMEM;
343
344 device_set_ivars(child, (void *)(uintptr_t) unit);
345 return 0;
346}
347
348static int
349ata_pci_attach(device_t dev)
350{
351 struct ata_pci_softc *sc = device_get_softc(dev);
282 u_int8_t class, subclass;
352 u_int8_t class, subclass;
283 u_int32_t cmd;
284 int32_t iobase_1, iobase_2, altiobase_1, altiobase_2;
285 int32_t bmaddr_1 = 0, bmaddr_2 = 0, irq1, irq2;
286 struct resource *irq = NULL;
287 int32_t lun;
353 u_int32_t type, cmd;
354 int rid;
288
289 /* set up vendor-specific stuff */
290 type = pci_get_devid(dev);
291 class = pci_get_class(dev);
292 subclass = pci_get_subclass(dev);
293 cmd = pci_read_config(dev, PCIR_COMMAND, 4);
294
355
356 /* set up vendor-specific stuff */
357 type = pci_get_devid(dev);
358 class = pci_get_class(dev);
359 subclass = pci_get_subclass(dev);
360 cmd = pci_read_config(dev, PCIR_COMMAND, 4);
361
295#ifdef ATA_DEBUG
296 printf("ata-pci%d: type=%08x class=%02x subclass=%02x cmd=%08x if=%02x\n",
297 unit, type, class, subclass, cmd, pci_get_progif(dev));
298#endif
299
300 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
301 iobase_1 = IO_WD1;
302 altiobase_1 = iobase_1 + ATA_ALTPORT;
303 irq1 = 14;
304 }
305 else {
306 iobase_1 = pci_read_config(dev, 0x10, 4) & IOMASK;
307 altiobase_1 = pci_read_config(dev, 0x14, 4) & IOMASK;
308 irq1 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff;
309 /* this is needed for old non-std systems */
310 if (iobase_1 == IO_WD1 && irq1 == 0x00)
311 irq1 = 14;
312 }
313
314 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
315 iobase_2 = IO_WD2;
316 altiobase_2 = iobase_2 + ATA_ALTPORT;
317 irq2 = 15;
318 }
319 else {
320 iobase_2 = pci_read_config(dev, 0x18, 4) & IOMASK;
321 altiobase_2 = pci_read_config(dev, 0x1c, 4) & IOMASK;
322 irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff;
323 /* this is needed for old non-std systems */
324 if (iobase_2 == IO_WD2 && irq2 == 0x00)
325 irq2 = 15;
326 }
327
328 /* is this controller busmaster DMA capable ? */
329 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
330 /* is busmastering support turned on ? */
331 if ((cmd & (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) ==
332 (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) {
362 /* is this controller busmaster DMA capable ? */
363 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
364 /* is busmastering support turned on ? */
365 if ((cmd & (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) ==
366 (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) {
367
333 /* is there a valid port range to connect to ? */
368 /* is there a valid port range to connect to ? */
334 if ((bmaddr_1 = pci_read_config(dev, 0x20, 4) & IOMASK))
335 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1;
336 else
337 printf("ata-pci%d: Busmastering DMA not configured\n", unit);
369 rid = 0x20;
370 sc->bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
371 0, ~0, 1, RF_ACTIVE);
372 if (!sc->bmio)
373 device_printf(dev, "Busmastering DMA not configured\n");
338 }
339 else
374 }
375 else
340 printf("ata-pci%d: Busmastering DMA not enabled\n", unit);
376 device_printf(dev, "Busmastering DMA not enabled\n");
341 }
342 else {
343 if (type == 0x4d33105a || type == 0x4d38105a || type == 0x00041103) {
344 /* Promise and HPT366 controllers support busmastering DMA */
377 }
378 else {
379 if (type == 0x4d33105a || type == 0x4d38105a || type == 0x00041103) {
380 /* Promise and HPT366 controllers support busmastering DMA */
345 bmaddr_1 = pci_read_config(dev, 0x20, 4) & IOMASK;
346 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1;
381 rid = 0x20;
382 sc->bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
383 0, ~0, 1, RF_ACTIVE);
347 }
348 else
349 /* we dont know this controller, no busmastering DMA */
384 }
385 else
386 /* we dont know this controller, no busmastering DMA */
350 printf("ata-pci%d: Busmastering DMA not supported\n", unit);
387 device_printf(dev, "Busmastering DMA not supported\n");
351 }
352
353 /* do extra chipset specific setups */
354 switch (type) {
355 case 0x522910b9: /* Aladdin need to activate the ATAPI FIFO */
356 pci_write_config(dev, 0x53,
357 (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1);
358 break;
388 }
389
390 /* do extra chipset specific setups */
391 switch (type) {
392 case 0x522910b9: /* Aladdin need to activate the ATAPI FIFO */
393 pci_write_config(dev, 0x53,
394 (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1);
395 break;
396
359 case 0x4d38105a: /* Promise 66's need their clock changed */
397 case 0x4d38105a: /* Promise 66's need their clock changed */
360 outb(bmaddr_1 + 0x11, inb(bmaddr_1 + 0x11) | 0x0a);
398 outb(rman_get_start(sc->bmio) + 0x11,
399 inb(rman_get_start(sc->bmio) + 0x11) | 0x0a);
361 /* FALLTHROUGH */
362
363 case 0x4d33105a: /* Promise's need burst mode to be turned on */
400 /* FALLTHROUGH */
401
402 case 0x4d33105a: /* Promise's need burst mode to be turned on */
364 outb(bmaddr_1 + 0x1f, inb(bmaddr_1 + 0x1f) | 0x01);
403 outb(rman_get_start(sc->bmio) + 0x1f,
404 inb(rman_get_start(sc->bmio) + 0x1f) | 0x01);
365 break;
366
367 case 0x00041103: /* HPT366 turn of fast interrupt prediction */
368 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
369 break;
370
371 case 0x05711106:
372 case 0x74091022: /* VIA 82C586, 82C596, 82C686 & AMD 756 default setup */

--- 10 unchanged lines hidden (view full) ---

383 /* set DMA read & end-of-sector fifo flush */
384 pci_write_config(dev, 0x46,
385 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
386
387 /* set sector size */
388 pci_write_config(dev, 0x60, DEV_BSIZE, 2);
389 pci_write_config(dev, 0x68, DEV_BSIZE, 2);
390
405 break;
406
407 case 0x00041103: /* HPT366 turn of fast interrupt prediction */
408 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
409 break;
410
411 case 0x05711106:
412 case 0x74091022: /* VIA 82C586, 82C596, 82C686 & AMD 756 default setup */

--- 10 unchanged lines hidden (view full) ---

423 /* set DMA read & end-of-sector fifo flush */
424 pci_write_config(dev, 0x46,
425 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
426
427 /* set sector size */
428 pci_write_config(dev, 0x60, DEV_BSIZE, 2);
429 pci_write_config(dev, 0x68, DEV_BSIZE, 2);
430
391 /* set the chiptype to the hostchip ID, makes life easier */
392 if (ata_find_dev(dev, 0x05861106))
393 type = 0x05861106;
394 if (ata_find_dev(dev, 0x05961106))
395 type = 0x05961106;
431 /* prepare for ATA-66 on the 82C686 */
396 if (ata_find_dev(dev, 0x06861106)) {
432 if (ata_find_dev(dev, 0x06861106)) {
397 type = 0x06861106;
398 /* prepare for ATA-66 on the 82C686 */
399 pci_write_config(dev, 0x50,
400 pci_read_config(dev, 0x50, 4) | 0x070f070f, 4);
401 }
402 break;
403 }
433 pci_write_config(dev, 0x50,
434 pci_read_config(dev, 0x50, 4) | 0x070f070f, 4);
435 }
436 break;
437 }
404
405 /* now probe the addresse found for "real" ATA/ATAPI hardware */
406 lun = 0;
407 if (iobase_1 && ata_probe(iobase_1, altiobase_1, bmaddr_1, dev, &lun)) {
408 int rid;
409 void *ih;
410
438
411 scp = atadevices[lun];
412 scp->chiptype = type;
413 rid = 0;
414 if (iobase_1 == IO_WD1) {
415#ifdef __alpha__
416 alpha_platform_setup_ide_intr(0, ataintr, scp);
417#else
418 bus_set_resource(dev, SYS_RES_IRQ, rid, irq1, 1);
419 if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
420 RF_SHAREABLE | RF_ACTIVE)))
421 printf("ata_pciattach: Unable to alloc interrupt\n");
422#endif
423 } else {
424 if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
425 RF_SHAREABLE | RF_ACTIVE)))
426 printf("ata_pciattach: Unable to alloc interrupt\n");
439 ata_pci_add_child(dev, 0);
440
441 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV ||
442 pci_read_config(dev, 0x18, 4) & IOMASK)
443 ata_pci_add_child(dev, 1);
444
445 return bus_generic_attach(dev);
446}
447
448static int
449ata_pci_print_child(device_t dev, device_t child)
450{
451 struct ata_softc *scp = device_get_softc(child);
452 int unit = (uintptr_t) device_get_ivars(child);
453 int retval = 0;
454
455 retval += bus_print_child_header(dev, child);
456 retval += printf(": at 0x%x", scp->ioaddr);
457
458 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV)
459 retval += printf(" irq %d", 14 + unit);
460
461 retval += bus_print_child_footer(dev, child);
462
463 return retval;
464}
465
466static struct resource *
467ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
468 u_long start, u_long end, u_long count, u_int flags)
469{
470 struct ata_pci_softc *sc = device_get_softc(dev);
471 int masterdev = pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV;
472 int unit = (int)device_get_ivars(child);
473 int myrid;
474
475 if (type == SYS_RES_IOPORT) {
476 switch (*rid) {
477 case ATA_IOADDR_RID:
478 if (masterdev) {
479 myrid = 0;
480 start = (unit == 0 ? IO_WD1 : IO_WD2);
481 end = start + ATA_IOSIZE - 1;
482 count = ATA_IOSIZE;
483 }
484 else
485 myrid = 0x10 + 8 * unit;
486 break;
487
488 case ATA_ALTIOADDR_RID:
489 if (masterdev) {
490 myrid = 0;
491 start = (unit == 0 ? IO_WD1 : IO_WD2) + ATA_ALTPORT;
492 end = start + ATA_ALTIOSIZE - 1;
493 count = ATA_ALTIOSIZE;
494 }
495 else
496 myrid = 0x14 + 8 * unit;
497 break;
498
499 case ATA_BMADDR_RID:
500 /* the busmaster resource is shared between the two channels */
501 if (sc->bmio) {
502 if (unit == 0) {
503 sc->bmio_1 = *sc->bmio;
504 sc->bmio_1.r_end = sc->bmio->r_start + ATA_BM_OFFSET1;
505 return &sc->bmio_1;
506 } else {
507 sc->bmio_2 = *sc->bmio;
508 sc->bmio_2.r_start = sc->bmio->r_start + ATA_BM_OFFSET1;
509 sc->bmio_2.r_end = sc->bmio_2.r_start + ATA_BM_OFFSET1;
510 return &sc->bmio_2;
511 }
512 }
513 break;
514
515 default:
516 return 0;
427 }
517 }
428 if (irq)
429 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih);
430 printf("ata%d at 0x%04x irq %d on ata-pci%d\n",
431 lun, iobase_1, isa_apic_irq(irq1), unit);
518
519 if (masterdev)
520 /* make the parent just pass through the allocation. */
521 return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
522 SYS_RES_IOPORT, &myrid,
523 start, end, count, flags);
524 else
525 /* we are using the parent resource directly. */
526 return BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
527 SYS_RES_IOPORT, &myrid,
528 start, end, count, flags);
432 }
529 }
433 lun = 1;
434 if (iobase_2 && ata_probe(iobase_2, altiobase_2, bmaddr_2, dev, &lun)) {
435 int rid;
436 void *ih;
437
530
438 scp = atadevices[lun];
439 scp->chiptype = type;
440 if (iobase_2 == IO_WD2) {
441#ifdef __alpha__
442 alpha_platform_setup_ide_intr(1, ataintr, scp);
531 if (type == SYS_RES_IRQ) {
532 if (*rid != 0)
533 return 0;
534
535 if (masterdev) {
536#ifdef __i386__
537 int irq = (unit == 0 ? 14 : 15);
538
539 return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
540 SYS_RES_IRQ, rid,
541 irq, irq, 1, flags & ~RF_SHAREABLE);
443#else
542#else
444 rid = 1;
445 bus_set_resource(dev, SYS_RES_IRQ, rid, irq2, 1);
446 if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
447 RF_SHAREABLE | RF_ACTIVE)))
448 printf("ata_pciattach: Unable to alloc interrupt\n");
543 return alpha_platform_alloc_ide_intr(unit);
449#endif
450 } else {
544#endif
545 } else {
451 rid = 0;
452 if (irq1 != irq2 || irq == NULL) {
453 if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
454 RF_SHAREABLE | RF_ACTIVE)))
455 printf("ata_pciattach: Unable to alloc interrupt\n");
456 }
546 /* primary and secondary channels share the same interrupt */
547 sc->irqcnt++;
548 if (!sc->irq)
549 sc->irq = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
550 SYS_RES_IRQ, rid, 0, ~0, 1, flags);
551 return sc->irq;
457 }
552 }
458 if (irq)
459 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih);
460 printf("ata%d at 0x%04x irq %d on ata-pci%d\n",
461 lun, iobase_2, isa_apic_irq(irq2), unit);
462 }
463 return 0;
464}
465
553 }
554 return 0;
555}
556
466int32_t
467ata_find_dev(device_t dev, int32_t type)
557static int
558ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
559 struct resource *r)
468{
560{
469 device_t *children, child;
470 int nchildren, i;
561 struct ata_pci_softc *sc = device_get_softc(dev);
562 int unit = (uintptr_t) device_get_ivars(child);
563 int masterdev = pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV;
564 int myrid = 0;
471
565
472 if (device_get_children(device_get_parent(dev), &children, &nchildren))
473 return 0;
566 if (type == SYS_RES_IOPORT) {
567 switch (rid) {
568 case ATA_IOADDR_RID:
569 if (masterdev)
570 myrid = 0;
571 else
572 myrid = 0x10 + 8 * unit;
573 break;
474
574
475 for (i = 0; i < nchildren; i++) {
476 child = children[i];
575 case ATA_ALTIOADDR_RID:
576 if (masterdev)
577 myrid = 0;
578 else
579 myrid = 0x14 + 8 * unit;
580 break;
477
581
478 /* check that it's on the same silicon and the device we want */
479 if (pci_get_slot(dev) == pci_get_slot(child) &&
480 pci_get_vendor(child) == (type & 0xffff) &&
481 pci_get_device(child) == ((type & 0xffff0000)>>16)) {
482 free(children, M_TEMP);
483 return 1;
582 case ATA_BMADDR_RID:
583 return 0;
584
585 default:
586 return ENOENT;
484 }
587 }
588
589 if (masterdev)
590 /* make the parent just pass through the allocation. */
591 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
592 SYS_RES_IOPORT, myrid, r);
593 else
594 /* we are using the parent resource directly. */
595 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
596 SYS_RES_IOPORT, myrid, r);
485 }
597 }
486 free(children, M_TEMP);
487 return 0;
598 if (type == SYS_RES_IRQ) {
599 if (rid != 0)
600 return ENOENT;
601
602 if (masterdev) {
603#ifdef __i386__
604 return BUS_RELEASE_RESOURCE(device_get_parent(dev),
605 child, SYS_RES_IRQ, rid, r);
606#else
607 return alpha_platform_release_ide_intr(unit, r);
608#endif
609 }
610 else {
611 if (--sc->irqcnt)
612 return 0;
613
614 return BUS_RELEASE_RESOURCE(device_get_parent(dev),
615 dev, SYS_RES_IRQ, rid, r);
616 }
617 }
618 return EINVAL;
488}
489
619}
620
621static int
622ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
623 int flags, driver_intr_t *intr, void *arg,
624 void **cookiep)
625{
626 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
627#ifdef __i386__
628 return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
629 flags, intr, arg, cookiep);
630#else
631 return alpha_platform_setup_ide_intr(irq, intr, arg, cookiep);
632#endif
633 }
634 else
635 return BUS_SETUP_INTR(device_get_parent(dev), dev, irq,
636 flags, intr, arg, cookiep);
637}
638
639static int
640ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
641 void *cookie)
642{
643 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
644#ifdef __i386__
645 return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
646#else
647 return alpha_platform_teardown_ide_intr(irq, cookie);
648#endif
649 }
650 else
651 return BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie);
652}
653
490static device_method_t ata_pci_methods[] = {
654static device_method_t ata_pci_methods[] = {
491 /* Device interface */
492 DEVMETHOD(device_probe, ata_pciprobe),
493 DEVMETHOD(device_attach, ata_pciattach),
655 /* device interface */
656 DEVMETHOD(device_probe, ata_pci_probe),
657 DEVMETHOD(device_attach, ata_pci_attach),
658 DEVMETHOD(device_shutdown, bus_generic_shutdown),
659 DEVMETHOD(device_suspend, bus_generic_suspend),
660 DEVMETHOD(device_resume, bus_generic_resume),
661
662 /* bus methods */
663 DEVMETHOD(bus_print_child, ata_pci_print_child),
664 DEVMETHOD(bus_alloc_resource, ata_pci_alloc_resource),
665 DEVMETHOD(bus_release_resource, ata_pci_release_resource),
666 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
667 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
668 DEVMETHOD(bus_setup_intr, ata_pci_setup_intr),
669 DEVMETHOD(bus_teardown_intr, ata_pci_teardown_intr),
494 { 0, 0 }
495};
496
497static driver_t ata_pci_driver = {
670 { 0, 0 }
671};
672
673static driver_t ata_pci_driver = {
498 "ata-pci",
674 "atapci",
499 ata_pci_methods,
675 ata_pci_methods,
500 sizeof(int),
676 sizeof(struct ata_pci_softc),
501};
502
677};
678
503DRIVER_MODULE(ata, pci, ata_pci_driver, ata_devclass, 0, 0);
679DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_devclass, 0, 0);
680
681static int
682ata_pcisub_probe(device_t dev)
683{
684 struct ata_softc *scp = device_get_softc(dev);
685
686 /* kids of pci ata chipsets has their physical unit number in ivars */
687 scp->unit = (uintptr_t) device_get_ivars(dev);
688
689 /* set the chiptype to the hostchip ID, makes life easier */
690 if (ata_find_dev(device_get_parent(dev), 0x05861106))
691 scp->chiptype = 0x05861106;
692 else if (ata_find_dev(device_get_parent(dev), 0x05961106))
693 scp->chiptype = 0x05961106;
694 else if (ata_find_dev(device_get_parent(dev), 0x06861106))
695 scp->chiptype = 0x06861106;
696 else
697 scp->chiptype = pci_get_devid(device_get_parent(dev));
698 return ata_probe(dev);
699}
700
701static device_method_t ata_pcisub_methods[] = {
702 /* device interface */
703 DEVMETHOD(device_probe, ata_pcisub_probe),
704 DEVMETHOD(device_attach, ata_attach),
705 DEVMETHOD(device_detach, ata_detach),
706 DEVMETHOD(device_resume, ata_resume),
707 { 0, 0 }
708};
709
710static driver_t ata_pcisub_driver = {
711 "ata",
712 ata_pcisub_methods,
713 sizeof(struct ata_softc),
714};
715
716DRIVER_MODULE(ata, atapci, ata_pcisub_driver, ata_pci_devclass, 0, 0);
504#endif
505
717#endif
718
506static int32_t
507ata_probe(int32_t ioaddr, int32_t altioaddr, int32_t bmaddr,
508 device_t dev, int32_t *unit)
719static int
720ata_probe(device_t dev)
509{
721{
510 struct ata_softc *scp;
511 int32_t lun, mask = 0;
722 struct ata_softc *scp = device_get_softc(dev);
723 struct resource *io = 0;
724 struct resource *altio = 0;
725 struct resource *bmio = 0;
726 int rid;
727 int32_t ioaddr, altioaddr, bmaddr;
728 int32_t mask = 0;
512 u_int8_t status0, status1;
513
729 u_int8_t status0, status1;
730
514 if (atanlun > MAXATA) {
515 printf("ata: unit out of range(%d)\n", atanlun);
516 return 0;
517 }
731 if (!scp || scp->flags & ATA_ATTACHED)
732 return ENXIO;
518
733
519 /* check if this is located at one of the std addresses */
520 if (ioaddr == IO_WD1)
521 lun = 0;
522 else if (ioaddr == IO_WD2)
523 lun = 1;
524 else
525 lun = atanlun++;
734 /* initialize the softc basics */
735 scp->active = ATA_IDLE;
736 scp->dev = dev;
737 scp->devices = 0;
526
738
527 if ((scp = atadevices[lun])) {
528 ata_printf(scp, -1, "unit already attached\n");
529 return 0;
530 }
531 scp = malloc(sizeof(struct ata_softc), M_ATA, M_NOWAIT);
532 if (scp == NULL) {
533 ata_printf(scp, -1, "failed to allocate driver storage\n");
534 return 0;
535 }
536 bzero(scp, sizeof(struct ata_softc));
739 rid = ATA_IOADDR_RID;
740 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
741 if (!io)
742 goto failure;
743 ioaddr = rman_get_start(io);
537
744
745 rid = ATA_ALTIOADDR_RID;
746 altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
747 if (!altio)
748 goto failure;
749 altioaddr = rman_get_start(altio);
750
751 rid = ATA_BMADDR_RID;
752 bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
753 bmaddr = bmio ? rman_get_start(bmio) : 0;
754
755 /* store the IO resources for eventual later release */
756 scp->r_io = io;
757 scp->r_altio = altio;
758 scp->r_bmio = bmio;
759
760 /* store the physical IO addresse for easy access */
538 scp->ioaddr = ioaddr;
539 scp->altioaddr = altioaddr;
540 scp->bmaddr = bmaddr;
761 scp->ioaddr = ioaddr;
762 scp->altioaddr = altioaddr;
763 scp->bmaddr = bmaddr;
541 scp->lun = lun;
542 scp->unit = *unit;
543 scp->active = ATA_IDLE;
544
545 if (bootverbose)
546 ata_printf(scp, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
547 scp->ioaddr, scp->altioaddr, scp->bmaddr);
548
549 /* do we have any signs of ATA/ATAPI HW being present ? */
550 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
551 DELAY(1);
552 status0 = inb(scp->ioaddr + ATA_STATUS);
553 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
554 DELAY(1);
555 status1 = inb(scp->ioaddr + ATA_STATUS);
764
765 if (bootverbose)
766 ata_printf(scp, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
767 scp->ioaddr, scp->altioaddr, scp->bmaddr);
768
769 /* do we have any signs of ATA/ATAPI HW being present ? */
770 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
771 DELAY(1);
772 status0 = inb(scp->ioaddr + ATA_STATUS);
773 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
774 DELAY(1);
775 status1 = inb(scp->ioaddr + ATA_STATUS);
556 if ((status0 & 0xf8) != 0xf8)
776 if ((status0 & 0xf8) != 0xf8 && status0 != 0xa5)
557 mask |= 0x01;
777 mask |= 0x01;
558 if ((status1 & 0xf8) != 0xf8)
778 if ((status1 & 0xf8) != 0xf8 && status1 != 0xa5)
559 mask |= 0x02;
560 if (bootverbose)
561 ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n",
562 mask, status0, status1);
779 mask |= 0x02;
780 if (bootverbose)
781 ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n",
782 mask, status0, status1);
563 if (!mask) {
564 free(scp, M_DEVBUF);
565 return 0;
566 }
783 if (!mask)
784 goto failure;
785
567 ata_reset(scp, &mask);
786 ata_reset(scp, &mask);
568 if (!mask) {
569 free(scp, M_DEVBUF);
570 return 0;
571 }
787 if (!mask)
788 goto failure;
789
572 /*
790 /*
573 * OK, we have at least one device on the chain,
574 * check for ATAPI signatures, if none check if its
575 * a good old ATA device.
791 * OK, we have at least one device on the chain, check for ATAPI
792 * signatures, if none check if its a good old ATA device.
576 */
577 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER));
578 DELAY(1);
579 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB &&
580 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) {
581 scp->devices |= ATA_ATAPI_MASTER;
582 }
583 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE));

--- 20 unchanged lines hidden (view full) ---

604 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 &&
605 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) {
606 scp->devices |= ATA_ATA_SLAVE;
607 }
608 }
609 if (bootverbose)
610 ata_printf(scp, -1, "devices = 0x%x\n", scp->devices);
611 if (!scp->devices) {
793 */
794 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER));
795 DELAY(1);
796 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB &&
797 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) {
798 scp->devices |= ATA_ATAPI_MASTER;
799 }
800 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE));

--- 20 unchanged lines hidden (view full) ---

821 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 &&
822 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) {
823 scp->devices |= ATA_ATA_SLAVE;
824 }
825 }
826 if (bootverbose)
827 ata_printf(scp, -1, "devices = 0x%x\n", scp->devices);
828 if (!scp->devices) {
612 free(scp, M_DEVBUF);
613 return 0;
829 goto failure;
614 }
615 TAILQ_INIT(&scp->ata_queue);
616 TAILQ_INIT(&scp->atapi_queue);
830 }
831 TAILQ_INIT(&scp->ata_queue);
832 TAILQ_INIT(&scp->atapi_queue);
617 *unit = scp->lun;
618 scp->dev = dev;
619 atadevices[scp->lun] = scp;
620
621 /* register callback for when interrupts are enabled */
622 if (!ata_attach_hook) {
623 if (!(ata_attach_hook = (struct intr_config_hook *)
624 malloc(sizeof(struct intr_config_hook),
625 M_TEMP, M_NOWAIT))) {
626 ata_printf(scp, -1, "ERROR malloc attach_hook failed\n");
627 return 0;
628 }
629 bzero(ata_attach_hook, sizeof(struct intr_config_hook));
630
631 ata_attach_hook->ich_func = ata_attach;
632 if (config_intrhook_establish(ata_attach_hook) != 0) {
633 ata_printf(scp, -1, "config_intrhook_establish failed\n");
634 free(ata_attach_hook, M_TEMP);
635 }
636 }
637#if NAPM > 0
638 scp->resume_hook.ah_fun = (void *)ata_reinit;
639 scp->resume_hook.ah_arg = scp;
640 scp->resume_hook.ah_name = "ATA driver";
641 scp->resume_hook.ah_order = APM_MID_ORDER;
642 apm_hook_establish(APM_HOOK_RESUME, &scp->resume_hook);
643#endif
644 return ATA_IOSIZE;
833 return 0;
834
835failure:
836 if (io)
837 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
838 if (altio)
839 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTIOADDR_RID, altio);
840 if (bmio)
841 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, bmio);
842 if (bootverbose)
843 ata_printf(scp, -1, "probe allocation failed\n");
844 return ENXIO;
645}
646
845}
846
647void
648ata_attach(void *dummy)
847static int
848ata_attach(device_t dev)
649{
849{
650 int32_t ctlr;
850 struct ata_softc *scp = device_get_softc(dev);
851 int rid = 0;
852 void *ih;
651
853
854 if (!scp || scp->flags & ATA_ATTACHED)
855 return ENXIO;
856
857 scp->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
858 RF_SHAREABLE | RF_ACTIVE);
859 if (!scp->r_irq) {
860 ata_printf(scp, -1, "unable to allocate interrupt\n");
861 return ENXIO;
862 }
863 bus_setup_intr(dev, scp->r_irq, INTR_TYPE_BIO, ata_intr, scp, &ih);
864
652 /*
865 /*
653 * run through atadevices[] and look for real ATA & ATAPI devices
654 * using the hints we found in the early probe to avoid probing
655 * of non-exsistent devices and thereby long delays
866 * do not attach devices if we are in early boot, this is done later
867 * when interrupts are enabled by a hook into the boot process.
868 * otherwise attach what the probe has found in scp->devices.
656 */
869 */
657 for (ctlr=0; ctlr<MAXATA; ctlr++) {
658 if (!atadevices[ctlr]) continue;
659 if (atadevices[ctlr]->devices & ATA_ATA_SLAVE)
660 if (ata_getparam(atadevices[ctlr], ATA_SLAVE, ATA_C_ATA_IDENTIFY))
661 atadevices[ctlr]->devices &= ~ATA_ATA_SLAVE;
662 if (atadevices[ctlr]->devices & ATA_ATAPI_SLAVE)
663 if (ata_getparam(atadevices[ctlr], ATA_SLAVE, ATA_C_ATAPI_IDENTIFY))
664 atadevices[ctlr]->devices &= ~ATA_ATAPI_SLAVE;
665 if (atadevices[ctlr]->devices & ATA_ATA_MASTER)
666 if (ata_getparam(atadevices[ctlr], ATA_MASTER, ATA_C_ATA_IDENTIFY))
667 atadevices[ctlr]->devices &= ~ATA_ATA_MASTER;
668 if (atadevices[ctlr]->devices & ATA_ATAPI_MASTER)
669 if (ata_getparam(atadevices[ctlr], ATA_MASTER,ATA_C_ATAPI_IDENTIFY))
670 atadevices[ctlr]->devices &= ~ATA_ATAPI_MASTER;
870 if (!ata_delayed_attach) {
871 if (scp->devices & ATA_ATA_SLAVE)
872 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY))
873 scp->devices &= ~ATA_ATA_SLAVE;
874 if (scp->devices & ATA_ATAPI_SLAVE)
875 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY))
876 scp->devices &= ~ATA_ATAPI_SLAVE;
877 if (scp->devices & ATA_ATA_MASTER)
878 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY))
879 scp->devices &= ~ATA_ATA_MASTER;
880 if (scp->devices & ATA_ATAPI_MASTER)
881 if (ata_getparam(scp, ATA_MASTER,ATA_C_ATAPI_IDENTIFY))
882 scp->devices &= ~ATA_ATAPI_MASTER;
883#if NATADISK > 0
884 if (scp->devices & ATA_ATA_MASTER)
885 ad_attach(scp, ATA_MASTER);
886 if (scp->devices & ATA_ATA_SLAVE)
887 ad_attach(scp, ATA_SLAVE);
888#endif
889#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
890 if (scp->devices & ATA_ATAPI_MASTER)
891 atapi_attach(scp, ATA_MASTER);
892 if (scp->devices & ATA_ATAPI_SLAVE)
893 atapi_attach(scp, ATA_SLAVE);
894#endif
671 }
895 }
896 scp->flags |= ATA_ATTACHED;
897 return 0;
898}
672
899
900static int
901ata_detach(device_t dev)
902{
903 struct ata_softc *scp = device_get_softc(dev);
904
905 if (!scp || !(scp->flags & ATA_ATTACHED))
906 return ENXIO;
907
673#if NATADISK > 0
908#if NATADISK > 0
674 /* now we know whats there, do the real attach, first the ATA disks */
675 for (ctlr=0; ctlr<MAXATA; ctlr++) {
676 if (!atadevices[ctlr]) continue;
677 if (atadevices[ctlr]->devices & ATA_ATA_MASTER)
678 ad_attach(atadevices[ctlr], ATA_MASTER);
679 if (atadevices[ctlr]->devices & ATA_ATA_SLAVE)
680 ad_attach(atadevices[ctlr], ATA_SLAVE);
681 }
909 if (scp->devices & ATA_ATA_MASTER)
910 ad_detach(scp, ATA_MASTER);
911 if (scp->devices & ATA_ATA_SLAVE)
912 ad_detach(scp, ATA_SLAVE);
682#endif
683#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
913#endif
914#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
684 /* then the atapi devices */
685 for (ctlr=0; ctlr<MAXATA; ctlr++) {
686 if (!atadevices[ctlr]) continue;
687 if (atadevices[ctlr]->devices & ATA_ATAPI_MASTER)
688 atapi_attach(atadevices[ctlr], ATA_MASTER);
689 if (atadevices[ctlr]->devices & ATA_ATAPI_SLAVE)
690 atapi_attach(atadevices[ctlr], ATA_SLAVE);
691 }
915 if (scp->devices & ATA_ATAPI_MASTER)
916 atapi_detach(scp, ATA_MASTER);
917 if (scp->devices & ATA_ATAPI_SLAVE)
918 atapi_detach(scp, ATA_SLAVE);
692#endif
919#endif
693 if (ata_attach_hook) {
694 config_intrhook_disestablish(ata_attach_hook);
695 free(ata_attach_hook, M_ATA);
696 ata_attach_hook = NULL;
920 if (scp->dev_param[ATA_DEV(ATA_MASTER)]) {
921 free(scp->dev_param[ATA_DEV(ATA_MASTER)], M_ATA);
922 scp->dev_param[ATA_DEV(ATA_MASTER)] = NULL;
697 }
923 }
924 if (scp->dev_param[ATA_DEV(ATA_SLAVE)]) {
925 free(scp->dev_param[ATA_DEV(ATA_SLAVE)], M_ATA);
926 scp->dev_param[ATA_DEV(ATA_SLAVE)] = NULL;
927 }
928 scp->mode[ATA_DEV(ATA_MASTER)] = ATA_PIO;
929 scp->mode[ATA_DEV(ATA_SLAVE)] = ATA_PIO;
930 bus_release_resource(dev, SYS_RES_IRQ, 0, scp->r_irq);
931 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, scp->r_bmio);
932 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTIOADDR_RID, scp->r_altio);
933 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, scp->r_io);
934 scp->flags &= ~ATA_ATTACHED;
935 return 0;
698}
699
936}
937
938static int
939ata_resume(device_t dev)
940{
941 struct ata_softc *scp = device_get_softc(dev);
942
943 ata_reinit(scp);
944 return 0;
945}
946
700static int32_t
701ata_getparam(struct ata_softc *scp, int32_t device, u_int8_t command)
702{
703 struct ata_params *ata_parm;
704 int8_t buffer[DEV_BSIZE];
705 int retry = 0;
706
707 /* select drive */
708 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
709 DELAY(1);
710
947static int32_t
948ata_getparam(struct ata_softc *scp, int32_t device, u_int8_t command)
949{
950 struct ata_params *ata_parm;
951 int8_t buffer[DEV_BSIZE];
952 int retry = 0;
953
954 /* select drive */
955 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
956 DELAY(1);
957
711 /* enable interrupts */
958 /* enable interrupt */
712 outb(scp->altioaddr, ATA_A_4BIT);
713 DELAY(1);
714
715 /* apparently some devices needs this repeated */
716 do {
717 if (ata_command(scp, device, command, 0, 0, 0, 0, 0, ATA_WAIT_INTR)) {
718 ata_printf(scp, device, "identify failed\n");
719 return -1;
720 }
721 if (retry++ > 4) {
959 outb(scp->altioaddr, ATA_A_4BIT);
960 DELAY(1);
961
962 /* apparently some devices needs this repeated */
963 do {
964 if (ata_command(scp, device, command, 0, 0, 0, 0, 0, ATA_WAIT_INTR)) {
965 ata_printf(scp, device, "identify failed\n");
966 return -1;
967 }
968 if (retry++ > 4) {
722 ata_printf(scp, device, "drive wont come ready after identify\n");
969 ata_printf(scp, device, "identify retries exceeded\n");
723 return -1;
724 }
725 } while (ata_wait(scp, device,
726 ((command == ATA_C_ATAPI_IDENTIFY) ?
727 ATA_S_DRQ : (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ))));
728
729 insw(scp->ioaddr + ATA_DATA, buffer, sizeof(buffer)/sizeof(int16_t));
730 ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT);
731 if (!ata_parm) {
970 return -1;
971 }
972 } while (ata_wait(scp, device,
973 ((command == ATA_C_ATAPI_IDENTIFY) ?
974 ATA_S_DRQ : (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ))));
975
976 insw(scp->ioaddr + ATA_DATA, buffer, sizeof(buffer)/sizeof(int16_t));
977 ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT);
978 if (!ata_parm) {
732 ata_printf(scp, device, "malloc for ata_param failed\n");
979 ata_printf(scp, device, "malloc for identify data failed\n");
733 return -1;
734 }
735 bcopy(buffer, ata_parm, sizeof(struct ata_params));
736 if (command == ATA_C_ATA_IDENTIFY ||
737 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
738 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X')))
739 bswap(ata_parm->model, sizeof(ata_parm->model));
740 btrim(ata_parm->model, sizeof(ata_parm->model));
741 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
742 bswap(ata_parm->revision, sizeof(ata_parm->revision));
743 btrim(ata_parm->revision, sizeof(ata_parm->revision));
744 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
745 scp->dev_param[ATA_DEV(device)] = ata_parm;
746 return 0;
747}
748
980 return -1;
981 }
982 bcopy(buffer, ata_parm, sizeof(struct ata_params));
983 if (command == ATA_C_ATA_IDENTIFY ||
984 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
985 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X')))
986 bswap(ata_parm->model, sizeof(ata_parm->model));
987 btrim(ata_parm->model, sizeof(ata_parm->model));
988 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
989 bswap(ata_parm->revision, sizeof(ata_parm->revision));
990 btrim(ata_parm->revision, sizeof(ata_parm->revision));
991 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
992 scp->dev_param[ATA_DEV(device)] = ata_parm;
993 return 0;
994}
995
996static void
997ata_boot_attach(void)
998{
999 struct ata_softc *scp;
1000 int32_t ctlr;
1001
1002 /*
1003 * run through all ata devices and look for real ATA & ATAPI devices
1004 * using the hints we found in the early probe, this avoids some of
1005 * the delays probing of non-exsistent devices can cause.
1006 */
1007 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
1008 if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
1009 continue;
1010 if (scp->devices & ATA_ATA_SLAVE)
1011 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY))
1012 scp->devices &= ~ATA_ATA_SLAVE;
1013 if (scp->devices & ATA_ATAPI_SLAVE)
1014 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY))
1015 scp->devices &= ~ATA_ATAPI_SLAVE;
1016 if (scp->devices & ATA_ATA_MASTER)
1017 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY))
1018 scp->devices &= ~ATA_ATA_MASTER;
1019 if (scp->devices & ATA_ATAPI_MASTER)
1020 if (ata_getparam(scp, ATA_MASTER,ATA_C_ATAPI_IDENTIFY))
1021 scp->devices &= ~ATA_ATAPI_MASTER;
1022 }
1023
1024#if NATADISK > 0
1025 /* now we know whats there, do the real attach, first the ATA disks */
1026 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
1027 if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
1028 continue;
1029 if (scp->devices & ATA_ATA_MASTER)
1030 ad_attach(scp, ATA_MASTER);
1031 if (scp->devices & ATA_ATA_SLAVE)
1032 ad_attach(scp, ATA_SLAVE);
1033 }
1034#endif
1035#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
1036 /* then the atapi devices */
1037 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
1038 if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
1039 continue;
1040 if (scp->devices & ATA_ATAPI_MASTER)
1041 atapi_attach(scp, ATA_MASTER);
1042 if (scp->devices & ATA_ATAPI_SLAVE)
1043 atapi_attach(scp, ATA_SLAVE);
1044 }
1045#endif
1046 if (ata_delayed_attach) {
1047 config_intrhook_disestablish(ata_delayed_attach);
1048 free(ata_delayed_attach, M_ATA);
1049 ata_delayed_attach = NULL;
1050 }
1051}
1052
749static void
1053static void
750ataintr(void *data)
1054ata_intr(void *data)
751{
752 struct ata_softc *scp = (struct ata_softc *)data;
753 u_int8_t dmastat;
754
755 /*
756 * since we might share the IRQ with another device, and in some
1055{
1056 struct ata_softc *scp = (struct ata_softc *)data;
1057 u_int8_t dmastat;
1058
1059 /*
1060 * since we might share the IRQ with another device, and in some
757 * case with our twin channel, we only want to process interrupts
1061 * cases with our twin channel, we only want to process interrupts
758 * that we know this channel generated.
759 */
760 switch (scp->chiptype) {
761#if NPCI > 0
762 case 0x00041103: /* HighPoint HPT366 */
763 if (!((dmastat = ata_dmastatus(scp)) & ATA_BMSTAT_INTERRUPT))
764 return;
765 outb(scp->bmaddr + ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT);
766 break;
767
768 case 0x4d33105a: /* Promise 33's */
769 case 0x4d38105a: /* Promise 66's */
1062 * that we know this channel generated.
1063 */
1064 switch (scp->chiptype) {
1065#if NPCI > 0
1066 case 0x00041103: /* HighPoint HPT366 */
1067 if (!((dmastat = ata_dmastatus(scp)) & ATA_BMSTAT_INTERRUPT))
1068 return;
1069 outb(scp->bmaddr + ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT);
1070 break;
1071
1072 case 0x4d33105a: /* Promise 33's */
1073 case 0x4d38105a: /* Promise 66's */
770 if (!(inl((pci_read_config(scp->dev, 0x20, 4) & IOMASK) + 0x1c) &
1074 {
1075 struct ata_pci_softc *sc=device_get_softc(device_get_parent(scp->dev));
1076
1077 if (!(inl(rman_get_start(sc->bmio) + 0x1c) &
771 ((scp->unit) ? 0x00004000 : 0x00000400)))
772 return;
1078 ((scp->unit) ? 0x00004000 : 0x00000400)))
1079 return;
1080 }
773 /* FALLTHROUGH */
774#endif
775 default:
776 if (scp->flags & ATA_DMA_ACTIVE) {
777 if (!(dmastat = ata_dmastatus(scp)) & ATA_BMSTAT_INTERRUPT)
778 return;
779 else
780 outb(scp->bmaddr+ATA_BMSTAT_PORT, dmastat|ATA_BMSTAT_INTERRUPT);

--- 118 unchanged lines hidden (view full) ---

899#endif
900 scp->active = ATA_IDLE;
901}
902
903void
904ata_reset(struct ata_softc *scp, int32_t *mask)
905{
906 int32_t timeout;
1081 /* FALLTHROUGH */
1082#endif
1083 default:
1084 if (scp->flags & ATA_DMA_ACTIVE) {
1085 if (!(dmastat = ata_dmastatus(scp)) & ATA_BMSTAT_INTERRUPT)
1086 return;
1087 else
1088 outb(scp->bmaddr+ATA_BMSTAT_PORT, dmastat|ATA_BMSTAT_INTERRUPT);

--- 118 unchanged lines hidden (view full) ---

1207#endif
1208 scp->active = ATA_IDLE;
1209}
1210
1211void
1212ata_reset(struct ata_softc *scp, int32_t *mask)
1213{
1214 int32_t timeout;
907 int8_t status0, status1;
1215 u_int8_t status0 = 0, status1 = 0;
908
909 /* reset channel */
910 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
911 DELAY(1);
912 inb(scp->ioaddr + ATA_STATUS);
913 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET);
914 DELAY(10000);
915 outb(scp->altioaddr, ATA_A_IDS);

--- 33 unchanged lines hidden (view full) ---

949
950int32_t
951ata_reinit(struct ata_softc *scp)
952{
953 int32_t mask = 0, omask;
954
955 scp->active = ATA_REINITING;
956 scp->running = NULL;
1216
1217 /* reset channel */
1218 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
1219 DELAY(1);
1220 inb(scp->ioaddr + ATA_STATUS);
1221 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET);
1222 DELAY(10000);
1223 outb(scp->altioaddr, ATA_A_IDS);

--- 33 unchanged lines hidden (view full) ---

1257
1258int32_t
1259ata_reinit(struct ata_softc *scp)
1260{
1261 int32_t mask = 0, omask;
1262
1263 scp->active = ATA_REINITING;
1264 scp->running = NULL;
957 ata_printf(scp, -1, "resetting devices .. ");
958 if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER))
959 mask |= 0x01;
960 if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE))
961 mask |= 0x02;
1265 if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER))
1266 mask |= 0x01;
1267 if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE))
1268 mask |= 0x02;
962 omask = mask;
963 ata_reset(scp, &mask);
964 if (omask != mask)
965 printf(" device dissapeared! %d ", omask & ~mask);
1269 if (mask) {
1270 omask = mask;
1271 ata_printf(scp, -1, "resetting devices .. ");
1272 ata_reset(scp, &mask);
1273 if (omask != mask)
1274 printf(" device dissapeared! %d ", omask & ~mask);
966
967#if NATADISK > 0
1275
1276#if NATADISK > 0
968 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0])
969 ad_reinit((struct ad_softc *)scp->dev_softc[0]);
970 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1])
971 ad_reinit((struct ad_softc *)scp->dev_softc[1]);
1277 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0])
1278 ad_reinit((struct ad_softc *)scp->dev_softc[0]);
1279 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1])
1280 ad_reinit((struct ad_softc *)scp->dev_softc[1]);
972#endif
973#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
1281#endif
1282#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
974 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0])
975 atapi_reinit((struct atapi_softc *)scp->dev_softc[0]);
976 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1])
977 atapi_reinit((struct atapi_softc *)scp->dev_softc[1]);
1283 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0])
1284 atapi_reinit((struct atapi_softc *)scp->dev_softc[0]);
1285 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1])
1286 atapi_reinit((struct atapi_softc *)scp->dev_softc[1]);
978#endif
1287#endif
979 printf("done\n");
1288 printf("done\n");
1289 }
980 scp->active = ATA_IDLE;
981 ata_start(scp);
982 return 0;
983}
984
985int32_t
986ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask)
987{

--- 110 unchanged lines hidden (view full) ---

1098
1099 default:
1100 ata_printf(scp, device, "DANGER: illegal interrupt flag=%s\n",
1101 active2str(flags));
1102 }
1103 return 0;
1104}
1105
1290 scp->active = ATA_IDLE;
1291 ata_start(scp);
1292 return 0;
1293}
1294
1295int32_t
1296ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask)
1297{

--- 110 unchanged lines hidden (view full) ---

1408
1409 default:
1410 ata_printf(scp, device, "DANGER: illegal interrupt flag=%s\n",
1411 active2str(flags));
1412 }
1413 return 0;
1414}
1415
1416int
1417ata_printf(struct ata_softc *scp, int32_t device, const char * fmt, ...)
1418{
1419 va_list ap;
1420 int ret;
1421
1422 if (device == -1)
1423 ret = printf("ata%d: ", device_get_unit(scp->dev));
1424 else
1425 ret = printf("ata%d-%s: ", device_get_unit(scp->dev),
1426 (device == ATA_MASTER) ? "master" : "slave");
1427 va_start(ap, fmt);
1428 ret += vprintf(fmt, ap);
1429 va_end(ap);
1430 return ret;
1431}
1432
1433int
1434ata_get_lun(u_int32_t *map)
1435{
1436 int lun = ffs(~*map) - 1;
1437
1438 *map |= (1 << lun);
1439 return lun;
1440}
1441
1442void
1443ata_free_lun(u_int32_t *map, int lun)
1444{
1445 *map &= ~(1 << lun);
1446}
1447
1106int8_t *
1107ata_mode2str(int32_t mode)
1108{
1109 switch (mode) {
1110 case ATA_PIO: return "BIOSPIO";
1111 case ATA_PIO0: return "PIO0";
1112 case ATA_PIO1: return "PIO1";
1113 case ATA_PIO2: return "PIO2";

--- 15 unchanged lines hidden (view full) ---

1129 case 0: return ATA_PIO0;
1130 case 1: return ATA_PIO1;
1131 case 2: return ATA_PIO2;
1132 case 3: return ATA_PIO3;
1133 case 4: return ATA_PIO4;
1134 }
1135}
1136
1448int8_t *
1449ata_mode2str(int32_t mode)
1450{
1451 switch (mode) {
1452 case ATA_PIO: return "BIOSPIO";
1453 case ATA_PIO0: return "PIO0";
1454 case ATA_PIO1: return "PIO1";
1455 case ATA_PIO2: return "PIO2";

--- 15 unchanged lines hidden (view full) ---

1471 case 0: return ATA_PIO0;
1472 case 1: return ATA_PIO1;
1473 case 2: return ATA_PIO2;
1474 case 3: return ATA_PIO3;
1475 case 4: return ATA_PIO4;
1476 }
1477}
1478
1479int
1480ata_pmode(struct ata_params *ap)
1481{
1482 if (ap->atavalid & ATA_FLAG_64_70) {
1483 if (ap->apiomodes & 2)
1484 return 4;
1485 if (ap->apiomodes & 1)
1486 return 3;
1487 }
1488 if (ap->opiomode == 2)
1489 return 2;
1490 if (ap->opiomode == 1)
1491 return 1;
1492 if (ap->opiomode == 0)
1493 return 0;
1494 return -1;
1495}
1496
1497int
1498ata_wmode(struct ata_params *ap)
1499{
1500 if (ap->wdmamodes & 4)
1501 return 2;
1502 if (ap->wdmamodes & 2)
1503 return 1;
1504 if (ap->wdmamodes & 1)
1505 return 0;
1506 return -1;
1507}
1508
1509int
1510ata_umode(struct ata_params *ap)
1511{
1512 if (ap->atavalid & ATA_FLAG_88) {
1513 if (ap->udmamodes & 0x10)
1514 return (ap->cblid ? 4 : 2);
1515 if (ap->udmamodes & 0x08)
1516 return (ap->cblid ? 3 : 2);
1517 if (ap->udmamodes & 0x04)
1518 return 2;
1519 if (ap->udmamodes & 0x02)
1520 return 1;
1521 if (ap->udmamodes & 0x01)
1522 return 0;
1523 }
1524 return -1;
1525}
1526
1137static int8_t *
1138active2str(int32_t active)
1139{
1140 static char buf[8];
1141
1142 switch (active) {
1143 case ATA_IDLE:
1144 return("ATA_IDLE");

--- 12 unchanged lines hidden (view full) ---

1157 case ATA_REINITING:
1158 return("ATA_REINITING");
1159 default:
1160 sprintf(buf, "0x%02x", active);
1161 return buf;
1162 }
1163}
1164
1527static int8_t *
1528active2str(int32_t active)
1529{
1530 static char buf[8];
1531
1532 switch (active) {
1533 case ATA_IDLE:
1534 return("ATA_IDLE");

--- 12 unchanged lines hidden (view full) ---

1547 case ATA_REINITING:
1548 return("ATA_REINITING");
1549 default:
1550 sprintf(buf, "0x%02x", active);
1551 return buf;
1552 }
1553}
1554
1165int32_t
1166ata_pmode(struct ata_params *ap)
1167{
1168 if (ap->atavalid & ATA_FLAG_64_70) {
1169 if (ap->apiomodes & 2) return 4;
1170 if (ap->apiomodes & 1) return 3;
1171 }
1172 if (ap->opiomode == 2) return 2;
1173 if (ap->opiomode == 1) return 1;
1174 if (ap->opiomode == 0) return 0;
1175 return -1;
1176}
1177
1178int32_t
1179ata_wmode(struct ata_params *ap)
1180{
1181 if (ap->wdmamodes & 4) return 2;
1182 if (ap->wdmamodes & 2) return 1;
1183 if (ap->wdmamodes & 1) return 0;
1184 return -1;
1185}
1186
1187int32_t
1188ata_umode(struct ata_params *ap)
1189{
1190 if (ap->atavalid & ATA_FLAG_88) {
1191 if (ap->udmamodes & 0x10) return (ap->cblid ? 4 : 2);
1192 if (ap->udmamodes & 0x08) return (ap->cblid ? 3 : 2);
1193 if (ap->udmamodes & 0x04) return 2;
1194 if (ap->udmamodes & 0x02) return 1;
1195 if (ap->udmamodes & 0x01) return 0;
1196 }
1197 return -1;
1198}
1199
1200static void
1201bswap(int8_t *buf, int32_t len)
1202{
1555static void
1556bswap(int8_t *buf, int32_t len)
1557{
1203 u_int16_t *p = (u_int16_t*)(buf + len);
1558 u_int16_t *ptr = (u_int16_t*)(buf + len);
1204
1559
1205 while (--p >= (u_int16_t*)buf)
1206 *p = ntohs(*p);
1560 while (--ptr >= (u_int16_t*)buf)
1561 *ptr = ntohs(*ptr);
1207}
1208
1209static void
1210btrim(int8_t *buf, int32_t len)
1211{
1562}
1563
1564static void
1565btrim(int8_t *buf, int32_t len)
1566{
1212 int8_t *p;
1567 int8_t *ptr;
1213
1568
1214 for (p = buf; p < buf+len; ++p)
1215 if (!*p)
1216 *p = ' ';
1217 for (p = buf + len - 1; p >= buf && *p == ' '; --p)
1218 *p = 0;
1569 for (ptr = buf; ptr < buf+len; ++ptr)
1570 if (!*ptr)
1571 *ptr = ' ';
1572 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1573 *ptr = 0;
1219}
1220
1221static void
1222bpack(int8_t *src, int8_t *dst, int32_t len)
1223{
1224 int32_t i, j, blank;
1225
1226 for (i = j = blank = 0 ; i < len; i++) {

--- 9 unchanged lines hidden (view full) ---

1236 continue;
1237 }
1238 dst[j++] = src[i];
1239 }
1240 if (j < len)
1241 dst[j] = 0x00;
1242}
1243
1574}
1575
1576static void
1577bpack(int8_t *src, int8_t *dst, int32_t len)
1578{
1579 int32_t i, j, blank;
1580
1581 for (i = j = blank = 0 ; i < len; i++) {

--- 9 unchanged lines hidden (view full) ---

1591 continue;
1592 }
1593 dst[j++] = src[i];
1594 }
1595 if (j < len)
1596 dst[j] = 0x00;
1597}
1598
1244int32_t
1245ata_printf(struct ata_softc *scp, int32_t device, const char * fmt, ...)
1246{
1247 va_list ap;
1248 int ret;
1249
1250 if (device == -1)
1251 ret = printf("ata%d: ", scp->lun);
1252 else
1253 ret = printf("ata%d-%s: ", scp->lun,
1254 (device == ATA_MASTER) ? "master" : "slave");
1255 va_start(ap, fmt);
1256 ret += vprintf(fmt, ap);
1257 va_end(ap);
1258 return ret;
1259}
1260
1261static char ata_conf[1024];
1262
1263static void
1264ata_change_mode(struct ata_softc *scp, int32_t device, int32_t mode)
1265{
1266 int32_t s = splbio();
1267
1268 while (scp->active != ATA_IDLE)
1269 tsleep((caddr_t)&s, PRIBIO, "atachm", hz/4);
1270 scp->active = ATA_REINITING;
1271 ata_dmainit(scp, device, ata_pmode(ATA_PARAM(scp, device)),
1272 mode < ATA_DMA ? -1 : ata_wmode(ATA_PARAM(scp, device)),
1273 mode < ATA_DMA ? -1 : ata_umode(ATA_PARAM(scp, device)));
1274 scp->active = ATA_IDLE;
1275 ata_start(scp);
1276 splx(s);
1277}
1278
1279static int
1280sysctl_hw_ata SYSCTL_HANDLER_ARGS
1281{
1599static void
1600ata_change_mode(struct ata_softc *scp, int32_t device, int32_t mode)
1601{
1602 int32_t s = splbio();
1603
1604 while (scp->active != ATA_IDLE)
1605 tsleep((caddr_t)&s, PRIBIO, "atachm", hz/4);
1606 scp->active = ATA_REINITING;
1607 ata_dmainit(scp, device, ata_pmode(ATA_PARAM(scp, device)),
1608 mode < ATA_DMA ? -1 : ata_wmode(ATA_PARAM(scp, device)),
1609 mode < ATA_DMA ? -1 : ata_umode(ATA_PARAM(scp, device)));
1610 scp->active = ATA_IDLE;
1611 ata_start(scp);
1612 splx(s);
1613}
1614
1615static int
1616sysctl_hw_ata SYSCTL_HANDLER_ARGS
1617{
1282 int error, i;
1618 struct ata_softc *scp;
1619 int ctlr, error, i;
1283
1284 /* readout internal state */
1285 bzero(ata_conf, sizeof(ata_conf));
1620
1621 /* readout internal state */
1622 bzero(ata_conf, sizeof(ata_conf));
1286 for (i = 0; i < (atanlun << 1); i++) {
1287 if (!atadevices[i >> 1] || !atadevices[ i >> 1]->dev_softc[i & 1])
1288 strcat(ata_conf, "---,");
1289 else if (atadevices[i >> 1]->mode[i & 1] >= ATA_DMA)
1290 strcat(ata_conf, "dma,");
1291 else
1292 strcat(ata_conf, "pio,");
1623 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
1624 if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
1625 continue;
1626 for (i = 0; i < 2; i++) {
1627 if (!scp->dev_softc[i])
1628 strcat(ata_conf, "---,");
1629 else if (scp->mode[i] >= ATA_DMA)
1630 strcat(ata_conf, "dma,");
1631 else
1632 strcat(ata_conf, "pio,");
1633 }
1293 }
1294 error = sysctl_handle_string(oidp, ata_conf, sizeof(ata_conf), req);
1295 if (error == 0 && req->newptr != NULL) {
1296 char *ptr = ata_conf;
1297
1298 /* update internal state */
1299 i = 0;
1300 while (*ptr) {
1301 if (!strncmp(ptr, "pio", 3) || !strncmp(ptr, "PIO", 3)) {
1634 }
1635 error = sysctl_handle_string(oidp, ata_conf, sizeof(ata_conf), req);
1636 if (error == 0 && req->newptr != NULL) {
1637 char *ptr = ata_conf;
1638
1639 /* update internal state */
1640 i = 0;
1641 while (*ptr) {
1642 if (!strncmp(ptr, "pio", 3) || !strncmp(ptr, "PIO", 3)) {
1302 if (atadevices[i >> 1] &&
1303 atadevices[i >> 1]->dev_softc[i & 1] &&
1304 atadevices[i >>1 ]->mode[i & 1] >= ATA_DMA)
1305 ata_change_mode(atadevices[i >> 1],
1306 (i & 1) ? ATA_SLAVE : ATA_MASTER, ATA_PIO);
1643 if ((scp = devclass_get_softc(ata_devclass, i >> 1)) &&
1644 scp->dev_softc[i & 1] && scp->mode[i & 1] >= ATA_DMA)
1645 ata_change_mode(scp, (i & 1)?ATA_SLAVE:ATA_MASTER, ATA_PIO);
1307 }
1308 else if (!strncmp(ptr, "dma", 3) || !strncmp(ptr, "DMA", 3)) {
1646 }
1647 else if (!strncmp(ptr, "dma", 3) || !strncmp(ptr, "DMA", 3)) {
1309 if (atadevices[i >> 1] &&
1310 atadevices[i >> 1]->dev_softc[i & 1] &&
1311 atadevices[i >> 1]->mode[i & 1] < ATA_DMA)
1312 ata_change_mode(atadevices[i >> 1],
1313 (i & 1) ? ATA_SLAVE : ATA_MASTER, ATA_DMA);
1648 if ((scp = devclass_get_softc(ata_devclass, i >> 1)) &&
1649 scp->dev_softc[i & 1] && scp->mode[i & 1] < ATA_DMA)
1650 ata_change_mode(scp, (i & 1)?ATA_SLAVE:ATA_MASTER, ATA_DMA);
1314 }
1315 else if (strncmp(ptr, "---", 3))
1316 break;
1317 ptr+=3;
1651 }
1652 else if (strncmp(ptr, "---", 3))
1653 break;
1654 ptr+=3;
1318 if (*ptr++ != ',' || ++i > (atanlun << 1))
1655 if (*ptr++ != ',' ||
1656 ++i > (devclass_get_maxunit(ata_devclass) << 1))
1319 break;
1320 }
1321 }
1322 return error;
1323}
1657 break;
1658 }
1659 }
1660 return error;
1661}
1324
1325SYSCTL_PROC(_hw, OID_AUTO, atamodes, CTLTYPE_STRING | CTLFLAG_RW,
1326 0, sizeof(ata_conf), sysctl_hw_ata, "A", "");
1662SYSCTL_PROC(_hw, OID_AUTO, atamodes, CTLTYPE_STRING | CTLFLAG_RW,
1663 0, sizeof(ata_conf), sysctl_hw_ata, "A", "");
1664
1665static void
1666ata_init(void)
1667{
1668 /* register boot attach to be run when interrupts are enabled */
1669 if (!(ata_delayed_attach = (struct intr_config_hook *)
1670 malloc(sizeof(struct intr_config_hook),
1671 M_TEMP, M_NOWAIT))) {
1672 printf("ata: malloc of delayed attach hook failed\n");
1673 return;
1674 }
1675 bzero(ata_delayed_attach, sizeof(struct intr_config_hook));
1676
1677 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1678 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1679 printf("ata: config_intrhook_establish failed\n");
1680 free(ata_delayed_attach, M_TEMP);
1681 }
1682}
1683SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)