FreeBSD/Linux Kernel Cross Reference
sys/pc98/pc98/atapi.c
1 /*
2 * Device-independent level for ATAPI drivers.
3 *
4 * Copyright (C) 1995 Cronyx Ltd.
5 * Author Serge Vakulenko, <vak@cronyx.ru>
6 *
7 * This software is distributed with NO WARRANTIES, not even the implied
8 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9 *
10 * Authors grant any other persons or organisations permission to use
11 * or modify this software as long as this message is kept with the software,
12 * all derivative works or modified versions.
13 *
14 * Version 1.9, Mon Oct 9 22:34:47 MSK 1995
15 *
16 * $FreeBSD: releng/5.0/sys/pc98/pc98/atapi.c 90521 2002-02-11 05:46:25Z nyan $
17 */
18
19 /*
20 * The ATAPI level is implemented as a machine-dependent layer
21 * between the device driver and the IDE controller.
22 * All the machine- and controller dependency is isolated inside
23 * the ATAPI level, while all the device dependency is located
24 * in the device subdriver.
25 *
26 * It seems that an ATAPI bus will became popular for medium-speed
27 * storage devices such as CD-ROMs, magneto-optical disks, tape streamers etc.
28 *
29 * To ease the development of new ATAPI drivers, the subdriver
30 * interface was designed to be as simple as possible.
31 *
32 * Three routines are available for the subdriver to access the device:
33 *
34 * struct atapires atapi_request_wait (ata, unit, cmd, a1, a2, a3, a4, a5,
35 * a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, addr, count);
36 * struct atapi *ata; -- atapi controller descriptor
37 * int unit; -- device unit number on the IDE bus
38 * u_char cmd; -- ATAPI command code
39 * u_char a1..a15; -- ATAPI command arguments
40 * char *addr; -- address of the data buffer for i/o
41 * int count; -- data length, >0 for read ops, <0 for write ops
42 *
43 * The atapi_request_wait() function puts the op in the queue of ATAPI
44 * commands for the IDE controller, starts the controller, the waits for
45 * operation to be completed (using tsleep).
46 * The function should be called from the user phase only (open(), close(),
47 * ioctl() etc).
48 * Ata and unit args are the values which the subdriver gets from the ATAPI
49 * level via attach() call.
50 * Buffer pointed to by *addr should be placed in core memory, static
51 * or dynamic, but not in stack.
52 * The function returns the error code structure, which consists of:
53 * - atapi driver code value
54 * - controller status port value
55 * - controller error port value
56 *
57 * struct atapires atapi_request_immediate (ata, unit, cmd, a1, a2, a3,
58 * a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15,
59 * addr, count);
60 *
61 * The atapi_request_immediate() function is similar to atapi_request_wait(),
62 * but it does not use interrupts for performing the request.
63 * It should be used during an attach phase to get parameters from the device.
64 *
65 * void atapi_request_callback (ata, unit, cmd, a1, a2, a3, a4, a5,
66 * a6, a7, a8, a9, a10, a11, a12, a13, a14, a15,
67 * addr, count, done, x, y);
68 * struct atapi *ata; -- atapi controller descriptor
69 * int unit; -- device unit number on the IDE bus
70 * u_char cmd; -- ATAPI command code
71 * u_char a1..a15; -- ATAPI command arguments
72 * char *addr; -- address of the data buffer for i/o
73 * int count; -- data length, >0 for read ops, <0 for write ops
74 * void (*done)(); -- function to call when op finished
75 * void *x, *y; -- arguments for done() function
76 *
77 * The atapi_request_callback() function puts the op in the queue of ATAPI
78 * commands for the IDE controller, starts the controller, then returns.
79 * When the operation finishes, then the callback function done()
80 * will be called on the interrupt level.
81 * The function is designed to be callable from the interrupt phase.
82 * The done() functions is called with the following arguments:
83 * (void) (*done) (x, y, count, errcode)
84 * void *x, *y; -- arguments from the atapi_request_callback()
85 * int count; -- the data residual count
86 * struct atapires errcode; -- error code structure, see above
87 *
88 * The new driver could be added in three steps:
89 * 1. Add entries for the new driver to bdevsw and cdevsw tables in conf.c.
90 * You will need to make at least three routines: open(), close(),
91 * strategy() and possibly ioctl().
92 * 2. Make attach() routine, which should allocate all the needed data
93 * structures and print the device description string (see xxxattach()).
94 * 3. Add an appropriate case to the switch in atapi_attach() routine,
95 * call attach() routine of the new driver here. Add the appropriate
96 * #include line at the top of attach.c.
97 * That's all!
98 *
99 * Use #define DEBUG in atapi.c to enable tracing of all i/o operations
100 * on the IDE bus.
101 */
102 #undef DEBUG
103
104 #include "wdc.h"
105
106 #include "wcd.h"
107 #include "wfd.h"
108 #include "wst.h"
109 /* #include "wmd.h" -- add your driver here */
110
111 #if NWDC > 0
112
113 #include <sys/param.h>
114 #include <sys/systm.h>
115 #include <sys/malloc.h>
116
117
118 #include <pc98/pc98/atapi.h>
119
120 /* this code is compiled part of the module */
121
122 #ifdef DEBUG
123 # define print(s) printf s
124 #else
125 # define print(s) {/*void*/}
126 #endif
127
128 /*
129 * ATAPI packet command phase.
130 */
131 #define PHASE_CMDOUT (ARS_DRQ | ARI_CMD)
132 #define PHASE_DATAIN (ARS_DRQ | ARI_IN)
133 #define PHASE_DATAOUT ARS_DRQ
134 #define PHASE_COMPLETED (ARI_IN | ARI_CMD)
135 #define PHASE_ABORTED 0 /* nonstandard - for NEC 260 */
136
137 static struct atapi atapitab[NWDC];
138
139 static struct atapi_params *atapi_probe (int port, int unit);
140 static int atapi_wait (int port, u_char bits_wanted);
141 static void atapi_send_cmd (struct atapi *ata, struct atapicmd *ac);
142 static int atapi_io (struct atapi *ata, struct atapicmd *ac);
143 static int atapi_start_cmd (struct atapi *ata, struct atapicmd *ac);
144 static int atapi_wait_cmd (struct atapi *ata, struct atapicmd *ac);
145
146 extern void wdstart (int ctrlr);
147 extern int acdattach(struct atapi*, int, struct atapi_params*, int);
148 extern int wfdattach(struct atapi*, int, struct atapi_params*, int);
149 extern int wstattach(struct atapi*, int, struct atapi_params*, int);
150
151 /*
152 * Probe the ATAPI device at IDE controller `ctlr', drive `unit'.
153 * Called at splbio().
154 */
155 int atapi_attach (int ctlr, int unit, int port)
156 {
157 struct atapi *ata = atapitab + ctlr;
158 struct atapi_params *ap;
159 char buf [sizeof(ap->model) + 1];
160 char revbuf [sizeof(ap->revision) + 1];
161 struct atapicmd *ac;
162
163 print (("atapi%d.%d at 0x%x: attach called\n", ctlr, unit, port));
164 ap = atapi_probe (port, unit);
165 if (! ap)
166 return (0);
167
168 bcopy (ap->model, buf, sizeof(buf)-1);
169 buf[sizeof(buf)-1] = 0;
170
171 bcopy (ap->revision, revbuf, sizeof(revbuf)-1);
172 revbuf[sizeof(revbuf)-1] = 0;
173
174 printf ("wdc%d: unit %d (atapi): <%s/%s>", ctlr, unit, buf, revbuf);
175
176 /* device is removable */
177 if (ap->removable)
178 printf (", removable");
179
180 /* packet command size */
181 switch (ap->cmdsz) {
182 case AT_PSIZE_12: break;
183 case AT_PSIZE_16: printf (", cmd16"); ata->cmd16 = 1; break;
184 default: printf (", cmd%d", ap->cmdsz);
185 }
186
187 /* DRQ type */
188 switch (ap->drqtype) {
189 case AT_DRQT_MPROC: ata->slow = 1; break;
190 case AT_DRQT_INTR: printf (", intr"); ata->intrcmd = 1; break;
191 case AT_DRQT_ACCEL: printf (", accel"); ata->accel = 1; break;
192 default: printf (", drq%d", ap->drqtype);
193 }
194 if (ata->slow)
195 ata->intrcmd = 0;
196
197 /*
198 * If we have two devices, one supporting INTR and one ACCEL, we
199 * have to pessimise - clear INTR and set slow.
200 */
201 if (ata->accel && ata->intrcmd) {
202 ata->intrcmd = 0;
203 ata->slow = 1;
204 }
205
206 /* overlap operation supported */
207 if (ap->ovlapflag)
208 printf (", ovlap");
209
210 /* interleaved DMA supported */
211 if (ap->idmaflag)
212 printf (", idma");
213 /* DMA supported */
214 else if (ap->dmaflag)
215 printf (", dma");
216
217 /* IORDY can be disabled */
218 if (ap->iordydis)
219 printf (", iordis");
220 /* IORDY supported */
221 else if (ap->iordyflag)
222 printf (", iordy");
223
224 printf ("\n");
225
226 ata->port = port;
227 ata->ctrlr = ctlr;
228 ata->attached[unit] = 0;
229 #ifdef DEBUG
230 ata->debug = 1;
231 #else
232 ata->debug = 0;
233 #endif
234 /* Initialize free queue. */
235 ata->cmdrq[15].next = 0;
236 for (ac = ata->cmdrq+14; ac >= ata->cmdrq; --ac)
237 ac->next = ac+1;
238 ata->free = ata->cmdrq;
239
240 if (ap->proto != AT_PROTO_ATAPI) {
241 printf ("wdc%d: unit %d: unknown ATAPI protocol=%d\n",
242 ctlr, unit, ap->proto);
243 free (ap, M_TEMP);
244 return (0);
245 }
246 switch (ap->devtype) {
247 default:
248 /* unknown ATAPI device */
249 printf ("wdc%d: unit %d: unknown ATAPI type=%d\n",
250 ctlr, unit, ap->devtype);
251 break;
252
253 case AT_TYPE_DIRECT: /* direct-access */
254 #if NWFD > 0
255 /* ATAPI Floppy(LS-120) */
256 if (wfdattach (ata, unit, ap, ata->debug) >= 0) {
257 /* Device attached successfully. */
258 ata->attached[unit] = 1;
259 return (1);
260 }
261 #endif
262 #if NWCD > 0
263 /* FALLTHROUGH */
264 #else
265 printf ("wdc%d: ATAPI Floppies not configured\n", ctlr);
266 break;
267 #endif
268 case AT_TYPE_CDROM: /* CD-ROM device */
269 #if NWCD > 0
270 /* ATAPI CD-ROM & CD-R/RW drives */
271 if (acdattach (ata, unit, ap, ata->debug) < 0)
272 break;
273 ata->attached[unit] = 1;
274 return (1);
275 #else
276 printf ("wdc%d: ATAPI CD-ROMs not configured\n", ctlr);
277 break;
278 #endif
279
280 case AT_TYPE_TAPE: /* streaming tape */
281 #if NWST > 0
282 /* ATAPI Streaming Tape */
283 if (wstattach (ata, unit, ap, ata->debug) < 0)
284 break;
285 /* Device attached successfully. */
286 ata->attached[unit] = 1;
287 return (1);
288 #else
289 printf ("wdc%d: ATAPI streaming tapes not configured\n", ctlr);
290 #endif
291 break;
292
293 case AT_TYPE_OPTICAL: /* optical disk */
294 #if NWMD > 0
295 /* Add your driver here */
296 #else
297 printf ("wdc%d: ATAPI optical disks not supported yet\n", ctlr);
298 #endif
299 break;
300 }
301 /* Attach failed. */
302 free (ap, M_TEMP);
303 return (0);
304 }
305
306 static char *cmdname (u_char cmd)
307 {
308 static char buf[8];
309
310 switch (cmd) {
311 case 0x00: return ("TEST_UNIT_READY");
312 case 0x01: return ("REZERO_UNIT");
313 case 0x03: return ("REQUEST_SENSE");
314 case 0x04: return ("FORMAT_UNIT");
315 case 0x1b: return ("START_STOP");
316 case 0x1e: return ("PREVENT_ALLOW");
317 case 0x25: return ("READ_CAPACITY");
318 case 0x28: return ("READ_BIG");
319 case 0x2a: return ("WRITE_BIG");
320 case 0x35: return ("SYNCHRONIZE_CACHE");
321 case 0x42: return ("READ_SUBCHANNEL");
322 case 0x43: return ("READ_TOC");
323 case 0x51: return ("READ_DISC_INFO");
324 case 0x52: return ("READ_TRACK_INFO");
325 case 0x53: return ("RESERVE_TRACK");
326 case 0x54: return ("SEND_OPC_INFO");
327 case 0x55: return ("MODE_SELECT");
328 case 0x58: return ("REPAIR_TRACK");
329 case 0x59: return ("READ_MASTER_CUE");
330 case 0x5a: return ("MODE_SENSE");
331 case 0x5b: return ("CLOSE_TRACK/SESSION");
332 case 0x5c: return ("READ_BUFFER_CAPACITY");
333 case 0x5d: return ("SEND_CUE_SHEET");
334 case 0x47: return ("PLAY_MSF");
335 case 0x4b: return ("PAUSE");
336 case 0x48: return ("PLAY_TRACK");
337 case 0xa1: return ("BLANK_CMD");
338 case 0xa5: return ("PLAY_BIG");
339 case 0xb4: return ("PLAY_CD");
340 case 0xbd: return ("ATAPI_MECH_STATUS");
341 case 0xbe: return ("READ_CD");
342 }
343 snprintf (buf, sizeof(buf), "[0x%x]", cmd);
344 return (buf);
345 }
346
347 static void bswap (char *buf, int len)
348 {
349 u_short *p = (u_short*) (buf + len);
350 while (--p >= (u_short*) buf)
351 *p = ntohs (*p);
352 }
353
354 static void btrim (char *buf, int len)
355 {
356 char *p;
357
358 /* Remove the trailing spaces. */
359 for (p=buf; p<buf+len; ++p)
360 if (! *p)
361 *p = ' ';
362 for (p=buf+len-1; p>=buf && *p==' '; --p)
363 *p = 0;
364 }
365
366 /*
367 * Issue IDENTIFY command to ATAPI drive to ask it what it is.
368 */
369 static struct atapi_params *atapi_probe (int port, int unit)
370 {
371 struct atapi_params *ap;
372 char tb [DEV_BSIZE];
373 #ifdef PC98
374 int cnt;
375
376 outb(0x432,unit%2);
377 print(("unit = %d,select %d\n",unit,unit%2));
378 #endif
379 /* Wait for controller not busy. */
380 #ifdef PC98
381 outb (port + AR_DRIVE, unit / 2 ? ARD_DRIVE1 : ARD_DRIVE0);
382 #else
383 outb (port + AR_DRIVE, unit ? ARD_DRIVE1 : ARD_DRIVE0);
384 #endif
385 if (atapi_wait (port, 0) < 0) {
386 print (("atapiX.%d at 0x%x: controller busy, status=%b\n",
387 unit, port, inb (port + AR_STATUS), ARS_BITS));
388 return (0);
389 }
390
391 /* Issue ATAPI IDENTIFY command. */
392 #ifdef PC98
393 outb (port + AR_DRIVE, unit/2 ? ARD_DRIVE1 : ARD_DRIVE0);
394
395 /* Wait for DRQ deassert. */
396 for (cnt=2000; cnt>0; --cnt)
397 if (! (inb (port + AR_STATUS) & ARS_DRQ))
398 break;
399
400 outb (port + AR_COMMAND, ATAPIC_IDENTIFY);
401 DELAY(500);
402 #else
403 outb (port + AR_DRIVE, unit ? ARD_DRIVE1 : ARD_DRIVE0);
404 outb (port + AR_COMMAND, ATAPIC_IDENTIFY);
405 #endif
406
407 /* Check that device is present. */
408 if (inb (port + AR_STATUS) == 0xff) {
409 print (("atapiX.%d at 0x%x: no device\n", unit, port));
410 #ifdef PC98
411 if (unit / 2)
412 #else
413 if (unit == 1)
414 #endif
415 /* Select unit 0. */
416 outb (port + AR_DRIVE, ARD_DRIVE0);
417 return (0);
418 }
419
420 /* Wait for data ready. */
421 if (atapi_wait (port, ARS_DRQ) != 0) {
422 print (("atapiX.%d at 0x%x: identify not ready, status=%b\n",
423 unit, port, inb (port + AR_STATUS), ARS_BITS));
424 #ifdef PC98
425 if (unit / 2)
426 #else
427 if (unit == 1)
428 #endif
429 /* Select unit 0. */
430 outb (port + AR_DRIVE, ARD_DRIVE0);
431 return (0);
432 }
433
434 /* check that DRQ isn't a fake */
435 if (inb (port + AR_STATUS) == 0xff) {
436 print (("atapiX.%d at 0x%x: no device\n", unit, port));
437 #ifdef PC98
438 if (unit / 2)
439 #else
440 if (unit == 1)
441 #endif
442 /* Select unit 0. */
443 outb (port + AR_DRIVE, ARD_DRIVE0);
444 return (0);
445 }
446
447 /* Obtain parameters. */
448 insw (port + AR_DATA, tb, sizeof(tb) / sizeof(short));
449
450 ap = malloc (sizeof *ap, M_TEMP, M_NOWAIT);
451 if (! ap)
452 return (0);
453 bcopy (tb, ap, sizeof *ap);
454
455 #ifdef PC98
456 /*
457 * Check model string.
458 * If all of it makes unprintable characters, ignore this device.
459 */
460 for (cnt = 0; cnt < sizeof(ap->model)-1; cnt++) {
461 if (ap->model[cnt] >= ' ')
462 break;
463 }
464 if (cnt >= sizeof(ap->model)-1) {
465 free (ap, M_TEMP);
466 return (0);
467 }
468 #endif
469
470 /*
471 * Shuffle string byte order.
472 * Mitsumi and NEC drives don't need this.
473 */
474 if (! ((ap->model[0] == 'N' && ap->model[1] == 'E') ||
475 (ap->model[0] == 'F' && ap->model[1] == 'X')))
476 bswap (ap->model, sizeof(ap->model));
477 bswap (ap->serial, sizeof(ap->serial));
478 bswap (ap->revision, sizeof(ap->revision));
479
480 /* Clean up the model name, serial and revision numbers. */
481 btrim (ap->model, sizeof(ap->model));
482 btrim (ap->serial, sizeof(ap->serial));
483 btrim (ap->revision, sizeof(ap->revision));
484 return (ap);
485 }
486
487 /*
488 * Wait uninterruptibly until controller is not busy and certain
489 * status bits are set.
490 * The wait is usually short unless it is for the controller to process
491 * an entire critical command.
492 * Return 1 for (possibly stale) controller errors, -1 for timeout errors,
493 * or 0 for no errors.
494 */
495 static int atapi_wait (int port, u_char bits_wanted)
496 {
497 int cnt;
498 u_char s;
499
500 /* Wait 5 sec for BUSY deassert. */
501 for (cnt=500000; cnt>0; --cnt) {
502 s = inb (port + AR_STATUS);
503 if (! (s & ARS_BSY))
504 break;
505 DELAY (10);
506 }
507 if (cnt <= 0)
508 return (-1);
509 if (! bits_wanted)
510 return (s & ARS_CHECK);
511
512 /* Wait 50 msec for bits wanted. */
513 for (cnt=5000; cnt>0; --cnt) {
514 s = inb (port + AR_STATUS);
515 if ((s & bits_wanted) == bits_wanted)
516 return (s & ARS_CHECK);
517 DELAY (10);
518 }
519 return (-1);
520 }
521
522 void atapi_debug (struct atapi *ata, int on)
523 {
524 ata->debug = on;
525 }
526
527 static struct atapicmd *atapi_alloc (struct atapi *ata)
528 {
529 struct atapicmd *ac;
530
531 while (! ata->free)
532 tsleep ((caddr_t)ata, PRIBIO, "atacmd", 100);
533 ac = ata->free;
534 ata->free = ac->next;
535 ac->busy = 1;
536 return (ac);
537 }
538
539 static void atapi_free (struct atapi *ata, struct atapicmd *ac)
540 {
541 if (! ata->free)
542 wakeup ((caddr_t)ata);
543 ac->busy = 0;
544 ac->next = ata->free;
545 ata->free = ac;
546 }
547
548 /*
549 * Add new command request to the end of the queue.
550 */
551 static void atapi_enqueue (struct atapi *ata, struct atapicmd *ac)
552 {
553 ac->next = 0;
554 if (ata->tail)
555 ata->tail->next = ac;
556 else
557 ata->queue = ac;
558 ata->tail = ac;
559 }
560
561 static void atapi_done (struct atapi *ata)
562 {
563 struct atapicmd *ac = ata->queue;
564
565 if (! ac)
566 return; /* cannot happen */
567
568 ata->queue = ac->next;
569 if (! ata->queue)
570 ata->tail = 0;
571
572 if (ac->callback) {
573 (*ac->callback) (ac->cbarg1, ac->cbarg2, ac->count, ac->result);
574 atapi_free (ata, ac);
575 } else
576 wakeup ((caddr_t)ac);
577 }
578
579 /*
580 * Start new packet op. Called from wdstart().
581 * Return 1 if op started, and we are waiting for interrupt.
582 * Return 0 when idle.
583 */
584 int atapi_start (int ctrlr)
585 {
586 struct atapi *ata = atapitab + ctrlr;
587 struct atapicmd *ac;
588 again:
589 ac = ata->queue;
590 if (! ac)
591 return (0);
592
593 /* Start packet command. */
594 if (atapi_start_cmd (ata, ac) < 0) {
595 atapi_done (ata);
596 goto again;
597 }
598
599 if (ata->intrcmd)
600 /* Wait for interrupt before sending packet command */
601 return (1);
602
603 /* Wait for DRQ. */
604 if (atapi_wait_cmd (ata, ac) < 0) {
605 atapi_done (ata);
606 goto again;
607 }
608
609 /* Send packet command. */
610 atapi_send_cmd (ata, ac);
611 return (1);
612 }
613
614 /*
615 * Start new packet op. Returns -1 on errors.
616 */
617 int atapi_start_cmd (struct atapi *ata, struct atapicmd *ac)
618 {
619 ac->result.error = 0;
620 ac->result.status = 0;
621
622 #ifdef PC98
623 outb(0x432,(ac->unit)%2);
624 print(("(ac->unit) = %d,select %d (2) \n",(ac->unit),(ac->unit)%2));
625 outb (ata->port + AR_DRIVE, (ac->unit)/2 ? ARD_DRIVE1 : ARD_DRIVE0);
626 #else
627 outb (ata->port + AR_DRIVE, ac->unit ? ARD_DRIVE1 : ARD_DRIVE0);
628 #endif
629 if (atapi_wait (ata->port, 0) < 0) {
630 printf ("atapi%d.%d: controller not ready for cmd\n",
631 ata->ctrlr, ac->unit);
632 ac->result.code = RES_NOTRDY;
633 return (-1);
634 }
635
636 /* Set up the controller registers. */
637 outb (ata->port + AR_FEATURES, 0);
638 outb (ata->port + AR_IREASON, 0);
639 outb (ata->port + AR_TAG, 0);
640 outb (ata->port + AR_CNTLO, ac->count & 0xff);
641 outb (ata->port + AR_CNTHI, ac->count >> 8);
642 outb (ata->port + AR_COMMAND, ATAPIC_PACKET);
643
644 if (ata->debug)
645 printf ("atapi%d.%d: start\n", ata->ctrlr, ac->unit);
646 return (0);
647 }
648
649 /*
650 * Wait for DRQ before sending packet cmd. Returns -1 on errors.
651 */
652 int atapi_wait_cmd (struct atapi *ata, struct atapicmd *ac)
653 {
654 /* Wait for DRQ from 100 usec to 3 msec for slow devices */
655 int cnt = ata->intrcmd ? 10000 : ata->slow ? 3000 : 100;
656 int ireason = 0, phase = 0;
657
658 /* Wait for command phase. */
659 for (; cnt>0; cnt-=10) {
660 ireason = inb (ata->port + AR_IREASON);
661 ac->result.status = inb (ata->port + AR_STATUS);
662 phase = (ireason & (ARI_CMD | ARI_IN)) |
663 (ac->result.status & (ARS_DRQ | ARS_BSY));
664 if (phase == PHASE_CMDOUT)
665 break;
666 DELAY (10);
667 }
668
669 if (phase != PHASE_CMDOUT) {
670 ac->result.code = RES_NODRQ;
671 ac->result.error = inb (ata->port + AR_ERROR);
672 printf ("atapi%d.%d: invalid command phase, ireason=0x%x, status=%b, error=%b\n",
673 ata->ctrlr, ac->unit, ireason,
674 ac->result.status, ARS_BITS,
675 ac->result.error, AER_BITS);
676 return (-1);
677 }
678 return (0);
679 }
680
681 /*
682 * Send packet cmd.
683 */
684 void atapi_send_cmd (struct atapi *ata, struct atapicmd *ac)
685 {
686 outsw (ata->port + AR_DATA, ac->cmd, ata->cmd16 ? 8 : 6);
687 if (ata->debug)
688 printf ("atapi%d.%d: send cmd %s %x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x\n",
689 ata->ctrlr, ac->unit, cmdname (ac->cmd[0]), ac->cmd[0],
690 ac->cmd[1], ac->cmd[2], ac->cmd[3], ac->cmd[4],
691 ac->cmd[5], ac->cmd[6], ac->cmd[7], ac->cmd[8],
692 ac->cmd[9], ac->cmd[10], ac->cmd[11], ac->cmd[12],
693 ac->cmd[13], ac->cmd[14], ac->cmd[15]);
694 }
695
696 /*
697 * Interrupt routine for the controller. Called from wdintr().
698 * Finish the started op, wakeup wait-type commands,
699 * run callbacks for callback-type commands, then return.
700 * Do not start new op here, it will be done by wdstart,
701 * which is called just after us.
702 * Return 1 if op continues, and we are waiting for new interrupt.
703 * Return 0 when idle.
704 */
705 int atapi_intr (int ctrlr)
706 {
707 struct atapi *ata = atapitab + ctrlr;
708 struct atapicmd *ac = ata->queue;
709
710 #ifdef PC98
711 outb(0x432,(ac->unit)%2);
712 print(("atapi_intr:(ac->unit)= %d,select %d\n",ac->unit,(ac->unit)%2));
713 #endif
714
715 if (! ac) {
716 printf ("atapi%d: stray interrupt\n", ata->ctrlr);
717 return (0);
718 }
719 if (atapi_io (ata, ac) > 0)
720 return (1);
721 atapi_done (ata);
722 return (0);
723 }
724
725 /*
726 * Process the i/o phase, transferring the command/data to/from the device.
727 * Return 1 if op continues, and we are waiting for new interrupt.
728 * Return 0 when idle.
729 */
730 int atapi_io (struct atapi *ata, struct atapicmd *ac)
731 {
732 u_char ireason;
733 u_short len, i;
734
735 if (atapi_wait (ata->port, 0) < 0) {
736 ac->result.status = inb (ata->port + AR_STATUS);
737 ac->result.error = inb (ata->port + AR_ERROR);
738 ac->result.code = RES_NOTRDY;
739 printf ("atapi%d.%d: controller not ready, status=%b, error=%b\n",
740 ata->ctrlr, ac->unit, ac->result.status, ARS_BITS,
741 ac->result.error, AER_BITS);
742 return (0);
743 }
744
745 ac->result.status = inb (ata->port + AR_STATUS);
746 ac->result.error = inb (ata->port + AR_ERROR);
747 len = inb (ata->port + AR_CNTLO);
748 len |= inb (ata->port + AR_CNTHI) << 8;
749 ireason = inb (ata->port + AR_IREASON);
750
751 if (ata->debug) {
752 printf ("atapi%d.%d: intr ireason=0x%x, len=%d, status=%b, error=%b\n",
753 ata->ctrlr, ac->unit, ireason, len,
754 ac->result.status, ARS_BITS,
755 ac->result.error, AER_BITS);
756 }
757 switch ((ireason & (ARI_CMD | ARI_IN)) | (ac->result.status & ARS_DRQ)) {
758 default:
759 printf ("atapi%d.%d: unknown phase\n", ata->ctrlr, ac->unit);
760 ac->result.code = RES_ERR;
761 break;
762
763 case PHASE_CMDOUT:
764 /* Send packet command. */
765 if (! (ac->result.status & ARS_DRQ)) {
766 printf ("atapi%d.%d: no cmd drq\n",
767 ata->ctrlr, ac->unit);
768 ac->result.code = RES_NODRQ;
769 break;
770 }
771 atapi_send_cmd (ata, ac);
772 return (1);
773
774 case PHASE_DATAOUT:
775 /* Write data */
776 if (ac->count > 0) {
777 printf ("atapi%d.%d: invalid data direction\n",
778 ata->ctrlr, ac->unit);
779 ac->result.code = RES_INVDIR;
780 break;
781 }
782 if (-ac->count < len) {
783 print (("atapi%d.%d: send data underrun, %d bytes left\n",
784 ata->ctrlr, ac->unit, -ac->count));
785 ac->result.code = RES_UNDERRUN;
786 outsw (ata->port + AR_DATA, ac->addr,
787 -ac->count / sizeof(short));
788 for (i= -ac->count; i<len; i+=sizeof(short))
789 outw (ata->port + AR_DATA, 0);
790 } else
791 outsw (ata->port + AR_DATA, ac->addr,
792 len / sizeof(short));
793 ac->addr += len;
794 ac->count += len;
795 return (1);
796
797 case PHASE_DATAIN:
798 /* Read data */
799 if (ac->count < 0) {
800 printf ("atapi%d.%d: invalid data direction\n",
801 ata->ctrlr, ac->unit);
802 ac->result.code = RES_INVDIR;
803 break;
804 }
805 if (ac->count < len) {
806 print (("atapi%d.%d: recv data overrun, %d bytes left\n",
807 ata->ctrlr, ac->unit, ac->count));
808 ac->result.code = RES_OVERRUN;
809 insw (ata->port + AR_DATA, ac->addr,
810 ac->count / sizeof(short));
811 for (i=ac->count; i<len; i+=sizeof(short))
812 inw (ata->port + AR_DATA);
813 } else
814 insw (ata->port + AR_DATA, ac->addr,
815 len / sizeof(short));
816 ac->addr += len;
817 ac->count -= len;
818 return (1);
819
820 case PHASE_ABORTED:
821 case PHASE_COMPLETED:
822 if (ac->result.status & (ARS_CHECK | ARS_DF))
823 ac->result.code = RES_ERR;
824 else if (ac->count < 0) {
825 print (("atapi%d.%d: send data overrun, %d bytes left\n",
826 ata->ctrlr, ac->unit, -ac->count));
827 ac->result.code = RES_OVERRUN;
828 } else if (ac->count > 0) {
829 print (("atapi%d.%d: recv data underrun, %d bytes left\n",
830 ata->ctrlr, ac->unit, ac->count));
831 ac->result.code = RES_UNDERRUN;
832 bzero (ac->addr, ac->count);
833 } else
834 ac->result.code = RES_OK;
835 break;
836 }
837 return (0);
838 }
839
840 /*
841 * Queue new packet request, then call wdstart().
842 * Called on splbio().
843 */
844 void atapi_request_callback (struct atapi *ata, int unit,
845 u_char cmd, u_char a1, u_char a2, u_char a3, u_char a4,
846 u_char a5, u_char a6, u_char a7, u_char a8, u_char a9,
847 u_char a10, u_char a11, u_char a12, u_char a13, u_char a14, u_char a15,
848 char *addr, int count, atapi_callback_t *done, void *x, void *y)
849 {
850 struct atapicmd *ac;
851
852 ac = atapi_alloc (ata);
853 ac->cmd[0] = cmd; ac->cmd[1] = a1;
854 ac->cmd[2] = a2; ac->cmd[3] = a3;
855 ac->cmd[4] = a4; ac->cmd[5] = a5;
856 ac->cmd[6] = a6; ac->cmd[7] = a7;
857 ac->cmd[8] = a8; ac->cmd[9] = a9;
858 ac->cmd[10] = a10; ac->cmd[11] = a11;
859 ac->cmd[12] = a12; ac->cmd[13] = a13;
860 ac->cmd[14] = a14; ac->cmd[15] = a15;
861 ac->unit = unit;
862 ac->addr = addr;
863 ac->count = count;
864 ac->callback = done;
865 ac->cbarg1 = x;
866 ac->cbarg2 = y;
867
868 if (ata->debug)
869 printf ("atapi%d.%d: req cb %x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x len=%d\n",
870 ata->ctrlr, ac->unit, ac->cmd[0], ac->cmd[1],
871 ac->cmd[2], ac->cmd[3], ac->cmd[4], ac->cmd[5],
872 ac->cmd[6], ac->cmd[7], ac->cmd[8], ac->cmd[9],
873 ac->cmd[10], ac->cmd[11], ac->cmd[12],
874 ac->cmd[13], ac->cmd[14], ac->cmd[15], count);
875 atapi_enqueue (ata, ac);
876 wdstart (ata->ctrlr);
877 }
878
879 /*
880 * Queue new packet request, then call wdstart().
881 * Wait until the request is finished.
882 * Called on spl0().
883 * Return atapi error.
884 * Buffer pointed to by *addr should be placed in core memory, not in stack!
885 */
886 struct atapires atapi_request_wait (struct atapi *ata, int unit,
887 u_char cmd, u_char a1, u_char a2, u_char a3, u_char a4,
888 u_char a5, u_char a6, u_char a7, u_char a8, u_char a9,
889 u_char a10, u_char a11, u_char a12, u_char a13, u_char a14, u_char a15,
890 char *addr, int count)
891 {
892 struct atapicmd *ac;
893 int x = splbio ();
894 struct atapires result;
895
896 ac = atapi_alloc (ata);
897 ac->cmd[0] = cmd; ac->cmd[1] = a1;
898 ac->cmd[2] = a2; ac->cmd[3] = a3;
899 ac->cmd[4] = a4; ac->cmd[5] = a5;
900 ac->cmd[6] = a6; ac->cmd[7] = a7;
901 ac->cmd[8] = a8; ac->cmd[9] = a9;
902 ac->cmd[10] = a10; ac->cmd[11] = a11;
903 ac->cmd[12] = a12; ac->cmd[13] = a13;
904 ac->cmd[14] = a14; ac->cmd[15] = a15;
905 ac->unit = unit;
906 ac->addr = addr;
907 ac->count = count;
908 ac->callback = 0;
909 ac->cbarg1 = 0;
910 ac->cbarg2 = 0;
911
912 if (ata->debug)
913 printf ("atapi%d.%d: req w %x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x len=%d\n",
914 ata->ctrlr, ac->unit, ac->cmd[0], ac->cmd[1],
915 ac->cmd[2], ac->cmd[3], ac->cmd[4], ac->cmd[5],
916 ac->cmd[6], ac->cmd[7], ac->cmd[8], ac->cmd[9],
917 ac->cmd[10], ac->cmd[11], ac->cmd[12],
918 ac->cmd[13], ac->cmd[14], ac->cmd[15], count);
919 atapi_enqueue (ata, ac);
920 wdstart (ata->ctrlr);
921 if (ata->tail == ac)
922 tsleep ((caddr_t)ac, PRIBIO, "atareq", 0);
923
924 result = ac->result;
925 atapi_free (ata, ac);
926 splx (x);
927 return (result);
928 }
929
930 /*
931 * Perform a packet command on the device.
932 * Should be called on splbio().
933 * Return atapi error.
934 */
935 struct atapires atapi_request_immediate (struct atapi *ata, int unit,
936 u_char cmd, u_char a1, u_char a2, u_char a3, u_char a4,
937 u_char a5, u_char a6, u_char a7, u_char a8, u_char a9,
938 u_char a10, u_char a11, u_char a12, u_char a13, u_char a14, u_char a15,
939 char *addr, int count)
940 {
941 struct atapicmd cmdbuf, *ac = &cmdbuf;
942 int cnt;
943
944 ac->cmd[0] = cmd; ac->cmd[1] = a1;
945 ac->cmd[2] = a2; ac->cmd[3] = a3;
946 ac->cmd[4] = a4; ac->cmd[5] = a5;
947 ac->cmd[6] = a6; ac->cmd[7] = a7;
948 ac->cmd[8] = a8; ac->cmd[9] = a9;
949 ac->cmd[10] = a10; ac->cmd[11] = a11;
950 ac->cmd[12] = a12; ac->cmd[13] = a13;
951 ac->cmd[14] = a14; ac->cmd[15] = a15;
952 ac->unit = unit;
953 ac->addr = addr;
954 ac->count = count;
955 ac->callback = 0;
956 ac->cbarg1 = 0;
957 ac->cbarg2 = 0;
958
959 if (ata->debug)
960 printf ("atapi%d.%d: req im %x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x len=%d\n",
961 ata->ctrlr, ac->unit, ac->cmd[0], ac->cmd[1],
962 ac->cmd[2], ac->cmd[3], ac->cmd[4], ac->cmd[5],
963 ac->cmd[6], ac->cmd[7], ac->cmd[8], ac->cmd[9],
964 ac->cmd[10], ac->cmd[11], ac->cmd[12],
965 ac->cmd[13], ac->cmd[14], ac->cmd[15], count);
966
967 /* Start packet command, wait for DRQ. */
968 if (atapi_start_cmd (ata, ac) >= 0 && atapi_wait_cmd (ata, ac) >= 0) {
969 /* Send packet command. */
970 atapi_send_cmd (ata, ac);
971
972 /* Wait for data i/o phase. */
973 for (cnt=20000; cnt>0; --cnt)
974 if (((inb (ata->port + AR_IREASON) & (ARI_CMD | ARI_IN)) |
975 (inb (ata->port + AR_STATUS) & ARS_DRQ)) != PHASE_CMDOUT)
976 break;
977
978 /* Do all needed i/o. */
979 while (atapi_io (ata, ac))
980 /* Wait for DRQ deassert. */
981 for (cnt=2000; cnt>0; --cnt) {
982 if (! (inb (ata->port + AR_STATUS) & ARS_DRQ))
983 break;
984 DELAY(10);
985 }
986 }
987 return (ac->result);
988 }
989
990 #endif /* NWDC */
Cache object: 7d4ed2627ea88ab1d4bc47fb8f353838
|