2 * Copyright (c) 1998 - 2008 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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/endian.h>
37 #include <sys/ctype.h>
40 #include <sys/taskqueue.h>
42 #include <machine/bus.h>
44 #include <dev/ata/ata-all.h>
45 #include <dev/ata/ata-pci.h>
49 static int ata_generic_status(device_t dev);
50 static int ata_wait(struct ata_channel *ch, int unit, u_int8_t);
51 static void ata_pio_read(struct ata_request *, int);
52 static void ata_pio_write(struct ata_request *, int);
53 static void ata_tf_read(struct ata_request *);
54 static void ata_tf_write(struct ata_request *);
57 * low level ATA functions
60 ata_generic_hw(device_t dev)
62 struct ata_channel *ch = device_get_softc(dev);
64 ch->hw.begin_transaction = ata_begin_transaction;
65 ch->hw.end_transaction = ata_end_transaction;
66 ch->hw.status = ata_generic_status;
67 ch->hw.softreset = NULL;
68 ch->hw.command = ata_generic_command;
69 ch->hw.tf_read = ata_tf_read;
70 ch->hw.tf_write = ata_tf_write;
71 ch->hw.pm_read = NULL;
72 ch->hw.pm_write = NULL;
75 /* must be called with ATA channel locked and state_mtx held */
77 ata_begin_transaction(struct ata_request *request)
79 struct ata_channel *ch = device_get_softc(request->parent);
82 ATA_DEBUG_RQ(request, "begin transaction");
84 /* disable ATAPI DMA writes if HW doesn't support it */
85 if ((ch->flags & ATA_NO_ATAPI_DMA) &&
86 (request->flags & ATA_R_ATAPI) == ATA_R_ATAPI)
87 request->flags &= ~ATA_R_DMA;
88 if ((ch->flags & ATA_ATAPI_DMA_RO) &&
89 ((request->flags & (ATA_R_ATAPI | ATA_R_DMA | ATA_R_WRITE)) ==
90 (ATA_R_ATAPI | ATA_R_DMA | ATA_R_WRITE)))
91 request->flags &= ~ATA_R_DMA;
93 switch (request->flags & (ATA_R_ATAPI | ATA_R_DMA)) {
95 /* ATA PIO data transfer and control commands */
98 /* record command direction here as our request might be gone later */
99 int write = (request->flags & ATA_R_WRITE);
102 if (ch->hw.command(request)) {
103 device_printf(request->parent, "error issuing %s command\n",
104 ata_cmd2str(request));
105 request->result = EIO;
109 /* device reset doesn't interrupt */
110 if (request->u.ata.command == ATA_DEVICE_RESET) {
112 int timeout = 1000000;
115 request->status = ATA_IDX_INB(ch, ATA_STATUS);
116 } while (request->status & ATA_S_BUSY && timeout--);
117 if (request->status & ATA_S_ERROR)
118 request->error = ATA_IDX_INB(ch, ATA_ERROR);
119 ch->hw.tf_read(request);
123 /* if write command output the data */
125 if (ata_wait(ch, request->unit, (ATA_S_READY | ATA_S_DRQ)) < 0) {
126 device_printf(request->parent,
127 "timeout waiting for write DRQ\n");
128 request->result = EIO;
131 ata_pio_write(request, request->transfersize);
136 /* ATA DMA data transfer commands */
138 /* check sanity, setup SG list and DMA engine */
139 if ((error = ch->dma.load(request, NULL, &dummy))) {
140 device_printf(request->parent, "setting up DMA failed\n");
141 request->result = error;
145 /* start DMA engine if necessary */
146 if ((ch->flags & ATA_DMA_BEFORE_CMD) &&
147 ch->dma.start && ch->dma.start(request)) {
148 device_printf(request->parent, "error starting DMA\n");
149 request->result = EIO;
154 if (ch->hw.command(request)) {
155 device_printf(request->parent, "error issuing %s command\n",
156 ata_cmd2str(request));
157 request->result = EIO;
161 /* start DMA engine */
162 if (!(ch->flags & ATA_DMA_BEFORE_CMD) &&
163 ch->dma.start && ch->dma.start(request)) {
164 device_printf(request->parent, "error starting DMA\n");
165 request->result = EIO;
170 /* ATAPI PIO commands */
172 /* is this just a POLL DSC command ? */
173 if (request->u.atapi.ccb[0] == ATAPI_POLL_DSC) {
174 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(request->unit));
176 if (!(ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_DSC))
177 request->result = EBUSY;
181 /* start ATAPI operation */
182 if (ch->hw.command(request)) {
183 device_printf(request->parent, "error issuing ATA PACKET command\n");
184 request->result = EIO;
189 /* ATAPI DMA commands */
190 case ATA_R_ATAPI|ATA_R_DMA:
191 /* is this just a POLL DSC command ? */
192 if (request->u.atapi.ccb[0] == ATAPI_POLL_DSC) {
193 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(request->unit));
195 if (!(ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_DSC))
196 request->result = EBUSY;
200 /* check sanity, setup SG list and DMA engine */
201 if ((error = ch->dma.load(request, NULL, &dummy))) {
202 device_printf(request->parent, "setting up DMA failed\n");
203 request->result = error;
207 /* start ATAPI operation */
208 if (ch->hw.command(request)) {
209 device_printf(request->parent, "error issuing ATA PACKET command\n");
210 request->result = EIO;
214 /* start DMA engine */
215 if (ch->dma.start && ch->dma.start(request)) {
216 request->result = EIO;
222 printf("ata_begin_transaction OOPS!!!\n");
225 if (ch->dma.unload) {
226 ch->dma.unload(request);
228 return ATA_OP_FINISHED;
231 callout_reset(&request->callout, request->timeout * hz,
232 (timeout_t*)ata_timeout, request);
233 return ATA_OP_CONTINUES;
236 /* must be called with ATA channel locked and state_mtx held */
238 ata_end_transaction(struct ata_request *request)
240 struct ata_channel *ch = device_get_softc(request->parent);
243 ATA_DEBUG_RQ(request, "end transaction");
245 /* clear interrupt and get status */
246 request->status = ATA_IDX_INB(ch, ATA_STATUS);
248 switch (request->flags & (ATA_R_ATAPI | ATA_R_DMA | ATA_R_CONTROL)) {
250 /* ATA PIO data transfer and control commands */
253 /* on timeouts we have no data or anything so just return */
254 if (request->flags & ATA_R_TIMEOUT)
257 /* Read back registers to the request struct. */
258 if ((request->status & ATA_S_ERROR) ||
259 (request->flags & (ATA_R_CONTROL | ATA_R_NEEDRESULT))) {
260 ch->hw.tf_read(request);
263 /* if we got an error we are done with the HW */
264 if (request->status & ATA_S_ERROR) {
265 request->error = ATA_IDX_INB(ch, ATA_ERROR);
269 /* are we moving data ? */
270 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
272 /* if read data get it */
273 if (request->flags & ATA_R_READ) {
274 int flags = ATA_S_DRQ;
276 if (request->u.ata.command != ATA_ATAPI_IDENTIFY)
277 flags |= ATA_S_READY;
278 if (ata_wait(ch, request->unit, flags) < 0) {
279 device_printf(request->parent,
280 "timeout waiting for read DRQ\n");
281 request->result = EIO;
284 ata_pio_read(request, request->transfersize);
287 /* update how far we've gotten */
288 request->donecount += request->transfersize;
290 /* do we need a scoop more ? */
291 if (request->bytecount > request->donecount) {
293 /* set this transfer size according to HW capabilities */
294 request->transfersize =
295 min((request->bytecount - request->donecount),
296 request->transfersize);
298 /* if data write command, output the data */
299 if (request->flags & ATA_R_WRITE) {
301 /* if we get an error here we are done with the HW */
302 if (ata_wait(ch, request->unit, (ATA_S_READY | ATA_S_DRQ)) < 0) {
303 device_printf(request->parent,
304 "timeout waiting for write DRQ\n");
305 request->status = ATA_IDX_INB(ch, ATA_STATUS);
309 /* output data and return waiting for new interrupt */
310 ata_pio_write(request, request->transfersize);
314 /* if data read command, return & wait for interrupt */
315 if (request->flags & ATA_R_READ)
322 /* ATA DMA data transfer commands */
325 /* stop DMA engine and get status */
327 request->dma->status = ch->dma.stop(request);
329 /* did we get error or data */
330 if (request->status & ATA_S_ERROR)
331 request->error = ATA_IDX_INB(ch, ATA_ERROR);
332 else if (request->dma->status & ATA_BMSTAT_ERROR)
333 request->status |= ATA_S_ERROR;
334 else if (!(request->flags & ATA_R_TIMEOUT))
335 request->donecount = request->bytecount;
337 /* Read back registers to the request struct. */
338 if ((request->status & ATA_S_ERROR) ||
339 (request->flags & (ATA_R_CONTROL | ATA_R_NEEDRESULT))) {
340 ch->hw.tf_read(request);
343 /* release SG list etc */
344 ch->dma.unload(request);
349 /* ATAPI PIO commands */
351 length = ATA_IDX_INB(ch, ATA_CYL_LSB)|(ATA_IDX_INB(ch, ATA_CYL_MSB)<<8);
353 /* on timeouts we have no data or anything so just return */
354 if (request->flags & ATA_R_TIMEOUT)
357 switch ((ATA_IDX_INB(ch, ATA_IREASON) & (ATA_I_CMD | ATA_I_IN)) |
358 (request->status & ATA_S_DRQ)) {
361 /* this seems to be needed for some (slow) devices */
364 if (!(request->status & ATA_S_DRQ)) {
365 device_printf(request->parent, "command interrupt without DRQ\n");
366 request->status = ATA_S_ERROR;
369 ATA_IDX_OUTSW_STRM(ch, ATA_DATA, (int16_t *)request->u.atapi.ccb,
370 (request->flags & ATA_R_ATAPI16) ? 8 : 6);
371 /* return wait for interrupt */
375 if (request->flags & ATA_R_READ) {
376 request->status = ATA_S_ERROR;
377 device_printf(request->parent,
378 "%s trying to write on read buffer\n",
379 ata_cmd2str(request));
383 ata_pio_write(request, length);
384 request->donecount += length;
386 /* set next transfer size according to HW capabilities */
387 request->transfersize = min((request->bytecount-request->donecount),
388 request->transfersize);
389 /* return wait for interrupt */
393 if (request->flags & ATA_R_WRITE) {
394 request->status = ATA_S_ERROR;
395 device_printf(request->parent,
396 "%s trying to read on write buffer\n",
397 ata_cmd2str(request));
400 ata_pio_read(request, length);
401 request->donecount += length;
403 /* set next transfer size according to HW capabilities */
404 request->transfersize = min((request->bytecount-request->donecount),
405 request->transfersize);
406 /* return wait for interrupt */
409 case ATAPI_P_DONEDRQ:
410 device_printf(request->parent,
411 "WARNING - %s DONEDRQ non conformant device\n",
412 ata_cmd2str(request));
413 if (request->flags & ATA_R_READ) {
414 ata_pio_read(request, length);
415 request->donecount += length;
417 else if (request->flags & ATA_R_WRITE) {
418 ata_pio_write(request, length);
419 request->donecount += length;
422 request->status = ATA_S_ERROR;
427 if (request->status & (ATA_S_ERROR | ATA_S_DWF))
428 request->error = ATA_IDX_INB(ch, ATA_ERROR);
432 device_printf(request->parent, "unknown transfer phase\n");
433 request->status = ATA_S_ERROR;
439 /* ATAPI DMA commands */
440 case ATA_R_ATAPI|ATA_R_DMA:
442 /* stop DMA engine and get status */
444 request->dma->status = ch->dma.stop(request);
446 /* did we get error or data */
447 if (request->status & (ATA_S_ERROR | ATA_S_DWF))
448 request->error = ATA_IDX_INB(ch, ATA_ERROR);
449 else if (request->dma->status & ATA_BMSTAT_ERROR)
450 request->status |= ATA_S_ERROR;
451 else if (!(request->flags & ATA_R_TIMEOUT))
452 request->donecount = request->bytecount;
454 /* release SG list etc */
455 ch->dma.unload(request);
461 printf("ata_end_transaction OOPS!!\n");
464 callout_stop(&request->callout);
465 return ATA_OP_FINISHED;
468 return ATA_OP_CONTINUES;
471 /* must be called with ATA channel locked and state_mtx held */
473 ata_generic_reset(device_t dev)
475 struct ata_channel *ch = device_get_softc(dev);
477 u_int8_t ostat0 = 0, stat0 = 0, ostat1 = 0, stat1 = 0;
478 u_int8_t err = 0, lsb = 0, msb = 0;
479 int mask = 0, timeout;
481 /* do we have any signs of ATA/ATAPI HW being present ? */
482 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | ATA_DEV(ATA_MASTER));
484 ostat0 = ATA_IDX_INB(ch, ATA_STATUS);
485 if (((ostat0 & 0xf8) != 0xf8 || (ch->flags & ATA_KNOWN_PRESENCE)) &&
491 /* in some setups we dont want to test for a slave */
492 if (!(ch->flags & ATA_NO_SLAVE)) {
493 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | ATA_DEV(ATA_SLAVE));
495 ostat1 = ATA_IDX_INB(ch, ATA_STATUS);
496 if (((ostat1 & 0xf8) != 0xf8 || (ch->flags & ATA_KNOWN_PRESENCE)) &&
504 device_printf(dev, "reset tp1 mask=%02x ostat0=%02x ostat1=%02x\n",
505 mask, ostat0, ostat1);
507 /* if nothing showed up there is no need to get any further */
508 /* XXX SOS is that too strong?, we just might lose devices here */
513 /* reset (both) devices on this channel */
514 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | ATA_DEV(ATA_MASTER));
516 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_IDS | ATA_A_RESET);
518 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_IDS);
520 ATA_IDX_INB(ch, ATA_ERROR);
522 /* wait for BUSY to go inactive */
523 for (timeout = 0; timeout < 310; timeout++) {
524 if ((mask & 0x01) && (stat0 & ATA_S_BUSY)) {
525 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(ATA_MASTER));
527 if (ch->flags & ATA_STATUS_IS_LONG)
528 stat0 = ATA_IDX_INL(ch, ATA_STATUS) & 0xff;
530 stat0 = ATA_IDX_INB(ch, ATA_STATUS);
531 err = ATA_IDX_INB(ch, ATA_ERROR);
532 lsb = ATA_IDX_INB(ch, ATA_CYL_LSB);
533 msb = ATA_IDX_INB(ch, ATA_CYL_MSB);
536 "stat0=0x%02x err=0x%02x lsb=0x%02x msb=0x%02x\n",
537 stat0, err, lsb, msb);
538 if (stat0 == err && lsb == err && msb == err &&
539 timeout > (stat0 & ATA_S_BUSY ? 100 : 10))
541 if (!(stat0 & ATA_S_BUSY)) {
542 if ((err & 0x7f) == ATA_E_ILI) {
543 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB) {
544 ch->devices |= ATA_ATAPI_MASTER;
546 else if (lsb == 0 && msb == 0 && (stat0 & ATA_S_READY)) {
547 ch->devices |= ATA_ATA_MASTER;
550 else if ((stat0 & 0x0f) && err == lsb && err == msb) {
556 if ((mask & 0x02) && (stat1 & ATA_S_BUSY) &&
557 !((mask & 0x01) && (stat0 & ATA_S_BUSY))) {
558 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(ATA_SLAVE));
560 if (ch->flags & ATA_STATUS_IS_LONG)
561 stat1 = ATA_IDX_INL(ch, ATA_STATUS) & 0xff;
563 stat1 = ATA_IDX_INB(ch, ATA_STATUS);
564 err = ATA_IDX_INB(ch, ATA_ERROR);
565 lsb = ATA_IDX_INB(ch, ATA_CYL_LSB);
566 msb = ATA_IDX_INB(ch, ATA_CYL_MSB);
569 "stat1=0x%02x err=0x%02x lsb=0x%02x msb=0x%02x\n",
570 stat1, err, lsb, msb);
571 if (stat1 == err && lsb == err && msb == err &&
572 timeout > (stat1 & ATA_S_BUSY ? 100 : 10))
574 if (!(stat1 & ATA_S_BUSY)) {
575 if ((err & 0x7f) == ATA_E_ILI) {
576 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB) {
577 ch->devices |= ATA_ATAPI_SLAVE;
579 else if (lsb == 0 && msb == 0 && (stat1 & ATA_S_READY)) {
580 ch->devices |= ATA_ATA_SLAVE;
583 else if ((stat1 & 0x0f) && err == lsb && err == msb) {
589 if ((ch->flags & ATA_KNOWN_PRESENCE) == 0 &&
590 timeout > ((mask == 0x03) ? 20 : 10)) {
591 if ((mask & 0x01) && stat0 == 0xff)
593 if ((mask & 0x02) && stat1 == 0xff)
596 if (((mask & 0x01) == 0 || !(stat0 & ATA_S_BUSY)) &&
597 ((mask & 0x02) == 0 || !(stat1 & ATA_S_BUSY)))
603 device_printf(dev, "reset tp2 stat0=%02x stat1=%02x devices=0x%x\n",
604 stat0, stat1, ch->devices);
607 /* must be called with ATA channel locked and state_mtx held */
609 ata_generic_status(device_t dev)
611 struct ata_channel *ch = device_get_softc(dev);
613 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
615 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
622 ata_wait(struct ata_channel *ch, int unit, u_int8_t mask)
629 /* wait at max 1 second for device to get !BUSY */
630 while (timeout < 1000000) {
631 status = ATA_IDX_INB(ch, ATA_ALTSTAT);
633 /* if drive fails status, reselect the drive and try again */
634 if (status == 0xff) {
635 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(unit));
642 if (!(status & ATA_S_BUSY))
645 if (timeout > 1000) {
654 if (timeout >= 1000000)
657 return (status & ATA_S_ERROR);
661 /* wait 50 msec for bits wanted */
664 status = ATA_IDX_INB(ch, ATA_ALTSTAT);
665 if ((status & mask) == mask)
666 return (status & ATA_S_ERROR);
673 ata_generic_command(struct ata_request *request)
675 struct ata_channel *ch = device_get_softc(request->parent);
678 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | ATA_DEV(request->unit));
680 /* ready to issue command ? */
681 if (ata_wait(ch, request->unit, 0) < 0) {
682 device_printf(request->parent, "timeout waiting to issue command\n");
683 request->flags |= ATA_R_TIMEOUT;
687 /* enable interrupt */
688 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_4BIT);
690 if (request->flags & ATA_R_ATAPI) {
694 /* issue packet command to controller */
695 if (request->flags & ATA_R_DMA) {
696 ATA_IDX_OUTB(ch, ATA_FEATURE, ATA_F_DMA);
697 ATA_IDX_OUTB(ch, ATA_CYL_LSB, 0);
698 ATA_IDX_OUTB(ch, ATA_CYL_MSB, 0);
701 ATA_IDX_OUTB(ch, ATA_FEATURE, 0);
702 ATA_IDX_OUTB(ch, ATA_CYL_LSB, request->transfersize);
703 ATA_IDX_OUTB(ch, ATA_CYL_MSB, request->transfersize >> 8);
705 ATA_IDX_OUTB(ch, ATA_COMMAND, ATA_PACKET_CMD);
707 /* command interrupt device ? just return and wait for interrupt */
708 if (request->flags & ATA_R_ATAPI_INTR)
711 /* command processed ? */
712 res = ata_wait(ch, request->unit, 0);
715 device_printf(request->parent,
716 "timeout waiting for PACKET command\n");
717 request->flags |= ATA_R_TIMEOUT;
721 /* wait for ready to write ATAPI command block */
723 int reason = ATA_IDX_INB(ch, ATA_IREASON);
724 int status = ATA_IDX_INB(ch, ATA_STATUS);
726 if (((reason & (ATA_I_CMD | ATA_I_IN)) |
727 (status & (ATA_S_DRQ | ATA_S_BUSY))) == ATAPI_P_CMDOUT)
732 device_printf(request->parent,
733 "timeout waiting for ATAPI ready\n");
734 request->flags |= ATA_R_TIMEOUT;
738 /* this seems to be needed for some (slow) devices */
741 /* output command block */
742 ATA_IDX_OUTSW_STRM(ch, ATA_DATA, (int16_t *)request->u.atapi.ccb,
743 (request->flags & ATA_R_ATAPI16) ? 8 : 6);
746 ch->hw.tf_write(request);
748 /* issue command to controller */
749 ATA_IDX_OUTB(ch, ATA_COMMAND, request->u.ata.command);
755 ata_tf_read(struct ata_request *request)
757 struct ata_channel *ch = device_get_softc(request->parent);
759 if (request->flags & ATA_R_48BIT) {
760 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_4BIT | ATA_A_HOB);
761 request->u.ata.count = (ATA_IDX_INB(ch, ATA_COUNT) << 8);
763 ((u_int64_t)(ATA_IDX_INB(ch, ATA_SECTOR)) << 24) |
764 ((u_int64_t)(ATA_IDX_INB(ch, ATA_CYL_LSB)) << 32) |
765 ((u_int64_t)(ATA_IDX_INB(ch, ATA_CYL_MSB)) << 40);
767 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_4BIT);
768 request->u.ata.count |= ATA_IDX_INB(ch, ATA_COUNT);
769 request->u.ata.lba |=
770 (ATA_IDX_INB(ch, ATA_SECTOR) |
771 (ATA_IDX_INB(ch, ATA_CYL_LSB) << 8) |
772 (ATA_IDX_INB(ch, ATA_CYL_MSB) << 16));
775 request->u.ata.count = ATA_IDX_INB(ch, ATA_COUNT);
776 request->u.ata.lba = ATA_IDX_INB(ch, ATA_SECTOR) |
777 (ATA_IDX_INB(ch, ATA_CYL_LSB) << 8) |
778 (ATA_IDX_INB(ch, ATA_CYL_MSB) << 16) |
779 ((ATA_IDX_INB(ch, ATA_DRIVE) & 0xf) << 24);
784 ata_tf_write(struct ata_request *request)
786 struct ata_channel *ch = device_get_softc(request->parent);
788 struct ata_device *atadev = device_get_softc(request->dev);
791 if (request->flags & ATA_R_48BIT) {
792 ATA_IDX_OUTB(ch, ATA_FEATURE, request->u.ata.feature >> 8);
793 ATA_IDX_OUTB(ch, ATA_FEATURE, request->u.ata.feature);
794 ATA_IDX_OUTB(ch, ATA_COUNT, request->u.ata.count >> 8);
795 ATA_IDX_OUTB(ch, ATA_COUNT, request->u.ata.count);
796 ATA_IDX_OUTB(ch, ATA_SECTOR, request->u.ata.lba >> 24);
797 ATA_IDX_OUTB(ch, ATA_SECTOR, request->u.ata.lba);
798 ATA_IDX_OUTB(ch, ATA_CYL_LSB, request->u.ata.lba >> 32);
799 ATA_IDX_OUTB(ch, ATA_CYL_LSB, request->u.ata.lba >> 8);
800 ATA_IDX_OUTB(ch, ATA_CYL_MSB, request->u.ata.lba >> 40);
801 ATA_IDX_OUTB(ch, ATA_CYL_MSB, request->u.ata.lba >> 16);
802 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_LBA | ATA_DEV(request->unit));
805 ATA_IDX_OUTB(ch, ATA_FEATURE, request->u.ata.feature);
806 ATA_IDX_OUTB(ch, ATA_COUNT, request->u.ata.count);
808 if (atadev->flags & ATA_D_USE_CHS) {
811 if (atadev->param.atavalid & ATA_FLAG_54_58) {
812 heads = atadev->param.current_heads;
813 sectors = atadev->param.current_sectors;
816 heads = atadev->param.heads;
817 sectors = atadev->param.sectors;
820 ATA_IDX_OUTB(ch, ATA_SECTOR, (request->u.ata.lba % sectors)+1);
821 ATA_IDX_OUTB(ch, ATA_CYL_LSB,
822 (request->u.ata.lba / (sectors * heads)));
823 ATA_IDX_OUTB(ch, ATA_CYL_MSB,
824 (request->u.ata.lba / (sectors * heads)) >> 8);
825 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(request->unit) |
826 (((request->u.ata.lba% (sectors * heads)) /
831 ATA_IDX_OUTB(ch, ATA_SECTOR, request->u.ata.lba);
832 ATA_IDX_OUTB(ch, ATA_CYL_LSB, request->u.ata.lba >> 8);
833 ATA_IDX_OUTB(ch, ATA_CYL_MSB, request->u.ata.lba >> 16);
834 ATA_IDX_OUTB(ch, ATA_DRIVE,
835 ATA_D_IBM | ATA_D_LBA | ATA_DEV(request->unit) |
836 ((request->u.ata.lba >> 24) & 0x0f));
844 ata_pio_read(struct ata_request *request, int length)
846 struct ata_channel *ch = device_get_softc(request->parent);
848 int size = min(request->transfersize, length);
850 uint8_t buf[2] __aligned(sizeof(int16_t));
851 #ifndef __NO_STRICT_ALIGNMENT
855 addr = (uint8_t *)request->data + request->donecount;
856 if (__predict_false(ch->flags & ATA_USE_16BIT ||
857 (size % sizeof(int32_t)) || ((uintptr_t)addr % sizeof(int32_t)))) {
858 #ifndef __NO_STRICT_ALIGNMENT
859 if (__predict_false((uintptr_t)addr % sizeof(int16_t))) {
860 for (i = 0, resid = size & ~1; resid > 0; resid -=
862 *(uint16_t *)&buf = ATA_IDX_INW_STRM(ch, ATA_DATA);
868 ATA_IDX_INSW_STRM(ch, ATA_DATA, (void*)addr, size /
871 *(uint16_t *)&buf = ATA_IDX_INW_STRM(ch, ATA_DATA);
872 (addr + (size & ~1))[0] = buf[0];
875 ATA_IDX_INSL_STRM(ch, ATA_DATA, (void*)addr, size / sizeof(int32_t));
877 if (request->transfersize < length) {
878 device_printf(request->parent, "WARNING - %s read data overrun %d>%d\n",
879 ata_cmd2str(request), length, request->transfersize);
880 for (resid = request->transfersize + (size & 1); resid < length;
881 resid += sizeof(int16_t))
882 ATA_IDX_INW(ch, ATA_DATA);
887 ata_pio_write(struct ata_request *request, int length)
889 struct ata_channel *ch = device_get_softc(request->parent);
891 int size = min(request->transfersize, length);
893 uint8_t buf[2] __aligned(sizeof(int16_t));
894 #ifndef __NO_STRICT_ALIGNMENT
898 size = min(request->transfersize, length);
899 addr = (uint8_t *)request->data + request->donecount;
900 if (__predict_false(ch->flags & ATA_USE_16BIT ||
901 (size % sizeof(int32_t)) || ((uintptr_t)addr % sizeof(int32_t)))) {
902 #ifndef __NO_STRICT_ALIGNMENT
903 if (__predict_false((uintptr_t)addr % sizeof(int16_t))) {
904 for (i = 0, resid = size & ~1; resid > 0; resid -=
908 ATA_IDX_OUTW_STRM(ch, ATA_DATA, *(uint16_t *)&buf);
912 ATA_IDX_OUTSW_STRM(ch, ATA_DATA, (void*)addr, size /
915 buf[0] = (addr + (size & ~1))[0];
916 ATA_IDX_OUTW_STRM(ch, ATA_DATA, *(uint16_t *)&buf);
919 ATA_IDX_OUTSL_STRM(ch, ATA_DATA, (void*)addr, size / sizeof(int32_t));
921 if (request->transfersize < length) {
922 device_printf(request->parent, "WARNING - %s write data underrun %d>%d\n",
923 ata_cmd2str(request), length, request->transfersize);
924 for (resid = request->transfersize + (size & 1); resid < length;
925 resid += sizeof(int16_t))
926 ATA_IDX_OUTW(ch, ATA_DATA, 0);