2 * Copyright (c) 1998,1999,2000,2001,2002 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.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
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.
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/malloc.h>
39 #include <sys/sysctl.h>
40 #include <machine/bus.h>
42 #include <dev/ata/ata-all.h>
43 #include <dev/ata/atapi-all.h>
46 static void atapi_read(struct atapi_request *, int);
47 static void atapi_write(struct atapi_request *, int);
48 static void atapi_finish(struct atapi_request *);
49 static void atapi_timeout(struct atapi_request *);
50 static char *atapi_cmd2str(u_int8_t);
51 static char *atapi_skey2str(u_int8_t);
54 #define ATAPI_MAX_RETRIES 3
57 static int atapi_dma = 0;
58 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
59 static MALLOC_DEFINE(M_ATAPI, "ATAPI generic", "ATAPI driver generic layer");
63 SYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RD, &atapi_dma, 0,
64 "ATAPI device DMA mode control");
67 atapi_attach(struct ata_device *atadev)
70 ata_prtdev(atadev, "piomode=%d dmamode=%d udmamode=%d dmaflag=%d\n",
71 ata_pmode(atadev->param), ata_wmode(atadev->param),
72 ata_umode(atadev->param), atadev->param->support_dma);
74 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
75 if (atapi_dma && !(atadev->param->drq_type == ATAPI_DRQT_INTR)) {
77 (ata_pmode(atadev->param) < 0) ?
78 (atadev->param->support_dma ? 4:0):ata_pmode(atadev->param),
79 (ata_wmode(atadev->param) < 0) ?
80 (atadev->param->support_dma ? 2:0):ata_wmode(atadev->param),
81 ata_umode(atadev->param));
85 ata_pmode(atadev->param) < 0 ? 0 : ata_pmode(atadev->param),
87 ATA_UNLOCK_CH(atadev->channel);
89 if (!(atadev->result = malloc(sizeof(struct atapi_reqsense), M_ATAPI,
91 ata_prtdev(atadev, "no memory for sense data\n");
93 switch (atadev->param->type) {
95 case ATAPI_TYPE_CDROM:
96 if (acdattach(atadev))
101 case ATAPI_TYPE_DIRECT:
102 if (afdattach(atadev))
107 case ATAPI_TYPE_TAPE:
108 if (astattach(atadev))
114 ata_prtdev(atadev, "<%.40s/%.8s> - NO DRIVER!\n",
115 atadev->param->model, atadev->param->revision);
116 free(atadev->result, M_ATAPI);
117 atadev->driver = NULL;
122 atapi_detach(struct ata_device *atadev)
124 struct atapi_request *request;
126 atadev->flags |= ATA_D_DETACHING;
127 ata_prtdev(atadev, "removed from configuration\n");
128 switch (atadev->param->type) {
130 case ATAPI_TYPE_CDROM:
135 case ATAPI_TYPE_DIRECT:
140 case ATAPI_TYPE_TAPE:
147 TAILQ_FOREACH(request, &atadev->channel->atapi_queue, chain) {
148 if (request->device != atadev)
150 TAILQ_REMOVE(&atadev->channel->atapi_queue, request, chain);
151 if (request->driver) {
152 struct bio *bp = (struct bio *) request->driver;
153 biofinish(bp, NULL, ENXIO);
155 free(request, M_ATAPI);
158 free(atadev->result, M_ATAPI);
159 atadev->driver = NULL;
165 atapi_queue_cmd(struct ata_device *atadev, int8_t *ccb, caddr_t data,
166 int count, int flags, int timeout,
167 atapi_callback_t callback, void *driver)
169 struct atapi_request *request;
172 if (!(request = malloc(sizeof(struct atapi_request), M_ATAPI,
176 request->device = atadev;
177 request->data = data;
178 request->bytecount = count;
179 request->flags = flags;
180 request->error = EINPROGRESS;
181 request->timeout = timeout * hz;
182 request->ccbsize = atadev->param->packet_size ? 16 : 12;
183 bcopy(ccb, request->ccb, request->ccbsize);
185 request->callback = callback;
186 request->driver = driver;
188 if (atadev->mode >= ATA_DMA) {
189 if (ata_dmaalloc(atadev))
190 atadev->mode = ATA_PIO;
194 ata_prtdev(atadev, "queueing %s ", atapi_cmd2str(request->ccb[0]));
195 atapi_dump("ccb = ", &request->ccb[0], sizeof(request->ccb));
197 /* append onto controller queue and try to start controller */
199 if (flags & ATPR_F_AT_HEAD)
200 TAILQ_INSERT_HEAD(&atadev->channel->atapi_queue, request, chain);
202 TAILQ_INSERT_TAIL(&atadev->channel->atapi_queue, request, chain);
203 ata_start(atadev->channel);
205 /* if callback used, then just return, gets called from interrupt context */
211 /* only sleep when command is in progress */
212 if (request->error == EINPROGRESS)
213 tsleep((caddr_t)request, PRIBIO, "atprq", 0);
215 error = request->error;
217 bcopy(&request->sense, atadev->result, sizeof(struct atapi_reqsense));
218 free(request, M_ATAPI);
223 atapi_start(struct ata_device *atadev)
225 switch (atadev->param->type) {
227 case ATAPI_TYPE_CDROM:
232 case ATAPI_TYPE_DIRECT:
237 case ATAPI_TYPE_TAPE:
247 atapi_transfer(struct atapi_request *request)
249 struct ata_device *atadev = request->device;
254 ata_prtdev(atadev, "starting %s ", atapi_cmd2str(request->ccb[0]));
255 atapi_dump("ccb = ", &request->ccb[0], sizeof(request->ccb));
257 /* is this just a POLL DSC command ? */
258 if (request->ccb[0] == ATAPI_POLL_DSC) {
259 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
261 if (ATA_INB(atadev->channel->r_altio, ATA_ALTSTAT) & ATA_S_DSC)
264 request->error = EBUSY;
265 atapi_finish(request);
266 return ATA_OP_FINISHED;
269 /* start timeout for this command */
270 request->timeout_handle = timeout((timeout_t *)atapi_timeout,
271 request, request->timeout);
273 if (!(request->flags & ATPR_F_INTERNAL))
274 atadev->cmd = request->ccb[0];
276 /* if DMA enabled setup DMA hardware */
277 request->flags &= ~ATPR_F_DMA_USED;
278 if ((atadev->mode >= ATA_DMA) &&
279 (request->ccb[0] == ATAPI_READ ||
280 request->ccb[0] == ATAPI_READ_BIG ||
281 request->ccb[0] == ATAPI_READ_CD ||
282 ((request->ccb[0] == ATAPI_WRITE ||
283 request->ccb[0] == ATAPI_WRITE_BIG) &&
284 !(atadev->channel->flags & ATA_ATAPI_DMA_RO))) &&
285 !ata_dmasetup(atadev, (void *)request->data, request->bytecount)) {
286 request->flags |= ATPR_F_DMA_USED;
289 /* start ATAPI operation */
290 if (ata_command(atadev, ATA_C_PACKET_CMD,
291 min(request->bytecount, 65534) << 8, 0,
292 (request->flags & ATPR_F_DMA_USED) ? ATA_F_DMA : 0,
294 ata_prtdev(atadev, "failure to send ATAPI packet command\n");
296 if (request->flags & ATPR_F_DMA_USED)
297 ata_dmastart(atadev, request->data, request->bytecount,
298 request->flags & ATPR_F_READ);
300 /* command interrupt device ? just return */
301 if (atadev->param->drq_type == ATAPI_DRQT_INTR)
302 return ATA_OP_CONTINUES;
304 /* ready to write ATAPI command */
305 timout = 5000; /* might be less for fast devices */
307 reason = ATA_INB(atadev->channel->r_io, ATA_IREASON);
308 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
309 if (((reason & (ATA_I_CMD | ATA_I_IN)) |
310 (atadev->channel->status&(ATA_S_DRQ|ATA_S_BUSY)))==ATAPI_P_CMDOUT)
315 ata_prtdev(atadev, "failure to execute ATAPI packet command\n");
316 untimeout((timeout_t *)atapi_timeout, request, request->timeout_handle);
317 request->error = EIO;
318 atapi_finish(request);
319 return ATA_OP_FINISHED;
322 /* this seems to be needed for some (slow) devices */
325 /* send actual command */
326 ATA_OUTSW_STRM(atadev->channel->r_io, ATA_DATA, (int16_t *)request->ccb,
327 request->ccbsize / sizeof(int16_t));
328 return ATA_OP_CONTINUES;
332 atapi_interrupt(struct atapi_request *request)
334 struct ata_device *atadev = request->device;
335 int reason, dma_stat = 0;
337 reason = (ATA_INB(atadev->channel->r_io, ATA_IREASON)&(ATA_I_CMD|ATA_I_IN))|
338 (atadev->channel->status & ATA_S_DRQ);
340 if (reason == ATAPI_P_CMDOUT) {
341 if (!(atadev->channel->status & ATA_S_DRQ)) {
342 ata_prtdev(atadev, "command interrupt without DRQ\n");
343 untimeout((timeout_t *)atapi_timeout,
344 request, request->timeout_handle);
345 request->error = EIO;
346 atapi_finish(request);
347 return ATA_OP_FINISHED;
349 ATA_OUTSW_STRM(atadev->channel->r_io, ATA_DATA, (int16_t *)request->ccb,
350 request->ccbsize / sizeof(int16_t));
351 return ATA_OP_CONTINUES;
354 if (request->flags & ATPR_F_DMA_USED) {
355 dma_stat = ata_dmadone(atadev);
356 if ((atadev->channel->status & (ATA_S_ERROR | ATA_S_DWF)) ||
357 dma_stat & ATA_BMSTAT_ERROR) {
358 request->result = ATA_INB(atadev->channel->r_io, ATA_ERROR);
362 request->donecount = request->bytecount;
363 request->bytecount = 0;
367 int length = ATA_INB(atadev->channel->r_io, ATA_CYL_LSB) |
368 ATA_INB(atadev->channel->r_io, ATA_CYL_MSB) << 8;
372 if (request->flags & ATPR_F_READ) {
373 request->result = ATA_INB(atadev->channel->r_io, ATA_ERROR);
374 ata_prtdev(atadev, "%s trying to write on read buffer\n",
375 atapi_cmd2str(atadev->cmd));
378 atapi_write(request, length);
379 return ATA_OP_CONTINUES;
382 if (!(request->flags & ATPR_F_READ)) {
383 request->result = ATA_INB(atadev->channel->r_io, ATA_ERROR);
384 ata_prtdev(atadev, "%s trying to read on write buffer\n",
385 atapi_cmd2str(atadev->cmd));
388 atapi_read(request, length);
389 return ATA_OP_CONTINUES;
391 case ATAPI_P_DONEDRQ:
392 ata_prtdev(atadev, "%s DONEDRQ\n", atapi_cmd2str(atadev->cmd));
393 if (request->flags & ATPR_F_READ)
394 atapi_read(request, length);
396 atapi_write(request, length);
401 if (atadev->channel->status & (ATA_S_ERROR | ATA_S_DWF))
402 request->result = ATA_INB(atadev->channel->r_io, ATA_ERROR);
404 if (!(request->flags & ATPR_F_INTERNAL))
409 ata_prtdev(atadev, "unknown transfer phase %d\n", reason);
412 untimeout((timeout_t *)atapi_timeout, request, request->timeout_handle);
414 /* check for error, if valid sense key, queue a request sense cmd */
415 if ((request->result & ATAPI_SK_MASK) &&
416 request->ccb[0] != ATAPI_REQUEST_SENSE) {
417 bzero(request->ccb, request->ccbsize);
418 request->ccb[0] = ATAPI_REQUEST_SENSE;
419 request->ccb[4] = sizeof(struct atapi_reqsense);
420 request->bytecount = sizeof(struct atapi_reqsense);
421 request->flags &= ATPR_F_QUIET;
422 request->flags |= ATPR_F_READ | ATPR_F_INTERNAL;
423 TAILQ_INSERT_HEAD(&atadev->channel->atapi_queue, request, chain);
426 if (request->result) {
427 switch ((request->result & ATAPI_SK_MASK)) {
428 case ATAPI_SK_NO_SENSE:
432 case ATAPI_SK_RECOVERED_ERROR:
433 ata_prtdev(atadev, "%s - recovered error\n",
434 atapi_cmd2str(atadev->cmd));
438 case ATAPI_SK_NOT_READY:
439 request->error = EBUSY;
442 case ATAPI_SK_UNIT_ATTENTION:
443 atadev->flags |= ATA_D_MEDIA_CHANGED;
444 request->error = EIO;
448 request->error = EIO;
450 if (request->flags & ATPR_F_QUIET)
453 ata_prtdev(atadev, "%s - %s asc=0x%02x ascq=0x%02x ",
454 atapi_cmd2str(atadev->cmd),
455 atapi_skey2str(request->sense.sense_key),
456 request->sense.asc, request->sense.ascq);
457 if (request->sense.sksv)
458 printf("sks=0x%02x 0x%02x 0x%02x ",
459 request->sense.sk_specific,
460 request->sense.sk_specific1,
461 request->sense.sk_specific2);
462 printf("error=0x%02x\n", request->result & ATAPI_E_MASK);
467 atapi_finish(request);
469 return ATA_OP_FINISHED;
473 atapi_reinit(struct ata_device *atadev)
475 /* reinit device parameters */
476 if (atadev->mode >= ATA_DMA)
478 (ata_pmode(atadev->param) < 0) ?
479 (atadev->param->support_dma ? 4:0):ata_pmode(atadev->param),
480 (ata_wmode(atadev->param) < 0) ?
481 (atadev->param->support_dma ? 2:0):ata_wmode(atadev->param),
482 ata_umode(atadev->param));
485 ata_pmode(atadev->param)<0 ? 0 : ata_pmode(atadev->param),
490 atapi_test_ready(struct ata_device *atadev)
492 int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
493 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
495 return atapi_queue_cmd(atadev, ccb, NULL, 0, 0, 30, NULL, NULL);
499 atapi_wait_dsc(struct ata_device *atadev, int timeout)
502 int8_t ccb[16] = { ATAPI_POLL_DSC, 0, 0, 0, 0,
503 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
506 while (timeout > 0) {
507 error = atapi_queue_cmd(atadev, ccb, NULL, 0, 0, 0, NULL, NULL);
510 tsleep((caddr_t)&error, PRIBIO, "atpwt", hz / 2);
517 atapi_dump(char *label, void *data, int len)
521 printf("%s %02x", label, *p++);
523 printf ("-%02x", *p++);
528 atapi_read(struct atapi_request *request, int length)
530 int8_t **buffer = (int8_t **)&request->data;
531 int size = min(request->bytecount, length);
532 struct ata_channel *ch = request->device->channel;
535 if (request->flags & ATPR_F_INTERNAL)
536 *buffer = (int8_t *)&request->sense;
538 if (ch->flags & ATA_USE_16BIT || (size % sizeof(int32_t)))
539 ATA_INSW_STRM(ch->r_io, ATA_DATA, (void *)((uintptr_t)*buffer),
540 size / sizeof(int16_t));
542 ATA_INSL_STRM(ch->r_io, ATA_DATA, (void *)((uintptr_t)*buffer),
543 size / sizeof(int32_t));
545 if (request->bytecount < length) {
546 ata_prtdev(request->device, "read data overrun %d/%d\n",
547 length, request->bytecount);
548 for (resid=request->bytecount; resid<length; resid+=sizeof(int16_t))
549 ATA_INW(ch->r_io, ATA_DATA);
552 request->bytecount -= size;
553 request->donecount += size;
557 atapi_write(struct atapi_request *request, int length)
559 int8_t **buffer = (int8_t **)&request->data;
560 int size = min(request->bytecount, length);
561 struct ata_channel *ch = request->device->channel;
564 if (request->flags & ATPR_F_INTERNAL)
565 *buffer = (int8_t *)&request->sense;
567 if (ch->flags & ATA_USE_16BIT || (size % sizeof(int32_t)))
568 ATA_OUTSW_STRM(ch->r_io, ATA_DATA, (void *)((uintptr_t)*buffer),
569 size / sizeof(int16_t));
571 ATA_OUTSL_STRM(ch->r_io, ATA_DATA, (void *)((uintptr_t)*buffer),
572 size / sizeof(int32_t));
574 if (request->bytecount < length) {
575 ata_prtdev(request->device, "write data underrun %d/%d\n",
576 length, request->bytecount);
577 for (resid=request->bytecount; resid<length; resid+=sizeof(int16_t))
578 ATA_OUTW(ch->r_io, ATA_DATA, 0);
581 request->bytecount -= size;
582 request->donecount += size;
586 atapi_finish(struct atapi_request *request)
589 ata_prtdev(request->device, "finished %s%s\n",
590 request->callback ? "callback " : "",
591 atapi_cmd2str(request->ccb[0]));
593 if (request->callback) {
594 if (!((request->callback)(request)))
595 free(request, M_ATAPI);
598 wakeup((caddr_t)request);
602 atapi_timeout(struct atapi_request *request)
604 struct ata_device *atadev = request->device;
606 atadev->channel->running = NULL;
607 ata_prtdev(atadev, "%s command timeout - resetting\n",
608 atapi_cmd2str(request->ccb[0]));
610 if (request->flags & ATPR_F_DMA_USED) {
612 if (request->retries == ATAPI_MAX_RETRIES) {
614 (ata_pmode(atadev->param) < 0) ? 0 :
615 ata_pmode(atadev->param), -1, -1);
616 ata_prtdev(atadev, "trying fallback to PIO mode\n");
617 request->retries = 0;
621 /* if retries still permit, reinject this request */
622 if (request->retries++ < ATAPI_MAX_RETRIES) {
625 TAILQ_INSERT_HEAD(&atadev->channel->atapi_queue, request, chain);
629 /* retries all used up, return error */
630 request->error = EIO;
631 wakeup((caddr_t)request);
633 ata_reinit(atadev->channel);
637 atapi_cmd2str(u_int8_t cmd)
640 case 0x00: return ("TEST_UNIT_READY");
641 case 0x01: return ("REZERO");
642 case 0x03: return ("REQUEST_SENSE");
643 case 0x04: return ("FORMAT_UNIT");
644 case 0x08: return ("READ");
645 case 0x0a: return ("WRITE");
646 case 0x10: return ("WEOF");
647 case 0x11: return ("SPACE");
648 case 0x15: return ("MODE_SELECT");
649 case 0x19: return ("ERASE");
650 case 0x1a: return ("MODE_SENSE");
651 case 0x1b: return ("START_STOP");
652 case 0x1e: return ("PREVENT_ALLOW");
653 case 0x23: return ("ATAPI_READ_FORMAT_CAPACITIES");
654 case 0x25: return ("READ_CAPACITY");
655 case 0x28: return ("READ_BIG");
656 case 0x2a: return ("WRITE_BIG");
657 case 0x2b: return ("LOCATE");
658 case 0x34: return ("READ_POSITION");
659 case 0x35: return ("SYNCHRONIZE_CACHE");
660 case 0x3b: return ("WRITE_BUFFER");
661 case 0x3c: return ("READ_BUFFER");
662 case 0x42: return ("READ_SUBCHANNEL");
663 case 0x43: return ("READ_TOC");
664 case 0x45: return ("PLAY_10");
665 case 0x47: return ("PLAY_MSF");
666 case 0x48: return ("PLAY_TRACK");
667 case 0x4b: return ("PAUSE");
668 case 0x51: return ("READ_DISK_INFO");
669 case 0x52: return ("READ_TRACK_INFO");
670 case 0x53: return ("RESERVE_TRACK");
671 case 0x54: return ("SEND_OPC_INFO");
672 case 0x55: return ("MODE_SELECT_BIG");
673 case 0x58: return ("REPAIR_TRACK");
674 case 0x59: return ("READ_MASTER_CUE");
675 case 0x5a: return ("MODE_SENSE_BIG");
676 case 0x5b: return ("CLOSE_TRACK/SESSION");
677 case 0x5c: return ("READ_BUFFER_CAPACITY");
678 case 0x5d: return ("SEND_CUE_SHEET");
679 case 0xa1: return ("BLANK_CMD");
680 case 0xa3: return ("SEND_KEY");
681 case 0xa4: return ("REPORT_KEY");
682 case 0xa5: return ("PLAY_12");
683 case 0xa6: return ("LOAD_UNLOAD");
684 case 0xad: return ("READ_DVD_STRUCTURE");
685 case 0xb4: return ("PLAY_CD");
686 case 0xbb: return ("SET_SPEED");
687 case 0xbd: return ("MECH_STATUS");
688 case 0xbe: return ("READ_CD");
689 case 0xff: return ("POLL_DSC");
691 static char buffer[20];
692 sprintf(buffer, "unknown CMD (0x%02x)", cmd);
699 atapi_skey2str(u_int8_t skey)
702 case 0x00: return ("NO SENSE");
703 case 0x01: return ("RECOVERED ERROR");
704 case 0x02: return ("NOT READY");
705 case 0x03: return ("MEDIUM ERROR");
706 case 0x04: return ("HARDWARE ERROR");
707 case 0x05: return ("ILLEGAL REQUEST");
708 case 0x06: return ("UNIT ATTENTION");
709 case 0x07: return ("DATA PROTECT");
710 case 0x08: return ("BLANK CHECK");
711 case 0x09: return ("VENDOR SPECIFIC");
712 case 0x0a: return ("COPY ABORTED");
713 case 0x0b: return ("ABORTED COMMAND");
714 case 0x0c: return ("EQUAL");
715 case 0x0d: return ("VOLUME OVERFLOW");
716 case 0x0e: return ("MISCOMPARE");
717 case 0x0f: return ("RESERVED");
718 default: return("UNKNOWN");