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$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #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>
52 #include <cam/cam_ccb.h>
55 static int ata_generic_status(device_t dev);
56 static int ata_wait(struct ata_channel *ch, int unit, u_int8_t);
57 static void ata_pio_read(struct ata_request *, int);
58 static void ata_pio_write(struct ata_request *, int);
59 static void ata_tf_read(struct ata_request *);
60 static void ata_tf_write(struct ata_request *);
63 * low level ATA functions
66 ata_generic_hw(device_t dev)
68 struct ata_channel *ch = device_get_softc(dev);
70 ch->hw.begin_transaction = ata_begin_transaction;
71 ch->hw.end_transaction = ata_end_transaction;
72 ch->hw.status = ata_generic_status;
73 ch->hw.softreset = NULL;
74 ch->hw.command = ata_generic_command;
75 ch->hw.tf_read = ata_tf_read;
76 ch->hw.tf_write = ata_tf_write;
77 ch->hw.pm_read = NULL;
78 ch->hw.pm_write = NULL;
81 /* must be called with ATA channel locked and state_mtx held */
83 ata_begin_transaction(struct ata_request *request)
85 struct ata_channel *ch = device_get_softc(request->parent);
88 ATA_DEBUG_RQ(request, "begin transaction");
90 /* disable ATAPI DMA writes if HW doesn't support it */
91 if ((ch->flags & ATA_NO_ATAPI_DMA) &&
92 (request->flags & ATA_R_ATAPI) == ATA_R_ATAPI)
93 request->flags &= ~ATA_R_DMA;
94 if ((ch->flags & ATA_ATAPI_DMA_RO) &&
95 ((request->flags & (ATA_R_ATAPI | ATA_R_DMA | ATA_R_WRITE)) ==
96 (ATA_R_ATAPI | ATA_R_DMA | ATA_R_WRITE)))
97 request->flags &= ~ATA_R_DMA;
99 switch (request->flags & (ATA_R_ATAPI | ATA_R_DMA)) {
101 /* ATA PIO data transfer and control commands */
104 /* record command direction here as our request might be gone later */
105 int write = (request->flags & ATA_R_WRITE);
108 if (ch->hw.command(request)) {
109 device_printf(request->parent, "error issuing %s command\n",
110 ata_cmd2str(request));
111 request->result = EIO;
115 /* device reset doesn't interrupt */
116 if (request->u.ata.command == ATA_DEVICE_RESET) {
118 int timeout = 1000000;
121 request->status = ATA_IDX_INB(ch, ATA_STATUS);
122 } while (request->status & ATA_S_BUSY && timeout--);
123 if (request->status & ATA_S_ERROR)
124 request->error = ATA_IDX_INB(ch, ATA_ERROR);
125 ch->hw.tf_read(request);
129 /* if write command output the data */
131 if (ata_wait(ch, request->unit, (ATA_S_READY | ATA_S_DRQ)) < 0) {
132 device_printf(request->parent,
133 "timeout waiting for write DRQ\n");
134 request->result = EIO;
137 ata_pio_write(request, request->transfersize);
142 /* ATA DMA data transfer commands */
144 /* check sanity, setup SG list and DMA engine */
145 if ((error = ch->dma.load(request, NULL, &dummy))) {
146 device_printf(request->parent, "setting up DMA failed\n");
147 request->result = error;
151 /* start DMA engine if necessary */
152 if ((ch->flags & ATA_DMA_BEFORE_CMD) &&
153 ch->dma.start && ch->dma.start(request)) {
154 device_printf(request->parent, "error starting DMA\n");
155 request->result = EIO;
160 if (ch->hw.command(request)) {
161 device_printf(request->parent, "error issuing %s command\n",
162 ata_cmd2str(request));
163 request->result = EIO;
167 /* start DMA engine */
168 if (!(ch->flags & ATA_DMA_BEFORE_CMD) &&
169 ch->dma.start && ch->dma.start(request)) {
170 device_printf(request->parent, "error starting DMA\n");
171 request->result = EIO;
176 /* ATAPI PIO commands */
178 /* is this just a POLL DSC command ? */
179 if (request->u.atapi.ccb[0] == ATAPI_POLL_DSC) {
180 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(request->unit));
182 if (!(ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_DSC))
183 request->result = EBUSY;
187 /* start ATAPI operation */
188 if (ch->hw.command(request)) {
189 device_printf(request->parent, "error issuing ATA PACKET command\n");
190 request->result = EIO;
195 /* ATAPI DMA commands */
196 case ATA_R_ATAPI|ATA_R_DMA:
197 /* is this just a POLL DSC command ? */
198 if (request->u.atapi.ccb[0] == ATAPI_POLL_DSC) {
199 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(request->unit));
201 if (!(ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_DSC))
202 request->result = EBUSY;
206 /* check sanity, setup SG list and DMA engine */
207 if ((error = ch->dma.load(request, NULL, &dummy))) {
208 device_printf(request->parent, "setting up DMA failed\n");
209 request->result = error;
213 /* start ATAPI operation */
214 if (ch->hw.command(request)) {
215 device_printf(request->parent, "error issuing ATA PACKET command\n");
216 request->result = EIO;
220 /* start DMA engine */
221 if (ch->dma.start && ch->dma.start(request)) {
222 request->result = EIO;
228 printf("ata_begin_transaction OOPS!!!\n");
231 if (ch->dma.unload) {
232 ch->dma.unload(request);
234 return ATA_OP_FINISHED;
237 callout_reset(&request->callout, request->timeout * hz,
238 (timeout_t*)ata_timeout, request);
239 return ATA_OP_CONTINUES;
242 /* must be called with ATA channel locked and state_mtx held */
244 ata_end_transaction(struct ata_request *request)
246 struct ata_channel *ch = device_get_softc(request->parent);
249 ATA_DEBUG_RQ(request, "end transaction");
251 /* clear interrupt and get status */
252 request->status = ATA_IDX_INB(ch, ATA_STATUS);
254 switch (request->flags & (ATA_R_ATAPI | ATA_R_DMA | ATA_R_CONTROL)) {
256 /* ATA PIO data transfer and control commands */
259 /* on timeouts we have no data or anything so just return */
260 if (request->flags & ATA_R_TIMEOUT)
263 /* Read back registers to the request struct. */
264 if ((request->status & ATA_S_ERROR) ||
265 (request->flags & (ATA_R_CONTROL | ATA_R_NEEDRESULT))) {
266 ch->hw.tf_read(request);
269 /* if we got an error we are done with the HW */
270 if (request->status & ATA_S_ERROR) {
271 request->error = ATA_IDX_INB(ch, ATA_ERROR);
275 /* are we moving data ? */
276 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
278 /* if read data get it */
279 if (request->flags & ATA_R_READ) {
280 int flags = ATA_S_DRQ;
282 if (request->u.ata.command != ATA_ATAPI_IDENTIFY)
283 flags |= ATA_S_READY;
284 if (ata_wait(ch, request->unit, flags) < 0) {
285 device_printf(request->parent,
286 "timeout waiting for read DRQ\n");
287 request->result = EIO;
290 ata_pio_read(request, request->transfersize);
293 /* update how far we've gotten */
294 request->donecount += request->transfersize;
296 /* do we need a scoop more ? */
297 if (request->bytecount > request->donecount) {
299 /* set this transfer size according to HW capabilities */
300 request->transfersize =
301 min((request->bytecount - request->donecount),
302 request->transfersize);
304 /* if data write command, output the data */
305 if (request->flags & ATA_R_WRITE) {
307 /* if we get an error here we are done with the HW */
308 if (ata_wait(ch, request->unit, (ATA_S_READY | ATA_S_DRQ)) < 0) {
309 device_printf(request->parent,
310 "timeout waiting for write DRQ\n");
311 request->status = ATA_IDX_INB(ch, ATA_STATUS);
315 /* output data and return waiting for new interrupt */
316 ata_pio_write(request, request->transfersize);
320 /* if data read command, return & wait for interrupt */
321 if (request->flags & ATA_R_READ)
328 /* ATA DMA data transfer commands */
331 /* stop DMA engine and get status */
333 request->dma->status = ch->dma.stop(request);
335 /* did we get error or data */
336 if (request->status & ATA_S_ERROR)
337 request->error = ATA_IDX_INB(ch, ATA_ERROR);
338 else if (request->dma->status & ATA_BMSTAT_ERROR)
339 request->status |= ATA_S_ERROR;
340 else if (!(request->flags & ATA_R_TIMEOUT))
341 request->donecount = request->bytecount;
343 /* Read back registers to the request struct. */
344 if ((request->status & ATA_S_ERROR) ||
345 (request->flags & (ATA_R_CONTROL | ATA_R_NEEDRESULT))) {
346 ch->hw.tf_read(request);
349 /* release SG list etc */
350 ch->dma.unload(request);
355 /* ATAPI PIO commands */
357 length = ATA_IDX_INB(ch, ATA_CYL_LSB)|(ATA_IDX_INB(ch, ATA_CYL_MSB)<<8);
359 /* on timeouts we have no data or anything so just return */
360 if (request->flags & ATA_R_TIMEOUT)
363 switch ((ATA_IDX_INB(ch, ATA_IREASON) & (ATA_I_CMD | ATA_I_IN)) |
364 (request->status & ATA_S_DRQ)) {
367 /* this seems to be needed for some (slow) devices */
370 if (!(request->status & ATA_S_DRQ)) {
371 device_printf(request->parent, "command interrupt without DRQ\n");
372 request->status = ATA_S_ERROR;
375 ATA_IDX_OUTSW_STRM(ch, ATA_DATA, (int16_t *)request->u.atapi.ccb,
376 (request->flags & ATA_R_ATAPI16) ? 8 : 6);
377 /* return wait for interrupt */
381 if (request->flags & ATA_R_READ) {
382 request->status = ATA_S_ERROR;
383 device_printf(request->parent,
384 "%s trying to write on read buffer\n",
385 ata_cmd2str(request));
388 ata_pio_write(request, length);
389 request->donecount += length;
391 /* set next transfer size according to HW capabilities */
392 request->transfersize = min((request->bytecount-request->donecount),
393 request->transfersize);
394 /* return wait for interrupt */
398 if (request->flags & ATA_R_WRITE) {
399 request->status = ATA_S_ERROR;
400 device_printf(request->parent,
401 "%s trying to read on write buffer\n",
402 ata_cmd2str(request));
405 ata_pio_read(request, length);
406 request->donecount += length;
408 /* set next transfer size according to HW capabilities */
409 request->transfersize = min((request->bytecount-request->donecount),
410 request->transfersize);
411 /* return wait for interrupt */
414 case ATAPI_P_DONEDRQ:
415 device_printf(request->parent,
416 "WARNING - %s DONEDRQ non conformant device\n",
417 ata_cmd2str(request));
418 if (request->flags & ATA_R_READ) {
419 ata_pio_read(request, length);
420 request->donecount += length;
422 else if (request->flags & ATA_R_WRITE) {
423 ata_pio_write(request, length);
424 request->donecount += length;
427 request->status = ATA_S_ERROR;
432 if (request->status & (ATA_S_ERROR | ATA_S_DWF))
433 request->error = ATA_IDX_INB(ch, ATA_ERROR);
437 device_printf(request->parent, "unknown transfer phase\n");
438 request->status = ATA_S_ERROR;
444 /* ATAPI DMA commands */
445 case ATA_R_ATAPI|ATA_R_DMA:
447 /* stop DMA engine and get status */
449 request->dma->status = ch->dma.stop(request);
451 /* did we get error or data */
452 if (request->status & (ATA_S_ERROR | ATA_S_DWF))
453 request->error = ATA_IDX_INB(ch, ATA_ERROR);
454 else if (request->dma->status & ATA_BMSTAT_ERROR)
455 request->status |= ATA_S_ERROR;
456 else if (!(request->flags & ATA_R_TIMEOUT))
457 request->donecount = request->bytecount;
459 /* release SG list etc */
460 ch->dma.unload(request);
466 printf("ata_end_transaction OOPS!!\n");
469 callout_stop(&request->callout);
470 return ATA_OP_FINISHED;
473 return ATA_OP_CONTINUES;
476 /* must be called with ATA channel locked and state_mtx held */
478 ata_generic_reset(device_t dev)
480 struct ata_channel *ch = device_get_softc(dev);
482 u_int8_t ostat0 = 0, stat0 = 0, ostat1 = 0, stat1 = 0;
483 u_int8_t err = 0, lsb = 0, msb = 0;
484 int mask = 0, timeout;
486 /* do we have any signs of ATA/ATAPI HW being present ? */
487 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | ATA_DEV(ATA_MASTER));
489 ostat0 = ATA_IDX_INB(ch, ATA_STATUS);
490 if (((ostat0 & 0xf8) != 0xf8 || (ch->flags & ATA_KNOWN_PRESENCE)) &&
496 /* in some setups we dont want to test for a slave */
497 if (!(ch->flags & ATA_NO_SLAVE)) {
498 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | ATA_DEV(ATA_SLAVE));
500 ostat1 = ATA_IDX_INB(ch, ATA_STATUS);
501 if (((ostat1 & 0xf8) != 0xf8 || (ch->flags & ATA_KNOWN_PRESENCE)) &&
509 device_printf(dev, "reset tp1 mask=%02x ostat0=%02x ostat1=%02x\n",
510 mask, ostat0, ostat1);
512 /* if nothing showed up there is no need to get any further */
513 /* XXX SOS is that too strong?, we just might lose devices here */
518 /* reset (both) devices on this channel */
519 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | ATA_DEV(ATA_MASTER));
521 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_IDS | ATA_A_RESET);
523 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_IDS);
525 ATA_IDX_INB(ch, ATA_ERROR);
527 /* wait for BUSY to go inactive */
528 for (timeout = 0; timeout < 310; timeout++) {
529 if ((mask & 0x01) && (stat0 & ATA_S_BUSY)) {
530 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(ATA_MASTER));
532 if (ch->flags & ATA_STATUS_IS_LONG)
533 stat0 = ATA_IDX_INL(ch, ATA_STATUS) & 0xff;
535 stat0 = ATA_IDX_INB(ch, ATA_STATUS);
536 err = ATA_IDX_INB(ch, ATA_ERROR);
537 lsb = ATA_IDX_INB(ch, ATA_CYL_LSB);
538 msb = ATA_IDX_INB(ch, ATA_CYL_MSB);
541 "stat0=0x%02x err=0x%02x lsb=0x%02x msb=0x%02x\n",
542 stat0, err, lsb, msb);
543 if (stat0 == err && lsb == err && msb == err &&
544 timeout > (stat0 & ATA_S_BUSY ? 100 : 10))
546 if (!(stat0 & ATA_S_BUSY)) {
547 if ((err & 0x7f) == ATA_E_ILI) {
548 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB) {
549 ch->devices |= ATA_ATAPI_MASTER;
551 else if (lsb == 0 && msb == 0 && (stat0 & ATA_S_READY)) {
552 ch->devices |= ATA_ATA_MASTER;
555 else if ((stat0 & 0x0f) && err == lsb && err == msb) {
561 if ((mask & 0x02) && (stat1 & ATA_S_BUSY) &&
562 !((mask & 0x01) && (stat0 & ATA_S_BUSY))) {
563 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(ATA_SLAVE));
565 if (ch->flags & ATA_STATUS_IS_LONG)
566 stat1 = ATA_IDX_INL(ch, ATA_STATUS) & 0xff;
568 stat1 = ATA_IDX_INB(ch, ATA_STATUS);
569 err = ATA_IDX_INB(ch, ATA_ERROR);
570 lsb = ATA_IDX_INB(ch, ATA_CYL_LSB);
571 msb = ATA_IDX_INB(ch, ATA_CYL_MSB);
574 "stat1=0x%02x err=0x%02x lsb=0x%02x msb=0x%02x\n",
575 stat1, err, lsb, msb);
576 if (stat1 == err && lsb == err && msb == err &&
577 timeout > (stat1 & ATA_S_BUSY ? 100 : 10))
579 if (!(stat1 & ATA_S_BUSY)) {
580 if ((err & 0x7f) == ATA_E_ILI) {
581 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB) {
582 ch->devices |= ATA_ATAPI_SLAVE;
584 else if (lsb == 0 && msb == 0 && (stat1 & ATA_S_READY)) {
585 ch->devices |= ATA_ATA_SLAVE;
588 else if ((stat1 & 0x0f) && err == lsb && err == msb) {
594 if ((ch->flags & ATA_KNOWN_PRESENCE) == 0 &&
595 timeout > ((mask == 0x03) ? 20 : 10)) {
596 if ((mask & 0x01) && stat0 == 0xff)
598 if ((mask & 0x02) && stat1 == 0xff)
601 if (((mask & 0x01) == 0 || !(stat0 & ATA_S_BUSY)) &&
602 ((mask & 0x02) == 0 || !(stat1 & ATA_S_BUSY)))
608 device_printf(dev, "reset tp2 stat0=%02x stat1=%02x devices=0x%x\n",
609 stat0, stat1, ch->devices);
612 /* must be called with ATA channel locked and state_mtx held */
614 ata_generic_status(device_t dev)
616 struct ata_channel *ch = device_get_softc(dev);
618 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
620 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
627 ata_wait(struct ata_channel *ch, int unit, u_int8_t mask)
634 /* wait at max 1 second for device to get !BUSY */
635 while (timeout < 1000000) {
636 status = ATA_IDX_INB(ch, ATA_ALTSTAT);
638 /* if drive fails status, reselect the drive and try again */
639 if (status == 0xff) {
640 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(unit));
647 if (!(status & ATA_S_BUSY))
650 if (timeout > 1000) {
659 if (timeout >= 1000000)
662 return (status & ATA_S_ERROR);
666 /* wait 50 msec for bits wanted */
669 status = ATA_IDX_INB(ch, ATA_ALTSTAT);
670 if ((status & mask) == mask)
671 return (status & ATA_S_ERROR);
678 ata_generic_command(struct ata_request *request)
680 struct ata_channel *ch = device_get_softc(request->parent);
683 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | ATA_DEV(request->unit));
685 /* ready to issue command ? */
686 if (ata_wait(ch, request->unit, 0) < 0) {
687 device_printf(request->parent, "timeout waiting to issue command\n");
688 request->flags |= ATA_R_TIMEOUT;
692 /* enable interrupt */
693 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_4BIT);
695 if (request->flags & ATA_R_ATAPI) {
699 /* issue packet command to controller */
700 if (request->flags & ATA_R_DMA) {
701 ATA_IDX_OUTB(ch, ATA_FEATURE, ATA_F_DMA);
702 ATA_IDX_OUTB(ch, ATA_CYL_LSB, 0);
703 ATA_IDX_OUTB(ch, ATA_CYL_MSB, 0);
706 ATA_IDX_OUTB(ch, ATA_FEATURE, 0);
707 ATA_IDX_OUTB(ch, ATA_CYL_LSB, request->transfersize);
708 ATA_IDX_OUTB(ch, ATA_CYL_MSB, request->transfersize >> 8);
710 ATA_IDX_OUTB(ch, ATA_COMMAND, ATA_PACKET_CMD);
712 /* command interrupt device ? just return and wait for interrupt */
713 if (request->flags & ATA_R_ATAPI_INTR)
716 /* command processed ? */
717 res = ata_wait(ch, request->unit, 0);
720 device_printf(request->parent,
721 "timeout waiting for PACKET command\n");
722 request->flags |= ATA_R_TIMEOUT;
726 /* wait for ready to write ATAPI command block */
728 int reason = ATA_IDX_INB(ch, ATA_IREASON);
729 int status = ATA_IDX_INB(ch, ATA_STATUS);
731 if (((reason & (ATA_I_CMD | ATA_I_IN)) |
732 (status & (ATA_S_DRQ | ATA_S_BUSY))) == ATAPI_P_CMDOUT)
737 device_printf(request->parent,
738 "timeout waiting for ATAPI ready\n");
739 request->flags |= ATA_R_TIMEOUT;
743 /* this seems to be needed for some (slow) devices */
746 /* output command block */
747 ATA_IDX_OUTSW_STRM(ch, ATA_DATA, (int16_t *)request->u.atapi.ccb,
748 (request->flags & ATA_R_ATAPI16) ? 8 : 6);
751 ch->hw.tf_write(request);
753 /* issue command to controller */
754 ATA_IDX_OUTB(ch, ATA_COMMAND, request->u.ata.command);
760 ata_tf_read(struct ata_request *request)
762 struct ata_channel *ch = device_get_softc(request->parent);
764 if (request->flags & ATA_R_48BIT) {
765 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_4BIT | ATA_A_HOB);
766 request->u.ata.count = (ATA_IDX_INB(ch, ATA_COUNT) << 8);
768 ((u_int64_t)(ATA_IDX_INB(ch, ATA_SECTOR)) << 24) |
769 ((u_int64_t)(ATA_IDX_INB(ch, ATA_CYL_LSB)) << 32) |
770 ((u_int64_t)(ATA_IDX_INB(ch, ATA_CYL_MSB)) << 40);
772 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_4BIT);
773 request->u.ata.count |= ATA_IDX_INB(ch, ATA_COUNT);
774 request->u.ata.lba |=
775 (ATA_IDX_INB(ch, ATA_SECTOR) |
776 (ATA_IDX_INB(ch, ATA_CYL_LSB) << 8) |
777 (ATA_IDX_INB(ch, ATA_CYL_MSB) << 16));
780 request->u.ata.count = ATA_IDX_INB(ch, ATA_COUNT);
781 request->u.ata.lba = ATA_IDX_INB(ch, ATA_SECTOR) |
782 (ATA_IDX_INB(ch, ATA_CYL_LSB) << 8) |
783 (ATA_IDX_INB(ch, ATA_CYL_MSB) << 16) |
784 ((ATA_IDX_INB(ch, ATA_DRIVE) & 0xf) << 24);
789 ata_tf_write(struct ata_request *request)
791 struct ata_channel *ch = device_get_softc(request->parent);
793 if (request->flags & ATA_R_48BIT) {
794 ATA_IDX_OUTB(ch, ATA_FEATURE, request->u.ata.feature >> 8);
795 ATA_IDX_OUTB(ch, ATA_FEATURE, request->u.ata.feature);
796 ATA_IDX_OUTB(ch, ATA_COUNT, request->u.ata.count >> 8);
797 ATA_IDX_OUTB(ch, ATA_COUNT, request->u.ata.count);
798 ATA_IDX_OUTB(ch, ATA_SECTOR, request->u.ata.lba >> 24);
799 ATA_IDX_OUTB(ch, ATA_SECTOR, request->u.ata.lba);
800 ATA_IDX_OUTB(ch, ATA_CYL_LSB, request->u.ata.lba >> 32);
801 ATA_IDX_OUTB(ch, ATA_CYL_LSB, request->u.ata.lba >> 8);
802 ATA_IDX_OUTB(ch, ATA_CYL_MSB, request->u.ata.lba >> 40);
803 ATA_IDX_OUTB(ch, ATA_CYL_MSB, request->u.ata.lba >> 16);
804 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_LBA | ATA_DEV(request->unit));
807 ATA_IDX_OUTB(ch, ATA_FEATURE, request->u.ata.feature);
808 ATA_IDX_OUTB(ch, ATA_COUNT, request->u.ata.count);
809 ATA_IDX_OUTB(ch, ATA_SECTOR, request->u.ata.lba);
810 ATA_IDX_OUTB(ch, ATA_CYL_LSB, request->u.ata.lba >> 8);
811 ATA_IDX_OUTB(ch, ATA_CYL_MSB, request->u.ata.lba >> 16);
812 ATA_IDX_OUTB(ch, ATA_DRIVE,
813 ATA_D_IBM | ATA_D_LBA | ATA_DEV(request->unit) |
814 ((request->u.ata.lba >> 24) & 0x0f));
819 ata_pio_read(struct ata_request *request, int length)
821 struct ata_channel *ch = device_get_softc(request->parent);
825 int todo, done, off, moff, resid, size, i;
826 uint8_t buf[2] __aligned(2);
828 todo = min(request->transfersize, length);
829 page = done = resid = 0;
830 while (done < todo) {
833 /* Prepare data address and limit size (if not sequential). */
834 off = request->donecount + done;
835 if ((request->flags & ATA_R_DATA_IN_CCB) == 0 ||
836 (request->ccb->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_VADDR) {
837 addr = (uint8_t *)request->data + off;
838 } else if ((request->ccb->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_BIO) {
839 bio = (struct bio *)request->data;
840 if ((bio->bio_flags & BIO_UNMAPPED) == 0) {
841 addr = (uint8_t *)bio->bio_data + off;
843 moff = bio->bio_ma_offset + off;
844 page = pmap_quick_enter_page(
845 bio->bio_ma[moff / PAGE_SIZE]);
847 size = min(size, PAGE_SIZE - moff);
848 addr = (void *)(page + moff);
851 panic("ata_pio_read: Unsupported CAM data type %x\n",
852 (request->ccb->ccb_h.flags & CAM_DATA_MASK));
854 /* We may have extra byte already read but not stored. */
862 /* Process main part of data. */
864 if (__predict_false((ch->flags & ATA_USE_16BIT) ||
865 (size % 4) != 0 || ((uintptr_t)addr % 4) != 0)) {
866 #ifndef __NO_STRICT_ALIGNMENT
867 if (__predict_false((uintptr_t)addr % 2)) {
868 for (i = 0; i + 1 < size; i += 2) {
870 ATA_IDX_INW_STRM(ch, ATA_DATA);
872 addr[i + 1] = buf[1];
876 ATA_IDX_INSW_STRM(ch, ATA_DATA, (void*)addr,
879 /* If we have extra byte of data, leave it for later. */
882 ATA_IDX_INW_STRM(ch, ATA_DATA);
883 addr[size - 1] = buf[0];
886 ATA_IDX_INSL_STRM(ch, ATA_DATA, (void*)addr, size / 4);
889 pmap_quick_remove_page(page);
896 device_printf(request->parent,
897 "WARNING - %s read data overrun %d > %d\n",
898 ata_cmd2str(request), length, done);
899 for (i = done + resid; i < length; i += 2)
900 ATA_IDX_INW(ch, ATA_DATA);
905 ata_pio_write(struct ata_request *request, int length)
907 struct ata_channel *ch = device_get_softc(request->parent);
911 int todo, done, off, moff, resid, size, i;
912 uint8_t buf[2] __aligned(2);
914 todo = min(request->transfersize, length);
915 page = done = resid = 0;
916 while (done < todo) {
919 /* Prepare data address and limit size (if not sequential). */
920 off = request->donecount + done;
921 if ((request->flags & ATA_R_DATA_IN_CCB) == 0 ||
922 (request->ccb->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_VADDR) {
923 addr = (uint8_t *)request->data + off;
924 } else if ((request->ccb->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_BIO) {
925 bio = (struct bio *)request->data;
926 if ((bio->bio_flags & BIO_UNMAPPED) == 0) {
927 addr = (uint8_t *)bio->bio_data + off;
929 moff = bio->bio_ma_offset + off;
930 page = pmap_quick_enter_page(
931 bio->bio_ma[moff / PAGE_SIZE]);
933 size = min(size, PAGE_SIZE - moff);
934 addr = (void *)(page + moff);
937 panic("ata_pio_write: Unsupported CAM data type %x\n",
938 (request->ccb->ccb_h.flags & CAM_DATA_MASK));
940 /* We may have extra byte to be written first. */
943 ATA_IDX_OUTW_STRM(ch, ATA_DATA, *(uint16_t *)&buf);
949 /* Process main part of data. */
951 if (__predict_false((ch->flags & ATA_USE_16BIT) ||
952 (size % 4) != 0 || ((uintptr_t)addr % 4) != 0)) {
953 #ifndef __NO_STRICT_ALIGNMENT
954 if (__predict_false((uintptr_t)addr % 2)) {
955 for (i = 0; i + 1 < size; i += 2) {
957 buf[1] = addr[i + 1];
958 ATA_IDX_OUTW_STRM(ch, ATA_DATA,
963 ATA_IDX_OUTSW_STRM(ch, ATA_DATA, (void*)addr,
966 /* If we have extra byte of data, save it for later. */
968 buf[0] = addr[size - 1];
970 ATA_IDX_OUTSL_STRM(ch, ATA_DATA,
971 (void*)addr, size / sizeof(int32_t));
974 pmap_quick_remove_page(page);
980 /* We may have extra byte of data to be written. Pad it with zero. */
983 ATA_IDX_OUTW_STRM(ch, ATA_DATA, *(uint16_t *)&buf);
987 device_printf(request->parent,
988 "WARNING - %s write data underrun %d > %d\n",
989 ata_cmd2str(request), length, done);
990 for (i = done + resid; i < length; i += 2)
991 ATA_IDX_OUTW(ch, ATA_DATA, 0);