Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/dev/ata/ata-pci.c
39562 views
1
/*-
2
* SPDX-License-Identifier: BSD-2-Clause
3
*
4
* Copyright (c) 1998 - 2008 Søren Schmidt <[email protected]>
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
*
17
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
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
29
#include <sys/param.h>
30
#include <sys/systm.h>
31
#include <sys/kernel.h>
32
#include <sys/module.h>
33
#include <sys/ata.h>
34
#include <sys/bus.h>
35
#include <sys/conf.h>
36
#include <sys/malloc.h>
37
#include <sys/sbuf.h>
38
#include <sys/sema.h>
39
#include <sys/stdarg.h>
40
#include <sys/taskqueue.h>
41
42
#include <vm/uma.h>
43
44
#include <machine/resource.h>
45
#include <machine/bus.h>
46
#include <sys/rman.h>
47
48
#include <dev/pci/pcivar.h>
49
#include <dev/pci/pcireg.h>
50
#include <dev/ata/ata-all.h>
51
#include <dev/ata/ata-pci.h>
52
#include <ata_if.h>
53
54
MALLOC_DEFINE(M_ATAPCI, "ata_pci", "ATA driver PCI");
55
56
/* misc defines */
57
#define IOMASK 0xfffffffc
58
59
/*
60
* generic PCI ATA device probe
61
*/
62
int
63
ata_pci_probe(device_t dev)
64
{
65
struct ata_pci_controller *ctlr = device_get_softc(dev);
66
67
/* is this a storage class device ? */
68
if (pci_get_class(dev) != PCIC_STORAGE)
69
return (ENXIO);
70
71
/* is this an IDE/ATA type device ? */
72
if (pci_get_subclass(dev) != PCIS_STORAGE_IDE)
73
return (ENXIO);
74
75
device_set_descf(dev, "%s ATA controller", ata_pcivendor2str(dev));
76
ctlr->chipinit = ata_generic_chipinit;
77
78
/* we are a low priority handler */
79
return (BUS_PROBE_GENERIC);
80
}
81
82
int
83
ata_pci_attach(device_t dev)
84
{
85
struct ata_pci_controller *ctlr = device_get_softc(dev);
86
device_t child;
87
u_int32_t cmd;
88
int unit;
89
90
/* do chipset specific setups only needed once */
91
ctlr->legacy = ata_legacy(dev);
92
if (ctlr->legacy || pci_read_config(dev, PCIR_BAR(2), 4) & IOMASK)
93
ctlr->channels = 2;
94
else
95
ctlr->channels = 1;
96
ctlr->ichannels = -1;
97
ctlr->ch_attach = ata_pci_ch_attach;
98
ctlr->ch_detach = ata_pci_ch_detach;
99
ctlr->dev = dev;
100
101
/* if needed try to enable busmastering */
102
pci_enable_busmaster(dev);
103
cmd = pci_read_config(dev, PCIR_COMMAND, 2);
104
105
/* if busmastering mode "stuck" use it */
106
if ((cmd & PCIM_CMD_BUSMASTEREN) == PCIM_CMD_BUSMASTEREN) {
107
ctlr->r_type1 = SYS_RES_IOPORT;
108
ctlr->r_rid1 = ATA_BMADDR_RID;
109
ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1, &ctlr->r_rid1,
110
RF_ACTIVE);
111
}
112
113
if (ctlr->chipinit(dev)) {
114
if (ctlr->r_res1)
115
bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,
116
ctlr->r_res1);
117
return ENXIO;
118
}
119
120
/* attach all channels on this controller */
121
for (unit = 0; unit < ctlr->channels; unit++) {
122
if ((ctlr->ichannels & (1 << unit)) == 0)
123
continue;
124
child = device_add_child(dev, "ata",
125
((unit == 0 || unit == 1) && ctlr->legacy) ?
126
unit : devclass_find_free_unit(ata_devclass, 2));
127
if (child == NULL)
128
device_printf(dev, "failed to add ata child device\n");
129
else
130
device_set_ivars(child, (void *)(intptr_t)unit);
131
}
132
bus_attach_children(dev);
133
return 0;
134
}
135
136
int
137
ata_pci_detach(device_t dev)
138
{
139
struct ata_pci_controller *ctlr = device_get_softc(dev);
140
int error;
141
142
/* detach & delete all children */
143
error = bus_generic_detach(dev);
144
if (error != 0)
145
return (error);
146
147
if (ctlr->r_irq) {
148
bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle);
149
bus_release_resource(dev, SYS_RES_IRQ, ctlr->r_irq_rid, ctlr->r_irq);
150
if (ctlr->r_irq_rid != ATA_IRQ_RID)
151
pci_release_msi(dev);
152
}
153
if (ctlr->chipdeinit != NULL)
154
ctlr->chipdeinit(dev);
155
if (ctlr->r_res2) {
156
bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
157
}
158
if (ctlr->r_res1) {
159
bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
160
}
161
162
return 0;
163
}
164
165
int
166
ata_pci_suspend(device_t dev)
167
{
168
struct ata_pci_controller *ctlr = device_get_softc(dev);
169
int error = 0;
170
171
bus_generic_suspend(dev);
172
if (ctlr->suspend)
173
error = ctlr->suspend(dev);
174
return error;
175
}
176
177
int
178
ata_pci_resume(device_t dev)
179
{
180
struct ata_pci_controller *ctlr = device_get_softc(dev);
181
int error = 0;
182
183
if (ctlr->resume)
184
error = ctlr->resume(dev);
185
bus_generic_resume(dev);
186
return error;
187
}
188
189
int
190
ata_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
191
{
192
193
return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
194
}
195
196
int
197
ata_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
198
{
199
200
return (BUS_WRITE_IVAR(device_get_parent(dev), dev, which, value));
201
}
202
203
uint32_t
204
ata_pci_read_config(device_t dev, device_t child, int reg, int width)
205
{
206
207
return (pci_read_config(dev, reg, width));
208
}
209
210
void
211
ata_pci_write_config(device_t dev, device_t child, int reg,
212
uint32_t val, int width)
213
{
214
215
pci_write_config(dev, reg, val, width);
216
}
217
218
struct resource *
219
ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
220
rman_res_t start, rman_res_t end, rman_res_t count,
221
u_int flags)
222
{
223
struct ata_pci_controller *controller = device_get_softc(dev);
224
struct resource *res = NULL;
225
226
if (device_get_devclass(child) == ata_devclass) {
227
int unit = ((struct ata_channel *)device_get_softc(child))->unit;
228
int myrid;
229
230
if (type == SYS_RES_IOPORT) {
231
switch (*rid) {
232
case ATA_IOADDR_RID:
233
if (controller->legacy) {
234
start = (unit ? ATA_SECONDARY : ATA_PRIMARY);
235
count = ATA_IOSIZE;
236
end = start + count - 1;
237
}
238
myrid = PCIR_BAR(0) + (unit << 3);
239
res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
240
SYS_RES_IOPORT, &myrid,
241
start, end, count, flags);
242
break;
243
case ATA_CTLADDR_RID:
244
if (controller->legacy) {
245
start = (unit ? ATA_SECONDARY : ATA_PRIMARY) +
246
ATA_CTLOFFSET;
247
count = ATA_CTLIOSIZE;
248
end = start + count - 1;
249
}
250
myrid = PCIR_BAR(1) + (unit << 3);
251
res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
252
SYS_RES_IOPORT, &myrid,
253
start, end, count, flags);
254
break;
255
}
256
}
257
if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
258
if (controller->legacy) {
259
int irq = (unit == 0 ? 14 : 15);
260
261
res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
262
SYS_RES_IRQ, rid, irq, irq, 1, flags);
263
} else
264
res = controller->r_irq;
265
}
266
} else {
267
if (type == SYS_RES_IRQ) {
268
if (*rid != ATA_IRQ_RID)
269
return (NULL);
270
res = controller->r_irq;
271
} else {
272
res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
273
type, rid, start, end, count, flags);
274
}
275
}
276
return (res);
277
}
278
279
int
280
ata_pci_release_resource(device_t dev, device_t child, struct resource *r)
281
{
282
int rid = rman_get_rid(r);
283
int type = rman_get_type(r);
284
285
if (device_get_devclass(child) == ata_devclass) {
286
struct ata_pci_controller *controller = device_get_softc(dev);
287
288
if (type == SYS_RES_IOPORT) {
289
switch (rid) {
290
case ATA_IOADDR_RID:
291
case ATA_CTLADDR_RID:
292
return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
293
r);
294
default:
295
return ENOENT;
296
}
297
}
298
if (type == SYS_RES_IRQ) {
299
if (rid != ATA_IRQ_RID)
300
return ENOENT;
301
if (controller->legacy) {
302
return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
303
r);
304
} else
305
return 0;
306
}
307
} else {
308
if (type == SYS_RES_IRQ) {
309
if (rid != ATA_IRQ_RID)
310
return (ENOENT);
311
return (0);
312
} else {
313
return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
314
r));
315
}
316
}
317
return (EINVAL);
318
}
319
320
int
321
ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
322
int flags, driver_filter_t *filter, driver_intr_t *function,
323
void *argument, void **cookiep)
324
{
325
struct ata_pci_controller *controller = device_get_softc(dev);
326
327
if (controller->legacy) {
328
return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
329
flags, filter, function, argument, cookiep);
330
} else {
331
struct ata_pci_controller *controller = device_get_softc(dev);
332
int unit;
333
334
if (filter != NULL) {
335
printf("ata-pci.c: we cannot use a filter here\n");
336
return (EINVAL);
337
}
338
if (device_get_devclass(child) == ata_devclass)
339
unit = ((struct ata_channel *)device_get_softc(child))->unit;
340
else
341
unit = ATA_PCI_MAX_CH - 1;
342
controller->interrupt[unit].function = function;
343
controller->interrupt[unit].argument = argument;
344
*cookiep = controller;
345
return 0;
346
}
347
}
348
349
int
350
ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
351
void *cookie)
352
{
353
struct ata_pci_controller *controller = device_get_softc(dev);
354
355
if (controller->legacy) {
356
return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
357
} else {
358
struct ata_pci_controller *controller = device_get_softc(dev);
359
int unit;
360
361
if (device_get_devclass(child) == ata_devclass)
362
unit = ((struct ata_channel *)device_get_softc(child))->unit;
363
else
364
unit = ATA_PCI_MAX_CH - 1;
365
controller->interrupt[unit].function = NULL;
366
controller->interrupt[unit].argument = NULL;
367
return 0;
368
}
369
}
370
371
int
372
ata_generic_setmode(device_t dev, int target, int mode)
373
{
374
375
return (min(mode, ATA_UDMA2));
376
}
377
378
int
379
ata_generic_chipinit(device_t dev)
380
{
381
struct ata_pci_controller *ctlr = device_get_softc(dev);
382
383
if (ata_setup_interrupt(dev, ata_generic_intr))
384
return ENXIO;
385
ctlr->setmode = ata_generic_setmode;
386
return 0;
387
}
388
389
int
390
ata_pci_ch_attach(device_t dev)
391
{
392
struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
393
struct ata_channel *ch = device_get_softc(dev);
394
struct resource *io = NULL, *ctlio = NULL;
395
int i, rid;
396
397
rid = ATA_IOADDR_RID;
398
if (!(io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE)))
399
return ENXIO;
400
401
rid = ATA_CTLADDR_RID;
402
if (!(ctlio = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,RF_ACTIVE))){
403
bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
404
return ENXIO;
405
}
406
407
ata_pci_dmainit(dev);
408
409
for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
410
ch->r_io[i].res = io;
411
ch->r_io[i].offset = i;
412
}
413
ch->r_io[ATA_CONTROL].res = ctlio;
414
ch->r_io[ATA_CONTROL].offset = ctlr->legacy ? 0 : 2;
415
ch->r_io[ATA_IDX_ADDR].res = io;
416
ata_default_registers(dev);
417
if (ctlr->r_res1) {
418
for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
419
ch->r_io[i].res = ctlr->r_res1;
420
ch->r_io[i].offset = (i - ATA_BMCMD_PORT) + (ch->unit*ATA_BMIOSIZE);
421
}
422
}
423
424
ata_pci_hw(dev);
425
return 0;
426
}
427
428
int
429
ata_pci_ch_detach(device_t dev)
430
{
431
struct ata_channel *ch = device_get_softc(dev);
432
433
ata_pci_dmafini(dev);
434
435
bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID,
436
ch->r_io[ATA_CONTROL].res);
437
bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID,
438
ch->r_io[ATA_IDX_ADDR].res);
439
440
return (0);
441
}
442
443
int
444
ata_pci_status(device_t dev)
445
{
446
struct ata_pci_controller *controller =
447
device_get_softc(device_get_parent(dev));
448
struct ata_channel *ch = device_get_softc(dev);
449
450
if ((dumping || !controller->legacy) &&
451
((ch->flags & ATA_ALWAYS_DMASTAT) ||
452
(ch->dma.flags & ATA_DMA_ACTIVE))) {
453
int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
454
455
if ((bmstat & ATA_BMSTAT_INTERRUPT) == 0)
456
return 0;
457
ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
458
DELAY(1);
459
}
460
if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
461
DELAY(100);
462
if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
463
return 0;
464
}
465
return 1;
466
}
467
468
void
469
ata_pci_hw(device_t dev)
470
{
471
struct ata_channel *ch = device_get_softc(dev);
472
473
ata_generic_hw(dev);
474
ch->hw.status = ata_pci_status;
475
}
476
477
static int
478
ata_pci_dmastart(struct ata_request *request)
479
{
480
struct ata_channel *ch = device_get_softc(request->parent);
481
482
ATA_DEBUG_RQ(request, "dmastart");
483
484
ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
485
(ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
486
ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, request->dma->sg_bus);
487
ch->dma.flags |= ATA_DMA_ACTIVE;
488
ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
489
(ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_WRITE_READ) |
490
((request->flags & ATA_R_READ) ? ATA_BMCMD_WRITE_READ : 0)|
491
ATA_BMCMD_START_STOP);
492
return 0;
493
}
494
495
static int
496
ata_pci_dmastop(struct ata_request *request)
497
{
498
struct ata_channel *ch = device_get_softc(request->parent);
499
int error;
500
501
ATA_DEBUG_RQ(request, "dmastop");
502
503
ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
504
ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
505
ch->dma.flags &= ~ATA_DMA_ACTIVE;
506
error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
507
ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
508
return error;
509
}
510
511
static void
512
ata_pci_dmareset(device_t dev)
513
{
514
struct ata_channel *ch = device_get_softc(dev);
515
struct ata_request *request;
516
517
ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
518
ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
519
ch->dma.flags &= ~ATA_DMA_ACTIVE;
520
ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
521
if ((request = ch->running)) {
522
device_printf(dev, "DMA reset calling unload\n");
523
ch->dma.unload(request);
524
}
525
}
526
527
void
528
ata_pci_dmainit(device_t dev)
529
{
530
struct ata_channel *ch = device_get_softc(dev);
531
532
ata_dmainit(dev);
533
ch->dma.start = ata_pci_dmastart;
534
ch->dma.stop = ata_pci_dmastop;
535
ch->dma.reset = ata_pci_dmareset;
536
}
537
538
void
539
ata_pci_dmafini(device_t dev)
540
{
541
542
ata_dmafini(dev);
543
}
544
545
int
546
ata_pci_print_child(device_t dev, device_t child)
547
{
548
int retval;
549
550
retval = bus_print_child_header(dev, child);
551
retval += printf(" at channel %d",
552
(int)(intptr_t)device_get_ivars(child));
553
retval += bus_print_child_footer(dev, child);
554
555
return (retval);
556
}
557
558
int
559
ata_pci_child_location(device_t dev, device_t child, struct sbuf *sb)
560
{
561
562
sbuf_printf(sb, "channel=%d",
563
(int)(intptr_t)device_get_ivars(child));
564
return (0);
565
}
566
567
static bus_dma_tag_t
568
ata_pci_get_dma_tag(device_t bus, device_t child)
569
{
570
571
return (bus_get_dma_tag(bus));
572
}
573
574
static device_method_t ata_pci_methods[] = {
575
/* device interface */
576
DEVMETHOD(device_probe, ata_pci_probe),
577
DEVMETHOD(device_attach, ata_pci_attach),
578
DEVMETHOD(device_detach, ata_pci_detach),
579
DEVMETHOD(device_suspend, ata_pci_suspend),
580
DEVMETHOD(device_resume, ata_pci_resume),
581
DEVMETHOD(device_shutdown, bus_generic_shutdown),
582
583
/* bus methods */
584
DEVMETHOD(bus_read_ivar, ata_pci_read_ivar),
585
DEVMETHOD(bus_write_ivar, ata_pci_write_ivar),
586
DEVMETHOD(bus_alloc_resource, ata_pci_alloc_resource),
587
DEVMETHOD(bus_release_resource, ata_pci_release_resource),
588
DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
589
DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
590
DEVMETHOD(bus_setup_intr, ata_pci_setup_intr),
591
DEVMETHOD(bus_teardown_intr, ata_pci_teardown_intr),
592
DEVMETHOD(pci_read_config, ata_pci_read_config),
593
DEVMETHOD(pci_write_config, ata_pci_write_config),
594
DEVMETHOD(bus_print_child, ata_pci_print_child),
595
DEVMETHOD(bus_child_location, ata_pci_child_location),
596
DEVMETHOD(bus_get_dma_tag, ata_pci_get_dma_tag),
597
598
DEVMETHOD_END
599
};
600
601
static driver_t ata_pci_driver = {
602
"atapci",
603
ata_pci_methods,
604
sizeof(struct ata_pci_controller),
605
};
606
607
DRIVER_MODULE(atapci, pci, ata_pci_driver, NULL, NULL);
608
MODULE_VERSION(atapci, 1);
609
MODULE_DEPEND(atapci, ata, 1, 1, 1);
610
611
static int
612
ata_pcichannel_probe(device_t dev)
613
{
614
615
if ((intptr_t)device_get_ivars(dev) < 0)
616
return (ENXIO);
617
device_set_desc(dev, "ATA channel");
618
619
return ata_probe(dev);
620
}
621
622
static int
623
ata_pcichannel_attach(device_t dev)
624
{
625
struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
626
struct ata_channel *ch = device_get_softc(dev);
627
int error;
628
629
if (ch->attached)
630
return (0);
631
ch->attached = 1;
632
633
ch->dev = dev;
634
ch->unit = (intptr_t)device_get_ivars(dev);
635
636
resource_int_value(device_get_name(dev),
637
device_get_unit(dev), "pm_level", &ch->pm_level);
638
639
if ((error = ctlr->ch_attach(dev)))
640
return error;
641
642
return ata_attach(dev);
643
}
644
645
static int
646
ata_pcichannel_detach(device_t dev)
647
{
648
struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
649
struct ata_channel *ch = device_get_softc(dev);
650
int error;
651
652
if (!ch->attached)
653
return (0);
654
ch->attached = 0;
655
656
if ((error = ata_detach(dev)))
657
return error;
658
659
if (ctlr->ch_detach)
660
return (ctlr->ch_detach(dev));
661
662
return (0);
663
}
664
static int
665
ata_pcichannel_suspend(device_t dev)
666
{
667
struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
668
struct ata_channel *ch = device_get_softc(dev);
669
int error;
670
671
if (!ch->attached)
672
return (0);
673
674
if ((error = ata_suspend(dev)))
675
return (error);
676
677
if (ctlr->ch_suspend != NULL && (error = ctlr->ch_suspend(dev)))
678
return (error);
679
680
return (0);
681
}
682
683
static int
684
ata_pcichannel_resume(device_t dev)
685
{
686
struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
687
struct ata_channel *ch = device_get_softc(dev);
688
int error;
689
690
if (!ch->attached)
691
return (0);
692
693
if (ctlr->ch_resume != NULL && (error = ctlr->ch_resume(dev)))
694
return (error);
695
696
return ata_resume(dev);
697
}
698
699
static void
700
ata_pcichannel_reset(device_t dev)
701
{
702
struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
703
struct ata_channel *ch = device_get_softc(dev);
704
705
/* if DMA engine present reset it */
706
if (ch->dma.reset)
707
ch->dma.reset(dev);
708
709
/* reset the controller HW */
710
if (ctlr->reset)
711
ctlr->reset(dev);
712
else
713
ata_generic_reset(dev);
714
}
715
716
static int
717
ata_pcichannel_setmode(device_t dev, int target, int mode)
718
{
719
struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
720
721
if (ctlr->setmode)
722
return (ctlr->setmode(dev, target, mode));
723
else
724
return (ata_generic_setmode(dev, target, mode));
725
}
726
727
static int
728
ata_pcichannel_getrev(device_t dev, int target)
729
{
730
struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
731
struct ata_channel *ch = device_get_softc(dev);
732
733
if (ch->flags & ATA_SATA) {
734
if (ctlr->getrev)
735
return (ctlr->getrev(dev, target));
736
else
737
return (0xff);
738
} else
739
return (0);
740
}
741
742
static device_method_t ata_pcichannel_methods[] = {
743
/* device interface */
744
DEVMETHOD(device_probe, ata_pcichannel_probe),
745
DEVMETHOD(device_attach, ata_pcichannel_attach),
746
DEVMETHOD(device_detach, ata_pcichannel_detach),
747
DEVMETHOD(device_shutdown, bus_generic_shutdown),
748
DEVMETHOD(device_suspend, ata_pcichannel_suspend),
749
DEVMETHOD(device_resume, ata_pcichannel_resume),
750
751
/* ATA methods */
752
DEVMETHOD(ata_setmode, ata_pcichannel_setmode),
753
DEVMETHOD(ata_getrev, ata_pcichannel_getrev),
754
DEVMETHOD(ata_reset, ata_pcichannel_reset),
755
756
DEVMETHOD_END
757
};
758
759
driver_t ata_pcichannel_driver = {
760
"ata",
761
ata_pcichannel_methods,
762
sizeof(struct ata_channel),
763
};
764
765
DRIVER_MODULE(ata, atapci, ata_pcichannel_driver, NULL, NULL);
766
767
/*
768
* misc support functions
769
*/
770
int
771
ata_legacy(device_t dev)
772
{
773
return (((pci_read_config(dev, PCIR_SUBCLASS, 1) == PCIS_STORAGE_IDE) &&
774
(pci_read_config(dev, PCIR_PROGIF, 1)&PCIP_STORAGE_IDE_MASTERDEV)&&
775
((pci_read_config(dev, PCIR_PROGIF, 1) &
776
(PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC)) !=
777
(PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC))) ||
778
(!pci_read_config(dev, PCIR_BAR(0), 4) &&
779
!pci_read_config(dev, PCIR_BAR(1), 4) &&
780
!pci_read_config(dev, PCIR_BAR(2), 4) &&
781
!pci_read_config(dev, PCIR_BAR(3), 4) &&
782
!pci_read_config(dev, PCIR_BAR(5), 4)));
783
}
784
785
void
786
ata_generic_intr(void *data)
787
{
788
struct ata_pci_controller *ctlr = data;
789
struct ata_channel *ch;
790
int unit;
791
792
for (unit = 0; unit < ATA_PCI_MAX_CH; unit++) {
793
if ((ch = ctlr->interrupt[unit].argument))
794
ctlr->interrupt[unit].function(ch);
795
}
796
}
797
798
int
799
ata_setup_interrupt(device_t dev, void *intr_func)
800
{
801
struct ata_pci_controller *ctlr = device_get_softc(dev);
802
int i, msi = 0;
803
804
if (!ctlr->legacy) {
805
if (resource_int_value(device_get_name(dev),
806
device_get_unit(dev), "msi", &i) == 0 && i != 0)
807
msi = 1;
808
if (msi && pci_msi_count(dev) > 0 && pci_alloc_msi(dev, &msi) == 0) {
809
ctlr->r_irq_rid = 0x1;
810
} else {
811
msi = 0;
812
ctlr->r_irq_rid = ATA_IRQ_RID;
813
}
814
if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
815
&ctlr->r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
816
device_printf(dev, "unable to map interrupt\n");
817
if (msi)
818
pci_release_msi(dev);
819
return ENXIO;
820
}
821
if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS, NULL,
822
intr_func, ctlr, &ctlr->handle))) {
823
device_printf(dev, "unable to setup interrupt\n");
824
bus_release_resource(dev,
825
SYS_RES_IRQ, ctlr->r_irq_rid, ctlr->r_irq);
826
if (msi)
827
pci_release_msi(dev);
828
return ENXIO;
829
}
830
}
831
return 0;
832
}
833
834
void
835
ata_set_desc(device_t dev)
836
{
837
struct ata_pci_controller *ctlr = device_get_softc(dev);
838
839
device_set_descf(dev, "%s %s %s controller",
840
ata_pcivendor2str(dev), ctlr->chip->text,
841
ata_mode2str(ctlr->chip->max_dma));
842
}
843
844
const struct ata_chip_id *
845
ata_match_chip(device_t dev, const struct ata_chip_id *index)
846
{
847
uint32_t devid;
848
uint8_t revid;
849
850
devid = pci_get_devid(dev);
851
revid = pci_get_revid(dev);
852
while (index->chipid != 0) {
853
if (devid == index->chipid && revid >= index->chiprev)
854
return (index);
855
index++;
856
}
857
return (NULL);
858
}
859
860
const struct ata_chip_id *
861
ata_find_chip(device_t dev, const struct ata_chip_id *index, int slot)
862
{
863
const struct ata_chip_id *idx;
864
device_t *children;
865
int nchildren, i;
866
uint8_t s;
867
868
if (device_get_children(device_get_parent(dev), &children, &nchildren))
869
return (NULL);
870
871
for (i = 0; i < nchildren; i++) {
872
s = pci_get_slot(children[i]);
873
if ((slot >= 0 && s == slot) || (slot < 0 && s <= -slot)) {
874
idx = ata_match_chip(children[i], index);
875
if (idx != NULL) {
876
free(children, M_TEMP);
877
return (idx);
878
}
879
}
880
}
881
free(children, M_TEMP);
882
return (NULL);
883
}
884
885
const char *
886
ata_pcivendor2str(device_t dev)
887
{
888
switch (pci_get_vendor(dev)) {
889
case ATA_ACARD_ID: return "Acard";
890
case ATA_ACER_LABS_ID: return "AcerLabs";
891
case ATA_AMD_ID: return "AMD";
892
case ATA_ADAPTEC_ID: return "Adaptec";
893
case ATA_ATI_ID: return "ATI";
894
case ATA_CYRIX_ID: return "Cyrix";
895
case ATA_CYPRESS_ID: return "Cypress";
896
case ATA_HIGHPOINT_ID: return "HighPoint";
897
case ATA_INTEL_ID: return "Intel";
898
case ATA_ITE_ID: return "ITE";
899
case ATA_JMICRON_ID: return "JMicron";
900
case ATA_MARVELL_ID: return "Marvell";
901
case ATA_MARVELL2_ID: return "Marvell";
902
case ATA_NATIONAL_ID: return "National";
903
case ATA_NETCELL_ID: return "Netcell";
904
case ATA_NVIDIA_ID: return "nVidia";
905
case ATA_PROMISE_ID: return "Promise";
906
case ATA_SERVERWORKS_ID: return "ServerWorks";
907
case ATA_SILICON_IMAGE_ID: return "SiI";
908
case ATA_SIS_ID: return "SiS";
909
case ATA_VIA_ID: return "VIA";
910
case ATA_CENATEK_ID: return "Cenatek";
911
case ATA_MICRON_ID: return "Micron";
912
default: return "Generic";
913
}
914
}
915
916
int
917
ata_mode2idx(int mode)
918
{
919
if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
920
return (mode & ATA_MODE_MASK) + 8;
921
if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
922
return (mode & ATA_MODE_MASK) + 5;
923
return (mode & ATA_MODE_MASK) - ATA_PIO0;
924
}
925
926