2 * Copyright (c) 1998 - 2006 Søren Schmidt <sos@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD: src/sys/dev/ata/ata-all.c,v 1.279 2007/02/23 16:25:08 jhb Exp $
31 #include <sys/param.h>
33 #include <sys/callout.h>
35 #include <sys/ctype.h>
36 #include <sys/device.h>
37 #include <sys/endian.h>
38 #include <sys/kernel.h>
39 #include <sys/libkern.h>
40 #include <sys/lock.h> /* for {get,rel}_mplock() */
41 #include <sys/malloc.h>
42 #include <sys/module.h>
44 #include <sys/objcache.h>
45 #include <sys/queue.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
49 #include <sys/mplock2.h>
54 /* device structure */
55 static d_ioctl_t ata_ioctl
;
56 static struct dev_ops ata_ops
= {
65 static void ata_boot_attach(void);
67 static device_t
ata_add_child(device_t
, struct ata_device
*, int);
68 static int ata_getparam(struct ata_device
*, int);
69 static void bswap(int8_t *, int);
70 static void btrim(int8_t *, int);
71 static void bpack(int8_t *, int8_t *, int);
74 MALLOC_DEFINE(M_ATA
, "ata_generic", "ATA driver generic layer");
75 int (*ata_raid_ioctl_func
)(u_long cmd
, caddr_t data
) = NULL
;
76 devclass_t ata_devclass
;
77 struct objcache
*ata_request_cache
;
78 struct objcache
*ata_composite_cache
;
79 struct objcache_malloc_args ata_request_malloc_args
= {
80 sizeof(struct ata_request
), M_ATA
};
81 struct objcache_malloc_args ata_composite_malloc_args
= {
82 sizeof(struct ata_composite
), M_ATA
};
86 static int ata_dma
= 1;
87 static int atapi_dma
= 1;
90 SYSCTL_NODE(_hw
, OID_AUTO
, ata
, CTLFLAG_RD
, 0, "ATA driver parameters");
91 TUNABLE_INT("hw.ata.ata_dma", &ata_dma
);
92 SYSCTL_INT(_hw_ata
, OID_AUTO
, ata_dma
, CTLFLAG_RW
, &ata_dma
, 0,
93 "ATA disk DMA mode control");
94 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma
);
95 SYSCTL_INT(_hw_ata
, OID_AUTO
, atapi_dma
, CTLFLAG_RW
, &atapi_dma
, 0,
96 "ATAPI device DMA mode control");
97 TUNABLE_INT("hw.ata.wc", &ata_wc
);
98 SYSCTL_INT(_hw_ata
, OID_AUTO
, ata_wc
, CTLFLAG_RW
, &ata_wc
, 0,
99 "ATA disk write caching");
102 * newbus device interface related functions
105 ata_probe(device_t dev
)
111 ata_attach(device_t dev
)
113 struct ata_channel
*ch
= device_get_softc(dev
);
116 /* check that we have a virgin channel to attach */
120 /* initialize the softc basics */
122 ch
->state
= ATA_IDLE
;
123 lockinit(&ch
->state_mtx
, "ataattach_state", 0, 0);
124 lockinit(&ch
->queue_mtx
, "ataattach_queue", 0, 0);
127 /* reset the controller HW, the channel and device(s) */
128 while (ATA_LOCKING(dev
, ATA_LF_LOCK
) != ch
->unit
)
129 tsleep(&error
, 0, "ataatch", 1);
131 ATA_LOCKING(dev
, ATA_LF_UNLOCK
);
133 /* setup interrupt delivery */
135 ch
->r_irq
= bus_alloc_resource_any(dev
, SYS_RES_IRQ
, &rid
,
136 RF_SHAREABLE
| RF_ACTIVE
);
138 device_printf(dev
, "unable to allocate interrupt\n");
141 if ((error
= bus_setup_intr(dev
, ch
->r_irq
, ATA_INTR_FLAGS
,
142 (driver_intr_t
*)ata_interrupt
, ch
, &ch
->ih
,
144 device_printf(dev
, "unable to setup interrupt\n");
148 /* probe and attach devices on this channel unless we are in early boot */
154 ata_detach(device_t dev
)
156 struct ata_channel
*ch
= device_get_softc(dev
);
160 /* check that we have a valid channel to detach */
164 /* grap the channel lock so no new requests gets launched */
165 lockmgr(&ch
->state_mtx
, LK_EXCLUSIVE
);
166 ch
->state
|= ATA_STALL_QUEUE
;
167 lockmgr(&ch
->state_mtx
, LK_RELEASE
);
169 /* detach & delete all children */
170 if (!device_get_children(dev
, &children
, &nchildren
)) {
171 for (i
= 0; i
< nchildren
; i
++)
173 device_delete_child(dev
, children
[i
]);
174 kfree(children
, M_TEMP
);
177 /* release resources */
178 bus_teardown_intr(dev
, ch
->r_irq
, ch
->ih
);
179 bus_release_resource(dev
, SYS_RES_IRQ
, ATA_IRQ_RID
, ch
->r_irq
);
181 lockuninit(&ch
->state_mtx
);
182 lockuninit(&ch
->queue_mtx
);
187 ata_reinit(device_t dev
)
189 struct ata_channel
*ch
= device_get_softc(dev
);
190 struct ata_request
*request
;
194 /* check that we have a valid channel to reinit */
195 if (!ch
|| !ch
->r_irq
)
199 device_printf(dev
, "reiniting channel ..\n");
201 /* poll for locking the channel */
202 while (ATA_LOCKING(dev
, ATA_LF_LOCK
) != ch
->unit
)
203 tsleep(&dev
, 0, "atarini", 1);
205 /* catch eventual request in ch->running */
206 lockmgr(&ch
->state_mtx
, LK_EXCLUSIVE
);
207 if ((request
= ch
->running
))
208 callout_stop_sync(&request
->callout
);
211 /* unconditionally grap the channel lock */
212 ch
->state
|= ATA_STALL_QUEUE
;
213 lockmgr(&ch
->state_mtx
, LK_RELEASE
);
215 /* reset the controller HW, the channel and device(s) */
218 /* reinit the children and delete any that fails */
219 if (!device_get_children(dev
, &children
, &nchildren
)) {
221 for (i
= 0; i
< nchildren
; i
++) {
222 /* did any children go missing ? */
223 if (children
[i
] && device_is_attached(children
[i
]) &&
224 ATA_REINIT(children
[i
])) {
226 * if we had a running request and its device matches
227 * this child we need to inform the request that the
230 if (request
&& request
->dev
== children
[i
]) {
231 request
->result
= ENXIO
;
232 device_printf(request
->dev
, "FAILURE - device detached\n");
234 /* if not timeout finish request here */
235 if (!(request
->flags
& ATA_R_TIMEOUT
))
239 device_delete_child(dev
, children
[i
]);
242 kfree(children
, M_TEMP
);
246 /* if we still have a good request put it on the queue again */
247 if (request
&& !(request
->flags
& ATA_R_TIMEOUT
)) {
248 device_printf(request
->dev
,
249 "WARNING - %s requeued due to channel reset",
250 ata_cmd2str(request
));
251 if (!(request
->flags
& (ATA_R_ATAPI
| ATA_R_CONTROL
)))
252 kprintf(" LBA=%ju", request
->u
.ata
.lba
);
254 request
->flags
|= ATA_R_REQUEUE
;
255 ata_queue_request(request
);
258 /* we're done release the channel for new work */
259 lockmgr(&ch
->state_mtx
, LK_EXCLUSIVE
);
260 ch
->state
= ATA_IDLE
;
261 lockmgr(&ch
->state_mtx
, LK_RELEASE
);
262 ATA_LOCKING(dev
, ATA_LF_UNLOCK
);
265 device_printf(dev
, "reinit done ..\n");
267 /* kick off requests on the queue */
273 ata_suspend(device_t dev
)
275 struct ata_channel
*ch
;
277 /* check for valid device */
278 if (!dev
|| !(ch
= device_get_softc(dev
)))
281 /* wait for the channel to be IDLE or detached before suspending */
283 lockmgr(&ch
->state_mtx
, LK_EXCLUSIVE
);
284 if (ch
->state
== ATA_IDLE
) {
285 ch
->state
= ATA_ACTIVE
;
286 lockmgr(&ch
->state_mtx
, LK_RELEASE
);
289 lockmgr(&ch
->state_mtx
, LK_RELEASE
);
290 tsleep(ch
, 0, "atasusp", hz
/10);
292 ATA_LOCKING(dev
, ATA_LF_UNLOCK
);
297 ata_resume(device_t dev
)
299 struct ata_channel
*ch
;
302 /* check for valid device */
303 if (!dev
|| !(ch
= device_get_softc(dev
)))
306 /* reinit the devices, we dont know what mode/state they are in */
307 error
= ata_reinit(dev
);
309 /* kick off requests on the queue */
315 ata_interrupt(void *data
)
317 struct ata_channel
*ch
= (struct ata_channel
*)data
;
318 struct ata_request
*request
;
320 lockmgr(&ch
->state_mtx
, LK_EXCLUSIVE
);
323 * Ignore interrupt if its not for us. This may also have the
324 * side effect of processing events unrelated to I/O requests.
326 if (ch
->hw
.status
&& !ch
->hw
.status(ch
->dev
))
330 * Check if we have a running request, and make sure it has been
331 * completely queued. Otherwise the channel status may indicate
332 * not-busy when, in fact, the command had not yet been issued.
334 if ((request
= ch
->running
) == NULL
)
336 if ((request
->flags
& ATA_R_HWCMDQUEUED
) == 0) {
337 kprintf("ata_interrupt: early interrupt\n");
341 ATA_DEBUG_RQ(request
, "interrupt");
343 /* safetycheck for the right state */
344 if (ch
->state
== ATA_IDLE
) {
345 device_printf(request
->dev
, "interrupt on idle channel ignored\n");
350 * we have the HW locks, so end the transaction for this request
351 * if it finishes immediately otherwise wait for next interrupt
353 if (ch
->hw
.end_transaction(request
) == ATA_OP_FINISHED
) {
355 if (ch
->state
== ATA_ACTIVE
)
356 ch
->state
= ATA_IDLE
;
357 lockmgr(&ch
->state_mtx
, LK_RELEASE
);
358 ATA_LOCKING(ch
->dev
, ATA_LF_UNLOCK
);
363 lockmgr(&ch
->state_mtx
, LK_RELEASE
);
368 * device related interfaces
371 ata_ioctl(struct dev_ioctl_args
*ap
)
373 device_t device
, *children
;
374 struct ata_ioc_devices
*devices
= (struct ata_ioc_devices
*)ap
->a_data
;
375 int *value
= (int *)ap
->a_data
;
376 int i
, nchildren
, error
= ENOTTY
;
379 case IOCATAGMAXCHANNEL
:
380 *value
= devclass_get_maxunit(ata_devclass
);
385 if (*value
> devclass_get_maxunit(ata_devclass
) ||
386 !(device
= devclass_get_device(ata_devclass
, *value
)))
388 error
= ata_reinit(device
);
393 if (*value
> devclass_get_maxunit(ata_devclass
) ||
394 !(device
= devclass_get_device(ata_devclass
, *value
)))
396 /* XXX SOS should enable channel HW on controller */
397 error
= ata_attach(device
);
401 if (*value
> devclass_get_maxunit(ata_devclass
) ||
402 !(device
= devclass_get_device(ata_devclass
, *value
)))
404 error
= ata_detach(device
);
405 /* XXX SOS should disable channel HW on controller */
409 if (devices
->channel
> devclass_get_maxunit(ata_devclass
) ||
410 !(device
= devclass_get_device(ata_devclass
, devices
->channel
)))
412 bzero(devices
->name
[0], 32);
413 bzero(&devices
->params
[0], sizeof(struct ata_params
));
414 bzero(devices
->name
[1], 32);
415 bzero(&devices
->params
[1], sizeof(struct ata_params
));
416 if (!device_get_children(device
, &children
, &nchildren
)) {
417 for (i
= 0; i
< nchildren
; i
++) {
418 if (children
[i
] && device_is_attached(children
[i
])) {
419 struct ata_device
*atadev
= device_get_softc(children
[i
]);
421 if (atadev
->unit
== ATA_MASTER
) {
422 strncpy(devices
->name
[0],
423 device_get_nameunit(children
[i
]), 32);
424 bcopy(&atadev
->param
, &devices
->params
[0],
425 sizeof(struct ata_params
));
427 if (atadev
->unit
== ATA_SLAVE
) {
428 strncpy(devices
->name
[1],
429 device_get_nameunit(children
[i
]), 32);
430 bcopy(&atadev
->param
, &devices
->params
[1],
431 sizeof(struct ata_params
));
435 kfree(children
, M_TEMP
);
443 if (ata_raid_ioctl_func
)
444 error
= ata_raid_ioctl_func(ap
->a_cmd
, ap
->a_data
);
450 ata_device_ioctl(device_t dev
, u_long cmd
, caddr_t data
)
452 struct ata_device
*atadev
= device_get_softc(dev
);
453 struct ata_ioc_request
*ioc_request
= (struct ata_ioc_request
*)data
;
454 struct ata_params
*params
= (struct ata_params
*)data
;
455 int *mode
= (int *)data
;
456 struct ata_request
*request
;
462 if (!(buf
= kmalloc(ioc_request
->count
, M_ATA
, M_WAITOK
| M_NULLOK
))) {
465 if (!(request
= ata_alloc_request())) {
469 if (ioc_request
->flags
& ATA_CMD_WRITE
) {
470 error
= copyin(ioc_request
->data
, buf
, ioc_request
->count
);
473 ata_free_request(request
);
478 if (ioc_request
->flags
& ATA_CMD_ATAPI
) {
479 request
->flags
= ATA_R_ATAPI
;
480 bcopy(ioc_request
->u
.atapi
.ccb
, request
->u
.atapi
.ccb
, 16);
483 request
->u
.ata
.command
= ioc_request
->u
.ata
.command
;
484 request
->u
.ata
.feature
= ioc_request
->u
.ata
.feature
;
485 request
->u
.ata
.lba
= ioc_request
->u
.ata
.lba
;
486 request
->u
.ata
.count
= ioc_request
->u
.ata
.count
;
488 request
->timeout
= ioc_request
->timeout
;
490 request
->bytecount
= ioc_request
->count
;
491 request
->transfersize
= request
->bytecount
;
492 if (ioc_request
->flags
& ATA_CMD_CONTROL
)
493 request
->flags
|= ATA_R_CONTROL
;
494 if (ioc_request
->flags
& ATA_CMD_READ
)
495 request
->flags
|= ATA_R_READ
;
496 if (ioc_request
->flags
& ATA_CMD_WRITE
)
497 request
->flags
|= ATA_R_WRITE
;
498 ata_queue_request(request
);
499 if (request
->flags
& ATA_R_ATAPI
) {
500 bcopy(&request
->u
.atapi
.sense
, &ioc_request
->u
.atapi
.sense
,
501 sizeof(struct atapi_sense
));
504 ioc_request
->u
.ata
.command
= request
->u
.ata
.command
;
505 ioc_request
->u
.ata
.feature
= request
->u
.ata
.feature
;
506 ioc_request
->u
.ata
.lba
= request
->u
.ata
.lba
;
507 ioc_request
->u
.ata
.count
= request
->u
.ata
.count
;
509 ioc_request
->error
= request
->result
;
510 if (ioc_request
->flags
& ATA_CMD_READ
)
511 error
= copyout(buf
, ioc_request
->data
, ioc_request
->count
);
515 ata_free_request(request
);
519 ata_getparam(atadev
, 0);
520 bcopy(&atadev
->param
, params
, sizeof(struct ata_params
));
524 atadev
->mode
= *mode
;
525 ATA_SETMODE(device_get_parent(dev
), dev
);
529 *mode
= atadev
->mode
;
539 ata_boot_attach(void)
541 struct ata_channel
*ch
;
546 /* kick of probe and attach on all channels */
547 for (ctlr
= 0; ctlr
< devclass_get_maxunit(ata_devclass
); ctlr
++) {
548 if ((ch
= devclass_get_softc(ata_devclass
, ctlr
))) {
549 ata_identify(ch
->dev
);
559 ata_print_cable(device_t dev
, u_int8_t
*who
)
562 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who
);
566 * misc support functions
569 ata_add_child(device_t parent
, struct ata_device
*atadev
, int unit
)
573 if ((child
= device_add_child(parent
, NULL
, unit
))) {
574 device_set_softc(child
, atadev
);
577 atadev
->max_iosize
= DEV_BSIZE
;
578 atadev
->mode
= ATA_PIO_MAX
;
584 ata_getparam(struct ata_device
*atadev
, int init
)
586 struct ata_channel
*ch
= device_get_softc(device_get_parent(atadev
->dev
));
587 struct ata_request
*request
;
588 u_int8_t command
= 0;
589 int error
= ENOMEM
, retries
= 2;
592 (atadev
->unit
== ATA_MASTER
? ATA_ATA_MASTER
: ATA_ATA_SLAVE
))
593 command
= ATA_ATA_IDENTIFY
;
595 (atadev
->unit
== ATA_MASTER
? ATA_ATAPI_MASTER
: ATA_ATAPI_SLAVE
))
596 command
= ATA_ATAPI_IDENTIFY
;
600 while (retries
-- > 0 && error
) {
601 if (!(request
= ata_alloc_request()))
603 request
->dev
= atadev
->dev
;
604 request
->timeout
= 1;
605 request
->retries
= 0;
606 request
->u
.ata
.command
= command
;
607 request
->flags
= (ATA_R_READ
|ATA_R_AT_HEAD
|ATA_R_DIRECT
|ATA_R_QUIET
);
608 request
->data
= (void *)&atadev
->param
;
609 request
->bytecount
= sizeof(struct ata_params
);
610 request
->donecount
= 0;
611 request
->transfersize
= DEV_BSIZE
;
612 ata_queue_request(request
);
613 error
= request
->result
;
614 ata_free_request(request
);
617 if (!error
&& (isprint(atadev
->param
.model
[0]) ||
618 isprint(atadev
->param
.model
[1]))) {
619 struct ata_params
*atacap
= &atadev
->param
;
622 for (ptr
= (int16_t *)atacap
;
623 ptr
< (int16_t *)atacap
+ sizeof(struct ata_params
)/2; ptr
++) {
624 *ptr
= le16toh(*ptr
);
626 if (!(!strncmp(atacap
->model
, "FX", 2) ||
627 !strncmp(atacap
->model
, "NEC", 3) ||
628 !strncmp(atacap
->model
, "Pioneer", 7) ||
629 !strncmp(atacap
->model
, "SHARP", 5))) {
630 bswap(atacap
->model
, sizeof(atacap
->model
));
631 bswap(atacap
->revision
, sizeof(atacap
->revision
));
632 bswap(atacap
->serial
, sizeof(atacap
->serial
));
634 btrim(atacap
->model
, sizeof(atacap
->model
));
635 bpack(atacap
->model
, atacap
->model
, sizeof(atacap
->model
));
636 btrim(atacap
->revision
, sizeof(atacap
->revision
));
637 bpack(atacap
->revision
, atacap
->revision
, sizeof(atacap
->revision
));
638 btrim(atacap
->serial
, sizeof(atacap
->serial
));
639 bpack(atacap
->serial
, atacap
->serial
, sizeof(atacap
->serial
));
642 kprintf("ata%d-%s: pio=%s wdma=%s udma=%s cable=%s wire\n",
643 device_get_unit(ch
->dev
),
644 atadev
->unit
== ATA_MASTER
? "master" : "slave",
645 ata_mode2str(ata_pmode(atacap
)),
646 ata_mode2str(ata_wmode(atacap
)),
647 ata_mode2str(ata_umode(atacap
)),
648 (atacap
->hwres
& ATA_CABLE_ID
) ? "80":"40");
653 ksprintf(buffer
, "%.40s/%.8s", atacap
->model
, atacap
->revision
);
654 device_set_desc_copy(atadev
->dev
, buffer
);
655 if ((atadev
->param
.config
& ATA_PROTO_ATAPI
) &&
656 (atadev
->param
.config
!= ATA_CFA_MAGIC1
) &&
657 (atadev
->param
.config
!= ATA_CFA_MAGIC2
)) {
658 if (atapi_dma
&& ch
->dma
&&
659 (atadev
->param
.config
& ATA_DRQ_MASK
) != ATA_DRQ_INTR
&&
660 ata_umode(&atadev
->param
) >= ATA_UDMA2
)
661 atadev
->mode
= ATA_DMA_MAX
;
664 if (ata_dma
&& ch
->dma
&&
665 (ata_umode(&atadev
->param
) > 0 ||
666 ata_wmode(&atadev
->param
) > 0))
667 atadev
->mode
= ATA_DMA_MAX
;
679 ata_identify(device_t dev
)
681 struct ata_channel
*ch
= device_get_softc(dev
);
682 struct ata_device
*master
= NULL
, *slave
= NULL
;
683 device_t master_child
= NULL
, slave_child
= NULL
;
684 int master_unit
= -1, slave_unit
= -1;
686 if (ch
->devices
& (ATA_ATA_MASTER
| ATA_ATAPI_MASTER
)) {
687 if (!(master
= kmalloc(sizeof(struct ata_device
),
688 M_ATA
, M_INTWAIT
| M_ZERO
))) {
689 device_printf(dev
, "out of memory\n");
692 master
->unit
= ATA_MASTER
;
694 if (ch
->devices
& (ATA_ATA_SLAVE
| ATA_ATAPI_SLAVE
)) {
695 if (!(slave
= kmalloc(sizeof(struct ata_device
),
696 M_ATA
, M_INTWAIT
| M_ZERO
))) {
697 kfree(master
, M_ATA
);
698 device_printf(dev
, "out of memory\n");
701 slave
->unit
= ATA_SLAVE
;
705 if (ch
->devices
& ATA_ATA_MASTER
)
706 master_unit
= (device_get_unit(dev
) << 1);
708 if (master
&& !(master_child
= ata_add_child(dev
, master
, master_unit
))) {
709 kfree(master
, M_ATA
);
713 if (ch
->devices
& ATA_ATA_SLAVE
)
714 slave_unit
= (device_get_unit(dev
) << 1) + 1;
716 if (slave
&& !(slave_child
= ata_add_child(dev
, slave
, slave_unit
))) {
721 if (slave
&& ata_getparam(slave
, 1)) {
722 device_delete_child(dev
, slave_child
);
725 if (master
&& ata_getparam(master
, 1)) {
726 device_delete_child(dev
, master_child
);
727 kfree(master
, M_ATA
);
730 bus_generic_probe(dev
);
731 bus_generic_attach(dev
);
736 ata_default_registers(device_t dev
)
738 struct ata_channel
*ch
= device_get_softc(dev
);
740 /* fill in the defaults from whats setup already */
741 ch
->r_io
[ATA_ERROR
].res
= ch
->r_io
[ATA_FEATURE
].res
;
742 ch
->r_io
[ATA_ERROR
].offset
= ch
->r_io
[ATA_FEATURE
].offset
;
743 ch
->r_io
[ATA_IREASON
].res
= ch
->r_io
[ATA_COUNT
].res
;
744 ch
->r_io
[ATA_IREASON
].offset
= ch
->r_io
[ATA_COUNT
].offset
;
745 ch
->r_io
[ATA_STATUS
].res
= ch
->r_io
[ATA_COMMAND
].res
;
746 ch
->r_io
[ATA_STATUS
].offset
= ch
->r_io
[ATA_COMMAND
].offset
;
747 ch
->r_io
[ATA_ALTSTAT
].res
= ch
->r_io
[ATA_CONTROL
].res
;
748 ch
->r_io
[ATA_ALTSTAT
].offset
= ch
->r_io
[ATA_CONTROL
].offset
;
752 ata_modify_if_48bit(struct ata_request
*request
)
754 struct ata_channel
*ch
= device_get_softc(device_get_parent(request
->dev
));
755 struct ata_device
*atadev
= device_get_softc(request
->dev
);
757 atadev
->flags
&= ~ATA_D_48BIT_ACTIVE
;
759 if (((request
->u
.ata
.lba
+ request
->u
.ata
.count
) >= ATA_MAX_28BIT_LBA
||
760 request
->u
.ata
.count
> 256) &&
761 atadev
->param
.support
.command2
& ATA_SUPPORT_ADDRESS48
) {
763 /* translate command into 48bit version */
764 switch (request
->u
.ata
.command
) {
766 request
->u
.ata
.command
= ATA_READ48
;
769 request
->u
.ata
.command
= ATA_READ_MUL48
;
772 if (ch
->flags
& ATA_NO_48BIT_DMA
) {
773 if (request
->transfersize
> DEV_BSIZE
)
774 request
->u
.ata
.command
= ATA_READ_MUL48
;
776 request
->u
.ata
.command
= ATA_READ48
;
777 request
->flags
&= ~ATA_R_DMA
;
780 request
->u
.ata
.command
= ATA_READ_DMA48
;
782 case ATA_READ_DMA_QUEUED
:
783 if (ch
->flags
& ATA_NO_48BIT_DMA
) {
784 if (request
->transfersize
> DEV_BSIZE
)
785 request
->u
.ata
.command
= ATA_READ_MUL48
;
787 request
->u
.ata
.command
= ATA_READ48
;
788 request
->flags
&= ~ATA_R_DMA
;
791 request
->u
.ata
.command
= ATA_READ_DMA_QUEUED48
;
794 request
->u
.ata
.command
= ATA_WRITE48
;
797 request
->u
.ata
.command
= ATA_WRITE_MUL48
;
800 if (ch
->flags
& ATA_NO_48BIT_DMA
) {
801 if (request
->transfersize
> DEV_BSIZE
)
802 request
->u
.ata
.command
= ATA_WRITE_MUL48
;
804 request
->u
.ata
.command
= ATA_WRITE48
;
805 request
->flags
&= ~ATA_R_DMA
;
808 request
->u
.ata
.command
= ATA_WRITE_DMA48
;
810 case ATA_WRITE_DMA_QUEUED
:
811 if (ch
->flags
& ATA_NO_48BIT_DMA
) {
812 if (request
->transfersize
> DEV_BSIZE
)
813 request
->u
.ata
.command
= ATA_WRITE_MUL48
;
815 request
->u
.ata
.command
= ATA_WRITE48
;
816 request
->u
.ata
.command
= ATA_WRITE48
;
817 request
->flags
&= ~ATA_R_DMA
;
820 request
->u
.ata
.command
= ATA_WRITE_DMA_QUEUED48
;
823 request
->u
.ata
.command
= ATA_FLUSHCACHE48
;
825 case ATA_READ_NATIVE_MAX_ADDRESS
:
826 request
->u
.ata
.command
= ATA_READ_NATIVE_MAX_ADDRESS48
;
828 case ATA_SET_MAX_ADDRESS
:
829 request
->u
.ata
.command
= ATA_SET_MAX_ADDRESS48
;
834 atadev
->flags
|= ATA_D_48BIT_ACTIVE
;
839 ata_udelay(int interval
)
842 * We can't use tsleep here, because we might be called from callout
845 if (1 || interval
< (1000000/hz
))
848 tsleep(&interval
, 0, "ataslp", 1 + interval
/ (1000000 / hz
));
852 ata_mode2str(int mode
)
855 case -1: return "UNSUPPORTED";
856 case ATA_PIO0
: return "PIO0";
857 case ATA_PIO1
: return "PIO1";
858 case ATA_PIO2
: return "PIO2";
859 case ATA_PIO3
: return "PIO3";
860 case ATA_PIO4
: return "PIO4";
861 case ATA_WDMA0
: return "WDMA0";
862 case ATA_WDMA1
: return "WDMA1";
863 case ATA_WDMA2
: return "WDMA2";
864 case ATA_UDMA0
: return "UDMA16";
865 case ATA_UDMA1
: return "UDMA25";
866 case ATA_UDMA2
: return "UDMA33";
867 case ATA_UDMA3
: return "UDMA40";
868 case ATA_UDMA4
: return "UDMA66";
869 case ATA_UDMA5
: return "UDMA100";
870 case ATA_UDMA6
: return "UDMA133";
871 case ATA_SA150
: return "SATA150";
872 case ATA_SA300
: return "SATA300";
873 case ATA_USB
: return "USB";
874 case ATA_USB1
: return "USB1";
875 case ATA_USB2
: return "USB2";
877 if (mode
& ATA_DMA_MASK
)
885 ata_atapi(device_t dev
)
887 struct ata_channel
*ch
= device_get_softc(device_get_parent(dev
));
888 struct ata_device
*atadev
= device_get_softc(dev
);
890 return ((atadev
->unit
== ATA_MASTER
&& ch
->devices
& ATA_ATAPI_MASTER
) ||
891 (atadev
->unit
== ATA_SLAVE
&& ch
->devices
& ATA_ATAPI_SLAVE
));
895 ata_pmode(struct ata_params
*ap
)
897 if (ap
->atavalid
& ATA_FLAG_64_70
) {
898 if (ap
->apiomodes
& 0x02)
900 if (ap
->apiomodes
& 0x01)
903 if (ap
->mwdmamodes
& 0x04)
905 if (ap
->mwdmamodes
& 0x02)
907 if (ap
->mwdmamodes
& 0x01)
909 if ((ap
->retired_piomode
& ATA_RETIRED_PIO_MASK
) == 0x200)
911 if ((ap
->retired_piomode
& ATA_RETIRED_PIO_MASK
) == 0x100)
913 if ((ap
->retired_piomode
& ATA_RETIRED_PIO_MASK
) == 0x000)
919 ata_wmode(struct ata_params
*ap
)
921 if (ap
->mwdmamodes
& 0x04)
923 if (ap
->mwdmamodes
& 0x02)
925 if (ap
->mwdmamodes
& 0x01)
931 ata_umode(struct ata_params
*ap
)
933 if (ap
->atavalid
& ATA_FLAG_88
) {
934 if (ap
->udmamodes
& 0x40)
936 if (ap
->udmamodes
& 0x20)
938 if (ap
->udmamodes
& 0x10)
940 if (ap
->udmamodes
& 0x08)
942 if (ap
->udmamodes
& 0x04)
944 if (ap
->udmamodes
& 0x02)
946 if (ap
->udmamodes
& 0x01)
953 ata_limit_mode(device_t dev
, int mode
, int maxmode
)
955 struct ata_device
*atadev
= device_get_softc(dev
);
957 if (maxmode
&& mode
> maxmode
)
960 if (mode
>= ATA_UDMA0
&& ata_umode(&atadev
->param
) > 0)
961 return min(mode
, ata_umode(&atadev
->param
));
963 if (mode
>= ATA_WDMA0
&& ata_wmode(&atadev
->param
) > 0)
964 return min(mode
, ata_wmode(&atadev
->param
));
966 if (mode
> ata_pmode(&atadev
->param
))
967 return min(mode
, ata_pmode(&atadev
->param
));
973 bswap(int8_t *buf
, int len
)
975 u_int16_t
*ptr
= (u_int16_t
*)(buf
+ len
);
977 while (--ptr
>= (u_int16_t
*)buf
)
982 btrim(int8_t *buf
, int len
)
986 for (ptr
= buf
; ptr
< buf
+len
; ++ptr
)
987 if (!*ptr
|| *ptr
== '_')
989 for (ptr
= buf
+ len
- 1; ptr
>= buf
&& *ptr
== ' '; --ptr
)
994 bpack(int8_t *src
, int8_t *dst
, int len
)
998 for (i
= j
= blank
= 0 ; i
< len
; i
++) {
999 if (blank
&& src
[i
] == ' ') continue;
1000 if (blank
&& src
[i
] != ' ') {
1005 if (src
[i
] == ' ') {
1021 ata_module_event_handler(module_t mod
, int what
, void *arg
)
1023 /* static because we need the reference at destruction time */
1024 static cdev_t atacdev
;
1028 /* register controlling device */
1029 atacdev
= make_dev(&ata_ops
, 0, UID_ROOT
, GID_OPERATOR
, 0600, "ata");
1030 reference_dev(atacdev
);
1034 /* deregister controlling device */
1035 destroy_dev(atacdev
);
1036 dev_ops_remove_all(&ata_ops
);
1044 static moduledata_t ata_moduledata
= { "ata", ata_module_event_handler
, NULL
};
1045 DECLARE_MODULE(ata
, ata_moduledata
, SI_SUB_CONFIGURE
, SI_ORDER_SECOND
);
1046 MODULE_VERSION(ata
, 1);
1049 * Construct a completely zero'ed ata_request. On objcache_put(), an
1050 * ata_request object is also zero'ed, so objcache_get() is guaranteed to give
1051 * completely zero'ed objects without spending too much time.
1054 ata_request_cache_ctor(void *obj
, void *private, int ocflags
)
1056 struct ata_request
*arp
= obj
;
1058 bzero(arp
, sizeof(struct ata_request
));
1063 * Construct a completely zero'ed ata_composite. On objcache_put(), an
1064 * ata_composite object is also zero'ed, so objcache_get() is guaranteed to give
1065 * completely zero'ed objects without spending too much time.
1068 ata_composite_cache_ctor(void *obj
, void *private, int ocflags
)
1070 struct ata_composite
*acp
= obj
;
1072 bzero(acp
, sizeof(struct ata_composite
));
1079 ata_request_cache
= objcache_create("ata_request", 0, 0,
1080 ata_request_cache_ctor
, NULL
, NULL
,
1081 objcache_malloc_alloc
,
1082 objcache_malloc_free
,
1083 &ata_request_malloc_args
);
1084 ata_composite_cache
= objcache_create("ata_composite", 0, 0,
1085 ata_composite_cache_ctor
, NULL
, NULL
,
1086 objcache_malloc_alloc
,
1087 objcache_malloc_free
,
1088 &ata_composite_malloc_args
);
1090 SYSINIT(ata_register
, SI_SUB_DRIVERS
, SI_ORDER_SECOND
, ata_init
, NULL
);
1095 objcache_destroy(ata_composite_cache
);
1096 objcache_destroy(ata_request_cache
);
1098 SYSUNINIT(ata_unregister
, SI_SUB_DRIVERS
, SI_ORDER_SECOND
, ata_uninit
, NULL
);