2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/endian.h>
36 #include <sys/ctype.h>
39 #include <sys/taskqueue.h>
41 #include <machine/bus.h>
43 #include <dev/ata/ata-all.h>
44 #include <dev/ata/ata-pci.h>
51 #include <cam/cam_ccb.h>
54 static int ata_generic_status(device_t dev);
55 static int ata_wait(struct ata_channel *ch, int unit, u_int8_t);
56 static void ata_pio_read(struct ata_request *, int);
57 static void ata_pio_write(struct ata_request *, int);
58 static void ata_tf_read(struct ata_request *);
59 static void ata_tf_write(struct ata_request *);
62 * low level ATA functions
65 ata_generic_hw(device_t dev)
67 struct ata_channel *ch = device_get_softc(dev);
69 ch->hw.begin_transaction = ata_begin_transaction;
70 ch->hw.end_transaction = ata_end_transaction;
71 ch->hw.status = ata_generic_status;
72 ch->hw.softreset = NULL;
73 ch->hw.command = ata_generic_command;
74 ch->hw.tf_read = ata_tf_read;
75 ch->hw.tf_write = ata_tf_write;
76 ch->hw.pm_read = NULL;
77 ch->hw.pm_write = NULL;
80 /* must be called with ATA channel locked and state_mtx held */
82 ata_begin_transaction(struct ata_request *request)
84 struct ata_channel *ch = device_get_softc(request->parent);
87 ATA_DEBUG_RQ(request, "begin transaction");
89 /* disable ATAPI DMA writes if HW doesn't support it */
90 if ((ch->flags & ATA_NO_ATAPI_DMA) &&
91 (request->flags & ATA_R_ATAPI) == ATA_R_ATAPI)
92 request->flags &= ~ATA_R_DMA;
93 if ((ch->flags & ATA_ATAPI_DMA_RO) &&
94 ((request->flags & (ATA_R_ATAPI | ATA_R_DMA | ATA_R_WRITE)) ==
95 (ATA_R_ATAPI | ATA_R_DMA | ATA_R_WRITE)))
96 request->flags &= ~ATA_R_DMA;
98 switch (request->flags & (ATA_R_ATAPI | ATA_R_DMA)) {
99 /* ATA PIO data transfer and control commands */
102 /* record command direction here as our request might be gone later */
103 int write = (request->flags & ATA_R_WRITE);
106 if (ch->hw.command(request)) {
107 device_printf(request->parent, "error issuing %s command\n",
108 ata_cmd2str(request));
109 request->result = EIO;
113 /* device reset doesn't interrupt */
114 if (request->u.ata.command == ATA_DEVICE_RESET) {
115 int timeout = 1000000;
118 request->status = ATA_IDX_INB(ch, ATA_STATUS);
119 } while (request->status & ATA_S_BUSY && timeout--);
120 if (request->status & ATA_S_ERROR)
121 request->error = ATA_IDX_INB(ch, ATA_ERROR);
122 ch->hw.tf_read(request);
126 /* if write command output the data */
128 if (ata_wait(ch, request->unit, (ATA_S_READY | ATA_S_DRQ)) < 0) {
129 device_printf(request->parent,
130 "timeout waiting for write DRQ\n");
131 request->result = EIO;
134 ata_pio_write(request, request->transfersize);
139 /* ATA DMA data transfer commands */
141 /* check sanity, setup SG list and DMA engine */
142 if ((error = ch->dma.load(request, NULL, &dummy))) {
143 device_printf(request->parent, "setting up DMA failed\n");
144 request->result = error;
148 /* start DMA engine if necessary */
149 if ((ch->flags & ATA_DMA_BEFORE_CMD) &&
150 ch->dma.start && ch->dma.start(request)) {
151 device_printf(request->parent, "error starting DMA\n");
152 request->result = EIO;
157 if (ch->hw.command(request)) {
158 device_printf(request->parent, "error issuing %s command\n",
159 ata_cmd2str(request));
160 request->result = EIO;
164 /* start DMA engine */
165 if (!(ch->flags & ATA_DMA_BEFORE_CMD) &&
166 ch->dma.start && ch->dma.start(request)) {
167 device_printf(request->parent, "error starting DMA\n");
168 request->result = EIO;
173 /* ATAPI PIO commands */
175 /* is this just a POLL DSC command ? */
176 if (request->u.atapi.ccb[0] == ATAPI_POLL_DSC) {
177 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(request->unit));
179 if (!(ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_DSC))
180 request->result = EBUSY;
184 /* start ATAPI operation */
185 if (ch->hw.command(request)) {
186 device_printf(request->parent, "error issuing ATA PACKET command\n");
187 request->result = EIO;
192 /* ATAPI DMA commands */
193 case ATA_R_ATAPI|ATA_R_DMA:
194 /* is this just a POLL DSC command ? */
195 if (request->u.atapi.ccb[0] == ATAPI_POLL_DSC) {
196 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(request->unit));
198 if (!(ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_DSC))
199 request->result = EBUSY;
203 /* check sanity, setup SG list and DMA engine */
204 if ((error = ch->dma.load(request, NULL, &dummy))) {
205 device_printf(request->parent, "setting up DMA failed\n");
206 request->result = error;
210 /* start ATAPI operation */
211 if (ch->hw.command(request)) {
212 device_printf(request->parent, "error issuing ATA PACKET command\n");
213 request->result = EIO;
217 /* start DMA engine */
218 if (ch->dma.start && ch->dma.start(request)) {
219 request->result = EIO;
225 printf("ata_begin_transaction OOPS!!!\n");
228 if (ch->dma.unload) {
229 ch->dma.unload(request);
231 return ATA_OP_FINISHED;
234 callout_reset(&request->callout, request->timeout * hz,
235 ata_timeout, request);
236 return ATA_OP_CONTINUES;
239 /* must be called with ATA channel locked and state_mtx held */
241 ata_end_transaction(struct ata_request *request)
243 struct ata_channel *ch = device_get_softc(request->parent);
246 ATA_DEBUG_RQ(request, "end transaction");
248 /* clear interrupt and get status */
249 request->status = ATA_IDX_INB(ch, ATA_STATUS);
251 switch (request->flags & (ATA_R_ATAPI | ATA_R_DMA | ATA_R_CONTROL)) {
252 /* ATA PIO data transfer and control commands */
255 /* on timeouts we have no data or anything so just return */
256 if (request->flags & ATA_R_TIMEOUT)
259 /* Read back registers to the request struct. */
260 if ((request->status & ATA_S_ERROR) ||
261 (request->flags & (ATA_R_CONTROL | ATA_R_NEEDRESULT))) {
262 ch->hw.tf_read(request);
265 /* if we got an error we are done with the HW */
266 if (request->status & ATA_S_ERROR) {
267 request->error = ATA_IDX_INB(ch, ATA_ERROR);
271 /* are we moving data ? */
272 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
273 /* if read data get it */
274 if (request->flags & ATA_R_READ) {
275 int flags = ATA_S_DRQ;
277 if (request->u.ata.command != ATA_ATAPI_IDENTIFY)
278 flags |= ATA_S_READY;
279 if (ata_wait(ch, request->unit, flags) < 0) {
280 device_printf(request->parent,
281 "timeout waiting for read DRQ\n");
282 request->result = EIO;
285 ata_pio_read(request, request->transfersize);
288 /* update how far we've gotten */
289 request->donecount += request->transfersize;
291 /* do we need a scoop more ? */
292 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) {
300 /* if we get an error here we are done with the HW */
301 if (ata_wait(ch, request->unit, (ATA_S_READY | ATA_S_DRQ)) < 0) {
302 device_printf(request->parent,
303 "timeout waiting for write DRQ\n");
304 request->status = ATA_IDX_INB(ch, ATA_STATUS);
308 /* output data and return waiting for new interrupt */
309 ata_pio_write(request, request->transfersize);
313 /* if data read command, return & wait for interrupt */
314 if (request->flags & ATA_R_READ)
321 /* ATA DMA data transfer commands */
324 /* stop DMA engine and get status */
326 request->dma->status = ch->dma.stop(request);
328 /* did we get error or data */
329 if (request->status & ATA_S_ERROR)
330 request->error = ATA_IDX_INB(ch, ATA_ERROR);
331 else if (request->dma->status & ATA_BMSTAT_ERROR)
332 request->status |= ATA_S_ERROR;
333 else if (!(request->flags & ATA_R_TIMEOUT))
334 request->donecount = request->bytecount;
336 /* Read back registers to the request struct. */
337 if ((request->status & ATA_S_ERROR) ||
338 (request->flags & (ATA_R_CONTROL | ATA_R_NEEDRESULT))) {
339 ch->hw.tf_read(request);
342 /* release SG list etc */
343 ch->dma.unload(request);
348 /* ATAPI PIO commands */
350 length = ATA_IDX_INB(ch, ATA_CYL_LSB)|(ATA_IDX_INB(ch, ATA_CYL_MSB)<<8);
352 /* on timeouts we have no data or anything so just return */
353 if (request->flags & ATA_R_TIMEOUT)
356 switch ((ATA_IDX_INB(ch, ATA_IREASON) & (ATA_I_CMD | ATA_I_IN)) |
357 (request->status & ATA_S_DRQ)) {
359 /* this seems to be needed for some (slow) devices */
362 if (!(request->status & ATA_S_DRQ)) {
363 device_printf(request->parent, "command interrupt without DRQ\n");
364 request->status = ATA_S_ERROR;
367 ATA_IDX_OUTSW_STRM(ch, ATA_DATA, (int16_t *)request->u.atapi.ccb,
368 (request->flags & ATA_R_ATAPI16) ? 8 : 6);
369 /* return wait for interrupt */
373 if (request->flags & ATA_R_READ) {
374 request->status = ATA_S_ERROR;
375 device_printf(request->parent,
376 "%s trying to write on read buffer\n",
377 ata_cmd2str(request));
380 ata_pio_write(request, length);
381 request->donecount += length;
383 /* set next transfer size according to HW capabilities */
384 request->transfersize = min((request->bytecount-request->donecount),
385 request->transfersize);
386 /* return wait for interrupt */
390 if (request->flags & ATA_R_WRITE) {
391 request->status = ATA_S_ERROR;
392 device_printf(request->parent,
393 "%s trying to read on write buffer\n",
394 ata_cmd2str(request));
397 ata_pio_read(request, length);
398 request->donecount += length;
400 /* set next transfer size according to HW capabilities */
401 request->transfersize = min((request->bytecount-request->donecount),
402 request->transfersize);
403 /* return wait for interrupt */
406 case ATAPI_P_DONEDRQ:
407 device_printf(request->parent,
408 "WARNING - %s DONEDRQ non conformant device\n",
409 ata_cmd2str(request));
410 if (request->flags & ATA_R_READ) {
411 ata_pio_read(request, length);
412 request->donecount += length;
414 else if (request->flags & ATA_R_WRITE) {
415 ata_pio_write(request, length);
416 request->donecount += length;
419 request->status = ATA_S_ERROR;
424 if (request->status & (ATA_S_ERROR | ATA_S_DWF))
425 request->error = ATA_IDX_INB(ch, ATA_ERROR);
429 device_printf(request->parent, "unknown transfer phase\n");
430 request->status = ATA_S_ERROR;
436 /* ATAPI DMA commands */
437 case ATA_R_ATAPI|ATA_R_DMA:
439 /* stop DMA engine and get status */
441 request->dma->status = ch->dma.stop(request);
443 /* did we get error or data */
444 if (request->status & (ATA_S_ERROR | ATA_S_DWF))
445 request->error = ATA_IDX_INB(ch, ATA_ERROR);
446 else if (request->dma->status & ATA_BMSTAT_ERROR)
447 request->status |= ATA_S_ERROR;
448 else if (!(request->flags & ATA_R_TIMEOUT))
449 request->donecount = request->bytecount;
451 /* release SG list etc */
452 ch->dma.unload(request);
458 printf("ata_end_transaction OOPS!!\n");
461 callout_stop(&request->callout);
462 return ATA_OP_FINISHED;
465 return ATA_OP_CONTINUES;
468 /* must be called with ATA channel locked and state_mtx held */
470 ata_generic_reset(device_t dev)
472 struct ata_channel *ch = device_get_softc(dev);
474 u_int8_t ostat0 = 0, stat0 = 0, ostat1 = 0, stat1 = 0;
475 u_int8_t err = 0, lsb = 0, msb = 0;
476 int mask = 0, timeout;
478 /* do we have any signs of ATA/ATAPI HW being present ? */
479 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | ATA_DEV(ATA_MASTER));
481 ostat0 = ATA_IDX_INB(ch, ATA_STATUS);
482 if (((ostat0 & 0xf8) != 0xf8 || (ch->flags & ATA_KNOWN_PRESENCE)) &&
488 /* in some setups we dont want to test for a slave */
489 if (!(ch->flags & ATA_NO_SLAVE)) {
490 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | ATA_DEV(ATA_SLAVE));
492 ostat1 = ATA_IDX_INB(ch, ATA_STATUS);
493 if (((ostat1 & 0xf8) != 0xf8 || (ch->flags & ATA_KNOWN_PRESENCE)) &&
501 device_printf(dev, "reset tp1 mask=%02x ostat0=%02x ostat1=%02x\n",
502 mask, ostat0, ostat1);
504 /* if nothing showed up there is no need to get any further */
505 /* XXX SOS is that too strong?, we just might lose devices here */
510 /* reset (both) devices on this channel */
511 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | ATA_DEV(ATA_MASTER));
513 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_IDS | ATA_A_RESET);
515 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_IDS);
517 ATA_IDX_INB(ch, ATA_ERROR);
519 /* wait for BUSY to go inactive */
520 for (timeout = 0; timeout < 310; timeout++) {
521 if ((mask & 0x01) && (stat0 & ATA_S_BUSY)) {
522 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(ATA_MASTER));
524 if (ch->flags & ATA_STATUS_IS_LONG)
525 stat0 = ATA_IDX_INL(ch, ATA_STATUS) & 0xff;
527 stat0 = ATA_IDX_INB(ch, ATA_STATUS);
528 err = ATA_IDX_INB(ch, ATA_ERROR);
529 lsb = ATA_IDX_INB(ch, ATA_CYL_LSB);
530 msb = ATA_IDX_INB(ch, ATA_CYL_MSB);
533 "stat0=0x%02x err=0x%02x lsb=0x%02x msb=0x%02x\n",
534 stat0, err, lsb, msb);
535 if (stat0 == err && lsb == err && msb == err &&
536 timeout > (stat0 & ATA_S_BUSY ? 100 : 10))
538 if (!(stat0 & ATA_S_BUSY)) {
539 if ((err & 0x7f) == ATA_E_ILI) {
540 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB) {
541 ch->devices |= ATA_ATAPI_MASTER;
543 else if (lsb == 0 && msb == 0 && (stat0 & ATA_S_READY)) {
544 ch->devices |= ATA_ATA_MASTER;
547 else if ((stat0 & 0x0f) && err == lsb && err == msb) {
553 if ((mask & 0x02) && (stat1 & ATA_S_BUSY) &&
554 !((mask & 0x01) && (stat0 & ATA_S_BUSY))) {
555 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(ATA_SLAVE));
557 if (ch->flags & ATA_STATUS_IS_LONG)
558 stat1 = ATA_IDX_INL(ch, ATA_STATUS) & 0xff;
560 stat1 = ATA_IDX_INB(ch, ATA_STATUS);
561 err = ATA_IDX_INB(ch, ATA_ERROR);
562 lsb = ATA_IDX_INB(ch, ATA_CYL_LSB);
563 msb = ATA_IDX_INB(ch, ATA_CYL_MSB);
566 "stat1=0x%02x err=0x%02x lsb=0x%02x msb=0x%02x\n",
567 stat1, err, lsb, msb);
568 if (stat1 == err && lsb == err && msb == err &&
569 timeout > (stat1 & ATA_S_BUSY ? 100 : 10))
571 if (!(stat1 & ATA_S_BUSY)) {
572 if ((err & 0x7f) == ATA_E_ILI) {
573 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB) {
574 ch->devices |= ATA_ATAPI_SLAVE;
576 else if (lsb == 0 && msb == 0 && (stat1 & ATA_S_READY)) {
577 ch->devices |= ATA_ATA_SLAVE;
580 else if ((stat1 & 0x0f) && err == lsb && err == msb) {
586 if ((ch->flags & ATA_KNOWN_PRESENCE) == 0 &&
587 timeout > ((mask == 0x03) ? 20 : 10)) {
588 if ((mask & 0x01) && stat0 == 0xff)
590 if ((mask & 0x02) && stat1 == 0xff)
593 if (((mask & 0x01) == 0 || !(stat0 & ATA_S_BUSY)) &&
594 ((mask & 0x02) == 0 || !(stat1 & ATA_S_BUSY)))
600 device_printf(dev, "reset tp2 stat0=%02x stat1=%02x devices=0x%x\n",
601 stat0, stat1, ch->devices);
604 /* must be called with ATA channel locked and state_mtx held */
606 ata_generic_status(device_t dev)
608 struct ata_channel *ch = device_get_softc(dev);
610 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
612 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
619 ata_wait(struct ata_channel *ch, int unit, u_int8_t mask)
626 /* wait at max 1 second for device to get !BUSY */
627 while (timeout < 1000000) {
628 status = ATA_IDX_INB(ch, ATA_ALTSTAT);
630 /* if drive fails status, reselect the drive and try again */
631 if (status == 0xff) {
632 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(unit));
639 if (!(status & ATA_S_BUSY))
642 if (timeout > 1000) {
651 if (timeout >= 1000000)
654 return (status & ATA_S_ERROR);
658 /* wait 50 msec for bits wanted */
661 status = ATA_IDX_INB(ch, ATA_ALTSTAT);
662 if ((status & mask) == mask)
663 return (status & ATA_S_ERROR);
670 ata_generic_command(struct ata_request *request)
672 struct ata_channel *ch = device_get_softc(request->parent);
675 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | ATA_DEV(request->unit));
677 /* ready to issue command ? */
678 if (ata_wait(ch, request->unit, 0) < 0) {
679 device_printf(request->parent, "timeout waiting to issue command\n");
680 request->flags |= ATA_R_TIMEOUT;
684 /* enable interrupt */
685 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_4BIT);
687 if (request->flags & ATA_R_ATAPI) {
691 /* issue packet command to controller */
692 if (request->flags & ATA_R_DMA) {
693 ATA_IDX_OUTB(ch, ATA_FEATURE, ATA_F_DMA);
694 ATA_IDX_OUTB(ch, ATA_CYL_LSB, 0);
695 ATA_IDX_OUTB(ch, ATA_CYL_MSB, 0);
698 ATA_IDX_OUTB(ch, ATA_FEATURE, 0);
699 ATA_IDX_OUTB(ch, ATA_CYL_LSB, request->transfersize);
700 ATA_IDX_OUTB(ch, ATA_CYL_MSB, request->transfersize >> 8);
702 ATA_IDX_OUTB(ch, ATA_COMMAND, ATA_PACKET_CMD);
704 /* command interrupt device ? just return and wait for interrupt */
705 if (request->flags & ATA_R_ATAPI_INTR)
708 /* command processed ? */
709 res = ata_wait(ch, request->unit, 0);
712 device_printf(request->parent,
713 "timeout waiting for PACKET command\n");
714 request->flags |= ATA_R_TIMEOUT;
718 /* wait for ready to write ATAPI command block */
720 int reason = ATA_IDX_INB(ch, ATA_IREASON);
721 int status = ATA_IDX_INB(ch, ATA_STATUS);
723 if (((reason & (ATA_I_CMD | ATA_I_IN)) |
724 (status & (ATA_S_DRQ | ATA_S_BUSY))) == ATAPI_P_CMDOUT)
729 device_printf(request->parent,
730 "timeout waiting for ATAPI ready\n");
731 request->flags |= ATA_R_TIMEOUT;
735 /* this seems to be needed for some (slow) devices */
738 /* output command block */
739 ATA_IDX_OUTSW_STRM(ch, ATA_DATA, (int16_t *)request->u.atapi.ccb,
740 (request->flags & ATA_R_ATAPI16) ? 8 : 6);
743 ch->hw.tf_write(request);
745 /* issue command to controller */
746 ATA_IDX_OUTB(ch, ATA_COMMAND, request->u.ata.command);
752 ata_tf_read(struct ata_request *request)
754 struct ata_channel *ch = device_get_softc(request->parent);
756 if (request->flags & ATA_R_48BIT) {
757 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_4BIT | ATA_A_HOB);
758 request->u.ata.count = (ATA_IDX_INB(ch, ATA_COUNT) << 8);
760 ((u_int64_t)(ATA_IDX_INB(ch, ATA_SECTOR)) << 24) |
761 ((u_int64_t)(ATA_IDX_INB(ch, ATA_CYL_LSB)) << 32) |
762 ((u_int64_t)(ATA_IDX_INB(ch, ATA_CYL_MSB)) << 40);
764 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_4BIT);
765 request->u.ata.count |= ATA_IDX_INB(ch, ATA_COUNT);
766 request->u.ata.lba |=
767 (ATA_IDX_INB(ch, ATA_SECTOR) |
768 (ATA_IDX_INB(ch, ATA_CYL_LSB) << 8) |
769 (ATA_IDX_INB(ch, ATA_CYL_MSB) << 16));
772 request->u.ata.count = ATA_IDX_INB(ch, ATA_COUNT);
773 request->u.ata.lba = ATA_IDX_INB(ch, ATA_SECTOR) |
774 (ATA_IDX_INB(ch, ATA_CYL_LSB) << 8) |
775 (ATA_IDX_INB(ch, ATA_CYL_MSB) << 16) |
776 ((ATA_IDX_INB(ch, ATA_DRIVE) & 0xf) << 24);
781 ata_tf_write(struct ata_request *request)
783 struct ata_channel *ch = device_get_softc(request->parent);
785 if (request->flags & ATA_R_48BIT) {
786 ATA_IDX_OUTB(ch, ATA_FEATURE, request->u.ata.feature >> 8);
787 ATA_IDX_OUTB(ch, ATA_FEATURE, request->u.ata.feature);
788 ATA_IDX_OUTB(ch, ATA_COUNT, request->u.ata.count >> 8);
789 ATA_IDX_OUTB(ch, ATA_COUNT, request->u.ata.count);
790 ATA_IDX_OUTB(ch, ATA_SECTOR, request->u.ata.lba >> 24);
791 ATA_IDX_OUTB(ch, ATA_SECTOR, request->u.ata.lba);
792 ATA_IDX_OUTB(ch, ATA_CYL_LSB, request->u.ata.lba >> 32);
793 ATA_IDX_OUTB(ch, ATA_CYL_LSB, request->u.ata.lba >> 8);
794 ATA_IDX_OUTB(ch, ATA_CYL_MSB, request->u.ata.lba >> 40);
795 ATA_IDX_OUTB(ch, ATA_CYL_MSB, request->u.ata.lba >> 16);
796 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_LBA | ATA_DEV(request->unit));
799 ATA_IDX_OUTB(ch, ATA_FEATURE, request->u.ata.feature);
800 ATA_IDX_OUTB(ch, ATA_COUNT, request->u.ata.count);
801 ATA_IDX_OUTB(ch, ATA_SECTOR, request->u.ata.lba);
802 ATA_IDX_OUTB(ch, ATA_CYL_LSB, request->u.ata.lba >> 8);
803 ATA_IDX_OUTB(ch, ATA_CYL_MSB, request->u.ata.lba >> 16);
804 ATA_IDX_OUTB(ch, ATA_DRIVE,
805 ATA_D_IBM | ATA_D_LBA | ATA_DEV(request->unit) |
806 ((request->u.ata.lba >> 24) & 0x0f));
811 ata_pio_read(struct ata_request *request, int length)
813 struct ata_channel *ch = device_get_softc(request->parent);
817 int todo, done, off, moff, resid, size, i;
818 uint8_t buf[2] __aligned(2);
820 todo = min(request->transfersize, length);
821 page = done = resid = 0;
822 while (done < todo) {
825 /* Prepare data address and limit size (if not sequential). */
826 off = request->donecount + done;
827 if ((request->flags & ATA_R_DATA_IN_CCB) == 0 ||
828 (request->ccb->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_VADDR) {
829 addr = (uint8_t *)request->data + off;
830 } else if ((request->ccb->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_BIO) {
831 bio = (struct bio *)request->data;
832 if ((bio->bio_flags & BIO_UNMAPPED) == 0) {
833 addr = (uint8_t *)bio->bio_data + off;
835 moff = bio->bio_ma_offset + off;
836 page = pmap_quick_enter_page(
837 bio->bio_ma[moff / PAGE_SIZE]);
839 size = min(size, PAGE_SIZE - moff);
840 addr = (void *)(page + moff);
843 panic("ata_pio_read: Unsupported CAM data type %x\n",
844 (request->ccb->ccb_h.flags & CAM_DATA_MASK));
846 /* We may have extra byte already read but not stored. */
854 /* Process main part of data. */
856 if (__predict_false((ch->flags & ATA_USE_16BIT) ||
857 (size % 4) != 0 || ((uintptr_t)addr % 4) != 0)) {
858 #ifndef __NO_STRICT_ALIGNMENT
859 if (__predict_false((uintptr_t)addr % 2)) {
860 for (i = 0; i + 1 < size; i += 2) {
862 ATA_IDX_INW_STRM(ch, ATA_DATA);
864 addr[i + 1] = buf[1];
868 ATA_IDX_INSW_STRM(ch, ATA_DATA, (void*)addr,
871 /* If we have extra byte of data, leave it for later. */
874 ATA_IDX_INW_STRM(ch, ATA_DATA);
875 addr[size - 1] = buf[0];
878 ATA_IDX_INSL_STRM(ch, ATA_DATA, (void*)addr, size / 4);
881 pmap_quick_remove_page(page);
888 device_printf(request->parent,
889 "WARNING - %s read data overrun %d > %d\n",
890 ata_cmd2str(request), length, done);
891 for (i = done + resid; i < length; i += 2)
892 ATA_IDX_INW(ch, ATA_DATA);
897 ata_pio_write(struct ata_request *request, int length)
899 struct ata_channel *ch = device_get_softc(request->parent);
903 int todo, done, off, moff, resid, size, i;
904 uint8_t buf[2] __aligned(2);
906 todo = min(request->transfersize, length);
907 page = done = resid = 0;
908 while (done < todo) {
911 /* Prepare data address and limit size (if not sequential). */
912 off = request->donecount + done;
913 if ((request->flags & ATA_R_DATA_IN_CCB) == 0 ||
914 (request->ccb->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_VADDR) {
915 addr = (uint8_t *)request->data + off;
916 } else if ((request->ccb->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_BIO) {
917 bio = (struct bio *)request->data;
918 if ((bio->bio_flags & BIO_UNMAPPED) == 0) {
919 addr = (uint8_t *)bio->bio_data + off;
921 moff = bio->bio_ma_offset + off;
922 page = pmap_quick_enter_page(
923 bio->bio_ma[moff / PAGE_SIZE]);
925 size = min(size, PAGE_SIZE - moff);
926 addr = (void *)(page + moff);
929 panic("ata_pio_write: Unsupported CAM data type %x\n",
930 (request->ccb->ccb_h.flags & CAM_DATA_MASK));
932 /* We may have extra byte to be written first. */
935 ATA_IDX_OUTW_STRM(ch, ATA_DATA, *(uint16_t *)&buf);
941 /* Process main part of data. */
943 if (__predict_false((ch->flags & ATA_USE_16BIT) ||
944 (size % 4) != 0 || ((uintptr_t)addr % 4) != 0)) {
945 #ifndef __NO_STRICT_ALIGNMENT
946 if (__predict_false((uintptr_t)addr % 2)) {
947 for (i = 0; i + 1 < size; i += 2) {
949 buf[1] = addr[i + 1];
950 ATA_IDX_OUTW_STRM(ch, ATA_DATA,
955 ATA_IDX_OUTSW_STRM(ch, ATA_DATA, (void*)addr,
958 /* If we have extra byte of data, save it for later. */
960 buf[0] = addr[size - 1];
962 ATA_IDX_OUTSL_STRM(ch, ATA_DATA,
963 (void*)addr, size / sizeof(int32_t));
966 pmap_quick_remove_page(page);
972 /* We may have extra byte of data to be written. Pad it with zero. */
975 ATA_IDX_OUTW_STRM(ch, ATA_DATA, *(uint16_t *)&buf);
979 device_printf(request->parent,
980 "WARNING - %s write data underrun %d > %d\n",
981 ata_cmd2str(request), length, done);
982 for (i = done + resid; i < length; i += 2)
983 ATA_IDX_OUTW(ch, ATA_DATA, 0);